decompiled_code:
__int64 __fastcall fibheap_increase(__int64 *a1, _QWORD *a2)
{
_QWORD *v2; // r12
__int64 result; // rax
const char *v4; // rdx

if ( !a1 )
{
v4 = "fibheap_increase: H==NULL";
LABEL_10:
fprintf(stderr, "fib_heap.c: %s\n", v4);
exit(1);
}
if ( !a2 )
{
v4 = "fibheap_increase: node==NULL";
goto LABEL_10;
}
v2 = (_QWORD *)a2[2];
if ( v2 && ((int (__fastcall *)(_QWORD, _QWORD))a1[2])(*v2, *a2) > 0 )
{
fibheap_cut_isra_0(*a1, (__int64)a2, (__int64)v2);
fibheap_casc_cut(a1, (__int64)v2);
}
result = ((__int64 (__fastcall *)(_QWORD, _QWORD))a1[2])(*(_QWORD *)*a1, *a2);
if ( (int)result > 0 )
*a1 = (__int64)a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 *a1, _QWORD *a2)
{
_QWORD *v2; // r12
__int64 result; // rax
const char *v4; // rdx

if ( !a1 )
{
v4 = "fibheap_increase: H==NULL";
LABEL_10:
fprintf(stderr, "fib_heap.c: %s\n", v4);
exit(1);
}
if ( !a2 )
{
v4 = "fibheap_increase: node==NULL";
goto LABEL_10;
}
v2 = (_QWORD *)a2[2];
if ( v2 && ((int (__fastcall *)(_QWORD, _QWORD))a1[2])(*v2, *a2) > 0 )
{
func4(*a1, (__int64)a2, (__int64)v2);
func5(a1, (__int64)v2);
}
result = ((__int64 (__fastcall *)(_QWORD, _QWORD))a1[2])(*(_QWORD *)*a1, *a2);
if ( (int)result > 0 )
*a1 = (__int64)a2;
return result;
}

original_code:
void
fibheap_increase(struct fib_heap *H, struct fib_heap_node *node)
{
 struct fib_heap_node *y;
 ({ if(!(H != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_increase: H==NULL")); exit(1); }; });
 ({ if(!(node != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_increase: node==NULL")); exit(1); }; });
 y = node->parent;
 if(y!=((void *)0) && ((H->compr)(y->data, node->data) > 0)) {
  fibheap_cut(H, node, y);
  fibheap_casc_cut(H, y);
 }
 if((H->compr)(H->the_one->data, node->data) > 0)
  H->the_one = node;
}

variable_types:
node
struct fib_heap_node *


repo: iliastsi/fibheap

decompiled_code:
__int64 __fastcall fibheap_decrease(__int64 a1, __int64 a2)
{
int v3; // edx
__int64 v4; // rsi
__int64 result; // rax
__int64 v6; // r13
_QWORD *i; // rbx
const char *v8; // rdx

if ( !a1 )
{
v8 = "fibheap_decrease: H==NULL";
goto LABEL_18;
}
if ( !a2 )
{
v8 = "fibheap_decrease: node==NULL";
LABEL_18:
fprintf(stderr, "fib_heap.c: %s\n", v8);
exit(1);
}
v3 = *(_DWORD *)(a2 + 8);
v4 = *(_QWORD *)(a2 + 24);
result = 0LL;
if ( v3 )
{
do
{
result = (unsigned int)(result + 1);
*(_QWORD *)(v4 + 16) = 0LL;
v4 = *(_QWORD *)(v4 + 40);
}
while ( (_DWORD)result != v3 );
*(_QWORD *)(a2 + 24) = v4;
}
if ( v4 )
result = dblink_list_concat(*(_QWORD *)a1, v4);
v6 = *(_QWORD *)(a2 + 16);
*(_QWORD *)(a2 + 24) = 0LL;
*(_DWORD *)(a2 + 8) = 0;
if ( v6 )
{
fibheap_cut_isra_0(*(_QWORD *)a1, a2, v6);
result = fibheap_casc_cut((__int64 *)a1, v6);
}
else if ( *(_QWORD *)a1 == a2 )
{
for ( i = *(_QWORD **)(a2 + 40); (_QWORD *)a2 != i; i = (_QWORD *)i[5] )
{
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 16))(*(_QWORD *)a2, *i);
if ( (int)result > 0 )
*(_QWORD *)a1 = i;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
int v3; // edx
__int64 v4; // rsi
__int64 result; // rax
__int64 v6; // r13
_QWORD *i; // rbx
const char *v8; // rdx

if ( !a1 )
{
v8 = "fibheap_decrease: H==NULL";
goto LABEL_18;
}
if ( !a2 )
{
v8 = "fibheap_decrease: node==NULL";
LABEL_18:
fprintf(stderr, "fib_heap.c: %s\n", v8);
exit(1);
}
v3 = *(_DWORD *)(a2 + 8);
v4 = *(_QWORD *)(a2 + 24);
result = 0LL;
if ( v3 )
{
do
{
result = (unsigned int)(result + 1);
*(_QWORD *)(v4 + 16) = 0LL;
v4 = *(_QWORD *)(v4 + 40);
}
while ( (_DWORD)result != v3 );
*(_QWORD *)(a2 + 24) = v4;
}
if ( v4 )
result = func1(*(_QWORD *)a1, v4);
v6 = *(_QWORD *)(a2 + 16);
*(_QWORD *)(a2 + 24) = 0LL;
*(_DWORD *)(a2 + 8) = 0;
if ( v6 )
{
func4(*(_QWORD *)a1, a2, v6);
result = func5((__int64 *)a1, v6);
}
else if ( *(_QWORD *)a1 == a2 )
{
for ( i = *(_QWORD **)(a2 + 40); (_QWORD *)a2 != i; i = (_QWORD *)i[5] )
{
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 16))(*(_QWORD *)a2, *i);
if ( (int)result > 0 )
*(_QWORD *)a1 = i;
}
}
return result;
}

original_code:
void
fibheap_decrease(struct fib_heap *H, struct fib_heap_node *node)
{
 unsigned int i;
 struct fib_heap_node *y;
 ({ if(!(H != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_decrease: H==NULL")); exit(1); }; });
 ({ if(!(node != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_decrease: node==NULL")); exit(1); }; });
 for(i=0U;i<node->degree;i++) {
  node->child->parent = ((void *)0);
  node->child = node->child->right;
 }
 if(node->child != ((void *)0))
  dblink_list_concat(H->the_one, node->child);
 node->child = ((void *)0);
 node->degree = 0U;
 y = node->parent;
 if(y != ((void *)0)) {
  fibheap_cut(H, node, y);
  fibheap_casc_cut(H, y);
 } else if(H->the_one == node) {
  y = node->right;
  while(y != node) {
   if((H->compr)(node->data, y->data) > 0)
    H->the_one = y;
   y = y->right;
  }
 }
}

variable_types: struct fib_heap { struct fib_heap_node { struct fib_heap_data { int key; } *data; unsigned int degree; char marked; struct fib_heap_node *parent; struct fib_heap_node *child; struct fib_heap_node *left; struct fib_heap_node *right; } *the_one; struct fib_heap_node **cons_array; int (*compr)(struct fib_heap_data *, struct fib_heap_data *); unsigned int total_nodes; unsigned int max_nodes; } *

variable_types:
i
unsigned int

return_type: void

user_defined_types: struct fib_heap { struct fib_heap_node *the_one; struct fib_heap_node **cons_array; int (*compr)(struct fib_heap_data *, struct fib_heap_data *); unsigned int total_nodes; unsigned int max_nodes; }

user_defined_types: struct fib_heap_node { struct fib_heap_data *data; unsigned int degree; char marked; struct fib_heap_node *parent; struct fib_heap_node *child; struct fib_heap_node *left; struct fib_heap_node *right; }


decompiled_code:
void __fastcall fibheap_delete(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
void *v3; // rax
const char *v4; // rdx

if ( !a1 )
{
v4 = "fibheap_delete: H==NULL";
goto LABEL_7;
}
if ( !a2 )
{
v4 = "fibheap_delete: node==NULL";
LABEL_7:
fprintf(stderr, "fib_heap.c: %s\n", v4);
exit(1);
}
v2 = *(_QWORD *)(a2 + 16);
if ( v2 )
{
fibheap_cut_isra_0(*a1, a2, *(_QWORD *)(a2 + 16));
fibheap_casc_cut(a1, v2);
}
*a1 = a2;
v3 = (void *)fibheap_extract((__int64 **)a1);
free(v3);
}

canonical_decompiled_code:
void __fastcall func11(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
void *v3; // rax
const char *v4; // rdx

if ( !a1 )
{
v4 = "fibheap_delete: H==NULL";
goto LABEL_7;
}
if ( !a2 )
{
v4 = "fibheap_delete: node==NULL";
LABEL_7:
fprintf(stderr, "fib_heap.c: %s\n", v4);
exit(1);
}
v2 = *(_QWORD *)(a2 + 16);
if ( v2 )
{
func4(*a1, a2, *(_QWORD *)(a2 + 16));
func5(a1, v2);
}
*a1 = a2;
v3 = (void *)func8((__int64 **)a1);
free(v3);
}

original_code:
void
fibheap_delete(struct fib_heap *H, struct fib_heap_node *node)
{
 struct fib_heap_node *y;
 struct fib_heap_data *d;
 ({ if(!(H != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_delete: H==NULL")); exit(1); }; });
 ({ if(!(node != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_delete: node==NULL")); exit(1); }; });
 y = node->parent;
 if(y != ((void *)0)) {
  fibheap_cut(H, node, y);
  fibheap_casc_cut(H, y);
 }
 H->the_one = node;
 d = fibheap_extract(H);
 free(d);
}

variable_types: struct fib_heap_node { struct fib_heap_data { int key; } *data; unsigned int degree; char marked; struct fib_heap_node *parent; struct fib_heap_node *child; struct fib_heap_node *left; struct fib_heap_node *right; } *

variable_types:
d
struct fib_heap_data { int key; } *


decompiled_code:
void __fastcall fibheap_destroy(void *ptr)
{
if ( !ptr )
{
fprintf(stderr, "fib_heap.c: %s\n", "fibheap_destroy: H==NULL");
exit(1);
}
fibheap_destroy_rec(*(_QWORD *)ptr);
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
if ( !ptr )
{
fprintf(stderr, "fib_heap.c: %s\n", "fibheap_destroy: H==NULL");
exit(1);
}
func3(*(_QWORD *)ptr);
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void
fibheap_destroy(struct fib_heap *H)
{
 ({ if(!(H != ((void *)0))) { fprintf(stderr, "fib_heap.c: %s\n", ("fibheap_destroy: H==NULL")); exit(1); }; });
 fibheap_destroy_rec(H->the_one);
 free(H->cons_array);
 free(H);
}


repo: illarionov/sirfdump

decompiled_code:
void __fastcall ura2meters(int a1)
{
if ( a1 != 3 && a1 != 5 && a1 != 1 )
{
if ( (unsigned int)a1 <= 6 )
{
pow(2.0, (double)a1 * 0.5 + 1.0);
}
else if ( (unsigned int)a1 <= 0xE )
{
pow(2.0, (double)a1 - 2.0);
}
}
}

original_code:
static double ura2meters(unsigned ura)
{
   switch (ura) {
      case 1: return 2.8;
      case 3: return 5.7;
      case 5: return 11.3;
      default:
  break;
   }
   if (ura <= 6)
      return pow(2.0, 1.0+(double)ura/2.0);
   if (ura < 15)
      return pow(2.0, (double)ura - 2.0);
   return 6145.0;
}

canonical_original_code:
static double ura2meters(unsigned int ura)
{
   switch (ura) {
      case 1: return 2.8;
      case 3: return 5.7;
      case 5: return 11.3;
      default:
  break;
   }
   if (ura <= 6)
      return pow(2.0, 1.0+(double)ura/2.0);
   if (ura < 15)
      return pow(2.0, (double)ura - 2.0);
   return 6145.0;
}

variable_types:
ura
unsigned int

return_type: double


decompiled_code:
char *new_rinex_nav_ctx()
{
char *v0; // rax
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x5348uLL);
v1 = v0;
if ( v0 )
{
strcpy(v0, "sirfdump");
v0[21] = 0;
time(&timer);
v2 = gmtime(&timer);
snprintf(
v1 + 42,
0x15uLL,
"%i-%3s-%02u %02i:%02i",
(unsigned int)v2->tm_mday,
*((const char **)&MonthName + v2->tm_mon),
(unsigned int)(v2->tm_year % 100),
(unsigned int)v2->tm_hour,
(unsigned int)v2->tm_min);
*((_QWORD *)v1 + 10) = 0LL;
*((_QWORD *)v1 + 11) = 0x40000000000LL;
init_nav_data(v1 + 96);
}
return v1;
}

canonical_decompiled_code:
char *func5()
{
char *v0; // rax
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x5348uLL);
v1 = v0;
if ( v0 )
{
strcpy(v0, "sirfdump");
v0[21] = 0;
time(&timer);
v2 = gmtime(&timer);
snprintf(
v1 + 42,
0x15uLL,
"%i-%3s-%02u %02i:%02i",
(unsigned int)v2->tm_mday,
*((const char **)&MonthName + v2->tm_mon),
(unsigned int)(v2->tm_year % 100),
(unsigned int)v2->tm_hour,
(unsigned int)v2->tm_min);
*((_QWORD *)v1 + 10) = 0LL;
*((_QWORD *)v1 + 11) = 0x40000000000LL;
init_nav_data(v1 + 96);
}
return v1;
}

original_code:
void *new_rinex_nav_ctx(int argc, char **argv)
{
   struct rinex_nav_ctx_t *ctx;
   struct tm *tm;
   time_t clock;
   if (argc || argv) {};
   ctx = malloc(sizeof(*ctx));
   if (ctx == ((void *)0))
      return ((void *)0);
   snprintf(ctx->file.pgm, sizeof(ctx->file.pgm), "sirfdump");
   ctx->file.run_by[0]='\0';
   time(&clock);
   tm = gmtime(&clock);
   snprintf(ctx->file.date, sizeof(ctx->file.date), "%i-%3s-%02u %02i:%02i",
  tm->tm_mday, MonthName[tm->tm_mon], tm->tm_year % 100,
  tm->tm_hour, tm->tm_min);
   ctx->opt_header_pos_valid = 0;
   ctx->header_printed = ctx->opt_header_printed = 0;
   ctx->gps_week = 1024;
   init_nav_data(&ctx->navdata);
   return ctx;
}

canonical_original_code:
void *new_rinex_nav_ctx(int argc, char **argv)
{
   struct rinex_nav_ctx_t *ctx;
   struct tm *tm;
   long clock;
   if (argc || argv) {};
   ctx = malloc(sizeof(*ctx));
   if (ctx == ((void *)0))
      return ((void *)0);
   snprintf(ctx->file.pgm, sizeof(ctx->file.pgm), "sirfdump");
   ctx->file.run_by[0]='\0';
   time(&clock);
   tm = gmtime(&clock);
   snprintf(ctx->file.date, sizeof(ctx->file.date), "%i-%3s-%02u %02i:%02i",
  tm->tm_mday, MonthName[tm->tm_mon], tm->tm_year % 100,
  tm->tm_hour, tm->tm_min);
   ctx->opt_header_pos_valid = 0;
   ctx->header_printed = ctx->opt_header_printed = 0;
   ctx->gps_week = 1024;
   init_nav_data(&ctx->navdata);
   return ctx;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct rinex_nav_ctx_t { struct { char pgm[21]; char run_by[21]; char date[21]; } file; struct _G_fpos_t { long __pos; struct __mbstate_t { int __count; union { unsigned int __wch; char __wchb[4]; } __value; } __state; } opt_header_pos; int opt_header_pos_valid; int header_printed; int opt_header_printed; unsigned int gps_week; struct nav_data_t { struct { double ion_alpha[4]; double ion_beta[4]; double a0; unsigned int WNt; unsigned int leap; unsigned int is_active; } sub4_18; struct nav_sat_data_t { unsigned int is_printed; unsigned int is_sub1_active; unsigned int is_sub2_active; unsigned int is_sub3_active; struct subframe_t { unsigned char subframe_num; unsigned char data_id; unsigned char pageid; unsigned char tSVID; unsigned int TOW17; long l_TOW17; _Bool integrity; _Bool alert; _Bool antispoof; int is_almanac; union { struct { unsigned short WN; unsigned short IODC; unsigned short toc; long l_toc; unsigned char l2; unsigned char l2p; unsigned int ura; unsigned int hlth; int af0; double d_af0; short af1; double d_af1; char af2; double d_af2; char Tgd; double d_Tgd; } sub1; struct { unsigned char IODE; unsigned char AODO; unsigned short u_AODO; unsigned char fit; unsigned short toe; long l_toe; short Crs; double d_Crs; short Cus; double d_Cus; short Cuc; double d_Cuc; short deltan; double d_deltan; int M0; double d_M0; unsigned int e; double d_eccentricity; unsigned int sqrtA; double d_sqrtA; } sub2; struct { unsigned char IODE; short IDOT; double d_IDOT; short Cic; double d_Cic; short Cis; double d_Cis; short Crc; double d_Crc; int i0; double d_i0; int Omega0; double d_Omega0; int omega; double d_omega; int Omegad; double d_Omegad; } sub3; struct { struct almanac_t { unsigned char sv; unsigned char toa; long l_toa; unsigned char svh; short deltai; double d_deltai; int M0; double d_M0; int Omega0; double d_Omega0; int omega; double d_omega; short af0; double d_af0; short af1; double d_af1; unsigned short e; double d_eccentricity; unsigned int sqrtA; double d_sqrtA; short Omegad; double d_Omegad; } almanac; } sub4; struct { char ERD[33]; unsigned char ai; } sub4_13; struct { char str[24]; } sub4_17; struct { int A0; double d_A0; int A1; double d_A1; char alpha0; double d_alpha0; char alpha1; double d_alpha1; char alpha2; double d_alpha2; char alpha3; double d_alpha3; char beta0; double d_beta0; char beta1; double d_beta1; char beta2; double d_beta2; char beta3; double d_beta3; char leap; char lsf; unsigned char tot; double d_tot; unsigned char WNt; unsigned char WNlsf; unsigned char DN; } sub4_18; struct { unsigned char svf[33]; unsigned char svhx[8]; } sub4_25; struct { struct almanac_t almanac; } sub5; struct { unsigned char toa; long l_toa; unsigned char WNa; unsigned char sv[25]; } sub5_25; }; } sub1; struct subframe_t sub2; struct subframe_t sub3; } prn[-1]; } navdata; } *

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: clock long

return_type: void *

user_defined_types: struct rinex_nav_ctx_t { struct { char pgm[21]; char run_by[21]; char date[21]; } file; struct _G_fpos_t opt_header_pos; int opt_header_pos_valid; int header_printed; int opt_header_printed; unsigned int gps_week; struct nav_data_t navdata; }

user_defined_types: struct _G_fpos_t { long __pos; struct __mbstate_t __state; }

user_defined_types: struct nav_data_t { struct { double ion_alpha[4]; double ion_beta[4]; double a0; unsigned int WNt; unsigned int leap; unsigned int is_active; } sub4_18; struct nav_sat_data_t prn[-1]; }

user_defined_types: struct nav_sat_data_t { unsigned int is_printed; unsigned int is_sub1_active; unsigned int is_sub2_active; unsigned int is_sub3_active; struct subframe_t sub1; struct subframe_t sub2; struct subframe_t sub3; }

user_defined_types: struct subframe_t { unsigned char subframe_num; unsigned char data_id; unsigned char pageid; unsigned char tSVID; unsigned int TOW17; long l_TOW17; _Bool integrity; _Bool alert; _Bool antispoof; int is_almanac; union { struct { unsigned short WN; unsigned short IODC; unsigned short toc; long l_toc; unsigned char l2; unsigned char l2p; unsigned int ura; unsigned int hlth; int af0; double d_af0; short af1; double d_af1; char af2; double d_af2; char Tgd; double d_Tgd; } sub1; struct { unsigned char IODE; unsigned char AODO; unsigned short u_AODO; unsigned char fit; unsigned short toe; long l_toe; short Crs; double d_Crs; short Cus; double d_Cus; short Cuc; double d_Cuc; short deltan; double d_deltan; int M0; double d_M0; unsigned int e; double d_eccentricity; unsigned int sqrtA; double d_sqrtA; } sub2; struct { unsigned char IODE; short IDOT; double d_IDOT; short Cic; double d_Cic; short Cis; double d_Cis; short Crc; double d_Crc; int i0; double d_i0; int Omega0; double d_Omega0; int omega; double d_omega; int Omegad; double d_Omegad; } sub3; struct { struct almanac_t almanac; } sub4; struct { char ERD[33]; unsigned char ai; } sub4_13; struct { char str[24]; } sub4_17; struct { int A0; double d_A0; int A1; double d_A1; char alpha0; double d_alpha0; char alpha1; double d_alpha1; char alpha2; double d_alpha2; char alpha3; double d_alpha3; char beta0; double d_beta0; char beta1; double d_beta1; char beta2; double d_beta2; char beta3; double d_beta3; char leap; char lsf; unsigned char tot; double d_tot; unsigned char WNt; unsigned char WNlsf; unsigned char DN; } sub4_18; struct { unsigned char svf[33]; unsigned char svhx[8]; } sub4_25; struct { struct almanac_t almanac; } sub5; struct { unsigned char toa; long l_toa; unsigned char WNa; unsigned char sv[25]; } sub5_25; }; }


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

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

original_code:
void free_rinex_nav_ctx(void *ctx)
{
   struct rinex_nav_ctx_t *c;
   c = (struct rinex_nav_ctx_t *)ctx;
   free(c);
}

variable_types:
ctx
void *

user_defined_types: struct __mbstate_t { int __count; union { unsigned int __wch; char __wchb[4]; } __value; }

user_defined_types: struct almanac_t { unsigned char sv; unsigned char toa; long l_toa; unsigned char svh; short deltai; double d_deltai; int M0; double d_M0; int Omega0; double d_Omega0; int omega; double d_omega; short af0; double d_af0; short af1; double d_af1; unsigned short e; double d_eccentricity; unsigned int sqrtA; double d_sqrtA; short Omegad; double d_Omegad; }


decompiled_code:
__int64 __fastcall crc24q_hash(_BYTE *a1, int a2)
{
__int64 v2; // rcx
int v3; // eax
int v4; // edx
unsigned __int8 v5; // al

if ( a2 <= 0 )
return 0LL;
v2 = (__int64)&a1[a2 - 1 + 1];
v3 = 0;
do
{
v4 = v3;
v5 = *a1++ ^ BYTE2(v3);
v3 = crc24q[v5] ^ (v4 << 8);
}
while ( (_BYTE *)v2 != a1 );
return v3 & 0xFFFFFF;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, int a2)
{
__int64 v2; // rcx
int v3; // eax
int v4; // edx
unsigned __int8 v5; // al

if ( a2 <= 0 )
return 0LL;
v2 = (__int64)&a1[a2 - 1 + 1];
v3 = 0;
do
{
v4 = v3;
v5 = *a1++ ^ BYTE2(v3);
v3 = crc24q[v5] ^ (v4 << 8);
}
while ( (_BYTE *)v2 != a1 );
return v3 & 0xFFFFFF;
}

original_code:
unsigned crc24q_hash(unsigned char *data, int len)
{
    int i;
    unsigned crc = 0;
    for (i = 0; i < len; i++) {
 crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc >> 16)];
    }
    crc = (crc & 0x00ffffff);
    return crc;
}

canonical_original_code:
unsigned int crc24q_hash(unsigned char *data, int len)
{
    int i;
    unsigned int crc = 0;
    for (i = 0; i < len; i++) {
 crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc >> 16)];
    }
    crc = (crc & 0x00ffffff);
    return crc;
}

variable_types:
data
unsigned char *

variable_types: len int

variable_types: i int

variable_types:
crc
unsigned int

return_type: unsigned int


original_code:
unsigned crc24q_hashv(struct crc24_iovec *iov, int iovcnt)
{
    int j;
    unsigned crc = 0;
    for (j=0; j<iovcnt; j++) {
       int i;
       int len;
       unsigned char *data;
       data = (unsigned char*)iov[j].iov_base;
       len = (int)iov[j].iov_len;
       for (i = 0; i < len; i++) {
   crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc >> 16)];
       }
    }
    crc = (crc & 0x00ffffff);
    return crc;
}

canonical_original_code:
unsigned int crc24q_hashv(struct crc24_iovec *iov, int iovcnt)
{
    int j;
    unsigned int crc = 0;
    for (j=0; j<iovcnt; j++) {
       int i;
       int len;
       unsigned char *data;
       data = (unsigned char *)iov[j].iov_base;
       len = (int)iov[j].iov_len;
       for (i = 0; i < len; i++) {
   crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc >> 16)];
       }
    }
    crc = (crc & 0x00ffffff);
    return crc;
}

variable_types: struct crc24_iovec { void *iov_base; unsigned long iov_len; } *

variable_types: iovcnt int

variable_types: j int


decompiled_code:
__int64 __fastcall crc24q_check(_BYTE *a1, int a2)
{
int v2; // eax
__int64 v3; // r8
__int64 v4; // r9
unsigned int v5; // er10
__int64 result; // rax

v2 = crc24q_hash(a1, a2 - 3);
if ( *(_BYTE *)(v4 + v3 - 3) == BYTE2(v2) && BYTE1(v2) == *(_BYTE *)(v4 + v3 - 2) )
result = *(_BYTE *)(v4 + v3 - 1) == (unsigned __int8)v2;
else
result = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1, int a2)
{
int v2; // eax
__int64 v3; // r8
__int64 v4; // r9
unsigned int v5; // er10
__int64 result; // rax

v2 = func0(a1, a2 - 3);
if ( *(_BYTE *)(v4 + v3 - 3) == BYTE2(v2) && BYTE1(v2) == *(_BYTE *)(v4 + v3 - 2) )
result = *(_BYTE *)(v4 + v3 - 1) == (unsigned __int8)v2;
else
result = v5;
return result;
}

original_code:
unsigned crc24q_check(unsigned char *data, int len)
{
    unsigned crc = crc24q_hash(data, len - 3);
    return (((data[len - 3] == (unsigned char)(((crc) >> 16) & 0xff)) &&
      (data[len - 2] == (unsigned char)(((crc) >> 8) & 0xff)) && (data[len - 1] == (unsigned char)((crc) & 0xff))));
}

canonical_original_code:
unsigned int crc24q_check(unsigned char *data, int len)
{
    unsigned int crc = crc24q_hash(data, len - 3);
    return (((data[len - 3] == (unsigned char)(((crc) >> 16) & 0xff)) &&
      (data[len - 2] == (unsigned char)(((crc) >> 8) & 0xff)) && (data[len - 1] == (unsigned char)((crc) & 0xff))));
}


strnlen

original_code:
size_t strnlen(
         const char * str,
         size_t size)
{
   size_t len;
   if (str == ((void *)0)) return 0;
   for (len = 0; len < size; len++)
   {
      if ('\0' == str[len])
      {
         break;
      }
   }
   return len;
}

canonical_original_code:
unsigned long strnlen(char *str, unsigned long size)
{
   unsigned long len;
   if (str == ((void *)0)) return 0;
   for (len = 0; len < size; len++)
   {
      if ('\0' == str[len])
      {
         break;
      }
   }
   return len;
}

variable_types:
str
char *

variable_types:
size
unsigned long

variable_types:
len
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall SIRF_PROTO_NMEA_AddCheckSum(char *a1, unsigned int a2)
{
char v2; // al
unsigned int v3; // edx
__int64 v4; // rcx

v2 = *a1;
if ( a2 && v2 )
{
v3 = 0;
LOBYTE(v4) = 0;
do
{
++a1;
LOBYTE(v4) = v2 ^ v4;
++v3;
v2 = *a1;
}
while ( *a1 && a2 > v3 );
v4 = (unsigned __int8)v4;
a2 -= v3;
}
else
{
v4 = 0LL;
}
return (snprintf(a1, a2, "*%02lX\r\n", v4) >> 31) & 0x4803;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, unsigned int a2)
{
char v2; // al
unsigned int v3; // edx
__int64 v4; // rcx

v2 = *a1;
if ( a2 && v2 )
{
v3 = 0;
LOBYTE(v4) = 0;
do
{
++a1;
LOBYTE(v4) = v2 ^ v4;
++v3;
v2 = *a1;
}
while ( *a1 && a2 > v3 );
v4 = (unsigned __int8)v4;
a2 -= v3;
}
else
{
v4 = 0LL;
}
return (snprintf(a1, a2, "*%02lX\r\n", v4) >> 31) & 0x4803;
}

original_code:
static tSIRF_RESULT SIRF_PROTO_NMEA_AddCheckSum( tSIRF_UINT8 *pBuf,
                                                 tSIRF_UINT32 bufsize)
{
   tSIRF_UINT8 ckSum = 0;
   tSIRF_UINT32 msgsize = 0;
   int result;
   while( (*pBuf != '\0') && (msgsize < bufsize) )
   {
      ckSum ^= *pBuf++;
      msgsize++;
   }
   result = snprintf( (char*)pBuf,
                      bufsize - msgsize,
                      "*%02lX\x0D\x0A",
                      (unsigned long)ckSum );
   if (result < 0)
   {
      return SIRF_PROTO_FRAME_CHECKSUM_ERROR;
   }
   else
   {
      return SIRF_SUCCESS;
   }
}

canonical_original_code:
static unsigned int SIRF_PROTO_NMEA_AddCheckSum(unsigned char *pBuf, unsigned int bufsize)
{
   unsigned char ckSum = 0;
   unsigned int msgsize = 0;
   int result;
   while( (*pBuf != '\0') && (msgsize < bufsize) )
   {
      ckSum ^= *pBuf++;
      msgsize++;
   }
   result = snprintf( (char *)pBuf,
                      bufsize - msgsize,
                      "*%02lX\x0D\x0A",
                      (unsigned long)ckSum );
   if (result < 0)
   {
      return SIRF_PROTO_FRAME_CHECKSUM_ERROR;
   }
   else
   {
      return SIRF_SUCCESS;
   }
}

variable_types:
pBuf
unsigned char *

variable_types:
bufsize
unsigned int

variable_types:
ckSum
unsigned char

variable_types:
msgsize
unsigned int

variable_types: result int


decompiled_code:
__int64 __fastcall SIRF_PROTO_NMEA_Encode(__int64 a1, __int64 a2, __int64 a3, _BYTE *a4, _DWORD *a5)
{
__int64 result; // rax
char *v6; // rbp
int v8[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = 18432LL;
if ( *a5 > 6u )
{
v6 = a4 + 1;
*a4 = 36;
v8[0] = *a5 - 7;
result = SIRF_CODEC_NMEA_Encode(a1, a2, a3, a4 + 1, v8);
if ( !(_DWORD)result )
{
result = SIRF_PROTO_NMEA_AddCheckSum(v6, v8[0] + 6);
*a5 = v8[0] + 7;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, _BYTE *a4, _DWORD *a5)
{
__int64 result; // rax
char *v6; // rbp
int v8[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = 18432LL;
if ( *a5 > 6u )
{
v6 = a4 + 1;
*a4 = 36;
v8[0] = *a5 - 7;
result = SIRF_CODEC_NMEA_Encode(a1, a2, a3, a4 + 1, v8);
if ( !(_DWORD)result )
{
result = func0(v6, v8[0] + 6);
*a5 = v8[0] + 7;
}
}
return result;
}

original_code:
tSIRF_RESULT SIRF_PROTO_NMEA_Encode(
   tSIRF_UINT32 message_id,
   void const * const message_structure,
   tSIRF_UINT32 message_length,
   tSIRF_UINT8 * const packet,
   tSIRF_UINT32 * const packet_length,
   tSIRF_UINT32 * const options )
{
   tSIRF_UINT8 * payload;
   tSIRF_UINT32 payload_length;
   tSIRF_UINT32 result;
   if (((1) + (6)) > *packet_length)
   {
      return SIRF_PROTO_BUFFER_ERROR;
   }
   packet[0] = '$';
   payload = &packet[(1)];
   payload_length = *packet_length - ((1) + (6));
   result = SIRF_CODEC_NMEA_Encode(message_id,
                                   message_structure,
                                   message_length,
                                   payload,
                                   &payload_length,
                                   options);
   if (SIRF_SUCCESS == result)
   {
      result = SIRF_PROTO_NMEA_AddCheckSum(payload,payload_length+(6));
      *packet_length = payload_length + ((1) + (6));
   }
   return result;
}

canonical_original_code:
unsigned int SIRF_PROTO_NMEA_Encode(unsigned int message_id, void *message_structure, unsigned int message_length, unsigned char *packet, unsigned int *packet_length, unsigned int *options)
{
   unsigned char *payload;
   unsigned int payload_length;
   unsigned int result;
   if (((1) + (6)) > *packet_length)
   {
      return SIRF_PROTO_BUFFER_ERROR;
   }
   packet[0] = '$';
   payload = &packet[(1)];
   payload_length = *packet_length - ((1) + (6));
   result = SIRF_CODEC_NMEA_Encode(message_id,
                                   message_structure,
                                   message_length,
                                   payload,
                                   &payload_length,
                                   options);
   if (SIRF_SUCCESS == result)
   {
      result = SIRF_PROTO_NMEA_AddCheckSum(payload,payload_length+(6));
      *packet_length = payload_length + ((1) + (6));
   }
   return result;
}

variable_types:
packet
unsigned char *

variable_types:
options
unsigned int *

variable_types:
payload
unsigned char *

variable_types:
result
unsigned int


original_code:
tSIRF_RESULT SIRF_PROTO_NMEA_Decode(
   tSIRF_UINT8 const * const packet,
   tSIRF_UINT32 packet_length,
   tSIRF_UINT32 * const message_id,
   void * const message_structure,
   tSIRF_UINT32 * const message_length,
   tSIRF_UINT32 * const options )
{
   tSIRF_RESULT result;
   tSIRF_UINT32 payload_length = 0;
   tSIRF_UINT8 const *ptr = packet;
   tSIRF_UINT32 calculated_checksum = 0;
   unsigned long read_checksum;
   tSIRF_CHAR checksum_buffer[(6)];
   tSIRF_UINT32 i;
   int ret_val;
   if ( packet_length < ((1) + (6)) )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   if ( *ptr++ != '$' )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   while ( (*ptr != '*') && (payload_length<(packet_length-1)) )
   {
      calculated_checksum ^= *ptr++;
      payload_length++;
   }
   if ( (payload_length == packet_length) ||
        (payload_length > (1024)) )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   for ( i=0; i<(6)-1; i++ )
   {
      checksum_buffer[i] = *ptr++;
   }
   checksum_buffer[i] = '\0';
   ret_val = sscanf( checksum_buffer, "*%02lX\x0D\x0A", &read_checksum );
   if ( ret_val != 1 )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   if ( calculated_checksum != read_checksum )
   {
      return SIRF_PROTO_FRAME_CHECKSUM_ERROR;
   }
   result = SIRF_CODEC_NMEA_Decode( packet+1, payload_length,
                                    message_id, message_structure, message_length,
                                    options );
   return result;
}

canonical_original_code:
unsigned int SIRF_PROTO_NMEA_Decode(unsigned char *packet, unsigned int packet_length, unsigned int *message_id, void *message_structure, unsigned int *message_length, unsigned int *options)
{
   unsigned int result;
   unsigned int payload_length = 0;
   unsigned char *ptr = packet;
   unsigned int calculated_checksum = 0;
   unsigned long read_checksum;
   char checksum_buffer[-1];
   unsigned int i;
   int ret_val;
   if ( packet_length < ((1) + (6)) )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   if ( *ptr++ != '$' )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   while ( (*ptr != '*') && (payload_length<(packet_length-1)) )
   {
      calculated_checksum ^= *ptr++;
      payload_length++;
   }
   if ( (payload_length == packet_length) ||
        (payload_length > (1024)) )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   for ( i=0; i<(6)-1; i++ )
   {
      checksum_buffer[i] = *ptr++;
   }
   checksum_buffer[i] = '\0';
   ret_val = sscanf( checksum_buffer, "*%02lX\x0D\x0A", &read_checksum );
   if ( ret_val != 1 )
   {
      return SIRF_PROTO_FRAME_ERROR;
   }
   if ( calculated_checksum != read_checksum )
   {
      return SIRF_PROTO_FRAME_CHECKSUM_ERROR;
   }
   result = SIRF_CODEC_NMEA_Decode( packet+1, payload_length,
                                    message_id, message_structure, message_length,
                                    options );
   return result;
}

variable_types:
ptr
unsigned char *

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall SIRF_PROTO_NMEA_Parser(_BYTE *a1, unsigned int a2, unsigned int *a3)
{
unsigned int v4; // er8
_BYTE *v5; // rax
unsigned int v6; // er8
__int64 result; // rax
__int64 v8; // rax
char v9; // dl
int v10; // ecx

v4 = 18433;
if ( a2 <= 6 )
return v4;
v5 = a1;
v6 = 0;
while ( *v5 != 36 )
{
++v6;
++v5;
if ( a2 == v6 )
goto LABEL_5;
}
if ( v6 )
{
a2 = v6;
LABEL_5:
*a3 = a2;
return 18434;
}
v8 = 1LL;
while ( 1 )
{
v9 = a1[v8];
v10 = v8;
if ( v9 == 10 )
{
result = 0LL;
*a3 = v10 + 1;
return result;
}
if ( v9 == 36 )
break;
if ( ++v8 == a2 )
return 18433LL;
}
*a3 = v8;
return 18434LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1, unsigned int a2, unsigned int *a3)
{
unsigned int v4; // er8
_BYTE *v5; // rax
unsigned int v6; // er8
__int64 result; // rax
__int64 v8; // rax
char v9; // dl
int v10; // ecx

v4 = 18433;
if ( a2 <= 6 )
return v4;
v5 = a1;
v6 = 0;
while ( *v5 != 36 )
{
++v6;
++v5;
if ( a2 == v6 )
goto LABEL_5;
}
if ( v6 )
{
a2 = v6;
LABEL_5:
*a3 = a2;
return 18434;
}
v8 = 1LL;
while ( 1 )
{
v9 = a1[v8];
v10 = v8;
if ( v9 == 10 )
{
result = 0LL;
*a3 = v10 + 1;
return result;
}
if ( v9 == 36 )
break;
if ( ++v8 == a2 )
return 18433LL;
}
*a3 = v8;
return 18434LL;
}

original_code:
tSIRF_RESULT SIRF_PROTO_NMEA_Parser(
   tSIRF_UINT8 const * const buffer,
   tSIRF_UINT32 bytes_in_buffer,
   tSIRF_UINT32 * const data_length )
{
   tSIRF_UINT32 i;
   if ( bytes_in_buffer < ((1) + (6)) )
      return SIRF_PROTO_NOT_ENOUGH_DATA_YET;
   for ( i=0; i<bytes_in_buffer; i++ )
   {
      if ( buffer[i] == '$' )
         break;
   }
   if ( i )
   {
      *data_length = i;
      return SIRF_PROTO_FRAME_ERROR;
   }
   for ( i=1; i<bytes_in_buffer; i++ )
   {
      if ( buffer[i] == 0x0A )
      {
         *data_length = i + 1;
         return SIRF_SUCCESS;
      }
      else if ( buffer[i] == '$' )
      {
         *data_length = i;
         return SIRF_PROTO_FRAME_ERROR;
      }
   }
   return SIRF_PROTO_NOT_ENOUGH_DATA_YET;
}

canonical_original_code:
unsigned int SIRF_PROTO_NMEA_Parser(unsigned char *buffer, unsigned int bytes_in_buffer, unsigned int *data_length)
{
   unsigned int i;
   if ( bytes_in_buffer < ((1) + (6)) )
      return SIRF_PROTO_NOT_ENOUGH_DATA_YET;
   for ( i=0; i<bytes_in_buffer; i++ )
   {
      if ( buffer[i] == '$' )
         break;
   }
   if ( i )
   {
      *data_length = i;
      return SIRF_PROTO_FRAME_ERROR;
   }
   for ( i=1; i<bytes_in_buffer; i++ )
   {
      if ( buffer[i] == 0x0A )
      {
         *data_length = i + 1;
         return SIRF_SUCCESS;
      }
      else if ( buffer[i] == '$' )
      {
         *data_length = i;
         return SIRF_PROTO_FRAME_ERROR;
      }
   }
   return SIRF_PROTO_NOT_ENOUGH_DATA_YET;
}

variable_types:
buffer
unsigned char *


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(void *dest, void *src, size_t maxlen)
{
size_t v3; // rbx
size_t v4; // rax
__int64 v5; // r13
__int64 result; // rax

if ( !dest || !maxlen )
return maxlen;
if ( src )
{
v3 = maxlen - 1;
v4 = strnlen((const char *)src, maxlen);
v5 = v4;
if ( v3 > v4 )
v3 = v4;
memcpy(dest, src, v3);
*((_BYTE *)dest + v3) = 0;
result = v5;
}
else
{
*(_BYTE *)dest = 0;
result = maxlen;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *dest, void *src, size_t maxlen)
{
size_t v3; // rbx
size_t v4; // rax
__int64 v5; // r13
__int64 result; // rax

if ( !dest || !maxlen )
return maxlen;
if ( src )
{
v3 = maxlen - 1;
v4 = strnlen((const char *)src, maxlen);
v5 = v4;
if ( v3 > v4 )
v3 = v4;
memcpy(dest, src, v3);
*((_BYTE *)dest + v3) = 0;
result = v5;
}
else
{
*(_BYTE *)dest = 0;
result = maxlen;
}
return result;
}

original_code:
size_t strlcpy(
         char *dst,
         const char *src,
         size_t size)
{
   size_t src_len, cpy_len;
   if (dst == ((void *)0) || size == 0)
      return size;
   if (src == ((void *)0))
   {
      dst[0] = '\0';
      return size;
   }
   src_len = strnlen(src, size);
   cpy_len = (src_len <= size - 1) ? src_len : size - 1;
   memcpy(dst, src, cpy_len);
   dst[cpy_len] = '\0';
   return src_len;
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long size)
{
   unsigned long src_len; unsigned long cpy_len;
   if (dst == ((void *)0) || size == 0)
      return size;
   if (src == ((void *)0))
   {
      dst[0] = '\0';
      return size;
   }
   src_len = strnlen(src, size);
   cpy_len = (src_len <= size - 1) ? src_len : size - 1;
   memcpy(dst, src, cpy_len);
   dst[cpy_len] = '\0';
   return src_len;
}

variable_types:
dst
char *

variable_types:
src
char *


strlcat

decompiled_code:
size_t __fastcall strlcat(const char *a1, const char *a2, size_t a3)
{
size_t v5; // rax
size_t v6; // r14
size_t v7; // rsi
size_t v8; // rbx
size_t v9; // rax
size_t v10; // r13

if ( !a1 || !a3 )
return a3;
v5 = strnlen(a1, a3);
v6 = v5;
if ( a2 )
{
v7 = a3;
v8 = a3 - 1;
v9 = strnlen(a2, v7);
v10 = v6 + v9;
if ( v8 > v6 + v9 )
v8 = v6 + v9;
if ( v6 < v8 )
memcpy((void *)&a1[v6], a2, v8 - v6);
a1[v8] = 0;
}
else
{
v10 = a3;
if ( a3 == v5 )
a1[a3 - 1] = 0;
}
return v10;
}

canonical_decompiled_code:
size_t __fastcall func1(const char *a1, const char *a2, size_t a3)
{
size_t v5; // rax
size_t v6; // r14
size_t v7; // rsi
size_t v8; // rbx
size_t v9; // rax
size_t v10; // r13

if ( !a1 || !a3 )
return a3;
v5 = strnlen(a1, a3);
v6 = v5;
if ( a2 )
{
v7 = a3;
v8 = a3 - 1;
v9 = strnlen(a2, v7);
v10 = v6 + v9;
if ( v8 > v6 + v9 )
v8 = v6 + v9;
if ( v6 < v8 )
memcpy((void *)&a1[v6], a2, v8 - v6);
a1[v8] = 0;
}
else
{
v10 = a3;
if ( a3 == v5 )
a1[a3 - 1] = 0;
}
return v10;
}

original_code:
size_t strlcat(
         char *dst,
         const char *src,
         size_t size)
{
   size_t dst_len, src_len, end_len, write_len;
   if (dst == ((void *)0) || size == 0)
      return size;
   dst_len = strnlen(dst, size);
   if (src == ((void *)0))
   {
      if (dst_len == size)
         dst[size-1] = '\0';
      return size;
   }
   src_len = strnlen(src, size);
   end_len = src_len + dst_len;
   write_len = (end_len <= size - 1) ? end_len : size - 1;
   if (dst_len < write_len)
      memcpy(dst + dst_len, src, write_len - dst_len);
   dst[write_len] = '\0';
   return end_len;
}

canonical_original_code:
unsigned long strlcat(char *dst, char *src, unsigned long size)
{
   unsigned long dst_len; unsigned long src_len; unsigned long end_len; unsigned long write_len;
   if (dst == ((void *)0) || size == 0)
      return size;
   dst_len = strnlen(dst, size);
   if (src == ((void *)0))
   {
      if (dst_len == size)
         dst[size-1] = '\0';
      return size;
   }
   src_len = strnlen(src, size);
   end_len = src_len + dst_len;
   write_len = (end_len <= size - 1) ? end_len : size - 1;
   if (dst_len < write_len)
      memcpy(dst + dst_len, src, write_len - dst_len);
   dst[write_len] = '\0';
   return end_len;
}


original_code:
unsigned int isgps_parity(isgps30bits_t th)
{
    isgps30bits_t t;
    unsigned int p;
    t = th & 0xbb1f3480u;
    p = parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
 parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & 0xff];
    t = th & 0x5d8f9a40u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0xaec7cd00u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x5763e680u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x6bb1f340u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x8b7a89c0u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    gpsd_report(7 + 2, "ISGPS parity %u\n", p);
    return (p);
}

canonical_original_code:
unsigned int isgps_parity(unsigned int th)
{
    unsigned int t;
    unsigned int p;
    t = th & 0xbb1f3480u;
    p = parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
 parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & 0xff];
    t = th & 0x5d8f9a40u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0xaec7cd00u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x5763e680u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x6bb1f340u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    t = th & 0x8b7a89c0u;
    p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^
      parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) &
          0xff]);
    gpsd_report(7 + 2, "ISGPS parity %u\n", p);
    return (p);
}

variable_types:
th
unsigned int

variable_types:
t
unsigned int

variable_types:
p
unsigned int


repo: illarionov/sirfmemdump

decompiled_code:
_BOOL8 __fastcall sirf_write(int fd, void *buf)
{
unsigned __int16 v4; // si
int v5; // edi
char *v6; // rax
unsigned int v7; // edx
char *v8; // rsi
int v9; // ecx
int v10; // ecx
size_t v11; // rbx
ssize_t v12; // r12

v4 = __ROL2__(*((_WORD *)buf + 1), 8);
v5 = v4;
if ( v4 )
{
v6 = (char *)buf;
v7 = 0;
v8 = (char *)buf + v4;
do
{
v9 = (unsigned __int8)v6[4];
++v6;
v7 += v9;
}
while ( v6 != v8 );
v10 = (v7 >> 8) & 0x7F;
}
else
{
LOBYTE(v7) = 0;
LOBYTE(v10) = 0;
}
*((_BYTE *)buf + v5 + 4) = v10;
v11 = v5 + 8;
*((_BYTE *)buf + v5 + 5) = v7;
v12 = write(fd, buf, v11);
tcdrain(fd);
return v11 == v12;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int fd, void *buf)
{
unsigned __int16 v4; // si
int v5; // edi
char *v6; // rax
unsigned int v7; // edx
char *v8; // rsi
int v9; // ecx
int v10; // ecx
size_t v11; // rbx
ssize_t v12; // r12

v4 = __ROL2__(*((_WORD *)buf + 1), 8);
v5 = v4;
if ( v4 )
{
v6 = (char *)buf;
v7 = 0;
v8 = (char *)buf + v4;
do
{
v9 = (unsigned __int8)v6[4];
++v6;
v7 += v9;
}
while ( v6 != v8 );
v10 = (v7 >> 8) & 0x7F;
}
else
{
LOBYTE(v7) = 0;
LOBYTE(v10) = 0;
}
*((_BYTE *)buf + v5 + 4) = v10;
v11 = v5 + 8;
*((_BYTE *)buf + v5 + 5) = v7;
v12 = write(fd, buf, v11);
tcdrain(fd);
return v11 == v12;
}

original_code:
static unsigned
sirf_write(int fd, unsigned char *msg)
{
   unsigned int crc;
   size_t i, len;
   unsigned ok;
   len = (size_t)((msg[2] << 8) | msg[3]);
   crc = 0;
   for (i = 0; i < len; i++)
        crc += (int)msg[4 + i];
   crc &= 0x7fff;
   msg[len + 4] = (unsigned char)((crc & 0xff00) >> 8);
   msg[len + 5] = (unsigned char)( crc & 0x00ff);
   ok = (write(fd, msg, len+8) == (ssize_t)(len+8));
   (void)tcdrain(fd);
   return(ok);
}

canonical_original_code:
static unsigned int sirf_write(int fd, unsigned char *msg)
{
   unsigned int crc;
   unsigned long i; unsigned long len;
   unsigned int ok;
   len = (unsigned long)((msg[2] << 8) | msg[3]);
   crc = 0;
   for (i = 0; i < len; i++)
        crc += (int)msg[4 + i];
   crc &= 0x7fff;
   msg[len + 4] = (unsigned char)((crc & 0xff00) >> 8);
   msg[len + 5] = (unsigned char)( crc & 0x00ff);
   ok = (write(fd, msg, len+8) == (long)(len+8));
   (void)tcdrain(fd);
   return(ok);
}

variable_types: fd int

variable_types:
msg
unsigned char *

variable_types:
crc
unsigned int

variable_types:
i
unsigned long

variable_types:
len
unsigned long

variable_types:
ok
unsigned int


decompiled_code:
__int64 __fastcall binary_send(int fd, __int64 a2, unsigned __int64 a3)
{
ssize_t v4; // r15
size_t v5; // r14
__int64 v6; // r12
int *v7; // r15
__int64 v8; // r15
ssize_t v9; // rax
int v10; // eax
int v12; // [rsp+8h] [rbp-40h]

v12 = clock();
fwrite("gpsflash: transferring binary... \b", 1uLL, 0x22uLL, stderr);
count_2 = 0;
if ( a3 )
{
v4 = 0LL;
while ( 2 )
{
v5 = 512LL;
v6 = v4 + a3;
if ( a3 <= 0x200 )
v5 = a3;
while ( 1 )
{
v8 = v6 - a3;
v9 = write(fd, (const void *)(a2 + v6 - a3), v5);
if ( v9 != -1 )
break;
v7 = _errno_location();
if ( *v7 != 11 )
return 0xFFFFFFFFLL;
++a3;
tcdrain(fd);
*v7 = 0;
}
a3 -= v9;
v4 = v9 + v8;
fputc(byte_691[count_2 % 4], stderr);
fputc(8, stderr);
fflush(stdout);
if ( a3 )
continue;
break;
}
}
v10 = clock();
fprintf(stderr, "...done (%2.2f sec).\n", ((double)v10 - (double)v12) / 1000000.0);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2, unsigned __int64 a3)
{
ssize_t v4; // r15
size_t v5; // r14
__int64 v6; // r12
int *v7; // r15
__int64 v8; // r15
ssize_t v9; // rax
int v10; // eax
int v12; // [rsp+8h] [rbp-40h]

v12 = clock();
fwrite("gpsflash: transferring binary... \b", 1uLL, 0x22uLL, stderr);
count_2 = 0;
if ( a3 )
{
v4 = 0LL;
while ( 2 )
{
v5 = 512LL;
v6 = v4 + a3;
if ( a3 <= 0x200 )
v5 = a3;
while ( 1 )
{
v8 = v6 - a3;
v9 = write(fd, (const void *)(a2 + v6 - a3), v5);
if ( v9 != -1 )
break;
v7 = _errno_location();
if ( *v7 != 11 )
return 0xFFFFFFFFLL;
++a3;
tcdrain(fd);
*v7 = 0;
}
a3 -= v9;
v4 = v9 + v8;
fputc(byte_691[count_2 % 4], stderr);
fputc(8, stderr);
fflush(stdout);
if ( a3 )
continue;
break;
}
}
v10 = clock();
fprintf(stderr, "...done (%2.2f sec).\n", ((double)v10 - (double)v12) / 1000000.0);
return 0LL;
}

original_code:
static int
binary_send(int pfd, char *data, size_t ls){
 size_t nbr, nbs, nbx;
 ssize_t r;
 static int count;
 double start = clock();
 fprintf(stderr, "gpsflash: transferring binary... \010");
 count = 0;
 nbr = ls; nbs = 512 ; nbx = 0;
 while(nbr){
  if(nbr > 512 )
   nbs = 512 ;
  else
   nbs = nbr;
r0: if((r = write(pfd, data+nbx, nbs)) == -1){
   if ((*__errno_location ()) == 11){
    (void)tcdrain(pfd);
    (*__errno_location ()) = 0;
    nbr -= r;
    nbx += r;
    goto r0;
   } else {
    return -1;
   }
  }
  nbr -= r;
  nbx += r;
  (void)fputc("-/|\\"[count % 4], stderr);
  (void)fputc('\010', stderr);
  (void)fflush(stdout);
 }
 (void)fprintf(stderr, "...done (%2.2f sec).\n", (clock()-start)/((__clock_t) 1000000));
 return 0;
}

canonical_original_code:
static int binary_send(int pfd, char *data, unsigned long ls){
 unsigned long nbr; unsigned long nbs; unsigned long nbx;
 long r;
 static int count;
 double start = clock();
 fprintf(stderr, "gpsflash: transferring binary... \010");
 count = 0;
 nbr = ls; nbs = 512 ; nbx = 0;
 while(nbr){
  if(nbr > 512 )
   nbs = 512 ;
  else
   nbs = nbr;
r0: if((r = write(pfd, data+nbx, nbs)) == -1){
   if ((*__errno_location ()) == 11){
    (void)tcdrain(pfd);
    (*__errno_location ()) = 0;
    nbr -= r;
    nbx += r;
    goto r0;
   } else {
    return -1;
   }
  }
  nbr -= r;
  nbx += r;
  (void)fputc("-/|\\"[count % 4], stderr);
  (void)fputc('\010', stderr);
  (void)fflush(stdout);
 }
 (void)fprintf(stderr, "...done (%2.2f sec).\n", (clock()-start)/((long) 1000000));
 return 0;
}

variable_types: pfd int

variable_types:
data
char *

variable_types:
ls
unsigned long

variable_types:
nbr
unsigned long

variable_types:
nbs
unsigned long

variable_types:
nbx
unsigned long

variable_types: r long

variable_types: count int

variable_types: start double

return_type: int


decompiled_code:
int __fastcall nmea_add_checksum(_BYTE *a1)
{
char *v2; // rdi
int v3; // eax
int v4; // edx
__int64 v5; // rcx

if ( *a1 != 36 && *a1 != 33 )
_assert_fail("*p == '$' || *p == '!'", "flashutils.c", 0x59u, "nmea_add_checksum");
v2 = a1 + 1;
v3 = (unsigned __int8)a1[1];
if ( !(_BYTE)v3 || (_BYTE)v3 == 42 )
{
v5 = 0LL;
}
else
{
v4 = 0;
do
{
++v2;
LODWORD(v5) = v3 ^ v4;
v3 = (unsigned __int8)*v2;
v4 = v5;
}
while ( (_BYTE)v3 && (_BYTE)v3 != 42 );
v5 = (unsigned __int8)v5;
}
*v2 = 42;
return snprintf(v2 + 1, 5uLL, "%02X\r\n", v5);
}

canonical_decompiled_code:
int __fastcall func2(_BYTE *a1)
{
char *v2; // rdi
int v3; // eax
int v4; // edx
__int64 v5; // rcx

if ( *a1 != 36 && *a1 != 33 )
_assert_fail("*p == '$' || *p == '!'", "flashutils.c", 0x59u, "nmea_add_checksum");
v2 = a1 + 1;
v3 = (unsigned __int8)a1[1];
if ( !(_BYTE)v3 || (_BYTE)v3 == 42 )
{
v5 = 0LL;
}
else
{
v4 = 0;
do
{
++v2;
LODWORD(v5) = v3 ^ v4;
v3 = (unsigned __int8)*v2;
v4 = v5;
}
while ( (_BYTE)v3 && (_BYTE)v3 != 42 );
v5 = (unsigned __int8)v5;
}
*v2 = 42;
return snprintf(v2 + 1, 5uLL, "%02X\r\n", v5);
}

original_code:
static void
nmea_add_checksum(char *sentence)
{
    unsigned char sum = '\0';
    char c;
    char *p = sentence;
    ((void) sizeof ((*p == '$' || *p == '!') ? 1 : 0), __extension__ ({ if (*p == '$' || *p == '!') ; else __assert_fail ("*p == '$' || *p == '!'", "flashutils.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    p++;
    while ( ((c = *p) != '*') && (c != '\0')) {
        sum ^= c;
        p++;
    }
    *p++ = '*';
    (void)snprintf(p, 5, "%02X\r\n", (unsigned)sum);
}

canonical_original_code:
static void nmea_add_checksum(char *sentence)
{
    unsigned char sum = '\0';
    char c;
    char *p = sentence;
    ((void) sizeof ((*p == '$' || *p == '!') ? 1 : 0), __extension__ ({ if (*p == '$' || *p == '!') ; else __assert_fail ("*p == '$' || *p == '!'", "flashutils.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    p++;
    while ( ((c = *p) != '*') && (c != '\0')) {
        sum ^= c;
        p++;
    }
    *p++ = '*';
    (void)snprintf(p, 5, "%02X\r\n", (unsigned int)sum);
}

variable_types:
sentence
char *

variable_types:
sum
unsigned char

variable_types: c char

variable_types:
p
char *


sirfEnterInternalBootMode

decompiled_code:
__int64 __fastcall sirfEnterInternalBootMode(int a1)
{
_BOOL4 v1; // ebx

v1 = sirf_write(a1, &msg_3);
sleep(2u);
return (unsigned int)-!v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
_BOOL4 v1; // ebx

v1 = func0(a1, &msg_3);
sleep(2u);
return (unsigned int)-!v1;
}

original_code:
int
sirfEnterInternalBootMode(int pfd){
 unsigned status;
 static unsigned char msg[] = {
    0xa0,0xa2,
    0x00,0x01,
    0x94,
    0x00,0x00,
    0xb0,0xb3};
 status = sirf_write(pfd, msg);
 (void)sleep(2);
 return status ? 0 : -1;
}

canonical_original_code:
int sirfEnterInternalBootMode(int pfd){
 unsigned int status;
 static unsigned char msg[] = {
    0xa0,0xa2,
    0x00,0x01,
    0x94,
    0x00,0x00,
    0xb0,0xb3};
 status = sirf_write(pfd, msg);
 (void)sleep(2);
 return status ? 0 : -1;
}

variable_types:
status
unsigned int

variable_types:
msg
unsigned char []


sirfSendLoader

decompiled_code:
__int64 __fastcall sirfSendLoader(int fd, __int64 a2, const void *a3, size_t a4)
{
unsigned __int64 v4; // r15
char *v7; // rax
char *v8; // rbp
unsigned int v9; // er12

v4 = a4 + 10;
v7 = (char *)malloc(a4 + 10);
if ( !v7 )
return (unsigned int)-1;
v8 = v7;
*(_WORD *)v7 = 83;
*(_DWORD *)(v7 + 2) = _byteswap_ulong(a4);
memcpy(v7 + 6, a3, a4);
*(_DWORD *)&v8[a4 + 6] = 0;
tcdrain(fd);
usleep(0x3E8u);
serialSpeed((unsigned int)fd, a2, 38400LL);
v9 = binary_send(fd, (__int64)v8, v4);
free(v8);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, __int64 a2, const void *a3, size_t a4)
{
unsigned __int64 v4; // r15
char *v7; // rax
char *v8; // rbp
unsigned int v9; // er12

v4 = a4 + 10;
v7 = (char *)malloc(a4 + 10);
if ( !v7 )
return (unsigned int)-1;
v8 = v7;
*(_WORD *)v7 = 83;
*(_DWORD *)(v7 + 2) = _byteswap_ulong(a4);
memcpy(v7 + 6, a3, a4);
*(_DWORD *)&v8[a4 + 6] = 0;
tcdrain(fd);
usleep(0x3E8u);
serialSpeed((unsigned int)fd, a2, 38400LL);
v9 = func1(fd, (__int64)v8, v4);
free(v8);
return v9;
}

original_code:
int
sirfSendLoader(int pfd, struct termios *term, char *loader, size_t ls){
 int r, speed = 38400;
 unsigned char boost[] = {'S', 0};
 unsigned char *msg;
 if((msg = malloc(ls+10)) == ((void *)0)){
  return -1;
 }
 msg[0] = 'S';
 msg[1] = (unsigned char)0;
 msg[2] = (unsigned char)((ls & 0xff000000) >> 24);
 msg[3] = (unsigned char)((ls & 0xff0000) >> 16);
 msg[4] = (unsigned char)((ls & 0xff00) >> 8);
 msg[5] = (unsigned char)(ls & 0xff);
 memcpy(msg+6, loader, ls);
 memset(msg+6+ls, 0, 4);
 (void)tcdrain(pfd);
 (void)usleep(1000);
 (void)serialSpeed(pfd, term, speed);
 r = binary_send(pfd, (char *)msg, ls+10);
 free(msg);
 return r;
}

canonical_original_code:
int sirfSendLoader(int pfd, struct termios *term, char *loader, unsigned long ls){
 int r; int speed = 38400;
 unsigned char boost[] = {'S', 0};
 unsigned char *msg;
 if((msg = malloc(ls+10)) == ((void *)0)){
  return -1;
 }
 msg[0] = 'S';
 msg[1] = (unsigned char)0;
 msg[2] = (unsigned char)((ls & 0xff000000) >> 24);
 msg[3] = (unsigned char)((ls & 0xff0000) >> 16);
 msg[4] = (unsigned char)((ls & 0xff00) >> 8);
 msg[5] = (unsigned char)(ls & 0xff);
 memcpy(msg+6, loader, ls);
 memset(msg+6+ls, 0, 4);
 (void)tcdrain(pfd);
 (void)usleep(1000);
 (void)serialSpeed(pfd, term, speed);
 r = binary_send(pfd, (char *)msg, ls+10);
 free(msg);
 return r;
}

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

variable_types:
loader
char *

variable_types: r int

variable_types: speed int

variable_types:
boost
unsigned char []

variable_types:
msg
unsigned char *


sirfSetProto

decompiled_code:
__int64 __fastcall sirfSetProto(__int64 fd, __int64 a2, unsigned int a3, unsigned int a4)
{
__int64 result; // rax
char *v7; // rbx
__int64 v8; // rdx
__int64 v9[3]; // [rsp+4h] [rbp-54h] BYREF
int v10; // [rsp+1Ch] [rbp-3Ch]
char v11; // [rsp+20h] [rbp-38h] BYREF

v9[0] = 0x96000000E100LL;
v9[1] = 0x4B0000007080LL;
v9[2] = 0x258000003840LL;
v10 = 4800;
result = serialConfig(fd, a2, 38400LL);
if ( (_DWORD)result != -1 )
{
v7 = (char *)v9;
dword_5E8 = _byteswap_ulong(a3);
byte_5E6 = a4;
byte_5E7 = a4;
sirf_write(fd, &sirf_1);
write(fd, "\r\n", 2uLL);
nmea_lowlevel_send_constprop_0(fd, (__int64)"$PSRF100,%u,%u,8,1,0", a4, a3);
v8 = 115200LL;
while ( 1 )
{
serialSpeed((unsigned int)fd, a2, v8);
sirf_write(fd, &sirf_1);
write(fd, "\r\n", 2uLL);
nmea_lowlevel_send_constprop_0(fd, (__int64)"$PSRF100,%u,%u,8,1,0", a4, a3);
tcdrain(fd);
usleep(0x186A0u);
if ( v7 == &v11 )
break;
v8 = *(unsigned int *)v7;
v7 += 4;
}
serialSpeed((unsigned int)fd, a2, a3);
tcflush(fd, 2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 fd, __int64 a2, unsigned int a3, unsigned int a4)
{
__int64 result; // rax
char *v7; // rbx
__int64 v8; // rdx
__int64 v9[3]; // [rsp+4h] [rbp-54h] BYREF
int v10; // [rsp+1Ch] [rbp-3Ch]
char v11; // [rsp+20h] [rbp-38h] BYREF

v9[0] = 0x96000000E100LL;
v9[1] = 0x4B0000007080LL;
v9[2] = 0x258000003840LL;
v10 = 4800;
result = serialConfig(fd, a2, 38400LL);
if ( (_DWORD)result != -1 )
{
v7 = (char *)v9;
dword_5E8 = _byteswap_ulong(a3);
byte_5E6 = a4;
byte_5E7 = a4;
func0(fd, &sirf_1);
write(fd, "\r\n", 2uLL);
func3(fd, (__int64)"$PSRF100,%u,%u,8,1,0", a4, a3);
v8 = 115200LL;
while ( 1 )
{
serialSpeed((unsigned int)fd, a2, v8);
func0(fd, &sirf_1);
write(fd, "\r\n", 2uLL);
func3(fd, (__int64)"$PSRF100,%u,%u,8,1,0", a4, a3);
tcdrain(fd);
usleep(0x186A0u);
if ( v7 == &v11 )
break;
v8 = *(unsigned int *)v7;
v7 += 4;
}
serialSpeed((unsigned int)fd, a2, a3);
tcflush(fd, 2);
result = 0LL;
}
return result;
}

original_code:
int
sirfSetProto(int pfd, struct termios *term, unsigned int speed, unsigned int proto){
 int i;
 int spd[8] = {115200, 57600, 38400, 28800, 19200, 14400, 9600, 4800};
 static unsigned char sirf[] = {
    0xa0,0xa2,
    0x00,0x31,
    0xa5,
    0x00,0,0, 0,0,0,0, 8,1,0, 0,0,
    0xff,0,0, 0,0,0,0, 0,0,0, 0,0,
    0xff,0,0, 0,0,0,0, 0,0,0, 0,0,
    0xff,0,0, 0,0,0,0, 0,0,0, 0,0,
    0x00,0x00,
    0xb0,0xb3};
 if (serialConfig(pfd, term, 38400) == -1)
  return -1;
 sirf[7] = sirf[6] = (unsigned char)proto;
 sirf[8] = (unsigned char)((speed & 0xff000000) >> 24);
 sirf[9] = (unsigned char)((speed & 0xff0000) >> 16);
 sirf[10] = (unsigned char)((speed & 0xff00) >> 8);
 sirf[11] = (unsigned char)(speed & 0xff);
 (void)sirf_write(pfd, sirf);
 write(pfd, "\r\n", 2);
 nmea_lowlevel_send(pfd, "$PSRF100,%u,%u,8,1,0", proto, speed);
 for(i = 0; i < (int)(sizeof(spd)/sizeof(spd[0])); i++) {
  (void)serialSpeed(pfd, term, spd[i]);
  (void)sirf_write(pfd, sirf);
  write(pfd, "\r\n", 2);
  nmea_lowlevel_send(pfd, "$PSRF100,%u,%u,8,1,0", proto, speed);
  (void)tcdrain(pfd);
  (void)usleep(100000);
 }
 (void)serialSpeed(pfd, term, (int)speed);
 (void)tcflush(pfd, 2);
 return 0;
}

variable_types: pfd int

variable_types:
speed
unsigned int

variable_types:
proto
unsigned int

variable_types: i int

variable_types:
spd
int [8]

variable_types:
sirf
unsigned char []

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


decompiled_code:
__int64 __fastcall mdproto_pkt_csum(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
int v3; // eax

if ( !a2 )
return 0LL;
v2 = &a1[a2];
v3 = 0;
do
LOBYTE(v3) = *a1++ + v3;
while ( a1 != v2 );
return (unsigned int)-v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
int v3; // eax

if ( !a2 )
return 0LL;
v2 = &a1[a2];
v3 = 0;
do
LOBYTE(v3) = *a1++ + v3;
while ( a1 != v2 );
return (unsigned int)-v3;
}

original_code:
uint8_t mdproto_pkt_csum(void *buf, size_t size)
{
   uint8_t csum = 0;
   size_t i;
   for (i=0; i < size ; i++)
      csum += ((uint8_t *)buf)[i];
   return (uint8_t)(0 - csum);
}

canonical_original_code:
unsigned char mdproto_pkt_csum(void *buf, unsigned long size)
{
   unsigned char csum = 0;
   unsigned long i;
   for (i=0; i < size ; i++)
      csum += ((unsigned char *)buf)[i];
   return (unsigned char)(0 - csum);
}

variable_types:
buf
void *

variable_types:
size
unsigned long

variable_types:
csum
unsigned char

variable_types:
i
unsigned long

return_type: unsigned char


decompiled_code:
__int64 __fastcall mdproto_pkt_init(_BYTE *a1, char a2, __int64 a3, unsigned int a4)
{
__int64 i; // rax
char v5; // al
__int64 v6; // r8
__int64 v7; // r9
int v8; // ecx

if ( a4 > 0x1FC )
return 0xFFFFFFFFLL;
a1[2] = a2;
*(_WORD *)a1 = ((a4 + 1) >> 8) | (((_WORD)a4 + 1) << 8);
if ( a4 )
{
for ( i = 0LL; i != a4; ++i )
a1[i + 3] = *(_BYTE *)(a3 + i);
}
v5 = mdproto_pkt_csum(a1, a4 + 3);
*(_BYTE *)(v6 + v7 + 2) = v5;
return (unsigned int)(v8 + 4);
}

original_code:
int mdproto_pkt_init(struct mdproto_cmd_buf_t *buf,
      unsigned cmd_id,
      void *raw_data,
      unsigned raw_data_size)
{
   unsigned data_size;
   unsigned i;
   if (raw_data_size > 508)
      return -1;
   data_size = raw_data_size+1;
   buf->data.id = cmd_id;
   buf->size = (data_size << 8) | (data_size >> 8);
   for (i=0; i < raw_data_size; i++)
      buf->data.p[i+1] = ((uint8_t *)raw_data)[i];
   buf->data.p[raw_data_size+1] = mdproto_pkt_csum(buf, data_size+2);
   return raw_data_size+4;
}

canonical_original_code:
int mdproto_pkt_init(struct mdproto_cmd_buf_t *buf, unsigned int cmd_id, void *raw_data, unsigned int raw_data_size)
{
   unsigned int data_size;
   unsigned int i;
   if (raw_data_size > 508)
      return -1;
   data_size = raw_data_size+1;
   buf->data.id = cmd_id;
   buf->size = (data_size << 8) | (data_size >> 8);
   for (i=0; i < raw_data_size; i++)
      buf->data.p[i+1] = ((unsigned char *)raw_data)[i];
   buf->data.p[raw_data_size+1] = mdproto_pkt_csum(buf, data_size+2);
   return raw_data_size+4;
}

variable_types: struct mdproto_cmd_buf_t { unsigned short size; union { unsigned char id; unsigned char p[512]; } data; unsigned char _csum_buf; } *

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall mdproto_pkt_append(_WORD *a1, char *a2, int a3)
{
unsigned int v5; // ecx
unsigned int v6; // er10
unsigned int v7; // edx
unsigned int v8; // ecx
__int64 v9; // r9
char v10; // al
__int64 v11; // rdi

v5 = (unsigned __int16)((unsigned __int8)HIBYTE(*a1) | (unsigned __int16)(*a1 << 8));
v6 = v5 + a3;
if ( v5 + a3 > 0x1FC )
return 0xFFFFFFFFLL;
v7 = ((v5 + a3) >> 8) + a3 - *((unsigned __int8 *)a1 + v5 + 2) - (v5 >> 8);
if ( a3 )
{
v8 = v5 - (_DWORD)a2;
v9 = (__int64)&a2[a3 - 1 + 1];
do
{
v10 = *a2;
v11 = v8 + (unsigned int)a2++;
*((_BYTE *)a1 + v11 + 2) = v10;
LOBYTE(v7) = v10 + v7;
}
while ( a2 != (char *)v9 );
}
*a1 = (v6 >> 8) | ((_WORD)v6 << 8);
*((_BYTE *)a1 + v6 + 2) = -(char)v7;
return v6 + 3;
}

canonical_decompiled_code:
__int64 __fastcall func2(_WORD *a1, char *a2, int a3)
{
unsigned int v5; // ecx
unsigned int v6; // er10
unsigned int v7; // edx
unsigned int v8; // ecx
__int64 v9; // r9
char v10; // al
__int64 v11; // rdi

v5 = (unsigned __int16)((unsigned __int8)HIBYTE(*a1) | (unsigned __int16)(*a1 << 8));
v6 = v5 + a3;
if ( v5 + a3 > 0x1FC )
return 0xFFFFFFFFLL;
v7 = ((v5 + a3) >> 8) + a3 - *((unsigned __int8 *)a1 + v5 + 2) - (v5 >> 8);
if ( a3 )
{
v8 = v5 - (_DWORD)a2;
v9 = (__int64)&a2[a3 - 1 + 1];
do
{
v10 = *a2;
v11 = v8 + (unsigned int)a2++;
*((_BYTE *)a1 + v11 + 2) = v10;
LOBYTE(v7) = v10 + v7;
}
while ( a2 != (char *)v9 );
}
*a1 = (v6 >> 8) | ((_WORD)v6 << 8);
*((_BYTE *)a1 + v6 + 2) = -(char)v7;
return v6 + 3;
}

original_code:
int mdproto_pkt_append(struct mdproto_cmd_buf_t *buf,
      void *data, unsigned appended_size)
{
   unsigned i;
   unsigned payload_size;
   unsigned new_size;
   int8_t csum;
   payload_size = ((((*buf).size << 8) | (((*buf).size >> 8) & 0xff)) & 0xffff);
   new_size = payload_size + appended_size;
   if (new_size > 508)
      return -1;
   csum = (int8_t)(0 - buf->data.p[payload_size]);
   csum -= (int8_t)((payload_size >> 8)&0xff);
   csum -= (int8_t)(payload_size & 0xff);
   csum += (int8_t)((new_size >> 8)&0xff);
   csum += (int8_t)(new_size & 0xff);
   for(i=0; i<appended_size; i++) {
      buf->data.p[payload_size+i] = ((uint8_t *)data)[i];
      csum += ((uint8_t *)data)[i];
   }
   buf->size = (new_size << 8) | (new_size >> 8);
   buf->data.p[new_size] = (uint8_t)(0-csum);
   return new_size+3;
}

canonical_original_code:
int mdproto_pkt_append(struct mdproto_cmd_buf_t *buf, void *data, unsigned int appended_size)
{
   unsigned int i;
   unsigned int payload_size;
   unsigned int new_size;
   char csum;
   payload_size = ((((*buf).size << 8) | (((*buf).size >> 8) & 0xff)) & 0xffff);
   new_size = payload_size + appended_size;
   if (new_size > 508)
      return -1;
   csum = (char)(0 - buf->data.p[payload_size]);
   csum -= (char)((payload_size >> 8)&0xff);
   csum -= (char)(payload_size & 0xff);
   csum += (char)((new_size >> 8)&0xff);
   csum += (char)(new_size & 0xff);
   for(i=0; i<appended_size; i++) {
      buf->data.p[payload_size+i] = ((unsigned char *)data)[i];
      csum += ((unsigned char *)data)[i];
   }
   buf->size = (new_size << 8) | (new_size >> 8);
   buf->data.p[new_size] = (unsigned char)(0-csum);
   return new_size+3;
}

variable_types:
data
void *

variable_types: csum char

user_defined_types: struct mdproto_cmd_buf_t { unsigned short size; union { unsigned char id; unsigned char p[512]; } data; unsigned char _csum_buf; }


serialSpeed

decompiled_code:
__int64 __fastcall serialSpeed(int fd, struct termios *termios_p, int a3)
{
speed_t v3; // er12
int v4; // er13

if ( a3 == 38400 )
{
v3 = 15;
}
else if ( a3 > 38400 )
{
if ( a3 == 57600 )
{
v3 = 4097;
}
else
{
if ( a3 != 115200 )
goto LABEL_14;
v3 = 4098;
}
}
else if ( a3 == 9600 )
{
v3 = 13;
}
else
{
v3 = 14;
if ( a3 != 19200 )
{
v3 = 13;
if ( a3 != 4800 )
{
LABEL_14:
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}
}
}
v4 = 0;
tcgetattr(fd, termios_p);
cfsetispeed(termios_p, v3);
cfsetospeed(termios_p, v3);
while ( tcsetattr(fd, 2, termios_p) == -1 )
{
if ( *_errno_location() != 4 || v4 == 3 )
return 0xFFFFFFFFLL;
++v4;
usleep(0x3E8u);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, struct termios *termios_p, int a3)
{
speed_t v3; // er12
int v4; // er13

if ( a3 == 38400 )
{
v3 = 15;
}
else if ( a3 > 38400 )
{
if ( a3 == 57600 )
{
v3 = 4097;
}
else
{
if ( a3 != 115200 )
goto LABEL_14;
v3 = 4098;
}
}
else if ( a3 == 9600 )
{
v3 = 13;
}
else
{
v3 = 14;
if ( a3 != 19200 )
{
v3 = 13;
if ( a3 != 4800 )
{
LABEL_14:
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}
}
}
v4 = 0;
tcgetattr(fd, termios_p);
cfsetispeed(termios_p, v3);
cfsetospeed(termios_p, v3);
while ( tcsetattr(fd, 2, termios_p) == -1 )
{
if ( *_errno_location() != 4 || v4 == 3 )
return 0xFFFFFFFFLL;
++v4;
usleep(0x3E8u);
}
return 0LL;
}

original_code:
int serialSpeed(int pfd, struct termios *term, int speed){
 int rv;
 int r = 0;
 switch(speed){
 case 115200:
  speed = 0010002;
  break;
 case 57600:
  speed = 0010001;
  break;
 case 38400:
  speed = 0000017;
  break;
 case 19200:
  speed = 0000016;
  break;
 case 9600:
  speed = 0000015;
  break;
 case 4800:
  speed = 0000015;
  break;
 default:
  (*__errno_location ()) = 22;
  return -1;
 }
 (int)tcgetattr(pfd, term);
 cfsetispeed(term, speed);
 cfsetospeed(term, speed);
 while (((rv = tcsetattr(pfd, 2, term)) == -1) && ((*__errno_location ()) == 4) && (r < 3)) {
  (void)usleep(1000);
  r++;
 }
 return rv == -1 ? -1 : 0;
}

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

variable_types: speed int

variable_types: rv int

variable_types: r int


serialConfig

decompiled_code:
__int64 __fastcall serialConfig(int fd, struct termios *termios_p, int a3)
{
int v4; // ebx
tcflag_t v5; // eax

v4 = 0;
tcgetattr(fd, termios_p);
cfmakeraw(termios_p);
v5 = termios_p->c_cflag;
termios_p->c_lflag &= 0xFFFFFFFD;
termios_p->c_cflag = v5 & 0xFFFFFC8F | 0x880;
*(_QWORD *)&termios_p->c_iflag &= 0x7FFFFFFEFFFFEBCFuLL;
*(_WORD *)&termios_p->c_cc[5] = 258;
while ( 1 )
{
if ( tcsetattr(fd, 2, termios_p) != -1 )
return serialSpeed(fd, termios_p, a3);
if ( *_errno_location() != 4 || v4 == 3 )
break;
++v4;
usleep(0x3E8u);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, struct termios *termios_p, int a3)
{
int v4; // ebx
tcflag_t v5; // eax

v4 = 0;
tcgetattr(fd, termios_p);
cfmakeraw(termios_p);
v5 = termios_p->c_cflag;
termios_p->c_lflag &= 0xFFFFFFFD;
termios_p->c_cflag = v5 & 0xFFFFFC8F | 0x880;
*(_QWORD *)&termios_p->c_iflag &= 0x7FFFFFFEFFFFEBCFuLL;
*(_WORD *)&termios_p->c_cc[5] = 258;
while ( 1 )
{
if ( tcsetattr(fd, 2, termios_p) != -1 )
return func1(fd, termios_p, a3);
if ( *_errno_location() != 4 || v4 == 3 )
break;
++v4;
usleep(0x3E8u);
}
return 0xFFFFFFFFLL;
}

variable_types: pfd int


expect

decompiled_code:
__int64 __fastcall expect(int fd, __int64 a2, __int64 a3, int a4)
{
__int64 v6; // rbx
ssize_t v7; // rax
double v9; // [rsp+8h] [rbp-40h]
char buf[41]; // [rsp+1Fh] [rbp-29h] BYREF

v6 = 0LL;
v9 = (double)(int)clock();
do
{
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 < 0 )
return 0LL;
if ( v7 )
break;
if ( (double)(int)clock() - v9 > (double)a4 * 1000000.0 )
return 0LL;
}
gpsd_report(2LL, "I see %zd: %02x\n", v6, (unsigned __int8)buf[0]);
if ( (double)(int)clock() - v9 > (double)a4 * 1000000.0 )
return 0LL;
if ( *(_BYTE *)(a2 + v6) == buf[0] )
++v6;
else
v6 = 0LL;
}
while ( v6 != a3 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2, __int64 a3, int a4)
{
__int64 v6; // rbx
ssize_t v7; // rax
double v9; // [rsp+8h] [rbp-40h]
char buf[41]; // [rsp+1Fh] [rbp-29h] BYREF

v6 = 0LL;
v9 = (double)(int)clock();
do
{
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 < 0 )
return 0LL;
if ( v7 )
break;
if ( (double)(int)clock() - v9 > (double)a4 * 1000000.0 )
return 0LL;
}
gpsd_report(2LL, "I see %zd: %02x\n", v6, (unsigned __int8)buf[0]);
if ( (double)(int)clock() - v9 > (double)a4 * 1000000.0 )
return 0LL;
if ( *(_BYTE *)(a2 + v6) == buf[0] )
++v6;
else
v6 = 0LL;
}
while ( v6 != a3 );
return 1LL;
}

original_code:
int expect(int pfd, const char *str, size_t len, time_t timeout)
{
    size_t got = 0;
    char ch;
    ssize_t read_cnt;
    double start = clock();
    for (;;) {
        read_cnt = read(pfd, &ch, 1);
 if (read_cnt < 0)
     return 0;
 if (read_cnt > 0)
    gpsd_report(2, "I see %zd: %02x\n", got, (unsigned)(ch & 0xff));
 if ((clock() - start) > (double)timeout*((__clock_t) 1000000))
     return 0;
 if (read_cnt > 0) {
    if (ch == str[got])
        got++;
    else
        got = 0;
     if (got == len)
        return 1;
 }
    }
}

canonical_original_code:
int expect(int pfd, char *str, unsigned long len, long timeout)
{
    unsigned long got = 0;
    char ch;
    long read_cnt;
    double start = clock();
    for (;;) {
        read_cnt = read(pfd, &ch, 1);
 if (read_cnt < 0)
     return 0;
 if (read_cnt > 0)
    gpsd_report(2, "I see %zd: %02x\n", got, (unsigned int)(ch & 0xff));
 if ((clock() - start) > (double)timeout*((long) 1000000))
     return 0;
 if (read_cnt > 0) {
    if (ch == str[got])
        got++;
    else
        got = 0;
     if (got == len)
        return 1;
 }
    }
}

variable_types:
str
char *

variable_types:
len
unsigned long

variable_types: timeout long

variable_types:
got
unsigned long

variable_types: ch char

variable_types: start double


decompiled_code:
__int64 __fastcall read_full(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 i; // rbx
ssize_t v5; // rax

if ( !a3 )
return 0LL;
for ( i = 0LL; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( v5 < 0 )
return 0xFFFFFFFFLL;
}
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 i; // rbx
ssize_t v5; // rax

if ( !a3 )
return 0LL;
for ( i = 0LL; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( v5 < 0 )
return 0xFFFFFFFFLL;
}
return (unsigned int)i;
}

original_code:
int read_full(int d, void *buf, size_t nbytes)
{
    size_t got = 0;
    ssize_t read_cnt;
    while(got < nbytes) {
       read_cnt = read(d, &((uint8_t *)buf)[got], nbytes-got);
       if (read_cnt < 0)
   return -1;
       got += read_cnt;
    }
    return (int)got;
}

canonical_original_code:
int read_full(int d, void *buf, unsigned long nbytes)
{
    unsigned long got = 0;
    long read_cnt;
    while(got < nbytes) {
       read_cnt = read(d, &((unsigned char *)buf)[got], nbytes-got);
       if (read_cnt < 0)
   return -1;
       got += read_cnt;
    }
    return (int)got;
}

variable_types: d int

variable_types:
buf
void *

variable_types:
nbytes
unsigned long


decompiled_code:
__int64 __fastcall read_mdproto_pkt(int fd, __int16 *a2)
{
int v2; // eax
unsigned int v3; // er8
unsigned __int16 v4; // ax
__int64 result; // rax
int v6; // ebx
int v7; // er13
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
char v11; // r12
int *v12; // rax
char *v13; // rax

v2 = read_full(fd, (__int64)a2, 2uLL);
if ( v2 < 0LL )
{
v12 = _errno_location();
v13 = strerror(*v12);
gpsd_report(1LL, "read() error: %s\n", v13);
result = 46LL;
}
else
{
v3 = 46;
if ( v2 <= 1LL
|| (v4 = _bswap_16(*a2), v4 > 0x200u)
|| (v6 = v4, v7 = v4 + 1, v8 = read_full(fd, (__int64)(a2 + 1), v7), v3 = 44, v7 > v8) )
{
result = v3;
}
else
{
v11 = *((_BYTE *)a2 + v6 + 2);
result = 8 * (unsigned int)(v11 == (char)mdproto_pkt_csum(a2, v6 + 2, v9, v10, 44LL)) + 35;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, __int16 *a2)
{
int v2; // eax
unsigned int v3; // er8
unsigned __int16 v4; // ax
__int64 result; // rax
int v6; // ebx
int v7; // er13
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
char v11; // r12
int *v12; // rax
char *v13; // rax

v2 = func4(fd, (__int64)a2, 2uLL);
if ( v2 < 0LL )
{
v12 = _errno_location();
v13 = strerror(*v12);
gpsd_report(1LL, "read() error: %s\n", v13);
result = 46LL;
}
else
{
v3 = 46;
if ( v2 <= 1LL
|| (v4 = func0(*a2), v4 > 0x200u)
|| (v6 = v4, v7 = v4 + 1, v8 = func4(fd, (__int64)(a2 + 1), v7), v3 = 44, v7 > v8) )
{
result = v3;
}
else
{
v11 = *((_BYTE *)a2 + v6 + 2);
result = 8 * (unsigned int)(v11 == (char)mdproto_pkt_csum(a2, v6 + 2, v9, v10, 44LL)) + 35;
}
}
return result;
}

original_code:
int read_mdproto_pkt(int pfd, struct mdproto_cmd_buf_t *dst)
{
   ssize_t cnt;
   uint16_t size;
   cnt = read_full(pfd, (void *)&dst->size, sizeof(dst->size));
   if (cnt < 0) {
      gpsd_report(1, "read() error: %s\n", strerror((*__errno_location ())));
      return MDPROTO_STATUS_READ_HEADER_TIMEOUT;
   }
   if (cnt < (ssize_t)sizeof(dst->size))
      return MDPROTO_STATUS_READ_HEADER_TIMEOUT;
   size = ntohs(dst->size);
   if (size > sizeof(dst->data.p))
      return MDPROTO_STATUS_TOO_BIG;
   cnt = read_full(pfd, (void *)dst->data.p, size+1);
   if (cnt < size+1)
      return MDPROTO_STATUS_READ_DATA_TIMEOUT;
   if (dst->data.p[size] != mdproto_pkt_csum(dst, size+2))
      return MDPROTO_STATUS_WRONG_CSUM;
   return MDPROTO_STATUS_OK;
}

canonical_original_code:
int read_mdproto_pkt(int pfd, struct mdproto_cmd_buf_t *dst)
{
   long cnt;
   unsigned short size;
   cnt = read_full(pfd, (void *)&dst->size, sizeof(dst->size));
   if (cnt < 0) {
      gpsd_report(1, "read() error: %s\n", strerror((*__errno_location ())));
      return MDPROTO_STATUS_READ_HEADER_TIMEOUT;
   }
   if (cnt < (long)sizeof(dst->size))
      return MDPROTO_STATUS_READ_HEADER_TIMEOUT;
   size = ntohs(dst->size);
   if (size > sizeof(dst->data.p))
      return MDPROTO_STATUS_TOO_BIG;
   cnt = read_full(pfd, (void *)dst->data.p, size+1);
   if (cnt < size+1)
      return MDPROTO_STATUS_READ_DATA_TIMEOUT;
   if (dst->data.p[size] != mdproto_pkt_csum(dst, size+2))
      return MDPROTO_STATUS_WRONG_CSUM;
   return MDPROTO_STATUS_OK;
}

variable_types: struct mdproto_cmd_buf_t { unsigned short size; union { unsigned char id; unsigned char p[512]; } data; unsigned char _csum_buf; } *

variable_types: cnt long

variable_types:
size
unsigned short


decompiled_code:
void __noreturn shutdown_server()
{
threadpool_destroy(threadpool);
unload_seats();
close(listenfd);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
threadpool_destroy(threadpool);
unload_seats();
close(listenfd);
exit(0);
}

original_code:
void shutdown_server(int signo){
    threadpool_destroy(threadpool);
    unload_seats();
    close(listenfd);
    exit(0);
}

variable_types: signo int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
socklen_t v4; // edx
int *v5; // rax
int *v6; // rax
int optval; // [rsp+8h] [rbp-420h] BYREF
int v8; // [rsp+Ch] [rbp-41Ch] BYREF
struct sockaddr v9; // [rsp+10h] [rbp-418h] BYREF
__int64 v10; // [rsp+420h] [rbp-8h] BYREF

v3 = 20;
listenfd = 0;
v8 = 0;
if ( argc > 1 )
v3 = atoi(argv[1]);
if ( signal(2, (__sighandler_t)shutdown_server) == (__sighandler_t)-1LL )
printf("Issue registering SIGINT handler");
listenfd = socket(2, 1, 0);
if ( listenfd >= 0 )
{
printf("Established Socket: %d\n", (unsigned int)listenfd);
optval = 1;
setsockopt(listenfd, 1, 2, &optval, 4u);
threadpool = threadpool_create(10LL, 50LL);
load_seats(v3);
memset(&v9.sa_data[6], 48, 1032);
v9.sa_family = 2;
*(_DWORD *)&v9.sa_data[2] = _bswap_32_constprop_0(&v10, &v9, 16LL);
*(_WORD *)v9.sa_data = _bswap_16_constprop_0();
if ( !bind(listenfd, &v9, v4) )
{
listen(listenfd, 10);
while ( 1 )
{
v8 = accept(listenfd, 0LL, 0LL);
threadpool_add_task(threadpool, &handle_connection_wrapper, &v8);
}
}
perror("socket--bind");
v5 = _errno_location();
exit(*v5);
}
perror("Socket");
v6 = _errno_location();
exit(*v6);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
socklen_t v4; // edx
int *v5; // rax
int *v6; // rax
int optval; // [rsp+8h] [rbp-420h] BYREF
int v8; // [rsp+Ch] [rbp-41Ch] BYREF
struct sockaddr v9; // [rsp+10h] [rbp-418h] BYREF
__int64 v10; // [rsp+420h] [rbp-8h] BYREF

v3 = 20;
listenfd = 0;
v8 = 0;
if ( argc > 1 )
v3 = atoi(argv[1]);
if ( signal(2, (__sighandler_t)shutdown_server) == (__sighandler_t)-1LL )
printf("Issue registering SIGINT handler");
listenfd = socket(2, 1, 0);
if ( listenfd >= 0 )
{
printf("Established Socket: %d\n", (unsigned int)listenfd);
optval = 1;
setsockopt(listenfd, 1, 2, &optval, 4u);
threadpool = threadpool_create(10LL, 50LL);
load_seats(v3);
memset(&v9.sa_data[6], 48, 1032);
v9.sa_family = 2;
*(_DWORD *)&v9.sa_data[2] = func1(&v10, &v9, 16LL);
*(_WORD *)v9.sa_data = func2();
if ( !bind(listenfd, &v9, v4) )
{
listen(listenfd, 10);
while ( 1 )
{
v8 = accept(listenfd, 0LL, 0LL);
threadpool_add_task(threadpool, &handle_connection_wrapper, &v8);
}
}
perror("socket--bind");
v5 = _errno_location();
exit(*v5);
}
perror("Socket");
v6 = _errno_location();
exit(*v6);
}

original_code:
int main(int argc,char *argv[])
{
    int flag, num_seats = 20;
    int connfd = 0;
    struct sockaddr_in serv_addr;
    char send_buffer[1024];
    listenfd = 0;
    int server_port = 8080;
    if (argc > 1)
    {
        num_seats = atoi(argv[1]);
    }
    if (server_port < 1500)
    {
        fprintf(stderr,"INVALID PORT NUMBER: %d; can't be < 1500\n",server_port);
        exit(-1);
    }
    if (signal(2, shutdown_server) == ((__sighandler_t) -1))
        printf("Issue registering SIGINT handler");
    listenfd = socket(2, SOCK_STREAM, 0);
    if ( listenfd < 0 ){
        perror("Socket");
        exit((*__errno_location ()));
    }
    printf("Established Socket: %d\n", listenfd);
    flag = 1;
    setsockopt( listenfd, 1, 2, &flag, sizeof(flag) );
    threadpool = threadpool_create(10,50);
    load_seats(num_seats);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(send_buffer, '0', sizeof(send_buffer));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    serv_addr.sin_port = htons(server_port);
    if ( bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) != 0)
    {
        perror("socket--bind");
        exit((*__errno_location ()));
    }
    listen(listenfd, 10);
    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)((void *)0), ((void *)0));
        threadpool_add_task(threadpool, handle_connection_wrapper, (void*) &connfd);
    }
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int flag; int num_seats = 20;
    int connfd = 0;
    struct sockaddr_in serv_addr;
    char send_buffer[1024];
    listenfd = 0;
    int server_port = 8080;
    if (argc > 1)
    {
        num_seats = atoi(argv[1]);
    }
    if (server_port < 1500)
    {
        fprintf(stderr,"INVALID PORT NUMBER: %d; can't be < 1500\n",server_port);
        exit(-1);
    }
    if (signal(2, shutdown_server) == ((__sighandler_t) -1))
        printf("Issue registering SIGINT handler");
    listenfd = socket(2, SOCK_STREAM, 0);
    if ( listenfd < 0 ){
        perror("Socket");
        exit((*__errno_location ()));
    }
    printf("Established Socket: %d\n", listenfd);
    flag = 1;
    setsockopt( listenfd, 1, 2, &flag, sizeof(flag) );
    threadpool = threadpool_create(10,50);
    load_seats(num_seats);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(send_buffer, '0', sizeof(send_buffer));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    serv_addr.sin_port = htons(server_port);
    if ( bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0)
    {
        perror("socket--bind");
        exit((*__errno_location ()));
    }
    listen(listenfd, 10);
    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr *)((void *)0), ((void *)0));
        threadpool_add_task(threadpool, handle_connection_wrapper, (void *) &connfd);
    }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: flag int

variable_types: connfd int

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

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

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


decompiled_code:
void __fastcall load_seats(int a1)
{
int v1; // ebp
_QWORD *v2; // rbx
_QWORD *v3; // r12
char *v4; // rax

if ( a1 > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
while ( 1 )
{
v3 = v2;
v4 = (char *)malloc(0x40uLL);
*(_DWORD *)v4 = v1;
v2 = v4;
*(_QWORD *)(v4 + 4) = 0xFFFFFFFFLL;
*((_QWORD *)v4 + 2) = 0LL;
pthread_mutex_init((pthread_mutex_t *)(v4 + 24), 0LL);
if ( !seat_header )
break;
++v1;
v3[2] = v2;
if ( a1 == v1 )
return;
}
++v1;
seat_header = v2;
}
while ( a1 != v1 );
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
int v1; // ebp
_QWORD *v2; // rbx
_QWORD *v3; // r12
char *v4; // rax

if ( a1 > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
while ( 1 )
{
v3 = v2;
v4 = (char *)malloc(0x40uLL);
*(_DWORD *)v4 = v1;
v2 = v4;
*(_QWORD *)(v4 + 4) = 0xFFFFFFFFLL;
*((_QWORD *)v4 + 2) = 0LL;
pthread_mutex_init((pthread_mutex_t *)(v4 + 24), 0LL);
if ( !seat_header )
break;
++v1;
v3[2] = v2;
if ( a1 == v1 )
return;
}
++v1;
seat_header = v2;
}
while ( a1 != v1 );
}
}

original_code:
void load_seats(int number_of_seats)
{
    seat_t* curr = ((void *)0);
    int i;
    for(i = 0; i < number_of_seats; i++)
    {
        seat_t* temp = (seat_t*) malloc(sizeof(seat_t));
        temp->id = i;
        temp->customer_id = -1;
        temp->state = AVAILABLE;
        temp->next = ((void *)0);
        pthread_mutex_init(&(temp->lock), ((void *)0));
        if (seat_header == ((void *)0))
        {
            seat_header = temp;
        }
        else
        {
            curr-> next = temp;
        }
        curr = temp;
    }
}

canonical_original_code:
void load_seats(int number_of_seats)
{
    struct seat_struct *curr = ((void *)0);
    int i;
    for(i = 0; i < number_of_seats; i++)
    {
        struct seat_struct *temp = (seat_t*) malloc(sizeof(seat_t));
        temp->id = i;
        temp->customer_id = -1;
        temp->state = AVAILABLE;
        temp->next = ((void *)0);
        pthread_mutex_init(&(temp->lock), ((void *)0));
        if (seat_header == ((void *)0))
        {
            seat_header = temp;
        }
        else
        {
            curr-> next = temp;
        }
        curr = temp;
    }
}

variable_types: struct seat_struct { int id; int customer_id; enum seat_state_t { AVAILABLE=0, PENDING=1, OCCUPIED=2 } state; struct seat_struct *next; 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; } lock; } *

variable_types: i int

user_defined_types: struct seat_struct { int id; int customer_id; enum seat_state_t state; struct seat_struct *next; union pthread_mutex_t lock; }

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


decompiled_code:
void unload_seats()
{
void *v0; // rbx
void *v1; // rdi

v0 = seat_header;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 2);
free(v1);
}
}

canonical_decompiled_code:
void func1()
{
void *v0; // rbx
void *v1; // rdi

v0 = seat_header;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 2);
free(v1);
}
}

original_code:
void unload_seats()
{
    seat_t* curr = seat_header;
    while(curr != ((void *)0))
    {
        seat_t* temp = curr;
        curr = curr->next;
        free(temp);
    }
}

canonical_original_code:
void unload_seats()
{
    struct seat_struct *curr = seat_header;
    while(curr != ((void *)0))
    {
        struct seat_struct *temp = curr;
        curr = curr->next;
        free(temp);
    }
}

user_defined_types: enum seat_state_t { AVAILABLE=0, PENDING=1, OCCUPIED=2 }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


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

result = 48LL;
if ( a1 <= 2 )
result = (unsigned __int8)CSWTCH_4[a1];
return result;
}

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

result = 48LL;
if ( a1 <= 2 )
result = (unsigned __int8)CSWTCH_4[a1];
return result;
}

original_code:
char seat_state_to_char(seat_state_t state)
{
    switch(state)
    {
        case AVAILABLE:
            return 'A';
        case PENDING:
            return 'P';
        case OCCUPIED:
            return 'O';
    }
    return '0';
}

canonical_original_code:
char seat_state_to_char(enum seat_state_t state)
{
    switch(state)
    {
        case AVAILABLE:
            return 'A';
        case PENDING:
            return 'P';
        case OCCUPIED:
            return 'O';
    }
    return '0';
}

variable_types: enum seat_state_t { AVAILABLE=0, PENDING=1, OCCUPIED=2 }

return_type: char


decompiled_code:
int __fastcall list_seats(char *a1, int a2)
{
unsigned int *v2; // rbx
int v3; // er12
__int64 v4; // rbp
__int64 v5; // rax
char v6; // al
const char *v7; // rdx
int v8; // ebp

v2 = (unsigned int *)seat_header;
if ( !seat_header || a2 == -6LL )
return snprintf(a1, a2, "No seats not found\n\n");
v3 = 0;
v4 = 0LL;
while ( 1 )
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 6));
v6 = seat_state_to_char(v2[2]);
v8 = snprintf(&a1[v4], a2 - v3, v7, *v2, (unsigned int)v6);
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
v2 = (unsigned int *)*((_QWORD *)v2 + 2);
v3 += v8;
if ( !v2 )
break;
v4 = v3;
v5 = v3;
if ( v3 >= (unsigned __int64)(a2 + 6LL) )
return snprintf(&a1[v5 - 1], a2 - v3 - 1, "\n");
}
v5 = v3;
return snprintf(&a1[v5 - 1], a2 - v3 - 1, "\n");
}

canonical_decompiled_code:
int __fastcall func3(char *a1, int a2)
{
unsigned int *v2; // rbx
int v3; // er12
__int64 v4; // rbp
__int64 v5; // rax
char v6; // al
const char *v7; // rdx
int v8; // ebp

v2 = (unsigned int *)seat_header;
if ( !seat_header || a2 == -6LL )
return snprintf(a1, a2, "No seats not found\n\n");
v3 = 0;
v4 = 0LL;
while ( 1 )
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 6));
v6 = func2(v2[2]);
v8 = snprintf(&a1[v4], a2 - v3, v7, *v2, (unsigned int)v6);
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
v2 = (unsigned int *)*((_QWORD *)v2 + 2);
v3 += v8;
if ( !v2 )
break;
v4 = v3;
v5 = v3;
if ( v3 >= (unsigned __int64)(a2 + 6LL) )
return snprintf(&a1[v5 - 1], a2 - v3 - 1, "\n");
}
v5 = v3;
return snprintf(&a1[v5 - 1], a2 - v3 - 1, "\n");
}

original_code:
void list_seats(char* buf, int bufsize)
{
    seat_t* curr = seat_header;
    int index = 0;
    while(curr != ((void *)0) && index < bufsize+ strlen("%d %c,"))
    {
        pthread_mutex_lock(&(curr->lock));
        int length = snprintf(buf+index, bufsize-index,
                "%d %c,", curr->id, seat_state_to_char(curr->state));
        pthread_mutex_unlock(&(curr->lock));
        if (length > 0)
            index = index + length;
        curr = curr->next;
    }
    if (index > 0)
        snprintf(buf+index-1, bufsize-index-1, "\n");
    else
        snprintf(buf, bufsize, "No seats not found\n\n");
}

canonical_original_code:
void list_seats(char *buf, int bufsize)
{
    struct seat_struct *curr = seat_header;
    int index = 0;
    while(curr != ((void *)0) && index < bufsize+ strlen("%d %c,"))
    {
        pthread_mutex_lock(&(curr->lock));
        int length = snprintf(buf+index, bufsize-index,
                "%d %c,", curr->id, seat_state_to_char(curr->state));
        pthread_mutex_unlock(&(curr->lock));
        if (length > 0)
            index = index + length;
        curr = curr->next;
    }
    if (index > 0)
        snprintf(buf+index-1, bufsize-index-1, "\n");
    else
        snprintf(buf, bufsize, "No seats not found\n\n");
}

variable_types:
buf
char *

variable_types: bufsize int

variable_types: index int

variable_types: length int


decompiled_code:
int __fastcall view_seat(char *s, int a2, int a3, unsigned int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
unsigned int v9; // edi
char v11; // al

v7 = (unsigned int *)seat_header;
if ( !seat_header )
return snprintf(s, a2, "Requested seat not found\n\n");
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Requested seat not found\n\n");
}
pthread_mutex_lock(v8);
v9 = v7[2];
if ( !v9 || v9 == 1 && v7[1] == a4 )
{
v11 = seat_state_to_char(v9);
snprintf(s, a2, "Confirm seat: %d %c ?\n\n", *v7, (unsigned int)v11);
v7[2] = 1;
v7[1] = a4;
}
else
{
snprintf(s, a2, "Seat unavailable\n\n");
}
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}

canonical_decompiled_code:
int __fastcall func4(char *s, int a2, int a3, unsigned int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
unsigned int v9; // edi
char v11; // al

v7 = (unsigned int *)seat_header;
if ( !seat_header )
return snprintf(s, a2, "Requested seat not found\n\n");
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Requested seat not found\n\n");
}
pthread_mutex_lock(v8);
v9 = v7[2];
if ( !v9 || v9 == 1 && v7[1] == a4 )
{
v11 = func2(v9);
snprintf(s, a2, "Confirm seat: %d %c ?\n\n", *v7, (unsigned int)v11);
v7[2] = 1;
v7[1] = a4;
}
else
{
snprintf(s, a2, "Seat unavailable\n\n");
}
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}

original_code:
void view_seat(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    seat_t* curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
      pthread_mutex_lock(&(curr->lock));
            if(curr->state == AVAILABLE || (curr->state == PENDING && curr->customer_id==customer_id))
            {
                snprintf(buf, bufsize, "Confirm seat: %d %c ?\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = PENDING;
                curr->customer_id = customer_id;
            }
            else
            {
                snprintf(buf, bufsize, "Seat unavailable\n\n");
            }
 pthread_mutex_unlock(&(curr->lock));
            return;
        }
        pthread_mutex_unlock(&(curr->lock));
 curr = curr->next;
    }
    snprintf(buf, bufsize, "Requested seat not found\n\n");
    return;
}

canonical_original_code:
void view_seat(char *buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    struct seat_struct *curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
      pthread_mutex_lock(&(curr->lock));
            if(curr->state == AVAILABLE || (curr->state == PENDING && curr->customer_id==customer_id))
            {
                snprintf(buf, bufsize, "Confirm seat: %d %c ?\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = PENDING;
                curr->customer_id = customer_id;
            }
            else
            {
                snprintf(buf, bufsize, "Seat unavailable\n\n");
            }
 pthread_mutex_unlock(&(curr->lock));
            return;
        }
        pthread_mutex_unlock(&(curr->lock));
 curr = curr->next;
    }
    snprintf(buf, bufsize, "Requested seat not found\n\n");
    return;
}


decompiled_code:
int __fastcall confirm_seat(char *s, int a2, int a3, int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
char v10; // al
const char *v11; // rdx

v7 = (unsigned int *)seat_header;
if ( seat_header )
{
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Requested seat not found\n\n");
}
pthread_mutex_lock(v8);
if ( v7[2] == 1 )
{
if ( v7[1] == a4 )
{
v10 = seat_state_to_char(1u);
snprintf(s, a2, v11, *v7, (unsigned int)v10);
v7[2] = 2;
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
}
else if ( a4 == v7[1] )
{
snprintf(s, a2, "No pending request\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
snprintf(s, a2, "Permission denied - seat held by another user\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
return snprintf(s, a2, "Requested seat not found\n\n");
}

canonical_decompiled_code:
int __fastcall func5(char *s, int a2, int a3, int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
char v10; // al
const char *v11; // rdx

v7 = (unsigned int *)seat_header;
if ( seat_header )
{
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Requested seat not found\n\n");
}
pthread_mutex_lock(v8);
if ( v7[2] == 1 )
{
if ( v7[1] == a4 )
{
v10 = func2(1u);
snprintf(s, a2, v11, *v7, (unsigned int)v10);
v7[2] = 2;
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
}
else if ( a4 == v7[1] )
{
snprintf(s, a2, "No pending request\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
snprintf(s, a2, "Permission denied - seat held by another user\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
return snprintf(s, a2, "Requested seat not found\n\n");
}

original_code:
void confirm_seat(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    seat_t* curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
     pthread_mutex_lock(&(curr->lock));
            if(curr->state == PENDING && curr->customer_id == customer_id )
            {
                snprintf(buf, bufsize, "Seat confirmed: %d %c\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = OCCUPIED;
            }
            else if(curr->customer_id != customer_id )
            {
                snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n");
            }
            else if(curr->state != PENDING)
            {
                snprintf(buf, bufsize, "No pending request\n\n");
            }
     pthread_mutex_unlock(&(curr->lock));
            return;
        }
 pthread_mutex_unlock(&(curr->lock));
        curr = curr->next;
    }
    snprintf(buf, bufsize, "Requested seat not found\n\n");
    return;
}

canonical_original_code:
void confirm_seat(char *buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    struct seat_struct *curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
     pthread_mutex_lock(&(curr->lock));
            if(curr->state == PENDING && curr->customer_id == customer_id )
            {
                snprintf(buf, bufsize, "Seat confirmed: %d %c\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = OCCUPIED;
            }
            else if(curr->customer_id != customer_id )
            {
                snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n");
            }
            else if(curr->state != PENDING)
            {
                snprintf(buf, bufsize, "No pending request\n\n");
            }
     pthread_mutex_unlock(&(curr->lock));
            return;
        }
 pthread_mutex_unlock(&(curr->lock));
        curr = curr->next;
    }
    snprintf(buf, bufsize, "Requested seat not found\n\n");
    return;
}


cancel

decompiled_code:
int __fastcall cancel(char *s, int a2, unsigned int a3, unsigned int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
char v10; // al
const char *v11; // rdx

printf("Cancelling seat %d for user %d\n", a3, a4);
v7 = (unsigned int *)seat_header;
if ( seat_header )
{
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Seat not found\n\n");
}
pthread_mutex_lock(v8);
if ( v7[2] == 1 )
{
if ( v7[1] == a4 )
{
v10 = seat_state_to_char(1u);
snprintf(s, a2, v11, *v7, (unsigned int)v10);
v7[2] = 0;
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
}
else if ( a4 == v7[1] )
{
snprintf(s, a2, "No pending request\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
snprintf(s, a2, "Permission denied - seat held by another user\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
return snprintf(s, a2, "Seat not found\n\n");
}

canonical_decompiled_code:
int __fastcall func6(char *s, int a2, unsigned int a3, unsigned int a4)
{
unsigned int *v7; // rbx
pthread_mutex_t *v8; // rdi
char v10; // al
const char *v11; // rdx

printf("Cancelling seat %d for user %d\n", a3, a4);
v7 = (unsigned int *)seat_header;
if ( seat_header )
{
while ( 1 )
{
v8 = (pthread_mutex_t *)(v7 + 6);
if ( *v7 == a3 )
break;
pthread_mutex_unlock(v8);
v7 = (unsigned int *)*((_QWORD *)v7 + 2);
if ( !v7 )
return snprintf(s, a2, "Seat not found\n\n");
}
pthread_mutex_lock(v8);
if ( v7[2] == 1 )
{
if ( v7[1] == a4 )
{
v10 = func2(1u);
snprintf(s, a2, v11, *v7, (unsigned int)v10);
v7[2] = 0;
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
}
else if ( a4 == v7[1] )
{
snprintf(s, a2, "No pending request\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
snprintf(s, a2, "Permission denied - seat held by another user\n\n");
return pthread_mutex_unlock((pthread_mutex_t *)(v7 + 6));
}
return snprintf(s, a2, "Seat not found\n\n");
}

original_code:
void cancel(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    printf("Cancelling seat %d for user %d\n", seat_id, customer_id);
    seat_t* curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
 pthread_mutex_lock(&(curr->lock));
            if(curr->state == PENDING && curr->customer_id == customer_id )
            {
                snprintf(buf, bufsize, "Seat request cancelled: %d %c\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = AVAILABLE;
            }
            else if(curr->customer_id != customer_id )
            {
                snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n");
            }
            else if(curr->state != PENDING)
            {
                snprintf(buf, bufsize, "No pending request\n\n");
            }
     pthread_mutex_unlock(&(curr->lock));
            return;
        }
        pthread_mutex_unlock(&(curr->lock));
        curr = curr->next;
    }
    snprintf(buf, bufsize, "Seat not found\n\n");
    return;
}

canonical_original_code:
void cancel(char *buf, int bufsize, int seat_id, int customer_id, int customer_priority)
{
    printf("Cancelling seat %d for user %d\n", seat_id, customer_id);
    struct seat_struct *curr = seat_header;
    while(curr != ((void *)0))
    {
        if(curr->id == seat_id)
        {
 pthread_mutex_lock(&(curr->lock));
            if(curr->state == PENDING && curr->customer_id == customer_id )
            {
                snprintf(buf, bufsize, "Seat request cancelled: %d %c\n\n",
                        curr->id, seat_state_to_char(curr->state));
                curr->state = AVAILABLE;
            }
            else if(curr->customer_id != customer_id )
            {
                snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n");
            }
            else if(curr->state != PENDING)
            {
                snprintf(buf, bufsize, "No pending request\n\n");
            }
     pthread_mutex_unlock(&(curr->lock));
            return;
        }
        pthread_mutex_unlock(&(curr->lock));
        curr = curr->next;
    }
    snprintf(buf, bufsize, "Seat not found\n\n");
    return;
}