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

// ============================================================================
// ReturnExplicit_FrameDict - String-keyed dictionary
// ============================================================================

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

typedef struct {
    ReturnExplicit_FrameDictEntry** buckets;
    int bucket_count;
    int size;
} ReturnExplicit_FrameDict;

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

static ReturnExplicit_FrameDict* ReturnExplicit_FrameDict_new(void) {
    ReturnExplicit_FrameDict* d = malloc(sizeof(ReturnExplicit_FrameDict));
    d->bucket_count = 16;
    d->buckets = calloc(d->bucket_count, sizeof(ReturnExplicit_FrameDictEntry*));
    d->size = 0;
    return d;
}

static void ReturnExplicit_FrameDict_set(ReturnExplicit_FrameDict* d, const char* key, void* value) {
    unsigned int idx = ReturnExplicit_hash_string(key) % d->bucket_count;
    ReturnExplicit_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            entry->value = value;
            return;
        }
        entry = entry->next;
    }
    ReturnExplicit_FrameDictEntry* new_entry = malloc(sizeof(ReturnExplicit_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* ReturnExplicit_FrameDict_get(ReturnExplicit_FrameDict* d, const char* key) {
    unsigned int idx = ReturnExplicit_hash_string(key) % d->bucket_count;
    ReturnExplicit_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }
    return NULL;
}

static int ReturnExplicit_FrameDict_has(ReturnExplicit_FrameDict* d, const char* key) {
    unsigned int idx = ReturnExplicit_hash_string(key) % d->bucket_count;
    ReturnExplicit_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return 1;
        }
        entry = entry->next;
    }
    return 0;
}

static ReturnExplicit_FrameDict* ReturnExplicit_FrameDict_copy(ReturnExplicit_FrameDict* src) {
    ReturnExplicit_FrameDict* dst = ReturnExplicit_FrameDict_new();
    for (int i = 0; i < src->bucket_count; i++) {
        ReturnExplicit_FrameDictEntry* entry = src->buckets[i];
        while (entry) {
            ReturnExplicit_FrameDict_set(dst, entry->key, entry->value);
            entry = entry->next;
        }
    }
    return dst;
}

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

// ============================================================================
// ReturnExplicit_FrameVec - Dynamic array
// ============================================================================

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

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

static void ReturnExplicit_FrameVec_push(ReturnExplicit_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* ReturnExplicit_FrameVec_pop(ReturnExplicit_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[--v->size];
}

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

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

static int ReturnExplicit_FrameVec_size(ReturnExplicit_FrameVec* v) {
    return v->size;
}

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

static ReturnExplicit_FrameVec* ReturnExplicit_FrameVec_copy(ReturnExplicit_FrameVec* src) {
    if (!src) return NULL;
    ReturnExplicit_FrameVec* v = ReturnExplicit_FrameVec_new();
    for (int i = 0; i < src->size; i++) {
        ReturnExplicit_FrameVec_push(v, src->items[i]);
    }
    return v;
}

// ============================================================================
// ReturnExplicit_pack_double / ReturnExplicit_unpack_double — bit-pun doubles through void*
// ============================================================================

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

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

// ============================================================================
// ReturnExplicit_FrameEvent - Event routing object
// ============================================================================

typedef struct {
    const char* _message;
    ReturnExplicit_FrameVec* _parameters;
    int _owns_parameters;
} ReturnExplicit_FrameEvent;

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

static void ReturnExplicit_FrameEvent_destroy(ReturnExplicit_FrameEvent* e) {
    if (e->_owns_parameters && e->_parameters) ReturnExplicit_FrameVec_destroy(e->_parameters);
    free(e);
}

// ============================================================================
// ReturnExplicit_FrameContext - Interface call context
// ============================================================================

typedef struct {
    ReturnExplicit_FrameEvent* event;
    void* _return;
    ReturnExplicit_FrameDict* _data;
    int _transitioned;
} ReturnExplicit_FrameContext;

static ReturnExplicit_FrameContext* ReturnExplicit_FrameContext_new(ReturnExplicit_FrameEvent* event, void* default_return) {
    ReturnExplicit_FrameContext* ctx = malloc(sizeof(ReturnExplicit_FrameContext));
    ctx->event = event;
    ctx->_return = default_return;
    ctx->_data = ReturnExplicit_FrameDict_new();
    ctx->_transitioned = 0;
    return ctx;
}

static void ReturnExplicit_FrameContext_destroy(ReturnExplicit_FrameContext* ctx) {
    ReturnExplicit_FrameDict_destroy(ctx->_data);
    free(ctx);
}

// ============================================================================
// ReturnExplicit_Compartment - State closure
// ============================================================================

typedef struct ReturnExplicit_Compartment {
    const char* state;
    ReturnExplicit_FrameVec* state_args;
    ReturnExplicit_FrameDict* state_vars;
    ReturnExplicit_FrameVec* enter_args;
    ReturnExplicit_FrameVec* exit_args;
    ReturnExplicit_FrameEvent* forward_event;
    struct ReturnExplicit_Compartment* parent_compartment;
    int _ref_count;
} ReturnExplicit_Compartment;

static ReturnExplicit_Compartment* ReturnExplicit_Compartment_new(const char* state) {
    ReturnExplicit_Compartment* c = malloc(sizeof(ReturnExplicit_Compartment));
    c->state = state;
    c->state_args = ReturnExplicit_FrameVec_new();
    c->state_vars = ReturnExplicit_FrameDict_new();
    c->enter_args = ReturnExplicit_FrameVec_new();
    c->exit_args = ReturnExplicit_FrameVec_new();
    c->forward_event = NULL;
    c->parent_compartment = NULL;
    c->_ref_count = 1;
    return c;
}

static ReturnExplicit_Compartment* ReturnExplicit_Compartment_ref(ReturnExplicit_Compartment* c) {
    if (c) c->_ref_count++;
    return c;
}

static void ReturnExplicit_Compartment_unref(ReturnExplicit_Compartment* c);
static void ReturnExplicit_Compartment_unref(ReturnExplicit_Compartment* c) {
    if (c == NULL) return;
    c->_ref_count--;
    if (c->_ref_count <= 0) {
        ReturnExplicit_Compartment_unref(c->parent_compartment);
        ReturnExplicit_FrameVec_destroy(c->state_args);
        ReturnExplicit_FrameDict_destroy(c->state_vars);
        ReturnExplicit_FrameVec_destroy(c->enter_args);
        ReturnExplicit_FrameVec_destroy(c->exit_args);
        free(c);
    }
}

static ReturnExplicit_Compartment* ReturnExplicit_Compartment_copy(ReturnExplicit_Compartment* src) {
    ReturnExplicit_Compartment* c = malloc(sizeof(ReturnExplicit_Compartment));
    c->state = src->state;
    c->state_args = ReturnExplicit_FrameVec_copy(src->state_args);
    c->state_vars = ReturnExplicit_FrameDict_copy(src->state_vars);
    c->enter_args = ReturnExplicit_FrameVec_copy(src->enter_args);
    c->exit_args = ReturnExplicit_FrameVec_copy(src->exit_args);
    c->forward_event = src->forward_event;  // Shallow copy OK
    c->parent_compartment = src->parent_compartment;
    return c;
}

static void ReturnExplicit_Compartment_destroy(ReturnExplicit_Compartment* c) {
    ReturnExplicit_FrameVec_destroy(c->state_args);
    ReturnExplicit_FrameDict_destroy(c->state_vars);
    ReturnExplicit_FrameVec_destroy(c->enter_args);
    ReturnExplicit_FrameVec_destroy(c->exit_args);
    free(c);
}

// Helper macros for context access
#define ReturnExplicit_CTX(self) ((ReturnExplicit_FrameContext*)ReturnExplicit_FrameVec_last((self)->_context_stack))
#define ReturnExplicit_PARAM(self, key) ReturnExplicit_FrameDict_get(ReturnExplicit_CTX(self)->event->_parameters, key)
#define ReturnExplicit_RETURN(self) ReturnExplicit_CTX(self)->_return
#define ReturnExplicit_DATA(self, key) ReturnExplicit_FrameDict_get(ReturnExplicit_CTX(self)->_data, key)
#define ReturnExplicit_DATA_SET(self, key, val) ReturnExplicit_FrameDict_set(ReturnExplicit_CTX(self)->_data, key, val)

// Forward declarations
typedef struct ReturnExplicit ReturnExplicit;
static void ReturnExplicit_kernel(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e);
static void ReturnExplicit_router(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e);
static void ReturnExplicit_transition(ReturnExplicit* self, ReturnExplicit_Compartment* next);
static int ReturnExplicit_hsm_chain(ReturnExplicit* self, const char* leaf, const char*** out_chain);
static ReturnExplicit_Compartment* ReturnExplicit_prepareEnter(ReturnExplicit* self, const char* leaf, ReturnExplicit_FrameVec* state_args, ReturnExplicit_FrameVec* enter_args);
static void ReturnExplicit_prepareExit(ReturnExplicit* self, ReturnExplicit_FrameVec* exit_args);
static void ReturnExplicit_state_Judging(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e, ReturnExplicit_Compartment* compartment);
static void ReturnExplicit_s_Judging_hdl_user_decide(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e, ReturnExplicit_Compartment* compartment);
char* ReturnExplicit_decide (ReturnExplicit* self, int score);

struct ReturnExplicit {
    ReturnExplicit_FrameVec* _state_stack;
    ReturnExplicit_Compartment* __compartment;
    ReturnExplicit_Compartment* __next_compartment;
    ReturnExplicit_FrameVec* _context_stack;
};

ReturnExplicit* ReturnExplicit_new(void) {
    ReturnExplicit* self = calloc(1, sizeof(ReturnExplicit));
    self->_state_stack = ReturnExplicit_FrameVec_new();
    self->_context_stack = ReturnExplicit_FrameVec_new();
    ReturnExplicit_FrameVec* __sa = NULL;
    ReturnExplicit_FrameVec* __ea = NULL;
    self->__compartment = ReturnExplicit_prepareEnter(self, "Judging", __sa, __ea);
    self->__next_compartment = NULL;
    ReturnExplicit_FrameEvent* __e = ReturnExplicit_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    ReturnExplicit_FrameContext* __ctx = ReturnExplicit_FrameContext_new(__e, NULL);
    ReturnExplicit_FrameVec_push(self->_context_stack, __ctx);
    ReturnExplicit_FrameContext* __init_ctx = (ReturnExplicit_FrameContext*)ReturnExplicit_FrameVec_pop(self->_context_stack);
    ReturnExplicit_FrameContext_destroy(__init_ctx);
    ReturnExplicit_FrameEvent_destroy(__e);
    return self;
}

ReturnExplicit* ReturnExplicit_create(void) {
    ReturnExplicit* self = ReturnExplicit_new();
    self->_state_stack = ReturnExplicit_FrameVec_new();
    self->_context_stack = ReturnExplicit_FrameVec_new();
    ReturnExplicit_FrameVec* __sa = NULL;
    ReturnExplicit_FrameVec* __ea = NULL;
    self->__compartment = ReturnExplicit_prepareEnter(self, "Judging", __sa, __ea);
    self->__next_compartment = NULL;
    ReturnExplicit_FrameEvent* __e = ReturnExplicit_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    ReturnExplicit_FrameContext* __ctx = ReturnExplicit_FrameContext_new(__e, NULL);
    ReturnExplicit_FrameVec_push(self->_context_stack, __ctx);
    ReturnExplicit_kernel(self, __e);
    ReturnExplicit_FrameContext* __init_ctx = (ReturnExplicit_FrameContext*)ReturnExplicit_FrameVec_pop(self->_context_stack);
    ReturnExplicit_FrameContext_destroy(__init_ctx);
    ReturnExplicit_FrameEvent_destroy(__e);
    return self;
}

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

static ReturnExplicit_Compartment* ReturnExplicit_prepareEnter(ReturnExplicit* self, const char* leaf, ReturnExplicit_FrameVec* state_args, ReturnExplicit_FrameVec* enter_args) {
    const char** chain = NULL;
    int n = ReturnExplicit_hsm_chain(self, leaf, &chain);
    ReturnExplicit_Compartment* comp = NULL;
    for (int i = 0; i < n; i++) {
        ReturnExplicit_Compartment* nc = ReturnExplicit_Compartment_new(chain[i]);
        if (state_args) {
            for (int j = 0; j < state_args->size; j++) ReturnExplicit_FrameVec_push(nc->state_args, state_args->items[j]);
        }
        if (enter_args) {
            for (int j = 0; j < enter_args->size; j++) ReturnExplicit_FrameVec_push(nc->enter_args, enter_args->items[j]);
        }
        nc->parent_compartment = comp;  // adopts ref
        comp = nc;
    }
    return comp;
}

static void ReturnExplicit_prepareExit(ReturnExplicit* self, ReturnExplicit_FrameVec* exit_args) {
    ReturnExplicit_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++) ReturnExplicit_FrameVec_push(comp->exit_args, exit_args->items[j]);
        }
        comp = comp->parent_compartment;
    }
}

static void ReturnExplicit_kernel(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e) {
    ReturnExplicit_router(self, __e);
    while (self->__next_compartment != NULL) {
        ReturnExplicit_Compartment* next_compartment = self->__next_compartment;
        self->__next_compartment = NULL;
        // Exit the current (leaf) state
        ReturnExplicit_FrameEvent* __exit_event = ReturnExplicit_FrameEvent_new("<$", self->__compartment->exit_args, 0);
        ReturnExplicit_router(self, __exit_event);
        ReturnExplicit_FrameEvent_destroy(__exit_event);
        ReturnExplicit_Compartment_unref(self->__compartment);
        self->__compartment = next_compartment;
        if (next_compartment->forward_event == NULL) {
            // No forwarded event — synthesize a fresh $>
            ReturnExplicit_FrameEvent* __enter_event = ReturnExplicit_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            ReturnExplicit_router(self, __enter_event);
            ReturnExplicit_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.
            ReturnExplicit_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            ReturnExplicit_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.
            ReturnExplicit_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            ReturnExplicit_FrameEvent* __enter_event = ReturnExplicit_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            ReturnExplicit_router(self, __enter_event);
            ReturnExplicit_FrameEvent_destroy(__enter_event);
            ReturnExplicit_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++) {
            ((ReturnExplicit_FrameContext*)self->_context_stack->items[__i])->_transitioned = 1;
        }
    }
}

static void ReturnExplicit_router(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e) {
    if (strcmp(self->__compartment->state, "Judging") == 0) {
        ReturnExplicit_state_Judging(self, __e, self->__compartment);
    }
}

static void ReturnExplicit_transition(ReturnExplicit* self, ReturnExplicit_Compartment* next_compartment) {
    self->__next_compartment = next_compartment;
}

void ReturnExplicit_destroy(ReturnExplicit* self) {
    // Unref current compartment (may free if not on stack)
    if (self->__compartment) ReturnExplicit_Compartment_unref(self->__compartment);
    if (self->__next_compartment) ReturnExplicit_Compartment_unref(self->__next_compartment);
    // Unref all state stack entries
    if (self->_state_stack) {
        for (int __i = 0; __i < self->_state_stack->size; __i++) {
            ReturnExplicit_Compartment_unref((ReturnExplicit_Compartment*)self->_state_stack->items[__i]);
        }
        ReturnExplicit_FrameVec_destroy(self->_state_stack);
    }
    if (self->_context_stack) ReturnExplicit_FrameVec_destroy(self->_context_stack);
    free(self);
}

char* ReturnExplicit_decide(ReturnExplicit* self, int score) {
    ReturnExplicit_FrameVec* __params = ReturnExplicit_FrameVec_new();
    ReturnExplicit_FrameVec_push(__params, (void*)(intptr_t)score);
    ReturnExplicit_FrameEvent* __e = ReturnExplicit_FrameEvent_new("decide", __params, 1);
    ReturnExplicit_FrameContext* __ctx = ReturnExplicit_FrameContext_new(__e, NULL);
    ReturnExplicit_FrameVec_push(self->_context_stack, __ctx);
    ReturnExplicit_kernel(self, __e);
    ReturnExplicit_FrameContext* __result_ctx = (ReturnExplicit_FrameContext*)ReturnExplicit_FrameVec_pop(self->_context_stack);
    char* __result = (char*)__result_ctx->_return;
    ReturnExplicit_FrameContext_destroy(__result_ctx);
    ReturnExplicit_FrameEvent_destroy(__e);
    return __result;
}

static void ReturnExplicit_state_Judging(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e, ReturnExplicit_Compartment* compartment) {
    if (strcmp(__e->_message, "decide") == 0) {
        ReturnExplicit_s_Judging_hdl_user_decide(self, __e, compartment);
        return;
    }
}

static void ReturnExplicit_s_Judging_hdl_user_decide(ReturnExplicit* self, ReturnExplicit_FrameEvent* __e, ReturnExplicit_Compartment* compartment) {
    int score = (int)(intptr_t)ReturnExplicit_FrameVec_get(__e->_parameters, 0);
    (void)score;
                    if score >= 60 {
    ReturnExplicit_CTX(self)->_return = (void*)(intptr_t)("pass");
                        return;
                    }
    ReturnExplicit_CTX(self)->_return = (void*)(intptr_t)("fail");
                    return;
}
