decompiled_code:
__int64 __fastcall exec_job_list(__int64 a1)
{
__int64 v1; // r14
__int64 v2; // r13
_BOOL4 v3; // er12
int v4; // er15
__int64 v5; // rbx
__pid_t v6; // eax
const char *v7; // rdi
int v8; // er15
const char *v9; // rdi
int v10; // eax
int fd; // [rsp+0h] [rbp-48h]
int v13; // [rsp+4h] [rbp-44h]
int pipedes[2]; // [rsp+8h] [rbp-40h] BYREF

if ( !a1 )
return 0LL;
v1 = a1;
v2 = a1;
do
{
pipedes[0] = -1;
pipedes[1] = -1;
v5 = *(_QWORD *)(v2 + 8);
if ( v5 )
{
v3 = 0;
do
{
fd = pipedes[0];
v13 = pipedes[1];
if ( *(_QWORD *)(v5 + 48) && pipe(pipedes) < 0 )
{
perror("pipe");
exit(3);
}
if ( (unsigned int)exec_buildin((_QWORD *)v5) )
*(_QWORD *)v5 = 0LL;
v6 = fork();
*(_DWORD *)(v5 + 16) = v6;
if ( v6 < 0 )
{
perror("fork");
exit(3);
}
if ( v6 > 0 )
{
if ( fd != -1 )
close(fd);
if ( v13 != -1 )
close(v13);
v3 = *(_QWORD *)(v5 + 48) != 0LL;
}
else
{
v7 = *(const char **)(v5 + 24);
if ( v7 )
{
v8 = open(v7, 0);
close(0);
dup2(v8, 0);
close(v8);
}
v9 = *(const char **)(v5 + 40);
if ( v9 )
{
v10 = *(_DWORD *)(v5 + 32);
if ( v10 )
{
v4 = -1;
if ( v10 == 1 )
v4 = open(v9, 1089, 384LL);
}
else
{
v4 = open(v9, 577, 384LL);
}
close(1);
dup2(v4, 1);
close(v4);
}
if ( v3 )
{
close(0);
dup2(fd, 0);
close(fd);
close(v13);
}
if ( *(_QWORD *)(v5 + 48) )
{
close(pipedes[0]);
close(1);
dup2(pipedes[1], 1);
close(pipedes[1]);
}
if ( !*(_QWORD *)v5 )
exit(0);
exec_process((const char **)v5);
}
v5 = *(_QWORD *)(v5 + 48);
}
while ( v5 );
}
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
do
{
wait_process_list(v1);
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
return 0LL;
}

original_code:
int exec_job_list(job* job_list)
{
    int index;
    job *jb;
    process *pr;
    for(index = 0, jb = job_list; jb != ((void *)0); jb = jb->next, ++index) {
        int fileds[2][2] = {
            {-1, -1},
            {-1, -1}
        };
        int has_before = 0;
        for(pr = jb->process_list; pr != ((void *)0); pr = pr->next) {
            fileds[0][0] = fileds[1][0];
            fileds[0][1] = fileds[1][1];
            if (pr->next) {
                if (pipe(fileds[1]) < 0) {
                    perror("pipe");
                    exit(3);
                }
            }
            if (exec_buildin(pr)) {
                pr->program_name = ((void *)0);
            }
            pr->pid = fork();
            if (pr->pid < 0) {
                perror("fork");
                exit(3);
            } else if (pr->pid > 0) {
                if (fileds[0][0] != -1) close(fileds[0][0]);
                if (fileds[0][1] != -1) close(fileds[0][1]);
                if (pr->next) {
                    has_before = 1;
                } else has_before = 0;
                continue;
            }
            if (pr->input_redirection) {
                int inputds = open(pr->input_redirection, 00);
                close(0); dup2(inputds, 0);
                close(inputds);
            }
            if (pr->output_redirection) {
                int outputds = -1;
                switch (pr->output_option) {
                    case APPEND:
                        outputds = open(pr->output_redirection,
                                01 | 02000 | 0100, 0400 | 0200);
                        break;
                    case TRUNC:
                        outputds = open(pr->output_redirection,
                                01 | 01000 | 0100, 0400 | 0200);
                }
                close(1); dup2(outputds, 1);
                close(outputds);
            }
            if (has_before) {
                close(0); dup2(fileds[0][0], 0);
                close(fileds[0][0]);
                close(fileds[0][1]);
            }
            if (pr->next) {
                close(fileds[1][0]);
                close(1); dup2(fileds[1][1], 1);
                close(fileds[1][1]);
            }
            if (!pr->program_name) exit(0);
            exec_process(pr);
        }
    }
    for(index = 0, jb = job_list; jb != ((void *)0); jb = jb->next, index++) {
        wait_process_list(jb);
    }
    return 0;
}

canonical_original_code:
int exec_job_list(struct job_ *job_list)
{
    int index;
    struct job_ *jb;
    struct process_ *pr;
    for(index = 0, jb = job_list; jb != ((void *)0); jb = jb->next, ++index) {
        int fileds[2][2] = {
            {-1, -1},
            {-1, -1}
        };
        int has_before = 0;
        for(pr = jb->process_list; pr != ((void *)0); pr = pr->next) {
            fileds[0][0] = fileds[1][0];
            fileds[0][1] = fileds[1][1];
            if (pr->next) {
                if (pipe(fileds[1]) < 0) {
                    perror("pipe");
                    exit(3);
                }
            }
            if (exec_buildin(pr)) {
                pr->program_name = ((void *)0);
            }
            pr->pid = fork();
            if (pr->pid < 0) {
                perror("fork");
                exit(3);
            } else if (pr->pid > 0) {
                if (fileds[0][0] != -1) close(fileds[0][0]);
                if (fileds[0][1] != -1) close(fileds[0][1]);
                if (pr->next) {
                    has_before = 1;
                } else has_before = 0;
                continue;
            }
            if (pr->input_redirection) {
                int inputds = open(pr->input_redirection, 00);
                close(0); dup2(inputds, 0);
                close(inputds);
            }
            if (pr->output_redirection) {
                int outputds = -1;
                switch (pr->output_option) {
                    case APPEND:
                        outputds = open(pr->output_redirection,
                                01 | 02000 | 0100, 0400 | 0200);
                        break;
                    case TRUNC:
                        outputds = open(pr->output_redirection,
                                01 | 01000 | 0100, 0400 | 0200);
                }
                close(1); dup2(outputds, 1);
                close(outputds);
            }
            if (has_before) {
                close(0); dup2(fileds[0][0], 0);
                close(fileds[0][0]);
                close(fileds[0][1]);
            }
            if (pr->next) {
                close(fileds[1][0]);
                close(1); dup2(fileds[1][1], 1);
                close(fileds[1][1]);
            }
            if (!pr->program_name) exit(0);
            exec_process(pr);
        }
    }
    for(index = 0, jb = job_list; jb != ((void *)0); jb = jb->next, index++) {
        wait_process_list(jb);
    }
    return 0;
}

variable_types:
fileds
int [2][2]

variable_types: inputds int

variable_types: outputds int


repo: tomoasleep/nemuish

decompiled_code:
__int64 __fastcall exec_cd(int a1, __int64 a2)
{
const char *v2; // rdi
int v3; // edx
__int64 result; // rax
__int64 v5; // rax

if ( a1 <= 2 )
{
v5 = search_env_by_name(*(_QWORD *)options, "HOME");
v2 = (const char *)v5;
if ( v5 )
v2 = *(const char **)(v5 + 32);
}
else
{
v2 = *(const char **)(a2 + 8);
}
v3 = chdir(v2);
result = 0LL;
if ( v3 < 0 )
{
perror("cd");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2)
{
const char *v2; // rdi
int v3; // edx
__int64 result; // rax
__int64 v5; // rax

if ( a1 <= 2 )
{
v5 = search_env_by_name(*(_QWORD *)options, "HOME");
v2 = (const char *)v5;
if ( v5 )
v2 = *(const char **)(v5 + 32);
}
else
{
v2 = *(const char **)(a2 + 8);
}
v3 = chdir(v2);
result = 0LL;
if ( v3 < 0 )
{
perror("cd");
result = 1LL;
}
return result;
}

original_code:
int exec_cd(int argc, char *argv[], char *env[]) {
    char *pth = ((void *)0);
    envi *hme = ((void *)0);
    if (argc > 2) {
        pth = argv[1];
    } else if ((hme = search_env_by_name(options->env, "HOME"))) {
        if (hme->contents) {
            pth = hme->contents->value;
        }
    }
    if (chdir(pth) < 0) {
        perror("cd");
        return 1;
    }
    return 0;
}

canonical_original_code:
int exec_cd(int argc, char *argv[], char *env[]) {
    char *pth = ((void *)0);
    struct _env *hme = ((void *)0);
    if (argc > 2) {
        pth = argv[1];
    } else if ((hme = search_env_by_name(options->env, "HOME"))) {
        if (hme->contents) {
            pth = hme->contents->value;
        }
    }
    if (chdir(pth) < 0) {
        perror("cd");
        return 1;
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
env
char *[]

variable_types:
pth
char *

variable_types: struct _env { char name[30]; struct _env_contents { char value[100]; struct _env_contents *next; } *contents; struct _env *next; } *

return_type: int

user_defined_types: struct _env { char name[30]; struct _env_contents *contents; struct _env *next; }


repo: tomrf/libtstr

decompiled_code:
__int64 __fastcall tstr_strlen(__int64 a1)
{
_BYTE *v1; // rdx
__int64 result; // rax

v1 = *(_BYTE **)(a1 + 8);
if ( !*v1 )
return 0LL;
result = 0LL;
do
++result;
while ( v1[result] );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
_BYTE *v1; // rdx
__int64 result; // rax

v1 = *(_BYTE **)(a1 + 8);
if ( !*v1 )
return 0LL;
result = 0LL;
do
++result;
while ( v1[result] );
return result;
}

original_code:
tstr_len_t tstr_strlen(tstr_t *ts)
{
  tstr_len_t len = 0;
  char *ptr;
  ptr = ts->string;
  while(*ptr++)
    len++;
  return len;
}

canonical_original_code:
unsigned long tstr_strlen(struct _tstr_element *ts)
{
  unsigned long len = 0;
  char *ptr;
  ptr = ts->string;
  while(*ptr++)
    len++;
  return len;
}

variable_types: struct _tstr_element { char *data; char *string; unsigned long data_len; } *

variable_types:
len
unsigned long

variable_types:
ptr
char *

return_type: unsigned long


main

canonical_name: main

repo: tomrf/ninja

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
_DWORD *v4; // r13
__int64 v5; // rbx
__int64 v6; // rbx
_BYTE v7[64]; // [rsp+0h] [rbp-A8h] BYREF
char v8[4]; // [rsp+40h] [rbp-68h] BYREF
unsigned int v9; // [rsp+44h] [rbp-64h]

wlist_load("./wlist.txt", argv, envp);
wlist_debug_dump_list();
v3 = (unsigned int)get_max_pid();
LOG(4LL, "max_pid=%d", v3);
v4 = calloc((int)v3, 4uLL);
if ( (int)v3 > 0 )
{
v5 = 0LL;
do
{
v4[v5] = 0;
if ( !(unsigned int)proc_get_info((unsigned int)v5, v8) && (unsigned int)proc_is_root(v8) )
v4[v5] = 1;
++v5;
}
while ( v5 != (unsigned int)v3 );
}
while ( 1 )
{
if ( (int)v3 > 0 )
{
v6 = 0LL;
do
{
if ( (unsigned int)proc_get_info((unsigned int)v6, v8) )
{
v4[v6] = 0;
}
else if ( (unsigned int)proc_is_root(v8) )
{
if ( v4[v6] != 1 )
{
proc_get_info(v9, v7);
check_proc(v8, v7);
v4[v6] = 1;
}
}
++v6;
}
while ( v6 != (unsigned int)v3 );
}
usleep(0x7A120u);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
_DWORD *v4; // r13
__int64 v5; // rbx
__int64 v6; // rbx
_BYTE v7[64]; // [rsp+0h] [rbp-A8h] BYREF
char v8[4]; // [rsp+40h] [rbp-68h] BYREF
unsigned int v9; // [rsp+44h] [rbp-64h]

wlist_load("./wlist.txt", argv, envp);
wlist_debug_dump_list();
v3 = (unsigned int)func0();
LOG(4LL, "max_pid=%d", v3);
v4 = calloc((int)v3, 4uLL);
if ( (int)v3 > 0 )
{
v5 = 0LL;
do
{
v4[v5] = 0;
if ( !(unsigned int)proc_get_info((unsigned int)v5, v8) && (unsigned int)proc_is_root(v8) )
v4[v5] = 1;
++v5;
}
while ( v5 != (unsigned int)v3 );
}
while ( 1 )
{
if ( (int)v3 > 0 )
{
v6 = 0LL;
do
{
if ( (unsigned int)proc_get_info((unsigned int)v6, v8) )
{
v4[v6] = 0;
}
else if ( (unsigned int)proc_is_root(v8) )
{
if ( v4[v6] != 1 )
{
proc_get_info(v9, v7);
check_proc(v8, v7);
v4[v6] = 1;
}
}
++v6;
}
while ( v6 != (unsigned int)v3 );
}
usleep(0x7A120u);
}
}

original_code:
int main(void)
{
    pid_t max_pid;
    pinfo_t pi, parent;
    int *root_pids;
    int ret;
    int i;
    wlist_load("./wlist.txt");
    wlist_debug_dump_list();
    max_pid = get_max_pid();
    LOG(4, "max_pid=%d", max_pid);
    root_pids = calloc(max_pid, sizeof (int));
    for (i = 0; i < max_pid; i++) {
        *(root_pids + i) = 0;
        ret = proc_get_info(i, &pi);
        if (ret == 0 && proc_is_root(&pi))
            *(root_pids + i) = 1;
    }
    while (1) {
        for (i = 0; i < max_pid; i++) {
            ret = proc_get_info(i, &pi);
            if (ret != 0) {
                *(root_pids + i) = 0;
            }
            else if (proc_is_root(&pi) && *(root_pids + i) != 1) {
                proc_get_info(pi.ppid, &parent);
                check_proc(&pi, &parent);
                *(root_pids + i) = 1;
            }
        }
        usleep(500 * 1000);
    }
    return 0;
}

canonical_original_code:
int main()
{
    int max_pid;
    struct proc_info pi; struct proc_info parent;
    int *root_pids;
    int ret;
    int i;
    wlist_load("./wlist.txt");
    wlist_debug_dump_list();
    max_pid = get_max_pid();
    LOG(4, "max_pid=%d", max_pid);
    root_pids = calloc(max_pid, sizeof (int));
    for (i = 0; i < max_pid; i++) {
        *(root_pids + i) = 0;
        ret = proc_get_info(i, &pi);
        if (ret == 0 && proc_is_root(&pi))
            *(root_pids + i) = 1;
    }
    while (1) {
        for (i = 0; i < max_pid; i++) {
            ret = proc_get_info(i, &pi);
            if (ret != 0) {
                *(root_pids + i) = 0;
            }
            else if (proc_is_root(&pi) && *(root_pids + i) != 1) {
                proc_get_info(pi.ppid, &parent);
                check_proc(&pi, &parent);
                *(root_pids + i) = 1;
            }
        }
        usleep(500 * 1000);
    }
    return 0;
}

variable_types: struct proc_info { int pid; int ppid; unsigned int uid; unsigned int gid; char name[16]; }

variable_types: ret int

variable_types: i int


decompiled_code:
__int64 __fastcall kill_children(unsigned int a1)
{
int v1; // ebp
unsigned int i; // ebx
__int64 result; // rax
__pid_t pid[22]; // [rsp+0h] [rbp-58h] BYREF

if ( a1 == 1 )
{
puts("EEEEEEEEEK, will not kill children of pid 1");
result = 0xFFFFFFFFLL;
}
else
{
v1 = get_max_pid();
if ( v1 <= 0 )
{
result = 0LL;
}
else
{
for ( i = 0; i != v1; ++i )
{
if ( !(unsigned int)proc_get_info(i, pid) && pid[1] == a1 )
{
printf(" - check it: %d is a child of %d !\n", (unsigned int)pid[0], a1);
printf("kill(%d, 9)\n", (unsigned int)pid[0]);
kill(pid[0], 9);
}
}
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
int v1; // ebp
unsigned int i; // ebx
__int64 result; // rax
__pid_t pid[22]; // [rsp+0h] [rbp-58h] BYREF

if ( a1 == 1 )
{
puts("EEEEEEEEEK, will not kill children of pid 1");
result = 0xFFFFFFFFLL;
}
else
{
v1 = get_max_pid();
if ( v1 <= 0 )
{
result = 0LL;
}
else
{
for ( i = 0; i != v1; ++i )
{
if ( !(unsigned int)proc_get_info(i, pid) && pid[1] == a1 )
{
printf(" - check it: %d is a child of %d !\n", (unsigned int)pid[0], a1);
printf("kill(%d, 9)\n", (unsigned int)pid[0]);
kill(pid[0], 9);
}
}
result = 0LL;
}
}
return result;
}

original_code:
int kill_children(const pid_t pid)
{
    pinfo_t pi;
    int max_pid;
    int count = 0;
    int ret;
    int i;
    if (pid == 1) {
        printf("EEEEEEEEEK, will not kill children of pid 1\n");
        return -1;
    }
    max_pid = get_max_pid();
    for (i = 0; i < max_pid; i++) {
        ret = proc_get_info(i, &pi);
        if (ret == 0 && pi.ppid == pid) {
            printf(" - check it: %d is a child of %d !\n", pi.pid, pid);
            printf("kill(%d, 9)\n", pi.pid);
            kill(pi.pid, 9);
        }
    }
    return count;
}

canonical_original_code:
int kill_children(int pid)
{
    struct proc_info pi;
    int max_pid;
    int count = 0;
    int ret;
    int i;
    if (pid == 1) {
        printf("EEEEEEEEEK, will not kill children of pid 1\n");
        return -1;
    }
    max_pid = get_max_pid();
    for (i = 0; i < max_pid; i++) {
        ret = proc_get_info(i, &pi);
        if (ret == 0 && pi.ppid == pid) {
            printf(" - check it: %d is a child of %d !\n", pi.pid, pid);
            printf("kill(%d, 9)\n", pi.pid);
            kill(pi.pid, 9);
        }
    }
    return count;
}

variable_types: pid int

variable_types: count int

user_defined_types: struct proc_info { int pid; int ppid; unsigned int uid; unsigned int gid; char name[16]; }


decompiled_code:
__int64 __fastcall check_proc(unsigned int *a1, const char *a2)
{
unsigned int v2; // er12
char buf[4112]; // [rsp+0h] [rbp-1098h] BYREF
char s[32]; // [rsp+1010h] [rbp-88h] BYREF
__int64 v6[6]; // [rsp+1030h] [rbp-68h] BYREF
void *ptr; // [rsp+1060h] [rbp-38h]

v2 = -1;
if ( !kill(*a1, 0) )
{
v2 = 0;
LOG(
3LL,
"process:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
(const char *)a1 + 40,
*a1,
a1[2],
a1[3],
a1[4],
a1[5],
a1[6],
a1[7],
a1[8],
a1[9]);
LOG(
3LL,
"parent:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
a2 + 40,
*(unsigned int *)a2,
*((unsigned int *)a2 + 2),
*((unsigned int *)a2 + 3),
*((unsigned int *)a2 + 4),
*((unsigned int *)a2 + 5),
*((unsigned int *)a2 + 6),
*((unsigned int *)a2 + 7),
*((unsigned int *)a2 + 8),
*((unsigned int *)a2 + 9));
memset(buf, 0, 0x1008uLL);
snprintf(s, 0x20uLL, "/proc/%d/exe", *a1);
readlink(s, buf, 0x1007uLL);
user_get_info(*((unsigned int *)a2 + 2), v6);
if ( *((_DWORD *)a2 + 2) || *(_DWORD *)a2 == 1 )
{
if ( (unsigned int)wlist_match(v6, buf) )
{
puts("wlist match");
}
else
{
puts("ALERT!");
printf("kill_children(%d)..\n", *a1);
kill_children(*a1);
printf("kill(%d, 9)\n", *a1);
kill(*a1, 9);
v2 = 1;
}
}
else
{
printf("ignoring root spawned proc, pid: %d\n", *a1);
}
if ( ptr )
free(ptr);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, const char *a2)
{
unsigned int v2; // er12
char buf[4112]; // [rsp+0h] [rbp-1098h] BYREF
char s[32]; // [rsp+1010h] [rbp-88h] BYREF
__int64 v6[6]; // [rsp+1030h] [rbp-68h] BYREF
void *ptr; // [rsp+1060h] [rbp-38h]

v2 = -1;
if ( !kill(*a1, 0) )
{
v2 = 0;
LOG(
3LL,
"process:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
(const char *)a1 + 40,
*a1,
a1[2],
a1[3],
a1[4],
a1[5],
a1[6],
a1[7],
a1[8],
a1[9]);
LOG(
3LL,
"parent:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
a2 + 40,
*(unsigned int *)a2,
*((unsigned int *)a2 + 2),
*((unsigned int *)a2 + 3),
*((unsigned int *)a2 + 4),
*((unsigned int *)a2 + 5),
*((unsigned int *)a2 + 6),
*((unsigned int *)a2 + 7),
*((unsigned int *)a2 + 8),
*((unsigned int *)a2 + 9));
memset(buf, 0, 0x1008uLL);
snprintf(s, 0x20uLL, "/proc/%d/exe", *a1);
readlink(s, buf, 0x1007uLL);
user_get_info(*((unsigned int *)a2 + 2), v6);
if ( *((_DWORD *)a2 + 2) || *(_DWORD *)a2 == 1 )
{
if ( (unsigned int)wlist_match(v6, buf) )
{
puts("wlist match");
}
else
{
puts("ALERT!");
printf("kill_children(%d)..\n", *a1);
func0(*a1);
printf("kill(%d, 9)\n", *a1);
kill(*a1, 9);
v2 = 1;
}
}
else
{
printf("ignoring root spawned proc, pid: %d\n", *a1);
}
if ( ptr )
free(ptr);
}
return v2;
}

original_code:
int check_proc(const pinfo_t *pi, const pinfo_t *ppi)
{
    uinfo_t uinfo;
    int ret;
    char link_path[32];
    char path[4096 + 8];
    if (kill(pi->pid, 0) != 0)
        return -1;
    LOG(3, "process:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
            pi->name, pi->pid, pi->uid, pi->euid, pi->suid, pi->fsuid,
            pi->gid, pi->egid, pi->sgid, pi->fsgid);
    LOG(3, "parent:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
            ppi->name, ppi->pid, ppi->uid, ppi->euid, ppi->suid, ppi->fsuid,
            ppi->gid, ppi->egid, ppi->sgid, ppi->fsgid);
    memset(path, '\0', sizeof path);
    snprintf(link_path, sizeof link_path, "/proc/%d/exe", pi->pid);
    readlink(link_path, path, sizeof path - 1);
    user_get_info(ppi->uid, &uinfo);
    if (ppi->uid == 0 && ppi->pid != 1) {
        printf("ignoring root spawned proc, pid: %d\n", pi->pid);
        ret = 0;
    }
    else if (wlist_match(&uinfo, path)) {
        printf("wlist match\n");
        ret = 0;
    }
    else {
        printf("ALERT!\n");
        printf("kill_children(%d)..\n", pi->pid);
        kill_children(pi->pid);
        printf("kill(%d, 9)\n", pi->pid);
        kill(pi->pid, 9);
        ret = 1;
    }
    if (uinfo.groups != ((void *)0))
        free(uinfo.groups);
    return ret;
}

canonical_original_code:
int check_proc(struct proc_info *pi, struct proc_info *ppi)
{
    struct user_info uinfo;
    int ret;
    char link_path[32];
    char path[-1];
    if (kill(pi->pid, 0) != 0)
        return -1;
    LOG(3, "process:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
            pi->name, pi->pid, pi->uid, pi->euid, pi->suid, pi->fsuid,
            pi->gid, pi->egid, pi->sgid, pi->fsgid);
    LOG(3, "parent:%s[%d] uids:%d,%d,%d,%d gids:%d,%d,%d,%d",
            ppi->name, ppi->pid, ppi->uid, ppi->euid, ppi->suid, ppi->fsuid,
            ppi->gid, ppi->egid, ppi->sgid, ppi->fsgid);
    memset(path, '\0', sizeof path);
    snprintf(link_path, sizeof link_path, "/proc/%d/exe", pi->pid);
    readlink(link_path, path, sizeof path - 1);
    user_get_info(ppi->uid, &uinfo);
    if (ppi->uid == 0 && ppi->pid != 1) {
        printf("ignoring root spawned proc, pid: %d\n", pi->pid);
        ret = 0;
    }
    else if (wlist_match(&uinfo, path)) {
        printf("wlist match\n");
        ret = 0;
    }
    else {
        printf("ALERT!\n");
        printf("kill_children(%d)..\n", pi->pid);
        kill_children(pi->pid);
        printf("kill(%d, 9)\n", pi->pid);
        kill(pi->pid, 9);
        ret = 1;
    }
    if (uinfo.groups != ((void *)0))
        free(uinfo.groups);
    return ret;
}

variable_types: struct proc_info { int pid; int ppid; unsigned int uid; unsigned int gid; char name[16]; } *

variable_types:
ppi
struct proc_info *

variable_types: struct user_info { unsigned int uid; unsigned int gid; char name[-1]; unsigned int *groups; unsigned long ngroups; }

variable_types:
path
char [-1]


decompiled_code:
_BOOL8 __fastcall proc_is_root(_DWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1[2] && a1[3] && a1[4] && a1[5] && a1[6] && a1[7] )
{
if ( a1[8] )
result = a1[9] == 0;
}
return result;
}

original_code:
int proc_is_root(const pinfo_t *pi)
{
    if (pi->uid == 0) return 1;
    if (pi->euid == 0) return 1;
    if (pi->suid == 0) return 1;
    if (pi->fsuid == 0) return 1;
    if (pi->gid == 0) return 1;
    if (pi->egid == 0) return 1;
    if (pi->sgid == 0) return 1;
    if (pi->fsgid == 0) return 1;
    return 0;
}

canonical_original_code:
int proc_is_root(struct proc_info *pi)
{
    if (pi->uid == 0) return 1;
    if (pi->euid == 0) return 1;
    if (pi->suid == 0) return 1;
    if (pi->fsuid == 0) return 1;
    if (pi->gid == 0) return 1;
    if (pi->egid == 0) return 1;
    if (pi->sgid == 0) return 1;
    if (pi->fsgid == 0) return 1;
    return 0;
}


decompiled_code:
__int64 __fastcall user_name_to_uid(const char *a1)
{
struct passwd *v1; // rax
__int64 result; // rax

v1 = getpwnam(a1);
if ( v1 )
result = v1->pw_uid;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
struct passwd *v1; // rax
__int64 result; // rax

v1 = getpwnam(a1);
if ( v1 )
result = v1->pw_uid;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
uid_t user_name_to_uid(const char *name)
{
    struct passwd *pwent;
    if ((pwent = getpwnam(name)) == ((void *)0))
        return -1;
    return pwent->pw_uid;
}

canonical_original_code:
unsigned int user_name_to_uid(char *name)
{
    struct passwd *pwent;
    if ((pwent = getpwnam(name)) == ((void *)0))
        return -1;
    return pwent->pw_uid;
}

variable_types:
name
char *

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

return_type: unsigned int


decompiled_code:
__int64 __fastcall user_group_to_gid(char *s2)
{
struct group *v1; // rax
struct group *v2; // rbx

setgrent();
while ( 1 )
{
v1 = getgrent();
v2 = v1;
if ( !v1 )
break;
if ( !strcmp(v1->gr_name, s2) )
{
endgrent();
return v2->gr_gid;
}
}
endgrent();
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2)
{
struct group *v1; // rax
struct group *v2; // rbx

setgrent();
while ( 1 )
{
v1 = getgrent();
v2 = v1;
if ( !v1 )
break;
if ( !strcmp(v1->gr_name, s2) )
{
endgrent();
return v2->gr_gid;
}
}
endgrent();
return 0xFFFFFFFFLL;
}

original_code:
gid_t user_group_to_gid(const char *name)
{
  struct group *grent;
  setgrent();
  while ((grent = getgrent()) != ((void *)0)) {
    if (!strcmp(grent->gr_name, name)) {
      endgrent();
      return grent->gr_gid;
    }
  }
  endgrent();
  return -1;
}

canonical_original_code:
unsigned int user_group_to_gid(char *name)
{
  struct group *grent;
  setgrent();
  while ((grent = getgrent()) != ((void *)0)) {
    if (!strcmp(grent->gr_name, name)) {
      endgrent();
      return grent->gr_gid;
    }
  }
  endgrent();
  return -1;
}

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *


decompiled_code:
__int64 __fastcall user_get_info(__uid_t a1, __int64 a2)
{
char *v3; // r12
struct passwd *v4; // rax
struct passwd *v5; // rbp
__gid_t v6; // esi
__int64 result; // rax
__gid_t *v8; // rax
int ngroups[7]; // [rsp+Ch] [rbp-1Ch] BYREF

ngroups[0] = 0;
v3 = (char *)(a2 + 8);
strcpy((char *)(a2 + 8), "NOUSER");
*(_DWORD *)a2 = a1;
*(_DWORD *)(a2 + 4) = -1;
*(_QWORD *)(a2 + 48) = 0LL;
*(_QWORD *)(a2 + 56) = 0LL;
v4 = getpwuid(a1);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = v4;
snprintf(v3, 0x21uLL, v4->pw_name);
*(_DWORD *)a2 = v5->pw_uid;
v6 = v5->pw_gid;
*(_DWORD *)(a2 + 4) = v6;
*(_QWORD *)(a2 + 56) = 0LL;
getgrouplist(v3, v6, 0LL, ngroups);
result = 0LL;
if ( ngroups[0] > 1 )
{
v8 = (__gid_t *)calloc(ngroups[0], 4uLL);
*(_QWORD *)(a2 + 48) = v8;
getgrouplist(v3, *(_DWORD *)(a2 + 4), v8, ngroups);
*(_QWORD *)(a2 + 56) = ngroups[0];
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__uid_t a1, __int64 a2)
{
char *v3; // r12
struct passwd *v4; // rax
struct passwd *v5; // rbp
__gid_t v6; // esi
__int64 result; // rax
__gid_t *v8; // rax
int ngroups[7]; // [rsp+Ch] [rbp-1Ch] BYREF

ngroups[0] = 0;
v3 = (char *)(a2 + 8);
strcpy((char *)(a2 + 8), "NOUSER");
*(_DWORD *)a2 = a1;
*(_DWORD *)(a2 + 4) = -1;
*(_QWORD *)(a2 + 48) = 0LL;
*(_QWORD *)(a2 + 56) = 0LL;
v4 = getpwuid(a1);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = v4;
snprintf(v3, 0x21uLL, v4->pw_name);
*(_DWORD *)a2 = v5->pw_uid;
v6 = v5->pw_gid;
*(_DWORD *)(a2 + 4) = v6;
*(_QWORD *)(a2 + 56) = 0LL;
getgrouplist(v3, v6, 0LL, ngroups);
result = 0LL;
if ( ngroups[0] > 1 )
{
v8 = (__gid_t *)calloc(ngroups[0], 4uLL);
*(_QWORD *)(a2 + 48) = v8;
getgrouplist(v3, *(_DWORD *)(a2 + 4), v8, ngroups);
*(_QWORD *)(a2 + 56) = ngroups[0];
result = 0LL;
}
return result;
}

original_code:
int user_get_info(const uid_t uid, uinfo_t *uinfo)
{
    struct passwd *pwent;
    int ng = 0;
    sprintf(uinfo->name, "NOUSER");
    uinfo->uid = uid;
    uinfo->gid = -1;
    uinfo->groups = ((void *)0);
    uinfo->ngroups = 0;
    if ((pwent = getpwuid(uid)) == ((void *)0))
        return -1;
    snprintf(uinfo->name, 32 + 1, pwent->pw_name);
    uinfo->uid = pwent->pw_uid;
    uinfo->gid = pwent->pw_gid;
    uinfo->ngroups = 0;
    getgrouplist(uinfo->name, uinfo->gid, ((void *)0), &ng);
    if (ng > 1) {
        uinfo->groups = calloc(ng, sizeof (gid_t));
        getgrouplist(uinfo->name, uinfo->gid, uinfo->groups, &ng);
        uinfo->ngroups = ng;
    }
    return 0;
}

canonical_original_code:
int user_get_info(unsigned int uid, struct user_info *uinfo)
{
    struct passwd *pwent;
    int ng = 0;
    sprintf(uinfo->name, "NOUSER");
    uinfo->uid = uid;
    uinfo->gid = -1;
    uinfo->groups = ((void *)0);
    uinfo->ngroups = 0;
    if ((pwent = getpwuid(uid)) == ((void *)0))
        return -1;
    snprintf(uinfo->name, 32 + 1, pwent->pw_name);
    uinfo->uid = pwent->pw_uid;
    uinfo->gid = pwent->pw_gid;
    uinfo->ngroups = 0;
    getgrouplist(uinfo->name, uinfo->gid, ((void *)0), &ng);
    if (ng > 1) {
        uinfo->groups = calloc(ng, sizeof (gid_t));
        getgrouplist(uinfo->name, uinfo->gid, uinfo->groups, &ng);
        uinfo->ngroups = ng;
    }
    return 0;
}

variable_types:
uid
unsigned int

variable_types: struct user_info { unsigned int uid; unsigned int gid; char name[-1]; unsigned int *groups; unsigned long ngroups; } *

variable_types: ng int

user_defined_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; }


decompiled_code:
__int64 __fastcall user_in_group(__int64 a1, int a2)
{
__int64 v2; // rdx
__int64 v3; // rax

v2 = *(_QWORD *)(a1 + 56);
if ( !v2 )
return 0LL;
v3 = 0LL;
while ( *(_DWORD *)(*(_QWORD *)(a1 + 48) + 4 * v3) != a2 )
{
if ( ++v3 == v2 )
return 0LL;
}
return 1LL;
}

original_code:
int user_in_group(const uinfo_t *uinfo, const gid_t gid)
{
    size_t i;
    for (i = 0; i < uinfo->ngroups; i++) {
        if (*(uinfo->groups + i) == gid)
            return 1;
    }
    return 0;
}

canonical_original_code:
int user_in_group(struct user_info *uinfo, unsigned int gid)
{
    unsigned long i;
    for (i = 0; i < uinfo->ngroups; i++) {
        if (*(uinfo->groups + i) == gid)
            return 1;
    }
    return 0;
}

variable_types:
gid
unsigned int

variable_types:
i
unsigned long

user_defined_types: struct user_info { unsigned int uid; unsigned int gid; char name[-1]; unsigned int *groups; unsigned long ngroups; }


original_code:
wlnode_t* wlist_get_ptr(void)
{
    static wlnode_t *ptr = ((void *)0);
    if (ptr == ((void *)0)) {
        ptr = malloc(sizeof (wlnode_t));
        ptr->next = ((void *)0);
        ptr->path[0] = '\0';
    }
    return ptr;
}

canonical_original_code:
struct wlist_node *wlist_get_ptr()
{
    static struct wlist_node *ptr = ((void *)0);
    if (ptr == ((void *)0)) {
        ptr = malloc(sizeof (wlnode_t));
        ptr->next = ((void *)0);
        ptr->path[0] = '\0';
    }
    return ptr;
}

variable_types: struct wlist_node { char path[4096]; unsigned int *uids; unsigned int *gids; unsigned long nuids; struct wlist_node *next; } *


decompiled_code:
int wlist_debug_dump_list()
{
__int64 v0; // rax
const char *v1; // rbx
unsigned __int64 v2; // rbp
unsigned __int64 v3; // rbp

v0 = wlist_get_ptr();
v1 = (const char *)v0;
if ( v0 )
{
do
{
if ( *v1 )
{
printf("+ %p\n", v1);
printf(" path: %s\n", v1);
printf(" uids (%d): ", *((_QWORD *)v1 + 514));
if ( *((_QWORD *)v1 + 514) )
{
v2 = 0LL;
do
printf("%d ", *(unsigned int *)(*((_QWORD *)v1 + 512) + 4 * v2++));
while ( *((_QWORD *)v1 + 514) > v2 );
}
printf("\n gids (%d): ", *((_QWORD *)v1 + 515));
if ( *((_QWORD *)v1 + 515) )
{
v3 = 0LL;
do
printf("%d ", *(unsigned int *)(*((_QWORD *)v1 + 513) + 4 * v3++));
while ( *((_QWORD *)v1 + 515) > v3 );
}
LODWORD(v0) = putchar(10);
}
v1 = (const char *)*((_QWORD *)v1 + 516);
}
while ( v1 );
}
return v0;
}

original_code:
void wlist_debug_dump_list(void)
{
    wlnode_t *ptr;
    size_t i;
    ptr = wlist_get_ptr();
    for (;ptr != ((void *)0); ptr = ptr->next) {
        if (!*(ptr->path))
            continue;
        printf("+ %p\n", ptr);
        printf("  path: %s\n", ptr->path);
        printf("  uids (%d): ", ptr->nuids);
        for (i = 0; i < ptr->nuids; i++)
            printf("%d ", *(ptr->uids + i));
        printf("\n  gids (%d): ", ptr->ngids);
        for (i = 0; i < ptr->ngids; i++)
            printf("%d ", *(ptr->gids + i));
        printf("\n");
    }
}

canonical_original_code:
void wlist_debug_dump_list()
{
    struct wlist_node *ptr;
    unsigned long i;
    ptr = wlist_get_ptr();
    for (;ptr != ((void *)0); ptr = ptr->next) {
        if (!*(ptr->path))
            continue;
        printf("+ %p\n", ptr);
        printf("  path: %s\n", ptr->path);
        printf("  uids (%d): ", ptr->nuids);
        for (i = 0; i < ptr->nuids; i++)
            printf("%d ", *(ptr->uids + i));
        printf("\n  gids (%d): ", ptr->ngids);
        for (i = 0; i < ptr->ngids; i++)
            printf("%d ", *(ptr->gids + i));
        printf("\n");
    }
}

return_type: void

user_defined_types: struct wlist_node { char path[4096]; unsigned int *uids; unsigned int *gids; unsigned long nuids; struct wlist_node *next; }


original_code:
int wlist_match(const uinfo_t *uinfo, const char *path)
{
    wlnode_t *ptr;
    size_t i, j;
    ptr = wlist_get_ptr();
    for (; ptr != ((void *)0); ptr = ptr->next) {
        if (!strcmp(ptr->path, path)) {
            for (i = 0; i < ptr->nuids; i++) {
                if (uinfo->uid == *(ptr->uids + i))
                    return 1;
            }
            for (i = 0; i < ptr->ngids; i++) {
                for (j = 0; j < uinfo->ngroups; j++) {
                    if (*(uinfo->groups + j) == *(ptr->gids + i))
                        return 2;
                }
            }
        }
    }
    return 0;
}

canonical_original_code:
int wlist_match(struct user_info *uinfo, char *path)
{
    struct wlist_node *ptr;
    unsigned long i; unsigned long j;
    ptr = wlist_get_ptr();
    for (; ptr != ((void *)0); ptr = ptr->next) {
        if (!strcmp(ptr->path, path)) {
            for (i = 0; i < ptr->nuids; i++) {
                if (uinfo->uid == *(ptr->uids + i))
                    return 1;
            }
            for (i = 0; i < ptr->ngids; i++) {
                for (j = 0; j < uinfo->ngroups; j++) {
                    if (*(uinfo->groups + j) == *(ptr->gids + i))
                        return 2;
                }
            }
        }
    }
    return 0;
}

variable_types:
path
char *

variable_types:
j
unsigned long


LOG

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

va_start(arg, a2);
fprintf(stdout, "LOG (%d): ", a1);
vfprintf(stdout, a2, arg);
return puts(&s);
}

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

va_start(arg, a2);
fprintf(stdout, "LOG (%d): ", a1);
vfprintf(stdout, a2, arg);
return puts(&s);
}

original_code:
void LOG(const int level, char *fmt, ...)
{
    va_list args;
    fprintf(stdout, "LOG (%d): ", level);
    __builtin_va_start(args,fmt);
    vfprintf(stdout, fmt, args);
    __builtin_va_end(args);
    puts("");
}

canonical_original_code:
void LOG(int level, char *fmt, ...)
{
    __builtin_va_list args;
    fprintf(stdout, "LOG (%d): ", level);
    __builtin_va_start(args,fmt);
    vfprintf(stdout, fmt, args);
    __builtin_va_end(args);
    puts("");
}

variable_types: level int

variable_types:
fmt
char *


decompiled_code:
double current_time()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_sec) * 1000000.0 + (double)SLODWORD(v1.tv_usec);
}

canonical_decompiled_code:
double func0()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_sec) * 1000000.0 + (double)SLODWORD(v1.tv_usec);
}

original_code:
double current_time(void)
{
 double timestamp;
 struct timeval tv;
 gettimeofday(&tv, 0);
 timestamp = (double)((double)(tv.tv_sec*1e6) +(double)tv.tv_usec);
 return timestamp;
}

canonical_original_code:
double current_time()
{
 double timestamp;
 struct timeval tv;
 gettimeofday(&tv, 0);
 timestamp = (double)((double)(tv.tv_sec*1e6) +(double)tv.tv_usec);
 return timestamp;
}

variable_types: timestamp double

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

return_type: double


sighandler

decompiled_code:
void sighandler()
{
resend = 1;
}

canonical_decompiled_code:
void func7()
{
resend = 1;
}

original_code:
void sighandler(int signo){
 resend = 1;
}

variable_types: signo int


diep

decompiled_code:
void __fastcall diep(const char *a1)
{
perror(a1);
}

canonical_decompiled_code:
void __fastcall func9(const char *a1)
{
perror(a1);
}

original_code:
void diep(char *a){
 perror(a);
 return ;
}

variable_types:
a
char *


decompiled_code:
__int64 __fastcall encode_char(__int64 a1, __int64 a2)
{
__int64 i; // rax

puts("encode char:");
for ( i = 0LL; i != 1000; ++i )
*(_BYTE *)(a1 + i) = *(_BYTE *)(a2 + i);
return a1 + 1000;
}

original_code:
unsigned char * encode_char(unsigned char *buffer, char *value){
 printf("encode char:\n");
 int i = 0;
 for(i = 0;i < 1000; i++){
  buffer[i] = value[i];
 }
 return buffer + 1000;
}

canonical_original_code:
unsigned char *encode_char(unsigned char *buffer, char *value){
 printf("encode char:\n");
 int i = 0;
 for(i = 0;i < 1000; i++){
  buffer[i] = value[i];
 }
 return buffer + 1000;
}

variable_types:
buffer
unsigned char *

variable_types:
value
char *

variable_types: i int

return_type: unsigned char *


decompiled_code:
__int64 __fastcall encode_struct(_BYTE *a1, int a2, __int64 a3)
{
_BYTE *v4; // rax

v4 = encode_int(a1, a2);
return encode_char((__int64)v4, a3);
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1, int a2, __int64 a3)
{
_BYTE *v4; // rax

v4 = encode_int(a1, a2);
return func10((__int64)v4, a3);
}

original_code:
unsigned char * encode_struct(unsigned char *buffer, int a, char *b){
 buffer = encode_int(buffer, a);
 buffer = encode_char(buffer, b);
 return buffer;
}

canonical_original_code:
unsigned char *encode_struct(unsigned char *buffer, int a, char *b){
 buffer = encode_int(buffer, a);
 buffer = encode_char(buffer, b);
 return buffer;
}

variable_types: a int

variable_types:
b
char *


decompiled_code:
__int64 __fastcall encode_data_struct(_BYTE *a1, int a2, int a3, __int64 a4)
{
_BYTE *v5; // rax
_BYTE *v6; // rax

v5 = encode_int(a1, a3);
v6 = encode_int(v5, a2);
return encode_char((__int64)v6, a4);
}

canonical_decompiled_code:
__int64 __fastcall func12(_BYTE *a1, int a2, int a3, __int64 a4)
{
_BYTE *v5; // rax
_BYTE *v6; // rax

v5 = encode_int(a1, a3);
v6 = encode_int(v5, a2);
return func10((__int64)v6, a4);
}

original_code:
unsigned char * encode_data_struct(unsigned char *buffer, int length, int sequence, char *data){
 buffer = encode_int(buffer, sequence);
 buffer = encode_int(buffer, length);
 buffer = encode_char(buffer, data);
 return buffer;
}

canonical_original_code:
unsigned char *encode_data_struct(unsigned char *buffer, int length, int sequence, char *data){
 buffer = encode_int(buffer, sequence);
 buffer = encode_int(buffer, length);
 buffer = encode_char(buffer, data);
 return buffer;
}

variable_types: length int

variable_types: sequence int

variable_types:
data
char *


decompiled_code:
int __fastcall send_connection_packet(char *s, int fd)
{
int v2; // ebx
int i; // er13
int v4; // eax
size_t j; // rbx
size_t v6; // rbx
int result; // eax
_BYTE v8[1022]; // [rsp+0h] [rbp-818h] BYREF
char v9[2]; // [rsp+3FEh] [rbp-41Ah] BYREF
char buf[1048]; // [rsp+400h] [rbp-418h] BYREF

v9[1] = 0;
v2 = open(s, 0);
if ( v2 == -1 )
diep("open file failed");
for ( i = 0; ; i += v4 )
{
v4 = read(v2, buf, 0x3E8uLL);
if ( v4 <= 0 )
break;
}
for ( j = 0LL; strlen(s) > j; ++j )
buf[j] = s[j];
v6 = encode_struct(v8, i, (__int64)buf) - (_QWORD)v8;
if ( sendto(fd, v8, v6, 0, &si_other, slen) == -1 )
diep("connection packet error\n");
while ( recvfrom(fd, v9, 1uLL, 0, &si_other, &slen) < 0 )
sendto(fd, v8, v6, 0, &si_other, slen);
result = printf("the recv ack is : %s\n", v9);
ackType = 1;
expectNum = 1;
return result;
}

canonical_decompiled_code:
int __fastcall func13(char *s, int fd)
{
int v2; // ebx
int i; // er13
int v4; // eax
size_t j; // rbx
size_t v6; // rbx
int result; // eax
_BYTE v8[1022]; // [rsp+0h] [rbp-818h] BYREF
char v9[2]; // [rsp+3FEh] [rbp-41Ah] BYREF
char buf[1048]; // [rsp+400h] [rbp-418h] BYREF

v9[1] = 0;
v2 = open(s, 0);
if ( v2 == -1 )
func9("open file failed");
for ( i = 0; ; i += v4 )
{
v4 = read(v2, buf, 0x3E8uLL);
if ( v4 <= 0 )
break;
}
for ( j = 0LL; strlen(s) > j; ++j )
buf[j] = s[j];
v6 = func11(v8, i, (__int64)buf) - (_QWORD)v8;
if ( sendto(fd, v8, v6, 0, &si_other, slen) == -1 )
func9("connection packet error\n");
while ( recvfrom(fd, v9, 1uLL, 0, &si_other, &slen) < 0 )
sendto(fd, v8, v6, 0, &si_other, slen);
result = printf("the recv ack is : %s\n", v9);
ackType = 1;
expectNum = 1;
return result;
}

original_code:
void send_connection_packet(char *fileName, int s){
 char readBuffer[1000],ack[2];
 int f, fileLength = 0, i, j;
 unsigned char buf[1000 + 8], *ptr;
 ack[1] = '\0';
 if((f = open(fileName, 00)) == -1)
  diep("open file failed");
 while((i = read(f, readBuffer, 1000)) > 0){
  fileLength += i;
 }
 for(j = 0;j < strlen(fileName); j++)
  readBuffer[j] = fileName[j];
 ptr = encode_struct(buf, fileLength, readBuffer);
 if(sendto(s, buf, ptr - buf, 0, &si_other, slen) == -1)
  diep("connection packet error\n");
 while(recvfrom(s, ack, 1, 0, &si_other, &slen) < 0){
  sendto(s, buf, ptr - buf, 0, &si_other, slen);
 }
 printf("the recv ack is : %s\n", ack);
 ackType = 1;
 expectNum = 1;
}

canonical_original_code:
void send_connection_packet(char *fileName, int s){
 char readBuffer[1000]; char ack[2];
 int f; int fileLength = 0; int i; int j;
 unsigned char buf[-1]; unsigned char *ptr;
 ack[1] = '\0';
 if((f = open(fileName, 00)) == -1)
  diep("open file failed");
 while((i = read(f, readBuffer, 1000)) > 0){
  fileLength += i;
 }
 for(j = 0;j < strlen(fileName); j++)
  readBuffer[j] = fileName[j];
 ptr = encode_struct(buf, fileLength, readBuffer);
 if(sendto(s, buf, ptr - buf, 0, &si_other, slen) == -1)
  diep("connection packet error\n");
 while(recvfrom(s, ack, 1, 0, &si_other, &slen) < 0){
  sendto(s, buf, ptr - buf, 0, &si_other, slen);
 }
 printf("the recv ack is : %s\n", ack);
 ackType = 1;
 expectNum = 1;
}

variable_types:
fileName
char *

variable_types: s int

variable_types:
readBuffer
char [1000]

variable_types:
ack
char [2]

variable_types: f int

variable_types: fileLength int

variable_types: j int

variable_types:
buf
unsigned char [-1]

variable_types:
ptr
unsigned char *


decompiled_code:
void __fastcall send_end_packet(int fd)
{
char v1[2]; // [rsp+Ch] [rbp-Ch] BYREF
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

puts("send_end_packet");
buf[0] = 49;
buf[1] = 0;
v1[1] = 0;
if ( sendto(fd, buf, 1uLL, 0, &si_other, slen) == -1 )
diep("end packet error\n");
while ( recvfrom(fd, v1, 1uLL, 0, &si_other, &slen) < 0 )
{
if ( sendto(fd, buf, 1uLL, 0, &si_other, slen) == -1 )
diep("end packet error\n");
}
if ( v1[0] != 49 )
diep("wrong fin\n");
fin = 1;
}

canonical_decompiled_code:
void __fastcall func14(int fd)
{
char v1[2]; // [rsp+Ch] [rbp-Ch] BYREF
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

puts("send_end_packet");
buf[0] = 49;
buf[1] = 0;
v1[1] = 0;
if ( sendto(fd, buf, 1uLL, 0, &si_other, slen) == -1 )
func9("end packet error\n");
while ( recvfrom(fd, v1, 1uLL, 0, &si_other, &slen) < 0 )
{
if ( sendto(fd, buf, 1uLL, 0, &si_other, slen) == -1 )
func9("end packet error\n");
}
if ( v1[0] != 49 )
func9("wrong fin\n");
fin = 1;
}

original_code:
void send_end_packet(int s){
 printf("send_end_packet\n");
 char end[2], rec[2];
 end[0] = '1';
 end[1] = '\0';
 rec[1] = '\0';
 if(sendto(s, end, 1, 0, &si_other, slen) == -1)
   diep("end packet error\n");
 while(recvfrom(s, rec, 1, 0, &si_other, &slen) < 0){
  if(sendto(s, end, 1, 0, &si_other, slen) == -1)
   diep("end packet error\n");
 }
 if(rec[0] != '1')
  diep("wrong fin\n");
 fin = 1;
}

canonical_original_code:
void send_end_packet(int s){
 printf("send_end_packet\n");
 char end[2]; char rec[2];
 end[0] = '1';
 end[1] = '\0';
 rec[1] = '\0';
 if(sendto(s, end, 1, 0, &si_other, slen) == -1)
   diep("end packet error\n");
 while(recvfrom(s, rec, 1, 0, &si_other, &slen) < 0){
  if(sendto(s, end, 1, 0, &si_other, slen) == -1)
   diep("end packet error\n");
 }
 if(rec[0] != '1')
  diep("wrong fin\n");
 fin = 1;
}

variable_types:
end
char [2]

variable_types:
rec
char [2]


decompiled_code:
__int64 __fastcall decode_int(unsigned __int8 *a1)
{
puts("decode int");
return a1[3] + (((((*a1 << 8) + a1[1]) << 8) + (unsigned int)a1[2]) << 8);
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int8 *a1)
{
puts("decode int");
return a1[3] + (((((*a1 << 8) + a1[1]) << 8) + (unsigned int)a1[2]) << 8);
}

original_code:
unsigned int decode_int(unsigned char *buffer){
 unsigned int ans = 0;
 int i = 0;
 printf("%s\n", "decode int");
 for(i = 0; i < 4;i++){
  ans += buffer[i];
  if(i != 3) ans = ans << 8;
 }
 return ans;
}

variable_types:
buffer
unsigned char *

variable_types:
ans
unsigned int

variable_types: i int


decompiled_code:
__int64 __fastcall decode_ack(unsigned __int8 *a1)
{
unsigned int v1; // eax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = decode_int(a1);
printf("expect sequence: %d\n", v1);
return decode_int(a1);
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned __int8 *a1)
{
unsigned int v1; // eax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = func15(a1);
printf("expect sequence: %d\n", v1);
return func15(a1);
}

original_code:
int decode_ack(char *ack){
 if(ack != ((void *)0)){
  printf("expect sequence: %d\n", decode_int(ack));
  return decode_int(ack);
 }
 return -1;
}

variable_types:
ack
char *


decompiled_code:
void __fastcall send_data_packet(char *file, int a2)
{
int v2; // er14
int v3; // er13
int v4; // eax
int v5; // ebx
__int64 v6; // r12
_BYTE v7[1019]; // [rsp+0h] [rbp-818h] BYREF
unsigned __int8 buf[5]; // [rsp+3FBh] [rbp-41Dh] BYREF
char v9[1048]; // [rsp+400h] [rbp-418h] BYREF

puts("send_data_packet");
buf[4] = 0;
if ( expectNum <= 0 )
diep("can not start sending data packet \n");
v2 = open(file, 0);
if ( v2 == -1 )
diep("can not open the file when sending data packets\n");
v3 = 0;
while ( 1 )
{
v4 = read(v2, v9, 0x3E8uLL);
v5 = v4;
if ( v4 <= 0 )
break;
v9[v4] = 0;
printf("reading: %s\n", v9);
v6 = encode_data_struct(v7, v5, expectNum, (__int64)v9);
expectNum += v5;
v3 += v5;
while ( 1 )
{
if ( sendto(a2, v7, v6 - (_QWORD)v7, 0, &si_other, slen) < 0 )
diep("send error\n");
alarm(5u);
if ( recvfrom(a2, buf, 4uLL, 0, &si_other, &slen) >= 0 && (unsigned int)decode_ack(buf) == expectNum )
break;
puts("recv error");
}
alarm(0);
}
if ( expectNum - 1 == v3 )
ackType = 2;
else
diep("data sending error\n");
}

canonical_decompiled_code:
void __fastcall func17(char *file, int a2)
{
int v2; // er14
int v3; // er13
int v4; // eax
int v5; // ebx
__int64 v6; // r12
_BYTE v7[1019]; // [rsp+0h] [rbp-818h] BYREF
unsigned __int8 buf[5]; // [rsp+3FBh] [rbp-41Dh] BYREF
char v9[1048]; // [rsp+400h] [rbp-418h] BYREF

puts("send_data_packet");
buf[4] = 0;
if ( expectNum <= 0 )
func9("can not start sending data packet \n");
v2 = open(file, 0);
if ( v2 == -1 )
func9("can not open the file when sending data packets\n");
v3 = 0;
while ( 1 )
{
v4 = read(v2, v9, 0x3E8uLL);
v5 = v4;
if ( v4 <= 0 )
break;
v9[v4] = 0;
printf("reading: %s\n", v9);
v6 = func12(v7, v5, expectNum, (__int64)v9);
expectNum += v5;
v3 += v5;
while ( 1 )
{
if ( sendto(a2, v7, v6 - (_QWORD)v7, 0, &si_other, slen) < 0 )
func9("send error\n");
alarm(5u);
if ( recvfrom(a2, buf, 4uLL, 0, &si_other, &slen) >= 0 && (unsigned int)func16(buf) == expectNum )
break;
puts("recv error");
}
alarm(0);
}
if ( expectNum - 1 == v3 )
ackType = 2;
else
func9("data sending error\n");
}

original_code:
void send_data_packet(char *fileName, int s){
 printf("send_data_packet\n");
 int f, i, fileLength = 0;
 char readBuffer[1000 + 1], ack[5];
 unsigned char buf[1000 + 8], *ptr;
 ack[4] = '\0';
 if(expectNum < 1) diep("can not start sending data packet \n");
 if((f = open(fileName, 00)) == -1) diep("can not open the file when sending data packets\n");
 while((i = read(f, readBuffer, 1000)) > 0){
  readBuffer[i] = '\0';
  printf("reading: %s\n", readBuffer);
  ptr = encode_data_struct(buf, i, expectNum, readBuffer);
  expectNum += i;
  fileLength += i;
  while(1){
   if(sendto(s, buf, ptr - buf, 0, &si_other, slen) < 0)
    diep("send error\n");
   alarm(5);
   if(recvfrom(s, ack, sizeof(char) * 4, 0, &si_other, &slen) >= 0){
    if(expectNum == decode_ack(ack)){
     alarm(0);
     break;
    }
   }
   printf("recv error\n");
  }
 }
 if(expectNum - 1 != fileLength) diep("data sending error\n");
 else ackType = 2;
}

canonical_original_code:
void send_data_packet(char *fileName, int s){
 printf("send_data_packet\n");
 int f; int i; int fileLength = 0;
 char readBuffer[-1]; char ack[5];
 unsigned char buf[-1]; unsigned char *ptr;
 ack[4] = '\0';
 if(expectNum < 1) diep("can not start sending data packet \n");
 if((f = open(fileName, 00)) == -1) diep("can not open the file when sending data packets\n");
 while((i = read(f, readBuffer, 1000)) > 0){
  readBuffer[i] = '\0';
  printf("reading: %s\n", readBuffer);
  ptr = encode_data_struct(buf, i, expectNum, readBuffer);
  expectNum += i;
  fileLength += i;
  while(1){
   if(sendto(s, buf, ptr - buf, 0, &si_other, slen) < 0)
    diep("send error\n");
   alarm(5);
   if(recvfrom(s, ack, sizeof(char) * 4, 0, &si_other, &slen) >= 0){
    if(expectNum == decode_ack(ack)){
     alarm(0);
     break;
    }
   }
   printf("recv error\n");
  }
 }
 if(expectNum - 1 != fileLength) diep("data sending error\n");
 else ackType = 2;
}

variable_types:
readBuffer
char [-1]

variable_types:
ack
char [5]


canonical_original_code:
void diep(char *a){
 perror(a);
 return ;
}

variable_types:
a
char *


canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 *a1)
{
puts("decode int");
return a1[3] + (((((*a1 << 8) + a1[1]) << 8) + (unsigned int)a1[2]) << 8);
}

canonical_original_code:
unsigned int decode_int(unsigned char *buffer){
 unsigned int ans = 0;
 int i = 0;
 printf("%s\n", "decode int");
 for(i = 0; i < 4;i++){
  ans += buffer[i];
  if(i != 3) ans = ans << 8;
 }
 return ans;
}


decompiled_code:
ssize_t __fastcall decode_buffer(unsigned __int8 *a1)
{
unsigned int v1; // ebp
const char *v2; // rax
ssize_t result; // rax

puts("Start decoding the packet!!");
v1 = decode_int(a1);
v2 = decode_char((__int64)(a1 + 4));
printf("decoded buffer is: %s, %d\n", v2, v1);
while ( 1 )
{
result = send_ack(1u, 0xFFFFFFFFLL);
if ( (int)result >= 0 )
break;
puts("failed ack");
}
connection = 1;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(unsigned __int8 *a1)
{
unsigned int v1; // ebp
const char *v2; // rax
ssize_t result; // rax

puts("Start decoding the packet!!");
v1 = func10(a1);
v2 = decode_char((__int64)(a1 + 4));
printf("decoded buffer is: %s, %d\n", v2, v1);
while ( 1 )
{
result = func11(1u, 0xFFFFFFFFLL);
if ( (int)result >= 0 )
break;
puts("failed ack");
}
connection = 1;
return result;
}

original_code:
void decode_buffer(unsigned char *buffer){
 printf("Start decoding the packet!!\n");
 unsigned int ans = decode_int(buffer);
 buffer +=4;
 unsigned char *str = malloc(sizeof(char) * 1001);
 str = decode_char(buffer);
 printf("decoded buffer is: %s, %d\n", str, ans);
 while(send_ack(1, -1) < 0){
  printf("failed ack\n");
 }
 connection = 1;
 return;
}

variable_types:
str
unsigned char *


decompiled_code:
int __fastcall decode_fileContent(unsigned __int8 *a1)
{
ssize_t v1; // rax
__int64 v2; // rbp
__int64 v3; // rsi
int v4; // er12
const char *v5; // rax
char buf[26]; // [rsp+Eh] [rbp-1Ah] BYREF

buf[0] = 49;
buf[1] = 0;
if ( strlen((const char *)a1) == 1 )
{
do
v1 = sendto(s, buf, 1uLL, 0, &si_other, slen);
while ( v1 < 0 );
end = 1;
}
else
{
v2 = (unsigned int)decode_int(a1);
printf("buffer > 1\n, seq: %d\n", v2);
v3 = (unsigned int)expectNum;
if ( expectNum == (_DWORD)v2
&& (v4 = decode_int(a1 + 4),
v5 = decode_char((__int64)(a1 + 8)),
LODWORD(v1) = printf("Sequence: %d, %s\n", (unsigned int)v2, v5),
v3 = (unsigned int)(expectNum + v4),
expectNum += v4,
!j)
&& (_DWORD)v3 == 2001 )
{
j = 1;
}
else
{
LODWORD(v1) = send_ack(2u, v3);
}
}
return v1;
}

canonical_decompiled_code:
int __fastcall func13(unsigned __int8 *a1)
{
ssize_t v1; // rax
__int64 v2; // rbp
__int64 v3; // rsi
int v4; // er12
const char *v5; // rax
char buf[26]; // [rsp+Eh] [rbp-1Ah] BYREF

buf[0] = 49;
buf[1] = 0;
if ( strlen((const char *)a1) == 1 )
{
do
v1 = sendto(s, buf, 1uLL, 0, &si_other, slen);
while ( v1 < 0 );
end = 1;
}
else
{
v2 = (unsigned int)func10(a1);
printf("buffer > 1\n, seq: %d\n", v2);
v3 = (unsigned int)expectNum;
if ( expectNum == (_DWORD)v2
&& (v4 = func10(a1 + 4),
v5 = decode_char((__int64)(a1 + 8)),
LODWORD(v1) = printf("Sequence: %d, %s\n", (unsigned int)v2, v5),
v3 = (unsigned int)(expectNum + v4),
expectNum += v4,
!j)
&& (_DWORD)v3 == 2001 )
{
j = 1;
}
else
{
LODWORD(v1) = func11(2u, v3);
}
}
return v1;
}

original_code:
void decode_fileContent(unsigned char *buffer){
 char fuck[2];
 fuck[0] = '1';
 fuck[1] = '\0';
 if(strlen(buffer) == 1){
  while(sendto(s, fuck, sizeof(char) * 1, 0, &si_other, slen) < 0){}
  end = 1;
 }else{
  int sequence = decode_int(buffer);
  printf("buffer > 1\n, seq: %d\n", sequence);
  if(sequence == expectNum){
   buffer += 4;
   unsigned int length = decode_int(buffer);
   buffer += 4;
   printf("Sequence: %d, %s\n",sequence, decode_char(buffer));
   expectNum += length;
   if(j == 0 && expectNum == 2001) j = 1;
   else
    send_ack(2, expectNum);
  }else{
   send_ack(2, expectNum);
  }
 }
}

variable_types:
fuck
char [2]

variable_types: sequence int

variable_types:
length
unsigned int


decompiled_code:
char *__fastcall create_node(int a1, const char *a2)
{
char *v2; // rbx

v2 = (char *)malloc(0x3F8uLL);
*(_DWORD *)v2 = a1;
strcpy(v2 + 4, a2);
*((_QWORD *)v2 + 126) = 0LL;
return v2;
}

canonical_decompiled_code:
char *__fastcall func14(int a1, const char *a2)
{
char *v2; // rbx

v2 = (char *)malloc(0x3F8uLL);
*(_DWORD *)v2 = a1;
strcpy(v2 + 4, a2);
*((_QWORD *)v2 + 126) = 0LL;
return v2;
}

original_code:
recvdata* create_node(int index, char *data){
 recvdata *n = (recvdata *)malloc(sizeof(recvdata));
 n->startIndex = index;
 strcpy(n->redata, data);
 n->next = ((void *)0);
 return n;
}

canonical_original_code:
struct recvData *create_node(int index, char *data){
 struct recvData *n = (recvdata *)malloc(sizeof(recvdata));
 n->startIndex = index;
 strcpy(n->redata, data);
 n->next = ((void *)0);
 return n;
}

variable_types: index int

variable_types:
data
char *

variable_types: struct recvData { int startIndex; char redata[1001]; struct recvData *next; } *


outchar

repo: tonybaldwin/bsdcalendar

decompiled_code:
__int64 __fastcall outchar(wchar_t a1)
{
unsigned int v1; // ebx

v1 = a1;
if ( putwchar(a1) == -1 )
v1 = -1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(wchar_t a1)
{
unsigned int v1; // ebx

v1 = a1;
if ( putwchar(a1) == -1 )
v1 = -1;
return v1;
}

original_code:
int
outchar(int c)
{
 return (putwchar(c) != (0xffffffffu) ? c : (-1));
}

variable_types: c int


overstrike

decompiled_code:
wint_t overstrike()
{
int v0; // edi
char *v1; // rdx
int v2; // ebp
int v3; // eax
wchar_t *v4; // rbx
char v5; // cl
int v6; // ecx
wint_t result; // eax
wchar_t v8; // er12
wchar_t v9; // edi
wchar_t *v10; // rbx
wchar_t v11; // edi
wchar_t *v12; // rbx
wchar_t *v13; // rbx
wchar_t wc; // [rsp+0h] [rbp-418h] BYREF
char v15; // [rsp+4h] [rbp-414h] BYREF

v0 = maxcol;
if ( maxcol <= 0 )
{
v2 = 0;
v4 = &wc;
}
else
{
v1 = &v15;
v2 = 0;
v3 = 0;
do
{
v5 = obuf[3 * v3];
if ( v5 == 8 )
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = 95;
}
else if ( v5 == 16 )
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = obuf[3 * v3 + 1];
v6 = obuf[3 * v3 + 2];
v2 = 1;
if ( v6 > 1 )
v3 = v6 + v3 - 1;
}
else
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = 32;
}
++v3;
v1 += 4;
}
while ( v3 < v0 );
}
result = putwchar(13);
*v4 = 32;
do
*v4-- = 0;
while ( *v4 == 32 );
v8 = wc;
if ( wc )
{
v9 = wc;
v10 = &wc;
do
{
result = putwchar(v9);
v9 = *++v10;
}
while ( *v10 );
if ( v2 )
{
putwchar(13);
v11 = v8;
v12 = &wc;
do
{
if ( v11 == 95 )
v11 = 32;
putwchar(v11);
v11 = *++v12;
}
while ( *v12 );
putwchar(13);
v13 = &wc;
do
{
if ( v8 == 95 )
v8 = 32;
result = putwchar(v8);
v8 = *++v13;
}
while ( *v13 );
}
}
else if ( v2 )
{
putwchar(13);
result = putwchar(13);
}
return result;
}

canonical_decompiled_code:
wint_t func2()
{
int v0; // edi
char *v1; // rdx
int v2; // ebp
int v3; // eax
wchar_t *v4; // rbx
char v5; // cl
int v6; // ecx
wint_t result; // eax
wchar_t v8; // er12
wchar_t v9; // edi
wchar_t *v10; // rbx
wchar_t v11; // edi
wchar_t *v12; // rbx
wchar_t *v13; // rbx
wchar_t wc; // [rsp+0h] [rbp-418h] BYREF
char v15; // [rsp+4h] [rbp-414h] BYREF

v0 = maxcol;
if ( maxcol <= 0 )
{
v2 = 0;
v4 = &wc;
}
else
{
v1 = &v15;
v2 = 0;
v3 = 0;
do
{
v5 = obuf[3 * v3];
if ( v5 == 8 )
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = 95;
}
else if ( v5 == 16 )
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = obuf[3 * v3 + 1];
v6 = obuf[3 * v3 + 2];
v2 = 1;
if ( v6 > 1 )
v3 = v6 + v3 - 1;
}
else
{
v4 = (wchar_t *)v1;
*((_DWORD *)v1 - 1) = 32;
}
++v3;
v1 += 4;
}
while ( v3 < v0 );
}
result = putwchar(13);
*v4 = 32;
do
*v4-- = 0;
while ( *v4 == 32 );
v8 = wc;
if ( wc )
{
v9 = wc;
v10 = &wc;
do
{
result = putwchar(v9);
v9 = *++v10;
}
while ( *v10 );
if ( v2 )
{
putwchar(13);
v11 = v8;
v12 = &wc;
do
{
if ( v11 == 95 )
v11 = 32;
putwchar(v11);
v11 = *++v12;
}
while ( *v12 );
putwchar(13);
v13 = &wc;
do
{
if ( v8 == 95 )
v8 = 32;
result = putwchar(v8);
v8 = *++v13;
}
while ( *v13 );
}
}
else if ( v2 )
{
putwchar(13);
result = putwchar(13);
}
return result;
}

original_code:
void
overstrike(void)
{
 int i;
 wchar_t lbuf[256];
 wchar_t *cp = lbuf;
 int hadbold=0;
 for (i=0; i<maxcol; i++)
  switch (obuf[i].c_mode) {
  case 000:
  default:
   *cp++ = ' ';
   break;
  case 010:
   *cp++ = '_';
   break;
  case 020:
   *cp++ = obuf[i].c_char;
   if (obuf[i].c_width > 1)
    i += obuf[i].c_width - 1;
   hadbold=1;
   break;
  }
 putwchar('\r');
 for (*cp=' '; *cp==' '; cp--)
  *cp = 0;
 for (cp=lbuf; *cp; cp++)
  putwchar(*cp);
 if (hadbold) {
  putwchar('\r');
  for (cp=lbuf; *cp; cp++)
   putwchar(*cp=='_' ? ' ' : *cp);
  putwchar('\r');
  for (cp=lbuf; *cp; cp++)
   putwchar(*cp=='_' ? ' ' : *cp);
 }
}

canonical_original_code:
void overstrike()
{
 int i;
 int lbuf[256];
 int *cp = lbuf;
 int hadbold = 0;
 for (i=0; i<maxcol; i++)
  switch (obuf[i].c_mode) {
  case 000:
  default:
   *cp++ = ' ';
   break;
  case 010:
   *cp++ = '_';
   break;
  case 020:
   *cp++ = obuf[i].c_char;
   if (obuf[i].c_width > 1)
    i += obuf[i].c_width - 1;
   hadbold=1;
   break;
  }
 putwchar('\r');
 for (*cp=' '; *cp==' '; cp--)
  *cp = 0;
 for (cp=lbuf; *cp; cp++)
  putwchar(*cp);
 if (hadbold) {
  putwchar('\r');
  for (cp=lbuf; *cp; cp++)
   putwchar(*cp=='_' ? ' ' : *cp);
  putwchar('\r');
  for (cp=lbuf; *cp; cp++)
   putwchar(*cp=='_' ? ' ' : *cp);
 }
}

variable_types:
lbuf
int [256]

variable_types:
cp
int *

variable_types: hadbold int


iattr

decompiled_code:
wint_t iattr()
{
unsigned int v0; // edi
int *v1; // rdx
char *v2; // rax
wchar_t *v3; // rax
wchar_t v4; // edi
wchar_t *v5; // rbx
wchar_t wc; // [rsp+0h] [rbp-408h] BYREF
char v8[1028]; // [rsp+4h] [rbp-404h] BYREF

v0 = maxcol;
if ( maxcol <= 0 )
{
v3 = &wc;
}
else
{
v1 = obuf;
v2 = v8;
do
{
switch ( *(_BYTE *)v1 )
{
case 0:
*((_DWORD *)v2 - 1) = 32;
break;
case 1:
*((_DWORD *)v2 - 1) = 103;
break;
case 2:
*((_DWORD *)v2 - 1) = 94;
break;
case 4:
*((_DWORD *)v2 - 1) = 118;
break;
case 8:
*((_DWORD *)v2 - 1) = 95;
break;
case 0x10:
*((_DWORD *)v2 - 1) = 33;
break;
default:
*((_DWORD *)v2 - 1) = 88;
break;
}
v1 += 3;
v2 += 4;
}
while ( v2 != &v8[4 * v0] );
v3 = &wc + v0;
}
*v3 = 32;
do
*v3-- = 0;
while ( *v3 == 32 );
v4 = wc;
if ( wc )
{
v5 = &wc;
do
{
putwchar(v4);
v4 = *++v5;
}
while ( *v5 );
}
return putwchar(10);
}

canonical_decompiled_code:
wint_t func3()
{
unsigned int v0; // edi
int *v1; // rdx
char *v2; // rax
wchar_t *v3; // rax
wchar_t v4; // edi
wchar_t *v5; // rbx
wchar_t wc; // [rsp+0h] [rbp-408h] BYREF
char v8[1028]; // [rsp+4h] [rbp-404h] BYREF

v0 = maxcol;
if ( maxcol <= 0 )
{
v3 = &wc;
}
else
{
v1 = obuf;
v2 = v8;
do
{
switch ( *(_BYTE *)v1 )
{
case 0:
*((_DWORD *)v2 - 1) = 32;
break;
case 1:
*((_DWORD *)v2 - 1) = 103;
break;
case 2:
*((_DWORD *)v2 - 1) = 94;
break;
case 4:
*((_DWORD *)v2 - 1) = 118;
break;
case 8:
*((_DWORD *)v2 - 1) = 95;
break;
case 0x10:
*((_DWORD *)v2 - 1) = 33;
break;
default:
*((_DWORD *)v2 - 1) = 88;
break;
}
v1 += 3;
v2 += 4;
}
while ( v2 != &v8[4 * v0] );
v3 = &wc + v0;
}
*v3 = 32;
do
*v3-- = 0;
while ( *v3 == 32 );
v4 = wc;
if ( wc )
{
v5 = &wc;
do
{
putwchar(v4);
v4 = *++v5;
}
while ( *v5 );
}
return putwchar(10);
}

original_code:
void
iattr(void)
{
 int i;
 wchar_t lbuf[256];
 wchar_t *cp = lbuf;
 for (i=0; i<maxcol; i++)
  switch (obuf[i].c_mode) {
  case 000: *cp++ = ' '; break;
  case 001: *cp++ = 'g'; break;
  case 002: *cp++ = '^'; break;
  case 004: *cp++ = 'v'; break;
  case 010: *cp++ = '_'; break;
  case 020: *cp++ = '!'; break;
  default: *cp++ = 'X'; break;
  }
 for (*cp=' '; *cp==' '; cp--)
  *cp = 0;
 for (cp=lbuf; *cp; cp++)
  putwchar(*cp);
 putwchar('\n');
}

canonical_original_code:
void iattr()
{
 int i;
 int lbuf[256];
 int *cp = lbuf;
 for (i=0; i<maxcol; i++)
  switch (obuf[i].c_mode) {
  case 000: *cp++ = ' '; break;
  case 001: *cp++ = 'g'; break;
  case 002: *cp++ = '^'; break;
  case 004: *cp++ = 'v'; break;
  case 010: *cp++ = '_'; break;
  case 020: *cp++ = '!'; break;
  default: *cp++ = 'X'; break;
  }
 for (*cp=' '; *cp==' '; cp--)
  *cp = 0;
 for (cp=lbuf; *cp; cp++)
  putwchar(*cp);
 putwchar('\n');
}


initbuf

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

result = 0LL;
memset(obuf, 0, 0x1800uLL);
col = 0;
maxcol = 0;
mode &= 1u;
return result;
}

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

result = 0LL;
memset(obuf, 0, 0x1800uLL);
col = 0;
maxcol = 0;
mode &= 1u;
return result;
}

original_code:
void
initbuf(void)
{
 bzero((char *)obuf, sizeof (obuf));
 col = 0;
 maxcol = 0;
 mode &= 001;
}

canonical_original_code:
void initbuf()
{
 bzero((char *)obuf, sizeof (obuf));
 col = 0;
 maxcol = 0;
 mode &= 001;
}


initcap

decompiled_code:
char *initcap()
{
char *v0; // rax
char *result; // rax
_BOOL4 v2; // edx
char *v3; // rdx
char *v4; // rdx
char *v5[2]; // [rsp+8h] [rbp-10h] BYREF

v5[0] = (char *)&tcapbuf_0;
CURS_UP = tgetstr("up", v5);
CURS_RIGHT = tgetstr("ri", v5);
if ( !CURS_RIGHT )
CURS_RIGHT = tgetstr("nd", v5);
CURS_LEFT = tgetstr("le", v5);
if ( !CURS_LEFT )
{
CURS_LEFT = tgetstr("bc", v5);
if ( !CURS_LEFT )
{
if ( tgetflag("bs") )
CURS_LEFT = "\b";
}
}
ENTER_STANDOUT = tgetstr("so", v5);
EXIT_STANDOUT = (__int64)tgetstr("se", v5);
ENTER_UNDERLINE = tgetstr("us", v5);
EXIT_UNDERLINE = tgetstr("ue", v5);
ENTER_DIM = tgetstr("mh", v5);
ENTER_BOLD = tgetstr("md", v5);
ENTER_REVERSE = tgetstr("mr", v5);
v0 = tgetstr("me", v5);
EXIT_ATTRIBUTES = v0;
if ( ENTER_BOLD )
goto LABEL_5;
if ( ENTER_REVERSE )
{
ENTER_BOLD = ENTER_REVERSE;
goto LABEL_5;
}
if ( !ENTER_STANDOUT )
{
LABEL_5:
if ( ENTER_UNDERLINE )
{
if ( ENTER_DIM )
goto LABEL_7;
v3 = ENTER_STANDOUT;
if ( !ENTER_STANDOUT )
goto LABEL_8;
LABEL_21:
ENTER_DIM = v3;
if ( ENTER_REVERSE )
goto LABEL_8;
v4 = ENTER_STANDOUT;
LABEL_27:
ENTER_REVERSE = v4;
goto LABEL_8;
}
if ( ENTER_STANDOUT )
{
ENTER_UNDERLINE = ENTER_STANDOUT;
EXIT_UNDERLINE = (char *)EXIT_STANDOUT;
if ( !ENTER_DIM )
{
LABEL_19:
v3 = ENTER_STANDOUT;
goto LABEL_21;
}
}
else if ( !ENTER_DIM )
{
goto LABEL_8;
}
goto LABEL_7;
}
ENTER_BOLD = ENTER_STANDOUT;
if ( !ENTER_UNDERLINE )
{
ENTER_UNDERLINE = ENTER_STANDOUT;
EXIT_UNDERLINE = (char *)EXIT_STANDOUT;
if ( ENTER_DIM )
{
LABEL_26:
v4 = ENTER_STANDOUT;
if ( !ENTER_STANDOUT )
goto LABEL_8;
goto LABEL_27;
}
goto LABEL_19;
}
if ( !ENTER_DIM )
{
v3 = ENTER_STANDOUT;
goto LABEL_21;
}
LABEL_7:
if ( !ENTER_REVERSE )
goto LABEL_26;
LABEL_8:
if ( !v0 && EXIT_STANDOUT )
EXIT_ATTRIBUTES = (char *)EXIT_STANDOUT;
result = tgetstr("uc", v5);
UNDER_CHAR = result;
v2 = 0;
if ( result )
v2 = ENTER_UNDERLINE == 0LL;
must_use_uc = v2;
return result;
}

canonical_decompiled_code:
char *func5()
{
char *v0; // rax
char *result; // rax
_BOOL4 v2; // edx
char *v3; // rdx
char *v4; // rdx
char *v5[2]; // [rsp+8h] [rbp-10h] BYREF

v5[0] = (char *)&tcapbuf_0;
CURS_UP = tgetstr("up", v5);
CURS_RIGHT = tgetstr("ri", v5);
if ( !CURS_RIGHT )
CURS_RIGHT = tgetstr("nd", v5);
CURS_LEFT = tgetstr("le", v5);
if ( !CURS_LEFT )
{
CURS_LEFT = tgetstr("bc", v5);
if ( !CURS_LEFT )
{
if ( tgetflag("bs") )
CURS_LEFT = "\b";
}
}
ENTER_STANDOUT = tgetstr("so", v5);
EXIT_STANDOUT = (__int64)tgetstr("se", v5);
ENTER_UNDERLINE = tgetstr("us", v5);
EXIT_UNDERLINE = tgetstr("ue", v5);
ENTER_DIM = tgetstr("mh", v5);
ENTER_BOLD = tgetstr("md", v5);
ENTER_REVERSE = tgetstr("mr", v5);
v0 = tgetstr("me", v5);
EXIT_ATTRIBUTES = v0;
if ( ENTER_BOLD )
goto LABEL_5;
if ( ENTER_REVERSE )
{
ENTER_BOLD = ENTER_REVERSE;
goto LABEL_5;
}
if ( !ENTER_STANDOUT )
{
LABEL_5:
if ( ENTER_UNDERLINE )
{
if ( ENTER_DIM )
goto LABEL_7;
v3 = ENTER_STANDOUT;
if ( !ENTER_STANDOUT )
goto LABEL_8;
LABEL_21:
ENTER_DIM = v3;
if ( ENTER_REVERSE )
goto LABEL_8;
v4 = ENTER_STANDOUT;
LABEL_27:
ENTER_REVERSE = v4;
goto LABEL_8;
}
if ( ENTER_STANDOUT )
{
ENTER_UNDERLINE = ENTER_STANDOUT;
EXIT_UNDERLINE = (char *)EXIT_STANDOUT;
if ( !ENTER_DIM )
{
LABEL_19:
v3 = ENTER_STANDOUT;
goto LABEL_21;
}
}
else if ( !ENTER_DIM )
{
goto LABEL_8;
}
goto LABEL_7;
}
ENTER_BOLD = ENTER_STANDOUT;
if ( !ENTER_UNDERLINE )
{
ENTER_UNDERLINE = ENTER_STANDOUT;
EXIT_UNDERLINE = (char *)EXIT_STANDOUT;
if ( ENTER_DIM )
{
LABEL_26:
v4 = ENTER_STANDOUT;
if ( !ENTER_STANDOUT )
goto LABEL_8;
goto LABEL_27;
}
goto LABEL_19;
}
if ( !ENTER_DIM )
{
v3 = ENTER_STANDOUT;
goto LABEL_21;
}
LABEL_7:
if ( !ENTER_REVERSE )
goto LABEL_26;
LABEL_8:
if ( !v0 && EXIT_STANDOUT )
EXIT_ATTRIBUTES = (char *)EXIT_STANDOUT;
result = tgetstr("uc", v5);
UNDER_CHAR = result;
v2 = 0;
if ( result )
v2 = ENTER_UNDERLINE == 0LL;
must_use_uc = v2;
return result;
}

original_code:
void
initcap(void)
{
 static char tcapbuf[512];
 char *bp = tcapbuf;
 CURS_UP = tgetstr("up", &bp);
 CURS_RIGHT = tgetstr("ri", &bp);
 if (CURS_RIGHT == ((void *)0))
  CURS_RIGHT = tgetstr("nd", &bp);
 CURS_LEFT = tgetstr("le", &bp);
 if (CURS_LEFT == ((void *)0))
  CURS_LEFT = tgetstr("bc", &bp);
 if (CURS_LEFT == ((void *)0) && tgetflag("bs"))
  CURS_LEFT = "\b";
 ENTER_STANDOUT = tgetstr("so", &bp);
 EXIT_STANDOUT = tgetstr("se", &bp);
 ENTER_UNDERLINE = tgetstr("us", &bp);
 EXIT_UNDERLINE = tgetstr("ue", &bp);
 ENTER_DIM = tgetstr("mh", &bp);
 ENTER_BOLD = tgetstr("md", &bp);
 ENTER_REVERSE = tgetstr("mr", &bp);
 EXIT_ATTRIBUTES = tgetstr("me", &bp);
 if (!ENTER_BOLD && ENTER_REVERSE)
  ENTER_BOLD = ENTER_REVERSE;
 if (!ENTER_BOLD && ENTER_STANDOUT)
  ENTER_BOLD = ENTER_STANDOUT;
 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
  ENTER_UNDERLINE = ENTER_STANDOUT;
  EXIT_UNDERLINE = EXIT_STANDOUT;
 }
 if (!ENTER_DIM && ENTER_STANDOUT)
  ENTER_DIM = ENTER_STANDOUT;
 if (!ENTER_REVERSE && ENTER_STANDOUT)
  ENTER_REVERSE = ENTER_STANDOUT;
 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
  EXIT_ATTRIBUTES = EXIT_STANDOUT;
 UNDER_CHAR = tgetstr("uc", &bp);
 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
}

canonical_original_code:
void initcap()
{
 static char tcapbuf[512];
 char *bp = tcapbuf;
 CURS_UP = tgetstr("up", &bp);
 CURS_RIGHT = tgetstr("ri", &bp);
 if (CURS_RIGHT == ((void *)0))
  CURS_RIGHT = tgetstr("nd", &bp);
 CURS_LEFT = tgetstr("le", &bp);
 if (CURS_LEFT == ((void *)0))
  CURS_LEFT = tgetstr("bc", &bp);
 if (CURS_LEFT == ((void *)0) && tgetflag("bs"))
  CURS_LEFT = "\b";
 ENTER_STANDOUT = tgetstr("so", &bp);
 EXIT_STANDOUT = tgetstr("se", &bp);
 ENTER_UNDERLINE = tgetstr("us", &bp);
 EXIT_UNDERLINE = tgetstr("ue", &bp);
 ENTER_DIM = tgetstr("mh", &bp);
 ENTER_BOLD = tgetstr("md", &bp);
 ENTER_REVERSE = tgetstr("mr", &bp);
 EXIT_ATTRIBUTES = tgetstr("me", &bp);
 if (!ENTER_BOLD && ENTER_REVERSE)
  ENTER_BOLD = ENTER_REVERSE;
 if (!ENTER_BOLD && ENTER_STANDOUT)
  ENTER_BOLD = ENTER_STANDOUT;
 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
  ENTER_UNDERLINE = ENTER_STANDOUT;
  EXIT_UNDERLINE = EXIT_STANDOUT;
 }
 if (!ENTER_DIM && ENTER_STANDOUT)
  ENTER_DIM = ENTER_STANDOUT;
 if (!ENTER_REVERSE && ENTER_STANDOUT)
  ENTER_REVERSE = ENTER_STANDOUT;
 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
  EXIT_ATTRIBUTES = EXIT_STANDOUT;
 UNDER_CHAR = tgetstr("uc", &bp);
 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
}

variable_types:
tcapbuf
char [512]

variable_types:
bp
char *


outc

decompiled_code:
wint_t __fastcall outc(wchar_t a1, int a2)
{
wint_t result; // eax
int i; // ebp
int j; // ebx

result = putwchar(a1);
if ( must_use_uc && (curmode & 8) != 0 && a2 > 0 )
{
for ( i = 0; ; ++i )
{
if ( CURS_LEFT )
tputs(CURS_LEFT, 1, (int (*)(int))outchar);
if ( a2 == i + 1 )
break;
}
for ( j = 0; ; ++j )
{
if ( UNDER_CHAR )
tputs(UNDER_CHAR, 1, (int (*)(int))outchar);
result = j + 1;
if ( i == j )
break;
}
}
return result;
}

canonical_decompiled_code:
wint_t __fastcall func6(wchar_t a1, int a2)
{
wint_t result; // eax
int i; // ebp
int j; // ebx

result = putwchar(a1);
if ( must_use_uc && (curmode & 8) != 0 && a2 > 0 )
{
for ( i = 0; ; ++i )
{
if ( CURS_LEFT )
tputs(CURS_LEFT, 1, (int (*)(int))outchar);
if ( a2 == i + 1 )
break;
}
for ( j = 0; ; ++j )
{
if ( UNDER_CHAR )
tputs(UNDER_CHAR, 1, (int (*)(int))outchar);
result = j + 1;
if ( i == j )
break;
}
}
return result;
}

original_code:
void
outc(wint_t c, int width)
{
 int i;
 putwchar(c);
 if (must_use_uc && (curmode&010)) {
  for (i = 0; i < width; i++)
   if (CURS_LEFT == ((void *)0)) ; else tputs(CURS_LEFT, 1, outchar);
  for (i = 0; i < width; i++)
   if (UNDER_CHAR == ((void *)0)) ; else tputs(UNDER_CHAR, 1, outchar);
 }
}

canonical_original_code:
void outc(unsigned int c, int width)
{
 int i;
 putwchar(c);
 if (must_use_uc && (curmode&010)) {
  for (i = 0; i < width; i++)
   if (CURS_LEFT == ((void *)0)) ; else tputs(CURS_LEFT, 1, outchar);
  for (i = 0; i < width; i++)
   if (UNDER_CHAR == ((void *)0)) ; else tputs(UNDER_CHAR, 1, outchar);
 }
}

variable_types:
c
unsigned int

variable_types: width int

variable_types: i int


setnewmode

decompiled_code:
void __fastcall setnewmode(int a1)
{
const char *v2; // rdi

if ( !iflag )
{
if ( curmode && a1 )
setnewmode(0LL);
switch ( a1 )
{
case 0:
if ( curmode )
{
if ( curmode == 8 )
{
v2 = EXIT_UNDERLINE;
if ( EXIT_UNDERLINE )
goto LABEL_13;
}
else
{
v2 = EXIT_ATTRIBUTES;
if ( EXIT_ATTRIBUTES )
goto LABEL_13;
}
}
break;
case 1:
v2 = ENTER_REVERSE;
if ( ENTER_REVERSE )
goto LABEL_13;
break;
case 2:
if ( ENTER_UNDERLINE )
tputs(ENTER_UNDERLINE, 1, (int (*)(int))outchar);
v2 = ENTER_DIM;
if ( ENTER_DIM )
goto LABEL_13;
break;
case 4:
v2 = ENTER_DIM;
if ( ENTER_DIM )
goto LABEL_13;
break;
case 8:
v2 = ENTER_UNDERLINE;
if ( ENTER_UNDERLINE )
goto LABEL_13;
break;
case 16:
v2 = ENTER_BOLD;
if ( ENTER_BOLD )
goto LABEL_13;
break;
default:
v2 = ENTER_STANDOUT;
if ( ENTER_STANDOUT )
LABEL_13:
tputs(v2, 1, (int (*)(int))outchar);
break;
}
}
curmode = a1;
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
const char *v2; // rdi

if ( !iflag )
{
if ( curmode && a1 )
func7(0LL);
switch ( a1 )
{
case 0:
if ( curmode )
{
if ( curmode == 8 )
{
v2 = EXIT_UNDERLINE;
if ( EXIT_UNDERLINE )
goto LABEL_13;
}
else
{
v2 = EXIT_ATTRIBUTES;
if ( EXIT_ATTRIBUTES )
goto LABEL_13;
}
}
break;
case 1:
v2 = ENTER_REVERSE;
if ( ENTER_REVERSE )
goto LABEL_13;
break;
case 2:
if ( ENTER_UNDERLINE )
tputs(ENTER_UNDERLINE, 1, (int (*)(int))outchar);
v2 = ENTER_DIM;
if ( ENTER_DIM )
goto LABEL_13;
break;
case 4:
v2 = ENTER_DIM;
if ( ENTER_DIM )
goto LABEL_13;
break;
case 8:
v2 = ENTER_UNDERLINE;
if ( ENTER_UNDERLINE )
goto LABEL_13;
break;
case 16:
v2 = ENTER_BOLD;
if ( ENTER_BOLD )
goto LABEL_13;
break;
default:
v2 = ENTER_STANDOUT;
if ( ENTER_STANDOUT )
LABEL_13:
tputs(v2, 1, (int (*)(int))outchar);
break;
}
}
curmode = a1;
}

original_code:
void
setnewmode(int newmode)
{
 if (!iflag) {
  if (curmode != 000 && newmode != 000)
   setnewmode(000);
  switch (newmode) {
  case 000:
   switch(curmode) {
   case 000:
    break;
   case 010:
    if (EXIT_UNDERLINE == ((void *)0)) ; else tputs(EXIT_UNDERLINE, 1, outchar);
    break;
   default:
    if (EXIT_ATTRIBUTES == ((void *)0)) ; else tputs(EXIT_ATTRIBUTES, 1, outchar);
    break;
   }
   break;
  case 001:
   if (ENTER_REVERSE == ((void *)0)) ; else tputs(ENTER_REVERSE, 1, outchar);
   break;
  case 002:
   if (ENTER_UNDERLINE == ((void *)0)) ; else tputs(ENTER_UNDERLINE, 1, outchar);
   if (ENTER_DIM == ((void *)0)) ; else tputs(ENTER_DIM, 1, outchar);
   break;
  case 004:
   if (ENTER_DIM == ((void *)0)) ; else tputs(ENTER_DIM, 1, outchar);
   break;
  case 010:
   if (ENTER_UNDERLINE == ((void *)0)) ; else tputs(ENTER_UNDERLINE, 1, outchar);
   break;
  case 020:
   if (ENTER_BOLD == ((void *)0)) ; else tputs(ENTER_BOLD, 1, outchar);
   break;
  default:
   if (ENTER_STANDOUT == ((void *)0)) ; else tputs(ENTER_STANDOUT, 1, outchar);
   break;
  }
 }
 curmode = newmode;
}

variable_types: newmode int


flushln

decompiled_code:
__int64 flushln()
{
int v0; // er12
int v1; // ebx
int v2; // ebp
int v3; // eax
int v4; // edi
wchar_t v5; // edi
bool v6; // bl

if ( maxcol <= 0 )
{
v6 = 0;
}
else
{
v0 = 0;
v1 = 0;
v2 = 0;
do
{
v4 = SLOBYTE(obuf[3 * v1]);
if ( v4 != v2 )
{
++v0;
setnewmode(v4);
v2 = SLOBYTE(obuf[3 * v1]);
}
v5 = obuf[3 * v1 + 1];
if ( v5 )
{
outc(v5, obuf[3 * v1 + 2]);
}
else if ( upln )
{
if ( CURS_RIGHT )
tputs(CURS_RIGHT, 1, (int (*)(int))outchar);
}
else
{
outc(32, 1);
}
v3 = obuf[3 * v1 + 2];
if ( v3 > 1 )
v1 = v3 + v1 - 1;
++v1;
}
while ( maxcol > v1 );
if ( v2 )
setnewmode(0);
v6 = v0 != 0;
if ( must_overstrike && v0 )
overstrike();
}
putwchar(10);
if ( iflag && v6 )
iattr();
fflush(stdout);
if ( upln )
--upln;
return initbuf();
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // er12
int v1; // ebx
int v2; // ebp
int v3; // eax
int v4; // edi
wchar_t v5; // edi
bool v6; // bl

if ( maxcol <= 0 )
{
v6 = 0;
}
else
{
v0 = 0;
v1 = 0;
v2 = 0;
do
{
v4 = SLOBYTE(obuf[3 * v1]);
if ( v4 != v2 )
{
++v0;
func7(v4);
v2 = SLOBYTE(obuf[3 * v1]);
}
v5 = obuf[3 * v1 + 1];
if ( v5 )
{
func6(v5, obuf[3 * v1 + 2]);
}
else if ( upln )
{
if ( CURS_RIGHT )
tputs(CURS_RIGHT, 1, (int (*)(int))outchar);
}
else
{
func6(32, 1);
}
v3 = obuf[3 * v1 + 2];
if ( v3 > 1 )
v1 = v3 + v1 - 1;
++v1;
}
while ( maxcol > v1 );
if ( v2 )
func7(0);
v6 = v0 != 0;
if ( must_overstrike && v0 )
func2();
}
putwchar(10);
if ( iflag && v6 )
func3();
fflush(stdout);
if ( upln )
--upln;
return func4();
}

original_code:
void
flushln(void)
{
 int lastmode;
 int i;
 int hadmodes = 0;
 lastmode = 000;
 for (i=0; i<maxcol; i++) {
  if (obuf[i].c_mode != lastmode) {
   hadmodes++;
   setnewmode(obuf[i].c_mode);
   lastmode = obuf[i].c_mode;
  }
  if (obuf[i].c_char == '\0') {
   if (upln)
    if (CURS_RIGHT == ((void *)0)) ; else tputs(CURS_RIGHT, 1, outchar);
   else
    outc(' ', 1);
  } else
   outc(obuf[i].c_char, obuf[i].c_width);
  if (obuf[i].c_width > 1)
   i += obuf[i].c_width - 1;
 }
 if (lastmode != 000) {
  setnewmode(0);
 }
 if (must_overstrike && hadmodes)
  overstrike();
 putwchar('\n');
 if (iflag && hadmodes)
  iattr();
 (void)fflush(stdout);
 if (upln)
  upln--;
 initbuf();
}

canonical_original_code:
void flushln()
{
 int lastmode;
 int i;
 int hadmodes = 0;
 lastmode = 000;
 for (i=0; i<maxcol; i++) {
  if (obuf[i].c_mode != lastmode) {
   hadmodes++;
   setnewmode(obuf[i].c_mode);
   lastmode = obuf[i].c_mode;
  }
  if (obuf[i].c_char == '\0') {
   if (upln)
    if (CURS_RIGHT == ((void *)0)) ; else tputs(CURS_RIGHT, 1, outchar);
   else
    outc(' ', 1);
  } else
   outc(obuf[i].c_char, obuf[i].c_width);
  if (obuf[i].c_width > 1)
   i += obuf[i].c_width - 1;
 }
 if (lastmode != 000) {
  setnewmode(0);
 }
 if (must_overstrike && hadmodes)
  overstrike();
 putwchar('\n');
 if (iflag && hadmodes)
  iattr();
 (void)fflush(stdout);
 if (upln)
  upln--;
 initbuf();
}

variable_types: lastmode int

variable_types: hadmodes int


fwd

decompiled_code:
__int64 fwd()
{
int v0; // ebp
int v1; // ebx
__int64 result; // rax

v0 = col;
v1 = maxcol;
result = flushln();
col = v0;
maxcol = v1;
return result;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // ebp
int v1; // ebx
__int64 result; // rax

v0 = col;
v1 = maxcol;
result = func8();
col = v0;
maxcol = v1;
return result;
}

original_code:
void
fwd(void)
{
 int oldcol, oldmax;
 oldcol = col;
 oldmax = maxcol;
 flushln();
 col = oldcol;
 maxcol = oldmax;
}

canonical_original_code:
void fwd()
{
 int oldcol; int oldmax;
 oldcol = col;
 oldmax = maxcol;
 flushln();
 col = oldcol;
 maxcol = oldmax;
}

variable_types: oldcol int

variable_types: oldmax int


reverse

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

++upln;
result = fwd();
if ( CURS_UP )
{
result = tputs(CURS_UP, 1, (int (*)(int))outchar);
if ( CURS_UP )
result = tputs(CURS_UP, 1, (int (*)(int))outchar);
}
++upln;
return result;
}

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

++upln;
result = func9();
if ( CURS_UP )
{
result = tputs(CURS_UP, 1, (int (*)(int))outchar);
if ( CURS_UP )
result = tputs(CURS_UP, 1, (int (*)(int))outchar);
}
++upln;
return result;
}

original_code:
void
reverse(void)
{
 upln++;
 fwd();
 if (CURS_UP == ((void *)0)) ; else tputs(CURS_UP, 1, outchar);
 if (CURS_UP == ((void *)0)) ; else tputs(CURS_UP, 1, outchar);
 upln++;
}

canonical_original_code:
void reverse()
{
 upln++;
 fwd();
 if (CURS_UP == ((void *)0)) ; else tputs(CURS_UP, 1, outchar);
 if (CURS_UP == ((void *)0)) ; else tputs(CURS_UP, 1, outchar);
 upln++;
}


usage

decompiled_code:
void __noreturn usage()
{
fwrite("usage: ul [-i] [-t terminal] [file ...]\n", 1uLL, 0x28uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fwrite("usage: ul [-i] [-t terminal] [file ...]\n", 1uLL, 0x28uLL, stderr);
exit(1);
}

original_code:
static void
usage(void)
{
 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
 exit(1);
}

canonical_original_code:
static void usage()
{
 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
 exit(1);
}


repo: tonyfischetti/qstats

decompiled_code:
double __fastcall get_sum(double *a1, int a2)
{
double *v2; // rax
double result; // xmm0_8

if ( a2 <= 0 )
return 0.0;
v2 = a1;
result = 0.0;
do
result = result + *v2++;
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
return result;
}

canonical_decompiled_code:
double __fastcall func0(double *a1, int a2)
{
double *v2; // rax
double result; // xmm0_8

if ( a2 <= 0 )
return 0.0;
v2 = a1;
result = 0.0;
do
result = result + *v2++;
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
return result;
}

original_code:
double get_sum(double *data_array, int size){
    double sum = 0;
    double mean;
    int i;
    for(i = 0; i < size; i++){
        sum += data_array[i];
    }
    return sum;
}

variable_types: size int

variable_types: sum double

variable_types: mean double


decompiled_code:
double __fastcall get_standard_deviation(double *a1, int a2, double a3)
{
double *v3; // rax
double v4; // xmm1_8
double v5; // xmm1_8
double result; // xmm0_8

if ( a2 <= 0 )
{
v4 = 0.0;
}
else
{
v3 = a1;
v4 = 0.0;
do
{
v4 = v4 + (*v3 - a3) * (*v3 - a3);
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
v5 = v4 / (double)a2;
if ( v5 < 0.0 )
result = sqrt(v5);
else
result = sqrt(v5);
return result;
}

canonical_decompiled_code:
double __fastcall func1(double *a1, int a2, double a3)
{
double *v3; // rax
double v4; // xmm1_8
double v5; // xmm1_8
double result; // xmm0_8

if ( a2 <= 0 )
{
v4 = 0.0;
}
else
{
v3 = a1;
v4 = 0.0;
do
{
v4 = v4 + (*v3 - a3) * (*v3 - a3);
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
v5 = v4 / (double)a2;
if ( v5 < 0.0 )
result = sqrt(v5);
else
result = sqrt(v5);
return result;
}

original_code:
double get_standard_deviation(double *data_array, double mean, int size){
    double stddev;
    double sum_of_squares = 0;
    double mean_sum_of_squares;
    int i;
    for(i = 0; i < size; i++){
        sum_of_squares += pow((data_array[i] - mean), 2);
    }
    mean_sum_of_squares = sum_of_squares / size;
    stddev = sqrt(mean_sum_of_squares);
    return(stddev);
}

variable_types: stddev double


decompiled_code:
__int64 __fastcall make_intervals(int a1, double **a2, double a3, double a4)
{
int v4; // ebx
double *v5; // rax
double *v6; // rcx
__int64 result; // rax

v4 = a1;
v5 = (double *)malloc(8LL * (a1 + 1));
if ( !v5 )
{
fwrite("Error allocating memory", 1uLL, 0x17uLL, stderr);
exit(1);
}
v6 = v5;
result = 0LL;
if ( a1 <= 0 )
{
v4 = 0;
}
else
{
do
{
v6[result] = (double)(int)result * ((a4 - a3) / (double)a1 - 1.0) + (double)((int)result - 1) + a3 + 1.0;
++result;
}
while ( result != a1 );
}
v6[v4] = a4 + 1.0;
*a2 = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, double **a2, double a3, double a4)
{
int v4; // ebx
double *v5; // rax
double *v6; // rcx
__int64 result; // rax

v4 = a1;
v5 = (double *)malloc(8LL * (a1 + 1));
if ( !v5 )
{
fwrite("Error allocating memory", 1uLL, 0x17uLL, stderr);
exit(1);
}
v6 = v5;
result = 0LL;
if ( a1 <= 0 )
{
v4 = 0;
}
else
{
do
{
v6[result] = (double)(int)result * ((a4 - a3) / (double)a1 - 1.0) + (double)((int)result - 1) + a3 + 1.0;
++result;
}
while ( result != a1 );
}
v6[v4] = a4 + 1.0;
*a2 = v6;
return result;
}

original_code:
void make_intervals(double themin, double themax,
                    int breaks, double** delivery_array){
    double range = themax - themin;
    double *to_return_array;
    int to_return_arrayindex = 0;
    double upto = themin;
    double thedivide = range/breaks-1;
    int i;
    to_return_array = (double *) malloc((breaks+1) * sizeof(double));
    if(to_return_array == ((void *)0)){
        fputs("Error allocating memory", stderr);
        exit(1);
    }
    for(i=0; i < breaks; i++){
        double newupto = i*thedivide+((i-1)*1)+themin+1;
        to_return_array[to_return_arrayindex] = newupto;
        to_return_arrayindex++;
        upto = newupto;
    }
    to_return_array[to_return_arrayindex] = themax+1;
    *delivery_array = to_return_array;
}

canonical_original_code:
void make_intervals(double themin, double themax, int breaks, double **delivery_array){
    double range = themax - themin;
    double *to_return_array;
    int to_return_arrayindex = 0;
    double upto = themin;
    double thedivide = range/breaks-1;
    int i;
    to_return_array = (double *) malloc((breaks+1) * sizeof(double));
    if(to_return_array == ((void *)0)){
        fputs("Error allocating memory", stderr);
        exit(1);
    }
    for(i=0; i < breaks; i++){
        double newupto = i*thedivide+((i-1)*1)+themin+1;
        to_return_array[to_return_arrayindex] = newupto;
        to_return_arrayindex++;
        upto = newupto;
    }
    to_return_array[to_return_arrayindex] = themax+1;
    *delivery_array = to_return_array;
}

variable_types: themin double

variable_types: themax double

variable_types: breaks int

variable_types: range double

variable_types: upto double

variable_types: thedivide double

variable_types: newupto double


decompiled_code:
__int64 __fastcall get_uniques(double *a1, int a2, _QWORD *a3)
{
double v4; // rbx
double *v5; // r12
double *v6; // rax
int v7; // edx
int v8; // ebp
double v9; // xmm1_8
void *v10; // rax

v4 = *a1;
v5 = (double *)malloc(8LL * a2);
*v5 = v4;
if ( a2 <= 1 )
{
v8 = 1;
}
else
{
v6 = a1 + 1;
v7 = 1;
v8 = 1;
do
{
v9 = *v6;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v4 - *v6) & LC3) > 0.0001 )
{
v5[v7++] = v9;
++v8;
v4 = v9;
}
++v6;
}
while ( v6 != &a1[(unsigned int)(a2 - 2) + 2] );
}
v10 = realloc(v5, 8LL * v8);
if ( !v10 )
{
free(v5);
fwrite("Error allocating memory", 1uLL, 0x17uLL, stderr);
exit(1);
}
*a3 = v10;
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(double *a1, int a2, _QWORD *a3)
{
double v4; // rbx
double *v5; // r12
double *v6; // rax
int v7; // edx
int v8; // ebp
double v9; // xmm1_8
void *v10; // rax

v4 = *a1;
v5 = (double *)malloc(8LL * a2);
*v5 = v4;
if ( a2 <= 1 )
{
v8 = 1;
}
else
{
v6 = a1 + 1;
v7 = 1;
v8 = 1;
do
{
v9 = *v6;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v4 - *v6) & LC3) > 0.0001 )
{
v5[v7++] = v9;
++v8;
v4 = v9;
}
++v6;
}
while ( v6 != &a1[(unsigned int)(a2 - 2) + 2] );
}
v10 = realloc(v5, 8LL * v8);
if ( !v10 )
{
free(v5);
fwrite("Error allocating memory", 1uLL, 0x17uLL, stderr);
exit(1);
}
*a3 = v10;
return (unsigned int)v8;
}

original_code:
int get_uniques(double *data_array, int size, double** delivery_uniques){
    double last_value = data_array[0];
    double *uniques;
    int new_size = 1;
    int old_index;
    int new_index = 1;
    double *temp;
    uniques = (double *) malloc(size * sizeof(double));
    uniques[0] = last_value;
    for(old_index=1; old_index < size; old_index++){
        if(!(fabs(last_value - data_array[old_index]) <= .0001)){
            uniques[new_index] = data_array[old_index];
            new_index++;
            new_size++;
            last_value = data_array[old_index];
        }
    }
    temp = realloc(uniques, new_size * sizeof(double));
    if(temp == ((void *)0)){
        free(uniques);
        fputs("Error allocating memory", stderr);
        exit(1);
    }
    uniques = temp;
    *delivery_uniques = uniques;
    return(new_size);
}

canonical_original_code:
int get_uniques(double *data_array, int size, double **delivery_uniques){
    double last_value = data_array[0];
    double *uniques;
    int new_size = 1;
    int old_index;
    int new_index = 1;
    double *temp;
    uniques = (double *) malloc(size * sizeof(double));
    uniques[0] = last_value;
    for(old_index=1; old_index < size; old_index++){
        if(!(fabs(last_value - data_array[old_index]) <= .0001)){
            uniques[new_index] = data_array[old_index];
            new_index++;
            new_size++;
            last_value = data_array[old_index];
        }
    }
    temp = realloc(uniques, new_size * sizeof(double));
    if(temp == ((void *)0)){
        free(uniques);
        fputs("Error allocating memory", stderr);
        exit(1);
    }
    uniques = temp;
    *delivery_uniques = uniques;
    return(new_size);
}

variable_types:
uniques
double *

variable_types:
temp
double *


original_code:
void draw_bars(int *data_array, int size){
    struct winsize w;
    int ncols;
    int wiggle;
    int sum = 0;
    int i;
    double themax;
    double factor;
    double *rel_freq;
    ioctl(1, 0x5413, &w);
    ncols = w.ws_col;
    wiggle = ncols - 15;
    rel_freq = (double *) malloc(size * sizeof(double));
    for(i = 0; i < size; i++){
        sum += data_array[i];
    }
    for(i = 0; i < size; i++){
        int thenum = data_array[i];
        rel_freq[i] = ((double)thenum/(double)sum) * 100;
    }
    themax = rel_freq[0];
    for(i = 0; i < size; i++){
        if(rel_freq[i] > themax){
            themax = rel_freq[i];
        }
    }
    factor = wiggle/themax;
    for(i = 0; i < size; i++){
        int m;
        int thenum = rel_freq[i];
        printf("%0.1f%%\t", rel_freq[i]);
        for(m = 0; m < floor(factor*thenum); m++){
            printf("#");
        }
        printf("\n");
    }
    free(rel_freq);
}

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types: ncols int

variable_types: wiggle int

variable_types: sum int

variable_types: i int

variable_types: factor double

variable_types: thenum int

variable_types: m int


decompiled_code:
__int64 __fastcall comp_func(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = *(double *)a1 > *(double *)a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = *(double *)a1 > *(double *)a2;
return result;
}

original_code:
int comp_func(const void * a, const void * b) {
    double *x = (double *) a;
    double *y = (double *) b;
    if (*x < *y){
        return -1;
    }
    else if (*x > *y) return 1;
    return 0;
}

canonical_original_code:
int comp_func(void *a, void *b) {
    double *x = (double *) a;
    double *y = (double *) b;
    if (*x < *y){
        return -1;
    }
    else if (*x > *y) return 1;
    return 0;
}

variable_types:
a
void *

variable_types:
b
void *

variable_types:
x
double *

variable_types:
y
double *


scale

original_code:
static inline
signed int scale(mad_fixed_t sample)
{
    sample += (1L << (28 - 16));
    if (sample >= ((mad_fixed_t) (0x10000000L)))
        sample = ((mad_fixed_t) (0x10000000L)) - 1;
    else if (sample < -((mad_fixed_t) (0x10000000L)))
        sample = -((mad_fixed_t) (0x10000000L));
    return sample >> (28 + 1 - 16);
}

canonical_original_code:
static inline int scale(int sample)
{
    sample += (1L << (28 - 16));
    if (sample >= ((mad_fixed_t) (0x10000000L)))
        sample = ((mad_fixed_t) (0x10000000L)) - 1;
    else if (sample < -((mad_fixed_t) (0x10000000L)))
        sample = -((mad_fixed_t) (0x10000000L));
    return sample >> (28 + 1 - 16);
}

variable_types: sample int


error

decompiled_code:
__int64 __fastcall error(_QWORD *a1, __int64 a2)
{
__int64 v2; // rbp
const char *v3; // rax

puts("this is mad_flow error");
v2 = *(_QWORD *)(a2 + 40) - *a1;
v3 = (const char *)mad_stream_errorstr();
fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n", *(unsigned int *)(a2 + 112), v3, v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, __int64 a2)
{
__int64 v2; // rbp
const char *v3; // rax

puts("this is mad_flow error");
v2 = *(_QWORD *)(a2 + 40) - *a1;
v3 = (const char *)mad_stream_errorstr();
fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n", *(unsigned int *)(a2 + 112), v3, v2);
return 0LL;
}

original_code:
static
enum mad_flow error(void *data,
        struct mad_stream *stream,
        struct mad_frame *frame)
{
    struct buffer *buffer = data;
    printf("this is mad_flow error\n");
    fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
            stream->error, mad_stream_errorstr(stream),
            stream->this_frame - buffer->start);
    return MAD_FLOW_CONTINUE;
}

variable_types:
data
void *

variable_types: struct mad_stream { unsigned char *buffer; unsigned char *bufend; unsigned long skiplen; int sync; unsigned long freerate; unsigned char *this_frame; unsigned char *next_frame; struct mad_bitptr { unsigned char *byte; unsigned short cache; unsigned short left; } ptr; struct mad_bitptr anc_ptr; unsigned int anc_bitlen; unsigned char (*main_data)[-1]; unsigned int md_len; int options; enum mad_error { MAD_ERROR_NONE=0, MAD_ERROR_BUFLEN=1, MAD_ERROR_BUFPTR=2, MAD_ERROR_NOMEM=49, MAD_ERROR_LOSTSYNC=257, MAD_ERROR_BADLAYER=258, MAD_ERROR_BADBITRATE=259, MAD_ERROR_BADSAMPLERATE=260, MAD_ERROR_BADEMPHASIS=261, MAD_ERROR_BADCRC=513, MAD_ERROR_BADBITALLOC=529, MAD_ERROR_BADSCALEFACTOR=545, MAD_ERROR_BADMODE=546, MAD_ERROR_BADFRAMELEN=561, MAD_ERROR_BADBIGVALUES=562, MAD_ERROR_BADBLOCKTYPE=563, MAD_ERROR_BADSCFSI=564, MAD_ERROR_BADDATAPTR=565, MAD_ERROR_BADPART3LEN=566, MAD_ERROR_BADHUFFTABLE=567, MAD_ERROR_BADHUFFDATA=568, MAD_ERROR_BADSTEREO=569 } error; } *

variable_types: struct mad_frame { struct mad_header { enum mad_layer { MAD_LAYER_I=1, MAD_LAYER_II=2, MAD_LAYER_III=3 } layer; enum mad_mode { MAD_MODE_SINGLE_CHANNEL=0, MAD_MODE_DUAL_CHANNEL=1, MAD_MODE_JOINT_STEREO=2, MAD_MODE_STEREO=3 } mode; int mode_extension; enum mad_emphasis { MAD_EMPHASIS_NONE=0, MAD_EMPHASIS_50_15_US=1, MAD_EMPHASIS_CCITT_J_17=3, MAD_EMPHASIS_RESERVED=2 } emphasis; unsigned long bitrate; unsigned int samplerate; unsigned short crc_check; unsigned short crc_target; int flags; int private_bits; struct mad_timer_t { long seconds; unsigned long fraction; } duration; } header; int options; int sbsample[2][36][32]; int (*overlap)[2][32][18]; } *

variable_types: struct buffer { unsigned char *start; unsigned long length; } *

return_type: enum mad_flow { MAD_FLOW_CONTINUE=0, MAD_FLOW_STOP=16, MAD_FLOW_BREAK=17, MAD_FLOW_IGNORE=32 }

user_defined_types: struct mad_stream { unsigned char *buffer; unsigned char *bufend; unsigned long skiplen; int sync; unsigned long freerate; unsigned char *this_frame; unsigned char *next_frame; struct mad_bitptr ptr; struct mad_bitptr anc_ptr; unsigned int anc_bitlen; unsigned char (*main_data)[-1]; unsigned int md_len; int options; enum mad_error error; }

user_defined_types: struct mad_frame { struct mad_header header; int options; int sbsample[2][36][32]; int (*overlap)[2][32][18]; }

user_defined_types: struct mad_header { enum mad_layer layer; enum mad_mode mode; int mode_extension; enum mad_emphasis emphasis; unsigned long bitrate; unsigned int samplerate; unsigned short crc_check; unsigned short crc_target; int flags; int private_bits; struct mad_timer_t duration; }


output

original_code:
static
enum mad_flow output(void *data,
        struct mad_header const *header,
        struct mad_pcm *pcm)
{
    unsigned int nchannels, nsamples,n;
    mad_fixed_t const *left_ch, *right_ch;
    nchannels = pcm->channels;
    n=nsamples = pcm->length;
    left_ch = pcm->samples[0];
    right_ch = pcm->samples[1];
    unsigned char Output[6912], *OutputPtr;
    int fmt, wrote, speed, exact_rate, err, dir;
    OutputPtr = Output;
    while (nsamples--)
    {
        signed int sample;
        sample = scale(*left_ch++);
        *(OutputPtr++) = sample >> 0;
        *(OutputPtr++) = sample >> 8;
        if (nchannels == 2)
        {
            sample = scale (*right_ch++);
            *(OutputPtr++) = sample >> 0;
            *(OutputPtr++) = sample >> 8;
        }
    }
    OutputPtr = Output;
    snd_pcm_writei (handle, OutputPtr, n);
    OutputPtr = Output;
    return MAD_FLOW_CONTINUE;
}

canonical_original_code:
static enum mad_flow output(void *data, struct mad_header *header, struct mad_pcm *pcm)
{
    unsigned int nchannels; unsigned int nsamples; unsigned int n;
    int *left_ch; int *right_ch;
    nchannels = pcm->channels;
    n=nsamples = pcm->length;
    left_ch = pcm->samples[0];
    right_ch = pcm->samples[1];
    unsigned char Output[6912]; unsigned char *OutputPtr;
    int fmt; int wrote; int speed; int exact_rate; int err; int dir;
    OutputPtr = Output;
    while (nsamples--)
    {
        int sample;
        sample = scale(*left_ch++);
        *(OutputPtr++) = sample >> 0;
        *(OutputPtr++) = sample >> 8;
        if (nchannels == 2)
        {
            sample = scale (*right_ch++);
            *(OutputPtr++) = sample >> 0;
            *(OutputPtr++) = sample >> 8;
        }
    }
    OutputPtr = Output;
    snd_pcm_writei (handle, OutputPtr, n);
    OutputPtr = Output;
    return MAD_FLOW_CONTINUE;
}

variable_types: struct mad_header { enum mad_layer { MAD_LAYER_I=1, MAD_LAYER_II=2, MAD_LAYER_III=3 } layer; enum mad_mode { MAD_MODE_SINGLE_CHANNEL=0, MAD_MODE_DUAL_CHANNEL=1, MAD_MODE_JOINT_STEREO=2, MAD_MODE_STEREO=3 } mode; int mode_extension; enum mad_emphasis { MAD_EMPHASIS_NONE=0, MAD_EMPHASIS_50_15_US=1, MAD_EMPHASIS_CCITT_J_17=3, MAD_EMPHASIS_RESERVED=2 } emphasis; unsigned long bitrate; unsigned int samplerate; unsigned short crc_check; unsigned short crc_target; int flags; int private_bits; struct mad_timer_t { long seconds; unsigned long fraction; } duration; } *

variable_types: struct mad_pcm { unsigned int samplerate; unsigned short channels; unsigned short length; int samples[2][1152]; } *

variable_types:
nchannels
unsigned int

variable_types:
nsamples
unsigned int

variable_types:
n
unsigned int

variable_types:
Output
unsigned char [6912]

variable_types:
OutputPtr
unsigned char *

variable_types: fmt int

variable_types: wrote int

variable_types: speed int

variable_types: err int

variable_types: dir int

user_defined_types: enum mad_flow { MAD_FLOW_CONTINUE=0, MAD_FLOW_STOP=16, MAD_FLOW_BREAK=17, MAD_FLOW_IGNORE=32 }


input

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

puts("this is input");
result = 16LL;
if ( *(_QWORD *)(a1 + 8) )
{
mad_stream_buffer();
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
}
return result;
}

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

puts("this is input");
result = 16LL;
if ( *(_QWORD *)(a1 + 8) )
{
mad_stream_buffer();
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
}
return result;
}

original_code:
static
enum mad_flow input(void *data,
        struct mad_stream *stream)
{
    struct buffer *buffer = data;
    printf("this is input\n");
    if (!buffer->length)
        return MAD_FLOW_STOP;
    mad_stream_buffer(stream, buffer->start, buffer->length);
    buffer->length = 0;
    return MAD_FLOW_CONTINUE;
}

user_defined_types: struct buffer { unsigned char *start; unsigned long length; }

user_defined_types: struct mad_bitptr { unsigned char *byte; unsigned short cache; unsigned short left; }

user_defined_types: enum mad_error { MAD_ERROR_NONE=0, MAD_ERROR_BUFLEN=1, MAD_ERROR_BUFPTR=2, MAD_ERROR_NOMEM=49, MAD_ERROR_LOSTSYNC=257, MAD_ERROR_BADLAYER=258, MAD_ERROR_BADBITRATE=259, MAD_ERROR_BADSAMPLERATE=260, MAD_ERROR_BADEMPHASIS=261, MAD_ERROR_BADCRC=513, MAD_ERROR_BADBITALLOC=529, MAD_ERROR_BADSCALEFACTOR=545, MAD_ERROR_BADMODE=546, MAD_ERROR_BADFRAMELEN=561, MAD_ERROR_BADBIGVALUES=562, MAD_ERROR_BADBLOCKTYPE=563, MAD_ERROR_BADSCFSI=564, MAD_ERROR_BADDATAPTR=565, MAD_ERROR_BADPART3LEN=566, MAD_ERROR_BADHUFFTABLE=567, MAD_ERROR_BADHUFFDATA=568, MAD_ERROR_BADSTEREO=569 }