decompiled_code:
__int64 __fastcall send_data(int fd, void *buf, size_t n)
{
size_t v4; // rbx
unsigned int v5; // er12
int v6; // eax

if ( !n )
return 0;
v4 = n;
v5 = 0;
while ( 1 )
{
v6 = send(fd, buf, v4, 0x4000);
if ( (unsigned int)(v6 + 1) <= 1 )
break;
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
if ( !v4 )
return v5;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, size_t n)
{
size_t v4; // rbx
unsigned int v5; // er12
int v6; // eax

if ( !n )
return 0;
v4 = n;
v5 = 0;
while ( 1 )
{
v6 = send(fd, buf, v4, 0x4000);
if ( (unsigned int)(v6 + 1) <= 1 )
break;
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
if ( !v4 )
return v5;
}
return (unsigned int)-1;
}

original_code:
int send_data(int fd,void *buf,size_t buflen)
{
     int cc=0,total=0;
     while (buflen > 0) {
          cc = send(fd, buf, buflen, MSG_NOSIGNAL);
          if (cc == -1) {
               return cc;
          }
          if (cc == 0) {
            return -1;
          }
          buf += cc;
          total += cc;
          buflen -= cc;
     }
     return total;
}

canonical_original_code:
int send_data(int fd, void *buf, unsigned long buflen)
{
     int cc = 0; int total = 0;
     while (buflen > 0) {
          cc = send(fd, buf, buflen, MSG_NOSIGNAL);
          if (cc == -1) {
               return cc;
          }
          if (cc == 0) {
            return -1;
          }
          buf += cc;
          total += cc;
          buflen -= cc;
     }
     return total;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
buflen
unsigned long

variable_types: cc int

variable_types: total int


decompiled_code:
__int64 __fastcall recv_data(int fd, void *buf, size_t n)
{
size_t v4; // rbx
unsigned int v5; // er12
int v6; // eax

if ( !n )
return 0;
v4 = n;
v5 = 0;
while ( 1 )
{
v6 = recv(fd, buf, v4, 0x4000);
if ( (unsigned int)(v6 + 1) <= 1 )
break;
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
if ( !v4 )
return v5;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, void *buf, size_t n)
{
size_t v4; // rbx
unsigned int v5; // er12
int v6; // eax

if ( !n )
return 0;
v4 = n;
v5 = 0;
while ( 1 )
{
v6 = recv(fd, buf, v4, 0x4000);
if ( (unsigned int)(v6 + 1) <= 1 )
break;
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
if ( !v4 )
return v5;
}
return (unsigned int)-1;
}

original_code:
int recv_data(int fd,void *buf,size_t buflen)
{
     int cc=0,total=0;
     while (buflen > 0) {
          cc = recv(fd, buf, buflen, MSG_NOSIGNAL);
          if (cc == -1) {
               return cc;
          }
          if (cc == 0) {
            return -1;
          }
          buf += cc;
          total += cc;
          buflen -= cc;
     }
     return total;
}

canonical_original_code:
int recv_data(int fd, void *buf, unsigned long buflen)
{
     int cc = 0; int total = 0;
     while (buflen > 0) {
          cc = recv(fd, buf, buflen, MSG_NOSIGNAL);
          if (cc == -1) {
               return cc;
          }
          if (cc == 0) {
            return -1;
          }
          buf += cc;
          total += cc;
          buflen -= cc;
     }
     return total;
}

return_type: int


nanotime

repo: jsravn/fixpixel

decompiled_code:
__syscall_slong_t nanotime()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(1, &v1);
return v1.tv_nsec + 1000000000 * v1.tv_sec;
}

canonical_decompiled_code:
__syscall_slong_t func7()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(1, &v1);
return v1.tv_nsec + 1000000000 * v1.tv_sec;
}

original_code:
static long nanotime(void) {
    struct timespec cur;
    clock_gettime(1, &cur);
    return cur.tv_sec * ONE_BILLION + cur.tv_nsec;
}

canonical_original_code:
static long nanotime() {
    struct timespec cur;
    clock_gettime(1, &cur);
    return cur.tv_sec * ONE_BILLION + cur.tv_nsec;
}

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

return_type: long


init

decompiled_code:
__syscall_slong_t init()
{
int v0; // eax
unsigned int v1; // eax
__syscall_slong_t result; // rax
int v3; // ebx

initscr();
cbreak();
noecho();
nodelay(_bss_start, 1);
curs_set(0);
v0 = start_color();
if ( v0 )
{
v3 = v0;
endwin();
puts("Terminal does not support color mode");
exit(v3);
}
init_pair(1, 7, 1);
init_pair(2, 7, 2);
init_pair(3, 7, 4);
v1 = time(0LL);
srand(v1);
frames = 0LL;
result = nanotime();
start = result;
return result;
}

canonical_decompiled_code:
__syscall_slong_t func8()
{
int v0; // eax
unsigned int v1; // eax
__syscall_slong_t result; // rax
int v3; // ebx

initscr();
cbreak();
noecho();
nodelay(_bss_start, 1);
curs_set(0);
v0 = start_color();
if ( v0 )
{
v3 = v0;
endwin();
puts("Terminal does not support color mode");
exit(v3);
}
init_pair(1, 7, 1);
init_pair(2, 7, 2);
init_pair(3, 7, 4);
v1 = time(0LL);
srand(v1);
frames = 0LL;
result = func7();
start = result;
return result;
}

original_code:
static void init(void) {
    initscr(); cbreak(); noecho(); nodelay(stdscr, 1); curs_set(0);
    int r = start_color();
    if (r) {
        endwin();
        puts("Terminal does not support color mode");
        exit(r);
    }
    init_pair(RED_BG, 7, 1);
    init_pair(GREEN_BG, 7, 2);
    init_pair(BLUE_BG, 7, 4);
    srand(time(((void *)0)));
    frames = 0;
    start = nanotime();
}

canonical_original_code:
static void init() {
    initscr(); cbreak(); noecho(); nodelay(stdscr, 1); curs_set(0);
    int r = start_color();
    if (r) {
        endwin();
        puts("Terminal does not support color mode");
        exit(r);
    }
    init_pair(RED_BG, 7, 1);
    init_pair(GREEN_BG, 7, 2);
    init_pair(BLUE_BG, 7, 4);
    srand(time(((void *)0)));
    frames = 0;
    start = nanotime();
}

variable_types: r int

return_type: void


decompiled_code:
__int64 rand_color_pair()
{
int v0; // eax

v0 = rand() % 3;
if ( v0 == 1 )
return 512LL;
if ( v0 == 2 )
return 768LL;
if ( v0 )
{
endwin();
puts("BUG - rand_color_pair");
exit(-1);
}
return 256LL;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // eax

v0 = rand() % 3;
if ( v0 == 1 )
return 512LL;
if ( v0 == 2 )
return 768LL;
if ( v0 )
{
endwin();
puts("BUG - rand_color_pair");
exit(-1);
}
return 256LL;
}

return_type: unsigned int


decompiled_code:
__syscall_slong_t wait_for_next_frame()
{
__syscall_slong_t result; // rax
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 100000LL;
while ( 1 )
{
result = nanotime();
if ( (double)((int)result - (int)last_nanos_0) > 16666666.66666667 )
break;
nanosleep(&v1, 0LL);
}
last_nanos_0 = result;
return result;
}

canonical_decompiled_code:
__syscall_slong_t func10()
{
__syscall_slong_t result; // rax
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 100000LL;
while ( 1 )
{
result = func7();
if ( (double)((int)result - (int)last_nanos_0) > 16666666.66666667 )
break;
nanosleep(&v1, 0LL);
}
last_nanos_0 = result;
return result;
}

original_code:
static void wait_for_next_frame(void) {
    static long last_nanos = 0;
    const struct timespec sleep_time = { 0, RESOLUTION_MS * ONE_MILLION };
    while (1) {
        long cur_nanos = nanotime();
        long since_last_nanos = cur_nanos - last_nanos;
        if (since_last_nanos > (1.0 / FPS_CAP) * ONE_BILLION) {
            last_nanos = cur_nanos;
            break;
        }
        nanosleep(&sleep_time, ((void *)0));
    }
}

canonical_original_code:
static void wait_for_next_frame() {
    static long last_nanos = 0;
    struct timespec sleep_time = { 0, RESOLUTION_MS * ONE_MILLION };
    while (1) {
        long cur_nanos = nanotime();
        long since_last_nanos = cur_nanos - last_nanos;
        if (since_last_nanos > (1.0 / FPS_CAP) * ONE_BILLION) {
            last_nanos = cur_nanos;
            break;
        }
        nanosleep(&sleep_time, ((void *)0));
    }
}

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


spamcolors

decompiled_code:
int spamcolors()
{
chtype v0; // rsi
int result; // eax

while ( 1 )
{
result = wgetch(_bss_start);
if ( result != -1 )
break;
v0 = (unsigned int)rand_color_pair();
wbkgd(_bss_start, v0);
wrefresh(_bss_start);
++frames;
wait_for_next_frame();
}
return result;
}

canonical_decompiled_code:
int func11()
{
chtype v0; // rsi
int result; // eax

while ( 1 )
{
result = wgetch(_bss_start);
if ( result != -1 )
break;
v0 = (unsigned int)func9();
wbkgd(_bss_start, v0);
wrefresh(_bss_start);
++frames;
func10();
}
return result;
}

original_code:
static void spamcolors(void) {
    while (wgetch(stdscr) == (-1)) {
        wbkgd(stdscr,(rand_color_pair()));
        wrefresh(stdscr);
        frames++;
        wait_for_next_frame();
    }
}

canonical_original_code:
static void spamcolors() {
    while (wgetch(stdscr) == (-1)) {
        wbkgd(stdscr,(rand_color_pair()));
        wrefresh(stdscr);
        frames++;
        wait_for_next_frame();
    }
}


cleanup

decompiled_code:
int cleanup()
{
int v0; // eax
double v1; // xmm0_8

endwin();
v0 = nanotime();
if ( frames < 0 )
v1 = (double)(int)(frames & 1 | ((unsigned __int64)frames >> 1))
+ (double)(int)(frames & 1 | ((unsigned __int64)frames >> 1));
else
v1 = (double)(int)frames;
return printf("Frames per second: %f\n", v1 / ((double)(v0 - (int)start) / 1000000000.0));
}

canonical_decompiled_code:
int func12()
{
int v0; // eax
double v1; // xmm0_8

endwin();
v0 = func7();
if ( frames < 0 )
v1 = (double)(int)(frames & 1 | ((unsigned __int64)frames >> 1))
+ (double)(int)(frames & 1 | ((unsigned __int64)frames >> 1));
else
v1 = (double)(int)frames;
return printf("Frames per second: %f\n", v1 / ((double)(v0 - (int)start) / 1000000000.0));
}

original_code:
static void cleanup(void) {
    endwin();
    double elapsed_s = ((nanotime() - start) / (double) ONE_BILLION);
    printf("Frames per second: %f\n", frames / elapsed_s);
}

canonical_original_code:
static void cleanup() {
    endwin();
    double elapsed_s = ((nanotime() - start) / (double) ONE_BILLION);
    printf("Frames per second: %f\n", frames / elapsed_s);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init();
spamcolors();
cleanup();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func8();
func11();
func12();
return 0;
}

original_code:
int main(void) {
    init();
    spamcolors();
    cleanup();
    return 0;
}

canonical_original_code:
int main() {
    init();
    spamcolors();
    cleanup();
    return 0;
}


dummyFunc

repo: jstancek/selinux-testsuite

decompiled_code:
void __fastcall __noreturn dummyFunc(void *arg)
{
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *arg)
{
exit(0);
}

original_code:
static int dummyFunc(void *arg)
{
 exit(0);
}

variable_types:
arg
void *


childFunc

decompiled_code:
void __fastcall __noreturn childFunc(void *arg)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

close(*((_DWORD *)arg + 3));
if ( read(*((_DWORD *)arg + 2), buf, 1uLL) )
{
fwrite("Failure in child: read from pipe returned != 0\n", 1uLL, 0x2FuLL, stderr);
exit(1);
}
printf("About to exec %s\n", **(const char ***)arg);
execvp(**(const char ***)arg, *(char *const **)arg);
perror("execvp");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *arg)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

close(*((_DWORD *)arg + 3));
if ( read(*((_DWORD *)arg + 2), buf, 1uLL) )
{
fwrite("Failure in child: read from pipe returned != 0\n", 1uLL, 0x2FuLL, stderr);
exit(1);
}
printf("About to exec %s\n", **(const char ***)arg);
execvp(**(const char ***)arg, *(char *const **)arg);
perror("execvp");
exit(1);
}

original_code:
static int
childFunc(void *arg)
{
 struct child_args *args = (struct child_args *) arg;
 char ch;
 close(args->pipe_fd[1]);
 if (read(args->pipe_fd[0], &ch, 1) != 0) {
  fprintf(stderr,
   "Failure in child: read from pipe returned != 0\n");
  exit(1);
 }
 printf("About to exec %s\n", args->argv[0]);
 execvp(args->argv[0], args->argv);
 do { perror("execvp"); exit(1); } while (0);
}

variable_types: struct child_args { char **argv; int pipe_fd[2]; } *

variable_types: ch char


decompiled_code:
int __fastcall update_map(void *buf, char *file)
{
size_t v2; // rbx
_BYTE *v3; // rax
int v4; // eax
int v5; // er12
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax

v2 = strlen((const char *)buf);
if ( v2 )
{
v3 = buf;
do
{
if ( *v3 == 44 )
*v3 = 10;
++v3;
}
while ( v3 != (char *)buf + v2 );
}
v4 = open(file, 2);
v5 = v4;
if ( v4 == -1 )
{
v7 = __errno_location();
v8 = strerror(*v7);
fprintf(stderr, "ERROR: open %s: %s\n", file, v8);
exit(1);
}
if ( write(v4, buf, v2) != v2 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "ERROR: write %s: %s\n", file, v10);
exit(1);
}
return close(v5);
}

canonical_decompiled_code:
int __fastcall func11(void *buf, char *file)
{
size_t v2; // rbx
_BYTE *v3; // rax
int v4; // eax
int v5; // er12
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax

v2 = strlen((const char *)buf);
if ( v2 )
{
v3 = buf;
do
{
if ( *v3 == 44 )
*v3 = 10;
++v3;
}
while ( v3 != (char *)buf + v2 );
}
v4 = open(file, 2);
v5 = v4;
if ( v4 == -1 )
{
v7 = __errno_location();
v8 = strerror(*v7);
fprintf(stderr, "ERROR: open %s: %s\n", file, v8);
exit(1);
}
if ( write(v4, buf, v2) != v2 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "ERROR: write %s: %s\n", file, v10);
exit(1);
}
return close(v5);
}

original_code:
static void
update_map(char *mapping, char *map_file)
{
 int fd, j;
 size_t map_len;
 map_len = strlen(mapping);
 for (j = 0; j < map_len; j++)
  if (mapping[j] == ',')
   mapping[j] = '\n';
 fd = open(map_file, 02);
 if (fd == -1) {
  fprintf(stderr, "ERROR: open %s: %s\n", map_file,
   strerror((*__errno_location ())));
  exit(1);
 }
 if (write(fd, mapping, map_len) != map_len) {
  fprintf(stderr, "ERROR: write %s: %s\n", map_file,
   strerror((*__errno_location ())));
  exit(1);
 }
 close(fd);
}

canonical_original_code:
static void update_map(char *mapping, char *map_file)
{
 int fd; int j;
 unsigned long map_len;
 map_len = strlen(mapping);
 for (j = 0; j < map_len; j++)
  if (mapping[j] == ',')
   mapping[j] = '\n';
 fd = open(map_file, 02);
 if (fd == -1) {
  fprintf(stderr, "ERROR: open %s: %s\n", map_file,
   strerror((*__errno_location ())));
  exit(1);
 }
 if (write(fd, mapping, map_len) != map_len) {
  fprintf(stderr, "ERROR: write %s: %s\n", map_file,
   strerror((*__errno_location ())));
  exit(1);
 }
 close(fd);
}

variable_types:
mapping
char *

variable_types: j int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(stderr, "Usage: %s [options] cmd [arg...]\n\n", a1);
fwrite(
"Create a child process that executes a shell command in a new user namespace,\n"
"and possibly also other new namespace(s).\n"
"\n",
1uLL,
0x79uLL,
stderr);
fwrite("Options can be:\n\n", 1uLL, 0x11uLL, stderr);
fprintf(stderr, " %s", "-i New IPC namespace\n");
fprintf(stderr, " %s", "-m New mount namespace\n");
fprintf(stderr, " %s", "-n New network namespace\n");
fprintf(stderr, " %s", "-p New PID namespace\n");
fprintf(stderr, " %s", "-u New UTS namespace\n");
fprintf(stderr, " %s", "-U New user namespace\n");
fprintf(stderr, " %s", "-M uid_map Specify UID map for user namespace\n");
fprintf(stderr, " %s", "-G gid_map Specify GID map for user namespace\n");
fprintf(stderr, " %s", "-z Map user's UID and GID to 0 in user namespace\n");
fprintf(stderr, " %s", " (equivalent to: -M '0 <uid> 1' -G '0 <gid> 1'\n");
fprintf(stderr, " %s", "-v Display verbose messages\n");
fprintf(stderr, " %s", "-t Test clone flags combination is supported\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "If -z, -M, or -G is specified, -U is required.\n");
fprintf(stderr, " %s", "It is not permitted to specify both -z and either -M or -G.\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "Map strings for -M and -G consist of records of the form:\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", " ID-inside-ns ID-outside-ns len\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "A map string can contain multiple records, separated by commas;\n");
fprintf(stderr, " %s", "the commas are replaced by newlines before writing to map files.\n");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
fprintf(stderr, "Usage: %s [options] cmd [arg...]\n\n", a1);
fwrite(
"Create a child process that executes a shell command in a new user namespace,\n"
"and possibly also other new namespace(s).\n"
"\n",
1uLL,
0x79uLL,
stderr);
fwrite("Options can be:\n\n", 1uLL, 0x11uLL, stderr);
fprintf(stderr, " %s", "-i New IPC namespace\n");
fprintf(stderr, " %s", "-m New mount namespace\n");
fprintf(stderr, " %s", "-n New network namespace\n");
fprintf(stderr, " %s", "-p New PID namespace\n");
fprintf(stderr, " %s", "-u New UTS namespace\n");
fprintf(stderr, " %s", "-U New user namespace\n");
fprintf(stderr, " %s", "-M uid_map Specify UID map for user namespace\n");
fprintf(stderr, " %s", "-G gid_map Specify GID map for user namespace\n");
fprintf(stderr, " %s", "-z Map user's UID and GID to 0 in user namespace\n");
fprintf(stderr, " %s", " (equivalent to: -M '0 <uid> 1' -G '0 <gid> 1'\n");
fprintf(stderr, " %s", "-v Display verbose messages\n");
fprintf(stderr, " %s", "-t Test clone flags combination is supported\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "If -z, -M, or -G is specified, -U is required.\n");
fprintf(stderr, " %s", "It is not permitted to specify both -z and either -M or -G.\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "Map strings for -M and -G consist of records of the form:\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", " ID-inside-ns ID-outside-ns len\n");
fprintf(stderr, " %s", "\n");
fprintf(stderr, " %s", "A map string can contain multiple records, separated by commas;\n");
fprintf(stderr, " %s", "the commas are replaced by newlines before writing to map files.\n");
exit(1);
}

original_code:
static void
usage(char *pname)
{
 fprintf(stderr, "Usage: %s [options] cmd [arg...]\n\n", pname);
 fprintf(stderr, "Create a child process that executes a shell "
  "command in a new user namespace,\n"
  "and possibly also other new namespace(s).\n\n");
 fprintf(stderr, "Options can be:\n\n");
 fprintf(stderr, "    %s", "-i          New IPC namespace\n");;
 fprintf(stderr, "    %s", "-m          New mount namespace\n");;
 fprintf(stderr, "    %s", "-n          New network namespace\n");;
 fprintf(stderr, "    %s", "-p          New PID namespace\n");;
 fprintf(stderr, "    %s", "-u          New UTS namespace\n");;
 fprintf(stderr, "    %s", "-U          New user namespace\n");;
 fprintf(stderr, "    %s", "-M uid_map  Specify UID map for user namespace\n");;
 fprintf(stderr, "    %s", "-G gid_map  Specify GID map for user namespace\n");;
 fprintf(stderr, "    %s", "-z          Map user's UID and GID to 0 in user namespace\n");;
 fprintf(stderr, "    %s", "            (equivalent to: -M '0 <uid> 1' -G '0 <gid> 1'\n");;
 fprintf(stderr, "    %s", "-v          Display verbose messages\n");;
 fprintf(stderr, "    %s", "-t          Test clone flags combination is supported\n");;
 fprintf(stderr, "    %s", "\n");;
 fprintf(stderr, "    %s", "If -z, -M, or -G is specified, -U is required.\n");;
 fprintf(stderr, "    %s", "It is not permitted to specify both -z and either -M or -G.\n");;
 fprintf(stderr, "    %s", "\n");;
 fprintf(stderr, "    %s", "Map strings for -M and -G consist of records of the form:\n");;
 fprintf(stderr, "    %s", "\n");;
 fprintf(stderr, "    %s", "    ID-inside-ns   ID-outside-ns   len\n");;
 fprintf(stderr, "    %s", "\n");;
 fprintf(stderr, "    %s", "A map string can contain multiple records, separated" " by commas;\n");;
 fprintf(stderr, "    %s", "the commas are replaced by newlines before writing" " to map files.\n");;
 exit(1);
}

variable_types:
pname
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
__int64 v4; // r14
unsigned int v5; // ebp
int v6; // eax
int v7; // edx
int result; // eax
int *v9; // rax
char *v10; // rax
int v11; // eax
int v12; // ebp
__uid_t v13; // eax
__gid_t v14; // eax
int v15; // [rsp+4h] [rbp-10C4h]
int v16; // [rsp+8h] [rbp-10C0h]
char buf[112]; // [rsp+10h] [rbp-10B8h] BYREF
char s[4096]; // [rsp+80h] [rbp-1048h] BYREF
const char **arg; // [rsp+1080h] [rbp-48h] BYREF
int pipedes[2]; // [rsp+1088h] [rbp-40h] BYREF

verbose = 0;
v3 = 0LL;
v4 = 0LL;
v16 = 0;
v15 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "+imnpuUM:G:zvt");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'G':
v3 = optarg;
break;
case 'M':
v4 = optarg;
break;
case 'U':
v5 |= 0x10000000u;
break;
case 'i':
v5 |= 0x8000000u;
break;
case 'm':
v5 |= 0x20000u;
break;
case 'n':
v5 |= 0x40000000u;
break;
case 'p':
v5 |= 0x20000000u;
break;
case 't':
v16 = 1;
break;
case 'u':
v5 |= 0x4000000u;
break;
case 'v':
verbose = 1;
break;
case 'z':
v15 = 1;
break;
default:
usage(*argv);
}
}
if ( ((_BYTE)v15 || v3 | v4) && ((v5 & 0x10000000) == 0 || (_BYTE)v15 && v3 | v4) )
usage(*argv);
arg = &argv[optind];
if ( pipe(pipedes) == -1 )
{
perror("pipe");
exit(1);
}
if ( !v16 )
{
v11 = clone((int (*)(void *))childFunc, &verbose, v5 | 0x11, &arg);
v12 = v11;
if ( v11 == -1 )
{
perror("clone");
exit(1);
}
if ( verbose )
printf("%s: PID of child created by clone() is %ld\n", *argv, v11);
if ( v4 || (_BYTE)v15 )
{
snprintf(s, 0x1000uLL, "/proc/%ld/uid_map", v12);
if ( v15 )
{
v13 = getuid();
snprintf(buf, 0x64uLL, "0 %ld 1", v13);
update_map(buf, s);
proc_setgroups_write(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
v14 = getgid();
snprintf(buf, 0x64uLL, "0 %ld 1", v14);
v3 = (unsigned __int64)buf;
}
else
{
update_map((void *)v4, s);
if ( !v3 )
goto LABEL_36;
proc_setgroups_write(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
}
}
else
{
if ( !v3 )
goto LABEL_36;
proc_setgroups_write(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
}
update_map((void *)v3, s);
LABEL_36:
close(pipedes[1]);
if ( waitpid(v12, 0LL, 0) != -1 )
{
if ( verbose )
printf("%s: terminating\n", *argv);
exit(0);
}
perror("waitpid");
exit(1);
}
v7 = clone((int (*)(void *))dummyFunc, &verbose, v5 | 0x11, &arg);
result = 0;
if ( v7 == -1 )
{
if ( verbose )
{
v9 = __errno_location();
v10 = strerror(*v9);
printf("clone(0x%x): %s\n", v5, v10);
}
result = *__errno_location();
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
__int64 v4; // r14
unsigned int v5; // ebp
int v6; // eax
int v7; // edx
int result; // eax
int *v9; // rax
char *v10; // rax
int v11; // eax
int v12; // ebp
__uid_t v13; // eax
__gid_t v14; // eax
int v15; // [rsp+4h] [rbp-10C4h]
int v16; // [rsp+8h] [rbp-10C0h]
char buf[112]; // [rsp+10h] [rbp-10B8h] BYREF
char s[4096]; // [rsp+80h] [rbp-1048h] BYREF
const char **arg; // [rsp+1080h] [rbp-48h] BYREF
int pipedes[2]; // [rsp+1088h] [rbp-40h] BYREF

verbose = 0;
v3 = 0LL;
v4 = 0LL;
v16 = 0;
v15 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "+imnpuUM:G:zvt");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'G':
v3 = optarg;
break;
case 'M':
v4 = optarg;
break;
case 'U':
v5 |= 0x10000000u;
break;
case 'i':
v5 |= 0x8000000u;
break;
case 'm':
v5 |= 0x20000u;
break;
case 'n':
v5 |= 0x40000000u;
break;
case 'p':
v5 |= 0x20000000u;
break;
case 't':
v16 = 1;
break;
case 'u':
v5 |= 0x4000000u;
break;
case 'v':
verbose = 1;
break;
case 'z':
v15 = 1;
break;
default:
func9(*argv);
}
}
if ( ((_BYTE)v15 || v3 | v4) && ((v5 & 0x10000000) == 0 || (_BYTE)v15 && v3 | v4) )
func9(*argv);
arg = &argv[optind];
if ( pipe(pipedes) == -1 )
{
perror("pipe");
exit(1);
}
if ( !v16 )
{
v11 = clone((int (*)(void *))childFunc, &verbose, v5 | 0x11, &arg);
v12 = v11;
if ( v11 == -1 )
{
perror("clone");
exit(1);
}
if ( verbose )
printf("%s: PID of child created by clone() is %ld\n", *argv, v11);
if ( v4 || (_BYTE)v15 )
{
snprintf(s, 0x1000uLL, "/proc/%ld/uid_map", v12);
if ( v15 )
{
v13 = getuid();
snprintf(buf, 0x64uLL, "0 %ld 1", v13);
func11(buf, s);
func10(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
v14 = getgid();
snprintf(buf, 0x64uLL, "0 %ld 1", v14);
v3 = (unsigned __int64)buf;
}
else
{
func11((void *)v4, s);
if ( !v3 )
goto LABEL_36;
func10(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
}
}
else
{
if ( !v3 )
goto LABEL_36;
func10(v12, "deny");
snprintf(s, 0x1000uLL, "/proc/%ld/gid_map", v12);
}
func11((void *)v3, s);
LABEL_36:
close(pipedes[1]);
if ( waitpid(v12, 0LL, 0) != -1 )
{
if ( verbose )
printf("%s: terminating\n", *argv);
exit(0);
}
perror("waitpid");
exit(1);
}
v7 = clone((int (*)(void *))dummyFunc, &verbose, v5 | 0x11, &arg);
result = 0;
if ( v7 == -1 )
{
if ( verbose )
{
v9 = __errno_location();
v10 = strerror(*v9);
printf("clone(0x%x): %s\n", v5, v10);
}
result = *__errno_location();
}
return result;
}

original_code:
int
main(int argc, char *argv[])
{
 int flags, opt, map_zero, test_clone = 0;
 pid_t child_pid;
 struct child_args args;
 char *uid_map, *gid_map;
 const int MAP_BUF_SIZE = 100;
 char map_buf[MAP_BUF_SIZE];
 char map_path[4096];
 flags = 0;
 verbose = 0;
 gid_map = ((void *)0);
 uid_map = ((void *)0);
 map_zero = 0;
 while ((opt = getopt(argc, argv, "+imnpuUM:G:zvt")) != -1) {
  switch (opt) {
  case 'i':
   flags |= 0x08000000;
   break;
  case 'm':
   flags |= 0x00020000;
   break;
  case 'n':
   flags |= 0x40000000;
   break;
  case 'p':
   flags |= 0x20000000;
   break;
  case 'u':
   flags |= 0x04000000;
   break;
  case 'v':
   verbose = 1;
   break;
  case 'z':
   map_zero = 1;
   break;
  case 'M':
   uid_map = optarg;
   break;
  case 'G':
   gid_map = optarg;
   break;
  case 'U':
   flags |= 0x10000000;
   break;
  case 't':
   test_clone = 1;
   break;
  default:
   usage(argv[0]);
  }
 }
 if (((uid_map != ((void *)0) || gid_map != ((void *)0) || map_zero) &&
      !(flags & 0x10000000)) ||
     (map_zero && (uid_map != ((void *)0) || gid_map != ((void *)0))))
  usage(argv[0]);
 args.argv = &argv[optind];
 if (pipe(args.pipe_fd) == -1)
  do { perror("pipe"); exit(1); } while (0);
 if (test_clone) {
  if (clone(dummyFunc, child_stack + (1024 * 1024),
     flags | 17, &args) == -1) {
   if (verbose)
    printf("clone(0x%x): %s\n", flags, strerror((*__errno_location ())));
   return (*__errno_location ());
  }
  return 0;
 }
 child_pid = clone(childFunc, child_stack + (1024 * 1024),
     flags | 17, &args);
 if (child_pid == -1)
  do { perror("clone"); exit(1); } while (0);
 if (verbose)
  printf("%s: PID of child created by clone() is %ld\n",
         argv[0], (long) child_pid);
 if (uid_map != ((void *)0) || map_zero) {
  snprintf(map_path, 4096, "/proc/%ld/uid_map",
    (long) child_pid);
  if (map_zero) {
   snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getuid());
   uid_map = map_buf;
  }
  update_map(uid_map, map_path);
 }
 if (gid_map != ((void *)0) || map_zero) {
  proc_setgroups_write(child_pid, "deny");
  snprintf(map_path, 4096, "/proc/%ld/gid_map",
    (long) child_pid);
  if (map_zero) {
   snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getgid());
   gid_map = map_buf;
  }
  update_map(gid_map, map_path);
 }
 close(args.pipe_fd[1]);
 if (waitpid(child_pid, ((void *)0), 0) == -1)
  do { perror("waitpid"); exit(1); } while (0);
 if (verbose)
  printf("%s: terminating\n", argv[0]);
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int flags; int opt; int map_zero; int test_clone = 0;
 int child_pid;
 struct child_args args;
 char *uid_map; char *gid_map;
 int MAP_BUF_SIZE = 100;
 char map_buf[-1];
 char map_path[4096];
 flags = 0;
 verbose = 0;
 gid_map = ((void *)0);
 uid_map = ((void *)0);
 map_zero = 0;
 while ((opt = getopt(argc, argv, "+imnpuUM:G:zvt")) != -1) {
  switch (opt) {
  case 'i':
   flags |= 0x08000000;
   break;
  case 'm':
   flags |= 0x00020000;
   break;
  case 'n':
   flags |= 0x40000000;
   break;
  case 'p':
   flags |= 0x20000000;
   break;
  case 'u':
   flags |= 0x04000000;
   break;
  case 'v':
   verbose = 1;
   break;
  case 'z':
   map_zero = 1;
   break;
  case 'M':
   uid_map = optarg;
   break;
  case 'G':
   gid_map = optarg;
   break;
  case 'U':
   flags |= 0x10000000;
   break;
  case 't':
   test_clone = 1;
   break;
  default:
   usage(argv[0]);
  }
 }
 if (((uid_map != ((void *)0) || gid_map != ((void *)0) || map_zero) &&
      !(flags & 0x10000000)) ||
     (map_zero && (uid_map != ((void *)0) || gid_map != ((void *)0))))
  usage(argv[0]);
 args.argv = &argv[optind];
 if (pipe(args.pipe_fd) == -1)
  do { perror("pipe"); exit(1); } while (0);
 if (test_clone) {
  if (clone(dummyFunc, child_stack + (1024 * 1024),
     flags | 17, &args) == -1) {
   if (verbose)
    printf("clone(0x%x): %s\n", flags, strerror((*__errno_location ())));
   return (*__errno_location ());
  }
  return 0;
 }
 child_pid = clone(childFunc, child_stack + (1024 * 1024),
     flags | 17, &args);
 if (child_pid == -1)
  do { perror("clone"); exit(1); } while (0);
 if (verbose)
  printf("%s: PID of child created by clone() is %ld\n",
         argv[0], (long) child_pid);
 if (uid_map != ((void *)0) || map_zero) {
  snprintf(map_path, 4096, "/proc/%ld/uid_map",
    (long) child_pid);
  if (map_zero) {
   snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getuid());
   uid_map = map_buf;
  }
  update_map(uid_map, map_path);
 }
 if (gid_map != ((void *)0) || map_zero) {
  proc_setgroups_write(child_pid, "deny");
  snprintf(map_path, 4096, "/proc/%ld/gid_map",
    (long) child_pid);
  if (map_zero) {
   snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getgid());
   gid_map = map_buf;
  }
  update_map(gid_map, map_path);
 }
 close(args.pipe_fd[1]);
 if (waitpid(child_pid, ((void *)0), 0) == -1)
  do { perror("waitpid"); exit(1); } while (0);
 if (verbose)
  printf("%s: terminating\n", argv[0]);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: flags int

variable_types: opt int

variable_types: struct child_args { char **argv; int pipe_fd[2]; }


worker

decompiled_code:
void *__fastcall worker(void *a1)
{
if ( (int)setcon() < 0 )
thread_status = *__errno_location();
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
if ( (int)setcon() < 0 )
thread_status = *__errno_location();
return 0LL;
}

original_code:
static void *worker(void *datap)
{
 security_context_t security_context = datap;
 int rc;
 rc = setcon(security_context);
 if (rc < 0)
  thread_status = (*__errno_location ());
 return ((void *)0);
}

canonical_original_code:
static void *worker(void *datap)
{
 char *security_context = datap;
 int rc;
 rc = setcon(security_context);
 if (rc < 0)
  thread_status = (*__errno_location ());
 return ((void *)0);
}

variable_types:
datap
void *

variable_types: rc int

return_type: void *


decompiled_code:
__int64 __fastcall file_read_ulong(const char *a1, const char *a2)
{
int v3; // eax
int v4; // er13
int v5; // ebx
int v6; // er14
char *v7; // rbx
const char *v8; // rdi
__int64 v9; // rbx
int *v11; // rax
char *v12; // rax
char *v13; // rax
char *endptr; // [rsp+8h] [rbp-830h] BYREF
char buf[2088]; // [rsp+10h] [rbp-828h] BYREF

v3 = open(a1, 0);
if ( v3 < 0 )
{
v11 = __errno_location();
v12 = strerror(*v11);
printf("Couldn't open %s: %s\n", a1, v12);
v9 = -1LL;
}
else
{
v4 = v3;
v5 = read(v3, buf, 0x800uLL);
v6 = *__errno_location();
close(v4);
if ( v5 < 0 )
{
v13 = strerror(v6);
printf("Error reading %s: %s\n", a1, v13);
v9 = -1LL;
}
else if ( v5 == 2048 )
{
printf("%s is too large\n", a1);
v9 = -1LL;
}
else
{
buf[v5] = 0;
if ( a2 )
{
v7 = strstr(buf, a2);
if ( !v7 )
return -1LL;
v8 = &v7[strlen(a2)];
}
else
{
v8 = buf;
}
v9 = strtol(v8, &endptr, 0);
if ( ((*__ctype_b_loc())[*endptr] & 0x2000) == 0 )
{
printf("Couldn't parse %s value\n", a1);
v9 = -1LL;
}
}
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, const char *a2)
{
int v3; // eax
int v4; // er13
int v5; // ebx
int v6; // er14
char *v7; // rbx
const char *v8; // rdi
__int64 v9; // rbx
int *v11; // rax
char *v12; // rax
char *v13; // rax
char *endptr; // [rsp+8h] [rbp-830h] BYREF
char buf[2088]; // [rsp+10h] [rbp-828h] BYREF

v3 = open(a1, 0);
if ( v3 < 0 )
{
v11 = __errno_location();
v12 = strerror(*v11);
printf("Couldn't open %s: %s\n", a1, v12);
v9 = -1LL;
}
else
{
v4 = v3;
v5 = read(v3, buf, 0x800uLL);
v6 = *__errno_location();
close(v4);
if ( v5 < 0 )
{
v13 = strerror(v6);
printf("Error reading %s: %s\n", a1, v13);
v9 = -1LL;
}
else if ( v5 == 2048 )
{
printf("%s is too large\n", a1);
v9 = -1LL;
}
else
{
buf[v5] = 0;
if ( a2 )
{
v7 = strstr(buf, a2);
if ( !v7 )
return -1LL;
v8 = &v7[strlen(a2)];
}
else
{
v8 = buf;
}
v9 = strtol(v8, &endptr, 0);
if ( ((*__ctype_b_loc())[*endptr] & 0x2000) == 0 )
{
printf("Couldn't parse %s value\n", a1);
v9 = -1LL;
}
}
}
return v9;
}

original_code:
long file_read_ulong(char *file, const char *tag)
{
 int fd;
 char buf[2048];
 int len, readerr;
 char *p, *q;
 long val;
 fd = open(file, 00);
 if (fd < 0) {
  printf("Couldn't open %s: %s\n", file, strerror((*__errno_location ())));
  return -1;
 }
 len = read(fd, buf, sizeof(buf));
 readerr = (*__errno_location ());
 close(fd);
 if (len < 0) {
  printf("Error reading %s: %s\n", file, strerror(readerr));
  return -1;
 }
 if (len == sizeof(buf)) {
  printf("%s is too large\n", file);
  return -1;
 }
 buf[len] = '\0';
 if (tag) {
  p = strstr(buf, tag);
  if (!p)
   return -1;
  p += strlen(tag);
 } else
  p = buf;
 val = strtol(p, &q, 0);
 if (! ((*__ctype_b_loc ())[(int) ((*q))] & (unsigned short int) _ISspace)) {
  printf("Couldn't parse %s value\n", file);
  return -1;
 }
 return val;
}

canonical_original_code:
long file_read_ulong(char *file, char *tag)
{
 int fd;
 char buf[2048];
 int len; int readerr;
 char *p; char *q;
 long val;
 fd = open(file, 00);
 if (fd < 0) {
  printf("Couldn't open %s: %s\n", file, strerror((*__errno_location ())));
  return -1;
 }
 len = read(fd, buf, sizeof(buf));
 readerr = (*__errno_location ());
 close(fd);
 if (len < 0) {
  printf("Error reading %s: %s\n", file, strerror(readerr));
  return -1;
 }
 if (len == sizeof(buf)) {
  printf("%s is too large\n", file);
  return -1;
 }
 buf[len] = '\0';
 if (tag) {
  p = strstr(buf, tag);
  if (!p)
   return -1;
  p += strlen(tag);
 } else
  p = buf;
 val = strtol(p, &q, 0);
 if (! ((*__ctype_b_loc ())[(int) ((*q))] & (unsigned short) _ISspace)) {
  printf("Couldn't parse %s value\n", file);
  return -1;
 }
 return val;
}

variable_types:
file
char *

variable_types:
tag
char *

variable_types: fd int

variable_types:
buf
char [2048]

variable_types: len int

variable_types: readerr int

variable_types:
p
char *

variable_types:
q
char *

variable_types: val long


getdefaulthugesize

decompiled_code:
__int64 getdefaulthugesize()
{
return file_read_ulong("/proc/meminfo", "Hugepagesize:") << 10;
}

canonical_decompiled_code:
__int64 func8()
{
return func7("/proc/meminfo", "Hugepagesize:") << 10;
}

original_code:
long getdefaulthugesize(void)
{
 return file_read_ulong("/proc/meminfo", "Hugepagesize:") * 1024;
}

canonical_original_code:
long getdefaulthugesize()
{
 return file_read_ulong("/proc/meminfo", "Hugepagesize:") * 1024;
}


decompiled_code:
void __noreturn sig_io()
{
puts("wait_io: got sigio");
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("wait_io: got sigio");
exit(0);
}

original_code:
static void sig_io(int signo)
{
 printf("wait_io:  got sigio\n");
 exit(0);
}

variable_types: signo int


decompiled_code:
void *__fastcall test_thread(void *a1)
{
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-1008h] BYREF

v1 = sysconf(30);
if ( mprotect((void *)((unsigned __int64)&v3 & -v1), v1, 7) < 0 )
{
perror("mprotect");
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-1008h] BYREF

v1 = sysconf(30);
if ( mprotect((void *)((unsigned __int64)&v3 & -v1), v1, 7) < 0 )
{
perror("mprotect");
exit(1);
}
return 0LL;
}

original_code:
static void *test_thread(void *p)
{
 char buf[4096];
 int rc;
 void *ptr;
 long pagesize = sysconf(_SC_PAGESIZE);
 ptr = (void *) (((unsigned long) buf) & ~(pagesize - 1));
 rc = mprotect(ptr, pagesize, 0x1 | 0x2 | 0x4);
 if (rc < 0) {
  perror("mprotect");
  exit(1);
 }
 return ((void *)0);
}

variable_types:
p
void *

variable_types:
buf
char [4096]

variable_types:
ptr
void *

variable_types: pagesize long


decompiled_code:
__int64 cleanup_ib_rsrc()
{
ibv_destroy_qp();
ibv_destroy_srq();
ibv_destroy_cq();
ibv_dealloc_pd();
return ibv_close_device();
}

canonical_decompiled_code:
__int64 func7()
{
ibv_destroy_qp();
ibv_destroy_srq();
ibv_destroy_cq();
ibv_dealloc_pd();
return ibv_close_device();
}

original_code:
void cleanup_ib_rsrc()
{
 ibv_destroy_qp(qp);
 ibv_destroy_srq(srq);
 ibv_destroy_cq(cq);
 ibv_dealloc_pd(pd);
 ibv_close_device(context);
}


decompiled_code:
__int64 __fastcall init_rc_qp(char a1, __int16 a2)
{
int v3[36]; // [rsp+0h] [rbp-98h] BYREF

memset(v3, 0, sizeof(v3));
v3[0] = 1;
LOWORD(v3[30]) = a2;
BYTE1(v3[32]) = a1;
return ibv_modify_qp();
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1, __int16 a2)
{
int v3[36]; // [rsp+0h] [rbp-98h] BYREF

memset(v3, 0, sizeof(v3));
v3[0] = 1;
LOWORD(v3[30]) = a2;
BYTE1(v3[32]) = a1;
return ibv_modify_qp();
}

original_code:
int init_rc_qp(uint8_t port, uint16_t pkey_index)
{
 struct ibv_qp_attr attr = {
  .qp_state = IBV_QPS_INIT,
  .pkey_index = pkey_index,
  .port_num = port,
  .qp_access_flags = 0
 };
 return ibv_modify_qp(qp, &attr,
        IBV_QP_STATE |
        IBV_QP_PKEY_INDEX |
        IBV_QP_PORT |
        IBV_QP_ACCESS_FLAGS);
}

canonical_original_code:
int init_rc_qp(unsigned char port, unsigned short pkey_index)
{
 struct ibv_qp_attr attr = {
  .qp_state = IBV_QPS_INIT,
  .pkey_index = pkey_index,
  .port_num = port,
  .qp_access_flags = 0
 };
 return ibv_modify_qp(qp, &attr,
        IBV_QP_STATE |
        IBV_QP_PKEY_INDEX |
        IBV_QP_PORT |
        IBV_QP_ACCESS_FLAGS);
}

variable_types:
port
unsigned char

variable_types: struct ibv_qp_attr { enum ibv_qp_state { IBV_QPS_RESET=0, IBV_QPS_INIT=1, IBV_QPS_RTR=2, IBV_QPS_RTS=3, IBV_QPS_SQD=4, IBV_QPS_SQE=5, IBV_QPS_ERR=6, IBV_QPS_UNKNOWN=7 } qp_state; enum ibv_qp_state { IBV_QPS_RESET=0, IBV_QPS_INIT=1, IBV_QPS_RTR=2, IBV_QPS_RTS=3, IBV_QPS_SQD=4, IBV_QPS_SQE=5, IBV_QPS_ERR=6, IBV_QPS_UNKNOWN=7 } cur_qp_state; enum ibv_mtu { IBV_MTU_256=1, IBV_MTU_512=2, IBV_MTU_1024=3, IBV_MTU_2048=4, IBV_MTU_4096=5 } path_mtu; enum ibv_mig_state { IBV_MIG_MIGRATED=0, IBV_MIG_REARM=1, IBV_MIG_ARMED=2 } path_mig_state; unsigned int qkey; unsigned int rq_psn; unsigned int sq_psn; unsigned int dest_qp_num; unsigned int qp_access_flags; struct ibv_qp_cap { unsigned int max_send_wr; unsigned int max_recv_wr; unsigned int max_send_sge; unsigned int max_recv_sge; unsigned int max_inline_data; } cap; struct ibv_ah_attr { struct ibv_global_route { union ibv_gid { unsigned char raw[16]; struct { unsigned long long subnet_prefix; unsigned long long interface_id; } global; } dgid; unsigned int flow_label; unsigned char sgid_index; unsigned char hop_limit; unsigned char traffic_class; } grh; unsigned short dlid; unsigned char sl; unsigned char src_path_bits; unsigned char static_rate; unsigned char is_global; unsigned char port_num; } ah_attr; struct ibv_ah_attr alt_ah_attr; unsigned short pkey_index; unsigned short alt_pkey_index; unsigned char en_sqd_async_notify; unsigned char sq_draining; unsigned char max_rd_atomic; unsigned char max_dest_rd_atomic; unsigned char min_rnr_timer; unsigned char port_num; unsigned char timeout; unsigned char retry_cnt; unsigned char rnr_retry; unsigned char alt_port_num; unsigned char alt_timeout; unsigned int rate_limit; }

user_defined_types: struct ibv_qp_attr { enum ibv_qp_state qp_state; enum ibv_qp_state cur_qp_state; enum ibv_mtu path_mtu; enum ibv_mig_state path_mig_state; unsigned int qkey; unsigned int rq_psn; unsigned int sq_psn; unsigned int dest_qp_num; unsigned int qp_access_flags; struct ibv_qp_cap cap; struct ibv_ah_attr ah_attr; struct ibv_ah_attr alt_ah_attr; unsigned short pkey_index; unsigned short alt_pkey_index; unsigned char en_sqd_async_notify; unsigned char sq_draining; unsigned char max_rd_atomic; unsigned char max_dest_rd_atomic; unsigned char min_rnr_timer; unsigned char port_num; unsigned char timeout; unsigned char retry_cnt; unsigned char rnr_retry; unsigned char alt_port_num; unsigned char alt_timeout; unsigned int rate_limit; }

user_defined_types: struct ibv_ah_attr { struct ibv_global_route grh; unsigned short dlid; unsigned char sl; unsigned char src_path_bits; unsigned char static_rate; unsigned char is_global; unsigned char port_num; }

user_defined_types: struct ibv_global_route { union ibv_gid dgid; unsigned int flow_label; unsigned char sgid_index; unsigned char hop_limit; unsigned char traffic_class; }


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3; // bp
__int16 v4; // r12
int v5; // ebx

if ( argc != 4 )
{
puts("Please enter <ib device name> <port number> <pkey index>");
exit(1);
}
v3 = atoi(argv[2]);
v4 = atoi(argv[3]);
init_ib_rsrc((char *)argv[1]);
v5 = init_rc_qp(v3, v4);
cleanup_ib_rsrc();
exit(v5);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3; // bp
__int16 v4; // r12
int v5; // ebx

if ( argc != 4 )
{
puts("Please enter <ib device name> <port number> <pkey index>");
exit(1);
}
v3 = atoi(argv[2]);
v4 = atoi(argv[3]);
func8((char *)argv[1]);
v5 = func9(v3, v4);
func7();
exit(v5);
}

original_code:
int main(int argc, char *argv[])
{
 uint16_t pkey_index;
 uint8_t port;
 int ret;
 if (argc != 4) {
  printf("Please enter <ib device name> <port number> <pkey index>\n");
  exit(1);
 }
 port = atoi(argv[2]);
 pkey_index = atoi(argv[3]);
 init_ib_rsrc(argv[1]);
 ret = init_rc_qp(port, pkey_index);
 cleanup_ib_rsrc();
 exit(ret);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 unsigned short pkey_index;
 unsigned char port;
 int ret;
 if (argc != 4) {
  printf("Please enter <ib device name> <port number> <pkey index>\n");
  exit(1);
 }
 port = atoi(argv[2]);
 pkey_index = atoi(argv[3]);
 init_ib_rsrc(argv[1]);
 ret = init_rc_qp(port, pkey_index);
 cleanup_ib_rsrc();
 exit(ret);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int


getuid

decompiled_code:
void __noreturn getuid()
{
puts("Evil code ran!");
exit(1);
}

canonical_decompiled_code:
void __noreturn func6()
{
puts("Evil code ran!");
exit(1);
}

original_code:
uid_t getuid(void)
{
 printf("Evil code ran!\n");
 exit(1);
}

canonical_original_code:
unsigned int getuid()
{
 printf("Evil code ran!\n");
 exit(1);
}


decompiled_code:
__int64 __fastcall clone_fn(void *arg)
{
execv(*((const char **)arg + 3), (char *const *)arg + 3);
perror(*((const char **)arg + 3));
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *arg)
{
execv(*((const char **)arg + 3), (char *const *)arg + 3);
perror(*((const char **)arg + 3));
return 0xFFFFFFFFLL;
}

original_code:
int clone_fn(void *arg)
{
 char **argv = arg;
 execv(argv[3], argv + 3);
 perror(argv[3]);
 return -1;
}

variable_types:
arg
void *

variable_types:
argv
char **


repo: jsvgoncalves/sope

decompiled_code:
void __fastcall free_cmd_info_pip(__int64 a1)
{
void *v2; // rdi
__int64 v3; // rbx
void *v4; // rdi
void *v5; // rdi

v2 = **(void ***)a1;
if ( v2 )
{
v3 = 8LL;
do
{
free(v2);
v2 = *(void **)(*(_QWORD *)a1 + v3);
v3 += 8LL;
}
while ( v2 );
}
v4 = *(void **)(a1 + 8);
if ( v4 )
free(v4);
v5 = *(void **)(a1 + 16);
if ( v5 )
free(v5);
if ( *(_QWORD *)(a1 + 48) )
free_cmd_info_pip();
}

original_code:
void free_cmd_info_pip(Command_Info_Pip * cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  free(cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  free(cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  free(cmd_info->outfile);
 }
 if(cmd_info->next != ((void *)0)) {
  free_cmd_info_pip(cmd_info->next);
 }
}

canonical_original_code:
void free_cmd_info_pip(struct command_info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  free(cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  free(cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  free(cmd_info->outfile);
 }
 if(cmd_info->next != ((void *)0)) {
  free_cmd_info_pip(cmd_info->next);
 }
}

variable_types: struct command_info { char **arg; char *infile; char *outfile; int background; int fd_input[2]; int fd_output[2]; struct command_info *next; struct command_info *previous; } *

variable_types: i int


decompiled_code:
int __fastcall print_cmd_info_pip(__int64 a1)
{
const char *v1; // rsi
__int64 v2; // rbx
const char *v3; // rsi
const char *v4; // rsi
int result; // eax

v1 = **(const char ***)a1;
if ( v1 )
{
v2 = 8LL;
do
{
printf("%s ", v1);
fflush(stdout);
v1 = *(const char **)(*(_QWORD *)a1 + v2);
v2 += 8LL;
}
while ( v1 );
}
v3 = *(const char **)(a1 + 8);
if ( v3 )
printf("< %s ", v3);
v4 = *(const char **)(a1 + 16);
if ( v4 )
printf("> %s ", v4);
if ( *(_DWORD *)(a1 + 24) == 1 )
printf("& ");
if ( *(_QWORD *)(a1 + 48) || (result = putchar(10), *(_QWORD *)(a1 + 48)) )
{
printf("| ");
fflush(stdout);
result = print_cmd_info_pip(*(_QWORD *)(a1 + 48));
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
const char *v1; // rsi
__int64 v2; // rbx
const char *v3; // rsi
const char *v4; // rsi
int result; // eax

v1 = **(const char ***)a1;
if ( v1 )
{
v2 = 8LL;
do
{
printf("%s ", v1);
fflush(stdout);
v1 = *(const char **)(*(_QWORD *)a1 + v2);
v2 += 8LL;
}
while ( v1 );
}
v3 = *(const char **)(a1 + 8);
if ( v3 )
printf("< %s ", v3);
v4 = *(const char **)(a1 + 16);
if ( v4 )
printf("> %s ", v4);
if ( *(_DWORD *)(a1 + 24) == 1 )
printf("& ");
if ( *(_QWORD *)(a1 + 48) || (result = putchar(10), *(_QWORD *)(a1 + 48)) )
{
printf("| ");
fflush(stdout);
result = func1(*(_QWORD *)(a1 + 48));
}
return result;
}

original_code:
void print_cmd_info_pip(Command_Info_Pip *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  printf("%s ", cmd_info->arg[i]);
  fflush(stdout);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  printf("< %s ", cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  printf("> %s ", cmd_info->outfile);
 }
 if(cmd_info->background == 1) {
  printf("& ");
 }
 if(cmd_info->next == ((void *)0)) {
  printf("\n");
 }
 if(cmd_info->next != ((void *)0)) {
  printf("| ");
  fflush(stdout);
  print_cmd_info_pip(cmd_info->next);
 }
}

canonical_original_code:
void print_cmd_info_pip(struct command_info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  printf("%s ", cmd_info->arg[i]);
  fflush(stdout);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  printf("< %s ", cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  printf("> %s ", cmd_info->outfile);
 }
 if(cmd_info->background == 1) {
  printf("& ");
 }
 if(cmd_info->next == ((void *)0)) {
  printf("\n");
 }
 if(cmd_info->next != ((void *)0)) {
  printf("| ");
  fflush(stdout);
  print_cmd_info_pip(cmd_info->next);
 }
}

user_defined_types: struct command_info { char **arg; char *infile; char *outfile; int background; int fd_input[2]; int fd_output[2]; struct command_info *next; struct command_info *previous; }


decompiled_code:
__int64 __fastcall create_pipes(__int64 a1)
{
unsigned int v1; // ebp
int v2; // edx
int v3; // eax
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

v1 = 0;
if ( *(_QWORD *)(a1 + 48) )
{
v1 = pipe(pipedes);
if ( v1 == -1 )
{
perror("pipe");
}
else
{
v2 = pipedes[0];
*(_DWORD *)(a1 + 36) = pipedes[0];
v3 = pipedes[1];
*(_DWORD *)(a1 + 40) = pipedes[1];
*(_DWORD *)(*(_QWORD *)(a1 + 48) + 28LL) = v2;
*(_DWORD *)(*(_QWORD *)(a1 + 48) + 32LL) = v3;
v1 = create_pipes(*(_QWORD *)(a1 + 48));
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // ebp
int v2; // edx
int v3; // eax
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

v1 = 0;
if ( *(_QWORD *)(a1 + 48) )
{
v1 = pipe(pipedes);
if ( v1 == -1 )
{
perror("pipe");
}
else
{
v2 = pipedes[0];
*(_DWORD *)(a1 + 36) = pipedes[0];
v3 = pipedes[1];
*(_DWORD *)(a1 + 40) = pipedes[1];
*(_DWORD *)(*(_QWORD *)(a1 + 48) + 28LL) = v2;
*(_DWORD *)(*(_QWORD *)(a1 + 48) + 32LL) = v3;
v1 = func2(*(_QWORD *)(a1 + 48));
}
}
return v1;
}

original_code:
int create_pipes(Command_Info_Pip * cmd_info) {
 if(cmd_info->next) {
  int fd[2];
  if(pipe(fd) == -1) {
   perror("pipe");
   return -1;
  }
  cmd_info->fd_output[0] = fd[0];
  cmd_info->fd_output[1] = fd[1];
  cmd_info->next->fd_input[0] = fd[0];
  cmd_info->next->fd_input[1] = fd[1];
  return create_pipes(cmd_info->next);
 }
 else {
  return 0;
 }
}

canonical_original_code:
int create_pipes(struct command_info *cmd_info) {
 if(cmd_info->next) {
  int fd[2];
  if(pipe(fd) == -1) {
   perror("pipe");
   return -1;
  }
  cmd_info->fd_output[0] = fd[0];
  cmd_info->fd_output[1] = fd[1];
  cmd_info->next->fd_input[0] = fd[0];
  cmd_info->next->fd_input[1] = fd[1];
  return create_pipes(cmd_info->next);
 }
 else {
  return 0;
 }
}

variable_types:
fd
int [2]


decompiled_code:
__int64 __fastcall close_pipes(__int64 a1)
{
unsigned int v1; // ebx

v1 = 0;
if ( *(_QWORD *)(a1 + 48) )
{
v1 = close(*(_DWORD *)(a1 + 40));
if ( v1 == -1 )
{
perror("close: pipe: write");
}
else
{
v1 = close(*(_DWORD *)(*(_QWORD *)(a1 + 48) + 28LL));
if ( v1 == -1 )
perror("close: pipe: read");
else
v1 = close_pipes(*(_QWORD *)(a1 + 48));
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // ebx

v1 = 0;
if ( *(_QWORD *)(a1 + 48) )
{
v1 = close(*(_DWORD *)(a1 + 40));
if ( v1 == -1 )
{
perror("close: pipe: write");
}
else
{
v1 = close(*(_DWORD *)(*(_QWORD *)(a1 + 48) + 28LL));
if ( v1 == -1 )
perror("close: pipe: read");
else
v1 = func3(*(_QWORD *)(a1 + 48));
}
}
return v1;
}

original_code:
int close_pipes(Command_Info_Pip * cmd_info) {
 if(cmd_info->next) {
  if(close(cmd_info->fd_output[1]) == -1) {
   perror("close: pipe: write");
   return -1;
  }
  if(close(cmd_info->next->fd_input[0]) == -1) {
   perror("close: pipe: read");
   return -1;
  }
  return close_pipes(cmd_info->next);
 }
 return 0;
}

canonical_original_code:
int close_pipes(struct command_info *cmd_info) {
 if(cmd_info->next) {
  if(close(cmd_info->fd_output[1]) == -1) {
   perror("close: pipe: write");
   return -1;
  }
  if(close(cmd_info->next->fd_input[0]) == -1) {
   perror("close: pipe: read");
   return -1;
  }
  return close_pipes(cmd_info->next);
 }
 return 0;
}


original_code:
void free_cmd_info(Command_Info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  free(cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  free(cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  free(cmd_info->outfile);
 }
}

canonical_original_code:
void free_cmd_info(struct Command_Info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  free(cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  free(cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  free(cmd_info->outfile);
 }
}

variable_types: struct Command_Info { char **arg; char *infile; char *outfile; int background; } *


decompiled_code:
int __fastcall print_cmd_info(__int64 a1)
{
const char *v1; // rsi
__int64 v2; // rbx
const char *v3; // rsi
const char *v4; // rsi

v1 = **(const char ***)a1;
if ( v1 )
{
v2 = 8LL;
do
{
printf("%s ", v1);
v1 = *(const char **)(*(_QWORD *)a1 + v2);
v2 += 8LL;
}
while ( v1 );
}
v3 = *(const char **)(a1 + 8);
if ( v3 )
printf("< %s ", v3);
v4 = *(const char **)(a1 + 16);
if ( v4 )
printf("> %s ", v4);
if ( *(_DWORD *)(a1 + 24) == 1 )
putchar(38);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
const char *v1; // rsi
__int64 v2; // rbx
const char *v3; // rsi
const char *v4; // rsi

v1 = **(const char ***)a1;
if ( v1 )
{
v2 = 8LL;
do
{
printf("%s ", v1);
v1 = *(const char **)(*(_QWORD *)a1 + v2);
v2 += 8LL;
}
while ( v1 );
}
v3 = *(const char **)(a1 + 8);
if ( v3 )
printf("< %s ", v3);
v4 = *(const char **)(a1 + 16);
if ( v4 )
printf("> %s ", v4);
if ( *(_DWORD *)(a1 + 24) == 1 )
putchar(38);
return putchar(10);
}

original_code:
void print_cmd_info(Command_Info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  printf("%s ", cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  printf("< %s ", cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  printf("> %s ", cmd_info->outfile);
 }
 if(cmd_info->background == 1) {
  printf("&");
 }
 printf("\n");
}

canonical_original_code:
void print_cmd_info(struct Command_Info *cmd_info) {
 int i = 0;
 while(cmd_info->arg[i] != ((void *)0)) {
  printf("%s ", cmd_info->arg[i]);
  i++;
 }
 if(cmd_info->infile != ((void *)0)) {
  printf("< %s ", cmd_info->infile);
 }
 if(cmd_info->outfile != ((void *)0)) {
  printf("> %s ", cmd_info->outfile);
 }
 if(cmd_info->background == 1) {
  printf("&");
 }
 printf("\n");
}

user_defined_types: struct Command_Info { char **arg; char *infile; char *outfile; int background; }


decompiled_code:
__int64 __fastcall check_valid_cmd(__int64 a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax
bool v4; // zf
__int64 v5; // rax
int v6; // er11
int v7; // er8
int v8; // ebx

if ( a2 > 1 )
{
v2 = 2LL;
while ( 1 )
{
if ( !strcmp(*(const char **)(a1 + 8 * v2 - 8), ">") || !strcmp(*(const char **)(a1 + 8 * v2 - 8), "<") )
{
if ( a2 == (_DWORD)v2 )
return 0LL;
result = strcmp(*(const char **)(a1 + 8 * v2), ">") != 0;
if ( !(_DWORD)result )
return result;
result = strcmp(*(const char **)(a1 + 8 * v2), "<") != 0;
if ( !(_DWORD)result )
return result;
v4 = strcmp(*(const char **)(a1 + 8 * v2), "&") == 0;
result = !v4;
if ( v4 )
return result;
}
if ( ++v2 == a2 + 1 )
{
v5 = 0LL;
v6 = 0;
v7 = 0;
v8 = 0;
goto LABEL_19;
}
}
}
if ( a2 != 1 )
{
v6 = 0;
v7 = 0;
v8 = 0;
return (unsigned int)(a2 - v8 - v7 - v6);
}
v5 = 0LL;
v6 = 0;
v7 = 0;
v8 = 0;
do
{
LABEL_19:
if ( !strcmp(*(const char **)(a1 + 8 * v5), ">") )
{
++v8;
}
else if ( !strcmp(*(const char **)(a1 + 8 * v5), "<") )
{
++v7;
}
else
{
v6 += strcmp(*(const char **)(a1 + 8 * v5), "&") == 0;
}
++v5;
}
while ( a2 > (int)v5 );
if ( v8 > 1 || v7 > 1 || v6 > 1 )
return 0LL;
if ( v6 != 1 )
return (unsigned int)(a2 - v8 - v7 - v6);
result = 0LL;
if ( !strcmp(*(const char **)(a1 + 8LL * a2 - 8), "&") )
return (unsigned int)(a2 - v8 - v7 - v6);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax
bool v4; // zf
__int64 v5; // rax
int v6; // er11
int v7; // er8
int v8; // ebx

if ( a2 > 1 )
{
v2 = 2LL;
while ( 1 )
{
if ( !strcmp(*(const char **)(a1 + 8 * v2 - 8), ">") || !strcmp(*(const char **)(a1 + 8 * v2 - 8), "<") )
{
if ( a2 == (_DWORD)v2 )
return 0LL;
result = strcmp(*(const char **)(a1 + 8 * v2), ">") != 0;
if ( !(_DWORD)result )
return result;
result = strcmp(*(const char **)(a1 + 8 * v2), "<") != 0;
if ( !(_DWORD)result )
return result;
v4 = strcmp(*(const char **)(a1 + 8 * v2), "&") == 0;
result = !v4;
if ( v4 )
return result;
}
if ( ++v2 == a2 + 1 )
{
v5 = 0LL;
v6 = 0;
v7 = 0;
v8 = 0;
goto LABEL_19;
}
}
}
if ( a2 != 1 )
{
v6 = 0;
v7 = 0;
v8 = 0;
return (unsigned int)(a2 - v8 - v7 - v6);
}
v5 = 0LL;
v6 = 0;
v7 = 0;
v8 = 0;
do
{
LABEL_19:
if ( !strcmp(*(const char **)(a1 + 8 * v5), ">") )
{
++v8;
}
else if ( !strcmp(*(const char **)(a1 + 8 * v5), "<") )
{
++v7;
}
else
{
v6 += strcmp(*(const char **)(a1 + 8 * v5), "&") == 0;
}
++v5;
}
while ( a2 > (int)v5 );
if ( v8 > 1 || v7 > 1 || v6 > 1 )
return 0LL;
if ( v6 != 1 )
return (unsigned int)(a2 - v8 - v7 - v6);
result = 0LL;
if ( !strcmp(*(const char **)(a1 + 8LL * a2 - 8), "&") )
return (unsigned int)(a2 - v8 - v7 - v6);
return result;
}

original_code:
int check_valid_cmd(char ** tokens, int num_tokens) {
 int next_position;
 char * next_token;
 int i;
 for(i = 1; i < num_tokens; i++) {
  if((strcmp(tokens[i], ">") == 0) ||
    (strcmp(tokens[i], "<") == 0)) {
   next_position = i + 1;
   if(next_position == num_tokens) {
    return 0;
   }
   else {
    next_token = tokens[next_position];
    if((strcmp(next_token, ">") == 0 ) ||
      (strcmp(next_token, "<") == 0) ||
      (strcmp(next_token, "&") == 0)) {
     return 0;
    }
   }
  }
 }
 int great = 0, less = 0, and = 0;
 for(i = 0; i < num_tokens; i++) {
  if(strcmp(tokens[i], ">") == 0) {
   great++;
  }
  else if(strcmp(tokens[i], "<") == 0) {
   less++;
  }
  else if(strcmp(tokens[i], "&") == 0) {
   and++;
  }
 }
 if(great > 1) {
  return 0;
 }
 if(less > 1) {
  return 0;
 }
 if(and > 1) {
  return 0;
 }
 else if(and == 1) {
  if(strcmp(tokens[num_tokens - 1], "&") != 0) {
   return 0;
  }
 }
 return num_tokens - great - less - and;
}

canonical_original_code:
int check_valid_cmd(char **tokens, int num_tokens) {
 int next_position;
 char *next_token;
 int i;
 for(i = 1; i < num_tokens; i++) {
  if((strcmp(tokens[i], ">") == 0) ||
    (strcmp(tokens[i], "<") == 0)) {
   next_position = i + 1;
   if(next_position == num_tokens) {
    return 0;
   }
   else {
    next_token = tokens[next_position];
    if((strcmp(next_token, ">") == 0 ) ||
      (strcmp(next_token, "<") == 0) ||
      (strcmp(next_token, "&") == 0)) {
     return 0;
    }
   }
  }
 }
 int great = 0; int less = 0; int and = 0;
 for(i = 0; i < num_tokens; i++) {
  if(strcmp(tokens[i], ">") == 0) {
   great++;
  }
  else if(strcmp(tokens[i], "<") == 0) {
   less++;
  }
  else if(strcmp(tokens[i], "&") == 0) {
   and++;
  }
 }
 if(great > 1) {
  return 0;
 }
 if(less > 1) {
  return 0;
 }
 if(and > 1) {
  return 0;
 }
 else if(and == 1) {
  if(strcmp(tokens[num_tokens - 1], "&") != 0) {
   return 0;
  }
 }
 return num_tokens - great - less - and;
}

variable_types:
tokens
char **

variable_types: great int

variable_types: less int

variable_types: and int


original_code:
int parse_cmd(char *cmd_line, Command_Info *cmd_info) {
 int num_tokens;
 char ** tokens = tokenize(cmd_line, ' ', &num_tokens);
 if(tokens == ((void *)0)) {
  return -1;
 }
 int num_good_tokens = check_valid_cmd(tokens, num_tokens);
 if(num_good_tokens == 0) {
  return -1;
 }
 cmd_info->arg = (char **) malloc((num_good_tokens + 1) * sizeof(char *));
 int i;
 for(i = 0; i < (num_good_tokens + 1); i++) {
  cmd_info->arg[i] = ((void *)0);
 }
 cmd_info->infile = ((void *)0);
 cmd_info->outfile = ((void *)0);
 cmd_info->background = 0;
 int j = 0;
 for(i = 0; i < num_tokens; i++) {
  if(strcmp(tokens[i], ">") == 0) {
   i++;
   cmd_info->outfile = tokens[i];
  }
  else if(strcmp(tokens[i], "<") == 0) {
   i++;
   cmd_info->infile = tokens[i];
  }
  else if(strcmp(tokens[i], "&") == 0) {
   cmd_info->background = 1;
  }
  else {
   cmd_info->arg[j] = tokens[i];
   j++;
  }
 }
 free(tokens);
 return num_good_tokens;
}

canonical_original_code:
int parse_cmd(char *cmd_line, struct Command_Info *cmd_info) {
 int num_tokens;
 char **tokens = tokenize(cmd_line, ' ', &num_tokens);
 if(tokens == ((void *)0)) {
  return -1;
 }
 int num_good_tokens = check_valid_cmd(tokens, num_tokens);
 if(num_good_tokens == 0) {
  return -1;
 }
 cmd_info->arg = (char **) malloc((num_good_tokens + 1) * sizeof(char *));
 int i;
 for(i = 0; i < (num_good_tokens + 1); i++) {
  cmd_info->arg[i] = ((void *)0);
 }
 cmd_info->infile = ((void *)0);
 cmd_info->outfile = ((void *)0);
 cmd_info->background = 0;
 int j = 0;
 for(i = 0; i < num_tokens; i++) {
  if(strcmp(tokens[i], ">") == 0) {
   i++;
   cmd_info->outfile = tokens[i];
  }
  else if(strcmp(tokens[i], "<") == 0) {
   i++;
   cmd_info->infile = tokens[i];
  }
  else if(strcmp(tokens[i], "&") == 0) {
   cmd_info->background = 1;
  }
  else {
   cmd_info->arg[j] = tokens[i];
   j++;
  }
 }
 free(tokens);
 return num_good_tokens;
}

variable_types: j int


decompiled_code:
__int64 __fastcall exec_simple_back(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // er12
const char *v5; // rdi
int v6; // edi
const char *v7; // rdi
int v8; // ebp

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
if ( signal(2, (__sighandler_t)((char *)exec_simple_back + 1)) == (__sighandler_t)-1LL )
{
puts("signal: child: sigint ignore failed");
exit(-1);
}
v5 = *(const char **)(a1 + 8);
if ( v5 )
{
v6 = open(v5, 0);
if ( v6 == -1 )
{
perror("open: file");
exit(2);
}
}
else
{
v6 = open("/dev/null", 0);
if ( v6 == -1 )
{
perror("open: /dev/null");
exit(2);
}
}
dup2(v6, 0);
v7 = *(const char **)(a1 + 16);
if ( v7 )
{
v8 = open(v7, 1089, 384LL);
if ( v8 == -1 )
{
perror("open: outfile");
exit(2);
}
}
else
{
v8 = open("bckgd_redir.txt", 1089, 384LL);
if ( v8 == -1 )
{
perror("open: bckgd_redir.txt");
exit(2);
}
}
dup2(v8, 1);
dup2(v8, 2);
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // er12
const char *v5; // rdi
int v6; // edi
const char *v7; // rdi
int v8; // ebp

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
if ( signal(2, (__sighandler_t)((char *)exec_simple_back + 1)) == (__sighandler_t)-1LL )
{
puts("signal: child: sigint ignore failed");
exit(-1);
}
v5 = *(const char **)(a1 + 8);
if ( v5 )
{
v6 = open(v5, 0);
if ( v6 == -1 )
{
perror("open: file");
exit(2);
}
}
else
{
v6 = open("/dev/null", 0);
if ( v6 == -1 )
{
perror("open: /dev/null");
exit(2);
}
}
dup2(v6, 0);
v7 = *(const char **)(a1 + 16);
if ( v7 )
{
v8 = open(v7, 1089, 384LL);
if ( v8 == -1 )
{
perror("open: outfile");
exit(2);
}
}
else
{
v8 = open("bckgd_redir.txt", 1089, 384LL);
if ( v8 == -1 )
{
perror("open: bckgd_redir.txt");
exit(2);
}
}
dup2(v8, 1);
dup2(v8, 2);
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

original_code:
int exec_simple_back(Command_Info * cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(signal(2, ((__sighandler_t) 1)) == ((__sighandler_t) -1)) {
     printf("signal: child: sigint ignore failed\n");
     exit(-1);
  }
  int fdi, fdo;
  if(cmd_info->infile == ((void *)0)) {
   if((fdi = open("/dev/null", 00)) == -1) {
    perror("open: /dev/null");
    exit(2);
   }
  }
  else {
   if((fdi = open(cmd_info->infile, 00)) == -1) {
    perror("open: file");
    exit(2);
   }
  }
  dup2(fdi, 0);
  if(cmd_info->outfile == ((void *)0)) {
   if((fdo = open("bckgd_redir.txt", 01 | 0100 | 02000, 0400 | 0200)) == -1) {
    perror("open: bckgd_redir.txt");
    exit(2);
   }
  }
  else {
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
     perror("open: outfile");
     exit(2);
   }
  }
  dup2(fdo, 1);
  dup2(fdo, 2);
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}

canonical_original_code:
int exec_simple_back(struct Command_Info *cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(signal(2, ((void (*)(int)) 1)) == ((__sighandler_t) -1)) {
     printf("signal: child: sigint ignore failed\n");
     exit(-1);
  }
  int fdi; int fdo;
  if(cmd_info->infile == ((void *)0)) {
   if((fdi = open("/dev/null", 00)) == -1) {
    perror("open: /dev/null");
    exit(2);
   }
  }
  else {
   if((fdi = open(cmd_info->infile, 00)) == -1) {
    perror("open: file");
    exit(2);
   }
  }
  dup2(fdi, 0);
  if(cmd_info->outfile == ((void *)0)) {
   if((fdo = open("bckgd_redir.txt", 01 | 0100 | 02000, 0400 | 0200)) == -1) {
    perror("open: bckgd_redir.txt");
    exit(2);
   }
  }
  else {
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
     perror("open: outfile");
     exit(2);
   }
  }
  dup2(fdo, 1);
  dup2(fdo, 2);
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}

variable_types: pid int

variable_types: fdi int

variable_types: fdo int


decompiled_code:
__int64 __fastcall exec_simple(const char ***a1)
{
__pid_t v1; // eax
unsigned int v2; // ebx

v1 = fork();
v2 = v1;
if ( v1 == -1 )
{
perror("fork");
}
else if ( !v1 && execvp(**a1, (char *const *)*a1) == -1 )
{
perror("execvp");
exit(1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char ***a1)
{
__pid_t v1; // eax
unsigned int v2; // ebx

v1 = fork();
v2 = v1;
if ( v1 == -1 )
{
perror("fork");
}
else if ( !v1 && execvp(**a1, (char *const *)*a1) == -1 )
{
perror("execvp");
exit(1);
}
return v2;
}

original_code:
int exec_simple(Command_Info * cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   exit(1);
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}

canonical_original_code:
int exec_simple(struct Command_Info *cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   exit(1);
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}


decompiled_code:
__int64 __fastcall exec_simple_redir(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // ebp
const char *v4; // rdi
int v5; // edi
const char *v6; // rdi
int v7; // edi

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
v4 = *(const char **)(a1 + 8);
if ( v4 )
{
v5 = open(v4, 0);
if ( v5 == -1 )
{
perror("open: infile");
exit(2);
}
dup2(v5, 0);
}
v6 = *(const char **)(a1 + 16);
if ( v6 )
{
v7 = open(v6, 1089, 384LL);
if ( v7 == -1 )
{
perror("open: outfile");
exit(2);
}
dup2(v7, 1);
}
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // ebp
const char *v4; // rdi
int v5; // edi
const char *v6; // rdi
int v7; // edi

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
v4 = *(const char **)(a1 + 8);
if ( v4 )
{
v5 = open(v4, 0);
if ( v5 == -1 )
{
perror("open: infile");
exit(2);
}
dup2(v5, 0);
}
v6 = *(const char **)(a1 + 16);
if ( v6 )
{
v7 = open(v6, 1089, 384LL);
if ( v7 == -1 )
{
perror("open: outfile");
exit(2);
}
dup2(v7, 1);
}
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

original_code:
int exec_simple_redir(Command_Info * cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  int fdi, fdo;
  if(cmd_info->infile != ((void *)0)) {
   if((fdi = open(cmd_info->infile, 00))==-1) {
    perror("open: infile");
    exit(2);
   }
   dup2(fdi, 0);
  }
  if(cmd_info->outfile != ((void *)0)) {
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
    perror("open: outfile");
    exit(2);
   }
   dup2(fdo, 1);
  }
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}

canonical_original_code:
int exec_simple_redir(struct Command_Info *cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  int fdi; int fdo;
  if(cmd_info->infile != ((void *)0)) {
   if((fdi = open(cmd_info->infile, 00))==-1) {
    perror("open: infile");
    exit(2);
   }
   dup2(fdi, 0);
  }
  if(cmd_info->outfile != ((void *)0)) {
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
    perror("open: outfile");
    exit(2);
   }
   dup2(fdo, 1);
  }
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}


decompiled_code:
int sig_handler_parent()
{
int result; // eax

result = kill(child_pid, 9);
if ( result == -1 )
result = puts("parent handler: failed to kill child");
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = kill(child_pid, 9);
if ( result == -1 )
result = puts("parent handler: failed to kill child");
return result;
}

original_code:
void sig_handler_parent(int sig) {
 if(kill(child_pid, 9) == -1) {
  printf("parent handler: failed to kill child\n");
 }
}

variable_types: sig int


decompiled_code:
__int64 __fastcall exec_simple_sig(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // ebp
const char *v5; // rdi
int v6; // edi
const char *v7; // rdi
int v8; // edi

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
if ( signal(2, (__sighandler_t)((char *)sig_handler_parent + 1)) == (__sighandler_t)-1LL )
{
puts("signal: child: sigint ignore failed");
exit(-1);
}
v5 = *(const char **)(a1 + 8);
if ( v5 )
{
v6 = open(v5, 0);
if ( v6 == -1 )
{
perror("open: infile");
exit(2);
}
dup2(v6, 0);
}
v7 = *(const char **)(a1 + 16);
if ( v7 )
{
v8 = open(v7, 1089, 384LL);
if ( v8 == -1 )
{
perror("open: outfile");
exit(2);
}
dup2(v8, 1);
}
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__pid_t v2; // eax
unsigned int v3; // ebp
const char *v5; // rdi
int v6; // edi
const char *v7; // rdi
int v8; // edi

v2 = fork();
v3 = v2;
if ( v2 == -1 )
{
perror("fork");
}
else if ( !v2 )
{
if ( signal(2, (__sighandler_t)((char *)sig_handler_parent + 1)) == (__sighandler_t)-1LL )
{
puts("signal: child: sigint ignore failed");
exit(-1);
}
v5 = *(const char **)(a1 + 8);
if ( v5 )
{
v6 = open(v5, 0);
if ( v6 == -1 )
{
perror("open: infile");
exit(2);
}
dup2(v6, 0);
}
v7 = *(const char **)(a1 + 16);
if ( v7 )
{
v8 = open(v7, 1089, 384LL);
if ( v8 == -1 )
{
perror("open: outfile");
exit(2);
}
dup2(v8, 1);
}
if ( execvp(**(const char ***)a1, *(char *const **)a1) == -1 )
{
perror("execvp");
v3 = 1;
}
}
return v3;
}

original_code:
int exec_simple_sig(Command_Info * cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(signal(2, ((__sighandler_t) 1)) == ((__sighandler_t) -1)) {
     printf("signal: child: sigint ignore failed\n");
     exit(-1);
  }
  int fdi, fdo;
  if(cmd_info->infile != ((void *)0)){
   if((fdi = open(cmd_info->infile, 00)) == -1) {
     perror("open: infile");
     exit(2);
   }
   dup2(fdi, 0);
  }
  if(cmd_info->outfile != ((void *)0)){
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
     perror("open: outfile");
     exit(2);
   }
   dup2(fdo, 1);
  }
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}

canonical_original_code:
int exec_simple_sig(struct Command_Info *cmd_info) {
 int pid = fork();
 if(pid == -1) {
  perror("fork");
  return -1;
 }
 else if(pid == 0) {
  if(signal(2, ((void (*)(int)) 1)) == ((__sighandler_t) -1)) {
     printf("signal: child: sigint ignore failed\n");
     exit(-1);
  }
  int fdi; int fdo;
  if(cmd_info->infile != ((void *)0)){
   if((fdi = open(cmd_info->infile, 00)) == -1) {
     perror("open: infile");
     exit(2);
   }
   dup2(fdi, 0);
  }
  if(cmd_info->outfile != ((void *)0)){
   if((fdo = open(cmd_info->outfile, 01 | 0100 | 02000, 0400 | 0200)) == -1) {
     perror("open: outfile");
     exit(2);
   }
   dup2(fdo, 1);
  }
  if(execvp(cmd_info->arg[0], cmd_info->arg) == -1) {
   perror("execvp");
   return 1;
  }
  else {
   return 0;
  }
 }
 else {
  return pid;
 }
}


acceptNewClient

repo: jtdreisb/networks

decompiled_code:
void __fastcall acceptNewClient(int fd)
{
int *v1; // rbx
int v2; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
v1 = (int *)newClient();
v2 = accept(fd, &addr, &addr_len);
*v1 = v2;
if ( v2 < 0 )
{
perror("acceptNewClient:accept");
free(v1);
}
else if ( v2 > *(_DWORD *)(clientList + 16LL) )
{
*(_DWORD *)(clientList + 16LL) = v2;
}
}

canonical_decompiled_code:
void __fastcall func2(int fd)
{
int *v1; // rbx
int v2; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
v1 = (int *)newClient();
v2 = accept(fd, &addr, &addr_len);
*v1 = v2;
if ( v2 < 0 )
{
perror("acceptNewClient:accept");
free(v1);
}
else if ( v2 > *(_DWORD *)(clientList + 16LL) )
{
*(_DWORD *)(clientList + 16LL) = v2;
}
}

original_code:
void acceptNewClient(int serverSocket)
{
 struct sockaddr_in newClientAddress;
 socklen_t sockLength = sizeof(newClientAddress);
 struct ClientNode *client = newClient();
 if ((client->clientSocket = accept(serverSocket, (struct sockaddr *) &newClientAddress, &sockLength)) < 0) {
  perror("acceptNewClient:accept");
  free(client);
  return;
 }
 if (clientList->maxSocket < client->clientSocket) {
  clientList->maxSocket = client->clientSocket;
 }
}

canonical_original_code:
void acceptNewClient(int serverSocket)
{
 struct sockaddr_in newClientAddress;
 unsigned int sockLength = sizeof(newClientAddress);
 struct ClientNode *client = newClient();
 if ((client->clientSocket = accept(serverSocket, (struct sockaddr *) &newClientAddress, &sockLength)) < 0) {
  perror("acceptNewClient:accept");
  free(client);
  return;
 }
 if (clientList->maxSocket < client->clientSocket) {
  clientList->maxSocket = client->clientSocket;
 }
}

variable_types: serverSocket int

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

variable_types:
sockLength
unsigned int

variable_types: struct ClientNode { int clientSocket; char *clientName; struct ClientNode *nextClient; struct ChatHeader { unsigned int sequenceNumber; unsigned short checksum; unsigned char flag; } *packetData; long packetLength; unsigned int sequenceNumber; } *

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

user_defined_types: struct ClientNode { int clientSocket; char *clientName; struct ClientNode *nextClient; struct ChatHeader *packetData; long packetLength; unsigned int sequenceNumber; }

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


respondToClient

decompiled_code:
void __fastcall respondToClient(unsigned int *a1, unsigned __int8 a2, const void *a3, size_t a4)
{
__int64 v6; // r15
__int64 v7; // rbx
unsigned __int16 v8; // ax
_WORD *v9; // rbx

v6 = *((_QWORD *)a1 + 3);
v7 = (unsigned int)_bswap_32(*(_DWORD *)v6);
v8 = _bswap_16(*(_WORD *)(v6 + 4));
v9 = makePacket(((unsigned __int64)v8 << 32) | v7 | ((unsigned __int64)a2 << 48), a3, a4);
if ( sendErr(*a1, v9, a4 + 8, 0LL) < 0 )
perror("respondToClient:send");
free(v9);
}

canonical_decompiled_code:
void __fastcall func3(unsigned int *a1, unsigned __int8 a2, const void *a3, size_t a4)
{
__int64 v6; // r15
__int64 v7; // rbx
unsigned __int16 v8; // ax
_WORD *v9; // rbx

v6 = *((_QWORD *)a1 + 3);
v7 = (unsigned int)func0(*(_DWORD *)v6);
v8 = func1(*(_WORD *)(v6 + 4));
v9 = makePacket(((unsigned __int64)v8 << 32) | v7 | ((unsigned __int64)a2 << 48), a3, a4);
if ( sendErr(*a1, v9, a4 + 8, 0LL) < 0 )
perror("respondToClient:send");
free(v9);
}

original_code:
void respondToClient(struct ClientNode *client, HeaderFlag flag, uint8_t *data, ssize_t dataLen)
{
 struct ChatHeader responseHeader;
 responseHeader.sequenceNumber = ntohl(client->packetData->sequenceNumber);
 responseHeader.checksum = ntohs(client->packetData->checksum);
 responseHeader.flag = flag;
 uint8_t *packet = makePacket(responseHeader, data, dataLen);
 ssize_t packetLength = sizeof(struct ChatHeader) + dataLen;
 ssize_t bytesSent;
 if ((bytesSent = sendErr(client->clientSocket, packet, packetLength, 0)) < 0) {
  perror("respondToClient:send");
 }
 free(packet);
}

canonical_original_code:
void respondToClient(struct ClientNode *client, enum HeaderFlag flag, unsigned char *data, long dataLen)
{
 struct ChatHeader responseHeader;
 responseHeader.sequenceNumber = ntohl(client->packetData->sequenceNumber);
 responseHeader.checksum = ntohs(client->packetData->checksum);
 responseHeader.flag = flag;
 unsigned char *packet = makePacket(responseHeader, data, dataLen);
 long packetLength = sizeof(struct ChatHeader) + dataLen;
 long bytesSent;
 if ((bytesSent = sendErr(client->clientSocket, packet, packetLength, 0)) < 0) {
  perror("respondToClient:send");
 }
 free(packet);
}

variable_types: enum HeaderFlag { FLAG_INIT_REQ=1, FLAG_INIT_ACK=2, FLAG_INIT_ERR=3, FLAG_MSG_REQ=6, FLAG_MSG_ERR=7, FLAG_MSG_ACK=255, FLAG_EXIT_REQ=8, FLAG_EXIT_ACK=9, FLAG_LIST_REQ=10, FLAG_LIST_RESP=11, FLAG_HNDL_REQ=12, FLAG_HNDL_RESP=13 }

variable_types:
data
unsigned char *

variable_types: dataLen long

variable_types: struct ChatHeader { unsigned int sequenceNumber; unsigned short checksum; unsigned char flag; }

variable_types:
packet
unsigned char *

variable_types: packetLength long

variable_types: bytesSent long


getClientCount

decompiled_code:
void __fastcall getClientCount(unsigned int *a1)
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = clientCount();
respondToClient(a1, 0xBu, v1, 4uLL);
}

canonical_decompiled_code:
void __fastcall func4(unsigned int *a1)
{
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = clientCount();
func3(a1, 0xBu, v1, 4uLL);
}

original_code:
void getClientCount(struct ClientNode *client)
{
 uint32_t numClients = clientCount();
 respondToClient(client, FLAG_LIST_RESP, (uint8_t *)&numClients, sizeof(uint32_t));
}

canonical_original_code:
void getClientCount(struct ClientNode *client)
{
 unsigned int numClients = clientCount();
 respondToClient(client, FLAG_LIST_RESP, (unsigned char *)&numClients, sizeof(unsigned int));
}

variable_types:
numClients
unsigned int

user_defined_types: struct ChatHeader { unsigned int sequenceNumber; unsigned short checksum; unsigned char flag; }


clientNameRequest

decompiled_code:
void __fastcall clientNameRequest(__int64 a1)
{
int v1; // edx
__int64 v2; // rbx
int v3; // eax
const char *v4; // rbp
size_t v5; // rax
_BYTE *v6; // r13
const char *v7; // rbp
size_t v8; // rax
size_t v9; // rax

v1 = _bswap_32(*(_DWORD *)(*(_QWORD *)(a1 + 24) + 8LL));
v2 = *clientList;
v3 = 0;
while ( 1 )
{
if ( v2 )
{
v4 = *(const char **)(v2 + 8);
if ( v4 )
break;
}
LABEL_2:
v2 = *(_QWORD *)(v2 + 16);
}
if ( v3 != v1 )
{
++v3;
goto LABEL_2;
}
v5 = strlen(*(const char **)(v2 + 8));
v6 = malloc(v5 + 1);
*v6 = strlen(v4);
v7 = *(const char **)(v2 + 8);
v8 = strlen(v7);
memcpy(v6 + 1, v7, v8);
v9 = strlen(*(const char **)(v2 + 8));
respondToClient((unsigned int *)a1, 0xDu, v6, v9 + 1);
free(v6);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
int v1; // edx
__int64 v2; // rbx
int v3; // eax
const char *v4; // rbp
size_t v5; // rax
_BYTE *v6; // r13
const char *v7; // rbp
size_t v8; // rax
size_t v9; // rax

v1 = func0(*(_DWORD *)(*(_QWORD *)(a1 + 24) + 8LL));
v2 = *clientList;
v3 = 0;
while ( 1 )
{
if ( v2 )
{
v4 = *(const char **)(v2 + 8);
if ( v4 )
break;
}
LABEL_2:
v2 = *(_QWORD *)(v2 + 16);
}
if ( v3 != v1 )
{
++v3;
goto LABEL_2;
}
v5 = strlen(*(const char **)(v2 + 8));
v6 = malloc(v5 + 1);
*v6 = strlen(v4);
v7 = *(const char **)(v2 + 8);
v8 = strlen(v7);
memcpy(v6 + 1, v7, v8);
v9 = strlen(*(const char **)(v2 + 8));
func3((unsigned int *)a1, 0xDu, v6, v9 + 1);
free(v6);
}

original_code:
void clientNameRequest(struct ClientNode *client)
{
 uint8_t *index = (uint8_t *)client->packetData;
 index += sizeof(struct ChatHeader);
 uint32_t clientIndex = *(uint32_t *)index;
 clientIndex = ntohl(clientIndex);
 struct ClientNode *node = clientList->firstClient;
 int i;
 for (i = 0;; node = node->nextClient) {
  if (node && node->clientName != ((void *)0)) {
   if (i == clientIndex) {
    break;
   }
   i++;
  }
 }
 uint8_t *payload = malloc(1 + strlen(node->clientName));
 payload[0] = strlen(node->clientName);
 memcpy(payload+1, node->clientName, strlen(node->clientName));
 respondToClient(client, FLAG_HNDL_RESP, payload, 1 + strlen(node->clientName));
 free(payload);
}

canonical_original_code:
void clientNameRequest(struct ClientNode *client)
{
 unsigned char *index = (uint8_t *)client->packetData;
 index += sizeof(struct ChatHeader);
 unsigned int clientIndex = *(uint32_t *)index;
 clientIndex = ntohl(clientIndex);
 struct ClientNode *node = clientList->firstClient;
 int i;
 for (i = 0;; node = node->nextClient) {
  if (node && node->clientName != ((void *)0)) {
   if (i == clientIndex) {
    break;
   }
   i++;
  }
 }
 unsigned char *payload = malloc(1 + strlen(node->clientName));
 payload[0] = strlen(node->clientName);
 memcpy(payload+1, node->clientName, strlen(node->clientName));
 respondToClient(client, FLAG_HNDL_RESP, payload, 1 + strlen(node->clientName));
 free(payload);
}

variable_types:
index
unsigned char *

variable_types:
clientIndex
unsigned int

variable_types: i int

variable_types:
payload
unsigned char *


clientExit

decompiled_code:
__int64 __fastcall clientExit(unsigned int *a1)
{
respondToClient(a1, 9u, 0LL, 0LL);
return removeClient(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1)
{
func3(a1, 9u, 0LL, 0LL);
return removeClient(a1);
}

original_code:
void clientExit(struct ClientNode *client)
{
 respondToClient(client, FLAG_EXIT_ACK, ((void *)0), 0);
 removeClient(client);
}


forwardMessage

decompiled_code:
void __fastcall forwardMessage(__int64 a1)
{
_BYTE *v1; // rbp
unsigned __int8 v2; // dl
_BYTE *v3; // rbp
unsigned __int8 v4; // al
unsigned __int8 v5; // cl
unsigned int *v6; // rdx
char *v7; // rsi
unsigned int v8; // ecx
int v9; // er12
unsigned int *v10; // rax
__int64 v11; // [rsp+0h] [rbp-120h]
_DWORD v12[70]; // [rsp+8h] [rbp-118h] BYREF

v1 = *(_BYTE **)(a1 + 24);
v2 = v1[6];
if ( v2 == 6 )
{
v3 = v1 + 8;
}
else
{
if ( v2 != 0xFF )
{
fprintf(stderr, "ERROR: Unknown forwarding packet scheme %d\n", v2);
return;
}
v3 = v1 + 12;
}
v4 = *v3;
v5 = *v3;
v6 = (unsigned int *)(v3 + 1);
v7 = v3 + 1;
if ( *v3 > 7u )
{
*(__int64 *)((char *)&v11 + v4) = *(_QWORD *)((char *)v6 + v4 - 8);
v8 = ((unsigned int)v4 - 1) >> 3;
qmemcpy(v12, v7, 8LL * v8);
v7 += 8 * v8;
}
else if ( (v5 & 4) != 0 )
{
v7 = (char *)*v6;
v12[0] = *v6;
*(_DWORD *)((char *)&v12[-1] + v4) = *(unsigned int *)((char *)v6 + v4 - 4);
}
else if ( *v3 )
{
v7 = (char *)*(unsigned __int8 *)v6;
LOBYTE(v12[0]) = *(_BYTE *)v6;
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v12 + v4 - 2) = *(_WORD *)((char *)v6 + v4 - 2);
}
v9 = v4;
*((_BYTE *)v12 + v4) = 0;
v10 = (unsigned int *)clientNamed(v12, v7);
if ( v10 )
{
if ( sendErr(*v10, *(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), 0LL) < 0 )
perror("forwardMessage:send");
}
else
{
respondToClient((unsigned int *)a1, 7u, v3, v9 + 1);
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
_BYTE *v1; // rbp
unsigned __int8 v2; // dl
_BYTE *v3; // rbp
unsigned __int8 v4; // al
unsigned __int8 v5; // cl
unsigned int *v6; // rdx
char *v7; // rsi
unsigned int v8; // ecx
int v9; // er12
unsigned int *v10; // rax
__int64 v11; // [rsp+0h] [rbp-120h]
_DWORD v12[70]; // [rsp+8h] [rbp-118h] BYREF

v1 = *(_BYTE **)(a1 + 24);
v2 = v1[6];
if ( v2 == 6 )
{
v3 = v1 + 8;
}
else
{
if ( v2 != 0xFF )
{
fprintf(stderr, "ERROR: Unknown forwarding packet scheme %d\n", v2);
return;
}
v3 = v1 + 12;
}
v4 = *v3;
v5 = *v3;
v6 = (unsigned int *)(v3 + 1);
v7 = v3 + 1;
if ( *v3 > 7u )
{
*(__int64 *)((char *)&v11 + v4) = *(_QWORD *)((char *)v6 + v4 - 8);
v8 = ((unsigned int)v4 - 1) >> 3;
qmemcpy(v12, v7, 8LL * v8);
v7 += 8 * v8;
}
else if ( (v5 & 4) != 0 )
{
v7 = (char *)*v6;
v12[0] = *v6;
*(_DWORD *)((char *)&v12[-1] + v4) = *(unsigned int *)((char *)v6 + v4 - 4);
}
else if ( *v3 )
{
v7 = (char *)*(unsigned __int8 *)v6;
LOBYTE(v12[0]) = *(_BYTE *)v6;
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v12 + v4 - 2) = *(_WORD *)((char *)v6 + v4 - 2);
}
v9 = v4;
*((_BYTE *)v12 + v4) = 0;
v10 = (unsigned int *)clientNamed(v12, v7);
if ( v10 )
{
if ( sendErr(*v10, *(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), 0LL) < 0 )
perror("forwardMessage:send");
}
else
{
func3((unsigned int *)a1, 7u, v3, v9 + 1);
}
}

original_code:
void forwardMessage(struct ClientNode *client)
{
 char handle[255];
 uint8_t *index = (uint8_t *) client->packetData;
 switch (client->packetData->flag) {
  case FLAG_MSG_REQ:
   index += sizeof(struct ChatHeader);
   break;
  case FLAG_MSG_ACK:
   index += sizeof(struct ChatHeader) + sizeof(uint32_t);
   break;
  default:
   fprintf(stderr, "ERROR: Unknown forwarding packet scheme %d\n", client->packetData->flag);
   return;
   break;
 }
 uint8_t handleSize = *index;
 memcpy(handle, index+1, handleSize);
 handle[handleSize] = '\0';
 struct ClientNode *destClient = clientNamed(handle);
  if (destClient == ((void *)0)) {
   respondToClient(client, FLAG_MSG_ERR, index, handleSize+1);
   return;
  }
 ssize_t bytesSent;
 if ((bytesSent = sendErr(destClient->clientSocket, client->packetData, client->packetLength, 0)) < 0) {
  perror("forwardMessage:send");
 }
}

canonical_original_code:
void forwardMessage(struct ClientNode *client)
{
 char handle[255];
 unsigned char *index = (uint8_t *) client->packetData;
 switch (client->packetData->flag) {
  case FLAG_MSG_REQ:
   index += sizeof(struct ChatHeader);
   break;
  case FLAG_MSG_ACK:
   index += sizeof(struct ChatHeader) + sizeof(unsigned int);
   break;
  default:
   fprintf(stderr, "ERROR: Unknown forwarding packet scheme %d\n", client->packetData->flag);
   return;
   break;
 }
 unsigned char handleSize = *index;
 memcpy(handle, index+1, handleSize);
 handle[handleSize] = '\0';
 struct ClientNode *destClient = clientNamed(handle);
  if (destClient == ((void *)0)) {
   respondToClient(client, FLAG_MSG_ERR, index, handleSize+1);
   return;
  }
 long bytesSent;
 if ((bytesSent = sendErr(destClient->clientSocket, client->packetData, client->packetLength, 0)) < 0) {
  perror("forwardMessage:send");
 }
}

variable_types:
handle
char [255]

variable_types:
handleSize
unsigned char


handleClient

decompiled_code:
void __fastcall handleClient(unsigned int *a1)
{
void *v1; // rax
void *v2; // rbx
__int64 v3; // rax
unsigned __int8 v4; // al

v1 = malloc(0x800uLL);
if ( v1 )
{
v2 = v1;
v3 = recvErr(*a1, v1, 2047LL, 0LL);
if ( v3 < 0 )
{
perror("handleClient:recv");
free(v2);
}
else
{
if ( v3 )
{
*((_QWORD *)a1 + 4) = v3;
*((_QWORD *)a1 + 3) = v2;
if ( !(unsigned __int16)in_cksum(v2, (unsigned int)v3) )
{
v4 = *(_BYTE *)(*((_QWORD *)a1 + 3) + 6LL);
if ( v4 > 0xCu )
{
if ( v4 == 0xFF )
LABEL_15:
forwardMessage((__int64)a1);
}
else if ( v4 )
{
switch ( v4 )
{
case 1u:
registerHandle((__int64)a1);
break;
case 6u:
goto LABEL_15;
case 8u:
clientExit(a1);
break;
case 0xAu:
getClientCount(a1);
break;
case 0xCu:
clientNameRequest((__int64)a1);
break;
default:
break;
}
}
}
*((_QWORD *)a1 + 3) = 0LL;
*((_QWORD *)a1 + 4) = 0LL;
}
else
{
removeClient(a1);
}
free(v2);
}
}
else
{
perror("handleClient:malloc");
}
}

canonical_decompiled_code:
void __fastcall func9(unsigned int *a1)
{
void *v1; // rax
void *v2; // rbx
__int64 v3; // rax
unsigned __int8 v4; // al

v1 = malloc(0x800uLL);
if ( v1 )
{
v2 = v1;
v3 = recvErr(*a1, v1, 2047LL, 0LL);
if ( v3 < 0 )
{
perror("handleClient:recv");
free(v2);
}
else
{
if ( v3 )
{
*((_QWORD *)a1 + 4) = v3;
*((_QWORD *)a1 + 3) = v2;
if ( !(unsigned __int16)in_cksum(v2, (unsigned int)v3) )
{
v4 = *(_BYTE *)(*((_QWORD *)a1 + 3) + 6LL);
if ( v4 > 0xCu )
{
if ( v4 == 0xFF )
LABEL_15:
func8((__int64)a1);
}
else if ( v4 )
{
switch ( v4 )
{
case 1u:
func6((__int64)a1);
break;
case 6u:
goto LABEL_15;
case 8u:
func7(a1);
break;
case 0xAu:
func4(a1);
break;
case 0xCu:
func5((__int64)a1);
break;
default:
break;
}
}
}
*((_QWORD *)a1 + 3) = 0LL;
*((_QWORD *)a1 + 4) = 0LL;
}
else
{
removeClient(a1);
}
free(v2);
}
}
else
{
perror("handleClient:malloc");
}
}

original_code:
void handleClient(struct ClientNode *client)
{
 ssize_t bytesRcvd;
 uint8_t *buf = malloc(2048);
 if (buf == ((void *)0)) {
  perror("handleClient:malloc");
  return;
 }
 if ((bytesRcvd = recvErr(client->clientSocket, buf, 2048 -1, 0)) < 0) {
  perror("handleClient:recv");
  free(buf);
  return;
 }
 if (bytesRcvd == 0) {
  removeClient(client);
 }
 else {
  client->packetLength = bytesRcvd;
  client->packetData = (struct ChatHeader *)buf;
  if (in_cksum((uint16_t *)client->packetData, client->packetLength) == 0) {
   switch (client->packetData->flag) {
    case FLAG_INIT_REQ:
     registerHandle(client);
     break;
    case FLAG_MSG_REQ:
    case FLAG_MSG_ACK:
     forwardMessage(client);
     break;
    case FLAG_EXIT_REQ:
     clientExit(client);
     break;
    case FLAG_LIST_REQ:
     getClientCount(client);
     break;
    case FLAG_HNDL_REQ:
     clientNameRequest(client);
     break;
    case FLAG_INIT_ACK:
    case FLAG_INIT_ERR:
    case FLAG_MSG_ERR:
    case FLAG_EXIT_ACK:
    case FLAG_LIST_RESP:
    case FLAG_HNDL_RESP:
    default:
     break;
   }
  }
  client->packetData = ((void *)0);
  client->packetLength = 0;
 }
 free(buf);
}

canonical_original_code:
void handleClient(struct ClientNode *client)
{
 long bytesRcvd;
 unsigned char *buf = malloc(2048);
 if (buf == ((void *)0)) {
  perror("handleClient:malloc");
  return;
 }
 if ((bytesRcvd = recvErr(client->clientSocket, buf, 2048 -1, 0)) < 0) {
  perror("handleClient:recv");
  free(buf);
  return;
 }
 if (bytesRcvd == 0) {
  removeClient(client);
 }
 else {
  client->packetLength = bytesRcvd;
  client->packetData = (struct ChatHeader *)buf;
  if (in_cksum((unsigned short *)client->packetData, client->packetLength) == 0) {
   switch (client->packetData->flag) {
    case FLAG_INIT_REQ:
     registerHandle(client);
     break;
    case FLAG_MSG_REQ:
    case FLAG_MSG_ACK:
     forwardMessage(client);
     break;
    case FLAG_EXIT_REQ:
     clientExit(client);
     break;
    case FLAG_LIST_REQ:
     getClientCount(client);
     break;
    case FLAG_HNDL_REQ:
     clientNameRequest(client);
     break;
    case FLAG_INIT_ACK:
    case FLAG_INIT_ERR:
    case FLAG_MSG_ERR:
    case FLAG_EXIT_ACK:
    case FLAG_LIST_RESP:
    case FLAG_HNDL_RESP:
    default:
     break;
   }
  }
  client->packetData = ((void *)0);
  client->packetLength = 0;
 }
 free(buf);
}

variable_types: bytesRcvd long

variable_types:
buf
unsigned char *


setActiveClientsForSelect

decompiled_code:
__int64 __fastcall setActiveClientsForSelect(__int64 a1)
{
__int64 result; // rax
__int64 i; // rsi
int v3; // eax

result = clientList;
for ( i = *clientList; i; i = *(_QWORD *)(i + 16) )
{
v3 = *(_DWORD *)i + 63;
if ( *(int *)i >= 0 )
v3 = *(_DWORD *)i;
result = v3 >> 6;
*(_QWORD *)(a1 + 8 * result) |= 1LL << (*(_DWORD *)i % 64);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 result; // rax
__int64 i; // rsi
int v3; // eax

result = clientList;
for ( i = *clientList; i; i = *(_QWORD *)(i + 16) )
{
v3 = *(_DWORD *)i + 63;
if ( *(int *)i >= 0 )
v3 = *(_DWORD *)i;
result = v3 >> 6;
*(_QWORD *)(a1 + 8 * result) |= 1LL << (*(_DWORD *)i % 64);
}
return result;
}

original_code:
void setActiveClientsForSelect(fd_set *fdSet)
{
 struct ClientNode *activeClient;
 activeClient = clientList->firstClient;
 while(activeClient != ((void *)0)) {
  ((void) (((fdSet)->__fds_bits)[((activeClient->clientSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((activeClient->clientSocket) % (8 * (int) sizeof (__fd_mask)))))));
  activeClient = activeClient->nextClient;
 }
}

canonical_original_code:
void setActiveClientsForSelect(struct fd_set *fdSet)
{
 struct ClientNode *activeClient;
 activeClient = clientList->firstClient;
 while(activeClient != ((void *)0)) {
  ((void) (((fdSet)->__fds_bits)[((activeClient->clientSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((activeClient->clientSocket) % (8 * (int) sizeof (__fd_mask)))))));
  activeClient = activeClient->nextClient;
 }
}

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

variable_types: struct ClientNode { int clientSocket; char *clientName; struct ClientNode *nextClient; struct ChatHeader { unsigned int sequenceNumber; unsigned short checksum; unsigned char flag; } *packetData; long packetLength; unsigned int sequenceNumber; } *


checkActiveClientsAfterSelect

decompiled_code:
void __fastcall checkActiveClientsAfterSelect(__int64 a1)
{
__int64 i; // rbx
int v2; // eax

for ( i = *clientList; i; i = *(_QWORD *)(i + 16) )
{
v2 = *(_DWORD *)i + 63;
if ( *(int *)i >= 0 )
v2 = *(_DWORD *)i;
if ( (*(_QWORD *)(a1 + 8LL * (v2 >> 6)) & (1LL << (*(_DWORD *)i % 64))) != 0 )
handleClient((unsigned int *)i);
}
}

original_code:
void checkActiveClientsAfterSelect(fd_set *fdSet)
{
 struct ClientNode *activeClient = clientList->firstClient;
 while(activeClient != ((void *)0)) {
  if(((((fdSet)->__fds_bits)[((activeClient->clientSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((activeClient->clientSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   handleClient(activeClient);
  }
  activeClient = activeClient->nextClient;
 }
}

canonical_original_code:
void checkActiveClientsAfterSelect(struct fd_set *fdSet)
{
 struct ClientNode *activeClient = clientList->firstClient;
 while(activeClient != ((void *)0)) {
  if(((((fdSet)->__fds_bits)[((activeClient->clientSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((activeClient->clientSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   handleClient(activeClient);
  }
  activeClient = activeClient->nextClient;
 }
}


registerHandle

decompiled_code:
void __fastcall registerHandle(__int64 a1)
{
__int64 v1; // r13
unsigned __int8 v2; // al
__int64 v3; // r12
_DWORD *v4; // rbp
__int64 v5; // rax
char *v6; // rsi
__int64 v7; // rcx

v1 = *(_QWORD *)(a1 + 24);
v2 = *(_BYTE *)(v1 + 8);
v3 = v2;
if ( *(_QWORD *)(a1 + 32) < (unsigned __int64)v2 + 9 )
goto LABEL_15;
v4 = malloc(v2 + 1LL);
if ( !v4 )
{
perror("registerHandle:malloc");
exit(1);
}
v5 = v1 + 9;
v6 = (char *)(v1 + 9);
if ( (unsigned int)v3 >= 8 )
{
*(_QWORD *)((char *)v4 + v3 - 8) = *(_QWORD *)(v5 + v3 - 8);
v7 = (unsigned int)(v3 - 1) >> 3;
qmemcpy(v4, v6, 8 * v7);
v6 += 8 * v7;
}
else if ( (v3 & 4) != 0 )
{
*v4 = *(_DWORD *)(v1 + 9);
*(_DWORD *)((char *)v4 + v3 - 4) = *(_DWORD *)(v5 + v3 - 4);
}
else if ( (_DWORD)v3 )
{
*(_BYTE *)v4 = *(_BYTE *)(v1 + 9);
if ( (v3 & 2) != 0 )
*(_WORD *)((char *)v4 + v3 - 2) = *(_WORD *)(v5 + v3 - 2);
}
*((_BYTE *)v4 + v3) = 0;
if ( clientNamed(v4, v6) && a1 != clientNamed(v4, v6) )
{
LABEL_15:
respondToClient((unsigned int *)a1, 3u, 0LL, 0LL);
removeClient(a1);
}
else
{
*(_QWORD *)(a1 + 8) = v4;
respondToClient((unsigned int *)a1, 2u, 0LL, 0LL);
}
}

original_code:
void registerHandle(struct ClientNode *client)
{
 uint8_t *buf = (uint8_t *)client->packetData;
 uint8_t handleLen = *(buf + sizeof(struct ChatHeader));
 if (client->packetLength < (sizeof(struct ChatHeader) + handleLen + 1)) {
  respondToClient(client, FLAG_INIT_ERR, ((void *)0), 0);
  removeClient(client);
  return;
 }
 char *clientHandle = malloc(handleLen+1);
 if (clientHandle == ((void *)0)) {
  perror("registerHandle:malloc");
  exit(1);
 }
 memcpy(clientHandle, buf + sizeof(struct ChatHeader) + 1, handleLen);
 clientHandle[handleLen] = '\0';
 if (clientNamed(clientHandle) && (clientNamed(clientHandle) != client)) {
  respondToClient(client, FLAG_INIT_ERR, ((void *)0), 0);
  removeClient(client);
  return;
 }
 client->clientName = clientHandle;
 respondToClient(client, FLAG_INIT_ACK, ((void *)0), 0);
}

canonical_original_code:
void registerHandle(struct ClientNode *client)
{
 unsigned char *buf = (uint8_t *)client->packetData;
 unsigned char handleLen = *(buf + sizeof(struct ChatHeader));
 if (client->packetLength < (sizeof(struct ChatHeader) + handleLen + 1)) {
  respondToClient(client, FLAG_INIT_ERR, ((void *)0), 0);
  removeClient(client);
  return;
 }
 char *clientHandle = malloc(handleLen+1);
 if (clientHandle == ((void *)0)) {
  perror("registerHandle:malloc");
  exit(1);
 }
 memcpy(clientHandle, buf + sizeof(struct ChatHeader) + 1, handleLen);
 clientHandle[handleLen] = '\0';
 if (clientNamed(clientHandle) && (clientNamed(clientHandle) != client)) {
  respondToClient(client, FLAG_INIT_ERR, ((void *)0), 0);
  removeClient(client);
  return;
 }
 client->clientName = clientHandle;
 respondToClient(client, FLAG_INIT_ACK, ((void *)0), 0);
}

variable_types:
handleLen
unsigned char

variable_types:
clientHandle
char *