#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA772_SU1E
extern unsigned char** F7227_RequestMemoryHandle(unsigned long P3976_ul_RequestedByteCount, int16_t P3977_i_Requirements);
extern void F1800_();
extern void F1801_();
extern void F1825_FreeMemoryHeap();
extern long F7228_FreeMemory();
extern unsigned char* F7229_();
extern void F7230_();
extern void F7317_MAIN_CopyBytes_Alt();

extern long G1086_VerticalBlankCount;

char G3555_[16];
int16_t** G3554_;
unsigned int16_t G3569_aui_Palette[16];
int16_t G3568_OpCodeParameterCount[31] = { -1, 0, 0, 3, 1, 3, 4, 1, 2, 0, 1, 1, 2, 0, 2, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 2, 3 };
STRUCT_32_B G3567_as_AnimationItems[200]; /* #0: Screen 1, #1: Screen 2, #2: Palette */



int16_t F1792_LoadAnimationScript(char* P5523_pc_FileName, char*** P5524_, int16_t P5525_, unsigned long* P5526_pul_ByteCount) {
        int16_t L5264_i_FileHandle;
        long L5445_l_Unreferenced;
        int16_t L5265_i_Result;


        L5264_i_FileHandle = Fopen(P5523_pc_FileName, C0_READ_ONLY);
        if (L5264_i_FileHandle < 0) {
                L5265_i_Result = -1;
                goto loc_1B26;
        }
        *P5526_pul_ByteCount = Fseek(0L, L5264_i_FileHandle, C2_SEEK_FROM_END_OF_FILE);
        Fseek(0L, L5264_i_FileHandle, C0_SEEK_FROM_BEGINNING_OF_FILE) >= 0;
        if (*P5524_ == NULL) {
                *P5524_ = (char**)F7227_RequestMemoryHandle((long)*P5526_pul_ByteCount, P5525_);
                if ((*P5524_ == NULL) != C0_FALSE) {
                        L5265_i_Result = -2;
                        goto loc_1B26;
                }
        }
        if (Fread(L5264_i_FileHandle, (long)*P5526_pul_ByteCount, **P5524_) != *P5526_pul_ByteCount) {
                L5265_i_Result = -3;
                goto loc_1B26;
        } else {
                L5265_i_Result = 0;
        }
        loc_1B26:
        Fclose(L5264_i_FileHandle);
        if (L5265_i_Result);
        return L5265_i_Result;
}

void F1793_(
)
{
        register int16_t L5266_;
        char* L5267_;
        long* L5268_pl_AnimationScriptByteCount;
        unsigned char* L5269_puc_AnimationScript;


        G3554_ = NULL;
        L5269_puc_AnimationScript = NULL;
        if (!F1799_LoadAnimationData("A:\\ANIMATE.DAT", G3555_, &G3554_)) {
                L5267_ = "glib_open";
        } else {
                L5269_puc_AnimationScript = NULL;
                if (F1792_LoadAnimationScript("A:\\ANIMATE.SCR", &L5269_puc_AnimationScript, MASK0x0400_MEMREQ, &L5268_pl_AnimationScriptByteCount) < 0) {
                        L5267_ = "getting scriptfile";
                } else {
                        G1086_VerticalBlankCount = 0;
                        F1786_();
                        F1798_(L5269_puc_AnimationScript, L5268_pl_AnimationScriptByteCount);
                        F1787_();
                        F1800_();
                        L5267_ = NULL;
                }
        }
        if (L5269_puc_AnimationScript) {
                F7228_FreeMemory(L5269_puc_AnimationScript);
        }
        if (G3554_) {
                F7228_FreeMemory(G3554_);
        }
        if (L5267_); /* Useless code */
}

void main(
)
{
        static char* G3558_ac_NextFTLModuleToRun = "A:\\FTLCODE";


        F1759_();
        F1525_OpenVDIWorkstation(0);
        if (Getrez()) {
                Cconws("MUST RUN IN LOW RES\n\r");
                for (;;);
        }
        G0578_B_UseByteBoxCoordinates = C0_FALSE;
        G0348_Bitmap_Screen = Physbase();
        F1372_SetVerticalBlankInterrupt(1);
        F1824_AllocateMemoryHeap(100, 100);
        F1793_();
        F1825_FreeMemoryHeap();
        F1372_SetVerticalBlankInterrupt(2);
        F1526_CloseWorkstationAndExitApplication();
        asm {
                move.w  #0x6160,(a5)
                move.w  #0x5223,2(a5)
                move.l  G3558_ac_NextFTLModuleToRun(a5),0x1C(a5)
        }
}

void F1795_Prepare(STRUCT_32_B* P5527_, int16_t P5528_) {
        int16_t L5270_;
        char* L5271_;
        int16_t L5272_;


        for (L5272_ = 0; L5272_ < P5528_; L5272_++) {
                P5527_[L5272_]._puc_0x02 = NULL;
        }
        P5527_[L5272_]._puc_0x06 = 0;
        P5527_[0]._puc_0x06 = (long)Logbase();
        M768_BOX_LEFT(P5527_[0]._s_0x0C_Box) = 0;
        M769_BOX_RIGHT(P5527_[0]._s_0x0C_Box) = 319;
        M770_BOX_TOP(P5527_[0]._s_0x0C_Box) = 0;
        M771_BOX_BOTTOM(P5527_[0]._s_0x0C_Box) = 199;
        P5527_[0]._i_0x16_Width = C160_BYTE_WIDTH_SCREEN;
        P5527_[0]._i_0x18_Height = C200_HEIGHT_SCREEN;
        P5527_[0]._i_0x14_TransparentColor = CM1_COLOR_NO_TRANSPARENCY;
        P5527_[1]._puc_0x02 = (long*)F7227_RequestMemoryHandle((long)(32768 + 255), MASK0x0010_MEMREQ);
        if ((P5527_[1]._puc_0x02 == NULL) != C0_FALSE) {
                L5271_ = "screen alloc";
        } else {
                P5527_[1]._puc_0x06 = *(P5527_[1]._puc_0x02) + 256;
                P5527_[1]._puc_0x06 = P5527_[1]._puc_0x06 & 0xFFFFFF00; /* Screen alignment on 256 bytes boundary */
                M768_BOX_LEFT(P5527_[1]._s_0x0C_Box) = 0;
                M769_BOX_RIGHT(P5527_[1]._s_0x0C_Box) = 319;
                M770_BOX_TOP(P5527_[1]._s_0x0C_Box) = 0;
                M771_BOX_BOTTOM(P5527_[1]._s_0x0C_Box) = 199;
                P5527_[1]._i_0x16_Width = C160_BYTE_WIDTH_SCREEN;
                P5527_[1]._i_0x18_Height = C200_HEIGHT_SCREEN;
                P5527_[1]._i_0x14_TransparentColor = CM1_COLOR_NO_TRANSPARENCY;
                P5527_[2]._puc_0x06 = (long)G3569_aui_Palette;
                L5271_ = NULL;
        }
        if (L5271_) {
                L5270_ = -1;
        } else {
                L5270_ = 0;
        }
}

void F1796_Clean(STRUCT_32_B* P5529_, int16_t P5530_) {
        int16_t L5273_;


        for (L5273_ = 0; L5273_ < P5530_; L5273_++) {
                if (P5529_[L5273_]._puc_0x02) {
                        F7228_FreeMemory(P5529_[L5273_]._puc_0x02);
                        P5529_[L5273_]._puc_0x02 = NULL;
                }
        }
}

int16_t F1797_ReadInstructionAndParameters(int16_t** P5531_ppi_AnimationData, int16_t* P5532_pi_InstructionAndParameters) {
        int16_t L5274_;
        long L5275_;
        int16_t* L5276_pi_Instruction;
        int16_t L5277_;


        L5276_pi_Instruction = *P5531_ppi_AnimationData;
        *P5532_pi_InstructionAndParameters = *L5276_pi_Instruction;
        for (L5277_ = 0; L5277_ < G3568_OpCodeParameterCount[*P5532_pi_InstructionAndParameters]; L5277_++) {
                *(P5532_pi_InstructionAndParameters + L5277_ + 1) = L5276_pi_Instruction[L5277_ + 1];
        }
        *P5531_ppi_AnimationData += G3568_OpCodeParameterCount[*P5532_pi_InstructionAndParameters] + 1;
        if ((long)*P5531_ppi_AnimationData & 1) {
                (*P5531_ppi_AnimationData)++;
        }
        L5275_ = 0;
        if (L5275_) {
                L5274_ = -1;
        } else {
                L5274_ = 0;
        }
        return L5274_;
}

int16_t F1798_(char* P5533_, long* P5534_) {
        int16_t L5281_;
        int16_t L5282_;
        char* L5283_;
        long L5284_;
        unsigned long L5285_;
        long L5286_l_RequestedByteCount;
        unsigned char* L5287_puc_AnimationData;
        int16_t* L5288_;
        unsigned char* L5289_;
        int16_t L5290_; /* Instruction parameters are stored in these variables, from L5300_ to L5290_ */
        int16_t L5291_;
        int16_t L5292_;
        int16_t L5293_;
        int16_t L5294_;
        int16_t L5295_;
        int16_t L5296_;
        int16_t L5297_;
        int16_t L5298_;
        int16_t L5299_i_Destination;
        int16_t L5300_i_Source;
        int16_t L5301_; /* Instruction is loaded in this variable */


        F1795_Prepare(G3567_as_AnimationItems, 200);
        L5287_puc_AnimationData = F7229_(P5533_);
        L5284_ = (long)L5287_puc_AnimationData + (long)P5534_;
        if (L5284_ < (unsigned long)L5287_puc_AnimationData) {
                L5283_ = "script not addable";
                goto loc_27BA;
        }
        L5285_ = G1086_VerticalBlankCount;
        while ((unsigned long)L5287_puc_AnimationData < L5284_) {
                F1797_ReadInstructionAndParameters(&L5287_puc_AnimationData, &L5301_);
                for (L5282_ = 0; L5282_ < G3568_OpCodeParameterCount[L5301_]; L5282_++); /* Useless code */
                switch (L5301_) {
                        case 1: /* Done */
                        case 2:
                                goto loc_27B6;
                        case 3: /* Load item */
                                if (!G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02) {
                                        L5286_l_RequestedByteCount = (*G3554_)[L5300_i_Source];
                                        G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02 = (long*)F7227_RequestMemoryHandle(L5286_l_RequestedByteCount, MASK0x0400_MEMREQ);
                                        if ((G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02 == NULL) != 0) {
                                                L5283_ = "no mem";
                                                goto loc_27BA;
                                        }
                                        F1801_((long)*(G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02), L5300_i_Source);
                                        switch (L5298_) {
                                                case 0:
                                                        L5288_ = (int16_t*)*(G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02);
                                                        G3567_as_AnimationItems[L5299_i_Destination]._i_0x16_Width = L5288_[0] >> 1;
                                                        G3567_as_AnimationItems[L5299_i_Destination]._i_0x18_Height = L5288_[1];
                                                        M768_BOX_LEFT(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = 0;
                                                        M770_BOX_TOP(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = 0;
                                                        M769_BOX_RIGHT(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = L5288_[0] - 1;
                                                        M771_BOX_BOTTOM(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = L5288_[1] - 1;
                                                        G3567_as_AnimationItems[L5299_i_Destination]._i_0x14_TransparentColor = -1;
                                        }
                                }
                                break;
                        case 4: /* Unload item */
                                if (G3567_as_AnimationItems[L5300_i_Source]._puc_0x02) {
                                        F7228_FreeMemory(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02);
                                        G3567_as_AnimationItems[L5300_i_Source]._puc_0x02 = NULL;
                                }
                                break;
                        case 5: /* Display image */
                                if (L5299_i_Destination <= 1) {
                                        L5289_ = (unsigned char*)G3567_as_AnimationItems[L5299_i_Destination]._puc_0x06;
                                } else {
                                        L5289_ = (unsigned char*)*(G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02);
                                }
                                F0466_EXPAND_GraphicToBitmap((unsigned char*)*(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02), L5289_, M768_BOX_LEFT(G3567_as_AnimationItems[L5298_]._s_0x0C_Box), M770_BOX_TOP(G3567_as_AnimationItems[L5298_]._s_0x0C_Box));
                                break;
                        case 6: /*  */
                                if (L5300_i_Source <= 1) {
                                        L5288_ = (int16_t*)G3567_as_AnimationItems[L5300_i_Source]._puc_0x06;
                                } else {
                                        L5288_ = (int16_t*)*(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02);
                                }
                                if (L5299_i_Destination <= 1) {
                                        L5289_ = (unsigned char*)G3567_as_AnimationItems[L5299_i_Destination]._puc_0x06;
                                } else {
                                        L5289_ = (unsigned char*)*(G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02);
                                }
                                M526_WaitVerticalBlank();
                                F0132_VIDEO_Blit(L5288_, L5289_, &G3567_as_AnimationItems[L5297_]._s_0x0C_Box, M768_BOX_LEFT(G3567_as_AnimationItems[L5298_]._s_0x0C_Box), M770_BOX_TOP(G3567_as_AnimationItems[L5298_]._s_0x0C_Box), G3567_as_AnimationItems[L5300_i_Source]._i_0x16_Width, G3567_as_AnimationItems[L5299_i_Destination]._i_0x16_Width, G3567_as_AnimationItems[L5299_i_Destination]._i_0x14_TransparentColor);
                                break;
                        case 7: /* Set palette */
                                if (L5300_i_Source == 2) {
                                        L5288_ = (int16_t*)G3567_as_AnimationItems[L5300_i_Source]._puc_0x06;
                                } else {
                                        L5288_ = (int16_t*)*(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02);
                                }
                                M526_WaitVerticalBlank();
                                Setpalette(L5288_);
                                break;
                        case 8: /* Fade to palette */
                                if (L5300_i_Source == 2) {
                                        L5288_ = (int16_t*)G3567_as_AnimationItems[L5300_i_Source]._puc_0x06;
                                } else {
                                        L5288_ = (int16_t*)*(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02);
                                }
                                F0436_STARTEND_FadeToPalette(L5288_, L5299_i_Destination);
                                break;
                        case 9: /*  */
                                L5285_ = 0;
                                break;
                        case 10: /* Wait 1 */
                                L5282_ = L5300_i_Source;
                                while (L5282_ > 0) {
                                        M526_WaitVerticalBlank();
                                        L5282_--;
                                }
                                break;
                        case 11: /* Wait 2 */
                                while (L5285_ + L5300_i_Source > G1086_VerticalBlankCount);
                                break;
                        case 12: /* Play sound */
                                F0060_SOUND_Play_CPSX((unsigned char*)*(G3567_as_AnimationItems[L5300_i_Source]._puc_0x02), L5299_i_Destination, 1);
                                break;
                        case 13: /* Wait until key pressed */
                                while (Cconis()) {
                                        Crawcin();
                                }
                                Crawcin();
                                break;
                        case 14: /* Set screen and palette */
                                M526_WaitVerticalBlank();
                                Setscreen(G3567_as_AnimationItems[L5300_i_Source]._puc_0x06, G3567_as_AnimationItems[L5300_i_Source]._puc_0x06, -1);
                                if (L5299_i_Destination != -1) {
                                        if (L5299_i_Destination == 2) {
                                                L5288_ = (int16_t*)G3567_as_AnimationItems[L5299_i_Destination]._puc_0x06;
                                        } else {
                                                L5288_ = (int16_t*)*(G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02);
                                        }
                                        Setpalette(L5288_);
                                }
                                break;
                        case 15: /*  */
                                M526_WaitVerticalBlank();
                                break;
                        case 16: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._puc_0x06 = (long)L5287_puc_AnimationData;
                                break;
                        case 17: /*  */
                                M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box)--;
                                break;
                        case 18: /*  */
                                if (M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) > 0) {
                                        L5287_puc_AnimationData = (unsigned char*)G3567_as_AnimationItems[L5299_i_Destination]._puc_0x06;
                                }
                                break;
                        case 28: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x1E = L5290_;
                        case 27: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x1C = L5291_;
                        case 26: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x1A = L5292_;
                        case 25: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x18_Height = L5293_;
                        case 24: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x16_Width = L5294_;
                        case 23: /*  */
                                G3567_as_AnimationItems[L5300_i_Source]._i_0x14_TransparentColor = L5295_;
                        case 22: /*  */
                                M771_BOX_BOTTOM(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5296_;
                        case 21: /*  */
                                M770_BOX_TOP(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5297_;
                        case 20: /*  */
                                M769_BOX_RIGHT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5298_;
                        case 19: /*  */
                                M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5299_i_Destination;
                                break;
                        case 29: /* Copy items to other slots? Link? */
                                L5286_l_RequestedByteCount = G3567_as_AnimationItems[L5300_i_Source]._i_0x16_Width * G3567_as_AnimationItems[L5300_i_Source]._i_0x18_Height * 4;
                                G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02 = (long*)F7227_RequestMemoryHandle(L5286_l_RequestedByteCount, MASK0x0400_MEMREQ);
                                if ((G3567_as_AnimationItems[L5299_i_Destination]._puc_0x02 == NULL) != C0_FALSE) {
                                        L5283_ = "no mem";
                                        goto loc_27BA;
                                }
                                F7317_MAIN_CopyBytes_Alt(&G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box, &G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box, MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, 20L);
                                M768_BOX_LEFT(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = 0;
                                M770_BOX_TOP(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = 0;
                                M769_BOX_RIGHT(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = M769_BOX_RIGHT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) - M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                                M771_BOX_BOTTOM(G3567_as_AnimationItems[L5299_i_Destination]._s_0x0C_Box) = M771_BOX_BOTTOM(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) - M770_BOX_TOP(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                                break;
                        case 30: /* Set Display Coordinates of top left corner of image */
                                M769_BOX_RIGHT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = M769_BOX_RIGHT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) - M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                                M771_BOX_BOTTOM(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = M771_BOX_BOTTOM(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) - M770_BOX_TOP(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                                M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5299_i_Destination;
                                M770_BOX_TOP(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) = L5298_;
                                M769_BOX_RIGHT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) += M768_BOX_LEFT(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                                M771_BOX_BOTTOM(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box) += M770_BOX_TOP(G3567_as_AnimationItems[L5300_i_Source]._s_0x0C_Box);
                }
        }
        loc_27B6:
        L5283_ = NULL;
        loc_27BA:
        F1796_Clean(G3567_as_AnimationItems, 200);
        F7230_(P5533_);
        if (L5283_) {
                L5281_ = -1;
        } else {
                L5281_ = 0;
        }
        return 0;
}
#endif
#ifdef MEDIA650_A31E_A35M_X31J_AU1E_AU2F
extern void F1207_();

typedef struct {
        int16_t Type;
        unsigned int16_t ByteCount;
        int16_t Attribute;
} ANIMSTEP;

typedef struct { /* Amiga: Size 278 = 632. X68000: Size 284 = 644  */
        long _l_0x000;
        struct {
                int16_t LoopCount;
                unsigned char* ItemOffset;
        } _as_0x004_ForLoops[16];
        unsigned int16_t _w_0x064;
        unsigned char* _pc_0x066_SDData[32];
        unsigned int16_t _w_0x0E6_SDCount;
        unsigned char* _pc_0x0E8_MDData[32];
        unsigned int16_t _w_0x168_MDCount;
        unsigned char* _apuc_0x16A_SFData[32];
        unsigned int16_t _w_0x1EA_SFCount;
        unsigned char* _apuc_0x1EC_MFData[32];
        unsigned int16_t _w_0x26C_MFCount;
        int16_t _B_0x26E_Breakable;
        char* _pc_0x270;
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        unsigned long _ui_0x274;
#endif
#ifdef MEDIA692_X31J
        unsigned int16_t _ui_0x274;
        unsigned int16_t _ui_0x276;
        unsigned int16_t _ui_0x278;
        unsigned long _l_0x27A;
        char Unreferenced[6];
#endif
} ANIMDESC;

typedef union {
        struct {
                unsigned char Index;
                unsigned char Red;
                unsigned char Green;
                unsigned char Blue;
        } Components;
        long ColorDef;
} COLOR_DEF;

typedef struct {
        int16_t ColorCount;
        COLOR_DEF Colors[1];
} PL_DATA;

typedef struct {
        unsigned char LeftVolume;
        unsigned char RightVolume;
        int16_t Period;
} SO_DATA;


extern FTL_LIBRARY* F9008_OpenPRIM();
extern FTL_LIBRARY* F9004_OpenVDEO();
extern FTL_LIBRARY* F9000_OpenGRF1();
extern FTL_LIBRARY* F9006_OpenMEM1();
extern FTL_LIBRARY* F9002_OpenUSIO();
extern FTL_LIBRARY* F9016_OpenMUSC();
extern unsigned char* MEM1_06_Allocate();
extern VDEO_15_ApplyNormalColorPalette(int16_t P4193_, char* P4194_, int16_t P4195_);

extern void F1198_();
extern void F1200_();
extern void F1203_();
extern void F1204_();
extern void F1205_();
extern void F1206_UpdateColorPalette();
extern void F1208_();
extern void F1209_();
extern void F1210_();
extern void F1211_();
extern unsigned char* F1212_();
extern void F1213_();
extern unsigned char* F1214_();
extern void F1215_();
extern void F1216_();
extern void F1217_();
extern void F1218_();
extern void F1219_();
extern void F1220_();
extern BOOLEAN F1221_AllocMemForSD();
extern void F1222_FreeMemForSD();
extern int16_t F1223_(ANIMDESC* P4314_, unsigned int16_t* P4315_pui_SoundData, unsigned char P4316_LeftVolume, unsigned char P4317_RightVolume, int16_t P4318_i_Period);
extern void F1225_();
extern void F1226_();
extern void F1227_();
extern void F1228_();
extern void F1251_(int16_t P4349_, unsigned int16_t* P4350_, long P4351_, int16_t P4352_, int16_t P4353_, int16_t P4354_);
extern long F1367_();


jmp_buf G2023_jmp_buf;
int16_t G3283_VerticalBlankWaitCount;
int16_t G3282_;
int16_t G3284_ = 1;
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
BOOLEAN G3289_;
ANIMDESC* G3287_;
int16_t G3288_;
#endif
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
unsigned char* G0348_Bitmap_Screen;
unsigned long G1086_VerticalBlankCount;
unsigned char* G3280_Screen;
unsigned char* G3281_;
#endif
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
COLOR_DEF G3286_[32];
#endif
#ifdef MEDIA762_AU1E
COLOR_DEF G3286_[16];
#endif
int16_t G3285_;


ANIMDESC* F1177_(P4254_pc_FileName)
char* P4254_pc_FileName;
{
        register ANIMDESC* L4712_ps_Buffer;
        register long L4714_l_FileSize;
        register long L4715_l_BufferSize;
        register FILE_HANDLE L4713_FileHandle;
        unsigned char* L4716_puc_AnimationData;


        L4712_ps_Buffer = NULL;
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        if (L4713_FileHandle = F0770_FILE_Open(P4254_pc_FileName)) {
#endif
#ifdef MEDIA692_X31J
        if ((L4713_FileHandle = F0770_FILE_Open(P4254_pc_FileName)) >= 0) {
#endif
                L4714_l_FileSize = F0775_FILE_GetSize(L4713_FileHandle);
                L4715_l_BufferSize = L4714_l_FileSize + sizeof(ANIMDESC);
                F0774_FILE_Seek(L4713_FileHandle, 0L);
                if (L4712_ps_Buffer = (ANIMDESC*)MEM1_06_Allocate(L4715_l_BufferSize, MASK0x0400_MEMREQ)) {
                        MEM1_05_ClearBytes(L4712_ps_Buffer, MASK0x0400_MEMREQ, (long)sizeof(ANIMDESC));
                        L4712_ps_Buffer->_l_0x000 = L4715_l_BufferSize;
                        L4712_ps_Buffer->_w_0x064 = 16;
                        L4712_ps_Buffer->_w_0x0E6_SDCount = 0;
                        L4712_ps_Buffer->_w_0x168_MDCount = 0;
                        L4712_ps_Buffer->_B_0x26E_Breakable = 0;
                        L4716_puc_AnimationData = (char*)L4712_ps_Buffer + sizeof(ANIMDESC);
                        if (F0772_FILE_Read(L4713_FileHandle, L4714_l_FileSize, L4716_puc_AnimationData)) {
                                if (F1202_(L4712_ps_Buffer)) {
                                        F0771_FILE_Close(L4713_FileHandle);
                                        return L4712_ps_Buffer;
                                }
                        }
                        MEM1_07_Free(L4712_ps_Buffer);
                }
                F0771_FILE_Close(L4713_FileHandle);
        }
        return NULL;
}

void F1178_(P4255_)
ANIMDESC* P4255_;
{
        if (P4255_ == NULL) {
                return;
        }
        F1203_(P4255_);
        while ((int16_t)(--P4255_->_w_0x1EA_SFCount) >= 0) {
                F1213_(P4255_, P4255_->_apuc_0x16A_SFData[P4255_->_w_0x1EA_SFCount]);
        }
        while ((int16_t)(--P4255_->_w_0x26C_MFCount) >= 0) {
                F1215_(P4255_, P4255_->_apuc_0x1EC_MFData[P4255_->_w_0x26C_MFCount]);
        }
        MEM1_07_Free(P4255_);
}

ANIMSTEP* F1179_Process_AN(P4256_, P4257_)
ANIMDESC* P4256_;
ANIMSTEP* P4257_;
{
        return (char*)P4257_ + P4257_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1180_Process_BR(P4258_, P4259_)
ANIMDESC* P4258_;
ANIMSTEP* P4259_;
{
        P4258_->_B_0x26E_Breakable = P4259_->Attribute;
        return (char*)P4259_ + P4259_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1181_Process_PL(P4260_, P4261_)
ANIMDESC* P4260_;
ANIMSTEP* P4261_;
{
        register PL_DATA* L4717_;
        register int16_t L4718_ColorIndex;


        L4718_ColorIndex = 0;
        F1206_UpdateColorPalette(P4260_, -1, 0, 0, 0);
        L4717_ = (char*)P4261_ + sizeof(ANIMSTEP);
        while (L4718_ColorIndex < L4717_->ColorCount) {
                F1206_UpdateColorPalette(P4260_, (int16_t)L4717_->Colors[L4718_ColorIndex].Components.Index, (int16_t)L4717_->Colors[L4718_ColorIndex].Components.Red, (int16_t)L4717_->Colors[L4718_ColorIndex].Components.Green, (int16_t)L4717_->Colors[L4718_ColorIndex].Components.Blue);
                L4718_ColorIndex++;
        }
        F1206_UpdateColorPalette(P4260_, -2, 0, 0, 0);
        return (char*)P4261_ + P4261_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1182_Process_EN(P4262_, P4263_)
ANIMDESC* P4262_;
ANIMSTEP* P4263_;
{
        F1204_(P4262_, (char*)P4263_ + sizeof(ANIMSTEP), P4263_->Attribute);
        return (char*)P4263_ + P4263_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1183_Process_DL(P4264_, P4265_)
ANIMDESC* P4264_;
ANIMSTEP* P4265_;
{
        F1205_(P4264_, (char*)P4265_ + sizeof(ANIMSTEP), P4265_->Attribute);
        return (char*)P4265_ + P4265_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1184_Process_SD(P4266_, P4267_)
ANIMDESC* P4266_;
ANIMSTEP* P4267_;
{
        P4266_->_pc_0x066_SDData[P4266_->_w_0x0E6_SDCount++] = (unsigned char*)P4267_ + sizeof(ANIMSTEP);
        return (char*)P4267_ + P4267_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1185_Process_MD(P4268_, P4269_)
ANIMDESC* P4268_;
ANIMSTEP* P4269_;
{
        P4268_->_pc_0x0E8_MDData[P4268_->_w_0x168_MDCount++] = (unsigned char*)P4269_ + sizeof(ANIMSTEP);
        return (char*)P4269_ + P4269_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1186_Process_SO(P4270_, P4271_)
ANIMDESC* P4270_;
ANIMSTEP* P4271_;
{
        SO_DATA* L4719_;


        L4719_ = (char*)P4271_ + sizeof(ANIMSTEP);
        F1207_(P4270_, P4271_->Attribute, L4719_->LeftVolume, L4719_->RightVolume, L4719_->Period);
        return (char*)P4271_ + P4271_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1187_Process_MI(P4272_, P4273_)
ANIMDESC* P4272_;
ANIMSTEP* P4273_;
{
        F1208_(P4272_, P4273_->Attribute);
        return (char*)P4273_ + P4273_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1188_Process_SF(P4274_, P4275_)
ANIMDESC* P4274_;
ANIMSTEP* P4275_;
{
        register unsigned char* L4720_;
        register long L4722_;
        register FILE_HANDLE L4721_FileHandle;


        L4720_ = NULL;
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        if (L4721_FileHandle = F0770_FILE_Open((char*)P4275_ + sizeof(ANIMSTEP))) {
#endif
#ifdef MEDIA692_X31J
        if ((L4721_FileHandle = F0770_FILE_Open((char*)P4275_ + sizeof(ANIMSTEP))) >= 0) {
#endif
                L4722_ = F0775_FILE_GetSize(L4721_FileHandle);
                F0774_FILE_Seek(L4721_FileHandle, 0L);
                if (L4720_ = F1212_(P4274_, L4722_)) {
                        if (F0772_FILE_Read(L4721_FileHandle, L4722_, L4720_)) {
                                F0771_FILE_Close(L4721_FileHandle);
                                P4274_->_apuc_0x16A_SFData[P4274_->_w_0x1EA_SFCount++] = L4720_;
                                P4274_->_pc_0x066_SDData[P4274_->_w_0x0E6_SDCount++] = L4720_;
                                return (char*)P4275_ + P4275_->ByteCount + sizeof(ANIMSTEP);
                        }
                        F1213_(P4274_, L4720_);
                }
                F0771_FILE_Close(L4721_FileHandle);
        }
        P4274_->_pc_0x066_SDData[P4274_->_w_0x0E6_SDCount++] = NULL;
        return (char*)P4275_ + P4275_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1189_Process_MF(P4276_, P4277_)
ANIMDESC* P4276_;
ANIMSTEP* P4277_;
{
        register unsigned char* L4723_;
        register long L4725_;
        register FILE_HANDLE L4724_FileHandle;


        L4723_ = NULL;
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        if (L4724_FileHandle = F0770_FILE_Open((char*)P4277_ + sizeof(ANIMSTEP))) {
#endif
#ifdef MEDIA692_X31J
        if ((L4724_FileHandle = F0770_FILE_Open((char*)P4277_ + sizeof(ANIMSTEP))) >= 0) {
#endif
                L4725_ = F0775_FILE_GetSize(L4724_FileHandle);
                F0774_FILE_Seek(L4724_FileHandle, 0L);
                if (L4723_ = F1214_(P4276_, L4725_)) {
                        if (F0772_FILE_Read(L4724_FileHandle, L4725_, L4723_)) {
                                F0771_FILE_Close(L4724_FileHandle);
                                P4276_->_apuc_0x1EC_MFData[P4276_->_w_0x26C_MFCount++] = L4723_;
                                P4276_->_pc_0x0E8_MDData[P4276_->_w_0x168_MDCount++] = L4723_;
                                return (char*)P4277_ + P4277_->ByteCount + sizeof(ANIMSTEP);
                        }
                        F1215_(P4276_, L4723_);
                }
                F0771_FILE_Close(L4724_FileHandle);
        }
        P4276_->_pc_0x0E8_MDData[P4276_->_w_0x168_MDCount++] = NULL;
        return (char*)P4277_ + P4277_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1190_Process_FO(P4278_, P4279_)
ANIMDESC* P4278_;
ANIMSTEP* P4279_;
{
        P4278_->_as_0x004_ForLoops[--P4278_->_w_0x064].ItemOffset = (char*)P4279_ + P4279_->ByteCount + sizeof(ANIMSTEP) + ((ANIMSTEP*)((char*)P4279_ + P4279_->ByteCount + sizeof(ANIMSTEP)))->ByteCount + sizeof(ANIMSTEP);
        P4278_->_as_0x004_ForLoops[P4278_->_w_0x064].LoopCount = P4279_->Attribute - 1;
        return (char*)P4279_ + P4279_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1191_Process_NE(P4280_, P4281_)
ANIMDESC* P4280_;
ANIMSTEP* P4281_;
{
        if (P4280_->_as_0x004_ForLoops[P4280_->_w_0x064].LoopCount--) {
                return (char*)P4281_ + P4281_->ByteCount + sizeof(ANIMSTEP);
        } else {
                P4280_->_w_0x064++;
                return (char*)P4281_ + P4281_->ByteCount + sizeof(ANIMSTEP) + ((ANIMSTEP*)((char*)P4281_ + P4281_->ByteCount + sizeof(ANIMSTEP)))->ByteCount + sizeof(ANIMSTEP) + ((ANIMSTEP*)((char*)P4281_ + P4281_->ByteCount + sizeof(ANIMSTEP) + ((ANIMSTEP*)((char*)P4281_ + P4281_->ByteCount + sizeof(ANIMSTEP)))->ByteCount + sizeof(ANIMSTEP)))->ByteCount + sizeof(ANIMSTEP);
        }
}

ANIMSTEP* F1192_Process_BN(P4282_, P4283_)
ANIMDESC* P4282_;
ANIMSTEP* P4283_;
{
        return P4282_->_as_0x004_ForLoops[P4282_->_w_0x064].ItemOffset;
}

ANIMSTEP* F1193_Process_DO(P4284_, P4285_)
ANIMDESC* P4284_;
ANIMSTEP* P4285_;
{
        F1216_(P4284_, P4285_->Attribute);
        return (char*)P4285_ + P4285_->ByteCount + sizeof(ANIMSTEP);
}

#ifdef MEDIA651_A31E_A35M_X31J_AU2F
ANIMSTEP* F1194_Process_WA(P4286_, P4287_)
ANIMDESC* P4286_;
ANIMSTEP* P4287_;
{
        F1210_(P4286_, P4287_->Attribute);
        return (char*)P4287_ + P4287_->ByteCount + sizeof(ANIMSTEP);
}

ANIMSTEP* F1195_Process_TR(P4288_, P4289_)
ANIMDESC* P4288_;
ANIMSTEP* P4289_;
{
        F1211_(P4288_, P4289_->Attribute);
        return (char*)P4289_ + P4289_->ByteCount + sizeof(ANIMSTEP);
}
#endif

ANIMSTEP* F1196_(P4290_, P4291_)
register ANIMDESC* P4290_;
register ANIMSTEP* P4291_;
{
        switch (P4291_->Type) {
                case 'PL':
                        P4291_ = F1181_Process_PL(P4290_, P4291_);
                        break;
                case 'EN':
                        P4291_ = F1182_Process_EN(P4290_, P4291_);
                        break;
                case 'DL':
                        P4291_ = F1183_Process_DL(P4290_, P4291_);
                        break;
                case 'BR':
                        P4291_ = F1180_Process_BR(P4290_, P4291_);
                        break;
                case 'SO':
                        P4291_ = F1186_Process_SO(P4290_, P4291_);
                        break;
                case 'MI':
                        P4291_ = F1187_Process_MI(P4290_, P4291_);
                        break;
                case 'FO':
                        P4291_ = F1190_Process_FO(P4290_, P4291_);
                        break;
                case 'NE':
                        P4291_ = F1191_Process_NE(P4290_, P4291_);
                        break;
                case 'BN':
                        P4291_ = F1192_Process_BN(P4290_, P4291_);
                        break;
                case 'AN':
                        P4291_ = F1179_Process_AN(P4290_, P4291_);
                        break;
                case 'SD':
                        P4291_ = F1184_Process_SD(P4290_, P4291_);
                        break;
                case 'MD':
                        P4291_ = F1185_Process_MD(P4290_, P4291_);
                        break;
                case 'SF':
                        P4291_ = F1188_Process_SF(P4290_, P4291_);
                        break;
                case 'MF':
                        P4291_ = F1189_Process_MF(P4290_, P4291_);
                        break;
                case 'DO':
                        P4291_ = F1193_Process_DO(P4290_, P4291_);
                        break;
                case 'TR':
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
                        P4291_ = F1195_Process_TR(P4290_, P4291_);
                        break;
                case 'WA':
                        P4291_ = F1194_Process_WA(P4290_, P4291_);
                        break;
#endif
                case 'TD':
#ifdef MEDIA762_AU1E
                case 'WA':
#endif
                default:
                        P4291_ = (char*)P4291_ + P4291_->ByteCount + sizeof(ANIMSTEP);
        }
        return P4291_;
}

void main(P4235_, P4236_)
FTL_LIBRARY* P4235_;
FTL_EXECUTABLE* P4236_;
{
        VDEO_FTL_LIBRARY* L4756_;
        GRF1_FTL_LIBRARY* L4757_;
        FTL_LIBRARY* L4758_;
        USIO_FTL_LIBRARY* L4759_;
#ifdef MEDIA692_X31J
        FTL_LIBRARY* L4835_;
#endif
        long L4760_[3];


        L4756_ = NULL;
        L4757_ = NULL;
        L4758_ = NULL;
        L4759_ = NULL;
#ifdef MEDIA692_X31J
        L4835_ = NULL;
#endif
        if (setjmp(G2023_jmp_buf) == 0) {
                if (F9008_OpenPRIM(P4235_, 2) == NULL) {
                        F1198_();
                }
                if ((L4756_ = F9004_OpenVDEO(2)) == NULL) {
                        F1198_();
                }
                if ((L4757_ = F9000_OpenGRF1(2)) == NULL) {
                        F1198_();
                }
                if ((L4758_ = F9006_OpenMEM1(2)) == NULL) {
                        F1198_();
                }
                if ((L4759_ = F9002_OpenUSIO(2)) == NULL) {
                        F1198_();
                }
#ifdef MEDIA692_X31J
                if ((L4835_ = F9016_OpenMUSC(3)) == NULL) {
                        F1198_();
                }
#endif
                L4760_[0] = (P4236_->Parameter != 0) ? P4236_->Parameter : 'ANIM';
                L4760_[1] = '.DAT';
                L4760_[2] = 0;
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
                F1217_();
#endif
#ifdef MEDIA692_X31J
                F1024_SetTrap14VectorErrorProcessing();
#endif
                F1199_(L4760_);
#ifdef MEDIA654_A31E_AU2F
                if (P4236_->Parameter == 'ENDA') { /* Infinite loop if end animation was played */
                        for (;;) {
                                Delay(12L);
                        }
                }
#endif
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
                F1226_();
#endif
        }
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        F1218_();
#endif
#ifdef MEDIA692_X31J
        F9017_CloseMUSC(L4835_);
#endif
        F9003_CloseUSIO(L4759_);
        F9007_CloseMEM1(L4758_);
        F9001_CloseGRF1(L4757_);
        F9005_CloseVDEO(L4756_);
        P4236_->Parameter = 'APPB';
}

void F1198_(
)
{
        longjmp(G2023_jmp_buf, -1);
}

BOOLEAN F1199_(P4292_)
char* P4292_;
{
        register ANIMDESC* L4726_;
        register ANIMSTEP* L4727_;


        L4726_ = NULL;
        if (G3282_) {
                return C1_TRUE;
        }
        if ((L4726_ = F1177_(P4292_)) == NULL) {
                return C0_FALSE;
        }
        F1200_();
        L4727_ = (char*)L4726_ + sizeof(ANIMDESC);
        while ((((char*)L4726_ + L4726_->_l_0x000) > L4727_) && !F1201_(L4726_)) {
                L4727_ = F1196_(L4726_, L4727_);
        }
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        while (G3283_VerticalBlankWaitCount);
#endif
#ifdef MEDIA692_X31J
        while (G3283_VerticalBlankWaitCount--) {
                VDEO_13_WaitVerticalBlank();
        }
#endif
        F1178_(L4726_);
        return C1_TRUE;
}

void F1200_(
)
{
        USIODATA L4728_;


        while (USIO_16_ExtractFirstUsioDataFromQueue(&L4728_));
}

int16_t F1201_(P4293_)
ANIMDESC* P4293_;
{
        USIODATA L4729_;
        register int16_t L4730_;


        if (P4293_->_B_0x26E_Breakable == C0_FALSE) {
                return 0;
        }
        L4730_ = USIO_16_ExtractFirstUsioDataFromQueue(&L4729_);
        G3282_ |= ((L4730_ == 1) || (L4730_ == 17)) ? 1 : 0;
        return G3282_;
}

BOOLEAN F1202_(P4294_)
ANIMDESC* P4294_;
{
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        return F1221_AllocMemForSD(P4294_);
#endif
#ifdef MEDIA692_X31J
        return C1_TRUE;
#endif
}

void F1203_(P4295_)
ANIMDESC* P4295_;
{
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        F1222_FreeMemForSD(P4295_);
#endif
#ifdef MEDIA692_X31J
        MUSC_05_StopMIDIMusic();
#endif
}

void F1204_(P4296_, P4297_, P4298_)
ANIMDESC* P4296_;
ANIMSTEP* P4297_;
int16_t P4298_;
{
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
        register int16_t L4731_i_Unreferenced;
#endif


#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        GRF1_05_F0466_EXPAND_GraphicToBitmap(P4297_, G0348_Bitmap_Screen);
        F1227_();
#endif
#ifdef MEDIA692_X31J
        while (G3283_VerticalBlankWaitCount--) {
                VDEO_13_WaitVerticalBlank();
        }
        G3283_VerticalBlankWaitCount = P4298_;
#endif
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
        F1209_();
#endif
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        G3283_VerticalBlankWaitCount = P4298_;
#endif
#ifdef MEDIA692_X31J
        if (G3285_) {
                for (L4731_i_Unreferenced = 0; L4731_i_Unreferenced < 16; L4731_i_Unreferenced++) {
                        if ((G3286_ + 16)[L4731_i_Unreferenced].ColorDef != G3286_[L4731_i_Unreferenced].ColorDef) {
                                break;
                        }
                }
                if (L4731_i_Unreferenced != 16) {
                        VDEO_16_PaletteControl(1);
                        VDEO_14_SetNormalColorPalette(16, G3286_);
                        for (L4731_i_Unreferenced = 0; L4731_i_Unreferenced < 16; L4731_i_Unreferenced++) {
                                (G3286_ + 16)[L4731_i_Unreferenced].ColorDef = G3286_[L4731_i_Unreferenced].ColorDef;
                        }
                } else {
                        G3285_ = C0_FALSE;
                }
        }
        VDEO_05_ExpandGraphicToBitmap(P4297_, 0);
        if (G3285_) {
                VDEO_16_PaletteControl(0);
        }
        G3285_ = C0_FALSE;
#endif
}

void F1205_(P4299_, P4300_, P4301_)
ANIMDESC* P4299_;
ANIMSTEP* P4300_;
int16_t P4301_;
{
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
        register int16_t L4732_i_Unreferenced;
#endif


#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        F1228_((G0348_Bitmap_Screen == G3280_Screen) ? G3281_ : G3280_Screen, G0348_Bitmap_Screen);
        GRF1_05_F0466_EXPAND_GraphicToBitmap(P4300_, G0348_Bitmap_Screen);
        F1227_();
#endif
#ifdef MEDIA692_X31J
        while (G3283_VerticalBlankWaitCount--) {
                VDEO_13_WaitVerticalBlank();
        }
        G3283_VerticalBlankWaitCount = P4301_;
#endif
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
        F1209_();
#endif
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        G3283_VerticalBlankWaitCount = P4301_;
#endif
#ifdef MEDIA692_X31J
        if (G3285_) {
                for (L4732_i_Unreferenced = 0; L4732_i_Unreferenced < 16; L4732_i_Unreferenced++) {
                        if ((G3286_ + 16)[L4732_i_Unreferenced].ColorDef != G3286_[L4732_i_Unreferenced].ColorDef) {
                                break;
                        }
                }
                if (L4732_i_Unreferenced != 16) {
                        VDEO_16_PaletteControl(1);
                        VDEO_14_SetNormalColorPalette(16, G3286_);
                        for (L4732_i_Unreferenced = 0; L4732_i_Unreferenced < 16; L4732_i_Unreferenced++) {
                                (G3286_ + 16)[L4732_i_Unreferenced].ColorDef = G3286_[L4732_i_Unreferenced].ColorDef;
                        }
                } else {
                        G3285_ = C0_FALSE;
                }
        }
        VDEO_05_ExpandGraphicToBitmap(P4300_, 0);
        if (G3285_) {
                VDEO_16_PaletteControl(0);
        }
        G3285_ = C0_FALSE;
#endif
}

void F1206_UpdateColorPalette(P4302_, P4303_i_Index, P4304_i_Red, P4305_i_Green, P4306_i_Blue)
ANIMDESC* P4302_;
int16_t P4303_i_Index;
int16_t P4304_i_Red;
int16_t P4305_i_Green;
int16_t P4306_i_Blue;
{
        if (G3284_) {
                VDEO_15_ApplyNormalColorPalette(G9002_VDEObase->_b_0x54_ColorCount, G9002_VDEObase->_pc_0x56_ColorComponents, 0);
        }
        G3284_ = 0;
        if (P4303_i_Index == -1) {
                return;
        }
        if (P4303_i_Index == -2) {
                G3285_ = 1;
                return;
        }
        if (P4303_i_Index >= 0) {
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
                G3286_[P4303_i_Index].Components.Index = P4303_i_Index;
#endif
                G3286_[P4303_i_Index].Components.Red = P4304_i_Red << 4;
                G3286_[P4303_i_Index].Components.Green = P4305_i_Green << 4;
                G3286_[P4303_i_Index].Components.Blue = P4306_i_Blue << 4;
        }
}

void F1207_(P4307_, P4308_, P4309_LeftVolume, P4310_RightVolume, P4311_i_Period)
ANIMDESC* P4307_;
unsigned int16_t P4308_;
unsigned char P4309_LeftVolume;
unsigned char P4310_RightVolume;
int16_t P4311_i_Period;
{
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        F1223_(P4307_, P4307_->_pc_0x066_SDData[P4308_ - 1], P4309_LeftVolume, P4310_RightVolume, P4311_i_Period);
#endif
#ifdef MEDIA692_X31J
        F0060_SOUND_Play_CPSX(P4307_->_pc_0x066_SDData[P4308_ - 1], 515, 0);
#endif
}

void F1208_(P4312_, P4313_)
ANIMDESC* P4312_;
int16_t P4313_;
{
#ifdef MEDIA651_A31E_A35M_X31J_AU2F
        G3287_ = P4312_;
        G3288_ = P4313_;
        G3289_ = C1_TRUE;
#endif
#ifdef MEDIA762_AU1E
        F1225_(P4312_, P4313_);
#endif
}

#ifdef MEDIA651_A31E_A35M_X31J_AU2F
void F1209_(
)
{
        register ANIMDESC* L4733_;
        register unsigned int16_t L4734_;


        if (G3289_) {
                L4733_ = G3287_;
                L4734_ = G3288_;
#ifdef MEDIA653_A31E_A35M_AU2F
                F1225_(L4733_, L4734_);
#endif
#ifdef MEDIA692_X31J
                L4733_->_pc_0x270 = L4733_->_pc_0x0E8_MDData[L4734_ - 1];
                L4733_->_ui_0x274 = -1;
                L4733_->_ui_0x276 = -1;
                L4733_->_l_0x27A = 0;
                MUSC_05_StopMIDIMusic();
                MUSC_04_PlayMIDIMusic(&L4733_->_pc_0x270, F1367_, 2L);
#endif
                G3289_ = C0_FALSE;
        }
}

void F1210_(P4494_, P4495_)
ANIMDESC* P4494_;
int16_t P4495_;
{
        int16_t L4709_i_Delay;


        L4709_i_Delay = 120;
#ifdef MEDIA692_X31J
        switch (P4495_) {
                case 3:
                        while ((F1367_(0L) & 0xFFFF) != 2);
                        while (L4709_i_Delay--) {
                                VDEO_13_WaitVerticalBlank();
                        }
        }
#endif
}

void F1211_(P4251_)
long P4251_;
{
#ifdef MEDIA692_X31J
        MUSC_07_();
#endif
}
#endif

unsigned char* F1212_(P4243_, P4244_)
long P4243_;
long P4244_;
{
        return MEM1_06_Allocate(P4244_, MASK0x0040_MEMREQ);
}

void F1213_(P4247_, P4248_)
long P4247_;
unsigned char* P4248_;
{
        if (P4248_ != NULL) {
                MEM1_07_Free(P4248_);
        }
}

unsigned char* F1214_(P4245_, P4246_)
long P4245_;
long P4246_;
{
        return MEM1_06_Allocate(P4246_, MASK0x0040_MEMREQ);
}

void F1215_(P4249_, P4250_)
long P4249_;
unsigned char* P4250_;
{
        if (P4250_ != NULL) {
                MEM1_07_Free(P4250_);
        }
}

void F1216_(P4242_)
int16_t P4242_;
{
#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
        F1246_();
#endif
#ifdef MEDIA692_X31J
        MUSC_05_StopMIDIMusic();
#endif
}

#ifdef MEDIA692_X31J
long F1367_(P4496_)
long P4496_;
{
        asm {
                        movem.l d0/a0,-(sp)
                        move.l  P4496_(a6),d0
                        beq.s   @Tloc_10AC
                        lea     @Tdword_10A8,a0
                        move.l  d0,(a0)
                        movem.l (sp)+,d0/a0
                        bra.s   @Tloc_10B4
                @Tdword_10A8:
                        dc.l 0
                @Tloc_10AC:
                        movem.l (sp)+,d0/a0
                        move.l  @Tdword_10A8,d0
                @Tloc_10B4:
        }
}
#endif

#ifdef MEDIA652_A31E_A35M_AU1E_AU2F
void F1217_(
)
{
        F1088_OpenAmigaStuff();
        if (F1138_() == 0) {
                F9073_DisplayError(0x80F1000B);
        }
        F0575_VerticalBlank_Initialize();
        F0908_InitSound();
        F1219_();
}

void F1218_(
)
{
        F1220_();
        F1230_CleanupSound();
        F0576_VerticalBlank_Deinitialize();
        F1089_CloseAmigaStuff();
}

void F1219_(
)
{

        G3280_Screen = G0348_Bitmap_Screen;
        G3281_ = AllocMem(32000L, MEMF_CHIP | MEMF_CLEAR);
        if (G3281_ == NULL) {
                F1198_();
        }
        G0348_Bitmap_Screen = G3281_;
}

void F1220_(
)
{
        if (G3280_Screen) {
                G0348_Bitmap_Screen = G3280_Screen;
        }
        if (G3281_) {
                FreeMem(G3281_, 32000L);
        }
        G3281_ = NULL;
        G3280_Screen = NULL;
}

BOOLEAN F1221_AllocMemForSD(P4252_)
register ANIMDESC* P4252_;
{
        register ANIMSTEP* L4710_;
        register long L4711_;


        L4711_ = 0;
        P4252_->_pc_0x270 = NULL;
        L4710_ = (char*)P4252_ + sizeof(ANIMDESC);
        while ((char*)P4252_ + P4252_->_l_0x000 > L4710_) {
                if (L4710_->Type == 'SD') {
                        L4711_ = (L4710_->ByteCount > L4711_) ? L4710_->ByteCount : L4711_;
                }
                L4710_ = (char*)L4710_ + L4710_->ByteCount + sizeof(ANIMSTEP);
        }
        if (L4711_) {
                P4252_->_ui_0x274 = L4711_;
                if ((P4252_->_pc_0x270 = MEM1_06_Allocate(L4711_, MASK0x0040_MEMREQ)) == NULL) {
                        return C0_FALSE;
                }
        }
        return C1_TRUE;
}

void F1222_FreeMemForSD(P4253_)
register ANIMDESC* P4253_;
{
        if (P4253_ != NULL) {
                if (P4253_->_pc_0x270) {
                        MEM1_07_Free(P4253_->_pc_0x270);
                        P4253_->_pc_0x270 = NULL;
                }
        }
        F1253_FadeOutSound(30, M737_ANIM_SOUND_PERIOD);
        F1250_();
}

int16_t F1223_(ANIMDESC* P4314_, unsigned int16_t* P4315_pui_SoundData, unsigned char P4316_LeftVolume, unsigned char P4317_RightVolume, int16_t P4318_i_Period)
{
        P4316_LeftVolume = P4316_LeftVolume + 1 >> 2;
        P4317_RightVolume = P4317_RightVolume + 1 >> 2;
        if ((unsigned int16_t)TypeOfMem(P4315_pui_SoundData) & MEMF_CHIP) { /* If sound data is stored in chip memory */
                return F1248_(P4315_pui_SoundData + 1, (long)*P4315_pui_SoundData, 650, 1, P4316_LeftVolume, P4317_RightVolume, 127, 0);
        }
        if (P4314_->_pc_0x270) { /* If sound data is not stored in chip memory, copy it to chip memory before playing */
                if (*P4315_pui_SoundData <= P4314_->_ui_0x274) {
                        F1245_();
                        MEM1_04_CopyBytes(P4315_pui_SoundData + 1, P4314_->_pc_0x270, MASK0x0400_MEMREQ, MASK0x0040_MEMREQ, (long)*P4315_pui_SoundData);
                        return F1248_(P4314_->_pc_0x270, (long)*P4315_pui_SoundData, 650, 1, P4316_LeftVolume, P4317_RightVolume, 127, 0);
                }
        }
}

char* F1224_(P4319_, P4320_)
unsigned int16_t* P4319_;
register int16_t P4320_;
{
        register unsigned int16_t* L4735_;
        register long L4736_;


        L4736_ = 0;
        L4735_ = P4319_ + P4319_[0] + 1;
        while (P4320_--) {
                L4736_ += *L4735_++;
        }
        return (char*)(P4319_ + P4319_[0] + P4319_[0]) + L4736_ + 2;
}

void F1225_(P4321_, P4322_)
ANIMDESC* P4321_;
unsigned int16_t P4322_;
{
        register unsigned int16_t* L4737_;
        register unsigned char* L4738_;
        register int16_t L4739_;
        register unsigned int16_t L4740_;
        register int16_t L4741_i_Unreferenced;
        unsigned int16_t* L4742_;


        F1250_();
        if (!(L4742_ = P4321_->_pc_0x0E8_MDData[P4322_ - 1])) {
                return;
        }
        if (*L4742_ & 0x8000) {
                return;
        }
        if (L4742_[1] != (L4742_ + L4742_[0])[1]) {
                return;
        }
        L4738_ = F1224_(L4742_, 0);
        L4739_ = 0;
        while (*L4738_++ != 255) {
                L4739_++;
        }
        if (L4739_ == 0) {
                return;
        }
        if (!F1249_(L4739_ * 6)) {
                return;
        }
        L4738_ = F1224_(L4742_, 0);
        for (L4740_ = 0; L4740_ < L4739_ * 6; L4740_++) {
                L4737_ = F1224_(L4742_, L4738_[L4740_ % L4739_]);
                F1251_(L4740_, L4737_ + 1, L4737_[0], M737_ANIM_SOUND_PERIOD, 30, 30);
        }
        F1252_();
}

void F1226_(
)
{
        register int16_t L4743_;


        if (G0348_Bitmap_Screen == G3280_Screen) {
                F1228_(G3281_, G3280_Screen);
        }
        for (L4743_ = 0; L4743_ < 4L; L4743_++) {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->BitMap.Planes[L4743_] = G3280_Screen + ((long)L4743_ * 8000);
        }
        MakeScreen(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
        RethinkDisplay();
}

void F1227_(
)
{
        register int16_t* L4744_;
        register int16_t L4745_;


        for (L4745_ = 0; L4745_ < 4L; L4745_++) {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->BitMap.Planes[L4745_] = G0348_Bitmap_Screen + ((long)L4745_ * 8000);
        }
        G0348_Bitmap_Screen = (G0348_Bitmap_Screen == G3280_Screen) ? G3281_ : G3280_Screen;
        if (G3285_) {
                L4744_ = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.ColorMap->ColorTable;
                for (L4745_ = 0; L4745_ < 16; L4745_++) {
                        *L4744_++ = (G3286_[L4745_].Components.Red << 4) | G3286_[L4745_].Components.Green | (G3286_[L4745_].Components.Blue >> 4);
                }
        }
        G3285_ = 0;
        MakeScreen(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
        while (G3283_VerticalBlankWaitCount);
        RethinkDisplay();
}

void F1228_(P4323_, P4324_)
unsigned char* P4323_;
unsigned char* P4324_;
{
        OwnBlitter();
        WaitBlit();
        asm {
                lea     (0xDFF000).l,a0
                move.l  #0,0x64(a0)
                move.l  #0xFFFFFFFF,0x44(a0)
                move.l  #0x9F00000,0x40(a0)
                move.l  P4324_(a6),0x54(a0)
                move.l  P4323_(a6),0x50(a0)
                move.w  #0x6428,0x58(a0)
        }
        DisownBlitter();
        WaitBlit();
}
#endif
#endif
#ifdef MEDIA709_I34E_I34M_P31J
#ifdef MEDIA667_P31J
typedef struct {
        long ItemOffset;
        int16_t LoopCount;
} FOR_LOOP;
#endif

#ifdef MEDIA707_I34E_I34M
unsigned char huge* G8195_;
unsigned int16_t* G8196_;
#endif
VIDEO_DRIVER* G2156_VideoDriver;
char G8198_[4];
IO_DRIVER* G2161_IODriver;
FOR_LOOP G8200_[16]; /* Support for up to 16 nested for loops */
unsigned char huge* G8201_[32];
IO_DRIVER* G2162_IODriver;
#ifdef MEDIA707_I34E_I34M
unsigned int16_t G8203_;
#endif
long G8204_;
#ifdef MEDIA707_I34E_I34M
int16_t G8205_;
long* G8207_;
#endif
int16_t G8208_;
#ifdef MEDIA707_I34E_I34M
char* G8209_;
#endif
#ifdef MEDIA667_P31J
unsigned char* G8211_MIDIFileNames[32];
#endif


#ifdef MEDIA707_I34E_I34M
#include "FILE.C"
#endif

void F8282_(
)
{
        G2156_VideoDriver = (VIDEO_DRIVER*)getvect(C255_DM_VIDEO_INTERRUPT);
}

#include "ANIMIMG.C"

void F8289_(int16_t* P2456_pi_Box, int16_t P2457_i_Color, int16_t P2458_i_Width)
{
        (*(G2156_VideoDriver->VIDRV_01_FillBox))(NULL, P2456_pi_Box, P2457_i_Color, P2458_i_Width);
}

void F8290_ClearScreen(
)
{
        int16_t L2113_ai_Box[4] = { 0, 319, 0, 199 };


        F8289_(&L2113_ai_Box, C00_COLOR_BLACK, 320);
}

void F8291_(char* P2459_Bitmap, int16_t* P2460_pi_Box, int16_t P2461_i_X, int16_t P2462_i_Y, int16_t P2463_i_SourceWidth, int16_t P2464_i_DestinationWidth, int16_t P2465_i_TransparentColor, int16_t P2466_i_Flip)
{
        (*(G2156_VideoDriver->VIDRV_02_Blit))(P2459_Bitmap, NULL, P2460_pi_Box, P2461_i_X, P2462_i_Y, P2463_i_SourceWidth, P2464_i_DestinationWidth, P2465_i_TransparentColor, P2466_i_Flip);
}

void F8292_(int16_t P2467_i_, int16_t P2468_i_, int16_t P2469_i_, int16_t P2470_i_)
{
        (*(G2156_VideoDriver->VIDRV_16_SetSingleColorComponentsInPalette))(P2467_i_, P2468_i_, P2469_i_, P2470_i_);
}

void F8293_(int16_t P2471_i_, int16_t P2472_i_)
{
        (*(G2156_VideoDriver->VIDRV_17_SetSingleColorInPalette1))(P2471_i_, P2472_i_);
}

#ifdef MEDIA707_I34E_I34M
void F8294_SetSingleColorInPalette(int16_t P2473_i_, int16_t P2474_i_)
{
        (*(G2156_VideoDriver->VIDRV_18_SetSingleColorInPalette))(P2473_i_, P2474_i_);
}
#endif

void F8295_(int16_t P2475_i_)
{
        (*(G2156_VideoDriver->VIDRV_08_SetMultipleColorsInPalette))(P2475_i_);
}

void F8296_PALETTE_SetCurtain(unsigned char P2476_uc_)
{
        (*(G2156_VideoDriver->VIDRV_11_PALETTE_SetCurtain))(P2476_uc_);
}

#ifdef MEDIA707_I34E_I34M
void F8297_(
)
{
        (*(G2156_VideoDriver->VIDRV_19_BlackenAllPixelsOneByOne))();
}
#endif

void F8298_(
)
{
#ifdef MEDIA667_P31J
        int16_t L2114_i_;


        L2114_i_ = 0;
#endif
        asm     push    ds
        asm     mov     ax, cs:V0713000_DSRegisterBackup
        asm     mov     ds, ax
        G8204_ -= G8208_;
        asm     pop     ds
}

void F0713_InitIOInterrupt(
)
{
#ifdef MEDIA667_P31J
        G2162_IODriver = G2161_IODriver = (IO_DRIVER*)getvect(C254_DM_IO_INTERRUPT);
#endif
#ifdef MEDIA707_I34E_I34M
        G2161_IODriver = (IO_DRIVER*)getvect(C254_DM_IO_INTERRUPT);
        G2162_IODriver = G2161_IODriver;
#endif
        asm     mov     ax, ds
        asm     mov     cs:V0713000_DSRegisterBackup, ax
        asm     jmp     loc_11304
        asm     V0713000_DSRegisterBackup      dw 0
        loc_11304:
        G8208_ = (*(G2162_IODriver->IODRV_14_SetCustomVerticalBlankRoutine))(F8298_);
}

int16_t F0540_INPUT_Crawcin(
)
{
        int16_t L2115_i_;


        L2115_i_ = (*(G2162_IODriver->IODRV_00_GetKeyboardInput))();
        return L2115_i_;
}

BOOLEAN F0539_INPUT_Cconis(
)
{
        return (*(G2162_IODriver->IODRV_01_IsKeyboardInputPresent))();
}

void F8302_(unsigned char huge* P2477_puc_, int16_t P2478_i_, int16_t P2479_i_)
{
        int16_t L2116_i_;


        L2116_i_ = (*P2477_puc_ << 8) | *(P2477_puc_ + 1);
        if (L2116_i_) {
                (*(G2161_IODriver->IODRV_15_))(P2477_puc_ + 2, L2116_i_, P2478_i_, P2479_i_);
        }
}

void F8303_WaitSoundPlayComplete(
)
{
        (*(G2161_IODriver->IODRV_16_WaitSoundPlayComplete))();
}

#ifdef MEDIA667_P31J
void F8271_PlayMIDIMusic(unsigned char* P2480_puc_MIDIData)
{
        (*(G2161_IODriver->IODRV_27_))(0, P2480_puc_MIDIData);
}

void F8272_StopMIDIMusic(
)
{
        (*(G2161_IODriver->IODRV_27_))(1);
}

void F8273_TRansition_MIDIMusic(
)
{
        (*(G2161_IODriver->IODRV_27_))(2);
}

BOOLEAN F8275_IsMIDIMusicPlaying(
)
{
        return (*(G2162_IODriver->IODRV_27_))(3);
}
#endif

#ifdef MEDIA707_I34E_I34M
BOOLEAN F8304_(char* P2481_pc_Filename)
{
        long L2117_l_;
        int16_t L2118_i_;


        if (F0780_FILE_IsHandleInvalid(G8205_ = F0770_FILE_Open(P2481_pc_Filename))) {
                return C0_FALSE;
        }
        if (!F0772_FILE_Read(G8205_, 2L, (unsigned char huge*)&G8203_)) {
                return C0_FALSE;
        }
        if (G8203_ != 0x8001) {
                return C0_FALSE;
        }
        if (!F0772_FILE_Read(G8205_, 2L, (unsigned char huge*)&G8203_)) {
                return C0_FALSE;
        }
        if ((G8196_ = (unsigned int16_t*)farmalloc((long)(G8203_ << 1))) == NULL) {
                return C0_FALSE;
        }
        if (!F0772_FILE_Read(G8205_, (long)(G8203_ << 1), (unsigned char huge*)G8196_)) {
                return C0_FALSE;
        }
        if (!(G8195_ = (unsigned char huge*)farmalloc((long)(G8203_ << 1)))) {
                return C0_FALSE;
        }
        if (!F0772_FILE_Read(G8205_, (long)(G8203_ << 1), G8195_)) {
                return C0_FALSE;
        }
        if ((G8209_ = (char*)farmalloc((long)(G8203_ << 2))) == NULL) {
                return C0_FALSE;
        }
        if (!F0772_FILE_Read(G8205_, (long)(G8203_ << 2), G8209_)) {
                return C0_FALSE;
        }
        if ((G8207_ = (long*)farmalloc((long)(G8203_ << 2))) == NULL) {
                return C0_FALSE;
        }
        L2117_l_ = (G8203_ << 2) + (G8203_ << 2) + 4;
        for (L2118_i_ = 0; L2118_i_ < G8203_; L2118_i_++) {
                G8207_[L2118_i_] = L2117_l_;
                L2117_l_ += G8196_[L2118_i_];
        }
        return C1_TRUE;
}

#endif
char G8212_[64] = { 255, 0 };
#ifdef MEDIA707_I34E_I34M
BOOLEAN G8213_ = C0_FALSE;
BOOLEAN G8214_ = C0_FALSE;

void F8305_(
)
{
        if (G8213_ && !G8214_) {
                farfree(G8209_);
                farfree(G8195_);
                farfree(G8196_);
                farfree(G8207_);
                F0771_FILE_Close(G8205_);
        }
}

char huge* F8306_(int16_t P2482_i_)
{
        char huge* L2119_pc_;


        if (G8214_) {
                return NULL;
        }
        if (G8213_ == 0) {
#ifdef MEDIA701_I34E
                if (!F8304_("data\\graphics.dat")) {
#endif
#ifdef MEDIA733_I34M
                if (!F8304_("eudata\\graphics.dat")) {
#endif
                        G8214_ = 1;
                        return NULL;
                }
                G8213_ = 1;
        }
        if ((L2119_pc_ = (char*)farmalloc((long)(G8196_[P2482_i_]))) == NULL) {
                return NULL;
        }
        F0774_FILE_Seek(G8205_, G8207_[P2482_i_]);
        if (F0772_FILE_Read(G8205_, (long)G8196_[P2482_i_], L2119_pc_)) {
                return L2119_pc_;
        }
        farfree(L2119_pc_);
        return NULL;
}
#endif

void main(int16_t argc, char* argv[])
{
        unsigned char huge* L2121_puc_AnimData;
        char huge* L2122_pc_;
        unsigned char huge* L2123_puc_;
        long L2124_l_AnimationFileSize;
#ifdef MEDIA667_P31J
        long L2125_l_;
#endif
        unsigned long L2126_ul_Offset;
        unsigned long L2127_ul_Size;
        long L2128_l_;
        long L2129_l_;
        unsigned int16_t L2130_ui_;
        unsigned int16_t L2131_ui_Attributes;
        int16_t L2132_i_;
        int16_t L2133_i_;
        int16_t L2134_i_;
        int16_t L2135_i_;
        int16_t L2136_i_;
        int16_t L2137_i_;
        int16_t L2138_i_;
#ifdef MEDIA667_P31J
        FILE* L2139_FileHandle;
#endif
#ifdef MEDIA707_I34E_I34M
        int16_t L2139_FileHandle;
#endif
        BOOLEAN L2140_B_LoopAnimation;
        BOOLEAN L2141_B_;
        BOOLEAN L2142_B_;
        char L2143_ac_[40]; /* Animation file name */
        unsigned char* L2144_puc_;
        int16_t L2145_i_;
        int16_t L2146_i_;
#ifdef MEDIA707_I34E_I34M
        unsigned int16_t L2147_ui_;
#endif
        int16_t L2148_i_;
        int16_t L2149_i_pc98;
        BOOLEAN L2150_B_BreakAllowed;
#ifdef MEDIA707_I34E_I34M
        BOOLEAN L2151_B_;
        BOOLEAN L2152_B_;
        BOOLEAN L2153_B_;
#endif
        int16_t L2154_i_;


        L2132_i_ = 0;
        L2134_i_ = 0;
        L2140_B_LoopAnimation = C0_FALSE;
        L2141_B_ = C0_FALSE;
        L2142_B_ = C0_FALSE;
        L2148_i_ = 0;
#ifdef MEDIA667_P31J
        L2149_i_pc98 = 0;
#endif
        L2150_B_BreakAllowed = C0_FALSE;
#ifdef MEDIA707_I34E_I34M
        L2151_B_ = C0_FALSE;
        L2152_B_ = C0_FALSE;
        L2153_B_ = C0_FALSE;
#endif
        L2143_ac_[0] = '\0';
        for (L2154_i_ = 1; L2154_i_ < argc; L2154_i_++) {
#ifdef MEDIA667_P31J
                if (argv[L2154_i_][0] == '-') {
                        switch (toupper(argv[L2154_i_][1])) {
#endif
#ifdef MEDIA707_I34E_I34M
                if ((argv[L2154_i_][0] == '-') && (toupper(argv[L2154_i_][1]) == 'A')) {
                        switch (toupper(argv[L2154_i_][2])) {
#endif
                                case 'B':
                                        L2140_B_LoopAnimation = C1_TRUE; /* If true then animation restarts from the beginning after being played */
                                        break;
                                case 'D': /* If true then animation cannot be interrupted by pressing a key else it may be interrupted if the animation file itself allows it (see BR) */
                                        L2141_B_ = C1_TRUE;
#ifdef MEDIA707_I34E_I34M
                                        break;
                                case 'H': /* Hold. If true then palette is not changed at the end of animation else palette is set to full black */
                                        L2151_B_ = C1_TRUE;
                                        break;
                                case 'E': /* End. If true then pixels are set to black one after the other after playing the animation */
                                        L2152_B_ = C1_TRUE;
                                        break;
                                case 'S': /* Start. If true then pixels are set to black one after the other before playing the animation */
                                        L2153_B_ = C1_TRUE;
#endif
                        }
                } else {
                        if (!L2142_B_) {
                                strcpy(L2143_ac_, argv[L2154_i_]);
                                L2142_B_ = C1_TRUE;
                        }
                }
        }
        F8282_();
        F0713_InitIOInterrupt();
#ifdef MEDIA667_P31J
        F8272_StopMIDIMusic();
#endif
        while (F0539_INPUT_Cconis()) {
                F0540_INPUT_Crawcin();
        }
#ifdef MEDIA667_P31J
        if ((L2143_ac_[0] == '\0') || ((L2139_FileHandle = fopen(L2143_ac_, "rb")) == 0)) {
                if ((L2139_FileHandle = fopen("anim.dat", "rb")) == 0) {
#endif
#ifdef MEDIA707_I34E_I34M
        if ((L2143_ac_[0] == '\0') || (F0780_FILE_IsHandleInvalid(L2139_FileHandle = F0770_FILE_Open(L2143_ac_)))) {
                if (F0780_FILE_IsHandleInvalid(L2139_FileHandle = F0770_FILE_Open("anim.dat")) == 0) {
#endif
#ifdef MEDIA667_P31J
                        exit(0);
#endif
#ifdef MEDIA707_I34E_I34M
                        _exit(0);
#endif
                }
        }
#ifdef MEDIA667_P31J
        fseek(L2139_FileHandle, 0L, 2);
        L2124_l_AnimationFileSize = ftell(L2139_FileHandle);
        fseek(L2139_FileHandle, 0L, 0);
#endif
#ifdef MEDIA707_I34E_I34M
        L2124_l_AnimationFileSize = F0775_FILE_GetSize(L2139_FileHandle);
#endif
        if (coreleft() < L2124_l_AnimationFileSize) {
#ifdef MEDIA667_P31J
                exit(0);
#endif
#ifdef MEDIA707_I34E_I34M
                _exit(0);
#endif
                goto loc_11F12;
        }
        if ((L2121_puc_AnimData = (unsigned char huge*)farmalloc(L2124_l_AnimationFileSize)) == NULL) {
#ifdef MEDIA667_P31J
                exit(0);
#endif
#ifdef MEDIA707_I34E_I34M
                _exit(0);
#endif
                goto loc_11F12;
        }
        if ((L2122_pc_ = (char huge*)farmalloc(32000L)) == NULL) {
#ifdef MEDIA667_P31J
                exit(0);
#endif
#ifdef MEDIA707_I34E_I34M
                _exit(0);
#endif
                goto loc_11F12;
        }
#ifdef MEDIA667_P31J
        L2125_l_ = 0;
        do {
                L2130_ui_ = fread(L2121_puc_AnimData + L2125_l_, 1, 32768, L2139_FileHandle);
                L2125_l_ += L2130_ui_;
        } while (L2130_ui_ == 32768);
        fclose(L2139_FileHandle);
#endif
#ifdef MEDIA707_I34E_I34M
        F0772_FILE_Read(L2139_FileHandle, L2124_l_AnimationFileSize, L2121_puc_AnimData);
        F0771_FILE_Close(L2139_FileHandle);
        if (L2153_B_) {
                F8297_(); /* Erase pixels one after the other */
        }
#endif
        F8296_PALETTE_SetCurtain(0);
        F8290_ClearScreen();
#ifdef MEDIA667_P31J
        L2126_ul_Offset = 0;
        L2133_i_ = L2129_l_ = 0;
#endif
#ifdef MEDIA707_I34E_I34M
        L2129_l_ = L2126_ul_Offset = 0;
        L2133_i_ = 0;
#endif
        do {
                L2123_puc_ = L2121_puc_AnimData + L2126_ul_Offset;
                L2131_ui_Attributes = *((L2121_puc_AnimData + (L2126_ul_Offset + 4)) + 1) | (*(L2126_ul_Offset + 4 + L2121_puc_AnimData) << 8);
                L2127_ul_Size = L2126_ul_Offset + 6 + ((*(L2126_ul_Offset + 2 + L2121_puc_AnimData) << 8) | *(L2126_ul_Offset + 3 + L2121_puc_AnimData));
                switch (*(L2121_puc_AnimData + L2126_ul_Offset) | (*(L2126_ul_Offset + 1 + L2121_puc_AnimData) << 8)) {
#ifdef MEDIA667_P31J
                        case 0x5254: /* TR */
                                F8273_TRansition_MIDIMusic();
                                break;
#endif
                        case 0x5242: /* BR */
                                L2150_B_BreakAllowed = L2131_ui_Attributes;
                                break;
                        case 0x4157: /* WA */
                                switch (L2131_ui_Attributes) {
                                        case 1: /* Wait end of sound */
                                                F8303_WaitSoundPlayComplete();
#ifdef MEDIA667_P31J
                                                break;
                                        case 3: /* Wait end of MIDI music */
                                                while (F8275_IsMIDIMusicPlaying());
#endif
                                }
                                break;
                        case 0x4453: /* SD */
#ifdef MEDIA707_I34E_I34M
                                L2144_puc_ =
#endif
                                G8201_[L2148_i_++] = L2126_ul_Offset + 6 + L2121_puc_AnimData;
#ifdef MEDIA707_I34E_I34M
                                L2147_ui_ = (*L2144_puc_ << 8) | *(L2144_puc_ + 1);
                                for (L2154_i_ = 0; L2154_i_ < L2147_ui_; L2154_i_++) {
                                        L2144_puc_[L2154_i_ + 2] += 0x80;
                                }
                                break;
                        case 0x4454: /* TD */
                                G8201_[L2148_i_++] = F8306_(L2131_ui_Attributes);
#endif
                                break;
                        case 0x4F53: /* SO */
                                L2144_puc_ = G8201_[L2131_ui_Attributes - 1];
                                L2146_i_ = 3;
                                L2145_i_ = 5000;
                                F8302_(L2144_puc_, L2146_i_, L2145_i_);
                                break;
#ifdef MEDIA667_P31J
                        case 0x444D:   /* MD */
                                G8211_MIDIFileNames[L2149_i_pc98++] = L2126_ul_Offset + 6 + L2121_puc_AnimData;
                                break;
                        case 0x494D:   /* MI */
                                F8271_PlayMIDIMusic(G8211_MIDIFileNames[L2131_ui_Attributes - 1]);
                                break;
#endif
                        case 0x4C44: /* DL */
                        case 0x4E45: /* EN */
                                G8204_ = L2131_ui_Attributes;
#ifdef MEDIA667_P31J
                                if (G8204_ < 5) {
                                        G8204_ = 5;
                                }
#endif
                                if ((*(L2126_ul_Offset + 6 + L2121_puc_AnimData) == 0xFF) && (*(L2126_ul_Offset + 7 + L2121_puc_AnimData) == 0x81)) { /* Signature of DL */
                                        F8288_(L2126_ul_Offset + 8 + L2121_puc_AnimData, L2122_pc_);
                                } else {
                                        F8288_(L2126_ul_Offset + 6 + L2121_puc_AnimData, L2122_pc_);
                                }
                                F8296_PALETTE_SetCurtain(1);
                                L2132_i_++;
                                while ((G8204_ > 0) && !L2141_B_);
                                if (L2134_i_) {
                                        L2134_i_ = 0;
                                        L2127_ul_Size = L2128_l_;
                                }
                                break;
                        case 0x3850: /* P8 */
                                F8296_PALETTE_SetCurtain(0);
                                for (L2154_i_ = 0; L2154_i_ < 16; L2154_i_++) {
#ifdef MEDIA707_I34E_I34M
                                        F8294_SetSingleColorInPalette(L2154_i_, *(L2126_ul_Offset + L2154_i_ + 6 + L2121_puc_AnimData));
#endif
#ifdef MEDIA667_P31J
                                        F8293_(L2154_i_, *(L2126_ul_Offset + L2154_i_ + 6 + L2121_puc_AnimData));
#endif
                                }
#ifdef MEDIA667_P31J
                                F8293_(-1, 0);
#endif
                                break;
                        case 0x4C50: /* PL */
                                L2130_ui_ = 0;
                                for (L2154_i_ = 0; L2154_i_ < 64; L2154_i_++) {
                                        if (*(L2126_ul_Offset + L2154_i_ + 8 + L2121_puc_AnimData) != G8212_[L2154_i_]) {
                                                L2130_ui_ = 1;
                                        }
                                        G8212_[L2154_i_] = *(L2126_ul_Offset + L2154_i_ + 8 + L2121_puc_AnimData);
                                }
                                if (L2130_ui_) {
                                        F8296_PALETTE_SetCurtain(0);
                                        for (L2154_i_ = 0; L2154_i_ < 64; L2154_i_ += 4) {
                                                L2135_i_ = *(L2126_ul_Offset + L2154_i_ + 8 + L2121_puc_AnimData);
                                                L2136_i_ = *(L2126_ul_Offset + L2154_i_ + 9 + L2121_puc_AnimData);
                                                L2137_i_ = *(L2126_ul_Offset + L2154_i_ + 10 + L2121_puc_AnimData);
                                                L2138_i_ = *(L2126_ul_Offset + L2154_i_ + 11 + L2121_puc_AnimData);
                                                F8292_(L2135_i_, L2136_i_, L2137_i_, L2138_i_);
                                        }
                                }
                                break;
                        case 0x4F46: /* FO */
                                G8200_[L2133_i_].LoopCount = L2131_ui_Attributes;
                                G8200_[L2133_i_++].ItemOffset = L2127_ul_Size + ((*(L2127_ul_Size + 2 + L2121_puc_AnimData) << 8) | *(L2127_ul_Size + 3 + L2121_puc_AnimData)) + 6;
                                break;
                        case 0x454E: /* NE */
                                L2130_ui_ = --G8200_[L2133_i_ - 1].LoopCount;
                                if ((L2130_ui_ > 0) && (L2130_ui_ < 10)) {
                                        L2134_i_ = 1;
                                        L2128_l_ = G8200_[L2133_i_ - 1].ItemOffset;
                                } else {
                                        L2133_i_--;
                                        L2127_ul_Size = L2127_ul_Size + ((*(L2127_ul_Size + 2 + L2121_puc_AnimData) << 8) | *(L2127_ul_Size + 3 + L2121_puc_AnimData)) + 6;
                                }
                }
                L2126_ul_Offset = L2127_ul_Size;
                if (L2140_B_LoopAnimation && (L2126_ul_Offset >= L2124_l_AnimationFileSize)) {
                        L2126_ul_Offset = L2129_l_;
                }
                if (F0539_INPUT_Cconis() && !L2141_B_ && L2150_B_BreakAllowed) {
                        F0540_INPUT_Crawcin();
#ifdef MEDIA667_P31J
                        F8272_StopMIDIMusic();
#endif
                        break;
                }
        } while (L2126_ul_Offset < L2124_l_AnimationFileSize);
#ifdef MEDIA667_P31J
        while (F8275_IsMIDIMusicPlaying());
        F8272_StopMIDIMusic();
#endif
#ifdef MEDIA707_I34E_I34M
        F8305_();
        if (L2152_B_) {
                F8297_(); /* Erase pixels one after the other */
        }
        if (!L2151_B_) {
#endif
                F8296_PALETTE_SetCurtain(0); /* Set palette to full black */
#ifdef MEDIA707_I34E_I34M
        }
#endif
        farfree(L2122_pc_);
        farfree(L2121_puc_AnimData);
        loc_11F12:
#ifdef MEDIA667_P31J
        exit(0);
#endif
#ifdef MEDIA707_I34E_I34M
        _exit(0);
#endif
}
#endif
#ifdef MEDIA670_F31E_F31J
extern void F0719_PlayMusicTrack(
int P2403_i_TrackNumber /* ANOMALY_FMT int required */
);

#include "ANIMIMG.C"


#if (EXETYPE == C05_ANIM)
FOR_LOOP G8200_[15];
#else
FOR_LOOP G8200_[16]; /* Support for up to 16 nested for loops */
#endif
unsigned char* G4127_[16];
#if (EXETYPE == C05_ANIM)
int16_t G4146_[18];
#else
int16_t G4146_[20];
#endif


#pragma Off(Optimize_FP); /* ANOMALY_FMT */
void F8292_(
int16_t P2467_i_ColorIndex SEPARATOR
unsigned int16_t    P2468_c_Red        SEPARATOR
unsigned int16_t    P2469_c_Green      SEPARATOR
unsigned int16_t    P2470_c_Blue       FINAL_SEPARATOR
{
        G8183_aac_FullPalette[P2467_i_ColorIndex][0] = P2468_c_Red << 2;
        G8183_aac_FullPalette[P2467_i_ColorIndex][1] = P2469_c_Green << 2;
        G8183_aac_FullPalette[P2467_i_ColorIndex][2] = P2470_c_Blue << 2;
}
#pragma On(Optimize_FP); /* ANOMALY_FMT */

void F2275_(
unsigned char* P6113_puc_AnimData        SEPARATOR
unsigned char* P6114_                    SEPARATOR
long           P6115_l_AnimationFileSize FINAL_SEPARATOR
{
        unsigned long L2127_ul_Size;
        long L2128_l_;
        unsigned long L2129_l_;
        unsigned int16_t L2130_ui_;
        unsigned int16_t L2131_ui_Attributes;
        int16_t L2132_i_;
        int16_t L2133_i_;
        int16_t L2134_i_;
        int16_t L2140_B_LoopAnimation;
        int16_t L2135_i_;
        int16_t L2136_i_;
        int16_t L2137_i_;
        int16_t L2138_i_;
        unsigned char L6565_[64];
        int16_t L6566_;
        unsigned char* L2144_puc_;
        unsigned int16_t L2145_ui_;
        int16_t L2146_i_;
        int16_t L6567_;
        int16_t L2148_i_;
        BOOLEAN L2150_B_BreakAllowed;
        unsigned long L2126_ul_Offset;
        int16_t L2154_i_;


        L2132_i_ = 0;
        L2134_i_ = 0;
        L2140_B_LoopAnimation = 0;
        L6566_ = 0;
        L6567_ = 0;
        L2148_i_ = 0;
        L2150_B_BreakAllowed = 0;
        F2266_(-1);
        L2126_ul_Offset = 0;
        L2129_l_ = 0;
        L2133_i_ = 0;
        do {
                L2127_ul_Size = ((P6113_puc_AnimData[L2126_ul_Offset + 2] << 8) | P6113_puc_AnimData[L2126_ul_Offset + 3]) + L2126_ul_Offset + 6;
                L2131_ui_Attributes = P6113_puc_AnimData[L2126_ul_Offset + 5] | (P6113_puc_AnimData[L2126_ul_Offset + 4] << 8);
                switch (P6113_puc_AnimData[L2126_ul_Offset] | (P6113_puc_AnimData[L2126_ul_Offset + 1] << 8)) {
                        case 0x5242: /* BR */
                                if (L2131_ui_Attributes == 1) {
                                        L2150_B_BreakAllowed = C1_TRUE;
                                }
                                break;
                        case 0x4157: /* WA */
                                switch (L2131_ui_Attributes) {
                                        case 1: /* Wait end of sound */
                                                F0710_WaitSoundPlayComplete();
                                                break;
                                        case 2: /* Wait end of CD music */
                                                while (F2257_IsCDPlaying());
                                        case 3: /* Wait end of MIDI music */
                                        case 4: /* Wait ? */
                                                break;
                                }
                                break;
                        case 0x4454: /* TD */
                                G4146_[L2148_i_++] = L2131_ui_Attributes;
                                break;
                        case 0x5254: /* TR */
                                F0719_PlayMusicTrack(G4146_[L2131_ui_Attributes - 1]);
                                break;
                        case 0x4453: /* SD */
                                G4127_[++L6567_ - 1] = P6113_puc_AnimData + L2126_ul_Offset + 6;
                                break;
                        case 0x4F53: /* SO */
                                L2144_puc_ = G4127_[L2131_ui_Attributes - 1];
                                L2146_i_ = 100;
                                L2145_ui_ = (*L2144_puc_ << 8) | *(L2144_puc_ + 1);
                                F0060_SOUND_Play_CPSX(L2144_puc_ + 2, L2145_ui_, L2146_i_);
                                break;
                        case 0x494D: /* MI */
                                F0814_TRansition_MIDIMusic();
                                break;
                        case 0x4C44: /* DL */
                        case 0x4E45: /* EN */
                                G8204_ = L2131_ui_Attributes;
                                if (G8204_ < 5) {
                                        G8204_ = 5;
                                }
                                if ((P6113_puc_AnimData[L2126_ul_Offset + 6] == 0xFF) && (P6113_puc_AnimData[L2126_ul_Offset + 7] == 0x81)) { /* Signature of DL */
                                        F8288_(P6113_puc_AnimData + L2126_ul_Offset + 8, P6114_);
                                } else {
                                        F8288_(P6113_puc_AnimData + L2126_ul_Offset + 6, P6114_);
                                }
                                F2267_(P6114_, L6566_);
                                L6566_ ^= 1;
                                L2132_i_++;
                                while (G8204_ > 0);
                                if (L2134_i_) {
                                        L2134_i_ = 0;
                                        L2127_ul_Size = L2128_l_;
                                }
                                break;
                        case 0x4C50: /* PL */
                                L2130_ui_ = 0;
                                for (L2154_i_ = 0; L2154_i_ < 64; L2154_i_++) {
                                        if (P6113_puc_AnimData[L2126_ul_Offset + 8 + L2154_i_] != L6565_[L2154_i_]) {
                                                L2130_ui_ = 1;
                                        }
                                        L6565_[L2154_i_] = P6113_puc_AnimData[L2126_ul_Offset + 8 + L2154_i_];
                                }
                                if (L2130_ui_) {
                                        for (L2154_i_ = 0; L2154_i_ < 64; L2154_i_ += 4) {
                                                L2135_i_ = P6113_puc_AnimData[L2126_ul_Offset + 8 + L2154_i_];
                                                L2136_i_ = P6113_puc_AnimData[L2126_ul_Offset + 9 + L2154_i_];
                                                L2137_i_ = P6113_puc_AnimData[L2126_ul_Offset + 10 + L2154_i_];
                                                L2138_i_ = P6113_puc_AnimData[L2126_ul_Offset + 11 + L2154_i_];
                                                F8292_(L2135_i_, L2136_i_, L2137_i_, L2138_i_);
                                        }
                                }
                                break;
                        case 0x4F46: /* FO */
                                G8200_[L2133_i_].LoopCount = L2131_ui_Attributes;
                                G8200_[L2133_i_].ItemOffset = L2127_ul_Size + ((P6113_puc_AnimData[L2127_ul_Size + 2] << 8) | P6113_puc_AnimData[L2127_ul_Size + 3]) + 6;
                                L2133_i_++;
                                break;
                        case 0x454E: /* NE */
                                L2130_ui_ = --G8200_[L2133_i_ - 1].LoopCount;
                                if ((L2130_ui_ > 0) && (L2130_ui_ < 10)) {
                                        L2134_i_ = 1;
                                        L2128_l_ = G8200_[L2133_i_ - 1].ItemOffset;
                                } else {
                                        L2133_i_--;
                                        L2127_ul_Size = L2127_ul_Size + ((P6113_puc_AnimData[L2127_ul_Size + 2] << 8) | P6113_puc_AnimData[L2127_ul_Size + 3 ]) + 6;
                                }
                                break;
                        default:
                        case 0x444D: /* MD */
                                break;
                }
                L2126_ul_Offset = L2127_ul_Size;
                if (L2140_B_LoopAnimation && (L2126_ul_Offset >= P6115_l_AnimationFileSize)) {
                        L2126_ul_Offset = L2129_l_;
                }
                if (F0539_INPUT_Cconis()) {
                        if (L2150_B_BreakAllowed) {
                                F0540_INPUT_Crawcin();
                                break;
                        }
                }
        } while (L2126_ul_Offset < P6115_l_AnimationFileSize);
        F0740_MUSIC_Pause();
}

void F2248_PlayAnimation(
char* P6112_ FINAL_SEPARATOR
{ /* Similar to main in ANIM.C */
        long L2125_l_;
        unsigned int16_t L2130_ui_;
        unsigned char* P6113_puc_AnimData;
        char* L2122_pc_;
        FILE_HANDLE L2139_FileHandle;
        long L2124_l_AnimationFileSize;


        while (F0539_INPUT_Cconis()) {
                F0540_INPUT_Crawcin();
        }
        if ((L2139_FileHandle = fopen(P6112_, "rb")) == NULL) {
                return;
        }
        fseek(L2139_FileHandle, 0, SEEK_END);
        L2124_l_AnimationFileSize = ftell(L2139_FileHandle);
        fseek(L2139_FileHandle, 0, SEEK_SET);
        if ((P6113_puc_AnimData = (unsigned char*)malloc(L2124_l_AnimationFileSize)) == NULL) {
                return;
        }
        if ((L2122_pc_ = (char*)malloc(32000L)) == NULL) {
                return;
        }
        L2125_l_ = 0;
        do {
                L2130_ui_ = fread(P6113_puc_AnimData + L2125_l_, 1, 32768, L2139_FileHandle);
                L2125_l_ += L2130_ui_;
        } while (L2130_ui_ == 32768);
        fclose(L2139_FileHandle);
        F2275_(P6113_puc_AnimData, M774_CAST_PUC(L2122_pc_), L2124_l_AnimationFileSize);
        free(L2122_pc_);
        free(P6113_puc_AnimData);
}
#endif
