emitPreDefCode

decompiled_code:
__int64 emitPreDefCode()
{
emitBlankLine();
emitSepLine();
emitBlankLine();
emitComment("Pre-defined ASC functions to follow");
emitBlankLine();
emitSepLine();
emitBlankLine();
return emitPreDefAsc();
}

canonical_decompiled_code:
__int64 func0()
{
emitBlankLine();
emitSepLine();
emitBlankLine();
emitComment("Pre-defined ASC functions to follow");
emitBlankLine();
emitSepLine();
emitBlankLine();
return emitPreDefAsc();
}

original_code:
void emitPreDefCode() {
 emitBlankLine();
 emitSepLine();
 emitBlankLine();
 emitComment("Pre-defined ASC functions to follow");
 emitBlankLine();
 emitSepLine();
 emitBlankLine();
 emitPreDefAsc();
}


emitInit

repo: a-krebs/YACC

decompiled_code:
__int64 emitInit()
{
emitComment("ASC code generated by Yet Another CMPUT415 Compiler.");
emitComment("May the Force be ever in your favour, Mr. Potter.");
emitComment("CMPUT 415 - Fall 2013");
return emitBlankLine();
}

canonical_decompiled_code:
__int64 func2()
{
emitComment("ASC code generated by Yet Another CMPUT415 Compiler.");
emitComment("May the Force be ever in your favour, Mr. Potter.");
emitComment("CMPUT 415 - Fall 2013");
return emitBlankLine();
}

original_code:
void emitInit() {
 emitComment("ASC code generated by Yet Another CMPUT415 Compiler.");
 emitComment("May the Force be ever in your favour, Mr. Potter.");
 emitComment("CMPUT 415 - Fall 2013");
 emitBlankLine();
}

return_type: void


forkAndRun

decompiled_code:
__int64 __fastcall forkAndRun(__int64 a1)
{
__pid_t v1; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc = 0;
v1 = fork();
if ( !v1 )
{
execlp(ASC_FEXE_NAME, ASC_FEXE_NAME, a1, 0LL);
exit(1);
}
waitpid(v1, &stat_loc, 0);
result = (unsigned int)stat_loc;
if ( (stat_loc & 0x7F) == 0 )
{
if ( BYTE1(stat_loc) )
{
fwrite(
"Failed to launch ASC interpreter, or ASC interpreter returned with error status.\n",
1uLL,
0x51uLL,
stderr);
exit(1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__pid_t v1; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc = 0;
v1 = fork();
if ( !v1 )
{
execlp(ASC_FEXE_NAME, ASC_FEXE_NAME, a1, 0LL);
exit(1);
}
waitpid(v1, &stat_loc, 0);
result = (unsigned int)stat_loc;
if ( (stat_loc & 0x7F) == 0 )
{
if ( BYTE1(stat_loc) )
{
fwrite(
"Failed to launch ASC interpreter, or ASC interpreter returned with error status.\n",
1uLL,
0x51uLL,
stderr);
exit(1);
}
}
return result;
}

original_code:
static void forkAndRun(char *ascFileName)
{
 int status = 0;
 pid_t pid;
 pid = fork();
 if (pid == 0) {
  execlp(ASC_FEXE_NAME, ASC_FEXE_NAME, ascFileName, (char *)0);
  exit(1);
 } else {
  waitpid(pid, &status, 0);
  if (((((status) & 0x7f) == 0))
      && ((((status) & 0xff00) >> 8) != 0)) {
   fprintf(stderr,"Failed to launch ASC interpreter, or "
       "ASC interpreter returned with error status.\n");
   exit(1);
  }
 }
}

canonical_original_code:
static void forkAndRun(char *ascFileName)
{
 int status = 0;
 int pid;
 pid = fork();
 if (pid == 0) {
  execlp(ASC_FEXE_NAME, ASC_FEXE_NAME, ascFileName, (char *)0);
  exit(1);
 } else {
  waitpid(pid, &status, 0);
  if (((((status) & 0x7f) == 0))
      && ((((status) & 0xff00) >> 8) != 0)) {
   fprintf(stderr,"Failed to launch ASC interpreter, or "
       "ASC interpreter returned with error status.\n");
   exit(1);
  }
 }
}

variable_types:
ascFileName
char *

variable_types: status int

variable_types: pid int


parseInputs

decompiled_code:
__int64 __fastcall parseInputs(int argc, char **argv, __int64 a3)
{
int v4; // er13
char **v6; // rsi
__int64 v7; // r8
unsigned int v8; // er15
unsigned int v10; // er12
int v11; // eax
__int64 v12; // rcx
__int64 v13; // r9
__int64 v14; // rdx
char ***v15; // r12
char **v16; // r14
__int64 v17; // rax
__int64 v18; // rax
int v19; // [rsp+0h] [rbp-58h]
int v20; // [rsp+4h] [rbp-54h]
int v21; // [rsp+8h] [rbp-50h]
int v22; // [rsp+Ch] [rbp-4Ch]
char *v23; // [rsp+10h] [rbp-48h]
__int64 v24; // [rsp+18h] [rbp-40h]

v4 = 0;
opterr = 0;
v20 = 0;
v19 = 0;
v21 = 0;
v22 = 0;
while ( 2 )
{
v6 = argv;
v8 = getopt(argc, argv, "Snaqc");
if ( v8 == -1 )
{
v11 = optind;
if ( optind >= argc )
{
puts("Missing file name argument.");
}
else
{
v12 = optind;
v13 = optind;
v23 = argv[v13];
v14 = (unsigned int)(optind + 1);
optind = v14;
if ( (int)v14 < argc )
{
v15 = (char ***)&argv[v13 + 1];
v16 = &(&argv[argc - v11 - 2])[(__int64)v11 + 2];
do
{
v6 = *v15++;
printf("Non-option argument %s\n", (const char *)v6);
}
while ( v15 != (char ***)v16 );
}
v17 = getListingFileName(v23, v6, v14, v12, v7, v13 * 8);
if ( v17 )
{
v24 = v17;
v18 = getAscFileName(v23);
if ( v18 )
{
*(_DWORD *)a3 = v4;
v8 = 0;
*(_QWORD *)(a3 + 24) = v23;
*(_DWORD *)(a3 + 4) = v22;
*(_QWORD *)(a3 + 32) = v24;
*(_DWORD *)(a3 + 8) = v21;
*(_QWORD *)(a3 + 40) = v18;
*(_DWORD *)(a3 + 12) = v19;
*(_DWORD *)(a3 + 16) = v20;
}
}
}
}
else if ( v8 == 63 )
{
v10 = optopt;
v8 = -1;
if ( isprint(optopt) )
fprintf(stderr, "Unknown option `-%c'.\n", v10);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", v10);
}
else
{
switch ( v8 )
{
case 'S':
v4 = 1;
continue;
case 'a':
v21 = 1;
continue;
case 'c':
v20 = 1;
continue;
case 'n':
v22 = 1;
continue;
case 'q':
v19 = 1;
continue;
default:
v8 = -1;
break;
}
}
break;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv, __int64 a3)
{
int v4; // er13
char **v6; // rsi
__int64 v7; // r8
unsigned int v8; // er15
unsigned int v10; // er12
int v11; // eax
__int64 v12; // rcx
__int64 v13; // r9
__int64 v14; // rdx
char ***v15; // r12
char **v16; // r14
__int64 v17; // rax
__int64 v18; // rax
int v19; // [rsp+0h] [rbp-58h]
int v20; // [rsp+4h] [rbp-54h]
int v21; // [rsp+8h] [rbp-50h]
int v22; // [rsp+Ch] [rbp-4Ch]
char *v23; // [rsp+10h] [rbp-48h]
__int64 v24; // [rsp+18h] [rbp-40h]

v4 = 0;
opterr = 0;
v20 = 0;
v19 = 0;
v21 = 0;
v22 = 0;
while ( 2 )
{
v6 = argv;
v8 = getopt(argc, argv, "Snaqc");
if ( v8 == -1 )
{
v11 = optind;
if ( optind >= argc )
{
puts("Missing file name argument.");
}
else
{
v12 = optind;
v13 = optind;
v23 = argv[v13];
v14 = (unsigned int)(optind + 1);
optind = v14;
if ( (int)v14 < argc )
{
v15 = (char ***)&argv[v13 + 1];
v16 = &(&argv[argc - v11 - 2])[(__int64)v11 + 2];
do
{
v6 = *v15++;
printf("Non-option argument %s\n", (const char *)v6);
}
while ( v15 != (char ***)v16 );
}
v17 = getListingFileName(v23, v6, v14, v12, v7, v13 * 8);
if ( v17 )
{
v24 = v17;
v18 = getAscFileName(v23);
if ( v18 )
{
*(_DWORD *)a3 = v4;
v8 = 0;
*(_QWORD *)(a3 + 24) = v23;
*(_DWORD *)(a3 + 4) = v22;
*(_QWORD *)(a3 + 32) = v24;
*(_DWORD *)(a3 + 8) = v21;
*(_QWORD *)(a3 + 40) = v18;
*(_DWORD *)(a3 + 12) = v19;
*(_DWORD *)(a3 + 16) = v20;
}
}
}
}
else if ( v8 == 63 )
{
v10 = optopt;
v8 = -1;
if ( isprint(optopt) )
fprintf(stderr, "Unknown option `-%c'.\n", v10);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", v10);
}
else
{
switch ( v8 )
{
case 'S':
v4 = 1;
continue;
case 'a':
v21 = 1;
continue;
case 'c':
v20 = 1;
continue;
case 'n':
v22 = 1;
continue;
case 'q':
v19 = 1;
continue;
default:
v8 = -1;
break;
}
}
break;
}
return v8;
}

original_code:
int parseInputs(int argc, char **argv, struct args* argStruct)
{
 int S = 0;
 int n = 0;
 int a = 0;
 int q = 0;
 int c = 0;
 char *ascFile = ((void *)0);
 char *file = ((void *)0);
 char *listing = ((void *)0);
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "Snaqc")) != -1) {
  switch (option) {
  case 'S':
   S = 1;
   break;
  case 'n':
   n = 1;
   break;
  case 'a':
   a = 1;
   break;
  case 'q':
   q = 1;
   break;
  case 'c':
   c = 1;
   break;
  case '?':
   if (isprint(optopt)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (optind >= argc) {
  printf("Missing file name argument.\n");
  return -1;
 }
 file = argv[optind];
 optind++;
 for (index = optind; index < argc; index++) {
  printf("Non-option argument %s\n", argv[index]);
 }
 listing = getListingFileName(file);
 if (listing == ((void *)0)) {
  return -1;
 }
 ascFile = getAscFileName(file);
 if (!ascFile) return -1;
 argStruct->S = S;
 argStruct->n = n;
 argStruct->a = a;
 argStruct->q = q;
 argStruct->c = c;
 argStruct->inFile = file;
 argStruct->listingFile = listing;
 argStruct->ascFile = ascFile;
 return 0;
}

canonical_original_code:
int parseInputs(int argc, char **argv, struct args *argStruct)
{
 int S = 0;
 int n = 0;
 int a = 0;
 int q = 0;
 int c = 0;
 char *ascFile = ((void *)0);
 char *file = ((void *)0);
 char *listing = ((void *)0);
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "Snaqc")) != -1) {
  switch (option) {
  case 'S':
   S = 1;
   break;
  case 'n':
   n = 1;
   break;
  case 'a':
   a = 1;
   break;
  case 'q':
   q = 1;
   break;
  case 'c':
   c = 1;
   break;
  case '?':
   if (isprint(optopt)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (optind >= argc) {
  printf("Missing file name argument.\n");
  return -1;
 }
 file = argv[optind];
 optind++;
 for (index = optind; index < argc; index++) {
  printf("Non-option argument %s\n", argv[index]);
 }
 listing = getListingFileName(file);
 if (listing == ((void *)0)) {
  return -1;
 }
 ascFile = getAscFileName(file);
 if (!ascFile) return -1;
 argStruct->S = S;
 argStruct->n = n;
 argStruct->a = a;
 argStruct->q = q;
 argStruct->c = c;
 argStruct->inFile = file;
 argStruct->listingFile = listing;
 argStruct->ascFile = ascFile;
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct args { int S; int n; int a; int q; int c; char *inFile; char *listingFile; char *ascFile; } *

variable_types: S int

variable_types: n int

variable_types: a int

variable_types: q int

variable_types: c int

variable_types:
ascFile
char *

variable_types:
file
char *

variable_types:
listing
char *

variable_types: index int

variable_types: option int

return_type: int


getListingFileName

decompiled_code:
char *__fastcall getListingFileName(char *src)
{
int v1; // eax
char *v2; // r8
int v3; // ebx
char *v4; // rax
char *v5; // rax

v1 = strlen(src);
v2 = 0LL;
v3 = v1 + 1;
if ( v1 + 1 > 4 )
{
v4 = (char *)calloc(v3, 1uLL);
v5 = strncpy(v4, src, v3);
*(_DWORD *)&v5[v3 - 5] = 1953721390;
v2 = v5;
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func1(char *src)
{
int v1; // eax
char *v2; // r8
int v3; // ebx
char *v4; // rax
char *v5; // rax

v1 = strlen(src);
v2 = 0LL;
v3 = v1 + 1;
if ( v1 + 1 > 4 )
{
v4 = (char *)calloc(v3, 1uLL);
v5 = strncpy(v4, src, v3);
*(_DWORD *)&v5[v3 - 5] = 1953721390;
v2 = v5;
}
return v2;
}

original_code:
char *getListingFileName(char *palFileName)
{
 int len = 0;
 char *listingFileName = ((void *)0);
 len = strlen(palFileName) + 1;
 if (len < 5) {
  return ((void *)0);
 }
 listingFileName = calloc(len, sizeof(char));
 strncpy(listingFileName, palFileName, len);
 strncpy(listingFileName + len - 5, ".lst", 4);
 return listingFileName;
}

variable_types:
palFileName
char *

variable_types: len int

variable_types:
listingFileName
char *

return_type: char *


numLetters

repo: a-krebs/euler

decompiled_code:
__int64 __fastcall numLetters(int a1, __int64 a2)
{
__int64 result; // rax
int v3; // edi
int v4; // edx
int v5; // edi
int v6; // edx
int v7; // ecx
int v8; // ecx

result = (unsigned int)(a1 / 1000);
v3 = a1 % 1000;
v4 = v3 / 100;
v5 = v3 % 100;
if ( (_DWORD)result )
{
v8 = *(_DWORD *)(a2 + 4LL * (int)result);
result = (unsigned int)(v8 + 8);
if ( v4 )
goto LABEL_3;
v4 = v8 + 11;
if ( v5 > 19 )
goto LABEL_5;
v4 = v8 + 11;
if ( !v5 )
return result;
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v5));
}
if ( !v4 )
{
if ( v5 > 19 )
goto LABEL_5;
if ( !v5 )
return result;
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v5));
}
LABEL_3:
v6 = *(_DWORD *)(a2 + 4LL * v4) + result;
result = (unsigned int)(v6 + 7);
if ( v5 <= 19 )
{
if ( v5 )
result = (unsigned int)(v6 + 10 + *(_DWORD *)(a2 + 4LL * v5));
return result;
}
v4 = v6 + 10;
LABEL_5:
if ( v5 % 10 )
v4 += *(_DWORD *)(a2 + 4LL * (v5 % 10));
v7 = v5 / 10;
if ( v5 <= 59 )
{
result = (unsigned int)(v4 + 5);
if ( v5 > 39 )
return result;
return (unsigned int)(v4 + 6);
}
if ( v7 == 8 )
return (unsigned int)(v4 + 6);
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v7) + 2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2)
{
__int64 result; // rax
int v3; // edi
int v4; // edx
int v5; // edi
int v6; // edx
int v7; // ecx
int v8; // ecx

result = (unsigned int)(a1 / 1000);
v3 = a1 % 1000;
v4 = v3 / 100;
v5 = v3 % 100;
if ( (_DWORD)result )
{
v8 = *(_DWORD *)(a2 + 4LL * (int)result);
result = (unsigned int)(v8 + 8);
if ( v4 )
goto LABEL_3;
v4 = v8 + 11;
if ( v5 > 19 )
goto LABEL_5;
v4 = v8 + 11;
if ( !v5 )
return result;
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v5));
}
if ( !v4 )
{
if ( v5 > 19 )
goto LABEL_5;
if ( !v5 )
return result;
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v5));
}
LABEL_3:
v6 = *(_DWORD *)(a2 + 4LL * v4) + result;
result = (unsigned int)(v6 + 7);
if ( v5 <= 19 )
{
if ( v5 )
result = (unsigned int)(v6 + 10 + *(_DWORD *)(a2 + 4LL * v5));
return result;
}
v4 = v6 + 10;
LABEL_5:
if ( v5 % 10 )
v4 += *(_DWORD *)(a2 + 4LL * (v5 % 10));
v7 = v5 / 10;
if ( v5 <= 59 )
{
result = (unsigned int)(v4 + 5);
if ( v5 > 39 )
return result;
return (unsigned int)(v4 + 6);
}
if ( v7 == 8 )
return (unsigned int)(v4 + 6);
return (unsigned int)(v4 + *(_DWORD *)(a2 + 4LL * v7) + 2);
}

original_code:
int numLetters(int n, int* lookup)
{
 int letters = 0;
 int thousands = (int)floor(n / 1000);
 n = n % 1000;
 int hundreds = (int)floor(n / 100);
 n = n % 100;
 int tens = n;
 int tmp = 0;
 int and = 0;
 if (thousands != 0)
 {
  letters += lookup[thousands];
  letters += 8;
  and = 1;
 }
 if (hundreds != 0)
 {
  letters += lookup[hundreds];
  letters += 7;
  and = 1;
 }
 if (tens > 19)
 {
  letters += (and == 1) ? 3 : 0;
  letters += (n % 10 == 0) ? 0 : lookup[n % 10];
  tmp = (int)floor(n/10);
  switch (tmp)
  {
   case 2:
    letters += 6;
    break;
   case 3:
    letters += 6;
    break;
   case 4:
    letters += 5;
    break;
   case 5:
    letters += 5;
    break;
   case 8:
    letters += 6;
    break;
   default:
    letters += lookup[tmp] + 2;
    break;
  }
 }
 else if (tens != 0)
 {
  letters += (and == 1) ? 3 : 0;
  letters += lookup[tens];
 }
 return letters;
}

canonical_original_code:
int numLetters(int n, int *lookup)
{
 int letters = 0;
 int thousands = (int)floor(n / 1000);
 n = n % 1000;
 int hundreds = (int)floor(n / 100);
 n = n % 100;
 int tens = n;
 int tmp = 0;
 int and = 0;
 if (thousands != 0)
 {
  letters += lookup[thousands];
  letters += 8;
  and = 1;
 }
 if (hundreds != 0)
 {
  letters += lookup[hundreds];
  letters += 7;
  and = 1;
 }
 if (tens > 19)
 {
  letters += (and == 1) ? 3 : 0;
  letters += (n % 10 == 0) ? 0 : lookup[n % 10];
  tmp = (int)floor(n/10);
  switch (tmp)
  {
   case 2:
    letters += 6;
    break;
   case 3:
    letters += 6;
    break;
   case 4:
    letters += 5;
    break;
   case 5:
    letters += 5;
    break;
   case 8:
    letters += 6;
    break;
   default:
    letters += lookup[tmp] + 2;
    break;
  }
 }
 else if (tens != 0)
 {
  letters += (and == 1) ? 3 : 0;
  letters += lookup[tens];
 }
 return letters;
}

variable_types:
lookup
int *

variable_types: letters int

variable_types: thousands int

variable_types: hundreds int

variable_types: tens int

variable_types: tmp int

variable_types: and int


main

canonical_name: main

original_code:
int main(int argc, char *argv[])
{
 long sum = 0;
 int from = 1;
 int to = 1000;
 if (argc == 3)
 {
  from = atoi(argv[1]);
  to = atoi(argv[2]);
 }
 int lookup[20] = {
  4,
  3,
  3,
  5,
  4,
  4,
  3,
  5,
  5,
  4,
  3,
  6,
  6,
  8,
  8,
  7,
  7,
  9,
  8,
  8,
 };
 for (int i = from; i < to + 1; i++)
 {
  sum += numLetters(i, lookup);
 }
 printf("%ld\n", sum);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sum long

variable_types: from int

variable_types: to int

variable_types:
lookup
int [20]

variable_types: i int


getLength

decompiled_code:
__int64 __fastcall getLength(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = 1LL;
if ( a1 != 1 )
{
if ( a1 > 999999 || (result = *(unsigned int *)(a2 + 4 * a1), !(_DWORD)result) )
{
if ( (a1 & 1) != 0 )
result = (unsigned int)getLength(3 * a1 + 1, a2) + 1;
else
result = (unsigned int)getLength(a1 / 2, a2) + 1;
if ( a1 <= 999999 )
*(_DWORD *)(a2 + 4 * a1) = result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = 1LL;
if ( a1 != 1 )
{
if ( a1 > 999999 || (result = *(unsigned int *)(a2 + 4 * a1), !(_DWORD)result) )
{
if ( (a1 & 1) != 0 )
result = (unsigned int)func7(3 * a1 + 1, a2) + 1;
else
result = (unsigned int)func7(a1 / 2, a2) + 1;
if ( a1 <= 999999 )
*(_DWORD *)(a2 + 4 * a1) = result;
}
}
return result;
}

original_code:
int getLength(long n, int* cache)
{
 int cached = 0;
 if (n == 1) {
  return 1;
 }
 if (n <= 999999) {
  cached = cache[n];
 }
 if (cached != 0) {
  return cached;
 } else if (n % 2 == 0) {
  cached = getLength(n / 2, cache) + 1;
 } else {
  cached = getLength(3*n + 1, cache) + 1;
 }
 if (n <= 999999) {
  cache[n] = cached;
 }
 return cached;
}

canonical_original_code:
int getLength(long n, int *cache)
{
 int cached = 0;
 if (n == 1) {
  return 1;
 }
 if (n <= 999999) {
  cached = cache[n];
 }
 if (cached != 0) {
  return cached;
 } else if (n % 2 == 0) {
  cached = getLength(n / 2, cache) + 1;
 } else {
  cached = getLength(3*n + 1, cache) + 1;
 }
 if (n <= 999999) {
  cache[n] = cached;
 }
 return cached;
}

variable_types: n long

variable_types:
cache
int *

variable_types: cached int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
__int64 v4; // rdx
void *v5; // rbp
int v6; // eax
__int64 v7; // rdx
__int64 v8; // r8
int v9; // ecx

v3 = calloc(0xF4240uLL, 4uLL);
v4 = 999999LL;
v5 = v3;
while ( 1 )
{
v6 = getLength(v4, v5);
if ( v6 > v9 )
v8 = v7;
v4 = v7 - 1;
if ( !v4 )
{
printf("%ld\n", v8);
free(v5);
exit(0);
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
__int64 v4; // rdx
void *v5; // rbp
int v6; // eax
__int64 v7; // rdx
__int64 v8; // r8
int v9; // ecx

v3 = calloc(0xF4240uLL, 4uLL);
v4 = 999999LL;
v5 = v3;
while ( 1 )
{
v6 = func7(v4, v5);
if ( v6 > v9 )
v8 = v7;
v4 = v7 - 1;
if ( !v4 )
{
printf("%ld\n", v8);
free(v5);
exit(0);
}
}
}

original_code:
int main(int argc, char *argv[])
{
 long n_longest_chain = 0;
 int longest_chain_length = 0;
 long tmp = 0;
 int* cache = calloc(999999 + 1, sizeof(int));
 for (long n = 999999; n > 0; n--) {
  tmp = getLength(n, cache);
  if (longest_chain_length < tmp) {
   n_longest_chain = n;
   longest_chain_length = tmp;
  }
 }
 printf("%ld\n", n_longest_chain);
 free(cache);
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 long n_longest_chain = 0;
 int longest_chain_length = 0;
 long tmp = 0;
 int *cache = calloc(999999 + 1, sizeof(int));
 for (long n = 999999; n > 0; n--) {
  tmp = getLength(n, cache);
  if (longest_chain_length < tmp) {
   n_longest_chain = n;
   longest_chain_length = tmp;
  }
 }
 printf("%ld\n", n_longest_chain);
 free(cache);
 exit(0);
}

variable_types: tmp long


showDigits

decompiled_code:
int __fastcall showDigits(unsigned int *a1)
{
int v1; // ebp

v1 = 10;
do
{
printf("%d", *a1);
a1 = (unsigned int *)*((_QWORD *)a1 + 1);
--v1;
}
while ( v1 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1)
{
int v1; // ebp

v1 = 10;
do
{
printf("%d", *a1);
a1 = (unsigned int *)*((_QWORD *)a1 + 1);
--v1;
}
while ( v1 );
return putchar(10);
}

original_code:
void showDigits(struct digit * ptr)
{
 struct digit * head = ptr;
 for (int f = 0; f < 10; f++)
 {
  printf("%d", head->digit);
  head = head->next;
 }
 printf("\n");
}

canonical_original_code:
void showDigits(struct digit *ptr)
{
 struct digit *head = ptr;
 for (int f = 0; f < 10; f++)
 {
  printf("%d", head->digit);
  head = head->next;
 }
 printf("\n");
}

variable_types: struct digit { int digit; struct digit *next; struct digit *previous; } *

variable_types: f int


main

variable_types:
matrix
int [100][50]

variable_types: carryover int

variable_types: sum int

variable_types: d int

variable_types: n int

user_defined_types: struct digit { int digit; struct digit *next; struct digit *previous; }


printTOD

repo: a-krebs/parallel-matrix

decompiled_code:
int printTOD()
{
struct tm *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-38h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

v2 = 0LL;
gettimeofday(&v2, 0LL);
v0 = localtime(&v2.tv_sec);
strftime(s, 0x20uLL, "%H:%M:%S", v0);
return printf("%s.%03ld\n", s, v2.tv_usec / 1000);
}

canonical_decompiled_code:
int func7()
{
struct tm *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-38h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

v2 = 0LL;
gettimeofday(&v2, 0LL);
v0 = localtime(&v2.tv_sec);
strftime(s, 0x20uLL, "%H:%M:%S", v0);
return printf("%s.%03ld\n", s, v2.tv_usec / 1000);
}

original_code:
void printTOD() {
 char buffer[32];
 struct timeval time;
 memset(&time, 0, sizeof(struct timeval));
 gettimeofday(&time, ((void *)0));
 strftime(buffer, 32,"%H:%M:%S", localtime(&time.tv_sec));
 printf("%s.%03ld\n", buffer, time.tv_usec/1000);
}

variable_types:
buffer
char [32]

variable_types: struct timeval { long tv_sec; long tv_usec; }


timevalSubtract

original_code:
int timevalSubtract(struct timeval *result, struct timeval *x, struct timeval *y) {
 if (x->tv_usec < y->tv_usec) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  y->tv_usec -= 1000000 * nsec;
  y->tv_sec += nsec;
 }
 if (x->tv_usec - y->tv_usec > 1000000) {
  int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  y->tv_usec += 1000000 * nsec;
  y->tv_sec -= nsec;
 }
 result->tv_sec = x->tv_sec - y->tv_sec;
 result->tv_usec = x->tv_usec - y->tv_usec;
 return x->tv_sec < y->tv_sec;
}

variable_types: struct timeval { long tv_sec; long tv_usec; } *

variable_types:
x
struct timeval *

variable_types:
y
struct timeval *

variable_types: nsec int


printElapsedTime

decompiled_code:
int __fastcall printElapsedTime(__int64 *a1, __int64 *a2)
{
int result; // eax
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

if ( timevalSubtract(v3, a2, a1) )
result = printf("Time difference negative!");
else
result = printf("%ld.%06ld", v3[0], v3[1]);
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 *a1, __int64 *a2)
{
int result; // eax
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

if ( func8(v3, a2, a1) )
result = printf("Time difference negative!");
else
result = printf("%ld.%06ld", v3[0], v3[1]);
return result;
}

original_code:
void printElapsedTime(struct timeval *r1, struct timeval *r2) {
 struct timeval diff;
 if (timevalSubtract(&diff, r2, r1) != 0) {
  printf("Time difference negative!");
  return;
 }
 printf("%ld.%06ld", diff.tv_sec, diff.tv_usec);
}

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


multiply

original_code:
void multiply(int **A, int **B, int **C, int size, int rStart, int rEnd) {
 int rowC = 0;
 int colC = 0;
 int k = 0;
 int product = 0;
 for (rowC = rStart; rowC <= rEnd; rowC++) {
  for (colC = 0; colC < size; colC++) {
   product = 0;
   for (k = 0; k < size; k++) {
    product +=
        A[rowC][k] * B[k][colC];
   }
   C[rowC][colC] = product;
  }
 }
}

variable_types:
A
int **

variable_types:
B
int **

variable_types:
C
int **

variable_types: size int

variable_types: rStart int

variable_types: rEnd int

variable_types: rowC int

variable_types: colC int

variable_types: k int

variable_types: product int


printMatrix

decompiled_code:
int __fastcall printMatrix(_QWORD *a1, int a2)
{
__int64 v2; // r13
__int64 v3; // r12
_QWORD *v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi

if ( a2 > 0 )
{
v2 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v3 = 4LL * (unsigned int)a2;
v4 = a1;
do
{
v5 = 0LL;
do
{
v6 = *(unsigned int *)(*v4 + v5);
v5 += 4LL;
printf("%d\t", v6);
}
while ( v3 != v5 );
++v4;
putchar(10);
}
while ( v4 != (_QWORD *)v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func11(_QWORD *a1, int a2)
{
__int64 v2; // r13
__int64 v3; // r12
_QWORD *v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi

if ( a2 > 0 )
{
v2 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v3 = 4LL * (unsigned int)a2;
v4 = a1;
do
{
v5 = 0LL;
do
{
v6 = *(unsigned int *)(*v4 + v5);
v5 += 4LL;
printf("%d\t", v6);
}
while ( v3 != v5 );
++v4;
putchar(10);
}
while ( v4 != (_QWORD *)v2 );
}
return putchar(10);
}

original_code:
void printMatrix(int **m, int size) {
 int row = 0;
 int col = 0;
 for (row = 0; row < size; row++) {
  for (col = 0; col < size; col++) {
   printf("%d\t", m[row][col]);
  }
  printf("\n");
 }
 printf("\n");
}

variable_types:
m
int **

variable_types: row int

variable_types: col int


freeMatrixInt

decompiled_code:
void __fastcall freeMatrixInt(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 > 0 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8);
do
{
v5 = *v3++;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 > 0 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8);
do
{
v5 = *v3++;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

original_code:
void freeMatrixInt(int **m, int size) {
 int i = 0;
 for (i = 0; i < size; i++) {
  free(m[i]);
 }
 free(m);
}

variable_types: i int


tearDownExp

decompiled_code:
void __fastcall tearDownExp(void **a1, void **a2, void **a3, int a4)
{
freeMatrixInt(*a1, a4);
freeMatrixInt(*a2, a4);
freeMatrixInt(*a3, a4);
}

canonical_decompiled_code:
void __fastcall func13(void **a1, void **a2, void **a3, int a4)
{
func12(*a1, a4);
func12(*a2, a4);
func12(*a3, a4);
}

original_code:
void tearDownExp(int ***A, int ***B, int ***C, int size) {
 freeMatrixInt(*A, size);
 freeMatrixInt(*B, size);
 freeMatrixInt(*C, size);
}

variable_types:
A
int ***

variable_types:
B
int ***

variable_types:
C
int ***


initMatrixInt

decompiled_code:
void __fastcall initMatrixInt(_QWORD *a1, int a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = 0LL;
do
{
*(_DWORD *)(*v2 + v3) = random();
v3 += 4LL;
}
while ( 4LL * (unsigned int)a2 != v3 );
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

canonical_decompiled_code:
void __fastcall func14(_QWORD *a1, int a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = 0LL;
do
{
*(_DWORD *)(*v2 + v3) = random();
v3 += 4LL;
}
while ( 4LL * (unsigned int)a2 != v3 );
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

original_code:
void initMatrixInt(int **m, int size){
 int i = 0;
 int j = 0;
 for (i = 0; i < size; i++) {
  for (j = 0; j < size; j++) {
   m[i][j] = (int)random();
  }
 }
}

variable_types: j int


setupExp

decompiled_code:
__int64 __fastcall setupExp(_QWORD **a1, _QWORD **a2, _QWORD *a3, int a4, unsigned int a5)
{
_QWORD *v8; // rax

*a1 = allocMatrixInt(a4);
*a2 = allocMatrixInt(a4);
v8 = allocMatrixInt(a4);
*a3 = v8;
if ( !*a1 || !*a2 || !v8 )
return 0xFFFFFFFFLL;
srandom(a5);
initMatrixInt(*a1, a4);
initMatrixInt(*a2, a4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD **a1, _QWORD **a2, _QWORD *a3, int a4, unsigned int a5)
{
_QWORD *v8; // rax

*a1 = allocMatrixInt(a4);
*a2 = allocMatrixInt(a4);
v8 = allocMatrixInt(a4);
*a3 = v8;
if ( !*a1 || !*a2 || !v8 )
return 0xFFFFFFFFLL;
srandom(a5);
func14(*a1, a4);
func14(*a2, a4);
return 0LL;
}

original_code:
int setupExp(int ***A, int ***B, int ***C, int size, unsigned int seed) {
 *A = allocMatrixInt(size);
 *B = allocMatrixInt(size);
 *C = allocMatrixInt(size);
 if ((*A == ((void *)0)) || (*B == ((void *)0)) || (*C == ((void *)0))) {
  return -1;
 }
 srandom(seed);
 initMatrixInt(*A, size);
 initMatrixInt(*B, size);
 return 0;
}

variable_types:
seed
unsigned int


decompiled_code:
__int64 __fastcall parse_args(int argc, char **argv, int *a3)
{
int v3; // er15
int v4; // er14
int v6; // er12
int v7; // er12
FILE *v8; // rbp
const unsigned __int16 **v9; // rax
__int64 v10; // rdx

v3 = 1;
v4 = 0;
opterr = 0;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = getopt(argc, argv, "p:s:k:");
if ( v6 == -1 )
{
if ( v3 <= 0 )
{
puts("p must be at least 1.");
}
else if ( v4 > 0 && v4 % v3 )
{
puts("s must be evenly divisible into p");
}
else if ( argc > _bss_start )
{
printf("Non-option argument %s\n", argv[_bss_start]);
}
else
{
*a3 = v3;
v6 = 0;
a3[1] = v4;
a3[2] = 1000;
}
return (unsigned int)v6;
}
if ( v6 != 112 )
break;
v3 = atoi(optarg);
}
if ( v6 <= 112 )
break;
if ( v6 != 115 )
return (unsigned int)-1;
v4 = atoi(optarg);
}
if ( v6 == 63 )
break;
if ( v6 != 107 )
return (unsigned int)-1;
}
v7 = optopt;
v8 = stderr;
if ( ((optopt - 107) & 0xFFFFFFF7) == 0 || optopt == 112 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
return (unsigned int)-1;
}
v9 = __ctype_b_loc();
v10 = (unsigned int)v7;
if ( ((*v9)[v7] & 0x4000) != 0 )
{
v6 = -1;
fprintf(v8, "Unknown option `-%c'.\n", v10);
}
else
{
v6 = -1;
fprintf(v8, "Unknown option character `\\x%x'.\n", v10);
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func16(int argc, char **argv, int *a3)
{
int v3; // er15
int v4; // er14
int v6; // er12
int v7; // er12
FILE *v8; // rbp
const unsigned __int16 **v9; // rax
__int64 v10; // rdx

v3 = 1;
v4 = 0;
opterr = 0;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = getopt(argc, argv, "p:s:k:");
if ( v6 == -1 )
{
if ( v3 <= 0 )
{
puts("p must be at least 1.");
}
else if ( v4 > 0 && v4 % v3 )
{
puts("s must be evenly divisible into p");
}
else if ( argc > _bss_start )
{
printf("Non-option argument %s\n", argv[_bss_start]);
}
else
{
*a3 = v3;
v6 = 0;
a3[1] = v4;
a3[2] = 1000;
}
return (unsigned int)v6;
}
if ( v6 != 112 )
break;
v3 = atoi(optarg);
}
if ( v6 <= 112 )
break;
if ( v6 != 115 )
return (unsigned int)-1;
v4 = atoi(optarg);
}
if ( v6 == 63 )
break;
if ( v6 != 107 )
return (unsigned int)-1;
}
v7 = optopt;
v8 = stderr;
if ( ((optopt - 107) & 0xFFFFFFF7) == 0 || optopt == 112 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
return (unsigned int)-1;
}
v9 = __ctype_b_loc();
v10 = (unsigned int)v7;
if ( ((*v9)[v7] & 0x4000) != 0 )
{
v6 = -1;
fprintf(v8, "Unknown option `-%c'.\n", v10);
}
else
{
v6 = -1;
fprintf(v8, "Unknown option character `\\x%x'.\n", v10);
}
return (unsigned int)v6;
}

original_code:
int parse_args(int argc, char *argv[], struct arguments* args) {
 int procs = 1;
 int size = 0;
 unsigned int seed = 1000;
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "p:s:k:")) != -1) {
  switch (option) {
  case 'p':
   procs = atoi(optarg);
   break;
  case 's':
   size = atoi(optarg);
   break;
  case 'k':
   option = atoi(optarg);
   break;
  case '?':
   if ((optopt == 'p')
       || (optopt == 's')
       || (optopt == 'k')){
    fprintf(
        stderr,
        "Option -%c requires an argument.\n",
        optopt);
   } else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (procs < 1) {
  printf("p must be at least 1.\n");
  return -1;
 }
 if ((size > 0) && (size % procs != 0)) {
  printf("s must be evenly divisible into p\n");
  return -1;
 }
 for (index = optind; index < argc; index++) {
  printf("Non-option argument %s\n", argv[index]);
  return -1;
 }
 args->procs = procs;
 args->size = size;
 args->seed = seed;
 return 0;
}

canonical_original_code:
int parse_args(int argc, char *argv[], struct arguments *args) {
 int procs = 1;
 int size = 0;
 unsigned int seed = 1000;
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "p:s:k:")) != -1) {
  switch (option) {
  case 'p':
   procs = atoi(optarg);
   break;
  case 's':
   size = atoi(optarg);
   break;
  case 'k':
   option = atoi(optarg);
   break;
  case '?':
   if ((optopt == 'p')
       || (optopt == 's')
       || (optopt == 'k')){
    fprintf(
        stderr,
        "Option -%c requires an argument.\n",
        optopt);
   } else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (procs < 1) {
  printf("p must be at least 1.\n");
  return -1;
 }
 if ((size > 0) && (size % procs != 0)) {
  printf("s must be evenly divisible into p\n");
  return -1;
 }
 for (index = optind; index < argc; index++) {
  printf("Non-option argument %s\n", argv[index]);
  return -1;
 }
 args->procs = procs;
 args->size = size;
 args->seed = seed;
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct arguments { int procs; int size; int seed; } *

variable_types: procs int

variable_types: index int

variable_types: option int


slave

decompiled_code:
__int64 __fastcall slave(__int64 a1)
{
pthread_barrier_wait(&barrier);
if ( !*(_DWORD *)a1 )
gettimeofday(&phase2Start, 0LL);
multiply(
*(_QWORD *)(a1 + 16),
*(_QWORD *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 4),
*(unsigned int *)(a1 + 8));
pthread_barrier_wait(&barrier);
if ( !*(_DWORD *)a1 )
gettimeofday(&phase2End, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
pthread_barrier_wait(&barrier);
if ( !*(_DWORD *)a1 )
gettimeofday(&phase2Start, 0LL);
multiply(
*(_QWORD *)(a1 + 16),
*(_QWORD *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 4),
*(unsigned int *)(a1 + 8));
pthread_barrier_wait(&barrier);
if ( !*(_DWORD *)a1 )
gettimeofday(&phase2End, 0LL);
return 0LL;
}

original_code:
void *slave(void *args) {
 struct threadArgs *localArgs = (struct threadArgs *) args;
 pthread_barrier_wait(&barrier);
 if (localArgs->id == 0) { gettimeofday(&phase2Start, ((void *)0)); }
 multiply(
     localArgs->A, localArgs->B, localArgs->C,
     localArgs->size, localArgs->rStart, localArgs->rEnd);
 pthread_barrier_wait(&barrier);
 if (localArgs->id == 0) { gettimeofday(&phase2End, ((void *)0)); }
 return 0;
}

variable_types:
args
void *

variable_types: struct threadArgs { int id; int rStart; int rEnd; int **A; int **B; int **C; int size; } *

return_type: void *


getRangeEnd

decompiled_code:
__int64 __fastcall getRangeEnd(int a1, int a2, int a3)
{
return (unsigned int)(a3 / a2 + a1 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3)
{
return (unsigned int)(a3 / a2 + a1 - 1);
}

original_code:
int getRangeEnd(int rStart, int p, int s) {
 return rStart + s/p - 1;
}

variable_types: rStart int

variable_types: p int

variable_types: s int


main

repo: a-krebs/psrs

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
struct timeval tv; // [rsp+10h] [rbp-138h] BYREF
struct timeval v5; // [rsp+20h] [rbp-128h] BYREF
char name[280]; // [rsp+30h] [rbp-118h] BYREF

if ( (unsigned int)parse_args(argc, (char **)argv) )
exit(1);
tv = 0LL;
v5 = 0LL;
v3 = malloc(0LL);
srandom(0);
gettimeofday(&tv, 0LL);
qsort(v3, 0LL, 4uLL, compare);
gettimeofday(&v5, 0LL);
memset(name, 0, 0x100uLL);
gethostname(name, 0xFFuLL);
printf("seq,%d,%d,%s,%d,%d,-1,-1,-1,-1,", 0LL, 1LL, name, 0LL, 0LL);
print_elapsed_time(&tv, &v5);
putchar(10);
free(v3);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
struct timeval tv; // [rsp+10h] [rbp-138h] BYREF
struct timeval v5; // [rsp+20h] [rbp-128h] BYREF
char name[280]; // [rsp+30h] [rbp-118h] BYREF

if ( (unsigned int)func8(argc, (char **)argv) )
exit(1);
tv = 0LL;
v5 = 0LL;
v3 = malloc(0LL);
srandom(0);
gettimeofday(&tv, 0LL);
qsort(v3, 0LL, 4uLL, compare);
gettimeofday(&v5, 0LL);
memset(name, 0, 0x100uLL);
gethostname(name, 0xFFuLL);
printf("seq,%d,%d,%s,%d,%d,-1,-1,-1,-1,", 0LL, 1LL, name, 0LL, 0LL);
func11(&tv, &v5);
putchar(10);
free(v3);
exit(0);
}

original_code:
int main(int argc, char **argv)
{
 int size = 1;
 char hname[ 256 ];
 struct arguments args;
 struct timeval start;
 struct timeval end;
 int *elem = ((void *)0);
 int i = 0;
 memset((void*)&args, 0, sizeof(struct arguments));
 if (parse_args(argc, argv, &args, size) != 0) {
  exit(1);
 }
 memset((void*)&start, 0, sizeof(struct timeval));
 memset((void*)&end, 0, sizeof(struct timeval));
 elem = malloc(args.nElem * sizeof(int));
 srandom(args.seed);
 for (i = 0; i < args.nElem; i++) {
  elem[i] = (int)random();
 }
 gettimeofday(&start, ((void *)0));
 qsort(elem, args.nElem, sizeof(int), &compare);
 gettimeofday(&end, ((void *)0));
 memset((void*)hname, 0, 256);
 gethostname(hname, 255);
 printf("seq,%d,%d,%s,%d,%d,-1,-1,-1,-1,",
     0, 1, hname, args.nElem, args.seed);
 print_elapsed_time(&start, &end);
 printf("\n");
 free(elem);
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int size = 1;
 char hname[256];
 struct arguments args;
 struct timeval start;
 struct timeval end;
 int *elem = ((void *)0);
 int i = 0;
 memset((void *)&args, 0, sizeof(struct arguments));
 if (parse_args(argc, argv, &args, size) != 0) {
  exit(1);
 }
 memset((void *)&start, 0, sizeof(struct timeval));
 memset((void *)&end, 0, sizeof(struct timeval));
 elem = malloc(args.nElem * sizeof(int));
 srandom(args.seed);
 for (i = 0; i < args.nElem; i++) {
  elem[i] = (int)random();
 }
 gettimeofday(&start, ((void *)0));
 qsort(elem, args.nElem, sizeof(int), &compare);
 gettimeofday(&end, ((void *)0));
 memset((void *)hname, 0, 256);
 gethostname(hname, 255);
 printf("seq,%d,%d,%s,%d,%d,-1,-1,-1,-1,",
     0, 1, hname, args.nElem, args.seed);
 print_elapsed_time(&start, &end);
 printf("\n");
 free(elem);
 exit(0);
}

variable_types:
argv
char **

variable_types: size int

variable_types:
hname
char [256]

variable_types: struct arguments { int nElem; int seed; }

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types:
elem
int *

variable_types: i int


compare

decompiled_code:
__int64 __fastcall compare(const void *a1, const void *a2)
{
__int64 result; // rax

result = *(_DWORD *)a1 > *(_DWORD *)a2;
if ( *(_DWORD *)a1 < *(_DWORD *)a2 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const void *a1, const void *a2)
{
__int64 result; // rax

result = *(_DWORD *)a1 > *(_DWORD *)a2;
if ( *(_DWORD *)a1 < *(_DWORD *)a2 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int compare(const void *x, const void *y) {
 int a = *(int*)x;
 int b = *(int*)y;
 if (a < b) return -1;
 if (a > b) return 1;
 return 0;
}

canonical_original_code:
int compare(void *x, void *y) {
 int a = *(int*)x;
 int b = *(int*)y;
 if (a < b) return -1;
 if (a > b) return 1;
 return 0;
}

variable_types:
x
void *

variable_types:
y
void *

variable_types: a int

variable_types: b int


decompiled_code:
__int64 __fastcall parse_args(int argc, char **argv, int *a3, int a4)
{
int v4; // er15
int v5; // er14
int v8; // er8
unsigned int v9; // er8
unsigned int v11; // er12
FILE *v12; // rbp
int v13; // [rsp+Ch] [rbp-3Ch]
int v14; // [rsp+Ch] [rbp-3Ch]

v4 = 1000;
v5 = 0;
opterr = 0;
while ( 1 )
{
while ( 1 )
{
v8 = getopt(argc, argv, "n:s:");
if ( v8 == -1 )
{
if ( a4 <= 0 )
{
fwrite("p must be at least 1.\n", 1uLL, 0x16uLL, stderr);
v9 = -1;
}
else if ( v5 > 0 && v5 % a4 )
{
v13 = v8;
fwrite("n must be evenly divisible into p\n", 1uLL, 0x22uLL, stderr);
v9 = v13;
}
else if ( argc > optind )
{
v14 = v8;
fprintf(stderr, "Non-option argument %s\n", argv[optind]);
v9 = v14;
}
else
{
*a3 = v5;
v9 = 0;
a3[1] = v4;
}
return v9;
}
if ( v8 != 110 )
break;
v5 = atoi(optarg);
}
if ( v8 != 115 )
break;
v4 = atoi(optarg);
}
if ( v8 != 63 )
return (unsigned int)-1;
v11 = optopt;
v12 = stderr;
if ( optopt == 110 || optopt == 115 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
return (unsigned int)-1;
}
if ( ((*__ctype_b_loc())[v11] & 0x4000) != 0 )
fprintf(v12, "Unknown option `-%c'.\n", v11);
else
fprintf(v12, "Unknown option character `\\x%x'.\n", v11);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func8(int argc, char **argv, int *a3, int a4)
{
int v4; // er15
int v5; // er14
int v8; // er8
unsigned int v9; // er8
unsigned int v11; // er12
FILE *v12; // rbp
int v13; // [rsp+Ch] [rbp-3Ch]
int v14; // [rsp+Ch] [rbp-3Ch]

v4 = 1000;
v5 = 0;
opterr = 0;
while ( 1 )
{
while ( 1 )
{
v8 = getopt(argc, argv, "n:s:");
if ( v8 == -1 )
{
if ( a4 <= 0 )
{
fwrite("p must be at least 1.\n", 1uLL, 0x16uLL, stderr);
v9 = -1;
}
else if ( v5 > 0 && v5 % a4 )
{
v13 = v8;
fwrite("n must be evenly divisible into p\n", 1uLL, 0x22uLL, stderr);
v9 = v13;
}
else if ( argc > optind )
{
v14 = v8;
fprintf(stderr, "Non-option argument %s\n", argv[optind]);
v9 = v14;
}
else
{
*a3 = v5;
v9 = 0;
a3[1] = v4;
}
return v9;
}
if ( v8 != 110 )
break;
v5 = atoi(optarg);
}
if ( v8 != 115 )
break;
v4 = atoi(optarg);
}
if ( v8 != 63 )
return (unsigned int)-1;
v11 = optopt;
v12 = stderr;
if ( optopt == 110 || optopt == 115 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
return (unsigned int)-1;
}
if ( ((*__ctype_b_loc())[v11] & 0x4000) != 0 )
fprintf(v12, "Unknown option `-%c'.\n", v11);
else
fprintf(v12, "Unknown option character `\\x%x'.\n", v11);
return (unsigned int)-1;
}

original_code:
int parse_args(int argc, char *argv[], struct arguments* args, int procs) {
 int nElem = 0;
 unsigned int seed = 1000;
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "n:s:")) != -1) {
  switch (option) {
  case 'n':
   nElem = atoi(optarg);
   break;
  case 's':
   seed = atoi(optarg);
   break;
  case '?':
   if ((optopt == 'n')
       || (optopt == 's')){
    fprintf(
        stderr,
        "Option -%c requires an argument.\n",
        optopt);
   } else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (procs < 1) {
  fprintf(stderr, "p must be at least 1.\n");
  return -1;
 }
 if ((nElem > 0) && (nElem % procs != 0)) {
  fprintf(stderr, "n must be evenly divisible into p\n");
  return -1;
 }
 for (index = optind; index < argc; index++) {
  fprintf(stderr, "Non-option argument %s\n", argv[index]);
  return -1;
 }
 args->nElem = nElem;
 args->seed = seed;
 return 0;
}

canonical_original_code:
int parse_args(int argc, char *argv[], struct arguments *args, int procs) {
 int nElem = 0;
 unsigned int seed = 1000;
 int index;
 int option;
 opterr = 0;
 while ((option = getopt(argc, argv, "n:s:")) != -1) {
  switch (option) {
  case 'n':
   nElem = atoi(optarg);
   break;
  case 's':
   seed = atoi(optarg);
   break;
  case '?':
   if ((optopt == 'n')
       || (optopt == 's')){
    fprintf(
        stderr,
        "Option -%c requires an argument.\n",
        optopt);
   } else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint)) {
    fprintf(
        stderr,
        "Unknown option `-%c'.\n",
        optopt);
   } else {
    fprintf(
        stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
   }
  default:
   return -1;
  }
 }
 if (procs < 1) {
  fprintf(stderr, "p must be at least 1.\n");
  return -1;
 }
 if ((nElem > 0) && (nElem % procs != 0)) {
  fprintf(stderr, "n must be evenly divisible into p\n");
  return -1;
 }
 for (index = optind; index < argc; index++) {
  fprintf(stderr, "Non-option argument %s\n", argv[index]);
  return -1;
 }
 args->nElem = nElem;
 args->seed = seed;
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct arguments { int nElem; int seed; } *

variable_types: procs int

variable_types: nElem int

variable_types:
seed
unsigned int

variable_types: index int

variable_types: option int


decompiled_code:
int print_TOD()
{
struct tm *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-38h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

v2 = 0LL;
gettimeofday(&v2, 0LL);
v0 = localtime(&v2.tv_sec);
strftime(s, 0x20uLL, "%H:%M:%S", v0);
return printf("%s.%03ld\n", s, v2.tv_usec / 1000);
}

canonical_decompiled_code:
int func9()
{
struct tm *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-38h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

v2 = 0LL;
gettimeofday(&v2, 0LL);
v0 = localtime(&v2.tv_sec);
strftime(s, 0x20uLL, "%H:%M:%S", v0);
return printf("%s.%03ld\n", s, v2.tv_usec / 1000);
}

original_code:
void print_TOD() {
 char buffer[32];
 struct timeval time;
 memset(&time, 0, sizeof(struct timeval));
 gettimeofday(&time, ((void *)0));
 strftime(buffer, 32,"%H:%M:%S", localtime(&time.tv_sec));
 printf("%s.%03ld\n", buffer, time.tv_usec/1000);
}

variable_types:
buffer
char [32]


original_code:
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) {
 if (x->tv_usec < y->tv_usec) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  y->tv_usec -= 1000000 * nsec;
  y->tv_sec += nsec;
 }
 if (x->tv_usec - y->tv_usec > 1000000) {
  int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  y->tv_usec += 1000000 * nsec;
  y->tv_sec -= nsec;
 }
 result->tv_sec = x->tv_sec - y->tv_sec;
 result->tv_usec = x->tv_usec - y->tv_usec;
 return x->tv_sec < y->tv_sec;
}

variable_types: struct timeval { long tv_sec; long tv_usec; } *

variable_types:
x
struct timeval *

variable_types:
y
struct timeval *

variable_types: nsec int


decompiled_code:
int __fastcall print_elapsed_time(__int64 *a1, __int64 *a2)
{
int result; // eax
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

if ( timeval_subtract(v3, a2, a1) )
result = printf("Time difference negative!");
else
result = printf("%ld.%06ld", v3[0], v3[1]);
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 *a1, __int64 *a2)
{
int result; // eax
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

if ( func10(v3, a2, a1) )
result = printf("Time difference negative!");
else
result = printf("%ld.%06ld", v3[0], v3[1]);
return result;
}

original_code:
void print_elapsed_time(struct timeval *r1, struct timeval *r2) {
 struct timeval diff;
 if (timeval_subtract(&diff, r2, r1) != 0) {
  printf("Time difference negative!");
  return;
 }
 printf("%ld.%06ld", diff.tv_sec, diff.tv_usec);
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


concat

repo: a-nikolaev/curseofwar

decompiled_code:
char *__fastcall concat(void *src, void *a2)
{
size_t v2; // rbx
size_t v3; // rbp
char *v4; // r12

v2 = strlen((const char *)src);
v3 = strlen((const char *)a2);
v4 = (char *)malloc(v2 + v3 + 1);
memcpy(v4, src, v2);
memcpy(&v4[v2], a2, v3 + 1);
return v4;
}

canonical_decompiled_code:
char *__fastcall func0(void *src, void *a2)
{
size_t v2; // rbx
size_t v3; // rbp
char *v4; // r12

v2 = strlen((const char *)src);
v3 = strlen((const char *)a2);
v4 = (char *)malloc(v2 + v3 + 1);
memcpy(v4, src, v2);
memcpy(&v4[v2], a2, v3 + 1);
return v4;
}

original_code:
char *concat(char *a, char *b) {
  char *res = (char*) malloc (sizeof(char) * (strlen(a) + strlen(b) + 1));
  strcpy(res, a);
  strcat(res, b);
  return res;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types:
res
char *


decompiled_code:
char *get_xdg_data_home()
{
char *v0; // rax
char *v2; // rdi
char *v3; // rax
char *v4; // rbp
char *v5; // r12

v0 = getenv("XDG_DATA_HOME");
if ( v0 )
return strdup(v0);
v2 = getenv("HOME");
if ( v2 )
return concat(v2, "/.local/share");
v3 = getenv("USER");
v4 = concat("/home/", v3);
v5 = concat(v4, "/.local/share");
free(v4);
return v5;
}

canonical_decompiled_code:
char *func1()
{
char *v0; // rax
char *v2; // rdi
char *v3; // rax
char *v4; // rbp
char *v5; // r12

v0 = getenv("XDG_DATA_HOME");
if ( v0 )
return strdup(v0);
v2 = getenv("HOME");
if ( v2 )
return func0(v2, "/.local/share");
v3 = getenv("USER");
v4 = func0("/home/", v3);
v5 = func0(v4, "/.local/share");
free(v4);
return v5;
}

original_code:
char *get_xdg_data_home() {
  char *s;
  if ((s = getenv("XDG_DATA_HOME")) != ((void *)0)) {
    return strdup(s);
  }
  else if ((s = getenv("HOME")) != ((void *)0)) {
    char *suffix = "/.local/share";
    return concat (s, suffix);
  }
  else {
    char *a = concat ("/home/", getenv("USER"));
    char *b = concat (a, "/.local/share");
    free(a);
    return b;
  }
}

variable_types:
s
char *

variable_types:
suffix
char *


decompiled_code:
_QWORD *get_search_paths()
{
_QWORD *v0; // r12
char *v1; // rbp
char *v2; // rax
char *v3; // rax

v0 = malloc(0x48uLL);
*v0 = strdup(&s);
v1 = get_xdg_data_home();
v0[1] = concat(v1, "/curseofwar/");
free(v1);
v2 = getenv("HOME");
v0[2] = concat(v2, "/.curseofwar/");
v0[3] = strdup("/usr/local/share/curseofwar/");
v0[4] = strdup("/usr/share/curseofwar/");
v0[5] = strdup("/usr/share/curseofwar-sdl/");
v0[6] = strdup("/usr/share/curseofwar-common/");
v3 = strdup("/usr/share/curseofwar/");
v0[8] = 0LL;
v0[7] = v3;
return v0;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // r12
char *v1; // rbp
char *v2; // rax
char *v3; // rax

v0 = malloc(0x48uLL);
*v0 = strdup(&s);
v1 = func1();
v0[1] = func0(v1, "/curseofwar/");
free(v1);
v2 = getenv("HOME");
v0[2] = func0(v2, "/.curseofwar/");
v0[3] = strdup("/usr/local/share/curseofwar/");
v0[4] = strdup("/usr/share/curseofwar/");
v0[5] = strdup("/usr/share/curseofwar-sdl/");
v0[6] = strdup("/usr/share/curseofwar-common/");
v3 = strdup("/usr/share/curseofwar/");
v0[8] = 0LL;
v0[7] = v3;
return v0;
}

original_code:
char **get_search_paths() {
  char *suffix = "/curseofwar/";
  int dirs_num = 8;
  char **path = (char**) malloc(sizeof(char*) * (dirs_num + 1));
  path[0] = strdup("");
  char *s;
  s = get_xdg_data_home();
  path[1] = concat(s, suffix);
  free(s);
  path[2] = concat(getenv("HOME"), "/.curseofwar/");
  path[3] = strdup("/usr/local/share/curseofwar/");
  path[4] = strdup("/usr/share/curseofwar/");
  path[5] = strdup("/usr/share/curseofwar-sdl/");
  path[6] = strdup("/usr/share/curseofwar-common/");
  path[7] = strdup("/usr/share/curseofwar/");
  path[dirs_num] = ((void *)0);
  return path;
}

variable_types:
path
char **

return_type: char **


decompiled_code:
void __fastcall destroy_search_paths(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
free(ptr);
}

original_code:
void destroy_search_paths(char** path) {
  int i=0;
  while (path[i] != ((void *)0)) {
    free(path[i]);
    ++i;
  }
  free(path);
}

canonical_original_code:
void destroy_search_paths(char **path) {
  int i = 0;
  while (path[i] != ((void *)0)) {
    free(path[i]);
    ++i;
  }
  free(path);
}

variable_types: i int


decompiled_code:
char *__fastcall find_file(void **a1, void *a2)
{
void *v3; // rdi
void **v4; // rbx
char *v5; // rbp
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v3 = *a1;
if ( !v3 )
return 0LL;
v4 = a1 + 1;
while ( 1 )
{
v5 = concat(v3, a2);
if ( !stat(v5, &v7) && (v7.st_mode & 0xF000) == 0x8000 )
break;
++v4;
free(v5);
v3 = *(v4 - 1);
if ( !v3 )
return 0LL;
}
return v5;
}

canonical_decompiled_code:
char *__fastcall func4(void **a1, void *a2)
{
void *v3; // rdi
void **v4; // rbx
char *v5; // rbp
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v3 = *a1;
if ( !v3 )
return 0LL;
v4 = a1 + 1;
while ( 1 )
{
v5 = func0(v3, a2);
if ( !stat(v5, &v7) && (v7.st_mode & 0xF000) == 0x8000 )
break;
++v4;
free(v5);
v3 = *(v4 - 1);
if ( !v3 )
return 0LL;
}
return v5;
}

original_code:
char *find_file(char** path, char* suffix) {
  char *res;
  struct stat sb;
  int i = 0;
  while (path[i] != ((void *)0)) {
    res = concat(path[i], suffix);
    if ( stat(res, &sb) == 0 && ((((sb.st_mode)) & 0170000) == (0100000)) )
      return res;
    else
      free(res);
    ++i;
  }
  return ((void *)0);
}

canonical_original_code:
char *find_file(char **path, char *suffix) {
  char *res;
  struct stat sb;
  int i = 0;
  while (path[i] != ((void *)0)) {
    res = concat(path[i], suffix);
    if ( stat(res, &sb) == 0 && ((((sb.st_mode)) & 0170000) == (0100000)) )
      return res;
    else
      free(res);
    ++i;
  }
  return ((void *)0);
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
void on_timer()
{
time_to_redraw = 1;
}

canonical_decompiled_code:
void func0()
{
time_to_redraw = 1;
}

original_code:
void on_timer(int signum)
{
  time_to_redraw = 1;
}

variable_types: signum int


decompiled_code:
void __fastcall win_or_lose_message(__int64 a1, int a2)
{
int v2; // eax

if ( __ROR4__(-1030792151 * a2 + 85899344, 2) <= 0x28F5C28u )
{
v2 = win_or_lose();
if ( v2 == -1 )
{
wattrset(stdscr, 2097664);
if ( wmove(stdscr, *(_DWORD *)(a1 + 4) + 2, 31) != -1 )
waddnstr(stdscr, "You are defeated!", -1);
}
else if ( v2 == 1 )
{
wattrset(stdscr, 2098176);
if ( wmove(stdscr, *(_DWORD *)(a1 + 4) + 2, 31) != -1 )
waddnstr(stdscr, "You are victorious!", -1);
}
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2)
{
int v2; // eax

if ( __ROR4__(-1030792151 * a2 + 85899344, 2) <= 0x28F5C28u )
{
v2 = win_or_lose();
if ( v2 == -1 )
{
wattrset(stdscr, 2097664);
if ( wmove(stdscr, *(_DWORD *)(a1 + 4) + 2, 31) != -1 )
waddnstr(stdscr, "You are defeated!", -1);
}
else if ( v2 == 1 )
{
wattrset(stdscr, 2098176);
if ( wmove(stdscr, *(_DWORD *)(a1 + 4) + 2, 31) != -1 )
waddnstr(stdscr, "You are victorious!", -1);
}
}
}

variable_types: struct state { struct grid { int width; int height; struct tile { enum tile_class { abyss=0, mountain=1, mine=2, grassland=3, village=4, town=5, castle=6 } cl; int pl; int units[8][1]; } tiles[40][29]; } grid; struct flag_grid { int width; int height; int flag[40][29]; int call[40][29]; } fg[8]; struct king { int value[40][29]; int pl; enum strategy { none=0, aggr_greedy=1, one_greedy=2, persistent_greedy=3, opportunist=4, noble=5, midas=6 } strategy; } king[8]; int kings_num; struct timeline { float data[8][72]; unsigned long time[72]; int mark; } timeline; int show_timeline; struct country { long gold; } country[8]; unsigned long time; int map_seed; int controlled; int conditions; int inequality; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } speed; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } prev_speed; enum config_dif { dif_easiest=0, dif_easy=1, dif_normal=2, dif_hard=3, dif_hardest=4 } dif; } *

variable_types: k int

user_defined_types: struct state { struct grid grid; struct flag_grid fg[8]; struct king king[8]; int kings_num; struct timeline timeline; int show_timeline; struct country country[8]; unsigned long time; int map_seed; int controlled; int conditions; int inequality; enum config_speed speed; enum config_speed prev_speed; enum config_dif dif; }

user_defined_types: struct grid { int width; int height; struct tile tiles[40][29]; }

user_defined_types: struct king { int value[40][29]; int pl; enum strategy strategy; }

user_defined_types: struct tile { enum tile_class cl; int pl; int units[8][1]; }


decompiled_code:
__int64 __fastcall dialog_quit_confirm(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

output_dialog_quit_on();
while ( 2 )
{
while ( fread(ptr, 1uLL, 1uLL, stdin) != 1 )
pause();
if ( ptr[0] == 27 )
{
LABEL_6:
v2 = 0;
}
else
{
switch ( ptr[0] )
{
case 'N':
case 'n':
goto LABEL_6;
case 'Q':
case 'Y':
case 'q':
case 'y':
v2 = 1;
break;
default:
continue;
}
}
break;
}
output_dialog_quit_off(a1, a2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

output_dialog_quit_on();
while ( 2 )
{
while ( fread(ptr, 1uLL, 1uLL, stdin) != 1 )
pause();
if ( ptr[0] == 27 )
{
LABEL_6:
v2 = 0;
}
else
{
switch ( ptr[0] )
{
case 'N':
case 'n':
goto LABEL_6;
case 'Q':
case 'Y':
case 'q':
case 'y':
v2 = 1;
break;
default:
continue;
}
}
break;
}
output_dialog_quit_off(a1, a2);
return v2;
}

original_code:
int dialog_quit_confirm(struct state *st, struct ui *ui) {
  output_dialog_quit_on(st, ui);
  int done = 0;
  int finished = 0;
  char buf[1];
  while(!done) {
    if ( fread(buf, 1, 1, stdin) == 1 ) {
      char c = buf[0];
      switch(c){
        case 'y': case 'Y': case 'q': case 'Q':
          finished = 1;
          done = 1;
          break;
        case 'n': case 'N': case '\033':
          finished = 0;
          done = 1;
          break;
        default:
          break;
      }
    }
    else
      pause();
  }
  output_dialog_quit_off(st, ui);
  return finished;
}

variable_types: struct ui { struct loc { int i; int j; } cursor; int xskip; int xlength; } *

variable_types: done int

variable_types: finished int

variable_types:
buf
char [1]

variable_types: c char

user_defined_types: struct ui { struct loc cursor; int xskip; int xlength; }

user_defined_types: struct timeline { float data[8][72]; unsigned long time[72]; int mark; }

user_defined_types: enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 }

user_defined_types: enum config_dif { dif_easiest=0, dif_easy=1, dif_normal=2, dif_hard=3, dif_hardest=4 }

user_defined_types: struct flag_grid { int width; int height; int flag[40][29]; int call[40][29]; }

user_defined_types: enum strategy { none=0, aggr_greedy=1, one_greedy=2, persistent_greedy=3, opportunist=4, noble=5, midas=6 }

user_defined_types: enum tile_class { abyss=0, mountain=1, mine=2, grassland=3, village=4, town=5, castle=6 }


decompiled_code:
__int64 __fastcall update_from_input(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v2 = dialog_quit_confirm(a1, a2);
else
v2 = singleplayer_process_input(a1, a2);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v2 = func2(a1, a2);
else
v2 = singleplayer_process_input(a1, a2);
}
return v2;
}

original_code:
int update_from_input( struct state *st, struct ui *ui )
{
    int c;
    char buf[1];
    int finished=0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch(c){
        case 'q': case 'Q':
          finished = dialog_quit_confirm(st, ui);
          break;
        default:
          finished = singleplayer_process_input (st, ui, c);
      }
    }
    return finished;
}

canonical_original_code:
int update_from_input(struct state *st, struct ui *ui)
{
    int c;
    char buf[1];
    int finished = 0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch(c){
        case 'q': case 'Q':
          finished = dialog_quit_confirm(st, ui);
          break;
        default:
          finished = singleplayer_process_input (st, ui, c);
      }
    }
    return finished;
}

variable_types: c int


run

decompiled_code:
int __fastcall run(__int64 a1, __int64 a2)
{
int v2; // er12
int v4; // ebp
int result; // eax
__int64 v6; // rdi

v2 = 0;
do
{
if ( !time_to_redraw )
goto LABEL_7;
++v2;
v6 = *(unsigned int *)(a1 + 160888);
if ( v2 > 1599 )
{
game_slowdown(v6);
v2 = 0;
LABEL_11:
if ( *(_DWORD *)(a1 + 160888) )
{
kings_move(a1);
simulate(a1);
if ( *(_DWORD *)(a1 + 160792) )
{
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * *(_QWORD *)(a1 + 160864), 1) <= 0x1999999999999999uLL )
update_timeline(a1);
}
}
goto LABEL_3;
}
if ( !(v2 % (int)game_slowdown(v6)) )
goto LABEL_11;
LABEL_3:
output_grid(a1, a2, (unsigned int)v2);
if ( *(_DWORD *)(a1 + 160792) )
{
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * *(_QWORD *)(a1 + 160864), 1) <= 0x1999999999999999uLL )
output_timeline(a1, a2);
}
time_to_redraw = 0;
win_or_lose_message(a1, v2);
LABEL_7:
v4 = update_from_input(a1, a2);
result = pause();
}
while ( !v4 );
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2)
{
int v2; // er12
int v4; // ebp
int result; // eax
__int64 v6; // rdi

v2 = 0;
do
{
if ( !time_to_redraw )
goto LABEL_7;
++v2;
v6 = *(unsigned int *)(a1 + 160888);
if ( v2 > 1599 )
{
game_slowdown(v6);
v2 = 0;
LABEL_11:
if ( *(_DWORD *)(a1 + 160888) )
{
kings_move(a1);
simulate(a1);
if ( *(_DWORD *)(a1 + 160792) )
{
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * *(_QWORD *)(a1 + 160864), 1) <= 0x1999999999999999uLL )
update_timeline(a1);
}
}
goto LABEL_3;
}
if ( !(v2 % (int)game_slowdown(v6)) )
goto LABEL_11;
LABEL_3:
output_grid(a1, a2, (unsigned int)v2);
if ( *(_DWORD *)(a1 + 160792) )
{
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * *(_QWORD *)(a1 + 160864), 1) <= 0x1999999999999999uLL )
output_timeline(a1, a2);
}
time_to_redraw = 0;
func1(a1, v2);
LABEL_7:
v4 = func3(a1, a2);
result = pause();
}
while ( !v4 );
return result;
}

original_code:
void run (struct state *st, struct ui *ui) {
  int k = 0;
  int finished = 0;
  while( !finished ) {
    if (time_to_redraw) {
      k++;
      if (k>=1600) k=0;
      int slowdown = game_slowdown(st->speed);
      if (k % slowdown == 0 && st->speed != sp_pause) {
        kings_move(st);
        simulate(st);
        if (st->show_timeline) {
          if (st->time%10 == 0)
            update_timeline(st);
        }
      }
      output_grid(st, ui, k);
      if (st->show_timeline) {
        if (st->time%10 == 0)
          output_timeline(st, ui);
      }
      time_to_redraw = 0;
      win_or_lose_message(st, k);
    }
    finished = update_from_input(st, ui);
    pause();
  }
}

canonical_original_code:
void run(struct state *st, struct ui *ui) {
  int k = 0;
  int finished = 0;
  while( !finished ) {
    if (time_to_redraw) {
      k++;
      if (k>=1600) k=0;
      int slowdown = game_slowdown(st->speed);
      if (k % slowdown == 0 && st->speed != sp_pause) {
        kings_move(st);
        simulate(st);
        if (st->show_timeline) {
          if (st->time%10 == 0)
            update_timeline(st);
        }
      }
      output_grid(st, ui, k);
      if (st->show_timeline) {
        if (st->time%10 == 0)
          output_timeline(st, ui);
      }
      time_to_redraw = 0;
      win_or_lose_message(st, k);
    }
    finished = update_from_input(st, ui);
    pause();
  }
}

variable_types: slowdown int


decompiled_code:
__int64 __fastcall update_from_input_client(__int64 a1, __int64 a2, unsigned int a3, __int64 a4)
{
unsigned int v4; // er14
char ptr[41]; // [rsp+Fh] [rbp-29h] BYREF

v4 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v4 = dialog_quit_confirm(a1, a2);
else
v4 = client_process_input(a1, a2, (unsigned int)ptr[0], a3, a4);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, unsigned int a3, __int64 a4)
{
unsigned int v4; // er14
char ptr[41]; // [rsp+Fh] [rbp-29h] BYREF

v4 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v4 = func2(a1, a2);
else
v4 = client_process_input(a1, a2, (unsigned int)ptr[0], a3, a4);
}
return v4;
}

original_code:
int update_from_input_client ( struct state *st, struct ui *ui, int sfd, struct addrinfo *srv_addr)
{
    int c;
    char buf[1];
    int finished=0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch (c){
        case 'q': case 'Q':
          finished = dialog_quit_confirm(st, ui);
          break;
        default:
          finished = client_process_input (st, ui, c, sfd, srv_addr);
      }
    }
    return finished;
}

canonical_original_code:
int update_from_input_client(struct state *st, struct ui *ui, int sfd, struct addrinfo *srv_addr)
{
    int c;
    char buf[1];
    int finished = 0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch (c){
        case 'q': case 'Q':
          finished = dialog_quit_confirm(st, ui);
          break;
        default:
          finished = client_process_input (st, ui, c, sfd, srv_addr);
      }
    }
    return finished;
}

variable_types: sfd int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
void __fastcall run_client(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // er14
unsigned int v6; // er12
int v7; // ebx
int fd; // [rsp+Ch] [rbp-5Ch] BYREF
char v9[88]; // [rsp+10h] [rbp-58h] BYREF

if ( !(unsigned int)client_init_session(&fd, a5, v9, a3, a4) )
{
v5 = 0;
v6 = 0;
fcntl(fd, 4, 2048LL);
*(_QWORD *)(a1 + 160864) = 0LL;
while ( 1 )
{
if ( !time_to_redraw )
goto LABEL_9;
time_to_redraw = 0;
if ( (int)++v6 > 1599 )
{
v6 = 0;
}
else if ( __ROR4__(-1030792151 * v6, 1) > 0x51EB851u )
{
goto LABEL_5;
}
send_msg_c((unsigned int)fd, v9, 30LL, 0LL, 0LL, 0LL);
LABEL_5:
if ( (unsigned int)client_receive_msg_s((unsigned int)fd, a1) == 10 )
{
if ( !v5 )
ui_init(a1, a2);
}
else if ( !v5 )
{
goto LABEL_9;
}
v5 = 1;
output_grid(a1, a2, v6);
win_or_lose_message(a1, v6);
LABEL_9:
v7 = update_from_input_client(a1, a2, fd, (__int64)v9);
pause();
if ( v7 )
{
close(fd);
return;
}
}
}
perror("Failed to initialize networking");
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // er14
unsigned int v6; // er12
int v7; // ebx
int fd; // [rsp+Ch] [rbp-5Ch] BYREF
char v9[88]; // [rsp+10h] [rbp-58h] BYREF

if ( !(unsigned int)client_init_session(&fd, a5, v9, a3, a4) )
{
v5 = 0;
v6 = 0;
fcntl(fd, 4, 2048LL);
*(_QWORD *)(a1 + 160864) = 0LL;
while ( 1 )
{
if ( !time_to_redraw )
goto LABEL_9;
time_to_redraw = 0;
if ( (int)++v6 > 1599 )
{
v6 = 0;
}
else if ( __ROR4__(-1030792151 * v6, 1) > 0x51EB851u )
{
goto LABEL_5;
}
send_msg_c((unsigned int)fd, v9, 30LL, 0LL, 0LL, 0LL);
LABEL_5:
if ( (unsigned int)client_receive_msg_s((unsigned int)fd, a1) == 10 )
{
if ( !v5 )
ui_init(a1, a2);
}
else if ( !v5 )
{
goto LABEL_9;
}
v5 = 1;
output_grid(a1, a2, v6);
func1(a1, v6);
LABEL_9:
v7 = func5(a1, a2, fd, (__int64)v9);
pause();
if ( v7 )
{
close(fd);
return;
}
}
}
perror("Failed to initialize networking");
}

original_code:
void run_client (struct state *st, struct ui *ui, char *s_server_addr, char *s_server_port, char *s_client_port) {
  int sfd;
  int ret_code;
  struct addrinfo srv_addr;
  if ((ret_code = client_init_session
        (&sfd, s_client_port, &srv_addr, s_server_addr, s_server_port)) != 0) {
    perror("Failed to initialize networking");
    return;
  }
  fcntl(sfd, 4, 04000);
  int k = 0;
  int finished = 0;
  int initialized = 0;
  st->time = 0;
  while( !finished ) {
    if (time_to_redraw) {
      k++;
      if (k>=1600) k=0;
      time_to_redraw = 0;
      if (k%50 == 0)
        send_msg_c (sfd, &srv_addr, 30, 0, 0, 0);
      int msg = client_receive_msg_s (sfd, st);
      if (msg == 10 && initialized != 1) {
        initialized = 1;
        ui_init(st, ui);
      }
      if (initialized) {
        output_grid(st, ui, k);
        win_or_lose_message(st, k);
      }
    }
    finished = update_from_input_client(st, ui, sfd, &srv_addr);
    pause();
  }
  close(sfd);
}

canonical_original_code:
void run_client(struct state *st, struct ui *ui, char *s_server_addr, char *s_server_port, char *s_client_port) {
  int sfd;
  int ret_code;
  struct addrinfo srv_addr;
  if ((ret_code = client_init_session
        (&sfd, s_client_port, &srv_addr, s_server_addr, s_server_port)) != 0) {
    perror("Failed to initialize networking");
    return;
  }
  fcntl(sfd, 4, 04000);
  int k = 0;
  int finished = 0;
  int initialized = 0;
  st->time = 0;
  while( !finished ) {
    if (time_to_redraw) {
      k++;
      if (k>=1600) k=0;
      time_to_redraw = 0;
      if (k%50 == 0)
        send_msg_c (sfd, &srv_addr, 30, 0, 0, 0);
      int msg = client_receive_msg_s (sfd, st);
      if (msg == 10 && initialized != 1) {
        initialized = 1;
        ui_init(st, ui);
      }
      if (initialized) {
        output_grid(st, ui, k);
        win_or_lose_message(st, k);
      }
    }
    finished = update_from_input_client(st, ui, sfd, &srv_addr);
    pause();
  }
  close(sfd);
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

variable_types: initialized int

variable_types: msg int


decompiled_code:
__int64 update_from_input_server()
{
unsigned int v0; // er12
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

v0 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v0 = 1;
}
return v0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // er12
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

v0 = 0;
while ( fread(ptr, 1uLL, 1uLL, stdin) == 1 )
{
if ( (ptr[0] & 0xDF) == 81 )
v0 = 1;
}
return v0;
}

original_code:
int update_from_input_server ( struct state *st )
{
    int c;
    char buf[1];
    int finished=0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch (c) {
        case 'q': case 'Q': finished = 1; break;
      }
    }
    return finished;
}

canonical_original_code:
int update_from_input_server(struct state *st)
{
    int c;
    char buf[1];
    int finished = 0;
    while ( fread(buf, 1, 1, stdin) == 1 ) {
      c = buf[0];
      switch (c) {
        case 'q': case 'Q': finished = 1; break;
      }
    }
    return finished;
}


original_code:
void run_server (struct state *st, int cl_num_need, char *s_server_port) {
  int sfd;
  struct sockaddr_storage peer_addr;
  socklen_t peer_addr_len = sizeof(struct sockaddr_storage);
  ssize_t nread;
  uint8_t buf[50000];
  int ret_code;
  if ((ret_code = server_init (&sfd, s_server_port)) != 0) {
    perror("Failed to initialize networking");
    return;
  }
  fcntl(sfd, 4, 04000);
  int cl_num = 0;
  struct client_record cl[8];
  enum server_mode mode = server_mode_lobby;
  int finished = 0;
  int k=0;
  while( !finished ) {
    if (time_to_redraw) {
      switch(mode){
        case server_mode_lobby:
          nread = recvfrom(sfd, buf, 50000 -1, 0,
              (struct sockaddr *) &peer_addr, &peer_addr_len);
          if ( server_get_msg(buf, nread) > 0 ) {
            int i;
            int found = 0;
            for(i=0; i<cl_num; ++i) {
              found = found || sa_match(&peer_addr, &cl[i].sa);
            }
            if (!found && cl_num < cl_num_need) {
              cl[cl_num].name = "Jim";
              cl[cl_num].id = cl_num;
              cl[cl_num].pl = cl_num + 1;
              cl[cl_num].sa = peer_addr;
              waddnstr(stdscr,("!"),-1);
              wrefresh(stdscr);
              cl_num++;
            }
            if (cl_num >= cl_num_need) {
              mode = server_mode_play;
              cl_num = cl_num_need;
            }
          }
          break;
        case server_mode_play:
          k++;
          if (k>=1600) k=0;
          int slowdown = game_slowdown(st->speed);
          if (k % slowdown == 0 && st->speed != sp_pause) {
            kings_move(st);
            simulate(st);
            server_send_msg_s_state(sfd, cl, cl_num, st);
          }
          nread = recvfrom(sfd, buf, 50000 -1, 0,
              (struct sockaddr *) &peer_addr, &peer_addr_len);
          if (nread != -1) {
            int found_i = -1;
            int i;
            for(i=0; i<cl_num; ++i) {
              if (sa_match(&peer_addr, &cl[i].sa)) found_i = i;
            }
            if (found_i>-1) {
              int msg = server_process_msg_c(buf, nread, st, cl[found_i].pl);
              if (msg == 30) waddnstr(stdscr,("."),-1);
              else waddnstr(stdscr,("+"),-1);
              wrefresh(stdscr);
            }
          }
          break;
      }
      time_to_redraw = 0;
    }
    finished = update_from_input_server(st);
    pause();
  }
  close(sfd);
}

canonical_original_code:
void run_server(struct state *st, int cl_num_need, char *s_server_port) {
  int sfd;
  struct sockaddr_storage peer_addr;
  unsigned int peer_addr_len = sizeof(struct sockaddr_storage);
  long nread;
  unsigned char buf[50000];
  int ret_code;
  if ((ret_code = server_init (&sfd, s_server_port)) != 0) {
    perror("Failed to initialize networking");
    return;
  }
  fcntl(sfd, 4, 04000);
  int cl_num = 0;
  struct client_record cl[8];
  enum server_mode mode = server_mode_lobby;
  int finished = 0;
  int k = 0;
  while( !finished ) {
    if (time_to_redraw) {
      switch(mode){
        case server_mode_lobby:
          nread = recvfrom(sfd, buf, 50000 -1, 0,
              (struct sockaddr *) &peer_addr, &peer_addr_len);
          if ( server_get_msg(buf, nread) > 0 ) {
            int i;
            int found = 0;
            for(i=0; i<cl_num; ++i) {
              found = found || sa_match(&peer_addr, &cl[i].sa);
            }
            if (!found && cl_num < cl_num_need) {
              cl[cl_num].name = "Jim";
              cl[cl_num].id = cl_num;
              cl[cl_num].pl = cl_num + 1;
              cl[cl_num].sa = peer_addr;
              waddnstr(stdscr,("!"),-1);
              wrefresh(stdscr);
              cl_num++;
            }
            if (cl_num >= cl_num_need) {
              mode = server_mode_play;
              cl_num = cl_num_need;
            }
          }
          break;
        case server_mode_play:
          k++;
          if (k>=1600) k=0;
          int slowdown = game_slowdown(st->speed);
          if (k % slowdown == 0 && st->speed != sp_pause) {
            kings_move(st);
            simulate(st);
            server_send_msg_s_state(sfd, cl, cl_num, st);
          }
          nread = recvfrom(sfd, buf, 50000 -1, 0,
              (struct sockaddr *) &peer_addr, &peer_addr_len);
          if (nread != -1) {
            int found_i = -1;
            int i;
            for(i=0; i<cl_num; ++i) {
              if (sa_match(&peer_addr, &cl[i].sa)) found_i = i;
            }
            if (found_i>-1) {
              int msg = server_process_msg_c(buf, nread, st, cl[found_i].pl);
              if (msg == 30) waddnstr(stdscr,("."),-1);
              else waddnstr(stdscr,("+"),-1);
              wrefresh(stdscr);
            }
          }
          break;
      }
      time_to_redraw = 0;
    }
    finished = update_from_input_server(st);
    pause();
  }
  close(sfd);
}

variable_types: nread long

variable_types:
buf
unsigned char [50000]

variable_types: struct client_record { int pl; int id; char *name; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } sa; } [8]

variable_types: enum server_mode { server_mode_lobby=0, server_mode_play=1 }

variable_types: i int

variable_types: found int

user_defined_types: struct client_record { int pl; int id; char *name; struct sockaddr_storage sa; }

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int result; // eax
__int64 v5; // rcx
sigset_t *v6; // rsi
__sigset_t *v7; // rdi
unsigned int v8; // eax
__int64 v9; // rdx
unsigned int v10; // er12
_BYTE v11[16]; // [rsp+0h] [rbp-27658h] BYREF
struct itimerval newa; // [rsp+10h] [rbp-27648h] BYREF
int v13; // [rsp+30h] [rbp-27628h] BYREF
int v14; // [rsp+34h] [rbp-27624h]
void *v15; // [rsp+38h] [rbp-27620h]
void *ptr; // [rsp+40h] [rbp-27618h]
void *v17; // [rsp+48h] [rbp-27610h]
int v18; // [rsp+50h] [rbp-27608h]
char v19[48]; // [rsp+60h] [rbp-275F8h] BYREF
sigset_t set; // [rsp+90h] [rbp-275C8h] BYREF
struct sigaction v21; // [rsp+110h] [rbp-27548h] BYREF
char v22[160872]; // [rsp+1B0h] [rbp-274A8h] BYREF
unsigned int v23; // [rsp+27618h] [rbp-40h]

v3 = time(0LL);
srand(v3);
result = get_options((unsigned int)argc, argv, v19, &v13);
if ( result != 1 )
{
v21.sa_flags = 0x10000000;
sigemptyset(&set);
v5 = 32LL;
v6 = &set;
v7 = &v21.sa_mask;
v21.sa_handler = (__sighandler_t)on_timer;
while ( v5 )
{
LODWORD(v7->__val[0]) = v6->__val[0];
v6 = (sigset_t *)((char *)v6 + 4);
v7 = (__sigset_t *)((char *)v7 + 4);
--v5;
}
if ( sigaction(14, &v21, 0LL) == -1 )
perror("sigaction");
setlocale(6, &locale);
initscr();
cbreak();
noecho();
start_color();
wclear(stdscr);
curs_set(0);
use_default_colors();
init_pair(0, 7, 0);
init_pair(1, 7, 0);
init_pair(2, 0, 0);
init_pair(3, 1, 0);
init_pair(4, 2, 0);
init_pair(5, 4, 0);
init_pair(6, 3, 0);
init_pair(7, 5, 0);
init_pair(8, 6, 0);
wcolor_set(stdscr, 0, 0LL);
assume_default_colors(7, 0);
wclear(stdscr);
wattrset(stdscr, 2097664);
if ( wmove(stdscr, 0, 0) != -1 )
waddnstr(stdscr, "Map is generated. Please wait.", -1);
wrefresh(stdscr);
state_init(v22, v19, &v13);
ui_init(v22, v11);
wclear(stdscr);
v8 = fcntl(0, 3);
v9 = v8;
v10 = v8;
BYTE1(v9) = BYTE1(v8) | 8;
fcntl(0, 4, v9);
newa.it_value.tv_sec = 0LL;
newa.it_value.tv_usec = 10000LL;
newa.it_interval.tv_sec = 0LL;
newa.it_interval.tv_usec = 10000LL;
setitimer(ITIMER_REAL, &newa, 0LL);
wrefresh(stdscr);
input_ready = 0;
time_to_redraw = 1;
if ( v13 )
{
if ( v14 )
run_server((__int64)v22, v18, (__int64)v17);
else
run_client((__int64)v22, (__int64)v11, (__int64)ptr, (__int64)v17, (__int64)v15);
}
else
{
run((__int64)v22, (__int64)v11);
}
echo();
curs_set(1);
wclear(stdscr);
endwin();
fcntl(0, 4, v10);
if ( !v13 || v14 )
printf("Random seed was %i\n", v23);
free(ptr);
free(v17);
free(v15);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int result; // eax
__int64 v5; // rcx
sigset_t *v6; // rsi
__sigset_t *v7; // rdi
unsigned int v8; // eax
__int64 v9; // rdx
unsigned int v10; // er12
_BYTE v11[16]; // [rsp+0h] [rbp-27658h] BYREF
struct itimerval newa; // [rsp+10h] [rbp-27648h] BYREF
int v13; // [rsp+30h] [rbp-27628h] BYREF
int v14; // [rsp+34h] [rbp-27624h]
void *v15; // [rsp+38h] [rbp-27620h]
void *ptr; // [rsp+40h] [rbp-27618h]
void *v17; // [rsp+48h] [rbp-27610h]
int v18; // [rsp+50h] [rbp-27608h]
char v19[48]; // [rsp+60h] [rbp-275F8h] BYREF
sigset_t set; // [rsp+90h] [rbp-275C8h] BYREF
struct sigaction v21; // [rsp+110h] [rbp-27548h] BYREF
char v22[160872]; // [rsp+1B0h] [rbp-274A8h] BYREF
unsigned int v23; // [rsp+27618h] [rbp-40h]

v3 = time(0LL);
srand(v3);
result = get_options((unsigned int)argc, argv, v19, &v13);
if ( result != 1 )
{
v21.sa_flags = 0x10000000;
sigemptyset(&set);
v5 = 32LL;
v6 = &set;
v7 = &v21.sa_mask;
v21.sa_handler = (__sighandler_t)on_timer;
while ( v5 )
{
LODWORD(v7->__val[0]) = v6->__val[0];
v6 = (sigset_t *)((char *)v6 + 4);
v7 = (__sigset_t *)((char *)v7 + 4);
--v5;
}
if ( sigaction(14, &v21, 0LL) == -1 )
perror("sigaction");
setlocale(6, &locale);
initscr();
cbreak();
noecho();
start_color();
wclear(stdscr);
curs_set(0);
use_default_colors();
init_pair(0, 7, 0);
init_pair(1, 7, 0);
init_pair(2, 0, 0);
init_pair(3, 1, 0);
init_pair(4, 2, 0);
init_pair(5, 4, 0);
init_pair(6, 3, 0);
init_pair(7, 5, 0);
init_pair(8, 6, 0);
wcolor_set(stdscr, 0, 0LL);
assume_default_colors(7, 0);
wclear(stdscr);
wattrset(stdscr, 2097664);
if ( wmove(stdscr, 0, 0) != -1 )
waddnstr(stdscr, "Map is generated. Please wait.", -1);
wrefresh(stdscr);
state_init(v22, v19, &v13);
ui_init(v22, v11);
wclear(stdscr);
v8 = fcntl(0, 3);
v9 = v8;
v10 = v8;
BYTE1(v9) = BYTE1(v8) | 8;
fcntl(0, 4, v9);
newa.it_value.tv_sec = 0LL;
newa.it_value.tv_usec = 10000LL;
newa.it_interval.tv_sec = 0LL;
newa.it_interval.tv_usec = 10000LL;
setitimer(ITIMER_REAL, &newa, 0LL);
wrefresh(stdscr);
input_ready = 0;
time_to_redraw = 1;
if ( v13 )
{
if ( v14 )
func8((__int64)v22, v18, (__int64)v17);
else
func6((__int64)v22, (__int64)v11, (__int64)ptr, (__int64)v17, (__int64)v15);
}
else
{
func4((__int64)v22, (__int64)v11);
}
echo();
curs_set(1);
wclear(stdscr);
endwin();
fcntl(0, 4, v10);
if ( !v13 || v14 )
printf("Random seed was %i\n", v23);
free(ptr);
free(v17);
free(v15);
result = 0;
}
return result;
}

original_code:
int main(int argc, char* argv[]){
  srand(time(((void *)0)));
  struct basic_options op;
  struct multi_options mop;
  if (get_options(argc, argv, &op, &mop) == 1) return 1;
  struct sigaction newhandler;
  sigset_t blocked;
  newhandler.sa_flags = 0x10000000;
  sigemptyset(&blocked);
  newhandler.sa_mask = blocked;
  newhandler.__sigaction_handler.sa_handler = on_timer;
  if ( sigaction(14, &newhandler, ((void *)0)) == -1 )
    perror("sigaction");
  setlocale(6, "");
  initscr();
  cbreak();
  noecho();
  start_color();
  wclear(stdscr);
  curs_set(0);
  use_default_colors();
  init_pair(0, 7, 0);
  init_pair(1, 7, 0);
  init_pair(2, 0, 0);
  init_pair(3, 1, 0);
  init_pair(4, 2, 0);
  init_pair(5, 4, 0);
  init_pair(6, 3, 0);
  init_pair(7, 5, 0);
  init_pair(8, 6, 0);
  wcolor_set(stdscr,(0),(((void *)0)));
  assume_default_colors(7, 0);
  wclear(stdscr);
  struct state st;
  struct ui ui;
  wattrset(stdscr,(((chtype)((1U)) << ((13) + 8)) | (((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8)))));
  (wmove((stdscr),((0)),((0))) == (-1) ? (-1) : waddnstr((stdscr),(("Map is generated. Please wait.")),-1));
  wrefresh(stdscr);
  state_init(&st, &op, &mop);
  ui_init(&st, &ui);
  wclear(stdscr);
  int fd_flags = fcntl(0, 3);
  fcntl(0, 4, (fd_flags|04000));
  struct itimerval it;
  it.it_value.tv_sec = 0;
  it.it_value.tv_usec = 10000;
  it.it_interval.tv_sec = 0;
  it.it_interval.tv_usec = 10000;
  setitimer(ITIMER_REAL, &it, ((void *)0));
  wrefresh(stdscr);
  input_ready = 0;
  time_to_redraw = 1;
  if (!mop.multiplayer_flag) {
    run(&st, &ui);
  }
  else {
    if (mop.server_flag) run_server(&st, mop.clients_num, mop.val_server_port);
    else run_client(&st, &ui, mop.val_server_addr, mop.val_server_port, mop.val_client_port);
  }
  echo();
  curs_set(1);
  wclear(stdscr);
  endwin();
  fcntl(0, 4, fd_flags);
  if (!mop.multiplayer_flag || mop.server_flag)
    printf ("Random seed was %i\n", st.map_seed);
  free(mop.val_server_addr);
  free(mop.val_server_port);
  free(mop.val_client_port);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  srand(time(((void *)0)));
  struct basic_options op;
  struct multi_options mop;
  if (get_options(argc, argv, &op, &mop) == 1) return 1;
  struct sigaction newhandler;
  struct __sigset_t blocked;
  newhandler.sa_flags = 0x10000000;
  sigemptyset(&blocked);
  newhandler.sa_mask = blocked;
  newhandler.__sigaction_handler.sa_handler = on_timer;
  if ( sigaction(14, &newhandler, ((void *)0)) == -1 )
    perror("sigaction");
  setlocale(6, "");
  initscr();
  cbreak();
  noecho();
  start_color();
  wclear(stdscr);
  curs_set(0);
  use_default_colors();
  init_pair(0, 7, 0);
  init_pair(1, 7, 0);
  init_pair(2, 0, 0);
  init_pair(3, 1, 0);
  init_pair(4, 2, 0);
  init_pair(5, 4, 0);
  init_pair(6, 3, 0);
  init_pair(7, 5, 0);
  init_pair(8, 6, 0);
  wcolor_set(stdscr,(0),(((void *)0)));
  assume_default_colors(7, 0);
  wclear(stdscr);
  struct state st;
  struct ui ui;
  wattrset(stdscr,(((chtype)((1U)) << ((13) + 8)) | (((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8)))));
  (wmove((stdscr),((0)),((0))) == (-1) ? (-1) : waddnstr((stdscr),(("Map is generated. Please wait.")),-1));
  wrefresh(stdscr);
  state_init(&st, &op, &mop);
  ui_init(&st, &ui);
  wclear(stdscr);
  int fd_flags = fcntl(0, 3);
  fcntl(0, 4, (fd_flags|04000));
  struct itimerval it;
  it.it_value.tv_sec = 0;
  it.it_value.tv_usec = 10000;
  it.it_interval.tv_sec = 0;
  it.it_interval.tv_usec = 10000;
  setitimer(ITIMER_REAL, &it, ((void *)0));
  wrefresh(stdscr);
  input_ready = 0;
  time_to_redraw = 1;
  if (!mop.multiplayer_flag) {
    run(&st, &ui);
  }
  else {
    if (mop.server_flag) run_server(&st, mop.clients_num, mop.val_server_port);
    else run_client(&st, &ui, mop.val_server_addr, mop.val_server_port, mop.val_client_port);
  }
  echo();
  curs_set(1);
  wclear(stdscr);
  endwin();
  fcntl(0, 4, fd_flags);
  if (!mop.multiplayer_flag || mop.server_flag)
    printf ("Random seed was %i\n", st.map_seed);
  free(mop.val_server_addr);
  free(mop.val_server_port);
  free(mop.val_client_port);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct basic_options { int keep_random_flag; enum config_dif { dif_easiest=0, dif_easy=1, dif_normal=2, dif_hard=3, dif_hardest=4 } dif; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } speed; int w; int h; int loc_num; unsigned int map_seed; int conditions; int timeline_flag; int inequality; enum stencil { st_rhombus=0, st_rect=1, st_hex=2 } shape; }

variable_types: struct multi_options { int multiplayer_flag; int server_flag; char *val_client_port; char *val_server_addr; char *val_server_port; int clients_num; }

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 state { struct grid { int width; int height; struct tile { enum tile_class { abyss=0, mountain=1, mine=2, grassland=3, village=4, town=5, castle=6 } cl; int pl; int units[8][1]; } tiles[40][29]; } grid; struct flag_grid { int width; int height; int flag[40][29]; int call[40][29]; } fg[8]; struct king { int value[40][29]; int pl; enum strategy { none=0, aggr_greedy=1, one_greedy=2, persistent_greedy=3, opportunist=4, noble=5, midas=6 } strategy; } king[8]; int kings_num; struct timeline { float data[8][72]; unsigned long time[72]; int mark; } timeline; int show_timeline; struct country { long gold; } country[8]; unsigned long time; int map_seed; int controlled; int conditions; int inequality; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } speed; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } prev_speed; enum config_dif { dif_easiest=0, dif_easy=1, dif_normal=2, dif_hard=3, dif_hardest=4 } dif; }

variable_types: struct ui { struct loc { int i; int j; } cursor; int xskip; int xlength; }

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

user_defined_types: struct basic_options { int keep_random_flag; enum config_dif dif; enum config_speed speed; int w; int h; int loc_num; unsigned int map_seed; int conditions; int timeline_flag; int inequality; enum stencil shape; }

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 itimerval { struct timeval it_interval; struct timeval it_value; }

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:
int print_help()
{
puts(
" __ \n"
" ____ / ] \n"
" / __ \\_ _ ___ ___ ___ __ _| |_ /\\ /\\___ ___ \n"
" _/ / \\/ | |X _/ __/ __\\ / \\ / \\ \\ /\\ / /__ \\X _/ \n"
" \\ X | | | | |__ | __X | X || | \\ V V // _ | | \n"
" \\ \\__/\\ __X_| \\___/___/ \\___/| | \\ /\\ / \\___X_| \n"
" \\____/ |/ V V \n"
"\n"
" Written by Alexey Nikolaev in 2013.\n");
puts(" Command line arguments:\n");
return puts(
"-W width\n"
"\tMap width (default is 21)\n"
"\n"
"-H height\n"
"\tMap height (default is 21)\n"
"\n"
"-S [rhombus|rect|hex]\n"
"\tMap shape (rectangle is default). Max number of countries N=4 for rhombus and rectangle, and N=6 for the he"
"xagon.\n"
"\n"
"-l [2|3| ... N]\n"
"\tSets L, the number of countries (default is N).\n"
"\n"
"-i [0|1|2|3|4]\n"
"\tInequality between the countries (0 is the lowest, 4 in the highest).\n"
"\n"
"-q [1|2| ... L]\n"
"\tChoose player's location by its quality (1 = the best available on the map, L = the worst). Only in the sin"
"gleplayer mode.\n"
"\n"
"-r\n"
"\tAbsolutely random initial conditions, overrides options -l, -i, and -q.\n"
"\n"
"-d [ee|e|n|h|hh]\n"
"\tDifficulty level (AI) from the easiest to the hardest (default is normal).\n"
"\n"
"-s [p|sss|ss|s|n|f|ff|fff]\n"
"\tGame speed from the slowest to the fastest (default is normal).\n"
"\n"
"-R seed\n"
"\tSpecify a random seed (unsigned integer) for map generation.\n"
"\n"
"-T\n"
"\tShow the timeline.\n"
"\n"
"-E [1|2| ... L]\n"
"\tStart a server for not more than L clients.\n"
"\n"
"-e port\n"
"\tServer's port (19140 is default).\n"
"\n"
"-C IP\n"
"\tStart a client and connect to the provided server's IP-address.\n"
"\n"
"-c port\n"
"\tClients's port (19150 is default).\n"
"\n"
"-v\n"
"\tDisplay the version number\n"
"\n"
"-h\n"
"\tDisplay this help \n");
}

canonical_decompiled_code:
int func0()
{
puts(
" __ \n"
" ____ / ] \n"
" / __ \\_ _ ___ ___ ___ __ _| |_ /\\ /\\___ ___ \n"
" _/ / \\/ | |X _/ __/ __\\ / \\ / \\ \\ /\\ / /__ \\X _/ \n"
" \\ X | | | | |__ | __X | X || | \\ V V // _ | | \n"
" \\ \\__/\\ __X_| \\___/___/ \\___/| | \\ /\\ / \\___X_| \n"
" \\____/ |/ V V \n"
"\n"
" Written by Alexey Nikolaev in 2013.\n");
puts(" Command line arguments:\n");
return puts(
"-W width\n"
"\tMap width (default is 21)\n"
"\n"
"-H height\n"
"\tMap height (default is 21)\n"
"\n"
"-S [rhombus|rect|hex]\n"
"\tMap shape (rectangle is default). Max number of countries N=4 for rhombus and rectangle, and N=6 for the he"
"xagon.\n"
"\n"
"-l [2|3| ... N]\n"
"\tSets L, the number of countries (default is N).\n"
"\n"
"-i [0|1|2|3|4]\n"
"\tInequality between the countries (0 is the lowest, 4 in the highest).\n"
"\n"
"-q [1|2| ... L]\n"
"\tChoose player's location by its quality (1 = the best available on the map, L = the worst). Only in the sin"
"gleplayer mode.\n"
"\n"
"-r\n"
"\tAbsolutely random initial conditions, overrides options -l, -i, and -q.\n"
"\n"
"-d [ee|e|n|h|hh]\n"
"\tDifficulty level (AI) from the easiest to the hardest (default is normal).\n"
"\n"
"-s [p|sss|ss|s|n|f|ff|fff]\n"
"\tGame speed from the slowest to the fastest (default is normal).\n"
"\n"
"-R seed\n"
"\tSpecify a random seed (unsigned integer) for map generation.\n"
"\n"
"-T\n"
"\tShow the timeline.\n"
"\n"
"-E [1|2| ... L]\n"
"\tStart a server for not more than L clients.\n"
"\n"
"-e port\n"
"\tServer's port (19140 is default).\n"
"\n"
"-C IP\n"
"\tStart a client and connect to the provided server's IP-address.\n"
"\n"
"-c port\n"
"\tClients's port (19150 is default).\n"
"\n"
"-v\n"
"\tDisplay the version number\n"
"\n"
"-h\n"
"\tDisplay this help \n");
}

original_code:
void print_help() {
  printf(
"                                 __                      \n"
"    ____                        /  ]                     \n"
"   / __ \\_ _ ___ ___ ___    __ _| |_  /\\      /\\___ ___  \n"
" _/ /  \\/ | |X _/ __/ __\\  /   \\   /  \\ \\ /\\ / /__ \\X _/ \n"
" \\ X    | | | | |__ | __X  | X || |    \\ V  V // _ | |   \n"
"  \\ \\__/\\ __X_| \\___/___/  \\___/| |     \\ /\\ / \\___X_|   \n"
"   \\____/                       |/       V  V            \n"
"\n"
"  Written by Alexey Nikolaev in 2013.\n"
"\n");
  printf("  Command line arguments:\n\n");
  printf(
    "-W width\n"
    "\tMap width (default is 21)\n\n"
    "-H height\n"
    "\tMap height (default is 21)\n\n"
    "-S [rhombus|rect|hex]\n"
    "\tMap shape (rectangle is default). Max number of countries N=4 for rhombus and rectangle, and N=6 for the hexagon.\n\n"
    "-l [2|3| ... N]\n"
    "\tSets L, the number of countries (default is N).\n\n"
    "-i [0|1|2|3|4]\n"
    "\tInequality between the countries (0 is the lowest, 4 in the highest).\n\n"
    "-q [1|2| ... L]\n"
    "\tChoose player's location by its quality (1 = the best available on the map, L = the worst). Only in the singleplayer mode.\n\n"
    "-r\n"
    "\tAbsolutely random initial conditions, overrides options -l, -i, and -q.\n\n"
    "-d [ee|e|n|h|hh]\n"
    "\tDifficulty level (AI) from the easiest to the hardest (default is normal).\n\n"
    "-s [p|sss|ss|s|n|f|ff|fff]\n"
    "\tGame speed from the slowest to the fastest (default is normal).\n\n"
    "-R seed\n"
    "\tSpecify a random seed (unsigned integer) for map generation.\n\n"
    "-T\n"
    "\tShow the timeline.\n\n"
    "-E [1|2| ... L]\n"
    "\tStart a server for not more than L clients.\n\n"
    "-e port\n"
    "\tServer's port (19140 is default).\n\n"
    "-C IP\n"
    "\tStart a client and connect to the provided server's IP-address.\n\n"
    "-c port\n"
    "\tClients's port (19150 is default).\n\n"
    "-v\n"
    "\tDisplay the version number\n\n"
    "-h\n"
    "\tDisplay this help \n\n"
  );
}


original_code:
int win_or_lose(struct state *st) {
  int i, j, p;
  int pop[8];
  for (p=0; p<8; ++p) pop[p] = 0;
  for(i=0; i<st->grid.width; ++i){
    for(j=0; j<st->grid.height; ++j){
      if(is_inhabitable(st->grid.tiles[i][j].cl)) {
        for(p=0; p<8; ++p){
          pop[p] += st->grid.tiles[i][j].units[p][citizen];
        }
      }
    }
  }
  int win = 1;
  int lose = 0;
  int best = 0;
  for(p=0; p<8; ++p){
    if(pop[best] < pop[p]) best = p;
    if(p!=st->controlled && pop[p]>0) win = 0;
  }
  if(pop[st->controlled] == 0) lose = 1;
  if (win) return 1;
  else if (lose) return -1;
  else return 0;
}

canonical_original_code:
int win_or_lose(struct state *st) {
  int i; int j; int p;
  int pop[8];
  for (p=0; p<8; ++p) pop[p] = 0;
  for(i=0; i<st->grid.width; ++i){
    for(j=0; j<st->grid.height; ++j){
      if(is_inhabitable(st->grid.tiles[i][j].cl)) {
        for(p=0; p<8; ++p){
          pop[p] += st->grid.tiles[i][j].units[p][citizen];
        }
      }
    }
  }
  int win = 1;
  int lose = 0;
  int best = 0;
  for(p=0; p<8; ++p){
    if(pop[best] < pop[p]) best = p;
    if(p!=st->controlled && pop[p]>0) win = 0;
  }
  if(pop[st->controlled] == 0) lose = 1;
  if (win) return 1;
  else if (lose) return -1;
  else return 0;
}

variable_types: struct state { struct grid { int width; int height; struct tile { enum tile_class { abyss=0, mountain=1, mine=2, grassland=3, village=4, town=5, castle=6 } cl; int pl; int units[8][1]; } tiles[40][29]; } grid; struct flag_grid { int width; int height; int flag[40][29]; int call[40][29]; } fg[8]; struct king { int value[40][29]; int pl; enum strategy { none=0, aggr_greedy=1, one_greedy=2, persistent_greedy=3, opportunist=4, noble=5, midas=6 } strategy; } king[8]; int kings_num; struct timeline { float data[8][72]; unsigned long time[72]; int mark; } timeline; int show_timeline; struct country { long gold; } country[8]; unsigned long time; int map_seed; int controlled; int conditions; int inequality; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } speed; enum config_speed { sp_pause=0, sp_slowest=1, sp_slower=2, sp_slow=3, sp_normal=4, sp_fast=5, sp_faster=6, sp_fastest=7 } prev_speed; enum config_dif { dif_easiest=0, dif_easy=1, dif_normal=2, dif_hard=3, dif_hardest=4 } dif; } *

variable_types: j int

variable_types: p int

variable_types:
pop
int [8]

variable_types: win int

variable_types: lose int

variable_types: best int

user_defined_types: struct state { struct grid grid; struct flag_grid fg[8]; struct king king[8]; int kings_num; struct timeline timeline; int show_timeline; struct country country[8]; unsigned long time; int map_seed; int controlled; int conditions; int inequality; enum config_speed speed; enum config_speed prev_speed; enum config_dif dif; }

user_defined_types: struct grid { int width; int height; struct tile tiles[40][29]; }