decompiled_code:
__int64 __fastcall find_q(int a1, __int64 a2, int a3)
{
int v6; // ebx

if ( a3 <= 0 )
return 0LL;
v6 = 0;
while ( *(_DWORD *)(a2 + 4) != a1 )
{
a2 = next_q(a2);
if ( a3 == ++v6 )
return 0LL;
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, __int64 a2, int a3)
{
int v6; // ebx

if ( a3 <= 0 )
return 0LL;
v6 = 0;
while ( *(_DWORD *)(a2 + 4) != a1 )
{
a2 = func0(a2);
if ( a3 == ++v6 )
return 0LL;
}
return a2;
}

original_code:
queue *find_q( sid_t id,queue *q, int len )
{
    queue *buf = q;
    int i;
    for( i = 0 ; i < len ; ++i )
    {
        if ( buf->id == id )
        {
            return buf;
        }
        buf = next_q( buf );
    }
    return ((void *)0);
}

canonical_original_code:
struct __queue *find_q(int id, struct __queue *q, int len)
{
    struct __queue *buf = q;
    int i;
    for( i = 0 ; i < len ; ++i )
    {
        if ( buf->id == id )
        {
            return buf;
        }
        buf = next_q( buf );
    }
    return ((void *)0);
}

variable_types: id int


repo: shmmy/OS

decompiled_code:
int __fastcall print_tree(__int64 a1)
{
return _print_tree(a1, 0);
}

canonical_decompiled_code:
int __fastcall func19(__int64 a1)
{
return func13(a1, 0);
}

original_code:
void
print_tree(struct tree_node *root)
{
 __print_tree(root, 0);
}

variable_types: struct tree_node { unsigned int nr_children; char name[16]; struct tree_node *children; } *

return_type: void


decompiled_code:
__int64 __fastcall output_mandel_line_to_ppm(__int64 a1)
{
__int64 v1; // rbx
unsigned __int8 v2; // r9
int v3; // er8
int v4; // ecx
size_t v5; // rax
int v6; // eax
__int64 result; // rax
char v8; // [rsp+Fh] [rbp-39h] BYREF
char s[56]; // [rsp+10h] [rbp-38h] BYREF

v8 = 10;
if ( x_chars > 0 )
{
v1 = 0LL;
do
{
v6 = *(_DWORD *)(a1 + 4 * v1);
if ( v6 == 255 )
{
v2 = 0;
LOBYTE(v3) = 0;
LOBYTE(v4) = 0;
}
else
{
v2 = *(_DWORD *)(a1 + 4 * v1);
v3 = v6 / 4;
v4 = v6 / 2;
}
snprintf(s, 0x14uLL, "%d\t %d\t %d\t", (unsigned __int8)v4, (unsigned __int8)v3, v2);
v5 = strlen(s);
if ( (unsigned int)insist_write((unsigned int)image_fd, s, v5) == -1 )
{
perror("child:output_mandel:for");
exit(1);
}
++v1;
}
while ( x_chars > (int)v1 );
}
result = insist_write((unsigned int)image_fd, &v8, 1LL);
if ( (_DWORD)result == -1 )
{
perror("child:output_mandel");
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rbx
unsigned __int8 v2; // r9
int v3; // er8
int v4; // ecx
size_t v5; // rax
int v6; // eax
__int64 result; // rax
char v8; // [rsp+Fh] [rbp-39h] BYREF
char s[56]; // [rsp+10h] [rbp-38h] BYREF

v8 = 10;
if ( x_chars > 0 )
{
v1 = 0LL;
do
{
v6 = *(_DWORD *)(a1 + 4 * v1);
if ( v6 == 255 )
{
v2 = 0;
LOBYTE(v3) = 0;
LOBYTE(v4) = 0;
}
else
{
v2 = *(_DWORD *)(a1 + 4 * v1);
v3 = v6 / 4;
v4 = v6 / 2;
}
snprintf(s, 0x14uLL, "%d\t %d\t %d\t", (unsigned __int8)v4, (unsigned __int8)v3, v2);
v5 = strlen(s);
if ( (unsigned int)insist_write((unsigned int)image_fd, s, v5) == -1 )
{
perror("child:output_mandel:for");
exit(1);
}
++v1;
}
while ( x_chars > (int)v1 );
}
result = insist_write((unsigned int)image_fd, &v8, 1LL);
if ( (_DWORD)result == -1 )
{
perror("child:output_mandel");
exit(1);
}
return result;
}

original_code:
void output_mandel_line_to_ppm(int color_val[])
{
    int i;
    int iocheck;
    unsigned char rgb[3];
    char rgb_trio[20];
    char nl = '\n';
    for(i=0;i<x_chars;i++)
    {
        if(color_val[i]==255)
        {
            rgb[2]=0;
            rgb[1]=0;
            rgb[0]=0;
        }
        else
        {
            rgb[2]=color_val[i];
            rgb[1]=color_val[i]/4;
            rgb[0]=color_val[i]/2;
        }
        snprintf(rgb_trio,20,"%d\t %d\t %d\t",rgb[0],rgb[1],rgb[2]);
        iocheck = insist_write(image_fd,rgb_trio,strlen(rgb_trio));
        if(iocheck == -1)
        {
            perror("child:output_mandel:for");
            exit(1);
        }
    }
    iocheck = insist_write(image_fd,&nl,1);
    if(iocheck == -1)
    {
        perror("child:output_mandel");
        exit(1);
    }
}

variable_types: i int

variable_types: iocheck int

variable_types:
rgb
unsigned char [3]

variable_types: nl char


decompiled_code:
double __fastcall compute_mandel_line(int a1, _DWORD *a2)
{
double v2; // r14
double result; // xmm0_8
double v5; // xmm0_8
double v6; // [rsp+8h] [rbp-20h]

v2 = *(double *)&ymax - (double)a1 * *(double *)&ystep;
v6 = *(double *)&xmin;
for ( result = *(double *)&xmax; *(double *)&xmax >= v6; result = *(double *)&xmax )
{
v5 = mandel_iterations_at_point(256LL, v6, v2) * 10.0;
if ( v5 > 255.0 )
v5 = *(double *)&LC0;
*a2 = (int)v5;
v6 = v6 + *(double *)&xstep;
++a2;
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(int a1, _DWORD *a2)
{
double v2; // r14
double result; // xmm0_8
double v5; // xmm0_8
double v6; // [rsp+8h] [rbp-20h]

v2 = *(double *)&ymax - (double)a1 * *(double *)&ystep;
v6 = *(double *)&xmin;
for ( result = *(double *)&xmax; *(double *)&xmax >= v6; result = *(double *)&xmax )
{
v5 = mandel_iterations_at_point(256LL, v6, v2) * 10.0;
if ( v5 > 255.0 )
v5 = *(double *)&LC0;
*a2 = (int)v5;
v6 = v6 + *(double *)&xstep;
++a2;
}
return result;
}

original_code:
void compute_mandel_line(int line, int color_val[])
{
    double x, y;
    int n;
    double val;
    y = ymax - ystep * line;
    for (x = xmin, n = 0; x <= xmax; x+= xstep, n++) {
        val = mandel_iterations_at_point(x, y, 256);
        val*=10;
        if (val > 255)
            val = 255;
        color_val[n] = val;
    }
}

canonical_original_code:
void compute_mandel_line(int line, int color_val[])
{
    double x; double y;
    int n;
    double val;
    y = ymax - ystep * line;
    for (x = xmin, n = 0; x <= xmax; x+= xstep, n++) {
        val = mandel_iterations_at_point(x, y, 256);
        val*=10;
        if (val > 255)
            val = 255;
        color_val[n] = val;
    }
}

variable_types: line int

variable_types: x double

variable_types: y double

variable_types: n int

variable_types: val double


child

decompiled_code:
__int64 __fastcall child(int a1, __int64 a2, __int64 a3)
{
void *v4; // rsp
int i; // ebx
double v6; // xmm0_8
_DWORD v8[8]; // [rsp+0h] [rbp-20h] BYREF

v4 = alloca(4LL * x_chars);
for ( i = start + a1; end > i; i += 2 )
{
v6 = compute_mandel_line(i, v8);
pipesem_wait(a2, v6);
output_mandel_line_to_ppm((__int64)v8);
pipesem_signal(a3);
}
return pipesem_destroy(a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2, __int64 a3)
{
void *v4; // rsp
int i; // ebx
double v6; // xmm0_8
_DWORD v8[8]; // [rsp+0h] [rbp-20h] BYREF

v4 = alloca(4LL * x_chars);
for ( i = start + a1; end > i; i += 2 )
{
v6 = func0(i, v8);
pipesem_wait(a2, v6);
func1((__int64)v8);
pipesem_signal(a3);
}
return pipesem_destroy(a2);
}

original_code:
void child(int ch_id,struct pipesem *mysem,struct pipesem *othersem)
{
    int line;
    int color_val[x_chars];
    for (line = start+ch_id; line < end; line+=2) {
        compute_mandel_line(line, color_val);
        pipesem_wait(mysem);
        output_mandel_line_to_ppm(color_val);
        pipesem_signal(othersem);
    }
    pipesem_destroy(mysem);
}

canonical_original_code:
void child(int ch_id, struct pipesem *mysem, struct pipesem *othersem)
{
    int line;
    int color_val[-1];
    for (line = start+ch_id; line < end; line+=2) {
        compute_mandel_line(line, color_val);
        pipesem_wait(mysem);
        output_mandel_line_to_ppm(color_val);
        pipesem_signal(othersem);
    }
    pipesem_destroy(mysem);
}

variable_types:
mysem
struct pipesem { int rfd; int wfd; } *

variable_types:
othersem
struct pipesem *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__pid_t v4; // eax
int v5; // edi
__pid_t v6; // eax
size_t v8; // rax
char s[208]; // [rsp+0h] [rbp-F8h] BYREF
char v10[8]; // [rsp+D0h] [rbp-28h] BYREF
char v11[20]; // [rsp+D8h] [rbp-20h] BYREF
__WAIT_STATUS stat_loc; // [rsp+ECh] [rbp-Ch] BYREF

if ( argc == 3 )
{
_isoc99_sscanf(argv[1], "%d", &start);
_isoc99_sscanf(argv[2], "%d", &end);
v3 = argv[1];
}
else
{
start = 0;
end = y_chars;
v3 = "mandel.ppm";
}
image_fd = open(v3, 577, 438LL);
if ( !start )
{
snprintf(
s,
0xC8uLL,
"P3\n%s\n%d\t %d\n%d\n",
"# mandelbrot set",
(unsigned int)x_chars,
(unsigned int)y_chars,
255LL);
v8 = strlen(s);
if ( (unsigned int)insist_write((unsigned int)image_fd, s, v8) == -1 )
{
perror("main:write");
exit(1);
}
}
*(double *)&xstep = (*(double *)&xmax - *(double *)&xmin) / (double)x_chars;
*(double *)&ystep = (*(double *)&ymax - *(double *)&ymin) / (double)y_chars;
pipesem_init(v10, 0LL);
pipesem_init(v11, 0LL);
v4 = fork();
v5 = v4;
if ( v4 < 0 )
goto LABEL_12;
if ( !v4 )
goto LABEL_14;
v6 = fork();
if ( v6 < 0 )
{
LABEL_12:
perror("parent: fork");
exit(1);
}
if ( !v6 )
{
v5 = 1;
LABEL_14:
child(v5, (__int64)&v10[8 * v5], (__int64)&v10[8 * ((v5 + 1) % 2)]);
exit(1);
}
pipesem_signal(v10);
wait((__WAIT_STATUS)&stat_loc);
wait((__WAIT_STATUS)&stat_loc);
close(image_fd);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__pid_t v4; // eax
int v5; // edi
__pid_t v6; // eax
size_t v8; // rax
char s[208]; // [rsp+0h] [rbp-F8h] BYREF
char v10[8]; // [rsp+D0h] [rbp-28h] BYREF
char v11[20]; // [rsp+D8h] [rbp-20h] BYREF
__WAIT_STATUS stat_loc; // [rsp+ECh] [rbp-Ch] BYREF

if ( argc == 3 )
{
_isoc99_sscanf(argv[1], "%d", &start);
_isoc99_sscanf(argv[2], "%d", &end);
v3 = argv[1];
}
else
{
start = 0;
end = y_chars;
v3 = "mandel.ppm";
}
image_fd = open(v3, 577, 438LL);
if ( !start )
{
snprintf(
s,
0xC8uLL,
"P3\n%s\n%d\t %d\n%d\n",
"# mandelbrot set",
(unsigned int)x_chars,
(unsigned int)y_chars,
255LL);
v8 = strlen(s);
if ( (unsigned int)insist_write((unsigned int)image_fd, s, v8) == -1 )
{
perror("main:write");
exit(1);
}
}
*(double *)&xstep = (*(double *)&xmax - *(double *)&xmin) / (double)x_chars;
*(double *)&ystep = (*(double *)&ymax - *(double *)&ymin) / (double)y_chars;
pipesem_init(v10, 0LL);
pipesem_init(v11, 0LL);
v4 = fork();
v5 = v4;
if ( v4 < 0 )
goto LABEL_12;
if ( !v4 )
goto LABEL_14;
v6 = fork();
if ( v6 < 0 )
{
LABEL_12:
perror("parent: fork");
exit(1);
}
if ( !v6 )
{
v5 = 1;
LABEL_14:
func2(v5, (__int64)&v10[8 * v5], (__int64)&v10[8 * ((v5 + 1) % 2)]);
exit(1);
}
pipesem_signal(v10);
wait((__WAIT_STATUS)&stat_loc);
wait((__WAIT_STATUS)&stat_loc);
close(image_fd);
return 0;
}

original_code:
int main(int argc,char **argv)
{
    const char *filename;
    if(argc != 3)
    {
        start=0;
        end=y_chars;
        filename="mandel.ppm";
    }
    else
    {
        sscanf(argv[1],"%d",&start);
        sscanf(argv[2],"%d",&end);
        filename=argv[1];
    }
    int i;
    pid_t p;
    int status;
    struct pipesem sem[2];
    image_fd = open(filename,0100 | 01 | 01000, 0400 | 0200 | (0400 >> 3) | (0200 >> 3) | ((0400 >> 3) >> 3) | ((0200 >> 3) >> 3) );
    if(start==0)
    {
        int iXmax = x_chars;
        int iYmax = y_chars;
        const int MaxColorComponentValue=255;
        char *comment="# mandelbrot set";
        char header[200];
        int iocheck;
        snprintf(header,200,"P3\n%s\n%d\t %d\n%d\n",comment,iXmax,iYmax,MaxColorComponentValue);
        iocheck = insist_write(image_fd,header,strlen(header));
        if (iocheck == -1 )
        {
            perror("main:write");
            exit(1);
        }
    }
    xstep = (xmax - xmin) / x_chars;
    ystep = (ymax - ymin) / y_chars;
    for(i=0;i<2;i++)
    {
        pipesem_init(&sem[i],0);
    }
    for(i=0;i<2;i++)
    {
        p = fork();
        if (p < 0) {
            perror("parent: fork");
            exit(1);
        }
        if (p == 0) {
            child(i,&sem[i],&sem[(i+1)%2]);
            exit(1);
        }
    }
    pipesem_signal(sem);
    for(i=0;i<2;i++)
    {
        wait(&status);
    }
    close(image_fd);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *filename;
    if(argc != 3)
    {
        start=0;
        end=y_chars;
        filename="mandel.ppm";
    }
    else
    {
        sscanf(argv[1],"%d",&start);
        sscanf(argv[2],"%d",&end);
        filename=argv[1];
    }
    int i;
    int p;
    int status;
    struct pipesem sem[2];
    image_fd = open(filename,0100 | 01 | 01000, 0400 | 0200 | (0400 >> 3) | (0200 >> 3) | ((0400 >> 3) >> 3) | ((0200 >> 3) >> 3) );
    if(start==0)
    {
        int iXmax = x_chars;
        int iYmax = y_chars;
        int MaxColorComponentValue = 255;
        char *comment = "# mandelbrot set";
        char header[200];
        int iocheck;
        snprintf(header,200,"P3\n%s\n%d\t %d\n%d\n",comment,iXmax,iYmax,MaxColorComponentValue);
        iocheck = insist_write(image_fd,header,strlen(header));
        if (iocheck == -1 )
        {
            perror("main:write");
            exit(1);
        }
    }
    xstep = (xmax - xmin) / x_chars;
    ystep = (ymax - ymin) / y_chars;
    for(i=0;i<2;i++)
    {
        pipesem_init(&sem[i],0);
    }
    for(i=0;i<2;i++)
    {
        p = fork();
        if (p < 0) {
            perror("parent: fork");
            exit(1);
        }
        if (p == 0) {
            child(i,&sem[i],&sem[(i+1)%2]);
            exit(1);
        }
    }
    pipesem_signal(sem);
    for(i=0;i<2;i++)
    {
        wait(&status);
    }
    close(image_fd);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
filename
char *

variable_types: p int

variable_types: status int

variable_types: struct pipesem { int rfd; int wfd; } [2]

variable_types: iXmax int

variable_types: iYmax int

variable_types: MaxColorComponentValue int

variable_types:
comment
char *

variable_types:
header
char [200]

return_type: int


help

decompiled_code:
int help()
{
return puts(
" ? : print help\n"
" q : quit\n"
" p : print tasks\n"
" k <id> : kill task identified by id\n"
" e <program>: execute program\n"
" h <id> : set task identified by id to high priority\n"
" l <id> : set task identified by id to low priority");
}

canonical_decompiled_code:
int func9()
{
return puts(
" ? : print help\n"
" q : quit\n"
" p : print tasks\n"
" k <id> : kill task identified by id\n"
" e <program>: execute program\n"
" h <id> : set task identified by id to high priority\n"
" l <id> : set task identified by id to low priority");
}

original_code:
void help(void)
{
 printf(" ?          : print help\n"
        " q          : quit\n"
        " p          : print tasks\n"
        " k <id>     : kill task identified by id\n"
        " e <program>: execute program\n"
        " h <id>     : set task identified by id to high priority\n"
        " l <id>     : set task identified by id to low priority\n");
}

canonical_original_code:
void help()
{
 printf(" ?          : print help\n"
        " q          : quit\n"
        " p          : print tasks\n"
        " k <id>     : kill task identified by id\n"
        " e <program>: execute program\n"
        " h <id>     : set task identified by id to high priority\n"
        " l <id>     : set task identified by id to low priority\n");
}


decompiled_code:
void __noreturn wait_forever()
{
while ( 1 )
sleep(0x64u);
}

canonical_decompiled_code:
void __noreturn func11()
{
while ( 1 )
sleep(0x64u);
}

original_code:
void
wait_forever(void)
{
 do {
  sleep(100);
 } while (1);
}

canonical_original_code:
void wait_forever()
{
 do {
  sleep(100);
 } while (1);
}


compute

decompiled_code:
__int64 __fastcall compute(int a1)
{
__int64 v1; // rdi
__int64 i; // rdx
__int64 result; // rax
__int64 v4; // [rsp+0h] [rbp-8h]

v4 = 0LL;
v1 = 1000000 * a1;
if ( v1 > 0 )
{
for ( i = 0LL; i != v1; ++i )
result = ++v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1)
{
__int64 v1; // rdi
__int64 i; // rdx
__int64 result; // rax
__int64 v4; // [rsp+0h] [rbp-8h]

v4 = 0LL;
v1 = 1000000 * a1;
if ( v1 > 0 )
{
for ( i = 0LL; i != v1; ++i )
result = ++v4;
}
return result;
}

original_code:
void compute(int count)
{
 long i;
 volatile long junk;
 junk = 0;
 for (i = 0; i < count * 1000000; i++) {
  junk++;
 }
}

canonical_original_code:
void compute(int count)
{
 long i;
 long junk;
 junk = 0;
 for (i = 0; i < count * 1000000; i++) {
  junk++;
 }
}

variable_types: count int

variable_types: i long

variable_types: junk long


decompiled_code:
int __fastcall change_pname(__int64 a1)
{
int result; // eax

result = prctl(15, a1);
if ( result == -1 )
{
perror("prctl set_name");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1)
{
int result; // eax

result = prctl(15, a1);
if ( result == -1 )
{
perror("prctl set_name");
exit(1);
}
return result;
}

original_code:
void
change_pname(const char *new_name)
{
 int ret;
 ret = prctl(15, new_name);
 if (ret == -1){
  perror("prctl set_name");
  exit(1);
 }
}

canonical_original_code:
void change_pname(char *new_name)
{
 int ret;
 ret = prctl(15, new_name);
 if (ret == -1){
  perror("prctl set_name");
  exit(1);
 }
}

variable_types: ret int


decompiled_code:
void __fastcall wait_for_ready_children(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
explain_wait_status(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}

canonical_decompiled_code:
void __fastcall func15(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
func14(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}

original_code:
void
wait_for_ready_children(int cnt)
{
 int i;
 pid_t p;
 int status;
 for (i = 0; i < cnt; i++) {
  p = waitpid(-1, &status, 2);
  explain_wait_status(p, status);
  if (!(((status) & 0xff) == 0x7f)) {
   fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n",
    (long)p);
   exit(1);
  }
 }
}

canonical_original_code:
void wait_for_ready_children(int cnt)
{
 int i;
 int p;
 int status;
 for (i = 0; i < cnt; i++) {
  p = waitpid(-1, &status, 2);
  explain_wait_status(p, status);
  if (!(((status) & 0xff) == 0x7f)) {
   fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n",
    (long)p);
   exit(1);
  }
 }
}

variable_types: cnt int

variable_types: i int


decompiled_code:
int __fastcall show_pstree(int a1)
{
int result; // eax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

snprintf(v2, 0x400uLL, "echo; echo; pstree -G -c -p %ld; echo; echo", a1);
v2[1023] = 0;
result = system(v2);
if ( result < 0 )
{
perror("system");
exit(104);
}
return result;
}

canonical_decompiled_code:
int __fastcall func16(int a1)
{
int result; // eax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

snprintf(v2, 0x400uLL, "echo; echo; pstree -G -c -p %ld; echo; echo", a1);
v2[1023] = 0;
result = system(v2);
if ( result < 0 )
{
perror("system");
exit(104);
}
return result;
}

original_code:
void
show_pstree(pid_t p)
{
 int ret;
 char cmd[1024];
 snprintf(cmd, sizeof(cmd), "echo; echo; pstree -G -c -p %ld; echo; echo",
  (long)p);
 cmd[sizeof(cmd)-1] = '\0';
 ret = system(cmd);
 if (ret < 0) {
  perror("system");
  exit(104);
 }
}

canonical_original_code:
void show_pstree(int p)
{
 int ret;
 char cmd[1024];
 snprintf(cmd, sizeof(cmd), "echo; echo; pstree -G -c -p %ld; echo; echo",
  (long)p);
 cmd[sizeof(cmd)-1] = '\0';
 ret = system(cmd);
 if (ret < 0) {
  perror("system");
  exit(104);
 }
}

variable_types:
cmd
char [1024]


decompiled_code:
void *__fastcall create_shared_memory_area(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(stderr, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func17(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(stderr, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}

original_code:
void *create_shared_memory_area(unsigned int numbytes)
{
 int pages;
 void *addr;
 if (numbytes == 0) {
  fprintf(stderr, "%s: internal error: called for numbytes == 0\n", __func__);
  exit(1);
 }
 pages = (numbytes - 1) / sysconf(_SC_PAGESIZE) + 1;
 addr = mmap(((void *)0), pages * sysconf(_SC_PAGESIZE),
  0x1 | 0x2, 0x01 | 0x20, -1, 0);
 if (addr == ((void *) -1)) {
  perror("create_shared_memory_area: mmap failed");
  exit(1);
 }
 return addr;
}

variable_types:
numbytes
unsigned int

variable_types: pages int

variable_types:
addr
void *

return_type: void *


canonical_decompiled_code:
void __noreturn func12()
{
while ( 1 )
sleep(0x64u);
}


canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
__int64 v1; // rdi
__int64 i; // rdx
__int64 result; // rax
__int64 v4; // [rsp+0h] [rbp-8h]

v4 = 0LL;
v1 = 1000000 * a1;
if ( v1 > 0 )
{
for ( i = 0LL; i != v1; ++i )
result = ++v4;
}
return result;
}


canonical_decompiled_code:
int __fastcall func14(__int64 a1)
{
int result; // eax

result = prctl(15, a1);
if ( result == -1 )
{
perror("prctl set_name");
exit(1);
}
return result;
}


canonical_decompiled_code:
void __fastcall func16(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
func15(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}

variable_types: p int

variable_types: status int


canonical_decompiled_code:
int __fastcall func17(int a1)
{
int result; // eax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

snprintf(v2, 0x400uLL, "echo; echo; pstree -G -c -p %ld; echo; echo", a1);
v2[1023] = 0;
result = system(v2);
if ( result < 0 )
{
perror("system");
exit(104);
}
return result;
}


canonical_decompiled_code:
void *__fastcall func18(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(stderr, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}

canonical_original_code:
void *create_shared_memory_area(unsigned int numbytes)
{
 int pages;
 void *addr;
 if (numbytes == 0) {
  fprintf(stderr, "%s: internal error: called for numbytes == 0\n", __func__);
  exit(1);
 }
 pages = (numbytes - 1) / sysconf(_SC_PAGESIZE) + 1;
 addr = mmap(((void *)0), pages * sysconf(_SC_PAGESIZE),
  0x1 | 0x2, 0x01 | 0x20, -1, 0);
 if (addr == ((void *) -1)) {
  perror("create_shared_memory_area: mmap failed");
  exit(1);
 }
 return addr;
}


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

result = prctl(15, a1);
if ( result == -1 )
{
perror("prctl set_name");
exit(1);
}
return result;
}


decompiled_code:
void __fastcall wait_for_ready_children(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
explain_wait_status(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(_bss_start, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}

canonical_decompiled_code:
void __fastcall func14(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
func13(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(_bss_start, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}


canonical_decompiled_code:
int __fastcall func15(int a1)
{
int result; // eax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

snprintf(v2, 0x400uLL, "echo; echo; pstree -G -c -p %ld; echo; echo", a1);
v2[1023] = 0;
result = system(v2);
if ( result < 0 )
{
perror("system");
exit(104);
}
return result;
}


decompiled_code:
void *__fastcall create_shared_memory_area(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(_bss_start, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func16(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(_bss_start, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}


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

result = prctl(15, a1);
if ( result == -1 )
{
perror("prctl set_name");
exit(1);
}
return result;
}


canonical_decompiled_code:
void __noreturn func8()
{
while ( 1 )
sleep(0x64u);
}


canonical_decompiled_code:
void __fastcall func10(int a1)
{
int i; // ebp
int v2; // ebx
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v2 = waitpid(-1, stat_loc, 2);
func9(v2, stat_loc[0]);
if ( LOBYTE(stat_loc[0]) != 127 )
{
fprintf(stderr, "Parent: Child with PID %ld has died unexpectedly!\n", v2);
exit(1);
}
}
}
}


canonical_decompiled_code:
int __fastcall func11(int a1)
{
int result; // eax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

snprintf(v2, 0x400uLL, "echo; echo; pstree -G -c -p %ld; echo; echo", a1);
v2[1023] = 0;
result = system(v2);
if ( result < 0 )
{
perror("system");
exit(104);
}
return result;
}


canonical_decompiled_code:
void *__fastcall func12(int a1)
{
__int64 v1; // rbp
__int64 v2; // rax
void *result; // rax

if ( !a1 )
{
fprintf(stderr, "%s: internal error: called for numbytes == 0\n", "create_shared_memory_area");
exit(1);
}
v1 = sysconf(30);
v2 = sysconf(30);
result = mmap(0LL, v2 * (int)((unsigned int)(a1 - 1) / v1 + 1), 3, 33, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("create_shared_memory_area: mmap failed");
exit(1);
}
return result;
}


repo: shoban/udpreplay

decompiled_code:
int __fastcall udpreplay_dump(const char *a1, const char *a2, char *a3, int a4)
{
int v4; // er14
int v7; // eax
unsigned int v8; // ebp
struct addrinfo *v9; // r13
char *v10; // r12
size_t v11; // rbx
const char *v12; // rax
unsigned int v13; // eax
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v7 = getaddrinfo(a1, a2, &req, &pai);
v8 = v7;
if ( v7 )
{
v12 = gai_strerror(v7);
fprintf(stderr, "getaddrinfo: %s\n", v12);
}
else
{
v9 = pai;
if ( pai )
{
while ( 1 )
{
v4 = socket(v9->ai_family, v9->ai_socktype, v9->ai_protocol);
if ( v4 != -1 )
break;
perror("socket");
v9 = v9->ai_next;
if ( !v9 )
goto LABEL_10;
}
if ( a4 )
{
while ( 1 )
{
v10 = a3 + 2;
v11 = *(unsigned __int16 *)a3;
if ( sendto(v4, v10, v11, 0, v9->ai_addr, v9->ai_addrlen) == -1 )
break;
a3 = &v10[v11];
if ( a4 == ++v8 )
goto LABEL_12;
}
perror("sendto() error");
}
}
else
{
LABEL_10:
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
v4 = -1;
}
}
LABEL_12:
freeaddrinfo(pai);
v13 = atoi(a2);
fprintf(stdout, "%d pkts sent to %s:%d.\n", v8, a1, v13);
return close(v4);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1, const char *a2, char *a3, int a4)
{
int v4; // er14
int v7; // eax
unsigned int v8; // ebp
struct addrinfo *v9; // r13
char *v10; // r12
size_t v11; // rbx
const char *v12; // rax
unsigned int v13; // eax
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v7 = getaddrinfo(a1, a2, &req, &pai);
v8 = v7;
if ( v7 )
{
v12 = gai_strerror(v7);
fprintf(stderr, "getaddrinfo: %s\n", v12);
}
else
{
v9 = pai;
if ( pai )
{
while ( 1 )
{
v4 = socket(v9->ai_family, v9->ai_socktype, v9->ai_protocol);
if ( v4 != -1 )
break;
perror("socket");
v9 = v9->ai_next;
if ( !v9 )
goto LABEL_10;
}
if ( a4 )
{
while ( 1 )
{
v10 = a3 + 2;
v11 = *(unsigned __int16 *)a3;
if ( sendto(v4, v10, v11, 0, v9->ai_addr, v9->ai_addrlen) == -1 )
break;
a3 = &v10[v11];
if ( a4 == ++v8 )
goto LABEL_12;
}
perror("sendto() error");
}
}
else
{
LABEL_10:
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
v4 = -1;
}
}
LABEL_12:
freeaddrinfo(pai);
v13 = atoi(a2);
fprintf(stdout, "%d pkts sent to %s:%d.\n", v8, a1, v13);
return close(v4);
}

original_code:
void udpreplay_dump(
        const char *dst_ip, const char *dst_port,
        uint8_t *data, uint32_t no_pkts)
{
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv, i;
    int numbytes;
    uint16_t len;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo(dst_ip, dst_port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        goto end;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("socket");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "failed to bind socket\n");
        goto end;
    }
    for (i = 0; i < no_pkts; i++) {
        memcpy(&len, data, sizeof(len));
        data += 2;
        if (sendto(sockfd, data, len, 0,
                    p->ai_addr, p->ai_addrlen) == -1) {
            perror("sendto() error");
            goto end;
        }
        data += len;
    }
end:
    freeaddrinfo(servinfo);
    fprintf(stdout, "%d pkts sent to %s:%d.\n", i, dst_ip, atoi(dst_port));
    close(sockfd);
}

canonical_original_code:
void udpreplay_dump(char *dst_ip, char *dst_port, unsigned char *data, unsigned int no_pkts)
{
    int sockfd;
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
    int rv; int i;
    int numbytes;
    unsigned short len;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo(dst_ip, dst_port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        goto end;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("socket");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "failed to bind socket\n");
        goto end;
    }
    for (i = 0; i < no_pkts; i++) {
        memcpy(&len, data, sizeof(len));
        data += 2;
        if (sendto(sockfd, data, len, 0,
                    p->ai_addr, p->ai_addrlen) == -1) {
            perror("sendto() error");
            goto end;
        }
        data += len;
    }
end:
    freeaddrinfo(servinfo);
    fprintf(stdout, "%d pkts sent to %s:%d.\n", i, dst_ip, atoi(dst_port));
    close(sockfd);
}

variable_types:
data
unsigned char *

variable_types: sockfd int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: rv int

variable_types: numbytes int

variable_types:
len
unsigned short

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall read_pkt(__int64 a1, int a2, __int64 a3, unsigned int a4, unsigned int a5)
{
int v8; // eax
unsigned int v9; // ebx
__int16 v10; // dx
__int64 result; // rax
unsigned __int16 v12; // dx
unsigned __int16 v13; // bx
char src[23]; // [rsp+0h] [rbp-2358h] BYREF
char v15; // [rsp+17h] [rbp-2341h]

v8 = read(a2, src, *(unsigned int *)(a1 + 8));
v9 = *(_DWORD *)(a1 + 8);
if ( v9 != v8 )
__assert_fail("rbytes == rh->incl_len", "pcap.c", 0x1Cu, "read_pkt");
v10 = 4 * (src[14] & 0xF);
if ( v15 == 6 )
{
v12 = v10 + 14 + 4 * ((unsigned __int8)src[(unsigned __int16)(v10 + 14) + 12] >> 4);
}
else
{
result = 0LL;
if ( v15 != 17 )
return result;
v12 = v10 + 22;
}
if ( v9 <= v12 )
return 0xFFFFFFFFLL;
v13 = v9 - v12;
if ( a4 + v13 + 2 >= a5 )
return 0xFFFFFFFFLL;
*(_WORD *)(a3 + a4) = v13;
memcpy((void *)(a3 + a4 + 2), &src[v12], v13);
return (unsigned int)v13 + 2;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, __int64 a3, unsigned int a4, unsigned int a5)
{
int v8; // eax
unsigned int v9; // ebx
__int16 v10; // dx
__int64 result; // rax
unsigned __int16 v12; // dx
unsigned __int16 v13; // bx
char src[23]; // [rsp+0h] [rbp-2358h] BYREF
char v15; // [rsp+17h] [rbp-2341h]

v8 = read(a2, src, *(unsigned int *)(a1 + 8));
v9 = *(_DWORD *)(a1 + 8);
if ( v9 != v8 )
__assert_fail("rbytes == rh->incl_len", "pcap.c", 0x1Cu, "read_pkt");
v10 = 4 * (src[14] & 0xF);
if ( v15 == 6 )
{
v12 = v10 + 14 + 4 * ((unsigned __int8)src[(unsigned __int16)(v10 + 14) + 12] >> 4);
}
else
{
result = 0LL;
if ( v15 != 17 )
return result;
v12 = v10 + 22;
}
if ( v9 <= v12 )
return 0xFFFFFFFFLL;
v13 = v9 - v12;
if ( a4 + v13 + 2 >= a5 )
return 0xFFFFFFFFLL;
*(_WORD *)(a3 + a4) = v13;
memcpy((void *)(a3 + a4 + 2), &src[v12], v13);
return (unsigned int)v13 + 2;
}

original_code:
int read_pkt(pcaprec_hdr_t *rh, int fd, uint8_t *data, uint32_t cur, uint32_t max)
{
    uint8_t tmp[9000];
    struct my_ip *iph;
    struct my_udp *uh;
    struct my_tcp *th;
    uint16_t len;
    int rbytes;
    rbytes = read(fd, tmp, rh->incl_len);
    ((void) sizeof ((rbytes == rh->incl_len) ? 1 : 0), __extension__ ({ if (rbytes == rh->incl_len) ; else __assert_fail ("rbytes == rh->incl_len", "pcap.c", 28, __extension__ __PRETTY_FUNCTION__); }));
    uint16_t start = 14;
    iph = (struct my_ip *) (tmp+start);
    start += iph->ip_hl << 2;
    if (iph->ip_p == 6) {
        th = (struct my_tcp *) (tmp + start);
        start += th->th_off << 2;
    } else if (iph->ip_p == 17) {
        uh = (struct my_udp *) (tmp + start);
        start += sizeof(struct my_udp);
    } else {
        return 0;
    }
    if (start >= rh->incl_len) goto err;
    len = rh->incl_len - start;
    if (cur + 2 + len >= max) goto err;
    memcpy(data+cur, &len, sizeof(len));
    memcpy(data+cur+2, tmp+start, len);
    return 2 + len;
err:
    return -1;
}

canonical_original_code:
int read_pkt(struct pcaprec_hdr_s *rh, int fd, unsigned char *data, unsigned int cur, unsigned int max)
{
    unsigned char tmp[9000];
    struct my_ip *iph;
    struct my_udp *uh;
    struct my_tcp *th;
    unsigned short len;
    int rbytes;
    rbytes = read(fd, tmp, rh->incl_len);
    ((void) sizeof ((rbytes == rh->incl_len) ? 1 : 0), __extension__ ({ if (rbytes == rh->incl_len) ; else __assert_fail ("rbytes == rh->incl_len", "pcap.c", 28, __extension__ __PRETTY_FUNCTION__); }));
    unsigned short start = 14;
    iph = (struct my_ip *) (tmp+start);
    start += iph->ip_hl << 2;
    if (iph->ip_p == 6) {
        th = (struct my_tcp *) (tmp + start);
        start += th->th_off << 2;
    } else if (iph->ip_p == 17) {
        uh = (struct my_udp *) (tmp + start);
        start += sizeof(struct my_udp);
    } else {
        return 0;
    }
    if (start >= rh->incl_len) goto err;
    len = rh->incl_len - start;
    if (cur + 2 + len >= max) goto err;
    memcpy(data+cur, &len, sizeof(len));
    memcpy(data+cur+2, tmp+start, len);
    return 2 + len;
err:
    return -1;
}

variable_types: struct pcaprec_hdr_s { unsigned int ts_sec; unsigned int ts_usec; unsigned int incl_len; unsigned int orig_len; } *

variable_types: fd int

variable_types:
cur
unsigned int

variable_types:
max
unsigned int

variable_types:
tmp
unsigned char [9000]

variable_types: struct my_ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; unsigned int ip_src; unsigned int ip_dst; } *

variable_types: struct my_udp { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; } *

variable_types: struct my_tcp { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned int th_flags2; unsigned int th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; } *

variable_types: rbytes int

variable_types:
start
unsigned short


decompiled_code:
__int64 __fastcall read_file(const char *a1, unsigned int *a2, __int64 a3, unsigned int a4)
{
int v6; // eax
int v7; // ebx
unsigned int v8; // er12
unsigned int v9; // ebp
unsigned int v10; // eax
unsigned int v11; // er15
int v12; // eax
char v14[16]; // [rsp+10h] [rbp-68h] BYREF
char buf[88]; // [rsp+20h] [rbp-58h] BYREF

v6 = open(a1, 0);
v7 = v6;
if ( v6 < 0 )
{
fprintf(stderr, "Unable to read file: %s\n", a1);
v11 = v7;
}
else
{
if ( read(v6, buf, 0x18uLL) != 24 )
__assert_fail("read(fd, &h, sizeof(h)) == sizeof(h)", "pcap.c", 0x49u, "read_file");
v8 = 0;
v9 = 0;
while ( 1 )
{
v10 = read(v7, v14, 0x10uLL);
v11 = v10;
if ( !v10 )
break;
if ( v10 != 16 )
__assert_fail("rbytes == sizeof(rh)", "pcap.c", 0x4Bu, "read_file");
v12 = read_pkt((__int64)v14, v7, a3, v9, a4);
if ( v12 > 0 )
{
v9 += v12;
++v8;
}
}
fprintf(stdout, "data in mem: %u pkts, %u bytes.\n", v8, v9);
if ( a2 )
*a2 = v8;
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, unsigned int *a2, __int64 a3, unsigned int a4)
{
int v6; // eax
int v7; // ebx
unsigned int v8; // er12
unsigned int v9; // ebp
unsigned int v10; // eax
unsigned int v11; // er15
int v12; // eax
char v14[16]; // [rsp+10h] [rbp-68h] BYREF
char buf[88]; // [rsp+20h] [rbp-58h] BYREF

v6 = open(a1, 0);
v7 = v6;
if ( v6 < 0 )
{
fprintf(stderr, "Unable to read file: %s\n", a1);
v11 = v7;
}
else
{
if ( read(v6, buf, 0x18uLL) != 24 )
__assert_fail("read(fd, &h, sizeof(h)) == sizeof(h)", "pcap.c", 0x49u, "read_file");
v8 = 0;
v9 = 0;
while ( 1 )
{
v10 = read(v7, v14, 0x10uLL);
v11 = v10;
if ( !v10 )
break;
if ( v10 != 16 )
__assert_fail("rbytes == sizeof(rh)", "pcap.c", 0x4Bu, "read_file");
v12 = func9((__int64)v14, v7, a3, v9, a4);
if ( v12 > 0 )
{
v9 += v12;
++v8;
}
}
fprintf(stdout, "data in mem: %u pkts, %u bytes.\n", v8, v9);
if ( a2 )
*a2 = v8;
}
return v11;
}

original_code:
int read_file(const char *file, uint32_t *no_pkts, uint8_t *data, uint32_t max)
{
    int fd, rbytes;
    pcap_hdr_t h;
    pcaprec_hdr_t rh;
    uint32_t cur = 0;
    uint32_t pkts = 0;
    fd = open(file, 00);
    if (fd < 0) {
        fprintf(stderr, "Unable to read file: %s\n", file);
        return fd;
    }
    ((void) sizeof ((read(fd, &h, sizeof(h)) == sizeof(h)) ? 1 : 0), __extension__ ({ if (read(fd, &h, sizeof(h)) == sizeof(h)) ; else __assert_fail ("read(fd, &h, sizeof(h)) == sizeof(h)", "pcap.c", 73, __extension__ __PRETTY_FUNCTION__); }));
    while ((rbytes = read(fd, &rh, sizeof(rh)))) {
        ((void) sizeof ((rbytes == sizeof(rh)) ? 1 : 0), __extension__ ({ if (rbytes == sizeof(rh)) ; else __assert_fail ("rbytes == sizeof(rh)", "pcap.c", 75, __extension__ __PRETTY_FUNCTION__); }));
        rbytes = read_pkt(&rh, fd, data, cur, max);
        if (rbytes > 0) cur += rbytes, pkts++;
    }
    fprintf(stdout, "data in mem: %u pkts, %u bytes.\n", pkts, cur);
    if (no_pkts) *no_pkts = pkts;
    return 0;
}

canonical_original_code:
int read_file(char *file, unsigned int *no_pkts, unsigned char *data, unsigned int max)
{
    int fd; int rbytes;
    struct pcap_hdr_s h;
    struct pcaprec_hdr_s rh;
    unsigned int cur = 0;
    unsigned int pkts = 0;
    fd = open(file, 00);
    if (fd < 0) {
        fprintf(stderr, "Unable to read file: %s\n", file);
        return fd;
    }
    ((void) sizeof ((read(fd, &h, sizeof(h)) == sizeof(h)) ? 1 : 0), __extension__ ({ if (read(fd, &h, sizeof(h)) == sizeof(h)) ; else __assert_fail ("read(fd, &h, sizeof(h)) == sizeof(h)", "pcap.c", 73, __extension__ __PRETTY_FUNCTION__); }));
    while ((rbytes = read(fd, &rh, sizeof(rh)))) {
        ((void) sizeof ((rbytes == sizeof(rh)) ? 1 : 0), __extension__ ({ if (rbytes == sizeof(rh)) ; else __assert_fail ("rbytes == sizeof(rh)", "pcap.c", 75, __extension__ __PRETTY_FUNCTION__); }));
        rbytes = read_pkt(&rh, fd, data, cur, max);
        if (rbytes > 0) cur += rbytes, pkts++;
    }
    fprintf(stdout, "data in mem: %u pkts, %u bytes.\n", pkts, cur);
    if (no_pkts) *no_pkts = pkts;
    return 0;
}

variable_types:
file
char *

variable_types: struct pcap_hdr_s { unsigned int magic_number; unsigned short version_major; unsigned short version_minor; int thiszone; unsigned int sigfigs; unsigned int snaplen; unsigned int network; }

variable_types: struct pcaprec_hdr_s { unsigned int ts_sec; unsigned int ts_usec; unsigned int incl_len; unsigned int orig_len; }

variable_types:
pkts
unsigned int


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(stderr, "USAGE: %s <pcap-dump> <dst-ip> <dst-port>\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(stderr, "USAGE: %s <pcap-dump> <dst-ip> <dst-port>\n", a1);
}

original_code:
void usage(const char *p)
{
    fprintf(stderr, "USAGE: %s <pcap-dump> <dst-ip> <dst-port>\n", p);
}

canonical_original_code:
void usage(char *p)
{
    fprintf(stderr, "USAGE: %s <pcap-dump> <dst-ip> <dst-port>\n", p);
}

variable_types:
p
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // rbp
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc != 4 )
{
usage(*argv);
exit(1);
}
v3 = (char *)malloc(0x40000000uLL);
v4 = v3;
if ( !v3 )
{
fwrite("Unable to alloc.\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
read_file(argv[1], v6, v3, 0x40000000LL);
udpreplay_dump(argv[2], argv[3], v4, v6[0]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // rbp
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc != 4 )
{
func7(*argv);
exit(1);
}
v3 = (char *)malloc(0x40000000uLL);
v4 = v3;
if ( !v3 )
{
fwrite("Unable to alloc.\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
func10(argv[1], v6, v3, 0x40000000LL);
func8(argv[2], argv[3], v4, v6[0]);
return 0;
}

original_code:
int main(int argc, const char **argv)
{
    int max;
    uint8_t *data;
    uint32_t no_pkts;
    if (argc != 4) {
        usage(argv[0]);
        exit(1);
    }
    max = 1024*1024*1024;
    data = (uint8_t *) malloc(max);
    if (!data) {
        fprintf(stderr, "Unable to alloc.\n");
        exit(1);
    }
    read_file(argv[1], &no_pkts, data, max);
    udpreplay_dump(
            argv[2], argv[3],
            data, no_pkts);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int max;
    unsigned char *data;
    unsigned int no_pkts;
    if (argc != 4) {
        usage(argv[0]);
        exit(1);
    }
    max = 1024*1024*1024;
    data = (unsigned char *) malloc(max);
    if (!data) {
        fprintf(stderr, "Unable to alloc.\n");
        exit(1);
    }
    read_file(argv[1], &no_pkts, data, max);
    udpreplay_dump(
            argv[2], argv[3],
            data, no_pkts);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: max int

variable_types:
data
unsigned char *


original_code:
void ts_add(struct timespec *a, uint32_t nanosecs)
{
    a->tv_nsec = a->tv_nsec + nanosecs;
    a->tv_sec += a->tv_nsec/1000000000;
    a->tv_nsec = a->tv_nsec%1000000000;
}

canonical_original_code:
void ts_add(struct timespec *a, unsigned int nanosecs)
{
    a->tv_nsec = a->tv_nsec + nanosecs;
    a->tv_sec += a->tv_nsec/1000000000;
    a->tv_nsec = a->tv_nsec%1000000000;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

variable_types:
nanosecs
unsigned int


decompiled_code:
double __fastcall ts_diff(_DWORD *a1, _DWORD *a2)
{
return (double)(a1[2] - a2[2]) * 0.000000001 + (double)(*a1 - *a2);
}

canonical_decompiled_code:
double __fastcall func8(_DWORD *a1, _DWORD *a2)
{
return (double)(a1[2] - a2[2]) * 0.000000001 + (double)(*a1 - *a2);
}

original_code:
double ts_diff(const struct timespec *a, const struct timespec *b)
{
    double r = a->tv_sec - b->tv_sec;
    r += (a->tv_nsec - b->tv_nsec)*1E-9;
    return r;
}

canonical_original_code:
double ts_diff(struct timespec *a, struct timespec *b)
{
    double r = a->tv_sec - b->tv_sec;
    r += (a->tv_nsec - b->tv_nsec)*1E-9;
    return r;
}

variable_types:
b
struct timespec *

variable_types: r double

return_type: double

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


decompiled_code:
int __fastcall udpplay_rate(const char *a1, const char *a2, char *a3, unsigned int a4, unsigned int a5, int a6)
{
int v6; // er15
int v9; // eax
struct addrinfo *v10; // r12
unsigned int v11; // ebp
unsigned int v12; // er14
const char *v13; // rax
double v14; // xmm0_8
unsigned int n_4; // [rsp+8h] [rbp-A0h]
struct timespec v19; // [rsp+10h] [rbp-98h] BYREF
struct timespec tp; // [rsp+20h] [rbp-88h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v9 = getaddrinfo(a1, a2, &req, &pai);
if ( v9 )
{
v13 = gai_strerror(v9);
fprintf(stderr, "getaddrinfo: %s\n", v13);
}
else
{
v10 = pai;
if ( pai )
{
while ( 1 )
{
v6 = socket(v10->ai_family, v10->ai_socktype, v10->ai_protocol);
if ( v6 != -1 )
break;
perror("socket");
v10 = v10->ai_next;
if ( !v10 )
goto LABEL_15;
}
clock_gettime(1, &tp);
v19 = (struct timespec)_mm_loadu_si128((const __m128i *)&tp);
n_4 = a4 / a5;
fprintf(stdout, "size=%u, pkt_size=%u, no_pkts=%u.\n", a4, a5, a4 / a5);
v11 = 0;
if ( a4 < a5 )
{
LABEL_12:
clock_gettime(1, &v19);
}
else
{
while ( 1 )
{
ts_add(&v19, 0xF4240u);
clock_nanosleep(1, 1, &v19, 0LL);
v12 = (int)((double)a6 * ts_diff(&v19, &tp) * 1024.0 * 1024.0 / (double)(int)a5);
if ( v12 > n_4 )
v12 = a4 / a5;
if ( v12 > v11 )
break;
LABEL_11:
if ( n_4 <= v11 )
goto LABEL_12;
}
while ( sendto(v6, a3, a5, 0, v10->ai_addr, v10->ai_addrlen) != -1 )
{
a3 += a5;
if ( ++v11 == v12 )
{
v11 = v12;
goto LABEL_11;
}
}
perror("sendto() error");
}
}
else
{
LABEL_15:
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
v6 = -1;
}
}
freeaddrinfo(pai);
v14 = ts_diff(&v19, &tp);
fprintf(stdout, "sent %u pkts, %u size in time %f secs.\n", n_4, n_4 * a5, v14);
return close(v6);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1, const char *a2, char *a3, unsigned int a4, unsigned int a5, int a6)
{
int v6; // er15
int v9; // eax
struct addrinfo *v10; // r12
unsigned int v11; // ebp
unsigned int v12; // er14
const char *v13; // rax
double v14; // xmm0_8
unsigned int n_4; // [rsp+8h] [rbp-A0h]
struct timespec v19; // [rsp+10h] [rbp-98h] BYREF
struct timespec tp; // [rsp+20h] [rbp-88h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v9 = getaddrinfo(a1, a2, &req, &pai);
if ( v9 )
{
v13 = gai_strerror(v9);
fprintf(stderr, "getaddrinfo: %s\n", v13);
}
else
{
v10 = pai;
if ( pai )
{
while ( 1 )
{
v6 = socket(v10->ai_family, v10->ai_socktype, v10->ai_protocol);
if ( v6 != -1 )
break;
perror("socket");
v10 = v10->ai_next;
if ( !v10 )
goto LABEL_15;
}
clock_gettime(1, &tp);
v19 = (struct timespec)_mm_loadu_si128((const __m128i *)&tp);
n_4 = a4 / a5;
fprintf(stdout, "size=%u, pkt_size=%u, no_pkts=%u.\n", a4, a5, a4 / a5);
v11 = 0;
if ( a4 < a5 )
{
LABEL_12:
clock_gettime(1, &v19);
}
else
{
while ( 1 )
{
func7(&v19, 0xF4240u);
clock_nanosleep(1, 1, &v19, 0LL);
v12 = (int)((double)a6 * func8(&v19, &tp) * 1024.0 * 1024.0 / (double)(int)a5);
if ( v12 > n_4 )
v12 = a4 / a5;
if ( v12 > v11 )
break;
LABEL_11:
if ( n_4 <= v11 )
goto LABEL_12;
}
while ( sendto(v6, a3, a5, 0, v10->ai_addr, v10->ai_addrlen) != -1 )
{
a3 += a5;
if ( ++v11 == v12 )
{
v11 = v12;
goto LABEL_11;
}
}
perror("sendto() error");
}
}
else
{
LABEL_15:
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
v6 = -1;
}
}
freeaddrinfo(pai);
v14 = func8(&v19, &tp);
fprintf(stdout, "sent %u pkts, %u size in time %f secs.\n", n_4, n_4 * a5, v14);
return close(v6);
}

original_code:
void udpplay_rate(
        const char *dst_ip, const char *dst_port,
        uint8_t *data, uint32_t size,
        uint32_t pkt_size, uint32_t rate)
{
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv, i;
    int numbytes;
    uint16_t len;
    struct timespec st, ct, slt;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo(dst_ip, dst_port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        goto end;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("socket");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "failed to bind socket\n");
        goto end;
    }
    clock_gettime(1, &st);
    slt = st;
    uint32_t no_pkts = size/pkt_size;
    uint32_t sent = 0;
    uint32_t must_send = 0;
    fprintf(stdout, "size=%u, pkt_size=%u, no_pkts=%u.\n", size, pkt_size, no_pkts);
    while (sent < no_pkts) {
        ts_add(&slt, 1000000);
        clock_nanosleep(1, 1, &slt, ((void *)0));
        must_send = ts_diff(&slt, &st)*rate*1024*1024/pkt_size;
        while (sent < must_send && sent < no_pkts) {
            if (sendto(sockfd, data, pkt_size, 0,
                        p->ai_addr, p->ai_addrlen) == -1) {
                perror("sendto() error");
                goto end;
            }
            data += pkt_size;
            sent++;
        }
    }
    clock_gettime(1, &slt);
end:
    freeaddrinfo(servinfo);
    fprintf(stdout, "sent %u pkts, %u size in time %f secs.\n",
            no_pkts, no_pkts*pkt_size, ts_diff(&slt, &st));
    close(sockfd);
}

canonical_original_code:
void udpplay_rate(char *dst_ip, char *dst_port, unsigned char *data, unsigned int size, unsigned int pkt_size, unsigned int rate)
{
    int sockfd;
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
    int rv; int i;
    int numbytes;
    unsigned short len;
    struct timespec st; struct timespec ct; struct timespec slt;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo(dst_ip, dst_port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        goto end;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("socket");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "failed to bind socket\n");
        goto end;
    }
    clock_gettime(1, &st);
    slt = st;
    unsigned int no_pkts = size/pkt_size;
    unsigned int sent = 0;
    unsigned int must_send = 0;
    fprintf(stdout, "size=%u, pkt_size=%u, no_pkts=%u.\n", size, pkt_size, no_pkts);
    while (sent < no_pkts) {
        ts_add(&slt, 1000000);
        clock_nanosleep(1, 1, &slt, ((void *)0));
        must_send = ts_diff(&slt, &st)*rate*1024*1024/pkt_size;
        while (sent < must_send && sent < no_pkts) {
            if (sendto(sockfd, data, pkt_size, 0,
                        p->ai_addr, p->ai_addrlen) == -1) {
                perror("sendto() error");
                goto end;
            }
            data += pkt_size;
            sent++;
        }
    }
    clock_gettime(1, &slt);
end:
    freeaddrinfo(servinfo);
    fprintf(stdout, "sent %u pkts, %u size in time %f secs.\n",
            no_pkts, no_pkts*pkt_size, ts_diff(&slt, &st));
    close(sockfd);
}

variable_types:
size
unsigned int

variable_types:
rate
unsigned int

variable_types: sockfd int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: rv int

variable_types: i int

variable_types: numbytes int

variable_types:
len
unsigned short

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

variable_types:
sent
unsigned int


decompiled_code:
__int64 __fastcall read_data_file(const char *a1, __int64 a2, unsigned int a3, _DWORD *a4)
{
int v6; // ebp
int v7; // ebx
int v8; // eax

v6 = open(a1, 0);
if ( v6 < 0 )
{
fprintf(stderr, "Unable to read file: %s\n", a1);
}
else
{
v7 = 0;
if ( a3 > 0x400 )
{
while ( 1 )
{
v8 = read(v6, (void *)(a2 + v7), 0x400uLL);
if ( v8 < 0 )
break;
if ( v8 )
{
v7 += v8;
if ( v7 + 1024 < a3 )
continue;
}
goto LABEL_9;
}
perror("read() error");
}
LABEL_9:
fprintf(stdout, "read %u mb in memory.\n", (unsigned int)(v7 / 0x100000));
*a4 = v7;
v6 = 0;
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, __int64 a2, unsigned int a3, _DWORD *a4)
{
int v6; // ebp
int v7; // ebx
int v8; // eax

v6 = open(a1, 0);
if ( v6 < 0 )
{
fprintf(stderr, "Unable to read file: %s\n", a1);
}
else
{
v7 = 0;
if ( a3 > 0x400 )
{
while ( 1 )
{
v8 = read(v6, (void *)(a2 + v7), 0x400uLL);
if ( v8 < 0 )
break;
if ( v8 )
{
v7 += v8;
if ( v7 + 1024 < a3 )
continue;
}
goto LABEL_9;
}
perror("read() error");
}
LABEL_9:
fprintf(stdout, "read %u mb in memory.\n", (unsigned int)(v7 / 0x100000));
*a4 = v7;
v6 = 0;
}
return (unsigned int)v6;
}

original_code:
int read_data_file(const char *file, uint8_t *data, uint32_t max,
        uint32_t *read_bytes)
{
    int fd, rb, tb;
    fd = open(file, 00);
    if (fd < 0) {
        fprintf(stderr, "Unable to read file: %s\n", file);
        return fd;
    }
    rb = 0;
    while (rb + 1024 < max) {
        tb = read(fd, &data[rb], 1024);
        if (tb < 0) {
            perror("read() error");
            break;
        }
        if (tb == 0) break;
        rb += tb;
    }
    fprintf(stdout, "read %u mb in memory.\n", rb/(1024*1024));
    *read_bytes = rb;
    return 0;
}

canonical_original_code:
int read_data_file(char *file, unsigned char *data, unsigned int max, unsigned int *read_bytes)
{
    int fd; int rb; int tb;
    fd = open(file, 00);
    if (fd < 0) {
        fprintf(stderr, "Unable to read file: %s\n", file);
        return fd;
    }
    rb = 0;
    while (rb + 1024 < max) {
        tb = read(fd, &data[rb], 1024);
        if (tb < 0) {
            perror("read() error");
            break;
        }
        if (tb == 0) break;
        rb += tb;
    }
    fprintf(stdout, "read %u mb in memory.\n", rb/(1024*1024));
    *read_bytes = rb;
    return 0;
}

variable_types:
file
char *

variable_types:
max
unsigned int

variable_types: fd int

variable_types: rb int

variable_types: tb int


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(stderr, "USAGE: %s <data-file> <rate-Mbps> <dst-ip> <dst-port>\n", a1);
}

canonical_decompiled_code:
int __fastcall func11(const char *a1)
{
return fprintf(stderr, "USAGE: %s <data-file> <rate-Mbps> <dst-ip> <dst-port>\n", a1);
}

original_code:
void usage(const char *p)
{
    fprintf(stderr, "USAGE: %s <data-file> <rate-Mbps> <dst-ip> <dst-port>\n", p);
}

canonical_original_code:
void usage(char *p)
{
    fprintf(stderr, "USAGE: %s <data-file> <rate-Mbps> <dst-ip> <dst-port>\n", p);
}

variable_types:
p
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // er12
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc != 5 )
{
usage(*argv);
exit(1);
}
v6[0] = 0;
v3 = (char *)malloc(0x40000000uLL);
v4 = atoi(argv[2]);
if ( !v3 )
{
fwrite("Unable to alloc.\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
if ( (int)read_data_file(argv[1], (__int64)v3, 0x40000000u, v6) < 0 )
exit(1);
udpplay_rate(argv[3], argv[4], v3, v6[0], 0x578u, v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // er12
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc != 5 )
{
func11(*argv);
exit(1);
}
v6[0] = 0;
v3 = (char *)malloc(0x40000000uLL);
v4 = atoi(argv[2]);
if ( !v3 )
{
fwrite("Unable to alloc.\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
if ( (int)func10(argv[1], (__int64)v3, 0x40000000u, v6) < 0 )
exit(1);
func9(argv[3], argv[4], v3, v6[0], 0x578u, v4);
return 0;
}

original_code:
int main(int argc, const char **argv)
{
    int max;
    uint8_t *data;
    uint32_t no_pkts;
    uint32_t rate, size;
    if (argc != 5) {
        usage(argv[0]);
        exit(1);
    }
    max = 1024*1024*1024;
    size = 0;
    data = (uint8_t *) malloc(max);
    rate = atoi(argv[2]);
    if (!data) {
        fprintf(stderr, "Unable to alloc.\n");
        exit(1);
    }
    if (read_data_file(argv[1], data, max, &size) < 0) {
        exit(1);
    }
    udpplay_rate(
            argv[3], argv[4],
            data, size, 1400, rate);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int max;
    unsigned char *data;
    unsigned int no_pkts;
    unsigned int rate; unsigned int size;
    if (argc != 5) {
        usage(argv[0]);
        exit(1);
    }
    max = 1024*1024*1024;
    size = 0;
    data = (unsigned char *) malloc(max);
    rate = atoi(argv[2]);
    if (!data) {
        fprintf(stderr, "Unable to alloc.\n");
        exit(1);
    }
    if (read_data_file(argv[1], data, max, &size) < 0) {
        exit(1);
    }
    udpplay_rate(
            argv[3], argv[4],
            data, size, 1400, rate);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: max int

variable_types:
data
unsigned char *


get

repo: shockkolate/dcpumulator

decompiled_code:
unsigned __int16 *__fastcall get(unsigned __int16 *a1, unsigned __int16 a2, char a3)
{
unsigned __int16 *result; // rax
int v4; // ebp
int v5; // ebp
int v6; // ebp
int v7; // ebp
int v8; // ebp
int v9; // ebp
int v10; // ebp
int v11; // ebp
unsigned __int16 v12; // ax
int v13; // ebp
unsigned __int16 v14; // ax

switch ( a2 )
{
case 0u:
result = a1;
break;
case 1u:
result = a1 + 1;
break;
case 2u:
result = a1 + 2;
break;
case 3u:
result = a1 + 3;
break;
case 4u:
result = a1 + 4;
break;
case 5u:
result = a1 + 5;
break;
case 6u:
result = a1 + 6;
break;
case 7u:
result = a1 + 7;
break;
case 8u:
result = &a1[*a1 + 12];
break;
case 9u:
result = &a1[a1[1] + 12];
break;
case 0xAu:
result = &a1[a1[2] + 12];
break;
case 0xBu:
result = &a1[a1[3] + 12];
break;
case 0xCu:
result = &a1[a1[4] + 12];
break;
case 0xDu:
result = &a1[a1[5] + 12];
break;
case 0xEu:
result = &a1[a1[6] + 12];
break;
case 0xFu:
result = &a1[a1[7] + 12];
break;
case 0x10u:
v4 = *a1;
result = &a1[v4 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x11u:
v5 = a1[1];
result = &a1[v5 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x12u:
v6 = a1[2];
result = &a1[v6 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x13u:
v7 = a1[3];
result = &a1[v7 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x14u:
v8 = a1[4];
result = &a1[v8 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x15u:
v9 = a1[5];
result = &a1[v9 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x16u:
v10 = a1[6];
result = &a1[v10 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x17u:
v11 = a1[7];
result = &a1[v11 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x18u:
if ( a3 )
{
v12 = a1[9] - 1;
a1[9] = v12;
}
else
{
v12 = a1[9];
a1[9] = v12 + 1;
}
result = &a1[v12 + 12];
break;
case 0x19u:
result = &a1[a1[9] + 12];
break;
case 0x1Au:
v13 = a1[9];
result = &a1[v13 + 12 + *(unsigned __int16 *)get(a1, 31LL, 0LL)];
break;
case 0x1Bu:
result = a1 + 9;
break;
case 0x1Cu:
result = a1 + 8;
break;
case 0x1Du:
result = a1 + 10;
break;
case 0x1Eu:
result = &a1[*(unsigned __int16 *)get(a1, 31LL, 0LL) + 12];
break;
case 0x1Fu:
v14 = a1[8];
a1[8] = v14 + 1;
result = &a1[v14 + 12];
break;
default:
result = (unsigned __int16 *)((char *)&literals + 2 * a2 - 64);
break;
}
return result;
}

canonical_decompiled_code:
unsigned __int16 *__fastcall func8(unsigned __int16 *a1, unsigned __int16 a2, char a3)
{
unsigned __int16 *result; // rax
int v4; // ebp
int v5; // ebp
int v6; // ebp
int v7; // ebp
int v8; // ebp
int v9; // ebp
int v10; // ebp
int v11; // ebp
unsigned __int16 v12; // ax
int v13; // ebp
unsigned __int16 v14; // ax

switch ( a2 )
{
case 0u:
result = a1;
break;
case 1u:
result = a1 + 1;
break;
case 2u:
result = a1 + 2;
break;
case 3u:
result = a1 + 3;
break;
case 4u:
result = a1 + 4;
break;
case 5u:
result = a1 + 5;
break;
case 6u:
result = a1 + 6;
break;
case 7u:
result = a1 + 7;
break;
case 8u:
result = &a1[*a1 + 12];
break;
case 9u:
result = &a1[a1[1] + 12];
break;
case 0xAu:
result = &a1[a1[2] + 12];
break;
case 0xBu:
result = &a1[a1[3] + 12];
break;
case 0xCu:
result = &a1[a1[4] + 12];
break;
case 0xDu:
result = &a1[a1[5] + 12];
break;
case 0xEu:
result = &a1[a1[6] + 12];
break;
case 0xFu:
result = &a1[a1[7] + 12];
break;
case 0x10u:
v4 = *a1;
result = &a1[v4 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x11u:
v5 = a1[1];
result = &a1[v5 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x12u:
v6 = a1[2];
result = &a1[v6 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x13u:
v7 = a1[3];
result = &a1[v7 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x14u:
v8 = a1[4];
result = &a1[v8 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x15u:
v9 = a1[5];
result = &a1[v9 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x16u:
v10 = a1[6];
result = &a1[v10 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x17u:
v11 = a1[7];
result = &a1[v11 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x18u:
if ( a3 )
{
v12 = a1[9] - 1;
a1[9] = v12;
}
else
{
v12 = a1[9];
a1[9] = v12 + 1;
}
result = &a1[v12 + 12];
break;
case 0x19u:
result = &a1[a1[9] + 12];
break;
case 0x1Au:
v13 = a1[9];
result = &a1[v13 + 12 + *(unsigned __int16 *)func8(a1, 31LL, 0LL)];
break;
case 0x1Bu:
result = a1 + 9;
break;
case 0x1Cu:
result = a1 + 8;
break;
case 0x1Du:
result = a1 + 10;
break;
case 0x1Eu:
result = &a1[*(unsigned __int16 *)func8(a1, 31LL, 0LL) + 12];
break;
case 0x1Fu:
v14 = a1[8];
a1[8] = v14 + 1;
result = &a1[v14 + 12];
break;
default:
result = (unsigned __int16 *)((char *)&literals + 2 * a2 - 64);
break;
}
return result;
}

original_code:
uint16_t * get(struct vm_state_s *state, uint16_t A, unsigned char is_B)
{
    switch(A)
    {
        case 0x00:
            return &state->A;
        case 0x01:
            return &state->B;
        case 0x02:
            return &state->C;
        case 0x03:
            return &state->X;
        case 0x04:
            return &state->Y;
        case 0x05:
            return &state->Z;
        case 0x06:
            return &state->I;
        case 0x07:
            return &state->J;
        case 0x08:
            return &state->mem[state->A];
        case 0x09:
            return &state->mem[state->B];
        case 0x0A:
            return &state->mem[state->C];
        case 0x0B:
            return &state->mem[state->X];
        case 0x0C:
            return &state->mem[state->Y];
        case 0x0D:
            return &state->mem[state->Z];
        case 0x0E:
            return &state->mem[state->I];
        case 0x0F:
            return &state->mem[state->J];
        case 0x10:
            return &state->mem[state->A + *get(state, 0x1F, 0)];
        case 0x11:
            return &state->mem[state->B + *get(state, 0x1F, 0)];
        case 0x12:
            return &state->mem[state->C + *get(state, 0x1F, 0)];
        case 0x13:
            return &state->mem[state->X + *get(state, 0x1F, 0)];
        case 0x14:
            return &state->mem[state->Y + *get(state, 0x1F, 0)];
        case 0x15:
            return &state->mem[state->Z + *get(state, 0x1F, 0)];
        case 0x16:
            return &state->mem[state->I + *get(state, 0x1F, 0)];
        case 0x17:
            return &state->mem[state->J + *get(state, 0x1F, 0)];
        case 0x18:
            return is_B ? &state->mem[--state->SP]
                        : &state->mem[state->SP++];
        case 0x19:
            return &state->mem[state->SP];
        case 0x1A:
            return &state->mem[state->SP + *get(state, 0x1F, 0)];
        case 0x1B:
            return &state->SP;
        case 0x1C:
            return &state->PC;
        case 0x1D:
            return &state->EX;
        case 0x1E:
            return &state->mem[*get(state, 0x1F, 0)];
        case 0x1F:
            return &state->mem[state->PC++];
        default:
            return &literals[A - 0x20];
    }
}

canonical_original_code:
unsigned short *get(struct vm_state_s *state, unsigned short A, unsigned char is_B)
{
    switch(A)
    {
        case 0x00:
            return &state->A;
        case 0x01:
            return &state->B;
        case 0x02:
            return &state->C;
        case 0x03:
            return &state->X;
        case 0x04:
            return &state->Y;
        case 0x05:
            return &state->Z;
        case 0x06:
            return &state->I;
        case 0x07:
            return &state->J;
        case 0x08:
            return &state->mem[state->A];
        case 0x09:
            return &state->mem[state->B];
        case 0x0A:
            return &state->mem[state->C];
        case 0x0B:
            return &state->mem[state->X];
        case 0x0C:
            return &state->mem[state->Y];
        case 0x0D:
            return &state->mem[state->Z];
        case 0x0E:
            return &state->mem[state->I];
        case 0x0F:
            return &state->mem[state->J];
        case 0x10:
            return &state->mem[state->A + *get(state, 0x1F, 0)];
        case 0x11:
            return &state->mem[state->B + *get(state, 0x1F, 0)];
        case 0x12:
            return &state->mem[state->C + *get(state, 0x1F, 0)];
        case 0x13:
            return &state->mem[state->X + *get(state, 0x1F, 0)];
        case 0x14:
            return &state->mem[state->Y + *get(state, 0x1F, 0)];
        case 0x15:
            return &state->mem[state->Z + *get(state, 0x1F, 0)];
        case 0x16:
            return &state->mem[state->I + *get(state, 0x1F, 0)];
        case 0x17:
            return &state->mem[state->J + *get(state, 0x1F, 0)];
        case 0x18:
            return is_B ? &state->mem[--state->SP]
                        : &state->mem[state->SP++];
        case 0x19:
            return &state->mem[state->SP];
        case 0x1A:
            return &state->mem[state->SP + *get(state, 0x1F, 0)];
        case 0x1B:
            return &state->SP;
        case 0x1C:
            return &state->PC;
        case 0x1D:
            return &state->EX;
        case 0x1E:
            return &state->mem[*get(state, 0x1F, 0)];
        case 0x1F:
            return &state->mem[state->PC++];
        default:
            return &literals[A - 0x20];
    }
}

variable_types: struct vm_state_s { unsigned short A; unsigned short B; unsigned short C; unsigned short X; unsigned short Y; unsigned short Z; unsigned short I; unsigned short J; unsigned short PC; unsigned short SP; unsigned short EX; unsigned short IA; unsigned short mem[65536]; unsigned char skip; } *

variable_types:
A
unsigned short

return_type: unsigned short *


original_code:
void process_special(struct vm_state_s *state, uint16_t B, uint16_t *VALA)
{
    if(state->skip) return;
    switch(B)
    {
        case 0x01:
            process(state, 0x01, get(state, 0x18, 1), get(state, 0x1B, 1));
            state->PC = *VALA;
            break;
    }
}

canonical_original_code:
void process_special(struct vm_state_s *state, unsigned short B, unsigned short *VALA)
{
    if(state->skip) return;
    switch(B)
    {
        case 0x01:
            process(state, 0x01, get(state, 0x18, 1), get(state, 0x1B, 1));
            state->PC = *VALA;
            break;
    }
}

variable_types:
B
unsigned short

variable_types:
VALA
unsigned short *

user_defined_types: struct vm_state_s { unsigned short A; unsigned short B; unsigned short C; unsigned short X; unsigned short Y; unsigned short Z; unsigned short I; unsigned short J; unsigned short PC; unsigned short SP; unsigned short EX; unsigned short IA; unsigned short mem[65536]; unsigned char skip; }


main

original_code:
int main(int argc, char **argv)
{
    struct vm_state_s state;
    state.PC = 0;
    state.SP = 0;
    state.IA = 0;
    state.skip = 0;
    freopen(((void *)0), "rb", stdin);
    fread(state.mem, 1, 0x10000, stdin);
    for(uint32_t i=0; i<0x10000; ++i)
    {
        state.mem[i] = __bswap_16 (state.mem[i]);
    }
    for(;;)
    {
        fputc('\n', stderr);
        fprintf(stderr, "PC=0x%.*x\n", 4, state.PC);
        fprintf(stderr, "[0x2000]=0x%.*x\n", 4, state.mem[0x2000]);
        uint16_t word = *get(&state, 0x1F, 0);
        uint16_t OP = word & 0x1F;
        uint16_t B = (word >> 5) & 0x1F;
        uint16_t A = (word >> 10);
        fprintf(stderr, "OP=0x%.*x\n", 2, OP);
        fprintf(stderr, "B=0x%.*x\n", 2, B);
        fprintf(stderr, "A=0x%.*x\n", 2, A);
        uint16_t *VALA = get(&state, A, 0);
        if(OP == 0)
        {
            process_special(&state, B, VALA);
            continue;
        }
        uint16_t *VALB = get(&state, B, 1);
        process(&state, OP, VALB, VALA);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct vm_state_s state;
    state.PC = 0;
    state.SP = 0;
    state.IA = 0;
    state.skip = 0;
    freopen(((void *)0), "rb", stdin);
    fread(state.mem, 1, 0x10000, stdin);
    for(unsigned int i = 0; i<0x10000; ++i)
    {
        state.mem[i] = __bswap_16 (state.mem[i]);
    }
    for(;;)
    {
        fputc('\n', stderr);
        fprintf(stderr, "PC=0x%.*x\n", 4, state.PC);
        fprintf(stderr, "[0x2000]=0x%.*x\n", 4, state.mem[0x2000]);
        unsigned short word = *get(&state, 0x1F, 0);
        unsigned short OP = word & 0x1F;
        unsigned short B = (word >> 5) & 0x1F;
        unsigned short A = (word >> 10);
        fprintf(stderr, "OP=0x%.*x\n", 2, OP);
        fprintf(stderr, "B=0x%.*x\n", 2, B);
        fprintf(stderr, "A=0x%.*x\n", 2, A);
        unsigned short *VALA = get(&state, A, 0);
        if(OP == 0)
        {
            process_special(&state, B, VALA);
            continue;
        }
        unsigned short *VALB = get(&state, B, 1);
        process(&state, OP, VALB, VALA);
    }
    return 0;
}

variable_types: struct vm_state_s { unsigned short A; unsigned short B; unsigned short C; unsigned short X; unsigned short Y; unsigned short Z; unsigned short I; unsigned short J; unsigned short PC; unsigned short SP; unsigned short EX; unsigned short IA; unsigned short mem[65536]; unsigned char skip; }

variable_types:
i
unsigned int

variable_types:
word
unsigned short

variable_types:
OP
unsigned short

variable_types:
VALB
unsigned short *


repo: shockkolate/dcpussembler

original_code:
uint16_t get_op(char *OP_str, uint16_t *special)
{
    int32_t OP = __bswap_32 (*(int32_t *)OP_str);
    switch(OP)
    {
        case 'SET\0':
            return 0x01;
        case 'ADD\0':
            return 0x02;
        case 'SUB\0':
            return 0x03;
        case 'MUL\0':
            return 0x04;
        case 'MLI\0':
            return 0x05;
        case 'DIV\0':
            return 0x06;
        case 'DVI\0':
            return 0x07;
        case 'MOD\0':
            return 0x08;
        case 'MDI\0':
            return 0x09;
        case 'AND\0':
            return 0x0A;
        case 'BOR\0':
            return 0x0B;
        case 'XOR\0':
            return 0x0C;
        case 'SHR\0':
            return 0x0D;
        case 'ASR\0':
            return 0x0E;
        case 'SHL\0':
            return 0x0F;
        case 'IFB\0':
            return 0x10;
        case 'IFC\0':
            return 0x11;
        case 'IFE\0':
            return 0x12;
        case 'IFN\0':
            return 0x13;
        case 'IFG\0':
            return 0x14;
        case 'IFA\0':
            return 0x15;
        case 'IFL\0':
            return 0x16;
        case 'IFU\0':
            return 0x17;
        case 'ADX\0':
            return 0x1A;
        case 'SBX\0':
            return 0x1B;
        case 'STI\0':
            return 0x1E;
        case 'STD\0':
            return 0x1F;
        case 'JSR\0':
            *special = 0x01;
            return 0;
        case 'INT\0':
            *special = 0x08;
            return 0;
        case 'IAG\0':
            *special = 0x09;
            return 0;
        case 'IAS\0':
            *special = 0x0A;
            return 0;
        case 'RFI\0':
            *special = 0x0B;
            return 0;
        case 'IAQ\0':
            *special = 0x0C;
            return 0;
        case 'HWN\0':
            *special = 0x10;
            return 0;
        case 'HWQ\0':
            *special = 0x11;
            return 0;
        case 'HWI\0':
            *special = 0x12;
            return 0;
        default:
            return 0xFF;
    }
}

canonical_original_code:
unsigned short get_op(char *OP_str, unsigned short *special)
{
    int OP = __bswap_32 (*(int32_t *)OP_str);
    switch(OP)
    {
        case 'SET\0':
            return 0x01;
        case 'ADD\0':
            return 0x02;
        case 'SUB\0':
            return 0x03;
        case 'MUL\0':
            return 0x04;
        case 'MLI\0':
            return 0x05;
        case 'DIV\0':
            return 0x06;
        case 'DVI\0':
            return 0x07;
        case 'MOD\0':
            return 0x08;
        case 'MDI\0':
            return 0x09;
        case 'AND\0':
            return 0x0A;
        case 'BOR\0':
            return 0x0B;
        case 'XOR\0':
            return 0x0C;
        case 'SHR\0':
            return 0x0D;
        case 'ASR\0':
            return 0x0E;
        case 'SHL\0':
            return 0x0F;
        case 'IFB\0':
            return 0x10;
        case 'IFC\0':
            return 0x11;
        case 'IFE\0':
            return 0x12;
        case 'IFN\0':
            return 0x13;
        case 'IFG\0':
            return 0x14;
        case 'IFA\0':
            return 0x15;
        case 'IFL\0':
            return 0x16;
        case 'IFU\0':
            return 0x17;
        case 'ADX\0':
            return 0x1A;
        case 'SBX\0':
            return 0x1B;
        case 'STI\0':
            return 0x1E;
        case 'STD\0':
            return 0x1F;
        case 'JSR\0':
            *special = 0x01;
            return 0;
        case 'INT\0':
            *special = 0x08;
            return 0;
        case 'IAG\0':
            *special = 0x09;
            return 0;
        case 'IAS\0':
            *special = 0x0A;
            return 0;
        case 'RFI\0':
            *special = 0x0B;
            return 0;
        case 'IAQ\0':
            *special = 0x0C;
            return 0;
        case 'HWN\0':
            *special = 0x10;
            return 0;
        case 'HWQ\0':
            *special = 0x11;
            return 0;
        case 'HWI\0':
            *special = 0x12;
            return 0;
        default:
            return 0xFF;
    }
}

variable_types:
special
unsigned short *

variable_types: OP int

return_type: unsigned short


original_code:
uint16_t get_value(char *s, int *pos, int *num_extra, uint16_t *extra)
{
    char unused[2];
    int newpos = 0;
    int is_left_bracket = sscanf(&s[*pos], " %1[[]%n", unused, &newpos) == 1;
    *pos += newpos;
    int first_i;
    char first_s[5] = {0,0,0,0,0};
    newpos = 0;
    int first_is_integer = sscanf(&s[*pos], " %i%n", &first_i, &newpos) == 1;
    *pos += newpos;
    if(!first_is_integer)
    {
        newpos = 0;
        if(sscanf(&s[*pos], " %4[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%n", first_s, &newpos) != 1)
        {
            return 0xFF;
        }
        *pos += newpos;
        for(unsigned char i=0; i<strlen(first_s); ++i)
        {
            first_s[i] = toupper(first_s[i]);
        }
    }
    newpos = 0;
    int is_plus = sscanf(&s[*pos], " %1[+]%n", unused, &newpos) == 1;
    *pos += newpos;
    int second_i;
    char second_s[5] = {0,0,0,0,0};
    int second_is_integer;
    if(is_plus)
    {
        newpos = 0;
        second_is_integer = sscanf(&s[*pos], " %i%n", &second_i, &newpos) == 1;
        *pos += newpos;
        if(!second_is_integer)
        {
            newpos = 0;
            if(sscanf(&s[*pos], " %4[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%n", second_s, &newpos) != 1)
            {
                return 0xFF;
            }
            *pos += newpos;
            for(unsigned char i=0; i<strlen(second_s); ++i)
            {
                second_s[i] = toupper(second_s[i]);
            }
        }
    }
    newpos = 0;
    int is_right_bracket = sscanf(&s[*pos], " %1[]]%n", unused, &newpos) == 1;
    *pos += newpos;
    if(sscanf(&s[*pos], " %1s", unused) == 1)
    {
        return 0xFF;
    }
    if(is_left_bracket != is_right_bracket)
    {
        return 0xFF;
    }
    if(!is_left_bracket && !is_plus)
    {
        if(first_is_integer)
        {
            if(first_i >= -1 && first_i <= 30)
            {
                return (0x21 + first_i);
            }
            else
            {
                extra[(*num_extra)++] = first_i;
                return 0x1F;
            }
        }
        int32_t first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                return 0x00;
            case 'B\0\0\0':
                return 0x01;
            case 'C\0\0\0':
                return 0x02;
            case 'X\0\0\0':
                return 0x03;
            case 'Y\0\0\0':
                return 0x04;
            case 'Z\0\0\0':
                return 0x05;
            case 'I\0\0\0':
                return 0x06;
            case 'J\0\0\0':
                return 0x02;
            case 'PUSH':
            case 'POP\0':
                return 0x18;
            case 'PEEK':
                return 0x19;
            case 'SP\0\0':
                return 0x1B;
            case 'PC\0\0':
                return 0x1C;
            case 'EX\0\0':
                return 0x1D;
        }
    }
    else if(is_left_bracket && !is_plus)
    {
        if(first_is_integer)
        {
            extra[(*num_extra)++] = first_i;
            return 0x1E;
        }
        int32_t first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                return 0x08;
            case 'B\0\0\0':
                return 0x09;
            case 'C\0\0\0':
                return 0x0A;
            case 'X\0\0\0':
                return 0x0B;
            case 'Y\0\0\0':
                return 0x0C;
            case 'Z\0\0\0':
                return 0x0D;
            case 'I\0\0\0':
                return 0x0E;
            case 'J\0\0\0':
                return 0x0F;
            case 'SP\0\0':
                return 0x19;
        }
    }
    else if(is_left_bracket && is_plus && !first_is_integer && second_is_integer)
    {
        int32_t first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x10;
            case 'B\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x11;
            case 'C\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x12;
            case 'X\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x13;
            case 'Y\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x14;
            case 'Z\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x15;
            case 'I\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x16;
            case 'J\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x17;
            case 'SP\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x1A;
        }
    }
    return 0xFF;
}

canonical_original_code:
unsigned short get_value(char *s, int *pos, int *num_extra, unsigned short *extra)
{
    char unused[2];
    int newpos = 0;
    int is_left_bracket = sscanf(&s[*pos], " %1[[]%n", unused, &newpos) == 1;
    *pos += newpos;
    int first_i;
    char first_s[5] = {0,0,0,0,0};
    newpos = 0;
    int first_is_integer = sscanf(&s[*pos], " %i%n", &first_i, &newpos) == 1;
    *pos += newpos;
    if(!first_is_integer)
    {
        newpos = 0;
        if(sscanf(&s[*pos], " %4[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%n", first_s, &newpos) != 1)
        {
            return 0xFF;
        }
        *pos += newpos;
        for(unsigned char i = 0; i<strlen(first_s); ++i)
        {
            first_s[i] = toupper(first_s[i]);
        }
    }
    newpos = 0;
    int is_plus = sscanf(&s[*pos], " %1[+]%n", unused, &newpos) == 1;
    *pos += newpos;
    int second_i;
    char second_s[5] = {0,0,0,0,0};
    int second_is_integer;
    if(is_plus)
    {
        newpos = 0;
        second_is_integer = sscanf(&s[*pos], " %i%n", &second_i, &newpos) == 1;
        *pos += newpos;
        if(!second_is_integer)
        {
            newpos = 0;
            if(sscanf(&s[*pos], " %4[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%n", second_s, &newpos) != 1)
            {
                return 0xFF;
            }
            *pos += newpos;
            for(unsigned char i = 0; i<strlen(second_s); ++i)
            {
                second_s[i] = toupper(second_s[i]);
            }
        }
    }
    newpos = 0;
    int is_right_bracket = sscanf(&s[*pos], " %1[]]%n", unused, &newpos) == 1;
    *pos += newpos;
    if(sscanf(&s[*pos], " %1s", unused) == 1)
    {
        return 0xFF;
    }
    if(is_left_bracket != is_right_bracket)
    {
        return 0xFF;
    }
    if(!is_left_bracket && !is_plus)
    {
        if(first_is_integer)
        {
            if(first_i >= -1 && first_i <= 30)
            {
                return (0x21 + first_i);
            }
            else
            {
                extra[(*num_extra)++] = first_i;
                return 0x1F;
            }
        }
        int first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                return 0x00;
            case 'B\0\0\0':
                return 0x01;
            case 'C\0\0\0':
                return 0x02;
            case 'X\0\0\0':
                return 0x03;
            case 'Y\0\0\0':
                return 0x04;
            case 'Z\0\0\0':
                return 0x05;
            case 'I\0\0\0':
                return 0x06;
            case 'J\0\0\0':
                return 0x02;
            case 'PUSH':
            case 'POP\0':
                return 0x18;
            case 'PEEK':
                return 0x19;
            case 'SP\0\0':
                return 0x1B;
            case 'PC\0\0':
                return 0x1C;
            case 'EX\0\0':
                return 0x1D;
        }
    }
    else if(is_left_bracket && !is_plus)
    {
        if(first_is_integer)
        {
            extra[(*num_extra)++] = first_i;
            return 0x1E;
        }
        int first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                return 0x08;
            case 'B\0\0\0':
                return 0x09;
            case 'C\0\0\0':
                return 0x0A;
            case 'X\0\0\0':
                return 0x0B;
            case 'Y\0\0\0':
                return 0x0C;
            case 'Z\0\0\0':
                return 0x0D;
            case 'I\0\0\0':
                return 0x0E;
            case 'J\0\0\0':
                return 0x0F;
            case 'SP\0\0':
                return 0x19;
        }
    }
    else if(is_left_bracket && is_plus && !first_is_integer && second_is_integer)
    {
        int first = __bswap_32 (*(int32_t *)first_s);
        switch(first)
        {
            case 'A\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x10;
            case 'B\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x11;
            case 'C\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x12;
            case 'X\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x13;
            case 'Y\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x14;
            case 'Z\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x15;
            case 'I\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x16;
            case 'J\0\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x17;
            case 'SP\0\0':
                extra[(*num_extra)++] = second_i;
                return 0x1A;
        }
    }
    return 0xFF;
}

variable_types:
s
char *

variable_types:
pos
int *

variable_types:
extra
unsigned short *

variable_types:
unused
char [2]

variable_types: newpos int

variable_types:
i
unsigned char

variable_types: first int


repo: shoenig/YALL

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

for ( result = 0LL; result != 97; ++result )
uft_table[result] = 0LL;
return result;
}

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

for ( result = 0LL; result != 97; ++result )
uft_table[result] = 0LL;
return result;
}

original_code:
void
uft_init() {
  size_t idx = 0;
  while(idx < 97) {
    uft_table[idx] = ((void *)0);
    idx++;
  }
}

canonical_original_code:
void uft_init() {
  unsigned long idx = 0;
  while(idx < 97) {
    uft_table[idx] = ((void *)0);
    idx++;
  }
}

variable_types:
idx
unsigned long


decompiled_code:
__int64 __fastcall uft_lookup(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = 0LL;
while ( 1 )
{
v2 = uft_table[v1];
if ( v2 )
{
if ( !strcmp(s1, *(const char **)(v2 + 16)) )
break;
}
if ( ++v1 == 97 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = 0LL;
while ( 1 )
{
v2 = uft_table[v1];
if ( v2 )
{
if ( !strcmp(s1, *(const char **)(v2 + 16)) )
break;
}
if ( ++v1 == 97 )
return 0LL;
}
return v2;
}

original_code:
UserFunc*
uft_lookup(char* fname) {
  size_t idx = 0;
  while(idx < 97) {
    if(uft_table[idx] && strcmp(fname, uft_table[idx]->func_name) == 0)
      return uft_table[idx];
    idx++;
  }
  return ((void *)0);
}

canonical_original_code:
struct UserFunc *uft_lookup(char *fname) {
  unsigned long idx = 0;
  while(idx < 97) {
    if(uft_table[idx] && strcmp(fname, uft_table[idx]->func_name) == 0)
      return uft_table[idx];
    idx++;
  }
  return ((void *)0);
}

variable_types:
fname
char *

return_type: struct UserFunc { int argc; char **args; char *func_name; struct ast_ { enum ASTTYPE { AST_INVALID=10, AST_INT=11, AST_FLOAT=12, AST_MINUS=13, AST_BOOL=14, AST_LIST=15, AST_LIST_ELEMENT=16, AST_REFERENCE=17, AST_LIST_FUNC=18, AST_BUILTIN_FUNC=19, AST_BOOLEAN_FUNC=20, AST_CALL_USERFUNC=21, AST_AMPER=22, AST_PIPE=23, AST_EQ=24, AST_PLUS=25, AST_SUB=26, AST_MULT=27, AST_DIV=28 } nodetype; struct evaltype { enum EVALTYPE { ET_INVALID=0, ET_INT=1, ET_FLOAT=2, ET_BOOL=3, ET_LIST=4, ET_REFERENCE=5 } type; union { long long i; double f; short bool; struct List { struct ast_ *head; } *list; char *str; enum bif { B_sqrt=40, B_abs=41, B_log=42, B_log2=43, B_log10=44, B_float=45, B_int=46, B_pow=47, B_fact=48, B_negate=49, B_floor=50, B_ceil=51, B_defint=52, B_deffloat=53, B_defbool=54, B_deflist=55, B_if=56, B_with=57, B_ufunc=58, B_defufunc=59 } b; enum cmpf { T_greater_than=70, T_less_than=71, T_not_equal=72, T_equal=73, T_greater_than_equal=74, T_less_than_equal=75, T_and=76, T_or=77 } t; enum listf { L_empty=80, L_len=81, L_peek=82, L_copy=83, L_pop=84, L_push=85 } lfunc; } val; } e; struct ast_ *left; struct ast_ *right; struct ast_ *aux; } *func_body; } *

user_defined_types: struct UserFunc { int argc; char **args; char *func_name; struct ast_ *func_body; }

user_defined_types: struct ast_ { enum ASTTYPE nodetype; struct evaltype e; struct ast_ *left; struct ast_ *right; struct ast_ *aux; }

user_defined_types: struct evaltype { enum EVALTYPE type; union { long long i; double f; short bool; struct List *list; char *str; enum bif b; enum cmpf t; enum listf lfunc; } val; }


decompiled_code:
__int64 __fastcall uft_put(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx
__int64 result; // rax

v1 = uft_lookup(*(char **)(a1 + 16));
if ( v1 )
{
v2 = v1;
delete_user_func(v1);
*(_DWORD *)v2 = *(_DWORD *)a1;
*(_QWORD *)(v2 + 8) = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v2 + 16) = *(_QWORD *)(a1 + 16);
result = *(_QWORD *)(a1 + 24);
*(_QWORD *)(v2 + 24) = result;
}
else
{
result = 0LL;
while ( uft_table[result] )
{
if ( ++result == 97 )
return crash("functable is out of space");
}
uft_table[result] = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx
__int64 result; // rax

v1 = func1(*(char **)(a1 + 16));
if ( v1 )
{
v2 = v1;
delete_user_func(v1);
*(_DWORD *)v2 = *(_DWORD *)a1;
*(_QWORD *)(v2 + 8) = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v2 + 16) = *(_QWORD *)(a1 + 16);
result = *(_QWORD *)(a1 + 24);
*(_QWORD *)(v2 + 24) = result;
}
else
{
result = 0LL;
while ( uft_table[result] )
{
if ( ++result == 97 )
return crash("functable is out of space");
}
uft_table[result] = a1;
}
return result;
}

original_code:
void
uft_put(UserFunc* uf) {
  UserFunc* temp = uft_lookup(uf->func_name);
  if(!temp) {
    size_t idx = 0;
    while(idx < 97) {
      if(!uft_table[idx]) {
        uft_table[idx] = uf;
        return;
      }
      idx++;
    }
    crash("functable is out of space");
  } else {
    delete_user_func(temp);
    temp->argc = uf->argc;
    temp->args = uf->args;
    temp->func_name = uf->func_name;
    temp->func_body = uf->func_body;
  }
}

canonical_original_code:
void uft_put(struct UserFunc *uf) {
  struct UserFunc *temp = uft_lookup(uf->func_name);
  if(!temp) {
    unsigned long idx = 0;
    while(idx < 97) {
      if(!uft_table[idx]) {
        uft_table[idx] = uf;
        return;
      }
      idx++;
    }
    crash("functable is out of space");
  } else {
    delete_user_func(temp);
    temp->argc = uf->argc;
    temp->args = uf->args;
    temp->func_name = uf->func_name;
    temp->func_body = uf->func_body;
  }
}

variable_types: struct UserFunc { int argc; char **args; char *func_name; struct ast_ { enum ASTTYPE { AST_INVALID=10, AST_INT=11, AST_FLOAT=12, AST_MINUS=13, AST_BOOL=14, AST_LIST=15, AST_LIST_ELEMENT=16, AST_REFERENCE=17, AST_LIST_FUNC=18, AST_BUILTIN_FUNC=19, AST_BOOLEAN_FUNC=20, AST_CALL_USERFUNC=21, AST_AMPER=22, AST_PIPE=23, AST_EQ=24, AST_PLUS=25, AST_SUB=26, AST_MULT=27, AST_DIV=28 } nodetype; struct evaltype { enum EVALTYPE { ET_INVALID=0, ET_INT=1, ET_FLOAT=2, ET_BOOL=3, ET_LIST=4, ET_REFERENCE=5 } type; union { long long i; double f; short bool; struct List { struct ast_ *head; } *list; char *str; enum bif { B_sqrt=40, B_abs=41, B_log=42, B_log2=43, B_log10=44, B_float=45, B_int=46, B_pow=47, B_fact=48, B_negate=49, B_floor=50, B_ceil=51, B_defint=52, B_deffloat=53, B_defbool=54, B_deflist=55, B_if=56, B_with=57, B_ufunc=58, B_defufunc=59 } b; enum cmpf { T_greater_than=70, T_less_than=71, T_not_equal=72, T_equal=73, T_greater_than_equal=74, T_less_than_equal=75, T_and=76, T_or=77 } t; enum listf { L_empty=80, L_len=81, L_peek=82, L_copy=83, L_pop=84, L_push=85 } lfunc; } val; } e; struct ast_ *left; struct ast_ *right; struct ast_ *aux; } *func_body; } *


decompiled_code:
char *__fastcall str_int(__int64 a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x20uLL);
sprintf(v1, "%lld", a1);
return v1;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x20uLL);
sprintf(v1, "%lld", a1);
return v1;
}

original_code:
char*
str_int(int64 i) {
  char* nstr = malloc(sizeof(char) * 32);
  sprintf(nstr, "%lld", i);
  return nstr;
}

canonical_original_code:
char *str_int(long long i) {
  char *nstr = malloc(sizeof(char) * 32);
  sprintf(nstr, "%lld", i);
  return nstr;
}

variable_types:
i
long long

variable_types:
nstr
char *

return_type: char *


decompiled_code:
char *__fastcall str_float(double a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x20uLL);
sprintf(v1, "%4.2f", a1);
return v1;
}

canonical_decompiled_code:
char *__fastcall func1(double a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x20uLL);
sprintf(v1, "%4.2f", a1);
return v1;
}

original_code:
char*
str_float(float64 f) {
  char* nstr = malloc(sizeof(char) * 32);
  sprintf(nstr, "%4.2f", f);
  return nstr;
}

canonical_original_code:
char *str_float(double f) {
  char *nstr = malloc(sizeof(char) * 32);
  sprintf(nstr, "%4.2f", f);
  return nstr;
}

variable_types: f double


decompiled_code:
char *__fastcall str_bool(__int16 a1)
{
char *result; // rax

result = (char *)malloc(6uLL);
if ( a1 )
strcpy(result, "true");
else
strcpy(result, "false");
return result;
}

canonical_decompiled_code:
char *__fastcall func2(__int16 a1)
{
char *result; // rax

result = (char *)malloc(6uLL);
if ( a1 )
strcpy(result, "true");
else
strcpy(result, "false");
return result;
}

original_code:
char*
str_bool(bool b) {
  char* nstr = malloc(sizeof(char) * 6);
  if(!b)
    sprintf(nstr, "false");
  else
    sprintf(nstr, "true");
  return nstr;
}

canonical_original_code:
char *str_bool(short b) {
  char *nstr = malloc(sizeof(char) * 6);
  if(!b)
    sprintf(nstr, "false");
  else
    sprintf(nstr, "true");
  return nstr;
}

variable_types: b short


decompiled_code:
__int64 str_ref()
{
return str_dup();
}

canonical_decompiled_code:
__int64 func3()
{
return str_dup();
}

original_code:
char*
str_ref(char* str) {
  return str_dup(str);
}

canonical_original_code:
char *str_ref(char *str) {
  return str_dup(str);
}

variable_types:
str
char *