decompiled_code:
double __fastcall dt_log(double *a1, int a2, double a3, double a4, double a5)
{
double *v5; // rbp
double v6; // xmm1_8
double v7; // xmm0_8
double v10; // [rsp+10h] [rbp-38h]

v5 = a1;
v10 = log(a5);
if ( a2 <= 0 )
return 0.0;
v6 = 0.0;
do
{
v7 = *v5++;
v6 = v6
+ log((v7 - a4) / a5 * ((v7 - a4) / a5) / a3 + 1.0)
* (COERCE_DOUBLE(COERCE_UNSIGNED_INT64(a3 + 1.0) ^ LC3)
* 0.5)
- v10;
}
while ( &a1[(unsigned int)(a2 - 1) + 1] != v5 );
return v6;
}

canonical_decompiled_code:
double __fastcall func1(double *a1, int a2, double a3, double a4, double a5)
{
double *v5; // rbp
double v6; // xmm1_8
double v7; // xmm0_8
double v10; // [rsp+10h] [rbp-38h]

v5 = a1;
v10 = log(a5);
if ( a2 <= 0 )
return 0.0;
v6 = 0.0;
do
{
v7 = *v5++;
v6 = v6
+ log((v7 - a4) / a5 * ((v7 - a4) / a5) / a3 + 1.0)
* (COERCE_DOUBLE(COERCE_UNSIGNED_INT64(a3 + 1.0) ^ LC3)
* 0.5)
- v10;
}
while ( &a1[(unsigned int)(a2 - 1) + 1] != v5 );
return v6;
}

original_code:
double dt_log(double * x, int n, double df, double location, double scale)
{
  int i;
  double logDensity = 0, z, logScale;
  logScale = log(scale);
  for (i=0; i<n; i++)
  {
    z = (x[i] - location) / scale;
    logDensity += -(df+1)/2 * log(1 + z*z/df) - logScale;
  }
  return logDensity;
}

canonical_original_code:
double dt_log(double *x, int n, double df, double location, double scale)
{
  int i;
  double logDensity = 0; double z; double logScale;
  logScale = log(scale);
  for (i=0; i<n; i++)
  {
    z = (x[i] - location) / scale;
    logDensity += -(df+1)/2 * log(1 + z*z/df) - logScale;
  }
  return logDensity;
}

variable_types:
x
double *

variable_types: df double

variable_types: location double

variable_types: scale double

variable_types: logDensity double

variable_types: z double

variable_types: logScale double


lmT

repo: awblocker/rowavedt

decompiled_code:
__int64 __fastcall lmT(__int64 a1, int a2, double a3, double a4, __int64 a5, __int64 a6, int a7, double *a8, double *a9, unsigned int a10, int a11, double *a12, double *a13, __int64 a14, double *a15)
{
int v16; // ebp
int v17; // er13
double v18; // xmm2_8
double v19; // xmm1_8
double *v20; // rax
double v21; // xmm0_8
__int64 v22; // r8
__m128d v23; // xmm0
__m128d v24; // xmm2
char *v25; // rdx
__int64 *v26; // rcx
int v27; // eax
__int64 v28; // xmm0_8
char *v29; // rdx
int v30; // eax
_QWORD *v31; // r14
double *v32; // r15
_QWORD *v33; // rax
double v34; // xmm1_8
__int64 v35; // rax
__int64 v36; // rdx
double v37; // xmm0_8
double v38; // xmm2_8
double v39; // xmm2_8
double v40; // xmm1_8
double v41; // xmm1_8
double v42; // xmm0_8
__int64 v43; // rax
double v44; // xmm3_8
double v45; // xmm1_8
__int64 v46; // rax
__int64 v47; // rdx
double v48; // xmm0_8
double v49; // xmm2_8
double v50; // xmm2_8
double v51; // xmm1_8
double v52; // xmm1_8
double v53; // xmm1_8
__int64 v57; // [rsp+18h] [rbp-B0h]
unsigned int v58; // [rsp+18h] [rbp-B0h]
char *ptr; // [rsp+20h] [rbp-A8h]
void *v60; // [rsp+28h] [rbp-A0h]
double v61; // [rsp+30h] [rbp-98h]
void *v63; // [rsp+38h] [rbp-90h]
void *v64; // [rsp+40h] [rbp-88h]
void *v65; // [rsp+48h] [rbp-80h]
void *v66; // [rsp+50h] [rbp-78h]
unsigned int v67; // [rsp+58h] [rbp-70h]
signed int v68; // [rsp+5Ch] [rbp-6Ch]
double *v69; // [rsp+60h] [rbp-68h]
double v71; // [rsp+80h] [rbp-48h] BYREF
double v72[8]; // [rsp+88h] [rbp-40h] BYREF

v16 = a7;
v68 = a7 + a10;
v17 = a7 + a10 - 1;
arrayMinMax(a8, (unsigned int)a7, &v71, v72);
if ( v16 > 0 )
{
v18 = v71;
v19 = v72[0] - v71;
v20 = a8;
do
{
v21 = *v20++;
*(v20 - 1) = (v21 - v18) / v19 * (double)(a2 - 1);
}
while ( v20 != &a8[(unsigned int)(a7 - 1) + 1] );
}
ptr = (char *)calloc((int)(v17 * a10), 8uLL);
if ( !ptr )
goto LABEL_52;
v60 = calloc(v17, 8uLL);
if ( !v60 )
goto LABEL_52;
dcopy((unsigned int)a7, a6, 1LL, v60, 1LL);
if ( a7 > 0 )
{
v22 = 0LL;
do
{
v23.m128d_f64[0] = a8[v22] + 0.5;
if ( COERCE_DOUBLE(*(_QWORD *)&v23.m128d_f64[0] & LC4) < 4.503599627370496e15 )
{
v24.m128d_f64[0] = (double)(int)v23.m128d_f64[0];
*(_QWORD *)&v23.m128d_f64[0] = COERCE_UNSIGNED_INT64(v24.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v24, v23) & LC5)) | ~LC4 & *(_QWORD *)&v23.m128d_f64[0];
}
if ( (int)a10 > 0 )
{
v25 = &ptr[8 * v22];
v26 = (__int64 *)(a1 + 8LL * (int)v23.m128d_f64[0]);
v27 = 0;
do
{
v28 = *v26;
++v27;
v26 += a2;
*(_QWORD *)v25 = v28;
v25 += 8 * v17;
}
while ( a10 != v27 );
}
++v22;
}
while ( a7 != v22 );
}
if ( a7 < v17 )
{
v29 = &ptr[8 * v17 + 8 * (__int64)a7];
v30 = a7;
do
{
++v30;
*(_QWORD *)v29 = LC5;
v29 += 8 * v68;
}
while ( v17 != v30 );
}
v63 = malloc(8LL * (int)(v17 * a10));
if ( !v63
|| (v64 = malloc(8LL * (int)(a10 * a10))) == 0LL
|| (v31 = calloc(v17, 8uLL)) == 0LL
|| (v65 = calloc(v17, 8uLL)) == 0LL
|| (v66 = calloc(v17, 8uLL)) == 0LL
|| (v32 = (double *)calloc(v17, 8uLL)) == 0LL )
{
LABEL_52:
fwrite("Error -- out of memory\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
if ( a7 > 0 )
{
v33 = v31;
do
*v33++ = LC5;
while ( &v31[a7] != v33 );
}
v67 = a10 - 1;
v57 = a7;
dcopy(a10 - 1, a9, 1LL, &v31[v57], 1LL);
wls(ptr, (unsigned int)v17, a10, v60, v31, v64, v65, v63, v66, a14);
calcResid(ptr, (unsigned int)v17, a10, v60, a14, v32);
*a15 = 0.0;
if ( v17 > 0 )
{
v34 = 0.0;
v35 = 0LL;
do
{
v36 = v35;
v37 = v32[v35] * v32[v35] * *(double *)&v31[v35];
++v35;
v34 = v34 + v37;
}
while ( v68 - 2 != v36 );
*a15 = v34;
}
v38 = *a15 / dasum((unsigned int)v17, v31, 1LL);
*a15 = v38;
if ( v38 < 0.0 )
v39 = sqrt(v38);
else
v39 = sqrt(v38);
*a13 = dt_log(v32, (unsigned int)a7, a3, 0.0, v39);
v40 = *a15 / *a9;
if ( v40 < 0.0 )
v41 = sqrt(v40);
else
v41 = sqrt(v40);
v69 = &v32[v57];
v42 = dnorm_log(&v32[v57], v67, 0.0, v41);
v58 = 0;
v61 = v42 + *a13;
*a12 = v61;
if ( a11 > 0 )
{
while ( 1 )
{
if ( a7 > 0 )
{
v43 = 0LL;
v44 = *a15;
do
{
*(double *)&v31[v43] = (a3 + 1.0) / (v32[v43] * v32[v43] / v44 + a3);
++v43;
}
while ( a7 != v43 );
}
wls(ptr, (unsigned int)v17, a10, v60, v31, v64, v65, v63, v66, a14);
calcResid(ptr, (unsigned int)v17, a10, v60, a14, v32);
*a15 = 0.0;
if ( v17 > 0 )
{
v45 = 0.0;
v46 = 0LL;
do
{
v47 = v46;
v48 = v32[v46] * v32[v46] * *(double *)&v31[v46];
++v46;
v45 = v45 + v48;
}
while ( v47 != v68 - 2 );
*a15 = v45;
}
v49 = *a15 / dasum((unsigned int)v17, v31, 1LL);
*a15 = v49;
v50 = v49 < 0.0 ? sqrt(v49) : sqrt(v49);
*a13 = dt_log(v32, (unsigned int)a7, a3, 0.0, v50);
v51 = *a15 / *a9;
v52 = v51 < 0.0 ? sqrt(v51) : sqrt(v51);
v53 = *a13 + dnorm_log(v69, v67, 0.0, v52);
*a12 = v53;
if ( a4 > (v53 - v61) / COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v61 + v53) & LC4)
+ (v53 - v61) / COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v61 + v53) & LC4) )
break;
if ( a11 == ++v58 )
break;
v61 = v53;
}
}
free(ptr);
free(v60);
free(v63);
free(v64);
free(v31);
free(v65);
free(v66);
free(v32);
return v58;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, double a3, double a4, __int64 a5, __int64 a6, int a7, double *a8, double *a9, unsigned int a10, int a11, double *a12, double *a13, __int64 a14, double *a15)
{
int v16; // ebp
int v17; // er13
double v18; // xmm2_8
double v19; // xmm1_8
double *v20; // rax
double v21; // xmm0_8
__int64 v22; // r8
__m128d v23; // xmm0
__m128d v24; // xmm2
char *v25; // rdx
__int64 *v26; // rcx
int v27; // eax
__int64 v28; // xmm0_8
char *v29; // rdx
int v30; // eax
_QWORD *v31; // r14
double *v32; // r15
_QWORD *v33; // rax
double v34; // xmm1_8
__int64 v35; // rax
__int64 v36; // rdx
double v37; // xmm0_8
double v38; // xmm2_8
double v39; // xmm2_8
double v40; // xmm1_8
double v41; // xmm1_8
double v42; // xmm0_8
__int64 v43; // rax
double v44; // xmm3_8
double v45; // xmm1_8
__int64 v46; // rax
__int64 v47; // rdx
double v48; // xmm0_8
double v49; // xmm2_8
double v50; // xmm2_8
double v51; // xmm1_8
double v52; // xmm1_8
double v53; // xmm1_8
__int64 v57; // [rsp+18h] [rbp-B0h]
unsigned int v58; // [rsp+18h] [rbp-B0h]
char *ptr; // [rsp+20h] [rbp-A8h]
void *v60; // [rsp+28h] [rbp-A0h]
double v61; // [rsp+30h] [rbp-98h]
void *v63; // [rsp+38h] [rbp-90h]
void *v64; // [rsp+40h] [rbp-88h]
void *v65; // [rsp+48h] [rbp-80h]
void *v66; // [rsp+50h] [rbp-78h]
unsigned int v67; // [rsp+58h] [rbp-70h]
signed int v68; // [rsp+5Ch] [rbp-6Ch]
double *v69; // [rsp+60h] [rbp-68h]
double v71; // [rsp+80h] [rbp-48h] BYREF
double v72[8]; // [rsp+88h] [rbp-40h] BYREF

v16 = a7;
v68 = a7 + a10;
v17 = a7 + a10 - 1;
arrayMinMax(a8, (unsigned int)a7, &v71, v72);
if ( v16 > 0 )
{
v18 = v71;
v19 = v72[0] - v71;
v20 = a8;
do
{
v21 = *v20++;
*(v20 - 1) = (v21 - v18) / v19 * (double)(a2 - 1);
}
while ( v20 != &a8[(unsigned int)(a7 - 1) + 1] );
}
ptr = (char *)calloc((int)(v17 * a10), 8uLL);
if ( !ptr )
goto LABEL_52;
v60 = calloc(v17, 8uLL);
if ( !v60 )
goto LABEL_52;
dcopy((unsigned int)a7, a6, 1LL, v60, 1LL);
if ( a7 > 0 )
{
v22 = 0LL;
do
{
v23.m128d_f64[0] = a8[v22] + 0.5;
if ( COERCE_DOUBLE(*(_QWORD *)&v23.m128d_f64[0] & LC4) < 4.503599627370496e15 )
{
v24.m128d_f64[0] = (double)(int)v23.m128d_f64[0];
*(_QWORD *)&v23.m128d_f64[0] = COERCE_UNSIGNED_INT64(v24.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v24, v23) & LC5)) | ~LC4 & *(_QWORD *)&v23.m128d_f64[0];
}
if ( (int)a10 > 0 )
{
v25 = &ptr[8 * v22];
v26 = (__int64 *)(a1 + 8LL * (int)v23.m128d_f64[0]);
v27 = 0;
do
{
v28 = *v26;
++v27;
v26 += a2;
*(_QWORD *)v25 = v28;
v25 += 8 * v17;
}
while ( a10 != v27 );
}
++v22;
}
while ( a7 != v22 );
}
if ( a7 < v17 )
{
v29 = &ptr[8 * v17 + 8 * (__int64)a7];
v30 = a7;
do
{
++v30;
*(_QWORD *)v29 = LC5;
v29 += 8 * v68;
}
while ( v17 != v30 );
}
v63 = malloc(8LL * (int)(v17 * a10));
if ( !v63
|| (v64 = malloc(8LL * (int)(a10 * a10))) == 0LL
|| (v31 = calloc(v17, 8uLL)) == 0LL
|| (v65 = calloc(v17, 8uLL)) == 0LL
|| (v66 = calloc(v17, 8uLL)) == 0LL
|| (v32 = (double *)calloc(v17, 8uLL)) == 0LL )
{
LABEL_52:
fwrite("Error -- out of memory\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
if ( a7 > 0 )
{
v33 = v31;
do
*v33++ = LC5;
while ( &v31[a7] != v33 );
}
v67 = a10 - 1;
v57 = a7;
dcopy(a10 - 1, a9, 1LL, &v31[v57], 1LL);
wls(ptr, (unsigned int)v17, a10, v60, v31, v64, v65, v63, v66, a14);
calcResid(ptr, (unsigned int)v17, a10, v60, a14, v32);
*a15 = 0.0;
if ( v17 > 0 )
{
v34 = 0.0;
v35 = 0LL;
do
{
v36 = v35;
v37 = v32[v35] * v32[v35] * *(double *)&v31[v35];
++v35;
v34 = v34 + v37;
}
while ( v68 - 2 != v36 );
*a15 = v34;
}
v38 = *a15 / dasum((unsigned int)v17, v31, 1LL);
*a15 = v38;
if ( v38 < 0.0 )
v39 = sqrt(v38);
else
v39 = sqrt(v38);
*a13 = dt_log(v32, (unsigned int)a7, a3, 0.0, v39);
v40 = *a15 / *a9;
if ( v40 < 0.0 )
v41 = sqrt(v40);
else
v41 = sqrt(v40);
v69 = &v32[v57];
v42 = dnorm_log(&v32[v57], v67, 0.0, v41);
v58 = 0;
v61 = v42 + *a13;
*a12 = v61;
if ( a11 > 0 )
{
while ( 1 )
{
if ( a7 > 0 )
{
v43 = 0LL;
v44 = *a15;
do
{
*(double *)&v31[v43] = (a3 + 1.0) / (v32[v43] * v32[v43] / v44 + a3);
++v43;
}
while ( a7 != v43 );
}
wls(ptr, (unsigned int)v17, a10, v60, v31, v64, v65, v63, v66, a14);
calcResid(ptr, (unsigned int)v17, a10, v60, a14, v32);
*a15 = 0.0;
if ( v17 > 0 )
{
v45 = 0.0;
v46 = 0LL;
do
{
v47 = v46;
v48 = v32[v46] * v32[v46] * *(double *)&v31[v46];
++v46;
v45 = v45 + v48;
}
while ( v47 != v68 - 2 );
*a15 = v45;
}
v49 = *a15 / dasum((unsigned int)v17, v31, 1LL);
*a15 = v49;
v50 = v49 < 0.0 ? sqrt(v49) : sqrt(v49);
*a13 = dt_log(v32, (unsigned int)a7, a3, 0.0, v50);
v51 = *a15 / *a9;
v52 = v51 < 0.0 ? sqrt(v51) : sqrt(v51);
v53 = *a13 + dnorm_log(v69, v67, 0.0, v52);
*a12 = v53;
if ( a4 > (v53 - v61) / COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v61 + v53) & LC4)
+ (v53 - v61) / COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v61 + v53) & LC4) )
break;
if ( a11 == ++v58 )
break;
v61 = v53;
}
}
free(ptr);
free(v60);
free(v63);
free(v64);
free(v31);
free(v65);
free(v66);
free(v32);
return v58;
}

original_code:
int lmT(double * basisMat, int basisRows, int basisCols,
    double * yVec, int n,
    double * timeVec,
    double * priorVec,
    double nu, int k,
    int maxIter, double tol,
    double * logPosterior,
    double * logLikelihood,
    double * coef,
    double * tau)
{
  int iter, i, j, m, tme;
  double logPosterior_tm1, logPrior, delta;
  m = n + k - 1;
  double minTime, maxTime;
  arrayMinMax(timeVec, n, &minTime, &maxTime);
  for (i=0; i<n; i++)
  {
    timeVec[i] = (timeVec[i] - minTime) / (maxTime - minTime);
    timeVec[i] = timeVec[i] * (basisRows-1);
  }
  double * dMat, * dVec;
  dMat = calloc( m * k, sizeof(double));
  if (dMat==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  dVec = calloc( m, sizeof(double) );
  if (dVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  dcopy(n, yVec, 1, dVec, 1);
  for (i=0; i<n; i++)
  {
    tme = (int) floor( timeVec[i] + 0.5 );
    for (j=0; j<k; j++)
    {
      dMat[i + j*m] = basisMat[tme + j*basisRows];
    }
  }
  for (i=n; i<m; i++)
  {
    j = i - n + 1;
    dMat[i + j*m] = 1;
  }
  double * sqwX, * XTX;
  sqwX = malloc(m * k * sizeof(double));
  if (sqwX==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  XTX = malloc(k * k * sizeof(double));
  if (XTX==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  double * w, * sqw, * sqwy, * resid;
  w = calloc(m, sizeof(double));
  if (w==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  sqw = calloc(m, sizeof(double));
  if (sqw==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  sqwy = calloc(m, sizeof(double));
  if (sqwy==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  resid = calloc(m, sizeof(double));
  if (resid==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  for (i=0; i<n; i++)
  {
    w[i] = 1;
  }
  dcopy(k-1, priorVec, 1, &w[n], 1);
  wls(dMat, m, k, dVec, w, XTX, sqw, sqwX, sqwy, coef);
  calcResid(dMat, m, k, dVec, coef, resid);
  (*tau) = 0;
  for (i=0; i<m; i++)
  {
    (*tau) += resid[i] * resid[i] * w[i];
  }
  (*tau) /= dasum(m, w, 1);
  (*logLikelihood) = dt_log(resid, n, nu, 0, sqrt(*tau));
  logPrior = dnorm_log(&resid[n], k-1, 0, sqrt((*tau)/priorVec[0]));
  (*logPosterior) = (*logLikelihood) + logPrior;
  logPosterior_tm1 = (*logPosterior);
  for (iter=0; iter<maxIter; iter++)
  {
    for (i=0; i<n; i++)
    {
      w[i] = (nu + 1) / (nu + resid[i]*resid[i]/(*tau));
    }
    wls(dMat, m, k, dVec, w, XTX, sqw, sqwX, sqwy, coef);
    calcResid(dMat, m, k, dVec, coef, resid);
    (*tau) = 0;
    for (i=0; i<m; i++)
    {
      (*tau) += resid[i] * resid[i] * w[i];
    }
    (*tau) /= dasum(m, w, 1);
    (*logLikelihood) = dt_log(resid, n, nu, 0, sqrt(*tau));
    logPrior = dnorm_log(&resid[n], k-1, 0, sqrt((*tau)/priorVec[0]));
    (*logPosterior) = (*logLikelihood) + logPrior;
    delta = ((*logPosterior) - logPosterior_tm1) /
      fabs((*logPosterior) + logPosterior_tm1) * 2;
    if (delta < tol)
    {
      logPosterior_tm1 = (*logPosterior);
      break;
    }
    logPosterior_tm1 = (*logPosterior);
  }
  free(dMat);
  dMat = ((void *)0);
  free(dVec);
  dVec = ((void *)0);
  free(sqwX);
  sqwX = ((void *)0);
  free(XTX);
  XTX = ((void *)0);
  free(w);
  w=((void *)0);
  free(sqw);
  sqw=((void *)0);
  free(sqwy);
  sqw=((void *)0);
  free(resid);
  resid=((void *)0);
  return iter;
}

canonical_original_code:
int lmT(double *basisMat, int basisRows, int basisCols, double *yVec, int n, double *timeVec, double *priorVec, double nu, int k, int maxIter, double tol, double *logPosterior, double *logLikelihood, double *coef, double *tau)
{
  int iter; int i; int j; int m; int tme;
  double logPosterior_tm1; double logPrior; double delta;
  m = n + k - 1;
  double minTime; double maxTime;
  arrayMinMax(timeVec, n, &minTime, &maxTime);
  for (i=0; i<n; i++)
  {
    timeVec[i] = (timeVec[i] - minTime) / (maxTime - minTime);
    timeVec[i] = timeVec[i] * (basisRows-1);
  }
  double *dMat; double *dVec;
  dMat = calloc( m * k, sizeof(double));
  if (dMat==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  dVec = calloc( m, sizeof(double) );
  if (dVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  dcopy(n, yVec, 1, dVec, 1);
  for (i=0; i<n; i++)
  {
    tme = (int) floor( timeVec[i] + 0.5 );
    for (j=0; j<k; j++)
    {
      dMat[i + j*m] = basisMat[tme + j*basisRows];
    }
  }
  for (i=n; i<m; i++)
  {
    j = i - n + 1;
    dMat[i + j*m] = 1;
  }
  double *sqwX; double *XTX;
  sqwX = malloc(m * k * sizeof(double));
  if (sqwX==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  XTX = malloc(k * k * sizeof(double));
  if (XTX==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  double *w; double *sqw; double *sqwy; double *resid;
  w = calloc(m, sizeof(double));
  if (w==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  sqw = calloc(m, sizeof(double));
  if (sqw==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  sqwy = calloc(m, sizeof(double));
  if (sqwy==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  resid = calloc(m, sizeof(double));
  if (resid==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  for (i=0; i<n; i++)
  {
    w[i] = 1;
  }
  dcopy(k-1, priorVec, 1, &w[n], 1);
  wls(dMat, m, k, dVec, w, XTX, sqw, sqwX, sqwy, coef);
  calcResid(dMat, m, k, dVec, coef, resid);
  (*tau) = 0;
  for (i=0; i<m; i++)
  {
    (*tau) += resid[i] * resid[i] * w[i];
  }
  (*tau) /= dasum(m, w, 1);
  (*logLikelihood) = dt_log(resid, n, nu, 0, sqrt(*tau));
  logPrior = dnorm_log(&resid[n], k-1, 0, sqrt((*tau)/priorVec[0]));
  (*logPosterior) = (*logLikelihood) + logPrior;
  logPosterior_tm1 = (*logPosterior);
  for (iter=0; iter<maxIter; iter++)
  {
    for (i=0; i<n; i++)
    {
      w[i] = (nu + 1) / (nu + resid[i]*resid[i]/(*tau));
    }
    wls(dMat, m, k, dVec, w, XTX, sqw, sqwX, sqwy, coef);
    calcResid(dMat, m, k, dVec, coef, resid);
    (*tau) = 0;
    for (i=0; i<m; i++)
    {
      (*tau) += resid[i] * resid[i] * w[i];
    }
    (*tau) /= dasum(m, w, 1);
    (*logLikelihood) = dt_log(resid, n, nu, 0, sqrt(*tau));
    logPrior = dnorm_log(&resid[n], k-1, 0, sqrt((*tau)/priorVec[0]));
    (*logPosterior) = (*logLikelihood) + logPrior;
    delta = ((*logPosterior) - logPosterior_tm1) /
      fabs((*logPosterior) + logPosterior_tm1) * 2;
    if (delta < tol)
    {
      logPosterior_tm1 = (*logPosterior);
      break;
    }
    logPosterior_tm1 = (*logPosterior);
  }
  free(dMat);
  dMat = ((void *)0);
  free(dVec);
  dVec = ((void *)0);
  free(sqwX);
  sqwX = ((void *)0);
  free(XTX);
  XTX = ((void *)0);
  free(w);
  w=((void *)0);
  free(sqw);
  sqw=((void *)0);
  free(sqwy);
  sqw=((void *)0);
  free(resid);
  resid=((void *)0);
  return iter;
}

variable_types:
basisMat
double *

variable_types: basisRows int

variable_types: basisCols int

variable_types:
yVec
double *

variable_types: n int

variable_types:
timeVec
double *

variable_types:
priorVec
double *

variable_types: nu double

variable_types: k int

variable_types: maxIter int

variable_types: tol double

variable_types:
logPosterior
double *

variable_types:
logLikelihood
double *

variable_types:
coef
double *

variable_types:
tau
double *

variable_types: iter int

variable_types: i int

variable_types: j int

variable_types: m int

variable_types: tme int

variable_types: logPrior double

variable_types: delta double

variable_types: minTime double

variable_types: maxTime double

variable_types:
dMat
double *

variable_types:
dVec
double *

variable_types:
sqwX
double *

variable_types:
XTX
double *

variable_types:
w
double *

variable_types:
sqw
double *

variable_types:
sqwy
double *

variable_types:
resid
double *

return_type: int


main

canonical_name: main

repo: awdavies/crumbshell

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v4; // bl
char src; // [rsp+Fh] [rbp-99h] BYREF
char dest[152]; // [rsp+10h] [rbp-98h] BYREF

v3 = getenv("PATH");
tokenize(&path, v3, 58LL);
printf("crsh> ");
memset(dest, 0, 0x80uLL);
do
{
while ( 1 )
{
src = getchar();
v4 = src;
if ( src != 10 )
break;
run_cmd_str(dest);
memset(dest, 0, 0x80uLL);
printf("crsh> ");
}
strncat(dest, &src, 1uLL);
}
while ( v4 != -1 );
clear_str_arr(&path);
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v4; // bl
char src; // [rsp+Fh] [rbp-99h] BYREF
char dest[152]; // [rsp+10h] [rbp-98h] BYREF

v3 = getenv("PATH");
func11(&path, v3, 58LL);
printf("crsh> ");
memset(dest, 0, 0x80uLL);
do
{
while ( 1 )
{
src = getchar();
v4 = src;
if ( src != 10 )
break;
func12(dest);
memset(dest, 0, 0x80uLL);
printf("crsh> ");
}
strncat(dest, &src, 1uLL);
}
while ( v4 != -1 );
func7(&path);
putchar(10);
return 0;
}

original_code:
int main(int argc, char ** argv, char ** envp) {
  char buf[128];
  char input = '\0';
  tokenize(path, getenv("PATH"), ':');
  printf(PS1);
  bzero(buf, 128);
  while (input != (-1)) {
    input = getchar();
    switch (input) {
      case '\n':
        run_cmd_str(buf);
        bzero(buf, 128);
        printf(PS1);
        break;
      default:
        strncat(buf, &input, 1);
        break;
    }
  }
  clear_str_arr(path);
  printf("\n");
  return 0;
}

canonical_original_code:
int main(int argc, char **argv, char **envp) {
  char buf[128];
  char input = '\0';
  tokenize(path, getenv("PATH"), ':');
  printf(PS1);
  bzero(buf, 128);
  while (input != (-1)) {
    input = getchar();
    switch (input) {
      case '\n':
        run_cmd_str(buf);
        bzero(buf, 128);
        printf(PS1);
        break;
      default:
        strncat(buf, &input, 1);
        break;
    }
  }
  clear_str_arr(path);
  printf("\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
envp
char **

variable_types:
buf
char [128]

variable_types: input char


decompiled_code:
void __fastcall clear_str_arr(void **a1)
{
void **v1; // rbx
void *v2; // rdi

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
free(v2);
*v1++ = 0LL;
v2 = *v1;
}
while ( *v1 );
}
}

canonical_decompiled_code:
void __fastcall func7(void **a1)
{
void **v1; // rbx
void *v2; // rdi

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
free(v2);
*v1++ = 0LL;
v2 = *v1;
}
while ( *v1 );
}
}

original_code:
static void clear_str_arr(char ** str_arr) {
  int i;
  for (i = 0; str_arr[i]; i++) {
    free(str_arr[i]);
    str_arr[i] = ((void *)0);
  }
}

canonical_original_code:
static void clear_str_arr(char **str_arr) {
  int i;
  for (i = 0; str_arr[i]; i++) {
    free(str_arr[i]);
    str_arr[i] = ((void *)0);
  }
}

variable_types: i int

return_type: void


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

if ( fork() )
return wait(0LL);
result = execv(arg_buf, &arg_buf);
if ( result < 0 )
{
printf("%s: no such command\n", arg_buf);
exit(1);
}
return result;
}

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

if ( fork() )
return wait(0LL);
result = execv(arg_buf, &arg_buf);
if ( result < 0 )
{
printf("%s: no such command\n", arg_buf);
exit(1);
}
return result;
}

original_code:
static void run_child_proc() {
  int status;
  pid_t ret = fork();
  if (ret == 0) {
    status= execv(arg_buf[0], arg_buf);
    if (status < 0) {
      printf("%s: no such command\n", arg_buf[0]);
      exit(1);
    }
  } else {
    wait(((void *)0));
  }
}

canonical_original_code:
static void run_child_proc() {
  int status;
  int ret = fork();
  if (ret == 0) {
    status= execv(arg_buf[0], arg_buf);
    if (status < 0) {
      printf("%s: no such command\n", arg_buf[0]);
      exit(1);
    }
  } else {
    wait(((void *)0));
  }
}

variable_types: status int

variable_types: ret int


tokenize

decompiled_code:
void __fastcall tokenize(char **a1, const char *a2, char a3)
{
char **v3; // r13
const char *v4; // rbx
char v5; // al
int v7; // er12
const char *v8; // rsi
char *v9; // r8
__int64 v10; // rax
char v11[184]; // [rsp-B8h] [rbp-B8h] BYREF

if ( a2 )
{
v3 = a1;
v4 = a2;
memset(v11, 0, 0x80uLL);
v5 = *a2;
if ( *a2 )
{
v7 = 0;
do
{
while ( a3 != v5 )
{
v8 = v4++;
strncat(v11, v8, 1uLL);
v5 = *v4;
if ( !*v4 )
goto LABEL_7;
}
++v4;
v9 = alloc_str_constprop_0(v11);
v10 = v7++;
a1[v10] = v9;
memset(v11, 0, 0x80uLL);
v5 = *v4;
}
while ( *v4 );
LABEL_7:
v3 = &a1[v7];
}
*v3 = alloc_str_constprop_0(v11);
}
}

original_code:
static void tokenize(char ** str_arr, char * buf, char delim) {
  int i = 0;
  char token[128];
  if (!buf || !str_arr) {
    return;
  }
  bzero(token, 128);
  for (i = 0; *buf; buf++) {
    if (*buf == delim) {
      str_arr[i] = alloc_str(str_arr[i], token);
      bzero(token, 128);
      ++i;
    } else {
      strncat(token, buf, 1);
    }
  }
  str_arr[i] = alloc_str(str_arr[i], token);
}

canonical_original_code:
static void tokenize(char **str_arr, char *buf, char delim) {
  int i = 0;
  char token[128];
  if (!buf || !str_arr) {
    return;
  }
  bzero(token, 128);
  for (i = 0; *buf; buf++) {
    if (*buf == delim) {
      str_arr[i] = alloc_str(str_arr[i], token);
      bzero(token, 128);
      ++i;
    } else {
      strncat(token, buf, 1);
    }
  }
  str_arr[i] = alloc_str(str_arr[i], token);
}

variable_types:
buf
char *

variable_types: delim char

variable_types:
token
char [128]


decompiled_code:
void __fastcall run_cmd_str(const char *a1)
{
if ( *a1 )
{
tokenize(&arg_buf, a1, 32);
if ( !(unsigned __int8)run_builtin() )
{
if ( !index(arg_buf, 47) )
concat_path_constprop_0();
run_child_proc();
}
clear_str_arr((void **)&arg_buf);
}
}

canonical_decompiled_code:
void __fastcall func12(const char *a1)
{
if ( *a1 )
{
func11(&arg_buf, a1, 32);
if ( !(unsigned __int8)func14() )
{
if ( !index(arg_buf, 47) )
func10();
func8();
}
func7((void **)&arg_buf);
}
}

original_code:
static void run_cmd_str(char * str) {
  if (str[0] != '\0') {
    tokenize(arg_buf, str, ' ');
    if (!run_builtin()) {
      if (!index(arg_buf[0], '/'))
        concat_path(path, arg_buf);
      run_child_proc();
    }
    clear_str_arr(arg_buf);
  }
}

canonical_original_code:
static void run_cmd_str(char *str) {
  if (str[0] != '\0') {
    tokenize(arg_buf, str, ' ');
    if (!run_builtin()) {
      if (!index(arg_buf[0], '/'))
        concat_path(path, arg_buf);
      run_child_proc();
    }
    clear_str_arr(arg_buf);
  }
}

variable_types:
str
char *


decompiled_code:
int run_from_file()
{
int v0; // eax
int v1; // er13
unsigned int v2; // ebx
int v3; // er15
char v4; // dl
__int64 v5; // rax
int v6; // esi
int result; // eax
const char *v8; // rdi
int v9; // eax
char v10[128]; // [rsp+0h] [rbp-138h] BYREF
char v11[184]; // [rsp+80h] [rbp-B8h] BYREF

v0 = open(file, 0);
if ( v0 < 0 )
{
v8 = "%s: no such file.\n";
v9 = *__errno_location();
if ( v9 != 17 )
{
v8 = "%s: is a directory.\n";
if ( v9 != 21 )
{
v8 = "%s: read access denied.\n";
if ( v9 != 13 )
v8 = "%s: read access error.\n";
}
}
result = printf(v8, file);
}
else
{
v1 = v0;
v2 = 0;
while ( 1 )
{
memset(v10, 0, sizeof(v10));
memset(v11, 0, 0x80uLL);
v3 = pread(v1, v10, 0x80uLL, v2);
if ( v3 < 0 )
{
puts("error while reading file.");
goto LABEL_9;
}
v4 = v10[0];
if ( (unsigned __int8)(v10[0] + 1) <= 1u )
break;
v5 = 1LL;
do
{
v6 = v5;
if ( v4 == 10 )
break;
v10[v5 + 127] = v4;
v4 = v10[v5++];
}
while ( (unsigned __int8)(v4 + 1) > 1u );
v2 += v6;
clear_str_arr((void **)&arg_buf);
run_cmd_str(v11);
if ( !v3 )
goto LABEL_9;
}
clear_str_arr((void **)&arg_buf);
run_cmd_str(v11);
LABEL_9:
result = close(v1);
}
return result;
}

canonical_decompiled_code:
int func13()
{
int v0; // eax
int v1; // er13
unsigned int v2; // ebx
int v3; // er15
char v4; // dl
__int64 v5; // rax
int v6; // esi
int result; // eax
const char *v8; // rdi
int v9; // eax
char v10[128]; // [rsp+0h] [rbp-138h] BYREF
char v11[184]; // [rsp+80h] [rbp-B8h] BYREF

v0 = open(file, 0);
if ( v0 < 0 )
{
v8 = "%s: no such file.\n";
v9 = *__errno_location();
if ( v9 != 17 )
{
v8 = "%s: is a directory.\n";
if ( v9 != 21 )
{
v8 = "%s: read access denied.\n";
if ( v9 != 13 )
v8 = "%s: read access error.\n";
}
}
result = printf(v8, file);
}
else
{
v1 = v0;
v2 = 0;
while ( 1 )
{
memset(v10, 0, sizeof(v10));
memset(v11, 0, 0x80uLL);
v3 = pread(v1, v10, 0x80uLL, v2);
if ( v3 < 0 )
{
puts("error while reading file.");
goto LABEL_9;
}
v4 = v10[0];
if ( (unsigned __int8)(v10[0] + 1) <= 1u )
break;
v5 = 1LL;
do
{
v6 = v5;
if ( v4 == 10 )
break;
v10[v5 + 127] = v4;
v4 = v10[v5++];
}
while ( (unsigned __int8)(v4 + 1) > 1u );
v2 += v6;
func7((void **)&arg_buf);
func12(v11);
if ( !v3 )
goto LABEL_9;
}
func7((void **)&arg_buf);
func12(v11);
LABEL_9:
result = close(v1);
}
return result;
}

original_code:
static void run_from_file() {
  int fd;
  unsigned int file_offset = 0;
  int file_status = 0;
  unsigned int i;
  char rbuf[128];
  char cbuf[128];
  fd = open(arg_buf[1], 00);
  if (fd < 0) {
    switch((*__errno_location ())) {
      case 13:
        printf("%s: read access denied.\n", arg_buf[1]);
        break;
      case 17:
        printf("%s: no such file.\n", arg_buf[1]);
        break;
      case 21:
        printf("%s: is a directory.\n", arg_buf[1]);
        break;
      default:
        printf("%s: read access error.\n", arg_buf[1]);
    }
  } else {
    do {
      bzero(rbuf, 128);
      bzero(cbuf, 128);
      file_status = pread(fd, rbuf, 128, file_offset);
      if (file_status < 0) {
        printf("error while reading file.\n");
        break;
      }
      for(i = 0; rbuf[i] != (-1) && rbuf[i] != '\0' && rbuf[i] != '\n' && i < 128; i++) {
        cbuf[i] = rbuf[i];
      }
      if (rbuf[i] == '\n')
        i++;
      file_offset += i;
      clear_str_arr(arg_buf);
      run_cmd_str(cbuf);
    } while (file_status != 0 && i != 0);
    close(fd);
  }
}

variable_types: fd int

variable_types:
i
unsigned int

variable_types:
rbuf
char [128]

variable_types:
cbuf
char [128]


decompiled_code:
__int64 run_builtin()
{
char *v0; // rdi
int v2; // eax

if ( *arg_buf == 99 && arg_buf[1] == 100 && !arg_buf[2] )
{
v0 = file;
if ( !file )
v0 = getenv("HOME");
if ( chdir(v0) < 0 )
{
v2 = *__errno_location();
switch ( v2 )
{
case 20:
printf("%s: not a directory\n", file);
break;
case 40:
printf("%s: symbolic loop encountered\n", file);
break;
case 2:
printf("%s: no such directory\n", file);
break;
default:
printf("%s: error encountered\n", file);
break;
}
}
return 1LL;
}
if ( !strcmp(arg_buf, "exit") )
{
clear_str_arr((void **)&arg_buf);
clear_str_arr((void **)&path);
exit(0);
}
if ( *arg_buf == 46 && !arg_buf[1] )
{
run_from_file();
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func14()
{
char *v0; // rdi
int v2; // eax

if ( *arg_buf == 99 && arg_buf[1] == 100 && !arg_buf[2] )
{
v0 = file;
if ( !file )
v0 = getenv("HOME");
if ( chdir(v0) < 0 )
{
v2 = *__errno_location();
switch ( v2 )
{
case 20:
printf("%s: not a directory\n", file);
break;
case 40:
printf("%s: symbolic loop encountered\n", file);
break;
case 2:
printf("%s: no such directory\n", file);
break;
default:
printf("%s: error encountered\n", file);
break;
}
}
return 1LL;
}
if ( !strcmp(arg_buf, "exit") )
{
func7((void **)&arg_buf);
func7((void **)&path);
exit(0);
}
if ( *arg_buf == 46 && !arg_buf[1] )
{
func13();
return 1LL;
}
return 0LL;
}

original_code:
static _Bool run_builtin() {
  int ret;
  if (strncmp(arg_buf[0], "cd", 3) == 0) {
    if (arg_buf[1])
      ret = chdir(arg_buf[1]);
    else
      ret = chdir(getenv("HOME"));
    if (ret < 0) {
      switch ((*__errno_location ())) {
        case 2:
          printf("%s: no such directory\n", arg_buf[1]);
          break;
        case 20:
          printf("%s: not a directory\n", arg_buf[1]);
          break;
        case 40:
          printf("%s: symbolic loop encountered\n", arg_buf[1]);
          break;
        default:
          printf("%s: error encountered\n", arg_buf[1]);
          break;
      }
    }
    return 1;
  }
  if (strncmp(arg_buf[0], "exit", 5) == 0) {
    clear_str_arr(arg_buf);
    clear_str_arr(path);
    exit(0);
  }
  if (strncmp(arg_buf[0], ".", 128) == 0) {
    run_from_file();
    return 1;
  }
  return 0;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
__int64 v5; // rsi
void *v6; // rbx
void *thread_return[2]; // [rsp+8h] [rbp-10h] BYREF

thread_pool = malloc(0x10uLL);
v3 = pthread_create((pthread_t *)thread_pool, 0LL, prime_tester, 0LL);
if ( v3 )
{
v5 = v3;
}
else
{
v4 = pthread_create((pthread_t *)thread_pool + 1, 0LL, prime_tester, (void *)1);
v5 = v4;
if ( !v4 )
{
pthread_join(*(_QWORD *)thread_pool, thread_return);
v6 = thread_return[0];
pthread_join(*((_QWORD *)thread_pool + 1), thread_return);
printf("The sum is %ld\n", (char *)thread_return[0] + (unsigned __int64)v6);
free(thread_pool);
pthread_exit(0LL);
}
}
printf("ERR! return code from pthread_create() is %d\n", v5);
return 1;
}

original_code:
int main(int argc, char **argv) {
  uint32_t nthreads = DEFAULT_THREADS;
  thread_pool = malloc(nthreads * sizeof(thread_pool));
  int res;
  for (long i = 0; i < nthreads; ++i) {
    res = pthread_create(&thread_pool[i], ((void *)0), prime_tester,
        (void *)i);
    if (res) {
      printf("ERR!  return code from pthread_create() is %d\n", res);
      return 1;
    }
  }
  long accumulator = 0;
  long join_res;
  for (long i = 0; i < nthreads; ++i) {
    pthread_join(thread_pool[i], (void **)&join_res);
    accumulator += join_res;
  }
  printf("The sum is %ld\n", accumulator);
  free(thread_pool);
  pthread_exit(((void *)0));
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  unsigned int nthreads = DEFAULT_THREADS;
  thread_pool = malloc(nthreads * sizeof(thread_pool));
  int res;
  for (long i = 0; i < nthreads; ++i) {
    res = pthread_create(&thread_pool[i], ((void *)0), prime_tester,
        (void *)i);
    if (res) {
      printf("ERR!  return code from pthread_create() is %d\n", res);
      return 1;
    }
  }
  long accumulator = 0;
  long join_res;
  for (long i = 0; i < nthreads; ++i) {
    pthread_join(thread_pool[i], (void **)&join_res);
    accumulator += join_res;
  }
  printf("The sum is %ld\n", accumulator);
  free(thread_pool);
  pthread_exit(((void *)0));
  return 0;
}

variable_types:
nthreads
unsigned int

variable_types: res int

variable_types: i long

variable_types: accumulator long


original_code:
static inline _Bool prime_test(long n) {
  if (n <= 1L) {
    return 0;
  }
  if (n % 2L == 0) {
    return 2L == n;
  }
  if (n % 3L == 0) {
    return 3L == n;
  }
  for (long i = 5L; i * i <= n; i += 6L) {
    if (n % i == 0L)
      return 0;
    if (n % (i + 2) == 0L)
      return 0;
  }
  return 1;
}

variable_types: n long


decompiled_code:
void __fastcall __noreturn prime_tester(void *a1)
{
__int64 v1; // rdi
char *v2; // r9
__int64 v3; // r10

v1 = 1000000LL * (_QWORD)a1;
while ( 1 )
{
if ( (unsigned __int8)prime_test(v1) )
v2 += v1;
if ( v3 == ++v1 )
pthread_exit(v2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
__int64 v1; // rdi
char *v2; // r9
__int64 v3; // r10

v1 = 1000000LL * (_QWORD)a1;
while ( 1 )
{
if ( (unsigned __int8)func7(v1) )
v2 += v1;
if ( v3 == ++v1 )
pthread_exit(v2);
}
}

original_code:
void *prime_tester(void *thread_num) {
  const long range = 2000000 / DEFAULT_THREADS;
  long thread_id = (long) thread_num;
  long accumulator = 0;
  for (long i = range * thread_id; i < (thread_id * range) + range; ++i) {
    if (prime_test(i)) {
      accumulator += i;
    }
  }
  pthread_exit((void *)accumulator);
}

canonical_original_code:
void *prime_tester(void *thread_num) {
  long range = 2000000 / DEFAULT_THREADS;
  long thread_id = (long) thread_num;
  long accumulator = 0;
  for (long i = range * thread_id; i < (thread_id * range) + range; ++i) {
    if (prime_test(i)) {
      accumulator += i;
    }
  }
  pthread_exit((void *)accumulator);
}

variable_types: range long

return_type: void *


repo: awilliam/tests

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int v4; // er12
size_t v5; // rax
ssize_t v6; // rax
int v7; // eax
unsigned int v8; // er13
int *v9; // rax
char *v10; // rax
int *v12; // rax
char *v13; // rax
int v14; // er12
int v15; // edi
unsigned int v16; // [rsp+0h] [rbp-148h]
unsigned int v17; // [rsp+4h] [rbp-144h]
unsigned int v18; // [rsp+8h] [rbp-140h]
unsigned int v19; // [rsp+Ch] [rbp-13Ch]
int fd[2]; // [rsp+10h] [rbp-138h] BYREF
__int64 v21; // [rsp+18h] [rbp-130h] BYREF
char s[64]; // [rsp+20h] [rbp-128h] BYREF
char buf[64]; // [rsp+60h] [rbp-E8h] BYREF
struct stat v24; // [rsp+A0h] [rbp-A8h] BYREF

v3 = argv;
v21 = 8LL;
if ( argc == 2 && (argv = (const char **)"%04x:%02x:%02x.%d", (unsigned int)__isoc99_sscanf() == 4) )
{
fd[0] = open("/dev/vfio/vfio", 2);
if ( fd[0] < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
printf("Failed to open /dev/vfio/vfio, %d (%s)\n", (unsigned int)fd[0], v10);
v4 = fd[0];
}
else
{
snprintf(s, 0x32uLL, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/", v16, v17, v18, v19);
v4 = stat(s, &v24);
if ( v4 < 0 )
{
puts("No such device");
}
else
{
v5 = strlen(s);
strncat(s, "iommu_group", 49 - v5);
v6 = readlink(s, buf, 0x32uLL);
if ( v6 <= 0 )
{
v4 = -1;
puts("No iommu_group for device");
}
else
{
buf[v6] = 0;
__xpg_basename(buf);
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
snprintf(s, 0x32uLL, "/dev/vfio/%d", (unsigned int)fd[1]);
v7 = open(s, 2);
v8 = v7;
if ( v7 < 0 )
{
v12 = __errno_location();
v4 = v8;
v13 = strerror(*v12);
printf("Failed to open %s, %d (%s)\n", s, v8, v13);
}
else
{
v4 = ioctl(v7, 0x3B67uLL, &v21);
if ( v4 )
{
puts("ioctl(VFIO_GROUP_GET_STATUS) failed");
}
else if ( (v21 & 0x100000000LL) != 0 )
{
v4 = ioctl(v8, 0x3B68uLL, fd);
if ( v4 )
{
puts("Failed to set group container");
}
else
{
v4 = ioctl(fd[0], 0x3B66uLL, 1LL);
if ( v4 )
{
puts("Failed to set IOMMU");
}
else
{
close(fd[0]);
strcpy(s, "THIS-IS-NOT-THE-RIGHT-NAME");
do
v14 = ioctl(v8, 0x3B6AuLL, s);
while ( v14 < 0 );
printf("Got a device named \"%s\"???\n", s);
close(v8);
v15 = v14;
v4 = -1;
close(v15);
}
}
}
else
{
v4 = -1;
puts("Group not viable, are all devices attached to vfio?");
}
}
}
else
{
v4 = -1;
puts("Unknown group");
}
}
}
}
}
else
{
v4 = -1;
usage(*v3, argv, envp);
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int v4; // er12
size_t v5; // rax
ssize_t v6; // rax
int v7; // eax
unsigned int v8; // er13
int *v9; // rax
char *v10; // rax
int *v12; // rax
char *v13; // rax
int v14; // er12
int v15; // edi
unsigned int v16; // [rsp+0h] [rbp-148h]
unsigned int v17; // [rsp+4h] [rbp-144h]
unsigned int v18; // [rsp+8h] [rbp-140h]
unsigned int v19; // [rsp+Ch] [rbp-13Ch]
int fd[2]; // [rsp+10h] [rbp-138h] BYREF
__int64 v21; // [rsp+18h] [rbp-130h] BYREF
char s[64]; // [rsp+20h] [rbp-128h] BYREF
char buf[64]; // [rsp+60h] [rbp-E8h] BYREF
struct stat v24; // [rsp+A0h] [rbp-A8h] BYREF

v3 = argv;
v21 = 8LL;
if ( argc == 2 && (argv = (const char **)"%04x:%02x:%02x.%d", (unsigned int)__isoc99_sscanf() == 4) )
{
fd[0] = open("/dev/vfio/vfio", 2);
if ( fd[0] < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
printf("Failed to open /dev/vfio/vfio, %d (%s)\n", (unsigned int)fd[0], v10);
v4 = fd[0];
}
else
{
snprintf(s, 0x32uLL, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/", v16, v17, v18, v19);
v4 = func10(s, &v24);
if ( v4 < 0 )
{
puts("No such device");
}
else
{
v5 = strlen(s);
strncat(s, "iommu_group", 49 - v5);
v6 = readlink(s, buf, 0x32uLL);
if ( v6 <= 0 )
{
v4 = -1;
puts("No iommu_group for device");
}
else
{
buf[v6] = 0;
__xpg_basename(buf);
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
snprintf(s, 0x32uLL, "/dev/vfio/%d", (unsigned int)fd[1]);
v7 = open(s, 2);
v8 = v7;
if ( v7 < 0 )
{
v12 = __errno_location();
v4 = v8;
v13 = strerror(*v12);
printf("Failed to open %s, %d (%s)\n", s, v8, v13);
}
else
{
v4 = ioctl(v7, 0x3B67uLL, &v21);
if ( v4 )
{
puts("ioctl(VFIO_GROUP_GET_STATUS) failed");
}
else if ( (v21 & 0x100000000LL) != 0 )
{
v4 = ioctl(v8, 0x3B68uLL, fd);
if ( v4 )
{
puts("Failed to set group container");
}
else
{
v4 = ioctl(fd[0], 0x3B66uLL, 1LL);
if ( v4 )
{
puts("Failed to set IOMMU");
}
else
{
close(fd[0]);
strcpy(s, "THIS-IS-NOT-THE-RIGHT-NAME");
do
v14 = ioctl(v8, 0x3B6AuLL, s);
while ( v14 < 0 );
printf("Got a device named \"%s\"???\n", s);
close(v8);
v15 = v14;
v4 = -1;
close(v15);
}
}
}
else
{
v4 = -1;
puts("Group not viable, are all devices attached to vfio?");
}
}
}
else
{
v4 = -1;
puts("Unknown group");
}
}
}
}
}
else
{
v4 = -1;
func7(*v3, argv, envp);
}
return v4;
}

original_code:
int main(int argc, char **argv)
{
 int seg, bus, slot, func;
 int ret, container, group, device, groupid, intx, unmask;
 char path[50], iommu_group_path[50], *group_name;
 struct stat st;
 ssize_t len;
 struct vfio_group_status group_status = {
  .argsz = sizeof(group_status)
 };
 struct vfio_device_info device_info = {
  .argsz = sizeof(device_info)
 };
 struct vfio_irq_info irq_info = {
  .argsz = sizeof(irq_info),
  .index = VFIO_PCI_INTX_IRQ_INDEX
 };
 struct vfio_irq_set *irq_set;
 int32_t *pfd;
 if (argc != 2) {
  usage(argv[0]);
  return -1;
 }
 ret = sscanf(argv[1], "%04x:%02x:%02x.%d", &seg, &bus, &slot, &func);
 if (ret != 4) {
  usage(argv[0]);
  return -1;
 }
 container = open("/dev/vfio/vfio", 02);
 if (container < 0) {
  printf("Failed to open /dev/vfio/vfio, %d (%s)\n",
         container, strerror((*__errno_location ())));
  return container;
 }
 snprintf(path, sizeof(path),
   "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
   seg, bus, slot, func);
 ret = stat(path, &st);
 if (ret < 0) {
  printf("No such device\n");
  return ret;
 }
 strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
 len = readlink(path, iommu_group_path, sizeof(iommu_group_path));
 if (len <= 0) {
  printf("No iommu_group for device\n");
  return -1;
 }
 iommu_group_path[len] = 0;
 group_name = __xpg_basename(iommu_group_path);
 if (sscanf(group_name, "%d", &groupid) != 1) {
  printf("Unknown group\n");
  return -1;
 }
 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
 group = open(path, 02);
 if (group < 0) {
  printf("Failed to open %s, %d (%s)\n",
         path, group, strerror((*__errno_location ())));
  return group;
 }
 ret = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 3)) << 0) | ((0) << ((0 +8)+8))), &group_status);
 if (ret) {
  printf("ioctl(VFIO_GROUP_GET_STATUS) failed\n");
  return ret;
 }
 if (!(group_status.flags & (1 << 0))) {
  printf("Group not viable, are all devices attached to vfio?\n");
  return -1;
 }
 ret = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 4)) << 0) | ((0) << ((0 +8)+8))), &container);
 if (ret) {
  printf("Failed to set group container\n");
  return ret;
 }
 ret = ioctl(container, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 2)) << 0) | ((0) << ((0 +8)+8))), 1);
 if (ret) {
  printf("Failed to set IOMMU\n");
  return ret;
 }
 close(container);
 snprintf(path, sizeof(path), "THIS-IS-NOT-THE-RIGHT-NAME");
        for (;;) {
  device = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 6)) << 0) | ((0) << ((0 +8)+8))), path);
  if (device >= 0) {
   printf("Got a device named \"%s\"???\n", path);
   break;
  }
 }
 close(group);
 close(device);
 return -1;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int seg; int bus; int slot; int func;
 int ret; int container; int group; int device; int groupid; int intx; int unmask;
 char path[50]; char iommu_group_path[50]; char *group_name;
 struct stat st;
 long len;
 struct vfio_group_status group_status = {
  .argsz = sizeof(group_status)
 };
 struct vfio_device_info device_info = {
  .argsz = sizeof(device_info)
 };
 struct vfio_irq_info irq_info = {
  .argsz = sizeof(irq_info),
  .index = VFIO_PCI_INTX_IRQ_INDEX
 };
 struct vfio_irq_set *irq_set;
 int *pfd;
 if (argc != 2) {
  usage(argv[0]);
  return -1;
 }
 ret = sscanf(argv[1], "%04x:%02x:%02x.%d", &seg, &bus, &slot, &func);
 if (ret != 4) {
  usage(argv[0]);
  return -1;
 }
 container = open("/dev/vfio/vfio", 02);
 if (container < 0) {
  printf("Failed to open /dev/vfio/vfio, %d (%s)\n",
         container, strerror((*__errno_location ())));
  return container;
 }
 snprintf(path, sizeof(path),
   "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
   seg, bus, slot, func);
 ret = stat(path, &st);
 if (ret < 0) {
  printf("No such device\n");
  return ret;
 }
 strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
 len = readlink(path, iommu_group_path, sizeof(iommu_group_path));
 if (len <= 0) {
  printf("No iommu_group for device\n");
  return -1;
 }
 iommu_group_path[len] = 0;
 group_name = __xpg_basename(iommu_group_path);
 if (sscanf(group_name, "%d", &groupid) != 1) {
  printf("Unknown group\n");
  return -1;
 }
 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
 group = open(path, 02);
 if (group < 0) {
  printf("Failed to open %s, %d (%s)\n",
         path, group, strerror((*__errno_location ())));
  return group;
 }
 ret = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 3)) << 0) | ((0) << ((0 +8)+8))), &group_status);
 if (ret) {
  printf("ioctl(VFIO_GROUP_GET_STATUS) failed\n");
  return ret;
 }
 if (!(group_status.flags & (1 << 0))) {
  printf("Group not viable, are all devices attached to vfio?\n");
  return -1;
 }
 ret = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 4)) << 0) | ((0) << ((0 +8)+8))), &container);
 if (ret) {
  printf("Failed to set group container\n");
  return ret;
 }
 ret = ioctl(container, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 2)) << 0) | ((0) << ((0 +8)+8))), 1);
 if (ret) {
  printf("Failed to set IOMMU\n");
  return ret;
 }
 close(container);
 snprintf(path, sizeof(path), "THIS-IS-NOT-THE-RIGHT-NAME");
        for (;;) {
  device = ioctl(group, (((0U) << (((0 +8)+8)+14)) | ((((';'))) << (0 +8)) | (((100 + 6)) << 0) | ((0) << ((0 +8)+8))), path);
  if (device >= 0) {
   printf("Got a device named \"%s\"???\n", path);
   break;
  }
 }
 close(group);
 close(device);
 return -1;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: seg int

variable_types: bus int

variable_types: slot int

variable_types: func int

variable_types: container int

variable_types: group int

variable_types: device int

variable_types: groupid int

variable_types: intx int

variable_types: unmask int

variable_types:
path
char [50]

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

variable_types: len long

variable_types: struct vfio_group_status { unsigned int argsz; unsigned int flags; }

variable_types: struct vfio_device_info { unsigned int argsz; unsigned int flags; unsigned int num_regions; unsigned int num_irqs; }

variable_types: struct vfio_irq_info { unsigned int argsz; unsigned int flags; unsigned int index; unsigned int count; }

variable_types: struct vfio_irq_set { unsigned int argsz; unsigned int flags; unsigned int index; unsigned int start; unsigned int count; unsigned char data[]; } *

variable_types:
pfd
int *

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


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
printf("usage: %s ssss:bb:dd.f\n", a1);
puts("\tssss: PCI segment, ex. 0000");
puts("\tbb: PCI bus, ex. 01");
puts("\tdd: PCI device, ex. 06");
return puts("\tf: PCI function, ex. 0");
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
printf("usage: %s ssss:bb:dd.f\n", a1);
puts("\tssss: PCI segment, ex. 0000");
puts("\tbb: PCI bus, ex. 01");
puts("\tdd: PCI device, ex. 06");
return puts("\tf: PCI function, ex. 0");
}

original_code:
void usage(char *name)
{
 printf("usage: %s ssss:bb:dd.f\n", name);
 printf("\tssss: PCI segment, ex. 0000\n");
 printf("\tbb:   PCI bus, ex. 01\n");
 printf("\tdd:   PCI device, ex. 06\n");
 printf("\tf:    PCI function, ex. 0\n");
}

variable_types:
name
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // rbx
int v5; // er12
int v6; // eax
const char *v7; // rdi
const char *v8; // r12
void *v9; // r14
int v10; // ebx
int v11; // er12
int *v13; // rax
char *v14; // rax
int fd[2]; // [rsp+4h] [rbp-34h]
int v16; // [rsp+Ch] [rbp-2Ch]

v16 = -1;
if ( argc <= 5 )
{
fprintf(stderr, "Usage: %s stdin# stdout# stderr# chdir program [argv0 ... ]\n", *argv);
}
else
{
v4 = 0LL;
v5 = atoi(argv[1]);
v6 = atoi(argv[2]);
v7 = argv[3];
fd[1] = v6;
v16 = atoi(v7);
if ( dup2(v5, 0) == -1 )
{
LABEL_5:
fwrite("Error in dup2\n", 1uLL, 0xEuLL, stderr);
}
else
{
while ( ++v4 != 3 )
{
if ( dup2(fd[v4], v4) == -1 )
goto LABEL_5;
}
v8 = argv[4];
if ( strlen(v8) == 1 && *v8 == 46 || !chdir(v8) )
{
v9 = malloc(8LL * (argc - 4));
memcpy(v9, argv + 5, 8LL * (unsigned int)(argc - 5));
*((_QWORD *)v9 + argc - 5) = 0LL;
v10 = 3;
v11 = sysconf(4);
if ( v11 > 3 )
{
do
fcntl(v10++, 2, 1LL);
while ( v11 != v10 );
}
if ( execvp(argv[5], (char *const *)v9) == -1 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "Error: %s\n", v14);
}
}
else
{
fwrite("Error in chdir()\n", 1uLL, 0x11uLL, stderr);
}
}
}
return -1;
}

original_code:
int main(int argc, char ** argv)
{
    int fds_map[3] = {-1, -1, -1};
    int i;
    char ** new_argv;
    int open_max;
    int open_index;
    if (argc < 6) {
        fprintf(stderr, "Usage: %s stdin# stdout# stderr# chdir program [argv0 ... ]\n",
                argv[0]);
        return -1;
    }
    fds_map[0] = atoi(argv[1]);
    fds_map[1] = atoi(argv[2]);
    fds_map[2] = atoi(argv[3]);
    for (i = 0; i < 3; i++) {
        if (dup2(fds_map[i], i) == -1) {
            fprintf(stderr, "Error in dup2\n");
            return -1;
        }
    }
    if (!(strlen(argv[4]) == 1 && strncmp(argv[4], ".", 1) == 0)) {
        if (chdir(argv[4]) != 0) {
            fprintf(stderr, "Error in chdir()\n");
            return -1;
        }
    }
    new_argv = (char **)malloc(sizeof(char *) * (argc - 4));
    for (i = 5; i < argc; i++) {
        new_argv[i - 5] = argv[i];
    }
    new_argv[argc - 5] = ((void *)0);
    open_max = sysconf(_SC_OPEN_MAX);
    for(open_index=3;open_index < open_max; open_index++) {
     fcntl(open_index, 2, 1);
    }
    if (execvp(argv[5], new_argv) == -1) {
        fprintf(stderr, "Error: %s\n", strerror((*__errno_location ())));
        return -1;
    }
    return -1;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int fds_map[3] = {-1, -1, -1};
    int i;
    char **new_argv;
    int open_max;
    int open_index;
    if (argc < 6) {
        fprintf(stderr, "Usage: %s stdin# stdout# stderr# chdir program [argv0 ... ]\n",
                argv[0]);
        return -1;
    }
    fds_map[0] = atoi(argv[1]);
    fds_map[1] = atoi(argv[2]);
    fds_map[2] = atoi(argv[3]);
    for (i = 0; i < 3; i++) {
        if (dup2(fds_map[i], i) == -1) {
            fprintf(stderr, "Error in dup2\n");
            return -1;
        }
    }
    if (!(strlen(argv[4]) == 1 && strncmp(argv[4], ".", 1) == 0)) {
        if (chdir(argv[4]) != 0) {
            fprintf(stderr, "Error in chdir()\n");
            return -1;
        }
    }
    new_argv = (char **)malloc(sizeof(char *) * (argc - 4));
    for (i = 5; i < argc; i++) {
        new_argv[i - 5] = argv[i];
    }
    new_argv[argc - 5] = ((void *)0);
    open_max = sysconf(_SC_OPEN_MAX);
    for(open_index=3;open_index < open_max; open_index++) {
     fcntl(open_index, 2, 1);
    }
    if (execvp(argv[5], new_argv) == -1) {
        fprintf(stderr, "Error: %s\n", strerror((*__errno_location ())));
        return -1;
    }
    return -1;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


repo: azat/chunkfs

decompiled_code:
__int64 __fastcall create_pool_summary(char *src, char *a2)
{
__int64 result; // rax

strcpy(a2 + 16, src);
result = 0LL;
memset(a2, 0, 0x418uLL);
*(_DWORD *)a2 = 1526618878;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *src, char *a2)
{
__int64 result; // rax

strcpy(a2 + 16, src);
result = 0LL;
memset(a2, 0, 0x418uLL);
*(_DWORD *)a2 = 1526618878;
return result;
}

original_code:
static void create_pool_summary(char *dev_name, struct chunkfs_pool *pool)
{
 struct chunkfs_dev_desc *dev_desc = &pool->p_root_desc;
 strcpy(dev_desc->d_hint, dev_name);
 dev_desc->d_uuid = ((__le64)(__u64)(0x001d001d));
 bzero(pool, sizeof(*pool));
 pool->p_magic = ((__le32)(__u32)(0x5afe5afe));
}

variable_types: struct chunkfs_pool { unsigned int p_magic; unsigned int p_chksum; unsigned long long p_flags; struct chunkfs_dev_desc { char d_hint[1024]; unsigned long long d_uuid; } p_root_desc; } *

variable_types: struct chunkfs_dev_desc { char d_hint[1024]; unsigned long long d_uuid; } *

user_defined_types: struct chunkfs_pool { unsigned int p_magic; unsigned int p_chksum; unsigned long long p_flags; struct chunkfs_dev_desc p_root_desc; }


decompiled_code:
ssize_t __fastcall write_block(char *a1, unsigned int a2, int a3, __off_t a4)
{
__int64 v6; // rcx
ssize_t result; // rax
int *v8; // rax
__int64 v9; // rsi
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // [rsp+0h] [rbp-8h]
int vars0[1030]; // [rsp+8h] [rbp+0h] BYREF

memset(vars0, 0, 0x1000uLL);
*(__int64 *)((char *)&v12 + a2) = *(_QWORD *)&a1[a2 - 8];
qmemcpy(vars0, a1, 8LL * ((a2 - 1) >> 3));
write_chksum_constprop_0((__int64)vars0);
printf("Writing magic %0x chksum %0x to offset %llu\n", (unsigned int)vars0[0], (unsigned int)vars0[1], v6);
if ( lseek(a3, a4, 0) < 0 )
{
v8 = __errno_location();
error(1, *v8, "Cannot seek");
goto LABEL_5;
}
result = write(a3, vars0, 0x1000uLL);
if ( result <= 4095 )
{
LABEL_5:
v9 = (unsigned int)*__errno_location();
error(1, v9, "Cannot write metadata at offset %llu", a4);
result = write_chunk_summaries_isra_0(1LL, v9, v10, v11);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(char *a1, unsigned int a2, int a3, __off_t a4)
{
__int64 v6; // rcx
ssize_t result; // rax
int *v8; // rax
__int64 v9; // rsi
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // [rsp+0h] [rbp-8h]
int vars0[1030]; // [rsp+8h] [rbp+0h] BYREF

memset(vars0, 0, 0x1000uLL);
*(__int64 *)((char *)&v12 + a2) = *(_QWORD *)&a1[a2 - 8];
qmemcpy(vars0, a1, 8LL * ((a2 - 1) >> 3));
func11((__int64)vars0);
printf("Writing magic %0x chksum %0x to offset %llu\n", (unsigned int)vars0[0], (unsigned int)vars0[1], v6);
if ( lseek(a3, a4, 0) < 0 )
{
v8 = __errno_location();
error(1, *v8, "Cannot seek");
goto LABEL_5;
}
result = write(a3, vars0, 0x1000uLL);
if ( result <= 4095 )
{
LABEL_5:
v9 = (unsigned int)*__errno_location();
error(1, v9, "Cannot write metadata at offset %llu", a4);
result = func13(1LL, v9, v10, v11);
}
return result;
}

original_code:
static void write_block(void *metadata, int size, int fd, __u64 offset)
{
 char buf[4096];
 int buf_size = sizeof (buf);
 struct chunkfs_chkmagic *x = (struct chunkfs_chkmagic *) buf;
 bzero(buf, buf_size);
 memcpy(buf, metadata, size);
 write_chksum(buf, size);
 printf("Writing magic %0x chksum %0x to offset %llu\n",
        ((__u32)(__le32)(x->x_magic)), ((__u32)(__le32)(x->x_chksum)),
        offset);
 if (lseek(fd, offset, 0) < 0)
  error(1, (*__errno_location ()), "Cannot seek");
 if (write(fd, buf, buf_size) < buf_size)
  error(1, (*__errno_location ()), "Cannot write metadata at offset %llu",
        (unsigned long long) offset);
}

canonical_original_code:
static void write_block(void *metadata, int size, int fd, unsigned long long offset)
{
 char buf[4096];
 int buf_size = sizeof (buf);
 struct chunkfs_chkmagic *x = (struct chunkfs_chkmagic *) buf;
 bzero(buf, buf_size);
 memcpy(buf, metadata, size);
 write_chksum(buf, size);
 printf("Writing magic %0x chksum %0x to offset %llu\n",
        ((__u32)(__le32)(x->x_magic)), ((__u32)(__le32)(x->x_chksum)),
        offset);
 if (lseek(fd, offset, 0) < 0)
  error(1, (*__errno_location ()), "Cannot seek");
 if (write(fd, buf, buf_size) < buf_size)
  error(1, (*__errno_location ()), "Cannot write metadata at offset %llu",
        (unsigned long long) offset);
}

variable_types:
metadata
void *

variable_types: size int

variable_types: fd int

variable_types:
offset
unsigned long long

variable_types:
buf
char [4096]

variable_types: struct chunkfs_chkmagic { unsigned int x_magic; unsigned int x_chksum; } *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
int v4; // eax
unsigned int v5; // ebp
__off_t v6; // rbx
int *v8; // rax
int *v9; // rax
_BYTE v10[96]; // [rsp+0h] [rbp-998h] BYREF
struct stat stat_buf; // [rsp+60h] [rbp-938h] BYREF
char v12[1056]; // [rsp+F0h] [rbp-8A8h] BYREF
__int64 v13[145]; // [rsp+510h] [rbp-488h] BYREF

memset(v12, 0, 0x418uLL);
memset(v13, 0, 0x448uLL);
memset(v10, 0, sizeof(v10));
cmd = (__int64)*argv;
if ( argc != 2 )
usage(&stat_buf, argv, envp);
v3 = (char *)argv[1];
v4 = open(v3, 2, envp);
v5 = v4;
if ( v4 < 0 )
{
LABEL_7:
v9 = __errno_location();
error(1, *v9, "Cannot open device %s", v3);
start();
}
if ( (unsigned int)fstat(v4, &stat_buf) )
{
v8 = __errno_location();
error(1, *v8, "Cannot stat device %s", v3);
goto LABEL_7;
}
v6 = stat_buf.st_size;
create_pool_summary(v3);
write_block(v12, 1048LL, v5, 0x8000LL);
create_dev_summary_constprop_0(v12, v13, v6 - 36865);
write_block(v13, 1096LL, v5, 36864LL);
write_chunk_summaries_isra_0(v13[4], v13[7], v10, v5);
close(v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
int v4; // eax
unsigned int v5; // ebp
__off_t v6; // rbx
int *v8; // rax
int *v9; // rax
_BYTE v10[96]; // [rsp+0h] [rbp-998h] BYREF
struct stat stat_buf; // [rsp+60h] [rbp-938h] BYREF
char v12[1056]; // [rsp+F0h] [rbp-8A8h] BYREF
__int64 v13[145]; // [rsp+510h] [rbp-488h] BYREF

memset(v12, 0, 0x418uLL);
memset(v13, 0, 0x448uLL);
memset(v10, 0, sizeof(v10));
cmd = (__int64)*argv;
if ( argc != 2 )
func7(&stat_buf, argv, envp);
v3 = (char *)argv[1];
v4 = open(v3, 2, envp);
v5 = v4;
if ( v4 < 0 )
{
LABEL_7:
v9 = __errno_location();
error(1, *v9, "Cannot open device %s", v3);
func1();
}
if ( (unsigned int)func16(v4, &stat_buf) )
{
v8 = __errno_location();
error(1, *v8, "Cannot stat device %s", v3);
goto LABEL_7;
}
v6 = stat_buf.st_size;
func8(v3);
func12(v12, 1048LL, v5, 0x8000LL);
func10(v12, v13, v6 - 36865);
func12(v13, 1096LL, v5, 36864LL);
func13(v13[4], v13[7], v10, v5);
close(v5);
return 0;
}

original_code:
int main (int argc, char * argv[])
{
 int fd;
 char * dev_name;
 struct stat stat_buf;
 off_t raw_dev_size;
 struct chunkfs_pool pool = { 0 };
 struct chunkfs_dev root_dev = { 0 };
 struct chunkfs_chunk root_chunk = { 0 };
 cmd = argv[0];
 if (argc != 2)
  usage();
 dev_name = argv[1];
 if ((fd = open(dev_name, 02)) < 0)
  error(1, (*__errno_location ()), "Cannot open device %s", dev_name);
 if (fstat(fd, &stat_buf) != 0)
  error(1, (*__errno_location ()), "Cannot stat device %s", dev_name);
 raw_dev_size = stat_buf.st_size;
 create_pool_summary(dev_name, &pool);
 write_block(&pool, sizeof(pool), fd, (8 * 4096));
 create_dev_summary(&pool, &root_dev, ((8 + 1) * 4096),
      raw_dev_size - ((8 + 1) * 4096) - 1);
 write_block(&root_dev, sizeof(root_dev), fd, ((8 + 1) * 4096));
 write_chunk_summaries(&root_dev, &root_chunk, fd);
 close(fd);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int fd;
 char *dev_name;
 struct stat stat_buf;
 long raw_dev_size;
 struct chunkfs_pool pool = { 0 };
 struct chunkfs_dev root_dev = { 0 };
 struct chunkfs_chunk root_chunk = { 0 };
 cmd = argv[0];
 if (argc != 2)
  usage();
 dev_name = argv[1];
 if ((fd = open(dev_name, 02)) < 0)
  error(1, (*__errno_location ()), "Cannot open device %s", dev_name);
 if (fstat(fd, &stat_buf) != 0)
  error(1, (*__errno_location ()), "Cannot stat device %s", dev_name);
 raw_dev_size = stat_buf.st_size;
 create_pool_summary(dev_name, &pool);
 write_block(&pool, sizeof(pool), fd, (8 * 4096));
 create_dev_summary(&pool, &root_dev, ((8 + 1) * 4096),
      raw_dev_size - ((8 + 1) * 4096) - 1);
 write_block(&root_dev, sizeof(root_dev), fd, ((8 + 1) * 4096));
 write_chunk_summaries(&root_dev, &root_chunk, fd);
 close(fd);
 return 0;
}

variable_types:
argv
char *[]

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

variable_types: struct chunkfs_pool { unsigned int p_magic; unsigned int p_chksum; unsigned long long p_flags; struct chunkfs_dev_desc { char d_hint[1024]; unsigned long long d_uuid; } p_root_desc; }

variable_types: struct chunkfs_dev { unsigned int d_magic; unsigned int d_chksum; unsigned long long d_flags; unsigned long long d_uuid; unsigned long long d_begin; unsigned long long d_end; unsigned long long d_innards_begin; unsigned long long d_innards_end; unsigned long long d_root_chunk; struct chunkfs_dev_desc { char d_hint[1024]; unsigned long long d_uuid; } d_next_dev; }

variable_types: struct chunkfs_chunk { unsigned int c_magic; unsigned int c_chksum; unsigned long long c_flags; unsigned long long c_chunk_id; unsigned long long c_begin; unsigned long long c_end; unsigned long long c_innards_begin; unsigned long long c_innards_end; unsigned long long c_next_chunk; char c_client_fs[32]; }

user_defined_types: struct chunkfs_dev { unsigned int d_magic; unsigned int d_chksum; unsigned long long d_flags; unsigned long long d_uuid; unsigned long long d_begin; unsigned long long d_end; unsigned long long d_innards_begin; unsigned long long d_innards_end; unsigned long long d_root_chunk; struct chunkfs_dev_desc d_next_dev; }


usage

decompiled_code:
void __noreturn usage()
{
fprintf(_bss_start, "Usage: %s <device>\n", (const char *)cmd);
_exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
fprintf(_bss_start, "Usage: %s <device>\n", (const char *)cmd);
_exit(1);
}

original_code:
static void usage (void)
{
 fprintf(stderr, "Usage: %s <device>\n", cmd);
 _exit(1);
}

canonical_original_code:
static void usage()
{
 fprintf(stderr, "Usage: %s <device>\n", cmd);
 _exit(1);
}


SigCatcher

decompiled_code:
void SigCatcher()
{
kill(readPid, 15);
kill(writePid, 15);
wait3(0LL, 1, 0LL);
signal(17, SigCatcher);
}

canonical_decompiled_code:
void func7()
{
kill(readPid, 15);
kill(writePid, 15);
wait3(0LL, 1, 0LL);
signal(17, SigCatcher);
}

original_code:
void
SigCatcher (int n)
{
 kill( readPid, 15 );
 kill( writePid, 15 );
 wait3 (((void *)0), 1, ((void *)0));
 signal (17, SigCatcher);
}

canonical_original_code:
void SigCatcher(int n)
{
 kill( readPid, 15 );
 kill( writePid, 15 );
 wait3 (((void *)0), 1, ((void *)0));
 signal (17, SigCatcher);
}

variable_types: n int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
int v5; // edi
socklen_t v6; // edx
int result; // eax
int v8; // eax
int v9; // ebp
__pid_t v10; // eax
socklen_t addr_len; // [rsp+8h] [rbp-40h] BYREF
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v14[2]; // [rsp+20h] [rbp-28h] BYREF

stat_loc = 0;
signal(17, SigCatcher);
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
puts("grrr, can't get the server socket");
result = 1;
}
else
{
v4 = v3;
*(_QWORD *)&v14[0].sa_data[6] = 0LL;
v14[0].sa_family = 2;
v5 = v3;
*(_QWORD *)&v14[0].sa_data[2] = (unsigned int)_bswap_32_constprop_0((unsigned int)v3, v14, 16LL);
*(_WORD *)v14[0].sa_data = _bswap_16_constprop_0();
if ( bind(v5, v14, v6) < 0 )
{
puts("grrr, can't bind server socket");
close(v4);
result = 2;
}
else if ( listen(v4, 5) >= 0 )
{
while ( 1 )
{
addr_len = 16;
v9 = accept(v4, &addr, &addr_len);
if ( v9 < 0 )
break;
readPid = fork();
v10 = fork();
writePid = v10;
if ( readPid )
{
if ( !v10 )
{
close(v4);
while ( 1 )
{
while ( feof(stdin) )
;
fgets(&buffer, 0x2000, stdin);
if ( write(v9, &buffer, 0x2000uLL) == -1 )
break;
printf("<<< %s", &buffer);
fflush(stdout);
}
}
}
else
{
close(v4);
while ( 1 )
{
v8 = read(v9, &buffer, 0x2000uLL);
if ( v8 <= 0 )
break;
printf(">>> %s", &buffer);
buffer = 0;
}
if ( v8 )
perror("There was an error with the connection.\n");
else
puts("The client closed the connection.");
}
close(v9);
waitpid(readPid, &stat_loc, 2);
waitpid(writePid, &stat_loc, 2);
}
puts("grrr, can't accept a packet from client");
result = 4;
}
else
{
puts("grrr, can't listen on socket");
close(v4);
result = 3;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
int v5; // edi
socklen_t v6; // edx
int result; // eax
int v8; // eax
int v9; // ebp
__pid_t v10; // eax
socklen_t addr_len; // [rsp+8h] [rbp-40h] BYREF
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v14[2]; // [rsp+20h] [rbp-28h] BYREF

stat_loc = 0;
signal(17, SigCatcher);
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
puts("grrr, can't get the server socket");
result = 1;
}
else
{
v4 = v3;
*(_QWORD *)&v14[0].sa_data[6] = 0LL;
v14[0].sa_family = 2;
v5 = v3;
*(_QWORD *)&v14[0].sa_data[2] = (unsigned int)func8((unsigned int)v3, v14, 16LL);
*(_WORD *)v14[0].sa_data = func9();
if ( bind(v5, v14, v6) < 0 )
{
puts("grrr, can't bind server socket");
close(v4);
result = 2;
}
else if ( listen(v4, 5) >= 0 )
{
while ( 1 )
{
addr_len = 16;
v9 = accept(v4, &addr, &addr_len);
if ( v9 < 0 )
break;
readPid = fork();
v10 = fork();
writePid = v10;
if ( readPid )
{
if ( !v10 )
{
close(v4);
while ( 1 )
{
while ( feof(stdin) )
;
fgets(&buffer, 0x2000, stdin);
if ( write(v9, &buffer, 0x2000uLL) == -1 )
break;
printf("<<< %s", &buffer);
fflush(stdout);
}
}
}
else
{
close(v4);
while ( 1 )
{
v8 = read(v9, &buffer, 0x2000uLL);
if ( v8 <= 0 )
break;
printf(">>> %s", &buffer);
buffer = 0;
}
if ( v8 )
perror("There was an error with the connection.\n");
else
puts("The client closed the connection.");
}
close(v9);
waitpid(readPid, &stat_loc, 2);
waitpid(writePid, &stat_loc, 2);
}
puts("grrr, can't accept a packet from client");
result = 4;
}
else
{
puts("grrr, can't listen on socket");
close(v4);
result = 3;
}
}
return result;
}

original_code:
int main (void) {
 int server_socket, client_socket;
 int client_len;
 struct sockaddr_in client_addr, server_addr;
 int bytesRead = 0;
 int status = 0;
 signal (17, SigCatcher);
 if ((server_socket = socket (2, SOCK_STREAM, 0)) < 0) {
  printf ("grrr, can't get the server socket\n");
  return 1;
 }
 memset (&server_addr, 0, sizeof (server_addr));
 server_addr.sin_family = 2;
 server_addr.sin_addr.s_addr = htonl (((in_addr_t) 0x00000000));
 server_addr.sin_port = htons (5000);
 if (bind (server_socket, (struct sockaddr *)&server_addr,
 sizeof (server_addr)) < 0) {
  printf ("grrr, can't bind server socket\n");
  close (server_socket);
  return 2;
 }
 if (listen (server_socket, 5) < 0) {
  printf ("grrr, can't listen on socket\n");
  close (server_socket);
  return 3;
 }
 while (1) {
    client_len = sizeof (client_addr);
  if ((client_socket = accept (server_socket,(struct sockaddr *)&client_addr, &client_len)) < 0) {
   printf ("grrr, can't accept a packet from client\n");
   return 4;
  }
  readPid = fork();
  writePid = fork();
  if ( readPid == 0 ) {
   close( server_socket );
   while (1) {
    if ( ( bytesRead = read( client_socket, buffer, 8192 ) ) <= 0 ) {
     if ( bytesRead == 0 ) {
      printf("The client closed the connection.\n");
     }
     else {
      perror("There was an error with the connection.\n");
     }
     close( client_socket );
     break;
    }
    else {
     printf(">>> %s", buffer);
    }
    strcpy( buffer, "" );
   }
  }
  else if ( writePid == 0 ) {
   close( server_socket );
   while (1) {
    if ( !feof( stdin ) ) {
     fgets( buffer, 8192, stdin );
     if ( write( client_socket, buffer, 8192 ) != -1 ) {
      printf( "<<< %s", buffer );
      fflush( stdout );
     }
     else {
      close( client_socket );
      break;
     }
    }
   }
  }
  else {
   close( client_socket );
  }
  waitpid(readPid, &status, 2);
  waitpid(writePid, &status, 2);
 }
 return 0;
}

canonical_original_code:
int main() {
 int server_socket; int client_socket;
 int client_len;
 struct sockaddr_in client_addr; struct sockaddr_in server_addr;
 int bytesRead = 0;
 int status = 0;
 signal (17, SigCatcher);
 if ((server_socket = socket (2, SOCK_STREAM, 0)) < 0) {
  printf ("grrr, can't get the server socket\n");
  return 1;
 }
 memset (&server_addr, 0, sizeof (server_addr));
 server_addr.sin_family = 2;
 server_addr.sin_addr.s_addr = htonl (((unsigned int) 0x00000000));
 server_addr.sin_port = htons (5000);
 if (bind (server_socket, (struct sockaddr *)&server_addr,
 sizeof (server_addr)) < 0) {
  printf ("grrr, can't bind server socket\n");
  close (server_socket);
  return 2;
 }
 if (listen (server_socket, 5) < 0) {
  printf ("grrr, can't listen on socket\n");
  close (server_socket);
  return 3;
 }
 while (1) {
    client_len = sizeof (client_addr);
  if ((client_socket = accept (server_socket,(struct sockaddr *)&client_addr, &client_len)) < 0) {
   printf ("grrr, can't accept a packet from client\n");
   return 4;
  }
  readPid = fork();
  writePid = fork();
  if ( readPid == 0 ) {
   close( server_socket );
   while (1) {
    if ( ( bytesRead = read( client_socket, buffer, 8192 ) ) <= 0 ) {
     if ( bytesRead == 0 ) {
      printf("The client closed the connection.\n");
     }
     else {
      perror("There was an error with the connection.\n");
     }
     close( client_socket );
     break;
    }
    else {
     printf(">>> %s", buffer);
    }
    strcpy( buffer, "" );
   }
  }
  else if ( writePid == 0 ) {
   close( server_socket );
   while (1) {
    if ( !feof( stdin ) ) {
     fgets( buffer, 8192, stdin );
     if ( write( client_socket, buffer, 8192 ) != -1 ) {
      printf( "<<< %s", buffer );
      fflush( stdout );
     }
     else {
      close( client_socket );
      break;
     }
    }
   }
  }
  else {
   close( client_socket );
  }
  waitpid(readPid, &status, 2);
  waitpid(writePid, &status, 2);
 }
 return 0;
}

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

variable_types: bytesRead int

variable_types: status int

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

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


handler

decompiled_code:
void __fastcall handler(int a1)
{
if ( a1 == 2 )
keep_playing = 0;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 2 )
keep_playing = 0;
}

original_code:
void handler(int signo) {
    switch(signo) {
        case 2:
            keep_playing = 0;
            break;
    }
}

variable_types: signo int


game

decompiled_code:
__int64 __fastcall game(int a1, int a2)
{
void *v2; // rsp
int v3; // er14
int v4; // ebx
int v5; // er15
__int64 v6; // r12
__int64 result; // rax
unsigned int v8; // er14
unsigned int v9; // ebx
_BYTE v10[4]; // [rsp+0h] [rbp-50h] BYREF
unsigned int v11; // [rsp+4h] [rbp-4Ch]
unsigned __int64 v12; // [rsp+8h] [rbp-48h]
__int64 v13; // [rsp+10h] [rbp-40h]
int v14; // [rsp+18h] [rbp-38h]
unsigned int v15; // [rsp+1Ch] [rbp-34h]

v11 = a1;
v15 = a2;
v2 = alloca(4 * a1 * (__int64)a2);
if ( a1 <= 0 )
goto LABEL_11;
v14 = 0;
v12 = (unsigned __int64)(4LL * a1) >> 2;
do
{
v3 = 0;
v13 = v14;
if ( (int)v15 > 0 )
{
do
{
v4 = rand() % 200000;
if ( v4 > 0 )
{
v5 = 0;
v6 = v13 + v12 * v3;
do
{
++v5;
*(_DWORD *)&v10[4 * v6] = rand() <= 214748363;
}
while ( v4 != v5 );
}
++v3;
}
while ( v15 != v3 );
}
++v14;
}
while ( v11 != v14 );
for ( result = (unsigned int)keep_playing; keep_playing; result = (unsigned int)keep_playing )
{
v8 = v15;
v9 = v11;
show(v10, v11, v15);
evolve(v10, v9, v8);
usleep(0x30D40u);
LABEL_11:
;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
void *v2; // rsp
int v3; // er14
int v4; // ebx
int v5; // er15
__int64 v6; // r12
__int64 result; // rax
unsigned int v8; // er14
unsigned int v9; // ebx
_BYTE v10[4]; // [rsp+0h] [rbp-50h] BYREF
unsigned int v11; // [rsp+4h] [rbp-4Ch]
unsigned __int64 v12; // [rsp+8h] [rbp-48h]
__int64 v13; // [rsp+10h] [rbp-40h]
int v14; // [rsp+18h] [rbp-38h]
unsigned int v15; // [rsp+1Ch] [rbp-34h]

v11 = a1;
v15 = a2;
v2 = alloca(4 * a1 * (__int64)a2);
if ( a1 <= 0 )
goto LABEL_11;
v14 = 0;
v12 = (unsigned __int64)(4LL * a1) >> 2;
do
{
v3 = 0;
v13 = v14;
if ( (int)v15 > 0 )
{
do
{
v4 = rand() % 200000;
if ( v4 > 0 )
{
v5 = 0;
v6 = v13 + v12 * v3;
do
{
++v5;
*(_DWORD *)&v10[4 * v6] = rand() <= 214748363;
}
while ( v4 != v5 );
}
++v3;
}
while ( v15 != v3 );
}
++v14;
}
while ( v11 != v14 );
for ( result = (unsigned int)keep_playing; keep_playing; result = (unsigned int)keep_playing )
{
v8 = v15;
v9 = v11;
show(v10, v11, v15);
evolve(v10, v9, v8);
usleep(0x30D40u);
LABEL_11:
;
}
return result;
}

original_code:
void
game(int w, int h)
{
  int x, y, rounds, round;
  unsigned univ[h][w];
  for (x = 0; x < w; x++) {
    for (y = 0; y < h; y++) {
      rounds = rand() % 200000;
      for (round = 0; round < rounds; round++) {
        univ[y][x] = rand() < 2147483647 / 10 ? 1 : 0;
      }
    }
  }
  while (keep_playing) {
    show(univ, w, h);
    evolve(univ, w, h);
    usleep(200000);
  }
}

canonical_original_code:
void game(int w, int h)
{
  int x; int y; int rounds; int round;
  unsigned int univ[-1][-1];
  for (x = 0; x < w; x++) {
    for (y = 0; y < h; y++) {
      rounds = rand() % 200000;
      for (round = 0; round < rounds; round++) {
        univ[y][x] = rand() < 2147483647 / 10 ? 1 : 0;
      }
    }
  }
  while (keep_playing) {
    show(univ, w, h);
    evolve(univ, w, h);
    usleep(200000);
  }
}

variable_types: w int

variable_types: h int

variable_types: x int

variable_types: y int

variable_types: rounds int

variable_types: round int

variable_types:
univ
unsigned int [-1][-1]


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // eax
int v5; // er13
int v6; // esi
struct sigaction v7; // [rsp+0h] [rbp-C0h] BYREF

v7.sa_handler = handler;
memset(&v7.sa_mask, 0, 0x90uLL);
sigaction(2, &v7, 0LL);
v3 = time(0LL);
srand(v3);
if ( argc > 1 )
{
v4 = atoi(argv[1]);
v5 = v4;
if ( argc != 2 )
{
v6 = atoi(argv[2]);
if ( v5 <= 0 )
v5 = 40;
if ( v6 > 0 )
{
LABEL_6:
game(v5, v6);
exit(0);
}
LABEL_9:
v6 = 40;
goto LABEL_6;
}
if ( v4 > 0 )
goto LABEL_9;
}
v5 = 40;
goto LABEL_9;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // eax
int v5; // er13
int v6; // esi
struct sigaction v7; // [rsp+0h] [rbp-C0h] BYREF

v7.sa_handler = handler;
memset(&v7.sa_mask, 0, 0x90uLL);
sigaction(2, &v7, 0LL);
v3 = time(0LL);
srand(v3);
if ( argc > 1 )
{
v4 = atoi(argv[1]);
v5 = v4;
if ( argc != 2 )
{
v6 = atoi(argv[2]);
if ( v5 <= 0 )
v5 = 40;
if ( v6 > 0 )
{
LABEL_6:
func1(v5, v6);
exit(0);
}
LABEL_9:
v6 = 40;
goto LABEL_6;
}
if ( v4 > 0 )
goto LABEL_9;
}
v5 = 40;
goto LABEL_9;
}

original_code:
int
main(int argc, char **argv)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.__sigaction_handler.sa_handler = &handler;
  sigaction(2, &sa, ((void *)0));
  srand(time(((void *)0)));
  int w = 0, h = 0;
  if (argc > 1) w = atoi(argv[1]);
  if (argc > 2) h = atoi(argv[2]);
  if (w <= 0) w = 40;
  if (h <= 0) h = 40;
  game(w, h);
  exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.__sigaction_handler.sa_handler = &handler;
  sigaction(2, &sa, ((void *)0));
  srand(time(((void *)0)));
  int w = 0; int h = 0;
  if (argc > 1) w = atoi(argv[1]);
  if (argc > 2) h = atoi(argv[2]);
  if (w <= 0) w = 40;
  if (h <= 0) h = 40;
  game(w, h);
  exit(0);
}

variable_types: argc int

variable_types:
argv
char **

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

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


convertFirstArgToCommandName

decompiled_code:
void __fastcall convertFirstArgToCommandName(__int64 a1)
{
char *v1; // r12
size_t v2; // rax
size_t v3; // rdx
int v4; // ebx
size_t v5; // r13
void *v6; // r14

v1 = *(char **)(a1 + 16);
v2 = strlen(v1);
if ( (v2 & 0x80000000) == 0LL )
{
v3 = v2;
v2 = (int)v2;
while ( 1 )
{
v4 = v2;
if ( v1[v2] == 47 )
break;
if ( (_DWORD)--v2 == -1 )
return;
}
v5 = v3 + 1 - v2;
v6 = malloc(v5);
memcpy(v6, &v1[v4 + 1], v5);
free(v1);
*(_QWORD *)(a1 + 16) = v6;
}
}

original_code:
void convertFirstArgToCommandName(commandT* cmd) {
 char* command = cmd->argv[0];
 int starting = -1;
 int i;
 for (i = strlen(command); i >= 0; --i) {
  if (command[i] == '/') {
   starting = i;
   break;
  }
 }
 if (starting != -1) {
  char* commandName = malloc((strlen(command) - starting + 1) * sizeof(char));
  memcpy(commandName, command + (starting + 1) * sizeof(char), (strlen(command) - starting + 1) * sizeof(char));
  free(cmd->argv[0]);
  cmd->argv[0] = commandName;
 }
}

canonical_original_code:
void convertFirstArgToCommandName(struct command_t *cmd) {
 char *command = cmd->argv[0];
 int starting = -1;
 int i;
 for (i = strlen(command); i >= 0; --i) {
  if (command[i] == '/') {
   starting = i;
   break;
  }
 }
 if (starting != -1) {
  char *commandName = malloc((strlen(command) - starting + 1) * sizeof(char));
  memcpy(commandName, command + (starting + 1) * sizeof(char), (strlen(command) - starting + 1) * sizeof(char));
  free(cmd->argv[0]);
  cmd->argv[0] = commandName;
 }
}

variable_types: struct command_t { char *name; int argc; char *argv[]; } *

variable_types:
command
char *

variable_types: starting int

variable_types: i int

variable_types:
commandName
char *


getCurrentWorkingDir

decompiled_code:
char *getCurrentWorkingDir()
{
char *v0; // rax

v0 = (char *)malloc(0x8000uLL);
return getcwd(v0, 0x1000uLL);
}

canonical_decompiled_code:
char *func11()
{
char *v0; // rax

v0 = (char *)malloc(0x8000uLL);
return getcwd(v0, 0x1000uLL);
}

original_code:
char*
getCurrentWorkingDir() {
 char* path = malloc(4096*sizeof(char*));
 return getcwd(path, 4096);
}

canonical_original_code:
char *getCurrentWorkingDir() {
 char *path = malloc(4096*sizeof(char*));
 return getcwd(path, 4096);
}

variable_types:
path
char *

return_type: char *


getFullPath

decompiled_code:
char *__fastcall getFullPath(char *src)
{
char *v1; // rbp
char *v2; // r13
char *v3; // r15
char *v4; // r14
char *v5; // rax
char *v7; // rbx
char *v8; // r15
char *v9; // rax
char *v10; // rbx
char *v11; // rbp
char *v12; // rax
char *v13; // rdi
__int16 v14; // [rsp+Eh] [rbp-4Ah]
char *v15; // [rsp+10h] [rbp-48h]
char *ptr; // [rsp+18h] [rbp-40h]

v1 = getenv("PATH");
v2 = (char *)malloc(0x8000uLL);
if ( *src == 47 )
{
if ( (unsigned int)fileExists(src) )
{
strcpy(v2, src);
return v2;
}
LABEL_14:
*(_QWORD *)v2 = 0x203A3120656E696CLL;
strcpy(v2 + 8, src);
PrintPError(v2);
v13 = v2;
v2 = 0LL;
free(v13);
return v2;
}
v3 = getenv("HOME");
v4 = (char *)malloc(0x8000uLL);
v5 = stpcpy(v4, v3);
*v5 = 47;
strcpy(v5 + 1, src);
if ( (unsigned int)fileExists(v4) )
{
strcpy(v2, v4);
free(v4);
return v2;
}
v7 = getCurrentWorkingDir();
v15 = v7;
v8 = (char *)malloc(0x8000uLL);
v9 = stpcpy(v8, v7);
*v9 = 47;
strcpy(v9 + 1, src);
if ( (unsigned int)fileExists(v8) )
{
strcpy(v2, v8);
free(v8);
free(v7);
free(v4);
return v2;
}
ptr = (char *)malloc(0x8000uLL);
strcpy(ptr, v1);
v10 = strtok(ptr, ":");
if ( !v10 )
{
free(ptr);
free(v8);
free(v15);
free(v4);
goto LABEL_14;
}
v14 = 0;
do
{
v11 = (char *)malloc(0x8000uLL);
v12 = stpcpy(v11, v10);
*v12 = 47;
strcpy(v12 + 1, src);
if ( (unsigned int)fileExists(v11) )
{
strcpy(v2, v11);
v14 = 1;
}
v10 = strtok(0LL, ":");
free(v11);
}
while ( v10 );
free(ptr);
free(v8);
free(v15);
free(v4);
if ( !v14 )
goto LABEL_14;
return v2;
}

canonical_decompiled_code:
char *__fastcall func12(char *src)
{
char *v1; // rbp
char *v2; // r13
char *v3; // r15
char *v4; // r14
char *v5; // rax
char *v7; // rbx
char *v8; // r15
char *v9; // rax
char *v10; // rbx
char *v11; // rbp
char *v12; // rax
char *v13; // rdi
__int16 v14; // [rsp+Eh] [rbp-4Ah]
char *v15; // [rsp+10h] [rbp-48h]
char *ptr; // [rsp+18h] [rbp-40h]

v1 = getenv("PATH");
v2 = (char *)malloc(0x8000uLL);
if ( *src == 47 )
{
if ( (unsigned int)func2(src) )
{
strcpy(v2, src);
return v2;
}
LABEL_14:
*(_QWORD *)v2 = 0x203A3120656E696CLL;
strcpy(v2 + 8, src);
PrintPError(v2);
v13 = v2;
v2 = 0LL;
free(v13);
return v2;
}
v3 = getenv("HOME");
v4 = (char *)malloc(0x8000uLL);
v5 = stpcpy(v4, v3);
*v5 = 47;
strcpy(v5 + 1, src);
if ( (unsigned int)func2(v4) )
{
strcpy(v2, v4);
free(v4);
return v2;
}
v7 = func11();
v15 = v7;
v8 = (char *)malloc(0x8000uLL);
v9 = stpcpy(v8, v7);
*v9 = 47;
strcpy(v9 + 1, src);
if ( (unsigned int)func2(v8) )
{
strcpy(v2, v8);
free(v8);
free(v7);
free(v4);
return v2;
}
ptr = (char *)malloc(0x8000uLL);
strcpy(ptr, v1);
v10 = strtok(ptr, ":");
if ( !v10 )
{
free(ptr);
free(v8);
free(v15);
free(v4);
goto LABEL_14;
}
v14 = 0;
do
{
v11 = (char *)malloc(0x8000uLL);
v12 = stpcpy(v11, v10);
*v12 = 47;
strcpy(v12 + 1, src);
if ( (unsigned int)func2(v11) )
{
strcpy(v2, v11);
v14 = 1;
}
v10 = strtok(0LL, ":");
free(v11);
}
while ( v10 );
free(ptr);
free(v8);
free(v15);
free(v4);
if ( !v14 )
goto LABEL_14;
return v2;
}

original_code:
char* getFullPath(char* filename) {
 char* paths = getenv("PATH");
 char* result = malloc(4096*sizeof(char*));
 short found = 0;
 if (filename[0] == '/') {
  if (fileExists(filename)) {
   strcpy(result, filename);
   found = 1;
  }
 } else {
  char* home = getenv("HOME");
  char* homePath = malloc(4096*sizeof(char*));
  strcpy(homePath, home);
  strcat(homePath, "/");
  strcat(homePath, filename);
  if (fileExists(homePath)) {
   strcpy(result, homePath);
   found = 1;
  } else {
   char* workingDir = getCurrentWorkingDir();
   char* fullWorkingDir = malloc(4096*sizeof(char*));
   strcpy(fullWorkingDir, workingDir);
   strcat(fullWorkingDir, "/");
   strcat(fullWorkingDir, filename);
   if (fileExists(fullWorkingDir)) {
    strcpy(result, fullWorkingDir);
    found = 1;
   } else {
    char* pathCopy = malloc(4096*sizeof(char*));
    strcpy(pathCopy, paths);
    char* path = strtok(pathCopy, ":");
    while (path != ((void *)0)) {
     char* fullPath = malloc(4096*sizeof(char*));
     strcpy(fullPath, path);
     strcat(fullPath, "/");
     strcat(fullPath, filename);
     if (fileExists(fullPath)) {
      strcpy(result, fullPath);
      found = 1;
     }
     path = strtok(((void *)0), ":");
     free(fullPath);
    }
    free(pathCopy);
   }
   free(fullWorkingDir);
   free(workingDir);
  }
  free(homePath);
 }
 if (found) {
  return result;
 } else {
  strcpy(result, "line 1: ");
  strcat(result, filename);
  PrintPError(result);
  free(result);
  return ((void *)0);
 }
}

canonical_original_code:
char *getFullPath(char *filename) {
 char *paths = getenv("PATH");
 char *result = malloc(4096*sizeof(char*));
 short found = 0;
 if (filename[0] == '/') {
  if (fileExists(filename)) {
   strcpy(result, filename);
   found = 1;
  }
 } else {
  char *home = getenv("HOME");
  char *homePath = malloc(4096*sizeof(char*));
  strcpy(homePath, home);
  strcat(homePath, "/");
  strcat(homePath, filename);
  if (fileExists(homePath)) {
   strcpy(result, homePath);
   found = 1;
  } else {
   char *workingDir = getCurrentWorkingDir();
   char *fullWorkingDir = malloc(4096*sizeof(char*));
   strcpy(fullWorkingDir, workingDir);
   strcat(fullWorkingDir, "/");
   strcat(fullWorkingDir, filename);
   if (fileExists(fullWorkingDir)) {
    strcpy(result, fullWorkingDir);
    found = 1;
   } else {
    char *pathCopy = malloc(4096*sizeof(char*));
    strcpy(pathCopy, paths);
    char *path = strtok(pathCopy, ":");
    while (path != ((void *)0)) {
     char *fullPath = malloc(4096*sizeof(char*));
     strcpy(fullPath, path);
     strcat(fullPath, "/");
     strcat(fullPath, filename);
     if (fileExists(fullPath)) {
      strcpy(result, fullPath);
      found = 1;
     }
     path = strtok(((void *)0), ":");
     free(fullPath);
    }
    free(pathCopy);
   }
   free(fullWorkingDir);
   free(workingDir);
  }
  free(homePath);
 }
 if (found) {
  return result;
 } else {
  strcpy(result, "line 1: ");
  strcat(result, filename);
  PrintPError(result);
  free(result);
  return ((void *)0);
 }
}

variable_types:
filename
char *

variable_types:
paths
char *

variable_types:
result
char *

variable_types: found short

variable_types:
home
char *

variable_types:
homePath
char *

variable_types:
workingDir
char *

variable_types:
fullWorkingDir
char *

variable_types:
pathCopy
char *

variable_types:
fullPath
char *


ResolveExternalCmd

decompiled_code:
__int64 __fastcall ResolveExternalCmd(char **a1)
{
char *v1; // rax
unsigned int v2; // er8

v1 = getFullPath(*a1);
v2 = 0;
if ( v1 )
{
*a1 = v1;
v2 = 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func13(char **a1)
{
char *v1; // rax
unsigned int v2; // er8

v1 = func12(*a1);
v2 = 0;
if ( v1 )
{
*a1 = v1;
v2 = 1;
}
return v2;
}

original_code:
static short
ResolveExternalCmd(commandT* cmd) {
 char* fullPath = getFullPath(cmd->name);
 if (fullPath != ((void *)0)) {
  cmd->name = fullPath;
  return 1;
 }
 free(fullPath);
 return 0;
}

canonical_original_code:
static short ResolveExternalCmd(struct command_t *cmd) {
 char *fullPath = getFullPath(cmd->name);
 if (fullPath != ((void *)0)) {
  cmd->name = fullPath;
  return 1;
 }
 free(fullPath);
 return 0;
}

return_type: short

user_defined_types: struct command_t { char *name; int argc; char *argv[]; }


RunExternalCmd

decompiled_code:
void __fastcall RunExternalCmd(char **a1, __int16 a2)
{
if ( (unsigned __int16)ResolveExternalCmd(a1) )
Exec((__int64)a1, a2);
}

canonical_decompiled_code:
void __fastcall func14(char **a1, __int16 a2)
{
if ( (unsigned __int16)func13(a1) )
func9((__int64)a1, a2);
}

original_code:
static void
RunExternalCmd(commandT* cmd, short fork) {
 if (ResolveExternalCmd(cmd))
  Exec(cmd, fork);
}

canonical_original_code:
static void RunExternalCmd(struct command_t *cmd, short fork) {
 if (ResolveExternalCmd(cmd))
  Exec(cmd, fork);
}

variable_types: fork short


PrintNewline

decompiled_code:
int PrintNewline()
{
return putchar(10);
}

canonical_decompiled_code:
int func0()
{
return putchar(10);
}

original_code:
void
PrintNewline()
{
 putchar('\n');
}

canonical_original_code:
void PrintNewline()
{
 putchar('\n');
}


Print

decompiled_code:
int __fastcall Print(const char *a1)
{
if ( !a1 )
_assert_fail("msg != NULL", "io.c", 0x64u, "Print");
return puts(a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
if ( !a1 )
_assert_fail("msg != NULL", "io.c", 0x64u, "Print");
return puts(a1);
}

original_code:
void
Print(char* msg)
{
 ((void) sizeof ((msg != ((void *)0)) ? 1 : 0), __extension__ ({ if (msg != ((void *)0)) ; else __assert_fail ("msg != NULL", "io.c", 100, __extension__ __PRETTY_FUNCTION__); }));
 puts(msg);
}

canonical_original_code:
void Print(char *msg)
{
 ((void) sizeof ((msg != ((void *)0)) ? 1 : 0), __extension__ ({ if (msg != ((void *)0)) ; else __assert_fail ("msg != NULL", "io.c", 100, __extension__ __PRETTY_FUNCTION__); }));
 puts(msg);
}

variable_types:
msg
char *


PrintPError

decompiled_code:
void __fastcall PrintPError(const char *a1)
{
char v1[136]; // [rsp-88h] [rbp-88h] BYREF

if ( a1 )
{
snprintf(v1, 0x77uLL, "%s: %s", "tsh", a1);
perror(v1);
}
else
{
perror("tsh");
}
}

canonical_decompiled_code:
void __fastcall func2(const char *a1)
{
char v1[136]; // [rsp-88h] [rbp-88h] BYREF

if ( a1 )
{
snprintf(v1, 0x77uLL, "%s: %s", "tsh", a1);
perror(v1);
}
else
{
perror("tsh");
}
}

original_code:
void
PrintPError(char* msg)
{
 char* format = "%s: %s";
 char str[120];
 if (msg == ((void *)0))
  {
   perror("tsh");
   return;
  }
 snprintf(str, 120 - 1, format, "tsh", msg);
 perror(str);
}

canonical_original_code:
void PrintPError(char *msg)
{
 char *format = "%s: %s";
 char str[120];
 if (msg == ((void *)0))
  {
   perror("tsh");
   return;
  }
 snprintf(str, 120 - 1, format, "tsh", msg);
 perror(str);
}

variable_types:
format
char *

variable_types:
str
char [120]


IsReading

decompiled_code:
__int64 IsReading()
{
return (unsigned __int16)isReading;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned __int16)isReading;
}

original_code:
short
IsReading()
{
 return isReading;
}

canonical_original_code:
short IsReading()
{
 return isReading;
}


getCommandLine

decompiled_code:
__int64 __fastcall getCommandLine(_BYTE **a1, int a2)
{
__int64 v3; // rbp
_BYTE *v4; // r12
char v5; // al
char v6; // bl
__int64 result; // rax

v3 = 0LL;
v4 = *a1;
**a1 = 0;
isReading = 1;
while ( 1 )
{
v5 = getc(stdin);
v6 = v5;
if ( v5 == -1 || v5 == 10 )
break;
if ( a2 == v3 )
{
a2 *= 2;
v4 = realloc(v4, a2 + 1);
}
v4[v3++] = v6;
v4[v3] = 0;
}
result = 0LL;
isReading = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE **a1, int a2)
{
__int64 v3; // rbp
_BYTE *v4; // r12
char v5; // al
char v6; // bl
__int64 result; // rax

v3 = 0LL;
v4 = *a1;
**a1 = 0;
isReading = 1;
while ( 1 )
{
v5 = getc(stdin);
v6 = v5;
if ( v5 == -1 || v5 == 10 )
break;
if ( a2 == v3 )
{
a2 *= 2;
v4 = realloc(v4, a2 + 1);
}
v4[v3++] = v6;
v4[v3] = 0;
}
result = 0LL;
isReading = 0;
return result;
}

original_code:
void
getCommandLine(char** buf, int size)
{
 char ch;
 size_t used = 0;
 char* cmd = *buf;
 cmd[0] = '\0';
 isReading = 1;
 while (((ch = getc(stdin)) != (-1)) && (ch != '\n'))
  {
   if (used == size)
    {
     size *= 2;
     cmd = realloc(cmd, sizeof(char) * (size + 1));
    }
   cmd[used] = ch;
   used++;
   cmd[used] = '\0';
  }
 isReading = 0;
}

canonical_original_code:
void getCommandLine(char **buf, int size)
{
 char ch;
 unsigned long used = 0;
 char *cmd = *buf;
 cmd[0] = '\0';
 isReading = 1;
 while (((ch = getc(stdin)) != (-1)) && (ch != '\n'))
  {
   if (used == size)
    {
     size *= 2;
     cmd = realloc(cmd, sizeof(char) * (size + 1));
    }
   cmd[used] = ch;
   used++;
   cmd[used] = '\0';
  }
 isReading = 0;
}

variable_types:
buf
char **

variable_types: size int

variable_types: ch char

variable_types:
used
unsigned long

variable_types:
cmd
char *


show

original_code:
void
show(void *u, int w, int h)
{
  int x, y;
  int (*univ)[w] = u;
  printf("\033[H");
  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++)
      printf(univ[y][x] ? "\033[44m  \033[m" : "  ");
    printf("\033[E");
  }
  fflush(stdout);
}

canonical_original_code:
void show(void *u, int w, int h)
{
  int x; int y;
  int (*univ)[-1] = u;
  printf("\033[H");
  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++)
      printf(univ[y][x] ? "\033[44m  \033[m" : "  ");
    printf("\033[E");
  }
  fflush(stdout);
}

variable_types:
u
void *

variable_types: w int

variable_types: h int

variable_types: x int

variable_types: y int

variable_types:
univ
int (*)[-1]


freeCommand

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

v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
{
v3 = (void **)((char *)ptr + 16);
do
{
free(v2);
*v3++ = 0LL;
v2 = *v3;
}
while ( *v3 );
}
free(ptr);
}

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

v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
{
v3 = (void **)((char *)ptr + 16);
do
{
free(v2);
*v3++ = 0LL;
v2 = *v3;
}
while ( *v3 );
}
free(ptr);
}

original_code:
void
freeCommand(commandT* cmd)
{
 int i;
 cmd->name = 0;
 for (i = 0; cmd->argv[i] != 0; i++)
  {
   free(cmd->argv[i]);
   cmd->argv[i] = 0;
  }
 free(cmd);
}

canonical_original_code:
void freeCommand(struct command_t *cmd)
{
 int i;
 cmd->name = 0;
 for (i = 0; cmd->argv[i] != 0; i++)
  {
   free(cmd->argv[i]);
   cmd->argv[i] = 0;
  }
 free(cmd);
}

variable_types: struct command_t { char *name; int argc; char *argv[]; } *

variable_types: i int


Interpret

decompiled_code:
void __fastcall Interpret(char *a1)
{
_QWORD *v1; // rbp

v1 = getCommand(a1);
RunCmd(v1);
fflush(stdout);
freeCommand(v1);
}

canonical_decompiled_code:
void __fastcall func1(char *a1)
{
_QWORD *v1; // rbp

v1 = getCommand(a1);
RunCmd(v1);
fflush(stdout);
func0(v1);
}

original_code:
void
Interpret(char* cmdLine)
{
 commandT* cmd = getCommand(cmdLine);
 RunCmd(cmd);
 fflush(stdout);
 freeCommand(cmd);
}

canonical_original_code:
void Interpret(char *cmdLine)
{
 struct command_t *cmd = getCommand(cmdLine);
 RunCmd(cmd);
 fflush(stdout);
 freeCommand(cmd);
}

variable_types:
cmdLine
char *


repo: b/libdtw

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

*(_DWORD *)(a1 + 12) = a2;
*(_DWORD *)(a1 + 8) = 0;
result = malloc(4LL * a2);
*(_DWORD *)(a1 + 20) = a2 - 1;
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 16) = 0;
return result;
}

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

*(_DWORD *)(a1 + 12) = a2;
*(_DWORD *)(a1 + 8) = 0;
result = malloc(4LL * a2);
*(_DWORD *)(a1 + 20) = a2 - 1;
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 16) = 0;
return result;
}

original_code:
void deq_new(struct deque *d, int capacity)
{
    d->capacity = capacity;
    d->size = 0;
    d->dq = (int *) malloc(sizeof(int)*d->capacity);
    d->f = 0;
    d->r = d->capacity-1;
}

variable_types: struct deque { int *dq; int size; int f; } *

variable_types: capacity int


decompiled_code:
void __fastcall deq_free(void **a1)
{
free(*a1);
}

canonical_decompiled_code:
void __fastcall func1(void **a1)
{
free(*a1);
}

original_code:
void deq_free(struct deque *d)
{
    free(d->dq);
}

user_defined_types: struct deque { int *dq; int size; int f; }


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

result = *a1;
*(_DWORD *)(*a1 + 4LL * *((int *)a1 + 5)) = a2;
if ( --*((_DWORD *)a1 + 5) < 0 )
{
result = (unsigned int)(*((_DWORD *)a1 + 3) - 1);
*((_DWORD *)a1 + 5) = result;
}
++*((_DWORD *)a1 + 2);
return result;
}

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

result = *a1;
*(_DWORD *)(*a1 + 4LL * *((int *)a1 + 5)) = a2;
if ( --*((_DWORD *)a1 + 5) < 0 )
{
result = (unsigned int)(*((_DWORD *)a1 + 3) - 1);
*((_DWORD *)a1 + 5) = result;
}
++*((_DWORD *)a1 + 2);
return result;
}

original_code:
void deq_push_back(struct deque *d, int v)
{
    d->dq[d->r] = v;
    d->r--;
    if (d->r < 0)
        d->r = d->capacity - 1;
    d->size++;
}

variable_types: v int


original_code:
int deq_front(struct deque *d)
{
    int aux = d->f - 1;
    if (aux < 0)
        aux = d->capacity - 1;
    return d->dq[aux];
}

variable_types: aux int


original_code:
int deq_back(struct deque *d)
{
    int aux = (d->r + 1) % d->capacity;
    return d->dq[aux];
}


decompiled_code:
_BOOL8 __fastcall deq_empty(__int64 a1)
{
return *(_DWORD *)(a1 + 8) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(__int64 a1)
{
return *(_DWORD *)(a1 + 8) == 0;
}

original_code:
int deq_empty(struct deque *d)
{
    return d->size == 0;
}


display

decompiled_code:
__int64 display()
{
glLoadIdentity();
glScalef(0.25, 0.25);
glClear(16640LL);
drawPoints();
glFlush();
return glutSwapBuffers();
}

canonical_decompiled_code:
__int64 func0()
{
glLoadIdentity();
glScalef(0.25, 0.25);
glClear(16640LL);
drawPoints();
glFlush();
return glutSwapBuffers();
}

original_code:
void display(void)
{
    glLoadIdentity();
 glScalef( 0.25f, 0.25f, 0.25f );
    glClear(0x00004000 | 0x00000100);
    drawPoints();
    glFlush();
    glutSwapBuffers();
}

canonical_original_code:
void display()
{
    glLoadIdentity();
 glScalef( 0.25f, 0.25f, 0.25f );
    glClear(0x00004000 | 0x00000100);
    drawPoints();
    glFlush();
    glutSwapBuffers();
}


key

decompiled_code:
void __fastcall key(char a1)
{
if ( a1 == 113 )
exit(0);
}

canonical_decompiled_code:
void __fastcall func1(char a1)
{
if ( a1 == 113 )
exit(0);
}

original_code:
void key( unsigned char keyPressed, int x, int y )
{
    switch( keyPressed )
    {
        case 'q':
            exit( 0 );
            break;
        default:
            break;
    }
}

canonical_original_code:
void key(unsigned char keyPressed, int x, int y)
{
    switch( keyPressed )
    {
        case 'q':
            exit( 0 );
            break;
        default:
            break;
    }
}

variable_types:
keyPressed
unsigned char