decompiled_code:
__int64 __fastcall remove_device(char *s2)
{
const char **v1; // rbx
void **v2; // r13
const char **v3; // rbp
int v4; // eax

v1 = (const char **)device_list;
if ( !device_list )
return 0LL;
v2 = &device_list;
while ( 1 )
{
v3 = v1;
v4 = strcmp(v1[1], s2);
v1 = (const char **)v1[35];
if ( !v4 )
break;
v2 = (void **)(v3 + 35);
if ( !v1 )
return 0LL;
}
*v2 = v1;
close(*(_DWORD *)v3);
free((void *)v3[1]);
free(v3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s2)
{
const char **v1; // rbx
void **v2; // r13
const char **v3; // rbp
int v4; // eax

v1 = (const char **)device_list;
if ( !device_list )
return 0LL;
v2 = &device_list;
while ( 1 )
{
v3 = v1;
v4 = strcmp(v1[1], s2);
v1 = (const char **)v1[35];
if ( !v4 )
break;
v2 = (void **)(v3 + 35);
if ( !v1 )
return 0LL;
}
*v2 = v1;
close(*(_DWORD *)v3);
free((void *)v3[1]);
free(v3);
return 1LL;
}

original_code:
int remove_device(char *dev) {
 device **p = &device_list;
 while (*p != ((void *)0)) {
  if ( strcmp( (*p)->devname, dev ) == 0 ) {
   device *copy = *p;
   *p = copy->next;
   close(copy->fd);
   free(copy->devname);
   free(copy);
   return 1;
  }
  p = &( (*p)->next );
 }
 return 0;
}

canonical_original_code:
int remove_device(char *dev) {
 struct device **p = &device_list;
 while (*p != ((void *)0)) {
  if ( strcmp( (*p)->devname, dev ) == 0 ) {
   struct device *copy = *p;
   *p = copy->next;
   close(copy->fd);
   free(copy->devname);
   free(copy);
   return 1;
  }
  p = &( (*p)->next );
 }
 return 0;
}

variable_types: struct device { int fd; char *devname; int exclusive; char tag[256]; struct device *next; } *


repo: wertarbyte/triggerhappy

decompiled_code:
void clear_devices()
{
_QWORD *v0; // rbx
void *v1; // rbp

v0 = device_list;
if ( device_list )
{
do
{
v1 = v0;
v0 = (_QWORD *)v0[35];
close(*(_DWORD *)v1);
free(*((void **)v1 + 1));
free(v1);
}
while ( v0 );
}
device_list = 0LL;
}

canonical_decompiled_code:
void func5()
{
_QWORD *v0; // rbx
void *v1; // rbp

v0 = device_list;
if ( device_list )
{
do
{
v1 = v0;
v0 = (_QWORD *)v0[35];
close(*(_DWORD *)v1);
free(*((void **)v1 + 1));
free(v1);
}
while ( v0 );
}
device_list = 0LL;
}

original_code:
void clear_devices(void) {
 device *p = device_list;
 while (p != ((void *)0)) {
  device *next = p->next;
  close(p->fd);
  free(p->devname);
  free(p);
  p = next;
 }
 device_list = ((void *)0);
}

canonical_original_code:
void clear_devices() {
 struct device *p = device_list;
 while (p != ((void *)0)) {
  struct device *next = p->next;
  close(p->fd);
  free(p->devname);
  free(p);
  p = next;
 }
 device_list = ((void *)0);
}

return_type: void

user_defined_types: struct device { int fd; char *devname; int exclusive; char tag[256]; struct device *next; }


decompiled_code:
__int64 count_devices()
{
_QWORD *v0; // rax
unsigned int v1; // er8

v0 = device_list;
v1 = 0;
if ( device_list )
{
do
{
v0 = (_QWORD *)v0[35];
++v1;
}
while ( v0 );
}
return v1;
}

canonical_decompiled_code:
__int64 func6()
{
_QWORD *v0; // rax
unsigned int v1; // er8

v0 = device_list;
v1 = 0;
if ( device_list )
{
do
{
v0 = (_QWORD *)v0[35];
++v1;
}
while ( v0 );
}
return v1;
}

original_code:
int count_devices(void) {
 int n = 0;
 device *p = device_list;
 while (p != ((void *)0)) {
  n++;
  p = p->next;
 }
 return n;
}

canonical_original_code:
int count_devices() {
 int n = 0;
 struct device *p = device_list;
 while (p != ((void *)0)) {
  n++;
  p = p->next;
 }
 return n;
}

variable_types: n int

return_type: int


decompiled_code:
__int64 __fastcall for_each_device(__int64 (__fastcall *a1)(_QWORD *))
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = device_list;
if ( device_list )
{
do
{
result = a1(v1);
v1 = (_QWORD *)v1[35];
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 (__fastcall *a1)(_QWORD *))
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = device_list;
if ( device_list )
{
do
{
result = a1(v1);
v1 = (_QWORD *)v1[35];
}
while ( v1 );
}
return result;
}

original_code:
void for_each_device( void(*func)(device*) ) {
 device *p = device_list;
 while (p != ((void *)0)) {
  func( p );
  p = p->next;
 }
}

canonical_original_code:
void for_each_device(void (*func)(struct device *)) {
 struct device *p = device_list;
 while (p != ((void *)0)) {
  func( p );
  p = p->next;
 }
}

variable_types: void (*)(struct device { int fd; char *devname; int exclusive; char tag[256]; struct device *next; } *)


decompiled_code:
size_t __fastcall init_keystate_holder(_QWORD **a1)
{
_QWORD *v1; // rax
void *v2; // rdi
unsigned int v3; // ecx
size_t result; // rax

v1 = *a1;
if ( !*a1 )
{
v1 = malloc(0x600uLL);
*a1 = v1;
if ( !v1 )
return fwrite("Unable to allocate memory for keystate table!\n", 1uLL, 0x2EuLL, stderr);
}
*v1 = 0LL;
v1[191] = 0LL;
v2 = (void *)((unsigned __int64)(v1 + 1) & 0xFFFFFFFFFFFFFFF8LL);
v3 = (_DWORD)v1 - (_DWORD)v2 + 1536;
result = 0LL;
memset(v2, 0, 8LL * (v3 >> 3));
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(_QWORD **a1)
{
_QWORD *v1; // rax
void *v2; // rdi
unsigned int v3; // ecx
size_t result; // rax

v1 = *a1;
if ( !*a1 )
{
v1 = malloc(0x600uLL);
*a1 = v1;
if ( !v1 )
return fwrite("Unable to allocate memory for keystate table!\n", 1uLL, 0x2EuLL, stderr);
}
*v1 = 0LL;
v1[191] = 0LL;
v2 = (void *)((unsigned __int64)(v1 + 1) & 0xFFFFFFFFFFFFFFF8LL);
v3 = (_DWORD)v1 - (_DWORD)v2 + 1536;
result = 0LL;
memset(v2, 0, 8LL * (v3 >> 3));
return result;
}

original_code:
void init_keystate_holder(keystate_holder **ksh) {
 if (*ksh == ((void *)0)) {
  *ksh = malloc(sizeof(keystate_holder));
 }
 if (*ksh == ((void *)0)) {
  fprintf(stderr, "Unable to allocate memory for keystate table!\n");
  return;
 }
 int i;
 for (i=0; i<=0x2ff; i++) {
  (**ksh)[i] = 0;
 }
}

canonical_original_code:
void init_keystate_holder(unsigned short (**ksh)[-1]) {
 if (*ksh == ((void *)0)) {
  *ksh = malloc(sizeof(keystate_holder));
 }
 if (*ksh == ((void *)0)) {
  fprintf(stderr, "Unable to allocate memory for keystate table!\n");
  return;
 }
 int i;
 for (i=0; i<=0x2ff; i++) {
  (**ksh)[i] = 0;
 }
}

variable_types:
ksh
unsigned short (**)[-1]

variable_types: i int


decompiled_code:
void *__fastcall get_keystate_ignore_key(__int64 a1, int a2)
{
__int64 v2; // r15
int v4; // ebx
__int64 v5; // rsi
size_t v6; // rax
size_t v7; // r13
void *v8; // rax
void *v9; // r8
char v11[1032]; // [rsp+0h] [rbp-440h] BYREF
char *src; // [rsp+408h] [rbp-38h]

v2 = 0LL;
v4 = 0;
v11[0] = 0;
do
{
v5 = (unsigned int)v2;
if ( a2 != (_DWORD)v2 && *(_WORD *)(a1 + 2 * v2) )
{
if ( v4 )
{
v5 = (unsigned int)v2;
*(_WORD *)&v11[strlen(v11)] = 43;
}
++v4;
src = (char *)lookup_event_name_i(1LL, v5);
v6 = strlen(v11);
strncat(v11, src, 1023 - v6);
}
++v2;
}
while ( v2 != 768 );
v7 = strlen(v11) + 1;
v8 = malloc(v7);
v9 = v8;
if ( v8 )
v9 = memcpy(v8, v11, v7);
return v9;
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1, int a2)
{
__int64 v2; // r15
int v4; // ebx
__int64 v5; // rsi
size_t v6; // rax
size_t v7; // r13
void *v8; // rax
void *v9; // r8
char v11[1032]; // [rsp+0h] [rbp-440h] BYREF
char *src; // [rsp+408h] [rbp-38h]

v2 = 0LL;
v4 = 0;
v11[0] = 0;
do
{
v5 = (unsigned int)v2;
if ( a2 != (_DWORD)v2 && *(_WORD *)(a1 + 2 * v2) )
{
if ( v4 )
{
v5 = (unsigned int)v2;
*(_WORD *)&v11[strlen(v11)] = 43;
}
++v4;
src = (char *)lookup_event_name_i(1LL, v5);
v6 = strlen(v11);
strncat(v11, src, 1023 - v6);
}
++v2;
}
while ( v2 != 768 );
v7 = strlen(v11) + 1;
v8 = malloc(v7);
v9 = v8;
if ( v8 )
v9 = memcpy(v8, v11, v7);
return v9;
}

original_code:
char *get_keystate_ignore_key(keystate_holder ksh, int ignore_key) {
 const int bsize = 1024;
 char *KS = "+";
 char buf[bsize];
 buf[0] = '\0';
 int i;
 int n = 0;
 for (i=0; i<=0x2ff; i++) {
  if (i != ignore_key && ksh[i] > 0) {
   if (n>0) {
    strncat( buf, KS, bsize-1-strlen(KS) );
   }
   const char *name = lookup_event_name_i(0x01, i);
   strncat( &(buf[0]), name, bsize-1-strlen(buf) );
   n++;
  }
 }
 char *res = malloc( sizeof(char) * (1+strlen(buf)) );
 if (res) {
  strcpy( res, buf );
 }
 return res;
}

canonical_original_code:
char *get_keystate_ignore_key(unsigned short ksh[-1], int ignore_key) {
 int bsize = 1024;
 char *KS = "+";
 char buf[-1];
 buf[0] = '\0';
 int i;
 int n = 0;
 for (i=0; i<=0x2ff; i++) {
  if (i != ignore_key && ksh[i] > 0) {
   if (n>0) {
    strncat( buf, KS, bsize-1-strlen(KS) );
   }
   char *name = lookup_event_name_i(0x01, i);
   strncat( &(buf[0]), name, bsize-1-strlen(buf) );
   n++;
  }
 }
 char *res = malloc( sizeof(char) * (1+strlen(buf)) );
 if (res) {
  strcpy( res, buf );
 }
 return res;
}

variable_types:
ksh
unsigned short [-1]

variable_types: bsize int

variable_types:
KS
char *

variable_types:
buf
char [-1]

variable_types:
name
char *

variable_types:
res
char *

return_type: char *


decompiled_code:
void *__fastcall get_keystate(__int64 a1)
{
return get_keystate_ignore_key(a1, -1);
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1)
{
return func2(a1, -1);
}

original_code:
char *get_keystate(keystate_holder ksh) {
 return get_keystate_ignore_key( ksh, -1 );
}

canonical_original_code:
char *get_keystate(unsigned short ksh[-1]) {
 return get_keystate_ignore_key( ksh, -1 );
}


decompiled_code:
__int64 __fastcall obey_command(int *a1, __int64 a2, __int64 a3, __int64 a4)
{
int v4; // eax
unsigned int v5; // er8
__int64 result; // rax

v4 = *a1;
if ( *a1 == 1 )
{
add_device(a1 + 1, 0xFFFFFFFFLL, (unsigned int)a1[66], a1 + 67);
result = 0LL;
}
else if ( v4 == 6 )
{
add_device(a1 + 1, (unsigned int)a1[65], (unsigned int)a1[66], a1 + 67);
result = 0LL;
}
else
{
switch ( v4 )
{
case 2:
remove_device(a1 + 1);
v5 = 0;
break;
case 3:
cleanup();
v5 = 0;
break;
case 5:
triggers_enabled(1LL);
v5 = 0;
break;
case 4:
triggers_enabled(0LL);
v5 = 0;
break;
case 7:
clear_devices();
v5 = 0;
break;
default:
v5 = 1;
if ( v4 == 8 )
{
change_trigger_mode(a1 + 1, a2, a3, a4, 1LL);
v5 = 0;
}
break;
}
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, __int64 a2, __int64 a3, __int64 a4)
{
int v4; // eax
unsigned int v5; // er8
__int64 result; // rax

v4 = *a1;
if ( *a1 == 1 )
{
add_device(a1 + 1, 0xFFFFFFFFLL, (unsigned int)a1[66], a1 + 67);
result = 0LL;
}
else if ( v4 == 6 )
{
add_device(a1 + 1, (unsigned int)a1[65], (unsigned int)a1[66], a1 + 67);
result = 0LL;
}
else
{
switch ( v4 )
{
case 2:
remove_device(a1 + 1);
v5 = 0;
break;
case 3:
cleanup();
v5 = 0;
break;
case 5:
triggers_enabled(1LL);
v5 = 0;
break;
case 4:
triggers_enabled(0LL);
v5 = 0;
break;
case 7:
clear_devices();
v5 = 0;
break;
default:
v5 = 1;
if ( v4 == 8 )
{
change_trigger_mode(a1 + 1, a2, a3, a4, 1LL);
v5 = 0;
}
break;
}
result = v5;
}
return result;
}

original_code:
int obey_command( struct command *cmd ) {
 if (cmd->type == CMD_ADD) {
  add_device( cmd->param, -1, cmd->exclusive, cmd->tag );
  return 0;
 }
 if (cmd->type == CMD_PASSFD) {
  add_device( cmd->param, cmd->fd, cmd->exclusive, cmd->tag );
  return 0;
 }
 if (cmd->type == CMD_REMOVE) {
  remove_device( cmd->param );
  return 0;
 }
 if (cmd->type == CMD_QUIT) {
  cleanup();
  return 0;
 }
 if (cmd->type == CMD_ENABLE) {
  triggers_enabled(1);
  return 0;
 }
 if (cmd->type == CMD_DISABLE) {
  triggers_enabled(0);
  return 0;
 }
 if (cmd->type == CMD_CLEARDEVS) {
  clear_devices();
  return 0;
 }
 if (cmd->type == CMD_CHANGEMODE) {
  change_trigger_mode( cmd->param );
  return 0;
 }
 return 1;
}

canonical_original_code:
int obey_command(struct command *cmd) {
 if (cmd->type == CMD_ADD) {
  add_device( cmd->param, -1, cmd->exclusive, cmd->tag );
  return 0;
 }
 if (cmd->type == CMD_PASSFD) {
  add_device( cmd->param, cmd->fd, cmd->exclusive, cmd->tag );
  return 0;
 }
 if (cmd->type == CMD_REMOVE) {
  remove_device( cmd->param );
  return 0;
 }
 if (cmd->type == CMD_QUIT) {
  cleanup();
  return 0;
 }
 if (cmd->type == CMD_ENABLE) {
  triggers_enabled(1);
  return 0;
 }
 if (cmd->type == CMD_DISABLE) {
  triggers_enabled(0);
  return 0;
 }
 if (cmd->type == CMD_CLEARDEVS) {
  clear_devices();
  return 0;
 }
 if (cmd->type == CMD_CHANGEMODE) {
  change_trigger_mode( cmd->param );
  return 0;
 }
 return 1;
}

variable_types: struct command { enum command_type { CMD_NOP=0, CMD_ADD=1, CMD_REMOVE=2, CMD_QUIT=3, CMD_DISABLE=4, CMD_ENABLE=5, CMD_PASSFD=6, CMD_CLEARDEVS=7, CMD_CHANGEMODE=8 } type; char param[256]; int fd; int exclusive; char tag[256]; } *

user_defined_types: struct command { enum command_type type; char param[256]; int fd; int exclusive; char tag[256]; }


decompiled_code:
size_t __fastcall ignore_key(int a1, size_t *a2)
{
size_t v2; // rax
size_t v3; // rsi
size_t *v4; // rbx
_QWORD *v5; // rax
size_t result; // rax

v2 = *a2;
if ( *a2 )
{
do
{
v3 = v2;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
v4 = (size_t *)(v3 + 8);
}
else
{
v4 = a2;
}
v5 = malloc(0x10uLL);
*v4 = (size_t)v5;
if ( !v5 )
return fwrite("Unable to allocate memory for ignored key!\n", 1uLL, 0x2BuLL, stderr);
v5[1] = 0LL;
result = *v4;
*(_DWORD *)*v4 = a1;
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(int a1, size_t *a2)
{
size_t v2; // rax
size_t v3; // rsi
size_t *v4; // rbx
_QWORD *v5; // rax
size_t result; // rax

v2 = *a2;
if ( *a2 )
{
do
{
v3 = v2;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
v4 = (size_t *)(v3 + 8);
}
else
{
v4 = a2;
}
v5 = malloc(0x10uLL);
*v4 = (size_t)v5;
if ( !v5 )
return fwrite("Unable to allocate memory for ignored key!\n", 1uLL, 0x2BuLL, stderr);
v5[1] = 0LL;
result = *v4;
*(_DWORD *)*v4 = a1;
return result;
}

original_code:
void ignore_key( int code, ignore **list ) {
 ignore **p = list;
 while ( *p != ((void *)0) ) {
  p = &( (*p)->next );
 }
 *p = malloc( sizeof(ignore) );
 if (*p) {
  (*p)->next = ((void *)0);
  (*p)->code = code;
 } else {
  fprintf(stderr, "Unable to allocate memory for ignored key!\n");
 }
}

canonical_original_code:
void ignore_key(int code, struct ignore **list) {
 struct ignore **p = list;
 while ( *p != ((void *)0) ) {
  p = &( (*p)->next );
 }
 *p = malloc( sizeof(ignore) );
 if (*p) {
  (*p)->next = ((void *)0);
  (*p)->code = code;
 } else {
  fprintf(stderr, "Unable to allocate memory for ignored key!\n");
 }
}

variable_types: code int

variable_types: struct ignore { int code; struct ignore *next; } **


decompiled_code:
void __fastcall print_ignores(__int64 a1)
{
while ( a1 )
a1 = *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
while ( a1 )
a1 = *(_QWORD *)(a1 + 8);
}

original_code:
void print_ignores( ignore *list ) {
 ignore *p = list;
 while ( p != ((void *)0) ) {
  p = p->next;
 }
}

canonical_original_code:
void print_ignores(struct ignore *list) {
 struct ignore *p = list;
 while ( p != ((void *)0) ) {
  p = p->next;
 }
}

variable_types: struct ignore { int code; struct ignore *next; } *


decompiled_code:
__int64 __fastcall is_ignored(int a1, __int64 a2)
{
while ( 1 )
{
if ( !a2 )
return 0LL;
if ( *(_DWORD *)a2 == a1 )
break;
a2 = *(_QWORD *)(a2 + 8);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
while ( 1 )
{
if ( !a2 )
return 0LL;
if ( *(_DWORD *)a2 == a1 )
break;
a2 = *(_QWORD *)(a2 + 8);
}
return 1LL;
}

original_code:
int is_ignored( int code, ignore *list ) {
 ignore *p = list;
 while ( p != ((void *)0) ) {
  if (p->code == code) {
   return 1;
  }
  p = p->next;
 }
 return 0;
}

canonical_original_code:
int is_ignored(int code, struct ignore *list) {
 struct ignore *p = list;
 while ( p != ((void *)0) ) {
  if (p->code == code) {
   return 1;
  }
  p = p->next;
 }
 return 0;
}

user_defined_types: struct ignore { int code; struct ignore *next; }


decompiled_code:
int close_uinput()
{
int result; // eax

if ( uinput_fd >= 0 )
{
result = close(uinput_fd);
uinput_fd = -1;
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

if ( uinput_fd >= 0 )
{
result = close(uinput_fd);
uinput_fd = -1;
}
return result;
}

original_code:
void close_uinput() {
 if (uinput_fd >= 0) {
  close(uinput_fd);
  uinput_fd = -1;
 }
}


repo: wesen/avr-bt-stack

original_code:
bt_peer_t *bt_dev_get_free_peer(bt_dev_t *dev) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if (dev->peers[i].state == 0) {
         dev->peers[i].pkt_type = 0x0008;
         dev->peers[i].clock_offset = 0;
         memset(dev->peers[i].bd_addr, 0, 6);
         memset(dev->peers[i].cod, 0, 3);
         return dev->peers + i;
      }
   }
   return ((void *)0);
}

canonical_original_code:
struct bt_peer_s *bt_dev_get_free_peer(struct bt_dev_s *dev) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if (dev->peers[i].state == 0) {
         dev->peers[i].pkt_type = 0x0008;
         dev->peers[i].clock_offset = 0;
         memset(dev->peers[i].bd_addr, 0, 6);
         memset(dev->peers[i].cod, 0, 3);
         return dev->peers + i;
      }
   }
   return ((void *)0);
}

variable_types: struct bt_dev_s { unsigned char state; unsigned char ncmds; unsigned char buf[256]; char name[32]; unsigned short pkt_type; struct bt_peer_s peers[3]; struct bt_acl_s { unsigned char state; unsigned short handle; unsigned short pkt_types; unsigned char max_slots; unsigned char encrypt_mode; struct bt_peer_s *peer; } acls[3]; char tty[128]; int ttyfd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } save_termios; } *

variable_types:
i
unsigned int

return_type: struct bt_peer_s { unsigned char state; unsigned char bd_addr[6]; unsigned short clock_offset; unsigned char pscan_rep_mode; } *

user_defined_types: struct bt_dev_s { unsigned char state; unsigned char ncmds; unsigned char buf[256]; char name[32]; unsigned short pkt_type; struct bt_peer_s peers[3]; struct bt_acl_s acls[3]; char tty[128]; int ttyfd; struct termios save_termios; }


original_code:
bt_peer_t *bt_dev_get_peer_by_bd_addr(bt_dev_t *dev, unsigned char *bd_addr){
   unsigned int i;
   for (i=0; i<3; i++) {
      if ((dev->peers[i].state != 0) &&
          !(memcmp(dev->peers[i].bd_addr, bd_addr, 6)))
         return dev->peers + i;
   }
   return ((void *)0);
}

canonical_original_code:
struct bt_peer_s *bt_dev_get_peer_by_bd_addr(struct bt_dev_s *dev, unsigned char *bd_addr){
   unsigned int i;
   for (i=0; i<3; i++) {
      if ((dev->peers[i].state != 0) &&
          !(memcmp(dev->peers[i].bd_addr, bd_addr, 6)))
         return dev->peers + i;
   }
   return ((void *)0);
}

user_defined_types: struct bt_peer_s { unsigned char state; unsigned char bd_addr[6]; unsigned short clock_offset; unsigned char pscan_rep_mode; }

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

user_defined_types: struct bt_acl_s { unsigned char state; unsigned short handle; unsigned short pkt_types; unsigned char max_slots; unsigned char encrypt_mode; struct bt_peer_s *peer; }


decompiled_code:
__int64 __fastcall bt_dev_get_free_acl(_BYTE *a1)
{
__int64 result; // rax
__int64 v2; // rax

if ( a1[360] )
{
if ( a1[376] )
{
result = 0LL;
if ( a1[392] )
return result;
v2 = 32LL;
}
else
{
v2 = 16LL;
}
}
else
{
v2 = 0LL;
}
return (__int64)&a1[v2 + 360];
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1)
{
__int64 result; // rax
__int64 v2; // rax

if ( a1[360] )
{
if ( a1[376] )
{
result = 0LL;
if ( a1[392] )
return result;
v2 = 32LL;
}
else
{
v2 = 16LL;
}
}
else
{
v2 = 0LL;
}
return (__int64)&a1[v2 + 360];
}

original_code:
bt_acl_t *bt_dev_get_free_acl(bt_dev_t *dev) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if (dev->acls[i].state == 0)
         return dev->acls + i;
   }
   return ((void *)0);
}

canonical_original_code:
struct bt_acl_s *bt_dev_get_free_acl(struct bt_dev_s *dev) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if (dev->acls[i].state == 0)
         return dev->acls + i;
   }
   return ((void *)0);
}

variable_types: struct bt_dev_s { unsigned char state; unsigned char ncmds; unsigned char buf[256]; char name[32]; unsigned short pkt_type; struct bt_peer_s peers[3]; struct bt_acl_s acls[3]; char tty[128]; int ttyfd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } save_termios; } *

return_type: struct bt_acl_s { unsigned char state; unsigned short handle; unsigned short pkt_types; unsigned char max_slots; unsigned char encrypt_mode; struct bt_peer_s { unsigned char state; unsigned char bd_addr[6]; unsigned short clock_offset; unsigned char pscan_rep_mode; } *peer; } *


original_code:
bt_acl_t *bt_dev_get_acl_by_handle(bt_dev_t *dev,
                                   unsigned short handle) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if ((dev->acls[i].state != 0) &&
            (dev->acls[i].handle == handle))
         return dev->acls + i;
   }
   return ((void *)0);
}

canonical_original_code:
struct bt_acl_s *bt_dev_get_acl_by_handle(struct bt_dev_s *dev, unsigned short handle) {
   unsigned int i;
   for (i=0; i<3; i++) {
      if ((dev->acls[i].state != 0) &&
            (dev->acls[i].handle == handle))
         return dev->acls + i;
   }
   return ((void *)0);
}

variable_types:
handle
unsigned short


decompiled_code:
__int64 __fastcall bt_dev_flush_hci(__int64 a1)
{
--*(_BYTE *)(a1 + 1);
return bt_serial_send(a1, a1 + 2, *(_QWORD *)(a1 + 264) - (a1 + 2));
}

original_code:
int bt_dev_flush_hci(bt_dev_t *dev) {
   dev->ncmds--;
   return bt_serial_send(dev, dev->buf, dev->ptr - dev->buf);
}

canonical_original_code:
int bt_dev_flush_hci(struct bt_dev_s *dev) {
   dev->ncmds--;
   return bt_serial_send(dev, dev->buf, dev->ptr - dev->buf);
}

variable_types: struct bt_dev_s { unsigned char state; unsigned char ncmds; unsigned char buf[256]; char name[32]; unsigned short pkt_type; struct bt_peer_s { unsigned char state; unsigned char bd_addr[6]; unsigned short clock_offset; unsigned char pscan_rep_mode; } peers[3]; struct bt_acl_s { unsigned char state; unsigned short handle; unsigned short pkt_types; unsigned char max_slots; unsigned char encrypt_mode; struct bt_peer_s *peer; } acls[3]; char tty[128]; int ttyfd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } save_termios; } *


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

*(_QWORD *)(a1 + 264) = a1 + 2;
if ( (unsigned int)bt_serial_read(a1, a1 + 2, 1LL) == 1 )
result = bt_hci_unpack_hci(a1, a2);
else
result = 1LL;
return result;
}

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

*(_QWORD *)(a1 + 264) = a1 + 2;
if ( (unsigned int)bt_serial_read(a1, a1 + 2, 1LL) == 1 )
result = bt_hci_unpack_hci(a1, a2);
else
result = 1LL;
return result;
}

original_code:
bt_dev_evt_e bt_dev_read_hci(bt_dev_t *dev, bt_callbacks_t *cb) {
   dev->ptr = dev->buf;
   if (bt_serial_read(dev, dev->ptr, 1) != 1)
      return dev_evt_timeout;
   return bt_hci_unpack_hci(dev, cb);
}

canonical_original_code:
enum bt_dev_evt_e bt_dev_read_hci(struct bt_dev_s *dev, struct bt_callbacks_s *cb) {
   dev->ptr = dev->buf;
   if (bt_serial_read(dev, dev->ptr, 1) != 1)
      return dev_evt_timeout;
   return bt_hci_unpack_hci(dev, cb);
}

variable_types: struct bt_callbacks_s { void (*cb_ready)(struct bt_dev_s *dev); int (*cb_peer_found)(struct bt_dev_s *dev, struct bt_peer_s *peer); int (*cb_conn_req)(struct bt_dev_s *dev, struct bt_peer_s *peer, unsigned char link_type); void (*cb_conn_succ)(struct bt_dev_s *dev, struct bt_acl_s *acl); void (*cb_conn_unsucc)(struct bt_dev_s *dev, struct bt_peer_s *peer); void (*cb_disconn_succ)(struct bt_dev_s *dev, struct bt_acl_s *acl, unsigned char reason); void (*cb_conn_data)(struct bt_dev_s *dev, struct bt_acl_s *acl); unsigned char *(*cb_pincode_req)(struct bt_dev_s *dev, struct bt_peer_s *peer); unsigned char *(*cb_link_key_req)(struct bt_dev_s *dev, struct bt_peer_s *peer); void (*cb_idle)(struct bt_dev_s *dev); } *

return_type: enum bt_dev_evt_e { dev_evt_none=0, dev_evt_timeout=1, dev_evt_garbage=2, dev_evt_acl=3, dev_evt_sco=4, dev_evt_inquiry_results=5, dev_evt_inquiry_complete_succ=6, dev_evt_inquiry_complete_unsucc=7, dev_evt_conn_request=8, dev_evt_conn_complete_succ=9, dev_evt_conn_complete_unsucc=10, dev_evt_disconn_complete_succ=11, dev_evt_disconn_complete_unsucc=12, dev_evt_auth_complete_succ=13, dev_evt_auth_complete_unsucc=14, dev_evt_pincode_req=15, dev_evt_link_key_not=16, dev_evt_link_key_req=17, dev_evt_link_key_reply_succ=18, dev_evt_link_key_reply_unsucc=19, dev_evt_link_key_reply_neg_succ=20, dev_evt_link_key_reply_neg_unsucc=21, dev_evt_pincode_reply_succ=22, dev_evt_pincode_reply_unsucc=23, dev_evt_pincode_reply_neg_succ=24, dev_evt_pincode_reply_neg_unsucc=25, dev_evt_read_features_succ=26, dev_evt_read_features_unsucc=27, dev_evt_num_comp_pkts=28, dev_evt_max_slots_chg=29, dev_evt_set_evt_flt_succ=30, dev_evt_set_evt_flt_unsucc=31, dev_evt_write_auth_enable_succ=32, dev_evt_write_auth_enable_unsucc=33, dev_evt_write_scan_enable_succ=34, dev_evt_write_scan_enable_unsucc=35, dev_evt_change_name_succ=36, dev_evt_change_name_unsucc=37, dev_evt_read_local_feat_succ=38, dev_evt_read_local_feat_unsucc=39, dev_evt_read_bd_addr_succ=40, dev_evt_read_bd_addr_unsucc=41 }


decompiled_code:
__int64 __fastcall bt_dev_pack_reset(__int64 a1)
{
return bt_hci_pack_cmd(a1, 3LL, 3LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return bt_hci_pack_cmd(a1, 3LL, 3LL, 0LL);
}

original_code:
void bt_dev_pack_reset(bt_dev_t *dev) {
   bt_hci_pack_cmd(dev, 0x03, 0x0003, 0);
}

canonical_original_code:
void bt_dev_pack_reset(struct bt_dev_s *dev) {
   bt_hci_pack_cmd(dev, 0x03, 0x0003, 0);
}


decompiled_code:
char *__fastcall bt_dev_pack_change_local_name(__int64 a1, const char *a2)
{
unsigned __int8 v2; // bl
_QWORD *v3; // rcx
char *result; // rax

v2 = strlen(a2);
bt_hci_pack_cmd(a1, 3LL, 19LL, 248LL);
v3 = *(_QWORD **)(a1 + 264);
*v3 = 0LL;
v3[30] = 0LL;
memset(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 248) >> 3));
result = strncpy(*(char **)(a1 + 264), a2, v2);
*(_QWORD *)(a1 + 264) += 248LL;
return result;
}

canonical_decompiled_code:
char *__fastcall func10(__int64 a1, const char *a2)
{
unsigned __int8 v2; // bl
_QWORD *v3; // rcx
char *result; // rax

v2 = strlen(a2);
bt_hci_pack_cmd(a1, 3LL, 19LL, 248LL);
v3 = *(_QWORD **)(a1 + 264);
*v3 = 0LL;
v3[30] = 0LL;
memset(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 248) >> 3));
result = strncpy(*(char **)(a1 + 264), a2, v2);
*(_QWORD *)(a1 + 264) += 248LL;
return result;
}

original_code:
void bt_dev_pack_change_local_name(bt_dev_t *dev, const char *s) {
   unsigned char len = strlen(s);
   bt_hci_pack_cmd(dev, 0x03, 0x0013, 248);
   memset((char *)dev->ptr, 0, 248);
   strncpy((char *)dev->ptr, s, len);
   dev->ptr += 248;
}

canonical_original_code:
void bt_dev_pack_change_local_name(struct bt_dev_s *dev, char *s) {
   unsigned char len = strlen(s);
   bt_hci_pack_cmd(dev, 0x03, 0x0013, 248);
   memset((char *)dev->ptr, 0, 248);
   strncpy((char *)dev->ptr, s, len);
   dev->ptr += 248;
}

variable_types:
s
char *

variable_types:
len
unsigned char


decompiled_code:
__int64 __fastcall bt_dev_pack_inquiry(__int64 a1, char a2, char a3)
{
__int64 result; // rax
int v5; // [rsp+Bh] [rbp-1Dh]

v5 = 10390323;
bt_hci_pack_cmd(a1, 1LL, 1LL, 5LL);
result = *(_QWORD *)(a1 + 264);
HIBYTE(v5) = a2;
*(_BYTE *)(result + 4) = a3;
*(_DWORD *)result = v5;
*(_QWORD *)(a1 + 264) += 5LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, char a2, char a3)
{
__int64 result; // rax
int v5; // [rsp+Bh] [rbp-1Dh]

v5 = 10390323;
bt_hci_pack_cmd(a1, 1LL, 1LL, 5LL);
result = *(_QWORD *)(a1 + 264);
HIBYTE(v5) = a2;
*(_BYTE *)(result + 4) = a3;
*(_DWORD *)result = v5;
*(_QWORD *)(a1 + 264) += 5LL;
return result;
}

original_code:
void bt_dev_pack_inquiry(bt_dev_t *dev,
                         unsigned char len,
                         unsigned char num_peers) {
   unsigned char inq_data[5] = {
      0x33, 0x8B, 0x9E,
      0,
      0
   };
   inq_data[3] = len;
   inq_data[4] = num_peers;
   bt_hci_pack_cmd(dev, 0x01, 0x0001, 5);
   memcpy(dev->ptr, inq_data, 5); dev->ptr += 5;
}

canonical_original_code:
void bt_dev_pack_inquiry(struct bt_dev_s *dev, unsigned char len, unsigned char num_peers) {
   unsigned char inq_data[5] = {
      0x33, 0x8B, 0x9E,
      0,
      0
   };
   inq_data[3] = len;
   inq_data[4] = num_peers;
   bt_hci_pack_cmd(dev, 0x01, 0x0001, 5);
   memcpy(dev->ptr, inq_data, 5); dev->ptr += 5;
}


original_code:
void bt_dev_pack_link_key_reply(bt_dev_t *dev,
                                unsigned char *bd_addr,
                                unsigned char *link_key) {
   bt_hci_pack_cmd(dev, 0x01, 0x000b, 22);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
   memcpy(dev->ptr, link_key, 16); dev->ptr += 16;
}

canonical_original_code:
void bt_dev_pack_link_key_reply(struct bt_dev_s *dev, unsigned char *bd_addr, unsigned char *link_key) {
   bt_hci_pack_cmd(dev, 0x01, 0x000b, 22);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
   memcpy(dev->ptr, link_key, 16); dev->ptr += 16;
}


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

bt_hci_pack_cmd(a1, 1LL, 12LL, 6LL);
result = *(_QWORD *)(a1 + 264);
*(_DWORD *)result = *(_DWORD *)a2;
*(_WORD *)(result + 4) = *(_WORD *)(a2 + 4);
*(_QWORD *)(a1 + 264) += 6LL;
return result;
}

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

bt_hci_pack_cmd(a1, 1LL, 12LL, 6LL);
result = *(_QWORD *)(a1 + 264);
*(_DWORD *)result = *(_DWORD *)a2;
*(_WORD *)(result + 4) = *(_WORD *)(a2 + 4);
*(_QWORD *)(a1 + 264) += 6LL;
return result;
}

original_code:
void bt_dev_pack_link_key_reply_neg(bt_dev_t *dev,
                                    unsigned char *bd_addr) {
   bt_hci_pack_cmd(dev, 0x01, 0x000c, 6);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
}

canonical_original_code:
void bt_dev_pack_link_key_reply_neg(struct bt_dev_s *dev, unsigned char *bd_addr) {
   bt_hci_pack_cmd(dev, 0x01, 0x000c, 6);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
}


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

bt_hci_pack_cmd(a1, 1LL, 14LL, 6LL);
result = *(_QWORD *)(a1 + 264);
*(_DWORD *)result = *(_DWORD *)a2;
*(_WORD *)(result + 4) = *(_WORD *)(a2 + 4);
*(_QWORD *)(a1 + 264) += 6LL;
return result;
}

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

bt_hci_pack_cmd(a1, 1LL, 14LL, 6LL);
result = *(_QWORD *)(a1 + 264);
*(_DWORD *)result = *(_DWORD *)a2;
*(_WORD *)(result + 4) = *(_WORD *)(a2 + 4);
*(_QWORD *)(a1 + 264) += 6LL;
return result;
}

original_code:
void bt_dev_pack_pincode_reply_neg(bt_dev_t *dev,
                                    unsigned char *bd_addr) {
   bt_hci_pack_cmd(dev, 0x01, 0x000e, 6);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
}

canonical_original_code:
void bt_dev_pack_pincode_reply_neg(struct bt_dev_s *dev, unsigned char *bd_addr) {
   bt_hci_pack_cmd(dev, 0x01, 0x000e, 6);
   memcpy(dev->ptr, bd_addr, 6); dev->ptr += 6;
}


decompiled_code:
__int64 __fastcall bt_dev_pack_read_local_features(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 3LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 3LL, 0LL);
}

original_code:
void bt_dev_pack_read_local_features(bt_dev_t *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0003, 0);
}

canonical_original_code:
void bt_dev_pack_read_local_features(struct bt_dev_s *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0003, 0);
}


decompiled_code:
__int64 __fastcall bt_dev_pack_read_buffer_size(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 5LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 5LL, 0LL);
}

original_code:
void bt_dev_pack_read_buffer_size(bt_dev_t *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0005, 0);
}

canonical_original_code:
void bt_dev_pack_read_buffer_size(struct bt_dev_s *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0005, 0);
}


decompiled_code:
__int64 __fastcall bt_dev_pack_read_bd_addr(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 9LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
return bt_hci_pack_cmd(a1, 4LL, 9LL, 0LL);
}

original_code:
void bt_dev_pack_read_bd_addr(bt_dev_t *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0009, 0);
}

canonical_original_code:
void bt_dev_pack_read_bd_addr(struct bt_dev_s *dev) {
   bt_hci_pack_cmd(dev, 0x04, 0x0009, 0);
}


original_code:
void bt_dev_pack_l2cap(bt_dev_t *dev, unsigned short handle,
                       unsigned char *data, int len) {
   bt_hci_pack_acl(dev, handle, 0x02, 0x00, len+4);
   { *(dev->ptr++) = ((len) & 0xFF); *(dev->ptr++) = (((len) >> 8) & 0xFF); };
   { *(dev->ptr++) = ((0x40) & 0xFF); *(dev->ptr++) = (((0x40) >> 8) & 0xFF); };
   memcpy(dev->ptr, data, len);
   dev->ptr += len;
}

canonical_original_code:
void bt_dev_pack_l2cap(struct bt_dev_s *dev, unsigned short handle, unsigned char *data, int len) {
   bt_hci_pack_acl(dev, handle, 0x02, 0x00, len+4);
   { *(dev->ptr++) = ((len) & 0xFF); *(dev->ptr++) = (((len) >> 8) & 0xFF); };
   { *(dev->ptr++) = ((0x40) & 0xFF); *(dev->ptr++) = (((0x40) >> 8) & 0xFF); };
   memcpy(dev->ptr, data, len);
   dev->ptr += len;
}

variable_types:
data
unsigned char *

variable_types: len int


decompiled_code:
__int64 __fastcall bt_serial_init(__int64 a1)
{
int v1; // eax
__m128i v2; // xmm0
__m128i v3; // xmm1
__m128i v4; // xmm2
int v5; // er8
__int64 result; // rax
struct termios v7; // [rsp+0h] [rbp-58h] BYREF

v1 = open((const char *)(a1 + 408), 258);
*(_DWORD *)(a1 + 536) = v1;
if ( v1 < 0 )
{
error_str("could not open TTY");
result = 0LL;
}
else if ( isatty(v1) && tcgetattr(*(_DWORD *)(a1 + 536), (struct termios *)(a1 + 540)) >= 0 )
{
v2 = _mm_loadu_si128((const __m128i *)(a1 + 540));
v3 = _mm_loadu_si128((const __m128i *)(a1 + 556));
v4 = _mm_loadu_si128((const __m128i *)(a1 + 572));
*(_QWORD *)&v7.c_cc[31] = *(_QWORD *)(a1 + 588);
v7.c_ospeed = *(_DWORD *)(a1 + 596);
v7.c_lflag = v2.m128i_i32[3] & 0xFFFF7FB4;
*(__m128i *)&v7.c_line = v3;
*(__m128i *)&v7.c_cc[15] = v4;
v7.c_cflag = v2.m128i_i32[2] & 0xFFFFFACF | 0x830;
*(_QWORD *)&v7.c_iflag = v2.m128i_i64[0] & 0xFFFFFFFEFFFFFA14LL;
*(_WORD *)&v7.c_cc[5] = 256;
if ( cfsetispeed(&v7, 0x1002u) < 0 || cfsetospeed(&v7, 0x1002u) < 0 )
{
error_str("Could not set 115200 bps baudrate");
tcsetattr(*(_DWORD *)(a1 + 536), 2, (const struct termios *)(a1 + 540));
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
else
{
v5 = tcsetattr(*(_DWORD *)(a1 + 536), 2, &v7);
result = 1LL;
if ( v5 < 0 )
{
error_str("Could not switch TTY to noncanonical mode");
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
}
}
else
{
error_str("TTY is not a tty");
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax
__m128i v2; // xmm0
__m128i v3; // xmm1
__m128i v4; // xmm2
int v5; // er8
__int64 result; // rax
struct termios v7; // [rsp+0h] [rbp-58h] BYREF

v1 = open((const char *)(a1 + 408), 258);
*(_DWORD *)(a1 + 536) = v1;
if ( v1 < 0 )
{
error_str("could not open TTY");
result = 0LL;
}
else if ( isatty(v1) && tcgetattr(*(_DWORD *)(a1 + 536), (struct termios *)(a1 + 540)) >= 0 )
{
v2 = _mm_loadu_si128((const __m128i *)(a1 + 540));
v3 = _mm_loadu_si128((const __m128i *)(a1 + 556));
v4 = _mm_loadu_si128((const __m128i *)(a1 + 572));
*(_QWORD *)&v7.c_cc[31] = *(_QWORD *)(a1 + 588);
v7.c_ospeed = *(_DWORD *)(a1 + 596);
v7.c_lflag = v2.m128i_i32[3] & 0xFFFF7FB4;
*(__m128i *)&v7.c_line = v3;
*(__m128i *)&v7.c_cc[15] = v4;
v7.c_cflag = v2.m128i_i32[2] & 0xFFFFFACF | 0x830;
*(_QWORD *)&v7.c_iflag = v2.m128i_i64[0] & 0xFFFFFFFEFFFFFA14LL;
*(_WORD *)&v7.c_cc[5] = 256;
if ( cfsetispeed(&v7, 0x1002u) < 0 || cfsetospeed(&v7, 0x1002u) < 0 )
{
error_str("Could not set 115200 bps baudrate");
tcsetattr(*(_DWORD *)(a1 + 536), 2, (const struct termios *)(a1 + 540));
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
else
{
v5 = tcsetattr(*(_DWORD *)(a1 + 536), 2, &v7);
result = 1LL;
if ( v5 < 0 )
{
error_str("Could not switch TTY to noncanonical mode");
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
}
}
else
{
error_str("TTY is not a tty");
close(*(_DWORD *)(a1 + 536));
result = 0LL;
}
return result;
}

original_code:
int bt_serial_init(bt_dev_t *dev) {
   struct termios term;
   if (((dev->ttyfd = open(dev->tty, 02 | 0400)) < 0)) {
      error_str(("could not open TTY"));
      return 0;
   }
   if (!isatty(dev->ttyfd) ||
       (tcgetattr(dev->ttyfd, &dev->save_termios) < 0)) {
      error_str(("TTY is not a tty"));
      close(dev->ttyfd);
      return 0;
   }
   term = dev->save_termios;
   term.c_lflag &= ~(0000010|0000100|0000002|0000001|0100000);
   term.c_iflag &= ~(0000001|0000002|0000010|0000040|0000100|0000200|0000400|0002000);
   term.c_cflag &= ~(0000060 | 0000400);
  term.c_cflag &= ~(0002000|0000400|0000060);
  term.c_cflag |= (0000060|0004000);
   term.c_oflag &= ~(0000001);
   term.c_cc[6] = 1;
   term.c_cc[5] = 0;
   if ((cfsetispeed(&term, 0010002) < 0) ||
       (cfsetospeed(&term, 0010002) <0 )) {
      error_str(("Could not set 115200 bps baudrate"));
      tcsetattr(dev->ttyfd, 2, &dev->save_termios);
      close(dev->ttyfd);
      return 0;
   }
   if (tcsetattr(dev->ttyfd, 2, &term) < 0) {
      error_str(("Could not switch TTY to noncanonical mode"));
      close(dev->ttyfd);
      return 0;
   }
   return 1;
}

canonical_original_code:
int bt_serial_init(struct bt_dev_s *dev) {
   struct termios term;
   if (((dev->ttyfd = open(dev->tty, 02 | 0400)) < 0)) {
      error_str(("could not open TTY"));
      return 0;
   }
   if (!isatty(dev->ttyfd) ||
       (tcgetattr(dev->ttyfd, &dev->save_termios) < 0)) {
      error_str(("TTY is not a tty"));
      close(dev->ttyfd);
      return 0;
   }
   term = dev->save_termios;
   term.c_lflag &= ~(0000010|0000100|0000002|0000001|0100000);
   term.c_iflag &= ~(0000001|0000002|0000010|0000040|0000100|0000200|0000400|0002000);
   term.c_cflag &= ~(0000060 | 0000400);
  term.c_cflag &= ~(0002000|0000400|0000060);
  term.c_cflag |= (0000060|0004000);
   term.c_oflag &= ~(0000001);
   term.c_cc[6] = 1;
   term.c_cc[5] = 0;
   if ((cfsetispeed(&term, 0010002) < 0) ||
       (cfsetospeed(&term, 0010002) <0 )) {
      error_str(("Could not set 115200 bps baudrate"));
      tcsetattr(dev->ttyfd, 2, &dev->save_termios);
      close(dev->ttyfd);
      return 0;
   }
   if (tcsetattr(dev->ttyfd, 2, &term) < 0) {
      error_str(("Could not switch TTY to noncanonical mode"));
      close(dev->ttyfd);
      return 0;
   }
   return 1;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
__int64 __fastcall bt_serial_close(const struct termios *a1)
{
__int64 result; // rax

if ( tcsetattr(a1[8].c_ospeed, 2, a1 + 9) < 0 )
{
error_str("Could not restore TTY settings");
close(a1[8].c_ospeed);
result = 0LL;
}
else
{
close(a1[8].c_ospeed);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const struct termios *a1)
{
__int64 result; // rax

if ( tcsetattr(a1[8].c_ospeed, 2, a1 + 9) < 0 )
{
error_str("Could not restore TTY settings");
close(a1[8].c_ospeed);
result = 0LL;
}
else
{
close(a1[8].c_ospeed);
result = 1LL;
}
return result;
}

original_code:
int bt_serial_close(bt_dev_t *dev) {
   if (tcsetattr(dev->ttyfd, 2, &dev->save_termios) < 0) {
      error_str(("Could not restore TTY settings"));
      close(dev->ttyfd);
      return 0;
   }
   close(dev->ttyfd);
   return 1;
}

canonical_original_code:
int bt_serial_close(struct bt_dev_s *dev) {
   if (tcsetattr(dev->ttyfd, 2, &dev->save_termios) < 0) {
      error_str(("Could not restore TTY settings"));
      close(dev->ttyfd);
      return 0;
   }
   close(dev->ttyfd);
   return 1;
}


decompiled_code:
__int64 __fastcall bt_serial_send(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 *v4; // rbx
unsigned __int8 *v5; // r12
__int64 v6; // r14
__int64 v7; // rsi
int v8; // er12
int v9; // eax

v4 = a2;
printf("Send to serial: ");
if ( a3 <= 0 )
{
v8 = 0;
putchar(10);
}
else
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
{
v7 = *v5++;
printf("%x, ", v7);
}
while ( v5 != (unsigned __int8 *)v6 );
v8 = 0;
putchar(10);
while ( 1 )
{
v9 = write(*(_DWORD *)(a1 + 536), &v4[v8], 1uLL);
if ( v9 < 0 )
break;
v8 += v9;
if ( a3 <= v8 )
return (unsigned int)v8;
}
v8 = -1;
}
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 *v4; // rbx
unsigned __int8 *v5; // r12
__int64 v6; // r14
__int64 v7; // rsi
int v8; // er12
int v9; // eax

v4 = a2;
printf("Send to serial: ");
if ( a3 <= 0 )
{
v8 = 0;
putchar(10);
}
else
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
{
v7 = *v5++;
printf("%x, ", v7);
}
while ( v5 != (unsigned __int8 *)v6 );
v8 = 0;
putchar(10);
while ( 1 )
{
v9 = write(*(_DWORD *)(a1 + 536), &v4[v8], 1uLL);
if ( v9 < 0 )
break;
v8 += v9;
if ( a3 <= v8 )
return (unsigned int)v8;
}
v8 = -1;
}
return (unsigned int)v8;
}

original_code:
int bt_serial_send(bt_dev_t *dev, unsigned char *buf, int len) {
   int tlen = 0, wlen;
   int i;
   printf("Send to serial: ");
   for (i = 0; i < len; i++) {
      printf("%x, ", buf[i]);
   }
   printf("\n");
   while (tlen < len) {
      if ((wlen = write(dev->ttyfd, (void *)(buf + tlen), 1)) < 0)
         return -1;
      tlen += wlen;
   }
   return tlen;
}

canonical_original_code:
int bt_serial_send(struct bt_dev_s *dev, unsigned char *buf, int len) {
   int tlen = 0; int wlen;
   int i;
   printf("Send to serial: ");
   for (i = 0; i < len; i++) {
      printf("%x, ", buf[i]);
   }
   printf("\n");
   while (tlen < len) {
      if ((wlen = write(dev->ttyfd, (void *)(buf + tlen), 1)) < 0)
         return -1;
      tlen += wlen;
   }
   return tlen;
}

variable_types:
buf
unsigned char *

variable_types: tlen int

variable_types: wlen int

variable_types: i int


decompiled_code:
__int64 __fastcall bt_serial_read(__int64 a1, __int64 a2, int a3)
{
int v3; // er12
unsigned __int8 *v5; // r15
int v6; // ebx

v3 = 0;
printf("Read from serial: ");
if ( a3 > 0 )
{
while ( 1 )
{
v5 = (unsigned __int8 *)(a2 + v3);
v6 = read(*(_DWORD *)(a1 + 536), v5, 1uLL);
if ( v6 <= 0 )
break;
v3 += v6;
printf("%x, ", *v5);
if ( a3 <= v3 )
goto LABEL_6;
}
v3 = -1;
}
else
{
LABEL_6:
putchar(10);
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, int a3)
{
int v3; // er12
unsigned __int8 *v5; // r15
int v6; // ebx

v3 = 0;
printf("Read from serial: ");
if ( a3 > 0 )
{
while ( 1 )
{
v5 = (unsigned __int8 *)(a2 + v3);
v6 = read(*(_DWORD *)(a1 + 536), v5, 1uLL);
if ( v6 <= 0 )
break;
v3 += v6;
printf("%x, ", *v5);
if ( a3 <= v3 )
goto LABEL_6;
}
v3 = -1;
}
else
{
LABEL_6:
putchar(10);
}
return (unsigned int)v3;
}

original_code:
int bt_serial_read(bt_dev_t *dev, unsigned char *buf, int len) {
   int tlen = 0, rlen;
   printf("Read from serial: ");
   while (tlen < len) {
      if ((rlen = read(dev->ttyfd, (void *)(buf + tlen), 1)) <= 0)
         return -1;
      printf("%x, ", buf[tlen]);
      tlen += rlen;
   }
    printf("\n");
   return tlen;
}

canonical_original_code:
int bt_serial_read(struct bt_dev_s *dev, unsigned char *buf, int len) {
   int tlen = 0; int rlen;
   printf("Read from serial: ");
   while (tlen < len) {
      if ((rlen = read(dev->ttyfd, (void *)(buf + tlen), 1)) <= 0)
         return -1;
      printf("%x, ", buf[tlen]);
      tlen += rlen;
   }
    printf("\n");
   return tlen;
}

variable_types: rlen int


decompiled_code:
int __fastcall io_str(const char *a1)
{
return printf("DEBUG: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return printf("DEBUG: %s\n", a1);
}

original_code:
void io_str(char *s) {
   printf("DEBUG: %s\n", s);
}


decompiled_code:
int __fastcall io_int(unsigned int a1)
{
return printf("DEBUG: 0x%x\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1)
{
return printf("DEBUG: 0x%x\n", a1);
}

original_code:
void io_int(int d) {
   printf("DEBUG: 0x%x\n", d);
}

variable_types: d int


decompiled_code:
int __fastcall error_str(const char *a1)
{
return fprintf(stderr, "ERROR: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
return fprintf(stderr, "ERROR: %s\n", a1);
}

original_code:
void error_str(char *s) {
   fprintf(stderr, "ERROR: %s\n", s);
}


original_code:
void io_bd_addr(unsigned char *bd_addr) {
   printf("%x:%x:%x:%x:%x:%x\n",
         bd_addr[0], bd_addr[1], bd_addr[2],
         bd_addr[3], bd_addr[4], bd_addr[5]);
}


decompiled_code:
__int64 __fastcall bt_l2cap_unpack_cmd(__int64 a1)
{
_QWORD *v2; // rdi
char *v3; // rax
char v4; // bl
unsigned __int8 v5; // r12
unsigned __int16 v6; // ax
__int64 result; // rax

v2 = (_QWORD *)(a1 + 264);
v3 = (char *)(*v2)++;
v4 = *v3;
*v2 = v3 + 2;
v5 = v3[1];
v6 = uint16_unpack__();
switch ( v4 )
{
case 1:
if ( v5 == 1 )
{
result = v6 < 2u ? 18 : 1;
}
else if ( v5 == 2 )
{
result = v6 < 4u ? 18 : 2;
}
else
{
result = v5 != 0 ? 0x12 : 0;
}
break;
case 2:
result = v6 < 4u ? 18 : 3;
break;
case 3:
result = v6 < 8u ? 18 : 4;
break;
case 4:
result = v6 < 4u ? 18 : 5;
break;
case 5:
result = v6 < 8u ? 18 : 6;
break;
case 6:
result = v6 < 4u ? 18 : 7;
break;
case 7:
result = v6 < 4u ? 18 : 8;
break;
case 8:
bt_l2cap_pack_cmd(a1, 9LL, v5, 0);
bt_dev_flush_hci();
result = 9LL;
break;
case 9:
result = 10LL;
break;
case 10:
result = v6 < 2u ? 18 : 11;
break;
case 11:
result = v6 < 4u ? 18 : 12;
break;
default:
result = 18LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_QWORD *v2; // rdi
char *v3; // rax
char v4; // bl
unsigned __int8 v5; // r12
unsigned __int16 v6; // ax
__int64 result; // rax

v2 = (_QWORD *)(a1 + 264);
v3 = (char *)(*v2)++;
v4 = *v3;
*v2 = v3 + 2;
v5 = v3[1];
v6 = uint16_unpack__();
switch ( v4 )
{
case 1:
if ( v5 == 1 )
{
result = v6 < 2u ? 18 : 1;
}
else if ( v5 == 2 )
{
result = v6 < 4u ? 18 : 2;
}
else
{
result = v5 != 0 ? 0x12 : 0;
}
break;
case 2:
result = v6 < 4u ? 18 : 3;
break;
case 3:
result = v6 < 8u ? 18 : 4;
break;
case 4:
result = v6 < 4u ? 18 : 5;
break;
case 5:
result = v6 < 8u ? 18 : 6;
break;
case 6:
result = v6 < 4u ? 18 : 7;
break;
case 7:
result = v6 < 4u ? 18 : 8;
break;
case 8:
bt_l2cap_pack_cmd(a1, 9LL, v5, 0);
bt_dev_flush_hci();
result = 9LL;
break;
case 9:
result = 10LL;
break;
case 10:
result = v6 < 2u ? 18 : 11;
break;
case 11:
result = v6 < 4u ? 18 : 12;
break;
default:
result = 18LL;
break;
}
return result;
}

original_code:
bt_l2cap_evt_e bt_l2cap_unpack_cmd(bt_dev_t *dev) {
   unsigned char code, id;
   unsigned short len;
   code = (*(dev->ptr++));
   id = (*(dev->ptr++));
   len = uint16_unpack__(&dev->ptr);
   switch (code) {
      case 0x01:
         switch (id) {
            case 0:
               return l2cap_evt_cmd_rej_not_understood;
            case 1:
               if (len < 2) {
                  return l2cap_evt_garbage;
               } else {
                  return l2cap_evt_cmd_rej_mtu_exceeded;
               }
            case 2:
               if (len < 4) {
                  return l2cap_evt_garbage;
               } else {
                  return l2cap_evt_cmd_rej_cid_invalid;
               }
            default:
               return l2cap_evt_garbage;
         }
      case 0x02:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conn_req;
         }
      case 0x03:
         if (len < 8) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conn_rsp;
         }
      case 0x04:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_req;
         }
      case 0x05:
         if (len < 8) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_rsp;
         }
      case 0x06:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_disconn_req;
         }
      case 0x07:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_disconn_rsp;
         }
      case 0x08:
         bt_l2cap_pack_cmd(dev, 0x09, id, 0);
         bt_dev_flush_hci(dev);
         return l2cap_evt_echo_req;
      case 0x09:
         return l2cap_evt_echo_rsp;
      case 0x0a:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_info_req;
         }
      case 0x0b:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_info_rsp;
         }
      default:
         return l2cap_evt_garbage;
   }
}

canonical_original_code:
enum bt_l2cap_evt_e bt_l2cap_unpack_cmd(struct bt_dev_s *dev) {
   unsigned char code; unsigned char id;
   unsigned short len;
   code = (*(dev->ptr++));
   id = (*(dev->ptr++));
   len = uint16_unpack__(&dev->ptr);
   switch (code) {
      case 0x01:
         switch (id) {
            case 0:
               return l2cap_evt_cmd_rej_not_understood;
            case 1:
               if (len < 2) {
                  return l2cap_evt_garbage;
               } else {
                  return l2cap_evt_cmd_rej_mtu_exceeded;
               }
            case 2:
               if (len < 4) {
                  return l2cap_evt_garbage;
               } else {
                  return l2cap_evt_cmd_rej_cid_invalid;
               }
            default:
               return l2cap_evt_garbage;
         }
      case 0x02:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conn_req;
         }
      case 0x03:
         if (len < 8) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conn_rsp;
         }
      case 0x04:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_req;
         }
      case 0x05:
         if (len < 8) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_rsp;
         }
      case 0x06:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_disconn_req;
         }
      case 0x07:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_disconn_rsp;
         }
      case 0x08:
         bt_l2cap_pack_cmd(dev, 0x09, id, 0);
         bt_dev_flush_hci(dev);
         return l2cap_evt_echo_req;
      case 0x09:
         return l2cap_evt_echo_rsp;
      case 0x0a:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_info_req;
         }
      case 0x0b:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_info_rsp;
         }
      default:
         return l2cap_evt_garbage;
   }
}

variable_types:
code
unsigned char

variable_types:
id
unsigned char

variable_types:
len
unsigned short

return_type: enum bt_l2cap_evt_e { l2cap_evt_cmd_rej_not_understood=0, l2cap_evt_cmd_rej_mtu_exceeded=1, l2cap_evt_cmd_rej_cid_invalid=2, l2cap_evt_conn_req=3, l2cap_evt_conn_rsp=4, l2cap_evt_conf_req=5, l2cap_evt_conf_rsp=6, l2cap_evt_disconn_req=7, l2cap_evt_disconn_rsp=8, l2cap_evt_echo_req=9, l2cap_evt_echo_rsp=10, l2cap_evt_info_req=11, l2cap_evt_info_rsp=12, l2cap_evt_conn_data=13, l2cap_evt_connless_data=14, l2cap_evt_conf_mtu=15, l2cap_evt_conf_flush=16, l2cap_evt_conf_qos=17, l2cap_evt_garbage=18 }


decompiled_code:
__int64 __fastcall bt_l2cap_unpack(__int64 a1)
{
unsigned __int16 v1; // bx
unsigned __int16 v2; // ax
int v3; // edx
__int64 result; // rax

v1 = uint16_unpack__(a1 + 264);
v2 = uint16_unpack__(a1 + 264);
if ( v2 == 1 )
{
result = 18LL;
if ( v1 > 3u )
result = bt_l2cap_unpack_cmd(a1);
}
else
{
v3 = v2;
result = 14LL;
if ( v3 != 2 )
result = v3 == 0 ? 18 : 13;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned __int16 v1; // bx
unsigned __int16 v2; // ax
int v3; // edx
__int64 result; // rax

v1 = uint16_unpack__(a1 + 264);
v2 = uint16_unpack__(a1 + 264);
if ( v2 == 1 )
{
result = 18LL;
if ( v1 > 3u )
result = func0(a1);
}
else
{
v3 = v2;
result = 14LL;
if ( v3 != 2 )
result = v3 == 0 ? 18 : 13;
}
return result;
}

original_code:
bt_l2cap_evt_e bt_l2cap_unpack(bt_dev_t *dev) {
   unsigned short cid, len;
   len = uint16_unpack__(&dev->ptr);
   cid = uint16_unpack__(&dev->ptr);
   switch (cid) {
      case 0:
         return l2cap_evt_garbage;
      case 1:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return bt_l2cap_unpack_cmd(dev);
         }
      case 2:
         return l2cap_evt_connless_data;
      default:
         return l2cap_evt_conn_data;
   }
}

canonical_original_code:
enum bt_l2cap_evt_e bt_l2cap_unpack(struct bt_dev_s *dev) {
   unsigned short cid; unsigned short len;
   len = uint16_unpack__(&dev->ptr);
   cid = uint16_unpack__(&dev->ptr);
   switch (cid) {
      case 0:
         return l2cap_evt_garbage;
      case 1:
         if (len < 4) {
            return l2cap_evt_garbage;
         } else {
            return bt_l2cap_unpack_cmd(dev);
         }
      case 2:
         return l2cap_evt_connless_data;
      default:
         return l2cap_evt_conn_data;
   }
}

variable_types:
cid
unsigned short

user_defined_types: enum bt_l2cap_evt_e { l2cap_evt_cmd_rej_not_understood=0, l2cap_evt_cmd_rej_mtu_exceeded=1, l2cap_evt_cmd_rej_cid_invalid=2, l2cap_evt_conn_req=3, l2cap_evt_conn_rsp=4, l2cap_evt_conf_req=5, l2cap_evt_conf_rsp=6, l2cap_evt_disconn_req=7, l2cap_evt_disconn_rsp=8, l2cap_evt_echo_req=9, l2cap_evt_echo_rsp=10, l2cap_evt_info_req=11, l2cap_evt_info_rsp=12, l2cap_evt_conn_data=13, l2cap_evt_connless_data=14, l2cap_evt_conf_mtu=15, l2cap_evt_conf_flush=16, l2cap_evt_conf_qos=17, l2cap_evt_garbage=18 }


decompiled_code:
__int64 __fastcall bt_l2cap_unpack_conf_option(__int64 a1)
{
char *v1; // rax
char v2; // dl
unsigned __int8 v3; // cl
__int64 result; // rax

v1 = *(char **)(a1 + 264);
*(_QWORD *)(a1 + 264) = v1 + 1;
v2 = *v1;
*(_QWORD *)(a1 + 264) = v1 + 2;
v3 = v1[1];
if ( v2 == 2 )
return v3 < 2u ? 18 : 16;
if ( v2 == 3 )
return (unsigned int)(v3 < 0x16u) + 17;
result = 18LL;
if ( v2 == 1 )
result = v3 < 2u ? 18 : 15;
return result;
}

original_code:
bt_l2cap_evt_e bt_l2cap_unpack_conf_option(bt_dev_t *dev) {
   unsigned char type, len;
   type = (*(dev->ptr++));
   len = (*(dev->ptr++));
   switch (type) {
      case 0x01:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_mtu;
         }
      case 0x02:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_flush;
         }
      case 0x03:
         if (len < 22) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_qos;
         }
      default:
         return l2cap_evt_garbage;
   }
}

canonical_original_code:
enum bt_l2cap_evt_e bt_l2cap_unpack_conf_option(struct bt_dev_s *dev) {
   unsigned char type; unsigned char len;
   type = (*(dev->ptr++));
   len = (*(dev->ptr++));
   switch (type) {
      case 0x01:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_mtu;
         }
      case 0x02:
         if (len < 2) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_flush;
         }
      case 0x03:
         if (len < 22) {
            return l2cap_evt_garbage;
         } else {
            return l2cap_evt_conf_qos;
         }
      default:
         return l2cap_evt_garbage;
   }
}

variable_types: struct bt_dev_s { unsigned char state; unsigned char ncmds; unsigned char buf[256]; char name[32]; unsigned short pkt_type; struct bt_peer_s { unsigned char state; unsigned char bd_addr[6]; unsigned short clock_offset; unsigned char pscan_rep_mode; } peers[3]; struct bt_acl_s { unsigned char state; unsigned short handle; unsigned short pkt_types; unsigned char max_slots; unsigned char encrypt_mode; struct bt_peer_s *peer; } acls[3]; char tty[128]; int ttyfd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } save_termios; } *

variable_types:
type
unsigned char


decompiled_code:
__int64 __fastcall uint16_unpack__(_QWORD *a1)
{
unsigned __int8 *v1; // rdx
int v2; // ecx

v1 = (unsigned __int8 *)(*a1)++;
v2 = *v1;
*a1 = v1 + 2;
return v2 | (v1[1] << 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
unsigned __int8 *v1; // rdx
int v2; // ecx

v1 = (unsigned __int8 *)(*a1)++;
v2 = *v1;
*a1 = v1 + 2;
return v2 | (v1[1] << 8);
}

original_code:
unsigned int uint16_unpack__(unsigned char **ptr) {
   int i = *((*ptr)++);
   return (i |= *((*ptr)++) << 8);
}

variable_types:
ptr
unsigned char **

return_type: unsigned int


main

canonical_name: main

repo: wesgarland/gpsee

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v5[2]; // [rsp+8h] [rbp-10h] BYREF

html_header(argc, argv, envp);
html_begin("Query Results");
v3 = read_cgi_input(v5);
printf("
Status = %d
\n", v3);
h1("Query results");
print_entries(v5[0]);
html_end();
list_clear(v5);
return 0;
}

original_code:
int main()
{
  llist entries;
  int status;
  html_header();
  html_begin("Query Results");
  status = read_cgi_input(&entries);
  printf("
Status = %d
\n",status);
  h1("Query results");
  print_entries(entries);
  html_end();
  list_clear(&entries);
  return 0;
}

canonical_original_code:
int main()
{
  struct llist entries;
  int status;
  html_header();
  html_begin("Query Results");
  status = read_cgi_input(&entries);
  printf("
Status = %d
\n",status);
  h1("Query results");
  print_entries(entries);
  html_end();
  list_clear(&entries);
  return 0;
}

variable_types: struct llist { struct _node { struct entrytype { char *name; char *value; } entry; struct _node *next; } *head; }

variable_types: status int

user_defined_types: struct _node { struct entrytype entry; struct _node *next; }


decompiled_code:
int html_header()
{
return puts("Content-type: text/html\n");
}

canonical_decompiled_code:
int func0()
{
return puts("Content-type: text/html\n");
}

original_code:
void html_header(void)
{
  printf("Content-type: text/html\n\n");
}

canonical_original_code:
void html_header()
{
  printf("Content-type: text/html\n\n");
}


decompiled_code:
int __fastcall mime_header(const char *a1)
{
return printf("Content-type: %s\n\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return printf("Content-type: %s\n\n", a1);
}

original_code:
void mime_header(char *mime)
{
  printf("Content-type: %s\n\n",mime);
}

variable_types:
mime
char *


decompiled_code:
int __fastcall nph_header(const char *a1)
{
printf("HTTP/1.0 %s\n", a1);
return puts("Server: CGI using cgihtml");
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
printf("HTTP/1.0 %s\n", a1);
return puts("Server: CGI using cgihtml");
}

original_code:
void nph_header(char *status)
{
  printf("HTTP/1.0 %s\n",status);
  printf("Server: CGI using cgihtml\n");
}

variable_types:
status
char *


decompiled_code:
int __fastcall show_html_page(const char *a1)
{
return printf("Location: %s\n\n", a1);
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
return printf("Location: %s\n\n", a1);
}

original_code:
void show_html_page(char *loc)
{
  printf("Location: %s\n\n",loc);
}

variable_types:
loc
char *


decompiled_code:
int __fastcall http_status(const char *a1)
{
return printf("Status: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func4(const char *a1)
{
return printf("Status: %s\n", a1);
}

original_code:
void http_status(char *status)
{
  printf("Status: %s\n",status);
}


pragma

decompiled_code:
int __fastcall pragma(const char *a1)
{
return printf("Pragma: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return printf("Pragma: %s\n", a1);
}

original_code:
void pragma(char *msg)
{
  printf("Pragma: %s\n",msg);
}

variable_types:
msg
char *


decompiled_code:
int __fastcall set_cookie(const char *a1, const char *a2, const char *a3, const char *a4, const char *a5, __int16 a6)
{
printf("Set-Cookie: %s=%s;", a1, a2);
if ( a3 )
printf(" EXPIRES=%s;", a3);
if ( a4 )
printf(" PATH=%s;", a4);
if ( a5 )
printf(" DOMAIN=%s;", a5);
if ( a6 )
printf(" SECURE");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func6(const char *a1, const char *a2, const char *a3, const char *a4, const char *a5, __int16 a6)
{
printf("Set-Cookie: %s=%s;", a1, a2);
if ( a3 )
printf(" EXPIRES=%s;", a3);
if ( a4 )
printf(" PATH=%s;", a4);
if ( a5 )
printf(" DOMAIN=%s;", a5);
if ( a6 )
printf(" SECURE");
return putchar(10);
}

original_code:
void set_cookie(char *name, char *value, char *expires, char *path,
  char *domain, short secure)
{
  printf("Set-Cookie: %s=%s;",name,value);
  if (expires != ((void *)0))
    printf(" EXPIRES=%s;",expires);
  if (path != ((void *)0))
    printf(" PATH=%s;",path);
  if (domain != ((void *)0))
    printf(" DOMAIN=%s;",domain);
  if (secure)
    printf(" SECURE");
  printf("\n");
}

variable_types:
name
char *

variable_types:
value
char *

variable_types:
expires
char *

variable_types:
path
char *

variable_types:
domain
char *

variable_types: secure short


decompiled_code:
int __fastcall html_begin(const char *a1)
{
puts("<html> <head>");
printf("<title>%s</title>\n", a1);
puts("</head>\n");
return puts("<body>");
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
puts("<html> <head>");
printf("<title>%s</title>\n", a1);
puts("</head>\n");
return puts("<body>");
}

original_code:
void html_begin(char *title)
{
  printf("<html> <head>\n");
  printf("<title>%s</title>\n",title);
  printf("</head>\n\n");
  printf("<body>\n");
}

variable_types:
title
char *


decompiled_code:
int html_end()
{
return puts("</body> </html>");
}

canonical_decompiled_code:
int func8()
{
return puts("</body> </html>");
}

original_code:
void html_end(void)
{
  printf("</body> </html>\n");
}

canonical_original_code:
void html_end()
{
  printf("</body> </html>\n");
}


decompiled_code:
int __fastcall h1(const char *a1)
{
return printf("<h1>%s</h1>\n", a1);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
return printf("<h1>%s</h1>\n", a1);
}

original_code:
void h1(char *header)
{
  printf("<h1>%s</h1>\n",header);
}

variable_types:
header
char *


decompiled_code:
int __fastcall h2(const char *a1)
{
return printf("<h2>%s</h2>\n", a1);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
return printf("<h2>%s</h2>\n", a1);
}

original_code:
void h2(char *header)
{
  printf("<h2>%s</h2>\n",header);
}


decompiled_code:
int __fastcall h3(const char *a1)
{
return printf("<h3>%s</h3>\n", a1);
}

canonical_decompiled_code:
int __fastcall func11(const char *a1)
{
return printf("<h3>%s</h3>\n", a1);
}

original_code:
void h3(char *header)
{
  printf("<h3>%s</h3>\n",header);
}


decompiled_code:
int __fastcall h4(const char *a1)
{
return printf("<h4>%s</h4>\n", a1);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
return printf("<h4>%s</h4>\n", a1);
}

original_code:
void h4(char *header)
{
  printf("<h4>%s</h4>\n",header);
}


decompiled_code:
int __fastcall h5(const char *a1)
{
return printf("<h5>%s</h5>\n", a1);
}

canonical_decompiled_code:
int __fastcall func13(const char *a1)
{
return printf("<h5>%s</h5>\n", a1);
}

original_code:
void h5(char *header)
{
  printf("<h5>%s</h5>\n",header);
}


decompiled_code:
int __fastcall h6(const char *a1)
{
return printf("<h6>%s</h6>\n", a1);
}

canonical_decompiled_code:
int __fastcall func14(const char *a1)
{
return printf("<h6>%s</h6>\n", a1);
}

original_code:
void h6(char *header)
{
  printf("<h6>%s</h6>\n",header);
}


hidden

decompiled_code:
int __fastcall hidden(const char *a1, const char *a2)
{
return printf("<input type=hidden name=\"%s\" value=\"%s\">\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func15(const char *a1, const char *a2)
{
return printf("<input type=hidden name=\"%s\" value=\"%s\">\n", a1, a2);
}

original_code:
void hidden(char *name, char *value)
{
  printf("<input type=hidden name=\"%s\" value=\"%s\">\n",name,value);
}


decompiled_code:
__int64 __fastcall list_next(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
node* list_next(node* w)
{
  return w->next;
}

canonical_original_code:
struct _node *list_next(struct _node *w)
{
  return w->next;
}

variable_types:
w
struct _node *

return_type: struct _node { struct entrytype { char *name; char *value; } entry; struct _node *next; } *


decompiled_code:
_BOOL8 __fastcall on_list(__int64 a1, __int64 a2)
{
return a2 != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, __int64 a2)
{
return a2 != 0;
}

original_code:
short on_list(llist *l, node* w)
{
  return (w != 0);
}

canonical_original_code:
short on_list(struct llist *l, struct _node *w)
{
  return (w != 0);
}

variable_types: struct llist { struct _node { struct entrytype { char *name; char *value; } entry; struct _node *next; } *head; } *

return_type: short


decompiled_code:
_BOOL8 __fastcall on_list_debug(__int64 *a1, __int64 a2)
{
__int64 i; // rax

if ( !a2 )
return 0LL;
for ( i = *a1; a2 != i; i = *(_QWORD *)(i + 16) )
{
if ( !i )
break;
}
return a2 == i;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 *a1, __int64 a2)
{
__int64 i; // rax

if ( !a2 )
return 0LL;
for ( i = *a1; a2 != i; i = *(_QWORD *)(i + 16) )
{
if ( !i )
break;
}
return a2 == i;
}

original_code:
short on_list_debug(llist *l, node* w)
{
  node* current;
  if (w == 0)
    return 0;
  else {
    current = l->head;
    while ( (current != w) && (current != 0) )
      current = current->next;
    if (current == w)
      return 1;
    else
      return 0;
  }
}

canonical_original_code:
short on_list_debug(struct llist *l, struct _node *w)
{
  struct _node *current;
  if (w == 0)
    return 0;
  else {
    current = l->head;
    while ( (current != w) && (current != 0) )
      current = current->next;
    if (current == w)
      return 1;
    else
      return 0;
  }
}

variable_types: struct _node { struct entrytype { char *name; char *value; } entry; struct _node *next; } *


original_code:
void list_clear(llist* l)
{
  node* nextnode;
  node* current;
  current = l->head;
  while (current) {
    nextnode=current->next;
    if (current->entry.name)
      free(current->entry.name);
    if (current->entry.value)
      free(current->entry.value);
    free(current);
    current=nextnode;
  }
}

canonical_original_code:
void list_clear(struct llist *l)
{
  struct _node *nextnode;
  struct _node *current;
  current = l->head;
  while (current) {
    nextnode=current->next;
    if (current->entry.name)
      free(current->entry.name);
    if (current->entry.value)
      free(current->entry.value);
    free(current);
    current=nextnode;
  }
}

user_defined_types: struct entrytype { char *name; char *value; }


newstr

decompiled_code:
void *__fastcall newstr(void *src)
{
size_t v1; // r12
void *v2; // rax
void *v3; // r8

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, v1);
return v3;
}

canonical_decompiled_code:
void *__fastcall func0(void *src)
{
size_t v1; // r12
void *v2; // rax
void *v3; // r8

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, v1);
return v3;
}

original_code:
char *newstr(char *str)
{
  char *tempstr = (char *)malloc(sizeof(char) * strlen(str) + 1);
  if (tempstr != ((void *)0))
    strcpy(tempstr,str);
  return tempstr;
}

variable_types:
str
char *

variable_types:
tempstr
char *


repo: wessels/pcap-tools

decompiled_code:
__int64 __fastcall tv_cmp(_QWORD *a1, _QWORD *a2)
{
__int64 result; // rax
__int64 v3; // rax
bool v4; // cc

if ( *a1 == *a2 )
{
v3 = a2[1];
v4 = a1[1] < v3;
result = a1[1] > v3;
if ( v4 )
result = 0xFFFFFFFFLL;
}
else
{
result = *a1 > *a2;
if ( *a1 < *a2 )
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, _QWORD *a2)
{
__int64 result; // rax
__int64 v3; // rax
bool v4; // cc

if ( *a1 == *a2 )
{
v3 = a2[1];
v4 = a1[1] < v3;
result = a1[1] > v3;
if ( v4 )
result = 0xFFFFFFFFLL;
}
else
{
result = *a1 > *a2;
if ( *a1 < *a2 )
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
tv_cmp(struct timeval *a, struct timeval *b)
{
    return (a->tv_sec == b->tv_sec) ? (a->tv_usec<b->tv_usec?-1:(a->tv_usec>b->tv_usec?1:0)) : (a->tv_sec<b->tv_sec?-1:(a->tv_sec>b->tv_sec?1:0));
}

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

variable_types:
b
struct timeval *


flush

decompiled_code:
void *__fastcall flush(int a1)
{
void *result; // rax

if ( a1 )
{
result = memmove(tvals, (char *)tvals + 16 * (count - (__int64)a1), 16LL * a1);
count = a1;
}
else
{
count = 0;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(int a1)
{
void *result; // rax

if ( a1 )
{
result = memmove(tvals, (char *)tvals + 16 * (count - (__int64)a1), 16LL * a1);
count = a1;
}
else
{
count = 0;
}
return result;
}

original_code:
void
flush(int keep)
{
    if (keep)
 memmove(tvals, tvals + count - keep, keep * sizeof(*tvals));
    count = keep;
}

variable_types: keep int


push

decompiled_code:
int __fastcall push(const __m128i *a1)
{
char *v1; // rsi
int result; // eax
__int64 v3; // rdx
_QWORD *v4; // rcx
char *v5; // rsi
_QWORD *v6; // rcx

if ( count >= ARRAYSZ )
__assert_fail("count < ARRAYSZ", "pcap-find-misordered.c", 0x28u, "push");
v1 = (char *)tvals;
result = count + 1;
v3 = 16LL * count;
v4 = (char *)tvals + v3;
*(__m128i *)((char *)tvals + v3) = _mm_loadu_si128(a1);
count = result;
if ( result > 1 )
{
v5 = &v1[v3 - 16];
result = tv_cmp(v4, v5);
if ( result < 0 )
{
result = printf(
"timestamp went from %10lld.%06ld to %10lld.%06ld\n",
*(_QWORD *)v5,
*((_QWORD *)v5 + 1),
*v6,
v6[1]);
++misordered;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(const __m128i *a1)
{
char *v1; // rsi
int result; // eax
__int64 v3; // rdx
_QWORD *v4; // rcx
char *v5; // rsi
_QWORD *v6; // rcx

if ( count >= ARRAYSZ )
__assert_fail("count < ARRAYSZ", "pcap-find-misordered.c", 0x28u, "push");
v1 = (char *)tvals;
result = count + 1;
v3 = 16LL * count;
v4 = (char *)tvals + v3;
*(__m128i *)((char *)tvals + v3) = _mm_loadu_si128(a1);
count = result;
if ( result > 1 )
{
v5 = &v1[v3 - 16];
result = func7(v4, v5);
if ( result < 0 )
{
result = printf(
"timestamp went from %10lld.%06ld to %10lld.%06ld\n",
*(_QWORD *)v5,
*((_QWORD *)v5 + 1),
*v6,
v6[1]);
++misordered;
}
}
return result;
}

original_code:
void
push(struct pcap_pkthdr *hdr, const u_char * data)
{
    ((void) sizeof ((count < ARRAYSZ) ? 1 : 0), __extension__ ({ if (count < ARRAYSZ) ; else __assert_fail ("count < ARRAYSZ", "pcap-find-misordered.c", 40, __extension__ __PRETTY_FUNCTION__); }));
    *(tvals + count) = hdr->ts;
    count++;
    if (count < 2)
 return;
    if (0 > tv_cmp(tvals + count - 1, tvals + count - 2)) {
 printf("timestamp went from %10lld.%06ld to %10lld.%06ld\n",
     (long long int) (tvals + count - 2)->tv_sec,
     (tvals + count - 2)->tv_usec, (long long int) (tvals + count - 1)->tv_sec, (tvals + count - 1)->tv_usec);
 misordered++;
    }
}

canonical_original_code:
void push(struct pcap_pkthdr *hdr, unsigned char *data)
{
    ((void) sizeof ((count < ARRAYSZ) ? 1 : 0), __extension__ ({ if (count < ARRAYSZ) ; else __assert_fail ("count < ARRAYSZ", "pcap-find-misordered.c", 40, __extension__ __PRETTY_FUNCTION__); }));
    *(tvals + count) = hdr->ts;
    count++;
    if (count < 2)
 return;
    if (0 > tv_cmp(tvals + count - 1, tvals + count - 2)) {
 printf("timestamp went from %10lld.%06ld to %10lld.%06ld\n",
     (long long) (tvals + count - 2)->tv_sec,
     (tvals + count - 2)->tv_usec, (long long) (tvals + count - 1)->tv_sec, (tvals + count - 1)->tv_usec);
 misordered++;
    }
}

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
data
unsigned char *

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


decompiled_code:
int __fastcall qsort_strcmp(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
int __fastcall func0(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
int
qsort_strcmp(const void *a, const void *b)
{
    return strcmp(*((char **) a), *((char **) b));
}

canonical_original_code:
int qsort_strcmp(void *a, void *b)
{
    return strcmp(*((char **) a), *((char **) b));
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
time_t __fastcall parse_timestamp(char *string)
{
unsigned int v2; // ebp
__int64 *v3; // rbx
const char *v4; // r13
const regex_t *v5; // rdi
time_t result; // rax
regex_t *v7; // rax
struct tm v8; // [rsp+0h] [rbp-B8h] BYREF
regmatch_t v9; // [rsp+40h] [rbp-78h] BYREF

v2 = 0;
v3 = time_patterns;
v4 = (const char *)time_patterns[1];
if ( v4 )
{
while ( 1 )
{
v5 = (const regex_t *)v3[3];
if ( !v5 )
{
v7 = (regex_t *)calloc(1uLL, 0x40uLL);
v3[3] = (__int64)v7;
if ( regcomp(v7, v4, 1) )
{
fprintf(stderr, "regcomp '%s' failed\n", (const char *)time_patterns[4 * v2 + 1]);
exit(1);
}
v5 = (const regex_t *)v3[3];
}
if ( !regexec(v5, string, *(int *)v3, &v9, 0) )
{
if ( v9.rm_so < 0 )
_assert_fail("pmatch[0].rm_so > -1", "pcap-join.c", 0x62u, "parse_timestamp");
if ( (unsigned int)strptime(&string[v9.rm_so], (const char *)v3[2], &v8) )
break;
}
v4 = (const char *)v3[5];
v3 += 4;
++v2;
if ( !v4 )
goto LABEL_8;
}
result = timegm(&v8);
}
else
{
LABEL_8:
fprintf(stderr, "no time patterns matched '%s'\n", string);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
time_t __fastcall func2(char *string)
{
unsigned int v2; // ebp
__int64 *v3; // rbx
const char *v4; // r13
const regex_t *v5; // rdi
time_t result; // rax
regex_t *v7; // rax
struct tm v8; // [rsp+0h] [rbp-B8h] BYREF
regmatch_t v9; // [rsp+40h] [rbp-78h] BYREF

v2 = 0;
v3 = time_patterns;
v4 = (const char *)time_patterns[1];
if ( v4 )
{
while ( 1 )
{
v5 = (const regex_t *)v3[3];
if ( !v5 )
{
v7 = (regex_t *)calloc(1uLL, 0x40uLL);
v3[3] = (__int64)v7;
if ( regcomp(v7, v4, 1) )
{
fprintf(stderr, "regcomp '%s' failed\n", (const char *)time_patterns[4 * v2 + 1]);
exit(1);
}
v5 = (const regex_t *)v3[3];
}
if ( !regexec(v5, string, *(int *)v3, &v9, 0) )
{
if ( v9.rm_so < 0 )
_assert_fail("pmatch[0].rm_so > -1", "pcap-join.c", 0x62u, "parse_timestamp");
if ( (unsigned int)strptime(&string[v9.rm_so], (const char *)v3[2], &v8) )
break;
}
v4 = (const char *)v3[5];
v3 += 4;
++v2;
if ( !v4 )
goto LABEL_8;
}
result = timegm(&v8);
}
else
{
LABEL_8:
fprintf(stderr, "no time patterns matched '%s'\n", string);
result = 0LL;
}
return result;
}

original_code:
time_t
parse_timestamp(const char *s)
{
    regmatch_t pmatch[10];
    struct tm tm;
    unsigned int k;
    for (k = 0; time_patterns[k].pat; k++) {
 if (0 == time_patterns[k].re) {
     time_patterns[k].re = calloc(1, sizeof(*time_patterns[k].re));
     if (0 != regcomp(time_patterns[k].re, time_patterns[k].pat, 1)) {
  fprintf(stderr, "regcomp '%s' failed\n", time_patterns[k].pat);
  exit(1);
     }
 }
 if (0 != regexec(time_patterns[k].re, s, time_patterns[k].nmatch, pmatch, 0)) {
     continue;
 }
 ((void) sizeof ((pmatch[0].rm_so > -1) ? 1 : 0), __extension__ ({ if (pmatch[0].rm_so > -1) ; else __assert_fail ("pmatch[0].rm_so > -1", "pcap-join.c", 98, __extension__ __PRETTY_FUNCTION__); }));
 if (0 == strptime(s + pmatch[0].rm_so, time_patterns[k].fmt, &tm))
     continue;
 return timegm(&tm);
    }
    fprintf(stderr, "no time patterns matched '%s'\n", s);
    return 0;
}

canonical_original_code:
long parse_timestamp(char *s)
{
    struct regmatch_t pmatch[10];
    struct tm tm;
    unsigned int k;
    for (k = 0; time_patterns[k].pat; k++) {
 if (0 == time_patterns[k].re) {
     time_patterns[k].re = calloc(1, sizeof(*time_patterns[k].re));
     if (0 != regcomp(time_patterns[k].re, time_patterns[k].pat, 1)) {
  fprintf(stderr, "regcomp '%s' failed\n", time_patterns[k].pat);
  exit(1);
     }
 }
 if (0 != regexec(time_patterns[k].re, s, time_patterns[k].nmatch, pmatch, 0)) {
     continue;
 }
 ((void) sizeof ((pmatch[0].rm_so > -1) ? 1 : 0), __extension__ ({ if (pmatch[0].rm_so > -1) ; else __assert_fail ("pmatch[0].rm_so > -1", "pcap-join.c", 98, __extension__ __PRETTY_FUNCTION__); }));
 if (0 == strptime(s + pmatch[0].rm_so, time_patterns[k].fmt, &tm))
     continue;
 return timegm(&tm);
    }
    fprintf(stderr, "no time patterns matched '%s'\n", s);
    return 0;
}

variable_types:
s
char *

variable_types: struct regmatch_t { int rm_so; int rm_eo; } [10]

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

variable_types:
k
unsigned int

return_type: long


decompiled_code:
__int64 __fastcall qsort_strptime(const void *a1, const void *a2)
{
time_t v2; // rbx

v2 = parse_timestamp(*(char **)a1);
return 2 * (unsigned int)(v2 > parse_timestamp(*(char **)a2)) - 1;
}

canonical_decompiled_code:
__int64 __fastcall func3(const void *a1, const void *a2)
{
time_t v2; // rbx

v2 = func2(*(char **)a1);
return 2 * (unsigned int)(v2 > func2(*(char **)a2)) - 1;
}

original_code:
int
qsort_strptime(const void *a, const void *b)
{
    time_t time_a = parse_timestamp(*((char **) a));
    time_t time_b = parse_timestamp(*((char **) b));
    if (time_a < time_b)
 return -1;
    if (time_a > time_b)
 return 1;
    return -1;
}

canonical_original_code:
int qsort_strptime(void *a, void *b)
{
    long time_a = parse_timestamp(*((char **) a));
    long time_b = parse_timestamp(*((char **) b));
    if (time_a < time_b)
 return -1;
    if (time_a > time_b)
 return 1;
    return -1;
}


decompiled_code:
_QWORD *filelist_init()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = calloc(1uLL, 0x10uLL);
*v0 = 64LL;
v1 = v0;
v0[1] = calloc(0x40uLL, 8uLL);
return v1;
}

canonical_decompiled_code:
_QWORD *func5()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = calloc(1uLL, 0x10uLL);
*v0 = 64LL;
v1 = v0;
v0[1] = calloc(0x40uLL, 8uLL);
return v1;
}

original_code:
filelist *
filelist_init(void)
{
    filelist *fl;
    fl = calloc(1, sizeof(*fl));
    fl->size = 64;
    fl->cnt = 0;
    fl->files = calloc(fl->size, sizeof(char *));
    return fl;
}

canonical_original_code:
struct filelist *filelist_init()
{
    struct filelist *fl;
    fl = calloc(1, sizeof(*fl));
    fl->size = 64;
    fl->cnt = 0;
    fl->files = calloc(fl->size, sizeof(char *));
    return fl;
}

variable_types: struct filelist { unsigned int size; unsigned int cnt; char **files; } *


decompiled_code:
char *__fastcall filelist_add(__int64 a1, const char *a2)
{
__int64 v2; // rbp
_QWORD *v3; // r13
char *result; // rax
_QWORD *v5; // rax
void *v6; // r14

v2 = *(unsigned int *)(a1 + 4);
if ( (_DWORD)v2 == *(_DWORD *)a1 )
{
v5 = calloc((unsigned int)(2 * v2), 8uLL);
v6 = *(void **)(a1 + 8);
v3 = v5;
memcpy(v5, v6, 8LL * (unsigned int)v2);
free(v6);
*(_QWORD *)(a1 + 8) = v3;
v2 = *(unsigned int *)(a1 + 4);
*(_DWORD *)a1 *= 2;
}
else
{
v3 = *(_QWORD **)(a1 + 8);
}
*(_DWORD *)(a1 + 4) = v2 + 1;
result = strdup(a2);
v3[v2] = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1, const char *a2)
{
__int64 v2; // rbp
_QWORD *v3; // r13
char *result; // rax
_QWORD *v5; // rax
void *v6; // r14

v2 = *(unsigned int *)(a1 + 4);
if ( (_DWORD)v2 == *(_DWORD *)a1 )
{
v5 = calloc((unsigned int)(2 * v2), 8uLL);
v6 = *(void **)(a1 + 8);
v3 = v5;
memcpy(v5, v6, 8LL * (unsigned int)v2);
free(v6);
*(_QWORD *)(a1 + 8) = v3;
v2 = *(unsigned int *)(a1 + 4);
*(_DWORD *)a1 *= 2;
}
else
{
v3 = *(_QWORD **)(a1 + 8);
}
*(_DWORD *)(a1 + 4) = v2 + 1;
result = strdup(a2);
v3[v2] = result;
return result;
}

original_code:
void
filelist_add(filelist * fl, const char *file)
{
    if (fl->cnt == fl->size) {
 char **tmp = calloc(fl->size << 1, sizeof(char *));
 memcpy(tmp, fl->files, fl->size * sizeof(char *));
 free(fl->files);
 fl->files = tmp;
 fl->size <<= 1;
    }
    fl->files[fl->cnt++] = strdup(file);
}

canonical_original_code:
void filelist_add(struct filelist *fl, char *file)
{
    if (fl->cnt == fl->size) {
 char **tmp = calloc(fl->size << 1, sizeof(char *));
 memcpy(tmp, fl->files, fl->size * sizeof(char *));
 free(fl->files);
 fl->files = tmp;
 fl->size <<= 1;
    }
    fl->files[fl->cnt++] = strdup(file);
}

variable_types:
file
char *

variable_types:
tmp
char **

user_defined_types: struct filelist { unsigned int size; unsigned int cnt; char **files; }


usage

decompiled_code:
void __noreturn usage()
{
fwrite("usage: pcap-join [options] pcapfiles ...\n", 1uLL, 0x29uLL, stderr);
fwrite(" pcap-join [options] directory\n", 1uLL, 0x25uLL, stderr);
fwrite(" pcap-join [options] (reads files from stdin)\n", 1uLL, 0x34uLL, stderr);
fwrite("options:\n", 1uLL, 9uLL, stderr);
fwrite(" -v verbose\n", 1uLL, 0x1CuLL, stderr);
fwrite(" -b filter apply pcap filter\n", 1uLL, 0x26uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func4()
{
fwrite("usage: pcap-join [options] pcapfiles ...\n", 1uLL, 0x29uLL, stderr);
fwrite(" pcap-join [options] directory\n", 1uLL, 0x25uLL, stderr);
fwrite(" pcap-join [options] (reads files from stdin)\n", 1uLL, 0x34uLL, stderr);
fwrite("options:\n", 1uLL, 9uLL, stderr);
fwrite(" -v verbose\n", 1uLL, 0x1CuLL, stderr);
fwrite(" -b filter apply pcap filter\n", 1uLL, 0x26uLL, stderr);
exit(1);
}

original_code:
void
usage(void)
{
    fprintf(stderr, "usage: pcap-join [options] pcapfiles ...\n");
    fprintf(stderr, "       pcap-join [options] directory\n");
    fprintf(stderr, "       pcap-join [options] (reads files from stdin)\n");
    fprintf(stderr, "options:\n");
    fprintf(stderr, "       -v           verbose\n");
    fprintf(stderr, "       -b filter    apply pcap filter\n");
    exit(1);
}

canonical_original_code:
void usage()
{
    fprintf(stderr, "usage: pcap-join [options] pcapfiles ...\n");
    fprintf(stderr, "       pcap-join [options] directory\n");
    fprintf(stderr, "       pcap-join [options] (reads files from stdin)\n");
    fprintf(stderr, "options:\n");
    fprintf(stderr, "       -v           verbose\n");
    fprintf(stderr, "       -b filter    apply pcap filter\n");
    exit(1);
}


decompiled_code:
_BOOL8 __fastcall is_dupe(const __m128i *a1, const void *a2)
{
_BOOL4 v2; // er12
size_t v3; // rdx
__int64 v4; // rax
__m128i v5; // xmm1

v2 = 0;
MD5_Init();
MD5_Update();
MD5_Final();
v3 = a1[1].m128i_u32[1];
if ( *a1 == lasthdr_2 && qword_405160 == a1[1].m128i_i64[0] )
v2 = lastdigest_1 == thisdigest_3;
v4 = a1[1].m128i_i64[0];
lastdigest_1 = (__int128)_mm_load_si128((const __m128i *)&thisdigest_3);
v5 = _mm_loadu_si128(a1);
qword_405160 = v4;
lasthdr_2 = (__int128)v5;
memcpy(&lastdata_0, a2, v3);
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(const __m128i *a1, const void *a2)
{
_BOOL4 v2; // er12
size_t v3; // rdx
__int64 v4; // rax
__m128i v5; // xmm1

v2 = 0;
MD5_Init();
MD5_Update();
MD5_Final();
v3 = a1[1].m128i_u32[1];
if ( *a1 == lasthdr_2 && qword_405160 == a1[1].m128i_i64[0] )
v2 = lastdigest_1 == thisdigest_3;
v4 = a1[1].m128i_i64[0];
lastdigest_1 = (__int128)_mm_load_si128((const __m128i *)&thisdigest_3);
v5 = _mm_loadu_si128(a1);
qword_405160 = v4;
lasthdr_2 = (__int128)v5;
memcpy(&lastdata_0, a2, v3);
return v2;
}

original_code:
int
is_dupe(struct pcap_pkthdr *thishdr, const u_char * thisdata)
{
    static struct pcap_pkthdr lasthdr;
    static char lastdata[4096];
    MD5_CTX md5;
    static unsigned int thisdigest[4];
    static unsigned int lastdigest[4];
    int rc = 0;
    MD5_Init(&md5);
    MD5_Update(&md5, thisdata, thishdr->len);
    MD5_Final((u_char *) thisdigest, &md5);
    if (thishdr->ts.tv_usec != lasthdr.ts.tv_usec)
 (void) 0;
    else if (thishdr->ts.tv_sec != lasthdr.ts.tv_sec)
 (void) 0;
    else if (thishdr->caplen != lasthdr.caplen)
 (void) 0;
    else if (thishdr->len != lasthdr.len)
 (void) 0;
    else {
 if (memcmp(thisdigest, lastdigest, 16) == 0)
     rc = 1;
    }
    memcpy(lastdigest, thisdigest, 16);
    lasthdr = *thishdr;
    memcpy(lastdata, thisdata, thishdr->len);
    return rc;
}

canonical_original_code:
int is_dupe(struct pcap_pkthdr *thishdr, unsigned char *thisdata)
{
    static struct pcap_pkthdr lasthdr;
    static char lastdata[4096];
    struct MD5state_st md5;
    static unsigned int thisdigest[4];
    static unsigned int lastdigest[4];
    int rc = 0;
    MD5_Init(&md5);
    MD5_Update(&md5, thisdata, thishdr->len);
    MD5_Final((unsigned char *) thisdigest, &md5);
    if (thishdr->ts.tv_usec != lasthdr.ts.tv_usec)
 (void) 0;
    else if (thishdr->ts.tv_sec != lasthdr.ts.tv_sec)
 (void) 0;
    else if (thishdr->caplen != lasthdr.caplen)
 (void) 0;
    else if (thishdr->len != lasthdr.len)
 (void) 0;
    else {
 if (memcmp(thisdigest, lastdigest, 16) == 0)
     rc = 1;
    }
    memcpy(lastdigest, thisdigest, 16);
    lasthdr = *thishdr;
    memcpy(lastdata, thisdata, thishdr->len);
    return rc;
}

variable_types:
thisdata
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; }

variable_types:
lastdata
char [4096]

variable_types: struct MD5state_st { unsigned int A; unsigned int Nl; unsigned int data[-1]; unsigned int num; }

variable_types:
thisdigest
unsigned int [4]

variable_types:
lastdigest
unsigned int [4]

variable_types: rc int


help

decompiled_code:
int help()
{
fprintf(stderr, "%s\n", "pcap-split");
return fprintf(
stderr,
"\n"
"options:\n"
"\t-? or -h print these instructions and exit\n"
"\t-B YYYY-MM-DD HH:MM:SS select packets starting on that date\n"
"\t-E YYYY-MM-DD HH:MM:SS select packets until this time/date\n"
"\t-t <interval> each <interval> seconds since\n"
"\t the start time indicated by '-B'\n"
"\t will close the old destination file\n"
"\t and create a new one.\n"
"\t-n <count> make new output file every <count> packets\n"
"\t-f <format> receives a format accepted by\n"
"\t strftime to name the files created.\n"
"\t Default %%s (UNIX timestamp)\n"
"\t-k <kick command> After closing an old destination\n"
"\t file, will execute this command with\n"
"\t the file name as first parameter\n"
"\t-v produce slightly more verbose output\n"
"\t-z automatcially compress each pcap\n"
"\t with gzip\n");
}

canonical_decompiled_code:
int func0()
{
fprintf(stderr, "%s\n", "pcap-split");
return fprintf(
stderr,
"\n"
"options:\n"
"\t-? or -h print these instructions and exit\n"
"\t-B YYYY-MM-DD HH:MM:SS select packets starting on that date\n"
"\t-E YYYY-MM-DD HH:MM:SS select packets until this time/date\n"
"\t-t <interval> each <interval> seconds since\n"
"\t the start time indicated by '-B'\n"
"\t will close the old destination file\n"
"\t and create a new one.\n"
"\t-n <count> make new output file every <count> packets\n"
"\t-f <format> receives a format accepted by\n"
"\t strftime to name the files created.\n"
"\t Default %%s (UNIX timestamp)\n"
"\t-k <kick command> After closing an old destination\n"
"\t file, will execute this command with\n"
"\t the file name as first parameter\n"
"\t-v produce slightly more verbose output\n"
"\t-z automatcially compress each pcap\n"
"\t with gzip\n");
}

original_code:
static void
help(void)
{
    fprintf(stderr, "%s\n", ProgramName);
    fprintf(stderr,
 "\noptions:\n"
 "\t-? or -h  print these instructions and exit\n"
 "\t-B YYYY-MM-DD HH:MM:SS        select packets starting on that date\n"
 "\t-E YYYY-MM-DD HH:MM:SS        select packets until this time/date\n"
 "\t-t <interval>                 each <interval> seconds since\n"
 "\t                              the start time indicated by '-B'\n"
 "\t                              will close the old destination file\n"
 "\t                              and create a new one.\n"
 "\t-n <count>                    make new output file every <count> packets\n"
 "\t-f <format>                   receives a format accepted by\n"
 "\t                              strftime to name the files created.\n"
 "\t                              Default %%s (UNIX timestamp)\n"
 "\t-k <kick command>             After closing an old destination\n"
 "\t                              file, will execute this command with\n"
 "\t                              the file name as first parameter\n"
 "\t-v                            produce slightly more verbose output\n"
 "\t-z                            automatcially compress each pcap\n"
 "\t                              with gzip\n");
}

canonical_original_code:
static void help()
{
    fprintf(stderr, "%s\n", ProgramName);
    fprintf(stderr,
 "\noptions:\n"
 "\t-? or -h  print these instructions and exit\n"
 "\t-B YYYY-MM-DD HH:MM:SS        select packets starting on that date\n"
 "\t-E YYYY-MM-DD HH:MM:SS        select packets until this time/date\n"
 "\t-t <interval>                 each <interval> seconds since\n"
 "\t                              the start time indicated by '-B'\n"
 "\t                              will close the old destination file\n"
 "\t                              and create a new one.\n"
 "\t-n <count>                    make new output file every <count> packets\n"
 "\t-f <format>                   receives a format accepted by\n"
 "\t                              strftime to name the files created.\n"
 "\t                              Default %%s (UNIX timestamp)\n"
 "\t-k <kick command>             After closing an old destination\n"
 "\t                              file, will execute this command with\n"
 "\t                              the file name as first parameter\n"
 "\t-v                            produce slightly more verbose output\n"
 "\t-z                            automatcially compress each pcap\n"
 "\t                              with gzip\n");
}


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(stderr, "%s: usage error: %s\n", "pcap-split", a1);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
fprintf(stderr, "%s: usage error: %s\n", "pcap-split", a1);
fputc(10, stderr);
exit(1);
}

original_code:
static void
usage(const char *msg)
{
    fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg);
    fprintf(stderr, "\n");
    exit(1);
}

canonical_original_code:
static void usage(char *msg)
{
    fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg);
    fprintf(stderr, "\n");
    exit(1);
}

variable_types:
msg
char *


decompiled_code:
_BOOL8 __fastcall is_bogus(__int64 a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( *(_DWORD *)(a1 + 16) <= 0xFFFFu )
result = *(_DWORD *)(a1 + 20) > 0xFFFFu;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(__int64 a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( *(_DWORD *)(a1 + 16) <= 0xFFFFu )
result = *(_DWORD *)(a1 + 20) > 0xFFFFu;
return result;
}

original_code:
int
is_bogus(struct pcap_pkthdr *thishdr, const u_char * thisdata)
{
    if (thishdr->caplen > 65535)
 return 1;
    if (thishdr->len > 65535)
 return 1;
    return 0;
}

canonical_original_code:
int is_bogus(struct pcap_pkthdr *thishdr, unsigned char *thisdata)
{
    if (thishdr->caplen > 65535)
 return 1;
    if (thishdr->len > 65535)
 return 1;
    return 0;
}

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *


WfaRcvStop

repo: wfapf/TDLS

decompiled_code:
__int64 __fastcall WfaRcvStop(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // rbx
char *v3; // rbp
pthread_mutex_t *v5; // r13
pthread_cond_t *v6; // rbx
int v7; // edi

v1 = *((int *)&wmmps_info + 10);
v2 = 50 * v1;
v3 = (char *)&wmm_thr[50 * v1];
*((_DWORD *)v3 + 1) = 0;
if ( *(_DWORD *)(a1 + 40) != 42 )
return 0LL;
v5 = (pthread_mutex_t *)&wmm_thr[v2 + 40];
v6 = (pthread_cond_t *)&wmm_thr[v2 + 18];
pthread_mutex_lock(v5);
while ( !*((_DWORD *)v3 + 1) )
pthread_cond_wait(v6, v5);
num_stops = 0;
pthread_mutex_unlock(v5);
v7 = psSockfd;
gtgWmmPS = 0;
wmm_thr[50 * v1 + 1] = 0;
close(v7);
psSockfd = -1;
signal(14, (__sighandler_t)((char *)WfaRcvStop + 1));
sleep(1u);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // rbx
char *v3; // rbp
pthread_mutex_t *v5; // r13
pthread_cond_t *v6; // rbx
int v7; // edi

v1 = *((int *)&wmmps_info + 10);
v2 = 50 * v1;
v3 = (char *)&wmm_thr[50 * v1];
*((_DWORD *)v3 + 1) = 0;
if ( *(_DWORD *)(a1 + 40) != 42 )
return 0LL;
v5 = (pthread_mutex_t *)&wmm_thr[v2 + 40];
v6 = (pthread_cond_t *)&wmm_thr[v2 + 18];
pthread_mutex_lock(v5);
while ( !*((_DWORD *)v3 + 1) )
pthread_cond_wait(v6, v5);
num_stops = 0;
pthread_mutex_unlock(v5);
v7 = psSockfd;
gtgWmmPS = 0;
wmm_thr[50 * v1 + 1] = 0;
close(v7);
psSockfd = -1;
signal(14, (__sighandler_t)((char *)WfaRcvStop + 1));
sleep(1u);
return 0LL;
}

original_code:
int WfaRcvStop(unsigned int *rmsg,int length,int *state)
{
   tgWMM_t *my_wmm = &wmm_thr[wmmps_info.ps_thread];
   my_wmm->stop_flag = 0;
   ;;
   if(rmsg[10] != (((((37 + 0x01) + 0x01) + 0x01) + 0x01) + 0x01))
   {
      ;;
   }
   else
   {
      pthread_mutex_lock(&my_wmm->thr_stop_mutex);
      while(!my_wmm->stop_flag)
      {
         pthread_cond_wait(&my_wmm->thr_stop_cond, &my_wmm->thr_stop_mutex);
      }
      num_stops=0;
      pthread_mutex_unlock(&my_wmm->thr_stop_mutex);
      my_wmm->stop_flag = 0;
      gtgWmmPS = 0;
      close(psSockfd);
      psSockfd = -1;
      signal(14, ((__sighandler_t) 1));
      sleep(1);
   }
   return 0;
}

canonical_original_code:
int WfaRcvStop(unsigned int *rmsg, int length, int *state)
{
   struct _tg_wmm *my_wmm = &wmm_thr[wmmps_info.ps_thread];
   my_wmm->stop_flag = 0;
   ;;
   if(rmsg[10] != (((((37 + 0x01) + 0x01) + 0x01) + 0x01) + 0x01))
   {
      ;;
   }
   else
   {
      pthread_mutex_lock(&my_wmm->thr_stop_mutex);
      while(!my_wmm->stop_flag)
      {
         pthread_cond_wait(&my_wmm->thr_stop_cond, &my_wmm->thr_stop_mutex);
      }
      num_stops=0;
      pthread_mutex_unlock(&my_wmm->thr_stop_mutex);
      my_wmm->stop_flag = 0;
      gtgWmmPS = 0;
      close(psSockfd);
      psSockfd = -1;
      signal(14, ((void (*)(int)) 1));
      sleep(1);
   }
   return 0;
}

variable_types:
rmsg
unsigned int *

variable_types: length int

variable_types:
state
int *

variable_types: struct _tg_wmm { int thr_flag; int stop_flag; unsigned long thr; int thr_id; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } thr_flag_cond; union pthread_cond_t thr_stop_cond; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } thr_flag_mutex; union pthread_mutex_t thr_stop_mutex; } *

user_defined_types: struct _tg_wmm { int thr_flag; int stop_flag; unsigned long thr; int thr_id; union pthread_cond_t thr_flag_cond; union pthread_cond_t thr_stop_cond; union pthread_mutex_t thr_flag_mutex; union pthread_mutex_t thr_stop_mutex; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }