#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA678_F31E_F31J_X31J_P31J_SU1E_AU1E_AU2E_AU3E
void F7052_SaveGame(int16_t P3712_i_GameIndex)
{                                       /* P31J_X31J_AU2E_AU3E */       /* AU1E_SU1E */
#ifdef MEDIA773_SU1E_AU1E
        register GAME* L3938_ps_Game;
        register int16_t* L3939_pi_Checksum;
#endif
#ifdef MEDIA762_AU1E
        char* L6184_pc_Portrait;
        char* L3950_pc_;
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        int16_t L3953_i_;
        GAME* L3938_ps_Game;
        int16_t* L3939_pi_Checksum;
        int16_t L3952_i_;
        int16_t L3940_i_;
#endif
        unsigned int16_t L3941_i_;
        int16_t L3942_i_Checksum;
#ifdef MEDIA773_SU1E_AU1E
        SAVE_PART L3946_as_SaveParts[16];
#endif
        CSB_SAVE_HEADER* L3943_ps_;
        unsigned int16_t* L3944_pui_;
        unsigned int16_t* L3945_pui_;
#ifdef MEDIA773_SU1E_AU1E
        register int16_t L3952_i_;
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        int16_t L6049_i_Unreferenced;
        SAVE_PART L3946_as_SaveParts[16];
        PROGRAM_STATE L3947_s_;
        char L3948_c_;
        int16_t L3949_i_DungeonID;
        char* L3950_pc_;
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
        long L3951_l_;
#endif
#ifdef MEDIA762_AU1E
        char L6185_c_LoadPortraits;
#endif
#ifdef MEDIA769_AU3E
        int16_t L6145_;
#endif


#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        L3947_s_.Previous = G7128_LastProgramState;
        L3947_s_.Ordinal = ++G7129_ProgramStateOrdinal;
        G7128_LastProgramState = &L3947_s_;
        L3947_s_.ReturnValue = setjmp(L3947_s_.State);
        L3952_i_ = L3947_s_.ReturnValue;
        if (L3952_i_) {
                L3938_ps_Game = &G7111_Games[P3712_i_GameIndex];
                L3947_s_.ReturnValue = 0;
                if ((L3952_i_ >= 23000) && (L3952_i_ <= 29999)) {
                        L3953_i_ = -1;
                } else {
                        if (L3948_c_) {
                                F7071_();
                                L3948_c_ = C0_FALSE;
                        }
                        L3953_i_ = -2;
                }
        } else {
#endif
                L3938_ps_Game = &G7111_Games[P3712_i_GameIndex];
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                L3948_c_ = C0_FALSE;
                L3943_ps_ = (CSB_SAVE_HEADER*)&L3938_ps_Game->SaveHeader;
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                L3950_pc_ = NULL;
#endif
#ifdef MEDIA762_AU1E
                L6185_c_LoadPortraits = C0_FALSE;
#endif
#ifdef MEDIA769_AU3E
                L6145_ = C0_FALSE;
#endif
                if (!G7111_Games[C0_GAME_SOURCE].GameLoaded || !G7114_LoadedChampionCount || (F7076_AreAllChampionNamesUnique() == 0)) {
#ifdef MEDIA773_SU1E_AU1E
                        return;
                }
#ifdef MEDIA762_AU1E
                for (L3952_i_ = 0; L3952_i_ < L3938_ps_Game->GlobalData.PartyChampionCount; L3952_i_++) {
                        L6184_pc_Portrait = (char*)F7067_GetChampionInfo(L3938_ps_Game, L3952_i_, C31_CHAMPION_INFO_PORTRAIT);
                        F2105_CHAMPION_ConvertBitmapFromAtariSTPlanar(L6184_pc_Portrait, 32, 29);
                }
#endif
                if (F1994_(1 - P3712_i_GameIndex, L3938_ps_Game->FileName)) {
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        L3953_i_ = -4;
                } else {
                        if (P3712_i_GameIndex) {
                                L3940_i_ = M746_FILE_ID_SAVE_CSBGAME_DAT;
                        } else {
                                if (L3938_ps_Game->SaveHeaderFormat == C0x01_SAVE_HEADER_FORMAT_DUNGEON_MASTER) {
                                        L3949_i_DungeonID = L3938_ps_Game->SaveHeader.DungeonID;
                                } else {
                                        L3949_i_DungeonID = L3943_ps_->DungeonID;
                                }
                                if (L3949_i_DungeonID == C10_DUNGEON_DM) {
                                        L3940_i_ = M745_FILE_ID_SAVE_DMSAVE_DAT;
                                } else {
                                        if ((L3949_i_DungeonID == C13_DUNGEON_CSB_GAME) || (L3949_i_DungeonID == C12_DUNGEON_CSB_PRISON)) {
                                                L3940_i_ = M746_FILE_ID_SAVE_CSBGAME_DAT;
                                        } else {
                                                L3953_i_ = -5;
                                                goto loc_13DAD;
                                        }
                                }
                        }
#ifdef MEDIA768_AU2E_AU3E
                        F2123_EncodeAllPortraitsBeforeSaving(L3938_ps_Game);
#endif
#ifdef MEDIA769_AU3E
                        L6145_ = C1_TRUE;
#endif
                        G7004_SavedGameFileHandle = F7132_(L3940_i_);
                        F7137_FileOperation(G7004_SavedGameFileHandle, 0xF004, C16_FILEOP_BYTE_COUNT, (long)1, 0);
#endif
                        F7072_GetDialogChoiceOrdinal((P3712_i_GameIndex != C0_GAME_SOURCE) ? G7076_PLEASE_WAIT_SAVING_NEW_ADVENTURE : G7072_PLEASE_WAIT_SAVING_GAME, C15_COLOR_WHITE, NULL, NULL);
#ifdef MEDIA772_SU1E
                        if ((G3805_FileHandle = Fcreate(L3938_ps_Game->FileName, C0_FILE_ATTRIBUTES)) < 0) {
                                goto loc_1328;
                        }
#endif
#ifdef MEDIA762_AU1E
                        G3805_FileHandle = NULL;
                        if (F1118_(G3802_pc_SaveFloppyDriveRootPath, "DungeonSave")) {
                                goto loc_1328;
                        }
                        G3805_FileHandle = F0776_FILE_Create(L3938_ps_Game->FileName);
                        if (G3805_FileHandle == NULL) {
                                goto loc_1328;
                        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        L3948_c_ = C1_TRUE;
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                        L3951_l_ = L3938_ps_Game->ChampionDataByteCount - sizeof(PARTY_INFO);
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        L3950_pc_ = (char*)F7023_Alloc(L3938_ps_Game->ChampionDataByteCount, MASK0x0400_MEMREQ);
#endif
#ifdef MEDIA762_AU1E
                        L3950_pc_ = (char*)F2095_AllocateMemory(L3938_ps_Game->ChampionDataByteCount);
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                        /* ANOMALY_FMT Filler content to compensate for too small code */
#ifdef MEDIA670_F31E_F31J
                        _inline(0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90);
#endif
                        /* ANOMALY_FMT Filler content */

                        F7065_ClearChampionPortraitPointersBeforeSavingGame(L3938_ps_Game); /* ANOMALY_FMT Unwanted optimization edi register usage */
                        /* T7052002:  edi saves value of L3950_pc_ for the next 3 later reuses */
#endif
#ifdef MEDIA762_AU1E
                        L6185_c_LoadPortraits = C1_TRUE;
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                        F7317_MAIN_CopyBytes_Alt((char*)L3938_ps_Game->Champions, (char*)L3950_pc_, MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, L3951_l_);
                        F7317_MAIN_CopyBytes_Alt((char*)&L3938_ps_Game->Party, (char*)((long)L3950_pc_ + L3951_l_), MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, sizeof(PARTY_INFO));
#endif
                        F0008_MAIN_ClearBytes(M772_CAST_PC(L3946_as_SaveParts), (long)96);
                        L3946_as_SaveParts[C0_SAVE_PART_GLOBAL_DATA].Address = (char*)&L3938_ps_Game->GlobalData;
                        L3946_as_SaveParts[C0_SAVE_PART_GLOBAL_DATA].ByteCount = sizeof(GLOBAL_DATA);
                        L3946_as_SaveParts[C1_SAVE_PART_ACTIVE_GROUP].Address = (char*)L3938_ps_Game->ActiveGroups;
                        L3946_as_SaveParts[C1_SAVE_PART_ACTIVE_GROUP].ByteCount = L3938_ps_Game->GlobalData.MaximumActiveGroupCount * sizeof(ACTIVE_GROUP);
#ifdef MEDIA772_SU1E
                        L3946_as_SaveParts[C2_SAVE_PART_PARTY].Address = (char*)&L3938_ps_Game->Party;
                        L3946_as_SaveParts[C2_SAVE_PART_PARTY].ByteCount = (long)sizeof(PARTY_INFO);
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                        L3946_as_SaveParts[C2_SAVE_PART_PARTY].Address = L3950_pc_;
                        L3946_as_SaveParts[C2_SAVE_PART_PARTY].ByteCount = L3938_ps_Game->ChampionDataByteCount;
#endif
                        L3946_as_SaveParts[C3_SAVE_PART_EVENTS].Address = (char*)L3938_ps_Game->Events;
                        L3946_as_SaveParts[C3_SAVE_PART_EVENTS].ByteCount = L3938_ps_Game->GlobalData.EventMaximumCount * sizeof(EVENT);
                        L3946_as_SaveParts[C4_SAVE_PART_TIMELINE].Address = (char*)L3938_ps_Game->TimeLine;
                        L3946_as_SaveParts[C4_SAVE_PART_TIMELINE].ByteCount = L3938_ps_Game->GlobalData.EventMaximumCount * sizeof(int16_t);
#ifdef MEDIA773_SU1E_AU1E
                        L3943_ps_ = (CSB_SAVE_HEADER*)&L3938_ps_Game->SaveHeader;
#endif
                        if (L3938_ps_Game->SaveHeaderFormat == C0x02_SAVE_HEADER_FORMAT_CHAOS_STRIKES_BACK) {
                                L3944_pui_ = L3943_ps_->Checksums;
                                L3945_pui_ = L3943_ps_->Keys;
#ifdef MEDIA772_SU1E
                                L3943_ps_->Useless = 2;
#endif
                        } else {
                                L3944_pui_ = L3938_ps_Game->SaveHeader.Checksums;
                                L3945_pui_ = L3938_ps_Game->SaveHeader.Keys;
#ifdef MEDIA772_SU1E
                                L3938_ps_Game->SaveHeader.Useless = 2;
#endif
                        }
                        L3952_i_ = 0;
                        while (L3952_i_ < 16) {
                                L3945_pui_[L3952_i_++] = M006_RANDOM(65536);
                        }
                        for (L3952_i_ = 0; L3952_i_ < 5; L3952_i_++) {
#ifdef MEDIA772_SU1E
                                L3944_pui_[L3952_i_] = F7056_F0418_SAVEUTIL_GetChecksum(L3946_as_SaveParts[L3952_i_].Address, L3945_pui_[L3952_i_], L3946_as_SaveParts[L3952_i_].ByteCount >> 1);
#endif
#ifdef MEDIA762_AU1E
                                L3944_pui_[L3952_i_] = F7056_F0418_SAVEUTIL_GetChecksum((unsigned int16_t*)L3946_as_SaveParts[L3952_i_].Address, L3945_pui_[L3952_i_], L3946_as_SaveParts[L3952_i_].ByteCount >> 1);
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                                L3944_pui_[L3952_i_] = F7056_F0418_SAVEUTIL_GetChecksum((unsigned int16_t*)L3946_as_SaveParts[L3952_i_].Address, L3945_pui_[L3952_i_], L3946_as_SaveParts[L3952_i_].ByteCount >> 1);
#endif
                        }
#ifdef MEDIA773_SU1E_AU1E
                        if (F7062_F0430_STARTEND_IsWriteObfuscatedSaveHeaderSuccessful(&L3938_ps_Game->SaveHeader, L3938_ps_Game->KeyWordIndex) == 0) {
                                goto loc_1328;
                        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        F7062_F0430_STARTEND_IsWriteObfuscatedSaveHeaderSuccessful(M773_CAST_PI(&L3938_ps_Game->SaveHeader), L3938_ps_Game->KeyWordIndex);
#endif
                        for (L3952_i_ = 0; L3952_i_ < 5; L3952_i_++) {
#ifdef MEDIA773_SU1E_AU1E
                                if (F7058_WriteSavePartWithChecksum(L3946_as_SaveParts[L3952_i_].Address, L3946_as_SaveParts[L3952_i_].ByteCount, L3945_pui_[L3952_i_], &L3941_i_) == 0) {
                                        goto loc_1328;
                                }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                                F7058_WriteSavePartWithChecksum(L3946_as_SaveParts[L3952_i_].Address, L3946_as_SaveParts[L3952_i_].ByteCount, L3945_pui_[L3952_i_], &L3941_i_);
#endif
                                if (L3941_i_ != L3944_pui_[L3952_i_]) {
#ifdef MEDIA772_SU1E
                                        F7101_sprintf(G3782_, "SAVE_GAME(): CHECKSUM %d != %d (KEY=%d)\n\r", L3944_pui_[L3952_i_], L3941_i_, L3945_pui_[L3952_i_]);
                                        Cconws(G3782_);
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                                        F7112_FallbackToLastProgramState(C0_FALSE, 23004, 0L);
#endif
                                }
                        }
#ifdef MEDIA762_AU1E
                        if (L3938_ps_Game->PortraitCount) {
                                if (!F0773_FILE_Write(G3805_FileHandle, L3938_ps_Game->PortraitByteCount * L3938_ps_Game->PortraitCount, L3938_ps_Game->Portraits)) {
                                        goto loc_1328;
                                }
                        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        if (L3938_ps_Game->PortraitCount != 0) {
                                F7137_FileOperation(G7004_SavedGameFileHandle, C0xF00B_OPERATION_WRITE, C16_FILEOP_BYTE_COUNT, L3938_ps_Game->PortraitByteCount * L3938_ps_Game->PortraitCount, C15_FILEOP_SET_BUFFER, (char*)L3938_ps_Game->Portraits, 0);
                        }
#endif
                        L3939_pi_Checksum = &L3942_i_Checksum;
                        L3942_i_Checksum = 0;
#ifdef MEDIA773_SU1E_AU1E
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.Header, L3939_pi_Checksum, sizeof(DUNGEON_HEADER)) == 0) {
                                goto loc_1328;
                        }
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.Maps, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->MapCount * sizeof(MAP)) == 0) {
                                goto loc_1328;
                        }
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.ColumnsCumulativeSquareThingCount, L3939_pi_Checksum, L3938_ps_Game->Dungeon.ColumnCount * sizeof(int16_t)) == 0) {
                                goto loc_1328;
                        }
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.SquareFirstThings, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->SquareFirstThingCount * sizeof(unsigned int16_t)) == 0) {
                                goto loc_1328;
                        }
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.TextData, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->TextDataWordCount * sizeof(int16_t)) == 0) {
                                goto loc_1328;
                        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.Header, L3939_pi_Checksum, sizeof(DUNGEON_HEADER));
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.Maps, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->MapCount * sizeof(MAP));
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.ColumnsCumulativeSquareThingCount, L3939_pi_Checksum, L3938_ps_Game->Dungeon.ColumnCount * sizeof(int16_t));
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.SquareFirstThings, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->SquareFirstThingCount * sizeof(unsigned int16_t));
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.TextData, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->TextDataWordCount * sizeof(int16_t));
#endif
                        for (L3952_i_ = 0; L3952_i_ < 16; L3952_i_++) {
#ifdef MEDIA772_SU1E
                                if (F7060_WriteDungeonPartWithChecksum(L3938_ps_Game->Dungeon.ThingData[L3952_i_], L3939_pi_Checksum, G0235_auc_Graphic559_ThingDataByteCount[L3952_i_] * L3938_ps_Game->Dungeon.Header->ThingCount[L3952_i_]) == 0) {
#endif
#ifdef MEDIA762_AU1E
                                if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.ThingData[L3952_i_], L3939_pi_Checksum, G0235_auc_Graphic559_ThingDataByteCount[L3952_i_] * L3938_ps_Game->Dungeon.Header->ThingCount[L3952_i_]) == 0) {
#endif
#ifdef MEDIA773_SU1E_AU1E
                                        goto loc_1328;
                                }
                        }
                        if (F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.RawMapData, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->RawMapDataByteCount) == 0) {
                                goto loc_1328;
                        }
                        if (F2000_FILE_Write(L3939_pi_Checksum, 2) == 0) {
                                loc_1328:
#endif
#ifdef MEDIA772_SU1E
                                F7072_GetDialogChoiceOrdinal((P3712_i_GameIndex != C0_GAME_SOURCE) ? G3794_ERROR_WHILE_SAVING_NEW_ADVENTURE : G7078_ERROR_WHILE_SAVING_GAME, C08_COLOR_RED, G7060_OK, &G2263_s_Input_Dialog1Choice);
                        }
                        Fclose(G3805_FileHandle);
        }
#endif
#ifdef MEDIA762_AU1E
                                F7074_DrawDialogErrorMessage((P3712_i_GameIndex != C0_GAME_SOURCE) ? G3794_ERROR_WHILE_SAVING_NEW_ADVENTURE : G7078_ERROR_WHILE_SAVING_GAME, C08_COLOR_RED);
                        }
                        F0771_FILE_Close(G3805_FileHandle);
                        Delay(200L);
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
                                F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.ThingData[L3952_i_], L3939_pi_Checksum, G0235_auc_Graphic559_ThingDataByteCount[L3952_i_] * L3938_ps_Game->Dungeon.Header->ThingCount[L3952_i_]);
                        }
                        F7060_WriteDungeonPartWithChecksum((unsigned char*)L3938_ps_Game->Dungeon.RawMapData, L3939_pi_Checksum, L3938_ps_Game->Dungeon.Header->RawMapDataByteCount);
                        F7137_FileOperation(G7004_SavedGameFileHandle, C0xF00B_OPERATION_WRITE, C16_FILEOP_BYTE_COUNT, (long)sizeof(int16_t), C15_FILEOP_SET_BUFFER, L3939_pi_Checksum, 0);
                        L3953_i_ = 0;
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        }
        loc_13DAD:
#endif
#ifdef MEDIA762_AU1E
        if (L6185_c_LoadPortraits) {
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
                F7066_LoadPortraits(L3938_ps_Game);
#endif
#ifdef MEDIA762_AU1E
        }
#endif
#ifdef MEDIA676_F31E_F31J_X31J_P31J_AU1E_AU2E_AU3E
        if (L3950_pc_) {
                T7052001: /* ANOMALY_FMT Label required to disable optimization */
                F7024_Free(L3950_pc_);
                L3950_pc_ = NULL;
        }
#endif
#ifdef MEDIA762_AU1E
        for (L3952_i_ = 0; L3952_i_ < L3938_ps_Game->GlobalData.PartyChampionCount; L3952_i_++) {
                L6184_pc_Portrait = (char*)F7067_GetChampionInfo(L3938_ps_Game, L3952_i_, C31_CHAMPION_INFO_PORTRAIT);
                F2104_CHAMPION_ConvertBitmapToAtariSTPlanar(L6184_pc_Portrait, 32, 29);
        }
#endif
#ifdef MEDIA773_SU1E_AU1E
        F1992_RequestUtilityDiskInDrive();
        F7071_();
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        if (G7004_SavedGameFileHandle != -1) {
                F7135_(G7004_SavedGameFileHandle);
                G7004_SavedGameFileHandle = -1;
        }
#ifdef MEDIA769_AU3E
        if (L6145_) {
#endif
#ifdef MEDIA768_AU2E_AU3E
                F2124_DecodeAllPortraitsAfterSaving(L3938_ps_Game);
#endif
#ifdef MEDIA769_AU3E
        }
#endif
        if ((L3953_i_ < 0) && L3948_c_) {
                F7071_();
                L3948_c_ = C0_FALSE;
                F7074_DrawDialogErrorMessage(G7078_ERROR_WHILE_SAVING_GAME, C08_COLOR_RED);
        }
        if (L3948_c_) {
                F7071_();
                L3948_c_ = C0_FALSE;
        }
        F7111_RestorePreviousProgramState();
#endif
}
#endif
