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

// ============================================================================
// MiniHsm_FrameDict - String-keyed dictionary
// ============================================================================

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

typedef struct {
    MiniHsm_FrameDictEntry** buckets;
    int bucket_count;
    int size;
} MiniHsm_FrameDict;

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

static MiniHsm_FrameDict* MiniHsm_FrameDict_new(void) {
    MiniHsm_FrameDict* d = malloc(sizeof(MiniHsm_FrameDict));
    d->bucket_count = 16;
    d->buckets = calloc(d->bucket_count, sizeof(MiniHsm_FrameDictEntry*));
    d->size = 0;
    return d;
}

static void MiniHsm_FrameDict_set(MiniHsm_FrameDict* d, const char* key, void* value) {
    unsigned int idx = MiniHsm_hash_string(key) % d->bucket_count;
    MiniHsm_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            entry->value = value;
            return;
        }
        entry = entry->next;
    }
    MiniHsm_FrameDictEntry* new_entry = malloc(sizeof(MiniHsm_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* MiniHsm_FrameDict_get(MiniHsm_FrameDict* d, const char* key) {
    unsigned int idx = MiniHsm_hash_string(key) % d->bucket_count;
    MiniHsm_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }
    return NULL;
}

static int MiniHsm_FrameDict_has(MiniHsm_FrameDict* d, const char* key) {
    unsigned int idx = MiniHsm_hash_string(key) % d->bucket_count;
    MiniHsm_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return 1;
        }
        entry = entry->next;
    }
    return 0;
}

static MiniHsm_FrameDict* MiniHsm_FrameDict_copy(MiniHsm_FrameDict* src) {
    MiniHsm_FrameDict* dst = MiniHsm_FrameDict_new();
    for (int i = 0; i < src->bucket_count; i++) {
        MiniHsm_FrameDictEntry* entry = src->buckets[i];
        while (entry) {
            MiniHsm_FrameDict_set(dst, entry->key, entry->value);
            entry = entry->next;
        }
    }
    return dst;
}

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

// ============================================================================
// MiniHsm_FrameVec - Dynamic array
// ============================================================================

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

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

static void MiniHsm_FrameVec_push(MiniHsm_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* MiniHsm_FrameVec_pop(MiniHsm_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[--v->size];
}

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

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

static int MiniHsm_FrameVec_size(MiniHsm_FrameVec* v) {
    return v->size;
}

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

static MiniHsm_FrameVec* MiniHsm_FrameVec_copy(MiniHsm_FrameVec* src) {
    if (!src) return NULL;
    MiniHsm_FrameVec* v = MiniHsm_FrameVec_new();
    for (int i = 0; i < src->size; i++) {
        MiniHsm_FrameVec_push(v, src->items[i]);
    }
    return v;
}

// ============================================================================
// MiniHsm_pack_double / MiniHsm_unpack_double — bit-pun doubles through void*
// ============================================================================

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

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

// ============================================================================
// MiniHsm_FrameEvent - Event routing object
// ============================================================================

typedef struct {
    const char* _message;
    MiniHsm_FrameVec* _parameters;
    int _owns_parameters;
} MiniHsm_FrameEvent;

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

static void MiniHsm_FrameEvent_destroy(MiniHsm_FrameEvent* e) {
    if (e->_owns_parameters && e->_parameters) MiniHsm_FrameVec_destroy(e->_parameters);
    free(e);
}

// ============================================================================
// MiniHsm_FrameContext - Interface call context
// ============================================================================

typedef struct {
    MiniHsm_FrameEvent* event;
    void* _return;
    MiniHsm_FrameDict* _data;
    int _transitioned;
} MiniHsm_FrameContext;

static MiniHsm_FrameContext* MiniHsm_FrameContext_new(MiniHsm_FrameEvent* event, void* default_return) {
    MiniHsm_FrameContext* ctx = malloc(sizeof(MiniHsm_FrameContext));
    ctx->event = event;
    ctx->_return = default_return;
    ctx->_data = MiniHsm_FrameDict_new();
    ctx->_transitioned = 0;
    return ctx;
}

static void MiniHsm_FrameContext_destroy(MiniHsm_FrameContext* ctx) {
    MiniHsm_FrameDict_destroy(ctx->_data);
    free(ctx);
}

// ============================================================================
// MiniHsm_Compartment - State closure
// ============================================================================

typedef struct MiniHsm_Compartment {
    const char* state;
    MiniHsm_FrameVec* state_args;
    MiniHsm_FrameDict* state_vars;
    MiniHsm_FrameVec* enter_args;
    MiniHsm_FrameVec* exit_args;
    MiniHsm_FrameEvent* forward_event;
    struct MiniHsm_Compartment* parent_compartment;
    int _ref_count;
} MiniHsm_Compartment;

static MiniHsm_Compartment* MiniHsm_Compartment_new(const char* state) {
    MiniHsm_Compartment* c = malloc(sizeof(MiniHsm_Compartment));
    c->state = state;
    c->state_args = MiniHsm_FrameVec_new();
    c->state_vars = MiniHsm_FrameDict_new();
    c->enter_args = MiniHsm_FrameVec_new();
    c->exit_args = MiniHsm_FrameVec_new();
    c->forward_event = NULL;
    c->parent_compartment = NULL;
    c->_ref_count = 1;
    return c;
}

static MiniHsm_Compartment* MiniHsm_Compartment_ref(MiniHsm_Compartment* c) {
    if (c) c->_ref_count++;
    return c;
}

static void MiniHsm_Compartment_unref(MiniHsm_Compartment* c);
static void MiniHsm_Compartment_unref(MiniHsm_Compartment* c) {
    if (c == NULL) return;
    c->_ref_count--;
    if (c->_ref_count <= 0) {
        MiniHsm_Compartment_unref(c->parent_compartment);
        MiniHsm_FrameVec_destroy(c->state_args);
        MiniHsm_FrameDict_destroy(c->state_vars);
        MiniHsm_FrameVec_destroy(c->enter_args);
        MiniHsm_FrameVec_destroy(c->exit_args);
        free(c);
    }
}

static MiniHsm_Compartment* MiniHsm_Compartment_copy(MiniHsm_Compartment* src) {
    MiniHsm_Compartment* c = malloc(sizeof(MiniHsm_Compartment));
    c->state = src->state;
    c->state_args = MiniHsm_FrameVec_copy(src->state_args);
    c->state_vars = MiniHsm_FrameDict_copy(src->state_vars);
    c->enter_args = MiniHsm_FrameVec_copy(src->enter_args);
    c->exit_args = MiniHsm_FrameVec_copy(src->exit_args);
    c->forward_event = src->forward_event;  // Shallow copy OK
    c->parent_compartment = src->parent_compartment;
    return c;
}

static void MiniHsm_Compartment_destroy(MiniHsm_Compartment* c) {
    MiniHsm_FrameVec_destroy(c->state_args);
    MiniHsm_FrameDict_destroy(c->state_vars);
    MiniHsm_FrameVec_destroy(c->enter_args);
    MiniHsm_FrameVec_destroy(c->exit_args);
    free(c);
}

// Helper macros for context access
#define MiniHsm_CTX(self) ((MiniHsm_FrameContext*)MiniHsm_FrameVec_last((self)->_context_stack))
#define MiniHsm_PARAM(self, key) MiniHsm_FrameDict_get(MiniHsm_CTX(self)->event->_parameters, key)
#define MiniHsm_RETURN(self) MiniHsm_CTX(self)->_return
#define MiniHsm_DATA(self, key) MiniHsm_FrameDict_get(MiniHsm_CTX(self)->_data, key)
#define MiniHsm_DATA_SET(self, key, val) MiniHsm_FrameDict_set(MiniHsm_CTX(self)->_data, key, val)

// Forward declarations
typedef struct MiniHsm MiniHsm;
static void MiniHsm_kernel(MiniHsm* self, MiniHsm_FrameEvent* __e);
static void MiniHsm_router(MiniHsm* self, MiniHsm_FrameEvent* __e);
static void MiniHsm_transition(MiniHsm* self, MiniHsm_Compartment* next);
static int MiniHsm_hsm_chain(MiniHsm* self, const char* leaf, const char*** out_chain);
static MiniHsm_Compartment* MiniHsm_prepareEnter(MiniHsm* self, const char* leaf, MiniHsm_FrameVec* state_args, MiniHsm_FrameVec* enter_args);
static void MiniHsm_prepareExit(MiniHsm* self, MiniHsm_FrameVec* exit_args);
static void MiniHsm_state_Live(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_state_Awake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_state_Asleep(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Live_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Live_hdl_user_sleep(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Live_hdl_user_wake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Awake_hdl_frame_enter(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Awake_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Asleep_hdl_frame_enter(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Asleep_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
static void MiniHsm_s_Asleep_hdl_user_wake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment);
void MiniHsm_wake (MiniHsm* self);
void MiniHsm_sleep (MiniHsm* self);
void MiniHsm_signal (MiniHsm* self);

struct MiniHsm {
    MiniHsm_FrameVec* _state_stack;
    MiniHsm_Compartment* __compartment;
    MiniHsm_Compartment* __next_compartment;
    MiniHsm_FrameVec* _context_stack;
    int awakes;
    int sleeps;
    int last;
};

MiniHsm* MiniHsm_new(void) {
    MiniHsm* self = calloc(1, sizeof(MiniHsm));
    self->_state_stack = MiniHsm_FrameVec_new();
    self->_context_stack = MiniHsm_FrameVec_new();
    self->awakes = 0;
    self->sleeps = 0;
    self->last = 0;
    MiniHsm_FrameVec* __sa = NULL;
    MiniHsm_FrameVec* __ea = NULL;
    self->__compartment = MiniHsm_prepareEnter(self, "Live", __sa, __ea);
    self->__next_compartment = NULL;
    MiniHsm_FrameEvent* __e = MiniHsm_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    MiniHsm_FrameContext* __ctx = MiniHsm_FrameContext_new(__e, NULL);
    MiniHsm_FrameVec_push(self->_context_stack, __ctx);
    MiniHsm_FrameContext* __init_ctx = (MiniHsm_FrameContext*)MiniHsm_FrameVec_pop(self->_context_stack);
    MiniHsm_FrameContext_destroy(__init_ctx);
    MiniHsm_FrameEvent_destroy(__e);
    return self;
}

MiniHsm* MiniHsm_create(void) {
    MiniHsm* self = MiniHsm_new();
    self->_state_stack = MiniHsm_FrameVec_new();
    self->_context_stack = MiniHsm_FrameVec_new();
    self->awakes = 0;
    self->sleeps = 0;
    self->last = 0;
    MiniHsm_FrameVec* __sa = NULL;
    MiniHsm_FrameVec* __ea = NULL;
    self->__compartment = MiniHsm_prepareEnter(self, "Live", __sa, __ea);
    self->__next_compartment = NULL;
    MiniHsm_FrameEvent* __e = MiniHsm_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    MiniHsm_FrameContext* __ctx = MiniHsm_FrameContext_new(__e, NULL);
    MiniHsm_FrameVec_push(self->_context_stack, __ctx);
    MiniHsm_kernel(self, __e);
    MiniHsm_FrameContext* __init_ctx = (MiniHsm_FrameContext*)MiniHsm_FrameVec_pop(self->_context_stack);
    MiniHsm_FrameContext_destroy(__init_ctx);
    MiniHsm_FrameEvent_destroy(__e);
    return self;
}

static int MiniHsm_hsm_chain(MiniHsm* self, const char* leaf, const char*** out_chain) {
    if (false) { (void)0; }
        else if (strcmp(leaf, "Live") == 0) {
            static const char* __chain[] = { "Live" };
            *out_chain = __chain;
            return 1;
        }
        else if (strcmp(leaf, "Awake") == 0) {
            static const char* __chain[] = { "Live", "Awake" };
            *out_chain = __chain;
            return 2;
        }
        else if (strcmp(leaf, "Asleep") == 0) {
            static const char* __chain[] = { "Live", "Asleep" };
            *out_chain = __chain;
            return 2;
        }
        *out_chain = NULL;
        return 0;
}

static MiniHsm_Compartment* MiniHsm_prepareEnter(MiniHsm* self, const char* leaf, MiniHsm_FrameVec* state_args, MiniHsm_FrameVec* enter_args) {
    const char** chain = NULL;
    int n = MiniHsm_hsm_chain(self, leaf, &chain);
    MiniHsm_Compartment* comp = NULL;
    for (int i = 0; i < n; i++) {
        MiniHsm_Compartment* nc = MiniHsm_Compartment_new(chain[i]);
        if (state_args) {
            for (int j = 0; j < state_args->size; j++) MiniHsm_FrameVec_push(nc->state_args, state_args->items[j]);
        }
        if (enter_args) {
            for (int j = 0; j < enter_args->size; j++) MiniHsm_FrameVec_push(nc->enter_args, enter_args->items[j]);
        }
        nc->parent_compartment = comp;  // adopts ref
        comp = nc;
    }
    return comp;
}

static void MiniHsm_prepareExit(MiniHsm* self, MiniHsm_FrameVec* exit_args) {
    MiniHsm_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++) MiniHsm_FrameVec_push(comp->exit_args, exit_args->items[j]);
        }
        comp = comp->parent_compartment;
    }
}

static void MiniHsm_kernel(MiniHsm* self, MiniHsm_FrameEvent* __e) {
    MiniHsm_router(self, __e);
    while (self->__next_compartment != NULL) {
        MiniHsm_Compartment* next_compartment = self->__next_compartment;
        self->__next_compartment = NULL;
        // Exit the current (leaf) state
        MiniHsm_FrameEvent* __exit_event = MiniHsm_FrameEvent_new("<$", self->__compartment->exit_args, 0);
        MiniHsm_router(self, __exit_event);
        MiniHsm_FrameEvent_destroy(__exit_event);
        MiniHsm_Compartment_unref(self->__compartment);
        self->__compartment = next_compartment;
        if (next_compartment->forward_event == NULL) {
            // No forwarded event — synthesize a fresh $>
            MiniHsm_FrameEvent* __enter_event = MiniHsm_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            MiniHsm_router(self, __enter_event);
            MiniHsm_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.
            MiniHsm_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            MiniHsm_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.
            MiniHsm_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            MiniHsm_FrameEvent* __enter_event = MiniHsm_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            MiniHsm_router(self, __enter_event);
            MiniHsm_FrameEvent_destroy(__enter_event);
            MiniHsm_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++) {
            ((MiniHsm_FrameContext*)self->_context_stack->items[__i])->_transitioned = 1;
        }
    }
}

static void MiniHsm_router(MiniHsm* self, MiniHsm_FrameEvent* __e) {
    if (strcmp(self->__compartment->state, "Live") == 0) {
        MiniHsm_state_Live(self, __e, self->__compartment);
    } else if (strcmp(self->__compartment->state, "Awake") == 0) {
        MiniHsm_state_Awake(self, __e, self->__compartment);
    } else if (strcmp(self->__compartment->state, "Asleep") == 0) {
        MiniHsm_state_Asleep(self, __e, self->__compartment);
    }
}

static void MiniHsm_transition(MiniHsm* self, MiniHsm_Compartment* next_compartment) {
    self->__next_compartment = next_compartment;
}

void MiniHsm_destroy(MiniHsm* self) {
    // Unref current compartment (may free if not on stack)
    if (self->__compartment) MiniHsm_Compartment_unref(self->__compartment);
    if (self->__next_compartment) MiniHsm_Compartment_unref(self->__next_compartment);
    // Unref all state stack entries
    if (self->_state_stack) {
        for (int __i = 0; __i < self->_state_stack->size; __i++) {
            MiniHsm_Compartment_unref((MiniHsm_Compartment*)self->_state_stack->items[__i]);
        }
        MiniHsm_FrameVec_destroy(self->_state_stack);
    }
    if (self->_context_stack) MiniHsm_FrameVec_destroy(self->_context_stack);
    free(self);
}

void MiniHsm_wake(MiniHsm* self) {
    MiniHsm_FrameEvent* __e = MiniHsm_FrameEvent_new("wake", NULL, 0);
    MiniHsm_FrameContext* __ctx = MiniHsm_FrameContext_new(__e, NULL);
    MiniHsm_FrameVec_push(self->_context_stack, __ctx);
    MiniHsm_kernel(self, __e);
    MiniHsm_FrameContext* __result_ctx = (MiniHsm_FrameContext*)MiniHsm_FrameVec_pop(self->_context_stack);
    MiniHsm_FrameContext_destroy(__result_ctx);
    MiniHsm_FrameEvent_destroy(__e);
}

void MiniHsm_sleep(MiniHsm* self) {
    MiniHsm_FrameEvent* __e = MiniHsm_FrameEvent_new("sleep", NULL, 0);
    MiniHsm_FrameContext* __ctx = MiniHsm_FrameContext_new(__e, NULL);
    MiniHsm_FrameVec_push(self->_context_stack, __ctx);
    MiniHsm_kernel(self, __e);
    MiniHsm_FrameContext* __result_ctx = (MiniHsm_FrameContext*)MiniHsm_FrameVec_pop(self->_context_stack);
    MiniHsm_FrameContext_destroy(__result_ctx);
    MiniHsm_FrameEvent_destroy(__e);
}

void MiniHsm_signal(MiniHsm* self) {
    MiniHsm_FrameEvent* __e = MiniHsm_FrameEvent_new("signal", NULL, 0);
    MiniHsm_FrameContext* __ctx = MiniHsm_FrameContext_new(__e, NULL);
    MiniHsm_FrameVec_push(self->_context_stack, __ctx);
    MiniHsm_kernel(self, __e);
    MiniHsm_FrameContext* __result_ctx = (MiniHsm_FrameContext*)MiniHsm_FrameVec_pop(self->_context_stack);
    MiniHsm_FrameContext_destroy(__result_ctx);
    MiniHsm_FrameEvent_destroy(__e);
}

static void MiniHsm_state_Live(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    if (strcmp(__e->_message, "signal") == 0) {
        MiniHsm_s_Live_hdl_user_signal(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "sleep") == 0) {
        MiniHsm_s_Live_hdl_user_sleep(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "wake") == 0) {
        MiniHsm_s_Live_hdl_user_wake(self, __e, compartment);
        return;
    }
}

static void MiniHsm_state_Awake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    if (strcmp(__e->_message, "$>") == 0) {
        MiniHsm_s_Awake_hdl_frame_enter(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "signal") == 0) {
        MiniHsm_s_Awake_hdl_user_signal(self, __e, compartment);
        return;
    }
}

static void MiniHsm_state_Asleep(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    if (strcmp(__e->_message, "$>") == 0) {
        MiniHsm_s_Asleep_hdl_frame_enter(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "signal") == 0) {
        MiniHsm_s_Asleep_hdl_user_signal(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "wake") == 0) {
        MiniHsm_s_Asleep_hdl_user_wake(self, __e, compartment);
        return;
    }
}

static void MiniHsm_s_Live_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
}

static void MiniHsm_s_Live_hdl_user_sleep(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
}

static void MiniHsm_s_Live_hdl_user_wake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
}

static void MiniHsm_s_Awake_hdl_frame_enter(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    self.awakes = self.awakes + 1
}

static void MiniHsm_s_Awake_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    self.last = 1;
    MiniHsm_state_Live(self, __e, compartment->parent_compartment);
}

static void MiniHsm_s_Asleep_hdl_frame_enter(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    self.sleeps = self.sleeps + 1
}

static void MiniHsm_s_Asleep_hdl_user_signal(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    self.last = 2;
    MiniHsm_state_Live(self, __e, compartment->parent_compartment);
}

static void MiniHsm_s_Asleep_hdl_user_wake(MiniHsm* self, MiniHsm_FrameEvent* __e, MiniHsm_Compartment* compartment) {
    {
        MiniHsm_FrameVec* __sa = NULL;
        MiniHsm_FrameVec* __ea = NULL;
        MiniHsm_Compartment* __compartment = MiniHsm_prepareEnter(self, "Awake", __sa, __ea);
        MiniHsm_transition(self, __compartment);
    }
    return;
}
