original_code:
void dump_context(Context* context)
{
 kprintf("CONTEXT DUMP:\n");
 kprintf("RDI: 0x%x\n", context->rdi);
 kprintf("RSI: 0x%x\n", context->rsi);
 kprintf("RAX: 0x%x\n", context->rax);
 kprintf("RBX: 0x%x\n", context->rbx);
 kprintf("RCX: 0x%x\n", context->rcx);
 kprintf("RDX: 0x%x\n", context->rdx);
 kprintf("rbp: 0x%x\n", context->rbp);
 kprintf("r8 : 0x%x\n", context->r8);
 kprintf("r9 : 0x%x\n", context->r9);
 kprintf("r10: 0x%x\n", context->r10);
 kprintf("r11: 0x%x\n", context->r11);
 kprintf("r12: 0x%x\n", context->r12);
 kprintf("r13: 0x%x\n", context->r13);
 kprintf("r14: 0x%x\n", context->r14);
 kprintf("r15: 0x%x\n", context->r15);
 kprintf("vec: 0x%x\n", context->vector);
 kprintf("err: 0x%x\n", context->error_code);
 kprintf("cs : 0x%x\n", context->cs);
 kprintf("rfl: 0x%x\n", context->rflags);
 kprintf("rsp: 0x%x\n", context->rsp);
 kprintf("ss : 0x%x\n", context->ss);
 kprintf("rip: 0x%x\n", context->rip);
}

canonical_original_code:
void dump_context(struct Context *context)
{
 kprintf("CONTEXT DUMP:\n");
 kprintf("RDI: 0x%x\n", context->rdi);
 kprintf("RSI: 0x%x\n", context->rsi);
 kprintf("RAX: 0x%x\n", context->rax);
 kprintf("RBX: 0x%x\n", context->rbx);
 kprintf("RCX: 0x%x\n", context->rcx);
 kprintf("RDX: 0x%x\n", context->rdx);
 kprintf("rbp: 0x%x\n", context->rbp);
 kprintf("r8 : 0x%x\n", context->r8);
 kprintf("r9 : 0x%x\n", context->r9);
 kprintf("r10: 0x%x\n", context->r10);
 kprintf("r11: 0x%x\n", context->r11);
 kprintf("r12: 0x%x\n", context->r12);
 kprintf("r13: 0x%x\n", context->r13);
 kprintf("r14: 0x%x\n", context->r14);
 kprintf("r15: 0x%x\n", context->r15);
 kprintf("vec: 0x%x\n", context->vector);
 kprintf("err: 0x%x\n", context->error_code);
 kprintf("cs : 0x%x\n", context->cs);
 kprintf("rfl: 0x%x\n", context->rflags);
 kprintf("rsp: 0x%x\n", context->rsp);
 kprintf("ss : 0x%x\n", context->ss);
 kprintf("rip: 0x%x\n", context->rip);
}

variable_types: struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; } *


decompiled_code:
__int64 __fastcall default_handler(unsigned __int64 a1, __int64 a2)
{
_QWORD *v3; // r12
const char *v4; // rdi
__int64 result; // rax

kprintf("Interrupt! vector: %u - Code: %u \n", a1, a2);
v3 = (_QWORD *)*current_pcb;
kprintf(" PCB: 0x%x\n", current_pcb);
kprintf(" Context: 0x%x\n", v3);
kprintf(" Faulting address: 0x%x\n", v3[17]);
dump_context(v3);
v4 = "Page Fault";
if ( a1 == 14 )
return panic(v4);
if ( a1 == 13 )
{
v4 = "GPF!";
return panic(v4);
}
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int64 a1, __int64 a2)
{
_QWORD *v3; // r12
const char *v4; // rdi
__int64 result; // rax

kprintf("Interrupt! vector: %u - Code: %u \n", a1, a2);
v3 = (_QWORD *)*current_pcb;
kprintf(" PCB: 0x%x\n", current_pcb);
kprintf(" Context: 0x%x\n", v3);
kprintf(" Faulting address: 0x%x\n", v3[17]);
func3(v3);
v4 = "Page Fault";
if ( a1 == 14 )
return panic(v4);
if ( a1 == 13 )
{
v4 = "GPF!";
return panic(v4);
}
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

original_code:
static void default_handler(uint64_t vector, uint64_t code)
{
 extern PCB* current_pcb;
 kprintf("Interrupt! vector: %u - Code: %u \n", vector, code);
 uint64_t context_addr = (uint64_t)current_pcb->context;
 Context* context = (Context*)context_addr;
 kprintf(" PCB: 0x%x\n", current_pcb);
 kprintf(" Context: 0x%x\n", context);
 kprintf(" Faulting address: 0x%x\n", context->rip);
 dump_context(context);
 if (vector == 14)
 {
  panic("Page Fault");
 }
 if (vector == 13)
 {
  panic("GPF!");
 }
 pic_acknowledge(vector);
}

canonical_original_code:
static void default_handler(unsigned long vector, unsigned long code)
{
 extern struct PCB *current_pcb;
 kprintf("Interrupt! vector: %u - Code: %u \n", vector, code);
 unsigned long context_addr = (uint64_t)current_pcb->context;
 struct Context *context = (Context*)context_addr;
 kprintf(" PCB: 0x%x\n", current_pcb);
 kprintf(" Context: 0x%x\n", context);
 kprintf(" Faulting address: 0x%x\n", context->rip);
 dump_context(context);
 if (vector == 14)
 {
  panic("Page Fault");
 }
 if (vector == 13)
 {
  panic("GPF!");
 }
 pic_acknowledge(vector);
}

variable_types:
vector
unsigned long

variable_types:
code
unsigned long

variable_types: struct PCB { struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; } *context; void *page_table; unsigned int sleep_time; unsigned int pid; unsigned int ppid; enum State { READY=0, RUNNING=1, SLEEPING=2, KILLED=3 } state; enum Priority { HIGH=0, NORMAL=1, LOW=2, IDLE=3 } priority; } *

return_type: void

user_defined_types: struct PCB { struct Context *context; void *page_table; unsigned int sleep_time; unsigned int pid; unsigned int ppid; enum State state; enum Priority priority; }


decompiled_code:
__int64 interrupts_init()
{
__int64 v0; // rbx
__int64 v1; // rdi

v0 = 0LL;
memclr(&init_pcb, 40LL);
qword_CC8 = kernel_table;
current_pcb = &init_pcb;
idt = (__int64)&start_idt_64 - 0x800000000000LL;
setup_tss_descriptor();
do
{
set_idt_entry(v0, isr_stub_table[v0]);
v1 = v0++;
interrupts_install_isr(v1, (__int64)default_handler);
}
while ( v0 != 256 );
interrupts_install_isr(36LL, (__int64)serial_handler);
interrupts_install_isr(39LL, (__int64)spurious_handler);
return apic_init();
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx
__int64 v1; // rdi

v0 = 0LL;
memclr(&init_pcb, 40LL);
qword_CC8 = kernel_table;
current_pcb = &init_pcb;
idt = (__int64)&start_idt_64 - 0x800000000000LL;
setup_tss_descriptor();
do
{
func2(v0, isr_stub_table[v0]);
v1 = v0++;
interrupts_install_isr(v1, (__int64)default_handler);
}
while ( v0 != 256 );
interrupts_install_isr(36LL, (__int64)serial_handler);
interrupts_install_isr(39LL, (__int64)spurious_handler);
return apic_init();
}

original_code:
void interrupts_init()
{
 memclr(&init_pcb, sizeof(PCB));
 init_pcb.page_table = kernel_table;
 current_pcb = &init_pcb;
 idt = ((void*)((uint64_t)(&start_idt_64[0]) + 0xFFFF800000000000));
 setup_tss_descriptor();
 extern interrupt_handler isr_stub_table[256];
 for (uint64_t i = 0; i < 256; ++i)
 {
  set_idt_entry(i, isr_stub_table[i]);
  interrupts_install_isr(i, &default_handler);
 }
 interrupts_install_isr(36, serial_handler);
 interrupts_install_isr(39, spurious_handler);
 apic_init();
}

canonical_original_code:
void interrupts_init()
{
 memclr(&init_pcb, sizeof(PCB));
 init_pcb.page_table = kernel_table;
 current_pcb = &init_pcb;
 idt = ((void *)((unsigned long)(&start_idt_64[0]) + 0xFFFF800000000000));
 setup_tss_descriptor();
 extern void (*isr_stub_table[256])();
 for (unsigned long i = 0; i < 256; ++i)
 {
  set_idt_entry(i, isr_stub_table[i]);
  interrupts_install_isr(i, &default_handler);
 }
 interrupts_install_isr(36, serial_handler);
 interrupts_install_isr(39, spurious_handler);
 apic_init();
}

variable_types:
i
unsigned long


decompiled_code:
void __fastcall check_scan_code(char a1)
{
char v1; // al
unsigned int v2; // edx
unsigned int v3; // edx

if ( a1 == 54 )
goto LABEL_5;
if ( (unsigned __int8)a1 <= 0x36u )
{
if ( a1 == 29 )
return;
if ( a1 == 42 )
{
LABEL_5:
shiftdown_1[0] = 1;
return;
}
goto LABEL_10;
}
if ( a1 == -86 || a1 == -74 )
{
shiftdown_1[0] = 0;
return;
}
if ( a1 >= 0 )
{
LABEL_10:
v1 = scan_code_table[128 * (unsigned __int64)shiftdown_1[0] + (unsigned __int8)a1];
if ( v1 )
{
v2 = next_index[0];
buffer[next_index[0]] = v1;
v3 = v2 + 1;
++buffer_size;
if ( v3 > 0xFF )
{
next_index[0] = 0;
if ( !last_index )
last_index = 1;
}
else
{
next_index[0] = v3;
}
if ( v1 == 49 )
{
page_up();
}
else if ( v1 == 50 )
{
page_down();
}
}
}
}

canonical_decompiled_code:
void __fastcall func0(char a1)
{
char v1; // al
unsigned int v2; // edx
unsigned int v3; // edx

if ( a1 == 54 )
goto LABEL_5;
if ( (unsigned __int8)a1 <= 0x36u )
{
if ( a1 == 29 )
return;
if ( a1 == 42 )
{
LABEL_5:
shiftdown_1[0] = 1;
return;
}
goto LABEL_10;
}
if ( a1 == -86 || a1 == -74 )
{
shiftdown_1[0] = 0;
return;
}
if ( a1 >= 0 )
{
LABEL_10:
v1 = scan_code_table[128 * (unsigned __int64)shiftdown_1[0] + (unsigned __int8)a1];
if ( v1 )
{
v2 = next_index[0];
buffer[next_index[0]] = v1;
v3 = v2 + 1;
++buffer_size;
if ( v3 > 0xFF )
{
next_index[0] = 0;
if ( !last_index )
last_index = 1;
}
else
{
next_index[0] = v3;
}
if ( v1 == 49 )
{
page_up();
}
else if ( v1 == 50 )
{
page_down();
}
}
}
}

original_code:
static
void check_scan_code(uint8_t code)
{
 static uint8_t shiftdown = 0;
 static uint8_t ctrl_mask = 0xFF;
 switch (code)
 {
  case 0x2A:
  case 0x36:
   shiftdown = 1;
   break;
  case 0xAA:
  case 0xB6:
   shiftdown = 0;
   break;
  case 0x1D:
   ctrl_mask = 0x1F;
   break;
  case 0x9D:
   ctrl_mask = 0xFF;
  default:
  {
   if ((code & 0x80) == 0)
   {
    code = scan_code_table[shiftdown][code];
    if (code != 0)
    {
     buffer[next_index] = code;
     ++buffer_size;
     ++next_index;
     if (next_index >= 256)
     {
      next_index = 0;
      if (next_index == last_index)
      {
       last_index = (last_index + 1) % 256;
      }
     }
     if (code == '1')
     {
      page_up();
     }
     else if (code == '2')
     {
      page_down();
     }
    }
   }
  }
 }
}

canonical_original_code:
static void check_scan_code(unsigned char code)
{
 static unsigned char shiftdown = 0;
 static unsigned char ctrl_mask = 0xFF;
 switch (code)
 {
  case 0x2A:
  case 0x36:
   shiftdown = 1;
   break;
  case 0xAA:
  case 0xB6:
   shiftdown = 0;
   break;
  case 0x1D:
   ctrl_mask = 0x1F;
   break;
  case 0x9D:
   ctrl_mask = 0xFF;
  default:
  {
   if ((code & 0x80) == 0)
   {
    code = scan_code_table[shiftdown][code];
    if (code != 0)
    {
     buffer[next_index] = code;
     ++buffer_size;
     ++next_index;
     if (next_index >= 256)
     {
      next_index = 0;
      if (next_index == last_index)
      {
       last_index = (last_index + 1) % 256;
      }
     }
     if (code == '1')
     {
      page_up();
     }
     else if (code == '2')
     {
      page_down();
     }
    }
   }
  }
 }
}

variable_types:
code
unsigned char

variable_types:
shiftdown
unsigned char


decompiled_code:
__int64 __fastcall keyboard_handler(unsigned __int64 a1)
{
char v1; // al
__int64 result; // rax

v1 = __inbyte(0x60u);
check_scan_code(v1);
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

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

v1 = __inbyte(0x60u);
func0(v1);
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

original_code:
static
void keyboard_handler(uint64_t vector, uint64_t code)
{
 ((void)vector);
 ((void)code);
 uint8_t scan_code = _inb(0x60);
 check_scan_code(scan_code);
 pic_acknowledge(vector);
}

canonical_original_code:
static void keyboard_handler(unsigned long vector, unsigned long code)
{
 ((void)vector);
 ((void)code);
 unsigned char scan_code = _inb(0x60);
 check_scan_code(scan_code);
 pic_acknowledge(vector);
}


decompiled_code:
bool keyboard_char_available()
{
return buffer_size != 0;
}

canonical_decompiled_code:
bool func2()
{
return buffer_size != 0;
}

original_code:
uint8_t keyboard_char_available()
{
 return buffer_size > 0;
}

canonical_original_code:
unsigned char keyboard_char_available()
{
 return buffer_size > 0;
}

return_type: unsigned char


decompiled_code:
char keyboard_get_char()
{
char result; // al

result = keyboard_char_available();
if ( result )
{
result = buffer[last_index];
--buffer_size;
if ( last_index + 1 <= 0xFF )
++last_index;
else
last_index = 0;
}
return result;
}

canonical_decompiled_code:
char func3()
{
char result; // al

result = func2();
if ( result )
{
result = buffer[last_index];
--buffer_size;
if ( last_index + 1 <= 0xFF )
++last_index;
else
last_index = 0;
}
return result;
}

original_code:
uint8_t keyboard_get_char()
{
 if (!keyboard_char_available())
 {
  return '\0';
 }
 uint8_t val = buffer[last_index];
 --buffer_size;
 ++last_index;
 if (last_index >= 256)
 {
  last_index = 0;
 }
 return val;
}

canonical_original_code:
unsigned char keyboard_get_char()
{
 if (!keyboard_char_available())
 {
  return '\0';
 }
 unsigned char val = buffer[last_index];
 --buffer_size;
 ++last_index;
 if (last_index >= 256)
 {
  last_index = 0;
 }
 return val;
}

variable_types:
val
unsigned char


decompiled_code:
__int64 keyboard_init()
{
return interrupts_install_isr(33LL, keyboard_handler);
}

canonical_decompiled_code:
__int64 func4()
{
return interrupts_install_isr(33LL, keyboard_handler);
}

original_code:
void keyboard_init()
{
 interrupts_install_isr(33, keyboard_handler);
}


decompiled_code:
__int64 random32()
{
next = 1103515245 * next + 12345;
return HIWORD(next) & 0x7FFF;
}

canonical_decompiled_code:
__int64 func0()
{
next = 1103515245 * next + 12345;
return HIWORD(next) & 0x7FFF;
}

canonical_original_code:
static unsigned int random32()
{
 next = next * 1103515245 + 12345;
 return (unsigned int)(next / 65536) % 32768;
}

return_type: unsigned int


rand

decompiled_code:
__int64 rand()
{
return (unsigned int)random32();
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)func0();
}

original_code:
uint64_t rand()
{
 return random32();
}

canonical_original_code:
unsigned long rand()
{
 return random32();
}

return_type: unsigned long


decompiled_code:
unsigned __int64 __fastcall water_mark_alloc_align(unsigned __int64 *a1, __int64 a2, __int64 a3)
{
unsigned __int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rsi
unsigned __int64 result; // rax

if ( !a2 || (a2 & (a2 - 1)) != 0 )
panic("ASSERT FAILED: src/kernel/data_structures/watermark.c:22");
v4 = a1[1];
v5 = a3 - v4 + ((v4 + a2 - 1) & -a2);
v6 = v5 + a1[2];
if ( v6 > a1[3] )
{
panic("Watermark: Size exceeded!");
v4 = a1[1];
v6 = v5 + a1[2];
}
v7 = *a1;
result = v4 - v5;
a1[2] = v6;
for ( a1[1] = result; v7 > result; *a1 = v7 )
{
if ( !(unsigned __int8)virt_map_page(kernel_table, v7 - 0x200000, 2LL, 2LL, 0LL) )
panic("WaterMark: Failed to allocate page!");
v7 = *a1 - 0x200000;
result = a1[1];
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned __int64 *a1, __int64 a2, __int64 a3)
{
unsigned __int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rsi
unsigned __int64 result; // rax

if ( !a2 || (a2 & (a2 - 1)) != 0 )
panic("ASSERT FAILED: src/kernel/data_structures/watermark.c:22");
v4 = a1[1];
v5 = a3 - v4 + ((v4 + a2 - 1) & -a2);
v6 = v5 + a1[2];
if ( v6 > a1[3] )
{
panic("Watermark: Size exceeded!");
v4 = a1[1];
v6 = v5 + a1[2];
}
v7 = *a1;
result = v4 - v5;
a1[2] = v6;
for ( a1[1] = result; v7 > result; *a1 = v7 )
{
if ( !(unsigned __int8)virt_map_page(kernel_table, v7 - 0x200000, 2LL, 2LL, 0LL) )
panic("WaterMark: Failed to allocate page!");
v7 = *a1 - 0x200000;
result = a1[1];
}
return result;
}

original_code:
void* water_mark_alloc_align(WaterMarkAllocator* wma, const uint64_t alignment, const uint64_t size)
{
 if (!((alignment != 0) && ((alignment & (alignment - 1)) == 0))) panic("ASSERT FAILED: " "src/kernel/data_structures/watermark.c" ":" "22");
 const uint64_t new_address = ((wma->current_address+alignment-1) & ~(alignment - 1));
 const uint64_t adjusted_size = (new_address - wma->current_address) + size;
 if (wma->current_size + adjusted_size > wma->max_size)
 {
  panic("Watermark: Size exceeded!");
 }
 wma->current_address -= adjusted_size;
 wma->current_size += adjusted_size;
 while (wma->current_address < wma->page_address)
 {
  const uint64_t location = (wma->page_address - 0x200000);
  if (!virt_map_page(kernel_table, location, 0x2, 0x2, ((void*)0)))
  {
   panic("WaterMark: Failed to allocate page!");
  }
  wma->page_address -= 0x200000;
 }
 void* returnVal = (void*)(wma->current_address);
 return returnVal;
}

canonical_original_code:
void *water_mark_alloc_align(struct WaterMarkAllocator *wma, unsigned long alignment, unsigned long size)
{
 if (!((alignment != 0) && ((alignment & (alignment - 1)) == 0))) panic("ASSERT FAILED: " "src/kernel/data_structures/watermark.c" ":" "22");
 unsigned long new_address = ((wma->current_address+alignment-1) & ~(alignment - 1));
 unsigned long adjusted_size = (new_address - wma->current_address) + size;
 if (wma->current_size + adjusted_size > wma->max_size)
 {
  panic("Watermark: Size exceeded!");
 }
 wma->current_address -= adjusted_size;
 wma->current_size += adjusted_size;
 while (wma->current_address < wma->page_address)
 {
  unsigned long location = (wma->page_address - 0x200000);
  if (!virt_map_page(kernel_table, location, 0x2, 0x2, ((void *)0)))
  {
   panic("WaterMark: Failed to allocate page!");
  }
  wma->page_address -= 0x200000;
 }
 void *returnVal = (void*)(wma->current_address);
 return returnVal;
}

variable_types: struct WaterMarkAllocator { unsigned long page_address; unsigned long current_address; unsigned long current_size; unsigned long max_size; } *

variable_types:
alignment
unsigned long

variable_types:
size
unsigned long

variable_types:
location
unsigned long

variable_types:
returnVal
void *

return_type: void *


decompiled_code:
unsigned __int64 __fastcall water_mark_alloc(unsigned __int64 *a1, __int64 a2)
{
return water_mark_alloc_align(a1, 1LL, a2);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(unsigned __int64 *a1, __int64 a2)
{
return func1(a1, 1LL, a2);
}

original_code:
void* water_mark_alloc(WaterMarkAllocator* wma, const uint64_t size)
{
 return water_mark_alloc_align(wma, 1, size);
}

canonical_original_code:
void *water_mark_alloc(struct WaterMarkAllocator *wma, unsigned long size)
{
 return water_mark_alloc_align(wma, 1, size);
}

user_defined_types: struct WaterMarkAllocator { unsigned long page_address; unsigned long current_address; unsigned long current_size; unsigned long max_size; }


fork

decompiled_code:
void fork()
{
__asm { int 80h; LINUX - }
}

canonical_decompiled_code:
void func0()
{
__asm { int 80h; LINUX - }
}

original_code:
void fork(PCB* pcb)
{
 ;
 PCB* new_pcb = alloc_pcb();
 if (new_pcb == ((void*)0))
 {
  pcb->context->rax = FAILURE;
  return;
 }
 memcpy(new_pcb, pcb, sizeof(PCB));
 ;
 ;
 void* new_page_table = virt_clone_mapping(pcb->page_table);
 new_pcb->page_table = new_page_table;
 ;
 uint64_t new_context_addr = 0;
 if (!virt_lookup_phys(new_page_table, (uint64_t)pcb->context, &new_context_addr))
 {
  panic("Fork: failed to lookup context location, very bad!");
 }
 ;
 Context* new_context = (Context*)((void*)((uint64_t)(new_context_addr) + 0xFFFF800000000000));
 ;
 new_pcb->context = pcb->context;
 pcb->context->rax = SUCCESS;
 *((Pid*)pcb->context->rdi) = 0;
 new_context->rax = SUCCESS;
 ;
 ;
 uint64_t pid_param_location = 0;
 if (!virt_lookup_phys(new_page_table, (uint64_t)new_context->rdi, &pid_param_location))
 {
  panic("Fork: failed to lookup parameter location!");
 }
 ;
 ;
 Pid* param_pid = (Pid*)((void*)((uint64_t)(pid_param_location) + 0xFFFF800000000000));
 *param_pid = 1;
 ;
 ;
 schedule(new_pcb);
 ;
 dispatch();
}

canonical_original_code:
void fork(struct PCB *pcb)
{
 ;
 struct PCB *new_pcb = alloc_pcb();
 if (new_pcb == ((void *)0))
 {
  pcb->context->rax = FAILURE;
  return;
 }
 memcpy(new_pcb, pcb, sizeof(PCB));
 ;
 ;
 void *new_page_table = virt_clone_mapping(pcb->page_table);
 new_pcb->page_table = new_page_table;
 ;
 unsigned long new_context_addr = 0;
 if (!virt_lookup_phys(new_page_table, (unsigned long)pcb->context, &new_context_addr))
 {
  panic("Fork: failed to lookup context location, very bad!");
 }
 ;
 struct Context *new_context = (Context*)((void*)((uint64_t)(new_context_addr) + 0xFFFF800000000000));
 ;
 new_pcb->context = pcb->context;
 pcb->context->rax = SUCCESS;
 *((unsigned int *)pcb->context->rdi) = 0;
 new_context->rax = SUCCESS;
 ;
 ;
 unsigned long pid_param_location = 0;
 if (!virt_lookup_phys(new_page_table, (unsigned long)new_context->rdi, &pid_param_location))
 {
  panic("Fork: failed to lookup parameter location!");
 }
 ;
 ;
 unsigned int *param_pid = (Pid*)((void*)((uint64_t)(pid_param_location) + 0xFFFF800000000000));
 *param_pid = 1;
 ;
 ;
 schedule(new_pcb);
 ;
 dispatch();
}

user_defined_types: struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; }

user_defined_types: enum State { READY=0, RUNNING=1, SLEEPING=2, KILLED=3 }

user_defined_types: enum Priority { HIGH=0, NORMAL=1, LOW=2, IDLE=3 }


decompiled_code:
void get_key()
{
__asm { int 80h; LINUX - }
}

canonical_decompiled_code:
void func5()
{
__asm { int 80h; LINUX - }
}

original_code:
void get_key(PCB* pcb)
{
 pcb->context->rax = keyboard_get_char();
}

canonical_original_code:
void get_key(struct PCB *pcb)
{
 pcb->context->rax = keyboard_get_char();
}


decompiled_code:
void read_key()
{
void (*v0)(); // rdx
void (__fastcall *v1)(_QWORD); // rcx
char v2; // al

v0 = key_available;
while ( 1 )
{
v0();
if ( v2 )
break;
v1(0LL);
}
get_key();
}

canonical_decompiled_code:
void func6()
{
void (*v0)(); // rdx
void (__fastcall *v1)(_QWORD); // rcx
char v2; // al

v0 = key_available;
while ( 1 )
{
v0();
if ( v2 )
break;
v1(0LL);
}
func5();
}

original_code:
uint8_t read_key(void)
{
 while (!key_available())
 {
  msleep(0);
 }
 return get_key();
}

canonical_original_code:
unsigned char read_key()
{
 while (!key_available())
 {
  msleep(0);
 }
 return get_key();
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // r14
int v4; // er15
const char **v5; // r13
FILE *v6; // rax
FILE *v7; // rbp
__int64 v8; // rbx
__int64 v9; // rax
_DWORD *v10; // rax
size_t v11; // rsi
int v12; // er13
const char *v14; // rdx
const char *v15; // rdx
const char *v16; // rdx
size_t size; // [rsp+18h] [rbp-250h]
void *sizea; // [rsp+18h] [rbp-250h]
unsigned int v20; // [rsp+28h] [rbp-240h]
__int64 ptr[71]; // [rsp+30h] [rbp-238h] BYREF

if ( argc <= 2 )
{
v12 = 1;
fprintf(stderr, "Usage: %s location file {[location] file ...}\n", *argv);
}
else
{
v20 = 0;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v3 = fopen("image.dat", "wb");
if ( v3 )
{
v4 = 2;
printf("%s:\n", *argv);
while ( 1 )
{
v5 = &argv[v4];
v6 = fopen(*v5, "rb");
v7 = v6;
if ( !v6 )
{
v14 = *v5;
v12 = 4;
fprintf(stderr, "Error reading in file %s\n", v14);
fclose(0LL);
fclose(v3);
return v12;
}
if ( fseek(v6, 0LL, 2) )
{
v15 = *v5;
v12 = 5;
fprintf(stderr, "Failed to seek to the end of the file %s\n", v15);
fclose(v7);
fclose(v3);
return v12;
}
v8 = ftell(v7);
if ( v8 == -1 )
{
v12 = 6;
perror("Couldn't figure out file size\n");
fclose(v7);
fclose(v3);
return v12;
}
if ( fseek(v7, 0LL, 0) )
{
v12 = 7;
fwrite("Failed to seek to beginning\n", 1uLL, 0x1CuLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
LODWORD(ptr[0]) = v20;
HIDWORD(ptr[0]) = (int)(v8 + 520 - (((_WORD)v8 + 8) & 0x1FF)) / 512;
if ( fwrite(ptr, 8uLL, 1uLL, v3) != 1 )
{
v12 = 8;
fwrite("Failed to write to destination file\n", 1uLL, 0x24uLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
if ( (unsigned int)dump_file(v7, v3) )
{
v16 = *v5;
v12 = 9;
fprintf(stderr, "Failed to copy file %s\n", v16);
fclose(v7);
fclose(v3);
return v12;
}
v9 = ((_WORD)v8 + 8) & 0x1FF;
if ( (((_WORD)v8 + 8) & 0x1FF) != 0 )
{
size = 512 - v9;
v10 = malloc(512 - v9);
v11 = size;
if ( (unsigned int)size >= 8 )
{
*(_QWORD *)((char *)v10 + (unsigned int)size - 8) = 0x2222222222222222LL;
memset(v10, 0x22u, 8LL * ((unsigned int)(size - 1) >> 3));
}
else if ( (size & 4) != 0 )
{
*v10 = 572662306;
*(_DWORD *)((char *)v10 + (unsigned int)size - 4) = 572662306;
}
else if ( (_DWORD)size )
{
*(_BYTE *)v10 = 34;
if ( (size & 2) != 0 )
*(_WORD *)((char *)v10 + (unsigned int)size - 2) = 8738;
}
sizea = v10;
if ( fwrite(v10, v11, 1uLL, v3) != 1 )
{
free(sizea);
v12 = 10;
fwrite("Failed to pad output file\n", 1uLL, 0x1AuLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
free(sizea);
}
printf(
"File: %s, load location: 0x%X, sector count: %d, file size: %ld bytes\n",
*v5,
v20,
(unsigned int)((int)(v8 + 520 - (((_WORD)v8 + 8) & 0x1FF)) / 512),
v8);
if ( v4 + 1 >= argc )
break;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v4 += 2;
v20 = 0;
fclose(v7);
if ( argc <= v4 )
{
v12 = 0;
goto LABEL_26;
}
}
else
{
v20 += v8;
++v4;
fclose(v7);
}
}
v12 = 0;
fclose(v7);
LABEL_26:
memset(ptr, 0, 0x200uLL);
ptr[0] = -1LL;
if ( fwrite(ptr, 0x200uLL, 1uLL, v3) != 1 )
{
v12 = 11;
fwrite("Failed to put end marker\n", 1uLL, 0x19uLL, stderr);
}
fclose(v3);
}
else
{
v12 = 3;
fwrite("Failed to create output file\n", 1uLL, 0x1DuLL, stderr);
}
}
else
{
v12 = 2;
fwrite("Invalid initial location\n", 1uLL, 0x19uLL, stderr);
}
}
return v12;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // r14
int v4; // er15
const char **v5; // r13
FILE *v6; // rax
FILE *v7; // rbp
__int64 v8; // rbx
__int64 v9; // rax
_DWORD *v10; // rax
size_t v11; // rsi
int v12; // er13
const char *v14; // rdx
const char *v15; // rdx
const char *v16; // rdx
size_t size; // [rsp+18h] [rbp-250h]
void *sizea; // [rsp+18h] [rbp-250h]
unsigned int v20; // [rsp+28h] [rbp-240h]
__int64 ptr[71]; // [rsp+30h] [rbp-238h] BYREF

if ( argc <= 2 )
{
v12 = 1;
fprintf(stderr, "Usage: %s location file {[location] file ...}\n", *argv);
}
else
{
v20 = 0;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v3 = fopen("image.dat", "wb");
if ( v3 )
{
v4 = 2;
printf("%s:\n", *argv);
while ( 1 )
{
v5 = &argv[v4];
v6 = fopen(*v5, "rb");
v7 = v6;
if ( !v6 )
{
v14 = *v5;
v12 = 4;
fprintf(stderr, "Error reading in file %s\n", v14);
fclose(0LL);
fclose(v3);
return v12;
}
if ( fseek(v6, 0LL, 2) )
{
v15 = *v5;
v12 = 5;
fprintf(stderr, "Failed to seek to the end of the file %s\n", v15);
fclose(v7);
fclose(v3);
return v12;
}
v8 = ftell(v7);
if ( v8 == -1 )
{
v12 = 6;
perror("Couldn't figure out file size\n");
fclose(v7);
fclose(v3);
return v12;
}
if ( fseek(v7, 0LL, 0) )
{
v12 = 7;
fwrite("Failed to seek to beginning\n", 1uLL, 0x1CuLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
LODWORD(ptr[0]) = v20;
HIDWORD(ptr[0]) = (int)(v8 + 520 - (((_WORD)v8 + 8) & 0x1FF)) / 512;
if ( fwrite(ptr, 8uLL, 1uLL, v3) != 1 )
{
v12 = 8;
fwrite("Failed to write to destination file\n", 1uLL, 0x24uLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
if ( (unsigned int)func7(v7, v3) )
{
v16 = *v5;
v12 = 9;
fprintf(stderr, "Failed to copy file %s\n", v16);
fclose(v7);
fclose(v3);
return v12;
}
v9 = ((_WORD)v8 + 8) & 0x1FF;
if ( (((_WORD)v8 + 8) & 0x1FF) != 0 )
{
size = 512 - v9;
v10 = malloc(512 - v9);
v11 = size;
if ( (unsigned int)size >= 8 )
{
*(_QWORD *)((char *)v10 + (unsigned int)size - 8) = 0x2222222222222222LL;
memset(v10, 0x22u, 8LL * ((unsigned int)(size - 1) >> 3));
}
else if ( (size & 4) != 0 )
{
*v10 = 572662306;
*(_DWORD *)((char *)v10 + (unsigned int)size - 4) = 572662306;
}
else if ( (_DWORD)size )
{
*(_BYTE *)v10 = 34;
if ( (size & 2) != 0 )
*(_WORD *)((char *)v10 + (unsigned int)size - 2) = 8738;
}
sizea = v10;
if ( fwrite(v10, v11, 1uLL, v3) != 1 )
{
free(sizea);
v12 = 10;
fwrite("Failed to pad output file\n", 1uLL, 0x1AuLL, stderr);
fclose(v7);
fclose(v3);
return v12;
}
free(sizea);
}
printf(
"File: %s, load location: 0x%X, sector count: %d, file size: %ld bytes\n",
*v5,
v20,
(unsigned int)((int)(v8 + 520 - (((_WORD)v8 + 8) & 0x1FF)) / 512),
v8);
if ( v4 + 1 >= argc )
break;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v4 += 2;
v20 = 0;
fclose(v7);
if ( argc <= v4 )
{
v12 = 0;
goto LABEL_26;
}
}
else
{
v20 += v8;
++v4;
fclose(v7);
}
}
v12 = 0;
fclose(v7);
LABEL_26:
memset(ptr, 0, 0x200uLL);
ptr[0] = -1LL;
if ( fwrite(ptr, 0x200uLL, 1uLL, v3) != 1 )
{
v12 = 11;
fwrite("Failed to put end marker\n", 1uLL, 0x19uLL, stderr);
}
fclose(v3);
}
else
{
v12 = 3;
fwrite("Failed to create output file\n", 1uLL, 0x1DuLL, stderr);
}
}
else
{
v12 = 2;
fwrite("Invalid initial location\n", 1uLL, 0x19uLL, stderr);
}
}
return v12;
}

original_code:
void main(void)
{
 set_priority(3);
 Pid pid;
 if (fork(&pid) == SUCCESS)
 {
  if (pid == 0)
  {
   while (1);
  }
  else
  {
   set_priority(0);
   shell_loop();
  }
 }
 else
 {
  text_mode_info.color.fg = 0xC;
  text_mode_info.color.bg = 0x0;
  text_mode_string("FAILED TO FORK!");
  while (1);
 }
}

canonical_original_code:
void main()
{
 set_priority(3);
 unsigned int pid;
 if (fork(&pid) == SUCCESS)
 {
  if (pid == 0)
  {
   while (1);
  }
  else
  {
   set_priority(0);
   shell_loop();
  }
 }
 else
 {
  text_mode_info.color.fg = 0xC;
  text_mode_info.color.bg = 0x0;
  text_mode_string("FAILED TO FORK!");
  while (1);
 }
}

variable_types:
pid
unsigned int


original_code:
ELF_Error elf_create_process(PCB* pcb, void* elf_file, void* page_table)
{
 const uint64_t elf_address = (uint64_t)elf_file;
 const ELF64_Ehdr* elf_hdr = (const ELF64_Ehdr*)elf_file;
 if (elf_hdr->e_ident[0] != 0x7F ||
  elf_hdr->e_ident[1] != 'E' ||
  elf_hdr->e_ident[2] != 'L' ||
  elf_hdr->e_ident[3] != 'F')
 {
  return 1;
 }
 if (elf_hdr->e_ident[4] != 2)
 {
  return 2;
 }
 if (elf_hdr->e_ident[5] != 1)
 {
  return 3;
 }
 if (elf_hdr->e_ident[7] != 0)
 {
  return 4;
 }
 if (elf_hdr->e_type != 2)
 {
  return 5;
 }
 if (elf_hdr->e_machine != 62)
 {
  return 6;
 }
 if (elf_hdr->e_version != 1)
 {
  return 7;
 }
 if (elf_hdr->e_entry == 0)
 {
  panic("ELF: Bad entry point");
 }
 virt_reset_table(page_table);
 const ELF64_Phdr* phdr_table = (const ELF64_Phdr*)(elf_address + elf_hdr->e_phoff);
 const uint64_t phdr_table_size = elf_hdr->e_phnum;
 for (uint64_t i = 0; i < phdr_table_size; ++i)
 {
  const ELF64_Phdr* cur_hdr = &phdr_table[i];
  if (cur_hdr->p_type != 1)
  {
   continue;
  }
  if ((cur_hdr->p_vaddr + cur_hdr->p_memsz) >= 0xFFFF800000000000)
  {
   return 8;
  }
  uint64_t page_size = 0x1000;
  uint64_t page_size_type = 0x1;
  if (cur_hdr->p_memsz >= 0x200000)
  {
   page_size = 0x200000;
   page_size_type = 0x2;
  }
  const uint64_t num_pages = cur_hdr->p_memsz / page_size + 1;
  uint64_t load_offset = cur_hdr->p_offset;
  uint64_t amount_left = cur_hdr->p_filesz;
  uint64_t vaddr = cur_hdr->p_vaddr;
  for (uint64_t i = 0; i < num_pages; ++i)
  {
   uint64_t memory_address;
   if (!virt_map_page(page_table, vaddr,
      0x2 | 0x4, page_size_type,
      &memory_address))
   {
    panic("ELF: Failed to map page");
   }
   const uint64_t copy_amount = page_size < amount_left ? page_size : amount_left;
   if (amount_left > 0)
   {
    memcpy(((void*)((uint64_t)(memory_address) + 0xFFFF800000000000)),
      (void*)(elf_address + load_offset), copy_amount);
    memclr(((void*)((uint64_t)(memory_address+copy_amount) + 0xFFFF800000000000)), page_size-copy_amount);
    amount_left -= copy_amount;
   }
   else
   {
    memclr(((void*)((uint64_t)(memory_address) + 0xFFFF800000000000)), page_size);
   }
   load_offset += copy_amount;
   vaddr += copy_amount;
  }
 }
 uint64_t address = 0x2000000 -0x4000;
 uint64_t memory_address;
 for (uint64_t i = 0; i < 4; ++i)
 {
  if (!virt_map_page(page_table, address,
     0x2 | 0x4, 0x1,
     &memory_address))
  {
   panic("ELF: Failed to allocate stack");
  }
  memclr(((void*)((uint64_t)(memory_address) + 0xFFFF800000000000)), 0x1000);
  address += 0x1000;
 }
 virt_map_page(page_table, (0x2000000 +0x1000), 0x2, 0x1, &memory_address);
 pcb->context = (Context*)((0x2000000 +0x1000) + 0x1000 - sizeof(Context));
 ;
 ;
 Context* context = (Context*)((void*)((uint64_t)(memory_address+0x1000 -sizeof(Context)) + 0xFFFF800000000000));
 ;
 context->rip = elf_hdr->e_entry;
 context->rsp = 0x2000000;
 context->rbp = 0x2000000;
 context->rflags = 0x202;
 context->cs = 0x30 | 3;
 context->ss = 0x40 | 3;
 return 0;
}

canonical_original_code:
unsigned char elf_create_process(struct PCB *pcb, void *elf_file, void *page_table)
{
 unsigned long elf_address = (uint64_t)elf_file;
 struct ELF64_Ehdr *elf_hdr = (const ELF64_Ehdr*)elf_file;
 if (elf_hdr->e_ident[0] != 0x7F ||
  elf_hdr->e_ident[1] != 'E' ||
  elf_hdr->e_ident[2] != 'L' ||
  elf_hdr->e_ident[3] != 'F')
 {
  return 1;
 }
 if (elf_hdr->e_ident[4] != 2)
 {
  return 2;
 }
 if (elf_hdr->e_ident[5] != 1)
 {
  return 3;
 }
 if (elf_hdr->e_ident[7] != 0)
 {
  return 4;
 }
 if (elf_hdr->e_type != 2)
 {
  return 5;
 }
 if (elf_hdr->e_machine != 62)
 {
  return 6;
 }
 if (elf_hdr->e_version != 1)
 {
  return 7;
 }
 if (elf_hdr->e_entry == 0)
 {
  panic("ELF: Bad entry point");
 }
 virt_reset_table(page_table);
 struct ELF64_Phdr *phdr_table = (const ELF64_Phdr*)(elf_address + elf_hdr->e_phoff);
 unsigned long phdr_table_size = elf_hdr->e_phnum;
 for (unsigned long i = 0; i < phdr_table_size; ++i)
 {
  struct ELF64_Phdr *cur_hdr = &phdr_table[i];
  if (cur_hdr->p_type != 1)
  {
   continue;
  }
  if ((cur_hdr->p_vaddr + cur_hdr->p_memsz) >= 0xFFFF800000000000)
  {
   return 8;
  }
  unsigned long page_size = 0x1000;
  unsigned long page_size_type = 0x1;
  if (cur_hdr->p_memsz >= 0x200000)
  {
   page_size = 0x200000;
   page_size_type = 0x2;
  }
  unsigned long num_pages = cur_hdr->p_memsz / page_size + 1;
  unsigned long load_offset = cur_hdr->p_offset;
  unsigned long amount_left = cur_hdr->p_filesz;
  unsigned long vaddr = cur_hdr->p_vaddr;
  for (unsigned long i = 0; i < num_pages; ++i)
  {
   unsigned long memory_address;
   if (!virt_map_page(page_table, vaddr,
      0x2 | 0x4, page_size_type,
      &memory_address))
   {
    panic("ELF: Failed to map page");
   }
   unsigned long copy_amount = page_size < amount_left ? page_size : amount_left;
   if (amount_left > 0)
   {
    memcpy(((void *)((unsigned long)(memory_address) + 0xFFFF800000000000)),
      (void *)(elf_address + load_offset), copy_amount);
    memclr(((void *)((unsigned long)(memory_address+copy_amount) + 0xFFFF800000000000)), page_size-copy_amount);
    amount_left -= copy_amount;
   }
   else
   {
    memclr(((void *)((unsigned long)(memory_address) + 0xFFFF800000000000)), page_size);
   }
   load_offset += copy_amount;
   vaddr += copy_amount;
  }
 }
 unsigned long address = 0x2000000 -0x4000;
 unsigned long memory_address;
 for (unsigned long i = 0; i < 4; ++i)
 {
  if (!virt_map_page(page_table, address,
     0x2 | 0x4, 0x1,
     &memory_address))
  {
   panic("ELF: Failed to allocate stack");
  }
  memclr(((void *)((unsigned long)(memory_address) + 0xFFFF800000000000)), 0x1000);
  address += 0x1000;
 }
 virt_map_page(page_table, (0x2000000 +0x1000), 0x2, 0x1, &memory_address);
 pcb->context = (struct Context *)((0x2000000 +0x1000) + 0x1000 - sizeof(Context));
 ;
 ;
 struct Context *context = (Context*)((void*)((uint64_t)(memory_address+0x1000 -sizeof(Context)) + 0xFFFF800000000000));
 ;
 context->rip = elf_hdr->e_entry;
 context->rsp = 0x2000000;
 context->rbp = 0x2000000;
 context->rflags = 0x202;
 context->cs = 0x30 | 3;
 context->ss = 0x40 | 3;
 return 0;
}

variable_types: struct ELF64_Ehdr { unsigned char e_ident[16]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_hsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *

variable_types: struct ELF64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; } *

variable_types:
vaddr
unsigned long

variable_types:
address
unsigned long


original_code:
void create_paging_structures(const page_struct_info* psi)
{
 uint64_t pds_left = psi->num_pds;
 uint64_t pdpts_left = psi->num_pdpts;
 uint64_t alloc_ptr = psi->base;
 uint64_t phys_address = (1024ULL*(1024ULL*1024ULL));
 PDP_Table* pdp_table = &kernel_PDPTE;
 for (uint32_t pdpt_index = 1; pdpt_index < 512ULL; ++pdpt_index)
 {
  if (pds_left == 0)
  {
   break;
  }
  PD_Table* pd_table = (PD_Table*) alloc_ptr;
  alloc_ptr += sizeof(PD_Table);
  --pds_left;
  memclr(pd_table, sizeof(PD_Table));
  pdp_table->entries[pdpt_index] = (uint64_t)pd_table | 0x2 | 0x1;
  __asm__ volatile("invlpg %0" :: "m" (pd_table));
  for (uint32_t j = 0; j < 512ULL; ++j)
  {
   pd_table->entries[j] = phys_address | 0x80 | 0x2 | 0x1;
   __asm__ volatile("invlpg %0" :: "m" (phys_address));
   phys_address += (2ULL*(1024ULL*1024ULL));
  }
  __asm__ volatile("invlpg %0" :: "m" (pd_table));
 }
 PML4_Table* pml4_table = &kernel_PML4;
 for (uint64_t pml4_index = 1; pml4_index < 512ULL && pdpts_left > 0; ++pml4_index)
 {
  ;
  if (pdpts_left == 0 || pds_left == 0)
  {
   break;
  }
  pdp_table = (PDP_Table*) alloc_ptr;
  alloc_ptr += sizeof(PDP_Table);
  --pdpts_left;
  memclr(pdp_table, sizeof(PDP_Table));
  pml4_table->entries[pml4_index] = (uint64_t)pdp_table | 0x2 | 0x1;
  __asm__ volatile("invlpg %0" :: "m" (pdp_table));
  for (uint32_t i = 0; i < 512ULL; ++i)
  {
   if (pds_left == 0)
   {
    break;
   }
   PD_Table* pd_table = (PD_Table*) alloc_ptr;
   alloc_ptr += sizeof(PD_Table);
   --pds_left;
   memclr(pd_table, sizeof(PD_Table));
   pdp_table->entries[i] = (uint64_t)pd_table | 0x2 | 0x1;
   __asm__ volatile("invlpg %0" :: "m" (pd_table));
   for (uint32_t j = 0; j < 512ULL; ++j)
   {
    pd_table->entries[j] = phys_address | 0x80 | 0x2 | 0x1;
    __asm__ volatile("invlpg %0" :: "m" (phys_address));
    phys_address += (2ULL*(1024ULL*1024ULL));
   }
  }
 }
}

canonical_original_code:
void create_paging_structures(struct page_struct_info *psi)
{
 unsigned long pds_left = psi->num_pds;
 unsigned long pdpts_left = psi->num_pdpts;
 unsigned long alloc_ptr = psi->base;
 unsigned long phys_address = (1024ULL*(1024ULL*1024ULL));
 struct PDP_Table *pdp_table = &kernel_PDPTE;
 for (unsigned int pdpt_index = 1; pdpt_index < 512ULL; ++pdpt_index)
 {
  if (pds_left == 0)
  {
   break;
  }
  struct PD_Table *pd_table = (PD_Table*) alloc_ptr;
  alloc_ptr += sizeof(PD_Table);
  --pds_left;
  memclr(pd_table, sizeof(PD_Table));
  pdp_table->entries[pdpt_index] = (unsigned long)pd_table | 0x2 | 0x1;
  __asm__ volatile("invlpg %0" :: "m" (pd_table));
  for (unsigned int j = 0; j < 512ULL; ++j)
  {
   pd_table->entries[j] = phys_address | 0x80 | 0x2 | 0x1;
   __asm__ volatile("invlpg %0" :: "m" (phys_address));
   phys_address += (2ULL*(1024ULL*1024ULL));
  }
  __asm__ volatile("invlpg %0" :: "m" (pd_table));
 }
 struct PML4_Table *pml4_table = &kernel_PML4;
 for (unsigned long pml4_index = 1; pml4_index < 512ULL && pdpts_left > 0; ++pml4_index)
 {
  ;
  if (pdpts_left == 0 || pds_left == 0)
  {
   break;
  }
  pdp_table = (struct PDP_Table *) alloc_ptr;
  alloc_ptr += sizeof(PDP_Table);
  --pdpts_left;
  memclr(pdp_table, sizeof(PDP_Table));
  pml4_table->entries[pml4_index] = (unsigned long)pdp_table | 0x2 | 0x1;
  __asm__ volatile("invlpg %0" :: "m" (pdp_table));
  for (unsigned int i = 0; i < 512ULL; ++i)
  {
   if (pds_left == 0)
   {
    break;
   }
   struct PD_Table *pd_table = (PD_Table*) alloc_ptr;
   alloc_ptr += sizeof(PD_Table);
   --pds_left;
   memclr(pd_table, sizeof(PD_Table));
   pdp_table->entries[i] = (unsigned long)pd_table | 0x2 | 0x1;
   __asm__ volatile("invlpg %0" :: "m" (pd_table));
   for (unsigned int j = 0; j < 512ULL; ++j)
   {
    pd_table->entries[j] = phys_address | 0x80 | 0x2 | 0x1;
    __asm__ volatile("invlpg %0" :: "m" (phys_address));
    phys_address += (2ULL*(1024ULL*1024ULL));
   }
  }
 }
}

variable_types: struct page_struct_info { unsigned long base; unsigned long length; unsigned long num_pds; unsigned long num_pdpts; unsigned long space_needed; } *

variable_types: struct PDP_Table { unsigned long entries[512]; } *

variable_types: struct PD_Table { unsigned long entries[512]; } *

variable_types:
j
unsigned int

variable_types: struct PML4_Table { unsigned long entries[512]; } *

variable_types:
i
unsigned int


decompiled_code:
_DWORD *Player_new()
{
_DWORD *v0; // r8
_DWORD *result; // rax

v0 = calloc(0x78uLL, 1uLL);
if ( count_0 )
{
if ( count_0 == 1 )
{
v0[16] = 1065353216;
count_0 = 0;
}
else
{
++count_0;
}
result = v0;
}
else
{
v0[16] = 0;
*((_QWORD *)v0 + 7) = 0x3F8000003F800000LL;
count_0 = 1;
result = v0;
}
return result;
}

original_code:
Player* Player_new( )
{
    static int count = 0;
    Player *player = (Player*)calloc( sizeof(Player), 1 );
    switch( count )
    {
    case 0:
        player->color[0] = 1.0f;
        player->color[1] = 1.0f;
        player->color[2] = 0.0f;
        break;
    case 1:
        player->color[0] = 0.0f;
        player->color[1] = 0.0f;
        player->color[2] = 1.0f;
        count = -1;
        break;
    }
    count++;
    return player;
}

canonical_original_code:
struct Player *Player_new()
{
    static int count = 0;
    struct Player *player = (Player*)calloc( sizeof(Player), 1 );
    switch( count )
    {
    case 0:
        player->color[0] = 1.0f;
        player->color[1] = 1.0f;
        player->color[2] = 0.0f;
        break;
    case 1:
        player->color[0] = 0.0f;
        player->color[1] = 0.0f;
        player->color[2] = 1.0f;
        count = -1;
        break;
    }
    count++;
    return player;
}

variable_types: count int

variable_types: struct Player { float headPosition[3]; struct _Body { float *position; int gridPosition[2]; struct _Body *previous; struct _Body *next; } *head; float tailPosition[3]; float headOffset; float tailOffset; int underGround; float color[3]; int score; enum Direction { DIRECTION_NORTH=0, DIRECTION_SOUTH=1, DIRECTION_EAST=2, DIRECTION_WEST=3 } currentDir; float forward[3]; float up[3]; float maxLength; float length; float radius; } *

user_defined_types: struct Player { float headPosition[3]; struct _Body *head; float tailPosition[3]; float headOffset; float tailOffset; int underGround; float color[3]; int score; enum Direction currentDir; float forward[3]; float up[3]; float maxLength; float length; float radius; }


decompiled_code:
void __fastcall Player_delete(void *ptr)
{
void *i; // rdi

for ( i = (void *)*((_QWORD *)ptr + 2); i; *((_QWORD *)ptr + 2) = i )
{
free(i);
i = *(void **)(*((_QWORD *)ptr + 2) + 24LL);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *i; // rdi

for ( i = (void *)*((_QWORD *)ptr + 2); i; *((_QWORD *)ptr + 2) = i )
{
free(i);
i = *(void **)(*((_QWORD *)ptr + 2) + 24LL);
}
free(ptr);
}

original_code:
void Player_delete( Player* player )
{
    while( player->head != ((void *)0) )
    {
        free(player->head);
        player->head = player->head->next;
    }
    free(player);
}

canonical_original_code:
void Player_delete(struct Player *player)
{
    while( player->head != ((void *)0) )
    {
        free(player->head);
        player->head = player->head->next;
    }
    free(player);
}

user_defined_types: enum Direction { DIRECTION_NORTH=0, DIRECTION_SOUTH=1, DIRECTION_EAST=2, DIRECTION_WEST=3 }

user_defined_types: struct _Body { float *position; int gridPosition[2]; struct _Body *previous; struct _Body *next; }


decompiled_code:
void __fastcall Body_delete(__int64 a1)
{
__int64 v1; // rax

if ( *(_QWORD *)(a1 + 24) )
Body_delete();
v1 = *(_QWORD *)(a1 + 16);
if ( v1 )
*(_QWORD *)(v1 + 24) = 0LL;
free(*(void **)a1);
free((void *)a1);
}

original_code:
void Body_delete( Body* body )
{
    if( body->next != ((void *)0) )
    {
        Body_delete(body->next);
    }
    if( body->previous != ((void *)0) )
    {
        body->previous->next = ((void *)0);
    }
    free(body->position);
    free(body);
}

canonical_original_code:
void Body_delete(struct _Body *body)
{
    if( body->next != ((void *)0) )
    {
        Body_delete(body->next);
    }
    if( body->previous != ((void *)0) )
    {
        body->previous->next = ((void *)0);
    }
    free(body->position);
    free(body);
}

variable_types: struct _Body { float *position; int gridPosition[2]; struct _Body *previous; struct _Body *next; } *


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

result = *a2;
*(_QWORD *)(a1 + 24) = *a2;
*(_QWORD *)(result + 16) = a1;
*a2 = a1;
return result;
}

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

result = *a2;
*(_QWORD *)(a1 + 24) = *a2;
*(_QWORD *)(result + 16) = a1;
*a2 = a1;
return result;
}

original_code:
void Body_push( Body* new_segment, Body** body )
{
    new_segment->next = *body;
    (*body)->previous = new_segment;
    *body = new_segment;
}

canonical_original_code:
void Body_push(struct _Body *new_segment, struct _Body **body)
{
    new_segment->next = *body;
    (*body)->previous = new_segment;
    *body = new_segment;
}

variable_types:
body
struct _Body **


decompiled_code:
float *__fastcall Body_setPosition(__int64 a1, int a2, int a3, float a4, float a5, float a6)
{
float *result; // rax

result = *(float **)a1;
*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = a3;
*result = a4;
result[1] = a5;
result[2] = a6;
return result;
}

canonical_decompiled_code:
float *__fastcall func4(__int64 a1, int a2, int a3, float a4, float a5, float a6)
{
float *result; // rax

result = *(float **)a1;
*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = a3;
*result = a4;
result[1] = a5;
result[2] = a6;
return result;
}

original_code:
void Body_setPosition( Body* body, int row, int column,
                       float x, float y, float z )
{
    body->gridPosition[0] = row;
    body->gridPosition[1] = column;
    body->position[0] = x;
    body->position[1] = y;
    body->position[2] = z;
}

canonical_original_code:
void Body_setPosition(struct _Body *body, int row, int column, float x, float y, float z)
{
    body->gridPosition[0] = row;
    body->gridPosition[1] = column;
    body->position[0] = x;
    body->position[1] = y;
    body->position[2] = z;
}

variable_types: row int

variable_types: column int

variable_types: x float

variable_types: y float

variable_types: z float


decompiled_code:
void __fastcall distance_between_points(float *a1, float *a2)
{
float v2; // xmm1_4
float v3; // xmm0_4

v2 = a1[1] - a2[1];
v3 = (float)((float)((float)(*a1 - *a2) * (float)(*a1 - *a2)) + (float)(v2 * v2))
+ (float)((float)(a1[2] - a2[2]) * (float)(a1[2] - a2[2]));
if ( v3 < 0.0 )
sqrtf(v3);
}

original_code:
float distance_between_points( Point point1, Point point2 )
{
    float x, y, z;
    x = point1[0] - point2[0];
    y = point1[1] - point2[1];
    z = point1[2] - point2[2];
    x = pow(x, 2);
    y = pow(y, 2);
    z = pow(z, 2);
    return sqrt( x + y + z );
}

canonical_original_code:
float distance_between_points(float point1[3], float point2[3])
{
    float x; float y; float z;
    x = point1[0] - point2[0];
    y = point1[1] - point2[1];
    z = point1[2] - point2[2];
    x = pow(x, 2);
    y = pow(y, 2);
    z = pow(z, 2);
    return sqrt( x + y + z );
}

return_type: float


decompiled_code:
float __fastcall bounded_random(float a1, float a2)
{
return (float)((float)((float)rand() * 4.6566129e-10) * (float)(a2 - a1)) + a1;
}

canonical_decompiled_code:
float __fastcall func1(float a1, float a2)
{
return (float)((float)((float)rand() * 4.6566129e-10) * (float)(a2 - a1)) + a1;
}

original_code:
float bounded_random( float min, float max )
{
    return min + (max - min) * ((float)rand() / (float)2147483647);
}

canonical_original_code:
float bounded_random(float min, float max)
{
    return min + (max - min) * ((float)rand() / (float)2147483647);
}

variable_types: min float

variable_types: max float


normaliseVector

original_code:
void normaliseVector(float normalvector[3])
{
    float norm = sqrt( pow(normalvector[0],2)
                        + pow(normalvector[1],2)
                        + pow(normalvector[2],2)
                        );
    normalvector[0] = normalvector[0] / norm;
    normalvector[1] = normalvector[1] / norm;
    normalvector[2] = normalvector[2] / norm;
}

variable_types:
normalvector
float [3]

variable_types: norm float


addVector

original_code:
void addVector(float* v1,float* v2,float* result)
{
 result[0] = v1[0]+v2[0];
 result[0] = v1[1]+v2[1];
 result[0] = v1[2]+v2[2];
}

canonical_original_code:
void addVector(float *v1, float *v2, float *result)
{
 result[0] = v1[0]+v2[0];
 result[0] = v1[1]+v2[1];
 result[0] = v1[2]+v2[2];
}

variable_types:
result
float *


multiplyScalar

decompiled_code:
void __fastcall multiplyScalar(float *a1, float *a2, float a3)
{
*a2 = *a1 * a3;
a2[1] = a1[1] * a3;
a2[2] = a3 * a1[2];
}

original_code:
void multiplyScalar(float scalar,float* vector, float* result)
{
 result[0] = vector[0]*scalar;
 result[1] = vector[1]*scalar;
 result[2] = vector[2]*scalar;
}

canonical_original_code:
void multiplyScalar(float scalar, float *vector, float *result)
{
 result[0] = vector[0]*scalar;
 result[1] = vector[1]*scalar;
 result[2] = vector[2]*scalar;
}

variable_types: scalar float

variable_types:
vector
float *


original_code:
float dist_line_to_line( float* L1P0,float* L1P1,float* L2P0, float* L2P1)
{
 float u[3] = {L1P1[0] - L1P0[0],
     L1P1[1] - L1P0[1],
     L1P1[2] - L1P0[2] };
    float v[3] = {L2P1[0] - L2P0[0],
     L2P1[1] - L2P0[1],
     L2P1[2] - L2P0[2] };
    float w[3] = {L1P0[0] - L2P0[0],
     L1P0[1] - L2P0[1],
     L1P0[2] - L2P0[2] };
 float dP[3],
  temp1[3],
  temp2[3],
  temp3[3];
    float a = dot(u,u);
    float b = dot(u,v);
    float c = dot(v,v);
    float d = dot(u,w);
    float e = dot(v,w);
    float D = a*c - b*b;
    float sc, tc;
    if (D < 0.000001f) {
        sc = 0.0;
        tc = (b>c ? d/b : e/c);
    }
    else {
        sc = (b*e - c*d) / D;
        tc = (a*e - b*d) / D;
    }
 multiplyScalar(sc,u,temp1);
 multiplyScalar(-tc,v,temp2);
 addVector(temp1,temp2,temp3);
 addVector(temp3,w,dP);
    return sqrt( pow(dP[0],2)
                        + pow(dP[1],2)
                        + pow(dP[2],2)
                        );
}

canonical_original_code:
float dist_line_to_line(float *L1P0, float *L1P1, float *L2P0, float *L2P1)
{
 float u[3] = {L1P1[0] - L1P0[0],
     L1P1[1] - L1P0[1],
     L1P1[2] - L1P0[2] };
    float v[3] = {L2P1[0] - L2P0[0],
     L2P1[1] - L2P0[1],
     L2P1[2] - L2P0[2] };
    float w[3] = {L1P0[0] - L2P0[0],
     L1P0[1] - L2P0[1],
     L1P0[2] - L2P0[2] };
 float dP[3]; float temp1[3]; float temp2[3]; float temp3[3];
    float a = dot(u,u);
    float b = dot(u,v);
    float c = dot(v,v);
    float d = dot(u,w);
    float e = dot(v,w);
    float D = a*c - b*b;
    float sc; float tc;
    if (D < 0.000001f) {
        sc = 0.0;
        tc = (b>c ? d/b : e/c);
    }
    else {
        sc = (b*e - c*d) / D;
        tc = (a*e - b*d) / D;
    }
 multiplyScalar(sc,u,temp1);
 multiplyScalar(-tc,v,temp2);
 addVector(temp1,temp2,temp3);
 addVector(temp3,w,dP);
    return sqrt( pow(dP[0],2)
                        + pow(dP[1],2)
                        + pow(dP[2],2)
                        );
}

variable_types:
u
float [3]

variable_types:
v
float [3]

variable_types:
w
float [3]

variable_types:
dP
float [3]

variable_types: a float

variable_types: b float

variable_types: c float

variable_types: d float

variable_types: e float

variable_types: D float

variable_types: sc float

variable_types: tc float


decompiled_code:
float __fastcall dist_point_to_line(float *a1, float *a2, float *a3, float a4)
{
int v5; // eax
float v6; // xmm2_4
double v7; // xmm0_8
double v8; // xmm0_8
float v10; // [rsp+8h] [rbp-20h]
float v11; // [rsp+Ch] [rbp-1Ch]

distance_between_points(a1, a2);
v11 = (float)(int)abs32((int)a4);
distance_between_points(a2, a3);
distance_between_points(a1, a3);
v5 = abs32((int)a4);
v6 = (float)v5;
v7 = (float)((float)((float)((float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - v11)
* (float)((float)((float)(v11 + v11) + (float)v5) * 0.5))
* (float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - v11))
* (float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - (float)v5));
if ( v7 < 0.0 )
{
v10 = (float)v5;
v8 = sqrt(v7);
v6 = v10;
}
else
{
v8 = sqrt(v7);
}
return (v8 + v8) / v6;
}

canonical_decompiled_code:
float __fastcall func7(float *a1, float *a2, float *a3, float a4)
{
int v5; // eax
float v6; // xmm2_4
double v7; // xmm0_8
double v8; // xmm0_8
float v10; // [rsp+8h] [rbp-20h]
float v11; // [rsp+Ch] [rbp-1Ch]

func0(a1, a2);
v11 = (float)(int)abs32((int)a4);
func0(a2, a3);
func0(a1, a3);
v5 = abs32((int)a4);
v6 = (float)v5;
v7 = (float)((float)((float)((float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - v11)
* (float)((float)((float)(v11 + v11) + (float)v5) * 0.5))
* (float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - v11))
* (float)((float)((float)((float)(v11 + v11) + (float)v5) * 0.5) - (float)v5));
if ( v7 < 0.0 )
{
v10 = (float)v5;
v8 = sqrt(v7);
v6 = v10;
}
else
{
v8 = sqrt(v7);
}
return (v8 + v8) / v6;
}

original_code:
float dist_point_to_line( float* point, float* line1, float* line2 )
{
    float s, a, b, c, result;
    a = abs( distance_between_points( point, line1) );
    b = abs( distance_between_points( line1, line2) );
    c = abs( distance_between_points( point, line2) );
    s = (a + b + c) / 2.0;
    result = (2 * sqrt(s * (s-a) * (s-b) * (s-c))) / c;
    return result;
}

canonical_original_code:
float dist_point_to_line(float *point, float *line1, float *line2)
{
    float s; float a; float b; float c; float result;
    a = abs( distance_between_points( point, line1) );
    b = abs( distance_between_points( line1, line2) );
    c = abs( distance_between_points( point, line2) );
    s = (a + b + c) / 2.0;
    result = (2 * sqrt(s * (s-a) * (s-b) * (s-c))) / c;
    return result;
}

variable_types:
point
float *

variable_types: s float

variable_types: result float


decompiled_code:
void __fastcall Viewport_delete(void *ptr)
{
Camera_delete(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
Camera_delete(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void Viewport_delete( Viewport* viewport )
{
    Camera_delete( viewport->camera );
    free(viewport);
}

canonical_original_code:
void Viewport_delete(struct Viewport *viewport)
{
    Camera_delete( viewport->camera );
    free(viewport);
}

variable_types: struct Viewport { struct Camera { float position[3]; float forward[3]; float up[3]; } *camera; int width; int x; int ortho; float left; } *

user_defined_types: struct Viewport { struct Camera *camera; int width; int x; int ortho; float left; }


original_code:
void Viewport_apply( Viewport* viewport )
{
 glMatrixMode(0x1701);
    glLoadIdentity();
    glViewport( viewport->x, viewport->y, viewport->width, viewport->height );
    if( viewport->ortho )
    {
        glOrtho( viewport->left, viewport->right,
                 viewport->bottom, viewport->top, 0.01f, 100.0f );
    }
    else
    {
        gluPerspective( 130.0f,
                        viewport->width / ((float) viewport->height),
                        0.01f,
                        100.0f );
    }
    gluLookAt( viewport->camera->position[0],
               viewport->camera->position[1],
               viewport->camera->position[2],
               viewport->camera->forward[0],
               viewport->camera->forward[1],
               viewport->camera->forward[2],
               viewport->camera->up[0],
               viewport->camera->up[1],
               viewport->camera->up[2] );
    glMatrixMode(0x1700);
    glLoadIdentity();
}

canonical_original_code:
void Viewport_apply(struct Viewport *viewport)
{
 glMatrixMode(0x1701);
    glLoadIdentity();
    glViewport( viewport->x, viewport->y, viewport->width, viewport->height );
    if( viewport->ortho )
    {
        glOrtho( viewport->left, viewport->right,
                 viewport->bottom, viewport->top, 0.01f, 100.0f );
    }
    else
    {
        gluPerspective( 130.0f,
                        viewport->width / ((float) viewport->height),
                        0.01f,
                        100.0f );
    }
    gluLookAt( viewport->camera->position[0],
               viewport->camera->position[1],
               viewport->camera->position[2],
               viewport->camera->forward[0],
               viewport->camera->forward[1],
               viewport->camera->forward[2],
               viewport->camera->up[0],
               viewport->camera->up[1],
               viewport->camera->up[2] );
    glMatrixMode(0x1700);
    glLoadIdentity();
}

user_defined_types: struct Camera { float position[3]; float forward[3]; float up[3]; }


decompiled_code:
__int64 register_callbacks()
{
glutDisplayFunc(display_event);
glutReshapeFunc(reshape_event);
glutVisibilityFunc(visibility_change_event);
glutKeyboardFunc(key_event);
glutMouseFunc(mouse_button_event);
glutMotionFunc(mouse_active_motion_event);
glutPassiveMotionFunc(mouse_passive_motion_event);
glutEntryFunc(mouse_entry_event);
return glutSpecialFunc(special_key_event);
}

canonical_decompiled_code:
__int64 func0()
{
glutDisplayFunc(display_event);
glutReshapeFunc(reshape_event);
glutVisibilityFunc(visibility_change_event);
glutKeyboardFunc(key_event);
glutMouseFunc(mouse_button_event);
glutMotionFunc(mouse_active_motion_event);
glutPassiveMotionFunc(mouse_passive_motion_event);
glutEntryFunc(mouse_entry_event);
return glutSpecialFunc(special_key_event);
}

original_code:
void register_callbacks()
{
    glutDisplayFunc( display_event );
    glutReshapeFunc( reshape_event );
    glutVisibilityFunc( visibility_change_event );
    glutKeyboardFunc( key_event );
    glutMouseFunc( mouse_button_event );
    glutMotionFunc( mouse_active_motion_event );
    glutPassiveMotionFunc( mouse_passive_motion_event );
    glutEntryFunc( mouse_entry_event );
    glutSpecialFunc( special_key_event );
}


decompiled_code:
void *init_registry()
{
void *result; // rax

if ( windows )
free(windows);
result = calloc(8uLL, 1uLL);
window_count = 0;
windows = result;
return result;
}

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

if ( windows )
free(windows);
result = calloc(8uLL, 1uLL);
window_count = 0;
windows = result;
return result;
}

original_code:
void init_registry()
{
    if( windows != ((void *)0) )
    {
        free(windows);
        window_count = 0;
    }
    windows = (Window**)calloc( sizeof(Window*), 1 );
    window_count = 0;
}

canonical_original_code:
void init_registry()
{
    if( windows != ((void *)0) )
    {
        free(windows);
        window_count = 0;
    }
    windows = (struct Window **)calloc( sizeof(struct Window *), 1 );
    window_count = 0;
}

user_defined_types: struct Window { int glutID; char *title; void (*displayHandler)(); void (*reshapeHandler)(int width, int height); void (*visibilityChangeHandler)(int state); void (*keyHandler)(unsigned char key, int x, int y); void (*specialKeyHandler)(int key, int x, int y); void (*mouseButtonHandler)(int button, int state, int x, int y); void (*mouseActiveMotionHandler)(int x, int y); void (*mousePassiveMotionHandler)(int x, int y); void (*mouseEntryHandler)(int state); }


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

result = (unsigned int)initialized_0;
if ( initialized_0 )
{
initialized_0 = 1;
}
else
{
init_registry();
result = register_callbacks();
}
return result;
}

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

result = (unsigned int)initialized_0;
if ( initialized_0 )
{
initialized_0 = 1;
}
else
{
func1();
result = func0();
}
return result;
}

original_code:
void Window_init()
{
 static int initialized = 0;
 if( initialized != 0 )
 {
  initialized = 1;
  return;
 }
    init_registry();
    register_callbacks();
}

variable_types: initialized int


decompiled_code:
__int64 __fastcall register_window(__int64 a1)
{
__int64 v1; // rcx
__int64 result; // rax

v1 = window_count;
*((_QWORD *)windows + window_count) = a1;
result = v1;
window_count = v1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rcx
__int64 result; // rax

v1 = window_count;
*((_QWORD *)windows + window_count) = a1;
result = v1;
window_count = v1 + 1;
return result;
}

original_code:
int register_window( Window* window )
{
    windows[window_count] = window;
    return window_count++;
}

canonical_original_code:
int register_window(struct Window *window)
{
    windows[window_count] = window;
    return window_count++;
}

variable_types: struct Window { int glutID; char *title; void (*displayHandler)(); void (*reshapeHandler)(int width, int height); void (*visibilityChangeHandler)(int state); void (*keyHandler)(unsigned char key, int x, int y); void (*specialKeyHandler)(int key, int x, int y); void (*mouseButtonHandler)(int button, int state, int x, int y); void (*mouseActiveMotionHandler)(int x, int y); void (*mousePassiveMotionHandler)(int x, int y); void (*mouseEntryHandler)(int state); } *

return_type: int


decompiled_code:
__int64 __fastcall Window_new(__int64 a1)
{
_QWORD *v1; // rbp

v1 = calloc(1uLL, 0x58uLL);
*(_DWORD *)v1 = glutCreateWindow(a1);
Window_init();
v1[1] = a1;
register_callbacks();
return register_window((__int64)v1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
_QWORD *v1; // rbp

v1 = calloc(1uLL, 0x58uLL);
*(_DWORD *)v1 = glutCreateWindow(a1);
func2();
v1[1] = a1;
func0();
return func3((__int64)v1);
}

original_code:
int Window_new( char* title )
{
    Window *window = (Window*)calloc( 1, sizeof(Window) );
    window->glutID = glutCreateWindow(title);
 Window_init();
    window->title = title;
    register_callbacks();
    return register_window(window);
}

canonical_original_code:
int Window_new(char *title)
{
    struct Window *window = (Window*)calloc( 1, sizeof(Window) );
    window->glutID = glutCreateWindow(title);
 Window_init();
    window->title = title;
    register_callbacks();
    return register_window(window);
}

variable_types:
title
char *


decompiled_code:
__int64 __fastcall get_window(int a1)
{
return *((_QWORD *)windows + a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
return *((_QWORD *)windows + a1);
}

original_code:
Window* get_window( int window_id )
{
    return windows[window_id];
}

canonical_original_code:
struct Window *get_window(int window_id)
{
    return windows[window_id];
}

return_type: struct Window { int glutID; char *title; void (*displayHandler)(); void (*reshapeHandler)(int width, int height); void (*visibilityChangeHandler)(int state); void (*keyHandler)(unsigned char key, int x, int y); void (*specialKeyHandler)(int key, int x, int y); void (*mouseButtonHandler)(int button, int state, int x, int y); void (*mouseActiveMotionHandler)(int x, int y); void (*mousePassiveMotionHandler)(int x, int y); void (*mouseEntryHandler)(int state); } *


decompiled_code:
void *__fastcall Window_delete(int a1)
{
unsigned int *v1; // r12

v1 = (unsigned int *)get_window(a1);
glutDestroyWindow(*v1);
free(v1);
return deregister_window(a1);
}

canonical_decompiled_code:
void *__fastcall func7(int a1)
{
unsigned int *v1; // r12

v1 = (unsigned int *)func6(a1);
glutDestroyWindow(*v1);
free(v1);
return func5(a1);
}

original_code:
void Window_delete( int window_id )
{
    Window* window = get_window(window_id);
    glutDestroyWindow(window->glutID);
    free(window);
    deregister_window(window_id);
}

canonical_original_code:
void Window_delete(int window_id)
{
    struct Window *window = get_window(window_id);
    glutDestroyWindow(window->glutID);
    free(window);
    deregister_window(window_id);
}


decompiled_code:
__int64 __fastcall Window_setTitle(int a1, __int64 a2)
{
unsigned int *v2; // rbx
unsigned int v3; // er12
__int64 result; // rax

v2 = (unsigned int *)get_window(a1);
v3 = glutGetWindow();
glutSetWindow(*v2);
glutSetWindowTitle(a2);
result = glutSetWindow(v3);
*((_QWORD *)v2 + 1) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, __int64 a2)
{
unsigned int *v2; // rbx
unsigned int v3; // er12
__int64 result; // rax

v2 = (unsigned int *)func6(a1);
v3 = glutGetWindow();
glutSetWindow(*v2);
glutSetWindowTitle(a2);
result = glutSetWindow(v3);
*((_QWORD *)v2 + 1) = a2;
return result;
}

original_code:
void Window_setTitle( int window_id, char* title )
{
    Window* window = get_window(window_id);
    int cur = glutGetWindow();
    glutSetWindow( window->glutID );
    glutSetWindowTitle( title );
    glutSetWindow(cur);
    window->title = title;
}

canonical_original_code:
void Window_setTitle(int window_id, char *title)
{
    struct Window *window = get_window(window_id);
    int cur = glutGetWindow();
    glutSetWindow( window->glutID );
    glutSetWindowTitle( title );
    glutSetWindow(cur);
    window->title = title;
}

variable_types: cur int


decompiled_code:
__int64 __fastcall Window_getTitle(int a1)
{
return *(_QWORD *)(get_window(a1) + 8);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
return *(_QWORD *)(func6(a1) + 8);
}

original_code:
char* Window_getTitle( int window_id )
{
    Window* window = get_window(window_id);
    return window->title;
}

canonical_original_code:
char *Window_getTitle(int window_id)
{
    struct Window *window = get_window(window_id);
    return window->title;
}

return_type: char *


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

result = get_window(a1);
*(_QWORD *)(result + 16) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 16) = a2;
return result;
}

original_code:
void Window_registerDisplayEventHandler( int window_id, void (*displayHandler)(void) )
{
    Window *window = get_window(window_id);
    window->displayHandler = displayHandler;
}

canonical_original_code:
void Window_registerDisplayEventHandler(int window_id, void (*displayHandler)())
{
    struct Window *window = get_window(window_id);
    window->displayHandler = displayHandler;
}

variable_types:
displayHandler
void (*)()


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

result = get_window(a1);
*(_QWORD *)(result + 24) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 24) = a2;
return result;
}

original_code:
void Window_registerReshapeEventHandler( int window_id, void (*reshapeHandler)(int width, int height) )
{
    Window *window = get_window(window_id);
    window->reshapeHandler = reshapeHandler;
}

canonical_original_code:
void Window_registerReshapeEventHandler(int window_id, void (*reshapeHandler)(int width, int height))
{
    struct Window *window = get_window(window_id);
    window->reshapeHandler = reshapeHandler;
}

variable_types:
reshapeHandler
void (*)(int width, int height)


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

result = get_window(a1);
*(_QWORD *)(result + 32) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 32) = a2;
return result;
}

original_code:
void Window_registerVisibilityChangeEventHandler( int window_id, void (*visibilityChangeHandler)(int state) )
{
    Window *window = get_window(window_id);
    window->visibilityChangeHandler = visibilityChangeHandler;
}

canonical_original_code:
void Window_registerVisibilityChangeEventHandler(int window_id, void (*visibilityChangeHandler)(int state))
{
    struct Window *window = get_window(window_id);
    window->visibilityChangeHandler = visibilityChangeHandler;
}

variable_types:
visibilityChangeHandler
void (*)(int state)


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

result = get_window(a1);
*(_QWORD *)(result + 40) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 40) = a2;
return result;
}

original_code:
void Window_registerKeyEventHandler( int window_id, void (*keyHandler)(unsigned char key, int x, int y) )
{
    Window *window = get_window(window_id);
    window->keyHandler = keyHandler;
}

canonical_original_code:
void Window_registerKeyEventHandler(int window_id, void (*keyHandler)(unsigned char key, int x, int y))
{
    struct Window *window = get_window(window_id);
    window->keyHandler = keyHandler;
}

variable_types: void (*)(unsigned char key, int x, int y)


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

result = get_window(a1);
*(_QWORD *)(result + 48) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 48) = a2;
return result;
}

original_code:
void Window_registerSpecialKeyEventHandler( int window_id, void (*specialKeyHandler)(int key, int x, int y) )
{
    Window *window = get_window(window_id);
    window->specialKeyHandler = specialKeyHandler;
}

canonical_original_code:
void Window_registerSpecialKeyEventHandler(int window_id, void (*specialKeyHandler)(int key, int x, int y))
{
    struct Window *window = get_window(window_id);
    window->specialKeyHandler = specialKeyHandler;
}

variable_types:
specialKeyHandler
void (*)(int key, int x, int y)


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

result = get_window(a1);
*(_QWORD *)(result + 56) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 56) = a2;
return result;
}

original_code:
void Window_registerMouseButtonEventHandler( int window_id, void (*mouseButtonHandler)(int button, int state, int x, int y) )
{
    Window *window = get_window(window_id);
    window->mouseButtonHandler = mouseButtonHandler;
}

canonical_original_code:
void Window_registerMouseButtonEventHandler(int window_id, void (*mouseButtonHandler)(int button, int state, int x, int y))
{
    struct Window *window = get_window(window_id);
    window->mouseButtonHandler = mouseButtonHandler;
}

variable_types: void (*)(int button, int state, int x, int y)


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

result = get_window(a1);
*(_QWORD *)(result + 64) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 64) = a2;
return result;
}

original_code:
void Window_registerMouseActiveMotionEventHandler( int window_id, void (*mouseActiveMotionHandler)(int x, int y) )
{
    Window *window = get_window(window_id);
    window->mouseActiveMotionHandler = mouseActiveMotionHandler;
}

canonical_original_code:
void Window_registerMouseActiveMotionEventHandler(int window_id, void (*mouseActiveMotionHandler)(int x, int y))
{
    struct Window *window = get_window(window_id);
    window->mouseActiveMotionHandler = mouseActiveMotionHandler;
}

variable_types:
mouseActiveMotionHandler
void (*)(int x, int y)


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

result = get_window(a1);
*(_QWORD *)(result + 72) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 72) = a2;
return result;
}

original_code:
void Window_registerMousePassiveMotionEventHandler( int window_id, void (*mousePassiveMotionHandler)(int x, int y) )
{
    Window *window = get_window(window_id);
    window->mousePassiveMotionHandler = mousePassiveMotionHandler;
}

canonical_original_code:
void Window_registerMousePassiveMotionEventHandler(int window_id, void (*mousePassiveMotionHandler)(int x, int y))
{
    struct Window *window = get_window(window_id);
    window->mousePassiveMotionHandler = mousePassiveMotionHandler;
}

variable_types:
mousePassiveMotionHandler
void (*)(int x, int y)


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

result = get_window(a1);
*(_QWORD *)(result + 80) = a2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(result + 80) = a2;
return result;
}

original_code:
void Window_registerMouseEntryEventHandler( int window_id, void (*mouseEntryHandler)(int state) )
{
    Window *window = get_window(window_id);
    window->mouseEntryHandler = mouseEntryHandler;
}

canonical_original_code:
void Window_registerMouseEntryEventHandler(int window_id, void (*mouseEntryHandler)(int state))
{
    struct Window *window = get_window(window_id);
    window->mouseEntryHandler = mouseEntryHandler;
}

variable_types:
mouseEntryHandler
void (*)(int state)


decompiled_code:
__int64 __fastcall get_window_glutID(int a1)
{
return *(unsigned int *)get_window(a1);
}

canonical_decompiled_code:
__int64 __fastcall func19(int a1)
{
return *(unsigned int *)func6(a1);
}

original_code:
int get_window_glutID( int window_id )
{
    Window* window = get_window(window_id);
    return window->glutID;
}

canonical_original_code:
int get_window_glutID(int window_id)
{
    struct Window *window = get_window(window_id);
    return window->glutID;
}


decompiled_code:
__int64 __fastcall Window_setIconTitle(int a1, __int64 a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax

v2 = glutGetWindow();
v3 = get_window_glutID(a1);
glutSetWindow(v3);
glutSetIconTitle(a2);
return glutSetWindow(v2);
}

canonical_decompiled_code:
__int64 __fastcall func20(int a1, __int64 a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax

v2 = glutGetWindow();
v3 = func19(a1);
glutSetWindow(v3);
glutSetIconTitle(a2);
return glutSetWindow(v2);
}

original_code:
void Window_setIconTitle( int window_id, char* title )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSetIconTitle(title);
    glutSetWindow(cur);
}

canonical_original_code:
void Window_setIconTitle(int window_id, char *title)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSetIconTitle(title);
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_makeFullScreen(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutFullScreen();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func21(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutFullScreen();
return glutSetWindow(v1);
}

original_code:
void Window_makeFullScreen( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutFullScreen();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_makeFullScreen(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutFullScreen();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_resize(int a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // eax

v4 = glutGetWindow();
v5 = get_window_glutID(a1);
glutSetWindow(v5);
glutReshapeWindow(a2, a3);
return glutSetWindow(v4);
}

canonical_decompiled_code:
__int64 __fastcall func22(int a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // eax

v4 = glutGetWindow();
v5 = func19(a1);
glutSetWindow(v5);
glutReshapeWindow(a2, a3);
return glutSetWindow(v4);
}

original_code:
void Window_resize( int window_id, int width, int height )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutReshapeWindow( width, height );
    glutSetWindow(cur);
}

canonical_original_code:
void Window_resize(int window_id, int width, int height)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutReshapeWindow( width, height );
    glutSetWindow(cur);
}

variable_types: width int

variable_types: height int


decompiled_code:
__int64 __fastcall Window_move(int a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // eax

v4 = glutGetWindow();
v5 = get_window_glutID(a1);
glutSetWindow(v5);
glutPositionWindow(a2, a3);
return glutSetWindow(v4);
}

canonical_decompiled_code:
__int64 __fastcall func23(int a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // eax

v4 = glutGetWindow();
v5 = func19(a1);
glutSetWindow(v5);
glutPositionWindow(a2, a3);
return glutSetWindow(v4);
}

original_code:
void Window_move( int window_id, int x, int y )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutPositionWindow( x, y );
    glutSetWindow(cur);
}

canonical_original_code:
void Window_move(int window_id, int x, int y)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutPositionWindow( x, y );
    glutSetWindow(cur);
}

variable_types: x int

variable_types: y int


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

v1 = get_window_glutID(a1);
return glutSetWindow(v1);
}

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

v1 = func19(a1);
return glutSetWindow(v1);
}

original_code:
void Window_makeCurrent( int window_id )
{
    glutSetWindow( get_window_glutID(window_id) );
}

canonical_original_code:
void Window_makeCurrent(int window_id)
{
    glutSetWindow( get_window_glutID(window_id) );
}


decompiled_code:
__int64 __fastcall Window_makeDirty(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutPostRedisplay();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func25(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutPostRedisplay();
return glutSetWindow(v1);
}

original_code:
void Window_makeDirty( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutPostRedisplay();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_makeDirty(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutPostRedisplay();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_swapBuffers(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutSwapBuffers();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func26(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutSwapBuffers();
return glutSetWindow(v1);
}

original_code:
void Window_swapBuffers( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSwapBuffers();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_swapBuffers(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSwapBuffers();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_show(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutShowWindow();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func27(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutShowWindow();
return glutSetWindow(v1);
}

original_code:
void Window_show( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutShowWindow();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_show(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutShowWindow();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_hide(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutHideWindow();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func28(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutHideWindow();
return glutSetWindow(v1);
}

original_code:
void Window_hide( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutHideWindow();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_hide(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutHideWindow();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_iconify(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)get_window_glutID(a1);
glutSetWindow(v2);
glutIconifyWindow();
return glutSetWindow(v1);
}

canonical_decompiled_code:
__int64 __fastcall func29(int a1)
{
unsigned int v1; // ebp
__int64 v2; // rdi

v1 = glutGetWindow();
v2 = (unsigned int)func19(a1);
glutSetWindow(v2);
glutIconifyWindow();
return glutSetWindow(v1);
}

original_code:
void Window_iconify( int window_id )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutIconifyWindow();
    glutSetWindow(cur);
}

canonical_original_code:
void Window_iconify(int window_id)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutIconifyWindow();
    glutSetWindow(cur);
}


decompiled_code:
__int64 __fastcall Window_setCursor(int a1, unsigned int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax

v2 = glutGetWindow();
v3 = get_window_glutID(a1);
glutSetWindow(v3);
glutSetCursor(a2);
return glutSetWindow(v2);
}

canonical_decompiled_code:
__int64 __fastcall func30(int a1, unsigned int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax

v2 = glutGetWindow();
v3 = func19(a1);
glutSetWindow(v3);
glutSetCursor(a2);
return glutSetWindow(v2);
}

original_code:
void Window_setCursor( int window_id, int cursor )
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSetCursor( cursor );
    glutSetWindow(cur);
}

canonical_original_code:
void Window_setCursor(int window_id, int cursor)
{
    int cur = glutGetWindow();
    glutSetWindow( get_window_glutID(window_id) );
    glutSetCursor( cursor );
    glutSetWindow(cur);
}

variable_types: cursor int


decompiled_code:
__int64 __fastcall get_state_variable(int a1, unsigned int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er12

v2 = glutGetWindow();
v3 = get_window_glutID(a1);
glutSetWindow(v3);
v4 = glutGet(a2);
glutSetWindow(v2);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func31(int a1, unsigned int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er12

v2 = glutGetWindow();
v3 = func19(a1);
glutSetWindow(v3);
v4 = glutGet(a2);
glutSetWindow(v2);
return v4;
}

original_code:
int get_state_variable( int window_id, GLenum en )
{
    int cur = glutGetWindow(), result;
    glutSetWindow( get_window_glutID(window_id) );
    result = glutGet( en );
    glutSetWindow(cur);
    return result;
}

canonical_original_code:
int get_state_variable(int window_id, unsigned int en)
{
    int cur = glutGetWindow(); int result;
    glutSetWindow( get_window_glutID(window_id) );
    result = glutGet( en );
    glutSetWindow(cur);
    return result;
}

variable_types:
en
unsigned int

variable_types: result int


decompiled_code:
__int64 __fastcall Window_getWidth(int a1)
{
return get_state_variable(a1, 0x66u);
}

canonical_decompiled_code:
__int64 __fastcall func32(int a1)
{
return func31(a1, 0x66u);
}

original_code:
int Window_getWidth( int window_id )
{
    return get_state_variable( window_id, 0x0066 );
}

canonical_original_code:
int Window_getWidth(int window_id)
{
    return get_state_variable( window_id, 0x0066 );
}


decompiled_code:
__int64 __fastcall Window_getHeight(int a1)
{
return get_state_variable(a1, 0x67u);
}

canonical_decompiled_code:
__int64 __fastcall func33(int a1)
{
return func31(a1, 0x67u);
}

original_code:
int Window_getHeight( int window_id )
{
    return get_state_variable( window_id, 0x0067 );
}

canonical_original_code:
int Window_getHeight(int window_id)
{
    return get_state_variable( window_id, 0x0067 );
}


decompiled_code:
_QWORD *GameState_new()
{
_QWORD *v0; // r12

v0 = calloc(0x40uLL, 1uLL);
v0[1] = Player_new();
v0[2] = Player_new();
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12

v0 = calloc(0x40uLL, 1uLL);
v0[1] = Player_new();
v0[2] = Player_new();
return v0;
}

original_code:
GameState* GameState_new( )
{
    GameState *gamestate = (GameState*)calloc( sizeof(GameState), 1 );
    gamestate->player1 = Player_new();
    gamestate->player2 = Player_new();
    return gamestate;
}

canonical_original_code:
struct GameState *GameState_new()
{
    struct GameState *gamestate = (GameState*)calloc( sizeof(GameState), 1 );
    gamestate->player1 = Player_new();
    gamestate->player2 = Player_new();
    return gamestate;
}

variable_types: struct GameState { enum GameMode { MODE_MENU=0, MODE_COUNTDOWN=1, MODE_RUNNING=2, MODE_PAUSED=3, MODE_FINISHED=4 } mode; float countdown; struct Player { float headPosition[3]; struct _Body { float *position; int gridPosition[2]; struct _Body *previous; struct _Body *next; } *head; float tailPosition[3]; float headOffset; float tailOffset; int underGround; float color[3]; int score; enum Direction { DIRECTION_NORTH=0, DIRECTION_SOUTH=1, DIRECTION_EAST=2, DIRECTION_WEST=3 } currentDir; float forward[3]; float up[3]; float maxLength; float length; float radius; } *player1; struct Player *player2; float playerSpeed; struct Object { float position[3]; float velocity[3]; float radius; } *player1_projectile; struct Object *player2_projectile; struct Object *edible; struct Landscape { float (**pointMap)[3]; float (**colorMap)[4]; float (**normalMap)[3]; int gridWidth; float northBound; float minHeight; float worldWidth; } *landscape; } *

user_defined_types: struct GameState { enum GameMode mode; float countdown; struct Player *player1; struct Player *player2; float playerSpeed; struct Object *player1_projectile; struct Object *player2_projectile; struct Object *edible; struct Landscape *landscape; }


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

Object_delete(*(_QWORD *)(a1 + 32));
result = Object_delete(*(_QWORD *)(a1 + 40));
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
return result;
}

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

Object_delete(*(_QWORD *)(a1 + 32));
result = Object_delete(*(_QWORD *)(a1 + 40));
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
return result;
}

original_code:
void GameState_clearProjectiles( GameState* gamestate )
{
    Object_delete(gamestate->player1_projectile);
    Object_delete(gamestate->player2_projectile);
    gamestate->player1_projectile = ((void *)0);
    gamestate->player2_projectile = ((void *)0);
}

canonical_original_code:
void GameState_clearProjectiles(struct GameState *gamestate)
{
    Object_delete(gamestate->player1_projectile);
    Object_delete(gamestate->player2_projectile);
    gamestate->player1_projectile = ((void *)0);
    gamestate->player2_projectile = ((void *)0);
}

user_defined_types: enum GameMode { MODE_MENU=0, MODE_COUNTDOWN=1, MODE_RUNNING=2, MODE_PAUSED=3, MODE_FINISHED=4 }

user_defined_types: struct Object { float position[3]; float velocity[3]; float radius; }

user_defined_types: struct Landscape { float (**pointMap)[3]; float (**colorMap)[4]; float (**normalMap)[3]; int gridWidth; float northBound; float minHeight; float worldWidth; }


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

result = Object_delete(*(_QWORD *)(a1 + 48));
*(_QWORD *)(a1 + 48) = 0LL;
return result;
}

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

result = Object_delete(*(_QWORD *)(a1 + 48));
*(_QWORD *)(a1 + 48) = 0LL;
return result;
}

original_code:
void GameState_clearEdibles( GameState* gamestate )
{
    Object_delete(gamestate->edible);
    gamestate->edible = ((void *)0);
}

canonical_original_code:
void GameState_clearEdibles(struct GameState *gamestate)
{
    Object_delete(gamestate->edible);
    gamestate->edible = ((void *)0);
}


decompiled_code:
__int64 __fastcall GameState_delete(_QWORD *a1)
{
if ( a1[1] )
Player_delete();
if ( a1[2] )
Player_delete();
GameState_clearProjectiles((__int64)a1);
GameState_clearEdibles((__int64)a1);
return Landscape_delete(a1[7]);
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
if ( a1[1] )
Player_delete();
if ( a1[2] )
Player_delete();
func1((__int64)a1);
func2((__int64)a1);
return Landscape_delete(a1[7]);
}

original_code:
void GameState_delete( GameState* gamestate )
{
    if( gamestate->player1 != ((void *)0) )
    {
        Player_delete( gamestate->player1 );
    }
    if( gamestate->player2 != ((void *)0) )
    {
        Player_delete( gamestate->player2 );
    }
    GameState_clearProjectiles(gamestate);
    GameState_clearEdibles(gamestate);
    Landscape_delete( gamestate->landscape );
}

canonical_original_code:
void GameState_delete(struct GameState *gamestate)
{
    if( gamestate->player1 != ((void *)0) )
    {
        Player_delete( gamestate->player1 );
    }
    if( gamestate->player2 != ((void *)0) )
    {
        Player_delete( gamestate->player2 );
    }
    GameState_clearProjectiles(gamestate);
    GameState_clearEdibles(gamestate);
    Landscape_delete( gamestate->landscape );
}


decompiled_code:
__int64 __fastcall GameState_clearPlayers(__int64 a1)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 result; // rax

Player_delete(*(_QWORD *)(a1 + 8));
v2 = Player_new();
v3 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v2;
Player_delete(v3);
result = Player_new();
*(_QWORD *)(a1 + 16) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 result; // rax

Player_delete(*(_QWORD *)(a1 + 8));
v2 = Player_new();
v3 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v2;
Player_delete(v3);
result = Player_new();
*(_QWORD *)(a1 + 16) = result;
return result;
}

original_code:
void GameState_clearPlayers( GameState* gamestate )
{
    Player_delete(gamestate->player1);
    gamestate->player1 = Player_new();
    Player_delete(gamestate->player2);
    gamestate->player2 = Player_new();
}

canonical_original_code:
void GameState_clearPlayers(struct GameState *gamestate)
{
    Player_delete(gamestate->player1);
    gamestate->player1 = Player_new();
    Player_delete(gamestate->player2);
    gamestate->player2 = Player_new();
}


decompiled_code:
void *Object_new()
{
return calloc(0x1CuLL, 1uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(0x1CuLL, 1uLL);
}

original_code:
Object* Object_new( )
{
    Object* object = (Object*)calloc( sizeof(Object), 1 );
    return object;
}

canonical_original_code:
struct Object *Object_new()
{
    struct Object *object = (Object*)calloc( sizeof(Object), 1 );
    return object;
}

variable_types: struct Object { float position[3]; float velocity[3]; float radius; } *


decompiled_code:
// attributes: thunk
void Object_delete(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void Object_delete( Object* object )
{
    free(object);
}

canonical_original_code:
void Object_delete(struct Object *object)
{
    free(object);
}


decompiled_code:
__int64 __fastcall draw_text(_BYTE *a1)
{
_BYTE *v1; // rbx
__int64 result; // rax

if ( *a1 )
{
v1 = a1;
do
{
++v1;
result = glutStrokeCharacter(&glutStrokeRoman);
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
_BYTE *v1; // rbx
__int64 result; // rax

if ( *a1 )
{
v1 = a1;
do
{
++v1;
result = glutStrokeCharacter(&glutStrokeRoman);
}
while ( *v1 );
}
return result;
}

original_code:
void draw_text( char* text )
{
    while( *text != '\0' )
    {
        glutStrokeCharacter( ((void *) &glutStrokeRoman), *text );
        text++;
    }
}

canonical_original_code:
void draw_text(char *text)
{
    while( *text != '\0' )
    {
        glutStrokeCharacter( ((void *) &glutStrokeRoman), *text );
        text++;
    }
}

variable_types:
text
char *


decompiled_code:
__int64 __fastcall draw_2D_text(_BYTE *a1, int a2, int a3, int a4, __int64 a5)
{
__int64 result; // rax
float v9; // [rsp+0h] [rbp-30h]
float v10; // [rsp+4h] [rbp-2Ch]

if ( a1 )
{
v10 = (float)*(int *)(a5 + 12) / (float)(*(float *)(a5 + 36) - *(float *)(a5 + 40));
v9 = (float)*(int *)(a5 + 8) / (float)(*(float *)(a5 + 32) - *(float *)(a5 + 28));
glMatrixMode(5888LL);
glPushMatrix();
glLoadIdentity();
glTranslatef((float)((float)a2 / v9) + *(float *)(a5 + 28), (float)((float)a3 / v10) + *(float *)(a5 + 40), 0.0);
glScalef((float)a4 / (float)(152.38 * v10), (float)a4 / (float)(152.38 * v10), 1.0);
draw_text(a1);
result = glPopMatrix();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, int a2, int a3, int a4, __int64 a5)
{
__int64 result; // rax
float v9; // [rsp+0h] [rbp-30h]
float v10; // [rsp+4h] [rbp-2Ch]

if ( a1 )
{
v10 = (float)*(int *)(a5 + 12) / (float)(*(float *)(a5 + 36) - *(float *)(a5 + 40));
v9 = (float)*(int *)(a5 + 8) / (float)(*(float *)(a5 + 32) - *(float *)(a5 + 28));
glMatrixMode(5888LL);
glPushMatrix();
glLoadIdentity();
glTranslatef((float)((float)a2 / v9) + *(float *)(a5 + 28), (float)((float)a3 / v10) + *(float *)(a5 + 40), 0.0);
glScalef((float)a4 / (float)(152.38 * v10), (float)a4 / (float)(152.38 * v10), 1.0);
func0(a1);
result = glPopMatrix();
}
return result;
}

original_code:
void draw_2D_text( char* text, int x, int y, int height, Viewport* vp )
{
    float vscale,
          hscale,
          vppu,
          hppu;
    if( text == ((void *)0) )
        return;
    vppu = vp->height / (vp->top - vp->bottom);
    hppu = vp->width / (vp->right - vp->left);
    glMatrixMode(0x1700);
    glPushMatrix();
    glLoadIdentity();
    vscale = height / (vppu * (float)152.38);
    glTranslatef( vp->left + x / hppu, vp->bottom + y / vppu, 0.0f );
    glScalef( vscale, vscale, 1.0f );
    draw_text(text);
    glPopMatrix();
}

canonical_original_code:
void draw_2D_text(char *text, int x, int y, int height, struct Viewport *vp)
{
    float vscale; float hscale; float vppu; float hppu;
    if( text == ((void *)0) )
        return;
    vppu = vp->height / (vp->top - vp->bottom);
    hppu = vp->width / (vp->right - vp->left);
    glMatrixMode(0x1700);
    glPushMatrix();
    glLoadIdentity();
    vscale = height / (vppu * (float)152.38);
    glTranslatef( vp->left + x / hppu, vp->bottom + y / vppu, 0.0f );
    glScalef( vscale, vscale, 1.0f );
    draw_text(text);
    glPopMatrix();
}

variable_types: struct Viewport { struct Camera { float position[3]; float forward[3]; float up[3]; } *camera; int width; int x; int ortho; float left; } *

variable_types: vscale float

variable_types: hscale float

variable_types: vppu float

variable_types: hppu float


decompiled_code:
__int64 __fastcall draw_centered_text(_BYTE *a1, int a2, int a3, int a4, __int64 a5)
{
float v5; // xmm0_4

v5 = 0.0;
if ( *a1 )
{
do
v5 = v5 + (float)(int)glutStrokeWidth(&glutStrokeRoman);
while ( *a1 );
v5 = v5 * 0.5;
}
return draw_2D_text(a1, (int)(float)((float)a2 - v5), a3, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1, int a2, int a3, int a4, __int64 a5)
{
float v5; // xmm0_4

v5 = 0.0;
if ( *a1 )
{
do
v5 = v5 + (float)(int)glutStrokeWidth(&glutStrokeRoman);
while ( *a1 );
v5 = v5 * 0.5;
}
return func1(a1, (int)(float)((float)a2 - v5), a3, a4, a5);
}

original_code:
void draw_centered_text( char* text, int x, int y, int height, Viewport* vp )
{
    char* c = text;
    float width = 0.0f;
    while( *c != '\0' )
    {
        width += glutStrokeWidth( ((void *) &glutStrokeRoman), *c );
    }
    draw_2D_text( text, x - width / 2, y, height, vp );
}

canonical_original_code:
void draw_centered_text(char *text, int x, int y, int height, struct Viewport *vp)
{
    char *c = text;
    float width = 0.0f;
    while( *c != '\0' )
    {
        width += glutStrokeWidth( ((void *) &glutStrokeRoman), *c );
    }
    draw_2D_text( text, x - width / 2, y, height, vp );
}

variable_types:
c
char *

variable_types: width float


decompiled_code:
void *Camera_new()
{
return calloc(0x24uLL, 1uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(0x24uLL, 1uLL);
}

original_code:
Camera* Camera_new()
{
    return (Camera*)calloc( sizeof(Camera), 1 );
}

canonical_original_code:
struct Camera *Camera_new()
{
    return (struct Camera *)calloc( sizeof(Camera), 1 );
}

return_type: struct Camera { float position[3]; float forward[3]; float up[3]; } *


decompiled_code:
// attributes: thunk
void Camera_delete(void *ptr)
{
free(ptr);
}

original_code:
void Camera_delete( Camera* camera )
{
    free(camera);
}

canonical_original_code:
void Camera_delete(struct Camera *camera)
{
    free(camera);
}

variable_types: struct Camera { float position[3]; float forward[3]; float up[3]; } *