[%### An example of how to generate C source code ###%]
[%# this next line refers to the generated file... %]
[% META namespace_prefix = "C" %]
/* --BEGIN-- [% autogenerated_message %] */

/* Preamble */
[% machine.preamble %]

enum [% machine.enum_id %]_states {
    UNKNOWN_STATE = 0,
[% FOREACH state = machine.states %]
    [% machine.enum_id %]_[% state.enum_id %] = [% state.number %],
[% END %]
} ;

[%# Non-MPC: static enum STATES_ENUM ... %]
static int [% machine.enum_id %]_state = UNKNOWN_STATE ;

/* Event handlers */

[% MACRO Exec(code_list) BLOCK %]
[% FOREACH code = code_list %]
        { [% code %] ;}
[% END %]
[% END %]

[% FOREACH event = machine.events %]
/* EVENT [% event.id %] */
[%   IF event.api %]
void [% machine.enum_id %]_[% event.api %] {
[%     Exec( event.pre_handlers ) %]

    switch ( [% machine.enum_id %]_state ) {
[%     FOREACH arc =  event.arcs %]
    case [% machine.enum_id %]_[% arc.from_state.enum_id %]:
[%       IF arc.from_state != arc.to_state %]
[%         Exec(arc.from_state.exit_handlers) %]
[%         IF event.handlers || arc.handlers %]
        [% machine.enum_id %]_state = UNKNOWN_STATE ;
[%         END %]
[%       END %]
[%       Exec(event.handlers) %]
[%       Exec(arc.handlers) %]
[%       IF arc.from_state != arc.to_state %]
        [% machine.enum_id %]_state = [% machine.enum_id %]_[% arc.to_state.enum_id %] ;
[%         Exec(arc.to_state.entry_handlers) %]
[%       END %]
        break ;
[%     END # FOREACH arc %]
    default:
[%     arc = machine.all_state_arc_for_event( event ) %]
[%     Exec(arc.to_state.exit_handlers) %]
[%     IF arc && arc.to != "#ALL" %]
        /* Handle #ALL arcs even from invalid/UNKNOWN states. */
        [% machine.enum_id %]_state = UNKNOWN_STATE ;
[%     END %]
[%         Exec(event.handlers) %]
[%     IF arc %]
[%         Exec(arc.handlers) %]
[%       IF arc.to != "#ALL" %]
        [% machine.enum_id %]_state = [% machine.enum_id %]_[% arc.to_state.enum_id %] ;
[%       END %]
[%       Exec(arc.to_state.entry_handlers) %]
[%     END %]
        break ;
    }
[%     Exec( event.post_handlers ) %]
}

[%   ELSE %]
/* No API declared for event [% event.id %] */
[%   END %]

[% END # FOREACH event %]

/* Postamble */
[% machine.postamble %]

/* --END-- [% machine.autogenerated_warning %] */