main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // er13
int v6; // er12
int v7; // eax
int v8; // ebp
const char *v9; // r15
const char *v10; // r15
int v11; // eax
const char **v12; // rbx
const char *v13; // rdi
int v14; // eax
int v15; // ebp
__int64 v16; // rcx
__int16 *v17; // rdi
int v18; // eax
__pid_t v19; // eax
char *v20; // rax
int *v21; // rax
int *v22; // rax
__int64 v23; // rcx
__int16 *v24; // rdi
int v25; // edi
int *v26; // rax
int *v27; // rax
int *v28; // rax
int *v29; // rax
__int64 v31; // rcx
__int16 *v32; // rdi
int v33; // [rsp+0h] [rbp-78h]
int v34; // [rsp+4h] [rbp-74h]
int v35; // [rsp+8h] [rbp-70h]
int oflag; // [rsp+Ch] [rbp-6Ch]
__int16 v37[12]; // [rsp+10h] [rbp-68h] BYREF
unsigned int v38; // [rsp+28h] [rbp-50h]
__WAIT_STATUS stat_loc; // [rsp+3Ch] [rbp-3Ch] BYREF

v3 = *argv;
v4 = strrchr(*argv, 47);
if ( v4 )
{
progname = (__int64)(v4 + 1);
*argv = v4 + 1;
}
else
{
progname = (__int64)v3;
}
if ( argc == 1 )
{
printf(
"%s: execute a program with a lock set.\n"
"Usage: %s [options] lockfile program [arguments]\n"
"where options are:\n"
" -w - if the lock is already held by another process,\n"
" wait for it to complete instead of failing immediately\n"
" -W sec - the same as -w but wait not more than sec seconds\n"
" -e - execute the program directly, no fork/wait\n"
" (keeps extra open file descriptor)\n"
" -E nnn - set the fd# to keep open in -e case (implies -e)\n"
" -n - do not create the lock file if it does not exist\n"
" -q - produce no output if lock is already held\n"
" -s - lock in shared (read) mode\n"
" -x - lock in exclusive (write) mode (default)\n"
" -t - test for lock existence (just prints pid if any with -q)\n"
" (implies -n)\n",
(const char *)progname,
(const char *)progname);
return 0;
}
v5 = -1;
v35 = 0;
v34 = 0;
v6 = 0;
v33 = 1;
oflag = 64;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "+wW:neE:sxtq");
LODWORD(stat_loc.__uptr) = v7;
if ( v7 != -1 )
{
switch ( v7 )
{
case 'E':
v10 = optarg;
v11 = atoi(optarg);
v5 = v11;
if ( v11 == 2 || v11 < 0 )
error(0, 64, "invalid fd# `%s'", v10);
v33 = 0;
continue;
case 'W':
v9 = optarg;
v6 = atoi(optarg);
if ( v6 <= 0 )
error(0, 64, "invalid wait time `%s'", v9);
continue;
case 'e':
v33 = 0;
continue;
case 'n':
oflag = 0;
continue;
case 'q':
quiet = 1;
continue;
case 's':
v34 = 1;
continue;
case 't':
v35 = 1;
oflag = 0;
continue;
case 'w':
if ( !v6 )
v6 = -1;
continue;
case 'x':
v34 = 0;
continue;
default:
return 64;
}
}
break;
}
v12 = &argv[optind];
if ( argc == optind || argc - optind <= 1 && !v35 )
error(0, 64, "too few arguments given");
v13 = *v12;
*(_QWORD *)lckfile = *v12;
if ( !v35 )
{
v14 = open(v13, oflag | (v34 == 0), 438LL);
v15 = v14;
if ( v14 >= 0 )
{
if ( v5 >= 0 )
{
if ( dup2(v14, v5) < 0 )
{
v22 = __errno_location();
error(*v22, 71, "dup2(%d,%d) failed", (unsigned int)v15, (unsigned int)v5);
}
close(v15);
v15 = v5;
}
if ( v6 > 0 )
{
alarm(v6);
signal(14, (__sighandler_t)sigalarm);
v23 = 8LL;
v24 = v37;
while ( v23 )
{
*(_DWORD *)v24 = 0;
v24 += 2;
--v23;
}
v37[0] = v34 == 0;
}
else
{
v16 = 8LL;
v17 = v37;
while ( v16 )
{
*(_DWORD *)v17 = 0;
v17 += 2;
--v16;
}
v37[0] = v34 == 0;
if ( !v6 )
{
LODWORD(stat_loc.__uptr) = fcntl(v15, 6, v37);
goto LABEL_38;
}
}
LODWORD(stat_loc.__uptr) = fcntl(v15, 7, v37);
if ( v6 > 0 )
alarm(0);
goto LABEL_38;
}
LABEL_48:
v21 = __errno_location();
error(*v21, 73, "unable to open `%s'", *(const char **)lckfile);
}
v15 = open(v13, oflag, 438LL);
if ( v15 < 0 )
{
if ( *__errno_location() == 2 )
{
v8 = quiet;
if ( quiet )
return 0;
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
return v8;
}
goto LABEL_48;
}
v31 = 8LL;
v32 = v37;
while ( v31 )
{
*(_DWORD *)v32 = 0;
v32 += 2;
--v31;
}
v37[0] = v34 == 0;
LODWORD(stat_loc.__uptr) = fcntl(v15, 5);
if ( !LODWORD(stat_loc.__uptr) )
{
if ( v37[0] == 2 )
{
v8 = quiet;
if ( quiet )
v8 = 0;
else
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
}
else
{
if ( quiet )
printf("%d\n", v38);
else
printf("lockfile `%s' is locked by process %d\n", *(const char **)lckfile, v38);
v8 = 75;
}
return v8;
}
LABEL_38:
if ( SLODWORD(stat_loc.__uptr) < 0 )
{
v25 = *__errno_location();
if ( ((v25 - 11) & 0xFFFFFFFD) != 0 )
error(v25, 71, "unable to lock `%s'", *(const char **)lckfile);
v8 = 75;
if ( !quiet )
error(0, 75, "lockfile `%s' is already locked", *(const char **)lckfile);
}
else
{
if ( !v33 )
goto LABEL_67;
v18 = fcntl(v15, 1, 0LL);
if ( v18 < 0 )
{
v26 = __errno_location();
error(*v26, 71, "fcntl() failed");
}
fcntl(v15, 2, v18 | 1u);
v19 = fork();
if ( v19 < 0 )
{
v27 = __errno_location();
error(*v27, 71, "unable to fork");
}
if ( !v19 )
{
LABEL_67:
execvp(v12[1], (char *const *)v12 + 1);
v29 = __errno_location();
error(*v29, 71, "unable to execute %s", v12[1]);
}
if ( wait((__WAIT_STATUS)&stat_loc) < 0 )
{
v28 = __errno_location();
error(*v28, 71, "wait() failed");
}
v8 = BYTE1(stat_loc.__uptr);
if ( !__OFSUB__(((__int64)stat_loc.__uptr & 0x7F) + 1, 1) && ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
v20 = strsignal((__int64)stat_loc.__uptr & 0x7F);
error(0, 70, "%s: %s", v12[1], v20);
}
}
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // er13
int v6; // er12
int v7; // eax
int v8; // ebp
const char *v9; // r15
const char *v10; // r15
int v11; // eax
const char **v12; // rbx
const char *v13; // rdi
int v14; // eax
int v15; // ebp
__int64 v16; // rcx
__int16 *v17; // rdi
int v18; // eax
__pid_t v19; // eax
char *v20; // rax
int *v21; // rax
int *v22; // rax
__int64 v23; // rcx
__int16 *v24; // rdi
int v25; // edi
int *v26; // rax
int *v27; // rax
int *v28; // rax
int *v29; // rax
__int64 v31; // rcx
__int16 *v32; // rdi
int v33; // [rsp+0h] [rbp-78h]
int v34; // [rsp+4h] [rbp-74h]
int v35; // [rsp+8h] [rbp-70h]
int oflag; // [rsp+Ch] [rbp-6Ch]
__int16 v37[12]; // [rsp+10h] [rbp-68h] BYREF
unsigned int v38; // [rsp+28h] [rbp-50h]
__WAIT_STATUS stat_loc; // [rsp+3Ch] [rbp-3Ch] BYREF

v3 = *argv;
v4 = strrchr(*argv, 47);
if ( v4 )
{
progname = (__int64)(v4 + 1);
*argv = v4 + 1;
}
else
{
progname = (__int64)v3;
}
if ( argc == 1 )
{
printf(
"%s: execute a program with a lock set.\n"
"Usage: %s [options] lockfile program [arguments]\n"
"where options are:\n"
" -w - if the lock is already held by another process,\n"
" wait for it to complete instead of failing immediately\n"
" -W sec - the same as -w but wait not more than sec seconds\n"
" -e - execute the program directly, no fork/wait\n"
" (keeps extra open file descriptor)\n"
" -E nnn - set the fd# to keep open in -e case (implies -e)\n"
" -n - do not create the lock file if it does not exist\n"
" -q - produce no output if lock is already held\n"
" -s - lock in shared (read) mode\n"
" -x - lock in exclusive (write) mode (default)\n"
" -t - test for lock existence (just prints pid if any with -q)\n"
" (implies -n)\n",
(const char *)progname,
(const char *)progname);
return 0;
}
v5 = -1;
v35 = 0;
v34 = 0;
v6 = 0;
v33 = 1;
oflag = 64;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "+wW:neE:sxtq");
LODWORD(stat_loc.__uptr) = v7;
if ( v7 != -1 )
{
switch ( v7 )
{
case 'E':
v10 = optarg;
v11 = atoi(optarg);
v5 = v11;
if ( v11 == 2 || v11 < 0 )
func7(0, 64, "invalid fd# `%s'", v10);
v33 = 0;
continue;
case 'W':
v9 = optarg;
v6 = atoi(optarg);
if ( v6 <= 0 )
func7(0, 64, "invalid wait time `%s'", v9);
continue;
case 'e':
v33 = 0;
continue;
case 'n':
oflag = 0;
continue;
case 'q':
quiet = 1;
continue;
case 's':
v34 = 1;
continue;
case 't':
v35 = 1;
oflag = 0;
continue;
case 'w':
if ( !v6 )
v6 = -1;
continue;
case 'x':
v34 = 0;
continue;
default:
return 64;
}
}
break;
}
v12 = &argv[optind];
if ( argc == optind || argc - optind <= 1 && !v35 )
func7(0, 64, "too few arguments given");
v13 = *v12;
*(_QWORD *)lckfile = *v12;
if ( !v35 )
{
v14 = open(v13, oflag | (v34 == 0), 438LL);
v15 = v14;
if ( v14 >= 0 )
{
if ( v5 >= 0 )
{
if ( dup2(v14, v5) < 0 )
{
v22 = __errno_location();
func7(*v22, 71, "dup2(%d,%d) failed", (unsigned int)v15, (unsigned int)v5);
}
close(v15);
v15 = v5;
}
if ( v6 > 0 )
{
alarm(v6);
signal(14, (__sighandler_t)sigalarm);
v23 = 8LL;
v24 = v37;
while ( v23 )
{
*(_DWORD *)v24 = 0;
v24 += 2;
--v23;
}
v37[0] = v34 == 0;
}
else
{
v16 = 8LL;
v17 = v37;
while ( v16 )
{
*(_DWORD *)v17 = 0;
v17 += 2;
--v16;
}
v37[0] = v34 == 0;
if ( !v6 )
{
LODWORD(stat_loc.__uptr) = fcntl(v15, 6, v37);
goto LABEL_38;
}
}
LODWORD(stat_loc.__uptr) = fcntl(v15, 7, v37);
if ( v6 > 0 )
alarm(0);
goto LABEL_38;
}
LABEL_48:
v21 = __errno_location();
func7(*v21, 73, "unable to open `%s'", *(const char **)lckfile);
}
v15 = open(v13, oflag, 438LL);
if ( v15 < 0 )
{
if ( *__errno_location() == 2 )
{
v8 = quiet;
if ( quiet )
return 0;
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
return v8;
}
goto LABEL_48;
}
v31 = 8LL;
v32 = v37;
while ( v31 )
{
*(_DWORD *)v32 = 0;
v32 += 2;
--v31;
}
v37[0] = v34 == 0;
LODWORD(stat_loc.__uptr) = fcntl(v15, 5);
if ( !LODWORD(stat_loc.__uptr) )
{
if ( v37[0] == 2 )
{
v8 = quiet;
if ( quiet )
v8 = 0;
else
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
}
else
{
if ( quiet )
printf("%d\n", v38);
else
printf("lockfile `%s' is locked by process %d\n", *(const char **)lckfile, v38);
v8 = 75;
}
return v8;
}
LABEL_38:
if ( SLODWORD(stat_loc.__uptr) < 0 )
{
v25 = *__errno_location();
if ( ((v25 - 11) & 0xFFFFFFFD) != 0 )
func7(v25, 71, "unable to lock `%s'", *(const char **)lckfile);
v8 = 75;
if ( !quiet )
func7(0, 75, "lockfile `%s' is already locked", *(const char **)lckfile);
}
else
{
if ( !v33 )
goto LABEL_67;
v18 = fcntl(v15, 1, 0LL);
if ( v18 < 0 )
{
v26 = __errno_location();
func7(*v26, 71, "fcntl() failed");
}
fcntl(v15, 2, v18 | 1u);
v19 = fork();
if ( v19 < 0 )
{
v27 = __errno_location();
func7(*v27, 71, "unable to fork");
}
if ( !v19 )
{
LABEL_67:
execvp(v12[1], (char *const *)v12 + 1);
v29 = __errno_location();
func7(*v29, 71, "unable to execute %s", v12[1]);
}
if ( wait((__WAIT_STATUS)&stat_loc) < 0 )
{
v28 = __errno_location();
func7(*v28, 71, "wait() failed");
}
v8 = BYTE1(stat_loc.__uptr);
if ( !__OFSUB__(((__int64)stat_loc.__uptr & 0x7F) + 1, 1) && ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
v20 = strsignal((__int64)stat_loc.__uptr & 0x7F);
func7(0, 70, "%s: %s", v12[1], v20);
}
}
return v8;
}

original_code:
int main(int argc, char **argv) {
 int fd;
 int c;
 int create = 0100;
 int dofork = 1;
 int waittime = 0;
 int shared = 0;
 int test = 0;
 int fdn = -1;
 struct flock fl;
 if ((progname = strrchr(argv[0], '/')) == ((void *)0))
  progname = argv[0];
 else
  argv[0] = ++progname;
 if (argc == 1) {
  printf(
   "%s: execute a program with a lock set.\n"
   "Usage: %s [options] lockfile program [arguments]\n"
   "where options are:\n"
   " -w - if the lock is already held by another process,\n"
   "   wait for it to complete instead of failing immediately\n"
   " -W sec - the same as -w but wait not more than sec seconds\n"
   " -e - execute the program directly, no fork/wait\n"
   "   (keeps extra open file descriptor)\n"
   " -E nnn - set the fd# to keep open in -e case (implies -e)\n"
   " -n - do not create the lock file if it does not exist\n"
   " -q - produce no output if lock is already held\n"
   " -s - lock in shared (read) mode\n"
   " -x - lock in exclusive (write) mode (default)\n"
   " -t - test for lock existence"
   " (just prints pid if any with -q)\n"
   "   (implies -n)\n"
  , progname, progname);
  return 0;
 }
 while ((c = getopt(argc, argv, "+wW:neE:sxtq")) != (-1)) {
  switch(c) {
  case 'w':
   if (!waittime)
    waittime = -1;
   break;
  case 'W':
   if ((waittime = atoi(optarg)) < 1)
    error(0, 64, "invalid wait time `%s'", optarg);
   break;
  case 't':
   test = 1;
  case 'n':
   create = 0;
   break;
  case 'E':
   if ((fdn = atoi(optarg)) < 0 || fdn == 2)
    error(0, 64, "invalid fd# `%s'", optarg);
  case 'e':
   dofork = 0;
   break;
  case 's':
   shared = 1;
   break;
  case 'x':
   shared = 0;
   break;
  case 'q':
   quiet = 1;
   break;
  default:
   return 64;
  }
 }
 argc -= optind; argv += optind;
 if (!argc || (!test && argc < 2))
  error(0, 64, "too few arguments given");
 lckfile = *argv++;
 if (!test)
  create |= shared ? 00 : 01;
 fd = open(lckfile, create, 0666);
 if (fd < 0) {
  if (test && (*__errno_location ()) == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  error((*__errno_location ()), 73, "unable to open `%s'", lckfile);
 }
 if (!test && fdn >= 0) {
  if (dup2(fd, fdn) < 0)
   error((*__errno_location ()), 71, "dup2(%d,%d) failed", fd, fdn);
  close(fd);
  fd = fdn;
 }
 if (test)
  waittime = 0;
 else if (waittime > 0) {
  alarm(waittime);
  signal(14, sigalarm);
 }
 memset(&fl, 0, sizeof(fl));
 fl.l_type = shared ? 0 : 1;
 c = fcntl(fd, test ? 5 : waittime ? 7 : 6, &fl);
 if (test && c == 0) {
  if (fl.l_type == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  if (!quiet)
   printf("lockfile `%s' is locked by process %d\n", lckfile, fl.l_pid);
  else
   printf("%d\n", fl.l_pid);
  return 75;
 }
 if (waittime > 0)
  alarm(0);
 if (c < 0) {
  if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11 && (*__errno_location ()) != 13)
   error((*__errno_location ()), 71, "unable to lock `%s'", lckfile);
  else if (quiet)
   return 75;
  else
   error(0, 75, "lockfile `%s' is already locked", lckfile);
 }
 if (dofork) {
  pid_t pid;
  int flags = fcntl(fd, 1, 0);
  if (flags < 0)
   error((*__errno_location ()), 71, "fcntl() failed");
  fcntl(fd, 2, flags | 1);
  pid = fork();
  if (pid < 0)
   error((*__errno_location ()), 71, "unable to fork");
  else if (pid) {
   if (wait(&c) < 0)
    error((*__errno_location ()), 71, "wait() failed");
   if ((((signed char) (((c) & 0x7f) + 1) >> 1) > 0))
    error(0, 70, "%s: %s", *argv,
      strsignal(((c) & 0x7f)));
   return (((c) & 0xff00) >> 8);
  }
 }
 execvp(*argv, argv);
 error((*__errno_location ()), 71, "unable to execute %s", *argv);
}

canonical_original_code:
int main(int argc, char **argv) {
 int fd;
 int c;
 int create = 0100;
 int dofork = 1;
 int waittime = 0;
 int shared = 0;
 int test = 0;
 int fdn = -1;
 struct flock fl;
 if ((progname = strrchr(argv[0], '/')) == ((void *)0))
  progname = argv[0];
 else
  argv[0] = ++progname;
 if (argc == 1) {
  printf(
   "%s: execute a program with a lock set.\n"
   "Usage: %s [options] lockfile program [arguments]\n"
   "where options are:\n"
   " -w - if the lock is already held by another process,\n"
   "   wait for it to complete instead of failing immediately\n"
   " -W sec - the same as -w but wait not more than sec seconds\n"
   " -e - execute the program directly, no fork/wait\n"
   "   (keeps extra open file descriptor)\n"
   " -E nnn - set the fd# to keep open in -e case (implies -e)\n"
   " -n - do not create the lock file if it does not exist\n"
   " -q - produce no output if lock is already held\n"
   " -s - lock in shared (read) mode\n"
   " -x - lock in exclusive (write) mode (default)\n"
   " -t - test for lock existence"
   " (just prints pid if any with -q)\n"
   "   (implies -n)\n"
  , progname, progname);
  return 0;
 }
 while ((c = getopt(argc, argv, "+wW:neE:sxtq")) != (-1)) {
  switch(c) {
  case 'w':
   if (!waittime)
    waittime = -1;
   break;
  case 'W':
   if ((waittime = atoi(optarg)) < 1)
    error(0, 64, "invalid wait time `%s'", optarg);
   break;
  case 't':
   test = 1;
  case 'n':
   create = 0;
   break;
  case 'E':
   if ((fdn = atoi(optarg)) < 0 || fdn == 2)
    error(0, 64, "invalid fd# `%s'", optarg);
  case 'e':
   dofork = 0;
   break;
  case 's':
   shared = 1;
   break;
  case 'x':
   shared = 0;
   break;
  case 'q':
   quiet = 1;
   break;
  default:
   return 64;
  }
 }
 argc -= optind; argv += optind;
 if (!argc || (!test && argc < 2))
  error(0, 64, "too few arguments given");
 lckfile = *argv++;
 if (!test)
  create |= shared ? 00 : 01;
 fd = open(lckfile, create, 0666);
 if (fd < 0) {
  if (test && (*__errno_location ()) == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  error((*__errno_location ()), 73, "unable to open `%s'", lckfile);
 }
 if (!test && fdn >= 0) {
  if (dup2(fd, fdn) < 0)
   error((*__errno_location ()), 71, "dup2(%d,%d) failed", fd, fdn);
  close(fd);
  fd = fdn;
 }
 if (test)
  waittime = 0;
 else if (waittime > 0) {
  alarm(waittime);
  signal(14, sigalarm);
 }
 memset(&fl, 0, sizeof(fl));
 fl.l_type = shared ? 0 : 1;
 c = fcntl(fd, test ? 5 : waittime ? 7 : 6, &fl);
 if (test && c == 0) {
  if (fl.l_type == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  if (!quiet)
   printf("lockfile `%s' is locked by process %d\n", lckfile, fl.l_pid);
  else
   printf("%d\n", fl.l_pid);
  return 75;
 }
 if (waittime > 0)
  alarm(0);
 if (c < 0) {
  if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11 && (*__errno_location ()) != 13)
   error((*__errno_location ()), 71, "unable to lock `%s'", lckfile);
  else if (quiet)
   return 75;
  else
   error(0, 75, "lockfile `%s' is already locked", lckfile);
 }
 if (dofork) {
  int pid;
  int flags = fcntl(fd, 1, 0);
  if (flags < 0)
   error((*__errno_location ()), 71, "fcntl() failed");
  fcntl(fd, 2, flags | 1);
  pid = fork();
  if (pid < 0)
   error((*__errno_location ()), 71, "unable to fork");
  else if (pid) {
   if (wait(&c) < 0)
    error((*__errno_location ()), 71, "wait() failed");
   if ((((char) (((c) & 0x7f) + 1) >> 1) > 0))
    error(0, 70, "%s: %s", *argv,
      strsignal(((c) & 0x7f)));
   return (((c) & 0xff00) >> 8);
  }
 }
 execvp(*argv, argv);
 error((*__errno_location ()), 71, "unable to execute %s", *argv);
}

variable_types: fd int

variable_types: create int

variable_types: dofork int

variable_types: waittime int

variable_types: shared int

variable_types: test int

variable_types: fdn int

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }

variable_types: pid int

variable_types: flags int


repo: ghuls/moreutils-parallel

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

result = (unsigned __int8)a1 & 0x80;
if ( (a1 & 0x80) != 0 )
{
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
a1 *= 2;
}
while ( (a1 & 0x80u) != 0 );
}
return result;
}

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

result = (unsigned __int8)a1 & 0x80;
if ( (a1 & 0x80) != 0 )
{
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
a1 *= 2;
}
while ( (a1 & 0x80u) != 0 );
}
return result;
}

original_code:
static int high_ones(int c) {
        int n;
        for (n = 0; (c & 0x80) == 0x80; c <<= 1)
                ++n;
        return n;
}

variable_types: c int

variable_types: n int

return_type: int


decompiled_code:
__int64 __fastcall decodeutf8(char *a1, int a2)
{
__int64 result; // rax
int v3; // ebp
int v4; // ecx
__int64 v5; // rsi

result = 0xFFFFFFFFLL;
if ( a2 > 0 )
{
if ( a2 == 1 )
{
result = (unsigned __int8)*a1;
if ( *a1 < 0 )
result = 0xFFFFFFFFLL;
}
else
{
v3 = (unsigned __int8)*a1;
v4 = high_ones(v3);
result = 0xFFFFFFFFLL;
if ( a2 == v4 )
{
result = v3 & (unsigned int)(255 >> v4);
v5 = 1LL;
do
{
if ( (a1[v5] & 0xC0) != 0x80 )
return 0xFFFFFFFFLL;
result = a1[v5++] & 0x3F | (unsigned __int64)(result << 6);
}
while ( v4 > (int)v5 );
if ( (unsigned __int64)(result - 55296) <= 0x7FF )
{
result = 0xFFFFFFFFLL;
}
else if ( (unsigned __int64)(result - 65534) <= 1 )
{
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, int a2)
{
__int64 result; // rax
int v3; // ebp
int v4; // ecx
__int64 v5; // rsi

result = 0xFFFFFFFFLL;
if ( a2 > 0 )
{
if ( a2 == 1 )
{
result = (unsigned __int8)*a1;
if ( *a1 < 0 )
result = 0xFFFFFFFFLL;
}
else
{
v3 = (unsigned __int8)*a1;
v4 = func7(v3);
result = 0xFFFFFFFFLL;
if ( a2 == v4 )
{
result = v3 & (unsigned int)(255 >> v4);
v5 = 1LL;
do
{
if ( (a1[v5] & 0xC0) != 0x80 )
return 0xFFFFFFFFLL;
result = a1[v5++] & 0x3F | (unsigned __int64)(result << 6);
}
while ( v4 > (int)v5 );
if ( (unsigned __int64)(result - 55296) <= 0x7FF )
{
result = 0xFFFFFFFFLL;
}
else if ( (unsigned __int64)(result - 65534) <= 1 )
{
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
static unsigned long decodeutf8(unsigned char *buf, int nbytes)
{
        unsigned long u;
        int i, j;
        if (nbytes <= 0)
                return INVALID_CHAR;
        if (nbytes == 1) {
                if (buf[0] >= 0x80)
                        return INVALID_CHAR;
                return buf[0];
        }
        i = high_ones(buf[0]);
        if (i != nbytes)
                return INVALID_CHAR;
        u = buf[0] & (0xff >> i);
        for (j = 1; j < nbytes; ++j) {
                if ((buf[j] & 0xC0) != 0x80)
                            return INVALID_CHAR;
                u = (u << 6) | (buf[j] & 0x3f);
        }
        if (u >= 0xD800 && u <= 0xDFFF)
            return INVALID_CHAR;
        if (u == 0xFFFE || u == 0xFFFF)
            return INVALID_CHAR;
        return u;
}

canonical_original_code:
static unsigned long decodeutf8(unsigned char *buf, int nbytes)
{
        unsigned long u;
        int i; int j;
        if (nbytes <= 0)
                return INVALID_CHAR;
        if (nbytes == 1) {
                if (buf[0] >= 0x80)
                        return INVALID_CHAR;
                return buf[0];
        }
        i = high_ones(buf[0]);
        if (i != nbytes)
                return INVALID_CHAR;
        u = buf[0] & (0xff >> i);
        for (j = 1; j < nbytes; ++j) {
                if ((buf[j] & 0xC0) != 0x80)
                            return INVALID_CHAR;
                u = (u << 6) | (buf[j] & 0x3f);
        }
        if (u >= 0xD800 && u <= 0xDFFF)
            return INVALID_CHAR;
        if (u == 0xFFFE || u == 0xFFFF)
            return INVALID_CHAR;
        return u;
}

variable_types:
buf
unsigned char *

variable_types: nbytes int

variable_types:
u
unsigned long

variable_types: i int

variable_types: j int

return_type: unsigned long


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n", a1);
puts("Check whether input files are valid UTF-8.");
return printf("This is version %s.\n", "1.1");
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n", a1);
puts("Check whether input files are valid UTF-8.");
return printf("This is version %s.\n", "1.1");
}

original_code:
static void usage(const char *program_name) {
 printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n",
        program_name);
 printf("Check whether input files are valid UTF-8.\n");
 printf("This is version %s.\n", "1.1");
}

canonical_original_code:
static void usage(char *program_name) {
 printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n",
        program_name);
 printf("Check whether input files are valid UTF-8.\n");
 printf("This is version %s.\n", "1.1");
}

return_type: void


repo: gianrigo/rk-ocl

decompiled_code:
__int64 opencl_create_platform()
{
__int64 result; // rax

clGetPlatformIDs();
result = clGetPlatformIDs();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to create plataform.");
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax

clGetPlatformIDs();
result = clGetPlatformIDs();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to create plataform.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_platform(unsigned int num_platforms){
  cl_uint num_platforms_found;
  clGetPlatformIDs( 0, ((void *)0), &num_platforms_found);
  if ( clGetPlatformIDs( num_platforms, &_platform, &num_platforms_found ) != 0 ){
    printf("\nERROR: Failed to create plataform.\n");
    exit(-1);
  }
}

canonical_original_code:
void opencl_create_platform(unsigned int num_platforms){
  unsigned int num_platforms_found;
  clGetPlatformIDs( 0, ((void *)0), &num_platforms_found);
  if ( clGetPlatformIDs( num_platforms, &_platform, &num_platforms_found ) != 0 ){
    printf("\nERROR: Failed to create plataform.\n");
    exit(-1);
  }
}


decompiled_code:
__int64 opencl_create_context()
{
__int64 result; // rax

result = clCreateContext();
context = result;
if ( !result )
{
puts("\nERROR: Failed to create context.");
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 result; // rax

result = clCreateContext();
context = result;
if ( !result )
{
puts("\nERROR: Failed to create context.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_context(){
  if( (_context = clCreateContext( 0, 1, _devices, ((void *)0), ((void *)0), ((void *)0) )) == ((void *)0) ){
    printf("\nERROR: Failed to create context.\n");
    exit(-1);
  }
}


decompiled_code:
__int64 opencl_create_queue()
{
__int64 result; // rax

result = clCreateCommandQueue();
queue = result;
if ( !result )
{
puts("\nERROR: Failed to create queue.");
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 result; // rax

result = clCreateCommandQueue();
queue = result;
if ( !result )
{
puts("\nERROR: Failed to create queue.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_queue(){
  if((_queue = clCreateCommandQueue(_context, _devices[_device_used], (1 << 1), ((void *)0))) == ((void *)0) ){
    printf("\nERROR: Failed to create queue.\n");
    exit(-1);
  }
}


decompiled_code:
__int64 opencl_build_program()
{
__int64 result; // rax
const char *v1; // rbx
__int64 v2; // [rsp+8h] [rbp-20h]

result = clBuildProgram();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to build program.");
clGetProgramBuildInfo();
v1 = (const char *)malloc(v2 + 1);
clGetProgramBuildInfo();
v1[v2] = 0;
printf("BUILD LOG: \n %s\n", v1);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 result; // rax
const char *v1; // rbx
__int64 v2; // [rsp+8h] [rbp-20h]

result = clBuildProgram();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to build program.");
clGetProgramBuildInfo();
v1 = (const char *)malloc(v2 + 1);
clGetProgramBuildInfo();
v1[v2] = 0;
printf("BUILD LOG: \n %s\n", v1);
exit(-1);
}
return result;
}

original_code:
void opencl_build_program(){
  int err;
  char *build_log;
  size_t ret_val_size;
  err = clBuildProgram(_program, 0, ((void *)0), ((void *)0), ((void *)0), ((void *)0));
  if ( err != 0 ) {
    printf("\nERROR: Failed to build program.\n");
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, 0, ((void *)0), &ret_val_size);
    build_log = (char*) malloc((ret_val_size+1)*sizeof(char));
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, ret_val_size, build_log, ((void *)0));
    build_log[ret_val_size] = '\0';
    printf("BUILD LOG: \n %s\n", build_log);
    exit(-1);
  }
}

canonical_original_code:
void opencl_build_program(){
  int err;
  char *build_log;
  unsigned long ret_val_size;
  err = clBuildProgram(_program, 0, ((void *)0), ((void *)0), ((void *)0), ((void *)0));
  if ( err != 0 ) {
    printf("\nERROR: Failed to build program.\n");
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, 0, ((void *)0), &ret_val_size);
    build_log = (char *) malloc((ret_val_size+1)*sizeof(char));
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, ret_val_size, build_log, ((void *)0));
    build_log[ret_val_size] = '\0';
    printf("BUILD LOG: \n %s\n", build_log);
    exit(-1);
  }
}

variable_types: err int


decompiled_code:
__int64 opencl_create_program()
{
__int64 v0; // rdi
int v2; // [rsp+4h] [rbp-24h]
int v3; // [rsp+14h] [rbp-14h] BYREF
void *v4; // [rsp+18h] [rbp-10h]

v4 = load_program_from_source(&v3);
v0 = context;
program = clCreateProgramWithSource();
if ( v2 )
{
puts("\nERROR: Failed to create program.");
exit(-1);
}
return opencl_build_program(v0, 1LL);
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rdi
int v2; // [rsp+4h] [rbp-24h]
int v3; // [rsp+14h] [rbp-14h] BYREF
void *v4; // [rsp+18h] [rbp-10h]

v4 = func11(&v3);
v0 = context;
program = clCreateProgramWithSource();
if ( v2 )
{
puts("\nERROR: Failed to create program.");
exit(-1);
}
return func12(v0, 1LL);
}

original_code:
void opencl_create_program(){
  char* program_source;
  int size;
  size_t prog_size;
  cl_int err;
  program_source = load_program_from_source(&size);
  prog_size = (size_t)size;
  _program = clCreateProgramWithSource(_context, 1, (const char**)&program_source, &prog_size, &err);
  if ( err != 0 ){
    printf("\nERROR: Failed to create program.\n");
    exit(-1);
  }
  opencl_build_program();
}

canonical_original_code:
void opencl_create_program(){
  char *program_source;
  int size;
  unsigned long prog_size;
  int err;
  program_source = load_program_from_source(&size);
  prog_size = (unsigned long)size;
  _program = clCreateProgramWithSource(_context, 1, (char **)&program_source, &prog_size, &err);
  if ( err != 0 ){
    printf("\nERROR: Failed to create program.\n");
    exit(-1);
  }
  opencl_build_program();
}

variable_types: size int


decompiled_code:
__int64 __fastcall opencl_create_kernel(const char *a1)
{
__int64 result; // rax

result = clCreateKernel();
kernel = result;
if ( !result )
{
printf("\nERROR: Failed to create kernel %s.\n", a1);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(const char *a1)
{
__int64 result; // rax

result = clCreateKernel();
kernel = result;
if ( !result )
{
printf("\nERROR: Failed to create kernel %s.\n", a1);
exit(-1);
}
return result;
}

original_code:
void opencl_create_kernel(char* kernel_name){
  _kernel = clCreateKernel(_program, kernel_name, ((void *)0));
  if ( _kernel == ((void *)0) ){
    printf("\nERROR: Failed to create kernel %s.\n",kernel_name);
    exit(-1);
  }
}

canonical_original_code:
void opencl_create_kernel(char *kernel_name){
  _kernel = clCreateKernel(_program, kernel_name, ((void *)0));
  if ( _kernel == ((void *)0) ){
    printf("\nERROR: Failed to create kernel %s.\n",kernel_name);
    exit(-1);
  }
}


decompiled_code:
void __fastcall opencl_run_kernel(unsigned int a1, int a2)
{
float *v2; // r13
unsigned int *v3; // r15
int i; // er14
unsigned int j; // ebx
int v6; // ebp
unsigned int *ptr; // [rsp+8h] [rbp-50h]

ptr = (unsigned int *)malloc(4LL * a1);
v2 = (float *)malloc(12LL * a2 * a1);
clEnqueueNDRangeKernel();
clReleaseEvent();
clFinish();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"n_points\".");
exit(-1);
}
clReleaseEvent();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"points\".");
exit(-1);
}
clReleaseEvent();
if ( a1 )
{
v3 = ptr;
for ( i = 0; i != a1; ++i )
{
v6 = i;
for ( j = 0; *v3 > j; v6 += 7 )
{
printf("%f %f %f\n", v2[3 * v6], v2[3 * v6 + 1], v2[3 * v6 + 2]);
++j;
}
putchar(10);
++v3;
}
}
free(ptr);
free(v2);
}

canonical_decompiled_code:
void __fastcall func16(unsigned int a1, int a2)
{
float *v2; // r13
unsigned int *v3; // r15
int i; // er14
unsigned int j; // ebx
int v6; // ebp
unsigned int *ptr; // [rsp+8h] [rbp-50h]

ptr = (unsigned int *)malloc(4LL * a1);
v2 = (float *)malloc(12LL * a2 * a1);
clEnqueueNDRangeKernel();
clReleaseEvent();
clFinish();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"n_points\".");
exit(-1);
}
clReleaseEvent();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"points\".");
exit(-1);
}
clReleaseEvent();
if ( a1 )
{
v3 = ptr;
for ( i = 0; i != a1; ++i )
{
v6 = i;
for ( j = 0; *v3 > j; v6 += 7 )
{
printf("%f %f %f\n", v2[3 * v6], v2[3 * v6 + 1], v2[3 * v6 + 2]);
++j;
}
putchar(10);
++v3;
}
}
free(ptr);
free(v2);
}

original_code:
void opencl_run_kernel(unsigned int count_v0, unsigned int max_points){
  size_t work_dim[1];
  unsigned int i, j, *n_points;
  vector *points;
  n_points = (unsigned int*) malloc(count_v0*sizeof(unsigned int));
  points = (vector*) malloc(count_v0*max_points*sizeof(vector));
  work_dim[0] = count_v0;
  clEnqueueNDRangeKernel(_queue, _kernel, 1, ((void *)0), work_dim, ((void *)0), 0, ((void *)0), &_event);
  clReleaseEvent(_event);
  clFinish(_queue);
  if( clEnqueueReadBuffer(_queue, _opencl_n_points, 1, 0, sizeof(unsigned int)*count_v0, n_points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"n_points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  if( clEnqueueReadBuffer(_queue, _opencl_points, 1, 0, sizeof(vector)*count_v0*max_points, points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  for(i = 0; i < count_v0; i++){
    for(j = 0; j < n_points[i]; j++){
     printf("%f %f %f\n", points[(i+7*j)].x,points[(i+7*j)].y,points[(i+7*j)].z);
    }
    printf("\n");
  }
  free(n_points);
  free(points);
}

canonical_original_code:
void opencl_run_kernel(unsigned int count_v0, unsigned int max_points){
  unsigned long work_dim[1];
  unsigned int i; unsigned int j; unsigned int *n_points;
  struct vec *points;
  n_points = (unsigned int *) malloc(count_v0*sizeof(unsigned int));
  points = (struct vec *) malloc(count_v0*max_points*sizeof(vector));
  work_dim[0] = count_v0;
  clEnqueueNDRangeKernel(_queue, _kernel, 1, ((void *)0), work_dim, ((void *)0), 0, ((void *)0), &_event);
  clReleaseEvent(_event);
  clFinish(_queue);
  if( clEnqueueReadBuffer(_queue, _opencl_n_points, 1, 0, sizeof(unsigned int)*count_v0, n_points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"n_points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  if( clEnqueueReadBuffer(_queue, _opencl_points, 1, 0, sizeof(vector)*count_v0*max_points, points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  for(i = 0; i < count_v0; i++){
    for(j = 0; j < n_points[i]; j++){
     printf("%f %f %f\n", points[(i+7*j)].x,points[(i+7*j)].y,points[(i+7*j)].z);
    }
    printf("\n");
  }
  free(n_points);
  free(points);
}

variable_types:
i
unsigned int

variable_types:
j
unsigned int

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


decompiled_code:
void __fastcall opencl_init(const char *a1, _DWORD a2, signed int a3)
{
__int64 v4; // rax
double v5; // xmm1_8
int v6; // [rsp+1Ch] [rbp-3Ch]

opencl_create_platform(2LL);
opencl_get_devices_id();
opencl_create_context();
opencl_create_queue();
opencl_create_program();
opencl_create_kernel(a1);
clGetDeviceInfo();
v4 = 12LL * a3;
if ( v4 < 0 )
v5 = (double)(int)((unsigned __int64)v4 >> 1) + (double)(int)((unsigned __int64)v4 >> 1);
else
v5 = (double)(int)v4;
prepare_kernel();
opencl_run_kernel(a3, (int)((double)v6 * 0.9 / v5));
}

canonical_decompiled_code:
void __fastcall func17(const char *a1, _DWORD a2, signed int a3)
{
__int64 v4; // rax
double v5; // xmm1_8
int v6; // [rsp+1Ch] [rbp-3Ch]

func7(2LL);
func8();
func9();
func10();
func13();
func14(a1);
clGetDeviceInfo();
v4 = 12LL * a3;
if ( v4 < 0 )
v5 = (double)(int)((unsigned __int64)v4 >> 1) + (double)(int)((unsigned __int64)v4 >> 1);
else
v5 = (double)(int)v4;
func15();
func16(a3, (int)((double)v6 * 0.9 / v5));
}

original_code:
void opencl_init(char* kernel_name, vector *v0, int count_v0, double h, int n_x, int n_y, int n_z, vector_field field ){
  unsigned int max_points, available;
  opencl_create_platform(2);
  opencl_get_devices_id();
  opencl_create_context();
  opencl_create_queue();
  opencl_create_program();
  opencl_create_kernel(kernel_name);
  clGetDeviceInfo(*_devices,0x101F,sizeof(available),&available,((void *)0));
  max_points = (available*0.9)/(sizeof(vector)*count_v0);
  prepare_kernel(v0, count_v0, h, n_x, n_y, n_z, field, max_points);
  opencl_run_kernel(count_v0, max_points);
}

canonical_original_code:
void opencl_init(char *kernel_name, struct vec *v0, int count_v0, double h, int n_x, int n_y, int n_z, struct vec *field){
  unsigned int max_points; unsigned int available;
  opencl_create_platform(2);
  opencl_get_devices_id();
  opencl_create_context();
  opencl_create_queue();
  opencl_create_program();
  opencl_create_kernel(kernel_name);
  clGetDeviceInfo(*_devices,0x101F,sizeof(available),&available,((void *)0));
  max_points = (available*0.9)/(sizeof(vector)*count_v0);
  prepare_kernel(v0, count_v0, h, n_x, n_y, n_z, field, max_points);
  opencl_run_kernel(count_v0, max_points);
}

variable_types: h double

variable_types:
field
struct vec *

variable_types:
available
unsigned int

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


repo: gibsson/linux-adk

decompiled_code:
void signal_handler()
{
puts("SIGINT: Closing accessory");
stop_acc = 1;
}

canonical_decompiled_code:
void func0()
{
puts("SIGINT: Closing accessory");
stop_acc = 1;
}

original_code:
static void signal_handler(int signo)
{
 printf("SIGINT: Closing accessory\n");
 stop_acc = 1;
}

variable_types: signo int


decompiled_code:
int __fastcall show_version(const char *a1)
{
return printf("%s v%s\nreport bugs to %s\n", a1, "0.4", "bisson.gary@gmail.com");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return printf("%s v%s\nreport bugs to %s\n", a1, "0.4", "bisson.gary@gmail.com");
}

original_code:
static void show_version(char *name)
{
 printf("%s v%s\nreport bugs to %s\n", name, "0.4",
        "bisson.gary@gmail.com");
 return;
}

variable_types:
name
char *


decompiled_code:
int __fastcall show_help(const char *a1)
{
return printf(
"Linux Accessory Development Kit\n"
"\n"
"usage: %s [OPTIONS]\n"
"OPTIONS:\n"
"\t-a, --aoa-max-version\n"
"\t\tAOA maximum version to be used. Default is no maximum version.\n"
"\t-d, --device\n"
"\t\tUSB device product and vendor IDs. Default is \"%s\".\n"
"\t-D, --description\n"
"\t\taccessory description. Default is \"%s\".\n"
"\t-m, --manufacturer\n"
"\t\tmanufacturer's name. Default is \"%s\".\n"
"\t-M, --model\n"
"\t\tmodel's name. Default is \"%s\".\n"
"\t-n, --vernumber\n"
"\t\taccessory version number. Default is \"%s\".\n"
"\t-N, --no_app\n"
"\t\toption that allows to connect without an Android App (AOA v2.0 only, for Audio and HID).\n"
"\t-s, --serial\n"
"\t\tserial numder. Default is \"%s\".\n"
"\t-u, --url\n"
"\t\taccessory url. Default is \"%s\".\n"
"\t-v, --version\n"
"\t\tShow program version and exit.\n"
"\t-V, --verbose\n"
"\t\tSets libusb verbose mode.\n"
"\t-h, --help\n"
"\t\tShow this help and exit.\n",
a1,
"18d1:4e42",
"Sample Program",
"Google, Inc.",
"AccessoryChat",
"1.0",
"0000000012345678",
"https://github.com/gibsson");
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
return printf(
"Linux Accessory Development Kit\n"
"\n"
"usage: %s [OPTIONS]\n"
"OPTIONS:\n"
"\t-a, --aoa-max-version\n"
"\t\tAOA maximum version to be used. Default is no maximum version.\n"
"\t-d, --device\n"
"\t\tUSB device product and vendor IDs. Default is \"%s\".\n"
"\t-D, --description\n"
"\t\taccessory description. Default is \"%s\".\n"
"\t-m, --manufacturer\n"
"\t\tmanufacturer's name. Default is \"%s\".\n"
"\t-M, --model\n"
"\t\tmodel's name. Default is \"%s\".\n"
"\t-n, --vernumber\n"
"\t\taccessory version number. Default is \"%s\".\n"
"\t-N, --no_app\n"
"\t\toption that allows to connect without an Android App (AOA v2.0 only, for Audio and HID).\n"
"\t-s, --serial\n"
"\t\tserial numder. Default is \"%s\".\n"
"\t-u, --url\n"
"\t\taccessory url. Default is \"%s\".\n"
"\t-v, --version\n"
"\t\tShow program version and exit.\n"
"\t-V, --verbose\n"
"\t\tSets libusb verbose mode.\n"
"\t-h, --help\n"
"\t\tShow this help and exit.\n",
a1,
"18d1:4e42",
"Sample Program",
"Google, Inc.",
"AccessoryChat",
"1.0",
"0000000012345678",
"https://github.com/gibsson");
}

original_code:
static void show_help(char *name)
{
 printf
     ("Linux Accessory Development Kit\n\nusage: %s [OPTIONS]\nOPTIONS:\n"
      "\t-a, --aoa-max-version\n\t\tAOA maximum version to be used. "
      "Default is no maximum version.\n"
      "\t-d, --device\n\t\tUSB device product and vendor IDs. "
      "Default is \"%s\".\n"
      "\t-D, --description\n\t\taccessory description. "
      "Default is \"%s\".\n"
      "\t-m, --manufacturer\n\t\tmanufacturer's name. "
      "Default is \"%s\".\n"
      "\t-M, --model\n\t\tmodel's name. "
      "Default is \"%s\".\n"
      "\t-n, --vernumber\n\t\taccessory version number. "
      "Default is \"%s\".\n"
      "\t-N, --no_app\n\t\toption that allows to connect without an "
      "Android App (AOA v2.0 only, for Audio and HID).\n"
      "\t-s, --serial\n\t\tserial numder. "
      "Default is \"%s\".\n"
      "\t-u, --url\n\t\taccessory url. "
      "Default is \"%s\".\n"
      "\t-v, --version\n\t\tShow program version and exit.\n"
      "\t-V, --verbose\n\t\tSets libusb verbose mode.\n"
      "\t-h, --help\n\t\tShow this help and exit.\n", name,
      acc_default.device, acc_default.description,
      acc_default.manufacturer, acc_default.model, acc_default.version,
      acc_default.serial, acc_default.url);
 return;
}


decompiled_code:
void *__fastcall receive_loop(void *a1)
{
char *v1; // rax
char *v2; // rdi
__int64 v3; // rax
char *v4; // rdx
int v5; // ebx
int v6; // ecx
int v7; // eax
int v8; // eax
const char *v9; // rax
unsigned __int128 v11; // [rsp+0h] [rbp-1B8h] BYREF
__int64 v12[2]; // [rsp+10h] [rbp-1A8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-198h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-118h] BYREF
fd_set exceptfds; // [rsp+120h] [rbp-98h] BYREF

if ( stop_acc )
return 0LL;
while ( 1 )
{
v11 = 0uLL;
v12[0] = 0LL;
v12[1] = 0LL;
v8 = libusb_get_next_timeout(0LL, &v11);
if ( v8 < 0 )
break;
if ( v8 == 1 && v11 == 0 )
{
v8 = libusb_handle_events_timeout_completed(0LL, v12, 0LL);
if ( v8 )
break;
}
else
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
readfds.fds_bits[0] |= 1uLL;
v1 = (char *)libusb_get_pollfds(0LL);
v2 = v1;
if ( !v1 )
return 0LL;
v3 = *(_QWORD *)v1;
if ( v3 )
{
v4 = v2 + 8;
v5 = 1;
do
{
if ( *(_DWORD *)v3 >= v5 )
v5 = *(_DWORD *)v3 + 1;
if ( (*(_BYTE *)(v3 + 4) & 1) != 0 )
readfds.fds_bits[*(_DWORD *)v3 / 64] |= 1LL << (*(_DWORD *)v3 % 64);
if ( (*(_BYTE *)(v3 + 4) & 4) != 0 )
{
v6 = *(_DWORD *)v3;
v7 = *(_DWORD *)v3 + 63;
if ( v6 >= 0 )
v7 = v6;
writefds.fds_bits[v7 >> 6] |= 1LL << (v6 % 64);
}
v4 += 8;
v3 = *((_QWORD *)v4 - 1);
}
while ( v3 );
}
else
{
v5 = 1;
}
free(v2);
if ( v11 == 0 )
select(v5, &readfds, &writefds, &exceptfds, 0LL);
else
select(v5, &readfds, &writefds, &exceptfds, (struct timeval *)&v11);
if ( (unsigned int)libusb_handle_events_timeout_completed(0LL, v12, 0LL) )
return 0LL;
}
if ( stop_acc )
return 0LL;
}
v9 = (const char *)libusb_error_name((unsigned int)v8);
printf("USB error : %s\n", v9);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func6(void *a1)
{
char *v1; // rax
char *v2; // rdi
__int64 v3; // rax
char *v4; // rdx
int v5; // ebx
int v6; // ecx
int v7; // eax
int v8; // eax
const char *v9; // rax
unsigned __int128 v11; // [rsp+0h] [rbp-1B8h] BYREF
__int64 v12[2]; // [rsp+10h] [rbp-1A8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-198h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-118h] BYREF
fd_set exceptfds; // [rsp+120h] [rbp-98h] BYREF

if ( stop_acc )
return 0LL;
while ( 1 )
{
v11 = 0uLL;
v12[0] = 0LL;
v12[1] = 0LL;
v8 = libusb_get_next_timeout(0LL, &v11);
if ( v8 < 0 )
break;
if ( v8 == 1 && v11 == 0 )
{
v8 = libusb_handle_events_timeout_completed(0LL, v12, 0LL);
if ( v8 )
break;
}
else
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
readfds.fds_bits[0] |= 1uLL;
v1 = (char *)libusb_get_pollfds(0LL);
v2 = v1;
if ( !v1 )
return 0LL;
v3 = *(_QWORD *)v1;
if ( v3 )
{
v4 = v2 + 8;
v5 = 1;
do
{
if ( *(_DWORD *)v3 >= v5 )
v5 = *(_DWORD *)v3 + 1;
if ( (*(_BYTE *)(v3 + 4) & 1) != 0 )
readfds.fds_bits[*(_DWORD *)v3 / 64] |= 1LL << (*(_DWORD *)v3 % 64);
if ( (*(_BYTE *)(v3 + 4) & 4) != 0 )
{
v6 = *(_DWORD *)v3;
v7 = *(_DWORD *)v3 + 63;
if ( v6 >= 0 )
v7 = v6;
writefds.fds_bits[v7 >> 6] |= 1LL << (v6 % 64);
}
v4 += 8;
v3 = *((_QWORD *)v4 - 1);
}
while ( v3 );
}
else
{
v5 = 1;
}
free(v2);
if ( v11 == 0 )
select(v5, &readfds, &writefds, &exceptfds, 0LL);
else
select(v5, &readfds, &writefds, &exceptfds, (struct timeval *)&v11);
if ( (unsigned int)libusb_handle_events_timeout_completed(0LL, v12, 0LL) )
return 0LL;
}
if ( stop_acc )
return 0LL;
}
v9 = (const char *)libusb_error_name((unsigned int)v8);
printf("USB error : %s\n", v9);
return 0LL;
}

original_code:
static void *receive_loop(void *arg)
{
 while (!stop_acc) {
  int ret = 0;
  int i;
  int nfds = 1;
  struct timeval tv, zero_tv;
  const struct libusb_pollfd **poll_list;
  fd_set rfds, wfds, efds;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  zero_tv.tv_sec = 0;
  zero_tv.tv_usec = 0;
  ret = libusb_get_next_timeout(((void *)0), &tv);
  if (ret < 0) {
   if (ret)
    printf("USB error : %s\n",
           libusb_error_name(ret));
   break;
  }
  if (ret == 1 && tv.tv_sec == 0 && tv.tv_usec == 0) {
   ret = libusb_handle_events_timeout_completed(((void *)0),
             &zero_tv,
             ((void *)0));
   if (ret) {
    if (ret)
     printf("USB error : %s\n",
            libusb_error_name(ret));
    break;
   }
   continue;
  }
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&efds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  poll_list = libusb_get_pollfds(((void *)0));
  if (poll_list) {
   for (i = 0; poll_list[i] != ((void *)0); i++) {
    const struct libusb_pollfd *cur_poll;
    cur_poll = poll_list[i];
    nfds = cur_poll->fd >= nfds
     ? cur_poll->fd + 1 : nfds;
    if (cur_poll->events & 0x001)
     ((void) (((&rfds)->__fds_bits)[((cur_poll->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cur_poll->fd) % (8 * (int) sizeof (__fd_mask)))))));
    if (cur_poll->events & 0x004)
     ((void) (((&wfds)->__fds_bits)[((cur_poll->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cur_poll->fd) % (8 * (int) sizeof (__fd_mask)))))));
   }
   free(poll_list);
  } else
   break;
  if (tv.tv_sec == 0 && tv.tv_usec == 0)
   ret = select(nfds, &rfds, &wfds, &efds, ((void *)0));
  else
   ret = select(nfds, &rfds, &wfds, &efds, &tv);
  if (libusb_handle_events_timeout_completed
      (((void *)0), &zero_tv, ((void *)0)))
   break;
 }
 return ((void *)0);
}

canonical_original_code:
static void *receive_loop(void *arg)
{
 while (!stop_acc) {
  int ret = 0;
  int i;
  int nfds = 1;
  struct timeval tv; struct timeval zero_tv;
  struct libusb_pollfd **poll_list;
  struct fd_set rfds; struct fd_set wfds; struct fd_set efds;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  zero_tv.tv_sec = 0;
  zero_tv.tv_usec = 0;
  ret = libusb_get_next_timeout(((void *)0), &tv);
  if (ret < 0) {
   if (ret)
    printf("USB error : %s\n",
           libusb_error_name(ret));
   break;
  }
  if (ret == 1 && tv.tv_sec == 0 && tv.tv_usec == 0) {
   ret = libusb_handle_events_timeout_completed(((void *)0),
             &zero_tv,
             ((void *)0));
   if (ret) {
    if (ret)
     printf("USB error : %s\n",
            libusb_error_name(ret));
    break;
   }
   continue;
  }
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&efds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  poll_list = libusb_get_pollfds(((void *)0));
  if (poll_list) {
   for (i = 0; poll_list[i] != ((void *)0); i++) {
    struct libusb_pollfd *cur_poll;
    cur_poll = poll_list[i];
    nfds = cur_poll->fd >= nfds
     ? cur_poll->fd + 1 : nfds;
    if (cur_poll->events & 0x001)
     ((void) (((&rfds)->__fds_bits)[((cur_poll->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cur_poll->fd) % (8 * (int) sizeof (__fd_mask)))))));
    if (cur_poll->events & 0x004)
     ((void) (((&wfds)->__fds_bits)[((cur_poll->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cur_poll->fd) % (8 * (int) sizeof (__fd_mask)))))));
   }
   free(poll_list);
  } else
   break;
  if (tv.tv_sec == 0 && tv.tv_usec == 0)
   ret = select(nfds, &rfds, &wfds, &efds, ((void *)0));
  else
   ret = select(nfds, &rfds, &wfds, &efds, &tv);
  if (libusb_handle_events_timeout_completed
      (((void *)0), &zero_tv, ((void *)0)))
   break;
 }
 return ((void *)0);
}

variable_types:
arg
void *

variable_types: ret int

variable_types: nfds int

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

variable_types: struct libusb_pollfd { int fd; short events; } **

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

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

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

return_type: void *


main

canonical_name: main

repo: gideonite/CLang

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
const char *v4; // r12
int v5; // edi
char *v6; // rax
char *v7; // rdi
bool v8; // cl
char *v9; // rsi
unsigned int v10; // ebx
unsigned int v11; // ebp
char v12; // dl
unsigned int v13; // er13

if ( argc != 2 )
{
puts("This implementation of wc accepts a single filename only");
return 1;
}
v4 = argv[1];
v5 = open(v4, 0, envp);
fd = v5;
if ( v5 == -1 )
{
perror("open");
exit(1);
}
if ( (unsigned int)fstat(v5, &sb) == -1 )
{
perror("fstat");
exit(1);
}
sysconf(30);
offset = 0LL;
pa_offset = 0LL;
length = sb.st_size;
v6 = (char *)mmap(0LL, sb.st_size, 1, 2, fd, 0LL);
v7 = v6;
addr = (__int64)v6;
if ( length )
{
v8 = *v6 != 32 && (unsigned __int8)(*v6 - 9) > 1u;
v9 = &v6[length];
v10 = 0;
v11 = 0;
while ( 1 )
{
v12 = *v6;
if ( v8 )
{
if ( (unsigned __int8)(v12 - 9) > 1u && v12 != 32 )
goto LABEL_11;
++v11;
}
v10 += v12 == 10;
LABEL_11:
if ( ++v6 == v9 )
{
v13 = length;
goto LABEL_17;
}
}
}
v10 = 0;
v11 = 0;
v13 = 0;
LABEL_17:
munmap(v7, length);
printf("%d %d %d %s\n", v10, v11, v13, v4);
result = close(fd);
close_success = result;
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
const char *v4; // r12
int v5; // edi
char *v6; // rax
char *v7; // rdi
bool v8; // cl
char *v9; // rsi
unsigned int v10; // ebx
unsigned int v11; // ebp
char v12; // dl
unsigned int v13; // er13

if ( argc != 2 )
{
puts("This implementation of wc accepts a single filename only");
return 1;
}
v4 = argv[1];
v5 = open(v4, 0, envp);
fd = v5;
if ( v5 == -1 )
{
perror("open");
exit(1);
}
if ( (unsigned int)func9(v5, &sb) == -1 )
{
perror("fstat");
exit(1);
}
sysconf(30);
offset = 0LL;
pa_offset = 0LL;
length = sb.st_size;
v6 = (char *)mmap(0LL, sb.st_size, 1, 2, fd, 0LL);
v7 = v6;
addr = (__int64)v6;
if ( length )
{
v8 = *v6 != 32 && (unsigned __int8)(*v6 - 9) > 1u;
v9 = &v6[length];
v10 = 0;
v11 = 0;
while ( 1 )
{
v12 = *v6;
if ( v8 )
{
if ( (unsigned __int8)(v12 - 9) > 1u && v12 != 32 )
goto LABEL_11;
++v11;
}
v10 += v12 == 10;
LABEL_11:
if ( ++v6 == v9 )
{
v13 = length;
goto LABEL_17;
}
}
}
v10 = 0;
v11 = 0;
v13 = 0;
LABEL_17:
munmap(v7, length);
printf("%d %d %d %s\n", v10, v11, v13, v4);
result = close(fd);
close_success = result;
return result;
}

original_code:
int main(int argc, char **argv)
{
    if (2 != argc) {
        printf("This implementation of wc accepts a single filename only\n");
        return 1;
    }
    char *filename = argv[1];
    fd = open(filename, 00);
    if (fd == -1)
        do { perror("open"); exit(1); } while (0);;
    if (fstat(fd, &sb) == -1)
        do { perror("fstat"); exit(1); } while (0);;
    int page_size = sysconf(_SC_PAGESIZE);
    int minus_pagesize = ~(page_size - 1);
    offset = 0;
    pa_offset = offset & minus_pagesize;
    length = sb.st_size - offset;
    addr = mmap(((void *)0), length + offset - pa_offset, 0x1, 0x02, fd,
            pa_offset);
    int char_count = 0;
    int space_count = 0;
    int newline_count = 0;
    int i;
    char curr;
    char prev;
    for (i=0; i < length; i++) {
        prev = *(addr);
        curr = *(addr+i);
        if ((' ' != prev && '\t' != prev && '\n' != prev)
                && ((' ' == curr) || ('\t' == curr) || ('\n' == curr)))
            space_count++;
        if ('\n' == curr)
            newline_count++;
        char_count++;
    }
    munmap(addr, length);
    printf("%d %d %d %s\n", newline_count, space_count, char_count, filename);
    close_success = close(fd);
    return close_success;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
filename
char *

variable_types: i int

variable_types: curr char

variable_types: prev char


defineitem

repo: gigaraptor/The-Third-Rogue

original_code:
void defineitem(item *item, unsigned int x, unsigned int y)
{
 item->x = x;
 item->y = y;
 int decider = rand() % 3 + 1;
 if(decider == 1)
 {
  item->display = '!';
  item->colour = rand() % 7 + 1;
  item->kind = 1;
  item->pairid = item->colour;
  strcpy(item->name, " Potion");
 }
 else if(decider == 2){
  item->display = '?';
  item->colour = rand() % 7 + 1;
  item->kind = 2;
  item->pairid = item->colour;
  strcpy(item->name, " Scroll");
 }
 else if(decider == 3){
  item->display = '/';
  item->colour = rand() % 7 + 1;
  item->kind = 2;
  item->pairid = item->colour;
  strcpy(item->name, " Weapon");
 }
}

canonical_original_code:
void defineitem(struct item *item, unsigned int x, unsigned int y)
{
 item->x = x;
 item->y = y;
 int decider = rand() % 3 + 1;
 if(decider == 1)
 {
  item->display = '!';
  item->colour = rand() % 7 + 1;
  item->kind = 1;
  item->pairid = item->colour;
  strcpy(item->name, " Potion");
 }
 else if(decider == 2){
  item->display = '?';
  item->colour = rand() % 7 + 1;
  item->kind = 2;
  item->pairid = item->colour;
  strcpy(item->name, " Scroll");
 }
 else if(decider == 3){
  item->display = '/';
  item->colour = rand() % 7 + 1;
  item->kind = 2;
  item->pairid = item->colour;
  strcpy(item->name, " Weapon");
 }
}

variable_types: struct item { unsigned int x; unsigned char kind; char name[100]; } *

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types: decider int


redefineitem

original_code:
void redefineitem(item *item, unsigned int x, unsigned int y)
{
 item->x = 0;
 item->y = 0;
 item->display = 0;
 item->colour = 0;
 item->kind = 0;
 item->pairid = 0;
 item->x = x;
 item->y = y;
 if(x)
 {
  item->display = '!';
  item->colour = rand() % 7 + 1;;
  item->kind = 1;
  item->pairid = item->colour;
  strcpy(item->name, "Potion");
 }
}

canonical_original_code:
void redefineitem(struct item *item, unsigned int x, unsigned int y)
{
 item->x = 0;
 item->y = 0;
 item->display = 0;
 item->colour = 0;
 item->kind = 0;
 item->pairid = 0;
 item->x = x;
 item->y = y;
 if(x)
 {
  item->display = '!';
  item->colour = rand() % 7 + 1;;
  item->kind = 1;
  item->pairid = item->colour;
  strcpy(item->name, "Potion");
 }
}

user_defined_types: struct item { unsigned int x; unsigned char kind; char name[100]; }


drawitemarray

decompiled_code:
int __fastcall drawitemarray(__int64 a1)
{
__int64 v1; // rbp
int result; // eax
int v3; // ebx
unsigned int v4; // ebx

v1 = a1;
do
{
v3 = *(unsigned __int8 *)(v1 + 9);
init_pair(*(unsigned __int8 *)(v1 + 9), *(unsigned __int8 *)(v1 + 9), 0);
v4 = v3 << 8;
wattr_on(stdscr, v4, 0LL);
if ( wmove(stdscr, *(_DWORD *)(v1 + 4), *(_DWORD *)v1) != -1 )
waddch(stdscr, *(unsigned __int8 *)(v1 + 10));
result = wattr_off(stdscr, v4, 0LL);
v1 += 112LL;
}
while ( a1 + 2240 != v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
__int64 v1; // rbp
int result; // eax
int v3; // ebx
unsigned int v4; // ebx

v1 = a1;
do
{
v3 = *(unsigned __int8 *)(v1 + 9);
init_pair(*(unsigned __int8 *)(v1 + 9), *(unsigned __int8 *)(v1 + 9), 0);
v4 = v3 << 8;
wattr_on(stdscr, v4, 0LL);
if ( wmove(stdscr, *(_DWORD *)(v1 + 4), *(_DWORD *)v1) != -1 )
waddch(stdscr, *(unsigned __int8 *)(v1 + 10));
result = wattr_off(stdscr, v4, 0LL);
v1 += 112LL;
}
while ( a1 + 2240 != v1 );
return result;
}

original_code:
void drawitemarray(item *itemarray)
{
 unsigned int counter = 0;
 item *temp = itemarray;
 while(counter < 20)
 {
  int s = itemarray->colour;
  init_pair(s, s, 0);
  wattr_on(stdscr, (attr_t)(((((chtype)(((s))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
  (wmove((stdscr),((itemarray->y)),((itemarray->x))) == (-1) ? (-1) : waddch((stdscr),((itemarray->display))));
  wattr_off(stdscr, (attr_t)(((((chtype)(((s))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
  counter++;
  itemarray++;
 }
 itemarray = temp;
}

canonical_original_code:
void drawitemarray(struct item *itemarray)
{
 unsigned int counter = 0;
 struct item *temp = itemarray;
 while(counter < 20)
 {
  int s = itemarray->colour;
  init_pair(s, s, 0);
  wattr_on(stdscr, (attr_t)(((((chtype)(((s))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
  (wmove((stdscr),((itemarray->y)),((itemarray->x))) == (-1) ? (-1) : waddch((stdscr),((itemarray->display))));
  wattr_off(stdscr, (attr_t)(((((chtype)(((s))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
  counter++;
  itemarray++;
 }
 itemarray = temp;
}

variable_types:
counter
unsigned int

variable_types: s int


pickupitem

original_code:
void pickupitem(creature *player, item *item){
 unsigned char x = 0;
 unsigned char indexer = 0;
 while(x != 1){
 if(player->inventory[indexer].x == 0 && player->inventory[indexer].y == 0 && player->inventory[indexer].display == 0){
  player->inventory[indexer].x = player->x;
  player->inventory[indexer].y = player->y;
  player->inventory[indexer].display = item->display;
  player->inventory[indexer].colour = item->colour;
  player->inventory[indexer].kind = item->kind;
  player->inventory[indexer].pairid = item->colour;
  strcpy(player->inventory[indexer].name, item->name);
  item->x = 1000;
  item->y = 1000;
 x = 1;
 break;
 }
  if(indexer >= 24 || x == 0){
   item->x = player->x;
   item->y = player->y;
  }
 indexer++;
 }
}

canonical_original_code:
void pickupitem(struct creature *player, struct item *item){
 unsigned char x = 0;
 unsigned char indexer = 0;
 while(x != 1){
 if(player->inventory[indexer].x == 0 && player->inventory[indexer].y == 0 && player->inventory[indexer].display == 0){
  player->inventory[indexer].x = player->x;
  player->inventory[indexer].y = player->y;
  player->inventory[indexer].display = item->display;
  player->inventory[indexer].colour = item->colour;
  player->inventory[indexer].kind = item->kind;
  player->inventory[indexer].pairid = item->colour;
  strcpy(player->inventory[indexer].name, item->name);
  item->x = 1000;
  item->y = 1000;
 x = 1;
 break;
 }
  if(indexer >= 24 || x == 0){
   item->x = player->x;
   item->y = player->y;
  }
 indexer++;
 }
}

variable_types: struct creature { unsigned int x; unsigned char colour; unsigned char kind; struct item { unsigned int x; unsigned char kind; char name[100]; } *inventory; } *

variable_types:
item
struct item *

variable_types:
x
unsigned char

variable_types:
indexer
unsigned char

user_defined_types: struct creature { unsigned int x; unsigned char colour; unsigned char kind; struct item *inventory; }


repo: gilbertasm/Klajoklis

original_code:
void dfs_visit (Graph *graph, int u) {
    Elem *adj_list, *head;
    int v;
    graph->info[u]->color = 2;
    adj_list = graph->q[u]->head;
    head = adj_list;
    while (head != ((void *)0)) {
        v = head->vertex;
        if (graph->info[v]->color == 1) {
            dfs_visit(graph, v);
        }
        head = head->next;
    }
    insert_first(graph->topological_order, u);
}

canonical_original_code:
void dfs_visit(struct graph_ *graph, int u) {
    struct elem_ *adj_list; struct elem_ *head;
    int v;
    graph->info[u]->color = 2;
    adj_list = graph->q[u]->head;
    head = adj_list;
    while (head != ((void *)0)) {
        v = head->vertex;
        if (graph->info[v]->color == 1) {
            dfs_visit(graph, v);
        }
        head = head->next;
    }
    insert_first(graph->topological_order, u);
}

variable_types: struct graph_ { struct queue_ { struct elem_ { struct elem_ *next; int vertex; } *head; struct elem_ *tail; } **q; int **matrix; int size; int *planets; int global_time; struct Attribute_info_ { int color; int time; int estimate; int parent; } **info; struct queue_ *topological_order; } *

variable_types: u int

variable_types: v int

user_defined_types: struct graph_ { struct queue_ **q; int **matrix; int size; int *planets; int global_time; struct Attribute_info_ **info; struct queue_ *topological_order; }

user_defined_types: struct queue_ { struct elem_ *head; struct elem_ *tail; }


original_code:
void initialise_single_source (Graph *graph, int s) {
    int i;
    for (i = 0; i < graph->size; i++) {
        graph->info[i]->parent = -1;
        graph->info[i]->estimate = 0x7fffffff;
    }
    graph->info[s]->estimate = 0;
}

canonical_original_code:
void initialise_single_source(struct graph_ *graph, int s) {
    int i;
    for (i = 0; i < graph->size; i++) {
        graph->info[i]->parent = -1;
        graph->info[i]->estimate = 0x7fffffff;
    }
    graph->info[s]->estimate = 0;
}

user_defined_types: struct Attribute_info_ { int color; int time; int estimate; int parent; }

user_defined_types: struct elem_ { struct elem_ *next; int vertex; }


relax

decompiled_code:
__int64 __fastcall relax(_QWORD *a1, int a2, int a3)
{
__int64 v3; // rax
__int64 v4; // r8
__int64 result; // rax

v3 = a1[5];
v4 = *(_QWORD *)(v3 + 8LL * a3);
result = (unsigned int)(*(_DWORD *)(a1[3] + 4LL * a3)
+ *(_DWORD *)(*(_QWORD *)(a1[1] + 8LL * a2) + 4LL * a3)
+ *(_DWORD *)(*(_QWORD *)(v3 + 8LL * a2) + 8LL));
if ( *(_DWORD *)(v4 + 8) > (int)result )
{
*(_DWORD *)(v4 + 8) = result;
result = *(_QWORD *)(a1[5] + 8LL * a3);
*(_DWORD *)(result + 12) = a2;
}
return result;
}

original_code:
void relax (Graph *graph, int u, int v ) {
    int estimate_u, estimate_v, estimate;
    estimate_u = graph->info[u]->estimate;
    estimate_v = graph->info[v]->estimate;
    estimate = estimate_u + graph->matrix[u][v] + graph->planets[v];
    if (estimate_v > estimate) {
        graph->info[v]->estimate = estimate;
        graph->info[v]->parent = u;
    }
}

canonical_original_code:
void relax(struct graph_ *graph, int u, int v) {
    int estimate_u; int estimate_v; int estimate;
    estimate_u = graph->info[u]->estimate;
    estimate_v = graph->info[v]->estimate;
    estimate = estimate_u + graph->matrix[u][v] + graph->planets[v];
    if (estimate_v > estimate) {
        graph->info[v]->estimate = estimate;
        graph->info[v]->parent = u;
    }
}

variable_types: estimate int


decompiled_code:
int __fastcall print_path(_QWORD *a1, __int64 a2, unsigned int a3)
{
int v4; // ecx
int result; // eax
__int64 v6; // rdx

if ( a3 != (_DWORD)a2 )
print_path(a1, a2, *(unsigned int *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 12LL));
if ( !a3 )
return printf(aPrad, 0LL);
v4 = *(_DWORD *)(a1[3] + 4LL * (int)a3);
if ( v4 )
{
v6 = *(_QWORD *)(a1[5] + 8LL * (int)a3);
result = printf(
aI,
a3,
abs32(*(_DWORD *)(*(_QWORD *)(a1[1] + 8LL * *(int *)(v6 + 12)) + 4LL * (int)a3)),
abs32(v4),
abs32(*(_DWORD *)(v6 + 8)));
}
else
{
printf(
format,
a3,
abs32(*(_DWORD *)(*(_QWORD *)(a1[1] + 8LL * *(int *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 12LL)) + 4LL * (int)a3)));
result = printf(aPlanetaNedraug, abs32(*(_DWORD *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 8LL)));
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(_QWORD *a1, __int64 a2, unsigned int a3)
{
int v4; // ecx
int result; // eax
__int64 v6; // rdx

if ( a3 != (_DWORD)a2 )
func10(a1, a2, *(unsigned int *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 12LL));
if ( !a3 )
return printf(aPrad, 0LL);
v4 = *(_DWORD *)(a1[3] + 4LL * (int)a3);
if ( v4 )
{
v6 = *(_QWORD *)(a1[5] + 8LL * (int)a3);
result = printf(
aI,
a3,
abs32(*(_DWORD *)(*(_QWORD *)(a1[1] + 8LL * *(int *)(v6 + 12)) + 4LL * (int)a3)),
abs32(v4),
abs32(*(_DWORD *)(v6 + 8)));
}
else
{
printf(
format,
a3,
abs32(*(_DWORD *)(*(_QWORD *)(a1[1] + 8LL * *(int *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 12LL)) + 4LL * (int)a3)));
result = printf(aPlanetaNedraug, abs32(*(_DWORD *)(*(_QWORD *)(a1[5] + 8LL * (int)a3) + 8LL)));
}
return result;
}

original_code:
void print_path (Graph *g, int s, int v) {
    if (v == s) {
    }
    else {
        print_path(g, s, g->info[v]->parent);
    }
    if (v == 0) {
        printf("Pradėjom kelionė iš planetos %d \n", v);
    } else {
        if (g->planets[v] == 0) {
            printf ("Praplaukiame planetą %d (joje neišsilaipindami!), atkarpos gerumas %d, ",
                    v, abs(g->matrix[g->info[v]->parent][v]));
            printf("planeta nedraugiška, bendras naudingumas %d\n", abs(g->info[v]->estimate));
        } else {
               printf ("Išsilaipinome planetoje %d, atkarpos gerumas %d, planetos gerumas %d, bendras naudingumas %d\n",
                       v, abs(g->matrix[g->info[v]->parent][v]), abs(g->planets[v]), abs(g->info[v]->estimate));
        }
    }
}

canonical_original_code:
void print_path(struct graph_ *g, int s, int v) {
    if (v == s) {
    }
    else {
        print_path(g, s, g->info[v]->parent);
    }
    if (v == 0) {
        printf("Pradėjom kelionė iš planetos %d \n", v);
    } else {
        if (g->planets[v] == 0) {
            printf ("Praplaukiame planetą %d (joje neišsilaipindami!), atkarpos gerumas %d, ",
                    v, abs(g->matrix[g->info[v]->parent][v]));
            printf("planeta nedraugiška, bendras naudingumas %d\n", abs(g->info[v]->estimate));
        } else {
               printf ("Išsilaipinome planetoje %d, atkarpos gerumas %d, planetos gerumas %d, bendras naudingumas %d\n",
                       v, abs(g->matrix[g->info[v]->parent][v]), abs(g->planets[v]), abs(g->info[v]->estimate));
        }
    }
}


dfs

original_code:
int dfs (Graph *graph) {
    int i, size;
    Attribute_info **info;
    info = graph->info;
    size = graph->size;
    for (i = 0; i < size; i++) {
        info[i]->color = 1;
    }
    for (i = 0; i < size; i++) {
        if (info[i]->color == 1)
            dfs_visit(graph, 0);
    }
    return 0;
}

canonical_original_code:
int dfs(struct graph_ *graph) {
    int i; int size;
    struct Attribute_info_ **info;
    info = graph->info;
    size = graph->size;
    for (i = 0; i < size; i++) {
        info[i]->color = 1;
    }
    for (i = 0; i < size; i++) {
        if (info[i]->color == 1)
            dfs_visit(graph, 0);
    }
    return 0;
}

variable_types: i int

variable_types: size int

variable_types: struct Attribute_info_ { int color; int time; int estimate; int parent; } **


decompiled_code:
_QWORD *initialise_graph()
{
_QWORD *result; // rax

result = malloc(0x38uLL);
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
result[3] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
return result;
}

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

result = malloc(0x38uLL);
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
result[3] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
return result;
}

original_code:
Graph *
initialise_graph (Graph *graph) {
    graph = malloc (sizeof(Graph));
    graph->q = ((void *)0);
    graph->matrix = ((void *)0);
    graph->size = 0;
    graph->planets = ((void *)0);
    graph->info = ((void *)0);
    graph->topological_order = ((void *)0);
    graph->info = ((void *)0);
    return graph;
}

canonical_original_code:
struct graph_ *initialise_graph(struct graph_ *graph) {
    graph = malloc (sizeof(Graph));
    graph->q = ((void *)0);
    graph->matrix = ((void *)0);
    graph->size = 0;
    graph->planets = ((void *)0);
    graph->info = ((void *)0);
    graph->topological_order = ((void *)0);
    graph->info = ((void *)0);
    return graph;
}

variable_types:
graph
struct graph_ *

return_type: struct graph_ { struct queue_ { struct elem_ { struct elem_ *next; int vertex; } *head; struct elem_ *tail; } **q; int **matrix; int size; int *planets; int global_time; struct Attribute_info_ { int color; int time; int estimate; int parent; } **info; struct queue_ *topological_order; } *


decompiled_code:
__int64 __fastcall print_queue_array(__int64 a1)
{
int v1; // er12
__int64 i; // rbx
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 16);
if ( v1 > 0 )
{
for ( i = 0LL; i != v1; result = print_queue(*(_QWORD *)(*(_QWORD *)a1 + 8 * i++)) )
printf("Vertex %d: ", (unsigned int)i);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
int v1; // er12
__int64 i; // rbx
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 16);
if ( v1 > 0 )
{
for ( i = 0LL; i != v1; result = func20(*(_QWORD *)(*(_QWORD *)a1 + 8 * i++)) )
printf("Vertex %d: ", (unsigned int)i);
}
return result;
}

original_code:
void print_queue_array (Graph *graph) {
    int i;
    int size = graph->size;
    for (i = 0; i < size; i++) {
        printf("Vertex %d: ", i);
        print_queue(graph->q[i]);
    }
}

canonical_original_code:
void print_queue_array(struct graph_ *graph) {
    int i;
    int size = graph->size;
    for (i = 0; i < size; i++) {
        printf("Vertex %d: ", i);
        print_queue(graph->q[i]);
    }
}


decompiled_code:
int *__fastcall make_adjacent_list(__int64 a1)
{
int v1; // er12
int *result; // rax
__int64 v3; // r13
__int64 v4; // r14
__int64 v5; // rbx
void **v6; // r12
__int64 v7; // r12
__int64 v8; // rbx

v1 = *(_DWORD *)(a1 + 16);
result = (int *)malloc(8LL * v1);
*(_QWORD *)a1 = result;
if ( !result )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
v3 = (unsigned int)v1;
v4 = 8LL * (unsigned int)v1;
v5 = 0LL;
if ( v1 > 0 )
{
do
{
v6 = (void **)(*(_QWORD *)a1 + v5);
*v6 = malloc(0x38uLL);
**(_QWORD **)(*(_QWORD *)a1 + v5) = 0LL;
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + v5) + 8LL) = 0LL;
v5 += 8LL;
}
while ( v5 != v4 );
v7 = 0LL;
do
{
v8 = 0LL;
do
{
result = *(int **)(*(_QWORD *)(a1 + 8) + v7);
if ( result[v8] < 0 )
result = (int *)insert_last(*(_QWORD *)(*(_QWORD *)a1 + v7), (unsigned int)v8);
++v8;
}
while ( v8 != v3 );
v7 += 8LL;
}
while ( v7 != v4 );
}
return result;
}

canonical_decompiled_code:
int *__fastcall func14(__int64 a1)
{
int v1; // er12
int *result; // rax
__int64 v3; // r13
__int64 v4; // r14
__int64 v5; // rbx
void **v6; // r12
__int64 v7; // r12
__int64 v8; // rbx

v1 = *(_DWORD *)(a1 + 16);
result = (int *)malloc(8LL * v1);
*(_QWORD *)a1 = result;
if ( !result )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
v3 = (unsigned int)v1;
v4 = 8LL * (unsigned int)v1;
v5 = 0LL;
if ( v1 > 0 )
{
do
{
v6 = (void **)(*(_QWORD *)a1 + v5);
*v6 = malloc(0x38uLL);
**(_QWORD **)(*(_QWORD *)a1 + v5) = 0LL;
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + v5) + 8LL) = 0LL;
v5 += 8LL;
}
while ( v5 != v4 );
v7 = 0LL;
do
{
v8 = 0LL;
do
{
result = *(int **)(*(_QWORD *)(a1 + 8) + v7);
if ( result[v8] < 0 )
result = (int *)func18(*(_QWORD *)(*(_QWORD *)a1 + v7), (unsigned int)v8);
++v8;
}
while ( v8 != v3 );
v7 += 8LL;
}
while ( v7 != v4 );
}
return result;
}

original_code:
void
make_adjacent_list (Graph *graph) {
    int i, j, value;
    int size = graph->size;
    graph->q = malloc( sizeof (Queue *) * size);
    if (graph->q == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit (-1);
    };
    for (i = 0; i < size; i++) {
        graph->q[i] = malloc (sizeof(Graph));
        graph->q[i]->head = ((void *)0);
        graph->q[i]->tail = ((void *)0);
    }
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            value = graph->matrix[i][j];
            if (value < 0) {
                insert_last (graph->q[i], j);
            }
        }
    }
}

canonical_original_code:
void make_adjacent_list(struct graph_ *graph) {
    int i; int j; int value;
    int size = graph->size;
    graph->q = malloc( sizeof (struct queue_ *) * size);
    if (graph->q == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit (-1);
    };
    for (i = 0; i < size; i++) {
        graph->q[i] = malloc (sizeof(Graph));
        graph->q[i]->head = ((void *)0);
        graph->q[i]->tail = ((void *)0);
    }
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            value = graph->matrix[i][j];
            if (value < 0) {
                insert_last (graph->q[i], j);
            }
        }
    }
}

variable_types: j int

variable_types: value int


decompiled_code:
void __fastcall destroy_matrix(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func15(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

original_code:
void destroy_matrix (int **matrix, int size) {
    int i;
    for (i = 0; i < size; i++)
        free(matrix[i]);
    free(matrix);
}

canonical_original_code:
void destroy_matrix(int **matrix, int size) {
    int i;
    for (i = 0; i < size; i++)
        free(matrix[i]);
    free(matrix);
}

variable_types:
matrix
int **


decompiled_code:
int __fastcall print_matrix(_QWORD *a1, int a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx
int result; // eax

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = 0LL;
do
{
printf("%d ", *(unsigned int *)(*v2 + v3));
v3 += 4LL;
}
while ( 4LL * (unsigned int)a2 != v3 );
result = putchar(10);
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
return result;
}

canonical_decompiled_code:
int __fastcall func16(_QWORD *a1, int a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx
int result; // eax

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = 0LL;
do
{
printf("%d ", *(unsigned int *)(*v2 + v3));
v3 += 4LL;
}
while ( 4LL * (unsigned int)a2 != v3 );
result = putchar(10);
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
return result;
}

original_code:
void print_matrix(int **matrix, int size) {
    int i,j;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

canonical_original_code:
void print_matrix(int **matrix, int size) {
    int i; int j;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}


decompiled_code:
_QWORD *initialise_queue()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
if ( !result )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*result = 0LL;
result[1] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func17()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
if ( !result )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*result = 0LL;
result[1] = 0LL;
return result;
}

original_code:
Queue*
initialise_queue (Queue *q) {
    q = malloc(sizeof(Queue));
    if (q == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit(-1);
    };
    q->head = ((void *)0);
    q->tail = ((void *)0);
    return q;
}

canonical_original_code:
struct queue_ *initialise_queue(struct queue_ *q) {
    q = malloc(sizeof(Queue));
    if (q == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit(-1);
    };
    q->head = ((void *)0);
    q->tail = ((void *)0);
    return q;
}

variable_types:
q
struct queue_ *

return_type: struct queue_ { struct elem_ { struct elem_ *next; int vertex; } *head; struct elem_ *tail; } *


decompiled_code:
__int64 __fastcall insert_last(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
_QWORD *v3; // rdx

v2 = malloc(0x10uLL);
if ( !v2 )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*(_QWORD *)v2 = 0LL;
v2[2] = a2;
if ( !*a1 )
*a1 = v2;
v3 = (_QWORD *)a1[1];
if ( v3 )
*v3 = v2;
a1[1] = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
_QWORD *v3; // rdx

v2 = malloc(0x10uLL);
if ( !v2 )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*(_QWORD *)v2 = 0LL;
v2[2] = a2;
if ( !*a1 )
*a1 = v2;
v3 = (_QWORD *)a1[1];
if ( v3 )
*v3 = v2;
a1[1] = v2;
return 0LL;
}

original_code:
int
insert_last(Queue *q, int vertex) {
    Elem *elem;
    elem = malloc(sizeof(Elem));
    if (elem == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit(-1);
    };
    elem->next = ((void *)0);
    elem->vertex = vertex;
    if (q->head == ((void *)0)) {
        q->head = elem;
    }
    if (q->tail != ((void *)0)) {
        q->tail->next = elem;
    }
    q->tail = elem;
    return 0;
}

canonical_original_code:
int insert_last(struct queue_ *q, int vertex) {
    struct elem_ *elem;
    elem = malloc(sizeof(Elem));
    if (elem == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit(-1);
    };
    elem->next = ((void *)0);
    elem->vertex = vertex;
    if (q->head == ((void *)0)) {
        q->head = elem;
    }
    if (q->tail != ((void *)0)) {
        q->tail->next = elem;
    }
    q->tail = elem;
    return 0;
}

variable_types: struct queue_ { struct elem_ { struct elem_ *next; int vertex; } *head; struct elem_ *tail; } *

variable_types: vertex int


decompiled_code:
__int64 __fastcall insert_first(_QWORD *a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(0x10uLL);
if ( !v2 )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*(_QWORD *)v2 = *a1;
v2[2] = a2;
*a1 = v2;
if ( !a1[1] )
a1[1] = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func19(_QWORD *a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(0x10uLL);
if ( !v2 )
{
fwrite("Malloc error. Quitting\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
*(_QWORD *)v2 = *a1;
v2[2] = a2;
*a1 = v2;
if ( !a1[1] )
a1[1] = v2;
return 0LL;
}

original_code:
int insert_first(Queue *q, int vertex) {
    Elem *elem;
    elem = malloc(sizeof(Elem));
    if (elem == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit (-1);
    };
    elem->next = q->head;
    elem->vertex = vertex;
    q->head = elem;
    if (q->tail == ((void *)0)) {
        q->tail = elem;
    }
    return 0;
}

canonical_original_code:
int insert_first(struct queue_ *q, int vertex) {
    struct elem_ *elem;
    elem = malloc(sizeof(Elem));
    if (elem == ((void *)0)) {
        fprintf(stderr, "Malloc error. Quitting\n");
        exit (-1);
    };
    elem->next = q->head;
    elem->vertex = vertex;
    q->head = elem;
    if (q->tail == ((void *)0)) {
        q->tail = elem;
    }
    return 0;
}

variable_types: struct elem_ { struct elem_ *next; int vertex; } *


decompiled_code:
int __fastcall print_queue(__int64 ***a1)
{
__int64 **i; // rbx

for ( i = *a1; i; i = (__int64 **)*i )
printf("%d ", *((unsigned int *)i + 2));
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func20(__int64 ***a1)
{
__int64 **i; // rbx

for ( i = *a1; i; i = (__int64 **)*i )
printf("%d ", *((unsigned int *)i + 2));
return putchar(10);
}

original_code:
void print_queue (Queue *q) {
    Elem *head = q->head;
    while (head != ((void *)0)) {
        printf("%d ", head->vertex);
        head = head->next;
    }
    printf("\n");
}

canonical_original_code:
void print_queue(struct queue_ *q) {
    struct elem_ *head = q->head;
    while (head != ((void *)0)) {
        printf("%d ", head->vertex);
        head = head->next;
    }
    printf("\n");
}


decompiled_code:
void __fastcall free_queue(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free(v3);
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func21(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free(v3);
}
while ( v2 );
}
free(ptr);
}

original_code:
void free_queue(Queue *q) {
    Elem *next_elem;
    Elem *head = q->head;
    while (head != ((void *)0)) {
        next_elem = head->next;
        free(head);
        head = next_elem;
    }
    free(q);
}

canonical_original_code:
void free_queue(struct queue_ *q) {
    struct elem_ *next_elem;
    struct elem_ *head = q->head;
    while (head != ((void *)0)) {
        next_elem = head->next;
        free(head);
        head = next_elem;
    }
    free(q);
}


repo: gilbertasm/blokai

decompiled_code:
void __fastcall prideti_sarasa(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 16) = a2;
}

original_code:
void prideti_sarasa (Virsunes *virsune, List *list) {
    virsune->list = list;
}

canonical_original_code:
void prideti_sarasa(struct virsunes_ *virsune, struct list_ *list) {
    virsune->list = list;
}

variable_types: struct virsunes_ { struct virsunes_ *next_virs; int virsune; struct list_ { struct list_ *next; int virsune; } *list; } *

variable_types:
list
struct list_ *

user_defined_types: struct virsunes_ { struct virsunes_ *next_virs; int virsune; struct list_ *list; }


decompiled_code:
__int64 __fastcall gretimos_virsunes(__int64 *a1, int a2)
{
if ( !a1 )
return 0LL;
while ( *((_DWORD *)a1 + 2) != a2 )
{
a1 = (__int64 *)*a1;
if ( !a1 )
return 0LL;
}
return a1[2];
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 *a1, int a2)
{
if ( !a1 )
return 0LL;
while ( *((_DWORD *)a1 + 2) != a2 )
{
a1 = (__int64 *)*a1;
if ( !a1 )
return 0LL;
}
return a1[2];
}

original_code:
List*
gretimos_virsunes(Virsunes *head, int virsune) {
    List *sarasas = ((void *)0);
    while (head != ((void *)0)) {
        if (head->virsune == virsune) {
            sarasas = head->list;
            break;
 }
        head = head->next_virs;
    }
    return sarasas;
}

canonical_original_code:
struct list_ *gretimos_virsunes(struct virsunes_ *head, int virsune) {
    struct list_ *sarasas = ((void *)0);
    while (head != ((void *)0)) {
        if (head->virsune == virsune) {
            sarasas = head->list;
            break;
 }
        head = head->next_virs;
    }
    return sarasas;
}

variable_types: struct virsunes_ { struct virsunes_ *next_virs; int virsune; struct list_ *list; } *

variable_types: virsune int

variable_types: struct list_ { struct list_ *next; int virsune; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // rbx
void *v4; // r12
_DWORD *v5; // rbp
void *v6; // rax
int v7; // edi

n = nuskaityti_faila((_QWORD **)&head_virs);
print_adj((__int64 **)head_virs);
v3 = n + 1;
v4 = calloc(v3, 4uLL);
DFNum = (__int64)v4;
v5 = calloc(v3, 4uLL);
LowPt = (__int64)v5;
v6 = calloc(v3, 4uLL);
Parent = (__int64)v6;
if ( v4 == 0LL || v5 == 0LL || !v6 )
{
fwrite("Calloc klaida\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
v7 = *(_DWORD *)(head_virs + 8);
v5[v7] = 1;
DFSearch(v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // rbx
void *v4; // r12
_DWORD *v5; // rbp
void *v6; // rax
int v7; // edi

n = func11((_QWORD **)&head_virs);
func9((__int64 **)head_virs);
v3 = n + 1;
v4 = calloc(v3, 4uLL);
DFNum = (__int64)v4;
v5 = calloc(v3, 4uLL);
LowPt = (__int64)v5;
v6 = calloc(v3, 4uLL);
Parent = (__int64)v6;
if ( v4 == 0LL || v5 == 0LL || !v6 )
{
fwrite("Calloc klaida\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
v7 = *(_DWORD *)(head_virs + 8);
v5[v7] = 1;
DFSearch(v7);
return 0;
}

original_code:
int
main (void)
{
    int u;
    n = nuskaityti_faila(&head_virs);
    print_adj(head_virs);
    DFNum = calloc(n+1, sizeof(int));
    LowPt = calloc(n+1, sizeof(int));
    Parent = calloc(n+1 , sizeof(int));
    if ( (DFNum == ((void *)0)) || (LowPt == ((void *)0)) || (Parent == ((void *)0)) ) {
        fprintf(stderr, "Calloc klaida\n");
        exit (1);
    }
    u = head_virs->virsune;
    LowPt[u] = 1;
    DFSearch(u);
    return 0;
}

canonical_original_code:
int main()
{
    int u;
    n = nuskaityti_faila(&head_virs);
    print_adj(head_virs);
    DFNum = calloc(n+1, sizeof(int));
    LowPt = calloc(n+1, sizeof(int));
    Parent = calloc(n+1 , sizeof(int));
    if ( (DFNum == ((void *)0)) || (LowPt == ((void *)0)) || (Parent == ((void *)0)) ) {
        fprintf(stderr, "Calloc klaida\n");
        exit (1);
    }
    u = head_virs->virsune;
    LowPt[u] = 1;
    DFSearch(u);
    return 0;
}

variable_types: u int


pop

decompiled_code:
__int64 pop()
{
__int64 v0; // rdx

v0 = head;
if ( head )
{
v0 = *(_QWORD *)head;
head = *(_QWORD *)(head + 8);
}
return v0;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rdx

v0 = head;
if ( head )
{
v0 = *(_QWORD *)head;
head = *(_QWORD *)(head + 8);
}
return v0;
}

original_code:
Briauna*
pop() {
    if (head != ((void *)0)) {
        Briauna* briauna = head->briauna;
        head = head->next;
 return briauna;
    }
    return (Briauna*) ((void *)0);
}

canonical_original_code:
struct briauna_ *pop() {
    if (head != ((void *)0)) {
        struct briauna_ *briauna = head->briauna;
        head = head->next;
 return briauna;
    }
    return (struct briauna_ *) ((void *)0);
}

variable_types: struct briauna_ { int pradzia; int pabaiga; } *


decompiled_code:
int __fastcall print_briauna(unsigned int *a1)
{
return printf("%d -> %d\n", *a1, a1[1]);
}

canonical_decompiled_code:
int __fastcall func14(unsigned int *a1)
{
return printf("%d -> %d\n", *a1, a1[1]);
}

original_code:
void print_briauna (Briauna *briauna) {
    printf("%d -> %d\n", briauna->pradzia, briauna->pabaiga);
}

canonical_original_code:
void print_briauna(struct briauna_ *briauna) {
    printf("%d -> %d\n", briauna->pradzia, briauna->pabaiga);
}

user_defined_types: struct briauna_ { int pradzia; int pabaiga; }


decompiled_code:
_BOOL8 __fastcall briaunos_lygios(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

original_code:
int briaunos_lygios(Briauna *briauna1, Briauna *briauna2) {
    if ( (briauna1->pradzia == briauna2->pradzia) &&
         (briauna1->pabaiga == briauna2->pabaiga)) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
int briaunos_lygios(struct briauna_ *briauna1, struct briauna_ *briauna2) {
    if ( (briauna1->pradzia == briauna2->pradzia) &&
         (briauna1->pabaiga == briauna2->pabaiga)) {
        return 1;
    } else {
        return 0;
    }
}


unstack

decompiled_code:
int __fastcall unstack(int a1, int a2)
{
_DWORD *v2; // rbp
unsigned int *v3; // rbx

v2 = malloc(8uLL);
*v2 = a1;
v2[1] = a2;
puts("Naujas blokas");
puts("----------------");
do
{
v3 = (unsigned int *)pop();
print_briauna(v3);
}
while ( !briaunos_lygios(v2, v3) );
return puts("----------------");
}

canonical_decompiled_code:
int __fastcall func16(int a1, int a2)
{
_DWORD *v2; // rbp
unsigned int *v3; // rbx

v2 = malloc(8uLL);
*v2 = a1;
v2[1] = a2;
puts("Naujas blokas");
puts("----------------");
do
{
v3 = (unsigned int *)func13();
func14(v3);
}
while ( !func15(v2, v3) );
return puts("----------------");
}

original_code:
void unstack (int u, int v) {
    Briauna *briauna2, *briauna1 = malloc(sizeof(briauna1));
    briauna1->pradzia = u;
    briauna1->pabaiga = v;
    printf("Naujas blokas\n");
    printf("----------------\n");
    do {
        briauna2 = pop();
        print_briauna(briauna2);
    } while ( briaunos_lygios(briauna1, briauna2) == 0);
    printf("----------------\n");
}

canonical_original_code:
void unstack(int u, int v) {
    struct briauna_ *briauna2; struct briauna_ *briauna1 = malloc(sizeof(briauna1));
    briauna1->pradzia = u;
    briauna1->pabaiga = v;
    printf("Naujas blokas\n");
    printf("----------------\n");
    do {
        briauna2 = pop();
        print_briauna(briauna2);
    } while ( briaunos_lygios(briauna1, briauna2) == 0);
    printf("----------------\n");
}

variable_types: v int


timer

repo: gille/ajaxers

decompiled_code:
void __fastcall __noreturn timer(void *a1)
{
int v1; // eax
int v2; // ebx
__int64 v3; // rcx
int *v4; // rdi
int v5[8]; // [rsp+0h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-18h] BYREF

v1 = socket(2, 2, 0);
if ( v1 != -1 )
{
v2 = v1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0x7F000001u);
*(_WORD *)addr.sa_data = _bswap_16(9999);
if ( connect(v2, &addr, 0x10u) != -1 )
{
v3 = 6LL;
v4 = v5;
while ( v3 )
{
*v4++ = 0;
--v3;
}
v5[0] = 12333;
while ( 1 )
{
sleep(1u);
send(v2, v5, 0x18uLL, 0);
}
}
perror("connect");
exit(-1);
}
perror("socket");
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // eax
int v2; // ebx
__int64 v3; // rcx
int *v4; // rdi
int v5[8]; // [rsp+0h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-18h] BYREF

v1 = socket(2, 2, 0);
if ( v1 != -1 )
{
v2 = v1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = func0(0x7F000001u);
*(_WORD *)addr.sa_data = func1(9999);
if ( connect(v2, &addr, 0x10u) != -1 )
{
v3 = 6LL;
v4 = v5;
while ( v3 )
{
*v4++ = 0;
--v3;
}
v5[0] = 12333;
while ( 1 )
{
sleep(1u);
send(v2, v5, 0x18uLL, 0);
}
}
perror("connect");
exit(-1);
}
perror("socket");
exit(-1);
}

original_code:
void * timer(void *arg) {
 int sockfd;
 struct sockaddr_in saddr;
 struct msg msg;
 sockfd = socket(2, SOCK_DGRAM, 0);
 if(sockfd == -1) {
  perror("socket");
  exit(-1);
 }
 saddr.sin_family = 2;
 saddr.sin_addr.s_addr = htonl(((in_addr_t) 0x7f000001));
 saddr.sin_port = htons(9999);
 if(connect(sockfd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
  perror("connect");
  exit(-1);
 }
 memset(&msg, 0, sizeof(msg));
 msg.cmd = 12333;
 while(1) {
  sleep(1);
  send(sockfd, &msg, sizeof(msg), 0);
 }
 return ((void *)0);
}

canonical_original_code:
void *timer(void *arg) {
 int sockfd;
 struct sockaddr_in saddr;
 struct msg msg;
 sockfd = socket(2, SOCK_DGRAM, 0);
 if(sockfd == -1) {
  perror("socket");
  exit(-1);
 }
 saddr.sin_family = 2;
 saddr.sin_addr.s_addr = htonl(((unsigned int) 0x7f000001));
 saddr.sin_port = htons(9999);
 if(connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
  perror("connect");
  exit(-1);
 }
 memset(&msg, 0, sizeof(msg));
 msg.cmd = 12333;
 while(1) {
  sleep(1);
  send(sockfd, &msg, sizeof(msg), 0);
 }
 return ((void *)0);
}

variable_types:
arg
void *

variable_types: sockfd int

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

variable_types: struct msg { int cmd; unsigned int size; int more_to_follow; unsigned int id; int state; char data[1]; }

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

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


decompiled_code:
__int64 __fastcall lookup_pid(int a1)
{
_DWORD *v1; // rax
__int64 result; // rax

v1 = lookup(a1);
if ( v1 )
result = (unsigned int)v1[4];
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
_DWORD *v1; // rax
__int64 result; // rax

v1 = lookup(a1);
if ( v1 )
result = (unsigned int)v1[4];
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
pid_t lookup_pid(uint32_t id) {
 struct task *t = lookup(id);
 return t?t->pid:-1;
}

canonical_original_code:
int lookup_pid(unsigned int id) {
 struct task *t = lookup(id);
 return t?t->pid:-1;
}

variable_types:
id
unsigned int

variable_types: struct task { struct list_elem { struct list_elem *next; struct list_elem *prev; } n; int pid; unsigned int id; int pipe_fd[2]; long lastpoll; int state; } *

user_defined_types: struct task { struct list_elem n; int pid; unsigned int id; int pipe_fd[2]; long lastpoll; int state; }


decompiled_code:
ssize_t __fastcall recv_msg(int a1, void *a2, int a3, struct sockaddr *a4)
{
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

addr_len[0] = 16;
return recvfrom(a1, a2, a3, 0, a4, addr_len);
}

canonical_decompiled_code:
ssize_t __fastcall func5(int a1, void *a2, int a3, struct sockaddr *a4)
{
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

addr_len[0] = 16;
return recvfrom(a1, a2, a3, 0, a4, addr_len);
}

original_code:
int recv_msg(int sockfd, struct msg *msg, int size, struct sockaddr *raddr) {
 unsigned int siz = sizeof(*raddr);
 return recvfrom(sockfd, msg, size, 0, raddr, &siz);
}

variable_types: struct msg { int cmd; unsigned int size; int more_to_follow; unsigned int id; int state; char data[1]; } *

variable_types: size int

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

variable_types:
siz
unsigned int