#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA772_SU1E
extern unsigned char* F1650_AllocateMemory();
extern void F1525_OpenVDIWorkstation();
extern void F1526_CloseWorkstationAndExitApplication();
extern void F0077_MOUSE_EnableScreenUpdate_CPSE();
extern void F0078_MOUSE_DisableScreenUpdate();
extern void F1530_ShowMousePointerAsArrow();
extern void F1531_MouseHandler();
extern char* F0088_strcpy();
extern void F1651_FreeMemory();
extern void F1653_ConvertCoordinates();

extern int16_t G3404_VDIHandle;
extern unsigned char* G0348_Bitmap_Screen;
extern int16_t G0578_B_UseByteBoxCoordinates;
extern long G1086_VerticalBlankCount;


unsigned int16_t G3401_aui_Palette_Black[16];
unsigned int16_t G3541_ui_WhiteColor_Unreferenced = 0x777;
unsigned int16_t G3406_aui_Palette_SwitchScreen[16] = { 0x000, 0x333, 0x444, 0x310, 0x066, 0x420, 0x040, 0x060, 0x700, 0x750, 0x643, 0x770, 0x222, 0x555, 0x007, 0x777 };
MOUSE2_INPUT G3407_as_MouseInput[23] = {
        {  1, 0, 0, 96, 0, 32, 1 },
        {  2, 0, 0, 96, 0, 32, 1 },
        {  3, 0, 0, 96, 0, 32, 1 },
        {  4, 0, 0, 96, 0, 32, 1 },
        {  5, 0, 0, 96, 0, 32, 1 },
        {  6, 0, 0, 96, 0, 32, 1 },
        {  7, 0, 0, 96, 0, 32, 1 },
        {  8, 0, 0, 96, 0, 32, 1 },
        {  9, 0, 0, 96, 0, 32, 1 },
        { 10, 0, 0, 96, 0, 32, 1 },
        { 11, 0, 0, 96, 0, 32, 1 },
        { 12, 0, 0, 96, 0, 32, 1 },
        { 13, 0, 0, 96, 0, 32, 1 },
        { 14, 0, 0, 96, 0, 32, 1 },
        { 15, 0, 0, 96, 0, 32, 1 },
        { 16, 0, 0, 96, 0, 32, 1 },
        { 17, 0, 0, 96, 0, 32, 1 },
        { 18, 0, 0, 96, 0, 32, 1 },
        { 19, 0, 0, 96, 0, 32, 1 },
        { 20, 0, 0, 96, 0, 32, 1 },
        { 21, 0, 0, 96, 0, 32, 1 },
        { 22, 0, 0, 96, 0, 32, 1 },
        {  0, 0, 0,  0, 0,  0, 0 } };
INPUT_INFO G3408_s_InputInfo = { 0, 0, G3407_as_MouseInput, NULL, NULL};
SWITCH_OPTION G3400_as_SwitchOptions[SWITCH_OPTION_COUNT];


long F1501_GetSwitchOptionBitmapByteCount(SWITCH_OPTION* P5094_ps_SwitchOption) {
        return (long)P5094_ps_SwitchOption->Width * (long)P5094_ps_SwitchOption->Height * (long)P5094_ps_SwitchOption->ColorDepth;
}

void F0924_SetCriticalErrorHandler(
)
{
        int16_t L5003_i_FloppyDriveCount;
        long L5002_l_SupervisorStack;


        L5002_l_SupervisorStack = Super(0L);
        L5003_i_FloppyDriveCount = *((int16_t*)0x4A6); /* _nflops (Number of floppy drives) */
        asm {
                        lea     @Tloc_31A,a0
                        move.l  a0,(0x404).w    /* etv_critic : Critical Error Handoff Vector (GEMDOS) */
                        bra.s   @Tloc_31E
                @Tloc_31A:
                        moveq   #0xFFFFFFFF,d0
                        rts
                @Tloc_31E:
        }
        Super(L5002_l_SupervisorStack);
}

int16_t F1503_ReadFTLHeaders(char* P5078_pc_FileName, unsigned char** P5079_ppuc_HeaderBuffer, unsigned char** P5080_ppuc_SegmentHeadersBuffer, int16_t P5081_i_Requirements)
{
        int16_t L5179_i_Result;
        long L5180_l_ByteCount;
        unsigned char* L5181_puc_HeaderBuffer;
        unsigned char* L5182_puc_SegmentHeadersBuffer;
        HEADER* L5183_ps_Header;
        SEGMENTHEADER* L5184_ps_SegmentHeaders;
        int16_t L5185_i_FileHandle;


        L5182_puc_SegmentHeadersBuffer = NULL;
        L5181_puc_HeaderBuffer = NULL;
        L5185_i_FileHandle = Fopen(P5078_pc_FileName, C0_READ_ONLY);
        if (L5185_i_FileHandle < 0) {
                L5179_i_Result = -1;
                goto loc_468;
        }
        L5180_l_ByteCount = sizeof(HEADER);
        L5181_puc_HeaderBuffer = F1650_AllocateMemory(L5180_l_ByteCount, P5081_i_Requirements);
        if (!L5181_puc_HeaderBuffer) {
                L5179_i_Result = -5;
                goto loc_468;
        }
        L5183_ps_Header = (HEADER*)L5181_puc_HeaderBuffer;
        if (Fread(L5185_i_FileHandle, L5180_l_ByteCount, L5183_ps_Header) != L5180_l_ByteCount) {
                L5179_i_Result = -2;
                goto loc_468;
        }
        if (L5183_ps_Header->Magic != 0x6160) {
                L5179_i_Result = -3;
                goto loc_468;
        }
        if (L5183_ps_Header->SegmentCount > 100) {
                L5179_i_Result = -4;
                goto loc_468;
        }
        L5180_l_ByteCount = L5183_ps_Header->SegmentCount * sizeof(SEGMENTHEADER);
        L5182_puc_SegmentHeadersBuffer = F1650_AllocateMemory(L5180_l_ByteCount, P5081_i_Requirements);
        if (L5181_puc_HeaderBuffer == NULL) { /* BUGX_XX: Wrong variable tested, should be L5182_puc_SegmentHeadersBuffer */
                L5179_i_Result = -6;
                asm {
                        bra.w  @loc_468                /* Branch size fix. goto generates a bra.s instead of bra.w */
                }
        }
        L5184_ps_SegmentHeaders = (SEGMENTHEADER*)L5182_puc_SegmentHeadersBuffer;
        if (Fread(L5185_i_FileHandle, L5180_l_ByteCount, L5184_ps_SegmentHeaders) != L5180_l_ByteCount) {
                L5179_i_Result = -2;
                goto loc_468;
        }
        if (F0930_GetHeaderChecksum(L5183_ps_Header, L5184_ps_SegmentHeaders) != L5183_ps_Header->Checksum) { /* Compute and compare checksum on header and segment headers */
                L5179_i_Result = -7;
                goto loc_468;
        }
        L5179_i_Result = 0;
        loc_468:
        Fclose(L5185_i_FileHandle);
        if (L5181_puc_HeaderBuffer); /* Useless code */
        if (L5182_puc_SegmentHeadersBuffer); /* Useless code */
        if (L5179_i_Result < 0) {
                if (L5181_puc_HeaderBuffer) {
                        F1651_FreeMemory(L5181_puc_HeaderBuffer);
                }
                if (L5182_puc_SegmentHeadersBuffer) {
                        F1651_FreeMemory(L5182_puc_SegmentHeadersBuffer);
                }
        } else {
                *P5079_ppuc_HeaderBuffer = L5181_puc_HeaderBuffer;
                *P5080_ppuc_SegmentHeadersBuffer = L5182_puc_SegmentHeadersBuffer;
        }
        return L5179_i_Result;
}

int16_t F1504_LoadSegment(char* P5082_pc_FileName, HEADER* P5083_ps_Header, SEGMENTHEADER* P5084_ps_SegmentHeaders, int16_t P5085_i_SegmentType, int16_t P5086_i_SegmentId, unsigned char** P5087_ppuc_SegmentContent, int16_t P5088_i_Requirements) {
        int16_t L5186_;
        int16_t L5187_i_SegmentIndex;
        int16_t L5188_i_FileHandle;
        unsigned char* L5189_;
        int16_t L5190_i_Result;


        L5189_ = NULL;
        L5188_i_FileHandle = Fopen(P5082_pc_FileName, C0_READ_ONLY);
        if (L5188_i_FileHandle < 0) {
                L5190_i_Result = -1;
                goto loc_612;
        }
        if ((P5083_ps_Header == NULL) || (P5084_ps_SegmentHeaders == NULL)) {
                L5190_i_Result = -4;
                goto loc_612;
        }
        L5187_i_SegmentIndex = -1;
        for (L5186_ = 0; L5186_ < P5083_ps_Header->SegmentCount; L5186_++) {
                if (P5084_ps_SegmentHeaders[L5186_].Type == P5085_i_SegmentType) {
                        if ((P5086_i_SegmentId == -1) || (P5086_i_SegmentId == P5084_ps_SegmentHeaders[L5186_].Id)) {
                                L5187_i_SegmentIndex = L5186_;
                                break;
                        }
                }
        }
        if (L5187_i_SegmentIndex < 0) {
                L5190_i_Result = -5;
                goto loc_612;
        }
        L5189_ = F1650_AllocateMemory((unsigned long)P5084_ps_SegmentHeaders[L5187_i_SegmentIndex].Size, P5088_i_Requirements);
        if (L5189_ == NULL) {
                L5190_i_Result = -6;
                goto loc_612;
        }
        if (Fseek((long)P5084_ps_SegmentHeaders[L5187_i_SegmentIndex].Offset, L5188_i_FileHandle, 0) < 0) {
                L5190_i_Result = -2;
                goto loc_612;
        }
        if (Fread(L5188_i_FileHandle, (long)P5084_ps_SegmentHeaders[L5187_i_SegmentIndex].Size, L5189_) != P5084_ps_SegmentHeaders[L5187_i_SegmentIndex].Size) {
                L5190_i_Result = -3;
                goto loc_612;
        }
        L5190_i_Result = 0;
        loc_612:
        Fclose(L5188_i_FileHandle);
        if (L5189_); /* Useless code */
        if (L5190_i_Result < 0) {
                if (L5189_) {
                        F1651_FreeMemory(L5189_);
                }
        } else {
                *P5087_ppuc_SegmentContent = L5189_;
        }
        return L5190_i_Result;
}

unsigned int16_t F0930_GetHeaderChecksum(HEADER* P1025_ps_, SEGMENTHEADER* P1026_ps_)
{
        register unsigned int16_t L1823_ui_;
        register unsigned int16_t L1824_ui_;
        register unsigned int16_t L1825_ui_;
        register unsigned char* L1826_puc_;


        L1824_ui_ = 0;
        L1826_puc_ = (unsigned char*)P1025_ps_;
        for (L1823_ui_ = 4; L1823_ui_ < 20; L1823_ui_++) {
                L1824_ui_ += (L1826_puc_[L1823_ui_] * L1823_ui_);
        }
        L1826_puc_ = (unsigned char*)P1026_ps_;
        L1825_ui_ = P1025_ps_->SegmentCount * sizeof(SEGMENTHEADER);
        for (L1823_ui_ = 0; L1823_ui_ < L1825_ui_; L1823_ui_++) {
                L1824_ui_ += L1826_puc_[L1823_ui_] * ((L1823_ui_ & 0x00FF) + 1);
        }
        return L1824_ui_;
}

void F1506_SetMouseInputFromSwitchOptions(SWITCH_OPTION* P5089_ps_SwitchOptions, int16_t P5090_i_SwitchOptionCount, MOUSE2_INPUT* P5483_ps_MouseInput) {
        int16_t L5191_i_SwitchOptionIndex;
        int16_t L5192_i_MouseInputIndex;
        MOUSE2_INPUT* L5193_as_MouseInput;


        L5193_as_MouseInput = G3407_as_MouseInput;
        L5192_i_MouseInputIndex = 0;
        for (L5191_i_SwitchOptionIndex = 1; L5191_i_SwitchOptionIndex < P5090_i_SwitchOptionCount; L5191_i_SwitchOptionIndex++) {
                if (P5089_ps_SwitchOptions[L5191_i_SwitchOptionIndex].Status < 0) {
                        P5089_ps_SwitchOptions[L5191_i_SwitchOptionIndex].Command = -2;
                } else {
                        F1653_ConvertCoordinates(P5089_ps_SwitchOptions[L5191_i_SwitchOptionIndex].Box, L5193_as_MouseInput[L5192_i_MouseInputIndex].Box);
                        L5193_as_MouseInput[L5192_i_MouseInputIndex].Command = L5192_i_MouseInputIndex + 1;
                        P5089_ps_SwitchOptions[L5191_i_SwitchOptionIndex].Command = L5193_as_MouseInput[L5192_i_MouseInputIndex].Command;
                        if (++L5192_i_MouseInputIndex > 22) {
                                break;
                        }
                }
        }
        L5193_as_MouseInput[L5192_i_MouseInputIndex].Command = 0;
        L5193_as_MouseInput[L5192_i_MouseInputIndex].Flags = 0;
        L5193_as_MouseInput[L5192_i_MouseInputIndex].Button = 0;
        M768_BOX_LEFT(L5193_as_MouseInput[L5192_i_MouseInputIndex].Box) = M769_BOX_RIGHT(L5193_as_MouseInput[L5192_i_MouseInputIndex].Box) = M770_BOX_TOP(L5193_as_MouseInput[L5192_i_MouseInputIndex].Box) = M771_BOX_BOTTOM(L5193_as_MouseInput[L5192_i_MouseInputIndex].Box) = 0;
}

int16_t F1507_DrawSwitchOptionBitmap(SWITCH_OPTION* P5093_ps_SwitchOption) {
        int16_t L5199_i_Unreferenced;
        int16_t L5195_i_Result;
        unsigned char* L5196_puc_TemporaryBuffer;
        unsigned char* L5197_puc_Bitmap;
        int16_t L5198_ai_XYZ[4];


        L5196_puc_TemporaryBuffer = NULL;
        if (P5093_ps_SwitchOption->Status < 0) {
                L5195_i_Result = -1;
                goto loc_8C8;
        }
        if (P5093_ps_SwitchOption->Status == 1) {
                L5196_puc_TemporaryBuffer = F1650_AllocateMemory(F1501_GetSwitchOptionBitmapByteCount(P5093_ps_SwitchOption), MASK0x0010_MEMREQ);
                if (L5196_puc_TemporaryBuffer == NULL) {
                        L5195_i_Result = -3;
                        goto loc_8C8;
                }
                L5197_puc_Bitmap = L5196_puc_TemporaryBuffer;
                F0466_EXPAND_GraphicToBitmap((unsigned char*)P5093_ps_SwitchOption->Graphic, L5197_puc_Bitmap, 0, 0);
        } else {
                if (P5093_ps_SwitchOption->Status == 2) {
                        L5196_puc_TemporaryBuffer = NULL;
                        L5197_puc_Bitmap = P5093_ps_SwitchOption->Graphic;
                } else {
                        L5195_i_Result = -2;
                        goto loc_8C8;
                }
        }
        F1653_ConvertCoordinates(P5093_ps_SwitchOption->Box, L5198_ai_XYZ);
        F0132_VIDEO_Blit(L5197_puc_Bitmap, G0348_Bitmap_Screen, L5198_ai_XYZ, 0, 0, P5093_ps_SwitchOption->Width, C160_BYTE_WIDTH_SCREEN, P5093_ps_SwitchOption->TransparentColor);
        L5195_i_Result = 0;
        loc_8C8:
        if (L5196_puc_TemporaryBuffer) {
                F1651_FreeMemory(L5196_puc_TemporaryBuffer);
                L5196_puc_TemporaryBuffer = NULL;
        }
        if (L5195_i_Result); /* Useless code */
        return L5195_i_Result;
}

void F1508_FreeMemoryOfAllSwitchOptionGraphics(SWITCH_OPTION* P5095_ps_SwitchOptions, int16_t P5096_i_SwitchOptionCount) {
        int16_t L5200_i_SwitchOptionIndex;


        for (L5200_i_SwitchOptionIndex = 0; L5200_i_SwitchOptionIndex < P5096_i_SwitchOptionCount; L5200_i_SwitchOptionIndex++) {
                if (P5095_ps_SwitchOptions[L5200_i_SwitchOptionIndex].Graphic) {
                        F1651_FreeMemory(P5095_ps_SwitchOptions[L5200_i_SwitchOptionIndex].Graphic);
                        P5095_ps_SwitchOptions[L5200_i_SwitchOptionIndex].Graphic = NULL;
                }
        }
}

void F1509_Initialization(
SWITCH_OPTION* P5097_ps_SwitchOption,
int16_t P5484_i_SwitchOptionCount)
{
        char L5201_ac_Unreference[18];


        if (P5097_ps_SwitchOption->Status < 0) {
                F0008_MAIN_ClearBytes(G0348_Bitmap_Screen, 32000L);
        } else {
                F0466_EXPAND_GraphicToBitmap((unsigned char*)P5097_ps_SwitchOption->Graphic, 0L, 0, 0);
                P5097_ps_SwitchOption->Status = -1;
        }
        if (P5097_ps_SwitchOption->Graphic) {
                F1651_FreeMemory(P5097_ps_SwitchOption->Graphic);
                P5097_ps_SwitchOption->Graphic = NULL;
        }
}

int16_t F1510_LoadDataFile(char* P5002_pc_FileName, SWITCH_OPTION* P5003_ps_SwitchOption, int16_t P5485_i_SwitchOptionCount)
{
        int16_t L5004_i_Result;
        int16_t L5005_;
        int16_t* L5006_;
        unsigned char* L5007_puc_HeaderBuffer;
        unsigned char* L5008_puc_SegmentHeadersBuffer;
        unsigned char* L5009_;
        unsigned int16_t* L5010_aui_Palette;
        HEADER* L5011_ps_Header;
        SEGMENTHEADER* L5012_ps_SegmentHeader;
        int16_t* L5013_;


        L5010_aui_Palette = NULL;
        L5009_ = NULL;
        L5008_puc_SegmentHeadersBuffer = NULL;
        L5007_puc_HeaderBuffer = NULL;
        if (F1503_ReadFTLHeaders(P5002_pc_FileName, &L5007_puc_HeaderBuffer, &L5008_puc_SegmentHeadersBuffer, MASK0x0800_MEMREQ) < 0) {
                L5004_i_Result = -1;
                goto loc_C86;
        }
        L5011_ps_Header = (HEADER*)L5007_puc_HeaderBuffer;
        L5012_ps_SegmentHeader = (SEGMENTHEADER*)L5008_puc_SegmentHeadersBuffer;
        if (L5011_ps_Header->Unknown1 != 0x5000) {
                L5004_i_Result = -2;
                goto loc_C86;
        }
        if (F1504_LoadSegment(P5002_pc_FileName, L5011_ps_Header, L5012_ps_SegmentHeader, 0x30, 0, &L5009_, MASK0x0800_MEMREQ) < 0) { /* Load data */
                L5004_i_Result = -3;
                goto loc_C86;
        }
        L5006_ = (int16_t*)L5009_;
        if (F1504_LoadSegment(P5002_pc_FileName, L5011_ps_Header, L5012_ps_SegmentHeader, 0x30, 1, (unsigned char**)&L5010_aui_Palette, MASK0x0800_MEMREQ) >= 0) { /* Load Color palette */
                F7317_MAIN_CopyBytes_Alt(L5010_aui_Palette, G3406_aui_Palette_SwitchScreen, MASK0x0800_MEMREQ, MASK0x0040_MEMREQ, 32L);
                F1651_FreeMemory(L5010_aui_Palette);
                L5010_aui_Palette = NULL;
        }
        for (L5005_ = 0; L5005_ < *L5006_; L5005_++) {
                P5003_ps_SwitchOption[L5005_].Command = -1;
                if (((SWITCHIMAGE*)(L5006_ + 1))[L5005_].SegmentType == 0) {
                        P5003_ps_SwitchOption[L5005_].Status = -1;
                        continue;
                }
                if (F1504_LoadSegment(P5002_pc_FileName, L5011_ps_Header, L5012_ps_SegmentHeader, ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].SegmentType, ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].SegmentId, &P5003_ps_SwitchOption[L5005_].Graphic, MASK0x0010_MEMREQ) < 0) {
                        L5004_i_Result = -4;
                        goto loc_C86;
                }
                L5013_ = (int16_t*)P5003_ps_SwitchOption[L5005_].Graphic;
                P5003_ps_SwitchOption[L5005_].Status = 1;
                P5003_ps_SwitchOption[L5005_].Box[2] = L5013_[0]; /* Width in pixels */
                P5003_ps_SwitchOption[L5005_].Box[3] = L5013_[1]; /* Height in pixels */
                P5003_ps_SwitchOption[L5005_].Box[0] = ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].X;
                P5003_ps_SwitchOption[L5005_].Box[1] = ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].Y;
                P5003_ps_SwitchOption[L5005_].TransparentColor = ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].TransparentColor;
                P5003_ps_SwitchOption[L5005_].Width = P5003_ps_SwitchOption[L5005_].Box[2] / 2;
                P5003_ps_SwitchOption[L5005_].Height = P5003_ps_SwitchOption[L5005_].Box[3];
                P5003_ps_SwitchOption[L5005_].ColorDepth = 4;
                F0088_strcpy(&P5003_ps_SwitchOption[L5005_].FTLFileName, ((SWITCHIMAGE*)(L5006_ + 1))[L5005_].FTLFileName);
        }
        L5004_i_Result = *L5006_; /* Number of switch options */
        loc_C86:
        if (L5007_puc_HeaderBuffer) {
                F1651_FreeMemory(L5007_puc_HeaderBuffer);
        }
        if (L5008_puc_SegmentHeadersBuffer) {
                F1651_FreeMemory(L5008_puc_SegmentHeadersBuffer);
        }
        if (L5009_) {
                F1651_FreeMemory(L5009_);
        }
        if (L5004_i_Result);
        return L5004_i_Result;
}

void F1511_Unreferenced(
)
{
        asm {
                rte
        }
}

void F1513_MainLoop(int16_t P5004_i_SwitchOptionCount)
{
        int16_t L5014_i_SwitchOptionIndex;
        int16_t L5015_i_Command;
        int16_t L5016_i_;
        long L5017_l_VerticalBlankCount;
        long L5018_l_Unreferenced;
        char* L5020_pc_NextFTLModuleToRun;
        UD_COMMAND L5019_s_UDCommand;


        F1506_SetMouseInputFromSwitchOptions(G3400_as_SwitchOptions, P5004_i_SwitchOptionCount, G3407_as_MouseInput);
        F1509_Initialization(G3400_as_SwitchOptions, P5004_i_SwitchOptionCount);
        F0078_MOUSE_DisableScreenUpdate();
        F0436_STARTEND_FadeToPalette(G3406_aui_Palette_SwitchScreen);
        F7012_AddToInputInfoList(&G3408_s_InputInfo);
        for (;;) {
                L5015_i_Command = F7015_ProcessNextUDCommand(&L5019_s_UDCommand) & 0xFF;
                for (L5014_i_SwitchOptionIndex = 0; L5014_i_SwitchOptionIndex < P5004_i_SwitchOptionCount; L5014_i_SwitchOptionIndex++) {
                        if (G3400_as_SwitchOptions[L5014_i_SwitchOptionIndex].Command == L5015_i_Command) {
                                L5020_pc_NextFTLModuleToRun = G3400_as_SwitchOptions[L5014_i_SwitchOptionIndex].FTLFileName;
                                F0077_MOUSE_EnableScreenUpdate_CPSE();
                                F1507_DrawSwitchOptionBitmap(&G3400_as_SwitchOptions[L5014_i_SwitchOptionIndex]);
                                goto loc_D76;
                        }
                }
        }
        loc_D76:
        F1508_FreeMemoryOfAllSwitchOptionGraphics(G3400_as_SwitchOptions, P5004_i_SwitchOptionCount);
        L5016_i_ = 0;
        if (L5016_i_ < 0) { /* Never executed */
                asm {
                        move.w  #0x6160,(a5)
                        move.w  #0,2(a5)
                }
        } else {
                if ((L5020_pc_NextFTLModuleToRun[0] == 'E') && (L5020_pc_NextFTLModuleToRun[1] == 'X') && (L5020_pc_NextFTLModuleToRun[2] == 'I') && (L5020_pc_NextFTLModuleToRun[3] == 'T')) {
                        L5017_l_VerticalBlankCount = G1086_VerticalBlankCount + 30;
                        while (G1086_VerticalBlankCount < L5017_l_VerticalBlankCount); /* Wait for 30 vertical blanks */
                        Super(0L);
                        asm {   /* Restart computer */
                                movea.w (0).w,sp
                                movea.l (4).w,a0
                                jmp     (a0)
                        }
                }
                asm {   /* Set next FTL module to run */
                        move.w  #0x6160,(a5)
                        move.w  #0x5223,2(a5)
                        move.l  L5020_pc_NextFTLModuleToRun(a6),0x1C(a5)
                }
        }
}

void main(int16_t P5000_)
{
        int16_t L5000_i_SwitchOptionCount;


        F1372_SetVerticalBlankInterrupt(1);
        F0924_SetCriticalErrorHandler();
        if ((L5000_i_SwitchOptionCount = F1510_LoadDataFile("A:\\SWITCH.DAT", G3400_as_SwitchOptions, SWITCH_OPTION_COUNT)) >= 0) {
                F0436_STARTEND_FadeToPalette(G3401_aui_Palette_Black);
                F1525_OpenVDIWorkstation(P5000_ == 0);
                G0578_B_UseByteBoxCoordinates = C0_FALSE;
                G0348_Bitmap_Screen = Physbase();
                F1531_MouseHandler(1, G3404_VDIHandle);
                F1530_ShowMousePointerAsArrow(G3404_VDIHandle, 0);
                F0077_MOUSE_EnableScreenUpdate_CPSE();
                F1513_MainLoop(L5000_i_SwitchOptionCount);
                F1372_SetVerticalBlankInterrupt(2);
                F1531_MouseHandler(2, G3404_VDIHandle);
        }
        F1526_CloseWorkstationAndExitApplication();
}
#endif
#ifdef MEDIA663_A31M_A35M_X31J_AU1E_AU2F_AU2G
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 unsigned char* MEM1_06_Allocate();
extern long GRF1_04_GetBitmapMemorySize();

extern char G3300_aac_Palette_Selection[16][4];
extern int16_t G3301_ai_Bitmap_Selection[8200];
extern int16_t G3302_ai_Bitmap_Option1[250];
extern int16_t G3303_ai_Bitmap_Option2[158];
extern int16_t G3304_ai_Bitmap_Option3[282];
extern int16_t* G3305_api_Bitmap_Options[3];
extern int16_t G3306_aai_XYZ[3][4];
extern long G3307_al_OptionNames[3];


BOOLEAN F0798_COMMAND_IsPointInZone(P0749_pi_XYZ, P0750_i_X, P0751_i_Y)
int16_t* P0749_pi_XYZ;
int16_t P0750_i_X;
int16_t P0751_i_Y;
{
        if ((P0750_i_X >= M704_ZONE_LEFT(P0749_pi_XYZ)) && (P0750_i_X <= M705_ZONE_RIGHT(P0749_pi_XYZ)) && (P0751_i_Y >= M706_ZONE_TOP(P0749_pi_XYZ)) && (P0751_i_Y <= M707_ZONE_BOTTOM(P0749_pi_XYZ))) {
                return C1_TRUE;
        } else {
                return C0_FALSE;
        }
}

long F1288_GetChosenOption(
#ifdef MEDIA660_A31M_X31J_AU1E_AU2F_AU2G
void)
#endif
#ifdef MEDIA742_A35M
long P4391_)
#endif
{
        register unsigned char* L4788_;
        register long L4789_l_ByteCount;
        register int16_t L4790_i_ChoiceIndex;
        register long L4791_l_ChosenOption;
        USIODATA L4792_;


        L4788_ = NULL;
        L4791_l_ChosenOption = 0;
#ifdef MEDIA742_A35M
        if ((P4391_ == 0) && ((L4790_i_ChoiceIndex = F1298_GetAutomaticStartupLanguage()) != -1)) {
                return G3307_al_OptionNames[L4790_i_ChoiceIndex];
        } else {
#endif
                VDEO_15_ApplyNormalColorPalette(G9002_VDEObase->_b_0x54_ColorCount, G9002_VDEObase->_pc_0x56_ColorComponents, 0);
                VDEO_16_PaletteControl(1);
                VDEO_14_SetNormalColorPalette(16, G3300_aac_Palette_Selection);
                VDEO_05_ExpandGraphicToBitmap(G3301_ai_Bitmap_Selection, 0);
                VDEO_15_ApplyNormalColorPalette(16, G3300_aac_Palette_Selection, 0);
                VDEO_16_PaletteControl(0);
                USIO_07_ShowPointer();
#ifdef MEDIA766_AU1E_AU2F_AU2G
                L4789_l_ByteCount = GRF1_04_GetBitmapMemorySize(53, 40);
#endif
#ifdef MEDIA692_X31J
                L4789_l_ByteCount = GRF1_04_GetBitmapMemorySize(62, 40);
#endif
#ifdef MEDIA655_A31M
                L4789_l_ByteCount = GRF1_04_GetBitmapMemorySize(62, 44);
#endif
#ifdef MEDIA742_A35M
                L4789_l_ByteCount = GRF1_04_GetBitmapMemorySize(32, 23);
#endif
                if (L4788_ = MEM1_06_Allocate(L4789_l_ByteCount, 8)) {
                        while (USIO_16_ExtractFirstUsioDataFromQueue(&L4792_)); /* Remove all entries from queue */
                        for (;;) {
                                while ((USIO_17_WaitUntilKeyboardOrMouseInput(&L4792_) != 17) || (L4792_.Data.UsioMouse.Buttons != 2));
                                for (L4790_i_ChoiceIndex = 0; L4790_i_ChoiceIndex < 3; L4790_i_ChoiceIndex++) {
                                        if (F0798_COMMAND_IsPointInZone(G3306_aai_XYZ[L4790_i_ChoiceIndex], L4792_.Data.UsioMouse.X, L4792_.Data.UsioMouse.Y)) {
                                                goto loc_F8A;
                                        }
                                }
                        }
                        loc_F8A:
                        GRF1_05_F0466_EXPAND_GraphicToBitmap(G3305_api_Bitmap_Options[L4790_i_ChoiceIndex], L4788_);
                        VDEO_06_BlitToScreen(L4788_, 0, G3306_aai_XYZ[L4790_i_ChoiceIndex], 0, 0, G3306_aai_XYZ[L4790_i_ChoiceIndex][2], -1, G3306_aai_XYZ[L4790_i_ChoiceIndex][3]);
                        L4791_l_ChosenOption = G3307_al_OptionNames[L4790_i_ChoiceIndex];
#ifdef MEDIA742_A35M
                        F1299_SaveLanguageChoice(L4790_i_ChoiceIndex);
#endif
                }
                if (L4788_ != NULL) {
                        MEM1_07_Free(L4788_);
                }
                USIO_06_HidePointer();
                return L4791_l_ChosenOption;
#ifdef MEDIA742_A35M
        }
#endif
}

void main(P4382_, P4383_, P4384_)
FTL_LIBRARY* P4382_;
FTL_EXECUTABLE* P4383_;
long P4384_;
{
        VDEO_FTL_LIBRARY* L4772_;
        GRF1_FTL_LIBRARY* L4773_;
        FTL_LIBRARY* L4774_;
        USIO_FTL_LIBRARY* L4775_;
        register long L4776_l_ChosenOption;


        L4776_l_ChosenOption = 0;
        L4772_ = NULL;
        L4773_ = NULL;
        L4774_ = NULL;
        L4775_ = NULL;
        if ((F9008_OpenPRIM(P4382_, 2)) && (L4772_ = F9004_OpenVDEO(2)) && (L4773_ = F9000_OpenGRF1(2)) && (L4774_ = F9006_OpenMEM1(2)) && (L4775_ = F9002_OpenUSIO(2))) {
#ifdef MEDIA742_A35M
                F1300_Initialize();
                L4776_l_ChosenOption = F1288_GetChosenOption(P4384_);
#endif
#ifdef MEDIA660_A31M_X31J_AU1E_AU2F_AU2G
                L4776_l_ChosenOption = F1288_GetChosenOption();
#endif
        }
#ifdef MEDIA742_A35M
        F1301_Cleanup();
#endif
        F9003_CloseUSIO(L4775_);
        F9007_CloseMEM1(L4774_);
        F9001_CloseGRF1(L4773_);
        F9005_CloseVDEO(L4772_);
        P4383_->Parameter = L4776_l_ChosenOption;
}
#endif
#ifdef MEDIA667_P31J
typedef struct {
        int16_t Box[4];
        unsigned char* Bitmap;
} FIRESTRUCT;

void F8531_();

extern unsigned char G4167_auc_Bitmap_SwitchMenu_Japanese[6725];
extern unsigned char G8483_[471];
extern unsigned char G8484_[310];
extern unsigned char G8485_[698];

unsigned char G0042_auc_Graphic562_Bitmap_ArrowPointer[M075_BITMAP_BYTE_COUNT(18, 18)] = {
        0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0xCC, 0xCC, 0xCC,
        0x0F, 0xF0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0x0C, 0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x00, 0xCC, 0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0C, 0xCC, 0xC0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xC0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0x00, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
int16_t G8486_MouseButtonsStatus = 0;

unsigned char* G2159_puc_Bitmap_Source;
unsigned char* G2160_puc_Bitmap_Destination;
long G8489_;
unsigned char G2158_auc_Bitmap_PixelLine[160]; /* 320 pixels */
FIRESTRUCT G8491_as_Firestruct[6];
int16_t G8492_MouseX;
int16_t G8493_MouseY;
IO_DRIVER* G2161_IODriver;
VIDEO_DRIVER* G2156_VideoDriver;
IO_DRIVER* G2162_IODriver;
char G8497_[4];

int16_t F8530_(int16_t P2561_i_X, int16_t P2562_i_Y, int16_t P2563_i_ButtonsStatus)
{
        int16_t L3412_i_;


        L3412_i_ = 0;
        asm     push    ds
        asm     mov     ax, cs:V0713000_DSRegisterBackup
        asm     mov     ds, ax
        if ((P2563_i_ButtonsStatus < C32_MOUSE_EVENT_CHANGE_SCREEN_REGION) && (P2563_i_ButtonsStatus & 0x03)) {
                G8492_MouseX = P2561_i_X;
                G8493_MouseY = P2562_i_Y;
                G8486_MouseButtonsStatus = P2563_i_ButtonsStatus;
        }
        asm     pop     ds
}

void F8531_(
)
{
        (*(G2161_IODriver->IODRV_09_RegisterMousePointerBitmap))(C0_POINTER_ARROW, G0042_auc_Graphic562_Bitmap_ArrowPointer, 0, 0);
        (*(G2161_IODriver->IODRV_10_SetMousePointer))(C0_POINTER_ARROW);
        (*(G2161_IODriver->IODRV_02_SetMouseHandler))(F8530_);
}

void F8532_MOUSE_ShowPointer(
)
{
        (*(G2161_IODriver->IODRV_07_ShowMousePointer))();
}

void F8533_MOUSE_HidePointer(
)
{
        (*(G2161_IODriver->IODRV_08_HideMousePointer))();
}

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

void F0713_InitIOInterrupt(
)
{
        G2162_IODriver = G2161_IODriver = (IO_DRIVER*)getvect(C254_DM_IO_INTERRUPT);
        asm     mov     ax, ds
        asm     mov     cs:V0713000_DSRegisterBackup, ax
        return;
        V0713000_DSRegisterBackup:
        asm     V0713000_DSRegisterBackup      dw      0
}

#include "IMAGE4.C"

FUNC_DEF void F0690_CopyPixelLineToScreenWithoutTransparency(
unsigned int16_t P2355_ui_DestinationPixelIndex,
int16_t P2356_ui_PixelCount)
{
        (*(G2156_VideoDriver->VIDRV_00_CopyPixelsToScreenWithoutTransparency))(G2158_auc_Bitmap_PixelLine, P2355_ui_DestinationPixelIndex % 320, P2355_ui_DestinationPixelIndex, P2356_ui_PixelCount);
}

FUNC_DEF void F0691_IMG_ExpandGraphicToScreen(
unsigned char* P2357_puc_Graphic,
int16_t P2358_i_X,
int16_t P2359_i_Y)
{
        unsigned int16_t L2740_ui_Width;
        unsigned int16_t L2741_ui_Height;
        unsigned int16_t L2743_ui_;
        int16_t L2744_i_;
        int16_t L2745_i_;
        int16_t L2746_i_;
        unsigned int16_t L2747_ui_;
        unsigned char L2748_uc_;
        char L2749_c_Color;
        int16_t L2750_i_;
        unsigned int16_t L2751_ui_;
        unsigned int16_t L2752_ui_;


        G2160_puc_Bitmap_Destination = G2158_auc_Bitmap_PixelLine;
        G2159_puc_Bitmap_Source = P2357_puc_Graphic;
        L2740_ui_Width = *((unsigned int16_t*)G2159_puc_Bitmap_Source);
        L2741_ui_Height = *((unsigned int16_t*)G2159_puc_Bitmap_Source + 1);
        L2745_i_ = ((L2741_ui_Height + P2359_i_Y - 1) * 320) + L2740_ui_Width + P2358_i_X;
        G2159_puc_Bitmap_Source += 4;
        L2750_i_ = L2743_ui_ = (P2359_i_Y * 320) + P2358_i_X;
        L2746_i_ = 320 - L2740_ui_Width;
        L2751_ui_ = L2740_ui_Width;
        do {
                L2748_uc_ = *G2159_puc_Bitmap_Source++;
                L2749_c_Color = L2748_uc_ & 0x0F;
                if (!(L2748_uc_ & 0x80)) {
                        L2752_ui_ = (L2748_uc_ >> 4) + 1;
                        while (L2752_ui_ >= L2751_ui_) {
                                F0685_IMG3_LineColorFilling(L2743_ui_ % 320, L2749_c_Color, L2751_ui_);
                                F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                L2750_i_ += 320;
                                L2743_ui_ += L2751_ui_ + L2746_i_;
                                L2752_ui_ -= L2751_ui_;
                                L2751_ui_ = L2740_ui_Width;
                        }
                        if (L2752_ui_ > 0) {
                                F0685_IMG3_LineColorFilling(L2743_ui_ % 320, L2749_c_Color, L2752_ui_);
                                L2743_ui_ += L2752_ui_;
                                L2751_ui_ -= L2752_ui_;
                        }
                } else {
                        if (!(L2748_uc_ & 0x40)) {
                                L2752_ui_ = *G2159_puc_Bitmap_Source++ + 1;
                        } else {
                                L2752_ui_ = (*(G2159_puc_Bitmap_Source + 1) | (*G2159_puc_Bitmap_Source << 8)) + 1;
                                G2159_puc_Bitmap_Source += 2;
                        }
                        switch (L2748_uc_ & 0x30) {
                                case 0x00:
                                        while (L2752_ui_ >= L2751_ui_) {
                                                F0685_IMG3_LineColorFilling(L2743_ui_ % 320, L2749_c_Color, L2751_ui_);
                                                F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                                L2750_i_ += 320;
                                                L2743_ui_ += L2751_ui_ + L2746_i_;
                                                L2752_ui_ -= L2751_ui_;
                                                L2751_ui_ = L2740_ui_Width;
                                        }
                                        if (L2752_ui_ > 0) {
                                                F0685_IMG3_LineColorFilling(L2743_ui_ % 320, L2749_c_Color, L2752_ui_);
                                                L2743_ui_ += L2752_ui_;
                                                L2751_ui_ -= L2752_ui_;
                                        }
                                        break;
                                case 0x10:
                                        if (L2752_ui_ & 1) {
                                                F0790_(L2743_ui_++ % 320, L2749_c_Color);
                                                if (--L2751_ui_ == 0) {
                                                        F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                                        L2750_i_ += 320;
                                                        L2751_ui_ = L2740_ui_Width;
                                                        L2743_ui_ += L2746_i_;
                                                }
                                                L2752_ui_--;
                                        }
                                        L2744_i_ = 0;
                                        L2747_ui_ = L2752_ui_;
                                        while (L2752_ui_ >= L2751_ui_) {
                                                F1003_(L2744_i_, L2743_ui_ % 320, L2751_ui_);
                                                F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                                L2750_i_ += 320;
                                                L2743_ui_ += L2751_ui_ + L2746_i_;
                                                L2744_i_ += L2751_ui_;
                                                L2752_ui_ -= L2751_ui_;
                                                L2751_ui_ = L2740_ui_Width;
                                        }
                                        if (L2752_ui_ > 0) {
                                                F1003_(L2744_i_, L2743_ui_ % 320, L2752_ui_);
                                                L2743_ui_ += L2752_ui_;
                                                L2744_i_ += L2752_ui_;
                                                L2751_ui_ -= L2752_ui_;
                                        }
                                        G2159_puc_Bitmap_Source += (L2747_ui_ >> 1);
                                        break;
                                case 0x30:
                                        while (L2752_ui_ >= L2751_ui_) {
                                                F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                                L2750_i_ += 320;
                                                L2743_ui_ += L2751_ui_ + L2746_i_;
                                                L2752_ui_ -= L2751_ui_;
                                                L2751_ui_ = L2740_ui_Width;
                                        }
                                        if (L2752_ui_ > 0) {
                                                L2743_ui_ += L2752_ui_;
                                                L2751_ui_ -= L2752_ui_;
                                        }
                                        F0790_(L2743_ui_++ % 320, L2749_c_Color);
                                        if (--L2751_ui_ == 0) {
                                                F0690_CopyPixelLineToScreenWithoutTransparency(L2750_i_, L2740_ui_Width);
                                                L2750_i_ += 320;
                                                L2743_ui_ += L2746_i_;
                                                L2751_ui_ = L2740_ui_Width;
                                        }
                        }
                }
        } while (L2743_ui_ < L2745_i_);
}

void F8541_(int16_t* P2577_pi_Box, int16_t P2578_i_Color, int16_t P2579_i_Width)
{
        (*(G2156_VideoDriver->VIDRV_01_FillBox))(NULL, P2577_pi_Box, P2578_i_Color, P2579_i_Width);
}

void F8542_(
)
{
        int16_t L3429_ai_Box[4] = { 0, 319, 0, 199 };


        F8541_(L3429_ai_Box, C00_COLOR_BLACK, 320);
}

void F8543_(unsigned char* P2580_puc_Bitmap_Source, int16_t* P2581_pi_Box, int16_t P2582_i_X, int16_t P2583_i_Y, int16_t P2584_i_SourceWidth, int16_t P2585_i_DestinationWidth, int16_t P2586_i_TransparentColor, int16_t P2587_i_Flip)
{
        (*(G2156_VideoDriver->VIDRV_02_Blit))(P2580_puc_Bitmap_Source, NULL, P2581_pi_Box, P2582_i_X, P2583_i_Y, P2584_i_SourceWidth, P2585_i_DestinationWidth, P2586_i_TransparentColor, P2587_i_Flip);
}

void F8544_(int16_t P2588_i_)
{
        (*(G2156_VideoDriver->VIDRV_08_SetMultipleColorsInPalette))(P2588_i_);
}

void F8545_PALETTE_SetCurtain(unsigned char P2589_uc_)
{
        (*(G2156_VideoDriver->VIDRV_11_PALETTE_SetCurtain))(P2589_uc_);
}

void F8546_(
)
{
        (*(G2156_VideoDriver->VIDRV_07_WaitVerticalBlank))();
}

int16_t G8498_ = 0;

void F8547_(unsigned char* P2590_puc_, int16_t P2591_i_, int16_t P2592_i_)
{
        int16_t L3430_i_;
        int16_t L3431_i_;


        if (G8498_ < 6) {
                L3430_i_ = *((unsigned int16_t*)P2590_puc_);
                L3431_i_ = *((unsigned int16_t*)(P2590_puc_ + 2));
                M768_BOX_LEFT(G8491_as_Firestruct[G8498_].Box) = P2591_i_;
                M769_BOX_RIGHT(G8491_as_Firestruct[G8498_].Box) = P2591_i_ + L3430_i_ - 1;
                M770_BOX_TOP(G8491_as_Firestruct[G8498_].Box) = P2592_i_;
                M771_BOX_BOTTOM(G8491_as_Firestruct[G8498_].Box) = P2592_i_ + L3431_i_ - 1;
                G8491_as_Firestruct[G8498_].Bitmap = P2590_puc_;
                G8498_++;
        }
}

void main(
)
{
        int16_t L3432_i_;
        int16_t L3433_i_CommandLineOrdinal = 0;
        char* L3434_apc_CommandLines[3] = {
                "anim story.anm",
                "csbutil",
                "csbgame"
        }; /* SCOPY */


        F8534_InitVideoInterrupt();
        F0713_InitIOInterrupt();
        F8531_();
        F8545_PALETTE_SetCurtain(C0_BLACK_PALETTE);
        F8542_();
        F8544_(C26_SWITCH);
        F0691_IMG_ExpandGraphicToScreen(G4167_auc_Bitmap_SwitchMenu_Japanese, 0, 0);
        F8545_PALETTE_SetCurtain(C1_NORMAL_PALETTE);
        F8547_(G8483_, 52, 23);
        F8547_(G8484_, 56, 75);
        F8547_(G8485_, 47, 131);
        F8532_MOUSE_ShowPointer();
        do {
                if (M007_GET(G8486_MouseButtonsStatus, MASK0x0002_MOUSE_LEFT_BUTTON)) {
                        for (L3432_i_ = 0; L3432_i_ < G8498_; L3432_i_++) {
                                if ((M768_BOX_LEFT(G8491_as_Firestruct[L3432_i_].Box) <= G8492_MouseX) && (M769_BOX_RIGHT(G8491_as_Firestruct[L3432_i_].Box) >= G8492_MouseX) && (M770_BOX_TOP(G8491_as_Firestruct[L3432_i_].Box) <= G8493_MouseY) && (M771_BOX_BOTTOM(G8491_as_Firestruct[L3432_i_].Box) >= G8493_MouseY)) {
                                        L3433_i_CommandLineOrdinal = L3432_i_ + 1;
                                        F8533_MOUSE_HidePointer();
                                        F0691_IMG_ExpandGraphicToScreen(G8491_as_Firestruct[L3432_i_].Bitmap, M768_BOX_LEFT(G8491_as_Firestruct[L3432_i_].Box), M770_BOX_TOP(G8491_as_Firestruct[L3432_i_].Box));
                                        break;
                                }
                        }
                }
        } while (L3433_i_CommandLineOrdinal == 0);
        setvect(C253_INTERRUPT, (void interrupt (*)())(L3434_apc_CommandLines[L3433_i_CommandLineOrdinal - 1]));
        exit(L3433_i_CommandLineOrdinal);
}
#endif
#ifdef MEDIA670_F31E_F31J
#include "SWITCHDA.C"

unsigned char G4105_WORK[2048];
unsigned char G4104_SWORK[16384];
unsigned char G4106_EGBPARA[150];
int G4103_WRITE_PAGE;
char G4196_i_Unreferenced[4];
char G8183_aac_FullPalette[32][3];
char G4148_ac_Unreferenced[3];
char G4094_CURTAIN_FLAG;
int16_t G8204_;
int16_t G4157_i_MouseX;
int16_t G4159_i_MouseY;
int16_t G4158_i_MouseButtonsStatus;
UDSTA G4161_as_ButtonBitmaps[6];
int16_t G4160_i_ButtonBitmapCount;


STATICFUNCTION void F2261_Palette(
void
) /* Similar to F8156_SetPalette */
{
        char L3778_c_Red;
        char L3779_c_Green;
        char L3780_c_Blue;
        int16_t L2531_i_;


        DWORD(G4106_EGBPARA) = 16;
        EGB_writePage(G4105_WORK, 1);
        DWORD(G4106_EGBPARA) = 16;
        for (L2531_i_ = 0; L2531_i_ < 16; L2531_i_++) {
                DWORD(G4106_EGBPARA + (L2531_i_ * 8) + 4) = L2531_i_; /* Color index */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 8) = G8183_aac_FullPalette[L2531_i_ + 16][2] << 2; /* Blue */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 9) = G8183_aac_FullPalette[L2531_i_ + 16][0] << 2; /* Red */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 10) = G8183_aac_FullPalette[L2531_i_ + 16][1] << 2; /* Green */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 11) = 0; /* 0 */
        }
        EGB_palette(G4105_WORK, 1, G4106_EGBPARA);
        EGB_writePage(G4105_WORK, 0);
        for (L2531_i_ = 0; L2531_i_ < 16; L2531_i_++) {
                DWORD(G4106_EGBPARA + (L2531_i_ * 8) + 4) = L2531_i_; /* Color index */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 8) = G8183_aac_FullPalette[L2531_i_][2] << 2; /* Blue */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 9) = G8183_aac_FullPalette[L2531_i_][0] << 2; /* Red */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 10) = G8183_aac_FullPalette[L2531_i_][1] << 2; /* Green */
                BYTE(G4106_EGBPARA + (L2531_i_ * 8) + 11) = 0; /* 0 */
        }
        EGB_palette(G4105_WORK, 1, G4106_EGBPARA);
}

void F0694_SetMultipleColorsInPalette(
int16_t P2368_i_PaletteIndex FINAL_SEPARATOR /* Similar to F8157_VIDRV_08_SetMultipleColorsInPalette */
{
        int16_t L3783_i_;
        char L3782_c_ColorIndex;


        for (L3783_i_ = 0; (L3782_c_ColorIndex = G8176_PaletteTable[P2368_i_PaletteIndex][L3783_i_].Index) >= 0; L3783_i_++) {
                if (L3782_c_ColorIndex >= 32)
                        continue;
                G8183_aac_FullPalette[L3782_c_ColorIndex][0] = G8176_PaletteTable[P2368_i_PaletteIndex][L3783_i_].Red;
                G8183_aac_FullPalette[L3782_c_ColorIndex][1] = G8176_PaletteTable[P2368_i_PaletteIndex][L3783_i_].Green;
                G8183_aac_FullPalette[L3782_c_ColorIndex][2] = G8176_PaletteTable[P2368_i_PaletteIndex][L3783_i_].Blue;
        }
        if (G4094_CURTAIN_FLAG == 1) {
                F2261_Palette();
        }
}

void F1012_PALETTE_SetCurtain(
unsigned char P2369_ui_ FINAL_SEPARATOR /* If 0, curtain (fully black palette). If 1, normal palette */
{
        int16_t L6550_;


        if (P2369_ui_ == C0_BLACK_PALETTE) {
                DWORD(G4106_EGBPARA + 0) = 16; /* Number of colors to set */
                for (L6550_ = 0; L6550_ < 16; L6550_++) {
                        DWORD(G4106_EGBPARA + (L6550_ * 8) + 4) = L6550_; /* Color index */
                        BYTE(G4106_EGBPARA + (L6550_ * 8) + 8) = 0; /* Blue */
                        BYTE(G4106_EGBPARA + (L6550_ * 8) + 9) = 0; /* Red */
                        BYTE(G4106_EGBPARA + (L6550_ * 8) + 10) = 0; /* Green */
                        BYTE(G4106_EGBPARA + (L6550_ * 8) + 11) = 0; /* 0 */
                }
                EGB_palette(G4105_WORK, 0, G4106_EGBPARA);
        }
        if (P2369_ui_ == 1) {
                F2261_Palette();
        }
        G4094_CURTAIN_FLAG = P2369_ui_;
}

void F2234_ClearBothScreenBuffers(
void
)
{
#ifdef MEDIA488_F20E_F20J
        int16_t L6510_[4] = { 0, 319, 0, 199 };
#endif
#ifdef MEDIA670_F31E_F31J
        int16_t L6510_[4] = { 0, 0, 320, 200 };
#endif


        G4103_WRITE_PAGE = 1;
        F0135_VIDEO_FillBox(NULL, L6510_, C00_COLOR_BLACK, 320);
        G4103_WRITE_PAGE = 0;
        F0135_VIDEO_FillBox(NULL, L6510_, C00_COLOR_BLACK, 320);
}

void F0693_WaitVerticalBlank(
void
)
{
        DWORD(G4106_EGBPARA) = 0;
        EGB_palette(G4105_WORK, 1, G4106_EGBPARA);
}

void F2262_TIMER_A_EVENT(
void
)
{
#ifdef MEDIA670_F31E_F31J
        G8204_--;
#endif
}

void F2263_INIT_TIMER(
void
)
{
        SND_int_timer_a_set(F2262_TIMER_A_EVENT);
        SND_fm_timer_a_set(1, 100);
}

void F2264_INIT_PCM_SOUND(
void
)
{
        if (SND_init(G4104_SWORK)) {
                exit(0);
        }
        SND_elevol_set(1, 100, 100);
        SND_elevol_mute(49);
}

void F0359_COMMAND_ProcessClick_CPSC(int16_t P0725_i_X, int16_t P0726_i_Y, int16_t P0727_i_ButtonsStatus)
{
        if (P0727_i_ButtonsStatus < 32) {
                if (M007_GET(P0727_i_ButtonsStatus, MASK0x0002_MOUSE_LEFT_BUTTON | MASK0x0001_MOUSE_RIGHT_BUTTON)) {
                        G4157_i_MouseX = P0725_i_X;
                        G4159_i_MouseY = P0726_i_Y;
                        G4158_i_MouseButtonsStatus = P0727_i_ButtonsStatus;
                }
        }
}

void F2279_(unsigned char* P6123_puc_Bitmap, int16_t P6124_i_X, int16_t P6125_i_Y)
{
        int16_t L6595_i_Width;
        int16_t L6595_i_Height;


        if (G4160_i_ButtonBitmapCount < 6) {
                L6595_i_Width = *((int16_t*)P6123_puc_Bitmap);
                L6595_i_Height = *((int16_t*)P6123_puc_Bitmap + 1);
                G4161_as_ButtonBitmaps[G4160_i_ButtonBitmapCount].X1 = P6124_i_X;
                G4161_as_ButtonBitmaps[G4160_i_ButtonBitmapCount].X2 = P6124_i_X + L6595_i_Width - 1;
                G4161_as_ButtonBitmaps[G4160_i_ButtonBitmapCount].Y1 = P6125_i_Y;
                G4161_as_ButtonBitmaps[G4160_i_ButtonBitmapCount].Y2 = P6125_i_Y + L6595_i_Height - 1;
                G4161_as_ButtonBitmaps[G4160_i_ButtonBitmapCount].Bitmap = P6123_puc_Bitmap;
                G4160_i_ButtonBitmapCount++;
        }
}

void F2280_(unsigned char* P6126_, int16_t P6127_, int16_t P6128_)
{
        int16_t L6596_i_Width;
        int16_t L6597_i_Height;
        int16_t L6598_ai_XYZ[4];


        L6596_i_Width = *((int16_t*)P6126_);
        L6597_i_Height = *((int16_t*)P6126_ + 1);
        L6598_ai_XYZ[0] = P6127_;
        L6598_ai_XYZ[1] = P6128_;
        L6598_ai_XYZ[2] = L6596_i_Width;
        L6598_ai_XYZ[3] = L6597_i_Height;
        F0689_IMG_ExpandGraphicToBitmap(P6126_, G4108_DEC_BUF);
        F0132_VIDEO_Blit(G4108_DEC_BUF, NULL, L6598_ai_XYZ, 0, 0, L6596_i_Width, 320, CM1_COLOR_NO_TRANSPARENCY);
}

int main(int argc, char *argv[])
{
        int16_t L6588_;
        int16_t L6589_;
        int16_t L6590_i_Language;


        F2264_INIT_PCM_SOUND();
        F2263_INIT_TIMER();
        F2259_INIT_TOWNS_SCREEN();
        F2240_INIT_TOWNS_MOUSE();
        F0066_MOUSE_Initialize();
        F1012_PALETTE_SetCurtain(0);
        F2234_ClearBothScreenBuffers();
        F0694_SetMultipleColorsInPalette(C26_SWITCH);
        if (strcmp(argv[1], "JAPAN") == 0) {
                L6590_i_Language = 0;
        } else {
                L6590_i_Language = 1;
        }
        do {
                for (L6588_ = 0; L6588_ < 60; L6588_++) {
                        F0693_WaitVerticalBlank();
                }
                L6590_i_Language ^= 1;
                F1012_PALETTE_SetCurtain(0);
                if (L6590_i_Language) {
                        F2280_(G4167_auc_Bitmap_SwitchMenu_Japanese, 0, 0);
                } else {
                        F2280_(G4168_auc_Bitmap_SwitchMenu_English, 0, 0);
                }
                F1012_PALETTE_SetCurtain(1);
                G4160_i_ButtonBitmapCount = 0;
                F2279_(G4169_auc_Bitmap, 52, 15);
                F2279_(G4170_auc_Bitmap, 57, 59);
                F2279_(G4171_auc_Bitmap, 47, 105);
                if (L6590_i_Language) {
                        F2279_(G4172_auc_Bitmap_Japanese, 50, 150);
                } else {
                        F2279_(G4173_auc_Bitmap_English, 50, 150);
                }
                F0078_MOUSE_DisableScreenUpdate();
                L6589_ = 0;
                G4158_i_MouseButtonsStatus = 0;
                do {
                        if (M007_GET(G4158_i_MouseButtonsStatus, MASK0x0002_MOUSE_LEFT_BUTTON)) {
                                for (L6588_ = 0; L6588_ < G4160_i_ButtonBitmapCount; L6588_++) {
                                        if ((G4157_i_MouseX >= G4161_as_ButtonBitmaps[L6588_].X1) && (G4157_i_MouseX <= G4161_as_ButtonBitmaps[L6588_].X2) && (G4159_i_MouseY >= G4161_as_ButtonBitmaps[L6588_].Y1) && (G4159_i_MouseY <= G4161_as_ButtonBitmaps[L6588_].Y2)) {
                                                L6589_ = L6588_ + 1;
                                                F0077_MOUSE_EnableScreenUpdate_CPSE();
                                                F2280_(G4161_as_ButtonBitmaps[L6588_].Bitmap, G4161_as_ButtonBitmaps[L6588_].X1, G4161_as_ButtonBitmaps[L6588_].Y1);
                                                break;
                                        }
                                }
                        }
                } while (L6589_ == 0);
        } while (L6589_ == 4);
        F2241_RESTORE_TOWNS_MOUSE();
        SND_end();
        SND_elevol_mute(0);
        if (L6590_i_Language) {
                exit(L6589_);
        } else {
                exit(L6589_ + 3);
        }
}
#endif
