decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er13
int v5; // ebp
signed int v6; // ebx

v3 = -999;
v4 = 998001;
v5 = 0;
do
{
v6 = v4;
if ( v4 >= v5 )
{
while ( !(unsigned int)is_palindromic(v6) )
{
v6 += v3;
if ( v6 < v5 )
goto LABEL_3;
}
v5 = v6;
}
LABEL_3:
v4 -= 999;
++v3;
}
while ( v5 < v4 );
printf("Largest is %d\n", (unsigned int)v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er13
int v5; // ebp
signed int v6; // ebx

v3 = -999;
v4 = 998001;
v5 = 0;
do
{
v6 = v4;
if ( v4 >= v5 )
{
while ( !(unsigned int)func7(v6) )
{
v6 += v3;
if ( v6 < v5 )
goto LABEL_3;
}
v5 = v6;
}
LABEL_3:
v4 -= 999;
++v3;
}
while ( v5 < v4 );
printf("Largest is %d\n", (unsigned int)v5);
return 0;
}

original_code:
int main() {
 int num1;
 int result = 0;
 for (num1 = 999; result < num1 * 999; num1--) {
  int num2 = 999;
  do {
   int tmp_result = num1 * num2;
   if (tmp_result < result) break;
   if (is_palindromic(tmp_result)) {
    result = tmp_result;
    break;
   }
   num2--;
  } while (1);
 }
 printf("Largest is %d\n", result);
}

variable_types: result int


sort

decompiled_code:
void __fastcall sort(__int64 a1, unsigned int a2, int a3)
{
int v3; // ebx
unsigned int v4; // er12
const char *v5; // r15
unsigned int v6; // ebx
unsigned int v7; // [rsp+4h] [rbp-44h]
__int64 *v8; // [rsp+8h] [rbp-40h]

v7 = a3;
v3 = a2 + 1;
if ( (int)(a2 + 1) < a3 )
{
v4 = a3;
v8 = (__int64 *)(a1 + 8LL * (int)a2);
v5 = (const char *)*v8;
do
{
if ( strcmp(*(const char **)(a1 + 8LL * v3), v5) > 0 )
swap((__int64 *)(a1 + 8LL * v3), (__int64 *)(a1 + 8LL * (int)--v4));
else
++v3;
}
while ( (int)v4 > v3 );
v6 = v3 - 1;
swap((__int64 *)(a1 + 8LL * (int)v6), v8);
sort(a1, a2, v6);
sort(a1, v4, v7);
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, unsigned int a2, int a3)
{
int v3; // ebx
unsigned int v4; // er12
const char *v5; // r15
unsigned int v6; // ebx
unsigned int v7; // [rsp+4h] [rbp-44h]
__int64 *v8; // [rsp+8h] [rbp-40h]

v7 = a3;
v3 = a2 + 1;
if ( (int)(a2 + 1) < a3 )
{
v4 = a3;
v8 = (__int64 *)(a1 + 8LL * (int)a2);
v5 = (const char *)*v8;
do
{
if ( strcmp(*(const char **)(a1 + 8LL * v3), v5) > 0 )
func7((__int64 *)(a1 + 8LL * v3), (__int64 *)(a1 + 8LL * (int)--v4));
else
++v3;
}
while ( (int)v4 > v3 );
v6 = v3 - 1;
func7((__int64 *)(a1 + 8LL * (int)v6), v8);
func8(a1, a2, v6);
func8(a1, v4, v7);
}
}

original_code:
void sort(char* arr[], int beg, int end)
{
  if (end > beg + 1)
  {
    char* piv = arr[beg];
    int l = beg + 1, r = end;
    while (l < r)
    {
      if (strcmp(arr[l],piv) <= 0)
        l++;
      else
        swap(&arr[l], &arr[--r]);
    }
    swap(&arr[--l], &arr[beg]);
    sort(arr, beg, l);
    sort(arr, r, end);
  }
}

canonical_original_code:
void sort(char *arr[], int beg, int end)
{
  if (end > beg + 1)
  {
    char *piv = arr[beg];
    int l = beg + 1; int r = end;
    while (l < r)
    {
      if (strcmp(arr[l],piv) <= 0)
        l++;
      else
        swap(&arr[l], &arr[--r]);
    }
    swap(&arr[--l], &arr[beg]);
    sort(arr, beg, l);
    sort(arr, r, end);
  }
}

variable_types:
arr
char *[]

variable_types: beg int

variable_types: end int

variable_types:
piv
char *

variable_types: l int

variable_types: r int

return_type: void


decompiled_code:
__int64 __fastcall alphabetical_value(const char *a1)
{
int v1; // eax
const char *v2; // rdx
__int64 v3; // rcx

v1 = strlen(a1);
if ( v1 <= 0 )
return 0LL;
v2 = a1;
v3 = 0LL;
do
v3 += *v2++ - 64;
while ( v2 != &a1[v1 - 1 + 1] );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
int v1; // eax
const char *v2; // rdx
__int64 v3; // rcx

v1 = strlen(a1);
if ( v1 <= 0 )
return 0LL;
v2 = a1;
v3 = 0LL;
do
v3 += *v2++ - 64;
while ( v2 != &a1[v1 - 1 + 1] );
return v3;
}

original_code:
unsigned long long alphabetical_value(char* name) {
 unsigned long long total = 0;
 int i, c = strlen(name);
 for (i=0;i<c;++i) {
  total += name[i] - 64;
 }
 return total;
}

canonical_original_code:
unsigned long long alphabetical_value(char *name) {
 unsigned long long total = 0;
 int i; int c = strlen(name);
 for (i=0;i<c;++i) {
  total += name[i] - 64;
 }
 return total;
}

variable_types:
name
char *

variable_types:
total
unsigned long long

variable_types: i int

variable_types: c int

return_type: unsigned long long


combinatorics

decompiled_code:
unsigned __int64 __fastcall combinatorics(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int64 v3; // r12
unsigned __int64 v4; // rbp

v2 = a2;
v3 = a1 - a2;
if ( a1 - a2 >= a2 )
a2 = a1 - a2;
v4 = factorial(a1, a2);
if ( v3 <= v2 )
v2 = v3;
return v4 / factorial(v2, 1LL);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int64 v3; // r12
unsigned __int64 v4; // rbp

v2 = a2;
v3 = a1 - a2;
if ( a1 - a2 >= a2 )
a2 = a1 - a2;
v4 = func7(a1, a2);
if ( v3 <= v2 )
v2 = v3;
return v4 / func7(v2, 1LL);
}

original_code:
unsigned long long combinatorics(unsigned long long n, unsigned long long r) {
 return factorial(n, (r<n-r?n-r:r)) / factorial((r>n-r?n-r:r), 1);
}

variable_types:
n
unsigned long long

variable_types:
r
unsigned long long


factorial

decompiled_code:
__int64 __fastcall factorial(unsigned __int64 a1, __int64 a2)
{
__int64 result; // rax

if ( a1 == a2 )
return 1LL;
result = 1LL;
if ( a1 > 1 )
result = a1 * factorial(a1 - 1);
return result;
}

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

if ( a1 == a2 )
return 1LL;
result = 1LL;
if ( a1 > 1 )
result = a1 * func7(a1 - 1);
return result;
}

original_code:
unsigned long long factorial (unsigned long long n, unsigned long long upto) {
 if (n == upto || n < 2) return 1;
 return n * factorial(n-1, upto);
}

canonical_original_code:
unsigned long long factorial(unsigned long long n, unsigned long long upto) {
 if (n == upto || n < 2) return 1;
 return n * factorial(n-1, upto);
}

variable_types:
upto
unsigned long long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rbp
unsigned int v4; // er14
int v5; // ebx

v3 = 2LL;
v4 = 0;
while ( v3 != 101 )
{
v5 = 1;
if ( (int)v3 > 3 )
{
do
{
if ( combinatorics(v3, v5) > 0xF4240 )
{
v4 += v3 - 2 * v5 + 1;
v5 += v3;
}
++v5;
}
while ( v5 < (int)v3 / 2 );
}
++v3;
}
printf("%d\n", v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rbp
unsigned int v4; // er14
int v5; // ebx

v3 = 2LL;
v4 = 0;
while ( v3 != 101 )
{
v5 = 1;
if ( (int)v3 > 3 )
{
do
{
if ( func8(v3, v5) > 0xF4240 )
{
v4 += v3 - 2 * v5 + 1;
v5 += v3;
}
++v5;
}
while ( v5 < (int)v3 / 2 );
}
++v3;
}
printf("%d\n", v4);
return 0;
}

variable_types: j int

return_type: int


currency

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

switch ( a1 )
{
case 0:
result = 1LL;
break;
case 1:
result = 2LL;
break;
case 2:
result = 5LL;
break;
case 3:
result = 10LL;
break;
case 4:
result = 20LL;
break;
case 5:
result = 50LL;
break;
case 6:
result = 100LL;
break;
case 7:
result = 200LL;
break;
default:
result = 400LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
result = 1LL;
break;
case 1:
result = 2LL;
break;
case 2:
result = 5LL;
break;
case 3:
result = 10LL;
break;
case 4:
result = 20LL;
break;
case 5:
result = 50LL;
break;
case 6:
result = 100LL;
break;
case 7:
result = 200LL;
break;
default:
result = 400LL;
break;
}
return result;
}

original_code:
int currency(int pos) {
 switch (pos) {
  case 0: return 1;
  case 1: return 2;
  case 2: return 5;
  case 3: return 10;
  case 4: return 20;
  case 5: return 50;
  case 6: return 100;
  case 7: return 200;
 }
 return 400;
}

variable_types: pos int


main

original_code:
int main() {
 int a,b,c,d,e,f,g,h;
 int a_max,b_max,c_max,d_max,e_max,f_max,g_max,h_max;
 a_max = 200 / currency(0);
 b_max = 200 / currency(1);
 c_max = 200 / currency(2);
 d_max = 200 / currency(3);
 e_max = 200 / currency(4);
 f_max = 200 / currency(5);
 g_max = 200 / currency(6);
 h_max = 200 / currency(7);
 int counter = 0;
 for (a=0;a<=a_max;++a)
 for (b=0;b<=b_max;++b)
 for (c=0;c<=c_max;++c)
 for (d=0;d<=d_max;++d)
 for (e=0;e<=e_max;++e)
 for (f=0;f<=f_max;++f)
 for (g=0;g<=g_max;++g)
 for (h=0;h<=h_max;++h) {
  if (a*currency(0)+b*currency(1)+c*currency(2)+d*currency(3)+e*currency(4)+f*currency(5)+g*currency(6)+h*currency(7) == 200) {
   counter++;
  }
 }
 printf("%d\n", counter);
 return 0;
}

canonical_original_code:
int main() {
 int a; int b; int c; int d; int e; int f; int g; int h;
 int a_max; int b_max; int c_max; int d_max; int e_max; int f_max; int g_max; int h_max;
 a_max = 200 / currency(0);
 b_max = 200 / currency(1);
 c_max = 200 / currency(2);
 d_max = 200 / currency(3);
 e_max = 200 / currency(4);
 f_max = 200 / currency(5);
 g_max = 200 / currency(6);
 h_max = 200 / currency(7);
 int counter = 0;
 for (a=0;a<=a_max;++a)
 for (b=0;b<=b_max;++b)
 for (c=0;c<=c_max;++c)
 for (d=0;d<=d_max;++d)
 for (e=0;e<=e_max;++e)
 for (f=0;f<=f_max;++f)
 for (g=0;g<=g_max;++g)
 for (h=0;h<=h_max;++h) {
  if (a*currency(0)+b*currency(1)+c*currency(2)+d*currency(3)+e*currency(4)+f*currency(5)+g*currency(6)+h*currency(7) == 200) {
   counter++;
  }
 }
 printf("%d\n", counter);
 return 0;
}

variable_types: a int

variable_types: b int

variable_types: d int

variable_types: e int

variable_types: f int

variable_types: g int

variable_types: h int

variable_types: counter int


yylex

original_code:
int
yylex( void )
    {
    int c, len, i;
    char buf[10000];
    do {
 c = getchar();
 }
    while ( c == ' ' || c == '\t' );
    if ( ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit) )
 {
 buf[0] = c;
 len = 1;
 while ( len < sizeof(buf) )
     {
     c = getchar();
     if ( ! ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit) )
  {
                ungetc( c, stdin );
                break;
                }
            buf[len++] = c;
            }
        buf[len] = '\0';
        yylval = str_to_bi( buf );
        return NUMBER;
        }
    else if ( ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) )
 {
 buf[0] = c;
 len = 1;
 while ( len < sizeof(buf) )
     {
     c = getchar();
     if ( ! ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) )
  {
                ungetc( c, stdin );
                break;
                }
            buf[len++] = c;
            }
        buf[len] = '\0';
 for ( i = 0; i < len; ++i )
     if ( ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISupper) )
  buf[i] = tolower( buf[i] );
        if ( strcmp( buf, "sqrt" ) == 0 )
     return SQRT;
        else if ( strcmp( buf, "gcd" ) == 0 )
     return GCD;
        else if ( strcmp( buf, "lcm" ) == 0 )
     return LCM;
        else if ( strcmp( buf, "modpow" ) == 0 )
     return MODPOW;
        else if ( strcmp( buf, "modinv" ) == 0 )
     return MODINV;
        else if ( strcmp( buf, "random" ) == 0 )
     return RANDOM;
        else if ( strcmp( buf, "jacobi" ) == 0 )
     return JACOBI;
        else if ( strcmp( buf, "isprime" ) == 0 )
     return ISPRIME;
        else if ( strcmp( buf, "genprime" ) == 0 )
     return GENPRIME;
        else if ( strcmp( buf, "bits" ) == 0 )
     return BITS;
        else if ( strcmp( buf, "bindiv" ) == 0 )
     return BINDIV;
 else if ( len == 1 )
     {
     yylval = int_to_bi( buf[0] - 'a' );
     return VAR;
     }
 else
     return '.';
        }
    else if ( c == '.' )
 {
 yylval = bi_copy( prev_val );
 return NUMBER;
 }
    else if ( c == (-1) )
 return 0;
    else
 return c;
    }

canonical_original_code:
int yylex()
    {
    int c; int len; int i;
    char buf[10000];
    do {
 c = getchar();
 }
    while ( c == ' ' || c == '\t' );
    if ( ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit) )
 {
 buf[0] = c;
 len = 1;
 while ( len < sizeof(buf) )
     {
     c = getchar();
     if ( ! ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit) )
  {
                ungetc( c, stdin );
                break;
                }
            buf[len++] = c;
            }
        buf[len] = '\0';
        yylval = str_to_bi( buf );
        return NUMBER;
        }
    else if ( ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) )
 {
 buf[0] = c;
 len = 1;
 while ( len < sizeof(buf) )
     {
     c = getchar();
     if ( ! ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) )
  {
                ungetc( c, stdin );
                break;
                }
            buf[len++] = c;
            }
        buf[len] = '\0';
 for ( i = 0; i < len; ++i )
     if ( ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISupper) )
  buf[i] = tolower( buf[i] );
        if ( strcmp( buf, "sqrt" ) == 0 )
     return SQRT;
        else if ( strcmp( buf, "gcd" ) == 0 )
     return GCD;
        else if ( strcmp( buf, "lcm" ) == 0 )
     return LCM;
        else if ( strcmp( buf, "modpow" ) == 0 )
     return MODPOW;
        else if ( strcmp( buf, "modinv" ) == 0 )
     return MODINV;
        else if ( strcmp( buf, "random" ) == 0 )
     return RANDOM;
        else if ( strcmp( buf, "jacobi" ) == 0 )
     return JACOBI;
        else if ( strcmp( buf, "isprime" ) == 0 )
     return ISPRIME;
        else if ( strcmp( buf, "genprime" ) == 0 )
     return GENPRIME;
        else if ( strcmp( buf, "bits" ) == 0 )
     return BITS;
        else if ( strcmp( buf, "bindiv" ) == 0 )
     return BINDIV;
 else if ( len == 1 )
     {
     yylval = int_to_bi( buf[0] - 'a' );
     return VAR;
     }
 else
     return '.';
        }
    else if ( c == '.' )
 {
 yylval = bi_copy( prev_val );
 return NUMBER;
 }
    else if ( c == (-1) )
 return 0;
    else
 return c;
    }

variable_types: len int

variable_types:
buf
char [10000]


repo: sergei/wiiuse

decompiled_code:
__int64 __fastcall calculate_gforce(unsigned __int8 *a1, unsigned __int8 *a2, float *a3)
{
float v3; // xmm2_4
float v4; // xmm1_4
__int64 result; // rax

v3 = (float)a1[4];
v4 = (float)a1[5];
*a3 = (float)((float)*a2 - (float)*a1) / (float)a1[3];
result = a1[1];
a3[1] = (float)((float)a2[1] - (float)(int)result) / v3;
a3[2] = (float)((float)a2[2] - (float)a1[2]) / v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, unsigned __int8 *a2, float *a3)
{
float v3; // xmm2_4
float v4; // xmm1_4
__int64 result; // rax

v3 = (float)a1[4];
v4 = (float)a1[5];
*a3 = (float)((float)*a2 - (float)*a1) / (float)a1[3];
result = a1[1];
a3[1] = (float)((float)a2[1] - (float)(int)result) / v3;
a3[2] = (float)((float)a2[2] - (float)a1[2]) / v4;
return result;
}

original_code:
void calculate_gforce(struct accel_t* ac, struct vec3b_t* accel, struct gforce_t* gforce) {
 float xg, yg, zg;
 xg = (float)ac->cal_g.x;
 yg = (float)ac->cal_g.y;
 zg = (float)ac->cal_g.z;
 gforce->x = ((float)accel->x - (float)ac->cal_zero.x) / xg;
 gforce->y = ((float)accel->y - (float)ac->cal_zero.y) / yg;
 gforce->z = ((float)accel->z - (float)ac->cal_zero.z) / zg;
}

canonical_original_code:
void calculate_gforce(struct accel_t *ac, struct vec3b_t *accel, struct gforce_t *gforce) {
 float xg; float yg; float zg;
 xg = (float)ac->cal_g.x;
 yg = (float)ac->cal_g.y;
 zg = (float)ac->cal_g.z;
 gforce->x = ((float)accel->x - (float)ac->cal_zero.x) / xg;
 gforce->y = ((float)accel->y - (float)ac->cal_zero.y) / yg;
 gforce->z = ((float)accel->z - (float)ac->cal_zero.z) / zg;
}

variable_types: struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } *

variable_types:
accel
struct vec3b_t *

variable_types:
gforce
struct gforce_t { float x; } *

variable_types: xg float

variable_types: yg float

variable_types: zg float

user_defined_types: struct accel_t { struct vec3b_t cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; }


original_code:
void calc_joystick_state(struct joystick_t* js, float x, float y) {
 float rx, ry, ang;
 if (x == js->center.x)
  rx = 0;
 else if (x >= js->center.x)
  rx = ((float)(x - js->center.x) / (float)(js->max.x - js->center.x));
 else
  rx = ((float)(x - js->min.x) / (float)(js->center.x - js->min.x)) - 1.0f;
 if (y == js->center.y)
  ry = 0;
 else if (y >= js->center.y)
  ry = ((float)(y - js->center.y) / (float)(js->max.y - js->center.y));
 else
  ry = ((float)(y - js->min.y) / (float)(js->center.y - js->min.y)) - 1.0f;
 ang = ((atanf(ry / rx) * 180.0f) / 3.14159265f);
 ang -= 90.0f;
 if (rx < 0.0f)
  ang -= 180.0f;
 js->ang = ((ang >= 0) ? (ang) : (ang * -1.0f));
 js->mag = (float) sqrt((rx * rx) + (ry * ry));
}

canonical_original_code:
void calc_joystick_state(struct joystick_t *js, float x, float y) {
 float rx; float ry; float ang;
 if (x == js->center.x)
  rx = 0;
 else if (x >= js->center.x)
  rx = ((float)(x - js->center.x) / (float)(js->max.x - js->center.x));
 else
  rx = ((float)(x - js->min.x) / (float)(js->center.x - js->min.x)) - 1.0f;
 if (y == js->center.y)
  ry = 0;
 else if (y >= js->center.y)
  ry = ((float)(y - js->center.y) / (float)(js->max.y - js->center.y));
 else
  ry = ((float)(y - js->min.y) / (float)(js->center.y - js->min.y)) - 1.0f;
 ang = ((atanf(ry / rx) * 180.0f) / 3.14159265f);
 ang -= 90.0f;
 if (rx < 0.0f)
  ang -= 180.0f;
 js->ang = ((ang >= 0) ? (ang) : (ang * -1.0f));
 js->mag = (float) sqrt((rx * rx) + (ry * ry));
}

variable_types: struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } *

variable_types: x float

variable_types: y float

variable_types: rx float

variable_types: ry float

variable_types: ang float

user_defined_types: struct joystick_t { struct vec2b_t max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; }


original_code:
void apply_smoothing(struct accel_t* ac, struct orient_t* orient, int type) {
 switch (type) {
  case 0x01:
  {
   if (__builtin_isnan (ac->st_roll) || __builtin_isinf_sign (ac->st_roll))
    ac->st_roll = 0.0f;
   if (((ac->st_roll < 0) && (orient->roll > 0)) || ((ac->st_roll > 0) && (orient->roll < 0))) {
    ac->st_roll = orient->roll;
   } else {
    orient->roll = ac->st_roll + (ac->st_alpha * (orient->a_roll - ac->st_roll));
    ac->st_roll = orient->roll;
   }
   return;
  }
  case 0x02:
  {
   if (__builtin_isnan (ac->st_pitch) || __builtin_isinf_sign (ac->st_pitch))
    ac->st_pitch = 0.0f;
   if (((ac->st_pitch < 0) && (orient->pitch > 0)) || ((ac->st_pitch > 0) && (orient->pitch < 0))) {
    ac->st_pitch = orient->pitch;
   } else {
    orient->pitch = ac->st_pitch + (ac->st_alpha * (orient->a_pitch - ac->st_pitch));
    ac->st_pitch = orient->pitch;
   }
   return;
  }
 }
}

canonical_original_code:
void apply_smoothing(struct accel_t *ac, struct orient_t *orient, int type) {
 switch (type) {
  case 0x01:
  {
   if (__builtin_isnan (ac->st_roll) || __builtin_isinf_sign (ac->st_roll))
    ac->st_roll = 0.0f;
   if (((ac->st_roll < 0) && (orient->roll > 0)) || ((ac->st_roll > 0) && (orient->roll < 0))) {
    ac->st_roll = orient->roll;
   } else {
    orient->roll = ac->st_roll + (ac->st_alpha * (orient->a_roll - ac->st_roll));
    ac->st_roll = orient->roll;
   }
   return;
  }
  case 0x02:
  {
   if (__builtin_isnan (ac->st_pitch) || __builtin_isinf_sign (ac->st_pitch))
    ac->st_pitch = 0.0f;
   if (((ac->st_pitch < 0) && (orient->pitch > 0)) || ((ac->st_pitch > 0) && (orient->pitch < 0))) {
    ac->st_pitch = orient->pitch;
   } else {
    orient->pitch = ac->st_pitch + (ac->st_alpha * (orient->a_pitch - ac->st_pitch));
    ac->st_pitch = orient->pitch;
   }
   return;
  }
 }
}

variable_types: struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } *

variable_types: type int


original_code:
void calculate_orientation(struct accel_t* ac, struct vec3b_t* accel, struct orient_t* orient, int smooth) {
 float xg, yg, zg;
 float x, y, z;
 orient->yaw = 0.0f;
 xg = (float)ac->cal_g.x;
 yg = (float)ac->cal_g.y;
 zg = (float)ac->cal_g.z;
 x = ((float)accel->x - (float)ac->cal_zero.x) / xg;
 y = ((float)accel->y - (float)ac->cal_zero.y) / yg;
 z = ((float)accel->z - (float)ac->cal_zero.z) / zg;
 if (x < -1.0f) x = -1.0f;
 else if (x > 1.0f) x = 1.0f;
 if (y < -1.0f) y = -1.0f;
 else if (y > 1.0f) y = 1.0f;
 if (z < -1.0f) z = -1.0f;
 else if (z > 1.0f) z = 1.0f;
 if (abs(accel->x - ac->cal_zero.x) <= ac->cal_g.x) {
  x = ((atan2f(x, z) * 180.0f) / 3.14159265f);
  orient->roll = x;
  orient->a_roll = x;
 }
 if (abs(accel->y - ac->cal_zero.y) <= ac->cal_g.y) {
  y = ((atan2f(y, z) * 180.0f) / 3.14159265f);
  orient->pitch = y;
  orient->a_pitch = y;
 }
 if (smooth) {
  apply_smoothing(ac, orient, 0x01);
  apply_smoothing(ac, orient, 0x02);
 }
}

canonical_original_code:
void calculate_orientation(struct accel_t *ac, struct vec3b_t *accel, struct orient_t *orient, int smooth) {
 float xg; float yg; float zg;
 float x; float y; float z;
 orient->yaw = 0.0f;
 xg = (float)ac->cal_g.x;
 yg = (float)ac->cal_g.y;
 zg = (float)ac->cal_g.z;
 x = ((float)accel->x - (float)ac->cal_zero.x) / xg;
 y = ((float)accel->y - (float)ac->cal_zero.y) / yg;
 z = ((float)accel->z - (float)ac->cal_zero.z) / zg;
 if (x < -1.0f) x = -1.0f;
 else if (x > 1.0f) x = 1.0f;
 if (y < -1.0f) y = -1.0f;
 else if (y > 1.0f) y = 1.0f;
 if (z < -1.0f) z = -1.0f;
 else if (z > 1.0f) z = 1.0f;
 if (abs(accel->x - ac->cal_zero.x) <= ac->cal_g.x) {
  x = ((atan2f(x, z) * 180.0f) / 3.14159265f);
  orient->roll = x;
  orient->a_roll = x;
 }
 if (abs(accel->y - ac->cal_zero.y) <= ac->cal_g.y) {
  y = ((atan2f(y, z) * 180.0f) / 3.14159265f);
  orient->pitch = y;
  orient->a_pitch = y;
 }
 if (smooth) {
  apply_smoothing(ac, orient, 0x01);
  apply_smoothing(ac, orient, 0x02);
 }
}

variable_types: smooth int

variable_types: z float

user_defined_types: struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; }


original_code:
void handle_event(struct wiimote_t* wm) {
 printf("\n\n--- EVENT [id %i] ---\n", wm->unid);
 if (((wm->btns & 0x0008) == 0x0008)) printf("A pressed\n");
 if (((wm->btns & 0x0004) == 0x0004)) printf("B pressed\n");
 if (((wm->btns & 0x0800) == 0x0800)) printf("UP pressed\n");
 if (((wm->btns & 0x0400) == 0x0400)) printf("DOWN pressed\n");
 if (((wm->btns & 0x0100) == 0x0100)) printf("LEFT pressed\n");
 if (((wm->btns & 0x0200) == 0x0200)) printf("RIGHT pressed\n");
 if (((wm->btns & 0x0010) == 0x0010)) printf("MINUS pressed\n");
 if (((wm->btns & 0x1000) == 0x1000)) printf("PLUS pressed\n");
 if (((wm->btns & 0x0002) == 0x0002)) printf("ONE pressed\n");
 if (((wm->btns & 0x0001) == 0x0001)) printf("TWO pressed\n");
 if (((wm->btns & 0x0080) == 0x0080)) printf("HOME pressed\n");
 if ((((wm->btns & 0x0010) == 0x0010) && !((wm->btns_held & 0x0010) == 0x0010)))
  wiiuse_motion_sensing(wm, 0);
 if ((((wm->btns & 0x1000) == 0x1000) && !((wm->btns_held & 0x1000) == 0x1000)))
  wiiuse_motion_sensing(wm, 1);
 if ((((wm->btns & 0x0004) == 0x0004) && !((wm->btns_held & 0x0004) == 0x0004)))
  wiiuse_toggle_rumble(wm);
 if ((((wm->btns & 0x0800) == 0x0800) && !((wm->btns_held & 0x0800) == 0x0800)))
  wiiuse_set_ir(wm, 1);
 if ((((wm->btns & 0x0400) == 0x0400) && !((wm->btns_held & 0x0400) == 0x0400)))
  wiiuse_set_ir(wm, 0);
 if (((wm->state & 0x020) == 0x020)) {
  printf("wiimote roll  = %f [%f]\n", wm->orient.roll, wm->orient.a_roll);
  printf("wiimote pitch = %f [%f]\n", wm->orient.pitch, wm->orient.a_pitch);
  printf("wiimote yaw   = %f\n", wm->orient.yaw);
 }
 if (((wm->state & 0x080) == 0x080)) {
  int i = 0;
  for (; i < 4; ++i) {
   if (wm->ir.dot[i].visible)
    printf("IR source %i: (%u, %u)\n", i, wm->ir.dot[i].x, wm->ir.dot[i].y);
  }
  printf("IR cursor: (%u, %u)\n", wm->ir.x, wm->ir.y);
  printf("IR z distance: %f\n", wm->ir.z);
 }
 if (wm->exp.type == 1) {
  struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
  if (((nc->btns & 0x02) == 0x02)) printf("Nunchuk: C pressed\n");
  if (((nc->btns & 0x01) == 0x01)) printf("Nunchuk: Z pressed\n");
  printf("nunchuk roll  = %f\n", nc->orient.roll);
  printf("nunchuk pitch = %f\n", nc->orient.pitch);
  printf("nunchuk yaw   = %f\n", nc->orient.yaw);
  printf("nunchuk joystick angle:     %f\n", nc->js.ang);
  printf("nunchuk joystick magnitude: %f\n", nc->js.mag);
 } else if (wm->exp.type == 2) {
  struct classic_ctrl_t* cc = (classic_ctrl_t*)&wm->exp.classic;
  if (((cc->btns & 0x0080) == 0x0080)) printf("Classic: ZL pressed\n");
  if (((cc->btns & 0x0040) == 0x0040)) printf("Classic: B pressed\n");
  if (((cc->btns & 0x0020) == 0x0020)) printf("Classic: Y pressed\n");
  if (((cc->btns & 0x0010) == 0x0010)) printf("Classic: A pressed\n");
  if (((cc->btns & 0x0008) == 0x0008)) printf("Classic: X pressed\n");
  if (((cc->btns & 0x0004) == 0x0004)) printf("Classic: ZR pressed\n");
  if (((cc->btns & 0x0002) == 0x0002)) printf("Classic: LEFT pressed\n");
  if (((cc->btns & 0x0001) == 0x0001)) printf("Classic: UP pressed\n");
  if (((cc->btns & 0x8000) == 0x8000)) printf("Classic: RIGHT pressed\n");
  if (((cc->btns & 0x4000) == 0x4000)) printf("Classic: DOWN pressed\n");
  if (((cc->btns & 0x2000) == 0x2000)) printf("Classic: FULL L pressed\n");
  if (((cc->btns & 0x1000) == 0x1000)) printf("Classic: MINUS pressed\n");
  if (((cc->btns & 0x0800) == 0x0800)) printf("Classic: HOME pressed\n");
  if (((cc->btns & 0x0400) == 0x0400)) printf("Classic: PLUS pressed\n");
  if (((cc->btns & 0x0200) == 0x0200)) printf("Classic: FULL R pressed\n");
  printf("classic L button pressed:         %f\n", cc->l_shoulder);
  printf("classic R button pressed:         %f\n", cc->r_shoulder);
  printf("classic left joystick angle:      %f\n", cc->ljs.ang);
  printf("classic left joystick magnitude:  %f\n", cc->ljs.mag);
  printf("classic right joystick angle:     %f\n", cc->rjs.ang);
  printf("classic right joystick magnitude: %f\n", cc->rjs.mag);
 } else if (wm->exp.type == 3) {
  struct guitar_hero_3_t* gh3 = (guitar_hero_3_t*)&wm->exp.gh3;
  if (((gh3->btns & 0x0001) == 0x0001)) printf("Guitar: Strum Up pressed\n");
  if (((gh3->btns & 0x4000) == 0x4000)) printf("Guitar: Strum Down pressed\n");
  if (((gh3->btns & 0x0008) == 0x0008)) printf("Guitar: Yellow pressed\n");
  if (((gh3->btns & 0x0010) == 0x0010)) printf("Guitar: Green pressed\n");
  if (((gh3->btns & 0x0020) == 0x0020)) printf("Guitar: Blue pressed\n");
  if (((gh3->btns & 0x0040) == 0x0040)) printf("Guitar: Red pressed\n");
  if (((gh3->btns & 0x0080) == 0x0080)) printf("Guitar: Orange pressed\n");
  if (((gh3->btns & 0x0400) == 0x0400)) printf("Guitar: Plus pressed\n");
  if (((gh3->btns & 0x1000) == 0x1000)) printf("Guitar: Minus pressed\n");
  printf("Guitar whammy bar:          %f\n", gh3->whammy_bar);
  printf("Guitar joystick angle:      %f\n", gh3->js.ang);
  printf("Guitar joystick magnitude:  %f\n", gh3->js.mag);
 }
}

canonical_original_code:
void handle_event(struct wiimote_t *wm) {
 printf("\n\n--- EVENT [id %i] ---\n", wm->unid);
 if (((wm->btns & 0x0008) == 0x0008)) printf("A pressed\n");
 if (((wm->btns & 0x0004) == 0x0004)) printf("B pressed\n");
 if (((wm->btns & 0x0800) == 0x0800)) printf("UP pressed\n");
 if (((wm->btns & 0x0400) == 0x0400)) printf("DOWN pressed\n");
 if (((wm->btns & 0x0100) == 0x0100)) printf("LEFT pressed\n");
 if (((wm->btns & 0x0200) == 0x0200)) printf("RIGHT pressed\n");
 if (((wm->btns & 0x0010) == 0x0010)) printf("MINUS pressed\n");
 if (((wm->btns & 0x1000) == 0x1000)) printf("PLUS pressed\n");
 if (((wm->btns & 0x0002) == 0x0002)) printf("ONE pressed\n");
 if (((wm->btns & 0x0001) == 0x0001)) printf("TWO pressed\n");
 if (((wm->btns & 0x0080) == 0x0080)) printf("HOME pressed\n");
 if ((((wm->btns & 0x0010) == 0x0010) && !((wm->btns_held & 0x0010) == 0x0010)))
  wiiuse_motion_sensing(wm, 0);
 if ((((wm->btns & 0x1000) == 0x1000) && !((wm->btns_held & 0x1000) == 0x1000)))
  wiiuse_motion_sensing(wm, 1);
 if ((((wm->btns & 0x0004) == 0x0004) && !((wm->btns_held & 0x0004) == 0x0004)))
  wiiuse_toggle_rumble(wm);
 if ((((wm->btns & 0x0800) == 0x0800) && !((wm->btns_held & 0x0800) == 0x0800)))
  wiiuse_set_ir(wm, 1);
 if ((((wm->btns & 0x0400) == 0x0400) && !((wm->btns_held & 0x0400) == 0x0400)))
  wiiuse_set_ir(wm, 0);
 if (((wm->state & 0x020) == 0x020)) {
  printf("wiimote roll  = %f [%f]\n", wm->orient.roll, wm->orient.a_roll);
  printf("wiimote pitch = %f [%f]\n", wm->orient.pitch, wm->orient.a_pitch);
  printf("wiimote yaw   = %f\n", wm->orient.yaw);
 }
 if (((wm->state & 0x080) == 0x080)) {
  int i = 0;
  for (; i < 4; ++i) {
   if (wm->ir.dot[i].visible)
    printf("IR source %i: (%u, %u)\n", i, wm->ir.dot[i].x, wm->ir.dot[i].y);
  }
  printf("IR cursor: (%u, %u)\n", wm->ir.x, wm->ir.y);
  printf("IR z distance: %f\n", wm->ir.z);
 }
 if (wm->exp.type == 1) {
  struct nunchuk_t *nc = (nunchuk_t*)&wm->exp.nunchuk;
  if (((nc->btns & 0x02) == 0x02)) printf("Nunchuk: C pressed\n");
  if (((nc->btns & 0x01) == 0x01)) printf("Nunchuk: Z pressed\n");
  printf("nunchuk roll  = %f\n", nc->orient.roll);
  printf("nunchuk pitch = %f\n", nc->orient.pitch);
  printf("nunchuk yaw   = %f\n", nc->orient.yaw);
  printf("nunchuk joystick angle:     %f\n", nc->js.ang);
  printf("nunchuk joystick magnitude: %f\n", nc->js.mag);
 } else if (wm->exp.type == 2) {
  struct classic_ctrl_t *cc = (classic_ctrl_t*)&wm->exp.classic;
  if (((cc->btns & 0x0080) == 0x0080)) printf("Classic: ZL pressed\n");
  if (((cc->btns & 0x0040) == 0x0040)) printf("Classic: B pressed\n");
  if (((cc->btns & 0x0020) == 0x0020)) printf("Classic: Y pressed\n");
  if (((cc->btns & 0x0010) == 0x0010)) printf("Classic: A pressed\n");
  if (((cc->btns & 0x0008) == 0x0008)) printf("Classic: X pressed\n");
  if (((cc->btns & 0x0004) == 0x0004)) printf("Classic: ZR pressed\n");
  if (((cc->btns & 0x0002) == 0x0002)) printf("Classic: LEFT pressed\n");
  if (((cc->btns & 0x0001) == 0x0001)) printf("Classic: UP pressed\n");
  if (((cc->btns & 0x8000) == 0x8000)) printf("Classic: RIGHT pressed\n");
  if (((cc->btns & 0x4000) == 0x4000)) printf("Classic: DOWN pressed\n");
  if (((cc->btns & 0x2000) == 0x2000)) printf("Classic: FULL L pressed\n");
  if (((cc->btns & 0x1000) == 0x1000)) printf("Classic: MINUS pressed\n");
  if (((cc->btns & 0x0800) == 0x0800)) printf("Classic: HOME pressed\n");
  if (((cc->btns & 0x0400) == 0x0400)) printf("Classic: PLUS pressed\n");
  if (((cc->btns & 0x0200) == 0x0200)) printf("Classic: FULL R pressed\n");
  printf("classic L button pressed:         %f\n", cc->l_shoulder);
  printf("classic R button pressed:         %f\n", cc->r_shoulder);
  printf("classic left joystick angle:      %f\n", cc->ljs.ang);
  printf("classic left joystick magnitude:  %f\n", cc->ljs.mag);
  printf("classic right joystick angle:     %f\n", cc->rjs.ang);
  printf("classic right joystick magnitude: %f\n", cc->rjs.mag);
 } else if (wm->exp.type == 3) {
  struct guitar_hero_3_t *gh3 = (guitar_hero_3_t*)&wm->exp.gh3;
  if (((gh3->btns & 0x0001) == 0x0001)) printf("Guitar: Strum Up pressed\n");
  if (((gh3->btns & 0x4000) == 0x4000)) printf("Guitar: Strum Down pressed\n");
  if (((gh3->btns & 0x0008) == 0x0008)) printf("Guitar: Yellow pressed\n");
  if (((gh3->btns & 0x0010) == 0x0010)) printf("Guitar: Green pressed\n");
  if (((gh3->btns & 0x0020) == 0x0020)) printf("Guitar: Blue pressed\n");
  if (((gh3->btns & 0x0040) == 0x0040)) printf("Guitar: Red pressed\n");
  if (((gh3->btns & 0x0080) == 0x0080)) printf("Guitar: Orange pressed\n");
  if (((gh3->btns & 0x0400) == 0x0400)) printf("Guitar: Plus pressed\n");
  if (((gh3->btns & 0x1000) == 0x1000)) printf("Guitar: Minus pressed\n");
  printf("Guitar whammy bar:          %f\n", gh3->whammy_bar);
  printf("Guitar joystick angle:      %f\n", gh3->js.ang);
  printf("Guitar joystick magnitude:  %f\n", gh3->js.mag);
 }
}

variable_types: struct wiimote_t { int unid; struct bdaddr_t { unsigned char b[6]; } bdaddr; char bdaddr_str[18]; int out_sock; int in_sock; int state; unsigned char leds; float battery_level; int flags; unsigned char handshake_state; struct read_req_t { void (*cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len); unsigned char *buf; unsigned int addr; unsigned short size; unsigned short wait; unsigned char dirty; struct read_req_t *next; } *read_req; struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct expansion_t { int type; union { struct nunchuk_t { struct accel_t accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } nunchuk; struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t ljs; struct joystick_t rjs; } classic; struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t js; } gh3; }; } exp; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; struct ir_t { struct ir_dot_t { unsigned char visible; unsigned int x; unsigned int y; short rx; short ry; unsigned char order; unsigned char size; } dot[4]; unsigned char num_dots; enum aspect_t { WIIUSE_ASPECT_4_3=0, WIIUSE_ASPECT_16_9=1 } aspect; enum ir_position_t { WIIUSE_IR_ABOVE=0, WIIUSE_IR_BELOW=1 } pos; unsigned int vres[2]; int offset[2]; int state; int ax; int ay; int x; int y; float distance; float z; } ir; unsigned short btns; unsigned short btns_held; unsigned short btns_released; float orient_threshold; int accel_threshold; struct wiimote_state_t { float exp_ljs_ang; float exp_rjs_ang; float exp_ljs_mag; float exp_rjs_mag; unsigned short exp_btns; struct orient_t exp_orient; struct vec3b_t exp_accel; float exp_r_shoulder; float exp_l_shoulder; int ir_ax; int ir_ay; float ir_distance; struct orient_t orient; unsigned short btns; struct vec3b_t accel; } lstate; enum WIIUSE_EVENT_TYPE { WIIUSE_NONE=0, WIIUSE_EVENT=1, WIIUSE_STATUS=2, WIIUSE_CONNECT=3, WIIUSE_DISCONNECT=4, WIIUSE_UNEXPECTED_DISCONNECT=5, WIIUSE_READ_DATA=6, WIIUSE_NUNCHUK_INSERTED=7, WIIUSE_NUNCHUK_REMOVED=8, WIIUSE_CLASSIC_CTRL_INSERTED=9, WIIUSE_CLASSIC_CTRL_REMOVED=10, WIIUSE_GUITAR_HERO_3_CTRL_INSERTED=11, WIIUSE_GUITAR_HERO_3_CTRL_REMOVED=12 } event; unsigned char event_buf[32]; } *

variable_types: i int

variable_types: struct nunchuk_t { struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } *

variable_types: struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } ljs; struct joystick_t rjs; } *

variable_types: struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; } *

user_defined_types: struct wiimote_t { int unid; struct bdaddr_t bdaddr; char bdaddr_str[18]; int out_sock; int in_sock; int state; unsigned char leds; float battery_level; int flags; unsigned char handshake_state; struct read_req_t *read_req; struct accel_t accel_calib; struct expansion_t exp; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; struct ir_t ir; unsigned short btns; unsigned short btns_held; unsigned short btns_released; float orient_threshold; int accel_threshold; struct wiimote_state_t lstate; enum WIIUSE_EVENT_TYPE event; unsigned char event_buf[32]; }

user_defined_types: struct nunchuk_t { struct accel_t accel_calib; struct joystick_t js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; }

user_defined_types: struct expansion_t { int type; union { struct nunchuk_t nunchuk; struct classic_ctrl_t classic; struct guitar_hero_3_t gh3; }; }

user_defined_types: struct ir_t { struct ir_dot_t dot[4]; unsigned char num_dots; enum aspect_t aspect; enum ir_position_t pos; unsigned int vres[2]; int offset[2]; int state; int ax; int ay; int x; int y; float distance; float z; }


original_code:
void handle_read(struct wiimote_t* wm, byte* data, unsigned short len) {
 int i = 0;
 printf("\n\n--- DATA READ [wiimote id %i] ---\n", wm->unid);
 printf("finished read of size %i\n", len);
 for (; i < len; ++i) {
  if (!(i%16))
   printf("\n");
  printf("%x ", data[i]);
 }
 printf("\n\n");
}

canonical_original_code:
void handle_read(struct wiimote_t *wm, unsigned char *data, unsigned short len) {
 int i = 0;
 printf("\n\n--- DATA READ [wiimote id %i] ---\n", wm->unid);
 printf("finished read of size %i\n", len);
 for (; i < len; ++i) {
  if (!(i%16))
   printf("\n");
  printf("%x ", data[i]);
 }
 printf("\n\n");
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned short

user_defined_types: struct wiimote_state_t { float exp_ljs_ang; float exp_rjs_ang; float exp_ljs_mag; float exp_rjs_mag; unsigned short exp_btns; struct orient_t exp_orient; struct vec3b_t exp_accel; float exp_r_shoulder; float exp_l_shoulder; int ir_ax; int ir_ay; float ir_distance; struct orient_t orient; unsigned short btns; struct vec3b_t accel; }

user_defined_types: enum WIIUSE_EVENT_TYPE { WIIUSE_NONE=0, WIIUSE_EVENT=1, WIIUSE_STATUS=2, WIIUSE_CONNECT=3, WIIUSE_DISCONNECT=4, WIIUSE_UNEXPECTED_DISCONNECT=5, WIIUSE_READ_DATA=6, WIIUSE_NUNCHUK_INSERTED=7, WIIUSE_NUNCHUK_REMOVED=8, WIIUSE_CLASSIC_CTRL_INSERTED=9, WIIUSE_CLASSIC_CTRL_REMOVED=10, WIIUSE_GUITAR_HERO_3_CTRL_INSERTED=11, WIIUSE_GUITAR_HERO_3_CTRL_REMOVED=12 }

user_defined_types: struct read_req_t { void (*cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len); unsigned char *buf; unsigned int addr; unsigned short size; unsigned short wait; unsigned char dirty; struct read_req_t *next; }

user_defined_types: enum aspect_t { WIIUSE_ASPECT_4_3=0, WIIUSE_ASPECT_16_9=1 }

user_defined_types: enum ir_position_t { WIIUSE_IR_ABOVE=0, WIIUSE_IR_BELOW=1 }

user_defined_types: struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t ljs; struct joystick_t rjs; }

user_defined_types: struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t js; }

user_defined_types: struct ir_dot_t { unsigned char visible; unsigned int x; unsigned int y; short rx; short ry; unsigned char order; unsigned char size; }


decompiled_code:
int __fastcall handle_ctrl_status(float *a1)
{
printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n", *(unsigned int *)a1);
printf("attachment: %i\n", *((unsigned int *)a1 + 22));
printf("speaker: %i\n", (*((_DWORD *)a1 + 9) >> 8) & 1);
printf("ir: %i\n", (*((_DWORD *)a1 + 9) >> 7) & 1);
printf(
"leds: %i %i %i %i\n",
((_BYTE)a1[10] & 0x10) != 0,
((_BYTE)a1[10] & 0x20) != 0,
((_BYTE)a1[10] & 0x40) != 0,
(unsigned int)*((char *)a1 + 40) >> 31);
return printf("battery: %f %%\n", a1[11]);
}

canonical_decompiled_code:
int __fastcall func9(float *a1)
{
printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n", *(unsigned int *)a1);
printf("attachment: %i\n", *((unsigned int *)a1 + 22));
printf("speaker: %i\n", (*((_DWORD *)a1 + 9) >> 8) & 1);
printf("ir: %i\n", (*((_DWORD *)a1 + 9) >> 7) & 1);
printf(
"leds: %i %i %i %i\n",
((_BYTE)a1[10] & 0x10) != 0,
((_BYTE)a1[10] & 0x20) != 0,
((_BYTE)a1[10] & 0x40) != 0,
(unsigned int)*((char *)a1 + 40) >> 31);
return printf("battery: %f %%\n", a1[11]);
}

original_code:
void handle_ctrl_status(struct wiimote_t* wm) {
 printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n", wm->unid);
 printf("attachment:      %i\n", wm->exp.type);
 printf("speaker:         %i\n", ((wm->state & 0x100) == 0x100));
 printf("ir:              %i\n", ((wm->state & 0x080) == 0x080));
 printf("leds:            %i %i %i %i\n", ((wm->leds & 0x10) == 0x10), ((wm->leds & 0x20) == 0x20), ((wm->leds & 0x40) == 0x40), ((wm->leds & 0x80) == 0x80));
 printf("battery:         %f %%\n", wm->battery_level);
}

canonical_original_code:
void handle_ctrl_status(struct wiimote_t *wm) {
 printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n", wm->unid);
 printf("attachment:      %i\n", wm->exp.type);
 printf("speaker:         %i\n", ((wm->state & 0x100) == 0x100));
 printf("ir:              %i\n", ((wm->state & 0x080) == 0x080));
 printf("leds:            %i %i %i %i\n", ((wm->leds & 0x10) == 0x10), ((wm->leds & 0x20) == 0x20), ((wm->leds & 0x40) == 0x40), ((wm->leds & 0x80) == 0x80));
 printf("battery:         %f %%\n", wm->battery_level);
}


decompiled_code:
int __fastcall handle_disconnect(unsigned int *a1)
{
return printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", *a1);
}

canonical_decompiled_code:
int __fastcall func10(unsigned int *a1)
{
return printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", *a1);
}

original_code:
void handle_disconnect(wiimote* wm) {
 printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", wm->unid);
}

canonical_original_code:
void handle_disconnect(struct wiimote_t *wm) {
 printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", wm->unid);
}


test

decompiled_code:
int __fastcall test(__int64 a1, unsigned __int8 *a2, unsigned __int16 a3)
{
return printf("test: %i [%x %x %x %x]\n", a3, *a2, a2[1], a2[2], a2[3]);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, unsigned __int8 *a2, unsigned __int16 a3)
{
return printf("test: %i [%x %x %x %x]\n", a3, *a2, a2[1], a2[2], a2[3]);
}

original_code:
void test(struct wiimote_t* wm, byte* data, unsigned short len) {
 printf("test: %i [%x %x %x %x]\n", len, data[0], data[1], data[2], data[3]);
}

canonical_original_code:
void test(struct wiimote_t *wm, unsigned char *data, unsigned short len) {
 printf("test: %i [%x %x %x %x]\n", len, data[0], data[1], data[2], data[3]);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
unsigned int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // eax
unsigned int **v7; // rbx
unsigned int *v9; // rdi

v3 = wiiuse_init();
v4 = wiiuse_find();
if ( v4 )
{
v5 = v4;
v6 = wiiuse_connect();
if ( v6 )
{
printf("Connected to %i wiimotes (of %i found).\n", v6, v5);
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_rumble();
wiiuse_rumble();
usleep(0x30D40u);
wiiuse_rumble();
wiiuse_rumble();
while ( 1 )
{
while ( !(unsigned int)wiiuse_poll() )
;
v7 = (unsigned int **)v3;
do
{
v9 = *v7;
switch ( (*v7)[118] )
{
case 1u:
handle_event(v9);
break;
case 2u:
handle_ctrl_status((float *)v9);
break;
case 4u:
case 5u:
handle_disconnect(v9);
break;
case 7u:
puts("Nunchuk inserted.");
break;
case 8u:
case 0xAu:
case 0xCu:
handle_ctrl_status((float *)v9);
puts("An expansion was removed.");
break;
case 9u:
puts("Classic controller inserted.");
break;
case 0xBu:
handle_ctrl_status((float *)v9);
puts("Guitar Hero 3 controller inserted.");
break;
default:
break;
}
++v7;
}
while ( v7 != (unsigned int **)(v3 + 32) );
}
}
puts("Failed to connect to any wiimote.");
}
else
{
printf("No wiimotes found.");
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
unsigned int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // eax
unsigned int **v7; // rbx
unsigned int *v9; // rdi

v3 = wiiuse_init();
v4 = wiiuse_find();
if ( v4 )
{
v5 = v4;
v6 = wiiuse_connect();
if ( v6 )
{
printf("Connected to %i wiimotes (of %i found).\n", v6, v5);
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_set_leds();
wiiuse_rumble();
wiiuse_rumble();
usleep(0x30D40u);
wiiuse_rumble();
wiiuse_rumble();
while ( 1 )
{
while ( !(unsigned int)wiiuse_poll() )
;
v7 = (unsigned int **)v3;
do
{
v9 = *v7;
switch ( (*v7)[118] )
{
case 1u:
func7(v9);
break;
case 2u:
func9((float *)v9);
break;
case 4u:
case 5u:
func10(v9);
break;
case 7u:
puts("Nunchuk inserted.");
break;
case 8u:
case 0xAu:
case 0xCu:
func9((float *)v9);
puts("An expansion was removed.");
break;
case 9u:
puts("Classic controller inserted.");
break;
case 0xBu:
func9((float *)v9);
puts("Guitar Hero 3 controller inserted.");
break;
default:
break;
}
++v7;
}
while ( v7 != (unsigned int **)(v3 + 32) );
}
}
puts("Failed to connect to any wiimote.");
}
else
{
printf("No wiimotes found.");
}
return 0;
}

original_code:
int main(int argc, char** argv) {
 wiimote** wiimotes;
 int found, connected;
 wiimotes = wiiuse_init(4);
 found = wiiuse_find(wiimotes, 4, 5);
 if (!found) {
  printf ("No wiimotes found.");
  return 0;
 }
 connected = wiiuse_connect(wiimotes, 4);
 if (connected)
  printf("Connected to %i wiimotes (of %i found).\n", connected, found);
 else {
  printf("Failed to connect to any wiimote.\n");
  return 0;
 }
 wiiuse_set_leds(wiimotes[0], 0x10);
 wiiuse_set_leds(wiimotes[1], 0x20);
 wiiuse_set_leds(wiimotes[2], 0x40);
 wiiuse_set_leds(wiimotes[3], 0x80);
 wiiuse_rumble(wiimotes[0], 1);
 wiiuse_rumble(wiimotes[1], 1);
  usleep(200000);
 wiiuse_rumble(wiimotes[0], 0);
 wiiuse_rumble(wiimotes[1], 0);
 while (1) {
  if (wiiuse_poll(wiimotes, 4)) {
   int i = 0;
   for (; i < 4; ++i) {
    switch (wiimotes[i]->event) {
     case WIIUSE_EVENT:
      handle_event(wiimotes[i]);
      break;
     case WIIUSE_STATUS:
      handle_ctrl_status(wiimotes[i]);
      break;
     case WIIUSE_DISCONNECT:
     case WIIUSE_UNEXPECTED_DISCONNECT:
      handle_disconnect(wiimotes[i]);
      break;
     case WIIUSE_READ_DATA:
      break;
     case WIIUSE_NUNCHUK_INSERTED:
      printf("Nunchuk inserted.\n");
      break;
     case WIIUSE_CLASSIC_CTRL_INSERTED:
      printf("Classic controller inserted.\n");
      break;
     case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
      handle_ctrl_status(wiimotes[i]);
      printf("Guitar Hero 3 controller inserted.\n");
      break;
     case WIIUSE_NUNCHUK_REMOVED:
     case WIIUSE_CLASSIC_CTRL_REMOVED:
     case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
      handle_ctrl_status(wiimotes[i]);
      printf("An expansion was removed.\n");
      break;
     default:
      break;
    }
   }
  }
 }
 wiiuse_cleanup(wiimotes, 4);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 struct wiimote_t **wiimotes;
 int found; int connected;
 wiimotes = wiiuse_init(4);
 found = wiiuse_find(wiimotes, 4, 5);
 if (!found) {
  printf ("No wiimotes found.");
  return 0;
 }
 connected = wiiuse_connect(wiimotes, 4);
 if (connected)
  printf("Connected to %i wiimotes (of %i found).\n", connected, found);
 else {
  printf("Failed to connect to any wiimote.\n");
  return 0;
 }
 wiiuse_set_leds(wiimotes[0], 0x10);
 wiiuse_set_leds(wiimotes[1], 0x20);
 wiiuse_set_leds(wiimotes[2], 0x40);
 wiiuse_set_leds(wiimotes[3], 0x80);
 wiiuse_rumble(wiimotes[0], 1);
 wiiuse_rumble(wiimotes[1], 1);
  usleep(200000);
 wiiuse_rumble(wiimotes[0], 0);
 wiiuse_rumble(wiimotes[1], 0);
 while (1) {
  if (wiiuse_poll(wiimotes, 4)) {
   int i = 0;
   for (; i < 4; ++i) {
    switch (wiimotes[i]->event) {
     case WIIUSE_EVENT:
      handle_event(wiimotes[i]);
      break;
     case WIIUSE_STATUS:
      handle_ctrl_status(wiimotes[i]);
      break;
     case WIIUSE_DISCONNECT:
     case WIIUSE_UNEXPECTED_DISCONNECT:
      handle_disconnect(wiimotes[i]);
      break;
     case WIIUSE_READ_DATA:
      break;
     case WIIUSE_NUNCHUK_INSERTED:
      printf("Nunchuk inserted.\n");
      break;
     case WIIUSE_CLASSIC_CTRL_INSERTED:
      printf("Classic controller inserted.\n");
      break;
     case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
      handle_ctrl_status(wiimotes[i]);
      printf("Guitar Hero 3 controller inserted.\n");
      break;
     case WIIUSE_NUNCHUK_REMOVED:
     case WIIUSE_CLASSIC_CTRL_REMOVED:
     case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
      handle_ctrl_status(wiimotes[i]);
      printf("An expansion was removed.\n");
      break;
     default:
      break;
    }
   }
  }
 }
 wiiuse_cleanup(wiimotes, 4);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct wiimote_t { int unid; struct bdaddr_t { unsigned char b[6]; } bdaddr; char bdaddr_str[18]; int out_sock; int in_sock; int state; unsigned char leds; float battery_level; int flags; unsigned char handshake_state; struct read_req_t { void (*cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len); unsigned char *buf; unsigned int addr; unsigned short size; unsigned short wait; unsigned char dirty; struct read_req_t *next; } *read_req; struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct expansion_t { int type; union { struct nunchuk_t { struct accel_t accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } nunchuk; struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t ljs; struct joystick_t rjs; } classic; struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t js; } gh3; }; } exp; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; struct ir_t { struct ir_dot_t { unsigned char visible; unsigned int x; unsigned int y; short rx; short ry; unsigned char order; unsigned char size; } dot[4]; unsigned char num_dots; enum aspect_t { WIIUSE_ASPECT_4_3=0, WIIUSE_ASPECT_16_9=1 } aspect; enum ir_position_t { WIIUSE_IR_ABOVE=0, WIIUSE_IR_BELOW=1 } pos; unsigned int vres[2]; int offset[2]; int state; int ax; int ay; int x; int y; float distance; float z; } ir; unsigned short btns; unsigned short btns_held; unsigned short btns_released; float orient_threshold; int accel_threshold; struct wiimote_state_t { float exp_ljs_ang; float exp_rjs_ang; float exp_ljs_mag; float exp_rjs_mag; unsigned short exp_btns; struct orient_t exp_orient; struct vec3b_t exp_accel; float exp_r_shoulder; float exp_l_shoulder; int ir_ax; int ir_ay; float ir_distance; struct orient_t orient; unsigned short btns; struct vec3b_t accel; } lstate; enum WIIUSE_EVENT_TYPE { WIIUSE_NONE=0, WIIUSE_EVENT=1, WIIUSE_STATUS=2, WIIUSE_CONNECT=3, WIIUSE_DISCONNECT=4, WIIUSE_UNEXPECTED_DISCONNECT=5, WIIUSE_READ_DATA=6, WIIUSE_NUNCHUK_INSERTED=7, WIIUSE_NUNCHUK_REMOVED=8, WIIUSE_CLASSIC_CTRL_INSERTED=9, WIIUSE_CLASSIC_CTRL_REMOVED=10, WIIUSE_GUITAR_HERO_3_CTRL_INSERTED=11, WIIUSE_GUITAR_HERO_3_CTRL_REMOVED=12 } event; unsigned char event_buf[32]; } **

variable_types: found int

variable_types: connected int


decompiled_code:
__int64 __fastcall wiiuse_find(unsigned int **a1, int a2, unsigned int a3)
{
unsigned int **v3; // r13
unsigned int **v6; // rax
__int64 v7; // rcx
int v8; // esi
unsigned int *v9; // rdi
unsigned int *v10; // r8
unsigned int *v11; // r9
unsigned int *v12; // r10
unsigned int *v13; // r11
unsigned int *v14; // r12
unsigned int *v15; // r14
unsigned int *v16; // r15
_QWORD *v17; // rdx
unsigned int *v18; // rax
_BYTE *v19; // rsi
_BYTE *v20; // rdi
_BYTE *v21; // r8
_BYTE *v22; // r9
_BYTE *v23; // r10
_BYTE *v24; // r11
int v25; // eax
unsigned int v26; // er12
int v27; // eax
int v28; // er14
__int64 v29; // rbp
int v30; // er12
int v31; // er15
char *v32; // rdi
unsigned int *v33; // r9
__int64 v34; // rdx
unsigned int *v36; // rax
__int64 v37; // rdx
unsigned int *v38; // r9
__int64 v39; // rdx
unsigned int *v40; // rax
__int64 v41; // rdx
unsigned int *v42; // rax
__int64 v43; // rdx
unsigned int *v44; // rdi
__int64 v45; // rdx
char *v46; // rdi
int v47; // er15
__int64 v48; // rbp
char *v49; // rdi
char *v50; // rdi
char *v51; // rcx
unsigned int *v52; // rax
__int64 v53; // r8
int fd; // [rsp+4h] [rbp-764h]
unsigned int **v55; // [rsp+8h] [rbp-760h]
unsigned int **v56; // [rsp+8h] [rbp-760h]
unsigned int **v57; // [rsp+8h] [rbp-760h]
unsigned int **v58; // [rsp+8h] [rbp-760h]
unsigned int **v59; // [rsp+10h] [rbp-758h]
unsigned int **v60; // [rsp+10h] [rbp-758h]
char *v61; // [rsp+28h] [rbp-740h] BYREF
char v62[1848]; // [rsp+30h] [rbp-738h] BYREF

v3 = a1;
if ( a2 > 0 )
{
v6 = a1;
v7 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v8 = a2 & 7;
if ( !v8 )
goto LABEL_71;
if ( v8 != 1LL )
{
if ( v8 != 2LL )
{
if ( v8 != 3LL )
{
if ( v8 != 4LL )
{
if ( v8 != 5LL )
{
if ( v8 != 6LL )
{
v9 = *a1;
*((_BYTE *)v9 + 4) = 0;
*((_BYTE *)v9 + 5) = 0;
*((_BYTE *)v9 + 6) = 0;
*((_BYTE *)v9 + 7) = 0;
*((_BYTE *)v9 + 8) = 0;
*((_BYTE *)v9 + 9) = 0;
++v6;
}
v10 = *v6;
*((_BYTE *)v10 + 4) = 0;
*((_BYTE *)v10 + 5) = 0;
*((_BYTE *)v10 + 6) = 0;
*((_BYTE *)v10 + 7) = 0;
*((_BYTE *)v10 + 8) = 0;
*((_BYTE *)v10 + 9) = 0;
++v6;
}
v11 = *v6;
*((_BYTE *)v11 + 4) = 0;
*((_BYTE *)v11 + 5) = 0;
*((_BYTE *)v11 + 6) = 0;
*((_BYTE *)v11 + 7) = 0;
*((_BYTE *)v11 + 8) = 0;
*((_BYTE *)v11 + 9) = 0;
++v6;
}
v12 = *v6;
*((_BYTE *)v12 + 4) = 0;
*((_BYTE *)v12 + 5) = 0;
*((_BYTE *)v12 + 6) = 0;
*((_BYTE *)v12 + 7) = 0;
*((_BYTE *)v12 + 8) = 0;
*((_BYTE *)v12 + 9) = 0;
++v6;
}
v13 = *v6;
*((_BYTE *)v13 + 4) = 0;
*((_BYTE *)v13 + 5) = 0;
*((_BYTE *)v13 + 6) = 0;
*((_BYTE *)v13 + 7) = 0;
*((_BYTE *)v13 + 8) = 0;
*((_BYTE *)v13 + 9) = 0;
++v6;
}
v14 = *v6;
*((_BYTE *)v14 + 4) = 0;
*((_BYTE *)v14 + 5) = 0;
*((_BYTE *)v14 + 6) = 0;
*((_BYTE *)v14 + 7) = 0;
*((_BYTE *)v14 + 8) = 0;
*((_BYTE *)v14 + 9) = 0;
++v6;
}
v15 = *v6;
*((_BYTE *)v15 + 4) = 0;
*((_BYTE *)v15 + 5) = 0;
*((_BYTE *)v15 + 6) = 0;
*((_BYTE *)v15 + 7) = 0;
*((_BYTE *)v15 + 8) = 0;
*((_BYTE *)v15 + 9) = 0;
if ( ++v6 != (unsigned int **)v7 )
{
LABEL_71:
do
{
v16 = *v6;
*((_BYTE *)v16 + 4) = 0;
*((_BYTE *)v16 + 5) = 0;
*((_BYTE *)v16 + 6) = 0;
*((_BYTE *)v16 + 7) = 0;
*((_BYTE *)v16 + 8) = 0;
*((_BYTE *)v16 + 9) = 0;
v17 = v6 + 1;
v18 = v6[1];
*((_BYTE *)v18 + 4) = 0;
*((_BYTE *)v18 + 5) = 0;
*((_BYTE *)v18 + 6) = 0;
*((_BYTE *)v18 + 7) = 0;
*((_BYTE *)v18 + 8) = 0;
*((_BYTE *)v18 + 9) = 0;
v19 = (_BYTE *)v17[1];
v19[4] = 0;
v19[5] = 0;
v19[6] = 0;
v19[7] = 0;
v19[8] = 0;
v19[9] = 0;
v20 = (_BYTE *)v17[2];
v20[4] = 0;
v20[5] = 0;
v20[6] = 0;
v20[7] = 0;
v20[8] = 0;
v20[9] = 0;
v21 = (_BYTE *)v17[3];
v21[4] = 0;
v21[5] = 0;
v21[6] = 0;
v21[7] = 0;
v21[8] = 0;
v21[9] = 0;
v22 = (_BYTE *)v17[4];
v22[4] = 0;
v22[5] = 0;
v22[6] = 0;
v22[7] = 0;
v22[8] = 0;
v22[9] = 0;
v23 = (_BYTE *)v17[5];
v23[4] = 0;
v23[5] = 0;
v23[6] = 0;
v23[7] = 0;
v23[8] = 0;
v23[9] = 0;
v24 = (_BYTE *)v17[6];
v24[4] = 0;
v24[5] = 0;
v24[6] = 0;
v24[7] = 0;
v24[8] = 0;
v24[9] = 0;
v6 = (unsigned int **)(v17 + 7);
}
while ( v17 + 7 != (_QWORD *)v7 );
}
}
v25 = hci_get_route(0LL);
v26 = v25;
if ( v25 < 0 )
{
perror("hci_get_route");
v30 = 0;
}
else
{
fd = hci_open_dev((unsigned int)v25);
if ( fd < 0 )
{
perror("hci_open_dev");
v30 = 0;
}
else
{
v61 = v62;
memset(v62, 0, 0x700uLL);
v27 = hci_inquiry(v26, a3, 128LL, 0LL, &v61, 1LL);
v28 = v27;
if ( v27 < 0 )
{
perror("hci_inquiry");
v30 = 0;
}
else
{
fprintf(stderr, "[INFO] Found %i bluetooth device(s).\n", (unsigned int)v27);
if ( v28 <= 0 || a2 <= 0 )
{
v30 = 0;
}
else
{
v29 = 0LL;
v30 = 0;
v31 = 0;
if ( (((_BYTE)v28 - 1) & 3) == 0 )
goto LABEL_72;
if ( v61[9] == 4 && v61[10] == 37 && !v61[11] )
{
ba2str(v61, (char *)*v3 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v3 + 10, **v3);
v36 = *v3;
v37 = (__int64)v61;
v36[1] = *(_DWORD *)v61;
*((_WORD *)v36 + 4) = *(_WORD *)(v37 + 4);
(*v3)[9] |= 1u;
v30 = 1;
}
v31 = 1;
v29 = 14LL;
if ( v28 > 1 && v30 < a2 )
{
if ( (((_BYTE)v28 - 1) & 3) == 1 )
goto LABEL_72;
if ( (((_BYTE)v28 - 1) & 3) == 2 )
goto LABEL_73;
if ( v61[23] == 4 && v61[24] == 37 && !v61[25] )
{
v56 = &v3[v30];
ba2str(v61 + 14, (char *)*v56 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v56 + 10, **v56);
v38 = *v56;
v39 = (__int64)v61;
v38[1] = *(_DWORD *)(v61 + 14);
*((_WORD *)v38 + 4) = *(_WORD *)(v39 + 18);
(*v56)[9] |= 1u;
++v30;
}
v31 = 2;
v29 = 28LL;
if ( v30 < a2 )
{
LABEL_73:
v32 = &v61[v29];
if ( v61[v29 + 9] == 4 && v32[10] == 37 && !v32[11] )
{
v57 = &v3[v30];
ba2str(v32, (char *)*v57 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v57 + 10, **v57);
v40 = *v57;
v41 = (__int64)v61;
v40[1] = *(_DWORD *)&v61[v29];
*((_WORD *)v40 + 4) = *(_WORD *)(v41 + v29 + 4);
(*v57)[9] |= 1u;
++v30;
}
++v31;
v29 += 14LL;
if ( v30 < a2 )
{
LABEL_72:
do
{
v46 = &v61[v29];
if ( v61[v29 + 9] == 4 && v46[10] == 37 && !v46[11] )
{
v55 = &v3[v30];
ba2str(v46, (char *)*v55 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v55 + 10, **v55);
v33 = *v55;
v34 = (__int64)v61;
v33[1] = *(_DWORD *)&v61[v29];
*((_WORD *)v33 + 4) = *(_WORD *)(v34 + v29 + 4);
(*v55)[9] |= 1u;
++v30;
}
v47 = v31 + 1;
v48 = v29 + 14;
if ( v28 <= v47 || v30 >= a2 )
break;
v49 = &v61[v48];
if ( v61[v48 + 9] == 4 && v49[10] == 37 && !v49[11] )
{
v58 = &v3[v30];
ba2str(v49, (char *)*v58 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v58 + 10, **v58);
v42 = *v58;
v43 = (__int64)v61;
v42[1] = *(_DWORD *)&v61[v48];
*((_WORD *)v42 + 4) = *(_WORD *)(v43 + v48 + 4);
(*v58)[9] |= 1u;
++v30;
}
if ( v30 >= a2 )
break;
v50 = &v61[v48 + 14];
if ( v50[9] == 4 && v50[10] == 37 && !v50[11] )
{
v59 = &v3[v30];
ba2str(v50, (char *)*v59 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v59 + 10, **v59);
v44 = *v59;
v45 = (__int64)v61;
v44[1] = *(_DWORD *)&v61[v48 + 14];
*((_WORD *)v44 + 4) = *(_WORD *)(v45 + v48 + 14 + 4);
(*v59)[9] |= 1u;
++v30;
}
if ( v30 >= a2 )
break;
v51 = &v61[v48 + 28];
if ( v51[9] == 4 && v51[10] == 37 && !v51[11] )
{
v60 = &v3[v30];
ba2str(v51, (char *)*v60 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v60 + 10, **v60);
v52 = *v60;
v53 = (__int64)v61;
v52[1] = *(_DWORD *)&v61[v48 + 28];
*((_WORD *)v52 + 4) = *(_WORD *)(v53 + v48 + 28 + 4);
(*v60)[9] |= 1u;
++v30;
}
v31 = v47 + 3;
v29 = v48 + 42;
}
while ( v30 < a2 );
}
}
}
}
close(fd);
}
}
}
return (unsigned int)v30;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int **a1, int a2, unsigned int a3)
{
unsigned int **v3; // r13
unsigned int **v6; // rax
__int64 v7; // rcx
int v8; // esi
unsigned int *v9; // rdi
unsigned int *v10; // r8
unsigned int *v11; // r9
unsigned int *v12; // r10
unsigned int *v13; // r11
unsigned int *v14; // r12
unsigned int *v15; // r14
unsigned int *v16; // r15
_QWORD *v17; // rdx
unsigned int *v18; // rax
_BYTE *v19; // rsi
_BYTE *v20; // rdi
_BYTE *v21; // r8
_BYTE *v22; // r9
_BYTE *v23; // r10
_BYTE *v24; // r11
int v25; // eax
unsigned int v26; // er12
int v27; // eax
int v28; // er14
__int64 v29; // rbp
int v30; // er12
int v31; // er15
char *v32; // rdi
unsigned int *v33; // r9
__int64 v34; // rdx
unsigned int *v36; // rax
__int64 v37; // rdx
unsigned int *v38; // r9
__int64 v39; // rdx
unsigned int *v40; // rax
__int64 v41; // rdx
unsigned int *v42; // rax
__int64 v43; // rdx
unsigned int *v44; // rdi
__int64 v45; // rdx
char *v46; // rdi
int v47; // er15
__int64 v48; // rbp
char *v49; // rdi
char *v50; // rdi
char *v51; // rcx
unsigned int *v52; // rax
__int64 v53; // r8
int fd; // [rsp+4h] [rbp-764h]
unsigned int **v55; // [rsp+8h] [rbp-760h]
unsigned int **v56; // [rsp+8h] [rbp-760h]
unsigned int **v57; // [rsp+8h] [rbp-760h]
unsigned int **v58; // [rsp+8h] [rbp-760h]
unsigned int **v59; // [rsp+10h] [rbp-758h]
unsigned int **v60; // [rsp+10h] [rbp-758h]
char *v61; // [rsp+28h] [rbp-740h] BYREF
char v62[1848]; // [rsp+30h] [rbp-738h] BYREF

v3 = a1;
if ( a2 > 0 )
{
v6 = a1;
v7 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v8 = a2 & 7;
if ( !v8 )
goto LABEL_71;
if ( v8 != 1LL )
{
if ( v8 != 2LL )
{
if ( v8 != 3LL )
{
if ( v8 != 4LL )
{
if ( v8 != 5LL )
{
if ( v8 != 6LL )
{
v9 = *a1;
*((_BYTE *)v9 + 4) = 0;
*((_BYTE *)v9 + 5) = 0;
*((_BYTE *)v9 + 6) = 0;
*((_BYTE *)v9 + 7) = 0;
*((_BYTE *)v9 + 8) = 0;
*((_BYTE *)v9 + 9) = 0;
++v6;
}
v10 = *v6;
*((_BYTE *)v10 + 4) = 0;
*((_BYTE *)v10 + 5) = 0;
*((_BYTE *)v10 + 6) = 0;
*((_BYTE *)v10 + 7) = 0;
*((_BYTE *)v10 + 8) = 0;
*((_BYTE *)v10 + 9) = 0;
++v6;
}
v11 = *v6;
*((_BYTE *)v11 + 4) = 0;
*((_BYTE *)v11 + 5) = 0;
*((_BYTE *)v11 + 6) = 0;
*((_BYTE *)v11 + 7) = 0;
*((_BYTE *)v11 + 8) = 0;
*((_BYTE *)v11 + 9) = 0;
++v6;
}
v12 = *v6;
*((_BYTE *)v12 + 4) = 0;
*((_BYTE *)v12 + 5) = 0;
*((_BYTE *)v12 + 6) = 0;
*((_BYTE *)v12 + 7) = 0;
*((_BYTE *)v12 + 8) = 0;
*((_BYTE *)v12 + 9) = 0;
++v6;
}
v13 = *v6;
*((_BYTE *)v13 + 4) = 0;
*((_BYTE *)v13 + 5) = 0;
*((_BYTE *)v13 + 6) = 0;
*((_BYTE *)v13 + 7) = 0;
*((_BYTE *)v13 + 8) = 0;
*((_BYTE *)v13 + 9) = 0;
++v6;
}
v14 = *v6;
*((_BYTE *)v14 + 4) = 0;
*((_BYTE *)v14 + 5) = 0;
*((_BYTE *)v14 + 6) = 0;
*((_BYTE *)v14 + 7) = 0;
*((_BYTE *)v14 + 8) = 0;
*((_BYTE *)v14 + 9) = 0;
++v6;
}
v15 = *v6;
*((_BYTE *)v15 + 4) = 0;
*((_BYTE *)v15 + 5) = 0;
*((_BYTE *)v15 + 6) = 0;
*((_BYTE *)v15 + 7) = 0;
*((_BYTE *)v15 + 8) = 0;
*((_BYTE *)v15 + 9) = 0;
if ( ++v6 != (unsigned int **)v7 )
{
LABEL_71:
do
{
v16 = *v6;
*((_BYTE *)v16 + 4) = 0;
*((_BYTE *)v16 + 5) = 0;
*((_BYTE *)v16 + 6) = 0;
*((_BYTE *)v16 + 7) = 0;
*((_BYTE *)v16 + 8) = 0;
*((_BYTE *)v16 + 9) = 0;
v17 = v6 + 1;
v18 = v6[1];
*((_BYTE *)v18 + 4) = 0;
*((_BYTE *)v18 + 5) = 0;
*((_BYTE *)v18 + 6) = 0;
*((_BYTE *)v18 + 7) = 0;
*((_BYTE *)v18 + 8) = 0;
*((_BYTE *)v18 + 9) = 0;
v19 = (_BYTE *)v17[1];
v19[4] = 0;
v19[5] = 0;
v19[6] = 0;
v19[7] = 0;
v19[8] = 0;
v19[9] = 0;
v20 = (_BYTE *)v17[2];
v20[4] = 0;
v20[5] = 0;
v20[6] = 0;
v20[7] = 0;
v20[8] = 0;
v20[9] = 0;
v21 = (_BYTE *)v17[3];
v21[4] = 0;
v21[5] = 0;
v21[6] = 0;
v21[7] = 0;
v21[8] = 0;
v21[9] = 0;
v22 = (_BYTE *)v17[4];
v22[4] = 0;
v22[5] = 0;
v22[6] = 0;
v22[7] = 0;
v22[8] = 0;
v22[9] = 0;
v23 = (_BYTE *)v17[5];
v23[4] = 0;
v23[5] = 0;
v23[6] = 0;
v23[7] = 0;
v23[8] = 0;
v23[9] = 0;
v24 = (_BYTE *)v17[6];
v24[4] = 0;
v24[5] = 0;
v24[6] = 0;
v24[7] = 0;
v24[8] = 0;
v24[9] = 0;
v6 = (unsigned int **)(v17 + 7);
}
while ( v17 + 7 != (_QWORD *)v7 );
}
}
v25 = hci_get_route(0LL);
v26 = v25;
if ( v25 < 0 )
{
perror("hci_get_route");
v30 = 0;
}
else
{
fd = hci_open_dev((unsigned int)v25);
if ( fd < 0 )
{
perror("hci_open_dev");
v30 = 0;
}
else
{
v61 = v62;
memset(v62, 0, 0x700uLL);
v27 = hci_inquiry(v26, a3, 128LL, 0LL, &v61, 1LL);
v28 = v27;
if ( v27 < 0 )
{
perror("hci_inquiry");
v30 = 0;
}
else
{
fprintf(stderr, "[INFO] Found %i bluetooth device(s).\n", (unsigned int)v27);
if ( v28 <= 0 || a2 <= 0 )
{
v30 = 0;
}
else
{
v29 = 0LL;
v30 = 0;
v31 = 0;
if ( (((_BYTE)v28 - 1) & 3) == 0 )
goto LABEL_72;
if ( v61[9] == 4 && v61[10] == 37 && !v61[11] )
{
ba2str(v61, (char *)*v3 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v3 + 10, **v3);
v36 = *v3;
v37 = (__int64)v61;
v36[1] = *(_DWORD *)v61;
*((_WORD *)v36 + 4) = *(_WORD *)(v37 + 4);
(*v3)[9] |= 1u;
v30 = 1;
}
v31 = 1;
v29 = 14LL;
if ( v28 > 1 && v30 < a2 )
{
if ( (((_BYTE)v28 - 1) & 3) == 1 )
goto LABEL_72;
if ( (((_BYTE)v28 - 1) & 3) == 2 )
goto LABEL_73;
if ( v61[23] == 4 && v61[24] == 37 && !v61[25] )
{
v56 = &v3[v30];
ba2str(v61 + 14, (char *)*v56 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v56 + 10, **v56);
v38 = *v56;
v39 = (__int64)v61;
v38[1] = *(_DWORD *)(v61 + 14);
*((_WORD *)v38 + 4) = *(_WORD *)(v39 + 18);
(*v56)[9] |= 1u;
++v30;
}
v31 = 2;
v29 = 28LL;
if ( v30 < a2 )
{
LABEL_73:
v32 = &v61[v29];
if ( v61[v29 + 9] == 4 && v32[10] == 37 && !v32[11] )
{
v57 = &v3[v30];
ba2str(v32, (char *)*v57 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v57 + 10, **v57);
v40 = *v57;
v41 = (__int64)v61;
v40[1] = *(_DWORD *)&v61[v29];
*((_WORD *)v40 + 4) = *(_WORD *)(v41 + v29 + 4);
(*v57)[9] |= 1u;
++v30;
}
++v31;
v29 += 14LL;
if ( v30 < a2 )
{
LABEL_72:
do
{
v46 = &v61[v29];
if ( v61[v29 + 9] == 4 && v46[10] == 37 && !v46[11] )
{
v55 = &v3[v30];
ba2str(v46, (char *)*v55 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v55 + 10, **v55);
v33 = *v55;
v34 = (__int64)v61;
v33[1] = *(_DWORD *)&v61[v29];
*((_WORD *)v33 + 4) = *(_WORD *)(v34 + v29 + 4);
(*v55)[9] |= 1u;
++v30;
}
v47 = v31 + 1;
v48 = v29 + 14;
if ( v28 <= v47 || v30 >= a2 )
break;
v49 = &v61[v48];
if ( v61[v48 + 9] == 4 && v49[10] == 37 && !v49[11] )
{
v58 = &v3[v30];
ba2str(v49, (char *)*v58 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v58 + 10, **v58);
v42 = *v58;
v43 = (__int64)v61;
v42[1] = *(_DWORD *)&v61[v48];
*((_WORD *)v42 + 4) = *(_WORD *)(v43 + v48 + 4);
(*v58)[9] |= 1u;
++v30;
}
if ( v30 >= a2 )
break;
v50 = &v61[v48 + 14];
if ( v50[9] == 4 && v50[10] == 37 && !v50[11] )
{
v59 = &v3[v30];
ba2str(v50, (char *)*v59 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v59 + 10, **v59);
v44 = *v59;
v45 = (__int64)v61;
v44[1] = *(_DWORD *)&v61[v48 + 14];
*((_WORD *)v44 + 4) = *(_WORD *)(v45 + v48 + 14 + 4);
(*v59)[9] |= 1u;
++v30;
}
if ( v30 >= a2 )
break;
v51 = &v61[v48 + 28];
if ( v51[9] == 4 && v51[10] == 37 && !v51[11] )
{
v60 = &v3[v30];
ba2str(v51, (char *)*v60 + 10);
fprintf(stderr, "[INFO] Found wiimote (%s) [id %i].\n", (const char *)*v60 + 10, **v60);
v52 = *v60;
v53 = (__int64)v61;
v52[1] = *(_DWORD *)&v61[v48 + 28];
*((_WORD *)v52 + 4) = *(_WORD *)(v53 + v48 + 28 + 4);
(*v60)[9] |= 1u;
++v30;
}
v31 = v47 + 3;
v29 = v48 + 42;
}
while ( v30 < a2 );
}
}
}
}
close(fd);
}
}
}
return (unsigned int)v30;
}

original_code:
int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) {
 int device_id;
 int device_sock;
 int found_devices;
 int found_wiimotes;
 for (found_wiimotes = 0; found_wiimotes < max_wiimotes; ++found_wiimotes)
  wm[found_wiimotes]->bdaddr = *(&(bdaddr_t) {{0, 0, 0, 0, 0, 0}});
 found_wiimotes = 0;
 device_id = hci_get_route(((void *)0));
 if (device_id < 0) {
  perror("hci_get_route");
  return 0;
 }
 device_sock = hci_open_dev(device_id);
 if (device_sock < 0) {
  perror("hci_open_dev");
  return 0;
 }
 inquiry_info scan_info_arr[128];
 inquiry_info* scan_info = scan_info_arr;
 memset(&scan_info_arr, 0, sizeof(scan_info_arr));
 found_devices = hci_inquiry(device_id, timeout, 128, ((void *)0), &scan_info, 0x0001);
 if (found_devices < 0) {
  perror("hci_inquiry");
  return 0;
 }
 fprintf(stderr, "[INFO] " "Found %i bluetooth device(s)." "\n", found_devices);
 int i = 0;
 for (; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
  if ((scan_info[i].dev_class[0] == 0x04) &&
   (scan_info[i].dev_class[1] == 0x25) &&
   (scan_info[i].dev_class[2] == 0x00))
  {
   ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str);
   fprintf(stderr, "[INFO] " "Found wiimote (%s) [id %i]." "\n", wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid);
   wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr;
   (wm[found_wiimotes]->state |= (0x0001));
   ++found_wiimotes;
  }
 }
 close(device_sock);
 return found_wiimotes;
}

canonical_original_code:
int wiiuse_find(struct wiimote_t **wm, int max_wiimotes, int timeout) {
 int device_id;
 int device_sock;
 int found_devices;
 int found_wiimotes;
 for (found_wiimotes = 0; found_wiimotes < max_wiimotes; ++found_wiimotes)
  wm[found_wiimotes]->bdaddr = *(&(struct bdaddr_t) {{0, 0, 0, 0, 0, 0}});
 found_wiimotes = 0;
 device_id = hci_get_route(((void *)0));
 if (device_id < 0) {
  perror("hci_get_route");
  return 0;
 }
 device_sock = hci_open_dev(device_id);
 if (device_sock < 0) {
  perror("hci_open_dev");
  return 0;
 }
 struct inquiry_info scan_info_arr[128];
 struct inquiry_info *scan_info = scan_info_arr;
 memset(&scan_info_arr, 0, sizeof(scan_info_arr));
 found_devices = hci_inquiry(device_id, timeout, 128, ((void *)0), &scan_info, 0x0001);
 if (found_devices < 0) {
  perror("hci_inquiry");
  return 0;
 }
 fprintf(stderr, "[INFO] " "Found %i bluetooth device(s)." "\n", found_devices);
 int i = 0;
 for (; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
  if ((scan_info[i].dev_class[0] == 0x04) &&
   (scan_info[i].dev_class[1] == 0x25) &&
   (scan_info[i].dev_class[2] == 0x00))
  {
   ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str);
   fprintf(stderr, "[INFO] " "Found wiimote (%s) [id %i]." "\n", wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid);
   wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr;
   (wm[found_wiimotes]->state |= (0x0001));
   ++found_wiimotes;
  }
 }
 close(device_sock);
 return found_wiimotes;
}

variable_types: timeout int

variable_types: struct inquiry_info { struct bdaddr_t { unsigned char b[6]; } bdaddr; unsigned char pscan_rep_mode; unsigned char pscan_period_mode; unsigned char pscan_mode; unsigned char dev_class[3]; unsigned short clock_offset; } [128]

variable_types: struct inquiry_info { struct bdaddr_t { unsigned char b[6]; } bdaddr; unsigned char pscan_rep_mode; unsigned char pscan_period_mode; unsigned char pscan_mode; unsigned char dev_class[3]; unsigned short clock_offset; } *

user_defined_types: struct inquiry_info { struct bdaddr_t bdaddr; unsigned char pscan_rep_mode; unsigned char pscan_period_mode; unsigned char pscan_mode; unsigned char dev_class[3]; unsigned short clock_offset; }


decompiled_code:
__int64 __fastcall wiiuse_connect(_QWORD *a1, int a2)
{
_QWORD *v2; // rbx
__int64 v3; // r12
unsigned int v4; // ebp
__int64 v5; // rdx
__int64 v6; // rdi
_QWORD *v8; // r13
__int64 v9; // rdi
__int64 v10; // rdi
__int64 v11; // rdi

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v4 = 0;
v5 = a2 & 3;
if ( (a2 & 3) == 0 )
goto LABEL_28;
if ( v5 != 1 )
{
if ( v5 != 2 )
{
v6 = *a1;
if ( (*(_BYTE *)(v6 + 36) & 1) != 0 )
v4 = wiiuse_connect_single((unsigned int *)v6, 0LL) != 0;
++v2;
}
if ( (*(_BYTE *)(*v2 + 36LL) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)*v2, 0LL) == 0) - 1;
++v2;
}
if ( (*(_BYTE *)(*v2 + 36LL) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)*v2, 0LL) == 0) - 1;
if ( ++v2 != (_QWORD *)v3 )
{
LABEL_28:
do
{
if ( (*(_BYTE *)(*v2 + 36LL) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)*v2, 0LL) == 0) - 1;
v8 = v2 + 1;
v9 = v2[1];
if ( (*(_BYTE *)(v9 + 36) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)v9, 0LL) == 0) - 1;
v10 = v2[2];
if ( (*(_BYTE *)(v10 + 36) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)v10, 0LL) == 0) - 1;
v11 = v2[3];
if ( (*(_BYTE *)(v11 + 36) & 1) != 0 )
v4 -= ((unsigned int)wiiuse_connect_single((unsigned int *)v11, 0LL) == 0) - 1;
v2 += 4;
}
while ( v8 + 3 != (_QWORD *)v3 );
}
return v4;
}

original_code:
int wiiuse_connect(struct wiimote_t** wm, int wiimotes) {
 int connected = 0;
 int i = 0;
 for (; i < wiimotes; ++i) {
  if (!((wm[i]->state & (0x0001)) == (0x0001)))
   continue;
  if (wiiuse_connect_single(wm[i], ((void *)0)))
   ++connected;
 }
 return connected;
}

canonical_original_code:
int wiiuse_connect(struct wiimote_t **wm, int wiimotes) {
 int connected = 0;
 int i = 0;
 for (; i < wiimotes; ++i) {
  if (!((wm[i]->state & (0x0001)) == (0x0001)))
   continue;
  if (wiiuse_connect_single(wm[i], ((void *)0)))
   ++connected;
 }
 return connected;
}

variable_types: wiimotes int


decompiled_code:
int __fastcall wiiuse_disconnect(int *a1)
{
int result; // eax

if ( a1 )
{
if ( (a1[9] & 8) == 0 )
{
close(a1[7]);
result = close(a1[8]);
a1[7] = -1;
a1[8] = -1;
a1[118] = 0;
a1[9] &= 0xFFFFFFF5;
}
}
return result;
}

original_code:
void wiiuse_disconnect(struct wiimote_t* wm) {
 if (!wm || (((wm->state & (0x0008)) == (0x0008))))
  return;
 close(wm->out_sock);
 close(wm->in_sock);
 wm->out_sock = -1;
 wm->in_sock = -1;
 wm->event = WIIUSE_NONE;
 (wm->state &= ~(0x0008));
 (wm->state &= ~(0x0002));
}

canonical_original_code:
void wiiuse_disconnect(struct wiimote_t *wm) {
 if (!wm || (((wm->state & (0x0008)) == (0x0008))))
  return;
 close(wm->out_sock);
 close(wm->in_sock);
 wm->out_sock = -1;
 wm->in_sock = -1;
 wm->event = WIIUSE_NONE;
 (wm->state &= ~(0x0008));
 (wm->state &= ~(0x0002));
}


decompiled_code:
__int64 wiiuse_io_read()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
return 0LL;
}

original_code:
int wiiuse_io_read(struct wiimote_t* wm) {
 return 0;
}

canonical_original_code:
int wiiuse_io_read(struct wiimote_t *wm) {
 return 0;
}


decompiled_code:
ssize_t __fastcall wiiuse_io_write(__int64 a1, const void *a2, int a3)
{
return write(*(_DWORD *)(a1 + 28), a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func5(__int64 a1, const void *a2, int a3)
{
return write(*(_DWORD *)(a1 + 28), a2, a3);
}

original_code:
int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) {
 return write(wm->out_sock, buf, len);
}

canonical_original_code:
int wiiuse_io_write(struct wiimote_t *wm, unsigned char *buf, int len) {
 return write(wm->out_sock, buf, len);
}

variable_types:
buf
unsigned char *

variable_types: len int


original_code:
int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) {
 int i;
 int offset = 0;
 nc->btns = 0;
 nc->btns_held = 0;
 nc->btns_released = 0;
 nc->flags = &wm->flags;
 nc->accel_calib.st_alpha = wm->accel_calib.st_alpha;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   byte* handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 nc->accel_calib.cal_zero.x = data[offset + 0];
 nc->accel_calib.cal_zero.y = data[offset + 1];
 nc->accel_calib.cal_zero.z = data[offset + 2];
 nc->accel_calib.cal_g.x = data[offset + 4];
 nc->accel_calib.cal_g.y = data[offset + 5];
 nc->accel_calib.cal_g.z = data[offset + 6];
 nc->js.max.x = data[offset + 8];
 nc->js.min.x = data[offset + 9];
 nc->js.center.x = data[offset + 10];
 nc->js.max.y = data[offset + 11];
 nc->js.min.y = data[offset + 12];
 nc->js.center.y = data[offset + 13];
 nc->orient_threshold = wm->orient_threshold;
 nc->accel_threshold = wm->accel_threshold;
 wm->exp.type = 1;
 return 1;
}

canonical_original_code:
int nunchuk_handshake(struct wiimote_t *wm, struct nunchuk_t *nc, unsigned char *data, unsigned short len) {
 int i;
 int offset = 0;
 nc->btns = 0;
 nc->btns_held = 0;
 nc->btns_released = 0;
 nc->flags = &wm->flags;
 nc->accel_calib.st_alpha = wm->accel_calib.st_alpha;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   unsigned char *handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 nc->accel_calib.cal_zero.x = data[offset + 0];
 nc->accel_calib.cal_zero.y = data[offset + 1];
 nc->accel_calib.cal_zero.z = data[offset + 2];
 nc->accel_calib.cal_g.x = data[offset + 4];
 nc->accel_calib.cal_g.y = data[offset + 5];
 nc->accel_calib.cal_g.z = data[offset + 6];
 nc->js.max.x = data[offset + 8];
 nc->js.min.x = data[offset + 9];
 nc->js.center.x = data[offset + 10];
 nc->js.max.y = data[offset + 11];
 nc->js.min.y = data[offset + 12];
 nc->js.center.y = data[offset + 13];
 nc->orient_threshold = wm->orient_threshold;
 nc->accel_threshold = wm->accel_threshold;
 wm->exp.type = 1;
 return 1;
}

variable_types: struct wiimote_t { int unid; struct bdaddr_t { unsigned char b[6]; } bdaddr; char bdaddr_str[18]; int out_sock; int in_sock; int state; unsigned char leds; float battery_level; int flags; unsigned char handshake_state; struct read_req_t { void (*cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len); unsigned char *buf; unsigned int addr; unsigned short size; unsigned short wait; unsigned char dirty; struct read_req_t *next; } *read_req; struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct expansion_t { int type; union { struct nunchuk_t { struct accel_t accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } nunchuk; struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t ljs; struct joystick_t rjs; } classic; struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t js; } gh3; }; } exp; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; struct ir_t { struct ir_dot_t { unsigned char visible; unsigned int x; unsigned int y; short rx; short ry; unsigned char order; unsigned char size; } dot[4]; unsigned char num_dots; enum aspect_t { WIIUSE_ASPECT_4_3=0, WIIUSE_ASPECT_16_9=1 } aspect; enum ir_position_t { WIIUSE_IR_ABOVE=0, WIIUSE_IR_BELOW=1 } pos; unsigned int vres[2]; int offset[2]; int state; int ax; int ay; int x; int y; float distance; float z; } ir; unsigned short btns; unsigned short btns_held; unsigned short btns_released; float orient_threshold; int accel_threshold; struct wiimote_state_t { float exp_ljs_ang; float exp_rjs_ang; float exp_ljs_mag; float exp_rjs_mag; unsigned short exp_btns; struct orient_t exp_orient; struct vec3b_t exp_accel; float exp_r_shoulder; float exp_l_shoulder; int ir_ax; int ir_ay; float ir_distance; struct orient_t orient; unsigned short btns; struct vec3b_t accel; } lstate; enum WIIUSE_EVENT_TYPE { WIIUSE_NONE=0, WIIUSE_EVENT=1, WIIUSE_STATUS=2, WIIUSE_CONNECT=3, WIIUSE_DISCONNECT=4, WIIUSE_UNEXPECTED_DISCONNECT=5, WIIUSE_READ_DATA=6, WIIUSE_NUNCHUK_INSERTED=7, WIIUSE_NUNCHUK_REMOVED=8, WIIUSE_CLASSIC_CTRL_INSERTED=9, WIIUSE_CLASSIC_CTRL_REMOVED=10, WIIUSE_GUITAR_HERO_3_CTRL_INSERTED=11, WIIUSE_GUITAR_HERO_3_CTRL_REMOVED=12 } event; unsigned char event_buf[32]; } *

variable_types:
nc
struct nunchuk_t *

variable_types: i int

variable_types: offset int


decompiled_code:
__int64 __fastcall nunchuk_disconnected(_QWORD *a1)
{
__int64 result; // rax

*a1 = 0LL;
a1[11] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 96) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
__int64 result; // rax

*a1 = 0LL;
a1[11] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 96) >> 3));
return result;
}

original_code:
void nunchuk_disconnected(struct nunchuk_t* nc) {
 memset(nc, 0, sizeof(struct nunchuk_t));
}

canonical_original_code:
void nunchuk_disconnected(struct nunchuk_t *nc) {
 memset(nc, 0, sizeof(struct nunchuk_t));
}

variable_types: struct nunchuk_t { struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } *


original_code:
void nunchuk_event(struct nunchuk_t* nc, byte* msg) {
 int i;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 nunchuk_pressed_buttons(nc, msg[5]);
 calc_joystick_state(&nc->js, msg[0], msg[1]);
 nc->accel.x = msg[2];
 nc->accel.y = msg[3];
 nc->accel.z = msg[4];
 calculate_orientation(&nc->accel_calib, &nc->accel, &nc->orient, ((*(nc->flags) & (0x01)) == (0x01)));
 calculate_gforce(&nc->accel_calib, &nc->accel, &nc->gforce);
}

canonical_original_code:
void nunchuk_event(struct nunchuk_t *nc, unsigned char *msg) {
 int i;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 nunchuk_pressed_buttons(nc, msg[5]);
 calc_joystick_state(&nc->js, msg[0], msg[1]);
 nc->accel.x = msg[2];
 nc->accel.y = msg[3];
 nc->accel.z = msg[4];
 calculate_orientation(&nc->accel_calib, &nc->accel, &nc->orient, ((*(nc->flags) & (0x01)) == (0x01)));
 calculate_gforce(&nc->accel_calib, &nc->accel, &nc->gforce);
}

variable_types:
msg
unsigned char *


decompiled_code:
void __fastcall wiiuse_set_nunchuk_orient_threshold(__int64 a1, float a2)
{
if ( a1 )
*(float *)(a1 + 148) = a2;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, float a2)
{
if ( a1 )
*(float *)(a1 + 148) = a2;
}

original_code:
void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold) {
 if (!wm) return;
 wm->exp.nunchuk.orient_threshold = threshold;
}

canonical_original_code:
void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t *wm, float threshold) {
 if (!wm) return;
 wm->exp.nunchuk.orient_threshold = threshold;
}

variable_types: threshold float


decompiled_code:
void __fastcall wiiuse_set_nunchuk_accel_threshold(__int64 a1, int a2)
{
if ( a1 )
*(_DWORD *)(a1 + 152) = a2;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
if ( a1 )
*(_DWORD *)(a1 + 152) = a2;
}

original_code:
void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold) {
 if (!wm) return;
 wm->exp.nunchuk.accel_threshold = threshold;
}

canonical_original_code:
void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t *wm, int threshold) {
 if (!wm) return;
 wm->exp.nunchuk.accel_threshold = threshold;
}

variable_types: threshold int


original_code:
int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len) {
 int i;
 int offset = 0;
 gh3->btns = 0;
 gh3->btns_held = 0;
 gh3->btns_released = 0;
 gh3->whammy_bar = 0.0f;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   byte* handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 gh3->js.max.x = 0xFC;
 gh3->js.min.x = 0xC5;
 gh3->js.center.x = 0xE0;
 gh3->js.max.y = 0xFA;
 gh3->js.min.y = 0xC5;
 gh3->js.center.y = 0xE0;
 wm->exp.type = 3;
 return 1;
}

canonical_original_code:
int guitar_hero_3_handshake(struct wiimote_t *wm, struct guitar_hero_3_t *gh3, unsigned char *data, unsigned short len) {
 int i;
 int offset = 0;
 gh3->btns = 0;
 gh3->btns_held = 0;
 gh3->btns_released = 0;
 gh3->whammy_bar = 0.0f;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   unsigned char *handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 gh3->js.max.x = 0xFC;
 gh3->js.min.x = 0xC5;
 gh3->js.center.x = 0xE0;
 gh3->js.max.y = 0xFA;
 gh3->js.min.y = 0xC5;
 gh3->js.center.y = 0xE0;
 wm->exp.type = 3;
 return 1;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned short


decompiled_code:
void __fastcall guitar_hero_3_disconnected(__int64 a1)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 24) = 0;
}

original_code:
void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3) {
 memset(gh3, 0, sizeof(struct guitar_hero_3_t));
}

canonical_original_code:
void guitar_hero_3_disconnected(struct guitar_hero_3_t *gh3) {
 memset(gh3, 0, sizeof(struct guitar_hero_3_t));
}

variable_types: struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; } *


original_code:
void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) {
 int i;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 guitar_hero_3_pressed_buttons(gh3, (htons(*(short*)(msg + 4))));
 gh3->whammy_bar = (msg[3] - 0xEF) / (float)(0xFA - 0xEF);
 calc_joystick_state(&gh3->js, msg[0], msg[1]);
}

canonical_original_code:
void guitar_hero_3_event(struct guitar_hero_3_t *gh3, unsigned char *msg) {
 int i;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 guitar_hero_3_pressed_buttons(gh3, (htons(*(short *)(msg + 4))));
 gh3->whammy_bar = (msg[3] - 0xEF) / (float)(0xFA - 0xEF);
 calc_joystick_state(&gh3->js, msg[0], msg[1]);
}


original_code:
void wiiuse_handshake(struct wiimote_t* wm, byte* data, unsigned short len) {
 if (!wm) return;
 switch (wm->handshake_state) {
  case 0:
  {
   byte* buf;
   (wm->state |= (0x0002));
   wiiuse_set_leds(wm, 0x00);
   buf = (byte*)malloc(sizeof(byte) * 8);
   wiiuse_read_data_cb(wm, wiiuse_handshake, buf, 0x16, 7);
   wm->handshake_state++;
   wiiuse_set_leds(wm, 0x00);
   break;
  }
  case 1:
  {
   struct read_req_t* req = wm->read_req;
   struct accel_t* accel = &wm->accel_calib;
   accel->cal_zero.x = req->buf[0];
   accel->cal_zero.y = req->buf[1];
   accel->cal_zero.z = req->buf[2];
   accel->cal_g.x = req->buf[4] - accel->cal_zero.x;
   accel->cal_g.y = req->buf[5] - accel->cal_zero.y;
   accel->cal_g.z = req->buf[6] - accel->cal_zero.z;
   free(req->buf);
   ;
   wiiuse_status(wm);
   (wm->state &= ~(0x0002));
   (wm->state |= (0x0004));
   wm->handshake_state++;
   if (((wm->state & (0x0080)) == (0x0080))) {
    ;
    (wm->state &= ~(0x0080));
    wiiuse_set_ir(wm, 1);
   }
   break;
  }
  default:
  {
   break;
  }
 }
}

canonical_original_code:
void wiiuse_handshake(struct wiimote_t *wm, unsigned char *data, unsigned short len) {
 if (!wm) return;
 switch (wm->handshake_state) {
  case 0:
  {
   unsigned char *buf;
   (wm->state |= (0x0002));
   wiiuse_set_leds(wm, 0x00);
   buf = (unsigned char *)malloc(sizeof(byte) * 8);
   wiiuse_read_data_cb(wm, wiiuse_handshake, buf, 0x16, 7);
   wm->handshake_state++;
   wiiuse_set_leds(wm, 0x00);
   break;
  }
  case 1:
  {
   struct read_req_t *req = wm->read_req;
   struct accel_t *accel = &wm->accel_calib;
   accel->cal_zero.x = req->buf[0];
   accel->cal_zero.y = req->buf[1];
   accel->cal_zero.z = req->buf[2];
   accel->cal_g.x = req->buf[4] - accel->cal_zero.x;
   accel->cal_g.y = req->buf[5] - accel->cal_zero.y;
   accel->cal_g.z = req->buf[6] - accel->cal_zero.z;
   free(req->buf);
   ;
   wiiuse_status(wm);
   (wm->state &= ~(0x0002));
   (wm->state |= (0x0004));
   wm->handshake_state++;
   if (((wm->state & (0x0080)) == (0x0080))) {
    ;
    (wm->state &= ~(0x0080));
    wiiuse_set_ir(wm, 1);
   }
   break;
  }
  default:
  {
   break;
  }
 }
}

variable_types: struct read_req_t { void (*cb)(struct wiimote_t { int unid; struct bdaddr_t { unsigned char b[6]; } bdaddr; char bdaddr_str[18]; int out_sock; int in_sock; int state; unsigned char leds; float battery_level; int flags; unsigned char handshake_state; struct read_req_t *read_req; struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } accel_calib; struct expansion_t { int type; union { struct nunchuk_t { struct accel_t accel_calib; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } js; int *flags; unsigned char btns; unsigned char btns_held; unsigned char btns_released; float orient_threshold; int accel_threshold; struct vec3b_t accel; struct orient_t { float roll; float pitch; float yaw; float a_roll; float a_pitch; } orient; struct gforce_t { float x; } gforce; } nunchuk; struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t ljs; struct joystick_t rjs; } classic; struct guitar_hero_3_t { short btns; short btns_held; short btns_released; float whammy_bar; struct joystick_t js; } gh3; }; } exp; struct vec3b_t accel; struct orient_t orient; struct gforce_t gforce; struct ir_t { struct ir_dot_t { unsigned char visible; unsigned int x; unsigned int y; short rx; short ry; unsigned char order; unsigned char size; } dot[4]; unsigned char num_dots; enum aspect_t { WIIUSE_ASPECT_4_3=0, WIIUSE_ASPECT_16_9=1 } aspect; enum ir_position_t { WIIUSE_IR_ABOVE=0, WIIUSE_IR_BELOW=1 } pos; unsigned int vres[2]; int offset[2]; int state; int ax; int ay; int x; int y; float distance; float z; } ir; unsigned short btns; unsigned short btns_held; unsigned short btns_released; float orient_threshold; int accel_threshold; struct wiimote_state_t { float exp_ljs_ang; float exp_rjs_ang; float exp_ljs_mag; float exp_rjs_mag; unsigned short exp_btns; struct orient_t exp_orient; struct vec3b_t exp_accel; float exp_r_shoulder; float exp_l_shoulder; int ir_ax; int ir_ay; float ir_distance; struct orient_t orient; unsigned short btns; struct vec3b_t accel; } lstate; enum WIIUSE_EVENT_TYPE { WIIUSE_NONE=0, WIIUSE_EVENT=1, WIIUSE_STATUS=2, WIIUSE_CONNECT=3, WIIUSE_DISCONNECT=4, WIIUSE_UNEXPECTED_DISCONNECT=5, WIIUSE_READ_DATA=6, WIIUSE_NUNCHUK_INSERTED=7, WIIUSE_NUNCHUK_REMOVED=8, WIIUSE_CLASSIC_CTRL_INSERTED=9, WIIUSE_CLASSIC_CTRL_REMOVED=10, WIIUSE_GUITAR_HERO_3_CTRL_INSERTED=11, WIIUSE_GUITAR_HERO_3_CTRL_REMOVED=12 } event; unsigned char event_buf[32]; } *wm, unsigned char *data, unsigned short len); unsigned char *buf; unsigned int addr; unsigned short size; unsigned short wait; unsigned char dirty; struct read_req_t *next; } *

variable_types: struct accel_t { struct vec3b_t { unsigned char x; } cal_zero; struct vec3b_t cal_g; float st_roll; float st_pitch; float st_alpha; } *


original_code:
int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) {
 int i;
 int offset = 0;
 cc->btns = 0;
 cc->btns_held = 0;
 cc->btns_released = 0;
 cc->r_shoulder = 0;
 cc->l_shoulder = 0;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   byte* handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 cc->ljs.max.x = data[0 + offset] / 4;
 cc->ljs.min.x = data[1 + offset] / 4;
 cc->ljs.center.x = data[2 + offset] / 4;
 cc->ljs.max.y = data[3 + offset] / 4;
 cc->ljs.min.y = data[4 + offset] / 4;
 cc->ljs.center.y = data[5 + offset] / 4;
 cc->rjs.max.x = data[6 + offset] / 8;
 cc->rjs.min.x = data[7 + offset] / 8;
 cc->rjs.center.x = data[8 + offset] / 8;
 cc->rjs.max.y = data[9 + offset] / 8;
 cc->rjs.min.y = data[10 + offset] / 8;
 cc->rjs.center.y = data[11 + offset] / 8;
 wm->exp.type = 2;
 return 1;
}

canonical_original_code:
int classic_ctrl_handshake(struct wiimote_t *wm, struct classic_ctrl_t *cc, unsigned char *data, unsigned short len) {
 int i;
 int offset = 0;
 cc->btns = 0;
 cc->btns_held = 0;
 cc->btns_released = 0;
 cc->r_shoulder = 0;
 cc->l_shoulder = 0;
 for (i = 0; i < len; ++i)
  data[i] = (data[i] ^ 0x17) + 0x17;
 if (data[offset] == 0xFF) {
  if (data[offset + 16] == 0xFF) {
   unsigned char *handshake_buf = malloc(224 * sizeof(byte));
   ;
   wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, 0x04A40020, 224);
   return 0;
  } else
   offset += 16;
 }
 cc->ljs.max.x = data[0 + offset] / 4;
 cc->ljs.min.x = data[1 + offset] / 4;
 cc->ljs.center.x = data[2 + offset] / 4;
 cc->ljs.max.y = data[3 + offset] / 4;
 cc->ljs.min.y = data[4 + offset] / 4;
 cc->ljs.center.y = data[5 + offset] / 4;
 cc->rjs.max.x = data[6 + offset] / 8;
 cc->rjs.min.x = data[7 + offset] / 8;
 cc->rjs.center.x = data[8 + offset] / 8;
 cc->rjs.max.y = data[9 + offset] / 8;
 cc->rjs.min.y = data[10 + offset] / 8;
 cc->rjs.center.y = data[11 + offset] / 8;
 wm->exp.type = 2;
 return 1;
}

variable_types:
cc
struct classic_ctrl_t *


decompiled_code:
void __fastcall classic_ctrl_disconnected(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
a1[3] = 0LL;
a1[4] = 0LL;
a1[5] = 0LL;
}

original_code:
void classic_ctrl_disconnected(struct classic_ctrl_t* cc) {
 memset(cc, 0, sizeof(struct classic_ctrl_t));
}

canonical_original_code:
void classic_ctrl_disconnected(struct classic_ctrl_t *cc) {
 memset(cc, 0, sizeof(struct classic_ctrl_t));
}

variable_types: struct classic_ctrl_t { short btns; short btns_held; short btns_released; float r_shoulder; float l_shoulder; struct joystick_t { struct vec2b_t { unsigned char x; } max; struct vec2b_t min; struct vec2b_t center; float ang; float mag; } ljs; struct joystick_t rjs; } *


original_code:
void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg) {
 int i, lx, ly, rx, ry;
 byte l, r;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 classic_ctrl_pressed_buttons(cc, (htons(*(short*)(msg + 4))));
 l = (((msg[2] & 0x60) >> 2) | ((msg[3] & 0xE0) >> 5));
 r = (msg[3] & 0x1F);
 cc->r_shoulder = ((float)r / 0x1F);
 cc->l_shoulder = ((float)l / 0x1F);
 lx = (msg[0] & 0x3F);
 ly = (msg[1] & 0x3F);
 rx = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7);
 ry = (msg[2] & 0x1F);
 calc_joystick_state(&cc->ljs, lx, ly);
 calc_joystick_state(&cc->rjs, rx, ry);
}

canonical_original_code:
void classic_ctrl_event(struct classic_ctrl_t *cc, unsigned char *msg) {
 int i; int lx; int ly; int rx; int ry;
 unsigned char l; unsigned char r;
 for (i = 0; i < 6; ++i)
  msg[i] = (msg[i] ^ 0x17) + 0x17;
 classic_ctrl_pressed_buttons(cc, (htons(*(short *)(msg + 4))));
 l = (((msg[2] & 0x60) >> 2) | ((msg[3] & 0xE0) >> 5));
 r = (msg[3] & 0x1F);
 cc->r_shoulder = ((float)r / 0x1F);
 cc->l_shoulder = ((float)l / 0x1F);
 lx = (msg[0] & 0x3F);
 ly = (msg[1] & 0x3F);
 rx = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7);
 ry = (msg[2] & 0x1F);
 calc_joystick_state(&cc->ljs, lx, ly);
 calc_joystick_state(&cc->rjs, rx, ry);
}

variable_types: lx int

variable_types: ly int

variable_types: rx int

variable_types: ry int

variable_types:
l
unsigned char

variable_types:
r
unsigned char


threadRoutine

repo: sergforce/iscsi

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

v1 = pthread_self();
pthread_detach(v1);
return (int)doClient2((unsigned int)a1);
}

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

v1 = pthread_self();
pthread_detach(v1);
return (int)doClient2((unsigned int)a1);
}

original_code:
void *threadRoutine(void *arg)
{
 int sockfd = (intptr_t)arg;
 pthread_detach(pthread_self());
 return (void *)(intptr_t)doClient2(sockfd);
}

canonical_original_code:
void *threadRoutine(void *arg)
{
 int sockfd = (intptr_t)arg;
 pthread_detach(pthread_self());
 return (void *)(long)doClient2(sockfd);
}

variable_types:
arg
void *

variable_types: sockfd int

return_type: void *


createNewClientThread

decompiled_code:
int __fastcall createNewClientThread(int a1)
{
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

return pthread_create(newthread, 0LL, (void *(*)(void *))threadRoutine, (void *)a1);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

return pthread_create(newthread, 0LL, (void *(*)(void *))threadRoutine, (void *)a1);
}

original_code:
int createNewClientThread(SOCKET scli)
{
 pthread_t tid;
 return pthread_create(&tid, ((void *)0), threadRoutine, (void *)(intptr_t)scli);
}

canonical_original_code:
int createNewClientThread(int scli)
{
 unsigned long tid;
 return pthread_create(&tid, ((void *)0), threadRoutine, (void *)(long)scli);
}

variable_types: scli int

variable_types:
tid
unsigned long