decompiled_code:
void __fastcall PBI_MIO_D1PutByte(__int16 a1, int a2)
{
__int16 v2; // di
char v3; // dl
int v4; // ebx
int v5; // ebp
int v6; // eax
int v7; // er10
const __m128i *v8; // rax
__m128i *v9; // rdx
const __m128i *v10; // r9
__int64 v11; // rsi
__m128i *v12; // rbx
char *v13; // rsi

v2 = a1 & 0xFFE3;
v3 = a2;
v4 = mio_ram_bank_offset;
v5 = mio_ram_enabled;
switch ( v2 )
{
case -11808:
v6 = ((unsigned __int8)a2 << 8) | mio_ram_bank_offset & 0xF0000;
mio_ram_bank_offset = v6;
if ( !mio_ram_enabled )
return;
goto LABEL_12;
case -11807:
if ( !mio_scsi_enabled )
return;
PBI_SCSI_PutByte((unsigned __int8)~(_BYTE)a2);
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
v6 = mio_ram_bank_offset;
v7 = mio_ram_enabled;
goto LABEL_8;
case -11806:
v7 = a2 & 0x20;
mio_ram_enabled = v7;
v6 = (unsigned __int16)mio_ram_bank_offset | (a2 << 16) & 0xF0000;
mio_ram_bank_offset = v6;
if ( mio_scsi_enabled )
{
PBI_SCSI_PutSEL((a2 & 0x10) != 0);
v6 = mio_ram_bank_offset;
v7 = mio_ram_enabled;
}
LABEL_8:
if ( !v7 )
{
if ( v5 )
{
v12 = (__m128i *)((char *)mio_ram + v4);
*v12 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v12[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v12[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v12[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v12[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v12[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v12[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v12[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v12[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v12[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v12[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v12[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v12[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v12[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v12[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v12[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((_OWORD *)&MEMORY_mem + 3424) = -1LL;
*((_OWORD *)&MEMORY_mem + 3425) = -1LL;
*((_OWORD *)&MEMORY_mem + 3426) = -1LL;
*((_OWORD *)&MEMORY_mem + 3427) = -1LL;
*((_OWORD *)&MEMORY_mem + 3428) = -1LL;
*((_OWORD *)&MEMORY_mem + 3429) = -1LL;
*((_OWORD *)&MEMORY_mem + 3430) = -1LL;
*((_OWORD *)&MEMORY_mem + 3431) = -1LL;
*((_OWORD *)&MEMORY_mem + 3432) = -1LL;
*((_OWORD *)&MEMORY_mem + 3433) = -1LL;
*((_OWORD *)&MEMORY_mem + 3434) = -1LL;
*((_OWORD *)&MEMORY_mem + 3435) = -1LL;
*((_OWORD *)&MEMORY_mem + 3436) = -1LL;
*((_OWORD *)&MEMORY_mem + 3437) = -1LL;
*((_OWORD *)&MEMORY_mem + 3438) = -1LL;
*((_OWORD *)&MEMORY_mem + 3439) = -1LL;
}
return;
}
if ( v5 != v7 )
{
v8 = (const __m128i *)((char *)mio_ram + v6);
*((__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);
*((__m128i *)&MEMORY_mem + 3439) = _mm_loadu_si128(v8 + 15);
return;
}
LABEL_12:
if ( v4 != v6 )
{
v9 = (__m128i *)((char *)mio_ram + v4);
*v9 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v9[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v9[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v9[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v9[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v9[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v9[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v9[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v9[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v9[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v9[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v9[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v9[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v9[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v9[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v10 = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
v9[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(v10);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(v10 + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(v10 + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(v10 + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(v10 + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(v10 + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(v10 + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(v10 + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(v10 + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(v10 + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(v10 + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(v10 + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(v10 + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(v10 + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(v10 + 14);
*((__m128i *)&MEMORY_mem + 3439) = _mm_loadu_si128(v10 + 15);
}
return;
}
if ( v2 == -11805 && mio_rom_bank != (_BYTE)a2 )
{
switch ( (_BYTE)a2 )
{
case 4:
v11 = 0x2000LL;
break;
case 8:
v11 = 10240LL;
break;
case 0x10:
v11 = 12288LL;
break;
default:
v11 = 14336LL;
if ( v3 != 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_20:
mio_rom_bank = v3;
return;
}
break;
}
v13 = (char *)mio_rom + v11;
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v13;
*((_QWORD *)&MEMORY_mem + 7167) = *((_QWORD *)v13 + 255);
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v13
- ((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_20;
}
}

canonical_decompiled_code:
void __fastcall func5(__int16 a1, int a2)
{
__int16 v2; // di
char v3; // dl
int v4; // ebx
int v5; // ebp
int v6; // eax
int v7; // er10
const __m128i *v8; // rax
__m128i *v9; // rdx
const __m128i *v10; // r9
__int64 v11; // rsi
__m128i *v12; // rbx
char *v13; // rsi

v2 = a1 & 0xFFE3;
v3 = a2;
v4 = mio_ram_bank_offset;
v5 = mio_ram_enabled;
switch ( v2 )
{
case -11808:
v6 = ((unsigned __int8)a2 << 8) | mio_ram_bank_offset & 0xF0000;
mio_ram_bank_offset = v6;
if ( !mio_ram_enabled )
return;
goto LABEL_12;
case -11807:
if ( !mio_scsi_enabled )
return;
PBI_SCSI_PutByte((unsigned __int8)~(_BYTE)a2);
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
v6 = mio_ram_bank_offset;
v7 = mio_ram_enabled;
goto LABEL_8;
case -11806:
v7 = a2 & 0x20;
mio_ram_enabled = v7;
v6 = (unsigned __int16)mio_ram_bank_offset | (a2 << 16) & 0xF0000;
mio_ram_bank_offset = v6;
if ( mio_scsi_enabled )
{
PBI_SCSI_PutSEL((a2 & 0x10) != 0);
v6 = mio_ram_bank_offset;
v7 = mio_ram_enabled;
}
LABEL_8:
if ( !v7 )
{
if ( v5 )
{
v12 = (__m128i *)((char *)mio_ram + v4);
*v12 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v12[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v12[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v12[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v12[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v12[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v12[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v12[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v12[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v12[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v12[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v12[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v12[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v12[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v12[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v12[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((_OWORD *)&MEMORY_mem + 3424) = -1LL;
*((_OWORD *)&MEMORY_mem + 3425) = -1LL;
*((_OWORD *)&MEMORY_mem + 3426) = -1LL;
*((_OWORD *)&MEMORY_mem + 3427) = -1LL;
*((_OWORD *)&MEMORY_mem + 3428) = -1LL;
*((_OWORD *)&MEMORY_mem + 3429) = -1LL;
*((_OWORD *)&MEMORY_mem + 3430) = -1LL;
*((_OWORD *)&MEMORY_mem + 3431) = -1LL;
*((_OWORD *)&MEMORY_mem + 3432) = -1LL;
*((_OWORD *)&MEMORY_mem + 3433) = -1LL;
*((_OWORD *)&MEMORY_mem + 3434) = -1LL;
*((_OWORD *)&MEMORY_mem + 3435) = -1LL;
*((_OWORD *)&MEMORY_mem + 3436) = -1LL;
*((_OWORD *)&MEMORY_mem + 3437) = -1LL;
*((_OWORD *)&MEMORY_mem + 3438) = -1LL;
*((_OWORD *)&MEMORY_mem + 3439) = -1LL;
}
return;
}
if ( v5 != v7 )
{
v8 = (const __m128i *)((char *)mio_ram + v6);
*((__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);
*((__m128i *)&MEMORY_mem + 3439) = _mm_loadu_si128(v8 + 15);
return;
}
LABEL_12:
if ( v4 != v6 )
{
v9 = (__m128i *)((char *)mio_ram + v4);
*v9 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
v9[1] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
v9[2] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
v9[3] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
v9[4] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
v9[5] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
v9[6] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
v9[7] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
v9[8] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
v9[9] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
v9[10] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
v9[11] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
v9[12] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
v9[13] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
v9[14] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v10 = (const __m128i *)((char *)mio_ram + mio_ram_bank_offset);
v9[15] = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(v10);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(v10 + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(v10 + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(v10 + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(v10 + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(v10 + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(v10 + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(v10 + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(v10 + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(v10 + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(v10 + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(v10 + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(v10 + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(v10 + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(v10 + 14);
*((__m128i *)&MEMORY_mem + 3439) = _mm_loadu_si128(v10 + 15);
}
return;
}
if ( v2 == -11805 && mio_rom_bank != (_BYTE)a2 )
{
switch ( (_BYTE)a2 )
{
case 4:
v11 = 0x2000LL;
break;
case 8:
v11 = 10240LL;
break;
case 0x10:
v11 = 12288LL;
break;
default:
v11 = 14336LL;
if ( v3 != 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_20:
mio_rom_bank = v3;
return;
}
break;
}
v13 = (char *)mio_rom + v11;
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v13;
*((_QWORD *)&MEMORY_mem + 7167) = *((_QWORD *)v13 + 255);
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v13
- ((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_20;
}
}

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];
}

variable_types:
addr
unsigned short

return_type: unsigned char


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

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

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

result = (unsigned int)mio_ram_enabled;
if ( mio_ram_enabled )
*((_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;
}

return_type: void


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

StateSav_SaveINT(&PBI_MIO_enabled, 1LL);
result = PBI_MIO_enabled;
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

StateSav_SaveINT(&PBI_MIO_enabled, 1LL);
result = PBI_MIO_enabled;
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

StateSav_ReadINT(&PBI_MIO_enabled, 1LL);
result = PBI_MIO_enabled;
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

StateSav_ReadINT(&PBI_MIO_enabled, 1LL);
result = PBI_MIO_enabled;
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)
 ;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

return_type: int


decompiled_code:
_BOOL8 __fastcall PBI_ReadConfig(__int64 a1, __int64 a2)
{
int v2; // er8
_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; // er8
_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:
string
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall PBI_D1GetByte(unsigned __int16 a1)
{
if ( PBI_MIO_enabled )
return PBI_MIO_D1GetByte(a1);
if ( PBI_BB_enabled )
return PBI_BB_D1GetByte(a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1)
{
if ( PBI_MIO_enabled )
return PBI_MIO_D1GetByte(a1);
if ( PBI_BB_enabled )
return PBI_BB_D1GetByte(a1);
return 0xFFFFFFFFLL;
}

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
_BYTE *v3; // rdi
char *v4; // r11
__int64 v5; // rdi

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

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

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 && (v5 = 0x100010117LL, _bittest64(&v5, a2))
|| (result = (unsigned int)(a2 - 64), (((_BYTE)a2 - 64) & 0xBF) == 0) )
{
D1FF_LATCH = a2;
if ( !fp_active_0 )
{
v3 = (_BYTE *)((unsigned __int64)&MEMORY_mem[6913] & 0xFFFFFFFFFFFFFFF8LL);
v4 = (char *)((char *)&MEMORY_mem[6912] - v3);
MEMORY_mem[6912] = MEMORY_os[768];
result = (unsigned int)((_DWORD)v4 + 2048);
MEMORY_mem[7167] = MEMORY_os[1023];
qmemcpy(v3, (const void *)((char *)&MEMORY_os[768] - v4), 8LL * ((unsigned int)((_DWORD)v4 + 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; // er12
__int64 result; // rax

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) )
goto LABEL_3;
goto LABEL_6;
case 6u:
case 7u:
case 8u:
if ( !(unsigned int)BINLOAD_Loader(a1) )
goto LABEL_3;
goto LABEL_4;
case 9u:
case 0xAu:
if ( (unsigned int)CARTRIDGE_Insert(a1) )
goto LABEL_3;
LABEL_6:
if ( a2 )
goto LABEL_7;
goto LABEL_4;
case 0xBu:
case 0xCu:
if ( !(unsigned int)CASSETTE_Insert(a1) )
goto LABEL_3;
if ( !a2 )
goto LABEL_4;
CASSETTE_hold_start = 1;
LABEL_7:
Atari800_Coldstart();
result = v6;
break;
case 0xDu:
case 0xEu:
if ( (unsigned int)StateSav_ReadAtariState(a1, "rb") )
{
result = v6;
*(_WORD *)((char *)&GTIA_consol_table + 1) = 3855;
}
else
{
LABEL_3:
v6 = 0;
LABEL_4:
result = v6;
}
break;
default:
goto LABEL_4;
}
return result;
}

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

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) )
goto LABEL_3;
goto LABEL_6;
case 6u:
case 7u:
case 8u:
if ( !(unsigned int)BINLOAD_Loader(a1) )
goto LABEL_3;
goto LABEL_4;
case 9u:
case 0xAu:
if ( (unsigned int)CARTRIDGE_Insert(a1) )
goto LABEL_3;
LABEL_6:
if ( a2 )
goto LABEL_7;
goto LABEL_4;
case 0xBu:
case 0xCu:
if ( !(unsigned int)CASSETTE_Insert(a1) )
goto LABEL_3;
if ( !a2 )
goto LABEL_4;
CASSETTE_hold_start = 1;
LABEL_7:
Atari800_Coldstart();
result = v6;
break;
case 0xDu:
case 0xEu:
if ( (unsigned int)StateSav_ReadAtariState(a1, "rb") )
{
result = v6;
*(_WORD *)((char *)&GTIA_consol_table + 1) = 3855;
}
else
{
LABEL_3:
v6 = 0;
LABEL_4:
result = v6;
}
break;
default:
goto LABEL_4;
}
return result;
}

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()
{
int v0; // ebp
unsigned __int8 *v1; // rbx
int v2; // ecx
int v3; // edx
int v4; // esi
int v5; // edi
__int64 result; // rax

v0 = 0;
v1 = (unsigned __int8 *)(Colours_external + 4104LL);
do
{
v2 = v1[2];
v3 = v1[1];
v4 = *v1;
v1 += 24;
Colours_SetRGB(v0, v4, v3, v2, (__int64)&Colours_table);
Colours_SetRGB(v0 + 1, *(v1 - 21), *(v1 - 20), *(v1 - 19), (__int64)&Colours_table);
Colours_SetRGB(v0 + 2, *(v1 - 18), *(v1 - 17), *(v1 - 16), (__int64)&Colours_table);
Colours_SetRGB(v0 + 3, *(v1 - 15), *(v1 - 14), *(v1 - 13), (__int64)&Colours_table);
Colours_SetRGB(v0 + 4, *(v1 - 12), *(v1 - 11), *(v1 - 10), (__int64)&Colours_table);
Colours_SetRGB(v0 + 5, *(v1 - 9), *(v1 - 8), *(v1 - 7), (__int64)&Colours_table);
Colours_SetRGB(v0 + 6, *(v1 - 6), *(v1 - 5), *(v1 - 4), (__int64)&Colours_table);
v5 = v0 + 7;
v0 += 8;
result = Colours_SetRGB(v5, *(v1 - 3), *(v1 - 2), *(v1 - 1), (__int64)&Colours_table);
}
while ( v0 != 256 );
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:
// attributes: thunk
__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
bool v2; // zf

result = &Colours_setup;
v1 = Colours_setup;
v2 = Atari800_tv_mode == 262;
*(_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 ( v2 )
result = (_QWORD *)COLOURS_NTSC_RestoreDefaults();
return result;
}

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

result = &Colours_setup;
v1 = Colours_setup;
v2 = Atari800_tv_mode == 262;
*(_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 ( v2 )
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()
{
if ( Atari800_tv_mode == 262 && (unsigned int)COLOURS_NTSC_GetPreset() )
return 3LL;
if ( COERCE_DOUBLE(*(_QWORD *)Colours_setup & LC3) <= 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 0LL;
}
if ( 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 1LL;
}
}
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)Colours_setup - 0.26) & LC3) > 0.001
|| COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.72) & LC3) > 0.001
|| COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) > 0.001
|| COERCE_DOUBLE(LC3 & COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.16)) > 0.001
|| *(_DWORD *)(Colours_setup + 32LL) != 16
|| *(_DWORD *)(Colours_setup + 36LL) != 235 )
{
return 3LL;
}
return 2LL;
}

canonical_decompiled_code:
__int64 func6()
{
if ( Atari800_tv_mode == 262 && (unsigned int)COLOURS_NTSC_GetPreset() )
return 3LL;
if ( COERCE_DOUBLE(*(_QWORD *)Colours_setup & LC3) <= 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 0LL;
}
if ( 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 1LL;
}
}
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)Colours_setup - 0.26) & LC3) > 0.001
|| COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 8LL) - 0.72) & LC3) > 0.001
|| COERCE_DOUBLE(COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 16LL) + 0.16) & LC3) > 0.001
|| COERCE_DOUBLE(LC3 & COERCE_UNSIGNED_INT64(*(double *)(Colours_setup + 24LL) - 0.16)) > 0.001
|| *(_DWORD *)(Colours_setup + 32LL) != 16
|| *(_DWORD *)(Colours_setup + 36LL) != 235 )
{
return 3LL;
}
return 2LL;
}

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()
{
__m128i v0; // xmm0
__m128i v1; // xmm1
__int64 v2; // rsi

COLOURS_PAL_setup = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 1) = 0LL;
v0 = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup);
*((_QWORD *)&COLOURS_PAL_setup + 2) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 3) = LC2;
v1 = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup + 1);
*((_DWORD *)&COLOURS_PAL_setup + 8) = 16;
*((_DWORD *)&COLOURS_PAL_setup + 9) = 235;
v2 = *((_QWORD *)&COLOURS_PAL_setup + 4);
COLOURS_NTSC_setup = v0;
*((_QWORD *)&COLOURS_NTSC_setup + 4) = v2;
*((__m128i *)&COLOURS_NTSC_setup + 1) = v1;
return COLOURS_NTSC_PreInitialise();
}

canonical_decompiled_code:
__int64 func8()
{
__m128i v0; // xmm0
__m128i v1; // xmm1
__int64 v2; // rsi

COLOURS_PAL_setup = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 1) = 0LL;
v0 = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup);
*((_QWORD *)&COLOURS_PAL_setup + 2) = 0LL;
*((_QWORD *)&COLOURS_PAL_setup + 3) = LC2;
v1 = _mm_loadu_si128((const __m128i *)&COLOURS_PAL_setup + 1);
*((_DWORD *)&COLOURS_PAL_setup + 8) = 16;
*((_DWORD *)&COLOURS_PAL_setup + 9) = 235;
v2 = *((_QWORD *)&COLOURS_PAL_setup + 4);
COLOURS_NTSC_setup = v0;
*((_QWORD *)&COLOURS_NTSC_setup + 4) = v2;
*((__m128i *)&COLOURS_NTSC_setup + 1) = v1;
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();
}


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

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

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

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

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

variable_types:
option
char *


decompiled_code:
__int64 __fastcall Colours_Initialise(int *a1, __int64 a2)
{
int v4; // er14
int v5; // ebx
int v6; // edx
const char **v7; // r10
const char *v8; // r8
double v9; // xmm0_8
unsigned int v10; // er15
double v12; // xmm0_8
double v13; // xmm0_8
bool v14; // al
bool v15; // cf
bool v16; // zf
int v17; // eax
const __m128i *v18; // r8
__int64 v19; // r14
__m128i v20; // xmm1
__m128i v21; // xmm0
const char *v22; // rdi
__int64 v23; // rcx
const char *v24; // rsi
int v25; // [rsp+0h] [rbp-48h]
_BOOL4 v26; // [rsp+4h] [rbp-44h]

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

canonical_decompiled_code:
__int64 __fastcall func11(int *a1, __int64 a2)
{
int v4; // er14
int v5; // ebx
int v6; // edx
const char **v7; // r10
const char *v8; // r8
double v9; // xmm0_8
unsigned int v10; // er15
double v12; // xmm0_8
double v13; // xmm0_8
bool v14; // al
bool v15; // cf
bool v16; // zf
int v17; // eax
const __m128i *v18; // r8
__int64 v19; // r14
__m128i v20; // xmm1
__m128i v21; // xmm0
const char *v22; // rdi
__int64 v23; // rcx
const char *v24; // rsi
int v25; // [rsp+0h] [rbp-48h]
_BOOL4 v26; // [rsp+4h] [rbp-44h]

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

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

variable_types: j int

variable_types: idx int


GetGZErrorText

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

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

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

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

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

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

variable_types:
error
char *


decompiled_code:
void __fastcall StateSav_SaveUBYTE(__int64 a1, unsigned int a2)
{
if ( StateFile && !nFileError && !(unsigned int)gzwrite(StateFile, a1, a2) )
GetGZErrorText();
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, unsigned int a2)
{
if ( StateFile && !nFileError && !(unsigned int)gzwrite(StateFile, a1, a2) )
func0();
}

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

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

variable_types:
data
unsigned char *

variable_types: num int


decompiled_code:
void __fastcall StateSav_ReadUBYTE(__int64 a1, unsigned int a2)
{
if ( StateFile && !nFileError && !(unsigned int)gzread(StateFile, a1, a2) )
GetGZErrorText();
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2)
{
if ( StateFile && !nFileError && !(unsigned int)gzread(StateFile, a1, a2) )
func0();
}

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


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

v3 = StateFile;
if ( StateFile && !nFileError && a2 > 0 )
{
v4 = &a1[a2];
if ( (a2 & 1) != 0 )
goto LABEL_9;
v5 = *a1++;
for ( v7[0] = v5; (unsigned int)gzwrite(StateFile, v7, 1LL); v7[0] = v5 )
{
v7[0] = HIBYTE(v5);
if ( !(unsigned int)gzwrite(StateFile, v7, 1LL) )
break;
v3 = StateFile;
LABEL_9:
v6 = *a1;
v7[0] = *a1;
if ( !(unsigned int)gzwrite(v3, v7, 1LL) )
break;
v7[0] = HIBYTE(v6);
if ( !(unsigned int)gzwrite(StateFile, v7, 1LL) )
break;
if ( a1 + 1 == v4 )
return;
v5 = a1[1];
a1 += 2;
}
GetGZErrorText();
}
}

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

v3 = StateFile;
if ( StateFile && !nFileError && a2 > 0 )
{
v4 = &a1[a2];
if ( (a2 & 1) != 0 )
goto LABEL_9;
v5 = *a1++;
for ( v7[0] = v5; (unsigned int)gzwrite(StateFile, v7, 1LL); v7[0] = v5 )
{
v7[0] = HIBYTE(v5);
if ( !(unsigned int)gzwrite(StateFile, v7, 1LL) )
break;
v3 = StateFile;
LABEL_9:
v6 = *a1;
v7[0] = *a1;
if ( !(unsigned int)gzwrite(v3, v7, 1LL) )
break;
v7[0] = HIBYTE(v6);
if ( !(unsigned int)gzwrite(StateFile, v7, 1LL) )
break;
if ( a1 + 1 == v4 )
return;
v5 = a1[1];
a1 += 2;
}
func0();
}
}

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

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

variable_types:
data
unsigned short *

variable_types:
temp
unsigned short

variable_types:
byte
unsigned char


decompiled_code:
void __fastcall StateSav_ReadUWORD(_WORD *a1, int a2)
{
__int64 v3; // rdi
_WORD *v4; // r13
__int16 v5[21]; // [rsp+Eh] [rbp-2Ah] BYREF

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

canonical_decompiled_code:
void __fastcall func4(_WORD *a1, int a2)
{
__int64 v3; // rdi
_WORD *v4; // r13
__int16 v5[21]; // [rsp+Eh] [rbp-2Ah] BYREF

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

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

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


decompiled_code:
void __fastcall StateSav_SaveINT(int *a1, int a2)
{
__int64 v3; // rdi
int *v4; // r13
int v5; // ebx
char v6; // r14
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

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

canonical_decompiled_code:
void __fastcall func5(int *a1, int a2)
{
__int64 v3; // rdi
int *v4; // r13
int v5; // ebx
char v6; // r14
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

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

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

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

variable_types:
data
int *

variable_types:
signbit
unsigned char

variable_types:
temp
unsigned int


decompiled_code:
void __fastcall StateSav_ReadINT(__int64 a1, int a2)
{
__int64 v3; // rdi
__int64 v4; // r13
int v5; // edi
unsigned __int8 v6; // [rsp+Ch] [rbp-2Ch] BYREF
unsigned __int8 v7; // [rsp+Dh] [rbp-2Bh] BYREF
unsigned __int8 v8; // [rsp+Eh] [rbp-2Ah] BYREF
char v9[41]; // [rsp+Fh] [rbp-29h] BYREF

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

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2)
{
__int64 v3; // rdi
__int64 v4; // r13
int v5; // edi
unsigned __int8 v6; // [rsp+Ch] [rbp-2Ch] BYREF
unsigned __int8 v7; // [rsp+Dh] [rbp-2Bh] BYREF
unsigned __int8 v8; // [rsp+Eh] [rbp-2Ah] BYREF
char v9[41]; // [rsp+Fh] [rbp-29h] BYREF

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

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

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

variable_types: temp int


decompiled_code:
void __fastcall StateSav_SaveFNAME(char *s1)
{
char *v1; // rbp
size_t v2; // rbx
unsigned __int16 v3; // [rsp+0h] [rbp-101Ah] BYREF
char buf[16]; // [rsp+2h] [rbp-1018h] BYREF
char v5[4080]; // [rsp+12h] [rbp-1008h] BYREF

v1 = s1;
*(_OWORD *)buf = 0LL;
memset(v5, 0, sizeof(v5));
if ( getcwd(buf, 0x1000uLL) )
{
v2 = strlen(buf);
if ( !strncmp(s1, buf, v2) )
v1 = &s1[v2 + 1];
}
v3 = strlen(v1);
StateSav_SaveUWORD((__int16 *)&v3, 1);
StateSav_SaveUBYTE((__int64)v1, v3);
}

canonical_decompiled_code:
void __fastcall func7(char *s1)
{
char *v1; // rbp
size_t v2; // rbx
unsigned __int16 v3; // [rsp+0h] [rbp-101Ah] BYREF
char buf[16]; // [rsp+2h] [rbp-1018h] BYREF
char v5[4080]; // [rsp+12h] [rbp-1008h] BYREF

v1 = s1;
*(_OWORD *)buf = 0LL;
memset(v5, 0, sizeof(v5));
if ( getcwd(buf, 0x1000uLL) )
{
v2 = strlen(buf);
if ( !strncmp(s1, buf, v2) )
v1 = &s1[v2 + 1];
}
v3 = strlen(v1);
func3((__int16 *)&v3, 1);
func1((__int64)v1, v3);
}

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

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

variable_types:
filename
char *

variable_types:
namelen
unsigned short

variable_types:
dirname
char [4096]


decompiled_code:
_BOOL8 __fastcall StateSav_SaveAtariState(const char *a1, __int64 a2, unsigned __int8 a3)
{
_BOOL4 v3; // ebp
int v4; // eax
_BOOL8 result; // rax
char v6[15]; // [rsp+Ch] [rbp-2Ch] BYREF
char v7; // [rsp+1Bh] [rbp-1Dh] BYREF
int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

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

canonical_decompiled_code:
_BOOL8 __fastcall func9(const char *a1, __int64 a2, unsigned __int8 a3)
{
_BOOL4 v3; // ebp
int v4; // eax
_BOOL8 result; // rax
char v6[15]; // [rsp+Ch] [rbp-2Ch] BYREF
char v7; // [rsp+1Bh] [rbp-1Dh] BYREF
int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

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

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

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

variable_types:
mode
char *

variable_types:
SaveVerbose
unsigned char

variable_types:
StateVersion
unsigned char


decompiled_code:
__int64 __fastcall StateSav_ReadAtariState(const char *a1, __int64 a2)
{
const char *v2; // rdi
unsigned int v3; // er12
__int64 result; // rax
unsigned __int8 v5; // [rsp+Eh] [rbp-2Ah] BYREF
unsigned __int8 v6; // [rsp+Fh] [rbp-29h] BYREF
int v7; // [rsp+10h] [rbp-28h] BYREF
int v8; // [rsp+14h] [rbp-24h] BYREF
__int64 v9[4]; // [rsp+18h] [rbp-20h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, __int64 a2)
{
const char *v2; // rdi
unsigned int v3; // er12
__int64 result; // rax
unsigned __int8 v5; // [rsp+Eh] [rbp-2Ah] BYREF
unsigned __int8 v6; // [rsp+Fh] [rbp-29h] BYREF
int v7; // [rsp+10h] [rbp-28h] BYREF
int v8; // [rsp+14h] [rbp-24h] BYREF
__int64 v9[4]; // [rsp+18h] [rbp-20h] BYREF

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

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

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


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

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

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

variable_types:
argc
int *

variable_types:
argv
char *[]


decompiled_code:
void Sound_Continue()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void Sound_Continue(void)
{
}

canonical_original_code:
void Sound_Continue()
{
}


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

result = (unsigned int)sound_enabled;
if ( sound_enabled )
sound_enabled = 0;
return result;
}

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

result = (unsigned int)sound_enabled;
if ( sound_enabled )
sound_enabled = 0;
return result;
}

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

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


decompiled_code:
void Sound_Update()
{
int v0; // ebp
unsigned int v1; // ebp
__int64 v2; // r11
__int64 v3; // rdi
_BYTE *v4; // rdx
int v5; // er10
int v6; // er9
char v7; // al
unsigned __int8 v8; // r8
unsigned __int8 v9; // al
unsigned __int8 v10; // r8
unsigned __int8 v11; // al
unsigned __int8 v12; // r8
unsigned __int8 v13; // al
unsigned __int8 v14; // r8
__int64 v15; // rdi
unsigned __int8 v16; // al
unsigned __int8 v17; // al
unsigned __int8 v18; // si
unsigned __int8 v19; // r8
unsigned __int8 v20; // r8
unsigned __int8 v21; // si
unsigned __int8 v22; // al
char *v23; // rdi
__int64 v24; // rdx
char *v25; // r8
__int64 v26; // r9
char v27; // si
char v28; // r10
char v29; // r11
char v30; // dl
char v31; // cl
char v32; // r9
char v33; // al
char v34; // si
char v35; // r10
char v36; // r11
char v37; // dl
char v38; // cl
char v39; // r9
char v40; // al
signed int v41; // er9
int v42; // edi
char *v43; // r10
int v44; // eax
int v45; // er8
char v46; // dl
__int64 v47; // rsi
char v48; // r8
int v49; // edx
__int64 v50; // rdi
char v51; // cl
int v52; // edi
__int64 v53; // r8
char v54; // si
char v55; // si
__int64 v56; // rcx
int v57; // edx
char v58; // si
__int64 v59; // rdi
int v60; // er8
char v61; // si

if ( sound_enabled && !Atari800_turbo )
{
v0 = 367;
if ( Atari800_tv_mode != 262 )
v0 = 441;
v1 = pokey_chips * v0;
POKEYSND_Process(&buffer_0, v1);
if ( output_channels == 2 )
{
if ( pokey_chips == 1 )
{
v41 = v1 - 1;
if ( (int)(v1 - 1) >= 0 )
{
v42 = 2 * v41;
v43 = (char *)&buffer_0 + v41;
v44 = 2 * v41 + 1;
v45 = v1 & 3;
if ( (v1 & 3) == 0 )
goto LABEL_86;
if ( v45 != 1 )
{
if ( v45 != 2 )
{
v46 = *v43;
v47 = v44;
v41 = v1 - 2;
--v43;
v44 = v42 - 1;
*((_BYTE *)&buffer_0 + v47) = v46;
*((_BYTE *)&buffer_0 + v42) = v46;
}
v48 = *v43;
v49 = 2 * v41;
v50 = v44;
--v41;
--v43;
v44 -= 2;
*((_BYTE *)&buffer_0 + v50) = v48;
*((_BYTE *)&buffer_0 + v49) = v48;
}
v51 = *v43;
v52 = 2 * v41;
v53 = v44;
--v41;
--v43;
v44 -= 2;
*((_BYTE *)&buffer_0 + v53) = v51;
*((_BYTE *)&buffer_0 + v52) = v51;
if ( v41 != -1 )
{
LABEL_86:
do
{
v54 = *v43;
v43 -= 4;
*((_BYTE *)&buffer_0 + v44) = v54;
*((_BYTE *)&buffer_0 + 2 * v41) = v54;
v55 = v43[3];
*((_BYTE *)&buffer_0 + v44 - 2) = v55;
v56 = v44 - 4;
v57 = v44 - 6;
v44 -= 8;
*((_BYTE *)&buffer_0 + 2 * v41 - 2) = v55;
v58 = v43[2];
v59 = 2 * v41 - 4;
v60 = 2 * v41 - 6;
v41 -= 4;
*((_BYTE *)&buffer_0 + v56) = v58;
*((_BYTE *)&buffer_0 + v59) = v58;
v61 = v43[1];
*((_BYTE *)&buffer_0 + v57) = v61;
*((_BYTE *)&buffer_0 + v60) = v61;
}
while ( v41 != -1 );
}
}
}
else if ( !POKEYSND_stereo_enabled && v1 )
{
v23 = (char *)&buffer_0;
v24 = (v1 - 1) >> 1;
v25 = (char *)&buffer_0 + 2 * v24 + 2;
v26 = ((unsigned __int8)((unsigned __int64)(2 * v24) >> 1) + 1) & 7;
if ( (((unsigned __int8)((unsigned __int64)(2 * v24) >> 1) + 1) & 7) == 0 )
goto LABEL_87;
if ( v26 != 1 )
{
if ( v26 != 2 )
{
if ( v26 != 3 )
{
if ( v26 != 4 )
{
if ( v26 != 5 )
{
if ( v26 != 6 )
{
v23 = (char *)&buffer_0 + 2;
byte_581 = buffer_0;
}
v27 = *v23;
v23 += 2;
*(v23 - 1) = v27;
}
v28 = *v23;
v23 += 2;
*(v23 - 1) = v28;
}
v29 = *v23;
v23 += 2;
*(v23 - 1) = v29;
}
v30 = *v23;
v23 += 2;
*(v23 - 1) = v30;
}
v31 = *v23;
v23 += 2;
*(v23 - 1) = v31;
}
v32 = *v23;
v23 += 2;
*(v23 - 1) = v32;
if ( v23 != v25 )
{
LABEL_87:
do
{
v33 = *v23;
v34 = v23[2];
v23 += 16;
v35 = *(v23 - 12);
v36 = *(v23 - 10);
v37 = *(v23 - 8);
v38 = *(v23 - 6);
*(v23 - 15) = v33;
v39 = *(v23 - 4);
v40 = *(v23 - 2);
*(v23 - 13) = v34;
*(v23 - 11) = v35;
*(v23 - 9) = v36;
*(v23 - 7) = v37;
*(v23 - 5) = v38;
*(v23 - 3) = v39;
*(v23 - 1) = v40;
}
while ( v23 != v25 );
}
}
}
else if ( pokey_chips == 2 && v1 >> 1 )
{
v2 = v1 >> 1;
v3 = 0LL;
v4 = (char *)&buffer_0 + 1;
v5 = POKEYSND_stereo_enabled;
v6 = (v1 >> 1) & 7;
if ( !v6 )
goto LABEL_88;
if ( v6 != 1LL )
{
if ( v6 != 2LL )
{
if ( v6 != 3LL )
{
if ( v6 != 4LL )
{
if ( v6 != 5LL )
{
if ( v6 != 6LL )
{
v7 = buffer_0;
if ( POKEYSND_stereo_enabled )
v7 = (*v4 >> 1) + (buffer_0 >> 1);
buffer_0 = v7;
v3 = 1LL;
}
v8 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( POKEYSND_stereo_enabled )
v8 = (v4[2 * v3] >> 1) + (v8 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v8;
}
v9 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v9 = (v4[2 * v3] >> 1) + (v9 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v9;
}
v10 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v10 = (v4[2 * v3] >> 1) + (v10 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v10;
}
v11 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v11 = (v4[2 * v3] >> 1) + (v11 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v11;
}
v12 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v12 = (v4[2 * v3] >> 1) + (v12 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v12;
}
v13 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v13 = (v4[2 * v3] >> 1) + (v13 >> 1);
*((_BYTE *)&buffer_0 + v3++) = v13;
if ( v3 != v2 )
{
LABEL_88:
do
{
v14 = *((_BYTE *)&buffer_0 + 2 * v3);
if ( v5 )
v14 = (v4[2 * v3] >> 1) + (v14 >> 1);
*((_BYTE *)&buffer_0 + v3) = v14;
v15 = v3 + 1;
v16 = *((_BYTE *)&buffer_0 + 2 * v15);
if ( v5 )
v16 = (v4[2 * v15] >> 1) + (v16 >> 1);
*((_BYTE *)&buffer_0 + v15) = v16;
v17 = *((_BYTE *)&buffer_0 + 2 * v15 + 2);
if ( v5 )
v17 = (v4[2 * v15 + 2] >> 1) + (v17 >> 1);
*((_BYTE *)&buffer_0 + v15 + 1) = v17;
v18 = *((_BYTE *)&buffer_0 + 2 * v15 + 4);
if ( v5 )
v18 = (v4[2 * v15 + 4] >> 1) + (v18 >> 1);
*((_BYTE *)&buffer_0 + v15 + 2) = v18;
v19 = *((_BYTE *)&buffer_0 + 2 * v15 + 6);
if ( v5 )
v19 = (v4[2 * v15 + 6] >> 1) + (v19 >> 1);
*((_BYTE *)&buffer_0 + v15 + 3) = v19;
v20 = *((_BYTE *)&buffer_0 + 2 * v15 + 8);
if ( v5 )
v20 = (v4[2 * v15 + 8] >> 1) + (v20 >> 1);
*((_BYTE *)&buffer_0 + v15 + 4) = v20;
v21 = *((_BYTE *)&buffer_0 + 2 * v15 + 10);
if ( v5 )
v21 = (v4[2 * v15 + 10] >> 1) + (v21 >> 1);
*((_BYTE *)&buffer_0 + v15 + 5) = v21;
v22 = *((_BYTE *)&buffer_0 + 2 * v15 + 12);
if ( v5 )
v22 = (v4[2 * v15 + 12] >> 1) + (v22 >> 1);
*((_BYTE *)&buffer_0 + v15 + 6) = v22;
v3 = v15 + 7;
}
while ( v3 != v2 );
}
}
ATR800WriteSoundBuffer(&buffer_0, v1);
}
}

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

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

variable_types:
buffer
unsigned char [4096]

variable_types:
len
unsigned int

variable_types: i int

variable_types: pos int


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

PIA_PACTL = 63;
result = 1LL;
PIA_PBCTL = 63;
PIA_PORTA = -1;
PIA_PORTB = -1;
PIA_PORTA_mask = -1;
PIA_PORTB_mask = -1;
PIA_PORT_input = -1;
return result;
}

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

PIA_PACTL = 63;
result = 1LL;
PIA_PBCTL = 63;
PIA_PORTA = -1;
PIA_PORTB = -1;
PIA_PORTA_mask = -1;
PIA_PORTB_mask = -1;
PIA_PORT_input = -1;
return result;
}

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


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

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

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

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

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

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


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

v1 = a1 & 3;
switch ( v1 )
{
case 2:
return PIA_PACTL & 0x3F;
case 3:
return PIA_PBCTL & 0x3F;
case 1:
v2 = PIA_PORTB_mask;
if ( (PIA_PBCTL & 4) == 0 )
return ~v2;
result = PIA_PORTB | PIA_PORTB_mask;
if ( Atari800_machine_type != 2 )
result &= PIA_PORT_input[1];
break;
default:
v2 = PIA_PORTA_mask;
if ( (PIA_PACTL & 4) == 0 )
return ~v2;
result = PIA_PORT_input[0] & (PIA_PORTA | PIA_PORTA_mask);
break;
}
return result;
}

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

v1 = a1 & 3;
switch ( v1 )
{
case 2:
return PIA_PACTL & 0x3F;
case 3:
return PIA_PBCTL & 0x3F;
case 1:
v2 = PIA_PORTB_mask;
if ( (PIA_PBCTL & 4) == 0 )
return ~v2;
result = PIA_PORTB | PIA_PORTB_mask;
if ( Atari800_machine_type != 2 )
result &= PIA_PORT_input[1];
break;
default:
v2 = PIA_PORTA_mask;
if ( (PIA_PACTL & 4) == 0 )
return ~v2;
result = PIA_PORT_input[0] & (PIA_PORTA | PIA_PORTA_mask);
break;
}
return result;
}

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

variable_types:
addr
unsigned short


decompiled_code:
void __fastcall PIA_PutByte(char a1, unsigned __int8 a2)
{
__int16 v2; // di
char v4; // bl
unsigned __int8 v5; // cl
char v6; // cl
unsigned __int8 *v7; // rbp
__int64 v8; // rsi
char v9; // bl

v2 = a1 & 3;
switch ( v2 )
{
case 2:
SIO_TapeMotor(((a2 >> 3) ^ 1) & 1);
PIA_PACTL = a2;
return;
case 3:
v7 = (unsigned __int8 *)&PIA_PBCTL;
if ( ((a2 ^ PIA_PBCTL) & 8) != 0 )
SIO_SwitchCommandFrame(((a2 >> 3) ^ 1) & 1);
goto LABEL_12;
case 1:
v6 = PIA_PBCTL & 4;
if ( Atari800_machine_type != 2 )
{
if ( v6 )
PIA_PORTB = a2;
else
PIA_PORTB_mask = ~a2;
return;
}
v7 = (unsigned __int8 *)&PIA_PORTB;
v8 = (unsigned __int8)(PIA_PORTB_mask | PIA_PORTB);
if ( !v6 )
{
v9 = ~a2;
MEMORY_HandlePORTB((unsigned __int8)(v9 | PIA_PORTB), v8);
PIA_PORTB_mask = v9;
return;
}
MEMORY_HandlePORTB((unsigned __int8)(a2 | PIA_PORTB_mask), v8);
LABEL_12:
*v7 = a2;
return;
}
if ( (PIA_PACTL & 4) != 0 )
{
v5 = a2;
PIA_PORTA = a2;
v4 = PIA_PORTA_mask;
}
else
{
v4 = ~a2;
PIA_PORTA_mask = ~a2;
v5 = PIA_PORTA;
}
INPUT_SelectMultiJoy((unsigned __int8)(v4 | v5) >> 4);
}

canonical_decompiled_code:
void __fastcall func3(char a1, unsigned __int8 a2)
{
__int16 v2; // di
char v4; // bl
unsigned __int8 v5; // cl
char v6; // cl
unsigned __int8 *v7; // rbp
__int64 v8; // rsi
char v9; // bl

v2 = a1 & 3;
switch ( v2 )
{
case 2:
SIO_TapeMotor(((a2 >> 3) ^ 1) & 1);
PIA_PACTL = a2;
return;
case 3:
v7 = (unsigned __int8 *)&PIA_PBCTL;
if ( ((a2 ^ PIA_PBCTL) & 8) != 0 )
SIO_SwitchCommandFrame(((a2 >> 3) ^ 1) & 1);
goto LABEL_12;
case 1:
v6 = PIA_PBCTL & 4;
if ( Atari800_machine_type != 2 )
{
if ( v6 )
PIA_PORTB = a2;
else
PIA_PORTB_mask = ~a2;
return;
}
v7 = (unsigned __int8 *)&PIA_PORTB;
v8 = (unsigned __int8)(PIA_PORTB_mask | PIA_PORTB);
if ( !v6 )
{
v9 = ~a2;
MEMORY_HandlePORTB((unsigned __int8)(v9 | PIA_PORTB), v8);
PIA_PORTB_mask = v9;
return;
}
MEMORY_HandlePORTB((unsigned __int8)(a2 | PIA_PORTB_mask), v8);
LABEL_12:
*v7 = a2;
return;
}
if ( (PIA_PACTL & 4) != 0 )
{
v5 = a2;
PIA_PORTA = a2;
v4 = PIA_PORTA_mask;
}
else
{
v4 = ~a2;
PIA_PORTA_mask = ~a2;
v5 = PIA_PORTA;
}
INPUT_SelectMultiJoy((unsigned __int8)(v4 | v5) >> 4);
}

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

variable_types:
byte
unsigned char


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

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

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

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

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

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