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

result = (unsigned __int8)next_interrupt;
if ( last_unused_interrupt != next_interrupt )
{
++next_interrupt;
result = trigger_interrupt(interrupts[(unsigned __int8)result]);
}
return result;
}

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

result = (unsigned __int8)next_interrupt;
if ( last_unused_interrupt != next_interrupt )
{
++next_interrupt;
result = func1(interrupts[(unsigned __int8)result]);
}
return result;
}

original_code:
void trigger_next_queued_interrupt(void)
{
 if (last_unused_interrupt != next_interrupt)
  trigger_interrupt(interrupts[next_interrupt++]);
}

canonical_original_code:
void trigger_next_queued_interrupt()
{
 if (last_unused_interrupt != next_interrupt)
  trigger_interrupt(interrupts[next_interrupt++]);
}


hwq

decompiled_code:
__int64 __fastcall hwq(unsigned __int16 *a1)
{
write_register(0LL, *a1);
write_register(1LL, HIWORD(*(_DWORD *)a1));
write_register(2LL, a1[2]);
write_register(3LL, a1[4]);
return write_register(4LL, a1[5]);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 *a1)
{
func13(0LL, *a1);
func13(1LL, HIWORD(*(_DWORD *)a1));
func13(2LL, a1[2]);
func13(3LL, a1[4]);
return func13(4LL, a1[5]);
}

original_code:
void hwq(struct hw_builtin *hw)
{
 write_register(REG_A, hw->id & 0xFFFF);
 write_register(REG_B, hw->id >> 16);
 write_register(REG_C, hw->version);
 write_register(REG_X, hw->mfg & 0xFFFF);
 write_register(REG_Y, hw->mfg >> 16);
}

variable_types: struct hw_builtin { unsigned int id; unsigned short version; unsigned int mfg; void (*init)(); int (*interrupt)(); void (*step)(); } *

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rbx
char *v5; // rax
bool v6; // cf
bool v7; // zf
const char *v8; // rdi
__int64 v9; // rcx
char *v10; // rsi
char v12[56]; // [rsp+0h] [rbp-38h] BYREF

v3 = fcntl(0, 3, envp);
BYTE1(v3) |= 8u;
fcntl(0, 4, v3);
v4 = get_hw();
puts(*argv);
fflush(stdout);
puts("ready");
fflush(stdout);
while ( !feof(stdin) )
{
(*(void (**)(void))(v4 + 32))();
v5 = fgets(v12, 32, stdin);
v6 = 0;
v7 = v5 == 0LL;
if ( v5 )
{
v8 = "hwi\n";
v9 = 5LL;
v10 = v12;
do
{
if ( !v9 )
break;
v6 = (unsigned __int8)*v10 < (unsigned int)*v8;
v7 = *v10++ == *v8++;
--v9;
}
while ( v7 );
if ( (!v6 && !v7) == v6 )
{
(*(void (__fastcall **)(const char *, char *))(v4 + 24))(v8, v10);
}
else if ( !strcmp(v12, "hwq\n") )
{
hwq((unsigned __int16 *)v4);
}
puts("done");
fflush(stdout);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rbx
char *v5; // rax
bool v6; // cf
bool v7; // zf
const char *v8; // rdi
__int64 v9; // rcx
char *v10; // rsi
char v12[56]; // [rsp+0h] [rbp-38h] BYREF

v3 = fcntl(0, 3, envp);
BYTE1(v3) |= 8u;
fcntl(0, 4, v3);
v4 = func21();
puts(*argv);
fflush(stdout);
puts("ready");
fflush(stdout);
while ( !feof(stdin) )
{
(*(void (**)(void))(v4 + 32))();
v5 = fgets(v12, 32, stdin);
v6 = 0;
v7 = v5 == 0LL;
if ( v5 )
{
v8 = "hwi\n";
v9 = 5LL;
v10 = v12;
do
{
if ( !v9 )
break;
v6 = (unsigned __int8)*v10 < (unsigned int)*v8;
v7 = *v10++ == *v8++;
--v9;
}
while ( v7 );
if ( (!v6 && !v7) == v6 )
{
(*(void (__fastcall **)(const char *, char *))(v4 + 24))(v8, v10);
}
else if ( !strcmp(v12, "hwq\n") )
{
func7((unsigned __int16 *)v4);
}
puts("done");
fflush(stdout);
}
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 struct hw_builtin *hw;
 char input[32];
 char *status;
 fcntl(0, 4, fcntl(0, 3) | 04000);
 hw = get_hw();
 printf("%s\n", argv[0]);
 fflush(stdout);
 printf("ready\n");
 fflush(stdout);
 while (!feof(stdin)) {
  (*hw->step)();
  status = fgets(input, 32, stdin);
  if (((void *)0) != status) {
   if (strcmp(input, "hwi\n") == 0)
    (*hw->interrupt)();
   else if (strcmp(input, "hwq\n") == 0)
    hwq(hw);
   printf("done\n");
   fflush(stdout);
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
input
char [32]

variable_types:
status
char *

return_type: int

user_defined_types: struct hw_builtin { unsigned int id; unsigned short version; unsigned int mfg; void (*init)(); int (*interrupt)(); void (*step)(); }


decompiled_code:
int set_input_blocking()
{
unsigned int v0; // eax

v0 = fcntl(0, 3);
BYTE1(v0) &= 0xF7u;
return fcntl(0, 4, v0);
}

canonical_decompiled_code:
int func8()
{
unsigned int v0; // eax

v0 = fcntl(0, 3);
BYTE1(v0) &= 0xF7u;
return fcntl(0, 4, v0);
}

original_code:
void set_input_blocking(void)
{
 int fl = fcntl(0, 3);
 fcntl(0, 4, fl & ~04000);
}

canonical_original_code:
void set_input_blocking()
{
 int fl = fcntl(0, 3);
 fcntl(0, 4, fl & ~04000);
}

variable_types: fl int


decompiled_code:
int set_input_nonblocking()
{
unsigned int v0; // eax

v0 = fcntl(0, 3);
BYTE1(v0) |= 8u;
return fcntl(0, 4, v0);
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // eax

v0 = fcntl(0, 3);
BYTE1(v0) |= 8u;
return fcntl(0, 4, v0);
}

original_code:
void set_input_nonblocking(void)
{
 int fl = fcntl(0, 3);
 fcntl(0, 4, fl | 04000);
}

canonical_original_code:
void set_input_nonblocking()
{
 int fl = fcntl(0, 3);
 fcntl(0, 4, fl | 04000);
}


decompiled_code:
__int64 get_uint16()
{
unsigned __int16 v1; // [rsp+Eh] [rbp-2Ah]
char s[40]; // [rsp+10h] [rbp-28h] BYREF

set_input_blocking();
fgets(s, 32, stdin);
set_input_nonblocking();
__isoc99_sscanf();
return v1;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned __int16 v1; // [rsp+Eh] [rbp-2Ah]
char s[40]; // [rsp+10h] [rbp-28h] BYREF

func8();
fgets(s, 32, stdin);
func9();
__isoc99_sscanf();
return v1;
}

original_code:
uint16_t get_uint16(void)
{
 char buf[32];
 uint16_t ret;
 set_input_blocking();
 fgets(buf, 32, stdin);
 set_input_nonblocking();
 sscanf(buf, "%hu", &ret);
 return ret;
}

canonical_original_code:
unsigned short get_uint16()
{
 char buf[32];
 unsigned short ret;
 set_input_blocking();
 fgets(buf, 32, stdin);
 set_input_nonblocking();
 sscanf(buf, "%hu", &ret);
 return ret;
}

variable_types:
buf
char [32]

variable_types:
ret
unsigned short

return_type: unsigned short


decompiled_code:
void fd_init()
{
;
}

canonical_decompiled_code:
void func16()
{
;
}

original_code:
void fd_init(void)
{
}

canonical_original_code:
void fd_init()
{
}


decompiled_code:
__int64 __fastcall start_read(unsigned __int16 a1, __int16 a2)
{
if ( (unsigned __int16)(state - 1) > 1u )
return 0xFFFFFFFFLL;
disk_sector = a1;
target_track = a1 / 0x12u;
ram_addr = a2;
read_in_progress = 1;
state = 3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned __int16 a1, __int16 a2)
{
if ( (unsigned __int16)(state - 1) > 1u )
return 0xFFFFFFFFLL;
disk_sector = a1;
target_track = a1 / 0x12u;
ram_addr = a2;
read_in_progress = 1;
state = 3;
return 0LL;
}

original_code:
static int start_read(uint16_t read_sector, uint16_t addr)
{
 if ((state == STATE_READY) || (state == STATE_READY_WP)) {
  disk_sector = read_sector;
  target_track = read_sector / 18;
  ram_addr = addr;
  read_in_progress = 1;
  state = STATE_BUSY;
  return 0;
 } else {
  return -1;
 }
}

canonical_original_code:
static int start_read(unsigned short read_sector, unsigned short addr)
{
 if ((state == STATE_READY) || (state == STATE_READY_WP)) {
  disk_sector = read_sector;
  target_track = read_sector / 18;
  ram_addr = addr;
  read_in_progress = 1;
  state = STATE_BUSY;
  return 0;
 } else {
  return -1;
 }
}

variable_types:
addr
unsigned short


decompiled_code:
__int64 __fastcall start_write(unsigned __int16 a1, __int16 a2)
{
if ( state != 1 )
return 0xFFFFFFFFLL;
disk_sector = a1;
target_track = a1 / 0x12u;
ram_addr = a2;
write_in_progress = 1;
state = 3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int16 a1, __int16 a2)
{
if ( state != 1 )
return 0xFFFFFFFFLL;
disk_sector = a1;
target_track = a1 / 0x12u;
ram_addr = a2;
write_in_progress = 1;
state = 3;
return 0LL;
}

original_code:
static int start_write(uint16_t write_sector, uint16_t addr)
{
 if (state == STATE_READY) {
  disk_sector = write_sector;
  target_track = write_sector / 18;
  ram_addr = addr;
  write_in_progress = 1;
  state = STATE_BUSY;
  return 0;
 } else {
  return -1;
 }
}

canonical_original_code:
static int start_write(unsigned short write_sector, unsigned short addr)
{
 if (state == STATE_READY) {
  disk_sector = write_sector;
  target_track = write_sector / 18;
  ram_addr = addr;
  write_in_progress = 1;
  state = STATE_BUSY;
  return 0;
 } else {
  return -1;
 }
}


decompiled_code:
void fd_step()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
__int16 v2; // ax

v0 = track;
v1 = target_track;
if ( track == target_track )
{
if ( read_in_progress )
{
if ( reading )
{
if ( clock() - read_start > 16287 )
write_memory(ram_addr, 0x200u, (unsigned __int16 *)(disk + 2LL * (unsigned int)(disk_sector << 9)));
}
else
{
reading = 1;
read_start = clock();
}
}
else if ( write_in_progress )
{
if ( writing )
{
if ( clock() - write_start > 16287 )
read_memory(ram_addr, 0x200u, (_WORD *)(disk + 2LL * (unsigned int)(disk_sector << 9)));
}
else
{
writing = 1;
write_start = clock();
}
}
}
else if ( seeking )
{
if ( clock() - seek_start > 2400 )
{
v2 = v0 - 1;
if ( v0 < v1 )
v2 = v0 + 1;
track = v2;
seeking = 0;
}
}
else
{
seeking = 1;
seek_start = clock();
}
}

canonical_decompiled_code:
void func19()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
__int16 v2; // ax

v0 = track;
v1 = target_track;
if ( track == target_track )
{
if ( read_in_progress )
{
if ( reading )
{
if ( clock() - read_start > 16287 )
func14(ram_addr, 0x200u, (unsigned __int16 *)(disk + 2LL * (unsigned int)(disk_sector << 9)));
}
else
{
reading = 1;
read_start = clock();
}
}
else if ( write_in_progress )
{
if ( writing )
{
if ( clock() - write_start > 16287 )
func12(ram_addr, 0x200u, (_WORD *)(disk + 2LL * (unsigned int)(disk_sector << 9)));
}
else
{
writing = 1;
write_start = clock();
}
}
}
else if ( seeking )
{
if ( clock() - seek_start > 2400 )
{
v2 = v0 - 1;
if ( v0 < v1 )
v2 = v0 + 1;
track = v2;
seeking = 0;
}
}
else
{
seeking = 1;
seek_start = clock();
}
}

original_code:
void fd_step(void)
{
 if (track != target_track) {
  if (!seeking) {
   seeking = 1;
   seek_start = clock();
  } else {
   if (((clock() - seek_start) * 1000000 / ((__clock_t) 1000000)) > 2400) {
    if (target_track > track)
     track++;
    else
     track--;
    seeking = 0;
   }
  }
 } else if (read_in_progress) {
  if (!reading) {
   reading = 1;
   read_start = clock();
  } else {
   if (((clock() - read_start) * 1000000 / ((__clock_t) 1000000)) > 16287)
    write_memory(ram_addr, 512, &disk[disk_sector * 512]);
  }
 } else if (write_in_progress) {
  if (!writing) {
   writing = 1;
   write_start = clock();
  } else {
   if (((clock() - write_start) * 1000000 / ((__clock_t) 1000000)) > 16287)
    read_memory(ram_addr, 512, &disk[disk_sector * 512]);
  }
 }
}

canonical_original_code:
void fd_step()
{
 if (track != target_track) {
  if (!seeking) {
   seeking = 1;
   seek_start = clock();
  } else {
   if (((clock() - seek_start) * 1000000 / ((long) 1000000)) > 2400) {
    if (target_track > track)
     track++;
    else
     track--;
    seeking = 0;
   }
  }
 } else if (read_in_progress) {
  if (!reading) {
   reading = 1;
   read_start = clock();
  } else {
   if (((clock() - read_start) * 1000000 / ((long) 1000000)) > 16287)
    write_memory(ram_addr, 512, &disk[disk_sector * 512]);
  }
 } else if (write_in_progress) {
  if (!writing) {
   writing = 1;
   write_start = clock();
  } else {
   if (((clock() - write_start) * 1000000 / ((long) 1000000)) > 16287)
    read_memory(ram_addr, 512, &disk[disk_sector * 512]);
  }
 }
}


decompiled_code:
__int64 fd_interrupt()
{
__int16 v0; // ax
__int16 v2; // bx
unsigned __int16 v3; // ax
__int16 v4; // bx
unsigned __int16 v5; // ax

read_register(3u);
v0 = read_register(0);
switch ( v0 )
{
case 2:
v2 = read_register(4u);
v3 = read_register(3u);
if ( (unsigned int)start_read(v3, v2) )
goto LABEL_10;
goto LABEL_7;
case 3:
v4 = read_register(4u);
v5 = read_register(3u);
if ( (unsigned int)start_write(v5, v4) )
{
LABEL_10:
write_register(1u, 0);
return 0LL;
}
LABEL_7:
write_register(1u, 1u);
return 0LL;
case 0:
write_register(1u, state);
write_register(2u, 0);
break;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func20()
{
__int16 v0; // ax
__int16 v2; // bx
unsigned __int16 v3; // ax
__int16 v4; // bx
unsigned __int16 v5; // ax

func11(3u);
v0 = func11(0);
switch ( v0 )
{
case 2:
v2 = func11(4u);
v3 = func11(3u);
if ( (unsigned int)func17(v3, v2) )
goto LABEL_10;
goto LABEL_7;
case 3:
v4 = func11(4u);
v5 = func11(3u);
if ( (unsigned int)func18(v5, v4) )
{
LABEL_10:
func13(1u, 0);
return 0LL;
}
LABEL_7:
func13(1u, 1u);
return 0LL;
case 0:
func13(1u, state);
func13(2u, 0);
break;
}
return 0LL;
}

original_code:
int fd_interrupt(void)
{
 uint16_t x = read_register(REG_X);
 switch (read_register(REG_A)) {
 case 0:
  write_register(REG_B, state);
  write_register(REG_C, error);
  break;
 case 1:
  if (x) {
   int_enabled = 1;
   int_msg = x;
  } else {
   int_enabled = 0;
  }
  break;
 case 2:
  if (start_read(read_register(REG_X), read_register(REG_Y)) == 0)
   write_register(REG_B, 1);
  else
   write_register(REG_B, 0);
  break;
 case 3:
  if (start_write(read_register(REG_X), read_register(REG_Y)) == 0)
   write_register(REG_B, 1);
  else
   write_register(REG_B, 0);
  break;
 default:
  break;
 }
 return 0;
}

canonical_original_code:
int fd_interrupt()
{
 unsigned short x = read_register(REG_X);
 switch (read_register(REG_A)) {
 case 0:
  write_register(REG_B, state);
  write_register(REG_C, error);
  break;
 case 1:
  if (x) {
   int_enabled = 1;
   int_msg = x;
  } else {
   int_enabled = 0;
  }
  break;
 case 2:
  if (start_read(read_register(REG_X), read_register(REG_Y)) == 0)
   write_register(REG_B, 1);
  else
   write_register(REG_B, 0);
  break;
 case 3:
  if (start_write(read_register(REG_X), read_register(REG_Y)) == 0)
   write_register(REG_B, 1);
  else
   write_register(REG_B, 0);
  break;
 default:
  break;
 }
 return 0;
}

variable_types:
x
unsigned short


decompiled_code:
__int64 __fastcall read_register(unsigned __int16 a1)
{
printf("reg r %hu\n", a1);
fflush(stdout);
return get_uint16();
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int16 a1)
{
printf("reg r %hu\n", a1);
fflush(stdout);
return func10();
}

original_code:
uint16_t read_register(uint16_t reg)
{
 printf("reg r %hu\n", reg);
 fflush(stdout);
 return get_uint16();
}

canonical_original_code:
unsigned short read_register(unsigned short reg)
{
 printf("reg r %hu\n", reg);
 fflush(stdout);
 return get_uint16();
}

variable_types:
reg
unsigned short


decompiled_code:
int __fastcall read_memory(unsigned __int16 a1, unsigned __int16 a2, _WORD *a3)
{
int result; // eax
_WORD *v5; // rbx

printf("mem r %hu %hu\n", a1, a2);
result = fflush(stdout);
if ( a2 )
{
v5 = a3;
do
{
result = get_uint16();
*v5++ = result;
}
while ( v5 != &a3[a2] );
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(unsigned __int16 a1, unsigned __int16 a2, _WORD *a3)
{
int result; // eax
_WORD *v5; // rbx

printf("mem r %hu %hu\n", a1, a2);
result = fflush(stdout);
if ( a2 )
{
v5 = a3;
do
{
result = func10();
*v5++ = result;
}
while ( v5 != &a3[a2] );
}
return result;
}

original_code:
void read_memory(uint16_t addr, uint16_t len, uint16_t *buf)
{
 int i;
 printf("mem r %hu %hu\n", addr, len);
 fflush(stdout);
 for (i = 0; i < len; i++)
  buf[i] = get_uint16();
}

canonical_original_code:
void read_memory(unsigned short addr, unsigned short len, unsigned short *buf)
{
 int i;
 printf("mem r %hu %hu\n", addr, len);
 fflush(stdout);
 for (i = 0; i < len; i++)
  buf[i] = get_uint16();
}

variable_types:
len
unsigned short

variable_types:
buf
unsigned short *

variable_types: i int


decompiled_code:
int __fastcall write_register(unsigned __int16 a1, unsigned __int16 a2)
{
printf("reg w %hu %hu\n", a1, a2);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func13(unsigned __int16 a1, unsigned __int16 a2)
{
printf("reg w %hu %hu\n", a1, a2);
return fflush(stdout);
}

original_code:
void write_register(uint16_t reg, uint16_t val)
{
 printf("reg w %hu %hu\n", reg, val);
 fflush(stdout);
}

canonical_original_code:
void write_register(unsigned short reg, unsigned short val)
{
 printf("reg w %hu %hu\n", reg, val);
 fflush(stdout);
}

variable_types:
val
unsigned short


decompiled_code:
int __fastcall write_memory(unsigned __int16 a1, unsigned __int16 a2, unsigned __int16 *a3)
{
unsigned __int16 *v4; // rbx

printf("mem w %hu %hu", a1, a2);
fflush(stdout);
if ( a2 )
{
v4 = a3;
do
{
printf(" %hu", *v4);
fflush(stdout);
++v4;
}
while ( v4 != &a3[a2] );
}
putchar(10);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func14(unsigned __int16 a1, unsigned __int16 a2, unsigned __int16 *a3)
{
unsigned __int16 *v4; // rbx

printf("mem w %hu %hu", a1, a2);
fflush(stdout);
if ( a2 )
{
v4 = a3;
do
{
printf(" %hu", *v4);
fflush(stdout);
++v4;
}
while ( v4 != &a3[a2] );
}
putchar(10);
return fflush(stdout);
}

original_code:
void write_memory(uint16_t addr, uint16_t len, uint16_t *buf)
{
 int i;
 printf("mem w %hu %hu", addr, len);
 fflush(stdout);
 for (i = 0; i < len; i++) {
  printf(" %hu", buf[i]);
  fflush(stdout);
 }
 printf("\n");
 fflush(stdout);
}

canonical_original_code:
void write_memory(unsigned short addr, unsigned short len, unsigned short *buf)
{
 int i;
 printf("mem w %hu %hu", addr, len);
 fflush(stdout);
 for (i = 0; i < len; i++) {
  printf(" %hu", buf[i]);
  fflush(stdout);
 }
 printf("\n");
 fflush(stdout);
}


decompiled_code:
int __fastcall raise_interrupt(unsigned __int16 a1)
{
printf("int %hu\n", a1);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func15(unsigned __int16 a1)
{
printf("int %hu\n", a1);
return fflush(stdout);
}

original_code:
void raise_interrupt(uint16_t msg)
{
 printf("int %hu\n", msg);
 fflush(stdout);
}

canonical_original_code:
void raise_interrupt(unsigned short msg)
{
 printf("int %hu\n", msg);
 fflush(stdout);
}

variable_types:
msg
unsigned short


decompiled_code:
__m128i *get_hw()
{
return m35fd(0LL);
}

original_code:
struct hw_builtin *get_hw(void)
{
 return m35fd(((void *)0));
}

canonical_original_code:
struct hw_builtin *get_hw()
{
 return m35fd(((void *)0));
}

return_type: struct hw_builtin { unsigned int id; unsigned short version; unsigned int mfg; void (*init)(); int (*interrupt)(); void (*step)(); } *


repo: jkarni/extendp

decompiled_code:
void __fastcall insert_substring(char *dest, char *src, int a3)
{
int v4; // ebp
char *v5; // r13
char *v6; // rbp

v4 = strlen(dest);
v5 = (char *)substring((__int64)dest, 1, a3 - 1);
v6 = (char *)substring((__int64)dest, a3, v4 - a3 + 1);
*dest = 0;
strcat(dest, v5);
free(v5);
strcat(dest, src);
strcat(dest, v6);
free(v6);
}

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

v4 = strlen(dest);
v5 = (char *)func0((__int64)dest, 1, a3 - 1);
v6 = (char *)func0((__int64)dest, a3, v4 - a3 + 1);
*dest = 0;
strcat(dest, v5);
free(v5);
strcat(dest, src);
strcat(dest, v6);
free(v6);
}

original_code:
void insert_substring(char *a, char *b, int position)
{
   char *f, *e;
   int length;
   length = strlen(a);
   f = substring(a, 1, position - 1 );
   e = substring(a, position, length-position+1);
   strcpy(a, "");
   strcat(a, f);
   free(f);
   strcat(a, b);
   strcat(a, e);
   free(e);
}

canonical_original_code:
void insert_substring(char *a, char *b, int position)
{
   char *f; char *e;
   int length;
   length = strlen(a);
   f = substring(a, 1, position - 1 );
   e = substring(a, position, length-position+1);
   strcpy(a, "");
   strcat(a, f);
   free(f);
   strcat(a, b);
   strcat(a, e);
   free(e);
}

variable_types:
a
char *

variable_types:
b
char *

variable_types: position int

variable_types:
f
char *

variable_types:
e
char *

variable_types: length int


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

insert_substring(a1, a2, offset + a3);
result = strlen(a2);
offset += result;
return result;
}

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

func1(a1, a2, offset + a3);
result = strlen(a2);
offset += result;
return result;
}

original_code:
void insert_substring_off(char *a, char *b, int position) {
   extern int offset;
   insert_substring(a, b, (position + offset));
   offset += strlen(b);
}

variable_types: offset int


xmalloc

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

result = malloc(a1);
if ( !result )
{
perror("Error: malloc");
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
perror("Error: malloc");
exit(1);
}
return result;
}

original_code:
void *xmalloc(size_t size)
{
    void *p = malloc(size);
    if (p) return p;
    perror("Error: malloc");
    exit(1);
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
    void *p = malloc(size);
    if (p) return p;
    perror("Error: malloc");
    exit(1);
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


xrealloc

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

result = realloc(a1, a2);
if ( !result )
{
perror("Error: realloc");
exit(1);
}
return result;
}

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

result = realloc(a1, a2);
if ( !result )
{
perror("Error: realloc");
exit(1);
}
return result;
}

original_code:
void *xrealloc(void *p, size_t size)
{
    p = realloc(p, size);
    if (p) return p;
    perror("Error: realloc");
    exit(1);
}

canonical_original_code:
void *xrealloc(void *p, unsigned long size)
{
    p = realloc(p, size);
    if (p) return p;
    perror("Error: realloc");
    exit(1);
}


repo: jkbenaim/cboy

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

if ( a1 > 7u )
{
result = 80LL;
if ( a1 != 83 )
{
if ( a1 == 84 )
{
result = 96LL;
}
else
{
result = 2LL;
if ( a1 == 82 )
result = 72LL;
}
}
}
else
{
result = 2LL;
if ( a1 )
{
switch ( a1 )
{
case 1u:
result = 4LL;
break;
case 2u:
result = 8LL;
break;
case 3u:
result = 16LL;
break;
case 4u:
result = 32LL;
break;
case 5u:
result = 64LL;
break;
case 6u:
result = 128LL;
break;
case 7u:
result = 256LL;
break;
default:
result = 2LL;
break;
}
}
}
return result;
}

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

if ( a1 > 7u )
{
result = 80LL;
if ( a1 != 83 )
{
if ( a1 == 84 )
{
result = 96LL;
}
else
{
result = 2LL;
if ( a1 == 82 )
result = 72LL;
}
}
}
else
{
result = 2LL;
if ( a1 )
{
switch ( a1 )
{
case 1u:
result = 4LL;
break;
case 2u:
result = 8LL;
break;
case 3u:
result = 16LL;
break;
case 4u:
result = 32LL;
break;
case 5u:
result = 64LL;
break;
case 6u:
result = 128LL;
break;
case 7u:
result = 256LL;
break;
default:
result = 2LL;
break;
}
}
}
return result;
}

original_code:
int mbc_romSize2numBanks(uint8_t romSize)
{
 switch (romSize) {
 case 0x00:
  return 2;
  break;
 case 0x01:
  return 4;
  break;
 case 0x02:
  return 8;
  break;
 case 0x03:
  return 16;
  break;
 case 0x04:
  return 32;
  break;
 case 0x05:
  return 64;
  break;
 case 0x06:
  return 128;
  break;
 case 0x07:
  return 256;
  break;
 case 0x52:
  return 72;
  break;
 case 0x53:
  return 80;
  break;
 case 0x54:
  return 96;
  break;
 default:
  return 2;
  break;
 }
}

canonical_original_code:
int mbc_romSize2numBanks(unsigned char romSize)
{
 switch (romSize) {
 case 0x00:
  return 2;
  break;
 case 0x01:
  return 4;
  break;
 case 0x02:
  return 8;
  break;
 case 0x03:
  return 16;
  break;
 case 0x04:
  return 32;
  break;
 case 0x05:
  return 64;
  break;
 case 0x06:
  return 128;
  break;
 case 0x07:
  return 256;
  break;
 case 0x52:
  return 72;
  break;
 case 0x53:
  return 80;
  break;
 case 0x54:
  return 96;
  break;
 default:
  return 2;
  break;
 }
}

variable_types:
romSize
unsigned char


decompiled_code:
__int64 __fastcall mbc_ramSize2numBytes(char a1)
{
__int64 result; // rax

switch ( a1 )
{
case 1:
result = 2048LL;
break;
case 2:
result = 0x2000LL;
break;
case 3:
result = 0x8000LL;
break;
case 4:
result = 0x20000LL;
break;
case 5:
result = 0x10000LL;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 1:
result = 2048LL;
break;
case 2:
result = 0x2000LL;
break;
case 3:
result = 0x8000LL;
break;
case 4:
result = 0x20000LL;
break;
case 5:
result = 0x10000LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int mbc_ramSize2numBytes(uint8_t ramSize)
{
 switch (ramSize) {
 case 0x00:
  return 0;
  break;
 case 0x01:
  return 2048;
  break;
 case 0x02:
  return 8192;
  break;
 case 0x03:
  return 32768;
  break;
 case 0x04:
  return 131072;
  break;
 case 0x05:
  return 65536;
  break;
 default:
  return 0;
  break;
 }
}

canonical_original_code:
int mbc_ramSize2numBytes(unsigned char ramSize)
{
 switch (ramSize) {
 case 0x00:
  return 0;
  break;
 case 0x01:
  return 2048;
  break;
 case 0x02:
  return 8192;
  break;
 case 0x03:
  return 32768;
  break;
 case 0x04:
  return 131072;
  break;
 case 0x05:
  return 65536;
  break;
 default:
  return 0;
  break;
 }
}

variable_types:
ramSize
unsigned char


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

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

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

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

variable_types:
address
unsigned short

return_type: unsigned char


decompiled_code:
void mbc_none_write_dummy()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

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

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

variable_types:
data
unsigned char


decompiled_code:
__int64 __fastcall mbc_none_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_none_read_bank_0(uint16_t address)
{
 return cart.cartrom_bank_zero[address];
}

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


decompiled_code:
__int64 __fastcall mbc_none_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_none_read_bank_n(uint16_t address)
{
 return cart.cartrom_bank_n[address & 0x3fff];
}

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


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

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

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

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


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

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

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

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

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

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


decompiled_code:
void mbc_none_cleanup()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void mbc_none_cleanup()
{
}

canonical_original_code:
void mbc_none_cleanup()
{
}


decompiled_code:
__int64 mbc_none_install()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
void (**v2)(); // rax
unsigned __int64 v3; // rdx
unsigned __int64 v4; // rdx
int v5; // eax
__int64 v6; // rcx
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rax
__int64 result; // rax

cart[20] = mbc_none_cleanup;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_none_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_none_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_none_write_dummy;
while ( v2 != (void (**)())(unsigned int)((_DWORD)&writemem + 1024) );
v3 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v3 = cart[18];
v4 = v3 >> 8;
v5 = v4 + 160;
if ( (int)v4 + 160 > 160 )
{
v6 = (unsigned int)(v4 - 1) + 161LL;
v7 = 160LL;
do
*((_QWORD *)&readmem + v7++) = mbc_none_read_extram;
while ( v7 != v6 );
}
if ( v5 <= 191 )
{
v8 = v5;
do
*((_QWORD *)&readmem + v8++) = mbc_none_read_ff;
while ( (int)v8 <= 191 );
if ( v5 <= 160 )
goto LABEL_23;
}
v9 = 160LL;
do
*((_QWORD *)&writemem + v9++) = mbc_none_write_extram;
while ( v5 > (int)v9 );
if ( v5 <= 191 )
{
LABEL_23:
do
*((_QWORD *)&writemem + v5++) = mbc_none_write_dummy;
while ( v5 <= 191 );
}
v10 = cart[4];
cart[6] = v10;
result = v10 + 0x4000;
cart[7] = result;
return result;
}

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

cart[20] = mbc_none_cleanup;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_none_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_none_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_none_write_dummy;
while ( v2 != (void (**)())(unsigned int)((_DWORD)&writemem + 1024) );
v3 = 0x2000LL;
if ( cart[18] <= 0x2000uLL )
v3 = cart[18];
v4 = v3 >> 8;
v5 = v4 + 160;
if ( (int)v4 + 160 > 160 )
{
v6 = (unsigned int)(v4 - 1) + 161LL;
v7 = 160LL;
do
*((_QWORD *)&readmem + v7++) = mbc_none_read_extram;
while ( v7 != v6 );
}
if ( v5 <= 191 )
{
v8 = v5;
do
*((_QWORD *)&readmem + v8++) = mbc_none_read_ff;
while ( (int)v8 <= 191 );
if ( v5 <= 160 )
goto LABEL_23;
}
v9 = 160LL;
do
*((_QWORD *)&writemem + v9++) = mbc_none_write_extram;
while ( v5 > (int)v9 );
if ( v5 <= 191 )
{
LABEL_23:
do
*((_QWORD *)&writemem + v5++) = mbc_none_write_dummy;
while ( v5 <= 191 );
}
v10 = cart[4];
cart[6] = v10;
result = v10 + 0x4000;
cart[7] = result;
return result;
}

original_code:
void mbc_none_install()
{
 cart.cleanup = &mbc_none_cleanup;
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_none_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_none_read_bank_n;
 }
 for (i = 0x00; i <= 0x7F; ++i) {
  writemem[i] = mbc_none_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_none_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_none_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_none_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_none_write_dummy;
 }
 cart.cartrom_bank_zero = cart.cartrom;
 cart.cartrom_bank_n = cart.cartrom + 0x4000;
}


decompiled_code:
_BOOL8 pqueue_init()
{
return calloc(1uLL, 8uLL) == 0LL;
}

canonical_decompiled_code:
_BOOL8 func0()
{
return calloc(1uLL, 8uLL) == 0LL;
}

original_code:
int pqueue_init(pqueue_root * root)
{
 pqueue_root *newRoot = calloc(1, sizeof(pqueue_root));
 if (newRoot) {
  root = newRoot;
  return 0;
 } else {
  return 1;
 }
}

canonical_original_code:
int pqueue_init(struct pqueue_root_s *root)
{
 struct pqueue_root_s *newRoot = calloc(1, sizeof(pqueue_root));
 if (newRoot) {
  root = newRoot;
  return 0;
 } else {
  return 1;
 }
}

variable_types: struct pqueue_root_s { struct pqueue_node_s { struct pqueue_node_s *next; int priority; int message; } *first; } *

user_defined_types: struct pqueue_root_s { struct pqueue_node_s *first; }


decompiled_code:
_BOOL8 __fastcall pqueue_isEmpty(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

original_code:
int pqueue_isEmpty(pqueue_root * root)
{
 return root == ((void *)0) || root->first == ((void *)0);
}

canonical_original_code:
int pqueue_isEmpty(struct pqueue_root_s *root)
{
 return root == ((void *)0) || root->first == ((void *)0);
}

user_defined_types: struct pqueue_node_s { struct pqueue_node_s *next; int priority; int message; }


decompiled_code:
__int64 __fastcall pqueue_peek(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = pqueue_isEmpty(a1);
result = 0LL;
if ( !v1 )
result = *(unsigned int *)(*a1 + 12LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = func1(a1);
result = 0LL;
if ( !v1 )
result = *(unsigned int *)(*a1 + 12LL);
return result;
}

original_code:
int pqueue_peek(pqueue_root * root)
{
 if (pqueue_isEmpty(root))
  return 0;
 else
  return root->first->message;
}

canonical_original_code:
int pqueue_peek(struct pqueue_root_s *root)
{
 if (pqueue_isEmpty(root))
  return 0;
 else
  return root->first->message;
}


decompiled_code:
__int64 __fastcall pqueue_pop(__int64 **a1)
{
unsigned int v1; // ebp
int v2; // edx
__int64 v3; // rax

v1 = pqueue_peek(a1);
if ( a1 && !pqueue_isEmpty(a1) )
{
v2 = *((_DWORD *)*a1 + 2);
v3 = **a1;
*a1 = (__int64 *)v3;
*(_DWORD *)(v3 + 8) += v2;
}
return v1;
}

original_code:
int pqueue_pop(pqueue_root * root)
{
 int returnValue = pqueue_peek(root);
 if (root == ((void *)0)) {
 } else {
  if (pqueue_isEmpty(root)) {
  } else {
   int priorityOfPoppedElement = root->first->priority;
   root->first = root->first->next;
   root->first->priority += priorityOfPoppedElement;
  }
 }
 return returnValue;
}

canonical_original_code:
int pqueue_pop(struct pqueue_root_s *root)
{
 int returnValue = pqueue_peek(root);
 if (root == ((void *)0)) {
 } else {
  if (pqueue_isEmpty(root)) {
  } else {
   int priorityOfPoppedElement = root->first->priority;
   root->first = root->first->next;
   root->first->priority += priorityOfPoppedElement;
  }
 }
 return returnValue;
}

variable_types: returnValue int

variable_types: priorityOfPoppedElement int


decompiled_code:
_BOOL8 __fastcall pqueue_hasNext(_QWORD *a1)
{
_BOOL4 v1; // edx
_BOOL8 result; // rax

v1 = pqueue_isEmpty(a1);
result = 1LL;
if ( !v1 )
result = *(_DWORD *)(*a1 + 8LL) > 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(_QWORD *a1)
{
_BOOL4 v1; // edx
_BOOL8 result; // rax

v1 = func1(a1);
result = 1LL;
if ( !v1 )
result = *(_DWORD *)(*a1 + 8LL) > 0;
return result;
}

original_code:
int pqueue_hasNext(pqueue_root * root)
{
 if (pqueue_isEmpty(root)) {
  return 1;
 } else {
  int priority = root->first->priority;
  if (priority <= 0) {
   return 0;
  } else {
   return 1;
  }
 }
}

canonical_original_code:
int pqueue_hasNext(struct pqueue_root_s *root)
{
 if (pqueue_isEmpty(root)) {
  return 1;
 } else {
  int priority = root->first->priority;
  if (priority <= 0) {
   return 0;
  } else {
   return 1;
  }
 }
}

variable_types: priority int


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

result = pqueue_isEmpty(a1);
if ( !(_DWORD)result )
{
result = *a1;
*(_DWORD *)(*a1 + 8LL) -= a2;
}
return result;
}

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

result = func1(a1);
if ( !(_DWORD)result )
{
result = *a1;
*(_DWORD *)(*a1 + 8LL) -= a2;
}
return result;
}

original_code:
void pqueue_pass(pqueue_root * root, int time)
{
 if (pqueue_isEmpty(root)) {
 } else {
  root->first->priority -= time;
 }
}

canonical_original_code:
void pqueue_pass(struct pqueue_root_s *root, int time)
{
 if (pqueue_isEmpty(root)) {
 } else {
  root->first->priority -= time;
 }
}

variable_types: time int


original_code:
int pqueue_insert(pqueue_root * root, int priority, int message)
{
 if (root == ((void *)0))
  return 1;
 if (priority < 0)
  return 2;
 int didInsertionHappen = 1;
 if (pqueue_isEmpty(root)) {
  pqueue_node *newElement = calloc(1, sizeof(pqueue_node));
  if (newElement) {
   newElement->priority = priority;
   newElement->message = message;
   root->first = newElement;
   didInsertionHappen = 0;
  } else {
   return 3;
  }
 } else {
  pqueue_node *candidate = root->first;
  int prioritySoFar = 0;
  while (candidate != ((void *)0)) {
   if (candidate->priority >= priority) {
    int priorityOfInsertedElement =
        priority - prioritySoFar;
    int priorityOfCopiedMark =
        candidate->priority -
        priorityOfInsertedElement;
    pqueue_node *newElement =
        calloc(1, sizeof(pqueue_node));
    newElement->priority = priorityOfCopiedMark;
    newElement->message = candidate->message;
    newElement->next = candidate->next;
    candidate->next = newElement;
    candidate->priority = priorityOfInsertedElement;
    candidate->message = message;
    didInsertionHappen = 0;
    break;
   }
   if (candidate->next == ((void *)0)) {
    pqueue_node *newElement =
        calloc(1, sizeof(pqueue_node));
    newElement->priority = priority - prioritySoFar;
    newElement->message = message;
    candidate->next = newElement;
    didInsertionHappen = 0;
    break;
   }
   prioritySoFar += candidate->priority;
   candidate = candidate->next;
  }
 }
 if (didInsertionHappen == 0)
  return 0;
 else
  return 4;
}

canonical_original_code:
int pqueue_insert(struct pqueue_root_s *root, int priority, int message)
{
 if (root == ((void *)0))
  return 1;
 if (priority < 0)
  return 2;
 int didInsertionHappen = 1;
 if (pqueue_isEmpty(root)) {
  struct pqueue_node_s *newElement = calloc(1, sizeof(pqueue_node));
  if (newElement) {
   newElement->priority = priority;
   newElement->message = message;
   root->first = newElement;
   didInsertionHappen = 0;
  } else {
   return 3;
  }
 } else {
  struct pqueue_node_s *candidate = root->first;
  int prioritySoFar = 0;
  while (candidate != ((void *)0)) {
   if (candidate->priority >= priority) {
    int priorityOfInsertedElement = priority - prioritySoFar;
    int priorityOfCopiedMark = candidate->priority -
        priorityOfInsertedElement;
    struct pqueue_node_s *newElement = calloc(1, sizeof(pqueue_node));
    newElement->priority = priorityOfCopiedMark;
    newElement->message = candidate->message;
    newElement->next = candidate->next;
    candidate->next = newElement;
    candidate->priority = priorityOfInsertedElement;
    candidate->message = message;
    didInsertionHappen = 0;
    break;
   }
   if (candidate->next == ((void *)0)) {
    struct pqueue_node_s *newElement = calloc(1, sizeof(pqueue_node));
    newElement->priority = priority - prioritySoFar;
    newElement->message = message;
    candidate->next = newElement;
    didInsertionHappen = 0;
    break;
   }
   prioritySoFar += candidate->priority;
   candidate = candidate->next;
  }
 }
 if (didInsertionHappen == 0)
  return 0;
 else
  return 4;
}

variable_types: message int

variable_types: didInsertionHappen int

variable_types: struct pqueue_node_s { struct pqueue_node_s *next; int priority; int message; } *

variable_types: prioritySoFar int

variable_types: priorityOfInsertedElement int

variable_types: priorityOfCopiedMark int


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

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

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


decompiled_code:
void mbc_mbc5_write_dummy()
{
;
}

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

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


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

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

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


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

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

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


decompiled_code:
void mbc_mbc5_write_ram_enable()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

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

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


decompiled_code:
__int64 __fastcall mbc_mbc5_write_rom_bank_select_low(__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_mbc5_write_rom_bank_select_low(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_mbc5_write_rom_bank_select_low(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;
}

variable_types: bank int

variable_types:
offset
unsigned long


decompiled_code:
__int64 __fastcall mbc_mbc5_write_rom_bank_select_high(__int64 a1, char a2)
{
int v2; // eax
int v3; // edx
__int64 result; // rax

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

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

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

original_code:
void mbc_mbc5_write_rom_bank_select_high(uint16_t address, uint8_t 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_mbc5_write_rom_bank_select_high(unsigned short address, unsigned char 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:
__int64 __fastcall mbc_mbc5_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_mbc5_read_extram(uint16_t address)
{
 return cart.extram_bank[address & 0x1fff];
}

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


decompiled_code:
__int64 __fastcall mbc_mbc5_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_mbc5_write_extram(uint16_t address, uint8_t data)
{
 cart.extram_bank[address & 0x1fff] = data;
}

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


decompiled_code:
void mbc_mbc5_cleanup()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

original_code:
void mbc_mbc5_cleanup()
{
}

canonical_original_code:
void mbc_mbc5_cleanup()
{
}


decompiled_code:
int __fastcall mbc_mbc5_write_ram_bank_select(__int64 a1, __int16 a2)
{
int result; // eax

if ( cart[18] )
cart[14] = cart[11] + (unsigned __int16)(a2 << 13);
result = LODWORD(cart[0]) - 28;
if ( (unsigned int)(LODWORD(cart[0]) - 28) <= 2 && (a2 & 8) != 0 )
result = printf("rumble ON, power: %d\n", (unsigned __int8)a2 >> 4);
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1, __int16 a2)
{
int result; // eax

if ( cart[18] )
cart[14] = cart[11] + (unsigned __int16)(a2 << 13);
result = LODWORD(cart[0]) - 28;
if ( (unsigned int)(LODWORD(cart[0]) - 28) <= 2 && (a2 & 8) != 0 )
result = printf("rumble ON, power: %d\n", (unsigned __int8)a2 >> 4);
return result;
}

original_code:
void mbc_mbc5_write_ram_bank_select(uint16_t address, uint8_t data)
{
 if (cart.extram_size > 0) {
  int bank = data & 0x07;
  cart.extram_bank = cart.extram + bank * 8192;
 }
 switch (cart.mbc_type) {
 case 0x1C:
 case 0x1D:
 case 0x1E:
  if (data & 0x08)
   printf("rumble ON, power: %d\n", data >> 4);
  break;
 default:
  break;
 }
}

canonical_original_code:
void mbc_mbc5_write_ram_bank_select(unsigned short address, unsigned char data)
{
 if (cart.extram_size > 0) {
  int bank = data & 0x07;
  cart.extram_bank = cart.extram + bank * 8192;
 }
 switch (cart.mbc_type) {
 case 0x1C:
 case 0x1D:
 case 0x1E:
  if (data & 0x08)
   printf("rumble ON, power: %d\n", data >> 4);
  break;
 default:
  break;
 }
}


decompiled_code:
__int64 mbc_mbc5_install()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
void (**v2)(); // rax
__int64 (__fastcall **v3)(__int64, unsigned __int8); // rax
__int64 (__fastcall **v4)(__int64, char); // rax
int (__fastcall **v5)(__int64, __int16); // rax
void (**v6)(); // 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_mbc5_cleanup;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_mbc5_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_mbc5_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_mbc5_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_mbc5_write_rom_bank_select_low;
while ( v3 != (__int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)&writemem + 384) );
v4 = (__int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 384);
do
*v4++ = mbc_mbc5_write_rom_bank_select_high;
while ( (__int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 512) != v4 );
v5 = (int (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 512);
do
*v5++ = mbc_mbc5_write_ram_bank_select;
while ( v5 != (int (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 768) );
v6 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v6++ = mbc_mbc5_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != 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_mbc5_read_extram;
while ( v11 != v10 );
}
if ( v9 <= 191 )
{
v12 = v9;
do
*((_QWORD *)&readmem + v12++) = mbc_mbc5_read_ff;
while ( (int)v12 <= 191 );
if ( v9 <= 160 )
goto LABEL_31;
}
v13 = 160LL;
do
*((_QWORD *)&writemem + v13++) = mbc_mbc5_write_extram;
while ( v9 > (int)v13 );
if ( v9 <= 191 )
{
LABEL_31:
do
*((_QWORD *)&writemem + v9++) = mbc_mbc5_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
void (**v2)(); // rax
__int64 (__fastcall **v3)(__int64, unsigned __int8); // rax
__int64 (__fastcall **v4)(__int64, char); // rax
int (__fastcall **v5)(__int64, __int16); // rax
void (**v6)(); // 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_mbc5_cleanup;
v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_mbc5_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_mbc5_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_mbc5_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_mbc5_write_rom_bank_select_low;
while ( v3 != (__int64 (__fastcall **)(__int64, unsigned __int8))(unsigned int)((_DWORD)&writemem + 384) );
v4 = (__int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 384);
do
*v4++ = mbc_mbc5_write_rom_bank_select_high;
while ( (__int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 512) != v4 );
v5 = (int (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 512);
do
*v5++ = mbc_mbc5_write_ram_bank_select;
while ( v5 != (int (__fastcall **)(__int64, __int16))(unsigned int)((_DWORD)&writemem + 768) );
v6 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v6++ = mbc_mbc5_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != 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_mbc5_read_extram;
while ( v11 != v10 );
}
if ( v9 <= 191 )
{
v12 = v9;
do
*((_QWORD *)&readmem + v12++) = mbc_mbc5_read_ff;
while ( (int)v12 <= 191 );
if ( v9 <= 160 )
goto LABEL_31;
}
v13 = 160LL;
do
*((_QWORD *)&writemem + v13++) = mbc_mbc5_write_extram;
while ( v9 > (int)v13 );
if ( v9 <= 191 )
{
LABEL_31:
do
*((_QWORD *)&writemem + v9++) = mbc_mbc5_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_mbc5_install()
{
 cart.cleanup = &mbc_mbc5_cleanup;
 int i;
 for (i = 0x00; i <= 0x3F; ++i) {
  readmem[i] = mbc_mbc5_read_bank_0;
 }
 for (i = 0x40; i <= 0x7F; ++i) {
  readmem[i] = mbc_mbc5_read_bank_n;
 }
 for (i = 0x00; i <= 0x1F; ++i) {
  writemem[i] = mbc_mbc5_write_ram_enable;
 }
 for (i = 0x20; i <= 0x2F; ++i) {
  writemem[i] = mbc_mbc5_write_rom_bank_select_low;
 }
 for (i = 0x30; i <= 0x3F; ++i) {
  writemem[i] = mbc_mbc5_write_rom_bank_select_high;
 }
 for (i = 0x40; i <= 0x5F; ++i) {
  writemem[i] = mbc_mbc5_write_ram_bank_select;
 }
 for (i = 0x60; i <= 0x7F; ++i) {
  writemem[i] = mbc_mbc5_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_mbc5_read_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  readmem[i] = mbc_mbc5_read_ff;
 }
 for (i = 0xA0; i < extram_end; ++i) {
  writemem[i] = mbc_mbc5_write_extram;
 }
 for (i = extram_end; i <= 0xBF; ++i) {
  writemem[i] = mbc_mbc5_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;
}

variable_types: i int


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

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

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_huc1_write_dummy()
{
;
}

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

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


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

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

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

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


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

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

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

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

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

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


decompiled_code:
__int64 mbc_huc1_install()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
unsigned __int64 (**v2)(); // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 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

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_huc1_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_huc1_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (unsigned __int64 (**)())&writemem;
do
*v2++ = mbc_huc1_write_ram_enable;
while ( v2 != (unsigned __int64 (**)())(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_huc1_write_rom_bank_select;
v3 += 8LL;
}
while ( v3 != (_DWORD)&writemem + 512 );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_huc1_write_ram_bank_select;
v4 += 8LL;
}
while ( v4 != (_DWORD)&writemem + 768 );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_huc1_write_mode_select;
v5 += 8LL;
}
while ( (_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_huc1_read_extram;
while ( v10 != v9 );
}
if ( v8 <= 191 )
{
v11 = v8;
do
*((_QWORD *)&readmem + v11++) = mbc_huc1_read_ff;
while ( (int)v11 <= 191 );
if ( v8 <= 160 )
goto LABEL_29;
}
v12 = 160LL;
do
*((_QWORD *)&writemem + v12++) = mbc_huc1_write_extram;
while ( v8 > (int)v12 );
if ( v8 <= 191 )
{
LABEL_29:
do
*((_QWORD *)&writemem + v8++) = mbc_huc1_write_dummy;
while ( v8 <= 191 );
}
v13 = cart[4];
cart[6] = v13;
cart[7] = v13 + 0x4000;
huc1_bank_low = 1;
huc1_bank_high = 0;
huc1_ram_bank = 0;
huc1_mode_select = 0;
result = cart[11];
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
unsigned __int64 (**v2)(); // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 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

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_huc1_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_huc1_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (unsigned __int64 (**)())&writemem;
do
*v2++ = mbc_huc1_write_ram_enable;
while ( v2 != (unsigned __int64 (**)())(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_huc1_write_rom_bank_select;
v3 += 8LL;
}
while ( v3 != (_DWORD)&writemem + 512 );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_huc1_write_ram_bank_select;
v4 += 8LL;
}
while ( v4 != (_DWORD)&writemem + 768 );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_huc1_write_mode_select;
v5 += 8LL;
}
while ( (_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_huc1_read_extram;
while ( v10 != v9 );
}
if ( v8 <= 191 )
{
v11 = v8;
do
*((_QWORD *)&readmem + v11++) = mbc_huc1_read_ff;
while ( (int)v11 <= 191 );
if ( v8 <= 160 )
goto LABEL_29;
}
v12 = 160LL;
do
*((_QWORD *)&writemem + v12++) = mbc_huc1_write_extram;
while ( v8 > (int)v12 );
if ( v8 <= 191 )
{
LABEL_29:
do
*((_QWORD *)&writemem + v8++) = mbc_huc1_write_dummy;
while ( v8 <= 191 );
}
v13 = cart[4];
cart[6] = v13;
cart[7] = v13 + 0x4000;
huc1_bank_low = 1;
huc1_bank_high = 0;
huc1_ram_bank = 0;
huc1_mode_select = 0;
result = cart[11];
cart[14] = result;
return result;
}

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


decompiled_code:
unsigned __int64 mbc_huc1_regs_changed()
{
int v0; // eax
__int16 v1; // cx
unsigned __int64 result; // rax
unsigned __int64 v3; // rtt

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

canonical_decompiled_code:
unsigned __int64 func8()
{
int v0; // eax
__int16 v1; // cx
unsigned __int64 result; // rax
unsigned __int64 v3; // rtt

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

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

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

variable_types: rambank int

variable_types: rombank int

variable_types:
cartoffset
unsigned long


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

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

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

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

variable_types:
address
unsigned short

variable_types:
data
unsigned char


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

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

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_huc1_write_extram_disabled()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

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

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


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

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

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

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

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

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


decompiled_code:
void mbc_boot_write_bank_0()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

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

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


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

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

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

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


decompiled_code:
void mbc_boot_write_bank_n()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

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

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


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

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_mbc1_write_dummy()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

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

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


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

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

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

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


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

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

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

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

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

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


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

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

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

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


decompiled_code:
void mbc_mbc1_write_extram_disabled()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

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

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


decompiled_code:
__int64 mbc_mbc1_install()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
unsigned __int64 (__fastcall **v2)(__int64, char); // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 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

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_mbc1_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_mbc1_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (unsigned __int64 (__fastcall **)(__int64, char))&writemem;
do
*v2++ = mbc_mbc1_write_ram_enable;
while ( v2 != (unsigned __int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc1_write_rom_bank_select;
v3 += 8LL;
}
while ( v3 != (_DWORD)&writemem + 512 );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc1_write_ram_bank_select;
v4 += 8LL;
}
while ( v4 != (_DWORD)&writemem + 768 );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc1_write_mode_select;
v5 += 8LL;
}
while ( (_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_mbc1_read_extram;
while ( v10 != v9 );
}
if ( v8 <= 191 )
{
v11 = v8;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc1_read_ff;
while ( (int)v11 <= 191 );
if ( v8 <= 160 )
goto LABEL_29;
}
v12 = 160LL;
do
*((_QWORD *)&writemem + v12++) = mbc_mbc1_write_extram;
while ( v8 > (int)v12 );
if ( v8 <= 191 )
{
LABEL_29:
do
*((_QWORD *)&writemem + v8++) = mbc_mbc1_write_dummy;
while ( v8 <= 191 );
}
v13 = cart[4];
cart[6] = v13;
cart[7] = v13 + 0x4000;
mbc1_bank_low = 1;
mbc1_bank_high = 0;
mbc1_ram_bank = 0;
mbc1_mode_select = 0;
result = cart[11];
cart[14] = result;
return result;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 (__fastcall **v0)(unsigned __int16); // rax
__int64 (__fastcall **v1)(__int16); // rax
unsigned __int64 (__fastcall **v2)(__int64, char); // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int64 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

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_mbc1_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_mbc1_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (unsigned __int64 (__fastcall **)(__int64, char))&writemem;
do
*v2++ = mbc_mbc1_write_ram_enable;
while ( v2 != (unsigned __int64 (__fastcall **)(__int64, char))(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned int)((_DWORD)&writemem + 256);
do
{
*(_QWORD *)v3 = mbc_mbc1_write_rom_bank_select;
v3 += 8LL;
}
while ( v3 != (_DWORD)&writemem + 512 );
v4 = (unsigned int)((_DWORD)&writemem + 512);
do
{
*(_QWORD *)v4 = mbc_mbc1_write_ram_bank_select;
v4 += 8LL;
}
while ( v4 != (_DWORD)&writemem + 768 );
v5 = (unsigned int)((_DWORD)&writemem + 768);
do
{
*(_QWORD *)v5 = mbc_mbc1_write_mode_select;
v5 += 8LL;
}
while ( (_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_mbc1_read_extram;
while ( v10 != v9 );
}
if ( v8 <= 191 )
{
v11 = v8;
do
*((_QWORD *)&readmem + v11++) = mbc_mbc1_read_ff;
while ( (int)v11 <= 191 );
if ( v8 <= 160 )
goto LABEL_29;
}
v12 = 160LL;
do
*((_QWORD *)&writemem + v12++) = mbc_mbc1_write_extram;
while ( v8 > (int)v12 );
if ( v8 <= 191 )
{
LABEL_29:
do
*((_QWORD *)&writemem + v8++) = mbc_mbc1_write_dummy;
while ( v8 <= 191 );
}
v13 = cart[4];
cart[6] = v13;
cart[7] = v13 + 0x4000;
mbc1_bank_low = 1;
mbc1_bank_high = 0;
mbc1_ram_bank = 0;
mbc1_mode_select = 0;
result = cart[11];
cart[14] = result;
return result;
}

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


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

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

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_cam_write_dummy()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_cam_write_ram_enable()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

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

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


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

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

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

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

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

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


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

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

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


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

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

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

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

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

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


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

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


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

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_cam_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_cam_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_cam_write_ram_enable;
while ( v2 != (void (**)())(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned __int64 (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 256);
do
*v3++ = mbc_cam_write_rom_bank_select;
while ( v3 != (unsigned __int64 (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 512) );
v4 = (void (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 512);
do
*v4++ = mbc_cam_write_extram_bank_select;
while ( v4 != (void (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 768) );
v5 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v5++ = mbc_cam_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != v5 );
v6 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1280);
do
*v6++ = mbc_cam_read_extram;
while ( v6 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1536) );
v7 = (void (__fastcall **)(__int16, char))(unsigned int)((_DWORD)&writemem + 1280);
do
*v7++ = mbc_cam_write_extram;
while ( (void (__fastcall **)(__int16, char))(unsigned int)((_DWORD)&writemem + 1536) != v7 );
v8 = cart[4];
cart[6] = v8;
cart[7] = v8 + 0x4000;
LOBYTE(cart[17]) = 0;
result = cart[11];
cart[14] = result;
cam_mode = 0;
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, int); // rax
void (__fastcall **v4)(__int64, int); // rax
void (**v5)(); // rax
__int64 (__fastcall **v6)(__int16); // rax
void (__fastcall **v7)(__int16, char); // rax
__int64 v8; // rax
__int64 result; // rax

v0 = (__int64 (__fastcall **)(unsigned __int16))&readmem;
do
*v0++ = mbc_cam_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_cam_read_bank_n;
while ( v1 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1024) );
v2 = (void (**)())&writemem;
do
*v2++ = mbc_cam_write_ram_enable;
while ( v2 != (void (**)())(unsigned int)((_DWORD)&writemem + 256) );
v3 = (unsigned __int64 (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 256);
do
*v3++ = mbc_cam_write_rom_bank_select;
while ( v3 != (unsigned __int64 (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 512) );
v4 = (void (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 512);
do
*v4++ = mbc_cam_write_extram_bank_select;
while ( v4 != (void (__fastcall **)(__int64, int))(unsigned int)((_DWORD)&writemem + 768) );
v5 = (void (**)())(unsigned int)((_DWORD)&writemem + 768);
do
*v5++ = mbc_cam_write_dummy;
while ( (void (**)())(unsigned int)((_DWORD)&writemem + 1024) != v5 );
v6 = (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1280);
do
*v6++ = mbc_cam_read_extram;
while ( v6 != (__int64 (__fastcall **)(__int16))(unsigned int)((_DWORD)&readmem + 1536) );
v7 = (void (__fastcall **)(__int16, char))(unsigned int)((_DWORD)&writemem + 1280);
do
*v7++ = mbc_cam_write_extram;
while ( (void (__fastcall **)(__int16, char))(unsigned int)((_DWORD)&writemem + 1536) != v7 );
v8 = cart[4];
cart[6] = v8;
cart[7] = v8 + 0x4000;
LOBYTE(cart[17]) = 0;
result = cart[11];
cart[14] = result;
cam_mode = 0;
return result;
}

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


decompiled_code:
void mbc_cam_write_mode_select()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

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

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


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

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

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

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


decompiled_code:
void mbc_mbc2_write_dummy()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

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

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


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

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

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

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


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

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

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

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


decompiled_code:
void mbc_mbc2_write_ram_enable()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

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

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