decompiled_code:
int erreur_fatale(__int64 a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
return vfprintf(stderr, a2, arg);
}

canonical_decompiled_code:
int func19(__int64 a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
return vfprintf(stderr, a2, arg);
}

original_code:
void erreur_fatale(void *user_data, const char *msg, ...) {
    va_list args;
    __builtin_va_start(args,msg);
    vfprintf(stderr, msg, args);
    __builtin_va_end(args);
}

canonical_original_code:
void erreur_fatale(void *user_data, char *msg, ...) {
    __builtin_va_list args;
    __builtin_va_start(args,msg);
    vfprintf(stderr, msg, args);
    __builtin_va_end(args);
}


decompiled_code:
int debut_document()
{
return puts(aD);
}

canonical_decompiled_code:
int func7()
{
return puts(aD);
}

original_code:
void debut_document(void *user_data) {
    printf("Début du document\n");
}

return_type: void


decompiled_code:
int __fastcall debut_element(__int64 a1, __int64 a2, const char **a3)
{
int result; // eax
const char *v5; // rsi
const char **v6; // rbx
const char *v7; // rdx

result = printf(format);
if ( a3 )
{
v5 = *a3;
if ( *a3 )
{
v6 = a3 + 1;
do
{
v7 = *v6;
v6 += 2;
result = printf("Attribut '%s' a pour valeur %s\n", v5, v7);
v5 = *(v6 - 1);
}
while ( v5 );
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, __int64 a2, const char **a3)
{
int result; // eax
const char *v5; // rsi
const char **v6; // rbx
const char *v7; // rdx

result = printf(format);
if ( a3 )
{
v5 = *a3;
if ( *a3 )
{
v6 = a3 + 1;
do
{
v7 = *v6;
v6 += 2;
result = printf("Attribut '%s' a pour valeur %s\n", v5, v7);
v5 = *(v6 - 1);
}
while ( v5 );
}
}
return result;
}

original_code:
void debut_element(void *user_data, const xmlChar *name, const xmlChar **attrs) {
    printf("Début de l'élément : %s\n", name);
    if (((void *)0) != attrs) {
        int i;
        for (i = 0; attrs[i] != ((void *)0); i+=2) {
            printf("Attribut '%s' a pour valeur %s\n", attrs[i], attrs[i+1]);
        }
    }
}

canonical_original_code:
void debut_element(void *user_data, unsigned char *name, unsigned char **attrs) {
    printf("Début de l'élément : %s\n", name);
    if (((void *)0) != attrs) {
        int i;
        for (i = 0; attrs[i] != ((void *)0); i+=2) {
            printf("Attribut '%s' a pour valeur %s\n", attrs[i], attrs[i+1]);
        }
    }
}

variable_types:
name
unsigned char *

variable_types:
attrs
unsigned char **

variable_types: i int


decompiled_code:
int fin_element()
{
return printf(aFinDeL);
}

canonical_decompiled_code:
int func10()
{
return printf(aFinDeL);
}

original_code:
void fin_element(void *user_data, const xmlChar *name) {
    printf("Fin de l'élément : %s\n", name);
}

canonical_original_code:
void fin_element(void *user_data, unsigned char *name) {
    printf("Fin de l'élément : %s\n", name);
}


caracteres

decompiled_code:
int __fastcall caracteres(__int64 a1, const char *a2, unsigned int a3)
{
return printf("Element texte : (longueur = %d) %.*s\n", a3, a3, a2);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, const char *a2, unsigned int a3)
{
return printf("Element texte : (longueur = %d) %.*s\n", a3, a3, a2);
}

original_code:
void caracteres(void *user_data, const xmlChar *ch, int len) {
    printf("Element texte : (longueur = %d) %.*s\n", len, len, ch);
}

canonical_original_code:
void caracteres(void *user_data, unsigned char *ch, int len) {
    printf("Element texte : (longueur = %d) %.*s\n", len, len, ch);
}

variable_types:
ch
unsigned char *

variable_types: len int


decompiled_code:
ssize_t __fastcall sax_read(int *a1, void *a2, int a3)
{
return read(*a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int *a1, void *a2, int a3)
{
return read(*a1, a2, a3);
}

original_code:
int sax_read(void *ctxt, char *buf, int len) {
    int fd = *((int *) ctxt);
    return read(fd, buf, len);
}

variable_types:
ctxt
void *

variable_types:
buf
char *

variable_types: fd int

return_type: int


decompiled_code:
int __fastcall sax_close(int *a1)
{
int v1; // edi
int result; // eax

v1 = *a1;
if ( v1 > 2 )
result = close(v1);
else
result = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func9(int *a1)
{
int v1; // edi
int result; // eax

v1 = *a1;
if ( v1 > 2 )
result = close(v1);
else
result = 0;
return result;
}

original_code:
int sax_close(void *ctxt) {
    int fd = *((int *) ctxt);
    if (fd > 2) {
        return close(fd);
    }
    return 0;
}


decompiled_code:
int debut_element()
{
return printf(format);
}

canonical_decompiled_code:
int func7()
{
return printf(format);
}

original_code:
void debut_element(void *user_data, const xmlChar *name, const xmlChar **attrs) {
    printf("Début de l'élément : %s\n", name);
}

canonical_original_code:
void debut_element(void *user_data, unsigned char *name, unsigned char **attrs) {
    printf("Début de l'élément : %s\n", name);
}


asprintf

decompiled_code:
int asprintf(void **a1, const char *a2, ...)
{
void *v3; // rax
int v4; // ebx
size_t v5; // r13
void *v6; // rax
char *v7; // rdi
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

v3 = malloc(0x80uLL);
*a1 = v3;
if ( !v3 )
return -1;
v4 = 128;
v5 = 128LL;
while ( 1 )
{
v7 = (char *)*a1;
va_start(arg, a2);
result = vsnprintf(v7, v5, a2, arg);
if ( v4 > (unsigned int)result )
break;
v4 *= 2;
if ( result >= 0 )
v4 = result + 1;
v5 = v4;
v6 = realloc(*a1, v4);
if ( !v6 )
{
free(*a1);
return -1;
}
*a1 = v6;
}
return result;
}

canonical_decompiled_code:
int func7(void **a1, const char *a2, ...)
{
void *v3; // rax
int v4; // ebx
size_t v5; // r13
void *v6; // rax
char *v7; // rdi
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

v3 = malloc(0x80uLL);
*a1 = v3;
if ( !v3 )
return -1;
v4 = 128;
v5 = 128LL;
while ( 1 )
{
v7 = (char *)*a1;
va_start(arg, a2);
result = vsnprintf(v7, v5, a2, arg);
if ( v4 > (unsigned int)result )
break;
v4 *= 2;
if ( result >= 0 )
v4 = result + 1;
v5 = v4;
v6 = realloc(*a1, v4);
if ( !v6 )
{
free(*a1);
return -1;
}
*a1 = v6;
}
return result;
}

original_code:
int asprintf(char **path, const char *format, ...) {
    int n, size = 128;
    char *np;
    va_list ap;
    if (((void *)0) == (*path = malloc(size))) {
        return -1;
    }
    while (1) {
        __builtin_va_start(ap,format);
        n = vsnprintf(*path, size, format, ap);
        __builtin_va_end(ap);
        if (n > -1 && n < size) {
            return n;
        }
        if (n > -1) {
            size = n + 1;
        } else {
            size *= 2;
        }
        if (((void *)0) == (np = realloc(*path, size))) {
            free(*path);
            return -1;
        } else {
            *path = np;
        }
    }
}

canonical_original_code:
int asprintf(char **path, char *format, ...) {
    int n; int size = 128;
    char *np;
    __builtin_va_list ap;
    if (((void *)0) == (*path = malloc(size))) {
        return -1;
    }
    while (1) {
        __builtin_va_start(ap,format);
        n = vsnprintf(*path, size, format, ap);
        __builtin_va_end(ap);
        if (n > -1 && n < size) {
            return n;
        }
        if (n > -1) {
            size = n + 1;
        } else {
            size *= 2;
        }
        if (((void *)0) == (np = realloc(*path, size))) {
            free(*path);
            return -1;
        } else {
            *path = np;
        }
    }
}

variable_types:
path
char **

variable_types:
format
char *

variable_types: n int

variable_types: size int

variable_types:
np
char *


main

canonical_name: main

repo: jumanjiman/hangwatch

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // er14
int v5; // er13
int v6; // er12
int v7; // eax
int v8; // eax
int v9; // ebp
int v10; // er14
unsigned int v11; // er12
char *v12; // rax
size_t i; // rbx
__pid_t v14; // eax
int v15; // edi
int *v16; // rax
int *v17; // rax
int result; // eax
double v19; // [rsp+0h] [rbp-240h]
char dest[8]; // [rsp+8h] [rbp-238h] BYREF
__int64 v21; // [rsp+10h] [rbp-230h]
__int128 v22; // [rsp+18h] [rbp-228h]
__int128 v23; // [rsp+28h] [rbp-218h]
__int128 v24; // [rsp+38h] [rbp-208h]
__int128 v25; // [rsp+48h] [rbp-1F8h]
__int128 v26; // [rsp+58h] [rbp-1E8h]
__int128 v27; // [rsp+68h] [rbp-1D8h]
__int128 v28; // [rsp+78h] [rbp-1C8h]
__int128 v29; // [rsp+88h] [rbp-1B8h]
__int128 v30; // [rsp+98h] [rbp-1A8h]
__int128 v31; // [rsp+A8h] [rbp-198h]
__int128 v32; // [rsp+B8h] [rbp-188h]
__int128 v33; // [rsp+C8h] [rbp-178h]
__int128 v34; // [rsp+D8h] [rbp-168h]
__int128 v35; // [rsp+E8h] [rbp-158h]
__int128 v36; // [rsp+F8h] [rbp-148h]
char buf[312]; // [rsp+108h] [rbp-138h] BYREF

v3 = 0;
v4 = 1;
v5 = 0;
v6 = 1;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
v28 = 0LL;
v29 = 0LL;
v30 = 0LL;
v31 = 0LL;
v32 = 0LL;
v33 = 0LL;
v34 = 0LL;
v35 = 0LL;
v36 = 0LL;
*(_QWORD *)dest = 7630957LL;
v21 = 0LL;
signal(1, signal_handler);
signal(15, signal_handler);
signal(2, signal_handler);
signal(3, signal_handler);
v19 = 5.0;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "i:s:t:hnlm|help");
if ( v7 != -1 )
{
switch ( v7 )
{
case 'i':
v6 = atoi(_bss_start);
if ( v6 <= 0 )
goto LABEL_5;
continue;
case 'l':
v5 = 1;
continue;
case 'm':
v3 = 1;
continue;
case 'n':
v4 = 0;
continue;
case 's':
strncpy(dest, _bss_start, 0x100uLL);
continue;
case 't':
v19 = atof(_bss_start);
if ( v19 <= 0.0 )
goto LABEL_5;
continue;
default:
LABEL_5:
usage(*argv);
return result;
}
}
break;
}
syslog(6, "%s daemon started.", "hangwatch");
if ( v4 )
{
v14 = fork();
if ( v14 < 0 )
goto LABEL_35;
v15 = 0;
if ( v14 )
goto LABEL_36;
umask(0);
if ( setsid() < 0 )
goto LABEL_35;
close(0);
close(1);
close(2);
}
v8 = open("/proc/sys/kernel/sysrq", 2);
v9 = v8;
if ( v8 == -1 )
{
perror("Can't open /proc/sys/kernel/sysrq");
v16 = __errno_location();
exit(*v16);
}
if ( (int)read(v8, buf, 0x100uLL) <= 0 )
{
perror("Can't read from /proc/sys/kernel/sysrq");
}
else if ( buf[0] == 48 && (puts("SysRq not enabled. Enabling..."), (int)write(v9, "1", 1uLL) <= 0) )
{
perror("Can't write to /proc/sys/kernel/sysrq");
}
else
{
close(v9);
v9 = open("/proc/sysrq-trigger", 1);
if ( v9 == -1 )
{
perror("Can't open /proc/sysrq-trigger");
v17 = __errno_location();
exit(*v17);
}
v10 = open("/proc/loadavg", 0);
if ( v10 == -1 )
{
perror("Can't open /proc/loadavg");
}
else
{
v11 = 60 * v6;
puts("Monitoring...");
while ( (int)read(v10, buf, 0x100uLL) > 0 )
{
lseek(v10, 0LL, 0);
v12 = strchr(buf, 32);
if ( !v12 )
{
puts("Unable to parse /proc/loadavg");
close(v10);
close(v9);
LABEL_35:
v15 = 1;
LABEL_36:
exit(v15);
}
*v12 = 0;
if ( atof(buf) > v19 )
{
for ( i = 0LL; strlen(dest) > i; ++i )
{
write(v9, &dest[i], 1uLL);
if ( v5 == 1 )
procinfo("/proc/slabinfo");
if ( v3 == 1 )
procinfo("/proc/meminfo");
sleep(1u);
}
puts(" Load exceeded threshold. Triggers activated.");
}
sleep(v11);
}
perror("Can't read from /proc/loadavg");
close(v10);
}
}
close(v9);
v15 = *__errno_location();
goto LABEL_36;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // er14
int v5; // er13
int v6; // er12
int v7; // eax
int v8; // eax
int v9; // ebp
int v10; // er14
unsigned int v11; // er12
char *v12; // rax
size_t i; // rbx
__pid_t v14; // eax
int v15; // edi
int *v16; // rax
int *v17; // rax
int result; // eax
double v19; // [rsp+0h] [rbp-240h]
char dest[8]; // [rsp+8h] [rbp-238h] BYREF
__int64 v21; // [rsp+10h] [rbp-230h]
__int128 v22; // [rsp+18h] [rbp-228h]
__int128 v23; // [rsp+28h] [rbp-218h]
__int128 v24; // [rsp+38h] [rbp-208h]
__int128 v25; // [rsp+48h] [rbp-1F8h]
__int128 v26; // [rsp+58h] [rbp-1E8h]
__int128 v27; // [rsp+68h] [rbp-1D8h]
__int128 v28; // [rsp+78h] [rbp-1C8h]
__int128 v29; // [rsp+88h] [rbp-1B8h]
__int128 v30; // [rsp+98h] [rbp-1A8h]
__int128 v31; // [rsp+A8h] [rbp-198h]
__int128 v32; // [rsp+B8h] [rbp-188h]
__int128 v33; // [rsp+C8h] [rbp-178h]
__int128 v34; // [rsp+D8h] [rbp-168h]
__int128 v35; // [rsp+E8h] [rbp-158h]
__int128 v36; // [rsp+F8h] [rbp-148h]
char buf[312]; // [rsp+108h] [rbp-138h] BYREF

v3 = 0;
v4 = 1;
v5 = 0;
v6 = 1;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
v28 = 0LL;
v29 = 0LL;
v30 = 0LL;
v31 = 0LL;
v32 = 0LL;
v33 = 0LL;
v34 = 0LL;
v35 = 0LL;
v36 = 0LL;
*(_QWORD *)dest = 7630957LL;
v21 = 0LL;
signal(1, signal_handler);
signal(15, signal_handler);
signal(2, signal_handler);
signal(3, signal_handler);
v19 = 5.0;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "i:s:t:hnlm|help");
if ( v7 != -1 )
{
switch ( v7 )
{
case 'i':
v6 = atoi(_bss_start);
if ( v6 <= 0 )
goto LABEL_5;
continue;
case 'l':
v5 = 1;
continue;
case 'm':
v3 = 1;
continue;
case 'n':
v4 = 0;
continue;
case 's':
strncpy(dest, _bss_start, 0x100uLL);
continue;
case 't':
v19 = atof(_bss_start);
if ( v19 <= 0.0 )
goto LABEL_5;
continue;
default:
LABEL_5:
func9(*argv);
return result;
}
}
break;
}
syslog(6, "%s daemon started.", "hangwatch");
if ( v4 )
{
v14 = fork();
if ( v14 < 0 )
goto LABEL_35;
v15 = 0;
if ( v14 )
goto LABEL_36;
umask(0);
if ( setsid() < 0 )
goto LABEL_35;
close(0);
close(1);
close(2);
}
v8 = open("/proc/sys/kernel/sysrq", 2);
v9 = v8;
if ( v8 == -1 )
{
perror("Can't open /proc/sys/kernel/sysrq");
v16 = __errno_location();
exit(*v16);
}
if ( (int)read(v8, buf, 0x100uLL) <= 0 )
{
perror("Can't read from /proc/sys/kernel/sysrq");
}
else if ( buf[0] == 48 && (puts("SysRq not enabled. Enabling..."), (int)write(v9, "1", 1uLL) <= 0) )
{
perror("Can't write to /proc/sys/kernel/sysrq");
}
else
{
close(v9);
v9 = open("/proc/sysrq-trigger", 1);
if ( v9 == -1 )
{
perror("Can't open /proc/sysrq-trigger");
v17 = __errno_location();
exit(*v17);
}
v10 = open("/proc/loadavg", 0);
if ( v10 == -1 )
{
perror("Can't open /proc/loadavg");
}
else
{
v11 = 60 * v6;
puts("Monitoring...");
while ( (int)read(v10, buf, 0x100uLL) > 0 )
{
lseek(v10, 0LL, 0);
v12 = strchr(buf, 32);
if ( !v12 )
{
puts("Unable to parse /proc/loadavg");
close(v10);
close(v9);
LABEL_35:
v15 = 1;
LABEL_36:
exit(v15);
}
*v12 = 0;
if ( atof(buf) > v19 )
{
for ( i = 0LL; strlen(dest) > i; ++i )
{
write(v9, &dest[i], 1uLL);
if ( v5 == 1 )
func8("/proc/slabinfo");
if ( v3 == 1 )
func8("/proc/meminfo");
sleep(1u);
}
puts(" Load exceeded threshold. Triggers activated.");
}
sleep(v11);
}
perror("Can't read from /proc/loadavg");
close(v10);
}
}
close(v9);
v15 = *__errno_location();
goto LABEL_36;
}

original_code:
int main(int argc, char **argv)
{
    int daemonize = 1;
    int slab = 0;
    int mem = 0;
    int interval = 1;
    char triggers[256] = "mpt";
    double threshold = 5.0;
    int c;
    signal(1, signal_handler);
    signal(15, signal_handler);
    signal(2, signal_handler);
    signal(3, signal_handler);
    while ( (c = getopt(argc, argv, "i:s:t:hnlm|help")) != -1) {
        switch (c) {
        case 'h':
            usage(argv[0]);
            break;
        case 'i':
            interval = atoi(optarg);
            if (interval < 1)
                usage(argv[0]);
            break;
        case 's':
            strncpy(triggers, optarg, 256);
            break;
        case 't':
            threshold = atof(optarg);
            if (!(threshold > 0.0))
                usage(argv[0]);
            break;
        case 'l':
            slab = 1;
            break;
        case 'm':
            mem = 1;
            break;
        case 'n':
            daemonize = 0;
            break;
        default:
            usage(argv[0]);
            break;
        }
    }
    syslog(6, "%s daemon started.", "hangwatch");
    pid_t pid, sid;
    if(daemonize) {
        pid = fork();
        if (pid < 0) {
            exit(1);
        }
        if(pid > 0) {
            exit(0);
        }
        umask(0);
        sid = setsid();
        if(sid < 0) {
            exit(1);
        }
        close(0);
        close(1);
        close(2);
    }
    char procbuf[256];
    int sysrqfd, loadfd, rc;
    sysrqfd = open("/proc/sys/kernel/sysrq", 02);
    if (sysrqfd == -1) {
        perror("Can't open /proc/sys/kernel/sysrq");
        exit((*__errno_location ()));
    }
    rc = read(sysrqfd, procbuf, 256);
    if (rc < 1) {
        perror("Can't read from /proc/sys/kernel/sysrq");
        close(sysrqfd);
        exit((*__errno_location ()));
    }
    if (procbuf[0] == '0') {
        printf("SysRq not enabled.  Enabling...\n");
        rc = write(sysrqfd, "1", 1);
        if (rc < 1) {
            perror("Can't write to /proc/sys/kernel/sysrq");
            close(sysrqfd);
            exit((*__errno_location ()));
        }
    }
    close(sysrqfd);
    sysrqfd = open("/proc/sysrq-trigger", 01);
    if (sysrqfd == -1) {
        perror("Can't open /proc/sysrq-trigger");
        exit((*__errno_location ()));
    }
    loadfd = open("/proc/loadavg", 00);
    if (loadfd == -1) {
        perror("Can't open /proc/loadavg");
        close(sysrqfd);
        exit((*__errno_location ()));
    }
    printf("Monitoring...\n");
    char *index;
    double load;
    size_t i;
    while(1) {
        rc = read(loadfd, procbuf, 256);
        if (rc < 1) {
            perror("Can't read from /proc/loadavg");
            close(loadfd);
            close(sysrqfd);
            exit((*__errno_location ()));
        }
        lseek(loadfd, 0, 0);
        index = strchr(procbuf, ' ');
        if (index == ((void *)0)) {
            printf("Unable to parse /proc/loadavg\n");
            close(loadfd);
            close(sysrqfd);
            exit(1);
        }
        *index = '\0';
        load = atof(procbuf);
        if (load > threshold) {
            for (i = 0; i < strlen(triggers); i++) {
                write(sysrqfd, triggers + i, 1);
                if (slab == 1) { procinfo("/proc/slabinfo"); }
                if (mem == 1) { procinfo("/proc/meminfo"); }
                sleep(1);
            }
            printf(" Load exceeded threshold.  Triggers activated.\n");
        }
        sleep(60 * interval);
    }
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    int daemonize = 1;
    int slab = 0;
    int mem = 0;
    int interval = 1;
    char triggers[256] = "mpt";
    double threshold = 5.0;
    int c;
    signal(1, signal_handler);
    signal(15, signal_handler);
    signal(2, signal_handler);
    signal(3, signal_handler);
    while ( (c = getopt(argc, argv, "i:s:t:hnlm|help")) != -1) {
        switch (c) {
        case 'h':
            usage(argv[0]);
            break;
        case 'i':
            interval = atoi(optarg);
            if (interval < 1)
                usage(argv[0]);
            break;
        case 's':
            strncpy(triggers, optarg, 256);
            break;
        case 't':
            threshold = atof(optarg);
            if (!(threshold > 0.0))
                usage(argv[0]);
            break;
        case 'l':
            slab = 1;
            break;
        case 'm':
            mem = 1;
            break;
        case 'n':
            daemonize = 0;
            break;
        default:
            usage(argv[0]);
            break;
        }
    }
    syslog(6, "%s daemon started.", "hangwatch");
    int pid; int sid;
    if(daemonize) {
        pid = fork();
        if (pid < 0) {
            exit(1);
        }
        if(pid > 0) {
            exit(0);
        }
        umask(0);
        sid = setsid();
        if(sid < 0) {
            exit(1);
        }
        close(0);
        close(1);
        close(2);
    }
    char procbuf[256];
    int sysrqfd; int loadfd; int rc;
    sysrqfd = open("/proc/sys/kernel/sysrq", 02);
    if (sysrqfd == -1) {
        perror("Can't open /proc/sys/kernel/sysrq");
        exit((*__errno_location ()));
    }
    rc = read(sysrqfd, procbuf, 256);
    if (rc < 1) {
        perror("Can't read from /proc/sys/kernel/sysrq");
        close(sysrqfd);
        exit((*__errno_location ()));
    }
    if (procbuf[0] == '0') {
        printf("SysRq not enabled.  Enabling...\n");
        rc = write(sysrqfd, "1", 1);
        if (rc < 1) {
            perror("Can't write to /proc/sys/kernel/sysrq");
            close(sysrqfd);
            exit((*__errno_location ()));
        }
    }
    close(sysrqfd);
    sysrqfd = open("/proc/sysrq-trigger", 01);
    if (sysrqfd == -1) {
        perror("Can't open /proc/sysrq-trigger");
        exit((*__errno_location ()));
    }
    loadfd = open("/proc/loadavg", 00);
    if (loadfd == -1) {
        perror("Can't open /proc/loadavg");
        close(sysrqfd);
        exit((*__errno_location ()));
    }
    printf("Monitoring...\n");
    char *index;
    double load;
    unsigned long i;
    while(1) {
        rc = read(loadfd, procbuf, 256);
        if (rc < 1) {
            perror("Can't read from /proc/loadavg");
            close(loadfd);
            close(sysrqfd);
            exit((*__errno_location ()));
        }
        lseek(loadfd, 0, 0);
        index = strchr(procbuf, ' ');
        if (index == ((void *)0)) {
            printf("Unable to parse /proc/loadavg\n");
            close(loadfd);
            close(sysrqfd);
            exit(1);
        }
        *index = '\0';
        load = atof(procbuf);
        if (load > threshold) {
            for (i = 0; i < strlen(triggers); i++) {
                write(sysrqfd, triggers + i, 1);
                if (slab == 1) { procinfo("/proc/slabinfo"); }
                if (mem == 1) { procinfo("/proc/meminfo"); }
                sleep(1);
            }
            printf(" Load exceeded threshold.  Triggers activated.\n");
        }
        sleep(60 * interval);
    }
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: daemonize int

variable_types: slab int

variable_types: mem int

variable_types: interval int

variable_types:
triggers
char [256]

variable_types: threshold double

variable_types: c int

variable_types: pid int

variable_types: sid int

variable_types:
procbuf
char [256]

variable_types: sysrqfd int

variable_types: loadfd int

variable_types: rc int

variable_types:
index
char *

variable_types: load double

variable_types:
i
unsigned long


decompiled_code:
void __fastcall __noreturn signal_handler(int a1)
{
const char *v1; // rsi

v1 = "Received SIGHUP signal.";
if ( a1 != 1 )
{
v1 = "Received SIGTERM signal.";
if ( a1 != 15 )
v1 = "Unhandled signal";
}
syslog(4, v1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(int a1)
{
const char *v1; // rsi

v1 = "Received SIGHUP signal.";
if ( a1 != 1 )
{
v1 = "Received SIGTERM signal.";
if ( a1 != 15 )
v1 = "Unhandled signal";
}
syslog(4, v1);
exit(0);
}

original_code:
void signal_handler(int sig)
{
    switch(sig) {
    case 1:
        syslog(4, "Received SIGHUP signal.");
        exit(0);
        break;
    case 15:
        syslog(4, "Received SIGTERM signal.");
        exit(0);
        break;
    default:
        syslog(4, "Unhandled signal");
        exit(0);
        break;
    }
}

variable_types: sig int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
puts("hangwatch (C) 2006-2007 Red Hat, Inc.");
puts("Distributed under version 2 of the GNU General Public License.\n");
printf("Usage: %s -h -t <#> -s <mpt> -i <#>\n", a1);
puts("\tOptions:");
puts("\t\t-h This help screen.");
puts("\t\t-s \"mpt\" sysrq triggers.");
puts("\t\t-t # Load threshold.");
puts("\t\t-i # Time interval in min(s).");
puts("\t\t-n # Run application in foreground.");
puts("\t\t-l # Print Slabinfo.");
puts("\t\t-m # Print Meminfo.");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
puts("hangwatch (C) 2006-2007 Red Hat, Inc.");
puts("Distributed under version 2 of the GNU General Public License.\n");
printf("Usage: %s -h -t <#> -s <mpt> -i <#>\n", a1);
puts("\tOptions:");
puts("\t\t-h This help screen.");
puts("\t\t-s \"mpt\" sysrq triggers.");
puts("\t\t-t # Load threshold.");
puts("\t\t-i # Time interval in min(s).");
puts("\t\t-n # Run application in foreground.");
puts("\t\t-l # Print Slabinfo.");
puts("\t\t-m # Print Meminfo.");
exit(1);
}

original_code:
void usage(const char *progname)
{
    printf("hangwatch (C) 2006-2007 Red Hat, Inc.\n");
    printf("Distributed under version 2 of " "the GNU General Public License.\n\n");
    printf("Usage: %s -h -t <#> -s <mpt> -i <#>\n", progname);
    printf("\tOptions:\n");
    printf("\t\t-h This help screen.\n");
    printf("\t\t-s \"mpt\" sysrq triggers.\n");
    printf("\t\t-t # Load threshold.\n");
    printf("\t\t-i # Time interval in min(s).\n");
    printf("\t\t-n # Run application in foreground.\n");
    printf("\t\t-l # Print Slabinfo.\n");
    printf("\t\t-m # Print Meminfo.\n");
    exit(1);
}

canonical_original_code:
void usage(char *progname)
{
    printf("hangwatch (C) 2006-2007 Red Hat, Inc.\n");
    printf("Distributed under version 2 of " "the GNU General Public License.\n\n");
    printf("Usage: %s -h -t <#> -s <mpt> -i <#>\n", progname);
    printf("\tOptions:\n");
    printf("\t\t-h This help screen.\n");
    printf("\t\t-s \"mpt\" sysrq triggers.\n");
    printf("\t\t-t # Load threshold.\n");
    printf("\t\t-i # Time interval in min(s).\n");
    printf("\t\t-n # Run application in foreground.\n");
    printf("\t\t-l # Print Slabinfo.\n");
    printf("\t\t-m # Print Meminfo.\n");
    exit(1);
}

variable_types:
progname
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm0_8
__int64 v5; // rbp
__int64 v6; // rbx
double *v7; // rbx
double v8; // xmm0_8
double v10; // [rsp+0h] [rbp-28h]
double v11; // [rsp+0h] [rbp-28h]
double v12; // [rsp+8h] [rbp-20h]

if ( argc <= 1 )
{
printf("rescale_power <P(k) file name> <sigma8>");
}
else
{
v10 = atof(argv[2]);
read_power_table_camb(argv[1]);
v3 = TopHatSigma2(8.0);
v12 = v10 * v10 / v3 * 19.73920880217872;
if ( v3 < 0.0 )
v4 = sqrt(v3);
else
v4 = sqrt(v3);
fprintf(stderr, "Input power spectrum sigma8 %f\n", v4);
if ( NPowerTable > 0 )
{
v5 = 0LL;
do
{
v6 = v5++;
v7 = (double *)(PowerTable + 16 * v6);
v11 = pow(10.0, *v7);
v8 = pow(10.0, v7[1]);
printf("%e %e\n", v11, v12 * v8 / (v11 * v11 * v11));
}
while ( NPowerTable > (int)v5 );
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm0_8
__int64 v5; // rbp
__int64 v6; // rbx
double *v7; // rbx
double v8; // xmm0_8
double v10; // [rsp+0h] [rbp-28h]
double v11; // [rsp+0h] [rbp-28h]
double v12; // [rsp+8h] [rbp-20h]

if ( argc <= 1 )
{
printf("rescale_power <P(k) file name> <sigma8>");
}
else
{
v10 = atof(argv[2]);
func7(argv[1]);
v3 = func10(8.0);
v12 = v10 * v10 / v3 * 19.73920880217872;
if ( v3 < 0.0 )
v4 = sqrt(v3);
else
v4 = sqrt(v3);
fprintf(stderr, "Input power spectrum sigma8 %f\n", v4);
if ( NPowerTable > 0 )
{
v5 = 0LL;
do
{
v6 = v5++;
v7 = (double *)(PowerTable + 16 * v6);
v11 = pow(10.0, *v7);
v8 = pow(10.0, v7[1]);
printf("%e %e\n", v11, v12 * v8 / (v11 * v11 * v11));
}
while ( NPowerTable > (int)v5 );
}
}
return 0;
}

original_code:
int main(int argc, char* argv[])
{
  if(argc < 2) {
    printf("rescale_power <P(k) file name> <sigma8>");
    return 0;
  }
  char* filename= argv[1];
  double sigma8= atof(argv[2]);
  read_power_table_camb(filename);
  const double R8 = 8.0;
  double sigma2 = TopHatSigma2(R8);
  double fac= sigma8*sigma8/sigma2*(2.0*3.14159265358979323846*3.14159265358979323846);
  fprintf(stderr, "Input power spectrum sigma8 %f\n", sqrt(sigma2));
  for(int i=0; i<NPowerTable; i++) {
    double k= pow(10.0, PowerTable[i].logk);
    double pk= fac*pow(10.0, PowerTable[i].logD)/(k*k*k);
    printf("%e %e\n", k, pk);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  if(argc < 2) {
    printf("rescale_power <P(k) file name> <sigma8>");
    return 0;
  }
  char *filename = argv[1];
  double sigma8 = atof(argv[2]);
  read_power_table_camb(filename);
  double R8 = 8.0;
  double sigma2 = TopHatSigma2(R8);
  double fac = sigma8*sigma8/sigma2*(2.0*3.14159265358979323846*3.14159265358979323846);
  fprintf(stderr, "Input power spectrum sigma8 %f\n", sqrt(sigma2));
  for(int i = 0; i<NPowerTable; i++) {
    double k = pow(10.0, PowerTable[i].logk);
    double pk = fac*pow(10.0, PowerTable[i].logD)/(k*k*k);
    printf("%e %e\n", k, pk);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
filename
char *

variable_types: fac double

variable_types: i int

variable_types: k double

variable_types: pk double


PowerSpec

decompiled_code:
double __fastcall PowerSpec(double a1)
{
double v1; // xmm0_8
int v2; // edx
int v3; // ecx
int v4; // eax
double v5; // xmm1_8
double *v6; // rcx
double *v7; // rax
double v8; // xmm1_8

v1 = log10(a1);
if ( *(double *)PowerTable > v1 || v1 > *(double *)(PowerTable + 16LL * NPowerTable - 16) )
return 0.0;
v2 = NPowerTable - 1;
v3 = 0;
while ( v2 - v3 > 1 )
{
v4 = (v3 + v2) / 2;
v5 = *(double *)(PowerTable + 16LL * v4);
if ( v5 <= v1 )
v3 = (v3 + v2) / 2;
if ( v5 > v1 )
v2 = v4;
}
v6 = (double *)(PowerTable + 16LL * v3);
v7 = (double *)(PowerTable + 16LL * v2);
v8 = *v7 - *v6;
if ( v8 <= 0.0 )
__assert_fail("dlogk > 0.0", "rescale_power.c", 0x65u, "PowerSpec");
return pow(10.0, (1.0 - (v1 - *v6) / v8) * v6[1] + (v1 - *v6) / v8 * v7[1])
* *(double *)&Norm
/ (a1
* (12.56637061435917
* a1
* a1));
}

canonical_decompiled_code:
double __fastcall func8(double a1)
{
double v1; // xmm0_8
int v2; // edx
int v3; // ecx
int v4; // eax
double v5; // xmm1_8
double *v6; // rcx
double *v7; // rax
double v8; // xmm1_8

v1 = log10(a1);
if ( *(double *)PowerTable > v1 || v1 > *(double *)(PowerTable + 16LL * NPowerTable - 16) )
return 0.0;
v2 = NPowerTable - 1;
v3 = 0;
while ( v2 - v3 > 1 )
{
v4 = (v3 + v2) / 2;
v5 = *(double *)(PowerTable + 16LL * v4);
if ( v5 <= v1 )
v3 = (v3 + v2) / 2;
if ( v5 > v1 )
v2 = v4;
}
v6 = (double *)(PowerTable + 16LL * v3);
v7 = (double *)(PowerTable + 16LL * v2);
v8 = *v7 - *v6;
if ( v8 <= 0.0 )
__assert_fail("dlogk > 0.0", "rescale_power.c", 0x65u, "PowerSpec");
return pow(10.0, (1.0 - (v1 - *v6) / v8) * v6[1] + (v1 - *v6) / v8 * v7[1])
* *(double *)&Norm
/ (a1
* (12.56637061435917
* a1
* a1));
}

original_code:
double PowerSpec(const double k)
{
  const double logk = log10(k);
  if(logk < PowerTable[0].logk || logk > PowerTable[NPowerTable - 1].logk)
    return 0;
  int binlow = 0;
  int binhigh = NPowerTable - 1;
  while(binhigh - binlow > 1) {
    int binmid = (binhigh + binlow) / 2;
    if(logk < PowerTable[binmid].logk)
      binhigh = binmid;
    else
      binlow = binmid;
  }
  const double dlogk = PowerTable[binhigh].logk - PowerTable[binlow].logk;
  ((void) sizeof ((dlogk > 0.0) ? 1 : 0), __extension__ ({ if (dlogk > 0.0) ; else __assert_fail ("dlogk > 0.0", "rescale_power.c", 101, __extension__ __PRETTY_FUNCTION__); }));
  const double u = (logk - PowerTable[binlow].logk) / dlogk;
  const double logD= (1 - u) * PowerTable[binlow].logD + u *
                     PowerTable[binhigh].logD;
  const double Delta2 = pow(10.0, logD);
  double P = Norm * Delta2 / (4.0*3.14159265358979323846*k*k*k);
  return P;
}

canonical_original_code:
double PowerSpec(double k)
{
  double logk = log10(k);
  if(logk < PowerTable[0].logk || logk > PowerTable[NPowerTable - 1].logk)
    return 0;
  int binlow = 0;
  int binhigh = NPowerTable - 1;
  while(binhigh - binlow > 1) {
    int binmid = (binhigh + binlow) / 2;
    if(logk < PowerTable[binmid].logk)
      binhigh = binmid;
    else
      binlow = binmid;
  }
  double dlogk = PowerTable[binhigh].logk - PowerTable[binlow].logk;
  ((void) sizeof ((dlogk > 0.0) ? 1 : 0), __extension__ ({ if (dlogk > 0.0) ; else __assert_fail ("dlogk > 0.0", "rescale_power.c", 101, __extension__ __PRETTY_FUNCTION__); }));
  double u = (logk - PowerTable[binlow].logk) / dlogk;
  double logD = (1 - u) * PowerTable[binlow].logD + u *
                     PowerTable[binhigh].logD;
  double Delta2 = pow(10.0, logD);
  double P = Norm * Delta2 / (4.0*3.14159265358979323846*k*k*k);
  return P;
}

variable_types: logk double

variable_types: binlow int

variable_types: binhigh int

variable_types: binmid int

variable_types: dlogk double

variable_types: u double

variable_types: logD double

variable_types: P double

return_type: double


decompiled_code:
double __fastcall sigma2_int(double a1)
{
double v1; // xmm0_8
double v3; // xmm0_8
double cosx; // [rsp+20h] [rbp-18h] BYREF
double sinx[2]; // [rsp+28h] [rbp-10h] BYREF

v1 = a1 * *(double *)&r_tophat;
if ( v1 < 0.00000001 )
return 0.0;
sincos(v1, sinx, &cosx);
v3 = 3.0 * (sinx[0] / (v1 * (v1 * v1)) - cosx / (v1 * v1));
return PowerSpec(a1) * (12.56637061435917 * a1 * a1 * v3 * v3);
}

canonical_decompiled_code:
double __fastcall func9(double a1)
{
double v1; // xmm0_8
double v3; // xmm0_8
double cosx; // [rsp+20h] [rbp-18h] BYREF
double sinx[2]; // [rsp+28h] [rbp-10h] BYREF

v1 = a1 * *(double *)&r_tophat;
if ( v1 < 0.00000001 )
return 0.0;
sincos(v1, sinx, &cosx);
v3 = 3.0 * (sinx[0] / (v1 * (v1 * v1)) - cosx / (v1 * v1));
return func8(a1) * (12.56637061435917 * a1 * a1 * v3 * v3);
}

original_code:
double sigma2_int(double k, void *param)
{
  double kr, kr3, kr2, w, x;
  kr = r_tophat * k;
  kr2 = kr * kr;
  kr3 = kr2 * kr;
  if(kr < 1e-8)
    return 0;
  w = 3 * (sin(kr) / kr3 - cos(kr) / kr2);
  x = 4 * 3.14159265358979323846 * k * k * w * w * PowerSpec(k);
  return x;
}

canonical_original_code:
double sigma2_int(double k, void *param)
{
  double kr; double kr3; double kr2; double w; double x;
  kr = r_tophat * k;
  kr2 = kr * kr;
  kr3 = kr2 * kr;
  if(kr < 1e-8)
    return 0;
  w = 3 * (sin(kr) / kr3 - cos(kr) / kr2);
  x = 4 * 3.14159265358979323846 * k * k * w * w * PowerSpec(k);
  return x;
}

variable_types:
param
void *

variable_types: kr double

variable_types: w double

variable_types: x double


decompiled_code:
double __fastcall TopHatSigma2(double a1)
{
double v2; // [rsp+10h] [rbp-28h]

gsl_integration_workspace_alloc();
r_tophat = *(_QWORD *)&a1;
gsl_integration_qag();
gsl_integration_workspace_free();
return v2;
}

canonical_decompiled_code:
double __fastcall func10(double a1)
{
double v2; // [rsp+10h] [rbp-28h]

gsl_integration_workspace_alloc();
r_tophat = *(_QWORD *)&a1;
gsl_integration_qag();
gsl_integration_workspace_free();
return v2;
}

original_code:
double TopHatSigma2(double R)
{
  double result, abserr;
  gsl_integration_workspace *workspace;
  gsl_function F;
  workspace = gsl_integration_workspace_alloc(100000);
  F.function = &sigma2_int;
  r_tophat = R;
  gsl_integration_qag(&F, 0, 500.0 * 1 / R,
          0, 1.0e-4, 100000, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
  gsl_integration_workspace_free(workspace);
  return result;
}

canonical_original_code:
double TopHatSigma2(double R)
{
  double result; double abserr;
  struct gsl_integration_workspace *workspace;
  struct gsl_function_struct F;
  workspace = gsl_integration_workspace_alloc(100000);
  F.function = &sigma2_int;
  r_tophat = R;
  gsl_integration_qag(&F, 0, 500.0 * 1 / R,
          0, 1.0e-4, 100000, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
  gsl_integration_workspace_free(workspace);
  return result;
}

variable_types: R double

variable_types: result double

variable_types: abserr double

variable_types: struct gsl_integration_workspace { unsigned long limit; unsigned long size; unsigned long nrmax; unsigned long i; unsigned long maximum_level; double *alist; double *blist; double *rlist; double *elist; unsigned long *order; unsigned long *level; } *

variable_types: struct gsl_function_struct { double (*function)(double x, void *params); void *params; }


repo: justesen/bfi

decompiled_code:
int __fastcall mem_dump(int *a1)
{
int *v1; // rbx
const unsigned __int16 **v2; // r12
__int64 v3; // rdx
__int64 v4; // rcx
int result; // eax

do
{
v1 = a1;
a1 = (int *)*((_QWORD *)a1 + 1);
}
while ( a1 );
puts("cell no | data (int) | data (char)");
v2 = _ctype_b_loc();
do
{
v3 = *v1;
v4 = 32LL;
if ( ((*v2)[v3] & 0x8000u) != 0 )
v4 = (unsigned int)v3;
result = printf("%7d | %10d | %11c\n", (unsigned int)v1[1], v3, v4);
v1 = (int *)*((_QWORD *)v1 + 2);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
int *v1; // rbx
const unsigned __int16 **v2; // r12
__int64 v3; // rdx
__int64 v4; // rcx
int result; // eax

do
{
v1 = a1;
a1 = (int *)*((_QWORD *)a1 + 1);
}
while ( a1 );
puts("cell no | data (int) | data (char)");
v2 = _ctype_b_loc();
do
{
v3 = *v1;
v4 = 32LL;
if ( ((*v2)[v3] & 0x8000u) != 0 )
v4 = (unsigned int)v3;
result = printf("%7d | %10d | %11c\n", (unsigned int)v1[1], v3, v4);
v1 = (int *)*((_QWORD *)v1 + 2);
}
while ( v1 );
return result;
}

original_code:
void mem_dump(struct node *ptr)
{
 while (ptr->prev != ((void *)0)) {
  ptr = ptr->prev;
 }
 printf("cell no | data (int) | data (char)\n");
 while (ptr != ((void *)0)) {
  printf("%7d | %10d | %11c\n",
         ptr->cell,
         ptr->data,
         ((*__ctype_b_loc ())[(int) ((ptr->data))] & (unsigned short int) _ISgraph) ? ptr->data : ' ') ;
  ptr = ptr->next;
 }
}

canonical_original_code:
void mem_dump(struct node *ptr)
{
 while (ptr->prev != ((void *)0)) {
  ptr = ptr->prev;
 }
 printf("cell no | data (int) | data (char)\n");
 while (ptr != ((void *)0)) {
  printf("%7d | %10d | %11c\n",
         ptr->cell,
         ptr->data,
         ((*__ctype_b_loc ())[(int) ((ptr->data))] & (unsigned short) _ISgraph) ? ptr->data : ' ') ;
  ptr = ptr->next;
 }
}

variable_types: struct node { int data; int cell; struct node *prev; struct node *next; } *


help

decompiled_code:
void __noreturn help()
{
printf(
"%s - a brainfuck interpreter\n"
"\n"
"Usage: %s [options] file\n"
"\n"
"Options:\n"
" -d --dump <char> Dump memory when <char> is met\n"
" -e --eof <num> Replace EOF with <num> if such is encountered (default\n"
" is no change)\n"
" -h --help Display this information\n"
" -v --version Display program name and version number\n"
" -w --warnings Print warnings\n"
"\n"
"A man page should have come with %s, see \n"
" man %s\n"
"for more info and examples\n",
"bfi",
"bfi",
"bfi",
"bfi");
exit(0);
}

canonical_decompiled_code:
void __noreturn func3()
{
printf(
"%s - a brainfuck interpreter\n"
"\n"
"Usage: %s [options] file\n"
"\n"
"Options:\n"
" -d --dump <char> Dump memory when <char> is met\n"
" -e --eof <num> Replace EOF with <num> if such is encountered (default\n"
" is no change)\n"
" -h --help Display this information\n"
" -v --version Display program name and version number\n"
" -w --warnings Print warnings\n"
"\n"
"A man page should have come with %s, see \n"
" man %s\n"
"for more info and examples\n",
"bfi",
"bfi",
"bfi",
"bfi");
exit(0);
}

original_code:
void help(void)
{
 printf("%s - a brainfuck interpreter\n\nUsage: %s [options] file\n\nOptions:\n  -d --dump <char>  Dump memory when <char> is met\n  -e --eof <num>    Replace EOF with <num> if such is encountered (default\n                    is no change)\n  -h --help         Display this information\n  -v --version      Display program name and version number\n  -w --warnings     Print warnings\n\nA man page should have come with %s, see \n  man %s\nfor more info and examples\n",
        "bfi", "bfi", "bfi", "bfi");
 exit(0);
}

canonical_original_code:
void help()
{
 printf("%s - a brainfuck interpreter\n\nUsage: %s [options] file\n\nOptions:\n  -d --dump <char>  Dump memory when <char> is met\n  -e --eof <num>    Replace EOF with <num> if such is encountered (default\n                    is no change)\n  -h --help         Display this information\n  -v --version      Display program name and version number\n  -w --warnings     Print warnings\n\nA man page should have come with %s, see \n  man %s\nfor more info and examples\n",
        "bfi", "bfi", "bfi", "bfi");
 exit(0);
}


version

decompiled_code:
void __noreturn version()
{
printf(
"%s %s\n"
"\n"
"For license and copyright information see the LICENSE file, which should\n"
"have been distributed with the software.\n",
"bfi",
"0.3");
exit(0);
}

canonical_decompiled_code:
void __noreturn func4()
{
printf(
"%s %s\n"
"\n"
"For license and copyright information see the LICENSE file, which should\n"
"have been distributed with the software.\n",
"bfi",
"0.3");
exit(0);
}

original_code:
void version(void)
{
 printf("%s %s\n\nFor license and copyright information see the LICENSE file, which should\nhave been distributed with the software.\n",
        "bfi", "0.3");
 exit(0);
}

canonical_original_code:
void version()
{
 printf("%s %s\n\nFor license and copyright information see the LICENSE file, which should\nhave been distributed with the software.\n",
        "bfi", "0.3");
 exit(0);
}


decompiled_code:
void __noreturn exit_err(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", "bfi");
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func5(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", "bfi");
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void exit_err(const char *ferrs, ...)
{
 va_list ap;
 __builtin_va_start(ap,ferrs);
 fprintf(stderr, "%s: error: ", "bfi");
 vfprintf(stderr, ferrs, ap);
 fputc('\n', stderr);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void exit_err(char *ferrs, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,ferrs);
 fprintf(stderr, "%s: error: ", "bfi");
 vfprintf(stderr, ferrs, ap);
 fputc('\n', stderr);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
ferrs
char *


warning

decompiled_code:
int warning(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: warning: ", "bfi");
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func6(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: warning: ", "bfi");
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

original_code:
void warning(const char *fwarns, ...)
{
 va_list ap;
 __builtin_va_start(ap,fwarns);
 fprintf(stderr, "%s: warning: ", "bfi");
 vfprintf(stderr, fwarns, ap);
 fputc('\n', stderr);
 __builtin_va_end(ap);
}

canonical_original_code:
void warning(char *fwarns, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fwarns);
 fprintf(stderr, "%s: warning: ", "bfi");
 vfprintf(stderr, fwarns, ap);
 fputc('\n', stderr);
 __builtin_va_end(ap);
}

variable_types:
fwarns
char *


original_code:
int insert_last(struct node **last)
{
 struct node *new = malloc(sizeof *new);
 if (new == ((void *)0)) {
  return 0;
 }
 new->data = 0;
 new->cell = (*last)->cell + 1;
 new->next = ((void *)0);
 new->prev = *last;
 (*last)->next = new;
 *last = new;
 return 1;
}

variable_types: struct node { int data; int cell; struct node *prev; struct node *next; } **


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

result = *(_QWORD *)(*a1 + 16);
if ( result )
{
*a1 = result;
}
else
{
result = insert_last(a1);
if ( !(_DWORD)result )
exit_err("memory allocation failure");
}
return result;
}

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

result = *(_QWORD *)(*a1 + 16);
if ( result )
{
*a1 = result;
}
else
{
result = func9(a1);
if ( !(_DWORD)result )
func5("memory allocation failure");
}
return result;
}

original_code:
void next_cell(struct node **ptr)
{
 if ((*ptr)->next == ((void *)0)) {
  if (!insert_last(ptr)) {
   exit_err("memory allocation failure");
  }
 } else {
  *ptr = (*ptr)->next;
 }
}

user_defined_types: struct node { int data; int cell; struct node *prev; struct node *next; }


original_code:
int insert_first(struct node **first)
{
 struct node *new = malloc(sizeof *new);
 if (new == ((void *)0)) {
  return 0;
 }
 new->data = 0;
 new->cell = (*first)->cell - 1;
 new->next = *first;
 new->prev = ((void *)0);
 (*first)->prev = new;
 *first = new;
 return 1;
}


decompiled_code:
void __fastcall free_mem(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

do
{
v1 = ptr;
ptr = (void *)*((_QWORD *)ptr + 2);
}
while ( ptr );
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
while ( v1 );
}

canonical_decompiled_code:
void __fastcall func15(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

do
{
v1 = ptr;
ptr = (void *)*((_QWORD *)ptr + 2);
}
while ( ptr );
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
while ( v1 );
}

original_code:
void free_mem(struct node *ptr)
{
 struct node *tmp;
 while (ptr->next != ((void *)0)) {
  ptr = ptr->next;
 }
 while (ptr != ((void *)0)) {
  tmp = ptr;
  ptr = ptr->prev;
  free(tmp);
 }
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
__int64 v4; // r12
__int64 v5; // rbp

if ( (unsigned int)start_memory(32LL, argv, envp) )
puts("WOOO");
v3 = get_memory(8LL);
v4 = get_memory(8LL);
v5 = get_memory(8LL);
release_memory(v3);
release_memory(v4);
release_memory(v5);
end_memory();
return 0;
}

original_code:
int main(int argc, char * argv[]){
  if(start_memory(32)){
    printf("WOOO\n");
  }
  void * mem = get_memory(8);
  void * mem2 = get_memory(8);
  void * mem3 = get_memory(8);
  release_memory(mem);
  release_memory(mem2);
  release_memory(mem3);
  end_memory();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  if(start_memory(32)){
    printf("WOOO\n");
  }
  void *mem = get_memory(8);
  void *mem2 = get_memory(8);
  void *mem3 = get_memory(8);
  release_memory(mem);
  release_memory(mem2);
  release_memory(mem3);
  end_memory();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
mem
void *


decompiled_code:
__int64 __fastcall BuddyTree_traverse_nodes(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 result; // rax
__int64 v6; // rdi

if ( !*(_QWORD *)(a1 + 16) || (result = ((__int64 (*)(void))BuddyTree_traverse_nodes)()) == 0 )
{
v6 = *(_QWORD *)(a1 + 24);
if ( !v6 || (result = BuddyTree_traverse_nodes(v6, a2, a3)) == 0 )
{
if ( node_found )
{
result = 0LL;
}
else
{
result = a3(a1, a2);
if ( result )
node_found = 1;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 result; // rax
__int64 v6; // rdi

if ( !*(_QWORD *)(a1 + 16) || (result = ((__int64 (*)(void))BuddyTree_traverse_nodes)()) == 0 )
{
v6 = *(_QWORD *)(a1 + 24);
if ( !v6 || (result = func0(v6, a2, a3)) == 0 )
{
if ( node_found )
{
result = 0LL;
}
else
{
result = a3(a1, a2);
if ( result )
node_found = 1;
}
}
}
return result;
}

original_code:
static inline long int BuddyTree_traverse_nodes(BuddyTreeNode *node, void *key, BuddyTree_traverse_cb traverse_cb){
  long int rc = 0;
 long int address = 0;
  if(node->left) {
    rc = BuddyTree_traverse_nodes(node->left, key,traverse_cb);
    if(rc != 0) return rc;
  }
  if(node->right) {
    rc = BuddyTree_traverse_nodes(node->right, key, traverse_cb);
    if(rc != 0) return rc;
  }
 if(node_found == 0){
  address = traverse_cb(node, key);
  if(address != 0){
   node_found = 1;
  }
 }
  return address;
}

canonical_original_code:
static inline long BuddyTree_traverse_nodes(struct BuddyTreeNode *node, void *key, long (*traverse_cb)(struct BuddyTreeNode *node, void *address)){
  long rc = 0;
 long address = 0;
  if(node->left) {
    rc = BuddyTree_traverse_nodes(node->left, key,traverse_cb);
    if(rc != 0) return rc;
  }
  if(node->right) {
    rc = BuddyTree_traverse_nodes(node->right, key, traverse_cb);
    if(rc != 0) return rc;
  }
 if(node_found == 0){
  address = traverse_cb(node, key);
  if(address != 0){
   node_found = 1;
  }
 }
  return address;
}

variable_types: struct BuddyTreeNode { void *key; void *data; struct BuddyTreeNode *left; struct BuddyTreeNode *right; struct BuddyTreeNode *parent; } *

variable_types:
key
void *

variable_types: long (*)(struct BuddyTreeNode *node, void *address)

variable_types: rc long

variable_types: address long

return_type: long


decompiled_code:
int __fastcall default_compare(const char *a1, const char *a2)
{
int result; // eax

result = strncmp(a1, a2, 0x20uLL);
if ( result )
{
if ( a2[++level] == 48 )
result = -1;
else
result = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, const char *a2)
{
int result; // eax

result = strncmp(a1, a2, 0x20uLL);
if ( result )
{
if ( a2[++level] == 48 )
result = -1;
else
result = 1;
}
return result;
}

original_code:
static int default_compare(void *a, void *b){
  if(strncmp((char *)a, (char *)b, 32) == 0){
    return 0;
  }
  else{
    if(((char *)b)[level + 1] == '0'){
      level++;
      return -1;
    }
    else{
      level++;
      return 1;
    }
  }
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
__int64 __fastcall BuddyTree_destroy_cb(void *a1)
{
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *a1)
{
free(a1);
return 0LL;
}

original_code:
static long int BuddyTree_destroy_cb(BuddyTreeNode *node, void *address){
  free(node);
  return 0;
}

canonical_original_code:
static long BuddyTree_destroy_cb(struct BuddyTreeNode *node, void *address){
  free(node);
  return 0;
}

variable_types:
address
void *

user_defined_types: struct BuddyTreeNode { void *key; void *data; struct BuddyTreeNode *left; struct BuddyTreeNode *right; struct BuddyTreeNode *parent; }


decompiled_code:
__int64 __fastcall BuddyTree_create(int (__fastcall *a1)(const char *a1, const char *a2), __int64 a2)
{
int *v3; // rax
char *v4; // r8
int *v5; // rbx

if ( a2 )
{
if ( !a1 )
a1 = default_compare;
*(_QWORD *)(a2 + 8) = a1;
}
else
{
v3 = _errno_location();
v4 = "None";
v5 = v3;
if ( *v3 )
v4 = strerror(*v3);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "buddytree.c", 26LL, v4);
*v5 = 0;
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int (__fastcall *a1)(const char *a1, const char *a2), __int64 a2)
{
int *v3; // rax
char *v4; // r8
int *v5; // rbx

if ( a2 )
{
if ( !a1 )
a1 = default_compare;
*(_QWORD *)(a2 + 8) = a1;
}
else
{
v3 = _errno_location();
v4 = "None";
v5 = v3;
if ( *v3 )
v4 = strerror(*v3);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "buddytree.c", 26LL, v4);
*v5 = 0;
}
return a2;
}

original_code:
BuddyTree *BuddyTree_create(BuddyTree_compare compare, void *address){
 BuddyTree *map = address;
 if(!((map))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "buddytree.c", 26, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 map->compare = compare == ((void *)0) ? default_compare : compare;
 return map;
error:
 if(map) {
  BuddyTree_destroy(map);
    }
   return ((void *)0);
}

canonical_original_code:
struct BuddyTree *BuddyTree_create(int (*compare)(void *a, void *b), void *address){
 struct BuddyTree *map = address;
 if(!((map))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "buddytree.c", 26, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 map->compare = compare == ((void *)0) ? default_compare : compare;
 return map;
error:
 if(map) {
  BuddyTree_destroy(map);
    }
   return ((void *)0);
}

variable_types:
compare
int (*)(void *a, void *b)

variable_types: struct BuddyTree { int count; int (*compare)(void *a, void *b); struct BuddyTreeNode { void *key; void *data; struct BuddyTreeNode *left; struct BuddyTreeNode *right; struct BuddyTreeNode *parent; } *root; } *

user_defined_types: struct BuddyTree { int count; int (*compare)(void *a, void *b); struct BuddyTreeNode *root; }


decompiled_code:
__int64 __fastcall BuddyTree_set(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
_QWORD *v5; // rax
int *v6; // rax
char *v7; // r8
int *v8; // rbp

if ( *(_QWORD *)(a1 + 16) )
{
BuddyTree_setnode(a1, *(_QWORD **)(a1 + 16), a2, a3, a4);
LABEL_3:
level = 0;
return 0LL;
}
v5 = BuddyTreeNode_create(0LL, a2, a3, a4);
*(_QWORD *)(a1 + 16) = v5;
if ( v5 )
goto LABEL_3;
v6 = _errno_location();
v7 = "None";
v8 = v6;
if ( *v6 )
v7 = strerror(*v6);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "buddytree.c", 86LL, v7);
*v8 = 0;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
_QWORD *v5; // rax
int *v6; // rax
char *v7; // r8
int *v8; // rbp

if ( *(_QWORD *)(a1 + 16) )
{
BuddyTree_setnode(a1, *(_QWORD **)(a1 + 16), a2, a3, a4);
LABEL_3:
level = 0;
return 0LL;
}
v5 = BuddyTreeNode_create(0LL, a2, a3, a4);
*(_QWORD *)(a1 + 16) = v5;
if ( v5 )
goto LABEL_3;
v6 = _errno_location();
v7 = "None";
v8 = v6;
if ( *v6 )
v7 = strerror(*v6);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "buddytree.c", 86LL, v7);
*v8 = 0;
return 0xFFFFFFFFLL;
}

original_code:
int BuddyTree_set(BuddyTree *map, void *key, void *data, void * address){
  if(map->root == ((void *)0)) {
      map->root = BuddyTreeNode_create(((void *)0), key, data, address);
      if(!((map->root))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "buddytree.c", 86, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  } else {
      BuddyTree_setnode(map, map->root, key, data, address);
  }
  level = 0;
  return 0;
error:
  return -1;
}

canonical_original_code:
int BuddyTree_set(struct BuddyTree *map, void *key, void *data, void *address){
  if(map->root == ((void *)0)) {
      map->root = BuddyTreeNode_create(((void *)0), key, data, address);
      if(!((map->root))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "buddytree.c", 86, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  } else {
      BuddyTree_setnode(map, map->root, key, data, address);
  }
  level = 0;
  return 0;
error:
  return -1;
}

variable_types:
data
void *


decompiled_code:
__int64 __fastcall BuddyTree_traverse(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 v3; // rdi
__int64 result; // rax

node_found = 0;
v3 = *(_QWORD *)(a1 + 16);
if ( v3 )
result = BuddyTree_traverse_nodes(v3, a2, a3);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 v3; // rdi
__int64 result; // rax

node_found = 0;
v3 = *(_QWORD *)(a1 + 16);
if ( v3 )
result = func0(v3, a2, a3);
else
result = 0LL;
return result;
}

original_code:
long int BuddyTree_traverse(BuddyTree *map, void *key, BuddyTree_traverse_cb traverse_cb){
  node_found = 0;
 if(map->root){
    return BuddyTree_traverse_nodes(map->root, key, traverse_cb);
  }
  return 0;
}

canonical_original_code:
long BuddyTree_traverse(struct BuddyTree *map, void *key, long (*traverse_cb)(struct BuddyTreeNode *node, void *address)){
  node_found = 0;
 if(map->root){
    return BuddyTree_traverse_nodes(map->root, key, traverse_cb);
  }
  return 0;
}


decompiled_code:
void __fastcall BuddyTree_destroy(void *ptr)
{
if ( ptr )
{
BuddyTree_traverse((__int64)ptr, 0LL, (__int64 (__fastcall *)(__int64, __int64))BuddyTree_destroy_cb);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
if ( ptr )
{
func5((__int64)ptr, 0LL, (__int64 (__fastcall *)(__int64, __int64))BuddyTree_destroy_cb);
free(ptr);
}
}

original_code:
void BuddyTree_destroy(BuddyTree *map){
 if(map) {
  BuddyTree_traverse(map, ((void *)0), BuddyTree_destroy_cb);
    free(map);
  }
}

canonical_original_code:
void BuddyTree_destroy(struct BuddyTree *map){
 if(map) {
  BuddyTree_traverse(map, ((void *)0), BuddyTree_destroy_cb);
    free(map);
  }
}


original_code:
static long int release_memory_block(BuddyTreeNode *node, void *address){
 mem_header_t * data = ((mem_header_t *)node->data);
 if(data->address == address){
  if(node->parent->left == node){
   mem_header_t *right_data = ((mem_header_t *)node->parent->right->data);
   if(right_data->free){
    node->parent->left = ((void *)0);
    node->parent->right = ((void *)0);
    return 1;
   }
  }
  else{
   mem_header_t *left_data = ((mem_header_t *)node->parent->left->data);
   if(left_data->free){
    node->parent->left = ((void *)0);
    node->parent->right = ((void *)0);
    return 1;
   }
  }
  data->free = 1;
  return 1;
 }
 return 0;
}

canonical_original_code:
static long release_memory_block(struct BuddyTreeNode *node, void *address){
 struct _mem_header *data = ((mem_header_t *)node->data);
 if(data->address == address){
  if(node->parent->left == node){
   struct _mem_header *right_data = ((mem_header_t *)node->parent->right->data);
   if(right_data->free){
    node->parent->left = ((void *)0);
    node->parent->right = ((void *)0);
    return 1;
   }
  }
  else{
   struct _mem_header *left_data = ((mem_header_t *)node->parent->left->data);
   if(left_data->free){
    node->parent->left = ((void *)0);
    node->parent->right = ((void *)0);
    return 1;
   }
  }
  data->free = 1;
  return 1;
 }
 return 0;
}

variable_types: struct _mem_header { int size; int free; void *address; char key[32]; } *


decompiled_code:
__int64 __fastcall grow_memory_cb(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
char v3[40]; // [rsp-28h] [rbp-28h] BYREF

result = 0LL;
if ( *(_QWORD *)(a1[1] + 8LL) == a2 )
{
sprintf(v3, "%p", a1);
result = strtol(v3, 0LL, 16);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
char v3[40]; // [rsp-28h] [rbp-28h] BYREF

result = 0LL;
if ( *(_QWORD *)(a1[1] + 8LL) == a2 )
{
sprintf(v3, "%p", a1);
result = strtol(v3, 0LL, 16);
}
return result;
}

original_code:
static long int grow_memory_cb(BuddyTreeNode *node, void *address){
 long int node_i = 0;
 mem_header_t * data = ((mem_header_t *)node->data);
 if(data->address == address){
  char node_s[18];
  sprintf(node_s, "%p", node);
  node_i = strtol(node_s, ((void *)0), 16);
  return node_i;
 }
 return node_i;
}

canonical_original_code:
static long grow_memory_cb(struct BuddyTreeNode *node, void *address){
 long node_i = 0;
 struct _mem_header *data = ((mem_header_t *)node->data);
 if(data->address == address){
  char node_s[18];
  sprintf(node_s, "%p", node);
  node_i = strtol(node_s, ((void *)0), 16);
  return node_i;
 }
 return node_i;
}

user_defined_types: struct _mem_header { int size; int free; void *address; char key[32]; }


decompiled_code:
__int64 __fastcall release_memory(__int64 a1)
{
return BuddyTree_traverse(tree, a1, release_memory_block);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return BuddyTree_traverse(tree, a1, release_memory_block);
}

original_code:
void release_memory(void *mem){
 if(!BuddyTree_traverse(tree, mem, release_memory_block)){
 }
}


decompiled_code:
_BOOL8 __fastcall start_memory(int a1)
{
int v2; // eax
int v4; // ebp
__int64 v5; // rdi
_QWORD *v6; // rcx
char *v7; // rbp
char *v8; // rsi
char *v9; // rdx

if ( (a1 & (a1 - 1)) != 0 )
return 0LL;
v2 = a1 + 3;
if ( a1 >= 0 )
v2 = a1;
v4 = a1 + 88 * (v2 >> 2) + 24;
mem = malloc(v4);
if ( !mem )
return 0LL;
tree = BuddyTree_create(0LL, mem);
v5 = tree;
if ( !tree )
return 0LL;
v6 = mem;
v7 = (char *)mem + v4 - a1;
*((_DWORD *)mem + 12) = a1;
v8 = (char *)(v6 + 8);
v9 = (char *)(v6 + 6);
v6[7] = v7;
v6 += 12;
*((_DWORD *)v6 - 11) = 1;
*(v6 - 3) = 0LL;
usermemoffset = (__int64)v7;
*(v6 - 4) = 48LL;
*((_OWORD *)v8 + 1) = 0LL;
return (unsigned int)BuddyTree_set(v5, v8, v9) != -1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1)
{
int v2; // eax
int v4; // ebp
__int64 v5; // rdi
_QWORD *v6; // rcx
char *v7; // rbp
char *v8; // rsi
char *v9; // rdx

if ( (a1 & (a1 - 1)) != 0 )
return 0LL;
v2 = a1 + 3;
if ( a1 >= 0 )
v2 = a1;
v4 = a1 + 88 * (v2 >> 2) + 24;
mem = malloc(v4);
if ( !mem )
return 0LL;
tree = BuddyTree_create(0LL, mem);
v5 = tree;
if ( !tree )
return 0LL;
v6 = mem;
v7 = (char *)mem + v4 - a1;
*((_DWORD *)mem + 12) = a1;
v8 = (char *)(v6 + 8);
v9 = (char *)(v6 + 6);
v6[7] = v7;
v6 += 12;
*((_DWORD *)v6 - 11) = 1;
*(v6 - 3) = 0LL;
usermemoffset = (__int64)v7;
*(v6 - 4) = 48LL;
*((_OWORD *)v8 + 1) = 0LL;
return (unsigned int)BuddyTree_set(v5, v8, v9) != -1;
}

original_code:
int start_memory(int size){
  int tmpsize;
  if((size & (size - 1)) == 0){
    int totalnodes = size / 4;
    tmpsize = sizeof(BuddyTree) + size +
              (sizeof(BuddyTreeNode) * totalnodes) +
              (sizeof(mem_header_t) * totalnodes);
    if((mem = malloc(tmpsize)) == ((void *)0)){
      return 0;
    }
  }
  else{
    return 0;
  }
  if((tree = BuddyTree_create(((void *)0), mem)) == ((void *)0)){
    return 0;
  }
  mem_header_t * data = ((mem_header_t *)((BuddyTree *)mem) + 1);
  data->size = size;
  data->free = 1;
  usermemoffset = mem + (tmpsize - size);
  data->address = usermemoffset;
  strncpy(data->key, "0", 32);
  BuddyTreeNode * address = (BuddyTreeNode *)(data + 1);
  if(BuddyTree_set(tree, data->key, data, address) == -1){
    return 0;
  }
  return 1;
}

canonical_original_code:
int start_memory(int size){
  int tmpsize;
  if((size & (size - 1)) == 0){
    int totalnodes = size / 4;
    tmpsize = sizeof(BuddyTree) + size +
              (sizeof(BuddyTreeNode) * totalnodes) +
              (sizeof(mem_header_t) * totalnodes);
    if((mem = malloc(tmpsize)) == ((void *)0)){
      return 0;
    }
  }
  else{
    return 0;
  }
  if((tree = BuddyTree_create(((void *)0), mem)) == ((void *)0)){
    return 0;
  }
  struct _mem_header *data = ((mem_header_t *)((BuddyTree *)mem) + 1);
  data->size = size;
  data->free = 1;
  usermemoffset = mem + (tmpsize - size);
  data->address = usermemoffset;
  strncpy(data->key, "0", 32);
  struct BuddyTreeNode *address = (BuddyTreeNode *)(data + 1);
  if(BuddyTree_set(tree, data->key, data, address) == -1){
    return 0;
  }
  return 1;
}

variable_types: size int

variable_types: tmpsize int

variable_types: totalnodes int


decompiled_code:
void end_memory()
{
free(mem);
}

canonical_decompiled_code:
void func4()
{
free(mem);
}

original_code:
void end_memory(void){
  free(mem);
}

canonical_original_code:
void end_memory(){
  free(mem);
}


original_code:
void split_node(mem_header_t * data, void ** left, void ** right){
   mem_header_t * newleft = ((void *)0),
                * newright = ((void *)0);
   void * nodeaddleft = ((void *)0),
        * nodeaddright = ((void *)0);
   newleft = (mem_header_t *)((BuddyTreeNode *)(data + 1)) + 1;
   nodeaddleft = newleft + 1;
   newright = (mem_header_t *)((BuddyTreeNode *)nodeaddleft) + 1;
   nodeaddright = newright + 1;
   newleft->size = data->size / 2;
   newleft->address = data->address;
   strncpy(newleft->key, data->key, 32);
   strncat(newleft->key, "0", 32);
   newleft->free = 1;
   newright->size = data->size / 2;
   newright->address = data->address + (data->size / 2);
   strncpy(newright->key, data->key, 32);
   strncat(newright->key, "1", 32);
   newright->free = 1;
  if(BuddyTree_set(tree, newleft->key, newleft, nodeaddleft) == -1){
  }
  if(BuddyTree_set(tree, newright->key, newright, nodeaddright) == -1){
  }
  if(left != ((void *)0)){
    *left = newleft->address;
  }
  if(right != ((void *)0)){
    *right = newright->address;
  }
}

canonical_original_code:
void split_node(struct _mem_header *data, void **left, void **right){
   struct _mem_header *newleft = ((void *)0); struct _mem_header *newright = ((void *)0);
   void *nodeaddleft = ((void *)0); void *nodeaddright = ((void *)0);
   newleft = (struct _mem_header *)((struct BuddyTreeNode *)(data + 1)) + 1;
   nodeaddleft = newleft + 1;
   newright = (struct _mem_header *)((struct BuddyTreeNode *)nodeaddleft) + 1;
   nodeaddright = newright + 1;
   newleft->size = data->size / 2;
   newleft->address = data->address;
   strncpy(newleft->key, data->key, 32);
   strncat(newleft->key, "0", 32);
   newleft->free = 1;
   newright->size = data->size / 2;
   newright->address = data->address + (data->size / 2);
   strncpy(newright->key, data->key, 32);
   strncat(newright->key, "1", 32);
   newright->free = 1;
  if(BuddyTree_set(tree, newleft->key, newleft, nodeaddleft) == -1){
  }
  if(BuddyTree_set(tree, newright->key, newright, nodeaddright) == -1){
  }
  if(left != ((void *)0)){
    *left = newleft->address;
  }
  if(right != ((void *)0)){
    *right = newright->address;
  }
}

variable_types:
left
void **

variable_types:
right
void **

variable_types:
nodeaddleft
void *

variable_types:
nodeaddright
void *


original_code:
static void * grow_memory_general(int size, void * mem){
 long int node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void * node_v = (void *)node_i;
 BuddyTreeNode * node = (BuddyTreeNode *)node_v;
 mem_header_t * data = (mem_header_t *)node->data;
 if(data->size >= size){
  return mem;
 }
 if((data->size * 2) >= size){
  BuddyTreeNode * parent_node = node->parent;
  mem_header_t * left_node = parent_node->left->data;
  mem_header_t * right_node = parent_node->right->data;
  if(left_node->free && right_node->free){
   mem_header_t * parent_data = parent_node->data;
   memcpy(parent_data->address, data->address, data->size);
   parent_data->size = size;
   parent_node->left = ((void *)0);
   parent_node->right = ((void *)0);
  }
  else{
   mem_header_t * new_block = get_memory(size);
   memcpy(new_block->address, data->address, data->size);
   new_block->size = size;
   release_memory(data->address);
   return (void *)new_block->address;
  }
 }
  return ((void *)0);
}

canonical_original_code:
static void *grow_memory_general(int size, void *mem){
 long node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void *node_v = (void *)node_i;
 struct BuddyTreeNode *node = (BuddyTreeNode *)node_v;
 struct _mem_header *data = (mem_header_t *)node->data;
 if(data->size >= size){
  return mem;
 }
 if((data->size * 2) >= size){
  struct BuddyTreeNode *parent_node = node->parent;
  struct _mem_header *left_node = parent_node->left->data;
  struct _mem_header *right_node = parent_node->right->data;
  if(left_node->free && right_node->free){
   struct _mem_header *parent_data = parent_node->data;
   memcpy(parent_data->address, data->address, data->size);
   parent_data->size = size;
   parent_node->left = ((void *)0);
   parent_node->right = ((void *)0);
  }
  else{
   struct _mem_header *new_block = get_memory(size);
   memcpy(new_block->address, data->address, data->size);
   new_block->size = size;
   release_memory(data->address);
   return (void *)new_block->address;
  }
 }
  return ((void *)0);
}

variable_types:
mem
void *

variable_types: struct BuddyTreeNode { void *key; void *data; struct BuddyTreeNode *left; struct BuddyTreeNode *right; struct BuddyTreeNode *parent; } *

return_type: void *


decompiled_code:
__int64 __fastcall grow_memory(int a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
__int64 v5; // r12
__int64 v6; // rdi
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = BuddyTree_traverse(tree, a2, grow_memory_cb);
if ( !v2 )
return 0LL;
v3 = *(_QWORD *)(v2 + 8);
if ( *(_DWORD *)v3 / 2 < a1 )
return grow_memory_general(a1, a2);
split_node((int *)v3, 0LL, v7);
v5 = v7[0];
memcpy(*(void **)(v7[0] + 8), *(const void **)(v3 + 8), a1);
v6 = *(_QWORD *)(v3 + 8);
*(_DWORD *)(v5 + 4) = 0;
release_memory(v6);
return *(_QWORD *)(v5 + 8);
}

original_code:
void * grow_memory(int size, void * mem){
 long int node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void * node_v = (void *)node_i;
 BuddyTreeNode * node = (BuddyTreeNode *)node_v;
 mem_header_t * data = (mem_header_t *)node->data;
 if(size <= data->size / 2){
  void * new_data;
  split_node(data, ((void *)0), &new_data);
  mem_header_t * headdata = new_data;
  memcpy(headdata->address, data->address, size);
  headdata->free = 0;
  release_memory(data->address);
  return headdata->address;
 }
  return grow_memory_general(size, mem);
}

canonical_original_code:
void *grow_memory(int size, void *mem){
 long node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void *node_v = (void *)node_i;
 struct BuddyTreeNode *node = (BuddyTreeNode *)node_v;
 struct _mem_header *data = (mem_header_t *)node->data;
 if(size <= data->size / 2){
  void *new_data;
  split_node(data, ((void *)0), &new_data);
  struct _mem_header *headdata = new_data;
  memcpy(headdata->address, data->address, size);
  headdata->free = 0;
  release_memory(data->address);
  return headdata->address;
 }
  return grow_memory_general(size, mem);
}


decompiled_code:
__int64 __fastcall pregrow_memory(int a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
__int64 v5; // r12
__int64 v6; // rdi
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = BuddyTree_traverse(tree, a2, grow_memory_cb);
if ( !v2 )
return 0LL;
v3 = *(_QWORD *)(v2 + 8);
if ( *(_DWORD *)v3 / 2 < a1 )
return grow_memory_general(a1, a2);
split_node((int *)v3, v7, 0LL);
v5 = v7[0];
memcpy(*(void **)(v7[0] + 8), *(const void **)(v3 + 8), a1);
v6 = *(_QWORD *)(v3 + 8);
*(_DWORD *)(v5 + 4) = 0;
release_memory(v6);
return *(_QWORD *)(v5 + 8);
}

original_code:
void * pregrow_memory(int size, void * mem){
 long int node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void * node_v = (void *)node_i;
 BuddyTreeNode * node = (BuddyTreeNode *)node_v;
 mem_header_t * data = (mem_header_t *)node->data;
 if(size <= data->size / 2){
  void * new_data;
  split_node(data, &new_data, ((void *)0));
  mem_header_t * headdata = new_data;
  memcpy(headdata->address, data->address, size);
  headdata->free = 0;
  release_memory(data->address);
  return headdata->address;
 }
  return grow_memory_general(size, mem);
}

canonical_original_code:
void *pregrow_memory(int size, void *mem){
 long node_i;
 node_i = BuddyTree_traverse(tree, mem, &grow_memory_cb);
 if(!node_i){
  return ((void *)0);
 }
 void *node_v = (void *)node_i;
 struct BuddyTreeNode *node = (BuddyTreeNode *)node_v;
 struct _mem_header *data = (mem_header_t *)node->data;
 if(size <= data->size / 2){
  void *new_data;
  split_node(data, &new_data, ((void *)0));
  struct _mem_header *headdata = new_data;
  memcpy(headdata->address, data->address, size);
  headdata->free = 0;
  release_memory(data->address);
  return headdata->address;
 }
  return grow_memory_general(size, mem);
}


list

repo: justinethier/linked-list

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

result = malloc(0x10uLL);
result[1] = 0LL;
*result = 0LL;
return result;
}

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

result = malloc(0x10uLL);
result[1] = 0LL;
*result = 0LL;
return result;
}

original_code:
struct node *list()
{
    struct node *head = (struct node *)malloc(sizeof(struct node));
    head->val = ((void *)0);
    head->next = ((void *)0);
    return head;
}

variable_types: struct node { struct node *next; void *val; } *


decompiled_code:
__int64 __fastcall list_add(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rbx
_QWORD *v3; // rax

if ( !a1 )
return 0LL;
do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
v3 = malloc(0x10uLL);
*v2 = v3;
*v3 = 0LL;
v3[1] = a2;
return 1LL;
}

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

if ( !a1 )
return 0LL;
do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
v3 = malloc(0x10uLL);
*v2 = v3;
*v3 = 0LL;
v3[1] = a2;
return 1LL;
}

original_code:
int list_add(struct node *head, void *val)
{
    struct node *ptr = head;
    if (ptr == ((void *)0)) return 0;
    while (ptr->next != ((void *)0)) {
        ptr = ptr->next;
    }
    ptr->next = (struct node *)malloc(sizeof(struct node));
    ptr = ptr->next;
    ptr->next = ((void *)0);
    ptr->val = val;
    return 1;
}

variable_types:
val
void *

user_defined_types: struct node { struct node *next; void *val; }


decompiled_code:
__int64 __fastcall list_contains(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(_QWORD, __int64))
{
_QWORD *v4; // rbx

if ( !a1 )
return 0LL;
v4 = a1;
do
{
v4 = (_QWORD *)*v4;
if ( !v4 )
return 0LL;
}
while ( !a3(v4[1], a2) );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(_QWORD, __int64))
{
_QWORD *v4; // rbx

if ( !a1 )
return 0LL;
v4 = a1;
do
{
v4 = (_QWORD *)*v4;
if ( !v4 )
return 0LL;
}
while ( !a3(v4[1], a2) );
return 1LL;
}

original_code:
int list_contains(struct node *head, void *val, int (*cmp_val)(void *, void *))
{
    struct node *ptr = head;
    if (ptr == ((void *)0)) return 0;
    while (ptr->next != ((void *)0)) {
        ptr = ptr->next;
        if (cmp_val(ptr->val, val)){
            return 1;
        }
    }
    return 0;
}


decompiled_code:
_BOOL8 __fastcall list_is_empty(_QWORD *a1)
{
_BOOL8 result; // rax

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

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

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

original_code:
int list_is_empty(struct node *head)
{
    return (head == ((void *)0) || head->next == ((void *)0));
}


decompiled_code:
// attributes: thunk
__int64 list_add_at_head()
{
return list_cons();
}

canonical_decompiled_code:
__int64 func13()
{
return list_cons();
}

original_code:
struct node *list_add_at_head(void *val, struct node *list)
{
    return list_cons(val, list);
}

variable_types:
list
struct node *

return_type: struct node { struct node *next; void *val; } *


decompiled_code:
void __fastcall list_append(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rbx

if ( a1 )
{
do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
if ( !(unsigned int)list_is_empty(a2) )
{
*v2 = *a2;
free(a2);
}
}
}

canonical_decompiled_code:
void __fastcall func14(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rbx

if ( a1 )
{
do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
if ( !(unsigned int)func12(a2) )
{
*v2 = *a2;
free(a2);
}
}
}

original_code:
void list_append(struct node *first, struct node *second)
{
    struct node *ptr = first;
    if (ptr == ((void *)0)) return;
    while (ptr->next != ((void *)0)) {
        ptr = ptr->next;
    }
    if (!list_is_empty(second)){
        ptr->next = second->next;
        free(second);
    }
    return;
}

variable_types:
second
struct node *


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

if ( !a1 )
return 0LL;
result = *a1;
if ( *a1 )
result = *(_QWORD *)(result + 8);
return result;
}

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

if ( !a1 )
return 0LL;
result = *a1;
if ( *a1 )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void *list_car(struct node *head)
{
    if (head == ((void *)0) || head->next == ((void *)0)) return ((void *)0);
    return head->next->val;
}


decompiled_code:
// attributes: thunk
__int64 list_value()
{
return list_car();
}

canonical_decompiled_code:
__int64 func16()
{
return func15();
}

original_code:
void *list_value(struct node *head)
{
    return list_car(head);
}


decompiled_code:
void __fastcall list_set_value(__int64 a1, __int64 a2)
{
if ( a1 )
{
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)a1 + 8LL) = a2;
}
}

original_code:
void list_set_value(struct node *head, void *val)
{
    if (head == ((void *)0) || head->next == ((void *)0)) return;
    head->next->val = val;
    return;
}


decompiled_code:
// attributes: thunk
__int64 list_set_car()
{
return list_set_value();
}

canonical_decompiled_code:
__int64 func18()
{
return func17();
}

original_code:
void list_set_car(struct node *head, void *val)
{
    list_set_value(head, val);
}


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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

original_code:
struct node *list_cdr(struct node *head)
{
    if (head == ((void *)0)) return ((void *)0);
    return head->next;
}

variable_types:
head
struct node *


decompiled_code:
// attributes: thunk
__int64 list_rest()
{
return list_cdr();
}

canonical_decompiled_code:
__int64 func20()
{
return func19();
}

original_code:
struct node *list_rest(struct node *head)
{
    return list_cdr(head);
}


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

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

original_code:
int list_set_rest(struct node *head, struct node *tail)
{
    if (head == ((void *)0) || tail == ((void *)0)) return 0;
    head->next->next = tail->next;
    return 1;
}

variable_types:
tail
struct node *


decompiled_code:
// attributes: thunk
__int64 list_set_cdr()
{
return list_set_rest();
}

canonical_decompiled_code:
__int64 func22()
{
return func21();
}

original_code:
int list_set_cdr(struct node *head, struct node *tail)
{
    return list_set_rest(head, tail);
}


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

for ( result = 0xFFFFFFFFLL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

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

for ( result = 0xFFFFFFFFLL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

original_code:
int list_length(struct node *head)
{
    int count = -1;
    while (head){
        head = head->next;
        count++;
    }
    return count;
}

variable_types: count int


repo: justinethier/rosalind

decompiled_code:
__int64 __fastcall num_perm(int a1)
{
int v1; // edi
unsigned int v2; // er8
int v3; // eax

if ( a1 <= 0 )
return 1LL;
v1 = a1 + 1;
v2 = 1;
v3 = 1;
do
v2 *= v3++;
while ( v3 != v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
int v1; // edi
unsigned int v2; // er8
int v3; // eax

if ( a1 <= 0 )
return 1LL;
v1 = a1 + 1;
v2 = 1;
v3 = 1;
do
v2 *= v3++;
while ( v3 != v1 );
return v2;
}

original_code:
int num_perm(int n){
    int i, result;
    for (result = i = 1; i <= n; i++){
        result *= i;
    }
    return result;
}

canonical_original_code:
int num_perm(int n){
    int i; int result;
    for (result = i = 1; i <= n; i++){
        result *= i;
    }
    return result;
}

variable_types: n int

variable_types: i int

variable_types: result int


decompiled_code:
int __fastcall print_perm(char *a1, int a2)
{
char *v2; // rbx
char *v3; // rax
int result; // eax

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

canonical_decompiled_code:
int __fastcall func8(char *a1, int a2)
{
char *v2; // rbx
char *v3; // rax
int result; // eax

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

original_code:
void print_perm(const char *v, const int size)
{
  int i = 0;
  if (v != 0) {
    for (i = 0; i < size; i++) {
      printf("%c ", v[i] );
    }
    printf("\n");
  }
}

canonical_original_code:
void print_perm(char *v, int size)
{
  int i = 0;
  if (v != 0) {
    for (i = 0; i < size; i++) {
      printf("%c ", v[i] );
    }
    printf("\n");
  }
}

variable_types:
v
char *

variable_types: size int


permute

decompiled_code:
int __fastcall permute(char *a1, int a2, int a3)
{
int result; // eax
char *v5; // rbp
char *v6; // rbx
char v7; // r12

result = a3 - 1;
if ( a3 - 1 == a2 )
return print_perm(a1, a3);
if ( a3 > a2 )
{
v5 = &a1[a2];
v6 = v5;
do
{
v7 = *v6++;
*(v6 - 1) = *v5;
*v5 = v7;
permute(a1, (unsigned int)(a2 + 1), (unsigned int)a3);
result = (unsigned __int8)*(v6 - 1);
*v5 = result;
*(v6 - 1) = v7;
}
while ( v6 != &v5[a3 - a2 - 1 + 1] );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *a1, int a2, int a3)
{
int result; // eax
char *v5; // rbp
char *v6; // rbx
char v7; // r12

result = a3 - 1;
if ( a3 - 1 == a2 )
return func8(a1, a3);
if ( a3 > a2 )
{
v5 = &a1[a2];
v6 = v5;
do
{
v7 = *v6++;
*(v6 - 1) = *v5;
*v5 = v7;
func9(a1, (unsigned int)(a2 + 1), (unsigned int)a3);
result = (unsigned __int8)*(v6 - 1);
*v5 = result;
*(v6 - 1) = v7;
}
while ( v6 != &v5[a3 - a2 - 1 + 1] );
}
return result;
}

original_code:
void permute(char *v, const int start, const int n)
{
  int i;
  if (start == n-1) {
    print_perm(v, n);
  }
  else {
    for (i = start; i < n; i++) {
      char tmp = v[i];
      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}

canonical_original_code:
void permute(char *v, int start, int n)
{
  int i;
  if (start == n-1) {
    print_perm(v, n);
  }
  else {
    for (i = start; i < n; i++) {
      char tmp = v[i];
      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}

variable_types: start int

variable_types: tmp char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
char nptr[2]; // [rsp+7h] [rbp-11h] BYREF
int v9[3]; // [rsp+9h] [rbp-Fh] BYREF

strcpy(nptr, "3");
v3 = nptr;
if ( argc > 1 )
v3 = (char *)argv[1];
v4 = atoi(v3);
v5 = num_perm(v4, argv);
printf("%d\n", v5);
qmemcpy(v9, "1234567", 7);
v6 = atoi(v3);
permute(v9, 0LL, v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
char nptr[2]; // [rsp+7h] [rbp-11h] BYREF
int v9[3]; // [rsp+9h] [rbp-Fh] BYREF

strcpy(nptr, "3");
v3 = nptr;
if ( argc > 1 )
v3 = (char *)argv[1];
v4 = atoi(v3);
v5 = func7(v4, argv);
printf("%d\n", v5);
qmemcpy(v9, "1234567", 7);
v6 = atoi(v3);
func9(v9, 0LL, v6);
return 0;
}

original_code:
int main(int argc, char **argv){
    char sample[] = "3";
    char *data = sample;
    int num;
    if (argc > 1){
        data = argv[1];
    }
    num = num_perm( atoi(data) );
    printf("%d\n", num);
    char v[] = {'1', '2', '3', '4', '5', '6', '7'};
    permute(v, 0, atoi(data));
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
sample
char []

variable_types:
data
char *

variable_types: num int

variable_types:
v
char []


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

result = 84LL;
if ( (_BYTE)a1 != 65 )
{
result = 65LL;
if ( (_BYTE)a1 != 84 )
{
result = 71LL;
if ( (_BYTE)a1 != 67 )
{
result = 67LL;
if ( (_BYTE)a1 != 71 )
result = a1;
}
}
}
return result;
}

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

result = 84LL;
if ( (_BYTE)a1 != 65 )
{
result = 65LL;
if ( (_BYTE)a1 != 84 )
{
result = 71LL;
if ( (_BYTE)a1 != 67 )
{
result = 67LL;
if ( (_BYTE)a1 != 71 )
result = a1;
}
}
}
return result;
}

original_code:
char dna_complement(char dna)
{
    if (dna == 'A') dna = 'T';
    else if (dna == 'T') dna = 'A';
    else if (dna == 'C') dna = 'G';
    else if (dna == 'G') dna = 'C';
    return dna;
}

variable_types: dna char

return_type: char


decompiled_code:
char *__fastcall dna_reverse_complement(char *src)
{
int v1; // ebx
char *v2; // r12
unsigned int v3; // edi
char v4; // al
_BYTE *v5; // rdx

v1 = strlen(src);
v2 = (char *)malloc(v1 + 1);
strncpy(v2, src, v1);
reverse(v2);
v3 = *v2;
if ( *v2 )
{
do
{
v4 = dna_complement(v3);
*v5++ = v4;
v3 = (char)*v5;
}
while ( *v5 );
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func1(char *src)
{
int v1; // ebx
char *v2; // r12
unsigned int v3; // edi
char v4; // al
_BYTE *v5; // rdx

v1 = strlen(src);
v2 = (char *)malloc(v1 + 1);
strncpy(v2, src, v1);
reverse(v2);
v3 = *v2;
if ( *v2 )
{
do
{
v4 = func0(v3);
*v5++ = v4;
v3 = (char)*v5;
}
while ( *v5 );
}
return v2;
}

original_code:
char *dna_reverse_complement(char *dna)
{
    int i = 0, len = strlen(dna);
    char *result = (char *)malloc((len+1)*sizeof(char));
    strncpy(result, dna, len);
    reverse(result);
    while (result[i]){
        result[i] = dna_complement(result[i]);
        i++;
    }
    return result;
}

canonical_original_code:
char *dna_reverse_complement(char *dna)
{
    int i = 0; int len = strlen(dna);
    char *result = (char *)malloc((len+1)*sizeof(char));
    strncpy(result, dna, len);
    reverse(result);
    while (result[i]){
        result[i] = dna_complement(result[i]);
        i++;
    }
    return result;
}

variable_types:
dna
char *

variable_types: len int

variable_types:
result
char *

return_type: char *


decompiled_code:
__int64 __fastcall dna_is_reverse_palindrome(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rdx
char v5; // al
__int64 v6; // rdx
char v7; // r8

LODWORD(v3) = a2 + a3 - 1;
if ( (int)a2 >= (int)v3 )
return 1LL;
a2 = (int)a2;
v3 = (int)v3;
while ( 1 )
{
v5 = dna_complement(*(char *)(a1 + v3));
if ( v7 != v5 )
break;
++a2;
v3 = v6 - 1;
if ( (int)v3 <= (int)a2 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rdx
char v5; // al
__int64 v6; // rdx
char v7; // r8

LODWORD(v3) = a2 + a3 - 1;
if ( (int)a2 >= (int)v3 )
return 1LL;
a2 = (int)a2;
v3 = (int)v3;
while ( 1 )
{
v5 = func0(*(char *)(a1 + v3));
if ( v7 != v5 )
break;
++a2;
v3 = v6 - 1;
if ( (int)v3 <= (int)a2 )
return 1LL;
}
return 0LL;
}

original_code:
int dna_is_reverse_palindrome(char *dna, int pos, int len)
{
    int i, ii;
    for (i = pos, ii = pos + len - 1; i < ii; i++, ii--){
        if (dna[i] != dna_complement(dna[ii]))
            return 0;
    }
    return 1;
}

canonical_original_code:
int dna_is_reverse_palindrome(char *dna, int pos, int len)
{
    int i; int ii;
    for (i = pos, ii = pos + len - 1; i < ii; i++, ii--){
        if (dna[i] != dna_complement(dna[ii]))
            return 0;
    }
    return 1;
}

variable_types: pos int

variable_types: ii int


decompiled_code:
_BOOL8 __fastcall start_codon(unsigned __int8 *a1)
{
int v1; // eax
_BOOL8 result; // rax

v1 = *a1 - 65;
if ( *a1 == 65 && (v1 = a1[1] - 85, a1[1] == 85) )
result = a1[2] == 71;
else
result = v1 == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned __int8 *a1)
{
int v1; // eax
_BOOL8 result; // rax

v1 = *a1 - 65;
if ( *a1 == 65 && (v1 = a1[1] - 85, a1[1] == 85) )
result = a1[2] == 71;
else
result = v1 == 0;
return result;
}

original_code:
int start_codon(char *codon){
    return (strncmp(codon, "AUG", 3) == 0);
}

variable_types:
codon
char *


original_code:
char encode_codon(char *codon)
{
    if (strncmp(codon, "UUU", 3) == 0) return 'F';
    if (strncmp(codon, "UUC", 3) == 0) return 'F';
    if (strncmp(codon, "UUA", 3) == 0) return 'L';
    if (strncmp(codon, "UUG", 3) == 0) return 'L';
    if (strncmp(codon, "UCU", 3) == 0) return 'S';
    if (strncmp(codon, "UCC", 3) == 0) return 'S';
    if (strncmp(codon, "UCA", 3) == 0) return 'S';
    if (strncmp(codon, "UCG", 3) == 0) return 'S';
    if (strncmp(codon, "UAU", 3) == 0) return 'Y';
    if (strncmp(codon, "UAC", 3) == 0) return 'Y';
    if (strncmp(codon, "UAA", 3) == 0) return '\0';
    if (strncmp(codon, "UAG", 3) == 0) return '\0';
    if (strncmp(codon, "UGU", 3) == 0) return 'C';
    if (strncmp(codon, "UGC", 3) == 0) return 'C';
    if (strncmp(codon, "UGA", 3) == 0) return '\0';
    if (strncmp(codon, "UGG", 3) == 0) return 'W';
    if (strncmp(codon, "CUU", 3) == 0) return 'L';
    if (strncmp(codon, "CUC", 3) == 0) return 'L';
    if (strncmp(codon, "CUA", 3) == 0) return 'L';
    if (strncmp(codon, "CUG", 3) == 0) return 'L';
    if (strncmp(codon, "CCU", 3) == 0) return 'P';
    if (strncmp(codon, "CCC", 3) == 0) return 'P';
    if (strncmp(codon, "CCA", 3) == 0) return 'P';
    if (strncmp(codon, "CCG", 3) == 0) return 'P';
    if (strncmp(codon, "CAU", 3) == 0) return 'H';
    if (strncmp(codon, "CAC", 3) == 0) return 'H';
    if (strncmp(codon, "CAA", 3) == 0) return 'Q';
    if (strncmp(codon, "CAG", 3) == 0) return 'Q';
    if (strncmp(codon, "CGU", 3) == 0) return 'R';
    if (strncmp(codon, "CGC", 3) == 0) return 'R';
    if (strncmp(codon, "CGA", 3) == 0) return 'R';
    if (strncmp(codon, "CGG", 3) == 0) return 'R';
    if (strncmp(codon, "AUU", 3) == 0) return 'I';
    if (strncmp(codon, "AUC", 3) == 0) return 'I';
    if (strncmp(codon, "AUA", 3) == 0) return 'I';
    if (strncmp(codon, "AUG", 3) == 0) return 'M';
    if (strncmp(codon, "ACU", 3) == 0) return 'T';
    if (strncmp(codon, "ACC", 3) == 0) return 'T';
    if (strncmp(codon, "ACA", 3) == 0) return 'T';
    if (strncmp(codon, "ACG", 3) == 0) return 'T';
    if (strncmp(codon, "AAU", 3) == 0) return 'N';
    if (strncmp(codon, "AAC", 3) == 0) return 'N';
    if (strncmp(codon, "AAA", 3) == 0) return 'K';
    if (strncmp(codon, "AAG", 3) == 0) return 'K';
    if (strncmp(codon, "AGU", 3) == 0) return 'S';
    if (strncmp(codon, "AGC", 3) == 0) return 'S';
    if (strncmp(codon, "AGA", 3) == 0) return 'R';
    if (strncmp(codon, "AGG", 3) == 0) return 'R';
    if (strncmp(codon, "GUU", 3) == 0) return 'V';
    if (strncmp(codon, "GUC", 3) == 0) return 'V';
    if (strncmp(codon, "GUA", 3) == 0) return 'V';
    if (strncmp(codon, "GUG", 3) == 0) return 'V';
    if (strncmp(codon, "GCU", 3) == 0) return 'A';
    if (strncmp(codon, "GCC", 3) == 0) return 'A';
    if (strncmp(codon, "GCA", 3) == 0) return 'A';
    if (strncmp(codon, "GCG", 3) == 0) return 'A';
    if (strncmp(codon, "GAU", 3) == 0) return 'D';
    if (strncmp(codon, "GAC", 3) == 0) return 'D';
    if (strncmp(codon, "GAA", 3) == 0) return 'E';
    if (strncmp(codon, "GAG", 3) == 0) return 'E';
    if (strncmp(codon, "GGU", 3) == 0) return 'G';
    if (strncmp(codon, "GGC", 3) == 0) return 'G';
    if (strncmp(codon, "GGA", 3) == 0) return 'G';
    if (strncmp(codon, "GGG", 3) == 0) return 'G';
    printf("ERROR: Unexpected codon %c%c%c\n", codon[0], codon[1], codon[2]);
    ((void) sizeof ((1) ? 1 : 0), __extension__ ({ if (1) ; else __assert_fail ("1", "src/lib/prot_lib.c", 85, __extension__ __PRETTY_FUNCTION__); }));
    return -1;
}


decompiled_code:
char *__fastcall set_max(char *a1, double *a2, const char *a3, double a4, double a5)
{
double v5; // xmm0_8
char *result; // rax

v5 = a4 * 100.0 / a5;
if ( v5 > *a2 )
{
*a2 = v5;
result = strncpy(a1, a3, 0x50uLL);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(char *a1, double *a2, const char *a3, double a4, double a5)
{
double v5; // xmm0_8
char *result; // rax

v5 = a4 * 100.0 / a5;
if ( v5 > *a2 )
{
*a2 = v5;
result = strncpy(a1, a3, 0x50uLL);
}
return result;
}

original_code:
void set_max(char *max_id, double *max_gc, char *id, double gc, double total){
    double gc_content = 100 * gc / ((double) total);
    if (*max_gc < gc_content) {
        *max_gc = gc_content;
        strncpy(max_id, id, 80);
    }
}

variable_types:
id
char *

variable_types: gc double

variable_types: total double


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // rdi
char v6[20]; // [rsp+4h] [rbp-14h] BYREF

strcpy(v6, "data/gc.fas");
v4 = v6;
if ( argc > 1 )
v4 = (char *)argv[1];
read_data(v4, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // rdi
char v6[20]; // [rsp+4h] [rbp-14h] BYREF

strcpy(v6, "data/gc.fas");
v4 = v6;
if ( argc > 1 )
v4 = (char *)argv[1];
func8(v4, argv, envp);
return 0;
}

original_code:
int main(int argc, char **argv){
    char sample[] = "data/gc.fas", *data = sample;
    if (argc > 1)
        data = argv[1];
    read_data(data);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv){
    char sample[] = "data/gc.fas"; char *data = sample;
    if (argc > 1)
        data = argv[1];
    read_data(data);
    return 0;
}


reverse

decompiled_code:
__int64 __fastcall reverse(const char *a1)
{
__int64 result; // rax
__int64 v2; // rdx
char v3; // cl

result = (unsigned int)strlen(a1) - 1;
if ( (int)result > 0 )
{
result = (int)result;
v2 = 0LL;
do
{
v3 = a1[v2];
a1[v2++] = a1[result];
a1[result--] = v3;
}
while ( (int)v2 < (int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
__int64 result; // rax
__int64 v2; // rdx
char v3; // cl

result = (unsigned int)strlen(a1) - 1;
if ( (int)result > 0 )
{
result = (int)result;
v2 = 0LL;
do
{
v3 = a1[v2];
a1[v2++] = a1[result];
a1[result--] = v3;
}
while ( (int)v2 < (int)result );
}
return result;
}

original_code:
void reverse(char s[])
{
    int c, i, j;
    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

canonical_original_code:
void reverse(char s[])
{
    int c; int i; int j;
    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

variable_types:
s
char []

variable_types: c int

variable_types: i int

variable_types: j int


original_code:
struct ntc count_nt(char *data, int len){
    int i = 0;
    struct ntc result;
    memset(&result, 0, sizeof(result));
    for (i = 0; i < len; i++){
        switch(data[i]){
            case 'A': result.a++; break;
            case 'C': result.c++; break;
            case 'G': result.g++; break;
            case 'T': result.t++; break;
        }
    }
    return result;
}

variable_types:
data
char *

variable_types: struct ntc { int a; int c; int g; int t; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
size_t v4; // rax
__int64 v5; // rax
unsigned __int64 v6; // rdx
__int128 v8[4]; // [rsp+0h] [rbp-58h] BYREF
char v9[8]; // [rsp+40h] [rbp-18h] BYREF

strcpy(v9, "AGCAGC");
v3 = (const char *)v8;
v8[0] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402020);
v8[1] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402030);
v8[2] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402040);
v8[3] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402050);
if ( argc > 1 )
v3 = argv[1];
v4 = strlen(v3);
v5 = count_nt(v3, v4);
printf("%d %d %d %d\n", v5, v5 >> 32, v6, HIDWORD(v6));
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
size_t v4; // rax
__int64 v5; // rax
unsigned __int64 v6; // rdx
__int128 v8[4]; // [rsp+0h] [rbp-58h] BYREF
char v9[8]; // [rsp+40h] [rbp-18h] BYREF

strcpy(v9, "AGCAGC");
v3 = (const char *)v8;
v8[0] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402020);
v8[1] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402030);
v8[2] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402040);
v8[3] = (__int128)_mm_load_si128((const __m128i *)&xmmword_402050);
if ( argc > 1 )
v3 = argv[1];
v4 = strlen(v3);
v5 = func7(v3, v4);
printf("%d %d %d %d\n", v5, v5 >> 32, v6, HIDWORD(v6));
return 0;
}

original_code:
int main(int argc, char **argv){
    char sample[] = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
    char *data = sample;
    if (argc > 1) data = argv[1];
    struct ntc result = count_nt(data, strlen(data));
    printf("%d %d %d %d\n", result.a, result.c, result.g, result.t);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
sample
char []

user_defined_types: struct ntc { int a; int c; int g; int t; }


decompiled_code:
__int64 __fastcall hamm_dist(const char *a1, const char *a2)
{
size_t v2; // r12
char v3; // dl
unsigned int v4; // er8
__int64 i; // rax
bool v6; // dl

v2 = strlen(a1);
if ( v2 != strlen(a2) )
__assert_fail("strlen(a) == strlen(b)", "src/hamm.c", 0x12u, "hamm_dist");
v3 = *a1;
v4 = 0;
for ( i = 0LL; v3; v3 = a1[i] )
{
v6 = a2[i++] != (unsigned __int8)v3;
v4 += v6;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, const char *a2)
{
size_t v2; // r12
char v3; // dl
unsigned int v4; // er8
__int64 i; // rax
bool v6; // dl

v2 = strlen(a1);
if ( v2 != strlen(a2) )
__assert_fail("strlen(a) == strlen(b)", "src/hamm.c", 0x12u, "hamm_dist");
v3 = *a1;
v4 = 0;
for ( i = 0LL; v3; v3 = a1[i] )
{
v6 = a2[i++] != (unsigned __int8)v3;
v4 += v6;
}
return v4;
}

original_code:
int hamm_dist(char *a, char *b){
    int dist = 0, i = 0;
    ((void) sizeof ((strlen(a) == strlen(b)) ? 1 : 0), __extension__ ({ if (strlen(a) == strlen(b)) ; else __assert_fail ("strlen(a) == strlen(b)", "src/hamm.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    while(a[i]){
        if (a[i] != b[i])
            dist++;
        i++;
    }
    return dist;
}

canonical_original_code:
int hamm_dist(char *a, char *b){
    int dist = 0; int i = 0;
    ((void) sizeof ((strlen(a) == strlen(b)) ? 1 : 0), __extension__ ({ if (strlen(a) == strlen(b)) ; else __assert_fail ("strlen(a) == strlen(b)", "src/hamm.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    while(a[i]){
        if (a[i] != b[i])
            dist++;
        i++;
    }
    return dist;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types: dist int


original_code:
int main(int argc, char **argv){
    char sample1[] = "GAGCCTACTAACGGGAT", sample2[] = "CATCGTAATGACGGCCT";
    char *data1 = sample1, *data2 = sample2;
    int dist;
    if (argc > 2){
        data1 = argv[1];
        data2 = argv[2];
    }
    dist = hamm_dist(data1, data2);
    printf("%d\n", dist);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv){
    char sample1[] = "GAGCCTACTAACGGGAT"; char sample2[] = "CATCGTAATGACGGCCT";
    char *data1 = sample1; char *data2 = sample2;
    int dist;
    if (argc > 2){
        data1 = argv[1];
        data2 = argv[2];
    }
    dist = hamm_dist(data1, data2);
    printf("%d\n", dist);
    return 0;
}


original_code:
int main(int argc, char **argv)
{
    double data[20];
    int len = 3, i;
    data[0] = 0.23;
    data[1] = 0.31;
    data[2] = 0.75;
    if (argc > 2){
        len = argc - 1;
        for (i = 0; i < len; i++){
            data[i] = atof(argv[i + 1]);
        }
    }
    for (i = 0; i < len; i++){
        printf("%lf ", prob(data[i]));
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    double data[20];
    int len = 3; int i;
    data[0] = 0.23;
    data[1] = 0.31;
    data[2] = 0.75;
    if (argc > 2){
        len = argc - 1;
        for (i = 0; i < len; i++){
            data[i] = atof(argv[i + 1]);
        }
    }
    for (i = 0; i < len; i++){
        printf("%lf ", prob(data[i]));
    }
    return 0;
}

variable_types:
data
double [20]

variable_types: len int