decompiled_code:
double __fastcall t1_zeta(int a1, int a2, double a3)
{
int v3; // ebx
double v4; // r15
double v5; // r13
double v6; // r12
double v7; // xmm0_8
double v10; // [rsp+8h] [rbp-40h]

if ( natoms <= 0 )
return *(double *)&LC1;
v3 = 0;
v4 = *(double *)&LC1;
do
{
if ( a1 != v3 && a2 != v3 )
{
v5 = t1_distance(a1, v3);
v6 = t1_f_cutoff(v5);
if ( v6 > 0.000001 )
{
v10 = t1_g(a1, a2, v3);
v7 = pow(a3 - v5, 3.0);
v4 = v4 + v6 * v10 * exp(v7 * 2.330419169512);
}
}
++v3;
}
while ( natoms > v3 );
return v4;
}

original_code:
inline double t1_zeta(int i, int j, double rij)
{
    double rik, f_cutoff_val, sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t1_distance(i, k);
            f_cutoff_val = t1_f_cutoff(rik);
            if (f_cutoff_val > 1e-6) {
                sum += f_cutoff_val * t1_g(i, j, k) * exp(pow(1.3258, 3) *
                                                      pow(rij - rik, 3));
            }
        }
    }
    return sum;
}

canonical_original_code:
inline double t1_zeta(int i, int j, double rij)
{
    double rik; double f_cutoff_val; double sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t1_distance(i, k);
            f_cutoff_val = t1_f_cutoff(rik);
            if (f_cutoff_val > 1e-6) {
                sum += f_cutoff_val * t1_g(i, j, k) * exp(pow(1.3258, 3) *
                                                      pow(rij - rik, 3));
            }
        }
    }
    return sum;
}


repo: apaznikov/qdmd

decompiled_code:
void tersoff1_forces()
{
__int64 v0; // rax
int v1; // er13
int v2; // ebp
double v3; // rbx
double v4; // xmm0_8
int v5; // er12

if ( natoms > 0 )
{
v0 = 0LL;
do
{
*(_QWORD *)(qword_860 + 8 * v0) = 0LL;
*(_QWORD *)(ay + 8 * v0) = 0LL;
*(_QWORD *)(az + 8 * v0++) = 0LL;
}
while ( natoms > (int)v0 );
}
if ( natoms > 1 )
{
v1 = 0;
do
{
v5 = v1++;
if ( v1 < natoms )
{
v2 = v1;
do
{
v3 = t1_distance(v5, v2);
v4 = t1_f_cutoff(v3);
if ( !t1_iszero(v4) )
{
t1_a(v5, v2, v3);
t1_f_repulsive(v3);
t1_b(v5, v2, v3);
t1_f_attractive(v3);
}
++v2;
}
while ( natoms > v2 );
}
}
while ( natoms - 1 > v1 );
}
}

canonical_decompiled_code:
void func11()
{
__int64 v0; // rax
int v1; // er13
int v2; // ebp
double v3; // rbx
double v4; // xmm0_8
int v5; // er12

if ( natoms > 0 )
{
v0 = 0LL;
do
{
*(_QWORD *)(qword_860 + 8 * v0) = 0LL;
*(_QWORD *)(ay + 8 * v0) = 0LL;
*(_QWORD *)(az + 8 * v0++) = 0LL;
}
while ( natoms > (int)v0 );
}
if ( natoms > 1 )
{
v1 = 0;
do
{
v5 = v1++;
if ( v1 < natoms )
{
v2 = v1;
do
{
v3 = func1(v5, v2);
v4 = func4(v3);
if ( !func0(v4) )
{
func6(v5, v2, v3);
func7(v3);
func9(v5, v2, v3);
func10(v3);
}
++v2;
}
while ( natoms > v2 );
}
}
while ( natoms - 1 > v1 );
}
}

original_code:
void tersoff1_forces()
{
    int i, j;
    double rij;
    double Uij;
    double U = 0;
    double f_cutoff_val;
    for (i = 0; i < natoms; i++) {
        ax[i] = 0.0;
        ay[i] = 0.0;
        az[i] = 0.0;
    }
    for (i = 0; i < natoms - 1; i++) {
        for (j = i + 1; j < natoms; j++) {
            rij = t1_distance(i, j);
            f_cutoff_val = t1_f_cutoff(rij);
            if (!t1_iszero(f_cutoff_val)) {
                Uij = f_cutoff_val * (t1_a(i, j, rij) * t1_f_repulsive(rij) +
                                      t1_b(i, j, rij) * t1_f_attractive(rij));
                U += Uij;
            }
        }
    }
}

canonical_original_code:
void tersoff1_forces()
{
    int i; int j;
    double rij;
    double Uij;
    double U = 0;
    double f_cutoff_val;
    for (i = 0; i < natoms; i++) {
        ax[i] = 0.0;
        ay[i] = 0.0;
        az[i] = 0.0;
    }
    for (i = 0; i < natoms - 1; i++) {
        for (j = i + 1; j < natoms; j++) {
            rij = t1_distance(i, j);
            f_cutoff_val = t1_f_cutoff(rij);
            if (!t1_iszero(f_cutoff_val)) {
                Uij = f_cutoff_val * (t1_a(i, j, rij) * t1_f_repulsive(rij) +
                                      t1_b(i, j, rij) * t1_f_attractive(rij));
                U += Uij;
            }
        }
    }
}

variable_types: i int

variable_types: j int

variable_types: rij double

variable_types: Uij double

variable_types: U double

return_type: void


decompiled_code:
_BOOL8 __fastcall kronecker_delta(int a1, int a2)
{
return a1 == a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1, int a2)
{
return a1 == a2;
}

original_code:
static int kronecker_delta(int i, int j)
{
    return i == j ? 1 : 0;
}

return_type: int


getcoord

decompiled_code:
double __fastcall getcoord(int a1, int a2)
{
double result; // xmm0_8

if ( a2 == 1 )
return *(double *)(y + 8LL * a1);
if ( a2 == 2 )
return *(double *)(z + 8LL * a1);
result = 0.0;
if ( !a2 )
result = *(double *)(x + 8LL * a1);
return result;
}

canonical_decompiled_code:
double __fastcall func1(int a1, int a2)
{
double result; // xmm0_8

if ( a2 == 1 )
return *(double *)(y + 8LL * a1);
if ( a2 == 2 )
return *(double *)(z + 8LL * a1);
result = 0.0;
if ( !a2 )
result = *(double *)(x + 8LL * a1);
return result;
}

original_code:
static double getcoord(int i, coord_t coord)
{
    switch(coord) {
        case COORD_X:
            return x[i];
            break;
        case COORD_Y:
            return y[i];
            break;
        case COORD_Z:
            return z[i];
            break;
    }
    return 0;
}

canonical_original_code:
static double getcoord(int i, enum coord_t coord)
{
    switch(coord) {
        case COORD_X:
            return x[i];
            break;
        case COORD_Y:
            return y[i];
            break;
        case COORD_Z:
            return z[i];
            break;
    }
    return 0;
}

variable_types: enum coord_t { COORD_X=0, COORD_Y=1, COORD_Z=2 }

return_type: double


decompiled_code:
double __fastcall rij_diff(int a1, int a2, int a3, int a4, double a5)
{
double result; // xmm0_8
double v9; // rbx
double v10; // rbx

if ( a1 == a2 )
{
v9 = getcoord(a2, a4);
result = (v9 - getcoord(a3, a4)) / a5;
}
else
{
result = 0.0;
if ( a1 == a3 )
{
v10 = getcoord(a3, a4);
result = (v10 - getcoord(a2, a4)) / a5;
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func2(int a1, int a2, int a3, int a4, double a5)
{
double result; // xmm0_8
double v9; // rbx
double v10; // rbx

if ( a1 == a2 )
{
v9 = func1(a2, a4);
result = (v9 - func1(a3, a4)) / a5;
}
else
{
result = 0.0;
if ( a1 == a3 )
{
v10 = func1(a3, a4);
result = (v10 - func1(a2, a4)) / a5;
}
}
return result;
}

original_code:
static double rij_diff(int z, int i, int j, double rij, coord_t coord)
{
    if (z == i) {
        return (getcoord(i, coord) - getcoord(j, coord)) / rij;
    } else if (z == j) {
        return (getcoord(j, coord) - getcoord(i, coord)) / rij;
    } else {
        return 0;
    }
}

canonical_original_code:
static double rij_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    if (z == i) {
        return (getcoord(i, coord) - getcoord(j, coord)) / rij;
    } else if (z == j) {
        return (getcoord(j, coord) - getcoord(i, coord)) / rij;
    } else {
        return 0;
    }
}

variable_types: z int

user_defined_types: enum coord_t { COORD_X=0, COORD_Y=1, COORD_Z=2 }


decompiled_code:
double __fastcall f_repulsive_diff(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v7; // r14
double v8; // xmm0_8
double v11; // [rsp+8h] [rbp-30h]
double v12; // [rsp+8h] [rbp-30h]

v7 = lambda_ij(a2, a3);
v11 = Bij(a2, a3) * v7;
v8 = lambda_ij(a2, a3);
v12 = exp(COERCE_DOUBLE(*(_QWORD *)&v8 ^ LC1) * a5) * v11;
return rij_diff(a1, a2, a3, a4, a5) * v12;
}

canonical_decompiled_code:
double __fastcall func3(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v7; // r14
double v8; // xmm0_8
double v11; // [rsp+8h] [rbp-30h]
double v12; // [rsp+8h] [rbp-30h]

v7 = lambda_ij(a2, a3);
v11 = Bij(a2, a3) * v7;
v8 = lambda_ij(a2, a3);
v12 = exp(COERCE_DOUBLE(*(_QWORD *)&v8 ^ LC1) * a5) * v11;
return func2(a1, a2, a3, a4, a5) * v12;
}

original_code:
static double f_repulsive_diff(int z, int i, int j, double rij, coord_t coord)
{
    return -lambda_ij(i, j) * -Bij(i, j) * exp(-lambda_ij(i, j) * rij) *
            rij_diff(z, i, j, rij, coord);
}

canonical_original_code:
static double f_repulsive_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    return -lambda_ij(i, j) * -Bij(i, j) * exp(-lambda_ij(i, j) * rij) *
            rij_diff(z, i, j, rij, coord);
}


decompiled_code:
double __fastcall g_diff(int a1, unsigned int a2, unsigned int a3, unsigned int a4, int a5, double a6, double a7)
{
double v10; // xmm0_8
double v11; // rbx
double v12; // xmm0_8
double v14; // [rsp+0h] [rbp-68h]
double v15; // [rsp+8h] [rbp-60h]
double v16; // [rsp+10h] [rbp-58h]
double v17; // [rsp+18h] [rbp-50h]

v14 = c(a2);
v15 = h(a2);
v16 = t2_cos_theta(a2, a3, a4);
v10 = d(a2);
v17 = v10 * v10;
v11 = h(a2);
v12 = t2_cos_theta(a2, a3, a4);
return COERCE_DOUBLE(cos_theta_diff(a1, a2, a3, a4, a5, a6, a7))
* (COERCE_DOUBLE(COERCE_UNSIGNED_INT64((v14 * v14 + v14 * v14) * (v15 - v16)) ^ LC1)
/ (((v11 - v12) * (v11 - v12) + v17)
* ((v11 - v12) * (v11 - v12) + v17)));
}

canonical_decompiled_code:
double __fastcall func4(int a1, unsigned int a2, unsigned int a3, unsigned int a4, int a5, double a6, double a7)
{
double v10; // xmm0_8
double v11; // rbx
double v12; // xmm0_8
double v14; // [rsp+0h] [rbp-68h]
double v15; // [rsp+8h] [rbp-60h]
double v16; // [rsp+10h] [rbp-58h]
double v17; // [rsp+18h] [rbp-50h]

v14 = c(a2);
v15 = h(a2);
v16 = t2_cos_theta(a2, a3, a4);
v10 = d(a2);
v17 = v10 * v10;
v11 = h(a2);
v12 = t2_cos_theta(a2, a3, a4);
return COERCE_DOUBLE(cos_theta_diff(a1, a2, a3, a4, a5, a6, a7))
* (COERCE_DOUBLE(COERCE_UNSIGNED_INT64((v14 * v14 + v14 * v14) * (v15 - v16)) ^ LC1)
/ (((v11 - v12) * (v11 - v12) + v17)
* ((v11 - v12) * (v11 - v12) + v17)));
}

original_code:
static double g_diff(int z, int i, int j, int k,
                     double rij, double rik, coord_t coord)
{
    return -(2 * pow(c(i), 2) * (h(i) - t2_cos_theta(i, j, k))) /
            pow(pow(d(i), 2) +
                pow(h(i) - t2_cos_theta(i, j, k), 2), 2) *
            cos_theta_diff(z, i, j, k, rij, rik, coord);
}

canonical_original_code:
static double g_diff(int z, int i, int j, int k, double rij, double rik, enum coord_t coord)
{
    return -(2 * pow(c(i), 2) * (h(i) - t2_cos_theta(i, j, k))) /
            pow(pow(d(i), 2) +
                pow(h(i) - t2_cos_theta(i, j, k), 2), 2) *
            cos_theta_diff(z, i, j, k, rij, rik, coord);
}

variable_types: k int

variable_types: rik double


decompiled_code:
double __fastcall f_attractive_diff(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v7; // r14
double v8; // xmm0_8
double v11; // [rsp+8h] [rbp-30h]
double v12; // [rsp+8h] [rbp-30h]

v7 = lambda_ij(a2, a3);
v11 = COERCE_DOUBLE(*(_QWORD *)&v7 ^ LC1) * Aij(a2, a3);
v8 = lambda_ij(a2, a3);
v12 = exp(COERCE_DOUBLE(*(_QWORD *)&v8 ^ LC1) * a5) * v11;
return rij_diff(a1, a2, a3, a4, a5) * v12;
}

canonical_decompiled_code:
double __fastcall func5(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v7; // r14
double v8; // xmm0_8
double v11; // [rsp+8h] [rbp-30h]
double v12; // [rsp+8h] [rbp-30h]

v7 = lambda_ij(a2, a3);
v11 = COERCE_DOUBLE(*(_QWORD *)&v7 ^ LC1) * Aij(a2, a3);
v8 = lambda_ij(a2, a3);
v12 = exp(COERCE_DOUBLE(*(_QWORD *)&v8 ^ LC1) * a5) * v11;
return func2(a1, a2, a3, a4, a5) * v12;
}

original_code:
static double f_attractive_diff(int z, int i, int j, double rij, coord_t coord)
{
    return -lambda_ij(i, j) * Aij(i, j) * exp(-lambda_ij(i, j) * rij) *
            rij_diff(z, i, j, rij, coord);
}

canonical_original_code:
static double f_attractive_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    return -lambda_ij(i, j) * Aij(i, j) * exp(-lambda_ij(i, j) * rij) *
            rij_diff(z, i, j, rij, coord);
}


original_code:
static double f_cutoff_diff(int z, int i, int j, double rij, coord_t coord)
{
    if (rij < Rij(i, j)) {
        return 0;
    } else if ((rij > Rij(i, j)) && (rij < Sij(i, j))) {
        return - 3.14159265358979323846 / (2 * (Sij(i, j), Rij(i, j))) *
                 cos(3.14159265358979323846 * (rij - Rij(i, j)) / (Sij(i, j) - Rij(i, j))) *
                 rij_diff(z, i, j, rij, coord);
    } else {
        return 0;
    }
}

canonical_original_code:
static double f_cutoff_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    if (rij < Rij(i, j)) {
        return 0;
    } else if ((rij > Rij(i, j)) && (rij < Sij(i, j))) {
        return - 3.14159265358979323846 / (2 * (Sij(i, j), Rij(i, j))) *
                 cos(3.14159265358979323846 * (rij - Rij(i, j)) / (Sij(i, j) - Rij(i, j))) *
                 rij_diff(z, i, j, rij, coord);
    } else {
        return 0;
    }
}


decompiled_code:
double __fastcall zeta_diff(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
unsigned int v7; // ebx
double v8; // r12
double v9; // r12
double v11; // [rsp+8h] [rbp-50h]
double v12; // [rsp+8h] [rbp-50h]
double v13; // [rsp+10h] [rbp-48h]
double v14; // [rsp+10h] [rbp-48h]

if ( natoms <= 0 )
return 0.0;
v7 = 0;
v13 = 0.0;
do
{
if ( a2 != v7 && a3 != v7 )
{
v11 = t2_distance(a2, v7);
v8 = t2_f_cutoff(a2, v7);
if ( !(unsigned int)iszero() )
{
v14 = v8 * omega_ij(a2, v7);
v13 = g_diff(a1, a2, a3, v7, a4, a5, v11) * v14;
v9 = f_cutoff_diff(a1, a2, v7, a4, v11);
if ( !(unsigned int)iszero() )
{
v12 = v9 * omega_ij(a2, v7);
v13 = t2_g(a2, a3, v7) * v12 + v13;
}
}
}
++v7;
}
while ( natoms > (int)v7 );
return v13;
}

canonical_decompiled_code:
double __fastcall func7(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
unsigned int v7; // ebx
double v8; // r12
double v9; // r12
double v11; // [rsp+8h] [rbp-50h]
double v12; // [rsp+8h] [rbp-50h]
double v13; // [rsp+10h] [rbp-48h]
double v14; // [rsp+10h] [rbp-48h]

if ( natoms <= 0 )
return 0.0;
v7 = 0;
v13 = 0.0;
do
{
if ( a2 != v7 && a3 != v7 )
{
v11 = t2_distance(a2, v7);
v8 = t2_f_cutoff(a2, v7);
if ( !(unsigned int)iszero() )
{
v14 = v8 * omega_ij(a2, v7);
v13 = func4(a1, a2, a3, v7, a4, a5, v11) * v14;
v9 = func6(a1, a2, v7, a4, v11);
if ( !(unsigned int)iszero() )
{
v12 = v9 * omega_ij(a2, v7);
v13 = t2_g(a2, a3, v7) * v12 + v13;
}
}
}
++v7;
}
while ( natoms > (int)v7 );
return v13;
}

original_code:
static double zeta_diff(int z, int i, int j, double rij, coord_t coord)
{
    double rik, f_cutoff_val, f_cutoff_diff_val, sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t2_distance(i, k);
            f_cutoff_val = t2_f_cutoff(i, k, rik);
            if (!iszero(f_cutoff_val)) {
                sum = f_cutoff_val * omega_ij(i, k) *
                      g_diff(z, i, j, k, rij, rik, coord);
                f_cutoff_diff_val = f_cutoff_diff(z, i, k, rik, coord);
                if (!iszero(f_cutoff_diff_val)) {
                    sum += f_cutoff_diff_val * omega_ij(i, k) * t2_g(i, j, k);
                }
            }
        }
    }
    return sum;
}

canonical_original_code:
static double zeta_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    double rik; double f_cutoff_val; double f_cutoff_diff_val; double sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t2_distance(i, k);
            f_cutoff_val = t2_f_cutoff(i, k, rik);
            if (!iszero(f_cutoff_val)) {
                sum = f_cutoff_val * omega_ij(i, k) *
                      g_diff(z, i, j, k, rij, rik, coord);
                f_cutoff_diff_val = f_cutoff_diff(z, i, k, rik, coord);
                if (!iszero(f_cutoff_diff_val)) {
                    sum += f_cutoff_diff_val * omega_ij(i, k) * t2_g(i, j, k);
                }
            }
        }
    }
    return sum;
}

variable_types: sum double


decompiled_code:
double __fastcall bij_diff(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v8; // r12
double v9; // xmm0_8
double v10; // r12
double v11; // xmm0_8
double result; // xmm0_8
double v13; // r12
double v14; // xmm0_8
double v15; // r12
double v16; // xmm1_8
double v17; // r12
double v18; // xmm0_8
double v19; // xmm0_8
double v20; // [rsp+8h] [rbp-50h]
double v21; // [rsp+8h] [rbp-50h]
double x; // [rsp+10h] [rbp-48h]
double v23; // [rsp+18h] [rbp-40h]

v8 = n(a2);
v9 = beta(a2);
v20 = pow(v9, v8);
v10 = n(a2);
v11 = t2_zeta(a2, a3);
v21 = pow(v11, v10) * v20 + 1.0;
result = 0.0;
if ( !(unsigned int)iszero() )
{
x = t2_zeta(a2, a3);
result = 0.0;
if ( !(unsigned int)iszero() )
{
v23 = chi(a2, a3, 0.0);
v13 = n(a2);
v14 = beta(a2);
v15 = pow(v14, v13);
v16 = n(a2) - 1.0;
v17 = COERCE_DOUBLE(*(_QWORD *)&v15 ^ LC1) * pow(x, v16) * v23;
v18 = n(a2);
v19 = pow(v21, 1.0 / (v18 + v18) + 1.0);
result = zeta_diff(a1, a2, a3, a4, a5) * (v17 / (v19 + v19));
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func8(int a1, unsigned int a2, unsigned int a3, int a4, double a5)
{
double v8; // r12
double v9; // xmm0_8
double v10; // r12
double v11; // xmm0_8
double result; // xmm0_8
double v13; // r12
double v14; // xmm0_8
double v15; // r12
double v16; // xmm1_8
double v17; // r12
double v18; // xmm0_8
double v19; // xmm0_8
double v20; // [rsp+8h] [rbp-50h]
double v21; // [rsp+8h] [rbp-50h]
double x; // [rsp+10h] [rbp-48h]
double v23; // [rsp+18h] [rbp-40h]

v8 = n(a2);
v9 = beta(a2);
v20 = pow(v9, v8);
v10 = n(a2);
v11 = t2_zeta(a2, a3);
v21 = pow(v11, v10) * v20 + 1.0;
result = 0.0;
if ( !(unsigned int)iszero() )
{
x = t2_zeta(a2, a3);
result = 0.0;
if ( !(unsigned int)iszero() )
{
v23 = chi(a2, a3, 0.0);
v13 = n(a2);
v14 = beta(a2);
v15 = pow(v14, v13);
v16 = n(a2) - 1.0;
v17 = COERCE_DOUBLE(*(_QWORD *)&v15 ^ LC1) * pow(x, v16) * v23;
v18 = n(a2);
v19 = pow(v21, 1.0 / (v18 + v18) + 1.0);
result = func7(a1, a2, a3, a4, a5) * (v17 / (v19 + v19));
}
}
return result;
}

original_code:
static double bij_diff(int z, int i, int j, double rij, coord_t coord)
{
    double bij_denom_tmp, t2_zeta_val;
    bij_denom_tmp = 1 + pow(beta(i), n(i)) * pow(t2_zeta(i, j), n(i));
    if (iszero(bij_denom_tmp)) {
        return 0.0;
    } else {
        t2_zeta_val = t2_zeta(i, j);
        if (iszero(t2_zeta_val)) {
            return 0.0;
        } else {
            return chi(i, j) * (-pow(beta(i), n(i)) *
                                 pow(t2_zeta_val, n(i) - 1)) /
                   (2 * pow(bij_denom_tmp, 1 / (2 * n(i)) + 1)) *
                   zeta_diff(z, i, j, rij, coord);
        }
    }
}

canonical_original_code:
static double bij_diff(int z, int i, int j, double rij, enum coord_t coord)
{
    double bij_denom_tmp; double t2_zeta_val;
    bij_denom_tmp = 1 + pow(beta(i), n(i)) * pow(t2_zeta(i, j), n(i));
    if (iszero(bij_denom_tmp)) {
        return 0.0;
    } else {
        t2_zeta_val = t2_zeta(i, j);
        if (iszero(t2_zeta_val)) {
            return 0.0;
        } else {
            return chi(i, j) * (-pow(beta(i), n(i)) *
                                 pow(t2_zeta_val, n(i) - 1)) /
                   (2 * pow(bij_denom_tmp, 1 / (2 * n(i)) + 1)) *
                   zeta_diff(z, i, j, rij, coord);
        }
    }
}


decompiled_code:
void __fastcall tersoff2_forces_ij(unsigned int a1, unsigned int a2)
{
if ( a1 != a2 )
{
*(double *)&Fx = Uij_diff(atom_z, a1, a2, 0) + *(double *)&Fx;
*(double *)&Fy = Uij_diff(atom_z, a1, a2, 1) + *(double *)&Fy;
*(double *)&Fz = Uij_diff(atom_z, a1, a2, 2) + *(double *)&Fz;
}
}

canonical_decompiled_code:
void __fastcall func10(unsigned int a1, unsigned int a2)
{
if ( a1 != a2 )
{
*(double *)&Fx = func9(atom_z, a1, a2, 0) + *(double *)&Fx;
*(double *)&Fy = func9(atom_z, a1, a2, 1) + *(double *)&Fy;
*(double *)&Fz = func9(atom_z, a1, a2, 2) + *(double *)&Fz;
}
}

original_code:
static void tersoff2_forces_ij(int atom_i, int atom_j)
{
    if (atom_i != atom_j) {
        Fx += Uij_diff(atom_z, atom_i, atom_j, COORD_X);
        Fy += Uij_diff(atom_z, atom_i, atom_j, COORD_Y);
        Fz += Uij_diff(atom_z, atom_i, atom_j, COORD_Z);
    }
}


decompiled_code:
__int64 tersoff2_forces()
{
__int64 v0; // rsi
__int64 v1; // rbx
double v2; // rbx
double v3; // rbx
__int64 result; // rax

atom_z = 0;
if ( natoms > 0 )
{
v0 = 0LL;
do
{
printf("z = %d\n", v0);
Fz = 0LL;
Fy = 0LL;
Fx = 0LL;
celllist_scan_atoms(tersoff2_forces_ij);
*(double *)&Fx = -0.5 * *(double *)&Fx;
v1 = Fx;
*(double *)&Fy = -0.5 * *(double *)&Fy;
*(double *)&Fz = -0.5 * *(double *)&Fz;
*(double *)(qword_FE0 + 8LL * atom_z) = *(double *)&v1 / mass((unsigned int)atom_z);
v2 = *(double *)&Fy;
*(double *)(ay + 8LL * atom_z) = v2 / mass((unsigned int)atom_z);
v3 = *(double *)&Fz;
*(double *)(az + 8LL * atom_z) = v3 / mass((unsigned int)atom_z);
result = (unsigned int)atom_z;
v0 = (unsigned int)(atom_z + 1);
atom_z = v0;
}
while ( (int)v0 < natoms );
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rsi
__int64 v1; // rbx
double v2; // rbx
double v3; // rbx
__int64 result; // rax

atom_z = 0;
if ( natoms > 0 )
{
v0 = 0LL;
do
{
printf("z = %d\n", v0);
Fz = 0LL;
Fy = 0LL;
Fx = 0LL;
celllist_scan_atoms(tersoff2_forces_ij);
*(double *)&Fx = -0.5 * *(double *)&Fx;
v1 = Fx;
*(double *)&Fy = -0.5 * *(double *)&Fy;
*(double *)&Fz = -0.5 * *(double *)&Fz;
*(double *)(qword_FE0 + 8LL * atom_z) = *(double *)&v1 / mass((unsigned int)atom_z);
v2 = *(double *)&Fy;
*(double *)(ay + 8LL * atom_z) = v2 / mass((unsigned int)atom_z);
v3 = *(double *)&Fz;
*(double *)(az + 8LL * atom_z) = v3 / mass((unsigned int)atom_z);
result = (unsigned int)atom_z;
v0 = (unsigned int)(atom_z + 1);
atom_z = v0;
}
while ( (int)v0 < natoms );
}
return result;
}

original_code:
void tersoff2_forces()
{
    for (atom_z = 0; atom_z < natoms; atom_z++) {
        printf("z = %d\n", atom_z);
        Fx = Fy = Fz = 0;
        celllist_scan_atoms(tersoff2_forces_ij);
        Fx *= -0.5;
        Fy *= -0.5;
        Fz *= -0.5;
        ax[atom_z] = Fx / mass(atom_z);
        ay[atom_z] = Fy / mass(atom_z);
        az[atom_z] = Fz / mass(atom_z);
    }
}


original_code:
static _Bool scan_cells(cell_t *cell)
{
    cell->z++;
    if (cell->z == cell_Lz) {
        cell->z = 0;
        cell->y++;
        if (cell->y == cell_Ly) {
            cell->y = 0;
            cell->x++;
            if (cell->x == cell_Lx) {
                cell->x = 0;
                return 0;
            }
        }
    }
    return 1;
}

canonical_original_code:
static _Bool scan_cells(struct cell_s *cell)
{
    cell->z++;
    if (cell->z == cell_Lz) {
        cell->z = 0;
        cell->y++;
        if (cell->y == cell_Ly) {
            cell->y = 0;
            cell->x++;
            if (cell->x == cell_Lx) {
                cell->x = 0;
                return 0;
            }
        }
    }
    return 1;
}

variable_types: struct cell_s { int x; int y; int z; int scal; } *


decompiled_code:
__int64 __fastcall celllist_scan_atoms(void (*a1)(void))
{
__int64 result; // rax
int v2; // [rsp+0h] [rbp-58h] BYREF
int v3; // [rsp+4h] [rbp-54h] BYREF
__int64 v4; // [rsp+8h] [rbp-50h] BYREF
__int64 v5; // [rsp+10h] [rbp-48h] BYREF
__int64 v6; // [rsp+18h] [rbp-40h] BYREF
__int64 v7; // [rsp+20h] [rbp-38h] BYREF
__int64 v8; // [rsp+28h] [rbp-30h]
__int64 v9; // [rsp+30h] [rbp-28h] BYREF
__int64 v10; // [rsp+38h] [rbp-20h]
__int64 v11; // [rsp+40h] [rbp-18h] BYREF
__int64 v12; // [rsp+48h] [rbp-10h]

init_cell(&v11);
while ( 1 )
{
result = scan_cells((int *)&v11);
if ( !(_BYTE)result )
break;
cell_vec_to_scal(&v11);
if ( HIDWORD(v12) == 1000 * (SHIDWORD(v12) / 1000) )
printf("cell = %d\n", HIDWORD(v12));
init_neigh_cell(v11, v12, &v9);
while ( (unsigned __int8)scan_neigh_cells(v11, v12, &v9) )
{
cell_periodic_bound_cond(v9, v10, &v7, &v4, &v5, &v6);
cell_vec_to_scal(&v7);
if ( SHIDWORD(v12) < SHIDWORD(v8) && init_atom_in_cell(v11, v12, &v2) )
{
do
{
if ( init_atom_in_cell(v7, v8, &v3) )
{
do
{
if ( v2 < v3 )
a1();
}
while ( scan_atom_in_cell(v7, v8, &v3) );
}
}
while ( scan_atom_in_cell(v11, v12, &v2) );
}
}
}
return result;
}

original_code:
void celllist_scan_atoms(void (*func) (int atom_i, int atom_j))
{
    cell_t cell;
    cell_t neigh_cell_raw;
    cell_t neigh_cell;
    init_cell(&cell);
    while (scan_cells(&cell)) {
        cell_vec_to_scal(&cell);
        if (cell.scal % 1000 == 0) {
            printf("cell = %d\n", cell.scal);
        }
        init_neigh_cell(cell, &neigh_cell_raw);
        while (scan_neigh_cells(cell, &neigh_cell_raw)) {
            int atom_cell, atom_neighcell;
            double atom_shift_x, atom_shift_y, atom_shift_z;
            cell_periodic_bound_cond(neigh_cell_raw, &neigh_cell,
                                   &atom_shift_x, &atom_shift_y, &atom_shift_z);
            cell_vec_to_scal(&neigh_cell);
            if (cell.scal >= neigh_cell.scal) {
                continue;
            }
            if (!init_atom_in_cell(cell, &atom_cell)) {
                continue;
            }
            do {
                if (!init_atom_in_cell(neigh_cell, &atom_neighcell)) {
                    continue;
                }
                do {
                    int atom_i = atom_cell;
                    int atom_j = atom_neighcell;
                    if (atom_i >= atom_j) {
                        continue;
                    }
                    func(atom_i, atom_j);
                } while (scan_atom_in_cell(neigh_cell, &atom_neighcell));
            } while (scan_atom_in_cell(cell, &atom_cell));
        }
    }
}

canonical_original_code:
void celllist_scan_atoms(void (*func)(int atom_i, int atom_j))
{
    struct cell_s cell;
    struct cell_s neigh_cell_raw;
    struct cell_s neigh_cell;
    init_cell(&cell);
    while (scan_cells(&cell)) {
        cell_vec_to_scal(&cell);
        if (cell.scal % 1000 == 0) {
            printf("cell = %d\n", cell.scal);
        }
        init_neigh_cell(cell, &neigh_cell_raw);
        while (scan_neigh_cells(cell, &neigh_cell_raw)) {
            int atom_cell; int atom_neighcell;
            double atom_shift_x; double atom_shift_y; double atom_shift_z;
            cell_periodic_bound_cond(neigh_cell_raw, &neigh_cell,
                                   &atom_shift_x, &atom_shift_y, &atom_shift_z);
            cell_vec_to_scal(&neigh_cell);
            if (cell.scal >= neigh_cell.scal) {
                continue;
            }
            if (!init_atom_in_cell(cell, &atom_cell)) {
                continue;
            }
            do {
                if (!init_atom_in_cell(neigh_cell, &atom_neighcell)) {
                    continue;
                }
                do {
                    int atom_i = atom_cell;
                    int atom_j = atom_neighcell;
                    if (atom_i >= atom_j) {
                        continue;
                    }
                    func(atom_i, atom_j);
                } while (scan_atom_in_cell(neigh_cell, &atom_neighcell));
            } while (scan_atom_in_cell(cell, &atom_cell));
        }
    }
}

variable_types:
func
void (*)(int atom_i, int atom_j)

variable_types: struct cell_s { int x; int y; int z; int scal; }


decompiled_code:
_DWORD *linked_cell_init()
{
__m128d v0; // xmm1
double v1; // xmm4_8
__m128d v2; // xmm4
int v3; // ecx
__m128d v4; // xmm3
double v5; // xmm5_8
__m128d v6; // xmm5
int v7; // edx
__m128d v8; // xmm4
double v9; // xmm6_8
__m128d v10; // xmm6
_DWORD *result; // rax
_DWORD *v12; // rdx

v0.m128d_f64[0] = Lx / 2.85;
v1 = Lx / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Lx / 2.85) & LC5) < 4.503599627370496e15 )
{
v2.m128d_f64[0] = (double)(int)v0.m128d_f64[0];
*(_QWORD *)&v1 = COERCE_UNSIGNED_INT64(v2.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v2, v0) & LC6)) | ~LC5 & *(_QWORD *)&v0.m128d_f64[0];
}
v3 = (int)v1;
cell_Lx = (int)v1;
v4.m128d_f64[0] = Ly / 2.85;
v5 = Ly / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Ly / 2.85) & LC5) < 4.503599627370496e15 )
{
v6.m128d_f64[0] = (double)(int)v4.m128d_f64[0];
*(_QWORD *)&v5 = COERCE_UNSIGNED_INT64(v6.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v6, v4) & LC6)) | ~LC5 & *(_QWORD *)&v4.m128d_f64[0];
}
v7 = (int)v5;
cell_Ly = (int)v5;
v8.m128d_f64[0] = Lz / 2.85;
v9 = Lz / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Lz / 2.85) & LC5) < 4.503599627370496e15 )
{
v10.m128d_f64[0] = (double)(int)v8.m128d_f64[0];
*(_QWORD *)&v9 = COERCE_UNSIGNED_INT64(v10.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v10, v8) & LC6)) | ~LC5 & *(_QWORD *)&v8.m128d_f64[0];
}
cell_Lz = (int)v9;
ncells = (int)v9 * v7 * v3;
cell_LyLz = (int)v9 * v7;
*(double *)&cell_rx = Lx / (double)v3;
*(double *)&cell_ry = Ly / (double)v7;
*(double *)&cell_rz = Lz / (double)(int)v9;
list = (void *)xmalloc(4LL * natoms);
result = (_DWORD *)xmalloc(4LL * ncells);
head = result;
if ( ncells > 0 )
{
v12 = &result[ncells];
do
*result++ = -1;
while ( result != v12 );
}
return result;
}

canonical_decompiled_code:
_DWORD *func9()
{
__m128d v0; // xmm1
double v1; // xmm4_8
__m128d v2; // xmm4
int v3; // ecx
__m128d v4; // xmm3
double v5; // xmm5_8
__m128d v6; // xmm5
int v7; // edx
__m128d v8; // xmm4
double v9; // xmm6_8
__m128d v10; // xmm6
_DWORD *result; // rax
_DWORD *v12; // rdx

v0.m128d_f64[0] = Lx / 2.85;
v1 = Lx / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Lx / 2.85) & LC5) < 4.503599627370496e15 )
{
v2.m128d_f64[0] = (double)(int)v0.m128d_f64[0];
*(_QWORD *)&v1 = COERCE_UNSIGNED_INT64(v2.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v2, v0) & LC6)) | ~LC5 & *(_QWORD *)&v0.m128d_f64[0];
}
v3 = (int)v1;
cell_Lx = (int)v1;
v4.m128d_f64[0] = Ly / 2.85;
v5 = Ly / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Ly / 2.85) & LC5) < 4.503599627370496e15 )
{
v6.m128d_f64[0] = (double)(int)v4.m128d_f64[0];
*(_QWORD *)&v5 = COERCE_UNSIGNED_INT64(v6.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v6, v4) & LC6)) | ~LC5 & *(_QWORD *)&v4.m128d_f64[0];
}
v7 = (int)v5;
cell_Ly = (int)v5;
v8.m128d_f64[0] = Lz / 2.85;
v9 = Lz / 2.85;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Lz / 2.85) & LC5) < 4.503599627370496e15 )
{
v10.m128d_f64[0] = (double)(int)v8.m128d_f64[0];
*(_QWORD *)&v9 = COERCE_UNSIGNED_INT64(v10.m128d_f64[0] - COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v10, v8) & LC6)) | ~LC5 & *(_QWORD *)&v8.m128d_f64[0];
}
cell_Lz = (int)v9;
ncells = (int)v9 * v7 * v3;
cell_LyLz = (int)v9 * v7;
*(double *)&cell_rx = Lx / (double)v3;
*(double *)&cell_ry = Ly / (double)v7;
*(double *)&cell_rz = Lz / (double)(int)v9;
list = (void *)xmalloc(4LL * natoms);
result = (_DWORD *)xmalloc(4LL * ncells);
head = result;
if ( ncells > 0 )
{
v12 = &result[ncells];
do
*result++ = -1;
while ( result != v12 );
}
return result;
}

original_code:
void linked_cell_init()
{
    const double r_cutoff_more = r_cutoff + delta_r;
    int cell;
    cell_Lx = floor(Lx / r_cutoff_more);
    cell_Ly = floor(Ly / r_cutoff_more);
    cell_Lz = floor(Lz / r_cutoff_more);
    ncells = cell_Lx * cell_Ly * cell_Lz;
    cell_LyLz = cell_Ly * cell_Lz;
    cell_rx = Lx / cell_Lx;
    cell_ry = Ly / cell_Ly;
    cell_rz = Lz / cell_Lz;
    list = xmalloc(sizeof(*list) * natoms);
    head = xmalloc(sizeof(*head) * ncells);
    for (cell = 0; cell < ncells; cell++) {
        head[cell] = LIST_END;
    }
}

canonical_original_code:
void linked_cell_init()
{
    double r_cutoff_more = r_cutoff + delta_r;
    int cell;
    cell_Lx = floor(Lx / r_cutoff_more);
    cell_Ly = floor(Ly / r_cutoff_more);
    cell_Lz = floor(Lz / r_cutoff_more);
    ncells = cell_Lx * cell_Ly * cell_Lz;
    cell_LyLz = cell_Ly * cell_Lz;
    cell_rx = Lx / cell_Lx;
    cell_ry = Ly / cell_Ly;
    cell_rz = Lz / cell_Lz;
    list = xmalloc(sizeof(*list) * natoms);
    head = xmalloc(sizeof(*head) * ncells);
    for (cell = 0; cell < ncells; cell++) {
        head[cell] = LIST_END;
    }
}

variable_types: cell int


decompiled_code:
_DWORD *linked_cell_update()
{
__int64 v0; // rbx
_DWORD *result; // rax
int v2[3]; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+Ch] [rbp-Ch]

if ( natoms > 0 )
{
v0 = 0LL;
do
{
v2[0] = (int)(*(double *)(x + 8 * v0) / *(double *)&cell_rx);
v2[1] = (int)(*(double *)(y + 8 * v0) / *(double *)&cell_ry);
v2[2] = (int)(*(double *)(z + 8 * v0) / *(double *)&cell_rz);
cell_vec_to_scal(v2);
result = head;
*((_DWORD *)list + v0) = *((_DWORD *)head + v3);
result[v3] = v0++;
}
while ( natoms > (int)v0 );
}
return result;
}

canonical_decompiled_code:
_DWORD *func10()
{
__int64 v0; // rbx
_DWORD *result; // rax
int v2[3]; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+Ch] [rbp-Ch]

if ( natoms > 0 )
{
v0 = 0LL;
do
{
v2[0] = (int)(*(double *)(x + 8 * v0) / *(double *)&cell_rx);
v2[1] = (int)(*(double *)(y + 8 * v0) / *(double *)&cell_ry);
v2[2] = (int)(*(double *)(z + 8 * v0) / *(double *)&cell_rz);
func0(v2);
result = head;
*((_DWORD *)list + v0) = *((_DWORD *)head + v3);
result[v3] = v0++;
}
while ( natoms > (int)v0 );
}
return result;
}

original_code:
void linked_cell_update()
{
    int atom;
    cell_t cell;
    for (atom = 0; atom < natoms; atom++) {
        cell.x = x[atom] / cell_rx;
        cell.y = y[atom] / cell_ry;
        cell.z = z[atom] / cell_rz;
        cell_vec_to_scal(&cell);
        list[atom] = head[cell.scal];
        head[cell.scal] = atom;
    }
}

canonical_original_code:
void linked_cell_update()
{
    int atom;
    struct cell_s cell;
    for (atom = 0; atom < natoms; atom++) {
        cell.x = x[atom] / cell_rx;
        cell.y = y[atom] / cell_ry;
        cell.z = z[atom] / cell_rz;
        cell_vec_to_scal(&cell);
        list[atom] = head[cell.scal];
        head[cell.scal] = atom;
    }
}

variable_types: atom int

user_defined_types: struct cell_s { int x; int y; int z; int scal; }


decompiled_code:
void linked_cell_finalize()
{
free(list);
free(head);
}

canonical_decompiled_code:
void func11()
{
free(list);
free(head);
}


decompiled_code:
bool cell_update_test()
{
double v0; // xmm0_8
__int64 i; // rax
double v2; // xmm1_8

v0 = *vx;
if ( natoms > 0 )
{
for ( i = 0LL; ; v0 = fmax(*(double *)(vx + i), v2) )
{
v2 = fmax(*(double *)(vz + i), fmax(*(double *)(vy + i), v0));
v0 = v2;
if ( 8LL * (unsigned int)(natoms - 1) == i )
break;
i += 8LL;
}
}
*(double *)&sum_max_v_0 = v0 + *(double *)&sum_max_v_0;
return *(double *)&sum_max_v_0 > 0.35 / (*(double *)&timestep_dt + *(double *)&timestep_dt);
}

canonical_decompiled_code:
bool func12()
{
double v0; // xmm0_8
__int64 i; // rax
double v2; // xmm1_8

v0 = *vx;
if ( natoms > 0 )
{
for ( i = 0LL; ; v0 = fmax(*(double *)(vx + i), v2) )
{
v2 = fmax(*(double *)(vz + i), fmax(*(double *)(vy + i), v0));
v0 = v2;
if ( 8LL * (unsigned int)(natoms - 1) == i )
break;
i += 8LL;
}
}
*(double *)&sum_max_v_0 = v0 + *(double *)&sum_max_v_0;
return *(double *)&sum_max_v_0 > 0.35 / (*(double *)&timestep_dt + *(double *)&timestep_dt);
}

original_code:
_Bool cell_update_test()
{
    int atom;
    double max_v = vx[0];
    static double sum_max_v = 0;
    for (atom = 0; atom < natoms; atom++) {
        if (vx[atom] > max_v) {
            max_v = vx[atom];
        }
        if (vy[atom] > max_v) {
            max_v = vy[atom];
        }
        if (vz[atom] > max_v) {
            max_v = vz[atom];
        }
    }
    sum_max_v += max_v;
    if (sum_max_v > delta_r / (2 * timestep_dt)) {
        return 1;
        sum_max_v = 0;
    } else {
        return 0;
    }
    return 1;
}


decompiled_code:
void compute_pos()
{
__int64 v0; // rdx
double *v1; // rcx
double v2; // xmm0_8
double *v3; // rcx
double v4; // xmm0_8
__int64 v5; // rax
double *v6; // rcx
double v7; // xmm0_8

if ( natoms > 0 )
{
v0 = 0LL;
do
{
v5 = 8 * v0;
*((double *)x + v0) = *(double *)&timestep_dt * *((double *)vx + v0)
+ *((double *)x + v0)
+ 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
*((double *)y + v0) = *(double *)&timestep_dt * *((double *)vy + v0)
+ *((double *)y + v0)
+ 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
*((double *)z + v0) = *(double *)&timestep_dt * *((double *)vz + v0)
+ *((double *)z + v0)
+ 0.5 * *((double *)az + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
v6 = (double *)((char *)x + 8 * v0);
v7 = *v6;
if ( *v6 < *(double *)&Lx * 0.5 )
{
if ( COERCE_DOUBLE(Lx ^ LC1) * 0.5 > v7 )
*v6 = v7 + *(double *)&Lx;
}
else
{
*v6 = v7 - *(double *)&Lx;
}
v1 = (double *)((char *)y + v5);
v2 = *((double *)y + v0);
if ( v2 < *(double *)&Ly * 0.5 )
{
if ( COERCE_DOUBLE(Ly ^ LC1) * 0.5 > v2 )
*v1 = v2 + *(double *)&Ly;
}
else
{
*v1 = v2 - *(double *)&Ly;
}
v3 = (double *)((char *)z + v5);
v4 = *((double *)z + v0);
if ( v4 < *(double *)&Lz * 0.5 )
{
if ( COERCE_DOUBLE(Lz ^ LC1) * 0.5 > v4 )
*v3 = v4 + *(double *)&Lz;
}
else
{
*v3 = v4 - *(double *)&Lz;
}
*((double *)vx + v0) = 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt + *((double *)vx + v0);
*((double *)vy + v0) = 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt + *((double *)vy + v0);
*((double *)vz + v0) = 0.5 * *((double *)az + v0) * *(double *)&timestep_dt + *((double *)vz + v0);
++v0;
}
while ( natoms > (int)v0 );
}
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rdx
double *v1; // rcx
double v2; // xmm0_8
double *v3; // rcx
double v4; // xmm0_8
__int64 v5; // rax
double *v6; // rcx
double v7; // xmm0_8

if ( natoms > 0 )
{
v0 = 0LL;
do
{
v5 = 8 * v0;
*((double *)x + v0) = *(double *)&timestep_dt * *((double *)vx + v0)
+ *((double *)x + v0)
+ 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
*((double *)y + v0) = *(double *)&timestep_dt * *((double *)vy + v0)
+ *((double *)y + v0)
+ 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
*((double *)z + v0) = *(double *)&timestep_dt * *((double *)vz + v0)
+ *((double *)z + v0)
+ 0.5 * *((double *)az + v0) * *(double *)&timestep_dt * *(double *)&timestep_dt;
v6 = (double *)((char *)x + 8 * v0);
v7 = *v6;
if ( *v6 < *(double *)&Lx * 0.5 )
{
if ( COERCE_DOUBLE(Lx ^ LC1) * 0.5 > v7 )
*v6 = v7 + *(double *)&Lx;
}
else
{
*v6 = v7 - *(double *)&Lx;
}
v1 = (double *)((char *)y + v5);
v2 = *((double *)y + v0);
if ( v2 < *(double *)&Ly * 0.5 )
{
if ( COERCE_DOUBLE(Ly ^ LC1) * 0.5 > v2 )
*v1 = v2 + *(double *)&Ly;
}
else
{
*v1 = v2 - *(double *)&Ly;
}
v3 = (double *)((char *)z + v5);
v4 = *((double *)z + v0);
if ( v4 < *(double *)&Lz * 0.5 )
{
if ( COERCE_DOUBLE(Lz ^ LC1) * 0.5 > v4 )
*v3 = v4 + *(double *)&Lz;
}
else
{
*v3 = v4 - *(double *)&Lz;
}
*((double *)vx + v0) = 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt + *((double *)vx + v0);
*((double *)vy + v0) = 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt + *((double *)vy + v0);
*((double *)vz + v0) = 0.5 * *((double *)az + v0) * *(double *)&timestep_dt + *((double *)vz + v0);
++v0;
}
while ( natoms > (int)v0 );
}
}

original_code:
static void compute_pos()
{
    int atom;
    for (atom = 0; atom < natoms; atom++) {
        x[atom] = x[atom] + vx[atom] * timestep_dt +
               0.5 * ax[atom] * timestep_dt * timestep_dt;
        y[atom] = y[atom] + vy[atom] * timestep_dt +
               0.5 * ay[atom] * timestep_dt * timestep_dt;
        z[atom] = z[atom] + vz[atom] * timestep_dt +
               0.5 * az[atom] * timestep_dt * timestep_dt;
        if (x[atom] >= Lx / 2) {
            x[atom] = x[atom] - Lx;
        } else if (x[atom] < -Lx / 2) {
            x[atom] = x[atom] + Lx;
        }
        if (y[atom] >= Ly / 2) {
            y[atom] = y[atom] - Ly;
        } else if (y[atom] < -Ly / 2) {
            y[atom] = y[atom] + Ly;
        }
        if (z[atom] >= Lz / 2) {
            z[atom] = z[atom] - Lz;
        } else if (z[atom] < -Lz / 2) {
            z[atom] = z[atom] + Lz;
        }
        vx[atom] = vx[atom] + 0.5 * ax[atom] * timestep_dt;
        vy[atom] = vy[atom] + 0.5 * ay[atom] * timestep_dt;
        vz[atom] = vz[atom] + 0.5 * az[atom] * timestep_dt;
    }
}


decompiled_code:
void compute_velocities()
{
__int64 v0; // rax

if ( natoms > 0 )
{
v0 = 0LL;
do
{
*((double *)vx + v0) = 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt + *((double *)vx + v0);
*((double *)vy + v0) = 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt + *((double *)vy + v0);
*((double *)vz + v0) = 0.5 * *((double *)az + v0) * *(double *)&timestep_dt + *((double *)vz + v0);
++v0;
}
while ( natoms > (int)v0 );
}
}

canonical_decompiled_code:
void func1()
{
__int64 v0; // rax

if ( natoms > 0 )
{
v0 = 0LL;
do
{
*((double *)vx + v0) = 0.5 * *((double *)ptr + v0) * *(double *)&timestep_dt + *((double *)vx + v0);
*((double *)vy + v0) = 0.5 * *((double *)ay + v0) * *(double *)&timestep_dt + *((double *)vy + v0);
*((double *)vz + v0) = 0.5 * *((double *)az + v0) * *(double *)&timestep_dt + *((double *)vz + v0);
++v0;
}
while ( natoms > (int)v0 );
}
}

original_code:
static void compute_velocities()
{
    int atom;
    for (atom = 0; atom < natoms; atom++) {
        vx[atom] = vx[atom] + 0.5 * ax[atom] * timestep_dt;
        vy[atom] = vy[atom] + 0.5 * ay[atom] * timestep_dt;
        vz[atom] = vz[atom] + 0.5 * az[atom] * timestep_dt;
    }
}


integrate

canonical_decompiled_code:
void func2()
{
func0();
tersoff2_forces();
tersoff2_energy();
func1();
}

original_code:
static void integrate()
{
    compute_pos();
    tersoff2_forces();
    tersoff2_energy();
    compute_velocities();
}


decompiled_code:
void *allocate_memory()
{
void *result; // rax

x = (void *)xmalloc(8LL * natoms);
y = (void *)xmalloc(8LL * natoms);
z = (void *)xmalloc(8LL * natoms);
vx = (void *)xmalloc(8LL * natoms);
vy = (void *)xmalloc(8LL * natoms);
vz = (void *)xmalloc(8LL * natoms);
ptr = (void *)xmalloc(8LL * natoms);
ay = (void *)xmalloc(8LL * natoms);
az = (void *)xmalloc(8LL * natoms);
result = (void *)xmalloc(4LL * natoms);
atomspecies = result;
return result;
}

canonical_decompiled_code:
void *func4()
{
void *result; // rax

x = (void *)xmalloc(8LL * natoms);
y = (void *)xmalloc(8LL * natoms);
z = (void *)xmalloc(8LL * natoms);
vx = (void *)xmalloc(8LL * natoms);
vy = (void *)xmalloc(8LL * natoms);
vz = (void *)xmalloc(8LL * natoms);
ptr = (void *)xmalloc(8LL * natoms);
ay = (void *)xmalloc(8LL * natoms);
az = (void *)xmalloc(8LL * natoms);
result = (void *)xmalloc(4LL * natoms);
atomspecies = result;
return result;
}

original_code:
static void allocate_memory()
{
    x = xmalloc(sizeof(*x) * natoms);
    y = xmalloc(sizeof(*y) * natoms);
    z = xmalloc(sizeof(*z) * natoms);
    vx = xmalloc(sizeof(*vx) * natoms);
    vy = xmalloc(sizeof(*vy) * natoms);
    vz = xmalloc(sizeof(*vz) * natoms);
    ax = xmalloc(sizeof(*ax) * natoms);
    ay = xmalloc(sizeof(*ay) * natoms);
    az = xmalloc(sizeof(*az) * natoms);
    atomspecies = xmalloc(sizeof(*atomspecies) * natoms);
}


decompiled_code:
void free_memory()
{
__int64 v0; // rbx

free(x);
free(y);
free(z);
free(vx);
free(vy);
free(vz);
free(ptr);
free(ay);
free(az);
free(atomspecies);
if ( natomspecies > 0 )
{
v0 = 0LL;
do
free(*((void **)atomspecies_names + v0++));
while ( natomspecies > (int)v0 );
}
free(atomspecies_names);
}

canonical_decompiled_code:
void func8()
{
__int64 v0; // rbx

free(x);
free(y);
free(z);
free(vx);
free(vy);
free(vz);
free(ptr);
free(ay);
free(az);
free(atomspecies);
if ( natomspecies > 0 )
{
v0 = 0LL;
do
free(*((void **)atomspecies_names + v0++));
while ( natomspecies > (int)v0 );
}
free(atomspecies_names);
}

original_code:
static void free_memory()
{
    int species;
    free(x);
    free(y);
    free(z);
    free(vx);
    free(vy);
    free(vz);
    free(ax);
    free(ay);
    free(az);
    free(atomspecies);
    for (species = 0; species < natomspecies; species++) {
        free(atomspecies_names[species]);
    }
    free(atomspecies_names);
}

variable_types: species int


decompiled_code:
__int64 md_run()
{
__int64 v0; // rsi

puts("Run modelling");
tersoff2_param_init();
linked_cell_init();
linked_cell_update();
tersoff2_energy();
tersoff2_forces();
timestep = 1;
if ( ntimesteps > 0 )
{
v0 = 1LL;
do
{
printf("step %6d timestep: %E sec.\n", v0, (double)(int)v0 * *(double *)&timestep_dt);
integrate();
if ( (unsigned int)cell_update_test() )
linked_cell_update();
v0 = (unsigned int)(timestep + 1);
timestep = v0;
}
while ( (int)v0 <= ntimesteps );
}
tersoff2_param_finalize();
return linked_cell_finalize();
}

canonical_decompiled_code:
__int64 func9()
{
__int64 v0; // rsi

puts("Run modelling");
tersoff2_param_init();
linked_cell_init();
linked_cell_update();
tersoff2_energy();
tersoff2_forces();
timestep = 1;
if ( ntimesteps > 0 )
{
v0 = 1LL;
do
{
printf("step %6d timestep: %E sec.\n", v0, (double)(int)v0 * *(double *)&timestep_dt);
func2();
if ( (unsigned int)cell_update_test() )
linked_cell_update();
v0 = (unsigned int)(timestep + 1);
timestep = v0;
}
while ( (int)v0 <= ntimesteps );
}
tersoff2_param_finalize();
return linked_cell_finalize();
}

original_code:
void md_run()
{
    double t;
    printf("Run modelling\n");
    tersoff2_param_init();
    linked_cell_init();
    linked_cell_update();
    tersoff2_energy();
    tersoff2_forces();
    for (timestep = 1; timestep <= ntimesteps; timestep++) {
        t = timestep * timestep_dt;
        printf("step %6d timestep: %E sec.\n", timestep, t);
        integrate();
        if (cell_update_test()) {
            linked_cell_update();
        }
    }
    tersoff2_param_finalize();
    linked_cell_finalize();
}

variable_types: t double


decompiled_code:
int __fastcall md_initialize(char *a1, char *a2)
{
read_atoms(a1);
return read_inputfile(a2);
}

canonical_decompiled_code:
int __fastcall func10(char *a1, char *a2)
{
func5(a1);
return func7(a2);
}

original_code:
void md_initialize(const char *cls_file, const char *input_file)
{
    read_atoms(cls_file);
    read_inputfile(input_file);
}

canonical_original_code:
void md_initialize(char *cls_file, char *input_file)
{
    read_atoms(cls_file);
    read_inputfile(input_file);
}


decompiled_code:
void md_finalize()
{
free_memory();
}

canonical_decompiled_code:
void func11()
{
func8();
}

original_code:
void md_finalize()
{
    free_memory();
}

canonical_original_code:
void md_finalize()
{
    free_memory();
}


decompiled_code:
void __noreturn exit_error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vsprintf(&buf_0, format, arg);
if ( buf_0 )
fprintf(stderr, "error: %s\n", &buf_0);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vsprintf(&buf_0, format, arg);
if ( buf_0 )
fprintf(stderr, "error: %s\n", &buf_0);
exit(1);
}

original_code:
void exit_error(const char *format, ...)
{
    va_list ap;
    static char buf[ERROR_BUF_MAX];
    __builtin_va_start(ap,format);
    vsprintf(buf, format, ap);
    __builtin_va_end(ap);
    if (strlen(buf) > 0)
        fprintf(stderr, "error: %s\n", buf);
    exit(1);
}

canonical_original_code:
void exit_error(char *format, ...)
{
    __builtin_va_list ap;
    static char buf[-1];
    __builtin_va_start(ap,format);
    vsprintf(buf, format, ap);
    __builtin_va_end(ap);
    if (strlen(buf) > 0)
        fprintf(stderr, "error: %s\n", buf);
    exit(1);
}

variable_types:
format
char *

variable_types:
buf
char [-1]


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

if ( !a1 )
return 0LL;
result = malloc(a1);
if ( !result )
exit_error("xmalloc: No enough memory");
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
void *result; // rax

if ( !a1 )
return 0LL;
result = malloc(a1);
if ( !result )
func0("xmalloc: No enough memory");
return result;
}

original_code:
void *xmalloc(size_t size)
{
    void *p;
    if (size == 0)
        return ((void *)0);
    if ( (p = malloc(size)) == ((void *)0)) {
        exit_error("xmalloc: No enough memory");
    }
    return p;
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
    void *p;
    if (size == 0)
        return ((void *)0);
    if ( (p = malloc(size)) == ((void *)0)) {
        exit_error("xmalloc: No enough memory");
    }
    return p;
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
exit_error("xrealloc: No enough memory");
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
func0("xrealloc: No enough memory");
return result;
}

original_code:
void *xrealloc(void *ptr, size_t size)
{
    void *p;
    if ( (p = realloc(ptr, size)) == ((void *)0)) {
        exit_error("xrealloc: No enough memory");
    }
    return p;
}

canonical_original_code:
void *xrealloc(void *ptr, unsigned long size)
{
    void *p;
    if ( (p = realloc(ptr, size)) == ((void *)0)) {
        exit_error("xrealloc: No enough memory");
    }
    return p;
}

variable_types:
ptr
void *


decompiled_code:
void __fastcall tersoff2_energy_ij(unsigned int a1, unsigned int a2)
{
double v2; // r12
double v3; // r13
double v4; // r14
double v5; // r15

v2 = t2_distance_noPBC();
v3 = t2_f_cutoff(a1, a2);
if ( !(unsigned int)iszero() )
{
v4 = t2_f_repulsive(a1, a2, v2);
v5 = t2_b(a1, a2);
*(double *)&U = (t2_f_attractive(a1, a2, v2) * v5 + v4) * v3 + *(double *)&U;
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned int a1, unsigned int a2)
{
double v2; // r12
double v3; // r13
double v4; // r14
double v5; // r15

v2 = t2_distance_noPBC();
v3 = t2_f_cutoff(a1, a2);
if ( !(unsigned int)iszero() )
{
v4 = t2_f_repulsive(a1, a2, v2);
v5 = t2_b(a1, a2);
*(double *)&U = (t2_f_attractive(a1, a2, v2) * v5 + v4) * v3 + *(double *)&U;
}
}

original_code:
static void tersoff2_energy_ij(int atom_i, int atom_j)
{
    double rij;
    double f_cutoff_val;
    rij = t2_distance_noPBC(atom_i, atom_j);
    f_cutoff_val = t2_f_cutoff(atom_i, atom_j, rij);
    if (!iszero(f_cutoff_val)) {
        double Uij;
        Uij = f_cutoff_val *
              (t2_f_repulsive(atom_i, atom_j, rij) +
               t2_b(atom_i, atom_j) *
               t2_f_attractive(atom_i, atom_j, rij));
        U += Uij;
    }
}

variable_types: rij double

variable_types: Uij double


decompiled_code:
void __noreturn tersoff2_energy()
{
celllist_scan_atoms(tersoff2_energy_ij);
exit(0);
}

canonical_decompiled_code:
void __noreturn func1()
{
celllist_scan_atoms(tersoff2_energy_ij);
exit(0);
}

original_code:
void tersoff2_energy()
{
    celllist_scan_atoms(tersoff2_energy_ij);
    exit(0);
}


decompiled_code:
int __fastcall print_array(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

original_code:
void print_array(int a[], int num_elements){
   int i;
   for(i=0; i<num_elements; i++)
   {
     printf("%d ", a[i]);
   }
   printf("\n");
}

variable_types:
a
int []

variable_types: i int


original_code:
int max_array(int a[], int num_elements){
   int i, max=-32000;
   for (i=0; i<num_elements; i++){
     if (a[i]>max){
        max=a[i];
     }
   }
   return(max);
}

canonical_original_code:
int max_array(int a[], int num_elements){
   int i; int max = -32000;
   for (i=0; i<num_elements; i++){
     if (a[i]>max){
        max=a[i];
     }
   }
   return(max);
}

variable_types: max int


randomizer

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

v2 = time(0LL);
v3 = rand();
srand(v3 + v2);
return (unsigned int)(rand() % (a1 + 1) + a2 - a1);
}

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

v2 = time(0LL);
v3 = rand();
srand(v3 + v2);
return (unsigned int)(rand() % (a1 + 1) + a2 - a1);
}

original_code:
int randomizer(int min, int max){
    int x;
    srand (time(((void *)0))+rand());
    x = (int)(rand() % (min+1) + (max-min));
    return x;
}

variable_types: min int

variable_types: x int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
__int64 v5; // [rsp+0h] [rbp-9FB8h] BYREF
_QWORD v6[102]; // [rsp+9C40h] [rbp-378h] BYREF
int v7[18]; // [rsp+9F70h] [rbp-48h] BYREF

for ( i = 1LL; i != 11; ++i )
*((_DWORD *)&v6[101] + i + 1) = atoi(argv[i]);
generator(v6, v7);
optimalAlgorithm(v6, (__int64)&v5);
return 0;
}

original_code:
int main(int argc, char *argv[]){
    int restr[10];
    int i;
    for(i=0; i <= 10 -1; i++){
        restr[i] = atoi(argv[i+1]);
    }
    knapsack sack = generator(restr);
    table t[50][50];
    optimalAlgorithm(&sack, t);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
    int restr[10];
    int i;
    for(i=0; i <= 10 -1; i++){
        restr[i] = atoi(argv[i+1]);
    }
    struct struct2 sack = generator(restr);
    struct struct3 t[50][50];
    optimalAlgorithm(&sack, t);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
restr
int [10]

variable_types: struct struct2 { int capacity; int objectsNum; struct struct1 { int name; int cost; int value; int copies; } objects[50]; }

variable_types: struct struct3 { int name; int value; int copiesNum; short color; } [50][50]

user_defined_types: struct struct2 { int capacity; int objectsNum; struct struct1 objects[50]; }


repo: apk/asciztalker

original_code:
void buf_init (struct buf *b) {
  b->pos = 0;
  b->fill = 0;
}

canonical_original_code:
void buf_init(struct buf *b) {
  b->pos = 0;
  b->fill = 0;
}

variable_types: struct buf { char buf[1024]; int fill; int pos; } *


decompiled_code:
__int64 __fastcall buf_appendz(__int64 a1, __int64 a2)
{
__int64 result; // rax
char v3; // dl

do
{
LODWORD(result) = *(_DWORD *)(a1 + 1024);
if ( (unsigned int)result > 0x3FF )
{
fwrite("Overflow\n", 1uLL, 9uLL, stderr);
exit(1);
}
++a2;
*(_DWORD *)(a1 + 1024) = result + 1;
v3 = *(_BYTE *)(a2 - 1);
result = (int)result;
*(_BYTE *)(a1 + (int)result) = v3;
}
while ( v3 );
return result;
}

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

do
{
LODWORD(result) = *(_DWORD *)(a1 + 1024);
if ( (unsigned int)result > 0x3FF )
{
fwrite("Overflow\n", 1uLL, 9uLL, stderr);
exit(1);
}
++a2;
*(_DWORD *)(a1 + 1024) = result + 1;
v3 = *(_BYTE *)(a2 - 1);
result = (int)result;
*(_BYTE *)(a1 + (int)result) = v3;
}
while ( v3 );
return result;
}

original_code:
void buf_appendz (struct buf *b, char *p) {
  do {
    if (b->fill >= sizeof (b->buf)) {
      fprintf (stderr, "Overflow\n");
      exit (1);
    }
  } while ((b->buf [b->fill ++] = *p ++));
}

canonical_original_code:
void buf_appendz(struct buf *b, char *p) {
  do {
    if (b->fill >= sizeof (b->buf)) {
      fprintf (stderr, "Overflow\n");
      exit (1);
    }
  } while ((b->buf [b->fill ++] = *p ++));
}

variable_types:
p
char *

user_defined_types: struct buf { char buf[1024]; int fill; int pos; }


decompiled_code:
_BOOL8 __fastcall buf_rd_p(__int64 a1)
{
return *(_DWORD *)(a1 + 1024) <= 0x3FFu;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(__int64 a1)
{
return *(_DWORD *)(a1 + 1024) <= 0x3FFu;
}

original_code:
int buf_rd_p (struct buf *b) {
  return b->fill < sizeof (b->buf);
}

canonical_original_code:
int buf_rd_p(struct buf *b) {
  return b->fill < sizeof (b->buf);
}


original_code:
int buf_wr_p (struct buf *b) {
  return b->pos < b->fill;
}

canonical_original_code:
int buf_wr_p(struct buf *b) {
  return b->pos < b->fill;
}


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

result = read(a2, (void *)(a1 + *(int *)(a1 + 1024)), 1024 - *(_DWORD *)(a1 + 1024));
if ( (_DWORD)result == -1 )
{
perror("read");
exit(1);
}
if ( !(_DWORD)result )
{
fwrite("EOF\n", 1uLL, 4uLL, stderr);
kill(pid, 2);
exit(1);
}
*(_DWORD *)(a1 + 1024) += result;
return result;
}

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

result = read(a2, (void *)(a1 + *(int *)(a1 + 1024)), 1024 - *(_DWORD *)(a1 + 1024));
if ( (_DWORD)result == -1 )
{
perror("read");
exit(1);
}
if ( !(_DWORD)result )
{
fwrite("EOF\n", 1uLL, 4uLL, stderr);
kill(pid, 2);
exit(1);
}
*(_DWORD *)(a1 + 1024) += result;
return result;
}

original_code:
void buf_rd_x (struct buf *b, int fd) {
  int sz = sizeof (b->buf) - b->fill;
  int r = read (fd, b->buf + b->fill, sz);
  if (r == -1) {
    perror ("read");
    exit (1);
  }
  if (r == 0) {
    fprintf (stderr, "EOF\n");
    kill (pid, 2);
    exit (1);
  }
  b->fill += r;
}

canonical_original_code:
void buf_rd_x(struct buf *b, int fd) {
  int sz = sizeof (b->buf) - b->fill;
  int r = read (fd, b->buf + b->fill, sz);
  if (r == -1) {
    perror ("read");
    exit (1);
  }
  if (r == 0) {
    fprintf (stderr, "EOF\n");
    kill (pid, 2);
    exit (1);
  }
  b->fill += r;
}

variable_types: fd int

variable_types: sz int

variable_types: r int


decompiled_code:
__int64 __fastcall buf_wr_x(void *dest, int fd)
{
int v2; // eax
__int64 result; // rax

v2 = write(fd, (char *)dest + *((int *)dest + 257), *((_DWORD *)dest + 256) - *((_DWORD *)dest + 257));
if ( v2 == -1 )
{
perror("read");
exit(1);
}
if ( !v2 )
{
fwrite("FULL?\n", 1uLL, 6uLL, stderr);
exit(1);
}
result = (unsigned int)(*((_DWORD *)dest + 257) + v2);
*((_DWORD *)dest + 257) = result;
if ( (unsigned int)result > 0x200 )
{
memmove(dest, (char *)dest + (int)result, *((_DWORD *)dest + 256) - (int)result);
result = *((unsigned int *)dest + 257);
*((_DWORD *)dest + 256) -= result;
*((_DWORD *)dest + 257) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(void *dest, int fd)
{
int v2; // eax
__int64 result; // rax

v2 = write(fd, (char *)dest + *((int *)dest + 257), *((_DWORD *)dest + 256) - *((_DWORD *)dest + 257));
if ( v2 == -1 )
{
perror("read");
exit(1);
}
if ( !v2 )
{
fwrite("FULL?\n", 1uLL, 6uLL, stderr);
exit(1);
}
result = (unsigned int)(*((_DWORD *)dest + 257) + v2);
*((_DWORD *)dest + 257) = result;
if ( (unsigned int)result > 0x200 )
{
memmove(dest, (char *)dest + (int)result, *((_DWORD *)dest + 256) - (int)result);
result = *((unsigned int *)dest + 257);
*((_DWORD *)dest + 256) -= result;
*((_DWORD *)dest + 257) = 0;
}
return result;
}

original_code:
void buf_wr_x (struct buf *b, int fd) {
  int sz = b->fill - b->pos;
  int r = write (fd, b->buf + b->pos, sz);
  if (r == -1) {
    perror ("read");
    exit (1);
  }
  if (r == 0) {
    fprintf (stderr, "FULL?\n");
    exit (1);
  }
  b->pos += r;
  if (b->pos > sizeof (b->buf) / 2) {
    memmove (b->buf, b->buf + b->pos, b->fill - b->pos);
    b->fill -= b->pos;
    b->pos = 0;
  }
}

canonical_original_code:
void buf_wr_x(struct buf *b, int fd) {
  int sz = b->fill - b->pos;
  int r = write (fd, b->buf + b->pos, sz);
  if (r == -1) {
    perror ("read");
    exit (1);
  }
  if (r == 0) {
    fprintf (stderr, "FULL?\n");
    exit (1);
  }
  b->pos += r;
  if (b->pos > sizeof (b->buf) / 2) {
    memmove (b->buf, b->buf + b->pos, b->fill - b->pos);
    b->fill -= b->pos;
    b->pos = 0;
  }
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__pid_t v4; // eax
int v5; // ebp
int v6; // eax
__int64 i; // r14
fd_set readfds; // [rsp+10h] [rbp-958h] BYREF
fd_set writefds; // [rsp+90h] [rbp-8D8h] BYREF
char v10[1024]; // [rsp+110h] [rbp-858h] BYREF
int v11; // [rsp+510h] [rbp-458h]
unsigned int v12; // [rsp+514h] [rbp-454h]
char dest[1032]; // [rsp+520h] [rbp-448h] BYREF
int fds[2]; // [rsp+928h] [rbp-40h] BYREF

v3 = getenv("yyx");
if ( socketpair(1, 1, 0, fds) != -1 )
{
if ( v3 )
{
v4 = fork();
pid = v4;
if ( v4 != -1 )
{
if ( !v4 )
{
dup2(fds[1], 0);
dup2(fds[1], 1);
close(fds[0]);
execvp(argv[1], (char *const *)argv + 1);
_exit(4);
}
buf_init((__int64)v10);
buf_init((__int64)dest);
buf_appendz((__int64)dest, (__int64)v3);
close(fds[1]);
v5 = 0;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
if ( v5 && buf_rd_p((__int64)dest) )
readfds.fds_bits[0] |= 1uLL;
if ( buf_wr_p((__int64)dest) )
writefds.fds_bits[fds[0] / 64] |= 1LL << (fds[0] % 64);
if ( buf_rd_p((__int64)v10) )
readfds.fds_bits[fds[0] / 64] |= 1LL << (fds[0] % 64);
if ( v5 && buf_wr_p((__int64)v10) )
writefds.fds_bits[0] |= 2uLL;
v6 = select(fds[0] + 1, &readfds, &writefds, 0LL, 0LL);
if ( v6 == -1 )
{
perror("select");
kill(pid, 2);
exit(1);
}
if ( !v6 )
{
fwrite("Oops\n", 1uLL, 5uLL, stderr);
kill(pid, 2);
exit(1);
}
if ( v5 && (readfds.fds_bits[0] & 1) != 0 )
buf_rd_x((__int64)dest, 0);
if ( (writefds.fds_bits[fds[0] / 64] & (1LL << (fds[0] % 64))) != 0 )
buf_wr_x(dest, fds[0]);
if ( (readfds.fds_bits[fds[0] / 64] & (1LL << (fds[0] % 64))) != 0 )
buf_rd_x((__int64)v10, fds[0]);
if ( v5 )
{
if ( (writefds.fds_bits[0] & 2) != 0 )
buf_wr_x(v10, 1);
}
else
{
fprintf(stderr, "up: %d..%d\n", v12, (unsigned int)v11);
for ( i = (int)v12; v11 > (int)i; ++i )
{
fprintf(stderr, " [%d]: %d\n", (unsigned int)i, (unsigned int)v10[i]);
if ( !v10[i] )
{
if ( v12 != (_DWORD)i )
{
fprintf(stderr, "FAIL '%s'\n", &v10[v12]);
kill(pid, 2);
exit(1);
}
v12 = i + 1;
fwrite("Good!\n", 1uLL, 6uLL, stderr);
v5 = 1;
}
}
}
}
}
perror("fork");
exit(1);
}
fwrite("no target", 1uLL, 9uLL, stderr);
exit(1);
}
perror("socketpair");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__pid_t v4; // eax
int v5; // ebp
int v6; // eax
__int64 i; // r14
fd_set readfds; // [rsp+10h] [rbp-958h] BYREF
fd_set writefds; // [rsp+90h] [rbp-8D8h] BYREF
char v10[1024]; // [rsp+110h] [rbp-858h] BYREF
int v11; // [rsp+510h] [rbp-458h]
unsigned int v12; // [rsp+514h] [rbp-454h]
char dest[1032]; // [rsp+520h] [rbp-448h] BYREF
int fds[2]; // [rsp+928h] [rbp-40h] BYREF

v3 = getenv("yyx");
if ( socketpair(1, 1, 0, fds) != -1 )
{
if ( v3 )
{
v4 = fork();
pid = v4;
if ( v4 != -1 )
{
if ( !v4 )
{
dup2(fds[1], 0);
dup2(fds[1], 1);
close(fds[0]);
execvp(argv[1], (char *const *)argv + 1);
_exit(4);
}
func7((__int64)v10);
func7((__int64)dest);
func8((__int64)dest, (__int64)v3);
close(fds[1]);
v5 = 0;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
if ( v5 && func9((__int64)dest) )
readfds.fds_bits[0] |= 1uLL;
if ( func10((__int64)dest) )
writefds.fds_bits[fds[0] / 64] |= 1LL << (fds[0] % 64);
if ( func9((__int64)v10) )
readfds.fds_bits[fds[0] / 64] |= 1LL << (fds[0] % 64);
if ( v5 && func10((__int64)v10) )
writefds.fds_bits[0] |= 2uLL;
v6 = select(fds[0] + 1, &readfds, &writefds, 0LL, 0LL);
if ( v6 == -1 )
{
perror("select");
kill(pid, 2);
exit(1);
}
if ( !v6 )
{
fwrite("Oops\n", 1uLL, 5uLL, stderr);
kill(pid, 2);
exit(1);
}
if ( v5 && (readfds.fds_bits[0] & 1) != 0 )
func11((__int64)dest, 0);
if ( (writefds.fds_bits[fds[0] / 64] & (1LL << (fds[0] % 64))) != 0 )
func12(dest, fds[0]);
if ( (readfds.fds_bits[fds[0] / 64] & (1LL << (fds[0] % 64))) != 0 )
func11((__int64)v10, fds[0]);
if ( v5 )
{
if ( (writefds.fds_bits[0] & 2) != 0 )
func12(v10, 1);
}
else
{
fprintf(stderr, "up: %d..%d\n", v12, (unsigned int)v11);
for ( i = (int)v12; v11 > (int)i; ++i )
{
fprintf(stderr, " [%d]: %d\n", (unsigned int)i, (unsigned int)v10[i]);
if ( !v10[i] )
{
if ( v12 != (_DWORD)i )
{
fprintf(stderr, "FAIL '%s'\n", &v10[v12]);
kill(pid, 2);
exit(1);
}
v12 = i + 1;
fwrite("Good!\n", 1uLL, 6uLL, stderr);
v5 = 1;
}
}
}
}
}
perror("fork");
exit(1);
}
fwrite("no target", 1uLL, 9uLL, stderr);
exit(1);
}
perror("socketpair");
exit(1);
}

original_code:
int main (int argc, char **argv) {
  int fds [2];
  char *tg = getenv ("yyx");
  int r = socketpair (1, SOCK_STREAM, 0, fds);
  if (r == -1) {
    perror ("socketpair");
    exit (1);
  }
  if (!tg) {
    fprintf (stderr, "no target");
    exit (1);
  }
  pid = fork ();
  if (pid == -1) {
    perror ("fork");
    exit (1);
  } else if (pid == 0) {
    dup2 (fds [1], 0);
    dup2 (fds [1], 1);
    close (fds [0]);
    execvp (argv [1], argv + 1);
    _exit (4);
  } else {
    int op = 0;
    struct buf up, down;
    buf_init (&up);
    buf_init (&down);
    buf_appendz (&down, tg);
    close (fds [1]);
    while (1) {
      fd_set R, W;
      do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&R)->__fds_bits)[0]) : "memory"); } while (0);
      do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&W)->__fds_bits)[0]) : "memory"); } while (0);
      if (op && buf_rd_p (&down)) {
 ((void) (((&R)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (buf_wr_p (&down)) {
 ((void) (((&W)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (buf_rd_p (&up)) {
 ((void) (((&R)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (op && buf_wr_p (&up)) {
 ((void) (((&W)->__fds_bits)[((1) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((1) % (8 * (int) sizeof (__fd_mask)))))));
      }
      r = select (fds [0] + 1, &R, &W, 0, 0);
      if (r == -1) {
 perror ("select");
 kill (pid, 2);
 exit (1);
      }
      if (r == 0) {
 fprintf (stderr, "Oops\n");
 kill (pid, 2);
 exit (1);
      }
      if (op && ((((&R)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_rd_x (&down, 0);
      }
      if (((((&W)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_wr_x (&down, fds [0]);
      }
      if (((((&R)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_rd_x (&up, fds [0]);
      }
      if (op && ((((&W)->__fds_bits)[((1) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((1) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_wr_x (&up, 1);
      }
      if (!op) {
 int i;
 fprintf (stderr, "up: %d..%d\n", up.pos, up.fill);
 for (i = up.pos; i < up.fill; i ++) {
   fprintf (stderr, "  [%d]: %d\n", i, up.buf [i]);
   if (up.buf [i] == 0) {
     if (i == up.pos) {
       up.pos ++;
       fprintf (stderr, "Good!\n");
       op = 1;
       continue;
     } else {
       fprintf (stderr, "FAIL '%s'\n", up.buf + up.pos);
       kill (pid, 2);
       exit (1);
     }
   }
 }
      }
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  int fds[2];
  char *tg = getenv ("yyx");
  int r = socketpair (1, SOCK_STREAM, 0, fds);
  if (r == -1) {
    perror ("socketpair");
    exit (1);
  }
  if (!tg) {
    fprintf (stderr, "no target");
    exit (1);
  }
  pid = fork ();
  if (pid == -1) {
    perror ("fork");
    exit (1);
  } else if (pid == 0) {
    dup2 (fds [1], 0);
    dup2 (fds [1], 1);
    close (fds [0]);
    execvp (argv [1], argv + 1);
    _exit (4);
  } else {
    int op = 0;
    struct buf up; struct buf down;
    buf_init (&up);
    buf_init (&down);
    buf_appendz (&down, tg);
    close (fds [1]);
    while (1) {
      struct fd_set R; struct fd_set W;
      do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&R)->__fds_bits)[0]) : "memory"); } while (0);
      do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&W)->__fds_bits)[0]) : "memory"); } while (0);
      if (op && buf_rd_p (&down)) {
 ((void) (((&R)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (buf_wr_p (&down)) {
 ((void) (((&W)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (buf_rd_p (&up)) {
 ((void) (((&R)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (op && buf_wr_p (&up)) {
 ((void) (((&W)->__fds_bits)[((1) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((1) % (8 * (int) sizeof (__fd_mask)))))));
      }
      r = select (fds [0] + 1, &R, &W, 0, 0);
      if (r == -1) {
 perror ("select");
 kill (pid, 2);
 exit (1);
      }
      if (r == 0) {
 fprintf (stderr, "Oops\n");
 kill (pid, 2);
 exit (1);
      }
      if (op && ((((&R)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_rd_x (&down, 0);
      }
      if (((((&W)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_wr_x (&down, fds [0]);
      }
      if (((((&R)->__fds_bits)[((fds [0]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fds [0]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_rd_x (&up, fds [0]);
      }
      if (op && ((((&W)->__fds_bits)[((1) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((1) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 buf_wr_x (&up, 1);
      }
      if (!op) {
 int i;
 fprintf (stderr, "up: %d..%d\n", up.pos, up.fill);
 for (i = up.pos; i < up.fill; i ++) {
   fprintf (stderr, "  [%d]: %d\n", i, up.buf [i]);
   if (up.buf [i] == 0) {
     if (i == up.pos) {
       up.pos ++;
       fprintf (stderr, "Good!\n");
       op = 1;
       continue;
     } else {
       fprintf (stderr, "FAIL '%s'\n", up.buf + up.pos);
       kill (pid, 2);
       exit (1);
     }
   }
 }
      }
    }
  }
  return 0;
}

variable_types:
argv
char **

variable_types:
fds
int [2]

variable_types:
tg
char *

variable_types: op int

variable_types: struct buf { char buf[1024]; int fill; int pos; }

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

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


decompiled_code:
void *__fastcall token_bucket_func(void *a1)
{
__useconds_t v1; // ebx
double v2; // xmm0_8
unsigned int **v3; // rbp
unsigned int *v4; // r12
int v5; // edx
double v6; // xmm1_8

v1 = (int)(1000.0 / *(double *)&r * 1000.0);
do
{
usleep(v1);
gettimeofday(&tim, 0LL);
v2 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
*(double *)&t7 = v2;
if ( B <= token_counter )
{
printf("%012.3fms: Token t%d arrives, dropped\n", (v2 - *(double *)&t1) * 1000.0, (unsigned int)token_no);
++drop_tokens;
++token_no;
}
else
{
pthread_mutex_lock(&mut_lock);
printf(
"%012.3fms: Token t%d arrives, token bucket now has %d tokens\n",
(*(double *)&t7 - *(double *)&t1) * 1000.0,
(unsigned int)++token_no,
(unsigned int)++token_counter);
pthread_mutex_unlock(&mut_lock);
if ( !(unsigned int)My402ListEmpty(&Queue_one) )
{
v3 = (unsigned int **)My402ListFirst(&Queue_one);
v4 = *v3;
pthread_mutex_lock(&mut_lock);
v5 = v4[4];
if ( v5 <= token_counter )
{
token_counter -= v5;
My402ListUnlink(&Queue_one, v3);
++packet_count_Q1exit;
gettimeofday(&tim, 0LL);
*(double *)&t9 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
v6 = *(double *)&t9 - *((double *)v4 + 4);
*(double *)&time_in_Q1 = v6 + *(double *)&time_in_Q1;
printf(
"%012.3fms: Packet p%d leaves Q1, time in Q1: %.3fms, token bucket now has %d tokens\n",
(*(double *)&t9 - *(double *)&t1) * 1000.0,
*v4,
v6 * 1000.0,
(unsigned int)token_counter);
*((_QWORD *)v4 + 5) = t9;
My402ListAppend(&Queue_two, v4);
pthread_cond_signal(&signal_server);
gettimeofday(&tim, 0LL);
*(double *)&t10 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
printf("%012.3fms: Packet p%d enters Q2\n", (*(double *)&t10 - *(double *)&t1) * 1000.0, *v4);
}
pthread_mutex_unlock(&mut_lock);
}
}
}
while ( packet_count_Q1exit != num_afterdrop );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
__useconds_t v1; // ebx
double v2; // xmm0_8
unsigned int **v3; // rbp
unsigned int *v4; // r12
int v5; // edx
double v6; // xmm1_8

v1 = (int)(1000.0 / *(double *)&r * 1000.0);
do
{
usleep(v1);
gettimeofday(&tim, 0LL);
v2 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
*(double *)&t7 = v2;
if ( B <= token_counter )
{
printf("%012.3fms: Token t%d arrives, dropped\n", (v2 - *(double *)&t1) * 1000.0, (unsigned int)token_no);
++drop_tokens;
++token_no;
}
else
{
pthread_mutex_lock(&mut_lock);
printf(
"%012.3fms: Token t%d arrives, token bucket now has %d tokens\n",
(*(double *)&t7 - *(double *)&t1) * 1000.0,
(unsigned int)++token_no,
(unsigned int)++token_counter);
pthread_mutex_unlock(&mut_lock);
if ( !(unsigned int)My402ListEmpty(&Queue_one) )
{
v3 = (unsigned int **)My402ListFirst(&Queue_one);
v4 = *v3;
pthread_mutex_lock(&mut_lock);
v5 = v4[4];
if ( v5 <= token_counter )
{
token_counter -= v5;
My402ListUnlink(&Queue_one, v3);
++packet_count_Q1exit;
gettimeofday(&tim, 0LL);
*(double *)&t9 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
v6 = *(double *)&t9 - *((double *)v4 + 4);
*(double *)&time_in_Q1 = v6 + *(double *)&time_in_Q1;
printf(
"%012.3fms: Packet p%d leaves Q1, time in Q1: %.3fms, token bucket now has %d tokens\n",
(*(double *)&t9 - *(double *)&t1) * 1000.0,
*v4,
v6 * 1000.0,
(unsigned int)token_counter);
*((_QWORD *)v4 + 5) = t9;
My402ListAppend(&Queue_two, v4);
pthread_cond_signal(&signal_server);
gettimeofday(&tim, 0LL);
*(double *)&t10 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
printf("%012.3fms: Packet p%d enters Q2\n", (*(double *)&t10 - *(double *)&t1) * 1000.0, *v4);
}
pthread_mutex_unlock(&mut_lock);
}
}
}
while ( packet_count_Q1exit != num_afterdrop );
return 0LL;
}

original_code:
void *token_bucket_func()
{
 My402ListElem *Queue1_elem = ((void *)0);
 MyPacketInfo *Queue1_head_data = ((void *)0);
 double token_arrival = (1000/r);
 double token_sleep = token_arrival *1000;
 int token_sleep1 = (int)token_sleep;
 while(1)
 {
   usleep(token_sleep1);
   gettimeofday(&tim,((void *)0));
   t7 = tim.tv_sec + (tim.tv_usec)/1000000.0;
   if ( token_counter > (B-1) )
   {
    printf("%012.3fms: Token t%d arrives, dropped\n",(t7-t1)*1000,token_no);
    drop_tokens ++;
    token_no ++;
   }
   else
   {
    pthread_mutex_lock(&mut_lock);
    token_counter ++;
    token_no ++;
    printf("%012.3fms: Token t%d arrives, token bucket now has %d tokens\n", (t7-t1)*1000, token_no, token_counter);
    pthread_mutex_unlock(&mut_lock);
    if(!My402ListEmpty(&Queue_one))
    {
     Queue1_elem = My402ListFirst(&Queue_one);
     Queue1_head_data = (MyPacketInfo*)Queue1_elem->obj;
     pthread_mutex_lock(&mut_lock);
     if ( Queue1_head_data->P <= token_counter )
     {
      token_counter = token_counter - Queue1_head_data->P;
      My402ListUnlink(&Queue_one, Queue1_elem);
      packet_count_Q1exit ++;
      gettimeofday(&tim,((void *)0));
      t9 = tim.tv_sec + (tim.tv_usec)/1000000.0;
      double time_Q1 = (t9-Queue1_head_data->Q1_entry_time);
      time_in_Q1 = time_in_Q1 + time_Q1;
      printf("%012.3fms: Packet p%d leaves Q1, time in Q1: %.3fms, token bucket now has %d tokens\n",(t9-t1)*1000, Queue1_head_data->packet_no, (t9-(Queue1_head_data->Q1_entry_time))*1000, token_counter);
      Queue1_head_data->Q2_entry_time = t9;
      My402ListAppend(&Queue_two,(void*)Queue1_head_data);
      pthread_cond_signal(&signal_server);
      gettimeofday(&tim,((void *)0));
      t10 = tim.tv_sec + (tim.tv_usec)/1000000.0;
      printf("%012.3fms: Packet p%d enters Q2\n", (t10-t1)*1000, Queue1_head_data->packet_no);
     }
     pthread_mutex_unlock(&mut_lock);
    }
   }
   if( packet_count_Q1exit == num_afterdrop )
   {
    break;
   }
 }
 return 0;
}

canonical_original_code:
void *token_bucket_func()
{
 struct tagMy402ListElem *Queue1_elem = ((void *)0);
 struct myPacketInfoStructure *Queue1_head_data = ((void *)0);
 double token_arrival = (1000/r);
 double token_sleep = token_arrival *1000;
 int token_sleep1 = (int)token_sleep;
 while(1)
 {
   usleep(token_sleep1);
   gettimeofday(&tim,((void *)0));
   t7 = tim.tv_sec + (tim.tv_usec)/1000000.0;
   if ( token_counter > (B-1) )
   {
    printf("%012.3fms: Token t%d arrives, dropped\n",(t7-t1)*1000,token_no);
    drop_tokens ++;
    token_no ++;
   }
   else
   {
    pthread_mutex_lock(&mut_lock);
    token_counter ++;
    token_no ++;
    printf("%012.3fms: Token t%d arrives, token bucket now has %d tokens\n", (t7-t1)*1000, token_no, token_counter);
    pthread_mutex_unlock(&mut_lock);
    if(!My402ListEmpty(&Queue_one))
    {
     Queue1_elem = My402ListFirst(&Queue_one);
     Queue1_head_data = (struct myPacketInfoStructure *)Queue1_elem->obj;
     pthread_mutex_lock(&mut_lock);
     if ( Queue1_head_data->P <= token_counter )
     {
      token_counter = token_counter - Queue1_head_data->P;
      My402ListUnlink(&Queue_one, Queue1_elem);
      packet_count_Q1exit ++;
      gettimeofday(&tim,((void *)0));
      t9 = tim.tv_sec + (tim.tv_usec)/1000000.0;
      double time_Q1 = (t9-Queue1_head_data->Q1_entry_time);
      time_in_Q1 = time_in_Q1 + time_Q1;
      printf("%012.3fms: Packet p%d leaves Q1, time in Q1: %.3fms, token bucket now has %d tokens\n",(t9-t1)*1000, Queue1_head_data->packet_no, (t9-(Queue1_head_data->Q1_entry_time))*1000, token_counter);
      Queue1_head_data->Q2_entry_time = t9;
      My402ListAppend(&Queue_two,(void *)Queue1_head_data);
      pthread_cond_signal(&signal_server);
      gettimeofday(&tim,((void *)0));
      t10 = tim.tv_sec + (tim.tv_usec)/1000000.0;
      printf("%012.3fms: Packet p%d enters Q2\n", (t10-t1)*1000, Queue1_head_data->packet_no);
     }
     pthread_mutex_unlock(&mut_lock);
    }
   }
   if( packet_count_Q1exit == num_afterdrop )
   {
    break;
   }
 }
 return 0;
}

variable_types: struct tagMy402ListElem { void *obj; struct tagMy402ListElem *next; struct tagMy402ListElem *prev; } *

variable_types: struct myPacketInfoStructure { int packet_no; double pkt_arrival; int P; double pkt_service; double Q1_entry_time; double Q2_entry_time; } *


decompiled_code:
void *__fastcall server_func(void *a1)
{
unsigned int **v1; // rax
unsigned int *v2; // rbx
__useconds_t v3; // ebp
double v4; // xmm2_8

do
{
pthread_mutex_lock(&mut_lock);
if ( (unsigned int)My402ListEmpty(&Queue_two) )
pthread_cond_wait(&signal_server, &mut_lock);
v1 = (unsigned int **)My402ListFirst(&Queue_two);
v2 = *v1;
My402ListUnlink(&Queue_two, v1);
gettimeofday(&tim, 0LL);
*(double *)&t14 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
printf(
"%012.3fms: Packet p%d begins service at S, time in Q2 = %.3fms\n",
(*(double *)&t14 - *(double *)&t1) * 1000.0,
*v2,
(*(double *)&t14 - *((double *)v2 + 5)) * 1000.0);
*(double *)&time_in_Q2 = *(double *)&t14 - *((double *)v2 + 5) + *(double *)&time_in_Q2;
gettimeofday(&tim, 0LL);
*(double *)&t12 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
++packet_count_Q2exit;
v3 = (int)(1000.0 * *((double *)v2 + 3));
pthread_mutex_unlock(&mut_lock);
usleep(v3);
gettimeofday(&tim, 0LL);
*(double *)&t8 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
*(double *)&service_time = *(double *)&t8 - *(double *)&t12 + *(double *)&service_time;
++server_count;
v4 = *(double *)&t8 - *((double *)v2 + 4);
*(double *)&time_in_sys = v4 + *(double *)&time_in_sys;
*(double *)&time_sd = v4 * v4 / (double)(server_count * server_count) + *(double *)&time_sd;
printf(
"%012.3fms: Packet p%d departs S, service time: %.3fms, time in system: %.3fms\n",
(*(double *)&t8 - *(double *)&t1) * 1000.0,
*v2,
*((double *)v2 + 3),
v4 * 1000.0);
*(double *)&time_in_S = *(double *)&t8 - *(double *)&t12 + *(double *)&time_in_S;
}
while ( packet_count_Q2exit != num_afterdrop && packet_Q1enter );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
unsigned int **v1; // rax
unsigned int *v2; // rbx
__useconds_t v3; // ebp
double v4; // xmm2_8

do
{
pthread_mutex_lock(&mut_lock);
if ( (unsigned int)My402ListEmpty(&Queue_two) )
pthread_cond_wait(&signal_server, &mut_lock);
v1 = (unsigned int **)My402ListFirst(&Queue_two);
v2 = *v1;
My402ListUnlink(&Queue_two, v1);
gettimeofday(&tim, 0LL);
*(double *)&t14 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
printf(
"%012.3fms: Packet p%d begins service at S, time in Q2 = %.3fms\n",
(*(double *)&t14 - *(double *)&t1) * 1000.0,
*v2,
(*(double *)&t14 - *((double *)v2 + 5)) * 1000.0);
*(double *)&time_in_Q2 = *(double *)&t14 - *((double *)v2 + 5) + *(double *)&time_in_Q2;
gettimeofday(&tim, 0LL);
*(double *)&t12 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
++packet_count_Q2exit;
v3 = (int)(1000.0 * *((double *)v2 + 3));
pthread_mutex_unlock(&mut_lock);
usleep(v3);
gettimeofday(&tim, 0LL);
*(double *)&t8 = (double)(int)tim.tv_usec / 1000000.0 + (double)SLODWORD(tim.tv_sec);
*(double *)&service_time = *(double *)&t8 - *(double *)&t12 + *(double *)&service_time;
++server_count;
v4 = *(double *)&t8 - *((double *)v2 + 4);
*(double *)&time_in_sys = v4 + *(double *)&time_in_sys;
*(double *)&time_sd = v4 * v4 / (double)(server_count * server_count) + *(double *)&time_sd;
printf(
"%012.3fms: Packet p%d departs S, service time: %.3fms, time in system: %.3fms\n",
(*(double *)&t8 - *(double *)&t1) * 1000.0,
*v2,
*((double *)v2 + 3),
v4 * 1000.0);
*(double *)&time_in_S = *(double *)&t8 - *(double *)&t12 + *(double *)&time_in_S;
}
while ( packet_count_Q2exit != num_afterdrop && packet_Q1enter );
return 0LL;
}

original_code:
void *server_func()
{
 while(1)
 {
  pthread_mutex_lock(&mut_lock);
  if (My402ListEmpty(&Queue_two))
   pthread_cond_wait(&signal_server, &mut_lock);
  My402ListElem *Queue2_elem = ((void *)0);
  MyPacketInfo *Q2_head_data = ((void *)0);
  Queue2_elem = My402ListFirst(&Queue_two);
  Q2_head_data = (MyPacketInfo*)Queue2_elem->obj;
  My402ListUnlink(&Queue_two, Queue2_elem);
  gettimeofday(&tim,((void *)0));
  t14 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  printf("%012.3fms: Packet p%d begins service at S, time in Q2 = %.3fms\n",(t14-t1)*1000, Q2_head_data->packet_no, (t14-(Q2_head_data->Q2_entry_time))*1000);
  double time_Q2 = (t14-Q2_head_data->Q2_entry_time);
  time_in_Q2 = time_in_Q2 + time_Q2;
  gettimeofday(&tim,((void *)0));
  t12 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  packet_count_Q2exit ++;
  double server_sleep = (Q2_head_data->pkt_service) * 1000;
  int server_sleep1 = (int)server_sleep;
  pthread_mutex_unlock(&mut_lock);
  usleep(server_sleep1);
  gettimeofday(&tim,((void *)0));
  t8 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  double serv_time = t8-t12;
  service_time = service_time + serv_time;
  server_count ++;
  double time_sys = (t8-(Q2_head_data->Q1_entry_time));
  time_in_sys = time_in_sys + time_sys;
  time_sd = time_sd + (( time_sys * time_sys )/( server_count * server_count ));
  printf("%012.3fms: Packet p%d departs S, service time: %.3fms, time in system: %.3fms\n",(t8-t1)*1000, Q2_head_data->packet_no, Q2_head_data->pkt_service,(t8-(Q2_head_data->Q1_entry_time))*1000);
  double time_S = (t8-t12);
  time_in_S = time_in_S + time_S;
  if( packet_count_Q2exit == num_afterdrop )
  {
   break;
  }
  else if( packet_Q1enter == 0)
  {
   break;
  }
 }
 return 0;
}

canonical_original_code:
void *server_func()
{
 while(1)
 {
  pthread_mutex_lock(&mut_lock);
  if (My402ListEmpty(&Queue_two))
   pthread_cond_wait(&signal_server, &mut_lock);
  struct tagMy402ListElem *Queue2_elem = ((void *)0);
  struct myPacketInfoStructure *Q2_head_data = ((void *)0);
  Queue2_elem = My402ListFirst(&Queue_two);
  Q2_head_data = (struct myPacketInfoStructure *)Queue2_elem->obj;
  My402ListUnlink(&Queue_two, Queue2_elem);
  gettimeofday(&tim,((void *)0));
  t14 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  printf("%012.3fms: Packet p%d begins service at S, time in Q2 = %.3fms\n",(t14-t1)*1000, Q2_head_data->packet_no, (t14-(Q2_head_data->Q2_entry_time))*1000);
  double time_Q2 = (t14-Q2_head_data->Q2_entry_time);
  time_in_Q2 = time_in_Q2 + time_Q2;
  gettimeofday(&tim,((void *)0));
  t12 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  packet_count_Q2exit ++;
  double server_sleep = (Q2_head_data->pkt_service) * 1000;
  int server_sleep1 = (int)server_sleep;
  pthread_mutex_unlock(&mut_lock);
  usleep(server_sleep1);
  gettimeofday(&tim,((void *)0));
  t8 = tim.tv_sec + (tim.tv_usec)/1000000.0;
  double serv_time = t8-t12;
  service_time = service_time + serv_time;
  server_count ++;
  double time_sys = (t8-(Q2_head_data->Q1_entry_time));
  time_in_sys = time_in_sys + time_sys;
  time_sd = time_sd + (( time_sys * time_sys )/( server_count * server_count ));
  printf("%012.3fms: Packet p%d departs S, service time: %.3fms, time in system: %.3fms\n",(t8-t1)*1000, Q2_head_data->packet_no, Q2_head_data->pkt_service,(t8-(Q2_head_data->Q1_entry_time))*1000);
  double time_S = (t8-t12);
  time_in_S = time_in_S + time_S;
  if( packet_count_Q2exit == num_afterdrop )
  {
   break;
  }
  else if( packet_Q1enter == 0)
  {
   break;
  }
 }
 return 0;
}

user_defined_types: struct tagMy402ListElem { void *obj; struct tagMy402ListElem *next; struct tagMy402ListElem *prev; }

user_defined_types: struct myPacketInfoStructure { int packet_no; double pkt_arrival; int P; double pkt_service; double Q1_entry_time; double Q2_entry_time; }


decompiled_code:
void *__fastcall server_termination_func(void *a1)
{
pthread_mutex_lock(&mut_lock2);
pthread_cond_wait(&terminate_server, &mut_lock2);
pthread_mutex_unlock(&mut_lock2);
if ( pthread_cancel(server_thread) )
{
fwrite("Pthread_cancel failed\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
if ( pthread_cancel(token_thread) )
{
fwrite("Pthread_cancel failed\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
pthread_mutex_lock(&mut_lock2);
pthread_cond_wait(&terminate_server, &mut_lock2);
pthread_mutex_unlock(&mut_lock2);
if ( pthread_cancel(server_thread) )
{
fwrite("Pthread_cancel failed\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
if ( pthread_cancel(token_thread) )
{
fwrite("Pthread_cancel failed\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
return 0LL;
}

original_code:
void *server_termination_func()
{
 pthread_mutex_lock(&mut_lock2);
 pthread_cond_wait(&terminate_server,&mut_lock2);
 pthread_mutex_unlock(&mut_lock2);
  int ret5 = pthread_cancel(server_thread);
  if( ret5 != 0 )
  {
   fprintf(stderr,"Pthread_cancel failed\n");
   exit(1);
  }
  int ret6 = pthread_cancel(token_thread);
  if ( ret6 != 0 )
  {
   fprintf(stderr, "Pthread_cancel failed\n");
   exit(1);
  }
  return 0;
}


decompiled_code:
double *make_packets()
{
double *result; // rax
double v1; // xmm0_8
double v2; // xmm0_8
char *v3; // rax
char **v4; // rbx
double v5; // r12
int v6; // ebp
double v7; // xmm0_8
char delim[27]; // [rsp+Dh] [rbp-1Bh] BYREF

if ( file_flag == 1 )
{
strcpy(delim, " \t");
fgets(buf, 1026, fp);
v3 = strtok(buf, delim);
str = (__int64)v3;
if ( v3 )
{
v4 = &a;
do
{
*v4 = v3;
v3 = strtok(0LL, delim);
str = (__int64)v3;
++v4;
}
while ( v3 );
}
v5 = atof(a);
v6 = atoi(*(&a + 1));
v7 = atof(*(&a + 2));
result = (double *)malloc(0x30uLL);
Queue_data = (__int64)result;
*(_DWORD *)result = ++packet_no;
result[1] = v5;
*((_DWORD *)result + 4) = v6;
result[3] = v7;
result[4] = 0.0;
result[5] = 0.0;
}
else
{
result = (double *)malloc(0x30uLL);
Queue_data = (__int64)result;
*(_DWORD *)result = ++packet_no;
v1 = 1000.0 / *(double *)λ
if ( 1000.0 / *(double *)&lambda > 10000.0 )
v1 = *(double *)&LC9;
result[1] = v1;
*((_DWORD *)result + 4) = P;
v2 = 1000.0 / *(double *)μ
if ( 1000.0 / *(double *)&mu > 10000.0 )
v2 = *(double *)&LC9;
result[3] = v2;
result[4] = 0.0;
result[5] = 0.0;
}
return result;
}

canonical_decompiled_code:
double *func3()
{
double *result; // rax
double v1; // xmm0_8
double v2; // xmm0_8
char *v3; // rax
char **v4; // rbx
double v5; // r12
int v6; // ebp
double v7; // xmm0_8
char delim[27]; // [rsp+Dh] [rbp-1Bh] BYREF

if ( file_flag == 1 )
{
strcpy(delim, " \t");
fgets(buf, 1026, fp);
v3 = strtok(buf, delim);
str = (__int64)v3;
if ( v3 )
{
v4 = &a;
do
{
*v4 = v3;
v3 = strtok(0LL, delim);
str = (__int64)v3;
++v4;
}
while ( v3 );
}
v5 = atof(a);
v6 = atoi(*(&a + 1));
v7 = atof(*(&a + 2));
result = (double *)malloc(0x30uLL);
Queue_data = (__int64)result;
*(_DWORD *)result = ++packet_no;
result[1] = v5;
*((_DWORD *)result + 4) = v6;
result[3] = v7;
result[4] = 0.0;
result[5] = 0.0;
}
else
{
result = (double *)malloc(0x30uLL);
Queue_data = (__int64)result;
*(_DWORD *)result = ++packet_no;
v1 = 1000.0 / *(double *)λ
if ( 1000.0 / *(double *)&lambda > 10000.0 )
v1 = *(double *)&LC9;
result[1] = v1;
*((_DWORD *)result + 4) = P;
v2 = 1000.0 / *(double *)μ
if ( 1000.0 / *(double *)&mu > 10000.0 )
v2 = *(double *)&LC9;
result[3] = v2;
result[4] = 0.0;
result[5] = 0.0;
}
return result;
}

original_code:
void make_packets()
{
 if( file_flag == 1)
 {
  int j = 0;
  char delim[] = " \t";
  fgets( buf, sizeof(buf), fp );
  str = strtok(buf,delim);
  while( str != ((void *)0))
  {
   a[j] = str;
   j++;
   str = strtok( ((void *)0),delim);
  }
  char *p_a = a[0];
  char *r_t = a[1];
  char *p_s = a[2];
  double pkt_arr = atof(p_a);
  int token_req = atoi(r_t);
  double pkt_ser = atof(p_s);
  Queue_data = (MyPacketInfo*)malloc(sizeof(MyPacketInfo));
  packet_no = packet_no + 1;
  Queue_data->packet_no = packet_no;
  Queue_data->pkt_arrival = pkt_arr;
  Queue_data->P = token_req;
  Queue_data->pkt_service = pkt_ser;
  Queue_data->Q1_entry_time = 0.0;
  Queue_data->Q2_entry_time = 0.0;
 }
 else
 {
  Queue_data = (MyPacketInfo*)malloc(sizeof(MyPacketInfo));
  packet_no = packet_no + 1;
  Queue_data->packet_no = packet_no;
  double packet_arr = (1000)/lambda;
  if( packet_arr > 10000 )
  {
   packet_arr = 10000.0;
  }
  Queue_data->pkt_arrival = packet_arr;
  Queue_data->P = P;
  double s_time = (1000)/mu;
  if( s_time > 10000 )
  {
   s_time = 10000.0;
  }
  Queue_data->pkt_service = s_time;
  Queue_data->Q1_entry_time = 0.0;
  Queue_data->Q2_entry_time = 0.0;
 }
}

canonical_original_code:
void make_packets()
{
 if( file_flag == 1)
 {
  int j = 0;
  char delim[] = " \t";
  fgets( buf, sizeof(buf), fp );
  str = strtok(buf,delim);
  while( str != ((void *)0))
  {
   a[j] = str;
   j++;
   str = strtok( ((void *)0),delim);
  }
  char *p_a = a[0];
  char *r_t = a[1];
  char *p_s = a[2];
  double pkt_arr = atof(p_a);
  int token_req = atoi(r_t);
  double pkt_ser = atof(p_s);
  Queue_data = (struct myPacketInfoStructure *)malloc(sizeof(MyPacketInfo));
  packet_no = packet_no + 1;
  Queue_data->packet_no = packet_no;
  Queue_data->pkt_arrival = pkt_arr;
  Queue_data->P = token_req;
  Queue_data->pkt_service = pkt_ser;
  Queue_data->Q1_entry_time = 0.0;
  Queue_data->Q2_entry_time = 0.0;
 }
 else
 {
  Queue_data = (struct myPacketInfoStructure *)malloc(sizeof(MyPacketInfo));
  packet_no = packet_no + 1;
  Queue_data->packet_no = packet_no;
  double packet_arr = (1000)/lambda;
  if( packet_arr > 10000 )
  {
   packet_arr = 10000.0;
  }
  Queue_data->pkt_arrival = packet_arr;
  Queue_data->P = P;
  double s_time = (1000)/mu;
  if( s_time > 10000 )
  {
   s_time = 10000.0;
  }
  Queue_data->pkt_service = s_time;
  Queue_data->Q1_entry_time = 0.0;
  Queue_data->Q2_entry_time = 0.0;
 }
}

variable_types: j int

variable_types:
delim
char []