#include <stdio.h>

struct s1 {
    int a, b;
    ~s1() { printf("  s1 deleted\n"); }
};

class Pointer_s1 {
    s1 *p;
public:
    Pointer_s1(s1 *ptr = 0) : p(ptr) {}
    ~Pointer_s1() { if (p) delete p; }
    s1 *operator=(s1 *ptr) {
        if (p)
            delete p;
        p = ptr;
        return p;
    }
    s1 &operator*() const { return *p; }
    s1 *operator->() const { return p; }
private:
    Pointer_s1(const Pointer_s1 &);             /* no copying */
    void operator=(const Pointer_s1 &);         /* no assignments */
};

int with_raw(int x)                 /* plain pointer */
{
    s1 *p = new s1;

    p->a = x;
    if (p->a < 0)
        return -1;                  /* forgot delete: leak */
    p->b = p->a + 36;
    if (p->b > 100)
        return 1;                   /* forgot delete: leak */
    int r = p->b;
    delete p;                       /* only this path frees */
    return r;
}

int with_owner(int x)               /* Pointer_s1 */
{
    Pointer_s1 p(new s1);

    p->a = x;
    if (p->a < 0)
        return -1;                  /* destructor deletes */
    p->b = p->a + 36;
    if (p->b > 100)
        return 1;                   /* destructor deletes */
    return p->b;                    /* destructor deletes */
}

int main()
{
    printf("with_raw(-5):\n");
    with_raw(-5);
    printf("with_raw(90):\n");
    with_raw(90);
    printf("with_owner(-5):\n");
    with_owner(-5);
    printf("with_owner(90):\n");
    with_owner(90);
    return 0;
}
