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

return_type: void


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
const char *v8; // rsi
__int64 v9; // rcx
char v10; // r11
bool v11; // cf
bool v12; // zf
const char *v13; // rsi
const char *v14; // rdi
__int64 v15; // rcx
__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 = a1;
v9 = 36LL;
do
{
if ( !v9 )
break;
v5 = *v8 < (unsigned int)*v7;
v6 = *v8++ == *v7++;
--v9;
}
while ( v6 );
v10 = (!v5 && !v6) - v5;
v11 = 0;
v12 = v10 == 0;
if ( v10 )
{
v13 = a1;
v14 = "COLOURS_PAL_ADJUST_EXTERNAL_PALETTE";
v15 = 36LL;
do
{
if ( !v15 )
break;
v11 = *v13 < (unsigned int)*v14;
v12 = *v13++ == *v14++;
--v15;
}
while ( v12 );
result = 0LL;
if ( (!v11 && !v12) == v11 )
{
v17 = Util_sscanbool(a2, v13);
COLOURS_PAL_external[1025] = v17;
result = v17 != -1;
}
}
else
{
v18 = Util_sscanbool(a2, v8);
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
const char *v8; // rsi
__int64 v9; // rcx
char v10; // r11
bool v11; // cf
bool v12; // zf
const char *v13; // rsi
const char *v14; // rdi
__int64 v15; // rcx
__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 = a1;
v9 = 36LL;
do
{
if ( !v9 )
break;
v5 = *v8 < (unsigned int)*v7;
v6 = *v8++ == *v7++;
--v9;
}
while ( v6 );
v10 = (!v5 && !v6) - v5;
v11 = 0;
v12 = v10 == 0;
if ( v10 )
{
v13 = a1;
v14 = "COLOURS_PAL_ADJUST_EXTERNAL_PALETTE";
v15 = 36LL;
do
{
if ( !v15 )
break;
v11 = *v13 < (unsigned int)*v14;
v12 = *v13++ == *v14++;
--v15;
}
while ( v12 );
result = 0LL;
if ( (!v11 && !v12) == v11 )
{
v17 = Util_sscanbool(a2, v13);
COLOURS_PAL_external[1025] = v17;
result = v17 != -1;
}
}
else
{
v18 = Util_sscanbool(a2, v8);
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 *

return_type: int


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 __int16)__ROL2__(word_542, 8) | ((unsigned __int8)byte_541 << 16) & 0x1F0000u) << 8,
0);
scsi_count = fread(&scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
return scsi_changephase(1);
}
if ( (unsigned __int8)scsi_buffer > 8u )
{
if ( scsi_buffer == 10 )
{
fseek(
PBI_SCSI_disk,
((unsigned __int16)__ROL2__(word_542, 8) | ((unsigned __int8)byte_541 << 16) & 0x1F0000u) << 8,
0);
result = scsi_changephase(2);
scsi_count = 256;
return result;
}
return scsi_changephase(0);
}
if ( scsi_buffer )
{
if ( scsi_buffer == 3 )
{
result = scsi_changephase(1);
scsi_buffer = 0;
scsi_count = 4;
return result;
}
return scsi_changephase(0);
}
result = scsi_changephase(4);
scsi_buffer = 0;
return result;
}

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

if ( scsi_buffer == 8 )
{
fseek(
PBI_SCSI_disk,
((unsigned __int16)__ROL2__(word_542, 8) | ((unsigned __int8)byte_541 << 16) & 0x1F0000u) << 8,
0);
scsi_count = fread(&scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
return scsi_changephase(1);
}
if ( (unsigned __int8)scsi_buffer > 8u )
{
if ( scsi_buffer == 10 )
{
fseek(
PBI_SCSI_disk,
((unsigned __int16)__ROL2__(word_542, 8) | ((unsigned __int8)byte_541 << 16) & 0x1F0000u) << 8,
0);
result = scsi_changephase(2);
scsi_count = 256;
return result;
}
return scsi_changephase(0);
}
if ( scsi_buffer )
{
if ( scsi_buffer == 3 )
{
result = scsi_changephase(1);
scsi_buffer = 0;
scsi_count = 4;
return result;
}
return scsi_changephase(0);
}
result = scsi_changephase(4);
scsi_buffer = 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
bool v2; // cc
int v3; // er9

result = (unsigned int)scsi_phase;
switch ( scsi_phase )
{
case 1:
result = (unsigned int)++scsi_bufpos;
if ( scsi_bufpos >= scsi_count )
{
LABEL_8:
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
return result;
}
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:
v3 = scsi_bufpos + 1;
scsi_buffer[scsi_bufpos] = scsi_byte;
scsi_bufpos = v3;
if ( v3 > 5 )
{
result = (unsigned __int64)scsi_process_command();
scsi_bufpos = 0;
}
break;
case 2:
v1 = scsi_bufpos + 1;
v2 = scsi_bufpos + 1 < scsi_count;
scsi_buffer[scsi_bufpos] = scsi_byte;
scsi_bufpos = v1;
if ( !v2 )
{
fwrite(scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
goto LABEL_8;
}
break;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func1()
{
unsigned __int64 result; // rax
int v1; // edx
bool v2; // cc
int v3; // er9

result = (unsigned int)scsi_phase;
switch ( scsi_phase )
{
case 1:
result = (unsigned int)++scsi_bufpos;
if ( scsi_bufpos >= scsi_count )
{
LABEL_8:
result = (unsigned __int64)scsi_changephase(4);
scsi_buffer[0] = 0;
return result;
}
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:
v3 = scsi_bufpos + 1;
scsi_buffer[scsi_bufpos] = scsi_byte;
scsi_bufpos = v3;
if ( v3 > 5 )
{
result = (unsigned __int64)func0();
scsi_bufpos = 0;
}
break;
case 2:
v1 = scsi_bufpos + 1;
v2 = scsi_bufpos + 1 < scsi_count;
scsi_buffer[scsi_bufpos] = scsi_byte;
scsi_bufpos = v1;
if ( !v2 )
{
fwrite(scsi_buffer, 1uLL, 0x100uLL, PBI_SCSI_disk);
goto LABEL_8;
}
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]);
}

return_type: unsigned char


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, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-20D0h] BYREF
char s[2]; // [rsp+20h] [rbp-20B8h] BYREF

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

canonical_decompiled_code:
int func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-20D0h] BYREF
char s[2]; // [rsp+20h] [rbp-20B8h] BYREF

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

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()
{
__int64 result; // rax
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");
result = 0xFFFFFFFFLL;
}
else
{
result = 0xFFFFFFFFLL;
CPU_regPC = MEMORY_mem[368];
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
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");
result = 0xFFFFFFFFLL;
}
else
{
result = 0xFFFFFFFFLL;
CPU_regPC = MEMORY_mem[368];
}
return result;
}

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:
void loader_cont()
{
int v0; // eax
__int16 v1; // bx
__int64 v2; // rbp
int v3; // eax
__int16 v4; // r15
__int16 v5; // bx
int v6; // eax
char v7; // r9
int v8; // eax
int v9; // edx
char v10; // r11
int v11; // eax
char v12; // si
int v13; // eax
char v14; // r10
int v15; // ebx
int v16; // eax
char v17; // bp
unsigned __int16 v18; // bp
int v19; // eax
char v20; // cl
unsigned __int16 v21; // bp
int v22; // eax
char v23; // r9
__int16 v24; // bx
bool v25; // zf
char v26; // di
char v27; // al
char v28; // al
__int16 v29; // cx
int v30; // [rsp+Ch] [rbp-3Ch]

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;
while ( 1 )
{
do
{
v0 = read_word();
v1 = v0;
}
while ( v0 == 0xFFFF );
if ( v0 < 0 )
return;
LODWORD(v2) = v0;
v3 = read_word();
v4 = v3;
if ( v3 < 0 )
return;
if ( BINLOAD_start_binloading )
{
MEMORY_mem[368] = v1;
BINLOAD_start_binloading = 0;
}
v5 = (v3 - v2) & 3;
if ( v5 )
{
v6 = fgetc(BINLOAD_bin_file);
if ( v6 == -1 )
goto LABEL_44;
v7 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v7 )
{
if ( v7 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v6);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v6;
}
v30 = v2 + 1;
v2 = (unsigned __int16)(v2 + 1);
if ( v5 != 1 )
{
if ( v5 != 2 )
{
v8 = fgetc(BINLOAD_bin_file);
if ( v8 == -1 )
goto LABEL_44;
v9 = v30;
v10 = *((_BYTE *)&MEMORY_attrib + v2);
if ( v10 )
{
if ( v10 == 2 )
{
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v8);
v9 = v30;
}
}
else
{
*((_BYTE *)MEMORY_mem + v2) = v8;
}
LODWORD(v2) = v9 + 1;
}
v11 = fgetc(BINLOAD_bin_file);
if ( v11 == -1 )
{
LABEL_44:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
v24 = MEMORY_mem[368];
v25 = HIBYTE(MEMORY_mem[369]) == 0xD7;
CPU_regPC = v24;
if ( !v25 )
{
v26 = CPU_regS;
v27 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + CPU_regS) = (unsigned __int16)(v24 - 1) >> 8;
CPU_regS = v26 - 2;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v27 - 1)) = v24 - 1;
CPU_regPC = MEMORY_mem[369];
}
return;
}
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;
}
LODWORD(v2) = v2 + 1;
}
}
v13 = fgetc(BINLOAD_bin_file);
if ( v13 == -1 )
goto LABEL_44;
while ( 1 )
{
v14 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( !v14 )
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v13;
goto LABEL_30;
}
if ( v14 == 2 )
break;
LABEL_30:
v15 = v2 + 1;
if ( (_WORD)v2 == v4 )
goto LABEL_50;
LABEL_31:
v16 = fgetc(BINLOAD_bin_file);
if ( v16 != -1 )
{
v17 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v15);
if ( v17 )
{
if ( v17 == 2 )
MEMORY_HwPutByte((unsigned __int16)v15, (unsigned __int8)v16);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v15) = v16;
}
v18 = v15 + 1;
v19 = fgetc(BINLOAD_bin_file);
if ( v19 != -1 )
{
v20 = *((_BYTE *)&MEMORY_attrib + v18);
if ( v20 )
{
if ( v20 == 2 )
MEMORY_HwPutByte(v18, (unsigned __int8)v19);
}
else
{
*((_BYTE *)MEMORY_mem + v18) = v19;
}
v21 = v15 + 2;
v22 = fgetc(BINLOAD_bin_file);
if ( v22 != -1 )
{
v23 = *((_BYTE *)&MEMORY_attrib + v21);
if ( v23 )
{
if ( v23 == 2 )
MEMORY_HwPutByte(v21, (unsigned __int8)v22);
}
else
{
*((_BYTE *)MEMORY_mem + v21) = v22;
}
LODWORD(v2) = v15 + 3;
v13 = fgetc(BINLOAD_bin_file);
if ( v13 != -1 )
continue;
}
}
}
goto LABEL_44;
}
v15 = v2 + 1;
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v13);
if ( (_WORD)v2 != v4 )
goto LABEL_31;
LABEL_50:
if ( HIBYTE(MEMORY_mem[369]) != 0xD7 )
{
--CPU_regS;
ESC_Add((unsigned int)CPU_regS + 256, 13LL, loader_cont);
v28 = CPU_regS;
v29 = MEMORY_mem[369];
LOBYTE(MEMORY_mem[384]) = 49;
CPU_regP |= 1u;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v28 - 1)) = 1;
CPU_regS = v28 - 3;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v28 - 2)) = v28 - 1;
CPU_regPC = v29;
return;
}
}
}
}

canonical_decompiled_code:
void func1()
{
int v0; // eax
__int16 v1; // bx
__int64 v2; // rbp
int v3; // eax
__int16 v4; // r15
__int16 v5; // bx
int v6; // eax
char v7; // r9
int v8; // eax
int v9; // edx
char v10; // r11
int v11; // eax
char v12; // si
int v13; // eax
char v14; // r10
int v15; // ebx
int v16; // eax
char v17; // bp
unsigned __int16 v18; // bp
int v19; // eax
char v20; // cl
unsigned __int16 v21; // bp
int v22; // eax
char v23; // r9
__int16 v24; // bx
bool v25; // zf
char v26; // di
char v27; // al
char v28; // al
__int16 v29; // cx
int v30; // [rsp+Ch] [rbp-3Ch]

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;
while ( 1 )
{
do
{
v0 = func0();
v1 = v0;
}
while ( v0 == 0xFFFF );
if ( v0 < 0 )
return;
LODWORD(v2) = v0;
v3 = func0();
v4 = v3;
if ( v3 < 0 )
return;
if ( BINLOAD_start_binloading )
{
MEMORY_mem[368] = v1;
BINLOAD_start_binloading = 0;
}
v5 = (v3 - v2) & 3;
if ( v5 )
{
v6 = fgetc(BINLOAD_bin_file);
if ( v6 == -1 )
goto LABEL_44;
v7 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( v7 )
{
if ( v7 == 2 )
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v6);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v6;
}
v30 = v2 + 1;
v2 = (unsigned __int16)(v2 + 1);
if ( v5 != 1 )
{
if ( v5 != 2 )
{
v8 = fgetc(BINLOAD_bin_file);
if ( v8 == -1 )
goto LABEL_44;
v9 = v30;
v10 = *((_BYTE *)&MEMORY_attrib + v2);
if ( v10 )
{
if ( v10 == 2 )
{
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v8);
v9 = v30;
}
}
else
{
*((_BYTE *)MEMORY_mem + v2) = v8;
}
LODWORD(v2) = v9 + 1;
}
v11 = fgetc(BINLOAD_bin_file);
if ( v11 == -1 )
{
LABEL_44:
fclose(BINLOAD_bin_file);
BINLOAD_bin_file = 0LL;
v24 = MEMORY_mem[368];
v25 = HIBYTE(MEMORY_mem[369]) == 0xD7;
CPU_regPC = v24;
if ( !v25 )
{
v26 = CPU_regS;
v27 = CPU_regS;
*((_BYTE *)&MEMORY_mem[128] + CPU_regS) = (unsigned __int16)(v24 - 1) >> 8;
CPU_regS = v26 - 2;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v27 - 1)) = v24 - 1;
CPU_regPC = MEMORY_mem[369];
}
return;
}
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;
}
LODWORD(v2) = v2 + 1;
}
}
v13 = fgetc(BINLOAD_bin_file);
if ( v13 == -1 )
goto LABEL_44;
while ( 1 )
{
v14 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v2);
if ( !v14 )
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v2) = v13;
goto LABEL_30;
}
if ( v14 == 2 )
break;
LABEL_30:
v15 = v2 + 1;
if ( (_WORD)v2 == v4 )
goto LABEL_50;
LABEL_31:
v16 = fgetc(BINLOAD_bin_file);
if ( v16 != -1 )
{
v17 = *((_BYTE *)&MEMORY_attrib + (unsigned __int16)v15);
if ( v17 )
{
if ( v17 == 2 )
MEMORY_HwPutByte((unsigned __int16)v15, (unsigned __int8)v16);
}
else
{
*((_BYTE *)MEMORY_mem + (unsigned __int16)v15) = v16;
}
v18 = v15 + 1;
v19 = fgetc(BINLOAD_bin_file);
if ( v19 != -1 )
{
v20 = *((_BYTE *)&MEMORY_attrib + v18);
if ( v20 )
{
if ( v20 == 2 )
MEMORY_HwPutByte(v18, (unsigned __int8)v19);
}
else
{
*((_BYTE *)MEMORY_mem + v18) = v19;
}
v21 = v15 + 2;
v22 = fgetc(BINLOAD_bin_file);
if ( v22 != -1 )
{
v23 = *((_BYTE *)&MEMORY_attrib + v21);
if ( v23 )
{
if ( v23 == 2 )
MEMORY_HwPutByte(v21, (unsigned __int8)v22);
}
else
{
*((_BYTE *)MEMORY_mem + v21) = v22;
}
LODWORD(v2) = v15 + 3;
v13 = fgetc(BINLOAD_bin_file);
if ( v13 != -1 )
continue;
}
}
}
goto LABEL_44;
}
v15 = v2 + 1;
MEMORY_HwPutByte((unsigned __int16)v2, (unsigned __int8)v13);
if ( (_WORD)v2 != v4 )
goto LABEL_31;
LABEL_50:
if ( HIBYTE(MEMORY_mem[369]) != 0xD7 )
{
--CPU_regS;
ESC_Add((unsigned int)CPU_regS + 256, 13LL, loader_cont);
v28 = CPU_regS;
v29 = MEMORY_mem[369];
LOBYTE(MEMORY_mem[384]) = 49;
CPU_regP |= 1u;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v28 - 1)) = 1;
CPU_regS = v28 - 3;
*((_BYTE *)&MEMORY_mem[128] + (unsigned __int8)(v28 - 2)) = v28 - 1;
CPU_regPC = v29;
return;
}
}
}
}

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


decompiled_code:
__int64 __fastcall BINLOAD_LoaderStart(_QWORD *a1)
{
*a1 = 0xDF2E47707000100LL;
ESC_Add(1798LL, 13LL, loader_cont);
return 67LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
*a1 = 0xDF2E47707000100LL;
ESC_Add(1798LL, 13LL, loader_cont);
return 67LL;
}

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 v1; // rdx
FILE *v2; // rcx
__int64 result; // rax
char ptr; // [rsp+Eh] [rbp-1Ah] BYREF
char v5; // [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");
v2 = BINLOAD_bin_file;
if ( BINLOAD_bin_file )
{
if ( SIO_drive_status == 1 )
{
SIO_DisableDrive(1LL, &SIO_drive_status, v1, BINLOAD_bin_file);
v2 = BINLOAD_bin_file;
}
if ( fread(&ptr, 1uLL, 2uLL, v2) != 2 )
goto LABEL_11;
if ( ptr == -1 )
{
if ( v5 != -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 ( v5 )
{
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 v1; // rdx
FILE *v2; // rcx
__int64 result; // rax
char ptr; // [rsp+Eh] [rbp-1Ah] BYREF
char v5; // [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");
v2 = BINLOAD_bin_file;
if ( BINLOAD_bin_file )
{
if ( SIO_drive_status == 1 )
{
SIO_DisableDrive(1LL, &SIO_drive_status, v1, BINLOAD_bin_file);
v2 = BINLOAD_bin_file;
}
if ( fread(&ptr, 1uLL, 2uLL, v2) != 2 )
goto LABEL_11;
if ( ptr == -1 )
{
if ( v5 != -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 ( v5 )
{
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; // rax
__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; // rax
__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; } *


decompiled_code:
__int64 __fastcall RTIME_Initialise(int *a1, __int64 a2)
{
int v4; // er15
int v5; // ebx
const char **v6; // rbp
const char *v7; // rdx
bool v8; // r11
bool v9; // cf
bool v10; // zf
__int64 v12; // rcx
const char *v13; // rsi
const char *v14; // rdi
__int64 v15; // rax
__int64 v16; // rdx
__int64 v17; // rcx

if ( *a1 > 1 )
{
v4 = 1;
v5 = 1;
while ( 1 )
{
while ( 1 )
{
v6 = (const char **)(a2 + 8LL * v5);
v7 = *v6;
if ( strcmp(*v6, "-rtime") )
break;
RTIME_enabled = 1;
LABEL_4:
if ( *a1 <= ++v5 )
goto LABEL_8;
}
v8 = strcmp(*v6, "-nortime") != 0;
v9 = 0;
v10 = !v8;
if ( v8 )
{
v12 = 6LL;
v13 = *v6;
v14 = "-help";
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v14;
v10 = *v13++ == *v14++;
--v12;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
Log_print("\t-rtime Enable R-Time 8 emulation", v13, v7, v12);
Log_print("\t-nortime Disable R-Time 8 emulation", v13, v16, v17);
v7 = *v6;
}
v15 = v4++;
*(_QWORD *)(a2 + 8 * v15) = v7;
goto LABEL_4;
}
++v5;
RTIME_enabled = 0;
if ( *a1 <= v5 )
goto LABEL_8;
}
}
v4 = 1;
LABEL_8:
*a1 = v4;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, __int64 a2)
{
int v4; // er15
int v5; // ebx
const char **v6; // rbp
const char *v7; // rdx
bool v8; // r11
bool v9; // cf
bool v10; // zf
__int64 v12; // rcx
const char *v13; // rsi
const char *v14; // rdi
__int64 v15; // rax
__int64 v16; // rdx
__int64 v17; // rcx

if ( *a1 > 1 )
{
v4 = 1;
v5 = 1;
while ( 1 )
{
while ( 1 )
{
v6 = (const char **)(a2 + 8LL * v5);
v7 = *v6;
if ( strcmp(*v6, "-rtime") )
break;
RTIME_enabled = 1;
LABEL_4:
if ( *a1 <= ++v5 )
goto LABEL_8;
}
v8 = strcmp(*v6, "-nortime") != 0;
v9 = 0;
v10 = !v8;
if ( v8 )
{
v12 = 6LL;
v13 = *v6;
v14 = "-help";
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v14;
v10 = *v13++ == *v14++;
--v12;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
Log_print("\t-rtime Enable R-Time 8 emulation", v13, v7, v12);
Log_print("\t-nortime Disable R-Time 8 emulation", v13, v16, v17);
v7 = *v6;
}
v15 = v4++;
*(_QWORD *)(a2 + 8 * v15) = v7;
goto LABEL_4;
}
++v5;
RTIME_enabled = 0;
if ( *a1 <= v5 )
goto LABEL_8;
}
}
v4 = 1;
LABEL_8:
*a1 = v4;
return 1LL;
}

original_code:
int RTIME_Initialise(int *argc, char *argv[])
{
 int i;
 int j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-rtime") == 0)
   RTIME_enabled = 1;
  else if (strcmp(argv[i], "-nortime") == 0)
   RTIME_enabled = 0;
  else {
   if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-rtime           Enable R-Time 8 emulation");
    Log_print("\t-nortime         Disable R-Time 8 emulation");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: i int

variable_types: j int


decompiled_code:
__int64 RTIME_GetByte()
{
__int64 result; // rax

if ( rtime_state == 1 )
{
rtime_state = 2;
if ( rtime_tmp <= 6 )
result = (unsigned int)((int)gettime(rtime_tmp) >> 4);
else
result = (int)regset[rtime_tmp] >> 4;
}
else if ( rtime_state == 2 )
{
rtime_state = 0;
if ( rtime_tmp <= 6 )
result = gettime(rtime_tmp) & 0xF;
else
result = regset[rtime_tmp] & 0xF;
}
else
{
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

if ( rtime_state == 1 )
{
rtime_state = 2;
if ( rtime_tmp <= 6 )
result = (unsigned int)((int)func1(rtime_tmp) >> 4);
else
result = (int)regset[rtime_tmp] >> 4;
}
else if ( rtime_state == 2 )
{
rtime_state = 0;
if ( rtime_tmp <= 6 )
result = func1(rtime_tmp) & 0xF;
else
result = regset[rtime_tmp] & 0xF;
}
else
{
result = 0LL;
}
return result;
}

original_code:
unsigned char RTIME_GetByte(void)
{
 switch (rtime_state) {
 case 0:
  return 0;
 case 1:
  rtime_state = 2;
  return (
   rtime_tmp <= 6 ?
   gettime(rtime_tmp) :
   regset[rtime_tmp]) >> 4;
 case 2:
  rtime_state = 0;
  return (
   rtime_tmp <= 6 ?
   gettime(rtime_tmp) :
   regset[rtime_tmp]) & 0x0f;
 }
 return 0;
}

canonical_original_code:
unsigned char RTIME_GetByte()
{
 switch (rtime_state) {
 case 0:
  return 0;
 case 1:
  rtime_state = 2;
  return (
   rtime_tmp <= 6 ?
   gettime(rtime_tmp) :
   regset[rtime_tmp]) >> 4;
 case 2:
  rtime_state = 0;
  return (
   rtime_tmp <= 6 ?
   gettime(rtime_tmp) :
   regset[rtime_tmp]) & 0x0f;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall RTIME_PutByte(unsigned __int8 a1)
{
__int64 result; // rax

result = (unsigned int)rtime_state;
switch ( rtime_state )
{
case 1:
rtime_state = 2;
rtime_tmp2 = 16 * a1;
break;
case 2:
rtime_state = 0;
regset[rtime_tmp] = rtime_tmp2 | a1 & 0xF;
break;
case 0:
rtime_state = 1;
rtime_tmp = a1 & 0xF;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
__int64 result; // rax

result = (unsigned int)rtime_state;
switch ( rtime_state )
{
case 1:
rtime_state = 2;
rtime_tmp2 = 16 * a1;
break;
case 2:
rtime_state = 0;
regset[rtime_tmp] = rtime_tmp2 | a1 & 0xF;
break;
case 0:
rtime_state = 1;
rtime_tmp = a1 & 0xF;
break;
}
return result;
}

original_code:
void RTIME_PutByte(unsigned char byte)
{
 switch (rtime_state) {
 case 0:
  rtime_tmp = byte & 0x0f;
  rtime_state = 1;
  break;
 case 1:
  rtime_tmp2 = byte << 4;
  rtime_state = 2;
  break;
 case 2:
  regset[rtime_tmp] = rtime_tmp2 | (byte & 0x0f);
  rtime_state = 0;
  break;
 }
}

variable_types:
byte
unsigned char


decompiled_code:
int __fastcall write32(unsigned int a1)
{
fputc((unsigned __int8)a1, sndoutput);
fputc(BYTE1(a1), sndoutput);
fputc(BYTE2(a1), sndoutput);
return fputc(HIBYTE(a1), sndoutput);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
fputc((unsigned __int8)a1, sndoutput);
fputc(BYTE1(a1), sndoutput);
fputc(BYTE2(a1), sndoutput);
return fputc(HIBYTE(a1), sndoutput);
}

original_code:
static void write32(long x)
{
 fputc(x & 0xff, sndoutput);
 fputc((x >> 8) & 0xff, sndoutput);
 fputc((x >> 16) & 0xff, sndoutput);
 fputc((x >> 24) & 0xff, sndoutput);
}

variable_types: x long


decompiled_code:
_BOOL8 SndSave_IsSoundFileOpen()
{
return sndoutput != 0LL;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return sndoutput != 0LL;
}

original_code:
int SndSave_IsSoundFileOpen(void)
{
 return sndoutput != ((void *)0);
}

canonical_original_code:
int SndSave_IsSoundFileOpen()
{
 return sndoutput != ((void *)0);
}


decompiled_code:
__int64 SndSave_CloseSoundFile()
{
unsigned int v0; // er12

v0 = 1;
if ( sndoutput )
{
if ( (byteswritten & 1) != 0 )
{
if ( putc(0, sndoutput) == -1 || fseek(sndoutput, 4LL, 0) )
goto LABEL_4;
}
else
{
LOBYTE(v0) = 0;
if ( fseek(sndoutput, 4LL, 0) )
goto LABEL_4;
}
write32(byteswritten + (char)v0 + 36);
if ( !fseek(sndoutput, 40LL, 0) )
{
v0 = 1;
write32(byteswritten);
goto LABEL_5;
}
LABEL_4:
v0 = 0;
LABEL_5:
fclose(sndoutput);
sndoutput = 0LL;
}
return v0;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // er12

v0 = 1;
if ( sndoutput )
{
if ( (byteswritten & 1) != 0 )
{
if ( putc(0, sndoutput) == -1 || fseek(sndoutput, 4LL, 0) )
goto LABEL_4;
}
else
{
LOBYTE(v0) = 0;
if ( fseek(sndoutput, 4LL, 0) )
goto LABEL_4;
}
func0(byteswritten + (char)v0 + 36);
if ( !fseek(sndoutput, 40LL, 0) )
{
v0 = 1;
func0(byteswritten);
goto LABEL_5;
}
LABEL_4:
v0 = 0;
LABEL_5:
fclose(sndoutput);
sndoutput = 0LL;
}
return v0;
}

original_code:
int SndSave_CloseSoundFile(void)
{
 int bSuccess = 1;
 char aligned = 0;
 if (sndoutput != ((void *)0)) {
  if (byteswritten & 1) {
   if (putc(0, sndoutput) == (-1))
    bSuccess = 0;
   else
    aligned = 1;
  }
  if (bSuccess) {
   if (fseek(sndoutput, 4, 0) != 0)
    bSuccess = 0;
   else {
    write32(byteswritten + 36 + aligned);
    if (fseek(sndoutput, 40, 0) != 0)
     bSuccess = 0;
    else {
     write32(byteswritten);
    }
   }
  }
  fclose(sndoutput);
  sndoutput = ((void *)0);
 }
 return bSuccess;
}

canonical_original_code:
int SndSave_CloseSoundFile()
{
 int bSuccess = 1;
 char aligned = 0;
 if (sndoutput != ((void *)0)) {
  if (byteswritten & 1) {
   if (putc(0, sndoutput) == (-1))
    bSuccess = 0;
   else
    aligned = 1;
  }
  if (bSuccess) {
   if (fseek(sndoutput, 4, 0) != 0)
    bSuccess = 0;
   else {
    write32(byteswritten + 36 + aligned);
    if (fseek(sndoutput, 40, 0) != 0)
     bSuccess = 0;
    else {
     write32(byteswritten);
    }
   }
  }
  fclose(sndoutput);
  sndoutput = ((void *)0);
 }
 return bSuccess;
}

variable_types: bSuccess int

variable_types: aligned char


decompiled_code:
__int64 __fastcall SndSave_OpenSoundFile(char *filename)
{
FILE *v1; // rax
__int64 result; // rax
int v3; // edi
int v4; // edi

SndSave_CloseSoundFile();
v1 = fopen(filename, "wb");
sndoutput = v1;
if ( !v1 )
return 0LL;
if ( fwrite("RIFF", 1uLL, 0x16uLL, v1) != 22 )
{
fclose(sndoutput);
sndoutput = 0LL;
return 0LL;
}
fputc(POKEYSND_num_pokeys, sndoutput);
fputc(0, sndoutput);
write32(POKEYSND_playback_freq);
v3 = POKEYSND_num_pokeys;
if ( (POKEYSND_snd_flags & 1) != 0 )
v3 = 2 * POKEYSND_num_pokeys;
write32(POKEYSND_playback_freq * v3);
v4 = POKEYSND_num_pokeys;
if ( (POKEYSND_snd_flags & 1) != 0 )
v4 = 2 * POKEYSND_num_pokeys;
fputc(v4, sndoutput);
fputc(0, sndoutput);
fputc((POKEYSND_snd_flags & 1) == 0 ? 8 : 16, sndoutput);
if ( fwrite(&unk_352, 1uLL, 9uLL, sndoutput) == 9 )
{
byteswritten = 0;
result = 1LL;
}
else
{
fclose(sndoutput);
result = 0LL;
sndoutput = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *filename)
{
FILE *v1; // rax
__int64 result; // rax
int v3; // edi
int v4; // edi

func2();
v1 = fopen(filename, "wb");
sndoutput = v1;
if ( !v1 )
return 0LL;
if ( fwrite("RIFF", 1uLL, 0x16uLL, v1) != 22 )
{
fclose(sndoutput);
sndoutput = 0LL;
return 0LL;
}
fputc(POKEYSND_num_pokeys, sndoutput);
fputc(0, sndoutput);
func0(POKEYSND_playback_freq);
v3 = POKEYSND_num_pokeys;
if ( (POKEYSND_snd_flags & 1) != 0 )
v3 = 2 * POKEYSND_num_pokeys;
func0(POKEYSND_playback_freq * v3);
v4 = POKEYSND_num_pokeys;
if ( (POKEYSND_snd_flags & 1) != 0 )
v4 = 2 * POKEYSND_num_pokeys;
fputc(v4, sndoutput);
fputc(0, sndoutput);
fputc((POKEYSND_snd_flags & 1) == 0 ? 8 : 16, sndoutput);
if ( fwrite(&unk_352, 1uLL, 9uLL, sndoutput) == 9 )
{
byteswritten = 0;
result = 1LL;
}
else
{
fclose(sndoutput);
result = 0LL;
sndoutput = 0LL;
}
return result;
}

original_code:
int SndSave_OpenSoundFile(const char *szFileName)
{
 SndSave_CloseSoundFile();
 sndoutput = fopen(szFileName, "wb");
 if (sndoutput == ((void *)0))
  return 0;
 if (fwrite("RIFF\0\0\0\0WAVEfmt \x10\0\0\0\1\0", 1, 22, sndoutput) != 22) {
  fclose(sndoutput);
  sndoutput = ((void *)0);
  return 0;
 }
 fputc(POKEYSND_num_pokeys, sndoutput);
 fputc(0, sndoutput);
 write32(POKEYSND_playback_freq);
 write32(POKEYSND_playback_freq * (POKEYSND_snd_flags & 1 ? POKEYSND_num_pokeys << 1 : POKEYSND_num_pokeys));
 fputc(POKEYSND_snd_flags & 1 ? POKEYSND_num_pokeys << 1 : POKEYSND_num_pokeys, sndoutput);
 fputc(0, sndoutput);
 fputc(POKEYSND_snd_flags & 1? 16: 8, sndoutput);
 if (fwrite("\0data\0\0\0\0", 1, 9, sndoutput) != 9) {
  fclose(sndoutput);
  sndoutput = ((void *)0);
  return 0;
 }
 byteswritten = 0;
 return 1;
}

canonical_original_code:
int SndSave_OpenSoundFile(char *szFileName)
{
 SndSave_CloseSoundFile();
 sndoutput = fopen(szFileName, "wb");
 if (sndoutput == ((void *)0))
  return 0;
 if (fwrite("RIFF\0\0\0\0WAVEfmt \x10\0\0\0\1\0", 1, 22, sndoutput) != 22) {
  fclose(sndoutput);
  sndoutput = ((void *)0);
  return 0;
 }
 fputc(POKEYSND_num_pokeys, sndoutput);
 fputc(0, sndoutput);
 write32(POKEYSND_playback_freq);
 write32(POKEYSND_playback_freq * (POKEYSND_snd_flags & 1 ? POKEYSND_num_pokeys << 1 : POKEYSND_num_pokeys));
 fputc(POKEYSND_snd_flags & 1 ? POKEYSND_num_pokeys << 1 : POKEYSND_num_pokeys, sndoutput);
 fputc(0, sndoutput);
 fputc(POKEYSND_snd_flags & 1? 16: 8, sndoutput);
 if (fwrite("\0data\0\0\0\0", 1, 9, sndoutput) != 9) {
  fclose(sndoutput);
  sndoutput = ((void *)0);
  return 0;
 }
 byteswritten = 0;
 return 1;
}

variable_types:
szFileName
char *


decompiled_code:
__int64 __fastcall SndSave_WriteToSoundFile(const void *a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // ebx
unsigned int v4; // eax

v2 = 0;
if ( !sndoutput )
return v2;
if ( !a1 || !a2 )
return 0LL;
v3 = 2 * a2;
if ( (POKEYSND_snd_flags & 1) == 0 )
v3 = a2;
v4 = fwrite(a1, 1uLL, v3, sndoutput);
byteswritten += v4;
v2 = v4;
if ( v4 == v3 )
return v2;
SndSave_CloseSoundFile();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const void *a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // ebx
unsigned int v4; // eax

v2 = 0;
if ( !sndoutput )
return v2;
if ( !a1 || !a2 )
return 0LL;
v3 = 2 * a2;
if ( (POKEYSND_snd_flags & 1) == 0 )
v3 = a2;
v4 = fwrite(a1, 1uLL, v3, sndoutput);
byteswritten += v4;
v2 = v4;
if ( v4 == v3 )
return v2;
func2();
return v2;
}

original_code:
int SndSave_WriteToSoundFile(const unsigned char *ucBuffer, unsigned int uiSize)
{
 if (sndoutput && ucBuffer && uiSize) {
  int result;
  if (POKEYSND_snd_flags & 1)
   uiSize <<= 1;
  result = fwrite(ucBuffer, 1, uiSize, sndoutput);
  byteswritten += result;
  if (result != uiSize) {
   SndSave_CloseSoundFile();
  }
  return result;
 }
 return 0;
}

canonical_original_code:
int SndSave_WriteToSoundFile(unsigned char *ucBuffer, unsigned int uiSize)
{
 if (sndoutput && ucBuffer && uiSize) {
  int result;
  if (POKEYSND_snd_flags & 1)
   uiSize <<= 1;
  result = fwrite(ucBuffer, 1, uiSize, sndoutput);
  byteswritten += result;
  if (result != uiSize) {
   SndSave_CloseSoundFile();
  }
  return result;
 }
 return 0;
}

variable_types:
ucBuffer
unsigned char *

variable_types:
uiSize
unsigned int

variable_types: result int


main

canonical_name: main

repo: cloudius-systems/glibc-testsuite

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rax
const char *v5; // rbp
char *v7; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

mallopt(-6, 42);
setbuf(stdout, 0LL);
while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "+", &options, 0LL);
if ( v3 == -1 )
break;
if ( v3 == 63 )
goto LABEL_15;
if ( v3 == 1001 )
test_dir = optarg;
}
v4 = getenv("TIMEOUTFACTOR");
if ( v4 )
{
endptr = v4;
strtoul(v4, &endptr, 0);
}
v5 = test_dir;
if ( test_dir )
{
setenv("TMPDIR", test_dir, 1);
if ( chdir(v5) < 0 )
{
perror("chdir");
LABEL_15:
exit(1);
}
}
else
{
v7 = getenv("TMPDIR");
test_dir = v7;
if ( !v7 || !*v7 )
test_dir = "/tmp";
}
setvbuf(stdout, 0LL, 2, 0LL);
atexit(delete_temp_files);
argv[optind - 1] = *argv;
return do_test();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rax
const char *v5; // rbp
char *v7; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

mallopt(-6, 42);
setbuf(stdout, 0LL);
while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "+", &options, 0LL);
if ( v3 == -1 )
break;
if ( v3 == 63 )
goto LABEL_15;
if ( v3 == 1001 )
test_dir = optarg;
}
v4 = getenv("TIMEOUTFACTOR");
if ( v4 )
{
endptr = v4;
strtoul(v4, &endptr, 0);
}
v5 = test_dir;
if ( test_dir )
{
setenv("TMPDIR", test_dir, 1);
if ( chdir(v5) < 0 )
{
perror("chdir");
LABEL_15:
exit(1);
}
}
else
{
v7 = getenv("TMPDIR");
test_dir = v7;
if ( !v7 || !*v7 )
test_dir = "/tmp";
}
setvbuf(stdout, 0LL, 2, 0LL);
func9(delete_temp_files);
argv[optind - 1] = *argv;
return func8();
}

original_code:
int
main (int argc, char *argv[])
{
  int direct = 1;
  int status;
  int opt;
  unsigned int timeoutfactor = 1;
  pid_t termpid;
  mallopt (-6, 42);
  while ((opt = getopt_long (argc, argv, "+", options, ((void *)0))) != -1)
    switch (opt)
      {
      case '?':
 exit (1);
      case 1000:
 direct = 1;
 break;
      case 1001:
 test_dir = optarg;
 break;
      }
  char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
  if (envstr_timeoutfactor != ((void *)0))
    {
      char *envstr_conv = envstr_timeoutfactor;
      unsigned long int env_fact;
      env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
      if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
 timeoutfactor = (((env_fact)>(1))?(env_fact):(1));
    }
  if (test_dir != ((void *)0))
    {
      setenv ("TMPDIR", test_dir, 1);
      if (chdir (test_dir) < 0)
 {
   perror ("chdir");
   exit (1);
 }
    }
  else
    {
      test_dir = getenv ("TMPDIR");
      if (test_dir == ((void *)0) || test_dir[0] == '\0')
 test_dir = "/tmp";
    }
  setvbuf (stdout, ((void *)0), 2, 0);
  atexit (delete_temp_files);
  argv[optind - 1] = argv[0];
  argv += optind - 1;
  argc -= optind - 1;
  if (direct)
    return do_test ();
  pid = fork ();
  if (pid == 0)
    {
      struct rlimit core_limit;
      core_limit.rlim_cur = 0;
      core_limit.rlim_max = 0;
      setrlimit (RLIMIT_CORE, &core_limit);
      struct rlimit data_limit;
      if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
 {
   if ((64 << 20) == ((__rlim_t) -1))
     data_limit.rlim_cur = data_limit.rlim_max;
   else if (data_limit.rlim_cur > (rlim_t) (64 << 20))
     data_limit.rlim_cur = ((((rlim_t) (64 << 20))<(data_limit.rlim_max))?((rlim_t) (64 << 20)):(data_limit.rlim_max));
   if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
     perror ("setrlimit: RLIMIT_DATA");
 }
      else
 perror ("getrlimit: RLIMIT_DATA");
      setpgid (0, 0);
      exit (do_test ());
    }
  else if (pid < 0)
    {
      perror ("Cannot fork test program");
      exit (1);
    }
  signal (14, signal_handler);
  alarm (2 * timeoutfactor);
  signal (2, signal_handler);
  termpid = (__extension__ ({ long int __result; do __result = (long int) (waitpid (pid, &status, 0)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  if (termpid == -1)
    {
      printf ("Waiting for test program failed: %m\n");
      exit (1);
    }
  if (termpid != pid)
    {
      printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
       (long int) pid, (long int) termpid);
      exit (1);
    }
  if (((status) & 0x7f) != 0)
    {
      if (0 != 0)
 {
   if (((status) & 0x7f) == 0)
     fprintf (stderr,
       "Expected signal '%s' from child, got none\n",
       strsignal (0));
   else
     fprintf (stderr,
       "Incorrect signal from child: got `%s', need `%s'\n",
       strsignal (((status) & 0x7f)),
       strsignal (0));
 }
      else
 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
   strsignal (((status) & 0x7f)));
      exit (1);
    }
  return (((status) & 0xff00) >> 8);
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int direct = 1;
  int status;
  int opt;
  unsigned int timeoutfactor = 1;
  int termpid;
  mallopt (-6, 42);
  while ((opt = getopt_long (argc, argv, "+", options, ((void *)0))) != -1)
    switch (opt)
      {
      case '?':
 exit (1);
      case 1000:
 direct = 1;
 break;
      case 1001:
 test_dir = optarg;
 break;
      }
  char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
  if (envstr_timeoutfactor != ((void *)0))
    {
      char *envstr_conv = envstr_timeoutfactor;
      unsigned long env_fact;
      env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
      if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
 timeoutfactor = (((env_fact)>(1))?(env_fact):(1));
    }
  if (test_dir != ((void *)0))
    {
      setenv ("TMPDIR", test_dir, 1);
      if (chdir (test_dir) < 0)
 {
   perror ("chdir");
   exit (1);
 }
    }
  else
    {
      test_dir = getenv ("TMPDIR");
      if (test_dir == ((void *)0) || test_dir[0] == '\0')
 test_dir = "/tmp";
    }
  setvbuf (stdout, ((void *)0), 2, 0);
  atexit (delete_temp_files);
  argv[optind - 1] = argv[0];
  argv += optind - 1;
  argc -= optind - 1;
  if (direct)
    return do_test ();
  pid = fork ();
  if (pid == 0)
    {
      struct rlimit core_limit;
      core_limit.rlim_cur = 0;
      core_limit.rlim_max = 0;
      setrlimit (RLIMIT_CORE, &core_limit);
      struct rlimit data_limit;
      if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
 {
   if ((64 << 20) == ((__rlim_t) -1))
     data_limit.rlim_cur = data_limit.rlim_max;
   else if (data_limit.rlim_cur > (rlim_t) (64 << 20))
     data_limit.rlim_cur = ((((rlim_t) (64 << 20))<(data_limit.rlim_max))?((rlim_t) (64 << 20)):(data_limit.rlim_max));
   if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
     perror ("setrlimit: RLIMIT_DATA");
 }
      else
 perror ("getrlimit: RLIMIT_DATA");
      setpgid (0, 0);
      exit (do_test ());
    }
  else if (pid < 0)
    {
      perror ("Cannot fork test program");
      exit (1);
    }
  signal (14, signal_handler);
  alarm (2 * timeoutfactor);
  signal (2, signal_handler);
  termpid = (__extension__ ({ long __result; do __result = (long) (waitpid (pid, &status, 0)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  if (termpid == -1)
    {
      printf ("Waiting for test program failed: %m\n");
      exit (1);
    }
  if (termpid != pid)
    {
      printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
       (long) pid, (long) termpid);
      exit (1);
    }
  if (((status) & 0x7f) != 0)
    {
      if (0 != 0)
 {
   if (((status) & 0x7f) == 0)
     fprintf (stderr,
       "Expected signal '%s' from child, got none\n",
       strsignal (0));
   else
     fprintf (stderr,
       "Incorrect signal from child: got `%s', need `%s'\n",
       strsignal (((status) & 0x7f)),
       strsignal (0));
 }
      else
 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
   strsignal (((status) & 0x7f)));
      exit (1);
    }
  return (((status) & 0xff00) >> 8);
}

variable_types: argc int

variable_types: direct int

variable_types: status int

variable_types: opt int

variable_types:
timeoutfactor
unsigned int

variable_types: termpid int

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }


decompiled_code:
_QWORD *delete_temp_files()
{
_QWORD *result; // rax

for ( result = temp_name_list; result; temp_name_list = result )
{
remove((const char *)result[3]);
result = (_QWORD *)*temp_name_list;
}
return result;
}

canonical_decompiled_code:
_QWORD *func6()
{
_QWORD *result; // rax

for ( result = temp_name_list; result; temp_name_list = result )
{
remove((const char *)result[3]);
result = (_QWORD *)*temp_name_list;
}
return result;
}

original_code:
static void
delete_temp_files (void)
{
  while (temp_name_list != ((void *)0))
    {
      remove (temp_name_list->name);
      temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
    }
}

canonical_original_code:
static void delete_temp_files()
{
  while (temp_name_list != ((void *)0))
    {
      remove (temp_name_list->name);
      temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
    }
}

user_defined_types: struct temp_name_list { struct qelem q; char *name; }

user_defined_types: struct qelem { struct qelem *q_forw; struct qelem *q_back; char q_data[1]; }


decompiled_code:
__int64 do_test()
{
unsigned int v0; // er12
clockid_t clock_id[3]; // [rsp+Ch] [rbp-Ch] BYREF

v0 = clock_test(0);
if ( clock_getcpuclockid(0, clock_id) )
puts("CPU clock unavailble, skipping test");
else
clock_test(clock_id[0]);
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // er12
clockid_t clock_id[3]; // [rsp+Ch] [rbp-Ch] BYREF

v0 = func7(0);
if ( clock_getcpuclockid(0, clock_id) )
puts("CPU clock unavailble, skipping test");
else
func7(clock_id[0]);
return v0;
}

original_code:
static int
do_test (void)
{
  pthread_mutex_t m;
  int e = pthread_mutex_init (&m, ((void *)0));
  if (((void *)0) != ((void *)0) && e == 95)
    {
      puts ("cannot support selected type of mutexes");
      return 0;
    }
  else if (e != 0)
    {
      puts ("mutex_init failed");
      return 1;
    }
  if (((void *)0) != ((void *)0) && pthread_mutexattr_destroy (((void *)0)) != 0)
    {
      puts ("mutexattr_destroy failed");
      return 1;
    }
  if (pthread_mutex_lock (&m) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }
  if (pthread_mutex_unlock (&m) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }
  if (pthread_mutex_destroy (&m) != 0)
    {
      puts ("mutex_destroy failed");
      return 1;
    }
  return 0;
}

canonical_original_code:
static int do_test()
{
  union pthread_mutex_t m;
  int e = pthread_mutex_init (&m, ((void *)0));
  if (((void *)0) != ((void *)0) && e == 95)
    {
      puts ("cannot support selected type of mutexes");
      return 0;
    }
  else if (e != 0)
    {
      puts ("mutex_init failed");
      return 1;
    }
  if (((void *)0) != ((void *)0) && pthread_mutexattr_destroy (((void *)0)) != 0)
    {
      puts ("mutexattr_destroy failed");
      return 1;
    }
  if (pthread_mutex_lock (&m) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }
  if (pthread_mutex_unlock (&m) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }
  if (pthread_mutex_destroy (&m) != 0)
    {
      puts ("mutex_destroy failed");
      return 1;
    }
  return 0;
}

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; }

variable_types: e int

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int *v3; // rax
int *v4; // rbp
void *v5; // rbp
void *v6; // rbp
void *v7; // rbp

v3 = __errno_location();
*v3 = 0;
v4 = v3;
if ( malloc(0xFFFFFFFFFFFFFFFFLL) )
{
merror("malloc (-1) succeeded.", argv);
}
else if ( *v4 != 12 )
{
merror("errno is not set correctly", argv);
}
v5 = malloc(0xAuLL);
if ( !v5 )
merror("malloc (10) failed.", argv);
if ( realloc(v5, 0LL) )
merror("realloc (p, 0) failed.", 0LL);
v6 = malloc(0LL);
if ( !v6 )
merror("malloc (0) failed.", 0LL);
if ( realloc(v6, 0LL) )
merror("realloc (p, 0) failed.", 0LL);
v7 = malloc(0x80400uLL);
if ( !v7 )
merror("malloc (513K) failed.", 0LL);
if ( malloc(0xFFFFFFFFFFF80000LL) )
merror("malloc (-512K) succeeded.", 0LL);
free(v7);
return errors != 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int *v3; // rax
int *v4; // rbp
void *v5; // rbp
void *v6; // rbp
void *v7; // rbp

v3 = __errno_location();
*v3 = 0;
v4 = v3;
if ( malloc(0xFFFFFFFFFFFFFFFFLL) )
{
func6("malloc (-1) succeeded.", argv);
}
else if ( *v4 != 12 )
{
func6("errno is not set correctly", argv);
}
v5 = malloc(0xAuLL);
if ( !v5 )
func6("malloc (10) failed.", argv);
if ( realloc(v5, 0LL) )
func6("realloc (p, 0) failed.", 0LL);
v6 = malloc(0LL);
if ( !v6 )
func6("malloc (0) failed.", 0LL);
if ( realloc(v6, 0LL) )
func6("realloc (p, 0) failed.", 0LL);
v7 = malloc(0x80400uLL);
if ( !v7 )
func6("malloc (513K) failed.", 0LL);
if ( malloc(0xFFFFFFFFFFF80000LL) )
func6("malloc (-512K) succeeded.", 0LL);
free(v7);
return errors != 0;
}

variable_types:
argv
char *[]

user_defined_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }


repo: cloudsec/wos

decompiled_code:
__int64 __fastcall set_gdt_desc(__int64 a1, int a2, int a3, int a4, int a5)
{
return _set_gdt_desc(a1, a2, a3, 12, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3, int a4, int a5)
{
return func0(a1, a2, a3, 12, a4, a5);
}

original_code:
void set_gdt_desc(struct gdt_desc *addr, unsigned int base, unsigned int limit,
                int type, int idx)
{
        __set_gdt_desc(addr, base, limit, 0xc, type, idx);
}

variable_types: struct gdt_desc { unsigned int a; unsigned int b; } *

variable_types:
base
unsigned int

variable_types:
limit
unsigned int

variable_types: type int

variable_types: idx int


decompiled_code:
__int64 __fastcall set_ldt_desc(__int64 a1, int a2, int a3, int a4, int a5)
{
return _set_gdt_desc(a1, a2, a3, 0, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int a3, int a4, int a5)
{
return func0(a1, a2, a3, 0, a4, a5);
}

original_code:
void set_ldt_desc(struct gdt_desc *addr, unsigned int base, unsigned int limit,
                int type, int idx)
{
        __set_gdt_desc(addr, base, limit, 0x0, type, idx);
}

user_defined_types: struct gdt_desc { unsigned int a; unsigned int b; }


decompiled_code:
// attributes: thunk
__int64 __fastcall set_tss_desc(__int64 a1, int a2, int a3, int a4, int a5)
{
return set_ldt_desc(a1, a2, a3, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, int a3, int a4, int a5)
{
return func2(a1, a2, a3, a4, a5);
}

original_code:
void set_tss_desc(struct gdt_desc *addr, unsigned int base, unsigned int limit,
                int type, int idx)
{
        __set_gdt_desc(addr, base, limit, 0x0, type, idx);
}


original_code:
void setup_gdt(void)
{
        set_gdt_desc(new_gdt, 0x0, 0xffffffff, 0x9a, 0x1);
        set_gdt_desc(new_gdt, 0x0, 0xffffffff, 0x92, 0x2);
        set_gdt_desc(new_gdt, 0xb8000, 0xffff, 0xf2, 0x3);
}

canonical_original_code:
void setup_gdt()
{
        set_gdt_desc(new_gdt, 0x0, 0xffffffff, 0x9a, 0x1);
        set_gdt_desc(new_gdt, 0x0, 0xffffffff, 0x92, 0x2);
        set_gdt_desc(new_gdt, 0xb8000, 0xffff, 0xf2, 0x3);
}


decompiled_code:
__int64 print_init_ldt_list()
{
unsigned int *v0; // rbx
__int64 v1; // rdx
__int64 v2; // rsi
__int64 result; // rax

v0 = (unsigned int *)&init_task;
printk("\nInit init_task ldt table:\n");
do
{
v1 = v0[28];
v2 = v0[29];
v0 += 2;
result = printk("0x%x, 0x%x\n", v2, v1);
}
while ( v0 != (unsigned int *)((char *)&init_task + 24) );
return result;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int *v0; // rbx
__int64 v1; // rdx
__int64 v2; // rsi
__int64 result; // rax

v0 = (unsigned int *)&init_task;
printk("\nInit init_task ldt table:\n");
do
{
v1 = v0[28];
v2 = v0[29];
v0 += 2;
result = printk("0x%x, 0x%x\n", v2, v1);
}
while ( v0 != (unsigned int *)((char *)&init_task + 24) );
return result;
}

original_code:
void print_init_ldt_list(void)
{
        int i = 0;
        printk("\nInit init_task ldt table:\n");
        for (i = 0; i < 3; i++)
                printk("0x%x, 0x%x\n", init_task.ldt[i].b, init_task.ldt[i].a);
}

canonical_original_code:
void print_init_ldt_list()
{
        int i = 0;
        printk("\nInit init_task ldt table:\n");
        for (i = 0; i < 3; i++)
                printk("0x%x, 0x%x\n", init_task.ldt[i].b, init_task.ldt[i].a);
}

variable_types: i int


decompiled_code:
__int64 print_gdt_list()
{
unsigned int *v0; // rbx
__int64 v1; // rdx
__int64 v2; // rsi
__int64 result; // rax

v0 = (unsigned int *)&new_gdt;
printk("\nGdt Table:\n");
do
{
v1 = v0[2];
v2 = v0[3];
v0 += 2;
result = printk("0x%x, 0x%x\n", v2, v1);
}
while ( v0 != (unsigned int *)((char *)&new_gdt + 24) );
return result;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int *v0; // rbx
__int64 v1; // rdx
__int64 v2; // rsi
__int64 result; // rax

v0 = (unsigned int *)&new_gdt;
printk("\nGdt Table:\n");
do
{
v1 = v0[2];
v2 = v0[3];
v0 += 2;
result = printk("0x%x, 0x%x\n", v2, v1);
}
while ( v0 != (unsigned int *)((char *)&new_gdt + 24) );
return result;
}

original_code:
void print_gdt_list(void)
{
        int i = 0;
        printk("\nGdt Table:\n");
        for (i = 1; i <= 3; i++)
                printk("0x%x, 0x%x\n", new_gdt[i].b, new_gdt[i].a);
}

canonical_original_code:
void print_gdt_list()
{
        int i = 0;
        printk("\nGdt Table:\n");
        for (i = 1; i <= 3; i++)
                printk("0x%x, 0x%x\n", new_gdt[i].b, new_gdt[i].a);
}


itoa

original_code:
char *itoa(int num, char *str, int radix)
{
        char string[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        char *s = str, *p;
        char tmp;
        if (num < 0)
                return (void *)0;
        if (!num) {
                *s++ = '\0';
                *s = 0;
                return s;
        }
        while (num) {
                *s++ = string[num % radix];
                num /= radix;
        }
        *s = 0;
        --s;
        for (p = str; p < s; s--, p++) {
                tmp = *p;
                *p = *s;
                *s = tmp;
        }
        return s;
}

canonical_original_code:
char *itoa(int num, char *str, int radix)
{
        char string[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        char *s = str; char *p;
        char tmp;
        if (num < 0)
                return (void *)0;
        if (!num) {
                *s++ = '\0';
                *s = 0;
                return s;
        }
        while (num) {
                *s++ = string[num % radix];
                num /= radix;
        }
        *s = 0;
        --s;
        for (p = str; p < s; s--, p++) {
                tmp = *p;
                *p = *s;
                *s = tmp;
        }
        return s;
}

variable_types: num int

variable_types:
str
char *

variable_types: radix int

variable_types:
string
char []

variable_types:
s
char *

variable_types:
p
char *

variable_types: tmp char

return_type: char *


strlen

decompiled_code:
__int64 __fastcall strlen(__int64 a1)
{
__int64 v1; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = a1;
do
++v1;
while ( *(_BYTE *)(v1 - 1) );
return (unsigned int)(v1 - a1 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = a1;
do
++v1;
while ( *(_BYTE *)(v1 - 1) );
return (unsigned int)(v1 - a1 - 1);
}

original_code:
int strlen(char *str)
{
 char *s = str;
 if (!s)
  return -1;
 while (*s++);
 return (int)(s - str - 1);
}


strcmp

decompiled_code:
__int64 __fastcall strcmp(char *a1, char *a2)
{
char v2; // dl
char v3; // cl
__int64 v4; // rax
__int64 result; // rax

v2 = *a1;
v3 = *a2;
if ( *a2 == *a1 )
{
v4 = 1LL;
do
{
v2 = a1[v4++];
v3 = a2[v4 - 1];
}
while ( v2 == v3 );
}
result = 1LL;
if ( v3 >= v2 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, char *a2)
{
char v2; // dl
char v3; // cl
__int64 v4; // rax
__int64 result; // rax

v2 = *a1;
v3 = *a2;
if ( *a2 == *a1 )
{
v4 = 1LL;
do
{
v2 = a1[v4++];
v3 = a2[v4 - 1];
}
while ( v2 == v3 );
}
result = 1LL;
if ( v3 >= v2 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int strcmp(char *src, char *dst)
{
 char *s = src, *p = dst;
 while (*s == *p) {
  s++;p++;
 }
 if (*s == *p)
  return 0;
 if (*s > *p)
  return 1;
 if (*s < *p)
  return -1;
}

canonical_original_code:
int strcmp(char *src, char *dst)
{
 char *s = src; char *p = dst;
 while (*s == *p) {
  s++;p++;
 }
 if (*s == *p)
  return 0;
 if (*s > *p)
  return 1;
 if (*s < *p)
  return -1;
}

variable_types:
src
char *

variable_types:
dst
char *


strncmp

decompiled_code:
__int64 __fastcall strncmp(char *a1, char *a2, int a3)
{
char v4; // cl
char v5; // dl
__int64 v6; // r8
__int64 v7; // rax
__int64 result; // rax

v4 = *a1;
v5 = *a2;
if ( *a2 == *a1 )
{
if ( a3 <= 0 )
return 0LL;
v6 = (unsigned int)(a3 + 1);
v7 = 1LL;
while ( 1 )
{
v4 = a1[v7];
v5 = a2[v7];
if ( v4 != v5 )
break;
if ( v6 == ++v7 )
return 0LL;
}
}
result = 1LL;
if ( v4 <= v5 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, char *a2, int a3)
{
char v4; // cl
char v5; // dl
__int64 v6; // r8
__int64 v7; // rax
__int64 result; // rax

v4 = *a1;
v5 = *a2;
if ( *a2 == *a1 )
{
if ( a3 <= 0 )
return 0LL;
v6 = (unsigned int)(a3 + 1);
v7 = 1LL;
while ( 1 )
{
v4 = a1[v7];
v5 = a2[v7];
if ( v4 != v5 )
break;
if ( v6 == ++v7 )
return 0LL;
}
}
result = 1LL;
if ( v4 <= v5 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int strncmp(char *src, char *dst, int n)
{
        char *s = src, *p = dst;
 int i = 0;
        while ((*s == *p) && i < n) {
                s++;p++;i++;
        }
        if (*s == *p)
                return 0;
        if (*s > *p)
                return 1;
        if (*s < *p)
                return -1;
}

canonical_original_code:
int strncmp(char *src, char *dst, int n)
{
        char *s = src; char *p = dst;
 int i = 0;
        while ((*s == *p) && i < n) {
                s++;p++;i++;
        }
        if (*s == *p)
                return 0;
        if (*s > *p)
                return 1;
        if (*s < *p)
                return -1;
}

variable_types: n int


memcpy

original_code:
void *memcpy(void *dst, void *src, int n)
{
        char *s = src, *p = dst;
        int i = 0;
        while (i < n) {
                *p++ = *s++;
                i++;
        }
}

canonical_original_code:
void *memcpy(void *dst, void *src, int n)
{
        char *s = src; char *p = dst;
        int i = 0;
        while (i < n) {
                *p++ = *s++;
                i++;
        }
}

variable_types:
dst
void *

variable_types:
src
void *

return_type: void *


memset

decompiled_code:
__int64 __fastcall memset(__int64 a1, __int64 a2, int a3)
{
while ( a3 > 0 )
;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3)
{
while ( a3 > 0 )
;
return 0LL;
}

original_code:
void *memset(void *s, int c, int n)
{
 int i;
 char *p = s;
 for (i = 0; i < n; i++)
  *p++ = c;
}

variable_types:
s
void *

variable_types: c int


decompiled_code:
__int64 __fastcall sys_write_s(__int64 a1)
{
return printk(&unk_3A, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return printk(&unk_3A, a1);
}

original_code:
int sys_write_s(char *msg)
{
 printk("%s", msg);
}

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall sys_write_i(unsigned int a1)
{
return printk("!%d\n", a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return printk("!%d\n", a1);
}

original_code:
int sys_write_i(int msg)
{
 printk("!%d\n", msg);
}

variable_types: msg int


decompiled_code:
int __fastcall sys_printf(FILE *a1, __int64 a2, void *a3)
{
char format[8]; // [rsp+10h] [rbp-8h] BYREF

return vfprintf(a1, format, a3);
}

canonical_decompiled_code:
int __fastcall func2(FILE *a1, __int64 a2, void *a3)
{
char format[8]; // [rsp+10h] [rbp-8h] BYREF

return vfprintf(a1, format, a3);
}

original_code:
int sys_printf(char *format, ...)
{
 char* arg;
 (arg = (char *)&format + sizeof(format));
 return vfprintf(format, arg);
}

canonical_original_code:
int sys_printf(char *format, ...)
{
 char *arg;
 (arg = (char *)&format + sizeof(format));
 return vfprintf(format, arg);
}

variable_types:
format
char *

variable_types:
arg
char *


decompiled_code:
void __fastcall add_timer(int a1, void (*a2)(void))
{
int v2; // eax
__int64 v3; // r12

if ( a2 )
{
if ( a1 )
{
_disable();
v2 = kmalloc(32LL);
if ( v2 )
{
v3 = v2;
printk("allocte new_timer struct at 0x%x\n", v2);
*(_DWORD *)v3 = a1;
*(_QWORD *)(v3 + 8) = a2;
list_add_tail_constprop_0((__int64 *)(v3 + 16));
}
}
else
{
a2();
}
_enable();
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, void (*a2)(void))
{
int v2; // eax
__int64 v3; // r12

if ( a2 )
{
if ( a1 )
{
_disable();
v2 = kmalloc(32LL);
if ( v2 )
{
v3 = v2;
printk("allocte new_timer struct at 0x%x\n", v2);
*(_DWORD *)v3 = a1;
*(_QWORD *)(v3 + 8) = a2;
func3((__int64 *)(v3 + 16));
}
}
else
{
a2();
}
_enable();
}
}

original_code:
void add_timer(unsigned int jiffies, void (*fn)(void))
{
 struct timer_list *new_timer = (void *)0;
 if (!fn)
  return ;
 if (jiffies <= 0) {
  fn();
  goto out;
 }
 asm("cli\n"::);
 new_timer = (struct timer_list *)kmalloc(sizeof(struct timer_list));
 if (!new_timer)
  goto out;
 printk("allocte new_timer struct at 0x%x\n", new_timer);
 new_timer->jiffies = jiffies;
 new_timer->fn = fn;
 list_add_tail(&(new_timer->list), &timer_list_head);
out:
 asm("sti\n"::);
 return ;
}

canonical_original_code:
void add_timer(unsigned int jiffies, void (*fn)())
{
 struct timer_list *new_timer = (void *)0;
 if (!fn)
  return ;
 if (jiffies <= 0) {
  fn();
  goto out;
 }
 asm("cli\n"::);
 new_timer = (struct timer_list *)kmalloc(sizeof(struct timer_list));
 if (!new_timer)
  goto out;
 printk("allocte new_timer struct at 0x%x\n", new_timer);
 new_timer->jiffies = jiffies;
 new_timer->fn = fn;
 list_add_tail(&(new_timer->list), &timer_list_head);
out:
 asm("sti\n"::);
 return ;
}

variable_types:
jiffies
unsigned int

variable_types:
fn
void (*)()

variable_types: struct timer_list { unsigned int jiffies; void (*fn)(); struct list_head { struct list_head *prev; } list; } *

user_defined_types: struct timer_list { unsigned int jiffies; void (*fn)(); struct list_head list; }


decompiled_code:
__int64 do_timer()
{
__int64 *i; // rbx
__int64 result; // rax

for ( i = (__int64 *)*(&timer_list_head + 1); i != &timer_list_head; i = (__int64 *)i[1] )
{
if ( (*((_DWORD *)i - 4))-- == 1 )
{
((void (*)(void))*(i - 1))();
list_del_isra_0(*i, (_QWORD *)i[1]);
}
}
++timer_tick;
if ( !current
|| (result = (unsigned int)(*(_DWORD *)(current + 144) - 1), *(_DWORD *)(current + 144) = result, (int)result <= 0) )
{
result = schedule();
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 *i; // rbx
__int64 result; // rax

for ( i = (__int64 *)*(&timer_list_head + 1); i != &timer_list_head; i = (__int64 *)i[1] )
{
if ( (*((_DWORD *)i - 4))-- == 1 )
{
((void (*)(void))*(i - 1))();
func4(*i, (_QWORD *)i[1]);
}
}
++timer_tick;
if ( !current
|| (result = (unsigned int)(*(_DWORD *)(current + 144) - 1), *(_DWORD *)(current + 144) = result, (int)result <= 0) )
{
result = schedule();
}
return result;
}

original_code:
void do_timer(unsigned int esp)
{
 struct regs *reg = (struct regs *)esp;
 struct timer_list *s = (void *)0;
 struct list_head *p = (void *)0;
 for (p = (&timer_list_head)->next; p != (&timer_list_head); p = p->next) {
  s = ({ (struct timer_list *)((char * )p - (char *)&(((struct timer_list *)0x0)->list)); });;
  if (s && --(s->jiffies) <= 0) {
   s->fn();
   list_del(p);
  }
 }
 timer_tick++;
 if (current && --current->counter > 0)
  return ;
 ("pid: %d counter is 0.\n", current->pid);
 schedule();
}

variable_types:
esp
unsigned int

variable_types: struct regs { unsigned int ebx; unsigned int ecx; unsigned int edx; unsigned int esi; unsigned int edi; unsigned int ebp; unsigned int eax; unsigned int ds; unsigned int es; unsigned int fs; unsigned int error_code; unsigned int orig_eip; unsigned int orig_cs; unsigned int eflags; unsigned int esp; unsigned int ss; } *

variable_types: struct list_head { struct list_head *prev; } *


decompiled_code:
__int64 __fastcall init_timer(int a1)
{
unsigned __int8 v1; // al
__int64 result; // rax

*(&timer_list_head + 1) = (__int64)&timer_list_head;
timer_list_head = (__int64)&timer_list_head;
__outbyte(0x43u, 0x36u);
__outbyte(0x40u, 1193180 / a1);
__outbyte(0x40u, (unsigned __int16)(1193180 / a1) >> 8);
v1 = __inbyte(0x21u);
result = v1 & 0xFE;
__outbyte(0x21u, result);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
unsigned __int8 v1; // al
__int64 result; // rax

*(&timer_list_head + 1) = (__int64)&timer_list_head;
timer_list_head = (__int64)&timer_list_head;
__outbyte(0x43u, 0x36u);
__outbyte(0x40u, 1193180 / a1);
__outbyte(0x40u, (unsigned __int16)(1193180 / a1) >> 8);
v1 = __inbyte(0x21u);
result = v1 & 0xFE;
__outbyte(0x21u, result);
return result;
}

original_code:
void init_timer(int hz)
{
        unsigned int divisor = 1193180 / hz;
 do { (&timer_list_head)->next = (&timer_list_head); (&timer_list_head)->prev = (&timer_list_head); }while (0);
        asm("outb %%al, %%dx\n"::"a"(0x36),"d"(0x43));
        asm("outb %%al, %%dx\n"::"a"(divisor&0xff),"d"(0x40));
        asm("outb %%al, %%dx\n"::"a"(divisor>>8),"d"(0x40));
        asm("outb %%al, %%dx\n"::"a"(({ unsigned char ret; asm("inb %%dx, %%al\n":"=a"(ret):"d"(0x21)); ret; })&0xfe),"d"(0x21));
}

variable_types: hz int

variable_types:
divisor
unsigned int

variable_types:
ret
unsigned char


decompiled_code:
void timer_test()
{
add_timer(500, (void (*)(void))_timer_test);
add_timer(500, (void (*)(void))_timer_test1);
add_timer(200, (void (*)(void))_timer_test2);
}

canonical_decompiled_code:
void func8()
{
func5(500, (void (*)(void))_timer_test);
func5(500, (void (*)(void))_timer_test1);
func5(200, (void (*)(void))_timer_test2);
}

original_code:
void timer_test(void)
{
 add_timer(500, __timer_test);
 add_timer(500, __timer_test1);
 add_timer(200, __timer_test2);
}

canonical_original_code:
void timer_test()
{
 add_timer(500, __timer_test);
 add_timer(500, __timer_test1);
 add_timer(200, __timer_test2);
}


decompiled_code:
__int64 get_pid()
{
if ( last_pid > 1024 )
return 0xFFFFFFFFLL;
return (unsigned int)++last_pid;
}

canonical_decompiled_code:
__int64 func1()
{
if ( last_pid > 1024 )
return 0xFFFFFFFFLL;
return (unsigned int)++last_pid;
}

original_code:
int get_pid(void)
{
 int pid;
 if (last_pid > 1024)
  return -1;
 pid = ++last_pid;
 return pid;
}

canonical_original_code:
int get_pid()
{
 int pid;
 if (last_pid > 1024)
  return -1;
 pid = ++last_pid;
 return pid;
}

variable_types: pid int


original_code:
int do_fork(unsigned int esp)
{
 struct task_struct *tsk;
 struct regs *reg = (struct regs *)(esp + 4);
 int pid;
 ("cs: 0x%x, eip: 0x%x, ds: 0x%x, eax: 0x%x, ebx: 0x%x\n"
  "ecx: 0x%x, es: 0x%x, fs: 0x%x, eflags: 0x%x\n"
  "ss: 0x%x, esp: 0x%x, orig_eax: 0x%x\n",
  reg->orig_cs, reg->orig_eip, reg->ds, reg->eax, reg->ebx,
  reg->ecx, reg->es, reg->fs, reg->eflags,
  reg->ss, reg->esp, reg->error_code);
 pid = get_pid();
 if (pid == -1) {
  printk("Get new pid failed.\n");
  return -1;
 }
 ("Get new pid: %d\n", pid);
 tsk = (struct task_struct *)alloc_page(0);
 if (!tsk) {
  ("Alloc tsk page failed.\n");
  return -1;
 }
 printk("Alloc task_struct page at: 0x%x\n", tsk);
 *tsk = *current;
 tsk->tss.prev_task_link = 0;
 tsk->tss.esp0 = (unsigned int)tsk + (4*1024) - 4;
 tsk->tss.ss0 = 0x10;
 tsk->tss.esp1 = 0;
 tsk->tss.ss1 = 0;
 tsk->tss.esp2 = 0;
 tsk->tss.ss2 = 0;
 tsk->tss.eip = reg->orig_eip;
 tsk->tss.eflags = reg->eflags;
 tsk->tss.eax = 0;
 tsk->tss.ebx = reg->ebx;
 tsk->tss.ecx = reg->ecx;
 tsk->tss.edx = reg->edx;
 tsk->tss.esp = reg->esp;
 tsk->tss.ebp = reg->ebp;
 tsk->tss.esi = reg->esi;
 tsk->tss.edi = reg->edi;
 tsk->tss.es = reg->es;
 tsk->tss.cs = reg->orig_cs;
 tsk->tss.ss = reg->ss;
 tsk->tss.ds = reg->ds;
 tsk->tss.fs = reg->fs;
 tsk->tss.gs = 0x17;
 tsk->tss.ldt_sel = ((4 + 2*pid + 1) << 3);
 tsk->tss.io_map = 0x80000000;
 tsk->pid = pid;
 tsk->tss_sel = ((4 + 2*pid) << 3);
 tsk->ldt_sel = ((4 + 2*pid + 1) << 3);
 tsk->state = 0;
 tsk->counter = 15;
 tsk->priority = 200;
 set_tss_desc(new_gdt, (unsigned int)&(tsk->tss), 0x68, 0x89, (4 + 2*pid));
 set_ldt_desc(new_gdt, (unsigned int)&(tsk->ldt), 0x40, 0x82, (4 + 2*pid + 1));
 set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0xfa, 1);
 set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0xf2, 2);
 copy_page_tables(tsk);
 list_add_tail(&(tsk->list), &task_list_head);
 printk("task esp0: 0x%x, esp: 0x%x\n", tsk->tss.esp0, tsk->tss.esp);
 return pid;
}

variable_types: struct task_struct { struct tss_struct { unsigned int prev_task_link; unsigned int esp0; unsigned int ss0; unsigned int esp1; unsigned int ss1; unsigned int esp2; unsigned int ss2; unsigned int cr3; unsigned int eip; unsigned int eflags; unsigned int eax; unsigned int ecx; unsigned int edx; unsigned int ebx; unsigned int esp; unsigned int ebp; unsigned int esi; unsigned int edi; unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds; unsigned int fs; unsigned int gs; unsigned int ldt_sel; unsigned int io_map; } tss; unsigned int tss_sel; unsigned int ldt_sel; struct gdt_desc { unsigned int a; unsigned int b; } ldt[3]; int pid; int state; int counter; int priority; struct list_head { struct list_head *prev; } list; } *

user_defined_types: struct task_struct { struct tss_struct tss; unsigned int tss_sel; unsigned int ldt_sel; struct gdt_desc ldt[3]; int pid; int state; int counter; int priority; struct list_head list; }

user_defined_types: struct regs { unsigned int ebx; unsigned int ecx; unsigned int edx; unsigned int esi; unsigned int edi; unsigned int ebp; unsigned int eax; unsigned int ds; unsigned int es; unsigned int fs; unsigned int error_code; unsigned int orig_eip; unsigned int orig_cs; unsigned int eflags; unsigned int esp; unsigned int ss; }


original_code:
int sys_creat_task(unsigned int eip)
{
        struct task_struct *tsk;
        int pid;
        pid = get_pid();
        if (pid == -1) {
                ("Get pid failed.\n");
                return -1;
        }
        printk("Get new pid: %d\n", pid);
        tsk = (struct task_struct *)alloc_page(0);
        if (!tsk) {
                ("Alloc tsk page failed.\n");
                return -1;
        }
        ("Alloc tsk page at: 0x%x\n", tsk);
 *tsk = *current;
        tsk->tss.prev_task_link = 0;
        tsk->tss.esp0 = (unsigned int)tsk + (4*1024);
        tsk->tss.ss0 = 0x10;
        tsk->tss.esp1 = 0;
        tsk->tss.ss1 = 0;
        tsk->tss.esp2 = 0;
        tsk->tss.ss2 = 0;
        tsk->tss.eip = eip;
        tsk->tss.eflags = 0x200;
        tsk->tss.eax = 0;
        tsk->tss.ebx = 0;
        tsk->tss.ecx = 0;
        tsk->tss.edx = 0;
        tsk->tss.esp = (unsigned int)alloc_page(0) + (4*1024);
 ("Alloc tsk ring3 stack at 0x%x\n", tsk->tss.esp - (4*1024));
        tsk->tss.ebp = 0;
        tsk->tss.esi = 0;
        tsk->tss.edi = 0;
        tsk->tss.es = 0x17;
        tsk->tss.cs = 0x0f;
        tsk->tss.ss = 0x17;
        tsk->tss.ds = 0x17;
        tsk->tss.fs = 0x17;
        tsk->tss.gs = 0x17;
        tsk->tss.ldt_sel = ((4 + 2*pid + 1) << 3);
        tsk->tss.io_map = 0x80000000;
        tsk->pid = pid;
        tsk->tss_sel = ((4 + 2*pid) << 3);
        tsk->ldt_sel = ((4 + 2*pid + 1) << 3);
        tsk->state = 0;
        tsk->counter = 15;
        tsk->priority = 200;
        set_tss_desc(new_gdt, (unsigned int)&(tsk->tss), 0x68, 0x89, (4 + 2*pid));
        set_ldt_desc(new_gdt, (unsigned int)&(tsk->ldt), 0x40, 0x82, (4 + 2*pid + 1));
        set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0xfa, 1);
        set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0xf2, 2);
 setup_task_pages(tsk);
        list_add_tail(&(tsk->list), &task_list_head);
 return 0;
}

variable_types:
eip
unsigned int

user_defined_types: struct tss_struct { unsigned int prev_task_link; unsigned int esp0; unsigned int ss0; unsigned int esp1; unsigned int ss1; unsigned int esp2; unsigned int ss2; unsigned int cr3; unsigned int eip; unsigned int eflags; unsigned int eax; unsigned int ecx; unsigned int edx; unsigned int ebx; unsigned int esp; unsigned int ebp; unsigned int esi; unsigned int edi; unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds; unsigned int fs; unsigned int gs; unsigned int ldt_sel; unsigned int io_map; }

user_defined_types: struct list_head { struct list_head *prev; }


panic

decompiled_code:
void __fastcall __noreturn panic(const char *a1, unsigned int a2)
{
printk("\n------------------------------------------\n");
printk("Kernel Panic: %s\tPid: %d\n", a1, *(unsigned int *)(current + 136));
printk("--------------------------------------------\n");
printk(
"error code: 0x%x eip: 0x%x esp: 0x%x eflags: 0x%x\n",
*(unsigned int *)((char *)&loc_24 + a2 + 4),
*(unsigned int *)((char *)&loc_2C + a2),
*(unsigned int *)((char *)&loc_38 + a2),
*(unsigned int *)((char *)&loc_31 + a2 + 3));
printk("\nGeneral Registers:\n");
printk(
"eax: 0x%x ebx: 0x%x ecx: 0x%x edx: 0x%x\n",
*(unsigned int *)((char *)&loc_15 + a2 + 3),
*(unsigned int *)a2,
*(unsigned int *)((char *)&loc_4 + a2),
*(unsigned int *)((char *)&loc_7 + a2 + 1));
printk("\nSegment Registers:\n");
printk(
"cs: 0x%x ds: 0x%x es: 0x%x ss: 0x%x fs: 0x%x\n",
*(unsigned int *)((char *)&loc_2C + a2 + 4),
*(unsigned int *)((char *)&loc_1C + a2),
*(unsigned int *)((char *)&loc_1F + a2 + 1),
*(unsigned int *)((char *)&loc_38 + a2 + 4),
*(unsigned int *)((char *)&loc_24 + a2));
printk("--------------------------------------------\n");
_disable();
__halt();
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, unsigned int a2)
{
printk("\n------------------------------------------\n");
printk("Kernel Panic: %s\tPid: %d\n", a1, *(unsigned int *)(current + 136));
printk("--------------------------------------------\n");
printk(
"error code: 0x%x eip: 0x%x esp: 0x%x eflags: 0x%x\n",
*(unsigned int *)((char *)&loc_24 + a2 + 4),
*(unsigned int *)((char *)&loc_2C + a2),
*(unsigned int *)((char *)&loc_38 + a2),
*(unsigned int *)((char *)&loc_31 + a2 + 3));
printk("\nGeneral Registers:\n");
printk(
"eax: 0x%x ebx: 0x%x ecx: 0x%x edx: 0x%x\n",
*(unsigned int *)((char *)&loc_15 + a2 + 3),
*(unsigned int *)a2,
*(unsigned int *)((char *)&loc_4 + a2),
*(unsigned int *)((char *)&loc_7 + a2 + 1));
printk("\nSegment Registers:\n");
printk(
"cs: 0x%x ds: 0x%x es: 0x%x ss: 0x%x fs: 0x%x\n",
*(unsigned int *)((char *)&loc_2C + a2 + 4),
*(unsigned int *)((char *)&loc_1C + a2),
*(unsigned int *)((char *)&loc_1F + a2 + 1),
*(unsigned int *)((char *)&loc_38 + a2 + 4),
*(unsigned int *)((char *)&loc_24 + a2));
printk("--------------------------------------------\n");
_disable();
__halt();
}

original_code:
void panic(char *msg, unsigned int esp)
{
        struct regs *reg = (struct regs *)esp;
 printk("\n------------------------------------------\n");
        printk("Kernel Panic: %s\tPid: %d\n", msg, current->pid);
 printk("--------------------------------------------\n");
        printk("error code: 0x%x eip: 0x%x esp: 0x%x eflags: 0x%x\n",
  reg->error_code, reg->orig_eip, reg->esp, reg->eflags);
        printk("\nGeneral Registers:\n");
        printk("eax: 0x%x ebx: 0x%x ecx: 0x%x edx: 0x%x\n",
                reg->eax, reg->ebx, reg->ecx, reg->edx);
        printk("\nSegment Registers:\n");
        printk("cs: 0x%x ds: 0x%x es: 0x%x ss: 0x%x fs: 0x%x\n",
  reg->orig_cs, reg->ds, reg->es, reg->ss, reg->fs);
 printk("--------------------------------------------\n");
        asm("cli\n\thlt\n"::);
}