/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

struct Data
{
};

static void os_ressource_allocate()
{
    printf("OS resource allocated\n");
}

static void os_ressource_deallocate()
{
    printf("OS resource deallocated\n");
}

static int m3()
{
    struct Data *o;
    printf("Data memory allocated\n");
    o = malloc(sizeof(struct Data));
    printf("**** Error ****\n");
    printf("Data memory deallocated\n");
    free(o);
    return FALSE;
}

static int m2()
{
    os_ressource_allocate();
    if(!m3())
    {
        os_ressource_deallocate();
        return FALSE;
    }
    os_ressource_deallocate();
    printf("Normal return\n");
    return TRUE;
}

static int m1()
{
    struct Data *o;
    printf("Data memory allocated\n");
    o = malloc(sizeof(struct Data));
    if(!m2())
    {
        printf("Data memory deallocated\n");
        free(o);
        return FALSE;
    }
    printf("Data memory deallocated\n");
    free(o);
    printf("Normal return\n");
    return TRUE;
}

int main(int argc, char *argv[])
{
    if(!m1())
    {
        printf("Hopefully everything has been deallocated\n");
        return 0;
    }
    printf("Normal return\n");
    return 0;
}
