main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
int v4; // ebp
int v5; // ebx
__int64 v6; // rax
unsigned int v7; // er12
unsigned int i; // ebx
__int64 v9; // rax
int v10; // ebx
char *v11; // rax
__int64 v12; // rax
int v13; // eax
__int64 v14; // rax
__int64 v15; // rdx
__int64 (__fastcall *v16)(char *); // rdx
unsigned int v17; // eax
unsigned int v18; // ebx
char *v19; // rbp
int v20; // ebx
__int64 v21; // rbx
char *v22; // rax
FILE *v23; // rax
FILE *v24; // rbp
FILE *v25; // rdi
char *v27; // rax
struct termios s[68]; // [rsp+0h] [rbp-1048h] BYREF
char v29; // [rsp+1000h] [rbp-48h]
__int64 v30[7]; // [rsp+1010h] [rbp-38h] BYREF

v30[0] = 1LL;
v30[1] = 0LL;
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");
setlocale(1, "C");
v3 = (void *)gfile_homedir();
gimx_params = v3;
if ( !v3 )
{
v4 = -1;
goto LABEL_69;
}
gpppcprog_read_user_ids(v3, "/.gimx/");
if ( (int)args_read((unsigned int)argc, argv, &gimx_params) < 0 )
{
v4 = -1;
goto LABEL_69;
}
if ( *((_DWORD *)&gimx_params + 38) )
bt_abs_value = 0;
v4 = adapter_detect();
if ( v4 )
goto LABEL_69;
v5 = *((_DWORD *)&gimx_params + 7);
if ( v5 == -1 )
{
v9 = adapter_get(0LL);
v10 = controller_get_default_refresh_period(*(unsigned int *)(v9 + 736));
*((_DWORD *)&gimx_params + 7) = v10;
*((_DWORD *)&gimx_params + 34) = 33750 / v10;
if ( !*((_DWORD *)&gimx_params + 12) )
{
argv = (const char **)"using default refresh period: %.02fms\n";
v11 = dcgettext(0LL, "using default refresh period: %.02fms\n", 5);
printf(v11, (double)v10 / 1000.0);
fflush(stdout);
}
}
else
{
v6 = adapter_get(0LL);
if ( v5 < (int)controller_get_min_refresh_period(*(unsigned int *)(v6 + 736)) )
{
setColorStderr(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v12 = adapter_get(0LL);
v13 = controller_get_min_refresh_period(*(unsigned int *)(v12 + 736));
fprintf(stderr, "Refresh period should be at least %.02fms\n", (double)v13 / 1000.0);
v4 = -1;
goto LABEL_69;
}
}
*((double *)&gimx_params + 4) = 11250.0 / (double)*((int *)&gimx_params + 7);
v7 = v4;
for ( i = 0; i != 7; ++i )
{
v14 = adapter_get(i);
if ( *(_DWORD *)(v14 + 748) )
{
*(_DWORD *)(v14 + 916) = 1;
v7 = 1;
if ( !*(_QWORD *)(v14 + 312) && !*((_DWORD *)&gimx_params + 12) )
{
puts("The --event argument may require running two gimx instances.");
fflush(stdout);
}
}
}
if ( v7 )
{
if ( (int)adapter_start() >= 0 )
{
adapter_send();
goto LABEL_32;
}
v4 = -1;
LABEL_69:
adapter_clean();
goto LABEL_70;
}
if ( !*(&gimx_params + 16) )
goto LABEL_48;
v16 = (__int64 (__fastcall *)(char *))ignore_event;
if ( !*(&gimx_params + 2) )
v16 = process_event;
*(_QWORD *)&s[0].c_iflag = &gpoll_register_fd;
*(_QWORD *)&s[0].c_cflag = &gpoll_remove_fd;
argv = (const char **)(1 - ((unsigned int)(*((_DWORD *)&gimx_params + 36) == 0) - 1));
if ( (int)ginput_init(s, argv, v16) < 0 )
goto LABEL_57;
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_57;
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
{
argv = (const char **)v7;
if ( (unsigned int)adapter_get_device(2LL, v7) != -1
|| (argv = (const char **)v7, (unsigned int)adapter_get_device(1LL, v7) != -1) )
{
*((_DWORD *)&gimx_params + 6) = 1;
}
++v7;
}
while ( v7 != 7 );
}
grab();
ginput_release_unused();
macros_init();
if ( !*(&gimx_params + 2) )
goto LABEL_47;
WORD1(v30[0]) = ginput_key_id();
if ( !WORD1(v30[0]) )
{
setColorStderr(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
v21 = (__int64)*(&gimx_params + 2);
v22 = dcgettext(0LL, "Unknown key name for argument --keygen: '%s'\n", 5);
fprintf(stderr, v22, v21);
LABEL_57:
v4 = -1;
goto LABEL_69;
}
macro_lookup(v30);
LABEL_47:
cfg_trigger_init();
LABEL_48:
if ( *((_DWORD *)&gimx_params + 11) )
{
glog_set_all_levels(0LL, argv, v15);
*((_DWORD *)&gimx_params + 12) = 1;
display_init();
}
else if ( !*(&gimx_params + 21) )
{
tcgetattr(1, s);
s[0].c_lflag &= 0xFFFFFFF7;
tcsetattr(1, 0, s);
}
if ( (int)adapter_start() < 0 )
{
v4 = -1;
goto LABEL_69;
}
usb_poll_interrupts();
if ( (int)gprio_init() < 0 && !*((_DWORD *)&gimx_params + 12) )
{
setColorStdout(14);
printf("Warning: ");
resetColorStdout();
puts("failed to set process priority");
fflush(stdout);
}
v4 = mainloop();
if ( v4 )
{
gprio_clean();
v20 = v4;
if ( *((_DWORD *)&gimx_params + 12) )
{
adapter_clean();
v18 = v4;
if ( !*(&gimx_params + 16) )
goto LABEL_35;
goto LABEL_72;
}
goto LABEL_85;
}
gprio_clean();
v20 = *((_DWORD *)&gimx_params + 12);
if ( !v20 )
{
LABEL_85:
v27 = dcgettext(0LL, "Exiting\n", 5);
printf(v27);
fflush(stdout);
v4 = v20;
}
LABEL_32:
v17 = adapter_clean();
v18 = v17;
if ( !v4 && v17 )
{
if ( !*(&gimx_params + 16) )
goto LABEL_35;
goto LABEL_72;
}
LABEL_70:
if ( *(&gimx_params + 16) )
{
v18 = v4;
LABEL_72:
macros_clean();
cfg_clean();
ginput_quit();
xmlCleanupParser();
v4 = v18;
}
if ( !v4 )
goto LABEL_74;
v18 = v4;
LABEL_35:
*(_QWORD *)&s[0].c_iflag = 0LL;
*(_QWORD *)&s[0].c_cflag = 0LL;
memset(&s[0].c_line, 0, 0xFF0uLL);
v29 = 0;
v19 = (char *)gfile_tempdir();
if ( strlen(v19) + 13 <= 0x1001 )
sprintf((char *)s, "%s/gimx.status", v19);
free(v19);
if ( LOBYTE(s[0].c_iflag) )
{
v23 = (FILE *)gfile_fopen(s, "w");
v24 = v23;
if ( v23 )
{
fprintf(v23, "%d\n", v18);
fclose(v24);
if ( (int)gfile_makeown(s) < 0 )
{
setColorStderr(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
fwrite("failed to set ownership of the gimx status file\n", 1uLL, 0x30uLL, stderr);
}
}
}
v4 = v18;
LABEL_74:
v25 = (FILE *)*(&gimx_params + 21);
if ( v25 )
{
fclose(v25);
snprintf(
(char *)s,
0x1000uLL,
"%s%s%s%s",
(const char *)gimx_params,
"/.gimx/",
"log/",
(const char *)*(&gimx_params + 20));
if ( (int)gfile_makeown(s) < 0 )
{
setColorStderr(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
resetColorStderr();
fwrite("failed to set ownership of the gimx log file\n", 1uLL, 0x2DuLL, stderr);
}
if ( !*((_DWORD *)&gimx_params + 11) )
{
if ( *(&gimx_params + 21) )
goto LABEL_79;
goto LABEL_65;
}
LABEL_67:
display_end();
goto LABEL_79;
}
if ( *((_DWORD *)&gimx_params + 11) )
goto LABEL_67;
LABEL_65:
tcgetattr(1, s);
s[0].c_lflag |= 8u;
tcsetattr(1, 0, s);
LABEL_79:
free(gimx_params);
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
int v4; // ebp
int v5; // ebx
__int64 v6; // rax
unsigned int v7; // er12
unsigned int i; // ebx
__int64 v9; // rax
int v10; // ebx
char *v11; // rax
__int64 v12; // rax
int v13; // eax
__int64 v14; // rax
__int64 v15; // rdx
__int64 (__fastcall *v16)(char *); // rdx
unsigned int v17; // eax
unsigned int v18; // ebx
char *v19; // rbp
int v20; // ebx
__int64 v21; // rbx
char *v22; // rax
FILE *v23; // rax
FILE *v24; // rbp
FILE *v25; // rdi
char *v27; // rax
struct termios s[68]; // [rsp+0h] [rbp-1048h] BYREF
char v29; // [rsp+1000h] [rbp-48h]
__int64 v30[7]; // [rsp+1010h] [rbp-38h] BYREF

v30[0] = 1LL;
v30[1] = 0LL;
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");
setlocale(1, "C");
v3 = (void *)gfile_homedir();
gimx_params = v3;
if ( !v3 )
{
v4 = -1;
goto LABEL_69;
}
gpppcprog_read_user_ids(v3, "/.gimx/");
if ( (int)args_read((unsigned int)argc, argv, &gimx_params) < 0 )
{
v4 = -1;
goto LABEL_69;
}
if ( *((_DWORD *)&gimx_params + 38) )
bt_abs_value = 0;
v4 = adapter_detect();
if ( v4 )
goto LABEL_69;
v5 = *((_DWORD *)&gimx_params + 7);
if ( v5 == -1 )
{
v9 = adapter_get(0LL);
v10 = controller_get_default_refresh_period(*(unsigned int *)(v9 + 736));
*((_DWORD *)&gimx_params + 7) = v10;
*((_DWORD *)&gimx_params + 34) = 33750 / v10;
if ( !*((_DWORD *)&gimx_params + 12) )
{
argv = (const char **)"using default refresh period: %.02fms\n";
v11 = dcgettext(0LL, "using default refresh period: %.02fms\n", 5);
printf(v11, (double)v10 / 1000.0);
fflush(stdout);
}
}
else
{
v6 = adapter_get(0LL);
if ( v5 < (int)controller_get_min_refresh_period(*(unsigned int *)(v6 + 736)) )
{
func6(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
func7();
v12 = adapter_get(0LL);
v13 = controller_get_min_refresh_period(*(unsigned int *)(v12 + 736));
fprintf(stderr, "Refresh period should be at least %.02fms\n", (double)v13 / 1000.0);
v4 = -1;
goto LABEL_69;
}
}
*((double *)&gimx_params + 4) = 11250.0 / (double)*((int *)&gimx_params + 7);
v7 = v4;
for ( i = 0; i != 7; ++i )
{
v14 = adapter_get(i);
if ( *(_DWORD *)(v14 + 748) )
{
*(_DWORD *)(v14 + 916) = 1;
v7 = 1;
if ( !*(_QWORD *)(v14 + 312) && !*((_DWORD *)&gimx_params + 12) )
{
puts("The --event argument may require running two gimx instances.");
fflush(stdout);
}
}
}
if ( v7 )
{
if ( (int)adapter_start() >= 0 )
{
adapter_send();
goto LABEL_32;
}
v4 = -1;
LABEL_69:
adapter_clean();
goto LABEL_70;
}
if ( !*(&gimx_params + 16) )
goto LABEL_48;
v16 = (__int64 (__fastcall *)(char *))ignore_event;
if ( !*(&gimx_params + 2) )
v16 = process_event;
*(_QWORD *)&s[0].c_iflag = &gpoll_register_fd;
*(_QWORD *)&s[0].c_cflag = &gpoll_remove_fd;
argv = (const char **)(1 - ((unsigned int)(*((_DWORD *)&gimx_params + 36) == 0) - 1));
if ( (int)ginput_init(s, argv, v16) < 0 )
goto LABEL_57;
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_57;
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
{
argv = (const char **)v7;
if ( (unsigned int)adapter_get_device(2LL, v7) != -1
|| (argv = (const char **)v7, (unsigned int)adapter_get_device(1LL, v7) != -1) )
{
*((_DWORD *)&gimx_params + 6) = 1;
}
++v7;
}
while ( v7 != 7 );
}
func8();
ginput_release_unused();
macros_init();
if ( !*(&gimx_params + 2) )
goto LABEL_47;
WORD1(v30[0]) = ginput_key_id();
if ( !WORD1(v30[0]) )
{
func6(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
func7();
v21 = (__int64)*(&gimx_params + 2);
v22 = dcgettext(0LL, "Unknown key name for argument --keygen: '%s'\n", 5);
fprintf(stderr, v22, v21);
LABEL_57:
v4 = -1;
goto LABEL_69;
}
macro_lookup(v30);
LABEL_47:
cfg_trigger_init();
LABEL_48:
if ( *((_DWORD *)&gimx_params + 11) )
{
glog_set_all_levels(0LL, argv, v15);
*((_DWORD *)&gimx_params + 12) = 1;
display_init();
}
else if ( !*(&gimx_params + 21) )
{
tcgetattr(1, s);
s[0].c_lflag &= 0xFFFFFFF7;
tcsetattr(1, 0, s);
}
if ( (int)adapter_start() < 0 )
{
v4 = -1;
goto LABEL_69;
}
usb_poll_interrupts();
if ( (int)gprio_init() < 0 && !*((_DWORD *)&gimx_params + 12) )
{
func4(14);
printf("Warning: ");
func5();
puts("failed to set process priority");
fflush(stdout);
}
v4 = mainloop();
if ( v4 )
{
gprio_clean();
v20 = v4;
if ( *((_DWORD *)&gimx_params + 12) )
{
adapter_clean();
v18 = v4;
if ( !*(&gimx_params + 16) )
goto LABEL_35;
goto LABEL_72;
}
goto LABEL_85;
}
gprio_clean();
v20 = *((_DWORD *)&gimx_params + 12);
if ( !v20 )
{
LABEL_85:
v27 = dcgettext(0LL, "Exiting\n", 5);
printf(v27);
fflush(stdout);
v4 = v20;
}
LABEL_32:
v17 = adapter_clean();
v18 = v17;
if ( !v4 && v17 )
{
if ( !*(&gimx_params + 16) )
goto LABEL_35;
goto LABEL_72;
}
LABEL_70:
if ( *(&gimx_params + 16) )
{
v18 = v4;
LABEL_72:
macros_clean();
cfg_clean();
ginput_quit();
xmlCleanupParser();
v4 = v18;
}
if ( !v4 )
goto LABEL_74;
v18 = v4;
LABEL_35:
*(_QWORD *)&s[0].c_iflag = 0LL;
*(_QWORD *)&s[0].c_cflag = 0LL;
memset(&s[0].c_line, 0, 0xFF0uLL);
v29 = 0;
v19 = (char *)gfile_tempdir();
if ( strlen(v19) + 13 <= 0x1001 )
sprintf((char *)s, "%s/gimx.status", v19);
free(v19);
if ( LOBYTE(s[0].c_iflag) )
{
v23 = (FILE *)gfile_fopen(s, "w");
v24 = v23;
if ( v23 )
{
fprintf(v23, "%d\n", v18);
fclose(v24);
if ( (int)gfile_makeown(s) < 0 )
{
func6(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
func7();
fwrite("failed to set ownership of the gimx status file\n", 1uLL, 0x30uLL, stderr);
}
}
}
v4 = v18;
LABEL_74:
v25 = (FILE *)*(&gimx_params + 21);
if ( v25 )
{
fclose(v25);
snprintf(
(char *)s,
0x1000uLL,
"%s%s%s%s",
(const char *)gimx_params,
"/.gimx/",
"log/",
(const char *)*(&gimx_params + 20));
if ( (int)gfile_makeown(s) < 0 )
{
func6(12);
fwrite("Error: ", 1uLL, 7uLL, stderr);
func7();
fwrite("failed to set ownership of the gimx log file\n", 1uLL, 0x2DuLL, stderr);
}
if ( !*((_DWORD *)&gimx_params + 11) )
{
if ( *(&gimx_params + 21) )
goto LABEL_79;
goto LABEL_65;
}
LABEL_67:
display_end();
goto LABEL_79;
}
if ( *((_DWORD *)&gimx_params + 11) )
goto LABEL_67;
LABEL_65:
tcgetattr(1, s);
s[0].c_lflag |= 8u;
tcsetattr(1, 0, s);
LABEL_79:
free(gimx_params);
return v4;
}

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; }; }


repo: matlo/GIMX

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;
}

return_type: int


decompiled_code:
__int64 __fastcall btstack_common_recv(_WORD *a1)
{
__int64 v2; // rdi
unsigned __int16 v3; // si
int v4; // eax
unsigned int v5; // edx

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

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

variable_types: ret int


decompiled_code:
__int64 __fastcall btstack_common_recv_packet(_WORD *a1)
{
int v2; // edi
int v3; // eax
__int64 v4; // rdx
__int64 result; // rax
struct timeval v6; // [rsp+0h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+10h] [rbp-118h] BYREF
fd_set readfds; // [rsp+90h] [rbp-98h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func2(_WORD *a1)
{
int v2; // edi
int v3; // eax
__int64 v4; // rdx
__int64 result; // rax
struct timeval v6; // [rsp+0h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+10h] [rbp-118h] BYREF
fd_set readfds; // [rsp+90h] [rbp-98h] BYREF

a1[516] = 0;
a1[517] = 0;
while ( 1 )
{
v6.tv_usec = 0LL;
v6.tv_sec = 1LL;
memset(&readfds, 0, sizeof(readfds));
v2 = btstack_fd;
readfds.fds_bits[btstack_fd / 64] |= 1LL << (btstack_fd % 64);
*(__m128i *)exceptfds.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
*(__m128i *)&exceptfds.fds_bits[2] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
*(__m128i *)&exceptfds.fds_bits[4] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
*(__m128i *)&exceptfds.fds_bits[6] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
*(__m128i *)&exceptfds.fds_bits[8] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)&exceptfds.fds_bits[10] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
*(__m128i *)&exceptfds.fds_bits[12] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
*(__m128i *)&exceptfds.fds_bits[14] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
v3 = select(v2 + 1, &readfds, 0LL, &exceptfds, &v6);
if ( v3 < 0 )
{
perror("select");
return 0xFFFFFFFFLL;
}
if ( v3 <= 0 )
return 0xFFFFFFFFLL;
v4 = 1LL << (btstack_fd % 64);
if ( (exceptfds.fds_bits[btstack_fd / 64] & v4) != 0 )
break;
if ( (readfds.fds_bits[btstack_fd / 64] & v4) != 0 )
{
result = func1(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)
{
unsigned int v6; // ebx
char v8[30]; // [rsp+Ah] [rbp-1Eh] BYREF

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

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

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

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 __int16 v1; // bx
__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 = 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 __int16 v1; // bx
__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 = 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; // ebx
int v3; // eax

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

canonical_decompiled_code:
__int64 func5()
{
in_addr_t v0; // eax
int v1; // ebx
int v3; // eax

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

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: type int

variable_types:
packet
unsigned char *


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

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

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

if ( (int)func4((__int64)&btstack_hci_disconnect, a1, 19LL) >= 0 )
{
do
{
while ( 1 )
{
v1 = func2(&recv_data);
if ( v1 > 0 )
break;
if ( v1 )
return tcp_close((unsigned int)btstack_fd);
}
}
while ( recv_data != 4 || byte_9A6 != 14 || (unsigned __int16)word_9A9 != btstack_hci_disconnect );
}
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 *

return_type: void


decompiled_code:
__int64 __fastcall st_write_bd_addr(unsigned int a1, _QWORD *a2)
{
return ericsson_store_in_flash(a1, 254, 6u, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, _QWORD *a2)
{
return func2(a1, 254, 6u, 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);
}

variable_types: dd int


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: 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(unsigned int a1, __int64 a2)
{
int v3; // [rsp+Ah] [rbp-4Eh] BYREF
__int16 v4; // [rsp+Eh] [rbp-4Ah]
__int64 v5[9]; // [rsp+10h] [rbp-48h] BYREF

v3 = 0;
v4 = 0;
bacpy((__int64)&v3, a2);
v5[0] = 852031LL;
v5[2] = 6LL;
v5[3] = 0LL;
v5[4] = 0LL;
v5[1] = (__int64)&v3;
return (unsigned int)((int)hci_send_req(a1, v5, 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 func10(__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


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)
{
int v2; // ebx
char *v4; // r12
char *v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned __int16 a2)
{
int v2; // ebx
char *v4; // r12
char *v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

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

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:
__int64 __fastcall tcp_send(int a1, const void *a2, int a3)
{
int v3; // ebx

v3 = send(a1, a2, a3, 16448);
if ( v3 < 0 )
perror("send");
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, const void *a2, int a3)
{
int v3; // ebx

v3 = send(a1, a2, a3, 16448);
if ( v3 < 0 )
perror("send");
return (unsigned int)v3;
}

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:
__int64 __fastcall tcp_recv(int a1, void *a2, int a3)
{
int v3; // ebx

v3 = recv(a1, a2, a3, 0);
if ( v3 < 0 )
perror("recv");
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, void *a2, int a3)
{
int v3; // ebx

v3 = recv(a1, a2, a3, 0);
if ( v3 < 0 )
perror("recv");
return (unsigned int)v3;
}

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 int a1)
{
__int64 result; // rax

switch ( (char)a1 )
{
case 0:
result = 1LL;
break;
case 1:
result = 3LL;
break;
case 2:
result = a1;
break;
case 3:
result = 6LL;
break;
case 4:
result = a1;
break;
case 5:
result = 12LL;
break;
case 6:
result = 8LL;
break;
case 7:
result = 9LL;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( (char)a1 )
{
case 0:
result = 1LL;
break;
case 1:
result = 3LL;
break;
case 2:
result = a1;
break;
case 3:
result = 6LL;
break;
case 4:
result = a1;
break;
case 5:
result = 12LL;
break;
case 6:
result = 8LL;
break;
case 7:
result = 9LL;
break;
default:
result = 0LL;
break;
}
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;
if ( (a4 & a5) != v6 )
{
*a2 = (v6 == 0) + 8;
a2[2] = a6;
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;
if ( (a4 & a5) != v6 )
{
*a2 = (v6 == 0) + 8;
a2[2] = a6;
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

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; }


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:
void __fastcall stats_clean(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
free(a1);
}

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)
{
unsigned __int64 result; // rax
__int64 v2; // rcx
unsigned __int64 v3; // rax

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

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

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

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_12;
type = 1;
}
else if ( result == 108 )
{
linkkey = (__int64)optarg;
}
else
{
if ( result != 109 )
{
LABEL_12:
usage();
exit(1);
}
master = (__int64)optarg;
}
}
if ( slave )
{
result = bdaddr2hex(slave, (__int64)&in);
if ( result < 0 )
{
usage();
exit(1);
}
}
if ( master )
{
result = bdaddr2hex(master, (__int64)&byte_8D6);
if ( result < 0 )
{
usage();
exit(1);
}
}
if ( linkkey )
{
result = linkkey2hex(linkkey, (__int64)&byte_8DC);
if ( result < 0 )
{
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_12;
type = 1;
}
else if ( result == 108 )
{
linkkey = (__int64)optarg;
}
else
{
if ( result != 109 )
{
LABEL_12:
func0();
exit(1);
}
master = (__int64)optarg;
}
}
if ( slave )
{
result = func6(slave, (__int64)&in);
if ( result < 0 )
{
func0();
exit(1);
}
}
if ( master )
{
result = func6(master, (__int64)&byte_8D6);
if ( result < 0 )
{
func0();
exit(1);
}
}
if ( linkkey )
{
result = func7(linkkey, (__int64)&byte_8DC);
if ( result < 0 )
{
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(_DWORD *a1, __int64 a2)
{
unsigned int result; // eax
unsigned int v3; // er12
char *v4; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h] BYREF
__int64 v7; // [rsp+18h] [rbp-20h]

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

canonical_decompiled_code:
unsigned int __fastcall func2(_DWORD *a1, __int64 a2)
{
unsigned int result; // eax
unsigned int v3; // er12
char *v4; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h] BYREF
__int64 v7; // [rsp+18h] [rbp-20h]

v6 = 0LL;
v7 = 0LL;
*(_DWORD *)((char *)&v6 + 1) = (unsigned __int8)*a1;
result = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 4 )
{
LOBYTE(v6) = 13;
if ( *(_BYTE *)(a2 + 4) )
{
*(_DWORD *)((char *)&v6 + 2) = *(_DWORD *)(a2 + 6);
HIWORD(v6) = *(_WORD *)(a2 + 10);
LODWORD(v7) = *(_DWORD *)(a2 + 12);
WORD2(v7) = *(_WORD *)(a2 + 16);
}
goto LABEL_15;
}
if ( result > 4 )
{
if ( result != 6 || gimx_params[12] )
return result;
v3 = *(unsigned __int16 *)(a2 + 6);
v4 = dcgettext(0LL, "adjust your wheel range to %u degrees\n", 5);
printf(v4, v3);
result = fflush(stdout);
}
else
{
if ( result == 2 )
{
LOBYTE(v6) = 11;
if ( *(_BYTE *)(a2 + 4) )
WORD1(v6) = *(_WORD *)(a2 + 6);
goto LABEL_15;
}
if ( result == 3 )
{
LOBYTE(v6) = 12;
if ( *(_BYTE *)(a2 + 4) )
{
*(_DWORD *)((char *)&v6 + 2) = *(_DWORD *)(a2 + 6);
HIWORD(v6) = *(_WORD *)(a2 + 10);
LODWORD(v7) = *(_DWORD *)(a2 + 12);
WORD2(v7) = *(_WORD *)(a2 + 16);
}
LABEL_15:
result = ginput_joystick_set_haptic(&v6);
if ( gimx_params[13] )
result = func1((unsigned __int8 *)&v6);
return result;
}
}
if ( *(_DWORD *)a2 == 1 )
{
v5[0] = 10LL;
v5[1] = 0LL;
*(_DWORD *)((char *)v5 + 1) = (unsigned __int8)*a1;
*(_DWORD *)((char *)v5 + 2) = *(_DWORD *)(a2 + 6);
result = ginput_queue_push(v5);
if ( gimx_params[13] )
result = func1((unsigned __int8 *)v5);
}
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; }


decompiled_code:
void __fastcall haptic_sink_os_clean(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
free(a1);
}

original_code:
static void haptic_sink_os_clean(struct haptic_sink_state * state) {
    free(state);
}

canonical_original_code:
static void haptic_sink_os_clean(struct haptic_sink_state *state) {
    free(state);
}


decompiled_code:
__int64 haptic_sink_os_constructor()
{
return haptic_sink_register(&sink_os);
}

canonical_decompiled_code:
__int64 func4()
{
return haptic_sink_register(&sink_os);
}

original_code:
void haptic_sink_os_constructor(void) {
    haptic_sink_register(&sink_os);
}

canonical_original_code:
void haptic_sink_os_constructor() {
    haptic_sink_register(&sink_os);
}


decompiled_code:
int __fastcall dump_event(unsigned __int8 *a1)
{
int result; // eax

result = *a1;
if ( (_BYTE)result == 12 )
{
if ( gimx_params[13] )
{
printf(
"< SPRING, saturation: %u %u, coefficient: %u %u, center: %d, deadband: %u\n",
*((unsigned __int16 *)a1 + 1),
*((unsigned __int16 *)a1 + 2),
(unsigned int)*((__int16 *)a1 + 3),
(unsigned int)*((__int16 *)a1 + 4),
(unsigned int)*((__int16 *)a1 + 5),
*((unsigned __int16 *)a1 + 6));
result = fflush(stdout);
}
}
else if ( (unsigned __int8)result > 0xCu )
{
if ( (_BYTE)result != 13 )
{
LABEL_15:
if ( gimx_params[13] )
{
puts("< UNKNOWN");
result = fflush(stdout);
}
return result;
}
if ( gimx_params[13] )
{
printf(
"< DAMPER, saturation: %u %u, coefficient: %d %d\n",
*((unsigned __int16 *)a1 + 1),
*((unsigned __int16 *)a1 + 2),
(unsigned int)*((__int16 *)a1 + 3),
(unsigned int)*((__int16 *)a1 + 4));
result = fflush(stdout);
}
}
else
{
if ( (_BYTE)result != 10 )
{
if ( (_BYTE)result == 11 )
{
if ( gimx_params[13] )
{
printf("< CONSTANT, level: %d\n", (unsigned int)*((__int16 *)a1 + 1));
result = fflush(stdout);
}
return result;
}
goto LABEL_15;
}
if ( gimx_params[13] )
{
printf("< RUMBLE, weak=%hu, strong=%hu\n", *((unsigned __int16 *)a1 + 1), *((unsigned __int16 *)a1 + 2));
result = fflush(stdout);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned __int8 *a1)
{
int result; // eax

result = *a1;
if ( (_BYTE)result == 12 )
{
if ( gimx_params[13] )
{
printf(
"< SPRING, saturation: %u %u, coefficient: %u %u, center: %d, deadband: %u\n",
*((unsigned __int16 *)a1 + 1),
*((unsigned __int16 *)a1 + 2),
(unsigned int)*((__int16 *)a1 + 3),
(unsigned int)*((__int16 *)a1 + 4),
(unsigned int)*((__int16 *)a1 + 5),
*((unsigned __int16 *)a1 + 6));
result = fflush(stdout);
}
}
else if ( (unsigned __int8)result > 0xCu )
{
if ( (_BYTE)result != 13 )
{
LABEL_15:
if ( gimx_params[13] )
{
puts("< UNKNOWN");
result = fflush(stdout);
}
return result;
}
if ( gimx_params[13] )
{
printf(
"< DAMPER, saturation: %u %u, coefficient: %d %d\n",
*((unsigned __int16 *)a1 + 1),
*((unsigned __int16 *)a1 + 2),
(unsigned int)*((__int16 *)a1 + 3),
(unsigned int)*((__int16 *)a1 + 4));
result = fflush(stdout);
}
}
else
{
if ( (_BYTE)result != 10 )
{
if ( (_BYTE)result == 11 )
{
if ( gimx_params[13] )
{
printf("< CONSTANT, level: %d\n", (unsigned int)*((__int16 *)a1 + 1));
result = fflush(stdout);
}
return result;
}
goto LABEL_15;
}
if ( gimx_params[13] )
{
printf("< RUMBLE, weak=%hu, strong=%hu\n", *((unsigned __int16 *)a1 + 1), *((unsigned __int16 *)a1 + 2));
result = fflush(stdout);
}
}
return result;
}

original_code:
static void dump_event(const GE_Event * event) {
    switch (event->type) {
    case GE_JOYRUMBLE:
        do { if(gimx_params.debug.haptic) { printf("< RUMBLE, weak=%hu, strong=%hu\n", event->jrumble.weak, event->jrumble.strong); fflush(stdout); } } while (0);
        break;
    case GE_JOYCONSTANTFORCE:
        do { if(gimx_params.debug.haptic) { printf("< CONSTANT, level: %d\n", event->jconstant.level); fflush(stdout); } } while (0);
        break;
    case GE_JOYSPRINGFORCE:
        do { if(gimx_params.debug.haptic) { printf("< SPRING, saturation: %u %u, coefficient: %u %u, center: %d, deadband: %u\n", event->jcondition.saturation.left, event->jcondition.saturation.right, event->jcondition.coefficient.left, event->jcondition.coefficient.right, event->jcondition.center, event->jcondition.deadband); fflush(stdout); } } while (0);
        break;
    case GE_JOYDAMPERFORCE:
        do { if(gimx_params.debug.haptic) { printf("< DAMPER, saturation: %u %u, coefficient: %d %d\n", event->jcondition.saturation.left, event->jcondition.saturation.right, event->jcondition.coefficient.left, event->jcondition.coefficient.right); fflush(stdout); } } while (0);
        break;
    default:
        do { if(gimx_params.debug.haptic) { printf("< UNKNOWN\n"); fflush(stdout); } } while (0);
        break;
    }
}

canonical_original_code:
static void dump_event(union GE_Event *event) {
    switch (event->type) {
    case GE_JOYRUMBLE:
        do { if(gimx_params.debug.haptic) { printf("< RUMBLE, weak=%hu, strong=%hu\n", event->jrumble.weak, event->jrumble.strong); fflush(stdout); } } while (0);
        break;
    case GE_JOYCONSTANTFORCE:
        do { if(gimx_params.debug.haptic) { printf("< CONSTANT, level: %d\n", event->jconstant.level); fflush(stdout); } } while (0);
        break;
    case GE_JOYSPRINGFORCE:
        do { if(gimx_params.debug.haptic) { printf("< SPRING, saturation: %u %u, coefficient: %u %u, center: %d, deadband: %u\n", event->jcondition.saturation.left, event->jcondition.saturation.right, event->jcondition.coefficient.left, event->jcondition.coefficient.right, event->jcondition.center, event->jcondition.deadband); fflush(stdout); } } while (0);
        break;
    case GE_JOYDAMPERFORCE:
        do { if(gimx_params.debug.haptic) { printf("< DAMPER, saturation: %u %u, coefficient: %d %d\n", event->jcondition.saturation.left, event->jcondition.saturation.right, event->jcondition.coefficient.left, event->jcondition.coefficient.right); fflush(stdout); } } while (0);
        break;
    default:
        do { if(gimx_params.debug.haptic) { printf("< UNKNOWN\n"); fflush(stdout); } } while (0);
        break;
    }
}

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; } *


decompiled_code:
__int64 bt_device_btstack_device_init()
{
return btstack_common_init();
}

canonical_decompiled_code:
__int64 func0()
{
return btstack_common_init();
}

original_code:
int bt_device_btstack_device_init()
{
  return btstack_common_init();
}


decompiled_code:
__int64 __fastcall bt_device_btstack_get_device_bdaddr(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 i; // rdx

result = btstack_common_send_cmd(&btstack_hci_read_bd_addr);
if ( (int)result >= 0 )
{
do
{
while ( 1 )
{
result = btstack_common_recv_packet(&recv_data);
if ( (int)result > 0 )
break;
if ( (_DWORD)result )
return result;
}
}
while ( recv_data != 4 || byte_146 != 14 || (unsigned __int16)word_149 != btstack_hci_read_bd_addr );
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a2 + i) = byte_14C[i];
}
return result;
}

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

result = btstack_common_send_cmd(&btstack_hci_read_bd_addr);
if ( (int)result >= 0 )
{
do
{
while ( 1 )
{
result = btstack_common_recv_packet(&recv_data);
if ( (int)result > 0 )
break;
if ( (_DWORD)result )
return result;
}
}
while ( recv_data != 4 || byte_146 != 14 || (unsigned __int16)word_149 != btstack_hci_read_bd_addr );
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a2 + i) = byte_14C[i];
}
return result;
}

original_code:
int bt_device_btstack_get_device_bdaddr(int ignored __attribute__((unused)), bdaddr_t* bdaddr)
{
  int ret;
  ret = btstack_common_send_cmd(&btstack_hci_read_bd_addr);
  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_read_bd_addr.opcode))
        {
          unsigned int i;
          for(i=0; i<sizeof(bdaddr->b); ++i)
          {
            bdaddr->b[i] = packet[5 + 1 + i];
          }
          break;
        }
      }
    }
  }
  return ret;
}

canonical_original_code:
int bt_device_btstack_get_device_bdaddr(int ignored, struct bdaddr_t *bdaddr)
{
  int ret;
  ret = btstack_common_send_cmd(&btstack_hci_read_bd_addr);
  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_read_bd_addr.opcode))
        {
          unsigned int i;
          for(i=0; i<sizeof(bdaddr->b); ++i)
          {
            bdaddr->b[i] = packet[5 + 1 + i];
          }
          break;
        }
      }
    }
  }
  return ret;
}

variable_types: ignored int

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

variable_types: type int

variable_types:
packet
unsigned char *

variable_types:
i
unsigned int


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

result = btstack_common_send_cmd(&btstack_hci_write_class_of_device);
if ( (int)result >= 0 )
{
do
{
while ( 1 )
{
result = btstack_common_recv_packet(&recv_data);
if ( (int)result > 0 )
break;
if ( (_DWORD)result )
return result;
}
}
while ( recv_data != 4 || byte_146 != 14 || (unsigned __int16)word_149 != btstack_hci_write_class_of_device );
}
return result;
}

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

result = btstack_common_send_cmd(&btstack_hci_write_class_of_device);
if ( (int)result >= 0 )
{
do
{
while ( 1 )
{
result = btstack_common_recv_packet(&recv_data);
if ( (int)result > 0 )
break;
if ( (_DWORD)result )
return result;
}
}
while ( recv_data != 4 || byte_146 != 14 || (unsigned __int16)word_149 != btstack_hci_write_class_of_device );
}
return result;
}

original_code:
int bt_device_btstack_write_device_class(int ignored __attribute__((unused)), uint32_t devclass)
{
  int ret = btstack_common_send_cmd(&btstack_hci_write_class_of_device, devclass);
  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_write_class_of_device.opcode) ) {
          break;
        }
      }
    }
  }
  return ret;
}

canonical_original_code:
int bt_device_btstack_write_device_class(int ignored, unsigned int devclass)
{
  int ret = btstack_common_send_cmd(&btstack_hci_write_class_of_device, devclass);
  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_write_class_of_device.opcode) ) {
          break;
        }
      }
    }
  }
  return ret;
}

variable_types:
devclass
unsigned int

variable_types: ret int


decompiled_code:
__int64 bt_device_btstack_init()
{
return bt_device_abs_register(0LL, &bt_device_btstack);
}

canonical_decompiled_code:
__int64 func3()
{
return bt_device_abs_register(0LL, &bt_device_btstack);
}

original_code:
void bt_device_btstack_init(void)
{
  bt_device_abs_register(E_BT_ABS_BTSTACK, &bt_device_btstack);
}

canonical_original_code:
void bt_device_btstack_init()
{
  bt_device_abs_register(E_BT_ABS_BTSTACK, &bt_device_btstack);
}


decompiled_code:
__int64 __fastcall l2cap_btstack_add_source(int a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
__int64 v5; // rdx

result = 8LL * a1;
v5 = 7LL * a1;
qword_A08[v5] = a2;
qword_A30[v5] = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
__int64 v5; // rdx

result = 8LL * a1;
v5 = 7LL * a1;
qword_A08[v5] = a2;
qword_A30[v5] = a4;
return result;
}

original_code:
static void l2cap_btstack_add_source(int channel, void * user,
    L2CAP_ABS_READ_CALLBACK read_callback __attribute__((unused)), L2CAP_ABS_PACKET_CALLBACK packet_callback, L2CAP_ABS_CLOSE_CALLBACK close_callback __attribute__((unused)))
{
  channels.entries[channel].user = user;
  channels.entries[channel].packet_callback = packet_callback;
}

canonical_original_code:
static void l2cap_btstack_add_source(int channel, void *user, int (*read_callback)(void *user), int (*packet_callback)(void *user, int psm, unsigned char *buf, int len), int (*close_callback)(void *user))
{
  channels.entries[channel].user = user;
  channels.entries[channel].packet_callback = packet_callback;
}

variable_types: channel int

variable_types:
user
void *

variable_types: int (*)(void *user, int psm, unsigned char *buf, int len)


decompiled_code:
__int64 l2cap_btstack_close()
{
fwrite("btstack_close is not implemented yet\n", 1uLL, 0x25uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
fwrite("btstack_close is not implemented yet\n", 1uLL, 0x25uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
static int l2cap_btstack_close(int channel __attribute__((unused)))
{
  fprintf(stderr, "btstack_close is not implemented yet\n");
  return -1;
}

canonical_original_code:
static int l2cap_btstack_close(int channel)
{
  fprintf(stderr, "btstack_close is not implemented yet\n");
  return -1;
}


decompiled_code:
__int64 l2cap_btstack_recv()
{
fwrite("btstack uses packet callbacks!\n", 1uLL, 0x1FuLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func3()
{
fwrite("btstack uses packet callbacks!\n", 1uLL, 0x1FuLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
static int l2cap_btstack_recv (int channel __attribute__((unused)), unsigned char* buf __attribute__((unused)), int len __attribute__((unused)))
{
  fprintf(stderr, "btstack uses packet callbacks!\n");
  return -1;
}

canonical_original_code:
static int l2cap_btstack_recv(int channel, unsigned char *buf, int len)
{
  fprintf(stderr, "btstack uses packet callbacks!\n");
  return -1;
}

variable_types:
buf
unsigned char *

variable_types: len int