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

// ============================================================================
// StateArgs_FrameDict - String-keyed dictionary
// ============================================================================

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

typedef struct {
    StateArgs_FrameDictEntry** buckets;
    int bucket_count;
    int size;
} StateArgs_FrameDict;

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

static StateArgs_FrameDict* StateArgs_FrameDict_new(void) {
    StateArgs_FrameDict* d = malloc(sizeof(StateArgs_FrameDict));
    d->bucket_count = 16;
    d->buckets = calloc(d->bucket_count, sizeof(StateArgs_FrameDictEntry*));
    d->size = 0;
    return d;
}

static void StateArgs_FrameDict_set(StateArgs_FrameDict* d, const char* key, void* value) {
    unsigned int idx = StateArgs_hash_string(key) % d->bucket_count;
    StateArgs_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            entry->value = value;
            return;
        }
        entry = entry->next;
    }
    StateArgs_FrameDictEntry* new_entry = malloc(sizeof(StateArgs_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* StateArgs_FrameDict_get(StateArgs_FrameDict* d, const char* key) {
    unsigned int idx = StateArgs_hash_string(key) % d->bucket_count;
    StateArgs_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }
    return NULL;
}

static int StateArgs_FrameDict_has(StateArgs_FrameDict* d, const char* key) {
    unsigned int idx = StateArgs_hash_string(key) % d->bucket_count;
    StateArgs_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return 1;
        }
        entry = entry->next;
    }
    return 0;
}

static StateArgs_FrameDict* StateArgs_FrameDict_copy(StateArgs_FrameDict* src) {
    StateArgs_FrameDict* dst = StateArgs_FrameDict_new();
    for (int i = 0; i < src->bucket_count; i++) {
        StateArgs_FrameDictEntry* entry = src->buckets[i];
        while (entry) {
            StateArgs_FrameDict_set(dst, entry->key, entry->value);
            entry = entry->next;
        }
    }
    return dst;
}

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

// ============================================================================
// StateArgs_FrameVec - Dynamic array
// ============================================================================

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

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

static void StateArgs_FrameVec_push(StateArgs_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* StateArgs_FrameVec_pop(StateArgs_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[--v->size];
}

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

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

static int StateArgs_FrameVec_size(StateArgs_FrameVec* v) {
    return v->size;
}

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

static StateArgs_FrameVec* StateArgs_FrameVec_copy(StateArgs_FrameVec* src) {
    if (!src) return NULL;
    StateArgs_FrameVec* v = StateArgs_FrameVec_new();
    for (int i = 0; i < src->size; i++) {
        StateArgs_FrameVec_push(v, src->items[i]);
    }
    return v;
}

// ============================================================================
// StateArgs_pack_double / StateArgs_unpack_double — bit-pun doubles through void*
// ============================================================================

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

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

// ============================================================================
// StateArgs_FrameEvent - Event routing object
// ============================================================================

typedef struct {
    const char* _message;
    StateArgs_FrameVec* _parameters;
    int _owns_parameters;
} StateArgs_FrameEvent;

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

static void StateArgs_FrameEvent_destroy(StateArgs_FrameEvent* e) {
    if (e->_owns_parameters && e->_parameters) StateArgs_FrameVec_destroy(e->_parameters);
    free(e);
}

// ============================================================================
// StateArgs_FrameContext - Interface call context
// ============================================================================

typedef struct {
    StateArgs_FrameEvent* event;
    void* _return;
    StateArgs_FrameDict* _data;
    int _transitioned;
} StateArgs_FrameContext;

static StateArgs_FrameContext* StateArgs_FrameContext_new(StateArgs_FrameEvent* event, void* default_return) {
    StateArgs_FrameContext* ctx = malloc(sizeof(StateArgs_FrameContext));
    ctx->event = event;
    ctx->_return = default_return;
    ctx->_data = StateArgs_FrameDict_new();
    ctx->_transitioned = 0;
    return ctx;
}

static void StateArgs_FrameContext_destroy(StateArgs_FrameContext* ctx) {
    StateArgs_FrameDict_destroy(ctx->_data);
    free(ctx);
}

// ============================================================================
// StateArgs_Compartment - State closure
// ============================================================================

typedef struct StateArgs_Compartment {
    const char* state;
    StateArgs_FrameVec* state_args;
    StateArgs_FrameDict* state_vars;
    StateArgs_FrameVec* enter_args;
    StateArgs_FrameVec* exit_args;
    StateArgs_FrameEvent* forward_event;
    struct StateArgs_Compartment* parent_compartment;
    int _ref_count;
} StateArgs_Compartment;

static StateArgs_Compartment* StateArgs_Compartment_new(const char* state) {
    StateArgs_Compartment* c = malloc(sizeof(StateArgs_Compartment));
    c->state = state;
    c->state_args = StateArgs_FrameVec_new();
    c->state_vars = StateArgs_FrameDict_new();
    c->enter_args = StateArgs_FrameVec_new();
    c->exit_args = StateArgs_FrameVec_new();
    c->forward_event = NULL;
    c->parent_compartment = NULL;
    c->_ref_count = 1;
    return c;
}

static StateArgs_Compartment* StateArgs_Compartment_ref(StateArgs_Compartment* c) {
    if (c) c->_ref_count++;
    return c;
}

static void StateArgs_Compartment_unref(StateArgs_Compartment* c);
static void StateArgs_Compartment_unref(StateArgs_Compartment* c) {
    if (c == NULL) return;
    c->_ref_count--;
    if (c->_ref_count <= 0) {
        StateArgs_Compartment_unref(c->parent_compartment);
        StateArgs_FrameVec_destroy(c->state_args);
        StateArgs_FrameDict_destroy(c->state_vars);
        StateArgs_FrameVec_destroy(c->enter_args);
        StateArgs_FrameVec_destroy(c->exit_args);
        free(c);
    }
}

static StateArgs_Compartment* StateArgs_Compartment_copy(StateArgs_Compartment* src) {
    StateArgs_Compartment* c = malloc(sizeof(StateArgs_Compartment));
    c->state = src->state;
    c->state_args = StateArgs_FrameVec_copy(src->state_args);
    c->state_vars = StateArgs_FrameDict_copy(src->state_vars);
    c->enter_args = StateArgs_FrameVec_copy(src->enter_args);
    c->exit_args = StateArgs_FrameVec_copy(src->exit_args);
    c->forward_event = src->forward_event;  // Shallow copy OK
    c->parent_compartment = src->parent_compartment;
    return c;
}

static void StateArgs_Compartment_destroy(StateArgs_Compartment* c) {
    StateArgs_FrameVec_destroy(c->state_args);
    StateArgs_FrameDict_destroy(c->state_vars);
    StateArgs_FrameVec_destroy(c->enter_args);
    StateArgs_FrameVec_destroy(c->exit_args);
    free(c);
}

// Helper macros for context access
#define StateArgs_CTX(self) ((StateArgs_FrameContext*)StateArgs_FrameVec_last((self)->_context_stack))
#define StateArgs_PARAM(self, key) StateArgs_FrameDict_get(StateArgs_CTX(self)->event->_parameters, key)
#define StateArgs_RETURN(self) StateArgs_CTX(self)->_return
#define StateArgs_DATA(self, key) StateArgs_FrameDict_get(StateArgs_CTX(self)->_data, key)
#define StateArgs_DATA_SET(self, key, val) StateArgs_FrameDict_set(StateArgs_CTX(self)->_data, key, val)

// Forward declarations
typedef struct StateArgs StateArgs;
static void StateArgs_kernel(StateArgs* self, StateArgs_FrameEvent* __e);
static void StateArgs_router(StateArgs* self, StateArgs_FrameEvent* __e);
static void StateArgs_transition(StateArgs* self, StateArgs_Compartment* next);
static int StateArgs_hsm_chain(StateArgs* self, const char* leaf, const char*** out_chain);
static StateArgs_Compartment* StateArgs_prepareEnter(StateArgs* self, const char* leaf, StateArgs_FrameVec* state_args, StateArgs_FrameVec* enter_args);
static void StateArgs_prepareExit(StateArgs* self, StateArgs_FrameVec* exit_args);
static void StateArgs_state_Idle(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment);
static void StateArgs_state_Holding(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment);
static void StateArgs_s_Idle_hdl_user_load(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment);
static void StateArgs_s_Holding_hdl_user_adjust(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment);
static void StateArgs_s_Holding_hdl_user_peek(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment);
void StateArgs_load (StateArgs* self, int initial);
void StateArgs_adjust (StateArgs* self, int delta);
int StateArgs_peek (StateArgs* self);

struct StateArgs {
    StateArgs_FrameVec* _state_stack;
    StateArgs_Compartment* __compartment;
    StateArgs_Compartment* __next_compartment;
    StateArgs_FrameVec* _context_stack;
};

StateArgs* StateArgs_new(void) {
    StateArgs* self = calloc(1, sizeof(StateArgs));
    self->_state_stack = StateArgs_FrameVec_new();
    self->_context_stack = StateArgs_FrameVec_new();
    StateArgs_FrameVec* __sa = NULL;
    StateArgs_FrameVec* __ea = NULL;
    self->__compartment = StateArgs_prepareEnter(self, "Idle", __sa, __ea);
    self->__next_compartment = NULL;
    StateArgs_FrameEvent* __e = StateArgs_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    StateArgs_FrameContext* __ctx = StateArgs_FrameContext_new(__e, NULL);
    StateArgs_FrameVec_push(self->_context_stack, __ctx);
    StateArgs_FrameContext* __init_ctx = (StateArgs_FrameContext*)StateArgs_FrameVec_pop(self->_context_stack);
    StateArgs_FrameContext_destroy(__init_ctx);
    StateArgs_FrameEvent_destroy(__e);
    return self;
}

StateArgs* StateArgs_create(void) {
    StateArgs* self = StateArgs_new();
    self->_state_stack = StateArgs_FrameVec_new();
    self->_context_stack = StateArgs_FrameVec_new();
    StateArgs_FrameVec* __sa = NULL;
    StateArgs_FrameVec* __ea = NULL;
    self->__compartment = StateArgs_prepareEnter(self, "Idle", __sa, __ea);
    self->__next_compartment = NULL;
    StateArgs_FrameEvent* __e = StateArgs_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    StateArgs_FrameContext* __ctx = StateArgs_FrameContext_new(__e, NULL);
    StateArgs_FrameVec_push(self->_context_stack, __ctx);
    StateArgs_kernel(self, __e);
    StateArgs_FrameContext* __init_ctx = (StateArgs_FrameContext*)StateArgs_FrameVec_pop(self->_context_stack);
    StateArgs_FrameContext_destroy(__init_ctx);
    StateArgs_FrameEvent_destroy(__e);
    return self;
}

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

static StateArgs_Compartment* StateArgs_prepareEnter(StateArgs* self, const char* leaf, StateArgs_FrameVec* state_args, StateArgs_FrameVec* enter_args) {
    const char** chain = NULL;
    int n = StateArgs_hsm_chain(self, leaf, &chain);
    StateArgs_Compartment* comp = NULL;
    for (int i = 0; i < n; i++) {
        StateArgs_Compartment* nc = StateArgs_Compartment_new(chain[i]);
        if (state_args) {
            for (int j = 0; j < state_args->size; j++) StateArgs_FrameVec_push(nc->state_args, state_args->items[j]);
        }
        if (enter_args) {
            for (int j = 0; j < enter_args->size; j++) StateArgs_FrameVec_push(nc->enter_args, enter_args->items[j]);
        }
        nc->parent_compartment = comp;  // adopts ref
        comp = nc;
    }
    return comp;
}

static void StateArgs_prepareExit(StateArgs* self, StateArgs_FrameVec* exit_args) {
    StateArgs_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++) StateArgs_FrameVec_push(comp->exit_args, exit_args->items[j]);
        }
        comp = comp->parent_compartment;
    }
}

static void StateArgs_kernel(StateArgs* self, StateArgs_FrameEvent* __e) {
    StateArgs_router(self, __e);
    while (self->__next_compartment != NULL) {
        StateArgs_Compartment* next_compartment = self->__next_compartment;
        self->__next_compartment = NULL;
        // Exit the current (leaf) state
        StateArgs_FrameEvent* __exit_event = StateArgs_FrameEvent_new("<$", self->__compartment->exit_args, 0);
        StateArgs_router(self, __exit_event);
        StateArgs_FrameEvent_destroy(__exit_event);
        StateArgs_Compartment_unref(self->__compartment);
        self->__compartment = next_compartment;
        if (next_compartment->forward_event == NULL) {
            // No forwarded event — synthesize a fresh $>
            StateArgs_FrameEvent* __enter_event = StateArgs_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            StateArgs_router(self, __enter_event);
            StateArgs_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.
            StateArgs_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            StateArgs_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.
            StateArgs_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            StateArgs_FrameEvent* __enter_event = StateArgs_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            StateArgs_router(self, __enter_event);
            StateArgs_FrameEvent_destroy(__enter_event);
            StateArgs_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++) {
            ((StateArgs_FrameContext*)self->_context_stack->items[__i])->_transitioned = 1;
        }
    }
}

static void StateArgs_router(StateArgs* self, StateArgs_FrameEvent* __e) {
    if (strcmp(self->__compartment->state, "Idle") == 0) {
        StateArgs_state_Idle(self, __e, self->__compartment);
    } else if (strcmp(self->__compartment->state, "Holding") == 0) {
        StateArgs_state_Holding(self, __e, self->__compartment);
    }
}

static void StateArgs_transition(StateArgs* self, StateArgs_Compartment* next_compartment) {
    self->__next_compartment = next_compartment;
}

void StateArgs_destroy(StateArgs* self) {
    // Unref current compartment (may free if not on stack)
    if (self->__compartment) StateArgs_Compartment_unref(self->__compartment);
    if (self->__next_compartment) StateArgs_Compartment_unref(self->__next_compartment);
    // Unref all state stack entries
    if (self->_state_stack) {
        for (int __i = 0; __i < self->_state_stack->size; __i++) {
            StateArgs_Compartment_unref((StateArgs_Compartment*)self->_state_stack->items[__i]);
        }
        StateArgs_FrameVec_destroy(self->_state_stack);
    }
    if (self->_context_stack) StateArgs_FrameVec_destroy(self->_context_stack);
    free(self);
}

void StateArgs_load(StateArgs* self, int initial) {
    StateArgs_FrameVec* __params = StateArgs_FrameVec_new();
    StateArgs_FrameVec_push(__params, (void*)(intptr_t)initial);
    StateArgs_FrameEvent* __e = StateArgs_FrameEvent_new("load", __params, 1);
    StateArgs_FrameContext* __ctx = StateArgs_FrameContext_new(__e, NULL);
    StateArgs_FrameVec_push(self->_context_stack, __ctx);
    StateArgs_kernel(self, __e);
    StateArgs_FrameContext* __result_ctx = (StateArgs_FrameContext*)StateArgs_FrameVec_pop(self->_context_stack);
    StateArgs_FrameContext_destroy(__result_ctx);
    StateArgs_FrameEvent_destroy(__e);
}

void StateArgs_adjust(StateArgs* self, int delta) {
    StateArgs_FrameVec* __params = StateArgs_FrameVec_new();
    StateArgs_FrameVec_push(__params, (void*)(intptr_t)delta);
    StateArgs_FrameEvent* __e = StateArgs_FrameEvent_new("adjust", __params, 1);
    StateArgs_FrameContext* __ctx = StateArgs_FrameContext_new(__e, NULL);
    StateArgs_FrameVec_push(self->_context_stack, __ctx);
    StateArgs_kernel(self, __e);
    StateArgs_FrameContext* __result_ctx = (StateArgs_FrameContext*)StateArgs_FrameVec_pop(self->_context_stack);
    StateArgs_FrameContext_destroy(__result_ctx);
    StateArgs_FrameEvent_destroy(__e);
}

int StateArgs_peek(StateArgs* self) {
    StateArgs_FrameEvent* __e = StateArgs_FrameEvent_new("peek", NULL, 0);
    StateArgs_FrameContext* __ctx = StateArgs_FrameContext_new(__e, NULL);
    StateArgs_FrameVec_push(self->_context_stack, __ctx);
    StateArgs_kernel(self, __e);
    StateArgs_FrameContext* __result_ctx = (StateArgs_FrameContext*)StateArgs_FrameVec_pop(self->_context_stack);
    int __result = (int)(intptr_t)__result_ctx->_return;
    StateArgs_FrameContext_destroy(__result_ctx);
    StateArgs_FrameEvent_destroy(__e);
    return __result;
}

static void StateArgs_state_Idle(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment) {
    if (strcmp(__e->_message, "load") == 0) {
        StateArgs_s_Idle_hdl_user_load(self, __e, compartment);
        return;
    }
}

static void StateArgs_state_Holding(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment) {
    int value = (int)(intptr_t)self->__compartment->state_args->items[0];
    if (strcmp(__e->_message, "adjust") == 0) {
        StateArgs_s_Holding_hdl_user_adjust(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "peek") == 0) {
        StateArgs_s_Holding_hdl_user_peek(self, __e, compartment);
        return;
    }
}

static void StateArgs_s_Idle_hdl_user_load(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment) {
    int initial = (int)(intptr_t)StateArgs_FrameVec_get(__e->_parameters, 0);
    (void)initial;
    {
        StateArgs_FrameVec* __sa = StateArgs_FrameVec_new();
        StateArgs_FrameVec_push(__sa, (void*)(intptr_t)(initial));
        StateArgs_FrameVec* __ea = NULL;
        StateArgs_Compartment* __compartment = StateArgs_prepareEnter(self, "Holding", __sa, __ea);
        StateArgs_FrameVec_destroy(__sa);
        StateArgs_transition(self, __compartment);
    }
    return;
}

static void StateArgs_s_Holding_hdl_user_adjust(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment) {
    int value = (int)(intptr_t)StateArgs_FrameVec_get(compartment->state_args, 0);
    (void)value;
    int delta = (int)(intptr_t)StateArgs_FrameVec_get(__e->_parameters, 0);
    (void)delta;
    {
        StateArgs_FrameVec* __sa = StateArgs_FrameVec_new();
        StateArgs_FrameVec_push(__sa, (void*)(intptr_t)(value + delta));
        StateArgs_FrameVec* __ea = NULL;
        StateArgs_Compartment* __compartment = StateArgs_prepareEnter(self, "Holding", __sa, __ea);
        StateArgs_FrameVec_destroy(__sa);
        StateArgs_transition(self, __compartment);
    }
    return;
}

static void StateArgs_s_Holding_hdl_user_peek(StateArgs* self, StateArgs_FrameEvent* __e, StateArgs_Compartment* compartment) {
    int value = (int)(intptr_t)StateArgs_FrameVec_get(compartment->state_args, 0);
    (void)value;
    StateArgs_CTX(self)->_return = (void*)(intptr_t)(value);
}
