gencode

decompiled_code:
void __fastcall gencode(__int64 a1, unsigned int *a2, int a3)
{
__int64 v4; // rdi
__int64 v5; // rax
int v6; // eax
int v7; // edx
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rdx
__int64 v11; // rdx
unsigned int *v12; // rbp
int v13; // eax
unsigned int *v14; // rbp
int v15; // eax
__int64 v16[4]; // [rsp+8h] [rbp-20h] BYREF

v16[0] = (__int64)a2;
v4 = *(_QWORD *)(a1 + 24);
if ( v4 )
{
v5 = *(_QWORD *)(a1 + 32);
if ( v5 && *(_DWORD *)a1 == 1 )
{
v6 = *(_DWORD *)(v5 + 16);
if ( v6 )
{
v7 = *(_DWORD *)(v4 + 16);
if ( v6 > v7 && v7 > 0 )
{
v16[0] = (__int64)swap(v16[0]);
gencode(*(_QWORD *)(a1 + 32), v16[0], 1LL);
v12 = pop((__int64)v16);
gencode(*(_QWORD *)(a1 + 24), v16[0], 0LL);
v13 = *(_DWORD *)(a1 + 8);
switch ( v13 )
{
case '+':
fprintf(stderr, "ADD R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
case '-':
fprintf(stderr, "SUB R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
case '*':
fprintf(stderr, "MUL R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
v16[0] = push(v16[0], (__int64)v12);
swap(v16[0]);
}
else if ( v6 <= v7 && v6 > 0 )
{
gencode(v4, v16[0], 0LL);
v14 = pop((__int64)v16);
gencode(*(_QWORD *)(a1 + 32), v16[0], 1LL);
v15 = *(_DWORD *)(a1 + 8);
switch ( v15 )
{
case '+':
fprintf(stderr, "ADD R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
case '-':
fprintf(stderr, "SUB R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
case '*':
fprintf(stderr, "MUL R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
push(v16[0], (__int64)v14);
}
else
{
fwrite("shouldn't get to gencode : case 4\n", 1uLL, 0x22uLL, stderr);
}
}
else
{
gencode(v4, v16[0], 0LL);
v8 = *(_DWORD *)(a1 + 8);
switch ( v8 )
{
case '+':
v9 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v9 )
{
if ( *(_DWORD *)v9 == 2 )
fprintf(stderr, "ADD %s, R%d\n", **(const char ***)(v9 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "ADD %d, R%d\n", *(unsigned int *)(v9 + 8), *(unsigned int *)v16[0]);
}
break;
case '-':
v10 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v10 )
{
if ( *(_DWORD *)v10 == 2 )
fprintf(stderr, "SUB %s, R%d\n", **(const char ***)(v10 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "SUB %d, R%d\n", *(unsigned int *)(v10 + 8), *(unsigned int *)v16[0]);
}
break;
case '*':
v11 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v11 )
{
if ( *(_DWORD *)v11 == 2 )
fprintf(stderr, "MUL %s, R%d\n", **(const char ***)(v11 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "MUL %d, R%d\n", *(unsigned int *)(v11 + 8), *(unsigned int *)v16[0]);
}
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
}
}
}
else if ( !*(_QWORD *)(a1 + 32) && !a3 )
{
if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 2 )
fprintf(stderr, "MOV %s, R%d\n", **(const char ***)(a1 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "MOV %d, R%d\n", *(unsigned int *)(a1 + 8), *a2);
}
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, unsigned int *a2, int a3)
{
__int64 v4; // rdi
__int64 v5; // rax
int v6; // eax
int v7; // edx
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rdx
__int64 v11; // rdx
unsigned int *v12; // rbp
int v13; // eax
unsigned int *v14; // rbp
int v15; // eax
__int64 v16[4]; // [rsp+8h] [rbp-20h] BYREF

v16[0] = (__int64)a2;
v4 = *(_QWORD *)(a1 + 24);
if ( v4 )
{
v5 = *(_QWORD *)(a1 + 32);
if ( v5 && *(_DWORD *)a1 == 1 )
{
v6 = *(_DWORD *)(v5 + 16);
if ( v6 )
{
v7 = *(_DWORD *)(v4 + 16);
if ( v6 > v7 && v7 > 0 )
{
v16[0] = (__int64)swap(v16[0]);
func7(*(_QWORD *)(a1 + 32), v16[0], 1LL);
v12 = pop((__int64)v16);
func7(*(_QWORD *)(a1 + 24), v16[0], 0LL);
v13 = *(_DWORD *)(a1 + 8);
switch ( v13 )
{
case '+':
fprintf(stderr, "ADD R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
case '-':
fprintf(stderr, "SUB R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
case '*':
fprintf(stderr, "MUL R%d, R%d\n", *v12, *(unsigned int *)v16[0]);
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
v16[0] = func3(v16[0], (__int64)v12);
swap(v16[0]);
}
else if ( v6 <= v7 && v6 > 0 )
{
func7(v4, v16[0], 0LL);
v14 = pop((__int64)v16);
func7(*(_QWORD *)(a1 + 32), v16[0], 1LL);
v15 = *(_DWORD *)(a1 + 8);
switch ( v15 )
{
case '+':
fprintf(stderr, "ADD R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
case '-':
fprintf(stderr, "SUB R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
case '*':
fprintf(stderr, "MUL R%d, R%d\n", *(unsigned int *)v16[0], *v14);
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
func3(v16[0], (__int64)v14);
}
else
{
fwrite("shouldn't get to gencode : case 4\n", 1uLL, 0x22uLL, stderr);
}
}
else
{
func7(v4, v16[0], 0LL);
v8 = *(_DWORD *)(a1 + 8);
switch ( v8 )
{
case '+':
v9 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v9 )
{
if ( *(_DWORD *)v9 == 2 )
fprintf(stderr, "ADD %s, R%d\n", **(const char ***)(v9 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "ADD %d, R%d\n", *(unsigned int *)(v9 + 8), *(unsigned int *)v16[0]);
}
break;
case '-':
v10 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v10 )
{
if ( *(_DWORD *)v10 == 2 )
fprintf(stderr, "SUB %s, R%d\n", **(const char ***)(v10 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "SUB %d, R%d\n", *(unsigned int *)(v10 + 8), *(unsigned int *)v16[0]);
}
break;
case '*':
v11 = *(_QWORD *)(a1 + 32);
if ( *(_DWORD *)v11 )
{
if ( *(_DWORD *)v11 == 2 )
fprintf(stderr, "MUL %s, R%d\n", **(const char ***)(v11 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "MUL %d, R%d\n", *(unsigned int *)(v11 + 8), *(unsigned int *)v16[0]);
}
break;
default:
fwrite("Unrecognized op value in gencode : case 1\n", 1uLL, 0x2AuLL, stderr);
break;
}
}
}
}
else if ( !*(_QWORD *)(a1 + 32) && !a3 )
{
if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 2 )
fprintf(stderr, "MOV %s, R%d\n", **(const char ***)(a1 + 8), *(unsigned int *)v16[0]);
}
else
{
fprintf(stderr, "MOV %d, R%d\n", *(unsigned int *)(a1 + 8), *a2);
}
}
}

original_code:
void gencode( t_node *root, r_node *rstack, int type ) {
 if( root->left == ((void *)0) && root->right == ((void *)0) && type == 0 ) {
  if( root->type == num )
   fprintf( stderr, "MOV %d, R%d\n", root->attr.ival, rstack->number );
  else if( root->type == id )
   fprintf( stderr, "MOV %s, R%d\n", root->attr.id->name, rstack->number );
 }
 else if( root->left != ((void *)0) && root->right != ((void *)0) && root->type == op ) {
  if( root->right->gcl == 0 ) {
   gencode( root->left, rstack, 0 );
   switch( root->attr.oval ) {
   case '+':
    if( root->right->type == num )
     fprintf( stderr, "ADD %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "ADD %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   case '-':
    if( root->right->type == num )
     fprintf( stderr, "SUB %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "SUB %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   case '*':
    if( root->right->type == num )
     fprintf( stderr, "MUL %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "MUL %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
  }
  else if( 1 <= root->left->gcl && root->left->gcl < root->right->gcl ) {
   rstack = swap( rstack );
   gencode( root->right, rstack, 1 );
   r_node *R = pop( &rstack );
   gencode( root->left, rstack, 0 );
   switch( root->attr.oval ) {
   case '+':
    fprintf( stderr, "ADD R%d, R%d\n", R->number, rstack->number );
    break;
   case '-':
    fprintf( stderr, "SUB R%d, R%d\n", R->number, rstack->number );
    break;
   case '*':
    fprintf( stderr, "MUL R%d, R%d\n", R->number, rstack->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
   rstack = push( rstack, R );
   rstack = swap( rstack );
  }
  else if( 1 <= root->right->gcl && root->right->gcl <= root->left->gcl ) {
   gencode( root->left, rstack, 0 );
   r_node *R = pop( &rstack );
   gencode( root->right, rstack, 1 );
   switch( root->attr.oval ) {
   case '+':
    fprintf( stderr, "ADD R%d, R%d\n", rstack->number, R->number );
    break;
   case '-':
    fprintf( stderr, "SUB R%d, R%d\n", rstack->number, R->number );
    break;
   case '*':
    fprintf( stderr, "MUL R%d, R%d\n", rstack->number, R->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
   rstack = push( rstack, R );
  }
  else {
   fprintf( stderr, "shouldn't get to gencode : case 4\n" );
  }
 }
}

canonical_original_code:
void gencode(struct t_node *root, struct r_node *rstack, int type) {
 if( root->left == ((void *)0) && root->right == ((void *)0) && type == 0 ) {
  if( root->type == num )
   fprintf( stderr, "MOV %d, R%d\n", root->attr.ival, rstack->number );
  else if( root->type == id )
   fprintf( stderr, "MOV %s, R%d\n", root->attr.id->name, rstack->number );
 }
 else if( root->left != ((void *)0) && root->right != ((void *)0) && root->type == op ) {
  if( root->right->gcl == 0 ) {
   gencode( root->left, rstack, 0 );
   switch( root->attr.oval ) {
   case '+':
    if( root->right->type == num )
     fprintf( stderr, "ADD %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "ADD %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   case '-':
    if( root->right->type == num )
     fprintf( stderr, "SUB %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "SUB %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   case '*':
    if( root->right->type == num )
     fprintf( stderr, "MUL %d, R%d\n", root->right->attr.ival, rstack->number );
    else if( root->right->type == id )
     fprintf( stderr, "MUL %s, R%d\n", root->right->attr.id->name, rstack->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
  }
  else if( 1 <= root->left->gcl && root->left->gcl < root->right->gcl ) {
   rstack = swap( rstack );
   gencode( root->right, rstack, 1 );
   struct r_node *R = pop( &rstack );
   gencode( root->left, rstack, 0 );
   switch( root->attr.oval ) {
   case '+':
    fprintf( stderr, "ADD R%d, R%d\n", R->number, rstack->number );
    break;
   case '-':
    fprintf( stderr, "SUB R%d, R%d\n", R->number, rstack->number );
    break;
   case '*':
    fprintf( stderr, "MUL R%d, R%d\n", R->number, rstack->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
   rstack = push( rstack, R );
   rstack = swap( rstack );
  }
  else if( 1 <= root->right->gcl && root->right->gcl <= root->left->gcl ) {
   gencode( root->left, rstack, 0 );
   struct r_node *R = pop( &rstack );
   gencode( root->right, rstack, 1 );
   switch( root->attr.oval ) {
   case '+':
    fprintf( stderr, "ADD R%d, R%d\n", rstack->number, R->number );
    break;
   case '-':
    fprintf( stderr, "SUB R%d, R%d\n", rstack->number, R->number );
    break;
   case '*':
    fprintf( stderr, "MUL R%d, R%d\n", rstack->number, R->number );
    break;
   default:
    fprintf( stderr, "Unrecognized op value in gencode : case 1\n" );
   }
   rstack = push( rstack, R );
  }
  else {
   fprintf( stderr, "shouldn't get to gencode : case 4\n" );
  }
 }
}


decompiled_code:
void __fastcall destroy_entry(void *ptr)
{
if ( ptr )
{
destroy_entry(*((void **)ptr + 2));
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
func0(*((void **)ptr + 2));
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void destroy_entry( entry *to_delete ) {
 if( to_delete == ((void *)0) )
  return;
 else {
  destroy_entry( to_delete->next );
  free( to_delete->name );
  free( to_delete );
 }
}

canonical_original_code:
void destroy_entry(struct entry *to_delete) {
 if( to_delete == ((void *)0) )
  return;
 else {
  destroy_entry( to_delete->next );
  free( to_delete->name );
  free( to_delete );
 }
}

variable_types: struct entry { char *name; int value; struct entry *next; } *

return_type: void


decompiled_code:
const char **__fastcall find_entry(const char ***a1, const char *a2)
{
const char ***v2; // r14
const char **v3; // rbx
const char ***v4; // r13
size_t v5; // rbp

v2 = a1;
v3 = (const char **)a1;
if ( a1 )
{
while ( 2 )
{
v4 = v2;
do
{
v3 = *v4;
if ( *v4 )
{
v5 = strlen(a2);
while ( strncmp(a2, *v3, v5) )
{
v3 = (const char **)v3[2];
if ( !v3 )
goto LABEL_3;
}
return v3;
}
LABEL_3:
++v4;
}
while ( v4 != v2 + 211 );
v2 = (const char ***)v2[211];
if ( v2 )
continue;
break;
}
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
const char **__fastcall func1(const char ***a1, const char *a2)
{
const char ***v2; // r14
const char **v3; // rbx
const char ***v4; // r13
size_t v5; // rbp

v2 = a1;
v3 = (const char **)a1;
if ( a1 )
{
while ( 2 )
{
v4 = v2;
do
{
v3 = *v4;
if ( *v4 )
{
v5 = strlen(a2);
while ( strncmp(a2, *v3, v5) )
{
v3 = (const char **)v3[2];
if ( !v3 )
goto LABEL_3;
}
return v3;
}
LABEL_3:
++v4;
}
while ( v4 != v2 + 211 );
v2 = (const char ***)v2[211];
if ( v2 )
continue;
break;
}
v3 = 0LL;
}
return v3;
}

original_code:
entry *find_entry( scope *stack, char *name ) {
 scope *s = stack;
 entry *ptr = ((void *)0);
 int i;
 while( s != ((void *)0) ) {
  for( i = 0; i < 211; i++ ) {
   if( s->sym_t[ i ] != ((void *)0) ) {
    ptr = s->sym_t[ i ];
    while( ptr != ((void *)0) ) {
     if( strncmp( name, ptr->name, strlen( name ) ) == 0 )
      return ptr;
     ptr = ptr->next;
    }
   }
  }
  s = s->parent;
 }
 return ((void *)0);
}

canonical_original_code:
struct entry *find_entry(struct scope *stack, char *name) {
 struct scope *s = stack;
 struct entry *ptr = ((void *)0);
 int i;
 while( s != ((void *)0) ) {
  for( i = 0; i < 211; i++ ) {
   if( s->sym_t[ i ] != ((void *)0) ) {
    ptr = s->sym_t[ i ];
    while( ptr != ((void *)0) ) {
     if( strncmp( name, ptr->name, strlen( name ) ) == 0 )
      return ptr;
     ptr = ptr->next;
    }
   }
  }
  s = s->parent;
 }
 return ((void *)0);
}

variable_types: struct scope { struct entry *sym_t[211]; struct scope *parent; } *

variable_types:
name
char *

variable_types: struct scope { struct entry { char *name; int value; struct entry *next; } *sym_t[211]; struct scope *parent; } *

variable_types: i int


decompiled_code:
__int64 __fastcall pop_scope(void *ptr)
{
__int64 v1; // r13
void **v2; // rbx

if ( !ptr )
return 0LL;
v1 = *((_QWORD *)ptr + 211);
v2 = (void **)ptr;
do
{
if ( *v2 )
destroy_entry(*v2);
++v2;
}
while ( v2 != (void **)((char *)ptr + 1688) );
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
__int64 v1; // r13
void **v2; // rbx

if ( !ptr )
return 0LL;
v1 = *((_QWORD *)ptr + 211);
v2 = (void **)ptr;
do
{
if ( *v2 )
func0(*v2);
++v2;
}
while ( v2 != (void **)((char *)ptr + 1688) );
free(ptr);
return v1;
}

original_code:
scope *pop_scope( scope *stack ) {
 scope *ptr;
 int i;
 if( stack == ((void *)0) )
  return ((void *)0);
 else {
  ptr = stack->parent;
  for( i = 0; i < 211; i++ ) {
   if( stack->sym_t[ i ] != ((void *)0) )
    destroy_entry( stack->sym_t[ i ] );
  }
  free( stack );
  return ptr;
 }
}

canonical_original_code:
struct scope *pop_scope(struct scope *stack) {
 struct scope *ptr;
 int i;
 if( stack == ((void *)0) )
  return ((void *)0);
 else {
  ptr = stack->parent;
  for( i = 0; i < 211; i++ ) {
   if( stack->sym_t[ i ] != ((void *)0) )
    destroy_entry( stack->sym_t[ i ] );
  }
  free( stack );
  return ptr;
 }
}

variable_types:
stack
struct scope *

return_type: struct scope { struct entry { char *name; int value; struct entry *next; } *sym_t[211]; struct scope *parent; } *

user_defined_types: struct scope { struct entry *sym_t[211]; struct scope *parent; }


hashpjw

original_code:
int hashpjw( const void *key, int tablesize ) {
 const char *ptr = key;
 int val = 0;
 while( *ptr != '\0' ) {
  int tmp;
  val = ( ( val << 4 ) + ( *ptr ) );
  if( ( tmp = ( val & 0xf0000000 ) ) ) {
   val = val ^ ( tmp >> 24 );
   val = val ^ tmp;
  }
  ptr++;
 }
 return val % tablesize;
}

canonical_original_code:
int hashpjw(void *key, int tablesize) {
 char *ptr = key;
 int val = 0;
 while( *ptr != '\0' ) {
  int tmp;
  val = ( ( val << 4 ) + ( *ptr ) );
  if( ( tmp = ( val & 0xf0000000 ) ) ) {
   val = val ^ ( tmp >> 24 );
   val = val ^ tmp;
  }
  ptr++;
 }
 return val % tablesize;
}

variable_types:
key
void *

variable_types: tablesize int

variable_types:
ptr
char *

variable_types: val int

variable_types: tmp int

return_type: int


decompiled_code:
size_t __fastcall print_symstack(__int64 *a1)
{
__int64 *i; // r13
__int64 *v2; // rbp
__int64 v3; // rbx
size_t result; // rax

for ( i = a1; i; i = (__int64 *)i[211] )
{
v2 = i;
do
{
v3 = *v2;
if ( *v2 )
{
do
{
fprintf(stderr, "[%s:%d]", *(const char **)v3, *(unsigned int *)(v3 + 8));
v3 = *(_QWORD *)(v3 + 16);
}
while ( v3 );
fputc(10, stderr);
}
++v2;
}
while ( i + 211 != v2 );
result = fwrite("---------------------\n", 1uLL, 0x16uLL, stderr);
}
return result;
}

original_code:
void print_symstack( scope *stack ) {
 scope *s = stack;
 entry *e;
 int i;
 while( s != ((void *)0) ) {
  for( i = 0; i < 211; i++ ) {
   if( ( e = s->sym_t[ i ] ) != ((void *)0) ) {
    while( e != ((void *)0) ) {
     fprintf( stderr, "[%s:%d]", e->name, e->value );
     e = e->next;
    }
    fprintf( stderr, "\n" );
   }
  }
  fprintf( stderr, "---------------------\n" );
  s = s->parent;
 }
}

canonical_original_code:
void print_symstack(struct scope *stack) {
 struct scope *s = stack;
 struct entry *e;
 int i;
 while( s != ((void *)0) ) {
  for( i = 0; i < 211; i++ ) {
   if( ( e = s->sym_t[ i ] ) != ((void *)0) ) {
    while( e != ((void *)0) ) {
     fprintf( stderr, "[%s:%d]", e->name, e->value );
     e = e->next;
    }
    fprintf( stderr, "\n" );
   }
  }
  fprintf( stderr, "---------------------\n" );
  s = s->parent;
 }
}

user_defined_types: struct entry { char *name; int value; struct entry *next; }


decompiled_code:
__int64 __fastcall eval_tree(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
int v3; // ebp
__int64 result; // rax
int v5; // ebp
int v6; // ebp

if ( !a1 )
_assert_fail("ptr != NULL", "semantics.c", 0xBu, "eval_tree");
v1 = *a1;
if ( (_DWORD)v1 == 1 )
{
v2 = a1[2];
switch ( (_DWORD)v2 )
{
case '+':
v3 = eval_tree(*((_QWORD *)a1 + 3));
result = v3 + (unsigned int)eval_tree(*((_QWORD *)a1 + 4));
break;
case '-':
v6 = eval_tree(*((_QWORD *)a1 + 3));
result = v6 - (unsigned int)eval_tree(*((_QWORD *)a1 + 4));
break;
case '*':
v5 = eval_tree(*((_QWORD *)a1 + 3));
result = v5 * (unsigned int)eval_tree(*((_QWORD *)a1 + 4));
break;
default:
fprintf(stderr, "Unknown operator type: %d\n", v2);
exit(1);
}
}
else if ( (_DWORD)v1 == 2 )
{
result = *(unsigned int *)(*((_QWORD *)a1 + 1) + 8LL);
}
else
{
if ( (_DWORD)v1 )
{
fprintf(stderr, "Unknown node type: %d\n", v1);
exit(1);
}
result = a1[2];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
int v3; // ebp
__int64 result; // rax
int v5; // ebp
int v6; // ebp

if ( !a1 )
_assert_fail("ptr != NULL", "semantics.c", 0xBu, "eval_tree");
v1 = *a1;
if ( (_DWORD)v1 == 1 )
{
v2 = a1[2];
switch ( (_DWORD)v2 )
{
case '+':
v3 = func0(*((_QWORD *)a1 + 3));
result = v3 + (unsigned int)func0(*((_QWORD *)a1 + 4));
break;
case '-':
v6 = func0(*((_QWORD *)a1 + 3));
result = v6 - (unsigned int)func0(*((_QWORD *)a1 + 4));
break;
case '*':
v5 = func0(*((_QWORD *)a1 + 3));
result = v5 * (unsigned int)func0(*((_QWORD *)a1 + 4));
break;
default:
fprintf(stderr, "Unknown operator type: %d\n", v2);
exit(1);
}
}
else if ( (_DWORD)v1 == 2 )
{
result = *(unsigned int *)(*((_QWORD *)a1 + 1) + 8LL);
}
else
{
if ( (_DWORD)v1 )
{
fprintf(stderr, "Unknown node type: %d\n", v1);
exit(1);
}
result = a1[2];
}
return result;
}

original_code:
int eval_tree( t_node *ptr ) {
 ((void) sizeof ((ptr != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr != ((void *)0)) ; else __assert_fail ("ptr != NULL", "semantics.c", 11, __extension__ __PRETTY_FUNCTION__); }));
 switch( ptr->type ) {
 case op:
  switch( ptr->attr.oval ) {
  case '+':
   return eval_tree( ptr->left ) + eval_tree( ptr->right );
  case '*':
   return eval_tree( ptr->left ) * eval_tree( ptr->right );
  case '-':
   return eval_tree( ptr->left ) - eval_tree( ptr->right );
  default:
   fprintf( stderr, "Unknown operator type: %d\n", ptr->attr.oval );
   exit( 1 );
  }
 case num:
  return ptr->attr.ival;
 case id:
  return ptr->attr.id->value;
 default:
  fprintf( stderr, "Unknown node type: %d\n", ptr->type );
  exit( 1 );
 }
}

canonical_original_code:
int eval_tree(struct t_node *ptr) {
 ((void) sizeof ((ptr != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr != ((void *)0)) ; else __assert_fail ("ptr != NULL", "semantics.c", 11, __extension__ __PRETTY_FUNCTION__); }));
 switch( ptr->type ) {
 case op:
  switch( ptr->attr.oval ) {
  case '+':
   return eval_tree( ptr->left ) + eval_tree( ptr->right );
  case '*':
   return eval_tree( ptr->left ) * eval_tree( ptr->right );
  case '-':
   return eval_tree( ptr->left ) - eval_tree( ptr->right );
  default:
   fprintf( stderr, "Unknown operator type: %d\n", ptr->attr.oval );
   exit( 1 );
  }
 case num:
  return ptr->attr.ival;
 case id:
  return ptr->attr.id->value;
 default:
  fprintf( stderr, "Unknown node type: %d\n", ptr->type );
  exit( 1 );
 }
}

variable_types: struct t_node { enum node_type { num=0, op=1, id=2 } type; union { int ival; int oval; struct entry { char *name; int value; struct entry *next; } *id; } attr; int gcl; struct t_node *left; } *

user_defined_types: struct t_node { enum node_type type; union { int ival; int oval; struct entry *id; } attr; int gcl; struct t_node *left; }


decompiled_code:
__int64 __fastcall eval_stmt(__int64 a1)
{
__int64 v2; // rdx
unsigned int v3; // ebp
__int64 v4; // r12
__int64 v6; // r13
unsigned int *v7; // rdi
__int64 v8; // rax
unsigned int *v9; // r12

if ( !a1 )
_assert_fail("ptr != NULL", "semantics.c", 0x28u, "eval_stmt");
v2 = *(unsigned int *)(a1 + 8);
if ( (_DWORD)v2 == 61 )
{
v6 = *(_QWORD *)(a1 + 24);
if ( !v6 )
_assert_fail("id != NULL", "semantics.c", 0x2Du, "eval_stmt");
v7 = *(unsigned int **)(a1 + 32);
if ( !v7 )
_assert_fail("ptr->right != NULL", "semantics.c", 0x2Eu, "eval_stmt");
v3 = eval_tree(v7);
v8 = *(_QWORD *)(v6 + 8);
if ( !v8 )
_assert_fail("id->attr.id != NULL", "semantics.c", 0x30u, "eval_stmt");
*(_DWORD *)(v8 + 8) = v3;
v9 = (unsigned int *)init_rstack();
label(*(_QWORD *)(a1 + 32), 0LL);
gencode(*(_QWORD *)(a1 + 32), v9, 0LL);
fprintf(stderr, "MOV R%d, %s\n", *v9, **(const char ***)(v6 + 8));
destroy_rstack(v9);
}
else
{
if ( (_DWORD)v2 != 112 )
{
fprintf(stderr, "Unknown operator type: %d\n", v2);
exit(1);
}
v3 = eval_tree(*(unsigned int **)(a1 + 24));
v4 = init_rstack();
label(*(_QWORD *)(a1 + 24), 0LL);
gencode(*(_QWORD *)(a1 + 24), v4, 0LL);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v2; // rdx
unsigned int v3; // ebp
__int64 v4; // r12
__int64 v6; // r13
unsigned int *v7; // rdi
__int64 v8; // rax
unsigned int *v9; // r12

if ( !a1 )
_assert_fail("ptr != NULL", "semantics.c", 0x28u, "eval_stmt");
v2 = *(unsigned int *)(a1 + 8);
if ( (_DWORD)v2 == 61 )
{
v6 = *(_QWORD *)(a1 + 24);
if ( !v6 )
_assert_fail("id != NULL", "semantics.c", 0x2Du, "eval_stmt");
v7 = *(unsigned int **)(a1 + 32);
if ( !v7 )
_assert_fail("ptr->right != NULL", "semantics.c", 0x2Eu, "eval_stmt");
v3 = func0(v7);
v8 = *(_QWORD *)(v6 + 8);
if ( !v8 )
_assert_fail("id->attr.id != NULL", "semantics.c", 0x30u, "eval_stmt");
*(_DWORD *)(v8 + 8) = v3;
v9 = (unsigned int *)init_rstack();
label(*(_QWORD *)(a1 + 32), 0LL);
gencode(*(_QWORD *)(a1 + 32), v9, 0LL);
fprintf(stderr, "MOV R%d, %s\n", *v9, **(const char ***)(v6 + 8));
destroy_rstack(v9);
}
else
{
if ( (_DWORD)v2 != 112 )
{
fprintf(stderr, "Unknown operator type: %d\n", v2);
exit(1);
}
v3 = func0(*(unsigned int **)(a1 + 24));
v4 = init_rstack();
label(*(_QWORD *)(a1 + 24), 0LL);
gencode(*(_QWORD *)(a1 + 24), v4, 0LL);
}
return v3;
}

original_code:
int eval_stmt( t_node *ptr ) {
 int value;
 t_node *id;
 r_node *rstack;
 ((void) sizeof ((ptr != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr != ((void *)0)) ; else __assert_fail ("ptr != NULL", "semantics.c", 40, __extension__ __PRETTY_FUNCTION__); }));
 switch( ptr->attr.oval ) {
 case '=':
  id = ptr->left;
  ((void) sizeof ((id != ((void *)0)) ? 1 : 0), __extension__ ({ if (id != ((void *)0)) ; else __assert_fail ("id != NULL", "semantics.c", 45, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((ptr->right != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr->right != ((void *)0)) ; else __assert_fail ("ptr->right != NULL", "semantics.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  value = eval_tree( ptr->right );
  ((void) sizeof ((id->attr.id != ((void *)0)) ? 1 : 0), __extension__ ({ if (id->attr.id != ((void *)0)) ; else __assert_fail ("id->attr.id != NULL", "semantics.c", 48, __extension__ __PRETTY_FUNCTION__); }));
  id->attr.id->value = value;
  rstack = init_rstack( );
  label( ptr->right, 0 );
  gencode( ptr->right, rstack, 0 );
  fprintf( stderr, "MOV R%d, %s\n", rstack->number, id->attr.id->name );
  destroy_rstack( rstack );
  return value;
 case 'p':
  value = eval_tree( ptr->left );
  rstack = init_rstack( );
  label( ptr->left, 0 );
  gencode( ptr->left, rstack, 0 );
  return value;
 default:
  fprintf( stderr, "Unknown operator type: %d\n", ptr->attr.oval );
  exit( 1 );
 }
}

canonical_original_code:
int eval_stmt(struct t_node *ptr) {
 int value;
 struct t_node *id;
 struct r_node *rstack;
 ((void) sizeof ((ptr != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr != ((void *)0)) ; else __assert_fail ("ptr != NULL", "semantics.c", 40, __extension__ __PRETTY_FUNCTION__); }));
 switch( ptr->attr.oval ) {
 case '=':
  id = ptr->left;
  ((void) sizeof ((id != ((void *)0)) ? 1 : 0), __extension__ ({ if (id != ((void *)0)) ; else __assert_fail ("id != NULL", "semantics.c", 45, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((ptr->right != ((void *)0)) ? 1 : 0), __extension__ ({ if (ptr->right != ((void *)0)) ; else __assert_fail ("ptr->right != NULL", "semantics.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  value = eval_tree( ptr->right );
  ((void) sizeof ((id->attr.id != ((void *)0)) ? 1 : 0), __extension__ ({ if (id->attr.id != ((void *)0)) ; else __assert_fail ("id->attr.id != NULL", "semantics.c", 48, __extension__ __PRETTY_FUNCTION__); }));
  id->attr.id->value = value;
  rstack = init_rstack( );
  label( ptr->right, 0 );
  gencode( ptr->right, rstack, 0 );
  fprintf( stderr, "MOV R%d, %s\n", rstack->number, id->attr.id->name );
  destroy_rstack( rstack );
  return value;
 case 'p':
  value = eval_tree( ptr->left );
  rstack = init_rstack( );
  label( ptr->left, 0 );
  gencode( ptr->left, rstack, 0 );
  return value;
 default:
  fprintf( stderr, "Unknown operator type: %d\n", ptr->attr.oval );
  exit( 1 );
 }
}

variable_types: value int

variable_types: struct r_node { int number; struct r_node *next; } *


decompiled_code:
void __fastcall destroy_node(_QWORD *a1)
{
if ( a1[3] )
destroy_node();
if ( a1[4] )
destroy_node();
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(_QWORD *a1)
{
if ( a1[3] )
func0();
if ( a1[4] )
func0();
free(a1);
}

original_code:
void destroy_node( t_node *to_delete ) {
 if( to_delete->left != ((void *)0) )
  destroy_node( to_delete->left );
 if( to_delete->right != ((void *)0) )
  destroy_node( to_delete->right );
 free( to_delete );
}

canonical_original_code:
void destroy_node(struct t_node *to_delete) {
 if( to_delete->left != ((void *)0) )
  destroy_node( to_delete->left );
 if( to_delete->right != ((void *)0) )
  destroy_node( to_delete->right );
 free( to_delete );
}


decompiled_code:
void __fastcall print_tree(const char ***a1, int a2)
{
int i; // ebx
int v3; // eax

if ( a1 )
{
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
fputc(32, stderr);
}
v3 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 1 )
{
fprintf(stderr, "[OP:%c|gcl=%d]", *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 4));
}
else if ( v3 == 2 )
{
fprintf(stderr, "[ID:%s=%d|gcl=%d]", *a1[1], *((unsigned int *)a1[1] + 2), *((unsigned int *)a1 + 4));
}
else if ( v3 )
{
fwrite("[UNKNOWN]", 1uLL, 9uLL, stderr);
}
else
{
fprintf(stderr, "[NUM:%d|gcl=%d]", *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 4));
}
fputc(10, stderr);
print_tree(a1[3], (unsigned int)(a2 + 4));
print_tree(a1[4], (unsigned int)(a2 + 4));
}
}

canonical_decompiled_code:
void __fastcall func1(const char ***a1, int a2)
{
int i; // ebx
int v3; // eax

if ( a1 )
{
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
fputc(32, stderr);
}
v3 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 1 )
{
fprintf(stderr, "[OP:%c|gcl=%d]", *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 4));
}
else if ( v3 == 2 )
{
fprintf(stderr, "[ID:%s=%d|gcl=%d]", *a1[1], *((unsigned int *)a1[1] + 2), *((unsigned int *)a1 + 4));
}
else if ( v3 )
{
fwrite("[UNKNOWN]", 1uLL, 9uLL, stderr);
}
else
{
fprintf(stderr, "[NUM:%d|gcl=%d]", *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 4));
}
fputc(10, stderr);
func1(a1[3], (unsigned int)(a2 + 4));
func1(a1[4], (unsigned int)(a2 + 4));
}
}

original_code:
void print_tree( t_node *root, int spaces ) {
 int i;
 if( root == ((void *)0) ) return;
 for( i = 0; i < spaces; i++ )
  fprintf( stderr, " " );
 switch( root->type ) {
 case op:
  fprintf( stderr, "[OP:%c|gcl=%d]", root->attr.oval, root->gcl );
  break;
 case num:
  fprintf( stderr, "[NUM:%d|gcl=%d]", root->attr.ival, root->gcl );
  break;
 case id:
  fprintf( stderr, "[ID:%s=%d|gcl=%d]", root->attr.id->name, root->attr.id->value, root->gcl );
  break;
 default:
  fprintf( stderr, "[UNKNOWN]" );
  break;
 }
 fprintf( stderr, "\n" );
 print_tree( root->left, spaces + 4 );
 print_tree( root->right, spaces + 4 );
}

canonical_original_code:
void print_tree(struct t_node *root, int spaces) {
 int i;
 if( root == ((void *)0) ) return;
 for( i = 0; i < spaces; i++ )
  fprintf( stderr, " " );
 switch( root->type ) {
 case op:
  fprintf( stderr, "[OP:%c|gcl=%d]", root->attr.oval, root->gcl );
  break;
 case num:
  fprintf( stderr, "[NUM:%d|gcl=%d]", root->attr.ival, root->gcl );
  break;
 case id:
  fprintf( stderr, "[ID:%s=%d|gcl=%d]", root->attr.id->name, root->attr.id->value, root->gcl );
  break;
 default:
  fprintf( stderr, "[UNKNOWN]" );
  break;
 }
 fprintf( stderr, "\n" );
 print_tree( root->left, spaces + 4 );
 print_tree( root->right, spaces + 4 );
}

variable_types: spaces int


DieWithError

repo: andrewkiluk/Basic-Web-Server

decompiled_code:
void __fastcall __noreturn DieWithError(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
static void DieWithError(const char *message)
{
    perror(message);
    exit(1);
}

canonical_original_code:
static void DieWithError(char *message)
{
    perror(message);
    exit(1);
}

variable_types:
message
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bx
const char *v4; // r12
int v5; // ebp
char *v6; // rax
int v7; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v10; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-28h] BYREF

if ( signal(13, (__sighandler_t)1) == (__sighandler_t)-1LL )
DieWithError("signal() failed");
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <server-port> <web_root>\n", *argv);
exit(1);
}
v3 = atoi(argv[1]);
v4 = argv[2];
v5 = socket(2, 1, 6);
if ( v5 < 0 )
DieWithError("socket() failed");
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(v3);
if ( bind(v5, &addr, 0x10u) < 0 )
DieWithError("bind() failed");
if ( listen(v5, 5) < 0 )
DieWithError("listen() failed");
while ( 1 )
{
addr_len = 16;
v7 = accept(v5, &v10, &addr_len);
if ( v7 < 0 )
break;
v6 = inet_ntoa(*(struct in_addr *)&v10.sa_data[2]);
printf("%s ", v6);
HandleHTTPClient(v4, (unsigned int)v7);
}
fwrite("failed accept() attempt\n", 1uLL, 0x18uLL, stderr);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bx
const char *v4; // r12
int v5; // ebp
char *v6; // rax
int v7; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v10; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-28h] BYREF

if ( signal(13, (__sighandler_t)1) == (__sighandler_t)-1LL )
func9("signal() failed");
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <server-port> <web_root>\n", *argv);
exit(1);
}
v3 = atoi(argv[1]);
v4 = argv[2];
v5 = socket(2, 1, 6);
if ( v5 < 0 )
func9("socket() failed");
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func7(0);
*(_WORD *)addr.sa_data = func8(v3);
if ( bind(v5, &addr, 0x10u) < 0 )
func9("bind() failed");
if ( listen(v5, 5) < 0 )
func9("listen() failed");
while ( 1 )
{
addr_len = 16;
v7 = accept(v5, &v10, &addr_len);
if ( v7 < 0 )
break;
v6 = inet_ntoa(*(struct in_addr *)&v10.sa_data[2]);
printf("%s ", v6);
func11(v4, (unsigned int)v7);
}
fwrite("failed accept() attempt\n", 1uLL, 0x18uLL, stderr);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    int servSock;
    int clntSock;
    struct sockaddr_in ServAddr;
    struct sockaddr_in ClntAddr;
    unsigned short ServPort;
    unsigned int clntLen;
    if (signal(13, ((__sighandler_t) 1)) == ((__sighandler_t) -1))
      DieWithError("signal() failed");
    if (argc != 3)
    {
        fprintf(stderr, "Usage:  %s <server-port> <web_root>\n", argv[0]);
        exit(1);
    }
    ServPort = atoi(argv[1]);
    char *webRoot = argv[2];
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");
    memset(&ServAddr, 0, sizeof(ServAddr));
    ServAddr.sin_family = 2;
    ServAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    ServAddr.sin_port = htons(ServPort);
    if (bind(servSock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
        DieWithError("bind() failed");
    if (listen(servSock, 5) < 0)
        DieWithError("listen() failed");
    for (;;)
    {
      clntLen = sizeof(ClntAddr);
      if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr,
                             &clntLen)) < 0){
        fprintf(stderr,"failed accept() attempt\n");
        break;
      }
      printf("%s ", inet_ntoa(ClntAddr.sin_addr));
      HandleHTTPClient(webRoot, clntSock);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int servSock;
    int clntSock;
    struct sockaddr_in ServAddr;
    struct sockaddr_in ClntAddr;
    unsigned short ServPort;
    unsigned int clntLen;
    if (signal(13, ((void (*)(int)) 1)) == ((__sighandler_t) -1))
      DieWithError("signal() failed");
    if (argc != 3)
    {
        fprintf(stderr, "Usage:  %s <server-port> <web_root>\n", argv[0]);
        exit(1);
    }
    ServPort = atoi(argv[1]);
    char *webRoot = argv[2];
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");
    memset(&ServAddr, 0, sizeof(ServAddr));
    ServAddr.sin_family = 2;
    ServAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    ServAddr.sin_port = htons(ServPort);
    if (bind(servSock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
        DieWithError("bind() failed");
    if (listen(servSock, 5) < 0)
        DieWithError("listen() failed");
    for (;;)
    {
      clntLen = sizeof(ClntAddr);
      if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr,
                             &clntLen)) < 0){
        fprintf(stderr,"failed accept() attempt\n");
        break;
      }
      printf("%s ", inet_ntoa(ClntAddr.sin_addr));
      HandleHTTPClient(webRoot, clntSock);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: servSock int

variable_types: clntSock 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:
ServPort
unsigned short

variable_types:
clntLen
unsigned int

variable_types:
webRoot
char *

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


decompiled_code:
__int64 __fastcall rl_max_size(__int64 a1)
{
return *(int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(int *)(a1 + 12);
}

original_code:
size_t rl_max_size(RL* rl)
{
    return rl->bufsize;
}

canonical_original_code:
unsigned long rl_max_size(struct RL *rl)
{
    return rl->bufsize;
}

variable_types: struct RL { unsigned char *buf; int fildes; int bufsize; struct BR { int fildes; int bufsize; unsigned char *buf; int bufpos; int read_len; } *reader; int skipping; } *

return_type: unsigned long

user_defined_types: struct RL { unsigned char *buf; int fildes; int bufsize; struct BR *reader; int skipping; }


decompiled_code:
ssize_t __fastcall read_char(__int64 a1)
{
__int64 v1; // rdx
int v2; // eax
ssize_t result; // rax

if ( *(_DWORD *)(a1 + 16) != *(_DWORD *)(a1 + 20) )
goto LABEL_2;
*(_DWORD *)(a1 + 16) = 0;
result = read(*(_DWORD *)a1, *(void **)(a1 + 8), *(int *)(a1 + 4));
*(_DWORD *)(a1 + 20) = result;
if ( !(_DWORD)result )
return 4294967294LL;
if ( (_DWORD)result != -1 )
{
LABEL_2:
v1 = *(_QWORD *)(a1 + 8);
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = v2 + 1;
result = *(unsigned __int8 *)(v1 + v2);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1)
{
__int64 v1; // rdx
int v2; // eax
ssize_t result; // rax

if ( *(_DWORD *)(a1 + 16) != *(_DWORD *)(a1 + 20) )
goto LABEL_2;
*(_DWORD *)(a1 + 16) = 0;
result = read(*(_DWORD *)a1, *(void **)(a1 + 8), *(int *)(a1 + 4));
*(_DWORD *)(a1 + 20) = result;
if ( !(_DWORD)result )
return 4294967294LL;
if ( (_DWORD)result != -1 )
{
LABEL_2:
v1 = *(_QWORD *)(a1 + 8);
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = v2 + 1;
result = *(unsigned __int8 *)(v1 + v2);
}
return result;
}

original_code:
int read_char(BR* br)
{
    if (br->bufpos == br->read_len)
    {
        br->bufpos = 0;
        br->read_len = read(br->fildes, br->buf, br->bufsize);
        if (br->read_len == 0)
            return -2;
        if (br->read_len == -1)
            return -1;
    }
    return br->buf[br->bufpos++];
}

canonical_original_code:
int read_char(struct BR *br)
{
    if (br->bufpos == br->read_len)
    {
        br->bufpos = 0;
        br->read_len = read(br->fildes, br->buf, br->bufsize);
        if (br->read_len == 0)
            return -2;
        if (br->read_len == -1)
            return -1;
    }
    return br->buf[br->bufpos++];
}

variable_types: struct BR { int fildes; int bufsize; unsigned char *buf; int bufpos; int read_len; } *


decompiled_code:
__int64 __fastcall br_close(__int64 a1)
{
free(*(void **)(a1 + 8));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
free(*(void **)(a1 + 8));
return 0LL;
}

original_code:
int br_close(BR* br)
{
    free(br->buf);
    return 0;
}

canonical_original_code:
int br_close(struct BR *br)
{
    free(br->buf);
    return 0;
}

user_defined_types: struct BR { int fildes; int bufsize; unsigned char *buf; int bufpos; int read_len; }


decompiled_code:
__int64 __fastcall rl_close(void *ptr)
{
br_close(*((_QWORD *)ptr + 2));
free(*(void **)ptr);
close(*((_DWORD *)ptr + 2));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(void *ptr)
{
func8(*((_QWORD *)ptr + 2));
free(*(void **)ptr);
close(*((_DWORD *)ptr + 2));
free(ptr);
return 0LL;
}

original_code:
int rl_close(RL* rl)
{
    br_close(rl->reader);
    free(rl->buf);
    close(rl->fildes);
    free(rl);
    return 0;
}

canonical_original_code:
int rl_close(struct RL *rl)
{
    br_close(rl->reader);
    free(rl->buf);
    close(rl->fildes);
    free(rl);
    return 0;
}


decompiled_code:
__int64 __fastcall rl_readline(__int64 a1, void *a2)
{
unsigned __int64 v2; // rbx
int v3; // er12
__int64 result; // rax

v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( rl_max_size(a1) <= v2 )
break;
result = read_char(*(_QWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
return result;
if ( (_DWORD)result == -2 )
return 0LL;
*(_BYTE *)(*(_QWORD *)a1 + v2++) = result;
if ( (_DWORD)result == 10 )
{
++v3;
break;
}
}
if ( v3 != rl_max_size(a1) || *(_BYTE *)(*(_QWORD *)a1 + v3 - 1LL) == 10 )
{
if ( *(_DWORD *)(a1 + 24) )
{
*(_DWORD *)(a1 + 24) = 0;
result = 4294967293LL;
}
else
{
memcpy(a2, *(const void **)a1, v3);
*((_BYTE *)a2 + v3) = 0;
result = (unsigned int)(v3 + 1);
}
}
else
{
*(_DWORD *)(a1 + 24) = 1;
result = 4294967293LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, void *a2)
{
unsigned __int64 v2; // rbx
int v3; // er12
__int64 result; // rax

v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( func6(a1) <= v2 )
break;
result = func7(*(_QWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
return result;
if ( (_DWORD)result == -2 )
return 0LL;
*(_BYTE *)(*(_QWORD *)a1 + v2++) = result;
if ( (_DWORD)result == 10 )
{
++v3;
break;
}
}
if ( v3 != func6(a1) || *(_BYTE *)(*(_QWORD *)a1 + v3 - 1LL) == 10 )
{
if ( *(_DWORD *)(a1 + 24) )
{
*(_DWORD *)(a1 + 24) = 0;
result = 4294967293LL;
}
else
{
memcpy(a2, *(const void **)a1, v3);
*((_BYTE *)a2 + v3) = 0;
result = (unsigned int)(v3 + 1);
}
}
else
{
*(_DWORD *)(a1 + 24) = 1;
result = 4294967293LL;
}
return result;
}

original_code:
int rl_readline(RL *rl, char* buf, size_t buf_size)
{
    int len;
    for (len = 0; len < rl_max_size(rl); len++)
    {
        int next_char = read_char(rl->reader);
        if (next_char == -1)
            return -1;
        if (next_char == -2)
            return 0;
        rl->buf[len] = next_char;
        if (next_char == '\n')
        {
            len++;
            break;
        }
    }
    if (len == rl_max_size(rl) && rl->buf[len - 1] != '\n')
    {
        rl->skipping = 1;
        return -3;
    }
    else
    {
        if (rl->skipping)
        {
            rl->skipping = 0;
            return -3;
        }
        else
        {
            memcpy(buf, rl->buf, len);
            buf[len] = 0;
            return len + 1;
        }
    }
}

canonical_original_code:
int rl_readline(struct RL *rl, char *buf, unsigned long buf_size)
{
    int len;
    for (len = 0; len < rl_max_size(rl); len++)
    {
        int next_char = read_char(rl->reader);
        if (next_char == -1)
            return -1;
        if (next_char == -2)
            return 0;
        rl->buf[len] = next_char;
        if (next_char == '\n')
        {
            len++;
            break;
        }
    }
    if (len == rl_max_size(rl) && rl->buf[len - 1] != '\n')
    {
        rl->skipping = 1;
        return -3;
    }
    else
    {
        if (rl->skipping)
        {
            rl->skipping = 0;
            return -3;
        }
        else
        {
            memcpy(buf, rl->buf, len);
            buf[len] = 0;
            return len + 1;
        }
    }
}

variable_types:
buf
char *

variable_types: len int


runLoopGame

repo: andrsoze/gPng

decompiled_code:
__int64 runLoopGame()
{
if ( (unsigned int)updatePongGame(&gPngGame) > 1 )
goto LABEL_2;
flashScreen();
currentGPngState = gameChangeState(&gPngGame);
if ( currentGPngState )
{
resetPongGame(&gPngGame);
printScores(&gPngGame);
LABEL_2:
handlePlayerInput(&gPngGame, 1LL);
handlePlayerInput(&gPngGame, 0LL);
return gameRenderGame(&gPngGame);
}
setIntroNeedsRedraw(1LL);
return introDisplayInit();
}

canonical_decompiled_code:
__int64 func0()
{
if ( (unsigned int)updatePongGame(&gPngGame) > 1 )
goto LABEL_2;
flashScreen();
currentGPngState = gameChangeState(&gPngGame);
if ( currentGPngState )
{
resetPongGame(&gPngGame);
printScores(&gPngGame);
LABEL_2:
handlePlayerInput(&gPngGame, 1LL);
handlePlayerInput(&gPngGame, 0LL);
return gameRenderGame(&gPngGame);
}
setIntroNeedsRedraw(1LL);
return introDisplayInit();
}

original_code:
void runLoopGame(void) {
    GravPngScoreChange scoreChange;
    scoreChange = updatePongGame(&gPngGame);
    if(scoreChange == PLAYER_ONE_GOAL || scoreChange == PLAYER_TWO_GOAL) {
 flashScreen();
 currentGPngState = gameChangeState(&gPngGame);
 if(currentGPngState == MENU_STATE) {
     setIntroNeedsRedraw(1);
     introDisplayInit();
     return;
 } else {
     resetPongGame(&gPngGame);
     printScores(&gPngGame);
 }
    }
    handlePlayerInput(&gPngGame, 1);
    handlePlayerInput(&gPngGame, 0);
    gameRenderGame(&gPngGame);
}

canonical_original_code:
void runLoopGame() {
    enum GRAVPNG_GAME_SCORE_CHANGE scoreChange;
    scoreChange = updatePongGame(&gPngGame);
    if(scoreChange == PLAYER_ONE_GOAL || scoreChange == PLAYER_TWO_GOAL) {
 flashScreen();
 currentGPngState = gameChangeState(&gPngGame);
 if(currentGPngState == MENU_STATE) {
     setIntroNeedsRedraw(1);
     introDisplayInit();
     return;
 } else {
     resetPongGame(&gPngGame);
     printScores(&gPngGame);
 }
    }
    handlePlayerInput(&gPngGame, 1);
    handlePlayerInput(&gPngGame, 0);
    gameRenderGame(&gPngGame);
}

variable_types: enum GRAVPNG_GAME_SCORE_CHANGE { PLAYER_ONE_GOAL=0, PLAYER_TWO_GOAL=1, NO_GOAL=2 }


setupNewGame

decompiled_code:
__int64 __fastcall setupNewGame(unsigned int a1)
{
if ( a1 > 2 )
_assert_fail("numHumanPlayers >= 0 && numHumanPlayers < 3", "gPng.c", 0x89u, "setupNewGame");
gameDisplayInit();
resetPongGame(&gPngGame);
*((_DWORD *)&gPngGame + 11) = 0;
*((_DWORD *)&gPngGame + 12) = 0;
*((_DWORD *)&gPngGame + 13) = 1;
*((_DWORD *)&gPngGame + 14) = a1 == 2;
return printScores(&gPngGame);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
if ( a1 > 2 )
_assert_fail("numHumanPlayers >= 0 && numHumanPlayers < 3", "gPng.c", 0x89u, "setupNewGame");
gameDisplayInit();
resetPongGame(&gPngGame);
*((_DWORD *)&gPngGame + 11) = 0;
*((_DWORD *)&gPngGame + 12) = 0;
*((_DWORD *)&gPngGame + 13) = 1;
*((_DWORD *)&gPngGame + 14) = a1 == 2;
return printScores(&gPngGame);
}

original_code:
void setupNewGame(int numHumanPlayers) {
    ((void) sizeof ((numHumanPlayers >= 0 && numHumanPlayers < 3) ? 1 : 0), __extension__ ({ if (numHumanPlayers >= 0 && numHumanPlayers < 3) ; else __assert_fail ("numHumanPlayers >= 0 && numHumanPlayers < 3", "gPng.c", 137, __extension__ __PRETTY_FUNCTION__); }));
    gameDisplayInit();
    resetPongGame(&gPngGame);
    gPngGame.pointsPlayerOne = 0;
    gPngGame.pointsPlayerTwo = 0;
    gPngGame.isPlayerOneHuman = 1;
    gPngGame.isPlayerTwoHuman = (numHumanPlayers == 2)?1:0;
    printScores(&gPngGame);
}

variable_types: numHumanPlayers int


changeState

decompiled_code:
__int64 __fastcall changeState(unsigned int a1)
{
__int64 result; // rax
_QWORD *v2; // rax
_QWORD *v3; // rax

currentGPngState = a1;
if ( a1 == 3 )
return setupNewGame(2u);
if ( a1 > 3 )
{
if ( a1 != 5 )
goto LABEL_10;
v2 = (_QWORD *)globalGameState;
*(_QWORD *)(globalGameState + 8) = &howDisplayInit;
v2[2] = &howHandleEvent;
v2[3] = &howUpdate;
v2[4] = &howRender;
v2[5] = &howClean;
v2[6] = &howChangeState;
result = howDisplayInit();
}
else
{
if ( a1 )
{
if ( a1 == 2 )
return setupNewGame(1u);
LABEL_10:
_assert_fail("0", "gPng.c", 0x84u, "changeState");
}
v3 = (_QWORD *)globalGameState;
*(_QWORD *)(globalGameState + 8) = &introDisplayInit;
v3[2] = &introHandleEvent;
v3[3] = &introUpdate;
v3[4] = &introRender;
v3[5] = &introClean;
v3[6] = &introChangeState;
result = introDisplayInit();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 result; // rax
_QWORD *v2; // rax
_QWORD *v3; // rax

currentGPngState = a1;
if ( a1 == 3 )
return func1(2u);
if ( a1 > 3 )
{
if ( a1 != 5 )
goto LABEL_10;
v2 = (_QWORD *)globalGameState;
*(_QWORD *)(globalGameState + 8) = &howDisplayInit;
v2[2] = &howHandleEvent;
v2[3] = &howUpdate;
v2[4] = &howRender;
v2[5] = &howClean;
v2[6] = &howChangeState;
result = howDisplayInit();
}
else
{
if ( a1 )
{
if ( a1 == 2 )
return func1(1u);
LABEL_10:
_assert_fail("0", "gPng.c", 0x84u, "changeState");
}
v3 = (_QWORD *)globalGameState;
*(_QWORD *)(globalGameState + 8) = &introDisplayInit;
v3[2] = &introHandleEvent;
v3[3] = &introUpdate;
v3[4] = &introRender;
v3[5] = &introClean;
v3[6] = &introChangeState;
result = introDisplayInit();
}
return result;
}

original_code:
void changeState(GPngState newState) {
    currentGPngState = newState;
    switch (newState) {
    case GAME_STATE_SINGLE:
 setupNewGame(1);
 break;
    case GAME_STATE_DOUBLE:
 setupNewGame(2);
 break;
    case HOW_TO_PLAY_STATE:
 globalGameState->initFPtr = howDisplayInit;
 globalGameState->handleEventFPtr = howHandleEvent;
 globalGameState->updateFPtr = howUpdate;
 globalGameState->renderFPtr = howRender;
 globalGameState->clean = howClean;
 globalGameState->changeState = howChangeState;
 globalGameState->initFPtr();
 break;
    case MENU_STATE:
 globalGameState->initFPtr = introDisplayInit;
 globalGameState->handleEventFPtr = introHandleEvent;
 globalGameState->updateFPtr = introUpdate;
 globalGameState->renderFPtr = introRender;
 globalGameState->clean = introClean;
 globalGameState->changeState = introChangeState;
 globalGameState->initFPtr();
 break;
    default:
 ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "gPng.c", 132, __extension__ __PRETTY_FUNCTION__); }));
    }
}

canonical_original_code:
void changeState(enum PNG_STATE newState) {
    currentGPngState = newState;
    switch (newState) {
    case GAME_STATE_SINGLE:
 setupNewGame(1);
 break;
    case GAME_STATE_DOUBLE:
 setupNewGame(2);
 break;
    case HOW_TO_PLAY_STATE:
 globalGameState->initFPtr = howDisplayInit;
 globalGameState->handleEventFPtr = howHandleEvent;
 globalGameState->updateFPtr = howUpdate;
 globalGameState->renderFPtr = howRender;
 globalGameState->clean = howClean;
 globalGameState->changeState = howChangeState;
 globalGameState->initFPtr();
 break;
    case MENU_STATE:
 globalGameState->initFPtr = introDisplayInit;
 globalGameState->handleEventFPtr = introHandleEvent;
 globalGameState->updateFPtr = introUpdate;
 globalGameState->renderFPtr = introRender;
 globalGameState->clean = introClean;
 globalGameState->changeState = introChangeState;
 globalGameState->initFPtr();
 break;
    default:
 ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "gPng.c", 132, __extension__ __PRETTY_FUNCTION__); }));
    }
}

variable_types: enum PNG_STATE { MENU_STATE=0, HIGHSCORE_STATE=1, GAME_STATE_SINGLE=2, GAME_STATE_DOUBLE=3, TITLE_STATE=4, HOW_TO_PLAY_STATE=5 }


runLoop

decompiled_code:
void __noreturn runLoop()
{
unsigned int v0; // eax

while ( 1 )
{
if ( (unsigned int)(currentGPngState - 2) <= 1 )
{
runLoopGame();
}
else
{
(*(void (**)(void))(globalGameState + 16))();
(*(void (**)(void))(globalGameState + 24))();
(*(void (**)(void))(globalGameState + 32))();
v0 = (*(__int64 (__fastcall **)(__int64 *))(globalGameState + 48))(&gPngGame);
if ( currentGPngState != v0 )
changeState(v0);
}
}
}

canonical_decompiled_code:
void __noreturn func3()
{
unsigned int v0; // eax

while ( 1 )
{
if ( (unsigned int)(currentGPngState - 2) <= 1 )
{
func0();
}
else
{
(*(void (**)(void))(globalGameState + 16))();
(*(void (**)(void))(globalGameState + 24))();
(*(void (**)(void))(globalGameState + 32))();
v0 = (*(__int64 (__fastcall **)(__int64 *))(globalGameState + 48))(&gPngGame);
if ( currentGPngState != v0 )
func2(v0);
}
}
}

original_code:
void runLoop(void)
{
    GPngState newState;
    while(1) {
 if(currentGPngState == GAME_STATE_SINGLE ||
    currentGPngState == GAME_STATE_DOUBLE) {
     runLoopGame();
 } else {
     globalGameState->handleEventFPtr();
     globalGameState->updateFPtr();
     globalGameState->renderFPtr();
     newState = globalGameState->changeState(&gPngGame);
     if(currentGPngState != newState) {
  changeState(newState);
     }
 }
    }
}

canonical_original_code:
void runLoop()
{
    enum PNG_STATE newState;
    while(1) {
 if(currentGPngState == GAME_STATE_SINGLE ||
    currentGPngState == GAME_STATE_DOUBLE) {
     runLoopGame();
 } else {
     globalGameState->handleEventFPtr();
     globalGameState->updateFPtr();
     globalGameState->renderFPtr();
     newState = globalGameState->changeState(&gPngGame);
     if(currentGPngState != newState) {
  changeState(newState);
     }
 }
    }
}

user_defined_types: enum PNG_STATE { MENU_STATE=0, HIGHSCORE_STATE=1, GAME_STATE_SINGLE=2, GAME_STATE_DOUBLE=3, TITLE_STATE=4, HOW_TO_PLAY_STATE=5 }


assembleDefaultPongGame

decompiled_code:
void assembleDefaultPongGame()
{
*(&gPngGame + 2) = (__int64)&ball;
gPngGame = (__int64)&paddleOne;
*(&gPngGame + 1) = (__int64)&paddleTwo;
*(&gPngGame + 3) = (__int64)&court;
*((_BYTE *)&gPngGame + 40) = 1;
*((_DWORD *)&gPngGame + 9) = 1;
*((_DWORD *)&gPngGame + 8) = 131080;
*((_DWORD *)&gPngGame + 11) = 0;
*((_DWORD *)&gPngGame + 12) = 0;
}

canonical_decompiled_code:
void func4()
{
*(&gPngGame + 2) = (__int64)&ball;
gPngGame = (__int64)&paddleOne;
*(&gPngGame + 1) = (__int64)&paddleTwo;
*(&gPngGame + 3) = (__int64)&court;
*((_BYTE *)&gPngGame + 40) = 1;
*((_DWORD *)&gPngGame + 9) = 1;
*((_DWORD *)&gPngGame + 8) = 131080;
*((_DWORD *)&gPngGame + 11) = 0;
*((_DWORD *)&gPngGame + 12) = 0;
}

original_code:
void assembleDefaultPongGame(void) {
    gPngGame.ball = &ball;
    gPngGame.paddleOne = &paddleOne;
    gPngGame.paddleTwo = &paddleTwo;
    gPngGame.court = &court;
    gPngGame.useGravity = 1;
    gPngGame.gravity = 1;
    gPngGame.epsilon = ((2<<(16))|8);
    gPngGame.pointsPlayerOne = 0;
    gPngGame.pointsPlayerTwo = 0;
}

canonical_original_code:
void assembleDefaultPongGame() {
    gPngGame.ball = &ball;
    gPngGame.paddleOne = &paddleOne;
    gPngGame.paddleTwo = &paddleTwo;
    gPngGame.court = &court;
    gPngGame.useGravity = 1;
    gPngGame.gravity = 1;
    gPngGame.epsilon = ((2<<(16))|8);
    gPngGame.pointsPlayerOne = 0;
    gPngGame.pointsPlayerTwo = 0;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rbx

v3 = malloc(0x38uLL);
globalGameState = (__int64)v3;
v3[1] = &titleDisplayInit;
v3[2] = &titleHandleEvent;
v3[3] = &titleUpdate;
v3[4] = &titleRender;
v3[5] = &titleClean;
v3[6] = &titleChangeState;
assembleDefaultPongGame();
((void (__fastcall *)(__int64, const char **))v3[1])(56LL, argv);
runLoop();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rbx

v3 = malloc(0x38uLL);
globalGameState = (__int64)v3;
v3[1] = &titleDisplayInit;
v3[2] = &titleHandleEvent;
v3[3] = &titleUpdate;
v3[4] = &titleRender;
v3[5] = &titleClean;
v3[6] = &titleChangeState;
func4();
((void (__fastcall *)(__int64, const char **))v3[1])(56LL, argv);
func3();
}

original_code:
int main()
{
    globalGameState = malloc(sizeof(GameState));
    globalGameState->initFPtr = titleDisplayInit;
    globalGameState->handleEventFPtr = titleHandleEvent;
    globalGameState->updateFPtr = titleUpdate;
    globalGameState->renderFPtr = titleRender;
    globalGameState->clean = titleClean;
    globalGameState->changeState = titleChangeState;
    assembleDefaultPongGame();
    globalGameState->initFPtr();
    runLoop();
    while(1){
    }
    free(globalGameState);
    return 1;
}


moveMenuUp

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

result = (unsigned int)selectedAlternative;
if ( selectedAlternative == 2 )
{
selectedAlternative = 1;
}
else if ( selectedAlternative == 3 )
{
selectedAlternative = 2;
}
return result;
}

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

result = (unsigned int)selectedAlternative;
if ( selectedAlternative == 2 )
{
selectedAlternative = 1;
}
else if ( selectedAlternative == 3 )
{
selectedAlternative = 2;
}
return result;
}

original_code:
void moveMenuUp(void) {
    if (selectedAlternative == TwoPlayerGame) {
 selectedAlternative = OnePlayerGame;
    } else if (selectedAlternative == HowToPlay) {
 selectedAlternative = TwoPlayerGame;
    }
}

canonical_original_code:
void moveMenuUp() {
    if (selectedAlternative == TwoPlayerGame) {
 selectedAlternative = OnePlayerGame;
    } else if (selectedAlternative == HowToPlay) {
 selectedAlternative = TwoPlayerGame;
    }
}


moveMenuDown

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

result = (unsigned int)selectedAlternative;
if ( selectedAlternative == 1 )
{
selectedAlternative = 2;
}
else if ( selectedAlternative == 2 )
{
selectedAlternative = 3;
}
return result;
}

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

result = (unsigned int)selectedAlternative;
if ( selectedAlternative == 1 )
{
selectedAlternative = 2;
}
else if ( selectedAlternative == 2 )
{
selectedAlternative = 3;
}
return result;
}

original_code:
void moveMenuDown(void) {
    if (selectedAlternative == OnePlayerGame) {
 selectedAlternative = TwoPlayerGame;
    } else if (selectedAlternative == TwoPlayerGame) {
 selectedAlternative = HowToPlay;
    }
}

canonical_original_code:
void moveMenuDown() {
    if (selectedAlternative == OnePlayerGame) {
 selectedAlternative = TwoPlayerGame;
    } else if (selectedAlternative == TwoPlayerGame) {
 selectedAlternative = HowToPlay;
    }
}


introHandleEvent

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

result = (unsigned int)(unsigned __int8)count + 1;
if ( (char)(count + 1) <= 55 )
{
++count;
}
else
{
count = 0;
if ( (MEMORY[0xDC00] & 1) != 0 )
{
if ( (MEMORY[0xDC00] & 2) != 0 )
{
if ( (MEMORY[0xDC00] & 0x10) == 0 )
buttonPressed = 1;
}
else
{
result = moveMenuDown();
}
}
else
{
result = moveMenuUp();
}
}
return result;
}

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

result = (unsigned int)(unsigned __int8)count + 1;
if ( (char)(count + 1) <= 55 )
{
++count;
}
else
{
count = 0;
if ( (MEMORY[0xDC00] & 1) != 0 )
{
if ( (MEMORY[0xDC00] & 2) != 0 )
{
if ( (MEMORY[0xDC00] & 0x10) == 0 )
buttonPressed = 1;
}
else
{
result = func1();
}
}
else
{
result = func0();
}
}
return result;
}

original_code:
void introHandleEvent(void) {
    char joystickEvent;
    volatile char *dataPortA = (char*) 0xdc00;
    joystickEvent = *(dataPortA);
    if (++count < 56) {
 return;
    }
    count = 0;
    if (!(joystickEvent & 1)) {
 moveMenuUp();
    } else if (!(joystickEvent & 2)) {
 moveMenuDown();
    } else if (!(joystickEvent & 16)) {
 buttonPressed = 1;
    }
}

canonical_original_code:
void introHandleEvent() {
    char joystickEvent;
    char *dataPortA = (char*) 0xdc00;
    joystickEvent = *(dataPortA);
    if (++count < 56) {
 return;
    }
    count = 0;
    if (!(joystickEvent & 1)) {
 moveMenuUp();
    } else if (!(joystickEvent & 2)) {
 moveMenuDown();
    } else if (!(joystickEvent & 16)) {
 buttonPressed = 1;
    }
}

variable_types: joystickEvent char

variable_types:
dataPortA
char *


introUpdate

decompiled_code:
void introUpdate()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void introUpdate(void) {
}

canonical_original_code:
void introUpdate() {
}


setIntroNeedsRedraw

decompiled_code:
void __fastcall setIntroNeedsRedraw(char a1)
{
needsRedraw = a1;
}

canonical_decompiled_code:
void __fastcall func4(char a1)
{
needsRedraw = a1;
}

original_code:
void setIntroNeedsRedraw(unsigned char nextNeedsRedraw) {
    needsRedraw = nextNeedsRedraw;
}

variable_types:
nextNeedsRedraw
unsigned char


introClean

decompiled_code:
void introClean()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void introClean(void) {
}

canonical_original_code:
void introClean() {
}


setupIntroSpriteMemPtrs

decompiled_code:
void setupIntroSpriteMemPtrs()
{
MEMORY[0x7F8] = 13;
}

canonical_decompiled_code:
void func6()
{
MEMORY[0x7F8] = 13;
}

original_code:
void setupIntroSpriteMemPtrs(void) {
    char * pokePtr;
    pokePtr = (char *) 0x7F8;
    *(pokePtr) = 13;
}

canonical_original_code:
void setupIntroSpriteMemPtrs() {
    char *pokePtr;
    pokePtr = (char *) 0x7F8;
    *(pokePtr) = 13;
}

variable_types:
pokePtr
char *


fillIntroSpriteMem

decompiled_code:
_BYTE *fillIntroSpriteMem()
{
_BYTE *result; // rax

result = (_BYTE *)&unk_340;
do
*result++ = -1;
while ( result != (_BYTE *)&off_37C + 3 );
return result;
}

canonical_decompiled_code:
_BYTE *func7()
{
_BYTE *result; // rax

result = (_BYTE *)&unk_340;
do
*result++ = -1;
while ( result != (_BYTE *)&off_37C + 3 );
return result;
}

original_code:
void fillIntroSpriteMem(void) {
    char * pokePtr;
    char i;
    for (i = 0; i < 63; i++) {
 pokePtr = (char *) 0x340;
 *(pokePtr + i) = 255;
    }
}

canonical_original_code:
void fillIntroSpriteMem() {
    char *pokePtr;
    char i;
    for (i = 0; i < 63; i++) {
 pokePtr = (char *) 0x340;
 *(pokePtr + i) = 255;
    }
}

variable_types: i char


drawSprite

decompiled_code:
__int64 drawSprite()
{
char v0; // bl
__int64 result; // rax

v0 = -124;
if ( selectedAlternative != 2 )
{
v0 = 92;
if ( selectedAlternative == 3 )
v0 = -84;
}
MEMORY[0xD027] = 1;
MEMORY[0xD000] = 26;
result = setMsb(0LL, 0LL);
MEMORY[0xD001] = v0;
MEMORY[0xD015] = 1;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
char v0; // bl
__int64 result; // rax

v0 = -124;
if ( selectedAlternative != 2 )
{
v0 = 92;
if ( selectedAlternative == 3 )
v0 = -84;
}
MEMORY[0xD027] = 1;
MEMORY[0xD000] = 26;
result = setMsb(0LL, 0LL);
MEMORY[0xD001] = v0;
MEMORY[0xD015] = 1;
return result;
}

original_code:
void drawSprite(void) {
    unsigned int x, y;
    char *pokePtr;
    x = 26;
    y = ((6 + 1) * 8) + 36;
    if (selectedAlternative == TwoPlayerGame) {
 y += (5 * 8);
    } else if (selectedAlternative == HowToPlay) {
 y += ((5 * 2) * 8);
    }
    pokePtr = (char *)0xD027;
    (*pokePtr) = 1;
    pokePtr = (char *) 0xD000;
    *(pokePtr) = x;
    setMsb(0, 0);
    pokePtr = (char *) 0xD001;
    *(pokePtr) = y;
    pokePtr = (char *) 0xD015;
    *(pokePtr) = 1;
}

canonical_original_code:
void drawSprite() {
    unsigned int x; unsigned int y;
    char *pokePtr;
    x = 26;
    y = ((6 + 1) * 8) + 36;
    if (selectedAlternative == TwoPlayerGame) {
 y += (5 * 8);
    } else if (selectedAlternative == HowToPlay) {
 y += ((5 * 2) * 8);
    }
    pokePtr = (char *)0xD027;
    (*pokePtr) = 1;
    pokePtr = (char *) 0xD000;
    *(pokePtr) = x;
    setMsb(0, 0);
    pokePtr = (char *) 0xD001;
    *(pokePtr) = y;
    pokePtr = (char *) 0xD015;
    *(pokePtr) = 1;
}

variable_types:
x
unsigned int

variable_types:
y
unsigned int


introRender

decompiled_code:
__int64 introRender()
{
return drawSprite();
}

canonical_decompiled_code:
__int64 func9()
{
return func8();
}

original_code:
void introRender(void) {
    drawSprite();
}

canonical_original_code:
void introRender() {
    drawSprite();
}


introDisplayInit

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

if ( needsRedraw )
{
fillIntroSpriteMem();
setupIntroSpriteMemPtrs();
drawSprite();
clearScreen();
printLine(4LL, 6LL, menuAlternativeStrings, (unsigned int)menuAlternativeStringLengths[0]);
printLine(
4LL,
11LL,
(unsigned int)((_DWORD)menuAlternativeStrings + 20),
(unsigned int)menuAlternativeStringLengths[1]);
result = printLine(
4LL,
16LL,
(unsigned int)((_DWORD)menuAlternativeStrings + 40),
(unsigned int)menuAlternativeStringLengths[2]);
}
return result;
}

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

if ( needsRedraw )
{
func7();
func6();
func8();
clearScreen();
printLine(4LL, 6LL, menuAlternativeStrings, (unsigned int)menuAlternativeStringLengths[0]);
printLine(
4LL,
11LL,
(unsigned int)((_DWORD)menuAlternativeStrings + 20),
(unsigned int)menuAlternativeStringLengths[1]);
result = printLine(
4LL,
16LL,
(unsigned int)((_DWORD)menuAlternativeStrings + 40),
(unsigned int)menuAlternativeStringLengths[2]);
}
return result;
}

original_code:
void introDisplayInit(void) {
    if (needsRedraw) {
 int i;
 fillIntroSpriteMem();
 setupIntroSpriteMemPtrs();
 drawSprite();
 clearScreen();
 for (i = 0; i < 3; i++) {
     printLine(4, 6 + (i * 5),
        menuAlternativeStrings[i], menuAlternativeStringLengths[i]);
 }
    }
}

canonical_original_code:
void introDisplayInit() {
    if (needsRedraw) {
 int i;
 fillIntroSpriteMem();
 setupIntroSpriteMemPtrs();
 drawSprite();
 clearScreen();
 for (i = 0; i < 3; i++) {
     printLine(4, 6 + (i * 5),
        menuAlternativeStrings[i], menuAlternativeStringLengths[i]);
 }
    }
}

variable_types: i int


initGameObjects

decompiled_code:
_BOOL8 __fastcall initGameObjects(int a1, __int64 a2)
{
_BOOL8 result; // rax

initDefaultBall(*(_QWORD *)(a2 + 16));
initDefaultCourt(*(_QWORD *)(a2 + 24));
initDefaultPaddleOne(*(_QWORD *)a2);
initDefaultPaddleTwo(*(_QWORD *)(a2 + 8));
*(_DWORD *)(a2 + 52) = 1;
result = a1 == 0;
*(_DWORD *)(a2 + 56) = result;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(int a1, __int64 a2)
{
_BOOL8 result; // rax

initDefaultBall(*(_QWORD *)(a2 + 16));
initDefaultCourt(*(_QWORD *)(a2 + 24));
initDefaultPaddleOne(*(_QWORD *)a2);
initDefaultPaddleTwo(*(_QWORD *)(a2 + 8));
*(_DWORD *)(a2 + 52) = 1;
result = a1 == 0;
*(_DWORD *)(a2 + 56) = result;
return result;
}

original_code:
void initGameObjects(int isOnePlayer, GPngGame *gPngGame)
{
    initDefaultBall(gPngGame->ball);
    initDefaultCourt(gPngGame->court);
    initDefaultPaddleOne(gPngGame->paddleOne);
    initDefaultPaddleTwo(gPngGame->paddleTwo);
    gPngGame->isPlayerOneHuman = 1;
    if(isOnePlayer) {
 gPngGame->isPlayerTwoHuman = 0;
    } else {
 gPngGame->isPlayerTwoHuman = 1;
    }
}

canonical_original_code:
void initGameObjects(int isOnePlayer, struct PONGGAME *gPngGame)
{
    initDefaultBall(gPngGame->ball);
    initDefaultCourt(gPngGame->court);
    initDefaultPaddleOne(gPngGame->paddleOne);
    initDefaultPaddleTwo(gPngGame->paddleTwo);
    gPngGame->isPlayerOneHuman = 1;
    if(isOnePlayer) {
 gPngGame->isPlayerTwoHuman = 0;
    } else {
 gPngGame->isPlayerTwoHuman = 1;
    }
}

variable_types: isOnePlayer int

variable_types: struct PONGGAME { struct PADDLE { int x; int y; int xVel; int yVel; int height; int width; int disabledCounter; unsigned char weight; } *paddleOne; struct PADDLE *paddleTwo; struct BALL { int x; int y; int xVel; int yVel; int height; int width; int disabledCounter; unsigned char weight; } *ball; struct COURT { int width; int height; int x; int y; int leftAccAreaX; int leftAccAreaWidth; int rightAccAreaX; int rightAccAreaWidth; } *court; int epsilon; int gravity; char useGravity; int pointsPlayerOne; int pointsPlayerTwo; int isPlayerOneHuman; int isPlayerTwoHuman; unsigned int hitCounter; enum GRAVITY_DIRECTION { noGravity=8, northGravity=2, northEastGravity=1, eastGravity=0, southEastGravity=7, southGravity=6, southWestGravity=5, westGravity=4, northWestGravity=3 } gravityDirection; int xGravity; int yGravity; } *

user_defined_types: struct PONGGAME { struct PADDLE *paddleOne; struct PADDLE *paddleTwo; struct BALL *ball; struct COURT *court; int epsilon; int gravity; char useGravity; int pointsPlayerOne; int pointsPlayerTwo; int isPlayerOneHuman; int isPlayerTwoHuman; unsigned int hitCounter; enum GRAVITY_DIRECTION gravityDirection; int xGravity; int yGravity; }


introChangeState

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

result = 0LL;
if ( buttonPressed )
{
buttonPressed = 0;
if ( selectedAlternative == 1 )
{
initGameObjects(1, a1);
result = 2LL;
}
else if ( selectedAlternative == 2 )
{
initGameObjects(0, a1);
result = 3LL;
}
else
{
result = 5 * (unsigned int)(selectedAlternative == 3);
}
}
return result;
}

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

result = 0LL;
if ( buttonPressed )
{
buttonPressed = 0;
if ( selectedAlternative == 1 )
{
func11(1, a1);
result = 2LL;
}
else if ( selectedAlternative == 2 )
{
func11(0, a1);
result = 3LL;
}
else
{
result = 5 * (unsigned int)(selectedAlternative == 3);
}
}
return result;
}

original_code:
GPngState introChangeState(GPngGame *gPngGame) {
    if (buttonPressed) {
 buttonPressed = 0;
 if (selectedAlternative == OnePlayerGame) {
     initGameObjects(1, gPngGame);
     return GAME_STATE_SINGLE;
 } else if (selectedAlternative == TwoPlayerGame) {
     initGameObjects(0, gPngGame);
     return GAME_STATE_DOUBLE;
 } else if (selectedAlternative == HowToPlay) {
     return HOW_TO_PLAY_STATE;
 } else {
     return MENU_STATE;
 }
    } else {
 return MENU_STATE;
    }
}

canonical_original_code:
enum PNG_STATE introChangeState(struct PONGGAME *gPngGame) {
    if (buttonPressed) {
 buttonPressed = 0;
 if (selectedAlternative == OnePlayerGame) {
     initGameObjects(1, gPngGame);
     return GAME_STATE_SINGLE;
 } else if (selectedAlternative == TwoPlayerGame) {
     initGameObjects(0, gPngGame);
     return GAME_STATE_DOUBLE;
 } else if (selectedAlternative == HowToPlay) {
     return HOW_TO_PLAY_STATE;
 } else {
     return MENU_STATE;
 }
    } else {
 return MENU_STATE;
    }
}

return_type: enum PNG_STATE { MENU_STATE=0, HIGHSCORE_STATE=1, GAME_STATE_SINGLE=2, GAME_STATE_DOUBLE=3, TITLE_STATE=4, HOW_TO_PLAY_STATE=5 }

user_defined_types: enum GRAVITY_DIRECTION { noGravity=8, northGravity=2, northEastGravity=1, eastGravity=0, southEastGravity=7, southGravity=6, southWestGravity=5, westGravity=4, northWestGravity=3 }

user_defined_types: struct PADDLE { int x; int y; int xVel; int yVel; int height; int width; int disabledCounter; unsigned char weight; }

user_defined_types: struct BALL { int x; int y; int xVel; int yVel; int height; int width; int disabledCounter; unsigned char weight; }

user_defined_types: struct COURT { int width; int height; int x; int y; int leftAccAreaX; int leftAccAreaWidth; int rightAccAreaX; int rightAccAreaWidth; }


printLine

decompiled_code:
unsigned __int64 __fastcall printLine(unsigned __int8 a1, unsigned __int8 a2, __int64 a3, int a4)
{
char v6; // dl
unsigned __int64 result; // rax
__int64 v8; // rcx
__int64 v9; // rsi

v6 = a4;
result = a1 + 40 * (unsigned int)a2 + 1024;
if ( a1 + a4 > 40 )
v6 = 40 - a1;
if ( v6 )
{
v8 = (unsigned __int8)(v6 - 1) + (__int64)((int)result + 1);
result = (int)result;
v9 = a3 - (int)result;
do
{
*(_BYTE *)result = *(_BYTE *)(v9 + result);
++result;
}
while ( result != v8 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int8 a1, unsigned __int8 a2, __int64 a3, int a4)
{
char v6; // dl
unsigned __int64 result; // rax
__int64 v8; // rcx
__int64 v9; // rsi

v6 = a4;
result = a1 + 40 * (unsigned int)a2 + 1024;
if ( a1 + a4 > 40 )
v6 = 40 - a1;
if ( v6 )
{
v8 = (unsigned __int8)(v6 - 1) + (__int64)((int)result + 1);
result = (int)result;
v9 = a3 - (int)result;
do
{
*(_BYTE *)result = *(_BYTE *)(v9 + result);
++result;
}
while ( result != v8 );
}
return result;
}

original_code:
void printLine(unsigned char indent,
        unsigned char row,
        char* lineOfChars,
        int length)
{
    unsigned char i;
    unsigned char * vidMemDst = (unsigned char*) ((1024) + ((40)*row) + indent);
    int actualLength = (length + indent > (40))?(40)-indent:length;
    unsigned char uchar_actualLength = (unsigned char)actualLength;
    for(i = 0; i < uchar_actualLength; i++){
 *(vidMemDst+i)=lineOfChars[i];
    }
}

canonical_original_code:
void printLine(unsigned char indent, unsigned char row, char *lineOfChars, int length)
{
    unsigned char i;
    unsigned char *vidMemDst = (unsigned char*) ((1024) + ((40)*row) + indent);
    int actualLength = (length + indent > (40))?(40)-indent:length;
    unsigned char uchar_actualLength = (unsigned char)actualLength;
    for(i = 0; i < uchar_actualLength; i++){
 *(vidMemDst+i)=lineOfChars[i];
    }
}

variable_types:
indent
unsigned char

variable_types:
row
unsigned char

variable_types:
lineOfChars
char *

variable_types: length int

variable_types:
i
unsigned char

variable_types:
vidMemDst
unsigned char *

variable_types: actualLength int


clearScreen

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

for ( result = 1024LL; result != 2024; ++result )
*(_BYTE *)result = 32;
return result;
}

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

for ( result = 1024LL; result != 2024; ++result )
*(_BYTE *)result = 32;
return result;
}

original_code:
void clearScreen(void)
{
    unsigned char *vidMemPtr;
    unsigned char character = ' ';
    int i;
    for(i = 1024; i < 2024; i++) {
 vidMemPtr = (unsigned char *)i;
 (*vidMemPtr)=character;
    }
}

canonical_original_code:
void clearScreen()
{
    unsigned char *vidMemPtr;
    unsigned char character = ' ';
    int i;
    for(i = 1024; i < 2024; i++) {
 vidMemPtr = (unsigned char *)i;
 (*vidMemPtr)=character;
    }
}

variable_types:
vidMemPtr
unsigned char *

variable_types:
character
unsigned char


decompiled_code:
void __noreturn events_usage()
{
const char **v0; // rbx
const char *v1; // rsi
const char **v2; // rbx
const char *v3; // rsi
const char **v4; // rbx
const char *v5; // rsi

puts("Software events:");
v0 = (const char **)&sw_events;
v1 = "cpu_clock";
do
{
printf(" sw:%s\n", v1);
v0 += 2;
v1 = *v0;
}
while ( *v0 );
putchar(10);
puts("Hardware events:");
v2 = (const char **)&hw_events;
v3 = "cpu_cycles";
do
{
printf(" hw:%s\n", v3);
v2 += 2;
v3 = *v2;
}
while ( *v2 );
putchar(10);
puts("Hardware cache events:");
puts(" Specified on the form 'hwc:cache:operation:result'.");
puts(" Available cache levels:");
v4 = (const char **)&hwc_events;
v5 = "l1d";
do
{
printf(" %s\n", v5);
v4 += 2;
v5 = *v4;
}
while ( *v4 );
puts(" Available operations:");
puts(" r - Read");
puts(" w - Write");
puts(" p - Prefetch");
puts(" Available results:");
puts(" a - Access");
puts(" m - Miss");
putchar(10);
puts("Raw events:");
puts(" Raw events are specified on the form 'raw:NUM' where NUM is the event number.");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
const char **v0; // rbx
const char *v1; // rsi
const char **v2; // rbx
const char *v3; // rsi
const char **v4; // rbx
const char *v5; // rsi

puts("Software events:");
v0 = (const char **)&sw_events;
v1 = "cpu_clock";
do
{
printf(" sw:%s\n", v1);
v0 += 2;
v1 = *v0;
}
while ( *v0 );
putchar(10);
puts("Hardware events:");
v2 = (const char **)&hw_events;
v3 = "cpu_cycles";
do
{
printf(" hw:%s\n", v3);
v2 += 2;
v3 = *v2;
}
while ( *v2 );
putchar(10);
puts("Hardware cache events:");
puts(" Specified on the form 'hwc:cache:operation:result'.");
puts(" Available cache levels:");
v4 = (const char **)&hwc_events;
v5 = "l1d";
do
{
printf(" %s\n", v5);
v4 += 2;
v5 = *v4;
}
while ( *v4 );
puts(" Available operations:");
puts(" r - Read");
puts(" w - Write");
puts(" p - Prefetch");
puts(" Available results:");
puts(" a - Access");
puts(" m - Miss");
putchar(10);
puts("Raw events:");
puts(" Raw events are specified on the form 'raw:NUM' where NUM is the event number.");
exit(0);
}

original_code:
static void
events_usage()
{
    printf("Software events:\n");
    for (const event_name_id_t *cur = sw_events; cur->name; cur++)
        printf("  sw:%s\n", cur->name);
    printf("\n");
    printf("Hardware events:\n");
    for (const event_name_id_t *cur = hw_events; cur->name; cur++)
        printf("  hw:%s\n", cur->name);
    printf("\n");
    printf("Hardware cache events:\n");
    printf("  Specified on the form 'hwc:cache:operation:result'.\n");
    printf("  Available cache levels:\n");
    for (const event_name_id_t *cur = hwc_events; cur->name; cur++)
        printf("    %s\n", cur->name);
    printf("  Available operations:\n");
    printf("    r - Read\n");
    printf("    w - Write\n");
    printf("    p - Prefetch\n");
    printf("  Available results:\n");
    printf("    a - Access\n");
    printf("    m - Miss\n");
    printf("\n");
    printf("Raw events:\n");
    printf("  Raw events are specified on the form 'raw:NUM' where NUM "
           "is the event number.\n");
    exit(0);
}

canonical_original_code:
static void events_usage()
{
    printf("Software events:\n");
    for (struct event_name_id_t *cur = sw_events; cur->name; cur++)
        printf("  sw:%s\n", cur->name);
    printf("\n");
    printf("Hardware events:\n");
    for (struct event_name_id_t *cur = hw_events; cur->name; cur++)
        printf("  hw:%s\n", cur->name);
    printf("\n");
    printf("Hardware cache events:\n");
    printf("  Specified on the form 'hwc:cache:operation:result'.\n");
    printf("  Available cache levels:\n");
    for (struct event_name_id_t *cur = hwc_events; cur->name; cur++)
        printf("    %s\n", cur->name);
    printf("  Available operations:\n");
    printf("    r - Read\n");
    printf("    w - Write\n");
    printf("    p - Prefetch\n");
    printf("  Available results:\n");
    printf("    a - Access\n");
    printf("    m - Miss\n");
    printf("\n");
    printf("Raw events:\n");
    printf("  Raw events are specified on the form 'raw:NUM' where NUM "
           "is the event number.\n");
    exit(0);
}

variable_types: struct event_name_id_t { char *name; unsigned long id; } *


decompiled_code:
_BOOL8 __fastcall is_sw_event(char *s2)
{
return strncmp("sw:", s2, 3uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char *s2)
{
return strncmp("sw:", s2, 3uLL) == 0;
}

original_code:
static int
is_sw_event(const char *arg)
{
    return !strncmp(prefix_sw_event, arg, sizeof(prefix_sw_event) - 1);
}

canonical_original_code:
static int is_sw_event(char *arg)
{
    return !strncmp(prefix_sw_event, arg, sizeof(prefix_sw_event) - 1);
}

variable_types:
arg
char *


decompiled_code:
_BOOL8 __fastcall is_hw_event(char *s2)
{
return strncmp("hw:", s2, 3uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(char *s2)
{
return strncmp("hw:", s2, 3uLL) == 0;
}

original_code:
static int
is_hw_event(const char *arg)
{
    return !strncmp(prefix_hw_event, arg, sizeof(prefix_hw_event) - 1);
}

canonical_original_code:
static int is_hw_event(char *arg)
{
    return !strncmp(prefix_hw_event, arg, sizeof(prefix_hw_event) - 1);
}


decompiled_code:
_BOOL8 __fastcall is_hwc_event(char *s2)
{
return strncmp("hwc:", s2, 4uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(char *s2)
{
return strncmp("hwc:", s2, 4uLL) == 0;
}

original_code:
static int
is_hwc_event(const char *arg)
{
    return !strncmp(prefix_hwc_event, arg, sizeof(prefix_hwc_event) - 1);
}

canonical_original_code:
static int is_hwc_event(char *arg)
{
    return !strncmp(prefix_hwc_event, arg, sizeof(prefix_hwc_event) - 1);
}


decompiled_code:
_BOOL8 __fastcall is_raw_event(char *s2)
{
return strncmp("raw:", s2, 4uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(char *s2)
{
return strncmp("raw:", s2, 4uLL) == 0;
}

original_code:
static int
is_raw_event(const char *arg)
{
    return !strncmp(prefix_raw_event, arg, sizeof(prefix_raw_event) - 1);
}

canonical_original_code:
static int is_raw_event(char *arg)
{
    return !strncmp(prefix_raw_event, arg, sizeof(prefix_raw_event) - 1);
}


decompiled_code:
const char **__fastcall find_event(const char **a1, const char *a2, int a3)
{
const char **v3; // r12
const char *v5; // rbp
size_t v6; // rbx
char v7; // al

if ( !a1 )
_assert_fail("list", "perf_argp.c", 0xB1u, "find_event");
v3 = a1;
if ( !a2 )
_assert_fail("name", "perf_argp.c", 0xB2u, "find_event");
v5 = *a1;
if ( *a1 )
{
do
{
v6 = (int)strlen(v5);
if ( !strncmp(v5, a2, v6) )
{
v7 = a2[v6];
if ( !v7 )
return v3;
if ( v7 == 58 && a3 )
return v3;
}
v3 += 2;
v5 = *v3;
}
while ( *v3 );
}
return (const char **)v5;
}

canonical_decompiled_code:
const char **__fastcall func5(const char **a1, const char *a2, int a3)
{
const char **v3; // r12
const char *v5; // rbp
size_t v6; // rbx
char v7; // al

if ( !a1 )
_assert_fail("list", "perf_argp.c", 0xB1u, "find_event");
v3 = a1;
if ( !a2 )
_assert_fail("name", "perf_argp.c", 0xB2u, "find_event");
v5 = *a1;
if ( *a1 )
{
do
{
v6 = (int)strlen(v5);
if ( !strncmp(v5, a2, v6) )
{
v7 = a2[v6];
if ( !v7 )
return v3;
if ( v7 == 58 && a3 )
return v3;
}
v3 += 2;
v5 = *v3;
}
while ( *v3 );
}
return (const char **)v5;
}

original_code:
static const event_name_id_t *
find_event(const event_name_id_t *list, const char *name, int extended)
{
    ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "perf_argp.c", 177, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((name) ? 1 : 0), __extension__ ({ if (name) ; else __assert_fail ("name", "perf_argp.c", 178, __extension__ __PRETTY_FUNCTION__); }));
    for (const event_name_id_t *cur = list; cur->name; cur++) {
        const int cur_len = strlen(cur->name);
        if (!strncmp(cur->name, name, cur_len) &&
            (name[cur_len] == '\0' || (extended && name[cur_len] == ':')))
            return cur;
    }
    return ((void *)0);
}

canonical_original_code:
static struct event_name_id_t *find_event(struct event_name_id_t *list, char *name, int extended)
{
    ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "perf_argp.c", 177, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((name) ? 1 : 0), __extension__ ({ if (name) ; else __assert_fail ("name", "perf_argp.c", 178, __extension__ __PRETTY_FUNCTION__); }));
    for (struct event_name_id_t *cur = list; cur->name; cur++) {
        int cur_len = strlen(cur->name);
        if (!strncmp(cur->name, name, cur_len) &&
            (name[cur_len] == '\0' || (extended && name[cur_len] == ':')))
            return cur;
    }
    return ((void *)0);
}

variable_types:
list
struct event_name_id_t *

variable_types:
name
char *

variable_types: extended int

return_type: struct event_name_id_t { char *name; unsigned long id; } *


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

sigemptyset(&v1);
sigaddset(&v1, 17);
sigaddset(&v1, 2);
sigaddset(&v1, 10);
if ( sigprocmask(0, &v1, 0LL) == -1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 174LL, "sigprocmask(SIG_BLOCK, &mask, NULL) != -1");
abort();
}
result = signalfd(-1, &v1, 0);
if ( result == -1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 176LL, "sfd != -1");
abort();
}
return result;
}

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

sigemptyset(&v1);
sigaddset(&v1, 17);
sigaddset(&v1, 2);
sigaddset(&v1, 10);
if ( sigprocmask(0, &v1, 0LL) == -1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 174LL, "sigprocmask(SIG_BLOCK, &mask, NULL) != -1");
abort();
}
result = signalfd(-1, &v1, 0);
if ( result == -1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 176LL, "sfd != -1");
abort();
}
return result;
}

original_code:
static int
create_sig_fd()
{
    sigset_t mask;
    int sfd;
    sigemptyset(&mask);
    sigaddset(&mask, 17);
    sigaddset(&mask, 2);
    sigaddset(&mask, 10);
    do { if (!(sigprocmask(0, &mask, ((void *)0)) != -1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 174, "sigprocmask(SIG_BLOCK, &mask, NULL) != -1"); abort(); } } while (0);
    sfd = signalfd(-1, &mask, 0);
    do { if (!(sfd != -1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 176, "sfd != -1"); abort(); } } while (0);
    return sfd;
}

canonical_original_code:
static int create_sig_fd()
{
    struct __sigset_t mask;
    int sfd;
    sigemptyset(&mask);
    sigaddset(&mask, 17);
    sigaddset(&mask, 2);
    sigaddset(&mask, 10);
    do { if (!(sigprocmask(0, &mask, ((void *)0)) != -1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 174, "sigprocmask(SIG_BLOCK, &mask, NULL) != -1"); abort(); } } while (0);
    sfd = signalfd(-1, &mask, 0);
    do { if (!(sfd != -1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 176, "sfd != -1"); abort(); } } while (0);
    return sfd;
}

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

variable_types: sfd int


barrier

decompiled_code:
void barrier()
{
_mm_mfence();
}

canonical_decompiled_code:
void func4()
{
_mm_mfence();
}

original_code:
static inline void
barrier()
{
    __sync_synchronize();
}


decompiled_code:
__int64 read_head()
{
__int64 v0; // rbx

v0 = *(_QWORD *)(perf_header + 1024);
barrier();
return v0;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx

v0 = *(_QWORD *)(perf_header + 1024);
func4();
return v0;
}

original_code:
static uint64_t
read_head()
{
    long head;
    head = perf_header->data_head;
    barrier();
    return head;
}

canonical_original_code:
static unsigned long read_head()
{
    long head;
    head = perf_header->data_head;
    barrier();
    return head;
}

variable_types: head long


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

barrier();
result = perf_header;
*(_QWORD *)(perf_header + 1032) = a1;
return result;
}

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

func4();
result = perf_header;
*(_QWORD *)(perf_header + 1032) = a1;
return result;
}

original_code:
static void
write_tail(uint64_t tail)
{
    barrier();
    perf_header->data_tail = tail;
}

canonical_original_code:
static void write_tail(unsigned long tail)
{
    barrier();
    perf_header->data_tail = tail;
}

variable_types:
tail
unsigned long


decompiled_code:
int __fastcall handle_signal(int a1)
{
ssize_t v1; // rax
int *v2; // rax
char *v3; // rax
int stat_loc; // [rsp+Ch] [rbp-8Ch] BYREF
unsigned int buf[3]; // [rsp+10h] [rbp-88h] BYREF
__pid_t pid; // [rsp+1Ch] [rbp-7Ch]

v1 = read(a1, buf, 0x80uLL);
if ( v1 != 128 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 185LL, "read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)");
abort();
}
if ( buf[0] != 10 )
{
if ( buf[0] == 17 )
{
if ( attach_pid == -1 )
{
if ( exec_pid != -1 )
{
if ( pid == exec_pid )
{
if ( waitpid(pid, &stat_loc, 1) > 0 )
{
dump_events();
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
{
fprintf(stderr, "Child terminated by signal '%i'.\n", stat_loc & 0x7F);
if ( (stat_loc & 0x80u) != 0 )
fwrite("Core dumped.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 220LL, "0");
abort();
}
fprintf(stderr, "Child exited with status '%i'.\n", BYTE1(stat_loc));
exit(BYTE1(stat_loc) != 0);
}
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(
stderr,
"%s:%i: %s\n %s\n",
"perfrecord.c",
206LL,
"waitpid(fdsi.ssi_pid, &status, WNOHANG) > 0",
v3);
abort();
}
_assert_fail("exec_pid == fdsi.ssi_pid", "perfrecord.c", 0xCCu, "handle_signal");
}
_assert_fail("exec_pid != NO_PID", "perfrecord.c", 0xCBu, "handle_signal");
}
_assert_fail("attach_pid == NO_PID", "perfrecord.c", 0xCAu, "handle_signal");
}
if ( buf[0] == 2 )
{
dump_events();
if ( exec_pid == -1 )
exit(0);
fwrite("Sending SIGTERM to child.\n", 1uLL, 0x1AuLL, stderr);
LODWORD(v1) = kill(exec_pid, 15);
}
else
{
LODWORD(v1) = fprintf(stderr, "Unhandled signal: %i\n", buf[0]);
}
}
return v1;
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
ssize_t v1; // rax
int *v2; // rax
char *v3; // rax
int stat_loc; // [rsp+Ch] [rbp-8Ch] BYREF
unsigned int buf[3]; // [rsp+10h] [rbp-88h] BYREF
__pid_t pid; // [rsp+1Ch] [rbp-7Ch]

v1 = read(a1, buf, 0x80uLL);
if ( v1 != 128 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 185LL, "read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)");
abort();
}
if ( buf[0] != 10 )
{
if ( buf[0] == 17 )
{
if ( attach_pid == -1 )
{
if ( exec_pid != -1 )
{
if ( pid == exec_pid )
{
if ( waitpid(pid, &stat_loc, 1) > 0 )
{
func7();
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
{
fprintf(stderr, "Child terminated by signal '%i'.\n", stat_loc & 0x7F);
if ( (stat_loc & 0x80u) != 0 )
fwrite("Core dumped.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 220LL, "0");
abort();
}
fprintf(stderr, "Child exited with status '%i'.\n", BYTE1(stat_loc));
exit(BYTE1(stat_loc) != 0);
}
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(
stderr,
"%s:%i: %s\n %s\n",
"perfrecord.c",
206LL,
"waitpid(fdsi.ssi_pid, &status, WNOHANG) > 0",
v3);
abort();
}
_assert_fail("exec_pid == fdsi.ssi_pid", "perfrecord.c", 0xCCu, "handle_signal");
}
_assert_fail("exec_pid != NO_PID", "perfrecord.c", 0xCBu, "handle_signal");
}
_assert_fail("attach_pid == NO_PID", "perfrecord.c", 0xCAu, "handle_signal");
}
if ( buf[0] == 2 )
{
func7();
if ( exec_pid == -1 )
exit(0);
fwrite("Sending SIGTERM to child.\n", 1uLL, 0x1AuLL, stderr);
LODWORD(v1) = kill(exec_pid, 15);
}
else
{
LODWORD(v1) = fprintf(stderr, "Unhandled signal: %i\n", buf[0]);
}
}
return v1;
}

original_code:
static void
handle_signal(int sfd)
{
    struct signalfd_siginfo fdsi;
    do { if (!(read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi))) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 185, "read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)"); abort(); } } while (0);
    switch (fdsi.ssi_signo) {
    case 2:
        dump_events();
        if (exec_pid != -1) {
            fprintf(stderr, "Sending SIGTERM to child.\n");
            kill(exec_pid, 15);
        } else
            exit(0);
        break;
    case 17: {
        int status;
        ((void) sizeof ((attach_pid == -1) ? 1 : 0), __extension__ ({ if (attach_pid == -1) ; else __assert_fail ("attach_pid == NO_PID", "perfrecord.c", 202, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((exec_pid != -1) ? 1 : 0), __extension__ ({ if (exec_pid != -1) ; else __assert_fail ("exec_pid != NO_PID", "perfrecord.c", 203, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((exec_pid == fdsi.ssi_pid) ? 1 : 0), __extension__ ({ if (exec_pid == fdsi.ssi_pid) ; else __assert_fail ("exec_pid == fdsi.ssi_pid", "perfrecord.c", 204, __extension__ __PRETTY_FUNCTION__); }));
        do { if (!(waitpid(fdsi.ssi_pid, &status, 1) > 0)) { fprintf(stderr, "%s:%i: %s\n  %s\n", "perfrecord.c", 206, "waitpid(fdsi.ssi_pid, &status, WNOHANG) > 0", strerror((*__errno_location ()))); abort(); } } while (0);
        dump_events();
        if ((((status) & 0x7f) == 0)) {
            fprintf(stderr, "Child exited with status '%i'.\n",
                    (((status) & 0xff00) >> 8));
            exit((((status) & 0xff00) >> 8) == 0 ? 0 : 1);
        } else if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            fprintf(stderr, "Child terminated by signal '%i'.\n",
                    ((status) & 0x7f));
            if (((status) & 0x80))
                fprintf(stderr, "Core dumped.\n");
            exit(1);
        } else
            do { if (!(0)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 220, "0"); abort(); } } while (0);
    } break;
    case 10:
        break;
    default:
        fprintf(stderr, "Unhandled signal: %i\n", fdsi.ssi_signo);
        break;
    }
}

canonical_original_code:
static void handle_signal(int sfd)
{
    struct signalfd_siginfo fdsi;
    do { if (!(read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi))) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 185, "read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)"); abort(); } } while (0);
    switch (fdsi.ssi_signo) {
    case 2:
        dump_events();
        if (exec_pid != -1) {
            fprintf(stderr, "Sending SIGTERM to child.\n");
            kill(exec_pid, 15);
        } else
            exit(0);
        break;
    case 17: {
        int status;
        ((void) sizeof ((attach_pid == -1) ? 1 : 0), __extension__ ({ if (attach_pid == -1) ; else __assert_fail ("attach_pid == NO_PID", "perfrecord.c", 202, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((exec_pid != -1) ? 1 : 0), __extension__ ({ if (exec_pid != -1) ; else __assert_fail ("exec_pid != NO_PID", "perfrecord.c", 203, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((exec_pid == fdsi.ssi_pid) ? 1 : 0), __extension__ ({ if (exec_pid == fdsi.ssi_pid) ; else __assert_fail ("exec_pid == fdsi.ssi_pid", "perfrecord.c", 204, __extension__ __PRETTY_FUNCTION__); }));
        do { if (!(waitpid(fdsi.ssi_pid, &status, 1) > 0)) { fprintf(stderr, "%s:%i: %s\n  %s\n", "perfrecord.c", 206, "waitpid(fdsi.ssi_pid, &status, WNOHANG) > 0", strerror((*__errno_location ()))); abort(); } } while (0);
        dump_events();
        if ((((status) & 0x7f) == 0)) {
            fprintf(stderr, "Child exited with status '%i'.\n",
                    (((status) & 0xff00) >> 8));
            exit((((status) & 0xff00) >> 8) == 0 ? 0 : 1);
        } else if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            fprintf(stderr, "Child terminated by signal '%i'.\n",
                    ((status) & 0x7f));
            if (((status) & 0x80))
                fprintf(stderr, "Core dumped.\n");
            exit(1);
        } else
            do { if (!(0)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 220, "0"); abort(); } } while (0);
    } break;
    case 10:
        break;
    default:
        fprintf(stderr, "Unhandled signal: %i\n", fdsi.ssi_signo);
        break;
    }
}

variable_types: struct signalfd_siginfo { unsigned int ssi_signo; int ssi_errno; int ssi_code; unsigned int ssi_pid; unsigned int ssi_uid; int ssi_fd; unsigned int ssi_tid; unsigned int ssi_band; unsigned int ssi_overrun; unsigned int ssi_trapno; int ssi_status; int ssi_int; unsigned long ssi_ptr; unsigned long ssi_utime; unsigned long ssi_stime; unsigned long ssi_addr; unsigned char __pad[48]; }

variable_types: status int


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

if ( force_cpu != -1 )
result = pin_process(0, force_cpu);
return result;
}

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

if ( force_cpu != -1 )
result = func1(0, force_cpu);
return result;
}

original_code:
static void
setup_child(void *data)
{
    if (force_cpu != -1)
        pin_process(0, force_cpu);
}

variable_types:
data
void *


decompiled_code:
unsigned __int64 dump_events()
{
unsigned __int64 result; // rax
__int64 v1; // rbx
size_t v2; // rsi
__int64 v3; // rdi
__int64 v4; // rbp

result = read_head();
if ( my_head > result )
_assert_fail("new_head >= my_head", "perfrecord.c", 0x6Au, "dump_events");
v1 = result;
if ( my_head != result )
{
v2 = result - my_head;
v3 = buf_mask & my_head;
if ( (~buf_mask & (result - my_head + (buf_mask & my_head) - 1)) != 0 )
{
v4 = buf_mask & (result - my_head + (buf_mask & my_head));
fwrite((const void *)(perf_buf + v3), v2 - v4, 1uLL, output);
v2 = v4;
v3 = 0LL;
}
if ( fwrite((const void *)(perf_buf + v3), v2, 1uLL, output) != 1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 132LL, "fwrite(perf_buf + start, size, 1, output) == 1");
abort();
}
my_head = v1;
result = write_tail(v1);
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func7()
{
unsigned __int64 result; // rax
__int64 v1; // rbx
size_t v2; // rsi
__int64 v3; // rdi
__int64 v4; // rbp

result = func5();
if ( my_head > result )
_assert_fail("new_head >= my_head", "perfrecord.c", 0x6Au, "dump_events");
v1 = result;
if ( my_head != result )
{
v2 = result - my_head;
v3 = buf_mask & my_head;
if ( (~buf_mask & (result - my_head + (buf_mask & my_head) - 1)) != 0 )
{
v4 = buf_mask & (result - my_head + (buf_mask & my_head));
fwrite((const void *)(perf_buf + v3), v2 - v4, 1uLL, output);
v2 = v4;
v3 = 0LL;
}
if ( fwrite((const void *)(perf_buf + v3), v2, 1uLL, output) != 1 )
{
fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 132LL, "fwrite(perf_buf + start, size, 1, output) == 1");
abort();
}
my_head = v1;
result = func6(v1);
}
return result;
}

original_code:
static void
dump_events()
{
    uint64_t new_head = read_head();
    size_t size, start;
    ((void) sizeof ((new_head >= my_head) ? 1 : 0), __extension__ ({ if (new_head >= my_head) ; else __assert_fail ("new_head >= my_head", "perfrecord.c", 106, __extension__ __PRETTY_FUNCTION__); }));
    if (new_head == my_head)
        return;
    size = new_head - my_head;
    start = my_head & buf_mask;
    if ((start + size - 1) & (~buf_mask)) {
        size_t start_size = (start + size) & buf_mask;
        fwrite(perf_buf + start, size - start_size, 1, output);
        start = 0;
        size = start_size;
    }
    do { if (!(fwrite(perf_buf + start, size, 1, output) == 1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 132, "fwrite(perf_buf + start, size, 1, output) == 1"); abort(); } } while (0);
    my_head = new_head;
    write_tail(new_head);
}

canonical_original_code:
static void dump_events()
{
    unsigned long new_head = read_head();
    unsigned long size; unsigned long start;
    ((void) sizeof ((new_head >= my_head) ? 1 : 0), __extension__ ({ if (new_head >= my_head) ; else __assert_fail ("new_head >= my_head", "perfrecord.c", 106, __extension__ __PRETTY_FUNCTION__); }));
    if (new_head == my_head)
        return;
    size = new_head - my_head;
    start = my_head & buf_mask;
    if ((start + size - 1) & (~buf_mask)) {
        unsigned long start_size = (start + size) & buf_mask;
        fwrite(perf_buf + start, size - start_size, 1, output);
        start = 0;
        size = start_size;
    }
    do { if (!(fwrite(perf_buf + start, size, 1, output) == 1)) { fprintf(stderr, "%s:%i: %s\n", "perfrecord.c", 132, "fwrite(perf_buf + start, size, 1, output) == 1"); abort(); } } while (0);
    my_head = new_head;
    write_tail(new_head);
}

variable_types:
size
unsigned long

variable_types:
start
unsigned long