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

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

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

if ( a2 )
{
LOWORD(cart[9]) = a2;
result = ((unsigned __int64)a2 << 14) / cart[10];
v2 = ((unsigned __int64)a2 << 14) % cart[10];
}
else
{
LOWORD(cart[9]) = 1;
v2 = 0x4000LL;
}
cart[7] = cart[4] + v2;
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;
}


repo: jkbenaim/cboy

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

variable_types:
address
unsigned short

return_type: unsigned char


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

v0 = (__int64 (__fastcall **)(unsigned __int16))readmem;
do
*v0++ = mbc_mbc2_read_bank_0;
while ( v0 != (__int64 (__fastcall **)(unsigned __int16))(unsigned int)((_DWORD)readmem + 512) );
v1 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)readmem + 512);
do
*v1++ = mbc_mbc2_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)readmem + 1024) );
v2 = (void (**)())writemem;
do
*v2++ = mbc_mbc2_write_ram_enable;
while ( v2 != (void (**)())(unsigned int)((_DWORD)writemem + 256) );
v3 = (unsigned __int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)writemem + 256);
do
*v3++ = mbc_mbc2_write_rom_bank_select;
while ( v3 != (unsigned __int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)writemem + 512) );
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;
readmem[161] = mbc_mbc2_read_extram;
v5 = (unsigned int)((_DWORD)readmem + 1296);
do
{
*(_QWORD *)v5 = mbc_mbc2_read_ff;
v5 += 8LL;
}
while ( v5 != (_DWORD)readmem + 1536 );
writemem[160] = mbc_mbc2_write_extram;
writemem[161] = mbc_mbc2_write_extram;
v6 = (void (**)())(unsigned int)((_DWORD)writemem + 1296);
do
*v6++ = mbc_mbc2_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)writemem + 1536) != v6 );
v7 = cart[4];
cart[6] = v7;
result = v7 + 0x4000;
cart[7] = result;
return result;
}

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

v0 = (__int64 (__fastcall **)(unsigned __int16))readmem;
do
*v0++ = mbc_mbc2_read_bank_0;
while ( v0 != (__int64 (__fastcall **)(unsigned __int16))(unsigned int)((_DWORD)readmem + 512) );
v1 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)readmem + 512);
do
*v1++ = mbc_mbc2_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)readmem + 1024) );
v2 = (void (**)())writemem;
do
*v2++ = mbc_mbc2_write_ram_enable;
while ( v2 != (void (**)())(unsigned int)((_DWORD)writemem + 256) );
v3 = (unsigned __int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)writemem + 256);
do
*v3++ = mbc_mbc2_write_rom_bank_select;
while ( v3 != (unsigned __int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)writemem + 512) );
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;
readmem[161] = mbc_mbc2_read_extram;
v5 = (unsigned int)((_DWORD)readmem + 1296);
do
{
*(_QWORD *)v5 = mbc_mbc2_read_ff;
v5 += 8LL;
}
while ( v5 != (_DWORD)readmem + 1536 );
writemem[160] = mbc_mbc2_write_extram;
writemem[161] = mbc_mbc2_write_extram;
v6 = (void (**)())(unsigned int)((_DWORD)writemem + 1296);
do
*v6++ = mbc_mbc2_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)writemem + 1536) != v6 );
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;
}

variable_types: i int

return_type: void


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

canonical_decompiled_code:
__int64 func0()
{
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);
}

canonical_decompiled_code:
__int64 __fastcall func1(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));
}

canonical_decompiled_code:
__int64 __fastcall func2(__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);
}

variable_types:
data
unsigned char


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; // edx
__int64 result; // rax

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

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

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
LODWORD(cart[1]) = a2;
v2 = (a2 + (HIDWORD(cart[1]) << 8)) % SHIDWORD(cart[0]);
result = 1LL;
if ( !v2 )
v2 = 1;
cart[7] = cart[4] + ((__int64)v2 << 14);
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;
}

variable_types: bank int

variable_types:
offset
unsigned long


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

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

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

printf("write: pc:%04X, %04X:%02X\n", state, a1, a2);
HIDWORD(cart[1]) = a2 & 1;
v2 = (LODWORD(cart[1]) + ((a2 & 1) << 8)) % SHIDWORD(cart[0]);
result = 1LL;
if ( !v2 )
v2 = 1;
cart[7] = cart[4] + ((__int64)v2 << 14);
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; // ebx

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

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx

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

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()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
__int64 (__fastcall **v2)(unsigned __int16, unsigned __int8); // rax
int (__fastcall **v3)(unsigned __int16, unsigned __int8); // rcx
int (__fastcall **v4)(unsigned __int16, unsigned __int8); // rdx
int (__fastcall **v5)(unsigned __int16, unsigned __int8); // rax
int (__fastcall **v6)(unsigned __int16, unsigned __int8); // rax
unsigned __int64 v7; // rdx
unsigned __int64 v8; // rdx
int v9; // eax
__int64 v10; // rcx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rax
__int64 result; // rax

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

canonical_decompiled_code:
__int64 func11()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
__int64 (__fastcall **v2)(unsigned __int16, unsigned __int8); // rax
int (__fastcall **v3)(unsigned __int16, unsigned __int8); // rcx
int (__fastcall **v4)(unsigned __int16, unsigned __int8); // rdx
int (__fastcall **v5)(unsigned __int16, unsigned __int8); // rax
int (__fastcall **v6)(unsigned __int16, unsigned __int8); // rax
unsigned __int64 v7; // rdx
unsigned __int64 v8; // rdx
int v9; // eax
__int64 v10; // rcx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rax
__int64 result; // rax

cart[20] = mbc_huc3_cleanup;
LODWORD(cart[53]) = 0;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_huc3_read_bank_0;
while ( v0 != (__int64 (__fastcall **)(unsigned __int16))(unsigned int)((_DWORD)&readmem + 512) );
v1 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 512);
do
*v1++ = mbc_huc3_read_bank_n;
while ( (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) != v1 );
v2 = (__int64 (__fastcall **)(unsigned __int16, unsigned __int8))(unsigned int)((_DWORD)writemem + 384);
v3 = (int (__fastcall **)(unsigned __int16, unsigned __int8))writemem;
do
*v2++ = mbc_huc3_write_rom_bank_select_high;
while ( v2 != (__int64 (__fastcall **)(unsigned __int16, unsigned __int8))(unsigned int)((_DWORD)writemem + 512) );
v4 = (int (__fastcall **)(unsigned __int16, unsigned __int8))(unsigned int)((_DWORD)writemem + 1024);
v5 = (int (__fastcall **)(unsigned __int16, unsigned __int8))writemem;
do
*v5++ = mbc_huc3_write_dummy;
while ( v4 != v5 );
do
*v3++ = mbc_huc3_write_ram_enable;
while ( (int (__fastcall **)(unsigned __int16, unsigned __int8))(unsigned int)((_DWORD)writemem + 256) != v3 );
writemem[32] = mbc_huc3_write_rom_bank_select_low;
writemem[64] = mbc_huc3_write_ram_bank_select;
v6 = (int (__fastcall **)(unsigned __int16, unsigned __int8))(unsigned int)((_DWORD)writemem + 768);
do
*v6++ = mbc_huc3_write_dummy;
while ( v4 != v6 );
v7 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v7 = cart[18];
v8 = v7 >> 8;
v9 = v8 + 160;
if ( (int)v8 + 160 > 160 )
{
v10 = (unsigned int)(v8 - 1) + 161LL;
v11 = 160LL;
do
*((_QWORD *)&readmem + v11++) = mbc_huc3_read_extram;
while ( v10 != v11 );
}
if ( v9 <= 191 )
{
v12 = v9;
do
*((_QWORD *)&readmem + v12++) = mbc_huc3_read_ff;
while ( (int)v12 <= 191 );
if ( v9 <= 160 )
goto LABEL_28;
}
v13 = 160LL;
do
writemem[v13++] = mbc_huc3_write_extram;
while ( v9 > (int)v13 );
if ( v9 <= 191 )
{
LABEL_28:
do
writemem[v9++] = mbc_huc3_write_dummy;
while ( v9 <= 191 );
}
v14 = cart[4];
cart[6] = v14;
cart[7] = v14 + 0x4000;
LODWORD(cart[1]) = 1;
HIDWORD(cart[1]) = 0;
result = cart[11];
cart[14] = result;
LOBYTE(cart[17]) = 0;
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_14;
case 1:
case 2:
case 3:
mbc_mbc1_install();
goto LABEL_14;
case 5:
case 6:
mbc_mbc2_install();
goto LABEL_14;
case 0xF:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
mbc_mbc3_install();
goto LABEL_14;
case 0x19:
case 0x1A:
case 0x1B:
case 0x1C:
case 0x1D:
case 0x1E:
mbc_mbc5_install();
goto LABEL_14;
case 0x22:
mbc_mbc7_install();
goto LABEL_14;
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_14:
result = printf("MBC reset: %02Xh %s\n", *(unsigned 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_14;
case 1:
case 2:
case 3:
mbc_mbc1_install();
goto LABEL_14;
case 5:
case 6:
mbc_mbc2_install();
goto LABEL_14;
case 0xF:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
mbc_mbc3_install();
goto LABEL_14;
case 0x19:
case 0x1A:
case 0x1B:
case 0x1C:
case 0x1D:
case 0x1E:
mbc_mbc5_install();
goto LABEL_14;
case 0x22:
mbc_mbc7_install();
goto LABEL_14;
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_14:
result = printf("MBC reset: %02Xh %s\n", *(unsigned 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;
}

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; // edx
__int64 result; // rax

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

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

LODWORD(cart[1]) = a2;
v2 = (a2 + (HIDWORD(cart[1]) << 8)) % SHIDWORD(cart[0]);
result = 1LL;
if ( !v2 )
v2 = 1;
cart[7] = cart[4] + ((__int64)v2 << 14);
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()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
void (**v2)(); // rax
__int64 (__fastcall **v3)(__int64, unsigned __int8); // rax
void (__fastcall **v4)(__int64, __int16); // rax
void (**v5)(); // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // eax
__int64 v9; // rcx
__int64 v10; // rdx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rax
__int64 result; // rax

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

canonical_decompiled_code:
__int64 func10()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
void (**v2)(); // rax
__int64 (__fastcall **v3)(__int64, unsigned __int8); // rax
void (__fastcall **v4)(__int64, __int16); // rax
void (**v5)(); // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
int v8; // eax
__int64 v9; // rcx
__int64 v10; // rdx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rax
__int64 result; // rax

cart[20] = mbc_mbc7_cleanup;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_mbc7_read_bank_0;
while ( v0 != (__int64 (__fastcall **)(unsigned __int16))(unsigned int)((_DWORD)&readmem + 512) );
v1 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 512);
do
*v1++ = mbc_mbc7_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_mbc7_write_ram_enable;
while ( v2 != (void (**)())(unsigned int)((_DWORD)&writemem + 256) );
v3 = (__int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)&writemem + 256);
do
*v3++ = mbc_mbc7_write_rom_bank_select;
while ( v3 != (__int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)&writemem + 512) );
v4 = (void (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 512);
do
*v4++ = mbc_mbc7_write_ram_bank_select;
while ( v4 != (void (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 768) );
v5 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v5++ = mbc_mbc7_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != v5 );
v6 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v6 = cart[18];
v7 = v6 >> 8;
v8 = v7 + 160;
if ( (int)v7 + 160 > 160 )
{
v9 = (unsigned int)(v7 - 1) + 161LL;
v10 = 160LL;
do
*((_QWORD *)&readmem + v10++) = mbc_mbc7_read_extram;
while ( v10 != v9 );
}
if ( v8 <= 191 )
{
v11 = v8;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc7_read_ff;
while ( (int)v11 <= 191 );
if ( v8 <= 160 )
goto LABEL_29;
}
v12 = 160LL;
do
*((_QWORD *)&writemem + v12++) = mbc_mbc7_write_extram;
while ( v8 > (int)v12 );
if ( v8 <= 191 )
{
LABEL_29:
do
*((_QWORD *)&writemem + v8++) = mbc_mbc7_write_dummy;
while ( v8 <= 191 );
}
v13 = cart[4];
cart[6] = v13;
cart[7] = v13 + 0x4000;
LODWORD(cart[1]) = 1;
HIDWORD(cart[1]) = 0;
result = cart[11];
cart[14] = result;
LOBYTE(cart[17]) = 0;
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[28] = -1;
state[29] = -1;
state[30] = -1;
state[31] = 32;
return result;
}

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

result = SDL_SetEventFilter(input_event_filter, 0LL);
state[28] = -1;
state[29] = -1;
state[30] = -1;
state[31] = 32;
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:
__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)
{
const char *v3; // r14
unsigned int v4; // er12
int v5; // ebp
int v6; // eax
unsigned __int8 v7; // bl

v3 = "__blob";
if ( argc > 0 )
v3 = argv[1];
printf("const unsigned char %s[] = {", v3);
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getchar();
v7 = v6;
if ( v6 == -1 )
break;
if ( v5 )
{
printf("0x%02x, ", (unsigned __int8)v6);
if ( ++v5 == 8 )
v5 = 0;
}
else
{
printf("\n/* %04x */ ", v4);
printf("0x%02x, ", v7);
v5 = 1;
}
++v4;
}
puts("\n};");
printf("const unsigned %s_len = 0x%x;\n", v3, v4);
return 0;
}

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)
{
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;
}

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


decompiled_code:
unsigned __int64 __fastcall do_strnlen_user(__int64 a1, unsigned __int64 a2, unsigned __int64 a3)
{
__int64 v3; // r13
unsigned __int64 v4; // r14
unsigned __int64 v5; // rbp
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // rax
unsigned __int64 result; // rax
__int64 v10; // rcx
__int64 v11; // rax
__int64 v12; // [rsp+8h] [rbp-40h] BYREF
__int64 v13[7]; // [rsp+10h] [rbp-38h] BYREF

v13[0] = 0x101010101010101LL;
v13[1] = 0x8080808080808080LL;
if ( a3 > a2 )
a3 = a2;
v3 = a1 & 7;
v4 = a1 & 0xFFFFFFFFFFFFFFF8LL;
v5 = v3 + a3;
v6 = *(_QWORD *)(a1 & 0xFFFFFFFFFFFFFFF8LL) | ((1LL << (8 * (unsigned __int8)v3)) - 1);
v7 = 0LL;
while ( 1 )
{
if ( has_zero(v6, &v12, v13) )
{
v8 = create_zero_mask(v12);
return v7 + find_zero(v8) - v3 + 1;
}
v10 = v7;
v11 = v7 + 8;
v7 += 8LL;
if ( v5 - v10 <= 7 )
break;
v6 = *(_QWORD *)(v4 + v11);
}
result = a2 + 1;
if ( v7 - v3 < a2 )
result = 0LL;
return result;
}

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:
unsigned __int64 __fastcall strnlen_user(__int64 a1, signed __int64 a2)
{
unsigned __int64 result; // rax

result = 0LL;
if ( a2 > 0 && a1 != -1 )
result = do_strnlen_user(a1, a2, ~a1);
return result;
}

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

result = 0LL;
if ( a2 > 0 && a1 != -1 )
result = func3(a1, a2, ~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; // edx
int v2; // eax
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; // edx
int v2; // eax
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; // eax
int v3; // edx
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
int v2; // eax
int v3; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 24);
v3 = *(_DWORD *)(a1 + 28);
if ( v2 >= v3 )
result = (v2 > a2) & (unsigned __int8)(v3 < a2);
else
result = (v3 < a2) | (unsigned __int8)(v2 > a2);
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; } *


tokenmsg

decompiled_code:
const char **__fastcall tokenmsg(char *src)
{
char *v1; // rbp
const char **v2; // rbx
char *v3; // r14
int v4; // er13
char *v5; // rsi
char *v6; // rsi
char *v7; // rsi
char *v9; // rsi
char *save_ptr[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (char *)malloc(0x100uLL);
v2 = (const char **)malloc(0x30uLL);
v3 = (char *)malloc(0x100uLL);
*v2 = v3;
v2[1] = (const char *)malloc(0x100uLL);
v2[2] = (const char *)malloc(0x100uLL);
v2[3] = (const char *)malloc(0x100uLL);
v2[4] = (const char *)malloc(0x100uLL);
v4 = interpret(src);
*((_DWORD *)v2 + 11) = v4;
strcpy(v3, src);
strcpy(v1, src);
if ( v4 == 3 || (v1 = strtok_r(v1, "&+-", save_ptr), *((_DWORD *)v2 + 11) != 4) )
{
v5 = strtok_r(v1, "`\n", save_ptr);
if ( !v5 )
{
puts("Error tokenizing ack!");
exit(1);
}
strcpy((char *)v2[1], v5);
*((_DWORD *)v2 + 10) = atoi(v2[1]);
v1 = 0LL;
}
v6 = strtok_r(v1, "`\n", save_ptr);
if ( !v6 )
{
puts("Error tokenizing src!");
exit(1);
}
strcpy((char *)v2[2], v6);
v7 = strtok_r(0LL, "`\n", save_ptr);
if ( !v7 )
{
puts("Error tokenizing dest!");
exit(1);
}
strcpy((char *)v2[3], v7);
if ( (unsigned int)(*((_DWORD *)v2 + 11) - 3) <= 1 )
{
v9 = strtok_r(0LL, "`\n", save_ptr);
if ( !v9 )
{
printf("[%s]\nError tokenizing payload!\n", *v2);
exit(1);
}
strcpy((char *)v2[4], v9);
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func7(char *src)
{
char *v1; // rbp
const char **v2; // rbx
char *v3; // r14
int v4; // er13
char *v5; // rsi
char *v6; // rsi
char *v7; // rsi
char *v9; // rsi
char *save_ptr[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (char *)malloc(0x100uLL);
v2 = (const char **)malloc(0x30uLL);
v3 = (char *)malloc(0x100uLL);
*v2 = v3;
v2[1] = (const char *)malloc(0x100uLL);
v2[2] = (const char *)malloc(0x100uLL);
v2[3] = (const char *)malloc(0x100uLL);
v2[4] = (const char *)malloc(0x100uLL);
v4 = func3(src);
*((_DWORD *)v2 + 11) = v4;
strcpy(v3, src);
strcpy(v1, src);
if ( v4 == 3 || (v1 = strtok_r(v1, "&+-", save_ptr), *((_DWORD *)v2 + 11) != 4) )
{
v5 = strtok_r(v1, "`\n", save_ptr);
if ( !v5 )
{
puts("Error tokenizing ack!");
exit(1);
}
strcpy((char *)v2[1], v5);
*((_DWORD *)v2 + 10) = atoi(v2[1]);
v1 = 0LL;
}
v6 = strtok_r(v1, "`\n", save_ptr);
if ( !v6 )
{
puts("Error tokenizing src!");
exit(1);
}
strcpy((char *)v2[2], v6);
v7 = strtok_r(0LL, "`\n", save_ptr);
if ( !v7 )
{
puts("Error tokenizing dest!");
exit(1);
}
strcpy((char *)v2[3], v7);
if ( (unsigned int)(*((_DWORD *)v2 + 11) - 3) <= 1 )
{
v9 = strtok_r(0LL, "`\n", save_ptr);
if ( !v9 )
{
printf("[%s]\nError tokenizing payload!\n", *v2);
exit(1);
}
strcpy((char *)v2[4], v9);
}
return v2;
}

original_code:
struct msgtok *tokenmsg(char *msg)
{
    char *tok, *saveptr;
    char *temp = malloc(256);
    struct msgtok *msgtok = malloc(sizeof(struct msgtok));
    msgtok->orig = malloc(256);
    msgtok->ack = malloc(256);
    msgtok->src = malloc(256);
    msgtok->dest = malloc(256);
    msgtok->pay = malloc(256);
    msgtok->type = interpret(msg);
    strcpy(msgtok->orig, msg);
    strcpy(temp, msg);
    if (msgtok->type != enumrealmsg) {
        tok = strtok_r(temp, "&+-", &saveptr);
        temp = tok;
    }
    if(msgtok->type != enumdvrmsg)
    {
 tok = strtok_r(temp, "`\n", &saveptr);
 if(tok == ((void *)0))
 {
  printf("Error tokenizing ack!\n");
  exit(1);
 }
 strcpy(msgtok->ack, tok);
 msgtok->acknum = atoi(msgtok->ack);
 temp = ((void *)0);
    }
    tok = strtok_r(temp, "`\n", &saveptr);
    if(tok == ((void *)0))
    {
        printf("Error tokenizing src!\n");
        exit(1);
    }
    strcpy(msgtok->src, tok);
    tok = strtok_r(((void *)0), "`\n", &saveptr);
    if(tok == ((void *)0))
    {
        printf("Error tokenizing dest!\n");
        exit(1);
    }
    strcpy(msgtok->dest, tok);
    if (msgtok->type == enumrealmsg || msgtok->type == enumdvrmsg)
    {
 tok = strtok_r(((void *)0), "`\n", &saveptr);
 if(tok == ((void *)0))
 {
     printf("[%s]\nError tokenizing payload!\n", msgtok->orig);
     exit(1);
 }
 else
     strcpy(msgtok->pay, tok);
    }
    return msgtok;
}

canonical_original_code:
struct msgtok *tokenmsg(char *msg)
{
    char *tok; char *saveptr;
    char *temp = malloc(256);
    struct msgtok *msgtok = malloc(sizeof(struct msgtok));
    msgtok->orig = malloc(256);
    msgtok->ack = malloc(256);
    msgtok->src = malloc(256);
    msgtok->dest = malloc(256);
    msgtok->pay = malloc(256);
    msgtok->type = interpret(msg);
    strcpy(msgtok->orig, msg);
    strcpy(temp, msg);
    if (msgtok->type != enumrealmsg) {
        tok = strtok_r(temp, "&+-", &saveptr);
        temp = tok;
    }
    if(msgtok->type != enumdvrmsg)
    {
 tok = strtok_r(temp, "`\n", &saveptr);
 if(tok == ((void *)0))
 {
  printf("Error tokenizing ack!\n");
  exit(1);
 }
 strcpy(msgtok->ack, tok);
 msgtok->acknum = atoi(msgtok->ack);
 temp = ((void *)0);
    }
    tok = strtok_r(temp, "`\n", &saveptr);
    if(tok == ((void *)0))
    {
        printf("Error tokenizing src!\n");
        exit(1);
    }
    strcpy(msgtok->src, tok);
    tok = strtok_r(((void *)0), "`\n", &saveptr);
    if(tok == ((void *)0))
    {
        printf("Error tokenizing dest!\n");
        exit(1);
    }
    strcpy(msgtok->dest, tok);
    if (msgtok->type == enumrealmsg || msgtok->type == enumdvrmsg)
    {
 tok = strtok_r(((void *)0), "`\n", &saveptr);
 if(tok == ((void *)0))
 {
     printf("[%s]\nError tokenizing payload!\n", msgtok->orig);
     exit(1);
 }
 else
     strcpy(msgtok->pay, tok);
    }
    return msgtok;
}

variable_types:
tok
char *

variable_types:
saveptr
char *

variable_types:
temp
char *

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


iamdest

decompiled_code:
_BOOL8 __fastcall iamdest(char *s2, __int64 a2)
{
return strcmp(*(const char **)(a2 + 24), s2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(char *s2, __int64 a2)
{
return strcmp(*(const char **)(a2 + 24), s2) == 0;
}

original_code:
int iamdest(char *name, struct msgtok *tok)
{
    if(strcmp(tok->dest, name) == 0)
        return 1;
    else
        return 0;
}

variable_types:
name
char *

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


sendbackack

decompiled_code:
int __fastcall sendbackack(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rax
__int64 v3; // rax
int result; // eax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

sprintf(v5, "+%d`%s`%s`", *(unsigned int *)(a1 + 40), *(const char **)(a1 + 24), *(const char **)(a1 + 16));
v1 = *(_QWORD *)(a1 + 16);
v2 = getnodefromname(*(_QWORD *)(a1 + 24));
v3 = getwindowlist(v2, v1);
if ( v3 )
result = enqueue(*(_QWORD *)(v3 + 16), v5);
else
result = fprintf(
stderr,
"sendbackack could not find window list %s-%s\n",
*(const char **)(a1 + 24),
*(const char **)(a1 + 16));
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rax
__int64 v3; // rax
int result; // eax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

sprintf(v5, "+%d`%s`%s`", *(unsigned int *)(a1 + 40), *(const char **)(a1 + 24), *(const char **)(a1 + 16));
v1 = *(_QWORD *)(a1 + 16);
v2 = getnodefromname(*(_QWORD *)(a1 + 24));
v3 = getwindowlist(v2, v1);
if ( v3 )
result = enqueue(*(_QWORD *)(v3 + 16), v5);
else
result = fprintf(
stderr,
"sendbackack could not find window list %s-%s\n",
*(const char **)(a1 + 24),
*(const char **)(a1 + 16));
return result;
}

original_code:
void sendbackack(struct msgtok *tok)
{
    char ackmsg[256];
    sprintf(ackmsg, "+%d`%s`%s`", tok->acknum, tok->dest, tok->src);
    struct windowlist* wl = getwindowlist(getnodefromname(tok->dest), tok->src);
    if(wl == ((void *)0)) {
        fprintf(stderr, "sendbackack could not find window list %s-%s\n", tok->dest, tok->src);
     return;
    }
    enqueue(wl->ackq, ackmsg);
    return;
}

canonical_original_code:
void sendbackack(struct msgtok *tok)
{
    char ackmsg[256];
    sprintf(ackmsg, "+%d`%s`%s`", tok->acknum, tok->dest, tok->src);
    struct windowlist *wl = getwindowlist(getnodefromname(tok->dest), tok->src);
    if(wl == ((void *)0)) {
        fprintf(stderr, "sendbackack could not find window list %s-%s\n", tok->dest, tok->src);
     return;
    }
    enqueue(wl->ackq, ackmsg);
    return;
}

variable_types:
ackmsg
char [256]

variable_types: struct windowlist { 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; } *sendq; struct window *ackq; char *name; struct windowlist *next; } *

user_defined_types: struct windowlist { struct window *sendq; struct window *ackq; char *name; struct windowlist *next; }


sendbacknack

decompiled_code:
int __fastcall sendbacknack(__int64 a1, unsigned int a2)
{
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // rax
int result; // eax
char v6[280]; // [rsp+0h] [rbp-118h] BYREF

sprintf(v6, "-%d`%s`%s`", a2, *(const char **)(a1 + 24), *(const char **)(a1 + 16));
v2 = *(_QWORD *)(a1 + 16);
v3 = getnodefromname(*(_QWORD *)(a1 + 24));
v4 = getwindowlist(v3, v2);
if ( v4 )
result = enqueue(*(_QWORD *)(v4 + 16), v6);
else
result = fprintf(
stderr,
"sendbacknack could not find window list %s-%s\n",
*(const char **)(a1 + 24),
*(const char **)(a1 + 16));
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, unsigned int a2)
{
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // rax
int result; // eax
char v6[280]; // [rsp+0h] [rbp-118h] BYREF

sprintf(v6, "-%d`%s`%s`", a2, *(const char **)(a1 + 24), *(const char **)(a1 + 16));
v2 = *(_QWORD *)(a1 + 16);
v3 = getnodefromname(*(_QWORD *)(a1 + 24));
v4 = getwindowlist(v3, v2);
if ( v4 )
result = enqueue(*(_QWORD *)(v4 + 16), v6);
else
result = fprintf(
stderr,
"sendbacknack could not find window list %s-%s\n",
*(const char **)(a1 + 24),
*(const char **)(a1 + 16));
return result;
}

original_code:
void sendbacknack(struct msgtok *tok, int out)
{
    char nackmsg[256];
    sprintf(nackmsg, "-%d`%s`%s`", out, tok->dest, tok->src);
    struct windowlist* wl = getwindowlist(getnodefromname(tok->dest), tok->src);
    if(wl == ((void *)0)) {
        fprintf(stderr, "sendbacknack could not find window list %s-%s\n", tok->dest, tok->src);
     return;
    }
    enqueue(wl->ackq, nackmsg);
    return;
}

canonical_original_code:
void sendbacknack(struct msgtok *tok, int out)
{
    char nackmsg[256];
    sprintf(nackmsg, "-%d`%s`%s`", out, tok->dest, tok->src);
    struct windowlist *wl = getwindowlist(getnodefromname(tok->dest), tok->src);
    if(wl == ((void *)0)) {
        fprintf(stderr, "sendbacknack could not find window list %s-%s\n", tok->dest, tok->src);
     return;
    }
    enqueue(wl->ackq, nackmsg);
    return;
}

variable_types: out int

variable_types:
nackmsg
char [256]


sendnacks

decompiled_code:
int __fastcall sendnacks(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
signed int i; // ebx
int result; // eax

v2 = *a1;
for ( i = *((_DWORD *)a1 + 6); v2; v2 = *(_QWORD *)(v2 + 32) )
{
do
{
if ( *(_DWORD *)(v2 + 16) == i )
break;
result = sendbacknack(a2, i++);
}
while ( *(_DWORD *)(a2 + 40) > i );
++i;
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
signed int i; // ebx
int result; // eax

v2 = *a1;
for ( i = *((_DWORD *)a1 + 6); v2; v2 = *(_QWORD *)(v2 + 32) )
{
do
{
if ( *(_DWORD *)(v2 + 16) == i )
break;
result = func11(a2, i++);
}
while ( *(_DWORD *)(a2 + 40) > i );
++i;
}
return result;
}

original_code:
void sendnacks(struct window *q, struct msgtok *tok)
{
    struct packet *el = q->head;
    int outack = q->first;
    while(el != ((void *)0))
    {
        while(el->acknum != outack)
        {
            sendbacknack(tok, outack);
            outack++;
            if(outack >= tok->acknum)
                break;
        }
        outack++;
        el = el->next;
    }
    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; } *

variable_types: outack int


receivemsg

decompiled_code:
__int64 __fastcall receivemsg(char *s2)
{
int v1; // eax
int v2; // eax
unsigned int v3; // ebx
const char **v4; // r12
int v5; // eax
__int64 v7; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-19Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-198h] BYREF
char buf[280]; // [rsp+90h] [rbp-118h] BYREF

v1 = getsockfromname();
v2 = recvfrom(v1, buf, 0xFFuLL, 0, &addr, &addr_len);
v3 = v2;
if ( v2 > 0 )
{
buf[v2] = 0;
v4 = tokenmsg(buf);
v5 = *((_DWORD *)v4 + 11);
switch ( v5 )
{
case 4:
v7 = getnodefromname(s2);
handledvrmessage(v7, v4);
break;
case 2:
handleack(s2, (__int64)v4);
break;
case 1:
handlenack(s2, (__int64)v4);
break;
default:
handlemsg(s2, v4);
break;
}
freetok(v4);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func16(char *s2)
{
int v1; // eax
int v2; // eax
unsigned int v3; // ebx
const char **v4; // r12
int v5; // eax
__int64 v7; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-19Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-198h] BYREF
char buf[280]; // [rsp+90h] [rbp-118h] BYREF

v1 = getsockfromname();
v2 = recvfrom(v1, buf, 0xFFuLL, 0, &addr, &addr_len);
v3 = v2;
if ( v2 > 0 )
{
buf[v2] = 0;
v4 = func7(buf);
v5 = *((_DWORD *)v4 + 11);
switch ( v5 )
{
case 4:
v7 = getnodefromname(s2);
handledvrmessage(v7, v4);
break;
case 2:
func13(s2, (__int64)v4);
break;
case 1:
func14(s2, (__int64)v4);
break;
default:
func15(s2, v4);
break;
}
func6(v4);
}
return v3;
}

original_code:
int receivemsg(char *name)
{
    char recvbuf[256];
    struct sockaddr_storage their;
    socklen_t addr_size;
    int sock = getsockfromname(name);
    int bytes = recvfrom(sock, recvbuf, 256 -1, 0,
                         (struct sockaddr*)&their, &addr_size);
    if(bytes <= 0)
        return bytes;
    recvbuf[bytes] = '\0';
    struct msgtok *tok = tokenmsg(recvbuf);
    if(tok->type == enumdvrmsg)
         handledvrmessage(getnodefromname(name), tok);
    else if(tok->type == enumpureack)
        handleack(name, tok);
    else if(tok->type == enumnack)
        handlenack(name, tok);
    else
        handlemsg(name, tok);
    freetok(tok);
    return bytes;
}

canonical_original_code:
int receivemsg(char *name)
{
    char recvbuf[256];
    struct sockaddr_storage their;
    unsigned int addr_size;
    int sock = getsockfromname(name);
    int bytes = recvfrom(sock, recvbuf, 256 -1, 0,
                         (struct sockaddr*)&their, &addr_size);
    if(bytes <= 0)
        return bytes;
    recvbuf[bytes] = '\0';
    struct msgtok *tok = tokenmsg(recvbuf);
    if(tok->type == enumdvrmsg)
         handledvrmessage(getnodefromname(name), tok);
    else if(tok->type == enumpureack)
        handleack(name, tok);
    else if(tok->type == enumnack)
        handlenack(name, tok);
    else
        handlemsg(name, tok);
    freetok(tok);
    return bytes;
}

variable_types:
recvbuf
char [256]

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types: sock int

variable_types: bytes int


decompiled_code:
void __fastcall set_interval(int a1)
{
dvr_interval = a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1)
{
dvr_interval = a1;
}

original_code:
void set_interval(int seconds)
{
    dvr_interval = seconds;
}

variable_types: seconds int


decompiled_code:
void __fastcall set_dvr_reset_interval(int a1)
{
dvr_reset_interval = a1;
}

canonical_decompiled_code:
void __fastcall func3(int a1)
{
dvr_reset_interval = a1;
}

original_code:
void set_dvr_reset_interval(int seconds)
{
    dvr_reset_interval = seconds;
}


decompiled_code:
_BOOL8 __fastcall is_neighbour(const char **a1)
{
return strcmp(a1[1], *a1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(const char **a1)
{
return strcmp(a1[1], *a1) == 0;
}

original_code:
int is_neighbour(struct routing_table_entry* rte)
{
    return (strcmp(rte->through, rte->name) == 0);
}

canonical_original_code:
int is_neighbour(struct routing_table_entry *rte)
{
    return (strcmp(rte->through, rte->name) == 0);
}

variable_types: struct routing_table_entry { char *name; char *through; float weight; int drop; int delay; struct routing_table_entry *next; } *


createlogdir

decompiled_code:
int createlogdir()
{
struct tm *v0; // rax
int result; // eax
char *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

*(_QWORD *)folderpath = 0LL;
*(_QWORD *)&folderpath[8] = 0LL;
*(_QWORD *)&folderpath[16] = 0LL;
*(_QWORD *)&folderpath[24] = 0LL;
*(_QWORD *)&folderpath[32] = 0LL;
*(_QWORD *)&folderpath[40] = 0LL;
*(_QWORD *)&folderpath[48] = 0LL;
*(_QWORD *)&folderpath[56] = 0LL;
time(&timer);
mkdir("logs", 0x1C0u);
v0 = localtime(&timer);
strftime(folderpath, 0x40uLL, "logs/%d-%m-%Y-%T", v0);
result = mkdir(folderpath, 0x1C0u);
if ( result )
{
v2 = strerror(result);
result = fprintf(stderr, "error creating directory: %s", v2);
}
return result;
}

canonical_decompiled_code:
int func0()
{
struct tm *v0; // rax
int result; // eax
char *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

*(_QWORD *)folderpath = 0LL;
*(_QWORD *)&folderpath[8] = 0LL;
*(_QWORD *)&folderpath[16] = 0LL;
*(_QWORD *)&folderpath[24] = 0LL;
*(_QWORD *)&folderpath[32] = 0LL;
*(_QWORD *)&folderpath[40] = 0LL;
*(_QWORD *)&folderpath[48] = 0LL;
*(_QWORD *)&folderpath[56] = 0LL;
time(&timer);
mkdir("logs", 0x1C0u);
v0 = localtime(&timer);
strftime(folderpath, 0x40uLL, "logs/%d-%m-%Y-%T", v0);
result = mkdir(folderpath, 0x1C0u);
if ( result )
{
v2 = strerror(result);
result = fprintf(stderr, "error creating directory: %s", v2);
}
return result;
}

original_code:
void createlogdir()
{
    int error;
    bzero(folderpath, 64);
    time_t curtime;
    time(&curtime);
    mkdir("logs", 0400 | 0100 | 0200);
    strftime(folderpath, 64, "logs/%d-%m-%Y-%T", localtime(&curtime));
    if ((error = mkdir(folderpath, 0400 | 0100 | 0200)) != 0)
    {
        fprintf(stderr, "error creating directory: %s", strerror(error));
    }
}

canonical_original_code:
void createlogdir()
{
    int error;
    bzero(folderpath, 64);
    long curtime;
    time(&curtime);
    mkdir("logs", 0400 | 0100 | 0200);
    strftime(folderpath, 64, "logs/%d-%m-%Y-%T", localtime(&curtime));
    if ((error = mkdir(folderpath, 0400 | 0100 | 0200)) != 0)
    {
        fprintf(stderr, "error creating directory: %s", strerror(error));
    }
}

variable_types: error int

variable_types: curtime long


original_code:
unsigned int chars_to_int(unsigned char *bytes)
{
 unsigned int *chars = (unsigned int *) bytes;
 return (*chars << 0x18) |
 (*chars << 0x08 & 0x00FF0000) |
 (*chars >> 0x08 & 0x0000FF00) |
 (*chars >> 0x18);
}

variable_types:
bytes
unsigned char *

variable_types:
chars
unsigned int *

return_type: unsigned int


original_code:
unsigned char *recalculate_crc(datachunk *chunk)
{
 int datasize = sizeof(char)*(4 + chunk->sizenum);
 int x;
 unsigned char *data = (unsigned char *)malloc(datasize);
 unsigned char *old_crc;
 for (x = 0; x < 4; x++)
  data[x] = chunk->type[x];
 for (x = 0; x < datasize - 4; x++)
  data[x+4] = chunk->body[x];
 old_crc = chunk->crc;
 chunk->crc = (unsigned char *)int_to_chars(chksum_crc32(data, datasize));
 free(old_crc);
 free(data);
 return chunk->crc;
}

canonical_original_code:
unsigned char *recalculate_crc(struct datachunk *chunk)
{
 int datasize = sizeof(char)*(4 + chunk->sizenum);
 int x;
 unsigned char *data = (unsigned char *)malloc(datasize);
 unsigned char *old_crc;
 for (x = 0; x < 4; x++)
  data[x] = chunk->type[x];
 for (x = 0; x < datasize - 4; x++)
  data[x+4] = chunk->body[x];
 old_crc = chunk->crc;
 chunk->crc = (unsigned char *)int_to_chars(chksum_crc32(data, datasize));
 free(old_crc);
 free(data);
 return chunk->crc;
}

variable_types: struct datachunk { unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc; unsigned int sizenum; unsigned int typenum; } *

variable_types: datasize int

variable_types: x int

variable_types:
data
unsigned char *

return_type: unsigned char *


decompiled_code:
int close_files()
{
int result; // eax

if ( fin )
result = fclose(fin);
if ( fout )
result = fclose(fout);
if ( ftext )
result = fclose(ftext);
fin = 0LL;
fout = 0LL;
ftext = 0LL;
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

if ( fin )
result = fclose(fin);
if ( fout )
result = fclose(fout);
if ( ftext )
result = fclose(ftext);
fin = 0LL;
fout = 0LL;
ftext = 0LL;
return result;
}

original_code:
void close_files()
{
 if(fin) fclose(fin);
 if(fout) fclose(fout);
 if(ftext) fclose(ftext);
 fin = ((void *)0);
 fout = ((void *)0);
 ftext = ((void *)0);
}


decompiled_code:
int __fastcall open_files(char *filename, char *a2, char *a3)
{
char *v4; // rax
FILE *v5; // rax
char *v6; // rdi
char *v7; // rdi

v4 = strstr(filename, ".png");
if ( v4 && strlen(v4) <= 4 )
{
v5 = fopen(filename, "r");
fin = v5;
if ( !v5 )
{
puts("ERROR\tInput file could not be opened!");
LODWORD(v5) = close_files();
}
}
else
{
puts("ERROR\tInput image file type must be .png!");
LODWORD(v5) = close_files();
}
if ( a2 )
{
v6 = strstr(a2, ".png");
if ( v6 && strlen(v6) <= 4 )
{
v5 = fopen(a2, "w");
fout = v5;
if ( !v5 )
{
puts("ERROR\tOutput file could not be opened!");
LODWORD(v5) = close_files();
}
}
else
{
puts("ERROR\tOutput image file type must be .png!");
LODWORD(v5) = close_files();
}
}
if ( a3 )
{
v7 = strstr(a3, ".txt");
if ( !v7 || strlen(v7) > 4 )
puts("WARNING\tText file may not be plaintext! Proceeding anyway...");
v5 = fopen(a3, "r");
ftext = v5;
if ( !v5 )
{
puts("ERROR\tText file could not be opened!");
LODWORD(v5) = close_files();
}
}
return (int)v5;
}

canonical_decompiled_code:
int __fastcall func3(char *filename, char *a2, char *a3)
{
char *v4; // rax
FILE *v5; // rax
char *v6; // rdi
char *v7; // rdi

v4 = strstr(filename, ".png");
if ( v4 && strlen(v4) <= 4 )
{
v5 = fopen(filename, "r");
fin = v5;
if ( !v5 )
{
puts("ERROR\tInput file could not be opened!");
LODWORD(v5) = func2();
}
}
else
{
puts("ERROR\tInput image file type must be .png!");
LODWORD(v5) = func2();
}
if ( a2 )
{
v6 = strstr(a2, ".png");
if ( v6 && strlen(v6) <= 4 )
{
v5 = fopen(a2, "w");
fout = v5;
if ( !v5 )
{
puts("ERROR\tOutput file could not be opened!");
LODWORD(v5) = func2();
}
}
else
{
puts("ERROR\tOutput image file type must be .png!");
LODWORD(v5) = func2();
}
}
if ( a3 )
{
v7 = strstr(a3, ".txt");
if ( !v7 || strlen(v7) > 4 )
puts("WARNING\tText file may not be plaintext! Proceeding anyway...");
v5 = fopen(a3, "r");
ftext = v5;
if ( !v5 )
{
puts("ERROR\tText file could not be opened!");
LODWORD(v5) = func2();
}
}
return (int)v5;
}

original_code:
void open_files(char *inname, char *outname, char* textname)
{
 int len = strlen(inname);
 char *intype;
 char *outtype;
 char *texttype;
 intype = strstr(inname, ".png");
 if (intype)
 {
  if (strlen(intype) > 4)
  {
   printf("ERROR\tInput image file type must be .png!\n");
   close_files();
  }
  else if((fin = fopen(inname, "r")) == ((void *)0))
  {
   printf("ERROR\tInput file could not be opened!\n");
   close_files();
  }
 }
 else
 {
  printf("ERROR\tInput image file type must be .png!\n");
  close_files();
 }
 if (outname)
 {
  outtype = strstr(outname, ".png");
  if (outtype)
  {
   if (strlen(outtype) > 4)
   {
    printf("ERROR\tOutput image file type must be .png!\n");
    close_files();
   }
   else if((fout = fopen(outname, "w")) == ((void *)0))
   {
    printf("ERROR\tOutput file could not be opened!\n");
    close_files();
   }
  }
  else
  {
   printf("ERROR\tOutput image file type must be .png!\n");
   close_files();
  }
 }
 if (textname)
 {
  texttype = strstr(textname, ".txt");
  if (texttype)
  {
   if (strlen(texttype) > 4)
    printf("WARNING\tText file may not be plaintext! Proceeding anyway...\n");
  }
  else
   printf("WARNING\tText file may not be plaintext! Proceeding anyway...\n");
  if((ftext = fopen(textname, "r")) == ((void *)0))
  {
   printf("ERROR\tText file could not be opened!\n");
   close_files();
  }
 }
}

canonical_original_code:
void open_files(char *inname, char *outname, char *textname)
{
 int len = strlen(inname);
 char *intype;
 char *outtype;
 char *texttype;
 intype = strstr(inname, ".png");
 if (intype)
 {
  if (strlen(intype) > 4)
  {
   printf("ERROR\tInput image file type must be .png!\n");
   close_files();
  }
  else if((fin = fopen(inname, "r")) == ((void *)0))
  {
   printf("ERROR\tInput file could not be opened!\n");
   close_files();
  }
 }
 else
 {
  printf("ERROR\tInput image file type must be .png!\n");
  close_files();
 }
 if (outname)
 {
  outtype = strstr(outname, ".png");
  if (outtype)
  {
   if (strlen(outtype) > 4)
   {
    printf("ERROR\tOutput image file type must be .png!\n");
    close_files();
   }
   else if((fout = fopen(outname, "w")) == ((void *)0))
   {
    printf("ERROR\tOutput file could not be opened!\n");
    close_files();
   }
  }
  else
  {
   printf("ERROR\tOutput image file type must be .png!\n");
   close_files();
  }
 }
 if (textname)
 {
  texttype = strstr(textname, ".txt");
  if (texttype)
  {
   if (strlen(texttype) > 4)
    printf("WARNING\tText file may not be plaintext! Proceeding anyway...\n");
  }
  else
   printf("WARNING\tText file may not be plaintext! Proceeding anyway...\n");
  if((ftext = fopen(textname, "r")) == ((void *)0))
  {
   printf("ERROR\tText file could not be opened!\n");
   close_files();
  }
 }
}

variable_types:
inname
char *

variable_types:
outname
char *

variable_types:
textname
char *

variable_types: len int

variable_types:
intype
char *

variable_types:
outtype
char *

variable_types:
texttype
char *


decompiled_code:
size_t __fastcall write_out(const void *a1, unsigned int a2)
{
return fwrite(a1, 1uLL, a2, fout);
}

canonical_decompiled_code:
size_t __fastcall func4(const void *a1, unsigned int a2)
{
return fwrite(a1, 1uLL, a2, fout);
}

original_code:
void write_out(unsigned char *data, unsigned int size)
{
 fwrite(data, sizeof(char), size, fout);
}

variable_types:
size
unsigned int


decompiled_code:
void *get_header()
{
void *v0; // rbx

v0 = malloc(8uLL);
fread(v0, 1uLL, 8uLL, fin);
return v0;
}

canonical_decompiled_code:
void *func5()
{
void *v0; // rbx

v0 = malloc(8uLL);
fread(v0, 1uLL, 8uLL, fin);
return v0;
}

original_code:
unsigned char *get_header()
{
 unsigned char *header;
 header = malloc(sizeof(char)*8);
 fread(header, sizeof(char), 8, fin);
 return header;
}

variable_types:
header
unsigned char *


decompiled_code:
_QWORD *process_chunk()
{
_DWORD *v0; // rbp
_DWORD *v1; // r12
void *v2; // r14
size_t v3; // rbx
_DWORD *v4; // r13
_QWORD *v5; // rbx

v0 = malloc(4uLL);
v1 = malloc(4uLL);
v2 = malloc(4uLL);
fread(v0, 1uLL, 4uLL, fin);
fread(v1, 1uLL, 4uLL, fin);
v3 = (unsigned int)chars_to_int(v0);
v4 = malloc(v3);
fread(v4, 1uLL, v3, fin);
fread(v2, 1uLL, 4uLL, fin);
v5 = malloc(0x28uLL);
*v5 = v0;
v5[1] = v1;
v5[2] = v4;
v5[3] = v2;
*((_DWORD *)v5 + 8) = chars_to_int(v0);
*((_DWORD *)v5 + 9) = chars_to_int(v1);
v5[3] = recalculate_crc((__int64)v5);
recalculate_crc((__int64)v5);
if ( *((_DWORD *)v5 + 9) == 1229472850 )
{
scanlen = chars_to_int(v4);
step = 3 * scanlen + 1;
}
return v5;
}

canonical_decompiled_code:
_QWORD *func6()
{
_DWORD *v0; // rbp
_DWORD *v1; // r12
void *v2; // r14
size_t v3; // rbx
_DWORD *v4; // r13
_QWORD *v5; // rbx

v0 = malloc(4uLL);
v1 = malloc(4uLL);
v2 = malloc(4uLL);
fread(v0, 1uLL, 4uLL, fin);
fread(v1, 1uLL, 4uLL, fin);
v3 = (unsigned int)func0(v0);
v4 = malloc(v3);
fread(v4, 1uLL, v3, fin);
fread(v2, 1uLL, 4uLL, fin);
v5 = malloc(0x28uLL);
*v5 = v0;
v5[1] = v1;
v5[2] = v4;
v5[3] = v2;
*((_DWORD *)v5 + 8) = func0(v0);
*((_DWORD *)v5 + 9) = func0(v1);
v5[3] = func1((__int64)v5);
func1((__int64)v5);
if ( *((_DWORD *)v5 + 9) == 1229472850 )
{
scanlen = func0(v4);
step = 3 * scanlen + 1;
}
return v5;
}

original_code:
datachunk *process_chunk()
{
 datachunk *chunk;
 unsigned char *size, *type, *body, *crc;
 unsigned int bodysize;
 size = malloc(sizeof(char)*4);
 type = malloc(sizeof(char)*4);
 body = malloc(sizeof(char)*4);
 crc = malloc(sizeof(char)*4);
 fread(size, sizeof(char), 4, fin);
 fread(type, sizeof(char), 4, fin);
 bodysize = chars_to_int(size);
 body = malloc(sizeof(char)*bodysize);
 fread(body, sizeof(char), bodysize, fin);
 fread(crc, sizeof(char), 4, fin);
 chunk = malloc(sizeof(char)*sizeof(datachunk));
 chunk->size = size;
 chunk->type = type;
 chunk->body = body;
 chunk->crc = crc;
 chunk->sizenum = chars_to_int(size);
 chunk->typenum = chars_to_int(type);
 chunk->crc = recalculate_crc(chunk);
 if(chars_to_int(crc) != chars_to_int(recalculate_crc(chunk)))
 {
 }
 if (chunk->typenum == 0x49484452)
 {
  scanlen = chars_to_int(body);
  step = (scanlen*3)+1;
 }
 return chunk;
}

canonical_original_code:
struct datachunk *process_chunk()
{
 struct datachunk *chunk;
 unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc;
 unsigned int bodysize;
 size = malloc(sizeof(char)*4);
 type = malloc(sizeof(char)*4);
 body = malloc(sizeof(char)*4);
 crc = malloc(sizeof(char)*4);
 fread(size, sizeof(char), 4, fin);
 fread(type, sizeof(char), 4, fin);
 bodysize = chars_to_int(size);
 body = malloc(sizeof(char)*bodysize);
 fread(body, sizeof(char), bodysize, fin);
 fread(crc, sizeof(char), 4, fin);
 chunk = malloc(sizeof(char)*sizeof(datachunk));
 chunk->size = size;
 chunk->type = type;
 chunk->body = body;
 chunk->crc = crc;
 chunk->sizenum = chars_to_int(size);
 chunk->typenum = chars_to_int(type);
 chunk->crc = recalculate_crc(chunk);
 if(chars_to_int(crc) != chars_to_int(recalculate_crc(chunk)))
 {
 }
 if (chunk->typenum == 0x49484452)
 {
  scanlen = chars_to_int(body);
  step = (scanlen*3)+1;
 }
 return chunk;
}

variable_types:
size
unsigned char *

variable_types:
type
unsigned char *

variable_types:
body
unsigned char *

variable_types:
crc
unsigned char *

variable_types:
bodysize
unsigned int

return_type: struct datachunk { unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc; unsigned int sizenum; unsigned int typenum; } *


collate

decompiled_code:
_QWORD *collate()
{
_QWORD *v0; // r12
void *v1; // r15
_QWORD *i; // rax
unsigned int v3; // ebx
_BYTE *v4; // rbp
_BYTE *v5; // r14
_BYTE *v6; // r13
_QWORD *j; // rdx
int v8; // ebx
unsigned int k; // eax
unsigned int v10; // ecx

v0 = malloc(0x28uLL);
v1 = malloc(4uLL);
get_header();
for ( i = process_chunk(); *((_DWORD *)i + 9) != 1229209940; i = process_chunk() )
;
v3 = 0;
do
{
v3 += *((_DWORD *)i + 8);
i = process_chunk();
}
while ( *((_DWORD *)i + 9) == 1229209940 );
v4 = malloc(v3);
v5 = int_to_chars(v3);
v6 = int_to_chars(1229209940);
rewind(fin);
get_header();
for ( j = process_chunk(); *((_DWORD *)j + 9) != 1229209940; j = process_chunk() )
;
v8 = 0;
do
{
v10 = *((_DWORD *)j + 8);
for ( k = 0; v10 > k; v10 = *((_DWORD *)j + 8) )
{
v4[v8 + k] = *(_BYTE *)(j[2] + k);
++k;
}
v8 += v10;
j = process_chunk();
}
while ( *((_DWORD *)j + 9) == 1229209940 );
blksize = (unsigned __int8)v4[3] + ((unsigned __int8)v4[4] << 8);
*v0 = v5;
v0[1] = v6;
v0[2] = v4;
v0[3] = v1;
v0[3] = recalculate_crc((__int64)v0);
*((_DWORD *)v0 + 8) = chars_to_int(v5);
*((_DWORD *)v0 + 9) = chars_to_int(v6);
rewind(fin);
return v0;
}

original_code:
datachunk *collate()
{
 datachunk *chunk = malloc(sizeof(char)*sizeof(datachunk));
 datachunk *collated = malloc(sizeof(char)*sizeof(datachunk));
 unsigned char *size, *type, *body, *crc;
 unsigned int fullsize = 0, currtype = 0, deposit = 0;
 unsigned int x;
 size = (unsigned char *)malloc(sizeof(char)*4);
 type = (unsigned char *)malloc(sizeof(char)*4);
 body = (unsigned char *)malloc(sizeof(char)*4);
 crc = (unsigned char *)malloc(sizeof(char)*4);
 get_header();
 chunk = process_chunk();
 currtype = chunk->typenum;
 while (currtype != 0x49444154)
 {
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 while (currtype == 0x49444154)
 {
  fullsize += chunk->sizenum;
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 body = malloc(sizeof(char)*fullsize);
 size = int_to_chars(fullsize);
 type = int_to_chars(0x49444154);
 rewind(fin);
 get_header();
 chunk = process_chunk();
 currtype = chunk->typenum;
 while (currtype != 0x49444154)
 {
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 while (currtype == 0x49444154)
 {
  for (x = 0; x < chunk->sizenum; x++)
   body[deposit+x] = chunk->body[x];
  deposit += chunk->sizenum;
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 blksize = 0x00000000;
 blksize = (body[4] << 8) + body[3];
 collated->size = size;
 collated->type = type;
 collated->body = body;
 collated->crc = crc;
 collated->crc = recalculate_crc(collated);
 collated->sizenum = chars_to_int(size);
 collated->typenum = chars_to_int(type);
 rewind(fin);
 return collated;
}

canonical_original_code:
struct datachunk *collate()
{
 struct datachunk *chunk = malloc(sizeof(char)*sizeof(datachunk));
 struct datachunk *collated = malloc(sizeof(char)*sizeof(datachunk));
 unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc;
 unsigned int fullsize = 0; unsigned int currtype = 0; unsigned int deposit = 0;
 unsigned int x;
 size = (unsigned char *)malloc(sizeof(char)*4);
 type = (unsigned char *)malloc(sizeof(char)*4);
 body = (unsigned char *)malloc(sizeof(char)*4);
 crc = (unsigned char *)malloc(sizeof(char)*4);
 get_header();
 chunk = process_chunk();
 currtype = chunk->typenum;
 while (currtype != 0x49444154)
 {
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 while (currtype == 0x49444154)
 {
  fullsize += chunk->sizenum;
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 body = malloc(sizeof(char)*fullsize);
 size = int_to_chars(fullsize);
 type = int_to_chars(0x49444154);
 rewind(fin);
 get_header();
 chunk = process_chunk();
 currtype = chunk->typenum;
 while (currtype != 0x49444154)
 {
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 while (currtype == 0x49444154)
 {
  for (x = 0; x < chunk->sizenum; x++)
   body[deposit+x] = chunk->body[x];
  deposit += chunk->sizenum;
  chunk = process_chunk();
  currtype = chunk->typenum;
 }
 blksize = 0x00000000;
 blksize = (body[4] << 8) + body[3];
 collated->size = size;
 collated->type = type;
 collated->body = body;
 collated->crc = crc;
 collated->crc = recalculate_crc(collated);
 collated->sizenum = chars_to_int(size);
 collated->typenum = chars_to_int(type);
 rewind(fin);
 return collated;
}

variable_types: struct datachunk { unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc; unsigned int sizenum; unsigned int typenum; } *

variable_types:
fullsize
unsigned int

variable_types:
currtype
unsigned int

variable_types:
deposit
unsigned int

variable_types:
x
unsigned int


decompiled_code:
__int64 __fastcall write_body(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
const void *v4; // rax

write_out(*(const void **)a1, 4u);
write_out(*(const void **)(a1 + 8), 4u);
if ( *(_DWORD *)(a1 + 36) == 1229209940 )
{
v2 = write_code(a1, a2);
write_out(*(const void **)(a1 + 16), *(_DWORD *)(a1 + 32));
v4 = (const void *)recalculate_crc(a1);
*(_QWORD *)(a1 + 24) = v4;
write_out(v4, 4u);
}
else
{
write_out(*(const void **)(a1 + 16), *(_DWORD *)(a1 + 32));
write_out(*(const void **)(a1 + 24), 4u);
v2 = 0;
}
return v2;
}

original_code:
int write_body(datachunk *chunk, char *msg)
{
 int success;
 write_out(chunk->size, 4);
 write_out(chunk->type, 4);
 if(chunk->typenum != 0x49444154)
 {
  write_out(chunk->body, chunk->sizenum);
  write_out(chunk->crc, 4);
  return 0;
 }
 success = write_code(chunk, msg);
 write_out(chunk->body, chunk->sizenum);
 chunk->crc = recalculate_crc(chunk);
 write_out(chunk->crc, 4);
 return success;
}

canonical_original_code:
int write_body(struct datachunk *chunk, char *msg)
{
 int success;
 write_out(chunk->size, 4);
 write_out(chunk->type, 4);
 if(chunk->typenum != 0x49444154)
 {
  write_out(chunk->body, chunk->sizenum);
  write_out(chunk->crc, 4);
  return 0;
 }
 success = write_code(chunk, msg);
 write_out(chunk->body, chunk->sizenum);
 chunk->crc = recalculate_crc(chunk);
 write_out(chunk->crc, 4);
 return success;
}

variable_types:
msg
char *

variable_types: success int

user_defined_types: struct datachunk { unsigned char *size; unsigned char *type; unsigned char *body; unsigned char *crc; unsigned int sizenum; unsigned int typenum; }


decompiled_code:
const char *encode_msg()
{
unsigned int v0; // ebx
const char *v1; // rbp
int v2; // ebx
char *v3; // rbx

v0 = 0;
while ( !feof(ftext) )
{
fgetc(ftext);
++v0;
}
v1 = (const char *)malloc(v0 + 1LL);
rewind(ftext);
v2 = 0;
while ( !feof(ftext) )
v1[v2++] = fgetc(ftext);
rewind(ftext);
v3 = (char *)&v1[v2 - 1];
*v3 = 0;
printf("%s\n\n", v1);
*v3 = -1;
if ( encrypt_flag )
v1 = (const char *)encrypt_text(v1);
return v1;
}

canonical_decompiled_code:
const char *func9()
{
unsigned int v0; // ebx
const char *v1; // rbp
int v2; // ebx
char *v3; // rbx

v0 = 0;
while ( !feof(ftext) )
{
fgetc(ftext);
++v0;
}
v1 = (const char *)malloc(v0 + 1LL);
rewind(ftext);
v2 = 0;
while ( !feof(ftext) )
v1[v2++] = fgetc(ftext);
rewind(ftext);
v3 = (char *)&v1[v2 - 1];
*v3 = 0;
printf("%s\n\n", v1);
*v3 = -1;
if ( encrypt_flag )
v1 = (const char *)encrypt_text(v1);
return v1;
}

original_code:
char *encode_msg()
{
 unsigned int len = 0;
 char *msg;
 while (!feof(ftext))
 {
  fgetc(ftext);
  len++;
 }
 msg = malloc(len*sizeof(char) + 1);
 rewind(ftext);
 len = 0;
 while (!feof(ftext))
  msg[len++] = fgetc(ftext);
 rewind(ftext);
 msg[len-1] = '\0';
 printf("%s\n\n", msg);
 msg[len-1] = (-1);
 if (!encrypt_flag)
  return msg;
 return encrypt_text(msg);
}

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall decode_msg(_BYTE *a1)
{
unsigned int v1; // edx
_BYTE *i; // rax

v1 = encrypt_flag;
if ( encrypt_flag )
return decrypt_text();
for ( i = a1; *i != 0xFF; i = &a1[v1] )
++v1;
*i = 0;
return (__int64)a1;
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1)
{
unsigned int v1; // edx
_BYTE *i; // rax

v1 = encrypt_flag;
if ( encrypt_flag )
return decrypt_text();
for ( i = a1; *i != 0xFF; i = &a1[v1] )
++v1;
*i = 0;
return (__int64)a1;
}

original_code:
char *decode_msg(char *msg)
{
 unsigned int i = 0;
 if (encrypt_flag)
  return decrypt_text(msg);
 while (msg[i] != (-1))
  i++;
 msg[i] = '\0';
 return msg;
}

variable_types:
i
unsigned int