decompiled_code:
__int64 mbc_huc1_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_huc1_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_huc1_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_huc1_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_huc1_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_huc1_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_huc1_write_mode_select;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_huc1_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_huc1_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_huc1_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_huc1_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
huc1_bank_low = 1;
huc1_bank_high = 0;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
huc1_ram_bank = 0;
huc1_mode_select = 0;
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_huc1_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_huc1_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_huc1_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_huc1_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_huc1_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_huc1_write_mode_select;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_huc1_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_huc1_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_huc1_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_huc1_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
huc1_bank_low = 1;
huc1_bank_high = 0;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
huc1_ram_bank = 0;
huc1_mode_select = 0;
cart[14] = result;
return result;
}

original_code:
void mbc_huc1_install()
{
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_huc1_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_huc1_read_bank_n;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_huc1_write_ram_enable;
 }
 for (i = 0x20; i <= 0x3F; ++i) {
  writemem[i] = mbc_huc1_write_rom_bank_select;
 }
 for (i = 0x40; i <= 0x5F; ++i) {
  writemem[i] = mbc_huc1_write_ram_bank_select;
 }
 for (i = 0x60; i <= 0x7F; ++i) {
  writemem[i] = mbc_huc1_write_mode_select;
 }
 int extram_end =
     0xA0 + (cart.extram_size > 8192 ? 8192 : cart.extram_size) / 256;
 for (i = 0xA0; i < extram_end; ++i) {
  readmem[i] = mbc_huc1_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_huc1_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_huc1_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_huc1_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
 huc1_bank_low = 0x01;
 huc1_bank_high = 0x00;
 huc1_ram_bank = 0x00;
 huc1_mode_select = 0x00;
 cart.extram_bank = cart.extram;
}


repo: jkbenaim/cboy

decompiled_code:
unsigned __int64 mbc_huc1_regs_changed()
{
unsigned __int64 v0; // rax
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx
unsigned __int64 v3; // rtt

v0 = (unsigned __int8)huc1_bank_low << 14;
if ( !huc1_bank_low )
v0 = 0x4000LL;
v3 = v0;
result = v0 / cart[10];
v2 = v3 % cart[10];
cart[14] = cart[11] + (((unsigned __int8)huc1_bank_high << 13) & 0x6000);
cart[7] = cart[4] + v2;
return result;
}

canonical_decompiled_code:
unsigned __int64 func8()
{
unsigned __int64 v0; // rax
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx
unsigned __int64 v3; // rtt

v0 = (unsigned __int8)huc1_bank_low << 14;
if ( !huc1_bank_low )
v0 = 0x4000LL;
v3 = v0;
result = v0 / cart[10];
v2 = v3 % cart[10];
cart[14] = cart[11] + (((unsigned __int8)huc1_bank_high << 13) & 0x6000);
cart[7] = cart[4] + v2;
return result;
}

original_code:
void mbc_huc1_regs_changed()
{
 int rambank;
 int rombank = huc1_bank_low;
 if (rombank == 0)
  rombank++;
 rambank = 0;
 rambank = huc1_bank_high & 0x03;
 size_t cartoffset;
 cartoffset = (rombank * 16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
 cart.extram_bank = cart.extram + rambank * 8192;
}

canonical_original_code:
void mbc_huc1_regs_changed()
{
 int rambank;
 int rombank = huc1_bank_low;
 if (rombank == 0)
  rombank++;
 rambank = 0;
 rambank = huc1_bank_high & 0x03;
 unsigned long cartoffset;
 cartoffset = (rombank * 16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
 cart.extram_bank = cart.extram + rambank * 8192;
}

variable_types: rambank int

variable_types: rombank int

variable_types:
cartoffset
unsigned long

return_type: void


decompiled_code:
unsigned __int64 __fastcall mbc_huc1_write_rom_bank_select(__int64 a1, char a2)
{
huc1_bank_low = a2;
return mbc_huc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__int64 a1, char a2)
{
huc1_bank_low = a2;
return func8();
}

original_code:
void mbc_huc1_write_rom_bank_select(uint16_t address, uint8_t data)
{
 huc1_bank_low = data;
 mbc_huc1_regs_changed();
}

canonical_original_code:
void mbc_huc1_write_rom_bank_select(unsigned short address, unsigned char data)
{
 huc1_bank_low = data;
 mbc_huc1_regs_changed();
}

variable_types:
address
unsigned short

variable_types:
data
unsigned char


decompiled_code:
unsigned __int64 __fastcall mbc_huc1_write_ram_bank_select(__int64 a1, char a2)
{
huc1_bank_high = a2;
return mbc_huc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(__int64 a1, char a2)
{
huc1_bank_high = a2;
return func8();
}

original_code:
void mbc_huc1_write_ram_bank_select(uint16_t address, uint8_t data)
{
 huc1_bank_high = data;
 mbc_huc1_regs_changed();
}

canonical_original_code:
void mbc_huc1_write_ram_bank_select(unsigned short address, unsigned char data)
{
 huc1_bank_high = data;
 mbc_huc1_regs_changed();
}


decompiled_code:
unsigned __int64 __fastcall mbc_huc1_write_mode_select(__int64 a1, char a2)
{
huc1_mode_select = a2;
return mbc_huc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(__int64 a1, char a2)
{
huc1_mode_select = a2;
return func8();
}

original_code:
void mbc_huc1_write_mode_select(uint16_t address, uint8_t data)
{
 huc1_mode_select = data;
 mbc_huc1_regs_changed();
}

canonical_original_code:
void mbc_huc1_write_mode_select(unsigned short address, unsigned char data)
{
 huc1_mode_select = data;
 mbc_huc1_regs_changed();
}


decompiled_code:
// attributes: thunk
__int64 mbc_huc1_read_extram_disabled()
{
return mbc_huc1_read_ff();
}

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

original_code:
uint8_t mbc_huc1_read_extram_disabled(uint16_t address)
{
 return 0xFF;
}

canonical_original_code:
unsigned char mbc_huc1_read_extram_disabled(unsigned short address)
{
 return 0xFF;
}

return_type: unsigned char


decompiled_code:
void mbc_huc1_write_extram_disabled()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void mbc_huc1_write_extram_disabled(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_huc1_write_extram_disabled(unsigned short address, unsigned char data)
{
}


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

if ( (unsigned __int16)(a1 - 512) <= 0x6FFu || a1 <= 0xFFu )
result = *(unsigned __int8 *)(cart[2] + a1);
else
result = *(unsigned __int8 *)(cart[4] + a1);
return result;
}

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

if ( (unsigned __int16)(a1 - 512) <= 0x6FFu || a1 <= 0xFFu )
result = *(unsigned __int8 *)(cart[2] + a1);
else
result = *(unsigned __int8 *)(cart[4] + a1);
return result;
}

original_code:
uint8_t mbc_boot_read_bank_0(uint16_t address)
{
 if ((address < 0x100) || ((address >= 0x200) && (address < 0x900))) {
  return cart.bootrom[address];
 } else {
  return cart.cartrom[address];
 }
}

canonical_original_code:
unsigned char mbc_boot_read_bank_0(unsigned short address)
{
 if ((address < 0x100) || ((address >= 0x200) && (address < 0x900))) {
  return cart.bootrom[address];
 } else {
  return cart.cartrom[address];
 }
}


decompiled_code:
void mbc_boot_write_bank_0()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void mbc_boot_write_bank_0(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_boot_write_bank_0(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_boot_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_boot_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

canonical_original_code:
unsigned char mbc_boot_read_bank_n(unsigned short address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}


decompiled_code:
void mbc_boot_write_bank_n()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void mbc_boot_write_bank_n(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_boot_write_bank_n(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 mbc_boot_install()
{
__int64 i; // rax
__int64 v1; // rax
__int64 result; // rax

for ( i = 0LL; i != 512; *(_QWORD *)((char *)&writemem + i - 8) = mbc_boot_write_bank_0 )
{
*(_QWORD *)((char *)&readmem + i) = mbc_boot_read_bank_0;
i += 8LL;
}
do
{
*(_QWORD *)((char *)&readmem + i) = mbc_boot_read_bank_n;
i += 8LL;
*(_QWORD *)((char *)&writemem + i - 8) = mbc_boot_write_bank_n;
}
while ( i != 1024 );
v1 = cart[4];
cart[6] = v1;
result = v1 + 0x4000;
cart[7] = result;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 i; // rax
__int64 v1; // rax
__int64 result; // rax

for ( i = 0LL; i != 512; *(_QWORD *)((char *)&writemem + i - 8) = mbc_boot_write_bank_0 )
{
*(_QWORD *)((char *)&readmem + i) = mbc_boot_read_bank_0;
i += 8LL;
}
do
{
*(_QWORD *)((char *)&readmem + i) = mbc_boot_read_bank_n;
i += 8LL;
*(_QWORD *)((char *)&writemem + i - 8) = mbc_boot_write_bank_n;
}
while ( i != 1024 );
v1 = cart[4];
cart[6] = v1;
result = v1 + 0x4000;
cart[7] = result;
return result;
}

original_code:
void mbc_boot_install()
{
 int i;
 for (i = 0x0; i <= (0x3F); ++i) {
  readmem[i] = mbc_boot_read_bank_0;
  writemem[i] = mbc_boot_write_bank_0;
 }
 for (i = 0x40; i <= (0x7F); ++i) {
  readmem[i] = mbc_boot_read_bank_n;
  writemem[i] = mbc_boot_write_bank_n;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
}

variable_types: i int


decompiled_code:
__int64 __fastcall mbc_mbc1_read_bank_0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

original_code:
uint8_t mbc_mbc1_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

canonical_original_code:
unsigned char mbc_mbc1_read_bank_0(unsigned short address)
{
 return cart.cartrom_bank_zero[address];
}


decompiled_code:
__int64 __fastcall mbc_mbc1_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_mbc1_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

canonical_original_code:
unsigned char mbc_mbc1_read_bank_n(unsigned short address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}


decompiled_code:
__int64 mbc_mbc1_read_ff()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func3()
{
return 0xFFFFFFFFLL;
}

original_code:
uint8_t mbc_mbc1_read_ff(uint16_t address)
{
 return 0xff;
}

canonical_original_code:
unsigned char mbc_mbc1_read_ff(unsigned short address)
{
 return 0xff;
}


decompiled_code:
void mbc_mbc1_write_dummy()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void mbc_mbc1_write_dummy(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc1_write_dummy(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_mbc1_read_extram(__int16 a1)
{
return *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1)
{
return *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}

original_code:
uint8_t mbc_mbc1_read_extram(uint16_t address)
{
 return cart.extram_bank[address & 0x1fff];
}

canonical_original_code:
unsigned char mbc_mbc1_read_extram(unsigned short address)
{
 return cart.extram_bank[address & 0x1fff];
}


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

result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

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

result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

original_code:
void mbc_mbc1_write_extram(uint16_t address, uint8_t data)
{
 cart.extram_bank[address & 0x1fff] = data;
}

canonical_original_code:
void mbc_mbc1_write_extram(unsigned short address, unsigned char data)
{
 cart.extram_bank[address & 0x1fff] = data;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall mbc_mbc1_read_extram_disabled()
{
return mbc_mbc1_read_ff();
}

canonical_decompiled_code:
__int64 __fastcall func7()
{
return func3();
}

original_code:
uint8_t mbc_mbc1_read_extram_disabled(uint16_t address)
{
 return 0xFF;
}

canonical_original_code:
unsigned char mbc_mbc1_read_extram_disabled(unsigned short address)
{
 return 0xFF;
}


decompiled_code:
void mbc_mbc1_write_extram_disabled()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code:
void mbc_mbc1_write_extram_disabled(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc1_write_extram_disabled(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 mbc_mbc1_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_mbc1_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc1_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_mbc1_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc1_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc1_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc1_write_mode_select;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc1_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_mbc1_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_mbc1_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_mbc1_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
mbc1_bank_low = 1;
mbc1_bank_high = 0;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
mbc1_ram_bank = 0;
mbc1_mode_select = 0;
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func9()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_mbc1_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc1_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_mbc1_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc1_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc1_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc1_write_mode_select;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc1_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_mbc1_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_mbc1_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_mbc1_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
mbc1_bank_low = 1;
mbc1_bank_high = 0;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
mbc1_ram_bank = 0;
mbc1_mode_select = 0;
cart[14] = result;
return result;
}

original_code:
void mbc_mbc1_install()
{
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_mbc1_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_mbc1_read_bank_n;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_mbc1_write_ram_enable;
 }
 for (i = 0x20; i <= 0x3F; ++i) {
  writemem[i] = mbc_mbc1_write_rom_bank_select;
 }
 for (i = 0x40; i <= 0x5F; ++i) {
  writemem[i] = mbc_mbc1_write_ram_bank_select;
 }
 for (i = 0x60; i <= 0x7F; ++i) {
  writemem[i] = mbc_mbc1_write_mode_select;
 }
 int extram_end =
     0xA0 + (cart.extram_size > 8192 ? 8192 : cart.extram_size) / 256;
 for (i = 0xA0; i < extram_end; ++i) {
  readmem[i] = mbc_mbc1_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_mbc1_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_mbc1_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_mbc1_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
 mbc1_bank_low = 0x01;
 mbc1_bank_high = 0x00;
 mbc1_ram_bank = 0x00;
 mbc1_mode_select = 0x00;
 cart.extram_bank = cart.extram;
}


decompiled_code:
unsigned __int64 mbc_mbc1_regs_changed()
{
int v0; // eax
__int64 v1; // rcx
unsigned __int64 result; // rax
unsigned __int64 v3; // rtt

v0 = (unsigned __int8)mbc1_bank_low;
if ( !mbc1_bank_low )
v0 = 1;
if ( mbc1_mode_select )
{
v1 = ((unsigned __int8)mbc1_bank_high << 13) & 0x6000;
}
else
{
v0 += (32 * mbc1_bank_high) & 0x60;
v1 = 0LL;
}
cart[14] = cart[11] + v1;
v3 = v0 << 14;
result = v3 / cart[10];
cart[7] = cart[4] + v3 % cart[10];
return result;
}

canonical_decompiled_code:
unsigned __int64 func10()
{
int v0; // eax
__int64 v1; // rcx
unsigned __int64 result; // rax
unsigned __int64 v3; // rtt

v0 = (unsigned __int8)mbc1_bank_low;
if ( !mbc1_bank_low )
v0 = 1;
if ( mbc1_mode_select )
{
v1 = ((unsigned __int8)mbc1_bank_high << 13) & 0x6000;
}
else
{
v0 += (32 * mbc1_bank_high) & 0x60;
v1 = 0LL;
}
cart[14] = cart[11] + v1;
v3 = v0 << 14;
result = v3 / cart[10];
cart[7] = cart[4] + v3 % cart[10];
return result;
}

original_code:
void mbc_mbc1_regs_changed()
{
 int rambank;
 int rombank = mbc1_bank_low;
 if (rombank == 0)
  rombank++;
 if (mbc1_mode_select == 0) {
  rombank += (mbc1_bank_high & 0x03) << 5;
  rambank = 0;
 } else {
  rambank = mbc1_bank_high & 0x03;
 }
 size_t cartoffset;
 cartoffset = (rombank * 16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
 cart.extram_bank = cart.extram + rambank * 8192;
}

canonical_original_code:
void mbc_mbc1_regs_changed()
{
 int rambank;
 int rombank = mbc1_bank_low;
 if (rombank == 0)
  rombank++;
 if (mbc1_mode_select == 0) {
  rombank += (mbc1_bank_high & 0x03) << 5;
  rambank = 0;
 } else {
  rambank = mbc1_bank_high & 0x03;
 }
 unsigned long cartoffset;
 cartoffset = (rombank * 16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
 cart.extram_bank = cart.extram + rambank * 8192;
}


decompiled_code:
unsigned __int64 __fastcall mbc_mbc1_write_rom_bank_select(__int64 a1, char a2)
{
mbc1_bank_low = a2;
return mbc_mbc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(__int64 a1, char a2)
{
mbc1_bank_low = a2;
return func10();
}

original_code:
void mbc_mbc1_write_rom_bank_select(uint16_t address, uint8_t data)
{
 mbc1_bank_low = data;
 mbc_mbc1_regs_changed();
}

canonical_original_code:
void mbc_mbc1_write_rom_bank_select(unsigned short address, unsigned char data)
{
 mbc1_bank_low = data;
 mbc_mbc1_regs_changed();
}


decompiled_code:
unsigned __int64 __fastcall mbc_mbc1_write_ram_bank_select(__int64 a1, char a2)
{
mbc1_bank_high = a2;
return mbc_mbc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func12(__int64 a1, char a2)
{
mbc1_bank_high = a2;
return func10();
}

original_code:
void mbc_mbc1_write_ram_bank_select(uint16_t address, uint8_t data)
{
 mbc1_bank_high = data;
 mbc_mbc1_regs_changed();
}

canonical_original_code:
void mbc_mbc1_write_ram_bank_select(unsigned short address, unsigned char data)
{
 mbc1_bank_high = data;
 mbc_mbc1_regs_changed();
}


decompiled_code:
unsigned __int64 __fastcall mbc_mbc1_write_mode_select(__int64 a1, char a2)
{
mbc1_mode_select = a2;
return mbc_mbc1_regs_changed();
}

canonical_decompiled_code:
unsigned __int64 __fastcall func13(__int64 a1, char a2)
{
mbc1_mode_select = a2;
return func10();
}

original_code:
void mbc_mbc1_write_mode_select(uint16_t address, uint8_t data)
{
 mbc1_mode_select = data;
 mbc_mbc1_regs_changed();
}

canonical_original_code:
void mbc_mbc1_write_mode_select(unsigned short address, unsigned char data)
{
 mbc1_mode_select = data;
 mbc_mbc1_regs_changed();
}


decompiled_code:
void mbc_cam_write_dummy()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void mbc_cam_write_dummy(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_cam_write_dummy(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_cam_read_bank_0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

original_code:
uint8_t mbc_cam_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

canonical_original_code:
unsigned char mbc_cam_read_bank_0(unsigned short address)
{
 return cart.cartrom_bank_zero[address];
}


decompiled_code:
__int64 __fastcall mbc_cam_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_cam_read_bank_n(uint16_t address) {
 return cart.cartrom_bank_n[address&0x3fff];
}

canonical_original_code:
unsigned char mbc_cam_read_bank_n(unsigned short address) {
 return cart.cartrom_bank_n[address&0x3fff];
}


decompiled_code:
unsigned __int64 __fastcall mbc_cam_write_rom_bank_select(__int64 a1, int a2)
{
int v2; // eax
unsigned __int64 result; // rax
unsigned __int64 v4; // rtt

v2 = 1;
if ( (_BYTE)a2 )
v2 = a2;
v4 = (v2 << 14) & 0x3FC000;
result = v4 / cart[10];
cart[7] = cart[4] + v4 % cart[10];
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1, int a2)
{
int v2; // eax
unsigned __int64 result; // rax
unsigned __int64 v4; // rtt

v2 = 1;
if ( (_BYTE)a2 )
v2 = a2;
v4 = (v2 << 14) & 0x3FC000;
result = v4 / cart[10];
cart[7] = cart[4] + v4 % cart[10];
return result;
}

original_code:
void mbc_cam_write_rom_bank_select(uint16_t address, uint8_t data) {
 size_t cartoffset;
 int rombank = (data==0)?1:data;
 cartoffset = (rombank*16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
}

canonical_original_code:
void mbc_cam_write_rom_bank_select(unsigned short address, unsigned char data) {
 unsigned long cartoffset;
 int rombank = (data==0)?1:data;
 cartoffset = (rombank*16384) % cart.cartromsize;
 cart.cartrom_bank_n = cart.cartrom + cartoffset;
}


decompiled_code:
void __fastcall mbc_cam_write_extram_bank_select(__int64 a1, int a2)
{
if ( (_BYTE)a2 == 16 )
{
cam_mode = 1;
}
else
{
cam_mode = 0;
cart[14] = cart[11] + ((a2 << 13) & 0x1E000);
}
}

original_code:
void mbc_cam_write_extram_bank_select(uint16_t address, uint8_t data) {
 if( data == 0x10 )
 {
  cam_mode = 1;
 }
 else
 {
  cam_mode = 0;
  int bank = data & 0x0F;
  cart.extram_bank = cart.extram + bank*8192;
 }
}

canonical_original_code:
void mbc_cam_write_extram_bank_select(unsigned short address, unsigned char data) {
 if( data == 0x10 )
 {
  cam_mode = 1;
 }
 else
 {
  cam_mode = 0;
  int bank = data & 0x0F;
  cart.extram_bank = cart.extram + bank*8192;
 }
}

variable_types: bank int


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

result = 0LL;
if ( !cam_mode )
result = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
return result;
}

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

result = 0LL;
if ( !cam_mode )
result = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
return result;
}

original_code:
uint8_t mbc_cam_read_extram(uint16_t address) {
 if( cam_mode == 0 )
 {
  return cart.extram_bank[address&0x1fff];
 }
 else
 {
  return 0;
 }
}

canonical_original_code:
unsigned char mbc_cam_read_extram(unsigned short address) {
 if( cam_mode == 0 )
 {
  return cart.extram_bank[address&0x1fff];
 }
 else
 {
  return 0;
 }
}


decompiled_code:
__int64 __fastcall mbc_cam_write_extram(__int16 a1, char a2)
{
__int64 result; // rax
int v3; // er11
int v4; // er10
int v5; // er9
int v6; // er8
int v7; // esi
int v8; // edi
__int64 v9; // rdx
int v10; // edx

result = (unsigned int)cam_mode;
if ( cam_mode )
{
if ( a2 == 3 && a1 == -24576 )
{
result = (unsigned int)pics_taken_0;
v3 = pics_taken_0 + 1;
pics_taken_0 = v3;
if ( v3 > 140 )
{
v4 = 528;
LOBYTE(v5) = 0;
LOBYTE(v6) = 0;
do
{
v7 = v4 - 256;
v8 = v3;
do
{
LODWORD(result) = v7 - 16;
v9 = (unsigned int)(v8 % 6);
if ( (unsigned int)v9 <= 5 )
{
v6 = CSWTCH_14[v9];
v5 = CSWTCH_15[v9];
}
do
{
*(_BYTE *)(cart[11] + (int)result) = v5;
v10 = result + 1;
result = (unsigned int)(result + 2);
*(_BYTE *)(cart[11] + v10) = v6;
}
while ( v7 != (_DWORD)result );
v7 += 16;
++v8;
}
while ( v7 != v4 );
v4 += 256;
++v3;
}
while ( v4 != 4112 );
}
}
}
else
{
result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, char a2)
{
__int64 result; // rax
int v3; // er11
int v4; // er10
int v5; // er9
int v6; // er8
int v7; // esi
int v8; // edi
__int64 v9; // rdx
int v10; // edx

result = (unsigned int)cam_mode;
if ( cam_mode )
{
if ( a2 == 3 && a1 == -24576 )
{
result = (unsigned int)pics_taken_0;
v3 = pics_taken_0 + 1;
pics_taken_0 = v3;
if ( v3 > 140 )
{
v4 = 528;
LOBYTE(v5) = 0;
LOBYTE(v6) = 0;
do
{
v7 = v4 - 256;
v8 = v3;
do
{
LODWORD(result) = v7 - 16;
v9 = (unsigned int)(v8 % 6);
if ( (unsigned int)v9 <= 5 )
{
v6 = CSWTCH_14[v9];
v5 = CSWTCH_15[v9];
}
do
{
*(_BYTE *)(cart[11] + (int)result) = v5;
v10 = result + 1;
result = (unsigned int)(result + 2);
*(_BYTE *)(cart[11] + v10) = v6;
}
while ( v7 != (_DWORD)result );
v7 += 16;
++v8;
}
while ( v7 != v4 );
v4 += 256;
++v3;
}
while ( v4 != 4112 );
}
}
}
else
{
result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
}
return result;
}

original_code:
void mbc_cam_write_extram(uint16_t address, uint8_t data) {
 if(cam_mode == 0) {
  cart.extram_bank[address&0x1fff] = data;
 } else {
 switch(address)
    {
      case 0xA000:
        switch( data )
        {
          case 0x03:
            {
            static int pics_taken = 0;
            pics_taken++;
            if(pics_taken <= 140)
            {
            } else {
              int tile_x, tile_y;
              int row_in_tile;
              int ram_address_offset = 0x0100;
              int value_to_write_hi=0, value_to_write_lo=0;
              for(tile_y = 0; tile_y < 14 ; tile_y++)
              for(tile_x = 0; tile_x < 16 ; tile_x++)
              {
                switch((tile_x + tile_y + pics_taken) % 6) {
                  case 0:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0x00;
                    break;
                  case 1:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0xff;
                    break;
                  case 2:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0x00;
                    break;
                  case 3:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0xff;
                    break;
                  case 4:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0x00;
                    break;
                  case 5:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0xff;
                    break;
                }
                for(row_in_tile = 0; row_in_tile < 8 ; row_in_tile++)
                {
                  cart.extram[ram_address_offset + 0] = (uint8_t)value_to_write_lo;
                  cart.extram[ram_address_offset + 1] = (uint8_t)value_to_write_hi;
                  ram_address_offset +=2;
                }
              }
            }
            }
            break;
          default:
            break;
        }
        break;
      default:
        break;
    }
  }
}

canonical_original_code:
void mbc_cam_write_extram(unsigned short address, unsigned char data) {
 if(cam_mode == 0) {
  cart.extram_bank[address&0x1fff] = data;
 } else {
 switch(address)
    {
      case 0xA000:
        switch( data )
        {
          case 0x03:
            {
            static int pics_taken = 0;
            pics_taken++;
            if(pics_taken <= 140)
            {
            } else {
              int tile_x; int tile_y;
              int row_in_tile;
              int ram_address_offset = 0x0100;
              int value_to_write_hi = 0; int value_to_write_lo = 0;
              for(tile_y = 0; tile_y < 14 ; tile_y++)
              for(tile_x = 0; tile_x < 16 ; tile_x++)
              {
                switch((tile_x + tile_y + pics_taken) % 6) {
                  case 0:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0x00;
                    break;
                  case 1:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0xff;
                    break;
                  case 2:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0x00;
                    break;
                  case 3:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0xff;
                    break;
                  case 4:
                    value_to_write_hi = 0xff;
                    value_to_write_lo = 0x00;
                    break;
                  case 5:
                    value_to_write_hi = 0x00;
                    value_to_write_lo = 0xff;
                    break;
                }
                for(row_in_tile = 0; row_in_tile < 8 ; row_in_tile++)
                {
                  cart.extram[ram_address_offset + 0] = (unsigned char)value_to_write_lo;
                  cart.extram[ram_address_offset + 1] = (unsigned char)value_to_write_hi;
                  ram_address_offset +=2;
                }
              }
            }
            }
            break;
          default:
            break;
        }
        break;
      default:
        break;
    }
  }
}


decompiled_code:
void mbc_cam_write_ram_enable()
{
;
}

original_code:
void mbc_cam_write_ram_enable(uint16_t address, uint8_t data) {
}

canonical_original_code:
void mbc_cam_write_ram_enable(unsigned short address, unsigned char data) {
}


decompiled_code:
__int64 mbc_cam_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
void (**v5)(); // rax
__int64 (__fastcall **v6)(__int16); // rax
unsigned __int64 v7; // rax
__int64 v8; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_cam_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_cam_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_cam_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_cam_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_cam_write_extram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v5++ = mbc_cam_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != v5 );
v6 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1280);
do
*v6++ = mbc_cam_read_extram;
while ( (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1536) != v6 );
v7 = (unsigned int)((_DWORD)&writemem + 1280);
do
{
*(_QWORD *)v7 = mbc_cam_write_extram;
v7 += 8LL;
}
while ( (_UNKNOWN *)v7 != (_UNKNOWN *)((char *)&writemem + 1536) );
v8 = cart[4];
LOBYTE(cart[17]) = 0;
cam_mode = 0;
cart[6] = v8;
cart[7] = v8 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func9()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
void (**v5)(); // rax
__int64 (__fastcall **v6)(__int16); // rax
unsigned __int64 v7; // rax
__int64 v8; // rax
__int64 result; // rax

v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_cam_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_cam_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_cam_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_cam_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_cam_write_extram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v5++ = mbc_cam_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != v5 );
v6 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1280);
do
*v6++ = mbc_cam_read_extram;
while ( (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1536) != v6 );
v7 = (unsigned int)((_DWORD)&writemem + 1280);
do
{
*(_QWORD *)v7 = mbc_cam_write_extram;
v7 += 8LL;
}
while ( (_UNKNOWN *)v7 != (_UNKNOWN *)((char *)&writemem + 1536) );
v8 = cart[4];
LOBYTE(cart[17]) = 0;
cam_mode = 0;
cart[6] = v8;
cart[7] = v8 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

original_code:
void mbc_cam_install()
{
 int i;
 for( i=0x00; i<=0x3F; ++i ) {
  readmem[i] = mbc_cam_read_bank_0;
 }
 for( i=0x40; i<=0x7F; ++i ) {
  readmem[i] = mbc_cam_read_bank_n;
 }
 for( i=0x00; i<=0x1F; ++i ) {
  writemem[i] = mbc_cam_write_ram_enable;
 }
 for( i=0x20; i<=0x3F; ++i ) {
  writemem[i] = mbc_cam_write_rom_bank_select;
 }
 for( i=0x40; i<=0x5F; ++i ) {
  writemem[i] = mbc_cam_write_extram_bank_select;
 }
 for( i=0x60; i<=0x7F; ++i ) {
  writemem[i] = mbc_cam_write_dummy;
 }
 for( i=0xA0; i<=0xBF; ++i ) {
  readmem[i] = mbc_cam_read_extram;
 }
 for( i=0xA0; i<=0xBF; ++i ) {
  writemem[i] = mbc_cam_write_extram;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
 cart.extram_bank_num = 0x00;
 cart.extram_bank = cart.extram;
 cam_mode = 0;
}


decompiled_code:
void mbc_cam_write_mode_select()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
void mbc_cam_write_mode_select(uint16_t address, uint8_t data) {
}

canonical_original_code:
void mbc_cam_write_mode_select(unsigned short address, unsigned char data) {
}


decompiled_code:
__int64 mbc_mbc2_read_ff()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0xFFFFFFFFLL;
}

original_code:
uint8_t mbc_mbc2_read_ff(uint16_t address)
{
 return 0xff;
}

canonical_original_code:
unsigned char mbc_mbc2_read_ff(unsigned short address)
{
 return 0xff;
}


decompiled_code:
void mbc_mbc2_write_dummy()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void mbc_mbc2_write_dummy(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc2_write_dummy(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_mbc2_read_bank_0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

original_code:
uint8_t mbc_mbc2_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

canonical_original_code:
unsigned char mbc_mbc2_read_bank_0(unsigned short address)
{
 return cart.cartrom_bank_zero[address];
}


decompiled_code:
__int64 __fastcall mbc_mbc2_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_mbc2_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

canonical_original_code:
unsigned char mbc_mbc2_read_bank_n(unsigned short address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}


decompiled_code:
void mbc_mbc2_write_ram_enable()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void mbc_mbc2_write_ram_enable(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc2_write_ram_enable(unsigned short address, unsigned char data)
{
}


decompiled_code:
unsigned __int64 __fastcall mbc_mbc2_write_rom_bank_select(__int64 a1, unsigned __int8 a2)
{
unsigned __int64 result; // rax
__int64 v3; // rdx

if ( a2 )
{
LOWORD(cart[9]) = a2;
result = ((unsigned __int64)a2 << 14) / cart[10];
v3 = cart[4] + ((unsigned __int64)a2 << 14) % cart[10];
}
else
{
result = 1LL;
v3 = cart[4] + 0x4000LL;
LOWORD(cart[9]) = 1;
}
cart[7] = v3;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(__int64 a1, unsigned __int8 a2)
{
unsigned __int64 result; // rax
__int64 v3; // rdx

if ( a2 )
{
LOWORD(cart[9]) = a2;
result = ((unsigned __int64)a2 << 14) / cart[10];
v3 = cart[4] + ((unsigned __int64)a2 << 14) % cart[10];
}
else
{
result = 1LL;
v3 = cart[4] + 0x4000LL;
LOWORD(cart[9]) = 1;
}
cart[7] = v3;
return result;
}

original_code:
void mbc_mbc2_write_rom_bank_select(uint16_t address, uint8_t data)
{
 size_t offset;
 if (data == 0) {
  cart.cart_bank_num = 1;
  offset = (size_t)16384;
 } else {
  cart.cart_bank_num = data;
  offset = (size_t)data * 16384 % cart.cartromsize;
 }
 cart.cartrom_bank_n = cart.cartrom + offset;
}

canonical_original_code:
void mbc_mbc2_write_rom_bank_select(unsigned short address, unsigned char data)
{
 unsigned long offset;
 if (data == 0) {
  cart.cart_bank_num = 1;
  offset = (unsigned long)16384;
 } else {
  cart.cart_bank_num = data;
  offset = (unsigned long)data * 16384 % cart.cartromsize;
 }
 cart.cartrom_bank_n = cart.cartrom + offset;
}

variable_types:
offset
unsigned long


decompiled_code:
__int64 __fastcall mbc_mbc2_read_extram(__int16 a1)
{
return *(unsigned __int8 *)(cart[11] + (a1 & 0x1FF));
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1)
{
return *(unsigned __int8 *)(cart[11] + (a1 & 0x1FF));
}

original_code:
uint8_t mbc_mbc2_read_extram(uint16_t address)
{
 return cart.extram[address & 0x01ff];
}

canonical_original_code:
unsigned char mbc_mbc2_read_extram(unsigned short address)
{
 return cart.extram[address & 0x01ff];
}


decompiled_code:
__int64 mbc_mbc2_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
void (**v4)(); // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rax
__int64 v7; // rax
__int64 result; // rax

v0 = (char *)readmem;
do
{
*(_QWORD *)v0 = mbc_mbc2_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem[64] );
v1 = (unsigned int)((_DWORD)readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc2_read_bank_n;
v1 += 8LL;
}
while ( (_QWORD *)v1 != &readmem[128] );
v2 = (char *)writemem;
do
{
*(_QWORD *)v2 = mbc_mbc2_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem[32] );
v3 = (unsigned int)((_DWORD)writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc2_write_rom_bank_select;
v3 += 8LL;
}
while ( (_QWORD *)v3 != &writemem[64] );
v4 = (void (**)())(unsigned int)((_DWORD)writemem + 512);
do
*v4++ = mbc_mbc2_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)writemem + 1024) != v4 );
readmem[160] = mbc_mbc2_read_extram;
v5 = (unsigned int)((_DWORD)readmem + 1296);
readmem[161] = mbc_mbc2_read_extram;
do
{
*(_QWORD *)v5 = mbc_mbc2_read_ff;
v5 += 8LL;
}
while ( (_QWORD *)v5 != &readmem[192] );
writemem[160] = mbc_mbc2_write_extram;
v6 = (unsigned int)((_DWORD)writemem + 1296);
writemem[161] = mbc_mbc2_write_extram;
do
{
*(_QWORD *)v6 = mbc_mbc2_write_dummy;
v6 += 8LL;
}
while ( (_QWORD *)v6 != &writemem[192] );
v7 = cart[4];
cart[6] = v7;
result = v7 + 0x4000;
cart[7] = result;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
void (**v4)(); // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rax
__int64 v7; // rax
__int64 result; // rax

v0 = (char *)readmem;
do
{
*(_QWORD *)v0 = mbc_mbc2_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem[64] );
v1 = (unsigned int)((_DWORD)readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc2_read_bank_n;
v1 += 8LL;
}
while ( (_QWORD *)v1 != &readmem[128] );
v2 = (char *)writemem;
do
{
*(_QWORD *)v2 = mbc_mbc2_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem[32] );
v3 = (unsigned int)((_DWORD)writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc2_write_rom_bank_select;
v3 += 8LL;
}
while ( (_QWORD *)v3 != &writemem[64] );
v4 = (void (**)())(unsigned int)((_DWORD)writemem + 512);
do
*v4++ = mbc_mbc2_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)writemem + 1024) != v4 );
readmem[160] = mbc_mbc2_read_extram;
v5 = (unsigned int)((_DWORD)readmem + 1296);
readmem[161] = mbc_mbc2_read_extram;
do
{
*(_QWORD *)v5 = mbc_mbc2_read_ff;
v5 += 8LL;
}
while ( (_QWORD *)v5 != &readmem[192] );
writemem[160] = mbc_mbc2_write_extram;
v6 = (unsigned int)((_DWORD)writemem + 1296);
writemem[161] = mbc_mbc2_write_extram;
do
{
*(_QWORD *)v6 = mbc_mbc2_write_dummy;
v6 += 8LL;
}
while ( (_QWORD *)v6 != &writemem[192] );
v7 = cart[4];
cart[6] = v7;
result = v7 + 0x4000;
cart[7] = result;
return result;
}

original_code:
void mbc_mbc2_install()
{
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_mbc2_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_mbc2_read_bank_n;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_mbc2_write_ram_enable;
 }
 for (i = 0x20; i <= 0x3F; ++i) {
  writemem[i] = mbc_mbc2_write_rom_bank_select;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  writemem[i] = mbc_mbc2_write_dummy;
 }
 for (i = 0xA0; i <= 0xA1; ++i) {
  readmem[i] = mbc_mbc2_read_extram;
 }
 for (i = 0xA2; i <= 0xBF; ++i) {
  readmem[i] = mbc_mbc2_read_ff;
 }
 for (i = 0xA0; i <= 0xA1; ++i) {
  writemem[i] = mbc_mbc2_write_extram;
 }
 for (i = 0xA2; i <= 0xBF; ++i) {
  writemem[i] = mbc_mbc2_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
}


decompiled_code:
__int64 mbc_huc3_read_ff()
{
return 0xFFFFFFFFLL;
}

original_code:
uint8_t mbc_huc3_read_ff(uint16_t address)
{
 return 0xff;
}

canonical_original_code:
unsigned char mbc_huc3_read_ff(unsigned short address)
{
 return 0xff;
}


decompiled_code:
__int64 __fastcall mbc_huc3_read_bank_0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

original_code:
uint8_t mbc_huc3_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

canonical_original_code:
unsigned char mbc_huc3_read_bank_0(unsigned short address)
{
 return cart.cartrom_bank_zero[address];
}


decompiled_code:
__int64 __fastcall mbc_huc3_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_huc3_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

canonical_original_code:
unsigned char mbc_huc3_read_bank_n(unsigned short address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}


decompiled_code:
void mbc_huc3_cleanup()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void mbc_huc3_cleanup()
{
}

canonical_original_code:
void mbc_huc3_cleanup()
{
}


decompiled_code:
int __fastcall mbc_huc3_write_dummy(unsigned __int16 a1, unsigned __int8 a2)
{
return printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 a1, unsigned __int8 a2)
{
return printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
}

original_code:
void mbc_huc3_write_dummy(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
}

canonical_original_code:
void mbc_huc3_write_dummy(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
}


decompiled_code:
int __fastcall mbc_huc3_write_ram_enable(unsigned __int16 a1, unsigned __int8 a2)
{
int result; // eax

result = printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
LODWORD(cart[53]) = a2;
return result;
}

canonical_decompiled_code:
int __fastcall func5(unsigned __int16 a1, unsigned __int8 a2)
{
int result; // eax

result = printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
LODWORD(cart[53]) = a2;
return result;
}

original_code:
void mbc_huc3_write_ram_enable(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.huc3_ram_mode = data;
}

canonical_original_code:
void mbc_huc3_write_ram_enable(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.huc3_ram_mode = data;
}


decompiled_code:
__int64 __fastcall mbc_huc3_write_rom_bank_select_low(unsigned __int16 a1, unsigned __int8 a2)
{
int v2; // eax
int v3; // edx
__int64 v4; // rax
__int64 result; // rax

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
v2 = HIDWORD(cart[1]);
LODWORD(cart[1]) = a2;
v3 = (a2 + (v2 << 8)) % SHIDWORD(cart[0]);
v4 = (__int64)v3 << 14;
if ( !v3 )
v4 = 0x4000LL;
result = cart[4] + v4;
cart[7] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int16 a1, unsigned __int8 a2)
{
int v2; // eax
int v3; // edx
__int64 v4; // rax
__int64 result; // rax

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
v2 = HIDWORD(cart[1]);
LODWORD(cart[1]) = a2;
v3 = (a2 + (v2 << 8)) % SHIDWORD(cart[0]);
v4 = (__int64)v3 << 14;
if ( !v3 )
v4 = 0x4000LL;
result = cart[4] + v4;
cart[7] = result;
return result;
}

original_code:
void mbc_huc3_write_rom_bank_select_low(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.reg_rom_bank_low = data;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 size_t offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}

canonical_original_code:
void mbc_huc3_write_rom_bank_select_low(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.reg_rom_bank_low = data;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 unsigned long offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}


decompiled_code:
__int64 __fastcall mbc_huc3_write_rom_bank_select_high(unsigned __int16 a1, unsigned __int8 a2)
{
int v2; // ebx
int v3; // eax
int v4; // edx
__int64 v5; // rax
__int64 result; // rax

v2 = a2 & 1;
printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
v3 = cart[1];
HIDWORD(cart[1]) = v2;
v4 = ((v2 << 8) + v3) % SHIDWORD(cart[0]);
v5 = (__int64)v4 << 14;
if ( !v4 )
v5 = 0x4000LL;
result = cart[4] + v5;
cart[7] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 a1, unsigned __int8 a2)
{
int v2; // ebx
int v3; // eax
int v4; // edx
__int64 v5; // rax
__int64 result; // rax

v2 = a2 & 1;
printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
v3 = cart[1];
HIDWORD(cart[1]) = v2;
v4 = ((v2 << 8) + v3) % SHIDWORD(cart[0]);
v5 = (__int64)v4 << 14;
if ( !v4 )
v5 = 0x4000LL;
result = cart[4] + v5;
cart[7] = result;
return result;
}

original_code:
void mbc_huc3_write_rom_bank_select_high(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.reg_rom_bank_high = data & 0x01;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 size_t offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}

canonical_original_code:
void mbc_huc3_write_rom_bank_select_high(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.reg_rom_bank_high = data & 0x01;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 unsigned long offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}


decompiled_code:
int __fastcall mbc_huc3_write_ram_bank_select(unsigned __int16 a1, __int16 a2)
{
int result; // eax

result = printf("write: pc:%04X, %04X:%02X\n", state, a1, (unsigned __int8)a2);
if ( cart[18] == 0x8000LL )
cart[14] = cart[11] + ((a2 << 13) & 0x6000);
return result;
}

canonical_decompiled_code:
int __fastcall func8(unsigned __int16 a1, __int16 a2)
{
int result; // eax

result = printf("write: pc:%04X, %04X:%02X\n", state, a1, (unsigned __int8)a2);
if ( cart[18] == 0x8000LL )
cart[14] = cart[11] + ((a2 << 13) & 0x6000);
return result;
}

original_code:
void mbc_huc3_write_ram_bank_select(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 if (cart.extram_size == 32768) {
  int bank = data & 0x03;
  cart.extram_bank = cart.extram + bank * 8192;
 }
}

canonical_original_code:
void mbc_huc3_write_ram_bank_select(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 if (cart.extram_size == 32768) {
  int bank = data & 0x03;
  cart.extram_bank = cart.extram + bank * 8192;
 }
}


decompiled_code:
__int64 __fastcall mbc_huc3_read_extram(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // eax
int v3; // ecx
__int64 v4; // rcx
unsigned int v5; // er12

v1 = cart[53];
if ( v1 == 10 )
{
v4 = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
v5 = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}
else
{
v2 = v1 - 12;
v3 = -(v2 < 2);
LOBYTE(v3) = v3 & 2;
v4 = (unsigned int)(v3 + 255);
v5 = v2 < 2 ? 1 : -1;
}
printf("read: pc:%04X, %04X:%02X\n", state, a1, v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // eax
int v3; // ecx
__int64 v4; // rcx
unsigned int v5; // er12

v1 = cart[53];
if ( v1 == 10 )
{
v4 = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
v5 = *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}
else
{
v2 = v1 - 12;
v3 = -(v2 < 2);
LOBYTE(v3) = v3 & 2;
v4 = (unsigned int)(v3 + 255);
v5 = v2 < 2 ? 1 : -1;
}
printf("read: pc:%04X, %04X:%02X\n", state, a1, v4);
return v5;
}

original_code:
uint8_t mbc_huc3_read_extram(uint16_t address)
{
 uint8_t data;
 switch (cart.huc3_ram_mode) {
 default:
 case 0:
  data = 0xff;
  break;
 case 0x0A:
  data = cart.extram_bank[address & 0x1fff];
  break;
 case 0x0C:
 case 0x0D:
  data = 0x01;
  break;
 }
 printf("read: pc:%04X, %04X:%02X\n", state.pc, address, data);
 return data;
}

canonical_original_code:
unsigned char mbc_huc3_read_extram(unsigned short address)
{
 unsigned char data;
 switch (cart.huc3_ram_mode) {
 default:
 case 0:
  data = 0xff;
  break;
 case 0x0A:
  data = cart.extram_bank[address & 0x1fff];
  break;
 case 0x0C:
 case 0x0D:
  data = 0x01;
  break;
 }
 printf("read: pc:%04X, %04X:%02X\n", state.pc, address, data);
 return data;
}


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

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

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

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

original_code:
void mbc_huc3_write_extram(uint16_t address, uint8_t data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.extram_bank[address & 0x1fff] = data;
}

canonical_original_code:
void mbc_huc3_write_extram(unsigned short address, unsigned char data)
{
 printf("write: pc:%04X, %04X:%02X\n", state.pc, address, data);
 cart.extram_bank[address & 0x1fff] = data;
}


decompiled_code:
__int64 mbc_huc3_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
unsigned __int64 v2; // rax
char *v3; // rdx
char *v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

cart[20] = mbc_huc3_cleanup;
v0 = (char *)&readmem;
LODWORD(cart[53]) = 0;
do
{
*(_QWORD *)v0 = mbc_huc3_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_huc3_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (unsigned int)((_DWORD)writemem + 384);
v3 = (char *)writemem;
do
{
*(_QWORD *)v2 = mbc_huc3_write_rom_bank_select_high;
v2 += 8LL;
}
while ( (_QWORD *)v2 != &writemem[64] );
v4 = (char *)writemem;
do
{
*(_QWORD *)v4 = mbc_huc3_write_dummy;
v4 += 8;
}
while ( v4 != (char *)&writemem[128] );
do
{
*(_QWORD *)v3 = mbc_huc3_write_ram_enable;
v3 += 8;
}
while ( v3 != (char *)&writemem[32] );
writemem[32] = mbc_huc3_write_rom_bank_select_low;
v5 = (unsigned int)((_DWORD)writemem + 768);
writemem[64] = mbc_huc3_write_ram_bank_select;
do
{
*(_QWORD *)v5 = mbc_huc3_write_dummy;
v5 += 8LL;
}
while ( (_QWORD *)v5 != &writemem[128] );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_20;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_huc3_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_20:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_huc3_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_28;
}
v14 = 160LL;
do
writemem[v14++] = mbc_huc3_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_28:
do
{
v15 = v9++;
writemem[v15] = mbc_huc3_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
LOBYTE(cart[17]) = 0;
cart[1] = 1LL;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
char *v0; // rax
unsigned __int64 v1; // rax
unsigned __int64 v2; // rax
char *v3; // rdx
char *v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

cart[20] = mbc_huc3_cleanup;
v0 = (char *)&readmem;
LODWORD(cart[53]) = 0;
do
{
*(_QWORD *)v0 = mbc_huc3_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_huc3_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (unsigned int)((_DWORD)writemem + 384);
v3 = (char *)writemem;
do
{
*(_QWORD *)v2 = mbc_huc3_write_rom_bank_select_high;
v2 += 8LL;
}
while ( (_QWORD *)v2 != &writemem[64] );
v4 = (char *)writemem;
do
{
*(_QWORD *)v4 = mbc_huc3_write_dummy;
v4 += 8;
}
while ( v4 != (char *)&writemem[128] );
do
{
*(_QWORD *)v3 = mbc_huc3_write_ram_enable;
v3 += 8;
}
while ( v3 != (char *)&writemem[32] );
writemem[32] = mbc_huc3_write_rom_bank_select_low;
v5 = (unsigned int)((_DWORD)writemem + 768);
writemem[64] = mbc_huc3_write_ram_bank_select;
do
{
*(_QWORD *)v5 = mbc_huc3_write_dummy;
v5 += 8LL;
}
while ( (_QWORD *)v5 != &writemem[128] );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_20;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_huc3_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_20:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_huc3_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_28;
}
v14 = 160LL;
do
writemem[v14++] = mbc_huc3_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_28:
do
{
v15 = v9++;
writemem[v15] = mbc_huc3_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
LOBYTE(cart[17]) = 0;
cart[1] = 1LL;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

original_code:
void mbc_huc3_install()
{
 cart.cleanup = &mbc_huc3_cleanup;
 cart.huc3_ram_mode = 0;
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_huc3_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_huc3_read_bank_n;
 }
 for (i = 0x30; i <= 0x3F; ++i) {
  writemem[i] = mbc_huc3_write_rom_bank_select_high;
 }
 for (i = 0x00; i <= 0x7F; ++i) {
  writemem[i] = mbc_huc3_write_dummy;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_huc3_write_ram_enable;
 }
 for (i = 0x20; i <= 0x20; ++i) {
  writemem[i] = mbc_huc3_write_rom_bank_select_low;
 }
 for (i = 0x40; i <= 0x40; ++i) {
  writemem[i] = mbc_huc3_write_ram_bank_select;
 }
 for (i = 0x60; i <= 0x7F; ++i) {
  writemem[i] = mbc_huc3_write_dummy;
 }
 int extram_end =
     0xA0 + (cart.extram_size > 8192 ? 8192 : cart.extram_size) / 256;
 for (i = 0xA0; i < extram_end; ++i) {
  readmem[i] = mbc_huc3_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_huc3_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_huc3_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_huc3_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
 cart.reg_rom_bank_low = 1;
 cart.reg_rom_bank_high = 0;
 cart.extram_bank = cart.extram;
 cart.extram_bank_num = 0;
}


decompiled_code:
void cart_default_cleanup()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void cart_default_cleanup()
{
}

canonical_original_code:
void cart_default_cleanup()
{
}


decompiled_code:
char *__fastcall romname2savename(char *dest, char *src, int a3)
{
char *v4; // r13
int v5; // er12
int v6; // er12

if ( !src )
return 0LL;
v4 = strrchr(src, 46);
if ( v4 )
{
v5 = strlen(src);
v6 = v5 - strlen(v4);
strncpy(dest, src, v6);
dest[v6] = 0;
}
else
{
strncpy(dest, src, a3 - 4);
}
strcat(dest, ".sav");
return dest;
}

canonical_decompiled_code:
char *__fastcall func1(char *dest, char *src, int a3)
{
char *v4; // r13
int v5; // er12
int v6; // er12

if ( !src )
return 0LL;
v4 = strrchr(src, 46);
if ( v4 )
{
v5 = strlen(src);
v6 = v5 - strlen(v4);
strncpy(dest, src, v6);
dest[v6] = 0;
}
else
{
strncpy(dest, src, a3 - 4);
}
strcat(dest, ".sav");
return dest;
}

original_code:
char *romname2savename(char *savename, char *romname, int savenamelength)
{
 if (romname == ((void *)0)) {
  return ((void *)0);
 }
 char *suffix = strrchr(romname, (int)'.');
 if (suffix == ((void *)0)) {
  strncpy(savename, romname, savenamelength - 4);
  strcat(savename, ".sav");
 } else {
  int prefixlength = strlen(romname) - strlen(suffix);
  strncpy(savename, romname, prefixlength);
  savename[prefixlength] = '\0';
  strcat(savename, ".sav");
 }
 return savename;
}

variable_types:
savename
char *

variable_types:
romname
char *

variable_types: savenamelength int

variable_types:
suffix
char *

variable_types: prefixlength int

return_type: char *


decompiled_code:
int cart_reset_mbc()
{
void (*v0)(void); // rax
int result; // eax

v0 = *(void (**)(void))&cart[160];
if ( v0 )
v0();
if ( state[21] )
{
puts("MBC reset: boot rom");
result = mbc_boot_install();
}
else
{
if ( *(int *)cart <= 34 )
{
if ( *(int *)cart >= 0 )
{
switch ( *(_DWORD *)cart )
{
case 0:
mbc_none_install();
goto LABEL_10;
case 1:
case 2:
case 3:
mbc_mbc1_install();
goto LABEL_10;
case 5:
case 6:
mbc_mbc2_install();
goto LABEL_10;
case 0xF:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
mbc_mbc3_install();
goto LABEL_10;
case 0x19:
case 0x1A:
case 0x1B:
case 0x1C:
case 0x1D:
case 0x1E:
mbc_mbc5_install();
goto LABEL_10;
case 0x22:
mbc_mbc7_install();
goto LABEL_10;
default:
break;
}
}
LABEL_21:
printf(
"MBC reset: Unhandled cart type: %02Xh %s\n",
*(unsigned int *)cart,
*((const char **)&cartdesc_carttype + *(int *)cart));
exit(1);
}
switch ( *(_DWORD *)cart )
{
case 0xFE:
mbc_huc3_install();
break;
case 0xFF:
mbc_huc1_install();
break;
case 0xFC:
mbc_cam_install();
break;
default:
goto LABEL_21;
}
LABEL_10:
result = printf("MBC reset: %02Xh %s\n", *(int *)cart, *((const char **)&cartdesc_carttype + *(int *)cart));
}
return result;
}

canonical_decompiled_code:
int func4()
{
void (*v0)(void); // rax
int result; // eax

v0 = *(void (**)(void))&cart[160];
if ( v0 )
v0();
if ( state[21] )
{
puts("MBC reset: boot rom");
result = mbc_boot_install();
}
else
{
if ( *(int *)cart <= 34 )
{
if ( *(int *)cart >= 0 )
{
switch ( *(_DWORD *)cart )
{
case 0:
mbc_none_install();
goto LABEL_10;
case 1:
case 2:
case 3:
mbc_mbc1_install();
goto LABEL_10;
case 5:
case 6:
mbc_mbc2_install();
goto LABEL_10;
case 0xF:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
mbc_mbc3_install();
goto LABEL_10;
case 0x19:
case 0x1A:
case 0x1B:
case 0x1C:
case 0x1D:
case 0x1E:
mbc_mbc5_install();
goto LABEL_10;
case 0x22:
mbc_mbc7_install();
goto LABEL_10;
default:
break;
}
}
LABEL_21:
printf(
"MBC reset: Unhandled cart type: %02Xh %s\n",
*(unsigned int *)cart,
*((const char **)&cartdesc_carttype + *(int *)cart));
exit(1);
}
switch ( *(_DWORD *)cart )
{
case 0xFE:
mbc_huc3_install();
break;
case 0xFF:
mbc_huc1_install();
break;
case 0xFC:
mbc_cam_install();
break;
default:
goto LABEL_21;
}
LABEL_10:
result = printf("MBC reset: %02Xh %s\n", *(int *)cart, *((const char **)&cartdesc_carttype + *(int *)cart));
}
return result;
}

original_code:
void cart_reset_mbc()
{
 if (cart.cleanup != ((void *)0))
  cart.cleanup();
 if (state.bootRomEnabled) {
  printf("MBC reset: boot rom\n");
  mbc_boot_install();
  return;
 }
 switch (cart.mbc_type) {
 case 0x00:
  mbc_none_install();
  break;
 case 0x01:
 case 0x02:
 case 0x03:
  mbc_mbc1_install();
  break;
 case 0x05:
 case 0x06:
  mbc_mbc2_install();
  break;
 case 0x0F:
 case 0x10:
 case 0x11:
 case 0x12:
 case 0x13:
  mbc_mbc3_install();
  break;
 case 0x19:
 case 0x1A:
 case 0x1B:
 case 0x1C:
 case 0x1D:
 case 0x1E:
  mbc_mbc5_install();
  break;
 case 0x22:
  mbc_mbc7_install();
  break;
 case 0xFC:
  mbc_cam_install();
  break;
 case 0xFE:
  mbc_huc3_install();
  break;
 case 0xFF:
  mbc_huc1_install();
  break;
 default:
  printf("MBC reset: Unhandled cart type: %02Xh %s\n",
         cart.mbc_type, cartdesc_carttype[cart.mbc_type]);
  exit(1);
  break;
 }
 printf("MBC reset: %02Xh %s\n", cart.mbc_type,
        cartdesc_carttype[cart.mbc_type]);
}


decompiled_code:
int __fastcall cart_init(char *filename, char *src)
{
struct stat v3; // [rsp+0h] [rbp-A8h] BYREF

if ( stat(src, &v3) )
{
printf("Couldn't stat cartrom %s\n", src);
exit(1);
}
return cart_init_file(filename, src);
}

canonical_decompiled_code:
int __fastcall func6(char *filename, char *src)
{
struct stat v3; // [rsp+0h] [rbp-A8h] BYREF

if ( stat(src, &v3) )
{
printf("Couldn't stat cartrom %s\n", src);
exit(1);
}
return func5(filename, src);
}

original_code:
void cart_init(char *bootromName, char *cartromName)
{
 struct stat s;
 if (stat(cartromName, &s) != 0) {
  printf("Couldn't stat cartrom %s\n", cartromName);
  exit(1);
 }
 cart_init_file(bootromName, cartromName);
}

variable_types:
bootromName
char *

variable_types:
cartromName
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
__int64 mbc_mbc7_read_ff()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0xFFFFFFFFLL;
}

original_code:
uint8_t mbc_mbc7_read_ff(uint16_t address)
{
 return 0xff;
}

canonical_original_code:
unsigned char mbc_mbc7_read_ff(unsigned short address)
{
 return 0xff;
}


decompiled_code:
void mbc_mbc7_write_dummy()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void mbc_mbc7_write_dummy(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc7_write_dummy(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_mbc7_read_bank_0(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
return *(unsigned __int8 *)(cart[6] + a1);
}

original_code:
uint8_t mbc_mbc7_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

canonical_original_code:
unsigned char mbc_mbc7_read_bank_0(unsigned short address)
{
 return cart.cartrom_bank_zero[address];
}


decompiled_code:
__int64 __fastcall mbc_mbc7_read_bank_n(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return *(unsigned __int8 *)(cart[7] + (a1 & 0x3FFF));
}

original_code:
uint8_t mbc_mbc7_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

canonical_original_code:
unsigned char mbc_mbc7_read_bank_n(unsigned short address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}


decompiled_code:
void mbc_mbc7_write_ram_enable()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void mbc_mbc7_write_ram_enable(uint16_t address, uint8_t data)
{
}

canonical_original_code:
void mbc_mbc7_write_ram_enable(unsigned short address, unsigned char data)
{
}


decompiled_code:
__int64 __fastcall mbc_mbc7_write_rom_bank_select(__int64 a1, unsigned __int8 a2)
{
int v2; // eax
int v3; // edx
__int64 v4; // rax
__int64 result; // rax

v2 = HIDWORD(cart[1]);
LODWORD(cart[1]) = a2;
v3 = (a2 + (v2 << 8)) % SHIDWORD(cart[0]);
v4 = (__int64)v3 << 14;
if ( !v3 )
v4 = 0x4000LL;
result = cart[4] + v4;
cart[7] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int8 a2)
{
int v2; // eax
int v3; // edx
__int64 v4; // rax
__int64 result; // rax

v2 = HIDWORD(cart[1]);
LODWORD(cart[1]) = a2;
v3 = (a2 + (v2 << 8)) % SHIDWORD(cart[0]);
v4 = (__int64)v3 << 14;
if ( !v3 )
v4 = 0x4000LL;
result = cart[4] + v4;
cart[7] = result;
return result;
}

original_code:
void mbc_mbc7_write_rom_bank_select(uint16_t address, uint8_t data)
{
 cart.reg_rom_bank_low = data;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 size_t offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}

canonical_original_code:
void mbc_mbc7_write_rom_bank_select(unsigned short address, unsigned char data)
{
 cart.reg_rom_bank_low = data;
 int bank = cart.reg_rom_bank_low + (int)(cart.reg_rom_bank_high) * 256;
 bank = bank % cart.cartrom_num_banks;
 if (bank == 0)
  bank = 1;
 unsigned long offset = (size_t)bank * 16384;
 cart.cartrom_bank_n = cart.cartrom + offset;
}


decompiled_code:
void __fastcall mbc_mbc7_write_ram_bank_select(__int64 a1, __int16 a2)
{
if ( cart[18] == 0x8000LL )
cart[14] = cart[11] + ((a2 << 13) & 0x6000);
}

original_code:
void mbc_mbc7_write_ram_bank_select(uint16_t address, uint8_t data)
{
 if (cart.extram_size == 32768) {
  int bank = data & 0x03;
  cart.extram_bank = cart.extram + bank * 8192;
 }
}

canonical_original_code:
void mbc_mbc7_write_ram_bank_select(unsigned short address, unsigned char data)
{
 if (cart.extram_size == 32768) {
  int bank = data & 0x03;
  cart.extram_bank = cart.extram + bank * 8192;
 }
}


decompiled_code:
__int64 __fastcall mbc_mbc7_read_extram(__int16 a1)
{
return *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1)
{
return *(unsigned __int8 *)(cart[14] + (a1 & 0x1FFF));
}

original_code:
uint8_t mbc_mbc7_read_extram(uint16_t address)
{
 return cart.extram_bank[address & 0x1fff];
}

canonical_original_code:
unsigned char mbc_mbc7_read_extram(unsigned short address)
{
 return cart.extram_bank[address & 0x1fff];
}


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

result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

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

result = cart[14];
*(_BYTE *)(result + (a1 & 0x1FFF)) = a2;
return result;
}

original_code:
void mbc_mbc7_write_extram(uint16_t address, uint8_t data)
{
 cart.extram_bank[address & 0x1fff] = data;
}

canonical_original_code:
void mbc_mbc7_write_extram(unsigned short address, unsigned char data)
{
 cart.extram_bank[address & 0x1fff] = data;
}


decompiled_code:
void mbc_mbc7_cleanup()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

original_code:
void mbc_mbc7_cleanup()
{
}

canonical_original_code:
void mbc_mbc7_cleanup()
{
}


decompiled_code:
__int64 mbc_mbc7_install()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

cart[20] = mbc_mbc7_cleanup;
v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_mbc7_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc7_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_mbc7_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc7_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc7_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc7_write_dummy;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc7_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_mbc7_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_mbc7_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_mbc7_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
LOBYTE(cart[17]) = 0;
cart[1] = 1LL;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func10()
{
char *v0; // rax
unsigned __int64 v1; // rax
char *v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // ecx
int v9; // eax
__int64 v10; // rsi
__int64 v11; // rdx
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rax
__int64 result; // rax

cart[20] = mbc_mbc7_cleanup;
v0 = (char *)&readmem;
do
{
*(_QWORD *)v0 = mbc_mbc7_read_bank_0;
v0 += 8;
}
while ( v0 != (char *)&readmem + 512 );
v1 = (unsigned int)((_DWORD)&readmem + 512);
do
{
*(_QWORD *)v1 = mbc_mbc7_read_bank_n;
v1 += 8LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&readmem + 1024) );
v2 = (char *)&writemem;
do
{
*(_QWORD *)v2 = mbc_mbc7_write_ram_enable;
v2 += 8;
}
while ( v2 != (char *)&writemem + 256 );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc7_write_rom_bank_select;
v3 += 8LL;
}
while ( (_UNKNOWN *)v3 != (_UNKNOWN *)((char *)&writemem + 512) );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc7_write_ram_bank_select;
v4 += 8LL;
}
while ( (_UNKNOWN *)v4 != (_UNKNOWN *)((char *)&writemem + 768) );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc7_write_dummy;
v5 += 8LL;
}
while ( (_UNKNOWN *)v5 != (_UNKNOWN *)((char *)&writemem + 1024) );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
v9 = v7 + 160;
if ( !(_DWORD)v7 )
goto LABEL_21;
v10 = (unsigned int)(v7 - 1);
v11 = 160LL;
v12 = v10 + 161;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc7_read_extram;
while ( v12 != v11 );
if ( v8 != 192 )
{
LABEL_21:
v13 = v8;
do
*((_QWORD *)&readmem + v13++) = mbc_mbc7_read_ff;
while ( (int)v13 <= 191 );
if ( v8 == 160 )
goto LABEL_29;
}
v14 = 160LL;
do
*((_QWORD *)&writemem + v14++) = mbc_mbc7_write_extram;
while ( v8 > (int)v14 );
if ( v8 != 192 )
{
LABEL_29:
do
{
v15 = v9++;
*((_QWORD *)&writemem + v15) = mbc_mbc7_write_dummy;
}
while ( v9 != 192 );
}
v16 = cart[4];
LOBYTE(cart[17]) = 0;
cart[1] = 1LL;
cart[6] = v16;
cart[7] = v16 + 0x4000;
result = cart[11];
cart[14] = result;
return result;
}

original_code:
void mbc_mbc7_install()
{
 cart.cleanup = &mbc_mbc7_cleanup;
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_mbc7_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_mbc7_read_bank_n;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_mbc7_write_ram_enable;
 }
 for (i = 0x20; i <= 0x3F; ++i) {
  writemem[i] = mbc_mbc7_write_rom_bank_select;
 }
 for (i = 0x40; i <= 0x5F; ++i) {
  writemem[i] = mbc_mbc7_write_ram_bank_select;
 }
 for (i = 0x60; i <= 0x7F; ++i) {
  writemem[i] = mbc_mbc7_write_dummy;
 }
 int extram_end =
     0xA0 + (cart.extram_size > 8192 ? 8192 : cart.extram_size) / 256;
 for (i = 0xA0; i < extram_end; ++i) {
  readmem[i] = mbc_mbc7_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_mbc7_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_mbc7_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_mbc7_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
 cart.reg_rom_bank_low = 1;
 cart.reg_rom_bank_high = 0;
 cart.extram_bank = cart.extram;
 cart.extram_bank_num = 0;
}


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

result = SDL_SetEventFilter(input_event_filter, 0LL);
state[7] = 553648127;
return result;
}

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

result = SDL_SetEventFilter(input_event_filter, 0LL);
state[7] = 553648127;
return result;
}

original_code:
void input_init()
{
 SDL_SetEventFilter(input_event_filter, ((void *)0));
 state.joyp = 0xFF;
 state.joyp_buttons = 0xFF;
 state.joyp_directions = 0xFF;
 state.joyp_select = 0x20;
}


decompiled_code:
// attributes: thunk
__int64 input_handle()
{
return SDL_PumpEvents();
}

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

original_code:
void input_handle()
{
 SDL_PumpEvents();
}


decompiled_code:
__int64 serial_receive_bit()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
int serial_receive_bit(int in, int reset)
{
 return 1;
}

variable_types: in int

variable_types: reset int

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
int v4; // eax

if ( argc > 1 )
{
v3 = (char *)argv[1];
v4 = get_cmd_index_for_name(v3);
if ( v4 != -1 )
return ((__int64 (__fastcall *)(_QWORD, const char **))(&cmds)[5 * v4 + 1])((unsigned int)argc, argv);
printf("%s: '%s' is not a command.\n", *argv, v3);
}
else
{
print_usage();
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
int v4; // eax

if ( argc > 1 )
{
v3 = (char *)argv[1];
v4 = func4(v3);
if ( v4 != -1 )
return ((__int64 (__fastcall *)(_QWORD, const char **))(&cmds)[5 * v4 + 1])((unsigned int)argc, argv);
printf("%s: '%s' is not a command.\n", *argv, v3);
}
else
{
func5();
}
return 1;
}

original_code:
int main(int argc, char *argv[])
{
 char *name;
 int c, i;
 unsigned length;
 if (argc >= 1)
  name = argv[1];
 else
  name = "__blob";
 printf("const unsigned char %s[] = {", name);
 i = 0;
 length = 0;
 while ((-1) != (c = getchar())) {
  if (i == 0)
   printf("\n/* %04x */  ", length);
  printf("0x%02x, ", (unsigned char)c);
  if (++i == 8)
   i = 0;
  length++;
 }
 printf("\n};\n");
 printf("const unsigned %s_len = 0x%x;\n", name, length);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char *name;
 int c; int i;
 unsigned int length;
 if (argc >= 1)
  name = argv[1];
 else
  name = "__blob";
 printf("const unsigned char %s[] = {", name);
 i = 0;
 length = 0;
 while ((-1) != (c = getchar())) {
  if (i == 0)
   printf("\n/* %04x */  ", length);
  printf("0x%02x, ", (unsigned char)c);
  if (++i == 8)
   i = 0;
  length++;
 }
 printf("\n};\n");
 printf("const unsigned %s_len = 0x%x;\n", name, length);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
name
char *

variable_types: c int

variable_types:
length
unsigned int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
unsigned int v4; // er12
int v5; // er15
int v6; // eax
unsigned int v7; // er14
unsigned int v8; // ebp
unsigned int v9; // ebx
int v10; // eax
int v11; // eax

v3 = "__blob";
if ( argc > 0 )
v3 = argv[1];
v4 = 0;
printf("const unsigned char %s[] = {", v3);
v5 = 0;
v6 = getchar();
if ( v6 != -1 )
{
while ( 1 )
{
v7 = v4 + 1;
v8 = (unsigned __int8)v6;
v9 = v4 + 1;
if ( !v5 )
break;
++v5;
printf("0x%02x, ", (unsigned __int8)v6);
if ( v5 == 8 )
{
v10 = getchar();
if ( v10 == -1 )
{
++v4;
goto LABEL_11;
}
v8 = (unsigned __int8)v10;
goto LABEL_8;
}
LABEL_10:
v4 = v7;
v6 = getchar();
if ( v6 == -1 )
goto LABEL_11;
}
v9 = v4;
LABEL_8:
v4 = v9 + 1;
printf("\n/* %04x */ ", v9);
printf("0x%02x, ", v8);
v11 = getchar();
if ( v11 == -1 )
goto LABEL_11;
v7 = v9 + 2;
printf("0x%02x, ", (unsigned __int8)v11);
v5 = 2;
goto LABEL_10;
}
LABEL_11:
puts("\n};");
printf("const unsigned %s_len = 0x%x;\n", v3, v4);
return 0;
}

variable_types: i int


decompiled_code:
unsigned __int64 __fastcall create_zero_mask(__int64 a1)
{
return ((a1 - 1) & (unsigned __int64)~a1) >> 7;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
return ((a1 - 1) & (unsigned __int64)~a1) >> 7;
}

original_code:
unsigned long create_zero_mask(unsigned long bits)
{
 bits = (bits - 1) & ~bits;
 return bits >> 7;
}

variable_types:
bits
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall has_zero(__int64 a1, __int64 *a2, _QWORD *a3)
{
__int64 result; // rax

result = (a1 - *a3) & a3[1] & ~a1;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 *a2, _QWORD *a3)
{
__int64 result; // rax

result = (a1 - *a3) & a3[1] & ~a1;
*a2 = result;
return result;
}

original_code:
unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
{
 unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
 *bits = mask;
 return mask;
}

canonical_original_code:
unsigned long has_zero(unsigned long a, unsigned long *bits, struct word_at_a_time *c)
{
 unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
 *bits = mask;
 return mask;
}

variable_types:
a
unsigned long

variable_types:
bits
unsigned long *

variable_types: struct word_at_a_time { unsigned long one_bits; } *

variable_types:
mask
unsigned long


decompiled_code:
unsigned __int64 __fastcall find_zero(__int64 a1)
{
return (unsigned __int64)(0x1020304050608LL * a1) >> 56;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1)
{
return (unsigned __int64)(0x1020304050608LL * a1) >> 56;
}

original_code:
unsigned long find_zero(unsigned long mask)
{
 unsigned long ret = mask*0x0001020304050608ul >> 56;
 return ret;
}

variable_types:
ret
unsigned long


original_code:
long do_strnlen_user(const char *src, unsigned long count, unsigned long max)
{
 const struct word_at_a_time constants = { ((~0ul / 0xff) * (0x01)), ((~0ul / 0xff) * (0x80)) };
 long align, res = 0;
 unsigned long c;
 if (max > count)
  max = count;
 align = (sizeof(long) - 1) & (unsigned long)src;
 src -= align;
 max += align;
 if ((({ switch(sizeof(*((unsigned long *)src))) { case 8: { (c) = *((unsigned long *)src); break; }; default: break; } 0; })))
  return 0;
 c |= ((1ul << 8*(align))-1);
 for (;;) {
  unsigned long data;
  if (has_zero(c, &data, &constants)) {
   data = (data);
   data = create_zero_mask(data);
   return res + find_zero(data) + 1 - align;
  }
  res += sizeof(unsigned long);
  if ((max < sizeof(unsigned long)))
   break;
  max -= sizeof(unsigned long);
  if ((({ switch(sizeof(*((unsigned long *)(src+res)))) { case 8: { (c) = *((unsigned long *)(src+res)); break; }; default: break; } 0; })))
   return 0;
 }
 res -= align;
 if (res >= count)
  return count+1;
 return 0;
}

canonical_original_code:
long do_strnlen_user(char *src, unsigned long count, unsigned long max)
{
 struct word_at_a_time constants = { ((~0ul / 0xff) * (0x01)), ((~0ul / 0xff) * (0x80)) };
 long align; long res = 0;
 unsigned long c;
 if (max > count)
  max = count;
 align = (sizeof(long) - 1) & (unsigned long)src;
 src -= align;
 max += align;
 if ((({ switch(sizeof(*((unsigned long *)src))) { case 8: { (c) = *((unsigned long *)src); break; }; default: break; } 0; })))
  return 0;
 c |= ((1ul << 8*(align))-1);
 for (;;) {
  unsigned long data;
  if (has_zero(c, &data, &constants)) {
   data = (data);
   data = create_zero_mask(data);
   return res + find_zero(data) + 1 - align;
  }
  res += sizeof(unsigned long);
  if ((max < sizeof(unsigned long)))
   break;
  max -= sizeof(unsigned long);
  if ((({ switch(sizeof(*((unsigned long *)(src+res)))) { case 8: { (c) = *((unsigned long *)(src+res)); break; }; default: break; } 0; })))
   return 0;
 }
 res -= align;
 if (res >= count)
  return count+1;
 return 0;
}

variable_types:
src
char *

variable_types:
count
unsigned long

variable_types:
max
unsigned long

variable_types: struct word_at_a_time { unsigned long one_bits; }

variable_types: align long

variable_types: res long

variable_types:
c
unsigned long

variable_types:
data
unsigned long

return_type: long


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

if ( a2 <= 0 || a1 == -1 )
result = 0LL;
else
result = do_strnlen_user(a1);
return result;
}

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

if ( a2 <= 0 || a1 == -1 )
result = 0LL;
else
result = func3(a1);
return result;
}

original_code:
long strnlen_user(const char *str, long count)
{
 unsigned long max_addr, src_addr;
 if (count <= 0)
  return 0;
 max_addr = -1UL;
 src_addr = (unsigned long)str;
 if (src_addr < max_addr) {
  unsigned long max = max_addr - src_addr;
  return do_strnlen_user(str, count, max);
 }
 return 0;
}

canonical_original_code:
long strnlen_user(char *str, long count)
{
 unsigned long max_addr; unsigned long src_addr;
 if (count <= 0)
  return 0;
 max_addr = -1UL;
 src_addr = (unsigned long)str;
 if (src_addr < max_addr) {
  unsigned long max = max_addr - src_addr;
  return do_strnlen_user(str, count, max);
 }
 return 0;
}

variable_types:
str
char *

variable_types: count long


incr

repo: jknightdoeswork/nettop

decompiled_code:
__int64 __fastcall incr(__int64 a1)
{
int v1; // eax
int v2; // edx
int v3; // edx
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 24);
v2 = v1 + 1;
if ( v1 == 19 )
v2 = 0;
*(_DWORD *)(a1 + 24) = v2;
v3 = *(_DWORD *)(a1 + 28);
result = (unsigned int)(v3 + 1);
if ( v3 == 19 )
result = 0LL;
*(_DWORD *)(a1 + 28) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax
int v2; // edx
int v3; // edx
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 24);
v2 = v1 + 1;
if ( v1 == 19 )
v2 = 0;
*(_DWORD *)(a1 + 24) = v2;
v3 = *(_DWORD *)(a1 + 28);
result = (unsigned int)(v3 + 1);
if ( v3 == 19 )
result = 0LL;
*(_DWORD *)(a1 + 28) = result;
return result;
}

original_code:
void incr(struct window *q)
{
    if(++(q->first) == 20)
        q->first = 0;
    if(++(q->last) == 20)
        q->last = 0;
    return;
}

variable_types: struct window { struct packet { char *msg; long enqT; int acknum; int received; int sent; struct packet *next; } *head; struct packet *tail; int sz; int max; int first; int last; int cur; } *

user_defined_types: struct window { struct packet *head; struct packet *tail; int sz; int max; int first; int last; int cur; }


plusone

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

result = (unsigned int)(a1 + 1);
if ( a1 >= 19 )
result = 0LL;
return result;
}

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

result = (unsigned int)(a1 + 1);
if ( a1 >= 19 )
result = 0LL;
return result;
}

original_code:
int plusone(int i)
{
    if(i<20 -1)
        return i+1;
    else return 0;
}

variable_types: i int


outside

decompiled_code:
__int64 __fastcall outside(__int64 a1, int a2)
{
int v2; // ecx
int v3; // edx
bool v4; // di
bool v5; // al
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 24);
v3 = *(_DWORD *)(a1 + 28);
v4 = v2 > a2;
v5 = v3 < a2;
if ( v2 >= v3 )
result = v4 & (unsigned __int8)v5;
else
result = v4 | (unsigned __int8)v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
int v2; // ecx
int v3; // edx
bool v4; // di
bool v5; // al
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 24);
v3 = *(_DWORD *)(a1 + 28);
v4 = v2 > a2;
v5 = v3 < a2;
if ( v2 >= v3 )
result = v4 & (unsigned __int8)v5;
else
result = v4 | (unsigned __int8)v5;
return result;
}

original_code:
int outside(struct window *q, int acknum)
{
    int first = q->first, last = q->last;
    if(last > first)
    {
        if(acknum < first || acknum > last)
            return 1;
        else
            return 0;
    }
    else
    {
        if(acknum < first && acknum > last)
            return 1;
        else
            return 0;
    }
}

canonical_original_code:
int outside(struct window *q, int acknum)
{
    int first = q->first; int last = q->last;
    if(last > first)
    {
        if(acknum < first || acknum > last)
            return 1;
        else
            return 0;
    }
    else
    {
        if(acknum < first && acknum > last)
            return 1;
        else
            return 0;
    }
}

variable_types: acknum int

variable_types: first int

variable_types: last int

user_defined_types: struct packet { char *msg; long enqT; int acknum; int received; int sent; struct packet *next; }


interpret

decompiled_code:
__int64 __fastcall interpret(char *a1)
{
char v1; // dl
__int64 result; // rax

v1 = *a1;
if ( *a1 == 43 )
return 2LL;
result = 1LL;
if ( v1 != 45 )
result = (unsigned int)(v1 == 38) + 3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1)
{
char v1; // dl
__int64 result; // rax

v1 = *a1;
if ( *a1 == 43 )
return 2LL;
result = 1LL;
if ( v1 != 45 )
result = (unsigned int)(v1 == 38) + 3;
return result;
}

original_code:
int interpret(char *msg)
{
    if(msg[0] == '+')
        return enumpureack;
    else if(msg[0] == '-')
        return enumnack;
    else if (msg[0] == '&')
        return enumdvrmsg;
    else
        return enumrealmsg;
}

variable_types:
msg
char *


ihavemsg

decompiled_code:
__int64 __fastcall ihavemsg(__int64 *a1, int a2)
{
__int64 result; // rax

result = *a1;
if ( !*a1 )
return 0LL;
while ( *(_DWORD *)(result + 16) != a2 )
{
result = *(_QWORD *)(result + 32);
if ( !result )
return result;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, int a2)
{
__int64 result; // rax

result = *a1;
if ( !*a1 )
return 0LL;
while ( *(_DWORD *)(result + 16) != a2 )
{
result = *(_QWORD *)(result + 32);
if ( !result )
return result;
}
return 1LL;
}

original_code:
int ihavemsg(struct window *q, int ack)
{
    struct packet *el = q->head;
    while(el != ((void *)0))
    {
        if(el->acknum == ack)
            return 1;
        el = el->next;
    }
    return 0;
}

variable_types: ack int

variable_types: struct packet { char *msg; long enqT; int acknum; int received; int sent; struct packet *next; } *


msginorder

decompiled_code:
_BOOL8 __fastcall msginorder(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 24) == a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 24) == a2;
}

original_code:
int msginorder(struct window *q, int ack)
{
    if(ack == q->first)
        return 1;
    else return 0;
}


freetok

decompiled_code:
void __fastcall freetok(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}
}

original_code:
void freetok(struct msgtok *tok)
{
    if(tok == ((void *)0))
        return;
    else
    {
     free(tok->orig);
        free(tok->ack);
        free(tok->src);
        free(tok->dest);
        free(tok->pay);
        free(tok);
    }
    return;
}

variable_types: struct msgtok { char *orig; char *ack; int acknum; int type; } *