repo: warfish/ftrace

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

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

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

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

original_code:
static struct ftrace_task* find_task(int pid)
{
 struct ftrace_task* task = g_task_list;
 while(task) {
  if(task->pid == pid) {
   return task;
  }
  task = task->next;
 }
 return ((void *)0);
}

canonical_original_code:
static struct ftrace_task *find_task(int pid)
{
 struct ftrace_task *task = g_task_list;
 while(task) {
  if(task->pid == pid) {
   return task;
  }
  task = task->next;
 }
 return ((void *)0);
}

variable_types: pid int

variable_types: struct ftrace_task { int pid; int status; int in_syscall; struct ftrace_task *next; } *


decompiled_code:
const char *__fastcall syscall_name(int a1)
{
const char *result; // rax

if ( a1 > 18 )
{
result = "rename";
if ( a1 != 82 )
{
result = "unknown syscall";
if ( a1 == 85 )
result = "creat";
}
}
else
{
result = "unknown syscall";
if ( a1 >= 0 )
{
switch ( a1 )
{
case 0:
result = "read";
break;
case 1:
result = "write";
break;
case 2:
result = "open";
break;
case 3:
result = "close";
break;
case 17:
result = "pread";
break;
case 18:
result = "pwrite";
break;
default:
result = "unknown syscall";
break;
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(int a1)
{
const char *result; // rax

if ( a1 > 18 )
{
result = "rename";
if ( a1 != 82 )
{
result = "unknown syscall";
if ( a1 == 85 )
result = "creat";
}
}
else
{
result = "unknown syscall";
if ( a1 >= 0 )
{
switch ( a1 )
{
case 0:
result = "read";
break;
case 1:
result = "write";
break;
case 2:
result = "open";
break;
case 3:
result = "close";
break;
case 17:
result = "pread";
break;
case 18:
result = "pwrite";
break;
default:
result = "unknown syscall";
break;
}
}
}
return result;
}

original_code:
static const char* syscall_name(int syscall)
{
 switch(syscall) {
 case 2: return "open";
 case 3: return "close";
 case 85: return "creat";
 case 82: return "rename";
 case 0: return "read";
 case 17: return "pread";
 case 1: return "write";
 case 18: return "pwrite";
 default: return "unknown syscall";
 };
}

canonical_original_code:
static char *syscall_name(int syscall)
{
 switch(syscall) {
 case 2: return "open";
 case 3: return "close";
 case 85: return "creat";
 case 82: return "rename";
 case 0: return "read";
 case 17: return "pread";
 case 1: return "write";
 case 18: return "pwrite";
 default: return "unknown syscall";
 };
}

variable_types: syscall int

return_type: char *


usage

decompiled_code:
int usage()
{
return puts(
"ftrace - Trace process file i/o:\n"
"usage: ftrace command [args...]\n"
" command [args...]\t- run command with optional arguments.");
}

canonical_decompiled_code:
int func2()
{
return puts(
"ftrace - Trace process file i/o:\n"
"usage: ftrace command [args...]\n"
" command [args...]\t- run command with optional arguments.");
}

original_code:
static void usage()
{
 printf( "ftrace - Trace process file i/o:\n"
  "usage: ftrace command [args...]\n"
  " command [args...]	- run command with optional arguments.\n"
 );
}

return_type: void


decompiled_code:
__int64 __fastcall setup_ptrace_options(unsigned int a1)
{
return ptrace(PTRACE_SETOPTIONS, a1, 10LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return ptrace(PTRACE_SETOPTIONS, a1, 10LL);
}

original_code:
static void setup_ptrace_options(int pid)
{
 ptrace(PTRACE_SETOPTIONS, pid, PTRACE_O_TRACEFORK | PTRACE_O_TRACECLONE);
}


decompiled_code:
__int64 __fastcall add_task(int a1)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = calloc(0x18uLL, 1uLL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = g_task_list;
*(_DWORD *)v1 = a1;
g_task_list = (__int64)v1;
v1[2] = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = calloc(0x18uLL, 1uLL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = g_task_list;
*(_DWORD *)v1 = a1;
g_task_list = (__int64)v1;
v1[2] = v2;
return 0LL;
}

original_code:
static int add_task(int pid)
{
 struct ftrace_task* task = malloc(sizeof(struct ftrace_task));
 if(!task) {
  return -1;
 }
 memset(task, 0, sizeof(*task));
 task->pid = pid;
 task->next = g_task_list;
 g_task_list = task;
 return 0;
}

canonical_original_code:
static int add_task(int pid)
{
 struct ftrace_task *task = malloc(sizeof(struct ftrace_task));
 if(!task) {
  return -1;
 }
 memset(task, 0, sizeof(*task));
 task->pid = pid;
 task->next = g_task_list;
 g_task_list = task;
 return 0;
}

return_type: int

user_defined_types: struct ftrace_task { int pid; int status; int in_syscall; struct ftrace_task *next; }


decompiled_code:
void __fastcall remove_task(_QWORD *a1)
{
__int64 v1; // rax

v1 = g_task_list;
if ( (_QWORD *)g_task_list == a1 )
{
g_task_list = a1[2];
free(a1);
}
else
{
while ( 1 )
{
if ( !v1 )
_assert_fail("(prev && (prev->next == task))", "ftrace.c", 0x52u, "remove_task");
if ( *(_QWORD **)(v1 + 16) == a1 )
break;
v1 = *(_QWORD *)(v1 + 16);
}
*(_QWORD *)(v1 + 16) = a1[2];
free(a1);
}
}

canonical_decompiled_code:
void __fastcall func5(_QWORD *a1)
{
__int64 v1; // rax

v1 = g_task_list;
if ( (_QWORD *)g_task_list == a1 )
{
g_task_list = a1[2];
free(a1);
}
else
{
while ( 1 )
{
if ( !v1 )
_assert_fail("(prev && (prev->next == task))", "ftrace.c", 0x52u, "remove_task");
if ( *(_QWORD **)(v1 + 16) == a1 )
break;
v1 = *(_QWORD *)(v1 + 16);
}
*(_QWORD *)(v1 + 16) = a1[2];
free(a1);
}
}

original_code:
static void remove_task(struct ftrace_task* task)
{
 if(task == g_task_list) {
  g_task_list = task->next;
 } else {
  struct ftrace_task* prev = g_task_list;
  while(prev && (prev->next != task)) {
   prev = prev->next;
  }
  ((void) sizeof (((prev && (prev->next == task))) ? 1 : 0), __extension__ ({ if ((prev && (prev->next == task))) ; else __assert_fail ("(prev && (prev->next == task))", "ftrace.c", 82, __extension__ __PRETTY_FUNCTION__); }));
  prev->next = task->next;
 }
 free(task);
}

canonical_original_code:
static void remove_task(struct ftrace_task *task)
{
 if(task == g_task_list) {
  g_task_list = task->next;
 } else {
  struct ftrace_task *prev = g_task_list;
  while(prev && (prev->next != task)) {
   prev = prev->next;
  }
  ((void) sizeof (((prev && (prev->next == task))) ? 1 : 0), __extension__ ({ if ((prev && (prev->next == task))) ; else __assert_fail ("(prev && (prev->next == task))", "ftrace.c", 82, __extension__ __PRETTY_FUNCTION__); }));
  prev->next = task->next;
 }
 free(task);
}


decompiled_code:
int __fastcall trace_task(unsigned int *a1)
{
int v1; // eax
unsigned int v2; // ebp
const char *v3; // rax
int result; // eax
const char *v5; // rax
int v6; // eax
__int128 v7; // [rsp+0h] [rbp-1018h] BYREF
char v8[4080]; // [rsp+10h] [rbp-1008h] BYREF

v1 = task_syscall_num();
v2 = v1;
if ( a1[2] )
{
v3 = syscall_name(v1);
result = fprintf(stderr, "task %d is returning from syscall \"%s\" (%d)\n", *a1, v3, v2);
a1[2] = 0;
}
else
{
v5 = syscall_name(v1);
result = fprintf(stderr, "task %d is entering syscall \"%s\" (%d)\n", *a1, v5, v2);
if ( v2 == 2 )
{
memset(v8, 0, sizeof(v8));
v7 = 0LL;
v6 = task_syscall_p1(a1);
task_peekstr(a1, v6, &v7, 4096LL, 0.0);
result = fprintf(stderr, "task %d opens file \"%s\"\n", *a1, (const char *)&v7);
}
a1[2] = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(unsigned int *a1)
{
int v1; // eax
unsigned int v2; // ebp
const char *v3; // rax
int result; // eax
const char *v5; // rax
int v6; // eax
__int128 v7; // [rsp+0h] [rbp-1018h] BYREF
char v8[4080]; // [rsp+10h] [rbp-1008h] BYREF

v1 = task_syscall_num();
v2 = v1;
if ( a1[2] )
{
v3 = func1(v1);
result = fprintf(stderr, "task %d is returning from syscall \"%s\" (%d)\n", *a1, v3, v2);
a1[2] = 0;
}
else
{
v5 = func1(v1);
result = fprintf(stderr, "task %d is entering syscall \"%s\" (%d)\n", *a1, v5, v2);
if ( v2 == 2 )
{
memset(v8, 0, sizeof(v8));
v7 = 0LL;
v6 = task_syscall_p1(a1);
task_peekstr(a1, v6, &v7, 4096LL, 0.0);
result = fprintf(stderr, "task %d opens file \"%s\"\n", *a1, (const char *)&v7);
}
a1[2] = 1;
}
return result;
}

original_code:
static void trace_task(struct ftrace_task* task)
{
 int sysnum = task_syscall_num(task);
 if(task->in_syscall) {
  fprintf(stderr, "task %d is returning from syscall \"%s\" (%d)\n", task->pid, syscall_name(sysnum), sysnum);
  task->in_syscall = 0;
 } else {
  fprintf(stderr, "task %d is entering syscall \"%s\" (%d)\n", task->pid, syscall_name(sysnum), sysnum);
  if(sysnum == 2) {
   char name[4096] = {0};
   long int nameaddr = task_syscall_p1(task);
   unsigned namelen = task_peekstr(task, nameaddr, name, 4096);
   fprintf(stderr, "task %d opens file \"%s\"\n", task->pid, name);
  }
  task->in_syscall = 1;
 }
}

canonical_original_code:
static void trace_task(struct ftrace_task *task)
{
 int sysnum = task_syscall_num(task);
 if(task->in_syscall) {
  fprintf(stderr, "task %d is returning from syscall \"%s\" (%d)\n", task->pid, syscall_name(sysnum), sysnum);
  task->in_syscall = 0;
 } else {
  fprintf(stderr, "task %d is entering syscall \"%s\" (%d)\n", task->pid, syscall_name(sysnum), sysnum);
  if(sysnum == 2) {
   char name[4096] = {0};
   long nameaddr = task_syscall_p1(task);
   unsigned int namelen = task_peekstr(task, nameaddr, name, 4096);
   fprintf(stderr, "task %d opens file \"%s\"\n", task->pid, name);
  }
  task->in_syscall = 1;
 }
}

variable_types: sysnum int

variable_types:
name
char [4096]

variable_types: nameaddr long

variable_types:
namelen
unsigned int


start

decompiled_code:
__int64 start()
{
int v0; // eax
unsigned int v1; // ebp
unsigned int *v2; // r12
int stat_loc; // [rsp+8h] [rbp-20h] BYREF
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, &stat_loc, 0);
v1 = v0;
if ( v0 == -1 )
break;
v2 = (unsigned int *)find_task(v0);
if ( !v2 )
_assert_fail("(task != ((void *)0))", "ftrace.c", 0xB7u, "start");
if ( (stat_loc & 0x7F) != 0 && (char)((stat_loc & 0x7F) + 1) <= 1 )
{
if ( (_BYTE)stat_loc == 127 )
{
if ( BYTE1(stat_loc) == 5 || BYTE1(stat_loc) == 19 )
{
if ( stat_loc >> 16 )
{
if ( (unsigned int)((stat_loc >> 16) - 1) <= 2 )
{
ptrace(PTRACE_GETEVENTMSG, *v2, 0LL, v5);
add_task(v5[0]);
fprintf(stderr, "task %d cloned, new pid is %d\n", *v2, v5[0]);
}
}
else
{
trace_task(v2);
}
}
}
else
{
fprintf(stderr, "Unknown task %d status\n", v1);
}
}
else
{
fprintf(stderr, "task %d terminated\n", v1);
remove_task(v2);
}
ptrace(PTRACE_SYSCALL, v1, 0LL, 0LL);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
unsigned int v1; // ebp
unsigned int *v2; // r12
int stat_loc; // [rsp+8h] [rbp-20h] BYREF
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, &stat_loc, 0);
v1 = v0;
if ( v0 == -1 )
break;
v2 = (unsigned int *)func0(v0);
if ( !v2 )
_assert_fail("(task != ((void *)0))", "ftrace.c", 0xB7u, "start");
if ( (stat_loc & 0x7F) != 0 && (char)((stat_loc & 0x7F) + 1) <= 1 )
{
if ( (_BYTE)stat_loc == 127 )
{
if ( BYTE1(stat_loc) == 5 || BYTE1(stat_loc) == 19 )
{
if ( stat_loc >> 16 )
{
if ( (unsigned int)((stat_loc >> 16) - 1) <= 2 )
{
ptrace(PTRACE_GETEVENTMSG, *v2, 0LL, v5);
func4(v5[0]);
fprintf(stderr, "task %d cloned, new pid is %d\n", *v2, v5[0]);
}
}
else
{
func6(v2);
}
}
}
else
{
fprintf(stderr, "Unknown task %d status\n", v1);
}
}
else
{
fprintf(stderr, "task %d terminated\n", v1);
func5(v2);
}
ptrace(PTRACE_SYSCALL, v1, 0LL, 0LL);
}
return 0LL;
}

original_code:
static ftrace_result_t start(void)
{
 int status;
 int pid;
 int signo;
 while((pid = waitpid(-1, &status, 0)) != -1) {
  signo = 0;
  struct ftrace_task* task = find_task(pid);
  ((void) sizeof (((task != ((void *)0))) ? 1 : 0), __extension__ ({ if ((task != ((void *)0))) ; else __assert_fail ("(task != ((void *)0))", "ftrace.c", 183, __extension__ __PRETTY_FUNCTION__); }));
  if((((status) & 0x7f) == 0) || (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
   fprintf(stderr, "task %d terminated\n", pid);
   remove_task(task);
  } else if((((status) & 0xff) == 0x7f)) {
   signo = (((status) & 0xff00) >> 8);
   if((signo == 5) || (signo == 19)) {
    switch(( (status) >> 16 )) {
    case PTRACE_EVENT_FORK:
    case PTRACE_EVENT_VFORK:
    case PTRACE_EVENT_CLONE: {
     int new_pid;
     ptrace(PTRACE_GETEVENTMSG, task->pid, ((void *)0), &new_pid);
     add_task(new_pid);
     fprintf(stderr, "task %d cloned, new pid is %d\n", task->pid, new_pid);
     break;
    }
    case 0: {
     trace_task(task);
    }
    default: break;
    };
   }
  } else {
   fprintf(stderr, "Unknown task %d status\n", pid);
  }
  ptrace(PTRACE_SYSCALL, pid, ((void *)0), ((void *)0));
 }
 return FTRACE_SUCCESS;
}

canonical_original_code:
static enum ftrace_result_t start()
{
 int status;
 int pid;
 int signo;
 while((pid = waitpid(-1, &status, 0)) != -1) {
  signo = 0;
  struct ftrace_task *task = find_task(pid);
  ((void) sizeof (((task != ((void *)0))) ? 1 : 0), __extension__ ({ if ((task != ((void *)0))) ; else __assert_fail ("(task != ((void *)0))", "ftrace.c", 183, __extension__ __PRETTY_FUNCTION__); }));
  if((((status) & 0x7f) == 0) || (((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
   fprintf(stderr, "task %d terminated\n", pid);
   remove_task(task);
  } else if((((status) & 0xff) == 0x7f)) {
   signo = (((status) & 0xff00) >> 8);
   if((signo == 5) || (signo == 19)) {
    switch(( (status) >> 16 )) {
    case PTRACE_EVENT_FORK:
    case PTRACE_EVENT_VFORK:
    case PTRACE_EVENT_CLONE: {
     int new_pid;
     ptrace(PTRACE_GETEVENTMSG, task->pid, ((void *)0), &new_pid);
     add_task(new_pid);
     fprintf(stderr, "task %d cloned, new pid is %d\n", task->pid, new_pid);
     break;
    }
    case 0: {
     trace_task(task);
    }
    default: break;
    };
   }
  } else {
   fprintf(stderr, "Unknown task %d status\n", pid);
  }
  ptrace(PTRACE_SYSCALL, pid, ((void *)0), ((void *)0));
 }
 return FTRACE_SUCCESS;
}

variable_types: status int

variable_types: signo int

return_type: enum ftrace_result_t { FTRACE_SUCCESS=0, FTRACE_NO_MEMORY=1, FTRACE_INVALID_PROCESS=2 }


decompiled_code:
__int64 __fastcall ftrace_create(const char *a1, char *const *a2)
{
int v2; // eax
unsigned int v3; // er12
int v4; // er8
__int64 result; // rax
struct _IO_FILE *v6; // rdi
__int64 v7; // rdx
int stat_loc[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v2 = vfork();
if ( v2 < 0 )
{
fwrite("Error: vfork failed\n", 1uLL, 0x14uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !v2 )
{
if ( ptrace(PTRACE_TRACEME, 0LL, 0LL, 0LL) >= 0 )
{
execvp(a1, a2);
fwrite("Error: exec failed\n", 1uLL, 0x13uLL, stderr);
exit(0);
}
fwrite("Error: ptrace failed\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}
v3 = v2;
stat_loc[0] = 0;
waitpid(v2, stat_loc, 2);
if ( (stat_loc[0] & 0x7F) != 0 )
{
v4 = add_task(v3);
result = 1LL;
if ( !v4 )
{
setup_ptrace_options(v3);
ptrace(PTRACE_SYSCALL, v3, 0LL, 0LL);
v6 = stderr;
fprintf(stderr, "Starting trace for pid %d\n", v3);
result = start(v6, "Starting trace for pid %d\n", v7);
}
}
else
{
fwrite("Error: child process failed to execute\n", 1uLL, 0x27uLL, stderr);
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, char *const *a2)
{
int v2; // eax
unsigned int v3; // er12
int v4; // er8
__int64 result; // rax
struct _IO_FILE *v6; // rdi
__int64 v7; // rdx
int stat_loc[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v2 = vfork();
if ( v2 < 0 )
{
fwrite("Error: vfork failed\n", 1uLL, 0x14uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !v2 )
{
if ( ptrace(PTRACE_TRACEME, 0LL, 0LL, 0LL) >= 0 )
{
execvp(a1, a2);
fwrite("Error: exec failed\n", 1uLL, 0x13uLL, stderr);
exit(0);
}
fwrite("Error: ptrace failed\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}
v3 = v2;
stat_loc[0] = 0;
waitpid(v2, stat_loc, 2);
if ( (stat_loc[0] & 0x7F) != 0 )
{
v4 = func4(v3);
result = 1LL;
if ( !v4 )
{
func3(v3);
ptrace(PTRACE_SYSCALL, v3, 0LL, 0LL);
v6 = stderr;
fprintf(stderr, "Starting trace for pid %d\n", v3);
result = func7(v6, "Starting trace for pid %d\n", v7);
}
}
else
{
fwrite("Error: child process failed to execute\n", 1uLL, 0x27uLL, stderr);
result = 2LL;
}
return result;
}

original_code:
static ftrace_result_t ftrace_create(const char* name, char** argv)
{
 int pid = vfork();
 if(pid < 0) {
  fprintf(stderr, "Error: vfork failed\n");
  return -1;
 }
 if(pid == 0) {
  long rc = ptrace(PTRACE_TRACEME, 0, ((void *)0), ((void *)0));
  if(rc < 0) {
   fprintf(stderr, "Error: ptrace failed\n");
   return -1;
  }
  execvp(name, argv);
  fprintf(stderr, "Error: exec failed\n");
  exit(0);
 }
 int status = 0;
 waitpid(pid, &status, 2);
 if((((status) & 0x7f) == 0)) {
  fprintf(stderr, "Error: child process failed to execute\n");
  return FTRACE_INVALID_PROCESS;
 }
 if(0 != add_task(pid)) {
  return FTRACE_NO_MEMORY;
 }
 setup_ptrace_options(pid);
 ptrace(PTRACE_SYSCALL, pid, ((void *)0), ((void *)0));
 fprintf(stderr, "Starting trace for pid %d\n", pid);
 return start();
}

canonical_original_code:
static enum ftrace_result_t ftrace_create(char *name, char **argv)
{
 int pid = vfork();
 if(pid < 0) {
  fprintf(stderr, "Error: vfork failed\n");
  return -1;
 }
 if(pid == 0) {
  long rc = ptrace(PTRACE_TRACEME, 0, ((void *)0), ((void *)0));
  if(rc < 0) {
   fprintf(stderr, "Error: ptrace failed\n");
   return -1;
  }
  execvp(name, argv);
  fprintf(stderr, "Error: exec failed\n");
  exit(0);
 }
 int status = 0;
 waitpid(pid, &status, 2);
 if((((status) & 0x7f) == 0)) {
  fprintf(stderr, "Error: child process failed to execute\n");
  return FTRACE_INVALID_PROCESS;
 }
 if(0 != add_task(pid)) {
  return FTRACE_NO_MEMORY;
 }
 setup_ptrace_options(pid);
 ptrace(PTRACE_SYSCALL, pid, ((void *)0), ((void *)0));
 fprintf(stderr, "Starting trace for pid %d\n", pid);
 return start();
}

variable_types:
name
char *

variable_types:
argv
char **

variable_types: rc long

user_defined_types: enum ftrace_result_t { FTRACE_SUCCESS=0, FTRACE_NO_MEMORY=1, FTRACE_INVALID_PROCESS=2 }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc > 1 )
return ftrace_create(argv[1], (char *const *)argv + 1);
usage();
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc > 1 )
return func8(argv[1], (char *const *)argv + 1);
func2();
return 1;
}

original_code:
int main(int argc, char** argv)
{
 if(argc < 2) {
  usage();
  return 1;
 }
 const char* cmd = argv[1];
 char** args = &argv[1];
 return ftrace_create(cmd, args);
}

canonical_original_code:
int main(int argc, char **argv)
{
 if(argc < 2) {
  usage();
  return 1;
 }
 char *cmd = argv[1];
 char **args = &argv[1];
 return ftrace_create(cmd, args);
}

variable_types: argc int

variable_types:
cmd
char *

variable_types:
args
char **


euclidianGCD

repo: warpling/RSA-Security-Assignment

decompiled_code:
__int64 __fastcall euclidianGCD(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v5; // [rsp+14h] [rbp-34h]

__gmpz_init();
__gmpz_init();
if ( *(int *)(a2 + 4) <= 0 || *(int *)(a3 + 4) <= 0 )
return fwrite("Cannot calculate GCD of non positive integer. GCD has not been set.\n", 1uLL, 0x44uLL, _bss_start);
if ( (int)__gmpz_cmp() < 0 )
{
__gmpz_set();
__gmpz_set();
__gmpz_set();
}
while ( 1 )
{
__gmpz_tdiv_qr();
if ( !v5 )
break;
__gmpz_set();
__gmpz_set();
result = (unsigned int)v5;
if ( v5 <= 0 )
return result;
}
return __gmpz_set();
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v5; // [rsp+14h] [rbp-34h]

__gmpz_init();
__gmpz_init();
if ( *(int *)(a2 + 4) <= 0 || *(int *)(a3 + 4) <= 0 )
return fwrite("Cannot calculate GCD of non positive integer. GCD has not been set.\n", 1uLL, 0x44uLL, _bss_start);
if ( (int)__gmpz_cmp() < 0 )
{
__gmpz_set();
__gmpz_set();
__gmpz_set();
}
while ( 1 )
{
__gmpz_tdiv_qr();
if ( !v5 )
break;
__gmpz_set();
__gmpz_set();
result = (unsigned int)v5;
if ( v5 <= 0 )
return result;
}
return __gmpz_set();
}

original_code:
void euclidianGCD(mpz_t *gcd, mpz_t numA, mpz_t numB) {
    mpz_t quotient; __gmpz_init(quotient);
    mpz_t remainder; __gmpz_init(remainder);
    if ((__builtin_constant_p (0) && (0) == 0 ? ((numA)->_mp_size < 0 ? -1 : (numA)->_mp_size > 0) : __gmpz_cmp_ui (numA,0)) <= 0 || (__builtin_constant_p (0) && (0) == 0 ? ((numB)->_mp_size < 0 ? -1 : (numB)->_mp_size > 0) : __gmpz_cmp_ui (numB,0)) <= 0) {
        fprintf(stderr, "Cannot calculate GCD of non positive integer. GCD has not been set.\n");
        return;
    }
    if (__gmpz_cmp(numA, numB) < 0) {
        mpz_t temp;
        __gmpz_set(temp, numA);
        __gmpz_set(numA, numB);
        __gmpz_set(numB, temp);
    }
    do {
        __gmpz_tdiv_qr(quotient, remainder, numA, numB);
        if((__builtin_constant_p (0) && (0) == 0 ? ((remainder)->_mp_size < 0 ? -1 : (remainder)->_mp_size > 0) : __gmpz_cmp_ui (remainder,0)) == 0) {
            __gmpz_set(*gcd, numB);
            return;
        }
        else {
            __gmpz_set(numA, numB);
            __gmpz_set(numB, remainder);
        }
    } while ((__builtin_constant_p (0) && (0) == 0 ? ((remainder)->_mp_size < 0 ? -1 : (remainder)->_mp_size > 0) : __gmpz_cmp_ui (remainder,0)) > 0);
}

canonical_original_code:
void euclidianGCD(struct __mpz_struct (*gcd)[1], struct __mpz_struct numA[1], struct __mpz_struct numB[1]) {
    struct __mpz_struct quotient[1]; __gmpz_init(quotient);
    struct __mpz_struct remainder[1]; __gmpz_init(remainder);
    if ((__builtin_constant_p (0) && (0) == 0 ? ((numA)->_mp_size < 0 ? -1 : (numA)->_mp_size > 0) : __gmpz_cmp_ui (numA,0)) <= 0 || (__builtin_constant_p (0) && (0) == 0 ? ((numB)->_mp_size < 0 ? -1 : (numB)->_mp_size > 0) : __gmpz_cmp_ui (numB,0)) <= 0) {
        fprintf(stderr, "Cannot calculate GCD of non positive integer. GCD has not been set.\n");
        return;
    }
    if (__gmpz_cmp(numA, numB) < 0) {
        struct __mpz_struct temp[1];
        __gmpz_set(temp, numA);
        __gmpz_set(numA, numB);
        __gmpz_set(numB, temp);
    }
    do {
        __gmpz_tdiv_qr(quotient, remainder, numA, numB);
        if((__builtin_constant_p (0) && (0) == 0 ? ((remainder)->_mp_size < 0 ? -1 : (remainder)->_mp_size > 0) : __gmpz_cmp_ui (remainder,0)) == 0) {
            __gmpz_set(*gcd, numB);
            return;
        }
        else {
            __gmpz_set(numA, numB);
            __gmpz_set(numB, remainder);
        }
    } while ((__builtin_constant_p (0) && (0) == 0 ? ((remainder)->_mp_size < 0 ? -1 : (remainder)->_mp_size > 0) : __gmpz_cmp_ui (remainder,0)) > 0);
}

variable_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; } (*)[1]

variable_types:
numA
struct __mpz_struct [1]

variable_types:
numB
struct __mpz_struct [1]

variable_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; } [1]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // [rsp+0h] [rbp-38h] BYREF
char v5[16]; // [rsp+10h] [rbp-28h] BYREF
char v6[24]; // [rsp+20h] [rbp-18h] BYREF

__gmpz_init();
__gmpz_init();
__gmpz_init();
__gmp_printf();
__gmp_scanf();
__gmp_printf();
__gmp_scanf();
__gmp_printf();
euclidianGCD(v6, &v4, v5);
__gmp_printf();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // [rsp+0h] [rbp-38h] BYREF
char v5[16]; // [rsp+10h] [rbp-28h] BYREF
char v6[24]; // [rsp+20h] [rbp-18h] BYREF

__gmpz_init();
__gmpz_init();
__gmpz_init();
__gmp_printf();
__gmp_scanf();
__gmp_printf();
__gmp_scanf();
__gmp_printf();
func7(v6, &v4, v5);
__gmp_printf();
return 0;
}

original_code:
int main(int argc, char const *argv[])
{
    mpz_t numA, numB, gcd;
    __gmpz_init(numA);
    __gmpz_init(numB);
    __gmpz_init(gcd);
    __gmp_printf("Input first number: ");
    __gmp_scanf("%Zd", numA);
    __gmp_printf("Input second number: ");
    __gmp_scanf("%Zd", numB);
    __gmp_printf("GDC of %Zd and %Zd is ", numA, numB);
    euclidianGCD(&gcd, numA, numB);
    __gmp_printf(" %Zd!\n", gcd);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct __mpz_struct numA[1]; struct __mpz_struct numB[1]; struct __mpz_struct gcd[1];
    __gmpz_init(numA);
    __gmpz_init(numB);
    __gmpz_init(gcd);
    __gmp_printf("Input first number: ");
    __gmp_scanf("%Zd", numA);
    __gmp_printf("Input second number: ");
    __gmp_scanf("%Zd", numB);
    __gmp_printf("GDC of %Zd and %Zd is ", numA, numB);
    euclidianGCD(&gcd, numA, numB);
    __gmp_printf(" %Zd!\n", gcd);
    return 0;
}

variable_types:
argv
char *[]

user_defined_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; }


repo: wartalker/flowers-by-opengl

decompiled_code:
__int64 __fastcall glcreate_shader(const char *a1, unsigned int a2)
{
__int64 v2; // r12
unsigned int v3; // er13
char *v4; // rbp
const char *v6; // [rsp+8h] [rbp-30h] BYREF
int v7; // [rsp+18h] [rbp-20h] BYREF
int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v6 = a1;
v2 = (unsigned int)glCreateShader(a2);
glShaderSource(v2, 1LL, &v6, 0LL);
glCompileShader((unsigned int)v2);
glGetShaderiv((unsigned int)v2, 35713LL, &v7);
if ( !v7 )
{
glGetShaderiv((unsigned int)v2, 35716LL, v8);
v3 = v8[0];
v4 = (char *)malloc(v8[0]);
glGetShaderInfoLog((unsigned int)v2, v3, 0LL, v4);
fprintf(stdout, "%s\n\n%s", v4, v6);
free(v4);
if ( !v7 )
LODWORD(v2) = 0;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2)
{
__int64 v2; // r12
unsigned int v3; // er13
char *v4; // rbp
const char *v6; // [rsp+8h] [rbp-30h] BYREF
int v7; // [rsp+18h] [rbp-20h] BYREF
int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v6 = a1;
v2 = (unsigned int)glCreateShader(a2);
glShaderSource(v2, 1LL, &v6, 0LL);
glCompileShader((unsigned int)v2);
glGetShaderiv((unsigned int)v2, 35713LL, &v7);
if ( !v7 )
{
glGetShaderiv((unsigned int)v2, 35716LL, v8);
v3 = v8[0];
v4 = (char *)malloc(v8[0]);
glGetShaderInfoLog((unsigned int)v2, v3, 0LL, v4);
fprintf(stdout, "%s\n\n%s", v4, v6);
free(v4);
if ( !v7 )
LODWORD(v2) = 0;
}
return (unsigned int)v2;
}

original_code:
static glshader
glcreate_shader(const glchar * src, glshader_type type) {
 glshader shid;
 glbool status;
 GLint n;
 char * log;
 shid = glCreateShader(type);
 glShaderSource(shid, 1, (const GLchar**)&src, ((void *)0));
 glCompileShader(shid);
 glGetShaderiv(shid, 0x8B81, &status);
 if (!status) {
  glGetShaderiv(shid, 0x8B84, &n);
  log = (char *) malloc(n);
  glGetShaderInfoLog(shid, n, ((void *)0), log);
  fprintf(stdout, "%s\n\n%s", log, src);
  free(log);
 }
 return (!status) ? 0 : shid;
}

canonical_original_code:
static unsigned int glcreate_shader(char *src, unsigned int type) {
 unsigned int shid;
 int status;
 int n;
 char *log;
 shid = glCreateShader(type);
 glShaderSource(shid, 1, (char **)&src, ((void *)0));
 glCompileShader(shid);
 glGetShaderiv(shid, 0x8B81, &status);
 if (!status) {
  glGetShaderiv(shid, 0x8B84, &n);
  log = (char *) malloc(n);
  glGetShaderInfoLog(shid, n, ((void *)0), log);
  fprintf(stdout, "%s\n\n%s", log, src);
  free(log);
 }
 return (!status) ? 0 : shid;
}

variable_types:
src
char *

variable_types:
type
unsigned int

variable_types:
shid
unsigned int

variable_types: n int

variable_types:
log
char *

return_type: unsigned int


decompiled_code:
void __fastcall glfree_vector(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void
glfree_vector(glvector * v) {
 if (((void *)0) != v) free(v);
}

canonical_original_code:
void glfree_vector(struct _glvector *v) {
 if (((void *)0) != v) free(v);
}

variable_types: struct _glvector { unsigned long size; unsigned long total; float vec[]; } *


decompiled_code:
__int64 __fastcall glget_vector_size(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
size_t
glget_vector_size(glvector * v) {
 return v->size;
}

canonical_original_code:
unsigned long glget_vector_size(struct _glvector *v) {
 return v->size;
}

return_type: unsigned long

user_defined_types: struct _glvector { unsigned long size; unsigned long total; float vec[]; }


decompiled_code:
__int64 __fastcall glget_vector_array(__int64 a1)
{
return a1 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return a1 + 16;
}

original_code:
glfloat *
glget_vector_array(glvector * v) {
 return v->vec;
}

canonical_original_code:
float *glget_vector_array(struct _glvector *v) {
 return v->vec;
}

return_type: float *


original_code:
void
glpush_vector(glvector * * pv, glfloat e) {
 glvector * t;
 size_t size = (*pv)->size;
 size_t total = (*pv)->total;
 if (size*sizeof(glfloat) >= total) {
  t = glalloc_vector(2*total);
  t->size = size;
  t->total = 2*total;
  memcpy(t->vec, (*pv)->vec, total);
  glfree_vector(*pv);
  *pv = t;
  t = ((void *)0);
 }
 (*pv)->vec[size] = e;
 (*pv)->size++;
}

canonical_original_code:
void glpush_vector(struct _glvector **pv, float e) {
 struct _glvector *t;
 unsigned long size = (*pv)->size;
 unsigned long total = (*pv)->total;
 if (size*sizeof(glfloat) >= total) {
  t = glalloc_vector(2*total);
  t->size = size;
  t->total = 2*total;
  memcpy(t->vec, (*pv)->vec, total);
  glfree_vector(*pv);
  *pv = t;
  t = ((void *)0);
 }
 (*pv)->vec[size] = e;
 (*pv)->size++;
}

variable_types: struct _glvector { unsigned long size; unsigned long total; float vec[]; } **

variable_types: e float

variable_types:
size
unsigned long

variable_types:
total
unsigned long


decompiled_code:
__int64 __fastcall glpush_vec3(void **a1, float *a2)
{
glpush_vector(a1, *a2);
glpush_vector(a1, a2[1]);
return glpush_vector(a1, a2[2]);
}

canonical_decompiled_code:
__int64 __fastcall func5(void **a1, float *a2)
{
func4(a1, *a2);
func4(a1, a2[1]);
return func4(a1, a2[2]);
}

original_code:
void
glpush_vec3(glvector * * pv, glvec3 * pp) {
 glpush_vector(pv, pp->x);
 glpush_vector(pv, pp->y);
 glpush_vector(pv, pp->z);
}

canonical_original_code:
void glpush_vec3(struct _glvector **pv, struct _glvec3 *pp) {
 glpush_vector(pv, pp->x);
 glpush_vector(pv, pp->y);
 glpush_vector(pv, pp->z);
}

variable_types: struct _glvec3 { float x; float y; float z; } *


decompiled_code:
__int64 __fastcall glpush_2vec3(void **a1, float *a2, float *a3)
{
glpush_vec3(a1, a2);
return glpush_vec3(a1, a3);
}

canonical_decompiled_code:
__int64 __fastcall func6(void **a1, float *a2, float *a3)
{
func5(a1, a2);
return func5(a1, a3);
}

original_code:
void
glpush_2vec3(glvector * * pv, glvec3 * pp1, glvec3 * pp2) {
 glpush_vec3(pv, pp1);
 glpush_vec3(pv, pp2);
}

canonical_original_code:
void glpush_2vec3(struct _glvector **pv, struct _glvec3 *pp1, struct _glvec3 *pp2) {
 glpush_vec3(pv, pp1);
 glpush_vec3(pv, pp2);
}

user_defined_types: struct _glvec3 { float x; float y; float z; }


original_code:
void
glappend_vector(glvector * * pv, glvector * v) {
 glvector * t;
 size_t sdst = (*pv)->size;
 size_t ssrc = v->size;
 size_t tdst = (*pv)->total;
 size_t size = sdst + ssrc;
 if (size*sizeof(glfloat) >= tdst) {
  t = glalloc_vector(size*sizeof(glfloat));
  t->size = sdst;
  t->total = size*sizeof(glfloat);
  memcpy(t->vec, (*pv)->vec, tdst);
  glfree_vector(*pv);
  *pv = t;
  t = ((void *)0);
 }
 memcpy((char *)((*pv)->vec) + sizeof(glfloat)*(*pv)->size, (char *)(v->vec), sizeof(glfloat)*v->size);
 (*pv)->size = size;
}

canonical_original_code:
void glappend_vector(struct _glvector **pv, struct _glvector *v) {
 struct _glvector *t;
 unsigned long sdst = (*pv)->size;
 unsigned long ssrc = v->size;
 unsigned long tdst = (*pv)->total;
 unsigned long size = sdst + ssrc;
 if (size*sizeof(glfloat) >= tdst) {
  t = glalloc_vector(size*sizeof(glfloat));
  t->size = sdst;
  t->total = size*sizeof(glfloat);
  memcpy(t->vec, (*pv)->vec, tdst);
  glfree_vector(*pv);
  *pv = t;
  t = ((void *)0);
 }
 memcpy((char *)((*pv)->vec) + sizeof(glfloat)*(*pv)->size, (char *)(v->vec), sizeof(glfloat)*v->size);
 (*pv)->size = size;
}

variable_types:
v
struct _glvector *

variable_types:
sdst
unsigned long

variable_types:
ssrc
unsigned long

variable_types:
tdst
unsigned long


decompiled_code:
unsigned __int64 __fastcall glprint_vector(__int64 a1)
{
unsigned __int64 i; // rbx
unsigned __int64 result; // rax

for ( i = 0LL; ; i += 3LL )
{
result = glget_vector_size(a1);
if ( result <= i )
break;
fprintf(stdout, "%f %f %f\n", *(float *)(a1 + 4 * i + 16), *(float *)(a1 + 4 * i + 20), *(float *)(a1 + 4 * i + 24));
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(__int64 a1)
{
unsigned __int64 i; // rbx
unsigned __int64 result; // rax

for ( i = 0LL; ; i += 3LL )
{
result = func2(a1);
if ( result <= i )
break;
fprintf(stdout, "%f %f %f\n", *(float *)(a1 + 4 * i + 16), *(float *)(a1 + 4 * i + 20), *(float *)(a1 + 4 * i + 24));
}
return result;
}

original_code:
void
glprint_vector(glvector * v) {
 size_t i;
 for (i = 0; i < glget_vector_size(v); i += 3) {
  fprintf(stdout, "%f %f %f\n", v->vec[i], v->vec[i+1], v->vec[i+2]);
 }
}

canonical_original_code:
void glprint_vector(struct _glvector *v) {
 unsigned long i;
 for (i = 0; i < glget_vector_size(v); i += 3) {
  fprintf(stdout, "%f %f %f\n", v->vec[i], v->vec[i+1], v->vec[i+2]);
 }
}

variable_types:
i
unsigned long


decompiled_code:
void *glalloc_programe()
{
return calloc(0xCuLL, 1uLL);
}

canonical_decompiled_code:
void *func9()
{
return calloc(0xCuLL, 1uLL);
}

original_code:
glprograme *
glalloc_programe() {
 glprograme * p = (glprograme *) malloc(sizeof(*p));
 if (p) memset(p, 0, sizeof(*p));
 return p;
}

canonical_original_code:
struct _glprograme *glalloc_programe() {
 struct _glprograme *p = (glprograme *) malloc(sizeof(*p));
 if (p) memset(p, 0, sizeof(*p));
 return p;
}

variable_types: struct _glprograme { unsigned int pid; unsigned int vid; unsigned int fid; } *


decompiled_code:
__int64 __fastcall glcreate_programe(_DWORD *a1, const char *a2, const char *a3)
{
unsigned int v4; // eax
__int64 result; // rax
unsigned int v6; // ebx
unsigned int v7; // er12
__int64 v8; // r13
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = glcreate_shader(a2, 0x8B31u);
if ( !v4 )
return 0LL;
v6 = v4;
v7 = glcreate_shader(a3, 0x8B30u);
if ( !v7 )
return 0LL;
v8 = (unsigned int)glCreateProgram();
glAttachShader(v8, v6);
glAttachShader((unsigned int)v8, v7);
glLinkProgram((unsigned int)v8);
glGetProgramiv((unsigned int)v8, 35714LL, v9);
if ( !v9[0] )
return 0LL;
*a1 = v8;
result = 1LL;
a1[1] = v6;
a1[2] = v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_DWORD *a1, const char *a2, const char *a3)
{
unsigned int v4; // eax
__int64 result; // rax
unsigned int v6; // ebx
unsigned int v7; // er12
__int64 v8; // r13
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = func0(a2, 0x8B31u);
if ( !v4 )
return 0LL;
v6 = v4;
v7 = func0(a3, 0x8B30u);
if ( !v7 )
return 0LL;
v8 = (unsigned int)glCreateProgram();
glAttachShader(v8, v6);
glAttachShader((unsigned int)v8, v7);
glLinkProgram((unsigned int)v8);
glGetProgramiv((unsigned int)v8, 35714LL, v9);
if ( !v9[0] )
return 0LL;
*a1 = v8;
result = 1LL;
a1[1] = v6;
a1[2] = v7;
return result;
}

original_code:
bool
glcreate_programe(glprograme * p, const glchar * vsource, const glchar * fsource) {
 glshader vid, fid;
 glpid pid;
 glbool status;
 vid = glcreate_shader(vsource, 0x8B31);
 if (0 == vid) return 0;
 fid = glcreate_shader(fsource, 0x8B30);
 if (0 == fid) return 0;
 pid = glCreateProgram();
 glAttachShader(pid, vid);
 glAttachShader(pid, fid);
 glLinkProgram(pid);
 glGetProgramiv(pid, 0x8B82, &status);
 if (!status) return 0;
 p->pid = pid;
 p->vid = vid;
 p->fid = fid;
 return 1;
}

canonical_original_code:
unsigned char glcreate_programe(struct _glprograme *p, char *vsource, char *fsource) {
 unsigned int vid; unsigned int fid;
 unsigned int pid;
 int status;
 vid = glcreate_shader(vsource, 0x8B31);
 if (0 == vid) return 0;
 fid = glcreate_shader(fsource, 0x8B30);
 if (0 == fid) return 0;
 pid = glCreateProgram();
 glAttachShader(pid, vid);
 glAttachShader(pid, fid);
 glLinkProgram(pid);
 glGetProgramiv(pid, 0x8B82, &status);
 if (!status) return 0;
 p->pid = pid;
 p->vid = vid;
 p->fid = fid;
 return 1;
}

variable_types:
vsource
char *

variable_types:
fsource
char *

variable_types:
vid
unsigned int

variable_types:
fid
unsigned int

variable_types:
pid
unsigned int

variable_types: status int

return_type: unsigned char

user_defined_types: struct _glprograme { unsigned int pid; unsigned int vid; unsigned int fid; }


decompiled_code:
__int64 __fastcall glfree_programe(unsigned int *a1)
{
__int64 result; // rax

if ( a1 )
{
glUseProgram(0LL);
glDetachShader(*a1, a1[1]);
glDetachShader(*a1, a1[2]);
glDeleteShader(a1[1]);
glDeleteShader(a1[2]);
result = glDeleteProgram(*a1);
*(_QWORD *)a1 = 0LL;
a1[2] = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1)
{
__int64 result; // rax

if ( a1 )
{
glUseProgram(0LL);
glDetachShader(*a1, a1[1]);
glDetachShader(*a1, a1[2]);
glDeleteShader(a1[1]);
glDeleteShader(a1[2]);
result = glDeleteProgram(*a1);
*(_QWORD *)a1 = 0LL;
a1[2] = 0;
}
return result;
}

original_code:
void
glfree_programe(glprograme * p) {
 if (((void *)0) == p) return;
 glUseProgram(0);
 glDetachShader(p->pid, p->vid);
 glDetachShader(p->pid, p->fid);
 glDeleteShader(p->vid);
 glDeleteShader(p->fid);
 glDeleteProgram(p->pid);
 memset(p, 0, sizeof(*p));
}

canonical_original_code:
void glfree_programe(struct _glprograme *p) {
 if (((void *)0) == p) return;
 glUseProgram(0);
 glDetachShader(p->pid, p->vid);
 glDetachShader(p->pid, p->fid);
 glDeleteShader(p->vid);
 glDeleteShader(p->fid);
 glDeleteProgram(p->pid);
 memset(p, 0, sizeof(*p));
}


decompiled_code:
void __fastcall glset_vec3(float *a1, float a2, float a3, float a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

canonical_decompiled_code:
void __fastcall func12(float *a1, float a2, float a3, float a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

original_code:
void
glset_vec3(glvec3 * pp, glfloat x, glfloat y, glfloat z) {
 pp->x = x;
 pp->y = y;
 pp->z = z;
}

canonical_original_code:
void glset_vec3(struct _glvec3 *pp, float x, float y, float z) {
 pp->x = x;
 pp->y = y;
 pp->z = z;
}

variable_types: x float

variable_types: y float

variable_types: z float


decompiled_code:
void __fastcall glassign_vec3(float *a1, float *a2)
{
glset_vec3(a1, *a2, a2[1], a2[2]);
}

canonical_decompiled_code:
void __fastcall func13(float *a1, float *a2)
{
func12(a1, *a2, a2[1], a2[2]);
}

original_code:
void
glassign_vec3(glvec3 * pdes, glvec3 * psrc) {
 glset_vec3(pdes, psrc->x, psrc->y, psrc->z);
}

canonical_original_code:
void glassign_vec3(struct _glvec3 *pdes, struct _glvec3 *psrc) {
 glset_vec3(pdes, psrc->x, psrc->y, psrc->z);
}

variable_types:
psrc
struct _glvec3 *


decompiled_code:
void *glalloc_mat4()
{
return calloc(0x40uLL, 1uLL);
}

canonical_decompiled_code:
void *func14()
{
return calloc(0x40uLL, 1uLL);
}

original_code:
glmat4 *
glalloc_mat4() {
 glmat4 * m;
 m = (glmat4 *) malloc(sizeof(*m));
 memset((void *)m, 0, sizeof(*m));
 return m;
}

canonical_original_code:
struct _glmat4 *glalloc_mat4() {
 struct _glmat4 *m;
 m = (struct _glmat4 *) malloc(sizeof(*m));
 memset((void *)m, 0, sizeof(*m));
 return m;
}

variable_types:
m
struct _glmat4 { float vecs[16]; } *

return_type: struct _glmat4 { float vecs[16]; } *


decompiled_code:
// attributes: thunk
void glfree_mat4(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func15(void *ptr)
{
free(ptr);
}

original_code:
void
glfree_mat4(glmat4 * m) {
 free((void *)m);
}

canonical_original_code:
void glfree_mat4(struct _glmat4 *m) {
 free((void *)m);
}


decompiled_code:
void glcreate_identify_mat4()
{
void *v0; // rax

v0 = glalloc_mat4();
glset_identify_mat4((__int64)v0);
}

original_code:
glmat4 *
glcreate_identify_mat4() {
 glmat4 * m;
 m = glalloc_mat4();
 glset_identify_mat4(m);
 return m;
}

canonical_original_code:
struct _glmat4 *glcreate_identify_mat4() {
 struct _glmat4 *m;
 m = glalloc_mat4();
 glset_identify_mat4(m);
 return m;
}


original_code:
void
glscale_mat4(glmat4 * m, glvec3 * s) {
 m->vecs[0] *= s->x;
 m->vecs[5] *= s->y;
 m->vecs[10] *= s->z;
}

canonical_original_code:
void glscale_mat4(struct _glmat4 *m, struct _glvec3 *s) {
 m->vecs[0] *= s->x;
 m->vecs[5] *= s->y;
 m->vecs[10] *= s->z;
}

variable_types: struct _glvec3 { float x; float y; float z; } *


decompiled_code:
void __fastcall glrotatefz_mat4(__int64 a1, float a2)
{
float v2; // xmm1_4
float v3; // xmm0_4
double v4; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

sincos(a2, sinx, &v4);
v2 = sinx[0];
v3 = v4;
*(float *)a1 = v3;
*(_DWORD *)(a1 + 4) = LODWORD(v2) ^ LC3;
*(float *)(a1 + 16) = v2;
*(float *)(a1 + 20) = v3;
}

canonical_decompiled_code:
void __fastcall func19(__int64 a1, float a2)
{
float v2; // xmm1_4
float v3; // xmm0_4
double v4; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

sincos(a2, sinx, &v4);
v2 = sinx[0];
v3 = v4;
*(float *)a1 = v3;
*(_DWORD *)(a1 + 4) = LODWORD(v2) ^ LC3;
*(float *)(a1 + 16) = v2;
*(float *)(a1 + 20) = v3;
}

original_code:
void glrotatefz_mat4(glmat4 * m, glfloat a) {
 glfloat sina = (glfloat) sin(a);
 glfloat cosa = (glfloat) cos(a);
 m->vecs[0] = cosa;
 m->vecs[1] = -sina;
 m->vecs[4] = sina;
 m->vecs[5] = cosa;
}

canonical_original_code:
void glrotatefz_mat4(struct _glmat4 *m, float a) {
 float sina = (glfloat) sin(a);
 float cosa = (glfloat) cos(a);
 m->vecs[0] = cosa;
 m->vecs[1] = -sina;
 m->vecs[4] = sina;
 m->vecs[5] = cosa;
}

variable_types: a float

variable_types: sina float

variable_types: cosa float


original_code:
void
glmove_mat4(glmat4 * m, glvec3 * v) {
 m->vecs[3] += v->x;
 m->vecs[7] += v->y;
 m->vecs[11] += v->z;
}

canonical_original_code:
void glmove_mat4(struct _glmat4 *m, struct _glvec3 *v) {
 m->vecs[3] += v->x;
 m->vecs[7] += v->y;
 m->vecs[11] += v->z;
}


decompiled_code:
void __fastcall glmvtrans_mat4(float *a1, float a2, float a3)
{
*a1 = (float)(*a1 / a2) - 0.5;
a1[5] = (float)(a1[5] / a3) - 0.5;
}

canonical_decompiled_code:
void __fastcall func21(float *a1, float a2, float a3)
{
*a1 = (float)(*a1 / a2) - 0.5;
a1[5] = (float)(a1[5] / a3) - 0.5;
}

original_code:
void
glmvtrans_mat4(glmat4 * m, glfloat w, glfloat h) {
 m->vecs[0] /= w;
 m->vecs[0] -= 0.50f;
 m->vecs[5] /= h;
 m->vecs[5] -= 0.50f;
}

canonical_original_code:
void glmvtrans_mat4(struct _glmat4 *m, float w, float h) {
 m->vecs[0] /= w;
 m->vecs[0] -= 0.50f;
 m->vecs[5] /= h;
 m->vecs[5] -= 0.50f;
}

variable_types: w float

variable_types: h float


decompiled_code:
__int64 __fastcall glget_mat4_array(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1)
{
return a1;
}

original_code:
glfloat *
glget_mat4_array(glmat4 * m) {
 return m->vecs;
}

canonical_original_code:
float *glget_mat4_array(struct _glmat4 *m) {
 return m->vecs;
}


original_code:
void
glassign_mat4(glmat4 * dest, glmat4 * src) {
 memcpy((void *)dest, (void *)src, sizeof(*dest));
}

canonical_original_code:
void glassign_mat4(struct _glmat4 *dest, struct _glmat4 *src) {
 memcpy((void *)dest, (void *)src, sizeof(*dest));
}

variable_types:
dest
struct _glmat4 { float vecs[16]; } *

variable_types:
src
struct _glmat4 *


variable_types:
destm
struct _glmat4 { float vecs[16]; } *

variable_types:
srcm
struct _glmat4 *

variable_types:
dest
float *

variable_types:
src
float *


decompiled_code:
void __fastcall glmutiply_scale_mat4(__int64 a1, float *a2)
{
float *v2; // r8

glset_identify_mat4((__int64)mt_2);
glscale_mat4(mt_2, a2);
glmutiply_mat4(v2, mt_2);
}

canonical_decompiled_code:
void __fastcall func25(__int64 a1, float *a2)
{
float *v2; // r8

func16((__int64)mt_2);
func18(mt_2, a2);
func24(v2, mt_2);
}

original_code:
void
glmutiply_scale_mat4(glmat4 * m, glvec3 * v) {
 static glmat4 mt;
 glset_identify_mat4(&mt);
 glscale_mat4(&mt, v);
 glmutiply_mat4(m, &mt);
}

canonical_original_code:
void glmutiply_scale_mat4(struct _glmat4 *m, struct _glvec3 *v) {
 static struct _glmat4 mt;
 glset_identify_mat4(&mt);
 glscale_mat4(&mt, v);
 glmutiply_mat4(m, &mt);
}

variable_types:
mt
struct _glmat4 { float vecs[16]; }


decompiled_code:
void __fastcall glmutiply_rotatez_mat4(float *a1, float a2)
{
glset_identify_mat4((__int64)mt_1);
glrotatefz_mat4((__int64)mt_1, a2);
glmutiply_mat4(a1, mt_1);
}

canonical_decompiled_code:
void __fastcall func26(float *a1, float a2)
{
func16((__int64)mt_1);
func19((__int64)mt_1, a2);
func24(a1, mt_1);
}

original_code:
void
glmutiply_rotatez_mat4(glmat4 * m, glfloat a) {
 static glmat4 mt;
 glset_identify_mat4(&mt);
 glrotatefz_mat4(&mt, a);
 glmutiply_mat4(m, &mt);
}

canonical_original_code:
void glmutiply_rotatez_mat4(struct _glmat4 *m, float a) {
 static struct _glmat4 mt;
 glset_identify_mat4(&mt);
 glrotatefz_mat4(&mt, a);
 glmutiply_mat4(m, &mt);
}


decompiled_code:
void __fastcall glmutiply_move_mat4(__int64 a1, float *a2)
{
float *v2; // r8

glset_identify_mat4((__int64)mt_0);
glmove_mat4(mt_0, a2);
glmutiply_mat4(v2, mt_0);
}

canonical_decompiled_code:
void __fastcall func27(__int64 a1, float *a2)
{
float *v2; // r8

func16((__int64)mt_0);
func20(mt_0, a2);
func24(v2, mt_0);
}

original_code:
void glmutiply_move_mat4(glmat4 * m, glvec3 * v) {
 static glmat4 mt;
 glset_identify_mat4(&mt);
 glmove_mat4(&mt, v);
 glmutiply_mat4(m, &mt);
}

canonical_original_code:
void glmutiply_move_mat4(struct _glmat4 *m, struct _glvec3 *v) {
 static struct _glmat4 mt;
 glset_identify_mat4(&mt);
 glmove_mat4(&mt, v);
 glmutiply_mat4(m, &mt);
}


decompiled_code:
float __fastcall glang_transform(int a1)
{
return (float)((float)a1 * 3.1415925) / 180.0;
}

canonical_decompiled_code:
float __fastcall func28(int a1)
{
return (float)((float)a1 * 3.1415925) / 180.0;
}

original_code:
glfloat glang_transform(unsigned int a) {
 return (glfloat)(a*(3.1415926f)/180.f);
}

variable_types:
a
unsigned int

return_type: float


decompiled_code:
void __fastcall glrotatez_mat4(__int64 a1, int a2)
{
float v2; // xmm0_4
__int64 v3; // r8

v2 = glang_transform(a2);
glrotatefz_mat4(v3, v2);
}

canonical_decompiled_code:
void __fastcall func29(__int64 a1, int a2)
{
float v2; // xmm0_4
__int64 v3; // r8

v2 = func28(a2);
func19(v3, v2);
}

original_code:
void
glrotatez_mat4(glmat4 * m, glangle ang) {
 glfloat a = glang_transform(ang);
 glrotatefz_mat4(m, a);
}

canonical_original_code:
void glrotatez_mat4(struct _glmat4 *m, int ang) {
 float a = glang_transform(ang);
 glrotatefz_mat4(m, a);
}

variable_types: ang int


render

decompiled_code:
__int64 render()
{
glEnable(2884LL);
glEnable(2929LL);
glEnable(3042LL);
glClear(256LL);
glClearDepth(1.0);
glClear(0x4000LL);
glBlendFunc(770LL, 771LL);
glDepthFunc(513LL);
glClearColor(1.0, 1.0, 1.0);
glrender_tree_context();
return glutSwapBuffers();
}

canonical_decompiled_code:
__int64 func0()
{
glEnable(2884LL);
glEnable(2929LL);
glEnable(3042LL);
glClear(256LL);
glClearDepth(1.0);
glClear(0x4000LL);
glBlendFunc(770LL, 771LL);
glDepthFunc(513LL);
glClearColor(1.0, 1.0, 1.0);
glrender_tree_context();
return glutSwapBuffers();
}

original_code:
static void
render(void) {
 glEnable(0x0B44);
 glEnable(0x0B71);
 glEnable(0x0BE2);
 glClear(0x00000100);
 glClearDepth(1.0f);
 glClear(0x00004000);
 glBlendFunc(0x0302, 0x0303);
 glDepthFunc(0x0201);
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glrender_tree_context();
 glutSwapBuffers();
}

canonical_original_code:
static void render() {
 glEnable(0x0B44);
 glEnable(0x0B71);
 glEnable(0x0BE2);
 glClear(0x00000100);
 glClearDepth(1.0f);
 glClear(0x00004000);
 glBlendFunc(0x0302, 0x0303);
 glDepthFunc(0x0201);
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glrender_tree_context();
 glutSwapBuffers();
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = argc;
glutInit(v3, argv, envp);
glutInitDisplayMode(18LL);
glutInitWindowSize(800LL, 800LL);
glutCreateWindow("flower");
glutDisplayFunc(render);
glinit_tree_context();
glClearColor(1.0, 1.0, 1.0);
glutMainLoop();
exit(0);
}

original_code:
int
main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(0x0000 | 0x0002 | 0x0010);
 glutInitWindowSize(800, 800);
 glutCreateWindow("flower");
 glutDisplayFunc(&render);
 glinit_tree_context();
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glutMainLoop();
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(0x0000 | 0x0002 | 0x0010);
 glutInitWindowSize(800, 800);
 glutCreateWindow("flower");
 glutDisplayFunc(&render);
 glinit_tree_context();
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glutMainLoop();
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **


repo: watsonjiang/hybirdthread

decompiled_code:
void __fastcall ht_ring_init(__int64 a1)
{
if ( a1 )
{
*(_QWORD *)a1 = 0LL;
*(_DWORD *)(a1 + 8) = 0;
}
}

original_code:
void
ht_ring_init(ht_ring_t *r)
{
    if (r == ((void *)0))
        return;
    r->r_hook = ((void *)0);
    r->r_nodes = 0;
    return;
}

canonical_original_code:
void ht_ring_init(struct ht_ring_st *r)
{
    if (r == ((void *)0))
        return;
    r->r_hook = ((void *)0);
    r->r_nodes = 0;
    return;
}

variable_types: struct ht_ring_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } *r_hook; unsigned int r_nodes; } *

user_defined_types: struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; }


original_code:
void
ht_ring_insert_after(ht_ring_t *r, ht_ringnode_t *rn1, ht_ringnode_t *rn2)
{
    if (r == ((void *)0) || rn1 == ((void *)0) || rn2 == ((void *)0))
        return;
    rn2->rn_prev = rn1;
    rn2->rn_next = rn1->rn_next;
    rn2->rn_prev->rn_next = rn2;
    rn2->rn_next->rn_prev = rn2;
    r->r_nodes++;
    return;
}

canonical_original_code:
void ht_ring_insert_after(struct ht_ring_st *r, struct ht_ringnode_st *rn1, struct ht_ringnode_st *rn2)
{
    if (r == ((void *)0) || rn1 == ((void *)0) || rn2 == ((void *)0))
        return;
    rn2->rn_prev = rn1;
    rn2->rn_next = rn1->rn_next;
    rn2->rn_prev->rn_next = rn2;
    rn2->rn_next->rn_prev = rn2;
    r->r_nodes++;
    return;
}

user_defined_types: struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; }


original_code:
void
ht_ring_insert_before(ht_ring_t *r, ht_ringnode_t *rn1, ht_ringnode_t *rn2)
{
    if (r == ((void *)0) || rn1 == ((void *)0) || rn2 == ((void *)0))
        return;
    rn2->rn_next = rn1;
    rn2->rn_prev = rn1->rn_prev;
    rn2->rn_prev->rn_next = rn2;
    rn2->rn_next->rn_prev = rn2;
    r->r_nodes++;
    return;
}

canonical_original_code:
void ht_ring_insert_before(struct ht_ring_st *r, struct ht_ringnode_st *rn1, struct ht_ringnode_st *rn2)
{
    if (r == ((void *)0) || rn1 == ((void *)0) || rn2 == ((void *)0))
        return;
    rn2->rn_next = rn1;
    rn2->rn_prev = rn1->rn_prev;
    rn2->rn_prev->rn_next = rn2;
    rn2->rn_next->rn_prev = rn2;
    r->r_nodes++;
    return;
}


decompiled_code:
__int64 __fastcall ht_ring_favorite(__int64 *a1, _QWORD *a2)
{
if ( !a1 || !*a1 )
return 0LL;
if ( (_QWORD *)*a1 != a2 )
{
ht_ring_delete((__int64)a1, a2);
ht_ring_prepend(a1, a2);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1, _QWORD *a2)
{
if ( !a1 || !*a1 )
return 0LL;
if ( (_QWORD *)*a1 != a2 )
{
func3((__int64)a1, a2);
func4(a1, a2);
}
return 1LL;
}

original_code:
int
ht_ring_favorite(ht_ring_t *r, ht_ringnode_t *rn)
{
    if (r == ((void *)0))
        return (0);
    if (r->r_hook == ((void *)0))
        return (0);
    if (r->r_hook == rn)
        return (!(0));
    ht_ring_delete(r, rn);
    ht_ring_prepend(r, rn);
    return (!(0));
}

canonical_original_code:
int ht_ring_favorite(struct ht_ring_st *r, struct ht_ringnode_st *rn)
{
    if (r == ((void *)0))
        return (0);
    if (r->r_hook == ((void *)0))
        return (0);
    if (r->r_hook == rn)
        return (!(0));
    ht_ring_delete(r, rn);
    ht_ring_prepend(r, rn);
    return (!(0));
}

variable_types:
rn
struct ht_ringnode_st *


decompiled_code:
__int64 __fastcall ht_ring_contains(_QWORD **a1, _QWORD *a2)
{
__int64 result; // rax
_QWORD *v3; // rax

if ( a1 && a2 )
{
result = 0LL;
if ( *a1 )
{
v3 = *a1;
do
{
if ( v3 == a2 )
return 1LL;
v3 = (_QWORD *)*v3;
}
while ( *a1 != v3 );
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD **a1, _QWORD *a2)
{
__int64 result; // rax
_QWORD *v3; // rax

if ( a1 && a2 )
{
result = 0LL;
if ( *a1 )
{
v3 = *a1;
do
{
if ( v3 == a2 )
return 1LL;
v3 = (_QWORD *)*v3;
}
while ( *a1 != v3 );
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

original_code:
int
ht_ring_contains(ht_ring_t *r, ht_ringnode_t *rns)
{
    ht_ringnode_t *rn;
    int rc;
    if (r == ((void *)0) || rns == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    rc = (0);
    rn = r->r_hook;
    if (rn != ((void *)0)) {
        do {
            if (rn == rns) {
                rc = (!(0));
                break;
            }
            rn = rn->rn_next;
        } while (rn != r->r_hook);
    }
    return rc;
}

canonical_original_code:
int ht_ring_contains(struct ht_ring_st *r, struct ht_ringnode_st *rns)
{
    struct ht_ringnode_st *rn;
    int rc;
    if (r == ((void *)0) || rns == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    rc = (0);
    rn = r->r_hook;
    if (rn != ((void *)0)) {
        do {
            if (rn == rns) {
                rc = (!(0));
                break;
            }
            rn = rn->rn_next;
        } while (rn != r->r_hook);
    }
    return rc;
}

variable_types:
rns
struct ht_ringnode_st *

variable_types: struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } *

variable_types: rc int


decompiled_code:
_BOOL8 ht_exit_cb()
{
return ht_SQ[2] + ht_WQ[2] + ht_NQ[2] + ht_RQ[2] == 1;
}

canonical_decompiled_code:
_BOOL8 func0()
{
return ht_SQ[2] + ht_WQ[2] + ht_NQ[2] + ht_RQ[2] == 1;
}

variable_types:
arg
void *


decompiled_code:
__int64 ht_version()
{
return 2097671LL;
}

original_code:
long ht_version(void)
{
    return 0x200207;
}

canonical_original_code:
long ht_version()
{
    return 0x200207;
}

return_type: long


decompiled_code:
__int64 ht_init()
{
unsigned int v0; // er12
__int64 v2; // rbp
int *v3; // rbx
int v4; // eax

if ( ht_initialized )
{
v0 = 0;
*_errno_location() = 1;
return v0;
}
ht_initialized = 1;
if ( (unsigned int)ht_scheduler_init() )
{
v0 = ht_worker_init(3LL);
if ( !v0 )
{
v2 = ht_attr_new();
ht_attr_set(v2, 0LL, 5LL);
ht_attr_set(v2, 1LL, "**SCHEDULER**");
ht_attr_set(v2, 2LL, 0LL);
ht_attr_set(v2, 3LL, 2LL);
ht_attr_set(v2, 4LL, 0x10000LL);
ht_attr_set(v2, 5LL, 0LL);
ht_sched = ht_spawn(v2, &ht_scheduler, 0LL);
if ( ht_sched
&& (ht_attr_set(v2, 0LL, 0LL),
ht_attr_set(v2, 1LL, "main"),
ht_attr_set(v2, 2LL, 1LL),
ht_attr_set(v2, 3LL, 9LL),
ht_attr_set(v2, 4LL, 0LL),
ht_attr_set(v2, 5LL, 0LL),
(ht_main = ht_spawn(v2, (void *)0xFFFFFFFFFFFFFFFFLL, 0LL)) != 0LL) )
{
v0 = 1;
ht_attr_destroy(v2);
ht_current = ht_sched;
swapcontext((ucontext_t *)(ht_main + 128LL), (const ucontext_t *)(ht_sched + 128LL));
}
else
{
v3 = _errno_location();
v4 = *v3;
ht_errno_flag = 1;
ht_errno_storage = v4;
ht_attr_destroy(v2);
ht_scheduler_kill();
*v3 = ht_errno_storage;
ht_errno_flag = 0;
}
return v0;
}
}
*_errno_location() = 11;
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er12
__int64 v2; // rbp
int *v3; // rbx
int v4; // eax

if ( ht_initialized )
{
v0 = 0;
*_errno_location() = 1;
return v0;
}
ht_initialized = 1;
if ( (unsigned int)ht_scheduler_init() )
{
v0 = ht_worker_init(3LL);
if ( !v0 )
{
v2 = ht_attr_new();
ht_attr_set(v2, 0LL, 5LL);
ht_attr_set(v2, 1LL, "**SCHEDULER**");
ht_attr_set(v2, 2LL, 0LL);
ht_attr_set(v2, 3LL, 2LL);
ht_attr_set(v2, 4LL, 0x10000LL);
ht_attr_set(v2, 5LL, 0LL);
ht_sched = ht_spawn(v2, &ht_scheduler, 0LL);
if ( ht_sched
&& (ht_attr_set(v2, 0LL, 0LL),
ht_attr_set(v2, 1LL, "main"),
ht_attr_set(v2, 2LL, 1LL),
ht_attr_set(v2, 3LL, 9LL),
ht_attr_set(v2, 4LL, 0LL),
ht_attr_set(v2, 5LL, 0LL),
(ht_main = ht_spawn(v2, (void *)0xFFFFFFFFFFFFFFFFLL, 0LL)) != 0LL) )
{
v0 = 1;
ht_attr_destroy(v2);
ht_current = ht_sched;
swapcontext((ucontext_t *)(ht_main + 128LL), (const ucontext_t *)(ht_sched + 128LL));
}
else
{
v3 = _errno_location();
v4 = *v3;
ht_errno_flag = 1;
ht_errno_storage = v4;
ht_attr_destroy(v2);
ht_scheduler_kill();
*v3 = ht_errno_storage;
ht_errno_flag = 0;
}
return v0;
}
}
*_errno_location() = 11;
return 0LL;
}

original_code:
int
ht_init(void)
{
    ht_attr_t t_attr;
    if (ht_initialized)
        return ((*__errno_location ()) = (1), ((0)));
    else
        ht_initialized = (!(0));
    ;
    if (!ht_scheduler_init()) {
        return ((*__errno_location ()) = (11), ((0)));
    }
    if (ht_worker_init(3)) {
       return ((*__errno_location ()) = (11), ((0)));
    }
    t_attr = ht_attr_new();
    ht_attr_set(t_attr, HT_ATTR_PRIO, +5);
    ht_attr_set(t_attr, HT_ATTR_NAME, "**SCHEDULER**");
    ht_attr_set(t_attr, HT_ATTR_JOINABLE, (0));
    ht_attr_set(t_attr, HT_ATTR_CANCEL_STATE, (1<<(1)));
    ht_attr_set(t_attr, HT_ATTR_STACK_SIZE, 64*1024);
    ht_attr_set(t_attr, HT_ATTR_STACK_ADDR, ((void *)0));
    ht_sched = ht_spawn(t_attr, ht_scheduler, ((void *)0));
    if (ht_sched == ((void *)0)) {
        for ( ht_errno_storage = (*__errno_location ()), ht_errno_flag = (!(0)); ht_errno_flag; (*__errno_location ()) = ht_errno_storage, ht_errno_flag = (0) ) {
            ht_attr_destroy(t_attr);
            ht_scheduler_kill();
        }
        return (0);
    }
    ht_attr_set(t_attr, HT_ATTR_PRIO, 0);
    ht_attr_set(t_attr, HT_ATTR_NAME, "main");
    ht_attr_set(t_attr, HT_ATTR_JOINABLE, (!(0)));
    ht_attr_set(t_attr, HT_ATTR_CANCEL_STATE, (1<<(0))|(1<<(3)));
    ht_attr_set(t_attr, HT_ATTR_STACK_SIZE, 0 );
    ht_attr_set(t_attr, HT_ATTR_STACK_ADDR, ((void *)0));
    ht_main = ht_spawn(t_attr, (void *(*)(void *))(-1), ((void *)0));
    if (ht_main == ((void *)0)) {
        for ( ht_errno_storage = (*__errno_location ()), ht_errno_flag = (!(0)); ht_errno_flag; (*__errno_location ()) = ht_errno_storage, ht_errno_flag = (0) ) {
            ht_attr_destroy(t_attr);
            ht_scheduler_kill();
        }
        return (0);
    }
    ht_attr_destroy(t_attr);
    ht_current = ht_sched;
    swapcontext(&((&ht_main->mctx)->uc), &((&ht_sched->mctx)->uc));;
    ;
    return (!(0));
}

canonical_original_code:
int ht_init()
{
    struct ht_attr_st *t_attr;
    if (ht_initialized)
        return ((*__errno_location ()) = (1), ((0)));
    else
        ht_initialized = (!(0));
    ;
    if (!ht_scheduler_init()) {
        return ((*__errno_location ()) = (11), ((0)));
    }
    if (ht_worker_init(3)) {
       return ((*__errno_location ()) = (11), ((0)));
    }
    t_attr = ht_attr_new();
    ht_attr_set(t_attr, HT_ATTR_PRIO, +5);
    ht_attr_set(t_attr, HT_ATTR_NAME, "**SCHEDULER**");
    ht_attr_set(t_attr, HT_ATTR_JOINABLE, (0));
    ht_attr_set(t_attr, HT_ATTR_CANCEL_STATE, (1<<(1)));
    ht_attr_set(t_attr, HT_ATTR_STACK_SIZE, 64*1024);
    ht_attr_set(t_attr, HT_ATTR_STACK_ADDR, ((void *)0));
    ht_sched = ht_spawn(t_attr, ht_scheduler, ((void *)0));
    if (ht_sched == ((void *)0)) {
        for ( ht_errno_storage = (*__errno_location ()), ht_errno_flag = (!(0)); ht_errno_flag; (*__errno_location ()) = ht_errno_storage, ht_errno_flag = (0) ) {
            ht_attr_destroy(t_attr);
            ht_scheduler_kill();
        }
        return (0);
    }
    ht_attr_set(t_attr, HT_ATTR_PRIO, 0);
    ht_attr_set(t_attr, HT_ATTR_NAME, "main");
    ht_attr_set(t_attr, HT_ATTR_JOINABLE, (!(0)));
    ht_attr_set(t_attr, HT_ATTR_CANCEL_STATE, (1<<(0))|(1<<(3)));
    ht_attr_set(t_attr, HT_ATTR_STACK_SIZE, 0 );
    ht_attr_set(t_attr, HT_ATTR_STACK_ADDR, ((void *)0));
    ht_main = ht_spawn(t_attr, (void *(*)(void *))(-1), ((void *)0));
    if (ht_main == ((void *)0)) {
        for ( ht_errno_storage = (*__errno_location ()), ht_errno_flag = (!(0)); ht_errno_flag; (*__errno_location ()) = ht_errno_storage, ht_errno_flag = (0) ) {
            ht_attr_destroy(t_attr);
            ht_scheduler_kill();
        }
        return (0);
    }
    ht_attr_destroy(t_attr);
    ht_current = ht_sched;
    swapcontext(&((&ht_main->mctx)->uc), &((&ht_sched->mctx)->uc));;
    ;
    return (!(0));
}

variable_types: struct ht_attr_st { struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 } state; struct timeval { long tv_sec; long tv_usec; } spawned; struct timeval lastran; struct timeval running; struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 } ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set { long __fds_bits[-1]; } *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; } mp_queue; } *mp; } MSG; struct { struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; } *mutex; } MUTEX; struct { struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; } *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; } *events; struct ht_mctx_st { struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } uc; int restored; int error; } mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; } *cleanups; struct ht_ring_st mutexring; } *a_tid; int a_prio; int a_dispatches; char a_name[40]; int a_joinable; unsigned int a_cancelstate; unsigned int a_stacksize; char *a_stackaddr; } *

user_defined_types: struct ht_attr_st { struct ht_st *a_tid; int a_prio; int a_dispatches; char a_name[40]; int a_joinable; unsigned int a_cancelstate; unsigned int a_stacksize; char *a_stackaddr; }

user_defined_types: struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en state; struct timeval spawned; struct timeval lastran; struct timeval running; struct ht_event_st *events; struct ht_mctx_st mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st *cleanups; struct ht_ring_st mutexring; }

user_defined_types: struct ht_mctx_st { struct ucontext_t uc; int restored; int error; }

user_defined_types: struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st *mp; } MSG; struct { struct ht_mutex_st *mutex; } MUTEX; struct { struct ht_cond_st *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }

user_defined_types: struct ht_msgport_st { struct ht_ringnode_st mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st mp_queue; }


decompiled_code:
__int64 ht_self()
{
return ht_current;
}

canonical_decompiled_code:
__int64 func4()
{
return ht_current;
}

original_code:
ht_t
ht_self(void)
{
    return ht_current;
}

canonical_original_code:
struct ht_st *ht_self()
{
    return ht_current;
}

return_type: struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 } state; struct timeval { long tv_sec; long tv_usec; } spawned; struct timeval lastran; struct timeval running; struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 } ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set { long __fds_bits[-1]; } *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; } mp_queue; } *mp; } MSG; struct { struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; } *mutex; } MUTEX; struct { struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; } *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; } *events; struct ht_mctx_st { struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } uc; int restored; int error; } mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; } *cleanups; struct ht_ring_st mutexring; } *


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

if ( (unsigned int)ht_pqueue_contains(ht_NQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_RQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_WQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_SQ, a1)
|| (v1 = ht_pqueue_contains(ht_DQ, a1)) != 0 )
{
return 1;
}
*_errno_location() = 3;
return v1;
}

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

if ( (unsigned int)ht_pqueue_contains(ht_NQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_RQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_WQ, a1)
|| (unsigned int)ht_pqueue_contains(ht_SQ, a1)
|| (v1 = ht_pqueue_contains(ht_DQ, a1)) != 0 )
{
return 1;
}
*_errno_location() = 3;
return v1;
}

original_code:
int
ht_thread_exists(ht_t t)
{
    if (!ht_pqueue_contains(&ht_NQ, t))
        if (!ht_pqueue_contains(&ht_RQ, t))
            if (!ht_pqueue_contains(&ht_WQ, t))
                if (!ht_pqueue_contains(&ht_SQ, t))
                    if (!ht_pqueue_contains(&ht_DQ, t))
                        return ((*__errno_location ()) = (3), ((0)));
    return (!(0));
}

canonical_original_code:
int ht_thread_exists(struct ht_st *t)
{
    if (!ht_pqueue_contains(&ht_NQ, t))
        if (!ht_pqueue_contains(&ht_RQ, t))
            if (!ht_pqueue_contains(&ht_WQ, t))
                if (!ht_pqueue_contains(&ht_SQ, t))
                    if (!ht_pqueue_contains(&ht_DQ, t))
                        return ((*__errno_location ()) = (3), ((0)));
    return (!(0));
}

variable_types: struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 } state; struct timeval { long tv_sec; long tv_usec; } spawned; struct timeval lastran; struct timeval running; struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 } ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set { long __fds_bits[-1]; } *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; } mp_queue; } *mp; } MSG; struct { struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; } *mutex; } MUTEX; struct { struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; } *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; } *events; struct ht_mctx_st { struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } uc; int restored; int error; } mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; } *cleanups; struct ht_ring_st mutexring; } *


decompiled_code:
__int64 __fastcall ht_thread_cleanup(__int64 a1)
{
if ( *(_QWORD *)(a1 + 1192) )
ht_cleanup_popall(a1, 1LL);
if ( *(_QWORD *)(a1 + 1168) )
ht_key_destroydata(a1);
return ht_mutex_releaseall(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
if ( *(_QWORD *)(a1 + 1192) )
ht_cleanup_popall(a1, 1LL);
if ( *(_QWORD *)(a1 + 1168) )
ht_key_destroydata(a1);
return ht_mutex_releaseall(a1);
}

original_code:
void
ht_thread_cleanup(ht_t thread)
{
    if (thread->cleanups != ((void *)0))
        ht_cleanup_popall(thread, (!(0)));
    if (thread->data_value != ((void *)0))
        ht_key_destroydata(thread);
    ht_mutex_releaseall(thread);
    return;
}

canonical_original_code:
void ht_thread_cleanup(struct ht_st *thread)
{
    if (thread->cleanups != ((void *)0))
        ht_cleanup_popall(thread, (!(0)));
    if (thread->data_value != ((void *)0))
        ht_key_destroydata(thread);
    ht_mutex_releaseall(thread);
    return;
}

user_defined_types: enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 }

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

user_defined_types: struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; }

user_defined_types: enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 }

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; }

user_defined_types: struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; }

user_defined_types: struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; }

user_defined_types: struct _libc_xmmreg { unsigned int element[4]; }


decompiled_code:
__int64 ht_kill()
{
unsigned int v0; // er12
__int64 result; // rax

v0 = ht_initialized;
if ( ht_initialized )
{
if ( ht_current == ht_main )
{
ht_worker_kill();
ht_thread_cleanup(ht_main);
ht_scheduler_kill();
ht_initialized = 0;
ht_tcb_free(ht_sched);
ht_tcb_free(ht_main);
result = 1LL;
}
else
{
*_errno_location() = 1;
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = v0;
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // er12
__int64 result; // rax

v0 = ht_initialized;
if ( ht_initialized )
{
if ( ht_current == ht_main )
{
ht_worker_kill();
func6(ht_main);
ht_scheduler_kill();
ht_initialized = 0;
ht_tcb_free(ht_sched);
ht_tcb_free(ht_main);
result = 1LL;
}
else
{
*_errno_location() = 1;
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = v0;
}
return result;
}

original_code:
int
ht_kill(void)
{
    if (!ht_initialized)
        return ((*__errno_location ()) = (22), ((0)));
    if (ht_current != ht_main)
        return ((*__errno_location ()) = (1), ((0)));
    ;
    ht_worker_kill();
    ht_thread_cleanup(ht_main);
    ht_scheduler_kill();
    ht_initialized = (0);
    ht_tcb_free(ht_sched);
    ht_tcb_free(ht_main);
    ;
    return (!(0));
}

canonical_original_code:
int ht_kill()
{
    if (!ht_initialized)
        return ((*__errno_location ()) = (22), ((0)));
    if (ht_current != ht_main)
        return ((*__errno_location ()) = (1), ((0)));
    ;
    ht_worker_kill();
    ht_thread_cleanup(ht_main);
    ht_scheduler_kill();
    ht_initialized = (0);
    ht_tcb_free(ht_sched);
    ht_tcb_free(ht_main);
    ;
    return (!(0));
}


decompiled_code:
void __fastcall __noreturn ht_exit(__int64 status)
{
__int64 v1; // r8
__int64 v2; // rax
__int64 v3; // r13

v1 = ht_current;
if ( ht_current == ht_main && !ht_exit_cb() )
{
v3 = ht_event(512LL, ht_exit_cb);
ht_wait(v3);
ht_event_free(v3, 0LL);
v1 = ht_current;
}
ht_thread_cleanup(v1);
v2 = ht_current;
if ( ht_current == ht_main )
{
ht_kill();
exit(status);
}
*(_QWORD *)(ht_current + 1160LL) = status;
*(_DWORD *)(v2 + 68) = 5;
swapcontext((ucontext_t *)(v2 + 128), (const ucontext_t *)(ht_sched + 128LL));
abort();
}

canonical_decompiled_code:
void __fastcall __noreturn func8(__int64 status)
{
__int64 v1; // r8
__int64 v2; // rax
__int64 v3; // r13

v1 = ht_current;
if ( ht_current == ht_main && !func0() )
{
v3 = ht_event(512LL, ht_exit_cb);
ht_wait(v3);
ht_event_free(v3, 0LL);
v1 = ht_current;
}
func6(v1);
v2 = ht_current;
if ( ht_current == ht_main )
{
func7();
exit(status);
}
*(_QWORD *)(ht_current + 1160LL) = status;
*(_DWORD *)(v2 + 68) = 5;
swapcontext((ucontext_t *)(v2 + 128), (const ucontext_t *)(ht_sched + 128LL));
abort();
}

original_code:
void
ht_exit(void *value)
{
    ht_event_t ev;
    ;
    if (ht_current == ht_main) {
        if (!ht_exit_cb(((void *)0))) {
            ev = ht_event((1<<(9)), ht_exit_cb);
            ht_wait(ev);
            ht_event_free(ev, HT_FREE_THIS);
        }
    }
    ht_thread_cleanup(ht_current);
    if (ht_current != ht_main) {
        ht_current->join_arg = value;
        ht_current->state = HT_STATE_DEAD;
        ;
        swapcontext(&((&ht_current->mctx)->uc), &((&ht_sched->mctx)->uc));;
    }
    else {
        ht_kill();
        exit((int)((long)value));
    }
    abort();
}

canonical_original_code:
void ht_exit(void *value)
{
    struct ht_event_st *ev;
    ;
    if (ht_current == ht_main) {
        if (!ht_exit_cb(((void *)0))) {
            ev = ht_event((1<<(9)), ht_exit_cb);
            ht_wait(ev);
            ht_event_free(ev, HT_FREE_THIS);
        }
    }
    ht_thread_cleanup(ht_current);
    if (ht_current != ht_main) {
        ht_current->join_arg = value;
        ht_current->state = HT_STATE_DEAD;
        ;
        swapcontext(&((&ht_current->mctx)->uc), &((&ht_sched->mctx)->uc));;
    }
    else {
        ht_kill();
        exit((int)((long)value));
    }
    abort();
}

variable_types:
value
void *

variable_types: struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 } ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set { long __fds_bits[-1]; } *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval { long tv_sec; long tv_usec; } tv; } TIME; struct { struct ht_msgport_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } mp_node; char *mp_name; struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 } state; struct timeval spawned; struct timeval lastran; struct timeval running; struct ht_event_st *events; struct ht_mctx_st { struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } uc; int restored; int error; } mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; } *cleanups; struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; } mutexring; } *mp_tid; struct ht_ring_st mp_queue; } *mp; } MSG; struct { struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; } *mutex; } MUTEX; struct { struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; } *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; } *

user_defined_types: struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en state; struct timeval spawned; struct timeval lastran; struct timeval running; struct ht_event_st *events; struct ht_mctx_st mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st *cleanups; struct ht_ring_st mutexring; }


decompiled_code:
void __noreturn ht_spawn_trampoline()
{
__int64 v0; // rax

v0 = (*(__int64 (__fastcall **)(_QWORD))(ht_current + 1136LL))(*(_QWORD *)(ht_current + 1144LL));
ht_exit(v0);
}

canonical_decompiled_code:
void __noreturn func9()
{
__int64 v0; // rax

v0 = (*(__int64 (__fastcall **)(_QWORD))(ht_current + 1136LL))(*(_QWORD *)(ht_current + 1144LL));
func8(v0);
}

original_code:
static
void
ht_spawn_trampoline(void)
{
    void *data;
    data = (*ht_current->start_func)(ht_current->start_arg);
    ht_exit(data);
    abort();
}

canonical_original_code:
static void ht_spawn_trampoline()
{
    void *data;
    data = (*ht_current->start_func)(ht_current->start_arg);
    ht_exit(data);
    abort();
}

variable_types:
data
void *


decompiled_code:
__int64 __fastcall ht_join(__int64 a1, _DWORD *a2)
{
__int64 v2; // rbp
__int64 v3; // rax
__int64 v5; // rax

if ( ht_current == a1 )
goto LABEL_10;
v2 = a1;
if ( a1 )
{
if ( !*(_DWORD *)(a1 + 1152) )
{
*_errno_location() = 22;
return 0LL;
}
if ( ht_ctrl(1008, a2) != 1 )
goto LABEL_5;
LABEL_10:
*_errno_location() = 35;
return 0LL;
}
if ( ht_ctrl(1008, a2) == 1 )
goto LABEL_10;
v2 = *(_QWORD *)ht_DQ;
if ( !*(_QWORD *)ht_DQ )
{
v5 = ht_event(4456704LL, &ev_key_1);
ht_wait(v5);
v2 = *(_QWORD *)ht_DQ;
if ( !*(_QWORD *)ht_DQ )
goto LABEL_8;
LABEL_7:
if ( *(_DWORD *)(v2 + 68) != 5 )
{
LABEL_8:
*_errno_location() = 5;
return 0LL;
}
goto LABEL_11;
}
LABEL_5:
if ( *(_DWORD *)(v2 + 68) != 5 )
{
v3 = ht_event(4456704LL, &ev_key_1);
ht_wait(v3);
goto LABEL_7;
}
LABEL_11:
if ( a2 )
*(_QWORD *)a2 = *(_QWORD *)(v2 + 1160);
ht_pqueue_delete(ht_DQ, v2);
ht_tcb_free(v2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, _DWORD *a2)
{
__int64 v2; // rbp
__int64 v3; // rax
__int64 v5; // rax

if ( ht_current == a1 )
goto LABEL_10;
v2 = a1;
if ( a1 )
{
if ( !*(_DWORD *)(a1 + 1152) )
{
*_errno_location() = 22;
return 0LL;
}
if ( func2(1008, a2) != 1 )
goto LABEL_5;
LABEL_10:
*_errno_location() = 35;
return 0LL;
}
if ( func2(1008, a2) == 1 )
goto LABEL_10;
v2 = *(_QWORD *)ht_DQ;
if ( !*(_QWORD *)ht_DQ )
{
v5 = ht_event(4456704LL, &ev_key_1);
ht_wait(v5);
v2 = *(_QWORD *)ht_DQ;
if ( !*(_QWORD *)ht_DQ )
goto LABEL_8;
LABEL_7:
if ( *(_DWORD *)(v2 + 68) != 5 )
{
LABEL_8:
*_errno_location() = 5;
return 0LL;
}
goto LABEL_11;
}
LABEL_5:
if ( *(_DWORD *)(v2 + 68) != 5 )
{
v3 = ht_event(4456704LL, &ev_key_1);
ht_wait(v3);
goto LABEL_7;
}
LABEL_11:
if ( a2 )
*(_QWORD *)a2 = *(_QWORD *)(v2 + 1160);
ht_pqueue_delete(ht_DQ, v2);
ht_tcb_free(v2);
return 1LL;
}

variable_types:
value
void **

user_defined_types: struct ht_mctx_st { struct ucontext_t uc; int restored; int error; }

user_defined_types: struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; }

user_defined_types: struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; }


decompiled_code:
__int64 __fastcall ht_yield(__int64 a1)
{
int v1; // eax
_DWORD *v2; // r12

if ( !a1 )
goto LABEL_7;
v1 = *(_DWORD *)(a1 + 68);
if ( v1 == 1 )
{
v2 = ht_NQ;
}
else
{
if ( v1 != 2 )
goto LABEL_8;
v2 = ht_RQ;
}
if ( (unsigned int)ht_pqueue_contains(v2, a1) )
{
ht_pqueue_favorite(v2, a1);
LABEL_7:
swapcontext((ucontext_t *)(ht_current + 128LL), (const ucontext_t *)(ht_sched + 128LL));
return 1LL;
}
LABEL_8:
*_errno_location() = 22;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
int v1; // eax
_DWORD *v2; // r12

if ( !a1 )
goto LABEL_7;
v1 = *(_DWORD *)(a1 + 68);
if ( v1 == 1 )
{
v2 = ht_NQ;
}
else
{
if ( v1 != 2 )
goto LABEL_8;
v2 = ht_RQ;
}
if ( (unsigned int)ht_pqueue_contains(v2, a1) )
{
ht_pqueue_favorite(v2, a1);
LABEL_7:
swapcontext((ucontext_t *)(ht_current + 128LL), (const ucontext_t *)(ht_sched + 128LL));
return 1LL;
}
LABEL_8:
*_errno_location() = 22;
return 0LL;
}

original_code:
int
ht_yield(ht_t to)
{
    ht_pqueue_t *q = ((void *)0);
    ;
    if (to != ((void *)0)) {
        switch (to->state) {
            case HT_STATE_NEW: q = &ht_NQ; break;
            case HT_STATE_READY: q = &ht_RQ; break;
            default: q = ((void *)0);
        }
        if (q == ((void *)0) || !ht_pqueue_contains(q, to))
            return ((*__errno_location ()) = (22), ((0)));
    }
    if (to != ((void *)0) && q != ((void *)0))
        ht_pqueue_favorite(q, to);
    if (to != ((void *)0))
        ;
    else
        ;
    swapcontext(&((&ht_current->mctx)->uc), &((&ht_sched->mctx)->uc));;
    ;
    ;
    return (!(0));
}

canonical_original_code:
int ht_yield(struct ht_st *to)
{
    struct ht_pqueue_st *q = ((void *)0);
    ;
    if (to != ((void *)0)) {
        switch (to->state) {
            case HT_STATE_NEW: q = &ht_NQ; break;
            case HT_STATE_READY: q = &ht_RQ; break;
            default: q = ((void *)0);
        }
        if (q == ((void *)0) || !ht_pqueue_contains(q, to))
            return ((*__errno_location ()) = (22), ((0)));
    }
    if (to != ((void *)0) && q != ((void *)0))
        ht_pqueue_favorite(q, to);
    if (to != ((void *)0))
        ;
    else
        ;
    swapcontext(&((&ht_current->mctx)->uc), &((&ht_sched->mctx)->uc));;
    ;
    ;
    return (!(0));
}

variable_types: struct ht_pqueue_st { struct ht_st { struct ht_st *q_next; struct ht_st *q_prev; int q_prio; int prio; char name[40]; int dispatches; enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 } state; struct timeval { long tv_sec; long tv_usec; } spawned; struct timeval lastran; struct timeval running; struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 } ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set { long __fds_bits[-1]; } *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st { struct ht_ringnode_st { struct ht_ringnode_st *rn_next; struct ht_ringnode_st *rn_prev; } mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st { struct ht_ringnode_st *r_hook; unsigned int r_nodes; } mp_queue; } *mp; } MSG; struct { struct ht_mutex_st { struct ht_ringnode_st mx_node; int mx_state; struct ht_st *mx_owner; unsigned long mx_count; } *mutex; } MUTEX; struct { struct ht_cond_st { unsigned long cn_state; unsigned int cn_waiters; } *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; } *events; struct ht_mctx_st { struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } uc; int restored; int error; } mctx; char *stack; unsigned int stacksize; long *stackguard; int stackloan; void *(*start_func)(void *); void *start_arg; int joinable; void *join_arg; void **data_value; int data_count; int cancelreq; unsigned int cancelstate; struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; } *cleanups; struct ht_ring_st mutexring; } *q_head; int q_num; } *

user_defined_types: struct ht_pqueue_st { struct ht_st *q_head; int q_num; }

user_defined_types: struct ht_event_st { struct ht_event_st *ev_next; struct ht_event_st *ev_prev; enum ht_status_t ev_status; int ev_type; int ev_goal; union { struct { int fd; } FD; struct { int *n; int nfd; struct fd_set *rfds; } SELECT; struct { int fini; } TASK; struct { struct timeval tv; } TIME; struct { struct ht_msgport_st *mp; } MSG; struct { struct ht_mutex_st *mutex; } MUTEX; struct { struct ht_cond_st *cond; } COND; struct { struct ht_st *tid; } TID; struct { int (*func)(void *); void *arg; struct timeval tv; } FUNC; } ev_args; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }


decompiled_code:
__int64 __fastcall ht_suspend(__int64 a1)
{
int v1; // eax
__int64 result; // rax
_DWORD *v3; // rbp

if ( a1 )
{
if ( ht_sched == a1 || ht_current == a1 )
goto LABEL_7;
v1 = *(_DWORD *)(a1 + 68);
switch ( v1 )
{
case 2:
v3 = ht_RQ;
break;
case 3:
v3 = ht_WQ;
break;
case 1:
v3 = ht_NQ;
break;
default:
LABEL_7:
*_errno_location() = 1;
return 0LL;
}
if ( (unsigned int)ht_pqueue_contains(v3, a1) )
{
ht_pqueue_delete(v3, a1);
ht_pqueue_insert(ht_SQ, 0LL, a1);
result = 1LL;
}
else
{
*_errno_location() = 3;
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
int v1; // eax
__int64 result; // rax
_DWORD *v3; // rbp

if ( a1 )
{
if ( ht_sched == a1 || ht_current == a1 )
goto LABEL_7;
v1 = *(_DWORD *)(a1 + 68);
switch ( v1 )
{
case 2:
v3 = ht_RQ;
break;
case 3:
v3 = ht_WQ;
break;
case 1:
v3 = ht_NQ;
break;
default:
LABEL_7:
*_errno_location() = 1;
return 0LL;
}
if ( (unsigned int)ht_pqueue_contains(v3, a1) )
{
ht_pqueue_delete(v3, a1);
ht_pqueue_insert(ht_SQ, 0LL, a1);
result = 1LL;
}
else
{
*_errno_location() = 3;
result = 0LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

original_code:
int
ht_suspend(ht_t t)
{
    ht_pqueue_t *q;
    if (t == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    if (t == ht_sched || t == ht_current)
        return ((*__errno_location ()) = (1), ((0)));
    switch (t->state) {
        case HT_STATE_NEW: q = &ht_NQ; break;
        case HT_STATE_READY: q = &ht_RQ; break;
        case HT_STATE_WAITING: q = &ht_WQ; break;
        default: q = ((void *)0);
    }
    if (q == ((void *)0))
        return ((*__errno_location ()) = (1), ((0)));
    if (!ht_pqueue_contains(q, t))
        return ((*__errno_location ()) = (3), ((0)));
    ht_pqueue_delete(q, t);
    ht_pqueue_insert(&ht_SQ, 0, t);
    ;
    return (!(0));
}

canonical_original_code:
int ht_suspend(struct ht_st *t)
{
    struct ht_pqueue_st *q;
    if (t == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    if (t == ht_sched || t == ht_current)
        return ((*__errno_location ()) = (1), ((0)));
    switch (t->state) {
        case HT_STATE_NEW: q = &ht_NQ; break;
        case HT_STATE_READY: q = &ht_RQ; break;
        case HT_STATE_WAITING: q = &ht_WQ; break;
        default: q = ((void *)0);
    }
    if (q == ((void *)0))
        return ((*__errno_location ()) = (1), ((0)));
    if (!ht_pqueue_contains(q, t))
        return ((*__errno_location ()) = (3), ((0)));
    ht_pqueue_delete(q, t);
    ht_pqueue_insert(&ht_SQ, 0, t);
    ;
    return (!(0));
}

user_defined_types: enum ht_state_en { HT_STATE_SCHEDULER=0, HT_STATE_NEW=1, HT_STATE_READY=2, HT_STATE_WAITING=3, HT_STATE_WAITING_FOR_SCHED_TO_WORKER=4, HT_STATE_DEAD=5 }

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

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }

user_defined_types: struct ht_msgport_st { struct ht_ringnode_st mp_node; char *mp_name; struct ht_st *mp_tid; struct ht_ring_st mp_queue; }


decompiled_code:
__int64 __fastcall ht_resume(__int64 a1)
{
__int64 result; // rax
int v3; // eax
_DWORD *v4; // rdi

if ( a1 )
{
if ( ht_sched == a1 || ht_current == a1 || !(unsigned int)ht_pqueue_contains(ht_SQ, a1) )
{
*_errno_location() = 1;
result = 0LL;
}
else
{
ht_pqueue_delete(ht_SQ, a1);
v3 = *(_DWORD *)(a1 + 68);
if ( v3 == 2 )
{
v4 = ht_RQ;
}
else if ( v3 == 3 )
{
v4 = ht_WQ;
}
else
{
v4 = 0LL;
if ( v3 == 1 )
v4 = ht_NQ;
}
ht_pqueue_insert(v4, 0LL, a1);
result = 1LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 result; // rax
int v3; // eax
_DWORD *v4; // rdi

if ( a1 )
{
if ( ht_sched == a1 || ht_current == a1 || !(unsigned int)ht_pqueue_contains(ht_SQ, a1) )
{
*_errno_location() = 1;
result = 0LL;
}
else
{
ht_pqueue_delete(ht_SQ, a1);
v3 = *(_DWORD *)(a1 + 68);
if ( v3 == 2 )
{
v4 = ht_RQ;
}
else if ( v3 == 3 )
{
v4 = ht_WQ;
}
else
{
v4 = 0LL;
if ( v3 == 1 )
v4 = ht_NQ;
}
ht_pqueue_insert(v4, 0LL, a1);
result = 1LL;
}
}
else
{
*_errno_location() = 22;
result = 0LL;
}
return result;
}

original_code:
int
ht_resume(ht_t t)
{
    ht_pqueue_t *q;
    if (t == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    if (t == ht_sched || t == ht_current)
        return ((*__errno_location ()) = (1), ((0)));
    if (!ht_pqueue_contains(&ht_SQ, t))
        return ((*__errno_location ()) = (1), ((0)));
    ht_pqueue_delete(&ht_SQ, t);
    switch (t->state) {
        case HT_STATE_NEW: q = &ht_NQ; break;
        case HT_STATE_READY: q = &ht_RQ; break;
        case HT_STATE_WAITING: q = &ht_WQ; break;
        default: q = ((void *)0);
    }
    ht_pqueue_insert(q, 0, t);
    ;
    return (!(0));
}

canonical_original_code:
int ht_resume(struct ht_st *t)
{
    struct ht_pqueue_st *q;
    if (t == ((void *)0))
        return ((*__errno_location ()) = (22), ((0)));
    if (t == ht_sched || t == ht_current)
        return ((*__errno_location ()) = (1), ((0)));
    if (!ht_pqueue_contains(&ht_SQ, t))
        return ((*__errno_location ()) = (1), ((0)));
    ht_pqueue_delete(&ht_SQ, t);
    switch (t->state) {
        case HT_STATE_NEW: q = &ht_NQ; break;
        case HT_STATE_READY: q = &ht_RQ; break;
        case HT_STATE_WAITING: q = &ht_WQ; break;
        default: q = ((void *)0);
    }
    ht_pqueue_insert(q, 0, t);
    ;
    return (!(0));
}

user_defined_types: struct ht_cleanup_st { struct ht_cleanup_st *next; void (*func)(void *); void *arg; }

user_defined_types: enum ht_status_t { HT_STATUS_PENDING=0, HT_STATUS_OCCURRED=1, HT_STATUS_FAILED=2 }

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
unsigned int __fastcall ht_fdmode(int fd, int a2)
{
unsigned int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result != -1 )
{
if ( (result & 0x800) != 0 )
{
if ( a2 == 1 )
{
BYTE1(result) &= 0xF7u;
fcntl(fd, 4, result);
}
result = 2;
}
else if ( a2 == 2 )
{
BYTE1(result) |= 8u;
fcntl(fd, 4, result);
result = 1;
}
else
{
result = 1;
}
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func14(int fd, int a2)
{
unsigned int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result != -1 )
{
if ( (result & 0x800) != 0 )
{
if ( a2 == 1 )
{
BYTE1(result) &= 0xF7u;
fcntl(fd, 4, result);
}
result = 2;
}
else if ( a2 == 2 )
{
BYTE1(result) |= 8u;
fcntl(fd, 4, result);
result = 1;
}
else
{
result = 1;
}
}
return result;
}

original_code:
int
ht_fdmode(int fd, int newmode)
{
    int fdmode;
    int oldmode;
    if ((fdmode = fcntl(fd, 3, ((void *)0))) == -1)
        oldmode = HT_FDMODE_ERROR;
    else if (fdmode & 04000)
        oldmode = HT_FDMODE_NONBLOCK;
    else
        oldmode = HT_FDMODE_BLOCK;
    if (oldmode == HT_FDMODE_BLOCK && newmode == HT_FDMODE_NONBLOCK)
        fcntl(fd, 4, (fdmode | 04000));
    if (oldmode == HT_FDMODE_NONBLOCK && newmode == HT_FDMODE_BLOCK)
        fcntl(fd, 4, (fdmode & ~(04000)));
    return oldmode;
}

variable_types: fd int

variable_types: newmode int

variable_types: fdmode int

variable_types: oldmode int