decompiled_code:
__int64 __fastcall print_registers(unsigned int *a1, unsigned int *a2)
{
vty_printf(" eflags=%08x", a2[2]);
vty_puts("\n");
vty_printf(" eax=%08x", a1[12]);
vty_printf(" ebx=%08x", a1[9]);
vty_printf(" ecx=%08x", a1[11]);
vty_printf(" edx=%08x", a1[10]);
vty_puts("\n");
vty_printf(" ebp=%08x", a1[7]);
vty_printf(" esi=%08x", a1[6]);
vty_printf(" edi=%08x", a1[5]);
vty_puts("\n");
vty_printf(" cs:eip=%04x:%08x", a2[1], *a2);
vty_printf(" ss:esp=%04x:%08x", a1[3], a1[8]);
vty_puts("\n");
vty_printf(" ds=%04x", a1[4]);
vty_printf(" es=%04x", a1[2]);
vty_printf(" fs=%04x", a1[1]);
vty_printf(" gs=%04x", *a1);
return vty_puts("\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, unsigned int *a2)
{
vty_printf(" eflags=%08x", a2[2]);
vty_puts("\n");
vty_printf(" eax=%08x", a1[12]);
vty_printf(" ebx=%08x", a1[9]);
vty_printf(" ecx=%08x", a1[11]);
vty_printf(" edx=%08x", a1[10]);
vty_puts("\n");
vty_printf(" ebp=%08x", a1[7]);
vty_printf(" esi=%08x", a1[6]);
vty_printf(" edi=%08x", a1[5]);
vty_puts("\n");
vty_printf(" cs:eip=%04x:%08x", a2[1], *a2);
vty_printf(" ss:esp=%04x:%08x", a1[3], a1[8]);
vty_puts("\n");
vty_printf(" ds=%04x", a1[4]);
vty_printf(" es=%04x", a1[2]);
vty_printf(" fs=%04x", a1[1]);
vty_printf(" gs=%04x", *a1);
return vty_puts("\n");
}

original_code:
static void print_registers(const StubRegisters * stub_registers,
                            const TrapRegisters * trap_registers) {
  vty_printf(" eflags=%08x", trap_registers->eflags);
  vty_puts("\n");
  vty_printf(" eax=%08x", stub_registers->eax);
  vty_printf(" ebx=%08x", stub_registers->ebx);
  vty_printf(" ecx=%08x", stub_registers->ecx);
  vty_printf(" edx=%08x", stub_registers->edx);
  vty_puts("\n");
  vty_printf(" ebp=%08x", stub_registers->ebp);
  vty_printf(" esi=%08x", stub_registers->esi);
  vty_printf(" edi=%08x", stub_registers->edi);
  vty_puts("\n");
  vty_printf(" cs:eip=%04x:%08x", trap_registers->cs, trap_registers->eip);
  vty_printf(" ss:esp=%04x:%08x", stub_registers->ss, stub_registers->esp);
  vty_puts("\n");
  vty_printf(" ds=%04x", stub_registers->ds);
  vty_printf(" es=%04x", stub_registers->es);
  vty_printf(" fs=%04x", stub_registers->fs);
  vty_printf(" gs=%04x", stub_registers->gs);
  vty_puts("\n");
}

canonical_original_code:
static void print_registers(struct StubRegisters *stub_registers, struct TrapRegisters *trap_registers) {
  vty_printf(" eflags=%08x", trap_registers->eflags);
  vty_puts("\n");
  vty_printf(" eax=%08x", stub_registers->eax);
  vty_printf(" ebx=%08x", stub_registers->ebx);
  vty_printf(" ecx=%08x", stub_registers->ecx);
  vty_printf(" edx=%08x", stub_registers->edx);
  vty_puts("\n");
  vty_printf(" ebp=%08x", stub_registers->ebp);
  vty_printf(" esi=%08x", stub_registers->esi);
  vty_printf(" edi=%08x", stub_registers->edi);
  vty_puts("\n");
  vty_printf(" cs:eip=%04x:%08x", trap_registers->cs, trap_registers->eip);
  vty_printf(" ss:esp=%04x:%08x", stub_registers->ss, stub_registers->esp);
  vty_puts("\n");
  vty_printf(" ds=%04x", stub_registers->ds);
  vty_printf(" es=%04x", stub_registers->es);
  vty_printf(" fs=%04x", stub_registers->fs);
  vty_printf(" gs=%04x", stub_registers->gs);
  vty_puts("\n");
}

variable_types: struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } *

variable_types: struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } *


repo: wconrad/brazzle

decompiled_code:
__int64 __fastcall print_trap_with_error(const char *a1, unsigned int *a2)
{
vty_printf("\n%s fault %04x\n", a1, a2[13]);
return print_registers(a2, a2 + 14);
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, unsigned int *a2)
{
vty_printf("\n%s fault %04x\n", a1, a2[13]);
return func0(a2, a2 + 14);
}

original_code:
static void print_trap_with_error(const char * name,
                           TrapParamsWithError * params) {
  vty_printf("\n%s fault %04x\n", name, params->error_code);
  print_registers(&params->stub_registers, &params->trap_registers);
}

canonical_original_code:
static void print_trap_with_error(char *name, struct TrapParamsWithError *params) {
  vty_printf("\n%s fault %04x\n", name, params->error_code);
  print_registers(&params->stub_registers, &params->trap_registers);
}

variable_types:
name
char *

variable_types: struct TrapParamsWithError { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; unsigned int error_code; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; } *

return_type: void

user_defined_types: struct TrapParamsWithError { struct StubRegisters stub_registers; unsigned int error_code; struct TrapRegisters trap_registers; }


decompiled_code:
__int64 load_idt()
{
__int16 v1; // [rsp+6h] [rbp-12h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1 = 135;
v2 = get_physaddr(&idt);
return lidt(&v1);
}

canonical_decompiled_code:
__int64 func2()
{
__int16 v1; // [rsp+6h] [rbp-12h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1 = 135;
v2 = get_physaddr(&idt);
return lidt(&v1);
}

original_code:
static void load_idt() {
  IdtAddr idt_addr = {
    .addr = get_physaddr(idt),
    .size = sizeof(idt) - 1,
  };
  lidt(&idt_addr);
}

canonical_original_code:
static void load_idt() {
  struct idt_addr idt_addr = {
    .addr = get_physaddr(idt),
    .size = sizeof(idt) - 1,
  };
  lidt(&idt_addr);
}

variable_types: struct idt_addr { unsigned short size; void *addr; }


decompiled_code:
__int64 __fastcall int3_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap_constprop_0((unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func3((unsigned int *)&a7);
return halt();
}

original_code:
void int3_handler(TrapParams params) {
  print_trap("BP", &params);
  halt();
}

canonical_original_code:
void int3_handler(struct TrapParams params) {
  print_trap("BP", &params);
  halt();
}

variable_types: struct TrapParams { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; }

user_defined_types: struct TrapParams { struct StubRegisters stub_registers; struct TrapRegisters trap_registers; }


decompiled_code:
__int64 __fastcall int8_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap_with_error("DF", (unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func1("DF", (unsigned int *)&a7);
return halt();
}

original_code:
void int8_handler(TrapParamsWithError params) {
  print_trap_with_error("DF", &params);
  halt();
}

canonical_original_code:
void int8_handler(struct TrapParamsWithError params) {
  print_trap_with_error("DF", &params);
  halt();
}

variable_types: struct TrapParamsWithError { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; unsigned int error_code; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; }


decompiled_code:
__int64 __fastcall int13_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap_with_error("GP", (unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func1("GP", (unsigned int *)&a7);
return halt();
}

original_code:
void int13_handler(TrapParamsWithError params) {
  print_trap_with_error("GP", &params);
  halt();
}

canonical_original_code:
void int13_handler(struct TrapParamsWithError params) {
  print_trap_with_error("GP", &params);
  halt();
}

user_defined_types: struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; }

user_defined_types: struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; }


decompiled_code:
__int64 int_init()
{
set_idt_entry(3LL, &int3_stub, 143LL);
set_idt_entry(8LL, &int8_stub, 143LL);
set_idt_entry(13LL, &int13_stub, 143LL);
return load_idt();
}

canonical_decompiled_code:
__int64 func7()
{
set_idt_entry(3LL, &int3_stub, 143LL);
set_idt_entry(8LL, &int8_stub, 143LL);
set_idt_entry(13LL, &int13_stub, 143LL);
return func2();
}


utoa

decompiled_code:
__int64 __fastcall utoa(__int64 *a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
__int64 v5; // r13
unsigned int v6; // eax

v4 = a2;
v5 = *a1;
if ( a2 )
{
do
{
buffwrite(a1, (unsigned int)byte_181[v4 % a3]);
v6 = v4;
v4 /= a3;
}
while ( a3 <= v6 );
}
else
{
buffwrite(a1, 48LL);
}
buffwrite(a1, 0LL);
return strrev(v5);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
__int64 v5; // r13
unsigned int v6; // eax

v4 = a2;
v5 = *a1;
if ( a2 )
{
do
{
buffwrite(a1, (unsigned int)byte_181[v4 % a3]);
v6 = v4;
v4 /= a3;
}
while ( a3 <= v6 );
}
else
{
buffwrite(a1, 48LL);
}
buffwrite(a1, 0LL);
return strrev(v5);
}

original_code:
static void utoa(BufferCursor * cursor,
                 unsigned n,
                 int radix) {
  char * start = cursor->p;
  if(n == 0)
    buffwrite(cursor, '0');
  else
    while(n != 0) {
      char c = "0123456789abcdef"[n % radix];
      buffwrite(cursor, c);
      n /= radix;
    }
  buffwrite(cursor, '\0');
  strrev(start);
}

canonical_original_code:
static void utoa(struct BufferCursor *cursor, unsigned int n, int radix) {
  char *start = cursor->p;
  if(n == 0)
    buffwrite(cursor, '0');
  else
    while(n != 0) {
      char c = "0123456789abcdef"[n % radix];
      buffwrite(cursor, c);
      n /= radix;
    }
  buffwrite(cursor, '\0');
  strrev(start);
}

variable_types: struct BufferCursor { char *p; int remaining; } *

variable_types:
n
unsigned int

variable_types: radix int

variable_types:
start
char *

variable_types: c char


utodec

decompiled_code:
__int64 __fastcall utodec(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 0xAu);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 0xAu);
return a1;
}

original_code:
char * utodec(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 10);
  return buffer;
}

canonical_original_code:
char *utodec(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 10);
  return buffer;
}

variable_types:
buffer
char *

variable_types:
buflen
unsigned long

variable_types: struct BufferCursor { char *p; int remaining; }

return_type: char *


utohex

decompiled_code:
__int64 __fastcall utohex(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 0x10u);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 0x10u);
return a1;
}

original_code:
char * utohex(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 16);
  return buffer;
}

canonical_original_code:
char *utohex(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 16);
  return buffer;
}

user_defined_types: struct BufferCursor { char *p; int remaining; }


utobin

decompiled_code:
__int64 __fastcall utobin(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 2u);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 2u);
return a1;
}

original_code:
char * utobin(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 2);
  return buffer;
}

canonical_original_code:
char *utobin(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 2);
  return buffer;
}


itodec

decompiled_code:
__int64 __fastcall itodec(__int64 a1, int a2, int a3)
{
unsigned int v3; // ebp
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
int v6; // [rsp+8h] [rbp-20h]

v3 = a3;
v5 = a1;
v6 = a2;
if ( a3 < 0 )
{
v3 = -a3;
buffwrite(&v5, 45LL);
}
utoa(&v5, v3, 0xAu);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, int a3)
{
unsigned int v3; // ebp
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
int v6; // [rsp+8h] [rbp-20h]

v3 = a3;
v5 = a1;
v6 = a2;
if ( a3 < 0 )
{
v3 = -a3;
buffwrite(&v5, 45LL);
}
func0(&v5, v3, 0xAu);
return a1;
}

original_code:
char * itodec(char * buffer, size_t buflen, int n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  unsigned u;
  if(n < 0) {
    buffwrite(&cursor, '-');
    u = -n;
  }
  else
    u = n;
  utoa(&cursor, u, 10);
  return buffer;
}

canonical_original_code:
char *itodec(char *buffer, unsigned long buflen, int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  unsigned int u;
  if(n < 0) {
    buffwrite(&cursor, '-');
    u = -n;
  }
  else
    u = n;
  utoa(&cursor, u, 10);
  return buffer;
}

variable_types: n int

variable_types:
u
unsigned int


dectou

decompiled_code:
__int64 __fastcall dectou(_BYTE *a1, _QWORD *a2)
{
int v2; // eax
unsigned int i; // er8

v2 = (char)*a1;
for ( i = 0; (unsigned __int8)(*a1 - 48) <= 9u; v2 = (char)*a1 )
{
++a1;
i = v2 + 10 * i - 48;
}
*a2 = a1;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func5(_BYTE *a1, _QWORD *a2)
{
int v2; // eax
unsigned int i; // er8

v2 = (char)*a1;
for ( i = 0; (unsigned __int8)(*a1 - 48) <= 9u; v2 = (char)*a1 )
{
++a1;
i = v2 + 10 * i - 48;
}
*a2 = a1;
return i;
}

original_code:
unsigned dectou(const char *buffer, const char **endptr) {
  const char * p = buffer;
  unsigned n = 0;
  for(;;) {
    char c = *p;
    if(c < '0' || c > '9')
      break;
    p++;
    n = 10 * n + (c - '0');
  }
  *endptr = p;
  return n;
}

canonical_original_code:
unsigned int dectou(char *buffer, char **endptr) {
  char *p = buffer;
  unsigned int n = 0;
  for(;;) {
    char c = *p;
    if(c < '0' || c > '9')
      break;
    p++;
    n = 10 * n + (c - '0');
  }
  *endptr = p;
  return n;
}

variable_types:
endptr
char **

variable_types:
p
char *

return_type: unsigned int


decompiled_code:
__int64 __fastcall page_table(int a1)
{
return 4LL * (a1 << 10) + 4290772992LL;
}

original_code:
static uint32_t *page_table(int n) {
  return ((uint32_t *) ((VirtualAddress) 0xffc00000)) + (0x400 * n);
}

canonical_original_code:
static unsigned int *page_table(int n) {
  return ((unsigned int *) ((void *) 0xffc00000)) + (0x400 * n);
}

return_type: unsigned int *


decompiled_code:
__int64 __fastcall page_directory_index(unsigned int a1)
{
return a1 >> 22;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return a1 >> 22;
}

original_code:
inline unsigned page_directory_index(VirtualAddress addr) {
  return (unsigned) addr >> 22;
}

canonical_original_code:
inline unsigned int page_directory_index(void *addr) {
  return (unsigned int) addr >> 22;
}

variable_types:
addr
void *


decompiled_code:
__int64 __fastcall page_table_index(unsigned int a1)
{
return (a1 >> 12) & 0x3FF;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
return (a1 >> 12) & 0x3FF;
}

original_code:
inline unsigned page_table_index(VirtualAddress addr) {
  return (unsigned) addr >> 12 & 0x03FF;
}

canonical_original_code:
inline unsigned int page_table_index(void *addr) {
  return (unsigned int) addr >> 12 & 0x03FF;
}


decompiled_code:
__int64 __fastcall page_offset(__int16 a1)
{
return a1 & 0xFFF;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return a1 & 0xFFF;
}

original_code:
inline unsigned page_offset(VirtualAddress addr) {
  return (unsigned) addr & 0xfff;
}

canonical_original_code:
inline unsigned int page_offset(void *addr) {
  return (unsigned int) addr & 0xfff;
}


decompiled_code:
__int64 __fastcall get_physaddr(unsigned int a1)
{
__int64 v1; // rcx
__int64 v2; // rax
__int16 v3; // r8
unsigned int v4; // edx
int v5; // eax
int v6; // edx

page_directory_index(a1);
page_table_index(a1);
if ( (*(_BYTE *)(4 * v1 + 0xFFFFF000LL) & 1) == 0 )
return 0LL;
v2 = page_table(v1);
if ( (*(_DWORD *)(v2 + 4LL * v4) & 1) == 0 )
return 0LL;
v5 = page_offset(v3);
return (unsigned int)(v6 + v5);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 v1; // rcx
__int64 v2; // rax
__int16 v3; // r8
unsigned int v4; // edx
int v5; // eax
int v6; // edx

func1(a1);
func2(a1);
if ( (*(_BYTE *)(4 * v1 + 0xFFFFF000LL) & 1) == 0 )
return 0LL;
v2 = func0(v1);
if ( (*(_DWORD *)(v2 + 4LL * v4) & 1) == 0 )
return 0LL;
v5 = func3(v3);
return (unsigned int)(v6 + v5);
}

original_code:
PhysicalAddress get_physaddr(VirtualAddress virt_addr)
{
  unsigned pdindex = page_directory_index(virt_addr);
  unsigned ptindex = page_table_index(virt_addr);
  unsigned offset = page_offset(virt_addr);
  unsigned * pd = ((VirtualAddress) 0xfffff000);
  if(!(pd[pdindex] & (1 << 0)))
     return 0;
  uint32_t * pt = page_table(pdindex);
  if(!(pt[ptindex] & (1 << 0)))
     return 0;
  uint32_t page_address = pt[ptindex] & 0xfffff000;
  return (void *) (page_address + offset);
}

canonical_original_code:
void *get_physaddr(void *virt_addr)
{
  unsigned int pdindex = page_directory_index(virt_addr);
  unsigned int ptindex = page_table_index(virt_addr);
  unsigned int offset = page_offset(virt_addr);
  unsigned int *pd = ((VirtualAddress) 0xfffff000);
  if(!(pd[pdindex] & (1 << 0)))
     return 0;
  unsigned int *pt = page_table(pdindex);
  if(!(pt[ptindex] & (1 << 0)))
     return 0;
  unsigned int page_address = pt[ptindex] & 0xfffff000;
  return (void *) (page_address + offset);
}

variable_types:
pdindex
unsigned int

variable_types:
ptindex
unsigned int

variable_types:
offset
unsigned int

variable_types:
pd
unsigned int *

variable_types:
pt
unsigned int *

return_type: void *


decompiled_code:
__int64 __fastcall page_table_phys_addr(unsigned int a1)
{
unsigned int v1; // eax

if ( a1 > 0x3FF )
return 0LL;
v1 = page_table(a1);
return get_physaddr(v1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
unsigned int v1; // eax

if ( a1 > 0x3FF )
return 0LL;
v1 = func0(a1);
return func4(v1);
}

original_code:
PhysicalAddress page_table_phys_addr(int n) {
  if (n < 0 || n >= 0x400)
    return 0;
  return get_physaddr(page_table(n));
}

canonical_original_code:
void *page_table_phys_addr(int n) {
  if (n < 0 || n >= 0x400)
    return 0;
  return get_physaddr(page_table(n));
}


decompiled_code:
bool __fastcall valid_block_number(unsigned int a1)
{
return a1 <= 0xFFFFF;
}

canonical_decompiled_code:
bool __fastcall func0(unsigned int a1)
{
return a1 <= 0xFFFFF;
}

original_code:
static _Bool valid_block_number(int block_number) {
  return block_number >= 0 && block_number < 0x00100000;
}


decompiled_code:
__int64 __fastcall pmmap_index(int a1)
{
return (unsigned int)(a1 / 32);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(a1 / 32);
}

original_code:
static int pmmap_index(int block_number) {
  return block_number / 32;
}

return_type: int


decompiled_code:
__int64 __fastcall pmmap_bitnum(int a1)
{
return (unsigned int)(a1 % 32);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return (unsigned int)(a1 % 32);
}

original_code:
static int pmmap_bitnum(int block_number) {
  return block_number % 32;
}


decompiled_code:
char __fastcall pmmap_set(unsigned int a1)
{
int v1; // eax
int v2; // edx
char v3; // cl

LOBYTE(v1) = valid_block_number(a1);
if ( (_BYTE)v1 )
{
pmmap_bitnum(a1);
v1 = pmmap_index(a1);
pmmap[v1] |= v2 << v3;
}
return v1;
}

canonical_decompiled_code:
char __fastcall func3(unsigned int a1)
{
int v1; // eax
int v2; // edx
char v3; // cl

LOBYTE(v1) = func0(a1);
if ( (_BYTE)v1 )
{
func2(a1);
v1 = func1(a1);
pmmap[v1] |= v2 << v3;
}
return v1;
}

original_code:
static void pmmap_set(int block_number) {
  if(!valid_block_number(block_number))
    return;
  pmmap[pmmap_index(block_number)] |= (1 << pmmap_bitnum(block_number));
}


decompiled_code:
char __fastcall pmmap_reset(unsigned int a1)
{
int v1; // eax
int v2; // edx
char v3; // cl

LOBYTE(v1) = valid_block_number(a1);
if ( (_BYTE)v1 )
{
pmmap_bitnum(a1);
v1 = pmmap_index(a1);
pmmap[v1] &= __ROL4__(v2, v3);
}
return v1;
}

canonical_decompiled_code:
char __fastcall func4(unsigned int a1)
{
int v1; // eax
int v2; // edx
char v3; // cl

LOBYTE(v1) = func0(a1);
if ( (_BYTE)v1 )
{
func2(a1);
v1 = func1(a1);
pmmap[v1] &= __ROL4__(v2, v3);
}
return v1;
}

original_code:
static void pmmap_reset(int block_number) {
  if(!valid_block_number(block_number))
    return;
  pmmap[pmmap_index(block_number)] &= ~(1 << pmmap_bitnum(block_number));
}


decompiled_code:
char __fastcall pmmap_mark_block(unsigned int a1, int a2)
{
char result; // al

if ( !a2 )
return pmmap_reset(a1);
if ( a2 == 1 )
result = pmmap_set(a1);
return result;
}

canonical_decompiled_code:
char __fastcall func5(unsigned int a1, int a2)
{
char result; // al

if ( !a2 )
return func4(a1);
if ( a2 == 1 )
result = func3(a1);
return result;
}

original_code:
static void pmmap_mark_block(int block_number, BlockStatus block_status) {
  switch(block_status) {
  case BLOCK_AVAIL:
    pmmap_reset(block_number);
    break;
  case BLOCK_UNAVAIL:
    pmmap_set(block_number);
    break;
  }
}

canonical_original_code:
static void pmmap_mark_block(int block_number, enum BlockStatus block_status) {
  switch(block_status) {
  case BLOCK_AVAIL:
    pmmap_reset(block_number);
    break;
  case BLOCK_UNAVAIL:
    pmmap_set(block_number);
    break;
  }
}

variable_types: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
_BOOL8 __fastcall pmmap_get_block_status(unsigned int a1)
{
bool v1; // al
_BOOL4 v2; // er8
__int64 v3; // rsi
char v4; // al
int v5; // er8

v1 = valid_block_number(a1);
v2 = 1;
if ( v1 )
{
v3 = (int)pmmap_index(a1);
v4 = pmmap_bitnum(a1);
v2 = (pmmap[v3] & (v5 << v4)) != 0;
}
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(unsigned int a1)
{
bool v1; // al
_BOOL4 v2; // er8
__int64 v3; // rsi
char v4; // al
int v5; // er8

v1 = func0(a1);
v2 = 1;
if ( v1 )
{
v3 = (int)func1(a1);
v4 = func2(a1);
v2 = (pmmap[v3] & (v5 << v4)) != 0;
}
return v2;
}

original_code:
static BlockStatus pmmap_get_block_status(int block_number) {
  if(!valid_block_number(block_number))
    return 1;
  if (pmmap[pmmap_index(block_number)] & (1 << pmmap_bitnum(block_number)))
    return BLOCK_UNAVAIL;
  else
    return BLOCK_AVAIL;
}

canonical_original_code:
static enum BlockStatus pmmap_get_block_status(int block_number) {
  if(!valid_block_number(block_number))
    return 1;
  if (pmmap[pmmap_index(block_number)] & (1 << pmmap_bitnum(block_number)))
    return BLOCK_UNAVAIL;
  else
    return BLOCK_AVAIL;
}

return_type: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
__int64 __fastcall pmmap_run_length(unsigned int a1)
{
bool v1; // r8
__int64 result; // rax
signed int v3; // edi
int v4; // eax
int v5; // er10
int v6; // er9

v1 = valid_block_number(a1);
result = 0LL;
if ( v1 )
{
pmmap_get_block_status(a1);
v3 = a1 + 1;
if ( v3 <= 0xFFFFF )
{
do
{
v4 = pmmap_get_block_status(v3);
if ( v4 != v6 )
break;
++v3;
}
while ( v3 != 0x100000 );
result = (unsigned int)(v3 - v5);
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
bool v1; // r8
__int64 result; // rax
signed int v3; // edi
int v4; // eax
int v5; // er10
int v6; // er9

v1 = func0(a1);
result = 0LL;
if ( v1 )
{
func6(a1);
v3 = a1 + 1;
if ( v3 <= 0xFFFFF )
{
do
{
v4 = func6(v3);
if ( v4 != v6 )
break;
++v3;
}
while ( v3 != 0x100000 );
result = (unsigned int)(v3 - v5);
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
static int pmmap_run_length(int first_block_number) {
  if(!valid_block_number(first_block_number))
    return 0;
  BlockStatus first_status = pmmap_get_block_status(first_block_number);
  int next_block_number = first_block_number + 1;
  while(next_block_number < 0x00100000 &&
        pmmap_get_block_status(next_block_number) == first_status)
    next_block_number++;
  return next_block_number - first_block_number;
}

canonical_original_code:
static int pmmap_run_length(int first_block_number) {
  if(!valid_block_number(first_block_number))
    return 0;
  enum BlockStatus first_status = pmmap_get_block_status(first_block_number);
  int next_block_number = first_block_number + 1;
  while(next_block_number < 0x00100000 &&
        pmmap_get_block_status(next_block_number) == first_status)
    next_block_number++;
  return next_block_number - first_block_number;
}

user_defined_types: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
__int64 __fastcall pmmap_addr(int a1)
{
return a1 << 12;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
return a1 << 12;
}

original_code:
static inline PhysicalAddress pmmap_addr(int block_number) {
  return (PhysicalAddress) (0x1000 * block_number);
}

canonical_original_code:
static inline void *pmmap_addr(int block_number) {
  return (PhysicalAddress) (0x1000 * block_number);
}


decompiled_code:
__int64 __fastcall pmmap_bytes(int a1)
{
return (unsigned int)(a1 << 12);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
return (unsigned int)(a1 << 12);
}

original_code:
static unsigned pmmap_bytes(int num_blocks) {
  return 0x1000 * num_blocks;
}

canonical_original_code:
static unsigned int pmmap_bytes(int num_blocks) {
  return 0x1000 * num_blocks;
}


decompiled_code:
const char *__fastcall pmmap_status_str(int a1)
{
const char *result; // rax

result = "AVAIL";
if ( a1 )
{
result = (const char *)&unk_20320;
if ( a1 == 1 )
result = "UNAVAIL";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func10(int a1)
{
const char *result; // rax

result = "AVAIL";
if ( a1 )
{
result = (const char *)&unk_20320;
if ( a1 == 1 )
result = "UNAVAIL";
}
return result;
}

original_code:
static const char * pmmap_status_str(BlockStatus status) {
  switch(status) {
  case BLOCK_AVAIL:
    return "AVAIL";
  case BLOCK_UNAVAIL:
    return "UNAVAIL";
  default:
    return "?";
  }
}

canonical_original_code:
static char *pmmap_status_str(enum BlockStatus status) {
  switch(status) {
  case BLOCK_AVAIL:
    return "AVAIL";
  case BLOCK_UNAVAIL:
    return "UNAVAIL";
  default:
    return "?";
  }
}


decompiled_code:
void *__fastcall pmmap_memset(unsigned __int8 a1)
{
return memset(pmmap, a1, (size_t)&unk_20000);
}

canonical_decompiled_code:
void *__fastcall func11(unsigned __int8 a1)
{
return memset(pmmap, a1, (size_t)&unk_20000);
}

original_code:
static void pmmap_memset(unsigned char value) {
  memset(pmmap, value, sizeof(pmmap));
}

variable_types:
value
unsigned char


decompiled_code:
void *pmmap_mark_all_unused()
{
return pmmap_memset(0);
}

canonical_decompiled_code:
void *func12()
{
return func11(0);
}

original_code:
static void pmmap_mark_all_unused() {
  pmmap_memset(0);
}


decompiled_code:
void *pmmap_mark_all_used()
{
return pmmap_memset(0xFFu);
}

canonical_decompiled_code:
void *func13()
{
return func11(0xFFu);
}

original_code:
void pmmap_mark_all_used() {
  pmmap_memset(0xff);
}


decompiled_code:
unsigned __int64 __fastcall pmmap_mark_region(unsigned int a1, __int64 a2, int a3)
{
unsigned int v4; // er8
int v5; // er8
unsigned __int64 result; // rax
unsigned __int64 v7; // r9

v4 = a1 >> 12;
do
{
pmmap_mark_block(v4, a3);
result = pmmap_addr(v5);
}
while ( v7 > result );
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func14(unsigned int a1, __int64 a2, int a3)
{
unsigned int v4; // er8
int v5; // er8
unsigned __int64 result; // rax
unsigned __int64 v7; // r9

v4 = a1 >> 12;
do
{
func5(v4, a3);
result = func8(v5);
}
while ( v7 > result );
return result;
}

original_code:
void pmmap_mark_region(PhysicalAddress addr,
                       unsigned length,
                       BlockStatus status) {
  uint32_t addr_value = (uint32_t) addr;
  int block_number = addr_value / 0x1000;
  do {
    pmmap_mark_block(block_number, status);
    block_number++;
  } while(pmmap_addr(block_number) < (PhysicalAddress) (addr_value + length));
}

canonical_original_code:
void pmmap_mark_region(void *addr, unsigned int length, enum BlockStatus status) {
  unsigned int addr_value = (uint32_t) addr;
  int block_number = addr_value / 0x1000;
  do {
    pmmap_mark_block(block_number, status);
    block_number++;
  } while(pmmap_addr(block_number) < (PhysicalAddress) (addr_value + length));
}

variable_types:
length
unsigned int


decompiled_code:
void *pmmap_init()
{
return pmmap_mark_all_unused();
}

canonical_decompiled_code:
void *func15()
{
return func12();
}

original_code:
void pmmap_init() {
  pmmap_mark_all_unused();
}


decompiled_code:
__int64 pmmap_print()
{
signed int v0; // ebx
int v1; // ebp
int v2; // eax
__int64 v3; // rsi
int v4; // edi
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r8
const char *v8; // r9
__int64 result; // rax

v0 = 0;
vty_puts("Physical memory map:\n");
vty_puts(" BLK # BLOCKS ADDR BYTES STATUS\n");
do
{
v1 = pmmap_run_length(v0);
v2 = pmmap_get_block_status(v0);
v3 = (unsigned int)v0;
pmmap_status_str(v2);
pmmap_bytes(v1);
v4 = v0;
v0 += v1;
v5 = pmmap_addr(v4);
result = vty_printf("%7d %7d %08x %08x %s\n", v3, v6, v5, v7, v8);
}
while ( v0 <= 0xFFFFF );
return result;
}

canonical_decompiled_code:
__int64 func16()
{
signed int v0; // ebx
int v1; // ebp
int v2; // eax
__int64 v3; // rsi
int v4; // edi
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r8
const char *v8; // r9
__int64 result; // rax

v0 = 0;
vty_puts("Physical memory map:\n");
vty_puts(" BLK # BLOCKS ADDR BYTES STATUS\n");
do
{
v1 = func7(v0);
v2 = func6(v0);
v3 = (unsigned int)v0;
func10(v2);
func9(v1);
v4 = v0;
v0 += v1;
v5 = func8(v4);
result = vty_printf("%7d %7d %08x %08x %s\n", v3, v6, v5, v7, v8);
}
while ( v0 <= 0xFFFFF );
return result;
}

original_code:
void pmmap_print() {
  vty_puts("Physical memory map:\n");
  vty_puts("  BLK #   BLOCKS      ADDR     BYTES  STATUS\n");
  int block_number = 0;
  while(block_number < 0x00100000) {
    int run_length = pmmap_run_length(block_number);
    vty_printf("%7d  %7d  %08x  %08x  %s\n", block_number,
               run_length,
               pmmap_addr(block_number),
               pmmap_bytes(run_length),
               pmmap_status_str(pmmap_get_block_status(block_number)));
    block_number += run_length;
  }
}


decompiled_code:
__int64 pmmap_alloc()
{
int v0; // edi
int v1; // er9

v0 = block_number_0;
do
{
if ( !pmmap_get_block_status(v0) )
{
pmmap_mark_block(v0, 1);
return pmmap_addr(v0);
}
v0 = (v0 + 1) % 0x100000;
block_number_0 = v0;
}
while ( v1 != 1 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func17()
{
int v0; // edi
int v1; // er9

v0 = block_number_0;
do
{
if ( !func6(v0) )
{
func5(v0, 1);
return func8(v0);
}
v0 = (v0 + 1) % 0x100000;
block_number_0 = v0;
}
while ( v1 != 1 );
return 0xFFFFFFFFLL;
}

original_code:
PhysicalAddress pmmap_alloc() {
  static int block_number = 0;
  for(int i = 0; i < 0x00100000; i++) {
    if(pmmap_get_block_status(block_number) == BLOCK_AVAIL) {
      pmmap_mark_block(block_number, BLOCK_UNAVAIL);
      return pmmap_addr(block_number);
    }
    block_number = (block_number + 1) % 0x00100000;
  }
  return ((PhysicalAddress *) 0xffffffff);
}

canonical_original_code:
void *pmmap_alloc() {
  static int block_number = 0;
  for(int i = 0; i < 0x00100000; i++) {
    if(pmmap_get_block_status(block_number) == BLOCK_AVAIL) {
      pmmap_mark_block(block_number, BLOCK_UNAVAIL);
      return pmmap_addr(block_number);
    }
    block_number = (block_number + 1) % 0x00100000;
  }
  return ((void **) 0xffffffff);
}

variable_types: i int


decompiled_code:
__int64 __fastcall mostly_set_descriptor(__int64 a1)
{
sd_set_address(a1, 0LL);
sd_set_limit(a1, 0xFFFFFFLL);
sd_set_present(a1);
sd_set_priv(a1, 0LL);
sd_set_default_size_32_bits(a1);
sd_set_granularity_4k(a1);
return sd_set_code_or_data(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
sd_set_address(a1, 0LL);
sd_set_limit(a1, 0xFFFFFFLL);
sd_set_present(a1);
sd_set_priv(a1, 0LL);
sd_set_default_size_32_bits(a1);
sd_set_granularity_4k(a1);
return sd_set_code_or_data(a1);
}

original_code:
static void mostly_set_descriptor(SegmentDescriptor * sd) {
  sd_set_address(sd, 0);
  sd_set_limit(sd, 0xffffff);
  sd_set_present(sd);
  sd_set_priv(sd, 0);
  sd_set_default_size_32_bits(sd);
  sd_set_granularity_4k(sd);
  sd_set_code_or_data(sd);
}

canonical_original_code:
static void mostly_set_descriptor(struct SegmentDescriptor *sd) {
  sd_set_address(sd, 0);
  sd_set_limit(sd, 0xffffff);
  sd_set_present(sd);
  sd_set_priv(sd, 0);
  sd_set_default_size_32_bits(sd);
  sd_set_granularity_4k(sd);
  sd_set_code_or_data(sd);
}

variable_types: struct SegmentDescriptor { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; } *


decompiled_code:
__int64 gdt_set()
{
sd_init(&gdt);
sd_init(&unk_130);
sd_init(&unk_138);
mostly_set_descriptor((__int64)&unk_130);
sd_set_code(&unk_130);
sd_set_readable(&unk_130);
mostly_set_descriptor((__int64)&unk_138);
sd_set_data(&unk_138);
return sd_set_writable(&unk_138);
}

canonical_decompiled_code:
__int64 func1()
{
sd_init(&gdt);
sd_init(&unk_130);
sd_init(&unk_138);
func0((__int64)&unk_130);
sd_set_code(&unk_130);
sd_set_readable(&unk_130);
func0((__int64)&unk_138);
sd_set_data(&unk_138);
return sd_set_writable(&unk_138);
}

original_code:
static void gdt_set() {
  for(unsigned i = 0; i < sizeof(gdt) / sizeof(*gdt); i++)
    sd_init(gdt + i);
  {
    SegmentDescriptor * sd = gdt + 1;
    mostly_set_descriptor(sd);
    sd_set_code(sd);
    sd_set_readable(sd);
  }
  {
    SegmentDescriptor * sd = gdt + 2;
    mostly_set_descriptor(sd);
    sd_set_data(sd);
    sd_set_writable(sd);
  }
}

canonical_original_code:
static void gdt_set() {
  for(unsigned int i = 0; i < sizeof(gdt) / sizeof(*gdt); i++)
    sd_init(gdt + i);
  {
    struct SegmentDescriptor *sd = gdt + 1;
    mostly_set_descriptor(sd);
    sd_set_code(sd);
    sd_set_readable(sd);
  }
  {
    struct SegmentDescriptor *sd = gdt + 2;
    mostly_set_descriptor(sd);
    sd_set_data(sd);
    sd_set_writable(sd);
  }
}

variable_types:
i
unsigned int

user_defined_types: struct SegmentDescriptor { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; }


decompiled_code:
__int64 gdt_install()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int16 v3; // [rsp+Ah] [rbp-Eh] BYREF
int v4; // [rsp+Ch] [rbp-Ch]

v3 = 23;
v4 = get_physaddr(&gdt);
lgdt(&v3);
v0 = selector_for_index(1LL);
set_code_selector(v0);
v1 = selector_for_index(2LL);
return set_data_selectors(v1);
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int16 v3; // [rsp+Ah] [rbp-Eh] BYREF
int v4; // [rsp+Ch] [rbp-Ch]

v3 = 23;
v4 = get_physaddr(&gdt);
lgdt(&v3);
v0 = selector_for_index(1LL);
set_code_selector(v0);
v1 = selector_for_index(2LL);
return set_data_selectors(v1);
}

original_code:
static void gdt_install() {
  GdtAddr gdt_addr = {
    .addr = (uint32_t) get_physaddr(gdt),
    .size = sizeof(gdt) - 1,
  };
  lgdt(&gdt_addr);
  set_code_selector(selector_for_index(1));
  set_data_selectors(selector_for_index(2));
}

canonical_original_code:
static void gdt_install() {
  struct GdtAddr gdt_addr = {
    .addr = (uint32_t) get_physaddr(gdt),
    .size = sizeof(gdt) - 1,
  };
  lgdt(&gdt_addr);
  set_code_selector(selector_for_index(1));
  set_data_selectors(selector_for_index(2));
}

variable_types: struct GdtAddr { unsigned short size; unsigned int addr; }


decompiled_code:
__int64 gdt_init()
{
gdt_set();
return gdt_install();
}

canonical_decompiled_code:
__int64 func3()
{
func1();
return func2();
}

original_code:
void gdt_init() {
  gdt_set();
  gdt_install();
}


decompiled_code:
const char *__fastcall bmmap_type_str(__int64 a1)
{
int v1; // edx
const char *result; // rax

v1 = *(_DWORD *)(a1 + 16);
result = "AVAIL";
if ( v1 != 1 )
{
result = (const char *)&unk_96;
if ( v1 == 2 )
result = "RESERVED";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(__int64 a1)
{
int v1; // edx
const char *result; // rax

v1 = *(_DWORD *)(a1 + 16);
result = "AVAIL";
if ( v1 != 1 )
{
result = (const char *)&unk_96;
if ( v1 == 2 )
result = "RESERVED";
}
return result;
}

original_code:
static const char * bmmap_type_str(const BiosMemoryMapEntry * entry) {
  switch(entry->type) {
  case 1:
    return "AVAIL";
  case 2:
    return "RESERVED";
  default:
    return "?";
  }
}

canonical_original_code:
static char *bmmap_type_str(struct BiosMemoryMapEntry *entry) {
  switch(entry->type) {
  case 1:
    return "AVAIL";
  case 2:
    return "RESERVED";
  default:
    return "?";
  }
}

variable_types: struct BiosMemoryMapEntry { unsigned int base_addr_low; unsigned int base_addr_high; unsigned int length_low; unsigned int length_high; unsigned int type; unsigned int reserved; } *


decompiled_code:
__int64 bmmap_print()
{
__int64 result; // rax
__int64 v1; // rbx
unsigned int v2; // ebp
__int64 v3; // rdi
const char *v4; // rax
__int64 v5; // [rsp-10h] [rbp-28h]

vty_puts("BIOS memory map:\n");
result = vty_printf(" ADDR LEN TYPE RESERVED\n");
if ( loader_data )
{
v1 = (unsigned int)((_DWORD)&loader_data + 4);
v2 = 0;
do
{
v3 = v1;
++v2;
v1 += 24LL;
v4 = bmmap_type_str(v3);
vty_printf(
"%08x:%08x %08x:%08x %8s %08x\n",
*(unsigned int *)(v1 - 20),
*(unsigned int *)(v1 - 24),
*(unsigned int *)(v1 - 12),
*(unsigned int *)(v1 - 16),
v4,
*(unsigned int *)(v1 - 4));
result = v5;
}
while ( loader_data > v2 );
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int64 v1; // rbx
unsigned int v2; // ebp
__int64 v3; // rdi
const char *v4; // rax
__int64 v5; // [rsp-10h] [rbp-28h]

vty_puts("BIOS memory map:\n");
result = vty_printf(" ADDR LEN TYPE RESERVED\n");
if ( loader_data )
{
v1 = (unsigned int)((_DWORD)&loader_data + 4);
v2 = 0;
do
{
v3 = v1;
++v2;
v1 += 24LL;
v4 = func0(v3);
vty_printf(
"%08x:%08x %08x:%08x %8s %08x\n",
*(unsigned int *)(v1 - 20),
*(unsigned int *)(v1 - 24),
*(unsigned int *)(v1 - 12),
*(unsigned int *)(v1 - 16),
v4,
*(unsigned int *)(v1 - 4));
result = v5;
}
while ( loader_data > v2 );
}
return result;
}

original_code:
void bmmap_print() {
  vty_puts("BIOS memory map:\n");
  vty_printf("       ADDR               LEN           TYPE    RESERVED\n");
  for(unsigned i = 0; i < loader_data.bios_memory_map.count; i++) {
    BiosMemoryMapEntry * entry = &loader_data.bios_memory_map.entries[i];
    vty_printf("%08x:%08x  %08x:%08x  %8s  %08x\n",
               entry->base_addr_high,
               entry->base_addr_low,
               entry->length_high,
               entry->length_low,
               bmmap_type_str(entry),
               entry->reserved);
  }
}

canonical_original_code:
void bmmap_print() {
  vty_puts("BIOS memory map:\n");
  vty_printf("       ADDR               LEN           TYPE    RESERVED\n");
  for(unsigned int i = 0; i < loader_data.bios_memory_map.count; i++) {
    struct BiosMemoryMapEntry *entry = &loader_data.bios_memory_map.entries[i];
    vty_printf("%08x:%08x  %08x:%08x  %8s  %08x\n",
               entry->base_addr_high,
               entry->base_addr_low,
               entry->length_high,
               entry->length_low,
               bmmap_type_str(entry),
               entry->reserved);
  }
}

user_defined_types: struct BiosMemoryMapEntry { unsigned int base_addr_low; unsigned int base_addr_high; unsigned int length_low; unsigned int length_high; unsigned int type; unsigned int reserved; }


decompiled_code:
void *__fastcall sd_init(void *a1)
{
return memset(a1, 0, 8uLL);
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
return memset(a1, 0, 8uLL);
}

original_code:
void sd_init(SegmentDescriptor * sd) {
  memset(sd, 0, sizeof(*sd));
}

canonical_original_code:
void sd_init(struct PACKED *sd) {
  memset(sd, 0, sizeof(*sd));
}

variable_types: struct PACKED { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; } *


decompiled_code:
__int64 __fastcall sd_set_address(__int64 a1, unsigned int a2)
{
__int64 result; // rax

*(_WORD *)(a1 + 2) = a2;
result = HIWORD(a2);
*(_BYTE *)(a1 + 7) = HIBYTE(a2);
*(_BYTE *)(a1 + 4) = BYTE2(a2);
return result;
}

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

*(_WORD *)(a1 + 2) = a2;
result = HIWORD(a2);
*(_BYTE *)(a1 + 7) = HIBYTE(a2);
*(_BYTE *)(a1 + 4) = BYTE2(a2);
return result;
}

original_code:
void sd_set_address(SegmentDescriptor * sd, uint32_t address) {
  sd->base_low = address & 0xffff;
  sd->base_mid = (address >> 16) & 0xff;
  sd->base_high = (address >> 24) & 0xff;
}

canonical_original_code:
void sd_set_address(struct PACKED *sd, unsigned int address) {
  sd->base_low = address & 0xffff;
  sd->base_mid = (address >> 16) & 0xff;
  sd->base_high = (address >> 24) & 0xff;
}

variable_types:
address
unsigned int

user_defined_types: struct PACKED { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; }


decompiled_code:
__int64 __fastcall sd_set_limit(__int64 a1, int a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(unsigned __int8 *)(a1 + 6);
*(_WORD *)a1 = a2;
result = v2 & 0xFFFFFFF0;
*(_BYTE *)(a1 + 6) = result | BYTE2(a2) & 0xF;
return result;
}

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

v2 = *(unsigned __int8 *)(a1 + 6);
*(_WORD *)a1 = a2;
result = v2 & 0xFFFFFFF0;
*(_BYTE *)(a1 + 6) = result | BYTE2(a2) & 0xF;
return result;
}

original_code:
void sd_set_limit(SegmentDescriptor * sd, uint32_t limit) {
  sd->limit_low = limit & 0xffff;
  sd->flags &= ~0x0f;
  sd->flags |= (limit >> 16) & 0x0f;
}

canonical_original_code:
void sd_set_limit(struct PACKED *sd, unsigned int limit) {
  sd->limit_low = limit & 0xffff;
  sd->flags &= ~0x0f;
  sd->flags |= (limit >> 16) & 0x0f;
}

variable_types:
limit
unsigned int


decompiled_code:
__int64 __fastcall vid_poke(int a1, int a2, char a3, char a4)
{
__int64 result; // rax
__int64 v5; // rsi

result = a2;
v5 = a2 + 80LL * a1;
*(_BYTE *)(2 * v5 + 0xB8000) = a3;
*(_BYTE *)(2 * v5 + 0xB8001) = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char a3, char a4)
{
__int64 result; // rax
__int64 v5; // rsi

result = a2;
v5 = a2 + 80LL * a1;
*(_BYTE *)(2 * v5 + 0xB8000) = a3;
*(_BYTE *)(2 * v5 + 0xB8001) = a4;
return result;
}

original_code:
void vid_poke(int row, int col, char c, char attr) {
  (*video)[row][col][0] = c;
  (*video)[row][col][1] = attr;
}

variable_types: row int

variable_types: col int

variable_types: c char

variable_types: attr char


decompiled_code:
__int64 __fastcall vid_clear_row(int a1)
{
int i; // er9
__int64 result; // rax
int v4; // er9

for ( i = 0; i != 80; i = v4 + 1 )
result = vid_poke(a1, i, 32, 7);
return result;
}

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

for ( i = 0; i != 80; i = v4 + 1 )
result = func0(a1, i, 32, 7);
return result;
}

original_code:
void vid_clear_row(int row) {
  for (int col = 0; col < 80; col++)
    vid_poke(row, col, ' ', 0x07);
}


decompiled_code:
__int64 vid_clear()
{
int i; // er11
__int64 result; // rax

for ( i = 0; i != 25; result = vid_clear_row(i) )
;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int i; // er11
__int64 result; // rax

for ( i = 0; i != 25; result = func1(i) )
;
return result;
}

original_code:
void vid_clear() {
  for (int row = 0; row < 25; row++)
    vid_clear_row(row);
}


decompiled_code:
void *vid_scroll()
{
return memmove((void *)0xB8000, (const void *)0xB80A0, 0xF00uLL);
}

canonical_decompiled_code:
void *func3()
{
return memmove((void *)0xB8000, (const void *)0xB80A0, 0xF00uLL);
}


decompiled_code:
__int64 __fastcall set_idt_entry(int a1, int a2, char a3)
{
__int16 *v4; // rbp
__int64 result; // rax

v4 = &idt[4 * a1];
*v4 = a2;
result = selector_for_index(1LL);
*((_BYTE *)v4 + 5) = a3;
v4[3] = HIWORD(a2);
v4[1] = result;
*((_BYTE *)v4 + 4) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char a3)
{
__int16 *v4; // rbp
__int64 result; // rax

v4 = &idt[4 * a1];
*v4 = a2;
result = selector_for_index(1LL);
*((_BYTE *)v4 + 5) = a3;
v4[3] = HIWORD(a2);
v4[1] = result;
*((_BYTE *)v4 + 4) = 0;
return result;
}

original_code:
void
set_idt_entry(int interrupt_number,
              void (*stub)(),
              int flags) {
  IdtEntry * entry = idt + interrupt_number;
  entry->offset_low = (unsigned) stub & 0xffff;
  entry->code_selector = selector_for_index(1);
  entry->unused = 0;
  entry->flags = flags;
  entry->offset_high = (unsigned) stub >> 16;
}

canonical_original_code:
void set_idt_entry(int interrupt_number, void (*stub)(), int flags) {
  struct IdtEntry *entry = idt + interrupt_number;
  entry->offset_low = (unsigned int) stub & 0xffff;
  entry->code_selector = selector_for_index(1);
  entry->unused = 0;
  entry->flags = flags;
  entry->offset_high = (unsigned int) stub >> 16;
}

variable_types:
stub
void (*)()

variable_types: flags int

variable_types: struct IdtEntry { unsigned short offset_low; unsigned short code_selector; unsigned char unused; unsigned char flags; unsigned short offset_high; } *


init

decompiled_code:
__int64 init()
{
loader_data_init();
vty_init();
gdt_init();
int_init();
return mem_init();
}

canonical_decompiled_code:
__int64 func0()
{
loader_data_init();
vty_init();
gdt_init();
int_init();
return mem_init();
}

original_code:
void init() {
  loader_data_init();
  vty_init();
  gdt_init();
  int_init();
  mem_init();
}


repo: wejeus/xmem

decompiled_code:
int __fastcall quicklist_free(int a1, __int64 a2)
{
__int64 v2; // r12
_QWORD **v3; // rax
_QWORD *i; // rbx

v2 = a2 - 16;
printf("\nquicklist_free");
if ( free_blocks[a1] )
{
printf("\nrelink with old blocks");
v3 = *(_QWORD ***)(a2 - 16);
free_blocks[a1] = v2;
for ( i = *v3; i; i = (_QWORD *)*i )
LODWORD(v3) = printf("\ntraversing..");
}
else
{
LODWORD(v3) = printf("\nnothing to connect to!");
free_blocks[a1] = v2;
}
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func0(int a1, __int64 a2)
{
__int64 v2; // r12
_QWORD **v3; // rax
_QWORD *i; // rbx

v2 = a2 - 16;
printf("\nquicklist_free");
if ( free_blocks[a1] )
{
printf("\nrelink with old blocks");
v3 = *(_QWORD ***)(a2 - 16);
free_blocks[a1] = v2;
for ( i = *v3; i; i = (_QWORD *)*i )
LODWORD(v3) = printf("\ntraversing..");
}
else
{
LODWORD(v3) = printf("\nnothing to connect to!");
free_blocks[a1] = v2;
}
return (int)v3;
}

original_code:
void quicklist_free(int block_pos, void *ptr)
{
 printf("\nquicklist_free");
 Header *tmp = free_blocks[block_pos];
 if (tmp == ((void *)0)) {
  printf("\nnothing to connect to!");
  free_blocks[block_pos] = (Header *) ptr-1;
 } else {
  printf("\nrelink with old blocks");
  free_blocks[block_pos] = (Header *) ptr-1;
  Header *next = free_blocks[block_pos]->s.next;
  while ( (next = next->s.next) != ((void *)0)) {
   printf("\ntraversing..");
  }
  next = tmp;
 }
}

canonical_original_code:
void quicklist_free(int block_pos, void *ptr)
{
 printf("\nquicklist_free");
 union header *tmp = free_blocks[block_pos];
 if (tmp == ((void *)0)) {
  printf("\nnothing to connect to!");
  free_blocks[block_pos] = (union header *) ptr-1;
 } else {
  printf("\nrelink with old blocks");
  free_blocks[block_pos] = (union header *) ptr-1;
  union header *next = free_blocks[block_pos]->s.next;
  while ( (next = next->s.next) != ((void *)0)) {
   printf("\ntraversing..");
  }
  next = tmp;
 }
}

variable_types:
ptr
void *

variable_types: union header { struct { union header *next; unsigned int size; } s; long x; } *


decompiled_code:
int __fastcall quickfit_free(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
unsigned __int64 v4; // rbx

v1 = (double)*(int *)(a1 - 8);
if ( v1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
v3 = v2 - 4.0;
if ( v3 >= 9.223372036854776e18 )
{
v4 = (unsigned int)(int)(v3 - 9.223372036854776e18) ^ 0x8000000000000000LL;
if ( v4 <= 3 )
goto LABEL_5;
}
else
{
v4 = (unsigned int)(int)v3;
if ( v4 <= 3 )
{
LABEL_5:
printf("\nquickfit_free(), within bounds of some quicklist");
return quicklist_free(v4, a1);
}
}
printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
return linear_free(a1);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
unsigned __int64 v4; // rbx

v1 = (double)*(int *)(a1 - 8);
if ( v1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
v3 = v2 - 4.0;
if ( v3 >= 9.223372036854776e18 )
{
v4 = (unsigned int)(int)(v3 - 9.223372036854776e18) ^ 0x8000000000000000LL;
if ( v4 <= 3 )
goto LABEL_5;
}
else
{
v4 = (unsigned int)(int)v3;
if ( v4 <= 3 )
{
LABEL_5:
printf("\nquickfit_free(), within bounds of some quicklist");
return func0(v4, a1);
}
}
printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
return linear_free(a1);
}

original_code:
void quickfit_free(void *ptr) {
 Header *bp = (Header *)ptr - 1;
 size_t block_pos = sqrt(bp->s.size) - 4;
 if (block_pos <= 3) {
  printf("\nquickfit_free(), within bounds of some quicklist");
  quicklist_free(block_pos, ptr);
 } else {
  printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
  linear_free(ptr);
 }
}

canonical_original_code:
void quickfit_free(void *ptr) {
 union header *bp = (Header *)ptr - 1;
 unsigned long block_pos = sqrt(bp->s.size) - 4;
 if (block_pos <= 3) {
  printf("\nquickfit_free(), within bounds of some quicklist");
  quicklist_free(block_pos, ptr);
 } else {
  printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
  linear_free(ptr);
 }
}

user_defined_types: union header { struct { union header *next; unsigned int size; } s; long x; }


decompiled_code:
int quickfit_realloc()
{
return printf("\nquickfit realloc");
}

canonical_decompiled_code:
int func5()
{
return printf("\nquickfit realloc");
}

original_code:
void *quickfit_realloc(void *ptr, size_t size) {
 printf("\nquickfit realloc");
}

canonical_original_code:
void *quickfit_realloc(void *ptr, unsigned long size) {
 printf("\nquickfit realloc");
}

variable_types:
size
unsigned long


morecore

decompiled_code:
__int64 __fastcall morecore(int a1)
{
int v1; // er13
char *v2; // rax
_DWORD *v3; // rsi
char *v4; // rdx

printf("\nquickfit_morecore");
v1 = (int)pow(2.0, (double)(a1 + 4));
printf("\nmorecore will request: %d units", (unsigned int)(8 * v1));
v2 = (char *)sbrk(16LL * (unsigned int)(8 * v1));
if ( v2 == (char *)-1LL )
return 0LL;
*((_DWORD *)v2 + 2) = v1;
v3 = v2;
v4 = v2 + 7;
do
{
*(_DWORD *)(++v2 + 7) = v1;
*(_QWORD *)(v2 - 1) = v2;
}
while ( v4 != v2 );
*(_QWORD *)((char *)v3 + 7) = 0LL;
quicklist_free(a1, (__int64)(v3 + 4));
return free_blocks[a1];
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // er13
char *v2; // rax
_DWORD *v3; // rsi
char *v4; // rdx

printf("\nquickfit_morecore");
v1 = (int)pow(2.0, (double)(a1 + 4));
printf("\nmorecore will request: %d units", (unsigned int)(8 * v1));
v2 = (char *)sbrk(16LL * (unsigned int)(8 * v1));
if ( v2 == (char *)-1LL )
return 0LL;
*((_DWORD *)v2 + 2) = v1;
v3 = v2;
v4 = v2 + 7;
do
{
*(_DWORD *)(++v2 + 7) = v1;
*(_QWORD *)(v2 - 1) = v2;
}
while ( v4 != v2 );
*(_QWORD *)((char *)v3 + 7) = 0LL;
func0(a1, (__int64)(v3 + 4));
return free_blocks[a1];
}

original_code:
static Header *morecore(int block_pos) {
 printf("\nquickfit_morecore");
 char *cp;
 Header *new_block;
 unsigned units = pow(2, block_pos + 4);
 printf("\nmorecore will request: %d units", 8 * units);
 cp = sbrk(8 * units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 int i;
 Header *first = (Header *) cp;
 new_block = (Header *) cp;
 new_block->s.size = units;
 for (i = 0; i < 8 -1; ++i) {
  ++cp;
  new_block->s.next = (Header *) cp;
  new_block->s.size = units;
  new_block = new_block->s.next;
 }
 new_block->s.next = ((void *)0);
 quicklist_free(block_pos, (void *)(first+1));
 return free_blocks[block_pos];
}

canonical_original_code:
static union header *morecore(int block_pos) {
 printf("\nquickfit_morecore");
 char *cp;
 union header *new_block;
 unsigned int units = pow(2, block_pos + 4);
 printf("\nmorecore will request: %d units", 8 * units);
 cp = sbrk(8 * units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 int i;
 union header *first = (Header *) cp;
 new_block = (union header *) cp;
 new_block->s.size = units;
 for (i = 0; i < 8 -1; ++i) {
  ++cp;
  new_block->s.next = (union header *) cp;
  new_block->s.size = units;
  new_block = new_block->s.next;
 }
 new_block->s.next = ((void *)0);
 quicklist_free(block_pos, (void *)(first+1));
 return free_blocks[block_pos];
}

variable_types:
cp
char *

variable_types:
units
unsigned int

variable_types: i int

return_type: union header { struct { union header *next; unsigned int size; } s; long x; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rbp
_DWORD *v5; // rax
_DWORD *v6; // rbp

printf("-- START XMEMTEST");
v3 = xmalloc(400LL, argv);
*(_DWORD *)(v3 + 4) = 4711;
v4 = v3;
printf("\n*p1: %d", 4711LL);
xfree(v4);
v5 = (_DWORD *)xmalloc(4LL, 4711LL);
*v5 = 666;
v6 = v5;
printf("\n*p4: %d", 666LL);
xfree(v6);
puts("\n-- XMEMTEST DONE!");
return 0;
}

original_code:
int main(int argc, char **argv) {
 printf("-- START XMEMTEST");
 int *p1 = xmalloc(100*sizeof(int));
 p1[1] = 4711;
 printf("\n*p1: %d", p1[1]);
 xfree(p1);
 int *p4 = xmalloc(sizeof(int));
 *p4 = 666;
 printf("\n*p4: %d", *p4);
 xfree(p4);
 printf("\n-- XMEMTEST DONE!\n");
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
unsigned __int64 __fastcall linear_free(__int64 a1)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rcx
unsigned __int64 v4; // rdx
__int64 v5; // rdi
__int64 v6; // rsi
unsigned __int64 result; // rax
int v8; // edi
int v9; // esi

printf("\nlinear_free");
v2 = p_free_blocks;
v3 = a1 - 16;
do
{
while ( 1 )
{
v4 = v2;
v2 = *(_QWORD *)v2;
if ( v4 >= v3 )
break;
if ( v3 < v2 || v4 >= v2 )
goto LABEL_5;
}
}
while ( v4 < v2 || v3 >= v2 );
LABEL_5:
v5 = *(unsigned int *)(a1 - 8);
if ( v2 == v3 + 16 * v5 )
{
v8 = *(_DWORD *)(v2 + 8) + v5;
v2 = *(_QWORD *)v2;
*(_DWORD *)(a1 - 8) = v8;
}
*(_QWORD *)(a1 - 16) = v2;
v6 = *(unsigned int *)(v4 + 8);
result = v4 + 16 * v6;
if ( v3 == result )
{
result = *(_QWORD *)(a1 - 16);
v9 = *(_DWORD *)(a1 - 8) + v6;
p_free_blocks = v4;
*(_DWORD *)(v4 + 8) = v9;
*(_QWORD *)v4 = result;
}
else
{
*(_QWORD *)v4 = v3;
p_free_blocks = v4;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rcx
unsigned __int64 v4; // rdx
__int64 v5; // rdi
__int64 v6; // rsi
unsigned __int64 result; // rax
int v8; // edi
int v9; // esi

printf("\nlinear_free");
v2 = p_free_blocks;
v3 = a1 - 16;
do
{
while ( 1 )
{
v4 = v2;
v2 = *(_QWORD *)v2;
if ( v4 >= v3 )
break;
if ( v3 < v2 || v4 >= v2 )
goto LABEL_5;
}
}
while ( v4 < v2 || v3 >= v2 );
LABEL_5:
v5 = *(unsigned int *)(a1 - 8);
if ( v2 == v3 + 16 * v5 )
{
v8 = *(_DWORD *)(v2 + 8) + v5;
v2 = *(_QWORD *)v2;
*(_DWORD *)(a1 - 8) = v8;
}
*(_QWORD *)(a1 - 16) = v2;
v6 = *(unsigned int *)(v4 + 8);
result = v4 + 16 * v6;
if ( v3 == result )
{
result = *(_QWORD *)(a1 - 16);
v9 = *(_DWORD *)(a1 - 8) + v6;
p_free_blocks = v4;
*(_DWORD *)(v4 + 8) = v9;
*(_QWORD *)v4 = result;
}
else
{
*(_QWORD *)v4 = v3;
p_free_blocks = v4;
}
return result;
}

original_code:
void linear_free(void *ptr) {
 printf("\nlinear_free");
 Header *bp, *p;
 bp = (Header *)ptr - 1;
 for (p = p_free_blocks; !(bp > p && bp < p->s.next); p = p->s.next)
  if (p >= p->s.next && (bp > p || bp < p->s.next))
   break;
 if (bp + bp->s.size == p->s.next) {
  bp->s.size += p->s.next->s.size;
  bp->s.next = p->s.next->s.next;
 } else
  bp->s.next = p->s.next;
 if (p + p->s.size == bp) {
  p->s.size += bp->s.size;
  p->s.next = bp->s.next;
 } else
  p->s.next = bp;
 p_free_blocks = p;
}

canonical_original_code:
void linear_free(void *ptr) {
 printf("\nlinear_free");
 union header *bp; union header *p;
 bp = (union header *)ptr - 1;
 for (p = p_free_blocks; !(bp > p && bp < p->s.next); p = p->s.next)
  if (p >= p->s.next && (bp > p || bp < p->s.next))
   break;
 if (bp + bp->s.size == p->s.next) {
  bp->s.size += p->s.next->s.size;
  bp->s.next = p->s.next->s.next;
 } else
  bp->s.next = p->s.next;
 if (p + p->s.size == bp) {
  p->s.size += bp->s.size;
  p->s.next = bp->s.next;
 } else
  p->s.next = bp;
 p_free_blocks = p;
}


firstfit

decompiled_code:
__int64 __fastcall firstfit(__int64 a1, unsigned int a2)
{
__int64 v2; // rdx
unsigned int v3; // ecx
__int64 result; // rax
__int64 v5; // rcx

printf("\n(void*) firstfit(...)");
v2 = *(_QWORD *)a1;
v3 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
if ( a2 > v3 )
{
while ( 1 )
{
result = v2;
if ( p_free_blocks == v2 )
{
result = morecore(a2);
if ( !result )
break;
}
v2 = *(_QWORD *)result;
v3 = *(_DWORD *)(*(_QWORD *)result + 8LL);
if ( v3 >= a2 )
goto LABEL_7;
}
}
else
{
result = a1;
LABEL_7:
if ( a2 == v3 )
{
*(_QWORD *)result = *(_QWORD *)v2;
}
else
{
v5 = v3 - a2;
*(_DWORD *)(v2 + 8) = v5;
v2 += 16 * v5;
*(_DWORD *)(v2 + 8) = a2;
}
p_free_blocks = result;
result = v2 + 16;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
__int64 v2; // rdx
unsigned int v3; // ecx
__int64 result; // rax
__int64 v5; // rcx

printf("\n(void*) firstfit(...)");
v2 = *(_QWORD *)a1;
v3 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
if ( a2 > v3 )
{
while ( 1 )
{
result = v2;
if ( p_free_blocks == v2 )
{
result = func1(a2);
if ( !result )
break;
}
v2 = *(_QWORD *)result;
v3 = *(_DWORD *)(*(_QWORD *)result + 8LL);
if ( v3 >= a2 )
goto LABEL_7;
}
}
else
{
result = a1;
LABEL_7:
if ( a2 == v3 )
{
*(_QWORD *)result = *(_QWORD *)v2;
}
else
{
v5 = v3 - a2;
*(_DWORD *)(v2 + 8) = v5;
v2 += 16 * v5;
*(_DWORD *)(v2 + 8) = a2;
}
p_free_blocks = result;
result = v2 + 16;
}
return result;
}

original_code:
void *firstfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) firstfit(...)");
 Header *p;
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (p->s.size == num_units)
    prev_block->s.next = p->s.next;
   else {
    p->s.size -= num_units;
    p += p->s.size;
    p->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(p+1);
  }
  if (p == p_free_blocks && (p = morecore(num_units)) == ((void *)0))
    return ((void *)0);
 }
}

canonical_original_code:
void *firstfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) firstfit(...)");
 union header *p;
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (p->s.size == num_units)
    prev_block->s.next = p->s.next;
   else {
    p->s.size -= num_units;
    p += p->s.size;
    p->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(p+1);
  }
  if (p == p_free_blocks && (p = morecore(num_units)) == ((void *)0))
    return ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall linear_malloc(__int64 a1)
{
__int64 *v2; // rdi

printf("\nlinear_malloc, using strategy: %d", 4LL);
v2 = (__int64 *)p_free_blocks;
if ( !p_free_blocks )
{
v2 = &base_block;
p_free_blocks = (__int64)&base_block;
base_block = (__int64)&base_block;
dword_448 = 0;
}
return firstfit((__int64)v2, (unsigned int)((unsigned __int64)(a1 + 15) >> 4) + 1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 *v2; // rdi

printf("\nlinear_malloc, using strategy: %d", 4LL);
v2 = (__int64 *)p_free_blocks;
if ( !p_free_blocks )
{
v2 = &base_block;
p_free_blocks = (__int64)&base_block;
base_block = (__int64)&base_block;
dword_448 = 0;
}
return func2((__int64)v2, (unsigned int)((unsigned __int64)(a1 + 15) >> 4) + 1);
}

original_code:
void *linear_malloc(size_t size) {
 printf("\nlinear_malloc, using strategy: %d", 4);
 Header *p, *prev_block;
 unsigned num_units;
 num_units = (size+sizeof(Header)-1)/sizeof(Header) + 1;
 if ((prev_block = p_free_blocks) == ((void *)0)) {
  base_block.s.next = p_free_blocks = prev_block = &base_block;
  base_block.s.size = 0;
 }
 switch (4) {
  case 1:
   return firstfit(prev_block, num_units);
  case 2:
   return bestfit(prev_block, num_units);
  case 3:
   return worstfit(prev_block, num_units);
  default:
   return firstfit(prev_block, num_units);
 }
}

canonical_original_code:
void *linear_malloc(unsigned long size) {
 printf("\nlinear_malloc, using strategy: %d", 4);
 union header *p; union header *prev_block;
 unsigned int num_units;
 num_units = (size+sizeof(Header)-1)/sizeof(Header) + 1;
 if ((prev_block = p_free_blocks) == ((void *)0)) {
  base_block.s.next = p_free_blocks = prev_block = &base_block;
  base_block.s.size = 0;
 }
 switch (4) {
  case 1:
   return firstfit(prev_block, num_units);
  case 2:
   return bestfit(prev_block, num_units);
  case 3:
   return worstfit(prev_block, num_units);
  default:
   return firstfit(prev_block, num_units);
 }
}


bestfit

decompiled_code:
__int64 __fastcall bestfit(__int64 *a1, __int64 a2)
{
unsigned int v2; // er14
_QWORD *v3; // r13
__int64 v4; // rbp
__int64 v5; // rbx
__int64 i; // r12
__int64 v7; // rsi
__int64 v9; // rax

v2 = a2;
v3 = 0LL;
v4 = (__int64)a1;
v5 = 0LL;
printf("\n(void*) bestfit(...), looking for blocksize: %d", a2);
for ( i = *a1; ; i = *(_QWORD *)i )
{
v7 = *(unsigned int *)(i + 8);
if ( (unsigned int)v7 >= v2 )
break;
if ( p_free_blocks == i )
{
if ( v5 )
goto LABEL_12;
i = morecore(v2);
if ( !i )
return 0LL;
}
LABEL_5:
v4 = i;
}
if ( !v5 )
{
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound first suitable block, size: %d", v7);
LABEL_4:
if ( i == p_free_blocks )
goto LABEL_12;
goto LABEL_5;
}
if ( (unsigned int)v7 >= *(_DWORD *)(v5 + 8) )
goto LABEL_4;
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound better block, size: %d", v7);
if ( i != p_free_blocks )
goto LABEL_5;
LABEL_12:
if ( *(_DWORD *)(v5 + 8) == v2 )
{
printf("\nblock size is exact");
*v3 = *(_QWORD *)v5;
}
else
{
printf("\nblocksize is to big, splitting");
v9 = *(_DWORD *)(v5 + 8) - v2;
*(_DWORD *)(v5 + 8) = v9;
v5 += 16 * v9;
*(_DWORD *)(v5 + 8) = v2;
}
p_free_blocks = v4;
return v5 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, __int64 a2)
{
unsigned int v2; // er14
_QWORD *v3; // r13
__int64 v4; // rbp
__int64 v5; // rbx
__int64 i; // r12
__int64 v7; // rsi
__int64 v9; // rax

v2 = a2;
v3 = 0LL;
v4 = (__int64)a1;
v5 = 0LL;
printf("\n(void*) bestfit(...), looking for blocksize: %d", a2);
for ( i = *a1; ; i = *(_QWORD *)i )
{
v7 = *(unsigned int *)(i + 8);
if ( (unsigned int)v7 >= v2 )
break;
if ( p_free_blocks == i )
{
if ( v5 )
goto LABEL_12;
i = func1(v2);
if ( !i )
return 0LL;
}
LABEL_5:
v4 = i;
}
if ( !v5 )
{
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound first suitable block, size: %d", v7);
LABEL_4:
if ( i == p_free_blocks )
goto LABEL_12;
goto LABEL_5;
}
if ( (unsigned int)v7 >= *(_DWORD *)(v5 + 8) )
goto LABEL_4;
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound better block, size: %d", v7);
if ( i != p_free_blocks )
goto LABEL_5;
LABEL_12:
if ( *(_DWORD *)(v5 + 8) == v2 )
{
printf("\nblock size is exact");
*v3 = *(_QWORD *)v5;
}
else
{
printf("\nblocksize is to big, splitting");
v9 = *(_DWORD *)(v5 + 8) - v2;
*(_DWORD *)(v5 + 8) = v9;
v5 += 16 * v9;
*(_DWORD *)(v5 + 8) = v2;
}
p_free_blocks = v4;
return v5 + 16;
}

original_code:
void *bestfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) bestfit(...), looking for blocksize: %d", num_units);
 Header *p;
 Header *currentBest = ((void *)0);
 Header *currentBestPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentBest == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentBest = p;
    currentBestPrev = prev_block;
   } else {
    if (p->s.size < currentBest->s.size) {
     printf("\nfound better block, size: %d", p->s.size);
     currentBest = p;
     currentBestPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentBest != ((void *)0)) ) {
   if (currentBest->s.size == num_units) {
    printf("\nblock size is exact");
    currentBestPrev->s.next = currentBest->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentBest->s.size -= num_units;
    currentBest += currentBest->s.size;
    currentBest->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentBest+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}

canonical_original_code:
void *bestfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) bestfit(...), looking for blocksize: %d", num_units);
 union header *p;
 union header *currentBest = ((void *)0);
 union header *currentBestPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentBest == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentBest = p;
    currentBestPrev = prev_block;
   } else {
    if (p->s.size < currentBest->s.size) {
     printf("\nfound better block, size: %d", p->s.size);
     currentBest = p;
     currentBestPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentBest != ((void *)0)) ) {
   if (currentBest->s.size == num_units) {
    printf("\nblock size is exact");
    currentBestPrev->s.next = currentBest->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentBest->s.size -= num_units;
    currentBest += currentBest->s.size;
    currentBest->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentBest+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}


worstfit

decompiled_code:
__int64 __fastcall worstfit(__int64 *a1, __int64 a2)
{
unsigned int v2; // er14
_QWORD *v3; // r13
__int64 v4; // rbp
__int64 v5; // rbx
__int64 i; // r12
__int64 v7; // rsi
__int64 v9; // rax

v2 = a2;
v3 = 0LL;
v4 = (__int64)a1;
v5 = 0LL;
printf("\n(void*) worstfit(...), looking for blocksize: %d", a2);
for ( i = *a1; ; i = *(_QWORD *)i )
{
v7 = *(unsigned int *)(i + 8);
if ( (unsigned int)v7 >= v2 )
break;
if ( p_free_blocks == i )
{
if ( v5 )
goto LABEL_12;
i = morecore(v2);
if ( !i )
return 0LL;
}
LABEL_5:
v4 = i;
}
if ( !v5 )
{
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound first suitable block, size: %d", v7);
LABEL_4:
if ( i == p_free_blocks )
goto LABEL_12;
goto LABEL_5;
}
if ( (unsigned int)v7 <= *(_DWORD *)(v5 + 8) )
goto LABEL_4;
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound worse block, size: %d", v7);
if ( i != p_free_blocks )
goto LABEL_5;
LABEL_12:
if ( *(_DWORD *)(v5 + 8) == v2 )
{
printf("\nblock size is exact");
*v3 = *(_QWORD *)v5;
}
else
{
printf("\nblocksize is to big, splitting");
v9 = *(_DWORD *)(v5 + 8) - v2;
*(_DWORD *)(v5 + 8) = v9;
v5 += 16 * v9;
*(_DWORD *)(v5 + 8) = v2;
}
p_free_blocks = v4;
return v5 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, __int64 a2)
{
unsigned int v2; // er14
_QWORD *v3; // r13
__int64 v4; // rbp
__int64 v5; // rbx
__int64 i; // r12
__int64 v7; // rsi
__int64 v9; // rax

v2 = a2;
v3 = 0LL;
v4 = (__int64)a1;
v5 = 0LL;
printf("\n(void*) worstfit(...), looking for blocksize: %d", a2);
for ( i = *a1; ; i = *(_QWORD *)i )
{
v7 = *(unsigned int *)(i + 8);
if ( (unsigned int)v7 >= v2 )
break;
if ( p_free_blocks == i )
{
if ( v5 )
goto LABEL_12;
i = func1(v2);
if ( !i )
return 0LL;
}
LABEL_5:
v4 = i;
}
if ( !v5 )
{
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound first suitable block, size: %d", v7);
LABEL_4:
if ( i == p_free_blocks )
goto LABEL_12;
goto LABEL_5;
}
if ( (unsigned int)v7 <= *(_DWORD *)(v5 + 8) )
goto LABEL_4;
v3 = (_QWORD *)v4;
v5 = i;
printf("\nfound worse block, size: %d", v7);
if ( i != p_free_blocks )
goto LABEL_5;
LABEL_12:
if ( *(_DWORD *)(v5 + 8) == v2 )
{
printf("\nblock size is exact");
*v3 = *(_QWORD *)v5;
}
else
{
printf("\nblocksize is to big, splitting");
v9 = *(_DWORD *)(v5 + 8) - v2;
*(_DWORD *)(v5 + 8) = v9;
v5 += 16 * v9;
*(_DWORD *)(v5 + 8) = v2;
}
p_free_blocks = v4;
return v5 + 16;
}

original_code:
void *worstfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) worstfit(...), looking for blocksize: %d", num_units);
 Header *p;
 Header *currentWorst = ((void *)0);
 Header *currentWorstPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentWorst == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentWorst = p;
    currentWorstPrev = prev_block;
   } else {
    if (p->s.size > currentWorst->s.size) {
     printf("\nfound worse block, size: %d", p->s.size);
     currentWorst = p;
     currentWorstPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentWorst != ((void *)0)) ) {
   if (currentWorst->s.size == num_units) {
    printf("\nblock size is exact");
    currentWorstPrev->s.next = currentWorst->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentWorst->s.size -= num_units;
    currentWorst += currentWorst->s.size;
    currentWorst->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentWorst+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}

canonical_original_code:
void *worstfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) worstfit(...), looking for blocksize: %d", num_units);
 union header *p;
 union header *currentWorst = ((void *)0);
 union header *currentWorstPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentWorst == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentWorst = p;
    currentWorstPrev = prev_block;
   } else {
    if (p->s.size > currentWorst->s.size) {
     printf("\nfound worse block, size: %d", p->s.size);
     currentWorst = p;
     currentWorstPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentWorst != ((void *)0)) ) {
   if (currentWorst->s.size == num_units) {
    printf("\nblock size is exact");
    currentWorstPrev->s.next = currentWorst->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentWorst->s.size -= num_units;
    currentWorst += currentWorst->s.size;
    currentWorst->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentWorst+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}


decompiled_code:
void linear_realloc()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void *linear_realloc(void *ptr, size_t size) {
}

canonical_original_code:
void *linear_realloc(void *ptr, unsigned long size) {
}


morecore

decompiled_code:
__int64 __fastcall morecore(unsigned int a1)
{
unsigned int v1; // ebx
_DWORD *v2; // rax

v1 = a1;
printf("\nlinear_morecore");
if ( a1 < 0x400 )
v1 = 1024;
v2 = sbrk(16LL * v1);
if ( v2 == (_DWORD *)-1LL )
return 0LL;
v2[2] = v1;
linear_free((__int64)(v2 + 4));
return p_free_blocks;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // ebx
_DWORD *v2; // rax

v1 = a1;
printf("\nlinear_morecore");
if ( a1 < 0x400 )
v1 = 1024;
v2 = sbrk(16LL * v1);
if ( v2 == (_DWORD *)-1LL )
return 0LL;
v2[2] = v1;
func0((__int64)(v2 + 4));
return p_free_blocks;
}

original_code:
static Header *morecore(unsigned units) {
 printf("\nlinear_morecore");
 char *cp;
 Header *new_block;
 if (units < 1024)
  units = 1024;
 cp = sbrk(units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 new_block = (Header *) cp;
 new_block->s.size = units;
 linear_free((void *)(new_block+1));
 return p_free_blocks;
}

canonical_original_code:
static union header *morecore(unsigned int units) {
 printf("\nlinear_morecore");
 char *cp;
 union header *new_block;
 if (units < 1024)
  units = 1024;
 cp = sbrk(units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 new_block = (union header *) cp;
 new_block->s.size = units;
 linear_free((void *)(new_block+1));
 return p_free_blocks;
}


xmalloc

decompiled_code:
// attributes: thunk
__int64 xmalloc()
{
return XMEM_CALLBACK();
}

canonical_decompiled_code:
__int64 func0()
{
return XMEM_CALLBACK();
}

original_code:
void *xmalloc(size_t size) {
 return XMEM_CALLBACK.XMEM_malloc(size);
}

canonical_original_code:
void *xmalloc(unsigned long size) {
 return XMEM_CALLBACK.XMEM_malloc(size);
}


xfree

decompiled_code:
// attributes: thunk
__int64 xfree()
{
return (*(&XMEM_CALLBACK + 1))();
}

canonical_decompiled_code:
__int64 func1()
{
return (*(&XMEM_CALLBACK + 1))();
}

original_code:
void xfree(void *ptr) {
 return XMEM_CALLBACK.XMEM_free(ptr);
}


xrealloc

decompiled_code:
void xrealloc()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void *xrealloc(void *ptr, size_t size) {
}

canonical_original_code:
void *xrealloc(void *ptr, unsigned long size) {
}


repo: welschmorgan/calc

decompiled_code:
int __fastcall node_print(__int64 a1, int a2, int a3)
{
__int64 v3; // r8
__int64 v4; // rcx
unsigned int v5; // er10
bool v6; // zf
const char *v7; // rsi
int result; // eax
void *v9; // rsi

if ( a1 )
{
v3 = *(unsigned int *)(a1 + 16);
v4 = *(unsigned int *)(a1 + 20);
v5 = *(char *)(a1 + 24);
if ( a3 )
{
v6 = a2 == 0;
v9 = &unk_253;
if ( v6 )
v9 = &unk_255;
result = printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n", v9, v5, v4, v3, *(_QWORD *)(a1 + 8), *(_QWORD *)a1);
}
else
{
v6 = a2 == 0;
v7 = (const char *)&unk_253;
if ( v6 )
v7 = (const char *)&unk_255;
result = printf("%s%i%c (priority: %i)\n", v7, v5, v4, v3);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2, int a3)
{
__int64 v3; // r8
__int64 v4; // rcx
unsigned int v5; // er10
bool v6; // zf
const char *v7; // rsi
int result; // eax
void *v9; // rsi

if ( a1 )
{
v3 = *(unsigned int *)(a1 + 16);
v4 = *(unsigned int *)(a1 + 20);
v5 = *(char *)(a1 + 24);
if ( a3 )
{
v6 = a2 == 0;
v9 = &unk_253;
if ( v6 )
v9 = &unk_255;
result = printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n", v9, v5, v4, v3, *(_QWORD *)(a1 + 8), *(_QWORD *)a1);
}
else
{
v6 = a2 == 0;
v7 = (const char *)&unk_253;
if ( v6 )
v7 = (const char *)&unk_255;
result = printf("%s%i%c (priority: %i)\n", v7, v5, v4, v3);
}
}
return result;
}

original_code:
void node_print(t_node*n, int sub_level, int debug)
{
 if(!n) return;
 char subs[20];
 int i=0;
 for( i=0; i<((((sub_level<0)?-sub_level:sub_level)<20)?((sub_level<0)?-sub_level:sub_level):20);i++ )
  subs[i] = '\t';
 subs[i] = '\0';
 if( !debug )
  printf("%s%i%c (priority: %i)\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level);
 else
  printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level,
    n->prev,
    n->next);
}

canonical_original_code:
void node_print(struct s_node *n, int sub_level, int debug)
{
 if(!n) return;
 char subs[20];
 int i = 0;
 for( i=0; i<((((sub_level<0)?-sub_level:sub_level)<20)?((sub_level<0)?-sub_level:sub_level):20);i++ )
  subs[i] = '\t';
 subs[i] = '\0';
 if( !debug )
  printf("%s%i%c (priority: %i)\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level);
 else
  printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level,
    n->prev,
    n->next);
}

variable_types: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *

variable_types: debug int

variable_types:
subs
char [20]


original_code:
void list_init(t_list*l)
{
 if(!l) return;
 l->tail=l->head=((void *)0);
 l->level=0;
}

canonical_original_code:
void list_init(struct s_list *l)
{
 if(!l) return;
 l->tail=l->head=((void *)0);
 l->level=0;
}

variable_types: struct s_list { struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *tail; struct s_node *head; int level; } *

user_defined_types: struct s_list { struct s_node *tail; struct s_node *head; int level; }


decompiled_code:
void list_alloc()
{
void *v0; // rax

v0 = malloc(0x18uLL);
list_init((__int64)v0);
}

canonical_decompiled_code:
void func3()
{
void *v0; // rax

v0 = malloc(0x18uLL);
func2((__int64)v0);
}

original_code:
t_list* list_alloc()
{
 t_list* l = (t_list*)malloc(sizeof(t_list));
 list_init(l);
 return l;
}

canonical_original_code:
struct s_list *list_alloc()
{
 struct s_list *l = (t_list*)malloc(sizeof(t_list));
 list_init(l);
 return l;
}

return_type: struct s_list { struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *tail; struct s_node *head; int level; } *


decompiled_code:
void __fastcall list_free(void *ptr)
{
if ( ptr )
{
list_clear((__int64)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
if ( ptr )
{
func4((__int64)ptr);
free(ptr);
}
}

original_code:
void list_free(t_list*l)
{
 if(!l) return;
 list_clear(l);
 free(l);
}

canonical_original_code:
void list_free(struct s_list *l)
{
 if(!l) return;
 list_clear(l);
 free(l);
}

user_defined_types: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; }


original_code:
void list_push(t_list*l,t_node*n)
{
 t_node * oldHead;
 if(!l) return;
 if(!l->head)
 {
  l->head=n;
 }
 else
 {
  oldHead=l->head;
  oldHead->next=n;
  n->prev = oldHead;
  l->head = n;
 }
}

canonical_original_code:
void list_push(struct s_list *l, struct s_node *n)
{
 struct s_node *oldHead;
 if(!l) return;
 if(!l->head)
 {
  l->head=n;
 }
 else
 {
  oldHead=l->head;
  oldHead->next=n;
  n->prev = oldHead;
  l->head = n;
 }
}

variable_types:
n
struct s_node *


original_code:
void node_init(t_node*n, int lvl, char op, int value)
{
 if(!n) return;
 n->level=lvl;
 n->op=op;
 n->value=value;
 n->next=n->prev=((void *)0);
}

canonical_original_code:
void node_init(struct s_node *n, int lvl, char op, int value)
{
 if(!n) return;
 n->level=lvl;
 n->op=op;
 n->value=value;
 n->next=n->prev=((void *)0);
}

variable_types: lvl int

variable_types: op char

variable_types: value int


decompiled_code:
void __fastcall node_alloc(int a1, char a2, int a3)
{
void *v4; // rax

v4 = malloc(0x20uLL);
node_init((__int64)v4, a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func8(int a1, char a2, int a3)
{
void *v4; // rax

v4 = malloc(0x20uLL);
func7((__int64)v4, a1, a2, a3);
}

original_code:
t_node* node_alloc(int lvl, char op, int value)
{
 t_node * n=(t_node*)malloc(sizeof(t_node));
 node_init(n,lvl,op,value);
 return n;
}

canonical_original_code:
struct s_node *node_alloc(int lvl, char op, int value)
{
 struct s_node *n = (t_node*)malloc(sizeof(t_node));
 node_init(n,lvl,op,value);
 return n;
}

return_type: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *


decompiled_code:
void __fastcall node_free(_QWORD *a1)
{
_QWORD *v1; // rax

if ( a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
*v1 = *a1;
free(a1);
}
}

canonical_decompiled_code:
void __fastcall func9(_QWORD *a1)
{
_QWORD *v1; // rax

if ( a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
*v1 = *a1;
free(a1);
}
}

original_code:
void node_free( t_node*n ){
 if(!n) return;
 if(n->prev)
  n->prev->next = n->next;
 free(n);
}

canonical_original_code:
void node_free(struct s_node *n){
 if(!n) return;
 if(n->prev)
  n->prev->next = n->next;
 free(n);
}


decompiled_code:
void __fastcall list_pop(__int64 a1)
{
_QWORD *v1; // rdi

if ( a1 )
{
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
node_free(v1);
}
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
_QWORD *v1; // rdi

if ( a1 )
{
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
func9(v1);
}
}

original_code:
void list_pop(t_list*l)
{
 if(!l||!l->head) return;
 node_free(l->head);
}

canonical_original_code:
void list_pop(struct s_list *l)
{
 if(!l||!l->head) return;
 node_free(l->head);
}


decompiled_code:
void signal_handler()
{
interrupted = 1;
}

canonical_decompiled_code:
void func7()
{
interrupted = 1;
}

original_code:
static void signal_handler(int signal) {
 interrupted = 1;
}

variable_types: signal int


decompiled_code:
void *__fastcall cpu_init(__int64 a1)
{
_OWORD *v1; // rdi

v1 = (_OWORD *)(a1 + 32);
*(v1 - 2) = 0LL;
*(v1 - 1) = 0LL;
return memset(v1, 0, 0xFFFFuLL);
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1)
{
_OWORD *v1; // rdi

v1 = (_OWORD *)(a1 + 32);
*(v1 - 2) = 0LL;
*(v1 - 1) = 0LL;
return memset(v1, 0, 0xFFFFuLL);
}

original_code:
void cpu_init(context_t *c) {
 memset(c->reg, 0, 16 * (sizeof(word_t) / sizeof(uint8_t)));
 memset(c->mem, 0, 0xFFFF * (sizeof(byte_t) / sizeof(uint8_t)));
}

canonical_original_code:
void cpu_init(struct context_t *c) {
 memset(c->reg, 0, 16 * (sizeof(word_t) / sizeof(unsigned char)));
 memset(c->mem, 0, 0xFFFF * (sizeof(byte_t) / sizeof(unsigned char)));
}

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; } *

user_defined_types: struct context_t { struct word_t reg[16]; unsigned char mem[65535]; }


decompiled_code:
void __fastcall cpu_wregw(__int64 a1, unsigned __int8 a2, __int16 a3)
{
*(_WORD *)(a1 + 2LL * a2) = a3;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, unsigned __int8 a2, __int16 a3)
{
*(_WORD *)(a1 + 2LL * a2) = a3;
}

original_code:
void cpu_wregw(context_t *c, raddr_t r, word_t v) {
 memcpy(&c->reg[r], &v, sizeof(word_t));
}

canonical_original_code:
void cpu_wregw(struct context_t *c, unsigned char r, struct word_t v) {
 memcpy(&c->reg[r], &v, sizeof(word_t));
}

variable_types:
r
unsigned char

variable_types:
v
struct word_t

user_defined_types: struct word_t { unsigned char l; unsigned char h; }


decompiled_code:
__int64 __fastcall cpu_rregw(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

original_code:
word_t *cpu_rregw(context_t *c, raddr_t r) {
 return &c->reg[r];
}

canonical_original_code:
struct word_t *cpu_rregw(struct context_t *c, unsigned char r) {
 return &c->reg[r];
}

variable_types: struct context_t { struct word_t reg[16]; unsigned char mem[65535]; } *

return_type: struct word_t { unsigned char l; unsigned char h; } *


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

result = a3;
*(_WORD *)(a1 + 2LL * a2) = a3;
return result;
}

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

result = a3;
*(_WORD *)(a1 + 2LL * a2) = a3;
return result;
}

original_code:
void cpu_wregb(context_t *c, raddr_t r, byte_t v) {
 word_t w;
 w.l = v;
 w.h = 0x00;
 memcpy(&c->reg[r], &w, sizeof(word_t));
}

canonical_original_code:
void cpu_wregb(struct context_t *c, unsigned char r, unsigned char v) {
 struct word_t w;
 w.l = v;
 w.h = 0x00;
 memcpy(&c->reg[r], &w, sizeof(word_t));
}

variable_types:
v
unsigned char

variable_types: struct word_t { unsigned char l; unsigned char h; }


decompiled_code:
__int64 __fastcall cpu_rregb(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

original_code:
byte_t *cpu_rregb(context_t *c, raddr_t r) {
 return &c->reg[r].l;
}

canonical_original_code:
unsigned char *cpu_rregb(struct context_t *c, unsigned char r) {
 return &c->reg[r].l;
}

return_type: unsigned char *


decompiled_code:
__int64 __fastcall cpu_rmemw(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

original_code:
word_t *cpu_rmemw(context_t *c, maddr_t a) {
 word_t *p = (word_t *)(&c->mem[a]);
 return p;
}

canonical_original_code:
struct word_t *cpu_rmemw(struct context_t *c, unsigned short a) {
 struct word_t *p = (word_t *)(&c->mem[a]);
 return p;
}

variable_types:
a
unsigned short

variable_types: struct word_t { unsigned char l; unsigned char h; } *


decompiled_code:
void __fastcall cpu_reset(__int64 a1)
{
__int16 *v1; // rax

v1 = (__int16 *)cpu_rmemw(a1, 0xFFFEu);
cpu_wregw(a1, 0, *v1);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
__int16 *v1; // rax

v1 = (__int16 *)func6(a1, 0xFFFEu);
func1(a1, 0, *v1);
}

original_code:
void cpu_reset(context_t *c) {
 word_t *rv = cpu_rmemw(c, 0xfffe);
 cpu_wregw(c, 0, *rv);
}

canonical_original_code:
void cpu_reset(struct context_t *c) {
 struct word_t *rv = cpu_rmemw(c, 0xfffe);
 cpu_wregw(c, 0, *rv);
}


decompiled_code:
// attributes: thunk
__int64 __fastcall cpu_rmemb(__int64 a1, unsigned __int16 a2)
{
return cpu_rmemw(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int16 a2)
{
return func6(a1, a2);
}

original_code:
byte_t *cpu_rmemb(context_t *c, maddr_t a) {
 byte_t *p = (byte_t *)(&c->mem[a]);
 return p;
}

canonical_original_code:
unsigned char *cpu_rmemb(struct context_t *c, unsigned short a) {
 unsigned char *p = (byte_t *)(&c->mem[a]);
 return p;
}

variable_types:
p
unsigned char *


decompiled_code:
void __fastcall cpu_process_instruction(__int64 a1)
{
unsigned __int16 *v1; // rax
char *v2; // rax
__int16 v3; // r9
_WORD *v4; // rdx
char v5; // bl
char v6; // cl
unsigned __int8 v7; // si
unsigned __int8 *v8; // rax
unsigned __int8 v9; // r8
__int16 *v10; // rax
unsigned __int8 v11; // r8
unsigned __int8 *v12; // rax
unsigned __int8 v13; // r8
__int16 v14; // r9
char v15; // r10
_WORD *v16; // rdx
unsigned __int16 *v17; // rax
char *v18; // rcx
_WORD *v19; // rdx
__int16 v20; // r9
char v21; // r10

v1 = (unsigned __int16 *)cpu_rregw(a1, 0);
v2 = (char *)cpu_rmemw(a1, *v1);
*v4 = v3 + 2;
v5 = (unsigned __int8)*v2 >> 7;
v6 = ((unsigned __int8)*v2 >> 4) & 3;
if ( (unsigned __int8)v2[1] >> 4 == 4 )
{
if ( v6 )
{
if ( v6 == 3 )
{
v12 = (unsigned __int8 *)cpu_rmemw(a1, v3 + 2);
*v16 = v14 + 4;
if ( v5 )
{
v17 = (unsigned __int16 *)cpu_rmemw(a1, v14 + 4);
*v19 = v20;
if ( v21 )
cpu_wmemb(a1, *v17, *v18);
else
cpu_wmemw(a1, *v17, *(_WORD *)v18);
}
else if ( v15 )
{
cpu_wregb(a1, v13, *v12);
}
else
{
cpu_wregw(a1, v13, *(_WORD *)v12);
}
}
}
else if ( *v2 >= 0 )
{
v7 = v2[1] & 0xF;
if ( (*v2 & 0x40) != 0 )
{
v8 = (unsigned __int8 *)cpu_rregb(a1, v7);
cpu_wregb(a1, v9, *v8);
}
else
{
v10 = (__int16 *)cpu_rregw(a1, v7);
cpu_wregw(a1, v11, *v10);
}
}
}
}

original_code:
int cpu_process_instruction(context_t *c) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 word_t *sv;
 word_t *dv;
 word_t *pc = cpu_rregw(c, 0);
 maddr_t a = *((maddr_t *)(pc));
 word_t *inst = cpu_rmemw(c, a);
 (*(maddr_t *)(pc)) += sizeof(word_t);
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  switch(op) {
   case 0b0100:
    if(as == 0) {
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, *cpu_rregb(c, sr)); else cpu_wregw(c, dr, *cpu_rregw(c, sr));
     }
    }
    else if(as == 3) {
     a = *((maddr_t *)(pc));
     sv = cpu_rmemw(c, a);
     (*(maddr_t *)(pc)) += sizeof(word_t);
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, sv->l); else cpu_wregw(c, dr, *sv);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + sizeof(word_t));
      (*(maddr_t *)(pc)) += sizeof(word_t);
      if(bw) cpu_wmemb(c, *((maddr_t *)(dv)), sv->l); else cpu_wmemw(c, *((maddr_t *)(dv)), *sv);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

canonical_original_code:
int cpu_process_instruction(struct context_t *c) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 struct word_t *sv;
 struct word_t *dv;
 struct word_t *pc = cpu_rregw(c, 0);
 unsigned short a = *((maddr_t *)(pc));
 struct word_t *inst = cpu_rmemw(c, a);
 (*(unsigned short *)(pc)) += sizeof(word_t);
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  switch(op) {
   case 0b0100:
    if(as == 0) {
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, *cpu_rregb(c, sr)); else cpu_wregw(c, dr, *cpu_rregw(c, sr));
     }
    }
    else if(as == 3) {
     a = *((unsigned short *)(pc));
     sv = cpu_rmemw(c, a);
     (*(unsigned short *)(pc)) += sizeof(word_t);
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, sv->l); else cpu_wregw(c, dr, *sv);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + sizeof(word_t));
      (*(unsigned short *)(pc)) += sizeof(word_t);
      if(bw) cpu_wmemb(c, *((unsigned short *)(dv)), sv->l); else cpu_wmemw(c, *((unsigned short *)(dv)), *sv);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

variable_types: op int

variable_types: ad int

variable_types: bw int

variable_types: as int

variable_types: sr int

variable_types: dr int


original_code:
int cpu_disasm_instruction(context_t *c, maddr_t a, char *buf) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 word_t *sv;
 word_t *dv;
 word_t *inst = cpu_rmemw(c, a);
 buf[0] = 0;
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  sprintf(&buf[strlen(buf)], "[op=%02x, sr=%02x, ad=%02x, bw=%02x, as=%02x, dr=%02x]\t", op, sr, ad, bw, as, dr);
  switch(op) {
   case 0b0100:
    strcat(buf, "mov.");
    if(bw) strcat(buf, "b"); else strcat(buf, "w");
    if(as == 0) {
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " R%d, R%d", sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      sprintf(&buf[strlen(buf)], " R%d, 0x%02x%02x(R%d)", sr, dv->h, dv->l, dr);
     }
    }
    else if(as == 1) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), R%d", sv->h, sv->l, sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(dr == 2) {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), &0x%02x%02x",
        sv->h, sv->l, sr, dv->h, dv->l);
      }
      else {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), 0x%02x%02x(R%d)",
        sv->h, sv->l, sr, dv->h, dv->l, dr);
      }
     }
    }
    else if(as == 3) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, R%d", sv->l, dr);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, R%d", sv->h, sv->l, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, &0x%02x%02x", sv->l, dv->h, dv->l);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, &0x%02x%02x", sv->h, sv->l, dv->h, dv->l);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

canonical_original_code:
int cpu_disasm_instruction(struct context_t *c, unsigned short a, char *buf) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 struct word_t *sv;
 struct word_t *dv;
 struct word_t *inst = cpu_rmemw(c, a);
 buf[0] = 0;
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  sprintf(&buf[strlen(buf)], "[op=%02x, sr=%02x, ad=%02x, bw=%02x, as=%02x, dr=%02x]\t", op, sr, ad, bw, as, dr);
  switch(op) {
   case 0b0100:
    strcat(buf, "mov.");
    if(bw) strcat(buf, "b"); else strcat(buf, "w");
    if(as == 0) {
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " R%d, R%d", sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      sprintf(&buf[strlen(buf)], " R%d, 0x%02x%02x(R%d)", sr, dv->h, dv->l, dr);
     }
    }
    else if(as == 1) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), R%d", sv->h, sv->l, sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(dr == 2) {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), &0x%02x%02x",
        sv->h, sv->l, sr, dv->h, dv->l);
      }
      else {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), 0x%02x%02x(R%d)",
        sv->h, sv->l, sr, dv->h, dv->l, dr);
      }
     }
    }
    else if(as == 3) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, R%d", sv->l, dr);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, R%d", sv->h, sv->l, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, &0x%02x%02x", sv->l, dv->h, dv->l);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, &0x%02x%02x", sv->h, sv->l, dv->h, dv->l);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

variable_types:
buf
char *