decompiled_code:
__int64 __fastcall mt_note_set_fret(_DWORD *a1, int a2)
{
__int64 result; // rax
double v3; // xmm0_8
__m128d v4; // xmm1
double v5; // xmm2_8
__m128d v6; // xmm4

if ( !a1 )
_assert_fail("note != NULL", "src/mt_note.c", 0x50u, "mt_note_set_fret");
if ( *a1 )
_assert_fail("note->type == MT_NOTE_NONE", "src/mt_note.c", 0x51u, "mt_note_set_fret");
*a1 = 1;
a1[2] = a2;
result = 1LL;
if ( a2 )
{
v3 = log10(COERCE_DOUBLE(COERCE_UNSIGNED_INT64((double)a2) & LC4));
v4.m128d_f64[0] = v3;
v5 = v3;
if ( COERCE_DOUBLE(*(_QWORD *)&v3 & LC4) < 4.503599627370496e15 )
{
v6.m128d_f64[0] = (double)(int)v3;
*(_QWORD *)&v5 = COERCE_UNSIGNED_INT64(v6.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v6, v4) & LC6)) | ~(_QWORD)LC4 & *(_QWORD *)&v3;
}
result = (unsigned int)(int)(v5 + 1.0);
}
a1[4] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, int a2)
{
__int64 result; // rax
double v3; // xmm0_8
__m128d v4; // xmm1
double v5; // xmm2_8
__m128d v6; // xmm4

if ( !a1 )
_assert_fail("note != NULL", "src/mt_note.c", 0x50u, "mt_note_set_fret");
if ( *a1 )
_assert_fail("note->type == MT_NOTE_NONE", "src/mt_note.c", 0x51u, "mt_note_set_fret");
*a1 = 1;
a1[2] = a2;
result = 1LL;
if ( a2 )
{
v3 = log10(COERCE_DOUBLE(COERCE_UNSIGNED_INT64((double)a2) & LC4));
v4.m128d_f64[0] = v3;
v5 = v3;
if ( COERCE_DOUBLE(*(_QWORD *)&v3 & LC4) < 4.503599627370496e15 )
{
v6.m128d_f64[0] = (double)(int)v3;
*(_QWORD *)&v5 = COERCE_UNSIGNED_INT64(v6.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v6, v4) & LC6)) | ~(_QWORD)LC4 & *(_QWORD *)&v3;
}
result = (unsigned int)(int)(v5 + 1.0);
}
a1[4] = result;
return result;
}

original_code:
void mt_note_set_fret(MtNote* note, int fret)
{
  ((void) sizeof ((note != ((void *)0)) ? 1 : 0), __extension__ ({ if (note != ((void *)0)) ; else __assert_fail ("note != NULL", "src/mt_note.c", 80, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((note->type == MT_NOTE_NONE) ? 1 : 0), __extension__ ({ if (note->type == MT_NOTE_NONE) ; else __assert_fail ("note->type == MT_NOTE_NONE", "src/mt_note.c", 81, __extension__ __PRETTY_FUNCTION__); }));
  note->type = MT_NOTE_NOTE;
  note->fret = fret;
  note->size = (fret == 0) ? 1 : (int) (floor(log10(fabs(fret))) + 1);
}

canonical_original_code:
void mt_note_set_fret(struct MtNote *note, int fret)
{
  ((void) sizeof ((note != ((void *)0)) ? 1 : 0), __extension__ ({ if (note != ((void *)0)) ; else __assert_fail ("note != NULL", "src/mt_note.c", 80, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((note->type == MT_NOTE_NONE) ? 1 : 0), __extension__ ({ if (note->type == MT_NOTE_NONE) ; else __assert_fail ("note->type == MT_NOTE_NONE", "src/mt_note.c", 81, __extension__ __PRETTY_FUNCTION__); }));
  note->type = MT_NOTE_NOTE;
  note->fret = fret;
  note->size = (fret == 0) ? 1 : (int) (floor(log10(fabs(fret))) + 1);
}

variable_types: fret int


repo: cknadler/marktab-c

decompiled_code:
void __fastcall mt_note_free(void *a1)
{
if ( !a1 )
_assert_fail("note != NULL", "src/mt_note.c", 0x63u, "mt_note_free");
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
if ( !a1 )
_assert_fail("note != NULL", "src/mt_note.c", 0x63u, "mt_note_free");
free(a1);
}

original_code:
void mt_note_free(MtNote* note)
{
  ((void) sizeof ((note != ((void *)0)) ? 1 : 0), __extension__ ({ if (note != ((void *)0)) ; else __assert_fail ("note != NULL", "src/mt_note.c", 99, __extension__ __PRETTY_FUNCTION__); }));
  (free(note));
}

canonical_original_code:
void mt_note_free(struct MtNote *note)
{
  ((void) sizeof ((note != ((void *)0)) ? 1 : 0), __extension__ ({ if (note != ((void *)0)) ; else __assert_fail ("note != NULL", "src/mt_note.c", 99, __extension__ __PRETTY_FUNCTION__); }));
  (free(note));
}

variable_types: struct MtNote { enum MtNoteType { MT_NOTE_NONE=0, MT_NOTE_NOTE=1, MT_NOTE_MUTE=2 } type; int string; int fret; enum MtModifier { MT_MODIFIER_NONE=0, MT_MODIFIER_PALM_MUTE=1, MT_MODIFIER_HARMONIC=2, MT_MODIFIER_VIBRATO=3 } modifier; int size; } *

return_type: void

user_defined_types: struct MtNote { enum MtNoteType type; int string; int fret; enum MtModifier modifier; int size; }


decompiled_code:
void __fastcall mt_chord_set_modifier(__int64 a1, int a2)
{
if ( !a1 )
_assert_fail("chord != NULL", "src/mt_chord.c", 0x21u, "mt_chord_set_modifier");
*(_DWORD *)(a1 + 8) = a2;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int a2)
{
if ( !a1 )
_assert_fail("chord != NULL", "src/mt_chord.c", 0x21u, "mt_chord_set_modifier");
*(_DWORD *)(a1 + 8) = a2;
}

original_code:
void mt_chord_set_modifier(MtChord* chord, MtModifier modifier)
{
  ((void) sizeof ((chord != ((void *)0)) ? 1 : 0), __extension__ ({ if (chord != ((void *)0)) ; else __assert_fail ("chord != NULL", "src/mt_chord.c", 33, __extension__ __PRETTY_FUNCTION__); }));
  chord->modifier = modifier;
}

canonical_original_code:
void mt_chord_set_modifier(struct MtChord *chord, enum MtModifier modifier)
{
  ((void) sizeof ((chord != ((void *)0)) ? 1 : 0), __extension__ ({ if (chord != ((void *)0)) ; else __assert_fail ("chord != NULL", "src/mt_chord.c", 33, __extension__ __PRETTY_FUNCTION__); }));
  chord->modifier = modifier;
}

variable_types: struct MtChord { struct MtQueue { int length; struct MtQueueNode { void *value; struct MtQueueNode *next; } *front; struct MtQueueNode *back; } *notes; enum MtModifier { MT_MODIFIER_NONE=0, MT_MODIFIER_PALM_MUTE=1, MT_MODIFIER_HARMONIC=2, MT_MODIFIER_VIBRATO=3 } modifier; int size; } *

user_defined_types: struct MtChord { struct MtQueue *notes; enum MtModifier modifier; int size; }

user_defined_types: struct MtQueue { int length; struct MtQueueNode *front; struct MtQueueNode *back; }


decompiled_code:
void __fastcall mt_chord_free(void *ptr)
{
if ( !ptr )
_assert_fail("chord != NULL", "src/mt_chord.c", 0x27u, "mt_chord_free");
mt_queue_free(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( !ptr )
_assert_fail("chord != NULL", "src/mt_chord.c", 0x27u, "mt_chord_free");
mt_queue_free(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void mt_chord_free(MtChord* chord)
{
  ((void) sizeof ((chord != ((void *)0)) ? 1 : 0), __extension__ ({ if (chord != ((void *)0)) ; else __assert_fail ("chord != NULL", "src/mt_chord.c", 39, __extension__ __PRETTY_FUNCTION__); }));
  mt_queue_free(chord->notes);
  (free(chord));
}

canonical_original_code:
void mt_chord_free(struct MtChord *chord)
{
  ((void) sizeof ((chord != ((void *)0)) ? 1 : 0), __extension__ ({ if (chord != ((void *)0)) ; else __assert_fail ("chord != NULL", "src/mt_chord.c", 39, __extension__ __PRETTY_FUNCTION__); }));
  mt_queue_free(chord->notes);
  (free(chord));
}

user_defined_types: enum MtModifier { MT_MODIFIER_NONE=0, MT_MODIFIER_PALM_MUTE=1, MT_MODIFIER_HARMONIC=2, MT_MODIFIER_VIBRATO=3 }

user_defined_types: struct MtQueueNode { void *value; struct MtQueueNode *next; }


anagram

repo: cknadler/questions

decompiled_code:
__int64 __fastcall anagram(const char *a1, const char *a2)
{
int v3; // ebx
size_t v4; // rdx
__int64 result; // rax
const char *v6; // rax
__int64 v7; // rsi
const char *v8; // rdx
__int64 v9; // rsi
__int64 v10; // rcx
int v11; // eax
int v12[262]; // [rsp+0h] [rbp-418h] BYREF

v3 = strlen(a1);
v4 = strlen(a2);
result = 0LL;
if ( v3 == v4 )
{
memset(v12, 0, 0x400uLL);
if ( v3 <= 0 )
{
result = 1LL;
}
else
{
v6 = a1;
v7 = (unsigned int)(v3 - 1);
do
++v12[*v6++];
while ( v6 != &a1[v7 + 1] );
v8 = a2;
v9 = (__int64)&a2[v7 + 1];
while ( 1 )
{
v10 = *v8;
v11 = v12[v10] - 1;
v12[v10] = v11;
if ( v11 < 0 )
break;
if ( ++v8 == (const char *)v9 )
return 1LL;
}
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, const char *a2)
{
int v3; // ebx
size_t v4; // rdx
__int64 result; // rax
const char *v6; // rax
__int64 v7; // rsi
const char *v8; // rdx
__int64 v9; // rsi
__int64 v10; // rcx
int v11; // eax
int v12[262]; // [rsp+0h] [rbp-418h] BYREF

v3 = strlen(a1);
v4 = strlen(a2);
result = 0LL;
if ( v3 == v4 )
{
memset(v12, 0, 0x400uLL);
if ( v3 <= 0 )
{
result = 1LL;
}
else
{
v6 = a1;
v7 = (unsigned int)(v3 - 1);
do
++v12[*v6++];
while ( v6 != &a1[v7 + 1] );
v8 = a2;
v9 = (__int64)&a2[v7 + 1];
while ( 1 )
{
v10 = *v8;
v11 = v12[v10] - 1;
v12[v10] = v11;
if ( v11 < 0 )
break;
if ( ++v8 == (const char *)v9 )
return 1LL;
}
result = 0LL;
}
}
return result;
}

original_code:
_Bool anagram(char* string1, char* string2)
{
  int length = strlen(string1);
  if (length != strlen(string2))
    return 0;
  int ascii_table[256];
  memset(ascii_table, 0, sizeof(ascii_table));
  int i;
  for (i = 0; i < length; ++i)
    ++ascii_table[string1[i]];
  for (i = 0; i < length; ++i)
  {
    --ascii_table[string2[i]];
    if (ascii_table[string2[i]] < 0)
      return 0;
  }
  return 1;
}

canonical_original_code:
_Bool anagram(char *string1, char *string2)
{
  int length = strlen(string1);
  if (length != strlen(string2))
    return 0;
  int ascii_table[256];
  memset(ascii_table, 0, sizeof(ascii_table));
  int i;
  for (i = 0; i < length; ++i)
    ++ascii_table[string1[i]];
  for (i = 0; i < length; ++i)
  {
    --ascii_table[string2[i]];
    if (ascii_table[string2[i]] < 0)
      return 0;
  }
  return 1;
}

variable_types: length int

variable_types: i int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
int result; // eax
char *v5; // r12
char *v6; // rbp

v3 = fopen("test.txt", "r");
result = 1;
if ( v3 )
{
v5 = (char *)malloc(0x100uLL);
v6 = (char *)malloc(0x100uLL);
__isoc99_fscanf();
__isoc99_fscanf();
fclose(v3);
if ( (unsigned __int8)anagram(v5, v6) )
printf("%s is an anagram of %s\n", v5, v6);
else
printf("%s is not an anagram of %s\n", v5, v6);
free(v5);
free(v6);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
int result; // eax
char *v5; // r12
char *v6; // rbp

v3 = fopen("test.txt", "r");
result = 1;
if ( v3 )
{
v5 = (char *)malloc(0x100uLL);
v6 = (char *)malloc(0x100uLL);
__isoc99_fscanf();
__isoc99_fscanf();
fclose(v3);
if ( (unsigned __int8)func7(v5, v6) )
printf("%s is an anagram of %s\n", v5, v6);
else
printf("%s is not an anagram of %s\n", v5, v6);
free(v5);
free(v6);
result = 0;
}
return result;
}

original_code:
int main()
{
  int an_int = 1;
  char* ptr = (char*) &an_int;
  if (((int) *ptr) == 1)
    printf("The system is little endian\n");
  else
    printf("The system is big endian\n");
  return 0;
}

canonical_original_code:
int main()
{
  int an_int = 1;
  char *ptr = (char*) &an_int;
  if (((int) *ptr) == 1)
    printf("The system is little endian\n");
  else
    printf("The system is big endian\n");
  return 0;
}

variable_types:
ptr
char *

return_type: int


reverse

decompiled_code:
char *__fastcall reverse(char *dest)
{
__int64 v1; // rax
char *v2; // rdx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

LODWORD(v1) = strlen(dest) - 1;
if ( (int)v1 >= 0 )
{
v1 = (int)v1;
v2 = v4;
do
*v2++ = dest[v1--];
while ( (int)v1 >= 0 );
}
return strcpy(dest, v4);
}

canonical_decompiled_code:
char *__fastcall func7(char *dest)
{
__int64 v1; // rax
char *v2; // rdx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

LODWORD(v1) = strlen(dest) - 1;
if ( (int)v1 >= 0 )
{
v1 = (int)v1;
v2 = v4;
do
*v2++ = dest[v1--];
while ( (int)v1 >= 0 );
}
return strcpy(dest, v4);
}

original_code:
void reverse(char* string)
{
  char back[256];
  int i, length = strlen(string) - 1;
  for(i = length; i >= 0; i--)
    back[length - i] = string[i];
  strcpy(string, back);
}

canonical_original_code:
void reverse(char *string)
{
  char back[256];
  int i; int length = strlen(string) - 1;
  for(i = length; i >= 0; i--)
    back[length - i] = string[i];
  strcpy(string, back);
}

variable_types:
string
char *

variable_types:
back
char [256]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
int result; // eax
char *v5; // rbp

v3 = fopen("test.txt", "r");
result = 1;
if ( v3 )
{
v5 = (char *)malloc(0x100uLL);
__isoc99_fscanf();
fclose(v3);
reverse(v5);
puts(v5);
free(v5);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
int result; // eax
char *v5; // rbp

v3 = fopen("test.txt", "r");
result = 1;
if ( v3 )
{
v5 = (char *)malloc(0x100uLL);
__isoc99_fscanf();
fclose(v3);
func7(v5);
puts(v5);
free(v5);
result = 0;
}
return result;
}


promptAndRead

repo: cksystemsgroup/libscm

decompiled_code:
__int64 __fastcall promptAndRead(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
return a2;
}

original_code:
unsigned long promptAndRead(char *msg, unsigned long defaultVal, char fmtCh)
{
 return defaultVal;
}

variable_types:
msg
char *

variable_types:
defaultVal
unsigned long

variable_types: fmtCh char

return_type: unsigned long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er14
time_t v5; // rbx
unsigned __int64 v6; // r12
unsigned __int64 v7; // r13
time_t v8; // rax
double v9; // r12
__int64 v10; // rdx
FILE *v11; // rdi
int v12; // eax
__int64 i; // rbp

v3 = getPagefault(argc, argv, envp);
setbuf(_bss_start, 0LL);
if ( argc > 1 )
{
fin = fopen(argv[1], "r");
if ( argc > 2 )
{
fout = fopen(argv[2], "w");
goto LABEL_4;
}
}
else
{
fin = (FILE *)stdin;
}
fout = _bss_start;
LABEL_4:
if ( !fin || !fout )
{
fwrite("Could not open file(s): ", 1uLL, 0x18uLL, stderr);
for ( i = 0LL; argc > (int)++i; fprintf(stderr, "%s ", argv[i]) )
;
fputc(10, stderr);
exit(-1);
}
ulCallCount = promptAndRead((__int64)"call count", ulCallCount);
uMinBlockSize = promptAndRead((__int64)"min block size", (unsigned int)uMinBlockSize);
uMaxBlockSize = promptAndRead((__int64)"max block size", (unsigned int)uMaxBlockSize);
v4 = clock();
v5 = time(0LL);
v6 = rdtsc();
doBench();
v7 = rdtsc() - v6;
v8 = time(0LL);
v9 = difftime(v8, v5);
LODWORD(v5) = clock();
fputc(10, fout);
fprintf(
fout,
"\nTotal elapsed time: %.2f (%.4f CPU) (%ld clock ticks read from register)\n",
v9,
(double)((int)v5 - v4) / 1000000.0,
v7);
if ( fin != (FILE *)stdin )
fclose(fin);
v11 = fout;
if ( fout != _bss_start )
fclose(fout);
v12 = getPagefault(v11, "\nTotal elapsed time: %.2f (%.4f CPU) (%ld clock ticks read from register)\n", v10);
return printf("Occurred page faults: %d\n", (unsigned int)(v12 - v3));
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er14
time_t v5; // rbx
unsigned __int64 v6; // r12
unsigned __int64 v7; // r13
time_t v8; // rax
double v9; // r12
__int64 v10; // rdx
FILE *v11; // rdi
int v12; // eax
__int64 i; // rbp

v3 = func8(argc, argv, envp);
setbuf(_bss_start, 0LL);
if ( argc > 1 )
{
fin = fopen(argv[1], "r");
if ( argc > 2 )
{
fout = fopen(argv[2], "w");
goto LABEL_4;
}
}
else
{
fin = (FILE *)stdin;
}
fout = _bss_start;
LABEL_4:
if ( !fin || !fout )
{
fwrite("Could not open file(s): ", 1uLL, 0x18uLL, stderr);
for ( i = 0LL; argc > (int)++i; fprintf(stderr, "%s ", argv[i]) )
;
fputc(10, stderr);
exit(-1);
}
ulCallCount = func10((__int64)"call count", ulCallCount);
uMinBlockSize = func10((__int64)"min block size", (unsigned int)uMinBlockSize);
uMaxBlockSize = func10((__int64)"max block size", (unsigned int)uMaxBlockSize);
v4 = clock();
v5 = time(0LL);
v6 = func7();
func9();
v7 = func7() - v6;
v8 = time(0LL);
v9 = difftime(v8, v5);
LODWORD(v5) = clock();
fputc(10, fout);
fprintf(
fout,
"\nTotal elapsed time: %.2f (%.4f CPU) (%ld clock ticks read from register)\n",
v9,
(double)((int)v5 - v4) / 1000000.0,
v7);
if ( fin != (FILE *)stdin )
fclose(fin);
v11 = fout;
if ( fout != _bss_start )
fclose(fout);
v12 = func8(v11, "\nTotal elapsed time: %.2f (%.4f CPU) (%ld clock ticks read from register)\n", v10);
return printf("Occurred page faults: %d\n", (unsigned int)(v12 - v3));
}

original_code:
void main(int argc, char *argv[])
{
 clock_t startCPU;
 time_t startTime;
 double elapsedTime, cpuTime;
 uint64_t clock_reg;
 int pf0 = getPagefault();
 setbuf(stdout, ((void *)0));
 if (argc > 1)
  fin = fopen(argv[1], "r");
 else
  fin = stdin;
 if (argc > 2)
  fout = fopen(argv[2], "w");
 else
  fout = stdout;
 if(fin == ((void *)0) || fout == ((void *)0)) {
  fprintf(stderr, "Could not open file(s): ");
  int i=1;
  for(i=1; i<argc;i++) {
   fprintf(stderr, "%s ", argv[i]);
  }
  fprintf(stderr, "\n");
  exit(-1);
 }
 ulCallCount = promptAndRead("call count", ulCallCount, 'u');
 uMinBlockSize = (unsigned)promptAndRead("min block size",uMinBlockSize,'u');
 uMaxBlockSize = (unsigned)promptAndRead("max block size",uMaxBlockSize,'u');
        0;
 startCPU = clock();
 startTime = time(((void *)0));
 clock_reg = rdtsc();
 doBench(((void *)0));
 uint64_t cpuTime_reg = (rdtsc() - clock_reg);
 elapsedTime = difftime(time(((void *)0)), startTime);
 cpuTime = (double)(clock()-startCPU) / (double)((__clock_t) 1000000);
 fprintf(fout, "\n");
 fprintf(fout, "\nTotal elapsed time"
   ": %.2f (%.4f CPU) (%ld clock ticks read from register)\n",
   elapsedTime, cpuTime, cpuTime_reg);
 if (fin != stdin)
  fclose(fin);
 if (fout != stdout)
  fclose(fout);
 printf("Occurred page faults: %d\n", getPagefault() - pf0);
}

canonical_original_code:
void main(int argc, char *argv[])
{
 long startCPU;
 long startTime;
 double elapsedTime; double cpuTime;
 unsigned long clock_reg;
 int pf0 = getPagefault();
 setbuf(stdout, ((void *)0));
 if (argc > 1)
  fin = fopen(argv[1], "r");
 else
  fin = stdin;
 if (argc > 2)
  fout = fopen(argv[2], "w");
 else
  fout = stdout;
 if(fin == ((void *)0) || fout == ((void *)0)) {
  fprintf(stderr, "Could not open file(s): ");
  int i = 1;
  for(i=1; i<argc;i++) {
   fprintf(stderr, "%s ", argv[i]);
  }
  fprintf(stderr, "\n");
  exit(-1);
 }
 ulCallCount = promptAndRead("call count", ulCallCount, 'u');
 uMinBlockSize = (unsigned int)promptAndRead("min block size",uMinBlockSize,'u');
 uMaxBlockSize = (unsigned int)promptAndRead("max block size",uMaxBlockSize,'u');
        0;
 startCPU = clock();
 startTime = time(((void *)0));
 clock_reg = rdtsc();
 doBench(((void *)0));
 unsigned long cpuTime_reg = (rdtsc() - clock_reg);
 elapsedTime = difftime(time(((void *)0)), startTime);
 cpuTime = (double)(clock()-startCPU) / (double)((long) 1000000);
 fprintf(fout, "\n");
 fprintf(fout, "\nTotal elapsed time"
   ": %.2f (%.4f CPU) (%ld clock ticks read from register)\n",
   elapsedTime, cpuTime, cpuTime_reg);
 if (fin != stdin)
  fclose(fin);
 if (fout != stdout)
  fclose(fout);
 printf("Occurred page faults: %d\n", getPagefault() - pf0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: startCPU long

variable_types: startTime long

variable_types: elapsedTime double

variable_types: cpuTime double


decompiled_code:
_QWORD *new_descriptor_page()
{
__int64 v0; // rax
__int64 v1; // rdx
_QWORD *result; // rax

v0 = *(_QWORD *)_tls_get_addr(&descriptor_root_tlsind);
v1 = *(_QWORD *)(v0 + 4728);
if ( v1 )
{
*(_QWORD *)(v0 + 4728) = v1 - 1;
result = *(_QWORD **)(v0 + 8 * v1 + 4640);
LABEL_3:
result[1] = 0LL;
*result = 0LL;
return result;
}
result = (_QWORD *)_real_malloc(4096LL);
if ( result )
goto LABEL_3;
return result;
}

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

v0 = *(_QWORD *)_tls_get_addr(&descriptor_root_tlsind);
v1 = *(_QWORD *)(v0 + 4728);
if ( v1 )
{
*(_QWORD *)(v0 + 4728) = v1 - 1;
result = *(_QWORD **)(v0 + 8 * v1 + 4640);
LABEL_3:
result[1] = 0LL;
*result = 0LL;
return result;
}
result = (_QWORD *)_real_malloc(4096LL);
if ( result )
goto LABEL_3;
return result;
}

original_code:
static descriptor_page_t *new_descriptor_page() {
    descriptor_page_t *new_page = ((void *)0);
    if (descriptor_root->number_of_pooled_descriptor_pages > 0) {
        descriptor_root->number_of_pooled_descriptor_pages--;
        new_page = descriptor_root->descriptor_page_pool
                   [descriptor_root->number_of_pooled_descriptor_pages];
    } else {
        new_page = __real_malloc(4096);
        if (!new_page) {
            return ((void *)0);
        }
    }
    new_page->number_of_descriptors = 0;
    new_page->next = ((void *)0);
    return new_page;
}

canonical_original_code:
static struct descriptor_page *new_descriptor_page() {
    struct descriptor_page *new_page = ((void *)0);
    if (descriptor_root->number_of_pooled_descriptor_pages > 0) {
        descriptor_root->number_of_pooled_descriptor_pages--;
        new_page = descriptor_root->descriptor_page_pool
                   [descriptor_root->number_of_pooled_descriptor_pages];
    } else {
        new_page = __real_malloc(4096);
        if (!new_page) {
            return ((void *)0);
        }
    }
    new_page->number_of_descriptors = 0;
    new_page->next = ((void *)0);
    return new_page;
}

variable_types: struct descriptor_page { struct descriptor_page *next; unsigned long number_of_descriptors; struct object_header { int dc_or_region_id; int finalizer_index; } *descriptors[-1]; } *

user_defined_types: struct descriptor_page { struct descriptor_page *next; unsigned long number_of_descriptors; struct object_header *descriptors[-1]; }


decompiled_code:
__int64 __fastcall recycle_descriptor_page(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx
unsigned __int64 v3; // rax
__int64 result; // rax

v1 = _tls_get_addr(&descriptor_root_tlsind);
v2 = *(_QWORD *)v1;
v3 = *(_QWORD *)(*(_QWORD *)v1 + 4728LL);
if ( v3 > 9 )
return _real_free(a1);
*(_QWORD *)(v2 + 8 * v3 + 4648) = a1;
result = v3 + 1;
*(_QWORD *)(v2 + 4728) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx
unsigned __int64 v3; // rax
__int64 result; // rax

v1 = _tls_get_addr(&descriptor_root_tlsind);
v2 = *(_QWORD *)v1;
v3 = *(_QWORD *)(*(_QWORD *)v1 + 4728LL);
if ( v3 > 9 )
return _real_free(a1);
*(_QWORD *)(v2 + 8 * v3 + 4648) = a1;
result = v3 + 1;
*(_QWORD *)(v2 + 4728) = result;
return result;
}

original_code:
static inline void recycle_descriptor_page(descriptor_page_t *page) {
    if (descriptor_root->number_of_pooled_descriptor_pages <
            10) {
        descriptor_root->descriptor_page_pool[descriptor_root->number_of_pooled_descriptor_pages] = page;
        descriptor_root->number_of_pooled_descriptor_pages++;
    } else {
        __real_free(page);
    }
}

canonical_original_code:
static inline void recycle_descriptor_page(struct descriptor_page *page) {
    if (descriptor_root->number_of_pooled_descriptor_pages <
            10) {
        descriptor_root->descriptor_page_pool[descriptor_root->number_of_pooled_descriptor_pages] = page;
        descriptor_root->number_of_pooled_descriptor_pages++;
    } else {
        __real_free(page);
    }
}

user_defined_types: struct object_header { int dc_or_region_id; int finalizer_index; }


original_code:
static void recycle_region(region_t* region) {
    region_page_t* legacy_pages;
    unsigned long number_of_recycle_region_pages;
    if (region->age == descriptor_root->current_time) {
        region_page_t* firstPage = region->firstPage;
        legacy_pages = firstPage->nextPage;
        memset(firstPage, '\0', 4096);
        region->last_address_in_last_page =
                firstPage->memory + (4096 - sizeof(region_page_t*));
        if (legacy_pages == ((void *)0)) {
            region->dc = 0;
            return;
        }
        number_of_recycle_region_pages =
            region->number_of_region_pages - 1;
    }
    else {
        legacy_pages = region->firstPage;
        if (legacy_pages == ((void *)0)) {
            region->dc = 0;
            return;
        }
        number_of_recycle_region_pages =
            region->number_of_region_pages;
    }
    unsigned long number_of_pooled_region_pages =
        descriptor_root->number_of_pooled_region_pages;
    if ((number_of_pooled_region_pages
            + number_of_recycle_region_pages) <=
            10) {
        region_page_t* first_in_pool = descriptor_root->region_page_pool;
        region_page_t* last_page = region->lastPage;
        last_page->nextPage = first_in_pool;
        descriptor_root->region_page_pool = legacy_pages;
        descriptor_root->number_of_pooled_region_pages =
            number_of_pooled_region_pages + number_of_recycle_region_pages;
    } else {
        region_page_t* page2free = legacy_pages;
        while (page2free != ((void *)0) && (number_of_pooled_region_pages
                + number_of_recycle_region_pages) >
                10) {
            region_page_t* next = page2free->nextPage;
            __real_free(page2free);
            page2free = next;
            number_of_recycle_region_pages--;
        }
        if (page2free != ((void *)0) && (number_of_pooled_region_pages
                + number_of_recycle_region_pages) <=
                10) {
            region_page_t* first_in_pool = descriptor_root->region_page_pool;
            region_page_t* last_page = region->lastPage;
            if(last_page != ((void *)0)) {
                last_page->nextPage = first_in_pool;
                descriptor_root->region_page_pool = page2free;
                descriptor_root->number_of_pooled_region_pages =
                        number_of_recycle_region_pages - 1;
            }
        }
        descriptor_root->number_of_pooled_region_pages =
                number_of_pooled_region_pages + number_of_recycle_region_pages;
    }
    region->number_of_region_pages = 1;
    region->lastPage = region->firstPage;
    region->last_address_in_last_page =
            region->lastPage->memory + (4096 - sizeof(region_page_t*));
    region->next_free_address = region->lastPage->memory;
    if (region->age != descriptor_root->current_time) {
        region->number_of_region_pages = 0;
        region->lastPage = region->firstPage = ((void *)0);
    }
}

canonical_original_code:
static void recycle_region(struct region *region) {
    struct region_page *legacy_pages;
    unsigned long number_of_recycle_region_pages;
    if (region->age == descriptor_root->current_time) {
        struct region_page *firstPage = region->firstPage;
        legacy_pages = firstPage->nextPage;
        memset(firstPage, '\0', 4096);
        region->last_address_in_last_page =
                firstPage->memory + (4096 - sizeof(struct region_page *));
        if (legacy_pages == ((void *)0)) {
            region->dc = 0;
            return;
        }
        number_of_recycle_region_pages =
            region->number_of_region_pages - 1;
    }
    else {
        legacy_pages = region->firstPage;
        if (legacy_pages == ((void *)0)) {
            region->dc = 0;
            return;
        }
        number_of_recycle_region_pages =
            region->number_of_region_pages;
    }
    unsigned long number_of_pooled_region_pages = descriptor_root->number_of_pooled_region_pages;
    if ((number_of_pooled_region_pages
            + number_of_recycle_region_pages) <=
            10) {
        struct region_page *first_in_pool = descriptor_root->region_page_pool;
        struct region_page *last_page = region->lastPage;
        last_page->nextPage = first_in_pool;
        descriptor_root->region_page_pool = legacy_pages;
        descriptor_root->number_of_pooled_region_pages =
            number_of_pooled_region_pages + number_of_recycle_region_pages;
    } else {
        struct region_page *page2free = legacy_pages;
        while (page2free != ((void *)0) && (number_of_pooled_region_pages
                + number_of_recycle_region_pages) >
                10) {
            struct region_page *next = page2free->nextPage;
            __real_free(page2free);
            page2free = next;
            number_of_recycle_region_pages--;
        }
        if (page2free != ((void *)0) && (number_of_pooled_region_pages
                + number_of_recycle_region_pages) <=
                10) {
            struct region_page *first_in_pool = descriptor_root->region_page_pool;
            struct region_page *last_page = region->lastPage;
            if(last_page != ((void *)0)) {
                last_page->nextPage = first_in_pool;
                descriptor_root->region_page_pool = page2free;
                descriptor_root->number_of_pooled_region_pages =
                        number_of_recycle_region_pages - 1;
            }
        }
        descriptor_root->number_of_pooled_region_pages =
                number_of_pooled_region_pages + number_of_recycle_region_pages;
    }
    region->number_of_region_pages = 1;
    region->lastPage = region->firstPage;
    region->last_address_in_last_page =
            region->lastPage->memory + (4096 - sizeof(struct region_page *));
    region->next_free_address = region->lastPage->memory;
    if (region->age != descriptor_root->current_time) {
        region->number_of_region_pages = 0;
        region->lastPage = region->firstPage = ((void *)0);
    }
}

variable_types: struct region { unsigned int dc; unsigned int number_of_region_pages; struct region_page { struct region_page *nextPage; char memory[-1]; } *firstPage; struct region_page *lastPage; unsigned int age; void *next_free_address; void *last_address_in_last_page; } *

user_defined_types: struct region { unsigned int dc; unsigned int number_of_region_pages; struct region_page *firstPage; struct region_page *lastPage; unsigned int age; void *next_free_address; void *last_address_in_last_page; }


original_code:
void expire_buffer(descriptor_buffer_t *buffer,
                   expired_descriptor_page_list_t *exp_list) {
    int to_be_expired_index = buffer->current_index - 1;
    if (to_be_expired_index < 0)
        to_be_expired_index += buffer->not_expired_length;
    descriptor_page_list_t *just_expired_page_list = &buffer->not_expired[to_be_expired_index];
    if (just_expired_page_list->first != ((void *)0)) {
        if (just_expired_page_list->first->number_of_descriptors != 0) {
            if (exp_list->first == ((void *)0)) {
                exp_list->first = just_expired_page_list->first;
                exp_list->last = just_expired_page_list->last;
                exp_list->collected = 0;
            } else {
                exp_list->last->next = just_expired_page_list->first;
                exp_list->last = just_expired_page_list->last;
            }
            just_expired_page_list->first = ((void *)0);
            just_expired_page_list->last = just_expired_page_list->first;
        } else {
        }
    } else {
    }
}

canonical_original_code:
void expire_buffer(struct descriptor_buffer *buffer, struct expired_descriptor_page_list *exp_list) {
    int to_be_expired_index = buffer->current_index - 1;
    if (to_be_expired_index < 0)
        to_be_expired_index += buffer->not_expired_length;
    struct descriptor_page_list *just_expired_page_list = &buffer->not_expired[to_be_expired_index];
    if (just_expired_page_list->first != ((void *)0)) {
        if (just_expired_page_list->first->number_of_descriptors != 0) {
            if (exp_list->first == ((void *)0)) {
                exp_list->first = just_expired_page_list->first;
                exp_list->last = just_expired_page_list->last;
                exp_list->collected = 0;
            } else {
                exp_list->last->next = just_expired_page_list->first;
                exp_list->last = just_expired_page_list->last;
            }
            just_expired_page_list->first = ((void *)0);
            just_expired_page_list->last = just_expired_page_list->first;
        } else {
        }
    } else {
    }
}

variable_types: struct descriptor_buffer { struct descriptor_page_list { struct descriptor_page { struct descriptor_page *next; unsigned long number_of_descriptors; struct object_header { int dc_or_region_id; int finalizer_index; } *descriptors[-1]; } *first; struct descriptor_page *last; } not_expired[-1]; unsigned int not_expired_length; unsigned int current_index; unsigned int age; } *

variable_types: struct expired_descriptor_page_list { struct descriptor_page *first; struct descriptor_page *last; unsigned long collected; } *

variable_types: struct descriptor_page_list { struct descriptor_page { struct descriptor_page *next; unsigned long number_of_descriptors; struct object_header { int dc_or_region_id; int finalizer_index; } *descriptors[-1]; } *first; struct descriptor_page *last; } *

user_defined_types: struct descriptor_buffer { struct descriptor_page_list not_expired[-1]; unsigned int not_expired_length; unsigned int current_index; unsigned int age; }

user_defined_types: struct descriptor_page_list { struct descriptor_page *first; struct descriptor_page *last; }


decompiled_code:
__int64 __fastcall expire_object_descriptor_if_exists(__int64 a1)
{
volatile signed __int32 *v1; // rax
unsigned int v2; // ebx
volatile signed __int32 *v3; // rbp

v1 = (volatile signed __int32 *)get_expired_memory(a1);
v2 = 0;
if ( v1 )
{
v3 = v1;
v2 = atomic_int_exchange_and_add(v1, 0xFFFFFFFF);
if ( v2 == 1 )
{
if ( !(unsigned int)run_finalizer(v3) )
_real_free(v3);
}
else
{
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
volatile signed __int32 *v1; // rax
unsigned int v2; // ebx
volatile signed __int32 *v3; // rbp

v1 = (volatile signed __int32 *)get_expired_memory(a1);
v2 = 0;
if ( v1 )
{
v3 = v1;
v2 = func0(v1, 0xFFFFFFFF);
if ( v2 == 1 )
{
if ( !(unsigned int)run_finalizer(v3) )
_real_free(v3);
}
else
{
v2 = 1;
}
}
return v2;
}

original_code:
int expire_object_descriptor_if_exists(expired_descriptor_page_list_t *list) {
    object_header_t *expired_object = (object_header_t*) get_expired_memory(list);
    if (expired_object != ((void *)0)) {
        if ((atomic_int_exchange_and_add(((int*) &expired_object->dc_or_region_id), -1) == 1)) {
            int finalizer_result = run_finalizer(expired_object);
            if (finalizer_result != 0) {
                return 1;
            }
            __real_free(expired_object);
            return 1;
        } else {
            return 1;
        }
    } else {
        return 0;
    }
}

canonical_original_code:
int expire_object_descriptor_if_exists(struct expired_descriptor_page_list *list) {
    struct object_header *expired_object = (object_header_t*) get_expired_memory(list);
    if (expired_object != ((void *)0)) {
        if ((atomic_int_exchange_and_add(((int *) &expired_object->dc_or_region_id), -1) == 1)) {
            int finalizer_result = run_finalizer(expired_object);
            if (finalizer_result != 0) {
                return 1;
            }
            __real_free(expired_object);
            return 1;
        } else {
            return 1;
        }
    } else {
        return 0;
    }
}

variable_types: struct expired_descriptor_page_list { struct descriptor_page { struct descriptor_page *next; unsigned long number_of_descriptors; struct object_header { int dc_or_region_id; int finalizer_index; } *descriptors[-1]; } *first; struct descriptor_page *last; unsigned long collected; } *

user_defined_types: struct expired_descriptor_page_list { struct descriptor_page *first; struct descriptor_page *last; unsigned long collected; }


decompiled_code:
__int64 __fastcall expire_region_descriptor_if_exists(__int64 a1)
{
volatile signed __int32 *v1; // rax
unsigned int v2; // ebp
__int64 v3; // rbx

v1 = (volatile signed __int32 *)get_expired_memory(a1);
v2 = 0;
if ( v1 )
{
v3 = (__int64)v1;
v2 = atomic_int_exchange_and_add(v1, 0xFFFFFFFF);
if ( v2 == 1 )
recycle_region(v3);
else
v2 = 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
volatile signed __int32 *v1; // rax
unsigned int v2; // ebp
__int64 v3; // rbx

v1 = (volatile signed __int32 *)get_expired_memory(a1);
v2 = 0;
if ( v1 )
{
v3 = (__int64)v1;
v2 = func0(v1, 0xFFFFFFFF);
if ( v2 == 1 )
func3(v3);
else
v2 = 1;
}
return v2;
}

original_code:
int expire_region_descriptor_if_exists(expired_descriptor_page_list_t *list) {
    region_t* expired_region = (region_t*) get_expired_memory(list);
    if (expired_region != ((void *)0)) {
        if ((atomic_int_exchange_and_add(((volatile int*) &expired_region->dc), -1) == 1)) {
            recycle_region(expired_region);
        }
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
int expire_region_descriptor_if_exists(struct expired_descriptor_page_list *list) {
    struct region *expired_region = (region_t*) get_expired_memory(list);
    if (expired_region != ((void *)0)) {
        if ((atomic_int_exchange_and_add(((int *) &expired_region->dc), -1) == 1)) {
            recycle_region(expired_region);
        }
        return 1;
    } else {
        return 0;
    }
}

user_defined_types: struct region_page { struct region_page *nextPage; char memory[-1]; }


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

result = atomic_int_exchange_and_add(finalizer_index, 1u);
if ( (int)result > 31 )
return 0xFFFFFFFFLL;
finalizer_table[(int)result] = a1;
return result;
}

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

result = func0(finalizer_index, 1u);
if ( (int)result > 31 )
return 0xFFFFFFFFLL;
finalizer_table[(int)result] = a1;
return result;
}

original_code:
int scm_register_finalizer(int(*scm_finalizer)(void*)) {
    int index = atomic_int_exchange_and_add(&finalizer_index, 1);
    if (index >= 32) return -1;
    finalizer_table[index] = scm_finalizer;
    return index;
}

canonical_original_code:
int scm_register_finalizer(int (*scm_finalizer)(void *)) {
    int index = atomic_int_exchange_and_add(&finalizer_index, 1);
    if (index >= 32) return -1;
    finalizer_table[index] = scm_finalizer;
    return index;
}

variable_types: index int


decompiled_code:
void __fastcall scm_set_finalizer(__int64 a1, int a2)
{
*(_DWORD *)(a1 - 4) = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, int a2)
{
*(_DWORD *)(a1 - 4) = a2;
}

original_code:
void scm_set_finalizer(void *ptr, int scm_finalizer_id) {
    object_header_t *o = (object_header_t*)(ptr - sizeof(object_header_t));
    o->finalizer_index = scm_finalizer_id;
}

canonical_original_code:
void scm_set_finalizer(void *ptr, int scm_finalizer_id) {
    struct object_header *o = (object_header_t*)(ptr - sizeof(object_header_t));
    o->finalizer_index = scm_finalizer_id;
}

variable_types:
ptr
void *

variable_types: struct object_header { int dc_or_region_id; int finalizer_index; } *


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

v1 = *(_DWORD *)(a1 + 4);
result = 0LL;
if ( v1 != -1 )
result = ((__int64 (__fastcall *)(__int64))finalizer_table[v1])(a1 + 8);
return result;
}

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

v1 = *(_DWORD *)(a1 + 4);
result = 0LL;
if ( v1 != -1 )
result = ((__int64 (__fastcall *)(__int64))finalizer_table[v1])(a1 + 8);
return result;
}

original_code:
int run_finalizer(object_header_t *o) {
    if (o->finalizer_index == -1) return 0;
    void *ptr = ((void*)(o) + sizeof(object_header_t));
    int (*finalizer)(void*);
    finalizer = finalizer_table[o->finalizer_index];
    return (*finalizer)(ptr);
}

canonical_original_code:
int run_finalizer(struct object_header *o) {
    if (o->finalizer_index == -1) return 0;
    void *ptr = ((void*)(o) + sizeof(object_header_t));
    int (*finalizer)(void *);
    finalizer = finalizer_table[o->finalizer_index];
    return (*finalizer)(ptr);
}

variable_types:
finalizer
int (*)(void *)


repo: clarkgrubb/data-tools

decompiled_code:
unsigned __int64 __fastcall push_stack(unsigned __int64 *a1, char a2)
{
unsigned __int64 v3; // rax
unsigned __int64 v4; // rdx
unsigned __int64 result; // rax
size_t v6; // rsi

v3 = *a1 + 1;
*a1 = v3;
v4 = a1[1];
if ( v3 > v4 )
{
v6 = 2 * v4 + 1;
a1[1] = v6;
a1[2] = (unsigned __int64)realloc((void *)a1[2], v6);
}
result = *a1;
*(_BYTE *)(a1[2] + *a1 - 1) = a2;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned __int64 *a1, char a2)
{
unsigned __int64 v3; // rax
unsigned __int64 v4; // rdx
unsigned __int64 result; // rax
size_t v6; // rsi

v3 = *a1 + 1;
*a1 = v3;
v4 = a1[1];
if ( v3 > v4 )
{
v6 = 2 * v4 + 1;
a1[1] = v6;
a1[2] = (unsigned __int64)realloc((void *)a1[2], v6);
}
result = *a1;
*(_BYTE *)(a1[2] + *a1 - 1) = a2;
return result;
}

original_code:
void
push_stack(json_type_stack *stack, json_type val) {
  stack->size += 1;
  if (stack->size > stack->capacity) {
    stack->capacity = 2 * (stack->capacity) + 1;
    stack->data = (json_type *)realloc(stack->data, stack->capacity);
  }
  stack->data[stack->size - 1] = val;
}

canonical_original_code:
void push_stack(struct json_type_stack *stack, _Bool val) {
  stack->size += 1;
  if (stack->size > stack->capacity) {
    stack->capacity = 2 * (stack->capacity) + 1;
    stack->data = (_Bool *)realloc(stack->data, stack->capacity);
  }
  stack->data[stack->size - 1] = val;
}

variable_types: struct json_type_stack { unsigned long size; unsigned long capacity; _Bool *data; } *


decompiled_code:
__int64 __fastcall peek_stack(_QWORD *a1)
{
if ( !*a1 )
{
fwrite("ERROR: stack underflow", 1uLL, 0x16uLL, stderr);
exit(1);
}
return *(unsigned __int8 *)(a1[2] + *a1 - 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
if ( !*a1 )
{
fwrite("ERROR: stack underflow", 1uLL, 0x16uLL, stderr);
exit(1);
}
return *(unsigned __int8 *)(a1[2] + *a1 - 1LL);
}

original_code:
json_type
peek_stack(json_type_stack *stack) {
  if (stack->size == 0) {
    fprintf(stderr, "ERROR: stack underflow");
    exit(1);
  }
  return stack->data[stack->size - 1];
}

canonical_original_code:
_Bool peek_stack(struct json_type_stack *stack) {
  if (stack->size == 0) {
    fprintf(stderr, "ERROR: stack underflow");
    exit(1);
  }
  return stack->data[stack->size - 1];
}

user_defined_types: struct json_type_stack { unsigned long size; unsigned long capacity; _Bool *data; }


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

result = peek_stack(a1);
--*a1;
return result;
}

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

result = func10(a1);
--*a1;
return result;
}

original_code:
json_type
pop_stack(json_type_stack *stack) {
  json_type retval = peek_stack(stack);
  stack->size -= 1;
  return retval;
}

canonical_original_code:
_Bool pop_stack(struct json_type_stack *stack) {
  _Bool retval = peek_stack(stack);
  stack->size -= 1;
  return retval;
}


decompiled_code:
__int64 json_pluck()
{
int v0; // er12
int v1; // ebp
int v2; // er13
__int64 v3; // rbx
__int64 v4; // r14
unsigned int v5; // eax
unsigned __int64 v7[9]; // [rsp+0h] [rbp-48h] BYREF

init_stack(v7);
v0 = 0;
v1 = 0;
v2 = 0;
v3 = 0LL;
v4 = 1LL;
while ( 1 )
{
v5 = getwchar();
if ( v5 == -1 )
break;
++v3;
if ( v5 <= 0x2C )
{
if ( v5 <= 8 )
goto LABEL_3;
switch ( v5 )
{
case 9u:
if ( (_BYTE)v1 )
fatal("tab in string", v4, v3);
v0 = v1;
break;
case 0xAu:
if ( (_BYTE)v1 )
fatal("newline in string", v4, v3);
++v4;
v0 = v1;
v3 = 0LL;
break;
case 0x20u:
v0 = v1;
if ( (_BYTE)v1 )
{
putwchar(32);
v0 = 0;
}
break;
case 0x22u:
v1 = v0 | v1 ^ 1;
putwchar(34);
v0 = 0;
break;
case 0x2Cu:
if ( (_BYTE)v1 )
{
putwchar(44);
v0 = 0;
}
else
{
if ( !v2 )
fatal("unexpected comma", v4, v3);
if ( v2 == 1 && (unsigned __int8)peek_stack(v7) == JSON_TYPE_ARRAY )
{
putwchar(10);
v0 = v1;
}
else
{
putwchar(44);
v0 = v1;
}
}
break;
default:
goto LABEL_3;
}
}
else if ( v5 - 91 > 0x22 )
{
LABEL_3:
putwchar(v5);
v0 = 0;
}
else
{
switch ( v5 )
{
case '[':
if ( (_BYTE)v1 )
{
putwchar(91);
v0 = 0;
}
else
{
push_stack(v7, JSON_TYPE_ARRAY);
if ( v2 > 0 )
putwchar(91);
++v2;
v0 = v1;
}
break;
case '\\':
if ( !(_BYTE)v1 )
fatal("backslash outside of string", v4, v3);
v0 ^= 1u;
putwchar(92);
break;
case ']':
if ( (_BYTE)v1 )
{
putwchar(93);
v0 = 0;
}
else
{
pop_stack(v7);
if ( --v2 > 0 )
{
putwchar(93);
v0 = v1;
}
else
{
if ( v2 < 0 )
fatal("unmatched right square bracket", v4, v3);
v0 = v1;
}
}
break;
case '{':
if ( !(_BYTE)v1 )
push_stack(v7, JSON_TYPE_OBJECT);
putwchar(123);
v0 = 0;
break;
case '}':
if ( !(_BYTE)v1 )
pop_stack(v7);
putwchar(125);
v0 = 0;
break;
default:
goto LABEL_3;
}
}
}
if ( v2 )
fatal("more left brackets than right brackets", v4 - 1, v3);
putwchar(10);
return 0LL;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // er12
int v1; // ebp
int v2; // er13
__int64 v3; // rbx
__int64 v4; // r14
unsigned int v5; // eax
unsigned __int64 v7[9]; // [rsp+0h] [rbp-48h] BYREF

func8(v7);
v0 = 0;
v1 = 0;
v2 = 0;
v3 = 0LL;
v4 = 1LL;
while ( 1 )
{
v5 = getwchar();
if ( v5 == -1 )
break;
++v3;
if ( v5 <= 0x2C )
{
if ( v5 <= 8 )
goto LABEL_3;
switch ( v5 )
{
case 9u:
if ( (_BYTE)v1 )
func7("tab in string", v4, v3);
v0 = v1;
break;
case 0xAu:
if ( (_BYTE)v1 )
func7("newline in string", v4, v3);
++v4;
v0 = v1;
v3 = 0LL;
break;
case 0x20u:
v0 = v1;
if ( (_BYTE)v1 )
{
putwchar(32);
v0 = 0;
}
break;
case 0x22u:
v1 = v0 | v1 ^ 1;
putwchar(34);
v0 = 0;
break;
case 0x2Cu:
if ( (_BYTE)v1 )
{
putwchar(44);
v0 = 0;
}
else
{
if ( !v2 )
func7("unexpected comma", v4, v3);
if ( v2 == 1 && (unsigned __int8)func10(v7) == JSON_TYPE_ARRAY )
{
putwchar(10);
v0 = v1;
}
else
{
putwchar(44);
v0 = v1;
}
}
break;
default:
goto LABEL_3;
}
}
else if ( v5 - 91 > 0x22 )
{
LABEL_3:
putwchar(v5);
v0 = 0;
}
else
{
switch ( v5 )
{
case '[':
if ( (_BYTE)v1 )
{
putwchar(91);
v0 = 0;
}
else
{
func9(v7, JSON_TYPE_ARRAY);
if ( v2 > 0 )
putwchar(91);
++v2;
v0 = v1;
}
break;
case '\\':
if ( !(_BYTE)v1 )
func7("backslash outside of string", v4, v3);
v0 ^= 1u;
putwchar(92);
break;
case ']':
if ( (_BYTE)v1 )
{
putwchar(93);
v0 = 0;
}
else
{
func11(v7);
if ( --v2 > 0 )
{
putwchar(93);
v0 = v1;
}
else
{
if ( v2 < 0 )
func7("unmatched right square bracket", v4, v3);
v0 = v1;
}
}
break;
case '{':
if ( !(_BYTE)v1 )
func9(v7, JSON_TYPE_OBJECT);
putwchar(123);
v0 = 0;
break;
case '}':
if ( !(_BYTE)v1 )
func11(v7);
putwchar(125);
v0 = 0;
break;
default:
goto LABEL_3;
}
}
}
if ( v2 )
func7("more left brackets than right brackets", v4 - 1, v3);
putwchar(10);
return 0LL;
}

original_code:
int
json_pluck() {
  wint_t ch;
  size_t lineno = 1, offsetno = 0;
  int array_depth = 0;
  _Bool inside_string = 0;
  _Bool after_backslash = 0;
  json_type_stack stack;
  init_stack(&stack);
  while ((ch = getwchar()) != (0xffffffffu)) {
    offsetno += 1;
    switch (ch) {
    case L'[':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        push_stack(&stack, JSON_TYPE_ARRAY);
        if (array_depth > 0) {
          putwchar(ch);
        }
        array_depth += 1;
      }
      after_backslash = 0;
      break;
    case L']':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        pop_stack(&stack);
        array_depth -= 1;
        if (array_depth > 0) {
          putwchar(ch);
        }
        if (array_depth < 0) {
          fatal("unmatched right square bracket", lineno, offsetno);
        }
      }
      after_backslash = 0;
      break;
    case L'{':
      if (!inside_string) {
        push_stack(&stack, JSON_TYPE_OBJECT);
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case L'}':
      if (!inside_string) {
        pop_stack(&stack);
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case ',':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        if (array_depth == 0) {
          fatal("unexpected comma", lineno, offsetno);
        }
        else if (array_depth == 1) {
          if (peek_stack(&stack) == JSON_TYPE_ARRAY) {
            putwchar(L'\n');
          }
          else {
            putwchar(ch);
          }
        }
        else {
          putwchar(ch);
        }
      }
      after_backslash = 0;
      break;
    case L'"':
      if (inside_string) {
        if (after_backslash) {
          inside_string = 1;
          after_backslash = 0;
        }
        else {
          inside_string = 0;
        }
      }
      else {
        inside_string = 1;
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case L'\\':
      if (inside_string) {
        after_backslash = !after_backslash;
      }
      else {
        fatal("backslash outside of string", lineno, offsetno);
      }
      putwchar(ch);
      break;
    case L' ':
      if (inside_string) {
        putwchar(ch);
      }
      after_backslash = 0;
      break;
    case L'\t':
      if (inside_string) {
        fatal("tab in string", lineno, offsetno);
      }
      after_backslash = 0;
      break;
    case L'\n':;
      if (inside_string) {
        fatal("newline in string", lineno, offsetno);
      }
      lineno += 1;
      offsetno = 0;
      after_backslash = 0;
      break;
    default:
      putwchar(ch);
      after_backslash = 0;
      break;
    }
  }
  if (array_depth != 0) {
    fatal("more left brackets than right brackets", lineno - 1, offsetno);
  }
  putwchar(L'\n');
  return 0;
}

canonical_original_code:
int json_pluck() {
  unsigned int ch;
  unsigned long lineno = 1; unsigned long offsetno = 0;
  int array_depth = 0;
  _Bool inside_string = 0;
  _Bool after_backslash = 0;
  struct json_type_stack stack;
  init_stack(&stack);
  while ((ch = getwchar()) != (0xffffffffu)) {
    offsetno += 1;
    switch (ch) {
    case L'[':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        push_stack(&stack, JSON_TYPE_ARRAY);
        if (array_depth > 0) {
          putwchar(ch);
        }
        array_depth += 1;
      }
      after_backslash = 0;
      break;
    case L']':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        pop_stack(&stack);
        array_depth -= 1;
        if (array_depth > 0) {
          putwchar(ch);
        }
        if (array_depth < 0) {
          fatal("unmatched right square bracket", lineno, offsetno);
        }
      }
      after_backslash = 0;
      break;
    case L'{':
      if (!inside_string) {
        push_stack(&stack, JSON_TYPE_OBJECT);
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case L'}':
      if (!inside_string) {
        pop_stack(&stack);
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case ',':
      if (inside_string) {
        putwchar(ch);
      }
      else {
        if (array_depth == 0) {
          fatal("unexpected comma", lineno, offsetno);
        }
        else if (array_depth == 1) {
          if (peek_stack(&stack) == JSON_TYPE_ARRAY) {
            putwchar(L'\n');
          }
          else {
            putwchar(ch);
          }
        }
        else {
          putwchar(ch);
        }
      }
      after_backslash = 0;
      break;
    case L'"':
      if (inside_string) {
        if (after_backslash) {
          inside_string = 1;
          after_backslash = 0;
        }
        else {
          inside_string = 0;
        }
      }
      else {
        inside_string = 1;
      }
      putwchar(ch);
      after_backslash = 0;
      break;
    case L'\\':
      if (inside_string) {
        after_backslash = !after_backslash;
      }
      else {
        fatal("backslash outside of string", lineno, offsetno);
      }
      putwchar(ch);
      break;
    case L' ':
      if (inside_string) {
        putwchar(ch);
      }
      after_backslash = 0;
      break;
    case L'\t':
      if (inside_string) {
        fatal("tab in string", lineno, offsetno);
      }
      after_backslash = 0;
      break;
    case L'\n':;
      if (inside_string) {
        fatal("newline in string", lineno, offsetno);
      }
      lineno += 1;
      offsetno = 0;
      after_backslash = 0;
      break;
    default:
      putwchar(ch);
      after_backslash = 0;
      break;
    }
  }
  if (array_depth != 0) {
    fatal("more left brackets than right brackets", lineno - 1, offsetno);
  }
  putwchar(L'\n');
  return 0;
}

variable_types:
ch
unsigned int

variable_types:
lineno
unsigned long

variable_types:
offsetno
unsigned long

variable_types: struct json_type_stack { unsigned long size; unsigned long capacity; _Bool *data; }


fatal

decompiled_code:
void __fastcall __noreturn fatal(const char *a1, __int64 a2, __int64 a3)
{
fprintf(stderr, "ERROR: line: %lu: offset: %lu: %s\n", a2, a3, a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1, __int64 a2, __int64 a3)
{
fprintf(stderr, "ERROR: line: %lu: offset: %lu: %s\n", a2, a3, a1);
exit(1);
}

original_code:
void
fatal(char *msg, size_t lineno, size_t offsetno) {
  fprintf(stderr, "ERROR: line: %lu: offset: %lu: %s\n",
          lineno, offsetno, msg);
  exit(1);
}

canonical_original_code:
void fatal(char *msg, unsigned long lineno, unsigned long offsetno) {
  fprintf(stderr, "ERROR: line: %lu: offset: %lu: %s\n",
          lineno, offsetno, msg);
  exit(1);
}

variable_types:
msg
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
setlocale(6, "");
return json_pluck();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
setlocale(6, "");
return func12();
}

original_code:
int
main(int argc, char **argv) {
  setlocale(6, "");
  return json_pluck();
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
void __fastcall write_wchars(__int64 a1, unsigned __int64 a2, unsigned __int64 a3, char a4, char a5, char a6)
{
unsigned __int64 v6; // rbx
wchar_t *v9; // rbp

v6 = a2;
if ( a4 )
{
if ( a5 )
putwchar(34);
if ( a2 <= a3 )
{
do
{
v9 = (wchar_t *)(a1 + 4 * (v6 & 0xFFF));
if ( *v9 == 34 )
putwchar(34);
if ( putwchar(*v9) == -1 )
{
fwrite("putwchar error", 1uLL, 0xEuLL, stderr);
exit(1);
}
++v6;
}
while ( a3 >= v6 );
}
if ( a6 )
putwchar(34);
}
else if ( a2 <= a3 )
{
do
{
if ( putwchar(*(_DWORD *)(a1 + 4 * (v6 & 0xFFF))) == -1 )
{
fwrite("putwchar error", 1uLL, 0xEuLL, stderr);
exit(1);
}
++v6;
}
while ( a3 >= v6 );
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, unsigned __int64 a2, unsigned __int64 a3, char a4, char a5, char a6)
{
unsigned __int64 v6; // rbx
wchar_t *v9; // rbp

v6 = a2;
if ( a4 )
{
if ( a5 )
putwchar(34);
if ( a2 <= a3 )
{
do
{
v9 = (wchar_t *)(a1 + 4 * (v6 & 0xFFF));
if ( *v9 == 34 )
putwchar(34);
if ( putwchar(*v9) == -1 )
{
fwrite("putwchar error", 1uLL, 0xEuLL, stderr);
exit(1);
}
++v6;
}
while ( a3 >= v6 );
}
if ( a6 )
putwchar(34);
}
else if ( a2 <= a3 )
{
do
{
if ( putwchar(*(_DWORD *)(a1 + 4 * (v6 & 0xFFF))) == -1 )
{
fwrite("putwchar error", 1uLL, 0xEuLL, stderr);
exit(1);
}
++v6;
}
while ( a3 >= v6 );
}
}

original_code:
static inline void
write_wchars(wchar_t buf[], size_t start, size_t end, _Bool escape,
             _Bool open_field, _Bool close_field) {
  size_t j;
  if (escape) {
    if (open_field)
      putwchar(L'"');
    for (j = start; j <= end; ++j) {
      if (buf[(j % 4096)] == L'"')
        putwchar(L'"');
      if (putwchar(buf[(j % 4096)]) == (0xffffffffu)) {
        fprintf(stderr, "putwchar error");
        exit(1);
      }
    }
    if (close_field)
      putwchar(L'"');
  } else {
    for (j = start; j <= end; ++j) {
      if (putwchar(buf[(j % 4096)]) == (0xffffffffu)) {
        fprintf(stderr, "putwchar error");
        exit(1);
      }
    }
  }
}

canonical_original_code:
static inline void write_wchars(int buf[], unsigned long start, unsigned long end, _Bool escape, _Bool open_field, _Bool close_field) {
  unsigned long j;
  if (escape) {
    if (open_field)
      putwchar(L'"');
    for (j = start; j <= end; ++j) {
      if (buf[(j % 4096)] == L'"')
        putwchar(L'"');
      if (putwchar(buf[(j % 4096)]) == (0xffffffffu)) {
        fprintf(stderr, "putwchar error");
        exit(1);
      }
    }
    if (close_field)
      putwchar(L'"');
  } else {
    for (j = start; j <= end; ++j) {
      if (putwchar(buf[(j % 4096)]) == (0xffffffffu)) {
        fprintf(stderr, "putwchar error");
        exit(1);
      }
    }
  }
}

variable_types:
buf
int []

variable_types:
start
unsigned long

variable_types:
end
unsigned long

variable_types:
j
unsigned long


repo: claryaldringen/ZCUChess

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int128 v5; // [rsp+20h] [rbp-28h] BYREF
int v6; // [rsp+30h] [rbp-18h]
char v7[14]; // [rsp+3Ah] [rbp-Eh] BYREF

show_header(argc, argv, envp);
init_chessboard();
while ( !(unsigned __int8)is_checkmate_or_stalemate() )
{
human_move = 1;
gets(v7);
if ( (unsigned __int8)is_exit(v7) )
break;
if ( !(unsigned __int8)is_help(v7) )
{
parse_move((__int64)&v5, v7);
if ( (_BYTE)v6 )
{
play_move(1LL);
if ( (unsigned __int8)is_checkmate_or_stalemate() )
return 0;
human_move = 0;
get_move(&v5);
play_move(1LL);
print_move(1LL, v7, v3);
show_check();
}
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int128 v5; // [rsp+20h] [rbp-28h] BYREF
int v6; // [rsp+30h] [rbp-18h]
char v7[14]; // [rsp+3Ah] [rbp-Eh] BYREF

show_header(argc, argv, envp);
init_chessboard();
while ( !(unsigned __int8)is_checkmate_or_stalemate() )
{
human_move = 1;
gets(v7);
if ( (unsigned __int8)is_exit(v7) )
break;
if ( !(unsigned __int8)is_help(v7) )
{
func0((__int64)&v5, v7);
if ( (_BYTE)v6 )
{
play_move(1LL);
if ( (unsigned __int8)is_checkmate_or_stalemate() )
return 0;
human_move = 0;
get_move(&v5);
play_move(1LL);
print_move(1LL, v7, v3);
show_check();
}
}
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
 char input[6];
 Move move;
 show_header();
 init_chessboard();
 while(!is_checkmate_or_stalemate())
 {
  human_move = 1;
  gets(input);
  if(is_exit(input))break;
  if(is_help(input))continue;
  move = parse_move(input);
  if(move.status == 0)continue;
  play_move(move, 1);
  if(is_checkmate_or_stalemate())break;
  human_move = 0;
  move = get_move();
  play_move(move, 1);
  print_move(move);
  show_check();
 }
 return (0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 char input[6];
 struct Move move;
 show_header();
 init_chessboard();
 while(!is_checkmate_or_stalemate())
 {
  human_move = 1;
  gets(input);
  if(is_exit(input))break;
  if(is_help(input))continue;
  move = parse_move(input);
  if(move.status == 0)continue;
  play_move(move, 1);
  if(is_checkmate_or_stalemate())break;
  human_move = 0;
  move = get_move();
  play_move(move, 1);
  print_move(move);
  show_check();
 }
 return (0);
}

variable_types:
input
char [6]

variable_types: struct Move { int from[2]; int to[2]; _Bool status; }


decompiled_code:
__int64 __fastcall load_position(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, __int128 a8, __int128 a9, __int128 a10, __int128 a11, __int128 a12, __int128 a13, __int128 a14, __int128 a15, __int128 a16, __int128 a17, __int128 a18, __int128 a19, __int128 a20, __int128 a21, __int128 a22, int a23, int a24, __int64 a25)
{
__int64 result; // rax

chessboard[0] = _mm_loadu_si128((const __m128i *)&a7);
chessboard[1] = _mm_loadu_si128((const __m128i *)&a8);
chessboard[2] = _mm_loadu_si128((const __m128i *)&a9);
chessboard[3] = _mm_loadu_si128((const __m128i *)&a10);
chessboard[4] = _mm_loadu_si128((const __m128i *)&a11);
chessboard[5] = _mm_loadu_si128((const __m128i *)&a12);
chessboard[6] = _mm_loadu_si128((const __m128i *)&a13);
chessboard[7] = _mm_loadu_si128((const __m128i *)&a14);
chessboard[8] = _mm_loadu_si128((const __m128i *)&a15);
chessboard[9] = _mm_loadu_si128((const __m128i *)&a16);
chessboard[10] = _mm_loadu_si128((const __m128i *)&a17);
chessboard[11] = _mm_loadu_si128((const __m128i *)&a18);
chessboard[12] = _mm_loadu_si128((const __m128i *)&a19);
chessboard[13] = _mm_loadu_si128((const __m128i *)&a20);
chessboard[14] = _mm_loadu_si128((const __m128i *)&a21);
chessboard[15] = _mm_loadu_si128((const __m128i *)&a22);
castlings = a23;
result = a25;
en_passant = a25;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, __int128 a8, __int128 a9, __int128 a10, __int128 a11, __int128 a12, __int128 a13, __int128 a14, __int128 a15, __int128 a16, __int128 a17, __int128 a18, __int128 a19, __int128 a20, __int128 a21, __int128 a22, int a23, int a24, __int64 a25)
{
__int64 result; // rax

chessboard[0] = _mm_loadu_si128((const __m128i *)&a7);
chessboard[1] = _mm_loadu_si128((const __m128i *)&a8);
chessboard[2] = _mm_loadu_si128((const __m128i *)&a9);
chessboard[3] = _mm_loadu_si128((const __m128i *)&a10);
chessboard[4] = _mm_loadu_si128((const __m128i *)&a11);
chessboard[5] = _mm_loadu_si128((const __m128i *)&a12);
chessboard[6] = _mm_loadu_si128((const __m128i *)&a13);
chessboard[7] = _mm_loadu_si128((const __m128i *)&a14);
chessboard[8] = _mm_loadu_si128((const __m128i *)&a15);
chessboard[9] = _mm_loadu_si128((const __m128i *)&a16);
chessboard[10] = _mm_loadu_si128((const __m128i *)&a17);
chessboard[11] = _mm_loadu_si128((const __m128i *)&a18);
chessboard[12] = _mm_loadu_si128((const __m128i *)&a19);
chessboard[13] = _mm_loadu_si128((const __m128i *)&a20);
chessboard[14] = _mm_loadu_si128((const __m128i *)&a21);
chessboard[15] = _mm_loadu_si128((const __m128i *)&a22);
castlings = a23;
result = a25;
en_passant = a25;
return result;
}

original_code:
void load_position(Position position)
{
 memcpy(chessboard, position.chessboard, sizeof(chessboard));
 memcpy(castlings, position.castlings, sizeof(castlings));
 memcpy(en_passant, position.en_passant, sizeof(en_passant));
}

canonical_original_code:
void load_position(struct Position position)
{
 memcpy(chessboard, position.chessboard, sizeof(chessboard));
 memcpy(castlings, position.castlings, sizeof(castlings));
 memcpy(en_passant, position.en_passant, sizeof(en_passant));
}

variable_types: struct Position { int chessboard[8][8]; int castlings[4]; int en_passant[2]; }


decompiled_code:
void __fastcall free_moves(int a1, void *a2)
{
if ( a1 > 0 )
free(a2);
}

canonical_decompiled_code:
void __fastcall func2(int a1, void *a2)
{
if ( a1 > 0 )
free(a2);
}

original_code:
void free_moves(Moves moves)
{
 if(moves.count > 0)free(moves.move);
}

canonical_original_code:
void free_moves(struct Moves moves)
{
 if(moves.count > 0)free(moves.move);
}

variable_types: struct Moves { int count; struct Move { int from[2]; int to[2]; _Bool status; } *move; }

user_defined_types: struct Moves { int count; struct Move *move; }


decompiled_code:
__int64 get_position_value()
{
__int64 v0; // r12
unsigned int v1; // er13
unsigned int v2; // ebp
__int64 i; // rbx
unsigned int v4; // er14
int v5; // eax

v0 = 0LL;
v1 = 0;
v2 = 0;
while ( 2 )
{
for ( i = 0LL; i != 8; ++i )
{
v4 = abs32(chessboard[i + v0]);
v5 = get_side_coeficient(v1, (unsigned int)i);
if ( v4 == 1 )
{
v2 += -100 * v5;
LABEL_3:
if ( v4 == 4 )
{
v2 += -350 * v5;
goto LABEL_7;
}
goto LABEL_11;
}
if ( v4 != 2 )
{
if ( v4 == 3 )
{
v2 += -300 * v5;
goto LABEL_7;
}
goto LABEL_3;
}
v2 += -400 * v5;
LABEL_11:
if ( v4 == 5 )
v2 += -750 * v5;
LABEL_7:
v2 -= position_bonus[i + v0] * v5;
}
++v1;
v0 += 8LL;
if ( v1 != 8 )
continue;
return v2;
}
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r12
unsigned int v1; // er13
unsigned int v2; // ebp
__int64 i; // rbx
unsigned int v4; // er14
int v5; // eax

v0 = 0LL;
v1 = 0;
v2 = 0;
while ( 2 )
{
for ( i = 0LL; i != 8; ++i )
{
v4 = abs32(chessboard[i + v0]);
v5 = get_side_coeficient(v1, (unsigned int)i);
if ( v4 == 1 )
{
v2 += -100 * v5;
LABEL_3:
if ( v4 == 4 )
{
v2 += -350 * v5;
goto LABEL_7;
}
goto LABEL_11;
}
if ( v4 != 2 )
{
if ( v4 == 3 )
{
v2 += -300 * v5;
goto LABEL_7;
}
goto LABEL_3;
}
v2 += -400 * v5;
LABEL_11:
if ( v4 == 5 )
v2 += -750 * v5;
LABEL_7:
v2 -= position_bonus[i + v0] * v5;
}
++v1;
v0 += 8LL;
if ( v1 != 8 )
continue;
return v2;
}
}

original_code:
int get_position_value()
{
 int value = 0;
 int side = 0;
 int figure;
 for(int col=0; col<8; col++)
 {
  for(int row=0; row<8; row++)
  {
   figure = abs(chessboard[col][row]);
   side = get_side_coeficient(col, row);
   if(figure == 1)value = value - 100*side;
   if(figure == 2)value = value - 400*side;
   if(figure == 3)value = value - 300*side;
   if(figure == 4)value = value - 350*side;
   if(figure == 5)value = value - 750*side;
   value = value - position_bonus[col][row]*side;
  }
 }
 return value;
}

canonical_original_code:
int get_position_value()
{
 int value = 0;
 int side = 0;
 int figure;
 for(int col = 0; col<8; col++)
 {
  for(int row = 0; row<8; row++)
  {
   figure = abs(chessboard[col][row]);
   side = get_side_coeficient(col, row);
   if(figure == 1)value = value - 100*side;
   if(figure == 2)value = value - 400*side;
   if(figure == 3)value = value - 300*side;
   if(figure == 4)value = value - 350*side;
   if(figure == 5)value = value - 750*side;
   value = value - position_bonus[col][row]*side;
  }
 }
 return value;
}

variable_types: value int

variable_types: side int

variable_types: figure int

variable_types: col int

variable_types: row int


decompiled_code:
__int64 __fastcall near_checkmate(int a1)
{
if ( a1 > 15000 )
return (unsigned int)(a1 + 1);
if ( a1 >= -15000 )
return (unsigned int)a1;
return (unsigned int)(a1 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
if ( a1 > 15000 )
return (unsigned int)(a1 + 1);
if ( a1 >= -15000 )
return (unsigned int)a1;
return (unsigned int)(a1 - 1);
}

original_code:
int near_checkmate(int value)
{
 if(value > 15000)value++;
 if(value < -15000)value--;
 return value;
}


decompiled_code:
__int64 __fastcall far_checkmate(int a1)
{
if ( a1 > 15000 )
return (unsigned int)(a1 - 1);
if ( a1 >= -15000 )
return (unsigned int)a1;
return (unsigned int)(a1 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
if ( a1 > 15000 )
return (unsigned int)(a1 - 1);
if ( a1 >= -15000 )
return (unsigned int)a1;
return (unsigned int)(a1 + 1);
}

original_code:
int far_checkmate(int value)
{
 if(value > 15000)value--;
 if(value < -15000)value++;
 return value;
}


alphabeta

decompiled_code:
__int64 __fastcall alphabeta(unsigned int a1, int a2, int a3, int a4)
{
__int64 v6; // rax
__int64 v7; // rdx
int v8; // ebx
__int64 v9; // r15
__int64 v10; // rbx
unsigned int v12; // er12
unsigned int v13; // eax
int v14; // eax
int v15; // er12
__int128 v16[18]; // [rsp-100h] [rbp-298h] BYREF
__int64 v17; // [rsp+28h] [rbp-170h]
unsigned int v18; // [rsp+34h] [rbp-164h]
__int64 v19; // [rsp+38h] [rbp-160h]
char v20[344]; // [rsp+40h] [rbp-158h] BYREF

if ( (unsigned __int8)is_checkmate() )
return 16000 * a1;
if ( a2 <= 0 )
return get_position_value();
v6 = get_all_moves(a1);
v17 = v7;
v8 = v6;
v19 = v6;
save_position(v20);
if ( v8 > 0 )
{
v9 = 20LL * (unsigned int)v8;
v10 = 0LL;
v18 = a2 - 1;
do
{
v16[16] = (__int128)_mm_loadu_si128((const __m128i *)(v10 + v17));
LODWORD(v16[17]) = *(_DWORD *)(v10 + v17 + 16);
play_move(1LL);
if ( (unsigned int)is_check(a1) )
{
qmemcpy(v16, v20, 0x118uLL);
load_position();
}
else
{
v12 = near_checkmate(a3);
v13 = near_checkmate(a4);
v14 = alphabeta(-a1, v18, v13, v12);
v15 = far_checkmate(v14);
qmemcpy(v16, v20, 0x118uLL);
load_position();
if ( a1 == 1 && v15 < a3 )
{
if ( a4 >= v15 )
goto LABEL_18;
a3 = v15;
}
else if ( a1 == -1 && v15 > a3 )
{
if ( a4 <= v15 )
{
LABEL_18:
free_moves((unsigned int)v19, v17);
return (unsigned int)a4;
}
a3 = v15;
}
}
v10 += 20LL;
}
while ( v9 != v10 );
}
free_moves((unsigned int)v19, v17);
return (unsigned int)a3;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, int a2, int a3, int a4)
{
__int64 v6; // rax
__int64 v7; // rdx
int v8; // ebx
__int64 v9; // r15
__int64 v10; // rbx
unsigned int v12; // er12
unsigned int v13; // eax
int v14; // eax
int v15; // er12
__int128 v16[18]; // [rsp-100h] [rbp-298h] BYREF
__int64 v17; // [rsp+28h] [rbp-170h]
unsigned int v18; // [rsp+34h] [rbp-164h]
__int64 v19; // [rsp+38h] [rbp-160h]
char v20[344]; // [rsp+40h] [rbp-158h] BYREF

if ( (unsigned __int8)is_checkmate() )
return 16000 * a1;
if ( a2 <= 0 )
return func0();
v6 = get_all_moves(a1);
v17 = v7;
v8 = v6;
v19 = v6;
save_position(v20);
if ( v8 > 0 )
{
v9 = 20LL * (unsigned int)v8;
v10 = 0LL;
v18 = a2 - 1;
do
{
v16[16] = (__int128)_mm_loadu_si128((const __m128i *)(v10 + v17));
LODWORD(v16[17]) = *(_DWORD *)(v10 + v17 + 16);
play_move(1LL);
if ( (unsigned int)is_check(a1) )
{
qmemcpy(v16, v20, 0x118uLL);
load_position();
}
else
{
v12 = func1(a3);
v13 = func1(a4);
v14 = func3(-a1, v18, v13, v12);
v15 = func2(v14);
qmemcpy(v16, v20, 0x118uLL);
load_position();
if ( a1 == 1 && v15 < a3 )
{
if ( a4 >= v15 )
goto LABEL_18;
a3 = v15;
}
else if ( a1 == -1 && v15 > a3 )
{
if ( a4 <= v15 )
{
LABEL_18:
free_moves((unsigned int)v19, v17);
return (unsigned int)a4;
}
a3 = v15;
}
}
v10 += 20LL;
}
while ( v9 != v10 );
}
free_moves((unsigned int)v19, v17);
return (unsigned int)a3;
}

original_code:
int alphabeta(int side, int depth, int alpha, int beta)
{
 Moves moves;
 Position position;
 int value;
 if(is_checkmate())return side*16000;
 if(depth <= 0)return get_position_value();
 moves = get_all_moves(side);
 position = save_position();
 for(int i=0; i<moves.count; i++)
 {
  play_move(moves.move[i], 1);
  if(is_check(side) != 0)
  {
   load_position(position);
   continue;
  }
  value = far_checkmate(alphabeta(side*(-1), depth-1, near_checkmate(beta), near_checkmate(alpha)));
  load_position(position);
  if((side == 1 && value < alpha) || (side == -1 && value > alpha))
  {
   alpha = value;
   if((side == 1 && value <= beta) || (side == -1 && value >= beta))
   {
    free_moves(moves);
    return beta;
   }
  }
 }
 free_moves(moves);
 return alpha;
}

canonical_original_code:
int alphabeta(int side, int depth, int alpha, int beta)
{
 struct Moves moves;
 struct Position position;
 int value;
 if(is_checkmate())return side*16000;
 if(depth <= 0)return get_position_value();
 moves = get_all_moves(side);
 position = save_position();
 for(int i = 0; i<moves.count; i++)
 {
  play_move(moves.move[i], 1);
  if(is_check(side) != 0)
  {
   load_position(position);
   continue;
  }
  value = far_checkmate(alphabeta(side*(-1), depth-1, near_checkmate(beta), near_checkmate(alpha)));
  load_position(position);
  if((side == 1 && value < alpha) || (side == -1 && value > alpha))
  {
   alpha = value;
   if((side == 1 && value <= beta) || (side == -1 && value >= beta))
   {
    free_moves(moves);
    return beta;
   }
  }
 }
 free_moves(moves);
 return alpha;
}

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: i int

user_defined_types: struct Position { int chessboard[8][8]; int castlings[4]; int en_passant[2]; }

user_defined_types: struct Move { int from[2]; int to[2]; _Bool status; }


decompiled_code:
__int64 __fastcall is_exit(const char *a1)
{
if ( strcmp(a1, "exit") )
return 0LL;
puts("Nashledanou");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
if ( strcmp(a1, "exit") )
return 0LL;
puts("Nashledanou");
return 1LL;
}

original_code:
_Bool is_exit(char* input)
{
 if(strcmp(input, "exit") == 0)
 {
  printf("Nashledanou\n");
  return 1;
 }
 return 0;
}

canonical_original_code:
_Bool is_exit(char *input)
{
 if(strcmp(input, "exit") == 0)
 {
  printf("Nashledanou\n");
  return 1;
 }
 return 0;
}

variable_types:
input
char *


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

result = (unsigned int)(a1 - 97);
if ( (unsigned int)result > 7 )
result = 0xFFFFFFFFLL;
return result;
}

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

result = (unsigned int)(a1 - 97);
if ( (unsigned int)result > 7 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int translate_col(char character)
{
 int number = (int)(character - 97);
 if(number > 7 || number < 0)number = -1;
 return number;
}

variable_types: character char

variable_types: number int


decompiled_code:
int show_chessboard()
{
unsigned int *v0; // rbp
unsigned int *v1; // rbx
int result; // eax

v0 = (unsigned int *)(unsigned int)((_DWORD)&chessboard + 32);
do
{
v1 = v0 - 8;
do
printf("%d ", *v1++);
while ( v1 != v0 );
result = putchar(10);
v0 += 8;
}
while ( v0 != (unsigned int *)(unsigned int)((_DWORD)&chessboard + 288) );
return result;
}

canonical_decompiled_code:
int func2()
{
unsigned int *v0; // rbp
unsigned int *v1; // rbx
int result; // eax

v0 = (unsigned int *)(unsigned int)((_DWORD)&chessboard + 32);
do
{
v1 = v0 - 8;
do
printf("%d ", *v1++);
while ( v1 != v0 );
result = putchar(10);
v0 += 8;
}
while ( v0 != (unsigned int *)(unsigned int)((_DWORD)&chessboard + 288) );
return result;
}

original_code:
void show_chessboard()
{
 for(int col = 0; col < 8; col++)
 {
  for(int row = 0; row < 8; row++)
  {
   printf("%d ", chessboard[col][row]);
  }
  printf("\n");
 }
}


decompiled_code:
int __fastcall print_move(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8)
{
return printf(
"%c%d %c%d \n",
(unsigned int)(a7 + 97),
(unsigned int)(HIDWORD(a7) + 1),
(unsigned int)(a8 + 97),
(unsigned int)(HIDWORD(a8) + 1));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8)
{
return printf(
"%c%d %c%d \n",
(unsigned int)(a7 + 97),
(unsigned int)(HIDWORD(a7) + 1),
(unsigned int)(a8 + 97),
(unsigned int)(HIDWORD(a8) + 1));
}

original_code:
void print_move(Move move)
{
 printf("%c%d %c%d         \n", move.from[0]+97, move.from[1]+1, move.to[0]+97, move.to[1]+1);
}

canonical_original_code:
void print_move(struct Move move)
{
 printf("%c%d %c%d         \n", move.from[0]+97, move.from[1]+1, move.to[0]+97, move.to[1]+1);
}


decompiled_code:
int show_help()
{
return puts(
"Zadani tahu: <odkud> <kam> (napr.: e2 e3)\n"
"Mala rosada: e1 h1\n"
"Velka rosada: e1 a1\n"
"Konec programu: exit\n"
"Napoveda: help");
}

canonical_decompiled_code:
int func4()
{
return puts(
"Zadani tahu: <odkud> <kam> (napr.: e2 e3)\n"
"Mala rosada: e1 h1\n"
"Velka rosada: e1 a1\n"
"Konec programu: exit\n"
"Napoveda: help");
}

original_code:
void show_help()
{
 printf("Zadani tahu: <odkud> <kam> (napr.: e2 e3)\nMala rosada: e1 h1\nVelka rosada: e1 a1\nKonec programu: exit\nNapoveda: help\n");
}


decompiled_code:
__int64 __fastcall is_help(const char *a1)
{
if ( strcmp(a1, "help") )
return 0LL;
show_help();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
if ( strcmp(a1, "help") )
return 0LL;
func4();
return 1LL;
}

original_code:
_Bool is_help(char* input)
{
 if(strcmp(input, "help") == 0)
 {
  show_help();
  return 1;
 }
 return 0;
}

canonical_original_code:
_Bool is_help(char *input)
{
 if(strcmp(input, "help") == 0)
 {
  show_help();
  return 1;
 }
 return 0;
}


decompiled_code:
int show_header()
{
puts(
"ZCU Chess\n"
"\n"
"Jednoduche sachy, vytvorene pod licenci GNU/GPL jako semestralni prace do predmetu KIV/PC\n"
"Autor: Martin Zadrazil <zadram1@gmail.com> v roce 2012\n"
"Zdrojovy kod: https://github.com/ClaryAldringen/ZCUChess\n");
return show_help();
}

canonical_decompiled_code:
int func6()
{
puts(
"ZCU Chess\n"
"\n"
"Jednoduche sachy, vytvorene pod licenci GNU/GPL jako semestralni prace do predmetu KIV/PC\n"
"Autor: Martin Zadrazil <zadram1@gmail.com> v roce 2012\n"
"Zdrojovy kod: https://github.com/ClaryAldringen/ZCUChess\n");
return func4();
}

original_code:
void show_header()
{
 printf("ZCU Chess\n\nJednoduche sachy, vytvorene pod licenci GNU/GPL jako semestralni prace do predmetu KIV/PC\nAutor: Martin Zadrazil <zadram1@gmail.com> v roce 2012\nZdrojovy kod: https://github.com/ClaryAldringen/ZCUChess\n\n");
 show_help();
}


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

if ( (unsigned int)is_check(0xFFFFFFFFLL) )
puts("Sach bily");
result = is_check(1LL);
if ( result )
result = puts("Sach cerny");
return result;
}

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

if ( (unsigned int)is_check(0xFFFFFFFFLL) )
puts("Sach bily");
result = is_check(1LL);
if ( result )
result = puts("Sach cerny");
return result;
}

original_code:
void show_check()
{
 if(is_check(-1))printf("Sach bily\n");
 if(is_check(1))printf("Sach cerny\n");
}


decompiled_code:
int show_thinking()
{
return printf("Premyslim...\r");
}

canonical_decompiled_code:
int func8()
{
return printf("Premyslim...\r");
}

original_code:
void show_thinking()
{
 printf("Premyslim...\r");
}


usage

repo: claudiughioc/KTracer

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
printf(
"Usage: %s <options>\n options:\n\tp - print\n\ts string - set buffer\n\tg - get buffer\n\td - down\n\tu - up\n",
a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
printf(
"Usage: %s <options>\n options:\n\tp - print\n\ts string - set buffer\n\tg - get buffer\n\td - down\n\tu - up\n",
a1);
exit(1);
}

original_code:
static void usage(const char *argv0)
{
 printf("Usage: %s <options>\n options:\n"
   "\tp - print\n"
   "\ts string - set buffer\n"
   "\tg - get buffer\n"
   "\td - down\n"
   "\tu - up\n", argv0);
 exit(1);
}

canonical_original_code:
static void usage(char *argv0)
{
 printf("Usage: %s <options>\n options:\n"
   "\tp - print\n"
   "\ts string - set buffer\n"
   "\tg - get buffer\n"
   "\td - down\n"
   "\tu - up\n", argv0);
 exit(1);
}


error

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

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

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

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

variable_types:
message
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v4; // ebp
char v5; // al
__int64 v6; // rbx
__int64 v7; // rbx

if ( argc <= 2 )
usage(*argv);
v3 = atoi(argv[2]);
if ( strlen(argv[1]) != 1 )
usage(*argv);
v4 = open("/dev/tracer", 0);
v5 = *argv[1];
if ( v5 == 97 )
{
printf("Add to trace process %d\n", v3);
v7 = (unsigned int)ioctl(v4, 0x4004012AuLL, v3);
printf("Ioctl code %d\n", v7);
if ( (_DWORD)v7 )
error("Error on ioctl\n");
}
else
{
if ( v5 != 114 )
error("Wrong parameter");
printf("Remove from trace process %d\n", v3);
v6 = (unsigned int)ioctl(v4, 0x4004012BuLL, v3);
printf("Ioctl code %d\n", v6);
if ( (_DWORD)v6 )
error("Error on ioctl\n");
}
close(v4);
return 0;
}

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

if ( argc <= 2 )
func7(*argv);
v3 = atoi(argv[2]);
if ( strlen(argv[1]) != 1 )
func7(*argv);
v4 = open("/dev/tracer", 0);
v5 = *argv[1];
if ( v5 == 97 )
{
printf("Add to trace process %d\n", v3);
v7 = (unsigned int)ioctl(v4, 0x4004012AuLL, v3);
printf("Ioctl code %d\n", v7);
if ( (_DWORD)v7 )
func8("Error on ioctl\n");
}
else
{
if ( v5 != 114 )
func8("Wrong parameter");
printf("Remove from trace process %d\n", v3);
v6 = (unsigned int)ioctl(v4, 0x4004012BuLL, v3);
printf("Ioctl code %d\n", v6);
if ( (_DWORD)v6 )
func8("Error on ioctl\n");
}
close(v4);
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int fd, ret;
 pid_t pid;
 if (argc < 3)
  usage(argv[0]);
 pid = (int)atoi(argv[2]);
 if (strlen(argv[1]) != 1)
  usage(argv[0]);
 fd = open("/dev/tracer", 00);
 switch (argv[1][0]) {
 case 'a':
  printf("Add to trace process %d\n", pid);
  ret = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | (((1U)) << (0 +8)) | (((42)) << 0) | ((((sizeof(pid_t)))) << ((0 +8)+8))), pid);
  printf("Ioctl code %d\n", ret);
  if(ret)
   error("Error on ioctl\n");
  break;
 case 'r':
  printf("Remove from trace process %d\n", pid);
  ret = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | (((1U)) << (0 +8)) | (((43)) << 0) | ((((sizeof(pid_t)))) << ((0 +8)+8))), pid);
  printf("Ioctl code %d\n", ret);
  if(ret)
   error("Error on ioctl\n");
  break;
 default:
  error("Wrong parameter");
 }
 close(fd);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int fd; int ret;
 int pid;
 if (argc < 3)
  usage(argv[0]);
 pid = (int)atoi(argv[2]);
 if (strlen(argv[1]) != 1)
  usage(argv[0]);
 fd = open("/dev/tracer", 00);
 switch (argv[1][0]) {
 case 'a':
  printf("Add to trace process %d\n", pid);
  ret = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | (((1U)) << (0 +8)) | (((42)) << 0) | ((((sizeof(pid_t)))) << ((0 +8)+8))), pid);
  printf("Ioctl code %d\n", ret);
  if(ret)
   error("Error on ioctl\n");
  break;
 case 'r':
  printf("Remove from trace process %d\n", pid);
  ret = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | (((1U)) << (0 +8)) | (((43)) << 0) | ((((sizeof(pid_t)))) << ((0 +8)+8))), pid);
  printf("Ioctl code %d\n", ret);
  if(ret)
   error("Error on ioctl\n");
  break;
 default:
  error("Wrong parameter");
 }
 close(fd);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: fd int

variable_types: ret int

variable_types: pid int


repo: claudyus/picobgp

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edi
unsigned int v4; // eax
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-18Ch] BYREF
struct sockaddr v7; // [rsp+10h] [rbp-188h] BYREF
__int64 buf; // [rsp+20h] [rbp-178h] BYREF
__int64 v9; // [rsp+28h] [rbp-170h]
fd_set readfds; // [rsp+40h] [rbp-158h] BYREF
int optval; // [rsp+CCh] [rbp-CCh] BYREF
struct sockaddr addr; // [rsp+D0h] [rbp-C8h] BYREF
struct sigaction v13; // [rsp+E0h] [rbp-B8h] BYREF

sd = socket(2, 2, 0);
if ( sd == -1 )
{
perror("socket(SOCK_DGRAM)");
exit(1);
}
parse_opt((unsigned int)argc, argv);
memset(&v13.sa_mask, 0, 0x90uLL);
v13.sa_handler = (__sighandler_t)&signal_handler;
sigaction(2, &v13, 0LL);
sigaction(10, &v13, 0LL);
sigaction(14, &v13, 0LL);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(9876);
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
optval = 1;
if ( setsockopt(sd, 1, 6, &optval, 4u) == -1 )
{
perror("setsockopt(SO_BROADCAST)");
exit(1);
}
*(_DWORD *)&addr.sa_data[2] = brd_ip;
if ( bind(sd, &addr, 0x10u) < 0 )
{
perror("bind(&server_sock)");
result = 1;
}
else
{
alarm(3u);
memset(&readfds, 0, sizeof(readfds));
while ( !one_shot )
{
v3 = sd;
readfds.fds_bits[sd / 64] |= 1LL << (sd % 64);
if ( select(v3 + 1, &readfds, 0LL, 0LL, 0LL) != -1 )
{
addr_len = 16;
v4 = recvfrom(sd, &buf, 0x18uLL, 0, &v7, &addr_len);
if ( v4 == -1 )
{
perror("read(&msg)");
exit(1);
}
if ( (_DWORD)v9 != loc_ip )
update_rt(v4);
}
}
let_me_spam();
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edi
unsigned int v4; // eax
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-18Ch] BYREF
struct sockaddr v7; // [rsp+10h] [rbp-188h] BYREF
__int64 buf; // [rsp+20h] [rbp-178h] BYREF
__int64 v9; // [rsp+28h] [rbp-170h]
fd_set readfds; // [rsp+40h] [rbp-158h] BYREF
int optval; // [rsp+CCh] [rbp-CCh] BYREF
struct sockaddr addr; // [rsp+D0h] [rbp-C8h] BYREF
struct sigaction v13; // [rsp+E0h] [rbp-B8h] BYREF

sd = socket(2, 2, 0);
if ( sd == -1 )
{
perror("socket(SOCK_DGRAM)");
exit(1);
}
parse_opt((unsigned int)argc, argv);
memset(&v13.sa_mask, 0, 0x90uLL);
v13.sa_handler = (__sighandler_t)&signal_handler;
sigaction(2, &v13, 0LL);
sigaction(10, &v13, 0LL);
sigaction(14, &v13, 0LL);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(9876);
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func1(0);
optval = 1;
if ( setsockopt(sd, 1, 6, &optval, 4u) == -1 )
{
perror("setsockopt(SO_BROADCAST)");
exit(1);
}
*(_DWORD *)&addr.sa_data[2] = brd_ip;
if ( bind(sd, &addr, 0x10u) < 0 )
{
perror("bind(&server_sock)");
result = 1;
}
else
{
alarm(3u);
memset(&readfds, 0, sizeof(readfds));
while ( !one_shot )
{
v3 = sd;
readfds.fds_bits[sd / 64] |= 1LL << (sd % 64);
if ( select(v3 + 1, &readfds, 0LL, 0LL, 0LL) != -1 )
{
addr_len = 16;
v4 = recvfrom(sd, &buf, 0x18uLL, 0, &v7, &addr_len);
if ( v4 == -1 )
{
perror("read(&msg)");
exit(1);
}
if ( (_DWORD)v9 != loc_ip )
update_rt(v4);
}
}
let_me_spam();
result = 0;
}
return result;
}

original_code:
int main (int argc, char *argv[]) {
 sd = socket(2, SOCK_DGRAM, 0);
 if (sd == -1) {
  perror("socket(SOCK_DGRAM)");
  exit(1);
 }
 parse_opt(argc, argv);
 struct sigaction handler; memset(&handler, 0, sizeof(struct sigaction));
 handler.__sigaction_handler.sa_handler = signal_handler;
 sigaction(2, &handler, 0);
 sigaction(10, &handler, 0);
 sigaction(14, &handler, 0);
 struct sockaddr_in server_sock; memset(&server_sock, 0, sizeof(struct sockaddr_in));;
 server_sock.sin_family = 2;
 server_sock.sin_port = htons(9876);
 server_sock.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 int broadcast = 1;
 if (setsockopt(sd, 1, 6, &broadcast,
  sizeof(int)) == -1) {
  perror("setsockopt(SO_BROADCAST)");
  exit(1);
 }
 server_sock.sin_addr.s_addr = brd_ip.s_addr;
 if ( bind(sd, (struct sockaddr *) &server_sock, sizeof(server_sock)) < 0 ) {
  perror("bind(&server_sock)");
  return 1;
 }
 alarm(3);
 fd_set fds;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 for (;!one_shot;) {
  ((void) (((&fds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  int r = select (sd+1, &fds, ((void *)0), ((void *)0), ((void *)0));
  if (r != -1) {
   bgpmsg msg;
   struct sockaddr_in loc;
   int locl = sizeof(struct sockaddr_in);
   if ((r = recvfrom (sd, &msg, sizeof(bgpmsg), 0, (struct sockaddr *)&loc, &locl)) == -1) {
    perror("read(&msg)");
    exit(1);
   }
   if (msg.loc_addr.s_addr != loc_ip.s_addr)
    update_rt(msg, r);
  }
 }
 let_me_spam();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 sd = socket(2, SOCK_DGRAM, 0);
 if (sd == -1) {
  perror("socket(SOCK_DGRAM)");
  exit(1);
 }
 parse_opt(argc, argv);
 struct sigaction handler; memset(&handler, 0, sizeof(struct sigaction));
 handler.__sigaction_handler.sa_handler = signal_handler;
 sigaction(2, &handler, 0);
 sigaction(10, &handler, 0);
 sigaction(14, &handler, 0);
 struct sockaddr_in server_sock; memset(&server_sock, 0, sizeof(struct sockaddr_in));;
 server_sock.sin_family = 2;
 server_sock.sin_port = htons(9876);
 server_sock.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 int broadcast = 1;
 if (setsockopt(sd, 1, 6, &broadcast,
  sizeof(int)) == -1) {
  perror("setsockopt(SO_BROADCAST)");
  exit(1);
 }
 server_sock.sin_addr.s_addr = brd_ip.s_addr;
 if ( bind(sd, (struct sockaddr *) &server_sock, sizeof(server_sock)) < 0 ) {
  perror("bind(&server_sock)");
  return 1;
 }
 alarm(3);
 struct fd_set fds;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 for (;!one_shot;) {
  ((void) (((&fds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  int r = select (sd+1, &fds, ((void *)0), ((void *)0), ((void *)0));
  if (r != -1) {
   struct sbgpmsg msg;
   struct sockaddr_in loc;
   int locl = sizeof(struct sockaddr_in);
   if ((r = recvfrom (sd, &msg, sizeof(bgpmsg), 0, (struct sockaddr *)&loc, &locl)) == -1) {
    perror("read(&msg)");
    exit(1);
   }
   if (msg.loc_addr.s_addr != loc_ip.s_addr)
    update_rt(msg, r);
  }
 }
 let_me_spam();
 return 0;
}

variable_types:
argv
char *[]

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

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: broadcast int

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

variable_types: r int

variable_types: struct sbgpmsg { struct in_addr { unsigned int s_addr; } addr; struct in_addr netmask; struct in_addr loc_addr; long l_update; }

variable_types: locl int

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

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

user_defined_types: struct sbgpmsg { struct in_addr addr; struct in_addr netmask; struct in_addr loc_addr; long l_update; }

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

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


decompiled_code:
void *__fastcall annips_del(int a1)
{
__m128i *v1; // rax
const __m128i *v2; // roff
void *result; // rax

v1 = (__m128i *)((char *)ann_ips + 24 * ann_len - 24);
v2 = (const __m128i *)((char *)ann_ips + 24 * a1);
*v1 = _mm_loadu_si128(v2);
v1[1].m128i_i64[0] = v2[1].m128i_i64[0];
result = realloc(ann_ips, 24LL * (ann_len - 1));
ann_ips = result;
--ann_len;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(int a1)
{
__m128i *v1; // rax
const __m128i *v2; // roff
void *result; // rax

v1 = (__m128i *)((char *)ann_ips + 24 * ann_len - 24);
v2 = (const __m128i *)((char *)ann_ips + 24 * a1);
*v1 = _mm_loadu_si128(v2);
v1[1].m128i_i64[0] = v2[1].m128i_i64[0];
result = realloc(ann_ips, 24LL * (ann_len - 1));
ann_ips = result;
--ann_len;
return result;
}

original_code:
void annips_del(int id){
 memcpy(&ann_ips[ann_len-1], &ann_ips[id], sizeof(bgpmsg));
 ann_ips = realloc(ann_ips, sizeof(bgpmsg)*(ann_len-1) );
 ann_len --;
}

variable_types: id int


decompiled_code:
__int64 get_if_info()
{
__int64 result; // rax
char v1[20]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v2; // [rsp+14h] [rbp-24h]

strncpy(v1, local_ifname, 0xFuLL);
if ( ioctl(sd, 0x8915uLL, v1) == -1 )
{
perror("ioctl(SIOCGIFADDR)");
exit(1);
}
loc_ip = v2;
ioctl(sd, 0x8919uLL, v1);
result = v2;
brd_ip = v2;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
char v1[20]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v2; // [rsp+14h] [rbp-24h]

strncpy(v1, local_ifname, 0xFuLL);
if ( ioctl(sd, 0x8915uLL, v1) == -1 )
{
perror("ioctl(SIOCGIFADDR)");
exit(1);
}
loc_ip = v2;
ioctl(sd, 0x8919uLL, v1);
result = v2;
brd_ip = v2;
return result;
}

original_code:
void get_if_info(void) {
 struct ifreq ifr;
 strncpy(ifr.ifr_ifrn.ifrn_name, local_ifname, 16 -1);
 if (ioctl(sd, 0x8915, &ifr) == -1) {
  perror("ioctl(SIOCGIFADDR)");
  exit(1);
 }
 loc_ip.s_addr = (*(struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr).sin_addr.s_addr;
 ioctl(sd, 0x8919, &ifr);
 brd_ip.s_addr = (*(struct sockaddr_in *)&ifr.ifr_ifru.ifru_broadaddr).sin_addr.s_addr;
}

canonical_original_code:
void get_if_info() {
 struct ifreq ifr;
 strncpy(ifr.ifr_ifrn.ifrn_name, local_ifname, 16 -1);
 if (ioctl(sd, 0x8915, &ifr) == -1) {
  perror("ioctl(SIOCGIFADDR)");
  exit(1);
 }
 loc_ip.s_addr = (*(struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr).sin_addr.s_addr;
 ioctl(sd, 0x8919, &ifr);
 brd_ip.s_addr = (*(struct sockaddr_in *)&ifr.ifr_ifru.ifru_broadaddr).sin_addr.s_addr;
}

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


decompiled_code:
int __fastcall add_ips(char *cp)
{
char *v1; // rax
const char *v2; // rbx
char v3; // al
__m128i *v4; // rax
char *v5; // rax
int result; // eax
struct in_addr in[4]; // [rsp+0h] [rbp-38h] BYREF
__int64 v8; // [rsp+10h] [rbp-28h]

v1 = index(cp, 47);
*v1 = 0;
v2 = v1 + 1;
v3 = atoi(v1 + 1);
in[1].s_addr = _bswap_32(-1 << (32 - v3));
inet_aton(cp, in);
if ( !loc_ip )
get_if_info();
in[2].s_addr = loc_ip;
if ( (in[0].s_addr & ~in[1].s_addr) != 0 )
{
printf("skip the subnet %s/%s conflicts!\n", cp, v2);
result = 0;
}
else
{
ips = realloc(ips, 24LL * (ips_len + 1));
v4 = (__m128i *)((char *)ips + 24 * ips_len);
*v4 = _mm_loadu_si128((const __m128i *)in);
v4[1].m128i_i64[0] = v8;
++ips_len;
v5 = inet_ntoa(in[0]);
result = fprintf(stderr, "add ip to ips %s\n", v5);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(char *cp)
{
char *v1; // rax
const char *v2; // rbx
char v3; // al
__m128i *v4; // rax
char *v5; // rax
int result; // eax
struct in_addr in[4]; // [rsp+0h] [rbp-38h] BYREF
__int64 v8; // [rsp+10h] [rbp-28h]

v1 = index(cp, 47);
*v1 = 0;
v2 = v1 + 1;
v3 = atoi(v1 + 1);
in[1].s_addr = func0(-1 << (32 - v3));
inet_aton(cp, in);
if ( !loc_ip )
func2();
in[2].s_addr = loc_ip;
if ( (in[0].s_addr & ~in[1].s_addr) != 0 )
{
printf("skip the subnet %s/%s conflicts!\n", cp, v2);
result = 0;
}
else
{
ips = realloc(ips, 24LL * (ips_len + 1));
v4 = (__m128i *)((char *)ips + 24 * ips_len);
*v4 = _mm_loadu_si128((const __m128i *)in);
v4[1].m128i_i64[0] = v8;
++ips_len;
v5 = inet_ntoa(in[0]);
result = fprintf(stderr, "add ip to ips %s\n", v5);
}
return result;
}

original_code:
int add_ips(char *cidr) {
 char *slash = index(cidr, '/');
 *slash = '\0';
 bgpmsg n_ips;
 n_ips.netmask.s_addr =htonl(0xffffffff << (32-atoi(slash+1)));
 inet_aton(cidr, &(n_ips.addr));
 if (loc_ip.s_addr == 0)
  get_if_info();
 n_ips.loc_addr.s_addr = loc_ip.s_addr;
 if (n_ips.addr.s_addr & ~n_ips.netmask.s_addr) {
  printf ("skip the subnet %s/%s conflicts!\n", cidr, slash+1);
  return 0;
 }
 ips = realloc(ips, sizeof(bgpmsg)*(ips_len+1) );
 memcpy(&ips[ips_len], &n_ips, sizeof(bgpmsg));
 ips_len++;
 fprintf(stderr, "add ip to ips %s\n", inet_ntoa(n_ips.addr));
}

canonical_original_code:
int add_ips(char *cidr) {
 char *slash = index(cidr, '/');
 *slash = '\0';
 struct sbgpmsg n_ips;
 n_ips.netmask.s_addr =htonl(0xffffffff << (32-atoi(slash+1)));
 inet_aton(cidr, &(n_ips.addr));
 if (loc_ip.s_addr == 0)
  get_if_info();
 n_ips.loc_addr.s_addr = loc_ip.s_addr;
 if (n_ips.addr.s_addr & ~n_ips.netmask.s_addr) {
  printf ("skip the subnet %s/%s conflicts!\n", cidr, slash+1);
  return 0;
 }
 ips = realloc(ips, sizeof(bgpmsg)*(ips_len+1) );
 memcpy(&ips[ips_len], &n_ips, sizeof(bgpmsg));
 ips_len++;
 fprintf(stderr, "add ip to ips %s\n", inet_ntoa(n_ips.addr));
}

variable_types:
cidr
char *

variable_types:
slash
char *