#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
extern int16_t F0913_DecompressPAK();
extern FTL_LIBRARY* PRIM_08_Library_Open();
extern FTL_LIBRARY* F9008_OpenPRIM();
extern FTL_LIBRARY* F9004_OpenVDEO();
extern FTL_LIBRARY* F9000_OpenGRF1();
extern FTL_LIBRARY* F9002_OpenUSIO();

extern char G3257_;
extern char G3256_[16][4];
extern char G3258_[2754];
extern char G3259_;
extern char G3260_[16][4];


FTL_LIBRARY* G9008_ps_VDEObase;
FTL_LIBRARY* G9007_ps_GRF1base;
FTL_LIBRARY* G9006_ps_USIObase;
FUNCTION_HOOK G9005_s_FunctionHook;

#include "PRIMSTUB.C"

int16_t main(P9010_ps_FTLLibrary, P3547_ppv_)
FTL_LIBRARY* P9010_ps_FTLLibrary;
void** P3547_ppv_;
{
        long L2158_l_;
        FTL_EXECUTABLE L2157_s_;
        long L4674_l_Unreferenced;
        long L2156_l_;
#ifdef MEDIA639_A31E_A31M_AU2E
        USIODATA L2155_s_;
#endif


        if (F9008_OpenPRIM(P9010_ps_FTLLibrary, 4)) {
                PRIM_04_Library_HookFunction(&G9005_s_FunctionHook, P9010_ps_FTLLibrary, (long)25, P3547_ppv_, F0913_DecompressPAK); /* Function 25 is F6049_PRIM_25_FTL_DecompressCODESegment */
                PRIM_08_Library_Open(FTL_CNFG, 0L);
#ifdef MEDIA639_A31E_A31M_AU2E
                if (G9008_ps_VDEObase = F9004_OpenVDEO(2)) {
                        if (G9007_ps_GRF1base = F9000_OpenGRF1(2)) {
#endif
#ifdef MEDIA692_X31J
                if (G9007_ps_GRF1base = F9000_OpenGRF1(2)) {
                        if (G9008_ps_VDEObase = F9004_OpenVDEO(2)) {
                                VDEO_19_Operation(1, 0L);
#endif
                                if (PRIM_26_FTL_LoadAndExecute(FTL_SWSH, &L2157_s_, 0L));
                                L2157_s_.Parameter = FTL_TITL;
                                if (PRIM_26_FTL_LoadAndExecute(FTL_ANIM, &L2157_s_, 0L));
#ifdef MEDIA639_A31E_A31M_AU2E
                                if (G9006_ps_USIObase = F9002_OpenUSIO(3)) {
#endif
#ifdef MEDIA692_X31J
                                if (G9006_ps_USIObase = F9002_OpenUSIO(2)) {
#endif
#ifdef MEDIA639_A31E_A31M_AU2E
                                        USIO_19_LockDF0(C1_TRUE);
#endif
                                        L2157_s_.Parameter = FTL_APPB;
                                        L2158_l_ = 0;
                                        while (L2157_s_.Parameter != 0) {
#ifdef MEDIA639_A31E_A31M_AU2E
                                                if ((L2157_s_.Parameter == FTL_ANIM) || (L2157_s_.Parameter == FTL_TITL) || (L2157_s_.Parameter == FTL_ENDA)) {
                                                        L2156_l_ = FTL_ANIM;
                                                } else {
#ifdef MEDIA664_A31M_AU2E
                                                        if (L2157_s_.Parameter == FTL_ENGL) {
                                                                L2156_l_ = FTL_KAOS;
                                                                L2157_s_.Parameter = 0;
                                                        } else {
                                                                if (L2157_s_.Parameter == FTL_FNCH) {
                                                                        L2156_l_ = FTL_KAOS;
                                                                        L2157_s_.Parameter = 1;
                                                                } else {
                                                                        if (L2157_s_.Parameter == FTL_GRMN) {
                                                                                L2156_l_ = FTL_KAOS;
                                                                                L2157_s_.Parameter = 2;
                                                                        } else {
#endif
                                                                                L2156_l_ = L2157_s_.Parameter;
                                                                                L2157_s_.Parameter = 0;
#ifdef MEDIA664_A31M_AU2E
                                                                        }
                                                                }
                                                        }
#endif
                                                }
                                                while (!USIO_19_LockDF0(C0_FALSE)) {
                                                        VDEO_15_ApplyNormalColorPalette(G9002_VDEObase->_b_0x54_ColorCount, G9002_VDEObase->_pc_0x56_ColorComponents, 1);
                                                        VDEO_16_PaletteControl(1);
                                                        VDEO_14_SetNormalColorPalette(G3257_, G3256_);
#endif
#ifdef MEDIA692_X31J
                                                L2156_l_ = L2157_s_.Parameter;
                                                while (C0_FALSE) { /* Never executed */
                                                        VDEO_15_ApplyNormalColorPalette(G3259_, G3260_, 1);
#endif
                                                        VDEO_05_ExpandGraphicToBitmap(G3258_, 0);
                                                        VDEO_15_ApplyNormalColorPalette(G3257_, G3256_, 1);
#ifdef MEDIA639_A31E_A31M_AU2E
                                                        VDEO_16_PaletteControl(0);
                                                        while (USIO_16_ExtractFirstUsioDataFromQueue(&L2155_s_));
                                                        USIO_17_WaitUntilKeyboardOrMouseInput(&L2155_s_);
                                                        VDEO_15_ApplyNormalColorPalette(G9002_VDEObase->_b_0x54_ColorCount, G9002_VDEObase->_pc_0x56_ColorComponents, 1);
#endif
#ifdef MEDIA692_X31J
                                                        USIO_17_WaitUntilKeyboardOrMouseInput(NULL); /* NULL pointer would break function call */
                                                        VDEO_15_ApplyNormalColorPalette(G3259_, G3260_, 1);
#endif
                                                }
#ifdef MEDIA692_X31J
                                                if ((L2156_l_ == FTL_KAOS) || (L2156_l_ == FTL_ANIM)) {
                                                        PRIM_10_Library_FlagAsCanBeExpunged(G9006_ps_USIObase);
                                                        F9003_CloseUSIO(G9006_ps_USIObase);
                                                        if (L2156_l_ == FTL_KAOS) {
                                                                VDEO_19_Operation(2, 0L);
                                                        }
                                                }
                                                if ((L2156_l_ == FTL_ENDA) || (L2156_l_ == FTL_ANIM)) {
                                                        L2157_s_.Parameter = L2156_l_;
                                                        L2156_l_ = FTL_ANIM;
                                                } else {
                                                        L2157_s_.Parameter = 0;
                                                }
#endif
                                                if (PRIM_26_FTL_LoadAndExecute(L2156_l_, &L2157_s_, L2158_l_)) {
                                                        break;
                                                }
#ifdef MEDIA692_X31J
                                                if ((L2156_l_ == FTL_KAOS) || (L2156_l_ == FTL_ANIM)) {
                                                        if (L2156_l_ == FTL_KAOS) {
                                                                VDEO_19_Operation(1, 0L);
                                                        }
                                                        if ((G9006_ps_USIObase = F9002_OpenUSIO(2)) == NULL) {
                                                                break;
                                                        }
                                                        if (L2156_l_ != FTL_KAOS) {
                                                                L2157_s_.Parameter = FTL_APPB;
                                                        }
                                                }
#endif
                                                L2158_l_++;
                                        }
                                }
                        }
                }
        }
#ifdef MEDIA692_X31J
        VDEO_19_Operation(2, 0L);
#endif
        F9003_CloseUSIO(G9006_ps_USIObase);
#ifdef MEDIA639_A31E_A31M_AU2E
        F9001_CloseGRF1(G9007_ps_GRF1base);
        F9005_CloseVDEO(G9008_ps_VDEObase);
#endif
#ifdef MEDIA692_X31J
        F9005_CloseVDEO(G9008_ps_VDEObase);
        F9001_CloseGRF1(G9007_ps_GRF1base);
#endif
        PRIM_09_Library_Close(FTL_CNFG);
        PRIM_28_Library_UnhookFunction(&G9005_s_FunctionHook);
        F9009_ClosePRIM();
        return 0;
}
#endif
