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

if ( (int)check_device_constprop_0(a1, 0x185u, "gpppcprog_output") < 0 )
result = 0xFFFFFFFFLL;
else
result = gpppcprog_send(a1, 4, a2, 0x24u);
return result;
}

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

if ( (int)func4(a1, 0x185u, "gpppcprog_output") < 0 )
result = 0xFFFFFFFFLL;
else
result = func5(a1, 4, a2, 0x24u);
return result;
}

original_code:
int8_t gpppcprog_output(int id, int8_t output[36])
{
  if(check_device(id, "pcprog.c", 389, __func__) < 0) { return -1; }
  return gpppcprog_send(id, 0x04, (uint8_t *)output, 36);
}

canonical_original_code:
char gpppcprog_output(int id, char output[36])
{
  if(check_device(id, "pcprog.c", 389, __func__) < 0) { return -1; }
  return gpppcprog_send(id, 0x04, (unsigned char *)output, 36);
}

variable_types: id int

variable_types:
output
char [36]


repo: matlo/GIMX

decompiled_code:
__int64 gimxgpp_log_constructor()
{
return glog_register("gimxgpp", gimxgpp_set_log_level);
}

canonical_decompiled_code:
__int64 func13()
{
return glog_register("gimxgpp", gimxgpp_set_log_level);
}

original_code: void gimxgpp_log_constructor(void) { glog_register("gimxgpp", gimxgpp_set_log_level); }

canonical_original_code: void gimxgpp_log_constructor() { glog_register("gimxgpp", gimxgpp_set_log_level); }

return_type: void


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

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

original_code:
static int bt_device_bluez_device_init()
{
  return 0;
}

return_type: int


decompiled_code:
__int64 __fastcall bt_device_bluez_write_device_class(__int64 a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // er12

v2 = hci_open_dev();
if ( v2 < 0 )
{
v4 = -1;
perror("hci_open_dev");
}
else
{
v3 = v2;
v4 = hci_write_class_of_dev((unsigned int)v2, a2, 1000LL);
if ( v4 < 0 )
perror("hci_write_class_of_dev");
hci_close_dev(v3);
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // er12

v2 = hci_open_dev();
if ( v2 < 0 )
{
v4 = -1;
perror("hci_open_dev");
}
else
{
v3 = v2;
v4 = hci_write_class_of_dev((unsigned int)v2, a2, 1000LL);
if ( v4 < 0 )
perror("hci_write_class_of_dev");
hci_close_dev(v3);
}
return (unsigned int)v4;
}

original_code:
static int bt_device_bluez_write_device_class(int device_number, uint32_t devclass)
{
  int ret = 0;
  int s = hci_open_dev (device_number);
  if(s >= 0)
  {
    ret = hci_write_class_of_dev(s, devclass, 1000);
    if(ret < 0)
    {
      perror("hci_write_class_of_dev");
    }
    hci_close_dev(s);
  }
  else
  {
    perror("hci_open_dev");
    ret = -1;
  }
  return ret;
}

canonical_original_code:
static int bt_device_bluez_write_device_class(int device_number, unsigned int devclass)
{
  int ret = 0;
  int s = hci_open_dev (device_number);
  if(s >= 0)
  {
    ret = hci_write_class_of_dev(s, devclass, 1000);
    if(ret < 0)
    {
      perror("hci_write_class_of_dev");
    }
    hci_close_dev(s);
  }
  else
  {
    perror("hci_open_dev");
    ret = -1;
  }
  return ret;
}

variable_types:
devclass
unsigned int

variable_types: ret int

variable_types: s int


decompiled_code:
__int64 __fastcall bt_device_bluez_get_device_bdaddr(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // er12

v2 = hci_open_dev();
if ( v2 < 0 )
{
v4 = -1;
perror("hci_open_dev");
}
else
{
v3 = v2;
v4 = hci_read_bd_addr((unsigned int)v2, a2, 1000LL);
if ( v4 < 0 )
perror("hci_read_bd_addr");
hci_close_dev(v3);
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // er12

v2 = hci_open_dev();
if ( v2 < 0 )
{
v4 = -1;
perror("hci_open_dev");
}
else
{
v3 = v2;
v4 = hci_read_bd_addr((unsigned int)v2, a2, 1000LL);
if ( v4 < 0 )
perror("hci_read_bd_addr");
hci_close_dev(v3);
}
return (unsigned int)v4;
}

original_code:
static int bt_device_bluez_get_device_bdaddr(int device_number, bdaddr_t* ba)
{
  int ret = 0;
  int s = hci_open_dev (device_number);
  if(s >= 0)
  {
    ret = hci_read_bd_addr(s, ba, 1000);
    if(ret < 0)
    {
      perror("hci_read_bd_addr");
    }
    hci_close_dev(s);
  }
  else
  {
    perror("hci_open_dev");
    ret = -1;
  }
  return ret;
}

canonical_original_code:
static int bt_device_bluez_get_device_bdaddr(int device_number, struct bdaddr_t *ba)
{
  int ret = 0;
  int s = hci_open_dev (device_number);
  if(s >= 0)
  {
    ret = hci_read_bd_addr(s, ba, 1000);
    if(ret < 0)
    {
      perror("hci_read_bd_addr");
    }
    hci_close_dev(s);
  }
  else
  {
    perror("hci_open_dev");
    ret = -1;
  }
  return ret;
}

variable_types: struct bdaddr_t { unsigned char b[6]; } *


decompiled_code:
__int64 bt_device_bluez_init()
{
return bt_device_abs_register(1LL, &bt_device_bluez);
}

canonical_decompiled_code:
__int64 func3()
{
return bt_device_abs_register(1LL, &bt_device_bluez);
}

original_code:
void bt_device_bluez_init(void)
{
  bt_device_abs_register(E_BT_ABS_BLUEZ, &bt_device_bluez);
}

canonical_original_code:
void bt_device_bluez_init()
{
  bt_device_abs_register(E_BT_ABS_BLUEZ, &bt_device_bluez);
}


getANSIColor

decompiled_code:
const char *__fastcall getANSIColor(int a1)
{
const char *result; // rax

result = "\x1B[01;31m";
if ( a1 == 14 )
result = "\x1B[01;33m";
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

result = "\x1B[01;31m";
if ( a1 == 14 )
result = "\x1B[01;33m";
return result;
}

original_code:
static inline const char * getANSIColor(const int c) {
    switch (c) {
        case BLACK : return ANSI_BLACK;
        case BLUE : return ANSI_BLUE;
        case GREEN : return ANSI_GREEN;
        case CYAN : return ANSI_CYAN;
        case RED : return ANSI_RED;
        case MAGENTA : return ANSI_MAGENTA;
        case BROWN : return ANSI_BROWN;
        case GREY : return ANSI_GREY;
        case DARKGREY : return ANSI_DARKGREY;
        case LIGHTBLUE : return ANSI_LIGHTBLUE;
        case LIGHTGREEN : return ANSI_LIGHTGREEN;
        case LIGHTCYAN : return ANSI_LIGHTCYAN;
        case LIGHTRED : return ANSI_LIGHTRED;
        case LIGHTMAGENTA: return ANSI_LIGHTMAGENTA;
        case YELLOW : return ANSI_YELLOW;
        case WHITE : return ANSI_WHITE;
        default: return "";
    }
}

canonical_original_code:
static inline char *getANSIColor(int c) {
    switch (c) {
        case BLACK : return ANSI_BLACK;
        case BLUE : return ANSI_BLUE;
        case GREEN : return ANSI_GREEN;
        case CYAN : return ANSI_CYAN;
        case RED : return ANSI_RED;
        case MAGENTA : return ANSI_MAGENTA;
        case BROWN : return ANSI_BROWN;
        case GREY : return ANSI_GREY;
        case DARKGREY : return ANSI_DARKGREY;
        case LIGHTBLUE : return ANSI_LIGHTBLUE;
        case LIGHTGREEN : return ANSI_LIGHTGREEN;
        case LIGHTCYAN : return ANSI_LIGHTCYAN;
        case LIGHTRED : return ANSI_LIGHTRED;
        case LIGHTMAGENTA: return ANSI_LIGHTMAGENTA;
        case YELLOW : return ANSI_YELLOW;
        case WHITE : return ANSI_WHITE;
        default: return "";
    }
}

variable_types: c int

return_type: char *


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

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

original_code:
int ignore_event(GE_Event* event __attribute__((unused)))
{
  return 0;
}

canonical_original_code:
int ignore_event(union GE_Event *event)
{
  return 0;
}

variable_types: union GE_Event { struct { unsigned char type; unsigned char which; }; struct GE_KeyboardEvent { unsigned char type; unsigned char which; unsigned short keysym; } key; struct GE_MouseMotionEvent { unsigned char type; unsigned char which; short xrel; short yrel; } motion; struct GE_MouseButtonEvent { unsigned char type; unsigned char which; unsigned char button; } button; struct GE_JoyAxisEvent { unsigned char type; unsigned char which; unsigned char axis; short value; } jaxis; struct GE_JoyHatEvent { unsigned char type; unsigned char which; unsigned char hat; unsigned char value; } jhat; struct GE_JoyButtonEvent { unsigned char type; unsigned char which; unsigned char button; unsigned char state; } jbutton; struct GE_JoyRumbleEvent { unsigned char type; unsigned char which; unsigned short weak; unsigned short strong; } jrumble; struct GE_JoyConstantForceEvent { unsigned char type; unsigned char which; short level; } jconstant; struct GE_JoyConditionForceEvent { unsigned char type; unsigned char which; struct { unsigned short left; unsigned short right; } saturation; struct { short left; short right; } coefficient; short center; unsigned short deadband; } jcondition; struct GE_JoyPeriodicForceEvent { unsigned char type; unsigned char which; struct { int direction; unsigned short period; short magnitude; short offset; } sine; } jperiodic; } *

user_defined_types: union GE_Event { struct { unsigned char type; unsigned char which; }; struct GE_KeyboardEvent key; struct GE_MouseMotionEvent motion; struct GE_MouseButtonEvent button; struct GE_JoyAxisEvent jaxis; struct GE_JoyHatEvent jhat; struct GE_JoyButtonEvent jbutton; struct GE_JoyRumbleEvent jrumble; struct GE_JoyConstantForceEvent jconstant; struct GE_JoyConditionForceEvent jcondition; struct GE_JoyPeriodicForceEvent jperiodic; }


terminate

decompiled_code:
void terminate()
{
set_done();
}

canonical_decompiled_code:
void func2()
{
set_done();
}

original_code:
void terminate(int sig __attribute__((unused)))
{
  set_done();
}

canonical_original_code:
void terminate(int sig)
{
  set_done();
}

variable_types: sig int


resetColorStdout

decompiled_code:
int resetColorStdout()
{
return printf("%s", "\x1B[0m");
}

canonical_decompiled_code:
int func4()
{
return printf("%s", "\x1B[0m");
}

original_code:
static inline void resetColorStdout(void) {
    printf("%s", ANSI_ATTRIBUTE_RESET);
}

canonical_original_code:
static inline void resetColorStdout() {
    printf("%s", ANSI_ATTRIBUTE_RESET);
}


resetColorStderr

decompiled_code:
size_t resetColorStderr()
{
return fwrite("\x1B[0m", 1uLL, 4uLL, stderr);
}

canonical_decompiled_code:
size_t func5()
{
return fwrite("\x1B[0m", 1uLL, 4uLL, stderr);
}

original_code:
static inline void resetColorStderr(void) {
    fprintf(stderr, "%s", ANSI_ATTRIBUTE_RESET);
}

canonical_original_code:
static inline void resetColorStderr() {
    fprintf(stderr, "%s", ANSI_ATTRIBUTE_RESET);
}


grab

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

result = *((unsigned int *)&gimx_params + 6);
if ( (_DWORD)result )
result = ginput_grab();
return result;
}

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

result = *((unsigned int *)&gimx_params + 6);
if ( (_DWORD)result )
result = ginput_grab();
return result;
}

original_code:
void grab()
{
  if(gimx_params.grab)
  {
    ginput_grab();
  }
}


decompiled_code:
int show_devices()
{
unsigned int v0; // ebx
unsigned int v1; // eax
__int64 v2; // rsi
const char *v3; // r12
unsigned int v4; // ebx
unsigned int v5; // eax
__int64 v6; // rsi
const char *v7; // r12
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 v10; // rsi
__int64 v11; // rax
const char *v12; // r12

v0 = 0;
do
{
v3 = (const char *)ginput_mouse_name(v0);
if ( !v3 )
break;
v1 = ginput_mouse_virtual_id(v0);
v2 = v0++;
printf("mouse %d has name '%s' and virtual id %d\n", v2, v3, v1);
}
while ( v0 != 256 );
v4 = 0;
do
{
v7 = (const char *)ginput_keyboard_name(v4);
if ( !v7 )
break;
v5 = ginput_keyboard_virtual_id(v4);
v6 = v4++;
printf("keyboard %d has name '%s' and virtual id %d\n", v6, v7, v5);
}
while ( v4 != 256 );
v8 = 0;
do
{
v11 = ginput_joystick_name(v8);
v12 = (const char *)v11;
if ( !v11 )
break;
v9 = ginput_joystick_virtual_id(v8);
v10 = v8++;
LODWORD(v11) = printf("joystick %d has name '%s' and virtual id %d\n", v10, v12, v9);
}
while ( v8 != 256 );
return v11;
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // ebx
unsigned int v1; // eax
__int64 v2; // rsi
const char *v3; // r12
unsigned int v4; // ebx
unsigned int v5; // eax
__int64 v6; // rsi
const char *v7; // r12
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 v10; // rsi
__int64 v11; // rax
const char *v12; // r12

v0 = 0;
do
{
v3 = (const char *)ginput_mouse_name(v0);
if ( !v3 )
break;
v1 = ginput_mouse_virtual_id(v0);
v2 = v0++;
printf("mouse %d has name '%s' and virtual id %d\n", v2, v3, v1);
}
while ( v0 != 256 );
v4 = 0;
do
{
v7 = (const char *)ginput_keyboard_name(v4);
if ( !v7 )
break;
v5 = ginput_keyboard_virtual_id(v4);
v6 = v4++;
printf("keyboard %d has name '%s' and virtual id %d\n", v6, v7, v5);
}
while ( v4 != 256 );
v8 = 0;
do
{
v11 = ginput_joystick_name(v8);
v12 = (const char *)v11;
if ( !v11 )
break;
v9 = ginput_joystick_virtual_id(v8);
v10 = v8++;
LODWORD(v11) = printf("joystick %d has name '%s' and virtual id %d\n", v10, v12, v9);
}
while ( v8 != 256 );
return v11;
}

original_code:
void show_devices()
{
  const char * name;
  int i;
  for (i = 0; i < 256 && (name = ginput_mouse_name(i)) != ((void *)0); ++i)
  {
    printf("mouse %d has name '%s' and virtual id %d\n", i, name, ginput_mouse_virtual_id(i));
  }
  for (i = 0; i < 256 && (name = ginput_keyboard_name(i)) != ((void *)0); ++i)
  {
    printf("keyboard %d has name '%s' and virtual id %d\n", i, name, ginput_keyboard_virtual_id(i));
  }
  for (i = 0; i < 256 && (name = ginput_joystick_name(i)) != ((void *)0); ++i)
  {
    printf("joystick %d has name '%s' and virtual id %d\n", i, name, ginput_joystick_virtual_id(i));
  }
}

canonical_original_code:
void show_devices()
{
  char *name;
  int i;
  for (i = 0; i < 256 && (name = ginput_mouse_name(i)) != ((void *)0); ++i)
  {
    printf("mouse %d has name '%s' and virtual id %d\n", i, name, ginput_mouse_virtual_id(i));
  }
  for (i = 0; i < 256 && (name = ginput_keyboard_name(i)) != ((void *)0); ++i)
  {
    printf("keyboard %d has name '%s' and virtual id %d\n", i, name, ginput_keyboard_virtual_id(i));
  }
  for (i = 0; i < 256 && (name = ginput_joystick_name(i)) != ((void *)0); ++i)
  {
    printf("joystick %d has name '%s' and virtual id %d\n", i, name, ginput_joystick_virtual_id(i));
  }
}

variable_types:
name
char *

variable_types: i int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rsi
void *v5; // rax
unsigned int v6; // er12
int v7; // ebx
__int64 v8; // rax
unsigned int v9; // er12
unsigned int v10; // ebx
__int64 v11; // rax
__int64 v12; // rdx
bool v13; // zf
__int64 v14; // rdx
FILE *v15; // rdi
int v16; // eax
char *v18; // r13
FILE *v19; // rax
FILE *v20; // r13
__int64 (__fastcall *v21)(char *); // rdx
bool v22; // cf
unsigned int v23; // ebx
unsigned int v24; // ebp
char *v25; // rax
__int64 v26; // rax
int v27; // ebx
char *v28; // rax
__int64 v29; // rax
int v30; // eax
unsigned int v31; // eax
__int64 v32; // r12
__int64 v33; // [rsp-10h] [rbp-1058h]
__int128 v34; // [rsp+0h] [rbp-1048h] BYREF
struct termios s[68]; // [rsp+10h] [rbp-1038h] BYREF
char v36; // [rsp+1010h] [rbp-38h]

v34 = 0LL;
LOBYTE(v34) = 1;
glog_set_all_levels(2LL, argv, envp);
signal(2, (__sighandler_t)terminate);
signal(15, (__sighandler_t)terminate);
signal(1, (__sighandler_t)terminate);
setlocale(6, &locale);
bindtextdomain("gimx", "/usr/share/locale");
textdomain("gimx");
v4 = "C";
setlocale(1, "C");
v5 = (void *)gfile_homedir();
gimx_params = v5;
if ( !v5 )
goto LABEL_16;
gpppcprog_read_user_ids(v5, "/.gimx/");
v4 = (const char *)argv;
if ( (int)args_read((unsigned int)argc, argv, &gimx_params) < 0 )
goto LABEL_16;
if ( *((_DWORD *)&gimx_params + 38) )
bt_abs_value = 0;
v6 = adapter_detect();
if ( v6 )
{
adapter_clean();
goto LABEL_17;
}
v7 = *((_DWORD *)&gimx_params + 7);
if ( v7 == -1 )
{
v26 = adapter_get(0LL);
v27 = controller_get_default_refresh_period(*(unsigned int *)(v26 + 736));
*((_DWORD *)&gimx_params + 7) = v27;
v13 = *((_DWORD *)&gimx_params + 12) == 0;
*((_DWORD *)&gimx_params + 34) = 33750 / v27;
if ( v13 )
{
v28 = dcgettext(0LL, "using default refresh period: %.02fms\n", 5);
printf(v28, (double)v27 / 1000.0);
fflush(stdout);
}
}
else
{
v8 = adapter_get(0LL);
if ( v7 < (int)controller_get_min_refresh_period(*(unsigned int *)(v8 + 736)) )
{
setColorStderr_constprop_0();
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v29 = adapter_get(0LL);
v30 = controller_get_min_refresh_period(*(unsigned int *)(v29 + 736));
v4 = "Refresh period should be at least %.02fms\n";
fprintf(stderr, "Refresh period should be at least %.02fms\n", (double)v30 / 1000.0);
goto LABEL_16;
}
}
v9 = 0;
v10 = 0;
*((double *)&gimx_params + 4) = 11250.0 / (double)*((int *)&gimx_params + 7);
do
{
v11 = adapter_get(v10);
v4 = (const char *)*(unsigned int *)(v11 + 748);
if ( (_DWORD)v4 )
{
v13 = *(_QWORD *)(v11 + 312) == 0LL;
v9 = 1;
*(_DWORD *)(v11 + 916) = 1;
if ( v13 && !*((_DWORD *)&gimx_params + 12) )
{
puts("The --event argument may require running two gimx instances.");
fflush(stdout);
}
}
++v10;
}
while ( v10 != 7 );
if ( v9 )
{
if ( (int)adapter_start() < 0 )
{
LABEL_16:
v6 = -1;
adapter_clean();
goto LABEL_17;
}
v23 = 0;
adapter_send();
goto LABEL_68;
}
if ( *(&gimx_params + 16) )
{
v21 = (__int64 (__fastcall *)(char *))ignore_event;
if ( !*(&gimx_params + 2) )
v21 = process_event;
v22 = *((_DWORD *)&gimx_params + 36) == 0;
*(_QWORD *)&s[0].c_iflag = &gpoll_register_fd;
*(_QWORD *)&s[0].c_cflag = &gpoll_remove_fd;
v4 = (const char *)(1 - ((unsigned int)v22 - 1));
if ( (int)ginput_init(s, v4, v21) < 0 )
goto LABEL_16;
if ( *(&gimx_params + 21) )
{
show_devices();
show_config();
}
cal_init();
cfg_intensity_init();
cfg_init_ffb_tweaks();
if ( (int)read_config_file(*(&gimx_params + 16)) < 0 )
goto LABEL_16;
if ( (unsigned int)ginput_get_mk_mode() == 1 )
{
cfg_clean();
ginput_free_mk_names();
cal_init();
cfg_intensity_init();
cfg_init_ffb_tweaks();
read_config_file(*(&gimx_params + 16));
}
cfg_read_calibration();
cfg_pair_mouse_mappers();
if ( *((_DWORD *)&gimx_params + 47) )
{
*((_DWORD *)&gimx_params + 6) = 0;
do
{
v4 = (const char *)v9;
if ( (unsigned int)adapter_get_device(2LL, v9) != -1
|| (v4 = (const char *)v9, (unsigned int)adapter_get_device(1LL, v9) != -1) )
{
*((_DWORD *)&gimx_params + 6) = 1;
}
++v9;
}
while ( v9 != 7 );
}
grab();
ginput_release_unused();
macros_init();
if ( *(&gimx_params + 2) )
{
WORD1(v34) = ginput_key_id();
if ( !WORD1(v34) )
{
setColorStderr_constprop_0();
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v32 = (__int64)*(&gimx_params + 2);
v4 = dcgettext(0LL, "Unknown key name for argument --keygen: '%s'\n", 5);
fprintf(stderr, v4, v32);
goto LABEL_16;
}
macro_lookup(&v34);
}
cfg_trigger_init();
}
if ( *((_DWORD *)&gimx_params + 11) )
{
glog_set_all_levels(0LL, v4, v12);
*((_DWORD *)&gimx_params + 12) = 1;
display_init();
}
else if ( !*(&gimx_params + 21) )
{
tcgetattr(1, s);
v4 = 0LL;
s[0].c_lflag &= 0xFFFFFFF7;
tcsetattr(1, 0, s);
}
if ( (int)adapter_start() < 0 )
goto LABEL_16;
usb_poll_interrupts();
if ( (int)gprio_init() < 0 && !*((_DWORD *)&gimx_params + 12) )
{
setColorStdout_constprop_0();
printf("Warning: ");
resetColorStdout();
puts("failed to set process priority");
fflush(stdout);
}
v23 = mainloop();
if ( v23 )
{
gprio_clean();
v24 = v23;
if ( *((_DWORD *)&gimx_params + 12) )
{
v6 = v23;
adapter_clean();
goto LABEL_17;
}
goto LABEL_61;
}
gprio_clean();
v24 = *((_DWORD *)&gimx_params + 12);
if ( !v24 )
{
LABEL_61:
v4 = "Exiting\n";
v23 = v24;
v25 = dcgettext(0LL, "Exiting\n", 5);
printf(v25);
fflush(stdout);
}
LABEL_68:
v31 = adapter_clean();
v6 = v31;
if ( v23 || !v31 )
{
v6 = v23;
if ( !*(&gimx_params + 16) )
{
LABEL_19:
if ( !v6 )
goto LABEL_20;
goto LABEL_28;
}
LABEL_18:
macros_clean();
cfg_clean();
ginput_quit();
xmlCleanupParser();
goto LABEL_19;
}
LABEL_17:
if ( *(&gimx_params + 16) )
goto LABEL_18;
LABEL_28:
*(_OWORD *)&s[0].c_iflag = 0LL;
memset(&s[0].c_line, 0, 0xFF0uLL);
v36 = 0;
v18 = (char *)gfile_tempdir();
if ( strlen(v18) + 13 <= 0x1001 )
{
v4 = "%s/gimx.status";
sprintf((char *)s, "%s/gimx.status", v18);
}
free(v18);
if ( LOBYTE(s[0].c_iflag) )
{
v4 = "w";
v19 = (FILE *)gfile_fopen(s, "w");
v20 = v19;
if ( v19 )
{
v4 = "%d\n";
fprintf(v19, "%d\n", v6);
fclose(v20);
if ( (int)gfile_makeown(s) < 0 )
{
setColorStderr_constprop_0();
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v4 = (_BYTE *)(getANSIColor + 1);
fwrite("failed to set ownership of the gimx status file\n", 1uLL, 0x30uLL, stderr);
}
}
}
LABEL_20:
v15 = (FILE *)*(&gimx_params + 21);
if ( !v15 )
{
if ( !*((_DWORD *)&gimx_params + 11) )
goto LABEL_62;
LABEL_27:
display_end(v15, v4, v14);
goto LABEL_25;
}
fclose(v15);
v4 = (const char *)&off_1000;
snprintf(
(char *)s,
0x1000uLL,
"%s%s%s%s",
(const char *)gimx_params,
"/.gimx/",
"log/",
(const char *)*(&gimx_params + 20));
v15 = (FILE *)s;
v16 = gfile_makeown(s);
v14 = v33;
if ( v16 < 0 )
{
setColorStderr_constprop_0();
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v4 = (_BYTE *)(getANSIColor + 1);
v15 = (FILE *)"failed to set ownership of the gimx log file\n";
fwrite("failed to set ownership of the gimx log file\n", 1uLL, 0x2DuLL, stderr);
}
if ( *((_DWORD *)&gimx_params + 11) )
goto LABEL_27;
if ( !*(&gimx_params + 21) )
{
LABEL_62:
tcgetattr(1, s);
s[0].c_lflag |= 8u;
tcsetattr(1, 0, s);
}
LABEL_25:
free(gimx_params);
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rsi
void *v5; // rax
unsigned int v6; // er12
int v7; // ebx
__int64 v8; // rax
unsigned int v9; // er12
unsigned int v10; // ebx
__int64 v11; // rax
__int64 v12; // rdx
bool v13; // zf
__int64 v14; // rdx
FILE *v15; // rdi
int v16; // eax
char *v18; // r13
FILE *v19; // rax
FILE *v20; // r13
__int64 (__fastcall *v21)(char *); // rdx
bool v22; // cf
unsigned int v23; // ebx
unsigned int v24; // ebp
char *v25; // rax
__int64 v26; // rax
int v27; // ebx
char *v28; // rax
__int64 v29; // rax
int v30; // eax
unsigned int v31; // eax
__int64 v32; // r12
__int64 v33; // [rsp-10h] [rbp-1058h]
__int128 v34; // [rsp+0h] [rbp-1048h] BYREF
struct termios s[68]; // [rsp+10h] [rbp-1038h] BYREF
char v36; // [rsp+1010h] [rbp-38h]

v34 = 0LL;
LOBYTE(v34) = 1;
glog_set_all_levels(2LL, argv, envp);
signal(2, (__sighandler_t)terminate);
signal(15, (__sighandler_t)terminate);
signal(1, (__sighandler_t)terminate);
setlocale(6, &locale);
bindtextdomain("gimx", "/usr/share/locale");
textdomain("gimx");
v4 = "C";
setlocale(1, "C");
v5 = (void *)gfile_homedir();
gimx_params = v5;
if ( !v5 )
goto LABEL_16;
gpppcprog_read_user_ids(v5, "/.gimx/");
v4 = (const char *)argv;
if ( (int)args_read((unsigned int)argc, argv, &gimx_params) < 0 )
goto LABEL_16;
if ( *((_DWORD *)&gimx_params + 38) )
bt_abs_value = 0;
v6 = adapter_detect();
if ( v6 )
{
adapter_clean();
goto LABEL_17;
}
v7 = *((_DWORD *)&gimx_params + 7);
if ( v7 == -1 )
{
v26 = adapter_get(0LL);
v27 = controller_get_default_refresh_period(*(unsigned int *)(v26 + 736));
*((_DWORD *)&gimx_params + 7) = v27;
v13 = *((_DWORD *)&gimx_params + 12) == 0;
*((_DWORD *)&gimx_params + 34) = 33750 / v27;
if ( v13 )
{
v28 = dcgettext(0LL, "using default refresh period: %.02fms\n", 5);
printf(v28, (double)v27 / 1000.0);
fflush(stdout);
}
}
else
{
v8 = adapter_get(0LL);
if ( v7 < (int)controller_get_min_refresh_period(*(unsigned int *)(v8 + 736)) )
{
func6();
fwrite("Error: ", 1uLL, 7uLL, stderr);
func5();
v29 = adapter_get(0LL);
v30 = controller_get_min_refresh_period(*(unsigned int *)(v29 + 736));
v4 = "Refresh period should be at least %.02fms\n";
fprintf(stderr, "Refresh period should be at least %.02fms\n", (double)v30 / 1000.0);
goto LABEL_16;
}
}
v9 = 0;
v10 = 0;
*((double *)&gimx_params + 4) = 11250.0 / (double)*((int *)&gimx_params + 7);
do
{
v11 = adapter_get(v10);
v4 = (const char *)*(unsigned int *)(v11 + 748);
if ( (_DWORD)v4 )
{
v13 = *(_QWORD *)(v11 + 312) == 0LL;
v9 = 1;
*(_DWORD *)(v11 + 916) = 1;
if ( v13 && !*((_DWORD *)&gimx_params + 12) )
{
puts("The --event argument may require running two gimx instances.");
fflush(stdout);
}
}
++v10;
}
while ( v10 != 7 );
if ( v9 )
{
if ( (int)adapter_start() < 0 )
{
LABEL_16:
v6 = -1;
adapter_clean();
goto LABEL_17;
}
v23 = 0;
adapter_send();
goto LABEL_68;
}
if ( *(&gimx_params + 16) )
{
v21 = (__int64 (__fastcall *)(char *))ignore_event;
if ( !*(&gimx_params + 2) )
v21 = process_event;
v22 = *((_DWORD *)&gimx_params + 36) == 0;
*(_QWORD *)&s[0].c_iflag = &gpoll_register_fd;
*(_QWORD *)&s[0].c_cflag = &gpoll_remove_fd;
v4 = (const char *)(1 - ((unsigned int)v22 - 1));
if ( (int)ginput_init(s, v4, v21) < 0 )
goto LABEL_16;
if ( *(&gimx_params + 21) )
{
func9();
func10();
}
cal_init();
cfg_intensity_init();
cfg_init_ffb_tweaks();
if ( (int)read_config_file(*(&gimx_params + 16)) < 0 )
goto LABEL_16;
if ( (unsigned int)ginput_get_mk_mode() == 1 )
{
cfg_clean();
ginput_free_mk_names();
cal_init();
cfg_intensity_init();
cfg_init_ffb_tweaks();
read_config_file(*(&gimx_params + 16));
}
cfg_read_calibration();
cfg_pair_mouse_mappers();
if ( *((_DWORD *)&gimx_params + 47) )
{
*((_DWORD *)&gimx_params + 6) = 0;
do
{
v4 = (const char *)v9;
if ( (unsigned int)adapter_get_device(2LL, v9) != -1
|| (v4 = (const char *)v9, (unsigned int)adapter_get_device(1LL, v9) != -1) )
{
*((_DWORD *)&gimx_params + 6) = 1;
}
++v9;
}
while ( v9 != 7 );
}
func8();
ginput_release_unused();
macros_init();
if ( *(&gimx_params + 2) )
{
WORD1(v34) = ginput_key_id();
if ( !WORD1(v34) )
{
func6();
fwrite("Error: ", 1uLL, 7uLL, stderr);
func5();
v32 = (__int64)*(&gimx_params + 2);
v4 = dcgettext(0LL, "Unknown key name for argument --keygen: '%s'\n", 5);
fprintf(stderr, v4, v32);
goto LABEL_16;
}
macro_lookup(&v34);
}
cfg_trigger_init();
}
if ( *((_DWORD *)&gimx_params + 11) )
{
glog_set_all_levels(0LL, v4, v12);
*((_DWORD *)&gimx_params + 12) = 1;
display_init();
}
else if ( !*(&gimx_params + 21) )
{
tcgetattr(1, s);
v4 = 0LL;
s[0].c_lflag &= 0xFFFFFFF7;
tcsetattr(1, 0, s);
}
if ( (int)adapter_start() < 0 )
goto LABEL_16;
usb_poll_interrupts();
if ( (int)gprio_init() < 0 && !*((_DWORD *)&gimx_params + 12) )
{
func7();
printf("Warning: ");
func4();
puts("failed to set process priority");
fflush(stdout);
}
v23 = mainloop();
if ( v23 )
{
gprio_clean();
v24 = v23;
if ( *((_DWORD *)&gimx_params + 12) )
{
v6 = v23;
adapter_clean();
goto LABEL_17;
}
goto LABEL_61;
}
gprio_clean();
v24 = *((_DWORD *)&gimx_params + 12);
if ( !v24 )
{
LABEL_61:
v4 = "Exiting\n";
v23 = v24;
v25 = dcgettext(0LL, "Exiting\n", 5);
printf(v25);
fflush(stdout);
}
LABEL_68:
v31 = adapter_clean();
v6 = v31;
if ( v23 || !v31 )
{
v6 = v23;
if ( !*(&gimx_params + 16) )
{
LABEL_19:
if ( !v6 )
goto LABEL_20;
goto LABEL_28;
}
LABEL_18:
macros_clean();
cfg_clean();
ginput_quit();
xmlCleanupParser();
goto LABEL_19;
}
LABEL_17:
if ( *(&gimx_params + 16) )
goto LABEL_18;
LABEL_28:
*(_OWORD *)&s[0].c_iflag = 0LL;
memset(&s[0].c_line, 0, 0xFF0uLL);
v36 = 0;
v18 = (char *)gfile_tempdir();
if ( strlen(v18) + 13 <= 0x1001 )
{
v4 = "%s/gimx.status";
sprintf((char *)s, "%s/gimx.status", v18);
}
free(v18);
if ( LOBYTE(s[0].c_iflag) )
{
v4 = "w";
v19 = (FILE *)gfile_fopen(s, "w");
v20 = v19;
if ( v19 )
{
v4 = "%d\n";
fprintf(v19, "%d\n", v6);
fclose(v20);
if ( (int)gfile_makeown(s) < 0 )
{
func6();
fwrite("Error: ", 1uLL, 7uLL, stderr);
func5();
v4 = (_BYTE *)(getANSIColor + 1);
fwrite("failed to set ownership of the gimx status file\n", 1uLL, 0x30uLL, stderr);
}
}
}
LABEL_20:
v15 = (FILE *)*(&gimx_params + 21);
if ( !v15 )
{
if ( !*((_DWORD *)&gimx_params + 11) )
goto LABEL_62;
LABEL_27:
display_end(v15, v4, v14);
goto LABEL_25;
}
fclose(v15);
v4 = (const char *)&off_1000;
snprintf(
(char *)s,
0x1000uLL,
"%s%s%s%s",
(const char *)gimx_params,
"/.gimx/",
"log/",
(const char *)*(&gimx_params + 20));
v15 = (FILE *)s;
v16 = gfile_makeown(s);
v14 = v33;
if ( v16 < 0 )
{
func6();
fwrite("Error: ", 1uLL, 7uLL, stderr);
func5();
v4 = (_BYTE *)(getANSIColor + 1);
v15 = (FILE *)"failed to set ownership of the gimx log file\n";
fwrite("failed to set ownership of the gimx log file\n", 1uLL, 0x2DuLL, stderr);
}
if ( *((_DWORD *)&gimx_params + 11) )
goto LABEL_27;
if ( !*(&gimx_params + 21) )
{
LABEL_62:
tcgetattr(1, s);
s[0].c_lflag |= 8u;
tcsetattr(1, 0, s);
}
LABEL_25:
free(gimx_params);
return v6;
}

original_code:
int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) {
    unsigned int i;
    for (i = 0; i < sizeof(test_cases) / sizeof(*test_cases); ++i) {
        printf("test case: %s: ", test_cases[i].name);
        s_haptic_core_data data;
        if (test_cases[i].in.cmd != 0xF8) {
            ff_lg_convert_force(ff_lg_get_caps(test_cases[i].src_pid), 0, &test_cases[i].in.force, 1, &data);
        } else {
            ff_lg_convert_extended(&test_cases[i].in.force, &data);
        }
        haptic_tweak_apply(&test_cases[i].tweaks, &data);
        s_ff_lg_report report;
        ff_lg_convert_slot(&data, test_cases[i].dst_slot, &report, ff_lg_get_caps(test_cases[i].dst_pid));
        if (compare_forces(test_cases[i].out.data, report.data + 1)) {
            printf("failed\n");
            continue;
        }
        printf("success\n");
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    unsigned int i;
    for (i = 0; i < sizeof(test_cases) / sizeof(*test_cases); ++i) {
        printf("test case: %s: ", test_cases[i].name);
        struct s_haptic_core_data data;
        if (test_cases[i].in.cmd != 0xF8) {
            ff_lg_convert_force(ff_lg_get_caps(test_cases[i].src_pid), 0, &test_cases[i].in.force, 1, &data);
        } else {
            ff_lg_convert_extended(&test_cases[i].in.force, &data);
        }
        haptic_tweak_apply(&test_cases[i].tweaks, &data);
        struct s_ff_lg_report report;
        ff_lg_convert_slot(&data, test_cases[i].dst_slot, &report, ff_lg_get_caps(test_cases[i].dst_pid));
        if (compare_forces(test_cases[i].out.data, report.data + 1)) {
            printf("failed\n");
            continue;
        }
        printf("success\n");
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
i
unsigned int

variable_types: struct s_haptic_core_data { enum e_haptic_core_data_type { E_DATA_TYPE_NONE=0, E_DATA_TYPE_RUMBLE=1, E_DATA_TYPE_CONSTANT=2, E_DATA_TYPE_SPRING=3, E_DATA_TYPE_DAMPER=4, E_DATA_TYPE_LEDS=5, E_DATA_TYPE_RANGE=6 } type; unsigned char playing; union { struct s_haptic_core_rumble { unsigned short weak; unsigned short strong; } rumble; struct s_haptic_core_constant { short level; } constant; struct s_haptic_core_condition { struct { unsigned short left; unsigned short right; } saturation; struct { short left; short right; } coefficient; short center; unsigned short deadband; } spring; struct s_haptic_core_condition damper; struct s_haptic_core_leds { unsigned char value; } leds; struct s_haptic_core_range { unsigned short value; } range; }; }

variable_types: struct s_ff_lg_report { unsigned char data[-1]; }

user_defined_types: struct s_haptic_core_data { enum e_haptic_core_data_type type; unsigned char playing; union { struct s_haptic_core_rumble rumble; struct s_haptic_core_constant constant; struct s_haptic_core_condition spring; struct s_haptic_core_condition damper; struct s_haptic_core_leds leds; struct s_haptic_core_range range; }; }


decompiled_code:
__int64 btstack_common_getfd()
{
return (unsigned int)btstack_fd;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)btstack_fd;
}

original_code:
int btstack_common_getfd()
{
  return btstack_fd;
}


decompiled_code:
__int64 __fastcall btstack_common_recv(_WORD *a1)
{
__int64 v2; // rdi
__int64 v3; // rsi
int v4; // eax
bool v5; // zf
__int16 v6; // dx
__int16 v7; // dx
__int64 result; // rax

v2 = (unsigned int)btstack_fd;
if ( btstack_fd < 0 )
{
fwrite("No connection to btstack.\n", 1uLL, 0x1AuLL, stderr);
return 0xFFFFFFFFLL;
}
v3 = (unsigned __int16)a1[516];
if ( (unsigned __int16)v3 <= 5u )
{
a1[517] = 6 - v3;
v4 = tcp_recv(v2, (char *)a1 + v3, (unsigned __int16)(6 - v3));
v5 = v4 == 0;
if ( v4 > 0 )
{
LABEL_4:
v6 = a1[516];
a1[517] -= v4;
v7 = v4 + v6;
result = 0LL;
a1[516] = v7;
if ( v7 != 6 )
goto LABEL_5;
LABEL_10:
a1[517] = a1[2];
return result;
}
}
else
{
v4 = tcp_recv((unsigned int)btstack_fd, (char *)a1 + v3, (unsigned __int16)a1[517]);
v5 = v4 == 0;
if ( v4 > 0 )
goto LABEL_4;
}
result = (unsigned int)-!v5;
if ( a1[516] == 6 )
goto LABEL_10;
LABEL_5:
if ( !a1[517] )
{
a1[516] = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_WORD *a1)
{
__int64 v2; // rdi
__int64 v3; // rsi
int v4; // eax
bool v5; // zf
__int16 v6; // dx
__int16 v7; // dx
__int64 result; // rax

v2 = (unsigned int)btstack_fd;
if ( btstack_fd < 0 )
{
fwrite("No connection to btstack.\n", 1uLL, 0x1AuLL, stderr);
return 0xFFFFFFFFLL;
}
v3 = (unsigned __int16)a1[516];
if ( (unsigned __int16)v3 <= 5u )
{
a1[517] = 6 - v3;
v4 = tcp_recv(v2, (char *)a1 + v3, (unsigned __int16)(6 - v3));
v5 = v4 == 0;
if ( v4 > 0 )
{
LABEL_4:
v6 = a1[516];
a1[517] -= v4;
v7 = v4 + v6;
result = 0LL;
a1[516] = v7;
if ( v7 != 6 )
goto LABEL_5;
LABEL_10:
a1[517] = a1[2];
return result;
}
}
else
{
v4 = tcp_recv((unsigned int)btstack_fd, (char *)a1 + v3, (unsigned __int16)a1[517]);
v5 = v4 == 0;
if ( v4 > 0 )
goto LABEL_4;
}
result = (unsigned int)-!v5;
if ( a1[516] == 6 )
goto LABEL_10;
LABEL_5:
if ( !a1[517] )
{
a1[516] = 0;
result = 1LL;
}
return result;
}

original_code:
int btstack_common_recv(recv_data_t* recv_data)
{
  int status = 0;
  if(btstack_fd < 0)
  {
    fprintf(stderr, "No connection to btstack.\n");
    return -1;
  }
  int ret;
  if(recv_data->read < sizeof(packet_header_t))
  {
    recv_data->remaining = sizeof(packet_header_t) - recv_data->read;
  }
  ret = tcp_recv(btstack_fd, recv_data->buffer+recv_data->read, recv_data->remaining);
  if(ret > 0)
  {
    recv_data->read += ret;
    recv_data->remaining -= ret;
  }
  else if( ret < 0)
  {
    status = -1;
  }
  if(recv_data->read == sizeof(packet_header_t))
  {
    recv_data->remaining = ( ((uint16_t) recv_data->buffer[4]) | (((uint16_t)recv_data->buffer[4 +1]) << 8));
  }
  else if(!recv_data->remaining)
  {
    status = 1;
    recv_data->read = 0;
  }
  return status;
}

canonical_original_code:
int btstack_common_recv(struct recv_data_t *recv_data)
{
  int status = 0;
  if(btstack_fd < 0)
  {
    fprintf(stderr, "No connection to btstack.\n");
    return -1;
  }
  int ret;
  if(recv_data->read < sizeof(packet_header_t))
  {
    recv_data->remaining = sizeof(packet_header_t) - recv_data->read;
  }
  ret = tcp_recv(btstack_fd, recv_data->buffer+recv_data->read, recv_data->remaining);
  if(ret > 0)
  {
    recv_data->read += ret;
    recv_data->remaining -= ret;
  }
  else if( ret < 0)
  {
    status = -1;
  }
  if(recv_data->read == sizeof(packet_header_t))
  {
    recv_data->remaining = ( ((unsigned short) recv_data->buffer[4]) | (((unsigned short)recv_data->buffer[4 +1]) << 8));
  }
  else if(!recv_data->remaining)
  {
    status = 1;
    recv_data->read = 0;
  }
  return status;
}

variable_types: struct recv_data_t { unsigned char buffer[-1]; unsigned short read; unsigned short remaining; } *

variable_types: status int


decompiled_code:
__int64 __fastcall btstack_common_recv_packet(__int64 a1)
{
int v2; // edi
__m128i v3; // xmm1
__m128i v4; // xmm2
__m128i v5; // xmm3
__m128i v6; // xmm4
__m128i v7; // xmm5
__m128i v8; // xmm6
__m128i v9; // xmm7
int v10; // eax
__int64 v11; // rax
__int64 v12; // r13
__int64 result; // rax
struct timeval v14; // [rsp+0h] [rbp-138h] BYREF
fd_set readfds; // [rsp+10h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+90h] [rbp-A8h] BYREF

*(_DWORD *)(a1 + 1032) = 0;
while ( 1 )
{
v14.tv_usec = 0LL;
v14.tv_sec = 1LL;
memset(&readfds, 0, sizeof(readfds));
v2 = btstack_fd;
readfds.fds_bits[btstack_fd / 64] |= 1LL << (btstack_fd % 64);
v3 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
v4 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
v5 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
v6 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)exceptfds.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
v7 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
v8 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
*(__m128i *)&exceptfds.fds_bits[2] = v3;
v9 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
*(__m128i *)&exceptfds.fds_bits[4] = v4;
*(__m128i *)&exceptfds.fds_bits[6] = v5;
*(__m128i *)&exceptfds.fds_bits[8] = v6;
*(__m128i *)&exceptfds.fds_bits[10] = v7;
*(__m128i *)&exceptfds.fds_bits[12] = v8;
*(__m128i *)&exceptfds.fds_bits[14] = v9;
v10 = select(v2 + 1, &readfds, 0LL, &exceptfds, &v14);
if ( v10 < 0 )
{
perror("select");
return 0xFFFFFFFFLL;
}
if ( !v10 )
return 0xFFFFFFFFLL;
v11 = btstack_fd / 64;
v12 = 1LL << (btstack_fd % 64);
if ( (exceptfds.fds_bits[v11] & v12) != 0 )
break;
if ( (readfds.fds_bits[v11] & v12) != 0 )
{
result = btstack_common_recv((_WORD *)a1);
if ( (_DWORD)result )
return result;
}
}
fwrite("connection with btstack is broken\n", 1uLL, 0x22uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v2; // edi
__m128i v3; // xmm1
__m128i v4; // xmm2
__m128i v5; // xmm3
__m128i v6; // xmm4
__m128i v7; // xmm5
__m128i v8; // xmm6
__m128i v9; // xmm7
int v10; // eax
__int64 v11; // rax
__int64 v12; // r13
__int64 result; // rax
struct timeval v14; // [rsp+0h] [rbp-138h] BYREF
fd_set readfds; // [rsp+10h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+90h] [rbp-A8h] BYREF

*(_DWORD *)(a1 + 1032) = 0;
while ( 1 )
{
v14.tv_usec = 0LL;
v14.tv_sec = 1LL;
memset(&readfds, 0, sizeof(readfds));
v2 = btstack_fd;
readfds.fds_bits[btstack_fd / 64] |= 1LL << (btstack_fd % 64);
v3 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
v4 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
v5 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
v6 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)exceptfds.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
v7 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
v8 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
*(__m128i *)&exceptfds.fds_bits[2] = v3;
v9 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
*(__m128i *)&exceptfds.fds_bits[4] = v4;
*(__m128i *)&exceptfds.fds_bits[6] = v5;
*(__m128i *)&exceptfds.fds_bits[8] = v6;
*(__m128i *)&exceptfds.fds_bits[10] = v7;
*(__m128i *)&exceptfds.fds_bits[12] = v8;
*(__m128i *)&exceptfds.fds_bits[14] = v9;
v10 = select(v2 + 1, &readfds, 0LL, &exceptfds, &v14);
if ( v10 < 0 )
{
perror("select");
return 0xFFFFFFFFLL;
}
if ( !v10 )
return 0xFFFFFFFFLL;
v11 = btstack_fd / 64;
v12 = 1LL << (btstack_fd % 64);
if ( (exceptfds.fds_bits[v11] & v12) != 0 )
break;
if ( (readfds.fds_bits[v11] & v12) != 0 )
{
result = func1((_WORD *)a1);
if ( (_DWORD)result )
return result;
}
}
fwrite("connection with btstack is broken\n", 1uLL, 0x22uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int btstack_common_recv_packet(recv_data_t* data)
{
  fd_set fdread, fderr;
  data->read = 0;
  data->remaining = 0;
  while (1)
  {
    struct timeval tv = { .tv_sec = 1 };
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdread)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fdread)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))));
    fderr = fdread;
    int ret = select(btstack_fd+1, &fdread, ((void *)0), &fderr, &tv);
    if(ret < 0)
    {
      perror("select");
      break;
    }
    else if(ret > 0)
    {
      if(((((&fderr)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
      {
        fprintf(stderr, "connection with btstack is broken\n");
        break;
      }
      else if(((((&fdread)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
      {
        int status = btstack_common_recv(data);
        if(status)
        {
          return status;
        }
      }
    }
    else
    {
      break;
    }
  }
  return -1;
}

canonical_original_code:
int btstack_common_recv_packet(struct recv_data_t *data)
{
  struct fd_set fdread; struct fd_set fderr;
  data->read = 0;
  data->remaining = 0;
  while (1)
  {
    struct timeval tv = { .tv_sec = 1 };
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdread)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fdread)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))));
    fderr = fdread;
    int ret = select(btstack_fd+1, &fdread, ((void *)0), &fderr, &tv);
    if(ret < 0)
    {
      perror("select");
      break;
    }
    else if(ret > 0)
    {
      if(((((&fderr)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
      {
        fprintf(stderr, "connection with btstack is broken\n");
        break;
      }
      else if(((((&fdread)->fds_bits)[((btstack_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((btstack_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
      {
        int status = btstack_common_recv(data);
        if(status)
        {
          return status;
        }
      }
    }
    else
    {
      break;
    }
  }
  return -1;
}

variable_types:
fdread
struct fd_set { long fds_bits[-1]; }

variable_types:
fderr
struct fd_set { long fds_bits[-1]; }

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

user_defined_types: struct recv_data_t { unsigned char buffer[-1]; unsigned short read; unsigned short remaining; }


decompiled_code:
__int64 __fastcall btstack_common_send_packet(unsigned __int16 a1, unsigned __int16 a2, __int64 a3, unsigned int a4)
{
char v7[30]; // [rsp+Ah] [rbp-1Eh] BYREF

bt_store_16(v7, 0LL, a1);
bt_store_16(v7, 2LL, a2);
bt_store_16(v7, 4LL, (unsigned __int16)a4);
if ( (int)tcp_send((unsigned int)btstack_fd, v7, 6LL) < 0 || (int)tcp_send((unsigned int)btstack_fd, a3, a4) < 0 )
a4 = -1;
return a4;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, unsigned __int16 a2, __int64 a3, unsigned int a4)
{
char v7[30]; // [rsp+Ah] [rbp-1Eh] BYREF

bt_store_16(v7, 0LL, a1);
bt_store_16(v7, 2LL, a2);
bt_store_16(v7, 4LL, (unsigned __int16)a4);
if ( (int)tcp_send((unsigned int)btstack_fd, v7, 6LL) < 0 || (int)tcp_send((unsigned int)btstack_fd, a3, a4) < 0 )
a4 = -1;
return a4;
}

original_code:
int btstack_common_send_packet(uint16_t type, uint16_t cid, const unsigned char * buf, uint32_t len)
{
  uint8_t header[sizeof(packet_header_t)];
  bt_store_16(header, 0, type);
  bt_store_16(header, 2, cid);
  bt_store_16(header, 4, len);
  if(tcp_send(btstack_fd, (const void*)header, 6) < 0)
  {
    return -1;
  }
  if(tcp_send(btstack_fd, (const void*)buf, len) < 0)
  {
    return -1;
  }
  return len;
}

canonical_original_code:
int btstack_common_send_packet(unsigned short type, unsigned short cid, unsigned char *buf, unsigned int len)
{
  unsigned char header[-1];
  bt_store_16(header, 0, type);
  bt_store_16(header, 2, cid);
  bt_store_16(header, 4, len);
  if(tcp_send(btstack_fd, (void *)header, 6) < 0)
  {
    return -1;
  }
  if(tcp_send(btstack_fd, (void *)buf, len) < 0)
  {
    return -1;
  }
  return len;
}

variable_types:
type
unsigned short

variable_types:
cid
unsigned short

variable_types:
buf
unsigned char *

variable_types:
len
unsigned int

variable_types:
header
unsigned char [-1]


decompiled_code:
__int64 btstack_common_send_cmd(__int64 a1, ...)
{
unsigned int v1; // er12
__int64 result; // rax
char v3[6]; // [rsp+2h] [rbp-D6h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( btstack_fd < 0 )
{
fwrite("No connection to btstack.\n", 1uLL, 0x1AuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
va_start(va, a1);
v1 = (unsigned __int16)hci_create_cmd_internal(&hci_cmd_buffer, a1, va);
bt_store_16(v3, 0LL, 1LL);
bt_store_16(v3, 2LL, 0LL);
bt_store_16(v3, 4LL, v1);
if ( (int)tcp_send((unsigned int)btstack_fd, v3, 6LL) < 0
|| (int)tcp_send((unsigned int)btstack_fd, &hci_cmd_buffer, v1) < 0 )
{
fwrite("Failed to send hci command.\n", 1uLL, 0x1CuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4(__int64 a1, ...)
{
unsigned int v1; // er12
__int64 result; // rax
char v3[6]; // [rsp+2h] [rbp-D6h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( btstack_fd < 0 )
{
fwrite("No connection to btstack.\n", 1uLL, 0x1AuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
va_start(va, a1);
v1 = (unsigned __int16)hci_create_cmd_internal(&hci_cmd_buffer, a1, va);
bt_store_16(v3, 0LL, 1LL);
bt_store_16(v3, 2LL, 0LL);
bt_store_16(v3, 4LL, v1);
if ( (int)tcp_send((unsigned int)btstack_fd, v3, 6LL) < 0
|| (int)tcp_send((unsigned int)btstack_fd, &hci_cmd_buffer, v1) < 0 )
{
fwrite("Failed to send hci command.\n", 1uLL, 0x1CuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int btstack_common_send_cmd(const hci_cmd_t *cmd, ...)
{
  if(btstack_fd < 0)
  {
    fprintf(stderr, "No connection to btstack.\n");
    return -1;
  }
  va_list argptr;
  __builtin_va_start(argptr,cmd);
  uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
  __builtin_va_end(argptr);
  uint8_t header[sizeof(packet_header_t)];
  bt_store_16(header, 0, 0x01);
  bt_store_16(header, 2, 0);
  bt_store_16(header, 4, len);
  if(tcp_send(btstack_fd, header, 6) < 0)
  {
    fprintf(stderr, "Failed to send hci command.\n");
    return -1;
  }
  if(tcp_send(btstack_fd, hci_cmd_buffer, len) < 0)
  {
    fprintf(stderr, "Failed to send hci command.\n");
    return -1;
  }
  return 0;
}

canonical_original_code:
int btstack_common_send_cmd(struct hci_cmd_t *cmd, ...)
{
  if(btstack_fd < 0)
  {
    fprintf(stderr, "No connection to btstack.\n");
    return -1;
  }
  __builtin_va_list argptr;
  __builtin_va_start(argptr,cmd);
  unsigned short len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
  __builtin_va_end(argptr);
  unsigned char header[-1];
  bt_store_16(header, 0, 0x01);
  bt_store_16(header, 2, 0);
  bt_store_16(header, 4, len);
  if(tcp_send(btstack_fd, header, 6) < 0)
  {
    fprintf(stderr, "Failed to send hci command.\n");
    return -1;
  }
  if(tcp_send(btstack_fd, hci_cmd_buffer, len) < 0)
  {
    fprintf(stderr, "Failed to send hci command.\n");
    return -1;
  }
  return 0;
}

variable_types: struct hci_cmd_t { unsigned short opcode; char *format; } *

variable_types:
len
unsigned short


decompiled_code:
__int64 btstack_common_init()
{
in_addr_t v0; // eax
int v1; // er12
int v2; // eax
__int64 result; // rax

v0 = inet_addr("127.0.0.1");
btstack_fd = tcp_connect(v0, 13333LL);
if ( btstack_fd < 0 )
return (unsigned int)-1;
v1 = btstack_common_send_cmd((__int64)&btstack_set_power_mode, 1LL);
if ( v1 >= 0 )
{
do
{
while ( 1 )
{
v2 = btstack_common_recv_packet((__int64)&recv_data);
v1 = v2;
if ( v2 > 0 )
break;
if ( v2 )
goto LABEL_9;
}
}
while ( recv_data != 4 || byte_A06 != 96 || byte_A08 != 2 );
return (unsigned int)v1;
}
LABEL_9:
tcp_close((unsigned int)btstack_fd);
result = (unsigned int)v1;
btstack_fd = -1;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
in_addr_t v0; // eax
int v1; // er12
int v2; // eax
__int64 result; // rax

v0 = inet_addr("127.0.0.1");
btstack_fd = tcp_connect(v0, 13333LL);
if ( btstack_fd < 0 )
return (unsigned int)-1;
v1 = func4((__int64)&btstack_set_power_mode, 1LL);
if ( v1 >= 0 )
{
do
{
while ( 1 )
{
v2 = func2((__int64)&recv_data);
v1 = v2;
if ( v2 > 0 )
break;
if ( v2 )
goto LABEL_9;
}
}
while ( recv_data != 4 || byte_A06 != 96 || byte_A08 != 2 );
return (unsigned int)v1;
}
LABEL_9:
tcp_close((unsigned int)btstack_fd);
result = (unsigned int)v1;
btstack_fd = -1;
return result;
}

original_code:
int btstack_common_init()
{
  int ret = 0;
  btstack_fd = tcp_connect(inet_addr("127.0.0.1"), 13333);
  if(btstack_fd < 0)
  {
    return -1;
  }
  ret = btstack_common_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
  while(ret >= 0)
  {
    ret = btstack_common_recv_packet(&recv_data);
    if(ret > 0)
    {
      int type = ( ((uint16_t) recv_data.buffer[0]) | (((uint16_t)recv_data.buffer[0 +1]) << 8));
      unsigned char* packet = recv_data.buffer+sizeof(packet_header_t);
      if(type == 0x04)
      {
        if(packet[0] == 0x60)
        {
          if (packet[2] == HCI_STATE_WORKING)
          {
            break;
          }
        }
      }
    }
  }
  if(ret < 0)
  {
    tcp_close(btstack_fd);
    btstack_fd = -1;
  }
  return ret;
}

canonical_original_code:
int btstack_common_init()
{
  int ret = 0;
  btstack_fd = tcp_connect(inet_addr("127.0.0.1"), 13333);
  if(btstack_fd < 0)
  {
    return -1;
  }
  ret = btstack_common_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
  while(ret >= 0)
  {
    ret = btstack_common_recv_packet(&recv_data);
    if(ret > 0)
    {
      int type = ( ((uint16_t) recv_data.buffer[0]) | (((uint16_t)recv_data.buffer[0 +1]) << 8));
      unsigned char *packet = recv_data.buffer+sizeof(packet_header_t);
      if(type == 0x04)
      {
        if(packet[0] == 0x60)
        {
          if (packet[2] == HCI_STATE_WORKING)
          {
            break;
          }
        }
      }
    }
  }
  if(ret < 0)
  {
    tcp_close(btstack_fd);
    btstack_fd = -1;
  }
  return ret;
}

variable_types: ret int

variable_types: type int

variable_types:
packet
unsigned char *


decompiled_code:
__int64 __fastcall btstack_common_disconnect(unsigned __int16 a1)
{
int v1; // eax
__int16 v2; // bx
int v3; // eax

v1 = btstack_common_send_cmd((__int64)&btstack_hci_disconnect, a1, 19LL);
v2 = btstack_hci_disconnect;
if ( v1 >= 0 )
{
do
{
while ( 1 )
{
v3 = btstack_common_recv_packet((__int64)&recv_data);
if ( v3 > 0 )
break;
if ( v3 )
return tcp_close((unsigned int)btstack_fd);
}
}
while ( recv_data != 4 || byte_A06 != 14 || v2 != word_A09 );
}
return tcp_close((unsigned int)btstack_fd);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int16 a1)
{
int v1; // eax
__int16 v2; // bx
int v3; // eax

v1 = func4((__int64)&btstack_hci_disconnect, a1, 19LL);
v2 = btstack_hci_disconnect;
if ( v1 >= 0 )
{
do
{
while ( 1 )
{
v3 = func2((__int64)&recv_data);
if ( v3 > 0 )
break;
if ( v3 )
return tcp_close((unsigned int)btstack_fd);
}
}
while ( recv_data != 4 || byte_A06 != 14 || v2 != word_A09 );
}
return tcp_close((unsigned int)btstack_fd);
}

original_code:
int btstack_common_disconnect(uint16_t handle)
{
  int ret = btstack_common_send_cmd(&btstack_hci_disconnect, handle, 0x13);
  while(ret >= 0)
  {
    ret = btstack_common_recv_packet(&recv_data);
    if(ret > 0)
    {
      int type = ( ((uint16_t) recv_data.buffer[0]) | (((uint16_t)recv_data.buffer[0 +1]) << 8));
      unsigned char* packet = recv_data.buffer+sizeof(packet_header_t);
      if(type == 0x04)
      {
        if ( ( packet[0] == 0x0E && ( ((uint16_t) packet[3]) | (((uint16_t)packet[3 +1]) << 8)) == btstack_hci_disconnect.opcode) ) {
          break;
        }
      }
    }
  }
  ret = tcp_close(btstack_fd);
  return ret;
}

canonical_original_code:
int btstack_common_disconnect(unsigned short handle)
{
  int ret = btstack_common_send_cmd(&btstack_hci_disconnect, handle, 0x13);
  while(ret >= 0)
  {
    ret = btstack_common_recv_packet(&recv_data);
    if(ret > 0)
    {
      int type = ( ((uint16_t) recv_data.buffer[0]) | (((uint16_t)recv_data.buffer[0 +1]) << 8));
      unsigned char *packet = recv_data.buffer+sizeof(packet_header_t);
      if(type == 0x04)
      {
        if ( ( packet[0] == 0x0E && ( ((unsigned short) packet[3]) | (((unsigned short)packet[3 +1]) << 8)) == btstack_hci_disconnect.opcode) ) {
          break;
        }
      }
    }
  }
  ret = tcp_close(btstack_fd);
  return ret;
}

variable_types:
handle
unsigned short


bacpy

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

*(_DWORD *)a1 = *(_DWORD *)a2;
result = *(unsigned __int16 *)(a2 + 4);
*(_WORD *)(a1 + 4) = result;
return result;
}

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

*(_DWORD *)a1 = *(_DWORD *)a2;
result = *(unsigned __int16 *)(a2 + 4);
*(_WORD *)(a1 + 4) = result;
return result;
}

original_code:
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
{
 memcpy(dst, src, sizeof(bdaddr_t));
}

canonical_original_code:
static inline void bacpy(struct bdaddr_t *dst, struct bdaddr_t *src)
{
 memcpy(dst, src, sizeof(bdaddr_t));
}

variable_types: struct bdaddr_t { unsigned char b[6]; } *

variable_types:
src
struct bdaddr_t *


original_code:
static int zeevo_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 struct hci_request rq;
 zeevo_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0001;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int zeevo_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 struct hci_request rq;
 struct zeevo_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0001;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

variable_types: dd int

variable_types: struct hci_request { unsigned short ogf; unsigned short ocf; int event; void *cparam; int clen; void *rparam; int rlen; }

variable_types: struct zeevo_write_bd_addr_cp { struct bdaddr_t { unsigned char b[6]; } bdaddr; }

user_defined_types: struct zeevo_write_bd_addr_cp { struct bdaddr_t bdaddr; }


original_code:
static int bcm_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 struct hci_request rq;
 bcm_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0001;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int bcm_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 struct hci_request rq;
 struct bcm_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0001;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

variable_types: struct bcm_write_bd_addr_cp { struct bdaddr_t { unsigned char b[6]; } bdaddr; }

user_defined_types: struct hci_request { unsigned short ogf; unsigned short ocf; int event; void *cparam; int clen; void *rparam; int rlen; }

user_defined_types: struct bcm_write_bd_addr_cp { struct bdaddr_t bdaddr; }


original_code:
static int ti_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 struct hci_request rq;
 ti_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0006;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int ti_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 struct hci_request rq;
 struct ti_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x0006;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

variable_types: struct ti_write_bd_addr_cp { struct bdaddr_t { unsigned char b[6]; } bdaddr; }

user_defined_types: struct ti_write_bd_addr_cp { struct bdaddr_t bdaddr; }


original_code:
static int csr_reset_device(int dd)
{
 unsigned char cmd[] = { 0x02, 0x00, 0x09, 0x00,
    0x00, 0x00, 0x01, 0x40, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 unsigned char cp[254], rp[254];
 struct hci_request rq;
 if (transient)
  cmd[6] = 0x02;
 memset(&cp, 0, sizeof(cp));
 cp[0] = 0xc2;
 memcpy(cp + 1, cmd, sizeof(cmd));
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x00;
 rq.event = 0xFF;
 rq.cparam = cp;
 rq.clen = sizeof(cmd) + 1;
 rq.rparam = rp;
 rq.rlen = sizeof(rp);
 if (hci_send_req(dd, &rq, 2000) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int csr_reset_device(int dd)
{
 unsigned char cmd[] = { 0x02, 0x00, 0x09, 0x00,
    0x00, 0x00, 0x01, 0x40, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 unsigned char cp[254]; unsigned char rp[254];
 struct hci_request rq;
 if (transient)
  cmd[6] = 0x02;
 memset(&cp, 0, sizeof(cp));
 cp[0] = 0xc2;
 memcpy(cp + 1, cmd, sizeof(cmd));
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x00;
 rq.event = 0xFF;
 rq.cparam = cp;
 rq.clen = sizeof(cmd) + 1;
 rq.rparam = rp;
 rq.rlen = sizeof(rp);
 if (hci_send_req(dd, &rq, 2000) < 0)
  return -1;
 return 0;
}

variable_types:
cmd
unsigned char []

variable_types:
cp
unsigned char [254]

variable_types:
rp
unsigned char [254]


original_code:
static int csr_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 unsigned char cmd[] = { 0x02, 0x00, 0x0c, 0x00, 0x11, 0x47, 0x03, 0x70,
    0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 unsigned char cp[254], rp[254];
 struct hci_request rq;
 if (transient)
  cmd[14] = 0x08;
 cmd[16] = bdaddr->b[2];
 cmd[17] = 0x00;
 cmd[18] = bdaddr->b[0];
 cmd[19] = bdaddr->b[1];
 cmd[20] = bdaddr->b[3];
 cmd[21] = 0x00;
 cmd[22] = bdaddr->b[4];
 cmd[23] = bdaddr->b[5];
 memset(&cp, 0, sizeof(cp));
 cp[0] = 0xc2;
 memcpy(cp + 1, cmd, sizeof(cmd));
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x00;
 rq.event = 0xFF;
 rq.cparam = cp;
 rq.clen = sizeof(cmd) + 1;
 rq.rparam = rp;
 rq.rlen = sizeof(rp);
 if (hci_send_req(dd, &rq, 2000) < 0)
  return -1;
 if (rp[0] != 0xc2) {
  (*__errno_location ()) = 5;
  return -1;
 }
 if ((rp[9] + (rp[10] << 8)) != 0) {
  (*__errno_location ()) = 6;
  return -1;
 }
 return 0;
}

canonical_original_code:
static int csr_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 unsigned char cmd[] = { 0x02, 0x00, 0x0c, 0x00, 0x11, 0x47, 0x03, 0x70,
    0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 unsigned char cp[254]; unsigned char rp[254];
 struct hci_request rq;
 if (transient)
  cmd[14] = 0x08;
 cmd[16] = bdaddr->b[2];
 cmd[17] = 0x00;
 cmd[18] = bdaddr->b[0];
 cmd[19] = bdaddr->b[1];
 cmd[20] = bdaddr->b[3];
 cmd[21] = 0x00;
 cmd[22] = bdaddr->b[4];
 cmd[23] = bdaddr->b[5];
 memset(&cp, 0, sizeof(cp));
 cp[0] = 0xc2;
 memcpy(cp + 1, cmd, sizeof(cmd));
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x00;
 rq.event = 0xFF;
 rq.cparam = cp;
 rq.clen = sizeof(cmd) + 1;
 rq.rparam = rp;
 rq.rlen = sizeof(rp);
 if (hci_send_req(dd, &rq, 2000) < 0)
  return -1;
 if (rp[0] != 0xc2) {
  (*__errno_location ()) = 5;
  return -1;
 }
 if ((rp[9] + (rp[10] << 8)) != 0) {
  (*__errno_location ()) = 6;
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall ericsson_write_bd_addr(__int64 a1, __int64 a2)
{
unsigned int v2; // er8
__int64 v3; // rdx
int v5; // [rsp+Ah] [rbp-3Eh] BYREF
__int16 v6; // [rsp+Eh] [rbp-3Ah]
__int128 v7; // [rsp+10h] [rbp-38h] BYREF
__int128 v8; // [rsp+20h] [rbp-28h]
__int64 v9; // [rsp+30h] [rbp-18h]

v6 = 0;
v5 = 0;
bacpy((__int64)&v5, a2);
*((_QWORD *)&v7 + 1) = v3;
v8 = 0LL;
v9 = 0LL;
*(_QWORD *)&v7 = 852031LL;
LODWORD(v8) = 6;
return (unsigned int)((int)hci_send_req(v2, &v7, 1000LL) >> 31);
}

original_code:
static int ericsson_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 struct hci_request rq;
 ericsson_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x000d;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int ericsson_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 struct hci_request rq;
 struct ericsson_write_bd_addr_cp cp;
 memset(&cp, 0, sizeof(cp));
 bacpy(&cp.bdaddr, bdaddr);
 memset(&rq, 0, sizeof(rq));
 rq.ogf = 0x3f;
 rq.ocf = 0x000d;
 rq.cparam = &cp;
 rq.clen = 6;
 rq.rparam = ((void *)0);
 rq.rlen = 0;
 if (hci_send_req(dd, &rq, 1000) < 0)
  return -1;
 return 0;
}

variable_types: struct ericsson_write_bd_addr_cp { struct bdaddr_t { unsigned char b[6]; } bdaddr; }

user_defined_types: struct ericsson_write_bd_addr_cp { struct bdaddr_t bdaddr; }


decompiled_code:
__int64 __fastcall generic_reset_device(__int64 a1)
{
__int64 result; // rax
char v2[14]; // [rsp+Ah] [rbp-Eh] BYREF

result = hci_send_cmd(a1, 3LL, 3LL, 0LL, 0LL);
if ( (int)result >= 0 )
result = hci_read_bd_addr((unsigned int)a1, v2, 10000LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
char v2[14]; // [rsp+Ah] [rbp-Eh] BYREF

result = hci_send_cmd(a1, 3LL, 3LL, 0LL, 0LL);
if ( (int)result >= 0 )
result = hci_read_bd_addr((unsigned int)a1, v2, 10000LL);
return result;
}

original_code:
static int generic_reset_device(int dd)
{
 bdaddr_t bdaddr;
 int err;
 err = hci_send_cmd(dd, 0x03, 0x0003, 0, ((void *)0));
 if (err < 0)
  return err;
 return hci_read_bd_addr(dd, &bdaddr, 10000);
}

canonical_original_code:
static int generic_reset_device(int dd)
{
 struct bdaddr_t bdaddr;
 int err;
 err = hci_send_cmd(dd, 0x03, 0x0003, 0, ((void *)0));
 if (err < 0)
  return err;
 return hci_read_bd_addr(dd, &bdaddr, 10000);
}

variable_types:
bdaddr
struct bdaddr_t { unsigned char b[6]; }

variable_types: err int


decompiled_code:
// attributes: thunk
__int64 __fastcall st_write_bd_addr(unsigned int a1, __int64 a2)
{
return ericsson_store_in_flash_constprop_0(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, __int64 a2)
{
return func9(a1, a2);
}

original_code:
static int st_write_bd_addr(int dd, bdaddr_t *bdaddr)
{
 return ericsson_store_in_flash(dd, 0xfe, 6, (uint8_t *) bdaddr);
}

canonical_original_code:
static int st_write_bd_addr(int dd, struct bdaddr_t *bdaddr)
{
 return ericsson_store_in_flash(dd, 0xfe, 6, (unsigned char *) bdaddr);
}


usage

decompiled_code:
int usage()
{
puts("bdaddr - Utility for changing the Bluetooth device address\n");
return puts("Usage:\n\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]");
}

canonical_decompiled_code:
int func0()
{
puts("bdaddr - Utility for changing the Bluetooth device address\n");
return puts("Usage:\n\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]");
}

original_code:
static void usage(void)
{
 printf("bdaddr - Utility for changing the Bluetooth device address\n\n");
 printf("Usage:\n"
  "\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]\n");
}

canonical_original_code:
static void usage()
{
 printf("bdaddr - Utility for changing the Bluetooth device address\n\n");
 printf("Usage:\n"
  "\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]\n");
}


decompiled_code:
__int64 __fastcall tcp_connect(int a1, unsigned __int16 a2)
{
unsigned int v2; // er12
__int16 v3; // ax
socklen_t v4; // edx
__int64 result; // rax
char *v6; // rbp
char *v7; // rax
int v8; // edi
struct sockaddr v9; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 6);
if ( v2 == -1 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)&v9.sa_data[6] = 0LL;
v9.sa_family = 2;
v3 = _bswap_16(a2);
*(_DWORD *)&v9.sa_data[2] = a1;
*(_WORD *)v9.sa_data = v3;
if ( connect(v2, &v9, v4) == -1 )
{
perror("connect");
if ( (v2 & 0x80000000) == 0 )
{
v8 = v2;
v2 = -1;
close(v8);
}
return v2;
}
if ( gimx_params[12] )
return v2;
v6 = inet_ntoa(*(struct in_addr *)&v9.sa_data[2]);
v7 = dcgettext(0LL, "connected to %s:%d\n", 5);
printf(v7, v6, a2);
fflush(stdout);
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned __int16 a2)
{
unsigned int v2; // er12
__int16 v3; // ax
socklen_t v4; // edx
__int64 result; // rax
char *v6; // rbp
char *v7; // rax
int v8; // edi
struct sockaddr v9; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 6);
if ( v2 == -1 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)&v9.sa_data[6] = 0LL;
v9.sa_family = 2;
v3 = func0(a2);
*(_DWORD *)&v9.sa_data[2] = a1;
*(_WORD *)v9.sa_data = v3;
if ( connect(v2, &v9, v4) == -1 )
{
perror("connect");
if ( (v2 & 0x80000000) == 0 )
{
v8 = v2;
v2 = -1;
close(v8);
}
return v2;
}
if ( gimx_params[12] )
return v2;
v6 = inet_ntoa(*(struct in_addr *)&v9.sa_data[2]);
v7 = dcgettext(0LL, "connected to %s:%d\n", 5);
printf(v7, v6, a2);
fflush(stdout);
result = v2;
}
return result;
}

original_code:
int tcp_connect(unsigned int ip, unsigned short port)
{
  int fd;
  int error = 0;
  if ((fd = socket(2, SOCK_STREAM, IPPROTO_TCP)) != -1)
  {
    struct sockaddr_in sa =
    { .sin_family = 2, .sin_port = htons(port), .sin_addr.s_addr = ip };
    if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1)
    {
      do { if(!gimx_params.curses_status) { printf(gettext("connected to %s:%d\n"), inet_ntoa(sa.sin_addr), port); fflush(stdout); } } while (0);
    }
    else
    {
      perror("connect");
      error = 1;
    }
  }
  else
  {
    perror("socket");
    error = 1;
  }
  if(error && fd >= 0)
  {
    close(fd);
    fd = -1;
  }
  return fd;
}

variable_types:
ip
unsigned int

variable_types:
port
unsigned short

variable_types: fd int

variable_types: error int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall tcp_close(int a1)
{
if ( a1 >= 0 )
close(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
if ( a1 >= 0 )
close(a1);
return 1LL;
}

original_code:
int tcp_close(int fd)
{
  if(fd >= 0)
  {
    close(fd);
  }
  return 1;
}


decompiled_code:
ssize_t __fastcall tcp_send(int a1, const void *a2, int a3)
{
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-10h]

result = send(a1, a2, a3, 16448);
if ( (int)result < 0 )
{
v4 = result;
perror("send");
result = v4;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int a1, const void *a2, int a3)
{
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-10h]

result = send(a1, a2, a3, 16448);
if ( (int)result < 0 )
{
v4 = result;
perror("send");
result = v4;
}
return result;
}

original_code:
int tcp_send(int fd, const unsigned char* buf, int length)
{
  int ret = send(fd, (const void*)buf, length, MSG_DONTWAIT | MSG_NOSIGNAL);
  if(ret < 0)
  {
    perror("send");
  }
  return ret;
}

canonical_original_code:
int tcp_send(int fd, unsigned char *buf, int length)
{
  int ret = send(fd, (const void*)buf, length, MSG_DONTWAIT | MSG_NOSIGNAL);
  if(ret < 0)
  {
    perror("send");
  }
  return ret;
}

variable_types:
buf
unsigned char *

variable_types: length int

variable_types: ret int


decompiled_code:
ssize_t __fastcall tcp_recv(int a1, void *a2, int a3)
{
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-10h]

result = recv(a1, a2, a3, 0);
if ( (int)result < 0 )
{
v4 = result;
perror("recv");
result = v4;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int a1, void *a2, int a3)
{
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-10h]

result = recv(a1, a2, a3, 0);
if ( (int)result < 0 )
{
v4 = result;
perror("recv");
result = v4;
}
return result;
}

original_code:
int tcp_recv(int fd, unsigned char* buf, int len)
{
  int ret = recv(fd, (void*)buf, len, 0);
  if(ret < 0)
  {
    perror("recv");
  }
  return ret;
}

canonical_original_code:
int tcp_recv(int fd, unsigned char *buf, int len)
{
  int ret = recv(fd, (void*)buf, len, 0);
  if(ret < 0)
  {
    perror("recv");
  }
  return ret;
}

variable_types: len int


hatToButtons

decompiled_code:
__int64 __fastcall hatToButtons(unsigned __int8 a1)
{
__int64 result; // rax

result = 0LL;
if ( a1 <= 7u )
result = (unsigned __int8)CSWTCH_2[a1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
__int64 result; // rax

result = 0LL;
if ( a1 <= 7u )
result = (unsigned __int8)CSWTCH_2[a1];
return result;
}

original_code:
static inline unsigned char hatToButtons(const unsigned char hat)
{
  unsigned char buttons = 0x00;
  switch(hat)
  {
    case 0x00:
      buttons = 0x01;
      break;
    case 0x01:
      buttons = 0x01 | 0x02;
      break;
    case 0x02:
      buttons = 0x02;
      break;
    case 0x03:
      buttons = 0x04 | 0x02;
      break;
    case 0x04:
      buttons = 0x04;
      break;
    case 0x05:
      buttons = 0x04 | 0x08;
      break;
    case 0x06:
      buttons = 0x08;
      break;
    case 0x07:
      buttons = 0x01 | 0x08;
      break;
    case 0x08:
      break;
  }
  return buttons;
}

canonical_original_code:
static inline unsigned char hatToButtons(unsigned char hat)
{
  unsigned char buttons = 0x00;
  switch(hat)
  {
    case 0x00:
      buttons = 0x01;
      break;
    case 0x01:
      buttons = 0x01 | 0x02;
      break;
    case 0x02:
      buttons = 0x02;
      break;
    case 0x03:
      buttons = 0x04 | 0x02;
      break;
    case 0x04:
      buttons = 0x04;
      break;
    case 0x05:
      buttons = 0x04 | 0x08;
      break;
    case 0x06:
      buttons = 0x08;
      break;
    case 0x07:
      buttons = 0x01 | 0x08;
      break;
    case 0x08:
      break;
  }
  return buttons;
}

variable_types:
hat
unsigned char

variable_types:
buttons
unsigned char

return_type: unsigned char


original_code:
static inline void update_finger(s_trackpad_finger* current, s_trackpad_finger* previous, int* presence, int* axis_x, int* axis_y)
{
  unsigned char* coords = current->coords;
  unsigned char* prevCoords = previous->coords;
  if(current->id & 0x80)
  {
    if(*presence == 1)
    {
      *presence = 0;
    }
  }
  else
  {
    *presence = 1;
    unsigned short prev_x;
    unsigned short prev_y;
    if(previous->id & 0x80)
    {
      prev_x = 1919/2;
      prev_y = 919/2;
    }
    else
    {
      prev_x = ((prevCoords[1] & 0x0F) << 8) | prevCoords[0];
      prev_y = prevCoords[2] << 4 | (prevCoords[1]>>4);
    }
    *axis_x = (((coords[1] & 0x0F) << 8) | coords[0]) - prev_x;
    *axis_y = (coords[2] << 4 | (coords[1]>>4)) - prev_y;
  }
}

canonical_original_code:
static inline void update_finger(struct s_trackpad_finger *current, struct s_trackpad_finger *previous, int *presence, int *axis_x, int *axis_y)
{
  unsigned char *coords = current->coords;
  unsigned char *prevCoords = previous->coords;
  if(current->id & 0x80)
  {
    if(*presence == 1)
    {
      *presence = 0;
    }
  }
  else
  {
    *presence = 1;
    unsigned short prev_x;
    unsigned short prev_y;
    if(previous->id & 0x80)
    {
      prev_x = 1919/2;
      prev_y = 919/2;
    }
    else
    {
      prev_x = ((prevCoords[1] & 0x0F) << 8) | prevCoords[0];
      prev_y = prevCoords[2] << 4 | (prevCoords[1]>>4);
    }
    *axis_x = (((coords[1] & 0x0F) << 8) | coords[0]) - prev_x;
    *axis_y = (coords[2] << 4 | (coords[1]>>4)) - prev_y;
  }
}

variable_types: struct s_trackpad_finger { unsigned char id; unsigned char coords[3]; } *

variable_types:
previous
struct s_trackpad_finger *

variable_types:
presence
int *

variable_types:
coords
unsigned char *

variable_types:
prevCoords
unsigned char *


decompiled_code:
__int64 __fastcall button2event(__int64 (__fastcall *a1)(_BYTE *), _BYTE *a2, __int16 a3, unsigned __int16 a4, unsigned __int16 a5, char a6)
{
__int16 v6; // dx
__int64 result; // rax

v6 = a5 & a3;
result = (__int64)a1;
if ( (a4 & a5) != v6 )
{
a2[2] = a6;
*a2 = (v6 == 0) + 8;
result = a1(a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 (__fastcall *a1)(_BYTE *), _BYTE *a2, __int16 a3, unsigned __int16 a4, unsigned __int16 a5, char a6)
{
__int16 v6; // dx
__int64 result; // rax

v6 = a5 & a3;
result = (__int64)a1;
if ( (a4 & a5) != v6 )
{
a2[2] = a6;
*a2 = (v6 == 0) + 8;
result = a1(a2);
}
return result;
}

original_code:
static inline void button2event(int (*callback)(GE_Event*), GE_Event* event, unsigned short buttons,
    unsigned short pbuttons, unsigned short button_mask, uint8_t button_id)
{
  unsigned short value;
  if((value = (buttons & button_mask)) ^ (pbuttons & button_mask))
  {
    event->type = value ? GE_JOYBUTTONDOWN : GE_JOYBUTTONUP;
    event->jbutton.button = button_id;
    callback(event);
  }
}

canonical_original_code:
static inline void button2event(int (*callback)(union GE_Event *), union GE_Event *event, unsigned short buttons, unsigned short pbuttons, unsigned short button_mask, unsigned char button_id)
{
  unsigned short value;
  if((value = (buttons & button_mask)) ^ (pbuttons & button_mask))
  {
    event->type = value ? GE_JOYBUTTONDOWN : GE_JOYBUTTONUP;
    event->jbutton.button = button_id;
    callback(event);
  }
}

variable_types: int (*)(union GE_Event { struct { unsigned char type; unsigned char which; }; struct GE_KeyboardEvent { unsigned char type; unsigned char which; unsigned short keysym; } key; struct GE_MouseMotionEvent { unsigned char type; unsigned char which; short xrel; short yrel; } motion; struct GE_MouseButtonEvent { unsigned char type; unsigned char which; unsigned char button; } button; struct GE_JoyAxisEvent { unsigned char type; unsigned char which; unsigned char axis; short value; } jaxis; struct GE_JoyHatEvent { unsigned char type; unsigned char which; unsigned char hat; unsigned char value; } jhat; struct GE_JoyButtonEvent { unsigned char type; unsigned char which; unsigned char button; unsigned char state; } jbutton; struct GE_JoyRumbleEvent { unsigned char type; unsigned char which; unsigned short weak; unsigned short strong; } jrumble; struct GE_JoyConstantForceEvent { unsigned char type; unsigned char which; short level; } jconstant; struct GE_JoyConditionForceEvent { unsigned char type; unsigned char which; struct { unsigned short left; unsigned short right; } saturation; struct { short left; short right; } coefficient; short center; unsigned short deadband; } jcondition; struct GE_JoyPeriodicForceEvent { unsigned char type; unsigned char which; struct { int direction; unsigned short period; short magnitude; short offset; } sine; } jperiodic; } *)

variable_types:
event
union GE_Event *

variable_types:
buttons
unsigned short

variable_types:
pbuttons
unsigned short

variable_types:
value
unsigned short


decompiled_code:
__int64 bt_device_abs_get()
{
return bt_device_abs[bt_abs_value];
}

canonical_decompiled_code:
__int64 func0()
{
return bt_device_abs[bt_abs_value];
}

original_code:
s_bt_device_abs * bt_device_abs_get()
{
  return bt_device_abs[bt_abs_value];
}

canonical_original_code:
struct s_bt_device_abs *bt_device_abs_get()
{
  return bt_device_abs[bt_abs_value];
}

return_type: struct s_bt_device_abs { int (*init)(); int (*get_bdaddr)(int device_number, struct bdaddr_t { unsigned char b[6]; } *bdaddr); int (*write_device_class)(int device_number, unsigned int devclass); } *

user_defined_types: struct s_bt_device_abs { int (*init)(); int (*get_bdaddr)(int device_number, struct bdaddr_t *bdaddr); int (*write_device_class)(int device_number, unsigned int devclass); }


decompiled_code:
void __fastcall bt_device_abs_register(int a1, __int64 a2)
{
bt_device_abs[a1] = a2;
}

canonical_decompiled_code:
void __fastcall func1(int a1, __int64 a2)
{
bt_device_abs[a1] = a2;
}

original_code:
void bt_device_abs_register(e_bt_abs index, s_bt_device_abs * value)
{
  bt_device_abs[index] = value;
}

canonical_original_code:
void bt_device_abs_register(enum e_bt_abs index, struct s_bt_device_abs *value)
{
  bt_device_abs[index] = value;
}

variable_types: enum e_bt_abs { E_BT_ABS_BTSTACK=0, E_BT_ABS_BLUEZ=1, E_BT_ABS_MAX=2 }

variable_types: struct s_bt_device_abs { int (*init)(); int (*get_bdaddr)(int device_number, struct bdaddr_t { unsigned char b[6]; } *bdaddr); int (*write_device_class)(int device_number, unsigned int devclass); } *


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

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

original_code:
void stats_clean(struct stats *s) {
    free(s);
}

variable_types: struct stats { enum stats_type { E_STATS_TYPE_CONTROLLER=0, E_STATS_TYPE_MOUSE=1 } type; unsigned long long last; unsigned int cpt; unsigned int periods[21]; int period; } *

user_defined_types: struct stats { enum stats_type type; unsigned long long last; unsigned int cpt; unsigned int periods[21]; int period; }


original_code:
void stats_update(struct stats *s) {
    s->cpt++;
    if (s->type == E_STATS_TYPE_MOUSE && s->period == -1) {
        gtime now = gtime_gettime();
        if (s->last != 0) {
            gtime delta = now - s->last;
            unsigned int index = (delta + 250000UL) / 1000000UL;
            if (index < sizeof(s->periods) / sizeof(*s->periods)) {
                if (gimx_params.debug.stats) {
                    do { if(!gimx_params.curses_status) { printf("tnow = ""%llu"", delta = ""%llu"", index = %u\n", now, delta, index); fflush(stdout); } } while (0);
                }
                s->periods[index]++;
            }
        }
        s->last = now;
        if (s->cpt > 250) {
            unsigned int i;
            for (i = 1; i < sizeof(s->periods) / sizeof(*s->periods); ++i) {
                if (s->periods[i] > s->cpt * 10 / 100) {
                    s->period = i * 1000;
                    break;
                }
            }
        }
    }
}

canonical_original_code:
void stats_update(struct stats *s) {
    s->cpt++;
    if (s->type == E_STATS_TYPE_MOUSE && s->period == -1) {
        unsigned long long now = gtime_gettime();
        if (s->last != 0) {
            unsigned long long delta = now - s->last;
            unsigned int index = (delta + 250000UL) / 1000000UL;
            if (index < sizeof(s->periods) / sizeof(*s->periods)) {
                if (gimx_params.debug.stats) {
                    do { if(!gimx_params.curses_status) { printf("tnow = ""%llu"", delta = ""%llu"", index = %u\n", now, delta, index); fflush(stdout); } } while (0);
                }
                s->periods[index]++;
            }
        }
        s->last = now;
        if (s->cpt > 250) {
            unsigned int i;
            for (i = 1; i < sizeof(s->periods) / sizeof(*s->periods); ++i) {
                if (s->periods[i] > s->cpt * 10 / 100) {
                    s->period = i * 1000;
                    break;
                }
            }
        }
    }
}

variable_types:
now
unsigned long long

variable_types:
delta
unsigned long long

variable_types:
index
unsigned int

variable_types:
i
unsigned int

user_defined_types: enum stats_type { E_STATS_TYPE_CONTROLLER=0, E_STATS_TYPE_MOUSE=1 }


decompiled_code:
unsigned __int64 __fastcall stats_get_period(__int64 a1)
{
__int64 v2; // rcx
unsigned __int64 v3; // rax
unsigned __int64 v4; // rsi

if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 1 )
return *(unsigned int *)(a1 + 104);
}
else
{
v2 = gtime_gettime();
v3 = v2 - *(_QWORD *)(a1 + 8);
if ( v3 > 0x1DCD6500 )
{
v4 = *(unsigned int *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v2;
*(_DWORD *)(a1 + 16) = 0;
return v3 / v4 / 0x3E8;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1)
{
__int64 v2; // rcx
unsigned __int64 v3; // rax
unsigned __int64 v4; // rsi

if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 1 )
return *(unsigned int *)(a1 + 104);
}
else
{
v2 = gtime_gettime();
v3 = v2 - *(_QWORD *)(a1 + 8);
if ( v3 > 0x1DCD6500 )
{
v4 = *(unsigned int *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v2;
*(_DWORD *)(a1 + 16) = 0;
return v3 / v4 / 0x3E8;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int stats_get_period(struct stats *s) {
    int ret = -1;
    if (s->type == E_STATS_TYPE_CONTROLLER) {
        gtime tnow = gtime_gettime();
        gtime tdiff = tnow - s->last;
        if (tdiff > 500000000LL) {
            ret = ((tdiff / s->cpt) / 1000UL);
            s->last = tnow;
            s->cpt = 0;
        }
    } else if(s->type == E_STATS_TYPE_MOUSE) {
        ret = s->period;
    }
    return ret;
}

canonical_original_code:
int stats_get_period(struct stats *s) {
    int ret = -1;
    if (s->type == E_STATS_TYPE_CONTROLLER) {
        unsigned long long tnow = gtime_gettime();
        unsigned long long tdiff = tnow - s->last;
        if (tdiff > 500000000LL) {
            ret = ((tdiff / s->cpt) / 1000UL);
            s->last = tnow;
            s->cpt = 0;
        }
    } else if(s->type == E_STATS_TYPE_MOUSE) {
        ret = s->period;
    }
    return ret;
}

variable_types: ret int

variable_types:
tnow
unsigned long long

variable_types:
tdiff
unsigned long long


decompiled_code:
__int64 __fastcall bdaddr2hex(__int64 a1, __int64 a2)
{
__int64 i; // rax
int v4; // [rsp+0h] [rbp-28h] BYREF
char v5; // [rsp+4h] [rbp-24h] BYREF
char v6; // [rsp+8h] [rbp-20h] BYREF
char v7; // [rsp+Ch] [rbp-1Ch] BYREF
char v8; // [rsp+10h] [rbp-18h] BYREF
char v9; // [rsp+14h] [rbp-14h] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%2x:%2x:%2x:%2x:%2x:%2x", &v4, &v5, &v6, &v7, &v8, &v9) != 6 )
return 0xFFFFFFFFLL;
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a2 + i) = *(&v4 + i);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 i; // rax
int v4; // [rsp+0h] [rbp-28h] BYREF
char v5; // [rsp+4h] [rbp-24h] BYREF
char v6; // [rsp+8h] [rbp-20h] BYREF
char v7; // [rsp+Ch] [rbp-1Ch] BYREF
char v8; // [rsp+10h] [rbp-18h] BYREF
char v9; // [rsp+14h] [rbp-14h] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%2x:%2x:%2x:%2x:%2x:%2x", &v4, &v5, &v6, &v7, &v8, &v9) != 6 )
return 0xFFFFFFFFLL;
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a2 + i) = *(&v4 + i);
return 0LL;
}

original_code:
int bdaddr2hex(const char * str, unsigned char hex[6]) {
    unsigned int tmp[6];
    if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", tmp, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5) != 6) {
        return -1;
    }
    int i;
    for (i = 0; i < 6; ++i) {
        hex[i] = tmp[i];
    }
    return 0;
}

canonical_original_code:
int bdaddr2hex(char *str, unsigned char hex[6]) {
    unsigned int tmp[6];
    if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", tmp, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5) != 6) {
        return -1;
    }
    int i;
    for (i = 0; i < 6; ++i) {
        hex[i] = tmp[i];
    }
    return 0;
}

variable_types:
str
char *

variable_types:
hex
unsigned char [6]

variable_types:
tmp
unsigned int [6]

variable_types: i int


original_code:
int linkkey2hex(const char * str, unsigned char hex[16]) {
    unsigned int tmp[16] = { };
    if (sscanf(str, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", tmp, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5, tmp + 6, tmp + 7, tmp + 8,
            tmp + 9, tmp + 10, tmp + 11, tmp + 12, tmp + 13, tmp + 14, tmp + 15) != 16) {
        return -1;
    }
    int i;
    for (i = 0; i < 16; ++i) {
        hex[i] = tmp[i];
    }
    return 0;
}

canonical_original_code:
int linkkey2hex(char *str, unsigned char hex[16]) {
    unsigned int tmp[16] = { };
    if (sscanf(str, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", tmp, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5, tmp + 6, tmp + 7, tmp + 8,
            tmp + 9, tmp + 10, tmp + 11, tmp + 12, tmp + 13, tmp + 14, tmp + 15) != 16) {
        return -1;
    }
    int i;
    for (i = 0; i < 16; ++i) {
        hex[i] = tmp[i];
    }
    return 0;
}

variable_types:
hex
unsigned char [16]

variable_types:
tmp
unsigned int [16]


decompiled_code:
int __fastcall read_args(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "l:m:s:t::");
if ( result == -1 )
break;
if ( result == 115 )
{
slave = (__int64)optarg;
}
else if ( result > 115 )
{
if ( result != 116 )
goto LABEL_18;
type = 1;
}
else if ( result == 108 )
{
linkkey = (__int64)optarg;
}
else
{
if ( result != 109 )
goto LABEL_18;
master = (__int64)optarg;
}
}
if ( slave && (result = bdaddr2hex(slave, (__int64)&in), result < 0)
|| master && (result = bdaddr2hex(master, (__int64)&word_686), result < 0)
|| linkkey && (result = linkkey2hex(linkkey, (__int64)&qword_68C), result < 0) )
{
LABEL_18:
usage();
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "l:m:s:t::");
if ( result == -1 )
break;
if ( result == 115 )
{
slave = (__int64)optarg;
}
else if ( result > 115 )
{
if ( result != 116 )
goto LABEL_18;
type = 1;
}
else if ( result == 108 )
{
linkkey = (__int64)optarg;
}
else
{
if ( result != 109 )
goto LABEL_18;
master = (__int64)optarg;
}
}
if ( slave && (result = func6(slave, (__int64)&in), result < 0)
|| master && (result = func6(master, (__int64)&word_686), result < 0)
|| linkkey && (result = func7(linkkey, (__int64)&qword_68C), result < 0) )
{
LABEL_18:
func0();
exit(1);
}
return result;
}

original_code:
static void read_args(int argc, char* argv[]) {
    int opt;
    while ((opt = getopt(argc, argv, "l:m:s:t::")) != -1) {
        switch (opt) {
        case 'm':
            master = optarg;
            break;
        case 's':
            slave = optarg;
            break;
        case 'l':
            linkkey = optarg;
            break;
        case 't':
            type = 1;
            break;
        default:
            usage();
            exit(1);
            break;
        }
    }
    if (slave) {
        if (bdaddr2hex(slave, in.slave) < 0) {
            usage();
            exit(1);
        }
    }
    if (master) {
        if (bdaddr2hex(master, in.master) < 0) {
            usage();
            exit(1);
        }
    }
    if (linkkey) {
        if (linkkey2hex(linkkey, in.linkkey) < 0) {
            usage();
            exit(1);
        }
    }
}

canonical_original_code:
static void read_args(int argc, char *argv[]) {
    int opt;
    while ((opt = getopt(argc, argv, "l:m:s:t::")) != -1) {
        switch (opt) {
        case 'm':
            master = optarg;
            break;
        case 's':
            slave = optarg;
            break;
        case 'l':
            linkkey = optarg;
            break;
        case 't':
            type = 1;
            break;
        default:
            usage();
            exit(1);
            break;
        }
    }
    if (slave) {
        if (bdaddr2hex(slave, in.slave) < 0) {
            usage();
            exit(1);
        }
    }
    if (master) {
        if (bdaddr2hex(master, in.master) < 0) {
            usage();
            exit(1);
        }
    }
    if (linkkey) {
        if (linkkey2hex(linkkey, in.linkkey) < 0) {
            usage();
            exit(1);
        }
    }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int


usage

decompiled_code:
size_t usage()
{
fwrite("Usage: ds4tool [-t] [-l lk] [-m master] [-s slave]\n", 1uLL, 0x33uLL, stderr);
fwrite(" -t: Teensy mode\n", 1uLL, 0x12uLL, stderr);
fwrite(" -l lk: the link key to set\n", 1uLL, 0x1DuLL, stderr);
fwrite(" -m master: the master bdaddr to set\n", 1uLL, 0x26uLL, stderr);
fwrite(" -s slave: when -t is used, the slave bdaddr to set\n", 1uLL, 0x35uLL, stderr);
return fwrite(" when -t is not used, the slave bdaddr to select\n", 1uLL, 0x3CuLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
fwrite("Usage: ds4tool [-t] [-l lk] [-m master] [-s slave]\n", 1uLL, 0x33uLL, stderr);
fwrite(" -t: Teensy mode\n", 1uLL, 0x12uLL, stderr);
fwrite(" -l lk: the link key to set\n", 1uLL, 0x1DuLL, stderr);
fwrite(" -m master: the master bdaddr to set\n", 1uLL, 0x26uLL, stderr);
fwrite(" -s slave: when -t is used, the slave bdaddr to set\n", 1uLL, 0x35uLL, stderr);
return fwrite(" when -t is not used, the slave bdaddr to select\n", 1uLL, 0x3CuLL, stderr);
}

original_code:
static void usage() {
    fprintf(stderr, "Usage: ds4tool [-t] [-l lk] [-m master] [-s slave]\n");
    fprintf(stderr, "  -t: Teensy mode\n");
    fprintf(stderr, "  -l lk: the link key to set\n");
    fprintf(stderr, "  -m master: the master bdaddr to set\n");
    fprintf(stderr, "  -s slave: when -t is used, the slave bdaddr to set\n");
    fprintf(stderr, "            when -t is not used, the slave bdaddr to select\n");
}


decompiled_code:
void haptic_sink_os_update()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void haptic_sink_os_update(struct haptic_sink_state * state __attribute__((unused))) {
}

canonical_original_code:
static void haptic_sink_os_update(struct haptic_sink_state *state) {
}

variable_types: struct haptic_sink_state { int joystick; } *


decompiled_code:
unsigned int __fastcall haptic_sink_os_process(int *a1, __int64 a2)
{
int v2; // eax
unsigned int result; // eax
bool v4; // zf
unsigned int v5; // er12
char *v6; // rax
int v7; // eax
int v8; // eax
__int128 v9; // [rsp+0h] [rbp-38h] BYREF
__int128 v10[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = *a1;
v9 = 0LL;
BYTE1(v9) = v2;
result = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 4 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 13;
if ( !v4 )
{
*(_QWORD *)((char *)&v9 + 2) = *(_QWORD *)(a2 + 6);
*(_DWORD *)((char *)&v9 + 10) = *(_DWORD *)(a2 + 14);
}
goto LABEL_7;
}
if ( result > 4 )
{
if ( result != 6 || gimx_params[12] )
return result;
v5 = *(unsigned __int16 *)(a2 + 6);
v6 = dcgettext(0LL, "adjust your wheel range to %u degrees\n", 5);
printf(v6, v5);
fflush(stdout);
result = *(_DWORD *)a2;
}
else
{
if ( result == 2 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 11;
if ( !v4 )
WORD1(v9) = *(_WORD *)(a2 + 6);
goto LABEL_7;
}
if ( result == 3 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 12;
if ( !v4 )
{
*(_QWORD *)((char *)&v9 + 2) = *(_QWORD *)(a2 + 6);
*(_DWORD *)((char *)&v9 + 10) = *(_DWORD *)(a2 + 14);
}
LABEL_7:
result = ginput_joystick_set_haptic(&v9);
if ( gimx_params[13] )
result = dump_event((unsigned __int8 *)&v9);
return result;
}
}
if ( result == 1 )
{
v7 = *a1;
v10[0] = 0LL;
BYTE1(v10[0]) = v7;
v8 = *(_DWORD *)(a2 + 6);
LOBYTE(v10[0]) = 10;
*(_DWORD *)((char *)v10 + 2) = v8;
ginput_queue_push(v10);
result = gimx_params[13];
if ( result )
result = dump_event((unsigned __int8 *)v10);
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func2(int *a1, __int64 a2)
{
int v2; // eax
unsigned int result; // eax
bool v4; // zf
unsigned int v5; // er12
char *v6; // rax
int v7; // eax
int v8; // eax
__int128 v9; // [rsp+0h] [rbp-38h] BYREF
__int128 v10[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = *a1;
v9 = 0LL;
BYTE1(v9) = v2;
result = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 4 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 13;
if ( !v4 )
{
*(_QWORD *)((char *)&v9 + 2) = *(_QWORD *)(a2 + 6);
*(_DWORD *)((char *)&v9 + 10) = *(_DWORD *)(a2 + 14);
}
goto LABEL_7;
}
if ( result > 4 )
{
if ( result != 6 || gimx_params[12] )
return result;
v5 = *(unsigned __int16 *)(a2 + 6);
v6 = dcgettext(0LL, "adjust your wheel range to %u degrees\n", 5);
printf(v6, v5);
fflush(stdout);
result = *(_DWORD *)a2;
}
else
{
if ( result == 2 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 11;
if ( !v4 )
WORD1(v9) = *(_WORD *)(a2 + 6);
goto LABEL_7;
}
if ( result == 3 )
{
v4 = *(_BYTE *)(a2 + 4) == 0;
LOBYTE(v9) = 12;
if ( !v4 )
{
*(_QWORD *)((char *)&v9 + 2) = *(_QWORD *)(a2 + 6);
*(_DWORD *)((char *)&v9 + 10) = *(_DWORD *)(a2 + 14);
}
LABEL_7:
result = ginput_joystick_set_haptic(&v9);
if ( gimx_params[13] )
result = func1((unsigned __int8 *)&v9);
return result;
}
}
if ( result == 1 )
{
v7 = *a1;
v10[0] = 0LL;
BYTE1(v10[0]) = v7;
v8 = *(_DWORD *)(a2 + 6);
LOBYTE(v10[0]) = 10;
*(_DWORD *)((char *)v10 + 2) = v8;
ginput_queue_push(v10);
result = gimx_params[13];
if ( result )
result = func1((unsigned __int8 *)v10);
}
return result;
}

original_code:
static void haptic_sink_os_process(struct haptic_sink_state * state, const s_haptic_core_data * data) {
    int ret = 0;
    GE_Event event = { .which = state->joystick };
    switch (data->type) {
    case E_DATA_TYPE_CONSTANT:
        event.type = GE_JOYCONSTANTFORCE;
        if (data->playing) {
            event.jconstant.level = data->constant.level;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_SPRING:
        event.type = GE_JOYSPRINGFORCE;
        if (data->playing) {
            event.jcondition.saturation.left = data->spring.saturation.left;
            event.jcondition.saturation.right = data->spring.saturation.right;
            event.jcondition.coefficient.left = data->spring.coefficient.left;
            event.jcondition.coefficient.right = data->spring.coefficient.right;
            event.jcondition.center = data->spring.center;
            event.jcondition.deadband = data->spring.deadband;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_DAMPER:
        event.type = GE_JOYDAMPERFORCE;
        if (data->playing) {
            event.jcondition.saturation.left = data->damper.saturation.left;
            event.jcondition.saturation.right = data->damper.saturation.right;
            event.jcondition.coefficient.left = data->damper.coefficient.left;
            event.jcondition.coefficient.right = data->damper.coefficient.right;
            event.jcondition.center = data->damper.center;
            event.jcondition.deadband = data->damper.deadband;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_RANGE:
        do { if(!gimx_params.curses_status) { printf(gettext("adjust your wheel range to %u degrees\n"), data->range.value); fflush(stdout); } } while (0);
        break;
    case E_DATA_TYPE_NONE:
    case E_DATA_TYPE_RUMBLE:
    case E_DATA_TYPE_LEDS:
        break;
    }
    if (ret != 0) {
        ginput_joystick_set_haptic(&event);
        if(gimx_params.debug.haptic) {
            dump_event(&event);
        }
    } else if (data->type == E_DATA_TYPE_RUMBLE) {
        GE_Event event = {
            .jrumble = {
                .type = GE_JOYRUMBLE,
                .which = state->joystick,
                .weak = data->rumble.weak,
                .strong = data->rumble.strong
            }
        };
        ginput_queue_push(&event);
        if(gimx_params.debug.haptic) {
            dump_event(&event);
        }
    }
}

canonical_original_code:
static void haptic_sink_os_process(struct haptic_sink_state *state, struct s_haptic_core_data *data) {
    int ret = 0;
    union GE_Event event = { .which = state->joystick };
    switch (data->type) {
    case E_DATA_TYPE_CONSTANT:
        event.type = GE_JOYCONSTANTFORCE;
        if (data->playing) {
            event.jconstant.level = data->constant.level;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_SPRING:
        event.type = GE_JOYSPRINGFORCE;
        if (data->playing) {
            event.jcondition.saturation.left = data->spring.saturation.left;
            event.jcondition.saturation.right = data->spring.saturation.right;
            event.jcondition.coefficient.left = data->spring.coefficient.left;
            event.jcondition.coefficient.right = data->spring.coefficient.right;
            event.jcondition.center = data->spring.center;
            event.jcondition.deadband = data->spring.deadband;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_DAMPER:
        event.type = GE_JOYDAMPERFORCE;
        if (data->playing) {
            event.jcondition.saturation.left = data->damper.saturation.left;
            event.jcondition.saturation.right = data->damper.saturation.right;
            event.jcondition.coefficient.left = data->damper.coefficient.left;
            event.jcondition.coefficient.right = data->damper.coefficient.right;
            event.jcondition.center = data->damper.center;
            event.jcondition.deadband = data->damper.deadband;
        }
        ret = 1;
        break;
    case E_DATA_TYPE_RANGE:
        do { if(!gimx_params.curses_status) { printf(gettext("adjust your wheel range to %u degrees\n"), data->range.value); fflush(stdout); } } while (0);
        break;
    case E_DATA_TYPE_NONE:
    case E_DATA_TYPE_RUMBLE:
    case E_DATA_TYPE_LEDS:
        break;
    }
    if (ret != 0) {
        ginput_joystick_set_haptic(&event);
        if(gimx_params.debug.haptic) {
            dump_event(&event);
        }
    } else if (data->type == E_DATA_TYPE_RUMBLE) {
        union GE_Event event = {
            .jrumble = {
                .type = GE_JOYRUMBLE,
                .which = state->joystick,
                .weak = data->rumble.weak,
                .strong = data->rumble.strong
            }
        };
        ginput_queue_push(&event);
        if(gimx_params.debug.haptic) {
            dump_event(&event);
        }
    }
}

variable_types: struct s_haptic_core_data { enum e_haptic_core_data_type { E_DATA_TYPE_NONE=0, E_DATA_TYPE_RUMBLE=1, E_DATA_TYPE_CONSTANT=2, E_DATA_TYPE_SPRING=3, E_DATA_TYPE_DAMPER=4, E_DATA_TYPE_LEDS=5, E_DATA_TYPE_RANGE=6 } type; unsigned char playing; union { struct s_haptic_core_rumble { unsigned short weak; unsigned short strong; } rumble; struct s_haptic_core_constant { short level; } constant; struct s_haptic_core_condition { struct { unsigned short left; unsigned short right; } saturation; struct { short left; short right; } coefficient; short center; unsigned short deadband; } spring; struct s_haptic_core_condition damper; struct s_haptic_core_leds { unsigned char value; } leds; struct s_haptic_core_range { unsigned short value; } range; }; } *

variable_types: union GE_Event { struct { unsigned char type; unsigned char which; }; struct GE_KeyboardEvent { unsigned char type; unsigned char which; unsigned short keysym; } key; struct GE_MouseMotionEvent { unsigned char type; unsigned char which; short xrel; short yrel; } motion; struct GE_MouseButtonEvent { unsigned char type; unsigned char which; unsigned char button; } button; struct GE_JoyAxisEvent { unsigned char type; unsigned char which; unsigned char axis; short value; } jaxis; struct GE_JoyHatEvent { unsigned char type; unsigned char which; unsigned char hat; unsigned char value; } jhat; struct GE_JoyButtonEvent { unsigned char type; unsigned char which; unsigned char button; unsigned char state; } jbutton; struct GE_JoyRumbleEvent { unsigned char type; unsigned char which; unsigned short weak; unsigned short strong; } jrumble; struct GE_JoyConstantForceEvent { unsigned char type; unsigned char which; short level; } jconstant; struct GE_JoyConditionForceEvent { unsigned char type; unsigned char which; struct { unsigned short left; unsigned short right; } saturation; struct { short left; short right; } coefficient; short center; unsigned short deadband; } jcondition; struct GE_JoyPeriodicForceEvent { unsigned char type; unsigned char which; struct { int direction; unsigned short period; short magnitude; short offset; } sine; } jperiodic; }

user_defined_types: struct haptic_sink_state { int joystick; }