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

result = &PBI_BB_enabled;
if ( PBI_BB_enabled )
{
if ( !buttondown )
{
CPU_IRQ = 1;
PBI_IRQ |= 1u;
buttondown = 1;
}
}
return result;
}

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

result = &PBI_BB_enabled;
if ( PBI_BB_enabled )
{
if ( !buttondown )
{
CPU_IRQ = 1;
PBI_IRQ |= 1u;
buttondown = 1;
}
}
return result;
}

original_code:
void PBI_BB_Menu(void)
{
 if (!PBI_BB_enabled) return;
 if (buttondown == 0) {
  do{}while(0);
  (CPU_IRQ = 1);
  PBI_IRQ |= 1;
  buttondown = 1;
 }
}

canonical_original_code:
void PBI_BB_Menu()
{
 if (!PBI_BB_enabled) return;
 if (buttondown == 0) {
  do{}while(0);
  (CPU_IRQ = 1);
  PBI_IRQ |= 1;
  buttondown = 1;
 }
}


decompiled_code:
void PBI_BB_Frame()
{
if ( buttondown )
{
if ( count_0 > 0 )
{
PBI_IRQ &= 0xFFFFFFFE;
POKEY_PutByte(14LL, POKEY_IRQEN);
buttondown = 0;
count_0 = 0;
}
else
{
++count_0;
}
}
}

canonical_decompiled_code:
void func9()
{
if ( buttondown )
{
if ( count_0 > 0 )
{
PBI_IRQ &= 0xFFFFFFFE;
POKEY_PutByte(14LL, POKEY_IRQEN);
buttondown = 0;
count_0 = 0;
}
else
{
++count_0;
}
}
}

original_code:
void PBI_BB_Frame(void)
{
 static int count = 0;
 if (buttondown) {
   if (count < 1) count++;
  else {
   do{}while(0);
   PBI_IRQ &= ~1;
   POKEY_PutByte(0x0e, POKEY_IRQEN);
   buttondown = 0;
   count = 0;
  }
 }
}

canonical_original_code:
void PBI_BB_Frame()
{
 static int count = 0;
 if (buttondown) {
   if (count < 1) count++;
  else {
   do{}while(0);
   PBI_IRQ &= ~1;
   POKEY_PutByte(0x0e, POKEY_IRQEN);
   buttondown = 0;
   count = 0;
  }
 }
}

variable_types: count int

return_type: void


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

result = StateSav_SaveINT(&PBI_BB_enabled, 1LL);
if ( PBI_BB_enabled )
{
StateSav_SaveFNAME(&bb_scsi_disk_filename);
StateSav_SaveFNAME(bb_rom_filename);
StateSav_SaveINT(&bb_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(bb_ram, 0x10000LL);
StateSav_SaveUBYTE(&bb_rom_bank, 1LL);
StateSav_SaveINT(&bb_rom_high_bit, 1LL);
result = StateSav_SaveUBYTE(&bb_PCR, 1LL);
}
return result;
}

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

result = StateSav_SaveINT(&PBI_BB_enabled, 1LL);
if ( PBI_BB_enabled )
{
StateSav_SaveFNAME(&bb_scsi_disk_filename);
StateSav_SaveFNAME(bb_rom_filename);
StateSav_SaveINT(&bb_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(bb_ram, 0x10000LL);
StateSav_SaveUBYTE(&bb_rom_bank, 1LL);
StateSav_SaveINT(&bb_rom_high_bit, 1LL);
result = StateSav_SaveUBYTE(&bb_PCR, 1LL);
}
return result;
}

original_code:
void PBI_BB_StateSave(void)
{
 StateSav_SaveINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_SaveFNAME(bb_scsi_disk_filename);
  StateSav_SaveFNAME(bb_rom_filename);
  StateSav_SaveINT(&bb_ram_bank_offset, 1);
  StateSav_SaveUBYTE(bb_ram, 0x10000);
  StateSav_SaveUBYTE(&bb_rom_bank, 1);
  StateSav_SaveINT(&bb_rom_high_bit, 1);
  StateSav_SaveUBYTE(&bb_PCR, 1);
 }
}

canonical_original_code:
void PBI_BB_StateSave()
{
 StateSav_SaveINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_SaveFNAME(bb_scsi_disk_filename);
  StateSav_SaveFNAME(bb_rom_filename);
  StateSav_SaveINT(&bb_ram_bank_offset, 1);
  StateSav_SaveUBYTE(bb_ram, 0x10000);
  StateSav_SaveUBYTE(&bb_rom_bank, 1);
  StateSav_SaveINT(&bb_rom_high_bit, 1);
  StateSav_SaveUBYTE(&bb_PCR, 1);
 }
}


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

result = StateSav_ReadINT(&PBI_BB_enabled, 1LL);
if ( PBI_BB_enabled )
{
StateSav_ReadFNAME(&bb_scsi_disk_filename);
StateSav_ReadFNAME(bb_rom_filename);
init_bb();
StateSav_ReadINT(&bb_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(bb_ram, 0x10000LL);
StateSav_ReadUBYTE(&bb_rom_bank, 1LL);
StateSav_ReadINT(&bb_rom_high_bit, 1LL);
result = StateSav_ReadUBYTE(&bb_PCR, 1LL);
}
return result;
}

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

result = StateSav_ReadINT(&PBI_BB_enabled, 1LL);
if ( PBI_BB_enabled )
{
StateSav_ReadFNAME(&bb_scsi_disk_filename);
StateSav_ReadFNAME(bb_rom_filename);
func0();
StateSav_ReadINT(&bb_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(bb_ram, 0x10000LL);
StateSav_ReadUBYTE(&bb_rom_bank, 1LL);
StateSav_ReadINT(&bb_rom_high_bit, 1LL);
result = StateSav_ReadUBYTE(&bb_PCR, 1LL);
}
return result;
}

original_code:
void PBI_BB_StateRead(void)
{
 StateSav_ReadINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_ReadFNAME(bb_scsi_disk_filename);
  StateSav_ReadFNAME(bb_rom_filename);
  init_bb();
  StateSav_ReadINT(&bb_ram_bank_offset, 1);
  StateSav_ReadUBYTE(bb_ram, 0x10000);
  StateSav_ReadUBYTE(&bb_rom_bank, 1);
  StateSav_ReadINT(&bb_rom_high_bit, 1);
  StateSav_ReadUBYTE(&bb_PCR, 1);
 }
}

canonical_original_code:
void PBI_BB_StateRead()
{
 StateSav_ReadINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_ReadFNAME(bb_scsi_disk_filename);
  StateSav_ReadFNAME(bb_rom_filename);
  init_bb();
  StateSav_ReadINT(&bb_ram_bank_offset, 1);
  StateSav_ReadUBYTE(bb_ram, 0x10000);
  StateSav_ReadUBYTE(&bb_rom_bank, 1);
  StateSav_ReadINT(&bb_rom_high_bit, 1);
  StateSav_ReadUBYTE(&bb_PCR, 1);
 }
}


decompiled_code:
_BOOL8 __fastcall Util_chrieq(unsigned __int8 a1, unsigned __int8 a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 != a2 )
{
result = 0LL;
if ( (a1 ^ a2) == 32 )
result = (unsigned __int8)((a1 & 0xDF) - 65) <= 0x19u;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned __int8 a1, unsigned __int8 a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 != a2 )
{
result = 0LL;
if ( (a1 ^ a2) == 32 )
result = (unsigned __int8)((a1 & 0xDF) - 65) <= 0x19u;
}
return result;
}

original_code:
int Util_chrieq(char c1, char c2)
{
 switch (c1 ^ c2) {
 case 0x00:
  return 1;
 case 0x20:
  return (c1 >= 'A' && c1 <= 'Z') || (c1 >= 'a' && c1 <= 'z');
 default:
  return 0;
 }
}

return_type: int


decompiled_code:
__int64 __fastcall Util_stpcpy(void *dest, void *src)
{
size_t v2; // rbp

v2 = strlen((const char *)src);
memcpy(dest, src, v2 + 1);
return (__int64)dest + v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *dest, void *src)
{
size_t v2; // rbp

v2 = strlen((const char *)src);
memcpy(dest, src, v2 + 1);
return (__int64)dest + v2;
}

original_code:
char *Util_stpcpy(char *dest, const char *src)
{
 size_t len = strlen(src);
 memcpy(dest, src, len + 1);
 return dest + len;
}

canonical_original_code:
char *Util_stpcpy(char *dest, char *src)
{
 unsigned long len = strlen(src);
 memcpy(dest, src, len + 1);
 return dest + len;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
len
unsigned long

return_type: char *


decompiled_code:
char *__fastcall safe_strncpy(char *a1, const char *a2, int a3)
{
if ( a1 && a3 > 0 )
{
if ( !a2 )
a2 = &LC0;
strncpy(a1, a2, a3);
a1[a3 - 1] = 0;
}
return a1;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1, const char *a2, int a3)
{
if ( a1 && a3 > 0 )
{
if ( !a2 )
a2 = &LC0;
strncpy(a1, a2, a3);
a1[a3 - 1] = 0;
}
return a1;
}

original_code:
char *safe_strncpy(char *buffer, const char *source, int bufsize)
{
 if (buffer == ((void *)0)) return ((void *)0);
 if (bufsize > 0) {
  strncpy(buffer, source != ((void *)0) ? source : "", bufsize);
  buffer[bufsize-1] = '\0';
 }
 return buffer;
}

canonical_original_code:
char *safe_strncpy(char *buffer, char *source, int bufsize)
{
 if (buffer == ((void *)0)) return ((void *)0);
 if (bufsize > 0) {
  strncpy(buffer, source != ((void *)0) ? source : "", bufsize);
  buffer[bufsize-1] = '\0';
 }
 return buffer;
}

variable_types:
buffer
char *

variable_types:
source
char *

variable_types: bufsize int


decompiled_code:
char *__fastcall Util_strlcpy(char *a1, const char *a2, size_t a3)
{
char *result; // rax

result = strncpy(a1, a2, a3);
a1[a3 - 1] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func3(char *a1, const char *a2, size_t a3)
{
char *result; // rax

result = strncpy(a1, a2, a3);
a1[a3 - 1] = 0;
return result;
}

original_code:
char *Util_strlcpy(char *dest, const char *src, size_t size)
{
 strncpy(dest, src, size);
 dest[size - 1] = '\0';
 return dest;
}

canonical_original_code:
char *Util_strlcpy(char *dest, char *src, unsigned long size)
{
 strncpy(dest, src, size);
 dest[size - 1] = '\0';
 return dest;
}

variable_types:
size
unsigned long


decompiled_code:
const char *__fastcall Util_chomp(const char *a1)
{
const char *result; // rax
const char *v2; // r8

result = (const char *)strlen(a1);
if ( (int)result <= 1 )
{
if ( (_DWORD)result != 1 )
return result;
goto LABEL_3;
}
if ( a1[(int)result - 1] != 10 )
{
LABEL_3:
result = &a1[(int)result - 1];
if ( *result == 10 || *result == 13 )
goto LABEL_8;
return result;
}
v2 = &a1[(int)result - 2];
if ( *v2 != 13 )
{
result = &a1[(int)result - 1];
LABEL_8:
*result = 0;
return result;
}
*v2 = 0;
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(const char *a1)
{
const char *result; // rax
const char *v2; // r8

result = (const char *)strlen(a1);
if ( (int)result <= 1 )
{
if ( (_DWORD)result != 1 )
return result;
goto LABEL_3;
}
if ( a1[(int)result - 1] != 10 )
{
LABEL_3:
result = &a1[(int)result - 1];
if ( *result == 10 || *result == 13 )
goto LABEL_8;
return result;
}
v2 = &a1[(int)result - 2];
if ( *v2 != 13 )
{
result = &a1[(int)result - 1];
LABEL_8:
*result = 0;
return result;
}
*v2 = 0;
return result;
}

variable_types:
s
char *

variable_types: len int


decompiled_code:
unsigned __int64 __fastcall Util_trim(void *src)
{
void *v1; // rbp
unsigned __int64 result; // rax
char *v3; // rbx
__int64 v4; // rdx
unsigned __int8 *v5; // rax
__int64 v6; // rsi
unsigned __int8 *v7; // rcx
unsigned __int64 v8; // rdi

v1 = src;
result = *(unsigned __int8 *)src;
v3 = (char *)src;
if ( (unsigned __int8)result <= 0x20u )
{
v4 = 0x100002600LL;
if ( _bittest64(&v4, result) )
{
do
result = (unsigned __int8)*++v3;
while ( (unsigned __int8)result <= 0x20u && _bittest64(&v4, result) );
}
}
if ( (_BYTE)result )
{
v5 = (unsigned __int8 *)src + strlen((const char *)src);
v6 = 0x100002600LL;
do
{
v7 = v5--;
v8 = *v5;
}
while ( (unsigned __int8)v8 <= 0x20u && _bittest64(&v6, v8) );
*v7 = 0;
result = (unsigned __int64)memmove(v1, v3, v7 + 1 - (unsigned __int8 *)v3);
}
else
{
*(_BYTE *)src = 0;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(void *src)
{
void *v1; // rbp
unsigned __int64 result; // rax
char *v3; // rbx
__int64 v4; // rdx
unsigned __int8 *v5; // rax
__int64 v6; // rsi
unsigned __int8 *v7; // rcx
unsigned __int64 v8; // rdi

v1 = src;
result = *(unsigned __int8 *)src;
v3 = (char *)src;
if ( (unsigned __int8)result <= 0x20u )
{
v4 = 0x100002600LL;
if ( _bittest64(&v4, result) )
{
do
result = (unsigned __int8)*++v3;
while ( (unsigned __int8)result <= 0x20u && _bittest64(&v4, result) );
}
}
if ( (_BYTE)result )
{
v5 = (unsigned __int8 *)src + strlen((const char *)src);
v6 = 0x100002600LL;
do
{
v7 = v5--;
v8 = *v5;
}
while ( (unsigned __int8)v8 <= 0x20u && _bittest64(&v6, v8) );
*v7 = 0;
result = (unsigned __int64)memmove(v1, v3, v7 + 1 - (unsigned __int8 *)v3);
}
else
{
*(_BYTE *)src = 0;
}
return result;
}

variable_types:
p
char *

variable_types:
q
char *


decompiled_code:
__int64 __fastcall Util_sscandec(char *a1)
{
char v1; // r8
unsigned int v2; // esi
__int64 result; // rax

v1 = *a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
v2 = v1 + 10 * v2 - 48;
v1 = *++a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
result = 0xFFFFFFFFLL;
if ( !v1 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
char v1; // r8
unsigned int v2; // esi
__int64 result; // rax

v1 = *a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
v2 = v1 + 10 * v2 - 48;
v1 = *++a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
result = 0xFFFFFFFFLL;
if ( !v1 )
result = v2;
return result;
}

original_code:
int Util_sscandec(const char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 10 * result + *s - '0';
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

canonical_original_code:
int Util_sscandec(char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 10 * result + *s - '0';
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

variable_types: result int


decompiled_code:
__int64 __fastcall Util_sscansdec(char *a1, _DWORD *a2)
{
unsigned int v2; // ebp
int v3; // eax

if ( *a1 == 43 )
{
v2 = 0;
}
else
{
v2 = 0;
if ( *a1 != 45 )
goto LABEL_5;
v2 = 1;
}
++a1;
LABEL_5:
v3 = Util_sscandec(a1);
*a2 = v3;
if ( v3 == -1 )
return 0;
if ( !v2 )
return 1;
*a2 = -v3;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, _DWORD *a2)
{
unsigned int v2; // ebp
int v3; // eax

if ( *a1 == 43 )
{
v2 = 0;
}
else
{
v2 = 0;
if ( *a1 != 45 )
goto LABEL_5;
v2 = 1;
}
++a1;
LABEL_5:
v3 = func6(a1);
*a2 = v3;
if ( v3 == -1 )
return 0;
if ( !v2 )
return 1;
*a2 = -v3;
return v2;
}

original_code:
int Util_sscansdec(char const *s, int *dest)
{
 int minus = 0;
 switch(*s) {
 case '-':
  minus = 1;
 case '+':
  ++s;
 }
 *dest = Util_sscandec(s);
 if (*dest == -1)
  return 0;
 if (minus)
  *dest = -*dest;
 return 1;
}

canonical_original_code:
int Util_sscansdec(char *s, int *dest)
{
 int minus = 0;
 switch(*s) {
 case '-':
  minus = 1;
 case '+':
  ++s;
 }
 *dest = Util_sscandec(s);
 if (*dest == -1)
  return 0;
 if (minus)
  *dest = -*dest;
 return 1;
}

variable_types:
dest
int *

variable_types: minus int


decompiled_code:
__int64 __fastcall Util_sscandouble(const char *a1, double *a2)
{
double v2; // xmm0_8
__int64 result; // rax
int *v4; // rcx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v2 = strtod(a1, &endptr);
result = 0LL;
if ( !*endptr )
{
v4 = _errno_location();
result = 0LL;
if ( *v4 != 34 )
{
*a2 = v2;
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, double *a2)
{
double v2; // xmm0_8
__int64 result; // rax
int *v4; // rcx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v2 = strtod(a1, &endptr);
result = 0LL;
if ( !*endptr )
{
v4 = _errno_location();
result = 0LL;
if ( *v4 != 34 )
{
*a2 = v2;
result = 1LL;
}
}
return result;
}

original_code:
int Util_sscandouble(char const *s, double *dest)
{
 char *endptr;
 double result;
 result = strtod(s, &endptr);
 if (endptr[0] != '\0' || (*__errno_location ()) == 34)
  return 0;
 *dest = result;
 return 1;
}

canonical_original_code:
int Util_sscandouble(char *s, double *dest)
{
 char *endptr;
 double result;
 result = strtod(s, &endptr);
 if (endptr[0] != '\0' || (*__errno_location ()) == 34)
  return 0;
 *dest = result;
 return 1;
}

variable_types:
dest
double *

variable_types:
endptr
char *

variable_types: result double


decompiled_code:
__int64 __fastcall Util_sscanhex(char *a1)
{
unsigned int v1; // edx
char v2; // al
bool v3; // zf
__int64 result; // rax

if ( !*a1 )
return 0xFFFFFFFFLL;
v1 = 0;
while ( 1 )
{
v2 = *a1;
if ( (unsigned __int8)(*a1 - 48) <= 9u )
{
v1 = 16 * v1 + v2 - 48;
goto LABEL_4;
}
if ( (unsigned __int8)(v2 - 65) <= 5u )
{
v1 = 16 * v1 + v2 - 55;
goto LABEL_4;
}
if ( (unsigned __int8)(v2 - 97) > 5u )
break;
v1 = 16 * v1 + v2 - 87;
LABEL_4:
++a1;
}
v3 = v2 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1)
{
unsigned int v1; // edx
char v2; // al
bool v3; // zf
__int64 result; // rax

if ( !*a1 )
return 0xFFFFFFFFLL;
v1 = 0;
while ( 1 )
{
v2 = *a1;
if ( (unsigned __int8)(*a1 - 48) <= 9u )
{
v1 = 16 * v1 + v2 - 48;
goto LABEL_4;
}
if ( (unsigned __int8)(v2 - 65) <= 5u )
{
v1 = 16 * v1 + v2 - 55;
goto LABEL_4;
}
if ( (unsigned __int8)(v2 - 97) > 5u )
break;
v1 = 16 * v1 + v2 - 87;
LABEL_4:
++a1;
}
v3 = v2 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v1;
return result;
}

original_code:
int Util_sscanhex(const char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 16 * result + *s - '0';
  else if (*s >= 'A' && *s <= 'F')
   result = 16 * result + *s - 'A' + 10;
  else if (*s >= 'a' && *s <= 'f')
   result = 16 * result + *s - 'a' + 10;
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

canonical_original_code:
int Util_sscanhex(char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 16 * result + *s - '0';
  else if (*s >= 'A' && *s <= 'F')
   result = 16 * result + *s - 'A' + 10;
  else if (*s >= 'a' && *s <= 'f')
   result = 16 * result + *s - 'a' + 10;
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}


decompiled_code:
__int64 __fastcall Util_sscanbool(_BYTE *a1)
{
__int64 result; // rax

if ( *a1 == 48 )
return (unsigned int)-(a1[1] != 0);
result = 0xFFFFFFFFLL;
if ( *a1 == 49 )
result = a1[1] == 0 ? 1 : -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1)
{
__int64 result; // rax

if ( *a1 == 48 )
return (unsigned int)-(a1[1] != 0);
result = 0xFFFFFFFFLL;
if ( *a1 == 49 )
result = a1[1] == 0 ? 1 : -1;
return result;
}

original_code:
int Util_sscanbool(const char *s)
{
 if (*s == '0' && s[1] == '\0')
  return 0;
 if (*s == '1' && s[1] == '\0')
  return 1;
 return -1;
}


original_code:
double Util_round(double x)
{
 return floor(x + 0.5);
}

variable_types: x double

return_type: double


decompiled_code:
void *__fastcall Util_malloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func12(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

original_code:
void *Util_malloc(size_t size)
{
 void *ptr = malloc(size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

canonical_original_code:
void *Util_malloc(unsigned long size)
{
 void *ptr = malloc(size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

variable_types:
ptr
void *

return_type: void *


decompiled_code:
void *__fastcall Util_realloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func13(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

original_code:
void *Util_realloc(void *ptr, size_t size)
{
 ptr = realloc(ptr, size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

canonical_original_code:
void *Util_realloc(void *ptr, unsigned long size)
{
 ptr = realloc(ptr, size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}


decompiled_code:
void *__fastcall Util_strdup(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = Util_malloc(v1);
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func14(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = func12(v1);
return memcpy(v2, src, v1);
}

original_code:
char *Util_strdup(const char *s)
{
 size_t size = strlen(s) + 1;
 char *ptr = (char *) Util_malloc(size);
 memcpy(ptr, s, size);
 return ptr;
}

canonical_original_code:
char *Util_strdup(char *s)
{
 unsigned long size = strlen(s) + 1;
 char *ptr = (char *) Util_malloc(size);
 memcpy(ptr, s, size);
 return ptr;
}

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall Util_splitpath(char *src, void *dest, char *a3)
{
__int64 result; // rax
char *v5; // rbx
char *v6; // rdx
int v7; // er14

result = strlen(src);
v5 = &src[result - 2];
if ( src > v5 )
goto LABEL_34;
result = ((_BYTE)result - 2) & 7;
if ( *v5 == 47 )
goto LABEL_39;
if ( src > --v5 )
{
LABEL_34:
if ( dest )
*(_BYTE *)dest = 0;
if ( a3 )
result = (__int64)strcpy(a3, src);
return result;
}
if ( !result )
{
LABEL_25:
while ( *v5 != 47 )
{
v6 = v5 - 1;
v5 = v6;
if ( *v6 == 47 )
break;
v5 = v6 - 1;
if ( *(v6 - 1) == 47 )
break;
v5 = v6 - 2;
if ( *(v6 - 2) == 47 )
break;
v5 = v6 - 3;
if ( *(v6 - 3) == 47 )
break;
v5 = v6 - 4;
if ( *(v6 - 4) == 47 )
break;
v5 = v6 - 5;
if ( *(v6 - 5) == 47 )
break;
v5 = v6 - 6;
if ( *(v6 - 6) == 47 )
break;
v5 = v6 - 7;
if ( src > v6 - 7 )
goto LABEL_34;
}
goto LABEL_39;
}
if ( result != 1 )
{
if ( result != 2 )
{
if ( result != 3 )
{
if ( result != 4 )
{
if ( result != 5 )
{
if ( result != 6 )
{
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 != 47 )
{
if ( src <= --v5 )
goto LABEL_25;
goto LABEL_34;
}
LABEL_39:
if ( dest )
{
v7 = (_DWORD)v5 - (_DWORD)src;
if ( src == v5 || v5 == src + 2 && src[1] == 58 )
v7 = (_DWORD)v5 - (_DWORD)src + 1;
result = (__int64)memcpy(dest, src, v7);
*((_BYTE *)dest + v7) = 0;
}
if ( a3 )
result = (__int64)strcpy(a3, v5 + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(char *src, void *dest, char *a3)
{
__int64 result; // rax
char *v5; // rbx
char *v6; // rdx
int v7; // er14

result = strlen(src);
v5 = &src[result - 2];
if ( src > v5 )
goto LABEL_34;
result = ((_BYTE)result - 2) & 7;
if ( *v5 == 47 )
goto LABEL_39;
if ( src > --v5 )
{
LABEL_34:
if ( dest )
*(_BYTE *)dest = 0;
if ( a3 )
result = (__int64)strcpy(a3, src);
return result;
}
if ( !result )
{
LABEL_25:
while ( *v5 != 47 )
{
v6 = v5 - 1;
v5 = v6;
if ( *v6 == 47 )
break;
v5 = v6 - 1;
if ( *(v6 - 1) == 47 )
break;
v5 = v6 - 2;
if ( *(v6 - 2) == 47 )
break;
v5 = v6 - 3;
if ( *(v6 - 3) == 47 )
break;
v5 = v6 - 4;
if ( *(v6 - 4) == 47 )
break;
v5 = v6 - 5;
if ( *(v6 - 5) == 47 )
break;
v5 = v6 - 6;
if ( *(v6 - 6) == 47 )
break;
v5 = v6 - 7;
if ( src > v6 - 7 )
goto LABEL_34;
}
goto LABEL_39;
}
if ( result != 1 )
{
if ( result != 2 )
{
if ( result != 3 )
{
if ( result != 4 )
{
if ( result != 5 )
{
if ( result != 6 )
{
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 != 47 )
{
if ( src <= --v5 )
goto LABEL_25;
goto LABEL_34;
}
LABEL_39:
if ( dest )
{
v7 = (_DWORD)v5 - (_DWORD)src;
if ( src == v5 || v5 == src + 2 && src[1] == 58 )
v7 = (_DWORD)v5 - (_DWORD)src + 1;
result = (__int64)memcpy(dest, src, v7);
*((_BYTE *)dest + v7) = 0;
}
if ( a3 )
result = (__int64)strcpy(a3, v5 + 1);
return result;
}

original_code:
void Util_splitpath(const char *path, char *dir_part, char *file_part)
{
 const char *p;
 for (p = path + strlen(path) - 2; p >= path; p--) {
  if (*p == '/'
     ) {
   if (dir_part != ((void *)0)) {
    int len = p - path;
    if (p == path || (p == path + 2 && path[1] == ':'))
     len++;
    memcpy(dir_part, path, len);
    dir_part[len] = '\0';
   }
   if (file_part != ((void *)0))
    strcpy(file_part, p + 1);
   return;
  }
 }
 if (dir_part != ((void *)0))
  dir_part[0] = '\0';
 if (file_part != ((void *)0))
  strcpy(file_part, path);
}

canonical_original_code:
void Util_splitpath(char *path, char *dir_part, char *file_part)
{
 char *p;
 for (p = path + strlen(path) - 2; p >= path; p--) {
  if (*p == '/'
     ) {
   if (dir_part != ((void *)0)) {
    int len = p - path;
    if (p == path || (p == path + 2 && path[1] == ':'))
     len++;
    memcpy(dir_part, path, len);
    dir_part[len] = '\0';
   }
   if (file_part != ((void *)0))
    strcpy(file_part, p + 1);
   return;
  }
 }
 if (dir_part != ((void *)0))
  dir_part[0] = '\0';
 if (file_part != ((void *)0))
  strcpy(file_part, path);
}

variable_types:
path
char *


decompiled_code:
int __fastcall Util_catpath(char *s, char *a2, _BYTE *a3)
{
const char *v4; // rdx
size_t v5; // rax

v4 = "%s%s";
if ( *a2 )
{
if ( *a3 != 47 )
{
v5 = strlen(a2);
v4 = "%s%s";
if ( a2[v5 - 1] != 47 )
v4 = "%s/%s";
}
}
return snprintf(s, 0x1000uLL, v4, a2, a3);
}

canonical_decompiled_code:
int __fastcall func16(char *s, char *a2, _BYTE *a3)
{
const char *v4; // rdx
size_t v5; // rax

v4 = "%s%s";
if ( *a2 )
{
if ( *a3 != 47 )
{
v5 = strlen(a2);
v4 = "%s%s";
if ( a2[v5 - 1] != 47 )
v4 = "%s/%s";
}
}
return snprintf(s, 0x1000uLL, v4, a2, a3);
}

original_code:
void Util_catpath(char *result, const char *path1, const char *path2)
{
 snprintf(result, 4096,
  path1[0] == '\0' || path2[0] == '/' || path1[strlen(path1) - 1] == '/'
   ? "%s%s" : "%s" "/" "%s", path1, path2);
}

canonical_original_code:
void Util_catpath(char *result, char *path1, char *path2)
{
 snprintf(result, 4096,
  path1[0] == '\0' || path2[0] == '/' || path1[strlen(path1) - 1] == '/'
   ? "%s%s" : "%s" "/" "%s", path1, path2);
}

variable_types:
result
char *


decompiled_code:
__int64 __fastcall Util_direxists(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

original_code:
int Util_direxists(const char *filename)
{
 struct stat filestatus;
 return stat(filename, &filestatus) == 0 && (filestatus.st_mode & 0040000);
}

canonical_original_code:
int Util_direxists(char *filename)
{
 struct stat filestatus;
 return stat(filename, &filestatus) == 0 && (filestatus.st_mode & 0040000);
}

variable_types:
filename
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
char *ESC_ClearAll()
{
_QWORD *v0; // rcx
char *result; // rax

v0 = &esc_function;
do
{
*v0 = 0LL;
result = (char *)(v0 + 1);
v0[1] = 0LL;
v0[2] = 0LL;
v0[3] = 0LL;
v0[4] = 0LL;
v0[5] = 0LL;
v0[6] = 0LL;
v0[7] = 0LL;
v0 += 8;
}
while ( v0 != (_QWORD *)((char *)&esc_function + 2048) );
return result;
}

canonical_decompiled_code:
char *func0()
{
_QWORD *v0; // rcx
char *result; // rax

v0 = &esc_function;
do
{
*v0 = 0LL;
result = (char *)(v0 + 1);
v0[1] = 0LL;
v0[2] = 0LL;
v0[3] = 0LL;
v0[4] = 0LL;
v0[5] = 0LL;
v0[6] = 0LL;
v0[7] = 0LL;
v0 += 8;
}
while ( v0 != (_QWORD *)((char *)&esc_function + 2048) );
return result;
}

original_code:
void ESC_ClearAll(void)
{
 int i;
 for (i = 0; i < 256; i++)
  esc_function[i] = ((void *)0);
}

canonical_original_code:
void ESC_ClearAll()
{
 int i;
 for (i = 0; i < 256; i++)
  esc_function[i] = ((void *)0);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ESC_Add(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
return result;
}

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

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
return result;
}

original_code:
void ESC_Add(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
}

canonical_original_code:
void ESC_Add(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
}

variable_types:
address
unsigned short

variable_types:
function
void (*)()


decompiled_code:
__int64 __fastcall ESC_AddEscRts(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
result = a1 + 2;
MEMORY_mem[result] = 96;
return result;
}

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

esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
result = a1 + 2;
MEMORY_mem[result] = 96;
return result;
}

original_code:
void ESC_AddEscRts(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
 (MEMORY_mem[address + 2] = 0x60);
}

canonical_original_code:
void ESC_AddEscRts(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
 (MEMORY_mem[address + 2] = 0x60);
}


decompiled_code:
__int64 __fastcall ESC_AddEscRts2(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -46;
MEMORY_mem[a1 + 1] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -46;
MEMORY_mem[a1 + 1] = a2;
return result;
}

original_code:
void ESC_AddEscRts2(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xd2);
 (MEMORY_mem[address + 1] = esc_code);
}

canonical_original_code:
void ESC_AddEscRts2(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xd2);
 (MEMORY_mem[address + 1] = esc_code);
}


decompiled_code:
__int64 __fastcall ESC_Run(unsigned __int8 a1)
{
__int64 (*v1)(void); // r11
__int64 result; // rax

if ( (unsigned __int16)esc_address[a1] == CPU_regPC - 2 )
{
v1 = (__int64 (*)(void))esc_function[a1];
if ( v1 )
return v1();
}
CPU_cim_encountered = 1;
Log_print("Invalid ESC code %02x at address %04x", a1);
result = Atari800_Exit(1LL);
if ( !(_DWORD)result )
exit(0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
__int64 (*v1)(void); // r11
__int64 result; // rax

if ( (unsigned __int16)esc_address[a1] == CPU_regPC - 2 )
{
v1 = (__int64 (*)(void))esc_function[a1];
if ( v1 )
return v1();
}
CPU_cim_encountered = 1;
Log_print("Invalid ESC code %02x at address %04x", a1);
result = Atari800_Exit(1LL);
if ( !(_DWORD)result )
exit(0);
return result;
}

original_code:
void ESC_Run(unsigned char esc_code)
{
 if (esc_address[esc_code] == CPU_regPC - 2 && esc_function[esc_code] != ((void *)0)) {
  esc_function[esc_code]();
  return;
 }
 CPU_cim_encountered = 1;
 Log_print("Invalid ESC code %02x at address %04x", esc_code, CPU_regPC - 2);
 if (!Atari800_Exit(1))
  exit(0);
}


decompiled_code:
_DWORD *ESC_PatchOS()
{
int v0; // ebx
_DWORD *result; // rax
char v2; // r8
char v3; // di
unsigned __int16 v4; // si
unsigned __int16 v5; // r10

v0 = Devices_PatchOS();
result = &ESC_enable_sio_patch;
if ( ESC_enable_sio_patch )
{
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
v2 = 0x80;
v3 = -96;
v4 = -4164;
v5 = -4236;
}
else
{
if ( Atari800_machine_type != 2 )
return result;
v2 = 3;
v3 = -87;
v4 = -672;
v5 = -749;
}
if ( MEMORY_mem[v5] == 0xA9
&& MEMORY_mem[(unsigned __int16)(v5 + 1)] == 3
&& MEMORY_mem[(unsigned __int16)(v5 + 2)] == 0x8D
&& MEMORY_mem[(unsigned __int16)(v5 + 3)] == 42
&& MEMORY_mem[(unsigned __int16)(v5 + 4)] == 2
&& MEMORY_mem[v4] == v3
&& MEMORY_mem[(unsigned __int16)(v4 + 1)] == v2
&& MEMORY_mem[(unsigned __int16)(v4 + 2)] == 32
&& MEMORY_mem[(unsigned __int16)(v4 + 3)] == 92
&& MEMORY_mem[(unsigned __int16)(v4 + 4)] == 0xE4 )
{
ESC_Add(v5, 0xA8u, (__int64)&CASSETTE_LeaderLoad);
ESC_Add(v4, 0xA9u, (__int64)&CASSETTE_LeaderSave);
}
result = (_DWORD *)ESC_AddEscRts(0xE459u, 0, (__int64)&SIO_Handler);
}
else
{
ESC_Remove(0xA8u);
ESC_Remove(0xA9u);
result = ESC_Remove(0);
if ( !v0 )
return result;
}
if ( Atari800_machine_type == 2 )
{
MEMORY_mem[49940] = -114;
MEMORY_mem[49941] = -1;
MEMORY_mem[49945] = -114;
MEMORY_mem[49946] = -1;
}
return result;
}

canonical_decompiled_code:
_DWORD *func5()
{
int v0; // ebx
_DWORD *result; // rax
char v2; // r8
char v3; // di
unsigned __int16 v4; // si
unsigned __int16 v5; // r10

v0 = Devices_PatchOS();
result = &ESC_enable_sio_patch;
if ( ESC_enable_sio_patch )
{
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
v2 = 0x80;
v3 = -96;
v4 = -4164;
v5 = -4236;
}
else
{
if ( Atari800_machine_type != 2 )
return result;
v2 = 3;
v3 = -87;
v4 = -672;
v5 = -749;
}
if ( MEMORY_mem[v5] == 0xA9
&& MEMORY_mem[(unsigned __int16)(v5 + 1)] == 3
&& MEMORY_mem[(unsigned __int16)(v5 + 2)] == 0x8D
&& MEMORY_mem[(unsigned __int16)(v5 + 3)] == 42
&& MEMORY_mem[(unsigned __int16)(v5 + 4)] == 2
&& MEMORY_mem[v4] == v3
&& MEMORY_mem[(unsigned __int16)(v4 + 1)] == v2
&& MEMORY_mem[(unsigned __int16)(v4 + 2)] == 32
&& MEMORY_mem[(unsigned __int16)(v4 + 3)] == 92
&& MEMORY_mem[(unsigned __int16)(v4 + 4)] == 0xE4 )
{
func1(v5, 0xA8u, (__int64)&CASSETTE_LeaderLoad);
func1(v4, 0xA9u, (__int64)&CASSETTE_LeaderSave);
}
result = (_DWORD *)func2(0xE459u, 0, (__int64)&SIO_Handler);
}
else
{
ESC_Remove(0xA8u);
ESC_Remove(0xA9u);
result = ESC_Remove(0);
if ( !v0 )
return result;
}
if ( Atari800_machine_type == 2 )
{
MEMORY_mem[49940] = -114;
MEMORY_mem[49941] = -1;
MEMORY_mem[49945] = -114;
MEMORY_mem[49946] = -1;
}
return result;
}

original_code:
void ESC_PatchOS(void)
{
 int patched = Devices_PatchOS();
 if (ESC_enable_sio_patch) {
  unsigned short addr_l;
  unsigned short addr_s;
  unsigned char check_s_0;
  unsigned char check_s_1;
  switch (Atari800_machine_type) {
  case 0:
  case 1:
   addr_l = 0xef74;
   addr_s = 0xefbc;
   check_s_0 = 0xa0;
   check_s_1 = 0x80;
   break;
  case 2:
   addr_l = 0xfd13;
   addr_s = 0xfd60;
   check_s_0 = 0xa9;
   check_s_1 = 0x03;
   break;
  default:
   return;
  }
  if ((MEMORY_mem[addr_l]) == 0xa9 && (MEMORY_mem[addr_l + 1]) == 0x03
   && (MEMORY_mem[addr_l + 2]) == 0x8d && (MEMORY_mem[addr_l + 3]) == 0x2a
   && (MEMORY_mem[addr_l + 4]) == 0x02
   && (MEMORY_mem[addr_s]) == check_s_0
   && (MEMORY_mem[addr_s + 1]) == check_s_1
   && (MEMORY_mem[addr_s + 2]) == 0x20 && (MEMORY_mem[addr_s + 3]) == 0x5c
   && (MEMORY_mem[addr_s + 4]) == 0xe4) {
   ESC_Add(addr_l, ESC_COPENLOAD, CASSETTE_LeaderLoad);
   ESC_Add(addr_s, ESC_COPENSAVE, CASSETTE_LeaderSave);
  }
  ESC_AddEscRts(0xe459, ESC_SIOV, SIO_Handler);
  patched = 1;
 }
 else {
  ESC_Remove(ESC_COPENLOAD);
  ESC_Remove(ESC_COPENSAVE);
  ESC_Remove(ESC_SIOV);
 };
 if (patched && Atari800_machine_type == 2) {
  (MEMORY_mem[0xc314] = 0x8e);
  (MEMORY_mem[0xc315] = 0xff);
  (MEMORY_mem[0xc319] = 0x8e);
  (MEMORY_mem[0xc31a] = 0xff);
 }
}

canonical_original_code:
void ESC_PatchOS()
{
 int patched = Devices_PatchOS();
 if (ESC_enable_sio_patch) {
  unsigned short addr_l;
  unsigned short addr_s;
  unsigned char check_s_0;
  unsigned char check_s_1;
  switch (Atari800_machine_type) {
  case 0:
  case 1:
   addr_l = 0xef74;
   addr_s = 0xefbc;
   check_s_0 = 0xa0;
   check_s_1 = 0x80;
   break;
  case 2:
   addr_l = 0xfd13;
   addr_s = 0xfd60;
   check_s_0 = 0xa9;
   check_s_1 = 0x03;
   break;
  default:
   return;
  }
  if ((MEMORY_mem[addr_l]) == 0xa9 && (MEMORY_mem[addr_l + 1]) == 0x03
   && (MEMORY_mem[addr_l + 2]) == 0x8d && (MEMORY_mem[addr_l + 3]) == 0x2a
   && (MEMORY_mem[addr_l + 4]) == 0x02
   && (MEMORY_mem[addr_s]) == check_s_0
   && (MEMORY_mem[addr_s + 1]) == check_s_1
   && (MEMORY_mem[addr_s + 2]) == 0x20 && (MEMORY_mem[addr_s + 3]) == 0x5c
   && (MEMORY_mem[addr_s + 4]) == 0xe4) {
   ESC_Add(addr_l, ESC_COPENLOAD, CASSETTE_LeaderLoad);
   ESC_Add(addr_s, ESC_COPENSAVE, CASSETTE_LeaderSave);
  }
  ESC_AddEscRts(0xe459, ESC_SIOV, SIO_Handler);
  patched = 1;
 }
 else {
  ESC_Remove(ESC_COPENLOAD);
  ESC_Remove(ESC_COPENSAVE);
  ESC_Remove(ESC_SIOV);
 };
 if (patched && Atari800_machine_type == 2) {
  (MEMORY_mem[0xc314] = 0x8e);
  (MEMORY_mem[0xc315] = 0xff);
  (MEMORY_mem[0xc319] = 0x8e);
  (MEMORY_mem[0xc31a] = 0xff);
 }
}

variable_types: patched int


decompiled_code:
int *ESC_UpdatePatches()
{
int *result; // rax

result = &Atari800_machine_type;
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type >= 0 )
{
memcpy(&MEMORY_mem[6912], MEMORY_os, 0x2800uLL);
ESC_PatchOS();
result = (int *)Devices_UpdatePatches();
}
}
else if ( Atari800_machine_type == 2 && (PIA_PORTB & 1) != 0 )
{
MEMORY_mem[6144] = MEMORY_os[0];
MEMORY_mem[6655] = MEMORY_os[511];
qmemcpy(
(void *)((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)MEMORY_os
- ((char *)&MEMORY_mem[6144]
- ((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)&MEMORY_mem[6144] - ((unsigned int)&MEMORY_mem[6145] & 0xFFFFFFF8) + 4096) >> 3));
memcpy(&MEMORY_mem[6912], &MEMORY_os[768], 0x2800uLL);
ESC_PatchOS();
result = (int *)Devices_UpdatePatches();
}
return result;
}

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

result = &Atari800_machine_type;
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type >= 0 )
{
memcpy(&MEMORY_mem[6912], MEMORY_os, 0x2800uLL);
func5();
result = (int *)Devices_UpdatePatches();
}
}
else if ( Atari800_machine_type == 2 && (PIA_PORTB & 1) != 0 )
{
MEMORY_mem[6144] = MEMORY_os[0];
MEMORY_mem[6655] = MEMORY_os[511];
qmemcpy(
(void *)((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)MEMORY_os
- ((char *)&MEMORY_mem[6144]
- ((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)&MEMORY_mem[6144] - ((unsigned int)&MEMORY_mem[6145] & 0xFFFFFFF8) + 4096) >> 3));
memcpy(&MEMORY_mem[6912], &MEMORY_os[768], 0x2800uLL);
func5();
result = (int *)Devices_UpdatePatches();
}
return result;
}

original_code:
void ESC_UpdatePatches(void)
{
 switch (Atari800_machine_type) {
 case 0:
 case 1:
  memcpy(MEMORY_mem + (0xd800), MEMORY_os, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 case 2:
  if ((PIA_PORTB & 1) == 0)
   break;
  memcpy(MEMORY_mem + (0xc000), MEMORY_os, 0x1000);
  memcpy(MEMORY_mem + (0xd800), MEMORY_os + 0x1800, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 default:
  break;
 }
}

canonical_original_code:
void ESC_UpdatePatches()
{
 switch (Atari800_machine_type) {
 case 0:
 case 1:
  memcpy(MEMORY_mem + (0xd800), MEMORY_os, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 case 2:
  if ((PIA_PORTB & 1) == 0)
   break;
  memcpy(MEMORY_mem + (0xc000), MEMORY_os, 0x1000);
  memcpy(MEMORY_mem + (0xd800), MEMORY_os + 0x1800, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 default:
  break;
 }
}


decompiled_code:
char *__fastcall CFG_FindROMImages(__int64 a1, int a2)
{
char **v2; // r13
char **v3; // rbx
char *result; // rax
_BYTE savedregs[4152]; // [rsp+0h] [rbp+0h] BYREF

v2 = (char **)&rom_filenames_1;
v3 = common_filenames_2;
do
{
if ( !a2 || (result = *v2, **v2 == 10) )
{
while ( 1 )
{
Util_catpath(savedregs, a1, *v3);
result = (char *)Util_fileexists(savedregs);
if ( (_DWORD)result )
break;
if ( !v3[1] )
{
v3 += 2;
goto LABEL_8;
}
++v3;
}
result = strcpy(*v2, savedregs);
}
do
++v3;
while ( *(v3 - 1) );
LABEL_8:
++v2;
}
while ( v2 != (char **)(&rom_filenames_1 + 5) );
return result;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, int a2)
{
char **v2; // r13
char **v3; // rbx
char *result; // rax
_BYTE savedregs[4152]; // [rsp+0h] [rbp+0h] BYREF

v2 = (char **)&rom_filenames_1;
v3 = common_filenames_2;
do
{
if ( !a2 || (result = *v2, **v2 == 10) )
{
while ( 1 )
{
Util_catpath(savedregs, a1, *v3);
result = (char *)Util_fileexists(savedregs);
if ( (_DWORD)result )
break;
if ( !v3[1] )
{
v3 += 2;
goto LABEL_8;
}
++v3;
}
result = strcpy(*v2, savedregs);
}
do
++v3;
while ( *(v3 - 1) );
LABEL_8:
++v2;
}
while ( v2 != (char **)(&rom_filenames_1 + 5) );
return result;
}

original_code:
void CFG_FindROMImages(const char *directory, int only_if_not_set)
{
 static char * const rom_filenames[5] = {
  CFG_osa_filename,
  CFG_osb_filename,
  CFG_xlxe_filename,
  CFG_5200_filename,
  CFG_basic_filename
 };
 static const char * const common_filenames[] = {
  "atariosa.rom", "atari_osa.rom", "atari_os_a.rom",
  "ATARIOSA.ROM", "ATARI_OSA.ROM", "ATARI_OS_A.ROM",
  ((void *)0),
  "atariosb.rom", "atari_osb.rom", "atari_os_b.rom",
  "ATARIOSB.ROM", "ATARI_OSB.ROM", "ATARI_OS_B.ROM",
  ((void *)0),
  "atarixlxe.rom", "atarixl.rom", "atari_xlxe.rom", "atari_xl_xe.rom",
  "ATARIXLXE.ROM", "ATARIXL.ROM", "ATARI_XLXE.ROM", "ATARI_XL_XE.ROM",
  ((void *)0),
  "atari5200.rom", "atar5200.rom", "5200.rom", "5200.bin", "atari_5200.rom",
  "ATARI5200.ROM", "ATAR5200.ROM", "5200.ROM", "5200.BIN", "ATARI_5200.ROM",
  ((void *)0),
  "ataribasic.rom", "ataribas.rom", "basic.rom", "atari_basic.rom",
  "ATARIBASIC.ROM", "ATARIBAS.ROM", "BASIC.ROM", "ATARI_BASIC.ROM",
  ((void *)0)
 };
 const char * const *common_filename = common_filenames;
 int i;
 for (i = 0; i < 5; i++) {
  if (!only_if_not_set || ((rom_filenames[i])[0] == '\n')) {
   do {
    char full_filename[4096];
    Util_catpath(full_filename, directory, *common_filename);
    if (Util_fileexists(full_filename)) {
     strcpy(rom_filenames[i], full_filename);
     break;
    }
   } while (*++common_filename != ((void *)0));
  }
  while (*common_filename++ != ((void *)0));
 }
}

canonical_original_code:
void CFG_FindROMImages(char *directory, int only_if_not_set)
{
 static char *rom_filenames[5] = {
  CFG_osa_filename,
  CFG_osb_filename,
  CFG_xlxe_filename,
  CFG_5200_filename,
  CFG_basic_filename
 };
 static char *common_filenames[] = {
  "atariosa.rom", "atari_osa.rom", "atari_os_a.rom",
  "ATARIOSA.ROM", "ATARI_OSA.ROM", "ATARI_OS_A.ROM",
  ((void *)0),
  "atariosb.rom", "atari_osb.rom", "atari_os_b.rom",
  "ATARIOSB.ROM", "ATARI_OSB.ROM", "ATARI_OS_B.ROM",
  ((void *)0),
  "atarixlxe.rom", "atarixl.rom", "atari_xlxe.rom", "atari_xl_xe.rom",
  "ATARIXLXE.ROM", "ATARIXL.ROM", "ATARI_XLXE.ROM", "ATARI_XL_XE.ROM",
  ((void *)0),
  "atari5200.rom", "atar5200.rom", "5200.rom", "5200.bin", "atari_5200.rom",
  "ATARI5200.ROM", "ATAR5200.ROM", "5200.ROM", "5200.BIN", "ATARI_5200.ROM",
  ((void *)0),
  "ataribasic.rom", "ataribas.rom", "basic.rom", "atari_basic.rom",
  "ATARIBASIC.ROM", "ATARIBAS.ROM", "BASIC.ROM", "ATARI_BASIC.ROM",
  ((void *)0)
 };
 char **common_filename = common_filenames;
 int i;
 for (i = 0; i < 5; i++) {
  if (!only_if_not_set || ((rom_filenames[i])[0] == '\n')) {
   do {
    char full_filename[4096];
    Util_catpath(full_filename, directory, *common_filename);
    if (Util_fileexists(full_filename)) {
     strcpy(rom_filenames[i], full_filename);
     break;
    }
   } while (*++common_filename != ((void *)0));
  }
  while (*common_filename++ != ((void *)0));
 }
}

variable_types:
directory
char *


decompiled_code:
__int64 __fastcall CFG_MatchTextParameter(char *s1, const char **a2, int a3)
{
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edx
unsigned int v6; // er14
__int64 v7; // rbx

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = (unsigned int)a3;
v4 = 0LL;
v5 = a3 & 7;
if ( v5 )
{
if ( v5 != 1LL )
{
if ( v5 != 2LL )
{
if ( v5 != 3LL )
{
if ( v5 != 4LL )
{
if ( v5 != 5LL )
{
if ( v5 != 6LL )
{
v6 = 0;
if ( !strcasecmp(s1, *a2) )
return v6;
v4 = 1LL;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( strcasecmp(s1, a2[v4]) )
{
if ( ++v4 != v3 )
goto LABEL_23;
LABEL_32:
v6 = -1;
}
}
else
{
LABEL_23:
while ( 1 )
{
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
break;
v7 = v4 + 1;
v6 = v7;
if ( !strcasecmp(s1, a2[v7]) )
break;
v6 = v7 + 1;
if ( !strcasecmp(s1, a2[v7 + 1]) )
break;
v6 = v7 + 2;
if ( !strcasecmp(s1, a2[v7 + 2]) )
break;
v6 = v7 + 3;
if ( !strcasecmp(s1, a2[v7 + 3]) )
break;
v6 = v7 + 4;
if ( !strcasecmp(s1, a2[v7 + 4]) )
break;
v6 = v7 + 5;
if ( !strcasecmp(s1, a2[v7 + 5]) )
break;
v6 = v7 + 6;
if ( !strcasecmp(s1, a2[v7 + 6]) )
break;
v4 = v7 + 7;
if ( v4 == v3 )
goto LABEL_32;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1, const char **a2, int a3)
{
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edx
unsigned int v6; // er14
__int64 v7; // rbx

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = (unsigned int)a3;
v4 = 0LL;
v5 = a3 & 7;
if ( v5 )
{
if ( v5 != 1LL )
{
if ( v5 != 2LL )
{
if ( v5 != 3LL )
{
if ( v5 != 4LL )
{
if ( v5 != 5LL )
{
if ( v5 != 6LL )
{
v6 = 0;
if ( !strcasecmp(s1, *a2) )
return v6;
v4 = 1LL;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( strcasecmp(s1, a2[v4]) )
{
if ( ++v4 != v3 )
goto LABEL_23;
LABEL_32:
v6 = -1;
}
}
else
{
LABEL_23:
while ( 1 )
{
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
break;
v7 = v4 + 1;
v6 = v7;
if ( !strcasecmp(s1, a2[v7]) )
break;
v6 = v7 + 1;
if ( !strcasecmp(s1, a2[v7 + 1]) )
break;
v6 = v7 + 2;
if ( !strcasecmp(s1, a2[v7 + 2]) )
break;
v6 = v7 + 3;
if ( !strcasecmp(s1, a2[v7 + 3]) )
break;
v6 = v7 + 4;
if ( !strcasecmp(s1, a2[v7 + 4]) )
break;
v6 = v7 + 5;
if ( !strcasecmp(s1, a2[v7 + 5]) )
break;
v6 = v7 + 6;
if ( !strcasecmp(s1, a2[v7 + 6]) )
break;
v4 = v7 + 7;
if ( v4 == v3 )
goto LABEL_32;
}
}
return v6;
}

original_code:
int CFG_MatchTextParameter(char const *param, char const * const cfg_strings[], int cfg_strings_size)
{
 int i;
 for (i = 0; i < cfg_strings_size; i ++) {
  if (strcasecmp(param, cfg_strings[i]) == 0)
   return i;
 }
 return -1;
}

canonical_original_code:
int CFG_MatchTextParameter(char *param, char *cfg_strings[], int cfg_strings_size)
{
 int i;
 for (i = 0; i < cfg_strings_size; i ++) {
  if (strcasecmp(param, cfg_strings[i]) == 0)
   return i;
 }
 return -1;
}

variable_types:
param
char *


decompiled_code:
void init_mio()
{
free(mio_rom);
mio_rom = (void *)Util_malloc(0x4000LL);
if ( (unsigned int)Atari800_LoadImage(&mio_rom_filename, mio_rom, 0x4000LL) )
{
PBI_MIO_enabled = 1;
if ( PBI_SCSI_disk )
fclose(PBI_SCSI_disk);
if ( mio_scsi_disk_filename != 10 )
{
PBI_SCSI_disk = fopen(&mio_scsi_disk_filename, "rb+");
if ( PBI_SCSI_disk )
{
mio_scsi_enabled = 1;
LABEL_7:
free(mio_ram);
mio_ram = (void *)Util_malloc(mio_ram_size);
memset(mio_ram, 0, mio_ram_size);
return;
}
Log_print("Error opening SCSI disk image:%s", &mio_scsi_disk_filename);
}
if ( !mio_scsi_enabled )
PBI_SCSI_BSY = 1;
goto LABEL_7;
}
free(mio_rom);
}

canonical_decompiled_code:
void func0()
{
free(mio_rom);
mio_rom = (void *)Util_malloc(0x4000LL);
if ( (unsigned int)Atari800_LoadImage(&mio_rom_filename, mio_rom, 0x4000LL) )
{
PBI_MIO_enabled = 1;
if ( PBI_SCSI_disk )
fclose(PBI_SCSI_disk);
if ( mio_scsi_disk_filename != 10 )
{
PBI_SCSI_disk = fopen(&mio_scsi_disk_filename, "rb+");
if ( PBI_SCSI_disk )
{
mio_scsi_enabled = 1;
LABEL_7:
free(mio_ram);
mio_ram = (void *)Util_malloc(mio_ram_size);
memset(mio_ram, 0, mio_ram_size);
return;
}
Log_print("Error opening SCSI disk image:%s", &mio_scsi_disk_filename);
}
if ( !mio_scsi_enabled )
PBI_SCSI_BSY = 1;
goto LABEL_7;
}
free(mio_rom);
}

original_code:
static void init_mio(void)
{
 free(mio_rom);
 mio_rom = (unsigned char *)Util_malloc(mio_rom_size);
 if (!Atari800_LoadImage(mio_rom_filename, mio_rom, mio_rom_size)) {
  free(mio_rom);
  return;
 }
 do{}while(0);
 PBI_MIO_enabled = 1;
 if (PBI_SCSI_disk != ((void *)0)) fclose(PBI_SCSI_disk);
 if (!((mio_scsi_disk_filename)[0] == '\n')) {
  PBI_SCSI_disk = fopen(mio_scsi_disk_filename, "rb+");
  if (PBI_SCSI_disk == ((void *)0)) {
   Log_print("Error opening SCSI disk image:%s", mio_scsi_disk_filename);
  }
  else {
   do{}while(0);
   mio_scsi_enabled = 1;
  }
 }
 if (!mio_scsi_enabled) {
  PBI_SCSI_BSY = 1;
 }
 free(mio_ram);
 mio_ram = (unsigned char *)Util_malloc(mio_ram_size);
 memset(mio_ram, 0, mio_ram_size);
}

canonical_original_code:
static void init_mio()
{
 free(mio_rom);
 mio_rom = (unsigned char *)Util_malloc(mio_rom_size);
 if (!Atari800_LoadImage(mio_rom_filename, mio_rom, mio_rom_size)) {
  free(mio_rom);
  return;
 }
 do{}while(0);
 PBI_MIO_enabled = 1;
 if (PBI_SCSI_disk != ((void *)0)) fclose(PBI_SCSI_disk);
 if (!((mio_scsi_disk_filename)[0] == '\n')) {
  PBI_SCSI_disk = fopen(mio_scsi_disk_filename, "rb+");
  if (PBI_SCSI_disk == ((void *)0)) {
   Log_print("Error opening SCSI disk image:%s", mio_scsi_disk_filename);
  }
  else {
   do{}while(0);
   mio_scsi_enabled = 1;
  }
 }
 if (!mio_scsi_enabled) {
  PBI_SCSI_BSY = 1;
 }
 free(mio_ram);
 mio_ram = (unsigned char *)Util_malloc(mio_ram_size);
 memset(mio_ram, 0, mio_ram_size);
}


decompiled_code:
__int64 __fastcall PBI_MIO_Initialise(int *a1, __int64 a2)
{
int v4; // er12
int v5; // ebx
const char **v6; // rbp
bool v7; // r8
bool v8; // cf
bool v9; // zf
__int64 v10; // rcx
const char *v11; // rsi
const char *v12; // rdi

if ( *a1 <= 1 )
{
v4 = 1;
}
else
{
v4 = 1;
v5 = 1;
do
{
v6 = (const char **)(a2 + 8LL * v5);
v7 = strcmp(*v6, "-mio") != 0;
v8 = 0;
v9 = !v7;
if ( v7 )
{
v10 = 6LL;
v11 = *v6;
v12 = "-help";
do
{
if ( !v10 )
break;
v8 = *v11 < (unsigned int)*v12;
v9 = *v11++ == *v12++;
--v10;
}
while ( v9 );
if ( (!v8 && !v9) == v8 )
Log_print("\t-mio Emulate the ICD MIO board", v11, *v6, v10);
*(_QWORD *)(a2 + 8LL * v4++) = *v6;
}
else
{
init_mio();
}
++v5;
}
while ( *a1 > v5 );
}
*a1 = v4;
return 1LL;
}

original_code:
int PBI_MIO_Initialise(int *argc, char *argv[])
{
 int i, j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-mio") == 0) {
   init_mio();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-mio             Emulate the ICD MIO board");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

canonical_original_code:
int PBI_MIO_Initialise(int *argc, char *argv[])
{
 int i; int j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-mio") == 0) {
   init_mio();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-mio             Emulate the ICD MIO board");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: j int


decompiled_code:
__int64 __fastcall PBI_MIO_ReadConfig(const char *a1, __int64 a2)
{
unsigned int v2; // er10

if ( !strcmp(a1, "MIO_ROM") )
{
Util_strlcpy(&mio_rom_filename, a2, 4096LL);
v2 = 1;
}
else
{
v2 = 0;
if ( !strcmp(a1, "MIO_SCSI_DISK") )
{
Util_strlcpy(&mio_scsi_disk_filename, a2, 4096LL);
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, __int64 a2)
{
unsigned int v2; // er10

if ( !strcmp(a1, "MIO_ROM") )
{
Util_strlcpy(&mio_rom_filename, a2, 4096LL);
v2 = 1;
}
else
{
v2 = 0;
if ( !strcmp(a1, "MIO_SCSI_DISK") )
{
Util_strlcpy(&mio_scsi_disk_filename, a2, 4096LL);
v2 = 1;
}
}
return v2;
}

original_code:
int PBI_MIO_ReadConfig(char *string, char *ptr)
{
 if (strcmp(string, "MIO_ROM") == 0)
  Util_strlcpy(mio_rom_filename, ptr, sizeof(mio_rom_filename));
 else if (strcmp(string, "MIO_SCSI_DISK") == 0)
  Util_strlcpy(mio_scsi_disk_filename, ptr, sizeof(mio_scsi_disk_filename));
 else return 0;
 return 1;
}

variable_types:
string
char *


decompiled_code:
__int64 __fastcall PBI_MIO_D1GetByte(int a1, __int64 a2, __int64 a3, int a4, __int64 a5, int a6)
{
int v6; // ebx
unsigned int v7; // edi
unsigned int v8; // ebx

v7 = a1 & 0xFFFFFFE3;
if ( (_WORD)v7 == 0xD1E2 )
{
LOBYTE(v6) = PBI_SCSI_CD == 0;
LOBYTE(a4) = PBI_SCSI_MSG == 0;
LOBYTE(v7) = PBI_SCSI_IO == 0;
LOBYTE(a6) = PBI_SCSI_BSY == 0;
v8 = (PBI_SCSI_REQ == 0 ? 0xFFFFFF80 : 0) | (32 * a6) | (4 * v7) | (2 * a4) | v6;
}
else
{
v8 = 0;
if ( (_WORD)v7 == 0xD1E1 && mio_scsi_enabled )
{
v8 = ~(unsigned int)PBI_SCSI_GetByte();
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, __int64 a2, __int64 a3, int a4, __int64 a5, int a6)
{
int v6; // ebx
unsigned int v7; // edi
unsigned int v8; // ebx

v7 = a1 & 0xFFFFFFE3;
if ( (_WORD)v7 == 0xD1E2 )
{
LOBYTE(v6) = PBI_SCSI_CD == 0;
LOBYTE(a4) = PBI_SCSI_MSG == 0;
LOBYTE(v7) = PBI_SCSI_IO == 0;
LOBYTE(a6) = PBI_SCSI_BSY == 0;
v8 = (PBI_SCSI_REQ == 0 ? 0xFFFFFF80 : 0) | (32 * a6) | (4 * v7) | (2 * a4) | v6;
}
else
{
v8 = 0;
if ( (_WORD)v7 == 0xD1E1 && mio_scsi_enabled )
{
v8 = ~(unsigned int)PBI_SCSI_GetByte();
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
}
}
return v8;
}

original_code:
unsigned char PBI_MIO_D1GetByte(unsigned short addr)
{
 unsigned char result = 0x00; ;
 addr &= 0xffe3;
 do{}while(0);
 if (addr == 0xd1e2) {
  result = ((!PBI_SCSI_CD) | (!PBI_SCSI_MSG<<1) | (!PBI_SCSI_IO<<2) | (!PBI_SCSI_BSY<<5) | (!PBI_SCSI_REQ<<7));
 }
 else if (addr == 0xd1e1) {
  if (mio_scsi_enabled) {
   result = PBI_SCSI_GetByte()^0xff;
   PBI_SCSI_PutACK(1);
   PBI_SCSI_PutACK(0);
  }
 }
 return result;
}

variable_types:
addr
unsigned short

variable_types:
result
unsigned char

return_type: unsigned char


decompiled_code:
const __m128i *__fastcall PBI_MIO_D1PutByte(__int16 a1, unsigned int a2)
{
const __m128i *result; // rax
int v3; // ebx
int v4; // ebp
__int16 v5; // di
int v6; // er10
__m128i *v7; // r8
const __m128i *v8; // rbp
__int64 v9; // r9
bool v10; // bp
__int64 v11; // rbx
char *v12; // rsi
__m128i *v13; // rdi

result = (const __m128i *)a2;
v3 = mio_ram_bank_offset;
v4 = mio_ram_enabled;
v5 = a1 & 0xFFE3;
switch ( v5 )
{
case -11808:
mio_ram_bank_offset = mio_ram_bank_offset & 0xF0000 | ((unsigned __int8)a2 << 8);
goto LABEL_12;
case -11807:
if ( !mio_scsi_enabled )
goto LABEL_12;
PBI_SCSI_PutByte((unsigned __int8)~(_BYTE)a2);
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
LABEL_19:
result = (const __m128i *)(unsigned int)mio_ram_bank_offset;
v10 = mio_ram_enabled != v4;
if ( v10 && mio_ram_enabled )
{
result = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(result);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(result + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(result + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(result + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(result + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(result + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(result + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(result + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(result + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(result + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(result + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(result + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(result + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(result + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(result + 14);
v11 = result[15].m128i_i64[1];
*((_QWORD *)&MEMORY_mem + 6878) = result[15].m128i_i64[0];
*((_QWORD *)&MEMORY_mem + 6879) = v11;
}
else
{
if ( mio_ram_bank_offset != v3 && mio_ram_enabled )
goto LABEL_14;
if ( !mio_ram_enabled && v10 )
{
v13 = (__m128i *)((char *)mio_ram + v3);
*v13 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v13[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v13[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v13[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v13[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v13[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v13[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v13[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v13[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v13[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v13[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v13[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v13[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v13[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v13[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v13[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((_QWORD *)&MEMORY_mem + 6848) = -1LL;
*((_QWORD *)&MEMORY_mem + 6849) = -1LL;
*((_QWORD *)&MEMORY_mem + 6850) = -1LL;
*((_QWORD *)&MEMORY_mem + 6851) = -1LL;
*((_QWORD *)&MEMORY_mem + 6852) = -1LL;
*((_QWORD *)&MEMORY_mem + 6853) = -1LL;
*((_QWORD *)&MEMORY_mem + 6854) = -1LL;
*((_QWORD *)&MEMORY_mem + 6855) = -1LL;
*((_QWORD *)&MEMORY_mem + 6856) = -1LL;
*((_QWORD *)&MEMORY_mem + 6857) = -1LL;
*((_QWORD *)&MEMORY_mem + 6858) = -1LL;
*((_QWORD *)&MEMORY_mem + 6859) = -1LL;
*((_QWORD *)&MEMORY_mem + 6860) = -1LL;
*((_QWORD *)&MEMORY_mem + 6861) = -1LL;
*((_QWORD *)&MEMORY_mem + 6862) = -1LL;
*((_QWORD *)&MEMORY_mem + 6863) = -1LL;
*((_QWORD *)&MEMORY_mem + 6864) = -1LL;
*((_QWORD *)&MEMORY_mem + 6865) = -1LL;
*((_QWORD *)&MEMORY_mem + 6866) = -1LL;
*((_QWORD *)&MEMORY_mem + 6867) = -1LL;
*((_QWORD *)&MEMORY_mem + 6868) = -1LL;
*((_QWORD *)&MEMORY_mem + 6869) = -1LL;
*((_QWORD *)&MEMORY_mem + 6870) = -1LL;
*((_QWORD *)&MEMORY_mem + 6871) = -1LL;
*((_QWORD *)&MEMORY_mem + 6872) = -1LL;
*((_QWORD *)&MEMORY_mem + 6873) = -1LL;
*((_QWORD *)&MEMORY_mem + 6874) = -1LL;
*((_QWORD *)&MEMORY_mem + 6875) = -1LL;
*((_QWORD *)&MEMORY_mem + 6876) = -1LL;
*((_QWORD *)&MEMORY_mem + 6877) = -1LL;
*((_QWORD *)&MEMORY_mem + 6878) = -1LL;
*((_QWORD *)&MEMORY_mem + 6879) = -1LL;
}
}
return result;
case -11806:
mio_ram_bank_offset = (unsigned __int16)mio_ram_bank_offset | (a2 << 16) & 0xF0000;
mio_ram_enabled = a2 & 0x20;
if ( mio_scsi_enabled )
PBI_SCSI_PutSEL((a2 & 0x10) != 0);
goto LABEL_19;
}
if ( v5 == -11805 && mio_rom_bank != (_BYTE)a2 )
{
switch ( (_BYTE)a2 )
{
case 4:
v6 = 0x2000;
break;
case 8:
v6 = 10240;
break;
case 0x10:
v6 = 12288;
break;
default:
v6 = 14336;
if ( (_BYTE)a2 != 32 )
{
*((_QWORD *)&MEMORY_mem + 6912) = MEMORY_os[768];
*((_QWORD *)&MEMORY_mem + 7167) = MEMORY_os[1023];
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)&MEMORY_os[768]
- ((char *)&MEMORY_mem
- (((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)
+ 55296)),
8LL * (((unsigned int)&MEMORY_mem + 55296 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
LABEL_26:
mio_rom_bank = (char)result;
goto LABEL_12;
}
break;
}
v12 = (char *)mio_rom + v6;
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v12;
*((_QWORD *)&MEMORY_mem + 7167) = *((_QWORD *)v12 + 255);
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v12
- ((char *)&MEMORY_mem
- (((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)
+ 55296)),
8LL * (((unsigned int)&MEMORY_mem + 55296 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
goto LABEL_26;
}
LABEL_12:
if ( mio_ram_bank_offset != v3 && v4 )
{
LABEL_14:
v7 = (__m128i *)((char *)mio_ram + v3);
result = (const __m128i *)&MEMORY_mem;
*v7 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v7[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v7[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v7[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v7[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v7[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v7[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v7[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v7[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v7[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v7[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v7[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v7[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v7[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v7[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v7[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
v8 = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(v8);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(v8 + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(v8 + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(v8 + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(v8 + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(v8 + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(v8 + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(v8 + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(v8 + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(v8 + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(v8 + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(v8 + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(v8 + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(v8 + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(v8 + 14);
v9 = v8[15].m128i_i64[1];
*((_QWORD *)&MEMORY_mem + 6878) = v8[15].m128i_i64[0];
*((_QWORD *)&MEMORY_mem + 6879) = v9;
}
return result;
}

canonical_decompiled_code:
const __m128i *__fastcall func5(__int16 a1, unsigned int a2)
{
const __m128i *result; // rax
int v3; // ebx
int v4; // ebp
__int16 v5; // di
int v6; // er10
__m128i *v7; // r8
const __m128i *v8; // rbp
__int64 v9; // r9
bool v10; // bp
__int64 v11; // rbx
char *v12; // rsi
__m128i *v13; // rdi

result = (const __m128i *)a2;
v3 = mio_ram_bank_offset;
v4 = mio_ram_enabled;
v5 = a1 & 0xFFE3;
switch ( v5 )
{
case -11808:
mio_ram_bank_offset = mio_ram_bank_offset & 0xF0000 | ((unsigned __int8)a2 << 8);
goto LABEL_12;
case -11807:
if ( !mio_scsi_enabled )
goto LABEL_12;
PBI_SCSI_PutByte((unsigned __int8)~(_BYTE)a2);
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
LABEL_19:
result = (const __m128i *)(unsigned int)mio_ram_bank_offset;
v10 = mio_ram_enabled != v4;
if ( v10 && mio_ram_enabled )
{
result = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(result);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(result + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(result + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(result + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(result + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(result + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(result + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(result + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(result + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(result + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(result + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(result + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(result + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(result + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(result + 14);
v11 = result[15].m128i_i64[1];
*((_QWORD *)&MEMORY_mem + 6878) = result[15].m128i_i64[0];
*((_QWORD *)&MEMORY_mem + 6879) = v11;
}
else
{
if ( mio_ram_bank_offset != v3 && mio_ram_enabled )
goto LABEL_14;
if ( !mio_ram_enabled && v10 )
{
v13 = (__m128i *)((char *)mio_ram + v3);
*v13 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v13[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v13[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v13[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v13[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v13[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v13[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v13[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v13[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v13[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v13[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v13[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v13[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v13[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v13[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v13[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((_QWORD *)&MEMORY_mem + 6848) = -1LL;
*((_QWORD *)&MEMORY_mem + 6849) = -1LL;
*((_QWORD *)&MEMORY_mem + 6850) = -1LL;
*((_QWORD *)&MEMORY_mem + 6851) = -1LL;
*((_QWORD *)&MEMORY_mem + 6852) = -1LL;
*((_QWORD *)&MEMORY_mem + 6853) = -1LL;
*((_QWORD *)&MEMORY_mem + 6854) = -1LL;
*((_QWORD *)&MEMORY_mem + 6855) = -1LL;
*((_QWORD *)&MEMORY_mem + 6856) = -1LL;
*((_QWORD *)&MEMORY_mem + 6857) = -1LL;
*((_QWORD *)&MEMORY_mem + 6858) = -1LL;
*((_QWORD *)&MEMORY_mem + 6859) = -1LL;
*((_QWORD *)&MEMORY_mem + 6860) = -1LL;
*((_QWORD *)&MEMORY_mem + 6861) = -1LL;
*((_QWORD *)&MEMORY_mem + 6862) = -1LL;
*((_QWORD *)&MEMORY_mem + 6863) = -1LL;
*((_QWORD *)&MEMORY_mem + 6864) = -1LL;
*((_QWORD *)&MEMORY_mem + 6865) = -1LL;
*((_QWORD *)&MEMORY_mem + 6866) = -1LL;
*((_QWORD *)&MEMORY_mem + 6867) = -1LL;
*((_QWORD *)&MEMORY_mem + 6868) = -1LL;
*((_QWORD *)&MEMORY_mem + 6869) = -1LL;
*((_QWORD *)&MEMORY_mem + 6870) = -1LL;
*((_QWORD *)&MEMORY_mem + 6871) = -1LL;
*((_QWORD *)&MEMORY_mem + 6872) = -1LL;
*((_QWORD *)&MEMORY_mem + 6873) = -1LL;
*((_QWORD *)&MEMORY_mem + 6874) = -1LL;
*((_QWORD *)&MEMORY_mem + 6875) = -1LL;
*((_QWORD *)&MEMORY_mem + 6876) = -1LL;
*((_QWORD *)&MEMORY_mem + 6877) = -1LL;
*((_QWORD *)&MEMORY_mem + 6878) = -1LL;
*((_QWORD *)&MEMORY_mem + 6879) = -1LL;
}
}
return result;
case -11806:
mio_ram_bank_offset = (unsigned __int16)mio_ram_bank_offset | (a2 << 16) & 0xF0000;
mio_ram_enabled = a2 & 0x20;
if ( mio_scsi_enabled )
PBI_SCSI_PutSEL((a2 & 0x10) != 0);
goto LABEL_19;
}
if ( v5 == -11805 && mio_rom_bank != (_BYTE)a2 )
{
switch ( (_BYTE)a2 )
{
case 4:
v6 = 0x2000;
break;
case 8:
v6 = 10240;
break;
case 0x10:
v6 = 12288;
break;
default:
v6 = 14336;
if ( (_BYTE)a2 != 32 )
{
*((_QWORD *)&MEMORY_mem + 6912) = MEMORY_os[768];
*((_QWORD *)&MEMORY_mem + 7167) = MEMORY_os[1023];
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)&MEMORY_os[768]
- ((char *)&MEMORY_mem
- (((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)
+ 55296)),
8LL * (((unsigned int)&MEMORY_mem + 55296 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
LABEL_26:
mio_rom_bank = (char)result;
goto LABEL_12;
}
break;
}
v12 = (char *)mio_rom + v6;
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v12;
*((_QWORD *)&MEMORY_mem + 7167) = *((_QWORD *)v12 + 255);
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v12
- ((char *)&MEMORY_mem
- (((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)
+ 55296)),
8LL * (((unsigned int)&MEMORY_mem + 55296 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
goto LABEL_26;
}
LABEL_12:
if ( mio_ram_bank_offset != v3 && v4 )
{
LABEL_14:
v7 = (__m128i *)((char *)mio_ram + v3);
result = (const __m128i *)&MEMORY_mem;
*v7 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v7[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v7[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v7[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v7[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v7[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v7[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v7[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v7[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v7[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v7[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v7[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v7[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v7[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v7[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v7[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
v8 = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(v8);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(v8 + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(v8 + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(v8 + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(v8 + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(v8 + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(v8 + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(v8 + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(v8 + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(v8 + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(v8 + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(v8 + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(v8 + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(v8 + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(v8 + 14);
v9 = v8[15].m128i_i64[1];
*((_QWORD *)&MEMORY_mem + 6878) = v8[15].m128i_i64[0];
*((_QWORD *)&MEMORY_mem + 6879) = v9;
}
return result;
}

original_code:
void PBI_MIO_D1PutByte(unsigned short addr, unsigned char byte)
{
 int old_mio_ram_bank_offset = mio_ram_bank_offset;
 int old_mio_ram_enabled = mio_ram_enabled;
 int offset_changed;
 int ram_enabled_changed;
 addr &= 0xffe3;
 if (addr == 0xd1e0) {
  mio_ram_bank_offset &= 0xf0000;
  mio_ram_bank_offset |= (byte << 8);
 }
 else if (addr == 0xd1e1) {
  if (mio_scsi_enabled) {
   PBI_SCSI_PutByte(byte^0xff);
   PBI_SCSI_PutACK(1);
   PBI_SCSI_PutACK(0);
  }
 }
 else if (addr == 0xd1e2) {
  mio_ram_bank_offset &= 0x0ffff;
  mio_ram_bank_offset |= ( (byte & 0x0f) << 16);
  mio_ram_enabled = (byte & 0x20);
  if (mio_scsi_enabled) PBI_SCSI_PutSEL(!!(byte & 0x10));
 }
 else if (addr == 0xd1e3) {
  if (mio_rom_bank != byte){
   int offset = -1;
   if (byte == 4) offset = 0x2000;
   else if (byte == 8) offset = 0x2800;
   else if (byte == 0x10) offset = 0x3000;
   else if (byte == 0x20) offset = 0x3800;
   if (offset != -1) {
    memcpy(MEMORY_mem + 0xd800, mio_rom+offset, 0x800);
    do{}while(0);
   }else{
    memcpy(MEMORY_mem + 0xd800, MEMORY_os + 0x1800, 0x800);
    do{}while(0);
   }
   mio_rom_bank = byte;
  }
 }
 offset_changed = (old_mio_ram_bank_offset != mio_ram_bank_offset);
 ram_enabled_changed = (old_mio_ram_enabled != mio_ram_enabled);
 if (mio_ram_enabled && ram_enabled_changed) {
  memcpy(MEMORY_mem + 0xd600, mio_ram + mio_ram_bank_offset, 0x100);
 } else if (mio_ram_enabled && offset_changed) {
  memcpy(mio_ram + old_mio_ram_bank_offset,MEMORY_mem + 0xd600, 0x100);
  memcpy(MEMORY_mem + 0xd600, mio_ram + mio_ram_bank_offset, 0x100);
 } else if (!mio_ram_enabled && ram_enabled_changed) {
  memcpy(mio_ram + old_mio_ram_bank_offset, MEMORY_mem + 0xd600, 0x100);
  memset(MEMORY_mem + 0xd600, 0xff, 0x100);
 }
 do{}while(0);
}

variable_types:
byte
unsigned char

variable_types: offset int


decompiled_code:
__int64 __fastcall PBI_MIO_D6GetByte(unsigned __int16 a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( mio_ram_enabled )
result = *((unsigned __int8 *)&MEMORY_mem + a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int16 a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( mio_ram_enabled )
result = *((unsigned __int8 *)&MEMORY_mem + a1);
return result;
}

original_code:
unsigned char PBI_MIO_D6GetByte(unsigned short addr)
{
 if (!mio_ram_enabled) return 0xff;
 return MEMORY_mem[addr];
}


decompiled_code:
void *__fastcall PBI_MIO_D6PutByte(unsigned __int16 a1, char a2)
{
void *result; // rax

if ( mio_ram_enabled )
{
result = &MEMORY_mem;
*((_BYTE *)&MEMORY_mem + a1) = a2;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func7(unsigned __int16 a1, char a2)
{
void *result; // rax

if ( mio_ram_enabled )
{
result = &MEMORY_mem;
*((_BYTE *)&MEMORY_mem + a1) = a2;
}
return result;
}

original_code:
void PBI_MIO_D6PutByte(unsigned short addr, unsigned char byte)
{
 if (!mio_ram_enabled) return;
 MEMORY_mem[addr]=byte;
}


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

result = StateSav_SaveINT(&PBI_MIO_enabled, 1LL);
if ( PBI_MIO_enabled )
{
StateSav_SaveFNAME(&mio_scsi_disk_filename);
StateSav_SaveFNAME(mio_rom_filename);
StateSav_SaveINT(&mio_ram_size, 1LL);
StateSav_SaveINT(&mio_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(mio_ram, (unsigned int)mio_ram_size);
StateSav_SaveUBYTE(&mio_rom_bank, 1LL);
result = StateSav_SaveINT(&mio_ram_enabled, 1LL);
}
return result;
}

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

result = StateSav_SaveINT(&PBI_MIO_enabled, 1LL);
if ( PBI_MIO_enabled )
{
StateSav_SaveFNAME(&mio_scsi_disk_filename);
StateSav_SaveFNAME(mio_rom_filename);
StateSav_SaveINT(&mio_ram_size, 1LL);
StateSav_SaveINT(&mio_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(mio_ram, (unsigned int)mio_ram_size);
StateSav_SaveUBYTE(&mio_rom_bank, 1LL);
result = StateSav_SaveINT(&mio_ram_enabled, 1LL);
}
return result;
}

original_code:
void PBI_MIO_StateSave(void)
{
 StateSav_SaveINT(&PBI_MIO_enabled, 1);
 if (PBI_MIO_enabled) {
  StateSav_SaveFNAME(mio_scsi_disk_filename);
  StateSav_SaveFNAME(mio_rom_filename);
  StateSav_SaveINT(&mio_ram_size, 1);
  StateSav_SaveINT(&mio_ram_bank_offset, 1);
  StateSav_SaveUBYTE(mio_ram, mio_ram_size);
  StateSav_SaveUBYTE(&mio_rom_bank, 1);
  StateSav_SaveINT(&mio_ram_enabled, 1);
 }
}

canonical_original_code:
void PBI_MIO_StateSave()
{
 StateSav_SaveINT(&PBI_MIO_enabled, 1);
 if (PBI_MIO_enabled) {
  StateSav_SaveFNAME(mio_scsi_disk_filename);
  StateSav_SaveFNAME(mio_rom_filename);
  StateSav_SaveINT(&mio_ram_size, 1);
  StateSav_SaveINT(&mio_ram_bank_offset, 1);
  StateSav_SaveUBYTE(mio_ram, mio_ram_size);
  StateSav_SaveUBYTE(&mio_rom_bank, 1);
  StateSav_SaveINT(&mio_ram_enabled, 1);
 }
}


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

result = StateSav_ReadINT(&PBI_MIO_enabled, 1LL);
if ( PBI_MIO_enabled )
{
StateSav_ReadFNAME(&mio_scsi_disk_filename);
StateSav_ReadFNAME(mio_rom_filename);
StateSav_ReadINT(&mio_ram_size, 1LL);
init_mio();
StateSav_ReadINT(&mio_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(mio_ram, (unsigned int)mio_ram_size);
StateSav_ReadUBYTE(&mio_rom_bank, 1LL);
result = StateSav_ReadINT(&mio_ram_enabled, 1LL);
}
return result;
}

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

result = StateSav_ReadINT(&PBI_MIO_enabled, 1LL);
if ( PBI_MIO_enabled )
{
StateSav_ReadFNAME(&mio_scsi_disk_filename);
StateSav_ReadFNAME(mio_rom_filename);
StateSav_ReadINT(&mio_ram_size, 1LL);
func0();
StateSav_ReadINT(&mio_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(mio_ram, (unsigned int)mio_ram_size);
StateSav_ReadUBYTE(&mio_rom_bank, 1LL);
result = StateSav_ReadINT(&mio_ram_enabled, 1LL);
}
return result;
}

original_code:
void PBI_MIO_StateRead(void)
{
 StateSav_ReadINT(&PBI_MIO_enabled, 1);
 if (PBI_MIO_enabled) {
  StateSav_ReadFNAME(mio_scsi_disk_filename);
  StateSav_ReadFNAME(mio_rom_filename);
  StateSav_ReadINT(&mio_ram_size, 1);
  init_mio();
  StateSav_ReadINT(&mio_ram_bank_offset, 1);
  StateSav_ReadUBYTE(mio_ram, mio_ram_size);
  StateSav_ReadUBYTE(&mio_rom_bank, 1);
  StateSav_ReadINT(&mio_ram_enabled, 1);
 }
}

canonical_original_code:
void PBI_MIO_StateRead()
{
 StateSav_ReadINT(&PBI_MIO_enabled, 1);
 if (PBI_MIO_enabled) {
  StateSav_ReadFNAME(mio_scsi_disk_filename);
  StateSav_ReadFNAME(mio_rom_filename);
  StateSav_ReadINT(&mio_ram_size, 1);
  init_mio();
  StateSav_ReadINT(&mio_ram_bank_offset, 1);
  StateSav_ReadUBYTE(mio_ram, mio_ram_size);
  StateSav_ReadUBYTE(&mio_rom_bank, 1);
  StateSav_ReadINT(&mio_ram_enabled, 1);
 }
}


decompiled_code:
__int64 __fastcall PBI_Initialise(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = PBI_BB_Initialise();
if ( (_DWORD)result )
result = (unsigned int)PBI_MIO_Initialise(a1, a2) != 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = PBI_BB_Initialise();
if ( (_DWORD)result )
result = (unsigned int)PBI_MIO_Initialise(a1, a2) != 0;
return result;
}

original_code:
int PBI_Initialise(int *argc, char *argv[])
{
 return 1
  && PBI_BB_Initialise(argc, argv)
  && PBI_MIO_Initialise(argc, argv)
 ;
}


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

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

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

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

original_code:
int PBI_ReadConfig(char *string, char *ptr)
{
 if (0) {
 }
 else if (PBI_MIO_ReadConfig(string, ptr)) {
 }
 else if (PBI_BB_ReadConfig(string, ptr)) {
 }
 else return 0;
 return 1;
}

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall PBI_D1GetByte(unsigned __int16 a1)
{
__int64 result; // rax

if ( PBI_MIO_enabled )
return PBI_MIO_D1GetByte(a1);
result = 0xFFFFFFFFLL;
if ( PBI_BB_enabled )
result = PBI_BB_D1GetByte(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1)
{
__int64 result; // rax

if ( PBI_MIO_enabled )
return PBI_MIO_D1GetByte(a1);
result = 0xFFFFFFFFLL;
if ( PBI_BB_enabled )
result = PBI_BB_D1GetByte(a1);
return result;
}

original_code:
unsigned char PBI_D1GetByte(unsigned short addr)
{
 int result = 0xff;
 if (PBI_MIO_enabled) return PBI_MIO_D1GetByte(addr);
 if (PBI_BB_enabled) return PBI_BB_D1GetByte(addr);
 if(result != -1) return (unsigned char)result;
 if (addr == 0xd1ff) {
  result = 0;
  return result;
 }
 do{}while(0);
 return result;
}

variable_types: result int


decompiled_code:
__int64 __fastcall PBI_D1PutByte(unsigned __int16 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rcx
_BYTE *v4; // rdi
char *v5; // r9

result = (__int64)&PBI_MIO_enabled;
if ( PBI_MIO_enabled )
return PBI_MIO_D1PutByte(a1, (unsigned __int8)a2);
if ( PBI_BB_enabled )
return PBI_BB_D1PutByte(a1, (unsigned __int8)a2);
if ( a1 == 0xD1FF && D1FF_LATCH != (_BYTE)a2 )
{
if ( (unsigned __int8)a2 <= 0x20u && (v3 = 0x100010117LL, _bittest64(&v3, a2))
|| (result = (unsigned int)(a2 - 64), (((_BYTE)a2 - 64) & 0xBF) == 0) )
{
D1FF_LATCH = a2;
if ( !fp_active_0 )
{
MEMORY_mem[6912] = MEMORY_os[768];
MEMORY_mem[7167] = MEMORY_os[1023];
v4 = (_BYTE *)((unsigned __int64)&MEMORY_mem[6913] & 0xFFFFFFFFFFFFFFF8LL);
v5 = (char *)((char *)&MEMORY_mem[6912] - v4);
result = (unsigned int)((_DWORD)v5 + 2048);
qmemcpy(v4, (const void *)((char *)&MEMORY_os[768] - v5), 8LL * ((unsigned int)((_DWORD)v5 + 2048) >> 3));
fp_active_0 = 1;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rcx
_BYTE *v4; // rdi
char *v5; // r9

result = (__int64)&PBI_MIO_enabled;
if ( PBI_MIO_enabled )
return PBI_MIO_D1PutByte(a1, (unsigned __int8)a2);
if ( PBI_BB_enabled )
return PBI_BB_D1PutByte(a1, (unsigned __int8)a2);
if ( a1 == 0xD1FF && D1FF_LATCH != (_BYTE)a2 )
{
if ( (unsigned __int8)a2 <= 0x20u && (v3 = 0x100010117LL, _bittest64(&v3, a2))
|| (result = (unsigned int)(a2 - 64), (((_BYTE)a2 - 64) & 0xBF) == 0) )
{
D1FF_LATCH = a2;
if ( !fp_active_0 )
{
MEMORY_mem[6912] = MEMORY_os[768];
MEMORY_mem[7167] = MEMORY_os[1023];
v4 = (_BYTE *)((unsigned __int64)&MEMORY_mem[6913] & 0xFFFFFFFFFFFFFFF8LL);
v5 = (char *)((char *)&MEMORY_mem[6912] - v4);
result = (unsigned int)((_DWORD)v5 + 2048);
qmemcpy(v4, (const void *)((char *)&MEMORY_os[768] - v5), 8LL * ((unsigned int)((_DWORD)v5 + 2048) >> 3));
fp_active_0 = 1;
}
}
}
return result;
}

original_code:
void PBI_D1PutByte(unsigned short addr, unsigned char byte)
{
 static int fp_active = 1;
 if (PBI_MIO_enabled) {
  PBI_MIO_D1PutByte(addr, byte);
  return;
 }
 if (PBI_BB_enabled) {
  PBI_BB_D1PutByte(addr, byte);
  return;
 }
 if (addr != 0xd1ff) {
  do{}while(0);
 }
 else if (addr == 0xd1ff) {
  do{}while(0);
  if (D1FF_LATCH != byte) {
   if (byte != 0 && byte != 1 && byte != 2 && byte != 4 && byte != 8 && byte != 0x10 && byte !=0x20 && byte != 0x40 && byte != 0x80){
    do{}while(0);
    return;
   }
   D1FF_LATCH = byte;
   if (!fp_active) {
    memcpy(MEMORY_mem + 0xd800, MEMORY_os + 0x1800, 0x800);
    do{}while(0);
    fp_active = 1;
   }
  }
 }
}


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

PBI_D1PutByte(0xD1FFu, 0LL);
result = &PBI_IRQ;
PBI_IRQ = 0;
return result;
}

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

func4(0xD1FFu, 0LL);
result = &PBI_IRQ;
PBI_IRQ = 0;
return result;
}

original_code:
void PBI_Reset(void)
{
 PBI_D1PutByte(0xd1ff, 0);
 PBI_IRQ = 0;
}

canonical_original_code:
void PBI_Reset()
{
 PBI_D1PutByte(0xd1ff, 0);
 PBI_IRQ = 0;
}


decompiled_code:
__int64 __fastcall PBI_D6GetByte(unsigned __int16 a1)
{
__int64 result; // rax

if ( PBI_MIO_enabled )
return PBI_MIO_D6GetByte(a1);
if ( PBI_BB_enabled )
return PBI_BB_D6GetByte(a1);
result = 0xFFFFFFFFLL;
if ( PBI_D6D7ram )
result = *((unsigned __int8 *)MEMORY_mem + a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int16 a1)
{
__int64 result; // rax

if ( PBI_MIO_enabled )
return PBI_MIO_D6GetByte(a1);
if ( PBI_BB_enabled )
return PBI_BB_D6GetByte(a1);
result = 0xFFFFFFFFLL;
if ( PBI_D6D7ram )
result = *((unsigned __int8 *)MEMORY_mem + a1);
return result;
}

original_code:
unsigned char PBI_D6GetByte(unsigned short addr)
{
 if (PBI_MIO_enabled) return PBI_MIO_D6GetByte(addr);
 if(PBI_BB_enabled) return PBI_BB_D6GetByte(addr);
 if (PBI_D6D7ram) return MEMORY_mem[addr];
 else return 0xff;
}


decompiled_code:
__int64 __fastcall PBI_D7GetByte(unsigned __int16 a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( PBI_D6D7ram )
result = *((unsigned __int8 *)MEMORY_mem + a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( PBI_D6D7ram )
result = *((unsigned __int8 *)MEMORY_mem + a1);
return result;
}

original_code:
unsigned char PBI_D7GetByte(unsigned short addr)
{
 do{}while(0);
 if (PBI_D6D7ram) return MEMORY_mem[addr];
 else return 0xff;
}


decompiled_code:
__int64 PBI_StateSave()
{
StateSav_SaveUBYTE(&D1FF_LATCH, 1LL);
StateSav_SaveINT(&PBI_D6D7ram, 1LL);
return StateSav_SaveINT(&PBI_IRQ, 1LL);
}

canonical_decompiled_code:
__int64 func8()
{
StateSav_SaveUBYTE(&D1FF_LATCH, 1LL);
StateSav_SaveINT(&PBI_D6D7ram, 1LL);
return StateSav_SaveINT(&PBI_IRQ, 1LL);
}

original_code:
void PBI_StateSave(void)
{
 StateSav_SaveUBYTE(&D1FF_LATCH, 1);
 StateSav_SaveINT(&PBI_D6D7ram, 1);
 StateSav_SaveINT(&PBI_IRQ, 1);
}

canonical_original_code:
void PBI_StateSave()
{
 StateSav_SaveUBYTE(&D1FF_LATCH, 1);
 StateSav_SaveINT(&PBI_D6D7ram, 1);
 StateSav_SaveINT(&PBI_IRQ, 1);
}


decompiled_code:
__int64 PBI_StateRead()
{
StateSav_ReadUBYTE(&D1FF_LATCH, 1LL);
StateSav_ReadINT(&PBI_D6D7ram, 1LL);
return StateSav_ReadINT(&PBI_IRQ, 1LL);
}

canonical_decompiled_code:
__int64 func9()
{
StateSav_ReadUBYTE(&D1FF_LATCH, 1LL);
StateSav_ReadINT(&PBI_D6D7ram, 1LL);
return StateSav_ReadINT(&PBI_IRQ, 1LL);
}

original_code:
void PBI_StateRead(void)
{
 StateSav_ReadUBYTE(&D1FF_LATCH, 1);
 StateSav_ReadINT(&PBI_D6D7ram, 1);
 StateSav_ReadINT(&PBI_IRQ, 1);
}

canonical_original_code:
void PBI_StateRead()
{
 StateSav_ReadUBYTE(&D1FF_LATCH, 1);
 StateSav_ReadINT(&PBI_D6D7ram, 1);
 StateSav_ReadINT(&PBI_IRQ, 1);
}


decompiled_code:
__int64 __fastcall AFILE_OpenFile(const char *a1, int a2, unsigned int a3, unsigned int a4)
{
unsigned int v6; // ebx

v6 = AFILE_DetectFileType(a1);
switch ( v6 )
{
case 1u:
case 2u:
case 3u:
case 4u:
case 5u:
case 0xFu:
case 0x10u:
if ( (unsigned int)SIO_Mount(a3, a1, a4) )
{
if ( a2 )
goto LABEL_4;
}
else
{
v6 = 0;
}
break;
case 6u:
case 7u:
case 8u:
if ( !(unsigned int)BINLOAD_Loader(a1) )
v6 = 0;
break;
case 9u:
case 0xAu:
if ( (unsigned int)CARTRIDGE_Insert(a1) )
{
v6 = 0;
}
else if ( a2 )
{
LABEL_4:
Atari800_Coldstart();
}
break;
case 0xBu:
case 0xCu:
if ( (unsigned int)CASSETTE_Insert(a1) )
{
if ( a2 )
{
CASSETTE_hold_start = 1;
Atari800_Coldstart();
}
}
else
{
v6 = 0;
}
break;
case 0xDu:
case 0xEu:
if ( (unsigned int)StateSav_ReadAtariState(a1, "rb") )
{
GTIA_consol_table[2] = 15;
GTIA_consol_table[1] = 15;
}
else
{
v6 = 0;
}
break;
default:
return v6;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, int a2, unsigned int a3, unsigned int a4)
{
unsigned int v6; // ebx

v6 = func0(a1);
switch ( v6 )
{
case 1u:
case 2u:
case 3u:
case 4u:
case 5u:
case 0xFu:
case 0x10u:
if ( (unsigned int)SIO_Mount(a3, a1, a4) )
{
if ( a2 )
goto LABEL_4;
}
else
{
v6 = 0;
}
break;
case 6u:
case 7u:
case 8u:
if ( !(unsigned int)BINLOAD_Loader(a1) )
v6 = 0;
break;
case 9u:
case 0xAu:
if ( (unsigned int)CARTRIDGE_Insert(a1) )
{
v6 = 0;
}
else if ( a2 )
{
LABEL_4:
Atari800_Coldstart();
}
break;
case 0xBu:
case 0xCu:
if ( (unsigned int)CASSETTE_Insert(a1) )
{
if ( a2 )
{
CASSETTE_hold_start = 1;
Atari800_Coldstart();
}
}
else
{
v6 = 0;
}
break;
case 0xDu:
case 0xEu:
if ( (unsigned int)StateSav_ReadAtariState(a1, "rb") )
{
GTIA_consol_table[2] = 15;
GTIA_consol_table[1] = 15;
}
else
{
v6 = 0;
}
break;
default:
return v6;
}
return v6;
}

original_code:
int AFILE_OpenFile(const char *filename, int reboot, int diskno, int readonly)
{
 int type = AFILE_DetectFileType(filename);
 switch (type) {
 case 1:
 case 16:
 case 2:
 case 3:
 case 4:
 case 5:
 case 15:
  if (!SIO_Mount(diskno, filename, readonly))
   return 0;
  if (reboot)
   Atari800_Coldstart();
  break;
 case 6:
 case 7:
 case 8:
  if (!BINLOAD_Loader(filename))
   return 0;
  break;
 case 9:
 case 10:
  if (CARTRIDGE_Insert(filename) != 0)
   return 0;
  if (reboot)
   Atari800_Coldstart();
  break;
 case 11:
 case 12:
  if (!CASSETTE_Insert(filename))
   return 0;
  if (reboot) {
   CASSETTE_hold_start = 1;
   Atari800_Coldstart();
  }
  break;
 case 13:
 case 14:
  if (!StateSav_ReadAtariState(filename, "rb"))
   return 0;
  GTIA_consol_table[1] = GTIA_consol_table[2] = 0xf;
  break;
 default:
  break;
 }
 return type;
}

canonical_original_code:
int AFILE_OpenFile(char *filename, int reboot, int diskno, int readonly)
{
 int type = AFILE_DetectFileType(filename);
 switch (type) {
 case 1:
 case 16:
 case 2:
 case 3:
 case 4:
 case 5:
 case 15:
  if (!SIO_Mount(diskno, filename, readonly))
   return 0;
  if (reboot)
   Atari800_Coldstart();
  break;
 case 6:
 case 7:
 case 8:
  if (!BINLOAD_Loader(filename))
   return 0;
  break;
 case 9:
 case 10:
  if (CARTRIDGE_Insert(filename) != 0)
   return 0;
  if (reboot)
   Atari800_Coldstart();
  break;
 case 11:
 case 12:
  if (!CASSETTE_Insert(filename))
   return 0;
  if (reboot) {
   CASSETTE_hold_start = 1;
   Atari800_Coldstart();
  }
  break;
 case 13:
 case 14:
  if (!StateSav_ReadAtariState(filename, "rb"))
   return 0;
  GTIA_consol_table[1] = GTIA_consol_table[2] = 0xf;
  break;
 default:
  break;
 }
 return type;
}

variable_types:
filename
char *

variable_types: reboot int

variable_types: diskno int

variable_types: readonly int

variable_types: type int


CopyExternalWithoutAdjustments

decompiled_code:
__int64 CopyExternalWithoutAdjustments()
{
unsigned __int8 *v0; // r12
int v1; // er14
int v2; // ebp
__int64 result; // rax

v0 = (unsigned __int8 *)(Colours_external + 4104LL);
v1 = 0;
do
{
Colours_SetRGB(v1, *v0, v0[1], v0[2], (__int64)&Colours_table);
v2 = v1 + 1;
Colours_SetRGB(v1 + 1, v0[3], v0[4], v0[5], (__int64)&Colours_table);
Colours_SetRGB(v1 + 2, v0[6], v0[7], v0[8], (__int64)&Colours_table);
Colours_SetRGB(v1 + 3, v0[9], v0[10], v0[11], (__int64)&Colours_table);
Colours_SetRGB(v1 + 4, v0[12], v0[13], v0[14], (__int64)&Colours_table);
Colours_SetRGB(v1 + 5, v0[15], v0[16], v0[17], (__int64)&Colours_table);
Colours_SetRGB(v1 + 6, v0[18], v0[19], v0[20], (__int64)&Colours_table);
result = Colours_SetRGB(v1 + 7, v0[21], v0[22], v0[23], (__int64)&Colours_table);
v1 += 8;
v0 += 24;
}
while ( v2 != 249 );
return result;
}

original_code:
static void CopyExternalWithoutAdjustments(void)
{
 int i;
 unsigned char *ext_ptr;
 for (i = 0, ext_ptr = Colours_external->palette; i < 256; i ++, ext_ptr += 3)
  Colours_SetRGB(i, *ext_ptr, *(ext_ptr + 1), *(ext_ptr + 2), Colours_table);
}

canonical_original_code:
static void CopyExternalWithoutAdjustments()
{
 int i;
 unsigned char *ext_ptr;
 for (i = 0, ext_ptr = Colours_external->palette; i < 256; i ++, ext_ptr += 3)
  Colours_SetRGB(i, *ext_ptr, *(ext_ptr + 1), *(ext_ptr + 2), Colours_table);
}

variable_types: i int


UpdatePalette

decompiled_code:
__int64 UpdatePalette()
{
if ( *(_DWORD *)(Colours_external + 4096LL) && !*(_DWORD *)(Colours_external + 4100LL) )
return CopyExternalWithoutAdjustments();
if ( Atari800_tv_mode == 262 )
return COLOURS_NTSC_Update(&Colours_table);
return COLOURS_PAL_Update(&Colours_table);
}

canonical_decompiled_code:
__int64 func2()
{
if ( *(_DWORD *)(Colours_external + 4096LL) && !*(_DWORD *)(Colours_external + 4100LL) )
return func1();
if ( Atari800_tv_mode == 262 )
return COLOURS_NTSC_Update(&Colours_table);
return COLOURS_PAL_Update(&Colours_table);
}

original_code:
static void UpdatePalette(void)
{
 if (Colours_external->loaded && !Colours_external->adjust)
  CopyExternalWithoutAdjustments();
 else if (Atari800_tv_mode == 262)
  COLOURS_NTSC_Update(Colours_table);
 else
  COLOURS_PAL_Update(Colours_table);
}

canonical_original_code:
static void UpdatePalette()
{
 if (Colours_external->loaded && !Colours_external->adjust)
  CopyExternalWithoutAdjustments();
 else if (Atari800_tv_mode == 262)
  COLOURS_NTSC_Update(Colours_table);
 else
  COLOURS_PAL_Update(Colours_table);
}


decompiled_code:
__int64 Colours_Update()
{
return UpdatePalette();
}

canonical_decompiled_code:
__int64 func3()
{
return func2();
}

original_code:
void Colours_Update(void)
{
 UpdatePalette();
}

canonical_original_code:
void Colours_Update()
{
 UpdatePalette();
}


decompiled_code:
__int64 __fastcall Colours_SetVideoSystem(int a1)
{
UpdateModeDependentPointers(a1);
return Colours_Update();
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
UpdateModeDependentPointers(a1);
return func3();
}

original_code:
void Colours_SetVideoSystem(int mode)
{
 UpdateModeDependentPointers(mode);
 Colours_Update();
}

variable_types: mode int


decompiled_code:
_QWORD *Colours_RestoreDefaults()
{
_QWORD *result; // rax
__int64 v1; // rcx

result = &Colours_setup;
v1 = Colours_setup;
*(_QWORD *)Colours_setup = 0LL;
*(_QWORD *)(v1 + 8) = 0LL;
*(_QWORD *)(v1 + 16) = 0LL;
*(_QWORD *)(v1 + 24) = LC2;
*(_DWORD *)(v1 + 32) = 16;
*(_DWORD *)(v1 + 36) = 235;
if ( Atari800_tv_mode == 262 )
result = (_QWORD *)COLOURS_NTSC_RestoreDefaults();
return result;
}

canonical_decompiled_code:
_QWORD *func5()
{
_QWORD *result; // rax
__int64 v1; // rcx

result = &Colours_setup;
v1 = Colours_setup;
*(_QWORD *)Colours_setup = 0LL;
*(_QWORD *)(v1 + 8) = 0LL;
*(_QWORD *)(v1 + 16) = 0LL;
*(_QWORD *)(v1 + 24) = LC2;
*(_DWORD *)(v1 + 32) = 16;
*(_DWORD *)(v1 + 36) = 235;
if ( Atari800_tv_mode == 262 )
result = (_QWORD *)COLOURS_NTSC_RestoreDefaults();
return result;
}

original_code:
void Colours_RestoreDefaults(void)
{
 *Colours_setup = presets[COLOURS_PRESET_STANDARD];
 if (Atari800_tv_mode == 262)
  COLOURS_NTSC_RestoreDefaults();
}

canonical_original_code:
void Colours_RestoreDefaults()
{
 *Colours_setup = presets[COLOURS_PRESET_STANDARD];
 if (Atari800_tv_mode == 262)
  COLOURS_NTSC_RestoreDefaults();
}


decompiled_code:
__int64 Colours_GetPreset()
{
double v0; // xmm1_8
unsigned int v1; // esi

if ( Atari800_tv_mode != 262 || (v1 = 3, !(unsigned int)COLOURS_NTSC_GetPreset()) )
{
*(_QWORD *)&v0 = *(_QWORD *)Colours_setup & LC3;
if ( v0 <= 0.001 )
{
if ( COERCE_DOUBLE(*(_QWORD *)(Colours_setup + 8LL) & LC3) <= 0.001
&& COERCE_DOUBLE(*(_QWORD *)(Colours_setup + 16LL) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.3) & LC3) <= 0.001
&& *(_DWORD *)(Colours_setup + 32LL) == 16
&& *(_DWORD *)(Colours_setup + 36LL) == 235 )
{
return 0;
}
if ( v0 <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.2) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.5) & LC3) <= 0.001
&& *(_DWORD *)(Colours_setup + 32LL) == 16
&& *(_DWORD *)(Colours_setup + 36LL) == 235 )
{
return 1;
}
}
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)Colours_setup - 0.26) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.72) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.16) & LC3) <= 0.001 )
{
v1 = 3;
if ( *(_DWORD *)(Colours_setup + 32LL) == 16 && *(_DWORD *)(Colours_setup + 36LL) == 235 )
v1 = 2;
}
}
}
}
}
return v1;
}

canonical_decompiled_code:
__int64 func6()
{
double v0; // xmm1_8
unsigned int v1; // esi

if ( Atari800_tv_mode != 262 || (v1 = 3, !(unsigned int)COLOURS_NTSC_GetPreset()) )
{
*(_QWORD *)&v0 = *(_QWORD *)Colours_setup & LC3;
if ( v0 <= 0.001 )
{
if ( COERCE_DOUBLE(*(_QWORD *)(Colours_setup + 8LL) & LC3) <= 0.001
&& COERCE_DOUBLE(*(_QWORD *)(Colours_setup + 16LL) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.3) & LC3) <= 0.001
&& *(_DWORD *)(Colours_setup + 32LL) == 16
&& *(_DWORD *)(Colours_setup + 36LL) == 235 )
{
return 0;
}
if ( v0 <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.2) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) <= 0.001
&& COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.5) & LC3) <= 0.001
&& *(_DWORD *)(Colours_setup + 32LL) == 16
&& *(_DWORD *)(Colours_setup + 36LL) == 235 )
{
return 1;
}
}
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)Colours_setup - 0.26) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.72) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) <= 0.001 )
{
v1 = 3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.16) & LC3) <= 0.001 )
{
v1 = 3;
if ( *(_DWORD *)(Colours_setup + 32LL) == 16 && *(_DWORD *)(Colours_setup + 36LL) == 235 )
v1 = 2;
}
}
}
}
}
return v1;
}

original_code:
Colours_preset_t Colours_GetPreset(void)
{
 int i;
 if (Atari800_tv_mode == 262 &&
     COLOURS_NTSC_GetPreset() != COLOURS_PRESET_STANDARD)
  return COLOURS_PRESET_CUSTOM;
 for (i = 0; i < COLOURS_PRESET_SIZE; i ++) {
  if ((fabs((Colours_setup->saturation)-(presets[i].saturation)) <= (0.001)) &&
      (fabs((Colours_setup->contrast)-(presets[i].contrast)) <= (0.001)) &&
      (fabs((Colours_setup->brightness)-(presets[i].brightness)) <= (0.001)) &&
      (fabs((Colours_setup->gamma)-(presets[i].gamma)) <= (0.001)) &&
      Colours_setup->black_level == presets[i].black_level &&
      Colours_setup->white_level == presets[i].white_level)
   return i;
 }
 return COLOURS_PRESET_CUSTOM;
}

canonical_original_code:
enum Colours_preset_t Colours_GetPreset()
{
 int i;
 if (Atari800_tv_mode == 262 &&
     COLOURS_NTSC_GetPreset() != COLOURS_PRESET_STANDARD)
  return COLOURS_PRESET_CUSTOM;
 for (i = 0; i < COLOURS_PRESET_SIZE; i ++) {
  if ((fabs((Colours_setup->saturation)-(presets[i].saturation)) <= (0.001)) &&
      (fabs((Colours_setup->contrast)-(presets[i].contrast)) <= (0.001)) &&
      (fabs((Colours_setup->brightness)-(presets[i].brightness)) <= (0.001)) &&
      (fabs((Colours_setup->gamma)-(presets[i].gamma)) <= (0.001)) &&
      Colours_setup->black_level == presets[i].black_level &&
      Colours_setup->white_level == presets[i].white_level)
   return i;
 }
 return COLOURS_PRESET_CUSTOM;
}

return_type: enum Colours_preset_t { COLOURS_PRESET_STANDARD=0, COLOURS_PRESET_DEEPBLACK=1, COLOURS_PRESET_VIBRANT=2, COLOURS_PRESET_CUSTOM=3, COLOURS_PRESET_SIZE=expr }


decompiled_code:
__int64 Colours_PreInitialise()
{
COLOURS_PAL_setup = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 1) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 2) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 3) = LC2;
*((_DWORD *)&COLOURS_PAL_setup + 8) = 16;
*((_DWORD *)&COLOURS_PAL_setup + 9) = 235;
COLOURS_NTSC_setup = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup);
*((__m128i *)&COLOURS_NTSC_setup + 1) = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup + 1);
*((_QWORD *)&COLOURS_NTSC_setup + 4) = *((_QWORD *)&COLOURS_PAL_setup + 4);
return COLOURS_NTSC_PreInitialise();
}

canonical_decompiled_code:
__int64 func8()
{
COLOURS_PAL_setup = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 1) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 2) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 3) = LC2;
*((_DWORD *)&COLOURS_PAL_setup + 8) = 16;
*((_DWORD *)&COLOURS_PAL_setup + 9) = 235;
COLOURS_NTSC_setup = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup);
*((__m128i *)&COLOURS_NTSC_setup + 1) = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup + 1);
*((_QWORD *)&COLOURS_NTSC_setup + 4) = *((_QWORD *)&COLOURS_PAL_setup + 4);
return COLOURS_NTSC_PreInitialise();
}

original_code:
void Colours_PreInitialise(void)
{
 COLOURS_NTSC_setup = COLOURS_PAL_setup = presets[COLOURS_PRESET_STANDARD];
 COLOURS_NTSC_PreInitialise();
}

canonical_original_code:
void Colours_PreInitialise()
{
 COLOURS_NTSC_setup = COLOURS_PAL_setup = presets[COLOURS_PRESET_STANDARD];
 COLOURS_NTSC_PreInitialise();
}