battle

decompiled_code:
__int64 __fastcall battle(int a1, int a2)
{
__int64 v2; // r13
unsigned int v3; // er15
__int64 v4; // rdx
unsigned int v5; // er13
int *v6; // rbx
__int64 i; // rbp
int v8; // eax
const char *v9; // rdx
int v10; // eax
_BOOL4 v11; // ebx
unsigned int v12; // edi
__int64 result; // rax
const char *v14; // rax
const char *v15; // rax
__int64 v16; // rdi
int v17; // ebp
__int64 v18; // r14
const char *v19; // rax
__int64 v20; // rdi
int v21; // er12
int v22; // er13
_BOOL4 v23; // ebx
_BOOL4 v24; // er14
const char *v25; // r14
const char *v26; // rax
unsigned int v27; // [rsp+8h] [rbp-170h]
const char *v28; // [rsp+8h] [rbp-170h]
unsigned int v29; // [rsp+10h] [rbp-168h]
unsigned int v30; // [rsp+14h] [rbp-164h]
const char *v31; // [rsp+18h] [rbp-160h]
char s[128]; // [rsp+20h] [rbp-158h] BYREF
const char *v33; // [rsp+A0h] [rbp-D8h]
const char *v34; // [rsp+A8h] [rbp-D0h]
unsigned int v35; // [rsp+B8h] [rbp-C0h]
unsigned int v36; // [rsp+BCh] [rbp-BCh]
int v37[2]; // [rsp+C0h] [rbp-B8h] BYREF
int v38[14]; // [rsp+C8h] [rbp-B0h]
int v39[30]; // [rsp+100h] [rbp-78h] BYREF

v2 = 30LL * a1;
v35 = *(__int16 *)(armies + v2 + 16);
v36 = *(__int16 *)(armies + 30LL * a2 + 16);
v3 = v36;
v29 = v35;
v33 = (const char *)nationcity(v35);
v34 = (const char *)nationcity(v36);
v5 = city_at((unsigned int)*(__int16 *)(armies + v2 + 18), (unsigned int)*(__int16 *)(armies + v2 + 20), v4);
v30 = v5;
sprintf(s, "Battle between %s and %s", v33, v34);
message_out(s, v29, v3, 1LL);
if ( v5 != -1 )
{
sprintf(s, " in %s", &cities[96 * v5]);
message_out(s, v29, v3, 1LL);
}
sprintf(s, " (Turn %d)", gen);
message_out(s, v29, v3, 0LL);
message_out("\n", v29, v3, 1LL);
make_stack(v37, a1);
make_stack(v39, a2);
sprintf(s, "(Odds: %d to %d)\n", (unsigned int)v37[0], (unsigned int)v39[0]);
message_out(s, v29, v3, 0LL);
v6 = v37;
for ( i = 0LL; i != 2; ++i )
{
sprintf(s, "%s has %d armies", (&v33)[i], (unsigned int)v6[2]);
message_out(s, v29, v3, 1LL);
v8 = v6[1];
if ( v8 == -1 )
{
message_out("\n", v29, v3, 1LL);
}
else
{
v9 = (const char *)(armies + 30LL * v37[16 * (int)i + 4 + v8]);
if ( !*v9 )
v9 = "a nameless hero";
sprintf(s, " led by %s\n", v9);
message_out(s, v29, v3, 1LL);
}
v6 += 16;
}
v10 = v38[0];
if ( v38[0] > 0 )
{
while ( 1 )
{
if ( v39[2] <= 0 )
goto LABEL_10;
v21 = roll((unsigned int)(v37[0] + v39[0]));
v22 = v37[0];
v23 = v21 < v37[0];
v24 = v21 >= v37[0];
if ( v37[16 * (unsigned __int64)(v21 < v37[0])] >= v37[16 * (unsigned __int64)(v21 >= v37[0])] / 2 )
goto LABEL_20;
v17 = v37[16 * (unsigned __int64)(v21 < v37[0]) + 1];
if ( v17 == -1 || *(_BYTE *)(armies + 30LL * v37[16 * (v21 < v37[0]) + 4 + v17] + 24) == 1 )
goto LABEL_20;
if ( (int)roll(10LL) > 4 )
{
sprintf(s, "set-eparm %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], 1LL);
fputs(s, mfile);
execpriv(s);
LABEL_20:
v16 = 16LL;
if ( v21 >= v22 )
v16 = 0LL;
v17 = least((__int64)&v37[v16]);
v28 = (&v33)[v23];
v18 = v17 + 16LL * v23 + 4;
v19 = (const char *)armyname((unsigned int)v37[v18]);
sprintf(s, "%s (%s) is destroyed.\n", v19, v28);
message_out(s, v35, v36, 0LL);
if ( *(_BYTE *)(armies + 30LL * v37[v18] + 23) )
fputs(s, news);
sprintf(s, "kill-army %d\n", (unsigned int)v37[16 * v23 + 4 + v17]);
fputs(s, mfile);
execpriv(s);
goto LABEL_25;
}
if ( v38[16 * (__int64)v24] > 11 || (int)roll(10LL) > 4 )
break;
v27 = *(&v35 + v24);
v31 = (&v33)[v23];
v14 = (const char *)armyname((unsigned int)v37[16 * v23 + 4 + v17]);
sprintf(s, "%s (%s) deserts", v14, v31);
message_out(s, v29, v3, 1LL);
if ( v27 == -1 )
goto LABEL_35;
v15 = (const char *)nationcity(v27);
sprintf(s, " and joins %s\n", v15);
message_out(s, v29, v3, 1LL);
sprintf(s, "kill-army -2 %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], v27);
fputs(s, mfile);
execpriv(s);
make_stack(&v37[16 * (__int64)v24], v17);
LABEL_25:
v20 = 16LL;
if ( v21 >= v22 )
v20 = 0LL;
stack_remove(&v37[v20], v17);
v10 = v38[0];
if ( v38[0] <= 0 )
goto LABEL_10;
}
v25 = (&v33)[v23];
v26 = (const char *)armyname((unsigned int)v37[16 * v23 + 4 + v17]);
sprintf(s, "%s (%s) deserts", v26, v25);
message_out(s, v29, v3, 1LL);
LABEL_35:
message_out("\n", v29, v3, 1LL);
sprintf(s, "kill-army -2 %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], 0xFFFFFFFFLL);
fputs(s, mfile);
execpriv(s);
goto LABEL_25;
}
LABEL_10:
v11 = v10 <= 0;
sprintf(s, "%s wins!\n", (&v33)[v10 <= 0]);
message_out(s, v29, v3, 1LL);
if ( v30 != -1 )
{
v12 = *(&v35 + v11);
if ( *(_DWORD *)&cities[96 * v30 + 24] != v12 )
capture(v12, v30);
}
result = (unsigned int)v37[16 * (__int64)v11 + 1];
if ( (_DWORD)result != -1 )
result = advance(v37[16 * v11 + 4 + (int)result]);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2)
{
__int64 v2; // r13
unsigned int v3; // er15
__int64 v4; // rdx
unsigned int v5; // er13
int *v6; // rbx
__int64 i; // rbp
int v8; // eax
const char *v9; // rdx
int v10; // eax
_BOOL4 v11; // ebx
unsigned int v12; // edi
__int64 result; // rax
const char *v14; // rax
const char *v15; // rax
__int64 v16; // rdi
int v17; // ebp
__int64 v18; // r14
const char *v19; // rax
__int64 v20; // rdi
int v21; // er12
int v22; // er13
_BOOL4 v23; // ebx
_BOOL4 v24; // er14
const char *v25; // r14
const char *v26; // rax
unsigned int v27; // [rsp+8h] [rbp-170h]
const char *v28; // [rsp+8h] [rbp-170h]
unsigned int v29; // [rsp+10h] [rbp-168h]
unsigned int v30; // [rsp+14h] [rbp-164h]
const char *v31; // [rsp+18h] [rbp-160h]
char s[128]; // [rsp+20h] [rbp-158h] BYREF
const char *v33; // [rsp+A0h] [rbp-D8h]
const char *v34; // [rsp+A8h] [rbp-D0h]
unsigned int v35; // [rsp+B8h] [rbp-C0h]
unsigned int v36; // [rsp+BCh] [rbp-BCh]
int v37[2]; // [rsp+C0h] [rbp-B8h] BYREF
int v38[14]; // [rsp+C8h] [rbp-B0h]
int v39[30]; // [rsp+100h] [rbp-78h] BYREF

v2 = 30LL * a1;
v35 = *(__int16 *)(armies + v2 + 16);
v36 = *(__int16 *)(armies + 30LL * a2 + 16);
v3 = v36;
v29 = v35;
v33 = (const char *)nationcity(v35);
v34 = (const char *)nationcity(v36);
v5 = city_at((unsigned int)*(__int16 *)(armies + v2 + 18), (unsigned int)*(__int16 *)(armies + v2 + 20), v4);
v30 = v5;
sprintf(s, "Battle between %s and %s", v33, v34);
message_out(s, v29, v3, 1LL);
if ( v5 != -1 )
{
sprintf(s, " in %s", &cities[96 * v5]);
message_out(s, v29, v3, 1LL);
}
sprintf(s, " (Turn %d)", gen);
message_out(s, v29, v3, 0LL);
message_out("\n", v29, v3, 1LL);
func0(v37, a1);
func0(v39, a2);
sprintf(s, "(Odds: %d to %d)\n", (unsigned int)v37[0], (unsigned int)v39[0]);
message_out(s, v29, v3, 0LL);
v6 = v37;
for ( i = 0LL; i != 2; ++i )
{
sprintf(s, "%s has %d armies", (&v33)[i], (unsigned int)v6[2]);
message_out(s, v29, v3, 1LL);
v8 = v6[1];
if ( v8 == -1 )
{
message_out("\n", v29, v3, 1LL);
}
else
{
v9 = (const char *)(armies + 30LL * v37[16 * (int)i + 4 + v8]);
if ( !*v9 )
v9 = "a nameless hero";
sprintf(s, " led by %s\n", v9);
message_out(s, v29, v3, 1LL);
}
v6 += 16;
}
v10 = v38[0];
if ( v38[0] > 0 )
{
while ( 1 )
{
if ( v39[2] <= 0 )
goto LABEL_10;
v21 = roll((unsigned int)(v37[0] + v39[0]));
v22 = v37[0];
v23 = v21 < v37[0];
v24 = v21 >= v37[0];
if ( v37[16 * (unsigned __int64)(v21 < v37[0])] >= v37[16 * (unsigned __int64)(v21 >= v37[0])] / 2 )
goto LABEL_20;
v17 = v37[16 * (unsigned __int64)(v21 < v37[0]) + 1];
if ( v17 == -1 || *(_BYTE *)(armies + 30LL * v37[16 * (v21 < v37[0]) + 4 + v17] + 24) == 1 )
goto LABEL_20;
if ( (int)roll(10LL) > 4 )
{
sprintf(s, "set-eparm %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], 1LL);
fputs(s, mfile);
execpriv(s);
LABEL_20:
v16 = 16LL;
if ( v21 >= v22 )
v16 = 0LL;
v17 = func4((__int64)&v37[v16]);
v28 = (&v33)[v23];
v18 = v17 + 16LL * v23 + 4;
v19 = (const char *)armyname((unsigned int)v37[v18]);
sprintf(s, "%s (%s) is destroyed.\n", v19, v28);
message_out(s, v35, v36, 0LL);
if ( *(_BYTE *)(armies + 30LL * v37[v18] + 23) )
fputs(s, news);
sprintf(s, "kill-army %d\n", (unsigned int)v37[16 * v23 + 4 + v17]);
fputs(s, mfile);
execpriv(s);
goto LABEL_25;
}
if ( v38[16 * (__int64)v24] > 11 || (int)roll(10LL) > 4 )
break;
v27 = *(&v35 + v24);
v31 = (&v33)[v23];
v14 = (const char *)armyname((unsigned int)v37[16 * v23 + 4 + v17]);
sprintf(s, "%s (%s) deserts", v14, v31);
message_out(s, v29, v3, 1LL);
if ( v27 == -1 )
goto LABEL_35;
v15 = (const char *)nationcity(v27);
sprintf(s, " and joins %s\n", v15);
message_out(s, v29, v3, 1LL);
sprintf(s, "kill-army -2 %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], v27);
fputs(s, mfile);
execpriv(s);
func0(&v37[16 * (__int64)v24], v17);
LABEL_25:
v20 = 16LL;
if ( v21 >= v22 )
v20 = 0LL;
func1(&v37[v20], v17);
v10 = v38[0];
if ( v38[0] <= 0 )
goto LABEL_10;
}
v25 = (&v33)[v23];
v26 = (const char *)armyname((unsigned int)v37[16 * v23 + 4 + v17]);
sprintf(s, "%s (%s) deserts", v26, v25);
message_out(s, v29, v3, 1LL);
LABEL_35:
message_out("\n", v29, v3, 1LL);
sprintf(s, "kill-army -2 %d %d\n", (unsigned int)v37[16 * v23 + 4 + v17], 0xFFFFFFFFLL);
fputs(s, mfile);
execpriv(s);
goto LABEL_25;
}
LABEL_10:
v11 = v10 <= 0;
sprintf(s, "%s wins!\n", (&v33)[v10 <= 0]);
message_out(s, v29, v3, 1LL);
if ( v30 != -1 )
{
v12 = *(&v35 + v11);
if ( *(_DWORD *)&cities[96 * v30 + 24] != v12 )
func3(v12, v30);
}
result = (unsigned int)v37[16 * (__int64)v11 + 1];
if ( (_DWORD)result != -1 )
result = func2(v37[16 * v11 + 4 + (int)result]);
return result;
}

original_code:
void battle(int a, int b)
{
 struct armystack stacks[2];
 int ntns[2], r, c, city, hi, total, rlose, rwin, l, i, d, dn;
 char *names[2], buff[128];
 ntns[0] = armies[a].nation;
 ntns[1] = armies[b].nation;
 names[0] = nationcity(ntns[0]);
 names[1] = nationcity(ntns[1]);
 r = armies[a].r;
 c = armies[a].c;
 city = city_at(r, c);
 sprintf(buff, "Battle between %s and %s", names[0], names[1]);
 message_out(buff, ntns[0], ntns[1], 1);
 if(city != -1) {
  sprintf(buff, " in %s", cities[city].name);
  message_out(buff, ntns[0], ntns[1], 1);
 }
 sprintf(buff, " (Turn %d)", gen);
 message_out(buff, ntns[0], ntns[1], 0);
 message_out("\n", ntns[0], ntns[1], 1);
 make_stack(&(stacks[0]), a);
 make_stack(&(stacks[1]), b);
 sprintf(buff, "(Odds:  %d to %d)\n", stacks[0].ctot, stacks[1].ctot);
 message_out(buff, ntns[0], ntns[1], 0);
 for(i = 0; i < 2; i++) {
  sprintf(buff, "%s has %d armies", names[i], stacks[i].count);
  message_out(buff, ntns[0], ntns[1], 1);
  if((hi = stacks[i].hero) != -1) {
   sprintf(buff, " led by %s\n",
    armies[stacks[i].index[hi]].name[0] == '\0'
     ? "a nameless hero"
     : armies[stacks[i].index[hi]].name);
   message_out(buff, ntns[0], ntns[1], 1);
  } else
   message_out("\n", ntns[0], ntns[1], 1);
 }
 while(stacks[0].count > 0 && stacks[1].count > 0) {
  total = stacks[0].ctot + stacks[1].ctot;
  if(roll(total) < stacks[0].ctot)
   rlose = 1;
  else
   rlose = 0;
  rwin = (rlose + 1) % 2;
  d = -1;
  if(stacks[rlose].ctot < (stacks[rwin].ctot / 2)
  && (hi = stacks[rlose].hero) != -1
  && armies[stacks[rlose].index[hi]].eparm1 != 1) {
   if(roll(10) < 5) {
    dn = -1;
    if(stacks[rwin].count < 12 && roll(10) < 5)
     dn = ntns[rwin];
    d = hi;
   } else {
    sprintf(buff, "set-eparm %d %d\n",
     stacks[rlose].index[hi], 1);
    fputs(buff, mfile);
    execpriv(buff);
   }
  }
  if(d == -1) {
   l = least(&(stacks[rlose]));
   sprintf(buff, "%s (%s) is destroyed.\n",
    armyname(stacks[rlose].index[l]), names[rlose]);
   message_out(buff, ntns[0], ntns[1], 0);
   if(armies[stacks[rlose].index[l]].hero > 0)
    fputs(buff, news);
   sprintf(buff, "kill-army %d\n", stacks[rlose].index[l]);
   fputs(buff, mfile);
   execpriv(buff);
  } else {
   l = d;
   sprintf(buff, "%s (%s) deserts",
    armyname(stacks[rlose].index[l]), names[rlose]);
   message_out(buff, ntns[0], ntns[1], 1);
   if(dn != -1) {
    sprintf(buff, " and joins %s\n", nationcity(dn));
    message_out(buff, ntns[0], ntns[1], 1);
   } else
    message_out("\n", ntns[0], ntns[1], 1);
   sprintf(buff, "kill-army -2 %d %d\n",
    stacks[rlose].index[d], dn);
   fputs(buff, mfile);
   execpriv(buff);
   if(dn != -1)
       make_stack(&(stacks[rwin]), d);
  }
  stack_remove(&(stacks[rlose]), l);
 }
 if(stacks[0].count < 1)
  rwin = 1;
 else
  rwin = 0;
 sprintf(buff, "%s wins!\n", names[rwin]);
 message_out(buff, ntns[0], ntns[1], 1);
 if(city != -1
 && cities[city].nation != ntns[rwin])
  capture(ntns[rwin], city);
 if((hi = stacks[rwin].hero) != -1)
        advance(stacks[rwin].index[hi]);
}

canonical_original_code:
void battle(int a, int b)
{
 struct armystack stacks[2];
 int ntns[2]; int r; int c; int city; int hi; int total; int rlose; int rwin; int l; int i; int d; int dn;
 char *names[2]; char buff[128];
 ntns[0] = armies[a].nation;
 ntns[1] = armies[b].nation;
 names[0] = nationcity(ntns[0]);
 names[1] = nationcity(ntns[1]);
 r = armies[a].r;
 c = armies[a].c;
 city = city_at(r, c);
 sprintf(buff, "Battle between %s and %s", names[0], names[1]);
 message_out(buff, ntns[0], ntns[1], 1);
 if(city != -1) {
  sprintf(buff, " in %s", cities[city].name);
  message_out(buff, ntns[0], ntns[1], 1);
 }
 sprintf(buff, " (Turn %d)", gen);
 message_out(buff, ntns[0], ntns[1], 0);
 message_out("\n", ntns[0], ntns[1], 1);
 make_stack(&(stacks[0]), a);
 make_stack(&(stacks[1]), b);
 sprintf(buff, "(Odds:  %d to %d)\n", stacks[0].ctot, stacks[1].ctot);
 message_out(buff, ntns[0], ntns[1], 0);
 for(i = 0; i < 2; i++) {
  sprintf(buff, "%s has %d armies", names[i], stacks[i].count);
  message_out(buff, ntns[0], ntns[1], 1);
  if((hi = stacks[i].hero) != -1) {
   sprintf(buff, " led by %s\n",
    armies[stacks[i].index[hi]].name[0] == '\0'
     ? "a nameless hero"
     : armies[stacks[i].index[hi]].name);
   message_out(buff, ntns[0], ntns[1], 1);
  } else
   message_out("\n", ntns[0], ntns[1], 1);
 }
 while(stacks[0].count > 0 && stacks[1].count > 0) {
  total = stacks[0].ctot + stacks[1].ctot;
  if(roll(total) < stacks[0].ctot)
   rlose = 1;
  else
   rlose = 0;
  rwin = (rlose + 1) % 2;
  d = -1;
  if(stacks[rlose].ctot < (stacks[rwin].ctot / 2)
  && (hi = stacks[rlose].hero) != -1
  && armies[stacks[rlose].index[hi]].eparm1 != 1) {
   if(roll(10) < 5) {
    dn = -1;
    if(stacks[rwin].count < 12 && roll(10) < 5)
     dn = ntns[rwin];
    d = hi;
   } else {
    sprintf(buff, "set-eparm %d %d\n",
     stacks[rlose].index[hi], 1);
    fputs(buff, mfile);
    execpriv(buff);
   }
  }
  if(d == -1) {
   l = least(&(stacks[rlose]));
   sprintf(buff, "%s (%s) is destroyed.\n",
    armyname(stacks[rlose].index[l]), names[rlose]);
   message_out(buff, ntns[0], ntns[1], 0);
   if(armies[stacks[rlose].index[l]].hero > 0)
    fputs(buff, news);
   sprintf(buff, "kill-army %d\n", stacks[rlose].index[l]);
   fputs(buff, mfile);
   execpriv(buff);
  } else {
   l = d;
   sprintf(buff, "%s (%s) deserts",
    armyname(stacks[rlose].index[l]), names[rlose]);
   message_out(buff, ntns[0], ntns[1], 1);
   if(dn != -1) {
    sprintf(buff, " and joins %s\n", nationcity(dn));
    message_out(buff, ntns[0], ntns[1], 1);
   } else
    message_out("\n", ntns[0], ntns[1], 1);
   sprintf(buff, "kill-army -2 %d %d\n",
    stacks[rlose].index[d], dn);
   fputs(buff, mfile);
   execpriv(buff);
   if(dn != -1)
       make_stack(&(stacks[rwin]), d);
  }
  stack_remove(&(stacks[rlose]), l);
 }
 if(stacks[0].count < 1)
  rwin = 1;
 else
  rwin = 0;
 sprintf(buff, "%s wins!\n", names[rwin]);
 message_out(buff, ntns[0], ntns[1], 1);
 if(city != -1
 && cities[city].nation != ntns[rwin])
  capture(ntns[rwin], city);
 if((hi = stacks[rwin].hero) != -1)
        advance(stacks[rwin].index[hi]);
}

variable_types: a int

variable_types: struct armystack { int ctot; int hero; int count; int city; int index[12]; } [2]

variable_types:
ntns
int [2]

variable_types: r int

variable_types: city int

variable_types: hi int

variable_types: total int

variable_types: rlose int

variable_types: rwin int

variable_types: l int

variable_types: d int

variable_types: dn int

variable_types:
names
char *[2]


combat

repo: Solomoriah/WAR

decompiled_code:
void __fastcall combat(int a1)
{
int v1; // edx
__int64 v3; // r14
int v4; // er13
int v5; // ebp
__int64 v6; // rbx
__int64 v7; // rsi
unsigned int v8; // edi
__int64 v9; // rbp
__int64 v10; // rbx
__int16 *v11; // rax
__int64 v12; // rdx
unsigned int v13; // eax
unsigned int v14; // er13

v1 = armycnt;
if ( armycnt > 0 )
{
v3 = 0LL;
v4 = 0;
do
{
v7 = armies + v3;
v8 = *(__int16 *)(armies + v3 + 16);
if ( v8 == a1 && v1 > 0 )
{
v6 = 0LL;
v5 = 0;
while ( *(__int16 *)(armies + v6 + 16) == a1
|| *(_WORD *)(armies + v6 + 18) != *(_WORD *)(v7 + 18)
|| *(_WORD *)(armies + v6 + 20) != *(_WORD *)(v7 + 20) )
{
++v5;
v6 += 30LL;
if ( v5 == v1 )
goto LABEL_8;
}
mail_line(" \b", v8);
mail_line(" \b", (unsigned int)*(__int16 *)(armies + v6 + 16));
battle(v4, v5);
putc(10, stdout);
putc(10, news);
}
LABEL_8:
++v4;
v1 = armycnt;
v3 += 30LL;
}
while ( armycnt > v4 );
if ( armycnt > 0 )
{
v9 = 0LL;
do
{
v10 = 30 * v9;
v11 = (__int16 *)(armies + 30 * v9);
v12 = (unsigned int)v11[8];
if ( (_DWORD)v12 == a1 )
{
v13 = city_at((unsigned int)v11[9], (unsigned int)v11[10], v12);
v14 = v13;
if ( v13 != -1 && *(_DWORD *)&cities[96 * v13 + 24] != a1 )
{
mail_line(" \b", (unsigned int)*(__int16 *)(armies + v10 + 16));
mail_line(" \b", *(unsigned int *)&cities[96 * v14 + 24]);
capture(*(__int16 *)(armies + v10 + 16), v14);
putc(10, stdout);
putc(10, news);
}
}
++v9;
}
while ( armycnt > (int)v9 );
}
}
}

original_code:
void combat(int n)
{
 int i, j, c;
 for(i = 0; i < armycnt; i++)
  if(armies[i].nation == n)
   for(j = 0; j < armycnt; j++)
    if(armies[j].nation != n
    && armies[j].r == armies[i].r
    && armies[j].c == armies[i].c) {
     mail_line(" \b", armies[i].nation);
     mail_line(" \b", armies[j].nation);
     battle(i, j);
     putc('\n', stdout);
     putc('\n', news);
     break;
    }
 for(i = 0; i < armycnt; i++)
  if(armies[i].nation == n)
   if((c = city_at(armies[i].r, armies[i].c)) != -1
   && cities[c].nation != n) {
    mail_line(" \b", armies[i].nation);
    mail_line(" \b", cities[c].nation);
    capture(armies[i].nation, c);
    putc('\n', stdout);
    putc('\n', news);
   }
}

canonical_original_code:
void combat(int n)
{
 int i; int j; int c;
 for(i = 0; i < armycnt; i++)
  if(armies[i].nation == n)
   for(j = 0; j < armycnt; j++)
    if(armies[j].nation != n
    && armies[j].r == armies[i].r
    && armies[j].c == armies[i].c) {
     mail_line(" \b", armies[i].nation);
     mail_line(" \b", armies[j].nation);
     battle(i, j);
     putc('\n', stdout);
     putc('\n', news);
     break;
    }
 for(i = 0; i < armycnt; i++)
  if(armies[i].nation == n)
   if((c = city_at(armies[i].r, armies[i].c)) != -1
   && cities[c].nation != n) {
    mail_line(" \b", armies[i].nation);
    mail_line(" \b", cities[c].nation);
    capture(armies[i].nation, c);
    putc('\n', stdout);
    putc('\n', news);
   }
}

variable_types: n int

variable_types: i int

variable_types: j int

variable_types: c int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
const char *v5; // rdx
__int64 v6; // rcx
unsigned __int64 v7; // rax

if ( chdir("/war") == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", "/war");
ggetch();
gend();
exit(1);
}
if ( argc <= 1 )
{
LABEL_16:
ginit();
reader("news", 0LL);
gend();
exit(0);
}
v3 = argv[1];
v4 = *(unsigned __int8 *)v3;
if ( (_BYTE)v4 == 46 )
{
if ( v3[1] == 46 )
{
if ( !v3[2] )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *((unsigned __int8 *)v3 + 1);
}
}
else if ( !(_BYTE)v4 )
{
LABEL_15:
if ( chdir(argv[1]) == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", v3);
ggetch();
gend();
exit(1);
}
goto LABEL_16;
}
v5 = argv[1];
v6 = 0x200000000801LL;
do
{
v7 = (unsigned int)(v4 - 47);
if ( (unsigned __int8)v7 <= 0x2Du && _bittest64(&v6, v7) )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *(unsigned __int8 *)++v5;
}
while ( (_BYTE)v4 );
goto LABEL_15;
}

original_code:
int main(int argc, char *argv[])
{
    struct stat st_buf;
    char *gamedir, *p;
    if(chdir("/war") == -1) {
        gmove(20,18);
        gprintf("Error Setting Directory %s\n", "/war");
        ggetch();
        gend();
        exit(1);
    }
    if(argc > 1) {
        gamedir = argv[1];
        if(gamedir[0] == '.' && gamedir[1] == '.' && gamedir[2] == '\0') {
            gmove(20,18);
            gprintf("Invalid Directory Name %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
        for(p = gamedir; *p; p++) {
            if(*p == '/' || *p == '\\' || *p == ':') {
                gmove(20,18);
                gprintf("Invalid Directory Name %s\n", gamedir);
                ggetch();
                gend();
                exit(1);
            }
        }
        if(chdir(gamedir) == -1) {
            gmove(20,18);
            gprintf("Error Setting Directory %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
    }
    ginit();
    reader("news", 0);
    gend();
    exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct stat st_buf;
    char *gamedir; char *p;
    if(chdir("/war") == -1) {
        gmove(20,18);
        gprintf("Error Setting Directory %s\n", "/war");
        ggetch();
        gend();
        exit(1);
    }
    if(argc > 1) {
        gamedir = argv[1];
        if(gamedir[0] == '.' && gamedir[1] == '.' && gamedir[2] == '\0') {
            gmove(20,18);
            gprintf("Invalid Directory Name %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
        for(p = gamedir; *p; p++) {
            if(*p == '/' || *p == '\\' || *p == ':') {
                gmove(20,18);
                gprintf("Invalid Directory Name %s\n", gamedir);
                ggetch();
                gend();
                exit(1);
            }
        }
        if(chdir(gamedir) == -1) {
            gmove(20,18);
            gprintf("Error Setting Directory %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
    }
    ginit();
    reader("news", 0);
    gend();
    exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
gamedir
char *

variable_types:
p
char *

return_type: int

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


mainscreen

decompiled_code:
void mainscreen()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void mainscreen()
{
    ;
}

canonical_original_code:
void mainscreen()
{
    ;
}


saystat

decompiled_code:
__int64 __fastcall saystat(__int64 a1)
{
gmove(24LL, 0LL);
return gputs(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
gmove(24LL, 0LL);
return gputs(a1);
}

original_code:
void saystat(char *s)
{
    gmove(24, 0);
    gputs(s);
}

variable_types:
s
char *


newcity

decompiled_code:
__int64 __fastcall newcity(__int64 a1, __int64 a2)
{
char *v2; // rax
unsigned __int64 v3; // rdx
int v4; // er8
int v5; // edi
__int64 v6; // rdi
int v7; // edi
unsigned int v8; // ebx
__int64 v9; // rdx
__int64 v10; // rdi
__int64 v11; // rax
int v12; // edx
unsigned __int64 v13; // rax
_BOOL8 v15; // rdx
__int64 v16; // rcx
__int64 v17; // rax
int v18; // ebp
_BOOL8 v19; // rdi
__int64 v20; // rax
int v21; // edx
unsigned __int64 v22; // rax
int v23[150]; // [rsp+0h] [rbp-278h] BYREF
char v24[32]; // [rsp+258h] [rbp-20h] BYREF

v2 = (char *)v23;
v3 = (unsigned __int64)v23;
do
{
*(_DWORD *)v3 = -1;
v3 += 4LL;
}
while ( (char *)v3 != v24 );
v4 = citycnt;
if ( citycnt > 0 )
{
a2 = 0LL;
do
{
if ( *(int *)((char *)&cities + a2 + 24) <= 0 )
{
v5 = *(_DWORD *)((char *)&cities + a2 + 28);
v3 = (unsigned int)(v5 / 150);
v6 = v5 % 150;
if ( v23[v6] == -1 )
v23[v6] = 0;
}
else
{
v7 = *(_DWORD *)((char *)&cities + a2 + 28);
v3 = (unsigned int)(150 * (v7 / 150));
v23[v7 % 150] = 1;
}
a2 += 96LL;
}
while ( a2 != 96LL * (unsigned int)v4 );
}
v8 = 0;
do
{
v8 += *(_DWORD *)v2 == 0;
v2 += 4;
}
while ( v2 != v24 );
if ( !v8 )
{
if ( v4 <= 0 )
return (unsigned int)-1;
v9 = 96LL * (unsigned int)v4;
LODWORD(v10) = 0;
v11 = 0LL;
do
{
v10 = (*(_DWORD *)((char *)&cities + v11 + 24) == 0) + (unsigned int)v10;
v11 += 96LL;
}
while ( v9 != v11 );
if ( !(_DWORD)v10 )
return (unsigned int)-1;
v12 = roll(v10, a2, v9, v24);
if ( citycnt <= 0 )
return (unsigned int)-1;
v13 = (unsigned int)((_DWORD)&cities + 24);
do
{
if ( !*(_DWORD *)v13 )
{
if ( !v12 )
return v8;
--v12;
}
++v8;
v13 += 96LL;
}
while ( v8 != citycnt );
return (unsigned int)-1;
}
v8 = roll(v8, a2, v3, v24);
v17 = 0LL;
while ( 1 )
{
v18 = v17;
if ( !v23[v17] )
break;
LABEL_26:
if ( ++v17 == 150 )
return (unsigned int)-1;
}
if ( v8 )
{
--v8;
goto LABEL_26;
}
if ( (int)v17 > 149 )
return (unsigned int)-1;
if ( citycnt <= 0 )
{
v19 = 0LL;
}
else
{
v16 = 96LL * citycnt;
LODWORD(v19) = 0;
v20 = 0LL;
do
{
v15 = *(_DWORD *)((char *)&cities + v20 + 28) == v18;
v19 += v15;
v20 += 96LL;
}
while ( v16 != v20 );
}
v21 = roll(v19, a2, v15, v16);
if ( citycnt <= 0 )
return (unsigned int)-1;
v22 = (unsigned int)((_DWORD)&cities + 28);
do
{
if ( *(_DWORD *)v22 == v18 )
{
if ( !v21 )
return v8;
--v21;
}
++v8;
v22 += 96LL;
}
while ( v8 != citycnt );
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
char *v2; // rax
unsigned __int64 v3; // rdx
int v4; // er8
int v5; // edi
__int64 v6; // rdi
int v7; // edi
unsigned int v8; // ebx
__int64 v9; // rdx
__int64 v10; // rdi
__int64 v11; // rax
int v12; // edx
unsigned __int64 v13; // rax
_BOOL8 v15; // rdx
__int64 v16; // rcx
__int64 v17; // rax
int v18; // ebp
_BOOL8 v19; // rdi
__int64 v20; // rax
int v21; // edx
unsigned __int64 v22; // rax
int v23[150]; // [rsp+0h] [rbp-278h] BYREF
char v24[32]; // [rsp+258h] [rbp-20h] BYREF

v2 = (char *)v23;
v3 = (unsigned __int64)v23;
do
{
*(_DWORD *)v3 = -1;
v3 += 4LL;
}
while ( (char *)v3 != v24 );
v4 = citycnt;
if ( citycnt > 0 )
{
a2 = 0LL;
do
{
if ( *(int *)((char *)&cities + a2 + 24) <= 0 )
{
v5 = *(_DWORD *)((char *)&cities + a2 + 28);
v3 = (unsigned int)(v5 / 150);
v6 = v5 % 150;
if ( v23[v6] == -1 )
v23[v6] = 0;
}
else
{
v7 = *(_DWORD *)((char *)&cities + a2 + 28);
v3 = (unsigned int)(150 * (v7 / 150));
v23[v7 % 150] = 1;
}
a2 += 96LL;
}
while ( a2 != 96LL * (unsigned int)v4 );
}
v8 = 0;
do
{
v8 += *(_DWORD *)v2 == 0;
v2 += 4;
}
while ( v2 != v24 );
if ( !v8 )
{
if ( v4 <= 0 )
return (unsigned int)-1;
v9 = 96LL * (unsigned int)v4;
LODWORD(v10) = 0;
v11 = 0LL;
do
{
v10 = (*(_DWORD *)((char *)&cities + v11 + 24) == 0) + (unsigned int)v10;
v11 += 96LL;
}
while ( v9 != v11 );
if ( !(_DWORD)v10 )
return (unsigned int)-1;
v12 = roll(v10, a2, v9, v24);
if ( citycnt <= 0 )
return (unsigned int)-1;
v13 = (unsigned int)((_DWORD)&cities + 24);
do
{
if ( !*(_DWORD *)v13 )
{
if ( !v12 )
return v8;
--v12;
}
++v8;
v13 += 96LL;
}
while ( v8 != citycnt );
return (unsigned int)-1;
}
v8 = roll(v8, a2, v3, v24);
v17 = 0LL;
while ( 1 )
{
v18 = v17;
if ( !v23[v17] )
break;
LABEL_26:
if ( ++v17 == 150 )
return (unsigned int)-1;
}
if ( v8 )
{
--v8;
goto LABEL_26;
}
if ( (int)v17 > 149 )
return (unsigned int)-1;
if ( citycnt <= 0 )
{
v19 = 0LL;
}
else
{
v16 = 96LL * citycnt;
LODWORD(v19) = 0;
v20 = 0LL;
do
{
v15 = *(_DWORD *)((char *)&cities + v20 + 28) == v18;
v19 += v15;
v20 += 96LL;
}
while ( v16 != v20 );
}
v21 = roll(v19, a2, v15, v16);
if ( citycnt <= 0 )
return (unsigned int)-1;
v22 = (unsigned int)((_DWORD)&cities + 28);
do
{
if ( *(_DWORD *)v22 == v18 )
{
if ( !v21 )
return v8;
--v21;
}
++v8;
v22 += 96LL;
}
while ( v8 != citycnt );
return (unsigned int)-1;
}

original_code:
int newcity()
{
    int i, clusters[150], cc, c, cl;
    for(i = 0; i < 150; i++)
        clusters[i] = -1;
    for(i = 0; i < citycnt; i++) {
        if(cities[i].nation > 0)
            clusters[cities[i].cluster % 150] = 1;
        else if(clusters[cities[i].cluster % 150] == -1)
            clusters[cities[i].cluster % 150] = 0;
    }
    cc = 0;
    for(i = 0; i < 150; i++)
        if(clusters[i] == 0)
            cc++;
    if(cc == 0) {
        cc = 0;
        for(i = 0; i < citycnt; i++)
            if(cities[i].nation == 0)
                cc++;
        if(cc == 0)
            return -1;
        c = roll(cc);
        for(i = 0; i < citycnt; i++)
            if(cities[i].nation == 0)
                if(c == 0)
                    return i;
                else
                    c--;
        return -1;
    } else {
        c = roll(cc);
        for(i = 0; i < 150; i++)
            if(clusters[i] == 0)
                if(c == 0)
                    break;
                else
                    c--;
        if(i < 150) {
            cl = i;
            cc = 0;
            for(i = 0; i < citycnt; i++)
                if(cities[i].cluster == cl)
                    cc++;
            c = roll(cc);
            for(i = 0; i < citycnt; i++)
                if(cities[i].cluster == cl)
                    if(c == 0)
                        return i;
                    else
                        c--;
        }
        return -1;
    }
}

canonical_original_code:
int newcity()
{
    int i; int clusters[150]; int cc; int c; int cl;
    for(i = 0; i < 150; i++)
        clusters[i] = -1;
    for(i = 0; i < citycnt; i++) {
        if(cities[i].nation > 0)
            clusters[cities[i].cluster % 150] = 1;
        else if(clusters[cities[i].cluster % 150] == -1)
            clusters[cities[i].cluster % 150] = 0;
    }
    cc = 0;
    for(i = 0; i < 150; i++)
        if(clusters[i] == 0)
            cc++;
    if(cc == 0) {
        cc = 0;
        for(i = 0; i < citycnt; i++)
            if(cities[i].nation == 0)
                cc++;
        if(cc == 0)
            return -1;
        c = roll(cc);
        for(i = 0; i < citycnt; i++)
            if(cities[i].nation == 0)
                if(c == 0)
                    return i;
                else
                    c--;
        return -1;
    } else {
        c = roll(cc);
        for(i = 0; i < 150; i++)
            if(clusters[i] == 0)
                if(c == 0)
                    break;
                else
                    c--;
        if(i < 150) {
            cl = i;
            cc = 0;
            for(i = 0; i < citycnt; i++)
                if(cities[i].cluster == cl)
                    cc++;
            c = roll(cc);
            for(i = 0; i < citycnt; i++)
                if(cities[i].cluster == cl)
                    if(c == 0)
                        return i;
                    else
                        c--;
        }
        return -1;
    }
}

variable_types:
clusters
int [150]

variable_types: cc int

variable_types: cl int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
const char *v5; // rdx
__int64 v6; // rcx
unsigned __int64 v7; // rax
__int64 v8; // rdi
int v9; // ebx
unsigned int v10; // ebx
__uid_t v11; // ebp
const char *v12; // rsi
const char *v13; // rdi
FILE *v14; // r14
unsigned int v15; // er12
unsigned int v16; // er13
__int64 v17; // rax
unsigned int v18; // er12
FILE *v19; // r14
int v20; // er13
FILE *v21; // r14
int v22; // ecx
__int64 i; // rax
char *j; // r15
char v25; // al
int v26; // eax
unsigned int v27; // er15
char *v28; // rax
__int64 v29; // rdx
__int64 v30; // r9
char v31; // al
__int64 v32; // rax
char *v33; // r15
__int64 v34; // rdx
__int64 v35; // rcx
__int64 v36; // rdx
__int64 v37; // rcx
int v38; // eax
unsigned int v39; // er13
FILE *v40; // rax
__int64 k; // rax
const char *v42; // rax
int v43; // [rsp+8h] [rbp-110h]
unsigned int v44; // [rsp+Ch] [rbp-10Ch]
char dest[15]; // [rsp+11h] [rbp-107h] BYREF
char s[128]; // [rsp+20h] [rbp-F8h] BYREF
char filename[120]; // [rsp+A0h] [rbp-78h] BYREF

if ( chdir("/war") == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", "/war");
ggetch();
gend();
exit(1);
}
if ( argc <= 1 )
goto LABEL_16;
v3 = argv[1];
v4 = *(unsigned __int8 *)v3;
if ( (_BYTE)v4 == 46 )
{
if ( v3[1] == 46 )
{
if ( !v3[2] )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *((unsigned __int8 *)v3 + 1);
}
}
else if ( !(_BYTE)v4 )
{
LABEL_15:
if ( chdir(argv[1]) == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", v3);
ggetch();
gend();
exit(1);
}
LABEL_16:
ginit();
titlescreen();
v8 = time(0LL);
srand48(v8);
v9 = loadmap();
if ( v9 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Map (%s)\n", *((const char **)&errors + v9));
ggetch();
gend();
exit(1);
}
v10 = loadsave();
if ( v10 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Game Save (%s)\n", *((const char **)&errors + (int)v10));
ggetch();
gend();
exit(1);
}
gmove(16LL, 10LL);
if ( world )
{
gputs("World: ");
gputs(&world);
gputs(" ");
}
gprintf("Turn: %d", gen);
v11 = getuid();
if ( !v11 )
{
gmove(20LL, 10LL);
gputs("UID Not Available; Aborting.");
ggetch();
gend();
exit(3);
}
gmove(20LL, 10LL);
gputs("Reading Master Commands... ");
grefresh();
v12 = "r";
v13 = "master.cmd";
v14 = fopen("master.cmd", "r");
v15 = v10;
if ( v14 )
{
while ( 1 )
{
v12 = (_BYTE *)&loc_80;
if ( !fgets(s, 128, v14) )
break;
v16 = execpriv(s);
if ( v16 )
{
gmove(20LL, 10LL);
gprintf("Master Cmd Failed, Line %d, Code %d ", v15 + 1, v16);
ggetch();
gend();
exit(2);
}
if ( !((int)++v15 % 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v15);
grefresh();
}
}
v13 = (const char *)v14;
fclose(v14);
}
v17 = 0LL;
do
{
v18 = v17;
if ( nationcnt <= (int)v17 )
goto LABEL_38;
++v17;
}
while ( *((_DWORD *)&nations + 8 * v17 - 1) != v11 );
if ( v18 == -1 )
{
LABEL_38:
if ( gen > 10 )
{
gmove(20LL, 10LL);
gprintf("Game has been running %d turns, too late to enter now.", gen);
gclrline();
ggetch();
gend();
exit(5);
}
v20 = newcity((__int64)v13, (__int64)v12);
if ( v20 < 0 )
{
gmove(20LL, 10LL);
gputs("No Cities Available. ");
gclrline();
ggetch();
gend();
exit(5);
}
v21 = fopen("master.cmd", "a");
v18 = nationcnt;
if ( !v21 )
{
gmove(20LL, 10LL);
gputs("Can't Write Master Cmd's ");
gclrline();
ggetch();
gend();
exit(6);
}
while ( 1 )
{
gmove(18LL, 10LL);
gprintf("Your City is %s", (const char *)&cities + 96 * v20);
gclrline();
s[0] = 0;
do
{
gmove(20LL, 10LL);
gputs("(Enter ! to Retry)");
gclrline();
gmove(19LL, 10LL);
gputs("Enter your name: ");
gclrline();
getstring(s);
}
while ( !s[0] );
if ( strcmp(s, "!") )
break;
v20 = newcity((__int64)s, (__int64)"!");
}
strncpy(dest, s, 0xEuLL);
dest[14] = 0;
gmove(21LL, 10LL);
gputs("Available: ");
strcpy(s, marks);
v22 = nationcnt;
for ( i = 0LL; v22 > (int)i; ++i )
s[*((int *)&nations + 8 * i + 4)] = 32;
for ( j = s; ; ++j )
{
v25 = *j;
if ( !*j )
break;
if ( v25 != 32 )
gputch((unsigned int)v25);
}
gmove(20LL, 10LL);
gputs("Select Nation Mark: ");
gclrline();
do
{
do
{
v26 = ggetch();
v27 = toupper(v26);
v28 = strchr(s, v27);
}
while ( v27 == 32 );
}
while ( !v28 );
gputch(v27);
gmove(21LL, 10LL);
gclrline();
v29 = 0LL;
do
{
v30 = (unsigned int)v29;
v31 = s[v29];
if ( !v31 )
break;
++v29;
}
while ( v31 != v27 );
sprintf(s, "new-nation '%s' %d %d %d\n", dest, v11, (unsigned int)v20, v30);
fputs(s, v21);
execpriv(s);
v32 = 96LL * v20;
v33 = (char *)&cities + v32;
v44 = ttypes[8 * *(int *)((char *)&cities + v32 + 36) + 6];
v43 = roll(2LL, v21, v34, v35);
v38 = roll(3LL, v21, v36, v37);
sprintf(
s,
"make-army '%s' %d %d %d %d %d %d %d %d %d\n",
dest,
v18,
*((unsigned int *)v33 + 4),
*((unsigned int *)v33 + 5),
(unsigned int)(v38 + 4),
(unsigned int)(v43 + 1),
8LL,
v44,
0LL,
1LL);
fputs(s, v21);
execpriv(s);
sprintf(s, "control-city %d %d\n", v18, (unsigned int)v20);
fputs(s, v21);
execpriv(s);
fclose(v21);
}
gmove(20LL, 10LL);
gputs("Reading Player Commands... ");
gclrline();
grefresh();
sprintf(filename, "%d.cmd", v11);
v19 = fopen(filename, "r");
if ( v19 )
{
while ( fgets(s, 128, v19) )
{
v39 = execpriv(s);
if ( v39 )
{
gmove(20LL, 10LL);
gprintf("Player Cmd Failed, Line %d, Code %d ", v10 + 1, v39);
ggetch();
gend();
exit(2);
}
if ( !((int)++v10 % 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v10);
grefresh();
}
}
fclose(v19);
}
v40 = fopen(filename, "a");
pfile = v40;
if ( v40 )
{
setbuf(v40, 0LL);
for ( k = 0LL; nationcnt > (int)k; ++k )
{
if ( *((_DWORD *)&nations + 8 * k + 7) == v11 )
v18 = k;
}
gmove(19LL, 10LL);
v42 = (const char *)nationcity(v18);
gprintf("Welcome, %s of %s!", (const char *)&nations + 32 * (int)v18, v42);
gclrline();
gmove(20LL, 10LL);
gputs("Press Any Key to Begin... ");
gclrline();
ggetch();
mainscreen();
mainloop(v18);
fclose(pfile);
sprintf(filename, "%d.map", v11);
pfile = fopen(filename, "w");
writemap(pfile, v18);
fclose(pfile);
gend();
exit(0);
}
gmove(20LL, 10LL);
gputs("Can't Create or Append Player Cmd's ");
gclrline();
ggetch();
exit(4);
}
v5 = argv[1];
v6 = 0x200000000801LL;
do
{
v7 = (unsigned int)(v4 - 47);
if ( (unsigned __int8)v7 <= 0x2Du && _bittest64(&v6, v7) )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *(unsigned __int8 *)++v5;
}
while ( (_BYTE)v4 );
goto LABEL_15;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
const char *v5; // rdx
__int64 v6; // rcx
unsigned __int64 v7; // rax
__int64 v8; // rdi
int v9; // ebx
unsigned int v10; // ebx
__uid_t v11; // ebp
const char *v12; // rsi
const char *v13; // rdi
FILE *v14; // r14
unsigned int v15; // er12
unsigned int v16; // er13
__int64 v17; // rax
unsigned int v18; // er12
FILE *v19; // r14
int v20; // er13
FILE *v21; // r14
int v22; // ecx
__int64 i; // rax
char *j; // r15
char v25; // al
int v26; // eax
unsigned int v27; // er15
char *v28; // rax
__int64 v29; // rdx
__int64 v30; // r9
char v31; // al
__int64 v32; // rax
char *v33; // r15
__int64 v34; // rdx
__int64 v35; // rcx
__int64 v36; // rdx
__int64 v37; // rcx
int v38; // eax
unsigned int v39; // er13
FILE *v40; // rax
__int64 k; // rax
const char *v42; // rax
int v43; // [rsp+8h] [rbp-110h]
unsigned int v44; // [rsp+Ch] [rbp-10Ch]
char dest[15]; // [rsp+11h] [rbp-107h] BYREF
char s[128]; // [rsp+20h] [rbp-F8h] BYREF
char filename[120]; // [rsp+A0h] [rbp-78h] BYREF

if ( chdir("/war") == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", "/war");
ggetch();
gend();
exit(1);
}
if ( argc <= 1 )
goto LABEL_16;
v3 = argv[1];
v4 = *(unsigned __int8 *)v3;
if ( (_BYTE)v4 == 46 )
{
if ( v3[1] == 46 )
{
if ( !v3[2] )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *((unsigned __int8 *)v3 + 1);
}
}
else if ( !(_BYTE)v4 )
{
LABEL_15:
if ( chdir(argv[1]) == -1 )
{
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", v3);
ggetch();
gend();
exit(1);
}
LABEL_16:
ginit();
titlescreen();
v8 = time(0LL);
srand48(v8);
v9 = loadmap();
if ( v9 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Map (%s)\n", *((const char **)&errors + v9));
ggetch();
gend();
exit(1);
}
v10 = loadsave();
if ( v10 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Game Save (%s)\n", *((const char **)&errors + (int)v10));
ggetch();
gend();
exit(1);
}
gmove(16LL, 10LL);
if ( world )
{
gputs("World: ");
gputs(&world);
gputs(" ");
}
gprintf("Turn: %d", gen);
v11 = getuid();
if ( !v11 )
{
gmove(20LL, 10LL);
gputs("UID Not Available; Aborting.");
ggetch();
gend();
exit(3);
}
gmove(20LL, 10LL);
gputs("Reading Master Commands... ");
grefresh();
v12 = "r";
v13 = "master.cmd";
v14 = fopen("master.cmd", "r");
v15 = v10;
if ( v14 )
{
while ( 1 )
{
v12 = (_BYTE *)&loc_80;
if ( !fgets(s, 128, v14) )
break;
v16 = execpriv(s);
if ( v16 )
{
gmove(20LL, 10LL);
gprintf("Master Cmd Failed, Line %d, Code %d ", v15 + 1, v16);
ggetch();
gend();
exit(2);
}
if ( !((int)++v15 % 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v15);
grefresh();
}
}
v13 = (const char *)v14;
fclose(v14);
}
v17 = 0LL;
do
{
v18 = v17;
if ( nationcnt <= (int)v17 )
goto LABEL_38;
++v17;
}
while ( *((_DWORD *)&nations + 8 * v17 - 1) != v11 );
if ( v18 == -1 )
{
LABEL_38:
if ( gen > 10 )
{
gmove(20LL, 10LL);
gprintf("Game has been running %d turns, too late to enter now.", gen);
gclrline();
ggetch();
gend();
exit(5);
}
v20 = func0((__int64)v13, (__int64)v12);
if ( v20 < 0 )
{
gmove(20LL, 10LL);
gputs("No Cities Available. ");
gclrline();
ggetch();
gend();
exit(5);
}
v21 = fopen("master.cmd", "a");
v18 = nationcnt;
if ( !v21 )
{
gmove(20LL, 10LL);
gputs("Can't Write Master Cmd's ");
gclrline();
ggetch();
gend();
exit(6);
}
while ( 1 )
{
gmove(18LL, 10LL);
gprintf("Your City is %s", (const char *)&cities + 96 * v20);
gclrline();
s[0] = 0;
do
{
gmove(20LL, 10LL);
gputs("(Enter ! to Retry)");
gclrline();
gmove(19LL, 10LL);
gputs("Enter your name: ");
gclrline();
getstring(s);
}
while ( !s[0] );
if ( strcmp(s, "!") )
break;
v20 = func0((__int64)s, (__int64)"!");
}
strncpy(dest, s, 0xEuLL);
dest[14] = 0;
gmove(21LL, 10LL);
gputs("Available: ");
strcpy(s, marks);
v22 = nationcnt;
for ( i = 0LL; v22 > (int)i; ++i )
s[*((int *)&nations + 8 * i + 4)] = 32;
for ( j = s; ; ++j )
{
v25 = *j;
if ( !*j )
break;
if ( v25 != 32 )
gputch((unsigned int)v25);
}
gmove(20LL, 10LL);
gputs("Select Nation Mark: ");
gclrline();
do
{
do
{
v26 = ggetch();
v27 = toupper(v26);
v28 = strchr(s, v27);
}
while ( v27 == 32 );
}
while ( !v28 );
gputch(v27);
gmove(21LL, 10LL);
gclrline();
v29 = 0LL;
do
{
v30 = (unsigned int)v29;
v31 = s[v29];
if ( !v31 )
break;
++v29;
}
while ( v31 != v27 );
sprintf(s, "new-nation '%s' %d %d %d\n", dest, v11, (unsigned int)v20, v30);
fputs(s, v21);
execpriv(s);
v32 = 96LL * v20;
v33 = (char *)&cities + v32;
v44 = ttypes[8 * *(int *)((char *)&cities + v32 + 36) + 6];
v43 = roll(2LL, v21, v34, v35);
v38 = roll(3LL, v21, v36, v37);
sprintf(
s,
"make-army '%s' %d %d %d %d %d %d %d %d %d\n",
dest,
v18,
*((unsigned int *)v33 + 4),
*((unsigned int *)v33 + 5),
(unsigned int)(v38 + 4),
(unsigned int)(v43 + 1),
8LL,
v44,
0LL,
1LL);
fputs(s, v21);
execpriv(s);
sprintf(s, "control-city %d %d\n", v18, (unsigned int)v20);
fputs(s, v21);
execpriv(s);
fclose(v21);
}
gmove(20LL, 10LL);
gputs("Reading Player Commands... ");
gclrline();
grefresh();
sprintf(filename, "%d.cmd", v11);
v19 = fopen(filename, "r");
if ( v19 )
{
while ( fgets(s, 128, v19) )
{
v39 = execpriv(s);
if ( v39 )
{
gmove(20LL, 10LL);
gprintf("Player Cmd Failed, Line %d, Code %d ", v10 + 1, v39);
ggetch();
gend();
exit(2);
}
if ( !((int)++v10 % 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v10);
grefresh();
}
}
fclose(v19);
}
v40 = fopen(filename, "a");
pfile = v40;
if ( v40 )
{
setbuf(v40, 0LL);
for ( k = 0LL; nationcnt > (int)k; ++k )
{
if ( *((_DWORD *)&nations + 8 * k + 7) == v11 )
v18 = k;
}
gmove(19LL, 10LL);
v42 = (const char *)nationcity(v18);
gprintf("Welcome, %s of %s!", (const char *)&nations + 32 * (int)v18, v42);
gclrline();
gmove(20LL, 10LL);
gputs("Press Any Key to Begin... ");
gclrline();
ggetch();
mainscreen();
mainloop(v18);
fclose(pfile);
sprintf(filename, "%d.map", v11);
pfile = fopen(filename, "w");
writemap(pfile, v18);
fclose(pfile);
gend();
exit(0);
}
gmove(20LL, 10LL);
gputs("Can't Create or Append Player Cmd's ");
gclrline();
ggetch();
exit(4);
}
v5 = argv[1];
v6 = 0x200000000801LL;
do
{
v7 = (unsigned int)(v4 - 47);
if ( (unsigned __int8)v7 <= 0x2Du && _bittest64(&v6, v7) )
{
gmove(20LL, 18LL);
gprintf("Invalid Directory Name %s\n", v3);
ggetch();
gend();
exit(1);
}
v4 = *(unsigned __int8 *)++v5;
}
while ( (_BYTE)v4 );
goto LABEL_15;
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
void nation_news()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void nation_news(char *name, int city)
{
    ;
}

variable_types:
name
char *

variable_types: city int


markup

decompiled_code:
__int64 __fastcall markup(int a1, int a2)
{
int v2; // er14
int v3; // er13
int v4; // er15
unsigned int v5; // er12
int v6; // er14
int v7; // er13
unsigned int v8; // ebp
int v9; // ebx
int v10; // edx
__int64 result; // rax

v2 = gran * (a1 / gran);
v3 = gran * (a2 / gran);
v4 = v3 - 7;
v5 = v2 - 7;
v6 = v2 + 9;
v7 = v3 + 9;
do
{
v8 = v4;
do
{
v9 = fixrow(v5);
v10 = fixcol(v8);
result = pmap + 160LL * v9;
if ( !*(_BYTE *)(result + v10) )
*(_BYTE *)(result + v10) = 3;
++v8;
}
while ( v8 != v7 );
++v5;
}
while ( v5 != v6 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
int v2; // er14
int v3; // er13
int v4; // er15
unsigned int v5; // er12
int v6; // er14
int v7; // er13
unsigned int v8; // ebp
int v9; // ebx
int v10; // edx
__int64 result; // rax

v2 = gran * (a1 / gran);
v3 = gran * (a2 / gran);
v4 = v3 - 7;
v5 = v2 - 7;
v6 = v2 + 9;
v7 = v3 + 9;
do
{
v8 = v4;
do
{
v9 = fixrow(v5);
v10 = fixcol(v8);
result = pmap + 160LL * v9;
if ( !*(_BYTE *)(result + v10) )
*(_BYTE *)(result + v10) = 3;
++v8;
}
while ( v8 != v7 );
++v5;
}
while ( v5 != v6 );
return result;
}

original_code:
static void markup(int r, int c)
{
    int r_origin, c_origin, i, j, fixed_r, fixed_c;
    r_origin = (r / gran) * gran - 7;
    c_origin = (c / gran) * gran - 7;
    for(i = 0; i < 16; i++)
        for(j = 0; j < 16; j++) {
            fixed_r = fixrow(r_origin+i);
            fixed_c = fixcol(c_origin+j);
            if(pmap[fixed_r][fixed_c] == 0)
                pmap[fixed_r][fixed_c] = 3;
        }
}

canonical_original_code:
static void markup(int r, int c)
{
    int r_origin; int c_origin; int i; int j; int fixed_r; int fixed_c;
    r_origin = (r / gran) * gran - 7;
    c_origin = (c / gran) * gran - 7;
    for(i = 0; i < 16; i++)
        for(j = 0; j < 16; j++) {
            fixed_r = fixrow(r_origin+i);
            fixed_c = fixcol(c_origin+j);
            if(pmap[fixed_r][fixed_c] == 0)
                pmap[fixed_r][fixed_c] = 3;
        }
}

variable_types: r int


countargs

decompiled_code:
__int64 __fastcall countargs(_QWORD *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

if ( !*a1 )
return 0;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

if ( !*a1 )
return 0;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

original_code:
int countargs(char **argv)
{
 int i;
 for(i = 0; argv[i] != ((void *)0); i++);
 return i;
}

variable_types:
argv
char **


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // ebx
FILE *v5; // rbp
unsigned int v6; // er12
FILE *v7; // rbx
char v8[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc <= 1 || (*(_QWORD *)&argc = argv[1], chdir(*(const char **)&argc) != -1) )
{
ginit(*(_QWORD *)&argc, argv, envp);
gmove(5LL, 10LL);
gputs("WARTOOL 1.0 Game Editor for Solomoriah's WAR!");
gmove(6LL, 10LL);
gputs("Copyright 1994, J. Christopher Gonnerman");
gmove(7LL, 10LL);
gputs("All Rights Reserved.");
v3 = loadmap();
if ( v3 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Map (%s)\n", *((const char **)&errors + v3));
ggetch();
gend();
exit(1);
}
v4 = loadsave();
if ( v4 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Game Save (%s)\n", *((const char **)&errors + (int)v4));
ggetch();
gend();
exit(1);
}
gmove(20LL, 10LL);
gputs("Reading Master Commands... ");
grefresh();
v5 = fopen("master.cmd", "r");
if ( v5 )
{
while ( fgets(v8, 128, v5) )
{
v6 = execpriv(v8);
if ( v6 )
{
gmove(20LL, 10LL);
gprintf("Master Cmd Failed, Line %d, Code %d ", v4 + 1, v6);
ggetch();
gend();
exit(2);
}
++v4;
if ( v4 == 10 * ((int)v4 / 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v4);
grefresh();
}
}
fclose(v5);
}
gmove(20LL, 10LL);
gputs("Press Any Key to Begin... ");
gclrline();
ggetch();
mainscreen();
toolmain();
unlink("game.bak");
if ( rename("game.sav", "game.bak") )
v7 = fopen("game.sv!", "w");
else
v7 = fopen("game.sav", "w");
savegame(v7);
fclose(v7);
endwin();
exit(0);
}
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", argv[1]);
ggetch();
gend();
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // ebx
FILE *v5; // rbp
unsigned int v6; // er12
FILE *v7; // rbx
char v8[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc <= 1 || (*(_QWORD *)&argc = argv[1], chdir(*(const char **)&argc) != -1) )
{
ginit(*(_QWORD *)&argc, argv, envp);
gmove(5LL, 10LL);
gputs("WARTOOL 1.0 Game Editor for Solomoriah's WAR!");
gmove(6LL, 10LL);
gputs("Copyright 1994, J. Christopher Gonnerman");
gmove(7LL, 10LL);
gputs("All Rights Reserved.");
v3 = loadmap();
if ( v3 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Map (%s)\n", *((const char **)&errors + v3));
ggetch();
gend();
exit(1);
}
v4 = loadsave();
if ( v4 )
{
gmove(20LL, 10LL);
gprintf("Error Loading Game Save (%s)\n", *((const char **)&errors + (int)v4));
ggetch();
gend();
exit(1);
}
gmove(20LL, 10LL);
gputs("Reading Master Commands... ");
grefresh();
v5 = fopen("master.cmd", "r");
if ( v5 )
{
while ( fgets(v8, 128, v5) )
{
v6 = execpriv(v8);
if ( v6 )
{
gmove(20LL, 10LL);
gprintf("Master Cmd Failed, Line %d, Code %d ", v4 + 1, v6);
ggetch();
gend();
exit(2);
}
++v4;
if ( v4 == 10 * ((int)v4 / 10) )
{
gmove(20LL, 48LL);
gprintf("%3d lines", v4);
grefresh();
}
}
fclose(v5);
}
gmove(20LL, 10LL);
gputs("Press Any Key to Begin... ");
gclrline();
ggetch();
mainscreen();
func2();
unlink("game.bak");
if ( rename("game.sav", "game.bak") )
v7 = fopen("game.sv!", "w");
else
v7 = fopen("game.sav", "w");
savegame(v7);
fclose(v7);
endwin();
exit(0);
}
gmove(20LL, 18LL);
gprintf("Error Setting Directory %s\n", argv[1]);
ggetch();
gend();
exit(1);
}


mailer

decompiled_code:
void mailer()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void mailer()
{
 ;
}

canonical_original_code:
void mailer()
{
 ;
}


reader

decompiled_code:
void reader()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void reader()
{
 ;
}

canonical_original_code:
void reader()
{
 ;
}


decompiled_code:
__int64 __fastcall lo3r(int a1)
{
return (unsigned int)(lrand48() % a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(lrand48() % a1);
}

original_code:
int lo3r(int max)
{
 int res1, res2, res3;
 return lrand48() % max;
 res1 = lrand48() % max;
 res2 = lrand48() % max;
 res3 = lrand48() % max;
 if(res1 < res2 && res1 < res3)
  return res1;
 if(res2 < res3)
  return res2;
 return res3;
}

canonical_original_code:
int lo3r(int max)
{
 int res1; int res2; int res3;
 return lrand48() % max;
 res1 = lrand48() % max;
 res2 = lrand48() % max;
 res3 = lrand48() % max;
 if(res1 < res2 && res1 < res3)
  return res1;
 if(res2 < res3)
  return res2;
 return res3;
}

variable_types: max int


docount

original_code:
int docount(int r, int c, int mode)
{
 int i, j, k, pos, diff;
 pos = 0;
 for(i = 0; i < 5; i++) {
  count[i] = 0;
  if(map[r][c] == terrain[i])
   pos = i;
 }
 for(i = -1; i < 2; i++)
  for(j = -1; j < 2; j++)
    for(k = 0; k < 5; k++)
     if(map[fixrow(r+i)][fixcol(c+j)] == terrain[k]) {
      diff = 1;
      if(r == i && c == j)
       diff = 2;
      if(terrain[k] != '~' || mode)
       count[k] += diff;
     }
 i = 0;
 j = -1;
 for(k = 1; k < 5; k++) {
  if(count[k] > count[i])
   i = k;
  if(count[k] > 5)
   j = k;
 }
 return i;
}

canonical_original_code:
int docount(int r, int c, int mode)
{
 int i; int j; int k; int pos; int diff;
 pos = 0;
 for(i = 0; i < 5; i++) {
  count[i] = 0;
  if(map[r][c] == terrain[i])
   pos = i;
 }
 for(i = -1; i < 2; i++)
  for(j = -1; j < 2; j++)
    for(k = 0; k < 5; k++)
     if(map[fixrow(r+i)][fixcol(c+j)] == terrain[k]) {
      diff = 1;
      if(r == i && c == j)
       diff = 2;
      if(terrain[k] != '~' || mode)
       count[k] += diff;
     }
 i = 0;
 j = -1;
 for(k = 1; k < 5; k++) {
  if(count[k] > count[i])
   i = k;
  if(count[k] > 5)
   j = k;
 }
 return i;
}

variable_types: mode int

variable_types: i int

variable_types: k int

variable_types: pos int

variable_types: diff int


generation

original_code:
int generation()
{
 int i, j, trans;
 char old;
 trans = 0;
 for(i = 0; i < maprows; i++)
  for(j = 0; j < mapcols; j++)
   switch(map[i][j]) {
   case '~' :
    break;
   default :
    old = map[i][j];
    mapb[i][j] = terrain[docount(i, j, 1)];
    if(old != mapb[i][j])
     trans++;
    break;
   }
 for(i = 0; i < maprows; i++)
  for(j = 0; j < mapcols; j++)
   map[i][j] = mapb[i][j];
 return trans;
}

canonical_original_code:
int generation()
{
 int i; int j; int trans;
 char old;
 trans = 0;
 for(i = 0; i < maprows; i++)
  for(j = 0; j < mapcols; j++)
   switch(map[i][j]) {
   case '~' :
    break;
   default :
    old = map[i][j];
    mapb[i][j] = terrain[docount(i, j, 1)];
    if(old != mapb[i][j])
     trans++;
    break;
   }
 for(i = 0; i < maprows; i++)
  for(j = 0; j < mapcols; j++)
   map[i][j] = mapb[i][j];
 return trans;
}

variable_types: j int

variable_types: trans int

variable_types: old char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
int i; // ebx
unsigned int v5; // eax

maprows = 64;
mapcols = 64;
v3 = time(0LL);
srand48(v3);
genmap();
for ( i = 1; ; ++i )
{
v5 = generation(v3, argv);
if ( mapcols * maprows / 30 >= (int)v5 || i > 10 )
break;
argv = (const char **)"Generation %4d -- %5d Transforms\n";
v3 = (__int64)stderr;
fprintf(stderr, "Generation %4d -- %5d Transforms\n", (unsigned int)i, v5);
}
fprintf(stderr, "Generation %4d -- %5d Transforms\n", (unsigned int)i, v5);
savemap();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
int i; // ebx
unsigned int v5; // eax

maprows = 64;
mapcols = 64;
v3 = time(0LL);
srand48(v3);
func2();
for ( i = 1; ; ++i )
{
v5 = func6(v3, argv);
if ( mapcols * maprows / 30 >= (int)v5 || i > 10 )
break;
argv = (const char **)"Generation %4d -- %5d Transforms\n";
v3 = (__int64)stderr;
fprintf(stderr, "Generation %4d -- %5d Transforms\n", (unsigned int)i, v5);
}
fprintf(stderr, "Generation %4d -- %5d Transforms\n", (unsigned int)i, v5);
func0();
return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
gamedir
char *

variable_types:
p
char *


fixcol

decompiled_code:
__int64 __fastcall fixcol(int a1)
{
return (unsigned int)((mapcols + a1) % mapcols);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
return (unsigned int)((mapcols + a1) % mapcols);
}

original_code:
int fixcol(int col)
{
 return (col + mapcols) % mapcols;
}

variable_types: col int


fixrow

decompiled_code:
__int64 __fastcall fixrow(int a1)
{
return (unsigned int)((maprows + a1) % maprows);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
return (unsigned int)((maprows + a1) % maprows);
}

original_code:
int fixrow(int row)
{
 return (row + maprows) % maprows;
}

variable_types: row int


execuser

decompiled_code:
__int64 execuser()
{
const char **v0; // rax
const char *v1; // rbp
const char **v2; // r12
__int64 v3; // rbx
const char *v4; // rdi
__int64 (__fastcall *v6)(_QWORD, const char **); // rbx
unsigned int v7; // eax

v0 = (const char **)parseline();
v1 = *v0;
if ( !*v0 )
return 5LL;
v2 = v0;
v3 = 0LL;
v4 = "move-army";
while ( strcmp(v4, v1) )
{
++v3;
v4 = (&table)[2 * v3];
if ( !v4 )
return 6LL;
}
v6 = (__int64 (__fastcall *)(_QWORD, const char **))*(&off_5E8 + 2 * (int)v3);
v7 = countargs(v2);
return v6(v7, v2);
}

canonical_decompiled_code:
__int64 func4()
{
const char **v0; // rax
const char *v1; // rbp
const char **v2; // r12
__int64 v3; // rbx
const char *v4; // rdi
__int64 (__fastcall *v6)(_QWORD, const char **); // rbx
unsigned int v7; // eax

v0 = (const char **)parseline();
v1 = *v0;
if ( !*v0 )
return 5LL;
v2 = v0;
v3 = 0LL;
v4 = "move-army";
while ( strcmp(v4, v1) )
{
++v3;
v4 = (&table)[2 * v3];
if ( !v4 )
return 6LL;
}
v6 = (__int64 (__fastcall *)(_QWORD, const char **))*(&off_5E8 + 2 * (int)v3);
v7 = countargs(v2);
return v6(v7, v2);
}

original_code:
int execuser(char *line)
{
    char **args;
    int n;
    args = parseline(line);
    if(args[0] == ((void *)0))
        return 5;
    for(n = 0; table[n].command != ((void *)0); n++)
        if(strcmp(table[n].command, args[0]) == 0)
            break;
    if(table[n].command == ((void *)0))
        return 6;
    return table[n].func(countargs(args), args);
}

variable_types:
line
char *

variable_types:
args
char **

variable_types: n int


setprod

decompiled_code:
__int64 __fastcall setprod(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // eax

v2 = atoi(*(const char **)(a2 + 8));
v3 = atoi(*(const char **)(a2 + 16));
cities[24 * v2 + 21] = v3;
cities[24 * v2 + 22] = cities[24 * v2 + 13 + v3];
fprintf(mfile, "set-produce %s %s\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // eax

v2 = atoi(*(const char **)(a2 + 8));
v3 = atoi(*(const char **)(a2 + 16));
cities[24 * v2 + 21] = v3;
cities[24 * v2 + 22] = cities[24 * v2 + 13 + v3];
fprintf(mfile, "set-produce %s %s\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
return 0LL;
}

original_code:
static int setprod(int argc, char *argv[])
{
    int c;
    c = atoi(argv[1]);
    cities[c].prod_type = atoi(argv[2]);
    cities[c].turns_left = cities[c].times[cities[c].prod_type];
    fprintf(mfile, "set-produce %s %s\n", argv[1], argv[2]);
    return 0;
}

variable_types: c int


movearmy

original_code:
static int movearmy(int argc, char *argv[])
{
    int a, b;
    a = atoi(argv[1]);
    if(armies[a].nation == -1)
        return 0;
    if(argc > 5) {
        b = atoi(argv[5]);
        if(a != b && armies[b].nation == -1)
            return 0;
    }
    b = armies[a].move_left;
    b -= atoi(argv[2]);
    if(b < 0)
        b = 0;
    armies[a].move_left = b;
    armies[a].r = atoi(argv[3]);
    armies[a].c = atoi(argv[4]);
    if(argc > 5)
        fprintf(mfile, "move-army %s %s %s %s %s\n",
            argv[1], argv[2], argv[3], argv[4], argv[5]);
    else
        fprintf(mfile, "move-army %s %s %s %s\n",
            argv[1], argv[2], argv[3], argv[4]);
    return 0;
}

canonical_original_code:
static int movearmy(int argc, char *argv[])
{
    int a; int b;
    a = atoi(argv[1]);
    if(armies[a].nation == -1)
        return 0;
    if(argc > 5) {
        b = atoi(argv[5]);
        if(a != b && armies[b].nation == -1)
            return 0;
    }
    b = armies[a].move_left;
    b -= atoi(argv[2]);
    if(b < 0)
        b = 0;
    armies[a].move_left = b;
    armies[a].r = atoi(argv[3]);
    armies[a].c = atoi(argv[4]);
    if(argc > 5)
        fprintf(mfile, "move-army %s %s %s %s %s\n",
            argv[1], argv[2], argv[3], argv[4], argv[5]);
    else
        fprintf(mfile, "move-army %s %s %s %s\n",
            argv[1], argv[2], argv[3], argv[4]);
    return 0;
}

variable_types: a int

variable_types: b int


namearmy

decompiled_code:
__int64 __fastcall namearmy(__int64 a1, __int64 a2)
{
__int64 v2; // rbx

v2 = 30LL * atoi(*(const char **)(a2 + 8));
if ( *(_WORD *)(armies + v2 + 16) != 0xFFFF )
{
strncpy((char *)(armies + v2), *(const char **)(a2 + 16), 0xEuLL);
*(_BYTE *)(armies + v2 + 14) = 0;
fprintf(mfile, "name-army %s '%s'\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rbx

v2 = 30LL * atoi(*(const char **)(a2 + 8));
if ( *(_WORD *)(armies + v2 + 16) != 0xFFFF )
{
strncpy((char *)(armies + v2), *(const char **)(a2 + 16), 0xEuLL);
*(_BYTE *)(armies + v2 + 14) = 0;
fprintf(mfile, "name-army %s '%s'\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
}
return 0LL;
}

original_code:
static int namearmy(int argc, char *argv[])
{
    int a;
    a = atoi(argv[1]);
    if(armies[a].nation == -1)
        return 0;
    strncpy(armies[a].name, argv[2], 14);
    armies[a].name[14] = '\0';
    fprintf(mfile, "name-army %s '%s'\n", argv[1], argv[2]);
    return 0;
}


makearmy

decompiled_code:
__int64 __fastcall makearmy(__int64 a1, const char **a2)
{
char v4; // si
int v5; // eax
__int64 v6; // rdx

if ( armycnt <= 1999 )
{
strncpy((char *)(armies + 30LL * armycnt), a2[1], 0xEuLL);
*(_BYTE *)(armies + 30LL * armycnt + 14) = 0;
*(_WORD *)(armies + 30LL * armycnt + 16) = atoi(a2[2]);
*(_WORD *)(armies + 30LL * armycnt + 18) = atoi(a2[3]);
*(_WORD *)(armies + 30LL * armycnt + 20) = atoi(a2[4]);
*(_BYTE *)(armies + 30LL * armycnt + 22) = atoi(a2[5]);
*(_BYTE *)(armies + 30LL * armycnt + 23) = atoi(a2[6]);
*(_BYTE *)(armies + 30LL * armycnt + 25) = atoi(a2[7]);
*(_BYTE *)(armies + 30LL * armycnt + 27) = atoi(a2[7]);
*(_BYTE *)(armies + 30LL * armycnt + 26) = atoi(a2[8]);
v4 = atoi(a2[9]);
v5 = armycnt;
v6 = armies + 30LL * armycnt;
*(_BYTE *)(v6 + 28) = v4;
*(_BYTE *)(v6 + 24) = 0;
armycnt = v5 + 1;
fprintf(
mfile,
"make-army '%s' %s %s %s %s %s %s %s %s\n",
a2[1],
a2[2],
a2[3],
a2[4],
a2[5],
a2[6],
a2[7],
a2[8],
a2[9]);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char **a2)
{
char v4; // si
int v5; // eax
__int64 v6; // rdx

if ( armycnt <= 1999 )
{
strncpy((char *)(armies + 30LL * armycnt), a2[1], 0xEuLL);
*(_BYTE *)(armies + 30LL * armycnt + 14) = 0;
*(_WORD *)(armies + 30LL * armycnt + 16) = atoi(a2[2]);
*(_WORD *)(armies + 30LL * armycnt + 18) = atoi(a2[3]);
*(_WORD *)(armies + 30LL * armycnt + 20) = atoi(a2[4]);
*(_BYTE *)(armies + 30LL * armycnt + 22) = atoi(a2[5]);
*(_BYTE *)(armies + 30LL * armycnt + 23) = atoi(a2[6]);
*(_BYTE *)(armies + 30LL * armycnt + 25) = atoi(a2[7]);
*(_BYTE *)(armies + 30LL * armycnt + 27) = atoi(a2[7]);
*(_BYTE *)(armies + 30LL * armycnt + 26) = atoi(a2[8]);
v4 = atoi(a2[9]);
v5 = armycnt;
v6 = armies + 30LL * armycnt;
*(_BYTE *)(v6 + 28) = v4;
*(_BYTE *)(v6 + 24) = 0;
armycnt = v5 + 1;
fprintf(
mfile,
"make-army '%s' %s %s %s %s %s %s %s %s\n",
a2[1],
a2[2],
a2[3],
a2[4],
a2[5],
a2[6],
a2[7],
a2[8],
a2[9]);
}
return 0LL;
}

original_code:
static int makearmy(int argc, char *argv[])
{
    if(armycnt >= 2000)
        return 0;
    strncpy(armies[armycnt].name, argv[1], 14);
    armies[armycnt].name[14] = '\0';
    armies[armycnt].nation = atoi(argv[2]);
    armies[armycnt].r = atoi(argv[3]);
    armies[armycnt].c = atoi(argv[4]);
    armies[armycnt].combat = atoi(argv[5]);
    armies[armycnt].hero = atoi(argv[6]);
    armies[armycnt].move_rate = atoi(argv[7]);
    armies[armycnt].move_left = atoi(argv[7]);
    armies[armycnt].move_tbl = atoi(argv[8]);
    armies[armycnt].special_mv = atoi(argv[9]);
    armies[armycnt].eparm1 = 0;
    armycnt++;
    fprintf(mfile, "make-army '%s' %s %s %s %s %s %s %s %s\n",
        argv[1], argv[2], argv[3],
        argv[4], argv[5], argv[6],
        argv[7], argv[8], argv[9]);
    return 0;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
int v4; // eax
int v5; // eax
int v6; // er12
FILE *v7; // rbp
int v8; // ebx
unsigned int v9; // eax
__int64 i; // r14
const char *v11; // rax
int v12; // ebx
unsigned int v13; // eax
unsigned int v14; // er13
FILE *v15; // rbp
const char *v16; // rax
FILE *v17; // rbx
struct stat v18; // [rsp+0h] [rbp-178h] BYREF
char s[128]; // [rsp+90h] [rbp-E8h] BYREF
char name[104]; // [rsp+110h] [rbp-68h] BYREF

fprintf(stdout, "\nWARUPD Version %d.%d \"Code by Solomoriah\"\n", major_ver, minor_ver);
fwrite("Copyright 1993, 1994, 2001, 2013, Chris Gonnerman\n", 1uLL, 0x32uLL, stdout);
fwrite("All Rights Reserved.\n\n", 1uLL, 0x16uLL, stdout);
if ( argc <= 1 || chdir(argv[1]) != -1 )
{
v3 = time(0LL);
srand48(v3);
v4 = loadmap();
if ( v4 )
{
printf("Error Loading Map (%s)\n", *((const char **)&errors + v4));
exit(1);
}
v5 = loadsave();
v6 = v5;
if ( !v5 )
{
if ( world )
printf("World: %s\n", &world);
news = fopen("news", "a");
if ( news )
{
printf("Turn Update %d\n\n", gen);
if ( world )
{
fwrite(" \b", 1uLL, 2uLL, news);
fputs(&world, news);
}
else
{
fwrite(" \bSolomoriah's WAR!", 1uLL, 0x13uLL, news);
}
fprintf(news, " News Report Turn %d\n\n", gen);
puts("Reading Master Commands...");
v7 = fopen("master.cmd", "r");
v8 = v6;
if ( v7 )
{
while ( fgets(s, 128, v7) )
{
v9 = execpriv(s);
if ( v9 )
{
printf("Master Cmd Failed, Line %d, Code %d\n", (unsigned int)(v8 + 1), v9);
exit(2);
}
++v8;
}
fclose(v7);
}
mfile = fopen("master.cmd", "a");
if ( mfile )
{
puts("Reading Player Commands...");
shuffle((__int64)"Reading Player Commands...", (__int64)"a");
for ( i = 0LL; nlcnt > (int)i; ++i )
{
v14 = nlist[i];
sprintf(name, "%d.cmd", *((unsigned int *)&nations + 8 * (int)v14 + 7));
v15 = fopen(name, "r");
if ( v15 )
{
v11 = (const char *)nationcity(v14);
printf("\n%s of %s moves...\n", (const char *)&nations + 32 * (int)v14, v11);
*((_DWORD *)&nations + 8 * (int)v14 + 6) = 0;
v12 = v6;
while ( fgets(s, 128, v15) )
{
v13 = execuser(s);
if ( v13 )
{
printf("Player Cmd Failed, Line %d, Code %d ", (unsigned int)(v12 + 1), v13);
exit(2);
}
++v12;
}
fclose(v15);
unlink(name);
}
else
{
v16 = (const char *)nationcity(v14);
printf("\n%s of %s is idle.\n", (const char *)&nations + 32 * (int)v14, v16);
++*((_DWORD *)&nations + 8 * (int)v14 + 6);
}
combat(v14);
if ( *((int *)&nations + 8 * (int)v14 + 6) <= 6 )
creator(v14);
else
deserter(v14);
}
puts("Global Update...\n");
execpriv("global-update");
fwrite("global-update\n", 1uLL, 0xEuLL, mfile);
fclose(news);
fclose(mfile);
mail_line(0LL, 0xFFFFFFFF);
sprintf(name, "master.%03d", gen);
if ( rename("master.cmd", name) != -1 )
{
sprintf(name, "game.%03d", gen);
if ( stat("game.sav", &v18) || rename("game.sav", name) != -1 )
{
v17 = fopen("game.sav", "w");
if ( v17 )
{
if ( gen > 4 )
{
sprintf(name, "master.%03d", (unsigned int)(gen - 4));
unlink(name);
sprintf(name, "game.%03d", (unsigned int)(gen - 4));
unlink(name);
}
savegame(v17);
fclose(v17);
exit(0);
}
puts("Can't Create New Save File");
exit(9);
}
puts("Can't Rename Game Save");
exit(9);
}
puts("Can't Rename Master Cmd File");
exit(9);
}
puts("Can't Append to Master File");
exit(5);
}
printf("Error Writing News File <%s>\n", "news");
exit(10);
}
printf("Error Loading Game Save (%s)\n", *((const char **)&errors + v5));
exit(1);
}
fprintf(stderr, "Error Setting Directory %s\n", argv[1]);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
int v4; // eax
int v5; // eax
int v6; // er12
FILE *v7; // rbp
int v8; // ebx
unsigned int v9; // eax
__int64 i; // r14
const char *v11; // rax
int v12; // ebx
unsigned int v13; // eax
unsigned int v14; // er13
FILE *v15; // rbp
const char *v16; // rax
FILE *v17; // rbx
struct stat v18; // [rsp+0h] [rbp-178h] BYREF
char s[128]; // [rsp+90h] [rbp-E8h] BYREF
char name[104]; // [rsp+110h] [rbp-68h] BYREF

fprintf(stdout, "\nWARUPD Version %d.%d \"Code by Solomoriah\"\n", major_ver, minor_ver);
fwrite("Copyright 1993, 1994, 2001, 2013, Chris Gonnerman\n", 1uLL, 0x32uLL, stdout);
fwrite("All Rights Reserved.\n\n", 1uLL, 0x16uLL, stdout);
if ( argc <= 1 || chdir(argv[1]) != -1 )
{
v3 = time(0LL);
srand48(v3);
v4 = loadmap();
if ( v4 )
{
printf("Error Loading Map (%s)\n", *((const char **)&errors + v4));
exit(1);
}
v5 = loadsave();
v6 = v5;
if ( !v5 )
{
if ( world )
printf("World: %s\n", &world);
news = fopen("news", "a");
if ( news )
{
printf("Turn Update %d\n\n", gen);
if ( world )
{
fwrite(" \b", 1uLL, 2uLL, news);
fputs(&world, news);
}
else
{
fwrite(" \bSolomoriah's WAR!", 1uLL, 0x13uLL, news);
}
fprintf(news, " News Report Turn %d\n\n", gen);
puts("Reading Master Commands...");
v7 = fopen("master.cmd", "r");
v8 = v6;
if ( v7 )
{
while ( fgets(s, 128, v7) )
{
v9 = execpriv(s);
if ( v9 )
{
printf("Master Cmd Failed, Line %d, Code %d\n", (unsigned int)(v8 + 1), v9);
exit(2);
}
++v8;
}
fclose(v7);
}
mfile = fopen("master.cmd", "a");
if ( mfile )
{
puts("Reading Player Commands...");
func0((__int64)"Reading Player Commands...", (__int64)"a");
for ( i = 0LL; nlcnt > (int)i; ++i )
{
v14 = nlist[i];
sprintf(name, "%d.cmd", *((unsigned int *)&nations + 8 * (int)v14 + 7));
v15 = fopen(name, "r");
if ( v15 )
{
v11 = (const char *)nationcity(v14);
printf("\n%s of %s moves...\n", (const char *)&nations + 32 * (int)v14, v11);
*((_DWORD *)&nations + 8 * (int)v14 + 6) = 0;
v12 = v6;
while ( fgets(s, 128, v15) )
{
v13 = execuser(s);
if ( v13 )
{
printf("Player Cmd Failed, Line %d, Code %d ", (unsigned int)(v12 + 1), v13);
exit(2);
}
++v12;
}
fclose(v15);
unlink(name);
}
else
{
v16 = (const char *)nationcity(v14);
printf("\n%s of %s is idle.\n", (const char *)&nations + 32 * (int)v14, v16);
++*((_DWORD *)&nations + 8 * (int)v14 + 6);
}
combat(v14);
if ( *((int *)&nations + 8 * (int)v14 + 6) <= 6 )
creator(v14);
else
deserter(v14);
}
puts("Global Update...\n");
execpriv("global-update");
fwrite("global-update\n", 1uLL, 0xEuLL, mfile);
fclose(news);
fclose(mfile);
func1(0LL, 0xFFFFFFFF);
sprintf(name, "master.%03d", gen);
if ( rename("master.cmd", name) != -1 )
{
sprintf(name, "game.%03d", gen);
if ( stat("game.sav", &v18) || rename("game.sav", name) != -1 )
{
v17 = fopen("game.sav", "w");
if ( v17 )
{
if ( gen > 4 )
{
sprintf(name, "master.%03d", (unsigned int)(gen - 4));
unlink(name);
sprintf(name, "game.%03d", (unsigned int)(gen - 4));
unlink(name);
}
savegame(v17);
fclose(v17);
exit(0);
}
puts("Can't Create New Save File");
exit(9);
}
puts("Can't Rename Game Save");
exit(9);
}
puts("Can't Rename Master Cmd File");
exit(9);
}
puts("Can't Append to Master File");
exit(5);
}
printf("Error Writing News File <%s>\n", "news");
exit(10);
}
printf("Error Loading Game Save (%s)\n", *((const char **)&errors + v5));
exit(1);
}
fprintf(stderr, "Error Setting Directory %s\n", argv[1]);
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
    struct stat st_buf;
    char *gamedir, *p;
    if(chdir("/war") == -1) {
        gmove(20,18);
        gprintf("Error Setting Directory %s\n", "/war");
        ggetch();
        gend();
        exit(1);
    }
    if(argc > 1) {
        gamedir = argv[1];
        if(gamedir[0] == '.' && gamedir[1] == '.' && gamedir[2] == '\0') {
            gmove(20,18);
            gprintf("Invalid Directory Name %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
        for(p = gamedir; *p; p++) {
            if(*p == '/' || *p == '\\' || *p == ':') {
                gmove(20,18);
                gprintf("Invalid Directory Name %s\n", gamedir);
                ggetch();
                gend();
                exit(1);
            }
        }
        if(chdir(gamedir) == -1) {
            gmove(20,18);
            gprintf("Error Setting Directory %s\n", gamedir);
            ggetch();
            gend();
            exit(1);
        }
    }
    ginit();
    reader("news", 0);
    gend();
    exit(0);
}


repo: Spacerat/irobot-follow-ball

decompiled_code:
int roombath_init()
{
return pthread_mutex_init(&command_mutex, 0LL);
}

canonical_decompiled_code:
int func0()
{
return pthread_mutex_init(&command_mutex, 0LL);
}

original_code:
void roombath_init() {
 pthread_mutex_init(&command_mutex, ((void *)0));
}


decompiled_code:
int roombath_free()
{
return pthread_mutex_destroy(&command_mutex);
}

canonical_decompiled_code:
int func1()
{
return pthread_mutex_destroy(&command_mutex);
}

original_code:
void roombath_free() {
 pthread_mutex_destroy(&command_mutex);
}


decompiled_code:
int __fastcall roombath_direct_drive(int a1, int a2)
{
pthread_mutex_lock(&command_mutex);
leftSpeed = a1;
rightSpeed = a2;
next_command = 145;
return pthread_mutex_unlock(&command_mutex);
}

canonical_decompiled_code:
int __fastcall func2(int a1, int a2)
{
pthread_mutex_lock(&command_mutex);
leftSpeed = a1;
rightSpeed = a2;
next_command = 145;
return pthread_mutex_unlock(&command_mutex);
}

original_code:
void roombath_direct_drive(int l, int r) {
 pthread_mutex_lock(&command_mutex);
 leftSpeed = l;
 rightSpeed = r;
 next_command = 145;
 pthread_mutex_unlock(&command_mutex);
}

variable_types: l int

variable_types: r int


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

pthread_mutex_lock(&command_mutex);
bumpsRead = 1;
leftBump = a1;
rightBump = a2;
next_command = 142;
pthread_mutex_unlock(&command_mutex);
result = (unsigned __int8)bumpsRead;
while ( bumpsRead && run_thread )
;
return result;
}

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

pthread_mutex_lock(&command_mutex);
bumpsRead = 1;
leftBump = a1;
rightBump = a2;
next_command = 142;
pthread_mutex_unlock(&command_mutex);
result = (unsigned __int8)bumpsRead;
while ( bumpsRead && run_thread )
;
return result;
}

original_code:
void roombath_read_bumps(unsigned char * l, unsigned char * r) {
 pthread_mutex_lock(&command_mutex);
 bumpsRead = 1;
 leftBump = l;
 rightBump = r;
 next_command = 142;
 pthread_mutex_unlock(&command_mutex);
 while(bumpsRead && run_thread) {}
}

canonical_original_code:
void roombath_read_bumps(unsigned char *l, unsigned char *r) {
 pthread_mutex_lock(&command_mutex);
 bumpsRead = 1;
 leftBump = l;
 rightBump = r;
 next_command = 142;
 pthread_mutex_unlock(&command_mutex);
 while(bumpsRead && run_thread) {}
}

variable_types:
l
unsigned char *

variable_types:
r
unsigned char *


decompiled_code:
int roombath_thread_end()
{
pthread_mutex_lock(&command_mutex);
next_command = 10;
roomba_direct_drive(0LL, 0LL);
run_thread = 0;
return pthread_mutex_unlock(&command_mutex);
}

canonical_decompiled_code:
int func4()
{
pthread_mutex_lock(&command_mutex);
next_command = 10;
roomba_direct_drive(0LL, 0LL);
run_thread = 0;
return pthread_mutex_unlock(&command_mutex);
}

original_code:
void roombath_thread_end() {
 pthread_mutex_lock(&command_mutex);
 next_command = 10;
 roomba_direct_drive(0, 0);
 run_thread = 0;
 pthread_mutex_unlock(&command_mutex);
}


error

decompiled_code:
__int64 __fastcall error(const char *a1)
{
fprintf(stderr, "%s\n", a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
fprintf(stderr, "%s\n", a1);
return 0xFFFFFFFFLL;
}

original_code:
static int error(char* description) {
 fprintf(stderr, "%s\n", description);
 return -1;
}

canonical_original_code:
static int error(char *description) {
 fprintf(stderr, "%s\n", description);
 return -1;
}

variable_types:
description
char *


decompiled_code:
__int64 tty_init()
{
int v0; // edx
__int64 result; // rax
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(ser, &v2) == -1
|| cfsetispeed(&v2, 0x1001u) == -1
|| cfsetospeed(&v2, 0x1001u) == -1
|| (cfmakeraw(&v2), v0 = tcsetattr(ser, 0, &v2), result = 0LL, v0 == -1) )
{
result = error("tcgetattr failed");
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // edx
__int64 result; // rax
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(ser, &v2) == -1
|| cfsetispeed(&v2, 0x1001u) == -1
|| cfsetospeed(&v2, 0x1001u) == -1
|| (cfmakeraw(&v2), v0 = tcsetattr(ser, 0, &v2), result = 0LL, v0 == -1) )
{
result = func0("tcgetattr failed");
}
return result;
}

original_code:
int tty_init()
{
 struct termios termsettings;
 if (tcgetattr(ser, &termsettings) == -1)
  return error("tcgetattr failed");
 if (cfsetispeed(&termsettings, 0010001) == -1)
  return error("tcgetattr failed");
 if (cfsetospeed(&termsettings, 0010001) == -1)
  return error("tcgetattr failed");
 cfmakeraw(&termsettings);
 if (tcsetattr(ser, 0, &termsettings) == -1)
  return error("tcgetattr failed");
 return 0;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
__int64 roomba_close()
{
return (unsigned int)-(close(ser) == -1);
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)-(close(ser) == -1);
}

original_code:
int roomba_close()
{
 if (close(ser) == -1) return -1;
 return 0;
}


decompiled_code:
ssize_t __fastcall roomba_read(void *buf, int a2)
{
int v2; // ebx
ssize_t result; // rax
int i; // er12

if ( a2 <= 0 )
return 0LL;
v2 = a2;
result = read(ser, buf, a2);
for ( i = 0; ; result = read(ser, (char *)buf + i, v2) )
{
v2 -= result;
i += result;
if ( v2 <= 0 )
break;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(void *buf, int a2)
{
int v2; // ebx
ssize_t result; // rax
int i; // er12

if ( a2 <= 0 )
return 0LL;
v2 = a2;
result = read(ser, buf, a2);
for ( i = 0; ; result = read(ser, (char *)buf + i, v2) )
{
v2 -= result;
i += result;
if ( v2 <= 0 )
break;
}
return result;
}

original_code:
int roomba_read(unsigned char buf[], int size)
{
    int rcount;
 int offset = 0;
 while (size > 0)
 {
  rcount = read(ser, buf + offset, size);
  if (size == -1) return -1;
  size = size - rcount;
  offset = offset + rcount;
 }
 return rcount;
}

variable_types:
buf
unsigned char []

variable_types: size int

variable_types: rcount int

variable_types: offset int


decompiled_code:
__int64 __fastcall roomba_write(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return (unsigned int)-(write(ser, buf, 1uLL) != 1);
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return (unsigned int)-(write(ser, buf, 1uLL) != 1);
}

original_code:
int roomba_write(unsigned char outbyte)
{
 if (write(ser, &outbyte, 1) != 1) return -1;
    return 0;
}

variable_types:
outbyte
unsigned char


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

ser = open("/dev/ttyUSB0", 2);
if ( ser == -1 )
return error("Open failed");
result = tty_init();
if ( (_DWORD)result != -1 )
{
roomba_write(128);
delay(10LL);
if ( a1 == 1 )
{
roomba_write(132);
fwrite("Starting roomba in FULL mode!\n", 1uLL, 0x1EuLL, stdout);
}
else
{
roomba_write(131);
fwrite("Starting roomba in SAFE mode!\n", 1uLL, 0x1EuLL, stdout);
}
result = delay(10LL);
}
return result;
}

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

ser = open("/dev/ttyUSB0", 2);
if ( ser == -1 )
return func0("Open failed");
result = func1();
if ( (_DWORD)result != -1 )
{
func4(128);
delay(10LL);
if ( a1 == 1 )
{
func4(132);
fwrite("Starting roomba in FULL mode!\n", 1uLL, 0x1EuLL, stdout);
}
else
{
func4(131);
fwrite("Starting roomba in SAFE mode!\n", 1uLL, 0x1EuLL, stdout);
}
result = delay(10LL);
}
return result;
}

original_code:
int roomba_open(int mode)
{
 ser = open("/dev/ttyUSB0", 02);
 if (ser == -1)
  return error("Open failed");
 if (tty_init() == -1)
  return -1;
 roomba_write(128);
 delay(10);
 if (mode == 1) {
  roomba_write(132);
  fprintf(stdout, "Starting roomba in FULL mode!\n");
 }
 else {
  roomba_write(131);
  fprintf(stdout, "Starting roomba in SAFE mode!\n");
 }
 delay(10);
}

variable_types: mode int


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

result = roomba_write(137);
if ( (_DWORD)result != -1 )
{
result = roomba_write(SHIBYTE(a1));
if ( (_DWORD)result != -1 )
{
result = roomba_write(a1);
if ( (_DWORD)result != -1 )
{
result = roomba_write(SHIBYTE(a2));
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)roomba_write(a2) == -1);
}
}
}
return result;
}

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

result = func4(137);
if ( (_DWORD)result != -1 )
{
result = func4(SHIBYTE(a1));
if ( (_DWORD)result != -1 )
{
result = func4(a1);
if ( (_DWORD)result != -1 )
{
result = func4(SHIBYTE(a2));
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4(a2) == -1);
}
}
}
return result;
}

original_code:
int roomba_drive(short velocity, short radius)
{
 if (roomba_write(137) == -1) return -1;
 if (roomba_write((unsigned char)(velocity >> 8)) == -1) return -1;
 if (roomba_write((unsigned char)(velocity & 0xFF)) == -1) return -1;
 if (roomba_write((unsigned char)(radius >> 8)) == -1) return -1;
 if (roomba_write((unsigned char)(radius & 0xFF)) == -1) return -1;
 return 0;
}

variable_types: velocity short

variable_types: radius short


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

result = roomba_write(145);
if ( (_DWORD)result != -1 )
{
result = roomba_write(SHIBYTE(a2));
if ( (_DWORD)result != -1 )
{
result = roomba_write(a2);
if ( (_DWORD)result != -1 )
{
result = roomba_write(SHIBYTE(a1));
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)roomba_write(a1) == -1);
}
}
}
return result;
}

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

result = func4(145);
if ( (_DWORD)result != -1 )
{
result = func4(SHIBYTE(a2));
if ( (_DWORD)result != -1 )
{
result = func4(a2);
if ( (_DWORD)result != -1 )
{
result = func4(SHIBYTE(a1));
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4(a1) == -1);
}
}
}
return result;
}

original_code:
int roomba_direct_drive(short left_velocity, short right_velocity)
{
 if (roomba_write(145) == -1) return -1;
 if (roomba_write((unsigned char)(right_velocity >> 8)) == -1) return -1;
 if (roomba_write((unsigned char)(right_velocity & 0xFF)) == -1) return -1;
 if (roomba_write((unsigned char)(left_velocity >> 8)) == -1) return -1;
 if (roomba_write((unsigned char)(left_velocity & 0xFF)) == -1) return -1;
 return 0;
}


decompiled_code:
__int64 __fastcall roomba_led(char a1, char a2, char a3, char a4)
{
__int64 result; // rax

result = roomba_write(139);
if ( (_DWORD)result != -1 )
{
result = roomba_write((unsigned __int8)(2 * (a1 != 0)) | (unsigned __int8)(8 * (a2 != 0)));
if ( (_DWORD)result != -1 )
{
result = roomba_write(a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)roomba_write(a4) == -1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1, char a2, char a3, char a4)
{
__int64 result; // rax

result = func4(139);
if ( (_DWORD)result != -1 )
{
result = func4((unsigned __int8)(2 * (a1 != 0)) | (unsigned __int8)(8 * (a2 != 0)));
if ( (_DWORD)result != -1 )
{
result = func4(a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4(a4) == -1);
}
}
return result;
}

original_code:
int roomba_led(unsigned char play, unsigned char advance, unsigned char colour, unsigned char intensity)
{
 unsigned char flags = (play ? 2 : 0) | (advance ? 8 : 0);
 if (roomba_write(139) == -1) return -1;
 if (roomba_write(flags) == -1) return -1;
 if (roomba_write(colour) == -1) return -1;
 if (roomba_write(intensity) == -1) return -1;
 return 0;
}

variable_types:
play
unsigned char

variable_types:
advance
unsigned char

variable_types:
colour
unsigned char

variable_types:
intensity
unsigned char

variable_types:
flags
unsigned char


decompiled_code:
__int64 __fastcall roomba_read_bumps(bool *a1, _BYTE *a2)
{
char v2; // al
__int64 result; // rax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

if ( (unsigned int)roomba_write(142) == -1
|| (unsigned int)roomba_write(1) == -1
|| (unsigned int)roomba_read(buf, 10) == -1 )
{
return 0xFFFFFFFFLL;
}
v2 = buf[0];
*a1 = (buf[0] & 2) != 0;
result = v2 & 1;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(bool *a1, _BYTE *a2)
{
char v2; // al
__int64 result; // rax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

if ( (unsigned int)func4(142) == -1
|| (unsigned int)func4(1) == -1
|| (unsigned int)func3(buf, 10) == -1 )
{
return 0xFFFFFFFFLL;
}
v2 = buf[0];
*a1 = (buf[0] & 2) != 0;
result = v2 & 1;
*a2 = result;
return result;
}

original_code:
int roomba_read_bumps(unsigned char * left_bump, unsigned char * right_bump)
{
 unsigned char sensorPacket[10];
 if (roomba_write(142) == -1) return -1;
 if (roomba_write(1) == -1) return -1;
 if (roomba_read(sensorPacket, 10) == -1) return -1;
 *left_bump = (sensorPacket[0] & 2) ? 1 : 0;
 *right_bump = (sensorPacket[0] & 1) ? 1 : 0;
}

canonical_original_code:
int roomba_read_bumps(unsigned char *left_bump, unsigned char *right_bump)
{
 unsigned char sensorPacket[10];
 if (roomba_write(142) == -1) return -1;
 if (roomba_write(1) == -1) return -1;
 if (roomba_read(sensorPacket, 10) == -1) return -1;
 *left_bump = (sensorPacket[0] & 2) ? 1 : 0;
 *right_bump = (sensorPacket[0] & 1) ? 1 : 0;
}

variable_types:
sensorPacket
unsigned char [10]


delay

decompiled_code:
int __fastcall delay(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000LL * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000LL * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

original_code:
void delay(int ms)
{
 struct timespec t;
 t.tv_sec = ms / 1000;
 t.tv_nsec = (long)(ms % 1000) * 1000000;
 nanosleep(&t, ((void *)0));
}

variable_types: ms int

variable_types: struct timespec { long tv_sec; long tv_nsec; }


rotate

repo: Spekkio/GTK-with-GLX

decompiled_code:
__int64 rotate()
{
float v0; // xmm0_4
float v1; // xmm0_4

glMatrixMode(5888LL);
glLoadIdentity();
v0 = rot_y_vel * 0.001;
glRotatef(v0, 1.0, 1.0);
v1 = rot_z_vel * 0.001;
glRotatef(v1, 1.0, 1.0);
glMultMatrixf(&rotation_matrix);
return glGetFloatv(2982LL, &rotation_matrix);
}

canonical_decompiled_code:
__int64 func0()
{
float v0; // xmm0_4
float v1; // xmm0_4

glMatrixMode(5888LL);
glLoadIdentity();
v0 = rot_y_vel * 0.001;
glRotatef(v0, 1.0, 1.0);
v1 = rot_z_vel * 0.001;
glRotatef(v1, 1.0, 1.0);
glMultMatrixf(&rotation_matrix);
return glGetFloatv(2982LL, &rotation_matrix);
}

original_code:
void rotate()
{
  glMatrixMode(0x1700);
  glLoadIdentity();
  glRotatef(rot_y_vel*0.001, 1.0, 1.0, 1.0);
  glRotatef(rot_z_vel*0.001, 1.0, 1.0, 1.0);
  glMultMatrixf(rotation_matrix);
  glGetFloatv(0x0BA6, rotation_matrix);
}


drawSphere

decompiled_code:
__int64 drawSphere()
{
return glCallList((unsigned int)sphere);
}

canonical_decompiled_code:
__int64 func1()
{
return glCallList((unsigned int)sphere);
}

original_code:
void drawSphere()
{
  glCallList(sphere);
}


calcSphere

variable_types: it float

variable_types: i float

variable_types: a float


drawCube

original_code:
void drawCube(float size)
{
  glEnable(0x0DE1);
  glBindTexture(0x0DE1, texture[0]);
  glBegin(0x0007);
  glTexCoord2f(1.0/3.0, 2.0/3.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(2.0/3.0, 2.0/3.0); glVertex3f( size, -size, -size);
  glTexCoord2f(2.0/3.0, 3.0/3.0); glVertex3f( size, size, -size);
  glTexCoord2f(1.0/3.0, 3.0/3.0); glVertex3f(-size, size, -size);
  glTexCoord2f(1.0/3.0, 1.0/3.0); glVertex3f(-size, -size, size);
  glTexCoord2f(2.0/3.0, 1.0/3.0); glVertex3f( size, -size, size);
  glTexCoord2f(2.0/3.0, 2.0/3.0); glVertex3f( size, size, size);
  glTexCoord2f(1.0/3.0, 2.0/3.0); glVertex3f(-size, size, size);
  glTexCoord2f(2.0/3.0, 1.0/3.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(3.0/3.0, 1.0/3.0); glVertex3f(-size, -size, size);
  glTexCoord2f(3.0/3.0, 2.0/3.0); glVertex3f(-size, size, size);
  glTexCoord2f(2.0/3.0, 2.0/3.0); glVertex3f(-size, size, -size);
  glTexCoord2f(0.0/3.0, 1.0/3.0); glVertex3f( size, -size, -size);
  glTexCoord2f(1.0/3.0, 1.0/3.0); glVertex3f( size, -size, size);
  glTexCoord2f(1.0/3.0, 2.0/3.0); glVertex3f( size, size, size);
  glTexCoord2f(0.0/3.0, 2.0/3.0); glVertex3f( size, size, -size);
  glTexCoord2f(1.0/3.0, 0.0/3.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(2.0/3.0, 0.0/3.0); glVertex3f(-size, -size, size);
  glTexCoord2f(2.0/3.0, 1.0/3.0); glVertex3f( size, -size, size);
  glTexCoord2f(1.0/3.0, 1.0/3.0); glVertex3f( size, -size, -size);
  glTexCoord2f(0.0/3.0, 0.0/3.0);glVertex3f(-size, size, -size);
  glTexCoord2f(1.0/3.0, 0.0/3.0);glVertex3f(-size, size, size);
  glTexCoord2f(1.0/3.0, 1.0/3.0);glVertex3f( size, size, size);
  glTexCoord2f(0.0/3.0, 1.0/3.0);glVertex3f( size, size, -size);
  glEnd();
  glDisable(0x0DE1);
}

variable_types: size float


drawXORCube

decompiled_code:
__int64 __fastcall drawXORCube(__m128i a1)
{
unsigned int v1; // ebx
__m128i v2; // xmm3
float v3; // ebp

v1 = _mm_cvtsi128_si32(a1);
glEnable(3553LL);
glBindTexture(3553LL, (unsigned int)xor_texture);
glBegin(7LL);
glTexCoord2f(0.0, 0.0);
v2 = (__m128i)_mm_xor_ps((__m128)_mm_cvtsi32_si128(v1), (__m128)LC13);
v3 = COERCE_FLOAT(_mm_cvtsi128_si32(v2));
glVertex3f(*(float *)v2.m128i_i32, *(float *)v2.m128i_i32);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glEnd(*(float *)&v1, *(float *)&v1, v3);
return glDisable(3553LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(__m128i a1)
{
unsigned int v1; // ebx
__m128i v2; // xmm3
float v3; // ebp

v1 = _mm_cvtsi128_si32(a1);
glEnable(3553LL);
glBindTexture(3553LL, (unsigned int)xor_texture);
glBegin(7LL);
glTexCoord2f(0.0, 0.0);
v2 = (__m128i)_mm_xor_ps((__m128)_mm_cvtsi32_si128(v1), (__m128)LC13);
v3 = COERCE_FLOAT(_mm_cvtsi128_si32(v2));
glVertex3f(*(float *)v2.m128i_i32, *(float *)v2.m128i_i32);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, v3);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, v3);
glTexCoord2f(0.0, 0.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(1.0, 0.0);
glVertex3f(v3, *(float *)&v1);
glTexCoord2f(1.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glTexCoord2f(0.0, 1.0);
glVertex3f(*(float *)&v1, *(float *)&v1);
glEnd(*(float *)&v1, *(float *)&v1, v3);
return glDisable(3553LL);
}

original_code:
void drawXORCube(float size)
{
  glEnable(0x0DE1);
  glBindTexture(0x0DE1, xor_texture);
  glBegin(0x0007);
  glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, -size);
  glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
  glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
  glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, size);
  glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
  glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
  glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, size);
  glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, -size);
  glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, size);
  glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
  glTexCoord2f(0.0, 1.0); glVertex3f( size, size, -size);
  glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
  glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  glTexCoord2f(1.0, 1.0); glVertex3f( size, -size, size);
  glTexCoord2f(0.0, 1.0); glVertex3f( size, -size, -size);
  glTexCoord2f(0.0, 0.0); glVertex3f(-size, size, -size);
  glTexCoord2f(1.0, 0.0); glVertex3f(-size, size, size);
  glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
  glTexCoord2f(0.0, 1.0); glVertex3f( size, size, -size);
  glEnd();
  glDisable(0x0DE1);
}


expose

decompiled_code:
__int64 expose()
{
size_t v0; // rax
size_t v1; // rax
__int64 v2; // rax
float v4; // [rsp+1Ch] [rbp-10Ch]
char s[264]; // [rsp+20h] [rbp-108h] BYREF

v4 = (float)(int)wa[2] / (float)(int)wa[3];
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(v4 * -2.5, 2.5 * v4, -2.5, 2.5, 1.0, 100.0);
glMatrixMode(5888LL);
glLoadIdentity();
gluLookAt(10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
glMultMatrixf(&rotation_matrix);
glClear(16640LL);
drawXORCube((__m128i)(unsigned int)LC1);
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(0.0, (float)(int)wa[2], 0.0, (float)(int)wa[3], -1.0, 1.0);
glMatrixMode(5888LL);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
sprintf(s, "%4.1f seconds * %4.1f fps at %i x %i", TimeCounter, FPS, wa[2], wa[3]);
glRasterPos2i(10LL, 10LL);
v0 = strnlen(s, 0x100uLL);
glCallLists(v0, 5121LL, s);
strcpy(s, "GLX with GTK test program");
glRasterPos2i(10LL, wa[3] - 32);
v1 = strlen(s);
glCallLists(v1, 5121LL, s);
v2 = gdk_x11_drawable_get_xid(*(_QWORD *)(window + 80LL));
return glXSwapBuffers(disp, v2);
}

canonical_decompiled_code:
__int64 func5()
{
size_t v0; // rax
size_t v1; // rax
__int64 v2; // rax
float v4; // [rsp+1Ch] [rbp-10Ch]
char s[264]; // [rsp+20h] [rbp-108h] BYREF

v4 = (float)(int)wa[2] / (float)(int)wa[3];
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(v4 * -2.5, 2.5 * v4, -2.5, 2.5, 1.0, 100.0);
glMatrixMode(5888LL);
glLoadIdentity();
gluLookAt(10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
glMultMatrixf(&rotation_matrix);
glClear(16640LL);
func4((__m128i)(unsigned int)LC1);
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(0.0, (float)(int)wa[2], 0.0, (float)(int)wa[3], -1.0, 1.0);
glMatrixMode(5888LL);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
sprintf(s, "%4.1f seconds * %4.1f fps at %i x %i", TimeCounter, FPS, wa[2], wa[3]);
glRasterPos2i(10LL, 10LL);
v0 = strnlen(s, 0x100uLL);
glCallLists(v0, 5121LL, s);
strcpy(s, "GLX with GTK test program");
glRasterPos2i(10LL, wa[3] - 32);
v1 = strlen(s);
glCallLists(v1, 5121LL, s);
v2 = gdk_x11_drawable_get_xid(*(_QWORD *)(window + 80LL));
return glXSwapBuffers(disp, v2);
}

original_code:
void expose(void)
{
  float aspect_ratio;
  char info_string[256];
  aspect_ratio = (float)(wa.width) / (float)(wa.height);
  glMatrixMode(0x1701);
  glLoadIdentity();
  glOrtho(-2.50*aspect_ratio, 2.50*aspect_ratio, -2.50, 2.50, 1., 100.);
  glMatrixMode(0x1700);
  glLoadIdentity();
  gluLookAt(10., 0., 0., 0., 0., 0., 0., 0., 1.);
  glMultMatrixf(rotation_matrix);
  glClear(0x00004000 | 0x00000100);
  drawXORCube(1.0);
  glMatrixMode(0x1701);
  glLoadIdentity();
  glOrtho(0, (float)wa.width, 0, (float)wa.height, -1., 1.);
  glMatrixMode(0x1700);
  glLoadIdentity();
  glColor3f(1.0, 1.0, 1.0);
  sprintf(info_string, "%4.1f seconds * %4.1f fps at %i x %i", TimeCounter, FPS, wa.width, wa.height);
  glRasterPos2i(10, 10);
  glCallLists(strnlen(info_string,256), 0x1401, info_string);
  sprintf(info_string, "GLX with GTK test program");
  glRasterPos2i(10, wa.height-32);
  glCallLists(strlen(info_string), 0x1401, info_string);
  glXSwapBuffers(disp, (gdk_x11_drawable_get_xid (window->window)));
}

canonical_original_code:
void expose()
{
  float aspect_ratio;
  char info_string[256];
  aspect_ratio = (float)(wa.width) / (float)(wa.height);
  glMatrixMode(0x1701);
  glLoadIdentity();
  glOrtho(-2.50*aspect_ratio, 2.50*aspect_ratio, -2.50, 2.50, 1., 100.);
  glMatrixMode(0x1700);
  glLoadIdentity();
  gluLookAt(10., 0., 0., 0., 0., 0., 0., 0., 1.);
  glMultMatrixf(rotation_matrix);
  glClear(0x00004000 | 0x00000100);
  drawXORCube(1.0);
  glMatrixMode(0x1701);
  glLoadIdentity();
  glOrtho(0, (float)wa.width, 0, (float)wa.height, -1., 1.);
  glMatrixMode(0x1700);
  glLoadIdentity();
  glColor3f(1.0, 1.0, 1.0);
  sprintf(info_string, "%4.1f seconds * %4.1f fps at %i x %i", TimeCounter, FPS, wa.width, wa.height);
  glRasterPos2i(10, 10);
  glCallLists(strnlen(info_string,256), 0x1401, info_string);
  sprintf(info_string, "GLX with GTK test program");
  glRasterPos2i(10, wa.height-32);
  glCallLists(strlen(info_string), 0x1401, info_string);
  glXSwapBuffers(disp, (gdk_x11_drawable_get_xid (window->window)));
}