---
source: framec/tests/c_snapshots.rs
expression: "compile_fixture(\"11_consts\", \"c\")"
---
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

// ============================================================================
// Consts_FrameDict - String-keyed dictionary
// ============================================================================

typedef struct Consts_FrameDictEntry {
    char* key;
    void* value;
    struct Consts_FrameDictEntry* next;
} Consts_FrameDictEntry;

typedef struct {
    Consts_FrameDictEntry** buckets;
    int bucket_count;
    int size;
} Consts_FrameDict;

static unsigned int Consts_hash_string(const char* str) {
    unsigned int hash = 5381;
    int c;
    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

static Consts_FrameDict* Consts_FrameDict_new(void) {
    Consts_FrameDict* d = malloc(sizeof(Consts_FrameDict));
    d->bucket_count = 16;
    d->buckets = calloc(d->bucket_count, sizeof(Consts_FrameDictEntry*));
    d->size = 0;
    return d;
}

static void Consts_FrameDict_set(Consts_FrameDict* d, const char* key, void* value) {
    unsigned int idx = Consts_hash_string(key) % d->bucket_count;
    Consts_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            entry->value = value;
            return;
        }
        entry = entry->next;
    }
    Consts_FrameDictEntry* new_entry = malloc(sizeof(Consts_FrameDictEntry));
    new_entry->key = strdup(key);
    new_entry->value = value;
    new_entry->next = d->buckets[idx];
    d->buckets[idx] = new_entry;
    d->size++;
}

static void* Consts_FrameDict_get(Consts_FrameDict* d, const char* key) {
    unsigned int idx = Consts_hash_string(key) % d->bucket_count;
    Consts_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }
    return NULL;
}

static int Consts_FrameDict_has(Consts_FrameDict* d, const char* key) {
    unsigned int idx = Consts_hash_string(key) % d->bucket_count;
    Consts_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return 1;
        }
        entry = entry->next;
    }
    return 0;
}

static Consts_FrameDict* Consts_FrameDict_copy(Consts_FrameDict* src) {
    Consts_FrameDict* dst = Consts_FrameDict_new();
    for (int i = 0; i < src->bucket_count; i++) {
        Consts_FrameDictEntry* entry = src->buckets[i];
        while (entry) {
            Consts_FrameDict_set(dst, entry->key, entry->value);
            entry = entry->next;
        }
    }
    return dst;
}

static void Consts_FrameDict_destroy(Consts_FrameDict* d) {
    for (int i = 0; i < d->bucket_count; i++) {
        Consts_FrameDictEntry* entry = d->buckets[i];
        while (entry) {
            Consts_FrameDictEntry* next = entry->next;
            free(entry->key);
            free(entry);
            entry = next;
        }
    }
    free(d->buckets);
    free(d);
}

// ============================================================================
// Consts_FrameVec - Dynamic array
// ============================================================================

typedef struct {
    void** items;
    int size;
    int capacity;
} Consts_FrameVec;

static Consts_FrameVec* Consts_FrameVec_new(void) {
    Consts_FrameVec* v = malloc(sizeof(Consts_FrameVec));
    v->capacity = 8;
    v->size = 0;
    v->items = malloc(sizeof(void*) * v->capacity);
    return v;
}

static void Consts_FrameVec_push(Consts_FrameVec* v, void* item) {
    if (v->size >= v->capacity) {
        v->capacity *= 2;
        v->items = realloc(v->items, sizeof(void*) * v->capacity);
    }
    v->items[v->size++] = item;
}

static void* Consts_FrameVec_pop(Consts_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[--v->size];
}

static void* Consts_FrameVec_last(Consts_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[v->size - 1];
}

static void* Consts_FrameVec_get(Consts_FrameVec* v, int index) {
    if (index < 0 || index >= v->size) return NULL;
    return v->items[index];
}

static int Consts_FrameVec_size(Consts_FrameVec* v) {
    return v->size;
}

static void Consts_FrameVec_destroy(Consts_FrameVec* v) {
    if (!v) return;
    free(v->items);
    free(v);
}

static Consts_FrameVec* Consts_FrameVec_copy(Consts_FrameVec* src) {
    if (!src) return NULL;
    Consts_FrameVec* v = Consts_FrameVec_new();
    for (int i = 0; i < src->size; i++) {
        Consts_FrameVec_push(v, src->items[i]);
    }
    return v;
}

// ============================================================================
// Consts_pack_double / Consts_unpack_double — bit-pun doubles through void*
// ============================================================================

static inline void* Consts_pack_double(double v) {
    void* p = 0;
    memcpy(&p, &v, sizeof(double));
    return p;
}

static inline double Consts_unpack_double(void* p) {
    double d;
    memcpy(&d, &p, sizeof(double));
    return d;
}

// ============================================================================
// Consts_FrameEvent - Event routing object
// ============================================================================

typedef struct {
    const char* _message;
    Consts_FrameVec* _parameters;
    int _owns_parameters;
} Consts_FrameEvent;

static Consts_FrameEvent* Consts_FrameEvent_new(const char* message, Consts_FrameVec* parameters, int owns_parameters) {
    Consts_FrameEvent* e = malloc(sizeof(Consts_FrameEvent));
    e->_message = message;
    e->_parameters = parameters;
    e->_owns_parameters = owns_parameters;
    return e;
}

static void Consts_FrameEvent_destroy(Consts_FrameEvent* e) {
    if (e->_owns_parameters && e->_parameters) Consts_FrameVec_destroy(e->_parameters);
    free(e);
}

// ============================================================================
// Consts_FrameContext - Interface call context
// ============================================================================

typedef struct {
    Consts_FrameEvent* event;
    void* _return;
    Consts_FrameDict* _data;
    int _transitioned;
} Consts_FrameContext;

static Consts_FrameContext* Consts_FrameContext_new(Consts_FrameEvent* event, void* default_return) {
    Consts_FrameContext* ctx = malloc(sizeof(Consts_FrameContext));
    ctx->event = event;
    ctx->_return = default_return;
    ctx->_data = Consts_FrameDict_new();
    ctx->_transitioned = 0;
    return ctx;
}

static void Consts_FrameContext_destroy(Consts_FrameContext* ctx) {
    Consts_FrameDict_destroy(ctx->_data);
    free(ctx);
}

// ============================================================================
// Consts_Compartment - State closure
// ============================================================================

typedef struct Consts_Compartment {
    const char* state;
    Consts_FrameVec* state_args;
    Consts_FrameDict* state_vars;
    Consts_FrameVec* enter_args;
    Consts_FrameVec* exit_args;
    Consts_FrameEvent* forward_event;
    struct Consts_Compartment* parent_compartment;
    int _ref_count;
} Consts_Compartment;

static Consts_Compartment* Consts_Compartment_new(const char* state) {
    Consts_Compartment* c = malloc(sizeof(Consts_Compartment));
    c->state = state;
    c->state_args = Consts_FrameVec_new();
    c->state_vars = Consts_FrameDict_new();
    c->enter_args = Consts_FrameVec_new();
    c->exit_args = Consts_FrameVec_new();
    c->forward_event = NULL;
    c->parent_compartment = NULL;
    c->_ref_count = 1;
    return c;
}

static Consts_Compartment* Consts_Compartment_ref(Consts_Compartment* c) {
    if (c) c->_ref_count++;
    return c;
}

static void Consts_Compartment_unref(Consts_Compartment* c);
static void Consts_Compartment_unref(Consts_Compartment* c) {
    if (c == NULL) return;
    c->_ref_count--;
    if (c->_ref_count <= 0) {
        Consts_Compartment_unref(c->parent_compartment);
        Consts_FrameVec_destroy(c->state_args);
        Consts_FrameDict_destroy(c->state_vars);
        Consts_FrameVec_destroy(c->enter_args);
        Consts_FrameVec_destroy(c->exit_args);
        free(c);
    }
}

static Consts_Compartment* Consts_Compartment_copy(Consts_Compartment* src) {
    Consts_Compartment* c = malloc(sizeof(Consts_Compartment));
    c->state = src->state;
    c->state_args = Consts_FrameVec_copy(src->state_args);
    c->state_vars = Consts_FrameDict_copy(src->state_vars);
    c->enter_args = Consts_FrameVec_copy(src->enter_args);
    c->exit_args = Consts_FrameVec_copy(src->exit_args);
    c->forward_event = src->forward_event;  // Shallow copy OK
    c->parent_compartment = src->parent_compartment;
    return c;
}

static void Consts_Compartment_destroy(Consts_Compartment* c) {
    Consts_FrameVec_destroy(c->state_args);
    Consts_FrameDict_destroy(c->state_vars);
    Consts_FrameVec_destroy(c->enter_args);
    Consts_FrameVec_destroy(c->exit_args);
    free(c);
}

// Helper macros for context access
#define Consts_CTX(self) ((Consts_FrameContext*)Consts_FrameVec_last((self)->_context_stack))
#define Consts_PARAM(self, key) Consts_FrameDict_get(Consts_CTX(self)->event->_parameters, key)
#define Consts_RETURN(self) Consts_CTX(self)->_return
#define Consts_DATA(self, key) Consts_FrameDict_get(Consts_CTX(self)->_data, key)
#define Consts_DATA_SET(self, key, val) Consts_FrameDict_set(Consts_CTX(self)->_data, key, val)

// Forward declarations
typedef struct Consts Consts;
static void Consts_kernel(Consts* self, Consts_FrameEvent* __e);
static void Consts_router(Consts* self, Consts_FrameEvent* __e);
static void Consts_transition(Consts* self, Consts_Compartment* next);
static int Consts_hsm_chain(Consts* self, const char* leaf, const char*** out_chain);
static Consts_Compartment* Consts_prepareEnter(Consts* self, const char* leaf, Consts_FrameVec* state_args, Consts_FrameVec* enter_args);
static void Consts_prepareExit(Consts* self, Consts_FrameVec* exit_args);
static void Consts_state_Running(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment);
static void Consts_s_Running_hdl_user_get_count(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment);
static void Consts_s_Running_hdl_user_tick(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment);
void Consts_tick (Consts* self);
i32 Consts_get_count (Consts* self);

struct Consts {
    Consts_FrameVec* _state_stack;
    Consts_Compartment* __compartment;
    Consts_Compartment* __next_compartment;
    Consts_FrameVec* _context_stack;
    i32 step;
    i32 limit;
    i32 count;
};

Consts* Consts_new(void) {
    Consts* self = calloc(1, sizeof(Consts));
    self->_state_stack = Consts_FrameVec_new();
    self->_context_stack = Consts_FrameVec_new();
    self->count = 0;
    Consts_FrameVec* __sa = NULL;
    Consts_FrameVec* __ea = NULL;
    self->__compartment = Consts_prepareEnter(self, "Running", __sa, __ea);
    self->__next_compartment = NULL;
    Consts_FrameEvent* __e = Consts_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    Consts_FrameContext* __ctx = Consts_FrameContext_new(__e, NULL);
    Consts_FrameVec_push(self->_context_stack, __ctx);
    Consts_FrameContext* __init_ctx = (Consts_FrameContext*)Consts_FrameVec_pop(self->_context_stack);
    Consts_FrameContext_destroy(__init_ctx);
    Consts_FrameEvent_destroy(__e);
    return self;
}

Consts* Consts_create(i32 step, i32 limit) {
    Consts* self = Consts_new();
    self->_state_stack = Consts_FrameVec_new();
    self->_context_stack = Consts_FrameVec_new();
    self->step = 5;
    self->limit = 20;
    self->count = 0;
    self->step = step;
    self->limit = limit;
    Consts_FrameVec* __sa = NULL;
    Consts_FrameVec* __ea = NULL;
    self->__compartment = Consts_prepareEnter(self, "Running", __sa, __ea);
    self->__next_compartment = NULL;
    Consts_FrameEvent* __e = Consts_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    Consts_FrameContext* __ctx = Consts_FrameContext_new(__e, NULL);
    Consts_FrameVec_push(self->_context_stack, __ctx);
    Consts_kernel(self, __e);
    Consts_FrameContext* __init_ctx = (Consts_FrameContext*)Consts_FrameVec_pop(self->_context_stack);
    Consts_FrameContext_destroy(__init_ctx);
    Consts_FrameEvent_destroy(__e);
    return self;
}

static int Consts_hsm_chain(Consts* self, const char* leaf, const char*** out_chain) {
    if (false) { (void)0; }
        else if (strcmp(leaf, "Running") == 0) {
            static const char* __chain[] = { "Running" };
            *out_chain = __chain;
            return 1;
        }
        *out_chain = NULL;
        return 0;
}

static Consts_Compartment* Consts_prepareEnter(Consts* self, const char* leaf, Consts_FrameVec* state_args, Consts_FrameVec* enter_args) {
    const char** chain = NULL;
    int n = Consts_hsm_chain(self, leaf, &chain);
    Consts_Compartment* comp = NULL;
    for (int i = 0; i < n; i++) {
        Consts_Compartment* nc = Consts_Compartment_new(chain[i]);
        if (state_args) {
            for (int j = 0; j < state_args->size; j++) Consts_FrameVec_push(nc->state_args, state_args->items[j]);
        }
        if (enter_args) {
            for (int j = 0; j < enter_args->size; j++) Consts_FrameVec_push(nc->enter_args, enter_args->items[j]);
        }
        nc->parent_compartment = comp;  // adopts ref
        comp = nc;
    }
    return comp;
}

static void Consts_prepareExit(Consts* self, Consts_FrameVec* exit_args) {
    Consts_Compartment* comp = self->__compartment;
    while (comp != NULL) {
        // Clear any prior exit_args before copying the new ones in.
        while (comp->exit_args->size > 0) comp->exit_args->size--;
        if (exit_args) {
            for (int j = 0; j < exit_args->size; j++) Consts_FrameVec_push(comp->exit_args, exit_args->items[j]);
        }
        comp = comp->parent_compartment;
    }
}

static void Consts_kernel(Consts* self, Consts_FrameEvent* __e) {
    Consts_router(self, __e);
    while (self->__next_compartment != NULL) {
        Consts_Compartment* next_compartment = self->__next_compartment;
        self->__next_compartment = NULL;
        // Exit the current (leaf) state
        Consts_FrameEvent* __exit_event = Consts_FrameEvent_new("<$", self->__compartment->exit_args, 0);
        Consts_router(self, __exit_event);
        Consts_FrameEvent_destroy(__exit_event);
        Consts_Compartment_unref(self->__compartment);
        self->__compartment = next_compartment;
        if (next_compartment->forward_event == NULL) {
            // No forwarded event — synthesize a fresh $>
            Consts_FrameEvent* __enter_event = Consts_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            Consts_router(self, __enter_event);
            Consts_FrameEvent_destroy(__enter_event);
        } else if (strcmp(next_compartment->forward_event->_message, "$>") == 0) {
            // Forwarded event IS $> — dispatch directly so the
            // destination's $> handler receives the caller's payload.
            // The forward_event is borrowed (owned by the wrapper that
            // queued the transition) — do NOT destroy it here.
            Consts_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            Consts_router(self, forward_event);
        } else {
            // Forwarded event is not $> — initialize the destination
            // with a fresh $>, then dispatch the forward to it. The
            // forward_event is borrowed; only the synthesized $> belongs
            // to the kernel and is freed here.
            Consts_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            Consts_FrameEvent* __enter_event = Consts_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            Consts_router(self, __enter_event);
            Consts_FrameEvent_destroy(__enter_event);
            Consts_router(self, forward_event);
        }
        // Mark every stacked context as having transitioned. Read by
        // @@:self.X() guard so outer self-calls short-circuit.
        for (int __i = 0; __i < self->_context_stack->size; __i++) {
            ((Consts_FrameContext*)self->_context_stack->items[__i])->_transitioned = 1;
        }
    }
}

static void Consts_router(Consts* self, Consts_FrameEvent* __e) {
    if (strcmp(self->__compartment->state, "Running") == 0) {
        Consts_state_Running(self, __e, self->__compartment);
    }
}

static void Consts_transition(Consts* self, Consts_Compartment* next_compartment) {
    self->__next_compartment = next_compartment;
}

void Consts_destroy(Consts* self) {
    // Unref current compartment (may free if not on stack)
    if (self->__compartment) Consts_Compartment_unref(self->__compartment);
    if (self->__next_compartment) Consts_Compartment_unref(self->__next_compartment);
    // Unref all state stack entries
    if (self->_state_stack) {
        for (int __i = 0; __i < self->_state_stack->size; __i++) {
            Consts_Compartment_unref((Consts_Compartment*)self->_state_stack->items[__i]);
        }
        Consts_FrameVec_destroy(self->_state_stack);
    }
    if (self->_context_stack) Consts_FrameVec_destroy(self->_context_stack);
    free(self);
}

void Consts_tick(Consts* self) {
    Consts_FrameEvent* __e = Consts_FrameEvent_new("tick", NULL, 0);
    Consts_FrameContext* __ctx = Consts_FrameContext_new(__e, NULL);
    Consts_FrameVec_push(self->_context_stack, __ctx);
    Consts_kernel(self, __e);
    Consts_FrameContext* __result_ctx = (Consts_FrameContext*)Consts_FrameVec_pop(self->_context_stack);
    Consts_FrameContext_destroy(__result_ctx);
    Consts_FrameEvent_destroy(__e);
}

i32 Consts_get_count(Consts* self) {
    Consts_FrameEvent* __e = Consts_FrameEvent_new("get_count", NULL, 0);
    Consts_FrameContext* __ctx = Consts_FrameContext_new(__e, NULL);
    Consts_FrameVec_push(self->_context_stack, __ctx);
    Consts_kernel(self, __e);
    Consts_FrameContext* __result_ctx = (Consts_FrameContext*)Consts_FrameVec_pop(self->_context_stack);
    i32 __result = (i32)__result_ctx->_return;
    Consts_FrameContext_destroy(__result_ctx);
    Consts_FrameEvent_destroy(__e);
    return __result;
}

static void Consts_state_Running(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment) {
    if (strcmp(__e->_message, "get_count") == 0) {
        Consts_s_Running_hdl_user_get_count(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "tick") == 0) {
        Consts_s_Running_hdl_user_tick(self, __e, compartment);
        return;
    }
}

static void Consts_s_Running_hdl_user_get_count(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment) {
    Consts_CTX(self)->_return = (void*)(intptr_t)(self.count);
}

static void Consts_s_Running_hdl_user_tick(Consts* self, Consts_FrameEvent* __e, Consts_Compartment* compartment) {
    self.count = self.count + self.step;
    if self.count >= self.limit {
        self.count = 0;
    }
}
