getWordFromStream

decompiled_code:
__int64 getWordFromStream()
{
bool v0; // dl
char v1; // al
int v2; // ecx
bool v3; // bl
bool v4; // dl
int v5; // ebp
__int64 result; // rax

do
{
while ( 1 )
{
v1 = getCharFromStream();
v0 = 0;
if ( v1 == 10 )
break;
if ( v1 != 13 )
{
v0 = v1 != 32;
if ( v1 != 9 )
break;
}
}
}
while ( !v0 );
v2 = 0;
v3 = v0;
while ( 1 )
{
if ( v1 == 32 )
{
v4 = v3;
goto LABEL_11;
}
if ( v1 == 10 || v1 == 13 )
break;
v4 = 0;
LABEL_11:
if ( v1 == 9 || v4 )
break;
v5 = v2 + 1;
WordBuffer[(unsigned __int8)v2] = v1;
v1 = getCharFromStream();
v2 = v5;
}
result = (unsigned __int8)v2;
WordBuffer[(unsigned __int8)v2] = 0;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
bool v0; // dl
char v1; // al
int v2; // ecx
bool v3; // bl
bool v4; // dl
int v5; // ebp
__int64 result; // rax

do
{
while ( 1 )
{
v1 = func0();
v0 = 0;
if ( v1 == 10 )
break;
if ( v1 != 13 )
{
v0 = v1 != 32;
if ( v1 != 9 )
break;
}
}
}
while ( !v0 );
v2 = 0;
v3 = v0;
while ( 1 )
{
if ( v1 == 32 )
{
v4 = v3;
goto LABEL_11;
}
if ( v1 == 10 || v1 == 13 )
break;
v4 = 0;
LABEL_11:
if ( v1 == 9 || v4 )
break;
v5 = v2 + 1;
WordBuffer[(unsigned __int8)v2] = v1;
v1 = func0();
v2 = v5;
}
result = (unsigned __int8)v2;
WordBuffer[(unsigned __int8)v2] = 0;
return result;
}

original_code:
void getWordFromStream()
{
 uint8_t charIndex=0;
 uint8_t exitFlag=(!(1==1));
 char c;
 while(!exitFlag)
 {
  c=getCharFromStream();
  exitFlag=(1==1);
  if(c==' ')exitFlag=(!(1==1));
  if(c=='\n')exitFlag=(!(1==1));
  if(c=='\r')exitFlag=(!(1==1));
  if(c=='\t')exitFlag=(!(1==1));
 }
 exitFlag=(!(1==1));
 while(!exitFlag)
 {
  if(c==' ')exitFlag=(1==1);
  if(c=='\n')exitFlag=(1==1);
  if(c=='\r')exitFlag=(1==1);
  if(c=='\t')exitFlag=(1==1);
  if(!exitFlag)
  {
   WordBuffer[charIndex++]=c;
   c=getCharFromStream();
  }
  else WordBuffer[charIndex]=0;
 }
}

canonical_original_code:
void getWordFromStream()
{
 unsigned char charIndex = 0;
 unsigned char exitFlag = (!(1==1));
 char c;
 while(!exitFlag)
 {
  c=getCharFromStream();
  exitFlag=(1==1);
  if(c==' ')exitFlag=(!(1==1));
  if(c=='\n')exitFlag=(!(1==1));
  if(c=='\r')exitFlag=(!(1==1));
  if(c=='\t')exitFlag=(!(1==1));
 }
 exitFlag=(!(1==1));
 while(!exitFlag)
 {
  if(c==' ')exitFlag=(1==1);
  if(c=='\n')exitFlag=(1==1);
  if(c=='\r')exitFlag=(1==1);
  if(c=='\t')exitFlag=(1==1);
  if(!exitFlag)
  {
   WordBuffer[charIndex++]=c;
   c=getCharFromStream();
  }
  else WordBuffer[charIndex]=0;
 }
}

variable_types:
charIndex
unsigned char

variable_types:
exitFlag
unsigned char


getWordFromStreamWithOutComments

repo: ChrisMicro/qrzForth

decompiled_code:
char getWordFromStreamWithOutComments()
{
bool v0; // al
bool v1; // cf
bool v2; // zf
char *v3; // rsi
const char *v4; // rdi
__int64 v5; // rcx
char v6; // al
bool v7; // cf
bool v8; // zf
char *v9; // rsi
const char *v10; // rdi
__int64 v11; // rcx
char result; // al
char v13; // al

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
getWordFromStream();
v0 = strcmp(WordBuffer, "{") != 0;
v1 = 0;
v2 = !v0;
if ( v0 )
break;
while ( (unsigned __int8)((__int64 (*)(void))getCharFromStream)() != 125 )
;
}
v3 = WordBuffer;
v4 = "\\";
v5 = 2LL;
do
{
if ( !v5 )
break;
v1 = (unsigned __int8)*v3 < (unsigned int)*v4;
v2 = *v3++ == *v4++;
--v5;
}
while ( v2 );
v6 = (!v1 && !v2) - v1;
v7 = 0;
v8 = v6 == 0;
if ( v6 )
break;
do
v13 = getCharFromStream(v4, v3);
while ( v13 != 13 && v13 != 10 );
}
v9 = WordBuffer;
v10 = "(";
v11 = 2LL;
do
{
if ( !v11 )
break;
v7 = (unsigned __int8)*v9 < (unsigned int)*v10;
v8 = *v9++ == *v10++;
--v11;
}
while ( v8 );
result = (!v7 && !v8) - v7;
if ( result )
break;
while ( (unsigned __int8)getCharFromStream(v10, v9) != 41 )
;
}
return result;
}

canonical_decompiled_code:
char func2()
{
bool v0; // al
bool v1; // cf
bool v2; // zf
char *v3; // rsi
const char *v4; // rdi
__int64 v5; // rcx
char v6; // al
bool v7; // cf
bool v8; // zf
char *v9; // rsi
const char *v10; // rdi
__int64 v11; // rcx
char result; // al
char v13; // al

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
func1();
v0 = strcmp(WordBuffer, "{") != 0;
v1 = 0;
v2 = !v0;
if ( v0 )
break;
while ( (unsigned __int8)((__int64 (*)(void))getCharFromStream)() != 125 )
;
}
v3 = WordBuffer;
v4 = "\\";
v5 = 2LL;
do
{
if ( !v5 )
break;
v1 = (unsigned __int8)*v3 < (unsigned int)*v4;
v2 = *v3++ == *v4++;
--v5;
}
while ( v2 );
v6 = (!v1 && !v2) - v1;
v7 = 0;
v8 = v6 == 0;
if ( v6 )
break;
do
v13 = func0(v4, v3);
while ( v13 != 13 && v13 != 10 );
}
v9 = WordBuffer;
v10 = "(";
v11 = 2LL;
do
{
if ( !v11 )
break;
v7 = (unsigned __int8)*v9 < (unsigned int)*v10;
v8 = *v9++ == *v10++;
--v11;
}
while ( v8 );
result = (!v7 && !v8) - v7;
if ( result )
break;
while ( (unsigned __int8)func0(v10, v9) != 41 )
;
}
return result;
}

original_code:
void getWordFromStreamWithOutComments()
{
 char c;
 uint8_t repeatFlag=(1==1);
 while(repeatFlag)
 {
  getWordFromStream();
  repeatFlag=(!(1==1));
  if(strcmp(WordBuffer,"{")==0)
  {
   do
   {
    c=getCharFromStream();
   }while(c!='}');
   repeatFlag=(1==1);
  }else
  if( strcmp(WordBuffer,"\\")==0)
  {
   do
   {
    c=getCharFromStream();
   }while((c!='\r')&&(c!='\n'));
   repeatFlag=(1==1);
  }else
  if(strcmp(WordBuffer,"(")==0)
  {
   do
   {
    c=getCharFromStream();
   }while(c!=')');
   repeatFlag=(1==1);
  }
 }
}

canonical_original_code:
void getWordFromStreamWithOutComments()
{
 char c;
 unsigned char repeatFlag = (1==1);
 while(repeatFlag)
 {
  getWordFromStream();
  repeatFlag=(!(1==1));
  if(strcmp(WordBuffer,"{")==0)
  {
   do
   {
    c=getCharFromStream();
   }while(c!='}');
   repeatFlag=(1==1);
  }else
  if( strcmp(WordBuffer,"\\")==0)
  {
   do
   {
    c=getCharFromStream();
   }while((c!='\r')&&(c!='\n'));
   repeatFlag=(1==1);
  }else
  if(strcmp(WordBuffer,"(")==0)
  {
   do
   {
    c=getCharFromStream();
   }while(c!=')');
   repeatFlag=(1==1);
  }
 }
}

variable_types: c char

variable_types:
repeatFlag
unsigned char

return_type: void


repo: ChristophBerg/newpid

decompiled_code:
int get_ctl_fd()
{
int result; // eax
int *v1; // rbx
int v2; // ebp

result = socket(2, 2, 0);
if ( result < 0 )
{
v1 = __errno_location();
v2 = *v1;
result = socket(17, 2, 0);
if ( result < 0 )
{
result = socket(10, 2, 0);
if ( result < 0 )
{
*v1 = v2;
perror("Cannot create control socket");
result = -1;
}
}
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax
int *v1; // rbx
int v2; // ebp

result = socket(2, 2, 0);
if ( result < 0 )
{
v1 = __errno_location();
v2 = *v1;
result = socket(17, 2, 0);
if ( result < 0 )
{
result = socket(10, 2, 0);
if ( result < 0 )
{
*v1 = v2;
perror("Cannot create control socket");
result = -1;
}
}
}
return result;
}

original_code:
static int
get_ctl_fd (void)
{
 int s_errno;
 int fd;
 fd = socket(2, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 s_errno = (*__errno_location ());
 fd = socket(17, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 fd = socket(10, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 (*__errno_location ()) = s_errno;
 perror("Cannot create control socket");
 return -1;
}

canonical_original_code:
static int get_ctl_fd()
{
 int s_errno;
 int fd;
 fd = socket(2, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 s_errno = (*__errno_location ());
 fd = socket(17, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 fd = socket(10, SOCK_DGRAM, 0);
 if (fd >= 0)
  return fd;
 (*__errno_location ()) = s_errno;
 perror("Cannot create control socket");
 return -1;
}

variable_types: fd int

return_type: int


decompiled_code:
__int64 __fastcall netns_switch(const char *a1)
{
int v1; // eax
int v2; // ebx
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
char v8[4120]; // [rsp+0h] [rbp-1018h] BYREF

snprintf(v8, 0x1000uLL, "%s/%s", "/var/run/netns", a1);
v1 = open(v8, 0x80000);
if ( v1 < 0 )
{
v4 = __errno_location();
v5 = strerror(*v4);
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", a1, v5);
result = 0xFFFFFFFFLL;
}
else
{
v2 = v1;
if ( setns(v1, 0x40000000) < 0 )
{
v6 = __errno_location();
v7 = strerror(*v6);
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n", a1, v7);
close(v2);
result = 0xFFFFFFFFLL;
}
else
{
close(v2);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
int v1; // eax
int v2; // ebx
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
char v8[4120]; // [rsp+0h] [rbp-1018h] BYREF

snprintf(v8, 0x1000uLL, "%s/%s", "/var/run/netns", a1);
v1 = open(v8, 0x80000);
if ( v1 < 0 )
{
v4 = __errno_location();
v5 = strerror(*v4);
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", a1, v5);
result = 0xFFFFFFFFLL;
}
else
{
v2 = v1;
if ( setns(v1, 0x40000000) < 0 )
{
v6 = __errno_location();
v7 = strerror(*v6);
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n", a1, v7);
close(v2);
result = 0xFFFFFFFFLL;
}
else
{
close(v2);
result = 0LL;
}
}
return result;
}

original_code:
int netns_switch(char *name)
{
 char net_path[4096];
 int netns;
 snprintf(net_path, sizeof(net_path), "%s/%s", "/var/run/netns", name);
 netns = open(net_path, 00 | 02000000);
 if (netns < 0) {
  fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
   name, strerror((*__errno_location ())));
  return -1;
 }
 if (setns(netns, 0x40000000) < 0) {
  fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
   name, strerror((*__errno_location ())));
  close(netns);
  return -1;
 }
 close(netns);
 return 0;
}

variable_types:
name
char *

variable_types: netns int


run

decompiled_code:
__int64 __fastcall run(void *arg)
{
const char **v1; // rbp
__uid_t v2; // eax
__pid_t v3; // eax
__pid_t v4; // ebx
__pid_t v5; // eax
char *v6; // rax
__int64 result; // rax
_BYTE stat_loc[4]; // [rsp+Ch] [rbp-2Ch] BYREF
_QWORD stat_loc_4[5]; // [rsp+10h] [rbp-28h] BYREF

v1 = (const char **)arg;
stat_loc_4[0] = 0LL;
stat_loc_4[1] = 0LL;
if ( mount("none", "/proc", 0LL, 0x84000uLL, 0LL) )
{
perror("remount proc private");
exit(1);
}
if ( mount("proc", "/proc", "proc", 0LL, 0LL) )
{
perror("mount proc");
exit(1);
}
if ( (byte_40413B & 0x40) != 0 )
{
if ( netns )
{
if ( (int)netns_switch((const char *)netns) < 0 )
exit(1);
}
else if ( (int)do_chflags("lo", 1LL, 1LL) < 0 )
{
exit(1);
}
}
v2 = getuid();
if ( setuid(v2) )
{
perror("setuid");
exit(1);
}
if ( !*(_QWORD *)arg )
{
v6 = getenv("SHELL");
if ( v6 )
stat_loc_4[0] = v6;
else
stat_loc_4[0] = "/bin/sh";
v1 = (const char **)stat_loc_4;
}
v3 = fork();
v4 = v3;
if ( v3 )
{
if ( v3 < 0 )
{
perror("fork");
exit(1);
}
}
else if ( execvp(*v1, (char *const *)v1) < 0 )
{
perror("execvp");
exit(1);
}
while ( 1 )
{
v5 = wait((__WAIT_STATUS)stat_loc);
if ( v4 == v5 )
break;
if ( v5 < 0 && *__errno_location() != 4 )
{
perror("waitpid");
exit(1);
}
}
if ( (stat_loc[0] & 0x7F) == 0 )
return stat_loc[1];
result = 0xFFFFFFFFLL;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
result = (stat_loc[0] & 0x7Fu) + 128;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *arg)
{
const char **v1; // rbp
__uid_t v2; // eax
__pid_t v3; // eax
__pid_t v4; // ebx
__pid_t v5; // eax
char *v6; // rax
__int64 result; // rax
_BYTE stat_loc[4]; // [rsp+Ch] [rbp-2Ch] BYREF
_QWORD stat_loc_4[5]; // [rsp+10h] [rbp-28h] BYREF

v1 = (const char **)arg;
stat_loc_4[0] = 0LL;
stat_loc_4[1] = 0LL;
if ( mount("none", "/proc", 0LL, 0x84000uLL, 0LL) )
{
perror("remount proc private");
exit(1);
}
if ( mount("proc", "/proc", "proc", 0LL, 0LL) )
{
perror("mount proc");
exit(1);
}
if ( (byte_40413B & 0x40) != 0 )
{
if ( netns )
{
if ( (int)func9((const char *)netns) < 0 )
exit(1);
}
else if ( (int)func8("lo", 1LL, 1LL) < 0 )
{
exit(1);
}
}
v2 = getuid();
if ( setuid(v2) )
{
perror("setuid");
exit(1);
}
if ( !*(_QWORD *)arg )
{
v6 = getenv("SHELL");
if ( v6 )
stat_loc_4[0] = v6;
else
stat_loc_4[0] = "/bin/sh";
v1 = (const char **)stat_loc_4;
}
v3 = fork();
v4 = v3;
if ( v3 )
{
if ( v3 < 0 )
{
perror("fork");
exit(1);
}
}
else if ( execvp(*v1, (char *const *)v1) < 0 )
{
perror("execvp");
exit(1);
}
while ( 1 )
{
v5 = wait((__WAIT_STATUS)stat_loc);
if ( v4 == v5 )
break;
if ( v5 < 0 && *__errno_location() != 4 )
{
perror("waitpid");
exit(1);
}
}
if ( (stat_loc[0] & 0x7F) == 0 )
return stat_loc[1];
result = 0xFFFFFFFFLL;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
result = (stat_loc[0] & 0x7Fu) + 128;
return result;
}

original_code:
int
run (void *argv_void)
{
 char *const *argv = argv_void;
 char *argv_sh[] = { ((void *)0), ((void *)0) };
 pid_t child;
 pid_t pid;
 if (mount("none", "/proc", ((void *)0), MS_SLAVE|MS_REC, ((void *)0)) != 0) {
  perror ("remount proc private");
  exit (1);
 }
 if (mount ("proc", "/proc", "proc", 0, ((void *)0)) != 0) {
  perror ("mount proc");
  exit (1);
 }
 if (cloneflags & 0x40000000) {
  if (netns) {
   if (netns_switch (netns) < 0)
    exit (1);
  } else {
   if (do_chflags ("lo", IFF_UP, IFF_UP) < 0)
    exit (1);
  }
 }
 if (setuid(getuid()) != 0) {
  perror ("setuid");
  exit (1);
 }
 if (argv[0] == ((void *)0)) {
  char *shell = getenv ("SHELL");
  if (shell)
   argv_sh[0] = shell;
  else
   argv_sh[0] = "/bin/sh";
  argv = argv_sh;
 }
 if ((child = fork ()) == 0) {
  if (execvp (argv[0], argv) < 0) {
   perror ("execvp");
   exit (1);
  }
 }
 if (child < 0) {
  perror ("fork");
  exit (1);
 }
 int status;
 while ((pid = wait (&status)) != child) {
  if (pid < 0 && (*__errno_location ()) != 4) {
   perror ("waitpid");
   exit (1);
  }
 }
 if ((((status) & 0x7f) == 0))
  return (((status) & 0xff00) >> 8);
 if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0))
  return 128 + ((status) & 0x7f);
 return -1;
}

canonical_original_code:
int run(void *argv_void)
{
 char **argv = argv_void;
 char *argv_sh[] = { ((void *)0), ((void *)0) };
 int child;
 int pid;
 if (mount("none", "/proc", ((void *)0), MS_SLAVE|MS_REC, ((void *)0)) != 0) {
  perror ("remount proc private");
  exit (1);
 }
 if (mount ("proc", "/proc", "proc", 0, ((void *)0)) != 0) {
  perror ("mount proc");
  exit (1);
 }
 if (cloneflags & 0x40000000) {
  if (netns) {
   if (netns_switch (netns) < 0)
    exit (1);
  } else {
   if (do_chflags ("lo", IFF_UP, IFF_UP) < 0)
    exit (1);
  }
 }
 if (setuid(getuid()) != 0) {
  perror ("setuid");
  exit (1);
 }
 if (argv[0] == ((void *)0)) {
  char *shell = getenv ("SHELL");
  if (shell)
   argv_sh[0] = shell;
  else
   argv_sh[0] = "/bin/sh";
  argv = argv_sh;
 }
 if ((child = fork ()) == 0) {
  if (execvp (argv[0], argv) < 0) {
   perror ("execvp");
   exit (1);
  }
 }
 if (child < 0) {
  perror ("fork");
  exit (1);
 }
 int status;
 while ((pid = wait (&status)) != child) {
  if (pid < 0 && (*__errno_location ()) != 4) {
   perror ("waitpid");
   exit (1);
  }
 }
 if ((((status) & 0x7f) == 0))
  return (((status) & 0xff00) >> 8);
 if ((((char) (((status) & 0x7f) + 1) >> 1) > 0))
  return 128 + ((status) & 0x7f);
 return -1;
}

variable_types:
argv
char **

variable_types: child int

variable_types: pid int

variable_types:
shell
char *

variable_types: status int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v5; // ebx
int v6; // edi
int stat_loc; // [rsp+Ch] [rbp-81Ch] BYREF
char child_stack[1048]; // [rsp+410h] [rbp-418h] BYREF

while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "+inN:u");
if ( v5 == -1 )
break;
if ( v5 == 110 )
goto LABEL_6;
if ( v5 > 110 )
{
if ( v5 != 117 )
goto LABEL_14;
cloneflags |= 0x4000000u;
}
else if ( v5 == 78 )
{
if ( memcmp(optarg, "newpid", 6uLL) )
{
fwrite("-N argument must start with 'newpid'\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
netns = (__int64)strdup(optarg);
LABEL_6:
cloneflags |= 0x40000000u;
}
else
{
if ( v5 != 105 )
{
LABEL_14:
fprintf(stderr, "Usage: %s [options] [command args ...]\n", *argv);
fwrite("Options:\n", 1uLL, 9uLL, stderr);
fwrite(" -i request new IPC namespace (CLONE_NEWIPC)\n", 1uLL, 0x38uLL, stderr);
fwrite(" -n request new network namespace (CLONE_NEWNET)\n", 1uLL, 0x3CuLL, stderr);
fwrite(" -N newpidns join named network namespace\n", 1uLL, 0x2CuLL, stderr);
fwrite(" -u request new UTS namespace (CLONE_NEWUTS)\n", 1uLL, 0x38uLL, stderr);
exit(1);
}
cloneflags |= 0x8000000u;
}
}
v6 = clone((int (*)(void *))run, child_stack, cloneflags | 0x20020011, &argv[optind]);
if ( v6 < 0 )
{
perror("clone");
exit(1);
}
if ( waitpid(v6, &stat_loc, 0) < 0 )
perror("waitpid");
if ( (stat_loc & 0x7F) == 0 )
return BYTE1(stat_loc);
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
v5 = (stat_loc & 0x7F) + 128;
return v5;
}

original_code:
int
main (int argc, char *argv[], char *envp[])
{
 int opt;
 while ((opt = getopt(argc, argv, "+inN:u")) != -1) {
  switch (opt) {
   case 'i':
    cloneflags |= 0x08000000;
    break;
   case 'N':
    if (strncmp(optarg, "newpid", sizeof("newpid")-1)) {
     fprintf(stderr, "-N argument must start with 'newpid'\n");
     exit(1);
    }
    netns = strdup(optarg);
   case 'n':
    cloneflags |= 0x40000000;
    break;
   case 'u':
    cloneflags |= 0x04000000;
    break;
   default:
    fprintf(stderr, "Usage: %s [options] [command args ...]\n",
      argv[0]);
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  -i           request new IPC namespace (CLONE_NEWIPC)\n");
    fprintf(stderr, "  -n           request new network namespace (CLONE_NEWNET)\n");
    fprintf(stderr, "  -N newpidns  join named network namespace\n");
    fprintf(stderr, "  -u           request new UTS namespace (CLONE_NEWUTS)\n");
    exit(1);
  }
 }
 char cstack[2048];
 int child;
 int status;
 if ((child =
   clone (run,
    cstack + 1024,
    0x20000000 | 0x00020000 | cloneflags | 17,
    argv + optind)
 ) < 0) {
  perror ("clone");
  exit (1);
 }
 if (waitpid (child, &status, 0) < 0) {
  perror ("waitpid");
 }
 if ((((status) & 0x7f) == 0))
  return (((status) & 0xff00) >> 8);
 if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0))
  return 128 + ((status) & 0x7f);
 return -1;
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[])
{
 int opt;
 while ((opt = getopt(argc, argv, "+inN:u")) != -1) {
  switch (opt) {
   case 'i':
    cloneflags |= 0x08000000;
    break;
   case 'N':
    if (strncmp(optarg, "newpid", sizeof("newpid")-1)) {
     fprintf(stderr, "-N argument must start with 'newpid'\n");
     exit(1);
    }
    netns = strdup(optarg);
   case 'n':
    cloneflags |= 0x40000000;
    break;
   case 'u':
    cloneflags |= 0x04000000;
    break;
   default:
    fprintf(stderr, "Usage: %s [options] [command args ...]\n",
      argv[0]);
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  -i           request new IPC namespace (CLONE_NEWIPC)\n");
    fprintf(stderr, "  -n           request new network namespace (CLONE_NEWNET)\n");
    fprintf(stderr, "  -N newpidns  join named network namespace\n");
    fprintf(stderr, "  -u           request new UTS namespace (CLONE_NEWUTS)\n");
    exit(1);
  }
 }
 char cstack[2048];
 int child;
 int status;
 if ((child =
   clone (run,
    cstack + 1024,
    0x20000000 | 0x00020000 | cloneflags | 17,
    argv + optind)
 ) < 0) {
  perror ("clone");
  exit (1);
 }
 if (waitpid (child, &status, 0) < 0) {
  perror ("waitpid");
 }
 if ((((status) & 0x7f) == 0))
  return (((status) & 0xff00) >> 8);
 if ((((char) (((status) & 0x7f) + 1) >> 1) > 0))
  return 128 + ((status) & 0x7f);
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
envp
char *[]

variable_types: opt int

variable_types:
cstack
char [2048]


ArriveBridge

repo: Clyde-Xu/Homework

decompiled_code:
int __fastcall ArriveBridge(int a1)
{
int v1; // eax
char *v2; // rax
int v3; // eax
int result; // eax
char *v5; // rax
int v6; // eax
char *v7; // rax
char *v8; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v2 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v2);
exit(1);
}
if ( a1 )
{
while ( onBridge == -3 || onBridge > 0 || to_NORWICH - to_HANOVER > 5 )
{
v6 = pthread_cond_wait(&han_cond, &mtx);
if ( v6 )
{
v7 = strerror(v6);
fprintf(stderr, "pthread_cond_wait:%s\n", v7);
exit(1);
}
}
--onBridge;
}
else
{
while ( onBridge == 3 || onBridge < 0 || to_HANOVER - to_NORWICH > 5 )
{
v3 = pthread_cond_wait(&nor_cond, &mtx);
if ( v3 )
{
v5 = strerror(v3);
fprintf(stderr, "pthread_cond_wait:%s\n", v5);
exit(1);
}
}
++onBridge;
}
result = pthread_mutex_unlock(&mtx);
if ( result )
{
v8 = strerror(result);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v8);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
int v1; // eax
char *v2; // rax
int v3; // eax
int result; // eax
char *v5; // rax
int v6; // eax
char *v7; // rax
char *v8; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v2 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v2);
exit(1);
}
if ( a1 )
{
while ( onBridge == -3 || onBridge > 0 || to_NORWICH - to_HANOVER > 5 )
{
v6 = pthread_cond_wait(&han_cond, &mtx);
if ( v6 )
{
v7 = strerror(v6);
fprintf(stderr, "pthread_cond_wait:%s\n", v7);
exit(1);
}
}
--onBridge;
}
else
{
while ( onBridge == 3 || onBridge < 0 || to_HANOVER - to_NORWICH > 5 )
{
v3 = pthread_cond_wait(&nor_cond, &mtx);
if ( v3 )
{
v5 = strerror(v3);
fprintf(stderr, "pthread_cond_wait:%s\n", v5);
exit(1);
}
}
++onBridge;
}
result = pthread_mutex_unlock(&mtx);
if ( result )
{
v8 = strerror(result);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v8);
exit(1);
}
return result;
}

original_code:
static void ArriveBridge(direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  while(onBridge == 3 || onBridge < 0 || to_HANOVER - to_NORWICH > 5){
   result = pthread_cond_wait(&nor_cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  onBridge++;
 }else{
  while(onBridge == -3 || onBridge > 0 || to_NORWICH - to_HANOVER > 5){
   result = pthread_cond_wait(&han_cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  onBridge--;
 }
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
}

canonical_original_code:
static void ArriveBridge(enum direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  while(onBridge == 3 || onBridge < 0 || to_HANOVER - to_NORWICH > 5){
   result = pthread_cond_wait(&nor_cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  onBridge++;
 }else{
  while(onBridge == -3 || onBridge > 0 || to_NORWICH - to_HANOVER > 5){
   result = pthread_cond_wait(&han_cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  onBridge--;
 }
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
}

variable_types: enum direction { TO_NORWICH=0, TO_HANOVER=1 }

variable_types: result int


OnBridge

decompiled_code:
unsigned int __fastcall OnBridge(int a1)
{
int v1; // eax
int v2; // eax
char *v4; // rax
char *v5; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v4 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v4);
exit(1);
}
if ( a1 )
printf("\x1B[32m%d\x1B[0m cars on bridge towards \x1B[32mHANOVER\x1B[0m\n\n", (unsigned int)-onBridge);
else
printf("\x1B[33m%d\x1B[0m cars on bridge towards \x1B[33mNORWICH\x1B[0m\n\n", (unsigned int)onBridge);
printf("\x1B[33m%d\x1B[0m cars in total towards \x1B[33mNORWICH\x1B[0m\n", (unsigned int)to_NORWICH);
printf("\x1B[32m%d\x1B[0m cars in total towards \x1B[32mHANOVER\x1B[0m\n", (unsigned int)to_HANOVER);
v2 = pthread_mutex_unlock(&mtx);
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v5);
exit(1);
}
puts("==============================================");
return sleep(2u);
}

canonical_decompiled_code:
unsigned int __fastcall func8(int a1)
{
int v1; // eax
int v2; // eax
char *v4; // rax
char *v5; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v4 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v4);
exit(1);
}
if ( a1 )
printf("\x1B[32m%d\x1B[0m cars on bridge towards \x1B[32mHANOVER\x1B[0m\n\n", (unsigned int)-onBridge);
else
printf("\x1B[33m%d\x1B[0m cars on bridge towards \x1B[33mNORWICH\x1B[0m\n\n", (unsigned int)onBridge);
printf("\x1B[33m%d\x1B[0m cars in total towards \x1B[33mNORWICH\x1B[0m\n", (unsigned int)to_NORWICH);
printf("\x1B[32m%d\x1B[0m cars in total towards \x1B[32mHANOVER\x1B[0m\n", (unsigned int)to_HANOVER);
v2 = pthread_mutex_unlock(&mtx);
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v5);
exit(1);
}
puts("==============================================");
return sleep(2u);
}

original_code:
static void OnBridge(direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  printf("\x1B[33m" "%d" "\x1B[0m" " cars on bridge towards " "\x1B[33m" "NORWICH" "\x1B[0m" "\n\n", onBridge);
 }else{
  printf("\x1B[32m" "%d" "\x1B[0m" " cars on bridge towards " "\x1B[32m" "HANOVER" "\x1B[0m" "\n\n", -onBridge);
 }
 printf("\x1B[33m" "%d" "\x1B[0m" " cars in total towards " "\x1B[33m" "NORWICH" "\x1B[0m" "\n", to_NORWICH);
 printf("\x1B[32m" "%d" "\x1B[0m" " cars in total towards " "\x1B[32m" "HANOVER" "\x1B[0m" "\n", to_HANOVER);
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
 printf("==============================================\n");
 sleep(2);
}

canonical_original_code:
static void OnBridge(enum direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  printf("\x1B[33m" "%d" "\x1B[0m" " cars on bridge towards " "\x1B[33m" "NORWICH" "\x1B[0m" "\n\n", onBridge);
 }else{
  printf("\x1B[32m" "%d" "\x1B[0m" " cars on bridge towards " "\x1B[32m" "HANOVER" "\x1B[0m" "\n\n", -onBridge);
 }
 printf("\x1B[33m" "%d" "\x1B[0m" " cars in total towards " "\x1B[33m" "NORWICH" "\x1B[0m" "\n", to_NORWICH);
 printf("\x1B[32m" "%d" "\x1B[0m" " cars in total towards " "\x1B[32m" "HANOVER" "\x1B[0m" "\n", to_HANOVER);
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
 printf("==============================================\n");
 sleep(2);
}

user_defined_types: enum direction { TO_NORWICH=0, TO_HANOVER=1 }


ExitBridge

decompiled_code:
int __fastcall ExitBridge(int a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
int result; // eax
char *v5; // rax
int v6; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // eax
char *v12; // rax
int v13; // eax
char *v14; // rax
char *v15; // rax
char *v16; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v5 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v5);
exit(1);
}
if ( a1 )
{
v10 = onBridge + 1;
onBridge = v10;
--to_HANOVER;
if ( v10 == -2 )
{
v13 = pthread_cond_signal(&han_cond);
if ( v13 )
{
v14 = strerror(v13);
fprintf(stderr, "pthread_cond_signal:%s\n", v14);
exit(1);
}
}
else if ( !v10 )
{
v11 = pthread_cond_broadcast(&nor_cond);
if ( v11 )
{
v12 = strerror(v11);
fprintf(stderr, "pthread_cond_signal:%s\n", v12);
exit(1);
}
}
}
else
{
--to_NORWICH;
v2 = onBridge - 1;
onBridge = v2;
if ( v2 == 2 )
{
v6 = pthread_cond_signal(&nor_cond);
if ( v6 )
{
v7 = strerror(v6);
fprintf(stderr, "pthread_cond_signal:%s\n", v7);
exit(1);
}
}
else if ( !v2 )
{
v8 = pthread_cond_broadcast(&han_cond);
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "pthread_cond_signal:%s\n", v9);
exit(1);
}
}
}
v3 = pthread_mutex_unlock(&mtx);
if ( v3 )
{
v15 = strerror(v3);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v15);
exit(1);
}
result = pthread_cond_signal(&cond);
if ( result )
{
v16 = strerror(result);
fprintf(stderr, "pthread_cond_signal:%s\n", v16);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
int result; // eax
char *v5; // rax
int v6; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // eax
char *v12; // rax
int v13; // eax
char *v14; // rax
char *v15; // rax
char *v16; // rax

v1 = pthread_mutex_lock(&mtx);
if ( v1 )
{
v5 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock:%s\n", v5);
exit(1);
}
if ( a1 )
{
v10 = onBridge + 1;
onBridge = v10;
--to_HANOVER;
if ( v10 == -2 )
{
v13 = pthread_cond_signal(&han_cond);
if ( v13 )
{
v14 = strerror(v13);
fprintf(stderr, "pthread_cond_signal:%s\n", v14);
exit(1);
}
}
else if ( !v10 )
{
v11 = pthread_cond_broadcast(&nor_cond);
if ( v11 )
{
v12 = strerror(v11);
fprintf(stderr, "pthread_cond_signal:%s\n", v12);
exit(1);
}
}
}
else
{
--to_NORWICH;
v2 = onBridge - 1;
onBridge = v2;
if ( v2 == 2 )
{
v6 = pthread_cond_signal(&nor_cond);
if ( v6 )
{
v7 = strerror(v6);
fprintf(stderr, "pthread_cond_signal:%s\n", v7);
exit(1);
}
}
else if ( !v2 )
{
v8 = pthread_cond_broadcast(&han_cond);
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "pthread_cond_signal:%s\n", v9);
exit(1);
}
}
}
v3 = pthread_mutex_unlock(&mtx);
if ( v3 )
{
v15 = strerror(v3);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v15);
exit(1);
}
result = pthread_cond_signal(&cond);
if ( result )
{
v16 = strerror(result);
fprintf(stderr, "pthread_cond_signal:%s\n", v16);
exit(1);
}
return result;
}

original_code:
static void ExitBridge(direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  to_NORWICH--;
  onBridge--;
  if(onBridge == 3 - 1){
   result = pthread_cond_signal(&nor_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
  else if(onBridge == 0){
   result = pthread_cond_broadcast(&han_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
 }else{
  onBridge++;
  to_HANOVER--;
  if(onBridge == -(3 - 1)){
   result = pthread_cond_signal(&han_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
  else if(onBridge == 0){
   result = pthread_cond_broadcast(&nor_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
 }
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
 result = pthread_cond_signal(&cond);
 if(result != 0){
  fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
  exit(1);
 }
}

canonical_original_code:
static void ExitBridge(enum direction direct)
{
 int result = pthread_mutex_lock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
  exit(1);
 }
 if(direct == TO_NORWICH){
  to_NORWICH--;
  onBridge--;
  if(onBridge == 3 - 1){
   result = pthread_cond_signal(&nor_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
  else if(onBridge == 0){
   result = pthread_cond_broadcast(&han_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
 }else{
  onBridge++;
  to_HANOVER--;
  if(onBridge == -(3 - 1)){
   result = pthread_cond_signal(&han_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
  else if(onBridge == 0){
   result = pthread_cond_broadcast(&nor_cond);
   if(result != 0){
    fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
    exit(1);
   }
  }
 }
 result = pthread_mutex_unlock(&mtx);
 if(result != 0){
  fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
  exit(1);
 }
 result = pthread_cond_signal(&cond);
 if(result != 0){
  fprintf(stderr, "pthread_cond_signal:%s\n", strerror(result));
  exit(1);
 }
}


OneVehicle

decompiled_code:
void *__fastcall OneVehicle(void *a1)
{
ArriveBridge((int)a1);
OnBridge((int)a1);
ExitBridge((int)a1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func10(void *a1)
{
func7((int)a1);
func8((int)a1);
func9((int)a1);
return 0LL;
}

original_code:
static void *OneVehicle(void *cardirect)
{
 direction direct = (direction)cardirect;
 ArriveBridge(direct);
 OnBridge(direct);
 ExitBridge(direct);
 return ((void *)0);
}

canonical_original_code:
static void *OneVehicle(void *cardirect)
{
 enum direction direct = (direction)cardirect;
 ArriveBridge(direct);
 OnBridge(direct);
 ExitBridge(direct);
 return ((void *)0);
}

variable_types:
cardirect
void *

return_type: void *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // eax
int v5; // eax
char *v6; // rax
char *v7; // rax
unsigned int v8; // ecx
int v9; // eax
int v10; // eax
int v11; // er12
int v12; // ebp
char *v13; // rax
char *v14; // rax
int v15; // eax
char *v16; // rax
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

v3 = time(0LL);
srand(v3);
while ( 1 )
{
v4 = pthread_mutex_lock(&mtx);
if ( v4 )
break;
while ( to_NORWICH + to_HANOVER == 15 )
{
v5 = pthread_cond_wait(&cond, &mtx);
if ( v5 )
{
v6 = strerror(v5);
fprintf(stderr, "pthread_cond_wait:%s\n", v6);
exit(1);
}
}
while ( 1 )
{
v11 = to_NORWICH;
v12 = to_HANOVER;
if ( to_NORWICH + to_HANOVER > 14 )
break;
if ( (rand() & 1) != 0 )
{
to_NORWICH = v11 + 1;
v8 = 0;
}
else
{
to_HANOVER = v12 + 1;
v8 = 1;
}
v9 = pthread_create(newthread, 0LL, OneVehicle, (void *)v8);
if ( v9 )
{
v13 = strerror(v9);
fprintf(stderr, "pthread_create:%s\n", v13);
exit(1);
}
v10 = pthread_detach(newthread[0]);
if ( v10 )
{
v14 = strerror(v10);
fprintf(stderr, "pthread_detach:%s\n", v14);
exit(1);
}
}
v15 = pthread_mutex_unlock(&mtx);
if ( v15 )
{
v16 = strerror(v15);
fprintf(stderr, "pthread_mutex_unlock:%s\n", v16);
exit(1);
}
}
v7 = strerror(v4);
fprintf(stderr, "pthread_mutex_lock:%s\n", v7);
exit(1);
}

original_code:
int main(void)
{
 int result;
 srand(time(((void *)0)));
 pthread_t thread;
 for(;;){
  result = pthread_mutex_lock(&mtx);
  if(result != 0){
   fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
   exit(1);
  }
  while(to_NORWICH + to_HANOVER == 15){
   result = pthread_cond_wait(&cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  while(to_NORWICH + to_HANOVER < 15){
   direction direct;
   if(rand() % 2){
    direct = TO_NORWICH;
    to_NORWICH++;
   }else{
    direct = TO_HANOVER;
    to_HANOVER++;
   }
   result = pthread_create(&thread, ((void *)0), OneVehicle, (void *)direct);
   if(result != 0){
    fprintf(stderr, "pthread_create:%s\n", strerror(result));
    exit(1);
   }
   result = pthread_detach(thread);
   if(result != 0){
    fprintf(stderr, "pthread_detach:%s\n", strerror(result));
    exit(1);
   }
  }
  result = pthread_mutex_unlock(&mtx);
  if(result != 0){
   fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
   exit(1);
  }
 }
}

canonical_original_code:
int main()
{
 int result;
 srand(time(((void *)0)));
 unsigned long thread;
 for(;;){
  result = pthread_mutex_lock(&mtx);
  if(result != 0){
   fprintf(stderr, "pthread_mutex_lock:%s\n", strerror(result));
   exit(1);
  }
  while(to_NORWICH + to_HANOVER == 15){
   result = pthread_cond_wait(&cond, &mtx);
   if(result != 0){
    fprintf(stderr, "pthread_cond_wait:%s\n", strerror(result));
    exit(1);
   }
  }
  while(to_NORWICH + to_HANOVER < 15){
   enum direction direct;
   if(rand() % 2){
    direct = TO_NORWICH;
    to_NORWICH++;
   }else{
    direct = TO_HANOVER;
    to_HANOVER++;
   }
   result = pthread_create(&thread, ((void *)0), OneVehicle, (void *)direct);
   if(result != 0){
    fprintf(stderr, "pthread_create:%s\n", strerror(result));
    exit(1);
   }
   result = pthread_detach(thread);
   if(result != 0){
    fprintf(stderr, "pthread_detach:%s\n", strerror(result));
    exit(1);
   }
  }
  result = pthread_mutex_unlock(&mtx);
  if(result != 0){
   fprintf(stderr, "pthread_mutex_unlock:%s\n", strerror(result));
   exit(1);
  }
 }
}

variable_types:
thread
unsigned long


repo: CodeMonkeySteve/contour-glucose

decompiled_code:
_BOOL8 __fastcall is_printable(char a1)
{
return (unsigned __int8)(a1 - 32) <= 0x5Fu;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(char a1)
{
return (unsigned __int8)(a1 - 32) <= 0x5Fu;
}

original_code:
static inline int is_printable(const unsigned char c)
{
 return c >= 0x20 && c < 0x80;
}

canonical_original_code:
static inline int is_printable(unsigned char c)
{
 return c >= 0x20 && c < 0x80;
}

variable_types:
c
unsigned char


datalen

decompiled_code:
__int64 __fastcall datalen(__int64 a1)
{
__int64 v1; // rax
int v2; // edx

v1 = 0LL;
v2 = 0;
do
{
if ( *(_BYTE *)(a1 + v1) )
v2 = v1;
++v1;
}
while ( v1 != 64 );
return (unsigned int)(v2 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rax
int v2; // edx

v1 = 0LL;
v2 = 0;
do
{
if ( *(_BYTE *)(a1 + v1) )
v2 = v1;
++v1;
}
while ( v1 != 64 );
return (unsigned int)(v2 + 1);
}

original_code:
int datalen(const unsigned char *data)
{
 int i, len;
 for (i = 0, len = 0; i < 64; i++)
  if (data[i])
   len = i;
 return len + 1;
}

canonical_original_code:
int datalen(unsigned char *data)
{
 int i; int len;
 for (i = 0, len = 0; i < 64; i++)
  if (data[i])
   len = i;
 return len + 1;
}

variable_types:
data
unsigned char *

variable_types: i int

variable_types: len int


decompiled_code:
int __fastcall print_hex(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("0x%02x ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("0x%02x ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

original_code:
void print_hex(const unsigned char *data, int len)
{
 int i;
 for (i = 0; i < len; i++)
  printf("0x%02x ", data[i]);
 printf("\n");
}

canonical_original_code:
void print_hex(unsigned char *data, int len)
{
 int i;
 for (i = 0; i < len; i++)
  printf("0x%02x ", data[i]);
 printf("\n");
}


decompiled_code:
int __fastcall print_ascii(char *a1, int a2)
{
char *v2; // rbp
int v3; // ebx

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = (unsigned __int8)*v2;
if ( !is_printable(*v2) )
v3 = 46;
putchar(v3);
++v2;
}
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(char *a1, int a2)
{
char *v2; // rbp
int v3; // ebx

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = (unsigned __int8)*v2;
if ( !func0(*v2) )
v3 = 46;
putchar(v3);
++v2;
}
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

original_code:
void print_ascii(const unsigned char *data, int len)
{
 int i;
 for (i = 0; i < len; i++)
  printf("%c", is_printable(data[i]) ? data[i] : '.');
 printf("\n");
}

canonical_original_code:
void print_ascii(unsigned char *data, int len)
{
 int i;
 for (i = 0; i < len; i++)
  printf("%c", is_printable(data[i]) ? data[i] : '.');
 printf("\n");
}


decompiled_code:
__int64 __fastcall contour_initialize(unsigned int a1, unsigned int a2)
{
int v2; // er12
int v4; // [rsp+0h] [rbp-68h] BYREF
__int64 v5; // [rsp+4h] [rbp-64h]
__int64 v6; // [rsp+Ch] [rbp-5Ch]
__int64 v7; // [rsp+14h] [rbp-54h]
__int64 v8; // [rsp+1Ch] [rbp-4Ch]
__int64 v9; // [rsp+24h] [rbp-44h]
__int64 v10; // [rsp+2Ch] [rbp-3Ch]
__int64 v11; // [rsp+34h] [rbp-34h]
__int64 v12; // [rsp+3Ch] [rbp-2Ch]
char v13; // [rsp+44h] [rbp-24h]

v4 = 667;
read_msgs(a1);
v5 = 0x40100000000LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
send_msg_with_proggress_note(&v4, a1, a2);
usleep(0x186A0u);
BYTE5(v5) = 6;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
BYTE5(v5) = 21;
v2 = 6;
do
{
usleep(0x186A0u);
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
--v2;
}
while ( v2 );
usleep(0xF4240u);
read_msgs(a1);
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 5;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
send_pattern(a1, a2, 82, 65);
send_pattern(a1, a2, 82, 67);
send_pattern(a1, a2, 82, 68);
send_pattern(a1, a2, 82, 71);
send_pattern(a1, a2, 82, 73);
send_pattern(a1, a2, 82, 77);
send_pattern(a1, a2, 82, 80);
send_pattern(a1, a2, 82, 82);
send_pattern(a1, a2, 82, 83);
send_pattern(a1, a2, 82, 84);
send_pattern(a1, a2, 82, 85);
send_pattern(a1, a2, 82, 86);
send_pattern(a1, a2, 82, 87);
send_pattern(a1, a2, 82, 88);
send_pattern(a1, a2, 87, 75);
usleep(0x186A0u);
v5 = 0x70624F0800000000LL;
v6 = 0x7C33636573LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x64190u);
v5 = 0x7C520200000000LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 89;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 87;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 75;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 67;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
send_pattern(a1, a2, 82, 90);
usleep(0x186A0u);
v5 = 0x40100000000LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 21;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 5;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 4;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
BYTE5(v5) = 6;
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
send_msg_with_proggress_note(&v4, a1, a2);
read_msgs(a1);
usleep(0x186A0u);
if ( trace_level >= 0 )
fputc(10, stderr);
return 0LL;
}

original_code:
int contour_initialize(int fd, int uc)
{
 int i, j;
 struct msg msg;
 msg.direction = OUT;
 read_msgs(fd);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x01); j = 5; } while (0);
 msg.data[j++] = (0x04);
 send_msg_with_proggress_note(&msg, fd, uc);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x06); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 do { msg.data[(1) + 4] = (0x15); j = (1) + 1; } while (0);
 for (i = 0; i < 6; i++) {
  usleep(100 * 1000);
  send_msg_with_proggress_note(&msg, fd, uc);
  read_msgs(fd);
 }
 usleep(1000 * 1000);
 read_msgs(fd);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x05); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 send_pattern(fd, uc, 'R', 'A');
 send_pattern(fd, uc, 'R', 'C');
 send_pattern(fd, uc, 'R', 'D');
 send_pattern(fd, uc, 'R', 'G');
 send_pattern(fd, uc, 'R', 'I');
 send_pattern(fd, uc, 'R', 'M');
 send_pattern(fd, uc, 'R', 'P');
 send_pattern(fd, uc, 'R', 'R');
 send_pattern(fd, uc, 'R', 'S');
 send_pattern(fd, uc, 'R', 'T');
 send_pattern(fd, uc, 'R', 'U');
 send_pattern(fd, uc, 'R', 'V');
 send_pattern(fd, uc, 'R', 'W');
 send_pattern(fd, uc, 'R', 'X');
 send_pattern(fd, uc, 'W', 'K');
 usleep(100 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x08); j = 5; } while (0);
 msg.data[j++] = ('O');
 msg.data[j++] = ('b');
 msg.data[j++] = ('p');
 msg.data[j++] = ('s');
 msg.data[j++] = ('e');
 msg.data[j++] = ('c');
 msg.data[j++] = ('3');
 msg.data[j++] = ('|');
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(410 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x02); j = 5; } while (0);
 msg.data[j++] = ('R');
 msg.data[j++] = ('|');
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('Y'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('W'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('K'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('C'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 send_pattern(fd, uc, 'R', 'Z');
 usleep(100 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x01); j = 5; } while (0);
 msg.data[j++] = (0x04);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x15); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x05); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x04); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x06); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { if (0 <= trace_level) fprintf(stderr, "\n"); } while (0);
 return 0;
}

canonical_original_code:
int contour_initialize(int fd, int uc)
{
 int i; int j;
 struct msg msg;
 msg.direction = OUT;
 read_msgs(fd);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x01); j = 5; } while (0);
 msg.data[j++] = (0x04);
 send_msg_with_proggress_note(&msg, fd, uc);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x06); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 do { msg.data[(1) + 4] = (0x15); j = (1) + 1; } while (0);
 for (i = 0; i < 6; i++) {
  usleep(100 * 1000);
  send_msg_with_proggress_note(&msg, fd, uc);
  read_msgs(fd);
 }
 usleep(1000 * 1000);
 read_msgs(fd);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x05); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 send_pattern(fd, uc, 'R', 'A');
 send_pattern(fd, uc, 'R', 'C');
 send_pattern(fd, uc, 'R', 'D');
 send_pattern(fd, uc, 'R', 'G');
 send_pattern(fd, uc, 'R', 'I');
 send_pattern(fd, uc, 'R', 'M');
 send_pattern(fd, uc, 'R', 'P');
 send_pattern(fd, uc, 'R', 'R');
 send_pattern(fd, uc, 'R', 'S');
 send_pattern(fd, uc, 'R', 'T');
 send_pattern(fd, uc, 'R', 'U');
 send_pattern(fd, uc, 'R', 'V');
 send_pattern(fd, uc, 'R', 'W');
 send_pattern(fd, uc, 'R', 'X');
 send_pattern(fd, uc, 'W', 'K');
 usleep(100 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x08); j = 5; } while (0);
 msg.data[j++] = ('O');
 msg.data[j++] = ('b');
 msg.data[j++] = ('p');
 msg.data[j++] = ('s');
 msg.data[j++] = ('e');
 msg.data[j++] = ('c');
 msg.data[j++] = ('3');
 msg.data[j++] = ('|');
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(410 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x02); j = 5; } while (0);
 msg.data[j++] = ('R');
 msg.data[j++] = ('|');
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('Y'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('W'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('K'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = ('C'); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 send_pattern(fd, uc, 'R', 'Z');
 usleep(100 * 1000);
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x01); j = 5; } while (0);
 msg.data[j++] = (0x04);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x15); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x05); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x04); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { msg.data[(1) + 4] = (0x06); j = (1) + 1; } while (0);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 send_msg_with_proggress_note(&msg, fd, uc);
 read_msgs(fd);
 usleep(100 * 1000);
 do { if (0 <= trace_level) fprintf(stderr, "\n"); } while (0);
 return 0;
}

variable_types: fd int

variable_types: uc int

variable_types: j int

variable_types: struct msg { int direction; unsigned char data[-1]; }


original_code:
int contour_read_entry(int fd, int uc, struct msg *in)
{
 struct msg msg;
 int j;
 msg.direction = OUT;
 do { memset(&msg.data, 0, sizeof(msg.data)); msg.data[4] = (0x01); j = 5; } while (0);
 do { msg.data[(1) + 4] = (0x06); j = (1) + 1; } while (0);
 send_msg(&msg, fd, uc);
 read_and_verify(in, fd);
 return datalen(in->data);
}

variable_types: struct msg { int direction; unsigned char data[-1]; } *


decompiled_code:
__int64 __fastcall wait_for_device(unsigned int a1, unsigned int a2, __int64 a3)
{
int v4; // ebx

v4 = ((__int64 (*)(void))hiddev_open_by_id)();
if ( v4 <= 0 )
{
if ( trace_level >= 0 )
fwrite("No suitable device found. Please plug in your glucose meter\n", 1uLL, 0x3CuLL, stderr);
do
{
usleep(0x7A120u);
v4 = hiddev_open_by_id(a1, a2, a3);
}
while ( v4 < 0 );
usleep(0x1E8480u);
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, unsigned int a2, __int64 a3)
{
int v4; // ebx

v4 = ((__int64 (*)(void))hiddev_open_by_id)();
if ( v4 <= 0 )
{
if ( trace_level >= 0 )
fwrite("No suitable device found. Please plug in your glucose meter\n", 1uLL, 0x3CuLL, stderr);
do
{
usleep(0x7A120u);
v4 = hiddev_open_by_id(a1, a2, a3);
}
while ( v4 < 0 );
usleep(0x1E8480u);
}
return (unsigned int)v4;
}

original_code:
int wait_for_device(int vendor, int product, int *usage_code)
{
 int fd;
 fd = hiddev_open_by_id(vendor, product, usage_code);
 if (fd > 0)
  return fd;
 do { if (0 <= trace_level) fprintf(stderr, "No suitable device found. Please plug in your glucose meter\n"); } while (0);
 do {
  usleep(500 * 1000);
  fd = hiddev_open_by_id(vendor, product, usage_code);
 } while(fd < 0);
 usleep(2000 * 1000);
 return fd;
}

variable_types: vendor int

variable_types: product int


decompiled_code:
__int64 __fastcall read_args(int argc, char **argv, __int64 a3)
{
__int64 result; // rax
int v5; // eax
char shortopts[9]; // [rsp+3h] [rbp-25h] BYREF
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

longind[0] = 0;
strcpy(shortopts, "d:v:o:f:");
while ( 1 )
{
v5 = getopt_long(argc, argv, shortopts, &long_options_0, longind);
if ( v5 == -1 )
break;
if ( v5 == 102 )
{
if ( !strcmp(optarg, "raw") )
{
*(_DWORD *)(a3 + 16) = 0;
}
else if ( !strcmp(optarg, "clean") )
{
*(_DWORD *)(a3 + 16) = 1;
}
else
{
if ( strcmp(optarg, "csv") )
{
result = 0xFFFFFFFFLL;
if ( trace_level >= 0 )
{
fprintf(stderr, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg);
result = 0xFFFFFFFFLL;
}
return result;
}
*(_DWORD *)(a3 + 16) = 2;
}
}
else if ( v5 <= 102 )
{
if ( v5 == 63 )
return 0xFFFFFFFFLL;
if ( v5 == 100 )
*(_QWORD *)a3 = optarg;
}
else
{
if ( v5 == 111 )
goto LABEL_9;
if ( v5 == 118 )
{
if ( optarg )
*(_DWORD *)(a3 + 20) = atoi(optarg);
else
++*(_DWORD *)(a3 + 20);
LABEL_9:
*(_QWORD *)(a3 + 8) = optarg;
}
}
}
result = 0LL;
if ( argc > optind )
{
do
LODWORD(result) = result + 1;
while ( (_DWORD)result != argc - optind );
optind = argc;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int argc, char **argv, __int64 a3)
{
__int64 result; // rax
int v5; // eax
char shortopts[9]; // [rsp+3h] [rbp-25h] BYREF
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

longind[0] = 0;
strcpy(shortopts, "d:v:o:f:");
while ( 1 )
{
v5 = getopt_long(argc, argv, shortopts, &long_options_0, longind);
if ( v5 == -1 )
break;
if ( v5 == 102 )
{
if ( !strcmp(optarg, "raw") )
{
*(_DWORD *)(a3 + 16) = 0;
}
else if ( !strcmp(optarg, "clean") )
{
*(_DWORD *)(a3 + 16) = 1;
}
else
{
if ( strcmp(optarg, "csv") )
{
result = 0xFFFFFFFFLL;
if ( trace_level >= 0 )
{
fprintf(stderr, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg);
result = 0xFFFFFFFFLL;
}
return result;
}
*(_DWORD *)(a3 + 16) = 2;
}
}
else if ( v5 <= 102 )
{
if ( v5 == 63 )
return 0xFFFFFFFFLL;
if ( v5 == 100 )
*(_QWORD *)a3 = optarg;
}
else
{
if ( v5 == 111 )
goto LABEL_9;
if ( v5 == 118 )
{
if ( optarg )
*(_DWORD *)(a3 + 20) = atoi(optarg);
else
++*(_DWORD *)(a3 + 20);
LABEL_9:
*(_QWORD *)(a3 + 8) = optarg;
}
}
}
result = 0LL;
if ( argc > optind )
{
do
LODWORD(result) = result + 1;
while ( (_DWORD)result != argc - optind );
optind = argc;
result = 0LL;
}
return result;
}

original_code:
int read_args(int argc, char *argv[], struct user_options *opts)
{
        int option_index = 0, c;
        static struct option long_options[] = {
                { .val = 'd', .name = "device", .has_arg = 1, },
                { .val = 'v', .name = "verbose", .has_arg = 2 },
                { .val = 'o', .name = "output", .has_arg = 1 },
                { .val = 'f', .name = "format", .has_arg = 1, },
        };
        char short_options[] = "d:v:o:f:";
        while (1) {
                c = getopt_long(argc, argv, short_options, long_options,
                                &option_index);
                if (c == -1)
                        break;
                switch (c) {
  case 'd':
   opts->usbdev = optarg;
   break;
  case 'v':
   if (optarg)
    opts->trace_level = atoi(optarg);
   else
    opts->trace_level++;
  case 'o':
   opts->output_path = optarg;
   break;
  case 'f':
   if ( strcmp(optarg, "raw") == 0 ) {
    opts->output_format = RAW;
   } else
   if ( strcmp(optarg, "clean") == 0 ) {
    opts->output_format = CLEAN;
   } else
   if ( strcmp(optarg, "csv") == 0 ) {
    opts->output_format = CSV;
   } else {
    do { if (0 <= trace_level) fprintf(stderr, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg); } while (0);
    return -1;
   }
   break;
                case '?':
   return -1;
                }
        }
        while (optind < argc) {
  optind++;
        }
 return 0;
}

canonical_original_code:
int read_args(int argc, char *argv[], struct user_options *opts)
{
        int option_index = 0; int c;
        static struct option long_options[] = {
                { .val = 'd', .name = "device", .has_arg = 1, },
                { .val = 'v', .name = "verbose", .has_arg = 2 },
                { .val = 'o', .name = "output", .has_arg = 1 },
                { .val = 'f', .name = "format", .has_arg = 1, },
        };
        char short_options[] = "d:v:o:f:";
        while (1) {
                c = getopt_long(argc, argv, short_options, long_options,
                                &option_index);
                if (c == -1)
                        break;
                switch (c) {
  case 'd':
   opts->usbdev = optarg;
   break;
  case 'v':
   if (optarg)
    opts->trace_level = atoi(optarg);
   else
    opts->trace_level++;
  case 'o':
   opts->output_path = optarg;
   break;
  case 'f':
   if ( strcmp(optarg, "raw") == 0 ) {
    opts->output_format = RAW;
   } else
   if ( strcmp(optarg, "clean") == 0 ) {
    opts->output_format = CLEAN;
   } else
   if ( strcmp(optarg, "csv") == 0 ) {
    opts->output_format = CSV;
   } else {
    do { if (0 <= trace_level) fprintf(stderr, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg); } while (0);
    return -1;
   }
   break;
                case '?':
   return -1;
                }
        }
        while (optind < argc) {
  optind++;
        }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct user_options { char *usbdev; char *output_path; enum { RAW=0, CLEAN=1, CSV=2 } output_format; int trace_level; } *

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


idle

repo: CofiTy/cofiTyHome

decompiled_code:
// attributes: thunk
void __noreturn idle()
{
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func0()
{
while ( 1 )
;
}

original_code:
void idle()
{
 for(;;);
}

canonical_original_code:
void idle()
{
 for(;;);
}


removeGThreadFromActivable

decompiled_code:
__int64 __fastcall removeGThreadFromActivable(__int64 *a1)
{
void *v1; // rcx
__int64 *v2; // rax
__int64 *v3; // rdx
__int64 result; // rax
__int64 v5; // rdx
__int64 v6; // rsi

v1 = firstThread;
v2 = (__int64 *)firstThread;
if ( firstThread == a1 )
{
v5 = *a1;
firstThread = (void *)*a1;
result = 1LL;
if ( (__int64 *)currentThread == a1 )
currentThread = v5;
}
else
{
while ( 1 )
{
v3 = v2;
v2 = (__int64 *)*v2;
if ( v2 == a1 )
break;
if ( !*v2 )
return 0xFFFFFFFFLL;
}
v6 = *a1;
*v3 = *a1;
result = 1LL;
if ( (__int64 *)currentThread == a1 )
{
if ( v6 )
{
currentThread = v6;
}
else
{
currentThread = (__int64)v1;
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1)
{
void *v1; // rcx
__int64 *v2; // rax
__int64 *v3; // rdx
__int64 result; // rax
__int64 v5; // rdx
__int64 v6; // rsi

v1 = firstThread;
v2 = (__int64 *)firstThread;
if ( firstThread == a1 )
{
v5 = *a1;
firstThread = (void *)*a1;
result = 1LL;
if ( (__int64 *)currentThread == a1 )
currentThread = v5;
}
else
{
while ( 1 )
{
v3 = v2;
v2 = (__int64 *)*v2;
if ( v2 == a1 )
break;
if ( !*v2 )
return 0xFFFFFFFFLL;
}
v6 = *a1;
*v3 = *a1;
result = 1LL;
if ( (__int64 *)currentThread == a1 )
{
if ( v6 )
{
currentThread = v6;
}
else
{
currentThread = (__int64)v1;
result = 1LL;
}
}
}
return result;
}

original_code:
static int removeGThreadFromActivable(gThread* toRemove)
{
 gThread* iter = firstThread;
 if (toRemove == firstThread)
 {
  firstThread = firstThread->next;
  if (currentThread == toRemove)
  {
   currentThread = firstThread;
  }
  return 1;
 }
 else
 {
  while (iter->next != toRemove)
  {
   iter = iter->next;
   if (iter->next == ((void *)0))
   {
    return -1;
   }
  }
  iter->next = toRemove->next;
  if (currentThread == toRemove)
  {
   currentThread = iter->next;
   if (currentThread == ((void *)0))
   {
    currentThread = firstThread;
   }
  }
 }
 return 1;
}

canonical_original_code:
static int removeGThreadFromActivable(struct gThread *toRemove)
{
 struct gThread *iter = firstThread;
 if (toRemove == firstThread)
 {
  firstThread = firstThread->next;
  if (currentThread == toRemove)
  {
   currentThread = firstThread;
  }
  return 1;
 }
 else
 {
  while (iter->next != toRemove)
  {
   iter = iter->next;
   if (iter->next == ((void *)0))
   {
    return -1;
   }
  }
  iter->next = toRemove->next;
  if (currentThread == toRemove)
  {
   currentThread = iter->next;
   if (currentThread == ((void *)0))
   {
    currentThread = firstThread;
   }
  }
 }
 return 1;
}

variable_types: struct gThread { struct gThread *next; struct mctx_st { int jb[6]; int toDelete; } context; int id; long timeToWait; char name[20]; int toDelete; char *stack; } *

user_defined_types: struct gThread { struct gThread *next; struct mctx_st context; int id; long timeToWait; char name[20]; int toDelete; char *stack; }


gc

decompiled_code:
void __noreturn gc()
{
void **v0; // rbp
void **v1; // rbx

while ( 1 )
{
do
v1 = (void **)threadForDeletion;
while ( !threadForDeletion );
do
{
v0 = v1;
v1 = (void **)*v1;
threadForDeletion = (__int64)v1;
free(v0[9]);
free(v0);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __noreturn func3()
{
void **v0; // rbp
void **v1; // rbx

while ( 1 )
{
do
v1 = (void **)threadForDeletion;
while ( !threadForDeletion );
do
{
v0 = v1;
v1 = (void **)*v1;
threadForDeletion = (__int64)v1;
free(v0[9]);
free(v0);
}
while ( v1 );
}
}

original_code:
void gc()
{
 for(;;)
 {
  while (threadForDeletion != ((void *)0))
  {
   gThread* toDeletion = threadForDeletion;
   threadForDeletion = threadForDeletion->next;
   free(toDeletion->stack);
   free(toDeletion);
  }
 }
}

canonical_original_code:
void gc()
{
 for(;;)
 {
  while (threadForDeletion != ((void *)0))
  {
   struct gThread *toDeletion = threadForDeletion;
   threadForDeletion = threadForDeletion->next;
   free(toDeletion->stack);
   free(toDeletion);
  }
 }
}

user_defined_types: struct mctx_st { int jb[6]; int toDelete; }


disableInterrupt

decompiled_code:
void disableInterrupt()
{
itEnabled = 0;
}

canonical_decompiled_code:
void func4()
{
itEnabled = 0;
}


enableInterrupt

decompiled_code:
void enableInterrupt()
{
itEnabled = 1;
}

canonical_decompiled_code:
void func5()
{
itEnabled = 1;
}


killThreadById

decompiled_code:
__int64 __fastcall killThreadById(unsigned int a1)
{
void *v1; // rbx
__int64 result; // rax

disableInterrupt();
if ( a1 > 2 && (v1 = firstThread) != 0LL )
{
while ( *((_DWORD *)v1 + 9) != a1 )
{
v1 = *(void **)v1;
if ( !v1 )
goto LABEL_5;
}
if ( (unsigned int)removeGThreadFromActivable((__int64 *)v1) != 1 )
removeGThreadFromWaiting((__int64 *)v1);
free(*((void **)v1 + 9));
free(v1);
enableInterrupt();
result = 1LL;
}
else
{
LABEL_5:
enableInterrupt();
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
void *v1; // rbx
__int64 result; // rax

func4();
if ( a1 > 2 && (v1 = firstThread) != 0LL )
{
while ( *((_DWORD *)v1 + 9) != a1 )
{
v1 = *(void **)v1;
if ( !v1 )
goto LABEL_5;
}
if ( (unsigned int)func2((__int64 *)v1) != 1 )
func1((__int64 *)v1);
free(*((void **)v1 + 9));
free(v1);
func5();
result = 1LL;
}
else
{
LABEL_5:
func5();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int killThreadById(int id)
{
 gThread* iter;
 disableInterrupt();
 if (id == 0 || id == 1 || id ==2 )
 {
  enableInterrupt();
  return -1;
 }
 iter = firstThread;
 while (iter != ((void *)0) && iter->id != id)
 {
  iter = iter->next;
 }
 if(iter == ((void *)0))
 {
  enableInterrupt();
  return -1;
 }
 if(removeGThreadFromActivable(iter) != 1)
 {
  removeGThreadFromWaiting(iter);
 }
 free(iter->stack);
 free(iter);
 enableInterrupt();
 return 1;
}

canonical_original_code:
int killThreadById(int id)
{
 struct gThread *iter;
 disableInterrupt();
 if (id == 0 || id == 1 || id ==2 )
 {
  enableInterrupt();
  return -1;
 }
 iter = firstThread;
 while (iter != ((void *)0) && iter->id != id)
 {
  iter = iter->next;
 }
 if(iter == ((void *)0))
 {
  enableInterrupt();
  return -1;
 }
 if(removeGThreadFromActivable(iter) != 1)
 {
  removeGThreadFromWaiting(iter);
 }
 free(iter->stack);
 free(iter);
 enableInterrupt();
 return 1;
}

variable_types: id int


gSleep

decompiled_code:
__int64 __fastcall gSleep(int a1)
{
time_t v1; // rax
__int64 *v2; // rbp
time_t v3; // rdx
__int64 *v4; // rbx
__int64 **v5; // r12
__int64 result; // rax

disableInterrupt();
if ( !counter )
initGThreadingSystem();
v1 = time(0LL);
v2 = (__int64 *)currentThread;
v3 = v1 + a1;
*(_QWORD *)(currentThread + 40) = v3;
v4 = (__int64 *)threadInWaitingState;
v5 = 0LL;
if ( threadInWaitingState )
{
while ( v3 > v4[5] )
{
v5 = (__int64 **)v4;
if ( !*v4 )
{
v4 = 0LL;
break;
}
v4 = (__int64 *)*v4;
}
}
else
{
v5 = (__int64 **)threadInWaitingState;
}
removeGThreadFromActivable(v2);
*v2 = (__int64)v4;
if ( v5 )
*v5 = v2;
else
threadInWaitingState = (__int64)v2;
enableInterrupt();
result = gSetJmp(v2 + 1);
if ( !(_DWORD)result )
{
gLongJmp(currentThread + 8, 1LL);
result = exitCurrentThread();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
time_t v1; // rax
__int64 *v2; // rbp
time_t v3; // rdx
__int64 *v4; // rbx
__int64 **v5; // r12
__int64 result; // rax

func4();
if ( !counter )
func7();
v1 = time(0LL);
v2 = (__int64 *)currentThread;
v3 = v1 + a1;
*(_QWORD *)(currentThread + 40) = v3;
v4 = (__int64 *)threadInWaitingState;
v5 = 0LL;
if ( threadInWaitingState )
{
while ( v3 > v4[5] )
{
v5 = (__int64 **)v4;
if ( !*v4 )
{
v4 = 0LL;
break;
}
v4 = (__int64 *)*v4;
}
}
else
{
v5 = (__int64 **)threadInWaitingState;
}
func2(v2);
*v2 = (__int64)v4;
if ( v5 )
*v5 = v2;
else
threadInWaitingState = (__int64)v2;
func5();
result = gSetJmp(v2 + 1);
if ( !(_DWORD)result )
{
gLongJmp(currentThread + 8, 1LL);
result = func10();
}
return result;
}

original_code:
void gSleep(int seconds)
{
 gThread *toSleep, *iter, *prev;
 disableInterrupt();
 if(counter == 0)
 {
  initGThreadingSystem();
 }
 currentThread->timeToWait = time(((void *)0)) + seconds;
 prev = ((void *)0);
 iter = threadInWaitingState;
 while(iter != ((void *)0)
  && iter->timeToWait < currentThread->timeToWait)
 {
  prev = iter;
  iter = iter->next;
 }
 toSleep = currentThread;
 removeGThreadFromActivable(currentThread);
 toSleep->next = iter;
 if(prev == ((void *)0))
 {
  threadInWaitingState = toSleep;
 }
 else
 {
  prev->next = toSleep;
 }
 enableInterrupt();
 if (gSetJmp((&(toSleep->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
}

canonical_original_code:
void gSleep(int seconds)
{
 struct gThread *toSleep; struct gThread *iter; struct gThread *prev;
 disableInterrupt();
 if(counter == 0)
 {
  initGThreadingSystem();
 }
 currentThread->timeToWait = time(((void *)0)) + seconds;
 prev = ((void *)0);
 iter = threadInWaitingState;
 while(iter != ((void *)0)
  && iter->timeToWait < currentThread->timeToWait)
 {
  prev = iter;
  iter = iter->next;
 }
 toSleep = currentThread;
 removeGThreadFromActivable(currentThread);
 toSleep->next = iter;
 if(prev == ((void *)0))
 {
  threadInWaitingState = toSleep;
 }
 else
 {
  prev->next = toSleep;
 }
 enableInterrupt();
 if (gSetJmp((&(toSleep->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
}

variable_types: seconds int


deleteSemaphore

decompiled_code:
__int64 __fastcall deleteSemaphore(_QWORD *a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( !a1[1] )
{
free(a1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( !a1[1] )
{
free(a1);
result = 1LL;
}
return result;
}

original_code:
int deleteSemaphore(semaphore* sem)
{
 if (sem->inWait != ((void *)0))
 {
  return -1;
 }
 free(sem);
 return 1;
}

canonical_original_code:
int deleteSemaphore(struct semaphore *sem)
{
 if (sem->inWait != ((void *)0))
 {
  return -1;
 }
 free(sem);
 return 1;
}

variable_types: struct semaphore { int counter; struct gThread { struct gThread *next; struct mctx_st { int jb[6]; int toDelete; } context; int id; long timeToWait; char name[20]; int toDelete; char *stack; } *inWait; } *

user_defined_types: struct semaphore { int counter; struct gThread *inWait; }


semTake

decompiled_code:
__int64 __fastcall semTake(__int64 a1)
{
__int64 result; // rax
_QWORD *v2; // rbp

disableInterrupt();
if ( *(_DWORD *)a1 )
{
result = (unsigned int)(*(_DWORD *)a1 - 1);
*(_DWORD *)a1 = result;
}
else
{
v2 = (_QWORD *)currentThread;
removeGThreadFromActivable((__int64 *)currentThread);
*v2 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v2;
enableInterrupt();
result = gSetJmp(v2 + 1);
if ( !(_DWORD)result )
{
gLongJmp(currentThread + 8, 1LL);
result = semGive();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 result; // rax
_QWORD *v2; // rbp

func4();
if ( *(_DWORD *)a1 )
{
result = (unsigned int)(*(_DWORD *)a1 - 1);
*(_DWORD *)a1 = result;
}
else
{
v2 = (_QWORD *)currentThread;
func2((__int64 *)currentThread);
*v2 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v2;
func5();
result = gSetJmp(v2 + 1);
if ( !(_DWORD)result )
{
gLongJmp(currentThread + 8, 1LL);
result = func14();
}
}
return result;
}

original_code:
void semTake(semaphore *sem)
{
 gThread *save;
 disableInterrupt();
 if (sem->counter == 0)
 {
  save = currentThread;
  removeGThreadFromActivable(currentThread);
  save->next = sem->inWait;
  sem->inWait = save;
  enableInterrupt();
  if (gSetJmp((&(save->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
 }
 else
 {
 sem->counter--;
 }
}

canonical_original_code:
void semTake(struct semaphore *sem)
{
 struct gThread *save;
 disableInterrupt();
 if (sem->counter == 0)
 {
  save = currentThread;
  removeGThreadFromActivable(currentThread);
  save->next = sem->inWait;
  sem->inWait = save;
  enableInterrupt();
  if (gSetJmp((&(save->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
 }
 else
 {
 sem->counter--;
 }
}


semGive

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

disableInterrupt();
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
{
*(_QWORD *)(a1 + 8) = *v1;
*v1 = firstThread;
firstThread = v1;
}
else
{
++*(_DWORD *)a1;
}
enableInterrupt();
}

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

func4();
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
{
*(_QWORD *)(a1 + 8) = *v1;
*v1 = firstThread;
firstThread = v1;
}
else
{
++*(_DWORD *)a1;
}
func5();
}

original_code:
void semGive(semaphore *sem)
{
 gThread *save;
 disableInterrupt();
 if (sem->inWait != ((void *)0))
 {
  save = sem->inWait;;
  sem->inWait = sem->inWait->next;
  save->next = firstThread;
  firstThread = save;
 }
 else
 {
  sem->counter++;
 }
 enableInterrupt();
}

canonical_original_code:
void semGive(struct semaphore *sem)
{
 struct gThread *save;
 disableInterrupt();
 if (sem->inWait != ((void *)0))
 {
  save = sem->inWait;;
  sem->inWait = sem->inWait->next;
  save->next = firstThread;
  firstThread = save;
 }
 else
 {
  sem->counter++;
 }
 enableInterrupt();
}


newMessageQueue

decompiled_code:
_QWORD *newMessageQueue()
{
_QWORD *v0; // rbx

disableInterrupt();
v0 = malloc(0x10uLL);
*v0 = 0LL;
v0[1] = 0LL;
enableInterrupt();
return v0;
}

canonical_decompiled_code:
_QWORD *func15()
{
_QWORD *v0; // rbx

func4();
v0 = malloc(0x10uLL);
*v0 = 0LL;
v0[1] = 0LL;
func5();
return v0;
}

original_code:
messageQueue *newMessageQueue()
{
 messageQueue *new;
 disableInterrupt();
 new = malloc(sizeof(messageQueue));
 new->inBox = ((void *)0);
 new->inWait = ((void *)0);
 enableInterrupt();
 return new;
}

canonical_original_code:
struct messageQueue *newMessageQueue()
{
 struct messageQueue *new;
 disableInterrupt();
 new = malloc(sizeof(messageQueue));
 new->inBox = ((void *)0);
 new->inWait = ((void *)0);
 enableInterrupt();
 return new;
}

variable_types: struct messageQueue { struct message { char *data; int size; struct message *next; } *inBox; struct gThread { struct gThread *next; struct mctx_st { int jb[6]; int toDelete; } context; int id; long timeToWait; char name[20]; int toDelete; char *stack; } *inWait; } *

user_defined_types: struct messageQueue { struct message *inBox; struct gThread *inWait; }


receiveMessage

decompiled_code:
__int64 __fastcall receiveMessage(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rax
unsigned int v4; // ebp
_QWORD *v6; // rbx

while ( 1 )
{
disableInterrupt();
v2 = *a1;
if ( *a1 )
break;
v6 = (_QWORD *)currentThread;
removeGThreadFromActivable((__int64 *)currentThread);
*v6 = a1[1];
a1[1] = (__int64)v6;
enableInterrupt();
if ( !(unsigned int)gSetJmp(v6 + 1) )
{
gLongJmp(currentThread + 8, 1LL);
return sendMessage();
}
}
if ( *(int *)(v2 + 8) > 0 )
{
v3 = 0LL;
do
{
*(_BYTE *)(a2 + v3) = *(_BYTE *)(*(_QWORD *)v2 + v3);
++v3;
}
while ( *(_DWORD *)(v2 + 8) > (int)v3 );
}
*a1 = *(_QWORD *)(v2 + 16);
v4 = *(_DWORD *)(v2 + 8);
free(*(void **)v2);
free((void *)v2);
enableInterrupt();
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rax
unsigned int v4; // ebp
_QWORD *v6; // rbx

while ( 1 )
{
func4();
v2 = *a1;
if ( *a1 )
break;
v6 = (_QWORD *)currentThread;
func2((__int64 *)currentThread);
*v6 = a1[1];
a1[1] = (__int64)v6;
func5();
if ( !(unsigned int)gSetJmp(v6 + 1) )
{
gLongJmp(currentThread + 8, 1LL);
return func17();
}
}
if ( *(int *)(v2 + 8) > 0 )
{
v3 = 0LL;
do
{
*(_BYTE *)(a2 + v3) = *(_BYTE *)(*(_QWORD *)v2 + v3);
++v3;
}
while ( *(_DWORD *)(v2 + 8) > (int)v3 );
}
*a1 = *(_QWORD *)(v2 + 16);
v4 = *(_DWORD *)(v2 + 8);
free(*(void **)v2);
free((void *)v2);
func5();
return v4;
}

original_code:
int receiveMessage(messageQueue *mQ, char* buffer)
{
 int i;
 int size;
 gThread *save;
 message *toDelete;
 for(;;)
 {
  disableInterrupt();
  toDelete = mQ->inBox;
  if (toDelete != ((void *)0))
  {
   for (i=0;i<toDelete->size;i++)
   {
    buffer[i] = toDelete->data[i];
   }
   mQ->inBox = toDelete->next;
   size = toDelete->size;
   free(toDelete->data);
   free(toDelete);
   enableInterrupt();
   return size;
  }
  save = currentThread;
  removeGThreadFromActivable(currentThread);
  save->next = mQ->inWait;
  mQ->inWait = save;
  enableInterrupt();
  if (gSetJmp((&(save->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
 }
}

canonical_original_code:
int receiveMessage(struct messageQueue *mQ, char *buffer)
{
 int i;
 int size;
 struct gThread *save;
 struct message *toDelete;
 for(;;)
 {
  disableInterrupt();
  toDelete = mQ->inBox;
  if (toDelete != ((void *)0))
  {
   for (i=0;i<toDelete->size;i++)
   {
    buffer[i] = toDelete->data[i];
   }
   mQ->inBox = toDelete->next;
   size = toDelete->size;
   free(toDelete->data);
   free(toDelete);
   enableInterrupt();
   return size;
  }
  save = currentThread;
  removeGThreadFromActivable(currentThread);
  save->next = mQ->inWait;
  mQ->inWait = save;
  enableInterrupt();
  if (gSetJmp((&(save->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
 }
}

variable_types:
buffer
char *

variable_types: i int

variable_types: size int

variable_types: struct message { char *data; int size; struct message *next; } *


sendMessage

original_code:
void sendMessage(messageQueue *mQ, char* myMessage, int messageSize)
{
 int i;
 message *iter;
 message *new;
 gThread *toRestore;
 disableInterrupt();
 if (messageSize == 0)
 {
  return;
 }
 new = malloc(sizeof(message));
 new->data = malloc(messageSize);
 new->size = messageSize;
 new->next = ((void *)0);
 for (i=0; i<messageSize;i++)
 {
  new->data[i] = myMessage[i];
 }
 if (mQ->inBox == ((void *)0))
 {
  mQ->inBox = new;
 }
 else
 {
  for(iter=mQ->inBox;iter->next!=((void *)0);iter=iter->next);
  iter->next = new;
 }
 if (mQ->inWait!=((void *)0))
 {
  toRestore = mQ->inWait;
  mQ->inWait = mQ->inWait->next;
  toRestore->next = firstThread;
  firstThread = toRestore;
 }
 enableInterrupt();
 return;
}

canonical_original_code:
void sendMessage(struct messageQueue *mQ, char *myMessage, int messageSize)
{
 int i;
 struct message *iter;
 struct message *new;
 struct gThread *toRestore;
 disableInterrupt();
 if (messageSize == 0)
 {
  return;
 }
 new = malloc(sizeof(message));
 new->data = malloc(messageSize);
 new->size = messageSize;
 new->next = ((void *)0);
 for (i=0; i<messageSize;i++)
 {
  new->data[i] = myMessage[i];
 }
 if (mQ->inBox == ((void *)0))
 {
  mQ->inBox = new;
 }
 else
 {
  for(iter=mQ->inBox;iter->next!=((void *)0);iter=iter->next);
  iter->next = new;
 }
 if (mQ->inWait!=((void *)0))
 {
  toRestore = mQ->inWait;
  mQ->inWait = mQ->inWait->next;
  toRestore->next = firstThread;
  firstThread = toRestore;
 }
 enableInterrupt();
 return;
}

variable_types:
myMessage
char *

variable_types: messageSize int

user_defined_types: struct message { char *data; int size; struct message *next; }


createGThread

decompiled_code:
__int64 __fastcall createGThread(__int64 a1, __int64 a2, int a3, __int64 a4)
{
int v6; // eax
unsigned int *v7; // rbx
__int64 i; // rax
int v9; // eax
void *v10; // rax

disableInterrupt();
v6 = counter++;
if ( v6 == -1 )
initGThreadingSystem();
if ( a3 < 0x100000 )
a3 = 0x100000;
v7 = (unsigned int *)malloc(0x50uLL);
for ( i = 0LL; i != 20; ++i )
*((_BYTE *)v7 + i + 48) = *(_BYTE *)(a4 + i);
v9 = counter++;
v7[9] = v9;
v10 = malloc(a3);
*((_QWORD *)v7 + 9) = v10;
mctx_create(v7 + 2, a1, a2, v10, 0x100000LL);
*(_QWORD *)v7 = firstThread;
firstThread = v7;
itEnabled = 1;
enableInterrupt();
return v7[9];
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3, __int64 a4)
{
int v6; // eax
unsigned int *v7; // rbx
__int64 i; // rax
int v9; // eax
void *v10; // rax

func4();
v6 = counter++;
if ( v6 == -1 )
func7();
if ( a3 < 0x100000 )
a3 = 0x100000;
v7 = (unsigned int *)malloc(0x50uLL);
for ( i = 0LL; i != 20; ++i )
*((_BYTE *)v7 + i + 48) = *(_BYTE *)(a4 + i);
v9 = counter++;
v7[9] = v9;
v10 = malloc(a3);
*((_QWORD *)v7 + 9) = v10;
mctx_create(v7 + 2, a1, a2, v10, 0x100000LL);
*(_QWORD *)v7 = firstThread;
firstThread = v7;
itEnabled = 1;
func5();
return v7[9];
}

original_code:
int createGThread(void (*sf_addr)(void*),void *sf_arg, int stackSize, char* name)
{
 int i;
 gThread *newThread;
 disableInterrupt();
 if (counter++ == -1)
 {
  initGThreadingSystem();
 }
 if(stackSize < 1048576)
 {
  stackSize = 1048576;
 }
 newThread = malloc(sizeof(gThread));
 for (i=0; i<20;i++)
 {
  newThread->name[i] = name[i];
 }
 newThread->id = counter++;
 newThread->stack = malloc(stackSize);
 mctx_create(&(newThread->context), sf_addr,sf_arg, newThread->stack, 1048576);
 newThread->next = firstThread;
 firstThread = newThread;
 itEnabled = 1;
 enableInterrupt();
 return newThread->id;
}

canonical_original_code:
int createGThread(void (*sf_addr)(void *), void *sf_arg, int stackSize, char *name)
{
 int i;
 struct gThread *newThread;
 disableInterrupt();
 if (counter++ == -1)
 {
  initGThreadingSystem();
 }
 if(stackSize < 1048576)
 {
  stackSize = 1048576;
 }
 newThread = malloc(sizeof(gThread));
 for (i=0; i<20;i++)
 {
  newThread->name[i] = name[i];
 }
 newThread->id = counter++;
 newThread->stack = malloc(stackSize);
 mctx_create(&(newThread->context), sf_addr,sf_arg, newThread->stack, 1048576);
 newThread->next = firstThread;
 firstThread = newThread;
 itEnabled = 1;
 enableInterrupt();
 return newThread->id;
}

variable_types: stackSize int

variable_types:
name
char *


initGThreadingSystem

decompiled_code:
__int64 initGThreadingSystem()
{
_DWORD *v0; // rbx
int v1; // eax
struct itimerval v3; // [rsp+0h] [rbp-28h] BYREF

v0 = malloc(0x50uLL);
v1 = counter++;
v0[9] = v1;
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 9) = malloc(0x100000uLL);
*((_BYTE *)v0 + 48) = 109;
*((_BYTE *)v0 + 49) = 97;
*((_BYTE *)v0 + 50) = 105;
*((_BYTE *)v0 + 51) = 110;
*((_BYTE *)v0 + 52) = 0;
firstThread = v0;
currentThread = (__int64)v0;
sigemptyset(&set);
sa_0.sa_handler = (__sighandler_t)yield;
dword_8E8 = 1342177280;
sigaction(14, &sa_0, 0LL);
v3.it_interval.tv_sec = 0LL;
v3.it_interval.tv_usec = 80000LL;
v3.it_value = (struct timeval)_mm_loadu_si128((const __m128i *)&v3);
setitimer(ITIMER_REAL, &v3, 0LL);
createGThread((__int64)idle, 0LL, 100, (__int64)"idle");
return createGThread((__int64)gc, 0LL, 0, (__int64)"gc");
}

canonical_decompiled_code:
__int64 func7()
{
_DWORD *v0; // rbx
int v1; // eax
struct itimerval v3; // [rsp+0h] [rbp-28h] BYREF

v0 = malloc(0x50uLL);
v1 = counter++;
v0[9] = v1;
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 9) = malloc(0x100000uLL);
*((_BYTE *)v0 + 48) = 109;
*((_BYTE *)v0 + 49) = 97;
*((_BYTE *)v0 + 50) = 105;
*((_BYTE *)v0 + 51) = 110;
*((_BYTE *)v0 + 52) = 0;
firstThread = v0;
currentThread = (__int64)v0;
sigemptyset(&set);
sa_0.sa_handler = (__sighandler_t)yield;
dword_8E8 = 1342177280;
sigaction(14, &sa_0, 0LL);
v3.it_interval.tv_sec = 0LL;
v3.it_interval.tv_usec = 80000LL;
v3.it_value = (struct timeval)_mm_loadu_si128((const __m128i *)&v3);
setitimer(ITIMER_REAL, &v3, 0LL);
func6((__int64)idle, 0LL, 100, (__int64)"idle");
return func6((__int64)gc, 0LL, 0, (__int64)"gc");
}

original_code:
static void initGThreadingSystem()
{
 gThread *mainThread = malloc(sizeof(gThread));
 static struct sigaction sa;
 struct itimerval value;
 firstThread = ((void *)0);
 mainThread->id = counter++;
 mainThread->next = firstThread;
 mainThread->stack = malloc(1048576);
 mainThread->name[0] = 'm';
 mainThread->name[1] = 'a';
 mainThread->name[2] = 'i';
 mainThread->name[3] = 'n';
 mainThread->name[4] = '\0';
 firstThread = mainThread;
 currentThread = mainThread;
 sigemptyset(&sa.sa_mask);
 sa.__sigaction_handler.sa_handler = yield;
 sa.sa_flags = 0x10000000 | 0x40000000;
 sigaction(14, &sa, (struct sigaction *)0);
 value.it_interval.tv_sec = 0;
 value.it_interval.tv_usec = 80*1000;
 value.it_value = value.it_interval;
 setitimer(ITIMER_REAL, &value, (struct itimerval *)0);
 createGThread(&idle,((void *)0),100,"idle");
 createGThread(&gc, ((void *)0), 0,"gc");
}

canonical_original_code:
static void initGThreadingSystem()
{
 struct gThread *mainThread = malloc(sizeof(gThread));
 static struct sigaction sa;
 struct itimerval value;
 firstThread = ((void *)0);
 mainThread->id = counter++;
 mainThread->next = firstThread;
 mainThread->stack = malloc(1048576);
 mainThread->name[0] = 'm';
 mainThread->name[1] = 'a';
 mainThread->name[2] = 'i';
 mainThread->name[3] = 'n';
 mainThread->name[4] = '\0';
 firstThread = mainThread;
 currentThread = mainThread;
 sigemptyset(&sa.sa_mask);
 sa.__sigaction_handler.sa_handler = yield;
 sa.sa_flags = 0x10000000 | 0x40000000;
 sigaction(14, &sa, (struct sigaction *)0);
 value.it_interval.tv_sec = 0;
 value.it_interval.tv_usec = 80*1000;
 value.it_value = value.it_interval;
 setitimer(ITIMER_REAL, &value, (struct itimerval *)0);
 createGThread(&idle,((void *)0),100,"idle");
 createGThread(&gc, ((void *)0), 0,"gc");
}

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

variable_types: struct itimerval { struct timeval { long tv_sec; long tv_usec; } it_interval; struct timeval it_value; }

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

user_defined_types: struct itimerval { struct timeval it_interval; struct timeval it_value; }

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


exitCurrentThread

decompiled_code:
__int64 exitCurrentThread()
{
__int64 *v0; // rbx
__int64 result; // rax

v0 = (__int64 *)currentThread;
if ( currentThread )
{
if ( *(_DWORD *)(currentThread + 36) > 2u )
{
disableInterrupt();
removeGThreadFromActivable(v0);
*v0 = threadForDeletion;
threadForDeletion = (__int64)v0;
enableInterrupt();
gLongJmp(currentThread + 8, 1LL);
result = yield();
}
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 *v0; // rbx
__int64 result; // rax

v0 = (__int64 *)currentThread;
if ( currentThread )
{
if ( *(_DWORD *)(currentThread + 36) > 2u )
{
func4();
func2(v0);
*v0 = threadForDeletion;
threadForDeletion = (__int64)v0;
func5();
gLongJmp(currentThread + 8, 1LL);
result = func11();
}
}
return result;
}

original_code:
void exitCurrentThread()
{
 gThread *save;
 if(currentThread == ((void *)0))
 {
  return;
 }
 if(currentThread->id == 0
  || currentThread->id == 1
  || currentThread->id == 2)
 {
  return;
 }
 disableInterrupt();
 save = currentThread;
 removeGThreadFromActivable(currentThread);
 save->next = threadForDeletion;
 threadForDeletion = save;
 enableInterrupt();
 gLongJmp((&(currentThread->context))->jb, 1);
}

canonical_original_code:
void exitCurrentThread()
{
 struct gThread *save;
 if(currentThread == ((void *)0))
 {
  return;
 }
 if(currentThread->id == 0
  || currentThread->id == 1
  || currentThread->id == 2)
 {
  return;
 }
 disableInterrupt();
 save = currentThread;
 removeGThreadFromActivable(currentThread);
 save->next = threadForDeletion;
 threadForDeletion = save;
 enableInterrupt();
 gLongJmp((&(currentThread->context))->jb, 1);
}


yield

decompiled_code:
void yield()
{
__int64 *i; // rbx
time_t v1; // rbp
__int64 v2; // rdi

if ( itEnabled == 1 )
{
for ( i = (__int64 *)threadInWaitingState; threadInWaitingState; i = (__int64 *)threadInWaitingState )
{
v1 = i[5];
if ( v1 > time(0LL) )
break;
removeGThreadFromWaiting(i);
*i = (__int64)firstThread;
firstThread = i;
}
if ( *(_DWORD *)(currentThread + 32) )
exitCurrentThread();
v2 = currentThread;
currentThread = *(_QWORD *)currentThread;
if ( !currentThread )
currentThread = (__int64)firstThread;
if ( (unsigned int)gSetJmp(v2 + 8) )
{
enableInterrupt();
}
else
{
gLongJmp(currentThread + 8, 1LL);
newSemaphore();
}
}
}

canonical_decompiled_code:
void func11()
{
__int64 *i; // rbx
time_t v1; // rbp
__int64 v2; // rdi

if ( itEnabled == 1 )
{
for ( i = (__int64 *)threadInWaitingState; threadInWaitingState; i = (__int64 *)threadInWaitingState )
{
v1 = i[5];
if ( v1 > time(0LL) )
break;
func1(i);
*i = (__int64)firstThread;
firstThread = i;
}
if ( *(_DWORD *)(currentThread + 32) )
func10();
v2 = currentThread;
currentThread = *(_QWORD *)currentThread;
if ( !currentThread )
currentThread = (__int64)firstThread;
if ( (unsigned int)gSetJmp(v2 + 8) )
{
func5();
}
else
{
gLongJmp(currentThread + 8, 1LL);
newSemaphore();
}
}
}

original_code:
void yield()
{
 gThread *old;
 if (itEnabled == 1)
 {
  while(threadInWaitingState != ((void *)0)
  && threadInWaitingState->timeToWait <= time(((void *)0)))
  {
   gThread *threadToWakeUp = threadInWaitingState;
   removeGThreadFromWaiting(threadToWakeUp);
   threadToWakeUp->next = firstThread;
   firstThread = threadToWakeUp;
  }
  if(currentThread->context.toDelete)
  {
   exitCurrentThread();
  }
  old = currentThread;
  currentThread = currentThread->next;
  if (currentThread == ((void *)0))
  {
   currentThread = firstThread;
  }
  if (gSetJmp((&(old->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
  enableInterrupt();
 }
}

canonical_original_code:
void yield()
{
 struct gThread *old;
 if (itEnabled == 1)
 {
  while(threadInWaitingState != ((void *)0)
  && threadInWaitingState->timeToWait <= time(((void *)0)))
  {
   struct gThread *threadToWakeUp = threadInWaitingState;
   removeGThreadFromWaiting(threadToWakeUp);
   threadToWakeUp->next = firstThread;
   firstThread = threadToWakeUp;
  }
  if(currentThread->context.toDelete)
  {
   exitCurrentThread();
  }
  old = currentThread;
  currentThread = currentThread->next;
  if (currentThread == ((void *)0))
  {
   currentThread = firstThread;
  }
  if (gSetJmp((&(old->context))->jb) == 0) gLongJmp((&(currentThread->context))->jb, 1);
  enableInterrupt();
 }
}


openCAFFE

decompiled_code:
int openCAFFE()
{
return puts("Demarrer la machine a caffe!!");
}

canonical_decompiled_code:
int func0()
{
return puts("Demarrer la machine a caffe!!");
}

original_code:
void openCAFFE(char id[9]) {
  puts("Demarrer la machine a caffe!!");
}

variable_types:
id
char [9]


closeCAFFE

decompiled_code:
int closeCAFFE()
{
return puts("Arreter la machine a caffe!!");
}

canonical_decompiled_code:
int func1()
{
return puts("Arreter la machine a caffe!!");
}

original_code:
void closeCAFFE(char id[9]) {
  puts("Arreter la machine a caffe!!");
}


closeCHAUFFAGE

decompiled_code:
int closeCHAUFFAGE()
{
return puts("Arreter le chauffage!");
}

canonical_decompiled_code:
int func2()
{
return puts("Arreter le chauffage!");
}

original_code:
void closeCHAUFFAGE(char id[9]) {
  puts("Arreter le chauffage!");
}


openCHAUFFAGE

decompiled_code:
int openCHAUFFAGE()
{
return puts("Demarrer le chauffage!");
}

canonical_decompiled_code:
int func3()
{
return puts("Demarrer le chauffage!");
}

original_code:
void openCHAUFFAGE(char id[9]) {
  puts("Demarrer le chauffage!");
}


openVOLETS

decompiled_code:
int openVOLETS()
{
return puts("Ouvrir les volets");
}

canonical_decompiled_code:
int func4()
{
return puts("Ouvrir les volets");
}

original_code:
void openVOLETS(char id[9]) {
  puts("Ouvrir les volets");
}


closeVOLETS

decompiled_code:
int closeVOLETS()
{
return puts("Fermer les volets");
}

canonical_decompiled_code:
int func5()
{
return puts("Fermer les volets");
}

original_code:
void closeVOLETS(char id[9]) {
  puts("Fermer les volets");
}


jeuMYSTERE

decompiled_code:
void jeuMYSTERE()
{
if ( onGoingJeu != 1 )
{
onGoingJeu = 1;
pJ = popen("armagetronad", "w");
if ( !pJ )
{
perror("popen");
exit(-3);
}
}
}

canonical_decompiled_code:
void func20()
{
if ( onGoingJeu != 1 )
{
onGoingJeu = 1;
pJ = popen("armagetronad", "w");
if ( !pJ )
{
perror("popen");
exit(-3);
}
}
}

original_code:
void jeuMYSTERE(char id[9]){
  if(onGoingJeu != 1){
    onGoingJeu = 1;
    if((pJ = popen("armagetronad", "w")) == ((void *)0)){
      perror("popen");
      exit(-3);
    }
  }
}


finJeuMYSTERE

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

if ( onGoingJeu == 1 )
{
system("killall -q armagetronad.real");
result = pclose(pJ);
onGoingJeu = 0;
}
return result;
}

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

if ( onGoingJeu == 1 )
{
system("killall -q armagetronad.real");
result = pclose(pJ);
onGoingJeu = 0;
}
return result;
}

original_code:
void finJeuMYSTERE(char id[9]){
  if(onGoingJeu == 1){
    system("killall -q armagetronad.real");
    pclose(pJ);
    onGoingJeu = 0;
  }
}


finMYSTERE

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

if ( onGoing == 1 )
{
fwrite("q\n", 1uLL, 2uLL, pp);
sleep(1u);
result = pclose(pp);
onGoing = 0;
}
return result;
}

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

if ( onGoing == 1 )
{
fwrite("q\n", 1uLL, 2uLL, pp);
sleep(1u);
result = pclose(pp);
onGoing = 0;
}
return result;
}

original_code:
void finMYSTERE(char id[9]){
  if(onGoing == 1){
    fputs("q\n", pp);
    sleep(1);
    pclose(pp);
    onGoing = 0;
  }
}


actionMYSTERE

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

if ( onGoing == 1 )
finMYSTERE();
onGoing = 1;
result = popen("mplayer weAre.mp3", "w");
pp = result;
if ( !result )
{
perror("popen");
exit(-3);
}
return result;
}

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

if ( onGoing == 1 )
func22();
onGoing = 1;
result = popen("mplayer weAre.mp3", "w");
pp = result;
if ( !result )
{
perror("popen");
exit(-3);
}
return result;
}

original_code:
void actionMYSTERE(char id[9]){
  if(onGoing == 1){
    finMYSTERE(((void *)0));
  }
  onGoing = 1;
  if((pp = popen("mplayer weAre.mp3", "w")) == ((void *)0)){
    perror("popen");
    exit(-3);
  }
}


itochar

original_code:
void itochar(int toBeTrans, char* buffer, int radix)
{
  int i = 0, n, reste;
  char* reverseBuffer = (char*) gMalloc((sizeof (buffer) + 1));
  n = toBeTrans;
  while (n > 0) {
    reste = n % radix;
    n = n / radix;
    switch (reste) {
      case 10:
        {
          reverseBuffer[i++] = 'A';
          break;
        }
      case 11:
        {
          reverseBuffer[i++] = 'B';
          break;
        }
      case 12:
        {
          reverseBuffer[i++] = 'C';
          break;
        }
      case 13:
        {
          reverseBuffer[i++] = 'D';
          break;
        }
      case 14:
        {
          reverseBuffer[i++] = 'E';
          break;
        }
      case 15:
        {
          reverseBuffer[i++] = 'F';
          break;
        }
      default:
        {
          reverseBuffer[i++] = '0' + reste;
          break;
        }
    }
  }
  reverseBuffer[i] = '\0';
  strncpy(buffer, reverseBuffer, i);
  gFree(reverseBuffer);
}

canonical_original_code:
void itochar(int toBeTrans, char *buffer, int radix)
{
  int i = 0; int n; int reste;
  char *reverseBuffer = (char*) gMalloc((sizeof (buffer) + 1));
  n = toBeTrans;
  while (n > 0) {
    reste = n % radix;
    n = n / radix;
    switch (reste) {
      case 10:
        {
          reverseBuffer[i++] = 'A';
          break;
        }
      case 11:
        {
          reverseBuffer[i++] = 'B';
          break;
        }
      case 12:
        {
          reverseBuffer[i++] = 'C';
          break;
        }
      case 13:
        {
          reverseBuffer[i++] = 'D';
          break;
        }
      case 14:
        {
          reverseBuffer[i++] = 'E';
          break;
        }
      case 15:
        {
          reverseBuffer[i++] = 'F';
          break;
        }
      default:
        {
          reverseBuffer[i++] = '0' + reste;
          break;
        }
    }
  }
  reverseBuffer[i] = '\0';
  strncpy(buffer, reverseBuffer, i);
  gFree(reverseBuffer);
}

variable_types: toBeTrans int

variable_types: radix int

variable_types: n int

variable_types: reste int

variable_types:
reverseBuffer
char *


oneCharHexToInt

decompiled_code:
__int64 __fastcall oneCharHexToInt(char a1)
{
__int64 result; // rax

switch ( a1 )
{
case '0':
result = 0LL;
break;
case '1':
result = 1LL;
break;
case '2':
result = 2LL;
break;
case '3':
result = 3LL;
break;
case '4':
result = 4LL;
break;
case '5':
result = 5LL;
break;
case '6':
result = 6LL;
break;
case '7':
result = 7LL;
break;
case '8':
result = 8LL;
break;
case '9':
result = 9LL;
break;
case 'A':
result = 10LL;
break;
case 'B':
result = 11LL;
break;
case 'C':
result = 12LL;
break;
case 'D':
result = 13LL;
break;
case 'E':
result = 14LL;
break;
case 'F':
result = 15LL;
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func25(char a1)
{
__int64 result; // rax

switch ( a1 )
{
case '0':
result = 0LL;
break;
case '1':
result = 1LL;
break;
case '2':
result = 2LL;
break;
case '3':
result = 3LL;
break;
case '4':
result = 4LL;
break;
case '5':
result = 5LL;
break;
case '6':
result = 6LL;
break;
case '7':
result = 7LL;
break;
case '8':
result = 8LL;
break;
case '9':
result = 9LL;
break;
case 'A':
result = 10LL;
break;
case 'B':
result = 11LL;
break;
case 'C':
result = 12LL;
break;
case 'D':
result = 13LL;
break;
case 'E':
result = 14LL;
break;
case 'F':
result = 15LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int oneCharHexToInt(char hex) {
  int result;
  switch (hex) {
    case 'A':
      {
        result = 10;
        break;
      }
    case 'B':
      {
        result = 11;
        break;
      }
    case 'C':
      {
        result = 12;
        break;
      }
    case 'D':
      {
        result = 13;
        break;
      }
    case 'E':
      {
        result = 14;
        break;
      }
    case 'F':
      {
        result = 15;
        break;
      }
    case '0':
      {
        result = 0;
        break;
      }
    case '1':
      {
        result = 1;
        break;
      }
    case '2':
      {
        result = 2;
        break;
      }
    case '3':
      {
        result = 3;
        break;
      }
    case '4':
      {
        result = 4;
        break;
      }
    case '5':
      {
        result = 5;
        break;
      }
    case '6':
      {
        result = 6;
        break;
      }
    case '7':
      {
        result = 7;
        break;
      }
    case '8':
      {
        result = 8;
        break;
      }
    case '9':
      {
        result = 9;
        break;
      }
    default:
      {
        break;
      }
  };
  return result;
}

variable_types: hex char

variable_types: result int


hexToInt

decompiled_code:
__int64 __fastcall hexToInt(char *a1)
{
int v1; // ebp

v1 = oneCharHexToInt(a1[1]);
return v1 + 16 * (unsigned int)oneCharHexToInt(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func26(char *a1)
{
int v1; // ebp

v1 = func25(a1[1]);
return v1 + 16 * (unsigned int)func25(*a1);
}

original_code:
int hexToInt(char* hex) {
  int result = 0, i, pow = 1, j = 0, cont = 0;
  for (i = 1; i >= 0; i--) {
    for (j = 0; j < cont; j++) {
      pow = pow * 16;
    }
    cont++;
    result += pow * oneCharHexToInt(hex[i]);
  }
  return result;
}

canonical_original_code:
int hexToInt(char *hex) {
  int result = 0; int i; int pow = 1; int j = 0; int cont = 0;
  for (i = 1; i >= 0; i--) {
    for (j = 0; j < cont; j++) {
      pow = pow * 16;
    }
    cont++;
    result += pow * oneCharHexToInt(hex[i]);
  }
  return result;
}

variable_types:
hex
char *

variable_types: i int

variable_types: pow int

variable_types: j int

variable_types: cont int