#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
#ifdef MEDIA699_X31J_AU2E_AU3E
PROGRAM_STATE* G7128_LastProgramState;
int16_t G7129_ProgramStateOrdinal;
int16_t (*G7125_)();
unsigned int16_t G7126_;
unsigned int16_t G7127_CleanupHandlerCount;
CLEANUP_HANDLER G2252_CleanupHandlers[5];
#endif
#ifdef MEDIA670_F31E_F31J
PROGRAM_STATE* G7128_LastProgramState;
int16_t G7129_ProgramStateOrdinal;
int16_t (*G7125_)();
unsigned int16_t G7126_;
unsigned int16_t G7127_CleanupHandlerCount;
CLEANUP_HANDLER G2252_CleanupHandlers[5];
#endif


int16_t F7105_HangIfThirdParameterNot0(int16_t P3789_i_, char* P3790_pc_, int16_t P3791_i_)
{
        int16_t L4091_i_;


        while (P3791_i_) {
                L4091_i_ = 0;
        }
        return 1;
}

void F7106_InitExceptionManagement(
)
{
        G7129_ProgramStateOrdinal = 0;
        G7125_ = (int16_t (*)())F7105_HangIfThirdParameterNot0;
        G7126_ = 0;
        G7127_CleanupHandlerCount = 0;
        G7128_LastProgramState = NULL;
}

void F7107_EmptyFunction(
)
{
}

unsigned int16_t F7108_GetG7126_(
)
{
        return G7126_;
}

void F7109_AddCleanupHandler(int16_t (*P3792_pfi_)(unsigned int16_t, long), int16_t P3793_i_HandlerID)
{

        G2252_CleanupHandlers[G7127_CleanupHandlerCount].HandlerID = P3793_i_HandlerID;
        G2252_CleanupHandlers[G7127_CleanupHandlerCount++].HandlerFunction = P3792_pfi_;
}

void F7110_RemoveCleanupHandlerByID(int16_t P3794_i_HandlerID)
{
        REGISTER CLEANUP_HANDLER* L4092_ps_;
        REGISTER int L4093_i_;


        for (L4093_i_ = 0, L4092_ps_ = G2252_CleanupHandlers; L4093_i_ < G7127_CleanupHandlerCount; L4093_i_++, L4092_ps_++) {
                if (L4092_ps_->HandlerID == P3794_i_HandlerID) {
                        F7317_MAIN_CopyBytes_Alt((char*)(L4092_ps_ + 1), (char*)L4092_ps_, MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, (long)((G7127_CleanupHandlerCount - L4093_i_ - 1) * sizeof(CLEANUP_HANDLER)));
                        G7127_CleanupHandlerCount--;
                        break;
                }
        }
}

void F7111_RestorePreviousProgramState(
)
{
        REGISTER PROGRAM_STATE* L4094_ps_;
        REGISTER PROGRAM_STATE* L4095_ps_;


        L4094_ps_ = G7128_LastProgramState;
        G7129_ProgramStateOrdinal--;
        G7128_LastProgramState = L4095_ps_ = L4094_ps_->Previous;
        if (L4094_ps_->ReturnValue && (L4095_ps_ != NULL)) {
                L4095_ps_->ReturnValue = L4094_ps_->ReturnValue;
                longjmp(L4095_ps_->State, L4094_ps_->ReturnValue);
        }
}

int16_t F7112_FallbackToLastProgramState(BOOLEAN P3795_B_Hang, REGISTER unsigned int16_t P3796_ui_ReturnCode, long P3797_l_)
{
        REGISTER CLEANUP_HANDLER* L4096_ps_;
#ifdef MEDIA671_F31E_F31J_P31J
        unsigned int16_t L4097_i_;
#endif
#ifdef MEDIA699_X31J_AU2E_AU3E
        register unsigned int16_t L4097_i_;
#endif
        int16_t L4098_i_;


        G7126_ = P3796_ui_ReturnCode;
        if (P3796_ui_ReturnCode) {
                L4096_ps_ = &G2252_CleanupHandlers[M001_ORDINAL_TO_INDEX(L4097_i_ = G7127_CleanupHandlerCount)];
                while (L4097_i_) {
                        L4098_i_ = L4096_ps_->HandlerFunction(P3796_ui_ReturnCode, P3797_l_);
                        if ((L4098_i_ == 0x1812) && !P3795_B_Hang) {
                                return 0x1812;
                        }
                        if (L4098_i_ == 0x1810) {
                                P3796_ui_ReturnCode = 0x1810;
                                break;
                        }
                        L4097_i_--;
                        L4096_ps_--;
                }
                if (P3795_B_Hang || (G7128_LastProgramState == NULL)) {
                        G7125_(P3796_ui_ReturnCode, P3797_l_, 1); /* Hangs the program execution */
                }
                G7128_LastProgramState->ReturnValue = P3796_ui_ReturnCode;
                T7112001:
                longjmp(G7128_LastProgramState->State, P3796_ui_ReturnCode);
        }
        return P3796_ui_ReturnCode;
}
#endif
