decompiled_code:
void __fastcall bitmap_print(__int64 a1)
{
unsigned __int64 v1; // rbx

if ( *(int *)(a1 + 16) > 0 )
{
v1 = 0LL;
do
{
printf("Bit %d): ", (unsigned int)v1);
if ( (unsigned int)bit(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)(v1 >> 13)) + 40LL), v1 & 0x1FFF) )
puts("SET");
else
puts("NOT SET");
++v1;
}
while ( *(_DWORD *)(a1 + 16) > (int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
unsigned __int64 v1; // rbx

if ( *(int *)(a1 + 16) > 0 )
{
v1 = 0LL;
do
{
printf("Bit %d): ", (unsigned int)v1);
if ( (unsigned int)func0(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)(v1 >> 13)) + 40LL), v1 & 0x1FFF) )
puts("SET");
else
puts("NOT SET");
++v1;
}
while ( *(_DWORD *)(a1 + 16) > (int)v1 );
}
}

original_code:
void bitmap_print(struct generic_bitmap *bmap)
{
 int i;
 int block_nr, block_offset;
 for(i = 0; i < bmap->num_bits; i++) {
  block_nr = i / ((1024/sizeof(int)) * (sizeof(int) << 3));
  block_offset = i % ((1024/sizeof(int)) * (sizeof(int) << 3));
  printf("Bit %d): ", i);
  if(bit(bmap->blocks[block_nr]->blk_data, block_offset))
   printf("SET\n");
  else
   printf("NOT SET\n");
 }
}

canonical_original_code:
void bitmap_print(struct generic_bitmap *bmap)
{
 int i;
 int block_nr; int block_offset;
 for(i = 0; i < bmap->num_bits; i++) {
  block_nr = i / ((1024/sizeof(int)) * (sizeof(int) << 3));
  block_offset = i % ((1024/sizeof(int)) * (sizeof(int) << 3));
  printf("Bit %d): ", i);
  if(bit(bmap->blocks[block_nr]->blk_data, block_offset))
   printf("SET\n");
  else
   printf("NOT SET\n");
 }
}

variable_types: struct generic_bitmap { int num_blocks; struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } **blocks; int num_bits; } *


repo: jordan-dinwiddy/Somix

original_code:
int alloc_bit(struct generic_bitmap *bitmap, int origin)
{
 register unsigned k;
 register int a;
 register int *wptr, *wlim;
 int i, b, w, o, block_count;
 struct minix_block *bp;
 if(origin >= bitmap->num_bits) origin = 0;
 b = origin >> 13;
 o = origin - (b << 13);
 w = o/(sizeof(int) << 3);
 block_count = bitmap->num_blocks;
 while(block_count--) {
  bp = bitmap->blocks[b];
  wptr = ((int *) bp->blk_data) + w;
  wlim = ((int *) bp->blk_data) +
    (1024/sizeof(int));
  a = w * (sizeof(int) << 3);
  while(wptr != wlim) {
   if((k = (unsigned) *wptr) != (unsigned) ~0) {
    for(i = 0; i < (sizeof(int) << 3); i++) {
     if(!bit((char *)wptr, i)) {
      a += (b * (1024/sizeof(int)) *
       (sizeof(int) << 3)) + i;
      if(a >= bitmap->num_bits) {
       wptr = wlim - 1;
       break;
      }
      setbit((char *)wptr, i);
      bp->blk_dirty = 1;
      return a;
     }
    }
   }
   wptr++;
   a += (sizeof(int) << 3);
  }
  if(++b == bitmap->num_blocks) b = 0;
  w = 0;
 }
 return -1;
}

canonical_original_code:
int alloc_bit(struct generic_bitmap *bitmap, int origin)
{
 register unsigned int k;
 register int a;
 register int *wptr; register int *wlim;
 int i; int b; int w; int o; int block_count;
 struct minix_block *bp;
 if(origin >= bitmap->num_bits) origin = 0;
 b = origin >> 13;
 o = origin - (b << 13);
 w = o/(sizeof(int) << 3);
 block_count = bitmap->num_blocks;
 while(block_count--) {
  bp = bitmap->blocks[b];
  wptr = ((int *) bp->blk_data) + w;
  wlim = ((int *) bp->blk_data) +
    (1024/sizeof(int));
  a = w * (sizeof(int) << 3);
  while(wptr != wlim) {
   if((k = (unsigned int) *wptr) != (unsigned int) ~0) {
    for(i = 0; i < (sizeof(int) << 3); i++) {
     if(!bit((char *)wptr, i)) {
      a += (b * (1024/sizeof(int)) *
       (sizeof(int) << 3)) + i;
      if(a >= bitmap->num_bits) {
       wptr = wlim - 1;
       break;
      }
      setbit((char *)wptr, i);
      bp->blk_dirty = 1;
      return a;
     }
    }
   }
   wptr++;
   a += (sizeof(int) << 3);
  }
  if(++b == bitmap->num_blocks) b = 0;
  w = 0;
 }
 return -1;
}

variable_types: origin int

variable_types:
k
unsigned int

variable_types: a int

variable_types:
wptr
int *

variable_types:
wlim
int *

variable_types: i int

variable_types: b int

variable_types: w int

variable_types: o int

variable_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *

return_type: int

user_defined_types: struct generic_bitmap { int num_blocks; struct minix_block **blocks; int num_bits; }


decompiled_code:
__int64 __fastcall free_bit(__int64 a1, signed int a2)
{
int v2; // ebx
__int64 result; // rax

v2 = a2 / 0x2000;
if ( !(unsigned int)bit(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (a2 / 0x2000)) + 40LL), a2 % 0x2000) )
panic("free_bit(%d): bit is already free", (unsigned int)a2);
clrbit(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v2) + 40LL), a2 % 0x2000);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v2);
*(_BYTE *)(result + 32) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, signed int a2)
{
int v2; // ebx
__int64 result; // rax

v2 = a2 / 0x2000;
if ( !(unsigned int)func0(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (a2 / 0x2000)) + 40LL), a2 % 0x2000) )
panic("free_bit(%d): bit is already free", (unsigned int)a2);
func2(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v2) + 40LL), a2 % 0x2000);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v2);
*(_BYTE *)(result + 32) = 1;
return result;
}

original_code:
void free_bit(struct generic_bitmap *bitmap, int bit_num)
{
 int bits_per_block = 1024 * 8;
 int block = bit_num / bits_per_block;
 int block_bit = bit_num - (block * bits_per_block);
 if(!bit(bitmap->blocks[block]->blk_data, block_bit)) {
  panic("free_bit(%d): bit is already free", bit_num);
 }
 clrbit(bitmap->blocks[block]->blk_data, block_bit);
 bitmap->blocks[block]->blk_dirty = 1;
}

variable_types: block int

return_type: void

user_defined_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; }


bit

decompiled_code:
__int64 __fastcall bit(__int64 a1, unsigned int a2)
{
return (*(char *)(a1 + (a2 >> 3)) >> (a2 & 7)) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
return (*(char *)(a1 + (a2 >> 3)) >> (a2 & 7)) & 1;
}

original_code:
static inline int bit(char * addr,unsigned int nr)
{
 return (addr[nr >> 3] & (1<<(nr & 7))) != 0;
}

canonical_original_code:
static inline int bit(char *addr, unsigned int nr)
{
 return (addr[nr >> 3] & (1<<(nr & 7))) != 0;
}

variable_types:
addr
char *

variable_types:
nr
unsigned int


setInterruptHandler

decompiled_code:
void __fastcall setInterruptHandler(int a1, int a2, __int16 a3)
{
idt[4 * a1] = a2;
idt[4 * a1 + 1] = 16;
idt[4 * a1 + 2] = (a3 << 13) | 0x8E00;
idt[4 * a1 + 3] = HIWORD(a2);
}

original_code:
void setInterruptHandler(int vector, void (*handler)(), int maxAccessibleFromPL)
{
  Word flags = (Word)(maxAccessibleFromPL << 13);
  flags |= 0x8E00;
  idt[vector].lowOffset = (Word)(((DWord)handler) & 0xFFFF);
  idt[vector].segmentSelector = 0x10;
  idt[vector].flags = flags;
  idt[vector].highOffset = (Word)((((DWord)handler) >> 16) & 0xFFFF);
}

canonical_original_code:
void setInterruptHandler(int vector, void (*handler)(), int maxAccessibleFromPL)
{
  unsigned short flags = (Word)(maxAccessibleFromPL << 13);
  flags |= 0x8E00;
  idt[vector].lowOffset = (Word)(((unsigned long)handler) & 0xFFFF);
  idt[vector].segmentSelector = 0x10;
  idt[vector].flags = flags;
  idt[vector].highOffset = (Word)((((unsigned long)handler) >> 16) & 0xFFFF);
}

variable_types: vector int

variable_types:
handler
void (*)()

variable_types: maxAccessibleFromPL int

variable_types:
flags
unsigned short


setTrapHandler

original_code:
void setTrapHandler(int vector, void (*handler)(), int maxAccessibleFromPL)
{
  Word flags = (Word)(maxAccessibleFromPL << 13);
  flags |= 0x8E00;
  idt[vector].lowOffset = (Word)(((DWord)handler) & 0xFFFF);
  idt[vector].segmentSelector = 0x10;
  idt[vector].flags = flags;
  idt[vector].highOffset = (Word)((((DWord)handler) >> 16) & 0xFFFF);
}

canonical_original_code:
void setTrapHandler(int vector, void (*handler)(), int maxAccessibleFromPL)
{
  unsigned short flags = (Word)(maxAccessibleFromPL << 13);
  flags |= 0x8E00;
  idt[vector].lowOffset = (Word)(((unsigned long)handler) & 0xFFFF);
  idt[vector].segmentSelector = 0x10;
  idt[vector].flags = flags;
  idt[vector].highOffset = (Word)((((unsigned long)handler) >> 16) & 0xFFFF);
}


setIdt

decompiled_code:
__int64 setIdt()
{
*(_QWORD *)(&idtR + 1) = idt;
idtR = 2047;
set_handlers();
setInterruptHandler(32, (int)&clock_handler, 0);
setInterruptHandler(33, (int)&keyboard_handler, 0);
setTrapHandler(128, (int)&system_call_handler, 3);
return set_idt_reg(&idtR);
}

canonical_decompiled_code:
__int64 func2()
{
*(_QWORD *)(&idtR + 1) = idt;
idtR = 2047;
set_handlers();
func0(32, (int)&clock_handler, 0);
func0(33, (int)&keyboard_handler, 0);
func1(128, (int)&system_call_handler, 3);
return set_idt_reg(&idtR);
}

original_code:
void setIdt()
{
  idtR.base = (DWord)idt;
  idtR.limit = 256 * sizeof(Gate) - 1;
  set_handlers();
  setInterruptHandler(32, clock_handler , 0 );
  setInterruptHandler(33, keyboard_handler , 0 );
  setTrapHandler(0x80, system_call_handler, 3);
  set_idt_reg(&idtR);
}

canonical_original_code:
void setIdt()
{
  idtR.base = (unsigned long)idt;
  idtR.limit = 256 * sizeof(Gate) - 1;
  set_handlers();
  setInterruptHandler(32, clock_handler , 0 );
  setInterruptHandler(33, keyboard_handler , 0 );
  setTrapHandler(0x80, system_call_handler, 3);
  set_idt_reg(&idtR);
}


decompiled_code:
__int64 clock_routine()
{
__int64 v0; // rax
__int64 v1; // rax

v0 = current();
user_to_system(v0 + 40);
zeos_show_clock();
++zeos_ticks;
update_sched_data_rr();
if ( (unsigned int)needs_sched_rr() )
{
update_current_state_rr(&readyqueue);
sched_next_rr();
}
v1 = current();
return system_to_user(v1 + 40);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rax
__int64 v1; // rax

v0 = current();
user_to_system(v0 + 40);
zeos_show_clock();
++zeos_ticks;
update_sched_data_rr();
if ( (unsigned int)needs_sched_rr() )
{
update_current_state_rr(&readyqueue);
sched_next_rr();
}
v1 = current();
return system_to_user(v1 + 40);
}

original_code:
void clock_routine() {
user_to_system(&(current()->st));
 zeos_show_clock();
 ++zeos_ticks;
 update_sched_data_rr();
 if (needs_sched_rr()){
   update_current_state_rr(&readyqueue);
   sched_next_rr();
 }
system_to_user(&(current()->st));
}


decompiled_code:
__int64 keyboard_routine()
{
__int64 v0; // rax
signed __int8 v1; // al
__int64 v2; // rax

v0 = current();
user_to_system(v0 + 40);
v1 = inb(0x60u);
if ( v1 >= 0 )
printc_xy(24LL, 24LL, (unsigned int)char_map[v1 & 0x7F]);
v2 = current();
return system_to_user(v2 + 40);
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v0; // rax
signed __int8 v1; // al
__int64 v2; // rax

v0 = current();
user_to_system(v0 + 40);
v1 = inb(0x60u);
if ( v1 >= 0 )
printc_xy(24LL, 24LL, (unsigned int)char_map[v1 & 0x7F]);
v2 = current();
return system_to_user(v2 + 40);
}

original_code:
void keyboard_routine() {
user_to_system(&(current()->st));
 unsigned char c,d,res;
 c = inb(0x60);
 d=0x80;
 Byte b=24;
 res=d&c;
 if (res == 0){
 c = 0x7F&c;
 c=char_map[c];
 printc_xy(b,b,c);
}
system_to_user(&(current()->st));
}

canonical_original_code:
void keyboard_routine() {
user_to_system(&(current()->st));
 unsigned char c; unsigned char d; unsigned char res;
 c = inb(0x60);
 d=0x80;
 unsigned char b = 24;
 res=d&c;
 if (res == 0){
 c = 0x7F&c;
 c=char_map[c];
 printc_xy(b,b,c);
}
system_to_user(&(current()->st));
}

variable_types:
c
unsigned char

variable_types:
d
unsigned char

variable_types:
res
unsigned char

variable_types:
b
unsigned char


decompiled_code:
void __fastcall list_del(__int64 a1)
{
_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = (void*) 0;
 entry->prev = (void*) 0;
}

canonical_original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = (void *) 0;
 entry->prev = (void *) 0;
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
void __noreturn cpu_idle()
{
_enable();
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func7()
{
_enable();
while ( 1 )
;
}

original_code:
void cpu_idle(void)
{
 __asm__ __volatile__("sti": : :"memory");
 while(1)
 {
 ;
 }
}

canonical_original_code:
void cpu_idle()
{
 __asm__ __volatile__("sti": : :"memory");
 while(1)
 {
 ;
 }
}


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

*(_QWORD *)(a1 + 8) = *(_QWORD *)(a1 + 8) + get_ticks() - *(_QWORD *)(a1 + 32);
result = get_ticks();
*(_QWORD *)(a1 + 32) = result;
return result;
}

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

*(_QWORD *)(a1 + 8) = *(_QWORD *)(a1 + 8) + get_ticks() - *(_QWORD *)(a1 + 32);
result = get_ticks();
*(_QWORD *)(a1 + 32) = result;
return result;
}

original_code:
void run_to_ready(struct stats *st){
st->system_ticks += get_ticks()- st->elapsed_total_ticks;
st->elapsed_total_ticks = get_ticks();
}

variable_types: struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; } *


decompiled_code:
__int64 __fastcall get_DIR(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
page_table_entry * get_DIR (struct task_struct *t)
{
 return t->dir_pages_baseAddr;
}

canonical_original_code:
union page_table_entry *get_DIR(struct task_struct *t)
{
 return t->dir_pages_baseAddr;
}

variable_types: struct task_struct { int PID; union page_table_entry *dir_pages_baseAddr; struct list_head { struct list_head *next; } list; int quantum; struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; } st; enum state_t { ST_RUN=0, ST_READY=1, ST_BLOCKED=2, ST_ZOMBIE=3 } estat; int pos; unsigned long *kernel_esp; } *

return_type: union page_table_entry { unsigned int entry; struct { unsigned int present; unsigned int rw; unsigned int user; unsigned int write_t; unsigned int cache_d; unsigned int accessed; unsigned int dirty; unsigned int ps_pat; unsigned int global; unsigned int avail; unsigned int pbase_addr; } bits; } *

user_defined_types: struct task_struct { int PID; union page_table_entry *dir_pages_baseAddr; struct list_head list; int quantum; struct stats st; enum state_t estat; int pos; unsigned long *kernel_esp; }


decompiled_code:
__int64 __fastcall get_PT(__int64 a1)
{
return **(_DWORD **)(a1 + 8) & 0xFFFFF000;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return **(_DWORD **)(a1 + 8) & 0xFFFFF000;
}

original_code:
page_table_entry * get_PT (struct task_struct *t)
{
 return (page_table_entry *)(((unsigned int)(t->dir_pages_baseAddr->bits.pbase_addr))<<12);
}

canonical_original_code:
union page_table_entry *get_PT(struct task_struct *t)
{
 return (union page_table_entry *)(((unsigned int)(t->dir_pages_baseAddr->bits.pbase_addr))<<12);
}

user_defined_types: union page_table_entry { unsigned int entry; struct { unsigned int present; unsigned int rw; unsigned int user; unsigned int write_t; unsigned int cache_d; unsigned int accessed; unsigned int dirty; unsigned int ps_pat; unsigned int global; unsigned int avail; unsigned int pbase_addr; } bits; }

user_defined_types: struct list_head { struct list_head *next; }

user_defined_types: struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; }

user_defined_types: enum state_t { ST_RUN=0, ST_READY=1, ST_BLOCKED=2, ST_ZOMBIE=3 }


original_code:
int allocate_DIR(struct task_struct *t)
{
 int pos = -1;
 int j;
 for (j = 0; j < 10 && pos == -1;++j){
  if (vdir[j] == 0) pos = j;
 }
 if (pos == -1) return -1;
 ++vdir[pos];
 t->pos = pos;
 t->dir_pages_baseAddr = (page_table_entry*) &dir_pages[pos];
 return 1;
}

canonical_original_code:
int allocate_DIR(struct task_struct *t)
{
 int pos = -1;
 int j;
 for (j = 0; j < 10 && pos == -1;++j){
  if (vdir[j] == 0) pos = j;
 }
 if (pos == -1) return -1;
 ++vdir[pos];
 t->pos = pos;
 t->dir_pages_baseAddr = (union page_table_entry *) &dir_pages[pos];
 return 1;
}

variable_types: struct task_struct { int PID; union page_table_entry { unsigned int entry; struct { unsigned int present; unsigned int rw; unsigned int user; unsigned int write_t; unsigned int cache_d; unsigned int accessed; unsigned int dirty; unsigned int ps_pat; unsigned int global; unsigned int avail; unsigned int pbase_addr; } bits; } *dir_pages_baseAddr; struct list_head { struct list_head *next; } list; int quantum; struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; } st; enum state_t { ST_RUN=0, ST_READY=1, ST_BLOCKED=2, ST_ZOMBIE=3 } estat; int pos; unsigned long *kernel_esp; } *

variable_types: pos int

variable_types: j int


decompiled_code:
_BOOL8 needs_sched_rr()
{
return current_quantum == 0;
}

canonical_decompiled_code:
_BOOL8 func16()
{
return current_quantum == 0;
}

original_code:
int needs_sched_rr (void){
return (current_quantum == 0);
}

canonical_original_code:
int needs_sched_rr(){
return (current_quantum == 0);
}


decompiled_code:
__int64 __fastcall get_quantum(__int64 a1)
{
return *(unsigned int *)(a1 + 32);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
return *(unsigned int *)(a1 + 32);
}

original_code:
int get_quantum (struct task_struct *t){
return t->quantum;
}

canonical_original_code:
int get_quantum(struct task_struct *t){
return t->quantum;
}


decompiled_code:
void __fastcall set_quantum(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 32) = a2;
}

canonical_decompiled_code:
void __fastcall func18(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 32) = a2;
}

original_code:
void set_quantum (struct task_struct *t, int new_quantum){
t->quantum = new_quantum;
}

canonical_original_code:
void set_quantum(struct task_struct *t, int new_quantum){
t->quantum = new_quantum;
}


original_code:
void init_idle (void)
{
task[0].task.PID = 0;
task[0].stack[1024 -1]= (unsigned long) cpu_idle;
task[0].stack[1024 -2]= 0;
task[0].task.kernel_esp = &task[0].stack[1024 -2];
set_quantum(&task[0].task,18);
task[0].task.estat = ST_READY;
}

canonical_original_code:
void init_idle()
{
task[0].task.PID = 0;
task[0].stack[1024 -1]= (unsigned long) cpu_idle;
task[0].stack[1024 -2]= 0;
task[0].task.kernel_esp = &task[0].stack[1024 -2];
set_quantum(&task[0].task,18);
task[0].task.estat = ST_READY;
}


decompiled_code:
void init_sched()
{
_DWORD *v0; // rax
__int64 v1; // rbx

INIT_LIST_HEAD(&freequeue);
INIT_LIST_HEAD(&readyqueue);
v0 = &semafor;
do
{
*v0 = -1;
v0 += 6;
}
while ( v0 != (_DWORD *)(unsigned int)((_DWORD)&semafor + 240) );
v1 = (unsigned int)((_DWORD)&task + 16400);
do
{
list_add((_QWORD *)v1, &freequeue);
*(_DWORD *)(v1 + 80) = 3;
v1 += 0x2000LL;
}
while ( v1 != (_DWORD)&task + 81936 );
}

canonical_decompiled_code:
void func20()
{
_DWORD *v0; // rax
__int64 v1; // rbx

func0(&freequeue);
func0(&readyqueue);
v0 = &semafor;
do
{
*v0 = -1;
v0 += 6;
}
while ( v0 != (_DWORD *)(unsigned int)((_DWORD)&semafor + 240) );
v1 = (unsigned int)((_DWORD)&task + 16400);
do
{
func2((_QWORD *)v1, &freequeue);
*(_DWORD *)(v1 + 80) = 3;
v1 += 0x2000LL;
}
while ( v1 != (_DWORD)&task + 81936 );
}

original_code:
void init_sched(){
int i;
  INIT_LIST_HEAD(&freequeue);
  INIT_LIST_HEAD(&readyqueue);
for (i = 0; i < 10; ++i){
 semafor[i].owner = -1;
}
for(i=2;i<10;++i) {
 list_add(&task[i].task.list,&freequeue);
 task[i].task.estat = ST_ZOMBIE;
  }
}

variable_types: i int


current

decompiled_code:
__int64 current()
{
int v0; // esp

return v0 & 0xFFFFF000;
}

canonical_decompiled_code:
__int64 func21()
{
int v0; // esp

return v0 & 0xFFFFF000;
}

original_code:
struct task_struct* current()
{
  int ret_value;
  __asm__ __volatile__(
   "movl %%esp, %0"
 : "=g" (ret_value)
  );
  return (struct task_struct*)(ret_value&0xfffff000);
}

canonical_original_code:
struct task_struct *current()
{
  int ret_value;
  __asm__ __volatile__(
   "movl %%esp, %0"
 : "=g" (ret_value)
  );
  return (struct task_struct *)(ret_value&0xfffff000);
}

return_type: struct task_struct { int PID; union page_table_entry { unsigned int entry; struct { unsigned int present; unsigned int rw; unsigned int user; unsigned int write_t; unsigned int cache_d; unsigned int accessed; unsigned int dirty; unsigned int ps_pat; unsigned int global; unsigned int avail; unsigned int pbase_addr; } bits; } *dir_pages_baseAddr; struct list_head { struct list_head *next; } list; int quantum; struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; } st; enum state_t { ST_RUN=0, ST_READY=1, ST_BLOCKED=2, ST_ZOMBIE=3 } estat; int pos; unsigned long *kernel_esp; } *


decompiled_code:
__int64 actualitzar_quantum()
{
int v0; // ebx
__int64 result; // rax

current_quantum = *(_DWORD *)(current() + 32);
v0 = current_quantum;
result = current();
*(_QWORD *)(result + 88) = v0;
return result;
}

canonical_decompiled_code:
__int64 func22()
{
int v0; // ebx
__int64 result; // rax

current_quantum = *(_DWORD *)(func21() + 32);
v0 = current_quantum;
result = func21();
*(_QWORD *)(result + 88) = v0;
return result;
}

original_code:
void actualitzar_quantum (void){
current_quantum = current()->quantum;
current()->st.remaining_ticks = current_quantum;
}

canonical_original_code:
void actualitzar_quantum(){
current_quantum = current()->quantum;
current()->st.remaining_ticks = current_quantum;
}


decompiled_code:
__int64 update_sched_data_rr()
{
int v0; // ebx
__int64 result; // rax

v0 = --current_quantum;
result = current();
*(_QWORD *)(result + 88) = v0;
return result;
}

canonical_decompiled_code:
__int64 func23()
{
int v0; // ebx
__int64 result; // rax

v0 = --current_quantum;
result = func21();
*(_QWORD *)(result + 88) = v0;
return result;
}

original_code:
void update_sched_data_rr(void){
current_quantum--;
current()->st.remaining_ticks = current_quantum;
}

canonical_original_code:
void update_sched_data_rr(){
current_quantum--;
current()->st.remaining_ticks = current_quantum;
}


decompiled_code:
__int64 sched_next_rr()
{
__int64 v0; // rax
__int64 result; // rax
__int64 v2; // rbx

v0 = current();
run_to_ready(v0 + 40);
if ( list_empty(&readyqueue) )
{
ready_to_run((_QWORD *)(unsigned int)((_DWORD)task + 40));
*(_DWORD *)(current() + 96) = 1;
task[24] = 0;
result = task_switch(task);
}
else
{
v2 = list_head_to_task_struct(readyqueue);
*(_DWORD *)(current() + 96) = 1;
list_del(v2 + 16);
*(_DWORD *)(v2 + 96) = 0;
ready_to_run((_QWORD *)(v2 + 40));
actualitzar_quantum();
current_quantum = *(_DWORD *)(v2 + 32);
result = task_switch(v2);
}
return result;
}

canonical_decompiled_code:
__int64 func25()
{
__int64 v0; // rax
__int64 result; // rax
__int64 v2; // rbx

v0 = func21();
func10(v0 + 40);
if ( func6(&readyqueue) )
{
func11((_QWORD *)(unsigned int)((_DWORD)task + 40));
*(_DWORD *)(func21() + 96) = 1;
task[24] = 0;
result = task_switch(task);
}
else
{
v2 = func12(readyqueue);
*(_DWORD *)(func21() + 96) = 1;
func5(v2 + 16);
*(_DWORD *)(v2 + 96) = 0;
func11((_QWORD *)(v2 + 40));
func22();
current_quantum = *(_DWORD *)(v2 + 32);
result = task_switch(v2);
}
return result;
}

original_code:
void sched_next_rr(void){
union task_union *p;
run_to_ready(&current()->st);
if ((list_empty(&readyqueue))){
 ready_to_run(&task[0].task.st);
 current()->estat = ST_READY;
 task[0].task.estat = ST_RUN;
 task_switch(&task[0]);
}
else {
 p = (union task_union *)(list_head_to_task_struct(readyqueue.next));
 current()->estat = ST_READY;
 list_del(&(p->task.list));
 p->task.estat = ST_RUN;
 ready_to_run(&p->task.st);
 actualitzar_quantum();
 current_quantum = p->task.quantum;
 task_switch(p);
 }
}

canonical_original_code:
void sched_next_rr(){
union task_union *p;
run_to_ready(&current()->st);
if ((list_empty(&readyqueue))){
 ready_to_run(&task[0].task.st);
 current()->estat = ST_READY;
 task[0].task.estat = ST_RUN;
 task_switch(&task[0]);
}
else {
 p = (union task_union *)(list_head_to_task_struct(readyqueue.next));
 current()->estat = ST_READY;
 list_del(&(p->task.list));
 p->task.estat = ST_RUN;
 ready_to_run(&p->task.st);
 actualitzar_quantum();
 current_quantum = p->task.quantum;
 task_switch(p);
 }
}

variable_types: union task_union { struct task_struct { int PID; union page_table_entry { unsigned int entry; struct { unsigned int present; unsigned int rw; unsigned int user; unsigned int write_t; unsigned int cache_d; unsigned int accessed; unsigned int dirty; unsigned int ps_pat; unsigned int global; unsigned int avail; unsigned int pbase_addr; } bits; } *dir_pages_baseAddr; struct list_head { struct list_head *next; } list; int quantum; struct stats { unsigned long user_ticks; unsigned long system_ticks; unsigned long blocked_ticks; unsigned long ready_ticks; unsigned long elapsed_total_ticks; unsigned long total_trans; unsigned long remaining_ticks; } st; enum state_t { ST_RUN=0, ST_READY=1, ST_BLOCKED=2, ST_ZOMBIE=3 } estat; int pos; unsigned long *kernel_esp; } task; unsigned long stack[1024]; } *

user_defined_types: union task_union { struct task_struct task; unsigned long stack[1024]; }


decompiled_code:
__int64 reset_stats()
{
__int64 v0; // rbx
__int64 result; // rax

*(_QWORD *)(current() + 40) = 0LL;
*(_QWORD *)(current() + 48) = 0LL;
*(_QWORD *)(current() + 56) = 0LL;
*(_QWORD *)(current() + 64) = 0LL;
v0 = current();
*(_QWORD *)(v0 + 72) = get_ticks();
*(_QWORD *)(current() + 80) = 0LL;
result = current();
*(_QWORD *)(result + 88) = 0LL;
return result;
}

original_code:
void reset_stats(void){
current()->st.user_ticks = 0;
current()->st.system_ticks = 0;
current()->st.blocked_ticks = 0;
current()->st.ready_ticks = 0;
current()->st.elapsed_total_ticks=get_ticks();
current()->st.total_trans = 0;
current()->st.remaining_ticks = 0;
}

canonical_original_code:
void reset_stats(){
current()->st.user_ticks = 0;
current()->st.system_ticks = 0;
current()->st.blocked_ticks = 0;
current()->st.ready_ticks = 0;
current()->st.elapsed_total_ticks=get_ticks();
current()->st.total_trans = 0;
current()->st.remaining_ticks = 0;
}


decompiled_code:
__int64 init_task1()
{
int v0; // eax
__int64 v1; // rax
__int64 result; // rax

v0 = nextPID;
task[2048] = nextPID;
nextPID = v0 + 1;
allocate_DIR((unsigned int)((_DWORD)task + 0x2000));
set_user_pages((unsigned int)((_DWORD)task + 0x2000));
tss[1] = &task[4096];
set_cr3(*(_QWORD *)&task[2050]);
set_quantum((unsigned int)((_DWORD)task + 0x2000), 18);
current_quantum = get_quantum((unsigned int)((_DWORD)task + 0x2000));
reset_stats();
v1 = current();
result = ready_to_run((_QWORD *)(v1 + 40));
task[2072] = 0;
return result;
}

canonical_decompiled_code:
__int64 func27()
{
int v0; // eax
__int64 v1; // rax
__int64 result; // rax

v0 = nextPID;
task[2048] = nextPID;
nextPID = v0 + 1;
func15((unsigned int)((_DWORD)task + 0x2000));
set_user_pages((unsigned int)((_DWORD)task + 0x2000));
tss[1] = &task[4096];
set_cr3(*(_QWORD *)&task[2050]);
func18((unsigned int)((_DWORD)task + 0x2000), 18);
current_quantum = func17((unsigned int)((_DWORD)task + 0x2000));
func26();
v1 = func21();
result = func11((_QWORD *)(v1 + 40));
task[2072] = 0;
return result;
}

original_code:
void init_task1(void)
{
task[1].task.PID = nextPID;
nextPID++;
allocate_DIR(&task[1].task);
set_user_pages(&task[1].task);
tss.esp0=(DWord)&task[1].stack[1024];
set_cr3(task[1].task.dir_pages_baseAddr);
set_quantum(&task[1].task,18);
current_quantum=get_quantum(&task[1].task);
reset_stats();
ready_to_run(&current()->st);
task[1].task.estat = ST_RUN;
}

canonical_original_code:
void init_task1()
{
task[1].task.PID = nextPID;
nextPID++;
allocate_DIR(&task[1].task);
set_user_pages(&task[1].task);
tss.esp0=(DWord)&task[1].stack[1024];
set_cr3(task[1].task.dir_pages_baseAddr);
set_quantum(&task[1].task,18);
current_quantum=get_quantum(&task[1].task);
reset_stats();
ready_to_run(&current()->st);
task[1].task.estat = ST_RUN;
}


die

decompiled_code:
void __noreturn die(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void die(const char * str, ...)
{
 va_list args;
 __builtin_va_start(args,str);
 vfprintf(stderr, str, args);
 fputc('\n', stderr);
 exit(1);
}

canonical_original_code:
void die(char *str, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,str);
 vfprintf(stderr, str, args);
 fputc('\n', stderr);
 exit(1);
}

variable_types:
str
char *


decompiled_code:
int __fastcall minix_open(const char *a1)
{
int v1; // eax
int result; // eax

v1 = open(a1, 0, 0LL);
fd = v1;
if ( v1 < 0 )
die("Unable to open `%s': %m", a1);
if ( read(v1, &buf, 0x20uLL) != 32 )
die("%s: Unable to read header", a1);
result = memcmp(&buf, hdr_1, 8uLL);
if ( result || qword_404108 )
die("%s: Non-Minix header", a1);
if ( qword_4040F8 )
die("%s: Illegal data segment", hdr_1);
if ( qword_404100 )
die("%s: Illegal bss segment", hdr_1);
if ( qword_404118 )
die("%s: Illegal symbol table", hdr_1);
return result;
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
int v1; // eax
int result; // eax

v1 = open(a1, 0, 0LL);
fd = v1;
if ( v1 < 0 )
func7("Unable to open `%s': %m", a1);
if ( read(v1, &buf, 0x20uLL) != 32 )
func7("%s: Unable to read header", a1);
result = memcmp(&buf, hdr_1, 8uLL);
if ( result || qword_404108 )
func7("%s: Non-Minix header", a1);
if ( qword_4040F8 )
func7("%s: Illegal data segment", hdr_1);
if ( qword_404100 )
func7("%s: Illegal bss segment", hdr_1);
if ( qword_404118 )
func7("%s: Illegal symbol table", hdr_1);
return result;
}

original_code:
void minix_open(const char *name)
{
 static byte hdr[] = { 0x01, 0x03, 0x10, 0x04, 0x20, 0x00, 0x00, 0x00 };
 static u32 *lb = (u32 *) buf;
 if ((fd = open(name, 00, 0)) < 0)
  die("Unable to open `%s': %m", name);
 if (read(fd, buf, 32) != 32)
  die("%s: Unable to read header", name);
 if (memcmp(buf, hdr, sizeof(hdr)) || lb[5])
  die("%s: Non-Minix header", name);
 if (lb[3])
  die("%s: Illegal data segment");
 if (lb[4])
  die("%s: Illegal bss segment");
 if (lb[7])
  die("%s: Illegal symbol table");
}

canonical_original_code:
void minix_open(char *name)
{
 static unsigned char hdr[] = { 0x01, 0x03, 0x10, 0x04, 0x20, 0x00, 0x00, 0x00 };
 static unsigned long *lb = (u32 *) buf;
 if ((fd = open(name, 00, 0)) < 0)
  die("Unable to open `%s': %m", name);
 if (read(fd, buf, 32) != 32)
  die("%s: Unable to read header", name);
 if (memcmp(buf, hdr, sizeof(hdr)) || lb[5])
  die("%s: Non-Minix header", name);
 if (lb[3])
  die("%s: Illegal data segment");
 if (lb[4])
  die("%s: Illegal bss segment");
 if (lb[7])
  die("%s: Illegal symbol table");
}

variable_types:
name
char *

variable_types:
hdr
unsigned char []

variable_types:
lb
unsigned long *


usage

decompiled_code:
void __noreturn usage()
{
die("Usage: build bootsect system user [> image]");
}

canonical_decompiled_code:
void __noreturn func9()
{
func7("Usage: build bootsect system user [> image]");
}

original_code:
void usage(void)
{
 die("Usage: build bootsect system user [> image]");
}

canonical_original_code:
void usage()
{
 die("Usage: build bootsect system user [> image]");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
int v4; // edi
unsigned int v5; // er15
unsigned int v6; // ebp
int v7; // ebx
int v8; // eax
int v9; // edi
unsigned int v10; // ebx
unsigned int v11; // ebp
int v12; // ebx
int v13; // eax
unsigned int v15; // [rsp+8h] [rbp-D0h]
struct stat stat_buf; // [rsp+10h] [rbp-C8h] BYREF

if ( argc != 4 )
usage(argc, argv, envp);
minix_open(argv[1]);
v3 = read(fd, &buf, 0x400uLL);
fprintf(stderr, "Boot sector %d bytes.\n", v3);
if ( v3 != 512 )
die("Boot block must be exactly 512 bytes");
if ( byte_4042DE != 85 || byte_4042DF != -86 )
die("Boot block hasn't got boot flag (0xAA55)");
byte_4042DC = 0;
byte_4042DD = 0;
if ( write(1, &buf, 0x200uLL) != 512 )
die("Write call failed");
close(fd);
v4 = open(argv[2], 0, 0LL);
fd = v4;
if ( v4 < 0 )
die("Unable to open `%s': %m", argv[2]);
if ( (unsigned int)fstat(v4, &stat_buf) )
die("Unable to stat `%s': %m", argv[2]);
v5 = stat_buf.st_size;
fprintf(stderr, "System is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v5 )
{
v6 = v5;
do
{
v7 = 1024;
if ( v6 <= 0x400 )
v7 = v6;
v8 = read(fd, &buf, v7);
if ( v7 != v8 )
{
if ( v8 >= 0 )
die("%s: Unexpected EOF", argv[2]);
die("Error reading %s: %m", argv[2]);
}
if ( write(1, &buf, v7) != v7 )
die("Write failed");
v6 -= v7;
}
while ( v6 );
}
close(fd);
v9 = open(argv[3], 0, 0LL);
fd = v9;
if ( v9 < 0 )
die("Unable to open `%s': %m", argv[3]);
if ( (unsigned int)fstat(v9, &stat_buf) )
die("Unable to stat `%s': %m", argv[3]);
v10 = stat_buf.st_size;
v15 = stat_buf.st_size;
fprintf(stderr, "User is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v10 )
{
v11 = v10;
do
{
v12 = 1024;
if ( v11 <= 0x400 )
v12 = v11;
v13 = read(fd, &buf, v12);
if ( v12 != v13 )
{
if ( v13 >= 0 )
die("%s: Unexpected EOF", argv[3]);
die("Error reading %s: %m", argv[3]);
}
if ( write(1, &buf, v12) != v12 )
die("Write failed");
v11 -= v12;
}
while ( v11 );
}
close(fd);
if ( lseek(1, 500LL, 0) != 500 )
die("Output: seek failed");
fprintf(stderr, "Image is %d kB\n", (unsigned int)((int)(v5 + v15) / 1024));
LOBYTE(buf) = (v5 + (unsigned __int64)v15 + 15) >> 4;
HIBYTE(buf) = (v5 + (unsigned __int64)v15 + 15) >> 12;
if ( write(1, &buf, 2uLL) != 2 )
die("Write of image length failed");
if ( lseek(1, 512LL, 0) != 512 )
die("Output: seek failed");
buf = v5;
byte_4040E2 = BYTE2(v5);
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
die("Write of system length failed");
if ( lseek(1, 516LL, 0) != 516 )
die("Output: seek failed");
buf = v15;
byte_4040E2 = BYTE2(v15);
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
die("Write of user length failed");
if ( lseek(1, 520LL, 0) != 520 )
die("Output: seek failed");
buf = 32218;
byte_4040E2 = 0;
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
die("Write of user length failed");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
int v4; // edi
unsigned int v5; // er15
unsigned int v6; // ebp
int v7; // ebx
int v8; // eax
int v9; // edi
unsigned int v10; // ebx
unsigned int v11; // ebp
int v12; // ebx
int v13; // eax
unsigned int v15; // [rsp+8h] [rbp-D0h]
struct stat stat_buf; // [rsp+10h] [rbp-C8h] BYREF

if ( argc != 4 )
func9(argc, argv, envp);
func8(argv[1]);
v3 = read(fd, &buf, 0x400uLL);
fprintf(stderr, "Boot sector %d bytes.\n", v3);
if ( v3 != 512 )
func7("Boot block must be exactly 512 bytes");
if ( byte_4042DE != 85 || byte_4042DF != -86 )
func7("Boot block hasn't got boot flag (0xAA55)");
byte_4042DC = 0;
byte_4042DD = 0;
if ( write(1, &buf, 0x200uLL) != 512 )
func7("Write call failed");
close(fd);
v4 = open(argv[2], 0, 0LL);
fd = v4;
if ( v4 < 0 )
func7("Unable to open `%s': %m", argv[2]);
if ( (unsigned int)func12(v4, &stat_buf) )
func7("Unable to stat `%s': %m", argv[2]);
v5 = stat_buf.st_size;
fprintf(stderr, "System is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v5 )
{
v6 = v5;
do
{
v7 = 1024;
if ( v6 <= 0x400 )
v7 = v6;
v8 = read(fd, &buf, v7);
if ( v7 != v8 )
{
if ( v8 >= 0 )
func7("%s: Unexpected EOF", argv[2]);
func7("Error reading %s: %m", argv[2]);
}
if ( write(1, &buf, v7) != v7 )
func7("Write failed");
v6 -= v7;
}
while ( v6 );
}
close(fd);
v9 = open(argv[3], 0, 0LL);
fd = v9;
if ( v9 < 0 )
func7("Unable to open `%s': %m", argv[3]);
if ( (unsigned int)func12(v9, &stat_buf) )
func7("Unable to stat `%s': %m", argv[3]);
v10 = stat_buf.st_size;
v15 = stat_buf.st_size;
fprintf(stderr, "User is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v10 )
{
v11 = v10;
do
{
v12 = 1024;
if ( v11 <= 0x400 )
v12 = v11;
v13 = read(fd, &buf, v12);
if ( v12 != v13 )
{
if ( v13 >= 0 )
func7("%s: Unexpected EOF", argv[3]);
func7("Error reading %s: %m", argv[3]);
}
if ( write(1, &buf, v12) != v12 )
func7("Write failed");
v11 -= v12;
}
while ( v11 );
}
close(fd);
if ( lseek(1, 500LL, 0) != 500 )
func7("Output: seek failed");
fprintf(stderr, "Image is %d kB\n", (unsigned int)((int)(v5 + v15) / 1024));
LOBYTE(buf) = (v5 + (unsigned __int64)v15 + 15) >> 4;
HIBYTE(buf) = (v5 + (unsigned __int64)v15 + 15) >> 12;
if ( write(1, &buf, 2uLL) != 2 )
func7("Write of image length failed");
if ( lseek(1, 512LL, 0) != 512 )
func7("Output: seek failed");
buf = v5;
byte_4040E2 = BYTE2(v5);
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
func7("Write of system length failed");
if ( lseek(1, 516LL, 0) != 516 )
func7("Output: seek failed");
buf = v15;
byte_4040E2 = BYTE2(v15);
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
func7("Write of user length failed");
if ( lseek(1, 520LL, 0) != 520 )
func7("Output: seek failed");
buf = 32218;
byte_4040E2 = 0;
byte_4040E3 = 0;
if ( write(1, &buf, 4uLL) != 4 )
func7("Write of user length failed");
return 0;
}

original_code:
int main(int argc, char ** argv)
{
 unsigned int i, sz, uz;
 u32 im_size, sys_size, usr_size;
 byte major_root, minor_root;
 struct stat sb;
 if (argc != 4)
  usage();
 else {
  major_root = 0;
  minor_root = 0;
 }
 minix_open(argv[1]);
 i = read(fd, buf, sizeof(buf));
 fprintf(stderr,"Boot sector %d bytes.\n",i);
 if (i != 512)
  die("Boot block must be exactly 512 bytes");
 if (buf[510] != 0x55 || buf[511] != 0xaa)
  die("Boot block hasn't got boot flag (0xAA55)");
 buf[508] = minor_root;
 buf[509] = major_root;
 if (write(1, buf, 512) != 512)
  die("Write call failed");
 close (fd);
 if ((fd = open(argv[2], 00, 0)) < 0)
  die("Unable to open `%s': %m", argv[2]);
 if (fstat (fd, &sb))
  die("Unable to stat `%s': %m", argv[2]);
 sys_size = sz = sb.st_size;
 fprintf (stderr, "System is %d kB\n", sz/1024);
 while (sz > 0) {
  int l, n;
  l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
  if ((n=read(fd, buf, l)) != l) {
   if (n < 0)
    die("Error reading %s: %m", argv[2]);
   else
    die("%s: Unexpected EOF", argv[2]);
  }
  if (write(1, buf, l) != l)
   die("Write failed");
  sz -= l;
 }
 close(fd);
 if ((fd = open(argv[3], 00, 0)) < 0)
  die("Unable to open `%s': %m", argv[3]);
 if (fstat (fd, &sb))
  die("Unable to stat `%s': %m", argv[3]);
 usr_size = uz = sb.st_size;
 fprintf (stderr, "User is %d kB\n", uz/1024);
 while (uz > 0) {
  int l, n;
  l = (uz > sizeof(buf)) ? sizeof(buf) : uz;
  if ((n=read(fd, buf, l)) != l) {
   if (n < 0)
    die("Error reading %s: %m", argv[3]);
   else
    die("%s: Unexpected EOF", argv[3]);
  }
  if (write(1, buf, l) != l)
   die("Write failed");
  uz -= l;
 }
 close(fd);
 if (lseek(1, 500, 0) != 500)
  die("Output: seek failed");
 im_size = (sys_size + usr_size + 15) / 16;
 fprintf (stderr, "Image is %d kB\n", (int)(sys_size + usr_size)/1024);
 buf[0] = (im_size & 0xff);
 buf[1] = ((im_size >> 8) & 0xff);
 if (write(1, buf, 2) != 2)
  die("Write of image length failed");
      if (lseek(1, 512, 0) != 512)
   die("Output: seek failed");
 buf[0] = (sys_size & 0xff);
 buf[1] = ((sys_size >> 8) & 0xff);
 buf[2] = ((sys_size >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of system length failed");
      if (lseek(1, 516, 0) != 516)
   die("Output: seek failed");
 buf[0] = (usr_size & 0xff);
 buf[1] = ((usr_size >> 8) & 0xff);
 buf[2] = ((usr_size >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of user length failed");
      if (lseek(1, 520, 0) != 520)
   die("Output: seek failed");
 buf[0] = (0x7DDA & 0xff);
 buf[1] = ((0x7DDA >> 8) & 0xff);
 buf[2] = ((0x7DDA >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of user length failed");
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 unsigned int i; unsigned int sz; unsigned int uz;
 unsigned long im_size; unsigned long sys_size; unsigned long usr_size;
 unsigned char major_root; unsigned char minor_root;
 struct stat sb;
 if (argc != 4)
  usage();
 else {
  major_root = 0;
  minor_root = 0;
 }
 minix_open(argv[1]);
 i = read(fd, buf, sizeof(buf));
 fprintf(stderr,"Boot sector %d bytes.\n",i);
 if (i != 512)
  die("Boot block must be exactly 512 bytes");
 if (buf[510] != 0x55 || buf[511] != 0xaa)
  die("Boot block hasn't got boot flag (0xAA55)");
 buf[508] = minor_root;
 buf[509] = major_root;
 if (write(1, buf, 512) != 512)
  die("Write call failed");
 close (fd);
 if ((fd = open(argv[2], 00, 0)) < 0)
  die("Unable to open `%s': %m", argv[2]);
 if (fstat (fd, &sb))
  die("Unable to stat `%s': %m", argv[2]);
 sys_size = sz = sb.st_size;
 fprintf (stderr, "System is %d kB\n", sz/1024);
 while (sz > 0) {
  int l; int n;
  l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
  if ((n=read(fd, buf, l)) != l) {
   if (n < 0)
    die("Error reading %s: %m", argv[2]);
   else
    die("%s: Unexpected EOF", argv[2]);
  }
  if (write(1, buf, l) != l)
   die("Write failed");
  sz -= l;
 }
 close(fd);
 if ((fd = open(argv[3], 00, 0)) < 0)
  die("Unable to open `%s': %m", argv[3]);
 if (fstat (fd, &sb))
  die("Unable to stat `%s': %m", argv[3]);
 usr_size = uz = sb.st_size;
 fprintf (stderr, "User is %d kB\n", uz/1024);
 while (uz > 0) {
  int l; int n;
  l = (uz > sizeof(buf)) ? sizeof(buf) : uz;
  if ((n=read(fd, buf, l)) != l) {
   if (n < 0)
    die("Error reading %s: %m", argv[3]);
   else
    die("%s: Unexpected EOF", argv[3]);
  }
  if (write(1, buf, l) != l)
   die("Write failed");
  uz -= l;
 }
 close(fd);
 if (lseek(1, 500, 0) != 500)
  die("Output: seek failed");
 im_size = (sys_size + usr_size + 15) / 16;
 fprintf (stderr, "Image is %d kB\n", (int)(sys_size + usr_size)/1024);
 buf[0] = (im_size & 0xff);
 buf[1] = ((im_size >> 8) & 0xff);
 if (write(1, buf, 2) != 2)
  die("Write of image length failed");
      if (lseek(1, 512, 0) != 512)
   die("Output: seek failed");
 buf[0] = (sys_size & 0xff);
 buf[1] = ((sys_size >> 8) & 0xff);
 buf[2] = ((sys_size >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of system length failed");
      if (lseek(1, 516, 0) != 516)
   die("Output: seek failed");
 buf[0] = (usr_size & 0xff);
 buf[1] = ((usr_size >> 8) & 0xff);
 buf[2] = ((usr_size >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of user length failed");
      if (lseek(1, 520, 0) != 520)
   die("Output: seek failed");
 buf[0] = (0x7DDA & 0xff);
 buf[1] = ((0x7DDA >> 8) & 0xff);
 buf[2] = ((0x7DDA >> 16) & 0xff);
 buf[3] = 0;
 if (write(1, buf, 4) != 4)
   die("Write of user length failed");
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
i
unsigned int

variable_types:
sz
unsigned int

variable_types:
uz
unsigned int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: l int

variable_types: n int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


perror

decompiled_code:
ssize_t perror()
{
const char *v0; // rbx
unsigned int v1; // eax

v0 = "Error de parametres\n";
if ( errno != 22 )
{
v0 = "Ha anat be";
if ( errno == 11 )
v0 = "Torna-ho a provar\n";
}
v1 = strlen(v0);
return write(1, v0, v1);
}

canonical_decompiled_code:
ssize_t func0()
{
const char *v0; // rbx
unsigned int v1; // eax

v0 = "Error de parametres\n";
if ( errno != 22 )
{
v0 = "Ha anat be";
if ( errno == 11 )
v0 = "Torna-ho a provar\n";
}
v1 = strlen(v0);
return write(1, v0, v1);
}

original_code:
void perror() {
 char *str="Ha anat be";
 if (errno==22) str="Error de parametres\n";
 if (errno==11) str="Torna-ho a provar\n";
 write(1,str,strlen(str));
}

canonical_original_code:
void perror() {
 char *str = "Ha anat be";
 if (errno==22) str="Error de parametres\n";
 if (errno==11) str="Torna-ho a provar\n";
 write(1,str,strlen(str));
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
runjp(argc, argv, envp);
while ( 1 )
;
}

original_code:
int __attribute__ ((__section__(".text.main")))
 main(void)
{
 runjp();
 while(1){
 }
 return 0;
}

canonical_original_code:
int main()
{
 runjp();
 while(1){
 }
 return 0;
}


decompiled_code:
__int64 __fastcall sys_write_console(char *a1, int a2)
{
char *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printc((unsigned int)*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return (unsigned int)a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, int a2)
{
char *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printc((unsigned int)*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return (unsigned int)a2;
}

original_code:
int sys_write_console(char *buffer,int size)
{
  int i;
  for (i=0; i<size; i++)
    printc(buffer[i]);
  return size;
}

canonical_original_code:
int sys_write_console(char *buffer, int size)
{
  int i;
  for (i=0; i<size; i++)
    printc(buffer[i]);
  return size;
}

variable_types:
buffer
char *

variable_types: size int


repo: jordonbiondo/echo-zebra

decompiled_code:
void __noreturn clean_and_exit()
{
shmctl(shmId, 0, 0LL);
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
shmctl(shmId, 0, 0LL);
exit(0);
}

original_code:
void clean_and_exit() {
  shmctl(shmId, 0, 0);
  exit(0);
}


decompiled_code:
void __fastcall signal_handler(int a1)
{
if ( a1 == 12 )
{
if ( DEBUG )
puts("new connection!");
++connected_readers;
}
else
{
if ( a1 <= 12 )
{
if ( a1 == 2 )
{
if ( DEBUG )
puts("got sigint");
clean_and_exit();
}
if ( a1 == 10 )
return;
LABEL_18:
printf("Sig handled when it shouldn't be: %d\n", (unsigned int)a1);
return;
}
if ( a1 != 14 )
goto LABEL_18;
if ( DEBUG )
puts("hmmm, maybe a reader died...");
if ( !--connected_readers && DEBUG )
puts("AAALLLL BYYYY MYSEEEEEEEEELF");
}
}

canonical_decompiled_code:
void __fastcall func8(int a1)
{
if ( a1 == 12 )
{
if ( DEBUG )
puts("new connection!");
++connected_readers;
}
else
{
if ( a1 <= 12 )
{
if ( a1 == 2 )
{
if ( DEBUG )
puts("got sigint");
func7();
}
if ( a1 == 10 )
return;
LABEL_18:
printf("Sig handled when it shouldn't be: %d\n", (unsigned int)a1);
return;
}
if ( a1 != 14 )
goto LABEL_18;
if ( DEBUG )
puts("hmmm, maybe a reader died...");
if ( !--connected_readers && DEBUG )
puts("AAALLLL BYYYY MYSEEEEEEEEELF");
}
}

original_code:
void signal_handler(int sig) {
  switch(sig) {
  case 14: {
    if (DEBUG) printf("hmmm, maybe a reader died...\n");
    connected_readers--;
    if (connected_readers == 0 && DEBUG) {
      printf("AAALLLL BYYYY MYSEEEEEEEEELF\n");
    }
    break;
  }
  case 10: {
    break;
  }
  case 12: {
    if (DEBUG) printf("new connection!\n");
    connected_readers ++;
    break;
  }
  case 2: {
    if (DEBUG) printf("got sigint\n");
    clean_and_exit();
    break;
  }
  default: {
    printf("Sig handled when it shouldn't be: %d\n", sig);
    return;
  }
  }
}

variable_types: sig int


decompiled_code:
__sighandler_t set_sig_handlers()
{
signal(2, signal_handler);
signal(10, signal_handler);
signal(12, signal_handler);
return signal(14, signal_handler);
}

canonical_decompiled_code:
__sighandler_t func9()
{
signal(2, signal_handler);
signal(10, signal_handler);
signal(12, signal_handler);
return signal(14, signal_handler);
}

original_code:
void set_sig_handlers() {
  signal(2, signal_handler);
  signal(10, signal_handler);
  signal(12, signal_handler);
  signal(14, signal_handler);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
bool v3; // al
key_t v4; // ebx
__pid_t v5; // ebp
int v6; // eax
int v7; // eax

v3 = 0;
if ( argc > 1 )
v3 = strcmp(argv[1], "-d") == 0;
DEBUG = v3;
set_sig_handlers();
v4 = ftok("shared_file.txt", 0);
v5 = getpid();
v6 = shmget(v4, 0x400uLL, 896);
shmId = v6;
if ( v6 < 0 )
{
puts("Failed get");
exit(-1);
}
sharedPtr = (__int64)shmat(v6, 0LL, 0);
*(_DWORD *)sharedPtr = v5;
*(_DWORD *)(sharedPtr + 4) = 0;
while ( 1 )
{
if ( DEBUG )
printf("read: %d/%d\n", *(unsigned int *)(sharedPtr + 8), (unsigned int)connected_readers);
v7 = *(_DWORD *)(sharedPtr + 8);
if ( v7 <= connected_readers )
{
if ( v7 >= connected_readers )
goto LABEL_14;
if ( DEBUG )
puts("pausin!");
alarm(4u);
pause();
alarm(0);
}
else
{
connected_readers = *(_DWORD *)(sharedPtr + 8);
LABEL_14:
fgets((char *)(sharedPtr + 12), 1012, _bss_start);
++*(_DWORD *)(sharedPtr + 4);
*(_DWORD *)(sharedPtr + 8) = 0;
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
bool v3; // al
key_t v4; // ebx
__pid_t v5; // ebp
int v6; // eax
int v7; // eax

v3 = 0;
if ( argc > 1 )
v3 = strcmp(argv[1], "-d") == 0;
DEBUG = v3;
func9();
v4 = ftok("shared_file.txt", 0);
v5 = getpid();
v6 = shmget(v4, 0x400uLL, 896);
shmId = v6;
if ( v6 < 0 )
{
puts("Failed get");
exit(-1);
}
sharedPtr = (__int64)shmat(v6, 0LL, 0);
*(_DWORD *)sharedPtr = v5;
*(_DWORD *)(sharedPtr + 4) = 0;
while ( 1 )
{
if ( DEBUG )
printf("read: %d/%d\n", *(unsigned int *)(sharedPtr + 8), (unsigned int)connected_readers);
v7 = *(_DWORD *)(sharedPtr + 8);
if ( v7 <= connected_readers )
{
if ( v7 >= connected_readers )
goto LABEL_14;
if ( DEBUG )
puts("pausin!");
alarm(4u);
pause();
alarm(0);
}
else
{
connected_readers = *(_DWORD *)(sharedPtr + 8);
LABEL_14:
fgets((char *)(sharedPtr + 12), 1012, _bss_start);
++*(_DWORD *)(sharedPtr + 4);
*(_DWORD *)(sharedPtr + 8) = 0;
}
}
}

original_code:
int main(int argc, char* argv[]) {
  DEBUG = (argc > 1 && strcmp(argv[1], "-d") == 0);
  set_sig_handlers();
  key_t mem_key = ftok("shared_file.txt", 0);
  pid_t my_pid = getpid();
  shmId = shmget(mem_key , (1024), 01000|0400|0200);
  if (shmId < 0) {
    printf("Failed get\n");
    exit(-1);
  }
  sharedPtr = shmat(shmId, (void*)0, 0);
  if (sharedPtr < 0) {
    printf("Failed attach :(\n");
    clean_and_exit();
    exit(-1);
  }
  ((int*)sharedPtr)[0] = my_pid;
  ((int*)sharedPtr)[1] = 0;
  while(1) {
    if(DEBUG) printf("read: %d/%d\n", ((int*)sharedPtr)[2], connected_readers);
    if (((int*)sharedPtr)[2] > connected_readers)
      connected_readers = ((int*)sharedPtr)[2];
    if (((int*)sharedPtr)[2] < connected_readers) {
      if (DEBUG) printf("pausin!\n");
      alarm(4);
      pause();
      alarm(0);
    } else {
      fgets((sharedPtr + sizeof(pid_t) + sizeof(int) + sizeof(int)), (1024 - sizeof(pid_t) - (sizeof(int) * 2)), stdin);
      ((int*)sharedPtr)[1] += 1;
      ((int*)sharedPtr)[2] = 0;
    }
  }
  clean_and_exit();
  return -1;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  DEBUG = (argc > 1 && strcmp(argv[1], "-d") == 0);
  set_sig_handlers();
  int mem_key = ftok("shared_file.txt", 0);
  int my_pid = getpid();
  shmId = shmget(mem_key , (1024), 01000|0400|0200);
  if (shmId < 0) {
    printf("Failed get\n");
    exit(-1);
  }
  sharedPtr = shmat(shmId, (void *)0, 0);
  if (sharedPtr < 0) {
    printf("Failed attach :(\n");
    clean_and_exit();
    exit(-1);
  }
  ((int *)sharedPtr)[0] = my_pid;
  ((int *)sharedPtr)[1] = 0;
  while(1) {
    if(DEBUG) printf("read: %d/%d\n", ((int *)sharedPtr)[2], connected_readers);
    if (((int *)sharedPtr)[2] > connected_readers)
      connected_readers = ((int *)sharedPtr)[2];
    if (((int *)sharedPtr)[2] < connected_readers) {
      if (DEBUG) printf("pausin!\n");
      alarm(4);
      pause();
      alarm(0);
    } else {
      fgets((sharedPtr + sizeof(pid_t) + sizeof(int) + sizeof(int)), (1024 - sizeof(pid_t) - (sizeof(int) * 2)), stdin);
      ((int *)sharedPtr)[1] += 1;
      ((int *)sharedPtr)[2] = 0;
    }
  }
  clean_and_exit();
  return -1;
}

variable_types:
argv
char *[]


repo: jorisvink/kore

decompiled_code:
unsigned __int64 __fastcall kore_buf_init(__int64 a1, __int64 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-20h]

v3 = __readfsqword(0x28u);
if ( a2 )
*(_QWORD *)a1 = kore_malloc(a2);
else
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 24) = 0LL;
*(_DWORD *)(a1 + 8) = 0;
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-20h]

v3 = __readfsqword(0x28u);
if ( a2 )
*(_QWORD *)a1 = kore_malloc(a2);
else
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 24) = 0LL;
*(_DWORD *)(a1 + 8) = 0;
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_init(struct kore_buf *buf, size_t initial)
{
 if (initial > 0)
  buf->data = kore_malloc(initial);
 else
  buf->data = ((void *)0);
 buf->length = initial;
 buf->offset = 0;
 buf->flags = 0;
}

canonical_original_code:
void kore_buf_init(struct kore_buf *buf, unsigned long initial)
{
 if (initial > 0)
  buf->data = kore_malloc(initial);
 else
  buf->data = ((void *)0);
 buf->length = initial;
 buf->offset = 0;
 buf->flags = 0;
}

variable_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; } *

variable_types:
initial
unsigned long


decompiled_code:
__int64 __fastcall kore_buf_alloc(__int64 a1)
{
__int64 v1; // rbx

v1 = kore_malloc(32LL);
kore_buf_init(v1, a1);
*(_DWORD *)(v1 + 8) = 1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rbx

v1 = kore_malloc(32LL);
func0(v1, a1);
*(_DWORD *)(v1 + 8) = 1;
return v1;
}

original_code:
struct kore_buf *
kore_buf_alloc(size_t initial)
{
 struct kore_buf *buf;
 buf = kore_malloc(sizeof(*buf));
 kore_buf_init(buf, initial);
 buf->flags = 0x0001;
 return (buf);
}

canonical_original_code:
struct kore_buf *kore_buf_alloc(unsigned long initial)
{
 struct kore_buf *buf;
 buf = kore_malloc(sizeof(*buf));
 kore_buf_init(buf, initial);
 buf->flags = 0x0001;
 return (buf);
}

return_type: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; } *


decompiled_code:
unsigned __int64 __fastcall kore_buf_cleanup(_QWORD *a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
kore_free(*a1);
*a1 = 0LL;
a1[3] = 0LL;
a1[2] = 0LL;
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
kore_free(*a1);
*a1 = 0LL;
a1[3] = 0LL;
a1[2] = 0LL;
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_cleanup(struct kore_buf *buf)
{
 kore_free(buf->data);
 buf->data = ((void *)0);
 buf->offset = 0;
 buf->length = 0;
}

user_defined_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; }


decompiled_code:
unsigned __int64 __fastcall kore_buf_free(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
kore_buf_cleanup((_QWORD *)a1);
if ( (*(_BYTE *)(a1 + 8) & 1) != 0 )
kore_free(a1);
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
func2((_QWORD *)a1);
if ( (*(_BYTE *)(a1 + 8) & 1) != 0 )
kore_free(a1);
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_free(struct kore_buf *buf)
{
 kore_buf_cleanup(buf);
 if (buf->flags & 0x0001)
  kore_free(buf);
}


decompiled_code:
unsigned __int64 __fastcall kore_buf_append(const char *a1, char *a2, size_t a3)
{
char *v3; // rbx
const void *v5; // r12
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
if ( __CFADD__(*((_QWORD *)a1 + 3), a3) )
{
a1 = "overflow in kore_buf_append";
fatal("overflow in kore_buf_append");
goto LABEL_5;
}
v3 = (char *)a1;
v5 = a2;
a2 = (char *)*((_QWORD *)a1 + 2);
if ( *((_QWORD *)a1 + 3) + a3 > (unsigned __int64)a2 )
{
LABEL_5:
*((_QWORD *)a1 + 2) = &a2[a3];
*(_QWORD *)v3 = kore_realloc(*(_QWORD *)a1);
}
memcpy((void *)(*(_QWORD *)v3 + *((_QWORD *)v3 + 3)), v5, a3);
*((_QWORD *)v3 + 3) += a3;
return v7 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(const char *a1, char *a2, size_t a3)
{
char *v3; // rbx
const void *v5; // r12
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
if ( __CFADD__(*((_QWORD *)a1 + 3), a3) )
{
a1 = "overflow in kore_buf_append";
fatal("overflow in kore_buf_append");
goto LABEL_5;
}
v3 = (char *)a1;
v5 = a2;
a2 = (char *)*((_QWORD *)a1 + 2);
if ( *((_QWORD *)a1 + 3) + a3 > (unsigned __int64)a2 )
{
LABEL_5:
*((_QWORD *)a1 + 2) = &a2[a3];
*(_QWORD *)v3 = kore_realloc(*(_QWORD *)a1);
}
memcpy((void *)(*(_QWORD *)v3 + *((_QWORD *)v3 + 3)), v5, a3);
*((_QWORD *)v3 + 3) += a3;
return v7 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_append(struct kore_buf *buf, const void *data, size_t len)
{
 if ((buf->offset + len) < len)
  fatal("overflow in kore_buf_append");
 if ((buf->offset + len) > buf->length) {
  buf->length += len;
  buf->data = kore_realloc(buf->data, buf->length);
 }
 memcpy((buf->data + buf->offset), data, len);
 buf->offset += len;
}

canonical_original_code:
void kore_buf_append(struct kore_buf *buf, void *data, unsigned long len)
{
 if ((buf->offset + len) < len)
  fatal("overflow in kore_buf_append");
 if ((buf->offset + len) > buf->length) {
  buf->length += len;
  buf->data = kore_realloc(buf->data, buf->length);
 }
 memcpy((buf->data + buf->offset), data, len);
 buf->offset += len;
}

variable_types:
data
void *

variable_types:
len
unsigned long


decompiled_code:
unsigned __int64 __fastcall kore_buf_appendv(const char *a1, __int64 a2, const __m128i *a3)
{
unsigned int v3; // eax
void *ptr; // [rsp+0h] [rbp-2048h] BYREF
__m128i v6; // [rsp+8h] [rbp-2040h] BYREF
__int64 v7; // [rsp+18h] [rbp-2030h]
char v8[8200]; // [rsp+20h] [rbp-2028h] BYREF
unsigned __int64 v9; // [rsp+2028h] [rbp-20h]

v9 = __readfsqword(0x28u);
v6 = _mm_loadu_si128(a3);
v7 = a3[1].m128i_i64[0];
v3 = _vsnprintf_chk(v8, 0x2000LL, 1LL, 0x2000LL, a2, a3);
if ( v3 == -1 )
{
fatal("kore_buf_appendv(): vsnprintf error");
}
else if ( v3 <= 0x1FFF )
{
ptr = v8;
goto LABEL_4;
}
v3 = _vasprintf_chk(&ptr, 1LL, a2, &v6);
if ( v3 == -1 )
fatal("kore_buf_appendv(): error or truncation");
LABEL_4:
kore_buf_append(a1, (char *)ptr, (int)v3);
if ( ptr != v8 )
free(ptr);
return v9 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(const char *a1, __int64 a2, const __m128i *a3)
{
unsigned int v3; // eax
void *ptr; // [rsp+0h] [rbp-2048h] BYREF
__m128i v6; // [rsp+8h] [rbp-2040h] BYREF
__int64 v7; // [rsp+18h] [rbp-2030h]
char v8[8200]; // [rsp+20h] [rbp-2028h] BYREF
unsigned __int64 v9; // [rsp+2028h] [rbp-20h]

v9 = __readfsqword(0x28u);
v6 = _mm_loadu_si128(a3);
v7 = a3[1].m128i_i64[0];
v3 = _vsnprintf_chk(v8, 0x2000LL, 1LL, 0x2000LL, a2, a3);
if ( v3 == -1 )
{
fatal("kore_buf_appendv(): vsnprintf error");
}
else if ( v3 <= 0x1FFF )
{
ptr = v8;
goto LABEL_4;
}
v3 = _vasprintf_chk(&ptr, 1LL, a2, &v6);
if ( v3 == -1 )
fatal("kore_buf_appendv(): error or truncation");
LABEL_4:
func4(a1, (char *)ptr, (int)v3);
if ( ptr != v8 )
free(ptr);
return v9 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args)
{
 int l;
 va_list copy;
 char *b, sb[8192];
 __builtin_va_copy(copy,args);
 l = vsnprintf(sb, sizeof(sb), fmt, args);
 if (l == -1)
  fatal("kore_buf_appendv(): vsnprintf error");
 if ((size_t)l >= sizeof(sb)) {
  l = vasprintf(&b, fmt, copy);
  if (l == -1)
   fatal("kore_buf_appendv(): error or truncation");
 } else {
  b = sb;
 }
 kore_buf_append(buf, b, l);
 if (b != sb)
  free(b);
 __builtin_va_end(copy);
}

canonical_original_code:
void kore_buf_appendv(struct kore_buf *buf, char *fmt, __builtin_va_list args)
{
 int l;
 __builtin_va_list copy;
 char *b; char sb[8192];
 __builtin_va_copy(copy,args);
 l = vsnprintf(sb, sizeof(sb), fmt, args);
 if (l == -1)
  fatal("kore_buf_appendv(): vsnprintf error");
 if ((unsigned long)l >= sizeof(sb)) {
  l = vasprintf(&b, fmt, copy);
  if (l == -1)
   fatal("kore_buf_appendv(): error or truncation");
 } else {
  b = sb;
 }
 kore_buf_append(buf, b, l);
 if (b != sb)
  free(b);
 __builtin_va_end(copy);
}

variable_types:
fmt
char *

variable_types:
b
char *

variable_types:
sb
char [8192]


decompiled_code:
unsigned __int64 kore_buf_appendf(const char *a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-D8h] BYREF
unsigned __int64 v4; // [rsp+18h] [rbp-C0h]

va_start(va, a2);
v4 = __readfsqword(0x28u);
kore_buf_appendv(a1, a2, (const __m128i *)va);
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func6(const char *a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-D8h] BYREF
unsigned __int64 v4; // [rsp+18h] [rbp-C0h]

va_start(va, a2);
v4 = __readfsqword(0x28u);
func5(a1, a2, (const __m128i *)va);
return v4 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_appendf(struct kore_buf *buf, const char *fmt, ...)
{
 va_list args;
 __builtin_va_start(args,fmt);
 kore_buf_appendv(buf, fmt, args);
 __builtin_va_end(args);
}

canonical_original_code:
void kore_buf_appendf(struct kore_buf *buf, char *fmt, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 kore_buf_appendv(buf, fmt, args);
 __builtin_va_end(args);
}


decompiled_code:
__int64 __fastcall kore_buf_stringify(__int64 a1, _QWORD *a2)
{
char v3; // [rsp+7h] [rbp-11h] BYREF
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
if ( a2 )
*a2 = *(_QWORD *)(a1 + 24);
v3 = 0;
kore_buf_append((const char *)a1, &v3, 1uLL);
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, _QWORD *a2)
{
char v3; // [rsp+7h] [rbp-11h] BYREF
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
if ( a2 )
*a2 = *(_QWORD *)(a1 + 24);
v3 = 0;
func4((const char *)a1, &v3, 1uLL);
return *(_QWORD *)a1;
}

original_code:
char *
kore_buf_stringify(struct kore_buf *buf, size_t *len)
{
 char c;
 if (len != ((void *)0))
  *len = buf->offset;
 c = '\0';
 kore_buf_append(buf, &c, sizeof(c));
 return ((char *)buf->data);
}

canonical_original_code:
char *kore_buf_stringify(struct kore_buf *buf, unsigned long *len)
{
 char c;
 if (len != ((void *)0))
  *len = buf->offset;
 c = '\0';
 kore_buf_append(buf, &c, sizeof(c));
 return ((char *)buf->data);
}

variable_types:
len
unsigned long *

variable_types: c char

return_type: char *


decompiled_code:
__int64 __fastcall kore_buf_release(__int64 *a1, _QWORD *a2)
{
__int64 v2; // rbx

v2 = *a1;
*a2 = a1[3];
*a1 = 0LL;
kore_buf_free((__int64)a1);
return v2;
}

original_code:
u_int8_t *
kore_buf_release(struct kore_buf *buf, size_t *len)
{
 u_int8_t *p;
 p = buf->data;
 *len = buf->offset;
 buf->data = ((void *)0);
 kore_buf_free(buf);
 return (p);
}

canonical_original_code:
unsigned int *kore_buf_release(struct kore_buf *buf, unsigned long *len)
{
 unsigned int *p;
 p = buf->data;
 *len = buf->offset;
 buf->data = ((void *)0);
 kore_buf_free(buf);
 return (p);
}

variable_types:
p
unsigned int *

return_type: unsigned int *


decompiled_code:
unsigned __int64 __fastcall kore_buf_replace_string(_QWORD *a1, const char *a2, const void *a3, size_t a4)
{
size_t v4; // rbx
__int64 v5; // rsi
size_t v6; // r15
const void *v7; // r14
__int64 v8; // rax
size_t v9; // rbx
size_t v10; // rbp
char *v11; // r13
void *src; // [rsp+10h] [rbp-68h]
size_t v16; // [rsp+20h] [rbp-58h]
unsigned __int64 v18; // [rsp+38h] [rbp-40h]

v18 = __readfsqword(0x28u);
v16 = strlen(a2);
v4 = 0LL;
while ( 1 )
{
v5 = a1[3];
v6 = v5 + a4;
v7 = (const void *)*a1;
v8 = kore_mem_find(*a1 + v4, v5 - v4, a2, v16);
if ( !v8 )
break;
src = (void *)(v16 + v8);
v9 = v8 - (_QWORD)v7;
v10 = *a1 + a1[3] - (v16 + v8);
v11 = (char *)kore_malloc(v6);
memcpy(v11, v7, v9);
if ( a3 )
memcpy(&v11[v9], a3, a4);
v4 = a4 + v9;
memcpy(&v11[v4], src, v10);
kore_free(*a1);
*a1 = v11;
a1[3] = v4 + v10;
a1[2] = v6;
}
return v18 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(_QWORD *a1, const char *a2, const void *a3, size_t a4)
{
size_t v4; // rbx
__int64 v5; // rsi
size_t v6; // r15
const void *v7; // r14
__int64 v8; // rax
size_t v9; // rbx
size_t v10; // rbp
char *v11; // r13
void *src; // [rsp+10h] [rbp-68h]
size_t v16; // [rsp+20h] [rbp-58h]
unsigned __int64 v18; // [rsp+38h] [rbp-40h]

v18 = __readfsqword(0x28u);
v16 = strlen(a2);
v4 = 0LL;
while ( 1 )
{
v5 = a1[3];
v6 = v5 + a4;
v7 = (const void *)*a1;
v8 = kore_mem_find(*a1 + v4, v5 - v4, a2, v16);
if ( !v8 )
break;
src = (void *)(v16 + v8);
v9 = v8 - (_QWORD)v7;
v10 = *a1 + a1[3] - (v16 + v8);
v11 = (char *)kore_malloc(v6);
memcpy(v11, v7, v9);
if ( a3 )
memcpy(&v11[v9], a3, a4);
v4 = a4 + v9;
memcpy(&v11[v4], src, v10);
kore_free(*a1);
*a1 = v11;
a1[3] = v4 + v10;
a1[2] = v6;
}
return v18 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_replace_string(struct kore_buf *b, const char *src,
    const void *dst, size_t len)
{
 char *key, *end, *tmp, *p;
 size_t blen, off, off2, nlen, klen;
 off = 0;
 klen = strlen(src);
 for (;;) {
  blen = b->offset;
  nlen = blen + len;
  p = (char *)b->data;
  key = kore_mem_find(p + off, b->offset - off, src, klen);
  if (key == ((void *)0))
   break;
  end = key + klen;
  off = key - p;
  off2 = ((char *)(b->data + b->offset) - end);
  tmp = kore_malloc(nlen);
  memcpy(tmp, p, off);
  if (dst != ((void *)0))
   memcpy((tmp + off), dst, len);
  memcpy((tmp + off + len), end, off2);
  kore_free(b->data);
  b->data = (u_int8_t *)tmp;
  b->offset = off + len + off2;
  b->length = nlen;
  off = off + len;
 }
}

canonical_original_code:
void kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, unsigned long len)
{
 char *key; char *end; char *tmp; char *p;
 unsigned long blen; unsigned long off; unsigned long off2; unsigned long nlen; unsigned long klen;
 off = 0;
 klen = strlen(src);
 for (;;) {
  blen = b->offset;
  nlen = blen + len;
  p = (char *)b->data;
  key = kore_mem_find(p + off, b->offset - off, src, klen);
  if (key == ((void *)0))
   break;
  end = key + klen;
  off = key - p;
  off2 = ((char *)(b->data + b->offset) - end);
  tmp = kore_malloc(nlen);
  memcpy(tmp, p, off);
  if (dst != ((void *)0))
   memcpy((tmp + off), dst, len);
  memcpy((tmp + off + len), end, off2);
  kore_free(b->data);
  b->data = (unsigned int *)tmp;
  b->offset = off + len + off2;
  b->length = nlen;
  off = off + len;
 }
}

variable_types:
src
char *

variable_types:
dst
void *

variable_types:
key
char *

variable_types:
end
char *

variable_types:
tmp
char *

variable_types:
p
char *

variable_types:
blen
unsigned long

variable_types:
off
unsigned long

variable_types:
nlen
unsigned long

variable_types:
klen
unsigned long


decompiled_code:
unsigned __int64 __fastcall kore_buf_reset(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 24) = 0LL;
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 24) = 0LL;
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_reset(struct kore_buf *buf)
{
 buf->offset = 0;
}

variable_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; } *


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

for ( result = msg_types; result; result = *(_QWORD *)(result + 16) )
{
if ( *(_BYTE *)result == a1 )
break;
}
return result;
}

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

for ( result = msg_types; result; result = *(_QWORD *)(result + 16) )
{
if ( *(_BYTE *)result == a1 )
break;
}
return result;
}

original_code:
static struct msg_type *
msg_type_lookup(u_int8_t id)
{
 struct msg_type *type;
 for ((type) = ((&msg_types)->tqh_first); (type); (type) = ((type)->list.tqe_next)) {
  if (type->id == id)
   return (type);
 }
 return (((void *)0));
}

canonical_original_code:
static struct msg_type *msg_type_lookup(unsigned int id)
{
 struct msg_type *type;
 for ((type) = ((&msg_types)->tqh_first); (type); (type) = ((type)->list.tqe_next)) {
  if (type->id == id)
   return (type);
 }
 return (((void *)0));
}

variable_types:
id
unsigned int

variable_types: struct msg_type { unsigned int id; void (*cb)(struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *, void *); struct { struct msg_type *tqe_next; struct msg_type **tqe_prev; } list; } *

user_defined_types: struct msg_type { unsigned int id; void (*cb)(struct kore_msg *, void *); struct { struct msg_type *tqe_next; struct msg_type **tqe_prev; } list; }


decompiled_code:
unsigned __int64 __fastcall msg_type_shutdown(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
if ( !kore_quiet )
kore_log(5LL, "shutdown requested by worker %u, going down", *(unsigned __int16 *)(a1 + 2));
kore_quit = *(_BYTE *)a1 == 13;
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
if ( !kore_quiet )
kore_log(5LL, "shutdown requested by worker %u, going down", *(unsigned __int16 *)(a1 + 2));
kore_quit = *(_BYTE *)a1 == 13;
return v2 - __readfsqword(0x28u);
}

original_code:
static void
msg_type_shutdown(struct kore_msg *msg, const void *data)
{
 if (!kore_quiet) {
  kore_log(5,
      "shutdown requested by worker %u, going down", msg->src);
 }
 if (msg->id == 13)
  kore_quit = 1;
 else
  kore_quit = 0;
}

canonical_original_code:
static void msg_type_shutdown(struct kore_msg *msg, void *data)
{
 if (!kore_quiet) {
  kore_log(5,
      "shutdown requested by worker %u, going down", msg->src);
 }
 if (msg->id == 13)
  kore_quit = 1;
 else
  kore_quit = 0;
}

variable_types: struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *

variable_types:
data
void *


decompiled_code:
unsigned __int64 kore_msg_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
msg_types = 0LL;
qword_838 = (__int64)&msg_types;
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func6()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
msg_types = 0LL;
qword_838 = (__int64)&msg_types;
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_msg_init(void)
{
 do { (&msg_types)->tqh_first = ((void *)0); (&msg_types)->tqh_last = &(&msg_types)->tqh_first; } while ( 0);
}

canonical_original_code:
void kore_msg_init()
{
 do { (&msg_types)->tqh_first = ((void *)0); (&msg_types)->tqh_last = &(&msg_types)->tqh_first; } while ( 0);
}


decompiled_code:
unsigned __int64 __fastcall kore_msg_unregister(char a1)
{
__int64 v1; // rax
__int64 v2; // rdi
__int64 v3; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
v1 = msg_type_lookup(a1);
if ( v1 )
{
v2 = v1;
v3 = *(_QWORD *)(v1 + 16);
if ( v3 )
*(_QWORD *)(v3 + 24) = *(_QWORD *)(v2 + 24);
else
qword_838 = *(_QWORD *)(v2 + 24);
**(_QWORD **)(v2 + 24) = *(_QWORD *)(v2 + 16);
kore_free();
}
return v5 - __readfsqword(0x28u);
}

original_code:
void
kore_msg_unregister(u_int8_t id)
{
 struct msg_type *type;
 if ((type = msg_type_lookup(id)) == ((void *)0))
  return;
 do { if (((type)->list.tqe_next) != ((void *)0)) (type)->list.tqe_next->list.tqe_prev = (type)->list.tqe_prev; else (&msg_types)->tqh_last = (type)->list.tqe_prev; *(type)->list.tqe_prev = (type)->list.tqe_next; } while ( 0);
 kore_free(type);
}

canonical_original_code:
void kore_msg_unregister(unsigned int id)
{
 struct msg_type *type;
 if ((type = msg_type_lookup(id)) == ((void *)0))
  return;
 do { if (((type)->list.tqe_next) != ((void *)0)) (type)->list.tqe_next->list.tqe_prev = (type)->list.tqe_prev; else (&msg_types)->tqh_last = (type)->list.tqe_prev; *(type)->list.tqe_prev = (type)->list.tqe_next; } while ( 0);
 kore_free(type);
}

user_defined_types: struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; }


decompiled_code:
__int64 __fastcall kore_msg_register(char a1, __int64 a2)
{
__int64 v2; // rdx
__int64 result; // rax
__int64 v4; // rax
__int64 *v5; // rdx

v2 = msg_type_lookup(a1);
result = 0LL;
if ( !v2 )
{
v4 = kore_malloc(32LL);
*(_BYTE *)v4 = a1;
*(_QWORD *)(v4 + 8) = a2;
*(_QWORD *)(v4 + 16) = 0LL;
v5 = (__int64 *)qword_838;
*(_QWORD *)(v4 + 24) = qword_838;
*v5 = v4;
qword_838 = v4 + 16;
result = 1LL;
}
return result;
}

original_code:
int
kore_msg_register(u_int8_t id, void (*cb)(struct kore_msg *, const void *))
{
 struct msg_type *type;
 if (msg_type_lookup(id) != ((void *)0))
  return (0);
 type = kore_malloc(sizeof(*type));
 type->id = id;
 type->cb = cb;
 do { (type)->list.tqe_next = ((void *)0); (type)->list.tqe_prev = (&msg_types)->tqh_last; *(&msg_types)->tqh_last = (type); (&msg_types)->tqh_last = &(type)->list.tqe_next; } while ( 0);
 return (1);
}

canonical_original_code:
int kore_msg_register(unsigned int id, void (*cb)(struct kore_msg *, void *))
{
 struct msg_type *type;
 if (msg_type_lookup(id) != ((void *)0))
  return (0);
 type = kore_malloc(sizeof(*type));
 type->id = id;
 type->cb = cb;
 do { (type)->list.tqe_next = ((void *)0); (type)->list.tqe_prev = (&msg_types)->tqh_last; *(&msg_types)->tqh_last = (type); (&msg_types)->tqh_last = &(type)->list.tqe_next; } while ( 0);
 return (1);
}

variable_types: void (*)(struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *, void *)


decompiled_code:
unsigned __int64 kore_msg_worker_init()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rax
_QWORD *v3; // rdx
__int64 v4; // rsi
unsigned __int64 v6; // [rsp+8h] [rbp-10h]

v6 = __readfsqword(0x28u);
kore_msg_register(1, (__int64)msg_type_websocket);
v0 = worker;
*(_QWORD *)(v0 + 40) = kore_connection_new(0LL);
v1 = worker;
*(_DWORD *)(*(_QWORD *)(worker + 40LL) + 16LL) = *(_DWORD *)(worker + 24LL);
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 88LL) = &net_read;
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 96LL) = &net_write;
*(_BYTE *)(*(_QWORD *)(v1 + 40) + 21LL) = 3;
*(_BYTE *)(*(_QWORD *)(v1 + 40) + 20LL) = 2;
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 72LL) = &kore_connection_handle;
*(_DWORD *)(*(_QWORD *)(v1 + 40) + 4LL) = 2;
v2 = worker;
*(_QWORD *)(*(_QWORD *)(worker + 40LL) + 328LL) = 0LL;
v3 = (_QWORD *)connections[1];
*(_QWORD *)(*(_QWORD *)(v2 + 40) + 336LL) = v3;
*v3 = *(_QWORD *)(v2 + 40);
v4 = *(_QWORD *)(worker + 40LL);
connections[1] = v4 + 328;
kore_platform_event_all(*(unsigned int *)(v4 + 16));
net_recv_queue(*(_QWORD *)(worker + 40LL), 16LL, 0LL, msg_recv_packet);
return v6 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func12()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rax
_QWORD *v3; // rdx
__int64 v4; // rsi
unsigned __int64 v6; // [rsp+8h] [rbp-10h]

v6 = __readfsqword(0x28u);
func10(1, (__int64)msg_type_websocket);
v0 = worker;
*(_QWORD *)(v0 + 40) = kore_connection_new(0LL);
v1 = worker;
*(_DWORD *)(*(_QWORD *)(worker + 40LL) + 16LL) = *(_DWORD *)(worker + 24LL);
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 88LL) = &net_read;
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 96LL) = &net_write;
*(_BYTE *)(*(_QWORD *)(v1 + 40) + 21LL) = 3;
*(_BYTE *)(*(_QWORD *)(v1 + 40) + 20LL) = 2;
*(_QWORD *)(*(_QWORD *)(v1 + 40) + 72LL) = &kore_connection_handle;
*(_DWORD *)(*(_QWORD *)(v1 + 40) + 4LL) = 2;
v2 = worker;
*(_QWORD *)(*(_QWORD *)(worker + 40LL) + 328LL) = 0LL;
v3 = (_QWORD *)connections[1];
*(_QWORD *)(*(_QWORD *)(v2 + 40) + 336LL) = v3;
*v3 = *(_QWORD *)(v2 + 40);
v4 = *(_QWORD *)(worker + 40LL);
connections[1] = v4 + 328;
kore_platform_event_all(*(unsigned int *)(v4 + 16));
net_recv_queue(*(_QWORD *)(worker + 40LL), 16LL, 0LL, msg_recv_packet);
return v6 - __readfsqword(0x28u);
}

original_code:
void
kore_msg_worker_init(void)
{
 kore_msg_register(1, msg_type_websocket);
 worker->msg[1] = kore_connection_new(((void *)0));
 worker->msg[1]->fd = worker->pipe[1];
 worker->msg[1]->read = net_read;
 worker->msg[1]->write = net_write;
 worker->msg[1]->proto = 3;
 worker->msg[1]->state = 2;
 worker->msg[1]->handle = kore_connection_handle;
 worker->msg[1]->evt.flags = 0x02;
 do { (worker->msg[1])->list.tqe_next = ((void *)0); (worker->msg[1])->list.tqe_prev = (&connections)->tqh_last; *(&connections)->tqh_last = (worker->msg[1]); (&connections)->tqh_last = &(worker->msg[1])->list.tqe_next; } while ( 0);
 kore_platform_event_all(worker->msg[1]->fd, worker->msg[1]);
 net_recv_queue(worker->msg[1],
     sizeof(struct kore_msg), 0, msg_recv_packet);
}

canonical_original_code:
void kore_msg_worker_init()
{
 kore_msg_register(1, msg_type_websocket);
 worker->msg[1] = kore_connection_new(((void *)0));
 worker->msg[1]->fd = worker->pipe[1];
 worker->msg[1]->read = net_read;
 worker->msg[1]->write = net_write;
 worker->msg[1]->proto = 3;
 worker->msg[1]->state = 2;
 worker->msg[1]->handle = kore_connection_handle;
 worker->msg[1]->evt.flags = 0x02;
 do { (worker->msg[1])->list.tqe_next = ((void *)0); (worker->msg[1])->list.tqe_prev = (&connections)->tqh_last; *(&connections)->tqh_last = (worker->msg[1]); (&connections)->tqh_last = &(worker->msg[1])->list.tqe_next; } while ( 0);
 kore_platform_event_all(worker->msg[1]->fd, worker->msg[1]);
 net_recv_queue(worker->msg[1],
     sizeof(struct kore_msg), 0, msg_recv_packet);
}


decompiled_code:
unsigned __int64 __fastcall pool_mark_entry_none(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 0) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_none", v4);
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 0) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_none", v4);
}
return v5 - __readfsqword(0x28u);
}

original_code:
static void
pool_mark_entry_none(struct kore_pool *pool, void *ptr)
{
 if (mprotect(ptr, pool->memsz - pool->pagesz, 0x0) == -1)
  fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
}

variable_types: struct kore_pool { unsigned long memsz; unsigned long growth; unsigned long pagesz; unsigned long elmlen; unsigned long uselen; unsigned int canary; int lock; char *name; struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; } *freelist; } *

variable_types:
ptr
void *

user_defined_types: struct kore_pool { unsigned long memsz; unsigned long growth; unsigned long pagesz; unsigned long elmlen; unsigned long uselen; unsigned int canary; int lock; char *name; struct kore_pool_entry *freelist; }


decompiled_code:
unsigned __int64 __fastcall pool_mark_entry_rw(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 3) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_rw", v4);
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 3) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_rw", v4);
}
return v5 - __readfsqword(0x28u);
}

original_code:
static void
pool_mark_entry_rw(struct kore_pool *pool, void *ptr)
{
 if (mprotect(ptr, pool->memsz - pool->pagesz,
     0x1 | 0x2) == -1)
  fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
}

user_defined_types: struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; }


decompiled_code:
unsigned __int64 __fastcall pool_grow(_QWORD *a1, __int64 a2)
{
_QWORD *v3; // r14
__int64 v4; // r13
_QWORD *v5; // rbx
char *v6; // rbp
unsigned __int64 v7; // rbp
size_t v8; // rsi
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
__int64 v14; // [rsp+8h] [rbp-50h]
unsigned __int64 v15; // [rsp+18h] [rbp-40h]

v15 = __readfsqword(0x28u);
v3 = (_QWORD *)a1[8];
v14 = 0LL;
if ( !kore_mem_guard )
v14 = kore_mmap_region(a2 * (a1[3] + 32LL));
if ( !a2 )
goto LABEL_23;
v4 = 0LL;
while ( 1 )
{
if ( kore_mem_guard )
{
v5 = (_QWORD *)kore_mmap_region(*a1);
v14 = (__int64)v5;
v6 = (char *)v5 + *a1 - a1[2] - a1[3];
}
else
{
v5 = (_QWORD *)(v14 + v4 * (a1[3] + 32LL));
v6 = (char *)(v5 + 4);
}
v5[1] = v6;
v5[3] = 0LL;
*(_BYTE *)v5 = 1;
v5[2] = &v6[a1[4]];
if ( !v3 )
goto LABEL_7;
v3[3] = v5;
if ( kore_mem_guard )
{
pool_mark_entry_none(a1, v3);
LABEL_7:
if ( a1[8] )
goto LABEL_8;
goto LABEL_19;
}
if ( a1[8] )
goto LABEL_12;
LABEL_19:
a1[8] = v5;
LABEL_8:
if ( !kore_mem_guard )
goto LABEL_12;
v7 = (unsigned __int64)&v6[a1[3]];
v8 = a1[2];
if ( v7 % v8 )
break;
if ( mprotect((void *)v7, v8, 0) == -1 )
goto LABEL_21;
if ( madvise((void *)v7, a1[2], 8) == -1 )
goto LABEL_22;
LABEL_12:
if ( a2 == ++v4 )
goto LABEL_25;
v3 = v5;
}
fatal("%s: misaligned page", "pool_grow");
LABEL_21:
v9 = _errno_location();
v10 = strerror(*v9);
fatal("%s: mprotect: %s", "pool_grow", v10);
LABEL_22:
v11 = _errno_location();
v12 = strerror(*v11);
fatal("%s: madvise: %s", "pool_grow", v12);
LABEL_23:
if ( v3 )
{
v5 = v3;
LABEL_25:
if ( kore_mem_guard )
pool_mark_entry_none(a1, v5);
}
return v15 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1, __int64 a2)
{
_QWORD *v3; // r14
__int64 v4; // r13
_QWORD *v5; // rbx
char *v6; // rbp
unsigned __int64 v7; // rbp
size_t v8; // rsi
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
__int64 v14; // [rsp+8h] [rbp-50h]
unsigned __int64 v15; // [rsp+18h] [rbp-40h]

v15 = __readfsqword(0x28u);
v3 = (_QWORD *)a1[8];
v14 = 0LL;
if ( !kore_mem_guard )
v14 = kore_mmap_region(a2 * (a1[3] + 32LL));
if ( !a2 )
goto LABEL_23;
v4 = 0LL;
while ( 1 )
{
if ( kore_mem_guard )
{
v5 = (_QWORD *)kore_mmap_region(*a1);
v14 = (__int64)v5;
v6 = (char *)v5 + *a1 - a1[2] - a1[3];
}
else
{
v5 = (_QWORD *)(v14 + v4 * (a1[3] + 32LL));
v6 = (char *)(v5 + 4);
}
v5[1] = v6;
v5[3] = 0LL;
*(_BYTE *)v5 = 1;
v5[2] = &v6[a1[4]];
if ( !v3 )
goto LABEL_7;
v3[3] = v5;
if ( kore_mem_guard )
{
func0(a1, v3);
LABEL_7:
if ( a1[8] )
goto LABEL_8;
goto LABEL_19;
}
if ( a1[8] )
goto LABEL_12;
LABEL_19:
a1[8] = v5;
LABEL_8:
if ( !kore_mem_guard )
goto LABEL_12;
v7 = (unsigned __int64)&v6[a1[3]];
v8 = a1[2];
if ( v7 % v8 )
break;
if ( mprotect((void *)v7, v8, 0) == -1 )
goto LABEL_21;
if ( madvise((void *)v7, a1[2], 8) == -1 )
goto LABEL_22;
LABEL_12:
if ( a2 == ++v4 )
goto LABEL_25;
v3 = v5;
}
fatal("%s: misaligned page", "pool_grow");
LABEL_21:
v9 = _errno_location();
v10 = strerror(*v9);
fatal("%s: mprotect: %s", "pool_grow", v10);
LABEL_22:
v11 = _errno_location();
v12 = strerror(*v11);
fatal("%s: madvise: %s", "pool_grow", v12);
LABEL_23:
if ( v3 )
{
v5 = v3;
LABEL_25:
if ( kore_mem_guard )
func0(a1, v5);
}
return v15 - __readfsqword(0x28u);
}

original_code:
static void
pool_grow(struct kore_pool *pool, size_t elms)
{
 size_t i;
 u_int8_t *base, *p;
 struct kore_pool_entry *entry, *prev;
 prev = pool->freelist;
 if (kore_mem_guard == 0)
  base = kore_mmap_region(elms * (sizeof(*entry) + pool->elmlen));
 else
  base = ((void *)0);
 for (i = 0; i < elms; i++) {
  if (kore_mem_guard) {
   base = kore_mmap_region(pool->memsz);
   p = base + (pool->memsz - pool->pagesz - pool->elmlen);
   entry = (struct kore_pool_entry *)base;
  } else {
   p = base + ((sizeof(*entry) + pool->elmlen) * i);
   entry = (struct kore_pool_entry *)p;
   p += sizeof(*entry);
  }
  entry->uptr = p;
  entry->nextfree = ((void *)0);
  entry->state = 1;
  entry->canary = p + pool->uselen;
  if (prev != ((void *)0)) {
   prev->nextfree = entry;
   if (kore_mem_guard)
    pool_mark_entry_none(pool, prev);
  }
  prev = entry;
  if (pool->freelist == ((void *)0))
   pool->freelist = entry;
  if (kore_mem_guard) {
   p += pool->elmlen;
   if (((uintptr_t)p % pool->pagesz) != 0)
    fatal("%s: misaligned page", __func__);
   if (mprotect(p, pool->pagesz, 0x0) == -1)
    fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
   if (madvise(p, pool->pagesz, 8) == -1)
    fatal("%s: madvise: %s", __func__, strerror((*__errno_location ())));
  }
 }
 if (prev != ((void *)0) && kore_mem_guard)
  pool_mark_entry_none(pool, prev);
}

canonical_original_code:
static void pool_grow(struct kore_pool *pool, unsigned long elms)
{
 unsigned long i;
 unsigned int *base; unsigned int *p;
 struct kore_pool_entry *entry; struct kore_pool_entry *prev;
 prev = pool->freelist;
 if (kore_mem_guard == 0)
  base = kore_mmap_region(elms * (sizeof(*entry) + pool->elmlen));
 else
  base = ((void *)0);
 for (i = 0; i < elms; i++) {
  if (kore_mem_guard) {
   base = kore_mmap_region(pool->memsz);
   p = base + (pool->memsz - pool->pagesz - pool->elmlen);
   entry = (struct kore_pool_entry *)base;
  } else {
   p = base + ((sizeof(*entry) + pool->elmlen) * i);
   entry = (struct kore_pool_entry *)p;
   p += sizeof(*entry);
  }
  entry->uptr = p;
  entry->nextfree = ((void *)0);
  entry->state = 1;
  entry->canary = p + pool->uselen;
  if (prev != ((void *)0)) {
   prev->nextfree = entry;
   if (kore_mem_guard)
    pool_mark_entry_none(pool, prev);
  }
  prev = entry;
  if (pool->freelist == ((void *)0))
   pool->freelist = entry;
  if (kore_mem_guard) {
   p += pool->elmlen;
   if (((unsigned long)p % pool->pagesz) != 0)
    fatal("%s: misaligned page", __func__);
   if (mprotect(p, pool->pagesz, 0x0) == -1)
    fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
   if (madvise(p, pool->pagesz, 8) == -1)
    fatal("%s: madvise: %s", __func__, strerror((*__errno_location ())));
  }
 }
 if (prev != ((void *)0) && kore_mem_guard)
  pool_mark_entry_none(pool, prev);
}

variable_types:
elms
unsigned long

variable_types:
i
unsigned long

variable_types:
base
unsigned int *

variable_types:
p
unsigned int *

variable_types: struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; } *


original_code:
void
kore_pool_init(struct kore_pool *pool, const char *name,
    size_t len, size_t elm)
{
 long pagesz;
 if (elm < 16)
  elm = 16;
 if ((pagesz = sysconf(_SC_PAGESIZE)) == -1)
  fatal("%s: sysconf: %s", __func__, strerror((*__errno_location ())));
 if ((pool->name = strdup(name)) == ((void *)0))
  fatal("kore_pool_init: strdup %s", strerror((*__errno_location ())));
 pool->uselen = len;
 len = len + sizeof(u_int64_t);
 len = (len + (16 - 1)) & ~(16 - 1);
 pool->elmlen = len;
 pool->lock = 0;
 pool->freelist = ((void *)0);
 pool->pagesz = pagesz;
 pool->growth = elm * 0.25f;
 pool->canary = (u_int64_t)kore_platform_random_uint32() << 32 |
     kore_platform_random_uint32();
 if (kore_mem_guard) {
  pool->memsz = pool->pagesz * 2;
  while (pool->elmlen >
      pool->pagesz - sizeof(struct kore_pool_entry)) {
   pool->memsz += pool->pagesz;
   pool->elmlen -= (((pool->elmlen)<(pool->pagesz))?(pool->elmlen):(pool->pagesz));
  }
  pool->elmlen = len;
 } else {
  pool->memsz = pool->elmlen;
 }
 pool_grow(pool, elm);
}

canonical_original_code:
void kore_pool_init(struct kore_pool *pool, char *name, unsigned long len, unsigned long elm)
{
 long pagesz;
 if (elm < 16)
  elm = 16;
 if ((pagesz = sysconf(_SC_PAGESIZE)) == -1)
  fatal("%s: sysconf: %s", __func__, strerror((*__errno_location ())));
 if ((pool->name = strdup(name)) == ((void *)0))
  fatal("kore_pool_init: strdup %s", strerror((*__errno_location ())));
 pool->uselen = len;
 len = len + sizeof(u_int64_t);
 len = (len + (16 - 1)) & ~(16 - 1);
 pool->elmlen = len;
 pool->lock = 0;
 pool->freelist = ((void *)0);
 pool->pagesz = pagesz;
 pool->growth = elm * 0.25f;
 pool->canary = (unsigned int)kore_platform_random_uint32() << 32 |
     kore_platform_random_uint32();
 if (kore_mem_guard) {
  pool->memsz = pool->pagesz * 2;
  while (pool->elmlen >
      pool->pagesz - sizeof(struct kore_pool_entry)) {
   pool->memsz += pool->pagesz;
   pool->elmlen -= (((pool->elmlen)<(pool->pagesz))?(pool->elmlen):(pool->pagesz));
  }
  pool->elmlen = len;
 } else {
  pool->memsz = pool->elmlen;
 }
 pool_grow(pool, elm);
}

variable_types:
name
char *

variable_types:
len
unsigned long

variable_types:
elm
unsigned long

variable_types: pagesz long


decompiled_code:
unsigned __int64 __fastcall kore_pool_cleanup(__int64 a1)
{
_QWORD *v2; // rbx
void *v3; // rdi
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( kore_mem_guard )
{
v2 = *(_QWORD **)(a1 + 64);
while ( v2 )
{
pool_mark_entry_rw((_QWORD *)a1, v2);
v3 = v2;
v2 = (_QWORD *)v2[3];
munmap(v3, *(_QWORD *)a1);
}
}
free(*(void **)(a1 + 56));
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 a1)
{
_QWORD *v2; // rbx
void *v3; // rdi
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( kore_mem_guard )
{
v2 = *(_QWORD **)(a1 + 64);
while ( v2 )
{
func1((_QWORD *)a1, v2);
v3 = v2;
v2 = (_QWORD *)v2[3];
munmap(v3, *(_QWORD *)a1);
}
}
free(*(void **)(a1 + 56));
return v5 - __readfsqword(0x28u);
}

original_code:
void
kore_pool_cleanup(struct kore_pool *pool)
{
 struct kore_pool_entry *entry, *next;
 if (kore_mem_guard) {
  for (entry = pool->freelist; entry != ((void *)0); entry = next) {
   pool_mark_entry_rw(pool, entry);
   next = entry->nextfree;
   (void)munmap(entry, pool->memsz);
  }
 }
 free(pool->name);
}

canonical_original_code:
void kore_pool_cleanup(struct kore_pool *pool)
{
 struct kore_pool_entry *entry; struct kore_pool_entry *next;
 if (kore_mem_guard) {
  for (entry = pool->freelist; entry != ((void *)0); entry = next) {
   pool_mark_entry_rw(pool, entry);
   next = entry->nextfree;
   (void)munmap(entry, pool->memsz);
  }
 }
 free(pool->name);
}


original_code:
void *
kore_pool_get(struct kore_pool *pool)
{
 u_int64_t canary;
 struct kore_pool_entry *entry;
 if (pool->freelist == ((void *)0))
  pool_grow(pool, pool->growth);
 entry = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_rw(pool, entry);
 pool->freelist = entry->nextfree;
 if (entry->state != 1)
  fatal("%s: element %p was not free", pool->name, (void *)entry);
 entry->nextfree = ((void *)0);
 entry->state = 0;
 canary = pool->canary;
 canary ^= (uintptr_t)entry;
 canary ^= (uintptr_t)entry->uptr;
 memcpy(entry->canary, &canary, sizeof(canary));
 return (entry->uptr);
}

canonical_original_code:
void *kore_pool_get(struct kore_pool *pool)
{
 unsigned int canary;
 struct kore_pool_entry *entry;
 if (pool->freelist == ((void *)0))
  pool_grow(pool, pool->growth);
 entry = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_rw(pool, entry);
 pool->freelist = entry->nextfree;
 if (entry->state != 1)
  fatal("%s: element %p was not free", pool->name, (void *)entry);
 entry->nextfree = ((void *)0);
 entry->state = 0;
 canary = pool->canary;
 canary ^= (unsigned long)entry;
 canary ^= (unsigned long)entry->uptr;
 memcpy(entry->canary, &canary, sizeof(canary));
 return (entry->uptr);
}

variable_types:
canary
unsigned int

return_type: void *


decompiled_code:
unsigned __int64 __fastcall kore_pool_put(__int64 a1, const char *a2)
{
unsigned __int64 v2; // rbx
const char *v3; // rdx
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
v2 = (unsigned __int64)(a2 - 32);
if ( kore_mem_guard )
v2 = (unsigned __int64)&a2[-((unsigned __int64)a2 % *(_QWORD *)(a1 + 16))];
v3 = *(const char **)(v2 + 8);
if ( v3 != a2 )
{
fatal("%s: uptr mismatch %p != %p", *(const char **)(a1 + 56), v3, a2);
goto LABEL_9;
}
if ( (**(_QWORD **)(v2 + 16) ^ (unsigned __int64)a2 ^ v2) != *(_QWORD *)(a1 + 40) )
{
LABEL_9:
a2 = *(const char **)(a1 + 56);
fatal("%s: memory corruption detected", a2);
goto LABEL_10;
}
if ( *(_BYTE *)v2 )
{
LABEL_10:
fatal("%s: element %p was not busy", *(const char **)(a1 + 56), a2);
goto LABEL_11;
}
*(_BYTE *)v2 = 1;
*(_QWORD *)(v2 + 24) = *(_QWORD *)(a1 + 64);
if ( kore_mem_guard )
LABEL_11:
pool_mark_entry_none((_QWORD *)a1, (void *)v2);
*(_QWORD *)(a1 + 64) = v2;
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(__int64 a1, const char *a2)
{
unsigned __int64 v2; // rbx
const char *v3; // rdx
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
v2 = (unsigned __int64)(a2 - 32);
if ( kore_mem_guard )
v2 = (unsigned __int64)&a2[-((unsigned __int64)a2 % *(_QWORD *)(a1 + 16))];
v3 = *(const char **)(v2 + 8);
if ( v3 != a2 )
{
fatal("%s: uptr mismatch %p != %p", *(const char **)(a1 + 56), v3, a2);
goto LABEL_9;
}
if ( (**(_QWORD **)(v2 + 16) ^ (unsigned __int64)a2 ^ v2) != *(_QWORD *)(a1 + 40) )
{
LABEL_9:
a2 = *(const char **)(a1 + 56);
fatal("%s: memory corruption detected", a2);
goto LABEL_10;
}
if ( *(_BYTE *)v2 )
{
LABEL_10:
fatal("%s: element %p was not busy", *(const char **)(a1 + 56), a2);
goto LABEL_11;
}
*(_BYTE *)v2 = 1;
*(_QWORD *)(v2 + 24) = *(_QWORD *)(a1 + 64);
if ( kore_mem_guard )
LABEL_11:
func0((_QWORD *)a1, (void *)v2);
*(_QWORD *)(a1 + 64) = v2;
return v5 - __readfsqword(0x28u);
}

original_code:
void
kore_pool_put(struct kore_pool *pool, void *ptr)
{
 void *base;
 u_int64_t canary;
 struct kore_pool_entry *entry;
 if (kore_mem_guard) {
  base = (u_int8_t *)ptr - ((uintptr_t)ptr % pool->pagesz);
 } else {
  base = (u_int8_t *)ptr - sizeof(*entry);
 }
 entry = (struct kore_pool_entry *)base;
 if (entry->uptr != ptr) {
  fatal("%s: uptr mismatch %p != %p",
      pool->name, entry->uptr, ptr);
 }
 memcpy(&canary, entry->canary, sizeof(canary));
 canary ^= (uintptr_t)entry;
 canary ^= (uintptr_t)ptr;
 if (canary != pool->canary)
  fatal("%s: memory corruption detected", pool->name);
 if (entry->state != 0)
  fatal("%s: element %p was not busy", pool->name, ptr);
 entry->state = 1;
 entry->nextfree = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_none(pool, entry);
 pool->freelist = entry;
}

canonical_original_code:
void kore_pool_put(struct kore_pool *pool, void *ptr)
{
 void *base;
 unsigned int canary;
 struct kore_pool_entry *entry;
 if (kore_mem_guard) {
  base = (unsigned int *)ptr - ((unsigned long)ptr % pool->pagesz);
 } else {
  base = (unsigned int *)ptr - sizeof(*entry);
 }
 entry = (struct kore_pool_entry *)base;
 if (entry->uptr != ptr) {
  fatal("%s: uptr mismatch %p != %p",
      pool->name, entry->uptr, ptr);
 }
 memcpy(&canary, entry->canary, sizeof(canary));
 canary ^= (unsigned long)entry;
 canary ^= (unsigned long)ptr;
 if (canary != pool->canary)
  fatal("%s: memory corruption detected", pool->name);
 if (entry->state != 0)
  fatal("%s: element %p was not busy", pool->name, ptr);
 entry->state = 1;
 entry->nextfree = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_none(pool, entry);
 pool->freelist = entry;
}

variable_types:
base
void *


meminfo

decompiled_code:
__int64 __fastcall meminfo(__int64 a1)
{
return a1 - 16;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1 - 16;
}

original_code:
static inline struct meminfo *
meminfo(void *ptr)
{
 return ((struct meminfo *)((u_int8_t *)ptr - sizeof(struct meminfo)));
}

canonical_original_code:
static inline struct meminfo *meminfo(void *ptr)
{
 return ((struct meminfo *)((unsigned int *)ptr - sizeof(struct meminfo)));
}

return_type: struct meminfo { unsigned long len; unsigned int flags; } *


decompiled_code:
unsigned __int64 __fastcall mem_index(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
unsigned __int64 i; // rax

if ( a1 <= 8 )
goto LABEL_7;
v1 = 0LL;
for ( i = 8LL; i < a1; i *= 2LL )
++v1;
if ( v1 > 0xA )
{
fatal("mem_index: idx too high");
LABEL_7:
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
unsigned __int64 i; // rax

if ( a1 <= 8 )
goto LABEL_7;
v1 = 0LL;
for ( i = 8LL; i < a1; i *= 2LL )
++v1;
if ( v1 > 0xA )
{
fatal("mem_index: idx too high");
LABEL_7:
v1 = 0LL;
}
return v1;
}

original_code:
static size_t
mem_index(size_t len)
{
 size_t mlen, idx;
 idx = 0;
 mlen = 8;
 while (mlen < len) {
  idx++;
  mlen = mlen << 1;
 }
 if (idx > (11 - 1))
  fatal("mem_index: idx too high");
 return (idx);
}

canonical_original_code:
static unsigned long mem_index(unsigned long len)
{
 unsigned long mlen; unsigned long idx;
 idx = 0;
 mlen = 8;
 while (mlen < len) {
  idx++;
  mlen = mlen << 1;
 }
 if (idx > (11 - 1))
  fatal("mem_index: idx too high");
 return (idx);
}

variable_types:
mlen
unsigned long

variable_types:
idx
unsigned long

return_type: unsigned long