main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__pid_t v4; // ebx
__int64 v5; // rcx
struct termios *v6; // rsi
struct termios *v7; // rdi
int v8; // er15
int result; // eax
__pid_t v10; // eax
__pid_t pid; // [rsp+Ch] [rbp-221Ch]
char buf; // [rsp+1Fh] [rbp-2209h] BYREF
int amaster; // [rsp+20h] [rbp-2208h] BYREF
int stat_loc; // [rsp+24h] [rbp-2204h] BYREF
struct winsize winp; // [rsp+28h] [rbp-2200h] BYREF
struct termios termios_p; // [rsp+30h] [rbp-21F8h] BYREF
struct termios v17; // [rsp+70h] [rbp-21B8h] BYREF
fd_set readfds; // [rsp+B0h] [rbp-2178h] BYREF
struct sigaction v19; // [rsp+130h] [rbp-20F8h] BYREF
iconv_t v20; // [rsp+1D0h] [rbp-2058h]
__int64 v21; // [rsp+1D8h] [rbp-2050h]
iconv_t v22; // [rsp+11E0h] [rbp-1048h]
__int64 v23; // [rsp+11E8h] [rbp-1040h]

setlocale(0, "");
if ( argc <= 1 )
{
fprintf(stderr, "Usage: %s <command> [args...]\n", *argv);
return 2;
}
if ( tcgetattr(0, &termios_p) )
{
perror("tcgetattr");
return 1;
}
if ( ioctl(0, 0x5413uLL, &winp) < 0 )
{
perror("tty_ioctl(TIOCGWINSZ)");
return 1;
}
v20 = iconv_open("CP437//TRANSLIT", "");
if ( v20 == (iconv_t)-1LL )
{
perror("iconv_open(CP437//TRANSLIT, \"\")");
return 1;
}
v22 = iconv_open("//TRANSLIT", "CP437");
if ( v22 == (iconv_t)-1LL )
{
perror("iconv_open(//TRANSLIT, \"CP437\")");
return 1;
}
v21 = 0LL;
v23 = 0LL;
v3 = forkpty(&amaster, 0LL, &termios_p, &winp);
v4 = v3;
if ( v3 == -1 )
{
perror("forkpty");
return 1;
}
if ( !v3 )
{
putenv("LANG=C");
execvp(argv[1], (char *const *)argv + 1);
perror("exec");
return 1;
}
v5 = 15LL;
v6 = &termios_p;
v7 = &v17;
while ( v5 )
{
v7->c_iflag = v6->c_iflag;
v6 = (struct termios *)((char *)v6 + 4);
v7 = (struct termios *)((char *)v7 + 4);
--v5;
}
cfmakeraw(&termios_p);
tcsetattr(0, 0, &termios_p);
pipe(signal_pipe);
sigemptyset(&v19.sa_mask);
v19.sa_flags = 0x10000000;
v19.sa_handler = (__sighandler_t)sigwinch;
sigaction(28, &v19, 0LL);
v19.sa_handler = (__sighandler_t)sigchld;
sigaction(17, &v19, 0LL);
v8 = amaster + 1;
if ( signal_pipe[0] > amaster )
v8 = signal_pipe[0] + 1;
while ( 1 )
{
LABEL_13:
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
readfds.fds_bits[amaster / 64] |= 1LL << (amaster % 64);
readfds.fds_bits[signal_pipe[0] / 64] |= 1LL << (signal_pipe[0] % 64);
if ( select(v8, &readfds, 0LL, 0LL, 0LL) >= 0 )
break;
if ( *__errno_location() != 4 )
goto LABEL_31;
}
if ( (readfds.fds_bits[0] & 1) != 0 && copy_converted(amaster, 0) <= 0
|| (readfds.fds_bits[amaster / 64] & (1LL << (amaster % 64))) != 0 && copy_converted(1, amaster) <= 0 )
{
break;
}
if ( (readfds.fds_bits[signal_pipe[0] / 64] & (1LL << (signal_pipe[0] % 64))) != 0 )
{
read(signal_pipe[0], &buf, 1uLL);
if ( buf == 67 )
{
while ( 1 )
{
v10 = waitpid(v4, &stat_loc, 3);
if ( v10 >= 0 )
break;
if ( *__errno_location() != 4 )
goto LABEL_13;
}
if ( v10 && (_BYTE)stat_loc == 127 )
{
pid = v10;
tcsetattr(0, 0, &v17);
raise(19);
kill(pid, 18);
tcsetattr(0, 0, &termios_p);
LABEL_19:
ioctl(0, 0x5413uLL, &winp);
ioctl(amaster, 0x5414uLL, &winp);
}
}
else if ( buf == 87 )
{
goto LABEL_19;
}
}
}
LABEL_31:
close(amaster);
while ( waitpid(v4, &stat_loc, 0) < 0 && *__errno_location() == 4 )
;
tcsetattr(0, 0, &v17);
tcsetattr(1, 0, &v17);
tcsetattr(2, 0, &v17);
if ( (stat_loc & 0x7F) != 0 )
result = 127;
else
result = BYTE1(stat_loc);
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__pid_t v4; // ebx
__int64 v5; // rcx
struct termios *v6; // rsi
struct termios *v7; // rdi
int v8; // er15
int result; // eax
__pid_t v10; // eax
__pid_t pid; // [rsp+Ch] [rbp-221Ch]
char buf; // [rsp+1Fh] [rbp-2209h] BYREF
int amaster; // [rsp+20h] [rbp-2208h] BYREF
int stat_loc; // [rsp+24h] [rbp-2204h] BYREF
struct winsize winp; // [rsp+28h] [rbp-2200h] BYREF
struct termios termios_p; // [rsp+30h] [rbp-21F8h] BYREF
struct termios v17; // [rsp+70h] [rbp-21B8h] BYREF
fd_set readfds; // [rsp+B0h] [rbp-2178h] BYREF
struct sigaction v19; // [rsp+130h] [rbp-20F8h] BYREF
iconv_t v20; // [rsp+1D0h] [rbp-2058h]
__int64 v21; // [rsp+1D8h] [rbp-2050h]
iconv_t v22; // [rsp+11E0h] [rbp-1048h]
__int64 v23; // [rsp+11E8h] [rbp-1040h]

setlocale(0, "");
if ( argc <= 1 )
{
fprintf(stderr, "Usage: %s <command> [args...]\n", *argv);
return 2;
}
if ( tcgetattr(0, &termios_p) )
{
perror("tcgetattr");
return 1;
}
if ( ioctl(0, 0x5413uLL, &winp) < 0 )
{
perror("tty_ioctl(TIOCGWINSZ)");
return 1;
}
v20 = iconv_open("CP437//TRANSLIT", "");
if ( v20 == (iconv_t)-1LL )
{
perror("iconv_open(CP437//TRANSLIT, \"\")");
return 1;
}
v22 = iconv_open("//TRANSLIT", "CP437");
if ( v22 == (iconv_t)-1LL )
{
perror("iconv_open(//TRANSLIT, \"CP437\")");
return 1;
}
v21 = 0LL;
v23 = 0LL;
v3 = forkpty(&amaster, 0LL, &termios_p, &winp);
v4 = v3;
if ( v3 == -1 )
{
perror("forkpty");
return 1;
}
if ( !v3 )
{
putenv("LANG=C");
execvp(argv[1], (char *const *)argv + 1);
perror("exec");
return 1;
}
v5 = 15LL;
v6 = &termios_p;
v7 = &v17;
while ( v5 )
{
v7->c_iflag = v6->c_iflag;
v6 = (struct termios *)((char *)v6 + 4);
v7 = (struct termios *)((char *)v7 + 4);
--v5;
}
cfmakeraw(&termios_p);
tcsetattr(0, 0, &termios_p);
pipe(signal_pipe);
sigemptyset(&v19.sa_mask);
v19.sa_flags = 0x10000000;
v19.sa_handler = (__sighandler_t)sigwinch;
sigaction(28, &v19, 0LL);
v19.sa_handler = (__sighandler_t)sigchld;
sigaction(17, &v19, 0LL);
v8 = amaster + 1;
if ( signal_pipe[0] > amaster )
v8 = signal_pipe[0] + 1;
while ( 1 )
{
LABEL_13:
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
readfds.fds_bits[amaster / 64] |= 1LL << (amaster % 64);
readfds.fds_bits[signal_pipe[0] / 64] |= 1LL << (signal_pipe[0] % 64);
if ( select(v8, &readfds, 0LL, 0LL, 0LL) >= 0 )
break;
if ( *__errno_location() != 4 )
goto LABEL_31;
}
if ( (readfds.fds_bits[0] & 1) != 0 && func9(amaster, 0) <= 0
|| (readfds.fds_bits[amaster / 64] & (1LL << (amaster % 64))) != 0 && func9(1, amaster) <= 0 )
{
break;
}
if ( (readfds.fds_bits[signal_pipe[0] / 64] & (1LL << (signal_pipe[0] % 64))) != 0 )
{
read(signal_pipe[0], &buf, 1uLL);
if ( buf == 67 )
{
while ( 1 )
{
v10 = waitpid(v4, &stat_loc, 3);
if ( v10 >= 0 )
break;
if ( *__errno_location() != 4 )
goto LABEL_13;
}
if ( v10 && (_BYTE)stat_loc == 127 )
{
pid = v10;
tcsetattr(0, 0, &v17);
raise(19);
kill(pid, 18);
tcsetattr(0, 0, &termios_p);
LABEL_19:
ioctl(0, 0x5413uLL, &winp);
ioctl(amaster, 0x5414uLL, &winp);
}
}
else if ( buf == 87 )
{
goto LABEL_19;
}
}
}
LABEL_31:
close(amaster);
while ( waitpid(v4, &stat_loc, 0) < 0 && *__errno_location() == 4 )
;
tcsetattr(0, 0, &v17);
tcsetattr(1, 0, &v17);
tcsetattr(2, 0, &v17);
if ( (stat_loc & 0x7F) != 0 )
result = 127;
else
result = BYTE1(stat_loc);
return result;
}

original_code:
int main(int argc, char *argv[])
{
 int master;
 int status;
 int nfds;
 pid_t childpid;
 struct termios term;
 struct termios term_orig;
 struct winsize win;
 struct conv to_child, from_child;
 struct sigaction sa;
 setlocale(0, "");
 if (argc < 2) {
  fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
  return 2;
 }
 if (tcgetattr(0, &term) != 0) {
  perror("tcgetattr");
  return 1;
 }
 if (ioctl(0, 0x5413, &win) < 0) {
  perror("tty_ioctl(TIOCGWINSZ)");
  return 1;
 }
 if ((to_child.cd = iconv_open("CP437//TRANSLIT", "")) == (iconv_t)-1) {
  perror("iconv_open(CP437//TRANSLIT, \"\")");
  return 1;
 }
 if ((from_child.cd = iconv_open("//TRANSLIT", "CP437")) == (iconv_t)-1) {
  perror("iconv_open(//TRANSLIT, \"CP437\")");
  return 1;
 }
 to_child.bytesleft = 0;
 from_child.bytesleft = 0;
 childpid = forkpty(&master, ((void *)0), &term, &win);
 switch(childpid)
 {
  case -1:
   perror("forkpty");
   return 1;
  case 0:
   putenv("LANG=C");
   execvp(argv[1], argv + 1);
   perror("exec");
   return 1;
  default:
   break;
 }
 term_orig = term;
 cfmakeraw(&term);
 tcsetattr(0, 0, &term);
 pipe(signal_pipe);
 sigemptyset(&sa.sa_mask);
 sa.sa_flags = 0x10000000;
 sa.__sigaction_handler.sa_handler = &sigwinch;
 sigaction(28, &sa, ((void *)0));
 sa.__sigaction_handler.sa_handler = &sigchld;
 sigaction(17, &sa, ((void *)0));
 if (signal_pipe[0] > master)
  nfds = signal_pipe[0] + 1;
 else
  nfds = master + 1;
 while (1) {
  ssize_t nbytes;
  fd_set readfds;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&readfds)->__fds_bits)[((master) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((master) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&readfds)->__fds_bits)[((signal_pipe[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((signal_pipe[0]) % (8 * (int) sizeof (__fd_mask)))))));
  if (select(nfds, &readfds, ((void *)0), ((void *)0), ((void *)0)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   else
    break;
  }
  if (((((&readfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   nbytes = copy_converted(master, 0, &to_child);
   if (nbytes < 1)
    break;
  }
  if (((((&readfds)->__fds_bits)[((master) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((master) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   nbytes = copy_converted(1, master, &from_child);
   if (nbytes < 1)
    break;
  }
  if (((((&readfds)->__fds_bits)[((signal_pipe[0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((signal_pipe[0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   char x;
   pid_t waited;
   read(signal_pipe[0], &x, 1);
   switch (x) {
    case 'W':
    ioctl(0, 0x5413, &win);
    ioctl(master, 0x5414, &win);
    break;
    case 'C':
    do {
     waited = waitpid(childpid, &status, 1 | 2);
    } while (waited < 0 && (*__errno_location ()) == 4);
    if (waited > 0 && (((status) & 0xff) == 0x7f)) {
     tcsetattr(0, 0, &term_orig);
     raise(19);
     kill(waited, 18);
     tcsetattr(0, 0, &term);
     ioctl(0, 0x5413, &win);
     ioctl(master, 0x5414, &win);
    }
    break;
   }
  }
 }
 close(master);
 while (waitpid(childpid, &status, 0) < 0 && (*__errno_location ()) == 4)
  ;
 tcsetattr(0, 0, &term_orig);
 tcsetattr(1, 0, &term_orig);
 tcsetattr(2, 0, &term_orig);
 return (((status) & 0x7f) == 0) ? (((status) & 0xff00) >> 8) : 127;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int master;
 int status;
 int nfds;
 int childpid;
 struct termios term;
 struct termios term_orig;
 struct winsize win;
 struct conv to_child; struct conv from_child;
 struct sigaction sa;
 setlocale(0, "");
 if (argc < 2) {
  fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
  return 2;
 }
 if (tcgetattr(0, &term) != 0) {
  perror("tcgetattr");
  return 1;
 }
 if (ioctl(0, 0x5413, &win) < 0) {
  perror("tty_ioctl(TIOCGWINSZ)");
  return 1;
 }
 if ((to_child.cd = iconv_open("CP437//TRANSLIT", "")) == (iconv_t)-1) {
  perror("iconv_open(CP437//TRANSLIT, \"\")");
  return 1;
 }
 if ((from_child.cd = iconv_open("//TRANSLIT", "CP437")) == (iconv_t)-1) {
  perror("iconv_open(//TRANSLIT, \"CP437\")");
  return 1;
 }
 to_child.bytesleft = 0;
 from_child.bytesleft = 0;
 childpid = forkpty(&master, ((void *)0), &term, &win);
 switch(childpid)
 {
  case -1:
   perror("forkpty");
   return 1;
  case 0:
   putenv("LANG=C");
   execvp(argv[1], argv + 1);
   perror("exec");
   return 1;
  default:
   break;
 }
 term_orig = term;
 cfmakeraw(&term);
 tcsetattr(0, 0, &term);
 pipe(signal_pipe);
 sigemptyset(&sa.sa_mask);
 sa.sa_flags = 0x10000000;
 sa.__sigaction_handler.sa_handler = &sigwinch;
 sigaction(28, &sa, ((void *)0));
 sa.__sigaction_handler.sa_handler = &sigchld;
 sigaction(17, &sa, ((void *)0));
 if (signal_pipe[0] > master)
  nfds = signal_pipe[0] + 1;
 else
  nfds = master + 1;
 while (1) {
  long nbytes;
  struct fd_set readfds;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&readfds)->__fds_bits)[((master) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((master) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&readfds)->__fds_bits)[((signal_pipe[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((signal_pipe[0]) % (8 * (int) sizeof (__fd_mask)))))));
  if (select(nfds, &readfds, ((void *)0), ((void *)0), ((void *)0)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   else
    break;
  }
  if (((((&readfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   nbytes = copy_converted(master, 0, &to_child);
   if (nbytes < 1)
    break;
  }
  if (((((&readfds)->__fds_bits)[((master) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((master) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   nbytes = copy_converted(1, master, &from_child);
   if (nbytes < 1)
    break;
  }
  if (((((&readfds)->__fds_bits)[((signal_pipe[0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((signal_pipe[0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   char x;
   int waited;
   read(signal_pipe[0], &x, 1);
   switch (x) {
    case 'W':
    ioctl(0, 0x5413, &win);
    ioctl(master, 0x5414, &win);
    break;
    case 'C':
    do {
     waited = waitpid(childpid, &status, 1 | 2);
    } while (waited < 0 && (*__errno_location ()) == 4);
    if (waited > 0 && (((status) & 0xff) == 0x7f)) {
     tcsetattr(0, 0, &term_orig);
     raise(19);
     kill(waited, 18);
     tcsetattr(0, 0, &term);
     ioctl(0, 0x5413, &win);
     ioctl(master, 0x5414, &win);
    }
    break;
   }
  }
 }
 close(master);
 while (waitpid(childpid, &status, 0) < 0 && (*__errno_location ()) == 4)
  ;
 tcsetattr(0, 0, &term_orig);
 tcsetattr(1, 0, &term_orig);
 tcsetattr(2, 0, &term_orig);
 return (((status) & 0x7f) == 0) ? (((status) & 0xff00) >> 8) : 127;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: master int

variable_types: status int

variable_types: nfds int

variable_types: childpid int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types: struct conv { void *cd; unsigned long bytesleft; char buf[4096]; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: nbytes long

variable_types:
readfds
struct fd_set { long __fds_bits[-1]; }

variable_types: x char

variable_types: waited int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


sigwinch

decompiled_code:
ssize_t sigwinch()
{
return write(fd, &unk_402004, 1uLL);
}

canonical_decompiled_code:
ssize_t func7()
{
return write(fd, &unk_402004, 1uLL);
}

original_code:
void sigwinch(int sig)
{
 write(signal_pipe[1], "W", 1);
}

variable_types: sig int

return_type: void


sigchld

decompiled_code:
ssize_t sigchld()
{
return write(fd, "C", 1uLL);
}

canonical_decompiled_code:
ssize_t func8()
{
return write(fd, "C", 1uLL);
}

original_code:
void sigchld(int sig)
{
 write(signal_pipe[1], "C", 1);
}


decompiled_code:
ssize_t __fastcall copy_converted(int fd, int a2, __int64 a3)
{
void *v3; // r14
__int64 v7; // rsi
ssize_t v8; // rax
ssize_t v9; // r13
void *v10; // rdi
int v11; // eax
size_t v12; // rdx
__int64 v14; // rax
char *inbuf; // [rsp+8h] [rbp-1050h] BYREF
char *outbuf; // [rsp+10h] [rbp-1048h] BYREF
size_t outbytesleft; // [rsp+18h] [rbp-1040h] BYREF
char buf[4152]; // [rsp+20h] [rbp-1038h] BYREF

v3 = (void *)(a3 + 16);
v7 = *(_QWORD *)(a3 + 8);
inbuf = (char *)(a3 + 16);
v8 = read(a2, (void *)(a3 + 16 + v7), 4096 - v7);
v9 = v8;
if ( v8 > 0 )
{
*(_QWORD *)(a3 + 8) += v8;
while ( 1 )
{
v10 = *(void **)a3;
outbuf = buf;
outbytesleft = 4096LL;
if ( iconv(v10, &inbuf, (size_t *)(a3 + 8), &outbuf, &outbytesleft) != -1LL )
break;
v11 = *__errno_location();
if ( v11 == 84 )
{
v14 = *(_QWORD *)(a3 + 8);
if ( !v14 )
__assert_fail("conv->bytesleft > 0", "cp437.c", 0x3Bu, "copy_converted");
++inbuf;
*(_QWORD *)(a3 + 8) = v14 - 1;
}
else if ( v11 != 7 )
{
break;
}
if ( outbuf > buf )
write(fd, buf, outbuf - buf);
}
if ( outbuf > buf )
write(fd, buf, outbuf - buf);
v12 = *(_QWORD *)(a3 + 8);
if ( v12 )
memmove(v3, inbuf, v12);
}
return v9;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int fd, int a2, __int64 a3)
{
void *v3; // r14
__int64 v7; // rsi
ssize_t v8; // rax
ssize_t v9; // r13
void *v10; // rdi
int v11; // eax
size_t v12; // rdx
__int64 v14; // rax
char *inbuf; // [rsp+8h] [rbp-1050h] BYREF
char *outbuf; // [rsp+10h] [rbp-1048h] BYREF
size_t outbytesleft; // [rsp+18h] [rbp-1040h] BYREF
char buf[4152]; // [rsp+20h] [rbp-1038h] BYREF

v3 = (void *)(a3 + 16);
v7 = *(_QWORD *)(a3 + 8);
inbuf = (char *)(a3 + 16);
v8 = read(a2, (void *)(a3 + 16 + v7), 4096 - v7);
v9 = v8;
if ( v8 > 0 )
{
*(_QWORD *)(a3 + 8) += v8;
while ( 1 )
{
v10 = *(void **)a3;
outbuf = buf;
outbytesleft = 4096LL;
if ( iconv(v10, &inbuf, (size_t *)(a3 + 8), &outbuf, &outbytesleft) != -1LL )
break;
v11 = *__errno_location();
if ( v11 == 84 )
{
v14 = *(_QWORD *)(a3 + 8);
if ( !v14 )
__assert_fail("conv->bytesleft > 0", "cp437.c", 0x3Bu, "copy_converted");
++inbuf;
*(_QWORD *)(a3 + 8) = v14 - 1;
}
else if ( v11 != 7 )
{
break;
}
if ( outbuf > buf )
write(fd, buf, outbuf - buf);
}
if ( outbuf > buf )
write(fd, buf, outbuf - buf);
v12 = *(_QWORD *)(a3 + 8);
if ( v12 )
memmove(v3, inbuf, v12);
}
return v9;
}

original_code:
ssize_t copy_converted(int to_fd, int from_fd, struct conv *conv)
{
 ssize_t nbytes;
 char *inptr = conv->buf;
 int retry;
 nbytes = read(from_fd, conv->buf + conv->bytesleft, sizeof conv->buf - conv->bytesleft);
 if (nbytes < 1)
  return nbytes;
 conv->bytesleft += nbytes;
 do {
  char buf[4096];
  char *outptr = buf;
  size_t outbytesleft = sizeof buf;
  retry = 0;
  if (iconv(conv->cd, &inptr, &conv->bytesleft, &outptr, &outbytesleft) == (size_t)-1) {
   if ((*__errno_location ()) == 84) {
    ((void) sizeof ((conv->bytesleft > 0) ? 1 : 0), __extension__ ({ if (conv->bytesleft > 0) ; else __assert_fail ("conv->bytesleft > 0", "cp437.c", 59, __extension__ __PRETTY_FUNCTION__); }));
    inptr++;
    conv->bytesleft--;
    retry = 1;
   } else if ((*__errno_location ()) == 7) {
    retry = 1;
   }
  }
  if (outptr > buf)
   write(to_fd, buf, outptr - buf);
 } while (retry);
 if (conv->bytesleft > 0)
  memmove(conv->buf, inptr, conv->bytesleft);
 return nbytes;
}

canonical_original_code:
long copy_converted(int to_fd, int from_fd, struct conv *conv)
{
 long nbytes;
 char *inptr = conv->buf;
 int retry;
 nbytes = read(from_fd, conv->buf + conv->bytesleft, sizeof conv->buf - conv->bytesleft);
 if (nbytes < 1)
  return nbytes;
 conv->bytesleft += nbytes;
 do {
  char buf[4096];
  char *outptr = buf;
  unsigned long outbytesleft = sizeof buf;
  retry = 0;
  if (iconv(conv->cd, &inptr, &conv->bytesleft, &outptr, &outbytesleft) == (unsigned long)-1) {
   if ((*__errno_location ()) == 84) {
    ((void) sizeof ((conv->bytesleft > 0) ? 1 : 0), __extension__ ({ if (conv->bytesleft > 0) ; else __assert_fail ("conv->bytesleft > 0", "cp437.c", 59, __extension__ __PRETTY_FUNCTION__); }));
    inptr++;
    conv->bytesleft--;
    retry = 1;
   } else if ((*__errno_location ()) == 7) {
    retry = 1;
   }
  }
  if (outptr > buf)
   write(to_fd, buf, outptr - buf);
 } while (retry);
 if (conv->bytesleft > 0)
  memmove(conv->buf, inptr, conv->bytesleft);
 return nbytes;
}

variable_types: struct conv { void *cd; unsigned long bytesleft; char buf[4096]; } *

variable_types:
inptr
char *

variable_types: retry int

variable_types:
buf
char [4096]

variable_types:
outptr
char *

variable_types:
outbytesleft
unsigned long

return_type: long


repo: kebrister/lscat-blcontrols

decompiled_code:
int lsevents_send_event(char *format, ...)
{
__int16 v1; // bx
gcc_va_list arg; // [rsp+8h] [rbp-1D0h] BYREF
char s[256]; // [rsp+20h] [rbp-1B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0xFFuLL, format, arg);
s[255] = 0;
pthread_mutex_lock(&lsevents_queue_mutex);
while ( 1 )
{
v1 = lsevents_queue_on;
if ( (((unsigned __int16)(lsevents_queue_on + 1) ^ (unsigned __int16)lsevents_queue_off) & 0x1FF) != 0 )
break;
pthread_cond_wait(&lsevents_queue_cond, &lsevents_queue_mutex);
}
++lsevents_queue_on;
lsevents_queue[v1 & 0x1FF] = (__int64)strdup(s);
pthread_cond_signal(&lsevents_queue_cond);
return pthread_mutex_unlock(&lsevents_queue_mutex);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
__int16 v1; // bx
gcc_va_list arg; // [rsp+8h] [rbp-1D0h] BYREF
char s[256]; // [rsp+20h] [rbp-1B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0xFFuLL, format, arg);
s[255] = 0;
pthread_mutex_lock(&lsevents_queue_mutex);
while ( 1 )
{
v1 = lsevents_queue_on;
if ( (((unsigned __int16)(lsevents_queue_on + 1) ^ (unsigned __int16)lsevents_queue_off) & 0x1FF) != 0 )
break;
pthread_cond_wait(&lsevents_queue_cond, &lsevents_queue_mutex);
}
++lsevents_queue_on;
lsevents_queue[v1 & 0x1FF] = (__int64)strdup(s);
pthread_cond_signal(&lsevents_queue_cond);
return pthread_mutex_unlock(&lsevents_queue_mutex);
}

original_code:
void lsevents_send_event( char *fmt, ...) {
  char event[256];
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( event, sizeof(event)-1, fmt, arg_ptr);
  event[sizeof(event)-1]=0;
  __builtin_va_end(arg_ptr);
  pthread_mutex_lock( &lsevents_queue_mutex);
  while( (lsevents_queue_on + 1) % 512 == lsevents_queue_off % 512)
    pthread_cond_wait( &lsevents_queue_cond, &lsevents_queue_mutex);
  lsevents_queue[(lsevents_queue_on++) % 512].evp = strdup(event);
  pthread_cond_signal( &lsevents_queue_cond);
  pthread_mutex_unlock( &lsevents_queue_mutex);
}

canonical_original_code:
void lsevents_send_event(char *fmt, ...) {
  char event[256];
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( event, sizeof(event)-1, fmt, arg_ptr);
  event[sizeof(event)-1]=0;
  __builtin_va_end(arg_ptr);
  pthread_mutex_lock( &lsevents_queue_mutex);
  while( (lsevents_queue_on + 1) % 512 == lsevents_queue_off % 512)
    pthread_cond_wait( &lsevents_queue_cond, &lsevents_queue_mutex);
  lsevents_queue[(lsevents_queue_on++) % 512].evp = strdup(event);
  pthread_cond_signal( &lsevents_queue_cond);
  pthread_mutex_unlock( &lsevents_queue_mutex);
}

variable_types:
fmt
char *

variable_types:
event
char [256]


decompiled_code:
void __fastcall __noreturn lsevents_worker(ENTRY a1)
{
int i; // eax
char *v2; // rbp
__int64 j; // rbx

while ( 1 )
{
pthread_mutex_lock(&lsevents_queue_mutex);
for ( i = lsevents_queue_off; lsevents_queue_off == lsevents_queue_on; i = lsevents_queue_off )
{
a1.data = &lsevents_queue_mutex;
pthread_cond_wait(&lsevents_queue_cond, &lsevents_queue_mutex);
}
lsevents_queue_off = i + 1;
v2 = (char *)lsevents_queue[i & 0x1FF];
pthread_cond_signal(&lsevents_queue_cond);
pthread_mutex_unlock(&lsevents_queue_mutex);
pthread_mutex_lock(&lsevents_listener_mutex);
a1.key = v2;
for ( j = lsevents_register_event(a1); j; j = *(_QWORD *)j )
(*(void (__fastcall **)(char *))(j + 8))(v2);
pthread_mutex_unlock(&lsevents_listener_mutex);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func4(ENTRY a1)
{
int i; // eax
char *v2; // rbp
__int64 j; // rbx

while ( 1 )
{
pthread_mutex_lock(&lsevents_queue_mutex);
for ( i = lsevents_queue_off; lsevents_queue_off == lsevents_queue_on; i = lsevents_queue_off )
{
a1.data = &lsevents_queue_mutex;
pthread_cond_wait(&lsevents_queue_cond, &lsevents_queue_mutex);
}
lsevents_queue_off = i + 1;
v2 = (char *)lsevents_queue[i & 0x1FF];
pthread_cond_signal(&lsevents_queue_cond);
pthread_mutex_unlock(&lsevents_queue_mutex);
pthread_mutex_lock(&lsevents_listener_mutex);
a1.key = v2;
for ( j = func3(a1); j; j = *(_QWORD *)j )
(*(void (__fastcall **)(char *))(j + 8))(v2);
pthread_mutex_unlock(&lsevents_listener_mutex);
free(v2);
}
}

original_code:
void *lsevents_worker(
       void *dummy
       ) {
  char *event;
  lsevents_callbacks_t *cbi;
  while( 1) {
    pthread_mutex_lock( &lsevents_queue_mutex);
    while( lsevents_queue_off == lsevents_queue_on)
      pthread_cond_wait( &lsevents_queue_cond, &lsevents_queue_mutex);
    event = lsevents_queue[(lsevents_queue_off++) % 512].evp;
    pthread_cond_signal( &lsevents_queue_cond);
    pthread_mutex_unlock( &lsevents_queue_mutex);
    pthread_mutex_lock( &lsevents_listener_mutex);
    for( cbi = lsevents_register_event( event); cbi != ((void *)0); cbi = cbi->next) {
      cbi->cb( event);
    }
    pthread_mutex_unlock( &lsevents_listener_mutex);
    free( event);
  }
  return ((void *)0);
}

canonical_original_code:
void *lsevents_worker(void *dummy) {
  char *event;
  struct lsevents_callbacks_struct *cbi;
  while( 1) {
    pthread_mutex_lock( &lsevents_queue_mutex);
    while( lsevents_queue_off == lsevents_queue_on)
      pthread_cond_wait( &lsevents_queue_cond, &lsevents_queue_mutex);
    event = lsevents_queue[(lsevents_queue_off++) % 512].evp;
    pthread_cond_signal( &lsevents_queue_cond);
    pthread_mutex_unlock( &lsevents_queue_mutex);
    pthread_mutex_lock( &lsevents_listener_mutex);
    for( cbi = lsevents_register_event( event); cbi != ((void *)0); cbi = cbi->next) {
      cbi->cb( event);
    }
    pthread_mutex_unlock( &lsevents_listener_mutex);
    free( event);
  }
  return ((void *)0);
}

variable_types:
dummy
void *

variable_types:
event
char *

variable_types: struct lsevents_callbacks_struct { struct lsevents_callbacks_struct *next; void (*cb)(char *); } *

return_type: void *


decompiled_code:
__int64 lsevents_preregister_event(char *format, ...)
{
ENTRY v1; // rdi
gcc_va_list arg; // [rsp+8h] [rbp-150h] BYREF
char s[128]; // [rsp+20h] [rbp-138h] BYREF

va_start(arg, format);
v1.data = &loc_7F;
vsnprintf(s, 0x7FuLL, format, arg);
v1.key = s;
s[127] = 0;
return lsevents_register_event(v1);
}

canonical_decompiled_code:
__int64 func5(char *format, ...)
{
ENTRY v1; // rdi
gcc_va_list arg; // [rsp+8h] [rbp-150h] BYREF
char s[128]; // [rsp+20h] [rbp-138h] BYREF

va_start(arg, format);
v1.data = &loc_7F;
vsnprintf(s, 0x7FuLL, format, arg);
v1.key = s;
s[127] = 0;
return func3(v1);
}

original_code:
void lsevents_preregister_event( char *fmt, ...) {
  char s[128];
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( s, sizeof( s) - 1, fmt, arg_ptr);
  s[sizeof(s)-1] = 0;
  __builtin_va_end(arg_ptr);
  lsevents_register_event( s);
}

canonical_original_code:
void lsevents_preregister_event(char *fmt, ...) {
  char s[128];
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( s, sizeof( s) - 1, fmt, arg_ptr);
  s[sizeof(s)-1] = 0;
  __builtin_va_end(arg_ptr);
  lsevents_register_event( s);
}

variable_types:
s
char [128]


decompiled_code:
int lsevents_init()
{
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(&lsevents_queue_mutex, &attr);
pthread_cond_init(&lsevents_queue_cond, 0LL);
pthread_mutex_init(&lsevents_listener_mutex, &attr);
return hcreate_r(2 * lsevents_max_events, &lsevents_event_name_ht);
}

canonical_decompiled_code:
int func6()
{
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(&lsevents_queue_mutex, &attr);
pthread_cond_init(&lsevents_queue_cond, 0LL);
pthread_mutex_init(&lsevents_listener_mutex, &attr);
return hcreate_r(2 * lsevents_max_events, &lsevents_event_name_ht);
}

original_code:
void lsevents_init() {
  pthread_mutexattr_t mutex_initializer;
  pthread_mutexattr_init( &mutex_initializer);
  pthread_mutexattr_settype( &mutex_initializer, PTHREAD_MUTEX_RECURSIVE);
  pthread_mutex_init( &lsevents_queue_mutex, &mutex_initializer);
  pthread_cond_init( &lsevents_queue_cond, ((void *)0));
  pthread_mutex_init( &lsevents_listener_mutex, &mutex_initializer);
  hcreate_r( 2*lsevents_max_events, &lsevents_event_name_ht);
}

canonical_original_code:
void lsevents_init() {
  union pthread_mutexattr_t mutex_initializer;
  pthread_mutexattr_init( &mutex_initializer);
  pthread_mutexattr_settype( &mutex_initializer, PTHREAD_MUTEX_RECURSIVE);
  pthread_mutex_init( &lsevents_queue_mutex, &mutex_initializer);
  pthread_cond_init( &lsevents_queue_cond, ((void *)0));
  pthread_mutex_init( &lsevents_listener_mutex, &mutex_initializer);
  hcreate_r( 2*lsevents_max_events, &lsevents_event_name_ht);
}

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }


decompiled_code:
int lsevents_run()
{
return pthread_create(&lsevents_thread, 0LL, (void *(*)(void *))lsevents_worker, 0LL);
}

canonical_decompiled_code:
int func7()
{
return pthread_create(&lsevents_thread, 0LL, (void *(*)(void *))lsevents_worker, 0LL);
}

original_code:
void lsevents_run() {
  pthread_create( &lsevents_thread, ((void *)0), lsevents_worker, ((void *)0));
}


decompiled_code:
void __fastcall __noreturn lslogging_worker(void *a1)
{
int i; // ebp
__int64 v2; // rbp
__int128 *v3; // r12
const char *v4; // rbp
__int64 v5; // rsi
struct tm v6; // [rsp+0h] [rbp-98h] BYREF
char s[88]; // [rsp+40h] [rbp-58h] BYREF

pthread_mutex_lock(&lslogging_mutex);
while ( 1 )
{
for ( i = lslogging_off; lslogging_on != lslogging_off; i = lslogging_off )
{
lslogging_off = i + 1;
v2 = 129LL * (i & 0x1FFF);
v3 = &lslogging_queue[v2];
v4 = (char *)&unk_350 + v2 * 16;
localtime_r((const time_t *)v3, &v6);
strftime(s, 0x3FuLL, "%Y-%m-%d %H:%M:%S", &v6);
v5 = *((_QWORD *)v3 + 1);
s[63] = 0;
fprintf(lslogging_file, "%s.%.06u %s\n", s, v5 / 1000, v4);
fflush(lslogging_file);
pgpmac_printf(&off_10203F0, v4);
}
pthread_cond_wait(&lslogging_cond, &lslogging_mutex);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
int i; // ebp
__int64 v2; // rbp
__int128 *v3; // r12
const char *v4; // rbp
__int64 v5; // rsi
struct tm v6; // [rsp+0h] [rbp-98h] BYREF
char s[88]; // [rsp+40h] [rbp-58h] BYREF

pthread_mutex_lock(&lslogging_mutex);
while ( 1 )
{
for ( i = lslogging_off; lslogging_on != lslogging_off; i = lslogging_off )
{
lslogging_off = i + 1;
v2 = 129LL * (i & 0x1FFF);
v3 = &lslogging_queue[v2];
v4 = (char *)&unk_350 + v2 * 16;
localtime_r((const time_t *)v3, &v6);
strftime(s, 0x3FuLL, "%Y-%m-%d %H:%M:%S", &v6);
v5 = *((_QWORD *)v3 + 1);
s[63] = 0;
fprintf(lslogging_file, "%s.%.06u %s\n", s, v5 / 1000, v4);
fflush(lslogging_file);
pgpmac_printf(&off_10203F0, v4);
}
pthread_cond_wait(&lslogging_cond, &lslogging_mutex);
}
}

original_code:
void *lslogging_worker(
        void *dummy
        ) {
  struct tm coarsetime;
  char tstr[64];
  unsigned int msecs;
  unsigned int off;
  pthread_mutex_lock( &lslogging_mutex);
  while( 1) {
    while( lslogging_on == lslogging_off) {
      pthread_cond_wait( &lslogging_cond, &lslogging_mutex);
    }
    off = (lslogging_off++) % 8192;
    localtime_r( &(lslogging_queue[off].ltime.tv_sec), &coarsetime);
    strftime( tstr, sizeof(tstr)-1, "%Y-%m-%d %H:%M:%S", &coarsetime);
    tstr[sizeof(tstr)-1] = 0;
    msecs = lslogging_queue[off].ltime.tv_nsec / 1000;
    fprintf( lslogging_file, "%s.%.06u  %s\n", tstr, msecs, lslogging_queue[off].lmsg);
    fflush( lslogging_file);
    pgpmac_printf( "\n%s", lslogging_queue[off].lmsg);
  }
}

canonical_original_code:
void *lslogging_worker(void *dummy) {
  struct tm coarsetime;
  char tstr[64];
  unsigned int msecs;
  unsigned int off;
  pthread_mutex_lock( &lslogging_mutex);
  while( 1) {
    while( lslogging_on == lslogging_off) {
      pthread_cond_wait( &lslogging_cond, &lslogging_mutex);
    }
    off = (lslogging_off++) % 8192;
    localtime_r( &(lslogging_queue[off].ltime.tv_sec), &coarsetime);
    strftime( tstr, sizeof(tstr)-1, "%Y-%m-%d %H:%M:%S", &coarsetime);
    tstr[sizeof(tstr)-1] = 0;
    msecs = lslogging_queue[off].ltime.tv_nsec / 1000;
    fprintf( lslogging_file, "%s.%.06u  %s\n", tstr, msecs, lslogging_queue[off].lmsg);
    fflush( lslogging_file);
    pgpmac_printf( "\n%s", lslogging_queue[off].lmsg);
  }
}

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

variable_types:
tstr
char [64]

variable_types:
msecs
unsigned int

variable_types:
off
unsigned int


decompiled_code:
FILE *lslogging_init()
{
FILE *result; // rax

pthread_mutex_init(&lslogging_mutex, 0LL);
pthread_cond_init(&lslogging_cond, 0LL);
result = fopen((const char *)&off_10203F0 + 6, (const char *)&off_10203F0 + 4);
lslogging_file = result;
return result;
}

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

pthread_mutex_init(&lslogging_mutex, 0LL);
pthread_cond_init(&lslogging_cond, 0LL);
result = fopen((const char *)&off_10203F0 + 6, (const char *)&off_10203F0 + 4);
lslogging_file = result;
return result;
}

original_code:
void lslogging_init() {
  pthread_mutex_init( &lslogging_mutex, ((void *)0));
  pthread_cond_init( &lslogging_cond, ((void *)0));
  lslogging_file = fopen( "/tmp/pgpmac.log", "w");
}


decompiled_code:
int lslogging_log_message(char *format, ...)
{
__int16 v1; // bx
unsigned __int64 v2; // rbx
__m128i v3; // xmm0
struct timespec v5; // [rsp+0h] [rbp-8F8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-8E0h] BYREF
char s[2048]; // [rsp+30h] [rbp-8C8h] BYREF

va_start(arg, format);
clock_gettime(0, &v5);
vsnprintf(s, 0x7FFuLL, format, arg);
s[2047] = 0;
pthread_mutex_lock(&lslogging_mutex);
v1 = lslogging_on++;
v2 = 2064LL * (v1 & 0x1FFF);
strncpy(&byte_350[v2], s, 0x7FFuLL);
v3 = _mm_loadu_si128((const __m128i *)&v5);
byte_B4F[v2] = 0;
lslogging_queue[v2 / 0x10] = (__int128)v3;
pthread_cond_signal(&lslogging_cond);
return pthread_mutex_unlock(&lslogging_mutex);
}

canonical_decompiled_code:
int func2(char *format, ...)
{
__int16 v1; // bx
unsigned __int64 v2; // rbx
__m128i v3; // xmm0
struct timespec v5; // [rsp+0h] [rbp-8F8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-8E0h] BYREF
char s[2048]; // [rsp+30h] [rbp-8C8h] BYREF

va_start(arg, format);
clock_gettime(0, &v5);
vsnprintf(s, 0x7FFuLL, format, arg);
s[2047] = 0;
pthread_mutex_lock(&lslogging_mutex);
v1 = lslogging_on++;
v2 = 2064LL * (v1 & 0x1FFF);
strncpy(&byte_350[v2], s, 0x7FFuLL);
v3 = _mm_loadu_si128((const __m128i *)&v5);
byte_B4F[v2] = 0;
lslogging_queue[v2 / 0x10] = (__int128)v3;
pthread_cond_signal(&lslogging_cond);
return pthread_mutex_unlock(&lslogging_mutex);
}

original_code:
void lslogging_log_message( char *fmt, ...) {
  char msg[2048];
  struct timespec theTime;
  va_list arg_ptr;
  unsigned int on;
  clock_gettime( 0, &theTime);
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( msg, sizeof(msg)-1, fmt, arg_ptr);
  __builtin_va_end(arg_ptr);
  msg[sizeof(msg)-1]=0;
  pthread_mutex_lock( &lslogging_mutex);
  on = (lslogging_on++) % 8192;
  strncpy( lslogging_queue[on].lmsg, msg, 2048 - 1);
  lslogging_queue[on].lmsg[2048 -1] = 0;
  memcpy( &(lslogging_queue[on].ltime), &theTime, sizeof(theTime));
  pthread_cond_signal( &lslogging_cond);
  pthread_mutex_unlock( &lslogging_mutex);
}

canonical_original_code:
void lslogging_log_message(char *fmt, ...) {
  char msg[2048];
  struct timespec theTime;
  __builtin_va_list arg_ptr;
  unsigned int on;
  clock_gettime( 0, &theTime);
  __builtin_va_start(arg_ptr,fmt);
  vsnprintf( msg, sizeof(msg)-1, fmt, arg_ptr);
  __builtin_va_end(arg_ptr);
  msg[sizeof(msg)-1]=0;
  pthread_mutex_lock( &lslogging_mutex);
  on = (lslogging_on++) % 8192;
  strncpy( lslogging_queue[on].lmsg, msg, 2048 - 1);
  lslogging_queue[on].lmsg[2048 -1] = 0;
  memcpy( &(lslogging_queue[on].ltime), &theTime, sizeof(theTime));
  pthread_cond_signal( &lslogging_cond);
  pthread_mutex_unlock( &lslogging_mutex);
}

variable_types:
msg
char [2048]

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

variable_types:
on
unsigned int


decompiled_code:
char __fastcall lslogging_event_cb(char *haystack)
{
bool v1; // zf
char *v2; // rax

v1 = strcmp(haystack, "Timer Update KVs") == 0;
LOBYTE(v2) = !v1;
if ( !v1 )
{
v2 = strstr(haystack, "accepted");
if ( !v2 )
{
v2 = strstr(haystack, "queued");
if ( !v2 )
LOBYTE(v2) = lslogging_log_message("EVENT: %s", haystack);
}
}
return (char)v2;
}

canonical_decompiled_code:
char __fastcall func3(char *haystack)
{
bool v1; // zf
char *v2; // rax

v1 = strcmp(haystack, "Timer Update KVs") == 0;
LOBYTE(v2) = !v1;
if ( !v1 )
{
v2 = strstr(haystack, "accepted");
if ( !v2 )
{
v2 = strstr(haystack, "queued");
if ( !v2 )
LOBYTE(v2) = func2("EVENT: %s", haystack);
}
}
return (char)v2;
}

original_code:
void lslogging_event_cb( char *event) {
  if( strcmp( event, "Timer Update KVs") != 0 && strstr( event, "accepted")==((void *)0) && strstr( event, "queued")==((void *)0)) {
    lslogging_log_message( "EVENT: %s", event);
  }
}

canonical_original_code:
void lslogging_event_cb(char *event) {
  if( strcmp( event, "Timer Update KVs") != 0 && strstr( event, "accepted")==((void *)0) && strstr( event, "queued")==((void *)0)) {
    lslogging_log_message( "EVENT: %s", event);
  }
}


decompiled_code:
__int64 lslogging_run()
{
pthread_create(&lslogging_thread, 0LL, (void *(*)(void *))lslogging_worker, 0LL);
lslogging_log_message("Start up");
return lsevents_add_listener(".+", lslogging_event_cb);
}

canonical_decompiled_code:
__int64 func4()
{
pthread_create(&lslogging_thread, 0LL, (void *(*)(void *))lslogging_worker, 0LL);
func2("Start up");
return lsevents_add_listener(".+", lslogging_event_cb);
}

original_code:
void lslogging_run() {
  pthread_create( &lslogging_thread, ((void *)0), &lslogging_worker, ((void *)0));
  lslogging_log_message( "Start up");
  lsevents_add_listener( ".+", lslogging_event_cb);
}


decompiled_code:
char *__fastcall address_str(__int64 a1, struct in_addr a2)
{
char *v2; // rax
size_t v3; // rax
char *v4; // rbp
size_t v5; // rax
size_t v6; // rax

v2 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
strncpy(buf_2, v2, 0xFFuLL);
v3 = strlen(buf_2);
snprintf(&buf_2[v3], 0xFFuLL, ":%i,", (unsigned __int16)a1);
v4 = inet_ntoa(a2);
v5 = strlen(buf_2);
strncpy(&buf_2[v5], v4, 0x100uLL);
v6 = strlen(buf_2);
snprintf(&buf_2[v6], 0xFFuLL, ":%i", WORD1(a1));
return buf_2;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, struct in_addr a2)
{
char *v2; // rax
size_t v3; // rax
char *v4; // rbp
size_t v5; // rax
size_t v6; // rax

v2 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
strncpy(buf_2, v2, 0xFFuLL);
v3 = strlen(buf_2);
snprintf(&buf_2[v3], 0xFFuLL, ":%i,", (unsigned __int16)a1);
v4 = inet_ntoa(a2);
v5 = strlen(buf_2);
strncpy(&buf_2[v5], v4, 0x100uLL);
v6 = strlen(buf_2);
snprintf(&buf_2[v6], 0xFFuLL, ":%i", WORD1(a1));
return buf_2;
}

original_code:
const char *address_str(struct tuple4 addr)
{
 static char buf[256];
 strncpy(buf, inet_ntoa(*((struct in_addr *)&(addr.saddr))), 255);
 snprintf(buf + strlen(buf), 255, ":%i,", addr.source);
 strncpy(buf + strlen(buf), inet_ntoa(*((struct in_addr *)&(addr.daddr))),256);
 snprintf(buf + strlen(buf), 255, ":%i", addr.dest);
 return buf;
}

variable_types: struct tuple4 { unsigned short source; unsigned short dest; unsigned int saddr; unsigned int daddr; }

variable_types:
buf
char [256]

return_type: char *


hexstr

decompiled_code:
char *__fastcall hexstr(unsigned __int8 *a1, int a2)
{
char *v3; // rax
char *v4; // r13
char *v5; // rbx
char *v6; // r12
__int64 v7; // rdx
char *v8; // rdi

v3 = (char *)calloc((unsigned int)(2 * a2 + 1), 1uLL);
v4 = v3;
if ( v3 && a2 )
{
v5 = v3;
v6 = &v3[2 * a2];
do
{
v7 = *a1;
v8 = v5;
v5 += 2;
++a1;
sprintf(v8, "%02x", v7);
}
while ( v5 != v6 );
}
return v4;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned __int8 *a1, int a2)
{
char *v3; // rax
char *v4; // r13
char *v5; // rbx
char *v6; // r12
__int64 v7; // rdx
char *v8; // rdi

v3 = (char *)calloc((unsigned int)(2 * a2 + 1), 1uLL);
v4 = v3;
if ( v3 && a2 )
{
v5 = v3;
v6 = &v3[2 * a2];
do
{
v7 = *a1;
v8 = v5;
v5 += 2;
++a1;
sprintf(v8, "%02x", v7);
}
while ( v5 != v6 );
}
return v4;
}

original_code:
unsigned char* hexstr (unsigned char *data, unsigned int len)
{
        unsigned char* result = ((void *)0);
        int i = 0;
        result = (char*)calloc( (2*len + 1), sizeof(char));
        if (result == ((void *)0)){
                return result;
        }
        for(i = 0; i < len; i++) {
                sprintf(&result[2*i], "%02x", data[i]);
        }
        return result;
}

canonical_original_code:
unsigned char *hexstr(unsigned char *data, unsigned int len)
{
        unsigned char *result = ((void *)0);
        int i = 0;
        result = (char *)calloc( (2*len + 1), sizeof(char));
        if (result == ((void *)0)){
                return result;
        }
        for(i = 0; i < len; i++) {
                sprintf(&result[2*i], "%02x", data[i]);
        }
        return result;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned int

variable_types:
result
unsigned char *

variable_types: i int

return_type: unsigned char *


original_code:
unsigned char* asprintf_hash(const struct e2k_hash_t* hash )
{
 byte* hash_data = hash->data;
 unsigned char* result = ((void *)0);
 int ret = 0;
 ret = asprintf(&result,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  hash_data[0], hash_data[1], hash_data[2], hash_data[3],
  hash_data[4], hash_data[5], hash_data[6], hash_data[7],
  hash_data[8], hash_data[9], hash_data[10],hash_data[11],
  hash_data[12],hash_data[13],hash_data[14],hash_data[15]);
 if (ret < 0){
  return ((void *)0);
 } else {
  return result;
 }
}

canonical_original_code:
unsigned char *asprintf_hash(struct e2k_hash_t *hash)
{
 unsigned char *hash_data = hash->data;
 unsigned char *result = ((void *)0);
 int ret = 0;
 ret = asprintf(&result,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  hash_data[0], hash_data[1], hash_data[2], hash_data[3],
  hash_data[4], hash_data[5], hash_data[6], hash_data[7],
  hash_data[8], hash_data[9], hash_data[10],hash_data[11],
  hash_data[12],hash_data[13],hash_data[14],hash_data[15]);
 if (ret < 0){
  return ((void *)0);
 } else {
  return result;
 }
}

variable_types: struct e2k_hash_t { unsigned char data[16]; } *

variable_types: ret int


original_code:
const unsigned char* hash2str(const struct e2k_hash_t* hash )
{
 byte* hash_data = hash->data;
 static unsigned char result[33];
 sprintf(result, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  hash_data[0], hash_data[1], hash_data[2], hash_data[3],
  hash_data[4], hash_data[5], hash_data[6], hash_data[7],
  hash_data[8], hash_data[9], hash_data[10],hash_data[11],
  hash_data[12],hash_data[13],hash_data[14],hash_data[15]);
 return result;
}

canonical_original_code:
unsigned char *hash2str(struct e2k_hash_t *hash)
{
 unsigned char *hash_data = hash->data;
 static unsigned char result[33];
 sprintf(result, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  hash_data[0], hash_data[1], hash_data[2], hash_data[3],
  hash_data[4], hash_data[5], hash_data[6], hash_data[7],
  hash_data[8], hash_data[9], hash_data[10],hash_data[11],
  hash_data[12],hash_data[13],hash_data[14],hash_data[15]);
 return result;
}

variable_types:
result
unsigned char [33]

user_defined_types: struct e2k_hash_t { unsigned char data[16]; }


strtimestamp

decompiled_code:
char *strtimestamp()
{
signed int v0; // eax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v0 = (unsigned int)localtime(&timer);
strftime(timestamp_0, 0x18uLL, "%s", (const struct tm *)v0);
return timestamp_0;
}

canonical_decompiled_code:
char *func4()
{
signed int v0; // eax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v0 = (unsigned int)localtime(&timer);
strftime(timestamp_0, 0x18uLL, "%s", (const struct tm *)v0);
return timestamp_0;
}

original_code:
unsigned char* strtimestamp()
{
 static unsigned char timestamp[25];
 time_t now;
 time(&now);
 strftime(timestamp,24,"%s",localtime(&now));
 return timestamp;
}

canonical_original_code:
unsigned char *strtimestamp()
{
 static unsigned char timestamp[25];
 long now;
 time(&now);
 strftime(timestamp,24,"%s",localtime(&now));
 return timestamp;
}

variable_types:
timestamp
unsigned char [25]

variable_types: now long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
unsigned int v4; // ebx
__int64 v5; // rsi
__int64 v7; // [rsp+8h] [rbp-420h] BYREF
char v8[1048]; // [rsp+10h] [rbp-418h] BYREF

memset(v8, 0x33u, 0x400uLL);
if ( (unsigned int)writers_pool_init(&v7, "/tmp/", envp) == -1 )
fatal_error("Could now init an instance of WritersPool");
if ( !writers_pool_writer_get(v7, "THIS_IS_A_FILE_ID") || (v3 = writers_pool_writer_get(v7, "THIS_IS_A_FILE_ID")) == 0 )
fatal_error("could not get a writer instance from the pool");
v4 = 0;
do
{
v5 = v4;
v4 += 1024;
if ( (unsigned int)writers_pool_writer_write(v3, v5, v4, v8) == -1 )
fatal_error("error sending request to writer");
}
while ( v4 != 0x100000 );
if ( (unsigned int)writers_pool_writer_release(v7, "THIS_IS_A_FILE_ID") == -1 )
fatal_error("Error releasing a writer reference");
if ( (unsigned int)writers_pool_destroy(v7) == -1 )
fatal_error("Error destroing WritersPool instance");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
unsigned int v4; // ebx
__int64 v5; // rsi
__int64 v7; // [rsp+8h] [rbp-420h] BYREF
char v8[1048]; // [rsp+10h] [rbp-418h] BYREF

memset(v8, 0x33u, 0x400uLL);
if ( (unsigned int)writers_pool_init(&v7, "/tmp/", envp) == -1 )
func0("Could now init an instance of WritersPool");
if ( !writers_pool_writer_get(v7, "THIS_IS_A_FILE_ID") || (v3 = writers_pool_writer_get(v7, "THIS_IS_A_FILE_ID")) == 0 )
func0("could not get a writer instance from the pool");
v4 = 0;
do
{
v5 = v4;
v4 += 1024;
if ( (unsigned int)writers_pool_writer_write(v3, v5, v4, v8) == -1 )
func0("error sending request to writer");
}
while ( v4 != 0x100000 );
if ( (unsigned int)writers_pool_writer_release(v7, "THIS_IS_A_FILE_ID") == -1 )
func0("Error releasing a writer reference");
if ( (unsigned int)writers_pool_destroy(v7) == -1 )
func0("Error destroing WritersPool instance");
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 signal(2,sigint_handler);
 n_active_connections = n_seen_connections = 0;
 n_a_connections = n_a_closed = n_a_reset = n_a_timedout = 0;
 is_sniffing = 0;
 next_syslog_report_time = time(((void *)0));
 pcap_descriptor = ((void *)0);
 openlog("e2ksniff", 0, (16<<3));
 if( writers_pool_init(&w_pool, "/tmp/cache/") !=
   WRITERS_POOL_OK)
 {
  syslog( nids_params.syslog_level,
    " == ERROR creating WritersPool");
  fprintf(stdout, " == ERROR creating WritersPool");
  fprintf(stderr, " == ERROR creating WritersPool");
 }
 nids_params.device = "any";
 nids_params.one_loop_less = 0;
 nids_params.scan_num_hosts = 0;
 nids_params.scan_num_ports = 0;
 nids_params.no_mem = out_of_memory_callback;
 nids_params.n_hosts=1024*4;
 nids_params.n_tcp_streams = 1024*64;
 nids_params.sk_buff_size = 1600;
 if (argc > 1){
  nids_params.device = ((void *)0);
  nids_params.filename = argv[1];
  fprintf(stdout," == Loading trace file: %s\n",nids_params.filename );
  is_sniffing = 0;
 } else {
  fprintf(stdout," == No tracefile given. Sniffing from the network.\n");
  is_sniffing = 1;
 }
 if (!nids_init()) {
  fprintf(stderr, " == ERROR: libNIDS error: %s\n", nids_errbuf);
  exit(1);
 }
 if( (is_sniffing) &&
     (drop_privilages("nobody") != 0) ){
  fprintf( stderr, " == ERROR: Could not drop privileges: %s\n",
   e2ksniff_errbuf);
  exit(1);
 } else {
  fprintf( stdout,
    " == Droped privilages. Impersonating '%s'\n",
    "nobody");
 }
 nids_register_tcp(tcp_callback);
 if (is_sniffing){
  fprintf( stdout,
    " == Starting STDOUT redirection and log rotation\n");
  if( rotate_logfile(15*60,10*1024*1024) < 0){
   syslog( nids_params.syslog_level,
    " == ERROR: Could not logrotate");
   fprintf( stderr, " == ERROR: Could not logrotate: %s\n",
     e2ksniff_errbuf);
   exit(1);
  }
 } else {
  fprintf( stdout,
    " == Not sniffing: logging on STDOUT\n");
 }
 syslog( nids_params.syslog_level," == Sniffing started.");
 fprintf(stdout," == Sniffing started.\n");
 nids_run();
 syslog( nids_params.syslog_level," == Sniffing stopped - ERROR?");
 fprintf(stdout," == Sniffing stopped - ERROR?\n");
 fprintf(stderr," == Sniffing stopped - ERROR?\n");
 if ( writers_pool_destroy(w_pool) != WRITERS_POOL_OK ) {
  syslog( nids_params.syslog_level,
    " == ERROR destroying WritersPool");
  fprintf(stdout, " == ERROR destroying WritersPool");
  fprintf(stderr, " == ERROR destroying WritersPool");
 }
 log_statistics();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 signal(2,sigint_handler);
 n_active_connections = n_seen_connections = 0;
 n_a_connections = n_a_closed = n_a_reset = n_a_timedout = 0;
 is_sniffing = 0;
 next_syslog_report_time = time(((void *)0));
 pcap_descriptor = ((void *)0);
 openlog("e2ksniff", 0, (16<<3));
 if( writers_pool_init(&w_pool, "/tmp/cache/") !=
   WRITERS_POOL_OK)
 {
  syslog( nids_params.syslog_level,
    " == ERROR creating WritersPool");
  fprintf(stdout, " == ERROR creating WritersPool");
  fprintf(stderr, " == ERROR creating WritersPool");
 }
 nids_params.device = "any";
 nids_params.one_loop_less = 0;
 nids_params.scan_num_hosts = 0;
 nids_params.scan_num_ports = 0;
 nids_params.no_mem = out_of_memory_callback;
 nids_params.n_hosts=1024*4;
 nids_params.n_tcp_streams = 1024*64;
 nids_params.sk_buff_size = 1600;
 if (argc > 1){
  nids_params.device = ((void *)0);
  nids_params.filename = argv[1];
  fprintf(stdout," == Loading trace file: %s\n",nids_params.filename );
  is_sniffing = 0;
 } else {
  fprintf(stdout," == No tracefile given. Sniffing from the network.\n");
  is_sniffing = 1;
 }
 if (!nids_init()) {
  fprintf(stderr, " == ERROR: libNIDS error: %s\n", nids_errbuf);
  exit(1);
 }
 if( (is_sniffing) &&
     (drop_privilages("nobody") != 0) ){
  fprintf( stderr, " == ERROR: Could not drop privileges: %s\n",
   e2ksniff_errbuf);
  exit(1);
 } else {
  fprintf( stdout,
    " == Droped privilages. Impersonating '%s'\n",
    "nobody");
 }
 nids_register_tcp(tcp_callback);
 if (is_sniffing){
  fprintf( stdout,
    " == Starting STDOUT redirection and log rotation\n");
  if( rotate_logfile(15*60,10*1024*1024) < 0){
   syslog( nids_params.syslog_level,
    " == ERROR: Could not logrotate");
   fprintf( stderr, " == ERROR: Could not logrotate: %s\n",
     e2ksniff_errbuf);
   exit(1);
  }
 } else {
  fprintf( stdout,
    " == Not sniffing: logging on STDOUT\n");
 }
 syslog( nids_params.syslog_level," == Sniffing started.");
 fprintf(stdout," == Sniffing started.\n");
 nids_run();
 syslog( nids_params.syslog_level," == Sniffing stopped - ERROR?");
 fprintf(stdout," == Sniffing stopped - ERROR?\n");
 fprintf(stderr," == Sniffing stopped - ERROR?\n");
 if ( writers_pool_destroy(w_pool) != WRITERS_POOL_OK ) {
  syslog( nids_params.syslog_level,
    " == ERROR destroying WritersPool");
  fprintf(stdout, " == ERROR destroying WritersPool");
  fprintf(stderr, " == ERROR destroying WritersPool");
 }
 log_statistics();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


decompiled_code:
__int64 __fastcall get_next_packet_offset(int a1, int a2)
{
return (unsigned int)(a1 + a2 + 5);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
return (unsigned int)(a1 + a2 + 5);
}

original_code:
int get_next_packet_offset(int current_offset, int current_packet_len)
{
 return current_offset + 6 -1 + current_packet_len;
}


goToSleep

decompiled_code:
void __fastcall __noreturn goToSleep(void *a1)
{
puts("This thread is going to sleep.");
usleep((__useconds_t)a1);
puts("This thread just awakened.");
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
puts("This thread is going to sleep.");
usleep((__useconds_t)a1);
puts("This thread just awakened.");
pthread_exit(0LL);
}

original_code:
void *goToSleep(void *length)
{
    printf("This thread is going to sleep.\n");
    usleep((int)length);
    printf("This thread just awakened.\n");
    pthread_exit(((void *)0));
}

variable_types:
length
void *


SendMsg

decompiled_code:
__int64 __fastcall SendMsg(unsigned int a1, __int64 a2, unsigned int a3, unsigned __int8 a4)
{
return syscall(349LL, a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, __int64 a2, unsigned int a3, unsigned __int8 a4)
{
return syscall(349LL, a1, a2, a3, a4);
}

original_code:
int SendMsg(pid_t dest, void *msg, int len, _Bool block)
{
    return syscall(349, dest, msg, len, block);
}

canonical_original_code:
int SendMsg(int dest, void *msg, int len, _Bool block)
{
    return syscall(349, dest, msg, len, block);
}

variable_types: dest int

variable_types:
msg
void *

variable_types: len int


RcvMsg

decompiled_code:
__int64 __fastcall RcvMsg(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return syscall(350LL, a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return syscall(350LL, a1, a2, a3, a4);
}

original_code:
int RcvMsg(pid_t *sender, void *msg, int *len, _Bool block)
{
    return syscall(350, sender, msg, len, block);
}

canonical_original_code:
int RcvMsg(int *sender, void *msg, int *len, _Bool block)
{
    return syscall(350, sender, msg, len, block);
}

variable_types:
sender
int *

variable_types:
len
int *


ManageMailbox

decompiled_code:
__int64 __fastcall ManageMailbox(unsigned __int8 a1, __int64 a2)
{
return syscall(351LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, __int64 a2)
{
return syscall(351LL, a1, a2);
}

original_code:
int ManageMailbox(_Bool stop, int *count)
{
    return syscall(351, stop, count);
}

variable_types:
count
int *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
int v4; // ebx
unsigned int v5; // eax
unsigned int v7; // [rsp+8h] [rbp-430h] BYREF
char v8[4]; // [rsp+Ch] [rbp-42Ch] BYREF
__m128i v9; // [rsp+10h] [rbp-428h] BYREF
pthread_t newthread; // [rsp+20h] [rbp-418h] BYREF

for ( i = 1000000LL; i != 6000000; i += 1000000LL )
pthread_create(&newthread, 0LL, goToSleep, (void *)i);
v4 = 50;
while ( 1 )
{
v5 = fork();
if ( !v5 )
break;
strcpy((char *)&newthread, "I am your father");
if ( (unsigned int)SendMsg(v5, &newthread, 17LL, 0LL) )
{
puts("Send failed");
if ( !--v4 )
{
LABEL_8:
usleep(0x186A0u);
pthread_exit(0LL);
}
}
else if ( !--v4 )
{
goto LABEL_8;
}
}
RcvMsg(&v7, &newthread, v8, 1LL);
printf("Message: %s\n", (const char *)&newthread);
v9 = _mm_load_si128((const __m128i *)&xmmword_402070);
if ( (unsigned int)SendMsg(v7, &v9, 16LL, 1LL) )
puts("Child send failed.");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
int v4; // ebx
unsigned int v5; // eax
unsigned int v7; // [rsp+8h] [rbp-430h] BYREF
char v8[4]; // [rsp+Ch] [rbp-42Ch] BYREF
__m128i v9; // [rsp+10h] [rbp-428h] BYREF
pthread_t newthread; // [rsp+20h] [rbp-418h] BYREF

for ( i = 1000000LL; i != 6000000; i += 1000000LL )
pthread_create(&newthread, 0LL, goToSleep, (void *)i);
v4 = 50;
while ( 1 )
{
v5 = fork();
if ( !v5 )
break;
strcpy((char *)&newthread, "I am your father");
if ( (unsigned int)func8(v5, &newthread, 17LL, 0LL) )
{
puts("Send failed");
if ( !--v4 )
{
LABEL_8:
usleep(0x186A0u);
pthread_exit(0LL);
}
}
else if ( !--v4 )
{
goto LABEL_8;
}
}
func9(&v7, &newthread, v8, 1LL);
printf("Message: %s\n", (const char *)&newthread);
v9 = _mm_load_si128((const __m128i *)&xmmword_402070);
if ( (unsigned int)func8(v7, &v9, 16LL, 1LL) )
puts("Child send failed.");
return 0;
}

original_code:
int main()
{
    int childCounter;
    int threadCounter;
    for (threadCounter = 0; threadCounter < 5; threadCounter++)
    {
        pthread_t newThread;
        int sleepFor = (threadCounter + 1) * 1000000;
        pthread_create(&newThread, ((void *)0), goToSleep, (void *) sleepFor);
    }
    for(childCounter = 0; childCounter < 50; childCounter++)
    {
        int childPID = fork();
        if(childPID == 0)
        {
            pid_t sender;
            void *msg[128];
            int len;
            _Bool block = 1;
            RcvMsg(&sender,msg,&len,block);
            printf("Message: %s\n", (char *)msg);
            char myMesg[] = "I am your child";
            if(SendMsg(sender, myMesg, 16, block))
            {
                printf("Child send failed.\n");
            }
            return 0;
        }
        else
        {
            char mesg[] = "I am your father";
            if (SendMsg(childPID, mesg, 17, 0))
            {
                printf("Send failed\n");
            }
        }
    }
    usleep(100000);
    pthread_exit(((void *)0));
}

canonical_original_code:
int main()
{
    int childCounter;
    int threadCounter;
    for (threadCounter = 0; threadCounter < 5; threadCounter++)
    {
        unsigned long newThread;
        int sleepFor = (threadCounter + 1) * 1000000;
        pthread_create(&newThread, ((void *)0), goToSleep, (void *) sleepFor);
    }
    for(childCounter = 0; childCounter < 50; childCounter++)
    {
        int childPID = fork();
        if(childPID == 0)
        {
            int sender;
            void *msg[128];
            int len;
            _Bool block = 1;
            RcvMsg(&sender,msg,&len,block);
            printf("Message: %s\n", (char *)msg);
            char myMesg[] = "I am your child";
            if(SendMsg(sender, myMesg, 16, block))
            {
                printf("Child send failed.\n");
            }
            return 0;
        }
        else
        {
            char mesg[] = "I am your father";
            if (SendMsg(childPID, mesg, 17, 0))
            {
                printf("Send failed\n");
            }
        }
    }
    usleep(100000);
    pthread_exit(((void *)0));
}

variable_types: childCounter int

variable_types: threadCounter int

variable_types:
newThread
unsigned long

variable_types: sleepFor int

variable_types: childPID int

variable_types: sender int

variable_types:
msg
void *[128]

variable_types:
myMesg
char []

variable_types:
mesg
char []


decompiled_code:
int __fastcall print_info(__int64 a1)
{
putchar(10);
puts(&s);
printf(&byte_4020A8, *(_QWORD *)(a1 + 8));
printf(&byte_4020C6, *(unsigned int *)(a1 + 16));
puts(&byte_4020E4);
printf(&byte_4020FE, *(unsigned int *)(a1 + 20));
printf(&byte_40211B, *(unsigned int *)(a1 + 24));
printf(&byte_402138, *(unsigned int *)(a1 + 28));
printf(&byte_402155, *(unsigned int *)(a1 + 32));
puts(&byte_402058);
return printf("\n~~$ ");
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
putchar(10);
puts(&s);
printf(&byte_4020A8, *(_QWORD *)(a1 + 8));
printf(&byte_4020C6, *(unsigned int *)(a1 + 16));
puts(&byte_4020E4);
printf(&byte_4020FE, *(unsigned int *)(a1 + 20));
printf(&byte_40211B, *(unsigned int *)(a1 + 24));
printf(&byte_402138, *(unsigned int *)(a1 + 28));
printf(&byte_402155, *(unsigned int *)(a1 + 32));
puts(&byte_402058);
return printf("\n~~$ ");
}

original_code:
void print_info(proc_info* p)
{
 printf("\n");
 printf("╔═══════════════════════╗\n");
 printf("║clock time (µs):  %i\t║\n", p->real_time);
 printf("║cpu time (µs):    %i\t║\n", p->cpu_time);
 printf("║context switches: \t║\n");
 printf("║    voluntary:    %i\t║\n", p->count_preempted);
 printf("║    involntary:   %i\t║\n", p->count_cpu_yeild);
 printf("║page faults:      %i\t║\n", p->count_page_fault);
 printf("║page reclaims:    %i\t║\n", p->count_pf_kcache);
 printf("╚═══════════════════════╝\n");
 printf("\n~~$ ");
}

canonical_original_code:
void print_info(struct proc_info *p)
{
 printf("\n");
 printf("╔═══════════════════════╗\n");
 printf("║clock time (µs):  %i\t║\n", p->real_time);
 printf("║cpu time (µs):    %i\t║\n", p->cpu_time);
 printf("║context switches: \t║\n");
 printf("║    voluntary:    %i\t║\n", p->count_preempted);
 printf("║    involntary:   %i\t║\n", p->count_cpu_yeild);
 printf("║page faults:      %i\t║\n", p->count_page_fault);
 printf("║page reclaims:    %i\t║\n", p->count_pf_kcache);
 printf("╚═══════════════════════╝\n");
 printf("\n~~$ ");
}

variable_types: struct proc_info { int err_code; long long real_time; int cpu_time; int count_preempted; int count_cpu_yeild; int count_page_fault; int count_pf_kcache; } *


decompiled_code:
unsigned __int64 __fastcall listen_all(void *a1)
{
unsigned __int64 result; // rax

result = (unsigned int)exeunt;
if ( !*(_QWORD *)a1 && !exeunt )
{
while ( 1 )
;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(void *a1)
{
unsigned __int64 result; // rax

result = (unsigned int)exeunt;
if ( !*(_QWORD *)a1 && !exeunt )
{
while ( 1 )
;
}
return result;
}

original_code:
void* listen_all(void* arg)
{
    proc_list** running = (proc_list**)(arg);
    while(!*running && exeunt == 0)
    {
        while(*running)
        {
            *running = prune(*running);
        }
    }
}

canonical_original_code:
void *listen_all(void *arg)
{
    struct proc_list **running = (proc_list**)(arg);
    while(!*running && exeunt == 0)
    {
        while(*running)
        {
            *running = prune(*running);
        }
    }
}

variable_types:
arg
void *

variable_types: struct proc_list { struct proc_list *next; struct proc_info { int err_code; long long real_time; int cpu_time; int count_preempted; int count_cpu_yeild; int count_page_fault; int count_pf_kcache; } *start_info; int pid; int my_id; } **

user_defined_types: struct proc_list { struct proc_list *next; struct proc_info *start_info; int pid; int my_id; }


decompiled_code:
int __fastcall change_dir(const char *a1)
{
char *v1; // rax
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

if ( !a1 )
a1 = "/";
chdir(a1);
v1 = getcwd(v3, 0x400uLL);
if ( v1 )
LODWORD(v1) = puts(v3);
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
char *v1; // rax
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

if ( !a1 )
a1 = "/";
chdir(a1);
v1 = getcwd(v3, 0x400uLL);
if ( v1 )
LODWORD(v1) = puts(v3);
return (int)v1;
}

original_code:
int change_dir(char* dirname)
{
    char dir[1024];
    if (dirname)
    {
        chdir(dirname);
    }
    else
    {
        chdir("/");
    }
    if (getcwd(dir, sizeof(dir)) != ((void *)0))
    {
        printf("%s\n", dir);
    }
}

canonical_original_code:
int change_dir(char *dirname)
{
    char dir[1024];
    if (dirname)
    {
        chdir(dirname);
    }
    else
    {
        chdir("/");
    }
    if (getcwd(dir, sizeof(dir)) != ((void *)0))
    {
        printf("%s\n", dir);
    }
}

variable_types:
dirname
char *

variable_types:
dir
char [1024]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BOOL4 v3; // er12
int v4; // ebx
char *v5; // rbp
int v6; // edx
__int16 v7; // cx
char v8; // al
bool v9; // cf
bool v10; // zf
const char **v11; // rax
const char *v12; // rsi
__int64 v13; // rcx
const char *v14; // rdx
const char **v15; // r14
const char *v16; // rdi
int result; // eax
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // [rsp+0h] [rbp-38h] BYREF
pthread_t newthread[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = 0;
v4 = 0;
printf("~~$ ");
v5 = (char *)malloc(0x400uLL);
v20 = 0LL;
pthread_create(newthread, 0LL, (void *(*)(void *))listen_all, &v20);
if ( !exeunt )
{
do
{
v8 = getchar();
if ( v8 == 10 )
{
if ( v4 )
{
v11 = (const char **)split_args(v5);
v12 = "exit";
v13 = 5LL;
v14 = *v11;
v15 = v11;
v16 = *v11;
do
{
if ( !v13 )
break;
v9 = *v12 < (unsigned int)*v16;
v10 = *v12++ == *v16++;
--v13;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
exeunt = 1;
memset(v5, 0, 0x80uLL);
break;
}
if ( *v14 == 99 && v14[1] == 100 && !v14[2] )
{
change_dir(v11[1]);
v6 = exeunt;
}
else
{
if ( v3 )
{
v20 = exec(v11, v20);
printf("\n~~$ ");
}
else
{
v18 = get_init(v16, v12);
v19 = exec_forg(v15, v18);
print_info(v19);
}
v6 = exeunt;
}
v4 = 0;
memset(v5, 0, 0x80uLL);
}
else
{
printf("~~$ ");
v6 = exeunt;
}
}
else if ( v8 == -1 )
{
exeunt = 1;
putchar(10);
v6 = exeunt;
}
else
{
v6 = exeunt;
if ( v8 != 8 )
{
v3 = v8 == 38;
if ( v8 != 38 )
{
v7 = v4++;
v5[v7 & 0x3FF] = v8;
}
}
}
}
while ( !v6 );
}
printf("waiting for termination from all sub-processess");
result = v20;
while ( v20 )
;
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BOOL4 v3; // er12
int v4; // ebx
char *v5; // rbp
int v6; // edx
__int16 v7; // cx
char v8; // al
bool v9; // cf
bool v10; // zf
const char **v11; // rax
const char *v12; // rsi
__int64 v13; // rcx
const char *v14; // rdx
const char **v15; // r14
const char *v16; // rdi
int result; // eax
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // [rsp+0h] [rbp-38h] BYREF
pthread_t newthread[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = 0;
v4 = 0;
printf("~~$ ");
v5 = (char *)malloc(0x400uLL);
v20 = 0LL;
pthread_create(newthread, 0LL, (void *(*)(void *))listen_all, &v20);
if ( !exeunt )
{
do
{
v8 = getchar();
if ( v8 == 10 )
{
if ( v4 )
{
v11 = (const char **)split_args(v5);
v12 = "exit";
v13 = 5LL;
v14 = *v11;
v15 = v11;
v16 = *v11;
do
{
if ( !v13 )
break;
v9 = *v12 < (unsigned int)*v16;
v10 = *v12++ == *v16++;
--v13;
}
while ( v10 );
if ( (!v9 && !v10) == v9 )
{
exeunt = 1;
memset(v5, 0, 0x80uLL);
break;
}
if ( *v14 == 99 && v14[1] == 100 && !v14[2] )
{
func1(v11[1]);
v6 = exeunt;
}
else
{
if ( v3 )
{
v20 = exec(v11, v20);
printf("\n~~$ ");
}
else
{
v18 = get_init(v16, v12);
v19 = exec_forg(v15, v18);
print_info(v19);
}
v6 = exeunt;
}
v4 = 0;
memset(v5, 0, 0x80uLL);
}
else
{
printf("~~$ ");
v6 = exeunt;
}
}
else if ( v8 == -1 )
{
exeunt = 1;
putchar(10);
v6 = exeunt;
}
else
{
v6 = exeunt;
if ( v8 != 8 )
{
v3 = v8 == 38;
if ( v8 != 38 )
{
v7 = v4++;
v5[v7 & 0x3FF] = v8;
}
}
}
}
while ( !v6 );
}
printf("waiting for termination from all sub-processess");
result = v20;
while ( v20 )
;
return result;
}

original_code:
int main(int argc, char*argv[])
{
    printf("~~$ ");
    char* buffer = (char*)malloc(1024);
    int bufferspace = 0;
    proc_list* ptr = 0;
    proc_list** running = &ptr;
    *running = 0;
    int background = 0;
    pthread_t thread;
    pthread_create(&thread, ((void *)0), listen_all, running);
    while(exeunt == 0)
    {
        char c = getchar();
        if (c == '\n')
        {
            if (bufferspace != 0)
            {
                char** args = split_args(buffer);
                if (strcmp("exit", args[0]) == 0)
                {
                    exeunt = 1;
                }
                else if (strcmp("cd", args[0]) == 0)
                {
                    change_dir(args[1]);
                }
                else
                {
                    if (background)
                    {
                        *running = exec(args, running[0]);
                        printf("\n~~$ ");
                    }
                    else
                    {
                        proc_info* start = get_init();
                        print_info(exec_forg(args, start));
                    }
                }
                memset(buffer, 0, 128);
                bufferspace = 0;
            }
            else
            {
                printf("~~$ ");
            }
        }
        else if (c == (-1))
        {
            exeunt = 1;
            printf("\n");
        }
        else if (c != '\b')
        {
            background = (c=='&');
            if (!background)
            {
                buffer[(bufferspace++)%1024] = c;
            }
        }
    }
    printf("waiting for termination from all sub-processess");
    while(*running);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    printf("~~$ ");
    char *buffer = (char*)malloc(1024);
    int bufferspace = 0;
    struct proc_list *ptr = 0;
    struct proc_list **running = &ptr;
    *running = 0;
    int background = 0;
    unsigned long thread;
    pthread_create(&thread, ((void *)0), listen_all, running);
    while(exeunt == 0)
    {
        char c = getchar();
        if (c == '\n')
        {
            if (bufferspace != 0)
            {
                char **args = split_args(buffer);
                if (strcmp("exit", args[0]) == 0)
                {
                    exeunt = 1;
                }
                else if (strcmp("cd", args[0]) == 0)
                {
                    change_dir(args[1]);
                }
                else
                {
                    if (background)
                    {
                        *running = exec(args, running[0]);
                        printf("\n~~$ ");
                    }
                    else
                    {
                        struct proc_info *start = get_init();
                        print_info(exec_forg(args, start));
                    }
                }
                memset(buffer, 0, 128);
                bufferspace = 0;
            }
            else
            {
                printf("~~$ ");
            }
        }
        else if (c == (-1))
        {
            exeunt = 1;
            printf("\n");
        }
        else if (c != '\b')
        {
            background = (c=='&');
            if (!background)
            {
                buffer[(bufferspace++)%1024] = c;
            }
        }
    }
    printf("waiting for termination from all sub-processess");
    while(*running);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
buffer
char *

variable_types: bufferspace int

variable_types: struct proc_list { struct proc_list *next; struct proc_info { int err_code; long long real_time; int cpu_time; int count_preempted; int count_cpu_yeild; int count_page_fault; int count_pf_kcache; } *start_info; int pid; int my_id; } *

variable_types: background int

variable_types:
thread
unsigned long

variable_types: c char

variable_types:
args
char **


initialize

decompiled_code:
int initialize()
{
clock_t v0; // rax

sim_last = clock();
v0 = clock();
avg = 0LL;
sim_start = v0;
pthread_mutex_init(&doorsign, 0LL);
return puts("initialized");
}

canonical_decompiled_code:
int func7()
{
clock_t v0; // rax

sim_last = clock();
v0 = clock();
avg = 0LL;
sim_start = v0;
pthread_mutex_init(&doorsign, 0LL);
return puts("initialized");
}

original_code:
void initialize()
{
 sim_last = clock();
 sim_start = clock();
 avg = 0;
 pthread_mutex_init(&doorsign, ((void *)0));
 printf("initialized\n");
}


finalize

decompiled_code:
int finalize()
{
clock_t v0; // rax

v0 = clock();
return printf(
"at maximum, there were:\n"
"\tmen: %i\n"
"\twomen: %i\n"
"and a total or %i men and %i women entered the bathroom\n"
"There were %llu people on average waiting for the bathroom\n",
(unsigned int)highest_men,
(unsigned int)highest_women,
(unsigned int)total_men,
(unsigned int)total_women,
avg / (unsigned __int64)(v0 - sim_start));
}

canonical_decompiled_code:
int func8()
{
clock_t v0; // rax

v0 = clock();
return printf(
"at maximum, there were:\n"
"\tmen: %i\n"
"\twomen: %i\n"
"and a total or %i men and %i women entered the bathroom\n"
"There were %llu people on average waiting for the bathroom\n",
(unsigned int)highest_men,
(unsigned int)highest_women,
(unsigned int)total_men,
(unsigned int)total_women,
avg / (unsigned __int64)(v0 - sim_start));
}

original_code:
void finalize()
{
 unsigned long long total = clock()-sim_start;
 printf("at maximum, there were:\n\tmen:   %i\n\twomen: %i\nand a total or %i men and %i women entered the bathroom\nThere were %llu people on average waiting for the bathroom\n",
     highest_men, highest_women, total_men, total_women, avg/total);
}

variable_types:
total
unsigned long long


stats

decompiled_code:
clock_t __fastcall stats(int a1)
{
clock_t result; // rax
clock_t v2; // rdx

if ( a1 == 1 )
{
++total_women;
if ( occupancy > highest_women )
highest_women = occupancy;
}
else if ( !a1 )
{
++total_men;
if ( occupancy > highest_men )
highest_men = occupancy;
}
result = clock();
v2 = result - sim_last;
sim_last = result;
avg += occupancy * v2;
return result;
}

canonical_decompiled_code:
clock_t __fastcall func9(int a1)
{
clock_t result; // rax
clock_t v2; // rdx

if ( a1 == 1 )
{
++total_women;
if ( occupancy > highest_women )
highest_women = occupancy;
}
else if ( !a1 )
{
++total_men;
if ( occupancy > highest_men )
highest_men = occupancy;
}
result = clock();
v2 = result - sim_last;
sim_last = result;
avg += occupancy * v2;
return result;
}

original_code:
void stats(gender g)
{
 if (g == FEMALE)
 {
  total_women ++;
  if (occupancy > highest_women)
  {
   highest_women = occupancy;
  }
 }
 if (g == MALE)
 {
  total_men ++;
  if (occupancy > highest_men)
  {
   highest_men = occupancy;
  }
 }
 clock_t sim_new = clock();
 avg += occupancy * (sim_new - sim_last);
 sim_last = sim_new;
}

canonical_original_code:
void stats(enum gender g)
{
 if (g == FEMALE)
 {
  total_women ++;
  if (occupancy > highest_women)
  {
   highest_women = occupancy;
  }
 }
 if (g == MALE)
 {
  total_men ++;
  if (occupancy > highest_men)
  {
   highest_men = occupancy;
  }
 }
 long sim_new = clock();
 avg += occupancy * (sim_new - sim_last);
 sim_last = sim_new;
}

variable_types:
g
enum gender { MALE=0, FEMALE=1 }


enter

decompiled_code:
int __fastcall enter(int a1)
{
int v1; // eax

while ( 1 )
{
pthread_mutex_lock(&doorsign);
if ( !occupancy )
{
v1 = 1;
goto LABEL_6;
}
if ( occupant == a1 )
break;
pthread_mutex_unlock(&doorsign);
}
v1 = occupancy + 1;
LABEL_6:
occupant = a1;
occupancy = v1;
stats(a1);
return pthread_mutex_unlock(&doorsign);
}

canonical_decompiled_code:
int __fastcall func10(int a1)
{
int v1; // eax

while ( 1 )
{
pthread_mutex_lock(&doorsign);
if ( !occupancy )
{
v1 = 1;
goto LABEL_6;
}
if ( occupant == a1 )
break;
pthread_mutex_unlock(&doorsign);
}
v1 = occupancy + 1;
LABEL_6:
occupant = a1;
occupancy = v1;
func9(a1);
return pthread_mutex_unlock(&doorsign);
}

original_code:
void enter(gender g)
{
 while(1)
 {
  pthread_mutex_lock(&doorsign);
  if (occupancy == 0 || occupant == g)
  {
   occupancy++;
   occupant = g;
   stats(g);
   pthread_mutex_unlock(&doorsign);
   return;
  }
  pthread_mutex_unlock(&doorsign);
 }
}

canonical_original_code:
void enter(enum gender g)
{
 while(1)
 {
  pthread_mutex_lock(&doorsign);
  if (occupancy == 0 || occupant == g)
  {
   occupancy++;
   occupant = g;
   stats(g);
   pthread_mutex_unlock(&doorsign);
   return;
  }
  pthread_mutex_unlock(&doorsign);
 }
}


leave

decompiled_code:
int leave()
{
int v0; // eax

pthread_mutex_lock(&doorsign);
v0 = occupancy - 1;
if ( occupancy - 1 <= 0 )
v0 = 0;
occupancy = v0;
stats(-2);
return pthread_mutex_unlock(&doorsign);
}

canonical_decompiled_code:
int func11()
{
int v0; // eax

pthread_mutex_lock(&doorsign);
v0 = occupancy - 1;
if ( occupancy - 1 <= 0 )
v0 = 0;
occupancy = v0;
func9(-2);
return pthread_mutex_unlock(&doorsign);
}

original_code:
void leave()
{
 pthread_mutex_lock(&doorsign);
 occupancy --;
 if (occupancy <= 0)
 {
  occupancy = 0;
 }
 stats(-2);
 pthread_mutex_unlock(&doorsign);
}


original_code:
int binary_dist(double mean, double stddev)
{
 double n2 = 0.0;
 int n2_cached = 0;
 if (!n2_cached)
 {
  double x, y, r;
  do
  {
   x = 2.0*rand()/2147483647 - 1;
   y = 2.0*rand()/2147483647 - 1;
   r = x*x + y*y;
  } while (r == 0.0 || r > 1.0);
  double d = sqrt(-2.0*log(r)/r);
  double n1 = x*d;
  n2 = y*d;
  double result = n1*stddev + mean;
  n2_cached = 1;
  return result;
 }
 else
 {
  n2_cached = 0;
  return n2*stddev + mean;
 }
}

canonical_original_code:
int binary_dist(double mean, double stddev)
{
 double n2 = 0.0;
 int n2_cached = 0;
 if (!n2_cached)
 {
  double x; double y; double r;
  do
  {
   x = 2.0*rand()/2147483647 - 1;
   y = 2.0*rand()/2147483647 - 1;
   r = x*x + y*y;
  } while (r == 0.0 || r > 1.0);
  double d = sqrt(-2.0*log(r)/r);
  double n1 = x*d;
  n2 = y*d;
  double result = n1*stddev + mean;
  n2_cached = 1;
  return result;
 }
 else
 {
  n2_cached = 0;
  return n2*stddev + mean;
 }
}

variable_types: mean double

variable_types: stddev double

variable_types: x double

variable_types: y double

variable_types: r double

variable_types: d double

variable_types: result double


individual

decompiled_code:
void *__fastcall individual(void *a1)
{
unsigned int v2; // eax
int v3; // eax
int v4; // er13
int v5; // er14
__int64 v6; // r12
unsigned int v7; // ebx
signed int v8; // er15
signed int v9; // edi
float v10; // xmm0_4
const char *v13; // rdx
int v14; // [rsp+0h] [rbp-58h]
unsigned int v15; // [rsp+Ch] [rbp-4Ch]
unsigned int v16; // [rsp+10h] [rbp-48h]
int v17; // [rsp+14h] [rbp-44h]
int v18; // [rsp+18h] [rbp-40h]
unsigned int v19; // [rsp+1Ch] [rbp-3Ch]

v2 = *((_DWORD *)a1 + 2);
v19 = v2;
v18 = v2 + 1;
v3 = v2 - 1;
if ( v3 < 0 )
{
v13 = "male";
if ( *(_DWORD *)a1 )
v13 = (const char *)&unk_4020B2;
printf(asc_4020C8, *((unsigned int *)a1 + 1), v13, v19, 999999999LL, 999999999LL, 0LL, 0LL, 0LL, 0LL);
if ( v19 )
printf("SHIT");
}
else
{
v4 = v3;
v5 = 0;
v6 = 0LL;
v7 = 999999999;
v17 = 0;
v16 = 0;
v15 = 999999999;
do
{
v8 = binary_dist((double)*((int *)a1 + 3), (double)(*((_DWORD *)a1 + 3) / 2));
v9 = binary_dist((double)*((int *)a1 + 4), (double)(*((_DWORD *)a1 + 4) / 2));
if ( v8 <= 0 )
v8 = 1 - v8;
if ( v9 <= 0 )
v9 = 1 - v9;
usleep(v9);
v14 = clock();
enter(*(_DWORD *)a1);
v17 += v8;
v10 = (float)(int)(clock() - v14);
v5 = (int)(float)((float)v5 + v10);
if ( v10 > (float)(int)v16 )
v16 = (int)v10;
if ( (float)(int)v15 > v10 )
v15 = (int)v10;
if ( (int)v6 < v8 )
v6 = (unsigned int)v8;
if ( (int)v7 > v8 )
v7 = v8;
usleep(v8);
leave();
}
while ( v4-- != 0 );
if ( *(_DWORD *)a1 )
printf(
asc_4020C8,
*((unsigned int *)a1 + 1),
&unk_4020B2,
v19,
v15,
v7,
v16,
v6,
(unsigned int)(v5 / v18),
(unsigned int)(v17 / v18));
else
printf(
asc_4020C8,
*((unsigned int *)a1 + 1),
"male",
v19,
v15,
v7,
v16,
v6,
(unsigned int)(v5 / v18),
(unsigned int)(v17 / v18));
}
free(a1);
return 0LL;
}

original_code:
void* individual(void* arg)
{
 indiv_info* me = (indiv_info*)(arg);
 int minimum_waiting = 999999999;
 int maximum_waiting = 0;
 int minimum_going = 999999999;
 int maximum_going = 0;
 int trips = me -> loops;
 int trips_loop = trips;
 int trips_count = 0;
 int total_waiting = 0;
 int total_going = 0;
 while(--trips_loop >= 0)
 {
  int poop = (int)(binary_dist(me->stay, me->stay/2));
  int hold_it = (int)(binary_dist(me->arr, me->arr/2));
  if (poop <= 0)
  {
   poop = -poop + 1;
  }
  if (hold_it <= 0)
  {
   hold_it = -hold_it + 1;
  }
  usleep(hold_it);
  clock_t start = clock(); enter(me -> g); float waiting_elapsed = clock()-start;
  {
   total_waiting += waiting_elapsed;
   total_going += poop;
   if (waiting_elapsed > maximum_waiting)
    maximum_waiting = waiting_elapsed;
   if (waiting_elapsed < minimum_waiting)
    minimum_waiting = waiting_elapsed;
   if (poop > maximum_going)
    maximum_going = poop;
   if (poop < minimum_going)
    minimum_going = poop;
   usleep(poop);
  }
  leave();
  trips_count++;
 }
 printf("\n╔═══════════════\n║id: %i\n║sex: %s\n║trips: %i\n║min:\n║\twait:%i\n║\tstay:%i\n║max:\n║\twait:%i\n║\tstay:%i\n║avg:\n║\twait:%i\n║\tstay:%i\n╚═══════════════\n",
   me -> id, (me -> g == MALE) ? "male" : "female", trips,
   minimum_waiting, minimum_going,
   maximum_waiting, maximum_going,
   total_waiting / (trips+1), total_going / (trips+1));
 if (trips -trips_count)
 {
  printf("SHIT");
 }
    free(me);
}

canonical_original_code:
void *individual(void *arg)
{
 struct indiv_info *me = (indiv_info*)(arg);
 int minimum_waiting = 999999999;
 int maximum_waiting = 0;
 int minimum_going = 999999999;
 int maximum_going = 0;
 int trips = me -> loops;
 int trips_loop = trips;
 int trips_count = 0;
 int total_waiting = 0;
 int total_going = 0;
 while(--trips_loop >= 0)
 {
  int poop = (int)(binary_dist(me->stay, me->stay/2));
  int hold_it = (int)(binary_dist(me->arr, me->arr/2));
  if (poop <= 0)
  {
   poop = -poop + 1;
  }
  if (hold_it <= 0)
  {
   hold_it = -hold_it + 1;
  }
  usleep(hold_it);
  long start = clock(); enter(me -> g); float waiting_elapsed = clock()-start;
  {
   total_waiting += waiting_elapsed;
   total_going += poop;
   if (waiting_elapsed > maximum_waiting)
    maximum_waiting = waiting_elapsed;
   if (waiting_elapsed < minimum_waiting)
    minimum_waiting = waiting_elapsed;
   if (poop > maximum_going)
    maximum_going = poop;
   if (poop < minimum_going)
    minimum_going = poop;
   usleep(poop);
  }
  leave();
  trips_count++;
 }
 printf("\n╔═══════════════\n║id: %i\n║sex: %s\n║trips: %i\n║min:\n║\twait:%i\n║\tstay:%i\n║max:\n║\twait:%i\n║\tstay:%i\n║avg:\n║\twait:%i\n║\tstay:%i\n╚═══════════════\n",
   me -> id, (me -> g == MALE) ? "male" : "female", trips,
   minimum_waiting, minimum_going,
   maximum_waiting, maximum_going,
   total_waiting / (trips+1), total_going / (trips+1));
 if (trips -trips_count)
 {
  printf("SHIT");
 }
    free(me);
}

variable_types:
arg
void *

variable_types: struct indiv_info { enum gender { MALE=0, FEMALE=1 } g; int id; int loops; int stay; int arr; } *

variable_types: trips int

variable_types: poop int

variable_types: start long

user_defined_types: struct indiv_info { enum gender g; int id; int loops; int stay; int arr; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // rbp
int v4; // er15
int v5; // ebx
size_t v6; // rbp
int v7; // er14
_QWORD *v8; // r12
unsigned int v9; // edi
__int64 v10; // r13
__int64 v11; // rbp
_DWORD *v12; // r15
char v13; // al
int v14; // eax
pthread_t *v15; // rdi
pthread_t v16; // rdi
int v18; // [rsp+Ch] [rbp-4Ch]
double v19; // [rsp+10h] [rbp-48h]
double v20; // [rsp+18h] [rbp-40h]

if ( argc <= 4 )
{
puts("useage:");
}
else
{
v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v5 = v3 - 1;
v6 = v3;
v18 = atoi(argv[3]);
v7 = atoi(argv[4]);
v8 = malloc(v6 * 8);
v9 = time(0LL);
srand(v9);
initialize();
if ( v5 >= 0 )
{
v10 = v5;
v11 = (__int64)&v8[v6 - 1];
v20 = (double)v4;
v19 = (double)(v4 / 3);
do
{
v12 = malloc(0x14uLL);
v13 = random();
v12[1] = v5;
*v12 = v13 & 1;
v14 = binary_dist(v20, v19);
v15 = (pthread_t *)v11;
v12[3] = v7;
if ( v14 < 0 )
v14 = 1;
--v5;
v11 -= 8LL;
v12[2] = v14;
v12[4] = v18;
pthread_create(v15, 0LL, individual, v12);
}
while ( v5 != -1 );
do
{
v16 = v8[v10--];
pthread_join(v16, 0LL);
}
while ( (int)v10 >= 0 );
}
finalize();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // rbp
int v4; // er15
int v5; // ebx
size_t v6; // rbp
int v7; // er14
_QWORD *v8; // r12
unsigned int v9; // edi
__int64 v10; // r13
__int64 v11; // rbp
_DWORD *v12; // r15
char v13; // al
int v14; // eax
pthread_t *v15; // rdi
pthread_t v16; // rdi
int v18; // [rsp+Ch] [rbp-4Ch]
double v19; // [rsp+10h] [rbp-48h]
double v20; // [rsp+18h] [rbp-40h]

if ( argc <= 4 )
{
puts("useage:");
}
else
{
v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v5 = v3 - 1;
v6 = v3;
v18 = atoi(argv[3]);
v7 = atoi(argv[4]);
v8 = malloc(v6 * 8);
v9 = time(0LL);
srand(v9);
func7();
if ( v5 >= 0 )
{
v10 = v5;
v11 = (__int64)&v8[v6 - 1];
v20 = (double)v4;
v19 = (double)(v4 / 3);
do
{
v12 = malloc(0x14uLL);
v13 = random();
v12[1] = v5;
*v12 = v13 & 1;
v14 = func12(v20, v19);
v15 = (pthread_t *)v11;
v12[3] = v7;
if ( v14 < 0 )
v14 = 1;
--v5;
v11 -= 8LL;
v12[2] = v14;
v12[4] = v18;
pthread_create(v15, 0LL, individual, v12);
}
while ( v5 != -1 );
do
{
v16 = v8[v10--];
pthread_join(v16, 0LL);
}
while ( (int)v10 >= 0 );
}
func8();
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
 if (argc < 5)
 {
  printf("useage:\n");
  return 0;
 }
 int arg_n_users = atoi (argv[1]);
 int arg_avg_loop_count = atoi (argv[2]);
 int arg_mean_arrival = atoi (argv[3]);
 int arg_mean_useage = atoi (argv[4]);
 int backup_n_user = arg_n_users - 1;
 pthread_t* all_threads = (pthread_t*)(malloc(sizeof(pthread_t) * arg_n_users));
 srand(time(((void *)0)));
 initialize();
 while (-- arg_n_users >= 0)
 {
  indiv_info* i = (indiv_info*)(malloc(sizeof(indiv_info)));
     i -> g = (random()%2==0) ? MALE : FEMALE;
     i -> id = arg_n_users;
     i -> loops = (int)(binary_dist(arg_avg_loop_count, arg_avg_loop_count/3));
     i -> loops = (i -> loops < 0) ? 1 : i -> loops;
     i -> stay = arg_mean_useage;
     i -> arr = arg_mean_arrival;
  pthread_create(all_threads+arg_n_users, ((void *)0), individual, i);
 }
 while(backup_n_user >= 0)
 {
  pthread_join(*(all_threads+backup_n_user), ((void *)0));
  backup_n_user --;
 }
 finalize();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 if (argc < 5)
 {
  printf("useage:\n");
  return 0;
 }
 int arg_n_users = atoi (argv[1]);
 int arg_avg_loop_count = atoi (argv[2]);
 int arg_mean_arrival = atoi (argv[3]);
 int arg_mean_useage = atoi (argv[4]);
 int backup_n_user = arg_n_users - 1;
 unsigned long *all_threads = (pthread_t*)(malloc(sizeof(pthread_t) * arg_n_users));
 srand(time(((void *)0)));
 initialize();
 while (-- arg_n_users >= 0)
 {
  struct indiv_info *i = (indiv_info*)(malloc(sizeof(indiv_info)));
     i -> g = (random()%2==0) ? MALE : FEMALE;
     i -> id = arg_n_users;
     i -> loops = (int)(binary_dist(arg_avg_loop_count, arg_avg_loop_count/3));
     i -> loops = (i -> loops < 0) ? 1 : i -> loops;
     i -> stay = arg_mean_useage;
     i -> arr = arg_mean_arrival;
  pthread_create(all_threads+arg_n_users, ((void *)0), individual, i);
 }
 while(backup_n_user >= 0)
 {
  pthread_join(*(all_threads+backup_n_user), ((void *)0));
  backup_n_user --;
 }
 finalize();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall exec_forg(char **argv, __int64 a2)
{
struct rusage *v2; // r13
unsigned int v3; // er12
__int64 result; // rax

v2 = (struct rusage *)malloc(0x90uLL);
v3 = fork();
inject_time(a2);
if ( v3 )
{
wait((__WAIT_STATUS)v3);
getrusage(RUSAGE_SELF, v2);
result = get_proc_info(v2, a2);
}
else
{
execvp(*argv, argv);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char **argv, __int64 a2)
{
struct rusage *v2; // r13
unsigned int v3; // er12
__int64 result; // rax

v2 = (struct rusage *)malloc(0x90uLL);
v3 = fork();
inject_time(a2);
if ( v3 )
{
wait((__WAIT_STATUS)v3);
getrusage(RUSAGE_SELF, v2);
result = get_proc_info(v2, a2);
}
else
{
execvp(*argv, argv);
result = 0LL;
}
return result;
}

original_code:
proc_info* exec_forg(char** exec, proc_info* status)
{
 struct rusage *r = (struct rusage*)(malloc(sizeof(struct rusage)));
 int pid = fork();
 inject_time(status);
 if (pid == 0)
 {
  execvp(exec[0], exec);
 }
 else
 {
  wait(pid);
  getrusage(RUSAGE_SELF, r);
  return get_proc_info(r, status);
 }
}

canonical_original_code:
struct proc_info *exec_forg(char **exec, struct proc_info *status)
{
 struct rusage *r = (struct rusage*)(malloc(sizeof(struct rusage)));
 int pid = fork();
 inject_time(status);
 if (pid == 0)
 {
  execvp(exec[0], exec);
 }
 else
 {
  wait(pid);
  getrusage(RUSAGE_SELF, r);
  return get_proc_info(r, status);
 }
}

variable_types:
exec
char **

variable_types:
status
struct proc_info *

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; } *

variable_types: pid int

return_type: struct proc_info { int err_code; long long real_time; int cpu_time; int count_preempted; int count_cpu_yeild; int count_page_fault; int count_pf_kcache; } *

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


decompiled_code:
int __fastcall print_all(__int64 **a1)
{
__int64 **v1; // rbx

v1 = a1;
puts(&s);
if ( a1 )
{
do
{
printf(&byte_1D0, *((unsigned int *)v1 + 5), *((unsigned int *)v1 + 4));
v1 = (__int64 **)*v1;
}
while ( v1 );
}
return puts(&byte_208);
}

canonical_decompiled_code:
int __fastcall func0(__int64 **a1)
{
__int64 **v1; // rbx

v1 = a1;
puts(&s);
if ( a1 )
{
do
{
printf(&byte_1D0, *((unsigned int *)v1 + 5), *((unsigned int *)v1 + 4));
v1 = (__int64 **)*v1;
}
while ( v1 );
}
return puts(&byte_208);
}

original_code:
void print_all(proc_list* current)
{
 printf("╔═processes═════╗\n");
 while(current != 0)
 {
  printf("║[%i] %i\t║\n", current->my_id, current->pid);
  current = current->next;
 }
 printf("╚═══════════════╝\n");
}

canonical_original_code:
void print_all(struct proc_list *current)
{
 printf("╔═processes═════╗\n");
 while(current != 0)
 {
  printf("║[%i] %i\t║\n", current->my_id, current->pid);
  current = current->next;
 }
 printf("╚═══════════════╝\n");
}

variable_types: struct proc_list { struct proc_list *next; struct proc_info { int err_code; long long real_time; int cpu_time; int count_preempted; int count_cpu_yeild; int count_page_fault; int count_pf_kcache; } *start_info; int pid; int my_id; } *


repo: tmerrifi/TaskClock

decompiled_code:
__int64 __fastcall perf_event_open(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, __int64 a5)
{
return (int)syscall(298LL, a1, a2, a3, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, __int64 a5)
{
return (int)syscall(298LL, a1, a2, a3, a4, a5);
}

original_code:
long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
                int cpu, int group_fd, unsigned long flags)
{
    int ret;
    ret = syscall(298, hw_event, pid, cpu,
                   group_fd, flags);
    return ret;
}

canonical_original_code:
long perf_event_open(struct perf_event_attr *hw_event, int pid, int cpu, int group_fd, unsigned long flags)
{
    int ret;
    ret = syscall(298, hw_event, pid, cpu,
                   group_fd, flags);
    return ret;
}

variable_types: struct perf_event_attr { unsigned int type; unsigned int size; unsigned long long config; union { unsigned long long sample_period; unsigned long long sample_freq; }; unsigned long long sample_type; unsigned long long read_format; unsigned long long disabled; union { unsigned int wakeup_events; unsigned int wakeup_watermark; }; unsigned int bp_type; union { unsigned long long bp_addr; unsigned long long config1; }; union { unsigned long long bp_len; unsigned long long config2; }; unsigned long long branch_sample_type; } *

variable_types: cpu int

variable_types:
flags
unsigned long

variable_types: ret int


decompiled_code:
char *__fastcall perf_counter_init(unsigned int a1, unsigned int a2, __int64 a3)
{
int v4; // eax
unsigned int v5; // ebp
char *v6; // r12
__pid_t v7; // eax
char *result; // rax
unsigned int v9; // ebx
unsigned int v10; // eax
__int64 v11; // [rsp+0h] [rbp-68h] BYREF
__int64 v12; // [rsp+8h] [rbp-60h]
__int64 v13; // [rsp+10h] [rbp-58h]
__int128 v14; // [rsp+18h] [rbp-50h]
__int128 v15; // [rsp+28h] [rbp-40h]
__int128 v16; // [rsp+38h] [rbp-30h]
__int64 v17; // [rsp+48h] [rbp-20h]

v11 = 0x5000000004LL;
v15 = 0LL;
v17 = 0LL;
v12 = 192LL;
v13 = a1;
LODWORD(v15) = 2097249;
v14 = 0LL;
v16 = 0LL;
v4 = perf_event_open((__int64)&v11, 0, 0xFFFFFFFF, a2, 0LL);
if ( v4 == -1 )
{
fprintf(stderr, "Error opening leader %llx\n", v12);
exit(1);
}
v5 = v4;
v6 = (char *)mmap(0LL, 0x41000uLL, 3, 1, v4, 0LL);
if ( v6 == (char *)-1LL )
{
v9 = *_errno_location();
v10 = getpid();
fprintf(stderr, "\nFAILED! %d fd %d %d\n", v10, v5, v9);
perror("FAILED");
close(v5);
exit(1);
}
v7 = getpid();
*(_DWORD *)(a3 + 4) = v5;
*(_DWORD *)a3 = v7;
result = v6 + 4096;
*(_QWORD *)(a3 + 16) = v6 + 4096;
*(_QWORD *)(a3 + 8) = v6;
*(_DWORD *)(a3 + 24) = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned int a1, unsigned int a2, __int64 a3)
{
int v4; // eax
unsigned int v5; // ebp
char *v6; // r12
__pid_t v7; // eax
char *result; // rax
unsigned int v9; // ebx
unsigned int v10; // eax
__int64 v11; // [rsp+0h] [rbp-68h] BYREF
__int64 v12; // [rsp+8h] [rbp-60h]
__int64 v13; // [rsp+10h] [rbp-58h]
__int128 v14; // [rsp+18h] [rbp-50h]
__int128 v15; // [rsp+28h] [rbp-40h]
__int128 v16; // [rsp+38h] [rbp-30h]
__int64 v17; // [rsp+48h] [rbp-20h]

v11 = 0x5000000004LL;
v15 = 0LL;
v17 = 0LL;
v12 = 192LL;
v13 = a1;
LODWORD(v15) = 2097249;
v14 = 0LL;
v16 = 0LL;
v4 = func0((__int64)&v11, 0, 0xFFFFFFFF, a2, 0LL);
if ( v4 == -1 )
{
fprintf(stderr, "Error opening leader %llx\n", v12);
exit(1);
}
v5 = v4;
v6 = (char *)mmap(0LL, 0x41000uLL, 3, 1, v4, 0LL);
if ( v6 == (char *)-1LL )
{
v9 = *_errno_location();
v10 = getpid();
fprintf(stderr, "\nFAILED! %d fd %d %d\n", v10, v5, v9);
perror("FAILED");
close(v5);
exit(1);
}
v7 = getpid();
*(_DWORD *)(a3 + 4) = v5;
*(_DWORD *)a3 = v7;
result = v6 + 4096;
*(_QWORD *)(a3 + 16) = v6 + 4096;
*(_QWORD *)(a3 + 8) = v6;
*(_DWORD *)(a3 + 24) = 0;
return result;
}

original_code:
void perf_counter_init(u_int32_t sample_period, int32_t group_fd, struct perf_counter_info * pci){
  struct perf_event_attr pe;
  int fd;
  long long count;
  memset(&pe, 0, sizeof(struct perf_event_attr));
  pe.type = PERF_TYPE_RAW;
  pe.size = sizeof(struct perf_event_attr);
  pe.config = (0x0C0ULL) | (0x0000ULL);
  pe.disabled = 1;
  pe.exclude_kernel = 1;
  pe.exclude_hv = 1;
  pe.sample_period = sample_period;
  pe.task_clock = 1;
  fd = perf_event_open(&pe, 0, -1, group_fd, 0);
  if (fd == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe.config);
    exit(1);
  }
  void * ring_buffer;
  if ((ring_buffer = mmap(((void *)0), 4096 + (4096 * 64) , 0x1 | 0x2,
     0x01, fd, 0)) == ((void *) -1)) {
      fprintf(stderr, "\nFAILED! %d fd %d %d\n", getpid(), fd, (*__errno_location ()));
      perror("FAILED");
      close(fd);
      exit(1);
  }
  pci->pid=getpid();
  pci->fd=fd;
  pci->ring_buffer_current=ring_buffer+4096;
  pci->ring_buffer=ring_buffer;
  pci->started=0;
}

canonical_original_code:
void perf_counter_init(unsigned int sample_period, int group_fd, struct perf_counter_info *pci){
  struct perf_event_attr pe;
  int fd;
  long long count;
  memset(&pe, 0, sizeof(struct perf_event_attr));
  pe.type = PERF_TYPE_RAW;
  pe.size = sizeof(struct perf_event_attr);
  pe.config = (0x0C0ULL) | (0x0000ULL);
  pe.disabled = 1;
  pe.exclude_kernel = 1;
  pe.exclude_hv = 1;
  pe.sample_period = sample_period;
  pe.task_clock = 1;
  fd = perf_event_open(&pe, 0, -1, group_fd, 0);
  if (fd == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe.config);
    exit(1);
  }
  void *ring_buffer;
  if ((ring_buffer = mmap(((void *)0), 4096 + (4096 * 64) , 0x1 | 0x2,
     0x01, fd, 0)) == ((void *) -1)) {
      fprintf(stderr, "\nFAILED! %d fd %d %d\n", getpid(), fd, (*__errno_location ()));
      perror("FAILED");
      close(fd);
      exit(1);
  }
  pci->pid=getpid();
  pci->fd=fd;
  pci->ring_buffer_current=ring_buffer+4096;
  pci->ring_buffer=ring_buffer;
  pci->started=0;
}

variable_types: struct perf_counter_info { int pid; int fd; void *ring_buffer; void *ring_buffer_current; int started; } *

variable_types: struct perf_event_attr { unsigned int type; unsigned int size; unsigned long long config; union { unsigned long long sample_period; unsigned long long sample_freq; }; unsigned long long sample_type; unsigned long long read_format; unsigned long long disabled; union { unsigned int wakeup_events; unsigned int wakeup_watermark; }; unsigned int bp_type; union { unsigned long long bp_addr; unsigned long long config1; }; union { unsigned long long bp_len; unsigned long long config2; }; unsigned long long branch_sample_type; }

variable_types: fd int

variable_types:
count
long long


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

result = ioctl(*(_DWORD *)(a1 + 4), 0x2400uLL, 0LL);
if ( result )
{
puts("\nenable wrong");
exit(1);
}
*(_DWORD *)(a1 + 24) = 1;
return result;
}

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

result = ioctl(*(_DWORD *)(a1 + 4), 0x2400uLL, 0LL);
if ( result )
{
puts("\nenable wrong");
exit(1);
}
*(_DWORD *)(a1 + 24) = 1;
return result;
}

user_defined_types: struct perf_counter_info { int pid; int fd; void *ring_buffer; void *ring_buffer_current; int started; }