decompiled_code:
__int64 operant_close()
{
comedi_close(operant_comedi_device);
return comedi_close(operant_comedi_device2);
}

canonical_decompiled_code:
__int64 func1()
{
comedi_close(operant_comedi_device);
return comedi_close(operant_comedi_device2);
}

original_code:
void operant_close()
{
  comedi_close(operant_comedi_device);
  comedi_close(operant_comedi_device2);
}


decompiled_code:
__int64 __fastcall set_base_io_chans(_DWORD *a1, _DWORD *a2, _DWORD *a3, _DWORD *a4, unsigned int a5)
{
switch ( a5 )
{
case 0u:
case 6u:
*a2 = 0;
*a4 = 8;
*a1 = 0;
*a3 = 0;
break;
case 1u:
case 7u:
*a2 = 16;
*a4 = 0;
*a1 = 0;
*a3 = 1;
break;
case 2u:
case 8u:
*a2 = 8;
*a4 = 16;
*a1 = 1;
*a3 = 1;
break;
case 3u:
case 9u:
*a2 = 0;
*a4 = 8;
*a1 = 2;
*a3 = 2;
break;
case 4u:
case 0xAu:
*a2 = 16;
*a4 = 0;
*a1 = 2;
*a3 = 3;
break;
case 5u:
case 0xBu:
*a2 = 8;
*a4 = 16;
*a1 = 3;
*a3 = 3;
break;
default:
printf("Error!: box %d not valid\n", a5);
*a4 = 0;
*a2 = 0;
*a3 = 0;
*a1 = 0;
break;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, _DWORD *a2, _DWORD *a3, _DWORD *a4, unsigned int a5)
{
switch ( a5 )
{
case 0u:
case 6u:
*a2 = 0;
*a4 = 8;
*a1 = 0;
*a3 = 0;
break;
case 1u:
case 7u:
*a2 = 16;
*a4 = 0;
*a1 = 0;
*a3 = 1;
break;
case 2u:
case 8u:
*a2 = 8;
*a4 = 16;
*a1 = 1;
*a3 = 1;
break;
case 3u:
case 9u:
*a2 = 0;
*a4 = 8;
*a1 = 2;
*a3 = 2;
break;
case 4u:
case 0xAu:
*a2 = 16;
*a4 = 0;
*a1 = 2;
*a3 = 3;
break;
case 5u:
case 0xBu:
*a2 = 8;
*a4 = 16;
*a1 = 3;
*a3 = 3;
break;
default:
printf("Error!: box %d not valid\n", a5);
*a4 = 0;
*a2 = 0;
*a3 = 0;
*a1 = 0;
break;
}
return 0LL;
}

original_code:
int set_base_io_chans(int *indev, int *input, int *outdev, int *output, int boxid)
{
  switch (boxid)
    {
    case 6:
    case 0:
      *input = 0;
      *output = 8;
      *indev = 0;
      *outdev = 0;
      break;
    case 7:
    case 1:
      *input = 16;
      *output = 0;
      *indev = 0;
      *outdev = 1;
      break;
    case 8:
    case 2:
      *input = 8;
      *output = 16;
      *indev = 1;
      *outdev = 1;
      break;
    case 9:
    case 3:
      *input = 0;
      *output = 8;
      *indev = 2;
      *outdev = 2;
      break;
    case 10:
    case 4:
      *input = 16;
      *output = 0;
      *indev = 2;
      *outdev = 3;
      break;
    case 11:
    case 5:
      *input = 8;
      *output = 16;
      *indev = 3;
      *outdev = 3;
      break;
    default:
      printf("Error!: box %d not valid\n", boxid);
      *indev=*outdev=*input=*output=0;
      break;
    }
  return 0;
}

variable_types:
indev
int *

variable_types:
input
int *

variable_types:
outdev
int *

variable_types:
output
int *

variable_types: boxid int

return_type: int


decompiled_code:
int __fastcall operant_write(int a1, __int64 a2, int a3)
{
int result; // eax
int v5; // [rsp+0h] [rbp-28h] BYREF
unsigned int v6; // [rsp+4h] [rbp-24h] BYREF
int v7; // [rsp+8h] [rbp-20h] BYREF
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)a2 > 7 )
return printf("Channel #%d doesn't exist!\n", a2);
set_base_io_chans(v8, &v7, &v6, &v5, a1 - 1);
if ( a1 - 1 > 5 )
{
if ( a3 )
result = comedi_dio_write(operant_comedi_device2, v6, (unsigned int)(v5 + 7 - a2), 0LL);
else
result = comedi_dio_write(operant_comedi_device2, v6, (unsigned int)(v5 + 7 - a2), 1LL);
}
else if ( a3 )
{
result = comedi_dio_write(operant_comedi_device, v6, (unsigned int)(v5 + a2), 0LL);
}
else
{
result = comedi_dio_write(operant_comedi_device, v6, (unsigned int)(v5 + a2), 1LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1, __int64 a2, int a3)
{
int result; // eax
int v5; // [rsp+0h] [rbp-28h] BYREF
unsigned int v6; // [rsp+4h] [rbp-24h] BYREF
int v7; // [rsp+8h] [rbp-20h] BYREF
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)a2 > 7 )
return printf("Channel #%d doesn't exist!\n", a2);
func2(v8, &v7, &v6, &v5, a1 - 1);
if ( a1 - 1 > 5 )
{
if ( a3 )
result = comedi_dio_write(operant_comedi_device2, v6, (unsigned int)(v5 + 7 - a2), 0LL);
else
result = comedi_dio_write(operant_comedi_device2, v6, (unsigned int)(v5 + 7 - a2), 1LL);
}
else if ( a3 )
{
result = comedi_dio_write(operant_comedi_device, v6, (unsigned int)(v5 + a2), 0LL);
}
else
{
result = comedi_dio_write(operant_comedi_device, v6, (unsigned int)(v5 + a2), 1LL);
}
return result;
}

original_code:
void operant_write(int box_id, int channel, int state)
{
  int indev, in, outdev,out;
  if(channel > 7 || channel <0)
      printf("Channel #%d doesn't exist!\n", channel);
  else{
    box_id--;
    set_base_io_chans(&indev, &in, &outdev, &out, box_id);
    if(box_id < 6)
      if (state)
 comedi_dio_write(operant_comedi_device,outdev,(out+channel), 0);
      else
 comedi_dio_write(operant_comedi_device,outdev,(out+channel), 1);
    else
      if (state){
 comedi_dio_write(operant_comedi_device2,outdev,(out+(7-channel)), 0);
      }
      else
 comedi_dio_write(operant_comedi_device2,outdev,(out+(7-channel)), 1);
  }
}

canonical_original_code:
void operant_write(int box_id, int channel, int state)
{
  int indev; int in; int outdev; int out;
  if(channel > 7 || channel <0)
      printf("Channel #%d doesn't exist!\n", channel);
  else{
    box_id--;
    set_base_io_chans(&indev, &in, &outdev, &out, box_id);
    if(box_id < 6)
      if (state)
 comedi_dio_write(operant_comedi_device,outdev,(out+channel), 0);
      else
 comedi_dio_write(operant_comedi_device,outdev,(out+channel), 1);
    else
      if (state){
 comedi_dio_write(operant_comedi_device2,outdev,(out+(7-channel)), 0);
      }
      else
 comedi_dio_write(operant_comedi_device2,outdev,(out+(7-channel)), 1);
  }
}

variable_types: channel int

variable_types: state int

variable_types: indev int

variable_types: in int

variable_types: outdev int

variable_types: out int

return_type: void


decompiled_code:
int __fastcall operant_clear(unsigned int a1)
{
unsigned int i; // ebx
int result; // eax

if ( !operant_initialized )
return printf("Attempted to clear box #%d without initializing\n", a1);
for ( i = 0; i != 8; ++i )
result = operant_write(a1, i, 0);
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1)
{
unsigned int i; // ebx
int result; // eax

if ( !operant_initialized )
return printf("Attempted to clear box #%d without initializing\n", a1);
for ( i = 0; i != 8; ++i )
result = func3(a1, i, 0);
return result;
}

original_code:
void operant_clear(int box_id)
{
  int i;
  if(operant_initialized)
    for(i=0; i<=7; i++)
      operant_write(box_id, i, 0);
  else
    printf("Attempted to clear box #%d without initializing\n", box_id);
}

variable_types: i int


decompiled_code:
__int64 __fastcall operant_read(int a1, __int64 a2)
{
__int64 result; // rax
int v3; // [rsp+0h] [rbp-28h] BYREF
int v4; // [rsp+4h] [rbp-24h] BYREF
int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)a2 <= 7 )
{
set_base_io_chans(v6, &v5, &v4, &v3, a1 - 1);
if ( a1 - 1 > 5 )
comedi_dio_read(operant_comedi_device2, v6[0], (unsigned int)(v5 + 7 - a2), &v3);
else
comedi_dio_read(operant_comedi_device, v6[0], (unsigned int)(v5 + a2), &v3);
result = v3 == 0;
}
else
{
printf("Channel #%d doesn't exist!\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, __int64 a2)
{
__int64 result; // rax
int v3; // [rsp+0h] [rbp-28h] BYREF
int v4; // [rsp+4h] [rbp-24h] BYREF
int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)a2 <= 7 )
{
func2(v6, &v5, &v4, &v3, a1 - 1);
if ( a1 - 1 > 5 )
comedi_dio_read(operant_comedi_device2, v6[0], (unsigned int)(v5 + 7 - a2), &v3);
else
comedi_dio_read(operant_comedi_device, v6[0], (unsigned int)(v5 + a2), &v3);
result = v3 == 0;
}
else
{
printf("Channel #%d doesn't exist!\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int operant_read(int box_id, int channel)
{
  int indev, in, outdev,out;
  box_id--;
  if(channel > 7 || channel <0)
    {
      printf("Channel #%d doesn't exist!\n", channel);
      return -1;
    }
  else{
    set_base_io_chans(&indev, &in, &outdev, &out, box_id);
    if(box_id < 6)
      comedi_dio_read(operant_comedi_device,indev,in+channel, &out );
    else
      comedi_dio_read(operant_comedi_device2,indev,in+(7-channel), &out );
    return !out;
  }
}

canonical_original_code:
int operant_read(int box_id, int channel)
{
  int indev; int in; int outdev; int out;
  box_id--;
  if(channel > 7 || channel <0)
    {
      printf("Channel #%d doesn't exist!\n", channel);
      return -1;
    }
  else{
    set_base_io_chans(&indev, &in, &outdev, &out, box_id);
    if(box_id < 6)
      comedi_dio_read(operant_comedi_device,indev,in+channel, &out );
    else
      comedi_dio_read(operant_comedi_device2,indev,in+(7-channel), &out );
    return !out;
  }
}


repo: geodynamics/hc

decompiled_code:
void __fastcall hc_torsol(__int64 a1, __int64 a2, int a3, unsigned int a4, double *a5, _QWORD *a6, _QWORD *a7, unsigned int *a8, __int64 a9, __int64 a10, __int16 a11)
{
_QWORD *v12; // r15
__int64 v13; // r14
unsigned int v14; // ebp
__int64 v15; // r15
__int64 v16; // r12
__int64 v17; // rbx
double *v18; // r15
double v19; // xmm0_8
int v20; // eax
__int64 v21; // rsi
__int64 v22; // rax
char *v23; // rdx
__int64 v24; // rcx
int v25; // esi
unsigned __int64 v26; // rax
__int64 v28; // r12
int v29; // ebp
__int64 v30; // rbp
int v31; // ebx
__int64 v32; // r12
double v33; // xmm2_8
double v34; // xmm4_8
double v35; // xmm1_8
__int64 v37; // [rsp+0h] [rbp-E8h]
double v38; // [rsp+0h] [rbp-E8h]
double v39; // [rsp+8h] [rbp-E0h]
double v40; // [rsp+10h] [rbp-D8h]
double v41; // [rsp+18h] [rbp-D0h]
__int64 v42; // [rsp+20h] [rbp-C8h]
double v43; // [rsp+28h] [rbp-C0h]
__int16 v44; // [rsp+36h] [rbp-B2h]
double v45; // [rsp+38h] [rbp-B0h]
__int64 v46; // [rsp+40h] [rbp-A8h]
double v47; // [rsp+48h] [rbp-A0h]
double v48; // [rsp+50h] [rbp-98h]
double v49; // [rsp+58h] [rbp-90h]
double *v50; // [rsp+60h] [rbp-88h]
double *v51; // [rsp+68h] [rbp-80h]
int v52; // [rsp+70h] [rbp-78h]
int v53; // [rsp+74h] [rbp-74h]
__int64 v54; // [rsp+78h] [rbp-70h]
__int64 v55; // [rsp+80h] [rbp-68h]
__int64 v56; // [rsp+88h] [rbp-60h]
void *ptr[8]; // [rsp+A8h] [rbp-40h] BYREF

v12 = a6;
v13 = a10;
v14 = a3 + 1;
v53 = a4 + 1;
hc_dvecrealloc(a6, (unsigned int)(a3 + 1), "hc_torsol: rv");
hc_dvecrealloc(a7, v14, "hc_torsol: visc");
*(_QWORD *)(*v12 + 8LL * a3) = LC4;
*(_QWORD *)(*a7 + 8LL * a3) = *(_QWORD *)(*a7 + 8LL * a3 - 8);
if ( a11 )
fprintf(stderr, "hc_torsol: toroidal velocities lmax %i and type %i\n", a8[2], *a8);
v15 = a10 + 8LL * *(_DWORD *)(a1 + 300) * v53;
if ( v53 <= 1 )
{
hc_hvecalloc(ptr, (unsigned int)v53, "hc_torsol: vecnor");
*(_QWORD *)ptr[0] = LC0;
}
else
{
v16 = 8LL * v53 + 8;
v46 = a4 - 1 + 2LL;
v56 = 8LL * v53;
v17 = 1LL;
v42 = a10 + 8LL * *(_DWORD *)(a1 + 300) * v53;
v18 = a5;
do
{
v19 = *v18;
*(_QWORD *)(a10 + 8 * v17) = LC0;
*(_QWORD *)(v42 + 8 * v17) = 0LL;
if ( *(int *)(a1 + 300) > 1 )
{
v41 = (double)(int)v17;
v47 = (double)(int)v17 + 2.0;
v48 = (double)(int)v17 - 1.0;
v49 = 1.0 / ((double)(int)v17 + (double)(int)v17 + 1.0);
v50 = (double *)(a10 + v16);
v51 = (double *)(v16 + v42);
v30 = 1LL;
v40 = *(double *)&LC0;
v39 = 0.0;
v52 = 0;
v54 = v17;
v31 = 1;
v55 = v16;
while ( 1 )
{
v32 = 8LL * v31;
while ( 1 )
{
v38 = *(double *)(*a6 + v32);
if ( v38 <= v18[v30] )
{
v44 = 0;
}
else
{
v38 = v18[v30];
v44 = 1;
}
v45 = log(v38 / v19);
v43 = exp(v41 * v45);
v33 = exp(COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v41 + 1.0) ^ LC7) * v45);
v34 = *(double *)(*a7 + 8LL * v52);
v35 = v47 * v48 * v34 * (v43 - v33) * v40 + (v43 * v48 + v47 * v33) * v39;
v40 = ((v43 * v47 + v33 * v48) * v40 + (v43 - v33) / v34 * v39) * v49;
v39 = v35 * v49;
v32 += 8LL;
if ( v44 )
break;
v52 = v31++;
v19 = v38;
}
*v50 = v40;
*v51 = v39;
++v30;
v50 = (double *)((char *)v50 + v56);
v51 = (double *)((char *)v51 + v56);
if ( *(_DWORD *)(a1 + 300) <= (int)v30 )
break;
v19 = v38;
}
v17 = v54;
v16 = v55;
}
++v17;
v16 += 8LL;
}
while ( v17 != v46 );
v15 = v42;
v13 = a10;
hc_hvecalloc(ptr, (unsigned int)v53, "hc_torsol: vecnor");
v20 = v53 * (*(_DWORD *)(a1 + 300) - 1);
*(_QWORD *)ptr[0] = LC0;
v21 = a10 + 8LL * v20;
v22 = 8LL;
do
{
*(double *)((char *)ptr[0] + v22) = 1.0 / *(double *)(v21 + v22);
v22 += 8LL;
}
while ( v22 != 8 * v46 );
}
if ( *(int *)(a1 + 300) > 0 )
{
v23 = (char *)ptr[0];
v24 = 8LL * a4 + 8;
v25 = 0;
do
{
v26 = 8 * ~(unsigned __int64)a4 + v24;
if ( v53 > 0 )
{
do
{
*(double *)(v13 + v26) = *(double *)(v13 + v26) * *(double *)&v23[v26];
*(double *)(v15 + v26) = *(double *)(v15 + v26) * *(double *)&v23[v26];
v26 += 8LL;
}
while ( v26 != v24 );
}
++v25;
v23 -= 8 * v53;
v24 += 8LL * v53;
}
while ( *(_DWORD *)(a1 + 300) > v25 );
}
free(ptr[0]);
if ( *(int *)(a1 + 300) > 0 )
{
v37 = 8LL * v53;
v28 = v15;
v29 = 0;
do
{
sh_aexp_equals_bexp_coeff(a9, a8);
sh_aexp_equals_bexp_coeff(a9 + 256, a8);
sh_scale_expansion_l_factor(a9, v13);
sh_scale_expansion_l_factor(a9 + 256, v28);
++v29;
a9 += 512LL;
v13 += v37;
v28 += v37;
}
while ( *(_DWORD *)(a1 + 300) > v29 );
}
if ( a11 )
fwrite("hc_torsol: done\n", 1uLL, 0x10uLL, stderr);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, __int64 a2, int a3, unsigned int a4, double *a5, _QWORD *a6, _QWORD *a7, unsigned int *a8, __int64 a9, __int64 a10, __int16 a11)
{
_QWORD *v12; // r15
__int64 v13; // r14
unsigned int v14; // ebp
__int64 v15; // r15
__int64 v16; // r12
__int64 v17; // rbx
double *v18; // r15
double v19; // xmm0_8
int v20; // eax
__int64 v21; // rsi
__int64 v22; // rax
char *v23; // rdx
__int64 v24; // rcx
int v25; // esi
unsigned __int64 v26; // rax
__int64 v28; // r12
int v29; // ebp
__int64 v30; // rbp
int v31; // ebx
__int64 v32; // r12
double v33; // xmm2_8
double v34; // xmm4_8
double v35; // xmm1_8
__int64 v37; // [rsp+0h] [rbp-E8h]
double v38; // [rsp+0h] [rbp-E8h]
double v39; // [rsp+8h] [rbp-E0h]
double v40; // [rsp+10h] [rbp-D8h]
double v41; // [rsp+18h] [rbp-D0h]
__int64 v42; // [rsp+20h] [rbp-C8h]
double v43; // [rsp+28h] [rbp-C0h]
__int16 v44; // [rsp+36h] [rbp-B2h]
double v45; // [rsp+38h] [rbp-B0h]
__int64 v46; // [rsp+40h] [rbp-A8h]
double v47; // [rsp+48h] [rbp-A0h]
double v48; // [rsp+50h] [rbp-98h]
double v49; // [rsp+58h] [rbp-90h]
double *v50; // [rsp+60h] [rbp-88h]
double *v51; // [rsp+68h] [rbp-80h]
int v52; // [rsp+70h] [rbp-78h]
int v53; // [rsp+74h] [rbp-74h]
__int64 v54; // [rsp+78h] [rbp-70h]
__int64 v55; // [rsp+80h] [rbp-68h]
__int64 v56; // [rsp+88h] [rbp-60h]
void *ptr[8]; // [rsp+A8h] [rbp-40h] BYREF

v12 = a6;
v13 = a10;
v14 = a3 + 1;
v53 = a4 + 1;
hc_dvecrealloc(a6, (unsigned int)(a3 + 1), "hc_torsol: rv");
hc_dvecrealloc(a7, v14, "hc_torsol: visc");
*(_QWORD *)(*v12 + 8LL * a3) = LC4;
*(_QWORD *)(*a7 + 8LL * a3) = *(_QWORD *)(*a7 + 8LL * a3 - 8);
if ( a11 )
fprintf(stderr, "hc_torsol: toroidal velocities lmax %i and type %i\n", a8[2], *a8);
v15 = a10 + 8LL * *(_DWORD *)(a1 + 300) * v53;
if ( v53 <= 1 )
{
hc_hvecalloc(ptr, (unsigned int)v53, "hc_torsol: vecnor");
*(_QWORD *)ptr[0] = LC0;
}
else
{
v16 = 8LL * v53 + 8;
v46 = a4 - 1 + 2LL;
v56 = 8LL * v53;
v17 = 1LL;
v42 = a10 + 8LL * *(_DWORD *)(a1 + 300) * v53;
v18 = a5;
do
{
v19 = *v18;
*(_QWORD *)(a10 + 8 * v17) = LC0;
*(_QWORD *)(v42 + 8 * v17) = 0LL;
if ( *(int *)(a1 + 300) > 1 )
{
v41 = (double)(int)v17;
v47 = (double)(int)v17 + 2.0;
v48 = (double)(int)v17 - 1.0;
v49 = 1.0 / ((double)(int)v17 + (double)(int)v17 + 1.0);
v50 = (double *)(a10 + v16);
v51 = (double *)(v16 + v42);
v30 = 1LL;
v40 = *(double *)&LC0;
v39 = 0.0;
v52 = 0;
v54 = v17;
v31 = 1;
v55 = v16;
while ( 1 )
{
v32 = 8LL * v31;
while ( 1 )
{
v38 = *(double *)(*a6 + v32);
if ( v38 <= v18[v30] )
{
v44 = 0;
}
else
{
v38 = v18[v30];
v44 = 1;
}
v45 = log(v38 / v19);
v43 = exp(v41 * v45);
v33 = exp(COERCE_DOUBLE(COERCE_UNSIGNED_INT64(v41 + 1.0) ^ LC7) * v45);
v34 = *(double *)(*a7 + 8LL * v52);
v35 = v47 * v48 * v34 * (v43 - v33) * v40 + (v43 * v48 + v47 * v33) * v39;
v40 = ((v43 * v47 + v33 * v48) * v40 + (v43 - v33) / v34 * v39) * v49;
v39 = v35 * v49;
v32 += 8LL;
if ( v44 )
break;
v52 = v31++;
v19 = v38;
}
*v50 = v40;
*v51 = v39;
++v30;
v50 = (double *)((char *)v50 + v56);
v51 = (double *)((char *)v51 + v56);
if ( *(_DWORD *)(a1 + 300) <= (int)v30 )
break;
v19 = v38;
}
v17 = v54;
v16 = v55;
}
++v17;
v16 += 8LL;
}
while ( v17 != v46 );
v15 = v42;
v13 = a10;
hc_hvecalloc(ptr, (unsigned int)v53, "hc_torsol: vecnor");
v20 = v53 * (*(_DWORD *)(a1 + 300) - 1);
*(_QWORD *)ptr[0] = LC0;
v21 = a10 + 8LL * v20;
v22 = 8LL;
do
{
*(double *)((char *)ptr[0] + v22) = 1.0 / *(double *)(v21 + v22);
v22 += 8LL;
}
while ( v22 != 8 * v46 );
}
if ( *(int *)(a1 + 300) > 0 )
{
v23 = (char *)ptr[0];
v24 = 8LL * a4 + 8;
v25 = 0;
do
{
v26 = 8 * ~(unsigned __int64)a4 + v24;
if ( v53 > 0 )
{
do
{
*(double *)(v13 + v26) = *(double *)(v13 + v26) * *(double *)&v23[v26];
*(double *)(v15 + v26) = *(double *)(v15 + v26) * *(double *)&v23[v26];
v26 += 8LL;
}
while ( v26 != v24 );
}
++v25;
v23 -= 8 * v53;
v24 += 8LL * v53;
}
while ( *(_DWORD *)(a1 + 300) > v25 );
}
free(ptr[0]);
if ( *(int *)(a1 + 300) > 0 )
{
v37 = 8LL * v53;
v28 = v15;
v29 = 0;
do
{
sh_aexp_equals_bexp_coeff(a9, a8);
sh_aexp_equals_bexp_coeff(a9 + 256, a8);
sh_scale_expansion_l_factor(a9, v13);
sh_scale_expansion_l_factor(a9 + 256, v28);
++v29;
a9 += 512LL;
v13 += v37;
v28 += v37;
}
while ( *(_DWORD *)(a1 + 300) > v29 );
}
if ( a11 )
fwrite("hc_torsol: done\n", 1uLL, 0x10uLL, stderr);
}

original_code:
void hc_torsol(struct hcs *hc,
        int nrad,int nvis,int lmax,double *r,
        double **rv,double **visc, struct sh_lms *pvel_tor,
        struct sh_lms *tor_sol,double *tvec,
        unsigned short verbose)
{
  double coef,*vecnor,hold,rlast,rnext,tloc[2],*tvec1,*tvec2;
  double exp_fac[2],p[2][2],diflog,el,elp2,elm1,efdiff;
  int l,jvisp1,jvis,i,j,nvisp1,lmaxp1,os;
  unsigned short qvis;
  nvisp1 = nvis + 1;
  lmaxp1 = lmax+1;
  hc_dvecrealloc(rv,nvisp1,"hc_torsol: rv");
  hc_dvecrealloc(visc,nvisp1,"hc_torsol: visc");
  (*(*rv+(nvis))) = 1.1;
  (*(*visc+(nvis))) = (*(*visc+(nvis-1)));
  if(verbose)
    fprintf(stderr,"hc_torsol: toroidal velocities lmax %i and type %i\n",
     pvel_tor->lmax,pvel_tor->type);
  tvec1 = tvec;
  tvec2 = (tvec + hc->nradp2 * lmaxp1);
  for(l=1;l < lmaxp1;l++){
    el = (double)l;
    elp2 = el + 2.0;
    elm1 = el - 1.0;
    coef = 1.0 / (2.0 * el + 1.0);
    jvisp1 = 1;
    jvis = 0;
    rlast = r[0];
    tloc[0] = 1.0;
    tloc[1] = 0.0;
    os = l;
    tvec1[os] = tloc[0];
    tvec2[os] = tloc[1];
    for(i=1;i < hc->nradp2;i++){
      os += lmaxp1;
      qvis = 0;
      do{
 if((*(*rv+(jvisp1))) > r[i])
   qvis = 1;
 rnext = (*(*rv+(jvisp1)));
 if(qvis)
   rnext = r[i];
 diflog = log(rnext / rlast);
 exp_fac[0] = exp( el * diflog);
 exp_fac[1] = exp(-(el + 1.0) * diflog);
 efdiff = exp_fac[0] - exp_fac[1];
 p[0][0] = elp2 * exp_fac[0] + elm1 * exp_fac[1];
 p[0][1] = efdiff / (*(*visc+(jvis)));
 p[1][0] = elp2 * elm1 * (*(*visc+(jvis))) * efdiff;
 p[1][1] = elm1 * exp_fac[0] + elp2 * exp_fac[1];
 rlast = rnext;
 hold = tloc[0];
  tloc[0] = (p[0][0] * hold + p[0][1] * tloc[1]);
 tloc[1] = (p[1][0] * hold + p[1][1] * tloc[1]);
 tloc[0] *= coef;
 tloc[1] *= coef;
 if(!qvis){
   jvis = jvisp1;
   jvisp1++;
 }
      }while(!qvis);
      tvec1[os] = tloc[0];
      tvec2[os] = tloc[1];
    }
  }
  hc_hvecalloc(&vecnor,lmaxp1,"hc_torsol: vecnor");
  os = (hc->nradp2-1) * lmaxp1;
  vecnor[0] = 1.0;
  for(l=1;l < lmaxp1;l++)
    vecnor[l] = 1.0 / tvec1[os+l];
  for(i=os=0;i < hc->nradp2;i++,os+=lmaxp1)
    for(l=0;l < lmaxp1;l++){
      tvec1[os+l] *= vecnor[l];
      tvec2[os+l] *= vecnor[l];
    }
  free(vecnor);
  for(os=i=j=0;i < hc->nradp2;i++,os+=lmaxp1,j+=2){
    sh_aexp_equals_bexp_coeff((tor_sol+j+0),pvel_tor);
    sh_aexp_equals_bexp_coeff((tor_sol+j+1),pvel_tor);
    sh_scale_expansion_l_factor((tor_sol+j+0),(tvec1+os));
    sh_scale_expansion_l_factor((tor_sol+j+1),(tvec2+os));
  }
  if(verbose)
    fprintf(stderr,"hc_torsol: done\n");
}

canonical_original_code:
void hc_torsol(struct hcs *hc, int nrad, int nvis, int lmax, double *r, double **rv, double **visc, struct sh_lms *pvel_tor, struct sh_lms *tor_sol, double *tvec, unsigned short verbose)
{
  double coef; double *vecnor; double hold; double rlast; double rnext; double tloc[2]; double *tvec1; double *tvec2;
  double exp_fac[2]; double p[2][2]; double diflog; double el; double elp2; double elm1; double efdiff;
  int l; int jvisp1; int jvis; int i; int j; int nvisp1; int lmaxp1; int os;
  unsigned short qvis;
  nvisp1 = nvis + 1;
  lmaxp1 = lmax+1;
  hc_dvecrealloc(rv,nvisp1,"hc_torsol: rv");
  hc_dvecrealloc(visc,nvisp1,"hc_torsol: visc");
  (*(*rv+(nvis))) = 1.1;
  (*(*visc+(nvis))) = (*(*visc+(nvis-1)));
  if(verbose)
    fprintf(stderr,"hc_torsol: toroidal velocities lmax %i and type %i\n",
     pvel_tor->lmax,pvel_tor->type);
  tvec1 = tvec;
  tvec2 = (tvec + hc->nradp2 * lmaxp1);
  for(l=1;l < lmaxp1;l++){
    el = (double)l;
    elp2 = el + 2.0;
    elm1 = el - 1.0;
    coef = 1.0 / (2.0 * el + 1.0);
    jvisp1 = 1;
    jvis = 0;
    rlast = r[0];
    tloc[0] = 1.0;
    tloc[1] = 0.0;
    os = l;
    tvec1[os] = tloc[0];
    tvec2[os] = tloc[1];
    for(i=1;i < hc->nradp2;i++){
      os += lmaxp1;
      qvis = 0;
      do{
 if((*(*rv+(jvisp1))) > r[i])
   qvis = 1;
 rnext = (*(*rv+(jvisp1)));
 if(qvis)
   rnext = r[i];
 diflog = log(rnext / rlast);
 exp_fac[0] = exp( el * diflog);
 exp_fac[1] = exp(-(el + 1.0) * diflog);
 efdiff = exp_fac[0] - exp_fac[1];
 p[0][0] = elp2 * exp_fac[0] + elm1 * exp_fac[1];
 p[0][1] = efdiff / (*(*visc+(jvis)));
 p[1][0] = elp2 * elm1 * (*(*visc+(jvis))) * efdiff;
 p[1][1] = elm1 * exp_fac[0] + elp2 * exp_fac[1];
 rlast = rnext;
 hold = tloc[0];
  tloc[0] = (p[0][0] * hold + p[0][1] * tloc[1]);
 tloc[1] = (p[1][0] * hold + p[1][1] * tloc[1]);
 tloc[0] *= coef;
 tloc[1] *= coef;
 if(!qvis){
   jvis = jvisp1;
   jvisp1++;
 }
      }while(!qvis);
      tvec1[os] = tloc[0];
      tvec2[os] = tloc[1];
    }
  }
  hc_hvecalloc(&vecnor,lmaxp1,"hc_torsol: vecnor");
  os = (hc->nradp2-1) * lmaxp1;
  vecnor[0] = 1.0;
  for(l=1;l < lmaxp1;l++)
    vecnor[l] = 1.0 / tvec1[os+l];
  for(i=os=0;i < hc->nradp2;i++,os+=lmaxp1)
    for(l=0;l < lmaxp1;l++){
      tvec1[os+l] *= vecnor[l];
      tvec2[os+l] *= vecnor[l];
    }
  free(vecnor);
  for(os=i=j=0;i < hc->nradp2;i++,os+=lmaxp1,j+=2){
    sh_aexp_equals_bexp_coeff((tor_sol+j+0),pvel_tor);
    sh_aexp_equals_bexp_coeff((tor_sol+j+1),pvel_tor);
    sh_scale_expansion_l_factor((tor_sol+j+0),(tvec1+os));
    sh_scale_expansion_l_factor((tor_sol+j+1),(tvec2+os));
  }
  if(verbose)
    fprintf(stderr,"hc_torsol: done\n");
}

variable_types: struct hcs { int lmax; int sh_type; int nrad; double *r; double *dvisc; int nvis; double *visc; double *rvisc; int inho; double *dfact; double *rden; double rho_top_kg; struct sh_lms { int type; unsigned short spectral_init; int lmax; int lmaxp1; int lmbig; int lmsmall2; int n_lm; int n_plm; int npoints; unsigned short plm_computed; int old_lmax; double *alm; struct rick_module { double *gauss_z; double *cfac; double *lfac; double *plm_f1; double *sin_theta; double dphi; int nlat; unsigned short initialized; unsigned short was_called; int old_lmax; } rick; } *dens_anom; struct sh_lms *dens_anom_orig; double dens_scale; unsigned short compressible; int npb; double *rpb; double *fpb; unsigned short free_slip; struct pvels { int n; double *t; struct sh_lms *p; } pvel; struct hc_ps { int ncalled; double rho_scale; double alpha; unsigned short rho_init; int solver_kludge_l; unsigned short tor_init; } psp; unsigned short save_solution; struct sh_lms *pol_sol; double *rho; double *rprops; int nprops; unsigned short *qwrite; struct sh_lms *tor_sol; unsigned short initialized; unsigned short orig_danom_saved; double *lfac; int lfac_init; struct prem_model { double crho[-1]; double cvp[-1]; double cvs[-1]; double cvpv[-1]; double cvph[-1]; double cvsv[-1]; double cvsh[-1]; double ceta[-1]; double cqmu[13]; double cqkappa[13]; int n; int np; double rb[13]; double r[13]; double r0; char model_filename[2000]; unsigned short init; } prem[1]; unsigned short prem_init; double timesc; double visnor; double g; double gacc; double re; double avg_den_core; double secyr; double vel_scale; double stress_scale; double r_cmb; double *plm; unsigned short spectral_solution_computed; } *

variable_types: nrad int

variable_types: nvis int

variable_types: lmax int

variable_types:
r
double *

variable_types:
rv
double **

variable_types:
visc
double **

variable_types:
tvec
double *

variable_types:
verbose
unsigned short

variable_types: coef double

variable_types:
vecnor
double *

variable_types: hold double

variable_types: rlast double

variable_types: rnext double

variable_types:
tloc
double [2]

variable_types:
p
double [2][2]

variable_types: diflog double

variable_types: el double

variable_types: efdiff double

variable_types: l int

variable_types: jvis int

variable_types: j int

variable_types: os int

variable_types:
qvis
unsigned short

user_defined_types: struct hcs { int lmax; int sh_type; int nrad; double *r; double *dvisc; int nvis; double *visc; double *rvisc; int inho; double *dfact; double *rden; double rho_top_kg; struct sh_lms *dens_anom; struct sh_lms *dens_anom_orig; double dens_scale; unsigned short compressible; int npb; double *rpb; double *fpb; unsigned short free_slip; struct pvels pvel; struct hc_ps psp; unsigned short save_solution; struct sh_lms *pol_sol; double *rho; double *rprops; int nprops; unsigned short *qwrite; struct sh_lms *tor_sol; unsigned short initialized; unsigned short orig_danom_saved; double *lfac; int lfac_init; struct prem_model prem[1]; unsigned short prem_init; double timesc; double visnor; double g; double gacc; double re; double avg_den_core; double secyr; double vel_scale; double stress_scale; double r_cmb; double *plm; unsigned short spectral_solution_computed; }

user_defined_types: struct sh_lms { int type; unsigned short spectral_init; int lmax; int lmaxp1; int lmbig; int lmsmall2; int n_lm; int n_plm; int npoints; unsigned short plm_computed; int old_lmax; double *alm; struct rick_module rick; }


decompiled_code:
void __fastcall rick_cs2ab(double *a1, int a2)
{
double *v2; // rax

*a1 = (double)a2 * *a1;
if ( a2 > 2 )
{
v2 = a1 + 2;
do
{
*v2 = (double)a2 * 0.5 * *v2;
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 3) + 3] );
}
}

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

*a1 = (double)a2 * *a1;
if ( a2 > 2 )
{
v2 = a1 + 2;
do
{
*v2 = (double)a2 * 0.5 * *v2;
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 3) + 3] );
}
}

original_code:
void rick_cs2ab(double *rdata,int n)
{
  int i;
  double en;
  en = (double)n;
  rdata[0] *= en;
  en/=2.0;
  for(i=2;i<n;i++)
    rdata[i] *= en;
}

canonical_original_code:
void rick_cs2ab(double *rdata, int n)
{
  int i;
  double en;
  en = (double)n;
  rdata[0] *= en;
  en/=2.0;
  for(i=2;i<n;i++)
    rdata[i] *= en;
}

variable_types:
rdata
double *

variable_types: n int

variable_types: i int

variable_types: en double


decompiled_code:
__int64 __fastcall hc_solve(__int64 a1, unsigned __int16 a2, unsigned int a3, __int64 a4, unsigned __int16 a5, unsigned __int16 a6, unsigned __int16 a7, __int16 a8, unsigned __int16 a9, __int64 a10, __int64 a11, __int64 a12, unsigned __int16 a13, unsigned __int16 a14)
{
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // rdx
__int64 v18; // rcx
unsigned __int16 v19; // ax
__int64 result; // rax
unsigned int v22; // [rsp+10h] [rbp-58h]
unsigned int v23; // [rsp+14h] [rbp-54h]
void *ptr; // [rsp+28h] [rbp-40h] BYREF

if ( !*(_WORD *)(a1 + 328) )
{
fprintf(stderr, "%s: error: %s, exiting\n", "hc_solve", "hc structure not initialized");
exit(-1);
}
if ( !a2 )
{
v15 = *(unsigned int *)(a10 + 8);
v16 = *(unsigned int *)(a11 + 8);
if ( (int)v15 < (int)v16 )
{
fprintf(stderr, "hc_solve: error: plate expansion lmax (%i) has to be >= density lmax (%i)\n", v15, v16);
exit(-1);
}
}
v17 = *(unsigned int *)(a4 + 8);
v18 = *(unsigned int *)(a10 + 8);
if ( (int)v17 < (int)v18 )
{
fprintf(stderr, "hc_solve: error: solution lmax (%i) has to be >= plate velocitiy lmax (%i)\n", v17, v18);
exit(-1);
}
v22 = 2 * (3 * *(_DWORD *)(a1 + 8) + 6);
if ( (!*(_WORD *)(a1 + 222) || (v19 = *(_WORD *)(a1 + 224)) == 0)
&& ((sh_allocate_and_init(a1 + 232, v22, *(unsigned int *)(a11 + 8), *(unsigned int *)(a1 + 4), 0LL, a13, 0LL),
(v19 = *(_WORD *)(a1 + 224)) == 0)
|| !*(_WORD *)(a1 + 222))
|| a7 | a5
|| !a2 && a6 )
{
hc_polsol(
a1,
*(unsigned int *)(a1 + 8),
*(_QWORD *)(a1 + 16),
*(unsigned int *)(a1 + 56),
*(_QWORD *)(a1 + 64),
a7,
a11,
*(unsigned __int16 *)(a1 + 112),
*(unsigned int *)(a1 + 116),
*(_QWORD *)(a1 + 120),
*(_QWORD *)(a1 + 128),
a2,
a10,
*(_QWORD *)(a1 + 232),
a9,
a12,
v19,
a13,
a14);
if ( a8 )
hc_print_poloidal_solution(*(_QWORD *)(a1 + 232), a1, 31LL, "psol.dat", 1LL, a13);
}
v23 = 0;
if ( !a2 )
{
if ( *(_WORD *)(a1 + 220) && *(_WORD *)(a1 + 224) )
{
v23 = 0;
}
else
{
v23 = 2 * *(_DWORD *)(a1 + 8) + 4;
sh_allocate_and_init(a1 + 320, v23, *(unsigned int *)(a10 + 264), *(unsigned int *)(a1 + 4), 0LL, a13, 0LL);
if ( !*(_WORD *)(a1 + 220) )
{
LABEL_15:
hc_vecalloc(&ptr, (unsigned int)(2 * (*(_DWORD *)(a10 + 264) + 1) * (*(_DWORD *)(a1 + 8) + 2)), "hc_solve");
hc_torsol(
a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 32),
*(unsigned int *)(a10 + 264),
*(_QWORD *)(a1 + 16),
a1 + 48,
a1 + 40,
a10 + 256,
*(_QWORD *)(a1 + 320),
ptr,
a13);
if ( a8 )
hc_print_toroidal_solution(
ptr,
*(unsigned int *)(a10 + 264),
a1,
*(unsigned int *)(a10 + 264),
"tsol.dat",
a13);
free(ptr);
goto LABEL_18;
}
}
if ( !(a6 | a7) && *(_WORD *)(a1 + 224) )
goto LABEL_18;
goto LABEL_15;
}
LABEL_18:
if ( a3 == 1 )
{
if ( a13 )
fwrite("hc_solve: computing solution for radial tractions\n", 1uLL, 0x32uLL, stderr);
}
else if ( a3 == 2 )
{
if ( a13 )
fwrite("hc_solve: computing solution for horizontal tractions\n", 1uLL, 0x36uLL, stderr);
}
else
{
if ( a3 )
{
fprintf(stderr, "hc_solve: error: solution mode %i undefined\n", a3);
exit(-1);
}
if ( a13 )
fwrite("hc_solve: computing solution for velocities\n", 1uLL, 0x2CuLL, stderr);
}
result = hc_sum(a1, *(unsigned int *)(a1 + 8), *(_QWORD *)(a1 + 232), *(_QWORD *)(a1 + 320), a3, a2, a4);
if ( !*(_WORD *)(a1 + 224) )
{
result = sh_free_expansion(*(_QWORD *)(a1 + 232), v22);
if ( !a2 )
result = sh_free_expansion(*(_QWORD *)(a1 + 320), v23);
}
*(_WORD *)(a1 + 222) = 1;
*(_WORD *)(a1 + 220) = 1;
*(_WORD *)(a1 + 6240) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int16 a2, unsigned int a3, __int64 a4, unsigned __int16 a5, unsigned __int16 a6, unsigned __int16 a7, __int16 a8, unsigned __int16 a9, __int64 a10, __int64 a11, __int64 a12, unsigned __int16 a13, unsigned __int16 a14)
{
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // rdx
__int64 v18; // rcx
unsigned __int16 v19; // ax
__int64 result; // rax
unsigned int v22; // [rsp+10h] [rbp-58h]
unsigned int v23; // [rsp+14h] [rbp-54h]
void *ptr; // [rsp+28h] [rbp-40h] BYREF

if ( !*(_WORD *)(a1 + 328) )
{
fprintf(stderr, "%s: error: %s, exiting\n", "hc_solve", "hc structure not initialized");
exit(-1);
}
if ( !a2 )
{
v15 = *(unsigned int *)(a10 + 8);
v16 = *(unsigned int *)(a11 + 8);
if ( (int)v15 < (int)v16 )
{
fprintf(stderr, "hc_solve: error: plate expansion lmax (%i) has to be >= density lmax (%i)\n", v15, v16);
exit(-1);
}
}
v17 = *(unsigned int *)(a4 + 8);
v18 = *(unsigned int *)(a10 + 8);
if ( (int)v17 < (int)v18 )
{
fprintf(stderr, "hc_solve: error: solution lmax (%i) has to be >= plate velocitiy lmax (%i)\n", v17, v18);
exit(-1);
}
v22 = 2 * (3 * *(_DWORD *)(a1 + 8) + 6);
if ( (!*(_WORD *)(a1 + 222) || (v19 = *(_WORD *)(a1 + 224)) == 0)
&& ((sh_allocate_and_init(a1 + 232, v22, *(unsigned int *)(a11 + 8), *(unsigned int *)(a1 + 4), 0LL, a13, 0LL),
(v19 = *(_WORD *)(a1 + 224)) == 0)
|| !*(_WORD *)(a1 + 222))
|| a7 | a5
|| !a2 && a6 )
{
hc_polsol(
a1,
*(unsigned int *)(a1 + 8),
*(_QWORD *)(a1 + 16),
*(unsigned int *)(a1 + 56),
*(_QWORD *)(a1 + 64),
a7,
a11,
*(unsigned __int16 *)(a1 + 112),
*(unsigned int *)(a1 + 116),
*(_QWORD *)(a1 + 120),
*(_QWORD *)(a1 + 128),
a2,
a10,
*(_QWORD *)(a1 + 232),
a9,
a12,
v19,
a13,
a14);
if ( a8 )
hc_print_poloidal_solution(*(_QWORD *)(a1 + 232), a1, 31LL, "psol.dat", 1LL, a13);
}
v23 = 0;
if ( !a2 )
{
if ( *(_WORD *)(a1 + 220) && *(_WORD *)(a1 + 224) )
{
v23 = 0;
}
else
{
v23 = 2 * *(_DWORD *)(a1 + 8) + 4;
sh_allocate_and_init(a1 + 320, v23, *(unsigned int *)(a10 + 264), *(unsigned int *)(a1 + 4), 0LL, a13, 0LL);
if ( !*(_WORD *)(a1 + 220) )
{
LABEL_15:
hc_vecalloc(&ptr, (unsigned int)(2 * (*(_DWORD *)(a10 + 264) + 1) * (*(_DWORD *)(a1 + 8) + 2)), "hc_solve");
hc_torsol(
a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 32),
*(unsigned int *)(a10 + 264),
*(_QWORD *)(a1 + 16),
a1 + 48,
a1 + 40,
a10 + 256,
*(_QWORD *)(a1 + 320),
ptr,
a13);
if ( a8 )
hc_print_toroidal_solution(
ptr,
*(unsigned int *)(a10 + 264),
a1,
*(unsigned int *)(a10 + 264),
"tsol.dat",
a13);
free(ptr);
goto LABEL_18;
}
}
if ( !(a6 | a7) && *(_WORD *)(a1 + 224) )
goto LABEL_18;
goto LABEL_15;
}
LABEL_18:
if ( a3 == 1 )
{
if ( a13 )
fwrite("hc_solve: computing solution for radial tractions\n", 1uLL, 0x32uLL, stderr);
}
else if ( a3 == 2 )
{
if ( a13 )
fwrite("hc_solve: computing solution for horizontal tractions\n", 1uLL, 0x36uLL, stderr);
}
else
{
if ( a3 )
{
fprintf(stderr, "hc_solve: error: solution mode %i undefined\n", a3);
exit(-1);
}
if ( a13 )
fwrite("hc_solve: computing solution for velocities\n", 1uLL, 0x2CuLL, stderr);
}
result = func0(a1, *(unsigned int *)(a1 + 8), *(_QWORD *)(a1 + 232), *(_QWORD *)(a1 + 320), a3, a2, a4);
if ( !*(_WORD *)(a1 + 224) )
{
result = sh_free_expansion(*(_QWORD *)(a1 + 232), v22);
if ( !a2 )
result = sh_free_expansion(*(_QWORD *)(a1 + 320), v23);
}
*(_WORD *)(a1 + 222) = 1;
*(_WORD *)(a1 + 220) = 1;
*(_WORD *)(a1 + 6240) = 1;
return result;
}

original_code:
void hc_solve(struct hcs *hc, unsigned short free_slip,
       int solve_mode,
       struct sh_lms *sol,
       unsigned short dens_anom_changed,
       unsigned short plate_vel_changed,
       unsigned short viscosity_or_layer_changed,
       unsigned short print_pt_sol,
       unsigned short compute_geoid,
       struct sh_lms *pvel,
       struct sh_lms *dens_anom,
       struct sh_lms *geoid,
       unsigned short verbose,
       unsigned short print_kernel_only)
{
  int nsh_pol,nsh_tor=0;
  static unsigned short convert_to_dt = 1;
  double *tvec;
  if(!hc->initialized)
    {fprintf(stderr,"%s: error: %s, exiting\n","hc_solve","hc structure not initialized");exit(-1);};
  if((!free_slip) && (pvel[0].lmax < dens_anom[0].lmax)){
    fprintf(stderr,"hc_solve: error: plate expansion lmax (%i) has to be >= density lmax (%i)\n",
     pvel[0].lmax,dens_anom[0].lmax);
    exit(-1);
  }
  if(sol[0].lmax < pvel[0].lmax){
    fprintf(stderr,"hc_solve: error: solution lmax (%i) has to be >= plate velocitiy lmax (%i)\n",
     sol[0].lmax,pvel[0].lmax);
    exit(-1);
  }
  nsh_pol = 6 * (hc->nrad+2);
  if((!hc->psp.pol_init)||(!hc->save_solution)){
    sh_allocate_and_init(&hc->pol_sol,nsh_pol,
    dens_anom[0].lmax,hc->sh_type,
    0,verbose,0);
  }
  if((!hc->save_solution) || (!hc->psp.pol_init) || viscosity_or_layer_changed ||
     dens_anom_changed || ((!free_slip) && (plate_vel_changed))){
    hc_polsol(hc,hc->nrad,hc->r,hc->inho,hc->dfact,
       viscosity_or_layer_changed,
       dens_anom,hc->compressible,
       hc->npb,hc->rpb,hc->fpb,free_slip,
       (pvel+0),hc->pol_sol,
       compute_geoid,geoid,hc->save_solution,
       verbose,print_kernel_only);
    if(print_pt_sol)
      hc_print_poloidal_solution(hc->pol_sol,hc,31,
     "psol.dat",convert_to_dt,verbose);
  }
  if(!free_slip){
    if((!hc->psp.tor_init)||(!hc->save_solution)){
      nsh_tor = 2 * (hc->nrad+2);
      sh_allocate_and_init(&hc->tor_sol,nsh_tor,pvel[1].lmax,
      hc->sh_type,0,verbose,0);
    }
    if((!hc->psp.tor_init) || viscosity_or_layer_changed || plate_vel_changed ||
       (!hc->save_solution)){
      hc_vecalloc(&tvec,(hc->nrad+2)*(pvel[1].lmax+1)*2,
    "hc_solve");
      hc_torsol(hc,hc->nrad,hc->nvis,pvel[1].lmax,hc->r,
  &hc->rvisc,&hc->visc,(pvel+1),hc->tor_sol,tvec,
  verbose);
      if(print_pt_sol)
 hc_print_toroidal_solution(tvec,pvel[1].lmax,
       hc,pvel[1].lmax,"tsol.dat",
       verbose);
      free(tvec);
    }
  }else{
    nsh_tor = 0;
  }
  switch(solve_mode){
  case 0:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for velocities\n");
    break;
  case 1:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for radial tractions\n");
    break;
  case 2:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for horizontal tractions\n");
    break;
  default:
    fprintf(stderr,"hc_solve: error: solution mode %i undefined\n",
     solve_mode);
    exit(-1);
    break;
  }
  hc_sum(hc,hc->nrad,hc->pol_sol,hc->tor_sol,solve_mode,free_slip,sol,
  verbose);
  if(!hc->save_solution){
    sh_free_expansion(hc->pol_sol,nsh_pol);
    if(!free_slip)
      sh_free_expansion(hc->tor_sol,nsh_tor);
  }
  hc->psp.pol_init = 1;
  hc->psp.tor_init = 1;
  hc->spectral_solution_computed = 1;
}

canonical_original_code:
void hc_solve(struct hcs *hc, unsigned short free_slip, int solve_mode, struct sh_lms *sol, unsigned short dens_anom_changed, unsigned short plate_vel_changed, unsigned short viscosity_or_layer_changed, unsigned short print_pt_sol, unsigned short compute_geoid, struct sh_lms *pvel, struct sh_lms *dens_anom, struct sh_lms *geoid, unsigned short verbose, unsigned short print_kernel_only)
{
  int nsh_pol; int nsh_tor = 0;
  static unsigned short convert_to_dt = 1;
  double *tvec;
  if(!hc->initialized)
    {fprintf(stderr,"%s: error: %s, exiting\n","hc_solve","hc structure not initialized");exit(-1);};
  if((!free_slip) && (pvel[0].lmax < dens_anom[0].lmax)){
    fprintf(stderr,"hc_solve: error: plate expansion lmax (%i) has to be >= density lmax (%i)\n",
     pvel[0].lmax,dens_anom[0].lmax);
    exit(-1);
  }
  if(sol[0].lmax < pvel[0].lmax){
    fprintf(stderr,"hc_solve: error: solution lmax (%i) has to be >= plate velocitiy lmax (%i)\n",
     sol[0].lmax,pvel[0].lmax);
    exit(-1);
  }
  nsh_pol = 6 * (hc->nrad+2);
  if((!hc->psp.pol_init)||(!hc->save_solution)){
    sh_allocate_and_init(&hc->pol_sol,nsh_pol,
    dens_anom[0].lmax,hc->sh_type,
    0,verbose,0);
  }
  if((!hc->save_solution) || (!hc->psp.pol_init) || viscosity_or_layer_changed ||
     dens_anom_changed || ((!free_slip) && (plate_vel_changed))){
    hc_polsol(hc,hc->nrad,hc->r,hc->inho,hc->dfact,
       viscosity_or_layer_changed,
       dens_anom,hc->compressible,
       hc->npb,hc->rpb,hc->fpb,free_slip,
       (pvel+0),hc->pol_sol,
       compute_geoid,geoid,hc->save_solution,
       verbose,print_kernel_only);
    if(print_pt_sol)
      hc_print_poloidal_solution(hc->pol_sol,hc,31,
     "psol.dat",convert_to_dt,verbose);
  }
  if(!free_slip){
    if((!hc->psp.tor_init)||(!hc->save_solution)){
      nsh_tor = 2 * (hc->nrad+2);
      sh_allocate_and_init(&hc->tor_sol,nsh_tor,pvel[1].lmax,
      hc->sh_type,0,verbose,0);
    }
    if((!hc->psp.tor_init) || viscosity_or_layer_changed || plate_vel_changed ||
       (!hc->save_solution)){
      hc_vecalloc(&tvec,(hc->nrad+2)*(pvel[1].lmax+1)*2,
    "hc_solve");
      hc_torsol(hc,hc->nrad,hc->nvis,pvel[1].lmax,hc->r,
  &hc->rvisc,&hc->visc,(pvel+1),hc->tor_sol,tvec,
  verbose);
      if(print_pt_sol)
 hc_print_toroidal_solution(tvec,pvel[1].lmax,
       hc,pvel[1].lmax,"tsol.dat",
       verbose);
      free(tvec);
    }
  }else{
    nsh_tor = 0;
  }
  switch(solve_mode){
  case 0:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for velocities\n");
    break;
  case 1:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for radial tractions\n");
    break;
  case 2:
    if(verbose)
      fprintf(stderr,"hc_solve: computing solution for horizontal tractions\n");
    break;
  default:
    fprintf(stderr,"hc_solve: error: solution mode %i undefined\n",
     solve_mode);
    exit(-1);
    break;
  }
  hc_sum(hc,hc->nrad,hc->pol_sol,hc->tor_sol,solve_mode,free_slip,sol,
  verbose);
  if(!hc->save_solution){
    sh_free_expansion(hc->pol_sol,nsh_pol);
    if(!free_slip)
      sh_free_expansion(hc->tor_sol,nsh_tor);
  }
  hc->psp.pol_init = 1;
  hc->psp.tor_init = 1;
  hc->spectral_solution_computed = 1;
}

variable_types: struct hcs { int lmax; int sh_type; int nrad; double *r; double *dvisc; int nvis; double *visc; double *rvisc; int inho; double *dfact; double *rden; double rho_top_kg; struct sh_lms { int type; unsigned short spectral_init; int lmax; int lmaxp1; int lmbig; int lmsmall2; int n_lm; int n_plm; int npoints; unsigned short plm_computed; int old_lmax; double *alm; struct rick_module { double *gauss_z; double *cfac; double *lfac; double *plm_f1; double *sin_theta; double dphi; int nlat; unsigned short initialized; unsigned short was_called; int old_lmax; } rick; } *dens_anom; struct sh_lms *dens_anom_orig; double dens_scale; unsigned short compressible; int npb; double *rpb; double *fpb; unsigned short free_slip; struct pvels { int n; double *t; struct sh_lms *p; } pvel; struct hc_ps { int ncalled; double rho_scale; double alpha; unsigned short rho_init; int solver_kludge_l; unsigned short tor_init; } psp; unsigned short save_solution; struct sh_lms *pol_sol; double *rho; double *rprops; int nprops; unsigned short *qwrite; struct sh_lms *tor_sol; unsigned short initialized; unsigned short orig_danom_saved; double *lfac; int lfac_init; struct prem_model { double crho[-1]; double cvp[-1]; double cvs[-1]; double cvpv[-1]; double cvph[-1]; double cvsv[-1]; double cvsh[-1]; double ceta[-1]; double cqmu[13]; double cqkappa[13]; int n; int np; double rb[13]; double r[13]; double r0; char model_filename[2000]; unsigned short init; } prem[1]; unsigned short prem_init; double timesc; double visnor; double g; double gacc; double re; double avg_den_core; double secyr; double vel_scale; double stress_scale; double r_cmb; double *plm; unsigned short spectral_solution_computed; } *

variable_types:
sol
struct sh_lms *

variable_types:
pvel
struct sh_lms *

variable_types:
geoid
struct sh_lms *

variable_types:
verbose
unsigned short

variable_types:
tvec
double *


original_code:
void hc_ludcmp_3x3(double a[3][3],int n,int *indx)
{
  int i,imax=0,j,k;
  double big,dum,sum,temp;
  double vv[3];
  for (i=0;i < n;i++) {
    big=0.0;
    for (j=0;j < n;j++)
      if ((temp = fabs(a[i][j])) > big)
 big=temp;
    if (fabs(big) < 3e-16) {
      fprintf(stderr,"hc_ludcmp_3x3: singular matrix in routine, big: %g\n",
       (double)big);
      for(j=0;j <n;j++){
 for(k=0;k<n;k++)
   fprintf(stderr,"%g ",(double)a[j][k]);
 fprintf(stderr,"\n");
      }
      exit(-1);
    }
    vv[i]=1.0/big;
  }
  for (j=0;j < n;j++) {
    for (i=0;i < j;i++) {
      sum = a[i][j];
      for (k=0;k < i;k++)
 sum -= a[i][k] * a[k][j];
      a[i][j]=sum;
    }
    big=0.0;
    for (i=j;i < n;i++) {
      sum=a[i][j];
      for (k=0;k < j;k++)
 sum -= a[i][k] * a[k][j];
      a[i][j]=sum;
      if ( (dum = vv[i]*fabs(sum)) >= big) {
 big=dum;
 imax=i;
      }
    }
    if (j != imax) {
      for (k=0;k < n;k++) {
 dum = a[imax][k];
 a[imax][k]=a[j][k];
 a[j][k]=dum;
      }
      vv[imax]=vv[j];
    }
    indx[j]=imax;
    if (fabs(a[j][j]) < 3e-16)
      a[j][j] = 1.0e-20;;
    if (j != 2) {
      dum=1.0/(a[j][j]);
      for (i=j+1;i < n;i++)
 a[i][j] *= dum;
    }
  }
}

canonical_original_code:
void hc_ludcmp_3x3(double a[3][3], int n, int *indx)
{
  int i; int imax = 0; int j; int k;
  double big; double dum; double sum; double temp;
  double vv[3];
  for (i=0;i < n;i++) {
    big=0.0;
    for (j=0;j < n;j++)
      if ((temp = fabs(a[i][j])) > big)
 big=temp;
    if (fabs(big) < 3e-16) {
      fprintf(stderr,"hc_ludcmp_3x3: singular matrix in routine, big: %g\n",
       (double)big);
      for(j=0;j <n;j++){
 for(k=0;k<n;k++)
   fprintf(stderr,"%g ",(double)a[j][k]);
 fprintf(stderr,"\n");
      }
      exit(-1);
    }
    vv[i]=1.0/big;
  }
  for (j=0;j < n;j++) {
    for (i=0;i < j;i++) {
      sum = a[i][j];
      for (k=0;k < i;k++)
 sum -= a[i][k] * a[k][j];
      a[i][j]=sum;
    }
    big=0.0;
    for (i=j;i < n;i++) {
      sum=a[i][j];
      for (k=0;k < j;k++)
 sum -= a[i][k] * a[k][j];
      a[i][j]=sum;
      if ( (dum = vv[i]*fabs(sum)) >= big) {
 big=dum;
 imax=i;
      }
    }
    if (j != imax) {
      for (k=0;k < n;k++) {
 dum = a[imax][k];
 a[imax][k]=a[j][k];
 a[j][k]=dum;
      }
      vv[imax]=vv[j];
    }
    indx[j]=imax;
    if (fabs(a[j][j]) < 3e-16)
      a[j][j] = 1.0e-20;;
    if (j != 2) {
      dum=1.0/(a[j][j]);
      for (i=j+1;i < n;i++)
 a[i][j] *= dum;
    }
  }
}

variable_types:
a
double [3][3]

variable_types:
indx
int *

variable_types: imax int

variable_types: k int

variable_types: big double

variable_types: dum double

variable_types: sum double

variable_types: temp double

variable_types:
vv
double [3]


variable_types: l int

variable_types: visc double

variable_types:
p
double *

variable_types:
f
double [4]

variable_types: r double

variable_types: rml double

variable_types: rs double

variable_types: lpp long

variable_types: lmm long

variable_types: i long

variable_types: j long

variable_types: k long


checksum

decompiled_code:
// attributes: thunk
void __noreturn checksum()
{
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func6()
{
while ( 1 )
;
}

original_code:
int checksum(int *data)
{
  unsigned i;
  int sum = 0;
  for(i=7; i>=0; i--)
    sum += *data++;
  return sum;
}

canonical_original_code:
int checksum(int *data)
{
  unsigned int i;
  int sum = 0;
  for(i=7; i>=0; i--)
    sum += *data++;
  return sum;
}

variable_types:
data
int *

variable_types:
i
unsigned int

variable_types: sum int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // [rsp+0h] [rbp-20h] BYREF

checksum(&v3, argv, envp);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // [rsp+0h] [rbp-20h] BYREF

func6(&v3, argv, envp);
}

original_code:
int main(void)
{
  int result;
  int x[8] = {2, 5, 6, 9, 17, 28, 13, 27};
  result = checksum(x);
  printf("The checksum of x is now %d\n",result);
  return 0;
}

canonical_original_code:
int main()
{
  int result;
  int x[8] = {2, 5, 6, 9, 17, 28, 13, 27};
  result = checksum(x);
  printf("The checksum of x is now %d\n",result);
  return 0;
}

variable_types: result int

variable_types:
x
int [8]


saturate

decompiled_code:
__int64 __fastcall saturate(signed int a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = a2;
if ( a1 >= (int)a2 )
{
result = a3;
if ( a1 <= (int)a3 )
result = (unsigned int)a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(signed int a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = a2;
if ( a1 >= (int)a2 )
{
result = a3;
if ( a1 <= (int)a3 )
result = (unsigned int)a1;
}
return result;
}

original_code:
int saturate(int val, int amin, int amax)
{
  if (val < amin)
    return amin;
  else if(val > amax)
    return amax;
  else
    return val;
}

variable_types: val int

variable_types: amin int

variable_types: amax int


main

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

printf("The value of b is %d\n", 10LL);
v3 = saturate(10, 0xAu, 0xAu);
printf("The value of b is now %d\n", v3);
return 0;
}

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

printf("The value of b is %d\n", 10LL);
v3 = func7(10, 0xAu, 0xAu);
printf("The value of b is now %d\n", v3);
return 0;
}

original_code:
int main(void)
{
  int a, b;
  int *x;
  a = 8;
  b = 9;
  x = &a;
  b = *x + 2;
  printf("The value of b is %d\n",b);
  a = saturate(b,10,10);
  printf("The value of b is now %d\n",a);
  return 0;
}

canonical_original_code:
int main()
{
  int a; int b;
  int *x;
  a = 8;
  b = 9;
  x = &a;
  b = *x + 2;
  printf("The value of b is %d\n",b);
  a = saturate(b,10,10);
  printf("The value of b is now %d\n",a);
  return 0;
}

variable_types: a int

variable_types: b int

variable_types:
x
int *


doSomeThing

decompiled_code:
void *__fastcall doSomeThing(void *a1)
{
__int64 v1; // rax

lock_mutex(&mutexlock);
printf("\n Job %d started\n", (unsigned int)++counter);
v1 = 0xFFFFFFFFLL;
do
--v1;
while ( v1 );
printf("\n Job %d finished\n", (unsigned int)counter);
unlock_mutex(&mutexlock);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
__int64 v1; // rax

lock_mutex(&mutexlock);
printf("\n Job %d started\n", (unsigned int)++counter);
v1 = 0xFFFFFFFFLL;
do
--v1;
while ( v1 );
printf("\n Job %d finished\n", (unsigned int)counter);
unlock_mutex(&mutexlock);
return 0LL;
}

original_code:
void* doSomeThing(void *arg)
{
    lock_mutex(&mutexlock);
    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);
    for(i=0; i<(0xFFFFFFFF);i++);
    printf("\n Job %d finished\n", counter);
    unlock_mutex(&mutexlock);
    return ((void *)0);
}

canonical_original_code:
void *doSomeThing(void *arg)
{
    lock_mutex(&mutexlock);
    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);
    for(i=0; i<(0xFFFFFFFF);i++);
    printf("\n Job %d finished\n", counter);
    unlock_mutex(&mutexlock);
    return ((void *)0);
}

variable_types:
arg
void *

variable_types:
i
unsigned long

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
char *v6; // rax
char *v7; // rax

v3 = pthread_create(&tid, 0LL, doSomeThing, 0LL);
if ( v3 )
{
v6 = strerror(v3);
printf("\ncan't create thread :[%s]", v6);
}
v4 = pthread_create((pthread_t *)(unsigned int)((_DWORD)&tid + 8), 0LL, doSomeThing, 0LL);
if ( v4 )
{
v7 = strerror(v4);
printf("\ncan't create thread :[%s]", v7);
}
pthread_join(tid, 0LL);
pthread_join(*(&tid + 1), 0LL);
return 0;
}

original_code:
int main(void)
{
    int i = 0;
    int err;
    while(i < 2)
    {
        err = pthread_create(&(tid[i]), ((void *)0), &doSomeThing, ((void *)0));
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }
    pthread_join(tid[0], ((void *)0));
    pthread_join(tid[1], ((void *)0));
    return 0;
}

canonical_original_code:
int main()
{
    int i = 0;
    int err;
    while(i < 2)
    {
        err = pthread_create(&(tid[i]), ((void *)0), &doSomeThing, ((void *)0));
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }
    pthread_join(tid[0], ((void *)0));
    pthread_join(tid[1], ((void *)0));
    return 0;
}

variable_types: i int

variable_types: err int


dividebytwo

decompiled_code:
__int64 __fastcall dividebytwo(int a1)
{
return (unsigned int)(a1 / 2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return (unsigned int)(a1 / 2);
}

original_code:
int dividebytwo(int a)
{
  return a/2;
}

canonical_original_code:
int dividebytwo(int a)
{
  return a/2;
}


main

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

v3 = dividebytwo(-6);
v4 = dividebytwo(8);
printf("a/2 / b/2 is %d\n", (unsigned int)(v3 / v4));
return 0;
}

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

v3 = func7(-6);
v4 = func7(8);
printf("a/2 / b/2 is %d\n", (unsigned int)(v3 / v4));
return 0;
}

original_code:
int main(void)
{
  int a, b, d;
  a = -6;
  b = 8;
  d = dividebytwo(a) / dividebytwo(b);
  printf("a/2 / b/2 is %d\n", d);
  return 0;
}

canonical_original_code:
int main()
{
  int a; int b; int d;
  a = -6;
  b = 8;
  d = dividebytwo(a) / dividebytwo(b);
  printf("a/2 / b/2 is %d\n", d);
  return 0;
}

variable_types: d int


multiply

decompiled_code:
__int64 __fastcall multiply(int a1, int a2)
{
return (unsigned int)(a2 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return (unsigned int)(a2 * a1);
}

original_code:
int multiply(int a, int b)
{
  return (a*b);
}


main

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

v3 = multiply(221412523, 3);
printf("a * b is %d\n", v3);
return 0;
}

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

v3 = func7(221412523, 3);
printf("a * b is %d\n", v3);
return 0;
}

original_code:
int main(void)
{
  int a, b, d;
  a = 221412523;
  b = 3;
  d = multiply(a,b);
  printf("a * b is %d\n", d);
  return 0;
}

canonical_original_code:
int main()
{
  int a; int b; int d;
  a = 221412523;
  b = 3;
  d = multiply(a,b);
  printf("a * b is %d\n", d);
  return 0;
}


DefineHandler

repo: geoffreymbrown/Tiva-Template

decompiled_code:
int __fastcall DefineHandler(const char *a1)
{
return fprintf(_bss_start, "void %s (void) __attribute__ ((weak, alias (\"default_handler\")));\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(_bss_start, "void %s (void) __attribute__ ((weak, alias (\"default_handler\")));\n", a1);
}

original_code:
void DefineHandler(const char *s){
  fprintf(stdout, "void %s (void) __attribute__ ((weak,  alias (\"default_handler\")));\n", s);
}

canonical_original_code:
void DefineHandler(char *s){
  fprintf(stdout, "void %s (void) __attribute__ ((weak,  alias (\"default_handler\")));\n", s);
}

variable_types:
s
char *


PrintVectorName

decompiled_code:
int __fastcall PrintVectorName(const char *a1)
{
int result; // eax

if ( a1 )
{
if ( zeros_0 )
fwrite("\n\t", 1uLL, 2uLL, _bss_start);
result = fprintf(_bss_start, "%s,\n\t", a1);
zeros_0 = 0;
}
else
{
if ( zeros_0 == 8 )
{
result = fwrite("\n\t0,", 1uLL, 4uLL, _bss_start);
zeros_0 = 0;
}
else
{
if ( zeros_0 )
fputc(32, _bss_start);
result = fwrite("0,", 1uLL, 2uLL, _bss_start);
}
++zeros_0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
int result; // eax

if ( a1 )
{
if ( zeros_0 )
fwrite("\n\t", 1uLL, 2uLL, _bss_start);
result = fprintf(_bss_start, "%s,\n\t", a1);
zeros_0 = 0;
}
else
{
if ( zeros_0 == 8 )
{
result = fwrite("\n\t0,", 1uLL, 4uLL, _bss_start);
zeros_0 = 0;
}
else
{
if ( zeros_0 )
fputc(32, _bss_start);
result = fwrite("0,", 1uLL, 2uLL, _bss_start);
}
++zeros_0;
}
return result;
}

original_code:
void PrintVectorName (const char *s){
  static int zeros = 0;
  if (s) {
      if (zeros)
 fprintf(stdout, "\n\t");
      fprintf(stdout, "%s,\n\t", s);
      zeros = 0;
  } else {
      if (zeros == 8) {
   fprintf(stdout, "\n\t0,");
   zeros = 0;
      } else {
 if (zeros)
   fprintf(stdout, " ");
 fprintf(stdout, "0,");
      }
      zeros++;
  }
}

canonical_original_code:
void PrintVectorName(char *s){
  static int zeros = 0;
  if (s) {
      if (zeros)
 fprintf(stdout, "\n\t");
      fprintf(stdout, "%s,\n\t", s);
      zeros = 0;
  } else {
      if (zeros == 8) {
   fprintf(stdout, "\n\t0,");
   zeros = 0;
      } else {
 if (zeros)
   fprintf(stdout, " ");
 fprintf(stdout, "0,");
      }
      zeros++;
  }
}

variable_types: zeros int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
const char **v4; // rbx

fputs(header, _bss_start);
v3 = (const char **)off_405090;
v4 = (const char **)Vectors;
do
{
if ( *v3 )
DefineHandler(*v3);
++v3;
}
while ( v3 != (const char **)&_bss_start );
fputs(mid, _bss_start);
do
PrintVectorName(*v4++);
while ( v4 != (const char **)&_bss_start );
fwrite("\n};\n", 1uLL, 4uLL, _bss_start);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
const char **v4; // rbx

fputs(header, _bss_start);
v3 = (const char **)off_405090;
v4 = (const char **)Vectors;
do
{
if ( *v3 )
func7(*v3);
++v3;
}
while ( v3 != (const char **)&_bss_start );
fputs(mid, _bss_start);
do
func8(*v4++);
while ( v4 != (const char **)&_bss_start );
fwrite("\n};\n", 1uLL, 4uLL, _bss_start);
return 0;
}

original_code:
int main(){
  int i;
  fprintf(stdout,"%s",header);
  for (i = 2; i < sizeof(Vectors)/sizeof(char *); i++) {
      if (Vectors[i])
 DefineHandler(Vectors[i]);
  }
  fprintf(stdout, "%s", mid);
  for (i = 0; i < sizeof(Vectors)/sizeof(char *); i++) {
    PrintVectorName(Vectors[i]);
  }
  fprintf(stdout, "\n};\n");
  return 0;
}


repo: geoffreymoller/cspp-exercises

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

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

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

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

original_code:
void show_bytes(byte_pointer start, int len) {
  int i;
  for(i = 0; i < len; i++)
    printf(" %.2x", start[i]);
  printf("\n");
}

canonical_original_code:
void show_bytes(unsigned char *start, int len) {
  int i;
  for(i = 0; i < len; i++)
    printf(" %.2x", start[i]);
  printf("\n");
}

variable_types:
start
unsigned char *

variable_types: len int


decompiled_code:
int __fastcall show_int(int a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = a1;
return show_bytes((unsigned __int8 *)v2, 4);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = a1;
return func7((unsigned __int8 *)v2, 4);
}

original_code:
void show_int(int x) {
  show_bytes((byte_pointer) &x, sizeof(int));
}

variable_types: x int


decompiled_code:
int __fastcall show_float(float a1)
{
float v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = a1;
return show_bytes((unsigned __int8 *)v2, 4);
}

canonical_decompiled_code:
int __fastcall func9(float a1)
{
float v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = a1;
return func7((unsigned __int8 *)v2, 4);
}

original_code:
void show_float(float x) {
  show_bytes((byte_pointer) &x, sizeof(float));
}

variable_types: x float


decompiled_code:
int __fastcall show_pointer(__int64 a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = a1;
return show_bytes((unsigned __int8 *)&v2, 8);
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = a1;
return func7((unsigned __int8 *)&v2, 8);
}

original_code:
void show_pointer(void *x) {
  show_bytes((byte_pointer) &x, sizeof(void *));
}

variable_types:
x
void *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 1900;
show_int(1900);
show_float(1900.3199);
show_pointer((__int64)v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 1900;
func8(1900);
func9(1900.3199);
func10((__int64)v4);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  int a = 1900;
  float b = 1900.32;
  int *c = &a;
  show_int(a);
  show_float(b);
  show_pointer(c);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: b float

variable_types:
c
int *


repo: geofft/enviable

decompiled_code:
int __fastcall enviable_setenv(char *name)
{
char *v1; // rax
char *v2; // rbx
int result; // eax

v1 = strchr(name, 61);
v2 = v1;
if ( &set_var_attribute && &do_assignment_no_expand )
{
do_assignment_no_expand();
*v2 = 0;
result = set_var_attribute();
}
else
{
*v1 = 0;
result = setenv(name, v1 + 1, 1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char *name)
{
char *v1; // rax
char *v2; // rbx
int result; // eax

v1 = strchr(name, 61);
v2 = v1;
if ( &set_var_attribute && &do_assignment_no_expand )
{
func2();
*v2 = 0;
result = func1();
}
else
{
*v1 = 0;
result = setenv(name, v1 + 1, 1);
}
return result;
}

original_code:
static void
enviable_setenv(char *line)
{
 char *eq = strchr(line, '=');
 if (do_assignment_no_expand && set_var_attribute) {
  do_assignment_no_expand(line);
  *eq = '\0';
  set_var_attribute(line, 1, 0);
 } else {
  *eq = '\0';
  setenv(line, eq + 1, 1);
 }
}

variable_types:
line
char *

variable_types:
eq
char *


decompiled_code:
void enviable_init()
{
__int64 (*v0)(void); // rax
__int64 (*v1)(void); // rbx
int v2; // eax
int v3; // ebp
unsigned int v4; // ebx
__sigset_t *v5; // rdi
__int64 i; // rcx
unsigned int v7; // eax
unsigned int v8; // eax
struct sigaction v9; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v10; // [rsp+A0h] [rbp-B8h] BYREF

v0 = (__int64 (*)(void))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "__libc_current_sigrtmin");
real___libc_current_sigrtmin = v0;
if ( !v0 )
{
perror("enviable: incompatible libc");
return;
}
v1 = v0;
v2 = inotify_init1(526336);
v3 = v2;
if ( v2 == -1 )
{
perror("enviable: inotify_init");
return;
}
if ( inotify_add_watch(v2, "/tmp/vars", 8u) == -1 )
{
perror("enviable: inotify_add_watch");
goto LABEL_11;
}
v4 = v1();
v5 = &v10.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v5->__val[0]) = 0;
v5 = (__sigset_t *)((char *)v5 + 4);
}
v10.sa_handler = (__sighandler_t)enviable_callback;
v10.sa_flags = 4;
if ( sigaction(v4, &v10, &v9) == -1 )
{
perror("enviable: sigaction");
goto LABEL_11;
}
if ( (v9.sa_flags & 4) != 0 || v9.sa_handler )
{
LABEL_10:
sigaction(v4, &v9, 0LL);
LABEL_11:
close(v3);
return;
}
v7 = fcntl(v3, 3);
if ( v7 == -1 )
{
perror("enviable: fcntl F_GETFL");
goto LABEL_10;
}
BYTE1(v7) |= 0x20u;
if ( fcntl(v3, 4, v7) == -1 )
{
perror("enviable: fcntl F_SETFL O_ASYNC");
goto LABEL_10;
}
v8 = getpid();
if ( fcntl(v3, 8, v8) == -1 )
{
perror("enviable: fcntl F_SETOWN");
goto LABEL_10;
}
if ( fcntl(v3, 10, v4) == -1 )
{
perror("enviable: fcntl F_SETSIG");
goto LABEL_10;
}
}

canonical_decompiled_code:
void func11()
{
__int64 (*v0)(void); // rax
__int64 (*v1)(void); // rbx
int v2; // eax
int v3; // ebp
unsigned int v4; // ebx
__sigset_t *v5; // rdi
__int64 i; // rcx
unsigned int v7; // eax
unsigned int v8; // eax
struct sigaction v9; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v10; // [rsp+A0h] [rbp-B8h] BYREF

v0 = (__int64 (*)(void))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "__libc_current_sigrtmin");
real___libc_current_sigrtmin = v0;
if ( !v0 )
{
perror("enviable: incompatible libc");
return;
}
v1 = v0;
v2 = inotify_init1(526336);
v3 = v2;
if ( v2 == -1 )
{
perror("enviable: inotify_init");
return;
}
if ( inotify_add_watch(v2, "/tmp/vars", 8u) == -1 )
{
perror("enviable: inotify_add_watch");
goto LABEL_11;
}
v4 = v1();
v5 = &v10.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v5->__val[0]) = 0;
v5 = (__sigset_t *)((char *)v5 + 4);
}
v10.sa_handler = (__sighandler_t)enviable_callback;
v10.sa_flags = 4;
if ( sigaction(v4, &v10, &v9) == -1 )
{
perror("enviable: sigaction");
goto LABEL_11;
}
if ( (v9.sa_flags & 4) != 0 || v9.sa_handler )
{
LABEL_10:
sigaction(v4, &v9, 0LL);
LABEL_11:
close(v3);
return;
}
v7 = fcntl(v3, 3);
if ( v7 == -1 )
{
perror("enviable: fcntl F_GETFL");
goto LABEL_10;
}
BYTE1(v7) |= 0x20u;
if ( fcntl(v3, 4, v7) == -1 )
{
perror("enviable: fcntl F_SETFL O_ASYNC");
goto LABEL_10;
}
v8 = getpid();
if ( fcntl(v3, 8, v8) == -1 )
{
perror("enviable: fcntl F_SETOWN");
goto LABEL_10;
}
if ( fcntl(v3, 10, v4) == -1 )
{
perror("enviable: fcntl F_SETSIG");
goto LABEL_10;
}
}

original_code:
void __attribute__((constructor))
enviable_init(void)
{
 real___libc_current_sigrtmin = dlsym(((void *) -1l), "__libc_current_sigrtmin");
 if (!real___libc_current_sigrtmin) {
  perror("enviable: incompatible libc");
  return;
 }
 int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
 if (fd == -1) {
  perror("enviable: inotify_init");
  return;
 }
 int watch = inotify_add_watch(fd, watchfile, 0x00000008);
 if (watch == -1) {
  perror("enviable: inotify_add_watch");
  goto err;
 }
 int sigrtmin = real___libc_current_sigrtmin();
 struct sigaction act = {
  .__sigaction_handler.sa_sigaction = enviable_callback,
  .sa_mask = 0,
  .sa_flags = 4,
 }, oldact;
 if (sigaction(sigrtmin, &act, &oldact) == -1) {
  perror("enviable: sigaction");
  goto err;
 }
 if ((oldact.sa_flags & 4) ||
     (oldact.__sigaction_handler.sa_handler != ((__sighandler_t) 0))) {
  goto err_sig;
 }
 int flags = fcntl(fd, 3);
 if (flags == -1) {
  perror("enviable: fcntl F_GETFL");
  goto err_sig;
 }
 if (fcntl(fd, 4, flags | 020000) == -1) {
  perror("enviable: fcntl F_SETFL O_ASYNC");
  goto err_sig;
 }
 if (fcntl(fd, 8, getpid()) == -1) {
  perror("enviable: fcntl F_SETOWN");
  goto err_sig;
 }
 if (fcntl(fd, 10, sigrtmin) == -1) {
  perror("enviable: fcntl F_SETSIG");
  goto err_sig;
 }
 return;
err_sig:
 sigaction(sigrtmin, &oldact, ((void *)0));
err:
 close(fd);
}

canonical_original_code:
void enviable_init()
{
 real___libc_current_sigrtmin = dlsym(((void *) -1l), "__libc_current_sigrtmin");
 if (!real___libc_current_sigrtmin) {
  perror("enviable: incompatible libc");
  return;
 }
 int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
 if (fd == -1) {
  perror("enviable: inotify_init");
  return;
 }
 int watch = inotify_add_watch(fd, watchfile, 0x00000008);
 if (watch == -1) {
  perror("enviable: inotify_add_watch");
  goto err;
 }
 int sigrtmin = real___libc_current_sigrtmin();
 struct sigaction act = {
  .__sigaction_handler.sa_sigaction = enviable_callback,
  .sa_mask = 0,
  .sa_flags = 4,
 }; struct sigaction oldact;
 if (sigaction(sigrtmin, &act, &oldact) == -1) {
  perror("enviable: sigaction");
  goto err;
 }
 if ((oldact.sa_flags & 4) ||
     (oldact.__sigaction_handler.sa_handler != ((void (*)(int)) 0))) {
  goto err_sig;
 }
 int flags = fcntl(fd, 3);
 if (flags == -1) {
  perror("enviable: fcntl F_GETFL");
  goto err_sig;
 }
 if (fcntl(fd, 4, flags | 020000) == -1) {
  perror("enviable: fcntl F_SETFL O_ASYNC");
  goto err_sig;
 }
 if (fcntl(fd, 8, getpid()) == -1) {
  perror("enviable: fcntl F_SETOWN");
  goto err_sig;
 }
 if (fcntl(fd, 10, sigrtmin) == -1) {
  perror("enviable: fcntl F_SETSIG");
  goto err_sig;
 }
 return;
err_sig:
 sigaction(sigrtmin, &oldact, ((void *)0));
err:
 close(fd);
}

variable_types: fd int

variable_types: watch int

variable_types: sigrtmin int

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

variable_types: flags int

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

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


repo: geofft/leaptest-timer

decompiled_code:
__int64 __fastcall timespec_add(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v4; // rdx
__int64 v5; // rdi

result = a1;
v4 = a2 + a3;
if ( v4 > 0x3B9AC9FF )
{
v5 = a1 + 1;
do
{
v4 -= 1000000000LL;
result = v5++;
}
while ( v4 > 0x3B9AC9FF );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v4; // rdx
__int64 v5; // rdi

result = a1;
v4 = a2 + a3;
if ( v4 > 0x3B9AC9FF )
{
v5 = a1 + 1;
do
{
v4 -= 1000000000LL;
result = v5++;
}
while ( v4 > 0x3B9AC9FF );
}
return result;
}

original_code:
struct timespec timespec_add(struct timespec ts, unsigned long long ns)
{
 ts.tv_nsec += ns;
 while(ts.tv_nsec >= 1000000000ULL) {
  ts.tv_nsec -= 1000000000ULL;
  ts.tv_sec++;
 }
 return ts;
}

variable_types:
ts
struct timespec

variable_types:
ns
unsigned long long

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


decompiled_code:
unsigned __int64 __fastcall timespec_diff(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v4; // rsi
unsigned __int64 result; // rax

v4 = 1000000000 * a1 - a4 + a2 - 1000000000 * a3;
if ( v4 < 0 )
result = -(__int64)(-v4 / 0x3B9ACA00uLL);
else
result = v4 / 0x3B9ACA00uLL;
return result;
}

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

v4 = 1000000000 * a1 - a4 + a2 - 1000000000 * a3;
if ( v4 < 0 )
result = -(__int64)(-v4 / 0x3B9ACA00uLL);
else
result = v4 / 0x3B9ACA00uLL;
return result;
}

original_code:
struct timespec timespec_diff(struct timespec a, struct timespec b)
{
 long long ns;
 int neg = 0;
 ns = a.tv_sec *1000000000ULL + a.tv_nsec;
 ns -= b.tv_sec *1000000000ULL + b.tv_nsec;
 if (ns < 0) {
  neg = 1;
  ns = -ns;
 }
 a.tv_sec = ns/1000000000ULL;
 a.tv_nsec = ns%1000000000ULL;
 if (neg) {
  a.tv_sec = -a.tv_sec;
  a.tv_nsec = -a.tv_nsec;
 }
 return a;
}

variable_types:
a
struct timespec

variable_types:
b
struct timespec

variable_types:
ns
long long

variable_types: neg int

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


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__time_t v3; // rbx
__syscall_slong_t v4; // rdx
__int64 v5; // rdx
signed __int64 v7; // [rsp+18h] [rbp-40h]
struct timespec req; // [rsp+20h] [rbp-38h] BYREF
struct timespec rem; // [rsp+30h] [rbp-28h] BYREF
struct timespec tp; // [rsp+40h] [rbp-18h] BYREF

clock_gettime(0, &tp);
v3 = tp.tv_sec;
do
{
rem.tv_sec = 0LL;
rem.tv_nsec = 0LL;
req.tv_sec = timespec_add(tp.tv_sec, tp.tv_nsec, 500000000LL);
req.tv_nsec = v4;
clock_nanosleep(0, 1, &req, &rem);
clock_gettime(0, &tp);
v7 = timespec_diff(tp.tv_sec, tp.tv_nsec, req.tv_sec, req.tv_nsec);
printf("diff: %Lf\n", (long double)v5 * 0.000000001 + (long double)v7);
}
while ( tp.tv_sec - v3 <= 9 );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__time_t v3; // rbx
__syscall_slong_t v4; // rdx
__int64 v5; // rdx
signed __int64 v7; // [rsp+18h] [rbp-40h]
struct timespec req; // [rsp+20h] [rbp-38h] BYREF
struct timespec rem; // [rsp+30h] [rbp-28h] BYREF
struct timespec tp; // [rsp+40h] [rbp-18h] BYREF

clock_gettime(0, &tp);
v3 = tp.tv_sec;
do
{
rem.tv_sec = 0LL;
rem.tv_nsec = 0LL;
req.tv_sec = func7(tp.tv_sec, tp.tv_nsec, 500000000LL);
req.tv_nsec = v4;
clock_nanosleep(0, 1, &req, &rem);
clock_gettime(0, &tp);
v7 = func8(tp.tv_sec, tp.tv_nsec, req.tv_sec, req.tv_nsec);
printf("diff: %Lf\n", (long double)v5 * 0.000000001 + (long double)v7);
}
while ( tp.tv_sec - v3 <= 9 );
return 0;
}

original_code:
int main(void)
{
 long now, then;
 struct timespec ts;
 int clock_type = 0;
 int flag = 1;
 long long sleeptime = 1000000000ULL/2;
 clock_gettime(clock_type, &ts);
 now = then = ts.tv_sec;
 while(now - then < 10){
  struct timespec target, diff, rem;
  rem.tv_sec = 0;
  rem.tv_nsec = 0;
  target = timespec_add(ts, sleeptime);
  clock_nanosleep(clock_type, 1, &target, &rem);
  clock_gettime(clock_type, &ts);
  diff = timespec_diff(ts, target);
  printf("diff: %Lf\n", diff.tv_sec + ((long double)0.000000001 * diff.tv_nsec));
  now = ts.tv_sec;
 }
 return 0;
}

canonical_original_code:
int main()
{
 long now; long then;
 struct timespec ts;
 int clock_type = 0;
 int flag = 1;
 long long sleeptime = 1000000000ULL/2;
 clock_gettime(clock_type, &ts);
 now = then = ts.tv_sec;
 while(now - then < 10){
  struct timespec target; struct timespec diff; struct timespec rem;
  rem.tv_sec = 0;
  rem.tv_nsec = 0;
  target = timespec_add(ts, sleeptime);
  clock_nanosleep(clock_type, 1, &target, &rem);
  clock_gettime(clock_type, &ts);
  diff = timespec_diff(ts, target);
  printf("diff: %Lf\n", diff.tv_sec + ((long double)0.000000001 * diff.tv_nsec));
  now = ts.tv_sec;
 }
 return 0;
}

variable_types: now long

variable_types: then long

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

variable_types: flag int

variable_types:
sleeptime
long long


repo: geofft/vireo

original_code:
static void
make_stack(ucontext_t *ucp)
{
 if (ucp->uc_stack.ss_sp)
  return;
 ucp->uc_stack.ss_sp = mmap(
  ((void *)0), 16384, 0x1 | 0x2,
  0x20 | 0x00100 | 0x02,
  -1, 0);
 ucp->uc_stack.ss_size = 16384;
}

canonical_original_code:
static void make_stack(struct ucontext_t *ucp)
{
 if (ucp->uc_stack.ss_sp)
  return;
 ucp->uc_stack.ss_sp = mmap(
  ((void *)0), 16384, 0x1 | 0x2,
  0x20 | 0x00100 | 0x02,
  -1, 0);
 ucp->uc_stack.ss_size = 16384;
}

variable_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } *

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }


decompiled_code:
void __noreturn vireo_schedule()
{
int v0; // ebp
int v1; // ebx

v0 = 0;
while ( 1 )
{
v1 = (curenv + v0 + 1) % 1024;
if ( envs[248 * v1] == 1 )
{
curenv = (curenv + v0 + 1) % 1024;
timer_settime(timer, 0, &ts, 0LL);
setcontext((const ucontext_t *)&envs[248 * v1 + 2]);
}
if ( ++v0 == 1024 )
exit(0);
}
}

canonical_decompiled_code:
void __noreturn func7()
{
int v0; // ebp
int v1; // ebx

v0 = 0;
while ( 1 )
{
v1 = (curenv + v0 + 1) % 1024;
if ( envs[248 * v1] == 1 )
{
curenv = (curenv + v0 + 1) % 1024;
timer_settime(timer, 0, &ts, 0LL);
setcontext((const ucontext_t *)&envs[248 * v1 + 2]);
}
if ( ++v0 == 1024 )
exit(0);
}
}

original_code:
static void
vireo_schedule(void)
{
 int attempts = 0;
 int candidate;
 while (attempts < 1024) {
  candidate = (curenv + attempts + 1) % 1024;
  if (envs[candidate].status == 1) {
   curenv = candidate;
   timer_settime(timer, 0, &ts, ((void *)0));
   setcontext(&envs[curenv].state);
  }
  attempts++;
 }
 exit(0);
}

canonical_original_code:
static void vireo_schedule()
{
 int attempts = 0;
 int candidate;
 while (attempts < 1024) {
  candidate = (curenv + attempts + 1) % 1024;
  if (envs[candidate].status == 1) {
   curenv = candidate;
   timer_settime(timer, 0, &ts, ((void *)0));
   setcontext(&envs[curenv].state);
  }
  attempts++;
 }
 exit(0);
}

variable_types: attempts int

variable_types: candidate int


decompiled_code:
void __noreturn vireo_exit()
{
envs[248 * curenv] = 0;
vireo_schedule();
}

canonical_decompiled_code:
void __noreturn func8()
{
envs[248 * curenv] = 0;
func7();
}

original_code:
void
vireo_exit(void)
{
 envs[curenv].status = 0;
 vireo_schedule();
}

canonical_original_code:
void vireo_exit()
{
 envs[curenv].status = 0;
 vireo_schedule();
}


decompiled_code:
int enable_preemption()
{
int v0; // eax
struct sigevent v2; // [rsp+0h] [rbp-E8h] BYREF
struct sigaction v3; // [rsp+40h] [rbp-A8h] BYREF

memset(&v3.sa_mask, 0, 0x90uLL);
v3.sa_handler = (__sighandler_t)preempt;
v3.sa_flags = 4;
v2.sigev_value.sival_ptr = 0LL;
*(_OWORD *)v2._sigev_un._pad = 0uLL;
*((_OWORD *)&v2._sigev_un._sigev_thread + 1) = 0uLL;
*((_OWORD *)&v2._sigev_un._sigev_thread + 2) = 0uLL;
*(_QWORD *)&v2.sigev_signo = (unsigned int)__libc_current_sigrtmin();
sigemptyset(&v3.sa_mask);
v0 = __libc_current_sigrtmin();
sigaction(v0, &v3, 0LL);
return timer_create(2, &v2, timer);
}

canonical_decompiled_code:
int func9()
{
int v0; // eax
struct sigevent v2; // [rsp+0h] [rbp-E8h] BYREF
struct sigaction v3; // [rsp+40h] [rbp-A8h] BYREF

memset(&v3.sa_mask, 0, 0x90uLL);
v3.sa_handler = (__sighandler_t)preempt;
v3.sa_flags = 4;
v2.sigev_value.sival_ptr = 0LL;
*(_OWORD *)v2._sigev_un._pad = 0uLL;
*((_OWORD *)&v2._sigev_un._sigev_thread + 1) = 0uLL;
*((_OWORD *)&v2._sigev_un._sigev_thread + 2) = 0uLL;
*(_QWORD *)&v2.sigev_signo = (unsigned int)__libc_current_sigrtmin();
sigemptyset(&v3.sa_mask);
v0 = __libc_current_sigrtmin();
sigaction(v0, &v3, 0LL);
return timer_create(2, &v2, timer);
}

original_code:
static void
enable_preemption(void)
{
 struct sigaction act = {
  .__sigaction_handler.sa_sigaction = preempt,
  .sa_flags = 4,
 };
 struct sigevent sigev = {
  .sigev_notify = SIGEV_SIGNAL,
  .sigev_signo = (__libc_current_sigrtmin ()),
  .sigev_value.sival_int = 0,
 };
 sigemptyset(&act.sa_mask);
 sigaction((__libc_current_sigrtmin ()), &act, ((void *)0));
 timer_create(2, &sigev, &timer);
}

canonical_original_code:
static void enable_preemption()
{
 struct sigaction act = {
  .__sigaction_handler.sa_sigaction = preempt,
  .sa_flags = 4,
 };
 struct sigevent sigev = {
  .sigev_notify = SIGEV_SIGNAL,
  .sigev_signo = (__libc_current_sigrtmin ()),
  .sigev_value.sival_int = 0,
 };
 sigemptyset(&act.sa_mask);
 sigaction((__libc_current_sigrtmin ()), &act, ((void *)0));
 timer_create(2, &sigev, &timer);
}

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

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

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 __fastcall vireo_create(void (*a1)(void))
{
_DWORD *v1; // rax
__int64 v2; // rcx
unsigned int v4; // [rsp+Ch] [rbp-1Ch]
ucontext_t *ucp; // [rsp+10h] [rbp-18h]

v1 = envs;
v4 = 0;
while ( *v1 )
{
++v4;
v1 += 248;
if ( v4 == 1024 )
return (unsigned int)-1;
}
v2 = 248LL * (int)v4;
envs[v2] = 1;
ucp = (ucontext_t *)&envs[v2 + 2];
getcontext(ucp);
make_stack((__int64)ucp);
*(_QWORD *)&envs[248 * v4 + 4] = &exiter;
makecontext(ucp, a1, 0);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(void (*a1)(void))
{
_DWORD *v1; // rax
__int64 v2; // rcx
unsigned int v4; // [rsp+Ch] [rbp-1Ch]
ucontext_t *ucp; // [rsp+10h] [rbp-18h]

v1 = envs;
v4 = 0;
while ( *v1 )
{
++v4;
v1 += 248;
if ( v4 == 1024 )
return (unsigned int)-1;
}
v2 = 248LL * (int)v4;
envs[v2] = 1;
ucp = (ucontext_t *)&envs[v2 + 2];
getcontext(ucp);
func6((__int64)ucp);
*(_QWORD *)&envs[248 * v4 + 4] = &exiter;
makecontext(ucp, a1, 0);
return v4;
}

original_code:
int
vireo_create(vireo_entry entry)
{
 int env;
 for (env = 0; (env < 1024); env++) {
  if (envs[env].status == 0) {
   break;
  }
 }
 if (env == 1024) {
  return -1;
 }
 envs[env].status = 1;
 getcontext(&envs[env].state);
 make_stack(&envs[env].state);
 envs[env].state.uc_link = &exiter;
 makecontext(&envs[env].state, entry, 0);
 return env;
}

canonical_original_code:
int vireo_create(void (*entry)())
{
 int env;
 for (env = 0; (env < 1024); env++) {
  if (envs[env].status == 0) {
   break;
  }
 }
 if (env == 1024) {
  return -1;
 }
 envs[env].status = 1;
 getcontext(&envs[env].state);
 make_stack(&envs[env].state);
 envs[env].state.uc_link = &exiter;
 makecontext(&envs[env].state, entry, 0);
 return env;
}

variable_types:
entry
void (*)()

variable_types: env int


decompiled_code:
__int64 vireo_yield()
{
__int64 v0; // rcx
int v1; // ecx
__int64 result; // rax

v0 = 248LL * curenv;
envs[v0 + 244] = 0;
getcontext((ucontext_t *)&envs[v0 + 2]);
v1 = envs[248 * curenv + 244];
result = 248LL * curenv;
envs[result + 244] = v1 + 1;
if ( !v1 )
vireo_schedule();
return result * 4;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rcx
int v1; // ecx
__int64 result; // rax

v0 = 248LL * curenv;
envs[v0 + 244] = 0;
getcontext((ucontext_t *)&envs[v0 + 2]);
v1 = envs[248 * curenv + 244];
result = 248LL * curenv;
envs[result + 244] = v1 + 1;
if ( !v1 )
func7();
return result * 4;
}

original_code:
void
vireo_yield(void)
{
 envs[curenv].state_reentered = 0;
 getcontext(&envs[curenv].state);
 if (envs[curenv].state_reentered++ == 0) {
  vireo_schedule();
 }
}

canonical_original_code:
void vireo_yield()
{
 envs[curenv].state_reentered = 0;
 getcontext(&envs[curenv].state);
 if (envs[curenv].state_reentered++ == 0) {
  vireo_schedule();
 }
}


preempt

decompiled_code:
__int64 preempt()
{
return vireo_yield();
}

canonical_decompiled_code:
__int64 func13()
{
return func12();
}

original_code:
static void
preempt(int signum __attribute__((unused)), siginfo_t *si __attribute__((unused)), void *context __attribute__((unused)))
{
 vireo_yield();
}

canonical_original_code:
static void preempt(int signum, struct siginfo_t *si, void *context)
{
 vireo_yield();
}

variable_types: signum int

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

variable_types:
context
void *


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

result = 248LL * a1;
envs[result] = 0;
return result * 4;
}

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

result = 248LL * a1;
envs[result] = 0;
return result * 4;
}

original_code:
void
vireo_destroy(int env)
{
 envs[env].status = 0;
}


decompiled_code:
__int64 vireo_getid()
{
return (unsigned int)curenv;
}

canonical_decompiled_code:
__int64 func15()
{
return (unsigned int)curenv;
}

original_code:
int
vireo_getid(void)
{
 return curenv;
}

canonical_original_code:
int vireo_getid()
{
 return curenv;
}


decompiled_code:
__int64 __fastcall vireo_recv(_DWORD *a1)
{
envs[248 * curenv] = 2;
vireo_yield();
if ( a1 )
*a1 = envs[248 * curenv + 245];
return (unsigned int)envs[248 * curenv + 246];
}

canonical_decompiled_code:
__int64 __fastcall func16(_DWORD *a1)
{
envs[248 * curenv] = 2;
func12();
if ( a1 )
*a1 = envs[248 * curenv + 245];
return (unsigned int)envs[248 * curenv + 246];
}

original_code:
int
vireo_recv(int *who)
{
 envs[curenv].status = 2;
 vireo_yield();
 if (who)
  *who = envs[curenv].ipc_sender;
 return envs[curenv].ipc_value;
}

variable_types:
who
int *


decompiled_code:
__int64 __fastcall vireo_send(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rcx

while ( envs[248 * a1] != 2 )
vireo_yield();
result = 32LL * a1;
envs[248 * a1 + 245] = curenv;
v3 = 248LL * a1;
envs[v3 + 246] = a2;
envs[v3] = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rcx

while ( envs[248 * a1] != 2 )
func12();
result = 32LL * a1;
envs[248 * a1 + 245] = curenv;
v3 = 248LL * a1;
envs[v3 + 246] = a2;
envs[v3] = 1;
return result;
}

original_code:
void
vireo_send(int toenv, int val)
{
 while (envs[toenv].status != 2)
  vireo_yield();
 envs[toenv].ipc_sender = curenv;
 envs[toenv].ipc_value = val;
 envs[toenv].status = 1;
}

variable_types: toenv int

variable_types: val int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
enable_preemption();
initialize_threads((__int64)&umain);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func9();
func11((__int64)&umain);
return 0;
}

original_code:
int
main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
{
 enable_preemption();
 initialize_threads(umain);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 enable_preemption();
 initialize_threads(umain);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


child

decompiled_code:
void __noreturn child()
{
puts("I am the child. Spinning...");
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("I am the child. Spinning...");
while ( 1 )
;
}

original_code:
void
child(void)
{
 printf("I am the child.  Spinning...\n");
 while (1)
                  ;
}

canonical_original_code:
void child()
{
 printf("I am the child.  Spinning...\n");
 while (1)
                  ;
}


umain

decompiled_code:
__int64 umain()
{
puts("I am the parent. Forking the child...");
vireo_create();
puts("I am the parent. Running the child...");
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
puts("I am the parent. Killing the child...");
return vireo_destroy();
}

canonical_decompiled_code:
__int64 func8()
{
puts("I am the parent. Forking the child...");
vireo_create();
puts("I am the parent. Running the child...");
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
vireo_yield();
puts("I am the parent. Killing the child...");
return vireo_destroy();
}

original_code:
void
umain(void)
{
 int env;
 printf("I am the parent.  Forking the child...\n");
 env = vireo_create(child);
 printf("I am the parent.  Running the child...\n");
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 printf("I am the parent.  Killing the child...\n");
 vireo_destroy(env);
}

canonical_original_code:
void umain()
{
 int env;
 printf("I am the parent.  Forking the child...\n");
 env = vireo_create(child);
 printf("I am the parent.  Running the child...\n");
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 vireo_yield();
 printf("I am the parent.  Killing the child...\n");
 vireo_destroy(env);
}


primeproc

decompiled_code:
void __noreturn primeproc()
{
__int64 v0; // rbx

v0 = (unsigned int)vireo_recv();
printf("%d ", v0);
if ( (unsigned int)vireo_create() != -1 )
{
while ( 1 )
{
while ( !((int)vireo_recv() % (int)v0) )
;
vireo_send();
}
}
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
__int64 v0; // rbx

v0 = (unsigned int)vireo_recv();
printf("%d ", v0);
if ( (unsigned int)vireo_create() != -1 )
{
while ( 1 )
{
while ( !((int)vireo_recv() % (int)v0) )
;
vireo_send();
}
}
exit(0);
}

original_code:
void
primeproc(void)
{
 int i, id, p;
 p = vireo_recv(((void *)0));
 printf("%d ", p);
 id = vireo_create(primeproc);
 if (id == -1)
  exit(0);
 while (1) {
  i = vireo_recv(((void *)0));
  if (i % p)
   vireo_send(id, i);
 }
}

canonical_original_code:
void primeproc()
{
 int i; int id; int p;
 p = vireo_recv(((void *)0));
 printf("%d ", p);
 id = vireo_create(primeproc);
 if (id == -1)
  exit(0);
 while (1) {
  i = vireo_recv(((void *)0));
  if (i % p)
   vireo_send(id, i);
 }
}

variable_types: i int

variable_types: id int

variable_types: p int


umain

decompiled_code:
void __noreturn umain()
{
int i; // ebx

vireo_create();
for ( i = 2; ; ++i )
vireo_send();
}

canonical_decompiled_code:
void __noreturn func8()
{
int i; // ebx

vireo_create();
for ( i = 2; ; ++i )
vireo_send();
}

original_code:
void
umain(void)
{
 int i, id;
 id = vireo_create(primeproc);
 for (i = 2; ; i++)
  vireo_send(id, i);
}

canonical_original_code:
void umain()
{
 int i; int id;
 id = vireo_create(primeproc);
 for (i = 2; ; i++)
  vireo_send(id, i);
}


pingpong

decompiled_code:
int pingpong()
{
unsigned int v0; // ebx
unsigned int v1; // eax
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch]

do
{
v0 = vireo_recv();
v1 = vireo_getid();
result = printf("%x got %d from %x\n", v1, v0, v3);
if ( v0 == 10 )
break;
result = vireo_send();
}
while ( v0 != 9 );
return result;
}

canonical_decompiled_code:
int func7()
{
unsigned int v0; // ebx
unsigned int v1; // eax
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch]

do
{
v0 = vireo_recv();
v1 = vireo_getid();
result = printf("%x got %d from %x\n", v1, v0, v3);
if ( v0 == 10 )
break;
result = vireo_send();
}
while ( v0 != 9 );
return result;
}

original_code:
void
pingpong(void)
{
 int who;
 while (1) {
  int i = vireo_recv(&who);
  printf("%x got %d from %x\n", vireo_getid(), i, who);
  if (i == 10)
   return;
  i++;
  vireo_send(who, i);
  if (i == 10)
   return;
 }
}

canonical_original_code:
void pingpong()
{
 int who;
 while (1) {
  int i = vireo_recv(&who);
  printf("%x got %d from %x\n", vireo_getid(), i, who);
  if (i == 10)
   return;
  i++;
  vireo_send(who, i);
  if (i == 10)
   return;
 }
}

variable_types: who int


umain

decompiled_code:
int umain()
{
unsigned int v0; // ebx
unsigned int v1; // eax

v0 = vireo_create();
v1 = vireo_getid();
printf("send 0 from %x to %x\n", v1, v0);
vireo_send();
return pingpong();
}

canonical_decompiled_code:
int func8()
{
unsigned int v0; // ebx
unsigned int v1; // eax

v0 = vireo_create();
v1 = vireo_getid();
printf("send 0 from %x to %x\n", v1, v0);
vireo_send();
return func7();
}

original_code:
void
umain(void)
{
 int who = vireo_create(pingpong);
 printf("send 0 from %x to %x\n", vireo_getid(), who);
 vireo_send(who, 0);
 pingpong();
}

canonical_original_code:
void umain()
{
 int who = vireo_create(pingpong);
 printf("send 0 from %x to %x\n", vireo_getid(), who);
 vireo_send(who, 0);
 pingpong();
}


decompiled_code:
int yield_thread()
{
unsigned int v0; // eax
unsigned int i; // ebx
unsigned int v2; // eax
unsigned int v3; // eax

v0 = vireo_getid();
printf("Hello, I am environment %08x.\n", v0);
for ( i = 0; i != 5; ++i )
{
vireo_yield();
v2 = vireo_getid();
printf("Back in environment %08x, iteration %d.\n", v2, i);
}
v3 = vireo_getid();
return printf("All done in environment %08x.\n", v3);
}

canonical_decompiled_code:
int func7()
{
unsigned int v0; // eax
unsigned int i; // ebx
unsigned int v2; // eax
unsigned int v3; // eax

v0 = vireo_getid();
printf("Hello, I am environment %08x.\n", v0);
for ( i = 0; i != 5; ++i )
{
vireo_yield();
v2 = vireo_getid();
printf("Back in environment %08x, iteration %d.\n", v2, i);
}
v3 = vireo_getid();
return printf("All done in environment %08x.\n", v3);
}

original_code:
static void
yield_thread(void)
{
 int i;
 printf("Hello, I am environment %08x.\n", vireo_getid());
 for (i = 0; i < 5; i++) {
  vireo_yield();
  printf("Back in environment %08x, iteration %d.\n",
         vireo_getid(), i);
 }
 printf("All done in environment %08x.\n", vireo_getid());
}

canonical_original_code:
static void yield_thread()
{
 int i;
 printf("Hello, I am environment %08x.\n", vireo_getid());
 for (i = 0; i < 5; i++) {
  vireo_yield();
  printf("Back in environment %08x, iteration %d.\n",
         vireo_getid(), i);
 }
 printf("All done in environment %08x.\n", vireo_getid());
}


decompiled_code:
__int64 umain()
{
vireo_create();
vireo_create();
vireo_create();
return vireo_exit();
}

canonical_decompiled_code:
__int64 func8()
{
vireo_create();
vireo_create();
vireo_create();
return vireo_exit();
}

original_code:
void
umain(void)
{
 int i;
 for (i = 0; i < 3; i++) {
  vireo_create(yield_thread);
 }
 vireo_exit();
}

canonical_original_code:
void umain()
{
 int i;
 for (i = 0; i < 3; i++) {
  vireo_create(yield_thread);
 }
 vireo_exit();
}


repo: geolocationrhit/Senior-Design

decompiled_code:
__int64 __fastcall gpio_export(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

original_code:
int gpio_export(unsigned int gpio)
{
 int fd, len;
 char buf[64];
 fd = open("/sys/class/gpio" "/export", 01);
 if (fd < 0) {
  perror("gpio/export");
  return fd;
 }
 len = snprintf(buf, sizeof(buf), "%d", gpio);
 write(fd, buf, len);
 close(fd);
 return 0;
}

canonical_original_code:
int gpio_export(unsigned int gpio)
{
 int fd; int len;
 char buf[64];
 fd = open("/sys/class/gpio" "/export", 01);
 if (fd < 0) {
  perror("gpio/export");
  return fd;
 }
 len = snprintf(buf, sizeof(buf), "%d", gpio);
 write(fd, buf, len);
 close(fd);
 return 0;
}

variable_types:
gpio
unsigned int

variable_types: fd int

variable_types: len int

variable_types:
buf
char [64]


decompiled_code:
__int64 __fastcall gpio_unexport(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

original_code:
int gpio_unexport(unsigned int gpio)
{
 int fd, len;
 char buf[64];
 fd = open("/sys/class/gpio" "/unexport", 01);
 if (fd < 0) {
  perror("gpio/export");
  return fd;
 }
 len = snprintf(buf, sizeof(buf), "%d", gpio);
 write(fd, buf, len);
 close(fd);
 return 0;
}

canonical_original_code:
int gpio_unexport(unsigned int gpio)
{
 int fd; int len;
 char buf[64];
 fd = open("/sys/class/gpio" "/unexport", 01);
 if (fd < 0) {
  perror("gpio/export");
  return fd;
 }
 len = snprintf(buf, sizeof(buf), "%d", gpio);
 write(fd, buf, len);
 close(fd);
 return 0;
}


decompiled_code:
__int64 __fastcall gpio_set_dir(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/direction");
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/direction");
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
v3 = 0;
}
return v3;
}

original_code:
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
 int fd, len;
 char buf[64];
 len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/direction", gpio);
 fd = open(buf, 01);
 if (fd < 0) {
  perror("gpio/direction");
  return fd;
 }
 if (out_flag)
  write(fd, "out", 4);
 else
  write(fd, "in", 3);
 close(fd);
 return 0;
}

canonical_original_code:
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
 int fd; int len;
 char buf[64];
 len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/direction", gpio);
 fd = open(buf, 01);
 if (fd < 0) {
  perror("gpio/direction");
  return fd;
 }
 if (out_flag)
  write(fd, "out", 4);
 else
  write(fd, "in", 3);
 close(fd);
 return 0;
}


decompiled_code:
__int64 __fastcall gpio_set_value(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/set-value");
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/set-value");
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
v3 = 0;
}
return v3;
}

original_code:
int gpio_set_value(unsigned int gpio, unsigned int value)
{
 int fd, len;
 char buf[64];
 len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
 fd = open(buf, 01);
 if (fd < 0) {
  perror("gpio/set-value");
  return fd;
 }
 if (value)
  write(fd, "1", 2);
 else
  write(fd, "0", 2);
 close(fd);
 return 0;
}

canonical_original_code:
int gpio_set_value(unsigned int gpio, unsigned int value)
{
 int fd; int len;
 char buf[64];
 len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
 fd = open(buf, 01);
 if (fd < 0) {
  perror("gpio/set-value");
  return fd;
 }
 if (value)
  write(fd, "1", 2);
 else
  write(fd, "0", 2);
 close(fd);
 return 0;
}

variable_types:
value
unsigned int