decompiled_code:
void __fastcall xyz_set(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

canonical_decompiled_code:
void __fastcall func2(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

original_code:
void
xyz_set( xyz_t * xyz, double x, double y, double z)
{
  xyz->x = x;
  xyz->y = y;
  xyz->z = z;
}

canonical_original_code:
void xyz_set(struct xyz_t *xyz, double x, double y, double z)
{
  xyz->x = x;
  xyz->y = y;
  xyz->z = z;
}

variable_types: x double

variable_types: y double

variable_types: z double


repo: ghorn/mathlib

original_code:
void
xyz_diff(xyz_t *diff, const xyz_t * const a, const xyz_t * const b)
{
  diff->x = a->x - b->x;
  diff->y = a->y - b->y;
  diff->z = a->z - b->z;
}

canonical_original_code:
void xyz_diff(struct xyz_t *diff, struct xyz_t *a, struct xyz_t *b)
{
  diff->x = a->x - b->x;
  diff->y = a->y - b->y;
  diff->z = a->z - b->z;
}

variable_types: struct xyz_t { double x; double y; double z; } *

variable_types:
a
struct xyz_t *

variable_types:
b
struct xyz_t *

return_type: void


original_code:
void
xyz_sum(xyz_t *sum, const xyz_t * const a, const xyz_t * const b)
{
  sum->x = a->x + b->x;
  sum->y = a->y + b->y;
  sum->z = a->z + b->z;
}

canonical_original_code:
void xyz_sum(struct xyz_t *sum, struct xyz_t *a, struct xyz_t *b)
{
  sum->x = a->x + b->x;
  sum->y = a->y + b->y;
  sum->z = a->z + b->z;
}

user_defined_types: struct xyz_t { double x; double y; double z; }


original_code:
void
xyz_sum_scale(xyz_t *sum, const double sum_scale,
              const xyz_t * const a, const double a_scale,
              const xyz_t * const b, const double b_scale)
{
  sum->x = sum->x*sum_scale + a->x*a_scale + b->x*b_scale;
  sum->y = sum->y*sum_scale + a->y*a_scale + b->y*b_scale;
  sum->z = sum->z*sum_scale + a->z*a_scale + b->z*b_scale;
}

canonical_original_code:
void xyz_sum_scale(struct xyz_t *sum, double sum_scale, struct xyz_t *a, double a_scale, struct xyz_t *b, double b_scale)
{
  sum->x = sum->x*sum_scale + a->x*a_scale + b->x*b_scale;
  sum->y = sum->y*sum_scale + a->y*a_scale + b->y*b_scale;
  sum->z = sum->z*sum_scale + a->z*a_scale + b->z*b_scale;
}


original_code:
void
xyz_cross( xyz_t * c, const xyz_t * const a, const xyz_t * const b)
{
  c->x = a->y*b->z - a->z*b->y;
  c->y = - a->x*b->z + a->z*b->x;
  c->z = a->x*b->y - a->y*b->x;
}

canonical_original_code:
void xyz_cross(struct xyz_t *c, struct xyz_t *a, struct xyz_t *b)
{
  c->x = a->y*b->z - a->z*b->y;
  c->y = - a->x*b->z + a->z*b->x;
  c->z = a->x*b->y - a->y*b->x;
}


decompiled_code:
double __fastcall xyz_dot(double *a1, double *a2)
{
return *a1 * *a2 + a1[1] * a2[1] + a1[2] * a2[2];
}

canonical_decompiled_code:
double __fastcall func7(double *a1, double *a2)
{
return *a1 * *a2 + a1[1] * a2[1] + a1[2] * a2[2];
}

original_code:
double
xyz_dot( const xyz_t * const a, const xyz_t * const b)
{
  return a->x*b->x + a->y*b->y + a->z*b->z;
}

canonical_original_code:
double xyz_dot(struct xyz_t *a, struct xyz_t *b)
{
  return a->x*b->x + a->y*b->y + a->z*b->z;
}

return_type: double


decompiled_code:
double __fastcall xyz_norm_squared(double *a1)
{
return xyz_dot(a1, a1);
}

canonical_decompiled_code:
double __fastcall func8(double *a1)
{
return func7(a1, a1);
}

original_code:
double
xyz_norm_squared( const xyz_t * const vec)
{
  return xyz_dot( vec, vec);
}

canonical_original_code:
double xyz_norm_squared(struct xyz_t *vec)
{
  return xyz_dot( vec, vec);
}


decompiled_code:
void __fastcall xyz_norm(double *a1)
{
double v1; // xmm0_8

v1 = xyz_norm_squared(a1);
if ( v1 < 0.0 )
sqrt(v1);
}

canonical_decompiled_code:
void __fastcall func9(double *a1)
{
double v1; // xmm0_8

v1 = func8(a1);
if ( v1 < 0.0 )
sqrt(v1);
}

original_code:
double
xyz_norm( const xyz_t * const vec)
{
  return sqrt(xyz_norm_squared(vec));
}

canonical_original_code:
double xyz_norm(struct xyz_t *vec)
{
  return sqrt(xyz_norm_squared(vec));
}


decompiled_code:
void __fastcall xyz_distance(double *a1, double *a2)
{
double v2[5]; // [rsp+0h] [rbp-28h] BYREF

xyz_diff(v2, a1, a2);
xyz_norm(v2);
}

canonical_decompiled_code:
void __fastcall func10(double *a1, double *a2)
{
double v2[5]; // [rsp+0h] [rbp-28h] BYREF

func3(v2, a1, a2);
func9(v2);
}

original_code:
double
xyz_distance( const xyz_t * const a, const xyz_t * const b )
{
  xyz_t diff;
  xyz_diff( &diff, a, b);
  return xyz_norm( &diff );
}

canonical_original_code:
double xyz_distance(struct xyz_t *a, struct xyz_t *b)
{
  struct xyz_t diff;
  xyz_diff( &diff, a, b);
  return xyz_norm( &diff );
}

variable_types: struct xyz_t { double x; double y; double z; }


decompiled_code:
void __fastcall xyz_scale(double *a1, double *a2, double a3)
{
double v3; // xmm1_8
double v4; // xmm0_8

*a1 = *a2 * a3;
v3 = a2[1] * a3;
v4 = a3 * a2[2];
a1[1] = v3;
a1[2] = v4;
}

canonical_decompiled_code:
void __fastcall func11(double *a1, double *a2, double a3)
{
double v3; // xmm1_8
double v4; // xmm0_8

*a1 = *a2 * a3;
v3 = a2[1] * a3;
v4 = a3 * a2[2];
a1[1] = v3;
a1[2] = v4;
}

original_code:
void
xyz_scale( xyz_t * vec_out, double scale_factor, const xyz_t * const vec_in)
{
  vec_out->x = scale_factor*vec_in->x;
  vec_out->y = scale_factor*vec_in->y;
  vec_out->z = scale_factor*vec_in->z;
}

canonical_original_code:
void xyz_scale(struct xyz_t *vec_out, double scale_factor, struct xyz_t *vec_in)
{
  vec_out->x = scale_factor*vec_in->x;
  vec_out->y = scale_factor*vec_in->y;
  vec_out->z = scale_factor*vec_in->z;
}


decompiled_code:
void __fastcall xyz_scale_self(double *a1, double a2)
{
xyz_scale(a1, a1, a2);
}

canonical_decompiled_code:
void __fastcall func12(double *a1, double a2)
{
func11(a1, a1, a2);
}

original_code:
void
xyz_scale_self( xyz_t * vec, double scale_factor)
{
  xyz_scale(vec, scale_factor, vec);
}

canonical_original_code:
void xyz_scale_self(struct xyz_t *vec, double scale_factor)
{
  xyz_scale(vec, scale_factor, vec);
}


decompiled_code:
void __fastcall xyz_normalize_to(double *a1, double *a2, double a3)
{
xyz_norm(a2);
xyz_scale(a1, a2, a3 / (a3 + 1.0e-12));
}

canonical_decompiled_code:
void __fastcall func13(double *a1, double *a2, double a3)
{
func9(a2);
func11(a1, a2, a3 / (a3 + 1.0e-12));
}

original_code:
void
xyz_normalize_to( xyz_t * vec_out, const double new_norm, const xyz_t * const vec_in)
{
  xyz_scale( vec_out, new_norm/(xyz_norm(vec_in) + 1e-12), vec_in );
}

canonical_original_code:
void xyz_normalize_to(struct xyz_t *vec_out, double new_norm, struct xyz_t *vec_in)
{
  xyz_scale( vec_out, new_norm/(xyz_norm(vec_in) + 1e-12), vec_in );
}


decompiled_code:
void __fastcall xyz_normalize(double *a1, double *a2)
{
xyz_normalize_to(a1, a2, 1.0);
}

canonical_decompiled_code:
void __fastcall func14(double *a1, double *a2)
{
func13(a1, a2, 1.0);
}

original_code:
void
xyz_normalize( xyz_t * vec_out, const xyz_t * const vec_in)
{
  xyz_normalize_to( vec_out, 1.0, vec_in);
}

canonical_original_code:
void xyz_normalize(struct xyz_t *vec_out, struct xyz_t *vec_in)
{
  xyz_normalize_to( vec_out, 1.0, vec_in);
}


decompiled_code:
void __fastcall xyz_normalize_self_to(double *a1, double a2)
{
xyz_norm(a1);
xyz_scale_self(a1, a2 / (a2 + 1.0e-12));
}

canonical_decompiled_code:
void __fastcall func15(double *a1, double a2)
{
func9(a1);
func12(a1, a2 / (a2 + 1.0e-12));
}

original_code:
void
xyz_normalize_self_to( xyz_t * vec, const double new_norm )
{
  xyz_scale_self( vec, new_norm/(xyz_norm(vec) + 1e-12) );
}

canonical_original_code:
void xyz_normalize_self_to(struct xyz_t *vec, double new_norm)
{
  xyz_scale_self( vec, new_norm/(xyz_norm(vec) + 1e-12) );
}


decompiled_code:
void __fastcall xyz_normalize_self(double *a1)
{
xyz_normalize_self_to(a1, 1.0);
}

canonical_decompiled_code:
void __fastcall func16(double *a1)
{
func15(a1, 1.0);
}

original_code:
void
xyz_normalize_self( xyz_t * vec )
{
  xyz_normalize_self_to( vec, 1.0 );
}

canonical_original_code:
void xyz_normalize_self(struct xyz_t *vec)
{
  xyz_normalize_self_to( vec, 1.0 );
}


original_code:
void
xyz_mult_3x3_by_xyz(xyz_t * v_out, const double * const M, const xyz_t * const v)
{
  v_out->x = M[0]*v->x + M[1]*v->y + M[2]*v->z;
  v_out->y = M[3]*v->x + M[4]*v->y + M[5]*v->z;
  v_out->z = M[6]*v->x + M[7]*v->y + M[8]*v->z;
}

variable_types:
M
double *

variable_types:
v
struct xyz_t *


original_code:
void
xyz_mult_3x3_transpose_by_xyz(xyz_t * v_out, const double * const M, const xyz_t * const v)
{
  v_out->x = M[0]*v->x + M[3]*v->y + M[6]*v->z;
  v_out->y = M[1]*v->x + M[4]*v->y + M[7]*v->z;
  v_out->z = M[2]*v->x + M[5]*v->y + M[8]*v->z;
}

canonical_original_code:
void xyz_mult_3x3_transpose_by_xyz(struct xyz_t *v_out, double *M, struct xyz_t *v)
{
  v_out->x = M[0]*v->x + M[3]*v->y + M[6]*v->z;
  v_out->y = M[1]*v->x + M[4]*v->y + M[7]*v->z;
  v_out->z = M[2]*v->x + M[5]*v->y + M[8]*v->z;
}


decompiled_code:
void __fastcall quat_memcpy(__m128i *a1, const __m128i *a2)
{
*a1 = _mm_loadu_si128(a2);
a1[1] = _mm_loadu_si128(a2 + 1);
}

original_code:
void
quat_memcpy( quat_t * q1, const quat_t * const q0)
{
  memcpy( q1, q0, sizeof(quat_t) );
}

canonical_original_code:
void quat_memcpy(struct quat_t *q1, struct quat_t *q0)
{
  memcpy( q1, q0, sizeof(quat_t) );
}

variable_types: struct quat_t { double q0; double q1; double q2; double q3; } *


original_code:
void
quat_inv(quat_t *q_out, const quat_t * const q_in)
{
  q_out->q0 = q_in->q0;
  q_out->q1 = -q_in->q1;
  q_out->q2 = -q_in->q2;
  q_out->q3 = -q_in->q3;
}

canonical_original_code:
void quat_inv(struct quat_t *q_out, struct quat_t *q_in)
{
  q_out->q0 = q_in->q0;
  q_out->q1 = -q_in->q1;
  q_out->q2 = -q_in->q2;
  q_out->q3 = -q_in->q3;
}

user_defined_types: struct quat_t { double q0; double q1; double q2; double q3; }


original_code:
double
quat_norm(const quat_t * const q)
{
  return sqrt( q->q0*q->q0 + q->q1*q->q1 + q->q2*q->q2 + q->q3*q->q3);
}

canonical_original_code:
double quat_norm(struct quat_t *q)
{
  return sqrt( q->q0*q->q0 + q->q1*q->q1 + q->q2*q->q2 + q->q3*q->q3);
}


original_code:
void
quat_normalize(quat_t * q)
{
  double norm_inv = 1.0/quat_norm(q);
  q->q0 = q->q0*norm_inv;
  q->q1 = q->q1*norm_inv;
  q->q2 = q->q2*norm_inv;
  q->q3 = q->q3*norm_inv;
}

canonical_original_code:
void quat_normalize(struct quat_t *q)
{
  double norm_inv = 1.0/quat_norm(q);
  q->q0 = q->q0*norm_inv;
  q->q1 = q->q1*norm_inv;
  q->q2 = q->q2*norm_inv;
  q->q3 = q->q3*norm_inv;
}


original_code:
void
quat_mult(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  q_out->q0 = qa->q0*qb->q0 - qa->q1*qb->q1 - qa->q2*qb->q2 - qa->q3*qb->q3;
  q_out->q1 = qa->q0*qb->q1 + qa->q1*qb->q0 + qa->q2*qb->q3 - qa->q3*qb->q2;
  q_out->q2 = qa->q0*qb->q2 - qa->q1*qb->q3 + qa->q2*qb->q0 + qa->q3*qb->q1;
  q_out->q3 = qa->q0*qb->q3 + qa->q1*qb->q2 - qa->q2*qb->q1 + qa->q3*qb->q0;
  if (q_out->q0 < 0){
    q_out->q0 = -q_out->q0;
    q_out->q1 = -q_out->q1;
    q_out->q2 = -q_out->q2;
    q_out->q3 = -q_out->q3;
  }
  quat_normalize( q_out );
}

canonical_original_code:
void quat_mult(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  q_out->q0 = qa->q0*qb->q0 - qa->q1*qb->q1 - qa->q2*qb->q2 - qa->q3*qb->q3;
  q_out->q1 = qa->q0*qb->q1 + qa->q1*qb->q0 + qa->q2*qb->q3 - qa->q3*qb->q2;
  q_out->q2 = qa->q0*qb->q2 - qa->q1*qb->q3 + qa->q2*qb->q0 + qa->q3*qb->q1;
  q_out->q3 = qa->q0*qb->q3 + qa->q1*qb->q2 - qa->q2*qb->q1 + qa->q3*qb->q0;
  if (q_out->q0 < 0){
    q_out->q0 = -q_out->q0;
    q_out->q1 = -q_out->q1;
    q_out->q2 = -q_out->q2;
    q_out->q3 = -q_out->q3;
  }
  quat_normalize( q_out );
}

variable_types:
qa
struct quat_t *

variable_types:
qb
struct quat_t *


decompiled_code:
void __fastcall quat_mult_inv(__int64 a1, __int64 a2, _QWORD *a3)
{
double *v3; // rdx
double *v4; // r8
double *v5; // r9
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

quat_inv(v6, a3);
quat_mult(v4, v5, v3);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, _QWORD *a3)
{
double *v3; // rdx
double *v4; // r8
double *v5; // r9
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

func1(v6, a3);
func4(v4, v5, v3);
}

original_code:
void
quat_mult_inv(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  quat_t qb_inv;
  quat_inv(&qb_inv,qb);
  quat_mult(q_out, qa, &qb_inv);
}

canonical_original_code:
void quat_mult_inv(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  struct quat_t qb_inv;
  quat_inv(&qb_inv,qb);
  quat_mult(q_out, qa, &qb_inv);
}

variable_types: struct quat_t { double q0; double q1; double q2; double q3; }


decompiled_code:
void __fastcall quat_inv_mult(__int64 a1, _QWORD *a2)
{
double *v2; // rdx
double *v3; // r8
double v4[5]; // [rsp+0h] [rbp-28h] BYREF

quat_inv(v4, a2);
quat_mult(v3, v4, v2);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, _QWORD *a2)
{
double *v2; // rdx
double *v3; // r8
double v4[5]; // [rsp+0h] [rbp-28h] BYREF

func1(v4, a2);
func4(v3, v4, v2);
}

original_code:
void
quat_inv_mult(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  quat_t qa_inv;
  quat_inv(&qa_inv,qa);
  quat_mult(q_out, &qa_inv, qb);
}

canonical_original_code:
void quat_inv_mult(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  struct quat_t qa_inv;
  quat_inv(&qa_inv,qa);
  quat_mult(q_out, &qa_inv, qb);
}


decompiled_code:
void __fastcall rotate_xyz_about_x(__int64 a1, __int64 a2, double a3)
{
double v3; // xmm2_8
double v4; // xmm3_8
double v5; // xmm1_8
double v6; // xmm0_8
double v7; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a3, sinx, &v7);
v3 = *(double *)(a2 + 8);
v4 = v7;
v5 = sinx[0];
*(_QWORD *)a1 = *(_QWORD *)a2;
v6 = *(double *)(a2 + 16);
*(double *)(a1 + 8) = v3 * v4 + v6 * v5;
*(double *)(a1 + 16) = v6 * v4 - v5 * *(double *)(a2 + 8);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, __int64 a2, double a3)
{
double v3; // xmm2_8
double v4; // xmm3_8
double v5; // xmm1_8
double v6; // xmm0_8
double v7; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a3, sinx, &v7);
v3 = *(double *)(a2 + 8);
v4 = v7;
v5 = sinx[0];
*(_QWORD *)a1 = *(_QWORD *)a2;
v6 = *(double *)(a2 + 16);
*(double *)(a1 + 8) = v3 * v4 + v6 * v5;
*(double *)(a1 + 16) = v6 * v4 - v5 * *(double *)(a2 + 8);
}

original_code:
void
rotate_xyz_about_x( xyz_t * b, const xyz_t * const a, const double rot_angle)
{
  double cos_theta = cos(rot_angle);
  double sin_theta = sin(rot_angle);
  b->x = a->x;
  b->y = a->y*cos_theta + a->z*sin_theta;
  b->z = -a->y*sin_theta + a->z*cos_theta;
}

canonical_original_code:
void rotate_xyz_about_x(struct xyz_t *b, struct xyz_t *a, double rot_angle)
{
  double cos_theta = cos(rot_angle);
  double sin_theta = sin(rot_angle);
  b->x = a->x;
  b->y = a->y*cos_theta + a->z*sin_theta;
  b->z = -a->y*sin_theta + a->z*cos_theta;
}


variable_types: struct euler_t { double roll; double pitch; double yaw; } *


original_code:
void
euler321_of_dcm(euler_t *e, const double * const R)
{
  double r11 = R[0];
  double r12 = R[1];
  double mr13 = -R[2];
  double r23 = R[5];
  double r33 = R[8];
  e->yaw = atan2( r12, r11 );
  if (mr13 > 1.0) mr13 = 1.0;
  else if (mr13 < -1.0) mr13 = -1.0;
  e->pitch = asin( mr13 );
  e->roll = atan2( r23, r33 );
}

canonical_original_code:
void euler321_of_dcm(struct euler_t *e, double *R)
{
  double r11 = R[0];
  double r12 = R[1];
  double mr13 = -R[2];
  double r23 = R[5];
  double r33 = R[8];
  e->yaw = atan2( r12, r11 );
  if (mr13 > 1.0) mr13 = 1.0;
  else if (mr13 < -1.0) mr13 = -1.0;
  e->pitch = asin( mr13 );
  e->roll = atan2( r23, r33 );
}

variable_types:
R
double *

user_defined_types: struct euler_t { double roll; double pitch; double yaw; }


original_code:
void
quat_of_euler321(quat_t * q, const euler_t * const e)
{
  double sr2 = sin(0.5*e->roll);
  double cr2 = cos(0.5*e->roll);
  double sp2 = sin(0.5*e->pitch);
  double cp2 = cos(0.5*e->pitch);
  double sy2 = sin(0.5*e->yaw);
  double cy2 = cos(0.5*e->yaw);
  q->q0 = cr2*cp2*cy2 + sr2*sp2*sy2;
  q->q1 = sr2*cp2*cy2 - cr2*sp2*sy2;
  q->q2 = cr2*sp2*cy2 + sr2*cp2*sy2;
  q->q3 = cr2*cp2*sy2 - sr2*sp2*cy2;
  if (q->q0 < 0){
    q->q0 = -q->q0;
    q->q1 = -q->q1;
    q->q2 = -q->q2;
    q->q3 = -q->q3;
  }
  quat_normalize(q);
}

canonical_original_code:
void quat_of_euler321(struct quat_t *q, struct euler_t *e)
{
  double sr2 = sin(0.5*e->roll);
  double cr2 = cos(0.5*e->roll);
  double sp2 = sin(0.5*e->pitch);
  double cp2 = cos(0.5*e->pitch);
  double sy2 = sin(0.5*e->yaw);
  double cy2 = cos(0.5*e->yaw);
  q->q0 = cr2*cp2*cy2 + sr2*sp2*sy2;
  q->q1 = sr2*cp2*cy2 - cr2*sp2*sy2;
  q->q2 = cr2*sp2*cy2 + sr2*cp2*sy2;
  q->q3 = cr2*cp2*sy2 - sr2*sp2*cy2;
  if (q->q0 < 0){
    q->q0 = -q->q0;
    q->q1 = -q->q1;
    q->q2 = -q->q2;
    q->q3 = -q->q3;
  }
  quat_normalize(q);
}


decompiled_code:
__int64 __fastcall quat_of_dcm_a2b(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

euler321_of_dcm(v3, a2);
return quat_of_euler321(a1, v3);
}

canonical_decompiled_code:
__int64 __fastcall func4(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

func2(v3, a2);
return func3(a1, v3);
}

original_code:
void
quat_of_dcm_a2b(quat_t * q, const double * const R)
{
  euler_t e;
  euler321_of_dcm( &e, R);
  quat_of_euler321( q, &e);
}

canonical_original_code:
void quat_of_dcm_a2b(struct quat_t *q, double *R)
{
  struct euler_t e;
  euler321_of_dcm( &e, R);
  quat_of_euler321( q, &e);
}

variable_types: struct euler_t { double roll; double pitch; double yaw; }


decompiled_code:
__int64 __fastcall quat_of_dcm_b2a(double *a1, double *a2)
{
quat_of_dcm_a2b(a1, a2);
return quat_inv(a1, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(double *a1, double *a2)
{
func4(a1, a2);
return quat_inv(a1, a1);
}

original_code:
void
quat_of_dcm_b2a(quat_t * q, const double * const R)
{
  quat_of_dcm_a2b(q, R);
  quat_inv(q, q);
}

canonical_original_code:
void quat_of_dcm_b2a(struct quat_t *q, double *R)
{
  quat_of_dcm_a2b(q, R);
  quat_inv(q, q);
}


original_code:
void
dcm_of_quat_b2a(double *R_b2a, const quat_t * const q_a2b)
{
  quat_t q_b2a;
  q_b2a.q0 = q_a2b->q0;
  q_b2a.q1 = -q_a2b->q1;
  q_b2a.q2 = -q_a2b->q2;
  q_b2a.q3 = -q_a2b->q3;
  dcm_of_quat_a2b(R_b2a, &q_b2a);
}

canonical_original_code:
void dcm_of_quat_b2a(double *R_b2a, struct quat_t *q_a2b)
{
  struct quat_t q_b2a;
  q_b2a.q0 = q_a2b->q0;
  q_b2a.q1 = -q_a2b->q1;
  q_b2a.q2 = -q_a2b->q2;
  q_b2a.q3 = -q_a2b->q3;
  dcm_of_quat_a2b(R_b2a, &q_b2a);
}


decompiled_code:
// attributes: thunk
__int64 rot_vec_by_dcm_a2b()
{
return xyz_mult_3x3_by_xyz();
}

canonical_decompiled_code:
__int64 func8()
{
return xyz_mult_3x3_by_xyz();
}

original_code:
void
rot_vec_by_dcm_a2b(xyz_t *vec_b, const double * const R_a2b, const xyz_t * const vec_a)
{
  xyz_mult_3x3_by_xyz( vec_b, R_a2b, vec_a );
}

canonical_original_code:
void rot_vec_by_dcm_a2b(struct xyz_t *vec_b, double *R_a2b, struct xyz_t *vec_a)
{
  xyz_mult_3x3_by_xyz( vec_b, R_a2b, vec_a );
}


decompiled_code:
// attributes: thunk
__int64 rot_vec_by_dcm_b2a()
{
return xyz_mult_3x3_transpose_by_xyz();
}

canonical_decompiled_code:
__int64 func9()
{
return xyz_mult_3x3_transpose_by_xyz();
}

original_code:
void
rot_vec_by_dcm_b2a(xyz_t *vec_a, const double * const R_a2b, const xyz_t * const vec_b)
{
  xyz_mult_3x3_transpose_by_xyz( vec_a, R_a2b, vec_b );
}

canonical_original_code:
void rot_vec_by_dcm_b2a(struct xyz_t *vec_a, double *R_a2b, struct xyz_t *vec_b)
{
  xyz_mult_3x3_transpose_by_xyz( vec_a, R_a2b, vec_b );
}


decompiled_code:
__int64 __fastcall rot_vec_by_quat_a2b(__int64 a1, double *a2)
{
__int64 v2; // r8
double v4[11]; // [rsp+0h] [rbp-58h] BYREF

dcm_of_quat_a2b(v4, a2);
return rot_vec_by_dcm_a2b(v2, v4);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, double *a2)
{
__int64 v2; // r8
double v4[11]; // [rsp+0h] [rbp-58h] BYREF

func6(v4, a2);
return func8(v2, v4);
}

original_code:
void
rot_vec_by_quat_a2b(xyz_t *vec_b, const quat_t * const q_a2b, const xyz_t * const vec_a)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_a2b(vec_b, R_a2b, vec_a);
}

canonical_original_code:
void rot_vec_by_quat_a2b(struct xyz_t *vec_b, struct quat_t *q_a2b, struct xyz_t *vec_a)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_a2b(vec_b, R_a2b, vec_a);
}


decompiled_code:
__int64 __fastcall rot_vec_by_quat_b2a(__int64 a1, double *a2)
{
__int64 v2; // r8
double v4[11]; // [rsp+0h] [rbp-58h] BYREF

dcm_of_quat_a2b(v4, a2);
return rot_vec_by_dcm_b2a(v2, v4);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, double *a2)
{
__int64 v2; // r8
double v4[11]; // [rsp+0h] [rbp-58h] BYREF

func6(v4, a2);
return func9(v2, v4);
}

original_code:
void
rot_vec_by_quat_b2a(xyz_t *vec_a, const quat_t * const q_a2b, const xyz_t * const vec_b)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_b2a(vec_a, R_a2b, vec_b);
}

canonical_original_code:
void rot_vec_by_quat_b2a(struct xyz_t *vec_a, struct quat_t *q_a2b, struct xyz_t *vec_b)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_b2a(vec_a, R_a2b, vec_b);
}


decompiled_code:
void __fastcall get_wind_angles_from_v_bw_b(double *a1, double *a2, double *a3, double *a4)
{
double v6; // xmm0_8

if ( a3 )
{
v6 = xyz_norm(a4) + 1.0e-12;
*a3 = v6;
if ( !a2 )
goto LABEL_4;
goto LABEL_3;
}
if ( a2 )
{
v6 = xyz_norm(a4) + 1.0e-12;
LABEL_3:
*a2 = asin(a4[1] / v6);
}
LABEL_4:
if ( a1 )
*a1 = atan2(a4[2], *a4);
}

canonical_decompiled_code:
void __fastcall func12(double *a1, double *a2, double *a3, double *a4)
{
double v6; // xmm0_8

if ( a3 )
{
v6 = xyz_norm(a4) + 1.0e-12;
*a3 = v6;
if ( !a2 )
goto LABEL_4;
goto LABEL_3;
}
if ( a2 )
{
v6 = xyz_norm(a4) + 1.0e-12;
LABEL_3:
*a2 = asin(a4[1] / v6);
}
LABEL_4:
if ( a1 )
*a1 = atan2(a4[2], *a4);
}

original_code:
void
get_wind_angles_from_v_bw_b(double * alpha, double * beta, double * airspeed, const xyz_t * const v_bw_b)
{
  double airspeed_internal_memory;
  double * airspeed_internal = &airspeed_internal_memory;
  if (airspeed != ((void *)0))
    *airspeed = xyz_norm(v_bw_b) + 1e-12;
  if (beta != ((void *)0))
  {
    if (airspeed != ((void *)0))
      airspeed_internal = airspeed;
    else
      *airspeed_internal = xyz_norm(v_bw_b) + 1e-12;
    *beta = asin ( v_bw_b->y / *airspeed_internal );
  }
  if (alpha != ((void *)0))
    *alpha = atan2( v_bw_b->z, v_bw_b->x );
}

canonical_original_code:
void get_wind_angles_from_v_bw_b(double *alpha, double *beta, double *airspeed, struct xyz_t *v_bw_b)
{
  double airspeed_internal_memory;
  double *airspeed_internal = &airspeed_internal_memory;
  if (airspeed != ((void *)0))
    *airspeed = xyz_norm(v_bw_b) + 1e-12;
  if (beta != ((void *)0))
  {
    if (airspeed != ((void *)0))
      airspeed_internal = airspeed;
    else
      *airspeed_internal = xyz_norm(v_bw_b) + 1e-12;
    *beta = asin ( v_bw_b->y / *airspeed_internal );
  }
  if (alpha != ((void *)0))
    *alpha = atan2( v_bw_b->z, v_bw_b->x );
}

variable_types:
alpha
double *

variable_types:
beta
double *

variable_types:
airspeed
double *


decompiled_code:
void __fastcall get_wind_angles(double *a1, double *a2, double *a3, __int64 a4, double *a5, __int64 a6)
{
_BYTE v9[32]; // [rsp+0h] [rbp-68h] BYREF
double v10[9]; // [rsp+20h] [rbp-48h] BYREF

rot_vec_by_quat_a2b((__int64)v9, a5);
xyz_diff(v10, a6, v9);
get_wind_angles_from_v_bw_b(a1, a2, a3, v10);
if ( a4 )
xyz_memcpy(a4, v10);
}

canonical_decompiled_code:
void __fastcall func13(double *a1, double *a2, double *a3, __int64 a4, double *a5, __int64 a6)
{
_BYTE v9[32]; // [rsp+0h] [rbp-68h] BYREF
double v10[9]; // [rsp+20h] [rbp-48h] BYREF

func10((__int64)v9, a5);
xyz_diff(v10, a6, v9);
func12(a1, a2, a3, v10);
if ( a4 )
xyz_memcpy(a4, v10);
}

original_code:
void
get_wind_angles( double * alpha,
                 double * beta,
                 double * airspeed,
                 xyz_t * v_bw_b_out,
                 const quat_t * const q_n2b,
                 const xyz_t * const v_bn_b,
                 const xyz_t * const v_wn_n)
{
  xyz_t v_wn_b;
  rot_vec_by_quat_a2b( &v_wn_b, q_n2b, v_wn_n);
  xyz_t v_bw_b;
  xyz_diff( &v_bw_b, v_bn_b, &v_wn_b);
  get_wind_angles_from_v_bw_b( alpha, beta, airspeed, &v_bw_b );
  if (v_bw_b_out != ((void *)0))
    xyz_memcpy( v_bw_b_out, &v_bw_b);
}

canonical_original_code:
void get_wind_angles(double *alpha, double *beta, double *airspeed, struct xyz_t *v_bw_b_out, struct quat_t *q_n2b, struct xyz_t *v_bn_b, struct xyz_t *v_wn_n)
{
  struct xyz_t v_wn_b;
  rot_vec_by_quat_a2b( &v_wn_b, q_n2b, v_wn_n);
  struct xyz_t v_bw_b;
  xyz_diff( &v_bw_b, v_bn_b, &v_wn_b);
  get_wind_angles_from_v_bw_b( alpha, beta, airspeed, &v_bw_b );
  if (v_bw_b_out != ((void *)0))
    xyz_memcpy( v_bw_b_out, &v_bw_b);
}


decompiled_code:
void __fastcall v_bw_b_from_wind_angles(double *a1, double a2, double a3, double a4)
{
double v4; // xmm0_8
double v5; // xmm1_8
double v6; // [rsp+0h] [rbp-38h]
double v7; // [rsp+8h] [rbp-30h]
double cosx; // [rsp+20h] [rbp-18h] BYREF
double sinx[2]; // [rsp+28h] [rbp-10h] BYREF

sincos(a3, sinx, &cosx);
v7 = cosx;
v6 = sinx[0];
sincos(a2, sinx, &cosx);
v4 = cosx * a4 * v7;
a1[1] = v6 * a4;
v5 = v7 * a4 * sinx[0];
*a1 = v4;
a1[2] = v5;
}

canonical_decompiled_code:
void __fastcall func14(double *a1, double a2, double a3, double a4)
{
double v4; // xmm0_8
double v5; // xmm1_8
double v6; // [rsp+0h] [rbp-38h]
double v7; // [rsp+8h] [rbp-30h]
double cosx; // [rsp+20h] [rbp-18h] BYREF
double sinx[2]; // [rsp+28h] [rbp-10h] BYREF

sincos(a3, sinx, &cosx);
v7 = cosx;
v6 = sinx[0];
sincos(a2, sinx, &cosx);
v4 = cosx * a4 * v7;
a1[1] = v6 * a4;
v5 = v7 * a4 * sinx[0];
*a1 = v4;
a1[2] = v5;
}

original_code:
void
v_bw_b_from_wind_angles( xyz_t * v_bw_b, const double alpha, const double beta, const double airspeed)
{
  v_bw_b->x = airspeed*cos(alpha)*cos(beta);
  v_bw_b->y = airspeed*sin(beta);
  v_bw_b->z = airspeed*cos(beta)*sin(alpha);
}

canonical_original_code:
void v_bw_b_from_wind_angles(struct xyz_t *v_bw_b, double alpha, double beta, double airspeed)
{
  v_bw_b->x = airspeed*cos(alpha)*cos(beta);
  v_bw_b->y = airspeed*sin(beta);
  v_bw_b->z = airspeed*cos(beta)*sin(alpha);
}

variable_types: alpha double

variable_types: beta double

variable_types: airspeed double


bound

decompiled_code:
double __fastcall bound(double a1, double a2, double a3)
{
float v3; // xmm0_4
double result; // xmm0_8
float v5; // xmm0_4
float v6; // xmm0_4

v3 = a1;
result = v3;
if ( a2 > result )
{
v5 = a2;
result = v5;
}
if ( result > a3 )
{
v6 = a3;
result = v6;
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(double a1, double a2, double a3)
{
float v3; // xmm0_4
double result; // xmm0_8
float v5; // xmm0_4
float v6; // xmm0_4

v3 = a1;
result = v3;
if ( a2 > result )
{
v5 = a2;
result = v5;
}
if ( result > a3 )
{
v6 = a3;
result = v6;
}
return result;
}

original_code:
double bound(const double in, const double min, const double max)
{
 float ret = in;
 if(ret<min) ret = min;
 if(ret>max) ret = max;
 return ret;
}

canonical_original_code:
double bound(double in, double min, double max)
{
 float ret = in;
 if(ret<min) ret = min;
 if(ret>max) ret = max;
 return ret;
}

variable_types: in double

variable_types: min double

variable_types: max double

variable_types: ret float


decompiled_code:
void __fastcall simple_lowpass(double *a1, double a2, double a3, double a4)
{
double v4; // xmm1_8

v4 = exp(COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC0) / a3);
*a1 = (1.0 - v4) * a4 + *a1 * v4;
}

canonical_decompiled_code:
void __fastcall func0(double *a1, double a2, double a3, double a4)
{
double v4; // xmm1_8

v4 = exp(COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC0) / a3);
*a1 = (1.0 - v4) * a4 + *a1 * v4;
}

original_code:
void
simple_lowpass(double dt, double tau, double *state, double input)
{
  double emdt = exp(-dt/tau);
  *state = (*state)*emdt + input*(1-emdt);
}

variable_types: dt double

variable_types: tau double

variable_types:
state
double *

variable_types: input double

variable_types: emdt double


decompiled_code:
void __fastcall xyz_simple_lowpass(double *a1, double *a2, double a3, double a4)
{
simple_lowpass(a1, a3, a4, *a2);
simple_lowpass(a1 + 1, a3, a4, a2[1]);
simple_lowpass(a1 + 2, a3, a4, a2[2]);
}

canonical_decompiled_code:
void __fastcall func1(double *a1, double *a2, double a3, double a4)
{
func0(a1, a3, a4, *a2);
func0(a1 + 1, a3, a4, a2[1]);
func0(a1 + 2, a3, a4, a2[2]);
}

original_code:
void
xyz_simple_lowpass(double dt, double tau, xyz_t * state, const xyz_t * const input)
{
  simple_lowpass(dt, tau, &(state->x), input->x);
  simple_lowpass(dt, tau, &(state->y), input->y);
  simple_lowpass(dt, tau, &(state->z), input->z);
}

canonical_original_code:
void xyz_simple_lowpass(double dt, double tau, struct xyz_t *state, struct xyz_t *input)
{
  simple_lowpass(dt, tau, &(state->x), input->x);
  simple_lowpass(dt, tau, &(state->y), input->y);
  simple_lowpass(dt, tau, &(state->z), input->z);
}

variable_types: struct xyz_t { double x; double y; double z; } *

variable_types:
input
struct xyz_t *


decompiled_code:
int __fastcall simple_pid(double *a1, double *a2, double a3, double a4, double a5, double a6, double a7)
{
double v8; // xmm4_8
double v9; // xmm1_8
int result; // eax

if ( *a2 < 0.0 )
{
result = puts("warning: negative p gain in simple_pid");
if ( a2[1] >= 0.0 )
{
LABEL_3:
if ( a2[2] >= 0.0 )
goto LABEL_4;
LABEL_8:
result = puts("warning: negative d gain in simple_pid");
if ( a2[3] >= 0.0 )
goto LABEL_5;
LABEL_9:
result = puts("warning: negative integral state bound simple_pid");
goto LABEL_5;
}
}
else if ( a2[1] >= 0.0 )
{
goto LABEL_3;
}
result = puts("warning: negative i gain in simple_pid");
if ( a2[2] < 0.0 )
goto LABEL_8;
LABEL_4:
if ( a2[3] < 0.0 )
goto LABEL_9;
LABEL_5:
v8 = a7 * (a5 - a3) + *a1;
*a1 = v8;
v9 = fmin(a2[3], v8);
*a1 = v9;
*a1 = fmax(COERCE_DOUBLE(*((_QWORD *)a2 + 3) ^ LC0), v9);
return result;
}

canonical_decompiled_code:
int __fastcall func2(double *a1, double *a2, double a3, double a4, double a5, double a6, double a7)
{
double v8; // xmm4_8
double v9; // xmm1_8
int result; // eax

if ( *a2 < 0.0 )
{
result = puts("warning: negative p gain in simple_pid");
if ( a2[1] >= 0.0 )
{
LABEL_3:
if ( a2[2] >= 0.0 )
goto LABEL_4;
LABEL_8:
result = puts("warning: negative d gain in simple_pid");
if ( a2[3] >= 0.0 )
goto LABEL_5;
LABEL_9:
result = puts("warning: negative integral state bound simple_pid");
goto LABEL_5;
}
}
else if ( a2[1] >= 0.0 )
{
goto LABEL_3;
}
result = puts("warning: negative i gain in simple_pid");
if ( a2[2] < 0.0 )
goto LABEL_8;
LABEL_4:
if ( a2[3] < 0.0 )
goto LABEL_9;
LABEL_5:
v8 = a7 * (a5 - a3) + *a1;
*a1 = v8;
v9 = fmin(a2[3], v8);
*a1 = v9;
*a1 = fmax(COERCE_DOUBLE(*((_QWORD *)a2 + 3) ^ LC0), v9);
return result;
}

original_code:
double
simple_pid(const double y,
           const double y_dot,
           const double r,
           const double r_dot,
           const double dt,
           double *i_state,
           const pid_conf_t * const c)
{
  if (c->kp < 0.0) printf("warning: negative p gain in simple_pid\n");
  if (c->ki < 0.0) printf("warning: negative i gain in simple_pid\n");
  if (c->kd < 0.0) printf("warning: negative d gain in simple_pid\n");
  if (c->integral_bound < 0.0) printf("warning: negative integral state bound simple_pid\n");
  double err = r - y;
  double err_dot = r_dot - y_dot;
  *i_state += err*dt;
  { if ((*i_state) > (c->integral_bound)) (*i_state) = (c->integral_bound); if ((*i_state) < (-c->integral_bound)) (*i_state) = (-c->integral_bound); };
  return c->kp * (err + c->ki * (*i_state) + c->kd * err_dot);
}

canonical_original_code:
double simple_pid(double y, double y_dot, double r, double r_dot, double dt, double *i_state, struct pid_conf_t *c)
{
  if (c->kp < 0.0) printf("warning: negative p gain in simple_pid\n");
  if (c->ki < 0.0) printf("warning: negative i gain in simple_pid\n");
  if (c->kd < 0.0) printf("warning: negative d gain in simple_pid\n");
  if (c->integral_bound < 0.0) printf("warning: negative integral state bound simple_pid\n");
  double err = r - y;
  double err_dot = r_dot - y_dot;
  *i_state += err*dt;
  { if ((*i_state) > (c->integral_bound)) (*i_state) = (c->integral_bound); if ((*i_state) < (-c->integral_bound)) (*i_state) = (-c->integral_bound); };
  return c->kp * (err + c->ki * (*i_state) + c->kd * err_dot);
}

variable_types: y double

variable_types: r double

variable_types: struct pid_conf_t { double kp; double ki; double kd; double integral_bound; } *

variable_types: err double


decompiled_code:
_DWORD *buffer_init()
{
_DWORD *result; // rax

result = malloc(0x20uLL);
if ( !result )
exit(1);
result[6] = 0;
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

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

result = malloc(0x20uLL);
if ( !result )
exit(1);
result[6] = 0;
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

original_code:
buffer *buffer_init(){
    buffer *buf;
    buf = (buffer *)malloc(sizeof(buffer));
    if(!buf)
        exit(1);
 buf->size = 0;
 buf->first = ((void *)0);
 buf->last = ((void *)0);
    buf->current = ((void *)0);
    return buf;
}

canonical_original_code:
struct buffer *buffer_init(){
    struct buffer *buf;
    buf = (struct buffer *)malloc(sizeof(buffer));
    if(!buf)
        exit(1);
 buf->size = 0;
 buf->first = ((void *)0);
 buf->last = ((void *)0);
    buf->current = ((void *)0);
    return buf;
}

variable_types: struct buffer { struct node { struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *val; struct node *next; } *first; struct node *last; struct node *current; int size; } *

user_defined_types: struct buffer { struct node *first; struct node *last; struct node *current; int size; }

user_defined_types: struct node { struct matrix *val; struct node *next; }

user_defined_types: struct matrix { int row; struct elemt *first; }


enqueue

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

result = *((unsigned int *)a1 + 6);
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
result = *a1;
a1[1] = a2;
*(_QWORD *)(result + 8) = a2;
*((_DWORD *)a1 + 6) = 2;
}
else if ( (int)result > 1 )
{
result = (unsigned int)(result + 1);
*(_QWORD *)(a1[1] + 8) = a2;
a1[1] = a2;
*((_DWORD *)a1 + 6) = result;
}
}
else
{
*a1 = a2;
*((_DWORD *)a1 + 6) = 1;
a1[2] = a2;
}
return result;
}

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

result = *((unsigned int *)a1 + 6);
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
result = *a1;
a1[1] = a2;
*(_QWORD *)(result + 8) = a2;
*((_DWORD *)a1 + 6) = 2;
}
else if ( (int)result > 1 )
{
result = (unsigned int)(result + 1);
*(_QWORD *)(a1[1] + 8) = a2;
a1[1] = a2;
*((_DWORD *)a1 + 6) = result;
}
}
else
{
*a1 = a2;
*((_DWORD *)a1 + 6) = 1;
a1[2] = a2;
}
return result;
}

original_code:
void enqueue(buffer *buf, struct node *new){
 if(buf->size==0){
  buf->first = new;
  buf->size = buf->size+1;
        buf->current = new;
 }
 else if(buf->size==1){
  buf->last=new;
  buf->first->next = new;
  buf->size = buf->size+1;
 }
 else if(buf->size > 1){
  (buf->last)->next = new;
  buf->last = new;
  buf->size = buf->size+1;
 }
}

canonical_original_code:
void enqueue(struct buffer *buf, struct node *new){
 if(buf->size==0){
  buf->first = new;
  buf->size = buf->size+1;
        buf->current = new;
 }
 else if(buf->size==1){
  buf->last=new;
  buf->first->next = new;
  buf->size = buf->size+1;
 }
 else if(buf->size > 1){
  (buf->last)->next = new;
  buf->last = new;
  buf->size = buf->size+1;
 }
}

variable_types:
new
struct node *

user_defined_types: struct elemt { int rPos; struct elemt *next; }


decompiled_code:
// attributes: thunk
void node_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func2(void *ptr)
{
free(ptr);
}

original_code:
void node_free(struct node *node){
 free(node);
}

variable_types: struct node { struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *val; struct node *next; } *


dequeue

original_code:
matrix *dequeue(buffer *buf){
 struct node *tempNode;
 matrix *temp;
 if(buf->size==2){
  temp = (buf->first)->val;
  buf->size=buf->size-1;
  tempNode = buf->first;
  buf->first = buf->last;
  buf->last = ((void *)0);
  node_free(tempNode);
        buf->current = buf->first;
 }
 else if(buf->size==1){
  temp = (buf->first)->val;
  tempNode = buf->first;
  buf->size=buf->size-1;
  node_free(tempNode);
  buf->first=((void *)0);
        buf->current=((void *)0);
 }
 else if(buf->size > 2){
        if(buf->current == buf->first){
            buf->current = buf->first->next;
        }
  tempNode = buf->first;
  temp = tempNode->val;
  buf->first=(buf->first)->next;
  node_free(tempNode);
  buf->size-=1;
 }
 return temp;
}

canonical_original_code:
struct matrix *dequeue(struct buffer *buf){
 struct node *tempNode;
 struct matrix *temp;
 if(buf->size==2){
  temp = (buf->first)->val;
  buf->size=buf->size-1;
  tempNode = buf->first;
  buf->first = buf->last;
  buf->last = ((void *)0);
  node_free(tempNode);
        buf->current = buf->first;
 }
 else if(buf->size==1){
  temp = (buf->first)->val;
  tempNode = buf->first;
  buf->size=buf->size-1;
  node_free(tempNode);
  buf->first=((void *)0);
        buf->current=((void *)0);
 }
 else if(buf->size > 2){
        if(buf->current == buf->first){
            buf->current = buf->first->next;
        }
  tempNode = buf->first;
  temp = tempNode->val;
  buf->first=(buf->first)->next;
  node_free(tempNode);
  buf->size-=1;
 }
 return temp;
}

variable_types: struct buffer { struct node { struct matrix *val; struct node *next; } *first; struct node *last; struct node *current; int size; } *

variable_types: struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *


decompiled_code:
void __fastcall node_matrix_free(void *ptr)
{
matrix_free(*(_QWORD *)ptr);
node_free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
matrix_free(*(_QWORD *)ptr);
func2(ptr);
}

original_code:
void node_matrix_free(struct node *node){
 matrix_free(node->val);
 node_free(node);
}


decompiled_code:
void __fastcall buffer_free(void *ptr)
{
int v1; // eax
_QWORD *v3; // rdi

v1 = *((_DWORD *)ptr + 6);
v3 = *(_QWORD **)ptr;
if ( v1 )
{
if ( v1 == 1 )
{
node_matrix_free(v3);
free(ptr);
}
else
{
while ( v3 )
{
*(_QWORD *)ptr = v3[1];
node_matrix_free(v3);
v3 = *(_QWORD **)ptr;
}
free(ptr);
}
}
else
{
node_free(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
int v1; // eax
_QWORD *v3; // rdi

v1 = *((_DWORD *)ptr + 6);
v3 = *(_QWORD **)ptr;
if ( v1 )
{
if ( v1 == 1 )
{
func4(v3);
free(ptr);
}
else
{
while ( v3 )
{
*(_QWORD *)ptr = v3[1];
func4(v3);
v3 = *(_QWORD **)ptr;
}
free(ptr);
}
}
else
{
func2(v3);
free(ptr);
}
}

original_code:
void buffer_free(buffer *buf){
 if(buf->size == 0){
  node_free(buf->first);
  free(buf);
 }
 else if(buf->size == 1){
        node_matrix_free(buf->first);
  free(buf);
 }
 else{
  while(buf->first != ((void *)0)){
   struct node *temp = buf->first;
   buf->first = buf->first->next;
   node_matrix_free(temp);
  }
  free(buf);
 }
}

canonical_original_code:
void buffer_free(struct buffer *buf){
 if(buf->size == 0){
  node_free(buf->first);
  free(buf);
 }
 else if(buf->size == 1){
        node_matrix_free(buf->first);
  free(buf);
 }
 else{
  while(buf->first != ((void *)0)){
   struct node *temp = buf->first;
   buf->first = buf->first->next;
   node_matrix_free(temp);
  }
  free(buf);
 }
}


repo: ghostshadow/cgaus

decompiled_code:
void __fastcall mat_mull(double *a1, int a2, double a3)
{
__int64 v4; // rax
double v5; // xmm0_8

if ( a2 > 0 )
{
v4 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
do
{
v5 = *a1++;
*(a1 - 1) = v5 * a3;
}
while ( a1 != (double *)v4 );
}
}

canonical_decompiled_code:
void __fastcall func0(double *a1, int a2, double a3)
{
__int64 v4; // rax
double v5; // xmm0_8

if ( a2 > 0 )
{
v4 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
do
{
v5 = *a1++;
*(a1 - 1) = v5 * a3;
}
while ( a1 != (double *)v4 );
}
}

original_code:
static void mat_mull(double* line,int col,double fact) {
 for(int ct=0;ct<col;++ct) line[ct]*=fact;
}

canonical_original_code:
static void mat_mull(double *line, int col, double fact) {
 for(int ct = 0;ct<col;++ct) line[ct]*=fact;
}

variable_types:
line
double *

variable_types: col int

variable_types: fact double

variable_types: ct int


original_code:
static void mat_addl(double* tline,int col,double* sline) {
 for(int ct=0;ct<col;++ct) tline[ct]+=sline[ct];
}

canonical_original_code:
static void mat_addl(double *tline, int col, double *sline) {
 for(int ct = 0;ct<col;++ct) tline[ct]+=sline[ct];
}

variable_types:
tline
double *

variable_types:
sline
double *


REPOOM

decompiled_code:
void __fastcall __noreturn REPOOM(const char *a1)
{
fprintf(stderr, "\n\nOut of memory: %s\n\n", a1);
abort();
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const char *a1)
{
fprintf(stderr, "\n\nOut of memory: %s\n\n", a1);
abort();
}

original_code:
static inline void REPOOM(char* x) {
 fprintf(stderr,"\n\nOut of memory: %s\n\n",x);
 abort();
}

canonical_original_code:
static inline void REPOOM(char *x) {
 fprintf(stderr,"\n\nOut of memory: %s\n\n",x);
 abort();
}

variable_types:
x
char *


original_code:
static int mat_swapl(mxp mat,int spos) {
 if((mat->val)[spos-1][spos-1]!=0) ;
 double* tmpp;
 if(!(tmpp=(mat->val)[spos-1]))
  REPOOM("tmpp@mat_swapl()");
 for(int rt=spos;rt<(mat->row);++rt) {
  if((mat->val)[rt][spos-1]==0)
   continue;
  (mat->val)[spos-1]=(mat->val)[rt];
  (mat->val)[rt]=tmpp;
  return 1;
 }
 return 0;
}

canonical_original_code:
static int mat_swapl(struct mat *mat, int spos) {
 if((mat->val)[spos-1][spos-1]!=0) ;
 double *tmpp;
 if(!(tmpp=(mat->val)[spos-1]))
  REPOOM("tmpp@mat_swapl()");
 for(int rt = spos;rt<(mat->row);++rt) {
  if((mat->val)[rt][spos-1]==0)
   continue;
  (mat->val)[spos-1]=(mat->val)[rt];
  (mat->val)[rt]=tmpp;
  return 1;
 }
 return 0;
}

variable_types:
mat
struct mat { double **val; int col; } *

variable_types: spos int

variable_types:
tmpp
double *

variable_types: rt int

return_type: int


decompiled_code:
__int64 __fastcall mat_get_col(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code: int mat_get_col(mxp mat) {return mat->col;}

canonical_original_code: int mat_get_col(struct mat *mat) {return mat->col;}


decompiled_code:
__int64 __fastcall mat_get_row(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code: int mat_get_row(mxp mat) {return mat->row;}

canonical_original_code: int mat_get_row(struct mat *mat) {return mat->row;}


decompiled_code:
_QWORD *mat_init()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
_QWORD *v2; // rax
_QWORD *v3; // rbx
_QWORD *v4; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
REPOOM("mat@mat_init()");
v1 = v0;
v0[1] = 0x100000002LL;
v2 = malloc(8uLL);
*v1 = v2;
v3 = v2;
if ( !v2 )
REPOOM("mat->val@mat_init()");
v4 = malloc(0x10uLL);
*v3 = v4;
if ( !v4 )
REPOOM("mat->val[0]@mat_init()");
*v4 = 0LL;
v4[1] = 0LL;
return v1;
}

canonical_decompiled_code:
_QWORD *func9()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
_QWORD *v2; // rax
_QWORD *v3; // rbx
_QWORD *v4; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
func2("mat@mat_init()");
v1 = v0;
v0[1] = 0x100000002LL;
v2 = malloc(8uLL);
*v1 = v2;
v3 = v2;
if ( !v2 )
func2("mat->val@mat_init()");
v4 = malloc(0x10uLL);
*v3 = v4;
if ( !v4 )
func2("mat->val[0]@mat_init()");
*v4 = 0LL;
v4[1] = 0LL;
return v1;
}

original_code:
mxp mat_init() {
 mxp mat;
 if(!(mat=malloc(sizeof(struct mat))))
  REPOOM("mat@mat_init()");
 (mat->col)=2;
 (mat->row)=1;
 if(!((mat->val)=malloc(sizeof(double*))))
  REPOOM("mat->val@mat_init()");
 if(!((mat->val)[0]=malloc(2*sizeof(double))))
  REPOOM("mat->val[0]@mat_init()");
 (mat->val)[0][0]=0;
 (mat->val)[0][1]=0;
 return mat;
}

canonical_original_code:
struct mat *mat_init() {
 struct mat *mat;
 if(!(mat=malloc(sizeof(struct mat))))
  REPOOM("mat@mat_init()");
 (mat->col)=2;
 (mat->row)=1;
 if(!((mat->val)=malloc(sizeof(double *))))
  REPOOM("mat->val@mat_init()");
 if(!((mat->val)[0]=malloc(2*sizeof(double))))
  REPOOM("mat->val[0]@mat_init()");
 (mat->val)[0][0]=0;
 (mat->val)[0][1]=0;
 return mat;
}

return_type: struct mat { double **val; int col; } *


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

if ( ptr )
{
if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}
}

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

if ( ptr )
{
if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void mat_destr(mxp mat) {
 if(!(mat==((void *)0))) {
  for(int rt=0;rt<(mat->row);++rt)
   free((mat->val)[rt]);
  free(mat->val);
  (mat->col)=0;
  (mat->row)=0;
  free(mat);
 }
}

canonical_original_code:
void mat_destr(struct mat *mat) {
 if(!(mat==((void *)0))) {
  for(int rt = 0;rt<(mat->row);++rt)
   free((mat->val)[rt]);
  free(mat->val);
  (mat->col)=0;
  (mat->row)=0;
  free(mat);
 }
}


decompiled_code:
double __fastcall mat_get_val(__int64 a1, int a2, int a3)
{
double result; // xmm0_8

result = 0.0;
if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 0.0;
else
result = *(double *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 8LL * a3);
}
return result;
}

canonical_decompiled_code:
double __fastcall func12(__int64 a1, int a2, int a3)
{
double result; // xmm0_8

result = 0.0;
if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 0.0;
else
result = *(double *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 8LL * a3);
}
return result;
}

original_code:
double mat_get_val(mxp mat,int r,int c) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  return (mat->val)[r][c];
 return -0;
}

canonical_original_code:
double mat_get_val(struct mat *mat, int r, int c) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  return (mat->val)[r][c];
 return -0;
}

variable_types: r int

variable_types: c int


decompiled_code:
__int64 __fastcall mat_set_val(__int64 a1, int a2, int a3, double a4)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 && a2 >= 0 && a3 >= 0 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
*(double *)(result + 8LL * a3) = a4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2, int a3, double a4)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 && a2 >= 0 && a3 >= 0 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
*(double *)(result + 8LL * a3) = a4;
}
return result;
}

original_code:
void mat_set_val(mxp mat,int r,int c,double val) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  (mat->val)[r][c]=val;
}

canonical_original_code:
void mat_set_val(struct mat *mat, int r, int c, double val) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  (mat->val)[r][c]=val;
}

variable_types: val double


decompiled_code:
__int64 __fastcall mat_solve(__int64 a1)
{
mat_solve_norm_isra_0(a1);
return mat_solvable_isra_0(*(_QWORD *)a1, *(_DWORD *)(a1 + 8), *(unsigned int *)(a1 + 12));
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
func6(a1);
return func5(*(_QWORD *)a1, *(_DWORD *)(a1 + 8), *(unsigned int *)(a1 + 12));
}

original_code:
int mat_solve(mxp mat) {
 mat_solve_norm(mat);
 return mat_solvable(mat);
}

canonical_original_code:
int mat_solve(struct mat *mat) {
 mat_solve_norm(mat);
 return mat_solvable(mat);
}


repo: ghuls/moreutils-parallel

decompiled_code:
size_t warning_prefix()
{
fputs(progname, stderr);
return fwrite(&unk_402004, 1uLL, 2uLL, stderr);
}

canonical_decompiled_code:
size_t func7()
{
fputs(progname, stderr);
return fwrite(&unk_402004, 1uLL, 2uLL, stderr);
}

original_code:
static void warning_prefix(void) {
 fputs(progname, stderr);
 fputs(": ", stderr);
}

canonical_original_code:
static void warning_prefix() {
 fputs(progname, stderr);
 fputs(": ", stderr);
}


decompiled_code:
void __fastcall __noreturn error_with_errno(char *s)
{
unsigned int v1; // er12

v1 = *__errno_location();
warning_prefix();
fputs(s, stderr);
fwrite(": error number ", 1uLL, 0xFuLL, stderr);
fprintf(stderr, "%i\n", v1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(char *s)
{
unsigned int v1; // er12

v1 = *__errno_location();
func7();
fputs(s, stderr);
fwrite(": error number ", 1uLL, 0xFuLL, stderr);
fprintf(stderr, "%i\n", v1);
exit(1);
}

original_code:
__attribute__(( noreturn ))
static void error_with_errno(const char* message) {
 int saved_errno;
 saved_errno=(*__errno_location ());
 warning_prefix();
 fputs(message, stderr);
 fputs(": error number ", stderr);
 fprintf(stderr, "%i\n", saved_errno);
 exit(1);
}

canonical_original_code:
static void error_with_errno(char *message) {
 int saved_errno;
 saved_errno=(*__errno_location ());
 warning_prefix();
 fputs(message, stderr);
 fputs(": error number ", stderr);
 fprintf(stderr, "%i\n", saved_errno);
 exit(1);
}

variable_types:
message
char *


decompiled_code:
void __fastcall __noreturn subprocess2(char *command)
{
if ( close(0) )
error_with_errno("Failed (in child) closing standard input");
if ( dup2(filedes[0], 0) != -1 )
{
if ( !close(filedes[0]) )
{
if ( close(fd) )
error_with_errno("Failed (in child) closing filedes[1]");
system(command);
if ( close(0) )
error_with_errno("Failed (in child) closing standard output (while cleaning up)");
exit(0);
}
error_with_errno("Failed (in child) closing filedes[0]");
}
error_with_errno("Failed (in child) redefining standard input");
}

canonical_decompiled_code:
void __fastcall __noreturn func9(char *command)
{
if ( close(0) )
func8("Failed (in child) closing standard input");
if ( dup2(filedes[0], 0) != -1 )
{
if ( !close(filedes[0]) )
{
if ( close(fd) )
func8("Failed (in child) closing filedes[1]");
system(command);
if ( close(0) )
func8("Failed (in child) closing standard output (while cleaning up)");
exit(0);
}
func8("Failed (in child) closing filedes[0]");
}
func8("Failed (in child) redefining standard input");
}

original_code:
__attribute__(( noreturn ))
static void subprocess2(const char* cmd) {
 if (close(0))
  error_with_errno("Failed (in child) closing standard input");
 if (dup2(filedes[0], 0) == -1)
  error_with_errno("Failed (in child) redefining standard input");
 if (close(filedes[0]))
  error_with_errno("Failed (in child) closing filedes[0]");
 if (close(filedes[1]))
  error_with_errno("Failed (in child) closing filedes[1]");
 system(cmd);
 if (close(0))
  error_with_errno("Failed (in child) closing standard output "
   " (while cleaning up)");
 exit(0);
}

canonical_original_code:
static void subprocess2(char *cmd) {
 if (close(0))
  error_with_errno("Failed (in child) closing standard input");
 if (dup2(filedes[0], 0) == -1)
  error_with_errno("Failed (in child) redefining standard input");
 if (close(filedes[0]))
  error_with_errno("Failed (in child) closing filedes[0]");
 if (close(filedes[1]))
  error_with_errno("Failed (in child) closing filedes[1]");
 system(cmd);
 if (close(0))
  error_with_errno("Failed (in child) closing standard output "
   " (while cleaning up)");
 exit(0);
}

variable_types:
cmd
char *


decompiled_code:
__int64 __fastcall shorten_status(__int16 a1)
{
int v2; // edi

v2 = a1 & 0x7F;
if ( !v2 )
return HIBYTE(a1);
if ( (char)(v2 + 1) > 1 )
return (unsigned int)(v2 + 128);
if ( (_BYTE)a1 != 127 )
{
if ( (a1 & 0x80u) != 0 )
error("Ow, somebody dumped core!");
error("shorten_status got an invalid status (?!)");
}
return (unsigned int)HIBYTE(a1) + 128;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int16 a1)
{
int v2; // edi

v2 = a1 & 0x7F;
if ( !v2 )
return HIBYTE(a1);
if ( (char)(v2 + 1) > 1 )
return (unsigned int)(v2 + 128);
if ( (_BYTE)a1 != 127 )
{
if ( (a1 & 0x80u) != 0 )
func10("Ow, somebody dumped core!");
func10("shorten_status got an invalid status (?!)");
}
return (unsigned int)HIBYTE(a1) + 128;
}

original_code:
__attribute__(( const ))
static int shorten_status(int status_big) {
 if ((((status_big) & 0x7f) == 0))
  return (((status_big) & 0xff00) >> 8);
 if ((((signed char) (((status_big) & 0x7f) + 1) >> 1) > 0))
  return 128+((status_big) & 0x7f);
 if ((((status_big) & 0xff) == 0x7f))
  return 128+(((status_big) & 0xff00) >> 8);
 if (((status_big) & 0x80))
   error("Ow, somebody dumped core!");
 error("shorten_status got an invalid status (?!)");
}

canonical_original_code:
static int shorten_status(int status_big) {
 if ((((status_big) & 0x7f) == 0))
  return (((status_big) & 0xff00) >> 8);
 if ((((char) (((status_big) & 0x7f) + 1) >> 1) > 0))
  return 128+((status_big) & 0x7f);
 if ((((status_big) & 0xff) == 0x7f))
  return 128+(((status_big) & 0xff00) >> 8);
 if (((status_big) & 0x80))
   error("Ow, somebody dumped core!");
 error("shorten_status got an invalid status (?!)");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // ebx
unsigned int v5; // ebp
__pid_t v6; // eax

progname = (char *)*argv;
if ( argc != 3 )
error("Wrong number of args, aborting\n", (char)argv);
if ( pipe(filedes) )
error_with_errno("pipe() failed");
v3 = fork();
v4 = v3;
if ( !v3 )
subprocess2((char *)argv[2]);
if ( v3 == -1 )
error_with_errno("fork() failed");
if ( close(1) )
error_with_errno("Failed closing standard output");
if ( dup2(fd, 1) == -1 )
error_with_errno("Failed redefining standard output");
if ( close(fd) )
error_with_errno("Failed closing filedes[1]");
if ( close(filedes[0]) )
error_with_errno("Failed closing filedes[0]");
v5 = system(argv[1]);
if ( close(1) )
error_with_errno("Failed closing standard output (while cleaning up)");
v6 = waitpid(v4, 0LL, 2);
if ( v6 == -1 )
error_with_errno("waitpid() failed");
if ( v4 != v6 )
error("waitpid(): Who died? %i\n", v6);
return shorten_status(v5);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // ebx
unsigned int v5; // ebp
__pid_t v6; // eax

progname = (char *)*argv;
if ( argc != 3 )
func10("Wrong number of args, aborting\n", (char)argv);
if ( pipe(filedes) )
func8("pipe() failed");
v3 = fork();
v4 = v3;
if ( !v3 )
func9((char *)argv[2]);
if ( v3 == -1 )
func8("fork() failed");
if ( close(1) )
func8("Failed closing standard output");
if ( dup2(fd, 1) == -1 )
func8("Failed redefining standard output");
if ( close(fd) )
func8("Failed closing filedes[1]");
if ( close(filedes[0]) )
func8("Failed closing filedes[0]");
v5 = system(argv[1]);
if ( close(1) )
func8("Failed closing standard output (while cleaning up)");
v6 = waitpid(v4, 0LL, 2);
if ( v6 == -1 )
func8("waitpid() failed");
if ( v4 != v6 )
func10("waitpid(): Who died? %i\n", v6);
return func11(v5);
}

original_code:
int main (int argc, const char ** argv) {
 int status_big;
 pid_t child2_pid;
 pid_t dead_pid;
 progname = argv[0];
 if (argc != 3)
  error("Wrong number of args, aborting\n");
 if (pipe(filedes))
  error_with_errno("pipe() failed");
 child2_pid = fork();
 if (child2_pid == 0)
  subprocess2(argv[2]);
 if (child2_pid == -1)
  error_with_errno("fork() failed");
 if (close(1))
  error_with_errno("Failed closing standard output");
 if (dup2(filedes[1], 1) == -1)
  error_with_errno("Failed redefining standard output");
 if (close(filedes[1]))
  error_with_errno("Failed closing filedes[1]");
 if (close(filedes[0]))
  error_with_errno("Failed closing filedes[0]");
 status_big = system(argv[1]);
 if (close(1))
  error_with_errno("Failed closing standard output (while cleaning up)");
 dead_pid = waitpid(child2_pid, ((void *)0), 2);
 if (dead_pid == -1) {
  error_with_errno("waitpid() failed");
 }
 else if (dead_pid != child2_pid) {
  error("waitpid(): Who died? %i\n", dead_pid);
 }
 return shorten_status(status_big);
}

canonical_original_code:
int main(int argc, char **argv) {
 int status_big;
 int child2_pid;
 int dead_pid;
 progname = argv[0];
 if (argc != 3)
  error("Wrong number of args, aborting\n");
 if (pipe(filedes))
  error_with_errno("pipe() failed");
 child2_pid = fork();
 if (child2_pid == 0)
  subprocess2(argv[2]);
 if (child2_pid == -1)
  error_with_errno("fork() failed");
 if (close(1))
  error_with_errno("Failed closing standard output");
 if (dup2(filedes[1], 1) == -1)
  error_with_errno("Failed redefining standard output");
 if (close(filedes[1]))
  error_with_errno("Failed closing filedes[1]");
 if (close(filedes[0]))
  error_with_errno("Failed closing filedes[0]");
 status_big = system(argv[1]);
 if (close(1))
  error_with_errno("Failed closing standard output (while cleaning up)");
 dead_pid = waitpid(child2_pid, ((void *)0), 2);
 if (dead_pid == -1) {
  error_with_errno("waitpid() failed");
 }
 else if (dead_pid != child2_pid) {
  error("waitpid(): Who died? %i\n", dead_pid);
 }
 return shorten_status(status_big);
}

variable_types: argc int

variable_types:
argv
char **


error

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

va_start(arg, format);
warning_prefix();
vfprintf(stderr, format, arg);
exit(1);
}

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

va_start(arg, format);
func7();
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
__attribute__(( noreturn, format (printf, 1, 2) ))
static void error(const char* formatstr, ...) {
 va_list ap;
 __builtin_va_start(ap,formatstr);
 warning_prefix();
 vfprintf(stderr, formatstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
static void error(char *formatstr, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,formatstr);
 warning_prefix();
 vfprintf(stderr, formatstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
formatstr
char *


sigalarm

decompiled_code:
void __noreturn sigalarm()
{
if ( quiet )
_exit(75);
error(0, 75, "lock file `%s' is already locked (timeout waiting)", *(const char **)lckfile);
}

canonical_decompiled_code:
void __noreturn func8()
{
if ( quiet )
_exit(75);
func7(0, 75, "lock file `%s' is already locked (timeout waiting)", *(const char **)lckfile);
}

original_code:
static void sigalarm(int sig) {
 if (quiet)
  _exit(75);
 error(0, 75,
  "lock file `%s' is already locked (timeout waiting)", lckfile);
}

variable_types: sig int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // er13
int v6; // er15
int v7; // er12
int v8; // er14
int v9; // eax
char v10; // cl
int v11; // er12
char v13; // r14
int v14; // eax
const char **v15; // rbx
const char *v16; // rdi
int v17; // ebp
int v18; // edi
__int16 v19; // r15
__int64 v20; // rcx
_DWORD *v21; // rdi
int v22; // esi
int v23; // eax
__int64 v24; // rcx
_DWORD *v25; // rdi
int v26; // eax
__pid_t v27; // eax
int v28; // edi
__int64 v29; // rcx
__int16 v30; // r15
_DWORD *v31; // rdi
int *v32; // rax
int *v33; // rax
char v34; // cl
int *v35; // rax
int *v36; // rax
int *v37; // rax
char v38; // cl
int *v39; // rax
char v40; // cl
int v41; // [rsp+0h] [rbp-78h]
int oflag; // [rsp+4h] [rbp-74h]
char v43; // [rsp+8h] [rbp-70h]
char v44; // [rsp+8h] [rbp-70h]
__WAIT_STATUS stat_loc; // [rsp+1Ch] [rbp-5Ch] BYREF
unsigned int v46; // [rsp+38h] [rbp-40h]

v3 = *argv;
v4 = strrchr(*argv, 47);
if ( v4 )
{
progname = (__int64)(v4 + 1);
*argv = v4 + 1;
}
else
{
progname = (__int64)v3;
}
if ( argc == 1 )
{
v11 = 0;
printf(
"%s: execute a program with a lock set.\n"
"Usage: %s [options] lockfile program [arguments]\n"
"where options are:\n"
" -w - if the lock is already held by another process,\n"
" wait for it to complete instead of failing immediately\n"
" -W sec - the same as -w but wait not more than sec seconds\n"
" -e - execute the program directly, no fork/wait\n"
" (keeps extra open file descriptor)\n"
" -E nnn - set the fd# to keep open in -e case (implies -e)\n"
" -n - do not create the lock file if it does not exist\n"
" -q - produce no output if lock is already held\n"
" -s - lock in shared (read) mode\n"
" -x - lock in exclusive (write) mode (default)\n"
" -t - test for lock existence (just prints pid if any with -q)\n"
" (implies -n)\n",
(const char *)progname,
(const char *)progname);
return v11;
}
v41 = 0;
v5 = -1;
v6 = 0;
v7 = 0;
oflag = 64;
v8 = 1;
while ( 2 )
{
v9 = getopt(argc, (char *const *)argv, "+wW:neE:sxtq");
LODWORD(stat_loc.__uptr) = v9;
if ( v9 != -1 )
{
switch ( v9 )
{
case 'E':
v13 = (char)optarg;
v14 = atoi(optarg);
v5 = v14;
if ( v14 < 0 || v14 == 2 )
error(0, 64, "invalid fd# `%s'", v13);
goto LABEL_11;
case 'W':
v43 = (char)optarg;
v7 = atoi(optarg);
if ( v7 <= 0 )
error(0, 64, "invalid wait time `%s'", v43);
continue;
case 'e':
LABEL_11:
v8 = 0;
continue;
case 'n':
goto LABEL_13;
case 'q':
quiet = 1;
continue;
case 's':
v6 = 1;
continue;
case 't':
v41 = 1;
LABEL_13:
oflag = 0;
continue;
case 'w':
if ( !v7 )
v7 = -1;
continue;
case 'x':
v6 = 0;
continue;
default:
return 64;
}
}
break;
}
v15 = &argv[optind];
if ( argc == optind || (v44 = v41 ^ 1, argc - optind <= 1) && (unsigned __int8)v41 != 1 )
error(0, 64, "too few arguments given", v10);
v16 = *v15;
*(_QWORD *)lckfile = *v15;
if ( !v41 )
{
v17 = open(v16, oflag | v6 ^ 1, 438LL);
if ( v17 >= 0 )
{
if ( v5 < 0 || !v44 )
goto LABEL_32;
goto LABEL_30;
}
LABEL_77:
v32 = __errno_location();
error(*v32, 73, "unable to open `%s'", lckfile[0]);
}
v17 = open(v16, oflag, 438LL);
if ( v17 < 0 )
{
if ( *__errno_location() == 2 )
{
v11 = quiet;
LABEL_48:
if ( v11 )
return 0;
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
return v11;
}
goto LABEL_77;
}
if ( v5 < 0 || !v44 )
{
v29 = 8LL;
v30 = v6 ^ 1;
v31 = (_DWORD *)&stat_loc.__iptr + 1;
v22 = 5;
while ( v29 )
{
*v31++ = 0;
--v29;
}
WORD2(stat_loc.__iptr) = v30;
goto LABEL_38;
}
LABEL_30:
if ( dup2(v17, v5) < 0 )
{
v35 = __errno_location();
error(*v35, 71, "dup2(%d,%d) failed", v17);
}
v18 = v17;
v17 = v5;
close(v18);
LABEL_32:
v19 = v6 ^ 1;
if ( v7 <= 0 )
{
v20 = 8LL;
v21 = (_DWORD *)&stat_loc.__iptr + 1;
while ( v20 )
{
*v21++ = 0;
--v20;
}
WORD2(stat_loc.__iptr) = v19;
if ( v7 )
{
v23 = fcntl(v17, 7, (char *)&stat_loc.__iptr + 4, 0LL);
goto LABEL_39;
}
v22 = 6;
LABEL_38:
v23 = fcntl(v17, v22, (char *)&stat_loc.__iptr + 4, 0LL);
LABEL_39:
LODWORD(stat_loc.__uptr) = v23;
if ( !v23 && v41 )
goto LABEL_41;
goto LABEL_56;
}
alarm(v7);
signal(14, sigalarm);
v24 = 8LL;
v25 = (_DWORD *)&stat_loc.__iptr + 1;
while ( v24 )
{
*v25++ = 0;
--v24;
}
WORD2(stat_loc.__iptr) = v19;
LODWORD(stat_loc.__uptr) = fcntl(v17, 7);
if ( !LODWORD(stat_loc.__uptr) && v41 )
{
LABEL_41:
v11 = quiet;
if ( WORD2(stat_loc.__iptr) != 2 )
{
v11 = 75;
if ( quiet )
printf("%d\n", v46);
else
printf("lockfile `%s' is locked by process %d\n", *(const char **)lckfile, v46);
return v11;
}
goto LABEL_48;
}
alarm(0);
LABEL_56:
if ( SLODWORD(stat_loc.__uptr) < 0 )
{
v28 = *__errno_location();
if ( ((v28 - 11) & 0xFFFFFFFD) != 0 )
error(v28, 71, "unable to lock `%s'", lckfile[0]);
v11 = 75;
if ( !quiet )
error(0, 75, "lockfile `%s' is already locked", lckfile[0]);
}
else
{
if ( !v8 )
goto LABEL_82;
v26 = fcntl(v17, 1, 0LL);
if ( v26 < 0 )
{
v39 = __errno_location();
error(*v39, 71, "fcntl() failed", v40);
}
fcntl(v17, 2, v26 | 1u);
v27 = fork();
if ( v27 < 0 )
{
v37 = __errno_location();
error(*v37, 71, "unable to fork", v38);
}
if ( !v27 )
{
LABEL_82:
execvp(v15[1], (char *const *)v15 + 1);
v36 = __errno_location();
error(*v36, 71, "unable to execute %s", (char)v15[1]);
}
if ( wait((__WAIT_STATUS)&stat_loc) < 0 )
{
v33 = __errno_location();
error(*v33, 71, "wait() failed", v34);
}
v11 = BYTE1(stat_loc.__uptr);
if ( !__OFSUB__(((__int64)stat_loc.__uptr & 0x7F) + 1, 1) && ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
strsignal((__int64)stat_loc.__uptr & 0x7F);
error(0, 70, "%s: %s", (char)v15[1]);
}
}
return v11;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // er13
int v6; // er15
int v7; // er12
int v8; // er14
int v9; // eax
char v10; // cl
int v11; // er12
char v13; // r14
int v14; // eax
const char **v15; // rbx
const char *v16; // rdi
int v17; // ebp
int v18; // edi
__int16 v19; // r15
__int64 v20; // rcx
_DWORD *v21; // rdi
int v22; // esi
int v23; // eax
__int64 v24; // rcx
_DWORD *v25; // rdi
int v26; // eax
__pid_t v27; // eax
int v28; // edi
__int64 v29; // rcx
__int16 v30; // r15
_DWORD *v31; // rdi
int *v32; // rax
int *v33; // rax
char v34; // cl
int *v35; // rax
int *v36; // rax
int *v37; // rax
char v38; // cl
int *v39; // rax
char v40; // cl
int v41; // [rsp+0h] [rbp-78h]
int oflag; // [rsp+4h] [rbp-74h]
char v43; // [rsp+8h] [rbp-70h]
char v44; // [rsp+8h] [rbp-70h]
__WAIT_STATUS stat_loc; // [rsp+1Ch] [rbp-5Ch] BYREF
unsigned int v46; // [rsp+38h] [rbp-40h]

v3 = *argv;
v4 = strrchr(*argv, 47);
if ( v4 )
{
progname = (__int64)(v4 + 1);
*argv = v4 + 1;
}
else
{
progname = (__int64)v3;
}
if ( argc == 1 )
{
v11 = 0;
printf(
"%s: execute a program with a lock set.\n"
"Usage: %s [options] lockfile program [arguments]\n"
"where options are:\n"
" -w - if the lock is already held by another process,\n"
" wait for it to complete instead of failing immediately\n"
" -W sec - the same as -w but wait not more than sec seconds\n"
" -e - execute the program directly, no fork/wait\n"
" (keeps extra open file descriptor)\n"
" -E nnn - set the fd# to keep open in -e case (implies -e)\n"
" -n - do not create the lock file if it does not exist\n"
" -q - produce no output if lock is already held\n"
" -s - lock in shared (read) mode\n"
" -x - lock in exclusive (write) mode (default)\n"
" -t - test for lock existence (just prints pid if any with -q)\n"
" (implies -n)\n",
(const char *)progname,
(const char *)progname);
return v11;
}
v41 = 0;
v5 = -1;
v6 = 0;
v7 = 0;
oflag = 64;
v8 = 1;
while ( 2 )
{
v9 = getopt(argc, (char *const *)argv, "+wW:neE:sxtq");
LODWORD(stat_loc.__uptr) = v9;
if ( v9 != -1 )
{
switch ( v9 )
{
case 'E':
v13 = (char)optarg;
v14 = atoi(optarg);
v5 = v14;
if ( v14 < 0 || v14 == 2 )
func7(0, 64, "invalid fd# `%s'", v13);
goto LABEL_11;
case 'W':
v43 = (char)optarg;
v7 = atoi(optarg);
if ( v7 <= 0 )
func7(0, 64, "invalid wait time `%s'", v43);
continue;
case 'e':
LABEL_11:
v8 = 0;
continue;
case 'n':
goto LABEL_13;
case 'q':
quiet = 1;
continue;
case 's':
v6 = 1;
continue;
case 't':
v41 = 1;
LABEL_13:
oflag = 0;
continue;
case 'w':
if ( !v7 )
v7 = -1;
continue;
case 'x':
v6 = 0;
continue;
default:
return 64;
}
}
break;
}
v15 = &argv[optind];
if ( argc == optind || (v44 = v41 ^ 1, argc - optind <= 1) && (unsigned __int8)v41 != 1 )
func7(0, 64, "too few arguments given", v10);
v16 = *v15;
*(_QWORD *)lckfile = *v15;
if ( !v41 )
{
v17 = open(v16, oflag | v6 ^ 1, 438LL);
if ( v17 >= 0 )
{
if ( v5 < 0 || !v44 )
goto LABEL_32;
goto LABEL_30;
}
LABEL_77:
v32 = __errno_location();
func7(*v32, 73, "unable to open `%s'", lckfile[0]);
}
v17 = open(v16, oflag, 438LL);
if ( v17 < 0 )
{
if ( *__errno_location() == 2 )
{
v11 = quiet;
LABEL_48:
if ( v11 )
return 0;
printf("lockfile `%s' is not locked\n", *(const char **)lckfile);
return v11;
}
goto LABEL_77;
}
if ( v5 < 0 || !v44 )
{
v29 = 8LL;
v30 = v6 ^ 1;
v31 = (_DWORD *)&stat_loc.__iptr + 1;
v22 = 5;
while ( v29 )
{
*v31++ = 0;
--v29;
}
WORD2(stat_loc.__iptr) = v30;
goto LABEL_38;
}
LABEL_30:
if ( dup2(v17, v5) < 0 )
{
v35 = __errno_location();
func7(*v35, 71, "dup2(%d,%d) failed", v17);
}
v18 = v17;
v17 = v5;
close(v18);
LABEL_32:
v19 = v6 ^ 1;
if ( v7 <= 0 )
{
v20 = 8LL;
v21 = (_DWORD *)&stat_loc.__iptr + 1;
while ( v20 )
{
*v21++ = 0;
--v20;
}
WORD2(stat_loc.__iptr) = v19;
if ( v7 )
{
v23 = fcntl(v17, 7, (char *)&stat_loc.__iptr + 4, 0LL);
goto LABEL_39;
}
v22 = 6;
LABEL_38:
v23 = fcntl(v17, v22, (char *)&stat_loc.__iptr + 4, 0LL);
LABEL_39:
LODWORD(stat_loc.__uptr) = v23;
if ( !v23 && v41 )
goto LABEL_41;
goto LABEL_56;
}
alarm(v7);
signal(14, sigalarm);
v24 = 8LL;
v25 = (_DWORD *)&stat_loc.__iptr + 1;
while ( v24 )
{
*v25++ = 0;
--v24;
}
WORD2(stat_loc.__iptr) = v19;
LODWORD(stat_loc.__uptr) = fcntl(v17, 7);
if ( !LODWORD(stat_loc.__uptr) && v41 )
{
LABEL_41:
v11 = quiet;
if ( WORD2(stat_loc.__iptr) != 2 )
{
v11 = 75;
if ( quiet )
printf("%d\n", v46);
else
printf("lockfile `%s' is locked by process %d\n", *(const char **)lckfile, v46);
return v11;
}
goto LABEL_48;
}
alarm(0);
LABEL_56:
if ( SLODWORD(stat_loc.__uptr) < 0 )
{
v28 = *__errno_location();
if ( ((v28 - 11) & 0xFFFFFFFD) != 0 )
func7(v28, 71, "unable to lock `%s'", lckfile[0]);
v11 = 75;
if ( !quiet )
func7(0, 75, "lockfile `%s' is already locked", lckfile[0]);
}
else
{
if ( !v8 )
goto LABEL_82;
v26 = fcntl(v17, 1, 0LL);
if ( v26 < 0 )
{
v39 = __errno_location();
func7(*v39, 71, "fcntl() failed", v40);
}
fcntl(v17, 2, v26 | 1u);
v27 = fork();
if ( v27 < 0 )
{
v37 = __errno_location();
func7(*v37, 71, "unable to fork", v38);
}
if ( !v27 )
{
LABEL_82:
execvp(v15[1], (char *const *)v15 + 1);
v36 = __errno_location();
func7(*v36, 71, "unable to execute %s", (char)v15[1]);
}
if ( wait((__WAIT_STATUS)&stat_loc) < 0 )
{
v33 = __errno_location();
func7(*v33, 71, "wait() failed", v34);
}
v11 = BYTE1(stat_loc.__uptr);
if ( !__OFSUB__(((__int64)stat_loc.__uptr & 0x7F) + 1, 1) && ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
strsignal((__int64)stat_loc.__uptr & 0x7F);
func7(0, 70, "%s: %s", (char)v15[1]);
}
}
return v11;
}

original_code:
int main(int argc, char **argv) {
 int fd;
 int c;
 int create = 0100;
 int dofork = 1;
 int waittime = 0;
 int shared = 0;
 int test = 0;
 int fdn = -1;
 struct flock fl;
 if ((progname = strrchr(argv[0], '/')) == ((void *)0))
  progname = argv[0];
 else
  argv[0] = ++progname;
 if (argc == 1) {
  printf(
   "%s: execute a program with a lock set.\n"
   "Usage: %s [options] lockfile program [arguments]\n"
   "where options are:\n"
   " -w - if the lock is already held by another process,\n"
   "   wait for it to complete instead of failing immediately\n"
   " -W sec - the same as -w but wait not more than sec seconds\n"
   " -e - execute the program directly, no fork/wait\n"
   "   (keeps extra open file descriptor)\n"
   " -E nnn - set the fd# to keep open in -e case (implies -e)\n"
   " -n - do not create the lock file if it does not exist\n"
   " -q - produce no output if lock is already held\n"
   " -s - lock in shared (read) mode\n"
   " -x - lock in exclusive (write) mode (default)\n"
   " -t - test for lock existence"
   " (just prints pid if any with -q)\n"
   "   (implies -n)\n"
  , progname, progname);
  return 0;
 }
 while ((c = getopt(argc, argv, "+wW:neE:sxtq")) != (-1)) {
  switch(c) {
  case 'w':
   if (!waittime)
    waittime = -1;
   break;
  case 'W':
   if ((waittime = atoi(optarg)) < 1)
    error(0, 64, "invalid wait time `%s'", optarg);
   break;
  case 't':
   test = 1;
  case 'n':
   create = 0;
   break;
  case 'E':
   if ((fdn = atoi(optarg)) < 0 || fdn == 2)
    error(0, 64, "invalid fd# `%s'", optarg);
  case 'e':
   dofork = 0;
   break;
  case 's':
   shared = 1;
   break;
  case 'x':
   shared = 0;
   break;
  case 'q':
   quiet = 1;
   break;
  default:
   return 64;
  }
 }
 argc -= optind; argv += optind;
 if (!argc || (!test && argc < 2))
  error(0, 64, "too few arguments given");
 lckfile = *argv++;
 if (!test)
  create |= shared ? 00 : 01;
 fd = open(lckfile, create, 0666);
 if (fd < 0) {
  if (test && (*__errno_location ()) == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  error((*__errno_location ()), 73, "unable to open `%s'", lckfile);
 }
 if (!test && fdn >= 0) {
  if (dup2(fd, fdn) < 0)
   error((*__errno_location ()), 71, "dup2(%d,%d) failed", fd, fdn);
  close(fd);
  fd = fdn;
 }
 if (test)
  waittime = 0;
 else if (waittime > 0) {
  alarm(waittime);
  signal(14, sigalarm);
 }
 memset(&fl, 0, sizeof(fl));
 fl.l_type = shared ? 0 : 1;
 c = fcntl(fd, test ? 5 : waittime ? 7 : 6, &fl);
 if (test && c == 0) {
  if (fl.l_type == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  if (!quiet)
   printf("lockfile `%s' is locked by process %d\n", lckfile, fl.l_pid);
  else
   printf("%d\n", fl.l_pid);
  return 75;
 }
 if (waittime > 0)
  alarm(0);
 if (c < 0) {
  if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11 && (*__errno_location ()) != 13)
   error((*__errno_location ()), 71, "unable to lock `%s'", lckfile);
  else if (quiet)
   return 75;
  else
   error(0, 75, "lockfile `%s' is already locked", lckfile);
 }
 if (dofork) {
  pid_t pid;
  int flags = fcntl(fd, 1, 0);
  if (flags < 0)
   error((*__errno_location ()), 71, "fcntl() failed");
  fcntl(fd, 2, flags | 1);
  pid = fork();
  if (pid < 0)
   error((*__errno_location ()), 71, "unable to fork");
  else if (pid) {
   if (wait(&c) < 0)
    error((*__errno_location ()), 71, "wait() failed");
   if ((((signed char) (((c) & 0x7f) + 1) >> 1) > 0))
    error(0, 70, "%s: %s", *argv,
      strsignal(((c) & 0x7f)));
   return (((c) & 0xff00) >> 8);
  }
 }
 execvp(*argv, argv);
 error((*__errno_location ()), 71, "unable to execute %s", *argv);
}

canonical_original_code:
int main(int argc, char **argv) {
 int fd;
 int c;
 int create = 0100;
 int dofork = 1;
 int waittime = 0;
 int shared = 0;
 int test = 0;
 int fdn = -1;
 struct flock fl;
 if ((progname = strrchr(argv[0], '/')) == ((void *)0))
  progname = argv[0];
 else
  argv[0] = ++progname;
 if (argc == 1) {
  printf(
   "%s: execute a program with a lock set.\n"
   "Usage: %s [options] lockfile program [arguments]\n"
   "where options are:\n"
   " -w - if the lock is already held by another process,\n"
   "   wait for it to complete instead of failing immediately\n"
   " -W sec - the same as -w but wait not more than sec seconds\n"
   " -e - execute the program directly, no fork/wait\n"
   "   (keeps extra open file descriptor)\n"
   " -E nnn - set the fd# to keep open in -e case (implies -e)\n"
   " -n - do not create the lock file if it does not exist\n"
   " -q - produce no output if lock is already held\n"
   " -s - lock in shared (read) mode\n"
   " -x - lock in exclusive (write) mode (default)\n"
   " -t - test for lock existence"
   " (just prints pid if any with -q)\n"
   "   (implies -n)\n"
  , progname, progname);
  return 0;
 }
 while ((c = getopt(argc, argv, "+wW:neE:sxtq")) != (-1)) {
  switch(c) {
  case 'w':
   if (!waittime)
    waittime = -1;
   break;
  case 'W':
   if ((waittime = atoi(optarg)) < 1)
    error(0, 64, "invalid wait time `%s'", optarg);
   break;
  case 't':
   test = 1;
  case 'n':
   create = 0;
   break;
  case 'E':
   if ((fdn = atoi(optarg)) < 0 || fdn == 2)
    error(0, 64, "invalid fd# `%s'", optarg);
  case 'e':
   dofork = 0;
   break;
  case 's':
   shared = 1;
   break;
  case 'x':
   shared = 0;
   break;
  case 'q':
   quiet = 1;
   break;
  default:
   return 64;
  }
 }
 argc -= optind; argv += optind;
 if (!argc || (!test && argc < 2))
  error(0, 64, "too few arguments given");
 lckfile = *argv++;
 if (!test)
  create |= shared ? 00 : 01;
 fd = open(lckfile, create, 0666);
 if (fd < 0) {
  if (test && (*__errno_location ()) == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  error((*__errno_location ()), 73, "unable to open `%s'", lckfile);
 }
 if (!test && fdn >= 0) {
  if (dup2(fd, fdn) < 0)
   error((*__errno_location ()), 71, "dup2(%d,%d) failed", fd, fdn);
  close(fd);
  fd = fdn;
 }
 if (test)
  waittime = 0;
 else if (waittime > 0) {
  alarm(waittime);
  signal(14, sigalarm);
 }
 memset(&fl, 0, sizeof(fl));
 fl.l_type = shared ? 0 : 1;
 c = fcntl(fd, test ? 5 : waittime ? 7 : 6, &fl);
 if (test && c == 0) {
  if (fl.l_type == 2) {
   if (!quiet)
    printf("lockfile `%s' is not locked\n", lckfile);
   return 0;
  }
  if (!quiet)
   printf("lockfile `%s' is locked by process %d\n", lckfile, fl.l_pid);
  else
   printf("%d\n", fl.l_pid);
  return 75;
 }
 if (waittime > 0)
  alarm(0);
 if (c < 0) {
  if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11 && (*__errno_location ()) != 13)
   error((*__errno_location ()), 71, "unable to lock `%s'", lckfile);
  else if (quiet)
   return 75;
  else
   error(0, 75, "lockfile `%s' is already locked", lckfile);
 }
 if (dofork) {
  int pid;
  int flags = fcntl(fd, 1, 0);
  if (flags < 0)
   error((*__errno_location ()), 71, "fcntl() failed");
  fcntl(fd, 2, flags | 1);
  pid = fork();
  if (pid < 0)
   error((*__errno_location ()), 71, "unable to fork");
  else if (pid) {
   if (wait(&c) < 0)
    error((*__errno_location ()), 71, "wait() failed");
   if ((((char) (((c) & 0x7f) + 1) >> 1) > 0))
    error(0, 70, "%s: %s", *argv,
      strsignal(((c) & 0x7f)));
   return (((c) & 0xff00) >> 8);
  }
 }
 execvp(*argv, argv);
 error((*__errno_location ()), 71, "unable to execute %s", *argv);
}

variable_types: fd int

variable_types: create int

variable_types: dofork int

variable_types: waittime int

variable_types: shared int

variable_types: test int

variable_types: fdn int

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }

variable_types: pid int

variable_types: flags int


error

decompiled_code:
void __noreturn error(int errnum, int status, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: ", (const char *)progname);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s\n", v4);
}
else
{
fputc(10, stderr);
}
exit(status);
}

canonical_decompiled_code:
void __noreturn func7(int errnum, int status, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: ", (const char *)progname);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s\n", v4);
}
else
{
fputc(10, stderr);
}
exit(status);
}

original_code:
static void
__attribute__((format(printf,3,4)))
__attribute__((noreturn))
error(int errnum, int exitcode, const char *fmt, ...) {
 va_list ap;
 fprintf(stderr, "%s: ", progname);
 __builtin_va_start(ap,fmt); vfprintf(stderr, fmt, ap); __builtin_va_end(ap);
 if (errnum)
  fprintf(stderr, ": %s\n", strerror(errnum));
 else
  fputs("\n", stderr);
 exit(exitcode);
}

canonical_original_code:
static void error(int errnum, int exitcode, char *fmt, ...) {
 __builtin_va_list ap;
 fprintf(stderr, "%s: ", progname);
 __builtin_va_start(ap,fmt); vfprintf(stderr, fmt, ap); __builtin_va_end(ap);
 if (errnum)
  fprintf(stderr, ": %s\n", strerror(errnum));
 else
  fputs("\n", stderr);
 exit(exitcode);
}

variable_types: errnum int

variable_types: exitcode int

variable_types:
fmt
char *


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

result = (unsigned __int8)a1 & 0x80;
if ( (a1 & 0x80) != 0 )
{
LODWORD(result) = 0;
do
{
a1 *= 2;
result = (unsigned int)(result + 1);
}
while ( (a1 & 0x80u) != 0 );
}
return result;
}

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

result = (unsigned __int8)a1 & 0x80;
if ( (a1 & 0x80) != 0 )
{
LODWORD(result) = 0;
do
{
a1 *= 2;
result = (unsigned int)(result + 1);
}
while ( (a1 & 0x80u) != 0 );
}
return result;
}

original_code:
static int high_ones(int c) {
        int n;
        for (n = 0; (c & 0x80) == 0x80; c <<= 1)
                ++n;
        return n;
}

variable_types: c int

variable_types: n int


decompiled_code:
__int64 __fastcall decodeutf8(unsigned __int8 *a1, int a2)
{
__int64 v2; // r8
unsigned __int8 v3; // al
int v4; // edx
int v5; // ecx
__int64 v6; // r9
int v7; // eax
__int64 v8; // rdx
__int64 v9; // r8
char v10; // al

v2 = 0xFFFFFFFFLL;
if ( a2 <= 0 )
return v2;
v3 = *a1;
if ( a2 == 1 )
{
if ( (v3 & 0x80u) == 0 )
v2 = v3;
return v2;
}
v5 = high_ones(v3);
if ( a2 != v5 )
return v2;
v7 = v4 & (255 >> v5);
v8 = 1LL;
v9 = v7;
do
{
v10 = *(_BYTE *)(v6 + v8);
if ( (v10 & 0xC0) != 0x80 )
return 0xFFFFFFFFLL;
++v8;
v9 = v10 & 0x3F | (unsigned __int64)(v9 << 6);
}
while ( v5 > (int)v8 );
if ( (unsigned __int64)(v9 - 55296) <= 0x7FF )
return 0xFFFFFFFFLL;
if ( (unsigned __int64)(v9 - 65534) <= 1 )
v9 = 0xFFFFFFFFLL;
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 *a1, int a2)
{
__int64 v2; // r8
unsigned __int8 v3; // al
int v4; // edx
int v5; // ecx
__int64 v6; // r9
int v7; // eax
__int64 v8; // rdx
__int64 v9; // r8
char v10; // al

v2 = 0xFFFFFFFFLL;
if ( a2 <= 0 )
return v2;
v3 = *a1;
if ( a2 == 1 )
{
if ( (v3 & 0x80u) == 0 )
v2 = v3;
return v2;
}
v5 = func8(v3);
if ( a2 != v5 )
return v2;
v7 = v4 & (255 >> v5);
v8 = 1LL;
v9 = v7;
do
{
v10 = *(_BYTE *)(v6 + v8);
if ( (v10 & 0xC0) != 0x80 )
return 0xFFFFFFFFLL;
++v8;
v9 = v10 & 0x3F | (unsigned __int64)(v9 << 6);
}
while ( v5 > (int)v8 );
if ( (unsigned __int64)(v9 - 55296) <= 0x7FF )
return 0xFFFFFFFFLL;
if ( (unsigned __int64)(v9 - 65534) <= 1 )
v9 = 0xFFFFFFFFLL;
return v9;
}

original_code:
static unsigned long decodeutf8(unsigned char *buf, int nbytes)
{
        unsigned long u;
        int i, j;
        if (nbytes <= 0)
                return INVALID_CHAR;
        if (nbytes == 1) {
                if (buf[0] >= 0x80)
                        return INVALID_CHAR;
                return buf[0];
        }
        i = high_ones(buf[0]);
        if (i != nbytes)
                return INVALID_CHAR;
        u = buf[0] & (0xff >> i);
        for (j = 1; j < nbytes; ++j) {
                if ((buf[j] & 0xC0) != 0x80)
                            return INVALID_CHAR;
                u = (u << 6) | (buf[j] & 0x3f);
        }
        if (u >= 0xD800 && u <= 0xDFFF)
            return INVALID_CHAR;
        if (u == 0xFFFE || u == 0xFFFF)
            return INVALID_CHAR;
        return u;
}

canonical_original_code:
static unsigned long decodeutf8(unsigned char *buf, int nbytes)
{
        unsigned long u;
        int i; int j;
        if (nbytes <= 0)
                return INVALID_CHAR;
        if (nbytes == 1) {
                if (buf[0] >= 0x80)
                        return INVALID_CHAR;
                return buf[0];
        }
        i = high_ones(buf[0]);
        if (i != nbytes)
                return INVALID_CHAR;
        u = buf[0] & (0xff >> i);
        for (j = 1; j < nbytes; ++j) {
                if ((buf[j] & 0xC0) != 0x80)
                            return INVALID_CHAR;
                u = (u << 6) | (buf[j] & 0x3f);
        }
        if (u >= 0xD800 && u <= 0xDFFF)
            return INVALID_CHAR;
        if (u == 0xFFFE || u == 0xFFFF)
            return INVALID_CHAR;
        return u;
}

variable_types:
buf
unsigned char *

variable_types: nbytes int

variable_types:
u
unsigned long

variable_types: i int

variable_types: j int

return_type: unsigned long


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n", a1);
puts("Check whether input files are valid UTF-8.");
return printf("This is version %s.\n", "1.1");
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n", a1);
puts("Check whether input files are valid UTF-8.");
return printf("This is version %s.\n", "1.1");
}

original_code:
static void usage(const char *program_name) {
 printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n",
        program_name);
 printf("Check whether input files are valid UTF-8.\n");
 printf("This is version %s.\n", "1.1");
}

canonical_original_code:
static void usage(char *program_name) {
 printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n",
        program_name);
 printf("Check whether input files are valid UTF-8.\n");
 printf("This is version %s.\n", "1.1");
}


repo: gianrigo/rk-ocl

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

clGetPlatformIDs();
result = clGetPlatformIDs();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to create plataform.");
exit(-1);
}
return result;
}

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

clGetPlatformIDs();
result = clGetPlatformIDs();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to create plataform.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_platform(unsigned int num_platforms){
  cl_uint num_platforms_found;
  clGetPlatformIDs( 0, ((void *)0), &num_platforms_found);
  if ( clGetPlatformIDs( num_platforms, &_platform, &num_platforms_found ) != 0 ){
    printf("\nERROR: Failed to create plataform.\n");
    exit(-1);
  }
}

canonical_original_code:
void opencl_create_platform(unsigned int num_platforms){
  unsigned int num_platforms_found;
  clGetPlatformIDs( 0, ((void *)0), &num_platforms_found);
  if ( clGetPlatformIDs( num_platforms, &_platform, &num_platforms_found ) != 0 ){
    printf("\nERROR: Failed to create plataform.\n");
    exit(-1);
  }
}


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

result = clCreateContext();
context = result;
if ( !result )
{
puts("\nERROR: Failed to create context.");
exit(-1);
}
return result;
}

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

result = clCreateContext();
context = result;
if ( !result )
{
puts("\nERROR: Failed to create context.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_context(){
  if( (_context = clCreateContext( 0, 1, _devices, ((void *)0), ((void *)0), ((void *)0) )) == ((void *)0) ){
    printf("\nERROR: Failed to create context.\n");
    exit(-1);
  }
}


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

result = clCreateCommandQueue();
queue = result;
if ( !result )
{
puts("\nERROR: Failed to create queue.");
exit(-1);
}
return result;
}

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

result = clCreateCommandQueue();
queue = result;
if ( !result )
{
puts("\nERROR: Failed to create queue.");
exit(-1);
}
return result;
}

original_code:
void opencl_create_queue(){
  if((_queue = clCreateCommandQueue(_context, _devices[_device_used], (1 << 1), ((void *)0))) == ((void *)0) ){
    printf("\nERROR: Failed to create queue.\n");
    exit(-1);
  }
}


decompiled_code:
__int64 opencl_build_program()
{
__int64 result; // rax
const char *v1; // rbp
__int64 v2; // [rsp+8h] [rbp-20h]

result = clBuildProgram();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to build program.");
clGetProgramBuildInfo();
v1 = (const char *)malloc(v2 + 1);
clGetProgramBuildInfo();
v1[v2] = 0;
printf("BUILD LOG: \n %s\n", v1);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 result; // rax
const char *v1; // rbp
__int64 v2; // [rsp+8h] [rbp-20h]

result = clBuildProgram();
if ( (_DWORD)result )
{
puts("\nERROR: Failed to build program.");
clGetProgramBuildInfo();
v1 = (const char *)malloc(v2 + 1);
clGetProgramBuildInfo();
v1[v2] = 0;
printf("BUILD LOG: \n %s\n", v1);
exit(-1);
}
return result;
}

original_code:
void opencl_build_program(){
  int err;
  char *build_log;
  size_t ret_val_size;
  err = clBuildProgram(_program, 0, ((void *)0), ((void *)0), ((void *)0), ((void *)0));
  if ( err != 0 ) {
    printf("\nERROR: Failed to build program.\n");
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, 0, ((void *)0), &ret_val_size);
    build_log = (char*) malloc((ret_val_size+1)*sizeof(char));
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, ret_val_size, build_log, ((void *)0));
    build_log[ret_val_size] = '\0';
    printf("BUILD LOG: \n %s\n", build_log);
    exit(-1);
  }
}

canonical_original_code:
void opencl_build_program(){
  int err;
  char *build_log;
  unsigned long ret_val_size;
  err = clBuildProgram(_program, 0, ((void *)0), ((void *)0), ((void *)0), ((void *)0));
  if ( err != 0 ) {
    printf("\nERROR: Failed to build program.\n");
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, 0, ((void *)0), &ret_val_size);
    build_log = (char *) malloc((ret_val_size+1)*sizeof(char));
    clGetProgramBuildInfo(_program, _devices[_device_used], 0x1183, ret_val_size, build_log, ((void *)0));
    build_log[ret_val_size] = '\0';
    printf("BUILD LOG: \n %s\n", build_log);
    exit(-1);
  }
}

variable_types: err int


decompiled_code:
__int64 opencl_create_program()
{
void *v0; // rax
__int64 v1; // rdi
int v3[2]; // [rsp+8h] [rbp-20h] BYREF
void *v4; // [rsp+10h] [rbp-18h]
__int64 v5; // [rsp+18h] [rbp-10h]

v0 = load_program_from_source(v3);
v1 = context;
v4 = v0;
v5 = v3[0];
program = clCreateProgramWithSource();
if ( v3[1] )
{
puts("\nERROR: Failed to create program.");
exit(-1);
}
return opencl_build_program(v1, 1LL);
}

canonical_decompiled_code:
__int64 func13()
{
void *v0; // rax
__int64 v1; // rdi
int v3[2]; // [rsp+8h] [rbp-20h] BYREF
void *v4; // [rsp+10h] [rbp-18h]
__int64 v5; // [rsp+18h] [rbp-10h]

v0 = func11(v3);
v1 = context;
v4 = v0;
v5 = v3[0];
program = clCreateProgramWithSource();
if ( v3[1] )
{
puts("\nERROR: Failed to create program.");
exit(-1);
}
return func12(v1, 1LL);
}

original_code:
void opencl_create_program(){
  char* program_source;
  int size;
  size_t prog_size;
  cl_int err;
  program_source = load_program_from_source(&size);
  prog_size = (size_t)size;
  _program = clCreateProgramWithSource(_context, 1, (const char**)&program_source, &prog_size, &err);
  if ( err != 0 ){
    printf("\nERROR: Failed to create program.\n");
    exit(-1);
  }
  opencl_build_program();
}

canonical_original_code:
void opencl_create_program(){
  char *program_source;
  int size;
  unsigned long prog_size;
  int err;
  program_source = load_program_from_source(&size);
  prog_size = (unsigned long)size;
  _program = clCreateProgramWithSource(_context, 1, (char **)&program_source, &prog_size, &err);
  if ( err != 0 ){
    printf("\nERROR: Failed to create program.\n");
    exit(-1);
  }
  opencl_build_program();
}

variable_types: size int


decompiled_code:
__int64 __fastcall opencl_create_kernel(const char *a1)
{
__int64 result; // rax

result = clCreateKernel();
kernel = result;
if ( !result )
{
printf("\nERROR: Failed to create kernel %s.\n", a1);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(const char *a1)
{
__int64 result; // rax

result = clCreateKernel();
kernel = result;
if ( !result )
{
printf("\nERROR: Failed to create kernel %s.\n", a1);
exit(-1);
}
return result;
}

original_code:
void opencl_create_kernel(char* kernel_name){
  _kernel = clCreateKernel(_program, kernel_name, ((void *)0));
  if ( _kernel == ((void *)0) ){
    printf("\nERROR: Failed to create kernel %s.\n",kernel_name);
    exit(-1);
  }
}

canonical_original_code:
void opencl_create_kernel(char *kernel_name){
  _kernel = clCreateKernel(_program, kernel_name, ((void *)0));
  if ( _kernel == ((void *)0) ){
    printf("\nERROR: Failed to create kernel %s.\n",kernel_name);
    exit(-1);
  }
}


decompiled_code:
void __fastcall opencl_run_kernel(unsigned int a1, int a2)
{
float *v2; // rbp
unsigned int *v3; // rbx
unsigned int i; // er12
unsigned int v5; // er14
unsigned int j; // er15
__int64 v7; // rax
unsigned int *ptr; // [rsp+8h] [rbp-50h]

ptr = (unsigned int *)malloc(4LL * a1);
v2 = (float *)malloc(12LL * a2 * a1);
clEnqueueNDRangeKernel();
clReleaseEvent();
clFinish();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"n_points\".");
exit(-1);
}
clReleaseEvent();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"points\".");
exit(-1);
}
clReleaseEvent();
if ( a1 )
{
v3 = ptr;
for ( i = 0; i != a1; ++i )
{
v5 = i;
for ( j = 0; *v3 > j; ++j )
{
v7 = 3LL * v5;
v5 += 7;
printf("%f %f %f\n", v2[v7], v2[v7 + 1], v2[v7 + 2]);
}
++v3;
putchar(10);
}
}
free(ptr);
free(v2);
}

canonical_decompiled_code:
void __fastcall func16(unsigned int a1, int a2)
{
float *v2; // rbp
unsigned int *v3; // rbx
unsigned int i; // er12
unsigned int v5; // er14
unsigned int j; // er15
__int64 v7; // rax
unsigned int *ptr; // [rsp+8h] [rbp-50h]

ptr = (unsigned int *)malloc(4LL * a1);
v2 = (float *)malloc(12LL * a2 * a1);
clEnqueueNDRangeKernel();
clReleaseEvent();
clFinish();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"n_points\".");
exit(-1);
}
clReleaseEvent();
if ( (unsigned int)clEnqueueReadBuffer() == -30 )
{
puts("\nERROR: Failed to read buffer \"points\".");
exit(-1);
}
clReleaseEvent();
if ( a1 )
{
v3 = ptr;
for ( i = 0; i != a1; ++i )
{
v5 = i;
for ( j = 0; *v3 > j; ++j )
{
v7 = 3LL * v5;
v5 += 7;
printf("%f %f %f\n", v2[v7], v2[v7 + 1], v2[v7 + 2]);
}
++v3;
putchar(10);
}
}
free(ptr);
free(v2);
}

original_code:
void opencl_run_kernel(unsigned int count_v0, unsigned int max_points){
  size_t work_dim[1];
  unsigned int i, j, *n_points;
  vector *points;
  n_points = (unsigned int*) malloc(count_v0*sizeof(unsigned int));
  points = (vector*) malloc(count_v0*max_points*sizeof(vector));
  work_dim[0] = count_v0;
  clEnqueueNDRangeKernel(_queue, _kernel, 1, ((void *)0), work_dim, ((void *)0), 0, ((void *)0), &_event);
  clReleaseEvent(_event);
  clFinish(_queue);
  if( clEnqueueReadBuffer(_queue, _opencl_n_points, 1, 0, sizeof(unsigned int)*count_v0, n_points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"n_points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  if( clEnqueueReadBuffer(_queue, _opencl_points, 1, 0, sizeof(vector)*count_v0*max_points, points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  for(i = 0; i < count_v0; i++){
    for(j = 0; j < n_points[i]; j++){
     printf("%f %f %f\n", points[(i+7*j)].x,points[(i+7*j)].y,points[(i+7*j)].z);
    }
    printf("\n");
  }
  free(n_points);
  free(points);
}

canonical_original_code:
void opencl_run_kernel(unsigned int count_v0, unsigned int max_points){
  unsigned long work_dim[1];
  unsigned int i; unsigned int j; unsigned int *n_points;
  struct vec *points;
  n_points = (unsigned int *) malloc(count_v0*sizeof(unsigned int));
  points = (struct vec *) malloc(count_v0*max_points*sizeof(vector));
  work_dim[0] = count_v0;
  clEnqueueNDRangeKernel(_queue, _kernel, 1, ((void *)0), work_dim, ((void *)0), 0, ((void *)0), &_event);
  clReleaseEvent(_event);
  clFinish(_queue);
  if( clEnqueueReadBuffer(_queue, _opencl_n_points, 1, 0, sizeof(unsigned int)*count_v0, n_points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"n_points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  if( clEnqueueReadBuffer(_queue, _opencl_points, 1, 0, sizeof(vector)*count_v0*max_points, points, 0, ((void *)0), &_event) == -30 ){
    printf("\nERROR: Failed to read buffer \"points\".\n");
    exit(-1);
  }
  clReleaseEvent(_event);
  for(i = 0; i < count_v0; i++){
    for(j = 0; j < n_points[i]; j++){
     printf("%f %f %f\n", points[(i+7*j)].x,points[(i+7*j)].y,points[(i+7*j)].z);
    }
    printf("\n");
  }
  free(n_points);
  free(points);
}

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types: struct vec { float x; float y; float z; } *


decompiled_code:
void __fastcall opencl_init(const char *a1, _DWORD a2, signed int a3)
{
__int64 v4; // rax
double v5; // xmm2_8
int v6; // [rsp+1Ch] [rbp-3Ch]

opencl_create_platform(2LL);
opencl_get_devices_id();
opencl_create_context();
opencl_create_queue();
opencl_create_program();
opencl_create_kernel(a1);
clGetDeviceInfo();
v4 = 12LL * a3;
if ( v4 < 0 )
v5 = (double)(int)((unsigned __int64)v4 >> 1) + (double)(int)((unsigned __int64)v4 >> 1);
else
v5 = (double)(int)v4;
prepare_kernel();
opencl_run_kernel(a3, (int)((double)v6 * 0.9 / v5));
}

canonical_decompiled_code:
void __fastcall func17(const char *a1, _DWORD a2, signed int a3)
{
__int64 v4; // rax
double v5; // xmm2_8
int v6; // [rsp+1Ch] [rbp-3Ch]

func7(2LL);
func8();
func9();
func10();
func13();
func14(a1);
clGetDeviceInfo();
v4 = 12LL * a3;
if ( v4 < 0 )
v5 = (double)(int)((unsigned __int64)v4 >> 1) + (double)(int)((unsigned __int64)v4 >> 1);
else
v5 = (double)(int)v4;
func15();
func16(a3, (int)((double)v6 * 0.9 / v5));
}

original_code:
void opencl_init(char* kernel_name, vector *v0, int count_v0, double h, int n_x, int n_y, int n_z, vector_field field ){
  unsigned int max_points, available;
  opencl_create_platform(2);
  opencl_get_devices_id();
  opencl_create_context();
  opencl_create_queue();
  opencl_create_program();
  opencl_create_kernel(kernel_name);
  clGetDeviceInfo(*_devices,0x101F,sizeof(available),&available,((void *)0));
  max_points = (available*0.9)/(sizeof(vector)*count_v0);
  prepare_kernel(v0, count_v0, h, n_x, n_y, n_z, field, max_points);
  opencl_run_kernel(count_v0, max_points);
}

canonical_original_code:
void opencl_init(char *kernel_name, struct vec *v0, int count_v0, double h, int n_x, int n_y, int n_z, struct vec *field){
  unsigned int max_points; unsigned int available;
  opencl_create_platform(2);
  opencl_get_devices_id();
  opencl_create_context();
  opencl_create_queue();
  opencl_create_program();
  opencl_create_kernel(kernel_name);
  clGetDeviceInfo(*_devices,0x101F,sizeof(available),&available,((void *)0));
  max_points = (available*0.9)/(sizeof(vector)*count_v0);
  prepare_kernel(v0, count_v0, h, n_x, n_y, n_z, field, max_points);
  opencl_run_kernel(count_v0, max_points);
}

variable_types: h double

variable_types:
field
struct vec *

variable_types:
available
unsigned int

user_defined_types: struct vec { float x; float y; float z; }


repo: gibsson/linux-adk

decompiled_code:
void signal_handler()
{
puts("SIGINT: Closing accessory");
stop_acc = 1;
}

canonical_decompiled_code:
void func0()
{
puts("SIGINT: Closing accessory");
stop_acc = 1;
}

original_code:
static void signal_handler(int signo)
{
 printf("SIGINT: Closing accessory\n");
 stop_acc = 1;
}

variable_types: signo int


decompiled_code:
int __fastcall show_version(const char *a1)
{
return printf("%s v%s\nreport bugs to %s\n", a1, "0.4", "bisson.gary@gmail.com");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return printf("%s v%s\nreport bugs to %s\n", a1, "0.4", "bisson.gary@gmail.com");
}

original_code:
static void show_version(char *name)
{
 printf("%s v%s\nreport bugs to %s\n", name, "0.4",
        "bisson.gary@gmail.com");
 return;
}

variable_types:
name
char *