decompiled_code:
int print_stats()
{
__int64 v0; // rbp
int v1; // ebx
double v2; // rbp
double v3; // rbx

puts("\nFinal averages\n");
if ( *((int *)&sys + 20) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
printf(
"%s %12.8lf +/- %12.8lf\n",
*(const char **)(*((_QWORD *)&sys + 60) + v0),
*(double *)(*((_QWORD *)&sys + 60) + v0 + 24),
*(double *)(*((_QWORD *)&sys + 60) + v0 + 40));
++v1;
v0 += 48LL;
}
while ( *((_DWORD *)&sys + 20) > v1 );
}
v2 = (double)*((int *)&sys + 12) / (double)*((int *)&sys + 14);
v3 = (double)*((int *)&sys + 11) / (double)*((int *)&sys + 13);
puts("\n\nSelection and acceptance stats\n");
puts("Particle Moves Accepted Percent");
puts("-------- --------- -------- -------");
printf(" Solvent %9d%10d%9.2lf\n", *((unsigned int *)&sys + 14), *((unsigned int *)&sys + 12), v2);
return printf(" Monomer %9d%10d%9.2lf\n", *((unsigned int *)&sys + 13), *((unsigned int *)&sys + 11), v3);
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rbp
int v1; // ebx
double v2; // rbp
double v3; // rbx

puts("\nFinal averages\n");
if ( *((int *)&sys + 20) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
printf(
"%s %12.8lf +/- %12.8lf\n",
*(const char **)(*((_QWORD *)&sys + 60) + v0),
*(double *)(*((_QWORD *)&sys + 60) + v0 + 24),
*(double *)(*((_QWORD *)&sys + 60) + v0 + 40));
++v1;
v0 += 48LL;
}
while ( *((_DWORD *)&sys + 20) > v1 );
}
v2 = (double)*((int *)&sys + 12) / (double)*((int *)&sys + 14);
v3 = (double)*((int *)&sys + 11) / (double)*((int *)&sys + 13);
puts("\n\nSelection and acceptance stats\n");
puts("Particle Moves Accepted Percent");
puts("-------- --------- -------- -------");
printf(" Solvent %9d%10d%9.2lf\n", *((unsigned int *)&sys + 14), *((unsigned int *)&sys + 12), v2);
return printf(" Monomer %9d%10d%9.2lf\n", *((unsigned int *)&sys + 13), *((unsigned int *)&sys + 11), v3);
}

original_code:
void print_stats(void) {
  int i;
  double perc_dpd, perc_mon;
  printf("\nFinal averages\n\n");
  for (i = 0; i < sys.n_stats; i++) {
    printf("%s %12.8lf +/- %12.8lf\n",
      sys.stats[i].name, sys.stats[i].sum, sys.stats[i].err);
  }
  perc_dpd = (double) sys.n_accept_solvent / sys.n_attempt_solvent;
  perc_mon = (double) sys.n_accept_mon / sys.n_attempt_mon;
  printf("\n\nSelection and acceptance stats\n\n");
  printf("Particle    Moves    Accepted  Percent\n");
  printf("--------  ---------  --------  -------\n");
  printf(" Solvent  %9d%10d%9.2lf\n",
    sys.n_attempt_solvent, sys.n_accept_solvent, perc_dpd);
  printf(" Monomer  %9d%10d%9.2lf\n",
    sys.n_attempt_mon, sys.n_accept_mon, perc_mon);
}

canonical_original_code:
void print_stats() {
  int i;
  double perc_dpd; double perc_mon;
  printf("\nFinal averages\n\n");
  for (i = 0; i < sys.n_stats; i++) {
    printf("%s %12.8lf +/- %12.8lf\n",
      sys.stats[i].name, sys.stats[i].sum, sys.stats[i].err);
  }
  perc_dpd = (double) sys.n_accept_solvent / sys.n_attempt_solvent;
  perc_mon = (double) sys.n_accept_mon / sys.n_attempt_mon;
  printf("\n\nSelection and acceptance stats\n\n");
  printf("Particle    Moves    Accepted  Percent\n");
  printf("--------  ---------  --------  -------\n");
  printf(" Solvent  %9d%10d%9.2lf\n",
    sys.n_attempt_solvent, sys.n_accept_solvent, perc_dpd);
  printf(" Monomer  %9d%10d%9.2lf\n",
    sys.n_attempt_mon, sys.n_accept_mon, perc_mon);
}


vmag

decompiled_code:
double __fastcall vmag(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v9; // xmm0_8
double result; // xmm0_8

v9 = a10 * a10 + a9 * a9 + a11 * a11;
if ( v9 < 0.0 )
result = sqrt(v9);
else
result = sqrt(v9);
return result;
}

canonical_decompiled_code:
double __fastcall func1(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v9; // xmm0_8
double result; // xmm0_8

v9 = a10 * a10 + a9 * a9 + a11 * a11;
if ( v9 < 0.0 )
result = sqrt(v9);
else
result = sqrt(v9);
return result;
}

original_code:
double vmag(Vector v) {
  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

canonical_original_code:
double vmag(struct vector_type v) {
  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

variable_types:
v
struct vector_type { double x; }

return_type: double


mod

decompiled_code:
__int64 __fastcall mod(int a1, int a2)
{
int v2; // edx
unsigned int v3; // esi
__int64 result; // rax

v2 = a1 % a2;
v3 = a1 % a2 + a2;
result = (unsigned int)v2;
if ( v2 < 0 )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
int v2; // edx
unsigned int v3; // esi
__int64 result; // rax

v2 = a1 % a2;
v3 = a1 % a2 + a2;
result = (unsigned int)v2;
if ( v2 < 0 )
result = v3;
return result;
}

original_code:
int mod(int a, int b) {
  int c = a % b;
  if (c < 0) {
    c += b;
  }
  return c;
}

variable_types: a int

variable_types: b int

variable_types: c int

return_type: int


original_code:
double ran3()
{
  static int ma[60],mj,mk,mz,i,ii,k,inext,inextp,iff,mbig,mseed;
  if (iff ==0) {
    iff=1;
    mbig = 1000000000;
      mseed = sys.iseed;
    mz =0;
    mj = mseed ;
    mj = mj % mbig;
    ma[55] = mj;
    mk = 1;
    for (i=1;i<55;i++){
      ii = (21*i) % 55;
      ma[ii] = mk;
      mk = mj - mk;
      if (mk<mz) mk=mk+mbig;
      mj = ma[ii];
    }
    for(k=1;k<=4;k++){
      for(i=1;i<=55;i++){
 ma[i] = ma[i] - ma[1 + ((i+30)%55)];
 if (ma[i]<mz) ma[i] = ma[i] + mbig;
      }
    }
    inext=0;
    inextp=31;
   }
  if (++inext == 56) inext =1;
  if (++inextp ==56) inextp =1;
  mj = ma[inext] - ma[inextp];
  if (mj<mz) mj=mj+mbig;
  ma[inext]=mj;
  sys.iseed = mj;
  return (double)mj / mbig;
}

canonical_original_code:
double ran3()
{
  static int ma[60]; static int mj; static int mk; static int mz; static int i; static int ii; static int k; static int inext; static int inextp; static int iff; static int mbig; static int mseed;
  if (iff ==0) {
    iff=1;
    mbig = 1000000000;
      mseed = sys.iseed;
    mz =0;
    mj = mseed ;
    mj = mj % mbig;
    ma[55] = mj;
    mk = 1;
    for (i=1;i<55;i++){
      ii = (21*i) % 55;
      ma[ii] = mk;
      mk = mj - mk;
      if (mk<mz) mk=mk+mbig;
      mj = ma[ii];
    }
    for(k=1;k<=4;k++){
      for(i=1;i<=55;i++){
 ma[i] = ma[i] - ma[1 + ((i+30)%55)];
 if (ma[i]<mz) ma[i] = ma[i] + mbig;
      }
    }
    inext=0;
    inextp=31;
   }
  if (++inext == 56) inext =1;
  if (++inextp ==56) inextp =1;
  mj = ma[inext] - ma[inextp];
  if (mj<mz) mj=mj+mbig;
  ma[inext]=mj;
  sys.iseed = mj;
  return (double)mj / mbig;
}

variable_types:
ma
int [60]

variable_types: mj int

variable_types: mk int

variable_types: mz int

variable_types: i int

variable_types: ii int

variable_types: k int

variable_types: inext int

variable_types: inextp int

variable_types: iff int

variable_types: mbig int

variable_types: mseed int


readrand

repo: tdz/performance

decompiled_code:
char *__fastcall readrand(size_t nbytes)
{
size_t v1; // rbp
char *v2; // r13
int v3; // eax
int v4; // ebx
char *v5; // r12
ssize_t v6; // rax

v1 = nbytes;
v2 = (char *)malloc(nbytes);
if ( !v2 )
{
perror("malloc");
return v2;
}
while ( 1 )
{
v3 = open("./random.data", 0);
v4 = v3;
if ( v3 != -1 )
break;
if ( *__errno_location() != 4 )
goto LABEL_4;
}
if ( v3 < 0 )
{
LABEL_4:
perror("open");
LABEL_19:
free(v2);
return 0LL;
}
v5 = v2;
if ( nbytes )
{
do
{
while ( 1 )
{
v6 = read(v4, v5, v1);
if ( v6 == -1 )
break;
if ( v6 < 0 )
goto LABEL_16;
if ( v6 )
{
v5 += v6;
v1 -= v6;
if ( v1 )
continue;
}
goto LABEL_8;
}
}
while ( *__errno_location() == 4 );
LABEL_16:
perror("read");
while ( close(v4) == -1 && *__errno_location() == 4 )
;
goto LABEL_19;
}
LABEL_8:
while ( close(v4) == -1 && *__errno_location() == 4 )
;
return v2;
}

canonical_decompiled_code:
char *__fastcall func7(size_t nbytes)
{
size_t v1; // rbp
char *v2; // r13
int v3; // eax
int v4; // ebx
char *v5; // r12
ssize_t v6; // rax

v1 = nbytes;
v2 = (char *)malloc(nbytes);
if ( !v2 )
{
perror("malloc");
return v2;
}
while ( 1 )
{
v3 = open("./random.data", 0);
v4 = v3;
if ( v3 != -1 )
break;
if ( *__errno_location() != 4 )
goto LABEL_4;
}
if ( v3 < 0 )
{
LABEL_4:
perror("open");
LABEL_19:
free(v2);
return 0LL;
}
v5 = v2;
if ( nbytes )
{
do
{
while ( 1 )
{
v6 = read(v4, v5, v1);
if ( v6 == -1 )
break;
if ( v6 < 0 )
goto LABEL_16;
if ( v6 )
{
v5 += v6;
v1 -= v6;
if ( v1 )
continue;
}
goto LABEL_8;
}
}
while ( *__errno_location() == 4 );
LABEL_16:
perror("read");
while ( close(v4) == -1 && *__errno_location() == 4 )
;
goto LABEL_19;
}
LABEL_8:
while ( close(v4) == -1 && *__errno_location() == 4 )
;
return v2;
}

original_code:
void *
readrand(size_t siz)
{
  void *buf = malloc(siz);
  if (!buf) {
    perror("malloc");
    goto err_malloc;
  }
  int fd = (__extension__ ({ long int __result; do __result = (long int) (open("./random.data", 00)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  if (fd < 0) {
    perror("open");
    goto err_open;
  }
  char *pos = buf;
  while (siz) {
    ssize_t res = (__extension__ ({ long int __result; do __result = (long int) (read(fd, pos, siz)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
    if (res < 0) {
      perror("read");
      goto err_read;
    } else if (res == 0) {
      break;
    }
    siz -= res;
    pos += res;
  }
  (__extension__ ({ long int __result; do __result = (long int) (close(fd)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  return buf;
err_read:
  (__extension__ ({ long int __result; do __result = (long int) (close(fd)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
err_open:
  free(buf);
err_malloc:
  return ((void *)0);
}

canonical_original_code:
void *readrand(unsigned long siz)
{
  void *buf = malloc(siz);
  if (!buf) {
    perror("malloc");
    goto err_malloc;
  }
  int fd = (__extension__ ({ long int __result; do __result = (long int) (open("./random.data", 00)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  if (fd < 0) {
    perror("open");
    goto err_open;
  }
  char *pos = buf;
  while (siz) {
    long res = (__extension__ ({ long int __result; do __result = (long int) (read(fd, pos, siz)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
    if (res < 0) {
      perror("read");
      goto err_read;
    } else if (res == 0) {
      break;
    }
    siz -= res;
    pos += res;
  }
  (__extension__ ({ long __result; do __result = (long) (close(fd)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
  return buf;
err_read:
  (__extension__ ({ long __result; do __result = (long) (close(fd)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
err_open:
  free(buf);
err_malloc:
  return ((void *)0);
}

variable_types:
siz
unsigned long

variable_types:
buf
void *

variable_types: fd int

variable_types:
pos
char *

variable_types: res long

return_type: void *


sort

original_code:
long long
sort(int *data, size_t len)
{
  long long nsteps = 0;
  struct range *s = malloc(sizeof(*s));
  if (!s) {
    perror("malloc");
    goto err_malloc;
  }
  size_t siz = 1;
  size_t top = 0;
  s[top].off = 0;
  s[top].len = len;
  ++top;
  long long iter = 0;
  while (top) {
    --top;
    size_t off = s[top].off;
    size_t len = s[top].len;
    size_t pivot = off + len / 2;
    size_t i;
    for (i = off; i < pivot; ++i) {
      if (data[i] < data[pivot])
        swap(data+i, data+pivot);
      ++nsteps;
    }
    size_t end = off+len;
    for (i = pivot+1; i < end; ++i) {
      if (data[pivot] < data[i])
        swap(data+pivot, data+i);
      ++nsteps;
    }
    if ((top+2) > siz) {
      siz *= 2;
      void *mem = realloc(s, siz*sizeof(*s));
      if (!mem) {
        perror("realloc");
        goto err_realloc;
      }
      s = mem;
    }
    if (pivot > off) {
      s[top].off = off;
      s[top].len = pivot-1-off;
      ++top;
    }
    if (pivot < end) {
      s[top].off = pivot+1;
      s[top].len = end-s[top].off;
      ++top;
    }
  }
  free(s);
  return nsteps;
err_realloc:
  free(s);
err_malloc:
  return -1;
}

canonical_original_code:
long long sort(int *data, unsigned long len)
{
  long long nsteps = 0;
  struct range *s = malloc(sizeof(*s));
  if (!s) {
    perror("malloc");
    goto err_malloc;
  }
  unsigned long siz = 1;
  unsigned long top = 0;
  s[top].off = 0;
  s[top].len = len;
  ++top;
  long long iter = 0;
  while (top) {
    --top;
    unsigned long off = s[top].off;
    unsigned long len = s[top].len;
    unsigned long pivot = off + len / 2;
    unsigned long i;
    for (i = off; i < pivot; ++i) {
      if (data[i] < data[pivot])
        swap(data+i, data+pivot);
      ++nsteps;
    }
    unsigned long end = off+len;
    for (i = pivot+1; i < end; ++i) {
      if (data[pivot] < data[i])
        swap(data+pivot, data+i);
      ++nsteps;
    }
    if ((top+2) > siz) {
      siz *= 2;
      void *mem = realloc(s, siz*sizeof(*s));
      if (!mem) {
        perror("realloc");
        goto err_realloc;
      }
      s = mem;
    }
    if (pivot > off) {
      s[top].off = off;
      s[top].len = pivot-1-off;
      ++top;
    }
    if (pivot < end) {
      s[top].off = pivot+1;
      s[top].len = end-s[top].off;
      ++top;
    }
  }
  free(s);
  return nsteps;
err_realloc:
  free(s);
err_malloc:
  return -1;
}

variable_types:
data
int *

variable_types:
len
unsigned long

variable_types:
nsteps
long long

variable_types: struct range { unsigned long off; unsigned long len; } *

variable_types:
top
unsigned long

variable_types:
iter
long long

variable_types:
off
unsigned long

variable_types:
pivot
unsigned long

variable_types:
i
unsigned long

variable_types:
end
unsigned long

variable_types:
mem
void *

return_type: long long


readrand

canonical_decompiled_code:
char *__fastcall func8(size_t nbytes)
{
size_t v1; // rbp
char *v2; // r13
int v3; // eax
int v4; // ebx
char *v5; // r12
ssize_t v6; // rax

v1 = nbytes;
v2 = (char *)malloc(nbytes);
if ( !v2 )
{
perror("malloc");
return v2;
}
while ( 1 )
{
v3 = open("./random.data", 0);
v4 = v3;
if ( v3 != -1 )
break;
if ( *__errno_location() != 4 )
goto LABEL_4;
}
if ( v3 < 0 )
{
LABEL_4:
perror("open");
LABEL_19:
free(v2);
return 0LL;
}
v5 = v2;
if ( nbytes )
{
do
{
while ( 1 )
{
v6 = read(v4, v5, v1);
if ( v6 == -1 )
break;
if ( v6 < 0 )
goto LABEL_16;
if ( v6 )
{
v5 += v6;
v1 -= v6;
if ( v1 )
continue;
}
goto LABEL_8;
}
}
while ( *__errno_location() == 4 );
LABEL_16:
perror("read");
while ( close(v4) == -1 && *__errno_location() == 4 )
;
goto LABEL_19;
}
LABEL_8:
while ( close(v4) == -1 && *__errno_location() == 4 )
;
return v2;
}


compare

repo: te-bachi/mpc-sort

decompiled_code:
__int64 __fastcall compare(const void *a1, const void *a2)
{
return (unsigned int)(int)(*(double *)a1 - *(double *)a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)(int)(*(double *)a1 - *(double *)a2);
}

original_code:
int compare(const void *a, const void *b) {
    return ( *(sort_t*)a - *(sort_t*)b );
}

canonical_original_code:
int compare(void *a, void *b) {
    return ( *(double *)a - *(double *)b );
}

variable_types:
a
void *

variable_types:
b
void *


sortThread

decompiled_code:
void *__fastcall sortThread(void *a1)
{
qsort(*(void **)a1, *((int *)a1 + 3), 8uLL, (__compar_fn_t)compare);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
qsort(*(void **)a1, *((int *)a1 + 3), 8uLL, (__compar_fn_t)compare);
return 0LL;
}

original_code:
void *sortThread(void *arg) {
    chunk_t *chunk = (chunk_t *)(arg);
    qsort(chunk->data, chunk->len, sizeof(sort_t), compare);
    return ((void *)0);
}

canonical_original_code:
void *sortThread(void *arg) {
    struct chunk_t *chunk = (chunk_t *)(arg);
    qsort(chunk->data, chunk->len, sizeof(sort_t), compare);
    return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct chunk_t { double *data; int idx; int len; } *


getNumCpus

decompiled_code:
__int64 getNumCpus()
{
return readInfo(1);
}

canonical_decompiled_code:
__int64 func3()
{
return func2(1);
}

original_code:
int getNumCpus(void) {
    return readInfo(1);
}

canonical_original_code:
int getNumCpus() {
    return readInfo(1);
}


getNumThreads

decompiled_code:
__int64 getNumThreads()
{
return readInfo(2);
}

canonical_decompiled_code:
__int64 func4()
{
return func2(2);
}

original_code:
int getNumThreads(void) {
    return readInfo(2);
}

canonical_original_code:
int getNumThreads() {
    return readInfo(2);
}


getNumCores

decompiled_code:
__int64 getNumCores()
{
return readInfo(3);
}

canonical_decompiled_code:
__int64 func5()
{
return func2(3);
}

original_code:
int getNumCores(void) {
    return readInfo(3);
}

canonical_original_code:
int getNumCores() {
    return readInfo(3);
}


resetIndexes

decompiled_code:
void __fastcall resetIndexes(__int64 a1, int a2)
{
_DWORD *v2; // rax

if ( a2 > 0 )
{
v2 = (_DWORD *)(a1 + 8);
do
{
*v2 = 0;
v2 += 4;
}
while ( v2 != (_DWORD *)(a1 + 16LL * (unsigned int)(a2 - 1) + 24) );
}
}

original_code:
void resetIndexes(chunk_t *chunks, int nThreads) {
    int i;
    for (i = 0; i < nThreads; i++) {
        chunks[i].idx = 0;
    }
}

canonical_original_code:
void resetIndexes(struct chunk_t *chunks, int nThreads) {
    int i;
    for (i = 0; i < nThreads; i++) {
        chunks[i].idx = 0;
    }
}

variable_types: nThreads int

variable_types: i int

return_type: void

user_defined_types: struct chunk_t { double *data; int idx; int len; }


getLeast

original_code:
sort_t *getLeast(chunk_t *chunks, int nThreads) {
    chunk_t *chunkOne;
    chunk_t *chunkTwo;
    sort_t *dataOne;
    sort_t *dataTwo;
    int start;
    int i;
    chunkOne = ((void *)0);
    for (i = 0; i < nThreads; i++) {
        if (chunks[i].idx < chunks[i].len) {
            chunkOne = &chunks[i];
            dataOne = &chunkOne->data[chunkOne->idx];
            start = i + 1;
            break;
        }
    }
    for (i = start; i < nThreads; i++) {
        if (chunks[i].idx < chunks[i].len) {
            chunkTwo = &chunks[i];
            dataTwo = &chunkTwo->data[chunkTwo->idx];
            if (*dataTwo < *dataOne) {
                chunkOne = chunkTwo;
                dataOne = &chunkOne->data[chunkOne->idx];
            }
        }
    }
    if (chunkOne != ((void *)0)) {
        chunkOne->idx++;
    }
    return dataOne;
}

canonical_original_code:
double *getLeast(struct chunk_t *chunks, int nThreads) {
    struct chunk_t *chunkOne;
    struct chunk_t *chunkTwo;
    double *dataOne;
    double *dataTwo;
    int start;
    int i;
    chunkOne = ((void *)0);
    for (i = 0; i < nThreads; i++) {
        if (chunks[i].idx < chunks[i].len) {
            chunkOne = &chunks[i];
            dataOne = &chunkOne->data[chunkOne->idx];
            start = i + 1;
            break;
        }
    }
    for (i = start; i < nThreads; i++) {
        if (chunks[i].idx < chunks[i].len) {
            chunkTwo = &chunks[i];
            dataTwo = &chunkTwo->data[chunkTwo->idx];
            if (*dataTwo < *dataOne) {
                chunkOne = chunkTwo;
                dataOne = &chunkOne->data[chunkOne->idx];
            }
        }
    }
    if (chunkOne != ((void *)0)) {
        chunkOne->idx++;
    }
    return dataOne;
}

variable_types:
dataOne
double *

variable_types:
dataTwo
double *

variable_types: start int

return_type: double *


parSort

original_code:
void parSort(sort_t *data, int len, int nThreads, int thresh) {
    pthread_t th[nThreads];
    chunk_t chunks[nThreads];
    int chunkSize;
    size_t allocBytes;
    long start;
    long i;
    if(nThreads < 1) {
        nThreads = getNumCpus();
    }
    chunkSize = len / nThreads;
    start = 0;
    for (i = 0; i < nThreads; i++) {
        if(i + 1 == nThreads) {
            chunks[i].len = len - ((nThreads - 1) * chunkSize);
        } else {
            chunks[i].len = chunkSize;
        }
        allocBytes = chunks[i].len * sizeof(sort_t);
        chunks[i].data = (sort_t *) malloc(allocBytes);
        memcpy(chunks[i].data, &data[start], allocBytes);
        printf("%li: len=%i\n", i, chunks[i].len);
        pthread_create(&th[i], ((void *)0), sortThread, (void *)&chunks[i]);
        start += chunkSize;
    }
    for (i = 0; i < nThreads; i++) {
        pthread_join(th[i], ((void *)0));
    }
    resetIndexes(chunks, nThreads);
    for (i = 0; i < len; i++) {
        data[i] = *getLeast(chunks, nThreads);
    }
    for (i = 0; i < nThreads; i++) {
        free(chunks[i].data);
    }
}

canonical_original_code:
void parSort(double *data, int len, int nThreads, int thresh) {
    unsigned long th[-1];
    struct chunk_t chunks[-1];
    int chunkSize;
    unsigned long allocBytes;
    long start;
    long i;
    if(nThreads < 1) {
        nThreads = getNumCpus();
    }
    chunkSize = len / nThreads;
    start = 0;
    for (i = 0; i < nThreads; i++) {
        if(i + 1 == nThreads) {
            chunks[i].len = len - ((nThreads - 1) * chunkSize);
        } else {
            chunks[i].len = chunkSize;
        }
        allocBytes = chunks[i].len * sizeof(sort_t);
        chunks[i].data = (double *) malloc(allocBytes);
        memcpy(chunks[i].data, &data[start], allocBytes);
        printf("%li: len=%i\n", i, chunks[i].len);
        pthread_create(&th[i], ((void *)0), sortThread, (void *)&chunks[i]);
        start += chunkSize;
    }
    for (i = 0; i < nThreads; i++) {
        pthread_join(th[i], ((void *)0));
    }
    resetIndexes(chunks, nThreads);
    for (i = 0; i < len; i++) {
        data[i] = *getLeast(chunks, nThreads);
    }
    for (i = 0; i < nThreads; i++) {
        free(chunks[i].data);
    }
}

variable_types:
data
double *

variable_types: len int

variable_types: thresh int

variable_types:
th
unsigned long [-1]

variable_types: struct chunk_t { double *data; int idx; int len; } [-1]

variable_types: chunkSize int

variable_types:
allocBytes
unsigned long

variable_types: start long

variable_types: i long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
double v5; // r14
double v6; // r15
unsigned __int64 v7; // rbp
__int64 i; // rbx
struct timespec v10; // [rsp+0h] [rbp-88h] BYREF
struct timespec v11; // [rsp+10h] [rbp-78h] BYREF
clockid_t clock_id; // [rsp+20h] [rbp-68h]
struct timespec tp; // [rsp+30h] [rbp-58h] BYREF
struct timespec v14; // [rsp+40h] [rbp-48h] BYREF
clockid_t v15; // [rsp+50h] [rbp-38h]

v3 = 0;
if ( argc > 1 )
v3 = atoi(argv[1]);
v4 = 16;
if ( v3 <= 16 )
v4 = v3;
v15 = 2;
clock_id = 1;
clock_gettime(2, &tp);
clock_gettime(1, &v10);
parSort(dummyDoubleSortData, &unk_1E8480, (unsigned int)v4, 0LL);
clock_gettime(v15, &v14);
clock_gettime(clock_id, &v11);
v5 = ((double)SLODWORD(v14.tv_nsec) - (double)SLODWORD(tp.tv_nsec)) / 1000000000.0
+ (double)SLODWORD(v14.tv_sec)
- (double)SLODWORD(tp.tv_sec);
v6 = ((double)SLODWORD(v11.tv_nsec) - (double)SLODWORD(v10.tv_nsec)) / 1000000000.0
+ (double)SLODWORD(v11.tv_sec)
- (double)SLODWORD(v10.tv_sec);
puts("after:");
v7 = 1LL;
for ( i = 0LL; ; ++i )
{
printf("%12.4f ", dummyDoubleSortData[i]);
if ( (_DWORD)i )
{
if ( 10 * ((int)i / 10) == (_DWORD)i )
putchar(10);
if ( v7 > (unsigned __int64)&unk_1E847F )
break;
}
++v7;
}
putchar(10);
printf("Elapsed: %2.2f, process %2.2f, average per CPU %2.2f)\n", v6, v5, v5 / (double)v4);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 ctimer_t cp;
 ctimer_t ce;
 int nThreads;
 sort_t *sort = dummyDoubleSortData;
 size_t sortLen = sizeof(dummyDoubleSortData) / sizeof(sort_t);
 nThreads = 0;
 if (argc > 1) {
     nThreads= atoi(argv[1]);
 }
 nThreads = (nThreads > 16) ? 16 : nThreads;
 cp.clockID = PROCESS;
 ce.clockID = MONOTONIC;
 clock_gettime(cp.clockID, &(cp.sT));
 clock_gettime(ce.clockID, &(ce.sT));
 parSort(sort, sortLen, nThreads, 0);
    clock_gettime(cp.clockID, &(cp.eT));
    clock_gettime(ce.clockID, &(ce.eT));
    double ptime = ((double)(cp.eT.tv_sec) - (double)(cp.sT.tv_sec)) + ((double)cp.eT.tv_nsec - (double)cp.sT.tv_nsec)/1e9;
    double etime = ((double)(ce.eT.tv_sec) - (double)(ce.sT.tv_sec)) + ((double)ce.eT.tv_nsec - (double)ce.sT.tv_nsec)/1e9;
    printf("after:\n");
    for (int i = 0; i < sortLen; i++) {
        printf("%12.4f ", sort[i]);
        if (i != 0 && i % 10 == 0) {
            printf("\n");
        }
    }
    printf("\n");
    printf("Elapsed: %2.2f, process %2.2f, " "average per CPU %2.2f)\n",
                    etime, ptime, ptime/((double)nThreads));
}

canonical_original_code:
int main(int argc, char *argv[]) {
 struct ctimer_t cp;
 struct ctimer_t ce;
 int nThreads;
 double *sort = dummyDoubleSortData;
 unsigned long sortLen = sizeof(dummyDoubleSortData) / sizeof(sort_t);
 nThreads = 0;
 if (argc > 1) {
     nThreads= atoi(argv[1]);
 }
 nThreads = (nThreads > 16) ? 16 : nThreads;
 cp.clockID = PROCESS;
 ce.clockID = MONOTONIC;
 clock_gettime(cp.clockID, &(cp.sT));
 clock_gettime(ce.clockID, &(ce.sT));
 parSort(sort, sortLen, nThreads, 0);
    clock_gettime(cp.clockID, &(cp.eT));
    clock_gettime(ce.clockID, &(ce.eT));
    double ptime = ((double)(cp.eT.tv_sec) - (double)(cp.sT.tv_sec)) + ((double)cp.eT.tv_nsec - (double)cp.sT.tv_nsec)/1e9;
    double etime = ((double)(ce.eT.tv_sec) - (double)(ce.sT.tv_sec)) + ((double)ce.eT.tv_nsec - (double)ce.sT.tv_nsec)/1e9;
    printf("after:\n");
    for (int i = 0; i < sortLen; i++) {
        printf("%12.4f ", sort[i]);
        if (i != 0 && i % 10 == 0) {
            printf("\n");
        }
    }
    printf("\n");
    printf("Elapsed: %2.2f, process %2.2f, " "average per CPU %2.2f)\n",
                    etime, ptime, ptime/((double)nThreads));
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct ctimer_t { struct timespec { long tv_sec; long tv_nsec; } sT; struct timespec eT; int clockID; }

variable_types:
sort
double *

variable_types:
sortLen
unsigned long

variable_types: ptime double

variable_types: etime double

user_defined_types: struct ctimer_t { struct timespec sT; struct timespec eT; int clockID; }


sigchldhandler

decompiled_code:
void sigchldhandler()
{
while ( waitpid(-1, 0LL, 1) > 0 )
;
}

canonical_decompiled_code:
void func1()
{
while ( waitpid(-1, 0LL, 1) > 0 )
;
}

original_code:
static void sigchldhandler(int sig)
{
 while(waitpid(-1, ((void *)0), 1) > 0);
}

variable_types: sig int


decompiled_code:
int __fastcall setup_alarms(unsigned int a1, unsigned int a2, unsigned int a3)
{
struct itimerspec v5; // [rsp+0h] [rbp-D8h] BYREF
struct itimerspec value; // [rsp+20h] [rbp-B8h] BYREF
struct sigevent v7; // [rsp+40h] [rbp-98h] BYREF
struct sigevent evp; // [rsp+80h] [rbp-58h] BYREF

evp.sigev_notify = 0;
evp.sigev_signo = 14;
evp.sigev_value.sival_ptr = &load_timer;
timer_create(0, &evp, &load_timer);
value.it_value.tv_sec = a1 / 0x3E8;
value.it_value.tv_nsec = 1000000 * (a1 % 0x3E8);
value.it_interval.tv_sec = a2 / 0x3E8 + a1 / 0x3E8;
value.it_interval.tv_nsec = 1000000 * (a1 % 0x3E8 + a2 % 0x3E8);
timer_settime(load_timer, 0, &value, 0LL);
v7.sigev_notify = 0;
v7.sigev_signo = 14;
v7.sigev_value.sival_ptr = &deadline_timer;
timer_create(0, &v7, &deadline_timer);
v5.it_value.tv_sec = a3 / 0x3E8;
v5.it_value.tv_nsec = 1000000 * (a3 % 0x3E8);
v5.it_interval.tv_sec = 0LL;
return timer_settime(deadline_timer, 0, &v5, 0LL);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, unsigned int a2, unsigned int a3)
{
struct itimerspec v5; // [rsp+0h] [rbp-D8h] BYREF
struct itimerspec value; // [rsp+20h] [rbp-B8h] BYREF
struct sigevent v7; // [rsp+40h] [rbp-98h] BYREF
struct sigevent evp; // [rsp+80h] [rbp-58h] BYREF

evp.sigev_notify = 0;
evp.sigev_signo = 14;
evp.sigev_value.sival_ptr = &load_timer;
timer_create(0, &evp, &load_timer);
value.it_value.tv_sec = a1 / 0x3E8;
value.it_value.tv_nsec = 1000000 * (a1 % 0x3E8);
value.it_interval.tv_sec = a2 / 0x3E8 + a1 / 0x3E8;
value.it_interval.tv_nsec = 1000000 * (a1 % 0x3E8 + a2 % 0x3E8);
timer_settime(load_timer, 0, &value, 0LL);
v7.sigev_notify = 0;
v7.sigev_signo = 14;
v7.sigev_value.sival_ptr = &deadline_timer;
timer_create(0, &v7, &deadline_timer);
v5.it_value.tv_sec = a3 / 0x3E8;
v5.it_value.tv_nsec = 1000000 * (a3 % 0x3E8);
v5.it_interval.tv_sec = 0LL;
return timer_settime(deadline_timer, 0, &v5, 0LL);
}

original_code:
static void setup_alarms(unsigned int onperiod, unsigned int offperiod, unsigned int deadline)
{
 struct sigevent sigev_load, sigev_deadline;
 struct itimerspec its_load, its_deadline;
 sigev_load.sigev_notify = SIGEV_SIGNAL;
 sigev_load.sigev_signo = 14;
 sigev_load.sigev_value.sival_ptr = &load_timer;
 timer_create(0, &sigev_load, &load_timer);
 its_load.it_value.tv_sec = onperiod / 1000;
 its_load.it_value.tv_nsec = (onperiod % 1000) * 1000000;
 its_load.it_interval.tv_sec = onperiod / 1000 + offperiod / 1000;
 its_load.it_interval.tv_nsec = (onperiod % 1000 + offperiod % 1000) * 1000000;
 timer_settime(load_timer, 0, &its_load, ((void *)0));
 sigev_deadline.sigev_notify = SIGEV_SIGNAL;
 sigev_deadline.sigev_signo = 14;
 sigev_deadline.sigev_value.sival_ptr = &deadline_timer;
 timer_create(0, &sigev_deadline, &deadline_timer);
 its_deadline.it_value.tv_sec = deadline / 1000;
 its_deadline.it_value.tv_nsec = (deadline % 1000) * 1000000;
 its_deadline.it_interval.tv_sec = 0;
 its_deadline.it_interval.tv_sec = 0;
 timer_settime(deadline_timer, 0, &its_deadline, ((void *)0));
}

canonical_original_code:
static void setup_alarms(unsigned int onperiod, unsigned int offperiod, unsigned int deadline)
{
 struct sigevent sigev_load; struct sigevent sigev_deadline;
 struct itimerspec its_load; struct itimerspec its_deadline;
 sigev_load.sigev_notify = SIGEV_SIGNAL;
 sigev_load.sigev_signo = 14;
 sigev_load.sigev_value.sival_ptr = &load_timer;
 timer_create(0, &sigev_load, &load_timer);
 its_load.it_value.tv_sec = onperiod / 1000;
 its_load.it_value.tv_nsec = (onperiod % 1000) * 1000000;
 its_load.it_interval.tv_sec = onperiod / 1000 + offperiod / 1000;
 its_load.it_interval.tv_nsec = (onperiod % 1000 + offperiod % 1000) * 1000000;
 timer_settime(load_timer, 0, &its_load, ((void *)0));
 sigev_deadline.sigev_notify = SIGEV_SIGNAL;
 sigev_deadline.sigev_signo = 14;
 sigev_deadline.sigev_value.sival_ptr = &deadline_timer;
 timer_create(0, &sigev_deadline, &deadline_timer);
 its_deadline.it_value.tv_sec = deadline / 1000;
 its_deadline.it_value.tv_nsec = (deadline % 1000) * 1000000;
 its_deadline.it_interval.tv_sec = 0;
 its_deadline.it_interval.tv_sec = 0;
 timer_settime(deadline_timer, 0, &its_deadline, ((void *)0));
}

variable_types:
onperiod
unsigned int

variable_types:
offperiod
unsigned int

variable_types:
deadline
unsigned int

variable_types: struct sigevent { union sigval { int sival_int; void *sival_ptr; } sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t { char __size[56]; long __align; } *_attribute; } _sigev_thread; } _sigev_un; }

variable_types: struct itimerspec { struct timespec { long tv_sec; long tv_nsec; } it_interval; struct timespec it_value; }

user_defined_types: struct sigevent { union sigval sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t *_attribute; } _sigev_thread; } _sigev_un; }

user_defined_types: struct itimerspec { struct timespec it_interval; struct timespec it_value; }


load

decompiled_code:
void __noreturn load()
{
double v0; // xmm0_8

while ( 1 )
{
do
v0 = (double)rand();
while ( v0 >= 0.0 );
sqrt(v0);
}
}

canonical_decompiled_code:
void __noreturn func3()
{
double v0; // xmm0_8

while ( 1 )
{
do
v0 = (double)rand();
while ( v0 >= 0.0 );
sqrt(v0);
}
}

original_code:
static void load()
{
 while (1)
  sqrt(rand());
}


bindcpu

decompiled_code:
int __fastcall bindcpu(__pid_t pid, int a2)
{
cpu_set_t v3; // [rsp+0h] [rbp-88h] BYREF

memset(&v3, 0, sizeof(v3));
if ( (unsigned __int64)a2 <= 0x3FF )
v3.__bits[(unsigned __int64)a2 >> 6] |= 1LL << a2;
return sched_setaffinity(pid, 0x80uLL, &v3);
}

canonical_decompiled_code:
int __fastcall func4(__pid_t pid, int a2)
{
cpu_set_t v3; // [rsp+0h] [rbp-88h] BYREF

memset(&v3, 0, sizeof(v3));
if ( (unsigned __int64)a2 <= 0x3FF )
v3.__bits[(unsigned __int64)a2 >> 6] |= 1LL << a2;
return sched_setaffinity(pid, 0x80uLL, &v3);
}

original_code:
static void bindcpu(pid_t pid, int cpu)
{
 cpu_set_t cpuset;
 do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(pid, sizeof(cpuset), &cpuset);
}

canonical_original_code:
static void bindcpu(int pid, int cpu)
{
 struct cpu_set_t cpuset;
 do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(pid, sizeof(cpuset), &cpuset);
}

variable_types: pid int

variable_types: cpu int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
__pid_t __fastcall create_workers(int a1, int a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
int v5; // ebx
__pid_t result; // eax
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8

if ( a1 <= 0 )
return 0;
v5 = a2;
while ( 1 )
{
result = fork();
if ( result == -1 )
break;
if ( !result )
{
prctl(1, 1LL);
setup_alarms(a3, a4, a5);
load(a3, a4, v10, v11, v12);
}
bindcpu(result, v5);
v5 += 2;
if ( v5 == a2 + 2 * a1 )
return 0;
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func5(int a1, int a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
int v5; // ebx
__pid_t result; // eax
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8

if ( a1 <= 0 )
return 0;
v5 = a2;
while ( 1 )
{
result = fork();
if ( result == -1 )
break;
if ( !result )
{
prctl(1, 1LL);
func2(a3, a4, a5);
func3(a3, a4, v10, v11, v12);
}
func4(result, v5);
v5 += 2;
if ( v5 == a2 + 2 * a1 )
return 0;
}
return result;
}

original_code:
static int create_workers(int workers, int startcpu, unsigned int onperiod, unsigned int offperiod, unsigned int deadline)
{
 int pid;
 for(; workers > 0; workers--) {
  pid = fork();
  if (pid == -1) {
   return -1;
  }
  if (pid == 0) {
   prctl(1, 1);
   setup_alarms(onperiod, offperiod, deadline);
   load();
  }
  else {
   bindcpu(pid, startcpu);
   startcpu += 2;
  }
 }
 return 0;
}

variable_types: workers int

variable_types: startcpu int


decompiled_code:
__int64 __fastcall synthload_start(unsigned int a1, unsigned int a2, unsigned int a3, int a4, int a5)
{
__pid_t v8; // eax
unsigned int v9; // er15
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
sigset_t v14; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v15; // [rsp+80h] [rbp-D8h] BYREF

offperiod = a2;
v8 = fork();
v9 = v8;
if ( v8 != -1 )
{
if ( v8 )
{
bindcpu(v8, a5 + 1);
}
else
{
signal(15, 0LL);
signal(2, 0LL);
signal(1, 0LL);
signal(20, 0LL);
signal(17, (__sighandler_t)sigchldhandler);
prctl(1, 1LL);
sigemptyset(&v14);
sigaddset(&v14, 15);
sigaddset(&v14, 1);
sigprocmask(1, &v14, 0LL);
v15.sa_flags = 4;
v15.sa_handler = (__sighandler_t)sigalrmhandler;
sigemptyset(&v15.sa_mask);
sigaction(14, &v15, 0LL);
if ( !create_workers(a4 - 1, a5 + 3, a1, a2, a3) )
{
setup_alarms(a1, a2, a3);
load(a1, a2, v10, v11, v12);
}
v9 = -2;
}
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2, unsigned int a3, int a4, int a5)
{
__pid_t v8; // eax
unsigned int v9; // er15
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
sigset_t v14; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v15; // [rsp+80h] [rbp-D8h] BYREF

offperiod = a2;
v8 = fork();
v9 = v8;
if ( v8 != -1 )
{
if ( v8 )
{
func4(v8, a5 + 1);
}
else
{
signal(15, 0LL);
signal(2, 0LL);
signal(1, 0LL);
signal(20, 0LL);
signal(17, (__sighandler_t)sigchldhandler);
prctl(1, 1LL);
sigemptyset(&v14);
sigaddset(&v14, 15);
sigaddset(&v14, 1);
sigprocmask(1, &v14, 0LL);
v15.sa_flags = 4;
v15.sa_handler = (__sighandler_t)sigalrmhandler;
sigemptyset(&v15.sa_mask);
sigaction(14, &v15, 0LL);
if ( !func5(a4 - 1, a5 + 3, a1, a2, a3) )
{
func2(a1, a2, a3);
func3(a1, a2, v10, v11, v12);
}
v9 = -2;
}
}
return v9;
}

original_code:
pid_t synthload_start(unsigned int onperiod, unsigned int offperiod, unsigned int deadline, int workers, int startcpu)
{
 int mpid;
 struct sigaction sa;
 _offperiod = offperiod;
 mpid = fork();
 if (mpid == -1) {
  return -1;
 }
 if (mpid == 0) {
  sigset_t unblock;
  signal(15, ((__sighandler_t) 0));
  signal(2, ((__sighandler_t) 0));
  signal(1, ((__sighandler_t) 0));
  signal(20, ((__sighandler_t) 0));
  signal(17, sigchldhandler);
  prctl(1, 1);
  sigemptyset(&unblock);
  sigaddset(&unblock, 15);
  sigaddset(&unblock, 1);
  sigprocmask(1, &unblock, ((void *)0));
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = sigalrmhandler;
  sigemptyset(&sa.sa_mask);
  sigaction(14, &sa, ((void *)0));
  if (create_workers(workers-1, startcpu+3, onperiod, offperiod, deadline) != 0)
   return -2;
  setup_alarms(onperiod, offperiod, deadline);
  load();
 } else {
  bindcpu(mpid, startcpu+1);
 }
 return mpid;
}

canonical_original_code:
int synthload_start(unsigned int onperiod, unsigned int offperiod, unsigned int deadline, int workers, int startcpu)
{
 int mpid;
 struct sigaction sa;
 _offperiod = offperiod;
 mpid = fork();
 if (mpid == -1) {
  return -1;
 }
 if (mpid == 0) {
  struct __sigset_t unblock;
  signal(15, ((void (*)(int)) 0));
  signal(2, ((void (*)(int)) 0));
  signal(1, ((void (*)(int)) 0));
  signal(20, ((void (*)(int)) 0));
  signal(17, sigchldhandler);
  prctl(1, 1);
  sigemptyset(&unblock);
  sigaddset(&unblock, 15);
  sigaddset(&unblock, 1);
  sigprocmask(1, &unblock, ((void *)0));
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = sigalrmhandler;
  sigemptyset(&sa.sa_mask);
  sigaction(14, &sa, ((void *)0));
  if (create_workers(workers-1, startcpu+3, onperiod, offperiod, deadline) != 0)
   return -2;
  setup_alarms(onperiod, offperiod, deadline);
  load();
 } else {
  bindcpu(mpid, startcpu+1);
 }
 return mpid;
}

variable_types: mpid int

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

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

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


sigalrmhandler

decompiled_code:
int __fastcall sigalrmhandler(__int64 a1, __int64 a2)
{
if ( *(timer_t **)(a2 + 24) == &deadline_timer )
exit(0);
return usleep(1000 * offperiod);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
if ( *(timer_t **)(a2 + 24) == &deadline_timer )
exit(0);
return usleep(1000 * offperiod);
}

original_code:
static void sigalrmhandler(int sig, siginfo_t *si, void *uc)
{
 if (si->_sifields._rt.si_sigval.sival_ptr == &deadline_timer) {
  _exit(0);
 }
 usleep(_offperiod * 1000);
}

canonical_original_code:
static void sigalrmhandler(int sig, struct siginfo_t *si, void *uc)
{
 if (si->_sifields._rt.si_sigval.sival_ptr == &deadline_timer) {
  _exit(0);
 }
 usleep(_offperiod * 1000);
}

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

variable_types:
uc
void *


repo: teamosceola/base-station

decompiled_code:
__int64 Init_Draw_Compass()
{
__int64 v0; // rbx
int i; // ebp
__int64 result; // rax

v0 = 0LL;
for ( i = 0; i != 361; ++i )
{
rotozoomSurfaceSize(500LL, 500LL, &v_delta_x[v0], &v_delta_y[v0], (double)i, 1.0);
v_delta_x[v0] = (int)((double)((v_delta_x[v0] - 500) / 2) + 78.0);
result = (unsigned int)(int)((double)((v_delta_y[v0] - 500) / 2) + 78.0);
v_delta_y[v0++] = result;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx
int i; // ebp
__int64 result; // rax

v0 = 0LL;
for ( i = 0; i != 361; ++i )
{
rotozoomSurfaceSize(500LL, 500LL, &v_delta_x[v0], &v_delta_y[v0], (double)i, 1.0);
v_delta_x[v0] = (int)((double)((v_delta_x[v0] - 500) / 2) + 78.0);
result = (unsigned int)(int)((double)((v_delta_y[v0] - 500) / 2) + 78.0);
v_delta_y[v0++] = result;
}
return result;
}

variable_types: i int


main

repo: technetia/project-tdm

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // ax

init_registers(argc, argv, envp);
init_memory();
puts(">> Begin 6502 emulation");
v3 = get_full_SP();
printf("SP: 0x%X\n", v3);
puts("Execute instruction DEX");
instr_dex();
printf("X: %d\n", (unsigned int)reg_X);
printf("A: %d\n", (unsigned int)reg_A);
puts("Execute instruction TXA");
instr_txa();
printf("X: %d\n", (unsigned int)reg_X);
printf("A: %d\n", (unsigned int)reg_A);
puts("Execute instruction LDA #$42");
instr_lda(42LL, 2LL);
printf("X: %d\n", (unsigned int)reg_X);
printf("A: %d\n", (unsigned int)reg_A);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
    init_registers();
    init_memory();
    printf(">> Begin 6502 emulation\n");
    printf("SP: 0x%X\n", get_full_SP());
    printf("Execute instruction DEX\n");
    instr_dex();
    printf("X: %d\n", reg_X.data);
    printf("A: %d\n", reg_A.data);
    printf("Execute instruction TXA\n");
    instr_txa();
    printf("X: %d\n", reg_X.data);
    printf("A: %d\n", reg_A.data);
    printf("Execute instruction LDA #$42\n");
    instr_lda(42, IMMEDIATE);
    printf("X: %d\n", reg_X.data);
    printf("A: %d\n", reg_A.data);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


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

result = (unsigned __int8)reg_SP;
reg_X = reg_SP;
if ( reg_SP )
{
if ( (reg_SP & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned __int8)reg_SP;
reg_X = reg_SP;
if ( reg_SP )
{
if ( (reg_SP & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_tsx(void) {
    reg_X.data = (signed char)reg_SP.data;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_tsx() {
    reg_X.data = (char)reg_SP.data;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned __int8)reg_X;
reg_SP = reg_X;
return result;
}

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

result = (unsigned __int8)reg_X;
reg_SP = reg_X;
return result;
}

original_code:
void instr_txs(void) {
    reg_SP.data = (unsigned char)reg_X.data;
}

canonical_original_code:
void instr_txs() {
    reg_SP.data = (unsigned char)reg_X.data;
}


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

result = (unsigned __int16)get_full_SP();
*((_BYTE *)&MEMORY_BLOCK + (unsigned __int16)result) = reg_A;
--reg_SP;
return result;
}

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

result = (unsigned __int16)get_full_SP();
*((_BYTE *)&MEMORY_BLOCK + (unsigned __int16)result) = reg_A;
--reg_SP;
return result;
}

original_code:
void instr_pha(void) {
    unsigned short stack_index = get_full_SP();
    MEMORY_BLOCK[stack_index] = reg_A.data;
    reg_SP.data--;
}

canonical_original_code:
void instr_pha() {
    unsigned short stack_index = get_full_SP();
    MEMORY_BLOCK[stack_index] = reg_A.data;
    reg_SP.data--;
}


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

result = (unsigned __int16)get_full_SP();
*((_BYTE *)&MEMORY_BLOCK + (unsigned __int16)result) = reg_PSR;
--reg_SP;
return result;
}

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

result = (unsigned __int16)get_full_SP();
*((_BYTE *)&MEMORY_BLOCK + (unsigned __int16)result) = reg_PSR;
--reg_SP;
return result;
}

original_code:
void instr_php(void) {
    unsigned short stack_index = get_full_SP();
    MEMORY_BLOCK[stack_index] = reg_PSR.data.reg;
    reg_SP.data--;
}

canonical_original_code:
void instr_php() {
    unsigned short stack_index = get_full_SP();
    MEMORY_BLOCK[stack_index] = reg_PSR.data.reg;
    reg_SP.data--;
}


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

++reg_SP;
result = *((unsigned __int8 *)&MEMORY_BLOCK + (unsigned __int16)get_full_SP());
reg_A = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

++reg_SP;
result = *((unsigned __int8 *)&MEMORY_BLOCK + (unsigned __int16)get_full_SP());
reg_A = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_pla(void) {
    unsigned short stack_index;
    reg_SP.data++;
    stack_index = get_full_SP();
    reg_A.data = MEMORY_BLOCK[stack_index];
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_pla() {
    unsigned short stack_index;
    reg_SP.data++;
    stack_index = get_full_SP();
    reg_A.data = MEMORY_BLOCK[stack_index];
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

++reg_SP;
result = *((unsigned __int8 *)&MEMORY_BLOCK + (unsigned __int16)get_full_SP());
reg_PSR = result;
return result;
}

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

++reg_SP;
result = *((unsigned __int8 *)&MEMORY_BLOCK + (unsigned __int16)get_full_SP());
reg_PSR = result;
return result;
}

original_code:
void instr_plp(void) {
    unsigned short stack_index;
    reg_SP.data++;
    stack_index = get_full_SP();
    reg_PSR.data.reg = MEMORY_BLOCK[stack_index];
}

canonical_original_code:
void instr_plp() {
    unsigned short stack_index;
    reg_SP.data++;
    stack_index = get_full_SP();
    reg_PSR.data.reg = MEMORY_BLOCK[stack_index];
}


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

result = (unsigned __int8)reg_A;
reg_X = reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned __int8)reg_A;
reg_X = reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_tax(void) {
    reg_X.data = reg_A.data;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_tax() {
    reg_X.data = reg_A.data;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned __int8)reg_A;
reg_Y = reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned __int8)reg_A;
reg_Y = reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_tay(void) {
    reg_Y.data = reg_A.data;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_tay() {
    reg_Y.data = reg_A.data;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned __int8)reg_X;
reg_A = reg_X;
if ( reg_X )
{
if ( (reg_X & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned __int8)reg_X;
reg_A = reg_X;
if ( reg_X )
{
if ( (reg_X & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_txa(void) {
    reg_A.data = reg_X.data;
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_txa() {
    reg_A.data = reg_X.data;
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned __int8)reg_Y;
reg_A = reg_Y;
if ( reg_Y )
{
if ( (reg_Y & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned __int8)reg_Y;
reg_A = reg_Y;
if ( reg_Y )
{
if ( (reg_Y & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_tya(void) {
    reg_A.data = reg_Y.data;
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_tya() {
    reg_A.data = reg_Y.data;
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned int)(unsigned __int8)reg_X + 1;
reg_X = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned int)(unsigned __int8)reg_X + 1;
reg_X = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_inx(void) {
    reg_X.data++;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_inx() {
    reg_X.data++;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned int)(unsigned __int8)reg_Y + 1;
reg_Y = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned int)(unsigned __int8)reg_Y + 1;
reg_Y = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_iny(void) {
    reg_Y.data++;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_iny() {
    reg_Y.data++;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned int)(unsigned __int8)reg_X - 1;
reg_X = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned int)(unsigned __int8)reg_X - 1;
reg_X = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_dex(void) {
    reg_X.data--;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_dex() {
    reg_X.data--;
    if (reg_X.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_X.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


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

result = (unsigned int)(unsigned __int8)reg_Y - 1;
reg_Y = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

result = (unsigned int)(unsigned __int8)reg_Y - 1;
reg_Y = result;
if ( (_BYTE)result )
{
if ( (result & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_dey(void) {
    reg_Y.data--;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_dey() {
    reg_Y.data--;
    if (reg_Y.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_Y.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}


decompiled_code:
void init_registers()
{
reg_A = 0;
reg_X = 0;
reg_Y = 0;
reg_PSR = 32;
reg_PC = 0;
reg_SP = -1;
*(&reg_SP + 1) = 1;
}

canonical_decompiled_code:
void func0()
{
reg_A = 0;
reg_X = 0;
reg_Y = 0;
reg_PSR = 32;
reg_PC = 0;
reg_SP = -1;
*(&reg_SP + 1) = 1;
}

original_code:
void init_registers(void) {
    reg_A.data = 0x00;
    reg_X.data = 0x00;
    reg_Y.data = 0x00;
    reg_PSR.data.reg = 0x00;
    reg_PSR.data.flags.X = 1;
    reg_PC.data = 0x00;
    reg_SP.data = 0xFF;
    reg_SP.page_index = 0x01;
}

canonical_original_code:
void init_registers() {
    reg_A.data = 0x00;
    reg_X.data = 0x00;
    reg_Y.data = 0x00;
    reg_PSR.data.reg = 0x00;
    reg_PSR.data.flags.X = 1;
    reg_PC.data = 0x00;
    reg_SP.data = 0xFF;
    reg_SP.page_index = 0x01;
}


decompiled_code:
__int64 get_full_SP()
{
return (unsigned __int8)reg_SP + ((unsigned __int8)*(&reg_SP + 1) << 8);
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned __int8)reg_SP + ((unsigned __int8)*(&reg_SP + 1) << 8);
}

original_code:
unsigned short get_full_SP(void) {
    return (reg_SP.page_index << 8) + reg_SP.data;
}

canonical_original_code:
unsigned short get_full_SP() {
    return (reg_SP.page_index << 8) + reg_SP.data;
}

return_type: unsigned short


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

switch ( a2 )
{
case 2:
reg_A = a1;
break;
case 3:
case 7:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1);
break;
case 8:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1 + (unsigned __int8)reg_X);
break;
case 9:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1 + (unsigned __int8)reg_Y);
break;
default:
break;
}
result = (unsigned __int8)reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

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

switch ( a2 )
{
case 2:
reg_A = a1;
break;
case 3:
case 7:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1);
break;
case 8:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1 + (unsigned __int8)reg_X);
break;
case 9:
reg_A = *((_BYTE *)&MEMORY_BLOCK + a1 + (unsigned __int8)reg_Y);
break;
default:
break;
}
result = (unsigned __int8)reg_A;
if ( reg_A )
{
if ( (reg_A & 0x10) != 0 )
reg_PSR |= 0x80u;
}
else
{
reg_PSR |= 2u;
}
return result;
}

original_code:
void instr_lda(long value, ADDR_MODE mode) {
    switch (mode) {
        case IMMEDIATE:
            reg_A.data = (signed char)value;
            break;
        case ZERO:
        case ABS:
            reg_A.data = (signed char)MEMORY_BLOCK[value];
            break;
        case ABS_X:
            reg_A.data = (signed char)MEMORY_BLOCK[value + (unsigned char)reg_X.data];
            break;
        case ABS_Y:
            reg_A.data = (signed char)MEMORY_BLOCK[value + (unsigned char)reg_Y.data];
            break;
        case ZERO_X:
            break;
        case ZERO_Y:
            break;
        case IDX_INDIRECT:
            break;
        case INDIRECT_IDX:
            break;
        default:
            break;
    }
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

canonical_original_code:
void instr_lda(long value, enum ADDR_MODE mode) {
    switch (mode) {
        case IMMEDIATE:
            reg_A.data = (char)value;
            break;
        case ZERO:
        case ABS:
            reg_A.data = (char)MEMORY_BLOCK[value];
            break;
        case ABS_X:
            reg_A.data = (char)MEMORY_BLOCK[value + (unsigned char)reg_X.data];
            break;
        case ABS_Y:
            reg_A.data = (char)MEMORY_BLOCK[value + (unsigned char)reg_Y.data];
            break;
        case ZERO_X:
            break;
        case ZERO_Y:
            break;
        case IDX_INDIRECT:
            break;
        case INDIRECT_IDX:
            break;
        default:
            break;
    }
    if (reg_A.data == 0x00) {
        reg_PSR.data.flags.Z = 1;
    }
    if (reg_A.data & 0x10) {
        reg_PSR.data.flags.N = 1;
    }
}

variable_types: value long

variable_types: enum ADDR_MODE { IMPLICIT=0, ACCUM=1, IMMEDIATE=2, ZERO=3, ZERO_X=4, ZERO_Y=5, RELATIVE=6, ABS=7, ABS_X=8, ABS_Y=9, INDIRECT=10, IDX_INDIRECT=11, INDIRECT_IDX=12 }


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

if ( (a2 & 0xFFFFFFFB) == 3 )
{
result = (unsigned __int8)reg_A;
*((_BYTE *)&MEMORY_BLOCK + a1) = reg_A;
}
return result;
}

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

if ( (a2 & 0xFFFFFFFB) == 3 )
{
result = (unsigned __int8)reg_A;
*((_BYTE *)&MEMORY_BLOCK + a1) = reg_A;
}
return result;
}

original_code:
void instr_sta(long value, ADDR_MODE mode) {
    switch (mode) {
        case ZERO:
        case ABS:
            MEMORY_BLOCK[value] = (char)reg_A.data;
            break;
        default:
            break;
    }
}

canonical_original_code:
void instr_sta(long value, enum ADDR_MODE mode) {
    switch (mode) {
        case ZERO:
        case ABS:
            MEMORY_BLOCK[value] = (char)reg_A.data;
            break;
        default:
            break;
    }
}

user_defined_types: enum ADDR_MODE { IMPLICIT=0, ACCUM=1, IMMEDIATE=2, ZERO=3, ZERO_X=4, ZERO_Y=5, RELATIVE=6, ABS=7, ABS_X=8, ABS_Y=9, INDIRECT=10, IDX_INDIRECT=11, INDIRECT_IDX=12 }


decompiled_code:
void instr_clc()
{
reg_PSR &= 0xFEu;
}

canonical_decompiled_code:
void func0()
{
reg_PSR &= 0xFEu;
}

original_code:
void instr_clc() {
    reg_PSR.data.flags.C = 0;
}


decompiled_code:
void instr_sec()
{
reg_PSR |= 1u;
}

canonical_decompiled_code:
void func1()
{
reg_PSR |= 1u;
}

original_code:
void instr_sec() {
    reg_PSR.data.flags.C = 1;
}


decompiled_code:
void instr_cli()
{
reg_PSR &= 0xFBu;
}

canonical_decompiled_code:
void func2()
{
reg_PSR &= 0xFBu;
}

original_code:
void instr_cli() {
    reg_PSR.data.flags.I = 0;
}


decompiled_code:
void instr_sei()
{
reg_PSR |= 4u;
}

canonical_decompiled_code:
void func3()
{
reg_PSR |= 4u;
}

original_code:
void instr_sei() {
    reg_PSR.data.flags.I = 1;
}


decompiled_code:
void instr_clv()
{
reg_PSR &= 0xBFu;
}

canonical_decompiled_code:
void func4()
{
reg_PSR &= 0xBFu;
}

original_code:
void instr_clv() {
    reg_PSR.data.flags.V = 0;
}


decompiled_code:
void instr_cld()
{
reg_PSR &= 0xF7u;
}

canonical_decompiled_code:
void func5()
{
reg_PSR &= 0xF7u;
}

original_code:
void instr_cld() {
    reg_PSR.data.flags.D = 0;
}


decompiled_code:
void instr_sed()
{
reg_PSR |= 8u;
}

canonical_decompiled_code:
void func6()
{
reg_PSR |= 8u;
}

original_code:
void instr_sed() {
    reg_PSR.data.flags.D = 1;
}


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

result = &MEMORY_BLOCK;
do
*result++ = 0;
while ( result != (_BYTE *)(unsigned int)((_DWORD)&MEMORY_BLOCK + 0x10000) );
return result;
}

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

result = &MEMORY_BLOCK;
do
*result++ = 0;
while ( result != (_BYTE *)(unsigned int)((_DWORD)&MEMORY_BLOCK + 0x10000) );
return result;
}

original_code:
void init_memory(void) {
    int i;
    for (i = 0; i < 65536; i++) {
        MEMORY_BLOCK[i] = '\0';
    }
}

canonical_original_code:
void init_memory() {
    int i;
    for (i = 0; i < 65536; i++) {
        MEMORY_BLOCK[i] = '\0';
    }
}


repo: tedajax/vaders

decompiled_code:
__int64 __fastcall collider_init(__int64 a1, unsigned int a2, unsigned int a3, int a4, unsigned int a5, __m128i a6)
{
__int64 result; // rax
__int64 v9; // rdx

result = rect_make(
(unsigned int)(int)COERCE_FLOAT(_mm_cvtsi128_si32(a6)),
(unsigned int)(int)*(float *)&a6.m128i_i32[1],
a2,
a3);
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = v9;
*(_DWORD *)(a1 + 16) = a4;
*(_QWORD *)(a1 + 20) = a5;
*(_DWORD *)(a1 + 28) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3, int a4, unsigned int a5, __m128i a6)
{
__int64 result; // rax
__int64 v9; // rdx

result = rect_make(
(unsigned int)(int)COERCE_FLOAT(_mm_cvtsi128_si32(a6)),
(unsigned int)(int)*(float *)&a6.m128i_i32[1],
a2,
a3);
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = v9;
*(_DWORD *)(a1 + 16) = a4;
*(_QWORD *)(a1 + 20) = a5;
*(_DWORD *)(a1 + 28) = 1;
return result;
}

original_code:
void collider_init(collider_t *self, vec2 pos, int width, int height, int tag, int collflag)
{
 self->bounds = rect_make(pos.x, pos.y, width, height);
 self->tag = tag;
 self->collidesWith = collflag;
 self->colliding = 0;
 self->enabled = 1;
}

canonical_original_code:
void collider_init(struct collider_t *self, struct vec2 pos, int width, int height, int tag, int collflag)
{
 self->bounds = rect_make(pos.x, pos.y, width, height);
 self->tag = tag;
 self->collidesWith = collflag;
 self->colliding = 0;
 self->enabled = 1;
}

variable_types: struct collider_t { struct rect { int x; } bounds; int tag; int collidesWith; int colliding; int enabled; } *

variable_types:
pos
struct vec2 { float x; }

variable_types: width int

variable_types: height int

variable_types: tag int

variable_types: collflag int

user_defined_types: struct collider_t { struct rect bounds; int tag; int collidesWith; int colliding; int enabled; }


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

if ( !a1 || !a2 )
_assert_fail("self && other", "collider.c", 0x13u, "collider_check_collision");
result = (unsigned int)(*(_DWORD *)(a2 + 16) & *(_DWORD *)(a1 + 20));
if ( (*(_DWORD *)(a2 + 16) & *(_DWORD *)(a1 + 20)) > 0 && *(_DWORD *)(a1 + 28) && *(_DWORD *)(a2 + 28) )
{
result = intersect_rr(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), *(_QWORD *)a2, *(_QWORD *)(a2 + 8));
if ( (_DWORD)result )
*(_DWORD *)(a1 + 24) = 1;
}
return result;
}

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

if ( !a1 || !a2 )
_assert_fail("self && other", "collider.c", 0x13u, "collider_check_collision");
result = (unsigned int)(*(_DWORD *)(a2 + 16) & *(_DWORD *)(a1 + 20));
if ( (*(_DWORD *)(a2 + 16) & *(_DWORD *)(a1 + 20)) > 0 && *(_DWORD *)(a1 + 28) && *(_DWORD *)(a2 + 28) )
{
result = intersect_rr(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), *(_QWORD *)a2, *(_QWORD *)(a2 + 8));
if ( (_DWORD)result )
*(_DWORD *)(a1 + 24) = 1;
}
return result;
}

original_code:
void collider_check_collision(collider_t *self, collider_t *other)
{
 ((self && other) ? (void) (0) : __assert_fail ("self && other", "collider.c", 19, __extension__ __PRETTY_FUNCTION__));
 if ((self->collidesWith & other->tag) > 0) {
  if (self->enabled && other->enabled) {
   if (intersect_rr(self->bounds, other->bounds)) {
    self->colliding = 1;
   }
  }
 }
}

canonical_original_code:
void collider_check_collision(struct collider_t *self, struct collider_t *other)
{
 ((self && other) ? (void) (0) : __assert_fail ("self && other", "collider.c", 19, __extension__ __PRETTY_FUNCTION__));
 if ((self->collidesWith & other->tag) > 0) {
  if (self->enabled && other->enabled) {
   if (intersect_rr(self->bounds, other->bounds)) {
    self->colliding = 1;
   }
  }
 }
}

variable_types:
other
struct collider_t *


decompiled_code:
__int64 colliders_init()
{
__int64 result; // rax
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

colliders = (__int64)malloc(0x320uLL);
memset((void *)colliders, 0, 0x320uLL);
stack_init(&colliderFreeIndices, 100LL, 4LL);
v1[0] = 0;
LODWORD(result) = 0;
do
{
*(_QWORD *)(colliders + 8LL * (int)result) = 0LL;
stack_push(&colliderFreeIndices, v1);
result = (unsigned int)(v1[0] + 1);
v1[0] = result;
}
while ( (int)result <= 99 );
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

colliders = (__int64)malloc(0x320uLL);
memset((void *)colliders, 0, 0x320uLL);
stack_init(&colliderFreeIndices, 100LL, 4LL);
v1[0] = 0;
LODWORD(result) = 0;
do
{
*(_QWORD *)(colliders + 8LL * (int)result) = 0LL;
stack_push(&colliderFreeIndices, v1);
result = (unsigned int)(v1[0] + 1);
v1[0] = result;
}
while ( (int)result <= 99 );
return result;
}

original_code:
void colliders_init()
{
 colliders = (collider_t **)malloc(sizeof(collider_t *) * MAX_COLLIDERS);
 memset(colliders, 0, MAX_COLLIDERS * sizeof(collider_t *));
 stack_init(&colliderFreeIndices, MAX_COLLIDERS, sizeof(int), ((void *)0));
 for (int i = 0; i < MAX_COLLIDERS; ++i) {
  colliders[i] = ((void *)0);
  stack_push(&colliderFreeIndices, (void *)&i);
 }
}

canonical_original_code:
void colliders_init()
{
 colliders = (struct collider_t **)malloc(sizeof(struct collider_t *) * MAX_COLLIDERS);
 memset(colliders, 0, MAX_COLLIDERS * sizeof(struct collider_t *));
 stack_init(&colliderFreeIndices, MAX_COLLIDERS, sizeof(int), ((void *)0));
 for (int i = 0; i < MAX_COLLIDERS; ++i) {
  colliders[i] = ((void *)0);
  stack_push(&colliderFreeIndices, (void *)&i);
 }
}


decompiled_code:
__int64 colliders_next_index()
{
unsigned int v1; // eax
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = -1;
if ( (unsigned int)stack_size(&colliderFreeIndices) )
stack_pop(&colliderFreeIndices, v2);
if ( v2[0] == -1 )
{
v1 = stack_size(&colliderFreeIndices);
fprintf(stderr, "Error: %d\n", v1);
}
return v2[0];
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v1; // eax
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = -1;
if ( (unsigned int)stack_size(&colliderFreeIndices) )
stack_pop(&colliderFreeIndices, v2);
if ( v2[0] == -1 )
{
v1 = stack_size(&colliderFreeIndices);
fprintf(stderr, "Error: %d\n", v1);
}
return v2[0];
}

original_code:
int colliders_next_index()
{
 int result = -1;
 if (stack_size(&colliderFreeIndices) > 0) {
  stack_pop(&colliderFreeIndices, &result);
 }
 if (result == -1) {
  fprintf(stderr, "Error: %d\n", stack_size(&colliderFreeIndices));
 }
 return result;
}

variable_types: result int


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

if ( !a1 )
_assert_fail("self", "collider.c", 0x2Cu, "colliders_add");
LODWORD(result) = colliders_next_index();
if ( (int)result < 0 )
_assert_fail("index >= 0", "collider.c", 0x31u, "colliders_add");
result = (int)result;
*(_QWORD *)(colliders + 8LL * (int)result) = a1;
return result;
}

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

if ( !a1 )
_assert_fail("self", "collider.c", 0x2Cu, "colliders_add");
LODWORD(result) = func3();
if ( (int)result < 0 )
_assert_fail("index >= 0", "collider.c", 0x31u, "colliders_add");
result = (int)result;
*(_QWORD *)(colliders + 8LL * (int)result) = a1;
return result;
}

original_code:
void colliders_add(collider_t *self)
{
 ((self) ? (void) (0) : __assert_fail ("self", "collider.c", 44, __extension__ __PRETTY_FUNCTION__));
 int index = colliders_next_index();
 ((index >= 0) ? (void) (0) : __assert_fail ("index >= 0", "collider.c", 49, __extension__ __PRETTY_FUNCTION__));
 colliders[index] = self;
}

canonical_original_code:
void colliders_add(struct collider_t *self)
{
 ((self) ? (void) (0) : __assert_fail ("self", "collider.c", 44, __extension__ __PRETTY_FUNCTION__));
 int index = colliders_next_index();
 ((index >= 0) ? (void) (0) : __assert_fail ("index >= 0", "collider.c", 49, __extension__ __PRETTY_FUNCTION__));
 colliders[index] = self;
}

variable_types: index int


decompiled_code:
__int64 colliders_update()
{
__int64 i; // rbp
__int64 j; // rbx
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rsi

for ( i = 0LL; i != 100; ++i )
{
for ( j = 0LL; j != 100; ++j )
{
if ( (_DWORD)j != (_DWORD)i )
{
result = colliders;
v3 = *(_QWORD *)(colliders + 8 * i);
if ( v3 )
{
v4 = *(_QWORD *)(colliders + 8 * j);
if ( v4 )
result = collider_check_collision(v3, v4);
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 i; // rbp
__int64 j; // rbx
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rsi

for ( i = 0LL; i != 100; ++i )
{
for ( j = 0LL; j != 100; ++j )
{
if ( (_DWORD)j != (_DWORD)i )
{
result = colliders;
v3 = *(_QWORD *)(colliders + 8 * i);
if ( v3 )
{
v4 = *(_QWORD *)(colliders + 8 * j);
if ( v4 )
result = func1(v3, v4);
}
}
}
}
return result;
}

original_code:
void colliders_update()
{
 for (int i = 0; i < MAX_COLLIDERS; ++i) {
  for (int j = 0; j < MAX_COLLIDERS; ++j) {
   if (i != j) {
    if (colliders[i] && colliders[j]) {
     collider_check_collision(colliders[i], colliders[j]);
    }
   }
  }
 }
}

variable_types: j int


decompiled_code:
__int64 colliders_debug_draw()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = 0LL; i != 800; i += 8LL )
{
result = *(_QWORD *)(colliders + i);
if ( result )
result = rectangleRGBA(
globals[2],
(unsigned int)(__int16)*(_DWORD *)result,
(unsigned int)(__int16)*(_DWORD *)(result + 4),
(unsigned int)(__int16)(*(_WORD *)(result + 8) + *(_WORD *)result),
(unsigned int)(__int16)(*(_WORD *)(result + 12) + *(_WORD *)(result + 4)),
255LL,
0LL,
0LL,
255LL);
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = 0LL; i != 800; i += 8LL )
{
result = *(_QWORD *)(colliders + i);
if ( result )
result = rectangleRGBA(
globals[2],
(unsigned int)(__int16)*(_DWORD *)result,
(unsigned int)(__int16)*(_DWORD *)(result + 4),
(unsigned int)(__int16)(*(_WORD *)(result + 8) + *(_WORD *)result),
(unsigned int)(__int16)(*(_WORD *)(result + 12) + *(_WORD *)(result + 4)),
255LL,
0LL,
0LL,
255LL);
}
return result;
}

original_code:
void colliders_debug_draw()
{
 for (int i = 0; i < MAX_COLLIDERS; ++i) {
  if (colliders[i]) {
   int x1 = colliders[i]->bounds.x;
   int y1 = colliders[i]->bounds.y;
   int x2 = x1 + colliders[i]->bounds.w;
   int y2 = y1 + colliders[i]->bounds.h;
   rectangleRGBA(globals.screen, x1, y1, x2, y2, 255, 0, 0, 255);
  }
 }
}


decompiled_code:
double enemies_calc_move()
{
__m128i v0; // xmm0
unsigned int v1; // edx
__m128i v2; // xmm0
double result; // xmm0_8

v0 = 0LL;
*(double *)v0.m128i_i64 = v2make(0.0);
v1 = _mm_cvtsi128_si32(v0);
v2 = _mm_cvtsi32_si128(v0.m128i_u32[1]);
if ( enemiesMoveRight )
{
if ( (float)(*(float *)&enemiesRight + 24.0) <= (float)SLODWORD(globals[0]) )
{
v1 = LC3;
}
else
{
enemiesMoveRight = 0;
v2 = (__m128i)(unsigned int)LC3;
}
}
else if ( (float)(*(float *)&enemiesLeft - 24.0) < 0.0 )
{
enemiesMoveRight = 1;
v2 = (__m128i)(unsigned int)LC3;
}
else
{
v1 = LC4;
}
*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(v2) << 32) | v1;
return result;
}

canonical_decompiled_code:
double func2()
{
__m128i v0; // xmm0
unsigned int v1; // edx
__m128i v2; // xmm0
double result; // xmm0_8

v0 = 0LL;
*(double *)v0.m128i_i64 = v2make(0.0);
v1 = _mm_cvtsi128_si32(v0);
v2 = _mm_cvtsi32_si128(v0.m128i_u32[1]);
if ( enemiesMoveRight )
{
if ( (float)(*(float *)&enemiesRight + 24.0) <= (float)SLODWORD(globals[0]) )
{
v1 = LC3;
}
else
{
enemiesMoveRight = 0;
v2 = (__m128i)(unsigned int)LC3;
}
}
else if ( (float)(*(float *)&enemiesLeft - 24.0) < 0.0 )
{
enemiesMoveRight = 1;
v2 = (__m128i)(unsigned int)LC3;
}
else
{
v1 = LC4;
}
*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(v2) << 32) | v1;
return result;
}

original_code:
vec2 enemies_calc_move()
{
 vec2 result = v2make(0, 0);
 if (enemiesMoveRight) {
  if (enemiesRight + HORIZONTAL_MOVEMENT_PER_TICK > globals.window.width) {
   enemiesMoveRight = 0;
   result.y = VERTICAL_MOVEMENT_PER_TICK;
  } else {
   result.x = HORIZONTAL_MOVEMENT_PER_TICK;
  }
 } else {
  if (enemiesLeft - HORIZONTAL_MOVEMENT_PER_TICK < 0) {
   enemiesMoveRight = 1;
   result.y = VERTICAL_MOVEMENT_PER_TICK;
  } else {
   result.x = -HORIZONTAL_MOVEMENT_PER_TICK;
  }
 }
 return result;
}

canonical_original_code:
struct vec2 enemies_calc_move()
{
 struct vec2 result = v2make(0, 0);
 if (enemiesMoveRight) {
  if (enemiesRight + HORIZONTAL_MOVEMENT_PER_TICK > globals.window.width) {
   enemiesMoveRight = 0;
   result.y = VERTICAL_MOVEMENT_PER_TICK;
  } else {
   result.x = HORIZONTAL_MOVEMENT_PER_TICK;
  }
 } else {
  if (enemiesLeft - HORIZONTAL_MOVEMENT_PER_TICK < 0) {
   enemiesMoveRight = 1;
   result.y = VERTICAL_MOVEMENT_PER_TICK;
  } else {
   result.x = -HORIZONTAL_MOVEMENT_PER_TICK;
  }
 }
 return result;
}

variable_types:
result
struct vec2 { float x; }

return_type: struct vec2 { float x; }


decompiled_code:
float **__fastcall enemies_move(double a1)
{
__int64 i; // rbx
__int64 v2; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 && !*(_DWORD *)(v2 + 48) )
enemy_move(v2, a1);
}
return enemies_update_bounds();
}

canonical_decompiled_code:
float **__fastcall func4(double a1)
{
__int64 i; // rbx
__int64 v2; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 && !*(_DWORD *)(v2 + 48) )
func0(v2, a1);
}
return func3();
}

original_code:
void enemies_move(vec2 dist)
{
 for (int i = 0; i < MAX_ENEMIES; ++i) {
  if (enemies[i] && !enemies[i]->dead) {
   enemy_move(enemies[i], dist);
  }
 }
 enemies_update_bounds();
}

canonical_original_code:
void enemies_move(struct vec2 dist)
{
 for (int i = 0; i < MAX_ENEMIES; ++i) {
  if (enemies[i] && !enemies[i]->dead) {
   enemy_move(enemies[i], dist);
  }
 }
 enemies_update_bounds();
}

variable_types:
dist
struct vec2 { float x; }


decompiled_code:
char *enemies_update_move_delay()
{
char *result; // rax

enemiesAlive = 0;
result = (char *)enemies;
do
{
if ( !*(_DWORD *)(*(_QWORD *)result + 48LL) )
++enemiesAlive;
result += 8;
}
while ( result != (char *)enemies + 240 );
*(float *)&moveDelay = (float)((float)((float)enemiesAlive / 30.0) * 1.475) + 0.025;
return result;
}

canonical_decompiled_code:
char *func5()
{
char *result; // rax

enemiesAlive = 0;
result = (char *)enemies;
do
{
if ( !*(_DWORD *)(*(_QWORD *)result + 48LL) )
++enemiesAlive;
result += 8;
}
while ( result != (char *)enemies + 240 );
*(float *)&moveDelay = (float)((float)((float)enemiesAlive / 30.0) * 1.475) + 0.025;
return result;
}

original_code:
void enemies_update_move_delay()
{
 enemiesAlive = 0;
 for (int i = 0; i < MAX_ENEMIES; ++i) {
  if (!enemies[i]->dead) {
   enemiesAlive++;
  }
 }
 moveDelay = MIN_MOVE_DELAY + (MAX_MOVE_DELAY - MIN_MOVE_DELAY) * ((float)enemiesAlive / (float)MAX_ENEMIES);
}

variable_types: i int


decompiled_code:
void *enemies_update()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi
double v3; // xmm0_8

for ( i = 0LL; i != 240; i += 8LL )
{
result = enemies;
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 && !*(_DWORD *)(v2 + 48) )
result = enemy_update(v2);
}
*(float *)&moveDelayTimer = *(float *)&moveDelayTimer - globals[14];
if ( *(float *)&moveDelayTimer <= 0.0 )
{
moveDelayTimer = moveDelay;
v3 = enemies_calc_move();
result = enemies_move(v3);
}
return result;
}

canonical_decompiled_code:
void *func8()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi
double v3; // xmm0_8

for ( i = 0LL; i != 240; i += 8LL )
{
result = enemies;
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 && !*(_DWORD *)(v2 + 48) )
result = func7(v2);
}
*(float *)&moveDelayTimer = *(float *)&moveDelayTimer - globals[14];
if ( *(float *)&moveDelayTimer <= 0.0 )
{
moveDelayTimer = moveDelay;
v3 = func2();
result = func4(v3);
}
return result;
}

original_code:
void enemies_update()
{
 for (int i = 0; i < MAX_ENEMIES; ++i) {
  if (enemies[i] && !enemies[i]->dead) {
   enemy_update(enemies[i]);
  }
 }
 moveDelayTimer -= globals.time.deltaf;
 if (moveDelayTimer <= 0.0f) {
  moveDelayTimer = moveDelay;
  enemies_move(enemies_calc_move());
 }
}


decompiled_code:
void *enemies_draw()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
result = enemies;
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 )
{
if ( !*(_DWORD *)(v2 + 48) )
result = (void *)enemy_draw(v2);
}
}
return result;
}

canonical_decompiled_code:
void *func11()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
result = enemies;
v2 = *(_QWORD *)((char *)enemies + i);
if ( v2 )
{
if ( !*(_DWORD *)(v2 + 48) )
result = (void *)func1(v2);
}
}
return result;
}

original_code:
void enemies_draw()
{
 int i = 0;
 for (i = 0; i < MAX_ENEMIES; ++i) {
  if (enemies[i] && !enemies[i]->dead) {
   enemy_draw(enemies[i]);
  }
 }
}


decompiled_code:
void enemies_cleanup()
{
__int64 i; // rbx
void *v1; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
v1 = *(void **)((char *)enemies + i);
if ( v1 )
{
free(v1);
*(_QWORD *)((char *)enemies + i) = 0LL;
}
}
free(enemies);
}

canonical_decompiled_code:
void func12()
{
__int64 i; // rbx
void *v1; // rdi

for ( i = 0LL; i != 240; i += 8LL )
{
v1 = *(void **)((char *)enemies + i);
if ( v1 )
{
free(v1);
*(_QWORD *)((char *)enemies + i) = 0LL;
}
}
free(enemies);
}

original_code:
void enemies_cleanup()
{
 for (int i = 0; i < MAX_ENEMIES; ++i) {
  if (enemies[i]) {
   free(enemies[i]);
   enemies[i] = ((void *)0);
  }
 }
 free(enemies);
}


init

decompiled_code:
__int64 __fastcall init(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // edx
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax

v2 = SDL_Init(0xFFFFLL);
v3 = 0;
if ( v2 != -1 )
{
init_globals(0xFFFFLL, a2, 0LL);
SDL_SetVideoMode(globals, *((unsigned int *)&globals + 1), *((unsigned int *)&globals + 2), 1LL);
v4 = SDL_GetVideoSurface();
*((_QWORD *)&globals + 2) = v4;
v3 = 0;
if ( v4 )
{
SDL_WM_SetCaption("Vaders", 0LL, 0LL);
input_init();
init_image_assets();
v5 = load_image("assets/tank.png");
push_image_asset("tank", v5);
v6 = load_image("assets/bullet.png");
push_image_asset("bullet", v6);
v7 = load_image("assets/enemy.png");
push_image_asset("enemy1", v7);
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // edx
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax

v2 = SDL_Init(0xFFFFLL);
v3 = 0;
if ( v2 != -1 )
{
init_globals(0xFFFFLL, a2, 0LL);
SDL_SetVideoMode(globals, *((unsigned int *)&globals + 1), *((unsigned int *)&globals + 2), 1LL);
v4 = SDL_GetVideoSurface();
*((_QWORD *)&globals + 2) = v4;
v3 = 0;
if ( v4 )
{
SDL_WM_SetCaption("Vaders", 0LL, 0LL);
input_init();
init_image_assets();
v5 = load_image("assets/tank.png");
push_image_asset("tank", v5);
v6 = load_image("assets/bullet.png");
push_image_asset("bullet", v6);
v7 = load_image("assets/enemy.png");
push_image_asset("enemy1", v7);
v3 = 1;
}
}
return v3;
}

original_code:
int init()
{
 if (SDL_Init(0x0000FFFF) == -1)
  return 0;
 init_globals();
 SDL_SetVideoMode(globals.window.width,
      globals.window.height,
      globals.window.bpp,
      0x00000001);
 globals.screen = SDL_GetVideoSurface();
 if (globals.screen == ((void *)0)) {
  return 0;
 }
 SDL_WM_SetCaption("Vaders", ((void *)0));
 input_init();
 init_image_assets();
 push_image_asset("tank", load_image("assets/tank.png"));
 push_image_asset("bullet", load_image("assets/bullet.png"));
 push_image_asset("enemy1", load_image("assets/enemy.png"));
 return 1;
}


cleanup

decompiled_code:
__int64 cleanup()
{
input_cleanup();
SDL_FreeSurface(globals[2]);
free_image_assets();
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func1()
{
input_cleanup();
SDL_FreeSurface(globals[2]);
free_image_assets();
return SDL_Quit();
}

original_code:
void cleanup()
{
 input_cleanup();
 SDL_FreeSurface(globals.screen);
 free_image_assets();
 SDL_Quit();
}


update

decompiled_code:
__int64 update()
{
player_update(player);
enemies_update();
bullets_update();
return colliders_update();
}

canonical_decompiled_code:
__int64 func2()
{
player_update(player);
enemies_update();
bullets_update();
return colliders_update();
}

original_code:
void update()
{
 player_update(player);
 enemies_update();
 bullets_update();
 colliders_update();
}


draw

decompiled_code:
__int64 draw()
{
player_draw(player);
enemies_draw();
return bullets_draw();
}

canonical_decompiled_code:
__int64 func3()
{
player_draw(player);
enemies_draw();
return bullets_draw();
}

original_code:
void draw()
{
 player_draw(player);
 enemies_draw();
 bullets_draw();
}


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

oldKeystates = malloc(0x50CuLL);
result = SDL_GetKeyState(0LL);
newKeystates = result;
return result;
}

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

oldKeystates = malloc(0x50CuLL);
result = SDL_GetKeyState(0LL);
newKeystates = result;
return result;
}

original_code:
void input_init()
{
 oldKeystates = (Uint8 *)malloc(sizeof(SDLKey) * SDLK_LAST);
 newKeystates = SDL_GetKeyState(((void *)0));
}

canonical_original_code:
void input_init()
{
 oldKeystates = (unsigned char *)malloc(sizeof(SDLKey) * SDLK_LAST);
 newKeystates = SDL_GetKeyState(((void *)0));
}


decompiled_code:
void input_cleanup()
{
free(oldKeystates);
}

canonical_decompiled_code:
void func1()
{
free(oldKeystates);
}


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

for ( result = 0LL; result != 323; ++result )
*((_BYTE *)oldKeystates + result) = *(_BYTE *)(newKeystates + result);
return result;
}

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

for ( result = 0LL; result != 323; ++result )
*((_BYTE *)oldKeystates + result) = *(_BYTE *)(newKeystates + result);
return result;
}

original_code:
void input_update()
{
 int i;
 for (i = 0; i < SDLK_LAST; i++)
  oldKeystates[i] = newKeystates[i];
}


decompiled_code:
__int64 __fastcall input_get_key(unsigned int a1)
{
return *(unsigned __int8 *)(newKeystates + a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return *(unsigned __int8 *)(newKeystates + a1);
}

original_code:
int input_get_key(SDLKey key)
{
 return newKeystates[key];
}

canonical_original_code:
int input_get_key(enum SDLKey key)
{
 return newKeystates[key];
}

variable_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
_BOOL8 __fastcall input_get_key_down(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *(_BYTE *)(newKeystates + a1) )
result = *((_BYTE *)oldKeystates + a1) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *(_BYTE *)(newKeystates + a1) )
result = *((_BYTE *)oldKeystates + a1) == 0;
return result;
}

original_code:
int input_get_key_down(SDLKey key)
{
 return (newKeystates[key] && !oldKeystates[key]);
}

canonical_original_code:
int input_get_key_down(enum SDLKey key)
{
 return (newKeystates[key] && !oldKeystates[key]);
}

user_defined_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
_BOOL8 __fastcall input_get_key_up(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( !*(_BYTE *)(newKeystates + a1) )
result = *((_BYTE *)oldKeystates + a1) != 0;
return result;
}

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

result = 0LL;
if ( !*(_BYTE *)(newKeystates + a1) )
result = *((_BYTE *)oldKeystates + a1) != 0;
return result;
}

original_code:
int input_get_key_up(SDLKey key)
{
 return (!newKeystates[key] && oldKeystates[key]);
}

canonical_original_code:
int input_get_key_up(enum SDLKey key)
{
 return (!newKeystates[key] && oldKeystates[key]);
}


decompiled_code:
__int64 __fastcall bullet_remove(signed int a1)
{
__int64 result; // rax
__int64 v2; // rdi
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
if ( (unsigned int)a1 > 0x13 )
_assert_fail("index >= 0 && index < MAX_BULLETS", "bullet.c", 0x32u, "bullet_remove");
result = a1;
v2 = *((_QWORD *)bullets + a1);
if ( v2 )
{
colliders_remove(v2 + 16);
free(*((void **)bullets + v3[0]));
*((_QWORD *)bullets + v3[0]) = 0LL;
result = stack_push(&freeIndices, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(signed int a1)
{
__int64 result; // rax
__int64 v2; // rdi
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
if ( (unsigned int)a1 > 0x13 )
_assert_fail("index >= 0 && index < MAX_BULLETS", "bullet.c", 0x32u, "bullet_remove");
result = a1;
v2 = *((_QWORD *)bullets + a1);
if ( v2 )
{
colliders_remove(v2 + 16);
free(*((void **)bullets + v3[0]));
*((_QWORD *)bullets + v3[0]) = 0LL;
result = stack_push(&freeIndices, v3);
}
return result;
}

original_code:
void bullet_remove(int index)
{
 ((index >= 0 && index < MAX_BULLETS) ? (void) (0) : __assert_fail ("index >= 0 && index < MAX_BULLETS", "bullet.c", 50, __extension__ __PRETTY_FUNCTION__));
 if (bullets[index]) {
  colliders_remove(&bullets[index]->collider);
  free(bullets[index]);
  bullets[index] = ((void *)0);
  stack_push(&freeIndices, (void *)&index);
 }
}


decompiled_code:
__int64 bullet_next_index()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)stack_size(&freeIndices) )
stack_pop(&freeIndices, v1);
else
v1[0] = -1;
return v1[0];
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)stack_size(&freeIndices) )
stack_pop(&freeIndices, v1);
else
v1[0] = -1;
return v1[0];
}

original_code:
int bullet_next_index()
{
 int result;
 if (stack_size(&freeIndices) > 0) {
  stack_pop(&freeIndices, &result);
 } else {
  result = -1;
 }
 return result;
}


decompiled_code:
__int64 bullets_update()
{
__int64 i; // rbx
__int64 v1; // rdi
__int64 j; // rbx
__int64 result; // rax

for ( i = 0LL; i != 160; i += 8LL )
{
v1 = *(_QWORD *)((char *)bullets + i);
if ( v1 )
bullet_update(v1);
}
for ( j = 0LL; j != 20; ++j )
{
result = *((_QWORD *)bullets + j);
if ( result )
{
if ( *(_DWORD *)(result + 64) )
result = bullet_remove(j);
}
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 i; // rbx
__int64 v1; // rdi
__int64 j; // rbx
__int64 result; // rax

for ( i = 0LL; i != 160; i += 8LL )
{
v1 = *(_QWORD *)((char *)bullets + i);
if ( v1 )
func0(v1);
}
for ( j = 0LL; j != 20; ++j )
{
result = *((_QWORD *)bullets + j);
if ( result )
{
if ( *(_DWORD *)(result + 64) )
result = func2(j);
}
}
return result;
}

original_code:
void bullets_update()
{
 int i;
 for (i = 0; i < MAX_BULLETS; ++i) {
  if (bullets[i]) {
   bullet_update(bullets[i]);
  }
 }
 for (i = 0; i < MAX_BULLETS; ++i) {
  if (bullets[i] && bullets[i]->destroy) {
   bullet_remove(i);
  }
 }
}


decompiled_code:
void *bullets_draw()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi

for ( i = 0LL; i != 160; i += 8LL )
{
result = bullets;
v2 = *(_QWORD *)((char *)bullets + i);
if ( v2 )
result = (void *)bullet_draw(v2);
}
return result;
}

canonical_decompiled_code:
void *func7()
{
__int64 i; // rbx
void *result; // rax
__int64 v2; // rdi

for ( i = 0LL; i != 160; i += 8LL )
{
result = bullets;
v2 = *(_QWORD *)((char *)bullets + i);
if ( v2 )
result = (void *)func1(v2);
}
return result;
}

original_code:
void bullets_draw()
{
 for (int i = 0; i < MAX_BULLETS; ++i) {
  if (bullets[i]) {
   bullet_draw(bullets[i]);
  }
 }
}


decompiled_code:
void bullets_cleanup()
{
__int64 i; // rbx
void *v1; // rdi

for ( i = 0LL; i != 160; i += 8LL )
{
v1 = *(void **)((char *)bullets + i);
if ( v1 )
{
free(v1);
*(_QWORD *)((char *)bullets + i) = 0LL;
}
}
free(bullets);
}

canonical_decompiled_code:
void func8()
{
__int64 i; // rbx
void *v1; // rdi

for ( i = 0LL; i != 160; i += 8LL )
{
v1 = *(void **)((char *)bullets + i);
if ( v1 )
{
free(v1);
*(_QWORD *)((char *)bullets + i) = 0LL;
}
}
free(bullets);
}

original_code:
void bullets_cleanup()
{
 for (int i = 0; i < MAX_BULLETS; ++i) {
  if (bullets[i]) {
   free(bullets[i]);
   bullets[i] = ((void *)0);
  }
 }
 free(bullets);
}


decompiled_code:
void init_image_assets()
{
imageAssetIndex = 0;
}

canonical_decompiled_code:
void func0()
{
imageAssetIndex = 0;
}

original_code:
void init_image_assets()
{
 int i;
 imageAssetIndex = 0;
}


decompiled_code:
__int64 free_image_assets()
{
__int64 v0; // rbx
__int64 result; // rax

if ( imageAssetIndex > 0 )
{
v0 = 0LL;
do
result = SDL_FreeSurface(image_assets[2 * v0++]);
while ( imageAssetIndex > (int)v0 );
}
return result;
}

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

if ( imageAssetIndex > 0 )
{
v0 = 0LL;
do
result = SDL_FreeSurface(image_assets[2 * v0++]);
while ( imageAssetIndex > (int)v0 );
}
return result;
}

original_code:
void free_image_assets()
{
 int i;
 for (i = 0; i < imageAssetIndex; i++)
 {
  SDL_FreeSurface(image_assets[i].image);
 }
}