decompiled_code:
__int64 __fastcall my_open(const char *a1, int a2)
{
unsigned int v2; // er12

if ( a1 )
{
v2 = open(a1, a2);
if ( v2 == -1 )
{
my_putstr(a1);
print_errno(3LL);
}
}
else
{
v2 = -1;
print_errno(1LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, int a2)
{
unsigned int v2; // er12

if ( a1 )
{
v2 = open(a1, a2);
if ( v2 == -1 )
{
my_putstr(a1);
print_errno(3LL);
}
}
else
{
v2 = -1;
print_errno(1LL);
}
return v2;
}

original_code:
int my_open(char *path, int flags)
{
  int fd;
  if (!path)
    {
      print_errno(1);
      return (-1);
    }
  if ((fd = open(path, flags)) == -1)
    {
      my_putstr(path);
      print_errno(3);
      return (-1);
    }
  else
    return (fd);
}

variable_types:
path
char *

variable_types: flags int


decompiled_code:
__int64 __fastcall my_create(const char *a1, int a2)
{
unsigned int v2; // er12

if ( a1 )
{
v2 = open(a1, a2 | 0x40);
if ( v2 == -1 )
print_errno(4LL);
}
else
{
v2 = -1;
print_errno(1LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2)
{
unsigned int v2; // er12

if ( a1 )
{
v2 = open(a1, a2 | 0x40);
if ( v2 == -1 )
print_errno(4LL);
}
else
{
v2 = -1;
print_errno(1LL);
}
return v2;
}

original_code:
int my_create(char *path, int flags, int mode)
{
  int fd;
  if (!path)
    {
      print_errno(1);
      return (-1);
    }
  if ((fd = open(path, flags | 0100, mode)) == -1)
    {
      print_errno(4);
      return (-1);
    }
  else
    return (fd);
}

variable_types: mode int

variable_types: fd int

return_type: int


decompiled_code:
int __fastcall my_close(int a1)
{
int result; // eax

if ( a1 < 0 || (result = close(a1), result == -1) )
{
print_errno(5LL);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int result; // eax

if ( a1 < 0 || (result = close(a1), result == -1) )
{
print_errno(5LL);
result = -1;
}
return result;
}

original_code:
int my_close(int fd)
{
  int ret;
  if (fd < 0 || (ret = close(fd)) == -1)
    {
      print_errno(5);
      return (-1);
    }
  return (ret);
}

variable_types: ret int


original_code:
char *my_stradd(char *str1, char *str2)
{
  char *ret;
  int i;
  int j;
  if (!(ret = my_malloc(my_strlen(str1) + my_strlen(str2) + 1)))
    return (ret);
  i = 0;
  while (str1 && str1[i] != '\0')
    {
      ret[i] = str1[i];
      i++;
    }
  j = 0;
  while (str2 && str2[j] != '\0')
    {
      ret[i + j] = str2[j];
      j++;
    }
  return (ret);
}

variable_types:
ret
char *

variable_types: i int

variable_types: j int

return_type: char *


decompiled_code:
int __fastcall mk_subcol(int a1, int a2, int a3, int a4)
{
int result; // eax

if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a4 - 1, a2) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), HIDWORD(acs_map[59]));
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a4, a2) != -1 )
wvline(*((WINDOW **)&g_ctrl + 7 * a1), 0LL, LINES - 4);
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a3, a2) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[55]));
result = wmove(*((WINDOW **)&g_ctrl + 7 * a1), LINES - 4, a2);
if ( result != -1 )
result = waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[59]));
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, int a4)
{
int result; // eax

if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a4 - 1, a2) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), HIDWORD(acs_map[59]));
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a4, a2) != -1 )
wvline(*((WINDOW **)&g_ctrl + 7 * a1), 0LL, LINES - 4);
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a3, a2) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[55]));
result = wmove(*((WINDOW **)&g_ctrl + 7 * a1), LINES - 4, a2);
if ( result != -1 )
result = waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[59]));
return result;
}

original_code:
void mk_subcol(int menu, int col, int inter, int start)
{
  (wmove((g_ctrl.menu[menu].win),(start - 1),(col)) == (-1) ? (-1) : waddch((g_ctrl.menu[menu].win),((acs_map[(unsigned char)(('w'))]))));
  (wmove((g_ctrl.menu[menu].win),(start),(col)) == (-1) ? (-1) : wvline((g_ctrl.menu[menu].win),(0),((LINES - 2 - 1) - 1)));
  (wmove((g_ctrl.menu[menu].win),(inter),(col)) == (-1) ? (-1) : waddch((g_ctrl.menu[menu].win),((acs_map[(unsigned char)(('n'))]))));
  (wmove((g_ctrl.menu[menu].win),((LINES - 2 - 1) - 1),(col)) == (-1) ? (-1) : waddch((g_ctrl.menu[menu].win),((acs_map[(unsigned char)(('v'))]))));
}

variable_types: menu int

variable_types: col int

variable_types: inter int

variable_types: start int

return_type: void


decompiled_code:
int __fastcall mk_subline(int a1, int a2)
{
int result; // eax

if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, 0) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[58]));
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, 1) != -1 )
whline(*((WINDOW **)&g_ctrl + 7 * a1), 0LL, (COLS - 5) / 2);
result = wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, (COLS - 5) / 2 - 1);
if ( result != -1 )
result = waddch(*((WINDOW **)&g_ctrl + 7 * a1), HIDWORD(acs_map[58]));
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2)
{
int result; // eax

if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, 0) != -1 )
waddch(*((WINDOW **)&g_ctrl + 7 * a1), LODWORD(acs_map[58]));
if ( wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, 1) != -1 )
whline(*((WINDOW **)&g_ctrl + 7 * a1), 0LL, (COLS - 5) / 2);
result = wmove(*((WINDOW **)&g_ctrl + 7 * a1), a2, (COLS - 5) / 2 - 1);
if ( result != -1 )
result = waddch(*((WINDOW **)&g_ctrl + 7 * a1), HIDWORD(acs_map[58]));
return result;
}

original_code:
void mk_subline(int menu, int line)
{
  (wmove((g_ctrl.menu[menu].win),(line),(0)) == (-1) ? (-1) : waddch((g_ctrl.menu[menu].win),((acs_map[(unsigned char)(('t'))]))));
  (wmove((g_ctrl.menu[menu].win),(line),(1)) == (-1) ? (-1) : whline((g_ctrl.menu[menu].win),(0),(((COLS - 4 - 1) / 2))));
  (wmove((g_ctrl.menu[menu].win),(line),(((COLS - 4 - 1) / 2) - 1)) == (-1) ? (-1) : waddch((g_ctrl.menu[menu].win),((acs_map[(unsigned char)(('u'))]))));
}

variable_types: line int


mkbox

decompiled_code:
int mkbox()
{
WINDOW **v0; // rbp
int v1; // ebx
WINDOW *v2; // rdi
int result; // eax

v0 = (WINDOW **)&g_ctrl;
v1 = 0;
wrefresh(stdscr);
while ( 1 )
{
v2 = *v0;
v0 += 7;
wborder(v2, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
mk_subline(v1, 2);
mk_subline(v1, 4);
mk_subcol(v1, (COLS - 5) / 2 - 13, 4, 3);
result = mk_subcol(v1, 12, 4, 3);
if ( v1 == 1 )
break;
v1 = 1;
}
return result;
}

canonical_decompiled_code:
int func2()
{
WINDOW **v0; // rbp
int v1; // ebx
WINDOW *v2; // rdi
int result; // eax

v0 = (WINDOW **)&g_ctrl;
v1 = 0;
wrefresh(stdscr);
while ( 1 )
{
v2 = *v0;
v0 += 7;
wborder(v2, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
func1(v1, 2);
func1(v1, 4);
func0(v1, (COLS - 5) / 2 - 13, 4, 3);
result = func0(v1, 12, 4, 3);
if ( v1 == 1 )
break;
v1 = 1;
}
return result;
}

original_code:
void mkbox()
{
  int i;
  i = 0;
  wrefresh(stdscr);
  while (i < 2)
    {
      wborder(g_ctrl.menu[i].win, 0, 0, 0, 0, 0, 0, 0, 0);
      mk_subline(i, 2);
      mk_subline(i, 4);
      mk_subcol(i, ((COLS - 4 - 1) / 2) - 13 , 4, 3);
      mk_subcol(i, 11 + 1, 4, 3);
      i++;
    }
}


decompiled_code:
int __fastcall printw_error(const char *a1)
{
wattr_on(stdscr, 0x800uLL, 0LL);
mvprintw(LINES - 1, 0, "%s", a1);
return wattr_off(stdscr, 0x800uLL, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
wattr_on(stdscr, 0x800uLL, 0LL);
mvprintw(LINES - 1, 0, "%s", a1);
return wattr_off(stdscr, 0x800uLL, 0LL);
}

variable_types:
str
char *


variable_types: struct s_item { char right[11]; char *name; char **pwd; 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]; } s_stat; int color; } *

variable_types: c char

user_defined_types: struct s_item { char right[11]; char *name; char **pwd; struct stat s_stat; int color; }

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


newitem

decompiled_code:
__int64 __fastcall newitem(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
char *v7; // rbp

v2 = my_malloc(184LL);
if ( !v2 )
return 0LL;
v3 = v2;
v4 = my_strdup(a1);
*(_QWORD *)(v3 + 16) = v4;
if ( !v4 )
return 0LL;
*(_QWORD *)(v3 + 24) = a2;
v5 = creat_path(a2);
v6 = my_stradd(v5, &unk_171);
v7 = (char *)my_stradd(v6, a1);
stat(v7, (struct stat *)(v3 + 32));
select_attr(v3);
fill_right(v3);
free(v7);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
char *v7; // rbp

v2 = my_malloc(184LL);
if ( !v2 )
return 0LL;
v3 = v2;
v4 = my_strdup(a1);
*(_QWORD *)(v3 + 16) = v4;
if ( !v4 )
return 0LL;
*(_QWORD *)(v3 + 24) = a2;
v5 = creat_path(a2);
v6 = my_stradd(v5, &unk_171);
v7 = (char *)my_stradd(v6, a1);
stat(v7, (struct stat *)(v3 + 32));
func0(v3);
func1(v3);
free(v7);
return v3;
}

original_code:
t_item *newitem(char *name, char **src_path)
{
  t_item *item;
  char *temp;
  if ((item = my_malloc(sizeof(*item))) == ((void *)0))
    return (((void *)0));
  if ((item->name = my_strdup(name)) == ((void *)0))
    return (((void *)0));
  item->pwd = src_path;
  temp = creat_path(src_path);
  temp = my_stradd(temp, "/");
  temp = my_stradd(temp, name);
  stat(temp, &item->s_stat);
  select_attr(item);
  fill_right(item);
  free(temp);
  return (item);
}

canonical_original_code:
struct s_item *newitem(char *name, char **src_path)
{
  struct s_item *item;
  char *temp;
  if ((item = my_malloc(sizeof(*item))) == ((void *)0))
    return (((void *)0));
  if ((item->name = my_strdup(name)) == ((void *)0))
    return (((void *)0));
  item->pwd = src_path;
  temp = creat_path(src_path);
  temp = my_stradd(temp, "/");
  temp = my_stradd(temp, name);
  stat(temp, &item->s_stat);
  select_attr(item);
  fill_right(item);
  free(temp);
  return (item);
}

variable_types:
name
char *

variable_types:
temp
char *

return_type: struct s_item { char right[11]; char *name; char **pwd; 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]; } s_stat; int color; } *


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

v1 = g_attrtab[0];
if ( g_attrtab[0] )
v1 = (g_attrtab[0] & *(_DWORD *)(a1 + 56)) != g_attrtab[0];
*(_DWORD *)(a1 + 176) = g_attrtab[2 * v1 + 1];
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax

v1 = g_attrtab[0];
if ( g_attrtab[0] )
v1 = (g_attrtab[0] & *(_DWORD *)(a1 + 56)) != g_attrtab[0];
*(_DWORD *)(a1 + 176) = g_attrtab[2 * v1 + 1];
return 1LL;
}

original_code:
t_bool select_attr(t_item *item)
{
  int i;
  i = 0;
  while (g_attrtab[i].cmp != 0 &&
  (int)(item->s_stat.st_mode & g_attrtab[i].cmp) != g_attrtab[i].cmp)
    i++;
  item->color = g_attrtab[i].color;
  return (1);
}

canonical_original_code:
unsigned char select_attr(struct s_item *item)
{
  int i;
  i = 0;
  while (g_attrtab[i].cmp != 0 &&
  (int)(item->s_stat.st_mode & g_attrtab[i].cmp) != g_attrtab[i].cmp)
    i++;
  item->color = g_attrtab[i].color;
  return (1);
}

return_type: unsigned char

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
void __fastcall my_nputnstr(_BYTE *a1, int a2, int a3)
{
chtype v5; // rsi
_BYTE *v6; // r15
int v7; // ebx

if ( a1 )
{
v5 = (unsigned int)(char)*a1;
if ( *a1 )
{
if ( a3 < 0 )
return;
v6 = a1 + 1;
v7 = 0;
while ( 1 )
{
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), v5);
v5 = (unsigned int)(char)*v6;
if ( !*v6 )
break;
++v6;
if ( a3 < v7 )
return;
}
}
else
{
v7 = 0;
}
while ( a3 >= v7 )
{
while ( a3 + 1 != v7 || (int)my_strlen(a1) <= a3 )
{
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), 0x20uLL);
if ( a3 < v7 )
return;
}
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), 0x7EuLL);
}
}
}

canonical_decompiled_code:
void __fastcall func1(_BYTE *a1, int a2, int a3)
{
chtype v5; // rsi
_BYTE *v6; // r15
int v7; // ebx

if ( a1 )
{
v5 = (unsigned int)(char)*a1;
if ( *a1 )
{
if ( a3 < 0 )
return;
v6 = a1 + 1;
v7 = 0;
while ( 1 )
{
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), v5);
v5 = (unsigned int)(char)*v6;
if ( !*v6 )
break;
++v6;
if ( a3 < v7 )
return;
}
}
else
{
v7 = 0;
}
while ( a3 >= v7 )
{
while ( a3 + 1 != v7 || (int)my_strlen(a1) <= a3 )
{
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), 0x20uLL);
if ( a3 < v7 )
return;
}
++v7;
waddch(*((WINDOW **)&g_ctrl + 7 * a2), 0x7EuLL);
}
}
}

original_code:
void my_nputnstr(char *str, int win, int max)
{
  int i;
  if (!str)
    return ;
  i = 0;
  while (str[i] != '\0' && i <= max)
    {
      waddch(g_ctrl.menu[win].win, (chtype) str[i]);
      i++;
    }
  while (i <= max)
    {
      if (i == max + 1 && my_strlen(str) > max)
 waddch(g_ctrl.menu[win].win, (chtype) '~');
      else
 waddch(g_ctrl.menu[win].win, (chtype) ' ');
      i++;
    }
}

canonical_original_code:
void my_nputnstr(char *str, int win, int max)
{
  int i;
  if (!str)
    return ;
  i = 0;
  while (str[i] != '\0' && i <= max)
    {
      waddch(g_ctrl.menu[win].win, (unsigned int) str[i]);
      i++;
    }
  while (i <= max)
    {
      if (i == max + 1 && my_strlen(str) > max)
 waddch(g_ctrl.menu[win].win, (unsigned int) '~');
      else
 waddch(g_ctrl.menu[win].win, (unsigned int) ' ');
      i++;
    }
}

variable_types: win int

variable_types: max int


printfield

original_code:
void printfield(int line, t_item *item, int win)
{
  char buff[12];
  char *name;
  char *temp;
  my_bzero(buff, 11);
  strftime(buff, 12, "%a %d %Y", localtime(&item->s_stat.st_mtime));
  name = item->name;
  if (((((item->s_stat.st_mode)) & 0170000) == (0040000)))
    temp = "/";
  else
    temp = " ";
  if ((name = my_stradd(temp, name)) == ((void *)0))
    return ;
  wmove(g_ctrl.menu[win].win, line, 1);
  my_nputnstr(item->right, win, 11 - 3);
  wmove(g_ctrl.menu[win].win, line, 2 + 11);
  my_nputnstr(name, win, ((COLS - 4 - 1) / 2) - (2 + 1) -
       (11 + 1) - (11 + 1));
  wmove(g_ctrl.menu[win].win, line, ((COLS - 4 - 1) / 2) - (11 + 1));
  my_nputnstr(buff, win, 11 - 1);
  free(name);
}

canonical_original_code:
void printfield(int line, struct s_item *item, int win)
{
  char buff[12];
  char *name;
  char *temp;
  my_bzero(buff, 11);
  strftime(buff, 12, "%a %d %Y", localtime(&item->s_stat.st_mtime));
  name = item->name;
  if (((((item->s_stat.st_mode)) & 0170000) == (0040000)))
    temp = "/";
  else
    temp = " ";
  if ((name = my_stradd(temp, name)) == ((void *)0))
    return ;
  wmove(g_ctrl.menu[win].win, line, 1);
  my_nputnstr(item->right, win, 11 - 3);
  wmove(g_ctrl.menu[win].win, line, 2 + 11);
  my_nputnstr(name, win, ((COLS - 4 - 1) / 2) - (2 + 1) -
       (11 + 1) - (11 + 1));
  wmove(g_ctrl.menu[win].win, line, ((COLS - 4 - 1) / 2) - (11 + 1));
  my_nputnstr(buff, win, 11 - 1);
  free(name);
}

variable_types: struct s_item { char right[11]; char *name; char **pwd; 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; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; } s_stat; int color; } *

variable_types:
buff
char [12]


decompiled_code:
__int64 __fastcall printfield_name(int a1, __int64 a2)
{
__int64 v2; // rbx
_BYTE *v3; // r13
__int64 result; // rax

v2 = 56LL * a1;
wattr_on(*(WINDOW **)((char *)&g_ctrl + v2), 0x200000uLL, 0LL);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, 1);
my_nputnstr("Right", a1, 9);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, 13);
my_nputnstr("Name", a1, (COLS - 5) / 2 - 27);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, (COLS - 5) / 2 - 12);
my_nputnstr("Last Modify", a1, 10);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 1, 1);
v3 = (_BYTE *)creat_path(a2);
result = 0LL;
if ( v3 )
{
my_nputnstr(v3, a1, (COLS - 5) / 2 - 3);
if ( (unsigned int)my_strlen(v3) != 1 )
free(v3);
wattr_off(*((WINDOW **)&g_ctrl + 7 * a1), 0x200000uLL, 0LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2)
{
__int64 v2; // rbx
_BYTE *v3; // r13
__int64 result; // rax

v2 = 56LL * a1;
wattr_on(*(WINDOW **)((char *)&g_ctrl + v2), 0x200000uLL, 0LL);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, 1);
func1("Right", a1, 9);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, 13);
func1("Name", a1, (COLS - 5) / 2 - 27);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 3, (COLS - 5) / 2 - 12);
func1("Last Modify", a1, 10);
wmove(*(WINDOW **)((char *)&g_ctrl + v2), 1, 1);
v3 = (_BYTE *)creat_path(a2);
result = 0LL;
if ( v3 )
{
func1(v3, a1, (COLS - 5) / 2 - 3);
if ( (unsigned int)my_strlen(v3) != 1 )
free(v3);
wattr_off(*((WINDOW **)&g_ctrl + 7 * a1), 0x200000uLL, 0LL);
result = 1LL;
}
return result;
}

original_code:
t_bool printfield_name(int win, char **path)
{
  char *pwd;
  wattr_on(g_ctrl.menu[win].win, (attr_t)(((chtype)((1U)) << ((13) + 8))), ((void *)0));
  wmove(g_ctrl.menu[win].win, 3, 1);
  my_nputnstr("Right", win, 11 - 2);
  wmove(g_ctrl.menu[win].win, 3, 1 + 11 + 1);
  my_nputnstr("Name", win, ((COLS - 4 - 1) / 2) - (2 + 1) - (11 + 1)
       - (11 + 1));
  wmove(g_ctrl.menu[win].win, 3, ((COLS - 4 - 1) / 2) - (11 + 1));
  my_nputnstr("Last Modify", win, 11 - 1);
  wmove(g_ctrl.menu[win].win, 1, 1);
  if ((pwd = creat_path(path)) == 0)
    return (0);
  my_nputnstr(pwd, win, ((COLS - 4 - 1) / 2) - 3);
  if (my_strlen(pwd) != 1)
      free(pwd);
  wattr_off(g_ctrl.menu[win].win, (attr_t)(((chtype)((1U)) << ((13) + 8))), ((void *)0));
  return (1);
}

canonical_original_code:
unsigned char printfield_name(int win, char **path)
{
  char *pwd;
  wattr_on(g_ctrl.menu[win].win, (attr_t)(((chtype)((1U)) << ((13) + 8))), ((void *)0));
  wmove(g_ctrl.menu[win].win, 3, 1);
  my_nputnstr("Right", win, 11 - 2);
  wmove(g_ctrl.menu[win].win, 3, 1 + 11 + 1);
  my_nputnstr("Name", win, ((COLS - 4 - 1) / 2) - (2 + 1) - (11 + 1)
       - (11 + 1));
  wmove(g_ctrl.menu[win].win, 3, ((COLS - 4 - 1) / 2) - (11 + 1));
  my_nputnstr("Last Modify", win, 11 - 1);
  wmove(g_ctrl.menu[win].win, 1, 1);
  if ((pwd = creat_path(path)) == 0)
    return (0);
  my_nputnstr(pwd, win, ((COLS - 4 - 1) / 2) - 3);
  if (my_strlen(pwd) != 1)
      free(pwd);
  wattr_off(g_ctrl.menu[win].win, (attr_t)(((chtype)((1U)) << ((13) + 8))), ((void *)0));
  return (1);
}

variable_types:
path
char **

variable_types:
pwd
char *


decompiled_code:
__int64 __fastcall print_list(int a1)
{
int v2; // er13
_QWORD *v3; // r12
__int64 v4; // r14
int v5; // ebx
int i; // eax
unsigned int v7; // ebp
int v8; // edi
int v9; // eax
__int64 v10; // rsi
__int64 result; // rax

v2 = 0;
v3 = g_ctrl;
while ( 1 )
{
v4 = v3[1];
v5 = 0;
for ( i = *((_DWORD *)v3 + 7); *(_QWORD *)(v4 + 8LL * i); i = v5 + *((_DWORD *)v3 + 7) )
{
if ( g_ctrl[8] <= i )
break;
v7 = select_attr(a1, v2, v5);
wattr_on((WINDOW *)*v3, v7, 0LL);
v8 = v5 + 5;
v9 = v5 + *((_DWORD *)v3 + 7);
++v5;
printfield(v8, *(_QWORD *)(v4 + 8LL * v9), v2);
wattr_off((WINDOW *)*v3, v7, 0LL);
}
v10 = v3[2];
v3 += 7;
result = printfield_name(v2, v10);
if ( v2 == 1 )
break;
v2 = 1;
}
return result;
}

original_code:
void print_list(int focus)
{
  int i;
  int j;
  t_item **itab;
  int attr;
  i = 0;
  while (i < 2)
    {
      j = 0;
      itab = g_ctrl.menu[i].list;
      while (itab[g_ctrl.menu[i].limit[0]+ j] != ((void *)0) &&
      g_ctrl.menu[i].limit[0]+ j < g_ctrl.menu[0].limit[1])
 {
   attr = select_attr(focus, i, j);
   wattr_on(g_ctrl.menu[i].win, (attr_t)(attr), ((void *)0));
   printfield(j + 5, itab[g_ctrl.menu[i].limit[0] + j], i);
   wattr_off(g_ctrl.menu[i].win, (attr_t)(attr), ((void *)0));
   j++;
 }
      printfield_name(i, g_ctrl.menu[i].path);
      i++;
    }
}

canonical_original_code:
void print_list(int focus)
{
  int i;
  int j;
  struct s_item **itab;
  int attr;
  i = 0;
  while (i < 2)
    {
      j = 0;
      itab = g_ctrl.menu[i].list;
      while (itab[g_ctrl.menu[i].limit[0]+ j] != ((void *)0) &&
      g_ctrl.menu[i].limit[0]+ j < g_ctrl.menu[0].limit[1])
 {
   attr = select_attr(focus, i, j);
   wattr_on(g_ctrl.menu[i].win, (attr_t)(attr), ((void *)0));
   printfield(j + 5, itab[g_ctrl.menu[i].limit[0] + j], i);
   wattr_off(g_ctrl.menu[i].win, (attr_t)(attr), ((void *)0));
   j++;
 }
      printfield_name(i, g_ctrl.menu[i].path);
      i++;
    }
}

variable_types: focus int

variable_types: i int

variable_types: j int

variable_types: struct s_item { char right[11]; char *name; char **pwd; 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; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; } s_stat; int color; } **

variable_types: attr int


decompiled_code:
__int64 __fastcall select_attr(int a1, int a2, int a3)
{
__int64 result; // rax
char *v4; // rcx

result = 1024LL;
if ( a2 == a1 )
{
v4 = (char *)&g_ctrl + 56 * a2;
result = 768LL;
if ( *((_DWORD *)v4 + 6) != a3 + *((_DWORD *)v4 + 7) )
result = *(unsigned int *)(*(_QWORD *)(*((_QWORD *)v4 + 1) + 8LL * a3) + 176LL);
}
return result;
}

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

result = 1024LL;
if ( a2 == a1 )
{
v4 = (char *)&g_ctrl + 56 * a2;
result = 768LL;
if ( *((_DWORD *)v4 + 6) != a3 + *((_DWORD *)v4 + 7) )
result = *(unsigned int *)(*(_QWORD *)(*((_QWORD *)v4 + 1) + 8LL * a3) + 176LL);
}
return result;
}

original_code:
static int select_attr(int focus, int i, int j)
{
  if (i != focus)
    return ((((chtype)(((4))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))));
  else if (g_ctrl.menu[i].cur == g_ctrl.menu[i].limit[0]+ j)
    return ((((chtype)(((3))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))));
  else
    return (g_ctrl.menu[i].list[j]->color);
}


decompiled_code:
__int64 __fastcall recurs_strtok(char *a1, __int64 a2, int a3)
{
char *v3; // rbp
int v4; // ebx
__int64 v5; // rdi
int v6; // er12
char *v7; // r14
__int64 v8; // rdi
__int64 v9; // r13
unsigned int v10; // er8
__int64 v11; // r13
__int64 v12; // rax

v3 = a1;
v4 = 0;
v5 = (unsigned int)*a1;
if ( !(_BYTE)v5 )
{
LABEL_13:
v6 = v4;
LABEL_14:
v10 = a3 + 1;
goto LABEL_15;
}
while ( (unsigned __int8)my_is_in(v5, a2) == 1 )
{
v5 = (unsigned int)*++v3;
++v4;
if ( !(_BYTE)v5 )
goto LABEL_13;
}
v6 = v4;
v7 = &a1[v4];
v8 = (unsigned int)*v7;
if ( !*v7 )
goto LABEL_14;
v9 = v4;
while ( !(unsigned __int8)my_is_in(v8, a2) )
{
++v9;
++v6;
v8 = (unsigned int)a1[v9];
if ( !a1[v9] )
goto LABEL_14;
}
v10 = a3 + 1;
if ( !a1[v9] )
{
LABEL_15:
v11 = my_malloc(8 * v10);
if ( !v11 )
return 0LL;
goto LABEL_11;
}
v11 = recurs_strtok(&a1[v9 + 1], a2, v10);
if ( !v11 )
return 0LL;
LABEL_11:
v12 = my_strndup(v3, (unsigned int)(v6 - v4));
*(_QWORD *)(v11 + 8LL * a3 - 8) = v12;
if ( !v12 )
return 0LL;
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, __int64 a2, int a3)
{
char *v3; // rbp
int v4; // ebx
__int64 v5; // rdi
int v6; // er12
char *v7; // r14
__int64 v8; // rdi
__int64 v9; // r13
unsigned int v10; // er8
__int64 v11; // r13
__int64 v12; // rax

v3 = a1;
v4 = 0;
v5 = (unsigned int)*a1;
if ( !(_BYTE)v5 )
{
LABEL_13:
v6 = v4;
LABEL_14:
v10 = a3 + 1;
goto LABEL_15;
}
while ( (unsigned __int8)my_is_in(v5, a2) == 1 )
{
v5 = (unsigned int)*++v3;
++v4;
if ( !(_BYTE)v5 )
goto LABEL_13;
}
v6 = v4;
v7 = &a1[v4];
v8 = (unsigned int)*v7;
if ( !*v7 )
goto LABEL_14;
v9 = v4;
while ( !(unsigned __int8)my_is_in(v8, a2) )
{
++v9;
++v6;
v8 = (unsigned int)a1[v9];
if ( !a1[v9] )
goto LABEL_14;
}
v10 = a3 + 1;
if ( !a1[v9] )
{
LABEL_15:
v11 = my_malloc(8 * v10);
if ( !v11 )
return 0LL;
goto LABEL_11;
}
v11 = func0(&a1[v9 + 1], a2, v10);
if ( !v11 )
return 0LL;
LABEL_11:
v12 = my_strndup(v3, (unsigned int)(v6 - v4));
*(_QWORD *)(v11 + 8LL * a3 - 8) = v12;
if ( !v12 )
return 0LL;
return v11;
}

original_code:
static char **recurs_strtok(char *str, char *delim, int wcount)
{
  int i;
  int j;
  char **ret;
  j = 0;
  while (str[j] != '\0' && my_is_in(str[j], delim) == 1)
    j++;
  i = j;
  while (str[i] != '\0' && my_is_in(str[i], delim) == 0)
    i++;
  if (str[i] != '\0')
    ret = recurs_strtok(str + i + 1, delim, wcount + 1);
  else
    ret = my_malloc((wcount + 1) * sizeof(*ret));
  if (ret == ((void *)0))
    return (((void *)0));
  if ((ret[wcount - 1] = my_strndup(str + j, i - j)) == ((void *)0))
    return (((void *)0));
  return (ret);
}

variable_types:
delim
char *

variable_types: wcount int

variable_types:
ret
char **

return_type: char **


decompiled_code:
__int64 __fastcall strtok_tab(char *a1, __int64 a2)
{
return recurs_strtok(a1, a2, 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, __int64 a2)
{
return func0(a1, a2, 1);
}

original_code:
char **strtok_tab(char *str, char *delim)
{
  return (recurs_strtok(str, delim, 1));
}

variable_types:
str
char *


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

if ( a3 > 0 && a1 )
result = memset(a1, a2, a3);
else
result = 0LL;
return result;
}

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

if ( a3 > 0 && a1 )
result = memset(a1, a2, a3);
else
result = 0LL;
return result;
}

original_code:
void *my_memset(void *s, char c, int n)
{
  int i;
  char *temp;
  i = 0;
  if (n <= 0 || !s)
    return (((void *)0));
  temp = s;
  while (i < n)
    {
      temp[i] = c;
      i++;
    }
  return (s);
}

variable_types:
s
void *

variable_types: c char

variable_types: n int

return_type: void *


decompiled_code:
ssize_t __fastcall my_putchar(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func0(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

original_code:
void my_putchar(char c)
{
  write(1, &c, 1);
}


original_code:
char *my_revstr(char *str)
{
  int i;
  int len;
  int temp;
  i = 0;
  len = my_strlen(str) - 1;
  if (!str)
    return (str);
  while (i < len - i)
    {
      temp = str[i];
      str[i] = str[len - i];
      str[len - i] = temp;
      i++;
    }
  return (str);
}

variable_types: len int

variable_types: temp int


original_code:
char *my_revmem(char *str, int len)
{
  int i;
  int temp;
  i = 0;
  len--;
  if (!str)
    return (str);
  while (i < len - i)
    {
      temp = str[i];
      str[i] = str[len - i];
      str[len - i] = temp;
      i++;
    }
  return (str);
}


decompiled_code:
ssize_t __fastcall my_putstr(void *buf)
{
int v1; // eax

v1 = my_strlen();
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func0(void *buf)
{
int v1; // eax

v1 = my_strlen();
return write(1, buf, v1);
}

original_code:
void my_putstr(const char *str)
{
  write(1, str, my_strlen(str));
}

canonical_original_code:
void my_putstr(char *str)
{
  write(1, str, my_strlen(str));
}


original_code:
char **add_tab(char *str, char **tab)
{
  char **new_array;
  int line;
  int i;
  i = 0;
  line = 0;
  if (tab == ((void *)0))
    return (((void *)0));
  while (tab[line] != ((void *)0))
    line++;
  if ((new_array = my_malloc(sizeof(*new_array) * (line + 2))) == ((void *)0))
    return (((void *)0));
  while (tab[i])
    {
      new_array[i] = my_strdup(tab[i]);
      i++;
    }
  new_array[i] = my_strdup(str);
  new_array[i + 1] = ((void *)0);
  return (new_array);
}

variable_types:
tab
char **

variable_types: line int


decompiled_code:
void *__fastcall my_bzero(void *a1, int a2)
{
void *result; // rax

if ( a1 )
{
if ( a2 > 0 )
result = memset(a1, 0, a2);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1, int a2)
{
void *result; // rax

if ( a1 )
{
if ( a2 > 0 )
result = memset(a1, 0, a2);
}
return result;
}

original_code:
void my_bzero(void *ptr, const int len)
{
  int i;
  char *temp;
  i = 0;
  temp = ptr;
  while (ptr != ((void *)0) && len > 0 && i < len)
    {
      temp[i] = '\0';
      i++;
    }
}

canonical_original_code:
void my_bzero(void *ptr, int len)
{
  int i;
  char *temp;
  i = 0;
  temp = ptr;
  while (ptr != ((void *)0) && len > 0 && i < len)
    {
      temp[i] = '\0';
      i++;
    }
}

variable_types:
ptr
void *

variable_types:
temp
char *


decompiled_code:
__int64 __fastcall print_error(void *buf)
{
int v1; // eax

v1 = my_strlen();
write(2, buf, v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *buf)
{
int v1; // eax

v1 = my_strlen();
write(2, buf, v1);
return 0LL;
}

original_code:
void *print_error(const char *str)
{
  write(2, str, my_strlen(str));
  return (((void *)0));
}

canonical_original_code:
void *print_error(char *str)
{
  write(2, str, my_strlen(str));
  return (((void *)0));
}


decompiled_code:
__int64 __fastcall print_errno(int a1)
{
char *v1; // rbp
int v2; // eax

v1 = str_error[a1];
v2 = my_strlen(v1);
write(2, v1, v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
char *v1; // rbp
int v2; // eax

v1 = str_error[a1];
v2 = my_strlen(v1);
write(2, v1, v2);
return 0LL;
}

original_code:
t_bool print_errno(int my_errno)
{
  write(2, str_error[my_errno], my_strlen(str_error[my_errno]));
  return (0);
}

canonical_original_code:
unsigned char print_errno(int my_errno)
{
  write(2, str_error[my_errno], my_strlen(str_error[my_errno]));
  return (0);
}


original_code:
t_bool sort_item(t_item **tab)
{
  int i;
  i = 0;
  while (tab[i + 1] != ((void *)0))
    {
      if (my_strcmp_case(tab[i]->name, tab[i + 1]->name) > 0)
 {
   my_swap(tab + i, tab + i + 1);
   i = 0;
 }
      else
 i++;
    }
  return (1);
}

canonical_original_code:
unsigned char sort_item(struct s_item **tab)
{
  int i;
  i = 0;
  while (tab[i + 1] != ((void *)0))
    {
      if (my_strcmp_case(tab[i]->name, tab[i + 1]->name) > 0)
 {
   my_swap(tab + i, tab + i + 1);
   i = 0;
 }
      else
 i++;
    }
  return (1);
}

variable_types: i 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; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
void *__fastcall creat_path(_QWORD *a1)
{
_QWORD *v1; // rbx
void *i; // r13
__int64 v3; // r12
void *v5; // rbp

if ( !*a1 )
return &unk_1B4;
v1 = a1 + 1;
for ( i = 0LL; ; i = (void *)v3 )
{
v5 = (void *)my_stradd(&unk_1B4);
if ( !v5 )
break;
v3 = my_stradd(i);
if ( !v3 )
break;
free(v5);
if ( i )
free(i);
if ( !*v1++ )
return (void *)v3;
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(_QWORD *a1)
{
_QWORD *v1; // rbx
void *i; // r13
__int64 v3; // r12
void *v5; // rbp

if ( !*a1 )
return &unk_1B4;
v1 = a1 + 1;
for ( i = 0LL; ; i = (void *)v3 )
{
v5 = (void *)my_stradd(&unk_1B4);
if ( !v5 )
break;
v3 = my_stradd(i);
if ( !v3 )
break;
free(v5);
if ( i )
free(i);
if ( !*v1++ )
return (void *)v3;
}
return 0LL;
}

original_code:
char *creat_path(char **path)
{
  char *ret;
  char *temp;
  char *temp2;
  int i;
  i = 0;
  ret = ((void *)0);
  while (path[i] != ((void *)0))
    {
      if ((temp = my_stradd("/", path[i])) == ((void *)0))
 return (((void *)0));
      if ((temp2 = my_stradd(ret, temp)) == ((void *)0))
 return (((void *)0));
      free(temp);
      i++;
      if (ret != ((void *)0))
 free(ret);
      ret = temp2;
    }
  if (i == 0)
    return ("/");
  return (ret);
}

variable_types:
path
char **

variable_types:
ret
char *


decompiled_code:
__int64 __fastcall my_strdup(char *a1)
{
int v1; // eax
__int64 v2; // r8
char v3; // dl
__int64 v4; // rax

if ( !a1 )
return 0LL;
v1 = my_strlen();
v2 = my_malloc((unsigned int)(v1 + 1));
if ( !v2 )
return 0LL;
v3 = *a1;
if ( *a1 )
{
v4 = 0LL;
do
{
*(_BYTE *)(v2 + v4++) = v3;
v3 = a1[v4];
}
while ( v3 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
int v1; // eax
__int64 v2; // r8
char v3; // dl
__int64 v4; // rax

if ( !a1 )
return 0LL;
v1 = my_strlen();
v2 = my_malloc((unsigned int)(v1 + 1));
if ( !v2 )
return 0LL;
v3 = *a1;
if ( *a1 )
{
v4 = 0LL;
do
{
*(_BYTE *)(v2 + v4++) = v3;
v3 = a1[v4];
}
while ( v3 );
}
return v2;
}

original_code:
char *my_strdup(char *str)
{
  char *ret;
  int i;
  if (!str)
    return (str);
  i = 0;
  if ((ret = my_malloc(sizeof(*ret) * (my_strlen(str) + 1))) == ((void *)0))
    return (((void *)0));
  while (str[i] != '\0')
    {
      ret[i] = str[i];
      i++;
    }
  return (ret);
}


DefaultFreeEntry

repo: emeryberger/eon

decompiled_code:
void __fastcall DefaultFreeEntry(__int64 a1, void *a2, int a3)
{
if ( a3 == 1 )
free(a2);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, void *a2, int a3)
{
if ( a3 == 1 )
free(a2);
}

original_code:
static void
DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
{
    if (flag == 1)
        free(he);
}

canonical_original_code:
static void DefaultFreeEntry(void *pool, struct PLHashEntry *he, unsigned int flag)
{
    if (flag == 1)
        free(he);
}

variable_types:
pool
void *

variable_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *

variable_types:
flag
unsigned int


DefaultFreeTable

decompiled_code:
void __fastcall DefaultFreeTable(__int64 a1, void *a2)
{
free(a2);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, void *a2)
{
free(a2);
}

original_code:
static void
DefaultFreeTable(void *pool, void *item)
{
    free(item);
}

variable_types:
item
void *


DefaultAllocEntry

decompiled_code:
void *DefaultAllocEntry()
{
return malloc(0x20uLL);
}

canonical_decompiled_code:
void *func2()
{
return malloc(0x20uLL);
}

original_code:
static PLHashEntry *
DefaultAllocEntry(void *pool, const void *key)
{
    return malloc(sizeof(PLHashEntry));
}

canonical_original_code:
static struct PLHashEntry *DefaultAllocEntry(void *pool, void *key)
{
    return malloc(sizeof(PLHashEntry));
}

variable_types:
key
void *

return_type: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *


DefaultAllocTable

decompiled_code:
void *__fastcall DefaultAllocTable(__int64 a1, size_t a2)
{
return malloc(a2);
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, size_t a2)
{
return malloc(a2);
}

original_code:
static void *
DefaultAllocTable(void *pool, PRSize size)
{
    return malloc(size);
}

canonical_original_code:
static void *DefaultAllocTable(void *pool, long size)
{
    return malloc(size);
}

variable_types: size long


original_code:
void
PL_HashTableDestroy(PLHashTable *ht)
{
    PRUint32 i, n;
    PLHashEntry *he, *next;
    const PLHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;
    n = (1 << (32 - (ht)->shift));
    for (i = 0; i < n; i++) {
        for (he = ht->buckets[i]; he; he = next) {
            next = he->next;
            (*allocOps->freeEntry)(allocPriv, he, 1);
        }
    }
    (*allocOps->freeTable)(allocPriv, ht->buckets);
    (*allocOps->freeTable)(allocPriv, ht);
}

canonical_original_code:
void PL_HashTableDestroy(struct PLHashTable *ht)
{
    unsigned int i; unsigned int n;
    struct PLHashEntry *he; struct PLHashEntry *next;
    struct PLHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;
    n = (1 << (32 - (ht)->shift));
    for (i = 0; i < n; i++) {
        for (he = ht->buckets[i]; he; he = next) {
            next = he->next;
            (*allocOps->freeEntry)(allocPriv, he, 1);
        }
    }
    (*allocOps->freeTable)(allocPriv, ht->buckets);
    (*allocOps->freeTable)(allocPriv, ht);
}

variable_types: struct PLHashTable { struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } **buckets; unsigned int nentries; unsigned int shift; unsigned int (*keyHash)(void *key); int (*keyCompare)(void *v1, void *v2); int (*valueCompare)(void *v1, void *v2); struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); } *allocOps; void *allocPriv; } *

variable_types:
i
unsigned int

variable_types:
n
unsigned int

variable_types: struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); } *

variable_types:
allocPriv
void *

user_defined_types: struct PLHashTable { struct PLHashEntry **buckets; unsigned int nentries; unsigned int shift; unsigned int (*keyHash)(void *key); int (*keyCompare)(void *v1, void *v2); int (*valueCompare)(void *v1, void *v2); struct PLHashAllocOps *allocOps; void *allocPriv; }


original_code:
void
PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he)
{
    PRUint32 i, n;
    PLHashEntry *next, **oldbuckets;
    PRSize nb;
    *hep = he->next;
    (*ht->allocOps->freeEntry)(ht->allocPriv, he, 1);
    n = (1 << (32 - (ht)->shift));
    if (--ht->nentries < (((n) > (1 << 4)) ? ((n) >> 2) : 0)) {
        oldbuckets = ht->buckets;
        nb = n * sizeof(PLHashEntry*) / 2;
        ht->buckets = (PLHashEntry**)(
            (*ht->allocOps->allocTable)(ht->allocPriv, nb));
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return;
        }
        memset(ht->buckets, 0, nb);
        ht->shift++;
        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
                do {} while(0);
                he->next = 0;
                *hep = he;
            }
        }
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
    }
}

canonical_original_code:
void PL_HashTableRawRemove(struct PLHashTable *ht, struct PLHashEntry **hep, struct PLHashEntry *he)
{
    unsigned int i; unsigned int n;
    struct PLHashEntry *next; struct PLHashEntry **oldbuckets;
    long nb;
    *hep = he->next;
    (*ht->allocOps->freeEntry)(ht->allocPriv, he, 1);
    n = (1 << (32 - (ht)->shift));
    if (--ht->nentries < (((n) > (1 << 4)) ? ((n) >> 2) : 0)) {
        oldbuckets = ht->buckets;
        nb = n * sizeof(struct PLHashEntry *) / 2;
        ht->buckets = (struct PLHashEntry **)(
            (*ht->allocOps->allocTable)(ht->allocPriv, nb));
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return;
        }
        memset(ht->buckets, 0, nb);
        ht->shift++;
        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
                do {} while(0);
                he->next = 0;
                *hep = he;
            }
        }
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
    }
}

variable_types:
hep
struct PLHashEntry **

variable_types:
he
struct PLHashEntry *

variable_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } **

variable_types: nb long

user_defined_types: struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); }


decompiled_code:
__int64 __fastcall PL_HashTableRemove(__int64 a1, __int64 a2)
{
int v2; // eax
_QWORD **v3; // rax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
v3 = (_QWORD **)PL_HashTableRawLookup(a1, v2, a2);
if ( !*v3 )
return 0LL;
PL_HashTableRawRemove(a1, v3, *v3);
return 1LL;
}

original_code:
PRBool
PL_HashTableRemove(PLHashTable *ht, const void *key)
{
    PLHashNumber keyHash;
    PLHashEntry *he, **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) == 0)
        return 0;
    PL_HashTableRawRemove(ht, hep, he);
    return 1;
}

canonical_original_code:
int PL_HashTableRemove(struct PLHashTable *ht, void *key)
{
    unsigned int keyHash;
    struct PLHashEntry *he; struct PLHashEntry **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) == 0)
        return 0;
    PL_HashTableRawRemove(ht, hep, he);
    return 1;
}

variable_types:
keyHash
unsigned int

user_defined_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; }


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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
result = *(_QWORD *)PL_HashTableRawLookup(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 24);
return result;
}

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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
result = *(_QWORD *)func7(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 24);
return result;
}

original_code:
void *
PL_HashTableLookup(PLHashTable *ht, const void *key)
{
    PLHashNumber keyHash;
    PLHashEntry *he, **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != 0) {
        return he->value;
    }
    return 0;
}

canonical_original_code:
void *PL_HashTableLookup(struct PLHashTable *ht, void *key)
{
    unsigned int keyHash;
    struct PLHashEntry *he; struct PLHashEntry **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != 0) {
        return he->value;
    }
    return 0;
}


original_code:
int
PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg)
{
    PLHashEntry *he, **hep;
    PRUint32 i, nbuckets;
    int rv, n = 0;
    PLHashEntry *todo = 0;
    nbuckets = (1 << (32 - (ht)->shift));
    for (i = 0; i < nbuckets; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != 0) {
            rv = (*f)(he, n, arg);
            n++;
            if (rv & (2 | 4)) {
                *hep = he->next;
                if (rv & 2) {
                    he->next = todo;
                    todo = he;
                }
            } else {
                hep = &he->next;
            }
            if (rv & 1) {
                goto out;
            }
        }
    }
out:
    hep = &todo;
    while ((he = *hep) != 0) {
        PL_HashTableRawRemove(ht, hep, he);
    }
    return n;
}

canonical_original_code:
int PL_HashTableEnumerateEntries(struct PLHashTable *ht, int (*f)(struct PLHashEntry *he, int i, void *arg), void *arg)
{
    struct PLHashEntry *he; struct PLHashEntry **hep;
    unsigned int i; unsigned int nbuckets;
    int rv; int n = 0;
    struct PLHashEntry *todo = 0;
    nbuckets = (1 << (32 - (ht)->shift));
    for (i = 0; i < nbuckets; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != 0) {
            rv = (*f)(he, n, arg);
            n++;
            if (rv & (2 | 4)) {
                *hep = he->next;
                if (rv & 2) {
                    he->next = todo;
                    todo = he;
                }
            } else {
                hep = &he->next;
            }
            if (rv & 1) {
                goto out;
            }
        }
    }
out:
    hep = &todo;
    while ((he = *hep) != 0) {
        PL_HashTableRawRemove(ht, hep, he);
    }
    return n;
}

variable_types: int (*)(struct PLHashEntry *he, int i, void *arg)

variable_types:
arg
void *

variable_types:
nbuckets
unsigned int

variable_types: rv int

variable_types: n int


decompiled_code:
__int64 __fastcall PL_HashString(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int i; // er8

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i = v1 ^ __ROL4__(i, 4);
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int i; // er8

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i = v1 ^ __ROL4__(i, 4);
}
return i;
}

original_code:
PLHashNumber
PL_HashString(const void *key)
{
    PLHashNumber h;
    const PRUint8 *s;
    h = 0;
    for (s = (const PRUint8*)key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ *s;
    return h;
}

canonical_original_code:
unsigned int PL_HashString(void *key)
{
    unsigned int h;
    unsigned char *s;
    h = 0;
    for (s = (unsigned char *)key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ *s;
    return h;
}

variable_types:
key
void *

variable_types:
h
unsigned int

variable_types:
s
unsigned char *

return_type: unsigned int


decompiled_code:
_BOOL8 __fastcall PL_CompareStrings(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
int
PL_CompareStrings(const void *v1, const void *v2)
{
  return strcmp((const char*)v1, (const char*)v2) == 0;
}

canonical_original_code:
int PL_CompareStrings(void *v1, void *v2)
{
  return strcmp((char *)v1, (char *)v2) == 0;
}


decompiled_code:
_BOOL8 __fastcall PL_CompareValues(__int64 a1, __int64 a2)
{
return a2 == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(__int64 a1, __int64 a2)
{
return a2 == a1;
}

original_code:
int
PL_CompareValues(const void *v1, const void *v2)
{
    return v1 == v2;
}

canonical_original_code:
int PL_CompareValues(void *v1, void *v2)
{
    return v1 == v2;
}


saferead

decompiled_code:
__int64 __fastcall saferead(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er15

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
while ( 1 )
{
v6 = read(fd, buf, v4);
v7 = v6;
if ( v6 != -1 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
while ( v4 > 0 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er15

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
while ( 1 )
{
v6 = read(fd, buf, v4);
v7 = v6;
if ( v6 != -1 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
while ( v4 > 0 );
return v5;
}

original_code:
int saferead(int fd, void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = read(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      if (n == 0)
 return actual;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}

variable_types: fd int

variable_types:
buffer
void *

variable_types: count int

variable_types: actual int


safewrite

decompiled_code:
__int64 __fastcall safewrite(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er15

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
while ( 1 )
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 != -1 )
break;
if ( *_errno_location() != 4 )
return v7;
}
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
while ( v4 > 0 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er15

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
while ( 1 )
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 != -1 )
break;
if ( *_errno_location() != 4 )
return v7;
}
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
while ( v4 > 0 );
return v5;
}

original_code:
int safewrite(int fd, const void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = write(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}

canonical_original_code:
int safewrite(int fd, void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = write(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}


decompiled_code:
__int64 __fastcall init_sf_source(int fd)
{
char v1; // al
unsigned int v2; // er8
char buf; // [rsp+8h] [rbp-20h] BYREF
char v5; // [rsp+9h] [rbp-1Fh]
__int16 v6; // [rsp+Ah] [rbp-1Eh] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6 = 8532;
if ( (unsigned int)safewrite(fd, &v6, 2) != 2 )
return (unsigned int)-1;
if ( (unsigned int)saferead(fd, &buf, 2) != 2 )
return (unsigned int)-1;
if ( buf != 84 )
return (unsigned int)-1;
v1 = v5;
if ( v5 <= 31 )
return (unsigned int)-1;
if ( SHIBYTE(v6) <= v5 )
v1 = HIBYTE(v6);
v2 = 0;
if ( v1 != 33 )
return v2;
v7[0] = platform;
if ( (unsigned int)safewrite(fd, v7, 4) != 4 )
return (unsigned int)-1;
return (unsigned int)-((unsigned int)saferead(fd, v7, 4) != 4);
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd)
{
char v1; // al
unsigned int v2; // er8
char buf; // [rsp+8h] [rbp-20h] BYREF
char v5; // [rsp+9h] [rbp-1Fh]
__int16 v6; // [rsp+Ah] [rbp-1Eh] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6 = 8532;
if ( (unsigned int)func2(fd, &v6, 2) != 2 )
return (unsigned int)-1;
if ( (unsigned int)func1(fd, &buf, 2) != 2 )
return (unsigned int)-1;
if ( buf != 84 )
return (unsigned int)-1;
v1 = v5;
if ( v5 <= 31 )
return (unsigned int)-1;
if ( SHIBYTE(v6) <= v5 )
v1 = HIBYTE(v6);
v2 = 0;
if ( v1 != 33 )
return v2;
v7[0] = platform;
if ( (unsigned int)func2(fd, v7, 4) != 4 )
return (unsigned int)-1;
return (unsigned int)-((unsigned int)func1(fd, v7, 4) != 4);
}

original_code:
int init_sf_source(int fd)
{
  char check[2], us[2];
  int version;
  unsigned char nonce[4];
  us[0] = 'T'; us[1] = '!';
  if (safewrite(fd, us, 2) != 2 ||
      saferead(fd, check, 2) != 2 ||
      check[0] != 'T' || check[1] < ' ')
    return -1;
  version = check[1];
  if (us[1] < version)
    version = us[1];
  switch (version)
    {
    case ' ': break;
    case '!':
      nonce[0] = platform;
      nonce[1] = platform >> 8;
      nonce[2] = platform >> 16;
      nonce[3] = platform >> 24;
      if (safewrite(fd, nonce, 4) != 4)
 return -1;
      if (saferead(fd, nonce, 4) != 4)
 return -1;
      break;
    }
  return 0;
}

canonical_original_code:
int init_sf_source(int fd)
{
  char check[2]; char us[2];
  int version;
  unsigned char nonce[4];
  us[0] = 'T'; us[1] = '!';
  if (safewrite(fd, us, 2) != 2 ||
      saferead(fd, check, 2) != 2 ||
      check[0] != 'T' || check[1] < ' ')
    return -1;
  version = check[1];
  if (us[1] < version)
    version = us[1];
  switch (version)
    {
    case ' ': break;
    case '!':
      nonce[0] = platform;
      nonce[1] = platform >> 8;
      nonce[2] = platform >> 16;
      nonce[3] = platform >> 24;
      if (safewrite(fd, nonce, 4) != 4)
 return -1;
      if (saferead(fd, nonce, 4) != 4)
 return -1;
      break;
    }
  return 0;
}

variable_types:
check
char [2]

variable_types:
us
char [2]

variable_types: version int

variable_types:
nonce
unsigned char [4]


decompiled_code:
__int64 __fastcall open_sf_source(char *name, __int16 a2)
{
int v2; // er12
struct hostent *v3; // rax
socklen_t v4; // edx
int v5; // edi
struct sockaddr v7[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 >= 0 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v7[0].sa_family = v3->h_addrtype;
memcpy(&v7[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v7[0].sa_data = _bswap_16(a2);
v5 = v2;
if ( connect(v2, v7, v4) < 0 )
{
LABEL_7:
close(v5);
return (unsigned int)-1;
}
if ( (int)init_sf_source(v2) >= 0 )
return (unsigned int)v2;
}
v5 = v2;
goto LABEL_7;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *name, __int16 a2)
{
int v2; // er12
struct hostent *v3; // rax
socklen_t v4; // edx
int v5; // edi
struct sockaddr v7[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 >= 0 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v7[0].sa_family = v3->h_addrtype;
memcpy(&v7[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v7[0].sa_data = func0(a2);
v5 = v2;
if ( connect(v2, v7, v4) < 0 )
{
LABEL_7:
close(v5);
return (unsigned int)-1;
}
if ( (int)func3(v2) >= 0 )
return (unsigned int)v2;
}
v5 = v2;
goto LABEL_7;
}
return (unsigned int)v2;
}

original_code:
int open_sf_source(const char *host, int port)
{
  int fd = socket(2, SOCK_STREAM, 0);
  struct hostent *entry;
  struct sockaddr_in addr;
  if (fd < 0)
    return fd;
  entry = gethostbyname(host);
  if (!entry)
    {
      close(fd);
      return -1;
    }
  addr.sin_family = entry->h_addrtype;
  memcpy(&addr.sin_addr, entry->h_addr_list[0], entry->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0)
    {
      close(fd);
      return -1;
    }
  if (init_sf_source(fd) < 0)
    {
      close(fd);
      return -1;
    }
  return fd;
}

canonical_original_code:
int open_sf_source(char *host, int port)
{
  int fd = socket(2, SOCK_STREAM, 0);
  struct hostent *entry;
  struct sockaddr_in addr;
  if (fd < 0)
    return fd;
  entry = gethostbyname(host);
  if (!entry)
    {
      close(fd);
      return -1;
    }
  addr.sin_family = entry->h_addrtype;
  memcpy(&addr.sin_addr, entry->h_addr_list[0], entry->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0)
    {
      close(fd);
      return -1;
    }
  if (init_sf_source(fd) < 0)
    {
      close(fd);
      return -1;
    }
  return fd;
}

variable_types:
host
char *

variable_types: port int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
void *__fastcall read_sf_packet(int fd, _DWORD *a2)
{
unsigned __int8 v2; // r13
void *v3; // rax
void *v4; // r12
int v5; // eax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( (unsigned int)saferead(fd, buf, 1) == 1 )
{
v2 = buf[0];
v3 = malloc((unsigned __int8)buf[0]);
v4 = v3;
if ( v3 )
{
v5 = saferead(fd, v3, v2);
if ( v5 == (unsigned __int8)buf[0] )
{
*a2 = v5;
return v4;
}
free(v4);
}
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(int fd, _DWORD *a2)
{
unsigned __int8 v2; // r13
void *v3; // rax
void *v4; // r12
int v5; // eax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( (unsigned int)func1(fd, buf, 1) == 1 )
{
v2 = buf[0];
v3 = malloc((unsigned __int8)buf[0]);
v4 = v3;
if ( v3 )
{
v5 = func1(fd, v3, v2);
if ( v5 == (unsigned __int8)buf[0] )
{
*a2 = v5;
return v4;
}
free(v4);
}
}
return 0LL;
}

original_code:
void *read_sf_packet(int fd, int *len)
{
  unsigned char l;
  void *packet;
  if (saferead(fd, &l, 1) != 1)
    return ((void *)0);
  packet = malloc(l);
  if (!packet)
    return ((void *)0);
  if (saferead(fd, packet, l) != l)
    {
      free(packet);
      return ((void *)0);
    }
  *len = l;
  return packet;
}

variable_types:
len
int *

variable_types:
l
unsigned char

variable_types:
packet
void *


decompiled_code:
__int64 __fastcall write_sf_packet(int fd, void *buf, unsigned __int8 a3)
{
int v3; // eax
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

bufa[0] = a3;
if ( (unsigned int)safewrite(fd, bufa, 1) != 1 )
return 0xFFFFFFFFLL;
v3 = safewrite(fd, buf, (unsigned __int8)bufa[0]);
return (unsigned int)-((unsigned __int8)bufa[0] != v3);
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, void *buf, unsigned __int8 a3)
{
int v3; // eax
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

bufa[0] = a3;
if ( (unsigned int)func2(fd, bufa, 1) != 1 )
return 0xFFFFFFFFLL;
v3 = func2(fd, buf, (unsigned __int8)bufa[0]);
return (unsigned int)-((unsigned __int8)bufa[0] != v3);
}

original_code:
int write_sf_packet(int fd, const void *packet, int len)
{
  unsigned char l = len;
  if (safewrite(fd, &l, 1) != 1 ||
      safewrite(fd, packet, l) != l)
    return -1;
  return 0;
}

canonical_original_code:
int write_sf_packet(int fd, void *packet, int len)
{
  unsigned char l = len;
  if (safewrite(fd, &l, 1) != 1 ||
      safewrite(fd, packet, l) != l)
    return -1;
  return 0;
}

variable_types: len int


decompiled_code:
void __fastcall hton_int16(_WORD *a1, __int16 a2)
{
*a1 = a2;
}

canonical_decompiled_code:
void __fastcall func0(_WORD *a1, __int16 a2)
{
*a1 = a2;
}

original_code:
void hton_int16(uint8_t *buf, int16_t data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
}

canonical_original_code:
void hton_int16(unsigned char *buf, short data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
}

variable_types:
buf
unsigned char *

variable_types: data short


decompiled_code:
void __fastcall hton_int32(_DWORD *a1, int a2)
{
*a1 = a2;
}

canonical_decompiled_code:
void __fastcall func1(_DWORD *a1, int a2)
{
*a1 = a2;
}

original_code:
void hton_int32(uint8_t *buf, int32_t data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
  buf[2] = (data >> 16) & 0x00ff;
  buf[3] = (data >> 24) & 0x00ff;
}

canonical_original_code:
void hton_int32(unsigned char *buf, int data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
  buf[2] = (data >> 16) & 0x00ff;
  buf[3] = (data >> 24) & 0x00ff;
}

variable_types: data int


decompiled_code:
void __fastcall hton_uint16(_WORD *a1, __int16 a2)
{
hton_int16(a1, a2);
}

canonical_decompiled_code:
void __fastcall func2(_WORD *a1, __int16 a2)
{
func0(a1, a2);
}

original_code:
void hton_uint16(uint8_t *buf, uint16_t data)
{
  hton_int16(buf,data);
}

canonical_original_code:
void hton_uint16(unsigned char *buf, unsigned short data)
{
  hton_int16(buf,data);
}

variable_types:
data
unsigned short


decompiled_code:
// attributes: thunk
void __fastcall hton_uint32(_DWORD *a1, int a2)
{
hton_int32(a1, a2);
}

canonical_decompiled_code:
void __fastcall func3(_DWORD *a1, int a2)
{
func1(a1, a2);
}

original_code:
void hton_uint32(uint8_t *buf, uint32_t data)
{
  hton_int32(buf,data);
}

canonical_original_code:
void hton_uint32(unsigned char *buf, unsigned int data)
{
  hton_int32(buf,data);
}

variable_types:
data
unsigned int


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

result = *a1;
*a2 = result;
return result;
}

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

result = *a1;
*a2 = result;
return result;
}

canonical_original_code:
void ntoh_int32(unsigned char *buf, int *data)
{
 *data = ((int)buf[0]) |
   (((int)buf[1]) << 8) |
   (((int)buf[2]) << 16) |
   (((int)buf[3]) << 24);
}

variable_types:
data
int *


decompiled_code:
// attributes: thunk
__int64 __fastcall ntoh_uint32(unsigned int *a1, _DWORD *a2)
{
return ntoh_int32(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1, _DWORD *a2)
{
return func5(a1, a2);
}

original_code:
void ntoh_uint32(uint8_t *buf, uint32_t *data)
{
 ntoh_int32(buf,(int32_t*)data);
}

canonical_original_code:
void ntoh_uint32(unsigned char *buf, unsigned int *data)
{
 ntoh_int32(buf,(int *)data);
}

variable_types:
data
unsigned int *


dbg

decompiled_code:
__int64 dbg(__int64 a1, const char *a2, ...)
{
unsigned int v2; // er12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
pthread_mutex_lock(&mutex);
v2 = vprintf(a2, arg);
pthread_mutex_unlock(&mutex);
return v2;
}

canonical_decompiled_code:
__int64 func8(__int64 a1, const char *a2, ...)
{
unsigned int v2; // er12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
pthread_mutex_lock(&mutex);
v2 = vprintf(a2, arg);
pthread_mutex_unlock(&mutex);
return v2;
}

original_code:
int dbg(int level, const char* format, ...)
{
 int val = 0;
 va_list ap;
 pthread_mutex_lock(&mutex);
 __builtin_va_start(ap,format);
 val = vprintf(format, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&mutex);
 return val;
}

canonical_original_code:
int dbg(int level, char *format, ...)
{
 int val = 0;
 __builtin_va_list ap;
 pthread_mutex_lock(&mutex);
 __builtin_va_start(ap,format);
 val = vprintf(format, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&mutex);
 return val;
}

variable_types: level int

variable_types:
format
char *

variable_types: val int


decompiled_code:
__int64 __fastcall open_telos_source(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12

pthread_mutex_lock(&sfwritemutex);
v2 = open_sf_source(a1, a2);
pthread_mutex_unlock(&sfwritemutex);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12

pthread_mutex_lock(&sfwritemutex);
v2 = open_sf_source(a1, a2);
pthread_mutex_unlock(&sfwritemutex);
return v2;
}

original_code:
int open_telos_source(const char *host, int port)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = open_sf_source(host,port);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

canonical_original_code:
int open_telos_source(char *host, int port)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = open_sf_source(host,port);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

variable_types:
host
char *

variable_types: port int

variable_types: result int


decompiled_code:
__int64 __fastcall init_telos_source(unsigned int a1)
{
unsigned int v1; // er12

pthread_mutex_lock(&sfwritemutex);
v1 = init_sf_source(a1);
pthread_mutex_unlock(&sfwritemutex);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // er12

pthread_mutex_lock(&sfwritemutex);
v1 = init_sf_source(a1);
pthread_mutex_unlock(&sfwritemutex);
return v1;
}

original_code:
int init_telos_source(int fd)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = init_sf_source(fd);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall free_telos_packet(void **a1)
{
free(*((void **)*a1 + 1));
free(*a1);
*a1 = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void **a1)
{
free(*((void **)*a1 + 1));
free(*a1);
*a1 = 0LL;
return 0LL;
}

original_code:
int free_telos_packet(telospacket **pkt)
{
 free((*pkt)->data);
 free(*pkt);
 *pkt = ((void *)0);
 return 0;
}

canonical_original_code:
int free_telos_packet(struct telospacket **pkt)
{
 free((*pkt)->data);
 free(*pkt);
 *pkt = ((void *)0);
 return 0;
}

variable_types: struct telospacket { unsigned char length; unsigned char dsn; unsigned short addr; unsigned char type; unsigned char group; unsigned char *data; } **


decompiled_code:
__int64 __fastcall write_telos_packet(unsigned int a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // bl
char *v4; // rax
char *v5; // rsi
char *v6; // rbp
char *v7; // rcx
char v8; // dl
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rsi
unsigned int v12; // er12

v2 = *a2;
if ( *a2 > 0x3Cu )
{
dbg(0LL, "packet too long! (%i bytes)\n", v2);
v12 = -1;
}
else
{
v4 = (char *)malloc((unsigned __int8)(v2 + 10));
v5 = (char *)*((_QWORD *)a2 + 1);
v6 = v4;
*v4 = v2;
*(_WORD *)(v4 + 1) = 2049;
LOBYTE(v4) = telos_dsn;
v7 = v6 + 10;
v6[3] = telos_dsn;
telos_dsn = (_BYTE)v4 + 1;
LOWORD(v4) = *((_WORD *)a2 + 1);
*((_WORD *)v6 + 2) = -1;
v8 = (unsigned __int8)v4 & 0xF;
v6[7] = BYTE1(v4) & 0xF;
LOWORD(v4) = *((_WORD *)a2 + 2);
v6[6] = v8;
*((_WORD *)v6 + 4) = (_WORD)v4;
v9 = *a2;
if ( (unsigned int)v9 >= 8 )
{
*(_QWORD *)(v6 + 10) = *(_QWORD *)v5;
*(_QWORD *)&v7[v9 - 8] = *(_QWORD *)&v5[v9 - 8];
qmemcpy(
(void *)((unsigned __int64)(v6 + 18) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - &v7[-((unsigned __int64)(v6 + 18) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v9 + (_DWORD)v7 - (((_DWORD)v6 + 18) & 0xFFFFFFF8)) >> 3));
}
else if ( (v9 & 4) != 0 )
{
*(_DWORD *)(v6 + 10) = *(_DWORD *)v5;
*(_DWORD *)&v7[v9 - 4] = *(_DWORD *)&v5[v9 - 4];
}
else if ( *a2 )
{
v6[10] = *v5;
if ( (v9 & 2) != 0 )
*(_WORD *)&v7[v9 - 2] = *(_WORD *)&v5[v9 - 2];
}
v10 = 0LL;
puts("Sending data...");
do
{
v11 = (unsigned __int8)v6[v10++];
printf("%X ", v11);
}
while ( *a2 + 9 >= (int)v10 );
puts("\nSend done.");
pthread_mutex_lock(&sfwritemutex);
v12 = write_sf_packet(a1, v6, (unsigned int)*a2 + 10);
pthread_mutex_unlock(&sfwritemutex);
free(v6);
}
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // bl
char *v4; // rax
char *v5; // rsi
char *v6; // rbp
char *v7; // rcx
char v8; // dl
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rsi
unsigned int v12; // er12

v2 = *a2;
if ( *a2 > 0x3Cu )
{
dbg(0LL, "packet too long! (%i bytes)\n", v2);
v12 = -1;
}
else
{
v4 = (char *)malloc((unsigned __int8)(v2 + 10));
v5 = (char *)*((_QWORD *)a2 + 1);
v6 = v4;
*v4 = v2;
*(_WORD *)(v4 + 1) = 2049;
LOBYTE(v4) = telos_dsn;
v7 = v6 + 10;
v6[3] = telos_dsn;
telos_dsn = (_BYTE)v4 + 1;
LOWORD(v4) = *((_WORD *)a2 + 1);
*((_WORD *)v6 + 2) = -1;
v8 = (unsigned __int8)v4 & 0xF;
v6[7] = BYTE1(v4) & 0xF;
LOWORD(v4) = *((_WORD *)a2 + 2);
v6[6] = v8;
*((_WORD *)v6 + 4) = (_WORD)v4;
v9 = *a2;
if ( (unsigned int)v9 >= 8 )
{
*(_QWORD *)(v6 + 10) = *(_QWORD *)v5;
*(_QWORD *)&v7[v9 - 8] = *(_QWORD *)&v5[v9 - 8];
qmemcpy(
(void *)((unsigned __int64)(v6 + 18) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - &v7[-((unsigned __int64)(v6 + 18) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v9 + (_DWORD)v7 - (((_DWORD)v6 + 18) & 0xFFFFFFF8)) >> 3));
}
else if ( (v9 & 4) != 0 )
{
*(_DWORD *)(v6 + 10) = *(_DWORD *)v5;
*(_DWORD *)&v7[v9 - 4] = *(_DWORD *)&v5[v9 - 4];
}
else if ( *a2 )
{
v6[10] = *v5;
if ( (v9 & 2) != 0 )
*(_WORD *)&v7[v9 - 2] = *(_WORD *)&v5[v9 - 2];
}
v10 = 0LL;
puts("Sending data...");
do
{
v11 = (unsigned __int8)v6[v10++];
printf("%X ", v11);
}
while ( *a2 + 9 >= (int)v10 );
puts("\nSend done.");
pthread_mutex_lock(&sfwritemutex);
v12 = write_sf_packet(a1, v6, (unsigned int)*a2 + 10);
pthread_mutex_unlock(&sfwritemutex);
free(v6);
}
return v12;
}

original_code:
int write_telos_packet(int fd, const telospacket *packet)
{
  unsigned char l;
  uint8_t *buffer;
  int result;
  int i;
  if (packet->length > 60)
  {
   dbg(TSRC,"packet too long! (%i bytes)\n",packet->length);
   return -1;
  }
  buffer = malloc(packet->length + 10);
  buffer[0] = packet->length;
  buffer[1] = 0x01;
  buffer[2] = 0x08;
  buffer[3] = telos_dsn;
  telos_dsn++;
  buffer[4] = 0xff;
  buffer[5] = 0xff;
  buffer[6] = packet->addr & 0x0F;
  buffer[7] = (packet->addr >> 8) & 0x0F;
  buffer[8] = packet->type;
  buffer[9] = packet->group;
  memcpy(buffer+10, packet->data, packet->length);
  printf("Sending data...\n");
  for (i=0; i < packet->length + 10; i++)
    printf("%X ", buffer[i]);
  printf("\nSend done.\n");
  l = packet->length + 10;
  pthread_mutex_lock(&sfwritemutex);
  result = write_sf_packet(fd, (void*)buffer, packet->length+10);
  pthread_mutex_unlock(&sfwritemutex);
  free((void*)buffer);
  return result;
}

canonical_original_code:
int write_telos_packet(int fd, struct telospacket *packet)
{
  unsigned char l;
  unsigned char *buffer;
  int result;
  int i;
  if (packet->length > 60)
  {
   dbg(TSRC,"packet too long! (%i bytes)\n",packet->length);
   return -1;
  }
  buffer = malloc(packet->length + 10);
  buffer[0] = packet->length;
  buffer[1] = 0x01;
  buffer[2] = 0x08;
  buffer[3] = telos_dsn;
  telos_dsn++;
  buffer[4] = 0xff;
  buffer[5] = 0xff;
  buffer[6] = packet->addr & 0x0F;
  buffer[7] = (packet->addr >> 8) & 0x0F;
  buffer[8] = packet->type;
  buffer[9] = packet->group;
  memcpy(buffer+10, packet->data, packet->length);
  printf("Sending data...\n");
  for (i=0; i < packet->length + 10; i++)
    printf("%X ", buffer[i]);
  printf("\nSend done.\n");
  l = packet->length + 10;
  pthread_mutex_lock(&sfwritemutex);
  result = write_sf_packet(fd, (void *)buffer, packet->length+10);
  pthread_mutex_unlock(&sfwritemutex);
  free((void *)buffer);
  return result;
}

variable_types: struct telospacket { unsigned char length; unsigned char dsn; unsigned short addr; unsigned char type; unsigned char group; unsigned char *data; } *

variable_types:
l
unsigned char

variable_types:
buffer
unsigned char *

variable_types: i int


repo: emestee/bk-emulator

decompiled_code:
void __fastcall disk_open(unsigned int *a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er12
void *v5; // rax
char *v6; // rax
char *v7; // rax

v2 = open(a2, 2);
if ( v2 == -1 )
{
*((_BYTE *)a1 + 26) = 1;
v2 = open(a2, 0);
if ( v2 == -1 )
{
perror(a2);
return;
}
}
v3 = lseek(v2, 0LL, 2);
*a1 = v3;
v4 = v3;
if ( v3 == -1 )
{
perror("seek");
v4 = *a1;
if ( (*a1 & 0x1FF) == 0 )
goto LABEL_4;
LABEL_11:
v7 = dcgettext(0LL, "%s is not an integer number of blocks: %d bytes\n", 5);
fprintf(stderr, v7, a2, v4);
close(v2);
return;
}
if ( (v3 & 0x1FF) != 0 )
goto LABEL_11;
LABEL_4:
v5 = mmap(0LL, v4, *((_BYTE *)a1 + 26) == 0 ? 3 : 1, 1, v2, 0LL);
*((_QWORD *)a1 + 1) = v5;
if ( v5 == (void *)-1LL )
{
*((_QWORD *)a1 + 1) = 0LL;
perror(a2);
}
if ( *((_BYTE *)a1 + 26) )
{
v6 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v6, a2);
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned int *a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er12
void *v5; // rax
char *v6; // rax
char *v7; // rax

v2 = open(a2, 2);
if ( v2 == -1 )
{
*((_BYTE *)a1 + 26) = 1;
v2 = open(a2, 0);
if ( v2 == -1 )
{
perror(a2);
return;
}
}
v3 = lseek(v2, 0LL, 2);
*a1 = v3;
v4 = v3;
if ( v3 == -1 )
{
perror("seek");
v4 = *a1;
if ( (*a1 & 0x1FF) == 0 )
goto LABEL_4;
LABEL_11:
v7 = dcgettext(0LL, "%s is not an integer number of blocks: %d bytes\n", 5);
fprintf(stderr, v7, a2, v4);
close(v2);
return;
}
if ( (v3 & 0x1FF) != 0 )
goto LABEL_11;
LABEL_4:
v5 = mmap(0LL, v4, *((_BYTE *)a1 + 26) == 0 ? 3 : 1, 1, v2, 0LL);
*((_QWORD *)a1 + 1) = v5;
if ( v5 == (void *)-1LL )
{
*((_QWORD *)a1 + 1) = 0LL;
perror(a2);
}
if ( *((_BYTE *)a1 + 26) )
{
v6 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v6, a2);
}
}

original_code:
void disk_open(disk_t * pdt, char * name) {
 int fd = open(name, 02|0);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00|0);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length == -1) perror("seek");
 if (pdt->length % 512) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks: %d bytes\n"), name, pdt->length);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

canonical_original_code:
void disk_open(struct disk_t *pdt, char *name) {
 int fd = open(name, 02|0);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00|0);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length == -1) perror("seek");
 if (pdt->length % 512) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks: %d bytes\n"), name, pdt->length);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

variable_types: struct disk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; unsigned char side; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char need_sidetrk; unsigned char need_sectsize; unsigned char cursec; } *

variable_types:
name
char *


decompiled_code:
__int64 disk_init()
{
__int64 *v0; // rax

if ( !init_done_1 )
{
disk_open((unsigned int *)&disks, floppyA);
disk_open((unsigned int *)(unsigned int)((_DWORD)&disks + 40), floppyB);
disk_open((unsigned int *)(unsigned int)((_DWORD)&disks + 80), floppyC);
disk_open((unsigned int *)(unsigned int)((_DWORD)&disks + 120), floppyD);
init_done_1 = 1;
}
v0 = &disks;
do
{
v0[2] = 0LL;
v0 += 5;
*((_BYTE *)v0 - 12) = 0;
*((_BYTE *)v0 - 13) = 0;
*((_BYTE *)v0 - 15) = 0;
*((_BYTE *)v0 - 16) = 0;
}
while ( v0 != &disks + 20 );
selected = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 *v0; // rax

if ( !init_done_1 )
{
func0((unsigned int *)&disks, floppyA);
func0((unsigned int *)(unsigned int)((_DWORD)&disks + 40), floppyB);
func0((unsigned int *)(unsigned int)((_DWORD)&disks + 80), floppyC);
func0((unsigned int *)(unsigned int)((_DWORD)&disks + 120), floppyD);
init_done_1 = 1;
}
v0 = &disks;
do
{
v0[2] = 0LL;
v0 += 5;
*((_BYTE *)v0 - 12) = 0;
*((_BYTE *)v0 - 13) = 0;
*((_BYTE *)v0 - 15) = 0;
*((_BYTE *)v0 - 16) = 0;
}
while ( v0 != &disks + 20 );
selected = -1;
return 0LL;
}

original_code:
int disk_init() {
 static char init_done = 0;
 int i;
 if (!init_done) {
  disk_open(&disks[0], floppyA);
  disk_open(&disks[1], floppyB);
  disk_open(&disks[2], floppyC);
  disk_open(&disks[3], floppyD);
  init_done = 1;
 }
 for (i = 0; i < 4; i++) {
  disks[i].ptr = ((void *)0);
  disks[i].track = disks[i].side =
  disks[i].motor = disks[i].inprogress = 0;
 }
 selected = -1;
 return 0;
}


decompiled_code:
int disk_finish()
{
__int64 *v0; // rbx
void *v1; // rdi
int result; // eax

v0 = &disks;
do
{
v1 = (void *)v0[1];
if ( v1 )
result = munmap(v1, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != &disks + 20 );
return result;
}

canonical_decompiled_code:
int func2()
{
__int64 *v0; // rbx
void *v1; // rdi
int result; // eax

v0 = &disks;
do
{
v1 = (void *)v0[1];
if ( v1 )
result = munmap(v1, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != &disks + 20 );
return result;
}

original_code:
void disk_finish() {
 int i;
 for (i = 0; i < 4; i++) {
  if (!disks[i].image)
   continue;
  munmap(disks[i].image, disks[i].length);
 }
}


original_code:
int index_flag() {
 extern double ticks;
 unsigned msec = ticks / (TICK_RATE/1000);
 return (msec % 100 == 0);
}

canonical_original_code:
int index_flag() {
 extern double ticks;
 unsigned int msec = ticks / (TICK_RATE/1000);
 return (msec % 100 == 0);
}

variable_types: ticks double

variable_types:
msec
unsigned int


original_code:
unsigned short
disk_read_word(disk_t * pdt) {
 unsigned short ret;
 if (pdt->need_sidetrk) {
  ret = (pdt->side << 8) | pdt->track;
  pdt->need_sidetrk = 0;
  pdt->need_sectsize = 1;
 } else if (pdt->need_sectsize) {
  ret = ((pdt->cursec+1) << 8) | 2;
  pdt->need_sectsize = 0;
  pdt->ptr = data_marker;
 } else {
  ret = *pdt->ptr++;
  if (pdt->ptr == index_marker + (sizeof(index_marker)/sizeof(*index_marker))) {
   pdt->need_sidetrk = 1;
  } else if (pdt->ptr == data_marker + (sizeof(data_marker)/sizeof(*data_marker))) {
   pdt->ptr = pdt->image +
   (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec) * 512/2;
  } else if (pdt->ptr ==
   pdt->image + (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec+1) * 512/2) {
   pdt->ptr = end_marker;
  } else if (pdt->ptr == end_marker + (sizeof(end_marker)/sizeof(*end_marker))) {
   if (++pdt->cursec == 10) pdt->inprogress = 0;
   pdt->ptr = index_marker;
  }
 }
 return ret;
}

canonical_original_code:
unsigned short disk_read_word(struct disk_t *pdt) {
 unsigned short ret;
 if (pdt->need_sidetrk) {
  ret = (pdt->side << 8) | pdt->track;
  pdt->need_sidetrk = 0;
  pdt->need_sectsize = 1;
 } else if (pdt->need_sectsize) {
  ret = ((pdt->cursec+1) << 8) | 2;
  pdt->need_sectsize = 0;
  pdt->ptr = data_marker;
 } else {
  ret = *pdt->ptr++;
  if (pdt->ptr == index_marker + (sizeof(index_marker)/sizeof(*index_marker))) {
   pdt->need_sidetrk = 1;
  } else if (pdt->ptr == data_marker + (sizeof(data_marker)/sizeof(*data_marker))) {
   pdt->ptr = pdt->image +
   (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec) * 512/2;
  } else if (pdt->ptr ==
   pdt->image + (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec+1) * 512/2) {
   pdt->ptr = end_marker;
  } else if (pdt->ptr == end_marker + (sizeof(end_marker)/sizeof(*end_marker))) {
   if (++pdt->cursec == 10) pdt->inprogress = 0;
   pdt->ptr = index_marker;
  }
 }
 return ret;
}

variable_types:
ret
unsigned short

return_type: unsigned short

user_defined_types: struct disk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; unsigned char side; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char need_sidetrk; unsigned char need_sectsize; unsigned char cursec; }


decompiled_code:
__int64 __fastcall disk_write(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
char *v3; // rax
char *v5; // rax
__int64 v6; // rdx
const char *v7; // rcx
FILE *v8; // rdi
__int16 v9; // bx
__int64 *v10; // rax
bool v11; // zf

v2 = a1 + 424;
if ( v2 )
{
if ( v2 == 2 )
{
v3 = dcgettext(0LL, "Writing 177132, data %06o\n", 5);
fprintf(stderr, v3, a2);
}
return 0LL;
}
if ( !a2 )
{
LABEL_6:
selected = -1;
return 0LL;
}
v5 = dcgettext(0LL, "Writing 177130, data %06o\n", 5);
fprintf(stderr, v5, a2);
switch ( a2 & 0xF )
{
case 0:
goto LABEL_6;
case 2:
case 6:
case 0xA:
case 0xE:
selected = 1;
v6 = 1LL;
break;
case 4:
case 0xC:
selected = 2;
v6 = 2LL;
break;
case 8:
selected = 3;
v6 = 3LL;
break;
default:
selected = 0;
v6 = 0LL;
break;
}
v7 = "ON";
v8 = stderr;
v9 = HIBYTE(a2) & 1;
v10 = &disks[5 * (int)v6];
v11 = ((unsigned __int8)v9 | *((_BYTE *)v10 + 28)) == 0;
*((_BYTE *)v10 + 28) |= v9;
if ( v11 )
v7 = "OFF";
fprintf(v8, "Drive %d i/o %s\n", v6, v7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
char *v3; // rax
char *v5; // rax
__int64 v6; // rdx
const char *v7; // rcx
FILE *v8; // rdi
__int16 v9; // bx
__int64 *v10; // rax
bool v11; // zf

v2 = a1 + 424;
if ( v2 )
{
if ( v2 == 2 )
{
v3 = dcgettext(0LL, "Writing 177132, data %06o\n", 5);
fprintf(stderr, v3, a2);
}
return 0LL;
}
if ( !a2 )
{
LABEL_6:
selected = -1;
return 0LL;
}
v5 = dcgettext(0LL, "Writing 177130, data %06o\n", 5);
fprintf(stderr, v5, a2);
switch ( a2 & 0xF )
{
case 0:
goto LABEL_6;
case 2:
case 6:
case 0xA:
case 0xE:
selected = 1;
v6 = 1LL;
break;
case 4:
case 0xC:
selected = 2;
v6 = 2LL;
break;
case 8:
selected = 3;
v6 = 3LL;
break;
default:
selected = 0;
v6 = 0LL;
break;
}
v7 = "ON";
v8 = stderr;
v9 = HIBYTE(a2) & 1;
v10 = &disks[5 * (int)v6];
v11 = ((unsigned __int8)v9 | *((_BYTE *)v10 + 28)) == 0;
*((_BYTE *)v10 + 28) |= v9;
if ( v11 )
v7 = "OFF";
fprintf(v8, "Drive %d i/o %s\n", v6, v7);
return 0LL;
}

original_code:
int
disk_write(c_addr addr, d_word word) {
 d_word offset = addr - 0177130;
 disk_t * pdt;
 switch (offset) {
 case 0:
  if (word) {
   fprintf(stderr, gettext ("Writing 177130, data %06o\n"), word);
  }
  switch (word & 0xf) {
  case 0: selected = -1; break;
  case 1: default: selected = 0; break;
  case 2: case 6: case 10: case 14: selected = 1; break;
  case 4: case 12: selected = 2; break;
  case 8: selected = 3; break;
  }
  if (selected >= 0) {
   pdt = &disks[selected];
   pdt->inprogress |= !! (word & 0400);
   fprintf(stderr, "Drive %d i/o %s\n", selected,
    pdt->inprogress ? "ON" : "OFF");
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing 177132, data %06o\n"), word);
  break;
 }
 return 0;
}

canonical_original_code:
int disk_write(unsigned long addr, unsigned short word) {
 unsigned short offset = addr - 0177130;
 struct disk_t *pdt;
 switch (offset) {
 case 0:
  if (word) {
   fprintf(stderr, gettext ("Writing 177130, data %06o\n"), word);
  }
  switch (word & 0xf) {
  case 0: selected = -1; break;
  case 1: default: selected = 0; break;
  case 2: case 6: case 10: case 14: selected = 1; break;
  case 4: case 12: selected = 2; break;
  case 8: selected = 3; break;
  }
  if (selected >= 0) {
   pdt = &disks[selected];
   pdt->inprogress |= !! (word & 0400);
   fprintf(stderr, "Drive %d i/o %s\n", selected,
    pdt->inprogress ? "ON" : "OFF");
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing 177132, data %06o\n"), word);
  break;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short

variable_types:
offset
unsigned short


decompiled_code:
__int64 __fastcall disk_bwrite(__int16 a1, unsigned __int8 a2)
{
unsigned __int16 v2; // di
const char *v4; // rsi
char *v5; // rax

v2 = a1 + 424;
if ( v2 == 2 )
{
v4 = "Writing byte 177132, data %03o\n";
goto LABEL_5;
}
if ( v2 <= 2u )
{
v4 = "Writing byte 177131, data %03o\n";
if ( !v2 )
v4 = "Writing byte 177130, data %03o\n";
goto LABEL_5;
}
v4 = "Writing byte 177133, data %03o\n";
if ( v2 == 3 )
{
LABEL_5:
v5 = dcgettext(0LL, v4, 5);
fprintf(stderr, v5, a2);
return 0LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1, unsigned __int8 a2)
{
unsigned __int16 v2; // di
const char *v4; // rsi
char *v5; // rax

v2 = a1 + 424;
if ( v2 == 2 )
{
v4 = "Writing byte 177132, data %03o\n";
goto LABEL_5;
}
if ( v2 <= 2u )
{
v4 = "Writing byte 177131, data %03o\n";
if ( !v2 )
v4 = "Writing byte 177130, data %03o\n";
goto LABEL_5;
}
v4 = "Writing byte 177133, data %03o\n";
if ( v2 == 3 )
{
LABEL_5:
v5 = dcgettext(0LL, v4, 5);
fprintf(stderr, v5, a2);
return 0LL;
}
return 0LL;
}

original_code:
int
disk_bwrite(c_addr addr, d_byte byte) {
 d_word offset = addr - 0177130;
 switch (offset) {
 case 0:
  fprintf(stderr, gettext ("Writing byte 177130, data %03o\n"), byte);
  break;
 case 1:
  fprintf(stderr, gettext ("Writing byte 177131, data %03o\n"), byte);
  break;
 case 2:
  fprintf(stderr, gettext ("Writing byte 177132, data %03o\n"), byte);
  break;
 case 3:
  fprintf(stderr, gettext ("Writing byte 177133, data %03o\n"), byte);
  break;
 }
 return 0;
}

canonical_original_code:
int disk_bwrite(unsigned long addr, unsigned char byte) {
 unsigned short offset = addr - 0177130;
 switch (offset) {
 case 0:
  fprintf(stderr, gettext ("Writing byte 177130, data %03o\n"), byte);
  break;
 case 1:
  fprintf(stderr, gettext ("Writing byte 177131, data %03o\n"), byte);
  break;
 case 2:
  fprintf(stderr, gettext ("Writing byte 177132, data %03o\n"), byte);
  break;
 case 3:
  fprintf(stderr, gettext ("Writing byte 177133, data %03o\n"), byte);
  break;
 }
 return 0;
}

variable_types:
byte
unsigned char


decompiled_code:
int __fastcall do_disk_io(unsigned int a1, unsigned int a2, int a3, unsigned int a4)
{
__int64 v5; // r13
unsigned int v6; // ebx
char *v7; // r12
char *v8; // rax
__int64 *v9; // rbp
unsigned int v10; // er12
int v11; // er15
__int64 v12; // rbx
char *v13; // rax
int v15; // er13
int v16; // er15
__int64 v17; // rbx
unsigned int v18; // er14
char *v19; // rax
char *v20; // rax
char *v21; // rax
char *v22; // rax
char *v23; // rax
int v24; // [rsp+Ch] [rbp-4Ch]
int v25; // [rsp+Ch] [rbp-4Ch]
__int16 v26[29]; // [rsp+1Eh] [rbp-3Ah] BYREF

v5 = a4;
if ( (a3 & 0x8000) != 0 )
{
v6 = (unsigned __int16)-(__int16)a3;
v7 = dcgettext(0LL, "Writing", 5);
}
else
{
v6 = a3;
v7 = dcgettext(0LL, "Reading", 5);
}
v8 = dcgettext(0LL, "%s block %d (%d words) from drive %d @ addr %06o\n", 5);
fprintf(stderr, v8, v7, a2, v6, a1, v5);
pdp[16] |= 1u;
sl_byte(pdp, 42LL, 0LL);
v9 = &disks[5 * (int)a1];
if ( !disks[5 * (int)a1 + 1] )
{
v22 = dcgettext(0LL, "Disk not ready\n", 5);
fprintf(stderr, v22);
sl_byte(pdp, 42LL, 6LL);
goto LABEL_13;
}
v10 = disks[5 * (int)a1];
if ( v10 >> 9 <= a2 )
{
v19 = dcgettext(0LL, "Block number %d too large for image size %d\n", 5);
fprintf(stderr, v19, a2, v10);
sl_byte(pdp, 42LL, 5LL);
goto LABEL_13;
}
if ( a3 <= 0x7FFF )
{
if ( a3 <= 0 )
{
v11 = 0;
}
else
{
v11 = 0;
v24 = v5 + 2 * a3;
v12 = 2LL * (int)(a2 << 8);
while ( v10 >> 1 > v11 + (a2 << 8) )
{
if ( (unsigned int)sc_word((unsigned int)v5, *(unsigned __int16 *)(v9[1] + v12)) )
{
v23 = dcgettext(0LL, "Read failure @ %06o\n", 5);
fprintf(stderr, v23, (unsigned int)v5);
sl_byte(pdp, 42LL, 7LL);
break;
}
++v11;
LODWORD(v5) = v5 + 2;
v12 += 2LL;
if ( (_DWORD)v5 == v24 )
break;
v10 = *(_DWORD *)v9;
}
}
if ( a3 != v11 )
goto LABEL_13;
LABEL_25:
pdp[16] &= 0xFEu;
goto LABEL_13;
}
if ( *((_BYTE *)v9 + 26) )
{
v21 = dcgettext(0LL, "Disk is read-only\n", 5);
fprintf(stderr, v21);
sl_byte(pdp, 42LL, 1LL);
goto LABEL_13;
}
v25 = (unsigned __int16)-(__int16)a3;
if ( !(_WORD)a3 )
goto LABEL_25;
v15 = v5 - (a2 << 9);
v16 = 0;
v17 = 2LL * (int)(a2 << 8);
while ( 1 )
{
v18 = v15 + v17;
if ( v10 >> 1 <= v16 + (a2 << 8) )
break;
if ( (unsigned int)lc_word(v18, v26) )
{
v20 = dcgettext(0LL, "Write failure @ %06o\n", 5);
fprintf(stderr, v20, v18);
sl_byte(pdp, 42LL, 7LL);
if ( v16 != v25 )
break;
goto LABEL_25;
}
++v16;
*(_WORD *)(v9[1] + v17) = v26[0];
v17 += 2LL;
if ( v25 == v16 )
goto LABEL_25;
v10 = *(_DWORD *)v9;
}
LABEL_13:
ticks = (double)(TICK_RATE / 100) + ticks;
pop(pdp, (unsigned int)((_DWORD)pdp + 14));
v13 = dcgettext(0LL, "Done\n", 5);
return fprintf(stderr, v13);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, unsigned int a2, int a3, unsigned int a4)
{
__int64 v5; // r13
unsigned int v6; // ebx
char *v7; // r12
char *v8; // rax
__int64 *v9; // rbp
unsigned int v10; // er12
int v11; // er15
__int64 v12; // rbx
char *v13; // rax
int v15; // er13
int v16; // er15
__int64 v17; // rbx
unsigned int v18; // er14
char *v19; // rax
char *v20; // rax
char *v21; // rax
char *v22; // rax
char *v23; // rax
int v24; // [rsp+Ch] [rbp-4Ch]
int v25; // [rsp+Ch] [rbp-4Ch]
__int16 v26[29]; // [rsp+1Eh] [rbp-3Ah] BYREF

v5 = a4;
if ( (a3 & 0x8000) != 0 )
{
v6 = (unsigned __int16)-(__int16)a3;
v7 = dcgettext(0LL, "Writing", 5);
}
else
{
v6 = a3;
v7 = dcgettext(0LL, "Reading", 5);
}
v8 = dcgettext(0LL, "%s block %d (%d words) from drive %d @ addr %06o\n", 5);
fprintf(stderr, v8, v7, a2, v6, a1, v5);
pdp[16] |= 1u;
sl_byte(pdp, 42LL, 0LL);
v9 = &disks[5 * (int)a1];
if ( !disks[5 * (int)a1 + 1] )
{
v22 = dcgettext(0LL, "Disk not ready\n", 5);
fprintf(stderr, v22);
sl_byte(pdp, 42LL, 6LL);
goto LABEL_13;
}
v10 = disks[5 * (int)a1];
if ( v10 >> 9 <= a2 )
{
v19 = dcgettext(0LL, "Block number %d too large for image size %d\n", 5);
fprintf(stderr, v19, a2, v10);
sl_byte(pdp, 42LL, 5LL);
goto LABEL_13;
}
if ( a3 <= 0x7FFF )
{
if ( a3 <= 0 )
{
v11 = 0;
}
else
{
v11 = 0;
v24 = v5 + 2 * a3;
v12 = 2LL * (int)(a2 << 8);
while ( v10 >> 1 > v11 + (a2 << 8) )
{
if ( (unsigned int)sc_word((unsigned int)v5, *(unsigned __int16 *)(v9[1] + v12)) )
{
v23 = dcgettext(0LL, "Read failure @ %06o\n", 5);
fprintf(stderr, v23, (unsigned int)v5);
sl_byte(pdp, 42LL, 7LL);
break;
}
++v11;
LODWORD(v5) = v5 + 2;
v12 += 2LL;
if ( (_DWORD)v5 == v24 )
break;
v10 = *(_DWORD *)v9;
}
}
if ( a3 != v11 )
goto LABEL_13;
LABEL_25:
pdp[16] &= 0xFEu;
goto LABEL_13;
}
if ( *((_BYTE *)v9 + 26) )
{
v21 = dcgettext(0LL, "Disk is read-only\n", 5);
fprintf(stderr, v21);
sl_byte(pdp, 42LL, 1LL);
goto LABEL_13;
}
v25 = (unsigned __int16)-(__int16)a3;
if ( !(_WORD)a3 )
goto LABEL_25;
v15 = v5 - (a2 << 9);
v16 = 0;
v17 = 2LL * (int)(a2 << 8);
while ( 1 )
{
v18 = v15 + v17;
if ( v10 >> 1 <= v16 + (a2 << 8) )
break;
if ( (unsigned int)lc_word(v18, v26) )
{
v20 = dcgettext(0LL, "Write failure @ %06o\n", 5);
fprintf(stderr, v20, v18);
sl_byte(pdp, 42LL, 7LL);
if ( v16 != v25 )
break;
goto LABEL_25;
}
++v16;
*(_WORD *)(v9[1] + v17) = v26[0];
v17 += 2LL;
if ( v25 == v16 )
goto LABEL_25;
v10 = *(_DWORD *)v9;
}
LABEL_13:
ticks = (double)(TICK_RATE / 100) + ticks;
pop(pdp, (unsigned int)((_DWORD)pdp + 14));
v13 = dcgettext(0LL, "Done\n", 5);
return fprintf(stderr, v13);
}

original_code:
void do_disk_io(int drive, int blkno, int nwords, int ioaddr) {
 fprintf(stderr, gettext ("%s block %d (%d words) from drive %d @ addr %06o\n"),
  nwords & 0100000 ? gettext ("Writing") : gettext ("Reading"), blkno,
  nwords & 0100000 ? -nwords & 0xffff : nwords, drive, ioaddr);
 pdp.psw |= 01;
 sl_byte(&pdp, 052, 0);
 if (disks[drive].image == 0) {
  fprintf(stderr, gettext ("Disk not ready\n"));
  sl_byte(&pdp, 052, 6);
 } else if (blkno >= disks[drive].length / 512) {
  fprintf(stderr, gettext ("Block number %d too large for image size %d\n"),
   blkno, disks[drive].length);
  sl_byte(&pdp, 052, 5);
 } else if (nwords < 0100000) {
  int i;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   if (0 != sc_word(ioaddr, disks[drive].image[256 * blkno + i])) {
    fprintf(stderr, gettext ("Read failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
    break;
   }
  }
  if (i == nwords) pdp.psw &= ~01;
 } else if (!disks[drive].ro) {
  int i;
  nwords = -nwords & 0xffff;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   d_word word;
                        if (0 != lc_word(ioaddr, &word)) {
                                fprintf(stderr, gettext ("Write failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
                                break;
                        }
   disks[drive].image[256 * blkno + i] = word;
                }
                if (i == nwords) pdp.psw &= ~01;
 } else {
  fprintf(stderr, gettext ("Disk is read-only\n"));
  sl_byte(&pdp, 052, 1);
 }
 ticks += TICK_RATE/100;
        pop( &pdp, &pdp.regs[7] );
 fprintf(stderr, gettext ("Done\n"));
}

canonical_original_code:
void do_disk_io(int drive, int blkno, int nwords, int ioaddr) {
 fprintf(stderr, gettext ("%s block %d (%d words) from drive %d @ addr %06o\n"),
  nwords & 0100000 ? gettext ("Writing") : gettext ("Reading"), blkno,
  nwords & 0100000 ? -nwords & 0xffff : nwords, drive, ioaddr);
 pdp.psw |= 01;
 sl_byte(&pdp, 052, 0);
 if (disks[drive].image == 0) {
  fprintf(stderr, gettext ("Disk not ready\n"));
  sl_byte(&pdp, 052, 6);
 } else if (blkno >= disks[drive].length / 512) {
  fprintf(stderr, gettext ("Block number %d too large for image size %d\n"),
   blkno, disks[drive].length);
  sl_byte(&pdp, 052, 5);
 } else if (nwords < 0100000) {
  int i;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   if (0 != sc_word(ioaddr, disks[drive].image[256 * blkno + i])) {
    fprintf(stderr, gettext ("Read failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
    break;
   }
  }
  if (i == nwords) pdp.psw &= ~01;
 } else if (!disks[drive].ro) {
  int i;
  nwords = -nwords & 0xffff;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   unsigned short word;
                        if (0 != lc_word(ioaddr, &word)) {
                                fprintf(stderr, gettext ("Write failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
                                break;
                        }
   disks[drive].image[256 * blkno + i] = word;
                }
                if (i == nwords) pdp.psw &= ~01;
 } else {
  fprintf(stderr, gettext ("Disk is read-only\n"));
  sl_byte(&pdp, 052, 1);
 }
 ticks += TICK_RATE/100;
        pop( &pdp, &pdp.regs[7] );
 fprintf(stderr, gettext ("Done\n"));
}

variable_types: drive int

variable_types: blkno int

variable_types: nwords int

variable_types: ioaddr int


decompiled_code:
int fake_disk_io()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
unsigned __int16 v2; // r12
__int16 v4[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(_WORD *)pdp;
v1 = *(_WORD *)&pdp[2];
v2 = *(_WORD *)&pdp[4];
lc_word(*(unsigned __int16 *)&pdp[6] + 28LL, v4);
return do_disk_io(v4[0] & 3, v0, v1, v2);
}

canonical_decompiled_code:
int func9()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
unsigned __int16 v2; // r12
__int16 v4[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(_WORD *)pdp;
v1 = *(_WORD *)&pdp[2];
v2 = *(_WORD *)&pdp[4];
lc_word(*(unsigned __int16 *)&pdp[6] + 28LL, v4);
return func8(v4[0] & 3, v0, v1, v2);
}

original_code:
void fake_disk_io() {
 d_word blkno = pdp.regs[0];
 d_word nwords = pdp.regs[1];
 c_addr ioaddr = pdp.regs[2];
 c_addr parms = pdp.regs[3];
 d_word drive;
 lc_word(parms+034, &drive);
 drive &= 3;
 do_disk_io(drive, blkno, nwords, ioaddr);
}

canonical_original_code:
void fake_disk_io() {
 unsigned short blkno = pdp.regs[0];
 unsigned short nwords = pdp.regs[1];
 unsigned long ioaddr = pdp.regs[2];
 unsigned long parms = pdp.regs[3];
 unsigned short drive;
 lc_word(parms+034, &drive);
 drive &= 3;
 do_disk_io(drive, blkno, nwords, ioaddr);
}

variable_types:
blkno
unsigned short

variable_types:
nwords
unsigned short

variable_types:
ioaddr
unsigned long

variable_types:
parms
unsigned long

variable_types:
drive
unsigned short


decompiled_code:
int fake_sector_io()
{
__int64 v0; // rbx
int v1; // ebp
unsigned __int16 v3; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v5; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(unsigned __int16 *)&pdp[6];
lc_word(v0 + 28, &v3);
lc_word(v0 + 22, &v5);
lc_word(v0 + 24, &v4);
v1 = HIBYTE(v3);
v3 &= 3u;
lc_word(v0 + 26, &v6);
return do_disk_io(v3, v1 + 10 * ((v6 & 1) + 2 * HIBYTE(v6)) - 1, v4, v5);
}

canonical_decompiled_code:
int func10()
{
__int64 v0; // rbx
int v1; // ebp
unsigned __int16 v3; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v5; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(unsigned __int16 *)&pdp[6];
lc_word(v0 + 28, &v3);
lc_word(v0 + 22, &v5);
lc_word(v0 + 24, &v4);
v1 = HIBYTE(v3);
v3 &= 3u;
lc_word(v0 + 26, &v6);
return func8(v3, v1 + 10 * ((v6 & 1) + 2 * HIBYTE(v6)) - 1, v4, v5);
}

original_code:
void fake_sector_io() {
 c_addr parms = pdp.regs[3];
 d_word drive;
 d_word nwords, ioaddr, side, trk, sector;
 int blkno;
        lc_word(parms+034, &drive);
 lc_word(parms+026, &ioaddr);
 lc_word(parms+030, &nwords);
 sector = drive >> 8;
 drive &= 3;
 lc_word(parms+032, &side);
 trk = side >> 8;
 side &= 1;
 blkno = sector-1 + 10 * (side + 2 * trk);
 do_disk_io(drive, blkno, nwords, ioaddr);
}

canonical_original_code:
void fake_sector_io() {
 unsigned long parms = pdp.regs[3];
 unsigned short drive;
 unsigned short nwords; unsigned short ioaddr; unsigned short side; unsigned short trk; unsigned short sector;
 int blkno;
        lc_word(parms+034, &drive);
 lc_word(parms+026, &ioaddr);
 lc_word(parms+030, &nwords);
 sector = drive >> 8;
 drive &= 3;
 lc_word(parms+032, &side);
 trk = side >> 8;
 side &= 1;
 blkno = sector-1 + 10 * (side + 2 * trk);
 do_disk_io(drive, blkno, nwords, ioaddr);
}

variable_types:
ioaddr
unsigned short

variable_types:
side
unsigned short

variable_types:
trk
unsigned short

variable_types:
sector
unsigned short


decompiled_code:
void tape_init()
{
char *v0; // rax

if ( tape_read_file )
{
if ( fake_tape )
{
fclose(tape_read_file);
fake_state = 0;
}
else
{
pclose(tape_read_file);
}
tape_read_file = 0LL;
}
if ( fake_tape )
{
if ( tape_write_file )
{
fclose(tape_write_file);
tape_write_file = 0LL;
}
}
else if ( !tape_write_file )
{
tape_write_file = popen("readtape", "w");
if ( tape_write_file )
{
v0 = dcgettext(0LL, "readtape open successful\n", 5);
fprintf(stderr, v0);
}
else
{
perror("readtape");
}
}
}

canonical_decompiled_code:
void func0()
{
char *v0; // rax

if ( tape_read_file )
{
if ( fake_tape )
{
fclose(tape_read_file);
fake_state = 0;
}
else
{
pclose(tape_read_file);
}
tape_read_file = 0LL;
}
if ( fake_tape )
{
if ( tape_write_file )
{
fclose(tape_write_file);
tape_write_file = 0LL;
}
}
else if ( !tape_write_file )
{
tape_write_file = popen("readtape", "w");
if ( tape_write_file )
{
v0 = dcgettext(0LL, "readtape open successful\n", 5);
fprintf(stderr, v0);
}
else
{
perror("readtape");
}
}
}

original_code:
void tape_init() {
 if (tape_read_file) {
     if (fake_tape) {
  fclose(tape_read_file);
  fake_state = Idle;
     } else {
  pclose(tape_read_file);
     }
     tape_read_file = ((void *)0);
 }
 if (fake_tape) {
     if (tape_write_file) {
  fclose(tape_write_file);
  tape_write_file = 0;
     }
 } else if (tape_write_file == ((void *)0)) {
  tape_write_file = popen("readtape", "w");
  if (tape_write_file) {
   fprintf(stderr, gettext ("readtape open successful\n"));
  } else perror("readtape");
 }
}