decompiled_code:
__int64 __fastcall float2int(__m128i a1)
{
int v1; // ebx
int *v2; // rax
int v3; // edx
int v4; // ecx
unsigned int v5; // esi
unsigned int v6; // ebp

v1 = _mm_cvtsi128_si32(a1);
v2 = __errno_location();
*v2 = 0;
v3 = (unsigned __int8)((unsigned int)v1 >> 23);
v4 = v3 - 150;
v5 = 0;
if ( v3 - 150 >= -23 )
{
if ( v4 > 7 )
{
*v2 = 34;
return ((unsigned int)v1 >> 31) + 0x7FFFFFFF;
}
v6 = v1 & 0x7FFFFF | 0x800000;
if ( v4 < 0 )
{
v6 >>= -106 - v3;
}
else
{
if ( v4 > 0 )
v6 <<= v3 + 106;
if ( v3 == 157 )
{
if ( v1 < 0 )
return -v6;
return v6;
}
}
if ( v1 < 0 )
return -v6;
return v6;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(__m128i a1)
{
int v1; // ebx
int *v2; // rax
int v3; // edx
int v4; // ecx
unsigned int v5; // esi
unsigned int v6; // ebp

v1 = _mm_cvtsi128_si32(a1);
v2 = __errno_location();
*v2 = 0;
v3 = (unsigned __int8)((unsigned int)v1 >> 23);
v4 = v3 - 150;
v5 = 0;
if ( v3 - 150 >= -23 )
{
if ( v4 > 7 )
{
*v2 = 34;
return ((unsigned int)v1 >> 31) + 0x7FFFFFFF;
}
v6 = v1 & 0x7FFFFF | 0x800000;
if ( v4 < 0 )
{
v6 >>= -106 - v3;
}
else
{
if ( v4 > 0 )
v6 <<= v3 + 106;
if ( v3 == 157 )
{
if ( v1 < 0 )
return -v6;
return v6;
}
}
if ( v1 < 0 )
return -v6;
return v6;
}
return v5;
}

original_code:
int float2int(const float value) {
 const uint32_t INT32_MIN_POSITIVE = 2147483648;
 IEEE745_float number = {.value = value};
 (*__errno_location ()) = 0;
 const int shift = number.fields.exp_bias - IEEE745_FLOAT_BIAS - 23;
 if (shift < -23) {
  return 0;
 }
 if (shift > 7) {
  (*__errno_location ()) = 34;
  return number.fields.sign ? (-2147483647-1) : (2147483647);
 }
 uint32_t integer = number.fields.fraction | (1 << 23);
 if (shift < 0)
  integer >>= -shift;
 else if (shift > 0)
  integer <<= shift;
 if (shift == 7) {
  if (number.fields.sign) {
   if (integer > INT32_MIN_POSITIVE) {
    (*__errno_location ()) = 34;
    return (-2147483647-1);
   }
  } else {
   if (integer > (2147483647)) {
    (*__errno_location ()) = 34;
    return (2147483647);
   }
  }
 }
 return (number.fields.sign) ? -integer : integer;
}

canonical_original_code:
int float2int(float value) {
 unsigned int INT32_MIN_POSITIVE = 2147483648;
 union IEEE745_float number = {.value = value};
 (*__errno_location ()) = 0;
 int shift = number.fields.exp_bias - IEEE745_FLOAT_BIAS - 23;
 if (shift < -23) {
  return 0;
 }
 if (shift > 7) {
  (*__errno_location ()) = 34;
  return number.fields.sign ? (-2147483647-1) : (2147483647);
 }
 unsigned int integer = number.fields.fraction | (1 << 23);
 if (shift < 0)
  integer >>= -shift;
 else if (shift > 0)
  integer <<= shift;
 if (shift == 7) {
  if (number.fields.sign) {
   if (integer > INT32_MIN_POSITIVE) {
    (*__errno_location ()) = 34;
    return (-2147483647-1);
   }
  } else {
   if (integer > (2147483647)) {
    (*__errno_location ()) = 34;
    return (2147483647);
   }
  }
 }
 return (number.fields.sign) ? -integer : integer;
}

variable_types: value float

variable_types: union IEEE745_float { struct { unsigned int fraction; unsigned int exp_bias; unsigned int sign; } fields; unsigned int dword; float value; }

variable_types: shift int

variable_types:
integer
unsigned int


repo: WojciechMula/toys

decompiled_code:
int __fastcall print_double_params(double a1)
{
printf("sign = %d\n", a1 < 0.0);
printf("exponent = %d\n", (unsigned int)((*(_QWORD *)&a1 >> 52) & 0x7FF) - 1023);
return printf("fraction [hex] = %013llx\n", *(_QWORD *)&a1 & 0xFFFFFFFFFFFFFLL);
}

canonical_decompiled_code:
int __fastcall func7(double a1)
{
printf("sign = %d\n", a1 < 0.0);
printf("exponent = %d\n", (unsigned int)((*(_QWORD *)&a1 >> 52) & 0x7FF) - 1023);
return printf("fraction [hex] = %013llx\n", *(_QWORD *)&a1 & 0xFFFFFFFFFFFFFLL);
}

original_code:
void print_double_params(const double x) {
 IEEE754_double number = {.value = x};
 printf("sign           = %d\n", number.fields.sign);
 printf("exponent       = %d\n", number.fields.exp_bias - IEEE754_DOUBLE_BIAS);
 printf("fraction [hex] = %013llx\n", number.fields.fraction);
}

canonical_original_code:
void print_double_params(double x) {
 union IEEE754_double number = {.value = x};
 printf("sign           = %d\n", number.fields.sign);
 printf("exponent       = %d\n", number.fields.exp_bias - IEEE754_DOUBLE_BIAS);
 printf("fraction [hex] = %013llx\n", number.fields.fraction);
}

variable_types: x double

variable_types: union IEEE754_double { struct { unsigned long fraction; unsigned long exp_bias; unsigned long sign; } fields; unsigned long qword; double value; }

return_type: void


decompiled_code:
int __fastcall print_bin(__int64 a1, int a2, unsigned int a3)
{
unsigned __int64 i; // rbx
int result; // eax

for ( i = a3; a2 <= (int)i; i = (unsigned int)(i - 1) )
{
if ( _bittest64(&a1, i) )
result = putchar(49);
else
result = putchar(48);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, int a2, unsigned int a3)
{
unsigned __int64 i; // rbx
int result; // eax

for ( i = a3; a2 <= (int)i; i = (unsigned int)(i - 1) )
{
if ( _bittest64(&a1, i) )
result = putchar(49);
else
result = putchar(48);
}
return result;
}

original_code:
void print_bin(const uint64_t value, int minbits, int maxbits) {
 for (int bit=maxbits; bit >= minbits; bit--) {
  if (value & ((uint64_t)1 << bit)) {
   putchar('1');
  } else {
   putchar('0');
  }
 }
}

canonical_original_code:
void print_bin(unsigned long value, int minbits, int maxbits) {
 for (int bit = maxbits; bit >= minbits; bit--) {
  if (value & ((unsigned long)1 << bit)) {
   putchar('1');
  } else {
   putchar('0');
  }
 }
}

variable_types:
value
unsigned long

variable_types: minbits int

variable_types: maxbits int

variable_types: bit int


decompiled_code:
int __fastcall repeat_char(char a1, int a2)
{
int i; // ebx
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
result = putchar(a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char a1, int a2)
{
int i; // ebx
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
result = putchar(a1);
}
return result;
}

original_code:
void repeat_char(const char c, const int repeat) {
 for (int i=0; i < repeat; i++)
  putchar(c);
}

canonical_original_code:
void repeat_char(char c, int repeat) {
 for (int i = 0; i < repeat; i++)
  putchar(c);
}

variable_types: c char

variable_types: repeat int

variable_types: i int


decompiled_code:
int __fastcall print_double(double a1)
{
putchar(43);
repeat_char(45, 1);
putchar(43);
repeat_char(45, 11);
putchar(43);
repeat_char(45, 52);
putchar(43);
putchar(10);
putchar(124);
print_bin(*(__int64 *)&a1, 63, 0x3Fu);
putchar(124);
print_bin(*(__int64 *)&a1, 52, 0x3Eu);
putchar(124);
print_bin(*(__int64 *)&a1, 0, 0x33u);
putchar(124);
putchar(10);
putchar(43);
repeat_char(45, 1);
putchar(43);
repeat_char(45, 11);
putchar(43);
repeat_char(45, 52);
putchar(43);
putchar(10);
printf("63");
repeat_char(32, 1);
printf("62");
repeat_char(32, 7);
printf("52");
repeat_char(32, 1);
printf("51");
repeat_char(32, 50);
putchar(48);
return putchar(10);
}

original_code:
void print_double(const double x) {
 IEEE754_double number = {.value = x};
 putchar('+');
 repeat_char('-', 1);
 putchar('+');
 repeat_char('-', 11);
 putchar('+');
 repeat_char('-', 52);
 putchar('+');
 putchar('\n');
 putchar('|');
  print_bin(number.qword, 63, 63);
 putchar('|');
  print_bin(number.qword, 52, 62);
 putchar('|');
  print_bin(number.qword, 0, 51);
 putchar('|');
 putchar('\n');
 putchar('+');
 repeat_char('-', 1);
 putchar('+');
 repeat_char('-', 11);
 putchar('+');
 repeat_char('-', 52);
 putchar('+');
 putchar('\n');
 printf("63");
 repeat_char(' ', 1);
 printf("62");
 repeat_char(' ', 7);
 printf("52");
 repeat_char(' ', 1);
 printf("51");
 repeat_char(' ', 50);
 printf("0");
 putchar('\n');
}

canonical_original_code:
void print_double(double x) {
 union IEEE754_double number = {.value = x};
 putchar('+');
 repeat_char('-', 1);
 putchar('+');
 repeat_char('-', 11);
 putchar('+');
 repeat_char('-', 52);
 putchar('+');
 putchar('\n');
 putchar('|');
  print_bin(number.qword, 63, 63);
 putchar('|');
  print_bin(number.qword, 52, 62);
 putchar('|');
  print_bin(number.qword, 0, 51);
 putchar('|');
 putchar('\n');
 putchar('+');
 repeat_char('-', 1);
 putchar('+');
 repeat_char('-', 11);
 putchar('+');
 repeat_char('-', 52);
 putchar('+');
 putchar('\n');
 printf("63");
 repeat_char(' ', 1);
 printf("62");
 repeat_char(' ', 7);
 printf("52");
 repeat_char(' ', 1);
 printf("51");
 repeat_char(' ', 50);
 printf("0");
 putchar('\n');
}

user_defined_types: union IEEE754_double { struct { unsigned long fraction; unsigned long exp_bias; unsigned long sign; } fields; unsigned long qword; double value; }


main

canonical_name: main

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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
v4 = atof(*v3);
print_double_params(v4);
print_double(v4);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
v4 = atof(*v3);
func7(v4);
func10(v4);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 for (int i=1; i < argc; i++) {
  const double value = atof(argv[i]);
  print_double_params(value);
  print_double(value);
 }
}

canonical_original_code:
int main(int argc, char *argv[]) {
 for (int i = 1; i < argc; i++) {
  double value = atof(argv[i]);
  print_double_params(value);
  print_double(value);
 }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: value double

return_type: int


pext

decompiled_code:
__int64 __fastcall pext(int a1, int a2)
{
int v2; // edx
unsigned int v3; // ecx
int v4; // eax

v2 = 1;
v3 = 0;
v4 = a2 & -a2;
if ( !v4 )
return 0;
do
{
a2 &= ~v4;
v3 |= v2 & (-(a1 & v4) >> 31);
v2 *= 2;
v4 = a2 & -a2;
}
while ( v4 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
int v2; // edx
unsigned int v3; // ecx
int v4; // eax

v2 = 1;
v3 = 0;
v4 = a2 & -a2;
if ( !v4 )
return 0;
do
{
a2 &= ~v4;
v3 |= v2 & (-(a1 & v4) >> 31);
v2 *= 2;
v4 = a2 & -a2;
}
while ( v4 );
return v3;
}

original_code:
uint32_t pext(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    uint32_t bit = 1;
    while (1) {
        const uint32_t LSB = (-mask & mask);
        if (LSB == 0) {
            return result;
        }
        mask &= ~LSB;
        const uint32_t value = src & LSB;
        const uint32_t mv = ((int32_t)(-value)) >> 31;
        result |= mv & bit;
        bit <<= 1;
    }
    return result;
}

canonical_original_code:
unsigned int pext(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    unsigned int bit = 1;
    while (1) {
        unsigned int LSB = (-mask & mask);
        if (LSB == 0) {
            return result;
        }
        mask &= ~LSB;
        unsigned int value = src & LSB;
        unsigned int mv = ((int32_t)(-value)) >> 31;
        result |= mv & bit;
        bit <<= 1;
    }
    return result;
}

variable_types:
src
unsigned int

variable_types:
mask
unsigned int

variable_types:
result
unsigned int

variable_types:
bit
unsigned int

variable_types:
LSB
unsigned int

variable_types:
value
unsigned int

variable_types:
mv
unsigned int

return_type: unsigned int


decompiled_code:
bool __fastcall get_bit(int a1, char a2)
{
return (a1 & (1 << a2)) != 0;
}

canonical_decompiled_code:
bool __fastcall func8(int a1, char a2)
{
return (a1 & (1 << a2)) != 0;
}

original_code:
char get_bit(uint32_t v, int bit) {
    const uint32_t mask = ((uint32_t)(1) << bit);
    return (v & mask) != 0;
}

canonical_original_code:
char get_bit(unsigned int v, int bit) {
    unsigned int mask = ((uint32_t)(1) << bit);
    return (v & mask) != 0;
}

variable_types:
v
unsigned int

return_type: char


decompiled_code:
__int64 __fastcall assign_bit(int a1, char a2, int a3)
{
__int64 result; // rax

result = a1 & (unsigned int)~(1 << a2);
if ( a3 )
result = a1 | (unsigned int)(1 << a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, char a2, int a3)
{
__int64 result; // rax

result = a1 & (unsigned int)~(1 << a2);
if ( a3 )
result = a1 | (unsigned int)(1 << a2);
return result;
}

original_code:
uint32_t assign_bit(uint32_t v, int bit, int value) {
    const uint32_t mask = ((uint32_t)(1) << bit);
    if (value) {
        return v | mask;
    } else {
        return v & ~mask;
    }
}

canonical_original_code:
unsigned int assign_bit(unsigned int v, int bit, int value) {
    unsigned int mask = ((uint32_t)(1) << bit);
    if (value) {
        return v | mask;
    } else {
        return v & ~mask;
    }
}

variable_types: value int


decompiled_code:
__int64 __fastcall pext_naive(int a1, int a2)
{
int v2; // ebx
int v3; // er12
unsigned int v4; // ebp
bool v5; // al

v2 = 0;
v3 = 0;
v4 = 0;
do
{
if ( get_bit(a2, v2) )
{
v5 = get_bit(a1, v2);
v4 = assign_bit(v4, v3++, v5);
}
++v2;
}
while ( v2 != 32 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
int v2; // ebx
int v3; // er12
unsigned int v4; // ebp
bool v5; // al

v2 = 0;
v3 = 0;
v4 = 0;
do
{
if ( func8(a2, v2) )
{
v5 = func8(a1, v2);
v4 = func9(v4, v3++, v5);
}
++v2;
}
while ( v2 != 32 );
return v4;
}

original_code:
uint32_t pext_naive(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    int k = 0;
    for (int i=0; i < 32; i++) {
        const char bit = get_bit(mask, i);
        if (bit) {
            result = assign_bit(result, k, get_bit(src, i));
            k += 1;
        }
    }
    return result;
}

canonical_original_code:
unsigned int pext_naive(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    int k = 0;
    for (int i = 0; i < 32; i++) {
        char bit = get_bit(mask, i);
        if (bit) {
            result = assign_bit(result, k, get_bit(src, i));
            k += 1;
        }
    }
    return result;
}

variable_types: k int

variable_types: bit char


original_code:
double sin_taylor5(const double x) {
 const double x2 = x*x;
 const double x3 = x*x2;
 const double x5 = x3*x2;
 const double x7 = x5*x2;
 const double x9 = x7*x2;
 return x - x3*(1.0/6) + x5*(1.0/120) - x7*(1.0/5040) + x9*(1.0/362880);
}

variable_types: x double

return_type: double


original_code:
double sin_taylor4(const double x) {
 const double x2 = x*x;
 const double x3 = x*x2;
 const double x5 = x3*x2;
 const double x7 = x5*x2;
 return x - x3*(1.0/6) + x5*(1.0/120) - x7*(1.0/5040);
}

canonical_original_code:
double sin_taylor4(double x) {
 double x2 = x*x;
 double x3 = x*x2;
 double x5 = x3*x2;
 double x7 = x5*x2;
 return x - x3*(1.0/6) + x5*(1.0/120) - x7*(1.0/5040);
}


original_code:
double sin_taylor3(const double x) {
 const double x2 = x*x;
 const double x3 = x*x2;
 const double x5 = x3*x2;
 return x - x3*(1.0/6) + x5*(1.0/120);
}

canonical_original_code:
double sin_taylor3(double x) {
 double x2 = x*x;
 double x3 = x*x2;
 double x5 = x3*x2;
 return x - x3*(1.0/6) + x5*(1.0/120);
}


mesure

variable_types:
msg
char *

variable_types: tref double

variable_types:
function
double (*)(double x)

variable_types: t double


decompiled_code:
__int64 get_time()
{
gettimeofday(&T_0, 0LL);
return (unsigned int)(LODWORD(T_0.tv_usec) + 1000000 * LODWORD(T_0.tv_sec));
}

canonical_decompiled_code:
__int64 func10()
{
gettimeofday(&T_0, 0LL);
return (unsigned int)(LODWORD(T_0.tv_usec) + 1000000 * LODWORD(T_0.tv_sec));
}

original_code:
uint32_t get_time(void) {
 static struct timeval T;
 gettimeofday(&T, ((void *)0));
 return (T.tv_sec * 1000000) + T.tv_usec;
}

canonical_original_code:
unsigned int get_time() {
 static struct timeval T;
 gettimeofday(&T, ((void *)0));
 return (T.tv_sec * 1000000) + T.tv_usec;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
double strstr_wrapper()
{
int v0; // ebx

v0 = get_time();
return (double)(int)(get_time() - v0) / 1000000.0;
}

canonical_decompiled_code:
double func8()
{
int v0; // ebx

v0 = func7();
return (double)(int)(func7() - v0) / 1000000.0;
}

original_code:
double strstr_wrapper(char* data, char* substr) {
 uint32_t t1 = get_time();
 char* c = strstr(data, substr);
 uint32_t t2 = get_time();
 return (t2 - t1)/1000000.0;
}

canonical_original_code:
double strstr_wrapper(char *data, char *substr) {
 unsigned int t1 = get_time();
 char *c = strstr(data, substr);
 unsigned int t2 = get_time();
 return (t2 - t1)/1000000.0;
}

variable_types:
data
char *

variable_types:
substr
char *

variable_types:
c
char *


canonical_decompiled_code:
__int64 func7()
{
gettimeofday(&T_0, 0LL);
return (unsigned int)(LODWORD(T_0.tv_usec) + 1000000 * LODWORD(T_0.tv_sec));
}

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
_BYTE *v4; // rax
_BYTE *v5; // rbp
const char **v6; // rbx
double v7; // xmm0_8

result = 0;
if ( argc != 1 )
{
v4 = malloc(0x1000000uLL);
v5 = v4;
if ( v4 )
{
memset(v4, 63, 0x1000000uLL);
v5[0xFFFFFF] = 0;
if ( argc > 1 )
{
v6 = argv + 1;
do
{
printf("searching string '%s'... ", *v6);
fflush(stdout);
v7 = strstr_wrapper();
printf("time: %0.3f\n", v7);
++v6;
}
while ( v6 != &argv[(unsigned int)(argc - 2) + 2] );
}
free(v5);
result = 0;
}
else
{
result = 1;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
_BYTE *v4; // rax
_BYTE *v5; // rbp
const char **v6; // rbx
double v7; // xmm0_8

result = 0;
if ( argc != 1 )
{
v4 = malloc(0x1000000uLL);
v5 = v4;
if ( v4 )
{
memset(v4, 63, 0x1000000uLL);
v5[0xFFFFFF] = 0;
if ( argc > 1 )
{
v6 = argv + 1;
do
{
printf("searching string '%s'... ", *v6);
fflush(stdout);
v7 = func8();
printf("time: %0.3f\n", v7);
++v6;
}
while ( v6 != &argv[(unsigned int)(argc - 2) + 2] );
}
free(v5);
result = 0;
}
else
{
result = 1;
}
}
return result;
}

original_code:
int main(int argc, char* argv[]) {
 if (argc == 1)
  return 0;
 const uint32_t MiB = 1024*1024;
 const uint32_t size = 16*MiB;
 char* buffer = (char*)malloc(size);
 if (buffer == ((void *)0))
  return 1;
 memset(buffer, '?', size);
 buffer[size - 1] = '\0';
 for (int i=1; i < argc; i++) {
  printf("searching string '%s'... ", argv[i]);
  fflush(stdout);
  const double dt = strstr_wrapper(buffer, argv[i]);
  printf("time: %0.3f\n", dt);
 }
 free(buffer);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 if (argc == 1)
  return 0;
 unsigned int MiB = 1024*1024;
 unsigned int size = 16*MiB;
 char *buffer = (char*)malloc(size);
 if (buffer == ((void *)0))
  return 1;
 memset(buffer, '?', size);
 buffer[size - 1] = '\0';
 for (int i = 1; i < argc; i++) {
  printf("searching string '%s'... ", argv[i]);
  fflush(stdout);
  double dt = strstr_wrapper(buffer, argv[i]);
  printf("time: %0.3f\n", dt);
 }
 free(buffer);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
MiB
unsigned int

variable_types:
size
unsigned int

variable_types:
buffer
char *

variable_types: i int

variable_types: dt double


decompiled_code:
char *__fastcall invoke_fbstp(double a1)
{
char *result; // rax

result = (char *)&buffer;
buffer = __FBSTP__(a1);
return result;
}

canonical_decompiled_code:
char *__fastcall func7(double a1)
{
char *result; // rax

result = (char *)&buffer;
buffer = __FBSTP__(a1);
return result;
}

original_code:
void invoke_fbstp(const double x) {
 __asm__ (
  "fbstp (%1)\n"
  :
  : "f" (x), "r" (buffer)
  : "memory"
 );
}

canonical_original_code:
void invoke_fbstp(double x) {
 __asm__ (
  "fbstp (%1)\n"
  :
  : "f" (x), "r" (buffer)
  : "memory"
 );
}


decompiled_code:
__int64 __fastcall simple_itoa(int a1)
{
int v1; // edx
__int64 result; // rax

if ( a1 > 0 )
{
do
{
v1 = a1;
a1 /= 10;
}
while ( v1 > 9 );
result = (unsigned int)(10 * (v1 / 10));
LOBYTE(buffer) = v1 % 10 + 48;
}
return result;
}

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

if ( a1 > 0 )
{
do
{
v1 = a1;
a1 /= 10;
}
while ( v1 > 9 );
result = (unsigned int)(10 * (v1 / 10));
LOBYTE(buffer) = v1 % 10 + 48;
}
return result;
}

original_code:
void simple_itoa(int x) {
 int i = 0;
 while (x > 0) {
  buffer[i] = x % 10 + '0';
  x /= 10;
 }
}

variable_types: x int


decompiled_code:
__int64 get_time()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (unsigned int)(LODWORD(v1.tv_usec) + 1000000 * LODWORD(v1.tv_sec));
}

canonical_decompiled_code:
__int64 func9()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (unsigned int)(LODWORD(v1.tv_usec) + 1000000 * LODWORD(v1.tv_sec));
}

original_code:
uint32_t get_time(void) {
 struct timeval T;
 gettimeofday(&T, ((void *)0));
 return (T.tv_sec * 1000000) + T.tv_usec;
}

canonical_original_code:
unsigned int get_time() {
 struct timeval T;
 gettimeofday(&T, ((void *)0));
 return (T.tv_sec * 1000000) + T.tv_usec;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rdi
int v4; // ebp
int i; // ebx
double v6; // r14
FILE *v7; // rdi
int v8; // ebp
unsigned int j; // ebx
__int64 v10; // rdi
double v12; // [rsp+8h] [rbp-20h]

printf("FBSTP [");
v3 = _bss_start;
fflush(_bss_start);
v4 = get_time(v3, argv);
for ( i = 1; i != 10000000; ++i )
invoke_fbstp((double)i);
v6 = (double)(int)(get_time(v3, argv) - v4) / 1000000.0;
printf("%0.3f]", v6);
fflush(_bss_start);
printf(" simple itoa [");
v7 = _bss_start;
fflush(_bss_start);
v8 = get_time(v7, argv);
for ( j = 1; j != 10000000; ++j )
{
v10 = j;
simple_itoa(j);
}
v12 = (double)(int)(get_time(v10, argv) - v8) / 1000000.0;
printf("%0.3f]\n", v12);
fflush(_bss_start);
puts("");
printf("FBST is %.2f slower than simple itoa\n", v6 / v12);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rdi
int v4; // ebp
int i; // ebx
double v6; // r14
FILE *v7; // rdi
int v8; // ebp
unsigned int j; // ebx
__int64 v10; // rdi
double v12; // [rsp+8h] [rbp-20h]

printf("FBSTP [");
v3 = _bss_start;
fflush(_bss_start);
v4 = func9(v3, argv);
for ( i = 1; i != 10000000; ++i )
func7((double)i);
v6 = (double)(int)(func9(v3, argv) - v4) / 1000000.0;
printf("%0.3f]", v6);
fflush(_bss_start);
printf(" simple itoa [");
v7 = _bss_start;
fflush(_bss_start);
v8 = func9(v7, argv);
for ( j = 1; j != 10000000; ++j )
{
v10 = j;
func8(j);
}
v12 = (double)(int)(func9(v10, argv) - v8) / 1000000.0;
printf("%0.3f]\n", v12);
fflush(_bss_start);
puts("");
printf("FBST is %.2f slower than simple itoa\n", v6 / v12);
return 0;
}

original_code:
int main() {
 int i;
 uint32_t t1, t2;
    double fbstp_time, naive_time;
 const int max = 10000000;
 printf("FBSTP [");
    fflush(stdout);
 t1 = get_time();
 for (i=1; i < max; i++) {
  invoke_fbstp(i);
 }
 t2 = get_time();
    fbstp_time = (t2 - t1)/1000000.0;
 printf("%0.3f]", fbstp_time);
    fflush(stdout);
 printf(" simple itoa [");
    fflush(stdout);
 t1 = get_time();
 for (i=1; i < max; i++) {
  simple_itoa(i);
 }
 t2 = get_time();
    naive_time = (t2 - t1)/1000000.0;
 printf("%0.3f]\n", naive_time);
    fflush(stdout);
    puts("");
    printf("FBST is %.2f slower than simple itoa\n", fbstp_time / naive_time);
 return 0;
}

canonical_original_code:
int main() {
 int i;
 unsigned int t1; unsigned int t2;
    double fbstp_time; double naive_time;
 int max = 10000000;
 printf("FBSTP [");
    fflush(stdout);
 t1 = get_time();
 for (i=1; i < max; i++) {
  invoke_fbstp(i);
 }
 t2 = get_time();
    fbstp_time = (t2 - t1)/1000000.0;
 printf("%0.3f]", fbstp_time);
    fflush(stdout);
 printf(" simple itoa [");
    fflush(stdout);
 t1 = get_time();
 for (i=1; i < max; i++) {
  simple_itoa(i);
 }
 t2 = get_time();
    naive_time = (t2 - t1)/1000000.0;
 printf("%0.3f]\n", naive_time);
    fflush(stdout);
    puts("");
    printf("FBST is %.2f slower than simple itoa\n", fbstp_time / naive_time);
 return 0;
}

variable_types: max int


decompiled_code:
__int64 get_mxcsr()
{
_mm_getcsr();
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
_mm_getcsr();
return 0LL;
}

original_code:
uint32_t get_mxcsr() {
 uint32_t result = 0;
 __asm__ __volatile__("stmxcsr %0": :"m" (result));
 return result;
}

canonical_original_code:
unsigned int get_mxcsr() {
 unsigned int result = 0;
 __asm__ __volatile__("stmxcsr %0": :"m" (result));
 return result;
}

variable_types:
result
unsigned int


decompiled_code:
int *__fastcall set_mxcsr(unsigned int a1)
{
int *result; // rax
int v2; // [rsp+0h] [rbp-4h] BYREF

result = &v2;
_mm_setcsr(a1);
return result;
}

canonical_decompiled_code:
int *__fastcall func8(unsigned int a1)
{
int *result; // rax
int v2; // [rsp+0h] [rbp-4h] BYREF

result = &v2;
_mm_setcsr(a1);
return result;
}

original_code:
void set_mxcsr(uint32_t mxcsr) {
 __asm__ __volatile__("ldmxcsr (%0)"
  :
  :"g" (&mxcsr)
  : "memory"
 );
}

canonical_original_code:
void set_mxcsr(unsigned int mxcsr) {
 __asm__ __volatile__("ldmxcsr (%0)"
  :
  :"g" (&mxcsr)
  : "memory"
 );
}

variable_types:
mxcsr
unsigned int


decompiled_code:
int dump_mxcsr()
{
unsigned int v0; // eax

v0 = get_mxcsr();
return print_mxcsr(_bss_start, v0);
}

canonical_decompiled_code:
int func11()
{
unsigned int v0; // eax

v0 = func7();
return func10(_bss_start, v0);
}

original_code:
void dump_mxcsr() {
 print_mxcsr(stdout, get_mxcsr());
}


decompiled_code:
const char *__fastcall fpclassifyf_str(float a1)
{
float v2; // xmm0_4
const char *result; // rax

v2 = fabs(a1);
if ( v2 > 3.4028235e38 )
return "infinite";
result = "normalized";
if ( v2 < 1.1754944e-38 )
{
if ( a1 == 0.0 )
result = "zero";
else
result = (const char *)&unk_40211C;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func13(float a1)
{
float v2; // xmm0_4
const char *result; // rax

v2 = fabs(a1);
if ( v2 > 3.4028235e38 )
return "infinite";
result = "normalized";
if ( v2 < 1.1754944e-38 )
{
if ( a1 == 0.0 )
result = "zero";
else
result = (const char *)&unk_40211C;
}
return result;
}

original_code:
const char* fpclassifyf_str(const float x) {
 switch (__builtin_fpclassify (0, 1, 4, 3, 2, x)) {
  case 0:
   return "NaN";
  case 1:
   return "infinite";
  case 2:
   return "zero";
  case 3:
   return "denormalized";
  case 4:
   return "normalized";
  default:
   return "<unknown>";
 }
}

canonical_original_code:
char *fpclassifyf_str(float x) {
 switch (__builtin_fpclassify (0, 1, 4, 3, 2, x)) {
  case 0:
   return "NaN";
  case 1:
   return "infinite";
  case 2:
   return "zero";
  case 3:
   return "denormalized";
  case 4:
   return "normalized";
  default:
   return "<unknown>";
 }
}

variable_types: x float

return_type: char *


decompiled_code:
void mulps_in_loop()
{
__int64 v0; // rcx
__m128 v1; // xmm1

v0 = 10000000LL;
do
{
v1 = _mm_mul_ps((__m128)min_floats, (__m128)min_floats);
--v0;
}
while ( v0 );
final_value = (__int128)v1;
}

original_code:
void mulps_in_loop() {
 const int32_t iterations = 10000000;
 uint32_t dummy;
 __asm__ __volatile__(
  "movups 	%2, %%xmm0\n"
  "1:\n"
  "movaps 	%%xmm0, %%xmm1\n"
  "mulps 		%%xmm1, %%xmm1\n"
  "loop 		1b\n"
  "movups		%%xmm1, %3\n"
  : "=c" (dummy)
  : "c" (iterations)
  , "m" (min_floats)
  , "m" (final_value)
  : "xmm0", "xmm1"
 );
}

canonical_original_code:
void mulps_in_loop() {
 int iterations = 10000000;
 unsigned int dummy;
 __asm__ __volatile__(
  "movups 	%2, %%xmm0\n"
  "1:\n"
  "movaps 	%%xmm0, %%xmm1\n"
  "mulps 		%%xmm1, %%xmm1\n"
  "loop 		1b\n"
  "movups		%%xmm1, %3\n"
  : "=c" (dummy)
  : "c" (iterations)
  , "m" (min_floats)
  , "m" (final_value)
  : "xmm0", "xmm1"
 );
}

variable_types: iterations int

variable_types:
dummy
unsigned int


bswap

decompiled_code:
__int64 __fastcall bswap(unsigned int a1)
{
return _byteswap_ulong(a1);
}

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

original_code:
uint32_t bswap(uint32_t x) {
    uint32_t result;
    __asm__ __volatile__("bswap %0" : "=&r" (result) : "0" (x));
    return result;
}

canonical_original_code:
unsigned int bswap(unsigned int x) {
    unsigned int result;
    __asm__ __volatile__("bswap %0" : "=&r" (result) : "0" (x));
    return result;
}

variable_types:
x
unsigned int


pext

decompiled_code:
__int64 __fastcall pext(int a1, int a2)
{
int v2; // edx
unsigned int v3; // ecx
int v4; // eax

v2 = 1;
v3 = 0;
v4 = a2 & -a2;
if ( !v4 )
return 0;
do
{
a2 &= ~v4;
v3 |= v2 & (-(a1 & v4) >> 31);
v2 *= 2;
v4 = a2 & -a2;
}
while ( v4 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
int v2; // edx
unsigned int v3; // ecx
int v4; // eax

v2 = 1;
v3 = 0;
v4 = a2 & -a2;
if ( !v4 )
return 0;
do
{
a2 &= ~v4;
v3 |= v2 & (-(a1 & v4) >> 31);
v2 *= 2;
v4 = a2 & -a2;
}
while ( v4 );
return v3;
}

original_code:
uint32_t pext(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    uint32_t bit = 1;
    while (1) {
        const uint32_t LSB = (-mask & mask);
        if (LSB == 0) {
            return result;
        }
        mask &= ~LSB;
        const uint32_t value = src & LSB;
        const uint32_t mv = ((int32_t)(-value)) >> 31;
        result |= mv & bit;
        bit <<= 1;
    }
    return result;
}

canonical_original_code:
unsigned int pext(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    unsigned int bit = 1;
    while (1) {
        unsigned int LSB = (-mask & mask);
        if (LSB == 0) {
            return result;
        }
        mask &= ~LSB;
        unsigned int value = src & LSB;
        unsigned int mv = ((int32_t)(-value)) >> 31;
        result |= mv & bit;
        bit <<= 1;
    }
    return result;
}

variable_types:
src
unsigned int

variable_types:
mask
unsigned int

variable_types:
bit
unsigned int

variable_types:
LSB
unsigned int

variable_types:
value
unsigned int

variable_types:
mv
unsigned int


decompiled_code:
__int64 __fastcall nibble_from_string(unsigned int *a1)
{
unsigned int v1; // edi
int v2; // eax

v1 = *a1;
if ( (v1 & 0xCECECECE) != 0 )
return 0xFFFFFFFFLL;
v2 = bswap(v1);
return pext(v2, 16843009);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1)
{
unsigned int v1; // edi
int v2; // eax

v1 = *a1;
if ( (v1 & 0xCECECECE) != 0 )
return 0xFFFFFFFFLL;
v2 = func7(v1);
return func8(v2, 16843009);
}

original_code:
int nibble_from_string(char* string) {
    const uint32_t chars = *((uint32_t*)string);
    if ((chars & ((uint8_t)(~0x31) * 0x01010101u)) != 0) {
        return -1;
    }
    return pext(bswap(chars), 0x01010101);
}

canonical_original_code:
int nibble_from_string(char *string) {
    unsigned int chars = *((uint32_t*)string);
    if ((chars & ((unsigned char)(~0x31) * 0x01010101u)) != 0) {
        return -1;
    }
    return pext(bswap(chars), 0x01010101);
}

variable_types:
string
char *

variable_types:
chars
unsigned int


decompiled_code:
char __fastcall to_bin_naive(int a1, _BYTE *a2)
{
_BYTE *v2; // rdx
int i; // ecx
char result; // al

v2 = a2;
for ( i = 3; i != -1; --i )
{
result = 48 - (((a1 & (1 << i)) == 0) - 1);
*v2++ = result;
}
a2[4] = 0;
return result;
}

canonical_decompiled_code:
char __fastcall func10(int a1, _BYTE *a2)
{
_BYTE *v2; // rdx
int i; // ecx
char result; // al

v2 = a2;
for ( i = 3; i != -1; --i )
{
result = 48 - (((a1 & (1 << i)) == 0) - 1);
*v2++ = result;
}
a2[4] = 0;
return result;
}

original_code:
void to_bin_naive(uint32_t x, char* c) {
    for (int i=3; i >= 0; i--, c++) {
        const uint32_t bit = (1 << i);
        *c = (x & bit) ? '1' : '0';
    }
    *c = 0;
}

canonical_original_code:
void to_bin_naive(unsigned int x, char *c) {
    for (int i = 3; i >= 0; i--, c++) {
        unsigned int bit = (1 << i);
        *c = (x & bit) ? '1' : '0';
    }
    *c = 0;
}

variable_types:
c
char *

variable_types: i int


decompiled_code:
void *__fastcall fill_buffer(double a1)
{
void *result; // rax

result = &buffer;
*(long double *)(unsigned int)&buffer = __FBSTP__(a1);
return result;
}

canonical_decompiled_code:
void *__fastcall func7(double a1)
{
void *result; // rax

result = &buffer;
*(long double *)(unsigned int)&buffer = __FBSTP__(a1);
return result;
}

original_code:
void fill_buffer(const double x) {
 __asm__ (
  "fbstp (%%eax)\n"
  :
  : "f" (x), "a" (buffer)
  : "memory"
 );
}

canonical_original_code:
void fill_buffer(double x) {
 __asm__ (
  "fbstp (%%eax)\n"
  :
  : "f" (x), "a" (buffer)
  : "memory"
 );
}

variable_types: x double


decompiled_code:
int print_buffer()
{
unsigned __int8 *v0; // rbx
char v1; // bp
int result; // eax
int v3; // er12
unsigned __int8 v4; // al
int v5; // er15

if ( byte_404051 )
{
if ( byte_404051 != (char)0x80 )
return printf("NaN/overflow");
putchar(45);
}
v0 = (unsigned __int8 *)&unk_404050;
v1 = 0;
while ( 1 )
{
v3 = *v0 & 0xF;
v4 = *v0 >> 4;
v5 = v4;
if ( v1 || v4 )
{
printf("%d", v4);
if ( v5 > 0 )
v1 = 1;
printf("%d", (unsigned int)v3);
if ( v3 > 0 )
v1 = 1;
}
else if ( (*v0 & 0xF) != 0 )
{
printf("%d", *v0 & 0xF);
v1 = 1;
}
else
{
v1 = 0;
}
result = (_DWORD)v0 - 1;
if ( &buffer == (_UNKNOWN *)v0 )
break;
--v0;
}
if ( !v1 )
result = putchar(48);
return result;
}

canonical_decompiled_code:
int func8()
{
unsigned __int8 *v0; // rbx
char v1; // bp
int result; // eax
int v3; // er12
unsigned __int8 v4; // al
int v5; // er15

if ( byte_404051 )
{
if ( byte_404051 != (char)0x80 )
return printf("NaN/overflow");
putchar(45);
}
v0 = (unsigned __int8 *)&unk_404050;
v1 = 0;
while ( 1 )
{
v3 = *v0 & 0xF;
v4 = *v0 >> 4;
v5 = v4;
if ( v1 || v4 )
{
printf("%d", v4);
if ( v5 > 0 )
v1 = 1;
printf("%d", (unsigned int)v3);
if ( v3 > 0 )
v1 = 1;
}
else if ( (*v0 & 0xF) != 0 )
{
printf("%d", *v0 & 0xF);
v1 = 1;
}
else
{
v1 = 0;
}
result = (_DWORD)v0 - 1;
if ( &buffer == (_UNKNOWN *)v0 )
break;
--v0;
}
if ( !v1 )
result = putchar(48);
return result;
}

original_code:
void print_buffer() {
 int i;
 char nonzero = 0;
 switch (buffer[9]) {
  case 0x00:
   break;
  case 0x80:
   putchar('-');
   break;
  default:
   printf("NaN/overflow");
   return;
 }
 for (i=8; i >= 0; i--) {
  const int lo = buffer[i] & 0x0f;
  const int hi = (buffer[i] >> 4) & 0x0f;
  if (nonzero || hi > 0) {
   printf("%d", hi);
   if (hi > 0)
    nonzero = 1;
  }
  if (nonzero || lo > 0) {
   printf("%d", lo);
   if (lo > 0)
    nonzero = 1;
  }
 }
 if (!nonzero) {
  putchar('0');
 }
}

canonical_original_code:
void print_buffer() {
 int i;
 char nonzero = 0;
 switch (buffer[9]) {
  case 0x00:
   break;
  case 0x80:
   putchar('-');
   break;
  default:
   printf("NaN/overflow");
   return;
 }
 for (i=8; i >= 0; i--) {
  int lo = buffer[i] & 0x0f;
  int hi = (buffer[i] >> 4) & 0x0f;
  if (nonzero || hi > 0) {
   printf("%d", hi);
   if (hi > 0)
    nonzero = 1;
  }
  if (nonzero || lo > 0) {
   printf("%d", lo);
   if (lo > 0)
    nonzero = 1;
  }
 }
 if (!nonzero) {
  putchar('0');
 }
}

variable_types: nonzero char

variable_types: lo int

variable_types: hi int


main

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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
atof(*v3);
v5 = (double)v4;
printf("printf => %f\n", (double)v4);
printf("FBSTP => ");
fill_buffer(v5);
print_buffer();
putchar(10);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
atof(*v3);
v5 = (double)v4;
printf("printf => %f\n", (double)v4);
printf("FBSTP => ");
func7(v5);
func8();
putchar(10);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 double x;
 int i;
 for (i=1; i < argc; i++) {
  x = atof(argv[i]);
  printf("printf => %f\n", x);
  printf("FBSTP  => ");
  fill_buffer(x);
  print_buffer();
  putchar('\n');
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 double x;
 int i;
 for (i=1; i < argc; i++) {
  x = atof(argv[i]);
  printf("printf => %f\n", x);
  printf("FBSTP  => ");
  fill_buffer(x);
  print_buffer();
  putchar('\n');
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
double custom_floor(const double x) {
 IEEE754_double number = {.value=x};
 const int dot_position = 52 - (number.fields.exp_bias - IEEE754_DOUBLE_BIAS);
 if (dot_position > 52) {
  return 0.0;
 }
 if (dot_position > 0 && dot_position <= 52) {
  const uint64_t mask = ((uint64_t)1 << dot_position) - 1;
  number.fields.fraction &= ~mask;
 }
 return number.value;
}

canonical_original_code:
double custom_floor(double x) {
 union IEEE754_double number = {.value=x};
 int dot_position = 52 - (number.fields.exp_bias - IEEE754_DOUBLE_BIAS);
 if (dot_position > 52) {
  return 0.0;
 }
 if (dot_position > 0 && dot_position <= 52) {
  unsigned long mask = ((uint64_t)1 << dot_position) - 1;
  number.fields.fraction &= ~mask;
 }
 return number.value;
}

variable_types: union IEEE754_double { struct { unsigned long fraction; unsigned long exp_bias; unsigned long sign; } fields; unsigned long qword; double value; }

variable_types:
mask
unsigned long


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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
v4 = atof(*v3);
v5 = custom_floor(v4);
printf("floor(%0.8f) = %0.8f\n", v4, v5);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

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

if ( argc <= 1 )
return 0;
v3 = argv + 1;
do
{
v4 = atof(*v3);
v5 = func7(v4);
printf("floor(%0.8f) = %0.8f\n", v4, v5);
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 for (int i=1; i < argc; i++) {
  const double x = atof(argv[i]);
  const double floor_x = custom_floor(x);
  printf("floor(%0.8f) = %0.8f\n", x, floor_x);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 for (int i = 1; i < argc; i++) {
  double x = atof(argv[i]);
  double floor_x = custom_floor(x);
  printf("floor(%0.8f) = %0.8f\n", x, floor_x);
 }
 return 0;
}


decompiled_code:
__int64 __fastcall fill_word_naive(int a1)
{
if ( a1 && !is_power_of_two_non_zero(a1) )
__assert_fail("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 0x1Eu, "fill_word_naive");
return (unsigned int)-(a1 != 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
if ( a1 && !func7(a1) )
__assert_fail("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 0x1Eu, "fill_word_naive");
return (unsigned int)-(a1 != 0);
}

original_code:
uint32_t fill_word_naive(uint32_t x) {
    ((x == 0 || is_power_of_two_non_zero(x)) ? (void) (0) : __assert_fail ("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 30, __extension__ __PRETTY_FUNCTION__));
    return x ? 0xffffffff : 0;
}

canonical_original_code:
unsigned int fill_word_naive(unsigned int x) {
    ((x == 0 || is_power_of_two_non_zero(x)) ? (void) (0) : __assert_fail ("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 30, __extension__ __PRETTY_FUNCTION__));
    return x ? 0xffffffff : 0;
}

variable_types:
x
unsigned int


decompiled_code:
__int64 __fastcall fill_word_branchless(int a1)
{
if ( a1 && !is_power_of_two_non_zero(a1) )
__assert_fail("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 0x25u, "fill_word_branchless");
return (unsigned int)(-a1 >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
if ( a1 && !func7(a1) )
__assert_fail("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 0x25u, "fill_word_branchless");
return (unsigned int)(-a1 >> 31);
}

original_code:
uint32_t fill_word_branchless(uint32_t x) {
    ((x == 0 || is_power_of_two_non_zero(x)) ? (void) (0) : __assert_fail ("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 37, __extension__ __PRETTY_FUNCTION__));
    const uint32_t y = -x;
    return ((int32_t)(y) >> 31);
}

canonical_original_code:
unsigned int fill_word_branchless(unsigned int x) {
    ((x == 0 || is_power_of_two_non_zero(x)) ? (void) (0) : __assert_fail ("x == 0 || is_power_of_two_non_zero(x)", "single_bit_to_mask.c", 37, __extension__ __PRETTY_FUNCTION__));
    unsigned int y = -x;
    return ((int)(y) >> 31);
}

variable_types:
y
unsigned int


decompiled_code:
_BOOL8 __fastcall is_power_of_two_non_zero(int a1)
{
return (a1 & -a1) == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(int a1)
{
return (a1 & -a1) == a1;
}

original_code:
int is_power_of_two_non_zero(uint32_t x) {
    return (x & -x) == x;
}

canonical_original_code:
int is_power_of_two_non_zero(unsigned int x) {
    return (x & -x) == x;
}


verify

decompiled_code:
__int64 verify()
{
int v0; // ebx
int v1; // er12
int v2; // ebx
int v3; // ebp
__int64 result; // rax

v0 = fill_word_branchless(0);
if ( v0 != (unsigned int)fill_word_naive(0) )
__assert_fail("fill_word_branchless(0) == fill_word_naive(0)", "single_bit_to_mask.c", 0x31u, "verify");
v1 = 32;
v2 = 1;
do
{
v3 = fill_word_branchless(v2);
result = fill_word_naive(v2);
if ( v3 != (_DWORD)result )
__assert_fail("fill_word_branchless(bit) == fill_word_naive(bit)", "single_bit_to_mask.c", 0x36u, "verify");
v2 *= 2;
--v1;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // ebx
int v1; // er12
int v2; // ebx
int v3; // ebp
__int64 result; // rax

v0 = func9(0);
if ( v0 != (unsigned int)func8(0) )
__assert_fail("fill_word_branchless(0) == fill_word_naive(0)", "single_bit_to_mask.c", 0x31u, "verify");
v1 = 32;
v2 = 1;
do
{
v3 = func9(v2);
result = func8(v2);
if ( v3 != (_DWORD)result )
__assert_fail("fill_word_branchless(bit) == fill_word_naive(bit)", "single_bit_to_mask.c", 0x36u, "verify");
v2 *= 2;
--v1;
}
while ( v1 );
return result;
}

original_code:
void verify() {
    ((fill_word_branchless(0) == fill_word_naive(0)) ? (void) (0) : __assert_fail ("fill_word_branchless(0) == fill_word_naive(0)", "single_bit_to_mask.c", 49, __extension__ __PRETTY_FUNCTION__));
    uint32_t bit = 1;
    for (int i=0; i < 32; i++, bit <<= 1) {
        ((fill_word_branchless(bit) == fill_word_naive(bit)) ? (void) (0) : __assert_fail ("fill_word_branchless(bit) == fill_word_naive(bit)", "single_bit_to_mask.c", 54, __extension__ __PRETTY_FUNCTION__));
    }
}

canonical_original_code:
void verify() {
    ((fill_word_branchless(0) == fill_word_naive(0)) ? (void) (0) : __assert_fail ("fill_word_branchless(0) == fill_word_naive(0)", "single_bit_to_mask.c", 49, __extension__ __PRETTY_FUNCTION__));
    unsigned int bit = 1;
    for (int i = 0; i < 32; i++, bit <<= 1) {
        ((fill_word_branchless(bit) == fill_word_naive(bit)) ? (void) (0) : __assert_fail ("fill_word_branchless(bit) == fill_word_naive(bit)", "single_bit_to_mask.c", 54, __extension__ __PRETTY_FUNCTION__));
    }
}

variable_types:
bit
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
verify(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func10(argc, argv, envp);
return 0;
}

original_code:
int main() {
    verify();
}

canonical_original_code:
int main() {
    verify();
}


thread

original_code:
void* thread(void* sec) {
 Job *job;
 double dRe, dIm;
 double Cre, Cim, Xre, Xim, Tre, Tim;
 int x, y, i;
 processed_t result;
    result.processed = 0;
 while (1) {
  pthread_mutex_lock(&mutex);
  if (free_job_idx == (((1024*4))/256 * ((1024*4))/256)) {
   pthread_mutex_unlock(&mutex);
   break;
  }
  else {
   job = &jobs[free_job_idx];
   free_job_idx += 1;
  }
  pthread_mutex_unlock(&mutex);
  result.processed += 1;
  dRe = (job->Re2 - job->Re1)/job->width;
  dIm = (job->Im2 - job->Im1)/job->height;
  Cim = job->Im1;
  for (y=0; y < job->height; y++) {
   Cre = job->Re1;
   for (x=0; x < job->width; x++) {
    Xre = 0.0;
    Xim = 0.0;
    for (i=0; i < job->maxiters; i++) {
     Tre = Xre*Xre - Xim*Xim + Cre;
     Tim = 2*Xre*Xim + Cim;
     if (Tre*Tre + Tim*Tim > job->threshold)
      break;
     Xre = Tre;
     Xim = Tim;
    }
    Image[job->yo + y][job->xo + x] = (job->maxiters - i);
    Cre += dRe;
   }
   Cim += dIm;
  }
 }
 return result.thread_result;
}

canonical_original_code:
void *thread(void *sec) {
 struct Job *job;
 double dRe; double dIm;
 double Cre; double Cim; double Xre; double Xim; double Tre; double Tim;
 int x; int y; int i;
 union processed_t result;
    result.processed = 0;
 while (1) {
  pthread_mutex_lock(&mutex);
  if (free_job_idx == (((1024*4))/256 * ((1024*4))/256)) {
   pthread_mutex_unlock(&mutex);
   break;
  }
  else {
   job = &jobs[free_job_idx];
   free_job_idx += 1;
  }
  pthread_mutex_unlock(&mutex);
  result.processed += 1;
  dRe = (job->Re2 - job->Re1)/job->width;
  dIm = (job->Im2 - job->Im1)/job->height;
  Cim = job->Im1;
  for (y=0; y < job->height; y++) {
   Cre = job->Re1;
   for (x=0; x < job->width; x++) {
    Xre = 0.0;
    Xim = 0.0;
    for (i=0; i < job->maxiters; i++) {
     Tre = Xre*Xre - Xim*Xim + Cre;
     Tim = 2*Xre*Xim + Cim;
     if (Tre*Tre + Tim*Tim > job->threshold)
      break;
     Xre = Tre;
     Xim = Tim;
    }
    Image[job->yo + y][job->xo + x] = (job->maxiters - i);
    Cre += dRe;
   }
   Cim += dIm;
  }
 }
 return result.thread_result;
}

variable_types:
sec
void *

variable_types: struct Job { int id; double Re1; double threshold; int xo; int width; int maxiters; } *

variable_types: dRe double

variable_types: dIm double

variable_types: Cre double

variable_types: Cim double

variable_types: Xre double

variable_types: Xim double

variable_types: Tre double

variable_types: Tim double

variable_types: x int

variable_types: y int

variable_types: union processed_t { int processed; void *thread_result; }

return_type: void *


decompiled_code:
int __fastcall print_errno(const char *a1)
{
unsigned int v1; // ebp
char *v2; // rax

v1 = *__errno_location();
v2 = strerror(v1);
return printf("%s: %s [errno=%d]\n", a1, v2, v1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
unsigned int v1; // ebp
char *v2; // rax

v1 = *__errno_location();
v2 = strerror(v1);
return printf("%s: %s [errno=%d]\n", a1, v2, v1);
}

original_code:
void print_errno(const char* prefix) {
 printf("%s: %s [errno=%d]\n", prefix, strerror((*__errno_location ())), (*__errno_location ()));
}

canonical_original_code:
void print_errno(char *prefix) {
 printf("%s: %s [errno=%d]\n", prefix, strerror((*__errno_location ())), (*__errno_location ()));
}

variable_types:
prefix
char *


main

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

if ( argc <= 1 )
{
printf("usage: %s directory\n", *argv);
result = 1;
}
else
{
v3 = (char **)(argv + 1);
do
fopen_directory(*v3++);
while ( v3 != (char **)&argv[(unsigned int)(argc - 2) + 2] );
result = 0;
}
return result;
}

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

if ( argc <= 1 )
{
printf("usage: %s directory\n", *argv);
result = 1;
}
else
{
v3 = (char **)(argv + 1);
do
func8(*v3++);
while ( v3 != (char **)&argv[(unsigned int)(argc - 2) + 2] );
result = 0;
}
return result;
}

original_code:
int main(int argc, char* argv[]) {
 int i;
 if (argc > 1) {
  for (i=1; i < argc; i++)
   fopen_directory(argv[i]);
  return 0;
 } else {
  printf("usage: %s directory\n", argv[0]);
  return 1;
 }
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int i;
 if (argc > 1) {
  for (i=1; i < argc; i++)
   fopen_directory(argv[i]);
  return 0;
 } else {
  printf("usage: %s directory\n", argv[0]);
  return 1;
 }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int


decompiled_code:
__int64 get_mxcsr()
{
_mm_getcsr();
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
_mm_getcsr();
return 0LL;
}

original_code:
uint32_t get_mxcsr() {
 uint32_t result = 0;
 __asm__ __volatile__("stmxcsr %0": :"m" (result));
 return result;
}

canonical_original_code:
unsigned int get_mxcsr() {
 unsigned int result = 0;
 __asm__ __volatile__("stmxcsr %0": :"m" (result));
 return result;
}

variable_types:
result
unsigned int


decompiled_code:
int *__fastcall set_mxcsr(unsigned int a1)
{
int *result; // rax
int v2; // [rsp+0h] [rbp-4h] BYREF

result = &v2;
_mm_setcsr(a1);
return result;
}

canonical_decompiled_code:
int *__fastcall func8(unsigned int a1)
{
int *result; // rax
int v2; // [rsp+0h] [rbp-4h] BYREF

result = &v2;
_mm_setcsr(a1);
return result;
}

original_code:
void set_mxcsr(uint32_t mxcsr) {
 __asm__ __volatile__("ldmxcsr (%0)"
  :
  :"g" (&mxcsr)
  : "memory"
 );
}

canonical_original_code:
void set_mxcsr(unsigned int mxcsr) {
 __asm__ __volatile__("ldmxcsr (%0)"
  :
  :"g" (&mxcsr)
  : "memory"
 );
}

variable_types:
mxcsr
unsigned int


decompiled_code:
int dump_mxcsr()
{
unsigned int v0; // eax

v0 = get_mxcsr();
return print_mxcsr(_bss_start, v0);
}

canonical_decompiled_code:
int func11()
{
unsigned int v0; // eax

v0 = func7();
return func10(_bss_start, v0);
}

original_code:
void dump_mxcsr() {
 print_mxcsr(stdout, get_mxcsr());
}


gettime

decompiled_code:
__suseconds_t gettime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (v1.tv_usec + 1000000 * v1.tv_sec) / 1000;
}

canonical_decompiled_code:
__suseconds_t func19()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (v1.tv_usec + 1000000 * v1.tv_sec) / 1000;
}

original_code:
unsigned gettime() {
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 return (tv.tv_sec * 1000000 + tv.tv_usec)/1000;
}

canonical_original_code:
unsigned int gettime() {
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 return (tv.tv_sec * 1000000 + tv.tv_usec)/1000;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


usage

decompiled_code:
int usage()
{
return puts("program dictionary-file word-test-file iterations-count");
}

canonical_decompiled_code:
int func20()
{
return puts("program dictionary-file word-test-file iterations-count");
}

original_code:
void usage() {
 puts("usage: progname min max count [functions]");
 puts("");
 puts("where function is:");
    struct Procedure* item;
    for (item = procedures; item->procedure != ((void *)0); item++) {
        printf("* %-8s - %s\n", item->keyword, item->name);
    }
 exit(1);
}

canonical_original_code:
void usage() {
 puts("usage: progname min max count [functions]");
 puts("");
 puts("where function is:");
    struct Procedure *item;
    for (item = procedures; item->procedure != ((void *)0); item++) {
        printf("* %-8s - %s\n", item->keyword, item->name);
    }
 exit(1);
}

variable_types: struct Procedure { char *(*procedure)(unsigned int); char *name; char *keyword; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
char *v4; // r12
FILE *v5; // rax
FILE *v6; // rbp
unsigned int v7; // eax
FILE *v8; // rax
FILE *v9; // rbp
int v10; // er14
int v11; // er15
int i; // er13
unsigned int j; // ebp
unsigned __int64 v14; // rbx
int v15; // eax
__int64 v16; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v17; // [rsp+8h] [rbp-40h]

if ( argc == 4 )
{
printf("loading dictionary... ");
fflush(_bss_start);
v4 = (char *)trie_new_node();
if ( !v4 )
__assert_fail("root != NULL", "test.c", 0x70u, "main");
v5 = fopen(argv[1], "rt");
v6 = v5;
if ( !v5 )
__assert_fail("f != NULL", "test.c", 0x72u, "main");
v7 = load_dictionary(v4, v5);
printf("%d words loaded\n", v7);
fclose(v6);
printf("loading test words... ");
fflush(_bss_start);
v8 = fopen(argv[2], "rt");
v9 = v8;
if ( !v8 )
__assert_fail("f != NULL", "test.c", 0x7Cu, "main");
load_words(v8, (__int64)&v16);
printf("%ld words loaded\n", v17);
fclose(v9);
puts("benchmarking...");
v10 = atoi(argv[3]);
if ( v10 <= 0 )
__assert_fail("iterations > 0", "test.c", 0x85u, "main");
v11 = gettime();
for ( i = 0; i != v10; ++i )
{
v14 = 0LL;
for ( j = 0; v17 > v14; ++v14 )
j += trie_lookup(v4, *(char **)(v16 + 8 * v14));
}
v15 = gettime();
printf("... time = %d ms, matched words = %d\n", (unsigned int)(v15 - v11), j);
free_strings((__int64)&v16);
trie_destroy((__int64)v4);
result = 0;
}
else
{
usage();
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
char *v4; // r12
FILE *v5; // rax
FILE *v6; // rbp
unsigned int v7; // eax
FILE *v8; // rax
FILE *v9; // rbp
int v10; // er14
int v11; // er15
int i; // er13
unsigned int j; // ebp
unsigned __int64 v14; // rbx
int v15; // eax
__int64 v16; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v17; // [rsp+8h] [rbp-40h]

if ( argc == 4 )
{
printf("loading dictionary... ");
fflush(_bss_start);
v4 = (char *)func11();
if ( !v4 )
__assert_fail("root != NULL", "test.c", 0x70u, "main");
v5 = fopen(argv[1], "rt");
v6 = v5;
if ( !v5 )
__assert_fail("f != NULL", "test.c", 0x72u, "main");
v7 = func16(v4, v5);
printf("%d words loaded\n", v7);
fclose(v6);
printf("loading test words... ");
fflush(_bss_start);
v8 = fopen(argv[2], "rt");
v9 = v8;
if ( !v8 )
__assert_fail("f != NULL", "test.c", 0x7Cu, "main");
func17(v8, (__int64)&v16);
printf("%ld words loaded\n", v17);
fclose(v9);
puts("benchmarking...");
v10 = atoi(argv[3]);
if ( v10 <= 0 )
__assert_fail("iterations > 0", "test.c", 0x85u, "main");
v11 = func19();
for ( i = 0; i != v10; ++i )
{
v14 = 0LL;
for ( j = 0; v17 > v14; ++v14 )
j += func13(v4, *(char **)(v16 + 8 * v14));
}
v15 = func19();
printf("... time = %d ms, matched words = %d\n", (unsigned int)(v15 - v11), j);
func18((__int64)&v16);
func14((__int64)v4);
result = 0;
}
else
{
func20();
result = 1;
}
return result;
}

original_code:
int main(int argc, char* argv[]) {
 if (argc < 4)
  usage();
    const uint32_t min = atoi(argv[1]);
    const uint32_t max = atoi(argv[2]);
    if (min >= max) {
        usage();
    }
    const uint32_t count = atoi(argv[3]);
    printf("Parameters: min = %u, max = %d, count = %d\n", min, max, count);
    struct Procedure* item;
    unsigned reference_time = 0;
    for (item = procedures; item->procedure != ((void *)0); item++) {
        if (!is_keyword(item->keyword, argc, argv)) {
            continue;
        }
        unsigned tmp = count;
        convfun proc = item->procedure;
        printf("%-20s ", item->name);
        fflush(stdout);
        const unsigned t1 = gettime();
        while (tmp-- > 0) {
            for (uint32_t x = min; x <= max; x++) {
                proc(x);
            }
        }
        const unsigned t2 = gettime();
        const unsigned time = t2 - t1;
        printf("%6u ms", time);
        if (reference_time == 0) {
            reference_time = time;
            putchar('\n');
        } else {
            printf(" (%0.2f)\n", ((float)reference_time)/time);
        }
    }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 if (argc < 4)
  usage();
    unsigned int min = atoi(argv[1]);
    unsigned int max = atoi(argv[2]);
    if (min >= max) {
        usage();
    }
    unsigned int count = atoi(argv[3]);
    printf("Parameters: min = %u, max = %d, count = %d\n", min, max, count);
    struct Procedure *item;
    unsigned int reference_time = 0;
    for (item = procedures; item->procedure != ((void *)0); item++) {
        if (!is_keyword(item->keyword, argc, argv)) {
            continue;
        }
        unsigned int tmp = count;
        char *(*proc)(unsigned int) = item->procedure;
        printf("%-20s ", item->name);
        fflush(stdout);
        unsigned int t1 = gettime();
        while (tmp-- > 0) {
            for (unsigned int x = min; x <= max; x++) {
                proc(x);
            }
        }
        unsigned int t2 = gettime();
        unsigned int time = t2 - t1;
        printf("%6u ms", time);
        if (reference_time == 0) {
            reference_time = time;
            putchar('\n');
        } else {
            printf(" (%0.2f)\n", ((float)reference_time)/time);
        }
    }
 return 0;
}

variable_types:
min
unsigned int

variable_types:
max
unsigned int

variable_types:
count
unsigned int

variable_types:
tmp
unsigned int

variable_types:
proc
char *(*)(unsigned int)

variable_types:
x
unsigned int

variable_types:
time
unsigned int

user_defined_types: struct Procedure { char *(*procedure)(unsigned int); char *name; char *keyword; }


bswap

decompiled_code:
__int64 __fastcall bswap(unsigned int a1)
{
return HIBYTE(a1) | (BYTE1(a1) << 16) | ((unsigned __int8)a1 << 24) | (BYTE2(a1) << 8);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
return HIBYTE(a1) | (BYTE1(a1) << 16) | ((unsigned __int8)a1 << 24) | (BYTE2(a1) << 8);
}

original_code:
uint32_t bswap(uint32_t x) {
    uint32_t result;
    __asm__ __volatile__("bswap %0" : "=&r" (result) : "0" (x));
    return result;
}

canonical_original_code:
unsigned int bswap(unsigned int x) {
    unsigned int result;
    __asm__ __volatile__("bswap %0" : "=&r" (result) : "0" (x));
    return result;
}


verify

decompiled_code:
__int64 verify(void)
{
__int64 result; // rax
int v1; // [rsp+10h] [rbp-20h]
int v2; // [rsp+14h] [rbp-1Ch]
__int64 v3; // [rsp+18h] [rbp-18h]
int v4; // [rsp+20h] [rbp-10h]
int v5; // [rsp+24h] [rbp-Ch]
unsigned int v6; // [rsp+28h] [rbp-8h]
unsigned int i; // [rsp+2Ch] [rbp-4h]

prepare_single_lookup();
result = prepare_two_lookups();
for ( i = 0; i <= 0xFFF; ++i )
{
v6 = to_oct_naive(i);
v5 = to_oct_mul(i);
v4 = to_oct_pdep(i);
v3 = to_oct_sse2(i | (i << 12));
v2 = to_oct_single_lookup(i);
v1 = to_oct_two_lookups(i);
if ( v6 != v5 )
__assert_fail("naive == mul", "verify.cpp", 0x34u, "void verify()");
if ( v6 != v4 )
__assert_fail("naive == pdep", "verify.cpp", 0x35u, "void verify()");
if ( v6 != (_DWORD)v3 )
__assert_fail("naive == sse2_lo", "verify.cpp", 0x36u, "void verify()");
if ( v6 != HIDWORD(v3) )
__assert_fail("naive == sse2_hi", "verify.cpp", 0x37u, "void verify()");
if ( v6 != v2 )
__assert_fail("naive == LUT1", "verify.cpp", 0x38u, "void verify()");
result = v6;
if ( v6 != v1 )
__assert_fail("naive == LUT2", "verify.cpp", 0x39u, "void verify()");
}
return result;
}

canonical_decompiled_code:
__int64 func8(void)
{
__int64 result; // rax
int v1; // [rsp+10h] [rbp-20h]
int v2; // [rsp+14h] [rbp-1Ch]
__int64 v3; // [rsp+18h] [rbp-18h]
int v4; // [rsp+20h] [rbp-10h]
int v5; // [rsp+24h] [rbp-Ch]
unsigned int v6; // [rsp+28h] [rbp-8h]
unsigned int i; // [rsp+2Ch] [rbp-4h]

func17();
result = func19();
for ( i = 0; i <= 0xFFF; ++i )
{
v6 = func13(i);
v5 = func14(i);
v4 = func15(i);
v3 = func16(i | (i << 12));
v2 = func18(i);
v1 = func20(i);
if ( v6 != v5 )
__assert_fail("naive == mul", "verify.cpp", 0x34u, "void verify()");
if ( v6 != v4 )
__assert_fail("naive == pdep", "verify.cpp", 0x35u, "void verify()");
if ( v6 != (_DWORD)v3 )
__assert_fail("naive == sse2_lo", "verify.cpp", 0x36u, "void verify()");
if ( v6 != HIDWORD(v3) )
__assert_fail("naive == sse2_hi", "verify.cpp", 0x37u, "void verify()");
if ( v6 != v2 )
__assert_fail("naive == LUT1", "verify.cpp", 0x38u, "void verify()");
result = v6;
if ( v6 != v1 )
__assert_fail("naive == LUT2", "verify.cpp", 0x39u, "void verify()");
}
return result;
}

original_code:
void verify() {
    char buf[5];
    for (int x=0; x < 16; x++) {
        to_bin_naive(x, buf);
        const int parsed = nibble_from_string(buf);
        printf("%d => '%s' => %d\n", x, buf, parsed);
        ((x == parsed) ? (void) (0) : __assert_fail ("x == parsed", "parse_string.c", 54, __extension__ __PRETTY_FUNCTION__));
    }
    buf[0] = '?';
    ((nibble_from_string(buf) < 0) ? (void) (0) : __assert_fail ("nibble_from_string(buf) < 0", "parse_string.c", 58, __extension__ __PRETTY_FUNCTION__));
}

canonical_original_code:
void verify() {
    char buf[5];
    for (int x = 0; x < 16; x++) {
        to_bin_naive(x, buf);
        int parsed = nibble_from_string(buf);
        printf("%d => '%s' => %d\n", x, buf, parsed);
        ((x == parsed) ? (void) (0) : __assert_fail ("x == parsed", "parse_string.c", 54, __extension__ __PRETTY_FUNCTION__));
    }
    buf[0] = '?';
    ((nibble_from_string(buf) < 0) ? (void) (0) : __assert_fail ("nibble_from_string(buf) < 0", "parse_string.c", 58, __extension__ __PRETTY_FUNCTION__));
}

variable_types:
buf
char [5]

variable_types: x int

variable_types: parsed int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
verify_correcteness();
verify();
puts("all ok");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7();
func8();
puts("all ok");
return 0;
}


compare

original_code:
void compare() {
    const uint32_t mask = 0x01010101;
    for (int i=0; i < 16; i++) {
        ((naive(i, mask) == branchless(i, mask)) ? (void) (0) : __assert_fail ("naive(i, mask) == branchless(i, mask)", "pdep_soft_emu.c", 79, __extension__ __PRETTY_FUNCTION__));
    }
}

canonical_original_code:
void compare() {
    unsigned int mask = 0x01010101;
    for (int i = 0; i < 16; i++) {
        ((naive(i, mask) == branchless(i, mask)) ? (void) (0) : __assert_fail ("naive(i, mask) == branchless(i, mask)", "pdep_soft_emu.c", 79, __extension__ __PRETTY_FUNCTION__));
    }
}

variable_types:
mask
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
compare();
return 0;
}

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

original_code:
int main() {
    compare();
    const int iterations = (1 << 20);
    uint32_t mask = 0;
    printf("| %8s | %15s | %15s | %10s |\n", "mask", "naive", "branchless", "speedup");
    for (int i=0; i < 33; i++) {
        const double tn = test_naive(iterations, mask);
        const double tb = test_branchless(iterations, mask);
        const double speedup = tn / tb;
        printf("| %08x | %15.4f | %15.4f | %10.2f |\n", mask, tn, tb, speedup);
        mask = 2*mask + 1;
    }
}

canonical_original_code:
int main() {
    compare();
    int iterations = (1 << 20);
    unsigned int mask = 0;
    printf("| %8s | %15s | %15s | %10s |\n", "mask", "naive", "branchless", "speedup");
    for (int i = 0; i < 33; i++) {
        double tn = test_naive(iterations, mask);
        double tb = test_branchless(iterations, mask);
        double speedup = tn / tb;
        printf("| %08x | %15.4f | %15.4f | %10.2f |\n", mask, tn, tb, speedup);
        mask = 2*mask + 1;
    }
}

variable_types: iterations int

variable_types: tn double

variable_types: tb double

variable_types: speedup double


naive

decompiled_code:
__int64 __fastcall convert_to_bin::naive(convert_to_bin *this)
{
char v1; // dl
__int64 v3; // [rsp+4h] [rbp-10h]
unsigned __int8 v4; // [rsp+Fh] [rbp-5h]
int i; // [rsp+10h] [rbp-4h]

for ( i = 7; i >= 0; --i )
{
v4 = 1 << i;
if ( (v4 & (unsigned __int8)this) != 0 )
v1 = 49;
else
v1 = 48;
*((_BYTE *)&v3 + i) = v1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall convert_to_bin::func7(convert_to_bin *this)
{
char v1; // dl
__int64 v3; // [rsp+4h] [rbp-10h]
unsigned __int8 v4; // [rsp+Fh] [rbp-5h]
int i; // [rsp+10h] [rbp-4h]

for ( i = 7; i >= 0; --i )
{
v4 = 1 << i;
if ( (v4 & (unsigned __int8)this) != 0 )
v1 = 49;
else
v1 = 48;
*((_BYTE *)&v3 + i) = v1;
}
return v3;
}

original_code:
uint32_t naive(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    uint32_t mask_bit = 1;
    uint32_t src_bit = 1;
    int i;
    for (i=0; i < 32; i++, mask_bit <<= 1) {
        if (mask & mask_bit) {
            if (src & src_bit) {
                result |= mask_bit;
            }
            src_bit <<= 1;
        }
    }
    return result;
}

canonical_original_code:
unsigned int naive(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    unsigned int mask_bit = 1;
    unsigned int src_bit = 1;
    int i;
    for (i=0; i < 32; i++, mask_bit <<= 1) {
        if (mask & mask_bit) {
            if (src & src_bit) {
                result |= mask_bit;
            }
            src_bit <<= 1;
        }
    }
    return result;
}

variable_types:
src
unsigned int

variable_types:
result
unsigned int

variable_types: i int


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
convert_to_bin::prepare_lookup(*(convert_to_bin **)&argc);
verify();
return 0;
}

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


bswap

decompiled_code:
__int64 __fastcall bswap(unsigned int a1)
{
return _byteswap_ulong(a1);
}

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("preconditions...");
verify_correctness();
puts("nibble expand...");
verify_expand();
puts("conversion to hex...");
verify_conv_to_hex();
puts("all ok!");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("preconditions...");
func18();
puts("nibble expand...");
func20();
puts("conversion to hex...");
func19();
puts("all ok!");
return 0;
}


strtonum

repo: WrathOfChris/event

original_code:
long long
strtonum(const char *numstr, long long minval, long long maxval,
    const char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval {
  const char *errstr;
  int err;
 } ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

canonical_original_code:
long long strtonum(char *numstr, long long minval, long long maxval, char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

variable_types:
numstr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstrp
char **

variable_types:
ll
long long

variable_types:
ep
char *

variable_types: error int

variable_types: struct errval { char *errstr; int err; } [4]

return_type: long long


repo: XanClic/shiny-c

decompiled_code:
__int64 __fastcall refl_assign(__m128i *a1, const __m128i *a2)
{
__int64 v4; // rax
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rax
_QWORD *v8; // r13
const char *v9; // rdi
const char *v10; // rsi
__int64 v11; // rax
__int64 v12; // rdi
const char *v13; // r14
const char *v14; // r13
const char *v15; // rax

v4 = ((__int64 (*)(void))refl_hash_find)();
v5 = v4;
if ( a1 == a2 )
{
*a1 = _mm_loadu_si128((const __m128i *)(v4 + 24));
}
else if ( (unsigned __int8)refl_is_virtual(a2) )
{
v7 = refl_hash_find(a2);
v8 = (_QWORD *)v7;
v9 = *(const char **)(v5 + 56);
if ( v9 )
{
v10 = *(const char **)(v7 + 56);
if ( v10 )
{
if ( strcmp(v9, v10) )
{
v13 = (const char *)refl_get_name(a2);
v14 = (const char *)v8[7];
v15 = (const char *)refl_get_name(a1);
fprintf(
stderr,
"Cannot redirect function %#lx (%s %s) to %#lx (%s %s); signatures differ\n",
a1,
*(const char **)(v5 + 56),
v15,
a2,
v14,
v13);
_assert_fail("0", "lib/assign.c", 0x21u, "refl_assign");
}
}
}
v11 = *(_QWORD *)(v5 + 40);
v12 = v8[4];
*(_QWORD *)(v11 + 80) = v8[3];
*(_QWORD *)(v11 + 88) = v12;
establish_trampoline(v5);
}
else
{
*(__m128i *)(*(_QWORD *)(v5 + 40) + 80LL) = _mm_loadu_si128(a2);
establish_trampoline(v5);
}
result = *(_QWORD *)(v5 + 40);
*(_QWORD *)(result + 72) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__m128i *a1, const __m128i *a2)
{
__int64 v4; // rax
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rax
_QWORD *v8; // r13
const char *v9; // rdi
const char *v10; // rsi
__int64 v11; // rax
__int64 v12; // rdi
const char *v13; // r14
const char *v14; // r13
const char *v15; // rax

v4 = ((__int64 (*)(void))refl_hash_find)();
v5 = v4;
if ( a1 == a2 )
{
*a1 = _mm_loadu_si128((const __m128i *)(v4 + 24));
}
else if ( (unsigned __int8)refl_is_virtual(a2) )
{
v7 = refl_hash_find(a2);
v8 = (_QWORD *)v7;
v9 = *(const char **)(v5 + 56);
if ( v9 )
{
v10 = *(const char **)(v7 + 56);
if ( v10 )
{
if ( strcmp(v9, v10) )
{
v13 = (const char *)refl_get_name(a2);
v14 = (const char *)v8[7];
v15 = (const char *)refl_get_name(a1);
fprintf(
stderr,
"Cannot redirect function %#lx (%s %s) to %#lx (%s %s); signatures differ\n",
a1,
*(const char **)(v5 + 56),
v15,
a2,
v14,
v13);
_assert_fail("0", "lib/assign.c", 0x21u, "refl_assign");
}
}
}
v11 = *(_QWORD *)(v5 + 40);
v12 = v8[4];
*(_QWORD *)(v11 + 80) = v8[3];
*(_QWORD *)(v11 + 88) = v12;
establish_trampoline(v5);
}
else
{
*(__m128i *)(*(_QWORD *)(v5 + 40) + 80LL) = _mm_loadu_si128(a2);
establish_trampoline(v5);
}
result = *(_QWORD *)(v5 + 40);
*(_QWORD *)(result + 72) = a2;
return result;
}

original_code:
void refl_assign(func_t dest, func_t src)
{
    const struct vf_ht_entry *de = refl_hash_find(dest);
    if (src == dest)
        memcpy((void *)dest, de->prologue, 16);
    else if (refl_is_virtual(src))
    {
        const struct vf_ht_entry *se = refl_hash_find(src);
        if (de->signature && se->signature && strcmp(de->signature, se->signature))
        {
            fprintf(stderr, "Cannot redirect function %#lx (%s %s) to %#lx (%s %s); signatures differ\n", dest, de->signature, refl_get_name(dest), src, se->signature, refl_get_name(src));
            ((0) ? (void) (0) : __assert_fail ("0", "lib/assign.c", 33, __extension__ __PRETTY_FUNCTION__));
        }
        memcpy(de->trampoline->prologue, se->prologue, 16);
        establish_trampoline(de);
    }
    else
    {
        memcpy(de->trampoline->prologue, (void *)src, 16);
        establish_trampoline(de);
    }
    de->trampoline->target = src;
}

canonical_original_code:
void refl_assign(unsigned long dest, unsigned long src)
{
    struct vf_ht_entry *de = refl_hash_find(dest);
    if (src == dest)
        memcpy((void *)dest, de->prologue, 16);
    else if (refl_is_virtual(src))
    {
        struct vf_ht_entry *se = refl_hash_find(src);
        if (de->signature && se->signature && strcmp(de->signature, se->signature))
        {
            fprintf(stderr, "Cannot redirect function %#lx (%s %s) to %#lx (%s %s); signatures differ\n", dest, de->signature, refl_get_name(dest), src, se->signature, refl_get_name(src));
            ((0) ? (void) (0) : __assert_fail ("0", "lib/assign.c", 33, __extension__ __PRETTY_FUNCTION__));
        }
        memcpy(de->trampoline->prologue, se->prologue, 16);
        establish_trampoline(de);
    }
    else
    {
        memcpy(de->trampoline->prologue, (void *)src, 16);
        establish_trampoline(de);
    }
    de->trampoline->target = src;
}

variable_types:
dest
unsigned long

variable_types:
src
unsigned long

variable_types: struct vf_ht_entry { struct vf_ht_entry *next; unsigned long vf; int symtab_idx; unsigned long prologue[2]; struct virt_trampoline { unsigned long code[8]; unsigned long scratch_ret; unsigned long target; unsigned long prologue[2]; unsigned long trampoline[2]; } *trampoline; struct annotation_entry { struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } annotation; struct annotation_entry *next; } *annotation_list; char *signature; } *

user_defined_types: struct vf_ht_entry { struct vf_ht_entry *next; unsigned long vf; int symtab_idx; unsigned long prologue[2]; struct virt_trampoline *trampoline; struct annotation_entry *annotation_list; char *signature; }

user_defined_types: struct annotation_entry { struct refl_annotation annotation; struct annotation_entry *next; }

user_defined_types: struct refl_annotation { unsigned long func; enum refl_annotation_type type; char *data; }


decompiled_code:
int __fastcall puts_wrapper(const char *a1)
{
return puts(a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return puts(a1);
}

original_code:
void puts_wrapper(const char *s)
{
    puts(s);
}

canonical_original_code:
void puts_wrapper(char *s)
{
    puts(s);
}

variable_types:
s
char *


foo

decompiled_code:
int __fastcall foo(unsigned int a1, unsigned int a2)
{
return printf("foo %i %i\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, unsigned int a2)
{
return printf("foo %i %i\n", a1, a2);
}

original_code:
void foo(int x, int y)
{
    printf("foo %i %i\n", x, y);
}

variable_types: x int

variable_types: y int


bar

decompiled_code:
int __fastcall bar(unsigned int a1, unsigned int a2)
{
return printf("bar %x %x\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, unsigned int a2)
{
return printf("bar %x %x\n", a1, a2);
}

original_code:
void bar(int x, int y)
{
    printf("bar %x %x\n", x, y);
}


decompiled_code:
int __fastcall print_target(const char *a1)
{
__int64 v1; // rax
__int64 v2; // rax
const char *v3; // rax

v1 = refl_find();
v2 = refl_get_target(v1);
v3 = (const char *)refl_get_name(v2);
return printf("%s() points to %s()\n", a1, v3);
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
__int64 v1; // rax
__int64 v2; // rax
const char *v3; // rax

v1 = refl_find();
v2 = refl_get_target(v1);
v3 = (const char *)refl_get_name(v2);
return printf("%s() points to %s()\n", a1, v3);
}

original_code:
void print_target(const char *func)
{
    printf("%s() points to %s()\n", func, refl_get_name(refl_get_target(refl_find(func))));
}

canonical_original_code:
void print_target(char *func)
{
    printf("%s() points to %s()\n", func, refl_get_name(refl_get_target(refl_find(func))));
}

variable_types:
func
char *


decompiled_code:
__int64 __fastcall refl_find(char *s2)
{
__int64 v1; // r12
unsigned int *v2; // rbx
__int64 v3; // r14

if ( _symtab_entries <= 0 )
return 0LL;
v1 = _strtab;
v2 = (unsigned int *)_symtab;
v3 = _symtab + 24LL * (unsigned int)(_symtab_entries - 1) + 24;
while ( strcmp((const char *)(v1 + *v2), s2) )
{
v2 += 6;
if ( v2 == (unsigned int *)v3 )
return 0LL;
}
return *((_QWORD *)v2 + 1) + _relocation;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2)
{
__int64 v1; // r12
unsigned int *v2; // rbx
__int64 v3; // r14

if ( _symtab_entries <= 0 )
return 0LL;
v1 = _strtab;
v2 = (unsigned int *)_symtab;
v3 = _symtab + 24LL * (unsigned int)(_symtab_entries - 1) + 24;
while ( strcmp((const char *)(v1 + *v2), s2) )
{
v2 += 6;
if ( v2 == (unsigned int *)v3 )
return 0LL;
}
return *((_QWORD *)v2 + 1) + _relocation;
}

original_code:
func_t refl_find(const char *name)
{
    for (int i = 0; i < __symtab_entries; i++)
        if (!strcmp(__strtab + __symtab[i].st_name, name))
            return __symtab[i].st_value + __relocation;
    return 0;
}

canonical_original_code:
unsigned long refl_find(char *name)
{
    for (int i = 0; i < __symtab_entries; i++)
        if (!strcmp(__strtab + __symtab[i].st_name, name))
            return __symtab[i].st_value + __relocation;
    return 0;
}

variable_types:
name
char *

return_type: unsigned long


decompiled_code:
__int64 __fastcall hash_vf(unsigned __int64 a1)
{
return (unsigned __int8)(a1 >> 4);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
return (unsigned __int8)(a1 >> 4);
}

original_code:
static unsigned hash_vf(func_t func)
{
    return (func >> 4) & ((1 << 8) - 1);
}

canonical_original_code:
static unsigned int hash_vf(unsigned long func)
{
    return (func >> 4) & ((1 << 8) - 1);
}

variable_types:
func
unsigned long


decompiled_code:
_BOOL8 __fastcall refl_is_virtual(unsigned __int64 a1)
{
bool v1; // al

v1 = 0;
if ( _vmin <= a1 )
v1 = _vmax > a1;
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned __int64 a1)
{
bool v1; // al

v1 = 0;
if ( _vmin <= a1 )
v1 = _vmax > a1;
return v1;
}

original_code:
_Bool refl_is_virtual(func_t func)
{
    return (func >= __vmin) && (func < __vmax);
}

canonical_original_code:
_Bool refl_is_virtual(unsigned long func)
{
    return (func >= __vmin) && (func < __vmax);
}


decompiled_code:
__int64 refl_call(__int64 a1, ...)
{
__int64 v1; // rax
char *v2; // rsi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = refl_hash_find();
if ( !v1 )
_assert_fail("e", "lib/call.c", 0x70u, "refl_call");
v2 = *(char **)(v1 + 56);
if ( !v2 )
_assert_fail("sig", "lib/call.c", 0x73u, "refl_call");
return raw_call(v1, v2, (int *)va);
}

canonical_decompiled_code:
__int64 func1(__int64 a1, ...)
{
__int64 v1; // rax
char *v2; // rsi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = refl_hash_find();
if ( !v1 )
_assert_fail("e", "lib/call.c", 0x70u, "refl_call");
v2 = *(char **)(v1 + 56);
if ( !v2 )
_assert_fail("sig", "lib/call.c", 0x73u, "refl_call");
return func0(v1, v2, (int *)va);
}

original_code:
void refl_call(func_t func, ...)
{
    va_list args;
    __builtin_va_start(args,func);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 112, __extension__ __PRETTY_FUNCTION__));
    const char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 115, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call(unsigned long func, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,func);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 112, __extension__ __PRETTY_FUNCTION__));
    char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 115, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

variable_types:
sig
char *

user_defined_types: struct virt_trampoline { unsigned long code[8]; unsigned long scratch_ret; unsigned long target; unsigned long prologue[2]; unsigned long trampoline[2]; }

user_defined_types: enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr }


decompiled_code:
__int64 refl_call_verify(__int64 a1, const char *a2, ...)
{
__int64 v2; // rax
__int64 v3; // rbx
char *v4; // r12
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
v3 = v2;
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x81u, "refl_call_verify");
v4 = *(char **)(v2 + 56);
if ( !v4 )
_assert_fail("sig", "lib/call.c", 0x84u, "refl_call_verify");
if ( strcmp(*(const char **)(v2 + 56), a2) )
{
fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", a2, v4);
_assert_fail("0", "lib/call.c", 0x88u, "refl_call_verify");
}
return raw_call(v3, v4, (int *)va);
}

canonical_decompiled_code:
__int64 func2(__int64 a1, const char *a2, ...)
{
__int64 v2; // rax
__int64 v3; // rbx
char *v4; // r12
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
v3 = v2;
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x81u, "refl_call_verify");
v4 = *(char **)(v2 + 56);
if ( !v4 )
_assert_fail("sig", "lib/call.c", 0x84u, "refl_call_verify");
if ( strcmp(*(const char **)(v2 + 56), a2) )
{
fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", a2, v4);
_assert_fail("0", "lib/call.c", 0x88u, "refl_call_verify");
}
return func0(v3, v4, (int *)va);
}

original_code:
void refl_call_verify(func_t func, const char *expsig, ...)
{
    va_list args;
    __builtin_va_start(args,expsig);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 129, __extension__ __PRETTY_FUNCTION__));
    const char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 132, __extension__ __PRETTY_FUNCTION__));
    if (strcmp(sig, expsig))
    {
        fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", expsig, sig);
        ((0) ? (void) (0) : __assert_fail ("0", "lib/call.c", 136, __extension__ __PRETTY_FUNCTION__));
    }
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call_verify(unsigned long func, char *expsig, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,expsig);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 129, __extension__ __PRETTY_FUNCTION__));
    char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 132, __extension__ __PRETTY_FUNCTION__));
    if (strcmp(sig, expsig))
    {
        fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", expsig, sig);
        ((0) ? (void) (0) : __assert_fail ("0", "lib/call.c", 136, __extension__ __PRETTY_FUNCTION__));
    }
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

variable_types:
expsig
char *


decompiled_code:
__int64 refl_call_sig(__int64 a1, char *a2, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x97u, "refl_call_sig");
if ( !a2 )
_assert_fail("sig", "lib/call.c", 0x98u, "refl_call_sig");
return raw_call(v2, a2, (int *)va);
}

canonical_decompiled_code:
__int64 func3(__int64 a1, char *a2, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x97u, "refl_call_sig");
if ( !a2 )
_assert_fail("sig", "lib/call.c", 0x98u, "refl_call_sig");
return func0(v2, a2, (int *)va);
}

original_code:
void refl_call_sig(func_t func, const char *sig, ...)
{
    va_list args;
    __builtin_va_start(args,sig);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 151, __extension__ __PRETTY_FUNCTION__));
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 152, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call_sig(unsigned long func, char *sig, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,sig);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 151, __extension__ __PRETTY_FUNCTION__));
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 152, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}


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

result = refl_hash_find();
if ( result )
{
result = *(_QWORD *)(result + 48);
if ( result )
{
while ( *(_DWORD *)(result + 8) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
}
return result;
}

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

result = refl_hash_find();
if ( result )
{
result = *(_QWORD *)(result + 48);
if ( result )
{
while ( *(_DWORD *)(result + 8) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
}
return result;
}

original_code:
const char *refl_get_annotation(func_t func, enum refl_annotation_type type)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return ((void *)0);
    for (struct annotation_entry *ae = e->annotation_list; ae != ((void *)0); ae = ae->next)
        if (ae->annotation.type == type)
            return ae->annotation.data;
    return ((void *)0);
}

canonical_original_code:
char *refl_get_annotation(unsigned long func, enum refl_annotation_type type)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return ((void *)0);
    for (struct annotation_entry *ae = e->annotation_list; ae != ((void *)0); ae = ae->next)
        if (ae->annotation.type == type)
            return ae->annotation.data;
    return ((void *)0);
}

variable_types: struct annotation_entry { struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } annotation; struct annotation_entry *next; } *


decompiled_code:
bool __fastcall refl_each_annotation_next(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rax

v2 = refl_hash_find();
if ( !v2 )
return 0;
if ( *(_QWORD *)a2 )
v3 = *(_QWORD *)(*(_QWORD *)a2 + 24LL);
else
v3 = *(_QWORD *)(v2 + 48);
*(_QWORD *)a2 = v3;
return v3 != 0;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rax

v2 = refl_hash_find();
if ( !v2 )
return 0;
if ( *(_QWORD *)a2 )
v3 = *(_QWORD *)(*(_QWORD *)a2 + 24LL);
else
v3 = *(_QWORD *)(v2 + 48);
*(_QWORD *)a2 = v3;
return v3 != 0;
}

original_code:
_Bool refl_each_annotation_next(func_t func, const struct refl_annotation **anno)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return 0;
    if (!*anno)
        *anno = &e->annotation_list->annotation;
    else
        *anno = &((const struct annotation_entry *)*anno)->next->annotation;
    return *anno != ((void *)0);
}

canonical_original_code:
_Bool refl_each_annotation_next(unsigned long func, struct refl_annotation **anno)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return 0;
    if (!*anno)
        *anno = &e->annotation_list->annotation;
    else
        *anno = &((struct annotation_entry *)*anno)->next->annotation;
    return *anno != ((void *)0);
}

variable_types: struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } **


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

result = refl_hash_find();
if ( result )
result = _strtab + *(unsigned int *)(_symtab + 24LL * *(int *)(result + 16));
return result;
}

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

result = refl_hash_find();
if ( result )
result = _strtab + *(unsigned int *)(_symtab + 24LL * *(int *)(result + 16));
return result;
}

original_code:
const char *refl_get_name(func_t func)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    return e ? __strtab + __symtab[e->symtab_idx].st_name : ((void *)0);
}

canonical_original_code:
char *refl_get_name(unsigned long func)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    return e ? __strtab + __symtab[e->symtab_idx].st_name : ((void *)0);
}


decompiled_code:
__int64 refl_get_target()
{
__int64 v0; // rax
__int64 v1; // rdx

v0 = refl_hash_find();
v1 = 0LL;
if ( v0 )
v1 = *(_QWORD *)(*(_QWORD *)(v0 + 40) + 72LL);
return v1;
}

original_code:
func_t refl_get_target(func_t func)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    return e ? e->trampoline->target : 0;
}

canonical_original_code:
unsigned long refl_get_target(unsigned long func)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    return e ? e->trampoline->target : 0;
}


main

repo: XanClic/standard

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int result; // eax
const char **v5; // rbx
__int64 v6; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

longind[0] = 0;
v3 = getopt_long(argc, (char *const *)argv, "hv", &long_options, longind);
if ( v3 == -1 )
{
term_init();
load_config();
if ( argc <= 1 )
{
new_buffer();
LABEL_11:
editor();
result = 0;
}
else
{
v5 = argv + 1;
while ( 1 )
{
v6 = new_buffer();
if ( !(unsigned __int8)buffer_load(v6, *v5) )
break;
if ( ++v5 == &argv[(unsigned int)(argc - 2) + 2] )
goto LABEL_11;
}
term_release();
fprintf(stderr, format, *v5);
result = 1;
}
}
else if ( v3 == 104 )
{
puts("Usage: std FILE\n--help, -h Show this help\n--version, -v Show current version");
result = 0;
}
else
{
if ( v3 == 118 )
puts(aStandard00);
result = 0;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
    int c;
    int option_index = 0;
    while ((c = getopt_long(argc, argv, "hv", long_options, &option_index)) != -1)
    {
        switch (c)
        {
            case 'h':
                printf("Usage: std FILE\n"
                        "--help, -h     Show this help\n"
                        "--version, -v  Show current version\n");
                return 0;
            case 'v':
                printf("standard " "0.0.元気" ": The standard text editor.\n"
                       "No need for ed anymore.\n");
                return 0;
            default:
                return 0;
        }
    }
    term_init();
    load_config();
    if (argc < 2)
        new_buffer();
    else
    {
        for (int i = 1; i < argc; i++)
        {
            buffer_t *buf = new_buffer();
            if (!buffer_load(buf, argv[i]))
            {
                term_release();
                fprintf(stderr, "Could not load “%s”.\n", argv[i]);
                return 1;
            }
        }
    }
    editor();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int c;
    int option_index = 0;
    while ((c = getopt_long(argc, argv, "hv", long_options, &option_index)) != -1)
    {
        switch (c)
        {
            case 'h':
                printf("Usage: std FILE\n"
                        "--help, -h     Show this help\n"
                        "--version, -v  Show current version\n");
                return 0;
            case 'v':
                printf("standard " "0.0.元気" ": The standard text editor.\n"
                       "No need for ed anymore.\n");
                return 0;
            default:
                return 0;
        }
    }
    term_init();
    load_config();
    if (argc < 2)
        new_buffer();
    else
    {
        for (int i = 1; i < argc; i++)
        {
            struct buffer *buf = new_buffer();
            if (!buffer_load(buf, argv[i]))
            {
                term_release();
                fprintf(stderr, "Could not load “%s”.\n", argv[i]);
                return 1;
            }
        }
    }
    editor();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types: i int

variable_types: struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *


print

decompiled_code:
size_t __fastcall print(void *ptr)
{
size_t v1; // rax

v1 = strlen((const char *)ptr);
return fwrite(ptr, 1uLL, v1, stdout);
}

canonical_decompiled_code:
size_t __fastcall func0(void *ptr)
{
size_t v1; // rax

v1 = strlen((const char *)ptr);
return fwrite(ptr, 1uLL, v1, stdout);
}

original_code:
void print(const char *s)
{
    fwrite(s, 1, strlen(s), stdout);
}

canonical_original_code:
void print(char *s)
{
    fwrite(s, 1, strlen(s), stdout);
}


decompiled_code:
int __fastcall print_flushed(void *a1)
{
print(a1);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func1(void *a1)
{
func0(a1);
return fflush(stdout);
}

original_code:
void print_flushed(const char *s)
{
    print(s);
    fflush(stdout);
}

canonical_original_code:
void print_flushed(char *s)
{
    print(s);
    fflush(stdout);
}


decompiled_code:
size_t term_clear()
{
return print("\x1B[2J\x1B[H");
}

canonical_decompiled_code:
size_t func2()
{
return func0("\x1B[2J\x1B[H");
}

original_code:
void term_clear(void)
{
    print("\033[2J\033[H");
}


decompiled_code:
size_t __fastcall term_underline(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[4m";
if ( v1 )
v2 = "\x1B[24m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func3(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[4m";
if ( v1 )
v2 = "\x1B[24m";
return func0(v2);
}

original_code:
void term_underline(_Bool ul)
{
    print(ul ? "\033[4m" : "\033[24m");
}


decompiled_code:
size_t __fastcall term_bold(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[1m";
if ( v1 )
v2 = "\x1B[22m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func4(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[1m";
if ( v1 )
v2 = "\x1B[22m";
return func0(v2);
}

original_code:
void term_bold(_Bool bold)
{
    print(bold ? "\033[1m" : "\033[22m");
}


decompiled_code:
int __fastcall term_set_color(__int64 a1, __int64 a2)
{
int result; // eax

switch ( (int)a1 )
{
case 0:
print("\x1B[22;39m");
break;
case 1:
printf("\x1B[3%im", BYTE4(a1) & 7);
break;
case 2:
if ( (a1 & 0x800000000LL) != 0 )
printf("\x1B[1;3%im", BYTE4(a1) & 7);
else
printf("\x1B[22;3%im", BYTE4(a1) & 7);
break;
case 3:
printf("\x1B[38;5;%im", BYTE4(a1));
break;
case 4:
printf("\x1B[38;2;%i;%i;%im", BYTE4(a1), BYTE5(a1), BYTE6(a1));
break;
default:
break;
}
result = a2;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", BYTE4(a2));
if ( (unsigned int)a2 > 3 )
{
if ( (_DWORD)a2 == 4 )
result = printf("\x1B[48;2;%i;%i;%im", BYTE4(a2), BYTE5(a2), BYTE6(a2));
}
else if ( (_DWORD)a2 )
{
result = printf("\x1B[4%im", BYTE4(a2) & 7);
}
else
{
result = print("\x1B[49m");
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2)
{
int result; // eax

switch ( (int)a1 )
{
case 0:
func0("\x1B[22;39m");
break;
case 1:
printf("\x1B[3%im", BYTE4(a1) & 7);
break;
case 2:
if ( (a1 & 0x800000000LL) != 0 )
printf("\x1B[1;3%im", BYTE4(a1) & 7);
else
printf("\x1B[22;3%im", BYTE4(a1) & 7);
break;
case 3:
printf("\x1B[38;5;%im", BYTE4(a1));
break;
case 4:
printf("\x1B[38;2;%i;%i;%im", BYTE4(a1), BYTE5(a1), BYTE6(a1));
break;
default:
break;
}
result = a2;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", BYTE4(a2));
if ( (unsigned int)a2 > 3 )
{
if ( (_DWORD)a2 == 4 )
result = printf("\x1B[48;2;%i;%i;%im", BYTE4(a2), BYTE5(a2), BYTE6(a2));
}
else if ( (_DWORD)a2 )
{
result = printf("\x1B[4%im", BYTE4(a2) & 7);
}
else
{
result = func0("\x1B[49m");
}
return result;
}

original_code:
void term_set_color(color_t fg, color_t bg)
{
    switch (fg.type)
    {
        case COL_DEFAULT:
            print("\033[22;39m");
            break;
        case COL_8:
            printf("\033[3%im", fg.color & 7);
            break;
        case COL_16:
            if (fg.color & 8)
                printf("\033[1;3%im", fg.color & 7);
            else
                printf("\033[22;3%im", fg.color & 7);
            break;
        case COL_256:
            printf("\033[38;5;%im", fg.color);
            break;
        case COL_RGB:
            printf("\033[38;2;%i;%i;%im", fg.r, fg.g, fg.b);
    }
    switch (bg.type)
    {
        case COL_DEFAULT:
            print("\033[49m");
            break;
        case COL_8:
        case COL_16:
            printf("\033[4%im", bg.color & 7);
            break;
        case COL_256:
            printf("\033[48;5;%im", bg.color);
            break;
        case COL_RGB:
            printf("\033[48;2;%i;%i;%im", bg.r, bg.g, bg.b);
    }
}

canonical_original_code:
void term_set_color(struct color fg, struct color bg)
{
    switch (fg.type)
    {
        case COL_DEFAULT:
            print("\033[22;39m");
            break;
        case COL_8:
            printf("\033[3%im", fg.color & 7);
            break;
        case COL_16:
            if (fg.color & 8)
                printf("\033[1;3%im", fg.color & 7);
            else
                printf("\033[22;3%im", fg.color & 7);
            break;
        case COL_256:
            printf("\033[38;5;%im", fg.color);
            break;
        case COL_RGB:
            printf("\033[38;2;%i;%i;%im", fg.r, fg.g, fg.b);
    }
    switch (bg.type)
    {
        case COL_DEFAULT:
            print("\033[49m");
            break;
        case COL_8:
        case COL_16:
            printf("\033[4%im", bg.color & 7);
            break;
        case COL_256:
            printf("\033[48;5;%im", bg.color);
            break;
        case COL_RGB:
            printf("\033[48;2;%i;%i;%im", bg.r, bg.g, bg.b);
    }
}

variable_types: struct color { enum color_type { COL_DEFAULT=0, COL_8=1, COL_16=2, COL_256=3, COL_RGB=4 } type; union { unsigned char color; struct { unsigned char r; }; }; }

variable_types:
bg
struct color

user_defined_types: struct color { enum color_type type; union { unsigned char color; struct { unsigned char r; }; }; }


decompiled_code:
size_t __fastcall term_invert(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[7m";
if ( v1 )
v2 = "\x1B[27m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func6(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[7m";
if ( v1 )
v2 = "\x1B[27m";
return func0(v2);
}

original_code:
void term_invert(_Bool invert)
{
    print(invert ? "\033[7m" : "\033[27m");
}


decompiled_code:
int __fastcall term_cursor_pos(int a1, int a2)
{
printf("\x1B[%i;%iH", (unsigned int)(a1 / term_width + a2 + 1), (unsigned int)(a1 % term_width + 1));
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2)
{
printf("\x1B[%i;%iH", (unsigned int)(a1 / term_width + a2 + 1), (unsigned int)(a1 % term_width + 1));
return fflush(stdout);
}

original_code:
void term_cursor_pos(int x, int y)
{
    y += x / term_width;
    x %= term_width;
    printf("\033[%i;%iH", y + 1, x + 1);
    fflush(stdout);
}


decompiled_code:
int __fastcall term_show_cursor(char a1)
{
return printf("\x1B[?25%c", a1 == 0 ? 108 : 104);
}

canonical_decompiled_code:
int __fastcall func8(char a1)
{
return printf("\x1B[?25%c", a1 == 0 ? 108 : 104);
}

original_code:
void term_show_cursor(_Bool show)
{
    printf("\033[?25%c", show ? 'h' : 'l');
}