parseSentence

decompiled_code:
int parseSentence()
{
int v0; // eax
int v1; // edi
int result; // eax
const char *v3; // rax
char *v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v0 = 0;
while ( 2 )
{
switch ( v0 )
{
case 0:
printf("(S ");
v0 = 1;
continue;
case 1:
printf("(NP ");
v0 = 2;
continue;
case 2:
getWord((__int64)&v4);
v0 = 3;
if ( v5 == 1 )
{
printf("[DET %s] ", v4);
getWord((__int64)&v4);
v0 = 3;
}
continue;
case 3:
v1 = v5;
if ( v5 != 2 )
goto LABEL_24;
printf("(NOUN %s) )", v4);
frame = (__int64)v4;
getWord((__int64)&v4);
v0 = 4;
continue;
case 4:
if ( v5 == 5 )
return puts(" )");
printf("\n\t(VP ");
v0 = 5;
continue;
case 5:
v1 = v5;
if ( v5 != 3 )
goto LABEL_24;
printf("(VERB %s) ", v4);
qword_4040A8 = (__int64)v4;
getWord((__int64)&v4);
v0 = 6;
continue;
case 6:
if ( v5 != 5 )
{
printf("\n\t\t(VP_NP ");
v0 = 7;
continue;
}
return puts(" ) )");
case 7:
v0 = 8;
if ( v5 == 1 )
{
printf("[DET %s] ", v4);
getWord((__int64)&v4);
v0 = 8;
}
continue;
case 8:
v0 = 9;
if ( v5 == 4 )
{
printf("[ADJ %s] ", v4);
qword_4040B0 = (__int64)v4;
getWord((__int64)&v4);
v0 = 9;
}
continue;
case 9:
v1 = v5;
if ( v5 == 2 )
{
printf("(NOUN %s) ) ) )\n", v4);
result = (int)v4;
s = v4;
}
else
{
LABEL_24:
v3 = strtype(v1);
result = printf("!ERROR! (type=%s)\n", v3);
}
return result;
default:
continue;
}
}
}

canonical_decompiled_code:
int func15()
{
int v0; // eax
int v1; // edi
int result; // eax
const char *v3; // rax
char *v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v0 = 0;
while ( 2 )
{
switch ( v0 )
{
case 0:
printf("(S ");
v0 = 1;
continue;
case 1:
printf("(NP ");
v0 = 2;
continue;
case 2:
func12((__int64)&v4);
v0 = 3;
if ( v5 == 1 )
{
printf("[DET %s] ", v4);
func12((__int64)&v4);
v0 = 3;
}
continue;
case 3:
v1 = v5;
if ( v5 != 2 )
goto LABEL_24;
printf("(NOUN %s) )", v4);
frame = (__int64)v4;
func12((__int64)&v4);
v0 = 4;
continue;
case 4:
if ( v5 == 5 )
return puts(" )");
printf("\n\t(VP ");
v0 = 5;
continue;
case 5:
v1 = v5;
if ( v5 != 3 )
goto LABEL_24;
printf("(VERB %s) ", v4);
qword_4040A8 = (__int64)v4;
func12((__int64)&v4);
v0 = 6;
continue;
case 6:
if ( v5 != 5 )
{
printf("\n\t\t(VP_NP ");
v0 = 7;
continue;
}
return puts(" ) )");
case 7:
v0 = 8;
if ( v5 == 1 )
{
printf("[DET %s] ", v4);
func12((__int64)&v4);
v0 = 8;
}
continue;
case 8:
v0 = 9;
if ( v5 == 4 )
{
printf("[ADJ %s] ", v4);
qword_4040B0 = (__int64)v4;
func12((__int64)&v4);
v0 = 9;
}
continue;
case 9:
v1 = v5;
if ( v5 == 2 )
{
printf("(NOUN %s) ) ) )\n", v4);
result = (int)v4;
s = v4;
}
else
{
LABEL_24:
v3 = func13(v1);
result = printf("!ERROR! (type=%s)\n", v3);
}
return result;
default:
continue;
}
}
}

original_code:
void parseSentence( void )
{
  int state = 0;
  word_t symbol;
  while (state != 10) {
    switch( state ) {
      case 0:
        printf("(S ");
        state = 1;
        break;
      case 1:
        printf("(NP ");
        state = 2;
        break;
      case 2:
        getWord( &symbol );
        if (symbol.type == 1) {
          printf("[DET %s] ", symbol.word);
          getWord( &symbol );
        }
        state = 3;
        break;
      case 3:
        if (symbol.type == 2) {
          printf("(NOUN %s) )", symbol.word);
          frame.actor = symbol.word;
          state = 4;
          getWord( &symbol );
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
      case 4:
        if (symbol.type == 5) {
          printf(" )\n");
          state = 10;
        } else {
          printf("\n\t(VP ");
          state = 5;
        }
        break;
      case 5:
        if (symbol.type == 3) {
          printf("(VERB %s) ", symbol.word);
          frame.action = symbol.word;
          state = 6;
          getWord( &symbol );
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
      case 6:
        if (symbol.type == 5) {
          printf(" ) )\n");
          state = 10;
        } else {
          printf("\n\t\t(VP_NP ");
          state = 7;
        }
        break;
      case 7:
        if (symbol.type == 1) {
          printf("[DET %s] ", symbol.word);
          getWord( &symbol );
        }
        state = 8;
        break;
      case 8:
        if (symbol.type == 4) {
          printf("[ADJ %s] ", symbol.word);
          frame.modifier = symbol.word;
          getWord( &symbol );
        }
        state = 9;
        break;
      case 9:
        if (symbol.type == 2) {
          printf("(NOUN %s) ) ) )\n", symbol.word);
          frame.object = symbol.word;
          state = 10;
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
    }
  }
  return;
}

canonical_original_code:
void parseSentence()
{
  int state = 0;
  struct word_t symbol;
  while (state != 10) {
    switch( state ) {
      case 0:
        printf("(S ");
        state = 1;
        break;
      case 1:
        printf("(NP ");
        state = 2;
        break;
      case 2:
        getWord( &symbol );
        if (symbol.type == 1) {
          printf("[DET %s] ", symbol.word);
          getWord( &symbol );
        }
        state = 3;
        break;
      case 3:
        if (symbol.type == 2) {
          printf("(NOUN %s) )", symbol.word);
          frame.actor = symbol.word;
          state = 4;
          getWord( &symbol );
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
      case 4:
        if (symbol.type == 5) {
          printf(" )\n");
          state = 10;
        } else {
          printf("\n\t(VP ");
          state = 5;
        }
        break;
      case 5:
        if (symbol.type == 3) {
          printf("(VERB %s) ", symbol.word);
          frame.action = symbol.word;
          state = 6;
          getWord( &symbol );
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
      case 6:
        if (symbol.type == 5) {
          printf(" ) )\n");
          state = 10;
        } else {
          printf("\n\t\t(VP_NP ");
          state = 7;
        }
        break;
      case 7:
        if (symbol.type == 1) {
          printf("[DET %s] ", symbol.word);
          getWord( &symbol );
        }
        state = 8;
        break;
      case 8:
        if (symbol.type == 4) {
          printf("[ADJ %s] ", symbol.word);
          frame.modifier = symbol.word;
          getWord( &symbol );
        }
        state = 9;
        break;
      case 9:
        if (symbol.type == 2) {
          printf("(NOUN %s) ) ) )\n", symbol.word);
          frame.object = symbol.word;
          state = 10;
        } else {
          printf("!ERROR! (type=%s)\n", strtype(symbol.type));
          state = 10;
        }
        break;
    }
  }
  return;
}

variable_types: state int

variable_types:
symbol
struct word_t { char *word; int type; }


init

decompiled_code:
__int64 init()
{
memset(lexicon, 0, 0x1E0uLL);
addWord(1, (__int64)"the");
addWord(1, (__int64)"a");
addWord(2, (__int64)"man");
addWord(2, (__int64)"house");
addWord(2, (__int64)"dog");
addWord(2, (__int64)"fruit");
addWord(2, (__int64)"car");
addWord(2, (__int64)"cat");
addWord(3, (__int64)"washed");
addWord(3, (__int64)"ate");
addWord(3, (__int64)"drove");
addWord(3, (__int64)"chased");
addWord(3, (__int64)"painted");
addWord(3, (__int64)"marked");
addWord(4, (__int64)"blue");
addWord(4, (__int64)"angry");
addWord(4, (__int64)"dirty");
addWord(4, (__int64)"quiet");
addWord(4, (__int64)"confused");
return addWord(4, (__int64)"noisy");
}

original_code:
void init( void )
{
  memset( &lexicon, 0, sizeof(lexicon_t) );
  addWord( 1, "the" ); addWord( 1, "a" );
  addWord( 2, "man" ); addWord( 2, "house" );
  addWord( 2, "dog" ); addWord( 2, "fruit" );
  addWord( 2, "car" ); addWord( 2, "cat" );
  addWord( 3, "washed" ); addWord( 3, "ate" );
  addWord( 3, "drove" ); addWord( 3, "chased" );
  addWord( 3, "painted" ); addWord( 3, "marked" );
  addWord( 4, "blue" ); addWord( 4, "angry" );
  addWord( 4, "dirty" ); addWord( 4, "quiet" );
  addWord( 4, "confused" ); addWord( 4, "noisy" );
  return;
}

canonical_original_code:
void init()
{
  memset( &lexicon, 0, sizeof(lexicon_t) );
  addWord( 1, "the" ); addWord( 1, "a" );
  addWord( 2, "man" ); addWord( 2, "house" );
  addWord( 2, "dog" ); addWord( 2, "fruit" );
  addWord( 2, "car" ); addWord( 2, "cat" );
  addWord( 3, "washed" ); addWord( 3, "ate" );
  addWord( 3, "drove" ); addWord( 3, "chased" );
  addWord( 3, "painted" ); addWord( 3, "marked" );
  addWord( 4, "blue" ); addWord( 4, "angry" );
  addWord( 4, "dirty" ); addWord( 4, "quiet" );
  addWord( 4, "confused" ); addWord( 4, "noisy" );
  return;
}

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init(argc, argv, envp);
initSentence("the man washed the blue car");
parseSentence();
emitFrame();
destroySentence();
initSentence("a dog chased a dirty cat");
parseSentence();
emitFrame();
destroySentence();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func9(argc, argv, envp);
func10("the man washed the blue car");
func15();
func14();
func11();
func10("a dog chased a dirty cat");
func15();
func14();
func11();
return 0;
}

original_code:
int main()
{
  init();
  initSentence( "the man washed the blue car" );
  parseSentence();
  emitFrame();
  destroySentence();
  initSentence( "a dog chased a dirty cat" );
  parseSentence();
  emitFrame();
  destroySentence();
  return 0;
}

return_type: int


initStateSpace

decompiled_code:
char *initStateSpace()
{
char *v0; // rdx
char *result; // rax

v0 = (char *)&unk_404580;
do
{
result = v0 - 800;
do
{
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = 0LL;
*((_QWORD *)result + 6) = 0LL;
*((_QWORD *)result + 7) = 0LL;
*((_QWORD *)result + 8) = 0LL;
*((_QWORD *)result + 9) = 0LL;
result += 80;
}
while ( v0 != result );
v0 += 800;
}
while ( v0 != (char *)4220096 );
return result;
}

canonical_decompiled_code:
char *func7()
{
char *v0; // rdx
char *result; // rax

v0 = (char *)&unk_404580;
do
{
result = v0 - 800;
do
{
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = 0LL;
*((_QWORD *)result + 6) = 0LL;
*((_QWORD *)result + 7) = 0LL;
*((_QWORD *)result + 8) = 0LL;
*((_QWORD *)result + 9) = 0LL;
result += 80;
}
while ( v0 != result );
v0 += 800;
}
while ( v0 != (char *)4220096 );
return result;
}

original_code:
void initStateSpace( void )
{
  int x_state, y_state, action;
  for ( y_state = 0 ; y_state < 10 ; y_state++ ) {
    for ( x_state = 0 ; x_state < 10 ; x_state++ ) {
      for (action = 0 ; action < 8 ; action++) {
        stateSpace[y_state][x_state].Q[action] = 0.0;
      }
      stateSpace[y_state][x_state].sumQ = 0.0;
      stateSpace[y_state][x_state].maxQ = 0.0;
    }
  }
  return;
}

canonical_original_code:
void initStateSpace()
{
  int x_state; int y_state; int action;
  for ( y_state = 0 ; y_state < 10 ; y_state++ ) {
    for ( x_state = 0 ; x_state < 10 ; x_state++ ) {
      for (action = 0 ; action < 8 ; action++) {
        stateSpace[y_state][x_state].Q[action] = 0.0;
      }
      stateSpace[y_state][x_state].sumQ = 0.0;
      stateSpace[y_state][x_state].maxQ = 0.0;
    }
  }
  return;
}

variable_types: action int


updateSum

decompiled_code:
__int64 __fastcall updateSum(int a1, int a2)
{
__int64 v2; // r8
__int64 v3; // rax
double v4; // xmm0_8
__int64 result; // rax

v2 = 100LL * a1;
qword_4042A0[10 * a2 + v2] = 0LL;
v3 = 0LL;
v4 = 0.0;
do
v4 = v4 + *(double *)&stateSpace[10 * a2 + v3++ + v2];
while ( v3 != 8 );
result = 100LL * a1;
*(double *)&qword_4042A0[10 * a2 + result] = v4;
return result * 8;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
__int64 v2; // r8
__int64 v3; // rax
double v4; // xmm0_8
__int64 result; // rax

v2 = 100LL * a1;
qword_4042A0[10 * a2 + v2] = 0LL;
v3 = 0LL;
v4 = 0.0;
do
v4 = v4 + *(double *)&stateSpace[10 * a2 + v3++ + v2];
while ( v3 != 8 );
result = 100LL * a1;
*(double *)&qword_4042A0[10 * a2 + result] = v4;
return result * 8;
}

original_code:
void updateSum( int y, int x )
{
  int i;
  stateSpace[y][x].sumQ = 0.0;
  for (i = 0 ; i < 8 ; i++) {
    stateSpace[y][x].sumQ += stateSpace[y][x].Q[i];
  }
  return;
}

canonical_original_code:
void updateSum(int y, int x)
{
  int i;
  stateSpace[y][x].sumQ = 0.0;
  for (i = 0 ; i < 8 ; i++) {
    stateSpace[y][x].sumQ += stateSpace[y][x].Q[i];
  }
  return;
}

variable_types: y int

variable_types: x int

variable_types: i int


legalMove

decompiled_code:
_BOOL8 __fastcall legalMove(int a1, int a2, int a3)
{
return grid[10 * dir[2 * a3] + 10 * a1 + dword_402044[2 * a3] + a2] >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(int a1, int a2, int a3)
{
return grid[10 * dir[2 * a3] + 10 * a1 + dword_402044[2 * a3] + a2] >= 0;
}

original_code:
int legalMove( int y_state, int x_state, int action )
{
  int y = y_state + dir[action].y;
  int x = x_state + dir[action].x;
  if (grid[y][x] < 0) return 0;
  else return 1;
}

canonical_original_code:
int legalMove(int y_state, int x_state, int action)
{
  int y = y_state + dir[action].y;
  int x = x_state + dir[action].x;
  if (grid[y][x] < 0) return 0;
  else return 1;
}


getRandomAction

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

do
v2 = (int)((double)rand() * 8.0 * 4.656612873077393e-10);
while ( !legalMove(a1, a2, v2) );
return v2;
}

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

do
v2 = (int)((double)rand() * 8.0 * 4.656612873077393e-10);
while ( !func10(a1, a2, v2) );
return v2;
}

original_code:
int getRandomAction( int y, int x )
{
  int action;
  do {
    action = (int)((double)(8)*rand()/(2147483647 +1.0));
  } while (!legalMove(y, x, action));
  return action;
}

canonical_original_code:
int getRandomAction(int y, int x)
{
  int action;
  do {
    action = (int)((double)(8)*rand()/(2147483647 +1.0));
  } while (!legalMove(y, x, action));
  return action;
}


chooseAction

decompiled_code:
__int64 __fastcall chooseAction(int a1, int a2)
{
int v3; // ebx
__int64 v5; // rsi
__int64 v6; // rcx
double v7; // r15
__int64 v8; // r14
__int64 v9; // r13
int v10; // er13
__int64 v11; // r14
__int64 v12; // r15
double v13; // [rsp+0h] [rbp-48h]
double v14; // [rsp+8h] [rbp-40h]

if ( actionSelection )
{
if ( actionSelection != 1 )
__assert_fail("0", "grid.c", 0xFFu, "chooseAction");
if ( (double)rand() / 2147483647.0 < 0.2 || stateSpace[100 * a1 + 8 + 10 * a2] == 0.0 )
{
LABEL_5:
v3 = getRandomAction(a1, a2);
}
else
{
v3 = getRandomAction(a1, a2);
v10 = 0;
v11 = 5LL * a1;
v12 = 20LL * a1;
while ( 1 )
{
if ( legalMove(a1, a2, v3) )
{
v13 = stateSpace[10 * a2 + 4 * v11 + 4 * v12 + v3];
v14 = *(double *)&qword_4042A0[10 * a2 + 4 * v11 + 4 * v12];
if ( v13 / v14 > (double)rand() / 2147483647.0 )
break;
}
if ( ++v3 == 8 )
v3 = 0;
if ( ++v10 == 8 )
goto LABEL_5;
}
}
}
else
{
findMaxQ(a1, a2);
v5 = 10LL * a2;
v6 = 100LL * a1;
v7 = *(double *)&qword_4042A8[v5 + v6];
v8 = v6 * 8 + v5 * 8;
v9 = 0LL;
while ( 1 )
{
v3 = v9;
if ( v7 == *(double *)((char *)&stateSpace[v9] + v8) && legalMove(a1, a2, v9) )
break;
if ( ++v9 == 8 )
__assert_fail("0", "grid.c", 0xD6u, "chooseAction");
}
}
return (unsigned int)v3;
}

original_code:
int chooseAction( int y, int x )
{
  int action;
  int count;
  if (actionSelection == 0) {
    findMaxQ(y, x);
    for (action = 0 ; action < 8 ; action++) {
      if (stateSpace[y][x].maxQ == stateSpace[y][x].Q[action]) {
        if (legalMove(y, x, action)) return(action);
      }
    }
    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "grid.c", 214, __extension__ __PRETTY_FUNCTION__); }));
  } else if (actionSelection == 1) {
    if ((0.2 > ((double)rand() / (double)2147483647)) ||
        (stateSpace[y][x].sumQ == 0.0)) {
      action = getRandomAction(y, x);
    } else {
      double prob;
      action = getRandomAction(y, x);
      for (count = 0 ; count < 8 ; count++) {
        prob = (stateSpace[y][x].Q[action] / stateSpace[y][x].sumQ);
        if (legalMove(y, x, action)) {
          if (prob > ((double)rand() / (double)2147483647)) {
            return(action);
          }
        }
        if (++action == 8) action = 0;
      }
      if (count == 8) {
        action = getRandomAction(y, x);
      }
    }
  } else {
    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "grid.c", 255, __extension__ __PRETTY_FUNCTION__); }));
  }
  return action;
}

canonical_original_code:
int chooseAction(int y, int x)
{
  int action;
  int count;
  if (actionSelection == 0) {
    findMaxQ(y, x);
    for (action = 0 ; action < 8 ; action++) {
      if (stateSpace[y][x].maxQ == stateSpace[y][x].Q[action]) {
        if (legalMove(y, x, action)) return(action);
      }
    }
    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "grid.c", 214, __extension__ __PRETTY_FUNCTION__); }));
  } else if (actionSelection == 1) {
    if ((0.2 > ((double)rand() / (double)2147483647)) ||
        (stateSpace[y][x].sumQ == 0.0)) {
      action = getRandomAction(y, x);
    } else {
      double prob;
      action = getRandomAction(y, x);
      for (count = 0 ; count < 8 ; count++) {
        prob = (stateSpace[y][x].Q[action] / stateSpace[y][x].sumQ);
        if (legalMove(y, x, action)) {
          if (prob > ((double)rand() / (double)2147483647)) {
            return(action);
          }
        }
        if (++action == 8) action = 0;
      }
      if (count == 8) {
        action = getRandomAction(y, x);
      }
    }
  } else {
    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "grid.c", 255, __extension__ __PRETTY_FUNCTION__); }));
  }
  return action;
}

variable_types: count int

variable_types: prob double


updateFunction

decompiled_code:
__int64 __fastcall updateFunction(int a1, int a2, int a3)
{
__int64 v5; // rbx
int v6; // edi
int v7; // esi
__int64 v8; // r15
__int64 v9; // rsi
double v11; // [rsp+0h] [rbp-38h]

v5 = a3;
v6 = dir[2 * a3] + a1;
v7 = dword_402044[2 * a3] + a2;
v8 = v7;
v11 = (double)grid[10 * v6 + v7] + 0.1;
findMaxQ(v6, v7);
v9 = v5 + 10LL * a2 + 100LL * a1;
stateSpace[v9] = (v11 + 0.5 * *(double *)&qword_4042A8[100 * v6 + 10 * v8] - stateSpace[v9]) * 0.6 + stateSpace[v9];
return updateSum(a1, a2);
}

original_code:
void updateFunction( int y, int x, int action )
{
  int newy = y + dir[action].y;
  int newx = x + dir[action].x;
  double reward;
  reward = (double)grid[newy][newx] + 0.1;
  findMaxQ( newy, newx );
  stateSpace[y][x].Q[action] +=
    0.6 * (reward + (0.5 * stateSpace[newy][newx].maxQ) -
                      stateSpace[y][x].Q[action]);
  updateSum( y, x );
  return;
}

canonical_original_code:
void updateFunction(int y, int x, int action)
{
  int newy = y + dir[action].y;
  int newx = x + dir[action].x;
  double reward;
  reward = (double)grid[newy][newx] + 0.1;
  findMaxQ( newy, newx );
  stateSpace[y][x].Q[action] +=
    0.6 * (reward + (0.5 * stateSpace[newy][newx].maxQ) -
                      stateSpace[y][x].Q[action]);
  updateSum( y, x );
  return;
}

variable_types: newy int

variable_types: newx int

variable_types: reward double


showPath

decompiled_code:
int showPath()
{
unsigned int *v0; // rbp
unsigned int *v1; // r12
unsigned int *v2; // rbx
int v3; // ebx
int i; // er12
int v5; // eax
unsigned int *v6; // rbx

actionSelection = 0;
putchar(10);
v0 = (unsigned int *)&unk_4040C8;
v1 = (unsigned int *)&unk_4040C8;
do
{
v2 = v1 - 10;
do
printf("%2d ", *v2++);
while ( v2 != v1 );
putchar(10);
v1 += 10;
}
while ( v1 != (unsigned int *)&unk_404258 );
putchar(10);
v3 = dword_40409C;
for ( i = start; ; i += dir[2 * v5] )
{
grid[10 * i + v3] = 0;
if ( end[1] == v3 && end[0] == i )
break;
v5 = chooseAction(i, v3);
v3 += dword_402044[2 * v5];
}
putchar(10);
do
{
v6 = v0 - 10;
do
printf("%2d ", *v6++);
while ( v6 != v0 );
putchar(10);
v0 += 10;
}
while ( v0 != (unsigned int *)&unk_404258 );
return putchar(10);
}

canonical_decompiled_code:
int func14()
{
unsigned int *v0; // rbp
unsigned int *v1; // r12
unsigned int *v2; // rbx
int v3; // ebx
int i; // er12
int v5; // eax
unsigned int *v6; // rbx

actionSelection = 0;
putchar(10);
v0 = (unsigned int *)&unk_4040C8;
v1 = (unsigned int *)&unk_4040C8;
do
{
v2 = v1 - 10;
do
printf("%2d ", *v2++);
while ( v2 != v1 );
putchar(10);
v1 += 10;
}
while ( v1 != (unsigned int *)&unk_404258 );
putchar(10);
v3 = dword_40409C;
for ( i = start; ; i += dir[2 * v5] )
{
grid[10 * i + v3] = 0;
if ( end[1] == v3 && end[0] == i )
break;
v5 = func12(i, v3);
v3 += dword_402044[2 * v5];
}
putchar(10);
do
{
v6 = v0 - 10;
do
printf("%2d ", *v6++);
while ( v6 != v0 );
putchar(10);
v0 += 10;
}
while ( v0 != (unsigned int *)&unk_404258 );
return putchar(10);
}

original_code:
void showPath( void )
{
  int x, y, action;
  actionSelection = 0;
  x = start.x; y = start.y;
  printf("\n");
  for ( y = 0 ; y < 10 ; y++ ) {
    for ( x = 0 ; x < 10 ; x++ ) {
      printf("%2d ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  x = start.x; y = start.y;
  while (1) {
    grid[y][x] = 0;
    if ((x == end.x) && (y == end.y)) break;
    action = chooseAction( y, x );
    x += dir[action].x;
    y += dir[action].y;
  }
  printf("\n");
  for ( y = 0 ; y < 10 ; y++ ) {
    for ( x = 0 ; x < 10 ; x++ ) {
      printf("%2d ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  return;
}

canonical_original_code:
void showPath()
{
  int x; int y; int action;
  actionSelection = 0;
  x = start.x; y = start.y;
  printf("\n");
  for ( y = 0 ; y < 10 ; y++ ) {
    for ( x = 0 ; x < 10 ; x++ ) {
      printf("%2d ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  x = start.x; y = start.y;
  while (1) {
    grid[y][x] = 0;
    if ((x == end.x) && (y == end.y)) break;
    action = chooseAction( y, x );
    x += dir[action].x;
    y += dir[action].y;
  }
  printf("\n");
  for ( y = 0 ; y < 10 ; y++ ) {
    for ( x = 0 ; x < 10 ; x++ ) {
      printf("%2d ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  return;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebp
unsigned int v5; // eax
__int64 v6; // r13
int v7; // ebx
int v8; // edx

v3 = dword_40409C;
v4 = start;
v5 = time(0LL);
srand(v5);
initStateSpace();
v6 = 100000LL;
do
{
v7 = chooseAction(v4, v3);
updateFunction(v4, v3, v7);
v3 += dword_402044[2 * v7];
v8 = dir[2 * v7] + v4;
v4 = v8;
if ( end[1] == v3 && end[0] == v8 )
{
v3 = dword_40409C;
v4 = start;
}
--v6;
}
while ( v6 );
showPath();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebp
unsigned int v5; // eax
__int64 v6; // r13
int v7; // ebx
int v8; // edx

v3 = dword_40409C;
v4 = start;
v5 = time(0LL);
srand(v5);
func7();
v6 = 100000LL;
do
{
v7 = func12(v4, v3);
func13(v4, v3, v7);
v3 += dword_402044[2 * v7];
v8 = dir[2 * v7] + v4;
v4 = v8;
if ( end[1] == v3 && end[0] == v8 )
{
v3 = dword_40409C;
v4 = start;
}
--v6;
}
while ( v6 );
func14();
return 0;
}

original_code:
int main()
{
  long epochs;
  int x, y, action;
  x = start.x; y = start.y;
  srand(time(((void *)0)));
  initStateSpace();
  for ( epochs = 0 ; epochs < 100000L ; epochs++ ) {
    action = chooseAction( y, x );
    updateFunction( y, x, action );
    x += dir[action].x;
    y += dir[action].y;
    if ((x == end.x) && (y == end.y)) {
      x = start.x; y = start.y;
    }
  }
  showPath();
  return 0;
}

canonical_original_code:
int main()
{
  long epochs;
  int x; int y; int action;
  x = start.x; y = start.y;
  srand(time(((void *)0)));
  initStateSpace();
  for ( epochs = 0 ; epochs < 100000L ; epochs++ ) {
    action = chooseAction( y, x );
    updateFunction( y, x, action );
    x += dir[action].x;
    y += dir[action].y;
    if ((x == end.x) && (y == end.y)) {
      x = start.x; y = start.y;
    }
  }
  showPath();
  return 0;
}

variable_types: epochs long


initAdjMatrix

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

result = 0LL;
memset(&adjMatrix, 0, 0x190uLL);
visited = 0LL;
qword_4040C8 = 0LL;
qword_4040D0 = 0LL;
qword_4040D8 = 0LL;
qword_4040E0 = 0LL;
return result;
}

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

result = 0LL;
memset(&adjMatrix, 0, 0x190uLL);
visited = 0LL;
qword_4040C8 = 0LL;
qword_4040D0 = 0LL;
qword_4040D8 = 0LL;
qword_4040E0 = 0LL;
return result;
}

original_code:
void initAdjMatrix( void )
{
  memset ( (void *)adjMatrix, 0, sizeof(adjMatrix) );
  memset ( (void *)visited, 0, sizeof(visited) );
  return;
}

canonical_original_code:
void initAdjMatrix()
{
  memset ( (void *)adjMatrix, 0, sizeof(adjMatrix) );
  memset ( (void *)visited, 0, sizeof(visited) );
  return;
}


initStack

decompiled_code:
void initStack()
{
stack_index = 0;
}

canonical_decompiled_code:
void func9()
{
stack_index = 0;
}

original_code:
void initStack( void )
{
  stack_index = 0;
  return;
}

canonical_original_code:
void initStack()
{
  stack_index = 0;
  return;
}


push

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

LODWORD(result) = stack_index;
if ( stack_index > 9 )
__assert_fail("stack_index < MAX_STACK", "dfs.c", 0x34u, "push");
++stack_index;
result = (int)result;
stack[(int)result] = a1;
return result;
}

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

LODWORD(result) = stack_index;
if ( stack_index > 9 )
__assert_fail("stack_index < MAX_STACK", "dfs.c", 0x34u, "push");
++stack_index;
result = (int)result;
stack[(int)result] = a1;
return result;
}

original_code:
void push( int elem )
{
  ((void) sizeof ((stack_index < 10) ? 1 : 0), __extension__ ({ if (stack_index < 10) ; else __assert_fail ("stack_index < MAX_STACK", "dfs.c", 52, __extension__ __PRETTY_FUNCTION__); }));
  stack[stack_index++] = elem;
  return;
}

canonical_original_code:
void push(int elem)
{
  ((void) sizeof ((stack_index < 10) ? 1 : 0), __extension__ ({ if (stack_index < 10) ; else __assert_fail ("stack_index < MAX_STACK", "dfs.c", 52, __extension__ __PRETTY_FUNCTION__); }));
  stack[stack_index++] = elem;
  return;
}

variable_types: elem int


pop

decompiled_code:
__int64 pop()
{
int v0; // eax

v0 = stack_index;
if ( stack_index <= 0 )
__assert_fail("stack_index > 0", "dfs.c", 0x3Fu, "pop");
--stack_index;
return (unsigned int)stack[v0 - 1];
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // eax

v0 = stack_index;
if ( stack_index <= 0 )
__assert_fail("stack_index > 0", "dfs.c", 0x3Fu, "pop");
--stack_index;
return (unsigned int)stack[v0 - 1];
}

original_code:
int pop( void )
{
  int elem;
  ((void) sizeof ((stack_index > 0) ? 1 : 0), __extension__ ({ if (stack_index > 0) ; else __assert_fail ("stack_index > 0", "dfs.c", 63, __extension__ __PRETTY_FUNCTION__); }));
  elem = stack[--stack_index];
  return elem;
}

canonical_original_code:
int pop()
{
  int elem;
  ((void) sizeof ((stack_index > 0) ? 1 : 0), __extension__ ({ if (stack_index > 0) ; else __assert_fail ("stack_index > 0", "dfs.c", 63, __extension__ __PRETTY_FUNCTION__); }));
  elem = stack[--stack_index];
  return elem;
}


searchWorkingMemory

decompiled_code:
__int64 __fastcall searchWorkingMemory(char *s1, char *a2)
{
__int64 v2; // rbx
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
char v7[97]; // [rsp+1h] [rbp-E9h]
char v8[96]; // [rsp+2h] [rbp-E8h] BYREF
char s2[136]; // [rsp+62h] [rbp-88h] BYREF

v2 = (unsigned int)((_DWORD)&workingMemory + 4);
do
{
if ( *(_DWORD *)(v2 - 4) )
{
_isoc99_sscanf(v2, "(%s %s)", s2, v8);
v3 = strlen(v8);
if ( v7[v3] == 41 )
v7[v3] = 0;
v4 = strlen(s1);
if ( !strncmp(s1, s2, v4) )
{
v5 = strlen(a2);
if ( !strncmp(a2, v8, v5) )
return 1LL;
}
}
v2 += 96LL;
}
while ( v2 != (_DWORD)&workingMemory + 3844 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1, char *a2)
{
__int64 v2; // rbx
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
char v7[97]; // [rsp+1h] [rbp-E9h]
char v8[96]; // [rsp+2h] [rbp-E8h] BYREF
char s2[136]; // [rsp+62h] [rbp-88h] BYREF

v2 = (unsigned int)((_DWORD)&workingMemory + 4);
do
{
if ( *(_DWORD *)(v2 - 4) )
{
_isoc99_sscanf(v2, "(%s %s)", s2, v8);
v3 = strlen(v8);
if ( v7[v3] == 41 )
v7[v3] = 0;
v4 = strlen(s1);
if ( !strncmp(s1, s2, v4) )
{
v5 = strlen(a2);
if ( !strncmp(a2, v8, v5) )
return 1LL;
}
}
v2 += 96LL;
}
while ( v2 != (_DWORD)&workingMemory + 3844 );
return 0LL;
}

original_code:
int searchWorkingMemory( char *term1, char *term2 )
{
  int ret = 0;
  int curMem = 0;
  char wm_term1[80 +1];
  char wm_term2[80 +1];
  while (1) {
    if (workingMemory[curMem].active) {
      sscanf(workingMemory[curMem].element, "(%s %s)", wm_term1, wm_term2);
      if (wm_term2[strlen(wm_term2)-1] == ')') wm_term2[strlen(wm_term2)-1] = 0;
      if ((!strncmp(term1, wm_term1, strlen(term1))) &&
          (!strncmp(term2, wm_term2, strlen(term2)))) {
        ret = 1;
        break;
      }
    }
    curMem++;
    if (curMem == 40) {
      break;
    }
  }
  return ret;
}

canonical_original_code:
int searchWorkingMemory(char *term1, char *term2)
{
  int ret = 0;
  int curMem = 0;
  char wm_term1[-1];
  char wm_term2[-1];
  while (1) {
    if (workingMemory[curMem].active) {
      sscanf(workingMemory[curMem].element, "(%s %s)", wm_term1, wm_term2);
      if (wm_term2[strlen(wm_term2)-1] == ')') wm_term2[strlen(wm_term2)-1] = 0;
      if ((!strncmp(term1, wm_term1, strlen(term1))) &&
          (!strncmp(term2, wm_term2, strlen(term2)))) {
        ret = 1;
        break;
      }
    }
    curMem++;
    if (curMem == 40) {
      break;
    }
  }
  return ret;
}

variable_types: ret int

variable_types: curMem int


checkPattern

decompiled_code:
__int64 __fastcall checkPattern(int a1, char *a2)
{
__int64 i; // r14
unsigned int v4; // er14
char *v5; // r12
size_t v6; // rax
__int64 v7; // rbx
size_t v8; // rax
size_t v9; // rax
__int64 v10; // rbx
size_t v11; // rax
size_t v12; // rax
unsigned __int64 v13; // rcx
void *v14; // rdi
_QWORD *j; // rdi
char v17[96]; // [rsp+0h] [rbp-1A8h] BYREF
char src[96]; // [rsp+60h] [rbp-148h] BYREF
char s[96]; // [rsp+C0h] [rbp-E8h] BYREF
char s1[136]; // [rsp+120h] [rbp-88h] BYREF

for ( i = ruleSet[13 * a1 + 11]; i; i = *(_QWORD *)(i + 88) )
{
_isoc99_sscanf(i + 4, "(%s %s)", s1, s);
v6 = strlen(s);
if ( src[v6 + 95] == 41 )
src[v6 + 95] = 0;
if ( s[0] == 63 )
{
v7 = (unsigned int)((_DWORD)&workingMemory + 4);
do
{
if ( *(_DWORD *)(v7 - 4) )
{
_isoc99_sscanf(v7, "(%s %s)", v17, src);
v9 = strlen(src);
if ( v17[v9 + 95] == 41 )
v17[v9 + 95] = 0;
v8 = strlen(s1);
if ( !strncmp(s1, v17, v8) )
addToChain(src);
}
v7 += 96LL;
}
while ( v7 != (_DWORD)&workingMemory + 3844 );
}
}
v4 = 0;
v5 = (char *)&ruleSet[13 * a1];
while ( 1 )
{
v10 = *((_QWORD *)v5 + 11);
if ( v10 )
{
while ( 1 )
{
_isoc99_sscanf(v10 + 4, "(%s %s)", s1, s);
v11 = strlen(s);
if ( src[v11 + 95] == 41 )
src[v11 + 95] = 0;
v12 = strlen(s1);
v13 = 5LL;
if ( v12 <= 5 )
v13 = v12;
if ( !memcmp(s1, "true", v13) )
break;
if ( s[0] == 63 && chain )
strcpy(s, (const char *)chain + 4);
v4 = searchWorkingMemory(s1, s);
if ( !v4 )
goto LABEL_25;
v10 = *(_QWORD *)(v10 + 88);
if ( !v10 )
goto LABEL_33;
}
v4 = 1;
LABEL_33:
for ( j = chain; chain; j = chain )
{
chain = (void *)j[11];
free(j);
}
strcpy(a2, s);
goto LABEL_27;
}
if ( v4 )
goto LABEL_33;
LABEL_25:
v14 = chain;
if ( !chain )
return 0;
chain = (void *)*((_QWORD *)chain + 11);
free(v14);
v4 = 0;
LABEL_27:
if ( !chain )
return v4;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, char *a2)
{
__int64 i; // r14
unsigned int v4; // er14
char *v5; // r12
size_t v6; // rax
__int64 v7; // rbx
size_t v8; // rax
size_t v9; // rax
__int64 v10; // rbx
size_t v11; // rax
size_t v12; // rax
unsigned __int64 v13; // rcx
void *v14; // rdi
_QWORD *j; // rdi
char v17[96]; // [rsp+0h] [rbp-1A8h] BYREF
char src[96]; // [rsp+60h] [rbp-148h] BYREF
char s[96]; // [rsp+C0h] [rbp-E8h] BYREF
char s1[136]; // [rsp+120h] [rbp-88h] BYREF

for ( i = ruleSet[13 * a1 + 11]; i; i = *(_QWORD *)(i + 88) )
{
_isoc99_sscanf(i + 4, "(%s %s)", s1, s);
v6 = strlen(s);
if ( src[v6 + 95] == 41 )
src[v6 + 95] = 0;
if ( s[0] == 63 )
{
v7 = (unsigned int)((_DWORD)&workingMemory + 4);
do
{
if ( *(_DWORD *)(v7 - 4) )
{
_isoc99_sscanf(v7, "(%s %s)", v17, src);
v9 = strlen(src);
if ( v17[v9 + 95] == 41 )
v17[v9 + 95] = 0;
v8 = strlen(s1);
if ( !strncmp(s1, v17, v8) )
addToChain(src);
}
v7 += 96LL;
}
while ( v7 != (_DWORD)&workingMemory + 3844 );
}
}
v4 = 0;
v5 = (char *)&ruleSet[13 * a1];
while ( 1 )
{
v10 = *((_QWORD *)v5 + 11);
if ( v10 )
{
while ( 1 )
{
_isoc99_sscanf(v10 + 4, "(%s %s)", s1, s);
v11 = strlen(s);
if ( src[v11 + 95] == 41 )
src[v11 + 95] = 0;
v12 = strlen(s1);
v13 = 5LL;
if ( v12 <= 5 )
v13 = v12;
if ( !memcmp(s1, "true", v13) )
break;
if ( s[0] == 63 && chain )
strcpy(s, (const char *)chain + 4);
v4 = func0(s1, s);
if ( !v4 )
goto LABEL_25;
v10 = *(_QWORD *)(v10 + 88);
if ( !v10 )
goto LABEL_33;
}
v4 = 1;
LABEL_33:
for ( j = chain; chain; j = chain )
{
chain = (void *)j[11];
free(j);
}
strcpy(a2, s);
goto LABEL_27;
}
if ( v4 )
goto LABEL_33;
LABEL_25:
v14 = chain;
if ( !chain )
return 0;
chain = (void *)*((_QWORD *)chain + 11);
free(v14);
v4 = 0;
LABEL_27:
if ( !chain )
return v4;
}
}

original_code:
int checkPattern( int rule, char *arg )
{
  int ret=0;
  char term1[80 +1];
  char term2[80 +1];
  memoryElementType *antecedent = ruleSet[rule].antecedent;
  while (antecedent) {
    sscanf( antecedent->element, "(%s %s)", term1, term2);
    if (term2[strlen(term2)-1] == ')') term2[strlen(term2)-1] = 0;
    if (term2[0] == '?') {
      int i;
      char wm_term1[80 +1];
      char wm_term2[80 +1];
      for (i = 0 ; i < 40 ; i++) {
        if (workingMemory[i].active) {
          sscanf( workingMemory[i].element, "(%s %s)", wm_term1, wm_term2 );
          if (wm_term2[strlen(wm_term2)-1] == ')')
            wm_term2[strlen(wm_term2)-1] = 0;
          if (!strncmp(term1, wm_term1, strlen(term1))) addToChain(wm_term2);
        }
      }
    }
    antecedent = antecedent->next;
  }
  do {
    memoryElementType *curRulePtr, *temp;
    curRulePtr = ruleSet[rule].antecedent;
    while (curRulePtr) {
      sscanf( curRulePtr->element, "(%s %s)", term1, term2 );
      if (term2[strlen(term2)-1] == ')') term2[strlen(term2)-1] = 0;
      if (!strncmp( term1, "true", strlen(term1))) {
        ret = 1;
        break;
      } else {
        if ((term2[0] == '?') && (chain)) strcpy(term2, chain->element);
      }
      ret = searchWorkingMemory( term1, term2 );
      if (!ret) break;
      curRulePtr = curRulePtr->next;
    }
    if (ret) {
      while (chain) {
        temp = chain;
        chain = chain->next;
        free(temp);
      }
      strcpy(arg, term2);
    } else {
      if (chain) {
        temp = chain;
        chain = chain->next;
        free(temp);
      }
    }
  } while (chain);
  return ret;
}

canonical_original_code:
int checkPattern(int rule, char *arg)
{
  int ret = 0;
  char term1[-1];
  char term2[-1];
  struct memoryElementStruct *antecedent = ruleSet[rule].antecedent;
  while (antecedent) {
    sscanf( antecedent->element, "(%s %s)", term1, term2);
    if (term2[strlen(term2)-1] == ')') term2[strlen(term2)-1] = 0;
    if (term2[0] == '?') {
      int i;
      char wm_term1[-1];
      char wm_term2[-1];
      for (i = 0 ; i < 40 ; i++) {
        if (workingMemory[i].active) {
          sscanf( workingMemory[i].element, "(%s %s)", wm_term1, wm_term2 );
          if (wm_term2[strlen(wm_term2)-1] == ')')
            wm_term2[strlen(wm_term2)-1] = 0;
          if (!strncmp(term1, wm_term1, strlen(term1))) addToChain(wm_term2);
        }
      }
    }
    antecedent = antecedent->next;
  }
  do {
    struct memoryElementStruct *curRulePtr; struct memoryElementStruct *temp;
    curRulePtr = ruleSet[rule].antecedent;
    while (curRulePtr) {
      sscanf( curRulePtr->element, "(%s %s)", term1, term2 );
      if (term2[strlen(term2)-1] == ')') term2[strlen(term2)-1] = 0;
      if (!strncmp( term1, "true", strlen(term1))) {
        ret = 1;
        break;
      } else {
        if ((term2[0] == '?') && (chain)) strcpy(term2, chain->element);
      }
      ret = searchWorkingMemory( term1, term2 );
      if (!ret) break;
      curRulePtr = curRulePtr->next;
    }
    if (ret) {
      while (chain) {
        temp = chain;
        chain = chain->next;
        free(temp);
      }
      strcpy(arg, term2);
    } else {
      if (chain) {
        temp = chain;
        chain = chain->next;
        free(temp);
      }
    }
  } while (chain);
  return ret;
}

variable_types: rule int

variable_types:
arg
char *

variable_types: struct memoryElementStruct { int active; char element[-1]; struct memoryElementStruct *next; } *


checkRule

decompiled_code:
__int64 __fastcall checkRule(unsigned int a1)
{
__int64 result; // rax
__int64 v2[11]; // [rsp+0h] [rbp-58h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
v2[8] = 0LL;
v2[9] = 0LL;
result = checkPattern(a1, (char *)v2);
if ( (_DWORD)result == 1 )
result = fireRule(a1, v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 result; // rax
__int64 v2[11]; // [rsp+0h] [rbp-58h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
v2[8] = 0LL;
v2[9] = 0LL;
result = func1(a1, (char *)v2);
if ( (_DWORD)result == 1 )
result = fireRule(a1, v2);
return result;
}

original_code:
int checkRule( int rule )
{
  int fire = 0;
  char arg[80]={0};
  extern int fireRule( int, char * );
  fire = checkPattern(rule, arg);
  if (fire == 1) {
    fire = fireRule( rule, arg );
  }
  return fire;
}

canonical_original_code:
int checkRule(int rule)
{
  int fire = 0;
  char arg[80] = {0};
  extern int fireRule(int, char *);
  fire = checkPattern(rule, arg);
  if (fire == 1) {
    fire = fireRule( rule, arg );
  }
  return fire;
}

variable_types: fire int

variable_types:
arg
char [80]

variable_types:
fireRule
int (int, char *)


initQueue

decompiled_code:
void initQueue()
{
read = 0;
write = 0;
}

canonical_decompiled_code:
void func9()
{
read = 0;
write = 0;
}

original_code:
void initQueue( void )
{
  write = read = 0;
  return;
}

canonical_original_code:
void initQueue()
{
  write = read = 0;
  return;
}


enqueue

decompiled_code:
__int64 __fastcall enqueue(int a1)
{
int v1; // edx
__int64 result; // rax

v1 = write;
queue[write] = a1;
result = (unsigned int)(v1 + 1);
if ( v1 == 9 )
result = 0LL;
write = result;
return result;
}

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

v1 = write;
queue[write] = a1;
result = (unsigned int)(v1 + 1);
if ( v1 == 9 )
result = 0LL;
write = result;
return result;
}

original_code:
void enqueue( int elem )
{
  queue[write] = elem;
  write = ((write == (10)-1) ? 0 : write+1);
  return;
}

canonical_original_code:
void enqueue(int elem)
{
  queue[write] = elem;
  write = ((write == (10)-1) ? 0 : write+1);
  return;
}


dequeue

decompiled_code:
__int64 dequeue()
{
int v0; // eax
unsigned int v1; // ecx

v0 = empty();
if ( v0 )
__assert_fail("!empty()", "bfs.c", 0x48u, "dequeue");
v1 = queue[read];
if ( read != 9 )
v0 = read + 1;
read = v0;
return v1;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // eax
unsigned int v1; // ecx

v0 = func10();
if ( v0 )
__assert_fail("!empty()", "bfs.c", 0x48u, "dequeue");
v1 = queue[read];
if ( read != 9 )
v0 = read + 1;
read = v0;
return v1;
}

original_code:
int dequeue( void )
{
  int elem;
  ((void) sizeof ((!empty()) ? 1 : 0), __extension__ ({ if (!empty()) ; else __assert_fail ("!empty()", "bfs.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  elem = queue[read];
  read = ((read == (10)-1) ? 0 : read+1);
  return elem;
}

canonical_original_code:
int dequeue()
{
  int elem;
  ((void) sizeof ((!empty()) ? 1 : 0), __extension__ ({ if (!empty()) ; else __assert_fail ("!empty()", "bfs.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  elem = queue[read];
  read = ((read == (10)-1) ? 0 : read+1);
  return elem;
}


initializeSolution

decompiled_code:
__int64 __fastcall initializeSolution(__int64 a1)
{
__int64 i; // rax
int v2; // ebx
__int64 result; // rax

for ( i = 0LL; i != 30; ++i )
*(_DWORD *)(a1 + 4 * i) = i;
v2 = 30;
do
{
result = tweakSolution(a1);
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 i; // rax
int v2; // ebx
__int64 result; // rax

for ( i = 0LL; i != 30; ++i )
*(_DWORD *)(a1 + 4 * i) = i;
v2 = 30;
do
{
result = func7(a1);
--v2;
}
while ( v2 );
return result;
}

original_code:
void initializeSolution( memberType *member )
{
  int i;
  for (i = 0 ; i < 30 ; i++) {
    member->solution[i] = i;
  }
  for (i = 0 ; i < 30 ; i++) {
    tweakSolution( member );
  }
}

canonical_original_code:
void initializeSolution(struct memberType *member)
{
  int i;
  for (i = 0 ; i < 30 ; i++) {
    member->solution[i] = i;
  }
  for (i = 0 ; i < 30 ; i++) {
    tweakSolution( member );
  }
}

variable_types: struct memberType { int solution[30]; float energy; } *


tweakSolution

decompiled_code:
__int64 __fastcall tweakSolution(__int64 a1)
{
int v1; // ebp
__int64 result; // rax
int v3; // edx

v1 = (int)((float)((float)rand() * 30.0) * 4.656612873077393e-10);
do
LODWORD(result) = (int)((float)((float)rand() * 30.0) * 4.656612873077393e-10);
while ( v1 == (_DWORD)result );
v3 = *(_DWORD *)(a1 + 4LL * v1);
result = (int)result;
*(_DWORD *)(a1 + 4LL * v1) = *(_DWORD *)(a1 + 4LL * (int)result);
*(_DWORD *)(a1 + 4LL * (int)result) = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
int v1; // ebp
__int64 result; // rax
int v3; // edx

v1 = (int)((float)((float)rand() * 30.0) * 4.656612873077393e-10);
do
LODWORD(result) = (int)((float)((float)rand() * 30.0) * 4.656612873077393e-10);
while ( v1 == (_DWORD)result );
v3 = *(_DWORD *)(a1 + 4LL * v1);
result = (int)result;
*(_DWORD *)(a1 + 4LL * v1) = *(_DWORD *)(a1 + 4LL * (int)result);
*(_DWORD *)(a1 + 4LL * (int)result) = v3;
return result;
}

original_code:
void tweakSolution( memberType *member )
{
  int temp, x, y;
  x = (int)((float)(30)*rand()/(2147483647 +1.0));
  do {
    y = (int)((float)(30)*rand()/(2147483647 +1.0));
  } while (x == y);
  temp = member->solution[x];
  member->solution[x] = member->solution[y];
  member->solution[y] = temp;
}

canonical_original_code:
void tweakSolution(struct memberType *member)
{
  int temp; int x; int y;
  x = (int)((float)(30)*rand()/(2147483647 +1.0));
  do {
    y = (int)((float)(30)*rand()/(2147483647 +1.0));
  } while (x == y);
  temp = member->solution[x];
  member->solution[x] = member->solution[y];
  member->solution[y] = temp;
}

variable_types: temp int

variable_types: x int

variable_types: y int

user_defined_types: struct memberType { int solution[30]; float energy; }


emitSolution

original_code:
void emitSolution( memberType *member )
{
  char board[30][30];
  int x, y;
  bzero( (void *)board, 30 * 30 );
  for (x = 0 ; x < 30 ; x++) {
    board[x][member->solution[x]] = 'Q';
  }
  printf("board:\n");
  for (y = 0 ; y < 30 ; y++) {
    for (x = 0 ; x < 30 ; x++) {
      if (board[x][y] == 'Q') printf("Q ");
      else printf(". ");
    }
    printf("\n");
  }
  printf("\n\n");
}

canonical_original_code:
void emitSolution(struct memberType *member)
{
  char board[30][30];
  int x; int y;
  bzero( (void *)board, 30 * 30 );
  for (x = 0 ; x < 30 ; x++) {
    board[x][member->solution[x]] = 'Q';
  }
  printf("board:\n");
  for (y = 0 ; y < 30 ; y++) {
    for (x = 0 ; x < 30 ; x++) {
      if (board[x][y] == 'Q') printf("Q ");
      else printf(". ");
    }
    printf("\n");
  }
  printf("\n\n");
}

variable_types:
board
char [30][30]


computeEnergy

original_code:
void computeEnergy( memberType *member )
{
  int i, j, x, y, tempx, tempy;
  char board[30][30];
  int conflicts;
  const int dx[4] = {-1, 1, -1, 1};
  const int dy[4] = {-1, 1, 1, -1};
  bzero( (void *)board, 30 * 30 );
  for (i = 0 ; i < 30 ; i++) {
    board[i][member->solution[i]] = 'Q';
  }
  conflicts = 0;
  for (i = 0 ; i < 30 ; i++) {
    x = i; y = member->solution[i];
    for (j = 0 ; j < 4 ; j++) {
      tempx = x ; tempy = y;
      while(1) {
        tempx += dx[j]; tempy += dy[j];
        if ((tempx < 0) || (tempx >= 30) ||
               (tempy < 0) || (tempy >= 30)) break;
        if (board[tempx][tempy] == 'Q') conflicts++;
      }
    }
  }
  member->energy = (float)conflicts;
}

canonical_original_code:
void computeEnergy(struct memberType *member)
{
  int i; int j; int x; int y; int tempx; int tempy;
  char board[30][30];
  int conflicts;
  int dx[4] = {-1, 1, -1, 1};
  int dy[4] = {-1, 1, 1, -1};
  bzero( (void *)board, 30 * 30 );
  for (i = 0 ; i < 30 ; i++) {
    board[i][member->solution[i]] = 'Q';
  }
  conflicts = 0;
  for (i = 0 ; i < 30 ; i++) {
    x = i; y = member->solution[i];
    for (j = 0 ; j < 4 ; j++) {
      tempx = x ; tempy = y;
      while(1) {
        tempx += dx[j]; tempy += dy[j];
        if ((tempx < 0) || (tempx >= 30) ||
               (tempy < 0) || (tempy >= 30)) break;
        if (board[tempx][tempy] == 'Q') conflicts++;
      }
    }
  }
  member->energy = (float)conflicts;
}

variable_types: i int

variable_types: j int

variable_types: tempx int

variable_types: tempy int

variable_types: conflicts int

variable_types:
dx
int [4]

variable_types:
dy
int [4]


interpret

decompiled_code:
void interpret()
{
char *v0; // rbp
unsigned int v1; // ebx

v0 = ruleSet;
v1 = 0;
while ( !*(_DWORD *)v0 || !(unsigned int)checkRule(v1) )
{
++v1;
v0 += 104;
if ( v1 == 40 )
return;
}
if ( debug )
printf("Fired rule %s (%d)\n", &ruleSet[104 * v1 + 4], v1);
}

canonical_decompiled_code:
void func0()
{
char *v0; // rbp
unsigned int v1; // ebx

v0 = ruleSet;
v1 = 0;
while ( !*(_DWORD *)v0 || !(unsigned int)checkRule(v1) )
{
++v1;
v0 += 104;
if ( v1 == 40 )
return;
}
if ( debug )
printf("Fired rule %s (%d)\n", &ruleSet[104 * v1 + 4], v1);
}

original_code:
void interpret( void )
{
  int rule;
  int fired = 0;
  extern int checkRule( int );
  extern int debug;
  for (rule = 0 ; rule < 40 ; rule++) {
    fired = 0;
    if (ruleSet[rule].active) {
      fired = checkRule( rule );
      if (fired) break;
    }
  }
  if (debug) {
    if (fired) printf("Fired rule %s (%d)\n", ruleSet[rule].ruleName, rule);
  }
  return;
}

canonical_original_code:
void interpret()
{
  int rule;
  int fired = 0;
  extern int checkRule(int);
  extern int debug;
  for (rule = 0 ; rule < 40 ; rule++) {
    fired = 0;
    if (ruleSet[rule].active) {
      fired = checkRule( rule );
      if (fired) break;
    }
  }
  if (debug) {
    if (fired) printf("Fired rule %s (%d)\n", ruleSet[rule].ruleName, rule);
  }
  return;
}

variable_types: rule int

variable_types: fired int

variable_types:
checkRule
int (int)

variable_types: debug int


grabResponse

decompiled_code:
__int64 __fastcall grabResponse(int a1, __int64 a2)
{
int v2; // er12
int v3; // ebx
_BYTE *v4; // rbp
__int64 v5; // r13
int v6; // edx
int v7; // ecx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v2 = 0;
v3 = 0;
do
{
v5 = v2;
v4 = (_BYTE *)(a2 + v2);
v6 = read(a1, v4, 2048 - v2);
v7 = v2;
v2 += v6;
if ( v2 > v7 )
{
do
{
if ( v3 )
{
switch ( v3 )
{
case 1:
v3 = 2 * (*v4 == 10);
break;
case 2:
v3 = 3 * (*v4 == 13);
break;
case 3:
if ( *v4 == 10 )
goto LABEL_17;
v3 = 0;
break;
default:
v3 = 0;
break;
}
}
else
{
v3 = *v4 == 13;
}
++v4;
}
while ( v4 != (_BYTE *)((unsigned int)(v2 + ~v7) + a2 + v5 + 1) );
}
}
while ( v2 + v6 <= 1968 );
LABEL_17:
result = (unsigned int)(v2 - 3);
*(_BYTE *)(a2 + (int)result) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
int v2; // er12
int v3; // ebx
_BYTE *v4; // rbp
__int64 v5; // r13
int v6; // edx
int v7; // ecx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v2 = 0;
v3 = 0;
do
{
v5 = v2;
v4 = (_BYTE *)(a2 + v2);
v6 = read(a1, v4, 2048 - v2);
v7 = v2;
v2 += v6;
if ( v2 > v7 )
{
do
{
if ( v3 )
{
switch ( v3 )
{
case 1:
v3 = 2 * (*v4 == 10);
break;
case 2:
v3 = 3 * (*v4 == 13);
break;
case 3:
if ( *v4 == 10 )
goto LABEL_17;
v3 = 0;
break;
default:
v3 = 0;
break;
}
}
else
{
v3 = *v4 == 13;
}
++v4;
}
while ( v4 != (_BYTE *)((unsigned int)(v2 + ~v7) + a2 + v5 + 1) );
}
}
while ( v2 + v6 <= 1968 );
LABEL_17:
result = (unsigned int)(v2 - 3);
*(_BYTE *)(a2 + (int)result) = 0;
return result;
}

original_code:
int grabResponse( int sock, char *buf )
{
  int i, len, stop, state, bufIdx;
  if (buf == ((void *)0)) return -1;
  len = bufIdx = state = stop = 0;
  while (!stop) {
    if (bufIdx+len > 2048 - 80) break;
    len = read( sock, &buf[bufIdx], (2048 -bufIdx) );
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (buf[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (buf[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (buf[i] == 0x0d) ) state = 3;
      else if ( (state == 3) && (buf[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += len;
  }
  bufIdx -= 3;
  buf[bufIdx] = 0;
  return bufIdx;
}

canonical_original_code:
int grabResponse(int sock, char *buf)
{
  int i; int len; int stop; int state; int bufIdx;
  if (buf == ((void *)0)) return -1;
  len = bufIdx = state = stop = 0;
  while (!stop) {
    if (bufIdx+len > 2048 - 80) break;
    len = read( sock, &buf[bufIdx], (2048 -bufIdx) );
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (buf[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (buf[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (buf[i] == 0x0d) ) state = 3;
      else if ( (state == 3) && (buf[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += len;
  }
  bufIdx -= 3;
  buf[bufIdx] = 0;
  return bufIdx;
}

variable_types: sock int

variable_types:
buf
char *

variable_types: len int

variable_types: stop int

variable_types: state int

variable_types: bufIdx int


monitorSite

decompiled_code:
__int64 __fastcall monitorSite(int a1)
{
int v1; // ebp
unsigned int v2; // ebx
struct hostent *v4; // rax
size_t v5; // rax
int v6; // er13
int v7; // er13
__int64 v8; // rax
char v9; // [rsp+Ch] [rbp-89Ch] BYREF
char cp[80]; // [rsp+10h] [rbp-898h] BYREF
char s[2064]; // [rsp+60h] [rbp-848h] BYREF
struct sockaddr addr; // [rsp+870h] [rbp-38h] BYREF

v1 = socket(2, 1, 0);
prune((char *)&monitors + 140 * a1 + 4, (__int64)cp);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(80);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
v4 = gethostbyname(cp);
if ( !v4 )
{
close(v1);
return (unsigned int)-1;
}
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v4->h_addr_list;
}
v2 = connect(v1, &addr, 0x10u);
if ( v2 )
{
v2 = 0;
}
else
{
strcpy(s, "GET / HTTP/1.0\n\n");
v5 = strlen(s);
v6 = write(v1, s, v5);
if ( v6 == strlen(s) )
{
v7 = grabResponse(v1, (__int64)s);
if ( strstr(s, "Content-Length:") )
{
_isoc99_sscanf(s, "Content-Length: %d", &v9);
if ( *((_DWORD *)&monitors + 35 * a1 + 32) != v7 )
{
v8 = 140LL * a1;
*(_DWORD *)((char *)&monitors + v8 + 136) = 0;
*(_DWORD *)((char *)&monitors + v8 + 132) = 1;
*(_DWORD *)((char *)&monitors + v8 + 128) = v7;
v2 = 1;
}
}
}
}
close(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v1; // ebp
unsigned int v2; // ebx
struct hostent *v4; // rax
size_t v5; // rax
int v6; // er13
int v7; // er13
__int64 v8; // rax
char v9; // [rsp+Ch] [rbp-89Ch] BYREF
char cp[80]; // [rsp+10h] [rbp-898h] BYREF
char s[2064]; // [rsp+60h] [rbp-848h] BYREF
struct sockaddr addr; // [rsp+870h] [rbp-38h] BYREF

v1 = socket(2, 1, 0);
func1((char *)&monitors + 140 * a1 + 4, (__int64)cp);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(80);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
v4 = gethostbyname(cp);
if ( !v4 )
{
close(v1);
return (unsigned int)-1;
}
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v4->h_addr_list;
}
v2 = connect(v1, &addr, 0x10u);
if ( v2 )
{
v2 = 0;
}
else
{
strcpy(s, "GET / HTTP/1.0\n\n");
v5 = strlen(s);
v6 = write(v1, s, v5);
if ( v6 == strlen(s) )
{
v7 = func2(v1, (__int64)s);
if ( strstr(s, "Content-Length:") )
{
_isoc99_sscanf(s, "Content-Length: %d", &v9);
if ( *((_DWORD *)&monitors + 35 * a1 + 32) != v7 )
{
v8 = 140LL * a1;
*(_DWORD *)((char *)&monitors + v8 + 136) = 0;
*(_DWORD *)((char *)&monitors + v8 + 132) = 1;
*(_DWORD *)((char *)&monitors + v8 + 128) = v7;
v2 = 1;
}
}
}
}
close(v1);
return v2;
}

original_code:
int monitorSite( int siteIndex )
{
  int ret=0, sock, result, len;
  struct sockaddr_in servaddr;
  char buffer[2048 +1];
  char fqdn[80];
  extern monitorEntryType monitors[];
  sock = socket(2, SOCK_STREAM, 0);
  prune( monitors[siteIndex].url, fqdn );
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_port = htons( 80 );
  servaddr.sin_addr.s_addr = inet_addr( fqdn );
  if ( servaddr.sin_addr.s_addr == 0xffffffff ) {
    struct hostent *hptr =
      (struct hostent *)gethostbyname( fqdn );
    if ( hptr == ((void *)0) ) {
      close(sock);
      return -1;
    } else {
      struct in_addr **addrs;
      addrs = (struct in_addr **)hptr->h_addr_list;
      memcpy( &servaddr.sin_addr, *addrs, sizeof(struct in_addr) );
    }
  }
  result = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (result == 0) {
    strcpy(buffer, "GET / HTTP/1.0\n\n");
    len = write(sock, buffer, strlen(buffer) );
    if ( len == strlen(buffer) ) {
      char *cur;
      len = grabResponse( sock, buffer );
      cur = strstr(buffer, "Content-Length:");
      if (cur != ((void *)0)) {
        int curLen;
        sscanf(buffer, "Content-Length: %d", &curLen);
        if (len != monitors[siteIndex].length) {
          monitors[siteIndex].shown = 0;
          monitors[siteIndex].changed = 1;
          monitors[siteIndex].length = len;
          ret = 1;
        }
      }
    }
  }
  close(sock);
  return(ret);
}

canonical_original_code:
int monitorSite(int siteIndex)
{
  int ret = 0; int sock; int result; int len;
  struct sockaddr_in servaddr;
  char buffer[-1];
  char fqdn[80];
  extern struct monitorEntryType monitors[];
  sock = socket(2, SOCK_STREAM, 0);
  prune( monitors[siteIndex].url, fqdn );
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_port = htons( 80 );
  servaddr.sin_addr.s_addr = inet_addr( fqdn );
  if ( servaddr.sin_addr.s_addr == 0xffffffff ) {
    struct hostent *hptr = (struct hostent *)gethostbyname( fqdn );
    if ( hptr == ((void *)0) ) {
      close(sock);
      return -1;
    } else {
      struct in_addr **addrs;
      addrs = (struct in_addr **)hptr->h_addr_list;
      memcpy( &servaddr.sin_addr, *addrs, sizeof(struct in_addr) );
    }
  }
  result = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (result == 0) {
    strcpy(buffer, "GET / HTTP/1.0\n\n");
    len = write(sock, buffer, strlen(buffer) );
    if ( len == strlen(buffer) ) {
      char *cur;
      len = grabResponse( sock, buffer );
      cur = strstr(buffer, "Content-Length:");
      if (cur != ((void *)0)) {
        int curLen;
        sscanf(buffer, "Content-Length: %d", &curLen);
        if (len != monitors[siteIndex].length) {
          monitors[siteIndex].shown = 0;
          monitors[siteIndex].changed = 1;
          monitors[siteIndex].length = len;
          ret = 1;
        }
      }
    }
  }
  close(sock);
  return(ret);
}

variable_types: siteIndex int

variable_types: ret int

variable_types: result int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types:
buffer
char [-1]

variable_types:
fqdn
char [80]

variable_types: struct monitorEntryType { int active; char url[80]; char urlName[-1]; int length; int changed; int shown; } []

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: struct in_addr { unsigned int s_addr; } **

variable_types:
cur
char *

variable_types: curLen int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


tnamecmp

repo: levaidaniel/ksh-openbsd

decompiled_code:
int __fastcall tnamecmp(__int64 a1, __int64 a2)
{
return strcmp((const char *)(*(_QWORD *)a1 + 40LL), (const char *)(*(_QWORD *)a2 + 40LL));
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
return strcmp((const char *)(*(_QWORD *)a1 + 40LL), (const char *)(*(_QWORD *)a2 + 40LL));
}

original_code:
static int
tnamecmp(const void *p1, const void *p2)
{
 char *name1 = (*(struct tbl **)p1)->name;
 char *name2 = (*(struct tbl **)p2)->name;
 return strcmp(name1, name2);
}

canonical_original_code:
static int tnamecmp(void *p1, void *p2)
{
 char *name1 = (*(struct tbl **)p1)->name;
 char *name2 = (*(struct tbl **)p2)->name;
 return strcmp(name1, name2);
}


hash

decompiled_code:
__int64 __fastcall hash(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
++a1;
v2 = v1 + 33 * v2;
v1 = *a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
++a1;
v2 = v1 + 33 * v2;
v1 = *a1;
}
while ( *a1 );
return v2;
}

original_code:
unsigned int
hash(const char *n)
{
 unsigned int h = 0;
 while (*n != '\0')
  h = 33*h + (unsigned char)(*n++);
 return h;
}

canonical_original_code:
unsigned int hash(char *n)
{
 unsigned int h = 0;
 while (*n != '\0')
  h = 33*h + (unsigned char)(*n++);
 return h;
}

variable_types:
n
char *

variable_types:
h
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall print_ulimit(__int64 a1, char a2)
{
rlim_t v2; // rax
struct rlimit v4; // [rsp+0h] [rbp-28h] BYREF

getrlimit(*(__rlimit_resource_t *)(a1 + 8), &v4);
v2 = v4.rlim_cur;
if ( (a2 & 1) == 0 )
{
if ( (a2 & 2) == 0 )
{
v2 = 0LL;
return shprintf("%li\n", v2 / *(int *)(a1 + 12));
}
v2 = v4.rlim_max;
}
if ( v2 != -1LL )
return shprintf("%li\n", v2 / *(int *)(a1 + 12));
return shprintf("unlimited\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2)
{
rlim_t v2; // rax
struct rlimit v4; // [rsp+0h] [rbp-28h] BYREF

getrlimit(*(__rlimit_resource_t *)(a1 + 8), &v4);
v2 = v4.rlim_cur;
if ( (a2 & 1) == 0 )
{
if ( (a2 & 2) == 0 )
{
v2 = 0LL;
return shprintf("%li\n", v2 / *(int *)(a1 + 12));
}
v2 = v4.rlim_max;
}
if ( v2 != -1LL )
return shprintf("%li\n", v2 / *(int *)(a1 + 12));
return shprintf("unlimited\n");
}

original_code:
static void
print_ulimit(const struct limits *l, int how)
{
 rlim_t val = 0;
 struct rlimit limit;
 getrlimit(l->resource, &limit);
 if (how & 0x1)
  val = limit.rlim_cur;
 else if (how & 0x2)
  val = limit.rlim_max;
 if (val == ((__rlim_t) -1))
  shprintf("unlimited\n");
 else {
  val /= l->factor;
  shprintf("%" "l" "i" "\n", (int64_t) val);
 }
}

canonical_original_code:
static void print_ulimit(struct limits *l, int how)
{
 unsigned long val = 0;
 struct rlimit limit;
 getrlimit(l->resource, &limit);
 if (how & 0x1)
  val = limit.rlim_cur;
 else if (how & 0x2)
  val = limit.rlim_max;
 if (val == ((__rlim_t) -1))
  shprintf("unlimited\n");
 else {
  val /= l->factor;
  shprintf("%" "l" "i" "\n", (long) val);
 }
}

variable_types: struct limits { char *name; int resource; int factor; char option; } *

variable_types: how int

variable_types:
val
unsigned long

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }


decompiled_code:
__int64 __fastcall set_ulimit(__int64 a1, const char *a2, char a3)
{
__int64 v4; // r13
rlim_t v5; // rbp
unsigned int v6; // ebp
char *v8; // rax
__int64 v9; // [rsp+8h] [rbp-40h] BYREF
struct rlimit rlimits; // [rsp+10h] [rbp-38h] BYREF

if ( !strcmp(a2, "unlimited") )
{
v5 = -1LL;
goto LABEL_9;
}
if ( !(unsigned int)evaluate(a2, &v9, 1LL, 0LL) )
return 1;
v4 = v9;
if ( !v9 && ((*_ctype_b_loc())[*(unsigned __int8 *)a2] & 0x800) == 0 )
{
bi_errorf("invalid limit: %s", a2);
return 1;
}
v5 = v4 * *(int *)(a1 + 12);
LABEL_9:
getrlimit(*(__rlimit_resource_t *)(a1 + 8), &rlimits);
if ( (a3 & 1) != 0 )
rlimits.rlim_cur = v5;
if ( (a3 & 2) != 0 )
rlimits.rlim_max = v5;
v6 = 0;
if ( setrlimit(*(__rlimit_resource_t *)(a1 + 8), &rlimits) == -1 )
{
v6 = *_errno_location();
if ( v6 == 1 )
{
bi_errorf("-%c exceeds allowable limit", (unsigned int)*(char *)(a1 + 16));
}
else
{
v8 = strerror(v6);
bi_errorf("bad -%c limit: %s", (unsigned int)*(char *)(a1 + 16), v8);
v6 = 1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, char a3)
{
__int64 v4; // r13
rlim_t v5; // rbp
unsigned int v6; // ebp
char *v8; // rax
__int64 v9; // [rsp+8h] [rbp-40h] BYREF
struct rlimit rlimits; // [rsp+10h] [rbp-38h] BYREF

if ( !strcmp(a2, "unlimited") )
{
v5 = -1LL;
goto LABEL_9;
}
if ( !(unsigned int)evaluate(a2, &v9, 1LL, 0LL) )
return 1;
v4 = v9;
if ( !v9 && ((*_ctype_b_loc())[*(unsigned __int8 *)a2] & 0x800) == 0 )
{
bi_errorf("invalid limit: %s", a2);
return 1;
}
v5 = v4 * *(int *)(a1 + 12);
LABEL_9:
getrlimit(*(__rlimit_resource_t *)(a1 + 8), &rlimits);
if ( (a3 & 1) != 0 )
rlimits.rlim_cur = v5;
if ( (a3 & 2) != 0 )
rlimits.rlim_max = v5;
v6 = 0;
if ( setrlimit(*(__rlimit_resource_t *)(a1 + 8), &rlimits) == -1 )
{
v6 = *_errno_location();
if ( v6 == 1 )
{
bi_errorf("-%c exceeds allowable limit", (unsigned int)*(char *)(a1 + 16));
}
else
{
v8 = strerror(v6);
bi_errorf("bad -%c limit: %s", (unsigned int)*(char *)(a1 + 16), v8);
v6 = 1;
}
}
return v6;
}

original_code:
static int
set_ulimit(const struct limits *l, const char *v, int how)
{
 rlim_t val = 0;
 struct rlimit limit;
 if (strcmp(v, "unlimited") == 0)
  val = ((__rlim_t) -1);
 else {
  int64_t rval;
  if (!evaluate(v, &rval, 0x1, 0))
   return 1;
  if (!rval && !((*__ctype_b_loc ())[(int) (((unsigned char)(v[0])))] & (unsigned short int) _ISdigit)) {
   bi_errorf("invalid limit: %s", v);
   return 1;
  }
  val = (rlim_t)rval * l->factor;
 }
 getrlimit(l->resource, &limit);
 if (how & 0x1)
  limit.rlim_cur = val;
 if (how & 0x2)
  limit.rlim_max = val;
 if (setrlimit(l->resource, &limit) == -1) {
  if ((*__errno_location ()) == 1)
   bi_errorf("-%c exceeds allowable limit", l->option);
  else
   bi_errorf("bad -%c limit: %s", l->option,
       strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}

canonical_original_code:
static int set_ulimit(struct limits *l, char *v, int how)
{
 unsigned long val = 0;
 struct rlimit limit;
 if (strcmp(v, "unlimited") == 0)
  val = ((__rlim_t) -1);
 else {
  long rval;
  if (!evaluate(v, &rval, 0x1, 0))
   return 1;
  if (!rval && !((*__ctype_b_loc ())[(int) (((unsigned char)(v[0])))] & (unsigned short) _ISdigit)) {
   bi_errorf("invalid limit: %s", v);
   return 1;
  }
  val = (unsigned long)rval * l->factor;
 }
 getrlimit(l->resource, &limit);
 if (how & 0x1)
  limit.rlim_cur = val;
 if (how & 0x2)
  limit.rlim_max = val;
 if (setrlimit(l->resource, &limit) == -1) {
  if ((*__errno_location ()) == 1)
   bi_errorf("-%c exceeds allowable limit", l->option);
  else
   bi_errorf("bad -%c limit: %s", l->option,
       strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}

variable_types:
v
char *

variable_types: rval long

user_defined_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

user_defined_types: struct limits { char *name; int resource; int factor; char option; }


decompiled_code:
_BOOL8 __fastcall c_ulimit(__int64 a1)
{
_BOOL4 v2; // ebx
char v3; // bp
int v4; // eax
char **v6; // rdi
const char *v7; // rdx
int v8; // eax
const char *v9; // rsi
char **v10; // rbx
const char *v11; // rsi
const char *v12; // rsi

v2 = 0;
v3 = 3;
while ( 1 )
{
v4 = ksh_getopt(a1, &builtin_opt, "HSat#f#c#d#s#l#m#n#p#");
if ( v4 == -1 )
break;
if ( v4 == 83 )
{
v3 = 1;
}
else if ( v4 > 83 )
{
if ( v4 == 97 )
v2 = 1;
}
else
{
if ( v4 == 63 )
return 1;
if ( v4 == 72 )
v3 = 2;
}
}
if ( *(_QWORD *)(a1 + 8LL * builtin_opt) )
{
bi_errorf("usage: ulimit [-acdfHlmnpSst] [value]");
v2 = 1;
}
else
{
ksh_getopt_reset(&builtin_opt, 1LL);
while ( 1 )
{
do
{
v8 = ksh_getopt(a1, &builtin_opt, "HSat#f#c#d#s#l#m#n#p#");
if ( v8 == -1 )
{
if ( v2 )
{
v10 = &limits_1;
v11 = "time(cpu-seconds)";
do
{
shprintf("%-20s ", v11);
print_ulimit((__int64)v10, v3);
v10 += 3;
v11 = *v10;
}
while ( *v10 );
v2 = 0;
}
else if ( builtin_opt == 1 )
{
v12 = *(const char **)(a1 + 8);
if ( v12 )
v2 = set_ulimit((__int64)&off_4D8, v12, v3) != 0;
else
print_ulimit((__int64)&off_4D8, v3);
}
return v2;
}
}
while ( v8 == 72 );
if ( v8 <= 72 )
break;
if ( v8 != 83 )
{
v6 = &limits_1;
v7 = "time(cpu-seconds)";
if ( v8 != 97 )
{
LABEL_18:
while ( *((char *)v6 + 16) != v8 )
{
v6 += 3;
v7 = *v6;
if ( !*v6 )
goto LABEL_20;
}
if ( !v7 )
{
LABEL_20:
internal_warningf("%s: %c", "c_ulimit", (unsigned int)v8);
return 1;
}
v9 = (const char *)*((_QWORD *)&builtin_opt + 1);
if ( v9 )
{
if ( (unsigned int)set_ulimit((__int64)v6, v9, v3) )
return 1;
}
else
{
print_ulimit((__int64)v6, v3);
}
}
}
}
if ( v8 != 63 )
{
v6 = &limits_1;
v7 = "time(cpu-seconds)";
goto LABEL_18;
}
v2 = 1;
}
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1)
{
_BOOL4 v2; // ebx
char v3; // bp
int v4; // eax
char **v6; // rdi
const char *v7; // rdx
int v8; // eax
const char *v9; // rsi
char **v10; // rbx
const char *v11; // rsi
const char *v12; // rsi

v2 = 0;
v3 = 3;
while ( 1 )
{
v4 = ksh_getopt(a1, &builtin_opt, "HSat#f#c#d#s#l#m#n#p#");
if ( v4 == -1 )
break;
if ( v4 == 83 )
{
v3 = 1;
}
else if ( v4 > 83 )
{
if ( v4 == 97 )
v2 = 1;
}
else
{
if ( v4 == 63 )
return 1;
if ( v4 == 72 )
v3 = 2;
}
}
if ( *(_QWORD *)(a1 + 8LL * builtin_opt) )
{
bi_errorf("usage: ulimit [-acdfHlmnpSst] [value]");
v2 = 1;
}
else
{
ksh_getopt_reset(&builtin_opt, 1LL);
while ( 1 )
{
do
{
v8 = ksh_getopt(a1, &builtin_opt, "HSat#f#c#d#s#l#m#n#p#");
if ( v8 == -1 )
{
if ( v2 )
{
v10 = &limits_1;
v11 = "time(cpu-seconds)";
do
{
shprintf("%-20s ", v11);
func0((__int64)v10, v3);
v10 += 3;
v11 = *v10;
}
while ( *v10 );
v2 = 0;
}
else if ( builtin_opt == 1 )
{
v12 = *(const char **)(a1 + 8);
if ( v12 )
v2 = func1((__int64)&off_4D8, v12, v3) != 0;
else
func0((__int64)&off_4D8, v3);
}
return v2;
}
}
while ( v8 == 72 );
if ( v8 <= 72 )
break;
if ( v8 != 83 )
{
v6 = &limits_1;
v7 = "time(cpu-seconds)";
if ( v8 != 97 )
{
LABEL_18:
while ( *((char *)v6 + 16) != v8 )
{
v6 += 3;
v7 = *v6;
if ( !*v6 )
goto LABEL_20;
}
if ( !v7 )
{
LABEL_20:
internal_warningf("%s: %c", "c_ulimit", (unsigned int)v8);
return 1;
}
v9 = (const char *)*((_QWORD *)&builtin_opt + 1);
if ( v9 )
{
if ( (unsigned int)func1((__int64)v6, v9, v3) )
return 1;
}
else
{
func0((__int64)v6, v3);
}
}
}
}
if ( v8 != 63 )
{
v6 = &limits_1;
v7 = "time(cpu-seconds)";
goto LABEL_18;
}
v2 = 1;
}
return v2;
}

original_code:
int
c_ulimit(char **wp)
{
 static const struct limits limits[] = {
  { "time(cpu-seconds)", RLIMIT_CPU, 1, 't' },
  { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
  { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
  { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
  { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
  { "lockedmem(kbytes)", __RLIMIT_MEMLOCK, 1024, 'l' },
  { "memory(kbytes)", __RLIMIT_RSS, 1024, 'm' },
  { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
  { "processes", __RLIMIT_NPROC, 1, 'p' },
  { ((void *)0) }
 };
 const char *options = "HSat#f#c#d#s#l#m#n#p#";
 int how = 0x1 | 0x2;
 const struct limits *l;
 int optc, all = 0;
 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
  switch (optc) {
  case 'H':
   how = 0x2;
   break;
  case 'S':
   how = 0x1;
   break;
  case 'a':
   all = 1;
   break;
  case '?':
   return 1;
  default:
   break;
  }
 if (wp[builtin_opt.optind] != ((void *)0)) {
  bi_errorf("usage: ulimit [-acdfHlmnpSst] [value]");
  return 1;
 }
 ksh_getopt_reset(&builtin_opt, (1<<(0)));
 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
  switch (optc) {
  case 'a':
  case 'H':
  case 'S':
   break;
  case '?':
   return 1;
  default:
   for (l = limits; l->name && l->option != optc; l++)
    ;
   if (!l->name) {
    internal_warningf("%s: %c", __func__, optc);
    return 1;
   }
   if (builtin_opt.optarg) {
    if (set_ulimit(l, builtin_opt.optarg, how))
     return 1;
   } else
    print_ulimit(l, how);
   break;
  }
 wp += builtin_opt.optind;
 if (all) {
  for (l = limits; l->name; l++) {
   shprintf("%-20s ", l->name);
   print_ulimit(l, how);
  }
 } else if (builtin_opt.optind == 1) {
  l = &limits[1];
  if (wp[0] != ((void *)0)) {
   if (set_ulimit(l, wp[0], how))
    return 1;
   wp++;
  } else {
   print_ulimit(l, how);
  }
 }
 return 0;
}

canonical_original_code:
int c_ulimit(char **wp)
{
 static struct limits limits[] = {
  { "time(cpu-seconds)", RLIMIT_CPU, 1, 't' },
  { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
  { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
  { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
  { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
  { "lockedmem(kbytes)", __RLIMIT_MEMLOCK, 1024, 'l' },
  { "memory(kbytes)", __RLIMIT_RSS, 1024, 'm' },
  { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
  { "processes", __RLIMIT_NPROC, 1, 'p' },
  { ((void *)0) }
 };
 char *options = "HSat#f#c#d#s#l#m#n#p#";
 int how = 0x1 | 0x2;
 struct limits *l;
 int optc; int all = 0;
 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
  switch (optc) {
  case 'H':
   how = 0x2;
   break;
  case 'S':
   how = 0x1;
   break;
  case 'a':
   all = 1;
   break;
  case '?':
   return 1;
  default:
   break;
  }
 if (wp[builtin_opt.optind] != ((void *)0)) {
  bi_errorf("usage: ulimit [-acdfHlmnpSst] [value]");
  return 1;
 }
 ksh_getopt_reset(&builtin_opt, (1<<(0)));
 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
  switch (optc) {
  case 'a':
  case 'H':
  case 'S':
   break;
  case '?':
   return 1;
  default:
   for (l = limits; l->name && l->option != optc; l++)
    ;
   if (!l->name) {
    internal_warningf("%s: %c", __func__, optc);
    return 1;
   }
   if (builtin_opt.optarg) {
    if (set_ulimit(l, builtin_opt.optarg, how))
     return 1;
   } else
    print_ulimit(l, how);
   break;
  }
 wp += builtin_opt.optind;
 if (all) {
  for (l = limits; l->name; l++) {
   shprintf("%-20s ", l->name);
   print_ulimit(l, how);
  }
 } else if (builtin_opt.optind == 1) {
  l = &limits[1];
  if (wp[0] != ((void *)0)) {
   if (set_ulimit(l, wp[0], how))
    return 1;
   wp++;
  } else {
   print_ulimit(l, how);
  }
 }
 return 0;
}

variable_types:
wp
char **

variable_types: struct limits { char *name; int resource; int factor; char option; } []

variable_types:
options
char *

variable_types: optc int

variable_types: all int


decompiled_code:
int alarm_catcher()
{
int *v0; // rax
int *v1; // rbx
int v2; // ebp

v0 = _errno_location();
v1 = v0;
v2 = *v0;
if ( ksh_tmout_state == 1 )
{
LODWORD(v0) = alarm(0);
if ( (_DWORD)v0 )
{
LODWORD(v0) = alarm((unsigned int)v0);
}
else
{
ksh_tmout_state = 2;
intrsig = 1;
}
}
*v1 = v2;
return (int)v0;
}

canonical_decompiled_code:
int func0()
{
int *v0; // rax
int *v1; // rbx
int v2; // ebp

v0 = _errno_location();
v1 = v0;
v2 = *v0;
if ( ksh_tmout_state == 1 )
{
LODWORD(v0) = alarm(0);
if ( (_DWORD)v0 )
{
LODWORD(v0) = alarm((unsigned int)v0);
}
else
{
ksh_tmout_state = 2;
intrsig = 1;
}
}
*v1 = v2;
return (int)v0;
}

original_code:
static void
alarm_catcher(int sig)
{
 int errno_ = (*__errno_location ());
 if (ksh_tmout_state == TMOUT_READING) {
  int left = alarm(0);
  if (left == 0) {
   ksh_tmout_state = TMOUT_LEAVING;
   intrsig = 1;
  } else
   alarm(left);
 }
 (*__errno_location ()) = errno_;
}

variable_types: sig int

variable_types: left int


decompiled_code:
__int64 fatal_trap_check()
{
__int64 *v0; // rax

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) && (*((_DWORD *)v0 + 9) & 0x240) != 0 )
return (unsigned int)(*(_DWORD *)v0 + 128);
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

original_code:
int
fatal_trap_check(void)
{
 int i;
 Trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (p->flags & ((1<<(6))|(1<<(9)))))
   return 128 + p->signal;
 return 0;
}

canonical_original_code:
int fatal_trap_check()
{
 int i;
 struct trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (p->flags & ((1<<(6))|(1<<(9)))))
   return 128 + p->signal;
 return 0;
}

variable_types: i int

variable_types: struct trap { int signal; char *name; char *mess; char *trap; int set; int flags; void (*cursig)(int); void (*shtrap)(int); } *


decompiled_code:
__int64 trap_pending()
{
__int64 *v0; // rax
_BYTE *v1; // rdx

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) )
{
v1 = (_BYTE *)v0[3];
if ( v1 )
{
if ( *v1 )
return *(unsigned int *)v0;
}
else if ( (*((_DWORD *)v0 + 9) & 0x240) != 0 )
{
return *(unsigned int *)v0;
}
}
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 *v0; // rax
_BYTE *v1; // rdx

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) )
{
v1 = (_BYTE *)v0[3];
if ( v1 )
{
if ( *v1 )
return *(unsigned int *)v0;
}
else if ( (*((_DWORD *)v0 + 9) & 0x240) != 0 )
{
return *(unsigned int *)v0;
}
}
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

original_code:
int
trap_pending(void)
{
 int i;
 Trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && ((p->trap && p->trap[0]) ||
      ((p->flags & ((1<<(6))|(1<<(9)))) && !p->trap)))
   return p->signal;
 return 0;
}

canonical_original_code:
int trap_pending()
{
 int i;
 struct trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && ((p->trap && p->trap[0]) ||
      ((p->flags & ((1<<(6))|(1<<(9)))) && !p->trap)))
   return p->signal;
 return 0;
}

user_defined_types: struct trap { int signal; char *name; char *mess; char *trap; int set; int flags; void (*cursig)(int); void (*shtrap)(int); }


runtrap

decompiled_code:
__int64 __fastcall runtrap(unsigned int *a1)
{
__int64 result; // rax
_BYTE *v2; // rbp
int v3; // ebx
unsigned int v4; // edx
unsigned int v5; // eax
int v6; // er12
int v7; // er13

result = *a1;
v2 = (_BYTE *)*((_QWORD *)a1 + 3);
a1[8] = 0;
if ( v2 )
{
if ( !*v2 )
return result;
if ( (_DWORD)result && (_DWORD)result != 65 )
{
v3 = exstat;
result = command(v2, current_lineno);
exstat = v3;
return result;
}
}
else
{
v4 = a1[9];
if ( (v4 & 0x200) != 0 )
{
exstat = result + 128;
LODWORD(result) = unwind(4LL);
}
else if ( (v4 & 0x40) == 0 )
{
return result;
}
exstat = result + 128;
unwind(5LL);
}
v5 = a1[9];
v6 = v5 & 0x100;
BYTE1(v5) &= 0xFEu;
a1[9] = v5;
*((_QWORD *)a1 + 3) = 0LL;
v7 = exstat;
result = command(v2, current_lineno);
exstat = v7;
if ( (*((_BYTE *)a1 + 37) & 1) != 0 )
result = afree(v2, &aperm);
else
*((_QWORD *)a1 + 3) = v2;
a1[9] |= v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1)
{
__int64 result; // rax
_BYTE *v2; // rbp
int v3; // ebx
unsigned int v4; // edx
unsigned int v5; // eax
int v6; // er12
int v7; // er13

result = *a1;
v2 = (_BYTE *)*((_QWORD *)a1 + 3);
a1[8] = 0;
if ( v2 )
{
if ( !*v2 )
return result;
if ( (_DWORD)result && (_DWORD)result != 65 )
{
v3 = exstat;
result = command(v2, current_lineno);
exstat = v3;
return result;
}
}
else
{
v4 = a1[9];
if ( (v4 & 0x200) != 0 )
{
exstat = result + 128;
LODWORD(result) = unwind(4LL);
}
else if ( (v4 & 0x40) == 0 )
{
return result;
}
exstat = result + 128;
unwind(5LL);
}
v5 = a1[9];
v6 = v5 & 0x100;
BYTE1(v5) &= 0xFEu;
a1[9] = v5;
*((_QWORD *)a1 + 3) = 0LL;
v7 = exstat;
result = command(v2, current_lineno);
exstat = v7;
if ( (*((_BYTE *)a1 + 37) & 1) != 0 )
result = afree(v2, &aperm);
else
*((_QWORD *)a1 + 3) = v2;
a1[9] |= v6;
return result;
}

original_code:
void
runtrap(Trap *p)
{
 int i = p->signal;
 char *trapstr = p->trap;
 int oexstat;
 int old_changed = 0;
 p->set = 0;
 if (trapstr == ((void *)0)) {
  if (p->flags & (1<<(9))) {
   exstat = 128 + i;
   unwind(4);
  }
  if (p->flags & (1<<(6))) {
   exstat = 128 + i;
   unwind(5);
  }
  return;
 }
 if (trapstr[0] == '\0')
  return;
 if (i == 0 || i == (64 + 1)) {
  old_changed = p->flags & (1<<(8));
  p->flags &= ~(1<<(8));
  p->trap = ((void *)0);
 }
 oexstat = exstat;
 command(trapstr, current_lineno);
 exstat = oexstat;
 if (i == 0 || i == (64 + 1)) {
  if (p->flags & (1<<(8)))
   afree(trapstr, &aperm);
  else
   p->trap = trapstr;
  p->flags |= old_changed;
 }
}

canonical_original_code:
void runtrap(struct trap *p)
{
 int i = p->signal;
 char *trapstr = p->trap;
 int oexstat;
 int old_changed = 0;
 p->set = 0;
 if (trapstr == ((void *)0)) {
  if (p->flags & (1<<(9))) {
   exstat = 128 + i;
   unwind(4);
  }
  if (p->flags & (1<<(6))) {
   exstat = 128 + i;
   unwind(5);
  }
  return;
 }
 if (trapstr[0] == '\0')
  return;
 if (i == 0 || i == (64 + 1)) {
  old_changed = p->flags & (1<<(8));
  p->flags &= ~(1<<(8));
  p->trap = ((void *)0);
 }
 oexstat = exstat;
 command(trapstr, current_lineno);
 exstat = oexstat;
 if (i == 0 || i == (64 + 1)) {
  if (p->flags & (1<<(8)))
   afree(trapstr, &aperm);
  else
   p->trap = trapstr;
  p->flags |= old_changed;
 }
}

variable_types:
trapstr
char *

variable_types: oexstat int


runtraps

decompiled_code:
__int64 __fastcall runtraps(unsigned int a1)
{
unsigned int v1; // ebp
__int64 *v2; // rbx
__int64 result; // rax

if ( ksh_tmout_state == 2 )
{
ksh_tmout_state = 0;
warningf(0LL, "timed out waiting for input");
LOBYTE(a1) = 2;
unwind(2LL);
}
else
{
v1 = a1;
ksh_tmout_state = 0;
if ( !a1 )
{
trap = 0;
goto LABEL_4;
}
}
if ( (a1 & 0x40) != 0 )
intrsig = 0;
if ( (v1 & 0x200) != 0 )
fatal_trap = 0;
LABEL_4:
v2 = sigtraps;
do
{
result = *((unsigned int *)v2 + 8);
if ( (_DWORD)result )
{
if ( !v1 || (result = *((_DWORD *)v2 + 9) & v1, (*((_DWORD *)v2 + 9) & v1) != 0) && !v2[3] )
result = runtrap((unsigned int *)v2);
}
v2 += 7;
}
while ( v2 != &sigtraps[462] );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
unsigned int v1; // ebp
__int64 *v2; // rbx
__int64 result; // rax

if ( ksh_tmout_state == 2 )
{
ksh_tmout_state = 0;
warningf(0LL, "timed out waiting for input");
LOBYTE(a1) = 2;
unwind(2LL);
}
else
{
v1 = a1;
ksh_tmout_state = 0;
if ( !a1 )
{
trap = 0;
goto LABEL_4;
}
}
if ( (a1 & 0x40) != 0 )
intrsig = 0;
if ( (v1 & 0x200) != 0 )
fatal_trap = 0;
LABEL_4:
v2 = sigtraps;
do
{
result = *((unsigned int *)v2 + 8);
if ( (_DWORD)result )
{
if ( !v1 || (result = *((_DWORD *)v2 + 9) & v1, (*((_DWORD *)v2 + 9) & v1) != 0) && !v2[3] )
result = func3((unsigned int *)v2);
}
v2 += 7;
}
while ( v2 != &sigtraps[462] );
return result;
}

original_code:
void
runtraps(int flag)
{
 int i;
 Trap *p;
 if (ksh_tmout_state == TMOUT_LEAVING) {
  ksh_tmout_state = TMOUT_EXECUTING;
  warningf(0, "timed out waiting for input");
  unwind(2);
 } else
  ksh_tmout_state = TMOUT_EXECUTING;
 if (!flag)
  trap = 0;
 if (flag & (1<<(6)))
  intrsig = 0;
 if (flag & (1<<(9)))
  fatal_trap = 0;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (!flag ||
      ((p->flags & flag) && p->trap == ((void *)0))))
   runtrap(p);
}

canonical_original_code:
void runtraps(int flag)
{
 int i;
 struct trap *p;
 if (ksh_tmout_state == TMOUT_LEAVING) {
  ksh_tmout_state = TMOUT_EXECUTING;
  warningf(0, "timed out waiting for input");
  unwind(2);
 } else
  ksh_tmout_state = TMOUT_EXECUTING;
 if (!flag)
  trap = 0;
 if (flag & (1<<(6)))
  intrsig = 0;
 if (flag & (1<<(9)))
  fatal_trap = 0;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (!flag ||
      ((p->flags & flag) && p->trap == ((void *)0))))
   runtrap(p);
}

variable_types: flag int


intrcheck

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

result = intrsig;
if ( intrsig )
result = runtraps(0x240u);
return result;
}

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

result = intrsig;
if ( intrsig )
result = func4(0x240u);
return result;
}

original_code:
void
intrcheck(void)
{
 if (intrsig)
  runtraps((1<<(6))|(1<<(9)));
}

canonical_original_code:
void intrcheck()
{
 if (intrsig)
  runtraps((1<<(6))|(1<<(9)));
}


setexecsig

decompiled_code:
__int64 __fastcall setexecsig(const char *a1, char a2)
{
__int64 result; // rax
int v3; // edx
int v4; // esi
const char *v5; // rcx
__int64 v6; // rdx

result = *((unsigned int *)a1 + 9);
if ( (result & 0xC) == 0 )
{
v5 = (const char *)*((_QWORD *)a1 + 1);
v6 = *(unsigned int *)a1;
a1 = "%s: unset signal %d(%s)";
LOBYTE(result) = internal_errorf("%s: unset signal %d(%s)", "setexecsig", v6, v5);
LABEL_7:
result = (result & 4) == 0 ? 32 : 16;
*((_DWORD *)a1 + 9) = result | v3;
return result;
}
v3 = result & 0xFFFFFFCF;
v4 = a2 & 3;
switch ( v4 )
{
case 2:
*((_DWORD *)a1 + 9) = v3 | 0x20;
break;
case 3:
*((_DWORD *)a1 + 9) = v3 | 0x10;
break;
case 1:
goto LABEL_7;
default:
*((_DWORD *)a1 + 9) = v3;
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, char a2)
{
__int64 result; // rax
int v3; // edx
int v4; // esi
const char *v5; // rcx
__int64 v6; // rdx

result = *((unsigned int *)a1 + 9);
if ( (result & 0xC) == 0 )
{
v5 = (const char *)*((_QWORD *)a1 + 1);
v6 = *(unsigned int *)a1;
a1 = "%s: unset signal %d(%s)";
LOBYTE(result) = internal_errorf("%s: unset signal %d(%s)", "setexecsig", v6, v5);
LABEL_7:
result = (result & 4) == 0 ? 32 : 16;
*((_DWORD *)a1 + 9) = result | v3;
return result;
}
v3 = result & 0xFFFFFFCF;
v4 = a2 & 3;
switch ( v4 )
{
case 2:
*((_DWORD *)a1 + 9) = v3 | 0x20;
break;
case 3:
*((_DWORD *)a1 + 9) = v3 | 0x10;
break;
case 1:
goto LABEL_7;
default:
*((_DWORD *)a1 + 9) = v3;
return result;
}
return result;
}

original_code:
void
setexecsig(Trap *p, int restore)
{
 if (!(p->flags & ((1<<(2))|(1<<(3)))))
  internal_errorf("%s: unset signal %d(%s)",
      __func__, p->signal, p->name);
 p->flags &= ~((1<<(4))|(1<<(5)));
 switch (restore & 0x3) {
 case 0:
  break;
 case 1:
  p->flags |= p->flags & (1<<(2)) ? (1<<(4)) : (1<<(5));
  break;
 case 2:
  p->flags |= (1<<(5));
  break;
 case 3:
  p->flags |= (1<<(4));
  break;
 }
}

canonical_original_code:
void setexecsig(struct trap *p, int restore)
{
 if (!(p->flags & ((1<<(2))|(1<<(3)))))
  internal_errorf("%s: unset signal %d(%s)",
      __func__, p->signal, p->name);
 p->flags &= ~((1<<(4))|(1<<(5)));
 switch (restore & 0x3) {
 case 0:
  break;
 case 1:
  p->flags |= p->flags & (1<<(2)) ? (1<<(4)) : (1<<(5));
  break;
 case 2:
  p->flags |= (1<<(5));
  break;
 case 3:
  p->flags |= (1<<(4));
  break;
 }
}

variable_types: restore int


setsig

decompiled_code:
__int64 __fastcall setsig(__int64 a1, void (*a2)(int), char a3)
{
int v4; // edi
void (*v5)(int); // r12
__int64 result; // rax
struct sigaction v8; // [rsp+0h] [rbp-B8h] BYREF

v4 = *(_DWORD *)a1;
if ( !v4 )
return 1LL;
v5 = a2;
if ( v4 == 65 )
return 1LL;
if ( (*(_BYTE *)(a1 + 36) & 0xC) == 0 )
{
sigaction(v4, &Sigact_ign, &v8);
*(_DWORD *)(a1 + 36) |= 4 * (v8.sa_handler != (__sighandler_t)((char *)trapsig + 1)) + 4;
*(_QWORD *)(a1 + 40) = 1LL;
}
if ( (*(_BYTE *)(a1 + 36) & 4) == 0 || (a3 & 8) != 0 )
{
setexecsig((const char *)a1, a3 & 3);
if ( (a3 & 0x10) == 0 )
*(_QWORD *)(a1 + 48) = 0LL;
}
else
{
result = a3 & 0x10;
if ( (a3 & 0x10) == 0 )
return result;
result = a3 & 8;
if ( !shell_flags[9] )
return result;
setexecsig((const char *)a1, a3 & 3);
}
if ( (a3 & 0x20) != 0 )
{
*(_QWORD *)(a1 + 48) = a2;
v5 = (void (*)(int))trapsig;
}
result = 1LL;
if ( *(void (**)(int))(a1 + 40) != v5 )
{
*(_QWORD *)(a1 + 40) = v5;
sigemptyset(&v8.sa_mask);
v8.sa_flags = 0;
v8.sa_handler = v5;
sigaction(*(_DWORD *)a1, &v8, 0LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, void (*a2)(int), char a3)
{
int v4; // edi
void (*v5)(int); // r12
__int64 result; // rax
struct sigaction v8; // [rsp+0h] [rbp-B8h] BYREF

v4 = *(_DWORD *)a1;
if ( !v4 )
return 1LL;
v5 = a2;
if ( v4 == 65 )
return 1LL;
if ( (*(_BYTE *)(a1 + 36) & 0xC) == 0 )
{
sigaction(v4, &Sigact_ign, &v8);
*(_DWORD *)(a1 + 36) |= 4 * (v8.sa_handler != (__sighandler_t)((char *)trapsig + 1)) + 4;
*(_QWORD *)(a1 + 40) = 1LL;
}
if ( (*(_BYTE *)(a1 + 36) & 4) == 0 || (a3 & 8) != 0 )
{
func6((const char *)a1, a3 & 3);
if ( (a3 & 0x10) == 0 )
*(_QWORD *)(a1 + 48) = 0LL;
}
else
{
result = a3 & 0x10;
if ( (a3 & 0x10) == 0 )
return result;
result = a3 & 8;
if ( !shell_flags[9] )
return result;
func6((const char *)a1, a3 & 3);
}
if ( (a3 & 0x20) != 0 )
{
*(_QWORD *)(a1 + 48) = a2;
v5 = (void (*)(int))trapsig;
}
result = 1LL;
if ( *(void (**)(int))(a1 + 40) != v5 )
{
*(_QWORD *)(a1 + 40) = v5;
sigemptyset(&v8.sa_mask);
v8.sa_flags = 0;
v8.sa_handler = v5;
sigaction(*(_DWORD *)a1, &v8, 0LL);
result = 1LL;
}
return result;
}

original_code:
int
setsig(Trap *p, sig_t f, int flags)
{
 struct sigaction sigact;
 if (p->signal == 0 || p->signal == (64 + 1))
  return 1;
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  sigaction(p->signal, &Sigact_ign, &sigact);
  p->flags |= sigact.__sigaction_handler.sa_handler == ((__sighandler_t) 1) ?
      (1<<(2)) : (1<<(3));
  p->cursig = ((__sighandler_t) 1);
 }
 if ((p->flags & (1<<(2))) && !(flags & (1<<(3))) &&
     (!(flags & (1<<(4))) || !(shell_flags[(int) (FTALKING)])))
  return 0;
 setexecsig(p, flags & 0x3);
 if (!(flags & (1<<(4))))
  p->shtrap = ((void *)0);
 if (flags & (1<<(5))) {
  p->shtrap = f;
  f = trapsig;
 }
 if (p->cursig != f) {
  p->cursig = f;
  sigemptyset(&sigact.sa_mask);
  sigact.sa_flags = 0 ;
  sigact.__sigaction_handler.sa_handler = f;
  sigaction(p->signal, &sigact, ((void *)0));
 }
 return 1;
}

canonical_original_code:
int setsig(struct trap *p, void (*f)(int), int flags)
{
 struct sigaction sigact;
 if (p->signal == 0 || p->signal == (64 + 1))
  return 1;
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  sigaction(p->signal, &Sigact_ign, &sigact);
  p->flags |= sigact.__sigaction_handler.sa_handler == ((void (*)(int)) 1) ?
      (1<<(2)) : (1<<(3));
  p->cursig = ((void (*)(int)) 1);
 }
 if ((p->flags & (1<<(2))) && !(flags & (1<<(3))) &&
     (!(flags & (1<<(4))) || !(shell_flags[(int) (FTALKING)])))
  return 0;
 setexecsig(p, flags & 0x3);
 if (!(flags & (1<<(4))))
  p->shtrap = ((void *)0);
 if (flags & (1<<(5))) {
  p->shtrap = f;
  f = trapsig;
 }
 if (p->cursig != f) {
  p->cursig = f;
  sigemptyset(&sigact.sa_mask);
  sigact.sa_flags = 0 ;
  sigact.__sigaction_handler.sa_handler = f;
  sigaction(p->signal, &sigact, ((void *)0));
 }
 return 1;
}

variable_types:
f
void (*)(int)

variable_types: flags int

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


inittraps

decompiled_code:
__int64 inittraps()
{
int v0; // ebx
int i; // eax

LODWORD(sigtraps[7]) = 1;
v0 = 1;
for ( i = 0; ; i = 0 )
{
do
{
if ( LODWORD((&sys_signame)[2 * i + 1]) == v0 )
{
sigtraps[7 * v0 + 1] = (__int64)(&sys_signame)[2 * i];
break;
}
++i;
}
while ( i <= 64 );
sigtraps[7 * v0 + 2] = (__int64)strsignal(v0);
if ( ++v0 > 65 )
goto LABEL_3;
LODWORD(sigtraps[7 * v0]) = v0;
if ( v0 == 65 )
break;
}
sigtraps[456] = (__int64)"ERR";
sigtraps[457] = (__int64)"Error handler";
LABEL_3:
LODWORD(sigtraps[0]) = 0;
sigtraps[1] = (__int64)"EXIT";
sigtraps[2] = (__int64)"Exited";
sigemptyset(&set);
dword_1C08 = 0;
Sigact_ign.sa_handler = (__sighandler_t)(trapsig + 1);
HIDWORD(sigtraps[18]) |= 0xC0u;
HIDWORD(sigtraps[25]) |= 0xC0u;
HIDWORD(sigtraps[109]) |= 0x40u;
HIDWORD(sigtraps[11]) |= 0x200u;
HIDWORD(sigtraps[123]) |= 1u;
setsig((unsigned int)((_DWORD)sigtraps + 112), (void (*)(int))trapsig, 1);
setsig((unsigned int)((_DWORD)sigtraps + 168), (void (*)(int))trapsig, 1);
setsig((unsigned int)((_DWORD)sigtraps + 840), (void (*)(int))trapsig, 1);
return setsig((unsigned int)((_DWORD)sigtraps + 56), (void (*)(int))trapsig, 1);
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // ebx
int i; // eax

LODWORD(sigtraps[7]) = 1;
v0 = 1;
for ( i = 0; ; i = 0 )
{
do
{
if ( LODWORD((&sys_signame)[2 * i + 1]) == v0 )
{
sigtraps[7 * v0 + 1] = (__int64)(&sys_signame)[2 * i];
break;
}
++i;
}
while ( i <= 64 );
sigtraps[7 * v0 + 2] = (__int64)strsignal(v0);
if ( ++v0 > 65 )
goto LABEL_3;
LODWORD(sigtraps[7 * v0]) = v0;
if ( v0 == 65 )
break;
}
sigtraps[456] = (__int64)"ERR";
sigtraps[457] = (__int64)"Error handler";
LABEL_3:
LODWORD(sigtraps[0]) = 0;
sigtraps[1] = (__int64)"EXIT";
sigtraps[2] = (__int64)"Exited";
sigemptyset(&set);
dword_1C08 = 0;
Sigact_ign.sa_handler = (__sighandler_t)(trapsig + 1);
HIDWORD(sigtraps[18]) |= 0xC0u;
HIDWORD(sigtraps[25]) |= 0xC0u;
HIDWORD(sigtraps[109]) |= 0x40u;
HIDWORD(sigtraps[11]) |= 0x200u;
HIDWORD(sigtraps[123]) |= 1u;
func7((unsigned int)((_DWORD)sigtraps + 112), (void (*)(int))trapsig, 1);
func7((unsigned int)((_DWORD)sigtraps + 168), (void (*)(int))trapsig, 1);
func7((unsigned int)((_DWORD)sigtraps + 840), (void (*)(int))trapsig, 1);
return func7((unsigned int)((_DWORD)sigtraps + 56), (void (*)(int))trapsig, 1);
}

original_code:
void
inittraps(void)
{
 int i, j;
 for (i = 1; i <= (64 + 1); i++) {
  sigtraps[i].signal = i;
  if (i == (64 + 1)) {
   sigtraps[i].name = "ERR";
   sigtraps[i].mess = "Error handler";
  } else {
   for(j = 0; j < (64 + 1); j++)
   {
    if( sys_signame[j].val == sigtraps[i].signal )
    {
     sigtraps[i].name = sys_signame[j].name;
     break;
    }
   }
   sigtraps[i].mess = strsignal(sigtraps[i].signal);
  }
 }
 sigtraps[0].signal = 0;
 sigtraps[0].name = "EXIT";
 sigtraps[0].mess = "Exited";
 sigemptyset(&Sigact_ign.sa_mask);
 Sigact_ign.sa_flags = 0;
 Sigact_ign.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
 Sigact_trap = Sigact_ign;
 Sigact_trap.__sigaction_handler.sa_handler = trapsig;
 sigtraps[2].flags |= (1<<(6)) | (1<<(7));
 sigtraps[3].flags |= (1<<(6)) | (1<<(7));
 sigtraps[15].flags |= (1<<(6));
 sigtraps[1].flags |= (1<<(9));
 sigtraps[17].flags |= (1<<(0));
 setsig(&sigtraps[2], trapsig, 1);
 setsig(&sigtraps[3], trapsig, 1);
 setsig(&sigtraps[15], trapsig, 1);
 setsig(&sigtraps[1], trapsig, 1);
}

canonical_original_code:
void inittraps()
{
 int i; int j;
 for (i = 1; i <= (64 + 1); i++) {
  sigtraps[i].signal = i;
  if (i == (64 + 1)) {
   sigtraps[i].name = "ERR";
   sigtraps[i].mess = "Error handler";
  } else {
   for(j = 0; j < (64 + 1); j++)
   {
    if( sys_signame[j].val == sigtraps[i].signal )
    {
     sigtraps[i].name = sys_signame[j].name;
     break;
    }
   }
   sigtraps[i].mess = strsignal(sigtraps[i].signal);
  }
 }
 sigtraps[0].signal = 0;
 sigtraps[0].name = "EXIT";
 sigtraps[0].mess = "Exited";
 sigemptyset(&Sigact_ign.sa_mask);
 Sigact_ign.sa_flags = 0;
 Sigact_ign.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
 Sigact_trap = Sigact_ign;
 Sigact_trap.__sigaction_handler.sa_handler = trapsig;
 sigtraps[2].flags |= (1<<(6)) | (1<<(7));
 sigtraps[3].flags |= (1<<(6)) | (1<<(7));
 sigtraps[15].flags |= (1<<(6));
 sigtraps[1].flags |= (1<<(9));
 sigtraps[17].flags |= (1<<(0));
 setsig(&sigtraps[2], trapsig, 1);
 setsig(&sigtraps[3], trapsig, 1);
 setsig(&sigtraps[15], trapsig, 1);
 setsig(&sigtraps[1], trapsig, 1);
}

variable_types: j int


decompiled_code:
__int64 alarm_init()
{
HIDWORD(sigtraps[102]) |= 1u;
return setsig((unsigned int)((_DWORD)sigtraps + 784), (void (*)(int))alarm_catcher, 41);
}

canonical_decompiled_code:
__int64 func9()
{
HIDWORD(sigtraps[102]) |= 1u;
return func7((unsigned int)((_DWORD)sigtraps + 784), (void (*)(int))alarm_catcher, 41);
}

original_code:
void
alarm_init(void)
{
 sigtraps[14].flags |= (1<<(0));
 setsig(&sigtraps[14], alarm_catcher,
  1|(1<<(3))|(1<<(5)));
}

canonical_original_code:
void alarm_init()
{
 sigtraps[14].flags |= (1<<(0));
 setsig(&sigtraps[14], alarm_catcher,
  1|(1<<(3))|(1<<(5)));
}


restoresigs

decompiled_code:
__int64 restoresigs()
{
__int64 *v0; // rbx
int v1; // esi
__int64 result; // rax

v0 = sigtraps;
do
{
v1 = *((_DWORD *)v0 + 9);
if ( (v1 & 0x30) != 0 )
result = setsig((__int64)v0, (void (*)(int))((v1 >> 4) & 1), 8);
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return result;
}


settrap

decompiled_code:
__int64 __fastcall settrap(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
int v4; // edx
void (*v5)(int); // rsi
int v6; // eax
unsigned int v7; // edx

afree(*(_QWORD *)(a1 + 24), &aperm);
*(_QWORD *)(a1 + 24) = str_save(a2, &aperm);
result = *(unsigned int *)(a1 + 36);
v4 = *(_DWORD *)(a1 + 36);
BYTE1(v4) |= 1u;
*(_DWORD *)(a1 + 36) = v4;
if ( a2 )
{
v5 = (void (*)(int))trapsig;
if ( !*a2 )
v5 = (void (*)(int))(trapsig + 1);
*(_DWORD *)(a1 + 36) = result | 0x102;
}
else
{
*(_DWORD *)(a1 + 36) = result | 0x102;
if ( (result & 0x240) != 0 )
{
v5 = (void (*)(int))trapsig;
return setsig(a1, v5, 16);
}
v5 = 0LL;
}
if ( (result & 1) == 0 )
return setsig(a1, v5, 16);
if ( (result & 4) == 0 || shell_flags[9] )
{
v6 = result & 0xFFFFFFCF;
v7 = v6 | 0x122;
result = v6 | 0x112u;
if ( v5 != (void (*)(int))((char *)trapsig + 1) )
result = v7;
*(_DWORD *)(a1 + 36) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
int v4; // edx
void (*v5)(int); // rsi
int v6; // eax
unsigned int v7; // edx

afree(*(_QWORD *)(a1 + 24), &aperm);
*(_QWORD *)(a1 + 24) = str_save(a2, &aperm);
result = *(unsigned int *)(a1 + 36);
v4 = *(_DWORD *)(a1 + 36);
BYTE1(v4) |= 1u;
*(_DWORD *)(a1 + 36) = v4;
if ( a2 )
{
v5 = (void (*)(int))trapsig;
if ( !*a2 )
v5 = (void (*)(int))(trapsig + 1);
*(_DWORD *)(a1 + 36) = result | 0x102;
}
else
{
*(_DWORD *)(a1 + 36) = result | 0x102;
if ( (result & 0x240) != 0 )
{
v5 = (void (*)(int))trapsig;
return func7(a1, v5, 16);
}
v5 = 0LL;
}
if ( (result & 1) == 0 )
return func7(a1, v5, 16);
if ( (result & 4) == 0 || shell_flags[9] )
{
v6 = result & 0xFFFFFFCF;
v7 = v6 | 0x122;
result = v6 | 0x112u;
if ( v5 != (void (*)(int))((char *)trapsig + 1) )
result = v7;
*(_DWORD *)(a1 + 36) = result;
}
return result;
}

variable_types:
s
char *


cleartraps

original_code:
void
cleartraps(void)
{
 int i;
 Trap *p;
 trap = 0;
 intrsig = 0;
 fatal_trap = 0;
 for (i = (64 + 1)+1, p = sigtraps; --i >= 0; p++) {
  p->set = 0;
  if ((p->flags & (1<<(1))) && (p->trap && p->trap[0]))
   settrap(p, ((void *)0));
 }
}

canonical_original_code:
void cleartraps()
{
 int i;
 struct trap *p;
 trap = 0;
 intrsig = 0;
 fatal_trap = 0;
 for (i = (64 + 1)+1, p = sigtraps; --i >= 0; p++) {
  p->set = 0;
  if ((p->flags & (1<<(1))) && (p->trap && p->trap[0]))
   settrap(p, ((void *)0));
 }
}


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

if ( (sigtraps[95] & 0xC00000000LL) != 0 )
{
result = 0LL;
if ( !sigtraps[96] )
{
setsig((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = 1LL;
}
}
else
{
setsig((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = (HIDWORD(sigtraps[95]) >> 3) & 1;
}
return result;
}

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

if ( (sigtraps[95] & 0xC00000000LL) != 0 )
{
result = 0LL;
if ( !sigtraps[96] )
{
func7((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = 1LL;
}
}
else
{
func7((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = (HIDWORD(sigtraps[95]) >> 3) & 1;
}
return result;
}

original_code:
int
block_pipe(void)
{
 int restore_dfl = 0;
 Trap *p = &sigtraps[13];
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  setsig(p, ((__sighandler_t) 1), 0);
  if (p->flags & (1<<(3)))
   restore_dfl = 1;
 } else if (p->cursig == ((__sighandler_t) 0)) {
  setsig(p, ((__sighandler_t) 1), 0);
  restore_dfl = 1;
 }
 return restore_dfl;
}

canonical_original_code:
int block_pipe()
{
 int restore_dfl = 0;
 struct trap *p = &sigtraps[13];
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  setsig(p, ((void (*)(int)) 1), 0);
  if (p->flags & (1<<(3)))
   restore_dfl = 1;
 } else if (p->cursig == ((void (*)(int)) 0)) {
  setsig(p, ((void (*)(int)) 1), 0);
  restore_dfl = 1;
 }
 return restore_dfl;
}


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

if ( a1 )
result = setsig((unsigned int)((_DWORD)sigtraps + 728), 0LL, 0);
return result;
}

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

if ( a1 )
result = func7((unsigned int)((_DWORD)sigtraps + 728), 0LL, 0);
return result;
}

original_code:
void
restore_pipe(int restore_dfl)
{
 if (restore_dfl)
  setsig(&sigtraps[13], ((__sighandler_t) 0), 0);
}

canonical_original_code:
void restore_pipe(int restore_dfl)
{
 if (restore_dfl)
  setsig(&sigtraps[13], ((void (*)(int)) 0), 0);
}