reversepath

decompiled_code:
char *__fastcall reversepath(char *a1, const char *a2, int a3, char *a4)
{
char *v6; // r12
char *v7; // r15
size_t v8; // rax
char *v9; // r13
struct stat v11; // [rsp+10h] [rbp-1C8h] BYREF
char v12[312]; // [rsp+A0h] [rbp-138h] BYREF

v6 = strcpy(&a4[-a3 + 4095], a2);
while ( 1 )
{
a1 = getcomponent(a1, v12);
if ( !a1 )
break;
if ( *(_WORD *)v12 != 46 )
{
if ( *(_WORD *)v12 != 11822 || v12[2] )
{
*(v6 - 1) = 47;
v6 -= 3;
*(_WORD *)v6 = 11822;
xchdir(v12);
}
else
{
if ( stat(".", &v11) < 0 )
fail("cannot stat current directory");
v7 = ino2name(v11.st_ino);
v8 = strlen(v7);
v9 = &v6[~(__int64)(int)v8];
memcpy(v9, v7, v8 + 1);
*(v6 - 1) = 47;
v6 = v9;
free(v7);
xchdir("..");
}
}
}
return strcpy(a4, v6);
}

original_code:
void
reversepath(char *inpath, char *name, int len, char *outpath)
{
    char *cp, *cp2;
    char buf[256];
    struct stat sb;
    cp = strcpy(outpath + 4096 - (len + 1), name);
    cp2 = inpath;
    while ((cp2 = getcomponent(cp2, buf)) != 0) {
 if (strcmp(buf, ".") == 0)
     continue;
 if (strcmp(buf, "..") == 0) {
     if (stat(".", &sb) < 0)
  fail("cannot stat current directory");
     name = ino2name(sb.st_ino, "..");
     len = strlen(name);
     cp -= len + 1;
     strcpy(cp, name);
     cp[len] = '/';
     free(name);
     xchdir("..");
 } else {
     cp -= 3;
     strncpy(cp, "../", 3);
     xchdir(buf);
 }
    }
    strcpy(outpath, cp);
}

canonical_original_code:
void reversepath(char *inpath, char *name, int len, char *outpath)
{
    char *cp; char *cp2;
    char buf[256];
    struct stat sb;
    cp = strcpy(outpath + 4096 - (len + 1), name);
    cp2 = inpath;
    while ((cp2 = getcomponent(cp2, buf)) != 0) {
 if (strcmp(buf, ".") == 0)
     continue;
 if (strcmp(buf, "..") == 0) {
     if (stat(".", &sb) < 0)
  fail("cannot stat current directory");
     name = ino2name(sb.st_ino, "..");
     len = strlen(name);
     cp -= len + 1;
     strcpy(cp, name);
     cp[len] = '/';
     free(name);
     xchdir("..");
 } else {
     cp -= 3;
     strncpy(cp, "../", 3);
     xchdir(buf);
 }
    }
    strcpy(outpath, cp);
}

variable_types:
inpath
char *

variable_types:
name
char *

variable_types: len int

variable_types:
outpath
char *

variable_types:
buf
char [256]

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
int v5; // ebp
char **v6; // rbx
char *v7; // r13
int v8; // er12
char *v9; // r13
int v10; // er12
char *v11; // r15
int v12; // eax
char *v13; // rbp
int v14; // eax
int v15; // er14
char *v16; // r15
int v17; // er9
int v18; // eax
__int16 v20; // ax
int v21; // er12
char *v22; // r14
int v23; // eax
int v24; // er15
int v25; // eax
size_t v26; // rax
__int128 v27; // [rsp+8h] [rbp-21E0h]
char *v28; // [rsp+18h] [rbp-21D0h]
int v29; // [rsp+20h] [rbp-21C8h]
int v30; // [rsp+24h] [rbp-21C4h]
char **v31; // [rsp+28h] [rbp-21C0h]
char *v32; // [rsp+38h] [rbp-21B0h]
char *ptr; // [rsp+40h] [rbp-21A8h]
int v34; // [rsp+48h] [rbp-21A0h]
__mode_t mode; // [rsp+4Ch] [rbp-219Ch]
int fd; // [rsp+50h] [rbp-2198h]
int fda; // [rsp+50h] [rbp-2198h]
int v38; // [rsp+54h] [rbp-2194h]
int v39; // [rsp+58h] [rbp-2190h]
int v40; // [rsp+5Ch] [rbp-218Ch]
__uid_t owner; // [rsp+60h] [rbp-2188h]
__gid_t group; // [rsp+64h] [rbp-2184h]
char *endptr; // [rsp+78h] [rbp-2170h] BYREF
struct utimbuf file_times; // [rsp+80h] [rbp-2168h] BYREF
struct stat v45; // [rsp+90h] [rbp-2158h] BYREF
struct stat v46; // [rsp+120h] [rbp-20C8h] BYREF
char s1[8248]; // [rsp+1B0h] [rbp-2038h] BYREF

v3 = 0;
ptr = 0LL;
program = *argv;
v27 = 0uLL;
v32 = 0LL;
mode = 493;
v38 = 0;
v40 = 0;
v39 = 0;
v29 = 0;
v30 = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "C:DdlL:Rm:o:g:t");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'C':
ptr = optarg;
continue;
case 'D':
v3 = 1;
continue;
case 'L':
v32 = optarg;
v29 = 1;
v38 = strlen(optarg);
continue;
case 'R':
v39 = 1;
v29 = 1;
continue;
case 'd':
v30 = 1;
continue;
case 'g':
*(_QWORD *)&v27 = optarg;
continue;
case 'l':
v29 = 1;
continue;
case 'm':
mode = strtoul(optarg, &endptr, 8);
if ( !mode && endptr == optarg )
goto LABEL_8;
continue;
case 'o':
*((_QWORD *)&v27 + 1) = optarg;
continue;
case 't':
v40 = 1;
continue;
default:
goto LABEL_8;
}
}
break;
}
v5 = argc - optind;
v6 = (char **)&argv[optind];
if ( 2 - v3 > argc - optind )
LABEL_8:
usage();
v7 = v6[v5 - 1];
if ( (stat(v7, &v45) < 0 || (v45.st_mode & 0xF000) != 0x4000) && (int)mkdirs_constprop_0(v7) < 0 )
fail("cannot make directory %s", v7);
if ( !v3 )
{
if ( !ptr )
ptr = getcwd(0LL, 0x1000uLL);
xchdir(v7);
v28 = getcwd(0LL, 0x1000uLL);
xchdir(ptr);
owner = -1;
v8 = strlen(v28);
if ( *((_QWORD *)&v27 + 1) )
owner = touid(*((const char **)&v27 + 1));
group = -1;
if ( (_QWORD)v27 )
group = togid((const char *)v27);
if ( v5 != 1 )
{
v34 = v8 + 1;
v31 = &v6[v5 - 1];
while ( 1 )
{
v9 = *v6++;
v10 = strlen(v9);
v11 = xbasename(v9);
v12 = strlen(v11);
v13 = (char *)xmalloc(v34 + v12 + 1);
sprintf(v13, "%s/%s", v28, v11);
v14 = lstat(v13, &v46);
v15 = v14;
if ( v30 )
{
if ( v14 )
{
LABEL_32:
if ( mkdir(v13, mode) < 0 )
fail("cannot make directory %s", v13);
}
else if ( (v46.st_mode & 0xF000) != 0x4000 )
{
unlink(v13);
goto LABEL_32;
}
if ( v27 != 0 && chown(v13, owner, group) < 0 )
fail("cannot change owner of %s", v13);
goto LABEL_34;
}
if ( !v29 )
{
fd = open(v9, 0);
if ( fd < 0 || fstat(fd, &v45) < 0 )
fail("cannot access %s", v9);
if ( !v15 )
{
v20 = v46.st_mode;
if ( (v46.st_mode & 0xF000) == 0x8000 )
{
if ( access(v13, 2) < 0 )
{
v20 = v46.st_mode;
goto LABEL_65;
}
}
else
{
LABEL_65:
if ( (v20 & 0xF000) == 0x4000 )
rmdir(v13);
else
unlink(v13);
}
}
v21 = open(v13, 65, 438LL);
if ( v21 < 0 )
fail("cannot create %s", v13);
v22 = s1;
LABEL_69:
while ( 1 )
{
v23 = read(fd, v22, 0x2000uLL);
v24 = v23;
if ( v23 <= 0 )
break;
while ( 1 )
{
v25 = write(v21, v22, v24);
if ( v25 <= 0 )
break;
v24 -= v25;
if ( !v24 )
goto LABEL_69;
v22 += v25;
}
if ( v25 )
fail("cannot write to %s", v13);
}
if ( v23 )
fail("cannot read from %s", v9);
if ( ftruncate(v21, v45.st_size) < 0 )
fail("cannot truncate %s", v13);
if ( v40 )
{
file_times.actime = v45.st_atim.tv_sec;
file_times.modtime = v45.st_mtim.tv_sec;
if ( utime(v13, &file_times) < 0 )
fail("cannot set times of %s", v13);
}
if ( fchmod(v21, mode) < 0 )
fail("cannot change mode of %s", v13);
if ( v27 != 0 && fchown(v21, owner, group) < 0 )
fail("cannot change owner of %s", v13);
if ( close(v21) < 0 )
fail("cannot write to %s", v13);
close(fd);
goto LABEL_34;
}
v16 = 0LL;
v17 = v10;
if ( *v9 != 47 )
{
if ( v32 )
{
v16 = (char *)xmalloc(v38 + v10 + 2);
sprintf(v16, "%s/%s", v32, v9);
v17 = v38 + v10 + 1;
v9 = v16;
}
else if ( v39 )
{
v16 = (char *)xmalloc(0x1000uLL);
if ( *v28 == 47 )
{
v38 = relatepaths(v28, ptr, v16);
strcpy(&v16[v38], v9);
}
else
{
reversepath(v28, v9, v10, v16);
xchdir(ptr);
}
v9 = v16;
v17 = strlen(v16);
}
else
{
v9 = 0LL;
}
}
if ( v15 )
goto LABEL_44;
v18 = v46.st_mode & 0xF000;
if ( v18 != 40960 )
goto LABEL_42;
fda = v17;
v26 = readlink(v13, s1, 0x2000uLL);
if ( v26 != fda || strncmp(s1, v9, v26) )
break;
LABEL_45:
if ( v27 != 0 && lchown(v13, owner, group) < 0 )
fail("cannot change owner of %s", v13);
if ( v16 )
{
free(v16);
free(v13);
if ( v6 == v31 )
goto LABEL_48;
}
else
{
LABEL_34:
free(v13);
if ( v6 == v31 )
goto LABEL_48;
}
}
v18 = v46.st_mode & 0xF000;
LABEL_42:
if ( v18 == 0x4000 )
rmdir(v13);
else
unlink(v13);
LABEL_44:
if ( symlink(v9, v13) < 0 )
fail("cannot make symbolic link %s", v13);
goto LABEL_45;
}
LABEL_48:
free(ptr);
free(v28);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
int v5; // ebp
char **v6; // rbx
char *v7; // r13
int v8; // er12
char *v9; // r13
int v10; // er12
char *v11; // r15
int v12; // eax
char *v13; // rbp
int v14; // eax
int v15; // er14
char *v16; // r15
int v17; // er9
int v18; // eax
__int16 v20; // ax
int v21; // er12
char *v22; // r14
int v23; // eax
int v24; // er15
int v25; // eax
size_t v26; // rax
__int128 v27; // [rsp+8h] [rbp-21E0h]
char *v28; // [rsp+18h] [rbp-21D0h]
int v29; // [rsp+20h] [rbp-21C8h]
int v30; // [rsp+24h] [rbp-21C4h]
char **v31; // [rsp+28h] [rbp-21C0h]
char *v32; // [rsp+38h] [rbp-21B0h]
char *ptr; // [rsp+40h] [rbp-21A8h]
int v34; // [rsp+48h] [rbp-21A0h]
__mode_t mode; // [rsp+4Ch] [rbp-219Ch]
int fd; // [rsp+50h] [rbp-2198h]
int fda; // [rsp+50h] [rbp-2198h]
int v38; // [rsp+54h] [rbp-2194h]
int v39; // [rsp+58h] [rbp-2190h]
int v40; // [rsp+5Ch] [rbp-218Ch]
__uid_t owner; // [rsp+60h] [rbp-2188h]
__gid_t group; // [rsp+64h] [rbp-2184h]
char *endptr; // [rsp+78h] [rbp-2170h] BYREF
struct utimbuf file_times; // [rsp+80h] [rbp-2168h] BYREF
struct stat v45; // [rsp+90h] [rbp-2158h] BYREF
struct stat v46; // [rsp+120h] [rbp-20C8h] BYREF
char s1[8248]; // [rsp+1B0h] [rbp-2038h] BYREF

v3 = 0;
ptr = 0LL;
program = *argv;
v27 = 0uLL;
v32 = 0LL;
mode = 493;
v38 = 0;
v40 = 0;
v39 = 0;
v29 = 0;
v30 = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "C:DdlL:Rm:o:g:t");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'C':
ptr = optarg;
continue;
case 'D':
v3 = 1;
continue;
case 'L':
v32 = optarg;
v29 = 1;
v38 = strlen(optarg);
continue;
case 'R':
v39 = 1;
v29 = 1;
continue;
case 'd':
v30 = 1;
continue;
case 'g':
*(_QWORD *)&v27 = optarg;
continue;
case 'l':
v29 = 1;
continue;
case 'm':
mode = strtoul(optarg, &endptr, 8);
if ( !mode && endptr == optarg )
goto LABEL_8;
continue;
case 'o':
*((_QWORD *)&v27 + 1) = optarg;
continue;
case 't':
v40 = 1;
continue;
default:
goto LABEL_8;
}
}
break;
}
v5 = argc - optind;
v6 = (char **)&argv[optind];
if ( 2 - v3 > argc - optind )
LABEL_8:
func0();
v7 = v6[v5 - 1];
if ( (stat(v7, &v45) < 0 || (v45.st_mode & 0xF000) != 0x4000) && (int)func1(v7) < 0 )
func2("cannot make directory %s", v7);
if ( !v3 )
{
if ( !ptr )
ptr = getcwd(0LL, 0x1000uLL);
func10(v7);
v28 = getcwd(0LL, 0x1000uLL);
func10(ptr);
owner = -1;
v8 = strlen(v28);
if ( *((_QWORD *)&v27 + 1) )
owner = func3(*((const char **)&v27 + 1));
group = -1;
if ( (_QWORD)v27 )
group = func4((const char *)v27);
if ( v5 != 1 )
{
v34 = v8 + 1;
v31 = &v6[v5 - 1];
while ( 1 )
{
v9 = *v6++;
v10 = strlen(v9);
v11 = func9(v9);
v12 = strlen(v11);
v13 = (char *)func6(v34 + v12 + 1);
sprintf(v13, "%s/%s", v28, v11);
v14 = lstat(v13, &v46);
v15 = v14;
if ( v30 )
{
if ( v14 )
{
LABEL_32:
if ( mkdir(v13, mode) < 0 )
func2("cannot make directory %s", v13);
}
else if ( (v46.st_mode & 0xF000) != 0x4000 )
{
unlink(v13);
goto LABEL_32;
}
if ( v27 != 0 && chown(v13, owner, group) < 0 )
func2("cannot change owner of %s", v13);
goto LABEL_34;
}
if ( !v29 )
{
fd = open(v9, 0);
if ( fd < 0 || fstat(fd, &v45) < 0 )
func2("cannot access %s", v9);
if ( !v15 )
{
v20 = v46.st_mode;
if ( (v46.st_mode & 0xF000) == 0x8000 )
{
if ( access(v13, 2) < 0 )
{
v20 = v46.st_mode;
goto LABEL_65;
}
}
else
{
LABEL_65:
if ( (v20 & 0xF000) == 0x4000 )
rmdir(v13);
else
unlink(v13);
}
}
v21 = open(v13, 65, 438LL);
if ( v21 < 0 )
func2("cannot create %s", v13);
v22 = s1;
LABEL_69:
while ( 1 )
{
v23 = read(fd, v22, 0x2000uLL);
v24 = v23;
if ( v23 <= 0 )
break;
while ( 1 )
{
v25 = write(v21, v22, v24);
if ( v25 <= 0 )
break;
v24 -= v25;
if ( !v24 )
goto LABEL_69;
v22 += v25;
}
if ( v25 )
func2("cannot write to %s", v13);
}
if ( v23 )
func2("cannot read from %s", v9);
if ( ftruncate(v21, v45.st_size) < 0 )
func2("cannot truncate %s", v13);
if ( v40 )
{
file_times.actime = v45.st_atim.tv_sec;
file_times.modtime = v45.st_mtim.tv_sec;
if ( utime(v13, &file_times) < 0 )
func2("cannot set times of %s", v13);
}
if ( fchmod(v21, mode) < 0 )
func2("cannot change mode of %s", v13);
if ( v27 != 0 && fchown(v21, owner, group) < 0 )
func2("cannot change owner of %s", v13);
if ( close(v21) < 0 )
func2("cannot write to %s", v13);
close(fd);
goto LABEL_34;
}
v16 = 0LL;
v17 = v10;
if ( *v9 != 47 )
{
if ( v32 )
{
v16 = (char *)func6(v38 + v10 + 2);
sprintf(v16, "%s/%s", v32, v9);
v17 = v38 + v10 + 1;
v9 = v16;
}
else if ( v39 )
{
v16 = (char *)func6(0x1000uLL);
if ( *v28 == 47 )
{
v38 = func11(v28, ptr, v16);
strcpy(&v16[v38], v9);
}
else
{
func12(v28, v9, v10, v16);
func10(ptr);
}
v9 = v16;
v17 = strlen(v16);
}
else
{
v9 = 0LL;
}
}
if ( v15 )
goto LABEL_44;
v18 = v46.st_mode & 0xF000;
if ( v18 != 40960 )
goto LABEL_42;
fda = v17;
v26 = readlink(v13, s1, 0x2000uLL);
if ( v26 != fda || strncmp(s1, v9, v26) )
break;
LABEL_45:
if ( v27 != 0 && lchown(v13, owner, group) < 0 )
func2("cannot change owner of %s", v13);
if ( v16 )
{
free(v16);
free(v13);
if ( v6 == v31 )
goto LABEL_48;
}
else
{
LABEL_34:
free(v13);
if ( v6 == v31 )
goto LABEL_48;
}
}
v18 = v46.st_mode & 0xF000;
LABEL_42:
if ( v18 == 0x4000 )
rmdir(v13);
else
unlink(v13);
LABEL_44:
if ( symlink(v9, v13) < 0 )
func2("cannot make symbolic link %s", v13);
goto LABEL_45;
}
LABEL_48:
free(ptr);
free(v28);
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
    int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
    mode_t mode = 0755;
    char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[8192];
    uid_t uid;
    gid_t gid;
    struct stat sb, tosb;
    struct utimbuf utb;
    program = argv[0];
    cwd = linkname = linkprefix = owner = group = 0;
    onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
    while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != (-1)) {
 switch (opt) {
   case 'C':
     cwd = optarg;
     break;
   case 'D':
     onlydir = 1;
     break;
   case 'd':
     dodir = 1;
     break;
   case 'l':
     dolink = 1;
     break;
   case 'L':
     linkprefix = optarg;
     lplen = strlen(linkprefix);
     dolink = 1;
     break;
   case 'R':
     dolink = dorelsymlink = 1;
     break;
   case 'm':
     mode = strtoul(optarg, &cp, 8);
     if (mode == 0 && cp == optarg)
  usage();
     break;
   case 'o':
     owner = optarg;
     break;
   case 'g':
     group = optarg;
     break;
   case 't':
     dotimes = 1;
     break;
   default:
     usage();
 }
    }
    argc -= optind;
    argv += optind;
    if (argc < 2 - onlydir)
 usage();
    todir = argv[argc-1];
    if ((stat(todir, &sb) < 0 || !((((sb.st_mode)) & 0170000) == (0040000))) &&
 mkdirs(todir, 0777) < 0) {
 fail("cannot make directory %s", todir);
    }
    if (onlydir)
 return 0;
    if (!cwd) {
 cwd = getcwd(0, 4096);
    }
    xchdir(todir);
    todir = getcwd(0, 4096);
    tdlen = strlen(todir);
    xchdir(cwd);
    tdlen = strlen(todir);
    uid = owner ? touid(owner) : -1;
    gid = group ? togid(group) : -1;
    while (--argc > 0) {
 name = *argv++;
 len = strlen(name);
 base = xbasename(name);
 bnlen = strlen(base);
 toname = (char*)xmalloc(tdlen + 1 + bnlen + 1);
 sprintf(toname, "%s/%s", todir, base);
 exists = (lstat(toname, &tosb) == 0);
 if (dodir) {
     if (exists && !((((tosb.st_mode)) & 0170000) == (0040000))) {
  (void) unlink(toname);
  exists = 0;
     }
     if (!exists && mkdir(toname, mode) < 0)
  fail("cannot make directory %s", toname);
     if ((owner || group) && chown(toname, uid, gid) < 0)
  fail("cannot change owner of %s", toname);
 } else if (dolink) {
     if (*name == '/') {
  linkname = 0;
     } else {
  if (linkprefix) {
      len += lplen + 1;
      linkname = (char*)xmalloc(len + 1);
      sprintf(linkname, "%s/%s", linkprefix, name);
  } else if (dorelsymlink) {
      linkname = (char*)xmalloc(4096);
      if (*todir == '/') {
   lplen = relatepaths(todir, cwd, linkname);
   strcpy(linkname + lplen, name);
      } else {
   reversepath(todir, name, len, linkname);
   xchdir(cwd);
      }
      len = strlen(linkname);
  }
  name = linkname;
     }
     if (exists &&
  (!((((tosb.st_mode)) & 0170000) == (0120000)) ||
   readlink(toname, buf, sizeof buf) != len ||
   strncmp(buf, name, len) != 0)) {
  (void) (((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
  exists = 0;
     }
     if (!exists && symlink(name, toname) < 0)
  fail("cannot make symbolic link %s", toname);
     if (linkname) {
  free(linkname);
  linkname = 0;
     }
 } else {
     fromfd = open(name, 00);
     if (fromfd < 0 || fstat(fromfd, &sb) < 0)
  fail("cannot access %s", name);
     if (exists && (!((((tosb.st_mode)) & 0170000) == (0100000)) || access(toname, 2) < 0))
  (void) (((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
     tofd = open(toname, 0100 | 01, 0666);
     if (tofd < 0)
  fail("cannot create %s", toname);
     bp = buf;
     while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
  while ((wc = write(tofd, bp, cc)) > 0) {
      if ((cc -= wc) == 0)
   break;
      bp += wc;
  }
  if (wc < 0)
      fail("cannot write to %s", toname);
     }
     if (cc < 0)
  fail("cannot read from %s", name);
     if (ftruncate(tofd, sb.st_size) < 0)
  fail("cannot truncate %s", toname);
     if (dotimes) {
  utb.actime = sb.st_atim.tv_sec;
  utb.modtime = sb.st_mtim.tv_sec;
  if (utime(toname, &utb) < 0)
      fail("cannot set times of %s", toname);
     }
     if (fchmod(tofd, mode) < 0)
  fail("cannot change mode of %s", toname);
     if ((owner || group) && fchown(tofd, uid, gid) < 0)
  fail("cannot change owner of %s", toname);
     if (close(tofd) < 0)
  fail("cannot write to %s", toname);
     close(fromfd);
 }
 free(toname);
    }
    free(cwd);
    free(todir);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int onlydir; int dodir; int dolink; int dorelsymlink; int dotimes; int opt; int len; int lplen; int tdlen; int bnlen; int exists; int fromfd; int tofd; int cc; int wc;
    unsigned int mode = 0755;
    char *linkprefix; char *owner; char *group; char *cp; char *cwd; char *todir; char *toname; char *name; char *base; char *linkname; char *bp; char buf[8192];
    unsigned int uid;
    unsigned int gid;
    struct stat sb; struct stat tosb;
    struct utimbuf utb;
    program = argv[0];
    cwd = linkname = linkprefix = owner = group = 0;
    onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
    while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != (-1)) {
 switch (opt) {
   case 'C':
     cwd = optarg;
     break;
   case 'D':
     onlydir = 1;
     break;
   case 'd':
     dodir = 1;
     break;
   case 'l':
     dolink = 1;
     break;
   case 'L':
     linkprefix = optarg;
     lplen = strlen(linkprefix);
     dolink = 1;
     break;
   case 'R':
     dolink = dorelsymlink = 1;
     break;
   case 'm':
     mode = strtoul(optarg, &cp, 8);
     if (mode == 0 && cp == optarg)
  usage();
     break;
   case 'o':
     owner = optarg;
     break;
   case 'g':
     group = optarg;
     break;
   case 't':
     dotimes = 1;
     break;
   default:
     usage();
 }
    }
    argc -= optind;
    argv += optind;
    if (argc < 2 - onlydir)
 usage();
    todir = argv[argc-1];
    if ((stat(todir, &sb) < 0 || !((((sb.st_mode)) & 0170000) == (0040000))) &&
 mkdirs(todir, 0777) < 0) {
 fail("cannot make directory %s", todir);
    }
    if (onlydir)
 return 0;
    if (!cwd) {
 cwd = getcwd(0, 4096);
    }
    xchdir(todir);
    todir = getcwd(0, 4096);
    tdlen = strlen(todir);
    xchdir(cwd);
    tdlen = strlen(todir);
    uid = owner ? touid(owner) : -1;
    gid = group ? togid(group) : -1;
    while (--argc > 0) {
 name = *argv++;
 len = strlen(name);
 base = xbasename(name);
 bnlen = strlen(base);
 toname = (char *)xmalloc(tdlen + 1 + bnlen + 1);
 sprintf(toname, "%s/%s", todir, base);
 exists = (lstat(toname, &tosb) == 0);
 if (dodir) {
     if (exists && !((((tosb.st_mode)) & 0170000) == (0040000))) {
  (void) unlink(toname);
  exists = 0;
     }
     if (!exists && mkdir(toname, mode) < 0)
  fail("cannot make directory %s", toname);
     if ((owner || group) && chown(toname, uid, gid) < 0)
  fail("cannot change owner of %s", toname);
 } else if (dolink) {
     if (*name == '/') {
  linkname = 0;
     } else {
  if (linkprefix) {
      len += lplen + 1;
      linkname = (char *)xmalloc(len + 1);
      sprintf(linkname, "%s/%s", linkprefix, name);
  } else if (dorelsymlink) {
      linkname = (char *)xmalloc(4096);
      if (*todir == '/') {
   lplen = relatepaths(todir, cwd, linkname);
   strcpy(linkname + lplen, name);
      } else {
   reversepath(todir, name, len, linkname);
   xchdir(cwd);
      }
      len = strlen(linkname);
  }
  name = linkname;
     }
     if (exists &&
  (!((((tosb.st_mode)) & 0170000) == (0120000)) ||
   readlink(toname, buf, sizeof buf) != len ||
   strncmp(buf, name, len) != 0)) {
  (void) (((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
  exists = 0;
     }
     if (!exists && symlink(name, toname) < 0)
  fail("cannot make symbolic link %s", toname);
     if (linkname) {
  free(linkname);
  linkname = 0;
     }
 } else {
     fromfd = open(name, 00);
     if (fromfd < 0 || fstat(fromfd, &sb) < 0)
  fail("cannot access %s", name);
     if (exists && (!((((tosb.st_mode)) & 0170000) == (0100000)) || access(toname, 2) < 0))
  (void) (((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
     tofd = open(toname, 0100 | 01, 0666);
     if (tofd < 0)
  fail("cannot create %s", toname);
     bp = buf;
     while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
  while ((wc = write(tofd, bp, cc)) > 0) {
      if ((cc -= wc) == 0)
   break;
      bp += wc;
  }
  if (wc < 0)
      fail("cannot write to %s", toname);
     }
     if (cc < 0)
  fail("cannot read from %s", name);
     if (ftruncate(tofd, sb.st_size) < 0)
  fail("cannot truncate %s", toname);
     if (dotimes) {
  utb.actime = sb.st_atim.tv_sec;
  utb.modtime = sb.st_mtim.tv_sec;
  if (utime(toname, &utb) < 0)
      fail("cannot set times of %s", toname);
     }
     if (fchmod(tofd, mode) < 0)
  fail("cannot change mode of %s", toname);
     if ((owner || group) && fchown(tofd, uid, gid) < 0)
  fail("cannot change owner of %s", toname);
     if (close(tofd) < 0)
  fail("cannot write to %s", toname);
     close(fromfd);
 }
 free(toname);
    }
    free(cwd);
    free(todir);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: onlydir int

variable_types: dodir int

variable_types: dolink int

variable_types: dorelsymlink int

variable_types: dotimes int

variable_types: opt int

variable_types: lplen int

variable_types: tdlen int

variable_types: bnlen int

variable_types: exists int

variable_types: fromfd int

variable_types: tofd int

variable_types: cc int

variable_types: wc int

variable_types:
mode
unsigned int

variable_types:
linkprefix
char *

variable_types:
owner
char *

variable_types:
group
char *

variable_types:
cp
char *

variable_types:
cwd
char *

variable_types:
todir
char *

variable_types:
toname
char *

variable_types:
base
char *

variable_types:
linkname
char *

variable_types:
bp
char *

variable_types:
buf
char [8192]

variable_types:
uid
unsigned int

variable_types:
gid
unsigned int

variable_types: struct utimbuf { long actime; long modtime; }

return_type: int

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


escapedSize

repo: dfellis/fakeShebang

decompiled_code:
__int64 __fastcall escapedSize(char *s)
{
char *v1; // rbx
size_t v2; // rbp
unsigned int v3; // er12
int v4; // edx

v1 = s;
v2 = strlen(s);
v3 = v2;
if ( strchr(s, 32) )
{
v3 = v2 + 2;
if ( strchr(s, 34) )
{
if ( v2 )
{
do
{
v4 = *s++ == 34;
v3 += v4;
}
while ( s != &v1[v2] );
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s)
{
char *v1; // rbx
size_t v2; // rbp
unsigned int v3; // er12
int v4; // edx

v1 = s;
v2 = strlen(s);
v3 = v2;
if ( strchr(s, 32) )
{
v3 = v2 + 2;
if ( strchr(s, 34) )
{
if ( v2 )
{
do
{
v4 = *s++ == 34;
v3 += v4;
}
while ( s != &v1[v2] );
}
}
}
return v3;
}

original_code:
int escapedSize(const char* toEscape) {
 int i, newSize = strlen(toEscape);
 if(strchr(toEscape, ' ') != ((void *)0)) {
  newSize += 2;
  if(strchr(toEscape, '"') != ((void *)0)) {
   for(i = 0; i < strlen(toEscape); i++) {
    if(*(toEscape+i) == '"') {
     newSize++;
    }
   }
  }
 }
 return newSize;
}

canonical_original_code:
int escapedSize(char *toEscape) {
 int i; int newSize = strlen(toEscape);
 if(strchr(toEscape, ' ') != ((void *)0)) {
  newSize += 2;
  if(strchr(toEscape, '"') != ((void *)0)) {
   for(i = 0; i < strlen(toEscape); i++) {
    if(*(toEscape+i) == '"') {
     newSize++;
    }
   }
  }
 }
 return newSize;
}

variable_types:
toEscape
char *

variable_types: i int

variable_types: newSize int


repo: dfm/nobody

decompiled_code:
void __fastcall kepler_mean_to_ecc_anomaly(_DWORD *a1, double a2, double a3)
{
int v3; // ebx
double v4; // xmm0_8
double v5; // xmm2_8
double v6; // [rsp+0h] [rbp-40h]
double v7; // [rsp+10h] [rbp-30h]
double cosx; // [rsp+18h] [rbp-28h] BYREF
double sinx[4]; // [rsp+20h] [rbp-20h] BYREF

if ( a3 >= 0.0 && a3 < 1.0 )
{
*a1 = 0;
v3 = 500;
v4 = fmod(a2, 6.283185307179586);
v7 = v4;
while ( 1 )
{
v6 = v4;
sincos(v4, sinx, &cosx);
v5 = (v6 - sinx[0] * a3 - v7 + v6 - sinx[0] * a3 - v7)
* (1.0 - cosx * a3)
/ ((1.0 - cosx * a3 + 1.0 - cosx * a3) * (1.0 - cosx * a3) - (v6 - sinx[0] * a3 - v7) * (sinx[0] * a3));
v4 = v4 - v5;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v6 - v5 - v6) & LC3) <= 1.48e-10 )
break;
if ( !--v3 )
{
*a1 = 1;
return;
}
}
}
else
{
*a1 = 2;
}
}

original_code:
double kepler_mean_to_ecc_anomaly (double M, double e, int *info)
{
    int i;
    double wt0, psi0, psi = 0.0;
    if (e < 0 || e >= 1) {
        *info = 2;
        return 0.0;
    }
    *info = 0;
    wt0 = fmod(M, 2 * 3.14159265358979323846);
    psi0 = wt0;
    for (i = 0; i < 500; ++i) {
        double spsi0 = sin(psi0),
               f = psi0 - e * spsi0 - wt0,
               fp = 1.0 - e * cos(psi0),
               fpp = e * spsi0;
        psi = psi0 - 2.0 * f * fp / (2.0 * fp * fp - f * fpp);
        if (fabs(psi - psi0) <= 1.48e-10) return psi;
        psi0 = psi;
    }
    *info = 1;
    return psi;
}

canonical_original_code:
double kepler_mean_to_ecc_anomaly(double M, double e, int *info)
{
    int i;
    double wt0; double psi0; double psi = 0.0;
    if (e < 0 || e >= 1) {
        *info = 2;
        return 0.0;
    }
    *info = 0;
    wt0 = fmod(M, 2 * 3.14159265358979323846);
    psi0 = wt0;
    for (i = 0; i < 500; ++i) {
        double spsi0 = sin(psi0); double f = psi0 - e * spsi0 - wt0; double fp = 1.0 - e * cos(psi0); double fpp = e * spsi0;
        psi = psi0 - 2.0 * f * fp / (2.0 * fp * fp - f * fpp);
        if (fabs(psi - psi0) <= 1.48e-10) return psi;
        psi0 = psi;
    }
    *info = 1;
    return psi;
}

variable_types: M double

variable_types: e double

variable_types:
info
int *

variable_types: psi double

variable_types: f double

variable_types: fp double

variable_types: fpp double

return_type: double


decompiled_code:
__int64 __fastcall kepler_solve_one(int a1, double *a2, double *a3, double *a4, int a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13)
{
__int64 v13; // r13
double *v14; // r12
double v17; // xmm0_8
double v18; // r15
double v19; // xmm1_8
double v20; // xmm1_8
double v21; // xmm0_8
double v22; // xmm0_8
__int64 result; // rax
__int64 v24; // r13
double v25; // xmm0_8
double v26; // xmm4_8
double v27; // xmm8_8
double v28; // xmm10_8
double v29; // xmm10_8
double v30; // xmm3_8
double v31; // xmm1_8
double v32; // xmm3_8
double v33; // xmm11_8
double v34; // xmm9_8
double v35; // xmm2_8
double v36; // xmm3_8
double v37; // xmm1_8
double v38; // xmm7_8
double v39; // xmm8_8
double v40; // xmm0_8
double v41; // xmm1_8
double v42; // xmm2_8
double v43; // xmm1_8
double v44; // xmm0_8
double v45; // xmm0_8
double v46; // xmm7_8
double v47; // xmm6_8
double v48; // xmm0_8
double v49; // xmm0_8
double v50; // xmm0_8
double v51; // xmm0_8
double v52; // xmm0_8
double x; // [rsp+0h] [rbp-C8h]
double xa; // [rsp+0h] [rbp-C8h]
double xb; // [rsp+0h] [rbp-C8h]
double v58; // [rsp+10h] [rbp-B8h]
double v59; // [rsp+18h] [rbp-B0h]
double v60; // [rsp+18h] [rbp-B0h]
double v61; // [rsp+20h] [rbp-A8h]
double v62; // [rsp+28h] [rbp-A0h]
double v63; // [rsp+30h] [rbp-98h]
double v64; // [rsp+38h] [rbp-90h]
double v65; // [rsp+40h] [rbp-88h]
double v66; // [rsp+48h] [rbp-80h]
double v67; // [rsp+50h] [rbp-78h]
double v68; // [rsp+58h] [rbp-70h]
double v69; // [rsp+58h] [rbp-70h]
double v70; // [rsp+68h] [rbp-60h]
double cosx; // [rsp+70h] [rbp-58h] BYREF
double sinx[2]; // [rsp+78h] [rbp-50h] BYREF
unsigned int v73[15]; // [rsp+8Ch] [rbp-3Ch] BYREF

v13 = a5;
v14 = a2;
v17 = a8 * a8 * a8 / 2945.462538537764 / (a6 + a7);
v73[0] = 0;
if ( v17 < 0.0 )
{
xb = a10;
v52 = sqrt(v17);
a10 = xb;
v18 = v52;
}
else
{
v18 = sqrt(v17);
}
v19 = (a10 + 1.0) / (1.0 - a10);
if ( v19 < 0.0 )
{
xa = a10;
v51 = sqrt(v19);
a10 = xa;
v20 = v51;
}
else
{
v20 = sqrt(v19);
}
v59 = a10;
v21 = tan(0.5 * a11);
v22 = atan2(v21, v20);
v68 = v22;
if ( v59 < 0.0 )
return 2LL;
result = 2LL;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v59 - 1.0) & LC3) > 2.0e-16 )
{
x = 6.283185307179586 / (v18 * 6.283185307179586);
if ( a1 <= 0 )
{
LABEL_18:
result = v73[0];
}
else
{
v24 = 8 * v13;
sincos(a13, sinx, &cosx);
v61 = cosx;
v62 = sinx[0];
sincos(a12, sinx, &cosx);
v63 = cosx;
v64 = sinx[0];
sincos(a11, sinx, &cosx);
v65 = cosx;
v66 = sinx[0];
v25 = sin(v22 + v22);
v26 = v59;
v60 = a9 - (v68 + v68 - v25 * v59) * (v18 * 6.283185307179586 * 0.5) / 3.141592653589793;
while ( 1 )
{
v45 = (*v14 - v60) * x;
v58 = v26;
kepler_mean_to_ecc_anomaly(v73, v45, v26);
result = v73[0];
if ( v73[0] )
break;
sincos(v45, sinx, &cosx);
v26 = v58;
v46 = sinx[0];
v47 = *(double *)&LC1;
v48 = 1.0 - v58 * cosx;
if ( v48 == 0.0 )
return 3LL;
v27 = (cosx - v58) / v48;
v28 = 1.0 - v27 * v27;
if ( v28 < 0.0 )
{
v67 = sinx[0];
v49 = sqrt(v28);
v26 = v58;
v46 = v67;
v29 = v49;
v47 = *(double *)&LC1;
}
else
{
v29 = sqrt(v28);
}
v30 = v47 - v26 * v26;
v31 = v47 / (v26 * v27 + v47) * (a8 * v30);
if ( v30 < 0.0 )
{
v70 = v26;
v69 = v47 / (v26 * v27 + v47) * (a8 * v30);
v50 = sqrt(v30);
v26 = v70;
v31 = v69;
v32 = v50;
}
else
{
v32 = sqrt(v30);
}
v33 = v27 * v31;
++v14;
v34 = (double)((v46 >= 0.0) - (v46 < 0.0));
v35 = x * a8 * v26 * v29 / v32;
v36 = v32 * (x * a8 * a8) / v31 / v31;
v37 = v31 * v29;
v38 = v37 * v34;
v39 = v27 * v35 - v37 * v36;
v40 = (v35 * v29 + v36 * v33) * v34;
v41 = v65 * v33 + v66 * (v37 * v34);
v42 = v38 * v65 - v33 * v66;
*a3 = v41 * v63;
a3[1] = v61 * v42 - v62 * (v64 * v41);
a3[2] = v42 * v62 + v64 * v41 * v61;
a3 = (double *)((char *)a3 + v24);
v43 = v65 * v39 + v66 * v40;
v44 = v40 * v65 - v39 * v66;
*a4 = v43 * v63 * v34;
a4[1] = (v61 * v44 - v62 * (v64 * v43)) * v34;
a4[2] = (v44 * v62 + v64 * v43 * v61) * v34;
a4 = (double *)((char *)a4 + v24);
if ( &a2[(unsigned int)(a1 - 1) + 1] == v14 )
goto LABEL_18;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, double *a2, double *a3, double *a4, int a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13)
{
__int64 v13; // r13
double *v14; // r12
double v17; // xmm0_8
double v18; // r15
double v19; // xmm1_8
double v20; // xmm1_8
double v21; // xmm0_8
double v22; // xmm0_8
__int64 result; // rax
__int64 v24; // r13
double v25; // xmm0_8
double v26; // xmm4_8
double v27; // xmm8_8
double v28; // xmm10_8
double v29; // xmm10_8
double v30; // xmm3_8
double v31; // xmm1_8
double v32; // xmm3_8
double v33; // xmm11_8
double v34; // xmm9_8
double v35; // xmm2_8
double v36; // xmm3_8
double v37; // xmm1_8
double v38; // xmm7_8
double v39; // xmm8_8
double v40; // xmm0_8
double v41; // xmm1_8
double v42; // xmm2_8
double v43; // xmm1_8
double v44; // xmm0_8
double v45; // xmm0_8
double v46; // xmm7_8
double v47; // xmm6_8
double v48; // xmm0_8
double v49; // xmm0_8
double v50; // xmm0_8
double v51; // xmm0_8
double v52; // xmm0_8
double x; // [rsp+0h] [rbp-C8h]
double xa; // [rsp+0h] [rbp-C8h]
double xb; // [rsp+0h] [rbp-C8h]
double v58; // [rsp+10h] [rbp-B8h]
double v59; // [rsp+18h] [rbp-B0h]
double v60; // [rsp+18h] [rbp-B0h]
double v61; // [rsp+20h] [rbp-A8h]
double v62; // [rsp+28h] [rbp-A0h]
double v63; // [rsp+30h] [rbp-98h]
double v64; // [rsp+38h] [rbp-90h]
double v65; // [rsp+40h] [rbp-88h]
double v66; // [rsp+48h] [rbp-80h]
double v67; // [rsp+50h] [rbp-78h]
double v68; // [rsp+58h] [rbp-70h]
double v69; // [rsp+58h] [rbp-70h]
double v70; // [rsp+68h] [rbp-60h]
double cosx; // [rsp+70h] [rbp-58h] BYREF
double sinx[2]; // [rsp+78h] [rbp-50h] BYREF
unsigned int v73[15]; // [rsp+8Ch] [rbp-3Ch] BYREF

v13 = a5;
v14 = a2;
v17 = a8 * a8 * a8 / 2945.462538537764 / (a6 + a7);
v73[0] = 0;
if ( v17 < 0.0 )
{
xb = a10;
v52 = sqrt(v17);
a10 = xb;
v18 = v52;
}
else
{
v18 = sqrt(v17);
}
v19 = (a10 + 1.0) / (1.0 - a10);
if ( v19 < 0.0 )
{
xa = a10;
v51 = sqrt(v19);
a10 = xa;
v20 = v51;
}
else
{
v20 = sqrt(v19);
}
v59 = a10;
v21 = tan(0.5 * a11);
v22 = atan2(v21, v20);
v68 = v22;
if ( v59 < 0.0 )
return 2LL;
result = 2LL;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v59 - 1.0) & LC3) > 2.0e-16 )
{
x = 6.283185307179586 / (v18 * 6.283185307179586);
if ( a1 <= 0 )
{
LABEL_18:
result = v73[0];
}
else
{
v24 = 8 * v13;
sincos(a13, sinx, &cosx);
v61 = cosx;
v62 = sinx[0];
sincos(a12, sinx, &cosx);
v63 = cosx;
v64 = sinx[0];
sincos(a11, sinx, &cosx);
v65 = cosx;
v66 = sinx[0];
v25 = sin(v22 + v22);
v26 = v59;
v60 = a9 - (v68 + v68 - v25 * v59) * (v18 * 6.283185307179586 * 0.5) / 3.141592653589793;
while ( 1 )
{
v45 = (*v14 - v60) * x;
v58 = v26;
func0(v73, v45, v26);
result = v73[0];
if ( v73[0] )
break;
sincos(v45, sinx, &cosx);
v26 = v58;
v46 = sinx[0];
v47 = *(double *)&LC1;
v48 = 1.0 - v58 * cosx;
if ( v48 == 0.0 )
return 3LL;
v27 = (cosx - v58) / v48;
v28 = 1.0 - v27 * v27;
if ( v28 < 0.0 )
{
v67 = sinx[0];
v49 = sqrt(v28);
v26 = v58;
v46 = v67;
v29 = v49;
v47 = *(double *)&LC1;
}
else
{
v29 = sqrt(v28);
}
v30 = v47 - v26 * v26;
v31 = v47 / (v26 * v27 + v47) * (a8 * v30);
if ( v30 < 0.0 )
{
v70 = v26;
v69 = v47 / (v26 * v27 + v47) * (a8 * v30);
v50 = sqrt(v30);
v26 = v70;
v31 = v69;
v32 = v50;
}
else
{
v32 = sqrt(v30);
}
v33 = v27 * v31;
++v14;
v34 = (double)((v46 >= 0.0) - (v46 < 0.0));
v35 = x * a8 * v26 * v29 / v32;
v36 = v32 * (x * a8 * a8) / v31 / v31;
v37 = v31 * v29;
v38 = v37 * v34;
v39 = v27 * v35 - v37 * v36;
v40 = (v35 * v29 + v36 * v33) * v34;
v41 = v65 * v33 + v66 * (v37 * v34);
v42 = v38 * v65 - v33 * v66;
*a3 = v41 * v63;
a3[1] = v61 * v42 - v62 * (v64 * v41);
a3[2] = v42 * v62 + v64 * v41 * v61;
a3 = (double *)((char *)a3 + v24);
v43 = v65 * v39 + v66 * v40;
v44 = v40 * v65 - v39 * v66;
*a4 = v43 * v63 * v34;
a4[1] = (v61 * v44 - v62 * (v64 * v43)) * v34;
a4[2] = (v44 * v62 + v64 * v43 * v61) * v34;
a4 = (double *)((char *)a4 + v24);
if ( &a2[(unsigned int)(a1 - 1) + 1] == v14 )
goto LABEL_18;
}
}
}
return result;
}

original_code:
int kepler_solve_one (int n, double *t, double mstar, double mplanet,
                      double a, double t0, double e, double pomega, double ix,
                      double iy, double *xout, double *vout, int incr)
{
    int i, offset, info = 0, sgn;
    double period = 2 * 3.14159265358979323846 * sqrt(a * a * a / 2945.4625385377644 / (mstar + mplanet)),
           psi0 = 2 * atan2(tan(0.5 * pomega), sqrt((1 + e) / (1 - e))),
           t1 = t0 - 0.5 * period * (psi0 - e * sin(psi0)) / 3.14159265358979323846,
           manom, psi, cpsi, spsi, d, cth, sth, r, x, y, xp, yp, xsx,
           dmanomdt, denom, drdt, dthdt, dxdt, dydt, dxpdt, dypdt,
           dxsxdt, cpomega = cos(pomega), spomega = sin(pomega),
           cix = cos(ix), six = sin(ix), ciy = cos(iy), siy = sin(iy);
    if (e < 0.0 || fabs(e - 1.0) <= 2e-16) return 2;
    dmanomdt = 2 * 3.14159265358979323846 / period;
    for (i = 0; i < n; ++i) {
        manom = dmanomdt * (t[i] - t1);
        psi = kepler_mean_to_ecc_anomaly (manom, e, &info);
        if (info != 0) return info;
        cpsi = cos(psi);
        spsi = sin(psi);
        d = 1.0 - e * cpsi;
        if (d == 0.0) return 3;
        cth = (cpsi - e) / d;
        sth = sqrt(1 - cth * cth);
        denom = 1.0 / (1 + e * cth);
        r = a * (1 - e * e) * denom;
        denom = sqrt(1 - e * e);
        drdt = dmanomdt * a * e * sth / denom;
        dthdt = dmanomdt * a * a * denom / r / r;
        sgn = (spsi >= 0) - (spsi < 0);
        x = r * cth;
        y = r * sth * sgn;
        dxdt = drdt * cth - r * sth * dthdt;
        dydt = (drdt * sth + r * cth * dthdt) * sgn;
        xp = x * cpomega + y * spomega;
        yp = -x * spomega + y * cpomega;
        xsx = xp * six;
        offset = incr*i;
        xout[offset] = xp * cix;
        xout[offset+1] = yp * ciy - xsx * siy;
        xout[offset+2] = yp * siy + xsx * ciy;
        dxpdt = dxdt * cpomega + dydt * spomega;
        dypdt = -dxdt * spomega + dydt * cpomega;
        dxsxdt = dxpdt * six;
        vout[offset] = sgn * (dxpdt * cix);
        vout[offset+1] = sgn * (dypdt * ciy - dxsxdt * siy);
        vout[offset+2] = sgn * (dypdt * siy + dxsxdt * ciy);
    }
    return info;
}

canonical_original_code:
int kepler_solve_one(int n, double *t, double mstar, double mplanet, double a, double t0, double e, double pomega, double ix, double iy, double *xout, double *vout, int incr)
{
    int i; int offset; int info = 0; int sgn;
    double period = 2 * 3.14159265358979323846 * sqrt(a * a * a / 2945.4625385377644 / (mstar + mplanet)); double psi0 = 2 * atan2(tan(0.5 * pomega), sqrt((1 + e) / (1 - e))); double t1 = t0 - 0.5 * period * (psi0 - e * sin(psi0)) / 3.14159265358979323846; double manom; double psi; double cpsi; double spsi; double d; double cth; double sth; double r; double x; double y; double xp; double yp; double xsx; double dmanomdt; double denom; double drdt; double dthdt; double dxdt; double dydt; double dxpdt; double dypdt; double dxsxdt; double cpomega = cos(pomega); double spomega = sin(pomega); double cix = cos(ix); double six = sin(ix); double ciy = cos(iy); double siy = sin(iy);
    if (e < 0.0 || fabs(e - 1.0) <= 2e-16) return 2;
    dmanomdt = 2 * 3.14159265358979323846 / period;
    for (i = 0; i < n; ++i) {
        manom = dmanomdt * (t[i] - t1);
        psi = kepler_mean_to_ecc_anomaly (manom, e, &info);
        if (info != 0) return info;
        cpsi = cos(psi);
        spsi = sin(psi);
        d = 1.0 - e * cpsi;
        if (d == 0.0) return 3;
        cth = (cpsi - e) / d;
        sth = sqrt(1 - cth * cth);
        denom = 1.0 / (1 + e * cth);
        r = a * (1 - e * e) * denom;
        denom = sqrt(1 - e * e);
        drdt = dmanomdt * a * e * sth / denom;
        dthdt = dmanomdt * a * a * denom / r / r;
        sgn = (spsi >= 0) - (spsi < 0);
        x = r * cth;
        y = r * sth * sgn;
        dxdt = drdt * cth - r * sth * dthdt;
        dydt = (drdt * sth + r * cth * dthdt) * sgn;
        xp = x * cpomega + y * spomega;
        yp = -x * spomega + y * cpomega;
        xsx = xp * six;
        offset = incr*i;
        xout[offset] = xp * cix;
        xout[offset+1] = yp * ciy - xsx * siy;
        xout[offset+2] = yp * siy + xsx * ciy;
        dxpdt = dxdt * cpomega + dydt * spomega;
        dypdt = -dxdt * spomega + dydt * cpomega;
        dxsxdt = dxpdt * six;
        vout[offset] = sgn * (dxpdt * cix);
        vout[offset+1] = sgn * (dypdt * ciy - dxsxdt * siy);
        vout[offset+2] = sgn * (dypdt * siy + dxsxdt * ciy);
    }
    return info;
}

variable_types: n int

variable_types:
t
double *

variable_types: mstar double

variable_types: mplanet double

variable_types: a double

variable_types: pomega double

variable_types: ix double

variable_types: iy double

variable_types:
xout
double *

variable_types:
vout
double *

variable_types: incr int

variable_types: offset int

variable_types: info int

variable_types: sgn int

variable_types: period double

variable_types: manom double

variable_types: cpsi double

variable_types: spsi double

variable_types: d double

variable_types: cth double

variable_types: sth double

variable_types: r double

variable_types: x double

variable_types: y double

variable_types: xp double

variable_types: yp double

variable_types: xsx double

variable_types: dmanomdt double

variable_types: denom double

variable_types: drdt double

variable_types: dthdt double

variable_types: dxdt double

variable_types: dydt double

variable_types: dxpdt double

variable_types: dypdt double

variable_types: dxsxdt double

variable_types: cpomega double

variable_types: spomega double

variable_types: cix double

variable_types: six double

variable_types: ciy double

variable_types: siy double


original_code:
int kepler_solve (int n, double *t, double mstar, int np, double *m,
                  double *a, double *t0, double *e, double *pomega,
                  double *ix, double *iy, double *x, double *v)
{
    int i, j, k, offset, info = 0;
    offset = 3 * (np + 1);
    for (i = 0; i < n; ++i)
        for (j = 0; j < 3; ++j) {
            x[offset*i+j] = 0.0;
            v[offset*i+j] = 0.0;
        }
    for (k = 0; k < np; ++k) {
        info = kepler_solve_one (n, t, mstar, m[k], a[k], t0[k], e[k],
                                 pomega[k], ix[k], iy[k],
                                 x+3*(1+k), v+3*(1+k), offset);
        if (info != 0) {
            return info;
        }
        for (i = 0; i < n; ++i) {
            for (j = 0; j < 3; ++j)
                v[offset*i+j] -= m[k] * v[offset*i+j+3*(k+1)] / mstar;
        }
    }
    return info;
}

canonical_original_code:
int kepler_solve(int n, double *t, double mstar, int np, double *m, double *a, double *t0, double *e, double *pomega, double *ix, double *iy, double *x, double *v)
{
    int i; int j; int k; int offset; int info = 0;
    offset = 3 * (np + 1);
    for (i = 0; i < n; ++i)
        for (j = 0; j < 3; ++j) {
            x[offset*i+j] = 0.0;
            v[offset*i+j] = 0.0;
        }
    for (k = 0; k < np; ++k) {
        info = kepler_solve_one (n, t, mstar, m[k], a[k], t0[k], e[k],
                                 pomega[k], ix[k], iy[k],
                                 x+3*(1+k), v+3*(1+k), offset);
        if (info != 0) {
            return info;
        }
        for (i = 0; i < n; ++i) {
            for (j = 0; j < 3; ++j)
                v[offset*i+j] -= m[k] * v[offset*i+j+3*(k+1)] / mstar;
        }
    }
    return info;
}

variable_types: n int

variable_types:
t
double *

variable_types: mstar double

variable_types: np int

variable_types:
m
double *

variable_types:
a
double *

variable_types:
e
double *

variable_types:
pomega
double *

variable_types:
ix
double *

variable_types:
iy
double *

variable_types:
x
double *

variable_types:
v
double *

variable_types: i int

variable_types: j int

variable_types: k int

variable_types: offset int

variable_types: info int


decompiled_code:
void __fastcall nobody_free_system(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void nobody_free_system (nobody_system *nbs)
{
    free(nbs->masses);
    free(nbs);
}

canonical_original_code:
void nobody_free_system(struct nobody_system_struct *nbs)
{
    free(nbs->masses);
    free(nbs);
}

variable_types: struct nobody_system_struct { int nbodies; double *masses; } *

return_type: void


original_code:
void nobody_leapfrog_step (double h, double *x, double *v,
                           double *xout, double *vout, nobody_system *nbs)
{
    int i, N = 3*nbs->nbodies;
    double hh = 0.5 * h,
           *a = malloc(N*sizeof(double));
    nobody_gradient (x, a, nbs);
    for (i = 0; i < N; ++i) {
        vout[i] = v[i] + hh * a[i];
        xout[i] = x[i] + h * vout[i];
    }
    nobody_gradient (xout, a, nbs);
    for (i = 0; i < N; ++i)
        vout[i] += hh * a[i];
    free(a);
}

canonical_original_code:
void nobody_leapfrog_step(double h, double *x, double *v, double *xout, double *vout, struct nobody_system_struct *nbs)
{
    int i; int N = 3*nbs->nbodies;
    double hh = 0.5 * h; double *a = malloc(N*sizeof(double));
    nobody_gradient (x, a, nbs);
    for (i = 0; i < N; ++i) {
        vout[i] = v[i] + hh * a[i];
        xout[i] = x[i] + h * vout[i];
    }
    nobody_gradient (xout, a, nbs);
    for (i = 0; i < N; ++i)
        vout[i] += hh * a[i];
    free(a);
}

variable_types: h double

variable_types:
xout
double *

variable_types:
vout
double *

variable_types: N int

variable_types: hh double

user_defined_types: struct nobody_system_struct { int nbodies; double *masses; }


original_code:
void nobody_midpoint_step (double H, int nstep, double *x, double *v,
                        double *xout, double *vout, nobody_system *nbs)
{
    int i, k, n = 3*nbs->nbodies;
    double h = H / nstep, swap,
           *x0 = malloc(n * sizeof(double)),
           *x1 = malloc(n * sizeof(double)),
           *dx = malloc(n * sizeof(double)),
           *v0 = malloc(n * sizeof(double)),
           *v1 = malloc(n * sizeof(double)),
           *dv = malloc(n * sizeof(double));
    nobody_gradient (x, dv, nbs);
    for (i = 0; i < n; ++i) {
        x0[i] = x[i];
        x1[i] = x[i] + h * v[i];
        v0[i] = v[i];
        v1[i] = v[i] + h * dv[i];
    }
    for (k = 2; k <= nstep; ++k) {
        nobody_gradient (x1, dv, nbs);
        for (i = 0; i < n; ++i) {
            swap = x1[i];
            x1[i] = x0[i] + 2 * h * v1[i];
            x0[i] = swap;
            swap = v1[i];
            v1[i] = v0[i] + 2 * h * dv[i];
            v0[i] = swap;
        }
    }
    nobody_gradient (x1, dv, nbs);
    for (i = 0; i < n; ++i) {
        xout[i] = 0.5 * (x0[i] + x1[i] + h * v1[i]);
        vout[i] = 0.5 * (v0[i] + v1[i] + h * dv[i]);
    }
    free(x0);
    free(x1);
    free(dx);
    free(v0);
    free(v1);
    free(dv);
}

canonical_original_code:
void nobody_midpoint_step(double H, int nstep, double *x, double *v, double *xout, double *vout, struct nobody_system_struct *nbs)
{
    int i; int k; int n = 3*nbs->nbodies;
    double h = H / nstep; double swap; double *x0 = malloc(n * sizeof(double)); double *x1 = malloc(n * sizeof(double)); double *dx = malloc(n * sizeof(double)); double *v0 = malloc(n * sizeof(double)); double *v1 = malloc(n * sizeof(double)); double *dv = malloc(n * sizeof(double));
    nobody_gradient (x, dv, nbs);
    for (i = 0; i < n; ++i) {
        x0[i] = x[i];
        x1[i] = x[i] + h * v[i];
        v0[i] = v[i];
        v1[i] = v[i] + h * dv[i];
    }
    for (k = 2; k <= nstep; ++k) {
        nobody_gradient (x1, dv, nbs);
        for (i = 0; i < n; ++i) {
            swap = x1[i];
            x1[i] = x0[i] + 2 * h * v1[i];
            x0[i] = swap;
            swap = v1[i];
            v1[i] = v0[i] + 2 * h * dv[i];
            v0[i] = swap;
        }
    }
    nobody_gradient (x1, dv, nbs);
    for (i = 0; i < n; ++i) {
        xout[i] = 0.5 * (x0[i] + x1[i] + h * v1[i]);
        vout[i] = 0.5 * (v0[i] + v1[i] + h * dv[i]);
    }
    free(x0);
    free(x1);
    free(dx);
    free(v0);
    free(v1);
    free(dv);
}

variable_types: H double

variable_types: nstep int

variable_types: n int

variable_types: swap double

variable_types:
dx
double *

variable_types:
dv
double *


decompiled_code:
void clear_tab_hold()
{
char *v0; // rbp
char **v1; // rbx
size_t v2; // rax

v0 = tabCompHold;
if ( tabCompHold )
{
v1 = &tabCompHold;
do
{
++v1;
v2 = strlen(v0);
memset(v0, 0, v2 + 1);
*(v1 - 1) = 0LL;
v0 = *v1;
}
while ( *v1 );
}
}

canonical_decompiled_code:
void func0()
{
char *v0; // rbp
char **v1; // rbx
size_t v2; // rax

v0 = tabCompHold;
if ( tabCompHold )
{
v1 = &tabCompHold;
do
{
++v1;
v2 = strlen(v0);
memset(v0, 0, v2 + 1);
*(v1 - 1) = 0LL;
v0 = *v1;
}
while ( *v1 );
}
}

original_code:
void clear_tab_hold(void)
{
 int i;
 for(i=0;tabCompHold[i]!=((void *)0);++i) {
  memset(tabCompHold[i],0,strlen(tabCompHold[i])+1);
  tabCompHold[i] = ((void *)0);
  free(tabCompHold[i]);
 }
}

canonical_original_code:
void clear_tab_hold()
{
 int i;
 for(i=0;tabCompHold[i]!=((void *)0);++i) {
  memset(tabCompHold[i],0,strlen(tabCompHold[i])+1);
  tabCompHold[i] = ((void *)0);
  free(tabCompHold[i]);
 }
}

variable_types: i int


decompiled_code:
__int64 __fastcall add_history(char *src)
{
_QWORD *v1; // rbp
_QWORD *v2; // rax
__int64 v3; // r14
_QWORD *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r15
_QWORD *v8; // rbp
char *v9; // rax
char *v10; // r13
size_t v11; // rax
__int64 result; // rax
__int64 v13; // rdx
char *v14; // rax
char *v15; // r13
size_t v16; // rax

v1 = malloc(0x18uLL);
v2 = malloc(0x18uLL);
v3 = historyHead;
curHistory = (__int64)v2;
v4 = v2;
*v2 = 0LL;
v2[1] = 0LL;
v5 = v3;
if ( v3 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
v7 = *(_QWORD *)(v6 + 16);
v8 = malloc(0x18uLL);
v9 = (char *)calloc(0x64uLL, 1uLL);
*v8 = v9;
v10 = v9;
v11 = strlen(src);
strncpy(v10, src, v11);
v8[1] = v4;
v8[2] = v7;
*(_QWORD *)(v7 + 8) = v8;
*(_QWORD *)(v3 + 16) = v8;
v4[2] = v8;
}
else
{
v14 = (char *)calloc(0x64uLL, 1uLL);
*v1 = v14;
v15 = v14;
v16 = strlen(src);
strncpy(v15, src, v16);
v1[2] = v1;
historyHead = (__int64)v1;
v1[1] = v4;
v4[2] = v1;
}
result = (unsigned int)++historyInd;
if ( historyInd > historyMax )
{
v13 = *(_QWORD *)(historyHead + 16);
result = *(_QWORD *)(historyHead + 8);
historyHead = result;
*(_QWORD *)(result + 16) = v13;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src)
{
_QWORD *v1; // rbp
_QWORD *v2; // rax
__int64 v3; // r14
_QWORD *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r15
_QWORD *v8; // rbp
char *v9; // rax
char *v10; // r13
size_t v11; // rax
__int64 result; // rax
__int64 v13; // rdx
char *v14; // rax
char *v15; // r13
size_t v16; // rax

v1 = malloc(0x18uLL);
v2 = malloc(0x18uLL);
v3 = historyHead;
curHistory = (__int64)v2;
v4 = v2;
*v2 = 0LL;
v2[1] = 0LL;
v5 = v3;
if ( v3 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
v7 = *(_QWORD *)(v6 + 16);
v8 = malloc(0x18uLL);
v9 = (char *)calloc(0x64uLL, 1uLL);
*v8 = v9;
v10 = v9;
v11 = strlen(src);
strncpy(v10, src, v11);
v8[1] = v4;
v8[2] = v7;
*(_QWORD *)(v7 + 8) = v8;
*(_QWORD *)(v3 + 16) = v8;
v4[2] = v8;
}
else
{
v14 = (char *)calloc(0x64uLL, 1uLL);
*v1 = v14;
v15 = v14;
v16 = strlen(src);
strncpy(v15, src, v16);
v1[2] = v1;
historyHead = (__int64)v1;
v1[1] = v4;
v4[2] = v1;
}
result = (unsigned int)++historyInd;
if ( historyInd > historyMax )
{
v13 = *(_QWORD *)(historyHead + 16);
result = *(_QWORD *)(historyHead + 8);
historyHead = result;
*(_QWORD *)(result + 16) = v13;
}
return result;
}

original_code:
void add_history(char *line)
{
 struct HistoryList *temp = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curHistory = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curHistory->data = ((void *)0);
 curHistory->next = ((void *)0);
 if(historyHead == ((void *)0)) {
  temp->data = (char*)malloc(sizeof(char) * 100);
  memset(temp->data,0,100);
  strncpy(temp->data,line,strlen(line));
  temp->next = ((void *)0);
  temp->prev = temp;
  historyHead = temp;
  historyHead->next = curHistory;
  curHistory->prev = historyHead;
 }
 else {
  temp = historyHead;
  while(temp->next != ((void *)0)) {
   temp = temp->next;
  }
  temp = temp->prev;
  struct HistoryList *temp2 = (struct HistoryList*)malloc(sizeof(struct HistoryList));
  temp2->data = (char *)malloc(sizeof(char) * 100);
  memset(temp2->data,0,100);
  strncpy(temp2->data,line,strlen(line));
  temp2->next = curHistory;
  temp2->prev = temp;
  temp->next = temp2;
  historyHead->prev = temp2;
  curHistory->prev = temp2;
 }
 ++historyInd;
 if (historyInd > historyMax) {
  struct HistoryList *tmp = historyHead->prev;
  historyHead = historyHead->next;
  historyHead->prev = tmp;
 }
}

canonical_original_code:
void add_history(char *line)
{
 struct HistoryList *temp = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curHistory = (struct HistoryList *)malloc(sizeof(struct HistoryList));
 curHistory->data = ((void *)0);
 curHistory->next = ((void *)0);
 if(historyHead == ((void *)0)) {
  temp->data = (char *)malloc(sizeof(char) * 100);
  memset(temp->data,0,100);
  strncpy(temp->data,line,strlen(line));
  temp->next = ((void *)0);
  temp->prev = temp;
  historyHead = temp;
  historyHead->next = curHistory;
  curHistory->prev = historyHead;
 }
 else {
  temp = historyHead;
  while(temp->next != ((void *)0)) {
   temp = temp->next;
  }
  temp = temp->prev;
  struct HistoryList *temp2 = (struct HistoryList*)malloc(sizeof(struct HistoryList));
  temp2->data = (char *)malloc(sizeof(char) * 100);
  memset(temp2->data,0,100);
  strncpy(temp2->data,line,strlen(line));
  temp2->next = curHistory;
  temp2->prev = temp;
  temp->next = temp2;
  historyHead->prev = temp2;
  curHistory->prev = temp2;
 }
 ++historyInd;
 if (historyInd > historyMax) {
  struct HistoryList *tmp = historyHead->prev;
  historyHead = historyHead->next;
  historyHead->prev = tmp;
 }
}

variable_types:
line
char *

variable_types: struct HistoryList { char *data; struct HistoryList *next; struct HistoryList *prev; } *


decompiled_code:
__int64 __fastcall add_nav_history(char *src)
{
_QWORD *v1; // rbp
_QWORD *v2; // rax
__int64 v3; // r14
_QWORD *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r15
_QWORD *v8; // rbp
char *v9; // rax
char *v10; // r13
size_t v11; // rax
__int64 result; // rax
__int64 v13; // rdx
char *v14; // rax
char *v15; // r13
size_t v16; // rax

v1 = malloc(0x18uLL);
v2 = malloc(0x18uLL);
v3 = navHistoryHead;
curNavHistory = (__int64)v2;
v4 = v2;
*v2 = 0LL;
v2[1] = 0LL;
v5 = v3;
if ( v3 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
v7 = *(_QWORD *)(v6 + 16);
v8 = malloc(0x18uLL);
v9 = (char *)calloc(0x64uLL, 1uLL);
*v8 = v9;
v10 = v9;
v11 = strlen(src);
strncpy(v10, src, v11);
v8[1] = v4;
v8[2] = v7;
*(_QWORD *)(v7 + 8) = v8;
*(_QWORD *)(v3 + 16) = v8;
v4[2] = v8;
}
else
{
v14 = (char *)calloc(0x64uLL, 1uLL);
*v1 = v14;
v15 = v14;
v16 = strlen(src);
strncpy(v15, src, v16);
v1[2] = v1;
navHistoryHead = (__int64)v1;
v1[1] = v4;
v4[2] = v1;
}
result = (unsigned int)++navHistoryInd;
if ( navHistoryInd > navHistoryMax )
{
v13 = *(_QWORD *)(navHistoryHead + 16);
result = *(_QWORD *)(navHistoryHead + 8);
navHistoryHead = result;
*(_QWORD *)(result + 16) = v13;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src)
{
_QWORD *v1; // rbp
_QWORD *v2; // rax
__int64 v3; // r14
_QWORD *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // r15
_QWORD *v8; // rbp
char *v9; // rax
char *v10; // r13
size_t v11; // rax
__int64 result; // rax
__int64 v13; // rdx
char *v14; // rax
char *v15; // r13
size_t v16; // rax

v1 = malloc(0x18uLL);
v2 = malloc(0x18uLL);
v3 = navHistoryHead;
curNavHistory = (__int64)v2;
v4 = v2;
*v2 = 0LL;
v2[1] = 0LL;
v5 = v3;
if ( v3 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
v7 = *(_QWORD *)(v6 + 16);
v8 = malloc(0x18uLL);
v9 = (char *)calloc(0x64uLL, 1uLL);
*v8 = v9;
v10 = v9;
v11 = strlen(src);
strncpy(v10, src, v11);
v8[1] = v4;
v8[2] = v7;
*(_QWORD *)(v7 + 8) = v8;
*(_QWORD *)(v3 + 16) = v8;
v4[2] = v8;
}
else
{
v14 = (char *)calloc(0x64uLL, 1uLL);
*v1 = v14;
v15 = v14;
v16 = strlen(src);
strncpy(v15, src, v16);
v1[2] = v1;
navHistoryHead = (__int64)v1;
v1[1] = v4;
v4[2] = v1;
}
result = (unsigned int)++navHistoryInd;
if ( navHistoryInd > navHistoryMax )
{
v13 = *(_QWORD *)(navHistoryHead + 16);
result = *(_QWORD *)(navHistoryHead + 8);
navHistoryHead = result;
*(_QWORD *)(result + 16) = v13;
}
return result;
}

original_code:
void add_nav_history(char *line)
{
 struct HistoryList *temp = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curNavHistory = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curNavHistory->data = ((void *)0);
 curNavHistory->next = ((void *)0);
 if(navHistoryHead == ((void *)0)) {
  temp->data = (char*)malloc(sizeof(char) * 100);
  memset(temp->data,0,100);
  strncpy(temp->data,line,strlen(line));
  temp->next = ((void *)0);
  temp->prev = temp;
  navHistoryHead = temp;
  navHistoryHead->next = curNavHistory;
  curNavHistory->prev = navHistoryHead;
 }
 else {
  temp = navHistoryHead;
  while(temp->next != ((void *)0)) {
   temp = temp->next;
  }
  temp = temp->prev;
  struct HistoryList *temp2 = (struct HistoryList*)malloc(sizeof(struct HistoryList));
  temp2->data = (char *)malloc(sizeof(char) * 100);
  memset(temp2->data,0,100);
  strncpy(temp2->data,line,strlen(line));
  temp2->next = curNavHistory;
  temp2->prev = temp;
  temp->next = temp2;
  navHistoryHead->prev = temp2;
  curNavHistory->prev = temp2;
 }
 ++navHistoryInd;
 if (navHistoryInd > navHistoryMax) {
  struct HistoryList *tmp = navHistoryHead->prev;
  navHistoryHead = navHistoryHead->next;
  navHistoryHead->prev = tmp;
 }
}

canonical_original_code:
void add_nav_history(char *line)
{
 struct HistoryList *temp = (struct HistoryList*)malloc(sizeof(struct HistoryList));
 curNavHistory = (struct HistoryList *)malloc(sizeof(struct HistoryList));
 curNavHistory->data = ((void *)0);
 curNavHistory->next = ((void *)0);
 if(navHistoryHead == ((void *)0)) {
  temp->data = (char *)malloc(sizeof(char) * 100);
  memset(temp->data,0,100);
  strncpy(temp->data,line,strlen(line));
  temp->next = ((void *)0);
  temp->prev = temp;
  navHistoryHead = temp;
  navHistoryHead->next = curNavHistory;
  curNavHistory->prev = navHistoryHead;
 }
 else {
  temp = navHistoryHead;
  while(temp->next != ((void *)0)) {
   temp = temp->next;
  }
  temp = temp->prev;
  struct HistoryList *temp2 = (struct HistoryList*)malloc(sizeof(struct HistoryList));
  temp2->data = (char *)malloc(sizeof(char) * 100);
  memset(temp2->data,0,100);
  strncpy(temp2->data,line,strlen(line));
  temp2->next = curNavHistory;
  temp2->prev = temp;
  temp->next = temp2;
  navHistoryHead->prev = temp2;
  curNavHistory->prev = temp2;
 }
 ++navHistoryInd;
 if (navHistoryInd > navHistoryMax) {
  struct HistoryList *tmp = navHistoryHead->prev;
  navHistoryHead = navHistoryHead->next;
  navHistoryHead->prev = tmp;
 }
}

user_defined_types: struct HistoryList { char *data; struct HistoryList *next; struct HistoryList *prev; }


decompiled_code:
char *prev_history()
{
char *v0; // r12
const char **v1; // rbx
char *v2; // rax
const char *v3; // rbp
size_t v4; // rax

v0 = (char *)curHistory;
if ( curHistory )
{
v1 = (const char **)historyHead;
if ( curHistory != historyHead )
{
v1 = *(const char ***)(curHistory + 16);
curHistory = (__int64)v1;
}
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = *v1;
v0 = v2;
v4 = strlen(*v1);
strncpy(v0, v3, v4);
}
return v0;
}

canonical_decompiled_code:
char *func2()
{
char *v0; // r12
const char **v1; // rbx
char *v2; // rax
const char *v3; // rbp
size_t v4; // rax

v0 = (char *)curHistory;
if ( curHistory )
{
v1 = (const char **)historyHead;
if ( curHistory != historyHead )
{
v1 = *(const char ***)(curHistory + 16);
curHistory = (__int64)v1;
}
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = *v1;
v0 = v2;
v4 = strlen(*v1);
strncpy(v0, v3, v4);
}
return v0;
}

original_code:
char* prev_history(void)
{
 if(curHistory == ((void *)0)) {return 0;}
 if(curHistory != historyHead) {
  curHistory = curHistory->prev;
 }
 char *tmp = (char*)malloc(sizeof(char)*100);
 memset(tmp,0,100);
 strncpy(tmp, curHistory->data, strlen(curHistory->data));
 return tmp;
}

canonical_original_code:
char *prev_history()
{
 if(curHistory == ((void *)0)) {return 0;}
 if(curHistory != historyHead) {
  curHistory = curHistory->prev;
 }
 char *tmp = (char*)malloc(sizeof(char)*100);
 memset(tmp,0,100);
 strncpy(tmp, curHistory->data, strlen(curHistory->data));
 return tmp;
}

variable_types:
tmp
char *

return_type: char *


decompiled_code:
char *next_history()
{
char *v0; // r12
const char **v1; // rbx
char *v2; // rax
const char *v3; // rbp
size_t v4; // rax

v0 = (char *)curHistory;
if ( curHistory )
{
v1 = *(const char ***)(curHistory + 8);
if ( v1 )
{
v0 = (char *)*v1;
curHistory = *(_QWORD *)(curHistory + 8);
if ( v0 )
{
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = *v1;
v0 = v2;
v4 = strlen(*v1);
strncpy(v0, v3, v4);
}
}
else
{
v0 = 0LL;
}
}
return v0;
}

canonical_decompiled_code:
char *func3()
{
char *v0; // r12
const char **v1; // rbx
char *v2; // rax
const char *v3; // rbp
size_t v4; // rax

v0 = (char *)curHistory;
if ( curHistory )
{
v1 = *(const char ***)(curHistory + 8);
if ( v1 )
{
v0 = (char *)*v1;
curHistory = *(_QWORD *)(curHistory + 8);
if ( v0 )
{
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = *v1;
v0 = v2;
v4 = strlen(*v1);
strncpy(v0, v3, v4);
}
}
else
{
v0 = 0LL;
}
}
return v0;
}

original_code:
char* next_history(void)
{
 if(curHistory == ((void *)0)) {return 0;}
 if(curHistory->next == ((void *)0)){return 0;}
 if(curHistory->next->data != ((void *)0)) {
  curHistory = curHistory->next;
 } else {curHistory = curHistory->next; return 0;}
 char *tmp = (char*)malloc(sizeof(char)*100);
 memset(tmp,0,100);
 strncpy(tmp, curHistory->data, strlen(curHistory->data));
 return tmp;
}

canonical_original_code:
char *next_history()
{
 if(curHistory == ((void *)0)) {return 0;}
 if(curHistory->next == ((void *)0)){return 0;}
 if(curHistory->next->data != ((void *)0)) {
  curHistory = curHistory->next;
 } else {curHistory = curHistory->next; return 0;}
 char *tmp = (char*)malloc(sizeof(char)*100);
 memset(tmp,0,100);
 strncpy(tmp, curHistory->data, strlen(curHistory->data));
 return tmp;
}


decompiled_code:
int print_history()
{
int v0; // ebp
__int64 v1; // rbx
int result; // eax

v0 = 0;
v1 = historyHead;
if ( !historyHead )
return puts("No history.");
do
{
if ( !*(_QWORD *)v1 )
break;
result = printf(" %i %s\n", (unsigned int)++v0, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int func4()
{
int v0; // ebp
__int64 v1; // rbx
int result; // eax

v0 = 0;
v1 = historyHead;
if ( !historyHead )
return puts("No history.");
do
{
if ( !*(_QWORD *)v1 )
break;
result = printf(" %i %s\n", (unsigned int)++v0, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

original_code:
void print_history(void)
{
 int x = 0;
 struct HistoryList *temp;
 temp = historyHead;
 if(temp == ((void *)0)) { printf("No history.\n"); }
 else {
  while(temp != ((void *)0) && temp->data != ((void *)0)) {
   printf("  %i    %s\n",x+1,temp->data);
   ++x;
   temp = temp->next;
  }
 }
}

canonical_original_code:
void print_history()
{
 int x = 0;
 struct HistoryList *temp;
 temp = historyHead;
 if(temp == ((void *)0)) { printf("No history.\n"); }
 else {
  while(temp != ((void *)0) && temp->data != ((void *)0)) {
   printf("  %i    %s\n",x+1,temp->data);
   ++x;
   temp = temp->next;
  }
 }
}

variable_types: x int


decompiled_code:
int print_nav_history()
{
int v0; // ebp
__int64 v1; // rbx
int result; // eax

v0 = 0;
v1 = navHistoryHead;
if ( !navHistoryHead )
return puts("No nav history.");
do
{
if ( !*(_QWORD *)v1 )
break;
result = printf(" %i %s\n", (unsigned int)++v0, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int func5()
{
int v0; // ebp
__int64 v1; // rbx
int result; // eax

v0 = 0;
v1 = navHistoryHead;
if ( !navHistoryHead )
return puts("No nav history.");
do
{
if ( !*(_QWORD *)v1 )
break;
result = printf(" %i %s\n", (unsigned int)++v0, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

original_code:
void print_nav_history(void)
{
 int x = 0;
 struct HistoryList *temp;
 temp = navHistoryHead;
 if(temp == ((void *)0)) { printf("No nav history.\n"); }
 else {
  while(temp != ((void *)0) && temp->data != ((void *)0)) {
   printf("  %i    %s\n",x+1,temp->data);
   ++x;
   temp = temp->next;
  }
 }
}

canonical_original_code:
void print_nav_history()
{
 int x = 0;
 struct HistoryList *temp;
 temp = navHistoryHead;
 if(temp == ((void *)0)) { printf("No nav history.\n"); }
 else {
  while(temp != ((void *)0) && temp->data != ((void *)0)) {
   printf("  %i    %s\n",x+1,temp->data);
   ++x;
   temp = temp->next;
  }
 }
}


decompiled_code:
void __fastcall exec_history(__int64 a1)
{
int v1; // eax
int v2; // er12
char *v3; // rbp
__int64 v4; // rcx
int v5; // er12
int i; // edx
const char *v7; // r12
size_t v8; // rax
char *v9; // r12
size_t v10; // rax
size_t v11; // rax
int v12; // eax

v1 = atoi((const char *)(a1 + 1));
v2 = historyInd;
if ( *(_BYTE *)(a1 + 1) )
v2 = v1;
if ( !v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: !%s: not a valid event\n", (const char *)(a1 + 1));
goto LABEL_12;
}
v3 = (char *)calloc(0x64uLL, 1uLL);
if ( v2 < 0 || historyInd < v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: !%i: event not found\n", (unsigned int)v2);
LABEL_12:
v11 = strlen(v3);
write(1, v3, v11);
goto LABEL_13;
}
v4 = historyHead;
if ( historyInd / 2 < v2 )
{
v4 = *(_QWORD *)(historyHead + 16);
if ( historyInd != v2 )
{
v12 = 0;
do
{
++v12;
v4 = *(_QWORD *)(v4 + 16);
}
while ( historyInd - v2 != v12 );
}
}
else
{
v5 = v2 - 1;
if ( v5 )
{
for ( i = 0; i != v5; ++i )
v4 = *(_QWORD *)(v4 + 8);
}
}
v7 = *(const char **)v4;
v8 = strlen(*(const char **)v4);
strncpy(v3, v7, v8);
v9 = (char *)calloc(0x64uLL, 1uLL);
sprintf(v9, "%s\n", v3);
v10 = strlen(v9);
write(1, v9, v10);
clear_args();
parse_args(v3);
add_history(v3);
execute(args);
LABEL_13:
free(v3);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
int v1; // eax
int v2; // er12
char *v3; // rbp
__int64 v4; // rcx
int v5; // er12
int i; // edx
const char *v7; // r12
size_t v8; // rax
char *v9; // r12
size_t v10; // rax
size_t v11; // rax
int v12; // eax

v1 = atoi((const char *)(a1 + 1));
v2 = historyInd;
if ( *(_BYTE *)(a1 + 1) )
v2 = v1;
if ( !v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: !%s: not a valid event\n", (const char *)(a1 + 1));
goto LABEL_12;
}
v3 = (char *)calloc(0x64uLL, 1uLL);
if ( v2 < 0 || historyInd < v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: !%i: event not found\n", (unsigned int)v2);
LABEL_12:
v11 = strlen(v3);
write(1, v3, v11);
goto LABEL_13;
}
v4 = historyHead;
if ( historyInd / 2 < v2 )
{
v4 = *(_QWORD *)(historyHead + 16);
if ( historyInd != v2 )
{
v12 = 0;
do
{
++v12;
v4 = *(_QWORD *)(v4 + 16);
}
while ( historyInd - v2 != v12 );
}
}
else
{
v5 = v2 - 1;
if ( v5 )
{
for ( i = 0; i != v5; ++i )
v4 = *(_QWORD *)(v4 + 8);
}
}
v7 = *(const char **)v4;
v8 = strlen(*(const char **)v4);
strncpy(v3, v7, v8);
v9 = (char *)calloc(0x64uLL, 1uLL);
sprintf(v9, "%s\n", v3);
v10 = strlen(v9);
write(1, v9, v10);
clear_args();
parse_args(v3);
func0(v3);
execute(args);
LABEL_13:
free(v3);
}

original_code:
void exec_history(char* cmd)
{
 ++cmd;
 int hisNum = atoi(cmd);
 if(strlen(cmd) == 0) { hisNum = historyInd; }
 if(hisNum == 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: !%s: not a valid event\n", cmd);
  write(1,tmp,strlen(tmp));
  free(tmp);
  return;
 }
 char *tmp = (char*)malloc(sizeof(char) * 100);
 memset(tmp,0,100);
 if(hisNum > historyInd || hisNum < 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: !%i: event not found\n", hisNum);
  write(1,tmp,strlen(tmp));
  free(tmp);
 } else {
  if(hisNum <= (int)(historyInd/2)) {
   int x = 0;
   struct HistoryList *cur = historyHead;
   for(;x<hisNum-1;++x) {
    cur = cur->next;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  } else {
   int x = 0;
   int y = historyInd-hisNum;
   struct HistoryList *cur = historyHead->prev;
   for(;x<y;++x) {
    cur = cur->prev;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  }
  char *tmp2 = (char*)malloc(sizeof(char) * 100);
  memset(tmp2,0,100);
  sprintf(tmp2,"%s\n",tmp);
  write(1,tmp2,strlen(tmp2));
  clear_args();
  parse_args(tmp);
  add_history(tmp);
  execute(args[0]);
  free(tmp);
 }
}

canonical_original_code:
void exec_history(char *cmd)
{
 ++cmd;
 int hisNum = atoi(cmd);
 if(strlen(cmd) == 0) { hisNum = historyInd; }
 if(hisNum == 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: !%s: not a valid event\n", cmd);
  write(1,tmp,strlen(tmp));
  free(tmp);
  return;
 }
 char *tmp = (char*)malloc(sizeof(char) * 100);
 memset(tmp,0,100);
 if(hisNum > historyInd || hisNum < 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: !%i: event not found\n", hisNum);
  write(1,tmp,strlen(tmp));
  free(tmp);
 } else {
  if(hisNum <= (int)(historyInd/2)) {
   int x = 0;
   struct HistoryList *cur = historyHead;
   for(;x<hisNum-1;++x) {
    cur = cur->next;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  } else {
   int x = 0;
   int y = historyInd-hisNum;
   struct HistoryList *cur = historyHead->prev;
   for(;x<y;++x) {
    cur = cur->prev;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  }
  char *tmp2 = (char*)malloc(sizeof(char) * 100);
  memset(tmp2,0,100);
  sprintf(tmp2,"%s\n",tmp);
  write(1,tmp2,strlen(tmp2));
  clear_args();
  parse_args(tmp);
  add_history(tmp);
  execute(args[0]);
  free(tmp);
 }
}

variable_types:
cmd
char *

variable_types: hisNum int

variable_types: y int


decompiled_code:
void __fastcall exec_nav_history(__int64 a1)
{
int v1; // eax
int v2; // er12
char *v3; // rbp
__int64 v4; // rcx
int v5; // er12
int i; // edx
const char *v7; // r12
size_t v8; // rax
size_t v9; // rax
int v10; // eax

v1 = atoi((const char *)(a1 + 1));
v2 = historyInd;
if ( *(_BYTE *)(a1 + 1) )
v2 = v1;
if ( !v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: @%s: not a valid event\n", (const char *)(a1 + 1));
goto LABEL_12;
}
v3 = (char *)calloc(0x64uLL, 1uLL);
if ( v2 < 0 || navHistoryInd < v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: @%i: event not found\n", (unsigned int)v2);
LABEL_12:
v9 = strlen(v3);
write(1, v3, v9);
goto LABEL_13;
}
v4 = navHistoryHead;
if ( navHistoryInd / 2 < v2 )
{
v4 = *(_QWORD *)(navHistoryHead + 16);
if ( navHistoryInd != v2 )
{
v10 = 0;
do
{
++v10;
v4 = *(_QWORD *)(v4 + 16);
}
while ( navHistoryInd - v2 != v10 );
}
}
else
{
v5 = v2 - 1;
if ( v5 )
{
for ( i = 0; i != v5; ++i )
v4 = *(_QWORD *)(v4 + 8);
}
}
v7 = *(const char **)v4;
v8 = strlen(*(const char **)v4);
strncpy(v3, v7, v8);
chdir(v3);
LABEL_13:
free(v3);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
int v1; // eax
int v2; // er12
char *v3; // rbp
__int64 v4; // rcx
int v5; // er12
int i; // edx
const char *v7; // r12
size_t v8; // rax
size_t v9; // rax
int v10; // eax

v1 = atoi((const char *)(a1 + 1));
v2 = historyInd;
if ( *(_BYTE *)(a1 + 1) )
v2 = v1;
if ( !v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: @%s: not a valid event\n", (const char *)(a1 + 1));
goto LABEL_12;
}
v3 = (char *)calloc(0x64uLL, 1uLL);
if ( v2 < 0 || navHistoryInd < v2 )
{
v3 = (char *)calloc(0x28uLL, 1uLL);
sprintf(v3, "-fritzbash: @%i: event not found\n", (unsigned int)v2);
LABEL_12:
v9 = strlen(v3);
write(1, v3, v9);
goto LABEL_13;
}
v4 = navHistoryHead;
if ( navHistoryInd / 2 < v2 )
{
v4 = *(_QWORD *)(navHistoryHead + 16);
if ( navHistoryInd != v2 )
{
v10 = 0;
do
{
++v10;
v4 = *(_QWORD *)(v4 + 16);
}
while ( navHistoryInd - v2 != v10 );
}
}
else
{
v5 = v2 - 1;
if ( v5 )
{
for ( i = 0; i != v5; ++i )
v4 = *(_QWORD *)(v4 + 8);
}
}
v7 = *(const char **)v4;
v8 = strlen(*(const char **)v4);
strncpy(v3, v7, v8);
chdir(v3);
LABEL_13:
free(v3);
}

original_code:
void exec_nav_history(char* cmd)
{
 ++cmd;
 int hisNum = atoi(cmd);
 if(strlen(cmd) == 0) { hisNum = historyInd; }
 if(hisNum == 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: @%s: not a valid event\n", cmd);
  write(1,tmp,strlen(tmp));
  free(tmp);
  return;
 }
 char *tmp = (char*)malloc(sizeof(char) * 100);
 memset(tmp,0,100);
 if(hisNum > navHistoryInd || hisNum < 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: @%i: event not found\n", hisNum);
  write(1,tmp,strlen(tmp));
  free(tmp);
 } else {
  if(hisNum <= (int)(navHistoryInd/2)) {
   int x = 0;
   struct HistoryList *cur = navHistoryHead;
   for(;x<hisNum-1;++x) {
    cur = cur->next;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  } else {
   int x = 0;
   int y = navHistoryInd-hisNum;
   struct HistoryList *cur = navHistoryHead->prev;
   for(;x<y;++x) {
    cur = cur->prev;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  }
  chdir(tmp);
  free(tmp);
 }
}

canonical_original_code:
void exec_nav_history(char *cmd)
{
 ++cmd;
 int hisNum = atoi(cmd);
 if(strlen(cmd) == 0) { hisNum = historyInd; }
 if(hisNum == 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: @%s: not a valid event\n", cmd);
  write(1,tmp,strlen(tmp));
  free(tmp);
  return;
 }
 char *tmp = (char*)malloc(sizeof(char) * 100);
 memset(tmp,0,100);
 if(hisNum > navHistoryInd || hisNum < 0) {
  char *tmp = (char*)malloc(sizeof(char) * 40);
  memset(tmp,0,40);
  sprintf(tmp, "-fritzbash: @%i: event not found\n", hisNum);
  write(1,tmp,strlen(tmp));
  free(tmp);
 } else {
  if(hisNum <= (int)(navHistoryInd/2)) {
   int x = 0;
   struct HistoryList *cur = navHistoryHead;
   for(;x<hisNum-1;++x) {
    cur = cur->next;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  } else {
   int x = 0;
   int y = navHistoryInd-hisNum;
   struct HistoryList *cur = navHistoryHead->prev;
   for(;x<y;++x) {
    cur = cur->prev;
   }
   strncpy(tmp,cur->data,strlen(cur->data));
  }
  chdir(tmp);
  free(tmp);
 }
}


decompiled_code:
void __fastcall check_redir()
{
int v0; // ebp
char *v1; // rbx
char *v2; // r12
char *v3; // rax
int v4; // ebx
char v5; // dl
char v6; // dl
__int64 v7; // rax
const char *v8; // rbp
char *v9; // rbp
size_t v10; // rax
__int64 v11; // rax
const char *v12; // rbp
char v13; // dl
char *v14; // rdx
const char *v15; // rsi
__int64 v16; // rbx
unsigned __int64 v17; // rdi
char *v18; // rbp
__int64 v19; // rax
const char *v20; // rbp

v1 = line;
if ( !strchr(line, 60) && !strchr(v1, 62) )
return;
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = args;
if ( !args )
{
*(_QWORD *)v1 = 0LL;
*(_QWORD *)(v1 + 92) = 0LL;
memset64(
(void *)((unsigned __int64)(v1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(unsigned __int64)v3,
((unsigned int)v1 - (((_DWORD)v1 + 8) & 0xFFFFFFF8) + 100) >> 3);
goto LABEL_28;
}
v4 = 0;
do
{
if ( *v3 == 62 )
{
v5 = v3[1];
if ( v5 )
{
if ( (unsigned __int8)((v5 & 0xDF) - 65) > 0x19u )
{
if ( v5 != 62 )
{
LABEL_10:
dup2(v0, 1);
close(v0);
goto LABEL_5;
}
v13 = v3[2];
if ( v13 )
{
if ( (unsigned __int8)((v13 & 0xDF) - 65) > 0x19u )
goto LABEL_10;
strcpy(v2, v3 + 2);
*(&args)[v4] = 0;
}
else
{
v19 = v4++;
*(&args)[v19] = 0;
v20 = (&args)[v4];
strcpy(v2, v20);
*v20 = 0;
}
v0 = open(v2, 1089, 484LL);
LABEL_16:
if ( v0 < 0 )
goto LABEL_17;
goto LABEL_10;
}
strcpy(v2, v3 + 1);
*(&args)[v4] = 0;
}
else
{
v7 = v4++;
*(&args)[v7] = 0;
v8 = (&args)[v4];
strcpy(v2, v8);
*v8 = 0;
}
v0 = open(v2, 65, 484LL);
goto LABEL_16;
}
if ( *v3 != 60 )
goto LABEL_5;
v6 = v3[1];
if ( !v6 )
{
v11 = v4++;
*(&args)[v11] = 0;
v12 = (&args)[v4];
strcpy(v2, v12);
*v12 = 0;
goto LABEL_20;
}
if ( (unsigned __int8)((v6 & 0xDF) - 65) <= 0x19u )
{
strcpy(v2, v3 + 1);
*(&args)[v4] = 0;
LABEL_20:
v0 = open(v2, 0);
if ( v0 < 0 )
{
LABEL_17:
v9 = (char *)calloc(0x64uLL, 1uLL);
sprintf(v9, "fash: %s: No such file or directory\n", v2);
v10 = strlen(v9);
write(1, v9, v10);
free(v9);
return;
}
}
close(0);
dup(v0);
close(v0);
LABEL_5:
v3 = (&args)[++v4];
}
while ( v3 );
v14 = line;
v15 = args;
v16 = (unsigned int)((_DWORD)&args + 8);
v17 = (unsigned __int64)(line + 8);
*(_QWORD *)line = 0LL;
*(_QWORD *)(v14 + 92) = 0LL;
memset((void *)(v17 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * (((unsigned int)v14 - (v17 & 0xFFFFFFF8) + 100) >> 3));
for ( ; v15; v15 = *(const char **)(v16 - 8) )
{
v16 += 8LL;
strcat(line, v15);
v18 = line;
*(_WORD *)&v18[strlen(line)] = 32;
}
LABEL_28:
clear_args();
parse_args(line);
free(v2);
}

canonical_decompiled_code:
void __fastcall func0()
{
int v0; // ebp
char *v1; // rbx
char *v2; // r12
char *v3; // rax
int v4; // ebx
char v5; // dl
char v6; // dl
__int64 v7; // rax
const char *v8; // rbp
char *v9; // rbp
size_t v10; // rax
__int64 v11; // rax
const char *v12; // rbp
char v13; // dl
char *v14; // rdx
const char *v15; // rsi
__int64 v16; // rbx
unsigned __int64 v17; // rdi
char *v18; // rbp
__int64 v19; // rax
const char *v20; // rbp

v1 = line;
if ( !strchr(line, 60) && !strchr(v1, 62) )
return;
v2 = (char *)calloc(0x64uLL, 1uLL);
v3 = args;
if ( !args )
{
*(_QWORD *)v1 = 0LL;
*(_QWORD *)(v1 + 92) = 0LL;
memset64(
(void *)((unsigned __int64)(v1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(unsigned __int64)v3,
((unsigned int)v1 - (((_DWORD)v1 + 8) & 0xFFFFFFF8) + 100) >> 3);
goto LABEL_28;
}
v4 = 0;
do
{
if ( *v3 == 62 )
{
v5 = v3[1];
if ( v5 )
{
if ( (unsigned __int8)((v5 & 0xDF) - 65) > 0x19u )
{
if ( v5 != 62 )
{
LABEL_10:
dup2(v0, 1);
close(v0);
goto LABEL_5;
}
v13 = v3[2];
if ( v13 )
{
if ( (unsigned __int8)((v13 & 0xDF) - 65) > 0x19u )
goto LABEL_10;
strcpy(v2, v3 + 2);
*(&args)[v4] = 0;
}
else
{
v19 = v4++;
*(&args)[v19] = 0;
v20 = (&args)[v4];
strcpy(v2, v20);
*v20 = 0;
}
v0 = open(v2, 1089, 484LL);
LABEL_16:
if ( v0 < 0 )
goto LABEL_17;
goto LABEL_10;
}
strcpy(v2, v3 + 1);
*(&args)[v4] = 0;
}
else
{
v7 = v4++;
*(&args)[v7] = 0;
v8 = (&args)[v4];
strcpy(v2, v8);
*v8 = 0;
}
v0 = open(v2, 65, 484LL);
goto LABEL_16;
}
if ( *v3 != 60 )
goto LABEL_5;
v6 = v3[1];
if ( !v6 )
{
v11 = v4++;
*(&args)[v11] = 0;
v12 = (&args)[v4];
strcpy(v2, v12);
*v12 = 0;
goto LABEL_20;
}
if ( (unsigned __int8)((v6 & 0xDF) - 65) <= 0x19u )
{
strcpy(v2, v3 + 1);
*(&args)[v4] = 0;
LABEL_20:
v0 = open(v2, 0);
if ( v0 < 0 )
{
LABEL_17:
v9 = (char *)calloc(0x64uLL, 1uLL);
sprintf(v9, "fash: %s: No such file or directory\n", v2);
v10 = strlen(v9);
write(1, v9, v10);
free(v9);
return;
}
}
close(0);
dup(v0);
close(v0);
LABEL_5:
v3 = (&args)[++v4];
}
while ( v3 );
v14 = line;
v15 = args;
v16 = (unsigned int)((_DWORD)&args + 8);
v17 = (unsigned __int64)(line + 8);
*(_QWORD *)line = 0LL;
*(_QWORD *)(v14 + 92) = 0LL;
memset((void *)(v17 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * (((unsigned int)v14 - (v17 & 0xFFFFFFF8) + 100) >> 3));
for ( ; v15; v15 = *(const char **)(v16 - 8) )
{
v16 += 8LL;
strcat(line, v15);
v18 = line;
*(_WORD *)&v18[strlen(line)] = 32;
}
LABEL_28:
clear_args();
parse_args(line);
free(v2);
}

variable_types: fd int

variable_types:
arg
char *

variable_types:
fName
char *

variable_types:
s
char *


decompiled_code:
__pid_t __fastcall pipe_execute(const char *a1, int a2)
{
int v2; // er12
int v3; // er13
__pid_t result; // eax
int v5; // er14
int v6; // er12

check_alias();
result = check_builtin(args);
if ( result )
{
if ( fork() )
return wait(0LL);
check_redir();
if ( a2 == 2 )
{
v3 = open("pipe", 0);
v2 = open("pipe_middle", 65, 484LL);
close(0);
dup(v3);
close(1);
dup(v2);
if ( execvp(args, &args) >= 0 )
goto LABEL_10;
}
else if ( a2 == 1 )
{
v6 = open("pipe", 65, 484LL);
dup2(v6, 1);
close(v6);
if ( execvp(args, &args) >= 0 )
{
close(1);
return close(v6);
}
}
else
{
v5 = open("pipe", 0);
close(0);
dup(v5);
close(v5);
if ( execvp(args, &args) >= 0 )
{
if ( !a2 )
{
close(0);
return close(v5);
}
LABEL_10:
close(0);
close(1);
close(v3);
close(v2);
return system("mv pipe_middle pipe");
}
}
printf("-fash: %s: command not found\n", a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func1(const char *a1, int a2)
{
int v2; // er12
int v3; // er13
__pid_t result; // eax
int v5; // er14
int v6; // er12

check_alias();
result = check_builtin(args);
if ( result )
{
if ( fork() )
return wait(0LL);
func0();
if ( a2 == 2 )
{
v3 = open("pipe", 0);
v2 = open("pipe_middle", 65, 484LL);
close(0);
dup(v3);
close(1);
dup(v2);
if ( execvp(args, &args) >= 0 )
goto LABEL_10;
}
else if ( a2 == 1 )
{
v6 = open("pipe", 65, 484LL);
dup2(v6, 1);
close(v6);
if ( execvp(args, &args) >= 0 )
{
close(1);
return close(v6);
}
}
else
{
v5 = open("pipe", 0);
close(0);
dup(v5);
close(v5);
if ( execvp(args, &args) >= 0 )
{
if ( !a2 )
{
close(0);
return close(v5);
}
LABEL_10:
close(0);
close(1);
close(v3);
close(v2);
return system("mv pipe_middle pipe");
}
}
printf("-fash: %s: command not found\n", a1);
exit(1);
}
return result;
}

original_code:
void pipe_execute(char *cmd, int readWrite)
{
 int i;
 check_alias(cmd);
 i = check_builtin(args[0]);
 if(!i) { return;}
 if(fork() == 0) {
  check_redir();
  int newfd, readfd, writefd;
  if(readWrite == 2){
   readfd = open("pipe", 00);
   writefd = open("pipe_middle", 01 | 0100, (0400|0200|0100) | (0400 >> 3) | ((0400 >> 3) >> 3));
   close(0);
   dup(readfd);
   close(1);
   dup(writefd);
  }else if (readWrite == 1){
   newfd = open("pipe", 01 | 0100, (0400|0200|0100) | (0400 >> 3) | ((0400 >> 3) >> 3));
   dup2(newfd,1);
   close(newfd);
  }else{
   newfd = open("pipe", 00);
   close(0);
   dup(newfd);
   close(newfd);
  }
  i = execvp(args[0], args);
  if(i < 0) {
   printf("-fash: %s: command not found\n", cmd);
   exit(1);
  }
  if(readWrite == 0){ close(0);close(newfd);}
  else if(readWrite ==1){close(1);close(newfd);}
  else{ close(0);close(1);close(readfd);close(writefd);system("mv pipe_middle pipe");}
 } else {wait(((void *)0));}
}

canonical_original_code:
void pipe_execute(char *cmd, int readWrite)
{
 int i;
 check_alias(cmd);
 i = check_builtin(args[0]);
 if(!i) { return;}
 if(fork() == 0) {
  check_redir();
  int newfd; int readfd; int writefd;
  if(readWrite == 2){
   readfd = open("pipe", 00);
   writefd = open("pipe_middle", 01 | 0100, (0400|0200|0100) | (0400 >> 3) | ((0400 >> 3) >> 3));
   close(0);
   dup(readfd);
   close(1);
   dup(writefd);
  }else if (readWrite == 1){
   newfd = open("pipe", 01 | 0100, (0400|0200|0100) | (0400 >> 3) | ((0400 >> 3) >> 3));
   dup2(newfd,1);
   close(newfd);
  }else{
   newfd = open("pipe", 00);
   close(0);
   dup(newfd);
   close(newfd);
  }
  i = execvp(args[0], args);
  if(i < 0) {
   printf("-fash: %s: command not found\n", cmd);
   exit(1);
  }
  if(readWrite == 0){ close(0);close(newfd);}
  else if(readWrite ==1){close(1);close(newfd);}
  else{ close(0);close(1);close(readfd);close(writefd);system("mv pipe_middle pipe");}
 } else {wait(((void *)0));}
}

variable_types: readWrite int

variable_types: newfd int

variable_types: readfd int

variable_types: writefd int


decompiled_code:
__int64 __fastcall check_pipe(char *s)
{
char *v1; // rbx
char *v2; // r8
__int64 result; // rax
int v4; // ebp
char *v5; // r14
__int64 v6; // rax
int v7; // edx
int v8; // ebp
char *v9; // r13
__int64 v10; // rcx
char v11; // al
char *v12; // rdi
__int64 v13; // rax
__int64 v14; // rbx
char *v15; // rax
__int64 v16[105]; // [rsp+0h] [rbp-348h]

v1 = s;
v2 = strchr(s, 124);
result = 0LL;
if ( v2 )
{
v4 = strlen(s);
v5 = (char *)calloc(0x64uLL, 1uLL);
if ( v4 <= 0 )
{
v8 = 0;
}
else
{
v6 = (unsigned int)(v4 - 1);
v7 = 0;
v8 = 0;
v9 = &s[v6 + 1];
do
{
while ( 1 )
{
v11 = *v1;
if ( *v1 == 124 )
break;
v10 = v7;
++v1;
++v7;
v5[v10] = v11;
if ( v1 == v9 )
goto LABEL_7;
}
++v1;
v12 = (char *)calloc(0x64uLL, 1uLL);
v13 = v8++;
v16[v13] = (__int64)v12;
strcpy(v12, v5);
memset(v5, 0, 0x60uLL);
v7 = 0;
*((_DWORD *)v5 + 24) = 0;
}
while ( v1 != v9 );
}
LABEL_7:
v14 = 0LL;
v15 = (char *)calloc(0x64uLL, 1uLL);
v16[v8] = (__int64)v15;
strcpy(v15, v5);
while ( 1 )
{
parse_args(v16[v14]);
if ( !v14 )
break;
if ( (_DWORD)v14 == v8 )
{
pipe_execute(args, 0);
LABEL_9:
++v14;
clear_args();
if ( v8 < (int)v14 )
goto LABEL_13;
}
else
{
++v14;
pipe_execute(args, 2);
clear_args();
if ( v8 < (int)v14 )
{
LABEL_13:
system("rm -f pipe");
system("rm -f pipe_middle");
return 1LL;
}
}
}
pipe_execute(args, 1);
goto LABEL_9;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s)
{
char *v1; // rbx
char *v2; // r8
__int64 result; // rax
int v4; // ebp
char *v5; // r14
__int64 v6; // rax
int v7; // edx
int v8; // ebp
char *v9; // r13
__int64 v10; // rcx
char v11; // al
char *v12; // rdi
__int64 v13; // rax
__int64 v14; // rbx
char *v15; // rax
__int64 v16[105]; // [rsp+0h] [rbp-348h]

v1 = s;
v2 = strchr(s, 124);
result = 0LL;
if ( v2 )
{
v4 = strlen(s);
v5 = (char *)calloc(0x64uLL, 1uLL);
if ( v4 <= 0 )
{
v8 = 0;
}
else
{
v6 = (unsigned int)(v4 - 1);
v7 = 0;
v8 = 0;
v9 = &s[v6 + 1];
do
{
while ( 1 )
{
v11 = *v1;
if ( *v1 == 124 )
break;
v10 = v7;
++v1;
++v7;
v5[v10] = v11;
if ( v1 == v9 )
goto LABEL_7;
}
++v1;
v12 = (char *)calloc(0x64uLL, 1uLL);
v13 = v8++;
v16[v13] = (__int64)v12;
strcpy(v12, v5);
memset(v5, 0, 0x60uLL);
v7 = 0;
*((_DWORD *)v5 + 24) = 0;
}
while ( v1 != v9 );
}
LABEL_7:
v14 = 0LL;
v15 = (char *)calloc(0x64uLL, 1uLL);
v16[v8] = (__int64)v15;
strcpy(v15, v5);
while ( 1 )
{
parse_args(v16[v14]);
if ( !v14 )
break;
if ( (_DWORD)v14 == v8 )
{
func1(args, 0);
LABEL_9:
++v14;
clear_args();
if ( v8 < (int)v14 )
goto LABEL_13;
}
else
{
++v14;
func1(args, 2);
clear_args();
if ( v8 < (int)v14 )
{
LABEL_13:
system("rm -f pipe");
system("rm -f pipe_middle");
return 1LL;
}
}
}
func1(args, 1);
goto LABEL_9;
}
return result;
}

original_code:
int check_pipe(char *line)
{
 if(!strchr(line,(int)'|')){ return 0;}
 char *pipes[100];
 int numPipes = 0;
 int i;
 int x = strlen(line);
 char *tmp = (char*)malloc(sizeof(char)*100);
 memset(tmp,0,100);
 int pos = 0;
 for(i = 0; i<x; ++i) {
  if(line[i] == '|') {
   strncat(tmp,"\0",1);
   pipes[numPipes] = (char *)malloc(sizeof(char)*100);
   memset(pipes[numPipes],0,100);
   strcpy(pipes[numPipes],tmp);
   memset(tmp,0,100);
   ++numPipes;
   pos = 0;
  } else {
   tmp[pos] = line[i];
   ++pos;
  }
 }
 strncat(tmp,"\0",1);
 pipes[numPipes] = (char *)malloc(sizeof(char)*100);
 memset(pipes[numPipes],0,100);
 strcpy(pipes[numPipes],tmp);
 for(i = 0; i<=numPipes;++i) {
  parse_args(pipes[i]);
  if(i == 0){
   pipe_execute(args[0], 1);
  }
  else if(i > 0 && i != numPipes) {
   pipe_execute(args[0], 2);
  }
  else if(i == numPipes) {
   pipe_execute(args[0], 0);
  }
  clear_args();
 }
  system("rm -f pipe");
  system("rm -f pipe_middle");
 return 1;
}

variable_types:
pipes
char *[100]

variable_types: numPipes int

variable_types: pos int


addvalue

repo: dfsmith/dfsmith-public

decompiled_code:
__int64 __fastcall addvalue(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rcx
unsigned int v4; // edx

result = sysstatus;
v3 = a1 + 4LL;
v4 = *(_DWORD *)(sysstatus + 4 * v3);
if ( a2 < 0 )
{
if ( -a2 <= v4 )
{
LABEL_4:
*(_DWORD *)(sysstatus + 4LL * a1 + 16) = v4 + a2;
return result;
}
*(_DWORD *)(sysstatus + 4 * v3) = 0;
}
else
{
if ( !a2 || 16484 - v4 > a2 )
goto LABEL_4;
*(_DWORD *)(sysstatus + 4 * v3) = 0x3FFF;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rcx
unsigned int v4; // edx

result = sysstatus;
v3 = a1 + 4LL;
v4 = *(_DWORD *)(sysstatus + 4 * v3);
if ( a2 < 0 )
{
if ( -a2 <= v4 )
{
LABEL_4:
*(_DWORD *)(sysstatus + 4LL * a1 + 16) = v4 + a2;
return result;
}
*(_DWORD *)(sysstatus + 4 * v3) = 0;
}
else
{
if ( !a2 || 16484 - v4 > a2 )
goto LABEL_4;
*(_DWORD *)(sysstatus + 4 * v3) = 0x3FFF;
}
return result;
}

original_code:
static void addvalue(int index,int amount) {
 uint *v;
 v=&sysstatus->set.data[index];
 if (amount<0 && -amount>*v) {*v=0; return;}
 if (amount>0 && amount>=16484-*v) {*v=16383; return;}
 *v+=amount;
}

canonical_original_code:
static void addvalue(int index, int amount) {
 unsigned int *v;
 v=&sysstatus->set.data[index];
 if (amount<0 && -amount>*v) {*v=0; return;}
 if (amount>0 && amount>=16484-*v) {*v=16383; return;}
 *v+=amount;
}

variable_types: index int

variable_types: amount int

variable_types:
v
unsigned int *


main

repo: dgkimura/Arbitrator

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init_services(argc, argv, envp);
init_prompt();
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
    init_services();
    init_prompt();
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int algorithm_leastconnections()
{
return puts("IMPLEMENT: least connections.");
}

canonical_decompiled_code:
int func0()
{
return puts("IMPLEMENT: least connections.");
}

original_code:
node_t *
algorithm_leastconnections(const char *source_ip)
{
    printf("IMPLEMENT: least connections.\n");
}

canonical_original_code:
struct Node *algorithm_leastconnections(char *source_ip)
{
    printf("IMPLEMENT: least connections.\n");
}

return_type: struct Node { int ip; unsigned int rtt; struct Node *next; } *


decompiled_code:
int init_leastconnections()
{
return puts("IMPLEMENT: init least connections.");
}

canonical_decompiled_code:
int func1()
{
return puts("IMPLEMENT: init least connections.");
}

original_code:
int
init_leastconnections()
{
    printf("IMPLEMENT: init least connections.\n");
}


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

_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*v1 = 0LL;
v1[1] = 0LL;
}

original_code:
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = (void *) 0;
    entry->prev = (void *) 0;
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
void sig_sigpipe()
{
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 33LL);
fwrite("ENCOUNTERED SIGNAL SIGPIPE!!!\n", 1uLL, 0x1EuLL, stream);
fputc(10, stream);
}
}

canonical_decompiled_code:
void func2()
{
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 33LL);
fwrite("ENCOUNTERED SIGNAL SIGPIPE!!!\n", 1uLL, 0x1EuLL, stream);
fputc(10, stream);
}
}

original_code:
void
sig_sigpipe(int signo)
{
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 33); fprintf(stream, "ENCOUNTERED SIGNAL SIGPIPE!!!\n"); fprintf(stream, "\n"); } } while(0);
}

variable_types: signo int