/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Crew log: each seat holds 1 when occupied and 0 when empty. */
#include <cmoc.h>
#include <stdlib.h>

static unsigned char storage[96];

int main(void)
{
    unsigned char *seats, *larger;
    if (!vcc_heap_init(storage, sizeof(storage)))
        return 1;

    seats = (unsigned char *) calloc(2, sizeof(*seats));
    if (!seats)
        return 1;
    printf("CREW AT LAUNCH: %u\n", (unsigned)(seats[0] + seats[1]));
    seats[0] = seats[1] = 1;

    /* Keep the current seat list until we know it can grow. */
    larger = (unsigned char *) realloc(seats, 3);
    if (!larger)
    {
        free(seats);
        return 1;
    }
    seats = larger;
    seats[2] = 1;
    printf("CREW AFTER GROWTH: %u\n", (unsigned)(seats[0] + seats[1] + seats[2]));

    /* The request exceeds the entire 96-byte budget. */
    larger = (unsigned char *) realloc(seats, 200);
    if (larger)
    {
        free(larger);
        return 1;
    }
    printf("NO ROOM FOR 200\n");
    printf("CREW STILL ABOARD: %u\n", (unsigned)(seats[0] + seats[1] + seats[2]));
    free(seats);
    printf("CREW LOG CLOSED\n");
    return 0;
}
