batchUpdate

decompiled_code:
void batchUpdate()
{
double v0; // xmm0_8
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
double v4; // xmm0_8

v0 = *(double *)&w_hid0inp0 + *(double *)&bw_hid0inp0;
bw_hid0inp0 = 0LL;
w_hid0inp0 = *(_QWORD *)&v0;
v1 = *(double *)&w_hid0inp1 + *(double *)&bw_hid0inp1;
bw_hid0inp1 = 0LL;
w_hid0inp1 = *(_QWORD *)&v1;
v2 = *(double *)&w_hid1inp0 + *(double *)&bw_hid1inp0;
bw_hid1inp0 = 0LL;
w_hid1inp0 = *(_QWORD *)&v2;
v3 = *(double *)&w_hid1inp1 + *(double *)&bw_hid1inp1;
bw_hid1inp1 = 0LL;
w_hid1inp1 = *(_QWORD *)&v3;
*(double *)&w_outh0 = *(double *)&w_outh0 + *(double *)&bw_outh0;
v4 = *(double *)&w_outh1 + *(double *)&bw_outh1;
bw_outh1 = 0LL;
w_outh1 = *(_QWORD *)&v4;
*(double *)&h0_bias = *(double *)&h0_bias + *(double *)&bh0_bias;
*(double *)&h1_bias = *(double *)&h1_bias + *(double *)&bh1_bias;
*(double *)&out_bias = *(double *)&out_bias + *(double *)&bout_bias;
bw_outh0 = 0LL;
bout_bias = 0LL;
bh1_bias = 0LL;
bh0_bias = 0LL;
}

canonical_decompiled_code:
void func12()
{
double v0; // xmm0_8
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
double v4; // xmm0_8

v0 = *(double *)&w_hid0inp0 + *(double *)&bw_hid0inp0;
bw_hid0inp0 = 0LL;
w_hid0inp0 = *(_QWORD *)&v0;
v1 = *(double *)&w_hid0inp1 + *(double *)&bw_hid0inp1;
bw_hid0inp1 = 0LL;
w_hid0inp1 = *(_QWORD *)&v1;
v2 = *(double *)&w_hid1inp0 + *(double *)&bw_hid1inp0;
bw_hid1inp0 = 0LL;
w_hid1inp0 = *(_QWORD *)&v2;
v3 = *(double *)&w_hid1inp1 + *(double *)&bw_hid1inp1;
bw_hid1inp1 = 0LL;
w_hid1inp1 = *(_QWORD *)&v3;
*(double *)&w_outh0 = *(double *)&w_outh0 + *(double *)&bw_outh0;
v4 = *(double *)&w_outh1 + *(double *)&bw_outh1;
bw_outh1 = 0LL;
w_outh1 = *(_QWORD *)&v4;
*(double *)&h0_bias = *(double *)&h0_bias + *(double *)&bh0_bias;
*(double *)&h1_bias = *(double *)&h1_bias + *(double *)&bh1_bias;
*(double *)&out_bias = *(double *)&out_bias + *(double *)&bout_bias;
bw_outh0 = 0LL;
bout_bias = 0LL;
bh1_bias = 0LL;
bh0_bias = 0LL;
}

original_code:
void batchUpdate( void )
{
  w_hid0inp0 += bw_hid0inp0; w_hid0inp1 += bw_hid0inp1;
  w_hid1inp0 += bw_hid1inp0; w_hid1inp1 += bw_hid1inp1;
  w_outh0 += bw_outh0; w_outh1 += bw_outh1;
  h0_bias += bh0_bias; h1_bias += bh1_bias;
  out_bias += bout_bias;
  bw_hid0inp0 = bw_hid0inp1 = bw_hid1inp0 = bw_hid1inp1 = 0.0;
  bw_outh0 = bw_outh1 = 0.0;
  bh0_bias = bh1_bias = bout_bias = 0.0;
  return;
}

canonical_original_code:
void batchUpdate()
{
  w_hid0inp0 += bw_hid0inp0; w_hid0inp1 += bw_hid0inp1;
  w_hid1inp0 += bw_hid1inp0; w_hid1inp1 += bw_hid1inp1;
  w_outh0 += bw_outh0; w_outh1 += bw_outh1;
  h0_bias += bh0_bias; h1_bias += bh1_bias;
  out_bias += bout_bias;
  bw_hid0inp0 = bw_hid0inp1 = bw_hid1inp0 = bw_hid1inp1 = 0.0;
  bw_outh0 = bw_outh1 = 0.0;
  bh0_bias = bh1_bias = bout_bias = 0.0;
  return;
}


storePbest

decompiled_code:
__int64 __fastcall storePbest(int a1)
{
__int64 result; // rax
__m128i v2; // xmm2
__m128i v3; // xmm3
double v4; // xmm0_8
bool v5; // cc
__int64 v6; // xmm1_8

result = 3LL * a1;
v2 = _mm_load_si128((const __m128i *)&particles[result]);
v3 = _mm_load_si128((const __m128i *)&xmmword_404210[result]);
xmmword_404120[result] = (__int128)_mm_load_si128((const __m128i *)&xmmword_404220[result]);
v4 = *((double *)&xmmword_404120[result] + 1);
v5 = v4 <= *(double *)&qword_4040E8;
pbest[result] = (__int128)v2;
xmmword_404110[result] = (__int128)v3;
if ( !v5 )
{
v6 = *((_QWORD *)&pbest[3 * a1] + 1);
qword_4040E8 = *(_QWORD *)&v4;
qword_4040C8 = v6;
qword_4040D0 = *(_QWORD *)&xmmword_404110[3 * a1];
}
return result * 16;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax
__m128i v2; // xmm2
__m128i v3; // xmm3
double v4; // xmm0_8
bool v5; // cc
__int64 v6; // xmm1_8

result = 3LL * a1;
v2 = _mm_load_si128((const __m128i *)&particles[result]);
v3 = _mm_load_si128((const __m128i *)&xmmword_404210[result]);
xmmword_404120[result] = (__int128)_mm_load_si128((const __m128i *)&xmmword_404220[result]);
v4 = *((double *)&xmmword_404120[result] + 1);
v5 = v4 <= *(double *)&qword_4040E8;
pbest[result] = (__int128)v2;
xmmword_404110[result] = (__int128)v3;
if ( !v5 )
{
v6 = *((_QWORD *)&pbest[3 * a1] + 1);
qword_4040E8 = *(_QWORD *)&v4;
qword_4040C8 = v6;
qword_4040D0 = *(_QWORD *)&xmmword_404110[3 * a1];
}
return result * 16;
}

original_code:
void storePbest( int index )
{
  memcpy( (void *)&pbest[index], (void *)&particles[index],
           sizeof(particle_t) );
  if (pbest[index].fitness > gbest.fitness) {
    gbest.position.x = pbest[index].position.x;
    gbest.position.y = pbest[index].position.y;
    gbest.fitness = pbest[index].fitness;
  }
  return;
}

canonical_original_code:
void storePbest(int index)
{
  memcpy( (void *)&pbest[index], (void *)&particles[index],
           sizeof(particle_t) );
  if (pbest[index].fitness > gbest.fitness) {
    gbest.position.x = pbest[index].position.x;
    gbest.position.y = pbest[index].position.y;
    gbest.fitness = pbest[index].fitness;
  }
  return;
}

variable_types: index int

return_type: void


closePopulation

decompiled_code:
int closePopulation()
{
__int128 *v0; // rbx
FILE *v1; // rdi
int result; // eax

v0 = particles;
do
{
v1 = *(FILE **)v0;
v0 += 3;
result = fclose(v1);
}
while ( v0 != (__int128 *)&end );
return result;
}

canonical_decompiled_code:
int func9()
{
__int128 *v0; // rbx
FILE *v1; // rdi
int result; // eax

v0 = particles;
do
{
v1 = *(FILE **)v0;
v0 += 3;
result = fclose(v1);
}
while ( v0 != (__int128 *)&end );
return result;
}

original_code:
void closePopulation( void )
{
  int i;
  for ( i = 0 ; i < 5 ; i++ ) {
    fclose( particles[i].fp );
  }
  return;
}

canonical_original_code:
void closePopulation()
{
  int i;
  for ( i = 0 ; i < 5 ; i++ ) {
    fclose( particles[i].fp );
  }
  return;
}

variable_types: i int


checkFitness

decompiled_code:
double __fastcall checkFitness(double a1, double a2)
{
double v2; // xmm1_8
double v3; // xmm0_8

v2 = a2 * a2 + a1 * a1;
if ( v2 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
return cos(v3) * 6.0 / (v2 + 6.0);
}

canonical_decompiled_code:
double __fastcall func10(double a1, double a2)
{
double v2; // xmm1_8
double v3; // xmm0_8

v2 = a2 * a2 + a1 * a1;
if ( v2 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
return cos(v3) * 6.0 / (v2 + 6.0);
}

original_code:
double checkFitness( double x, double y )
{
  return ( (double)(6.0 * cos(sqrt(x*x+y*y))/(x*x+y*y+6.0)) );
}

canonical_original_code:
double checkFitness(double x, double y)
{
  return ( (double)(6.0 * cos(sqrt(x*x+y*y))/(x*x+y*y+6.0)) );
}

variable_types: x double

variable_types: y double

return_type: double


initPopulation

original_code:
void initPopulation( void )
{
  int i;
  char filename[80 +1];
  extern double checkFitness( double, double );
  gbest.fitness = (double)-9999.0;
  for ( i = 0 ; i < 5 ; i++ ) {
    sprintf( filename, "particle%02d.txt", i );
    particles[i].fp = fopen( filename, "w" );
    particles[i].position.x = (((4.0 * ((double)3.1415926)) * ((double)rand() / (double)2147483647)) - (2.0 * ((double)3.1415926)));
    particles[i].position.y = (((4.0 * ((double)3.1415926)) * ((double)rand() / (double)2147483647)) - (2.0 * ((double)3.1415926)));
    particles[i].fitness =
      checkFitness( particles[i].position.x, particles[i].position.y );
    particles[i].velocity.x = (((double)rand() / (double)2147483647) / (double)10.0);
    particles[i].velocity.y = (((double)rand() / (double)2147483647) / (double)10.0);
    storePbest( i );
    fprintf( particles[i].fp, "%lg, %lg, %lg\n",
             particles[i].position.x, particles[i].position.y,
             particles[i].fitness );
  }
  return;
}

canonical_original_code:
void initPopulation()
{
  int i;
  char filename[-1];
  extern double checkFitness(double, double);
  gbest.fitness = (double)-9999.0;
  for ( i = 0 ; i < 5 ; i++ ) {
    sprintf( filename, "particle%02d.txt", i );
    particles[i].fp = fopen( filename, "w" );
    particles[i].position.x = (((4.0 * ((double)3.1415926)) * ((double)rand() / (double)2147483647)) - (2.0 * ((double)3.1415926)));
    particles[i].position.y = (((4.0 * ((double)3.1415926)) * ((double)rand() / (double)2147483647)) - (2.0 * ((double)3.1415926)));
    particles[i].fitness =
      checkFitness( particles[i].position.x, particles[i].position.y );
    particles[i].velocity.x = (((double)rand() / (double)2147483647) / (double)10.0);
    particles[i].velocity.y = (((double)rand() / (double)2147483647) / (double)10.0);
    storePbest( i );
    fprintf( particles[i].fp, "%lg, %lg, %lg\n",
             particles[i].position.x, particles[i].position.y,
             particles[i].fitness );
  }
  return;
}

variable_types:
filename
char [-1]

variable_types:
checkFitness
double (double, double)


clearEntries

original_code:
void clearEntries( void )
{
  elementType *walker = &head;
  elementType *temp;
  int i;
  extern monitorEntryType monitors[];
  while (walker->next) {
    if (walker->next->shown) {
      temp = walker->next;
      walker->next = walker->next->next;
      free(temp);
    } else {
      walker = walker->next;
    }
  }
  for (i = 0 ; i < 20 ; i++) {
    if ((monitors[i].active) && (monitors[i].shown)) {
      monitors[i].changed = 0;
      monitors[i].shown = 0;
    }
  }
  emitGroupStatus();
}

canonical_original_code:
void clearEntries()
{
  struct elementStruct *walker = &head;
  struct elementStruct *temp;
  int i;
  extern struct monitorEntryType monitors[];
  while (walker->next) {
    if (walker->next->shown) {
      temp = walker->next;
      walker->next = walker->next->next;
      free(temp);
    } else {
      walker = walker->next;
    }
  }
  for (i = 0 ; i < 20 ; i++) {
    if ((monitors[i].active) && (monitors[i].shown)) {
      monitors[i].changed = 0;
      monitors[i].shown = 0;
    }
  }
  emitGroupStatus();
}

variable_types: struct elementStruct { int group; int rank; int msgId; char subject[-1]; char msgDate[-1]; char link[-1]; int shown; struct elementStruct *next; } *

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


showNewsItems

decompiled_code:
__int64 showNewsItems()
{
__int64 result; // rax
_QWORD *v1; // rbx

puts("Current news items:\n");
result = head[40];
if ( result )
{
v1 = head;
do
{
printf("id : %d\n", *(unsigned int *)(result + 8));
printf("rank : %d\n", *(unsigned int *)(v1[40] + 4LL));
printf("group : %d\n", *(unsigned int *)v1[40]);
printf("subject : %s\n", (const char *)(v1[40] + 12LL));
printf("date : %s\n\n", (const char *)(v1[40] + 113LL));
v1 = (_QWORD *)v1[40];
result = v1[40];
}
while ( result );
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
_QWORD *v1; // rbx

puts("Current news items:\n");
result = head[40];
if ( result )
{
v1 = head;
do
{
printf("id : %d\n", *(unsigned int *)(result + 8));
printf("rank : %d\n", *(unsigned int *)(v1[40] + 4LL));
printf("group : %d\n", *(unsigned int *)v1[40]);
printf("subject : %s\n", (const char *)(v1[40] + 12LL));
printf("date : %s\n\n", (const char *)(v1[40] + 113LL));
v1 = (_QWORD *)v1[40];
result = v1[40];
}
while ( result );
}
return result;
}

original_code:
void showNewsItems( void )
{
  elementType *walker = &head;
  printf("Current news items:\n\n");
  while (walker->next) {
    printf("id      : %d\n", walker->next->msgId);
    printf("rank    : %d\n", walker->next->rank);
    printf("group   : %d\n", walker->next->group);
    printf("subject : %s\n", walker->next->subject);
    printf("date    : %s\n\n", walker->next->msgDate);
    walker = walker->next;
  }
}

canonical_original_code:
void showNewsItems()
{
  struct elementStruct *walker = &head;
  printf("Current news items:\n\n");
  while (walker->next) {
    printf("id      : %d\n", walker->next->msgId);
    printf("rank    : %d\n", walker->next->rank);
    printf("group   : %d\n", walker->next->group);
    printf("subject : %s\n", walker->next->subject);
    printf("date    : %s\n\n", walker->next->msgDate);
    walker = walker->next;
  }
}

user_defined_types: struct elementStruct { int group; int rank; int msgId; char subject[-1]; char msgDate[-1]; char link[-1]; int shown; struct elementStruct *next; }


checkGroup

decompiled_code:
int __fastcall checkGroup(signed int a1)
{
int v2; // ebx
__int64 v3; // rbp
const char *v4; // r14
char *v5; // rbp
int v6; // er13
char v7[80]; // [rsp+0h] [rbp-1C8h] BYREF
void *ptr; // [rsp+50h] [rbp-178h] BYREF
int v9; // [rsp+58h] [rbp-170h]
int v10; // [rsp+5Ch] [rbp-16Ch]

v9 = 0x10000;
ptr = calloc(0x10001uLL, 1uLL);
prune(&feed, v7);
printf("checkGroup called %s\n", v7);
if ( !(unsigned int)nntpConnect(v7) )
{
v2 = 100;
v3 = 508LL * a1;
v4 = (char *)&feed + v3 + 84;
v5 = (char *)&feed + v3;
v6 = nntpSetGroup(v4, *((unsigned int *)v5 + 42));
printf("Group set to %s (last read %d)\n", v4, *((unsigned int *)v5 + 42));
if ( v6 <= 100 )
v2 = v6;
if ( v6 > 0 )
{
do
{
if ( (int)nntpPeek(&ptr, 0x10000LL) > 0 && !(unsigned int)nntpParse(&ptr, 1LL) )
testNewsItem(a1, (__int64)&ptr);
--v2;
*((_DWORD *)v5 + 42) = v10;
nntpSkip();
}
while ( v2 > 0 );
}
}
free(ptr);
nntpDisconnect();
return puts("Disconnected");
}

canonical_decompiled_code:
int __fastcall func3(signed int a1)
{
int v2; // ebx
__int64 v3; // rbp
const char *v4; // r14
char *v5; // rbp
int v6; // er13
char v7[80]; // [rsp+0h] [rbp-1C8h] BYREF
void *ptr; // [rsp+50h] [rbp-178h] BYREF
int v9; // [rsp+58h] [rbp-170h]
int v10; // [rsp+5Ch] [rbp-16Ch]

v9 = 0x10000;
ptr = calloc(0x10001uLL, 1uLL);
prune(&feed, v7);
printf("checkGroup called %s\n", v7);
if ( !(unsigned int)nntpConnect(v7) )
{
v2 = 100;
v3 = 508LL * a1;
v4 = (char *)&feed + v3 + 84;
v5 = (char *)&feed + v3;
v6 = nntpSetGroup(v4, *((unsigned int *)v5 + 42));
printf("Group set to %s (last read %d)\n", v4, *((unsigned int *)v5 + 42));
if ( v6 <= 100 )
v2 = v6;
if ( v6 > 0 )
{
do
{
if ( (int)nntpPeek(&ptr, 0x10000LL) > 0 && !(unsigned int)nntpParse(&ptr, 1LL) )
func2(a1, (__int64)&ptr);
--v2;
*((_DWORD *)v5 + 42) = v10;
nntpSkip();
}
while ( v2 > 0 );
}
}
free(ptr);
nntpDisconnect();
return puts("Disconnected");
}

original_code:
void checkGroup( int group )
{
  int result, count, index = 0;
  char fqdn[80];
  news_t news;
  news.msg = (char *)malloc((64*1024)+1);
  bzero( news.msg, (64*1024)+1 );
  news.msgLen = (64*1024);
  prune( feed.url, fqdn );
  printf("checkGroup called %s\n", fqdn);
  count = nntpConnect( fqdn );
  if (count == 0) {
    count = nntpSetGroup( feed.groups[group].groupName,
                          feed.groups[group].lastMessageRead );
    printf("Group set to %s (last read %d)\n",
        feed.groups[group].groupName, feed.groups[group].lastMessageRead);
    index = 0;
    if (count > 100) count = 100;
    while (count-- > 0) {
      result = nntpPeek( &news, (64*1024) );
      if (result > 0) {
        result = nntpParse( &news, 1 );
        if (result == 0) {
          testNewsItem( group, &news );
        }
      }
      feed.groups[group].lastMessageRead = news.msgId;
      nntpSkip();
    }
  }
  free( news.msg );
  nntpDisconnect();
  printf("Disconnected\n");
  return;
}

canonical_original_code:
void checkGroup(int group)
{
  int result; int count; int index = 0;
  char fqdn[80];
  struct news_t news;
  news.msg = (char *)malloc((64*1024)+1);
  bzero( news.msg, (64*1024)+1 );
  news.msgLen = (64*1024);
  prune( feed.url, fqdn );
  printf("checkGroup called %s\n", fqdn);
  count = nntpConnect( fqdn );
  if (count == 0) {
    count = nntpSetGroup( feed.groups[group].groupName,
                          feed.groups[group].lastMessageRead );
    printf("Group set to %s (last read %d)\n",
        feed.groups[group].groupName, feed.groups[group].lastMessageRead);
    index = 0;
    if (count > 100) count = 100;
    while (count-- > 0) {
      result = nntpPeek( &news, (64*1024) );
      if (result > 0) {
        result = nntpParse( &news, 1 );
        if (result == 0) {
          testNewsItem( group, &news );
        }
      }
      feed.groups[group].lastMessageRead = news.msgId;
      nntpSkip();
    }
  }
  free( news.msg );
  nntpDisconnect();
  printf("Disconnected\n");
  return;
}

variable_types: group int

variable_types: result int

variable_types: count int

variable_types:
fqdn
char [80]

variable_types: struct news_t { char *msg; int msgLen; int msgId; char subject[-1]; char sender[-1]; char msgDate[-1]; char *bodyStart; }


checkNewsSources

decompiled_code:
int checkNewsSources()
{
int *v0; // rbp
signed int i; // ebx
int result; // eax
signed int v3; // edi

v0 = (int *)(unsigned int)((_DWORD)&feed + 80);
for ( i = 0; i != 20; ++i )
{
while ( 1 )
{
result = *v0;
if ( *v0 )
break;
++i;
v0 += 127;
if ( i == 20 )
return result;
}
v3 = i;
v0 += 127;
result = checkGroup(v3);
}
return result;
}

canonical_decompiled_code:
int func4()
{
int *v0; // rbp
signed int i; // ebx
int result; // eax
signed int v3; // edi

v0 = (int *)(unsigned int)((_DWORD)&feed + 80);
for ( i = 0; i != 20; ++i )
{
while ( 1 )
{
result = *v0;
if ( *v0 )
break;
++i;
v0 += 127;
if ( i == 20 )
return result;
}
v3 = i;
v0 += 127;
result = func3(v3);
}
return result;
}

original_code:
void checkNewsSources( void )
{
  int i;
  extern feedEntryType feed;
  for (i = 0 ; i < 20 ; i++) {
    if ( feed.groups[i].active ) {
      checkGroup( i );
    }
  }
  return;
}

canonical_original_code:
void checkNewsSources()
{
  int i;
  extern struct feedEntryType feed;
  for (i = 0 ; i < 20 ; i++) {
    if ( feed.groups[i].active ) {
      checkGroup( i );
    }
  }
  return;
}

variable_types: struct feedEntryType { char url[80]; struct groupEntryType { int active; char groupName[-1]; int lastMessageRead; char searchString[10][-1]; int numSearchStrings; } groups[20]; }

user_defined_types: struct feedEntryType { char url[80]; struct groupEntryType groups[20]; }


addWord

decompiled_code:
__int64 __fastcall addWord(int a1, __int64 a2)
{
__int64 result; // rax
unsigned __int64 v3; // rcx

result = 0LL;
while ( lexicon[2 * result] )
{
if ( ++result == 30 )
__assert_fail("0", "main.c", 0x25u, "addWord");
}
v3 = 16LL * (int)result;
dword_4040E8[v3 / 4] = a1;
lexicon[v3 / 8] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2)
{
__int64 result; // rax
unsigned __int64 v3; // rcx

result = 0LL;
while ( lexicon[2 * result] )
{
if ( ++result == 30 )
__assert_fail("0", "main.c", 0x25u, "addWord");
}
v3 = 16LL * (int)result;
dword_4040E8[v3 / 4] = a1;
lexicon[v3 / 8] = a2;
return result;
}

original_code:
void addWord( int type, char *word )
{
  int i=0;
  while (1) {
    if (lexicon.words[i].word == (char *)((void *)0)) {
      lexicon.words[i].type = type;
      lexicon.words[i].word = word;
      return;
    } else {
      if (++i == 30) break;
    }
  }
  ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "main.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  return;
}

canonical_original_code:
void addWord(int type, char *word)
{
  int i = 0;
  while (1) {
    if (lexicon.words[i].word == (char *)((void *)0)) {
      lexicon.words[i].type = type;
      lexicon.words[i].word = word;
      return;
    } else {
      if (++i == 30) break;
    }
  }
  ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "main.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  return;
}

variable_types: type int

variable_types:
word
char *


findWord

decompiled_code:
int __fastcall findWord(__int64 a1)
{
__int64 v1; // rbx
int result; // eax
const char *v3; // rbp
const char *v4; // r14
size_t v5; // rax

v1 = 0LL;
*(_DWORD *)(a1 + 8) = 5;
while ( 1 )
{
result = 16 * v1;
v3 = (const char *)lexicon[2 * v1];
if ( v3 )
{
v4 = *(const char **)a1;
v5 = strlen(*(const char **)a1);
result = strncmp(v3, v4, v5);
if ( !result )
break;
}
if ( ++v1 == 30 )
return result;
}
result = dword_4040E8[4 * (int)v1];
*(_DWORD *)(a1 + 8) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
__int64 v1; // rbx
int result; // eax
const char *v3; // rbp
const char *v4; // r14
size_t v5; // rax

v1 = 0LL;
*(_DWORD *)(a1 + 8) = 5;
while ( 1 )
{
result = 16 * v1;
v3 = (const char *)lexicon[2 * v1];
if ( v3 )
{
v4 = *(const char **)a1;
v5 = strlen(*(const char **)a1);
result = strncmp(v3, v4, v5);
if ( !result )
break;
}
if ( ++v1 == 30 )
return result;
}
result = dword_4040E8[4 * (int)v1];
*(_DWORD *)(a1 + 8) = result;
return result;
}

original_code:
void findWord( word_t *symbol )
{
  int i;
  symbol->type = 5;
  for (i = 0 ; i < 30 ; i++) {
    if (lexicon.words[i].word != (char *)((void *)0)) {
      if (!strncmp(lexicon.words[i].word,
                    symbol->word, strlen(symbol->word))) {
        symbol->type = lexicon.words[i].type;
        return;
      }
    }
  }
  return;
}

canonical_original_code:
void findWord(struct word_t *symbol)
{
  int i;
  symbol->type = 5;
  for (i = 0 ; i < 30 ; i++) {
    if (lexicon.words[i].word != (char *)((void *)0)) {
      if (!strncmp(lexicon.words[i].word,
                    symbol->word, strlen(symbol->word))) {
        symbol->type = lexicon.words[i].type;
        return;
      }
    }
  }
  return;
}

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


initSentence

decompiled_code:
char *__fastcall initSentence(char *src)
{
int v1; // eax
size_t v2; // rbx
char *result; // rax

v1 = strlen(src);
v2 = v1;
workingString = malloc(v1 + 1);
result = strncpy((char *)workingString, src, v2);
windex = 0;
result[v2] = 0;
frame = 0LL;
qword_4040A8 = 0LL;
qword_4040B0 = 0LL;
s = 0LL;
return result;
}

canonical_decompiled_code:
char *__fastcall func10(char *src)
{
int v1; // eax
size_t v2; // rbx
char *result; // rax

v1 = strlen(src);
v2 = v1;
workingString = malloc(v1 + 1);
result = strncpy((char *)workingString, src, v2);
windex = 0;
result[v2] = 0;
frame = 0LL;
qword_4040A8 = 0LL;
qword_4040B0 = 0LL;
s = 0LL;
return result;
}

original_code:
void initSentence( char *sentence )
{
  int len;
  len = strlen(sentence);
  workingString = malloc( len+1 );
  strncpy( workingString, sentence, len );
  workingString[len] = 0;
  windex = 0;
  frame.actor = ((void *)0);
  frame.action = ((void *)0);
  frame.modifier = ((void *)0);
  frame.object = ((void *)0);
  return;
}

canonical_original_code:
void initSentence(char *sentence)
{
  int len;
  len = strlen(sentence);
  workingString = malloc( len+1 );
  strncpy( workingString, sentence, len );
  workingString[len] = 0;
  windex = 0;
  frame.actor = ((void *)0);
  frame.action = ((void *)0);
  frame.modifier = ((void *)0);
  frame.object = ((void *)0);
  return;
}

variable_types:
sentence
char *

variable_types: len int


destroySentence

decompiled_code:
void destroySentence()
{
free(workingString);
}

canonical_decompiled_code:
void func11()
{
free(workingString);
}

original_code:
void destroySentence( void )
{
  free( workingString );
  return;
}

canonical_original_code:
void destroySentence()
{
  free( workingString );
  return;
}


getWord

decompiled_code:
int __fastcall getWord(__int64 a1)
{
char *v1; // rax
_BYTE *v2; // r9
char *v4; // rcx
char v5; // dl
int v6; // edi
__int64 v7; // rdx
char *v8; // rax
int v9; // esi
char v10; // cl
char *v11; // rdx
int v12; // edi

LODWORD(v1) = windex;
v2 = workingString;
v4 = (char *)workingString + windex;
v5 = *v4;
if ( *v4 == 32 )
{
v1 = (char *)workingString + windex + 1;
do
{
v6 = (_DWORD)v1 - (_DWORD)workingString;
v4 = v1;
v5 = *v1++;
}
while ( v5 == 32 );
windex = v6;
}
if ( v5 )
{
v7 = windex;
*(_QWORD *)a1 = v4;
v8 = &v2[v7];
v9 = v7;
v10 = v2[v7];
if ( (v10 & 0xDF) != 0 )
{
v11 = &v2[v7 + 1];
v12 = v9 + 1 - (_DWORD)v11;
do
{
v10 = *v11;
v9 = v12 + (_DWORD)v11;
v8 = v11++;
}
while ( (v10 & 0xDF) != 0 );
windex = v9;
}
if ( v10 == 32 )
{
*v8 = 0;
windex = v9 + 1;
}
LODWORD(v1) = findWord(a1);
}
else
{
*(_DWORD *)(a1 + 8) = 5;
}
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
char *v1; // rax
_BYTE *v2; // r9
char *v4; // rcx
char v5; // dl
int v6; // edi
__int64 v7; // rdx
char *v8; // rax
int v9; // esi
char v10; // cl
char *v11; // rdx
int v12; // edi

LODWORD(v1) = windex;
v2 = workingString;
v4 = (char *)workingString + windex;
v5 = *v4;
if ( *v4 == 32 )
{
v1 = (char *)workingString + windex + 1;
do
{
v6 = (_DWORD)v1 - (_DWORD)workingString;
v4 = v1;
v5 = *v1++;
}
while ( v5 == 32 );
windex = v6;
}
if ( v5 )
{
v7 = windex;
*(_QWORD *)a1 = v4;
v8 = &v2[v7];
v9 = v7;
v10 = v2[v7];
if ( (v10 & 0xDF) != 0 )
{
v11 = &v2[v7 + 1];
v12 = v9 + 1 - (_DWORD)v11;
do
{
v10 = *v11;
v9 = v12 + (_DWORD)v11;
v8 = v11++;
}
while ( (v10 & 0xDF) != 0 );
windex = v9;
}
if ( v10 == 32 )
{
*v8 = 0;
windex = v9 + 1;
}
LODWORD(v1) = func8(a1);
}
else
{
*(_DWORD *)(a1 + 8) = 5;
}
return (int)v1;
}

original_code:
void getWord( word_t *symbol )
{
  while (workingString[windex] == ' ') windex++;
  if (workingString[windex] == 0) {
    symbol->type = 5;
    return;
  }
  symbol->word = &workingString[windex];
  while ((workingString[windex] != ' ') &&
         (workingString[windex] != 0)) windex++;
  if (workingString[windex] == ' ') {
    workingString[windex++] = 0;
  }
  findWord(symbol);
  return;
}

canonical_original_code:
void getWord(struct word_t *symbol)
{
  while (workingString[windex] == ' ') windex++;
  if (workingString[windex] == 0) {
    symbol->type = 5;
    return;
  }
  symbol->word = &workingString[windex];
  while ((workingString[windex] != ' ') &&
         (workingString[windex] != 0)) windex++;
  if (workingString[windex] == ' ') {
    workingString[windex++] = 0;
  }
  findWord(symbol);
  return;
}


strtype

decompiled_code:
const char *__fastcall strtype(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 - 1);
result = "BAD TYPE";
if ( (unsigned int)v1 <= 4 )
result = CSWTCH_25[v1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func13(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 - 1);
result = "BAD TYPE";
if ( (unsigned int)v1 <= 4 )
result = CSWTCH_25[v1];
return result;
}

original_code:
char *strtype( int type )
{
  switch(type) {
    case 1: return("DET"); break;
    case 2: return("NOUN"); break;
    case 3: return("VERB"); break;
    case 4: return("ADJECTIVE"); break;
    case 5: return("EOS"); break;
  }
  return ("BAD TYPE");
}

canonical_original_code:
char *strtype(int type)
{
  switch(type) {
    case 1: return("DET"); break;
    case 2: return("NOUN"); break;
    case 3: return("VERB"); break;
    case 4: return("ADJECTIVE"); break;
    case 5: return("EOS"); break;
  }
  return ("BAD TYPE");
}

return_type: char *


emitFrame

decompiled_code:
int emitFrame()
{
putchar(10);
if ( frame )
printf("Actor: %s\n", (const char *)frame);
if ( qword_4040A8 )
printf(" Action: %s\n", (const char *)qword_4040A8);
if ( s )
{
printf(" Object: ");
if ( qword_4040B0 )
printf("%s ", (const char *)qword_4040B0);
puts(s);
}
return puts("\n");
}

canonical_decompiled_code:
int func14()
{
putchar(10);
if ( frame )
printf("Actor: %s\n", (const char *)frame);
if ( qword_4040A8 )
printf(" Action: %s\n", (const char *)qword_4040A8);
if ( s )
{
printf(" Object: ");
if ( qword_4040B0 )
printf("%s ", (const char *)qword_4040B0);
puts(s);
}
return puts("\n");
}

original_code:
void emitFrame( void )
{
  printf("\n");
  if (frame.actor) printf("Actor: %s\n", frame.actor);
  if (frame.action) printf("  Action: %s\n", frame.action);
  if (frame.object) {
    printf("    Object: ");
    if (frame.modifier) printf("%s ", frame.modifier);
    printf("%s\n", frame.object);
  }
  printf("\n\n");
  return;
}

canonical_original_code:
void emitFrame()
{
  printf("\n");
  if (frame.actor) printf("Actor: %s\n", frame.actor);
  if (frame.action) printf("  Action: %s\n", frame.action);
  if (frame.object) {
    printf("    Object: ");
    if (frame.modifier) printf("%s ", frame.modifier);
    printf("%s\n", frame.object);
  }
  printf("\n\n");
  return;
}


parseSentence

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

printf("(S ");
printf("(NP ");
getWord((__int64)&v3);
v0 = v4;
if ( v4 == 1 )
{
printf("[DET %s] ", v3);
getWord((__int64)&v3);
v0 = v4;
}
if ( v0 != 2 )
goto LABEL_12;
printf("(NOUN %s) )", v3);
frame = (__int64)v3;
getWord((__int64)&v3);
if ( v4 == 5 )
return puts(" )");
printf("\n\t(VP ");
v0 = v4;
if ( v4 != 3 )
{
LABEL_12:
v1 = strtype(v0);
return printf("!ERROR! (type=%s)\n", v1);
}
printf("(VERB %s) ", v3);
qword_4040A8 = (__int64)v3;
getWord((__int64)&v3);
if ( v4 == 5 )
return puts(" ) )");
printf("\n\t\t(VP_NP ");
v0 = v4;
if ( v4 == 1 )
{
printf("[DET %s] ", v3);
getWord((__int64)&v3);
v0 = v4;
}
if ( v0 == 4 )
{
printf("[ADJ %s] ", v3);
qword_4040B0 = (__int64)v3;
getWord((__int64)&v3);
v0 = v4;
}
if ( v0 != 2 )
goto LABEL_12;
printf("(NOUN %s) ) ) )\n", v3);
result = (int)v3;
s = v3;
return result;
}

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


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


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, 4202798LL);
addWord(2, 4202823LL);
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;
}


initStateSpace

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

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

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

v0 = (char *)&unk_404580;
do
{
result = v0 - 800;
do
{
*((_QWORD *)result + 8) = 0LL;
result += 80;
*((_OWORD *)result - 5) = 0LL;
*((_OWORD *)result - 4) = 0LL;
*((_OWORD *)result - 3) = 0LL;
*((_OWORD *)result - 2) = 0LL;
*((_QWORD *)result - 1) = 0LL;
}
while ( result != v0 );
v0 = result + 800;
}
while ( result != (char *)&end );
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)
{
double v2; // xmm0_8
__int64 v3; // rdx
__int64 v4; // rax
__int64 result; // rax

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

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

v2 = 0.0;
v3 = 80LL * a2 + 800LL * a1;
v4 = 0LL;
*(__int64 *)((char *)qword_4042A0 + v3) = 0LL;
do
v2 = v2 + *(double *)((char *)&stateSpace[v4++] + v3);
while ( v4 != 8 );
result = 100LL * a1;
*(double *)&qword_4042A0[10 * a2 + result] = v2;
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)
{
int v2; // eax
unsigned int v3; // er8

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

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

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

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)
{
__int64 v3; // rcx
__int64 v4; // r8
double v5; // xmm0_8
unsigned int v6; // er12
int v7; // ebx
double v8; // [rsp+0h] [rbp-48h]
double v9; // [rsp+8h] [rbp-40h]

if ( actionSelection )
{
if ( actionSelection != 1 )
__assert_fail("0", "grid.c", 0xFFu, "chooseAction");
if ( (double)rand() / 2147483647.0 < 0.2 || *(double *)&qword_4042A0[100 * a1 + 10 * a2] == 0.0 )
return getRandomAction(a1, a2);
v7 = 8;
v6 = getRandomAction(a1, a2);
while ( 1 )
{
if ( legalMove(a1, a2, v6) )
{
v9 = stateSpace[100 * a1 + 10 * a2 + (int)v6];
v8 = *(double *)&qword_4042A0[100 * a1 + 10 * a2];
if ( v9 / v8 > (double)rand() / 2147483647.0 )
break;
}
if ( ++v6 == 8 )
v6 = 0;
if ( !--v7 )
return getRandomAction(a1, a2);
}
}
else
{
findMaxQ(a1, a2);
v3 = 0LL;
v4 = 80LL * a2 + 800LL * a1;
v5 = *(double *)((char *)&qword_4042A8 + v4);
while ( 1 )
{
v6 = v3;
if ( v5 == *(double *)((char *)&stateSpace[v3] + v4) && legalMove(a1, a2, v3) )
break;
if ( ++v3 == 8 )
__assert_fail("0", "grid.c", 0xD6u, "chooseAction");
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, int a2)
{
__int64 v3; // rcx
__int64 v4; // r8
double v5; // xmm0_8
unsigned int v6; // er12
int v7; // ebx
double v8; // [rsp+0h] [rbp-48h]
double v9; // [rsp+8h] [rbp-40h]

if ( actionSelection )
{
if ( actionSelection != 1 )
__assert_fail("0", "grid.c", 0xFFu, "chooseAction");
if ( (double)rand() / 2147483647.0 < 0.2 || *(double *)&qword_4042A0[100 * a1 + 10 * a2] == 0.0 )
return func11(a1, a2);
v7 = 8;
v6 = func11(a1, a2);
while ( 1 )
{
if ( func10(a1, a2, v6) )
{
v9 = stateSpace[100 * a1 + 10 * a2 + (int)v6];
v8 = *(double *)&qword_4042A0[100 * a1 + 10 * a2];
if ( v9 / v8 > (double)rand() / 2147483647.0 )
break;
}
if ( ++v6 == 8 )
v6 = 0;
if ( !--v7 )
return func11(a1, a2);
}
}
else
{
func8(a1, a2);
v3 = 0LL;
v4 = 80LL * a2 + 800LL * a1;
v5 = *(double *)((char *)&qword_4042A8 + v4);
while ( 1 )
{
v6 = v3;
if ( v5 == *(double *)((char *)&stateSpace[v3] + v4) && func10(a1, a2, v3) )
break;
if ( ++v3 == 8 )
__assert_fail("0", "grid.c", 0xD6u, "chooseAction");
}
}
return v6;
}

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: prob double


updateFunction

decompiled_code:
__int64 __fastcall updateFunction(int a1, int a2, int a3)
{
int v3; // edi
int v4; // esi
double v5; // xmm2_8
int v6; // er8
int v7; // er9
__int64 v8; // r10
__int64 v9; // r10
__int64 v10; // r11

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

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()
{
char *v0; // rbp
char *v1; // r12
char *v2; // rbx
__int64 v3; // rsi
int v4; // ebx
int i; // er12
int v6; // eax
bool v7; // zf
char *v8; // rbx
__int64 v9; // rsi

v0 = (char *)&unk_4040C8;
v1 = (char *)&unk_4040C8;
actionSelection = 0;
putchar(10);
do
{
v2 = v1 - 40;
do
{
v3 = *(unsigned int *)v2;
v2 += 4;
printf("%2d ", v3);
}
while ( v2 != v1 );
v1 = v2 + 40;
putchar(10);
}
while ( v2 != (char *)&_bss_start );
putchar(10);
v4 = dword_40409C;
for ( i = start; ; i += dir[2 * v6] )
{
v7 = HIDWORD(end) == v4;
grid[10 * i + v4] = 0;
if ( v7 && (_DWORD)end == i )
break;
v6 = chooseAction(i, v4);
v4 += dword_402044[2 * v6];
}
putchar(10);
do
{
v8 = v0 - 40;
do
{
v9 = *(unsigned int *)v8;
v8 += 4;
printf("%2d ", v9);
}
while ( v8 != v0 );
v0 = v8 + 40;
putchar(10);
}
while ( v8 != (char *)&_bss_start );
return putchar(10);
}

canonical_decompiled_code:
int func14()
{
char *v0; // rbp
char *v1; // r12
char *v2; // rbx
__int64 v3; // rsi
int v4; // ebx
int i; // er12
int v6; // eax
bool v7; // zf
char *v8; // rbx
__int64 v9; // rsi

v0 = (char *)&unk_4040C8;
v1 = (char *)&unk_4040C8;
actionSelection = 0;
putchar(10);
do
{
v2 = v1 - 40;
do
{
v3 = *(unsigned int *)v2;
v2 += 4;
printf("%2d ", v3);
}
while ( v2 != v1 );
v1 = v2 + 40;
putchar(10);
}
while ( v2 != (char *)&_bss_start );
putchar(10);
v4 = dword_40409C;
for ( i = start; ; i += dir[2 * v6] )
{
v7 = HIDWORD(end) == v4;
grid[10 * i + v4] = 0;
if ( v7 && (_DWORD)end == i )
break;
v6 = func12(i, v4);
v4 += dword_402044[2 * v6];
}
putchar(10);
do
{
v8 = v0 - 40;
do
{
v9 = *(unsigned int *)v8;
v8 += 4;
printf("%2d ", v9);
}
while ( v8 != v0 );
v0 = v8 + 40;
putchar(10);
}
while ( v8 != (char *)&_bss_start );
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)
{
__int64 v3; // r13
unsigned int v4; // ebp
unsigned int v5; // er12
time_t v6; // rdi
unsigned int v7; // eax
__int64 v8; // rbx

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

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

v3 = 100000LL;
v4 = dword_40409C;
v5 = start;
v6 = time(0LL);
srand(v6);
func7(v6, argv);
do
{
while ( 1 )
{
v7 = func12(v5, v4);
v8 = (int)v7;
func13(v5, v4, v7);
v4 += dword_402044[2 * v8];
v5 += dir[2 * v8];
if ( end == __PAIR64__(v4, v5) )
break;
if ( !--v3 )
goto LABEL_5;
}
v4 = dword_40409C;
v5 = start;
--v3;
}
while ( v3 );
LABEL_5:
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;
visited = 0LL;
memset(&adjMatrix, 0, 0x190uLL);
xmmword_4040D0 = 0LL;
qword_4040E0 = 0LL;
return result;
}

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

result = 0LL;
visited = 0LL;
memset(&adjMatrix, 0, 0x190uLL);
xmmword_4040D0 = 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

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

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

result = stack_index;
if ( stack_index > 9 )
__assert_fail("stack_index < MAX_STACK", "dfs.c", 0x34u, "push");
stack[stack_index] = a1;
stack_index = result + 1;
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)
{
unsigned __int64 v2; // rbx
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
char v7[96]; // [rsp+0h] [rbp-D8h] BYREF
char s[120]; // [rsp+60h] [rbp-78h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func0(char *s1, char *a2)
{
unsigned __int64 v2; // rbx
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
char v7[96]; // [rsp+0h] [rbp-D8h] BYREF
char s[120]; // [rsp+60h] [rbp-78h] BYREF

v2 = (unsigned int)((_DWORD)&workingMemory + 4);
do
{
if ( *(_DWORD *)(v2 - 4) )
{
_isoc99_sscanf(v2, "(%s %s)", v7, s);
v3 = strlen(s);
if ( v7[v3 + 95] == 41 )
v7[v3 + 95] = 0;
v4 = strlen(s1);
if ( !strncmp(s1, v7, v4) )
{
v5 = strlen(a2);
if ( !strncmp(a2, s, v5) )
return 1LL;
}
}
v2 += 96LL;
}
while ( (_UNKNOWN *)v2 != (_UNKNOWN *)((char *)&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 v2; // rbx
size_t v3; // rax
__int64 v4; // r15
size_t v5; // rax
size_t v6; // rax
unsigned int v7; // er15
size_t v8; // rax
unsigned __int64 v9; // rax
void *v10; // rdi
_QWORD *i; // rdi
__int64 v13; // [rsp+0h] [rbp-1C8h]
char s1[96]; // [rsp+10h] [rbp-1B8h] BYREF
char s[96]; // [rsp+70h] [rbp-158h] BYREF
char s2[96]; // [rsp+D0h] [rbp-F8h] BYREF
char src[152]; // [rsp+130h] [rbp-98h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func1(int a1, char *a2)
{
__int64 v2; // rbx
size_t v3; // rax
__int64 v4; // r15
size_t v5; // rax
size_t v6; // rax
unsigned int v7; // er15
size_t v8; // rax
unsigned __int64 v9; // rax
void *v10; // rdi
_QWORD *i; // rdi
__int64 v13; // [rsp+0h] [rbp-1C8h]
char s1[96]; // [rsp+10h] [rbp-1B8h] BYREF
char s[96]; // [rsp+70h] [rbp-158h] BYREF
char s2[96]; // [rsp+D0h] [rbp-F8h] BYREF
char src[152]; // [rsp+130h] [rbp-98h] BYREF

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

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
__int128 v2[5]; // [rsp+0h] [rbp-58h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 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
__int128 v2[5]; // [rsp+0h] [rbp-58h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 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; // eax
int v2; // edx
__int64 result; // rax

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

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

v1 = write;
v2 = write;
queue[write] = a1;
result = (unsigned int)(v1 + 1);
if ( v2 == 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; // er8

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; // er8

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; // ebx
__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 );
result = (int)result;
v3 = *(_DWORD *)(a1 + 4LL * v1);
*(_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; // ebx
__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 );
result = (int)result;
v3 = *(_DWORD *)(a1 + 4LL * v1);
*(_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()
{
unsigned int v0; // er12
char *i; // rbx

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

canonical_decompiled_code:
void func0()
{
unsigned int v0; // er12
char *i; // rbx

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

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 fd, __int64 a2)
{
int v2; // er15
int v3; // er13
_BYTE *v4; // rbx
int v5; // ecx
int v6; // eax
__int64 result; // rax

if ( a2 )
{
v2 = 0;
v3 = 0;
LABEL_3:
while ( 1 )
{
v4 = (_BYTE *)(a2 + v3);
v5 = read(fd, v4, 2048 - v3);
v6 = v3;
v3 += v5;
if ( v3 > v6 )
break;
LABEL_20:
if ( v3 + v5 > 1968 )
goto LABEL_16;
}
if ( v2 )
{
if ( v2 != 1 )
{
if ( v2 != 2 )
goto LABEL_15;
goto LABEL_13;
}
if ( *v4 == 10 )
{
LABEL_12:
++v6;
v2 = 2;
if ( v3 <= v6 )
goto LABEL_20;
LABEL_13:
if ( *(_BYTE *)(a2 + v6) == 13 )
{
++v6;
v2 = 3;
if ( v3 <= v6 )
goto LABEL_20;
LABEL_15:
if ( *(_BYTE *)(a2 + v6) == 10 )
goto LABEL_16;
}
}
goto LABEL_7;
}
while ( 1 )
{
if ( *v4 == 13 )
{
if ( v3 <= ++v6 )
{
v2 = 1;
if ( v3 + v5 > 1968 )
{
LABEL_16:
result = (unsigned int)(v3 - 3);
*(_BYTE *)(a2 + (int)result) = 0;
return result;
}
goto LABEL_3;
}
if ( *(_BYTE *)(a2 + v6) == 10 )
goto LABEL_12;
}
LABEL_7:
if ( v3 <= ++v6 )
{
v2 = 0;
goto LABEL_20;
}
v4 = (_BYTE *)(a2 + v6);
}
}
return 0xFFFFFFFFLL;
}

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; // er12
struct hostent *v4; // rax
int v5; // er13
int v6; // er13
char v7; // [rsp+Ch] [rbp-89Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-898h] BYREF
char cp[80]; // [rsp+20h] [rbp-888h] BYREF
__m128i buf; // [rsp+70h] [rbp-838h] BYREF
char v11; // [rsp+80h] [rbp-828h]

v1 = socket(2, 1, 0);
prune((char *)&monitors + 140 * a1 + 4, (__int64)cp);
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16_constprop_0(cp);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
v4 = gethostbyname(cp);
if ( !v4 )
{
v2 = -1;
close(v1);
return v2;
}
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v4->h_addr_list;
}
v2 = connect(v1, &addr, 0x10u);
if ( !v2
&& (v11 = 0, buf = _mm_load_si128((const __m128i *)&LC2), v5 = write(v1, &buf, 0x10uLL), v5 == strlen(buf.m128i_i8))
&& (v6 = grabResponse(v1, (__int64)&buf), strstr(buf.m128i_i8, "Content-Length:")) )
{
_isoc99_sscanf(&buf, "Content-Length: %d", &v7);
if ( *((_DWORD *)&monitors + 35 * a1 + 32) != v6 )
{
*((_DWORD *)&monitors + 35 * a1 + 32) = v6;
v2 = 1;
*(_QWORD *)((char *)&monitors + 140 * a1 + 132) = 1LL;
}
}
else
{
v2 = 0;
}
close(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v1; // ebp
unsigned int v2; // er12
struct hostent *v4; // rax
int v5; // er13
int v6; // er13
char v7; // [rsp+Ch] [rbp-89Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-898h] BYREF
char cp[80]; // [rsp+20h] [rbp-888h] BYREF
__m128i buf; // [rsp+70h] [rbp-838h] BYREF
char v11; // [rsp+80h] [rbp-828h]

v1 = socket(2, 1, 0);
func1((char *)&monitors + 140 * a1 + 4, (__int64)cp);
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(cp);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
v4 = gethostbyname(cp);
if ( !v4 )
{
v2 = -1;
close(v1);
return v2;
}
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v4->h_addr_list;
}
v2 = connect(v1, &addr, 0x10u);
if ( !v2
&& (v11 = 0, buf = _mm_load_si128((const __m128i *)&LC2), v5 = write(v1, &buf, 0x10uLL), v5 == strlen(buf.m128i_i8))
&& (v6 = func2(v1, (__int64)&buf), strstr(buf.m128i_i8, "Content-Length:")) )
{
_isoc99_sscanf(&buf, "Content-Length: %d", &v7);
if ( *((_DWORD *)&monitors + 35 * a1 + 32) != v6 )
{
*((_DWORD *)&monitors + 35 * a1 + 32) = v6;
v2 = 1;
*(_QWORD *)((char *)&monitors + 140 * a1 + 132) = 1LL;
}
}
else
{
v2 = 0;
}
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 i; // er8

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i = v1 + 33 * i;
}
return i;
}

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

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i = v1 + 33 * i;
}
return i;
}

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)
{
rlim_t v4; // r14
rlim_t v5; // rbp
unsigned int v6; // er12
char *v8; // rax
struct rlimit v9[3]; // [rsp+0h] [rbp-38h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, char a3)
{
rlim_t v4; // r14
rlim_t v5; // rbp
unsigned int v6; // er12
char *v8; // rax
struct rlimit v9[3]; // [rsp+0h] [rbp-38h] BYREF

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

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:
__int64 __fastcall c_ulimit(__int64 a1)
{
char v1; // r13
_BOOL4 v2; // er12
int v4; // eax
__int64 result; // rax
int v6; // edx
int v7; // eax
char **v8; // rdi
const char *v9; // rsi
const char **v10; // rbx
const char *v11; // rsi
__int64 v12; // rdi
const char *v13; // rsi

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
char v1; // r13
_BOOL4 v2; // er12
int v4; // eax
__int64 result; // rax
int v6; // edx
int v7; // eax
char **v8; // rdi
const char *v9; // rsi
const char **v10; // rbx
const char *v11; // rsi
__int64 v12; // rdi
const char *v13; // rsi

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

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; // ebp
int *v2; // rbx

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

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

v0 = _errno_location();
v1 = *v0;
v2 = v0;
if ( ksh_tmout_state == 1 )
{
LODWORD(v0) = alarm(0);
if ( !(_DWORD)v0 )
{
ksh_tmout_state = 2;
intrsig = 1;
*v2 = v1;
return (int)v0;
}
LODWORD(v0) = alarm((unsigned int)v0);
}
*v2 = v1;
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