decompiled_code:
_BOOL8 __fastcall Colours_ReadConfig(__int64 a1, __int64 a2)
{
int v2; // edx
_BOOL8 result; // rax

v2 = COLOURS_NTSC_ReadConfig();
result = 1LL;
if ( !v2 )
result = (unsigned int)COLOURS_PAL_ReadConfig(a1, a2) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(__int64 a1, __int64 a2)
{
int v2; // edx
_BOOL8 result; // rax

v2 = COLOURS_NTSC_ReadConfig();
result = 1LL;
if ( !v2 )
result = (unsigned int)COLOURS_PAL_ReadConfig(a1, a2) != 0;
return result;
}

original_code:
int Colours_ReadConfig(char *option, char *ptr)
{
 if (COLOURS_NTSC_ReadConfig(option, ptr)) {
 }
 else if (COLOURS_PAL_ReadConfig(option, ptr)) {
 }
 else return 0;
 return 1;
}

variable_types:
option
char *


decompiled_code:
__int64 __fastcall Colours_Initialise(int *a1, __int64 a2)
{
int v4; // edx
int v5; // ebx
double v6; // xmm0_8
int v7; // ebp
const char **v8; // r12
double v9; // xmm0_8
double v10; // xmm0_8
bool v11; // zf
int v12; // eax
double *v13; // r10
double v14; // rdx
double v15; // r12
double v16; // rsi
double v17; // rdi
double v18; // rcx
unsigned int v20; // [rsp+8h] [rbp-40h]
int v21; // [rsp+Ch] [rbp-3Ch]

v4 = *a1;
if ( *a1 > 1 )
{
v21 = 1;
v5 = 1;
while ( 1 )
{
v7 = v5 + 1;
v8 = (const char **)(a2 + 8LL * v5);
if ( !strcmp(*v8, "-saturation") )
{
if ( v7 >= v4 )
goto LABEL_28;
COLOURS_PAL_setup[0] = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_NTSC_setup[0] = COLOURS_PAL_setup[0];
}
else if ( !strcmp(*v8, "-contrast") )
{
if ( v7 >= v4 )
goto LABEL_28;
v6 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[1] = v6;
COLOURS_NTSC_setup[1] = v6;
}
else if ( !strcmp(*v8, "-brightness") )
{
if ( v7 >= v4 )
goto LABEL_28;
v9 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[2] = v9;
COLOURS_NTSC_setup[2] = v9;
}
else if ( !strcmp(*v8, "-gamma") )
{
if ( v7 >= v4 )
goto LABEL_28;
v10 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[3] = v10;
COLOURS_NTSC_setup[3] = v10;
}
else
{
v11 = strcmp(*v8, "-colors-preset") == 0;
v20 = !v11;
if ( v11 )
{
if ( v7 >= v4 )
{
LABEL_28:
Log_print("Missing argument for '%s'", *v8);
return 0;
}
v12 = CFG_MatchTextParameter(*(_QWORD *)(a2 + 8LL * v7), &preset_cfg_strings, 3LL);
if ( v12 < 0 )
{
Log_print("Invalid value for -colors-preset");
return v20;
}
v13 = (double *)((char *)&presets + 40 * v12);
v15 = v13[1];
COLOURS_PAL_setup[0] = *v13;
v14 = COLOURS_PAL_setup[0];
COLOURS_PAL_setup[1] = v15;
v16 = v13[2];
v17 = v13[3];
COLOURS_PAL_setup[2] = v16;
COLOURS_PAL_setup[3] = v17;
v18 = v13[4];
COLOURS_PAL_setup[4] = v18;
COLOURS_NTSC_setup[0] = v14;
COLOURS_NTSC_setup[1] = v15;
COLOURS_NTSC_setup[2] = v16;
COLOURS_NTSC_setup[3] = v17;
COLOURS_NTSC_setup[4] = v18;
COLOURS_NTSC_SetPreset(0LL);
}
else
{
if ( !strcmp(*v8, "-help") )
{
Log_print("\t-colors-preset standard|deep-black|vibrant");
Log_print("\t Use one of predefined color adjustments");
Log_print("\t-saturation <num> Set color saturation");
Log_print("\t-contrast <num> Set contrast");
Log_print("\t-brightness <num> Set brightness");
Log_print("\t-gamma <num> Set color gamma factor");
}
*(_QWORD *)(a2 + 8LL * v21++) = *v8;
v7 = v5;
}
}
v5 = v7 + 1;
v4 = *a1;
if ( *a1 <= v7 + 1 )
goto LABEL_25;
}
}
v21 = 1;
LABEL_25:
*a1 = v21;
v20 = COLOURS_NTSC_Initialise(a1, a2);
if ( v20 )
{
v20 = COLOURS_PAL_Initialise(a1, a2);
if ( v20 )
{
UpdateModeDependentPointers(Atari800_tv_mode);
UpdatePalette();
v20 = 1;
}
}
return v20;
}

canonical_decompiled_code:
__int64 __fastcall func11(int *a1, __int64 a2)
{
int v4; // edx
int v5; // ebx
double v6; // xmm0_8
int v7; // ebp
const char **v8; // r12
double v9; // xmm0_8
double v10; // xmm0_8
bool v11; // zf
int v12; // eax
double *v13; // r10
double v14; // rdx
double v15; // r12
double v16; // rsi
double v17; // rdi
double v18; // rcx
unsigned int v20; // [rsp+8h] [rbp-40h]
int v21; // [rsp+Ch] [rbp-3Ch]

v4 = *a1;
if ( *a1 > 1 )
{
v21 = 1;
v5 = 1;
while ( 1 )
{
v7 = v5 + 1;
v8 = (const char **)(a2 + 8LL * v5);
if ( !strcmp(*v8, "-saturation") )
{
if ( v7 >= v4 )
goto LABEL_28;
COLOURS_PAL_setup[0] = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_NTSC_setup[0] = COLOURS_PAL_setup[0];
}
else if ( !strcmp(*v8, "-contrast") )
{
if ( v7 >= v4 )
goto LABEL_28;
v6 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[1] = v6;
COLOURS_NTSC_setup[1] = v6;
}
else if ( !strcmp(*v8, "-brightness") )
{
if ( v7 >= v4 )
goto LABEL_28;
v9 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[2] = v9;
COLOURS_NTSC_setup[2] = v9;
}
else if ( !strcmp(*v8, "-gamma") )
{
if ( v7 >= v4 )
goto LABEL_28;
v10 = atof(*(const char **)(a2 + 8LL * v7));
COLOURS_PAL_setup[3] = v10;
COLOURS_NTSC_setup[3] = v10;
}
else
{
v11 = strcmp(*v8, "-colors-preset") == 0;
v20 = !v11;
if ( v11 )
{
if ( v7 >= v4 )
{
LABEL_28:
Log_print("Missing argument for '%s'", *v8);
return 0;
}
v12 = CFG_MatchTextParameter(*(_QWORD *)(a2 + 8LL * v7), &preset_cfg_strings, 3LL);
if ( v12 < 0 )
{
Log_print("Invalid value for -colors-preset");
return v20;
}
v13 = (double *)((char *)&presets + 40 * v12);
v15 = v13[1];
COLOURS_PAL_setup[0] = *v13;
v14 = COLOURS_PAL_setup[0];
COLOURS_PAL_setup[1] = v15;
v16 = v13[2];
v17 = v13[3];
COLOURS_PAL_setup[2] = v16;
COLOURS_PAL_setup[3] = v17;
v18 = v13[4];
COLOURS_PAL_setup[4] = v18;
COLOURS_NTSC_setup[0] = v14;
COLOURS_NTSC_setup[1] = v15;
COLOURS_NTSC_setup[2] = v16;
COLOURS_NTSC_setup[3] = v17;
COLOURS_NTSC_setup[4] = v18;
COLOURS_NTSC_SetPreset(0LL);
}
else
{
if ( !strcmp(*v8, "-help") )
{
Log_print("\t-colors-preset standard|deep-black|vibrant");
Log_print("\t Use one of predefined color adjustments");
Log_print("\t-saturation <num> Set color saturation");
Log_print("\t-contrast <num> Set contrast");
Log_print("\t-brightness <num> Set brightness");
Log_print("\t-gamma <num> Set color gamma factor");
}
*(_QWORD *)(a2 + 8LL * v21++) = *v8;
v7 = v5;
}
}
v5 = v7 + 1;
v4 = *a1;
if ( *a1 <= v7 + 1 )
goto LABEL_25;
}
}
v21 = 1;
LABEL_25:
*a1 = v21;
v20 = COLOURS_NTSC_Initialise(a1, a2);
if ( v20 )
{
v20 = COLOURS_PAL_Initialise(a1, a2);
if ( v20 )
{
UpdateModeDependentPointers(Atari800_tv_mode);
func2();
v20 = 1;
}
}
return v20;
}

original_code:
int Colours_Initialise(int *argc, char *argv[])
{
 int i;
 int j;
 for (i = j = 1; i < *argc; i++) {
  int i_a = (i + 1 < *argc);
  int a_m = 0;
  if (strcmp(argv[i], "-saturation") == 0) {
   if (i_a)
    COLOURS_NTSC_setup.saturation = COLOURS_PAL_setup.saturation = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-contrast") == 0) {
   if (i_a)
    COLOURS_NTSC_setup.contrast = COLOURS_PAL_setup.contrast = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-brightness") == 0) {
   if (i_a)
    COLOURS_NTSC_setup.brightness = COLOURS_PAL_setup.brightness = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-gamma") == 0) {
   if (i_a)
    COLOURS_NTSC_setup.gamma = COLOURS_PAL_setup.gamma = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-colors-preset") == 0) {
   if (i_a) {
    int idx = CFG_MatchTextParameter(argv[++i], preset_cfg_strings, COLOURS_PRESET_SIZE);
    if (idx < 0) {
     Log_print("Invalid value for -colors-preset");
     return 0;
    }
    COLOURS_NTSC_setup = COLOURS_PAL_setup = presets[idx];
    COLOURS_NTSC_SetPreset(COLOURS_PRESET_STANDARD);
   } else a_m = 1;
  }
  else {
   if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-colors-preset standard|deep-black|vibrant");
    Log_print("\t                       Use one of predefined color adjustments");
    Log_print("\t-saturation <num>      Set color saturation");
    Log_print("\t-contrast <num>        Set contrast");
    Log_print("\t-brightness <num>      Set brightness");
    Log_print("\t-gamma <num>           Set color gamma factor");
   }
   argv[j++] = argv[i];
  }
  if (a_m) {
   Log_print("Missing argument for '%s'", argv[i]);
   return 0;
  }
 }
 *argc = j;
 if (!COLOURS_NTSC_Initialise(argc, argv) ||
     !COLOURS_PAL_Initialise(argc, argv))
  return 0;
 UpdateModeDependentPointers(Atari800_tv_mode);
 UpdatePalette();
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: i int

variable_types: j int

variable_types: idx int

return_type: int


GetGZErrorText

decompiled_code:
__int64 GetGZErrorText()
{
const char *v0; // rax
__int64 result; // rax
int *v2; // rax
char *v3; // rax

v0 = (const char *)gzerror(StateFile, &nFileError);
if ( nFileError == -1 )
{
Log_print("The following general file I/O error occurred:");
v2 = _errno_location();
v3 = strerror(*v2);
result = Log_print(v3);
}
else
{
Log_print("ZLIB returned the following error: %s", v0);
result = Log_print("State file I/O failed.");
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
const char *v0; // rax
__int64 result; // rax
int *v2; // rax
char *v3; // rax

v0 = (const char *)gzerror(StateFile, &nFileError);
if ( nFileError == -1 )
{
Log_print("The following general file I/O error occurred:");
v2 = _errno_location();
v3 = strerror(*v2);
result = Log_print(v3);
}
else
{
Log_print("ZLIB returned the following error: %s", v0);
result = Log_print("State file I/O failed.");
}
return result;
}

original_code:
static void GetGZErrorText(void)
{
 const char *error = gzerror(StateFile, &nFileError);
 if (nFileError == (-1)) {
  Log_print("The following general file I/O error occurred:");
  Log_print(strerror((*__errno_location ())));
  return;
 }
 Log_print("ZLIB returned the following error: %s", error);
 Log_print("State file I/O failed.");
}

canonical_original_code:
static void GetGZErrorText()
{
 char *error = gzerror(StateFile, &nFileError);
 if (nFileError == (-1)) {
  Log_print("The following general file I/O error occurred:");
  Log_print(strerror((*__errno_location ())));
  return;
 }
 Log_print("ZLIB returned the following error: %s", error);
 Log_print("State file I/O failed.");
}

variable_types:
error
char *

return_type: void


decompiled_code:
__int64 __fastcall StateSav_SaveUBYTE(__int64 a1, unsigned int a2)
{
__int64 result; // rax

result = StateFile;
if ( StateFile && !nFileError )
{
result = gzwrite(StateFile, a1, a2);
if ( !(_DWORD)result )
result = GetGZErrorText();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
__int64 result; // rax

result = StateFile;
if ( StateFile && !nFileError )
{
result = gzwrite(StateFile, a1, a2);
if ( !(_DWORD)result )
result = func0();
}
return result;
}

original_code:
void StateSav_SaveUBYTE(const unsigned char *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 if (gzwrite(StateFile, (const voidp) data, num) == 0)
  GetGZErrorText();
}

canonical_original_code:
void StateSav_SaveUBYTE(unsigned char *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 if (gzwrite(StateFile, (void *) data, num) == 0)
  GetGZErrorText();
}

variable_types:
data
unsigned char *

variable_types: num int


decompiled_code:
__int64 __fastcall StateSav_ReadUBYTE(__int64 a1, unsigned int a2)
{
__int64 result; // rax

result = StateFile;
if ( StateFile && !nFileError )
{
result = gzread(StateFile, a1, a2);
if ( !(_DWORD)result )
result = GetGZErrorText();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
__int64 result; // rax

result = StateFile;
if ( StateFile && !nFileError )
{
result = gzread(StateFile, a1, a2);
if ( !(_DWORD)result )
result = func0();
}
return result;
}

original_code:
void StateSav_ReadUBYTE(unsigned char *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 if (gzread(StateFile, data, num) == 0)
  GetGZErrorText();
}


decompiled_code:
void __fastcall StateSav_SaveUWORD(_WORD *a1, int a2)
{
_WORD *v2; // rbx
_WORD *v3; // r13
__int16 v4; // bp
_WORD *v5; // rbx
__int16 v6; // bp
__int16 v7; // bp
char v8[41]; // [rsp+1h] [rbp-29h] BYREF

if ( !StateFile )
return;
v2 = a1;
if ( nFileError )
return;
v3 = &a1[a2];
if ( a2 <= 0 )
return;
if ( ((2 * (_BYTE)a2) & 2) == 0 )
{
LABEL_8:
while ( 1 )
{
v5 = v2 + 1;
v6 = *(v5 - 1);
v8[0] = v6;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v8[0] = HIBYTE(v6);
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v2 = v5 + 1;
v7 = *(v2 - 1);
v8[0] = v7;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v8[0] = HIBYTE(v7);
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
if ( v2 == v3 )
return;
}
goto LABEL_14;
}
v2 = a1 + 1;
v4 = *a1;
v8[0] = *a1;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) || (v8[0] = HIBYTE(v4), !(unsigned int)gzwrite(StateFile, v8, 1LL)) )
{
LABEL_14:
GetGZErrorText();
return;
}
if ( v2 != v3 )
goto LABEL_8;
}

canonical_decompiled_code:
void __fastcall func3(_WORD *a1, int a2)
{
_WORD *v2; // rbx
_WORD *v3; // r13
__int16 v4; // bp
_WORD *v5; // rbx
__int16 v6; // bp
__int16 v7; // bp
char v8[41]; // [rsp+1h] [rbp-29h] BYREF

if ( !StateFile )
return;
v2 = a1;
if ( nFileError )
return;
v3 = &a1[a2];
if ( a2 <= 0 )
return;
if ( ((2 * (_BYTE)a2) & 2) == 0 )
{
LABEL_8:
while ( 1 )
{
v5 = v2 + 1;
v6 = *(v5 - 1);
v8[0] = v6;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v8[0] = HIBYTE(v6);
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v2 = v5 + 1;
v7 = *(v2 - 1);
v8[0] = v7;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
v8[0] = HIBYTE(v7);
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) )
break;
if ( v2 == v3 )
return;
}
goto LABEL_14;
}
v2 = a1 + 1;
v4 = *a1;
v8[0] = *a1;
if ( !(unsigned int)gzwrite(StateFile, v8, 1LL) || (v8[0] = HIBYTE(v4), !(unsigned int)gzwrite(StateFile, v8, 1LL)) )
{
LABEL_14:
func0();
return;
}
if ( v2 != v3 )
goto LABEL_8;
}

original_code:
void StateSav_SaveUWORD(const unsigned short *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned short temp;
  unsigned char byte;
  temp = *data++;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  num--;
 }
}

canonical_original_code:
void StateSav_SaveUWORD(unsigned short *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned short temp;
  unsigned char byte;
  temp = *data++;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  num--;
 }
}

variable_types:
data
unsigned short *

variable_types:
temp
unsigned short

variable_types:
byte
unsigned char


decompiled_code:
void __fastcall StateSav_ReadUWORD(_WORD *a1, int a2)
{
_WORD *v2; // rbx
_WORD *v3; // r12
_WORD *v4; // rbx
__int16 v5[13]; // [rsp+0h] [rbp-1Ah] BYREF

if ( !StateFile )
return;
v2 = a1;
if ( nFileError )
return;
v3 = &a1[a2];
if ( a2 <= 0 )
return;
if ( ((2 * (_BYTE)a2) & 2) == 0 )
{
LABEL_8:
while ( (unsigned int)gzread(StateFile, v5, 1LL) )
{
if ( !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
break;
v4 = v2 + 1;
*(v4 - 1) = v5[0];
if ( !(unsigned int)gzread(StateFile, v5, 1LL) || !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
break;
v2 = v4 + 1;
*(v2 - 1) = v5[0];
if ( v2 == v3 )
return;
}
goto LABEL_14;
}
if ( !(unsigned int)gzread(StateFile, v5, 1LL) || !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
{
LABEL_14:
GetGZErrorText();
return;
}
v2 = a1 + 1;
*a1 = v5[0];
if ( a1 + 1 != v3 )
goto LABEL_8;
}

canonical_decompiled_code:
void __fastcall func4(_WORD *a1, int a2)
{
_WORD *v2; // rbx
_WORD *v3; // r12
_WORD *v4; // rbx
__int16 v5[13]; // [rsp+0h] [rbp-1Ah] BYREF

if ( !StateFile )
return;
v2 = a1;
if ( nFileError )
return;
v3 = &a1[a2];
if ( a2 <= 0 )
return;
if ( ((2 * (_BYTE)a2) & 2) == 0 )
{
LABEL_8:
while ( (unsigned int)gzread(StateFile, v5, 1LL) )
{
if ( !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
break;
v4 = v2 + 1;
*(v4 - 1) = v5[0];
if ( !(unsigned int)gzread(StateFile, v5, 1LL) || !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
break;
v2 = v4 + 1;
*(v2 - 1) = v5[0];
if ( v2 == v3 )
return;
}
goto LABEL_14;
}
if ( !(unsigned int)gzread(StateFile, v5, 1LL) || !(unsigned int)gzread(StateFile, (char *)v5 + 1, 1LL) )
{
LABEL_14:
func0();
return;
}
v2 = a1 + 1;
*a1 = v5[0];
if ( a1 + 1 != v3 )
goto LABEL_8;
}

original_code:
void StateSav_ReadUWORD(unsigned short *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char byte1, byte2;
  if (gzread(StateFile, &byte1, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte2, 1) == 0) {
   GetGZErrorText();
   break;
  }
  *data++ = (byte2 << 8) | byte1;
  num--;
 }
}

canonical_original_code:
void StateSav_ReadUWORD(unsigned short *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char byte1; unsigned char byte2;
  if (gzread(StateFile, &byte1, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte2, 1) == 0) {
   GetGZErrorText();
   break;
  }
  *data++ = (byte2 << 8) | byte1;
  num--;
 }
}


decompiled_code:
void __fastcall StateSav_SaveINT(__int64 a1, int a2)
{
__int64 v2; // rbp
int v3; // er15
char v4; // r14
char v5[57]; // [rsp+1h] [rbp-39h] BYREF

if ( StateFile )
{
v2 = a1;
if ( !nFileError && a2 > 0 )
{
while ( 1 )
{
v2 += 4LL;
v3 = *(_DWORD *)(v2 - 4);
v4 = 0;
if ( v3 < 0 )
{
v3 = -v3;
v4 = 0x80;
}
v5[0] = v3;
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL)
|| (v5[0] = BYTE1(v3), !(unsigned int)gzwrite(StateFile, v5, 1LL)) )
{
GetGZErrorText();
return;
}
v5[0] = BYTE2(v3);
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL) )
break;
v5[0] = v4 | HIBYTE(v3) & 0x7F;
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL) )
break;
if ( v2 == a1 + 4LL * (unsigned int)a2 )
return;
}
GetGZErrorText();
}
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
__int64 v2; // rbp
int v3; // er15
char v4; // r14
char v5[57]; // [rsp+1h] [rbp-39h] BYREF

if ( StateFile )
{
v2 = a1;
if ( !nFileError && a2 > 0 )
{
while ( 1 )
{
v2 += 4LL;
v3 = *(_DWORD *)(v2 - 4);
v4 = 0;
if ( v3 < 0 )
{
v3 = -v3;
v4 = 0x80;
}
v5[0] = v3;
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL)
|| (v5[0] = BYTE1(v3), !(unsigned int)gzwrite(StateFile, v5, 1LL)) )
{
func0();
return;
}
v5[0] = BYTE2(v3);
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL) )
break;
v5[0] = v4 | HIBYTE(v3) & 0x7F;
if ( !(unsigned int)gzwrite(StateFile, v5, 1LL) )
break;
if ( v2 == a1 + 4LL * (unsigned int)a2 )
return;
}
func0();
}
}
}

original_code:
void StateSav_SaveINT(const int *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char signbit = 0;
  unsigned int temp;
  unsigned char byte;
  int temp0;
  temp0 = *data++;
  if (temp0 < 0) {
   temp0 = -temp0;
   signbit = 0x80;
  }
  temp = (unsigned int) temp0;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = (temp & 0x7f) | signbit;
  if (gzwrite(StateFile, (const voidp) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  num--;
 }
}

canonical_original_code:
void StateSav_SaveINT(int *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char signbit = 0;
  unsigned int temp;
  unsigned char byte;
  int temp0;
  temp0 = *data++;
  if (temp0 < 0) {
   temp0 = -temp0;
   signbit = 0x80;
  }
  temp = (unsigned int) temp0;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = temp & 0xff;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  temp >>= 8;
  byte = (temp & 0x7f) | signbit;
  if (gzwrite(StateFile, (void *) &byte, 1) == 0) {
   GetGZErrorText();
   break;
  }
  num--;
 }
}

variable_types:
data
int *

variable_types:
signbit
unsigned char

variable_types:
temp
unsigned int


decompiled_code:
void __fastcall StateSav_ReadINT(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 v3; // r12
int v4; // esi
unsigned __int8 v5; // [rsp+0h] [rbp-1Ch] BYREF
unsigned __int8 v6; // [rsp+1h] [rbp-1Bh] BYREF
unsigned __int8 v7; // [rsp+2h] [rbp-1Ah] BYREF
char v8[25]; // [rsp+3h] [rbp-19h] BYREF

if ( StateFile )
{
v2 = a1;
if ( !nFileError )
{
v3 = a1 + 4LL * (unsigned int)a2;
if ( a2 > 0 )
{
while ( 1 )
{
if ( !(unsigned int)gzread(StateFile, &v5, 1LL) || !(unsigned int)gzread(StateFile, &v6, 1LL) )
{
GetGZErrorText();
return;
}
if ( !(unsigned int)gzread(StateFile, &v7, 1LL) || !(unsigned int)gzread(StateFile, v8, 1LL) )
break;
v4 = ((v8[0] & 0x7F) << 24) | v5 | (v6 << 8) | (v7 << 16);
if ( v8[0] < 0 )
v4 = -v4;
v2 += 4LL;
*(_DWORD *)(v2 - 4) = v4;
if ( v2 == v3 )
return;
}
GetGZErrorText();
}
}
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 v3; // r12
int v4; // esi
unsigned __int8 v5; // [rsp+0h] [rbp-1Ch] BYREF
unsigned __int8 v6; // [rsp+1h] [rbp-1Bh] BYREF
unsigned __int8 v7; // [rsp+2h] [rbp-1Ah] BYREF
char v8[25]; // [rsp+3h] [rbp-19h] BYREF

if ( StateFile )
{
v2 = a1;
if ( !nFileError )
{
v3 = a1 + 4LL * (unsigned int)a2;
if ( a2 > 0 )
{
while ( 1 )
{
if ( !(unsigned int)gzread(StateFile, &v5, 1LL) || !(unsigned int)gzread(StateFile, &v6, 1LL) )
{
func0();
return;
}
if ( !(unsigned int)gzread(StateFile, &v7, 1LL) || !(unsigned int)gzread(StateFile, v8, 1LL) )
break;
v4 = ((v8[0] & 0x7F) << 24) | v5 | (v6 << 8) | (v7 << 16);
if ( v8[0] < 0 )
v4 = -v4;
v2 += 4LL;
*(_DWORD *)(v2 - 4) = v4;
if ( v2 == v3 )
return;
}
func0();
}
}
}
}

original_code:
void StateSav_ReadINT(int *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char signbit = 0;
  int temp;
  unsigned char byte1, byte2, byte3, byte4;
  if (gzread(StateFile, &byte1, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte2, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte3, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte4, 1) == 0) {
   GetGZErrorText();
   break;
  }
  signbit = byte4 & 0x80;
  byte4 &= 0x7f;
  temp = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1;
  if (signbit)
   temp = -temp;
  *data++ = temp;
  num--;
 }
}

canonical_original_code:
void StateSav_ReadINT(int *data, int num)
{
 if (!StateFile || nFileError != 0)
  return;
 while (num > 0) {
  unsigned char signbit = 0;
  int temp;
  unsigned char byte1; unsigned char byte2; unsigned char byte3; unsigned char byte4;
  if (gzread(StateFile, &byte1, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte2, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte3, 1) == 0) {
   GetGZErrorText();
   break;
  }
  if (gzread(StateFile, &byte4, 1) == 0) {
   GetGZErrorText();
   break;
  }
  signbit = byte4 & 0x80;
  byte4 &= 0x7f;
  temp = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1;
  if (signbit)
   temp = -temp;
  *data++ = temp;
  num--;
 }
}

variable_types: temp int


decompiled_code:
__int64 __fastcall StateSav_SaveFNAME(char *s1)
{
char *v1; // rbx
size_t v2; // rbp
__int64 v4[2]; // [rsp+0h] [rbp-1028h] BYREF
char v5[4080]; // [rsp+10h] [rbp-1018h] BYREF
unsigned __int16 v6[13]; // [rsp+100Eh] [rbp-1Ah] BYREF

v1 = s1;
v4[0] = 0LL;
v4[1] = 0LL;
memset(v5, 0, sizeof(v5));
if ( getcwd((char *)v4, 0x1000uLL) )
{
v2 = strlen((const char *)v4);
if ( !strncmp(s1, (const char *)v4, v2) )
v1 = &s1[v2 + 1];
}
v6[0] = strlen(v1);
StateSav_SaveUWORD(v6, 1);
return StateSav_SaveUBYTE((__int64)v1, v6[0]);
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s1)
{
char *v1; // rbx
size_t v2; // rbp
__int64 v4[2]; // [rsp+0h] [rbp-1028h] BYREF
char v5[4080]; // [rsp+10h] [rbp-1018h] BYREF
unsigned __int16 v6[13]; // [rsp+100Eh] [rbp-1Ah] BYREF

v1 = s1;
v4[0] = 0LL;
v4[1] = 0LL;
memset(v5, 0, sizeof(v5));
if ( getcwd((char *)v4, 0x1000uLL) )
{
v2 = strlen((const char *)v4);
if ( !strncmp(s1, (const char *)v4, v2) )
v1 = &s1[v2 + 1];
}
v6[0] = strlen(v1);
func3(v6, 1);
return func1((__int64)v1, v6[0]);
}

original_code:
void StateSav_SaveFNAME(const char *filename)
{
 unsigned short namelen;
 char dirname[4096]="";
 if (getcwd(dirname, 4096) != ((void *)0)) {
  if (strncmp(filename, dirname, strlen(dirname)) == 0)
   filename += strlen(dirname) + 1;
 }
 namelen = strlen(filename);
 StateSav_SaveUWORD(&namelen, 1);
 StateSav_SaveUBYTE((const unsigned char *) filename, namelen);
}

canonical_original_code:
void StateSav_SaveFNAME(char *filename)
{
 unsigned short namelen;
 char dirname[4096] = "";
 if (getcwd(dirname, 4096) != ((void *)0)) {
  if (strncmp(filename, dirname, strlen(dirname)) == 0)
   filename += strlen(dirname) + 1;
 }
 namelen = strlen(filename);
 StateSav_SaveUWORD(&namelen, 1);
 StateSav_SaveUBYTE((unsigned char *) filename, namelen);
}

variable_types:
filename
char *

variable_types:
namelen
unsigned short

variable_types:
dirname
char [4096]


decompiled_code:
__int64 __fastcall StateSav_SaveAtariState(const char *a1, __int64 a2, unsigned __int8 a3)
{
__int64 v4; // rdi
unsigned int v5; // ebp
char v7[12]; // [rsp+Ch] [rbp-2Ch] BYREF
int v8; // [rsp+18h] [rbp-20h] BYREF
char v9[25]; // [rsp+1Fh] [rbp-19h] BYREF

v7[0] = a3;
v9[0] = 6;
if ( StateFile )
{
((void (*)(void))gzclose)();
StateFile = 0LL;
}
nFileError = 0;
v4 = gzopen(a1, a2);
StateFile = v4;
if ( v4 )
{
v5 = gzwrite(v4, "ATARI800", 8LL);
if ( v5 )
{
StateSav_SaveUBYTE((__int64)v9, 1u);
StateSav_SaveUBYTE((__int64)v7, 1u);
Atari800_StateSave();
CARTRIDGE_StateSave();
SIO_StateSave();
ANTIC_StateSave();
CPU_StateSave((unsigned __int8)v7[0]);
GTIA_StateSave();
PIA_StateSave();
POKEY_StateSave();
v8 = 0;
StateSav_SaveINT((__int64)&v8, 1);
PBI_StateSave();
PBI_MIO_StateSave();
PBI_BB_StateSave();
v8 = 0;
StateSav_SaveINT((__int64)&v8, 1);
if ( (unsigned int)gzclose(StateFile) )
{
StateFile = 0LL;
v5 = 0;
}
else
{
StateFile = 0LL;
v5 = nFileError == 0;
}
}
else
{
GetGZErrorText();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
Log_print("Could not open %s for state save.", a1);
GetGZErrorText();
v5 = 0;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1, __int64 a2, unsigned __int8 a3)
{
__int64 v4; // rdi
unsigned int v5; // ebp
char v7[12]; // [rsp+Ch] [rbp-2Ch] BYREF
int v8; // [rsp+18h] [rbp-20h] BYREF
char v9[25]; // [rsp+1Fh] [rbp-19h] BYREF

v7[0] = a3;
v9[0] = 6;
if ( StateFile )
{
((void (*)(void))gzclose)();
StateFile = 0LL;
}
nFileError = 0;
v4 = gzopen(a1, a2);
StateFile = v4;
if ( v4 )
{
v5 = gzwrite(v4, "ATARI800", 8LL);
if ( v5 )
{
func1((__int64)v9, 1u);
func1((__int64)v7, 1u);
Atari800_StateSave();
CARTRIDGE_StateSave();
SIO_StateSave();
ANTIC_StateSave();
CPU_StateSave((unsigned __int8)v7[0]);
GTIA_StateSave();
PIA_StateSave();
POKEY_StateSave();
v8 = 0;
func5((__int64)&v8, 1);
PBI_StateSave();
PBI_MIO_StateSave();
PBI_BB_StateSave();
v8 = 0;
func5((__int64)&v8, 1);
if ( (unsigned int)gzclose(StateFile) )
{
StateFile = 0LL;
v5 = 0;
}
else
{
StateFile = 0LL;
v5 = nFileError == 0;
}
}
else
{
func0();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
Log_print("Could not open %s for state save.", a1);
func0();
v5 = 0;
}
return v5;
}

original_code:
int StateSav_SaveAtariState(const char *filename, const char *mode, unsigned char SaveVerbose)
{
 unsigned char StateVersion = 6;
 if (StateFile != ((void *)0)) {
  gzclose(StateFile);
  StateFile = ((void *)0);
 }
 nFileError = 0;
 StateFile = gzopen(filename, mode);
 if (StateFile == ((void *)0)) {
  Log_print("Could not open %s for state save.", filename);
  GetGZErrorText();
  return 0;
 }
 if (gzwrite(StateFile, (const voidp) "ATARI800", 8) == 0) {
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 StateSav_SaveUBYTE(&StateVersion, 1);
 StateSav_SaveUBYTE(&SaveVerbose, 1);
 Atari800_StateSave();
 CARTRIDGE_StateSave();
 SIO_StateSave();
 ANTIC_StateSave();
 CPU_StateSave(SaveVerbose);
 GTIA_StateSave();
 PIA_StateSave();
 POKEY_StateSave();
 {
  int local_xep80_enabled = 0;
  StateSav_SaveINT(&local_xep80_enabled, 1);
 }
 PBI_StateSave();
 PBI_MIO_StateSave();
 PBI_BB_StateSave();
 {
  int local_xld_enabled = 0;
  StateSav_SaveINT(&local_xld_enabled, 1);
 }
 if (gzclose(StateFile) != 0) {
  StateFile = ((void *)0);
  return 0;
 }
 StateFile = ((void *)0);
 if (nFileError != 0)
  return 0;
 return 1;
}

canonical_original_code:
int StateSav_SaveAtariState(char *filename, char *mode, unsigned char SaveVerbose)
{
 unsigned char StateVersion = 6;
 if (StateFile != ((void *)0)) {
  gzclose(StateFile);
  StateFile = ((void *)0);
 }
 nFileError = 0;
 StateFile = gzopen(filename, mode);
 if (StateFile == ((void *)0)) {
  Log_print("Could not open %s for state save.", filename);
  GetGZErrorText();
  return 0;
 }
 if (gzwrite(StateFile, (void *) "ATARI800", 8) == 0) {
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 StateSav_SaveUBYTE(&StateVersion, 1);
 StateSav_SaveUBYTE(&SaveVerbose, 1);
 Atari800_StateSave();
 CARTRIDGE_StateSave();
 SIO_StateSave();
 ANTIC_StateSave();
 CPU_StateSave(SaveVerbose);
 GTIA_StateSave();
 PIA_StateSave();
 POKEY_StateSave();
 {
  int local_xep80_enabled = 0;
  StateSav_SaveINT(&local_xep80_enabled, 1);
 }
 PBI_StateSave();
 PBI_MIO_StateSave();
 PBI_BB_StateSave();
 {
  int local_xld_enabled = 0;
  StateSav_SaveINT(&local_xld_enabled, 1);
 }
 if (gzclose(StateFile) != 0) {
  StateFile = ((void *)0);
  return 0;
 }
 StateFile = ((void *)0);
 if (nFileError != 0)
  return 0;
 return 1;
}

variable_types:
mode
char *

variable_types:
SaveVerbose
unsigned char

variable_types:
StateVersion
unsigned char


decompiled_code:
__int64 __fastcall StateSav_ReadAtariState(const char *a1, __int64 a2)
{
__int64 v3; // rdi
unsigned int v4; // ebx
int v6; // [rsp+Ch] [rbp-2Ch] BYREF
int v7; // [rsp+10h] [rbp-28h] BYREF
unsigned __int8 v8; // [rsp+16h] [rbp-22h] BYREF
unsigned __int8 v9; // [rsp+17h] [rbp-21h] BYREF
char s1[32]; // [rsp+18h] [rbp-20h] BYREF

v9 = 0;
v8 = 0;
if ( StateFile )
{
gzclose(StateFile);
StateFile = 0LL;
}
nFileError = 0;
v3 = gzopen(a1, a2);
StateFile = v3;
if ( v3 )
{
v4 = gzread(v3, s1, 8LL);
if ( v4 )
{
v4 = memcmp(s1, "ATARI800", 8uLL);
if ( v4 )
{
Log_print("This is not an Atari800 state save file.");
gzclose(StateFile);
StateFile = 0LL;
v4 = 0;
}
else if ( (unsigned int)gzread(StateFile, &v9, 1LL) && (unsigned int)gzread(StateFile, &v8, 1LL) )
{
if ( v9 <= 2u )
{
Log_print("Cannot read this state file because it is an incompatible version.");
gzclose(StateFile);
StateFile = 0LL;
}
else
{
Atari800_StateRead();
if ( v9 > 3u )
{
CARTRIDGE_StateRead();
SIO_StateRead();
}
ANTIC_StateRead();
CPU_StateRead(v8, v9);
GTIA_StateRead();
PIA_StateRead();
POKEY_StateRead();
if ( v9 > 5u )
{
StateSav_ReadINT((__int64)&v6, 1);
if ( v6 )
{
Log_print("Cannot read this state file because this version does not support XEP80.");
gzclose(StateFile);
StateFile = 0LL;
return v4;
}
PBI_StateRead();
PBI_MIO_StateRead();
PBI_BB_StateRead();
StateSav_ReadINT((__int64)&v7, 1);
if ( v7 )
{
Log_print("Cannot read this state file because this version does not support the 1400XL/1450XLD.");
gzclose(StateFile);
StateFile = 0LL;
return v4;
}
}
gzclose(StateFile);
StateFile = 0LL;
v4 = nFileError == 0;
}
}
else
{
Log_print("Failed read from Atari state file.");
GetGZErrorText();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
GetGZErrorText();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
Log_print("Could not open %s for state read.", a1);
GetGZErrorText();
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, __int64 a2)
{
__int64 v3; // rdi
unsigned int v4; // ebx
int v6; // [rsp+Ch] [rbp-2Ch] BYREF
int v7; // [rsp+10h] [rbp-28h] BYREF
unsigned __int8 v8; // [rsp+16h] [rbp-22h] BYREF
unsigned __int8 v9; // [rsp+17h] [rbp-21h] BYREF
char s1[32]; // [rsp+18h] [rbp-20h] BYREF

v9 = 0;
v8 = 0;
if ( StateFile )
{
gzclose(StateFile);
StateFile = 0LL;
}
nFileError = 0;
v3 = gzopen(a1, a2);
StateFile = v3;
if ( v3 )
{
v4 = gzread(v3, s1, 8LL);
if ( v4 )
{
v4 = memcmp(s1, "ATARI800", 8uLL);
if ( v4 )
{
Log_print("This is not an Atari800 state save file.");
gzclose(StateFile);
StateFile = 0LL;
v4 = 0;
}
else if ( (unsigned int)gzread(StateFile, &v9, 1LL) && (unsigned int)gzread(StateFile, &v8, 1LL) )
{
if ( v9 <= 2u )
{
Log_print("Cannot read this state file because it is an incompatible version.");
gzclose(StateFile);
StateFile = 0LL;
}
else
{
Atari800_StateRead();
if ( v9 > 3u )
{
CARTRIDGE_StateRead();
SIO_StateRead();
}
ANTIC_StateRead();
CPU_StateRead(v8, v9);
GTIA_StateRead();
PIA_StateRead();
POKEY_StateRead();
if ( v9 > 5u )
{
func6((__int64)&v6, 1);
if ( v6 )
{
Log_print("Cannot read this state file because this version does not support XEP80.");
gzclose(StateFile);
StateFile = 0LL;
return v4;
}
PBI_StateRead();
PBI_MIO_StateRead();
PBI_BB_StateRead();
func6((__int64)&v7, 1);
if ( v7 )
{
Log_print("Cannot read this state file because this version does not support the 1400XL/1450XLD.");
gzclose(StateFile);
StateFile = 0LL;
return v4;
}
}
gzclose(StateFile);
StateFile = 0LL;
v4 = nFileError == 0;
}
}
else
{
Log_print("Failed read from Atari state file.");
func0();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
func0();
gzclose(StateFile);
StateFile = 0LL;
}
}
else
{
Log_print("Could not open %s for state read.", a1);
func0();
v4 = 0;
}
return v4;
}

original_code:
int StateSav_ReadAtariState(const char *filename, const char *mode)
{
 char header_string[8];
 unsigned char StateVersion = 0;
 unsigned char SaveVerbose = 0;
 if (StateFile != ((void *)0)) {
  gzclose(StateFile);
  StateFile = ((void *)0);
 }
 nFileError = 0;
 StateFile = gzopen(filename, mode);
 if (StateFile == ((void *)0)) {
  Log_print("Could not open %s for state read.", filename);
  GetGZErrorText();
  return 0;
 }
 if (gzread(StateFile, header_string, 8) == 0) {
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (memcmp(header_string, "ATARI800", 8) != 0) {
  Log_print("This is not an Atari800 state save file.");
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (gzread(StateFile, &StateVersion, 1) == 0
  || gzread(StateFile, &SaveVerbose, 1) == 0) {
  Log_print("Failed read from Atari state file.");
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (StateVersion != 6 && StateVersion < 3) {
  Log_print("Cannot read this state file because it is an incompatible version.");
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 Atari800_StateRead();
 if (StateVersion >= 4) {
  CARTRIDGE_StateRead();
  SIO_StateRead();
 }
 ANTIC_StateRead();
 CPU_StateRead(SaveVerbose, StateVersion);
 GTIA_StateRead();
 PIA_StateRead();
 POKEY_StateRead();
 if (StateVersion >= 6) {
  int local_xep80_enabled;
  StateSav_ReadINT(&local_xep80_enabled,1);
  if (local_xep80_enabled) {
   Log_print("Cannot read this state file because this version does not support XEP80.");
   gzclose(StateFile);
   StateFile = ((void *)0);
   return 0;
  }
  PBI_StateRead();
  PBI_MIO_StateRead();
  PBI_BB_StateRead();
  {
   int local_xld_enabled;
   StateSav_ReadINT(&local_xld_enabled,1);
   if (local_xld_enabled) {
    Log_print("Cannot read this state file because this version does not support the 1400XL/1450XLD.");
    gzclose(StateFile);
    StateFile = ((void *)0);
    return 0;
   }
  }
 }
 gzclose(StateFile);
 StateFile = ((void *)0);
 if (nFileError != 0)
  return 0;
 return 1;
}

canonical_original_code:
int StateSav_ReadAtariState(char *filename, char *mode)
{
 char header_string[8];
 unsigned char StateVersion = 0;
 unsigned char SaveVerbose = 0;
 if (StateFile != ((void *)0)) {
  gzclose(StateFile);
  StateFile = ((void *)0);
 }
 nFileError = 0;
 StateFile = gzopen(filename, mode);
 if (StateFile == ((void *)0)) {
  Log_print("Could not open %s for state read.", filename);
  GetGZErrorText();
  return 0;
 }
 if (gzread(StateFile, header_string, 8) == 0) {
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (memcmp(header_string, "ATARI800", 8) != 0) {
  Log_print("This is not an Atari800 state save file.");
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (gzread(StateFile, &StateVersion, 1) == 0
  || gzread(StateFile, &SaveVerbose, 1) == 0) {
  Log_print("Failed read from Atari state file.");
  GetGZErrorText();
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 if (StateVersion != 6 && StateVersion < 3) {
  Log_print("Cannot read this state file because it is an incompatible version.");
  gzclose(StateFile);
  StateFile = ((void *)0);
  return 0;
 }
 Atari800_StateRead();
 if (StateVersion >= 4) {
  CARTRIDGE_StateRead();
  SIO_StateRead();
 }
 ANTIC_StateRead();
 CPU_StateRead(SaveVerbose, StateVersion);
 GTIA_StateRead();
 PIA_StateRead();
 POKEY_StateRead();
 if (StateVersion >= 6) {
  int local_xep80_enabled;
  StateSav_ReadINT(&local_xep80_enabled,1);
  if (local_xep80_enabled) {
   Log_print("Cannot read this state file because this version does not support XEP80.");
   gzclose(StateFile);
   StateFile = ((void *)0);
   return 0;
  }
  PBI_StateRead();
  PBI_MIO_StateRead();
  PBI_BB_StateRead();
  {
   int local_xld_enabled;
   StateSav_ReadINT(&local_xld_enabled,1);
   if (local_xld_enabled) {
    Log_print("Cannot read this state file because this version does not support the 1400XL/1450XLD.");
    gzclose(StateFile);
    StateFile = ((void *)0);
    return 0;
   }
  }
 }
 gzclose(StateFile);
 StateFile = ((void *)0);
 if (nFileError != 0)
  return 0;
 return 1;
}


decompiled_code:
__int64 Sound_Initialise()
{
output_channels = 2;
pokey_chips = 2;
return POKEYSND_Init(1789790LL, 22050LL, 2LL, 0LL);
}

canonical_decompiled_code:
__int64 func0()
{
output_channels = 2;
pokey_chips = 2;
return POKEYSND_Init(1789790LL, 22050LL, 2LL, 0LL);
}

original_code:
void Sound_Initialise(int *argc, char *argv[])
{
 output_channels = 2;
 pokey_chips = 2;
 POKEYSND_Init(1789790, dsprate, pokey_chips, 0);
}

variable_types:
argc
int *

variable_types:
argv
char *[]


decompiled_code:
void Sound_Continue()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void Sound_Continue(void)
{
}

canonical_original_code:
void Sound_Continue()
{
}


decompiled_code:
void Sound_Exit()
{
if ( sound_enabled )
sound_enabled = 0;
}

canonical_decompiled_code:
void func3()
{
if ( sound_enabled )
sound_enabled = 0;
}

original_code:
void Sound_Exit(void)
{
 if (sound_enabled) {
  sound_enabled = 0;
 }
}

canonical_original_code:
void Sound_Exit()
{
 if (sound_enabled) {
  sound_enabled = 0;
 }
}


original_code:
void Sound_Update(void)
{
 static unsigned char buffer[4096];
 unsigned int len;
 if (!sound_enabled || Atari800_turbo)
  return;
 len = dsprate / (Atari800_tv_mode == 262 ? 60 : 50) * pokey_chips;
 POKEYSND_Process(buffer, len);
 if (output_channels == 2) {
  if (pokey_chips == 1) {
   int i;
   for (i = len-1; i >= 0; i--) {
    int pos = i * 2;
    buffer[pos] = buffer[pos+1] = buffer[i];
   }
  }
  else if (! POKEYSND_stereo_enabled) {
   int i;
   for (i = 0; i < len; i+=2) {
    buffer[i+1] = buffer[i];
   }
  }
 }
 else {
  if (pokey_chips == 2) {
   int i;
   for (i = 0; i < len/2; i++) {
    int pos = i * 2;
    buffer[i] = (POKEYSND_stereo_enabled ? buffer[pos]/2 + buffer[pos+1]/2 :
        buffer[pos]);
   }
  }
 }
 ATR800WriteSoundBuffer(buffer, len);
}

canonical_original_code:
void Sound_Update()
{
 static unsigned char buffer[4096];
 unsigned int len;
 if (!sound_enabled || Atari800_turbo)
  return;
 len = dsprate / (Atari800_tv_mode == 262 ? 60 : 50) * pokey_chips;
 POKEYSND_Process(buffer, len);
 if (output_channels == 2) {
  if (pokey_chips == 1) {
   int i;
   for (i = len-1; i >= 0; i--) {
    int pos = i * 2;
    buffer[pos] = buffer[pos+1] = buffer[i];
   }
  }
  else if (! POKEYSND_stereo_enabled) {
   int i;
   for (i = 0; i < len; i+=2) {
    buffer[i+1] = buffer[i];
   }
  }
 }
 else {
  if (pokey_chips == 2) {
   int i;
   for (i = 0; i < len/2; i++) {
    int pos = i * 2;
    buffer[i] = (POKEYSND_stereo_enabled ? buffer[pos]/2 + buffer[pos+1]/2 :
        buffer[pos]);
   }
  }
 }
 ATR800WriteSoundBuffer(buffer, len);
}

variable_types:
buffer
unsigned char [4096]

variable_types:
len
unsigned int

variable_types: i int

variable_types: pos int


decompiled_code:
__int64 PIA_Initialise()
{
PIA_PACTL = 63;
PIA_PBCTL = 63;
PIA_PORTA = -1;
PIA_PORTB = -1;
PIA_PORTA_mask = -1;
PIA_PORTB_mask = -1;
PIA_PORT_input[0] = -1;
PIA_PORT_input[1] = -1;
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
PIA_PACTL = 63;
PIA_PBCTL = 63;
PIA_PORTA = -1;
PIA_PORTB = -1;
PIA_PORTA_mask = -1;
PIA_PORTB_mask = -1;
PIA_PORT_input[0] = -1;
PIA_PORT_input[1] = -1;
return 1LL;
}

original_code:
int PIA_Initialise(int *argc, char *argv[])
{
 PIA_PACTL = 0x3f;
 PIA_PBCTL = 0x3f;
 PIA_PORTA = 0xff;
 PIA_PORTB = 0xff;
 PIA_PORTA_mask = 0xff;
 PIA_PORTB_mask = 0xff;
 PIA_PORT_input[0] = 0xff;
 PIA_PORT_input[1] = 0xff;
 return 1;
}


decompiled_code:
void *PIA_Reset()
{
void *result; // rax

result = &PIA_PORTA;
PIA_PORTA = -1;
if ( Atari800_machine_type == 2 )
result = (void *)MEMORY_HandlePORTB(255LL, (unsigned __int8)(PIA_PORTB_mask | PIA_PORTB));
PIA_PORTB = -1;
return result;
}

canonical_decompiled_code:
void *func1()
{
void *result; // rax

result = &PIA_PORTA;
PIA_PORTA = -1;
if ( Atari800_machine_type == 2 )
result = (void *)MEMORY_HandlePORTB(255LL, (unsigned __int8)(PIA_PORTB_mask | PIA_PORTB));
PIA_PORTB = -1;
return result;
}

original_code:
void PIA_Reset(void)
{
 PIA_PORTA = 0xff;
 if (Atari800_machine_type == 2) {
  MEMORY_HandlePORTB(0xff, (unsigned char) (PIA_PORTB | PIA_PORTB_mask));
 }
 PIA_PORTB = 0xff;
}

canonical_original_code:
void PIA_Reset()
{
 PIA_PORTA = 0xff;
 if (Atari800_machine_type == 2) {
  MEMORY_HandlePORTB(0xff, (unsigned char) (PIA_PORTB | PIA_PORTB_mask));
 }
 PIA_PORTB = 0xff;
}


decompiled_code:
char __fastcall PIA_GetByte(char a1)
{
unsigned __int16 v1; // di
char result; // al

v1 = a1 & 3;
if ( v1 == 2 )
return PIA_PACTL & 0x3F;
if ( v1 > 2u )
return PIA_PBCTL & 0x3F;
if ( v1 )
{
if ( (PIA_PBCTL & 4) != 0 )
{
if ( Atari800_machine_type == 2 )
result = PIA_PORTB_mask | PIA_PORTB;
else
result = PIA_PORT_input[1] & (PIA_PORTB_mask | PIA_PORTB);
}
else
{
result = ~PIA_PORTB_mask;
}
}
else if ( (PIA_PACTL & 4) != 0 )
{
result = PIA_PORT_input[0] & (PIA_PORTA_mask | PIA_PORTA);
}
else
{
result = ~PIA_PORTA_mask;
}
return result;
}

canonical_decompiled_code:
char __fastcall func2(char a1)
{
unsigned __int16 v1; // di
char result; // al

v1 = a1 & 3;
if ( v1 == 2 )
return PIA_PACTL & 0x3F;
if ( v1 > 2u )
return PIA_PBCTL & 0x3F;
if ( v1 )
{
if ( (PIA_PBCTL & 4) != 0 )
{
if ( Atari800_machine_type == 2 )
result = PIA_PORTB_mask | PIA_PORTB;
else
result = PIA_PORT_input[1] & (PIA_PORTB_mask | PIA_PORTB);
}
else
{
result = ~PIA_PORTB_mask;
}
}
else if ( (PIA_PACTL & 4) != 0 )
{
result = PIA_PORT_input[0] & (PIA_PORTA_mask | PIA_PORTA);
}
else
{
result = ~PIA_PORTA_mask;
}
return result;
}

original_code:
unsigned char PIA_GetByte(unsigned short addr)
{
 switch (addr & 0x03) {
 case 0x02:
  return PIA_PACTL & 0x3f;
 case 0x03:
  return PIA_PBCTL & 0x3f;
 case 0x00:
  if ((PIA_PACTL & 0x04) == 0) {
   return ~PIA_PORTA_mask;
  }
  else {
   return PIA_PORT_input[0] & (PIA_PORTA | PIA_PORTA_mask);
  }
 case 0x01:
  if ((PIA_PBCTL & 0x04) == 0) {
   return ~PIA_PORTB_mask;
  }
  else {
   if (Atari800_machine_type == 2) {
    return PIA_PORTB | PIA_PORTB_mask;
   }
   else {
    return PIA_PORT_input[1] & (PIA_PORTB | PIA_PORTB_mask);
   }
  }
 }
 return 0xff;
}

variable_types:
addr
unsigned short

return_type: unsigned char


decompiled_code:
char __fastcall PIA_PutByte(char a1, unsigned __int8 a2)
{
unsigned __int16 v2; // di
void *v3; // rax

v2 = a1 & 3;
if ( v2 == 2 )
{
SIO_TapeMotor(((a2 >> 3) ^ 1) & 1);
v3 = &PIA_PACTL;
PIA_PACTL = a2;
}
else if ( v2 > 2u )
{
LOBYTE(v3) = PIA_PBCTL ^ a2;
if ( ((PIA_PBCTL ^ a2) & 8) != 0 )
LOBYTE(v3) = SIO_SwitchCommandFrame(((a2 >> 3) ^ 1) & 1);
PIA_PBCTL = a2;
}
else if ( v2 )
{
if ( Atari800_machine_type == 2 )
{
if ( (PIA_PBCTL & 4) != 0 )
{
LOBYTE(v3) = MEMORY_HandlePORTB(
(unsigned __int8)(a2 | PIA_PORTB_mask),
(unsigned __int8)(PIA_PORTB | PIA_PORTB_mask));
PIA_PORTB = a2;
}
else
{
LOBYTE(v3) = MEMORY_HandlePORTB(
(unsigned __int8)(~a2 | PIA_PORTB),
(unsigned __int8)(PIA_PORTB_mask | PIA_PORTB));
PIA_PORTB_mask = ~a2;
}
}
else if ( (PIA_PBCTL & 4) != 0 )
{
v3 = &PIA_PORTB;
PIA_PORTB = a2;
}
else
{
PIA_PORTB_mask = ~a2;
}
}
else
{
if ( (PIA_PACTL & 4) != 0 )
PIA_PORTA = a2;
else
PIA_PORTA_mask = ~a2;
LOBYTE(v3) = INPUT_SelectMultiJoy((unsigned __int8)(PIA_PORTA_mask | PIA_PORTA) >> 4);
}
return (char)v3;
}

canonical_decompiled_code:
char __fastcall func3(char a1, unsigned __int8 a2)
{
unsigned __int16 v2; // di
void *v3; // rax

v2 = a1 & 3;
if ( v2 == 2 )
{
SIO_TapeMotor(((a2 >> 3) ^ 1) & 1);
v3 = &PIA_PACTL;
PIA_PACTL = a2;
}
else if ( v2 > 2u )
{
LOBYTE(v3) = PIA_PBCTL ^ a2;
if ( ((PIA_PBCTL ^ a2) & 8) != 0 )
LOBYTE(v3) = SIO_SwitchCommandFrame(((a2 >> 3) ^ 1) & 1);
PIA_PBCTL = a2;
}
else if ( v2 )
{
if ( Atari800_machine_type == 2 )
{
if ( (PIA_PBCTL & 4) != 0 )
{
LOBYTE(v3) = MEMORY_HandlePORTB(
(unsigned __int8)(a2 | PIA_PORTB_mask),
(unsigned __int8)(PIA_PORTB | PIA_PORTB_mask));
PIA_PORTB = a2;
}
else
{
LOBYTE(v3) = MEMORY_HandlePORTB(
(unsigned __int8)(~a2 | PIA_PORTB),
(unsigned __int8)(PIA_PORTB_mask | PIA_PORTB));
PIA_PORTB_mask = ~a2;
}
}
else if ( (PIA_PBCTL & 4) != 0 )
{
v3 = &PIA_PORTB;
PIA_PORTB = a2;
}
else
{
PIA_PORTB_mask = ~a2;
}
}
else
{
if ( (PIA_PACTL & 4) != 0 )
PIA_PORTA = a2;
else
PIA_PORTA_mask = ~a2;
LOBYTE(v3) = INPUT_SelectMultiJoy((unsigned __int8)(PIA_PORTA_mask | PIA_PORTA) >> 4);
}
return (char)v3;
}

original_code:
void PIA_PutByte(unsigned short addr, unsigned char byte)
{
 switch (addr & 0x03) {
 case 0x02:
  SIO_TapeMotor(byte & 0x08 ? 0 : 1);
  PIA_PACTL = byte;
  break;
 case 0x03:
  if ((PIA_PBCTL ^ byte) & 0x08) {
   SIO_SwitchCommandFrame(byte & 0x08 ? 0 : 1);
  }
  PIA_PBCTL = byte;
  break;
 case 0x00:
  if ((PIA_PACTL & 0x04) == 0) {
    PIA_PORTA_mask = ~byte;
  }
  else {
   PIA_PORTA = byte;
  }
  INPUT_SelectMultiJoy((PIA_PORTA | PIA_PORTA_mask) >> 4);
  break;
 case 0x01:
  if (Atari800_machine_type == 2) {
   if ((PIA_PBCTL & 0x04) == 0) {
    MEMORY_HandlePORTB((unsigned char) (PIA_PORTB | ~byte), (unsigned char) (PIA_PORTB | PIA_PORTB_mask));
    PIA_PORTB_mask = ~byte;
   }
   else {
    MEMORY_HandlePORTB((unsigned char) (byte | PIA_PORTB_mask), (unsigned char) (PIA_PORTB | PIA_PORTB_mask));
    PIA_PORTB = byte;
   }
  }
  else {
   if ((PIA_PBCTL & 0x04) == 0) {
    PIA_PORTB_mask = ~byte;
   }
   else {
    PIA_PORTB = byte;
   }
  }
  break;
 }
}

variable_types:
byte
unsigned char


decompiled_code:
__int64 PIA_StateSave()
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
if ( MEMORY_ram_size == 320 )
{
v1[0] = 1;
}
else if ( MEMORY_ram_size == 321 )
{
v1[0] = 2;
}
StateSav_SaveUBYTE(&PIA_PACTL, 1LL);
StateSav_SaveUBYTE(&PIA_PBCTL, 1LL);
StateSav_SaveUBYTE(&PIA_PORTA, 1LL);
StateSav_SaveUBYTE(&PIA_PORTB, 1LL);
StateSav_SaveINT(&MEMORY_xe_bank, 1LL);
StateSav_SaveINT(&MEMORY_selftest_enabled, 1LL);
StateSav_SaveINT(v1, 1LL);
StateSav_SaveINT(&MEMORY_cartA0BF_enabled, 1LL);
StateSav_SaveUBYTE(&PIA_PORTA_mask, 1LL);
return StateSav_SaveUBYTE(&PIA_PORTB_mask, 1LL);
}

canonical_decompiled_code:
__int64 func4()
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
if ( MEMORY_ram_size == 320 )
{
v1[0] = 1;
}
else if ( MEMORY_ram_size == 321 )
{
v1[0] = 2;
}
StateSav_SaveUBYTE(&PIA_PACTL, 1LL);
StateSav_SaveUBYTE(&PIA_PBCTL, 1LL);
StateSav_SaveUBYTE(&PIA_PORTA, 1LL);
StateSav_SaveUBYTE(&PIA_PORTB, 1LL);
StateSav_SaveINT(&MEMORY_xe_bank, 1LL);
StateSav_SaveINT(&MEMORY_selftest_enabled, 1LL);
StateSav_SaveINT(v1, 1LL);
StateSav_SaveINT(&MEMORY_cartA0BF_enabled, 1LL);
StateSav_SaveUBYTE(&PIA_PORTA_mask, 1LL);
return StateSav_SaveUBYTE(&PIA_PORTB_mask, 1LL);
}

original_code:
void PIA_StateSave(void)
{
 int Ram256 = 0;
 if (MEMORY_ram_size == 320)
  Ram256 = 1;
 else if (MEMORY_ram_size == 321)
  Ram256 = 2;
 StateSav_SaveUBYTE( &PIA_PACTL, 1 );
 StateSav_SaveUBYTE( &PIA_PBCTL, 1 );
 StateSav_SaveUBYTE( &PIA_PORTA, 1 );
 StateSav_SaveUBYTE( &PIA_PORTB, 1 );
 StateSav_SaveINT( &MEMORY_xe_bank, 1 );
 StateSav_SaveINT( &MEMORY_selftest_enabled, 1 );
 StateSav_SaveINT( &Ram256, 1 );
 StateSav_SaveINT( &MEMORY_cartA0BF_enabled, 1 );
 StateSav_SaveUBYTE( &PIA_PORTA_mask, 1 );
 StateSav_SaveUBYTE( &PIA_PORTB_mask, 1 );
}

canonical_original_code:
void PIA_StateSave()
{
 int Ram256 = 0;
 if (MEMORY_ram_size == 320)
  Ram256 = 1;
 else if (MEMORY_ram_size == 321)
  Ram256 = 2;
 StateSav_SaveUBYTE( &PIA_PACTL, 1 );
 StateSav_SaveUBYTE( &PIA_PBCTL, 1 );
 StateSav_SaveUBYTE( &PIA_PORTA, 1 );
 StateSav_SaveUBYTE( &PIA_PORTB, 1 );
 StateSav_SaveINT( &MEMORY_xe_bank, 1 );
 StateSav_SaveINT( &MEMORY_selftest_enabled, 1 );
 StateSav_SaveINT( &Ram256, 1 );
 StateSav_SaveINT( &MEMORY_cartA0BF_enabled, 1 );
 StateSav_SaveUBYTE( &PIA_PORTA_mask, 1 );
 StateSav_SaveUBYTE( &PIA_PORTB_mask, 1 );
}


decompiled_code:
__int64 PIA_StateRead()
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
StateSav_ReadUBYTE(&PIA_PACTL, 1LL);
StateSav_ReadUBYTE(&PIA_PBCTL, 1LL);
StateSav_ReadUBYTE(&PIA_PORTA, 1LL);
StateSav_ReadUBYTE(&PIA_PORTB, 1LL);
StateSav_ReadINT(&MEMORY_xe_bank, 1LL);
StateSav_ReadINT(&MEMORY_selftest_enabled, 1LL);
StateSav_ReadINT(v1, 1LL);
if ( v1[0] == 1 && Atari800_machine_type == 2 && MEMORY_ram_size == 321 )
MEMORY_ram_size = 320;
StateSav_ReadINT(&MEMORY_cartA0BF_enabled, 1LL);
StateSav_ReadUBYTE(&PIA_PORTA_mask, 1LL);
return StateSav_ReadUBYTE(&PIA_PORTB_mask, 1LL);
}

canonical_decompiled_code:
__int64 func5()
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
StateSav_ReadUBYTE(&PIA_PACTL, 1LL);
StateSav_ReadUBYTE(&PIA_PBCTL, 1LL);
StateSav_ReadUBYTE(&PIA_PORTA, 1LL);
StateSav_ReadUBYTE(&PIA_PORTB, 1LL);
StateSav_ReadINT(&MEMORY_xe_bank, 1LL);
StateSav_ReadINT(&MEMORY_selftest_enabled, 1LL);
StateSav_ReadINT(v1, 1LL);
if ( v1[0] == 1 && Atari800_machine_type == 2 && MEMORY_ram_size == 321 )
MEMORY_ram_size = 320;
StateSav_ReadINT(&MEMORY_cartA0BF_enabled, 1LL);
StateSav_ReadUBYTE(&PIA_PORTA_mask, 1LL);
return StateSav_ReadUBYTE(&PIA_PORTB_mask, 1LL);
}

original_code:
void PIA_StateRead(void)
{
 int Ram256 = 0;
 StateSav_ReadUBYTE( &PIA_PACTL, 1 );
 StateSav_ReadUBYTE( &PIA_PBCTL, 1 );
 StateSav_ReadUBYTE( &PIA_PORTA, 1 );
 StateSav_ReadUBYTE( &PIA_PORTB, 1 );
 StateSav_ReadINT( &MEMORY_xe_bank, 1 );
 StateSav_ReadINT( &MEMORY_selftest_enabled, 1 );
 StateSav_ReadINT( &Ram256, 1 );
 if (Ram256 == 1 && Atari800_machine_type == 2 && MEMORY_ram_size == 321)
  MEMORY_ram_size = 320;
 StateSav_ReadINT( &MEMORY_cartA0BF_enabled, 1 );
 StateSav_ReadUBYTE( &PIA_PORTA_mask, 1 );
 StateSav_ReadUBYTE( &PIA_PORTB_mask, 1 );
}

canonical_original_code:
void PIA_StateRead()
{
 int Ram256 = 0;
 StateSav_ReadUBYTE( &PIA_PACTL, 1 );
 StateSav_ReadUBYTE( &PIA_PBCTL, 1 );
 StateSav_ReadUBYTE( &PIA_PORTA, 1 );
 StateSav_ReadUBYTE( &PIA_PORTB, 1 );
 StateSav_ReadINT( &MEMORY_xe_bank, 1 );
 StateSav_ReadINT( &MEMORY_selftest_enabled, 1 );
 StateSav_ReadINT( &Ram256, 1 );
 if (Ram256 == 1 && Atari800_machine_type == 2 && MEMORY_ram_size == 321)
  MEMORY_ram_size = 320;
 StateSav_ReadINT( &MEMORY_cartA0BF_enabled, 1 );
 StateSav_ReadUBYTE( &PIA_PORTA_mask, 1 );
 StateSav_ReadUBYTE( &PIA_PORTB_mask, 1 );
}


AdjustExternal

original_code:
static void AdjustExternal(int colourtable[256])
{
 const double gamma = 1 - COLOURS_PAL_setup.gamma / 2.0;
 unsigned char *ext_ptr = COLOURS_PAL_external.palette;
 int n;
 for (n = 0; n < 256; n ++) {
  double r = (double)*ext_ptr++ / 255.0;
  double g = (double)*ext_ptr++ / 255.0;
  double b = (double)*ext_ptr++ / 255.0;
  double y = 0.299 * r + 0.587 * g + 0.114 * b;
  double u = -0.14713 * r - 0.28886 * g + 0.436 * b;
  double v = 0.615 * r - 0.51499 * g - 0.10001 * b;
  y = pow(y, gamma);
  y *= COLOURS_PAL_setup.contrast * 0.5 + 1;
  y += COLOURS_PAL_setup.brightness * 0.5;
  if (y > 1.0)
   y = 1.0;
  else if (y < 0.0)
   y = 0.0;
  u *= COLOURS_PAL_setup.saturation + 1.0;
  v *= COLOURS_PAL_setup.saturation + 1.0;
  r = y + 1.13983 * v;
  g = y - 0.39465 * u - 0.58060 * v;
  b = y + 2.03211 * u;
  Colours_SetRGB(n, (int) (r * 255), (int) (g * 255), (int) (b * 255), colourtable);
 }
}

canonical_original_code:
static void AdjustExternal(int colourtable[256])
{
 double gamma = 1 - COLOURS_PAL_setup.gamma / 2.0;
 unsigned char *ext_ptr = COLOURS_PAL_external.palette;
 int n;
 for (n = 0; n < 256; n ++) {
  double r = (double)*ext_ptr++ / 255.0;
  double g = (double)*ext_ptr++ / 255.0;
  double b = (double)*ext_ptr++ / 255.0;
  double y = 0.299 * r + 0.587 * g + 0.114 * b;
  double u = -0.14713 * r - 0.28886 * g + 0.436 * b;
  double v = 0.615 * r - 0.51499 * g - 0.10001 * b;
  y = pow(y, gamma);
  y *= COLOURS_PAL_setup.contrast * 0.5 + 1;
  y += COLOURS_PAL_setup.brightness * 0.5;
  if (y > 1.0)
   y = 1.0;
  else if (y < 0.0)
   y = 0.0;
  u *= COLOURS_PAL_setup.saturation + 1.0;
  v *= COLOURS_PAL_setup.saturation + 1.0;
  r = y + 1.13983 * v;
  g = y - 0.39465 * u - 0.58060 * v;
  b = y + 2.03211 * u;
  Colours_SetRGB(n, (int) (r * 255), (int) (g * 255), (int) (b * 255), colourtable);
 }
}

variable_types:
colourtable
int [256]

variable_types: gamma double

variable_types: n int

variable_types: r double

variable_types: g double

variable_types: b double

variable_types: y double

variable_types: u double

variable_types: v double


GeneratePalette

original_code:
static void GeneratePalette(int colourtable[256])
{
 int cr, lm;
 double scaled_black_level = (double)COLOURS_PAL_setup.black_level / 255.0f;
 double scaled_white_level = (double)COLOURS_PAL_setup.white_level / 255.0f;
 const double gamma = 1 - COLOURS_PAL_setup.gamma / 2.0;
 double luma_mult[16] = {
  0.6941, 0.7091, 0.7241, 0.7401,
  0.7560, 0.7741, 0.7931, 0.8121,
         0.8260, 0.8470, 0.8700, 0.8930,
         0.9160, 0.9420, 0.9690, 1.0000};
 double color_angles[16] = {
  0.0, 2.267095, 1.927638, 1.715966,
  1.228852, 0.863367, 0.447344, -0.361837,
  -0.815415, -1.198193, 4.568137, 4.014433,
  3.568811, 3.134938, 2.71825, 2.279574
 };
 for (cr = 0; cr < 16; cr ++) {
  double angle = color_angles[cr];
  double saturation = (cr ? (COLOURS_PAL_setup.saturation + 1) * 0.175f: 0.0f);
  double u = cos(angle) * saturation;
  double v = sin(angle) * saturation;
  for (lm = 0; lm < 16; lm ++) {
   double y = (luma_mult[lm] - luma_mult[0]) / (luma_mult[15] - luma_mult[0]);
   double r, g, b;
   y = pow(y, gamma);
   y *= COLOURS_PAL_setup.contrast * 0.5 + 1;
   y += COLOURS_PAL_setup.brightness * 0.5;
   y = y * (scaled_white_level - scaled_black_level) + scaled_black_level;
   r = y + 1.13983 * v;
   g = y - 0.39465 * u - 0.58060 * v;
   b = y + 2.03211 * u;
   Colours_SetRGB(cr * 16 + lm, (int) (r * 255), (int) (g * 255), (int) (b * 255), colourtable);
  }
 }
}

canonical_original_code:
static void GeneratePalette(int colourtable[256])
{
 int cr; int lm;
 double scaled_black_level = (double)COLOURS_PAL_setup.black_level / 255.0f;
 double scaled_white_level = (double)COLOURS_PAL_setup.white_level / 255.0f;
 double gamma = 1 - COLOURS_PAL_setup.gamma / 2.0;
 double luma_mult[16] = {
  0.6941, 0.7091, 0.7241, 0.7401,
  0.7560, 0.7741, 0.7931, 0.8121,
         0.8260, 0.8470, 0.8700, 0.8930,
         0.9160, 0.9420, 0.9690, 1.0000};
 double color_angles[16] = {
  0.0, 2.267095, 1.927638, 1.715966,
  1.228852, 0.863367, 0.447344, -0.361837,
  -0.815415, -1.198193, 4.568137, 4.014433,
  3.568811, 3.134938, 2.71825, 2.279574
 };
 for (cr = 0; cr < 16; cr ++) {
  double angle = color_angles[cr];
  double saturation = (cr ? (COLOURS_PAL_setup.saturation + 1) * 0.175f: 0.0f);
  double u = cos(angle) * saturation;
  double v = sin(angle) * saturation;
  for (lm = 0; lm < 16; lm ++) {
   double y = (luma_mult[lm] - luma_mult[0]) / (luma_mult[15] - luma_mult[0]);
   double r; double g; double b;
   y = pow(y, gamma);
   y *= COLOURS_PAL_setup.contrast * 0.5 + 1;
   y += COLOURS_PAL_setup.brightness * 0.5;
   y = y * (scaled_white_level - scaled_black_level) + scaled_black_level;
   r = y + 1.13983 * v;
   g = y - 0.39465 * u - 0.58060 * v;
   b = y + 2.03211 * u;
   Colours_SetRGB(cr * 16 + lm, (int) (r * 255), (int) (g * 255), (int) (b * 255), colourtable);
  }
 }
}

variable_types: cr int

variable_types: lm int

variable_types: angle double

variable_types: saturation double


decompiled_code:
__int64 __fastcall COLOURS_PAL_Update(__int64 a1)
{
__int64 result; // rax

if ( COLOURS_PAL_external[1024] )
result = AdjustExternal(a1);
else
result = GeneratePalette(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax

if ( COLOURS_PAL_external[1024] )
result = func0(a1);
else
result = func1(a1);
return result;
}

original_code:
void COLOURS_PAL_Update(int colourtable[256])
{
 if (COLOURS_PAL_external.loaded)
  AdjustExternal(colourtable);
 else
  GeneratePalette(colourtable);
}


decompiled_code:
__int64 __fastcall COLOURS_PAL_ReadConfig(const char *a1, __int64 a2)
{
bool v4; // dl
bool v5; // cf
bool v6; // zf
const char *v7; // rdi
__int64 v8; // rcx
const char *v9; // rsi
char v10; // r9
bool v11; // cf
bool v12; // zf
const char *v13; // rdi
__int64 v14; // rcx
const char *v15; // rsi
__int64 result; // rax
int v17; // eax
int v18; // eax

if ( !strcmp(a1, "COLOURS_PAL_SATURATION") )
return Util_sscandouble(a2, COLOURS_PAL_setup);
if ( !strcmp(a1, "COLOURS_PAL_CONTRAST") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[1]);
if ( !strcmp(a1, "COLOURS_PAL_BRIGHTNESS") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[2]);
if ( !strcmp(a1, "COLOURS_PAL_GAMMA") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[3]);
v4 = strcmp(a1, "COLOURS_PAL_EXTERNAL_PALETTE") != 0;
v5 = 0;
v6 = !v4;
if ( v4 )
{
v7 = "COLOURS_PAL_EXTERNAL_PALETTE_LOADED";
v8 = 36LL;
v9 = a1;
do
{
if ( !v8 )
break;
v5 = *v9 < (unsigned int)*v7;
v6 = *v9++ == *v7++;
--v8;
}
while ( v6 );
v10 = (!v5 && !v6) - v5;
v11 = 0;
v12 = v10 == 0;
if ( v10 )
{
v13 = "COLOURS_PAL_ADJUST_EXTERNAL_PALETTE";
v14 = 36LL;
v15 = a1;
do
{
if ( !v14 )
break;
v11 = *v15 < (unsigned int)*v13;
v12 = *v15++ == *v13++;
--v14;
}
while ( v12 );
result = 0LL;
if ( (!v11 && !v12) == v11 )
{
v17 = Util_sscanbool(a2, v15);
COLOURS_PAL_external[1025] = v17;
result = v17 != -1;
}
}
else
{
v18 = Util_sscanbool(a2, v9);
COLOURS_PAL_external[1024] = v18;
result = v18 != -1;
}
}
else
{
Util_strlcpy(COLOURS_PAL_external, a2, 4096LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2)
{
bool v4; // dl
bool v5; // cf
bool v6; // zf
const char *v7; // rdi
__int64 v8; // rcx
const char *v9; // rsi
char v10; // r9
bool v11; // cf
bool v12; // zf
const char *v13; // rdi
__int64 v14; // rcx
const char *v15; // rsi
__int64 result; // rax
int v17; // eax
int v18; // eax

if ( !strcmp(a1, "COLOURS_PAL_SATURATION") )
return Util_sscandouble(a2, COLOURS_PAL_setup);
if ( !strcmp(a1, "COLOURS_PAL_CONTRAST") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[1]);
if ( !strcmp(a1, "COLOURS_PAL_BRIGHTNESS") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[2]);
if ( !strcmp(a1, "COLOURS_PAL_GAMMA") )
return Util_sscandouble(a2, &COLOURS_PAL_setup[3]);
v4 = strcmp(a1, "COLOURS_PAL_EXTERNAL_PALETTE") != 0;
v5 = 0;
v6 = !v4;
if ( v4 )
{
v7 = "COLOURS_PAL_EXTERNAL_PALETTE_LOADED";
v8 = 36LL;
v9 = a1;
do
{
if ( !v8 )
break;
v5 = *v9 < (unsigned int)*v7;
v6 = *v9++ == *v7++;
--v8;
}
while ( v6 );
v10 = (!v5 && !v6) - v5;
v11 = 0;
v12 = v10 == 0;
if ( v10 )
{
v13 = "COLOURS_PAL_ADJUST_EXTERNAL_PALETTE";
v14 = 36LL;
v15 = a1;
do
{
if ( !v14 )
break;
v11 = *v15 < (unsigned int)*v13;
v12 = *v15++ == *v13++;
--v14;
}
while ( v12 );
result = 0LL;
if ( (!v11 && !v12) == v11 )
{
v17 = Util_sscanbool(a2, v15);
COLOURS_PAL_external[1025] = v17;
result = v17 != -1;
}
}
else
{
v18 = Util_sscanbool(a2, v9);
COLOURS_PAL_external[1024] = v18;
result = v18 != -1;
}
}
else
{
Util_strlcpy(COLOURS_PAL_external, a2, 4096LL);
result = 1LL;
}
return result;
}

original_code:
int COLOURS_PAL_ReadConfig(char *option, char *ptr)
{
 if (strcmp(option, "COLOURS_PAL_SATURATION") == 0)
  return Util_sscandouble(ptr, &COLOURS_PAL_setup.saturation);
 else if (strcmp(option, "COLOURS_PAL_CONTRAST") == 0)
  return Util_sscandouble(ptr, &COLOURS_PAL_setup.contrast);
 else if (strcmp(option, "COLOURS_PAL_BRIGHTNESS") == 0)
  return Util_sscandouble(ptr, &COLOURS_PAL_setup.brightness);
 else if (strcmp(option, "COLOURS_PAL_GAMMA") == 0)
  return Util_sscandouble(ptr, &COLOURS_PAL_setup.gamma);
 else if (strcmp(option, "COLOURS_PAL_EXTERNAL_PALETTE") == 0)
  Util_strlcpy(COLOURS_PAL_external.filename, ptr, sizeof(COLOURS_PAL_external.filename));
 else if (strcmp(option, "COLOURS_PAL_EXTERNAL_PALETTE_LOADED") == 0)
  return (COLOURS_PAL_external.loaded = Util_sscanbool(ptr)) != -1;
 else if (strcmp(option, "COLOURS_PAL_ADJUST_EXTERNAL_PALETTE") == 0)
  return (COLOURS_PAL_external.adjust = Util_sscanbool(ptr)) != -1;
 else
  return 0;
 return 1;
}

variable_types:
option
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall COLOURS_PAL_Initialise(int *a1, __int64 a2)
{
int v3; // edx
int v5; // ebx
int v6; // ebp
const char **v7; // r12
bool v8; // dl
bool v9; // cf
bool v10; // zf
const char *v11; // rdi
__int64 v12; // rcx
const char *v13; // rsi
__int64 result; // rax
int v15; // er15
int v16; // [rsp+Ch] [rbp-3Ch]

v3 = *a1;
if ( *a1 > 1 )
{
v16 = 1;
v5 = 1;
while ( 1 )
{
v6 = v5 + 1;
v7 = (const char **)(a2 + 8LL * v5);
if ( !strcmp(*v7, "-pal-saturation") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[0] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-contrast") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[1] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-brightness") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[2] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-gamma") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[3] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-palettep") )
{
if ( v6 >= v3 )
{
LABEL_31:
Log_print("Missing argument for '%s'", *v7);
return 0LL;
}
Util_strlcpy(COLOURS_PAL_external, *(_QWORD *)(a2 + 8LL * v6), 4096LL);
COLOURS_PAL_external[1024] = 1;
}
else
{
v8 = strcmp(*v7, "-palettep-adjust") != 0;
v9 = 0;
v10 = !v8;
if ( v8 )
{
v11 = "-help";
v12 = 6LL;
v13 = *v7;
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v11;
v10 = *v13++ == *v11++;
--v12;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
Log_print("\t-pal-saturation <num> Set PAL color saturation", v13);
Log_print("\t-pal-contrast <num> Set PAL contrast", v13);
Log_print("\t-pal-brightness <num> Set PAL brightness", v13);
Log_print("\t-pal-gamma <num> Set PAL color gamma factor", v13);
Log_print("\t-palettep <filename> Load PAL external palette", v13);
Log_print("\t-palettep-adjust Apply adjustments to PAL external palette", v13);
}
*(_QWORD *)(a2 + 8LL * v16++) = *v7;
v6 = v5;
}
else
{
COLOURS_PAL_external[1025] = 1;
v6 = v5;
}
}
v5 = v6 + 1;
v3 = *a1;
if ( *a1 <= v6 + 1 )
goto LABEL_28;
}
}
v16 = 1;
LABEL_28:
*a1 = v16;
result = 1LL;
if ( COLOURS_PAL_external[1024] )
{
v15 = COLOURS_EXTERNAL_Read(COLOURS_PAL_external);
result = 1LL;
if ( !v15 )
{
Log_print("Cannot read PAL palette from %s", (const char *)COLOURS_PAL_external);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1, __int64 a2)
{
int v3; // edx
int v5; // ebx
int v6; // ebp
const char **v7; // r12
bool v8; // dl
bool v9; // cf
bool v10; // zf
const char *v11; // rdi
__int64 v12; // rcx
const char *v13; // rsi
__int64 result; // rax
int v15; // er15
int v16; // [rsp+Ch] [rbp-3Ch]

v3 = *a1;
if ( *a1 > 1 )
{
v16 = 1;
v5 = 1;
while ( 1 )
{
v6 = v5 + 1;
v7 = (const char **)(a2 + 8LL * v5);
if ( !strcmp(*v7, "-pal-saturation") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[0] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-contrast") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[1] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-brightness") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[2] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-pal-gamma") )
{
if ( v6 >= v3 )
goto LABEL_31;
COLOURS_PAL_setup[3] = atof(*(const char **)(a2 + 8LL * v6));
}
else if ( !strcmp(*v7, "-palettep") )
{
if ( v6 >= v3 )
{
LABEL_31:
Log_print("Missing argument for '%s'", *v7);
return 0LL;
}
Util_strlcpy(COLOURS_PAL_external, *(_QWORD *)(a2 + 8LL * v6), 4096LL);
COLOURS_PAL_external[1024] = 1;
}
else
{
v8 = strcmp(*v7, "-palettep-adjust") != 0;
v9 = 0;
v10 = !v8;
if ( v8 )
{
v11 = "-help";
v12 = 6LL;
v13 = *v7;
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v11;
v10 = *v13++ == *v11++;
--v12;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
Log_print("\t-pal-saturation <num> Set PAL color saturation", v13);
Log_print("\t-pal-contrast <num> Set PAL contrast", v13);
Log_print("\t-pal-brightness <num> Set PAL brightness", v13);
Log_print("\t-pal-gamma <num> Set PAL color gamma factor", v13);
Log_print("\t-palettep <filename> Load PAL external palette", v13);
Log_print("\t-palettep-adjust Apply adjustments to PAL external palette", v13);
}
*(_QWORD *)(a2 + 8LL * v16++) = *v7;
v6 = v5;
}
else
{
COLOURS_PAL_external[1025] = 1;
v6 = v5;
}
}
v5 = v6 + 1;
v3 = *a1;
if ( *a1 <= v6 + 1 )
goto LABEL_28;
}
}
v16 = 1;
LABEL_28:
*a1 = v16;
result = 1LL;
if ( COLOURS_PAL_external[1024] )
{
v15 = COLOURS_EXTERNAL_Read(COLOURS_PAL_external);
result = 1LL;
if ( !v15 )
{
Log_print("Cannot read PAL palette from %s", (const char *)COLOURS_PAL_external);
result = 1LL;
}
}
return result;
}

original_code:
int COLOURS_PAL_Initialise(int *argc, char *argv[])
{
 int i;
 int j;
 for (i = j = 1; i < *argc; i++) {
  int i_a = (i + 1 < *argc);
  int a_m = 0;
  if (strcmp(argv[i], "-pal-saturation") == 0) {
   if (i_a)
    COLOURS_PAL_setup.saturation = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-pal-contrast") == 0) {
   if (i_a)
    COLOURS_PAL_setup.contrast = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-pal-brightness") == 0) {
   if (i_a)
    COLOURS_PAL_setup.brightness = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-pal-gamma") == 0) {
   if (i_a)
    COLOURS_PAL_setup.gamma = atof(argv[++i]);
   else a_m = 1;
  }
  else if (strcmp(argv[i], "-palettep") == 0) {
   if (i_a) {
    Util_strlcpy(COLOURS_PAL_external.filename, argv[++i], sizeof(COLOURS_PAL_external.filename));
    COLOURS_PAL_external.loaded = 1;
   } else a_m = 1;
  }
  else if (strcmp(argv[i], "-palettep-adjust") == 0)
   COLOURS_PAL_external.adjust = 1;
  else {
   if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-pal-saturation <num>  Set PAL color saturation");
    Log_print("\t-pal-contrast <num>    Set PAL contrast");
    Log_print("\t-pal-brightness <num>  Set PAL brightness");
    Log_print("\t-pal-gamma <num>       Set PAL color gamma factor");
    Log_print("\t-palettep <filename>   Load PAL external palette");
    Log_print("\t-palettep-adjust       Apply adjustments to PAL external palette");
   }
   argv[j++] = argv[i];
  }
  if (a_m) {
   Log_print("Missing argument for '%s'", argv[i]);
   return 0;
  }
 }
 *argc = j;
 if (COLOURS_PAL_external.loaded && !COLOURS_EXTERNAL_Read(&COLOURS_PAL_external))
  Log_print("Cannot read PAL palette from %s", COLOURS_PAL_external.filename);
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall COLOURS_EXTERNAL_Remove(__int64 a1)
{
*(_DWORD *)(a1 + 4096) = 0;
}

original_code:
void COLOURS_EXTERNAL_Remove(COLOURS_EXTERNAL_t *colours)
{
 colours->loaded = 0;
}

canonical_original_code:
void COLOURS_EXTERNAL_Remove(struct COLOURS_EXTERNAL_t *colours)
{
 colours->loaded = 0;
}

variable_types: struct COLOURS_EXTERNAL_t { char filename[4096]; int loaded; int adjust; unsigned char palette[768]; } *


decompiled_code:
__int64 __fastcall COLOURS_EXTERNAL_ReadFilename(__int64 a1, __int64 a2)
{
Util_strlcpy(a1, a2, 4096LL);
return COLOURS_EXTERNAL_Read(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
Util_strlcpy(a1, a2, 4096LL);
return func0(a1);
}

original_code:
int COLOURS_EXTERNAL_ReadFilename(COLOURS_EXTERNAL_t *colours, char *filename)
{
 Util_strlcpy(colours->filename, filename, 4096);
 return COLOURS_EXTERNAL_Read(colours);
}

canonical_original_code:
int COLOURS_EXTERNAL_ReadFilename(struct COLOURS_EXTERNAL_t *colours, char *filename)
{
 Util_strlcpy(colours->filename, filename, 4096);
 return COLOURS_EXTERNAL_Read(colours);
}

variable_types:
filename
char *

user_defined_types: struct COLOURS_EXTERNAL_t { char filename[4096]; int loaded; int adjust; unsigned char palette[768]; }


decompiled_code:
_DWORD *scsi_process_command()
{
_DWORD *result; // rax

if ( scsi_buffer == 8 )
{
fseek(
PBI_SCSI_disk,
(((unsigned __int8)byte_501 << 16) & 0x1F0000 | (unsigned __int8)byte_503 | ((unsigned __int8)byte_502 << 8)) << 8,
0);
scsi_count = fread(&scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
result = scsi_changephase(1);
}
else
{
if ( (unsigned __int8)scsi_buffer > 8u )
{
if ( scsi_buffer == 10 )
{
fseek(
PBI_SCSI_disk,
(((unsigned __int8)byte_501 << 16) & 0x1F0000 | (unsigned __int8)byte_503 | ((unsigned __int8)byte_502 << 8)) << 8,
0);
result = scsi_changephase(2);
scsi_count = 256;
return result;
}
}
else
{
if ( !scsi_buffer )
{
result = scsi_changephase(4);
scsi_buffer = 0;
return result;
}
if ( scsi_buffer == 3 )
{
result = scsi_changephase(1);
scsi_buffer = 0;
scsi_count = 4;
return result;
}
}
result = scsi_changephase(0);
}
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

if ( scsi_buffer == 8 )
{
fseek(
PBI_SCSI_disk,
(((unsigned __int8)byte_501 << 16) & 0x1F0000 | (unsigned __int8)byte_503 | ((unsigned __int8)byte_502 << 8)) << 8,
0);
scsi_count = fread(&scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
result = scsi_changephase(1);
}
else
{
if ( (unsigned __int8)scsi_buffer > 8u )
{
if ( scsi_buffer == 10 )
{
fseek(
PBI_SCSI_disk,
(((unsigned __int8)byte_501 << 16) & 0x1F0000 | (unsigned __int8)byte_503 | ((unsigned __int8)byte_502 << 8)) << 8,
0);
result = scsi_changephase(2);
scsi_count = 256;
return result;
}
}
else
{
if ( !scsi_buffer )
{
result = scsi_changephase(4);
scsi_buffer = 0;
return result;
}
if ( scsi_buffer == 3 )
{
result = scsi_changephase(1);
scsi_buffer = 0;
scsi_count = 4;
return result;
}
}
result = scsi_changephase(0);
}
return result;
}

original_code:
static void scsi_process_command(void)
{
 int i;
 int lba;
 int lun;
 do{}while(0);
 for (i = 0; i < 6; i++) {
  do{}while(0);
 }
 do{}while(0);
 switch (scsi_buffer[0]) {
  case 0x00:
   do{}while(0);
   scsi_changephase(4);
   scsi_buffer[0] = 0;
   break;
  case 0x03:
   do{}while(0);
   scsi_changephase(1);
   memset(scsi_buffer,0,1);
   scsi_count = 4;
   break;
  case 0x08:
   lun = ((scsi_buffer[1]&0xe0)>>5);
   lba = (((scsi_buffer[1]&0x1f)<<16)|(scsi_buffer[2]<<8)|(scsi_buffer[3]));
   do{}while(0);
   fseek(PBI_SCSI_disk, lba*256, 0);
   scsi_count = fread(scsi_buffer, 1, 256, PBI_SCSI_disk);
   scsi_changephase(1);
   break;
  case 0x0a:
   lun = ((scsi_buffer[1]&0xe0)>>5);
   lba = (((scsi_buffer[1]&0x1f)<<16)|(scsi_buffer[2]<<8)|(scsi_buffer[3]));
   do{}while(0);
   fseek(PBI_SCSI_disk, lba*256, 0);
   scsi_changephase(2);
   scsi_count = 256;
   break;
  default:
   do{}while(0);
   scsi_changephase(0);
   break;
 }
}

canonical_original_code:
static void scsi_process_command()
{
 int i;
 int lba;
 int lun;
 do{}while(0);
 for (i = 0; i < 6; i++) {
  do{}while(0);
 }
 do{}while(0);
 switch (scsi_buffer[0]) {
  case 0x00:
   do{}while(0);
   scsi_changephase(4);
   scsi_buffer[0] = 0;
   break;
  case 0x03:
   do{}while(0);
   scsi_changephase(1);
   memset(scsi_buffer,0,1);
   scsi_count = 4;
   break;
  case 0x08:
   lun = ((scsi_buffer[1]&0xe0)>>5);
   lba = (((scsi_buffer[1]&0x1f)<<16)|(scsi_buffer[2]<<8)|(scsi_buffer[3]));
   do{}while(0);
   fseek(PBI_SCSI_disk, lba*256, 0);
   scsi_count = fread(scsi_buffer, 1, 256, PBI_SCSI_disk);
   scsi_changephase(1);
   break;
  case 0x0a:
   lun = ((scsi_buffer[1]&0xe0)>>5);
   lba = (((scsi_buffer[1]&0x1f)<<16)|(scsi_buffer[2]<<8)|(scsi_buffer[3]));
   do{}while(0);
   fseek(PBI_SCSI_disk, lba*256, 0);
   scsi_changephase(2);
   scsi_count = 256;
   break;
  default:
   do{}while(0);
   scsi_changephase(0);
   break;
 }
}

variable_types: lba int

variable_types: lun int


decompiled_code:
unsigned __int64 scsi_nextbyte()
{
unsigned __int64 result; // rax
int v1; // edx
int v2; // er8

result = (unsigned int)scsi_phase;
switch ( scsi_phase )
{
case 1:
if ( ++scsi_bufpos >= scsi_count )
{
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
}
break;
case 4:
result = (unsigned __int64)scsi_changephase(5);
scsi_buffer[0] = 0;
break;
case 5:
result = (unsigned __int64)scsi_changephase(0);
break;
case 3:
LODWORD(result) = scsi_bufpos++;
v2 = scsi_bufpos;
result = (int)result;
scsi_buffer[(int)result] = scsi_byte;
if ( v2 > 5 )
{
result = (unsigned __int64)scsi_process_command();
scsi_bufpos = 0;
}
break;
case 2:
LODWORD(result) = scsi_bufpos++;
v1 = scsi_bufpos;
result = (int)result;
scsi_buffer[(int)result] = scsi_byte;
if ( v1 >= scsi_count )
{
fwrite(scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
}
break;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func1()
{
unsigned __int64 result; // rax
int v1; // edx
int v2; // er8

result = (unsigned int)scsi_phase;
switch ( scsi_phase )
{
case 1:
if ( ++scsi_bufpos >= scsi_count )
{
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
}
break;
case 4:
result = (unsigned __int64)scsi_changephase(5);
scsi_buffer[0] = 0;
break;
case 5:
result = (unsigned __int64)scsi_changephase(0);
break;
case 3:
LODWORD(result) = scsi_bufpos++;
v2 = scsi_bufpos;
result = (int)result;
scsi_buffer[(int)result] = scsi_byte;
if ( v2 > 5 )
{
result = (unsigned __int64)func0();
scsi_bufpos = 0;
}
break;
case 2:
LODWORD(result) = scsi_bufpos++;
v1 = scsi_bufpos;
result = (int)result;
scsi_buffer[(int)result] = scsi_byte;
if ( v1 >= scsi_count )
{
fwrite(scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
}
break;
}
return result;
}

original_code:
static void scsi_nextbyte(void)
{
 if (scsi_phase == 1) {
  scsi_bufpos++;
  if (scsi_bufpos >= scsi_count) {
   scsi_changephase(4);
   scsi_buffer[0] = 0;
  }
 }
 else if (scsi_phase == 4) {
  do{}while(0);
  scsi_changephase(5);
  scsi_buffer[0] = 0;
 }
 else if (scsi_phase == 5) {
  do{}while(0);
  scsi_changephase(0);
 }
 else if (scsi_phase == 3) {
  scsi_buffer[scsi_bufpos++] = scsi_byte;
  if (scsi_bufpos >= 0x06) {
   scsi_process_command();
   scsi_bufpos = 0;
  }
 }
 else if (scsi_phase == 2) {
  do{}while(0);
  scsi_buffer[scsi_bufpos++] = scsi_byte;
  if (scsi_bufpos >= scsi_count) {
   fwrite(scsi_buffer, 1, 256, PBI_SCSI_disk);
   scsi_changephase(4);
   scsi_buffer[0] = 0;
  }
 }
}

canonical_original_code:
static void scsi_nextbyte()
{
 if (scsi_phase == 1) {
  scsi_bufpos++;
  if (scsi_bufpos >= scsi_count) {
   scsi_changephase(4);
   scsi_buffer[0] = 0;
  }
 }
 else if (scsi_phase == 4) {
  do{}while(0);
  scsi_changephase(5);
  scsi_buffer[0] = 0;
 }
 else if (scsi_phase == 5) {
  do{}while(0);
  scsi_changephase(0);
 }
 else if (scsi_phase == 3) {
  scsi_buffer[scsi_bufpos++] = scsi_byte;
  if (scsi_bufpos >= 0x06) {
   scsi_process_command();
   scsi_bufpos = 0;
  }
 }
 else if (scsi_phase == 2) {
  do{}while(0);
  scsi_buffer[scsi_bufpos++] = scsi_byte;
  if (scsi_bufpos >= scsi_count) {
   fwrite(scsi_buffer, 1, 256, PBI_SCSI_disk);
   scsi_changephase(4);
   scsi_buffer[0] = 0;
  }
 }
}


decompiled_code:
__int64 PBI_SCSI_GetByte()
{
return (unsigned __int8)scsi_buffer[scsi_bufpos];
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned __int8)scsi_buffer[scsi_bufpos];
}

original_code:
unsigned char PBI_SCSI_GetByte(void)
{
 return (scsi_buffer[scsi_bufpos]);
}

canonical_original_code:
unsigned char PBI_SCSI_GetByte()
{
 return (scsi_buffer[scsi_bufpos]);
}


decompiled_code:
void __fastcall PBI_SCSI_PutByte(char a1)
{
scsi_byte = a1;
}

canonical_decompiled_code:
void __fastcall func3(char a1)
{
scsi_byte = a1;
}

original_code:
void PBI_SCSI_PutByte(unsigned char byte)
{
 scsi_byte = byte;
}

variable_types:
byte
unsigned char


decompiled_code:
int Log_print(char *format, ...)
{
char v2[8200]; // [rsp+0h] [rbp-20D8h] BYREF
gcc_va_list arg; // [rsp+2008h] [rbp-D0h] BYREF

va_start(arg, format);
vsnprintf(v2, 0x1FFEuLL, format, arg);
*(_WORD *)&v2[strlen(v2)] = 10;
return printf("%s", v2);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
char v2[8200]; // [rsp+0h] [rbp-20D8h] BYREF
gcc_va_list arg; // [rsp+2008h] [rbp-D0h] BYREF

va_start(arg, format);
vsnprintf(v2, 0x1FFEuLL, format, arg);
*(_WORD *)&v2[strlen(v2)] = 10;
return printf("%s", v2);
}

original_code:
void Log_print(char *format, ...)
{
 va_list args;
 char buffer[8192];
 __builtin_va_start(args,format);
 vsnprintf(buffer, sizeof(buffer) - 2 , format, args);
 __builtin_va_end(args);
 strcat(buffer, "\n");
 printf("%s", buffer);
}

canonical_original_code:
void Log_print(char *format, ...)
{
 __builtin_va_list args;
 char buffer[8192];
 __builtin_va_start(args,format);
 vsnprintf(buffer, sizeof(buffer) - 2 , format, args);
 __builtin_va_end(args);
 strcat(buffer, "\n");
 printf("%s", buffer);
}

variable_types:
format
char *

variable_types:
buffer
char [8192]


decompiled_code:
void Log_flushlog()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void Log_flushlog(void)
{
}

canonical_original_code:
void Log_flushlog()
{
}


decompiled_code:
__int64 read_word()
{
char ptr[10]; // [rsp+Eh] [rbp-Ah] BYREF

if ( fread(ptr, 1uLL, 2uLL, BINLOAD_bin_file) == 2 )
return (unsigned __int8)ptr[0] + ((unsigned __int8)ptr[1] << 8);
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
if ( BINLOAD_start_binloading )
{
BINLOAD_start_binloading = 0;
Log_print("binload: not valid BIN file");
}
else
{
CPU_regPC = MEMORY_mem[368];
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
char ptr[10]; // [rsp+Eh] [rbp-Ah] BYREF

if ( fread(ptr, 1uLL, 2uLL, BINLOAD_bin_file) == 2 )
return (unsigned __int8)ptr[0] + ((unsigned __int8)ptr[1] << 8);
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
if ( BINLOAD_start_binloading )
{
BINLOAD_start_binloading = 0;
Log_print("binload: not valid BIN file");
}
else
{
CPU_regPC = MEMORY_mem[368];
}
return 0xFFFFFFFFLL;
}

original_code:
static int read_word(void)
{
 unsigned char buf[2];
 if (fread(buf, 1, 2, BINLOAD_bin_file) != 2) {
  fclose(BINLOAD_bin_file);
  BINLOAD_bin_file = ((void *)0);
  if (BINLOAD_start_binloading) {
   BINLOAD_start_binloading = 0;
   Log_print("binload: not valid BIN file");
   return -1;
  }
  CPU_regPC = (*(const unsigned short *) (&MEMORY_mem[0x2e0]));
  return -1;
 }
 return buf[0] + (buf[1] << 8);
}

canonical_original_code:
static int read_word()
{
 unsigned char buf[2];
 if (fread(buf, 1, 2, BINLOAD_bin_file) != 2) {
  fclose(BINLOAD_bin_file);
  BINLOAD_bin_file = ((void *)0);
  if (BINLOAD_start_binloading) {
   BINLOAD_start_binloading = 0;
   Log_print("binload: not valid BIN file");
   return -1;
  }
  CPU_regPC = (*(unsigned short *) (&MEMORY_mem[0x2e0]));
  return -1;
 }
 return buf[0] + (buf[1] << 8);
}

variable_types:
buf
unsigned char [2]


decompiled_code:
__int64 loader_cont()
{
__int64 result; // rax
__int16 v1; // r13
int v2; // ebp
__int16 v3; // r12
__int16 v4; // bx
int v5; // eax
char v6; // dl
char v7; // dl
char v8; // r8
int v9; // eax
char v10; // bl
int v11; // eax
char v12; // cl
unsigned __int16 v13; // bp
int v14; // eax
char v15; // r11
unsigned __int16 v16; // bp
int v17; // eax
char v18; // cl
int v19; // eax
char v20; // r10
int v21; // ebx
int v22; // eax
char v23; // dl

result = (__int64)&BINLOAD_bin_file;
if ( BINLOAD_bin_file )
{
if ( BINLOAD_start_binloading )
{
LOBYTE(MEMORY_mem[290]) = 0;
HIBYTE(MEMORY_mem[4]) = 1;
}
else
{
CPU_regS += 2;
}
HIBYTE(MEMORY_mem[369]) = -41;
do
{
LABEL_5:
result = read_word();
v1 = result;
}
while ( (_DWORD)result == 0xFFFF );
if ( (int)result >= 0 )
{
v2 = result;
result = read_word();
v3 = result;
if ( (int)result >= 0 )
{
if ( BINLOAD_start_binloading )
{
MEMORY_mem[368] = v1;
BINLOAD_start_binloading = 0;
}
v4 = (result - v2) & 3;
if ( !v4 )
{
LABEL_50:
while ( 1 )
{
v19 = fgetc(BINLOAD_bin_file);
if ( v19 == -1 )
goto LABEL_16;
v20 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v20 )
{
if ( v20 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v19);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v19;
}
v21 = v2 + 1;
if ( (_WORD)v2 == v3 )
{
if ( HIBYTE(MEMORY_mem[369]) == 0xD7 )
goto LABEL_5;
--CPU_regS;
result = ESC_Add((unsigned int)CPU_regS + 256, 13LL, loader_cont);
v8 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(CPU_regS - 1)) = 1;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v8 - 2)) = v8 - 1;
CPU_regS = v8 - 3;
CPU_regPC = MEMORY_mem[369];
CPU_regP |= 1u;
LOBYTE(MEMORY_mem[384]) = 49;
return result;
}
v22 = fgetc(BINLOAD_bin_file);
if ( v22 == -1 )
goto LABEL_16;
v23 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v21);
if ( v23 )
{
if ( v23 == 2 )
MEMORY_HwPutByte((unsigned __int16)v21, (unsigned __int8)v22);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v21) = v22;
}
v13 = v2 + 2;
v14 = fgetc(BINLOAD_bin_file);
if ( v14 == -1 )
goto LABEL_16;
v15 = *((_BYTE *)&MEMORY_attrib + v13);
if ( v15 )
{
if ( v15 == 2 )
MEMORY_HwPutByte(v13, (unsigned __int8)v14);
}
else
{
*((_BYTE *)MEMORY_mem + v13) = v14;
}
v16 = v21 + 2;
v17 = fgetc(BINLOAD_bin_file);
if ( v17 == -1 )
goto LABEL_16;
v18 = *((_BYTE *)&MEMORY_attrib + v16);
if ( v18 )
{
if ( v18 == 2 )
MEMORY_HwPutByte(v16, (unsigned __int8)v17);
}
else
{
*((_BYTE *)MEMORY_mem + v16) = v17;
}
v2 = v21 + 3;
}
}
v5 = fgetc(BINLOAD_bin_file);
if ( v5 != -1 )
{
v6 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v6 )
{
if ( v6 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v5);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v5;
}
++v2;
if ( v4 == 1 )
goto LABEL_50;
if ( v4 == 2 )
goto LABEL_32;
v9 = fgetc(BINLOAD_bin_file);
if ( v9 != -1 )
{
v10 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v10 )
{
if ( v10 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v9);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v9;
}
++v2;
LABEL_32:
v11 = fgetc(BINLOAD_bin_file);
if ( v11 != -1 )
{
v12 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v12 )
{
if ( v12 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v11);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v11;
}
++v2;
goto LABEL_50;
}
goto LABEL_16;
}
}
LABEL_16:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
result = (unsigned __int16)MEMORY_mem[368];
CPU_regPC = MEMORY_mem[368];
if ( HIBYTE(MEMORY_mem[369]) != 0xD7 )
{
v7 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + CPU_regS) = (unsigned __int16)(result - 1) >> 8;
CPU_regS = v7 - 2;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v7 - 1)) = result - 1;
CPU_regPC = MEMORY_mem[369];
}
return result;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int16 v1; // r13
int v2; // ebp
__int16 v3; // r12
__int16 v4; // bx
int v5; // eax
char v6; // dl
char v7; // dl
char v8; // r8
int v9; // eax
char v10; // bl
int v11; // eax
char v12; // cl
unsigned __int16 v13; // bp
int v14; // eax
char v15; // r11
unsigned __int16 v16; // bp
int v17; // eax
char v18; // cl
int v19; // eax
char v20; // r10
int v21; // ebx
int v22; // eax
char v23; // dl

result = (__int64)&BINLOAD_bin_file;
if ( BINLOAD_bin_file )
{
if ( BINLOAD_start_binloading )
{
LOBYTE(MEMORY_mem[290]) = 0;
HIBYTE(MEMORY_mem[4]) = 1;
}
else
{
CPU_regS += 2;
}
HIBYTE(MEMORY_mem[369]) = -41;
do
{
LABEL_5:
result = func0();
v1 = result;
}
while ( (_DWORD)result == 0xFFFF );
if ( (int)result >= 0 )
{
v2 = result;
result = func0();
v3 = result;
if ( (int)result >= 0 )
{
if ( BINLOAD_start_binloading )
{
MEMORY_mem[368] = v1;
BINLOAD_start_binloading = 0;
}
v4 = (result - v2) & 3;
if ( !v4 )
{
LABEL_50:
while ( 1 )
{
v19 = fgetc(BINLOAD_bin_file);
if ( v19 == -1 )
goto LABEL_16;
v20 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v20 )
{
if ( v20 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v19);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v19;
}
v21 = v2 + 1;
if ( (_WORD)v2 == v3 )
{
if ( HIBYTE(MEMORY_mem[369]) == 0xD7 )
goto LABEL_5;
--CPU_regS;
result = ESC_Add((unsigned int)CPU_regS + 256, 13LL, loader_cont);
v8 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(CPU_regS - 1)) = 1;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v8 - 2)) = v8 - 1;
CPU_regS = v8 - 3;
CPU_regPC = MEMORY_mem[369];
CPU_regP |= 1u;
LOBYTE(MEMORY_mem[384]) = 49;
return result;
}
v22 = fgetc(BINLOAD_bin_file);
if ( v22 == -1 )
goto LABEL_16;
v23 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v21);
if ( v23 )
{
if ( v23 == 2 )
MEMORY_HwPutByte((unsigned __int16)v21, (unsigned __int8)v22);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v21) = v22;
}
v13 = v2 + 2;
v14 = fgetc(BINLOAD_bin_file);
if ( v14 == -1 )
goto LABEL_16;
v15 = *((_BYTE *)&MEMORY_attrib + v13);
if ( v15 )
{
if ( v15 == 2 )
MEMORY_HwPutByte(v13, (unsigned __int8)v14);
}
else
{
*((_BYTE *)MEMORY_mem + v13) = v14;
}
v16 = v21 + 2;
v17 = fgetc(BINLOAD_bin_file);
if ( v17 == -1 )
goto LABEL_16;
v18 = *((_BYTE *)&MEMORY_attrib + v16);
if ( v18 )
{
if ( v18 == 2 )
MEMORY_HwPutByte(v16, (unsigned __int8)v17);
}
else
{
*((_BYTE *)MEMORY_mem + v16) = v17;
}
v2 = v21 + 3;
}
}
v5 = fgetc(BINLOAD_bin_file);
if ( v5 != -1 )
{
v6 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v6 )
{
if ( v6 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v5);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v5;
}
++v2;
if ( v4 == 1 )
goto LABEL_50;
if ( v4 == 2 )
goto LABEL_32;
v9 = fgetc(BINLOAD_bin_file);
if ( v9 != -1 )
{
v10 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v10 )
{
if ( v10 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v9);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v9;
}
++v2;
LABEL_32:
v11 = fgetc(BINLOAD_bin_file);
if ( v11 != -1 )
{
v12 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v12 )
{
if ( v12 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v11);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v11;
}
++v2;
goto LABEL_50;
}
goto LABEL_16;
}
}
LABEL_16:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
result = (unsigned __int16)MEMORY_mem[368];
CPU_regPC = MEMORY_mem[368];
if ( HIBYTE(MEMORY_mem[369]) != 0xD7 )
{
v7 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + CPU_regS) = (unsigned __int16)(result - 1) >> 8;
CPU_regS = v7 - 2;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v7 - 1)) = result - 1;
CPU_regPC = MEMORY_mem[369];
}
return result;
}
}
}
return result;
}

original_code:
static void loader_cont(void)
{
 if (BINLOAD_bin_file == ((void *)0))
  return;
 if (BINLOAD_start_binloading) {
  (MEMORY_mem[0x244] = 0);
  (MEMORY_mem[0x09] = 1);
 }
 else
  CPU_regS += 2;
 (MEMORY_mem[0x2e3] = 0xd7);
 do {
  int temp;
  unsigned short from;
  unsigned short to;
  do
   temp = read_word();
  while (temp == 0xffff);
  if (temp < 0)
   return;
  from = (unsigned short) temp;
  temp = read_word();
  if (temp < 0)
   return;
  to = (unsigned short) temp;
  if (BINLOAD_start_binloading) {
   (*(unsigned short *) (&MEMORY_mem[0x2e0]) = ((from)));
   BINLOAD_start_binloading = 0;
  }
  to++;
  do {
   int byte = fgetc(BINLOAD_bin_file);
   if (byte == (-1)) {
    fclose(BINLOAD_bin_file);
    BINLOAD_bin_file = ((void *)0);
    CPU_regPC = (*(const unsigned short *) (&MEMORY_mem[0x2e0]));
    if ((MEMORY_mem[0x2e3]) != 0xd7) {
     CPU_regPC--;
     (MEMORY_mem[0x0100 + CPU_regS--] = CPU_regPC >> 8);
     (MEMORY_mem[0x0100 + CPU_regS--] = CPU_regPC & 0xff);
     CPU_regPC = (*(const unsigned short *) (&MEMORY_mem[0x2e2]));
    }
    return;
   }
   do { if (MEMORY_attrib[from] == 0) MEMORY_mem[from] = (unsigned char) byte; else if (MEMORY_attrib[from] == 2) MEMORY_HwPutByte(from, (unsigned char) byte); } while (0);
   from++;
  } while (from != to);
 } while ((MEMORY_mem[0x2e3]) == 0xd7);
 CPU_regS--;
 ESC_Add((unsigned short) (0x100 + CPU_regS), ESC_BINLOADER_CONT, loader_cont);
 CPU_regS--;
 (MEMORY_mem[0x0100 + CPU_regS--] = 0x01);
 (MEMORY_mem[0x0100 + CPU_regS] = CPU_regS + 1);
 CPU_regS--;
 CPU_regPC = (*(const unsigned short *) (&MEMORY_mem[0x2e2]));
 CPU_regP |= 0x01;
 (MEMORY_mem[0x0300] = 0x31);
}

canonical_original_code:
static void loader_cont()
{
 if (BINLOAD_bin_file == ((void *)0))
  return;
 if (BINLOAD_start_binloading) {
  (MEMORY_mem[0x244] = 0);
  (MEMORY_mem[0x09] = 1);
 }
 else
  CPU_regS += 2;
 (MEMORY_mem[0x2e3] = 0xd7);
 do {
  int temp;
  unsigned short from;
  unsigned short to;
  do
   temp = read_word();
  while (temp == 0xffff);
  if (temp < 0)
   return;
  from = (unsigned short) temp;
  temp = read_word();
  if (temp < 0)
   return;
  to = (unsigned short) temp;
  if (BINLOAD_start_binloading) {
   (*(unsigned short *) (&MEMORY_mem[0x2e0]) = ((from)));
   BINLOAD_start_binloading = 0;
  }
  to++;
  do {
   int byte = fgetc(BINLOAD_bin_file);
   if (byte == (-1)) {
    fclose(BINLOAD_bin_file);
    BINLOAD_bin_file = ((void *)0);
    CPU_regPC = (*(unsigned short *) (&MEMORY_mem[0x2e0]));
    if ((MEMORY_mem[0x2e3]) != 0xd7) {
     CPU_regPC--;
     (MEMORY_mem[0x0100 + CPU_regS--] = CPU_regPC >> 8);
     (MEMORY_mem[0x0100 + CPU_regS--] = CPU_regPC & 0xff);
     CPU_regPC = (*(unsigned short *) (&MEMORY_mem[0x2e2]));
    }
    return;
   }
   do { if (MEMORY_attrib[from] == 0) MEMORY_mem[from] = (unsigned char) byte; else if (MEMORY_attrib[from] == 2) MEMORY_HwPutByte(from, (unsigned char) byte); } while (0);
   from++;
  } while (from != to);
 } while ((MEMORY_mem[0x2e3]) == 0xd7);
 CPU_regS--;
 ESC_Add((unsigned short) (0x100 + CPU_regS), ESC_BINLOADER_CONT, loader_cont);
 CPU_regS--;
 (MEMORY_mem[0x0100 + CPU_regS--] = 0x01);
 (MEMORY_mem[0x0100 + CPU_regS] = CPU_regS + 1);
 CPU_regS--;
 CPU_regPC = (*(unsigned short *) (&MEMORY_mem[0x2e2]));
 CPU_regP |= 0x01;
 (MEMORY_mem[0x0300] = 0x31);
}

variable_types: temp int

variable_types:
from
unsigned short

variable_types:
to
unsigned short

variable_types: byte int


original_code:
int BINLOAD_LoaderStart(unsigned char *buffer)
{
 buffer[0] = 0x00;
 buffer[1] = 0x01;
 buffer[2] = 0x00;
 buffer[3] = 0x07;
 buffer[4] = 0x77;
 buffer[5] = 0xe4;
 buffer[6] = 0xf2;
 buffer[7] = ESC_BINLOADER_CONT;
 ESC_Add(0x706, ESC_BINLOADER_CONT, loader_cont);
 return 'C';
}

variable_types:
buffer
unsigned char *


decompiled_code:
__int64 __fastcall BINLOAD_Loader(char *filename)
{
__int64 result; // rax
char ptr; // [rsp+Eh] [rbp-1Ah] BYREF
char v3; // [rsp+Fh] [rbp-19h]

if ( BINLOAD_bin_file )
{
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
BINLOAD_loading_basic = 0;
}
if ( Atari800_machine_type == 3 )
{
Log_print("binload: can't run Atari programs directly on the 5200");
result = 0LL;
}
else
{
BINLOAD_bin_file = fopen(filename, "rb");
if ( BINLOAD_bin_file )
{
if ( SIO_drive_status == 1 )
SIO_DisableDrive(1LL);
if ( fread(&ptr, 1uLL, 2uLL, BINLOAD_bin_file) != 2 )
goto LABEL_11;
if ( ptr == -1 )
{
if ( v3 != -1 )
goto LABEL_11;
BINLOAD_start_binloading = 1;
Atari800_Coldstart();
result = 1LL;
}
else if ( ptr )
{
if ( (unsigned __int8)(ptr - 48) > 9u )
goto LABEL_11;
BINLOAD_loading_basic = 2;
Devices_PatchOS();
Atari800_Coldstart();
result = 1LL;
}
else
{
if ( v3 )
{
LABEL_11:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
Log_print("binload: \"%s\" not recognized as a DOS or BASIC program", filename);
return 0LL;
}
BINLOAD_loading_basic = 1;
Devices_PatchOS();
Atari800_Coldstart();
result = 1LL;
}
}
else
{
Log_print("binload: can't open \"%s\"", filename);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *filename)
{
__int64 result; // rax
char ptr; // [rsp+Eh] [rbp-1Ah] BYREF
char v3; // [rsp+Fh] [rbp-19h]

if ( BINLOAD_bin_file )
{
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
BINLOAD_loading_basic = 0;
}
if ( Atari800_machine_type == 3 )
{
Log_print("binload: can't run Atari programs directly on the 5200");
result = 0LL;
}
else
{
BINLOAD_bin_file = fopen(filename, "rb");
if ( BINLOAD_bin_file )
{
if ( SIO_drive_status == 1 )
SIO_DisableDrive(1LL);
if ( fread(&ptr, 1uLL, 2uLL, BINLOAD_bin_file) != 2 )
goto LABEL_11;
if ( ptr == -1 )
{
if ( v3 != -1 )
goto LABEL_11;
BINLOAD_start_binloading = 1;
Atari800_Coldstart();
result = 1LL;
}
else if ( ptr )
{
if ( (unsigned __int8)(ptr - 48) > 9u )
goto LABEL_11;
BINLOAD_loading_basic = 2;
Devices_PatchOS();
Atari800_Coldstart();
result = 1LL;
}
else
{
if ( v3 )
{
LABEL_11:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
Log_print("binload: \"%s\" not recognized as a DOS or BASIC program", filename);
return 0LL;
}
BINLOAD_loading_basic = 1;
Devices_PatchOS();
Atari800_Coldstart();
result = 1LL;
}
}
else
{
Log_print("binload: can't open \"%s\"", filename);
result = 0LL;
}
}
return result;
}

original_code:
int BINLOAD_Loader(const char *filename)
{
 unsigned char buf[2];
 if (BINLOAD_bin_file != ((void *)0)) {
  fclose(BINLOAD_bin_file);
  BINLOAD_bin_file = ((void *)0);
  BINLOAD_loading_basic = 0;
 }
 if (Atari800_machine_type == 3) {
  Log_print("binload: can't run Atari programs directly on the 5200");
  return 0;
 }
 BINLOAD_bin_file = fopen(filename, "rb");
 if (BINLOAD_bin_file == ((void *)0)) {
  Log_print("binload: can't open \"%s\"", filename);
  return 0;
 }
 if (SIO_drive_status[0] == SIO_NO_DISK)
  SIO_DisableDrive(1);
 if (fread(buf, 1, 2, BINLOAD_bin_file) == 2) {
  if (buf[0] == 0xff && buf[1] == 0xff) {
   BINLOAD_start_binloading = 1;
   Atari800_Coldstart();
   return 1;
  }
  else if (buf[0] == 0 && buf[1] == 0) {
   BINLOAD_loading_basic = 1;
   Devices_PatchOS();
   Atari800_Coldstart();
   return 1;
  }
  else if (buf[0] >= '0' && buf[0] <= '9') {
   BINLOAD_loading_basic = 2;
   Devices_PatchOS();
   Atari800_Coldstart();
   return 1;
  }
 }
 fclose(BINLOAD_bin_file);
 BINLOAD_bin_file = ((void *)0);
 Log_print("binload: \"%s\" not recognized as a DOS or BASIC program", filename);
 return 0;
}

canonical_original_code:
int BINLOAD_Loader(char *filename)
{
 unsigned char buf[2];
 if (BINLOAD_bin_file != ((void *)0)) {
  fclose(BINLOAD_bin_file);
  BINLOAD_bin_file = ((void *)0);
  BINLOAD_loading_basic = 0;
 }
 if (Atari800_machine_type == 3) {
  Log_print("binload: can't run Atari programs directly on the 5200");
  return 0;
 }
 BINLOAD_bin_file = fopen(filename, "rb");
 if (BINLOAD_bin_file == ((void *)0)) {
  Log_print("binload: can't open \"%s\"", filename);
  return 0;
 }
 if (SIO_drive_status[0] == SIO_NO_DISK)
  SIO_DisableDrive(1);
 if (fread(buf, 1, 2, BINLOAD_bin_file) == 2) {
  if (buf[0] == 0xff && buf[1] == 0xff) {
   BINLOAD_start_binloading = 1;
   Atari800_Coldstart();
   return 1;
  }
  else if (buf[0] == 0 && buf[1] == 0) {
   BINLOAD_loading_basic = 1;
   Devices_PatchOS();
   Atari800_Coldstart();
   return 1;
  }
  else if (buf[0] >= '0' && buf[0] <= '9') {
   BINLOAD_loading_basic = 2;
   Devices_PatchOS();
   Atari800_Coldstart();
   return 1;
  }
 }
 fclose(BINLOAD_bin_file);
 BINLOAD_bin_file = ((void *)0);
 Log_print("binload: \"%s\" not recognized as a DOS or BASIC program", filename);
 return 0;
}


decompiled_code:
__int64 __fastcall hex2bcd(int a1)
{
return (a1 % 10) | (unsigned int)(16 * (a1 / 10));
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (a1 % 10) | (unsigned int)(16 * (a1 / 10));
}

original_code:
static int hex2bcd(int h)
{
 return ((h / 10) << 4) | (h % 10);
}

variable_types: h int


gettime

decompiled_code:
__int64 __fastcall gettime(int a1)
{
struct tm *v1; // rdx
__int64 result; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v1 = localtime(&timer);
switch ( a1 )
{
case 0:
result = hex2bcd(v1->tm_sec);
break;
case 1:
result = hex2bcd(v1->tm_min);
break;
case 2:
result = hex2bcd(v1->tm_hour);
break;
case 3:
result = hex2bcd(v1->tm_mday);
break;
case 4:
result = hex2bcd(v1->tm_mon + 1);
break;
case 5:
result = hex2bcd(v1->tm_year % 100);
break;
case 6:
result = hex2bcd((v1->tm_wday + 2) % 7 + 1);
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
struct tm *v1; // rdx
__int64 result; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v1 = localtime(&timer);
switch ( a1 )
{
case 0:
result = func0(v1->tm_sec);
break;
case 1:
result = func0(v1->tm_min);
break;
case 2:
result = func0(v1->tm_hour);
break;
case 3:
result = func0(v1->tm_mday);
break;
case 4:
result = func0(v1->tm_mon + 1);
break;
case 5:
result = func0(v1->tm_year % 100);
break;
case 6:
result = func0((v1->tm_wday + 2) % 7 + 1);
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
static int gettime(int p)
{
 time_t tt;
 struct tm *lt;
 tt = time(((void *)0));
 lt = localtime(&tt);
 switch (p) {
 case 0:
  return hex2bcd(lt->tm_sec);
 case 1:
  return hex2bcd(lt->tm_min);
 case 2:
  return hex2bcd(lt->tm_hour);
 case 3:
  return hex2bcd(lt->tm_mday);
 case 4:
  return hex2bcd(lt->tm_mon + 1);
 case 5:
  return hex2bcd(lt->tm_year % 100);
 case 6:
  return hex2bcd(((lt->tm_wday + 2) % 7) + 1);
 }
 return 0;
}

canonical_original_code:
static int gettime(int p)
{
 long tt;
 struct tm *lt;
 tt = time(((void *)0));
 lt = localtime(&tt);
 switch (p) {
 case 0:
  return hex2bcd(lt->tm_sec);
 case 1:
  return hex2bcd(lt->tm_min);
 case 2:
  return hex2bcd(lt->tm_hour);
 case 3:
  return hex2bcd(lt->tm_mday);
 case 4:
  return hex2bcd(lt->tm_mon + 1);
 case 5:
  return hex2bcd(lt->tm_year % 100);
 case 6:
  return hex2bcd(((lt->tm_wday + 2) % 7) + 1);
 }
 return 0;
}

variable_types: p int

variable_types: tt long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *