ClearPoint

decompiled_code:
__int64 __fastcall ClearPoint(unsigned int a1, unsigned int a2)
{
int v2; // ebx
int v3; // ebp

if ( a1 > 0x2F || a2 > 0xF )
return 0xFFFFFFFFLL;
v2 = dword_770 + a1 + 1;
v3 = dword_774 + a2 + 1;
wattr_on(stdscr, 0x300uLL, 0LL);
if ( wmove(stdscr, v3, v2) != -1 )
waddch(stdscr, 0x20uLL);
wattr_off(stdscr, 0x300uLL, 0LL);
Curs_Return();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, unsigned int a2)
{
int v2; // ebx
int v3; // ebp

if ( a1 > 0x2F || a2 > 0xF )
return 0xFFFFFFFFLL;
v2 = dword_770 + a1 + 1;
v3 = dword_774 + a2 + 1;
wattr_on(stdscr, 0x300uLL, 0LL);
if ( wmove(stdscr, v3, v2) != -1 )
waddch(stdscr, 0x20uLL);
wattr_off(stdscr, 0x300uLL, 0LL);
func2();
return 0LL;
}


repo: shmily/linux

decompiled_code:
void Game_loop()
{
snake_Move((unsigned int)G_key);
wrefresh(stdscr);
}

canonical_decompiled_code:
void func0()
{
snake_Move((unsigned int)G_key);
wrefresh(stdscr);
}

original_code:
void Game_loop(int mod)
{
 snake_Move(G_key);
 wrefresh(stdscr);
}

variable_types: mod int

return_type: void


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

keypad(stdscr, 1);
result = wgetch(stdscr);
G_key = result;
return result;
}

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

keypad(stdscr, 1);
result = wgetch(stdscr);
G_key = result;
return result;
}

original_code:
void Key_loop(void)
{
 keypad(stdscr, 1);
 G_key = wgetch(stdscr);
}

canonical_original_code:
void Key_loop()
{
 keypad(stdscr, 1);
 G_key = wgetch(stdscr);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Init_Disp(argc, argv, envp);
CreatSnake();
signal(14, (__sighandler_t)Game_loop);
set_ticker(400LL);
while ( !Game_over )
Key_loop();
endwin();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Init_Disp(argc, argv, envp);
CreatSnake();
signal(14, (__sighandler_t)Game_loop);
set_ticker(400LL);
while ( !Game_over )
func1();
endwin();
return 0;
}

original_code:
int main(void)
{
 Init_Disp();
 CreatSnake();
 signal(14, Game_loop);
 set_ticker(400);
 while(Game_over==0){
  Key_loop();
 }
 endwin();
 return 0;
}

canonical_original_code:
int main()
{
 Init_Disp();
 CreatSnake();
 signal(14, Game_loop);
 set_ticker(400);
 while(Game_over==0){
  Key_loop();
 }
 endwin();
 return 0;
}

return_type: int


hanoi

decompiled_code:
int __fastcall hanoi(unsigned int a1, char a2, char a3, char a4)
{
unsigned int v4; // er13
unsigned int v5; // ebp

if ( a1 == 1 )
return printf("will move %d from %c to %c.\n", 1LL, (unsigned int)a2, (unsigned int)a4);
v4 = a3;
v5 = a4;
hanoi(a1 - 1, (unsigned int)a2, (unsigned int)a4, (unsigned int)a3);
printf("will move %d from %c to %c.\n", a1, (unsigned int)a2, v5);
return hanoi(a1 - 1, v4, (unsigned int)a2, v5);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, char a2, char a3, char a4)
{
unsigned int v4; // er13
unsigned int v5; // ebp

if ( a1 == 1 )
return printf("will move %d from %c to %c.\n", 1LL, (unsigned int)a2, (unsigned int)a4);
v4 = a3;
v5 = a4;
func7(a1 - 1, (unsigned int)a2, (unsigned int)a4, (unsigned int)a3);
printf("will move %d from %c to %c.\n", a1, (unsigned int)a2, v5);
return func7(a1 - 1, v4, (unsigned int)a2, v5);
}

original_code:
void hanoi( int n, char start, char middle, char end )
{
    if( n == 1 )
    {
        printf( "will move %d from %c to %c.\n", n, start, end );
    }
    else
    {
        hanoi( n - 1, start, end, middle );
        printf( "will move %d from %c to %c.\n", n, start, end );
        hanoi( n - 1, middle, start, end );
    }
}

canonical_original_code:
void hanoi(int n, char start, char middle, char end)
{
    if( n == 1 )
    {
        printf( "will move %d from %c to %c.\n", n, start, end );
    }
    else
    {
        hanoi( n - 1, start, end, middle );
        printf( "will move %d from %c to %c.\n", n, start, end );
        hanoi( n - 1, middle, start, end );
    }
}

variable_types: n int

variable_types: start char

variable_types: middle char

variable_types: end char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("input the number of plates;");
__isoc99_scanf();
hanoi(0, 65, 66, 67);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("input the number of plates;");
__isoc99_scanf();
func7(0, 65, 66, 67);
return 0;
}

original_code:
int main()
{
    int n = 0;
    printf( "input the number of plates;\n" );
    scanf( "%d", &n );
    hanoi( n, 'A', 'B', 'C' );
    return 0;
}


variable_types:
str
char *

variable_types: i int

variable_types: year int

variable_types: mon int

variable_types: day int

variable_types:
p
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
_BYTE v5[40]; // [rsp+0h] [rbp-28h] BYREF

gets(v5, argv, envp);
v3 = (unsigned int)check_id((__int64)v5);
printf("%d\n", v3);
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
_BYTE v5[40]; // [rsp+0h] [rbp-28h] BYREF

gets(v5, argv, envp);
v3 = (unsigned int)func0((__int64)v5);
printf("%d\n", v3);
return v3;
}

original_code:
int main(void)
{
 char str[32];
 int err;
 gets(str);
 err = check_id(str);
 printf("%d\n", err);
    return err;
}

canonical_original_code:
int main()
{
 char str[32];
 int err;
 gets(str);
 err = check_id(str);
 printf("%d\n", err);
    return err;
}

variable_types:
str
char [32]

variable_types: err int


GetByondAvgNumber

decompiled_code:
__int64 __fastcall GetByondAvgNumber(int *a1, int a2)
{
int *v2; // rcx
int *v3; // rdx
int v4; // eax
int v5; // eax
unsigned int v6; // edx

if ( a2 <= 0 )
return 0;
v2 = &a1[a2];
v3 = a1;
v4 = 0;
do
v4 += *v3++;
while ( v2 != v3 );
v5 = (a2 - 1 + v4) / a2;
v6 = 0;
do
v6 += *a1++ >= v5;
while ( v2 != a1 );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, int a2)
{
int *v2; // rcx
int *v3; // rdx
int v4; // eax
int v5; // eax
unsigned int v6; // edx

if ( a2 <= 0 )
return 0;
v2 = &a1[a2];
v3 = a1;
v4 = 0;
do
v4 += *v3++;
while ( v2 != v3 );
v5 = (a2 - 1 + v4) / a2;
v6 = 0;
do
v6 += *a1++ >= v5;
while ( v2 != a1 );
return v6;
}

original_code:
int GetByondAvgNumber(int iArray[], int iLen)
{
 int sum = 0;
 int average = 0;
 int count = 0;
 int i;
 int *p;
 p = iArray;
 i = iLen;
 while( i-- > 0 ){
  sum += *p++;
 }
 average = (sum + (iLen-1)) / iLen;
 p = iArray;
 i = iLen;
 while( i-- > 0){
  if(*p >= average) count++;
  p++;
 }
 return count;
}

variable_types:
iArray
int []

variable_types: iLen int

variable_types: sum int

variable_types: average int

variable_types: count int

variable_types:
p
int *


isPrime

decompiled_code:
__int64 __fastcall isPrime(int a1)
{
__int64 result; // rax
double v2; // xmm0_8
double v3; // xmm0_8
int v4; // esi
int v5; // ecx

result = 1LL;
if ( (unsigned int)(a1 - 2) > 1 )
{
result = 0LL;
if ( (a1 & 1) != 0 )
{
v2 = (double)a1;
if ( (double)a1 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
v4 = (int)(v3 + 1.0);
if ( v4 <= 3 )
{
result = 1LL;
}
else
{
result = (unsigned int)(a1 % 3);
if ( a1 % 3 )
{
v5 = 3;
while ( 1 )
{
v5 += 2;
if ( v4 <= v5 )
break;
result = (unsigned int)(a1 % v5);
if ( !(a1 % v5) )
return result;
}
result = 1LL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 result; // rax
double v2; // xmm0_8
double v3; // xmm0_8
int v4; // esi
int v5; // ecx

result = 1LL;
if ( (unsigned int)(a1 - 2) > 1 )
{
result = 0LL;
if ( (a1 & 1) != 0 )
{
v2 = (double)a1;
if ( (double)a1 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
v4 = (int)(v3 + 1.0);
if ( v4 <= 3 )
{
result = 1LL;
}
else
{
result = (unsigned int)(a1 % 3);
if ( a1 % 3 )
{
v5 = 3;
while ( 1 )
{
v5 += 2;
if ( v4 <= v5 )
break;
result = (unsigned int)(a1 % v5);
if ( !(a1 % v5) )
return result;
}
result = 1LL;
}
}
}
}
return result;
}

original_code:
int isPrime(int n)
{
 int i;
 int upper;
 if(n == 2|| n == 3)
  return 1;
 if(n % 2 ==0)
  return 0;
 upper = sqrt(n) + 1;
 for(i = 3;i < upper; i += 2){
  if(n %i == 0)
  return 0;
 }
 return 1;
}

variable_types: n int

variable_types: upper int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_scanf(&unk_D3, v5, envp);
v3 = isPrime(v5[0]);
printf("%d\n", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_scanf(&unk_D3, v5, envp);
v3 = func0(v5[0]);
printf("%d\n", v3);
return 0;
}

original_code:
int main(void)
{
    int num;
    scanf("%d", &num);
    printf("%d\n", isPrime(num));
    return 0;
}

canonical_original_code:
int main()
{
    int num;
    scanf("%d", &num);
    printf("%d\n", isPrime(num));
    return 0;
}

variable_types: num int


decompiled_code:
__int64 __fastcall init_mask(_QWORD *a1)
{
_QWORD *v1; // rsi
char **v2; // rcx
char *v3; // rdx
__int64 result; // rax

v1 = a1;
*a1 = 0LL;
*(_QWORD *)((char *)a1 + 252) = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 260) >> 3));
v2 = &mis;
do
{
v3 = *v2;
for ( result = (unsigned __int8)**v2; (_BYTE)result; result = (unsigned __int8)*v3 )
{
if ( (_BYTE)result != 32 )
*((_BYTE *)v1 + (char)result - 97) = 1;
++v3;
}
v1 = (_QWORD *)((char *)v1 + 26);
++v2;
}
while ( v2 != (char **)(unsigned int)((_DWORD)&mis + 80) );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
_QWORD *v1; // rsi
char **v2; // rcx
char *v3; // rdx
__int64 result; // rax

v1 = a1;
*a1 = 0LL;
*(_QWORD *)((char *)a1 + 252) = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 260) >> 3));
v2 = &mis;
do
{
v3 = *v2;
for ( result = (unsigned __int8)**v2; (_BYTE)result; result = (unsigned __int8)*v3 )
{
if ( (_BYTE)result != 32 )
*((_BYTE *)v1 + (char)result - 97) = 1;
++v3;
}
v1 = (_QWORD *)((char *)v1 + 26);
++v2;
}
while ( v2 != (char **)(unsigned int)((_DWORD)&mis + 80) );
return result;
}

original_code:
void init_mask(char *p)
{
 int i;
 char *pname;
 memset(p, 0, 26*10);
 for(i=0; i<10; i++){
  pname = mis[i];
  while(*pname!='\0'){
   if(*pname!=' ')
    *(p+(*pname - 'a')) = 1;
   pname++;
  }
  p = p + 26;
 }
}

variable_types:
pname
char *


original_code:
int main(void)
{
 int i;
    char mr[32];
 char mask[10][26];
 int index;
 char *p;
 int rap[10];
 int max;
 init_mask((char *)mask);
    scanf("%[^\n]", mr);
 memset(rap, 0, sizeof(rap));
 p = mr;
 while(*p!='\0'){
  if(*p!=' '){
   index = *p - 'a';
   for(i=0; i<10; i++){
    rap[i] = rap[i] + mask[i][index];
    mask[i][index] = 0;
   }
  }
  p++;
 }
 max = 0;
 index = 0;
 for(i=0; i<10; i++){
  if(rap[i]>max){
   max = rap[i];
   index = i;
  }
 }
 printf("%s\n", mis[index]);
    return 0;
}

canonical_original_code:
int main()
{
 int i;
    char mr[32];
 char mask[10][26];
 int index;
 char *p;
 int rap[10];
 int max;
 init_mask((char *)mask);
    scanf("%[^\n]", mr);
 memset(rap, 0, sizeof(rap));
 p = mr;
 while(*p!='\0'){
  if(*p!=' '){
   index = *p - 'a';
   for(i=0; i<10; i++){
    rap[i] = rap[i] + mask[i][index];
    mask[i][index] = 0;
   }
  }
  p++;
 }
 max = 0;
 index = 0;
 for(i=0; i<10; i++){
  if(rap[i]>max){
   max = rap[i];
   index = i;
  }
 }
 printf("%s\n", mis[index]);
    return 0;
}

variable_types: i int

variable_types:
mr
char [32]

variable_types:
mask
char [10][26]

variable_types: index int

variable_types:
rap
int [10]

variable_types: max int


inside

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

while ( a1 % 10 != 7 )
{
result = (unsigned int)(a1 / 10);
a1 = result;
if ( !(_DWORD)result )
return result;
}
return 1LL;
}

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

while ( a1 % 10 != 7 )
{
result = (unsigned int)(a1 / 10);
a1 = result;
if ( !(_DWORD)result )
return result;
}
return 1LL;
}

original_code:
int inside(int num)
{
    do{
        if( num%10 == 7 ) return 1;
        else num = num / 10;
    }while(num!=0);
    return 0;
}


main

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

_isoc99_scanf(&unk_DA, v7, envp);
v3 = v7[0];
if ( v7[0] <= 6 )
{
v4 = 0;
}
else
{
v4 = 0;
v5 = 7;
do
{
if ( v5 == 7 * (v5 / 7) || (unsigned int)inside(v5) == 1 )
++v4;
++v5;
}
while ( v5 <= v3 );
}
printf("%d\n", v4);
return 0;
}

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

_isoc99_scanf(&unk_DA, v7, envp);
v3 = v7[0];
if ( v7[0] <= 6 )
{
v4 = 0;
}
else
{
v4 = 0;
v5 = 7;
do
{
if ( v5 == 7 * (v5 / 7) || (unsigned int)func0(v5) == 1 )
++v4;
++v5;
}
while ( v5 <= v3 );
}
printf("%d\n", v4);
return 0;
}

original_code:
int main(void)
{
    int limit;
    int i;
    int count = 0;
    scanf("%d", &limit);
    for(i=7; i<=limit; i++){
        if( ((i%7)==0) || (inside(i)==1) ){
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

canonical_original_code:
int main()
{
    int limit;
    int i;
    int count = 0;
    scanf("%d", &limit);
    for(i=7; i<=limit; i++){
        if( ((i%7)==0) || (inside(i)==1) ){
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

variable_types: limit int


search

original_code:
int search(int *p, int len, int target)
{
    int low = -1;
    int high = len;
    int probe;
    while(high-low > 1){
        probe = (low + high)>>1;
        if(*(p+probe)>target){
            high = probe;
        }else{
            low = probe;
        }
    }
    if((low==-1)||(*(p+low)!=target))
        return -1;
    else
        return low;
}

variable_types: len int

variable_types: target int

variable_types: low int

variable_types: high int

variable_types: probe int


main

original_code:
int main(void)
{
    int i;
    int count;
    int *p, *pboy, *pgirl;
    int *_p, *_pboy, *_pgirl;
    int position_b, _count;
 int *pos_b, *pos_g, *_pos_b;
 int result;
 while(1){
     scanf("%d", &count);
     if(count<=0) break;
     count = count*2;
     p = (int *)malloc(sizeof(int)*count);
     pboy = (int *)malloc(sizeof(int)*count);
     pgirl = (int *)malloc(sizeof(int)*count);
  pos_b = (int *)malloc(sizeof(int)*count);
     _p = p;
     _pboy = pboy;
     _pgirl = pgirl;
  _pos_b = pos_b;
     i = 0;
     while(i<count){
         scanf("%d", p+i);
         i++;
     }
     *pboy++ = *p;
     pgirl = pgirl+count-1;
     *pgirl-- = *(p+count-1);
     for(i=1; i<count; i++){
         *pboy = *(pboy-1) + *(p+i);
         *pgirl = *(pgirl+1) + *(p+count-1-i);
         pboy++;
         pgirl--;
     }
  pos_g = _p;
  memset(pos_g, 0, count);
  memset(pos_b, 0, count);
  result = 1;
  *pos_g++ = 1;
  *pos_b++ = count;
     _count = count;
     count = count-1;
     for(i=1; i<_count; i++){
         position_b = search(_pboy, count, *(_pgirl+i));
         if(position_b!=-1){
    *pos_g++ = i+1;
    *pos_b++ = position_b+1;
    result++;
             count = position_b;
         }
     }
  pos_g = _p;
  pos_b = _pos_b;
  printf("共有%d种站法\n", result);
  for(i=0; i<result; i++){
   printf("第%d种站法:女方站位置%d,男方站位置%d\n", i+1, *pos_g++, *pos_b++);
  }
     free(_p);
     free(_pboy);
     free(_pgirl);
  free(_pos_b);
 }
    return 0;
}

canonical_original_code:
int main()
{
    int i;
    int count;
    int *p; int *pboy; int *pgirl;
    int *_p; int *_pboy; int *_pgirl;
    int position_b; int _count;
 int *pos_b; int *pos_g; int *_pos_b;
 int result;
 while(1){
     scanf("%d", &count);
     if(count<=0) break;
     count = count*2;
     p = (int *)malloc(sizeof(int)*count);
     pboy = (int *)malloc(sizeof(int)*count);
     pgirl = (int *)malloc(sizeof(int)*count);
  pos_b = (int *)malloc(sizeof(int)*count);
     _p = p;
     _pboy = pboy;
     _pgirl = pgirl;
  _pos_b = pos_b;
     i = 0;
     while(i<count){
         scanf("%d", p+i);
         i++;
     }
     *pboy++ = *p;
     pgirl = pgirl+count-1;
     *pgirl-- = *(p+count-1);
     for(i=1; i<count; i++){
         *pboy = *(pboy-1) + *(p+i);
         *pgirl = *(pgirl+1) + *(p+count-1-i);
         pboy++;
         pgirl--;
     }
  pos_g = _p;
  memset(pos_g, 0, count);
  memset(pos_b, 0, count);
  result = 1;
  *pos_g++ = 1;
  *pos_b++ = count;
     _count = count;
     count = count-1;
     for(i=1; i<_count; i++){
         position_b = search(_pboy, count, *(_pgirl+i));
         if(position_b!=-1){
    *pos_g++ = i+1;
    *pos_b++ = position_b+1;
    result++;
             count = position_b;
         }
     }
  pos_g = _p;
  pos_b = _pos_b;
  printf("共有%d种站法\n", result);
  for(i=0; i<result; i++){
   printf("第%d种站法:女方站位置%d,男方站位置%d\n", i+1, *pos_g++, *pos_b++);
  }
     free(_p);
     free(_pboy);
     free(_pgirl);
  free(_pos_b);
 }
    return 0;
}

variable_types: count int

variable_types:
p
int *

variable_types:
pboy
int *

variable_types:
pgirl
int *

variable_types: result int


Parser

decompiled_code:
__int64 __fastcall Parser(_BYTE *a1)
{
_BYTE *v1; // rdx
int *v2; // rsi
char v3; // al
int *v4; // rcx
__int64 result; // rax
int v6; // edx
int *v7; // rcx
int *v8; // rsi
int v9; // edi
int v10[32]; // [rsp+0h] [rbp-100h] BYREF
int v11[32]; // [rsp+80h] [rbp-80h] BYREF

v1 = a1;
memset(v11, 0, 0x78uLL);
v2 = v10;
memset(v10, 0, 0x78uLL);
v3 = *a1;
if ( *a1 )
{
v4 = v11;
do
{
if ( v3 > 47 )
{
*v4 = v3 + 10 * *v4 - 48;
}
else
{
*v2++ = v3;
++v4;
}
v3 = *++v1;
}
while ( *v1 );
}
result = (unsigned int)v11[0];
v6 = v10[0];
if ( v10[0] != 48 )
{
v7 = &v11[2];
v8 = v10;
do
{
if ( v6 == 45 )
{
result = (unsigned int)(result - *(v7 - 1));
}
else if ( v6 > 45 )
{
if ( v6 != 47 )
return result;
v9 = *(v7 - 1);
if ( !v9 )
return result;
result = (unsigned int)((int)result / v9);
}
else if ( v6 == 42 )
{
result = (unsigned int)(*(v7 - 1) * result);
}
else
{
if ( v6 != 43 )
return result;
result = (unsigned int)(*(v7 - 1) + result);
}
v6 = *++v8;
++v7;
}
while ( *v8 != 48 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
_BYTE *v1; // rdx
int *v2; // rsi
char v3; // al
int *v4; // rcx
__int64 result; // rax
int v6; // edx
int *v7; // rcx
int *v8; // rsi
int v9; // edi
int v10[32]; // [rsp+0h] [rbp-100h] BYREF
int v11[32]; // [rsp+80h] [rbp-80h] BYREF

v1 = a1;
memset(v11, 0, 0x78uLL);
v2 = v10;
memset(v10, 0, 0x78uLL);
v3 = *a1;
if ( *a1 )
{
v4 = v11;
do
{
if ( v3 > 47 )
{
*v4 = v3 + 10 * *v4 - 48;
}
else
{
*v2++ = v3;
++v4;
}
v3 = *++v1;
}
while ( *v1 );
}
result = (unsigned int)v11[0];
v6 = v10[0];
if ( v10[0] != 48 )
{
v7 = &v11[2];
v8 = v10;
do
{
if ( v6 == 45 )
{
result = (unsigned int)(result - *(v7 - 1));
}
else if ( v6 > 45 )
{
if ( v6 != 47 )
return result;
v9 = *(v7 - 1);
if ( !v9 )
return result;
result = (unsigned int)((int)result / v9);
}
else if ( v6 == 42 )
{
result = (unsigned int)(*(v7 - 1) * result);
}
else
{
if ( v6 != 43 )
return result;
result = (unsigned int)(*(v7 - 1) + result);
}
v6 = *++v8;
++v7;
}
while ( *v8 != 48 );
}
return result;
}

original_code:
int Parser(const char *pstr)
{
 int num[30];
 int op[30];
 int sum;
 int *p_num;
 int *p_op;
 memset(num, 0, sizeof(num));
 memset(op, 0, sizeof(op));
 p_num = num;
 p_op = op;
 while(*pstr!='\0'){
  if(*pstr<'0'){
   *p_op = *pstr;
   p_op++;
   p_num++;
  }else{
   *p_num = (*p_num * 10) + (*pstr - '0');
  }
  pstr++;
 }
 p_num = num;
 p_op = op;
 sum = *p_num++;
 while(*p_op != '0'){
  switch(*p_op){
   case '+':
    sum = sum + (*p_num++);
    break;
   case '-':
    sum = sum - (*p_num++);
    break;
   case '*':
    sum = sum * (*p_num++);
    break;
   case '/':
    if(*p_num!=0){
     sum = sum / (*p_num++);
    }else{
     goto _return;
    }
    break;
   default:
    goto _return;
    break;
  }
  p_op++;
 }
_return:
 return sum;
}

canonical_original_code:
int Parser(char *pstr)
{
 int num[30];
 int op[30];
 int sum;
 int *p_num;
 int *p_op;
 memset(num, 0, sizeof(num));
 memset(op, 0, sizeof(op));
 p_num = num;
 p_op = op;
 while(*pstr!='\0'){
  if(*pstr<'0'){
   *p_op = *pstr;
   p_op++;
   p_num++;
  }else{
   *p_num = (*p_num * 10) + (*pstr - '0');
  }
  pstr++;
 }
 p_num = num;
 p_op = op;
 sum = *p_num++;
 while(*p_op != '0'){
  switch(*p_op){
   case '+':
    sum = sum + (*p_num++);
    break;
   case '-':
    sum = sum - (*p_num++);
    break;
   case '*':
    sum = sum * (*p_num++);
    break;
   case '/':
    if(*p_num!=0){
     sum = sum / (*p_num++);
    }else{
     goto _return;
    }
    break;
   default:
    goto _return;
    break;
  }
  p_op++;
 }
_return:
 return sum;
}

variable_types:
pstr
char *

variable_types:
num
int [30]

variable_types:
op
int [30]

variable_types: sum int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
_BYTE v5[264]; // [rsp+0h] [rbp-108h] BYREF

memset(v5, 0, 0x100uLL);
_isoc99_scanf(&unk_11A, v5, envp);
v3 = Parser(v5);
printf("%d", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
_BYTE v5[264]; // [rsp+0h] [rbp-108h] BYREF

memset(v5, 0, 0x100uLL);
_isoc99_scanf(&unk_11A, v5, envp);
v3 = func0(v5);
printf("%d", v3);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 char str[256];
 int res;
 memset(str, 0, sizeof(str));
 scanf("%s", str);
 res = Parser(str);
 printf("%d", res);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
str
char [256]

variable_types: res int


decompiled_code:
int __fastcall set_ticker(int a1)
{
struct itimerval v2; // [rsp+0h] [rbp-28h] BYREF

v2.it_interval.tv_sec = a1 / 1000;
v2.it_interval.tv_usec = 1000LL * (a1 % 1000);
v2.it_value = v2.it_interval;
return setitimer(ITIMER_REAL, &v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
struct itimerval v2; // [rsp+0h] [rbp-28h] BYREF

v2.it_interval.tv_sec = a1 / 1000;
v2.it_interval.tv_usec = 1000LL * (a1 % 1000);
v2.it_value = v2.it_interval;
return setitimer(ITIMER_REAL, &v2, 0LL);
}

original_code:
int set_ticker(int n_msecs)
{
 struct itimerval timer;
 long n_sec, n_usec;
 n_sec = n_msecs / 1000;
 n_usec = (n_msecs % 1000)*1000L;
 timer.it_interval.tv_sec = n_sec;
 timer.it_interval.tv_usec = n_usec;
 timer.it_value.tv_sec = n_sec;
 timer.it_value.tv_usec = n_usec;
 return setitimer(ITIMER_REAL, &timer, ((void *)0));
}

canonical_original_code:
int set_ticker(int n_msecs)
{
 struct itimerval timer;
 long n_sec; long n_usec;
 n_sec = n_msecs / 1000;
 n_usec = (n_msecs % 1000)*1000L;
 timer.it_interval.tv_sec = n_sec;
 timer.it_interval.tv_usec = n_usec;
 timer.it_value.tv_sec = n_sec;
 timer.it_value.tv_usec = n_usec;
 return setitimer(ITIMER_REAL, &timer, ((void *)0));
}

variable_types: struct itimerval { struct timeval { long tv_sec; long tv_usec; } it_interval; struct timeval it_value; }

user_defined_types: struct itimerval { struct timeval it_interval; struct timeval it_value; }


isValidPassword

decompiled_code:
__int64 __fastcall isValidPassword(const char *a1)
{
const char *v1; // rbx
char v2; // al
int *v3; // rax
int v4; // edx

v1 = a1;
if ( (unsigned int)strlen(a1) - 8 > 0xC )
return 0xFFFFFFFFLL;
count[0] = 0;
count[1] = 0;
count[2] = 0;
count[3] = 0;
count[4] = 0;
v2 = *a1;
if ( *a1 )
{
do
{
count[mask[v2]] = 1;
v2 = *++v1;
}
while ( *v1 );
if ( count[0] > 0 )
return 0xFFFFFFFFLL;
}
v3 = count;
v4 = 0;
do
{
v4 += v3[1] > 0;
++v3;
}
while ( v3 != (int *)(unsigned int)((_DWORD)count + 16) );
return (unsigned int)-(v4 <= 2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
const char *v1; // rbx
char v2; // al
int *v3; // rax
int v4; // edx

v1 = a1;
if ( (unsigned int)strlen(a1) - 8 > 0xC )
return 0xFFFFFFFFLL;
count[0] = 0;
count[1] = 0;
count[2] = 0;
count[3] = 0;
count[4] = 0;
v2 = *a1;
if ( *a1 )
{
do
{
count[mask[v2]] = 1;
v2 = *++v1;
}
while ( *v1 );
if ( count[0] > 0 )
return 0xFFFFFFFFLL;
}
v3 = count;
v4 = 0;
do
{
v4 += v3[1] > 0;
++v3;
}
while ( v3 != (int *)(unsigned int)((_DWORD)count + 16) );
return (unsigned int)-(v4 <= 2);
}

original_code:
int isValidPassword(char *p)
{
 int i, len, typeCount;
 len = strlen(p);
 if( len<8||len>20 ) return -1;
 _count[0] = 0;
 _count[1] = 0;
 _count[2] = 0;
 _count[3] = 0;
 _count[4] = 0;
 while(*p!='\0'){
  i = (int)*p;
  _count[_mask[i]] = 1;
  p++;
 }
 if(_count[0]>0) return -1;
 typeCount = 0;
 for(i=1; i<5; i++){
  if(_count[i]>0) typeCount++;
 }
 if(typeCount<3) return -1;
 return 0;
}

canonical_original_code:
int isValidPassword(char *p)
{
 int i; int len; int typeCount;
 len = strlen(p);
 if( len<8||len>20 ) return -1;
 _count[0] = 0;
 _count[1] = 0;
 _count[2] = 0;
 _count[3] = 0;
 _count[4] = 0;
 while(*p!='\0'){
  i = (int)*p;
  _count[_mask[i]] = 1;
  p++;
 }
 if(_count[0]>0) return -1;
 typeCount = 0;
 for(i=1; i<5; i++){
  if(_count[i]>0) typeCount++;
 }
 if(typeCount<3) return -1;
 return 0;
}

variable_types:
p
char *

variable_types: i int

variable_types: len int

variable_types: typeCount int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
char v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

while ( 1 )
{
v3 = gets(v5, argv, envp);
if ( !v3 )
break;
if ( (unsigned int)isValidPassword(v5) )
puts("false");
else
puts("true");
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
char v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

while ( 1 )
{
v3 = gets(v5, argv, envp);
if ( !v3 )
break;
if ( (unsigned int)func0(v5) )
puts("false");
else
puts("true");
}
return v3;
}

original_code:
int main(void)
{
 char str[1000];
 int ret;
 while(gets(str)){
  ret = isValidPassword(str);
  if(ret==0) printf("true\n");
  else printf("false\n");
 }
    return 0;
}

canonical_original_code:
int main()
{
 char str[1000];
 int ret;
 while(gets(str)){
  ret = isValidPassword(str);
  if(ret==0) printf("true\n");
  else printf("false\n");
 }
    return 0;
}

variable_types:
str
char [1000]

variable_types: ret int


repo: shmmy/OS

decompiled_code:
void __noreturn sigint_handler()
{
reset_xterm_color(1LL);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
reset_xterm_color(1LL);
exit(1);
}

original_code:
static void
sigint_handler(int signum)
{
    reset_xterm_color(1);
    exit(1);
}

variable_types: signum int


decompiled_code:
int install_signal_handler()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
v1.sa_flags = 0x10000000;
sigemptyset(&set);
sigaddset(&set, 2);
*(__m128i *)v1.sa_mask.__val = _mm_loadu_si128((const __m128i *)&set);
*(__m128i *)&v1.sa_mask.__val[2] = _mm_loadu_si128((const __m128i *)&set.__val[2]);
*(__m128i *)&v1.sa_mask.__val[4] = _mm_loadu_si128((const __m128i *)&set.__val[4]);
*(__m128i *)&v1.sa_mask.__val[6] = _mm_loadu_si128((const __m128i *)&set.__val[6]);
*(__m128i *)&v1.sa_mask.__val[8] = _mm_loadu_si128((const __m128i *)&set.__val[8]);
*(__m128i *)&v1.sa_mask.__val[10] = _mm_loadu_si128((const __m128i *)&set.__val[10]);
*(__m128i *)&v1.sa_mask.__val[12] = _mm_loadu_si128((const __m128i *)&set.__val[12]);
*(__m128i *)&v1.sa_mask.__val[14] = _mm_loadu_si128((const __m128i *)&set.__val[14]);
result = sigaction(2, &v1, 0LL);
if ( result < 0 )
{
perror("sigaction: sigint");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
v1.sa_flags = 0x10000000;
sigemptyset(&set);
sigaddset(&set, 2);
*(__m128i *)v1.sa_mask.__val = _mm_loadu_si128((const __m128i *)&set);
*(__m128i *)&v1.sa_mask.__val[2] = _mm_loadu_si128((const __m128i *)&set.__val[2]);
*(__m128i *)&v1.sa_mask.__val[4] = _mm_loadu_si128((const __m128i *)&set.__val[4]);
*(__m128i *)&v1.sa_mask.__val[6] = _mm_loadu_si128((const __m128i *)&set.__val[6]);
*(__m128i *)&v1.sa_mask.__val[8] = _mm_loadu_si128((const __m128i *)&set.__val[8]);
*(__m128i *)&v1.sa_mask.__val[10] = _mm_loadu_si128((const __m128i *)&set.__val[10]);
*(__m128i *)&v1.sa_mask.__val[12] = _mm_loadu_si128((const __m128i *)&set.__val[12]);
*(__m128i *)&v1.sa_mask.__val[14] = _mm_loadu_si128((const __m128i *)&set.__val[14]);
result = sigaction(2, &v1, 0LL);
if ( result < 0 )
{
perror("sigaction: sigint");
exit(1);
}
return result;
}

original_code:
static void
install_signal_handler(void)
{
    sigset_t sigset;
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = sigint_handler;
    sa.sa_flags = 0x10000000 ;
    sigemptyset(&sigset);
    sigaddset(&sigset, 2);
    sa.sa_mask = sigset;
    if (sigaction(2, &sa, ((void *)0)) < 0) {
        perror("sigaction: sigint");
        exit(1);
    }
}

canonical_original_code:
static void install_signal_handler()
{
    struct __sigset_t sigset;
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = sigint_handler;
    sa.sa_flags = 0x10000000 ;
    sigemptyset(&sigset);
    sigaddset(&sigset, 2);
    sa.sa_mask = sigset;
    if (sigaction(2, &sa, ((void *)0)) < 0) {
        perror("sigaction: sigint");
        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:
double __fastcall compute_mandel_line(int a1, _DWORD *a2)
{
double v2; // r14
double result; // xmm0_8
int v5; // eax
double v6; // [rsp+8h] [rbp-20h]

v2 = *(double *)&ymax - (double)a1 * *(double *)&ystep;
v6 = *(double *)&xmin;
for ( result = *(double *)&xmax; *(double *)&xmax >= v6; result = *(double *)&xmax )
{
v5 = mandel_iterations_at_point(100000LL, v6, v2);
if ( v5 > 255 )
v5 = 255;
*a2 = (unsigned __int8)xterm_color((unsigned int)v5);
v6 = v6 + *(double *)&xstep;
++a2;
}
return result;
}

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

v2 = *(double *)&ymax - (double)a1 * *(double *)&ystep;
v6 = *(double *)&xmin;
for ( result = *(double *)&xmax; *(double *)&xmax >= v6; result = *(double *)&xmax )
{
v5 = mandel_iterations_at_point(100000LL, v6, v2);
if ( v5 > 255 )
v5 = 255;
*a2 = (unsigned __int8)xterm_color((unsigned int)v5);
v6 = v6 + *(double *)&xstep;
++a2;
}
return result;
}

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

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

variable_types: line int

variable_types: x double

variable_types: y double

variable_types: n int

variable_types: val int


decompiled_code:
ssize_t __fastcall output_mandel_line(int fd, __int64 a2)
{
__int64 v2; // rbx
ssize_t result; // rax
char v4; // [rsp+Eh] [rbp-1Ah] BYREF
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

buf[0] = 64;
v4 = 10;
if ( x_chars > 0 )
{
v2 = 0LL;
do
{
set_xterm_color((unsigned int)fd, *(unsigned __int8 *)(a2 + 4 * v2));
if ( write(fd, buf, 1uLL) != 1 )
{
perror("compute_and_output_mandel_line: write point");
exit(1);
}
++v2;
}
while ( x_chars > (int)v2 );
}
result = write(fd, &v4, 1uLL);
if ( result != 1 )
{
perror("compute_and_output_mandel_line: write newline");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int fd, __int64 a2)
{
__int64 v2; // rbx
ssize_t result; // rax
char v4; // [rsp+Eh] [rbp-1Ah] BYREF
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

buf[0] = 64;
v4 = 10;
if ( x_chars > 0 )
{
v2 = 0LL;
do
{
set_xterm_color((unsigned int)fd, *(unsigned __int8 *)(a2 + 4 * v2));
if ( write(fd, buf, 1uLL) != 1 )
{
perror("compute_and_output_mandel_line: write point");
exit(1);
}
++v2;
}
while ( x_chars > (int)v2 );
}
result = write(fd, &v4, 1uLL);
if ( result != 1 )
{
perror("compute_and_output_mandel_line: write newline");
exit(1);
}
return result;
}

original_code:
void output_mandel_line(int fd, int color_val[])
{
    int i;
    char point ='@';
    char newline='\n';
    for (i = 0; i < x_chars; i++) {
        set_xterm_color(fd, color_val[i]);
        if (write(fd, &point, 1) != 1) {
            perror("compute_and_output_mandel_line: write point");
            exit(1);
        }
    }
    if (write(fd, &newline, 1) != 1) {
        perror("compute_and_output_mandel_line: write newline");
        exit(1);
    }
}

canonical_original_code:
void output_mandel_line(int fd, int color_val[])
{
    int i;
    char point = '@';
    char newline = '\n';
    for (i = 0; i < x_chars; i++) {
        set_xterm_color(fd, color_val[i]);
        if (write(fd, &point, 1) != 1) {
            perror("compute_and_output_mandel_line: write point");
            exit(1);
        }
    }
    if (write(fd, &newline, 1) != 1) {
        perror("compute_and_output_mandel_line: write newline");
        exit(1);
    }
}

variable_types: fd int

variable_types: point char

variable_types: newline char


decompiled_code:
ssize_t __fastcall compute_and_output_mandel_line(int fd, int a2)
{
void *v2; // rsp
_DWORD v4[4]; // [rsp+0h] [rbp-10h] BYREF

v2 = alloca(4LL * x_chars);
compute_mandel_line(a2, v4);
return output_mandel_line(fd, (__int64)v4);
}

canonical_decompiled_code:
ssize_t __fastcall func4(int fd, int a2)
{
void *v2; // rsp
_DWORD v4[4]; // [rsp+0h] [rbp-10h] BYREF

v2 = alloca(4LL * x_chars);
func2(a2, v4);
return func3(fd, (__int64)v4);
}

original_code:
void compute_and_output_mandel_line(int fd, int line)
{
    int color_val[x_chars];
    compute_mandel_line(line, color_val);
    output_mandel_line(fd, color_val);
}

canonical_original_code:
void compute_and_output_mandel_line(int fd, int line)
{
    int color_val[-1];
    compute_mandel_line(line, color_val);
    output_mandel_line(fd, color_val);
}


child

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

v3 = a1;
for ( i = alloca(4LL * x_chars); y_chars > v3; v3 += 30 )
{
v6 = compute_mandel_line(v3, v8);
pipesem_wait(a2, v6);
output_mandel_line(1, (__int64)v8);
pipesem_signal(a3);
}
return pipesem_destroy(a2);
}

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

v3 = a1;
for ( i = alloca(4LL * x_chars); y_chars > v3; v3 += 30 )
{
v6 = func2(v3, v8);
pipesem_wait(a2, v6);
func3(1, (__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 = ch_id; line < y_chars; line+=30) {
        compute_mandel_line(line, color_val);
        pipesem_wait(mysem);
        output_mandel_line(1, 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 = ch_id; line < y_chars; line+=30) {
        compute_mandel_line(line, color_val);
        pipesem_wait(mysem);
        output_mandel_line(1, 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)
{
char *v3; // rbx
int i; // ebx
__pid_t v5; // eax
_QWORD v7[30]; // [rsp+0h] [rbp-118h] BYREF
char v8; // [rsp+F0h] [rbp-28h] BYREF
__WAIT_STATUS stat_loc; // [rsp+FCh] [rbp-1Ch] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int i; // ebx
__pid_t v5; // eax
_QWORD v7[30]; // [rsp+0h] [rbp-118h] BYREF
char v8; // [rsp+F0h] [rbp-28h] BYREF
__WAIT_STATUS stat_loc; // [rsp+FCh] [rbp-1Ch] BYREF

*(double *)&xstep = (*(double *)&xmax - *(double *)&xmin) / (double)x_chars;
*(double *)&ystep = (*(double *)&ymax - *(double *)&ymin) / (double)y_chars;
func1();
v3 = (char *)v7;
do
{
pipesem_init(v3, 0LL);
v3 += 8;
}
while ( v3 != &v8 );
for ( i = 0; i != 30; ++i )
{
v5 = fork();
if ( v5 < 0 )
{
perror("parent: fork");
exit(1);
}
if ( !v5 )
{
func5(i, (__int64)&v7[i], (__int64)&v7[(i + 1) % 30]);
exit(1);
}
}
pipesem_signal(v7);
do
{
wait((__WAIT_STATUS)&stat_loc);
--i;
}
while ( i );
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: i int

variable_types: p int

variable_types: status int

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


decompiled_code:
__int64 __fastcall xterm2rgb(int a1, char *a2)
{
char *v2; // rax
__int64 result; // rax
unsigned __int8 v4; // cl
char v5; // di

if ( (unsigned __int8)a1 > 0xFu )
{
v4 = a1 - 16;
if ( (unsigned __int8)(a1 - 16) > 0xD8u )
{
result = (unsigned int)(a1 + 23);
if ( (unsigned __int8)(a1 + 23) <= 0x14u )
{
v5 = 10 * a1 - 8;
a2[2] = v5;
a2[1] = v5;
*a2 = v5;
}
}
else
{
*a2 = valuerange[v4 / 0x24u % 6];
a2[1] = valuerange[v4 / 6u % 6];
result = (unsigned __int8)valuerange[v4 % 6u];
a2[2] = result;
}
}
else
{
v2 = &basic16[3 * (unsigned __int8)a1];
*a2 = *v2;
a2[1] = v2[1];
result = (unsigned __int8)v2[2];
a2[2] = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, char *a2)
{
char *v2; // rax
__int64 result; // rax
unsigned __int8 v4; // cl
char v5; // di

if ( (unsigned __int8)a1 > 0xFu )
{
v4 = a1 - 16;
if ( (unsigned __int8)(a1 - 16) > 0xD8u )
{
result = (unsigned int)(a1 + 23);
if ( (unsigned __int8)(a1 + 23) <= 0x14u )
{
v5 = 10 * a1 - 8;
a2[2] = v5;
a2[1] = v5;
*a2 = v5;
}
}
else
{
*a2 = valuerange[v4 / 0x24u % 6];
a2[1] = valuerange[v4 / 6u % 6];
result = (unsigned __int8)valuerange[v4 % 6u];
a2[2] = result;
}
}
else
{
v2 = &basic16[3 * (unsigned __int8)a1];
*a2 = *v2;
a2[1] = v2[1];
result = (unsigned __int8)v2[2];
a2[2] = 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 *


maketable

decompiled_code:
__int64 maketable()
{
char *v0; // rbx
int i; // ebp
__int64 result; // rax
__int16 v3; // [rsp+Dh] [rbp-13h] BYREF
unsigned __int8 v4; // [rsp+Fh] [rbp-11h]

v3 = 0;
v4 = 0;
v0 = (char *)&colortable;
for ( i = 0; i != 254; ++i )
{
xterm2rgb(i, (char *)&v3);
*(_WORD *)v0 = v3;
result = v4;
v0[2] = v4;
v0 += 3;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
char *v0; // rbx
int i; // ebp
__int64 result; // rax
__int16 v3; // [rsp+Dh] [rbp-13h] BYREF
unsigned __int8 v4; // [rsp+Fh] [rbp-11h]

v3 = 0;
v4 = 0;
v0 = (char *)&colortable;
for ( i = 0; i != 254; ++i )
{
func7(i, (char *)&v3);
*(_WORD *)v0 = v3;
result = v4;
v0[2] = v4;
v0 += 3;
}
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 v1; // er9
int v2; // er8
int v3; // edi
unsigned __int8 *v4; // rax
double v5; // xmm3_8
unsigned int v6; // esi
unsigned int v7; // edx
double v8; // xmm2_8
double v9; // xmm0_8
bool v10; // cc

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

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

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

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:
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[120]; // [rsp+0h] [rbp-78h] 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[120]; // [rsp+0h] [rbp-78h] 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: fd int

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

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

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

result = (unsigned int)high_tasks;
if ( high_tasks <= 0 )
{
if ( !high_tasks && current_state == 1 )
{
fwrite("low tasks allowed\n", 1uLL, 0x12uLL, stderr);
high_proc = current_proc;
current_tasks = (__int64)&low_tasks;
result = low_proc;
current_proc = low_proc;
current_state = 0;
}
}
else if ( !current_state )
{
fwrite("high tasks only\n", 1uLL, 0x10uLL, stderr);
low_proc = current_proc;
current_tasks = (__int64)&high_tasks;
result = high_proc;
current_proc = high_proc;
current_state = 1;
}
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; // ebp
__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 func11(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // ebp
__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; // ebp
__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 func12(unsigned int a1)
{
unsigned int *v1; // rax
unsigned int v2; // ebp
__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 )
{
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 )
{
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 func4(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()
{
__sighandler_t result; // rax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

v1.sa_handler = (__sighandler_t)sigchld_handler;
v1.sa_flags = 0x10000000;
sigemptyset(&set);
sigaddset(&set, 17);
sigaddset(&set, 14);
*(__m128i *)v1.sa_mask.__val = _mm_loadu_si128((const __m128i *)&set);
*(__m128i *)&v1.sa_mask.__val[2] = _mm_loadu_si128((const __m128i *)&set.__val[2]);
*(__m128i *)&v1.sa_mask.__val[4] = _mm_loadu_si128((const __m128i *)&set.__val[4]);
*(__m128i *)&v1.sa_mask.__val[6] = _mm_loadu_si128((const __m128i *)&set.__val[6]);
*(__m128i *)&v1.sa_mask.__val[8] = _mm_loadu_si128((const __m128i *)&set.__val[8]);
*(__m128i *)&v1.sa_mask.__val[10] = _mm_loadu_si128((const __m128i *)&set.__val[10]);
*(__m128i *)&v1.sa_mask.__val[12] = _mm_loadu_si128((const __m128i *)&set.__val[12]);
*(__m128i *)&v1.sa_mask.__val[14] = _mm_loadu_si128((const __m128i *)&set.__val[14]);
if ( sigaction(17, &v1, 0LL) < 0 )
{
perror("sigaction: sigchld");
exit(1);
}
v1.sa_handler = (__sighandler_t)sigalrm_handler;
if ( sigaction(14, &v1, 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 func5()
{
__sighandler_t result; // rax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

v1.sa_handler = (__sighandler_t)sigchld_handler;
v1.sa_flags = 0x10000000;
sigemptyset(&set);
sigaddset(&set, 17);
sigaddset(&set, 14);
*(__m128i *)v1.sa_mask.__val = _mm_loadu_si128((const __m128i *)&set);
*(__m128i *)&v1.sa_mask.__val[2] = _mm_loadu_si128((const __m128i *)&set.__val[2]);
*(__m128i *)&v1.sa_mask.__val[4] = _mm_loadu_si128((const __m128i *)&set.__val[4]);
*(__m128i *)&v1.sa_mask.__val[6] = _mm_loadu_si128((const __m128i *)&set.__val[6]);
*(__m128i *)&v1.sa_mask.__val[8] = _mm_loadu_si128((const __m128i *)&set.__val[8]);
*(__m128i *)&v1.sa_mask.__val[10] = _mm_loadu_si128((const __m128i *)&set.__val[10]);
*(__m128i *)&v1.sa_mask.__val[12] = _mm_loadu_si128((const __m128i *)&set.__val[12]);
*(__m128i *)&v1.sa_mask.__val[14] = _mm_loadu_si128((const __m128i *)&set.__val[14]);
if ( sigaction(17, &v1, 0LL) < 0 )
{
perror("sigaction: sigchld");
exit(1);
}
v1.sa_handler = (__sighandler_t)sigalrm_handler;
if ( sigaction(14, &v1, 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)(); }


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

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

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

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

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 func7()
{
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 func8()
{
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 func9()
{
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
int result; // eax
__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 )
{
result = kill(*v1, 9);
}
else
{
v3 = (__pid_t *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( v3 )
result = kill(*v3, 15);
else
result = -1;
}
}
else
{
fwrite("Cannot kill shell\n", 1uLL, 0x12uLL, stderr);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1)
{
__pid_t *v1; // rax
int result; // eax
__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 )
{
result = kill(*v1, 9);
}
else
{
v3 = (__pid_t *)find_q(a1, high_proc, (unsigned int)high_tasks);
if ( v3 )
result = kill(*v3, 15);
else
result = -1;
}
}
else
{
fwrite("Cannot kill shell\n", 1uLL, 0x12uLL, stderr);
result = -1;
}
return result;
}

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


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:
result = -38;
break;
}
return result;
}

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

switch ( *(_DWORD *)a1 )
{
case 0:
func9();
result = 0;
break;
case 1:
result = func10(*(_DWORD *)(a1 + 4));
break;
case 2:
func4((char *)(a1 + 8));
result = 0;
break;
case 3:
func11(*(_DWORD *)(a1 + 4));
result = 0;
break;
case 4:
func12(*(_DWORD *)(a1 + 4));
result = 0;
break;
default:
result = -38;
break;
}
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)
{
_BYTE v3[76]; // [rsp+0h] [rbp-68h] BYREF
int buf[7]; // [rsp+4Ch] [rbp-1Ch] BYREF

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

canonical_decompiled_code:
int __fastcall func14(int fd, int a2)
{
_BYTE v3[76]; // [rsp+0h] [rbp-68h] BYREF
int buf[7]; // [rsp+4Ch] [rbp-1Ch] BYREF

while ( read(fd, v3, 0x44uLL) == 68 )
{
func7();
buf[0] = func13((__int64)v3);
func8();
if ( write(a2, buf, 4uLL) != 4 )
{
perror("scheduler: write to shell");
fwrite("Scheduler: giving up on shell request processing.\n", 1uLL, 0x32uLL, stderr);
return fflush(stderr);
}
}
perror("scheduler: read from shell");
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

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

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 )
{
perror("main: init, bad alloc");
exit(1);
}
v4 = (__int64)v3;
high_proc = (__int64)malloc(0x18uLL);
if ( !high_proc )
{
perror("main: init, bad alloc");
exit(1);
}
current_proc = v4;
init_q(v4, argv);
init_q(high_proc, argv);
sched_create_shell("shell", &request_fd_2, &return_fd_1);
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; // rbx
__int64 v5; // rbx

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 )
{
perror("main: init, bad alloc");
exit(1);
}
v4 = (__int64)v3;
high_proc = (__int64)malloc(0x18uLL);
if ( !high_proc )
{
perror("main: init, bad alloc");
exit(1);
}
current_proc = v4;
init_q(v4, argv);
init_q(high_proc, argv);
func3("shell", &request_fd_2, &return_fd_1);
wait_for_ready_children(1LL);
func5();
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);
func14(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; } *


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 i; // ebp

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

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

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

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