main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rdi
int i; // ebp
__pid_t v6; // eax
int v7; // ebx
__WAIT_STATUS stat_loc[30]; // [rsp+0h] [rbp-10Ch] BYREF
char v10; // [rsp+F4h] [rbp-18h] BYREF

v3 = (char *)&stat_loc[0].__iptr + 4;
*(double *)&xstep = (*(double *)&xmax - *(double *)&xmin) / (double)x_chars;
*(double *)&ystep = (*(double *)&ymax - *(double *)&ymin) / (double)y_chars;
install_signal_handler();
do
{
v4 = v3;
v3 += 8;
pipesem_init(v4, 0LL);
}
while ( v3 != &v10 );
for ( i = 0; ; ++i )
{
v6 = fork();
if ( v6 < 0 )
{
perror("parent: fork");
exit(1);
}
if ( !v6 )
{
child(i, (__int64)&stat_loc[i].__iptr + 4, (__int64)&stat_loc[(i + 1) % 30].__iptr + 4);
exit(1);
}
if ( i == 29 )
break;
}
v7 = 30;
pipesem_signal((char *)&stat_loc[0].__iptr + 4);
do
{
wait((__WAIT_STATUS)stat_loc);
--v7;
}
while ( v7 );
reset_xterm_color(1LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rdi
int i; // ebp
__pid_t v6; // eax
int v7; // ebx
__WAIT_STATUS stat_loc[30]; // [rsp+0h] [rbp-10Ch] BYREF
char v10; // [rsp+F4h] [rbp-18h] BYREF

v3 = (char *)&stat_loc[0].__iptr + 4;
*(double *)&xstep = (*(double *)&xmax - *(double *)&xmin) / (double)x_chars;
*(double *)&ystep = (*(double *)&ymax - *(double *)&ymin) / (double)y_chars;
func1();
do
{
v4 = v3;
v3 += 8;
pipesem_init(v4, 0LL);
}
while ( v3 != &v10 );
for ( i = 0; ; ++i )
{
v6 = fork();
if ( v6 < 0 )
{
perror("parent: fork");
exit(1);
}
if ( !v6 )
{
func5(i, (__int64)&stat_loc[i].__iptr + 4, (__int64)&stat_loc[(i + 1) % 30].__iptr + 4);
exit(1);
}
if ( i == 29 )
break;
}
v7 = 30;
pipesem_signal((char *)&stat_loc[0].__iptr + 4);
do
{
wait((__WAIT_STATUS)stat_loc);
--v7;
}
while ( v7 );
reset_xterm_color(1LL);
return 0;
}

original_code:
int main(void)
{
    int i;
    pid_t p;
    int status;
    struct pipesem sem[30];
    xstep = (xmax - xmin) / x_chars;
    ystep = (ymax - ymin) / y_chars;
    install_signal_handler();
    for(i=0;i<30;i++)
    {
        pipesem_init(&sem[i],0);
    }
    for(i=0;i<30;i++)
    {
        p = fork();
        if (p < 0) {
            perror("parent: fork");
            exit(1);
        }
        if (p == 0) {
            child(i,&sem[i],&sem[(i+1)%30]);
            exit(1);
        }
    }
    pipesem_signal(sem);
    for(i=0;i<30;i++)
    {
        wait(&status);
    }
    reset_xterm_color(1);
    return 0;
}

canonical_original_code:
int main()
{
    int i;
    int p;
    int status;
    struct pipesem sem[30];
    xstep = (xmax - xmin) / x_chars;
    ystep = (ymax - ymin) / y_chars;
    install_signal_handler();
    for(i=0;i<30;i++)
    {
        pipesem_init(&sem[i],0);
    }
    for(i=0;i<30;i++)
    {
        p = fork();
        if (p < 0) {
            perror("parent: fork");
            exit(1);
        }
        if (p == 0) {
            child(i,&sem[i],&sem[(i+1)%30]);
            exit(1);
        }
    }
    pipesem_signal(sem);
    for(i=0;i<30;i++)
    {
        wait(&status);
    }
    reset_xterm_color(1);
    return 0;
}

variable_types: p int

variable_types: status int

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


repo: shmmy/OS

decompiled_code:
__int64 __fastcall xterm2rgb(unsigned __int8 a1, _BYTE *a2)
{
__int64 v2; // rax
char v3; // dl
__int64 result; // rax
unsigned __int8 v5; // cl
char v6; // di

if ( a1 > 0xFu )
{
v5 = a1 - 16;
if ( (unsigned __int8)(a1 - 16) > 0xD8u )
{
v6 = 10 * a1 - 8;
a2[2] = v6;
a2[1] = v6;
*a2 = v6;
}
else
{
*a2 = valuerange[v5 / 0x24u % 6];
result = (unsigned __int8)valuerange[v5 % 6u];
a2[2] = result;
a2[1] = valuerange[v5 / 6u % 6];
}
}
else
{
v2 = 3LL * a1;
v3 = byte_403862[v2];
result = *(unsigned __int16 *)((char *)&basic16 + v2);
a2[2] = v3;
*(_WORD *)a2 = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, _BYTE *a2)
{
__int64 v2; // rax
char v3; // dl
__int64 result; // rax
unsigned __int8 v5; // cl
char v6; // di

if ( a1 > 0xFu )
{
v5 = a1 - 16;
if ( (unsigned __int8)(a1 - 16) > 0xD8u )
{
v6 = 10 * a1 - 8;
a2[2] = v6;
a2[1] = v6;
*a2 = v6;
}
else
{
*a2 = valuerange[v5 / 0x24u % 6];
result = (unsigned __int8)valuerange[v5 % 6u];
a2[2] = result;
a2[1] = valuerange[v5 / 6u % 6];
}
}
else
{
v2 = 3LL * a1;
v3 = byte_403862[v2];
result = *(unsigned __int16 *)((char *)&basic16 + v2);
a2[2] = v3;
*(_WORD *)a2 = result;
}
return result;
}

original_code:
static void xterm2rgb(unsigned char color, unsigned char* rgb)
{
 if(color<16)
 {
  rgb[0] = basic16[color][0];
  rgb[1] = basic16[color][1];
  rgb[2] = basic16[color][2];
 }
 if(color>=16 && color<=232)
 {
  color-=16;
  rgb[0] = valuerange[(color/36)%6];
  rgb[1] = valuerange[(color/6)%6];
  rgb[2] = valuerange[color%6];
 }
 if(color>=233 && color<=253)
 {
  rgb[0]=rgb[1]=rgb[2] = 8+(color-232)*0x0a;
 }
}

canonical_original_code:
static void xterm2rgb(unsigned char color, unsigned char *rgb)
{
 if(color<16)
 {
  rgb[0] = basic16[color][0];
  rgb[1] = basic16[color][1];
  rgb[2] = basic16[color][2];
 }
 if(color>=16 && color<=232)
 {
  color-=16;
  rgb[0] = valuerange[(color/36)%6];
  rgb[1] = valuerange[(color/6)%6];
  rgb[2] = valuerange[color%6];
 }
 if(color>=233 && color<=253)
 {
  rgb[0]=rgb[1]=rgb[2] = 8+(color-232)*0x0a;
 }
}

variable_types:
color
unsigned char

variable_types:
rgb
unsigned char *

return_type: void


maketable

decompiled_code:
__int64 maketable()
{
int v0; // er11
__int64 v1; // r10
__int64 result; // rax
__int16 v3; // [rsp+Dh] [rbp-3h] BYREF
unsigned __int8 v4; // [rsp+Fh] [rbp-1h]

LOBYTE(v0) = 0;
v3 = 0;
v4 = 0;
do
{
xterm2rgb(v0, &v3);
*(_WORD *)(v1 - 3) = v3;
result = v4;
*(_BYTE *)(v1 - 1) = v4;
}
while ( v0 != 254 );
return result;
}

original_code:
static void maketable()
{
 unsigned char c, rgb[3] = {0, 0, 0};
 for(c=0;c<=253;c++)
 {
  xterm2rgb(c,rgb);
  colortable[c][0] = rgb[0];
  colortable[c][1] = rgb[1];
  colortable[c][2] = rgb[2];
 }
}

canonical_original_code:
static void maketable()
{
 unsigned char c; unsigned char rgb[3] = {0, 0, 0};
 for(c=0;c<=253;c++)
 {
  xterm2rgb(c,rgb);
  colortable[c][0] = rgb[0];
  colortable[c][1] = rgb[1];
  colortable[c][2] = rgb[2];
 }
}

variable_types:
c
unsigned char

variable_types:
rgb
unsigned char [3]


decompiled_code:
__int64 __fastcall rgb2xterm(unsigned __int8 *a1)
{
int v2; // er9
int v3; // edi
unsigned int v4; // er8
double v5; // xmm3_8
unsigned __int8 *v6; // rax
unsigned int v7; // edx
double v8; // xmm2_8
double v9; // xmm0_8
bool v10; // cc
double v11; // xmm0_8

maketable();
v2 = *a1;
v3 = a1[1];
v4 = 0;
v5 = 1.0e10;
v6 = (unsigned __int8 *)&colortable;
v7 = 0;
do
{
v8 = (double)(v6[1] - v3);
v9 = (double)(v6[2] - a1[2]) * (double)(v6[2] - a1[2]) + (double)(*v6 - v2) * (double)(*v6 - v2) + v8 * v8;
v10 = v5 <= v9;
v11 = fmin(v9, v5);
if ( !v10 )
v4 = v7;
++v7;
v5 = v11;
v6 += 3;
}
while ( (_BYTE)v7 != 0xFE );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 *a1)
{
int v2; // er9
int v3; // edi
unsigned int v4; // er8
double v5; // xmm3_8
unsigned __int8 *v6; // rax
unsigned int v7; // edx
double v8; // xmm2_8
double v9; // xmm0_8
bool v10; // cc
double v11; // xmm0_8

func8();
v2 = *a1;
v3 = a1[1];
v4 = 0;
v5 = 1.0e10;
v6 = (unsigned __int8 *)&colortable;
v7 = 0;
do
{
v8 = (double)(v6[1] - v3);
v9 = (double)(v6[2] - a1[2]) * (double)(v6[2] - a1[2]) + (double)(*v6 - v2) * (double)(*v6 - v2) + v8 * v8;
v10 = v5 <= v9;
v11 = fmin(v9, v5);
if ( !v10 )
v4 = v7;
++v7;
v5 = v11;
v6 += 3;
}
while ( (_BYTE)v7 != 0xFE );
return v4;
}

original_code:
static unsigned char rgb2xterm(unsigned char* rgb)
{
 unsigned char c, best_match=0;
 double d, smallest_distance;
 if(!initialized)
  maketable();
 smallest_distance = 10000000000.0;
 for(c=0;c<=253;c++)
 {
  d = pow(colortable[c][0]-rgb[0],2.0) +
   pow(colortable[c][1]-rgb[1],2.0) +
   pow(colortable[c][2]-rgb[2],2.0);
  if(d<smallest_distance)
  {
   smallest_distance = d;
   best_match=c;
  }
 }
 return best_match;
}

canonical_original_code:
static unsigned char rgb2xterm(unsigned char *rgb)
{
 unsigned char c; unsigned char best_match = 0;
 double d; double smallest_distance;
 if(!initialized)
  maketable();
 smallest_distance = 10000000000.0;
 for(c=0;c<=253;c++)
 {
  d = pow(colortable[c][0]-rgb[0],2.0) +
   pow(colortable[c][1]-rgb[1],2.0) +
   pow(colortable[c][2]-rgb[2],2.0);
  if(d<smallest_distance)
  {
   smallest_distance = d;
   best_match=c;
  }
 }
 return best_match;
}

variable_types: d double

return_type: unsigned char


original_code:
unsigned char xterm_color(int color_val)
{
 unsigned char rgb[3];
 if (color_val > 255)
  color_val = 255;
 rgb[0] = 255.0 * mandel256[color_val].red;
 rgb[1] = 255.0 * mandel256[color_val].green;
 rgb[2] = 255.0 * mandel256[color_val].blue;
 color_val = rgb2xterm(rgb);
 ((void) sizeof ((0 <= color_val && color_val <= 255) ? 1 : 0), __extension__ ({ if (0 <= color_val && color_val <= 255) ; else __assert_fail ("0 <= color_val && color_val <= 255", "mandel-lib.c", 437, __extension__ __PRETTY_FUNCTION__); }));
 return color_val;
}


decompiled_code:
ssize_t __fastcall insist_write(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t result; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
result = write(fd, buf, v5);
if ( result < 0 )
break;
buf = (char *)buf + result;
v5 -= result;
if ( !v5 )
return v3;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t result; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
result = write(fd, buf, v5);
if ( result < 0 )
break;
buf = (char *)buf + result;
v5 -= result;
if ( !v5 )
return v3;
}
return result;
}

original_code:
ssize_t insist_write(int fd, const char *buf, size_t count)
{
 ssize_t ret;
 size_t orig_count = count;
 while (count > 0) {
  ret = write(fd, buf, count);
  if (ret < 0)
   return ret;
  buf += ret;
  count -= ret;
 }
 return orig_count;
}

canonical_original_code:
long insist_write(int fd, char *buf, unsigned long count)
{
 long ret;
 unsigned long orig_count = count;
 while (count > 0) {
  ret = write(fd, buf, count);
  if (ret < 0)
   return ret;
  buf += ret;
  count -= ret;
 }
 return orig_count;
}

variable_types: fd int

variable_types:
buf
char *

variable_types:
count
unsigned long

variable_types: ret long

return_type: long


decompiled_code:
size_t __fastcall set_xterm_color(int fd, unsigned __int8 a2)
{
size_t v2; // rax
ssize_t v3; // rbx
size_t result; // rax
char v5[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v5, 0x64uLL, "\x1B[38;5;%dm", a2);
v2 = strlen(v5);
v3 = insist_write(fd, v5, v2);
result = strlen(v5);
if ( v3 != result )
{
perror("set_xterm_color: insist_write");
exit(1);
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func13(int fd, unsigned __int8 a2)
{
size_t v2; // rax
ssize_t v3; // rbx
size_t result; // rax
char v5[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v5, 0x64uLL, "\x1B[38;5;%dm", a2);
v2 = strlen(v5);
v3 = func12(fd, v5, v2);
result = strlen(v5);
if ( v3 != result )
{
perror("set_xterm_color: insist_write");
exit(1);
}
return result;
}

original_code:
void set_xterm_color(int fd, unsigned char color)
{
 char buf[100];
 snprintf(buf, 100, "\033[38;5;%dm", color);
 if (insist_write(fd, buf, strlen(buf)) != strlen(buf)) {
  perror("set_xterm_color: insist_write");
  exit(1);
 }
}

variable_types:
buf
char [100]


decompiled_code:
ssize_t __fastcall reset_xterm_color(int a1)
{
ssize_t result; // rax

result = insist_write(a1, "\x1B[0m", 4uLL);
if ( result != 4 )
{
perror("reset_xterm_color: insist_write");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func14(int a1)
{
ssize_t result; // rax

result = func12(a1, "\x1B[0m", 4uLL);
if ( result != 4 )
{
perror("reset_xterm_color: insist_write");
exit(1);
}
return result;
}

original_code:
void reset_xterm_color(int fd)
{
 if (insist_write(fd, "\033[0m", 4) != 4) {
  perror("reset_xterm_color: insist_write");
  exit(1);
 }
}


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

if ( high_tasks <= 0 )
{
if ( !high_tasks && current_state == 1 )
{
fwrite("low tasks allowed\n", 1uLL, 0x12uLL, stderr);
current_tasks = (__int64)&low_tasks;
current_state = 0;
high_proc = current_proc;
result = low_proc;
current_proc = low_proc;
}
}
else
{
result = (unsigned int)current_state;
if ( !current_state )
{
fwrite("high tasks only\n", 1uLL, 0x10uLL, stderr);
current_tasks = (__int64)&high_tasks;
current_state = 1;
low_proc = current_proc;
result = high_proc;
current_proc = high_proc;
}
}
return result;
}

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

if ( high_tasks <= 0 )
{
if ( !high_tasks && current_state == 1 )
{
fwrite("low tasks allowed\n", 1uLL, 0x12uLL, stderr);
current_tasks = (__int64)&low_tasks;
current_state = 0;
high_proc = current_proc;
result = low_proc;
current_proc = low_proc;
}
}
else
{
result = (unsigned int)current_state;
if ( !current_state )
{
fwrite("high tasks only\n", 1uLL, 0x10uLL, stderr);
current_tasks = (__int64)&high_tasks;
current_state = 1;
low_proc = current_proc;
result = high_proc;
current_proc = high_proc;
}
}
return result;
}

original_code:
static void
state_check( void )
{
    if ( high_tasks > 0 && current_state == 0 )
    {
        fprintf(stderr,"high tasks only\n");
        low_proc = current_proc;
        current_tasks = &high_tasks;
        current_proc = high_proc;
        current_state = 1;
    }
    else if ( high_tasks == 0 && current_state == 1 )
    {
        fprintf(stderr,"low tasks allowed\n");
        high_proc = current_proc;
        current_tasks = &low_tasks;
        current_proc = low_proc;
        current_state = 0;
    }
}

canonical_original_code:
static void state_check()
{
    if ( high_tasks > 0 && current_state == 0 )
    {
        fprintf(stderr,"high tasks only\n");
        low_proc = current_proc;
        current_tasks = &high_tasks;
        current_proc = high_proc;
        current_state = 1;
    }
    else if ( high_tasks == 0 && current_state == 1 )
    {
        fprintf(stderr,"low tasks allowed\n");
        high_proc = current_proc;
        current_tasks = &low_tasks;
        current_proc = low_proc;
        current_state = 0;
    }
}


decompiled_code:
int __fastcall sched_high_task_by_id(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // er12
__int64 v4; // rax

kill(*(_DWORD *)current_proc, 19);
if ( *(_DWORD *)(low_proc + 4) == a1 )
{
v2 = *(_DWORD *)low_proc;
v4 = remove_q(low_proc);
low_proc = v4;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = v4;
}
else
{
v1 = (unsigned int *)find_q(a1, low_proc, (unsigned int)low_tasks);
if ( !v1 )
{
fwrite("\t\tProcess not found in low priority queue\n", 1uLL, 0x2AuLL, stderr);
fflush(stderr);
goto LABEL_6;
}
v2 = *v1;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = remove_q(v1);
else
remove_q(v1);
}
--low_tasks;
insert_q(v2, a1, high_proc);
++high_tasks;
LABEL_6:
state_check();
return kill(*(_DWORD *)current_proc, 18);
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // er12
__int64 v4; // rax

kill(*(_DWORD *)current_proc, 19);
if ( *(_DWORD *)(low_proc + 4) == a1 )
{
v2 = *(_DWORD *)low_proc;
v4 = remove_q(low_proc);
low_proc = v4;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = v4;
}
else
{
v1 = (unsigned int *)find_q(a1, low_proc, (unsigned int)low_tasks);
if ( !v1 )
{
fwrite("\t\tProcess not found in low priority queue\n", 1uLL, 0x2AuLL, stderr);
fflush(stderr);
goto LABEL_6;
}
v2 = *v1;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = remove_q(v1);
else
remove_q(v1);
}
--low_tasks;
insert_q(v2, a1, high_proc);
++high_tasks;
LABEL_6:
func0();
return kill(*(_DWORD *)current_proc, 18);
}

original_code:
static void
sched_high_task_by_id( int id )
{
    pid_t pid_to_be;
        kill( current_proc -> pid, 19 );
    if ( low_proc -> id == id )
    {
        pid_to_be = low_proc -> pid;
        low_proc = remove_q( low_proc );
            if ( current_proc -> id == id )
            {
                current_proc = low_proc;
            }
        low_tasks--;
        insert_q( pid_to_be, id, high_proc );
        high_tasks++;
    }
    else
    {
        queue *buf = find_q ( id, low_proc, low_tasks );
        if ( buf != ((void *)0) )
        {
            pid_to_be = buf -> pid;
            if ( current_proc -> id == id )
            {
                current_proc = remove_q( buf );
            }
            else
            {
                remove_q( buf );
            }
            low_tasks--;
            insert_q( pid_to_be, id, high_proc );
            high_tasks++;
        }
        else
        {
            fprintf( stderr, "\t\tProcess not found in low priority queue\n" );
            fflush( stderr );
        }
    }
    state_check();
    kill( current_proc -> pid, 18 );
}

canonical_original_code:
static void sched_high_task_by_id(int id)
{
    int pid_to_be;
        kill( current_proc -> pid, 19 );
    if ( low_proc -> id == id )
    {
        pid_to_be = low_proc -> pid;
        low_proc = remove_q( low_proc );
            if ( current_proc -> id == id )
            {
                current_proc = low_proc;
            }
        low_tasks--;
        insert_q( pid_to_be, id, high_proc );
        high_tasks++;
    }
    else
    {
        struct __queue *buf = find_q ( id, low_proc, low_tasks );
        if ( buf != ((void *)0) )
        {
            pid_to_be = buf -> pid;
            if ( current_proc -> id == id )
            {
                current_proc = remove_q( buf );
            }
            else
            {
                remove_q( buf );
            }
            low_tasks--;
            insert_q( pid_to_be, id, high_proc );
            high_tasks++;
        }
        else
        {
            fprintf( stderr, "\t\tProcess not found in low priority queue\n" );
            fflush( stderr );
        }
    }
    state_check();
    kill( current_proc -> pid, 18 );
}

variable_types: id int

variable_types: struct __queue { int pid; int id; struct __queue *prev; struct __queue *next; } *


decompiled_code:
int __fastcall sched_low_task_by_id(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // er12
__int64 v4; // rax

kill(*(_DWORD *)current_proc, 19);
if ( *(_DWORD *)(high_proc + 4) == a1 )
{
v2 = *(_DWORD *)high_proc;
v4 = remove_q(high_proc);
high_proc = v4;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = v4;
}
else
{
v1 = (unsigned int *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( !v1 )
{
fwrite("\t\tProcess not found in high priority queue\n", 1uLL, 0x2BuLL, stderr);
fflush(stderr);
goto LABEL_6;
}
v2 = *v1;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = remove_q(v1);
else
remove_q(v1);
}
--high_tasks;
insert_q(v2, a1, low_proc);
++low_tasks;
LABEL_6:
state_check();
return kill(*(_DWORD *)current_proc, 18);
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // er12
__int64 v4; // rax

kill(*(_DWORD *)current_proc, 19);
if ( *(_DWORD *)(high_proc + 4) == a1 )
{
v2 = *(_DWORD *)high_proc;
v4 = remove_q(high_proc);
high_proc = v4;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = v4;
}
else
{
v1 = (unsigned int *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( !v1 )
{
fwrite("\t\tProcess not found in high priority queue\n", 1uLL, 0x2BuLL, stderr);
fflush(stderr);
goto LABEL_6;
}
v2 = *v1;
if ( *(_DWORD *)(current_proc + 4) == a1 )
current_proc = remove_q(v1);
else
remove_q(v1);
}
--high_tasks;
insert_q(v2, a1, low_proc);
++low_tasks;
LABEL_6:
func0();
return kill(*(_DWORD *)current_proc, 18);
}

original_code:
static void
sched_low_task_by_id( int id )
{
    pid_t pid_to_be;
    kill( current_proc -> pid, 19 );
    if ( high_proc -> id == id )
    {
        pid_to_be = high_proc->pid;
        high_proc = remove_q( high_proc );
            if ( current_proc -> id == id )
            {
                current_proc = high_proc;
            }
        high_tasks--;
        insert_q( pid_to_be, id, low_proc );
        low_tasks++;
    }
    else
    {
        queue *buf = find_q ( id, high_proc, high_tasks );
        if ( buf != ((void *)0) )
        {
            pid_to_be = buf->pid;
            if ( current_proc -> id == id )
            {
                current_proc = remove_q( buf );
            }
            else
            {
                remove_q( buf );
            }
            high_tasks--;
            insert_q( pid_to_be, id, low_proc );
            low_tasks++;
        }
        else
        {
            fprintf( stderr, "\t\tProcess not found in high priority queue\n" );
            fflush( stderr );
        }
    }
    state_check();
    kill( current_proc -> pid, 18 );
}

canonical_original_code:
static void sched_low_task_by_id(int id)
{
    int pid_to_be;
    kill( current_proc -> pid, 19 );
    if ( high_proc -> id == id )
    {
        pid_to_be = high_proc->pid;
        high_proc = remove_q( high_proc );
            if ( current_proc -> id == id )
            {
                current_proc = high_proc;
            }
        high_tasks--;
        insert_q( pid_to_be, id, low_proc );
        low_tasks++;
    }
    else
    {
        struct __queue *buf = find_q ( id, high_proc, high_tasks );
        if ( buf != ((void *)0) )
        {
            pid_to_be = buf->pid;
            if ( current_proc -> id == id )
            {
                current_proc = remove_q( buf );
            }
            else
            {
                remove_q( buf );
            }
            high_tasks--;
            insert_q( pid_to_be, id, low_proc );
            low_tasks++;
        }
        else
        {
            fprintf( stderr, "\t\tProcess not found in high priority queue\n" );
            fflush( stderr );
        }
    }
    state_check();
    kill( current_proc -> pid, 18 );
}

user_defined_types: struct __queue { int pid; int id; struct __queue *prev; struct __queue *next; }


decompiled_code:
unsigned int sigalrm_handler()
{
if ( *(int *)current_tasks <= 0 || !current_proc )
return alarm(2u);
fprintf(stderr, "\t\tSTOP\t\tid:%d\n", *(unsigned int *)(current_proc + 4));
fflush(stderr);
kill(*(_DWORD *)current_proc, 19);
return alarm(2u);
}

canonical_decompiled_code:
unsigned int func1()
{
if ( *(int *)current_tasks <= 0 || !current_proc )
return alarm(2u);
fprintf(stderr, "\t\tSTOP\t\tid:%d\n", *(unsigned int *)(current_proc + 4));
fflush(stderr);
kill(*(_DWORD *)current_proc, 19);
return alarm(2u);
}

original_code:
static void
sigalrm_handler( int signum )
{
    if ( ( (*current_tasks) > 0 ) && current_proc )
    {
        fprintf( stderr, "\t\tSTOP\t\tid:%d\n", current_proc->id );
        fflush( stderr );
        kill( current_proc->pid, 19 );
    }
    alarm( 2 );
}

canonical_original_code:
static void sigalrm_handler(int signum)
{
    if ( ( (*current_tasks) > 0 ) && current_proc )
    {
        fprintf( stderr, "\t\tSTOP\t\tid:%d\n", current_proc->id );
        fflush( stderr );
        kill( current_proc->pid, 19 );
    }
    alarm( 2 );
}

variable_types: signum int


decompiled_code:
__int64 __fastcall sched_create_task(char *path)
{
__pid_t v1; // eax
__int64 v2; // rsi
__int64 result; // rax
char *envp; // [rsp+8h] [rbp-30h] BYREF
char *argv[5]; // [rsp+10h] [rbp-28h] BYREF

v1 = fork();
if ( v1 < 0 )
{
perror("scheduler: fork");
exit(1);
}
if ( !v1 )
{
argv[0] = path;
argv[1] = 0LL;
argv[2] = 0LL;
argv[3] = 0LL;
envp = 0LL;
raise(19);
execve(path, argv, &envp);
exit(-1);
}
v2 = (unsigned int)super_id++;
if ( current_state )
result = insert_q((unsigned int)v1, v2, low_proc);
else
result = insert_q((unsigned int)v1, v2, current_proc);
++low_tasks;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *path)
{
__pid_t v1; // eax
__int64 v2; // rsi
__int64 result; // rax
char *envp; // [rsp+8h] [rbp-30h] BYREF
char *argv[5]; // [rsp+10h] [rbp-28h] BYREF

v1 = fork();
if ( v1 < 0 )
{
perror("scheduler: fork");
exit(1);
}
if ( !v1 )
{
argv[0] = path;
argv[1] = 0LL;
argv[2] = 0LL;
argv[3] = 0LL;
envp = 0LL;
raise(19);
execve(path, argv, &envp);
exit(-1);
}
v2 = (unsigned int)super_id++;
if ( current_state )
result = insert_q((unsigned int)v1, v2, low_proc);
else
result = insert_q((unsigned int)v1, v2, current_proc);
++low_tasks;
return result;
}

original_code:
static void
sched_create_task( char *executable )
{
    pid_t p;
    p = fork();
    if ( p < 0 ) {
        perror( "scheduler: fork" );
        exit( 1 );
    }
    if ( p == 0 ) {
        char *newargv[] = { executable, ((void *)0), ((void *)0), ((void *)0) };
        char *newenviron[] = { ((void *)0) };
        raise( 19 );
        execve( executable, newargv, newenviron );
        exit( -1 );
    }
    else {
        if ( current_state == 0 )
            insert_q( p, super_id++, current_proc );
        else
            insert_q( p, super_id++, low_proc );
        low_tasks++;
    }
}

canonical_original_code:
static void sched_create_task(char *executable)
{
    int p;
    p = fork();
    if ( p < 0 ) {
        perror( "scheduler: fork" );
        exit( 1 );
    }
    if ( p == 0 ) {
        char *newargv[] = { executable, ((void *)0), ((void *)0), ((void *)0) };
        char *newenviron[] = { ((void *)0) };
        raise( 19 );
        execve( executable, newargv, newenviron );
        exit( -1 );
    }
    else {
        if ( current_state == 0 )
            insert_q( p, super_id++, current_proc );
        else
            insert_q( p, super_id++, low_proc );
        low_tasks++;
    }
}

variable_types:
executable
char *

variable_types:
newargv
char *[]

variable_types:
newenviron
char *[]


decompiled_code:
__sighandler_t install_signal_handlers()
{
__m128i v0; // xmm1
__m128i v1; // xmm2
__m128i v2; // xmm3
__m128i v3; // xmm4
__m128i v4; // xmm5
__m128i v5; // xmm6
__m128i v6; // xmm7
__sighandler_t result; // rax
sigset_t v8; // [rsp+0h] [rbp-128h] BYREF
struct sigaction v9; // [rsp+80h] [rbp-A8h] BYREF

v9.sa_handler = (__sighandler_t)sigchld_handler;
v9.sa_flags = 0x10000000;
sigemptyset(&v8);
sigaddset(&v8, 17);
sigaddset(&v8, 14);
v0 = _mm_loadu_si128((const __m128i *)&v8.__val[2]);
v1 = _mm_loadu_si128((const __m128i *)&v8.__val[4]);
v2 = _mm_loadu_si128((const __m128i *)&v8.__val[6]);
v3 = _mm_loadu_si128((const __m128i *)&v8.__val[8]);
v4 = _mm_loadu_si128((const __m128i *)&v8.__val[10]);
*(__m128i *)v9.sa_mask.__val = _mm_loadu_si128((const __m128i *)&v8);
v5 = _mm_loadu_si128((const __m128i *)&v8.__val[12]);
v6 = _mm_loadu_si128((const __m128i *)&v8.__val[14]);
*(__m128i *)&v9.sa_mask.__val[2] = v0;
*(__m128i *)&v9.sa_mask.__val[4] = v1;
*(__m128i *)&v9.sa_mask.__val[6] = v2;
*(__m128i *)&v9.sa_mask.__val[8] = v3;
*(__m128i *)&v9.sa_mask.__val[10] = v4;
*(__m128i *)&v9.sa_mask.__val[12] = v5;
*(__m128i *)&v9.sa_mask.__val[14] = v6;
if ( sigaction(17, &v9, 0LL) < 0 )
{
perror("sigaction: sigchld");
exit(1);
}
v9.sa_handler = (__sighandler_t)sigalrm_handler;
if ( sigaction(14, &v9, 0LL) < 0 )
{
perror("sigaction: sigalrm");
exit(1);
}
result = signal(13, (__sighandler_t)((char *)state_check + 1));
if ( result == (__sighandler_t)-1LL )
{
perror("signal: sigpipe");
exit(1);
}
return result;
}

canonical_decompiled_code:
__sighandler_t func3()
{
__m128i v0; // xmm1
__m128i v1; // xmm2
__m128i v2; // xmm3
__m128i v3; // xmm4
__m128i v4; // xmm5
__m128i v5; // xmm6
__m128i v6; // xmm7
__sighandler_t result; // rax
sigset_t v8; // [rsp+0h] [rbp-128h] BYREF
struct sigaction v9; // [rsp+80h] [rbp-A8h] BYREF

v9.sa_handler = (__sighandler_t)sigchld_handler;
v9.sa_flags = 0x10000000;
sigemptyset(&v8);
sigaddset(&v8, 17);
sigaddset(&v8, 14);
v0 = _mm_loadu_si128((const __m128i *)&v8.__val[2]);
v1 = _mm_loadu_si128((const __m128i *)&v8.__val[4]);
v2 = _mm_loadu_si128((const __m128i *)&v8.__val[6]);
v3 = _mm_loadu_si128((const __m128i *)&v8.__val[8]);
v4 = _mm_loadu_si128((const __m128i *)&v8.__val[10]);
*(__m128i *)v9.sa_mask.__val = _mm_loadu_si128((const __m128i *)&v8);
v5 = _mm_loadu_si128((const __m128i *)&v8.__val[12]);
v6 = _mm_loadu_si128((const __m128i *)&v8.__val[14]);
*(__m128i *)&v9.sa_mask.__val[2] = v0;
*(__m128i *)&v9.sa_mask.__val[4] = v1;
*(__m128i *)&v9.sa_mask.__val[6] = v2;
*(__m128i *)&v9.sa_mask.__val[8] = v3;
*(__m128i *)&v9.sa_mask.__val[10] = v4;
*(__m128i *)&v9.sa_mask.__val[12] = v5;
*(__m128i *)&v9.sa_mask.__val[14] = v6;
if ( sigaction(17, &v9, 0LL) < 0 )
{
perror("sigaction: sigchld");
exit(1);
}
v9.sa_handler = (__sighandler_t)sigalrm_handler;
if ( sigaction(14, &v9, 0LL) < 0 )
{
perror("sigaction: sigalrm");
exit(1);
}
result = signal(13, (__sighandler_t)((char *)state_check + 1));
if ( result == (__sighandler_t)-1LL )
{
perror("signal: sigpipe");
exit(1);
}
return result;
}

original_code:
static void
install_signal_handlers( void )
{
    sigset_t sigset;
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sa.sa_flags = 0x10000000;
    sigemptyset( &sigset );
    sigaddset( &sigset, 17 );
    sigaddset( &sigset, 14 );
    sa.sa_mask = sigset;
    if ( sigaction(17, &sa, ((void *)0) ) < 0 ) {
        perror( "sigaction: sigchld" );
        exit( 1 );
    }
    sa.__sigaction_handler.sa_handler = sigalrm_handler;
    if ( sigaction( 14, &sa, ((void *)0) ) < 0 ) {
        perror( "sigaction: sigalrm" );
        exit( 1 );
    }
    if ( signal(13, ((__sighandler_t) 1)) == ((__sighandler_t) -1) ) {
        perror( "signal: sigpipe" );
        exit( 1 );
    }
}

canonical_original_code:
static void install_signal_handlers()
{
    struct __sigset_t sigset;
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sa.sa_flags = 0x10000000;
    sigemptyset( &sigset );
    sigaddset( &sigset, 17 );
    sigaddset( &sigset, 14 );
    sa.sa_mask = sigset;
    if ( sigaction(17, &sa, ((void *)0) ) < 0 ) {
        perror( "sigaction: sigchld" );
        exit( 1 );
    }
    sa.__sigaction_handler.sa_handler = sigalrm_handler;
    if ( sigaction( 14, &sa, ((void *)0) ) < 0 ) {
        perror( "sigaction: sigalrm" );
        exit( 1 );
    }
    if ( signal(13, ((void (*)(int)) 1)) == ((__sighandler_t) -1) ) {
        perror( "signal: sigpipe" );
        exit( 1 );
    }
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
signed int sigchld_handler()
{
signed int result; // eax
unsigned int v1; // ebp
__int64 v2; // rdi
__int64 v3; // rdi
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rbp
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rbp
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = current_tasks;
if ( *(int *)current_tasks <= 0 || !current_proc )
return result;
do
{
result = waitpid(-1, stat_loc, 11);
v1 = result;
if ( result <= 0 )
return result;
result = stat_loc[0];
if ( stat_loc[0] == 0xFFFF )
return result;
if ( LOBYTE(stat_loc[0]) == 127 )
{
if ( *(_DWORD *)current_proc != v1 )
{
fwrite("\t\tSIGSTOP irrelevant\n", 1uLL, 0x15uLL, stderr);
return fflush(stderr);
}
current_proc = next_q();
v6 = current_proc;
goto LABEL_30;
}
if ( (stat_loc[0] & 0x7F) == 0 )
{
fprintf(stderr, "\t\tDEAD\t\tid:%d\n", *(unsigned int *)(current_proc + 4));
fflush(stderr);
if ( *(_DWORD *)current_proc == v1 )
{
v7 = ((__int64 (*)(void))remove_q)();
current_proc = v7;
--*(_DWORD *)current_tasks;
if ( current_state == 1 )
high_proc = v7;
else
low_proc = v7;
}
else
{
v2 = find_q_with_pid(v1, high_proc, (unsigned int)high_tasks);
if ( v2 )
{
remove_q(v2);
--high_tasks;
}
else
{
v4 = find_q_with_pid(v1, low_proc, (unsigned int)low_tasks);
if ( v4 )
{
remove_q(v4);
--low_tasks;
}
}
}
state_check();
if ( !*(_DWORD *)current_tasks )
{
fwrite("\t\tboth queues are empty\n", 1uLL, 0x18uLL, stderr);
fflush(stderr);
exit(0);
}
v6 = current_proc;
LABEL_30:
kill(*(_DWORD *)v6, 18);
fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v6 + 4));
return alarm(2u);
}
}
while ( (char)((stat_loc[0] & 0x7F) + 1) <= 1 );
if ( *(_DWORD *)current_proc == v1 )
{
v8 = remove_q(current_proc);
current_proc = v8;
if ( current_state == 1 )
high_proc = v8;
else
low_proc = v8;
--*(_DWORD *)current_tasks;
state_check();
alarm(2u);
}
else
{
v3 = find_q_with_pid(v1, high_proc, (unsigned int)high_tasks);
if ( v3 )
{
remove_q(v3);
--high_tasks;
}
else
{
v5 = find_q_with_pid(v1, low_proc, (unsigned int)low_tasks);
if ( v5 )
{
remove_q(v5);
--low_tasks;
}
}
state_check();
}
if ( !*(_DWORD *)current_tasks )
{
fwrite("\t\tboth queues are empty\n", 1uLL, 0x18uLL, stderr);
fflush(stderr);
exit(0);
}
v9 = current_proc;
kill(*(_DWORD *)current_proc, 18);
return fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v9 + 4));
}

canonical_decompiled_code:
signed int func4()
{
signed int result; // eax
unsigned int v1; // ebp
__int64 v2; // rdi
__int64 v3; // rdi
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rbp
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rbp
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = current_tasks;
if ( *(int *)current_tasks <= 0 || !current_proc )
return result;
do
{
result = waitpid(-1, stat_loc, 11);
v1 = result;
if ( result <= 0 )
return result;
result = stat_loc[0];
if ( stat_loc[0] == 0xFFFF )
return result;
if ( LOBYTE(stat_loc[0]) == 127 )
{
if ( *(_DWORD *)current_proc != v1 )
{
fwrite("\t\tSIGSTOP irrelevant\n", 1uLL, 0x15uLL, stderr);
return fflush(stderr);
}
current_proc = next_q();
v6 = current_proc;
goto LABEL_30;
}
if ( (stat_loc[0] & 0x7F) == 0 )
{
fprintf(stderr, "\t\tDEAD\t\tid:%d\n", *(unsigned int *)(current_proc + 4));
fflush(stderr);
if ( *(_DWORD *)current_proc == v1 )
{
v7 = ((__int64 (*)(void))remove_q)();
current_proc = v7;
--*(_DWORD *)current_tasks;
if ( current_state == 1 )
high_proc = v7;
else
low_proc = v7;
}
else
{
v2 = find_q_with_pid(v1, high_proc, (unsigned int)high_tasks);
if ( v2 )
{
remove_q(v2);
--high_tasks;
}
else
{
v4 = find_q_with_pid(v1, low_proc, (unsigned int)low_tasks);
if ( v4 )
{
remove_q(v4);
--low_tasks;
}
}
}
func0();
if ( !*(_DWORD *)current_tasks )
{
fwrite("\t\tboth queues are empty\n", 1uLL, 0x18uLL, stderr);
fflush(stderr);
exit(0);
}
v6 = current_proc;
LABEL_30:
kill(*(_DWORD *)v6, 18);
fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v6 + 4));
return alarm(2u);
}
}
while ( (char)((stat_loc[0] & 0x7F) + 1) <= 1 );
if ( *(_DWORD *)current_proc == v1 )
{
v8 = remove_q(current_proc);
current_proc = v8;
if ( current_state == 1 )
high_proc = v8;
else
low_proc = v8;
--*(_DWORD *)current_tasks;
func0();
alarm(2u);
}
else
{
v3 = find_q_with_pid(v1, high_proc, (unsigned int)high_tasks);
if ( v3 )
{
remove_q(v3);
--high_tasks;
}
else
{
v5 = find_q_with_pid(v1, low_proc, (unsigned int)low_tasks);
if ( v5 )
{
remove_q(v5);
--low_tasks;
}
}
func0();
}
if ( !*(_DWORD *)current_tasks )
{
fwrite("\t\tboth queues are empty\n", 1uLL, 0x18uLL, stderr);
fflush(stderr);
exit(0);
}
v9 = current_proc;
kill(*(_DWORD *)current_proc, 18);
return fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v9 + 4));
}

original_code:
static void
sigchld_handler( int signum )
{
    int status;
    pid_t p;
    queue * buf;
    if ( *current_tasks > 0 && current_proc )
    {
        while( ( p = waitpid( -1, &status, 2 | 8 | 1 ) ) > 0 )
        {
            if ( ((status) == 0xffff) )
            {
                return;
            }
            else if ( (((status) & 0xff) == 0x7f) )
            {
                if ( p == current_proc->pid )
                {
                    current_proc = next_q( current_proc );
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    alarm( 2 );
                    return;
                }
                else
                {
                    fprintf( stderr, "\t\tSIGSTOP irrelevant\n" );
                    fflush( stderr );
                    return;
                }
            }
            else if ( (((status) & 0x7f) == 0) )
            {
                fprintf( stderr, "\t\tDEAD\t\tid:%d\n", current_proc -> id );
                fflush( stderr );
                if ( current_proc -> pid == p )
                {
                    current_proc = remove_q( current_proc );
                    *current_tasks = *current_tasks-1;
                    if ( current_state == 1 )
                        high_proc = current_proc;
                    else
                        low_proc = current_proc;
                    state_check();
                }
                else
                {
                    buf = find_q_with_pid( p, high_proc, high_tasks );
                    if ( buf != ((void *)0) )
                    {
                        remove_q( buf );
                        high_tasks--;
                    }
                    else
                    {
                        buf = find_q_with_pid( p, low_proc, low_tasks );
                        if ( buf != ((void *)0) )
                        {
                            remove_q( buf );
                            low_tasks--;
                        }
                    }
                    state_check();
                }
                if ( *current_tasks )
                {
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    alarm( 2 );
                    return;
                }
                else
                {
                    fprintf( stderr, "\t\tboth queues are empty\n" );
                    fflush( stderr );
                    exit( 0 );
                }
            }
            else if ( (((signed char) (((status) & 0x7f) + 1) >> 1) > 0) )
            {
                if ( p == current_proc->pid)
                {
                    current_proc = remove_q( current_proc );
                    if ( current_state == 1 )
                        high_proc = current_proc;
                    else
                        low_proc = current_proc;
                    *current_tasks = *current_tasks-1;
                    state_check();
                    alarm( 2 );
                }
                else
                {
                    buf = find_q_with_pid( p, high_proc, high_tasks );
                    if ( buf != ((void *)0) )
                    {
                        remove_q( buf );
                        high_tasks--;
                    }
                    else
                    {
                        buf = find_q_with_pid( p, low_proc, low_tasks );
                        if ( buf != ((void *)0) )
                        {
                            remove_q( buf );
                            low_tasks--;
                        }
                    }
                    state_check();
                }
                if ( *current_tasks )
                {
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    return;
                }
                else
                {
                    fprintf( stderr,"\t\tboth queues are empty\n" );
                    fflush( stderr );
                    exit( 0 );
                }
            }
        }
    }
}

canonical_original_code:
static void sigchld_handler(int signum)
{
    int status;
    int p;
    struct __queue *buf;
    if ( *current_tasks > 0 && current_proc )
    {
        while( ( p = waitpid( -1, &status, 2 | 8 | 1 ) ) > 0 )
        {
            if ( ((status) == 0xffff) )
            {
                return;
            }
            else if ( (((status) & 0xff) == 0x7f) )
            {
                if ( p == current_proc->pid )
                {
                    current_proc = next_q( current_proc );
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    alarm( 2 );
                    return;
                }
                else
                {
                    fprintf( stderr, "\t\tSIGSTOP irrelevant\n" );
                    fflush( stderr );
                    return;
                }
            }
            else if ( (((status) & 0x7f) == 0) )
            {
                fprintf( stderr, "\t\tDEAD\t\tid:%d\n", current_proc -> id );
                fflush( stderr );
                if ( current_proc -> pid == p )
                {
                    current_proc = remove_q( current_proc );
                    *current_tasks = *current_tasks-1;
                    if ( current_state == 1 )
                        high_proc = current_proc;
                    else
                        low_proc = current_proc;
                    state_check();
                }
                else
                {
                    buf = find_q_with_pid( p, high_proc, high_tasks );
                    if ( buf != ((void *)0) )
                    {
                        remove_q( buf );
                        high_tasks--;
                    }
                    else
                    {
                        buf = find_q_with_pid( p, low_proc, low_tasks );
                        if ( buf != ((void *)0) )
                        {
                            remove_q( buf );
                            low_tasks--;
                        }
                    }
                    state_check();
                }
                if ( *current_tasks )
                {
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    alarm( 2 );
                    return;
                }
                else
                {
                    fprintf( stderr, "\t\tboth queues are empty\n" );
                    fflush( stderr );
                    exit( 0 );
                }
            }
            else if ( (((char) (((status) & 0x7f) + 1) >> 1) > 0) )
            {
                if ( p == current_proc->pid)
                {
                    current_proc = remove_q( current_proc );
                    if ( current_state == 1 )
                        high_proc = current_proc;
                    else
                        low_proc = current_proc;
                    *current_tasks = *current_tasks-1;
                    state_check();
                    alarm( 2 );
                }
                else
                {
                    buf = find_q_with_pid( p, high_proc, high_tasks );
                    if ( buf != ((void *)0) )
                    {
                        remove_q( buf );
                        high_tasks--;
                    }
                    else
                    {
                        buf = find_q_with_pid( p, low_proc, low_tasks );
                        if ( buf != ((void *)0) )
                        {
                            remove_q( buf );
                            low_tasks--;
                        }
                    }
                    state_check();
                }
                if ( *current_tasks )
                {
                    kill( current_proc->pid, 18 );
                    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", current_proc -> id);
                    return;
                }
                else
                {
                    fprintf( stderr,"\t\tboth queues are empty\n" );
                    fflush( stderr );
                    exit( 0 );
                }
            }
        }
    }
}

variable_types: status int

variable_types: p int


decompiled_code:
int signals_disable()
{
int result; // eax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v1);
sigaddset(&v1, 14);
sigaddset(&v1, 17);
result = sigprocmask(0, &v1, 0LL);
if ( result < 0 )
{
perror("signals_disable: sigprocmask");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v1);
sigaddset(&v1, 14);
sigaddset(&v1, 17);
result = sigprocmask(0, &v1, 0LL);
if ( result < 0 )
{
perror("signals_disable: sigprocmask");
exit(1);
}
return result;
}

original_code:
static void
signals_disable( void )
{
    sigset_t sigset;
    sigemptyset( &sigset );
    sigaddset( &sigset, 14 );
    sigaddset( &sigset, 17 );
    if ( sigprocmask(0, &sigset, ((void *)0) ) < 0 ) {
        perror( "signals_disable: sigprocmask" );
        exit( 1 );
    }
}

canonical_original_code:
static void signals_disable()
{
    struct __sigset_t sigset;
    sigemptyset( &sigset );
    sigaddset( &sigset, 14 );
    sigaddset( &sigset, 17 );
    if ( sigprocmask(0, &sigset, ((void *)0) ) < 0 ) {
        perror( "signals_disable: sigprocmask" );
        exit( 1 );
    }
}

variable_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
int signals_enable()
{
int result; // eax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v1);
sigaddset(&v1, 14);
sigaddset(&v1, 17);
result = sigprocmask(1, &v1, 0LL);
if ( result < 0 )
{
perror("signals_enable: sigprocmask");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v1);
sigaddset(&v1, 14);
sigaddset(&v1, 17);
result = sigprocmask(1, &v1, 0LL);
if ( result < 0 )
{
perror("signals_enable: sigprocmask");
exit(1);
}
return result;
}

original_code:
static void
signals_enable( void )
{
    sigset_t sigset;
    sigemptyset( &sigset );
    sigaddset( &sigset, 14 );
    sigaddset( &sigset, 17 );
    if ( sigprocmask(1, &sigset, ((void *)0) ) < 0 ) {
        perror( "signals_enable: sigprocmask" );
        exit( 1 );
    }
}

canonical_original_code:
static void signals_enable()
{
    struct __sigset_t sigset;
    sigemptyset( &sigset );
    sigaddset( &sigset, 14 );
    sigaddset( &sigset, 17 );
    if ( sigprocmask(1, &sigset, ((void *)0) ) < 0 ) {
        perror( "signals_enable: sigprocmask" );
        exit( 1 );
    }
}

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 sched_print_tasks()
{
puts("high priority");
print_q(high_proc, (unsigned int)high_tasks);
puts("low priority");
return print_q(low_proc, (unsigned int)low_tasks);
}

canonical_decompiled_code:
__int64 func7()
{
puts("high priority");
print_q(high_proc, (unsigned int)high_tasks);
puts("low priority");
return print_q(low_proc, (unsigned int)low_tasks);
}

original_code:
static void
sched_print_tasks( void )
{
    queue *buf;
    printf( "high priority\n" );
    buf = high_proc;
    print_q( buf, high_tasks );
    printf( "low priority\n" );
    buf = low_proc;
    print_q( buf, low_tasks );
}

canonical_original_code:
static void sched_print_tasks()
{
    struct __queue *buf;
    printf( "high priority\n" );
    buf = high_proc;
    print_q( buf, high_tasks );
    printf( "low priority\n" );
    buf = low_proc;
    print_q( buf, low_tasks );
}


decompiled_code:
int __fastcall sched_kill_task_by_id(unsigned int a1)
{
__pid_t *v1; // rax
__pid_t *v3; // rax

if ( a1 )
{
fprintf(stderr, "DIE DIE DIE!!!!! %d\n", a1);
v1 = (__pid_t *)find_q(a1, low_proc, (unsigned int)low_tasks);
if ( v1 )
return kill(*v1, 9);
v3 = (__pid_t *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( v3 )
return kill(*v3, 15);
}
else
{
fwrite("Cannot kill shell\n", 1uLL, 0x12uLL, stderr);
}
return -1;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1)
{
__pid_t *v1; // rax
__pid_t *v3; // rax

if ( a1 )
{
fprintf(stderr, "DIE DIE DIE!!!!! %d\n", a1);
v1 = (__pid_t *)find_q(a1, low_proc, (unsigned int)low_tasks);
if ( v1 )
return kill(*v1, 9);
v3 = (__pid_t *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( v3 )
return kill(*v3, 15);
}
else
{
fwrite("Cannot kill shell\n", 1uLL, 0x12uLL, stderr);
}
return -1;
}

original_code:
static int
sched_kill_task_by_id(int id)
{
    if ( id == 0 )
    {
        fprintf(stderr,"Cannot kill shell\n");
        return -1;
    }
    fprintf(stderr,"DIE DIE DIE!!!!! %d\n",id);
    queue *buf = find_q( id, low_proc, low_tasks );
    if ( buf != ((void *)0) )
    {
        return ( kill( buf->pid, 9 ) );
    }
    else
    {
        queue *buf = find_q( id, high_proc, high_tasks );
        if ( buf != ((void *)0) )
            return ( kill( buf->pid, 15 ) );
    }
    return -1;
}

canonical_original_code:
static int sched_kill_task_by_id(int id)
{
    if ( id == 0 )
    {
        fprintf(stderr,"Cannot kill shell\n");
        return -1;
    }
    fprintf(stderr,"DIE DIE DIE!!!!! %d\n",id);
    struct __queue *buf = find_q( id, low_proc, low_tasks );
    if ( buf != ((void *)0) )
    {
        return ( kill( buf->pid, 9 ) );
    }
    else
    {
        struct __queue *buf = find_q( id, high_proc, high_tasks );
        if ( buf != ((void *)0) )
            return ( kill( buf->pid, 15 ) );
    }
    return -1;
}

return_type: int


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

switch ( *(_DWORD *)a1 )
{
case 0:
sched_print_tasks();
result = 0;
break;
case 1:
result = sched_kill_task_by_id(*(_DWORD *)(a1 + 4));
break;
case 2:
sched_create_task((char *)(a1 + 8));
result = 0;
break;
case 3:
sched_high_task_by_id(*(_DWORD *)(a1 + 4));
result = 0;
break;
case 4:
sched_low_task_by_id(*(_DWORD *)(a1 + 4));
result = 0;
break;
default:
JUMPOUT(0xF93LL);
}
return result;
}

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

switch ( *(_DWORD *)a1 )
{
case 0:
func7();
result = 0;
break;
case 1:
result = func8(*(_DWORD *)(a1 + 4));
break;
case 2:
func2((char *)(a1 + 8));
result = 0;
break;
case 3:
func9(*(_DWORD *)(a1 + 4));
result = 0;
break;
case 4:
func10(*(_DWORD *)(a1 + 4));
result = 0;
break;
default:
JUMPOUT(0xF93LL);
}
return result;
}

original_code:
static int
process_request( struct request_struct *rq )
{
    switch ( rq->request_no ) {
        case REQ_PRINT_TASKS:
            sched_print_tasks();
            return 0;
        case REQ_KILL_TASK:
            return sched_kill_task_by_id(rq->task_arg);
        case REQ_EXEC_TASK:
            sched_create_task(rq->exec_task_arg);
            return 0;
        case REQ_HIGH_TASK:
            sched_high_task_by_id(rq->task_arg);
            return 0;
        case REQ_LOW_TASK:
            sched_low_task_by_id(rq->task_arg);
            return 0;
        default:
            return -38;
    }
}

canonical_original_code:
static int process_request(struct request_struct *rq)
{
    switch ( rq->request_no ) {
        case REQ_PRINT_TASKS:
            sched_print_tasks();
            return 0;
        case REQ_KILL_TASK:
            return sched_kill_task_by_id(rq->task_arg);
        case REQ_EXEC_TASK:
            sched_create_task(rq->exec_task_arg);
            return 0;
        case REQ_HIGH_TASK:
            sched_high_task_by_id(rq->task_arg);
            return 0;
        case REQ_LOW_TASK:
            sched_low_task_by_id(rq->task_arg);
            return 0;
        default:
            return -38;
    }
}

variable_types: struct request_struct { enum request_enum { REQ_PRINT_TASKS=0, REQ_KILL_TASK=1, REQ_EXEC_TASK=2, REQ_HIGH_TASK=3, REQ_LOW_TASK=4 } request_no; int task_arg; char exec_task_arg[60]; } *

user_defined_types: struct request_struct { enum request_enum request_no; int task_arg; char exec_task_arg[60]; }


decompiled_code:
int __fastcall shell_request_loop(int fd, int a2)
{
const char *v2; // rdi
int buf; // [rsp+Ch] [rbp-6Ch] BYREF
char v5[104]; // [rsp+10h] [rbp-68h] BYREF

do
{
if ( read(fd, v5, 0x44uLL) != 68 )
{
v2 = "scheduler: read from shell";
goto LABEL_5;
}
signals_disable();
buf = process_request((__int64)v5);
signals_enable();
}
while ( write(a2, &buf, 4uLL) == 4 );
v2 = "scheduler: write to shell";
LABEL_5:
perror(v2);
fwrite("Scheduler: giving up on shell request processing.\n", 1uLL, 0x32uLL, stderr);
return fflush(stderr);
}

canonical_decompiled_code:
int __fastcall func12(int fd, int a2)
{
const char *v2; // rdi
int buf; // [rsp+Ch] [rbp-6Ch] BYREF
char v5[104]; // [rsp+10h] [rbp-68h] BYREF

do
{
if ( read(fd, v5, 0x44uLL) != 68 )
{
v2 = "scheduler: read from shell";
goto LABEL_5;
}
func5();
buf = func11((__int64)v5);
func6();
}
while ( write(a2, &buf, 4uLL) == 4 );
v2 = "scheduler: write to shell";
LABEL_5:
perror(v2);
fwrite("Scheduler: giving up on shell request processing.\n", 1uLL, 0x32uLL, stderr);
return fflush(stderr);
}

original_code:
static void
shell_request_loop( int request_fd, int return_fd )
{
    int ret;
    struct request_struct rq;
    for ( ;; ) {
        if ( read( request_fd, &rq, sizeof( rq ) ) != sizeof( rq ) ) {
            perror( "scheduler: read from shell" );
            fprintf( stderr, "Scheduler: giving up on shell request processing.\n" );
            fflush( stderr );
            break;
        }
        signals_disable();
        ret = process_request( &rq );
        signals_enable();
        if ( write( return_fd, &ret, sizeof( ret ) ) != sizeof( ret ) ) {
            perror( "scheduler: write to shell" );
            fprintf( stderr, "Scheduler: giving up on shell request processing.\n" );
            fflush( stderr );
            break;
        }
    }
}

canonical_original_code:
static void shell_request_loop(int request_fd, int return_fd)
{
    int ret;
    struct request_struct rq;
    for ( ;; ) {
        if ( read( request_fd, &rq, sizeof( rq ) ) != sizeof( rq ) ) {
            perror( "scheduler: read from shell" );
            fprintf( stderr, "Scheduler: giving up on shell request processing.\n" );
            fflush( stderr );
            break;
        }
        signals_disable();
        ret = process_request( &rq );
        signals_enable();
        if ( write( return_fd, &ret, sizeof( ret ) ) != sizeof( ret ) ) {
            perror( "scheduler: write to shell" );
            fprintf( stderr, "Scheduler: giving up on shell request processing.\n" );
            fflush( stderr );
            break;
        }
    }
}

variable_types: ret int

variable_types: struct request_struct { enum request_enum { REQ_PRINT_TASKS=0, REQ_KILL_TASK=1, REQ_EXEC_TASK=2, REQ_HIGH_TASK=3, REQ_LOW_TASK=4 } request_no; int task_arg; char exec_task_arg[60]; }


main

canonical_name: main

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

low_tasks = 0;
super_id = 0;
current_tasks = (__int64)&low_tasks;
current_state = 0;
high_tasks = 0;
v3 = malloc(0x18uLL);
low_proc = (__int64)v3;
if ( !v3 || (v4 = (__int64)v3, (high_proc = (__int64)malloc(0x18uLL)) == 0) )
{
perror("main: init, bad alloc");
exit(1);
}
current_proc = v4;
init_q(v4, argv);
init_q(high_proc, argv);
sched_create_shell_constprop_0();
wait_for_ready_children(1LL);
install_signal_handlers();
fwrite("\t\thandlers installed, now running\n", 1uLL, 0x22uLL, stderr);
fflush(stderr);
v5 = low_proc;
kill(*(_DWORD *)low_proc, 18);
fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v5 + 4));
alarm(2u);
shell_request_loop(request_fd_2, return_fd_1);
while ( pause() )
;
fwrite("Internal error: Reached unreachable point\n", 1uLL, 0x2AuLL, stderr);
fflush(stderr);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
__int64 v4; // rbp
__int64 v5; // rbp

low_tasks = 0;
super_id = 0;
current_tasks = (__int64)&low_tasks;
current_state = 0;
high_tasks = 0;
v3 = malloc(0x18uLL);
low_proc = (__int64)v3;
if ( !v3 || (v4 = (__int64)v3, (high_proc = (__int64)malloc(0x18uLL)) == 0) )
{
perror("main: init, bad alloc");
exit(1);
}
current_proc = v4;
init_q(v4, argv);
init_q(high_proc, argv);
func14();
wait_for_ready_children(1LL);
func3();
fwrite("\t\thandlers installed, now running\n", 1uLL, 0x22uLL, stderr);
fflush(stderr);
v5 = low_proc;
kill(*(_DWORD *)low_proc, 18);
fprintf(stderr, "\t\tNEXT\t\tid:%d\n", *(unsigned int *)(v5 + 4));
alarm(2u);
func12(request_fd_2, return_fd_1);
while ( pause() )
;
fwrite("Internal error: Reached unreachable point\n", 1uLL, 0x2AuLL, stderr);
fflush(stderr);
return 1;
}

original_code:
int main(void)
{
    int nproc;
    static int request_fd, return_fd;
    low_tasks=0;
    super_id = 0;
    current_tasks = &low_tasks;
    current_state = 0;
    high_tasks=0;
    nproc = 1;
    low_proc = ( queue * ) malloc( sizeof( queue ) );
    if ( !low_proc )
    {
        perror( "main: init, bad alloc" );
        exit( 1 );
    }
    high_proc = ( queue * ) malloc( sizeof( queue ) );
    if ( !high_proc )
    {
        perror( "main: init, bad alloc" );
        exit( 1 );
    }
    current_proc = low_proc;
    init_q( low_proc );
    init_q( high_proc );
    sched_create_shell( "shell", &request_fd, &return_fd );
    wait_for_ready_children( nproc );
    install_signal_handlers();
    fprintf( stderr, "\t\thandlers installed, now running\n" );
    fflush( stderr );
    kill( low_proc->pid, 18 );
    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", low_proc -> id);
    alarm( 2 );
    shell_request_loop( request_fd, return_fd );
    while ( pause() )
        ;
    fprintf( stderr, "Internal error: Reached unreachable point\n" );
    fflush( stderr );
    return 1;
}

canonical_original_code:
int main()
{
    int nproc;
    static int request_fd; static int return_fd;
    low_tasks=0;
    super_id = 0;
    current_tasks = &low_tasks;
    current_state = 0;
    high_tasks=0;
    nproc = 1;
    low_proc = ( struct __queue * ) malloc( sizeof( queue ) );
    if ( !low_proc )
    {
        perror( "main: init, bad alloc" );
        exit( 1 );
    }
    high_proc = ( struct __queue * ) malloc( sizeof( queue ) );
    if ( !high_proc )
    {
        perror( "main: init, bad alloc" );
        exit( 1 );
    }
    current_proc = low_proc;
    init_q( low_proc );
    init_q( high_proc );
    sched_create_shell( "shell", &request_fd, &return_fd );
    wait_for_ready_children( nproc );
    install_signal_handlers();
    fprintf( stderr, "\t\thandlers installed, now running\n" );
    fflush( stderr );
    kill( low_proc->pid, 18 );
    fprintf( stderr, "\t\tNEXT\t\tid:%d\n", low_proc -> id);
    alarm( 2 );
    shell_request_loop( request_fd, return_fd );
    while ( pause() )
        ;
    fprintf( stderr, "Internal error: Reached unreachable point\n" );
    fflush( stderr );
    return 1;
}

variable_types: nproc int


decompiled_code:
__int64 __fastcall next_q(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
inline
queue *next_q( queue *q )
{
    return q->next;
}

canonical_original_code:
inline struct __queue *next_q(struct __queue *q)
{
    return q->next;
}

variable_types:
q
struct __queue *

return_type: struct __queue { int pid; int id; struct __queue *prev; struct __queue *next; } *


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

result = 0xFFFFFFFF00000000LL;
a1[1] = a1;
*a1 = 0xFFFFFFFF00000000LL;
a1[2] = a1;
return result;
}

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

result = 0xFFFFFFFF00000000LL;
a1[1] = a1;
*a1 = 0xFFFFFFFF00000000LL;
a1[2] = a1;
return result;
}

original_code:
void init_q( queue *head )
{
    head -> pid = 0;
    head -> id = -1;
    head -> prev = head;
    head -> next = head;
}

canonical_original_code:
void init_q(struct __queue *head)
{
    head -> pid = 0;
    head -> id = -1;
    head -> prev = head;
    head -> next = head;
}


decompiled_code:
void __fastcall print_q(unsigned int *a1, int a2)
{
int v3; // ebx

if ( a2 > 0 )
{
v3 = 0;
do
{
++v3;
fprintf(stdout, "sid:\t%d pid:\t%d prog\n", a1[1], *a1);
fflush(stdout);
a1 = (unsigned int *)next_q((__int64)a1);
}
while ( a2 != v3 );
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int *a1, int a2)
{
int v3; // ebx

if ( a2 > 0 )
{
v3 = 0;
do
{
++v3;
fprintf(stdout, "sid:\t%d pid:\t%d prog\n", a1[1], *a1);
fflush(stdout);
a1 = (unsigned int *)func0((__int64)a1);
}
while ( a2 != v3 );
}
}

original_code:
void print_q(queue *q,int len)
{
    int i;
    for ( i = 0 ; i < len ; ++i )
    {
        fprintf( stdout, "sid:\t%d pid:\t%d prog\n",q->id,q->pid);
        fflush( stdout );
        q=next_q(q);
    }
}

canonical_original_code:
void print_q(struct __queue *q, int len)
{
    int i;
    for ( i = 0 ; i < len ; ++i )
    {
        fprintf( stdout, "sid:\t%d pid:\t%d prog\n",q->id,q->pid);
        fflush( stdout );
        q=next_q(q);
    }
}

variable_types: len int

variable_types: i int


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

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

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

if ( a3 <= 0 )
return 0LL;
while ( *(_DWORD *)(a2 + 4) != a1 )
{
a2 = func0(a2);
if ( v6 == v5 + 1 )
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


decompiled_code:
__int64 __fastcall print_tree(const char *a1)
{
return _print_tree(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func13(const char *a1)
{
return func7(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; } *


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

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

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

v1 = 0LL;
for ( i = 10; x_chars > (int)v1; ++v1 )
{
v7 = *(_DWORD *)(a1 + 4 * v1);
if ( v7 == 255 )
{
v5 = 0LL;
v4 = 0LL;
v3 = 0LL;
}
else
{
v2 = v7 + 3;
v3 = (unsigned __int8)v7;
if ( v7 >= 0 )
v2 = *(_DWORD *)(a1 + 4 * v1);
v4 = (unsigned __int8)(v2 >> 2);
v5 = (unsigned __int8)(v7 / 2);
}
snprintf(s, 0x14uLL, "%d\t %d\t %d\t", v5, v4, v3);
v6 = strlen(s);
if ( (unsigned int)insist_write((unsigned int)image_fd, s, v6) == -1 )
{
perror("child:output_mandel:for");
exit(1);
}
}
result = insist_write((unsigned int)image_fd, &i, 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: iocheck int

variable_types:
rgb
unsigned char [3]

variable_types: nl char


decompiled_code:
void __fastcall compute_mandel_line(__int64 a1, int *a2)
{
double v2; // xmm2_8
double v4; // xmm0_8
int v5; // eax
double v6; // xmm0_8
double v7; // xmm0_8

v2 = *(double *)&xmin;
if ( *(double *)&xmax >= *(double *)&xmin )
{
do
{
v4 = mandel_iterations_at_point(256LL, v2);
v5 = 255;
v6 = v4 * 10.0;
if ( v6 <= 255.0 )
v5 = (int)v6;
v2 = v2 + *(double *)&xstep;
v7 = *(double *)&xmax;
*a2++ = v5;
}
while ( v7 >= v2 );
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int *a2)
{
double v2; // xmm2_8
double v4; // xmm0_8
int v5; // eax
double v6; // xmm0_8
double v7; // xmm0_8

v2 = *(double *)&xmin;
if ( *(double *)&xmax >= *(double *)&xmin )
{
do
{
v4 = mandel_iterations_at_point(256LL, v2);
v5 = 255;
v6 = v4 * 10.0;
if ( v6 <= 255.0 )
v5 = (int)v6;
v2 = v2 + *(double *)&xstep;
v7 = *(double *)&xmax;
*a2++ = v5;
}
while ( v7 >= v2 );
}
}

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)
{
int v3; // ebx
void *v4; // rsp
__int64 v6; // rdi
int v8[8]; // [rsp+0h] [rbp-20h] BYREF

v3 = a1 + start;
v4 = alloca(4LL * x_chars);
while ( end > v3 )
{
v6 = (unsigned int)v3;
v3 += 2;
compute_mandel_line(v6, v8);
pipesem_wait(a2);
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)
{
int v3; // ebx
void *v4; // rsp
__int64 v6; // rdi
int v8[8]; // [rsp+0h] [rbp-20h] BYREF

v3 = a1 + start;
v4 = alloca(4LL * x_chars);
while ( end > v3 )
{
v6 = (unsigned int)v3;
v3 += 2;
func0(v6, v8);
pipesem_wait(a2);
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

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int v4; // eax
__pid_t v5; // eax
size_t v7; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-DCh] BYREF
char v9[208]; // [rsp+18h] [rbp-D0h] BYREF

if ( argc == 3 )
{
_isoc99_sscanf(argv[1], "%d", &start);
_isoc99_sscanf(argv[2], "%d", &end);
v3 = argv[1];
}
else
{
v3 = "mandel.ppm";
start = 0;
end = y_chars;
}
image_fd = open(v3, 577, 438LL);
if ( !start )
{
snprintf(
(char *)&stat_loc.__iptr + 4,
0xC8uLL,
"P3\n%s\n%d\t %d\n%d\n",
"# mandelbrot set",
(unsigned int)x_chars,
(unsigned int)y_chars,
255LL);
v7 = strlen((const char *)&stat_loc.__iptr + 4);
if ( (unsigned int)insist_write((unsigned int)image_fd, (char *)&stat_loc.__iptr + 4, v7) == -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((char *)&stat_loc.__iptr + 4, 0LL);
pipesem_init(v9, 0LL);
v4 = fork();
if ( v4 < 0 )
goto LABEL_14;
if ( !v4 )
goto LABEL_13;
v5 = fork();
if ( v5 < 0 )
{
LABEL_14:
perror("parent: fork");
exit(1);
}
if ( !v5 )
{
v4 = 1;
LABEL_13:
child(
v4,
(__int64)&stat_loc.__iptr + 8 * v4 + 4,
(__int64)&stat_loc.__iptr + 8 * (((unsigned __int8)v4 + 1) & 1) + 4);
exit(1);
}
pipesem_signal((char *)&stat_loc.__iptr + 4);
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
int v4; // eax
__pid_t v5; // eax
size_t v7; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-DCh] BYREF
char v9[208]; // [rsp+18h] [rbp-D0h] BYREF

if ( argc == 3 )
{
_isoc99_sscanf(argv[1], "%d", &start);
_isoc99_sscanf(argv[2], "%d", &end);
v3 = argv[1];
}
else
{
v3 = "mandel.ppm";
start = 0;
end = y_chars;
}
image_fd = open(v3, 577, 438LL);
if ( !start )
{
snprintf(
(char *)&stat_loc.__iptr + 4,
0xC8uLL,
"P3\n%s\n%d\t %d\n%d\n",
"# mandelbrot set",
(unsigned int)x_chars,
(unsigned int)y_chars,
255LL);
v7 = strlen((const char *)&stat_loc.__iptr + 4);
if ( (unsigned int)insist_write((unsigned int)image_fd, (char *)&stat_loc.__iptr + 4, v7) == -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((char *)&stat_loc.__iptr + 4, 0LL);
pipesem_init(v9, 0LL);
v4 = fork();
if ( v4 < 0 )
goto LABEL_14;
if ( !v4 )
goto LABEL_13;
v5 = fork();
if ( v5 < 0 )
{
LABEL_14:
perror("parent: fork");
exit(1);
}
if ( !v5 )
{
v4 = 1;
LABEL_13:
func2(
v4,
(__int64)&stat_loc.__iptr + 8 * v4 + 4,
(__int64)&stat_loc.__iptr + 8 * (((unsigned __int8)v4 + 1) & 1) + 4);
exit(1);
}
pipesem_signal((char *)&stat_loc.__iptr + 4);
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]


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 func10()
{
while ( 1 )
sleep(0x64u);
}


canonical_decompiled_code:
__int64 __fastcall func11(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 func12(__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 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(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 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;
}


canonical_decompiled_code:
void *__fastcall func16(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 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;
}


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;
}


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


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);
}
}
}
}


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;
}


repo: shoban/udpreplay

decompiled_code:
int __fastcall udpreplay_dump(const char *a1, const char *a2, char *a3, unsigned int a4)
{
int v4; // ebp
int v7; // eax
int v8; // er15
struct addrinfo *v9; // r13
char *v10; // r14
size_t v11; // rbx
unsigned int v12; // eax
const char *v14; // rax
unsigned int v15; // [rsp+0h] [rbp-8Ch]
struct addrinfo *pai; // [rsp+1Ch] [rbp-70h] BYREF
struct addrinfo req; // [rsp+24h] [rbp-68h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = 2;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
v7 = getaddrinfo(a1, a2, &req, &pai);
v8 = v7;
if ( v7 )
{
v14 = gai_strerror(v7);
fprintf(stderr, "getaddrinfo: %s\n", v14);
}
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_12;
}
v15 = a4;
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;
++v8;
a3 = &v10[v11];
if ( a4 == v8 )
goto LABEL_9;
}
perror("sendto() error");
v15 = v8;
}
}
else
{
LABEL_12:
v4 = -1;
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
}
}
LABEL_9:
freeaddrinfo(pai);
v12 = atoi(a2);
fprintf(stdout, "%d pkts sent to %s:%d.\n", v15, a1, v12);
return close(v4);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1, const char *a2, char *a3, unsigned int a4)
{
int v4; // ebp
int v7; // eax
int v8; // er15
struct addrinfo *v9; // r13
char *v10; // r14
size_t v11; // rbx
unsigned int v12; // eax
const char *v14; // rax
unsigned int v15; // [rsp+0h] [rbp-8Ch]
struct addrinfo *pai; // [rsp+1Ch] [rbp-70h] BYREF
struct addrinfo req; // [rsp+24h] [rbp-68h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = 2;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
v7 = getaddrinfo(a1, a2, &req, &pai);
v8 = v7;
if ( v7 )
{
v14 = gai_strerror(v7);
fprintf(stderr, "getaddrinfo: %s\n", v14);
}
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_12;
}
v15 = a4;
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;
++v8;
a3 = &v10[v11];
if ( a4 == v8 )
goto LABEL_9;
}
perror("sendto() error");
v15 = v8;
}
}
else
{
LABEL_12:
v4 = -1;
fwrite("failed to bind socket\n", 1uLL, 0x16uLL, stderr);
}
}
LABEL_9:
freeaddrinfo(pai);
v12 = atoi(a2);
fprintf(stdout, "%d pkts sent to %s:%d.\n", v15, a1, v12);
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; }