decompiled_code:
int __fastcall print_statistics(void *root)
{
int v1; // er12
unsigned int v2; // ebp
int v3; // er13
unsigned int v4; // eax
__int64 *v5; // rbx
const char **v6; // rax
const char **v7; // rbx
unsigned int v9; // [rsp+18h] [rbp-40h] BYREF
int v10; // [rsp+1Ch] [rbp-3Ch] BYREF
unsigned int v11; // [rsp+20h] [rbp-38h] BYREF
int v12; // [rsp+24h] [rbp-34h] BYREF
unsigned int v13; // [rsp+28h] [rbp-30h] BYREF
int v14[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

v14[0] = 0;
v13 = 0;
v12 = 0;
v11 = 0;
v10 = 0;
v9 = 0;
puts("Statistics:");
fflush(stdout);
reset_hash_iterator(file_hash);
v1 = 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v6 = (const char **)next_hash_entry(file_hash);
v7 = v6;
if ( !v6 )
break;
v4 = strlen(*v6);
v5 = (__int64 *)v7[1];
++v3;
if ( v2 < v4 )
v2 = v4;
v1 += v4;
count_hash(v5[1], v14, &v13);
count_hash(v5[2], &v12, &v11);
count_hash(v5[3], &v10, &v9);
}
printf(
"Num files: %u\nMax filename len: %u, Average filename len: %.2f\n",
(unsigned int)v3,
v2,
(float)((float)v1 / (float)v3));
printf("Max revisions for file: %u, Average revisions for file: %.2f\n", v13, (float)((float)v14[0] / (float)v3));
printf("Max branches for file: %u, Average branches for file: %.2f\n", v11, (float)((float)v12 / (float)v3));
printf("Max branches_sym for file: %u, Average branches_sym for file: %.2f\n", v9, (float)((float)v10 / (float)v3));
twalk(root, (__action_fn_t)stat_ps_tree_node);
printf("Num patchsets: %u\n", (unsigned int)num_patch_sets);
printf(
"Max PS members in PS: %u\nAverage PS members in PS: %.2f\n",
(unsigned int)max_ps_member_in_ps,
(float)((float)num_ps_member / (float)num_patch_sets));
printf(
"Num authors: %u, Max author len: %u, Avg. author len: %.2f\n",
(unsigned int)num_authors,
(unsigned int)max_author_len,
(float)((float)total_author_len / (float)num_authors));
return printf(
"Max desc len: %u, Avg. desc len: %.2f\n",
(unsigned int)max_descr_len,
(float)((float)total_descr_len / (float)num_patch_sets));
}

canonical_decompiled_code:
int __fastcall func2(void *root)
{
int v1; // er12
unsigned int v2; // ebp
int v3; // er13
unsigned int v4; // eax
__int64 *v5; // rbx
const char **v6; // rax
const char **v7; // rbx
unsigned int v9; // [rsp+18h] [rbp-40h] BYREF
int v10; // [rsp+1Ch] [rbp-3Ch] BYREF
unsigned int v11; // [rsp+20h] [rbp-38h] BYREF
int v12; // [rsp+24h] [rbp-34h] BYREF
unsigned int v13; // [rsp+28h] [rbp-30h] BYREF
int v14[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

v14[0] = 0;
v13 = 0;
v12 = 0;
v11 = 0;
v10 = 0;
v9 = 0;
puts("Statistics:");
fflush(stdout);
reset_hash_iterator(file_hash);
v1 = 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v6 = (const char **)next_hash_entry(file_hash);
v7 = v6;
if ( !v6 )
break;
v4 = strlen(*v6);
v5 = (__int64 *)v7[1];
++v3;
if ( v2 < v4 )
v2 = v4;
v1 += v4;
func0(v5[1], v14, &v13);
func0(v5[2], &v12, &v11);
func0(v5[3], &v10, &v9);
}
printf(
"Num files: %u\nMax filename len: %u, Average filename len: %.2f\n",
(unsigned int)v3,
v2,
(float)((float)v1 / (float)v3));
printf("Max revisions for file: %u, Average revisions for file: %.2f\n", v13, (float)((float)v14[0] / (float)v3));
printf("Max branches for file: %u, Average branches for file: %.2f\n", v11, (float)((float)v12 / (float)v3));
printf("Max branches_sym for file: %u, Average branches_sym for file: %.2f\n", v9, (float)((float)v10 / (float)v3));
twalk(root, (__action_fn_t)stat_ps_tree_node);
printf("Num patchsets: %u\n", (unsigned int)num_patch_sets);
printf(
"Max PS members in PS: %u\nAverage PS members in PS: %.2f\n",
(unsigned int)max_ps_member_in_ps,
(float)((float)num_ps_member / (float)num_patch_sets));
printf(
"Num authors: %u, Max author len: %u, Avg. author len: %.2f\n",
(unsigned int)num_authors,
(unsigned int)max_author_len,
(float)((float)total_author_len / (float)num_authors));
return printf(
"Max desc len: %u, Avg. desc len: %.2f\n",
(unsigned int)max_descr_len,
(float)((float)total_descr_len / (float)num_patch_sets));
}

original_code:
void print_statistics(void * ps_tree)
{
    unsigned int num_files = 0, max_file_len = 0, total_file_len = 0;
    unsigned int total_revisions = 0, max_revisions_for_file = 0;
    unsigned int total_branches = 0, max_branches_for_file = 0;
    unsigned int total_branches_sym = 0, max_branches_sym_for_file = 0;
    struct hash_entry *he;
    printf("Statistics:\n");
    fflush(stdout);
    reset_hash_iterator(file_hash);
    while ((he=next_hash_entry(file_hash)))
    {
 int len = strlen(he->he_key);
 CvsFile *file = (CvsFile *)he->he_obj;
 num_files++;
 max_file_len = ((max_file_len) > (len) ? (max_file_len) : (len));
 total_file_len += len;
 count_hash(file->revisions, &total_revisions, &max_revisions_for_file);
 count_hash(file->branches, &total_branches, &max_branches_for_file);
 count_hash(file->branches_sym, &total_branches_sym,
     &max_branches_sym_for_file);
    }
    printf("Num files: %u\nMax filename len: %u, Average filename len: %.2f\n",
     num_files, max_file_len, (float)total_file_len/num_files);
    printf("Max revisions for file: %u, Average revisions for file: %.2f\n",
   max_revisions_for_file, (float)total_revisions/num_files);
    printf("Max branches for file: %u, Average branches for file: %.2f\n",
   max_branches_for_file, (float)total_branches/num_files);
    printf("Max branches_sym for file: %u, Average branches_sym for file: %.2f\n",
   max_branches_sym_for_file, (float)total_branches_sym/num_files);
    twalk(ps_tree, stat_ps_tree_node);
    printf("Num patchsets: %u\n", num_patch_sets);
    printf("Max PS members in PS: %u\nAverage PS members in PS: %.2f\n",
     max_ps_member_in_ps, (float)num_ps_member/num_patch_sets);
    printf("Num authors: %u, Max author len: %u, Avg. author len: %.2f\n",
     num_authors, max_author_len, (float)total_author_len/num_authors);
    printf("Max desc len: %u, Avg. desc len: %.2f\n",
     max_descr_len, (float)total_descr_len/num_patch_sets);
}

canonical_original_code:
void print_statistics(void *ps_tree)
{
    unsigned int num_files = 0; unsigned int max_file_len = 0; unsigned int total_file_len = 0;
    unsigned int total_revisions = 0; unsigned int max_revisions_for_file = 0;
    unsigned int total_branches = 0; unsigned int max_branches_for_file = 0;
    unsigned int total_branches_sym = 0; unsigned int max_branches_sym_for_file = 0;
    struct hash_entry *he;
    printf("Statistics:\n");
    fflush(stdout);
    reset_hash_iterator(file_hash);
    while ((he=next_hash_entry(file_hash)))
    {
 int len = strlen(he->he_key);
 struct _CvsFile *file = (CvsFile *)he->he_obj;
 num_files++;
 max_file_len = ((max_file_len) > (len) ? (max_file_len) : (len));
 total_file_len += len;
 count_hash(file->revisions, &total_revisions, &max_revisions_for_file);
 count_hash(file->branches, &total_branches, &max_branches_for_file);
 count_hash(file->branches_sym, &total_branches_sym,
     &max_branches_sym_for_file);
    }
    printf("Num files: %u\nMax filename len: %u, Average filename len: %.2f\n",
     num_files, max_file_len, (float)total_file_len/num_files);
    printf("Max revisions for file: %u, Average revisions for file: %.2f\n",
   max_revisions_for_file, (float)total_revisions/num_files);
    printf("Max branches for file: %u, Average branches for file: %.2f\n",
   max_branches_for_file, (float)total_branches/num_files);
    printf("Max branches_sym for file: %u, Average branches_sym for file: %.2f\n",
   max_branches_sym_for_file, (float)total_branches_sym/num_files);
    twalk(ps_tree, stat_ps_tree_node);
    printf("Num patchsets: %u\n", num_patch_sets);
    printf("Max PS members in PS: %u\nAverage PS members in PS: %.2f\n",
     max_ps_member_in_ps, (float)num_ps_member/num_patch_sets);
    printf("Num authors: %u, Max author len: %u, Avg. author len: %.2f\n",
     num_authors, max_author_len, (float)total_author_len/num_authors);
    printf("Max desc len: %u, Avg. desc len: %.2f\n",
     max_descr_len, (float)total_descr_len/num_patch_sets);
}

variable_types: struct hash_entry { char *he_key; void *he_obj; struct list_head { struct list_head *next; } he_list; } *

variable_types: struct _CvsFile { char *filename; struct hash_table { int ht_size; struct list_head { struct list_head *next; } *ht_lists; int iterator; struct list_head *iterator_ptr; } *revisions; struct hash_table *branches; struct hash_table *branches_sym; struct hash_table *symbols; _Bool have_branches; } *

user_defined_types: struct _CvsFile { char *filename; struct hash_table *revisions; struct hash_table *branches; struct hash_table *branches_sym; struct hash_table *symbols; _Bool have_branches; }


main

canonical_name: main

repo: jlinnosa/mathprep

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er15
unsigned int v4; // er14
unsigned int i; // er13
unsigned int v6; // er13
char v7; // r8
unsigned int v8; // ebp
unsigned int v9; // er12
int v10; // ebx
char *s; // [rsp+8h] [rbp-40h]

puts("mathprep\n");
init_randomness("mathprep\n", argv);
s = (char *)malloc(0x64uLL);
if ( s )
{
v3 = 0;
v4 = 0;
for ( i = 0; ; i = v8 )
{
do
{
v8 = random_int(1LL, 8LL);
v9 = random_int(1LL, 3LL);
v10 = random_int(1LL, 2LL);
}
while ( v4 == v9 && i == v8 && v3 == v10 );
if ( v10 == 1 )
{
v6 = v8 + v9;
v7 = 43;
}
else
{
if ( v10 != 2 )
__assert_fail("0", "mathprep.c", 0x2Eu, "main");
v6 = v8 - v9;
v7 = 45;
}
snprintf(s, 0x64uLL, "%i %c %i", v8, (unsigned int)v7, v9);
if ( !(unsigned int)ask(s, v6) )
{
free(s);
exit(0);
}
v3 = v10;
v4 = v9;
}
}
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er15
unsigned int v4; // er14
unsigned int i; // er13
unsigned int v6; // er13
char v7; // r8
unsigned int v8; // ebp
unsigned int v9; // er12
int v10; // ebx
char *s; // [rsp+8h] [rbp-40h]

puts("mathprep\n");
func8("mathprep\n", argv);
s = (char *)malloc(0x64uLL);
if ( s )
{
v3 = 0;
v4 = 0;
for ( i = 0; ; i = v8 )
{
do
{
v8 = func9(1LL, 8LL);
v9 = func9(1LL, 3LL);
v10 = func9(1LL, 2LL);
}
while ( v4 == v9 && i == v8 && v3 == v10 );
if ( v10 == 1 )
{
v6 = v8 + v9;
v7 = 43;
}
else
{
if ( v10 != 2 )
__assert_fail("0", "mathprep.c", 0x2Eu, "main");
v6 = v8 - v9;
v7 = 45;
}
snprintf(s, 0x64uLL, "%i %c %i", v8, (unsigned int)v7, v9);
if ( !(unsigned int)func7(s, v6) )
{
free(s);
exit(0);
}
v3 = v10;
v4 = v9;
}
}
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
 int x, y, op, prev_x, prev_y, prev_op, result;
 char operator, *str;
 printf("mathprep\n\n");
 init_randomness();
 x = y = op = 0;
 if ((str = malloc(100)) == ((void *)0))
  exit(1);
 for (;;) {
  prev_x = x;
  prev_y = y;
  prev_op = op;
  do {
   x = random_int(1, 8);
   y = random_int(1, 3);
   op = random_int(1, 2);
  } while (x == prev_x && y == prev_y && op == prev_op);
  switch (op) {
  case 1:
   operator = '+';
   result = x + y;
   break;
  case 2:
   operator = '-';
   result = x - y;
   break;
  default:
   ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "mathprep.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  };
  snprintf(str, 100, "%i %c %i", x, operator, y);
  if (!ask(str, result))
   break;
 }
 free(str);
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int x; int y; int op; int prev_x; int prev_y; int prev_op; int result;
 char operator; char *str;
 printf("mathprep\n\n");
 init_randomness();
 x = y = op = 0;
 if ((str = malloc(100)) == ((void *)0))
  exit(1);
 for (;;) {
  prev_x = x;
  prev_y = y;
  prev_op = op;
  do {
   x = random_int(1, 8);
   y = random_int(1, 3);
   op = random_int(1, 2);
  } while (x == prev_x && y == prev_y && op == prev_op);
  switch (op) {
  case 1:
   operator = '+';
   result = x + y;
   break;
  case 2:
   operator = '-';
   result = x - y;
   break;
  default:
   ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "mathprep.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  };
  snprintf(str, 100, "%i %c %i", x, operator, y);
  if (!ask(str, result))
   break;
 }
 free(str);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: x int

variable_types: y int

variable_types: op int

variable_types: result int

variable_types: operator char

variable_types:
str
char *

return_type: int


ask

decompiled_code:
__int64 __fastcall ask(const char *a1, int a2)
{
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-1Ch]

while ( 1 )
{
printf("%s = ", a1);
result = __isoc99_scanf();
if ( !(_DWORD)result )
break;
if ( v3 == a2 )
{
puts("Oikein!\n");
return 1LL;
}
puts("Ei ihan. Koita uudestaan!");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, int a2)
{
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-1Ch]

while ( 1 )
{
printf("%s = ", a1);
result = __isoc99_scanf();
if ( !(_DWORD)result )
break;
if ( v3 == a2 )
{
puts("Oikein!\n");
return 1LL;
}
puts("Ei ihan. Koita uudestaan!");
}
return result;
}

original_code:
int ask(char *str, int result)
{
 int answer;
 for (;;) {
  printf("%s = ", str);
  if (!scanf("%i", &answer))
   return 0;
  if (result == answer) {
   printf("Oikein!\n\n");
   return 1;
  } else {
   printf("Ei ihan. Koita uudestaan!\n");
  }
 }
}

variable_types: answer int


decompiled_code:
void init_randomness()
{
struct timeval v0; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v0, 0LL);
srand(v0.tv_usec);
}

canonical_decompiled_code:
void func8()
{
struct timeval v0; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v0, 0LL);
srand(v0.tv_usec);
}

original_code:
void init_randomness()
{
 struct timeval t;
 gettimeofday(&t, 0);
 srand(t.tv_usec);
 return;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

return_type: void


decompiled_code:
__int64 __fastcall random_int(int a1, int a2)
{
if ( a2 <= a1 )
__assert_fail("max > min", "tools.c", 0xFu, "random_int");
return (unsigned int)(rand() % (a2 + 1 - a1) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
if ( a2 <= a1 )
__assert_fail("max > min", "tools.c", 0xFu, "random_int");
return (unsigned int)(rand() % (a2 + 1 - a1) + a1);
}

original_code:
int random_int(int min, int max)
{
 ((void) sizeof ((max > min) ? 1 : 0), __extension__ ({ if (max > min) ; else __assert_fail ("max > min", "tools.c", 15, __extension__ __PRETTY_FUNCTION__); }));
 int range = (max + 1) - min;
 return rand() % range + min;
}

variable_types: min int

variable_types: max int

variable_types: range int


original_code:
void timer_reset()
{
    timer.regs.div = 0;
    timer.regs.count = 0;
    timer.regs.mod = 0;
    timer.regs.ctrl = 0;
}


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

if ( a1 == 0xFF06 )
return *((unsigned __int8 *)&timer + 14);
if ( a1 > 0xFF06u )
{
result = 0LL;
if ( a1 == 0xFF07 )
result = *((unsigned __int8 *)&timer + 15);
}
else
{
result = *((unsigned __int8 *)&timer + 12);
if ( a1 != 0xFF04 )
{
if ( a1 == 0xFF05 )
result = *((unsigned __int8 *)&timer + 13);
else
result = 0LL;
}
}
return result;
}

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

if ( a1 == 0xFF06 )
return *((unsigned __int8 *)&timer + 14);
if ( a1 > 0xFF06u )
{
result = 0LL;
if ( a1 == 0xFF07 )
result = *((unsigned __int8 *)&timer + 15);
}
else
{
result = *((unsigned __int8 *)&timer + 12);
if ( a1 != 0xFF04 )
{
if ( a1 == 0xFF05 )
result = *((unsigned __int8 *)&timer + 13);
else
result = 0LL;
}
}
return result;
}

original_code:
Byte timer_getbyte(Word addr)
{
    switch (addr)
    {
        case 0xFF04:
            return timer.regs.div;
        case 0xFF05:
            return timer.regs.count;
        case 0xFF06:
            return timer.regs.mod;
        case 0xFF07:
            return timer.regs.ctrl;
        default:
            return 0;
    }
}

canonical_original_code:
unsigned char timer_getbyte(unsigned short addr)
{
    switch (addr)
    {
        case 0xFF04:
            return timer.regs.div;
        case 0xFF05:
            return timer.regs.count;
        case 0xFF06:
            return timer.regs.mod;
        case 0xFF07:
            return timer.regs.ctrl;
        default:
            return 0;
    }
}

variable_types:
addr
unsigned short

return_type: unsigned char


decompiled_code:
void __fastcall timer_putbyte(unsigned __int16 a1, char a2)
{
if ( a1 == 0xFF06 )
{
*((_BYTE *)&timer + 14) = a2;
}
else if ( a1 > 0xFF06u )
{
if ( a1 == 0xFF07 )
*((_BYTE *)&timer + 15) = a2;
}
else if ( a1 == 0xFF04 )
{
*((_BYTE *)&timer + 12) = a2;
}
else if ( a1 == 0xFF05 )
{
*((_BYTE *)&timer + 13) = a2;
}
}

original_code:
void timer_putbyte(Word addr, Byte value)
{
    switch (addr)
    {
        case 0xFF04:
            timer.regs.div = value;
            break;
        case 0xFF05:
            timer.regs.count = value;
            break;
        case 0xFF06:
            timer.regs.mod = value;
            break;
        case 0xFF07:
            timer.regs.ctrl = value;
            break;
    }
}

canonical_original_code:
void timer_putbyte(unsigned short addr, unsigned char value)
{
    switch (addr)
    {
        case 0xFF04:
            timer.regs.div = value;
            break;
        case 0xFF05:
            timer.regs.count = value;
            break;
        case 0xFF06:
            timer.regs.mod = value;
            break;
        case 0xFF07:
            timer.regs.ctrl = value;
            break;
    }
}

variable_types:
value
unsigned char


decompiled_code:
__int64 __fastcall timer_update(unsigned __int8 a1)
{
int v1; // edi
int v2; // edx
int v3; // eax
char v4; // r10
char v5; // r9
int v6; // ecx
char v7; // si
__int64 result; // rax

v1 = *(&timer + 1) + a1;
*(&timer + 1) = v1;
v2 = timer;
v3 = *(&timer + 2);
v4 = *((_BYTE *)&timer + 12);
v5 = 0;
LABEL_2:
v6 = v1 & 3;
v7 = v5;
while ( v1 != v6 )
{
++v2;
v1 -= 4;
++v3;
v7 = 1;
if ( v3 == 16 )
{
++v4;
v3 = 0;
v5 = 1;
goto LABEL_2;
}
}
if ( v7 )
{
timer = v2;
*(&timer + 2) = v3;
*(&timer + 1) = v6;
}
if ( v5 )
*((_BYTE *)&timer + 12) = v4;
result = *((unsigned __int8 *)&timer + 15);
if ( (result & 4) != 0 )
{
LOBYTE(result) = result & 3;
if ( (_BYTE)result == 2 )
{
LOBYTE(result) = 4;
}
else if ( (_BYTE)result == 3 )
{
LOBYTE(result) = 16;
}
else if ( (_BYTE)result != 1 )
{
LOBYTE(result) = 64;
}
result = (unsigned __int8)result;
if ( timer >= (unsigned int)(unsigned __int8)result )
{
timer = 0;
result = (unsigned int)(unsigned __int8)(*((_BYTE *)&timer + 13))++ + 1;
if ( !(_BYTE)result )
{
result = *((unsigned __int8 *)&timer + 14);
*((_BYTE *)&timer + 13) = result;
mmu[258] |= 4u;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
int v1; // edi
int v2; // edx
int v3; // eax
char v4; // r10
char v5; // r9
int v6; // ecx
char v7; // si
__int64 result; // rax

v1 = *(&timer + 1) + a1;
*(&timer + 1) = v1;
v2 = timer;
v3 = *(&timer + 2);
v4 = *((_BYTE *)&timer + 12);
v5 = 0;
LABEL_2:
v6 = v1 & 3;
v7 = v5;
while ( v1 != v6 )
{
++v2;
v1 -= 4;
++v3;
v7 = 1;
if ( v3 == 16 )
{
++v4;
v3 = 0;
v5 = 1;
goto LABEL_2;
}
}
if ( v7 )
{
timer = v2;
*(&timer + 2) = v3;
*(&timer + 1) = v6;
}
if ( v5 )
*((_BYTE *)&timer + 12) = v4;
result = *((unsigned __int8 *)&timer + 15);
if ( (result & 4) != 0 )
{
LOBYTE(result) = result & 3;
if ( (_BYTE)result == 2 )
{
LOBYTE(result) = 4;
}
else if ( (_BYTE)result == 3 )
{
LOBYTE(result) = 16;
}
else if ( (_BYTE)result != 1 )
{
LOBYTE(result) = 64;
}
result = (unsigned __int8)result;
if ( timer >= (unsigned int)(unsigned __int8)result )
{
timer = 0;
result = (unsigned int)(unsigned __int8)(*((_BYTE *)&timer + 13))++ + 1;
if ( !(_BYTE)result )
{
result = *((unsigned __int8 *)&timer + 14);
*((_BYTE *)&timer + 13) = result;
mmu[258] |= 4u;
}
}
}
return result;
}

original_code:
void timer_update(Byte ticks)
{
    timer.clock.sub += ticks;
    while (timer.clock.sub >= 4)
    {
        timer.clock.main++;
        timer.clock.div++;
        timer.clock.sub -= 4;
        if (timer.clock.div == 16)
        {
            timer.regs.div++;
            timer.clock.div = 0;
        }
    }
    if (timer.regs.ctrl & 0x04)
    {
        Byte threshold;
        switch (timer.regs.ctrl & 0x03)
        {
            case 1:
                threshold = 1;
                break;
            case 2:
                threshold = 4;
                break;
            case 3:
                threshold = 16;
                break;
            default:
                threshold = 64;
                break;
        }
        if (timer.clock.main >= threshold)
        {
            timer.clock.main = 0;
            timer.regs.count++;
            if (timer.regs.count == 0)
            {
                timer.regs.count = timer.regs.mod;
                mmu.iflag |= 0x04;
            }
        }
    }
}

canonical_original_code:
void timer_update(unsigned char ticks)
{
    timer.clock.sub += ticks;
    while (timer.clock.sub >= 4)
    {
        timer.clock.main++;
        timer.clock.div++;
        timer.clock.sub -= 4;
        if (timer.clock.div == 16)
        {
            timer.regs.div++;
            timer.clock.div = 0;
        }
    }
    if (timer.regs.ctrl & 0x04)
    {
        unsigned char threshold;
        switch (timer.regs.ctrl & 0x03)
        {
            case 1:
                threshold = 1;
                break;
            case 2:
                threshold = 4;
                break;
            case 3:
                threshold = 16;
                break;
            default:
                threshold = 64;
                break;
        }
        if (timer.clock.main >= threshold)
        {
            timer.clock.main = 0;
            timer.regs.count++;
            if (timer.regs.count == 0)
            {
                timer.regs.count = timer.regs.mod;
                mmu.iflag |= 0x04;
            }
        }
    }
}

variable_types:
ticks
unsigned char

variable_types:
threshold
unsigned char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 2 )
{
SDL_Init(32LL, argv, envp);
gbe_reset(argv[1]);
gbe_run();
mbc_cleanup();
SDL_Quit();
}
else
{
fwrite("Syntax: gbe <rom-path>\n", 1uLL, 0x17uLL, stderr);
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    if (argc == 2)
    {
        SDL_Init(0x00000020);
        gbe_reset(argv[1]);
        gbe_run();
        mbc_cleanup();
        SDL_Quit();
    }
    else
    {
        fprintf(stderr, "Syntax: gbe <rom-path>\n");
    }
    return 0;
}

variable_types: argc int


decompiled_code:
__int64 __fastcall dma_transfer(int a1)
{
__int64 v2; // rbx
unsigned __int16 v3; // r12
__int64 result; // rax

v2 = (unsigned int)((_DWORD)&gpu + 8215);
do
{
*(_BYTE *)v2 = mmu_getbyte((unsigned __int16)a1);
*(_BYTE *)(v2 + 1) = mmu_getbyte((unsigned __int16)(a1 + 1));
v3 = a1 + 3;
*(_BYTE *)(v2 + 2) = mmu_getbyte((unsigned __int16)(a1 + 2));
a1 += 4;
v2 += 4LL;
result = mmu_getbyte(v3);
*(_BYTE *)(v2 - 1) = result;
}
while ( (__int64 *)v2 != (__int64 *)((char *)&gpu + 8375) );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v2; // rbx
unsigned __int16 v3; // r12
__int64 result; // rax

v2 = (unsigned int)((_DWORD)&gpu + 8215);
do
{
*(_BYTE *)v2 = mmu_getbyte((unsigned __int16)a1);
*(_BYTE *)(v2 + 1) = mmu_getbyte((unsigned __int16)(a1 + 1));
v3 = a1 + 3;
*(_BYTE *)(v2 + 2) = mmu_getbyte((unsigned __int16)(a1 + 2));
a1 += 4;
v2 += 4LL;
result = mmu_getbyte(v3);
*(_BYTE *)(v2 - 1) = result;
}
while ( (__int64 *)v2 != (__int64 *)((char *)&gpu + 8375) );
return result;
}

original_code:
void dma_transfer(Word addr)
{
    Byte *oam = gpu.oam;
    for (Byte i = 0; i < 40; i++)
    {
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
    }
}

canonical_original_code:
void dma_transfer(unsigned short addr)
{
    unsigned char *oam = gpu.oam;
    for (unsigned char i = 0; i < 40; i++)
    {
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
        *oam++ = mmu_getbyte(addr++);
    }
}

variable_types:
oam
unsigned char *

variable_types:
i
unsigned char


original_code:
void gpu_reset()
{
    gpu.screen = SDL_SetVideoMode(160, 144, 32,
            0x00000001 | 0x40000000);
    ((gpu.screen != ((void *)0)) ? (void) (0) : __assert_fail ("gpu.screen != NULL", "src/gpu.c", 28, __extension__ __PRETTY_FUNCTION__));
    gpu.mode = 0;
    gpu.modeclock = 0;
    gpu.regs.control = 0;
    gpu.regs.scrollx = 0;
    gpu.regs.scrolly = 0;
    gpu.regs.wndposx = 0;
    gpu.regs.wndposy = 0;
    gpu.regs.line = 0;
    gpu.regs.coincidence = 0;
    gpu.regs.status = 0;
    gpu.pal.bg = 0;
    gpu.pal.obj0 = 0;
    gpu.pal.obj1 = 0;
    for (Word i = 0; i < 8192; i++)
    {
        gpu.vram[i] = 0;
    }
    for (Word i = 0; i < 160; i++)
    {
        gpu.oam[i] = 0;
    }
    SDL_Rect rect = {0, 0, 160, 144};
    SDL_LockSurface(gpu.screen);
    SDL_FillRect(gpu.screen, &rect,
            SDL_MapRGBA(gpu.screen->format, 0xFF, 0xFF, 0xFF, 0xFF));
    SDL_UnlockSurface(gpu.screen);
    SDL_Flip(gpu.screen);
}

canonical_original_code:
void gpu_reset()
{
    gpu.screen = SDL_SetVideoMode(160, 144, 32,
            0x00000001 | 0x40000000);
    ((gpu.screen != ((void *)0)) ? (void) (0) : __assert_fail ("gpu.screen != NULL", "src/gpu.c", 28, __extension__ __PRETTY_FUNCTION__));
    gpu.mode = 0;
    gpu.modeclock = 0;
    gpu.regs.control = 0;
    gpu.regs.scrollx = 0;
    gpu.regs.scrolly = 0;
    gpu.regs.wndposx = 0;
    gpu.regs.wndposy = 0;
    gpu.regs.line = 0;
    gpu.regs.coincidence = 0;
    gpu.regs.status = 0;
    gpu.pal.bg = 0;
    gpu.pal.obj0 = 0;
    gpu.pal.obj1 = 0;
    for (unsigned short i = 0; i < 8192; i++)
    {
        gpu.vram[i] = 0;
    }
    for (unsigned short i = 0; i < 160; i++)
    {
        gpu.oam[i] = 0;
    }
    struct SDL_Rect rect = {0, 0, 160, 144};
    SDL_LockSurface(gpu.screen);
    SDL_FillRect(gpu.screen, &rect,
            SDL_MapRGBA(gpu.screen->format, 0xFF, 0xFF, 0xFF, 0xFF));
    SDL_UnlockSurface(gpu.screen);
    SDL_Flip(gpu.screen);
}

variable_types:
i
unsigned short

variable_types: struct SDL_Rect { short x; unsigned short w; }


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

switch ( a1 )
{
case -192:
*((_BYTE *)&gpu + 12) = a2;
break;
case -191:
result = *((_BYTE *)&gpu + 19) & 7;
*((_BYTE *)&gpu + 19) = result | a2 & 0x78;
break;
case -190:
*((_BYTE *)&gpu + 14) = a2;
break;
case -189:
*((_BYTE *)&gpu + 13) = a2;
break;
case -187:
*((_BYTE *)&gpu + 18) = a2;
break;
case -186:
result = dma_transfer((unsigned __int16)(a2 << 8));
break;
case -185:
*((_BYTE *)&gpu + 20) = a2;
break;
case -184:
*((_BYTE *)&gpu + 21) = a2;
break;
case -183:
*((_BYTE *)&gpu + 22) = a2;
break;
case -182:
*((_BYTE *)&gpu + 16) = a2;
break;
case -181:
*((_BYTE *)&gpu + 15) = a2;
break;
default:
return result;
}
return result;
}

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

switch ( a1 )
{
case -192:
*((_BYTE *)&gpu + 12) = a2;
break;
case -191:
result = *((_BYTE *)&gpu + 19) & 7;
*((_BYTE *)&gpu + 19) = result | a2 & 0x78;
break;
case -190:
*((_BYTE *)&gpu + 14) = a2;
break;
case -189:
*((_BYTE *)&gpu + 13) = a2;
break;
case -187:
*((_BYTE *)&gpu + 18) = a2;
break;
case -186:
result = func0((unsigned __int16)(a2 << 8));
break;
case -185:
*((_BYTE *)&gpu + 20) = a2;
break;
case -184:
*((_BYTE *)&gpu + 21) = a2;
break;
case -183:
*((_BYTE *)&gpu + 22) = a2;
break;
case -182:
*((_BYTE *)&gpu + 16) = a2;
break;
case -181:
*((_BYTE *)&gpu + 15) = a2;
break;
default:
return result;
}
return result;
}

original_code:
void gpu_putbyte(Word addr, Byte value)
{
    switch (addr)
    {
        case 0xFF40:
            gpu.regs.control = value;
            break;
        case 0xFF41:
            gpu.regs.status = (value & 0x78) | (gpu.regs.status & 0x07);
            break;
        case 0xFF42:
            gpu.regs.scrolly = value;
            break;
        case 0xFF43:
            gpu.regs.scrollx = value;
            break;
        case 0xFF45:
            gpu.regs.coincidence = value;
            break;
        case 0xFF46:
            dma_transfer(value << 8);
            break;
        case 0xFF47:
            gpu.pal.bg = value;
            break;
        case 0xFF48:
            gpu.pal.obj0 = value;
            break;
        case 0xFF49:
            gpu.pal.obj1 = value;
            break;
        case 0xFF4A:
            gpu.regs.wndposy = value;
            break;
        case 0xFF4B:
            gpu.regs.wndposx = value;
            break;
    }
}

canonical_original_code:
void gpu_putbyte(unsigned short addr, unsigned char value)
{
    switch (addr)
    {
        case 0xFF40:
            gpu.regs.control = value;
            break;
        case 0xFF41:
            gpu.regs.status = (value & 0x78) | (gpu.regs.status & 0x07);
            break;
        case 0xFF42:
            gpu.regs.scrolly = value;
            break;
        case 0xFF43:
            gpu.regs.scrollx = value;
            break;
        case 0xFF45:
            gpu.regs.coincidence = value;
            break;
        case 0xFF46:
            dma_transfer(value << 8);
            break;
        case 0xFF47:
            gpu.pal.bg = value;
            break;
        case 0xFF48:
            gpu.pal.obj0 = value;
            break;
        case 0xFF49:
            gpu.pal.obj1 = value;
            break;
        case 0xFF4A:
            gpu.regs.wndposy = value;
            break;
        case 0xFF4B:
            gpu.regs.wndposx = value;
            break;
    }
}


original_code:
Byte get_lcd_status()
{
    Byte status = gpu.regs.status;
    status |= (gpu.mode & 0x03);
    if (gpu.regs.line == gpu.regs.coincidence)
    {
        status |= 0x04;
    }
    return status;
}

canonical_original_code:
unsigned char get_lcd_status()
{
    unsigned char status = gpu.regs.status;
    status |= (gpu.mode & 0x03);
    if (gpu.regs.line == gpu.regs.coincidence)
    {
        status |= 0x04;
    }
    return status;
}

variable_types:
status
unsigned char


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

switch ( a1 )
{
case -192:
result = *((unsigned __int8 *)&gpu + 12);
break;
case -191:
result = get_lcd_status();
break;
case -190:
result = *((unsigned __int8 *)&gpu + 14);
break;
case -189:
result = *((unsigned __int8 *)&gpu + 13);
break;
case -188:
result = *((unsigned __int8 *)&gpu + 17);
break;
case -187:
result = *((unsigned __int8 *)&gpu + 18);
break;
case -185:
result = *((unsigned __int8 *)&gpu + 20);
break;
case -184:
result = *((unsigned __int8 *)&gpu + 21);
break;
case -183:
result = *((unsigned __int8 *)&gpu + 22);
break;
case -182:
result = *((unsigned __int8 *)&gpu + 16);
break;
case -181:
result = *((unsigned __int8 *)&gpu + 15);
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case -192:
result = *((unsigned __int8 *)&gpu + 12);
break;
case -191:
result = func3();
break;
case -190:
result = *((unsigned __int8 *)&gpu + 14);
break;
case -189:
result = *((unsigned __int8 *)&gpu + 13);
break;
case -188:
result = *((unsigned __int8 *)&gpu + 17);
break;
case -187:
result = *((unsigned __int8 *)&gpu + 18);
break;
case -185:
result = *((unsigned __int8 *)&gpu + 20);
break;
case -184:
result = *((unsigned __int8 *)&gpu + 21);
break;
case -183:
result = *((unsigned __int8 *)&gpu + 22);
break;
case -182:
result = *((unsigned __int8 *)&gpu + 16);
break;
case -181:
result = *((unsigned __int8 *)&gpu + 15);
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
Byte gpu_getbyte(Word addr)
{
    switch (addr)
    {
        case 0xFF40:
            return gpu.regs.control;
        case 0xFF41:
            return get_lcd_status();
        case 0xFF42:
            return gpu.regs.scrolly;
        case 0xFF43:
            return gpu.regs.scrollx;
        case 0xFF44:
            return gpu.regs.line;
        case 0xFF45:
            return gpu.regs.coincidence;
        case 0xFF47:
            return gpu.pal.bg;
        case 0xFF48:
            return gpu.pal.obj0;
        case 0xFF49:
            return gpu.pal.obj1;
        case 0xFF4A:
            return gpu.regs.wndposy;
        case 0xFF4B:
            return gpu.regs.wndposx;
        default:
            return 0;
    }
}

canonical_original_code:
unsigned char gpu_getbyte(unsigned short addr)
{
    switch (addr)
    {
        case 0xFF40:
            return gpu.regs.control;
        case 0xFF41:
            return get_lcd_status();
        case 0xFF42:
            return gpu.regs.scrolly;
        case 0xFF43:
            return gpu.regs.scrollx;
        case 0xFF44:
            return gpu.regs.line;
        case 0xFF45:
            return gpu.regs.coincidence;
        case 0xFF47:
            return gpu.pal.bg;
        case 0xFF48:
            return gpu.pal.obj0;
        case 0xFF49:
            return gpu.pal.obj1;
        case 0xFF4A:
            return gpu.regs.wndposy;
        case 0xFF4B:
            return gpu.regs.wndposx;
        default:
            return 0;
    }
}


original_code:
void set_mode(Byte mode)
{
    gpu.mode = mode;
    gpu.modeclock = 0;
    switch (mode)
    {
        case 0:
            if (gpu.regs.status & 0x08)
            {
                mmu.iflag |= 0x02;
            }
            break;
        case 1:
            if (gpu.regs.status & 0x10)
            {
                mmu.iflag |= 0x02;
            }
            break;
        case 2:
            if (gpu.regs.status & 0x20)
            {
                mmu.iflag |= 0x02;
            }
            break;
    }
}

canonical_original_code:
void set_mode(unsigned char mode)
{
    gpu.mode = mode;
    gpu.modeclock = 0;
    switch (mode)
    {
        case 0:
            if (gpu.regs.status & 0x08)
            {
                mmu.iflag |= 0x02;
            }
            break;
        case 1:
            if (gpu.regs.status & 0x10)
            {
                mmu.iflag |= 0x02;
            }
            break;
        case 2:
            if (gpu.regs.status & 0x20)
            {
                mmu.iflag |= 0x02;
            }
            break;
    }
}

variable_types:
mode
unsigned char


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

result = (unsigned int)(unsigned __int8)(*((_BYTE *)&gpu + 17))++ + 1;
if ( (*((_BYTE *)&gpu + 19) & 0x40) != 0 && (_BYTE)result == *((_BYTE *)&gpu + 18) )
mmu[258] |= 2u;
return result;
}

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

result = (unsigned int)(unsigned __int8)(*((_BYTE *)&gpu + 17))++ + 1;
if ( (*((_BYTE *)&gpu + 19) & 0x40) != 0 && (_BYTE)result == *((_BYTE *)&gpu + 18) )
mmu[258] |= 2u;
return result;
}

original_code:
void next_line()
{
    gpu.regs.line++;
    if ((gpu.regs.status & 0x40) &&
        gpu.regs.line == gpu.regs.coincidence)
    {
        mmu.iflag |= 0x02;
    }
}


decompiled_code:
void __fastcall gpu_step(__int16 a1)
{
unsigned __int16 v1; // di
unsigned __int8 v2; // al

v1 = *((_WORD *)&gpu + 5) + a1;
*((_WORD *)&gpu + 5) = v1;
v2 = *((_BYTE *)&gpu + 8);
if ( v2 == 2 )
{
if ( v1 > 0x13u )
set_mode(3);
}
else if ( v2 > 2u )
{
if ( v2 != 3 )
_assert_fail("false", "src/gpu.c", 0xE3u, "gpu_step");
if ( v1 > 0x2Au )
{
render_scanline();
set_mode(0);
}
}
else if ( v2 )
{
if ( v1 > 0x71u )
{
if ( *((_BYTE *)&gpu + 17) == 0x99 )
{
*((_BYTE *)&gpu + 17) = 0;
set_mode(2);
}
else
{
next_line();
*((_WORD *)&gpu + 5) = 0;
}
}
}
else if ( v1 > 0x32u )
{
if ( *((_BYTE *)&gpu + 17) == 0x8F )
{
SDL_Flip(gpu);
set_mode(1);
mmu[258] |= 1u;
}
else
{
set_mode(2);
}
next_line();
}
}

original_code:
void gpu_step(Word ticks)
{
    gpu.modeclock += ticks;
    switch (gpu.mode)
    {
        case 2:
            if (gpu.modeclock >= 20)
            {
                set_mode(3);
            }
            break;
        case 3:
            if (gpu.modeclock >= 43)
            {
                render_scanline();
                set_mode(0);
            }
            break;
        case 0:
            if (gpu.modeclock >= 51)
            {
                if (gpu.regs.line == (144 - 1))
                {
                    SDL_Flip(gpu.screen);
                    set_mode(1);
                    mmu.iflag |= 0x01;
                }
                else
                {
                    set_mode(2);
                }
                next_line();
            }
            break;
        case 1:
            if (gpu.modeclock >= 114)
            {
                if (gpu.regs.line == (144 + 9))
                {
                    gpu.regs.line = 0;
                    set_mode(2);
                }
                else
                {
                    next_line();
                    gpu.modeclock = 0;
                }
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/gpu.c", 227, __extension__ __PRETTY_FUNCTION__));
    }
}

canonical_original_code:
void gpu_step(unsigned short ticks)
{
    gpu.modeclock += ticks;
    switch (gpu.mode)
    {
        case 2:
            if (gpu.modeclock >= 20)
            {
                set_mode(3);
            }
            break;
        case 3:
            if (gpu.modeclock >= 43)
            {
                render_scanline();
                set_mode(0);
            }
            break;
        case 0:
            if (gpu.modeclock >= 51)
            {
                if (gpu.regs.line == (144 - 1))
                {
                    SDL_Flip(gpu.screen);
                    set_mode(1);
                    mmu.iflag |= 0x01;
                }
                else
                {
                    set_mode(2);
                }
                next_line();
            }
            break;
        case 1:
            if (gpu.modeclock >= 114)
            {
                if (gpu.regs.line == (144 + 9))
                {
                    gpu.regs.line = 0;
                    set_mode(2);
                }
                else
                {
                    next_line();
                    gpu.modeclock = 0;
                }
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/gpu.c", 227, __extension__ __PRETTY_FUNCTION__));
    }
}

variable_types:
ticks
unsigned short


decompiled_code:
bool is_battery()
{
return (mbc & 0xEF) == 3;
}

canonical_decompiled_code:
bool func0()
{
return (mbc & 0xEF) == 3;
}

original_code:
static inline _Bool is_battery()
{
    switch (mbc.cart_type)
    {
        case 0x03:
        case 0x13:
            return 1;
        default:
            return 0;
    }
}


decompiled_code:
void mbc_cleanup()
{
if ( is_battery() )
fclose(*((FILE **)&mbc + 5));
free(*((void **)&mbc + 3));
free(*((void **)&mbc + 1));
}

canonical_decompiled_code:
void func3()
{
if ( func0() )
fclose(*((FILE **)&mbc + 5));
free(*((void **)&mbc + 3));
free(*((void **)&mbc + 1));
}

original_code:
void mbc_cleanup()
{
    if (is_battery())
    {
        fclose(mbc.ram_file);
    }
    free(mbc.ram);
    free(mbc.rom);
}


original_code:
Byte mbc_getbyte(Word addr)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
            return mbc.rom[addr & 0x3FFF];
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            return mbc.rom[mbc.rom_offset + (addr & 0x3FFF)];
        case 0xA000:
        case 0xB000:
            (((mbc.ram_offset + (addr & 0x1FFF)) < mbc.ram_size) ? (void) (0) : __assert_fail ("(mbc.ram_offset + (addr & 0x1FFF)) < mbc.ram_size", "src/mbc.c", 182, __extension__ __PRETTY_FUNCTION__));
            return mbc.ram[mbc.ram_offset + (addr & 0x1FFF)];
        default:
            return 0;
    }
}

canonical_original_code:
unsigned char mbc_getbyte(unsigned short addr)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
            return mbc.rom[addr & 0x3FFF];
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            return mbc.rom[mbc.rom_offset + (addr & 0x3FFF)];
        case 0xA000:
        case 0xB000:
            (((mbc.ram_offset + (addr & 0x1FFF)) < mbc.ram_size) ? (void) (0) : __assert_fail ("(mbc.ram_offset + (addr & 0x1FFF)) < mbc.ram_size", "src/mbc.c", 182, __extension__ __PRETTY_FUNCTION__));
            return mbc.ram[mbc.ram_offset + (addr & 0x1FFF)];
        default:
            return 0;
    }
}


original_code:
void mbc_putbyte(Word addr, Byte value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
            switch (mbc.cart_type)
            {
                case 0x02:
                case 0x03:
                case 0x12:
                case 0x13:
                    mbc.regs.ram_on = ((value & 0x0F) == 0x0A) ? 1 : 0;
                    break;
            }
            break;
        case 0x2000:
        case 0x3000:
            switch (mbc.cart_type)
            {
                case 0x01:
                case 0x02:
                case 0x03:
                    mbc.regs.rom_bank = (mbc.regs.rom_bank & 0x60) + (value & 0x1F);
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    mbc.regs.rom_bank = value & 0x7F;
                    break;
            }
            mbc.rom_offset = (mbc.regs.rom_bank ? mbc.regs.rom_bank : 1) * 0x4000;
            break;
        case 0x4000:
        case 0x5000:
            switch (mbc.cart_type)
            {
                case 0x01:
                case 0x02:
                case 0x03:
                    if (mbc.regs.mode)
                    {
                        mbc.regs.ram_bank = value & 0x03;
                        mbc.ram_offset = mbc.regs.ram_bank * 0x2000;
                    }
                    else
                    {
                        mbc.regs.rom_bank = (mbc.regs.rom_bank & 0x1F) +
                            ((value & 0x03) << 5);
                        mbc.rom_offset = mbc.regs.rom_bank * 0x4000;
                    }
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    if (value <= 0x03)
                    {
                        mbc.regs.ram_bank = value & 0x03;
                        mbc.ram_offset = mbc.regs.ram_bank * 0x2000;
                    }
                    else
                    {
                    }
                    break;
            }
            break;
        case 0x6000:
        case 0x7000:
            switch (mbc.cart_type)
            {
                case 0x02:
                case 0x03:
                    mbc.regs.mode = value & 0x01;
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    break;
            }
            break;
        case 0xA000:
        case 0xB000:
            {
                uint32_t offset = mbc.ram_offset + (addr & 0x1FFF);
                if (offset < mbc.ram_size)
                {
                    mbc.ram[offset] = value;
                    if (is_battery())
                    {
                        fseek(mbc.ram_file, offset, 0);
                        fputc(value, mbc.ram_file);
                    }
                }
            }
            break;
    }
}

canonical_original_code:
void mbc_putbyte(unsigned short addr, unsigned char value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
            switch (mbc.cart_type)
            {
                case 0x02:
                case 0x03:
                case 0x12:
                case 0x13:
                    mbc.regs.ram_on = ((value & 0x0F) == 0x0A) ? 1 : 0;
                    break;
            }
            break;
        case 0x2000:
        case 0x3000:
            switch (mbc.cart_type)
            {
                case 0x01:
                case 0x02:
                case 0x03:
                    mbc.regs.rom_bank = (mbc.regs.rom_bank & 0x60) + (value & 0x1F);
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    mbc.regs.rom_bank = value & 0x7F;
                    break;
            }
            mbc.rom_offset = (mbc.regs.rom_bank ? mbc.regs.rom_bank : 1) * 0x4000;
            break;
        case 0x4000:
        case 0x5000:
            switch (mbc.cart_type)
            {
                case 0x01:
                case 0x02:
                case 0x03:
                    if (mbc.regs.mode)
                    {
                        mbc.regs.ram_bank = value & 0x03;
                        mbc.ram_offset = mbc.regs.ram_bank * 0x2000;
                    }
                    else
                    {
                        mbc.regs.rom_bank = (mbc.regs.rom_bank & 0x1F) +
                            ((value & 0x03) << 5);
                        mbc.rom_offset = mbc.regs.rom_bank * 0x4000;
                    }
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    if (value <= 0x03)
                    {
                        mbc.regs.ram_bank = value & 0x03;
                        mbc.ram_offset = mbc.regs.ram_bank * 0x2000;
                    }
                    else
                    {
                    }
                    break;
            }
            break;
        case 0x6000:
        case 0x7000:
            switch (mbc.cart_type)
            {
                case 0x02:
                case 0x03:
                    mbc.regs.mode = value & 0x01;
                    break;
                case 0x11:
                case 0x12:
                case 0x13:
                    break;
            }
            break;
        case 0xA000:
        case 0xB000:
            {
                unsigned int offset = mbc.ram_offset + (addr & 0x1FFF);
                if (offset < mbc.ram_size)
                {
                    mbc.ram[offset] = value;
                    if (is_battery())
                    {
                        fseek(mbc.ram_file, offset, 0);
                        fputc(value, mbc.ram_file);
                    }
                }
            }
            break;
    }
}

variable_types:
offset
unsigned int


decompiled_code:
unsigned __int64 mmu_reset()
{
unsigned __int64 v0; // rax
unsigned __int64 result; // rax

mmu[0] = 1;
mmu[257] = 0;
mmu[258] = 0;
v0 = (unsigned int)((_DWORD)mmu + 259);
do
*(_BYTE *)v0++ = 0;
while ( v0 != (_DWORD)mmu + 8451 );
result = (unsigned int)((_DWORD)mmu + 8451);
do
*(_BYTE *)result++ = 0;
while ( result != (_DWORD)mmu + 8578 );
return result;
}

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

mmu[0] = 1;
mmu[257] = 0;
mmu[258] = 0;
v0 = (unsigned int)((_DWORD)mmu + 259);
do
*(_BYTE *)v0++ = 0;
while ( v0 != (_DWORD)mmu + 8451 );
result = (unsigned int)((_DWORD)mmu + 8451);
do
*(_BYTE *)result++ = 0;
while ( result != (_DWORD)mmu + 8578 );
return result;
}

original_code:
void mmu_reset()
{
    mmu.in_bios = 1;
    mmu.ienable = 0;
    mmu.iflag = 0;
    for (Word i = 0; i < 8192; i++)
    {
        mmu.wram[i] = 0;
    }
    for (Word i = 0; i < 127; i++)
    {
        mmu.zram[i] = 0;
    }
}

canonical_original_code:
void mmu_reset()
{
    mmu.in_bios = 1;
    mmu.ienable = 0;
    mmu.iflag = 0;
    for (unsigned short i = 0; i < 8192; i++)
    {
        mmu.wram[i] = 0;
    }
    for (unsigned short i = 0; i < 127; i++)
    {
        mmu.zram[i] = 0;
    }
}


original_code:
Byte mmu_getbyte(Word addr)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
            if (mmu.in_bios)
            {
                if (addr < 0x0100)
                {
                    return mmu.bios[addr];
                }
                else if (z80.regs.pc == 0x0100)
                {
                    mmu.in_bios = 0;
                }
            }
            return mbc_getbyte(addr);
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            return mbc_getbyte(addr);
        case 0x8000:
        case 0x9000:
            return gpu.vram[addr & 0x1FFF];
        case 0xA000:
        case 0xB000:
            return mbc_getbyte(addr);
        case 0xC000:
        case 0xD000:
            return mmu.wram[addr & 0x1FFF];
        case 0xE000:
            return mmu.wram[addr & 0x1FFF];
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    return mmu.wram[addr & 0x1FFF];
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        return gpu.oam[addr & 0xFF];
                    }
                    else
                    {
                        return 0;
                    }
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        return mmu.ienable;
                    }
                    else if (addr >= 0xFF80)
                    {
                        return mmu.zram[addr & 0x7F];
                    }
                    else if (addr >= 0xFF40)
                    {
                        return gpu_getbyte(addr);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                return joypad_getbyte();
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                return timer_getbyte(addr);
                            case 0xFF0F:
                                return mmu.iflag;
                            default:
                                return 0;
                        }
                    }
            }
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 168, __extension__ __PRETTY_FUNCTION__));
            return 0;
    }
}

canonical_original_code:
unsigned char mmu_getbyte(unsigned short addr)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
            if (mmu.in_bios)
            {
                if (addr < 0x0100)
                {
                    return mmu.bios[addr];
                }
                else if (z80.regs.pc == 0x0100)
                {
                    mmu.in_bios = 0;
                }
            }
            return mbc_getbyte(addr);
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            return mbc_getbyte(addr);
        case 0x8000:
        case 0x9000:
            return gpu.vram[addr & 0x1FFF];
        case 0xA000:
        case 0xB000:
            return mbc_getbyte(addr);
        case 0xC000:
        case 0xD000:
            return mmu.wram[addr & 0x1FFF];
        case 0xE000:
            return mmu.wram[addr & 0x1FFF];
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    return mmu.wram[addr & 0x1FFF];
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        return gpu.oam[addr & 0xFF];
                    }
                    else
                    {
                        return 0;
                    }
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        return mmu.ienable;
                    }
                    else if (addr >= 0xFF80)
                    {
                        return mmu.zram[addr & 0x7F];
                    }
                    else if (addr >= 0xFF40)
                    {
                        return gpu_getbyte(addr);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                return joypad_getbyte();
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                return timer_getbyte(addr);
                            case 0xFF0F:
                                return mmu.iflag;
                            default:
                                return 0;
                        }
                    }
            }
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 168, __extension__ __PRETTY_FUNCTION__));
            return 0;
    }
}


decompiled_code:
__int64 __fastcall mmu_getword(unsigned __int16 a1, __int64 a2, __int64 a3)
{
unsigned __int8 v3; // bp
__int64 v4; // rdx

v3 = mmu_getbyte(a1, a2, a3);
return v3 + ((unsigned int)mmu_getbyte((unsigned __int16)(a1 + 1), a2, v4) << 8);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1, __int64 a2, __int64 a3)
{
unsigned __int8 v3; // bp
__int64 v4; // rdx

v3 = func1(a1, a2, a3);
return v3 + ((unsigned int)func1((unsigned __int16)(a1 + 1), a2, v4) << 8);
}

original_code:
Word mmu_getword(Word addr)
{
    return mmu_getbyte(addr) + (mmu_getbyte(addr + 1) << 8);
}

canonical_original_code:
unsigned short mmu_getword(unsigned short addr)
{
    return mmu_getbyte(addr) + (mmu_getbyte(addr + 1) << 8);
}

return_type: unsigned short


decompiled_code:
unsigned __int16 __fastcall mmu_putbyte(unsigned __int16 a1, unsigned __int8 a2)
{
unsigned __int16 result; // ax

result = a1 & 0xF000;
if ( (a1 & 0xF000) == 0xB000 )
return mbc_putbyte(a1, a2);
if ( (a1 & 0xF000u) <= 0xB000 )
{
if ( result == 0x8000 )
goto LABEL_35;
if ( result <= 0x8000u )
{
if ( result != 0x4000 )
{
if ( result <= 0x4000u )
{
if ( result != 0x2000 )
{
if ( result <= 0x2000u )
{
if ( (a1 & 0xE000) != 0 )
goto LABEL_32;
}
else if ( result != 12288 )
{
goto LABEL_32;
}
}
}
else if ( result != 24576 && (a1 & 0xD000) != 20480 )
{
goto LABEL_32;
}
}
return mbc_putbyte(a1, a2);
}
if ( result == 0x9000 )
{
LABEL_35:
gpu[(a1 & 0x1FFF) + 23] = a2;
return result;
}
if ( result != 0xA000 )
goto LABEL_32;
return mbc_putbyte(a1, a2);
}
if ( result == 0xE000 )
{
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( result <= 0xE000u )
{
result = a1 & 0xE000;
if ( (a1 & 0xE000) == 0xC000 )
{
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
LABEL_32:
_assert_fail("false", "src/mmu.c", 0x11Fu, "mmu_putbyte");
}
if ( result != 0xF000 )
goto LABEL_32;
result = a1 & 0xF00;
if ( (a1 & 0xF00) != 3584 )
{
if ( (a1 & 0xF00u) > 0xE00 )
{
if ( result == 3840 )
{
if ( a1 == 0xFFFF )
{
mmu[257] = a2;
}
else if ( a1 <= 0xFF7Fu )
{
if ( a1 > 0xFF3Fu )
return gpu_putbyte(a1, a2);
if ( a1 > 0xFF07u )
{
if ( a1 == 0xFF0F )
mmu[258] = a2;
}
else if ( a1 > 0xFF03u )
{
result = timer_putbyte(a1, a2);
}
else if ( a1 == 0xFF00 )
{
result = joypad_putbyte(a2);
}
return result;
}
mmu[(a1 & 0x7F) + 8451] = a2;
return result;
}
LABEL_42:
_assert_fail("false", "src/mmu.c", 0x11Au, "mmu_putbyte");
}
if ( result != 1792 )
{
if ( result <= 0x700u )
{
if ( result != 768 )
{
if ( result <= 0x300u )
{
if ( result != 512 && (a1 & 0xE00) != 0 )
goto LABEL_42;
}
else if ( result != 1536 )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 1024 )
goto LABEL_42;
}
}
}
else if ( result != 2816 )
{
if ( result <= 0xB00u )
{
if ( result != 2560 )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 2048 )
goto LABEL_42;
}
}
else
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 3072 )
goto LABEL_42;
}
}
}
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( a1 <= 0xFE9Fu )
gpu[(unsigned __int8)a1 + 8215] = a2;
return result;
}

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

result = a1 & 0xF000;
if ( (a1 & 0xF000) == 0xB000 )
return mbc_putbyte(a1, a2);
if ( (a1 & 0xF000u) <= 0xB000 )
{
if ( result == 0x8000 )
goto LABEL_35;
if ( result <= 0x8000u )
{
if ( result != 0x4000 )
{
if ( result <= 0x4000u )
{
if ( result != 0x2000 )
{
if ( result <= 0x2000u )
{
if ( (a1 & 0xE000) != 0 )
goto LABEL_32;
}
else if ( result != 12288 )
{
goto LABEL_32;
}
}
}
else if ( result != 24576 && (a1 & 0xD000) != 20480 )
{
goto LABEL_32;
}
}
return mbc_putbyte(a1, a2);
}
if ( result == 0x9000 )
{
LABEL_35:
gpu[(a1 & 0x1FFF) + 23] = a2;
return result;
}
if ( result != 0xA000 )
goto LABEL_32;
return mbc_putbyte(a1, a2);
}
if ( result == 0xE000 )
{
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( result <= 0xE000u )
{
result = a1 & 0xE000;
if ( (a1 & 0xE000) == 0xC000 )
{
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
LABEL_32:
_assert_fail("false", "src/mmu.c", 0x11Fu, "mmu_putbyte");
}
if ( result != 0xF000 )
goto LABEL_32;
result = a1 & 0xF00;
if ( (a1 & 0xF00) != 3584 )
{
if ( (a1 & 0xF00u) > 0xE00 )
{
if ( result == 3840 )
{
if ( a1 == 0xFFFF )
{
mmu[257] = a2;
}
else if ( a1 <= 0xFF7Fu )
{
if ( a1 > 0xFF3Fu )
return gpu_putbyte(a1, a2);
if ( a1 > 0xFF07u )
{
if ( a1 == 0xFF0F )
mmu[258] = a2;
}
else if ( a1 > 0xFF03u )
{
result = timer_putbyte(a1, a2);
}
else if ( a1 == 0xFF00 )
{
result = joypad_putbyte(a2);
}
return result;
}
mmu[(a1 & 0x7F) + 8451] = a2;
return result;
}
LABEL_42:
_assert_fail("false", "src/mmu.c", 0x11Au, "mmu_putbyte");
}
if ( result != 1792 )
{
if ( result <= 0x700u )
{
if ( result != 768 )
{
if ( result <= 0x300u )
{
if ( result != 512 && (a1 & 0xE00) != 0 )
goto LABEL_42;
}
else if ( result != 1536 )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 1024 )
goto LABEL_42;
}
}
}
else if ( result != 2816 )
{
if ( result <= 0xB00u )
{
if ( result != 2560 )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 2048 )
goto LABEL_42;
}
}
else
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 3072 )
goto LABEL_42;
}
}
}
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( a1 <= 0xFE9Fu )
gpu[(unsigned __int8)a1 + 8215] = a2;
return result;
}

original_code:
void mmu_putbyte(Word addr, Byte value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            mbc_putbyte(addr, value);
            break;
        case 0x8000:
        case 0x9000:
            gpu.vram[addr & 0x1FFF] = value;
            break;
        case 0xA000:
        case 0xB000:
            mbc_putbyte(addr, value);
            break;
        case 0xC000:
        case 0xD000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xE000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    mmu.wram[addr & 0x1FFF] = value;
                    break;
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        gpu.oam[addr & 0xFF] = value;
                    }
                    break;
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        mmu.ienable = value;
                    }
                    if (addr >= 0xFF80)
                    {
                        mmu.zram[addr & 0x7F] = value;
                    }
                    else if (addr >= 0xFF40)
                    {
                        gpu_putbyte(addr, value);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                joypad_putbyte(value);
                                break;
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                timer_putbyte(addr, value);
                                break;
                            case 0xFF0F:
                                mmu.iflag = value;
                                break;
                        }
                    }
                    break;
                default:
                    ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 282, __extension__ __PRETTY_FUNCTION__));
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 287, __extension__ __PRETTY_FUNCTION__));
    }
}

canonical_original_code:
void mmu_putbyte(unsigned short addr, unsigned char value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            mbc_putbyte(addr, value);
            break;
        case 0x8000:
        case 0x9000:
            gpu.vram[addr & 0x1FFF] = value;
            break;
        case 0xA000:
        case 0xB000:
            mbc_putbyte(addr, value);
            break;
        case 0xC000:
        case 0xD000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xE000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    mmu.wram[addr & 0x1FFF] = value;
                    break;
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        gpu.oam[addr & 0xFF] = value;
                    }
                    break;
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        mmu.ienable = value;
                    }
                    if (addr >= 0xFF80)
                    {
                        mmu.zram[addr & 0x7F] = value;
                    }
                    else if (addr >= 0xFF40)
                    {
                        gpu_putbyte(addr, value);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                joypad_putbyte(value);
                                break;
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                timer_putbyte(addr, value);
                                break;
                            case 0xFF0F:
                                mmu.iflag = value;
                                break;
                        }
                    }
                    break;
                default:
                    ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 282, __extension__ __PRETTY_FUNCTION__));
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 287, __extension__ __PRETTY_FUNCTION__));
    }
}


decompiled_code:
unsigned __int16 __fastcall mmu_putword(unsigned __int16 a1, __int16 a2)
{
mmu_putbyte(a1, a2);
return mmu_putbyte(a1 + 1, HIBYTE(a2));
}

canonical_decompiled_code:
unsigned __int16 __fastcall func4(unsigned __int16 a1, __int16 a2)
{
func3(a1, a2);
return func3(a1 + 1, HIBYTE(a2));
}

original_code:
void mmu_putword(Word addr, Word value)
{
    mmu_putbyte(addr, value & 0xFF);
    mmu_putbyte(addr + 1, value >> 8);
}

canonical_original_code:
void mmu_putword(unsigned short addr, unsigned short value)
{
    mmu_putbyte(addr, value & 0xFF);
    mmu_putbyte(addr + 1, value >> 8);
}

variable_types:
value
unsigned short


decompiled_code:
__int64 __fastcall gbe_reset(__int64 a1)
{
gpu_reset();
mmu_reset();
z80_reset();
return mbc_load(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
gpu_reset();
mmu_reset();
z80_reset();
return mbc_load(a1);
}

original_code:
void gbe_reset(const char *filename)
{
    gpu_reset();
    mmu_reset();
    z80_reset();
    mbc_load(filename);
}

canonical_original_code:
void gbe_reset(char *filename)
{
    gpu_reset();
    mmu_reset();
    z80_reset();
    mbc_load(filename);
}

variable_types:
filename
char *


decompiled_code:
void joypad_reset()
{
joypad[0] = 0;
joypad[1] = 15;
joypad[2] = 15;
}

canonical_decompiled_code:
void func0()
{
joypad[0] = 0;
joypad[1] = 15;
joypad[2] = 15;
}

original_code:
void joypad_reset()
{
    joypad.column = 0;
    joypad.dpad = 0xF;
    joypad.button = 0xF;
}


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

result = (unsigned __int8)joypad[2];
if ( joypad[0] != 16 )
{
result = (unsigned __int8)joypad[1];
if ( joypad[0] != 32 )
result = 0LL;
}
return result;
}

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

result = (unsigned __int8)joypad[2];
if ( joypad[0] != 16 )
{
result = (unsigned __int8)joypad[1];
if ( joypad[0] != 32 )
result = 0LL;
}
return result;
}

original_code:
Byte joypad_getbyte()
{
    switch (joypad.column)
    {
        case 0x20:
            return joypad.dpad;
        case 0x10:
            return joypad.button;
        default:
            return 0;
    }
}

canonical_original_code:
unsigned char joypad_getbyte()
{
    switch (joypad.column)
    {
        case 0x20:
            return joypad.dpad;
        case 0x10:
            return joypad.button;
        default:
            return 0;
    }
}


decompiled_code:
void __fastcall joypad_putbyte(char a1)
{
joypad[0] = a1 & 0x30;
}

canonical_decompiled_code:
void __fastcall func2(char a1)
{
joypad[0] = a1 & 0x30;
}

original_code:
void joypad_putbyte(Byte value)
{
    joypad.column = value & 0x30;
}

canonical_original_code:
void joypad_putbyte(unsigned char value)
{
    joypad.column = value & 0x30;
}


decompiled_code:
void __fastcall joypad_keydown(unsigned int a1)
{
if ( a1 == 273 )
{
joypad[1] &= 0xFBu;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
joypad[1] &= 0xFEu;
break;
case 0x114u:
joypad[1] &= 0xFDu;
break;
case 0x112u:
joypad[1] &= 0xF7u;
break;
}
}
else if ( a1 == 120 )
{
joypad[2] &= 0xFDu;
}
else if ( a1 <= 0x78 )
{
if ( a1 == 13 )
{
joypad[2] &= 0xF7u;
}
else if ( a1 == 32 )
{
joypad[2] &= 0xFBu;
}
}
else if ( a1 == 122 )
{
joypad[2] &= 0xFEu;
}
mmu[258] |= 0x10u;
}

canonical_decompiled_code:
void __fastcall func3(unsigned int a1)
{
if ( a1 == 273 )
{
joypad[1] &= 0xFBu;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
joypad[1] &= 0xFEu;
break;
case 0x114u:
joypad[1] &= 0xFDu;
break;
case 0x112u:
joypad[1] &= 0xF7u;
break;
}
}
else if ( a1 == 120 )
{
joypad[2] &= 0xFDu;
}
else if ( a1 <= 0x78 )
{
if ( a1 == 13 )
{
joypad[2] &= 0xF7u;
}
else if ( a1 == 32 )
{
joypad[2] &= 0xFBu;
}
}
else if ( a1 == 122 )
{
joypad[2] &= 0xFEu;
}
mmu[258] |= 0x10u;
}

original_code:
void joypad_keydown(SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad &= ~0x08;
            break;
        case SDLK_UP:
            joypad.dpad &= ~0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad &= ~0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad &= ~0x01;
            break;
        case SDLK_RETURN:
            joypad.button &= ~0x08;
            break;
        case SDLK_SPACE:
            joypad.button &= ~0x04;
            break;
        case SDLK_x:
            joypad.button &= ~0x02;
            break;
        case SDLK_z:
            joypad.button &= ~0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

canonical_original_code:
void joypad_keydown(enum SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad &= ~0x08;
            break;
        case SDLK_UP:
            joypad.dpad &= ~0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad &= ~0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad &= ~0x01;
            break;
        case SDLK_RETURN:
            joypad.button &= ~0x08;
            break;
        case SDLK_SPACE:
            joypad.button &= ~0x04;
            break;
        case SDLK_x:
            joypad.button &= ~0x02;
            break;
        case SDLK_z:
            joypad.button &= ~0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

variable_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
void __fastcall joypad_keyup(unsigned int a1)
{
if ( a1 == 273 )
{
joypad[1] |= 4u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
joypad[1] |= 1u;
break;
case 0x114u:
joypad[1] |= 2u;
break;
case 0x112u:
joypad[1] |= 8u;
break;
}
}
else if ( a1 == 120 )
{
joypad[2] |= 2u;
}
else if ( a1 <= 0x78 )
{
if ( a1 == 13 )
{
joypad[2] |= 8u;
}
else if ( a1 == 32 )
{
joypad[2] |= 4u;
}
}
else if ( a1 == 122 )
{
joypad[2] |= 1u;
}
mmu[258] |= 0x10u;
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1)
{
if ( a1 == 273 )
{
joypad[1] |= 4u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
joypad[1] |= 1u;
break;
case 0x114u:
joypad[1] |= 2u;
break;
case 0x112u:
joypad[1] |= 8u;
break;
}
}
else if ( a1 == 120 )
{
joypad[2] |= 2u;
}
else if ( a1 <= 0x78 )
{
if ( a1 == 13 )
{
joypad[2] |= 8u;
}
else if ( a1 == 32 )
{
joypad[2] |= 4u;
}
}
else if ( a1 == 122 )
{
joypad[2] |= 1u;
}
mmu[258] |= 0x10u;
}

original_code:
void joypad_keyup(SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad |= 0x08;
            break;
        case SDLK_UP:
            joypad.dpad |= 0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad |= 0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad |= 0x01;
            break;
        case SDLK_RETURN:
            joypad.button |= 0x08;
            break;
        case SDLK_SPACE:
            joypad.button |= 0x04;
            break;
        case SDLK_x:
            joypad.button |= 0x02;
            break;
        case SDLK_z:
            joypad.button |= 0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

canonical_original_code:
void joypad_keyup(enum SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad |= 0x08;
            break;
        case SDLK_UP:
            joypad.dpad |= 0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad |= 0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad |= 0x01;
            break;
        case SDLK_RETURN:
            joypad.button |= 0x08;
            break;
        case SDLK_SPACE:
            joypad.button |= 0x04;
            break;
        case SDLK_x:
            joypad.button |= 0x02;
            break;
        case SDLK_z:
            joypad.button |= 0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

user_defined_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


repo: jlmjlm/atc-ai

decompiled_code:
int scroll_down()
{
int result; // eax
int v1; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * (sr_start + 1)),
(const void *)(display + screen_width * (sr_start + 1) - screen_width),
sr_end * screen_width - (__int64)(screen_width * (sr_start + 1) - screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
*(_BYTE *)(display + result + sr_start * v1) = 32;
++result;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled down. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
int v1; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * (sr_start + 1)),
(const void *)(display + screen_width * (sr_start + 1) - screen_width),
sr_end * screen_width - (__int64)(screen_width * (sr_start + 1) - screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
*(_BYTE *)(display + result + sr_start * v1) = 32;
++result;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled down. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

original_code:
static void scroll_down() {
    char *start = &(display[(sr_start+1)*screen_width + (0)]);
    char *from = &(display[(sr_start)*screen_width + (0)]);
    char *end = &(display[(sr_end)*screen_width + (0)]);
    memmove(start, from, end-from);
    for (int i = 0; i < screen_width; i++)
        (display[(sr_start)*screen_width + (i)]) = ' ';
    if (verbose) {
        fprintf(logff, "Scrolled down.  New display:\n%.*s\n",
                screen_height*screen_width, display);
    }
}

variable_types:
start
char *

variable_types:
from
char *

variable_types:
end
char *

variable_types: i int


decompiled_code:
int scroll_up()
{
int result; // eax
int v1; // edx

result = (unsigned int)memmove(
(void *)(display + sr_start * screen_width),
(const void *)(display + screen_width + sr_start * screen_width),
sr_end * screen_width - (__int64)(sr_start * screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
*(_BYTE *)(display + result + sr_end * v1) = 32;
++result;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled up. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

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

result = (unsigned int)memmove(
(void *)(display + sr_start * screen_width),
(const void *)(display + screen_width + sr_start * screen_width),
sr_end * screen_width - (__int64)(sr_start * screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
*(_BYTE *)(display + result + sr_end * v1) = 32;
++result;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled up. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

original_code:
static void scroll_up() {
    char *start = &(display[(sr_start)*screen_width + (0)]);
    char *from = &(display[(sr_start+1)*screen_width + (0)]);
    char *end = &(display[(sr_end)*screen_width + (0)]);
    memmove(start, from, end-start);
    for (int i = 0; i < screen_width; i++)
        (display[(sr_end)*screen_width + (i)]) = ' ';
    if (verbose) {
        fprintf(logff, "Scrolled up.  New display:\n%.*s\n",
                screen_height*screen_width, display);
    }
}