rename

decompiled_code:
__int64 __fastcall rename(const char *a1, const char *a2)
{
__int64 v2; // rax
const char *v3; // r12
const char *v4; // rbp
__int64 v5; // rax
__int64 v6; // r14
__int64 v7; // rax
__int64 v8; // r13
char v10[32]; // [rsp+0h] [rbp-78h] BYREF
char expbuf[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = last_dir(a1, v10);
if ( !v2 )
return 4294967294LL;
v3 = (const char *)v2;
v4 = (const char *)last_dir(a2, expbuf);
if ( !v4 )
return 4294967294LL;
LODWORD(v5) = advance(v3, v10);
v6 = v5;
if ( !v5 )
return 4294967294LL;
LODWORD(v7) = advance(v4, expbuf);
v8 = v7;
if ( v7 )
{
dir_delete((__int64)v4, expbuf);
--*(_BYTE *)(v8 + 13);
*(_BYTE *)(v8 + 34) = 1;
put_inode(v8);
}
dir_add_isra_0((__int64)v4, expbuf, *(_WORD *)(v6 + 32));
if ( (unsigned int)dir_delete((__int64)v3, v10) != 1 )
panic("rename(\"%s\", \"%s\"): unable to delete old entry", a1, a2);
put_inode(v3);
put_inode(v4);
put_inode(v6);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char *a1, const char *a2)
{
__int64 v2; // rax
const char *v3; // r12
const char *v4; // rbp
__int64 v5; // rax
__int64 v6; // r14
__int64 v7; // rax
__int64 v8; // r13
char v10[32]; // [rsp+0h] [rbp-78h] BYREF
char expbuf[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = last_dir(a1, v10);
if ( !v2 )
return 4294967294LL;
v3 = (const char *)v2;
v4 = (const char *)last_dir(a2, expbuf);
if ( !v4 )
return 4294967294LL;
LODWORD(v5) = advance(v3, v10);
v6 = v5;
if ( !v5 )
return 4294967294LL;
LODWORD(v7) = advance(v4, expbuf);
v8 = v7;
if ( v7 )
{
func11((__int64)v4, expbuf);
--*(_BYTE *)(v8 + 13);
*(_BYTE *)(v8 + 34) = 1;
put_inode(v8);
}
func6((__int64)v4, expbuf, *(_WORD *)(v6 + 32));
if ( (unsigned int)func11((__int64)v3, v10) != 1 )
panic("rename(\"%s\", \"%s\"): unable to delete old entry", a1, a2);
put_inode(v3);
put_inode(v4);
put_inode(v6);
return 1LL;
}

original_code:
int rename(const char *src_path, const char *dest_path)
{
 struct minix_inode *s_dir, *d_dir, *i, *di;
 char old_name[30];
 char new_name[30];
 ;
 if((s_dir = last_dir(src_path, old_name)) == ((void *)0))
  return -2;
 if((d_dir = last_dir(dest_path, new_name)) == ((void *)0))
  return -2;
 if((i = advance(s_dir, old_name)) == ((void *)0))
  return -2;
 if((di = advance(d_dir, new_name)) != ((void *)0)) {
  dir_delete(d_dir, new_name);
  di->i_nlinks--;
  di->i_dirty = 1;
  put_inode(di);
 }
 dir_add(d_dir, new_name, i->i_num);
 if(dir_delete(s_dir, old_name) != 1) {
  panic("rename(\"%s\", \"%s\"): unable to delete old entry",
   src_path, dest_path);
 }
 put_inode(s_dir);
 put_inode(d_dir);
 put_inode(i);
 ;
 return 1;
}

canonical_original_code:
int rename(char *src_path, char *dest_path)
{
 struct minix_inode *s_dir; struct minix_inode *d_dir; struct minix_inode *i; struct minix_inode *di;
 char old_name[30];
 char new_name[30];
 ;
 if((s_dir = last_dir(src_path, old_name)) == ((void *)0))
  return -2;
 if((d_dir = last_dir(dest_path, new_name)) == ((void *)0))
  return -2;
 if((i = advance(s_dir, old_name)) == ((void *)0))
  return -2;
 if((di = advance(d_dir, new_name)) != ((void *)0)) {
  dir_delete(d_dir, new_name);
  di->i_nlinks--;
  di->i_dirty = 1;
  put_inode(di);
 }
 dir_add(d_dir, new_name, i->i_num);
 if(dir_delete(s_dir, old_name) != 1) {
  panic("rename(\"%s\", \"%s\"): unable to delete old entry",
   src_path, dest_path);
 }
 put_inode(s_dir);
 put_inode(d_dir);
 put_inode(i);
 ;
 return 1;
}


unlink

repo: jordan-dinwiddy/Somix

decompiled_code:
__int64 __fastcall unlink(const char *a1)
{
const char *v1; // r12
__int64 v2; // rax
__int64 v3; // rbp
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = (const char *)last_dir(a1, v5);
if ( !v1 )
panic("unlink(\"%s\"): failed to resolve final dir in path", a1);
LODWORD(v2) = advance(v1, v5);
v3 = v2;
if ( !v2 )
{
put_inode(v1);
panic("unlink(\"%s\"): failed to resolve final component", a1);
}
dir_delete((__int64)v1, v5);
--*(_BYTE *)(v3 + 13);
*(_BYTE *)(v3 + 34) = 1;
put_inode(v3);
put_inode(v1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(const char *a1)
{
const char *v1; // r12
__int64 v2; // rax
__int64 v3; // rbp
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = (const char *)last_dir(a1, v5);
if ( !v1 )
panic("unlink(\"%s\"): failed to resolve final dir in path", a1);
LODWORD(v2) = advance(v1, v5);
v3 = v2;
if ( !v2 )
{
put_inode(v1);
panic("unlink(\"%s\"): failed to resolve final component", a1);
}
func11((__int64)v1, v5);
--*(_BYTE *)(v3 + 13);
*(_BYTE *)(v3 + 34) = 1;
put_inode(v3);
put_inode(v1);
return 1LL;
}

original_code:
int unlink(const char *path)
{
 struct minix_inode *p_dir;
 struct minix_inode *i;
 char filename[30];
 ;
 if((p_dir = last_dir(path, filename)) == ((void *)0))
  panic("unlink(\"%s\"): failed to resolve final dir in path",
   path);
 ;
 if((i = advance(p_dir, filename)) == ((void *)0)) {
  put_inode(p_dir);
  panic("unlink(\"%s\"): failed to resolve final component",
   path);
 }

 dir_delete(p_dir, filename);
 i->i_nlinks--;
 i->i_dirty = 1;
 put_inode(i);
 put_inode(p_dir);
 return 1;
}

canonical_original_code:
int unlink(char *path)
{
 struct minix_inode *p_dir;
 struct minix_inode *i;
 char filename[30];
 ;
 if((p_dir = last_dir(path, filename)) == ((void *)0))
  panic("unlink(\"%s\"): failed to resolve final dir in path",
   path);
 ;
 if((i = advance(p_dir, filename)) == ((void *)0)) {
  put_inode(p_dir);
  panic("unlink(\"%s\"): failed to resolve final component",
   path);
 }

 dir_delete(p_dir, filename);
 i->i_nlinks--;
 i->i_dirty = 1;
 put_inode(i);
 put_inode(p_dir);
 return 1;
}

variable_types:
path
char *

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *

variable_types:
filename
char [30]

return_type: int


original_code:
static void rw_inode(struct minix_inode *i, int rw_flag)
{
 int i_block;
 int i_block_offset;
 struct minix_block *blk;
 i_block = 2 + sb.s_imap_blocks + sb.s_zmap_blocks;
 i_block += ((i->i_num - 1) * sizeof(struct minix_inode_disk)) / 1024;
 i_block_offset = ((i->i_num - 1) * sizeof(struct minix_inode_disk)) % 1024;
 ;
 blk = get_block(i_block, 1);
 if(rw_flag == 1) {
  memcpy((void *)i, (void *) (blk->blk_data + i_block_offset),
   sizeof(struct minix_inode_disk));
 }
 else {
  memcpy((void *) (blk->blk_data + i_block_offset), (void *) i,
   sizeof(struct minix_inode_disk));
  blk->blk_dirty = 1;
 }
 put_block(blk, 0);
 i->i_dirty = 0;
}

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: void

user_defined_types: struct minix_inode_disk { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; }

user_defined_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; }


decompiled_code:
char *__fastcall get_inode(unsigned __int16 a1)
{
char *result; // rax
__m128i *v2; // rbx
int v3; // edx

result = inode_table;
v2 = 0LL;
do
{
while ( 1 )
{
v3 = *((_DWORD *)result + 9);
if ( v3 > 0 )
break;
v2 = (__m128i *)result;
result += 40;
if ( result == &inode_table[1280] )
goto LABEL_6;
}
if ( *((_WORD *)result + 16) == a1 )
{
*((_DWORD *)result + 9) = v3 + 1;
return result;
}
result += 40;
}
while ( result != &inode_table[1280] );
LABEL_6:
if ( !v2 )
panic("get_inode(%d): no free slots in inode table to read in read in inode", a1);
v2[2].m128i_i16[0] = a1;
v2[2].m128i_i32[1] = 1;
rw_inode(v2, 1);
return (char *)v2;
}

canonical_decompiled_code:
char *__fastcall func2(unsigned __int16 a1)
{
char *result; // rax
__m128i *v2; // rbx
int v3; // edx

result = inode_table;
v2 = 0LL;
do
{
while ( 1 )
{
v3 = *((_DWORD *)result + 9);
if ( v3 > 0 )
break;
v2 = (__m128i *)result;
result += 40;
if ( result == &inode_table[1280] )
goto LABEL_6;
}
if ( *((_WORD *)result + 16) == a1 )
{
*((_DWORD *)result + 9) = v3 + 1;
return result;
}
result += 40;
}
while ( result != &inode_table[1280] );
LABEL_6:
if ( !v2 )
panic("get_inode(%d): no free slots in inode table to read in read in inode", a1);
v2[2].m128i_i16[0] = a1;
v2[2].m128i_i32[1] = 1;
func1(v2, 1);
return (char *)v2;
}

original_code:
struct minix_inode *get_inode(inode_nr i_num)
{
 struct minix_inode *i, *free_slot;
 free_slot = 0;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_count > 0) {
   if(i->i_num == i_num) {
    ;
    i->i_count++;
    return i;
   }
  }
  else {
   free_slot = i;
  }
 }
 if(free_slot == 0)
  panic("get_inode(%d): no free slots in inode table to read in "
   "read in inode", i_num);
 ;
 free_slot->i_num = i_num;
 free_slot->i_count = 1;
 rw_inode(free_slot, 1);
 ;
 return free_slot;
}

canonical_original_code:
struct minix_inode *get_inode(unsigned short i_num)
{
 struct minix_inode *i; struct minix_inode *free_slot;
 free_slot = 0;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_count > 0) {
   if(i->i_num == i_num) {
    ;
    i->i_count++;
    return i;
   }
  }
  else {
   free_slot = i;
  }
 }
 if(free_slot == 0)
  panic("get_inode(%d): no free slots in inode table to read in "
   "read in inode", i_num);
 ;
 free_slot->i_num = i_num;
 free_slot->i_count = 1;
 rw_inode(free_slot, 1);
 ;
 return free_slot;
}

return_type: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *


decompiled_code:
int print_inode_table()
{
char *v0; // rbx
const char *v1; // rcx
__int64 v2; // rdx
__int64 v3; // rsi
int result; // eax

v0 = inode_table;
do
{
v1 = "No";
v2 = *((unsigned int *)v0 + 9);
if ( v0[34] == 1 )
v1 = "Yes";
v3 = *((unsigned __int16 *)v0 + 16);
v0 += 40;
result = printf(" inode %d: count=%d, dirty = %s xxyyzz\n", v3, v2, v1);
}
while ( v0 != &inode_table[1280] );
return result;
}

canonical_decompiled_code:
int func3()
{
char *v0; // rbx
const char *v1; // rcx
__int64 v2; // rdx
__int64 v3; // rsi
int result; // eax

v0 = inode_table;
do
{
v1 = "No";
v2 = *((unsigned int *)v0 + 9);
if ( v0[34] == 1 )
v1 = "Yes";
v3 = *((unsigned __int16 *)v0 + 16);
v0 += 40;
result = printf(" inode %d: count=%d, dirty = %s xxyyzz\n", v3, v2, v1);
}
while ( v0 != &inode_table[1280] );
return result;
}

original_code:
void print_inode_table(void)
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  printf("    inode %d: count=%d, dirty = %s xxyyzz\n", i->i_num,
   i->i_count, i->i_dirty == 1 ? "Yes" : "No");
 }
}

canonical_original_code:
void print_inode_table()
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  printf("    inode %d: count=%d, dirty = %s xxyyzz\n", i->i_num,
   i->i_count, i->i_dirty == 1 ? "Yes" : "No");
 }
}


decompiled_code:
__int64 alloc_inode()
{
unsigned __int16 v0; // bx
char *v1; // rax
__int64 v2; // r8
__int64 result; // rax

v0 = alloc_bit(*(_QWORD *)&sb[16], 0LL);
v1 = get_inode(v0);
wipe_inode((__int64)v1);
*(_WORD *)(v2 + 32) = v0;
result = v2;
*(_BYTE *)(v2 + 34) = 1;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned __int16 v0; // bx
char *v1; // rax
__int64 v2; // r8
__int64 result; // rax

v0 = alloc_bit(*(_QWORD *)&sb[16], 0LL);
v1 = func2(v0);
func0((__int64)v1);
*(_WORD *)(v2 + 32) = v0;
result = v2;
*(_BYTE *)(v2 + 34) = 1;
return result;
}

original_code:
struct minix_inode *alloc_inode(void)
{
 struct minix_inode *inode;
 inode_nr i_num;
 ;
 i_num = (inode_nr) alloc_bit(sb.imap, 0);
 if(i_num < 0)
  panic("alloc_inode(): no free inodes available");
 inode = get_inode(i_num);
 wipe_inode(inode);
 inode->i_num = i_num;
 inode->i_dirty = 1;
 return inode;
}

canonical_original_code:
struct minix_inode *alloc_inode()
{
 struct minix_inode *inode;
 unsigned short i_num;
 ;
 i_num = (unsigned short) alloc_bit(sb.imap, 0);
 if(i_num < 0)
  panic("alloc_inode(): no free inodes available");
 inode = get_inode(i_num);
 wipe_inode(inode);
 inode->i_num = i_num;
 inode->i_dirty = 1;
 return inode;
}


decompiled_code:
__int64 __fastcall free_inode(unsigned __int16 a1)
{
return free_bit(*(_QWORD *)&sb[16], a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1)
{
return free_bit(*(_QWORD *)&sb[16], a1);
}

original_code:
void free_inode(inode_nr i_num)
{
 ;
 free_bit(sb.imap, (int) i_num);
}

canonical_original_code:
void free_inode(unsigned short i_num)
{
 ;
 free_bit(sb.imap, (int) i_num);
}


decompiled_code:
__int64 __fastcall flush_inode_table(__int64 a1, __off_t a2)
{
char *v2; // rbx
__m128i *v3; // rdi
__int64 result; // rax

v2 = inode_table;
do
{
while ( v2[34] != 1 )
{
v2 += 40;
if ( v2 == &inode_table[1280] )
return result;
}
if ( !v2[13] )
{
truncate(v2, a2);
free_inode(*((_WORD *)v2 + 16));
}
v3 = (__m128i *)v2;
a2 = 2LL;
v2 += 40;
result = rw_inode(v3, 2);
}
while ( v2 != &inode_table[1280] );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __off_t a2)
{
char *v2; // rbx
__m128i *v3; // rdi
__int64 result; // rax

v2 = inode_table;
do
{
while ( v2[34] != 1 )
{
v2 += 40;
if ( v2 == &inode_table[1280] )
return result;
}
if ( !v2[13] )
{
truncate(v2, a2);
func5(*((_WORD *)v2 + 16));
}
v3 = (__m128i *)v2;
a2 = 2LL;
v2 += 40;
result = func1(v3, 2);
}
while ( v2 != &inode_table[1280] );
return result;
}

original_code:
void flush_inode_table(void)
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_dirty == 1) {
   ;
   if(i->i_nlinks == 0) {
    ;
    truncate(i);
    free_inode(i->i_num);
   }
   rw_inode(i, 2);
  }
 }
}

canonical_original_code:
void flush_inode_table()
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_dirty == 1) {
   ;
   if(i->i_nlinks == 0) {
    ;
    truncate(i);
    free_inode(i->i_num);
   }
   rw_inode(i, 2);
  }
 }
}


decompiled_code:
__int64 __fastcall put_inode(char *file, __off_t a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *((unsigned int *)file + 9);
v3 = (unsigned int)(result - 1);
*((_DWORD *)file + 9) = v3;
if ( (int)v3 < 0 )
{
a2 = *((unsigned __int16 *)file + 16);
result = panic("put_inode(%d): i_count decremeted to %d!", a2, v3);
LODWORD(v3) = *((_DWORD *)file + 9);
}
if ( !(_DWORD)v3 )
{
if ( file[13] )
{
if ( file[34] != 1 )
return result;
return rw_inode((__m128i *)file, 2);
}
truncate(file, a2);
result = free_inode(*((_WORD *)file + 16));
if ( file[34] == 1 )
return rw_inode((__m128i *)file, 2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *file, __off_t a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *((unsigned int *)file + 9);
v3 = (unsigned int)(result - 1);
*((_DWORD *)file + 9) = v3;
if ( (int)v3 < 0 )
{
a2 = *((unsigned __int16 *)file + 16);
result = panic("put_inode(%d): i_count decremeted to %d!", a2, v3);
LODWORD(v3) = *((_DWORD *)file + 9);
}
if ( !(_DWORD)v3 )
{
if ( file[13] )
{
if ( file[34] != 1 )
return result;
return func1((__m128i *)file, 2);
}
truncate(file, a2);
result = func5(*((_WORD *)file + 16));
if ( file[34] == 1 )
return func1((__m128i *)file, 2);
}
return result;
}

original_code:
void put_inode(struct minix_inode *inode)
{
 inode->i_count--;
 if(inode->i_count < 0) {
  panic("put_inode(%d): i_count decremeted to %d!",
   inode->i_num, inode->i_count);
 }
 if(inode->i_count == 0) {
  if(inode->i_nlinks == 0) {
   ;
   truncate(inode);
   free_inode(inode->i_num);
  }
  if(inode->i_dirty == 1) {
   ;
    rw_inode(inode, 2);
  }
  else {
   ;
  }
 }
 else {
  ;
 }
 ;
}


variable_types: i int


original_code:
static void read_super(void)
{
 struct minix_block *blk;
 ;
 blk = get_block(1, 1);
 memcpy(&sb, blk->blk_data, sizeof(struct minix_super_block_disk));
 ;
 put_block(blk, 5 + 0200);
}

canonical_original_code:
static void read_super()
{
 struct minix_block *blk;
 ;
 blk = get_block(1, 1);
 memcpy(&sb, blk->blk_data, sizeof(struct minix_super_block_disk));
 ;
 put_block(blk, 5 + 0200);
}

user_defined_types: struct minix_super_block_disk { unsigned short s_ninodes; unsigned short s_nzones; unsigned short s_imap_blocks; unsigned short s_zmap_blocks; unsigned short s_firstdatazone; unsigned short s_log_zone_size; unsigned int s_max_size; unsigned short s_magic; unsigned short s_state; unsigned int s_zones; }

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; }


original_code:
static void unload_bitmaps(void)
{
 int i;
 for(i = 0; i < sb.imap->num_blocks; i++)
  put_block(sb.imap->blocks[i], 3 + 0200);
 for(i = 0; i < sb.zmap->num_blocks; i++)
  put_block(sb.zmap->blocks[i], 4 + 0200);
}

canonical_original_code:
static void unload_bitmaps()
{
 int i;
 for(i = 0; i < sb.imap->num_blocks; i++)
  put_block(sb.imap->blocks[i], 3 + 0200);
 for(i = 0; i < sb.zmap->num_blocks; i++)
  put_block(sb.zmap->blocks[i], 4 + 0200);
}


decompiled_code:
int minix_print_version()
{
unsigned __int16 v0; // ax

v0 = *((_WORD *)&sb + 8);
if ( v0 == 9320 )
{
puts("identified minix v2 file system");
goto LABEL_6;
}
if ( v0 > 0x2468u )
{
if ( v0 == 9336 )
{
puts("identified minix v2 file system (30 char names");
goto LABEL_6;
}
LABEL_10:
puts("unable to identify minix file system");
goto LABEL_6;
}
if ( v0 == 4991 )
{
puts("identified minix v1 file system");
goto LABEL_6;
}
if ( v0 != 5007 )
goto LABEL_10;
puts("identified minix v1 file system (30 char names");
LABEL_6:
printf("block size = %d bytes\n", 1024LL);
printf("buffer cache size = %dMB\n", 1LL);
printf("buffer cache hash table size = %d\n", 0x4000LL);
return puts("cache write_immed = OFF");
}

canonical_decompiled_code:
int func2()
{
unsigned __int16 v0; // ax

v0 = *((_WORD *)&sb + 8);
if ( v0 == 9320 )
{
puts("identified minix v2 file system");
goto LABEL_6;
}
if ( v0 > 0x2468u )
{
if ( v0 == 9336 )
{
puts("identified minix v2 file system (30 char names");
goto LABEL_6;
}
LABEL_10:
puts("unable to identify minix file system");
goto LABEL_6;
}
if ( v0 == 4991 )
{
puts("identified minix v1 file system");
goto LABEL_6;
}
if ( v0 != 5007 )
goto LABEL_10;
puts("identified minix v1 file system (30 char names");
LABEL_6:
printf("block size = %d bytes\n", 1024LL);
printf("buffer cache size = %dMB\n", 1LL);
printf("buffer cache hash table size = %d\n", 0x4000LL);
return puts("cache write_immed = OFF");
}

original_code:
static void minix_print_version(void)
{
 switch(sb.s_magic) {
  case 0x137F:
   printf("identified minix v1 file system\n");
   break;
  case 0x138F:
   printf("identified minix v1 file system (30 char "
    "names\n");
   break;
  case 0x2468:
   printf("identified minix v2 file system\n");
   break;
  case 0x2478:
   printf("identified minix v2 file system (30 char "
    "names\n");
   break;
  default:
   printf("unable to identify minix file system\n");
 }
 printf("block size = %d bytes\n", 1024);
 printf("buffer cache size = %dMB\n", 1024*1 / 1024);
 printf("buffer cache hash table size = %d\n", 16384);
 printf("cache write_immed = OFF\n");
}

canonical_original_code:
static void minix_print_version()
{
 switch(sb.s_magic) {
  case 0x137F:
   printf("identified minix v1 file system\n");
   break;
  case 0x138F:
   printf("identified minix v1 file system (30 char "
    "names\n");
   break;
  case 0x2468:
   printf("identified minix v2 file system\n");
   break;
  case 0x2478:
   printf("identified minix v2 file system (30 char "
    "names\n");
   break;
  default:
   printf("unable to identify minix file system\n");
 }
 printf("block size = %d bytes\n", 1024);
 printf("buffer cache size = %dMB\n", 1024*1 / 1024);
 printf("buffer cache hash table size = %d\n", 16384);
 printf("cache write_immed = OFF\n");
}


decompiled_code:
_DWORD *get_imap()
{
_DWORD *result; // rax

result = get_bitmap(2u, WORD2(sb));
result[4] = (unsigned __int16)sb;
return result;
}

canonical_decompiled_code:
_DWORD *func3()
{
_DWORD *result; // rax

result = get_bitmap(2u, WORD2(sb));
result[4] = (unsigned __int16)sb;
return result;
}

original_code:
static struct generic_bitmap *get_imap()
{
 int imap_offset = 2;
 struct generic_bitmap *bmap;
 bmap = get_bitmap(imap_offset, sb.s_imap_blocks);
 bmap->num_bits = sb.s_ninodes;
 return bmap;
}

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; } *

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


decompiled_code:
_DWORD *get_zmap()
{
_DWORD *result; // rax

result = get_bitmap((unsigned int)WORD2(sb) + 2, WORD3(sb));
result[4] = WORD1(sb);
return result;
}

canonical_decompiled_code:
_DWORD *func4()
{
_DWORD *result; // rax

result = get_bitmap((unsigned int)WORD2(sb) + 2, WORD3(sb));
result[4] = WORD1(sb);
return result;
}

original_code:
static struct generic_bitmap *get_zmap()
{
 int zmap_offset = 2 +
  sb.s_imap_blocks;
 struct generic_bitmap *bmap;
 bmap = get_bitmap(zmap_offset, sb.s_zmap_blocks);
 bmap->num_bits = sb.s_nzones;
 return bmap;
}

return_type: 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; } *


decompiled_code:
_DWORD *load_bitmaps()
{
_DWORD *result; // rax

*((_QWORD *)&sb + 4) = get_imap();
result = get_zmap();
*((_QWORD *)&sb + 5) = result;
return result;
}

canonical_decompiled_code:
_DWORD *func5()
{
_DWORD *result; // rax

*((_QWORD *)&sb + 4) = func3();
result = func4();
*((_QWORD *)&sb + 5) = result;
return result;
}

original_code:
static void load_bitmaps(void)
{
 ;
 sb.imap = get_imap();
 sb.zmap = get_zmap();
}

canonical_original_code:
static void load_bitmaps()
{
 ;
 sb.imap = get_imap();
 sb.zmap = get_zmap();
}


decompiled_code:
__int64 __fastcall minix_mount(char *src)
{
size_t v1; // rax
char *v2; // rax
__int64 result; // rax

open_blk_device();
init_cache();
read_super();
minix_print_version();
v1 = strlen(src);
v2 = (char *)malloc(v1);
*((_QWORD *)&sb + 3) = v2;
strcpy(v2, src);
load_bitmaps();
result = get_inode(1LL);
*((_QWORD *)&sb + 6) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src)
{
size_t v1; // rax
char *v2; // rax
__int64 result; // rax

open_blk_device();
init_cache();
func0();
func2();
v1 = strlen(src);
v2 = (char *)malloc(v1);
*((_QWORD *)&sb + 3) = v2;
strcpy(v2, src);
func5();
result = get_inode(1LL);
*((_QWORD *)&sb + 6) = result;
return result;
}

original_code:
void minix_mount(const char *device_name)
{
 open_blk_device(device_name);
 init_cache();
 read_super();
 minix_print_version();
 sb.device_name = malloc(strlen(device_name));
 strcpy(sb.device_name, device_name);
 load_bitmaps();
 sb.root_inode = get_inode((inode_nr) 1);
}

canonical_original_code:
void minix_mount(char *device_name)
{
 open_blk_device(device_name);
 init_cache();
 read_super();
 minix_print_version();
 sb.device_name = malloc(strlen(device_name));
 strcpy(sb.device_name, device_name);
 load_bitmaps();
 sb.root_inode = get_inode((unsigned short) 1);
}


original_code:
int timeval_subtract(struct timeval *result, struct timeval *x,
 struct timeval *y)
{
 if (x->tv_usec < y->tv_usec) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  y->tv_usec -= 1000000 * nsec;
  y->tv_sec += nsec;
 }
 if (x->tv_usec - y->tv_usec > 1000000) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000;
  y->tv_usec += 1000000 * nsec;
  y->tv_sec -= nsec;
 }
 result->tv_sec = x->tv_sec - y->tv_sec;
 result->tv_usec = x->tv_usec - y->tv_usec;
 return x->tv_sec < y->tv_sec;
}

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

variable_types:
x
struct timeval *

variable_types:
y
struct timeval *

variable_types: nsec int


decompiled_code:
__int64 minix_unmount()
{
int v0; // ebp
int v1; // ebx
struct timeval v3; // [rsp+0h] [rbp-48h] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF
__int64 v5[5]; // [rsp+20h] [rbp-28h] BYREF

v0 = clock();
gettimeofday(&v3, 0LL);
put_inode(*((_QWORD *)&sb + 6));
flush_inode_table();
unload_bitmaps();
sync_cache();
v1 = clock();
gettimeofday(&tv, 0LL);
timeval_subtract(v5, &tv.tv_sec, &v3.tv_sec);
printf("INFO: ");
printf("unmount(): CPU time elapsed: %f seconds\n", (float)((float)(v1 - v0) / 1000000.0));
printf("INFO: ");
printf("umount(): Wall time elapsed: %ld.%06ld seconds\n", v5[0], v5[1]);
printf("INFO: ");
puts("unmount(): destroying cache...");
return cache_destroy();
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // ebp
int v1; // ebx
struct timeval v3; // [rsp+0h] [rbp-48h] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF
__int64 v5[5]; // [rsp+20h] [rbp-28h] BYREF

v0 = clock();
gettimeofday(&v3, 0LL);
put_inode(*((_QWORD *)&sb + 6));
flush_inode_table();
func1();
sync_cache();
v1 = clock();
gettimeofday(&tv, 0LL);
func7(v5, &tv.tv_sec, &v3.tv_sec);
printf("INFO: ");
printf("unmount(): CPU time elapsed: %f seconds\n", (float)((float)(v1 - v0) / 1000000.0));
printf("INFO: ");
printf("umount(): Wall time elapsed: %ld.%06ld seconds\n", v5[0], v5[1]);
printf("INFO: ");
puts("unmount(): destroying cache...");
return cache_destroy();
}

original_code:
void minix_unmount(void)
{
 clock_t cpu_start, cpu_end;
 struct timeval wall_start, wall_end, wall_elapsed;
 float cpu_elapsed;
 cpu_start = clock();
 gettimeofday(&wall_start, ((void *)0));
 ;
 put_inode(sb.root_inode);
 ;
 flush_inode_table();
 ;
 unload_bitmaps();
 ;
 sync_cache();
 cpu_end = clock();
 gettimeofday(&wall_end, ((void *)0));
 cpu_elapsed = ((float) (cpu_end - cpu_start)) / ((__clock_t) 1000000);
 timeval_subtract(&wall_elapsed, &wall_end, &wall_start);
 printf("INFO: "); printf("unmount(): CPU time elapsed: %f seconds" "\n", cpu_elapsed);;
 printf("INFO: "); printf("umount(): Wall time elapsed: %ld.%06ld seconds" "\n", (long) wall_elapsed.tv_sec, (long) wall_elapsed.tv_usec);;
 printf("INFO: "); printf("unmount(): destroying cache..." "\n");;
 cache_destroy();
}

canonical_original_code:
void minix_unmount()
{
 long cpu_start; long cpu_end;
 struct timeval wall_start; struct timeval wall_end; struct timeval wall_elapsed;
 float cpu_elapsed;
 cpu_start = clock();
 gettimeofday(&wall_start, ((void *)0));
 ;
 put_inode(sb.root_inode);
 ;
 flush_inode_table();
 ;
 unload_bitmaps();
 ;
 sync_cache();
 cpu_end = clock();
 gettimeofday(&wall_end, ((void *)0));
 cpu_elapsed = ((float) (cpu_end - cpu_start)) / ((long) 1000000);
 timeval_subtract(&wall_elapsed, &wall_end, &wall_start);
 printf("INFO: "); printf("unmount(): CPU time elapsed: %f seconds" "\n", cpu_elapsed);;
 printf("INFO: "); printf("umount(): Wall time elapsed: %ld.%06ld seconds" "\n", (long) wall_elapsed.tv_sec, (long) wall_elapsed.tv_usec);;
 printf("INFO: "); printf("unmount(): destroying cache..." "\n");;
 cache_destroy();
}

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


decompiled_code:
__int64 __fastcall short_array_add(__int16 a1, __int64 a2)
{
int v3; // eax
void *v4; // rdi
__int64 v5; // rdx
__int64 result; // rax
int v7; // eax
void *v8; // rax

v3 = *(_DWORD *)(a2 + 4);
v4 = *(void **)(a2 + 8);
if ( *(_DWORD *)a2 == v3 )
{
v7 = 2 * v3;
*(_DWORD *)a2 = v7;
v8 = realloc(v4, 2LL * v7);
*(_QWORD *)(a2 + 8) = v8;
v4 = v8;
v3 = *(_DWORD *)(a2 + 4);
}
v5 = v3;
result = (unsigned int)(v3 + 1);
*((_WORD *)v4 + v5) = a1;
*(_DWORD *)(a2 + 4) = result;
return result;
}

original_code:
void short_array_add(short s, struct short_array *array)
{
 if(array->capacity == array->size) {
  array->capacity *= 2;
  array->data = (short *) realloc(array->data,
   array->capacity * sizeof(short));
 }
 array->data[array->size] = s;
 array->size++;
}

variable_types: s short

variable_types: struct short_array { int capacity; int size; short *data; } *


decompiled_code:
void __fastcall short_array_destroy(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void short_array_destroy(struct short_array *array)
{
 free(array->data);
 free(array);
}

user_defined_types: struct short_array { int capacity; int size; short *data; }


original_code:
static int path_get_nth_cmpo(const char *path, char *buf, int n)
{
 int offset = 0;
 while(*(path + offset) == '/')
  offset++;
 for( ; *(path+offset) != '\0' && n != 0; ) {
  if(*(path+offset) == '/') {
   while(*(path+offset)=='/') offset++;
   n--;
   continue;
  }
  offset++;
 }
 if(n != 0) return 0;
 if(*(path+offset) == '\0') return 0;
 while(*(path+offset) != '/' && *(path+offset) != '\0') {
  *buf = *(path+offset);
  offset++;
  buf++;
 }
 *buf = '\0';
 return 1;
}

canonical_original_code:
static int path_get_nth_cmpo(char *path, char *buf, int n)
{
 int offset = 0;
 while(*(path + offset) == '/')
  offset++;
 for( ; *(path+offset) != '\0' && n != 0; ) {
  if(*(path+offset) == '/') {
   while(*(path+offset)=='/') offset++;
   n--;
   continue;
  }
  offset++;
 }
 if(n != 0) return 0;
 if(*(path+offset) == '\0') return 0;
 while(*(path+offset) != '/' && *(path+offset) != '\0') {
  *buf = *(path+offset);
  offset++;
  buf++;
 }
 *buf = '\0';
 return 1;
}

variable_types:
buf
char *

variable_types: n int

variable_types: offset int


decompiled_code:
__int64 __fastcall path_cnt_cmpos(char *a1)
{
char v1; // al
unsigned int v2; // er8
char v3; // al

v1 = *a1;
v2 = 0;
if ( *a1 )
{
while ( 1 )
{
if ( v1 == 47 )
{
do
v3 = *++a1;
while ( v3 == 47 );
if ( !v3 )
break;
}
++v2;
while ( 1 )
{
v1 = *++a1;
if ( v1 == 47 )
break;
if ( !v1 )
return v2;
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
char v1; // al
unsigned int v2; // er8
char v3; // al

v1 = *a1;
v2 = 0;
if ( *a1 )
{
while ( 1 )
{
if ( v1 == 47 )
{
do
v3 = *++a1;
while ( v3 == 47 );
if ( !v3 )
break;
}
++v2;
while ( 1 )
{
v1 = *++a1;
if ( v1 == 47 )
break;
if ( !v1 )
return v2;
}
}
}
return v2;
}

original_code:
int path_cnt_cmpos(const char *path)
{
 int i = 0;
 while(*path != '\0') {
  while(*path == '/' && *path != '\0')
   path++;
  if(*path != '\0') i++;
  while(*path != '/' && *path != '\0')
   path++;
 }
 return i;
}

canonical_original_code:
int path_cnt_cmpos(char *path)
{
 int i = 0;
 while(*path != '\0') {
  while(*path == '/' && *path != '\0')
   path++;
  if(*path != '\0') i++;
  while(*path != '/' && *path != '\0')
   path++;
 }
 return i;
}


decompiled_code:
__int64 __fastcall path_get_last_cmpo(char *a1, _BYTE *a2)
{
int v2; // eax
char *v3; // r9

v2 = path_cnt_cmpos(a1);
if ( v2 <= 0 )
return 0LL;
path_get_nth_cmpo(v3, a2, v2 - 1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, _BYTE *a2)
{
int v2; // eax
char *v3; // r9

v2 = func1(a1);
if ( v2 <= 0 )
return 0LL;
func0(v3, a2, v2 - 1);
return 1LL;
}

original_code:
int path_get_last_cmpo(const char *path, char *buf)
{
 int cnt = 0;
 if((cnt = path_cnt_cmpos(path)) > 0) {
  path_get_nth_cmpo(path, buf, cnt-1);
  return 1;
 }
 return 0;
}

canonical_original_code:
int path_get_last_cmpo(char *path, char *buf)
{
 int cnt = 0;
 if((cnt = path_cnt_cmpos(path)) > 0) {
  path_get_nth_cmpo(path, buf, cnt-1);
  return 1;
 }
 return 0;
}

variable_types: cnt int


decompiled_code:
__int64 __fastcall resolve_path(__int64 a1, char *a2, int a3)
{
int v4; // er12
__int64 v5; // rdi
__int64 v6; // r14
unsigned int v7; // ebp
unsigned __int16 v8; // bx
_BYTE v10[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = a3;
if ( a3 == -1 )
v4 = path_cnt_cmpos(a2);
v5 = *(unsigned __int16 *)(a1 + 32);
if ( !v4 )
return get_inode(v5);
v6 = get_inode(v5);
if ( v4 > 0 )
{
v7 = 0;
while ( 1 )
{
if ( !(unsigned int)path_get_nth_cmpo(a2, v10, v7) )
panic("resolve_path(): couldn't get component %d", v7);
v8 = dir_search(v6, v10);
if ( !v8 )
break;
put_inode(v6);
++v7;
v6 = get_inode(v8);
if ( v4 == v7 )
return v6;
}
put_inode(v6);
v6 = 0LL;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char *a2, int a3)
{
int v4; // er12
__int64 v5; // rdi
__int64 v6; // r14
unsigned int v7; // ebp
unsigned __int16 v8; // bx
_BYTE v10[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = a3;
if ( a3 == -1 )
v4 = func1(a2);
v5 = *(unsigned __int16 *)(a1 + 32);
if ( !v4 )
return get_inode(v5);
v6 = get_inode(v5);
if ( v4 > 0 )
{
v7 = 0;
while ( 1 )
{
if ( !(unsigned int)func0(a2, v10, v7) )
panic("resolve_path(): couldn't get component %d", v7);
v8 = dir_search(v6, v10);
if ( !v8 )
break;
put_inode(v6);
++v7;
v6 = get_inode(v8);
if ( v4 == v7 )
return v6;
}
put_inode(v6);
v6 = 0LL;
}
return v6;
}

original_code:
struct minix_inode *resolve_path(struct minix_inode *root, const char *path,
 int levels)
{
 char file[30];
 int i;
 struct minix_inode *inode;
 inode_nr i_num;
 if(levels == -1)
  levels = path_cnt_cmpos(path);
 if(levels == 0)
  return get_inode(root->i_num);
 inode = get_inode(root->i_num);
 for(i = 0; i < levels; i++) {
  if(!path_get_nth_cmpo(path, file, i)) {
   panic("resolve_path(): couldn't get component %d", i);
  }
  i_num = dir_search(inode, file);
  if(i_num == 0) {
   ;
   put_inode(inode);
   return ((void *)0);
  }
  put_inode(inode);
  inode = get_inode(i_num);
 }
 ;
 return inode;
}

canonical_original_code:
struct minix_inode *resolve_path(struct minix_inode *root, char *path, int levels)
{
 char file[30];
 int i;
 struct minix_inode *inode;
 unsigned short i_num;
 if(levels == -1)
  levels = path_cnt_cmpos(path);
 if(levels == 0)
  return get_inode(root->i_num);
 inode = get_inode(root->i_num);
 for(i = 0; i < levels; i++) {
  if(!path_get_nth_cmpo(path, file, i)) {
   panic("resolve_path(): couldn't get component %d", i);
  }
  i_num = dir_search(inode, file);
  if(i_num == 0) {
   ;
   put_inode(inode);
   return ((void *)0);
  }
  put_inode(inode);
  inode = get_inode(i_num);
 }
 ;
 return inode;
}

variable_types:
root
struct minix_inode *

variable_types:
path
char *

variable_types: levels int

variable_types:
file
char [30]


decompiled_code:
__int64 __fastcall last_dir(char *a1, _BYTE *a2)
{
char *v2; // r10
char *v3; // r10
int v4; // er11
__int64 result; // rax

path_cnt_cmpos(a1);
if ( (unsigned int)path_get_last_cmpo(v2, a2) )
result = resolve_path(sb[6], v3, v4 - 1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, _BYTE *a2)
{
char *v2; // r10
char *v3; // r10
int v4; // er11
__int64 result; // rax

func1(a1);
if ( (unsigned int)func2(v2, a2) )
result = func3(sb[6], v3, v4 - 1);
else
result = 0LL;
return result;
}

original_code:
struct minix_inode *last_dir(const char *path, char *buf)
{
 int n = path_cnt_cmpos(path);
 struct minix_inode *p_dir;
 if(path_get_last_cmpo(path, buf) == 0)
  return ((void *)0);
 p_dir = resolve_path(sb.root_inode, path, n - 1);
 return p_dir;
}

canonical_original_code:
struct minix_inode *last_dir(char *path, char *buf)
{
 int n = path_cnt_cmpos(path);
 struct minix_inode *p_dir;
 if(path_get_last_cmpo(path, buf) == 0)
  return ((void *)0);
 p_dir = resolve_path(sb.root_inode, path, n - 1);
 return p_dir;
}

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *


advance

decompiled_code:
__int64 __fastcall advance(__int64 a1, __int64 a2)
{
unsigned __int16 v2; // ax
__int64 result; // rax

v2 = dir_search(a1, a2);
if ( v2 )
result = get_inode(v2);
else
result = 0LL;
return result;
}

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

v2 = dir_search(a1, a2);
if ( v2 )
result = get_inode(v2);
else
result = 0LL;
return result;
}

original_code:
struct minix_inode *advance(struct minix_inode *p_dir, const char *filename)
{
 inode_nr i_num;
 struct minix_inode *retval;
 ;
 if((i_num = dir_search(p_dir, filename)) == 0) {
  ;
  return ((void *)0);
 }
 retval = get_inode(i_num);
 return retval;
}

canonical_original_code:
struct minix_inode *advance(struct minix_inode *p_dir, char *filename)
{
 unsigned short i_num;
 struct minix_inode *retval;
 ;
 if((i_num = dir_search(p_dir, filename)) == 0) {
  ;
  return ((void *)0);
 }
 retval = get_inode(i_num);
 return retval;
}

variable_types:
filename
char *


original_code:
zone_nr read_map(struct minix_inode *inode, int byte_offset)
{
 int rel_z = byte_offset / 1024;
 zone_nr z;
 struct minix_block *blk;
 int excess;
 if(rel_z < 7) {
  if((z = inode->i_zone[rel_z]) == 0)
   return 0;
  return z;
 }
 excess = rel_z - 7;
 if(excess < (1024/2)) {
  z = inode->i_zone[7];
 }
 else {
  if((z = inode->i_zone[7 +1]) == 0)
   return 0;
  excess -= (1024/2);
  blk = get_block(z, 1);
  z = ((u16 *)blk->blk_data)[excess/(1024/2)];
  excess = excess % (1024/2);
  put_block(blk, 2);
 }
 if(z == 0) return 0;
 blk = get_block(z, 1);
 z = ((u16 *)blk->blk_data)[excess];
 put_block(blk, 2);
 return z;
}

canonical_original_code:
unsigned short read_map(struct minix_inode *inode, int byte_offset)
{
 int rel_z = byte_offset / 1024;
 unsigned short z;
 struct minix_block *blk;
 int excess;
 if(rel_z < 7) {
  if((z = inode->i_zone[rel_z]) == 0)
   return 0;
  return z;
 }
 excess = rel_z - 7;
 if(excess < (1024/2)) {
  z = inode->i_zone[7];
 }
 else {
  if((z = inode->i_zone[7 +1]) == 0)
   return 0;
  excess -= (1024/2);
  blk = get_block(z, 1);
  z = ((unsigned short *)blk->blk_data)[excess/(1024/2)];
  excess = excess % (1024/2);
  put_block(blk, 2);
 }
 if(z == 0) return 0;
 blk = get_block(z, 1);
 z = ((unsigned short *)blk->blk_data)[excess];
 put_block(blk, 2);
 return z;
}

variable_types:
z
unsigned short

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; } *

variable_types: excess int

return_type: unsigned short


decompiled_code:
__int64 __fastcall dir_search(__int64 a1, const char *a2)
{
int i; // er15
unsigned int v3; // eax
unsigned int v4; // er12
__int64 v5; // r14
__int64 v6; // r13
__int64 v8; // [rsp+8h] [rbp-40h]

for ( i = 0; ; i += 1024 )
{
v3 = read_map(a1, i);
v4 = v3;
if ( !(_WORD)v3 )
break;
v5 = 0LL;
v6 = get_block((unsigned __int16)v3, 1LL);
v8 = *(_QWORD *)(v6 + 40);
do
{
if ( !strcmp((const char *)(v8 + 2 + v5), a2) )
{
v4 = *(unsigned __int16 *)(v8 + (int)v5);
put_block(v6, 1LL);
return v4;
}
v5 += 32LL;
}
while ( v5 != 1024 );
put_block(v6, 1LL);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
int i; // er15
unsigned int v3; // eax
unsigned int v4; // er12
__int64 v5; // r14
__int64 v6; // r13
__int64 v8; // [rsp+8h] [rbp-40h]

for ( i = 0; ; i += 1024 )
{
v3 = func0(a1, i);
v4 = v3;
if ( !(_WORD)v3 )
break;
v5 = 0LL;
v6 = get_block((unsigned __int16)v3, 1LL);
v8 = *(_QWORD *)(v6 + 40);
do
{
if ( !strcmp((const char *)(v8 + 2 + v5), a2) )
{
v4 = *(unsigned __int16 *)(v8 + (int)v5);
put_block(v6, 1LL);
return v4;
}
v5 += 32LL;
}
while ( v5 != 1024 );
put_block(v6, 1LL);
}
return v4;
}

original_code:
inode_nr dir_search(struct minix_inode *inode, const char *file)
{
 struct minix_block *blk;
 zone_nr z;
 int c_pos = 0;
 int i;
 inode_nr retval;
 ;
 while((z = read_map(inode, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    retval = *((inode_nr *)(blk->blk_data + i));
    put_block(blk, 1);
    ;
    return retval;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 ;
 return 0;
}

canonical_original_code:
unsigned short dir_search(struct minix_inode *inode, char *file)
{
 struct minix_block *blk;
 unsigned short z;
 int c_pos = 0;
 int i;
 unsigned short retval;
 ;
 while((z = read_map(inode, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    retval = *((unsigned short *)(blk->blk_data + i));
    put_block(blk, 1);
    ;
    return retval;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 ;
 return 0;
}

variable_types:
file
char *

variable_types: i int

variable_types:
retval
unsigned short


original_code:
int minix_read(struct minix_inode *inode, char *buf, size_t size, off_t offset)
{
 int sbytes = 0;
 int nbytes = size;
 int c_pos = offset;
 zone_nr z = 0;
 zone_nr z_data = 0;
 int z_offset = 0;
 int chunk = 0;
 struct minix_block *blk;
 if(nbytes > inode->i_size)
  nbytes = inode->i_size;
 while(nbytes > 0) {
  z = c_pos / 1024;
  z_offset = c_pos % 1024;
  chunk = ((nbytes) < ((1024 - z_offset)) ? (nbytes) : ((1024 - z_offset)));
  if((z_data = read_map(inode, z * 1024)) == 0)
   return sbytes;
  blk = get_block(z_data, 1);
  memcpy(buf + sbytes, blk->blk_data + z_offset, chunk);
  put_block(blk, 6);
  sbytes += chunk;
  nbytes -= chunk;
  c_pos += chunk;
 }
 return sbytes;
}

canonical_original_code:
int minix_read(struct minix_inode *inode, char *buf, unsigned long size, long offset)
{
 int sbytes = 0;
 int nbytes = size;
 int c_pos = offset;
 unsigned short z = 0;
 unsigned short z_data = 0;
 int z_offset = 0;
 int chunk = 0;
 struct minix_block *blk;
 if(nbytes > inode->i_size)
  nbytes = inode->i_size;
 while(nbytes > 0) {
  z = c_pos / 1024;
  z_offset = c_pos % 1024;
  chunk = ((nbytes) < ((1024 - z_offset)) ? (nbytes) : ((1024 - z_offset)));
  if((z_data = read_map(inode, z * 1024)) == 0)
   return sbytes;
  blk = get_block(z_data, 1);
  memcpy(buf + sbytes, blk->blk_data + z_offset, chunk);
  put_block(blk, 6);
  sbytes += chunk;
  nbytes -= chunk;
  c_pos += chunk;
 }
 return sbytes;
}

variable_types:
size
unsigned long

variable_types: offset long

variable_types: sbytes int

variable_types: nbytes int

variable_types: chunk int


decompiled_code:
int __fastcall bitmap_print(__int64 a1)
{
int result; // eax
unsigned __int64 v2; // rbx

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

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
unsigned __int64 v2; // rbx

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

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; } *


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: b int

variable_types: w int

variable_types: o int


decompiled_code:
__int64 __fastcall free_bit(__int64 a1, int a2)
{
int v3; // ebx
int v4; // ebp
int v5; // ebx
__int64 v6; // rdi
unsigned int v7; // er8
__int64 result; // rax

v3 = a2 + 0x1FFF;
v4 = a2 % 0x2000;
if ( a2 >= 0 )
v3 = a2;
v5 = v3 >> 13;
v6 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5) + 40LL);
if ( !(unsigned int)bit(v6, v4) )
{
panic("free_bit(%d): bit is already free", v7);
v6 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5) + 40LL);
}
clrbit_isra_0(v6, v4);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5);
*(_BYTE *)(result + 32) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
int v3; // ebx
int v4; // ebp
int v5; // ebx
__int64 v6; // rdi
unsigned int v7; // er8
__int64 result; // rax

v3 = a2 + 0x1FFF;
v4 = a2 % 0x2000;
if ( a2 >= 0 )
v3 = a2;
v5 = v3 >> 13;
v6 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5) + 40LL);
if ( !(unsigned int)func0(v6, v4) )
{
panic("free_bit(%d): bit is already free", v7);
v6 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5) + 40LL);
}
func1(v6, v4);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v5);
*(_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


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:
__int64 __fastcall setInterruptHandler(int a1, int a2, __int16 a3)
{
__int64 result; // rax

result = 16LL;
idt[4 * a1] = a2;
idt[4 * a1 + 1] = 16;
idt[4 * a1 + 2] = (a3 << 13) | 0x8E00;
idt[4 * a1 + 3] = HIWORD(a2);
return result;
}

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

result = 16LL;
idt[4 * a1] = a2;
idt[4 * a1 + 1] = 16;
idt[4 * a1 + 2] = (a3 << 13) | 0x8E00;
idt[4 * a1 + 3] = HIWORD(a2);
return result;
}

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

decompiled_code:
// attributes: thunk
__int64 __fastcall setTrapHandler(int a1, int a2, __int16 a3)
{
return setInterruptHandler(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, __int16 a3)
{
return func0(a1, a2, a3);
}

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);
setInterruptHandler(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);
func0(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
unsigned __int8 v1; // al
__int64 v2; // rax

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

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

v0 = current();
user_to_system(v0 + 40);
v1 = inb(0x60u);
if ( (v1 & 0x80u) == 0 )
printc_xy(24LL, 24LL, (unsigned int)char_map[v1]);
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)
{
_QWORD *v1; // rax

_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*v1 = 0LL;
v1[1] = 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 func5()
{
_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 }


decompiled_code:
__int64 __fastcall allocate_DIR(__int64 a1)
{
__int64 i; // rax
__int64 v2; // rdx
__int64 result; // rax

for ( i = 0LL; i != 10; ++i )
{
if ( !vdir[i] )
{
++vdir[(int)i];
v2 = (__int64)(int)i << 12;
*(_DWORD *)(a1 + 100) = i;
result = 1LL;
*(_QWORD *)(a1 + 8) = (char *)&dir_pages + v2;
return result;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
__int64 i; // rax
__int64 v2; // rdx
__int64 result; // rax

for ( i = 0LL; i != 10; ++i )
{
if ( !vdir[i] )
{
++vdir[(int)i];
v2 = (__int64)(int)i << 12;
*(_DWORD *)(a1 + 100) = i;
result = 1LL;
*(_QWORD *)(a1 + 8) = (char *)&dir_pages + v2;
return result;
}
}
return 0xFFFFFFFFLL;
}

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()
{
char *v0; // rax
int *v1; // rax

INIT_LIST_HEAD(&freequeue);
INIT_LIST_HEAD(&readyqueue);
do
{
*(_DWORD *)v0 = -1;
v0 += 24;
}
while ( v0 != (char *)&semafor + 240 );
v1 = (int *)(unsigned int)((_DWORD)&task + 16400);
do
{
list_add_constprop_0(v1);
*(v1 - 2028) = 3;
}
while ( v1 != &task + 20484 );
}

canonical_decompiled_code:
void func20()
{
char *v0; // rax
int *v1; // rax

func0(&freequeue);
func0(&readyqueue);
do
{
*(_DWORD *)v0 = -1;
v0 += 24;
}
while ( v0 != (char *)&semafor + 240 );
v1 = (int *)(unsigned int)((_DWORD)&task + 16400);
do
{
func7(v1);
*(v1 - 2028) = 3;
}
while ( v1 != &task + 20484 );
}

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()
{
__int64 result; // rax
__int64 v1; // rdx

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

canonical_decompiled_code:
__int64 func22()
{
__int64 result; // rax
__int64 v1; // rdx

current_quantum = *(_DWORD *)(func21() + 32);
result = func21();
*(_QWORD *)(result + 88) = v1;
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 __fastcall update_sched_data_rr(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = current(a1, a2, --current_quantum);
*(_QWORD *)(result + 88) = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = func21(a1, a2, --current_quantum);
*(_QWORD *)(result + 88) = v3;
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 __fastcall sched_next_rr(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 v4; // rdx
__int64 v6; // rdi
__int64 v7; // rbp
__int64 v8; // rdx

v3 = current(a1, a2, a3);
run_to_ready(v3 + 40);
if ( list_empty_constprop_0() )
{
ready_to_run((_QWORD *)(unsigned int)((_DWORD)task + 40));
*(_DWORD *)(current(task, a2, v4) + 96) = 1;
task[24] = 0;
}
else
{
v6 = readyqueue;
v7 = list_head_to_task_struct(readyqueue);
*(_DWORD *)(current(v6, a2, v8) + 96) = 1;
list_del(v7 + 16);
*(_DWORD *)(v7 + 96) = 0;
ready_to_run((_QWORD *)(v7 + 40));
actualitzar_quantum(v7);
current_quantum = *(_DWORD *)(v7 + 32);
}
return task_switch();
}

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 __fastcall reset_stats(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rbx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 result; // rax

*(_QWORD *)(current(a1, a2, a3) + 40) = 0LL;
*(_QWORD *)(current(a1, a2, v3) + 48) = 0LL;
*(_QWORD *)(current(a1, a2, v4) + 56) = 0LL;
*(_QWORD *)(current(a1, a2, v5) + 64) = 0LL;
v7 = current(a1, a2, v6);
*(_QWORD *)(v7 + 72) = get_ticks();
*(_QWORD *)(current(a1, a2, v8) + 80) = 0LL;
result = current(a1, a2, v9);
*(_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; // rdi
__int64 v2; // rdi
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rax
__int64 result; // rax

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

canonical_decompiled_code:
__int64 func27()
{
int v0; // eax
__int64 v1; // rdi
__int64 v2; // rdi
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rax
__int64 result; // rax

v0 = nextPID;
task[2048] = nextPID;
nextPID = v0 + 1;
func15((unsigned int)((_DWORD)task + 0x2000));
set_user_pages();
v1 = *(_QWORD *)&task[2050];
tss[1] = &task[4096];
set_cr3(v1);
v2 = (unsigned int)((_DWORD)task + 0x2000);
func18(v2, 18);
current_quantum = func17(v2);
func26(v2, 18LL, v3);
v5 = func21(v2, 18LL, v4);
result = func11((_QWORD *)(v5 + 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(_bss_start, format, arg);
fputc(10, _bss_start);
exit(1);
}

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

va_start(arg, format);
vfprintf(_bss_start, format, arg);
fputc(10, _bss_start);
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:
__int64 __fastcall minix_open(const char *a1)
{
int v1; // eax
__int64 result; // rax

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 = hdr_1;
if ( buf != hdr_1 || qword_4040E8 )
die("%s: Non-Minix header", a1);
if ( qword_4040D8 )
die("%s: Illegal data segment", (const char *)&buf);
if ( qword_4040E0 )
die("%s: Illegal bss segment", (const char *)&buf);
if ( qword_4040F8 )
die("%s: Illegal symbol table", (const char *)&buf);
return result;
}

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

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 = hdr_1;
if ( buf != hdr_1 || qword_4040E8 )
func7("%s: Non-Minix header", a1);
if ( qword_4040D8 )
func7("%s: Illegal data segment", (const char *)&buf);
if ( qword_4040E0 )
func7("%s: Illegal bss segment", (const char *)&buf);
if ( qword_4040F8 )
func7("%s: Illegal symbol table", (const char *)&buf);
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
unsigned int v4; // er14
unsigned int v5; // ebx
unsigned int v6; // ebp
int v7; // eax
const char *v8; // rsi
unsigned int v9; // ebx
char v10; // si
unsigned __int64 v11; // r12
unsigned int v13; // ebp
int v14; // eax
unsigned int v15; // [rsp+0h] [rbp-D8h]
__int16 v16; // [rsp+Ch] [rbp-CCh]
__int16 v17; // [rsp+Eh] [rbp-CAh]
struct stat stat_buf; // [rsp+10h] [rbp-C8h] BYREF

if ( argc != 4 )
usage(argc, argv, envp);
minix_open((char)argv[1]);
v3 = read(fd, &buf, 0x400uLL);
fprintf(_bss_start, "Boot sector %d bytes.\n", v3);
if ( v3 != 512 )
die("Boot block must be exactly 512 bytes", (char)"Boot sector %d bytes.\n");
if ( byte_4042BE != 85 || byte_4042BF != -86 )
die("Boot block hasn't got boot flag (0xAA55)", (char)"Boot sector %d bytes.\n");
word_4042BC = 0;
if ( write(1, &buf, 0x200uLL) != 512 )
die("Write call failed", (char)&buf);
close(fd);
fd = open(argv[2], 0, 0LL);
if ( fd < 0 )
die("Unable to open `%s': %m", (char)argv[2]);
if ( (unsigned int)fstat(fd, &stat_buf) )
die("Unable to stat `%s': %m", (char)argv[2]);
v4 = stat_buf.st_size;
v16 = stat_buf.st_size;
v5 = stat_buf.st_size;
fprintf(_bss_start, "System is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v4 )
{
while ( 1 )
{
v6 = 1024;
if ( v5 <= 0x400 )
v6 = v5;
v7 = read(fd, &buf, v6);
if ( v6 != v7 )
break;
if ( write(1, &buf, v6) != v6 )
LABEL_36:
die("Write failed", (char)&buf);
v5 -= v6;
if ( !v5 )
goto LABEL_17;
}
v8 = argv[2];
if ( v7 < 0 )
LABEL_35:
die("Error reading %s: %m", (char)v8);
LABEL_16:
die("%s: Unexpected EOF", (char)v8);
}
LABEL_17:
close(fd);
fd = open(argv[3], 0, 0LL);
if ( fd < 0 )
die("Unable to open `%s': %m", (char)argv[3]);
if ( (unsigned int)fstat(fd, &stat_buf) )
die("Unable to stat `%s': %m", (char)argv[3]);
v9 = stat_buf.st_size;
v17 = stat_buf.st_size;
v15 = stat_buf.st_size;
fprintf(_bss_start, "User is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v9 )
{
while ( 1 )
{
v13 = 1024;
if ( v9 <= 0x400 )
v13 = v9;
v14 = read(fd, &buf, v13);
if ( v13 != v14 )
break;
if ( write(1, &buf, v13) != v13 )
goto LABEL_36;
v9 -= v13;
if ( !v9 )
goto LABEL_20;
}
v8 = argv[3];
if ( v14 < 0 )
goto LABEL_35;
goto LABEL_16;
}
LABEL_20:
close(fd);
v10 = -12;
if ( lseek(1, 500LL, 0) != 500 )
goto LABEL_45;
v11 = v4 + (unsigned __int64)v15 + 15;
fprintf(_bss_start, "Image is %d kB\n", (unsigned int)((int)(v4 + v15) / 1024));
BYTE1(buf) = v11 >> 12;
LOBYTE(buf) = v11 >> 4;
if ( write(1, &buf, 2uLL) != 2 )
die("Write of image length failed", (char)&buf);
v10 = 0;
if ( lseek(1, 512LL, 0) != 512 )
goto LABEL_45;
LOWORD(buf) = v16;
WORD1(buf) = BYTE2(v4);
if ( write(1, &buf, 4uLL) != 4 )
die("Write of system length failed", (char)&buf);
v10 = 4;
if ( lseek(1, 516LL, 0) != 516 )
goto LABEL_45;
LOWORD(buf) = v17;
WORD1(buf) = BYTE2(v15);
if ( write(1, &buf, 4uLL) != 4 )
goto LABEL_48;
v10 = 8;
if ( lseek(1, 520LL, 0) != 520 )
LABEL_45:
die("Output: seek failed", v10);
LODWORD(buf) = 32218;
if ( write(1, &buf, 4uLL) != 4 )
LABEL_48:
die("Write of user length failed", (char)&buf);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
unsigned int v4; // er14
unsigned int v5; // ebx
unsigned int v6; // ebp
int v7; // eax
const char *v8; // rsi
unsigned int v9; // ebx
char v10; // si
unsigned __int64 v11; // r12
unsigned int v13; // ebp
int v14; // eax
unsigned int v15; // [rsp+0h] [rbp-D8h]
__int16 v16; // [rsp+Ch] [rbp-CCh]
__int16 v17; // [rsp+Eh] [rbp-CAh]
struct stat stat_buf; // [rsp+10h] [rbp-C8h] BYREF

if ( argc != 4 )
func9(argc, argv, envp);
func8((char)argv[1]);
v3 = read(fd, &buf, 0x400uLL);
fprintf(_bss_start, "Boot sector %d bytes.\n", v3);
if ( v3 != 512 )
func7("Boot block must be exactly 512 bytes", (char)"Boot sector %d bytes.\n");
if ( byte_4042BE != 85 || byte_4042BF != -86 )
func7("Boot block hasn't got boot flag (0xAA55)", (char)"Boot sector %d bytes.\n");
word_4042BC = 0;
if ( write(1, &buf, 0x200uLL) != 512 )
func7("Write call failed", (char)&buf);
close(fd);
fd = open(argv[2], 0, 0LL);
if ( fd < 0 )
func7("Unable to open `%s': %m", (char)argv[2]);
if ( (unsigned int)func12(fd, &stat_buf) )
func7("Unable to stat `%s': %m", (char)argv[2]);
v4 = stat_buf.st_size;
v16 = stat_buf.st_size;
v5 = stat_buf.st_size;
fprintf(_bss_start, "System is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v4 )
{
while ( 1 )
{
v6 = 1024;
if ( v5 <= 0x400 )
v6 = v5;
v7 = read(fd, &buf, v6);
if ( v6 != v7 )
break;
if ( write(1, &buf, v6) != v6 )
LABEL_36:
func7("Write failed", (char)&buf);
v5 -= v6;
if ( !v5 )
goto LABEL_17;
}
v8 = argv[2];
if ( v7 < 0 )
LABEL_35:
func7("Error reading %s: %m", (char)v8);
LABEL_16:
func7("%s: Unexpected EOF", (char)v8);
}
LABEL_17:
close(fd);
fd = open(argv[3], 0, 0LL);
if ( fd < 0 )
func7("Unable to open `%s': %m", (char)argv[3]);
if ( (unsigned int)func12(fd, &stat_buf) )
func7("Unable to stat `%s': %m", (char)argv[3]);
v9 = stat_buf.st_size;
v17 = stat_buf.st_size;
v15 = stat_buf.st_size;
fprintf(_bss_start, "User is %d kB\n", LODWORD(stat_buf.st_size) >> 10);
if ( v9 )
{
while ( 1 )
{
v13 = 1024;
if ( v9 <= 0x400 )
v13 = v9;
v14 = read(fd, &buf, v13);
if ( v13 != v14 )
break;
if ( write(1, &buf, v13) != v13 )
goto LABEL_36;
v9 -= v13;
if ( !v9 )
goto LABEL_20;
}
v8 = argv[3];
if ( v14 < 0 )
goto LABEL_35;
goto LABEL_16;
}
LABEL_20:
close(fd);
v10 = -12;
if ( lseek(1, 500LL, 0) != 500 )
goto LABEL_45;
v11 = v4 + (unsigned __int64)v15 + 15;
fprintf(_bss_start, "Image is %d kB\n", (unsigned int)((int)(v4 + v15) / 1024));
BYTE1(buf) = v11 >> 12;
LOBYTE(buf) = v11 >> 4;
if ( write(1, &buf, 2uLL) != 2 )
func7("Write of image length failed", (char)&buf);
v10 = 0;
if ( lseek(1, 512LL, 0) != 512 )
goto LABEL_45;
LOWORD(buf) = v16;
WORD1(buf) = BYTE2(v4);
if ( write(1, &buf, 4uLL) != 4 )
func7("Write of system length failed", (char)&buf);
v10 = 4;
if ( lseek(1, 516LL, 0) != 516 )
goto LABEL_45;
LOWORD(buf) = v17;
WORD1(buf) = BYTE2(v15);
if ( write(1, &buf, 4uLL) != 4 )
goto LABEL_48;
v10 = 8;
if ( lseek(1, 520LL, 0) != 520 )
LABEL_45:
func7("Output: seek failed", v10);
LODWORD(buf) = 32218;
if ( write(1, &buf, 4uLL) != 4 )
LABEL_48:
func7("Write of user length failed", (char)&buf);
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; // rbp
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; // rbp
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
__int64 v3; // rbp
__int64 v4; // rdi

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

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

if ( a2 > 0 )
{
v2 = a1;
v3 = (__int64)&a1[a2 - 1 + 1];
do
{
v4 = (unsigned int)*v2++;
printc(v4);
}
while ( v2 != (char *)v3 );
}
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:
// local variable allocation has failed, the output may be wrong!
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
bool v3; // al
key_t v4; // ebp
__pid_t v5; // ebx
_DWORD *v6; // rax
__int64 v7; // rsi
__int64 v8; // rdi
bool v9; // cc
bool v10; // zf

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

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
bool v3; // al
key_t v4; // ebp
__pid_t v5; // ebx
_DWORD *v6; // rax
__int64 v7; // rsi
__int64 v8; // rdi
bool v9; // cc
bool v10; // zf

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

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 *[]


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_12:
printf("Sig handled when it shouldn't be: %d\n", (unsigned int)a1);
return;
}
if ( a1 != 14 )
goto LABEL_12;
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_12:
printf("Sig handled when it shouldn't be: %d\n", (unsigned int)a1);
return;
}
if ( a1 != 14 )
goto LABEL_12;
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);
}


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