decompiled_code:
int show_opstat()
{
const char *v0; // rax
float v1; // xmm0_4
const char *v2; // rax
__int64 v3; // rdi
const char *v4; // rax
__int64 v5; // rbx
const char *v6; // rax
unsigned __int16 v7; // ax
unsigned __int16 v8; // ax
unsigned __int64 v9; // r13
unsigned int i; // ebp
int v11; // edi
unsigned int v12; // ebx
__int64 v13; // r12
float v14; // xmm0_4
char *v15; // rax
float v16; // xmm0_4
int v17; // eax
char *v18; // rax
int result; // eax
char *v20; // rax
int v21; // eax
char *v22; // rax
__int64 j; // rbx
char *v24; // rax
char *v25; // rax
float v26; // xmm0_4
int v27; // eax
const char *v28; // rax
const char *v29; // rax
char *v30; // rax
const char *v31; // rax
float v32; // xmm0_4
float v33; // xmm0_4
float v34; // xmm0_4
__int64 k; // rbx
int v36; // eax
char v37[16]; // [rsp+0h] [rbp-258h] BYREF
char s[16]; // [rsp+10h] [rbp-248h] BYREF
char src[256]; // [rsp+20h] [rbp-238h] BYREF
char dest[268]; // [rsp+120h] [rbp-138h] BYREF
char v41[44]; // [rsp+22Ch] [rbp-2Ch] BYREF

lib_get_state(context, v41, &opstat);
putchar(10);
printf("Station Name: %s\n", (const char *)&opstat);
printf("Data Port: %d\n", (unsigned int)*((unsigned __int16 *)&opstat + 5) + 1);
printf("Tag ID: %d\n", *((unsigned int *)&opstat + 3));
v0 = (const char *)showsn((unsigned int)((_DWORD)&opstat + 16), src);
printf("Serial Number: %s\n", v0);
v1 = ((double)*((int *)&opstat + 7) + now() - (double)*((int *)&opstat + 6)) / 86400.0;
v2 = (const char *)jul_string(*((unsigned int *)&opstat + 6), dest);
printf("Time of Last Reboot: %s/%4.2fd\n", v2, v1);
v3 = *((unsigned int *)&opstat + 109);
if ( (_DWORD)v3 )
{
v4 = (const char *)jul_string(v3, dest);
printf("Last Data Received: %s\n", v4);
}
else
{
puts("No Data Recieved");
}
v5 = *((unsigned __int16 *)&opstat + 222);
v6 = (const char *)showdot(*((unsigned int *)&opstat + 110), src);
printf("Current Address: %s:%d\n", v6, v5);
v7 = *((_WORD *)&opstat + 124);
if ( v7 <= 1u )
{
s[0] = 0;
}
else if ( v7 <= 0x3Bu )
{
sprintf(s, " %dmin=", v7);
}
else
{
strcpy(s, " Hour=");
}
v8 = *((_WORD *)&opstat + 125);
if ( v8 <= 1u )
{
v37[0] = 0;
}
else if ( v8 <= 0x17u )
{
sprintf(v37, " %dhr=", v8);
}
else
{
strcpy(v37, " Day=");
}
v9 = (unsigned int)((_DWORD)&opstat + 32);
for ( i = 0; i != 18; ++i )
{
lib_acc_types(i, dest);
v12 = 0;
v13 = 3LL * i;
while ( 1 )
{
if ( v12 == 1 )
{
strcat(dest, s);
if ( *((_WORD *)&opstat + 124) <= 1u || !s[0] )
goto LABEL_31;
LABEL_35:
v11 = *((_DWORD *)&opstat + v12 + v13 + 8);
if ( v11 == -1 )
goto LABEL_30;
goto LABEL_36;
}
if ( v12 == 2 )
break;
if ( v12 )
goto LABEL_35;
if ( *(_DWORD *)v9 != -1 )
strcat(dest, " minute=");
v11 = *((_DWORD *)&opstat + v13 + 8);
if ( v11 == -1 )
goto LABEL_31;
LABEL_36:
switch ( i )
{
case 0xAu:
goto LABEL_29;
case 0xBu:
v16 = (double)v11 * 0.01;
sprintf(src, "%4.2f", v16);
strcat(dest, src);
break;
case 7u:
LABEL_29:
v14 = (double)v11 * 0.1;
sprintf(src, "%3.1f%", v14);
strcat(dest, src);
break;
default:
v15 = scale_value(v11, src);
strcat(dest, v15);
break;
}
LABEL_30:
if ( v12 + 1 > 2 )
goto LABEL_25;
LABEL_31:
++v12;
}
strcat(dest, v37);
if ( *((_WORD *)&opstat + 125) > 1u )
{
if ( v37[0] )
{
v11 = *((_DWORD *)&opstat + v13 + 10);
if ( v11 != -1 )
goto LABEL_36;
}
}
LABEL_25:
if ( dest[0] )
puts(dest);
v9 += 12LL;
}
v17 = *((_DWORD *)&opstat + 66);
if ( v17 < 0 )
{
v20 = format_time((int)abs32(v17), src);
printf("Time Disonnected: %s\n", v20);
}
else
{
v18 = format_time((int)abs32(v17), src);
printf("Time Connected: %s\n", v18);
}
if ( current_state == 10 )
{
v21 = *((_DWORD *)&opstat + 64);
if ( v21 != -66666666 )
{
v24 = format_time(v21, src);
printf("Data Latency: %s\n", v24);
}
v22 = scale_value(*((unsigned int *)&opstat + 67), src);
printf("Total Data Gaps: %s\n", v22);
dest[0] = 0;
for ( j = 0LL; j != 6; ++j )
{
if ( (*((_QWORD *)&opstat + 42) & (1 << j)) != 0LL )
{
sprintf(src, "%d", j + 1);
strcat(dest, src);
}
}
if ( dest[0] )
printf("Digitzer Cal. Error on %s\n", dest);
result = *((_DWORD *)&opstat + 65);
if ( result != -66666666 )
goto LABEL_59;
}
else
{
result = *((_DWORD *)&opstat + 65);
if ( result == -66666666 )
goto LABEL_45;
LABEL_59:
v25 = format_time(result, src);
result = printf("Status Latency: %s\n", v25);
}
if ( current_state == 10 )
{
v26 = *((float *)&opstat + 88);
if ( v26 > 5.0 )
{
printf("Input Supply Voltage: %4.2f\n", v26);
v27 = lib_round((float)(1000.0 * *((float *)&opstat + 89)));
printf("Supply Current: %dma\n", v27);
result = printf("Temperature: %d\n", *((_QWORD *)&opstat + 43));
}
}
LABEL_45:
if ( *((_DWORD *)&opstat + 90) != -1 )
{
v28 = (const char *)lib_gps_state(*((unsigned int *)&opstat + 91), src);
printf("GPS State: %s\n", v28);
v29 = (const char *)lib_gps_fix(*((unsigned int *)&opstat + 92), src);
printf("GPS Fix: %s\n", v29);
v30 = format_time(*((int *)&opstat + 90), src);
printf("GPS Age: %s\n", v30);
v31 = (const char *)lib_pll_state(*((unsigned int *)&opstat + 93), src);
printf("PLL State: %s\n", v31);
v32 = *((double *)&opstat + 47);
printf("Latitude: %8.6f\n", v32);
v33 = *((double *)&opstat + 48);
printf("Longitude: %8.6f\n", v33);
v34 = *((double *)&opstat + 49);
result = printf("Elevation: %3.1f\n", v34);
}
if ( current_state == 10 )
{
strcpy(dest, "Mass Positions:");
for ( k = 0LL; k != 6; ++k )
{
sprintf(src, " %d", *((_QWORD *)&opstat + k + 36));
strcat(dest, src);
}
puts(dest);
v36 = lib_round(*((float *)&opstat + 68));
printf("Packet Buffer: %d%%\n", v36);
printf("Clock Quality: %d%%\n", *((unsigned __int16 *)&opstat + 138));
result = printf("Clock Drift: %dusec\n", *((int *)&opstat + 70));
}
return result;
}

canonical_decompiled_code:
int func3()
{
const char *v0; // rax
float v1; // xmm0_4
const char *v2; // rax
__int64 v3; // rdi
const char *v4; // rax
__int64 v5; // rbx
const char *v6; // rax
unsigned __int16 v7; // ax
unsigned __int16 v8; // ax
unsigned __int64 v9; // r13
unsigned int i; // ebp
int v11; // edi
unsigned int v12; // ebx
__int64 v13; // r12
float v14; // xmm0_4
char *v15; // rax
float v16; // xmm0_4
int v17; // eax
char *v18; // rax
int result; // eax
char *v20; // rax
int v21; // eax
char *v22; // rax
__int64 j; // rbx
char *v24; // rax
char *v25; // rax
float v26; // xmm0_4
int v27; // eax
const char *v28; // rax
const char *v29; // rax
char *v30; // rax
const char *v31; // rax
float v32; // xmm0_4
float v33; // xmm0_4
float v34; // xmm0_4
__int64 k; // rbx
int v36; // eax
char v37[16]; // [rsp+0h] [rbp-258h] BYREF
char s[16]; // [rsp+10h] [rbp-248h] BYREF
char src[256]; // [rsp+20h] [rbp-238h] BYREF
char dest[268]; // [rsp+120h] [rbp-138h] BYREF
char v41[44]; // [rsp+22Ch] [rbp-2Ch] BYREF

lib_get_state(context, v41, &opstat);
putchar(10);
printf("Station Name: %s\n", (const char *)&opstat);
printf("Data Port: %d\n", (unsigned int)*((unsigned __int16 *)&opstat + 5) + 1);
printf("Tag ID: %d\n", *((unsigned int *)&opstat + 3));
v0 = (const char *)showsn((unsigned int)((_DWORD)&opstat + 16), src);
printf("Serial Number: %s\n", v0);
v1 = ((double)*((int *)&opstat + 7) + now() - (double)*((int *)&opstat + 6)) / 86400.0;
v2 = (const char *)jul_string(*((unsigned int *)&opstat + 6), dest);
printf("Time of Last Reboot: %s/%4.2fd\n", v2, v1);
v3 = *((unsigned int *)&opstat + 109);
if ( (_DWORD)v3 )
{
v4 = (const char *)jul_string(v3, dest);
printf("Last Data Received: %s\n", v4);
}
else
{
puts("No Data Recieved");
}
v5 = *((unsigned __int16 *)&opstat + 222);
v6 = (const char *)showdot(*((unsigned int *)&opstat + 110), src);
printf("Current Address: %s:%d\n", v6, v5);
v7 = *((_WORD *)&opstat + 124);
if ( v7 <= 1u )
{
s[0] = 0;
}
else if ( v7 <= 0x3Bu )
{
sprintf(s, " %dmin=", v7);
}
else
{
strcpy(s, " Hour=");
}
v8 = *((_WORD *)&opstat + 125);
if ( v8 <= 1u )
{
v37[0] = 0;
}
else if ( v8 <= 0x17u )
{
sprintf(v37, " %dhr=", v8);
}
else
{
strcpy(v37, " Day=");
}
v9 = (unsigned int)((_DWORD)&opstat + 32);
for ( i = 0; i != 18; ++i )
{
lib_acc_types(i, dest);
v12 = 0;
v13 = 3LL * i;
while ( 1 )
{
if ( v12 == 1 )
{
strcat(dest, s);
if ( *((_WORD *)&opstat + 124) <= 1u || !s[0] )
goto LABEL_31;
LABEL_35:
v11 = *((_DWORD *)&opstat + v12 + v13 + 8);
if ( v11 == -1 )
goto LABEL_30;
goto LABEL_36;
}
if ( v12 == 2 )
break;
if ( v12 )
goto LABEL_35;
if ( *(_DWORD *)v9 != -1 )
strcat(dest, " minute=");
v11 = *((_DWORD *)&opstat + v13 + 8);
if ( v11 == -1 )
goto LABEL_31;
LABEL_36:
switch ( i )
{
case 0xAu:
goto LABEL_29;
case 0xBu:
v16 = (double)v11 * 0.01;
sprintf(src, "%4.2f", v16);
strcat(dest, src);
break;
case 7u:
LABEL_29:
v14 = (double)v11 * 0.1;
sprintf(src, "%3.1f%", v14);
strcat(dest, src);
break;
default:
v15 = func0(v11, src);
strcat(dest, v15);
break;
}
LABEL_30:
if ( v12 + 1 > 2 )
goto LABEL_25;
LABEL_31:
++v12;
}
strcat(dest, v37);
if ( *((_WORD *)&opstat + 125) > 1u )
{
if ( v37[0] )
{
v11 = *((_DWORD *)&opstat + v13 + 10);
if ( v11 != -1 )
goto LABEL_36;
}
}
LABEL_25:
if ( dest[0] )
puts(dest);
v9 += 12LL;
}
v17 = *((_DWORD *)&opstat + 66);
if ( v17 < 0 )
{
v20 = func2((int)abs32(v17), src);
printf("Time Disonnected: %s\n", v20);
}
else
{
v18 = func2((int)abs32(v17), src);
printf("Time Connected: %s\n", v18);
}
if ( current_state == 10 )
{
v21 = *((_DWORD *)&opstat + 64);
if ( v21 != -66666666 )
{
v24 = func2(v21, src);
printf("Data Latency: %s\n", v24);
}
v22 = func0(*((unsigned int *)&opstat + 67), src);
printf("Total Data Gaps: %s\n", v22);
dest[0] = 0;
for ( j = 0LL; j != 6; ++j )
{
if ( (*((_QWORD *)&opstat + 42) & (1 << j)) != 0LL )
{
sprintf(src, "%d", j + 1);
strcat(dest, src);
}
}
if ( dest[0] )
printf("Digitzer Cal. Error on %s\n", dest);
result = *((_DWORD *)&opstat + 65);
if ( result != -66666666 )
goto LABEL_59;
}
else
{
result = *((_DWORD *)&opstat + 65);
if ( result == -66666666 )
goto LABEL_45;
LABEL_59:
v25 = func2(result, src);
result = printf("Status Latency: %s\n", v25);
}
if ( current_state == 10 )
{
v26 = *((float *)&opstat + 88);
if ( v26 > 5.0 )
{
printf("Input Supply Voltage: %4.2f\n", v26);
v27 = lib_round((float)(1000.0 * *((float *)&opstat + 89)));
printf("Supply Current: %dma\n", v27);
result = printf("Temperature: %d\n", *((_QWORD *)&opstat + 43));
}
}
LABEL_45:
if ( *((_DWORD *)&opstat + 90) != -1 )
{
v28 = (const char *)lib_gps_state(*((unsigned int *)&opstat + 91), src);
printf("GPS State: %s\n", v28);
v29 = (const char *)lib_gps_fix(*((unsigned int *)&opstat + 92), src);
printf("GPS Fix: %s\n", v29);
v30 = func2(*((int *)&opstat + 90), src);
printf("GPS Age: %s\n", v30);
v31 = (const char *)lib_pll_state(*((unsigned int *)&opstat + 93), src);
printf("PLL State: %s\n", v31);
v32 = *((double *)&opstat + 47);
printf("Latitude: %8.6f\n", v32);
v33 = *((double *)&opstat + 48);
printf("Longitude: %8.6f\n", v33);
v34 = *((double *)&opstat + 49);
result = printf("Elevation: %3.1f\n", v34);
}
if ( current_state == 10 )
{
strcpy(dest, "Mass Positions:");
for ( k = 0LL; k != 6; ++k )
{
sprintf(src, " %d", *((_QWORD *)&opstat + k + 36));
strcat(dest, src);
}
puts(dest);
v36 = lib_round(*((float *)&opstat + 68));
printf("Packet Buffer: %d%%\n", v36);
printf("Clock Quality: %d%%\n", *((unsigned __int16 *)&opstat + 138));
result = printf("Clock Drift: %dusec\n", *((int *)&opstat + 70));
}
return result;
}

original_code:
void show_opstat (void)
{
  enum tliberr err ;
  enum tacctype acctype ;
  enum taccdur accdur ;
  string s, s1 ;
  double toff ;
  float r ;
  int64_t pwc, i, v ;
  string15 shour, sday ;
  lib_get_state (context, &(err), &(opstat)) ;
  printf ("\n") ;
  printf ("Station Name: %s\n", opstat.station_name) ;
  printf ("Data Port: %d\n", opstat.station_port + 1) ;
  printf ("Tag ID: %d\n", opstat.station_tag) ;
  printf ("Serial Number: %s\n", showsn(opstat.station_serial, &(s1))) ;
  toff = now () + opstat.timezone_offset - opstat.station_reboot ;
  r = toff / 86400.0 ;
  printf ("Time of Last Reboot: %s/%4.2fd\n", jul_string (opstat.station_reboot, &(s)), r) ;
  if (opstat.last_data_time)

      printf("Last Data Received: %s\n", jul_string (opstat.last_data_time, &(s))) ;
    else
      printf ("No Data Recieved\n") ;
  printf ("Current Address: %s:%d\n", showdot(opstat.current_ip, &(s1)), (int64_t)opstat.current_port) ;
  if (opstat.minutes_of_stats < 2)

      shour[0] = 0 ;
  else if (opstat.minutes_of_stats >= 60)

      strcpy(shour, " Hour=") ;
    else
      sprintf(shour, " %dmin=", opstat.minutes_of_stats) ;
  if (opstat.hours_of_stats < 2)

      sday[0] = 0 ;
  else if (opstat.hours_of_stats >= 24)

      strcpy(sday, " Day=") ;
    else
      sprintf(sday, " %dhr=", opstat.hours_of_stats) ;
  for (acctype = AC_GAPS ; acctype <= AC_IOERR ; acctype++)
    {
      lib_acc_types (acctype, &(s)) ;
      for (accdur = AD_MINUTE ; accdur <= AD_DAY ; accdur++)
        {
          switch (accdur) {
            case AD_MINUTE :
              if (opstat.accstats[acctype][accdur] != -1)

                  strcat(s, " minute=") ;
              break ;
            case AD_HOUR :
              strcat(s, shour) ;
              break ;
            case AD_DAY :
              strcat(s, sday) ;
              break ;
          }
          if ((accdur == AD_HOUR) && ((opstat.minutes_of_stats < 2) || (shour[0] == 0)) ||
             (accdur == AD_DAY) && ((opstat.hours_of_stats < 2) || (sday[0] == 0)) ||
             (opstat.accstats[acctype][accdur] == -1))

              ;
            else
              switch (acctype) {
                case AC_DUTY :
                case AC_COMEFF :
                  r = opstat.accstats[acctype][accdur] * 0.1 ;
                  sprintf(s1, "%3.1f%", r) ;
                  strcat(s, s1) ;
                  break ;
                case AC_THROUGH :
                  r = opstat.accstats[acctype][accdur] * 0.01 ;
                  sprintf(s1, "%4.2f", r) ;
                  strcat(s, s1) ;
                  break ;
                default :
                  strcat(s, scale_value(opstat.accstats[acctype][accdur], &(s1))) ;
                  break ;
              }
        }
      if (s[0])

          printf ("%s\n", s) ;
    }
  if (opstat.runtime >= 0)

      printf("Time Connected: %s\n", format_time(abs(opstat.runtime), &(s1))) ;
    else
      printf("Time Disonnected: %s\n", format_time(abs(opstat.runtime), &(s1))) ;
  if (current_state == LIBSTATE_RUN)

      {
        if (opstat.data_latency != -66666666)

            printf ("Data Latency: %s\n", format_time(opstat.data_latency, &(s1))) ;
        printf ("Total Data Gaps: %s\n", scale_value(opstat.totalgaps, &(s1))) ;
        s[0] = 0 ;
        for (i = 0 ; i <= 6 - 1 ; i++)
          if (opstat.calibration_errors & (1 << i))

              {
                sprintf(s1, "%d", i + 1) ;
                strcat(s, s1) ;
              }
        if (s[0])

            printf ("Digitzer Cal. Error on %s\n", s) ;
      }
   if (opstat.status_latency != -66666666)

      printf ("Status Latency: %s\n", format_time(opstat.status_latency, &(s1))) ;
  if ((current_state == LIBSTATE_RUN) && (opstat.pwr_volt > 5))

      {
        r = opstat.pwr_volt ;
        printf ("Input Supply Voltage: %4.2f\n", r) ;
        pwc = lib_round(opstat.pwr_cur * 1000) ;
        printf ("Supply Current: %dma\n", pwc) ;
        printf ("Temperature: %d\n", opstat.sys_temp) ;
      }
  if (opstat.gps_age != -1)

      {
        printf ("GPS State: %s\n", lib_gps_state (opstat.gps_stat, &(s1))) ;
        printf ("GPS Fix: %s\n", lib_gps_fix (opstat.gps_fix, &(s1))) ;
        printf ("GPS Age: %s\n", format_time(opstat.gps_age, &(s1))) ;
        printf ("PLL State: %s\n", lib_pll_state(opstat.pll_stat, &(s1))) ;
        r = opstat.gps_lat ;
        printf ("Latitude: %8.6f\n", r) ;
        r = opstat.gps_long ;
        printf ("Longitude: %8.6f\n", r) ;
        r = opstat.gps_elev ;
        printf ("Elevation: %3.1f\n", r) ;
      }
  if (current_state == LIBSTATE_RUN)

      {
        strcpy(s, "Mass Positions:") ;
        for (i = 0 ; i <= 5 ; i++)
          {
            sprintf(s1, " %d", opstat.mass_pos[i]) ;
            strcat(s, s1) ;
          }
        printf ("%s\n", s) ;
        v = lib_round(opstat.pkt_full) ;
        printf ("Packet Buffer: %d%%\n", v) ;
        printf ("Clock Quality: %d%%\n", opstat.clock_qual) ;
        v = opstat.clock_drift ;
        printf ("Clock Drift: %dusec\n", v) ;
      }
}

canonical_original_code:
void show_opstat()
{
  enum tliberr err;
  enum tacctype acctype;
  enum taccdur accdur;
  char s[256]; char s1[256];
  double toff;
  float r;
  long pwc; long i; long v;
  char shour[16]; char sday[16];
  lib_get_state (context, &(err), &(opstat)) ;
  printf ("\n") ;
  printf ("Station Name: %s\n", opstat.station_name) ;
  printf ("Data Port: %d\n", opstat.station_port + 1) ;
  printf ("Tag ID: %d\n", opstat.station_tag) ;
  printf ("Serial Number: %s\n", showsn(opstat.station_serial, &(s1))) ;
  toff = now () + opstat.timezone_offset - opstat.station_reboot ;
  r = toff / 86400.0 ;
  printf ("Time of Last Reboot: %s/%4.2fd\n", jul_string (opstat.station_reboot, &(s)), r) ;
  if (opstat.last_data_time)

      printf("Last Data Received: %s\n", jul_string (opstat.last_data_time, &(s))) ;
    else
      printf ("No Data Recieved\n") ;
  printf ("Current Address: %s:%d\n", showdot(opstat.current_ip, &(s1)), (long)opstat.current_port) ;
  if (opstat.minutes_of_stats < 2)

      shour[0] = 0 ;
  else if (opstat.minutes_of_stats >= 60)

      strcpy(shour, " Hour=") ;
    else
      sprintf(shour, " %dmin=", opstat.minutes_of_stats) ;
  if (opstat.hours_of_stats < 2)

      sday[0] = 0 ;
  else if (opstat.hours_of_stats >= 24)

      strcpy(sday, " Day=") ;
    else
      sprintf(sday, " %dhr=", opstat.hours_of_stats) ;
  for (acctype = AC_GAPS ; acctype <= AC_IOERR ; acctype++)
    {
      lib_acc_types (acctype, &(s)) ;
      for (accdur = AD_MINUTE ; accdur <= AD_DAY ; accdur++)
        {
          switch (accdur) {
            case AD_MINUTE :
              if (opstat.accstats[acctype][accdur] != -1)

                  strcat(s, " minute=") ;
              break ;
            case AD_HOUR :
              strcat(s, shour) ;
              break ;
            case AD_DAY :
              strcat(s, sday) ;
              break ;
          }
          if ((accdur == AD_HOUR) && ((opstat.minutes_of_stats < 2) || (shour[0] == 0)) ||
             (accdur == AD_DAY) && ((opstat.hours_of_stats < 2) || (sday[0] == 0)) ||
             (opstat.accstats[acctype][accdur] == -1))

              ;
            else
              switch (acctype) {
                case AC_DUTY :
                case AC_COMEFF :
                  r = opstat.accstats[acctype][accdur] * 0.1 ;
                  sprintf(s1, "%3.1f%", r) ;
                  strcat(s, s1) ;
                  break ;
                case AC_THROUGH :
                  r = opstat.accstats[acctype][accdur] * 0.01 ;
                  sprintf(s1, "%4.2f", r) ;
                  strcat(s, s1) ;
                  break ;
                default :
                  strcat(s, scale_value(opstat.accstats[acctype][accdur], &(s1))) ;
                  break ;
              }
        }
      if (s[0])

          printf ("%s\n", s) ;
    }
  if (opstat.runtime >= 0)

      printf("Time Connected: %s\n", format_time(abs(opstat.runtime), &(s1))) ;
    else
      printf("Time Disonnected: %s\n", format_time(abs(opstat.runtime), &(s1))) ;
  if (current_state == LIBSTATE_RUN)

      {
        if (opstat.data_latency != -66666666)

            printf ("Data Latency: %s\n", format_time(opstat.data_latency, &(s1))) ;
        printf ("Total Data Gaps: %s\n", scale_value(opstat.totalgaps, &(s1))) ;
        s[0] = 0 ;
        for (i = 0 ; i <= 6 - 1 ; i++)
          if (opstat.calibration_errors & (1 << i))

              {
                sprintf(s1, "%d", i + 1) ;
                strcat(s, s1) ;
              }
        if (s[0])

            printf ("Digitzer Cal. Error on %s\n", s) ;
      }
   if (opstat.status_latency != -66666666)

      printf ("Status Latency: %s\n", format_time(opstat.status_latency, &(s1))) ;
  if ((current_state == LIBSTATE_RUN) && (opstat.pwr_volt > 5))

      {
        r = opstat.pwr_volt ;
        printf ("Input Supply Voltage: %4.2f\n", r) ;
        pwc = lib_round(opstat.pwr_cur * 1000) ;
        printf ("Supply Current: %dma\n", pwc) ;
        printf ("Temperature: %d\n", opstat.sys_temp) ;
      }
  if (opstat.gps_age != -1)

      {
        printf ("GPS State: %s\n", lib_gps_state (opstat.gps_stat, &(s1))) ;
        printf ("GPS Fix: %s\n", lib_gps_fix (opstat.gps_fix, &(s1))) ;
        printf ("GPS Age: %s\n", format_time(opstat.gps_age, &(s1))) ;
        printf ("PLL State: %s\n", lib_pll_state(opstat.pll_stat, &(s1))) ;
        r = opstat.gps_lat ;
        printf ("Latitude: %8.6f\n", r) ;
        r = opstat.gps_long ;
        printf ("Longitude: %8.6f\n", r) ;
        r = opstat.gps_elev ;
        printf ("Elevation: %3.1f\n", r) ;
      }
  if (current_state == LIBSTATE_RUN)

      {
        strcpy(s, "Mass Positions:") ;
        for (i = 0 ; i <= 5 ; i++)
          {
            sprintf(s1, " %d", opstat.mass_pos[i]) ;
            strcat(s, s1) ;
          }
        printf ("%s\n", s) ;
        v = lib_round(opstat.pkt_full) ;
        printf ("Packet Buffer: %d%%\n", v) ;
        printf ("Clock Quality: %d%%\n", opstat.clock_qual) ;
        v = opstat.clock_drift ;
        printf ("Clock Drift: %dusec\n", v) ;
      }
}

variable_types: enum tliberr { LIBERR_NOERR=0, LIBERR_PERM=1, LIBERR_TMSERV=2, LIBERR_NOTR=3, LIBERR_INVREG=4, LIBERR_PAR=5, LIBERR_SNV=6, LIBERR_CTRL=7, LIBERR_SPEC=8, LIBERR_MEM=9, LIBERR_CIP=10, LIBERR_DNA=11, LIBERR_DB9=12, LIBERR_MEMEW=13, LIBERR_THREADERR=14, LIBERR_BADDIR=15, LIBERR_REGTO=16, LIBERR_STATTO=17, LIBERR_DATATO=18, LIBERR_NOSTAT=19, LIBERR_INVSTAT=20, LIBERR_CFGWAIT=21, LIBERR_INVCFG=22, LIBERR_TOKENS_CHANGE=23, LIBERR_INVAL_TOKENS=24, LIBERR_BUFSHUT=25, LIBERR_CONNSHUT=26, LIBERR_CLOSED=27, LIBERR_NETFAIL=28, LIBERR_TUNBUSY=29, LIBERR_INVCTX=30 }

variable_types: enum tacctype { AC_GAPS=0, AC_BOOTS=1, AC_READ=2, AC_WRITE=3, AC_COMATP=4, AC_COMSUC=5, AC_PACKETS=6, AC_COMEFF=7, AC_POCS=8, AC_NEWIP=9, AC_DUTY=10, AC_THROUGH=11, AC_MISSING=12, AC_FILL=13, AC_CMDTO=14, AC_SEQERR=15, AC_CHECK=16, AC_IOERR=17 }

variable_types: enum taccdur { AD_MINUTE=0, AD_HOUR=1, AD_DAY=2 }

variable_types: toff double

variable_types: pwc long

variable_types: i long

variable_types:
shour
char [16]

variable_types:
sday
char [16]


repo: joeledwards/asl-station-processor

decompiled_code:
int show_modules()
{
__int64 v0; // rbp
__int64 v1; // rbx
__int64 v2; // rbp
__int64 v3; // rbp
int result; // eax
__int64 v5; // rbx
__int64 v6; // rbp

v0 = get_modules();
puts("Seneca Modules and Versions:");
v1 = v0;
v2 = v0 + 744;
do
{
if ( !*(_BYTE *)v1 )
break;
printf("%13s %d\n", (const char *)v1, *(_QWORD *)(v1 + 16));
v1 += 24LL;
}
while ( v1 != v2 );
v3 = lib_get_modules();
result = puts("Lib330 Modules and Versions:");
v5 = v3;
v6 = v3 + 744;
do
{
if ( !*(_BYTE *)v5 )
break;
result = printf("%13s %d\n", (const char *)v5, *(_QWORD *)(v5 + 16));
v5 += 24LL;
}
while ( v5 != v6 );
return result;
}

canonical_decompiled_code:
int func4()
{
__int64 v0; // rbp
__int64 v1; // rbx
__int64 v2; // rbp
__int64 v3; // rbp
int result; // eax
__int64 v5; // rbx
__int64 v6; // rbp

v0 = get_modules();
puts("Seneca Modules and Versions:");
v1 = v0;
v2 = v0 + 744;
do
{
if ( !*(_BYTE *)v1 )
break;
printf("%13s %d\n", (const char *)v1, *(_QWORD *)(v1 + 16));
v1 += 24LL;
}
while ( v1 != v2 );
v3 = lib_get_modules();
result = puts("Lib330 Modules and Versions:");
v5 = v3;
v6 = v3 + 744;
do
{
if ( !*(_BYTE *)v5 )
break;
result = printf("%13s %d\n", (const char *)v5, *(_QWORD *)(v5 + 16));
v5 += 24LL;
}
while ( v5 != v6 );
return result;
}

original_code:
void show_modules (void)
{
  int64_t i ;
  pmodules pm ;
  tmodule *pone ;
  pm = get_modules () ;
  printf ("Seneca Modules and Versions:\n") ;
  for (i = 0 ; i <= (31 - 1) ; i++)
    {
      pone = &((*pm)[i]) ;
      if ((*pm)[i].name[0])

          printf ("%13s %d\n", &(pone->name), pone->ver) ;
        else
          break ;
    }
  pm = lib_get_modules () ;
  printf ("Lib330 Modules and Versions:\n") ;
  for (i = 0 ; i <= (31 - 1) ; i++)
    {
      pone = &((*pm)[i]) ;
      if ((*pm)[i].name[0])

          printf ("%13s %d\n", &(pone->name), pone->ver) ;
        else
          break ;
    }
}

canonical_original_code:
void show_modules()
{
  long i;
  struct tmodule (*pm)[31];
  struct tmodule *pone;
  pm = get_modules () ;
  printf ("Seneca Modules and Versions:\n") ;
  for (i = 0 ; i <= (31 - 1) ; i++)
    {
      pone = &((*pm)[i]) ;
      if ((*pm)[i].name[0])

          printf ("%13s %d\n", &(pone->name), pone->ver) ;
        else
          break ;
    }
  pm = lib_get_modules () ;
  printf ("Lib330 Modules and Versions:\n") ;
  for (i = 0 ; i <= (31 - 1) ; i++)
    {
      pone = &((*pm)[i]) ;
      if ((*pm)[i].name[0])

          printf ("%13s %d\n", &(pone->name), pone->ver) ;
        else
          break ;
    }
}

variable_types: struct tmodule { char name[16]; long ver; } (*)[31]

variable_types: struct tmodule { char name[16]; long ver; } *

return_type: void


decompiled_code:
// attributes: thunk
__int64 __fastcall ST_Julian2(__int64 a1, __int64 a2, __int64 a3)
{
return julday2(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
return func1(a1, a2, a3);
}

original_code:
long ST_Julian2(long year, long mon, long day) {
  return(_julday2(year, mon, day));
}

variable_types: year long

variable_types: mon long

variable_types: day long

return_type: long


decompiled_code:
__int64 __fastcall ST_CnvJulToSTD2(int a1)
{
unsigned __int16 v2; // [rsp+1Eh] [rbp-1Ah] BYREF
__int16 v3; // [rsp+20h] [rbp-18h] BYREF
__int16 v4; // [rsp+22h] [rbp-16h] BYREF
__int16 v5; // [rsp+24h] [rbp-14h] BYREF
__int64 v6; // [rsp+26h] [rbp-12h]

ST_CnvJulToCal2(a1, &v5, &v4, &v3, &v2);
LOWORD(v6) = v5;
*(_DWORD *)((char *)&v6 + 2) = v2;
BYTE6(v6) = 0;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
unsigned __int16 v2; // [rsp+1Eh] [rbp-1Ah] BYREF
__int16 v3; // [rsp+20h] [rbp-18h] BYREF
__int16 v4; // [rsp+22h] [rbp-16h] BYREF
__int16 v5; // [rsp+24h] [rbp-14h] BYREF
__int64 v6; // [rsp+26h] [rbp-12h]

ST_CnvJulToCal2(a1, &v5, &v4, &v3, &v2);
LOWORD(v6) = v5;
*(_DWORD *)((char *)&v6 + 2) = v2;
BYTE6(v6) = 0;
return v6;
}

original_code:
STDTIME2 ST_CnvJulToSTD2(JULIAN injul) {
  STDTIME2 rettim = {0, 0, 0, 0, 0, 0};
  WORD outyr, outmon, outday, outjday;
  ST_CnvJulToCal2(injul, &outyr, &outmon, &outday, &outjday);
  rettim.year = outyr;
  rettim.day = outjday;
  return(rettim);
}

canonical_original_code:
struct __sjtim2 ST_CnvJulToSTD2(long injul) {
  struct __sjtim2 rettim = {0, 0, 0, 0, 0, 0};
  short outyr; short outmon; short outday; short outjday;
  ST_CnvJulToCal2(injul, &outyr, &outmon, &outday, &outjday);
  rettim.year = outyr;
  rettim.day = outjday;
  return(rettim);
}

variable_types: injul long

variable_types: struct __sjtim2 { short year; short day; char hour; char minute; char second; short tenth_msec; }

variable_types: outyr short

variable_types: outmon short

variable_types: outday short

variable_types: outjday short


decompiled_code:
__int64 __fastcall ms_gswap4a(_DWORD *a1)
{
__int64 result; // rax

result = (*a1 >> 8) & 0xFF00 | (*a1 << 24) | HIBYTE(*a1) | (*a1 << 8) & 0xFF0000u;
*a1 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_DWORD *a1)
{
__int64 result; // rax

result = (*a1 >> 8) & 0xFF00 | (*a1 << 24) | HIBYTE(*a1) | (*a1 << 8) & 0xFF0000u;
*a1 = result;
return result;
}

canonical_original_code:
void ms_gswap4a(void *data4)
{
  unsigned int *data = data4;
  *data=(((*data>>24)&0xff) | ((*data&0xff)<<24) |
  ((*data>>8)&0xff00) | ((*data&0xff00)<<8));
}

variable_types:
data
unsigned int *


decompiled_code:
__int64 __fastcall ms_gswap8a(int *a1)
{
int v1; // ecx
int v2; // eax
__int64 result; // rax

v1 = ((unsigned int)*a1 >> 8) & 0xFF00 | (*a1 << 24) | HIBYTE(*a1);
v2 = (*a1 << 8) & 0xFF0000;
*a1 = ((unsigned int)a1[1] >> 8) & 0xFF00 | (a1[1] << 24) | HIBYTE(a1[1]) | (a1[1] << 8) & 0xFF0000;
result = v1 | (unsigned int)v2;
a1[1] = result;
return result;
}


decompiled_code:
__int64 __fastcall prioqueue_init(__int64 a1, int a2, __int64 a3, __int64 a4)
{
if ( !a1 )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 72) = a2;
*(_DWORD *)(a1 + 76) = 0;
*(_QWORD *)(a1 + 80) = a3;
*(_QWORD *)(a1 + 88) = a4;
return avltree_init(a1, queue_element_compare, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, __int64 a3, __int64 a4)
{
if ( !a1 )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 72) = a2;
*(_DWORD *)(a1 + 76) = 0;
*(_QWORD *)(a1 + 80) = a3;
*(_QWORD *)(a1 + 88) = a4;
return avltree_init(a1, queue_element_compare, 0LL);
}

original_code:
int prioqueue_init(queue_t *queue, int capacity,
                   prioqueue_duplicate duplicate,
                   prioqueue_deallocate deallocate)
{
    int result = -1;
    if (queue) {
        queue->capacity = capacity;
        queue->size = 0;
        queue->callback_duplicate = duplicate;
        queue->callback_deallocate = deallocate;
        result = avltree_init(&queue->tree, _queue_element_compare, 0);
    }
    return result;
}

canonical_original_code:
int prioqueue_init(struct QUEUE *queue, int capacity, void (*duplicate)(void *item, void **duplicate), void (*deallocate)(void *item))
{
    int result = -1;
    if (queue) {
        queue->capacity = capacity;
        queue->size = 0;
        queue->callback_duplicate = duplicate;
        queue->callback_deallocate = deallocate;
        result = avltree_init(&queue->tree, _queue_element_compare, 0);
    }
    return result;
}

variable_types: struct QUEUE { struct avltree { struct avltree_node { struct avltree_node *left; unsigned long parent; } *root; int (*cmp_fn)(struct avltree_node *, struct avltree_node *); int height; struct avltree_node *first; unsigned long reserved[4]; } tree; int capacity; int size; void (*callback_duplicate)(void *item, void **duplicate); void (*callback_deallocate)(void *item); } *

variable_types: capacity int

variable_types:
duplicate
void (*)(void *item, void **duplicate)

variable_types:
deallocate
void (*)(void *item)

variable_types: result int

return_type: int

user_defined_types: struct QUEUE { struct avltree tree; int capacity; int size; void (*callback_duplicate)(void *item, void **duplicate); void (*callback_deallocate)(void *item); }

user_defined_types: struct avltree { struct avltree_node *root; int (*cmp_fn)(struct avltree_node *, struct avltree_node *); int height; struct avltree_node *first; unsigned long reserved[4]; }


decompiled_code:
__int64 __fastcall prioqueue_full(__int64 a1)
{
int v1; // edx
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = *(_DWORD *)(a1 + 72);
result = 0LL;
if ( v1 >= 0 )
result = v1 <= *(_DWORD *)(a1 + 76);
return result;
}

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

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = *(_DWORD *)(a1 + 72);
result = 0LL;
if ( v1 >= 0 )
result = v1 <= *(_DWORD *)(a1 + 76);
return result;
}

original_code:
int prioqueue_full(queue_t *queue)
{
    if (queue == ((void *)0))
        return -1;
    if ((queue->capacity > -1) && (queue->size >= queue->capacity))
        return 1;
    return 0;
}

canonical_original_code:
int prioqueue_full(struct QUEUE *queue)
{
    if (queue == ((void *)0))
        return -1;
    if ((queue->capacity > -1) && (queue->size >= queue->capacity))
        return 1;
    return 0;
}

user_defined_types: struct avltree_node { struct avltree_node *left; unsigned long parent; }


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

if ( a1 )
result = *(_DWORD *)(a1 + 76) == 0;
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *(_DWORD *)(a1 + 76) == 0;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int prioqueue_empty(queue_t *queue)
{
    if (queue == ((void *)0))
        return -1;
    return (queue->size == 0);
}

canonical_original_code:
int prioqueue_empty(struct QUEUE *queue)
{
    if (queue == ((void *)0))
        return -1;
    return (queue->size == 0);
}


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

if ( a1 )
result = *(unsigned int *)(a1 + 76);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *(unsigned int *)(a1 + 76);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int prioqueue_size(queue_t *queue)
{
    if (queue == ((void *)0))
        return -1;
    return queue->size;
}

canonical_original_code:
int prioqueue_size(struct QUEUE *queue)
{
    if (queue == ((void *)0))
        return -1;
    return queue->size;
}


decompiled_code:
__int64 __fastcall prioqueue_add(__int64 a1, __int64 a2, int a3)
{
int v3; // eax
char *v5; // r14
__int64 v6; // rbp
void (__fastcall *v7)(_QWORD); // rax
void (__fastcall *v8)(__int64, char *); // rax
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF

v3 = *(_DWORD *)(a1 + 72);
if ( !v3 )
return 0xFFFFFFFFLL;
v5 = 0LL;
if ( v3 <= 0 )
goto LABEL_18;
while ( *(_DWORD *)(a1 + 76) >= v3 )
{
v6 = avltree_first(a1);
avltree_remove(v6, a1);
--*(_DWORD *)(a1 + 76);
v5 = (char *)(v6 - 32);
v7 = *(void (__fastcall **)(_QWORD))(a1 + 88);
if ( v7 )
v7(*(_QWORD *)(v6 - 8));
*(_QWORD *)(v6 - 8) = 0LL;
v3 = *(_DWORD *)(a1 + 72);
if ( v3 <= 0 )
goto LABEL_11;
}
if ( !v5 )
{
LABEL_18:
v5 = (char *)malloc(0x38uLL);
if ( !v5 )
return 0xFFFFFFFFLL;
}
LABEL_11:
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
gettimeofday((struct timeval *)(v5 + 8), &tz);
*(_DWORD *)v5 = a3;
*((_QWORD *)v5 + 3) = a2;
v8 = *(void (__fastcall **)(__int64, char *))(a1 + 80);
if ( v8 )
v8(a2, v5 + 24);
avltree_insert(v5 + 32, a1);
++*(_DWORD *)(a1 + 76);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3)
{
int v3; // eax
char *v5; // r14
__int64 v6; // rbp
void (__fastcall *v7)(_QWORD); // rax
void (__fastcall *v8)(__int64, char *); // rax
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF

v3 = *(_DWORD *)(a1 + 72);
if ( !v3 )
return 0xFFFFFFFFLL;
v5 = 0LL;
if ( v3 <= 0 )
goto LABEL_18;
while ( *(_DWORD *)(a1 + 76) >= v3 )
{
v6 = avltree_first(a1);
avltree_remove(v6, a1);
--*(_DWORD *)(a1 + 76);
v5 = (char *)(v6 - 32);
v7 = *(void (__fastcall **)(_QWORD))(a1 + 88);
if ( v7 )
v7(*(_QWORD *)(v6 - 8));
*(_QWORD *)(v6 - 8) = 0LL;
v3 = *(_DWORD *)(a1 + 72);
if ( v3 <= 0 )
goto LABEL_11;
}
if ( !v5 )
{
LABEL_18:
v5 = (char *)malloc(0x38uLL);
if ( !v5 )
return 0xFFFFFFFFLL;
}
LABEL_11:
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
gettimeofday((struct timeval *)(v5 + 8), &tz);
*(_DWORD *)v5 = a3;
*((_QWORD *)v5 + 3) = a2;
v8 = *(void (__fastcall **)(__int64, char *))(a1 + 80);
if ( v8 )
v8(a2, v5 + 24);
avltree_insert(v5 + 32, a1);
++*(_DWORD *)(a1 + 76);
return 0LL;
}

original_code:
int prioqueue_add(queue_t *queue, void *data, int priority)
{
    struct avltree_node *node = ((void *)0);
    queue_element_t *element = ((void *)0);
    struct timezone tz_info;
    if (queue->capacity == 0) {
        return -1;
    }
    while (queue->capacity > 0 && queue->size >= queue->capacity) {
        node = avltree_first(&queue->tree);
        avltree_remove(node, &queue->tree);
        queue->size--;
        element = (queue_element_t *)({ const struct avltree_node *__mptr = (node); (queue_element_t *)( (char *)__mptr - __builtin_offsetof (queue_element_t, node) );});
        if (queue->callback_deallocate != ((void *)0)) {
            queue->callback_deallocate(element->data);
        }
        element->data = ((void *)0);
        node = ((void *)0);
    }
    if ( (element == ((void *)0)) &&
         (element = (queue_element_t *)malloc(sizeof(queue_element_t))) == ((void *)0))
    {
        return -1;
    }
    else {
        tz_info.tz_minuteswest = 0;
        tz_info.tz_dsttime = 0;
        gettimeofday(&element->time_received, &tz_info);
        element->priority = priority;
        element->data = data;
        if (queue->callback_duplicate != ((void *)0)) {
            queue->callback_duplicate(data, &element->data);
        }
        avltree_insert(&element->node, &queue->tree);
        queue->size++;
    }
    return 0;
}

canonical_original_code:
int prioqueue_add(struct QUEUE *queue, void *data, int priority)
{
    struct avltree_node *node = ((void *)0);
    struct QUEUE_ELEMENT *element = ((void *)0);
    struct timezone tz_info;
    if (queue->capacity == 0) {
        return -1;
    }
    while (queue->capacity > 0 && queue->size >= queue->capacity) {
        node = avltree_first(&queue->tree);
        avltree_remove(node, &queue->tree);
        queue->size--;
        element = (struct QUEUE_ELEMENT *)({ struct avltree_node *__mptr = (node); (struct QUEUE_ELEMENT *)( (char *)__mptr - __builtin_offsetof (queue_element_t, node) );});
        if (queue->callback_deallocate != ((void *)0)) {
            queue->callback_deallocate(element->data);
        }
        element->data = ((void *)0);
        node = ((void *)0);
    }
    if ( (element == ((void *)0)) &&
         (element = (struct QUEUE_ELEMENT *)malloc(sizeof(queue_element_t))) == ((void *)0))
    {
        return -1;
    }
    else {
        tz_info.tz_minuteswest = 0;
        tz_info.tz_dsttime = 0;
        gettimeofday(&element->time_received, &tz_info);
        element->priority = priority;
        element->data = data;
        if (queue->callback_duplicate != ((void *)0)) {
            queue->callback_duplicate(data, &element->data);
        }
        avltree_insert(&element->node, &queue->tree);
        queue->size++;
    }
    return 0;
}

variable_types:
data
void *

variable_types: priority int

variable_types: struct avltree_node { struct avltree_node *left; unsigned long parent; } *

variable_types: struct QUEUE_ELEMENT { int priority; struct timeval { long tv_sec; long tv_usec; } time_received; void *data; struct avltree_node { struct avltree_node *left; unsigned long parent; } node; } *

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

user_defined_types: struct QUEUE_ELEMENT { int priority; struct timeval time_received; void *data; struct avltree_node node; }


decompiled_code:
__int64 __fastcall prioqueue_clone(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 i; // rbx

if ( !a1 || !a2 )
return (unsigned int)-1;
*(_DWORD *)(a1 + 72) = *(_DWORD *)(a2 + 72);
*(_DWORD *)(a1 + 76) = 0;
*(_QWORD *)(a1 + 80) = *(_QWORD *)(a2 + 80);
*(_QWORD *)(a1 + 88) = *(_QWORD *)(a2 + 88);
v2 = avltree_init(a1, queue_element_compare, 0LL);
if ( !v2 )
{
for ( i = avltree_first(a2); i; i = avltree_next(i) )
prioqueue_add(a1, *(_QWORD *)(i - 8), *(_DWORD *)(i - 32));
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 i; // rbx

if ( !a1 || !a2 )
return (unsigned int)-1;
*(_DWORD *)(a1 + 72) = *(_DWORD *)(a2 + 72);
*(_DWORD *)(a1 + 76) = 0;
*(_QWORD *)(a1 + 80) = *(_QWORD *)(a2 + 80);
*(_QWORD *)(a1 + 88) = *(_QWORD *)(a2 + 88);
v2 = avltree_init(a1, queue_element_compare, 0LL);
if ( !v2 )
{
for ( i = avltree_first(a2); i; i = avltree_next(i) )
func5(a1, *(_QWORD *)(i - 8), *(_DWORD *)(i - 32));
}
return v2;
}

original_code:
int prioqueue_clone(queue_t *target, queue_t *source)
{
    int result = -1;
    struct avltree_node *node;
    queue_element_t *element = ((void *)0);
    if (target && source) {
        target->capacity = source->capacity;
        target->size = 0;
        target->callback_duplicate = source->callback_duplicate;
        target->callback_deallocate = source->callback_deallocate;
        if (!(result = avltree_init(&target->tree, _queue_element_compare, 0))) {
            node = avltree_first(&source->tree);
            while (node != ((void *)0)) {
                element = (queue_element_t *)({ const struct avltree_node *__mptr = (node); (queue_element_t *)( (char *)__mptr - __builtin_offsetof (queue_element_t, node) );});
                prioqueue_add(target, element->data, element->priority);
                node = avltree_next(node);
            }
            result = 0;
        }
    }
    return result;
}

canonical_original_code:
int prioqueue_clone(struct QUEUE *target, struct QUEUE *source)
{
    int result = -1;
    struct avltree_node *node;
    struct QUEUE_ELEMENT *element = ((void *)0);
    if (target && source) {
        target->capacity = source->capacity;
        target->size = 0;
        target->callback_duplicate = source->callback_duplicate;
        target->callback_deallocate = source->callback_deallocate;
        if (!(result = avltree_init(&target->tree, _queue_element_compare, 0))) {
            node = avltree_first(&source->tree);
            while (node != ((void *)0)) {
                element = (struct QUEUE_ELEMENT *)({ struct avltree_node *__mptr = (node); (struct QUEUE_ELEMENT *)( (char *)__mptr - __builtin_offsetof (queue_element_t, node) );});
                prioqueue_add(target, element->data, element->priority);
                node = avltree_next(node);
            }
            result = 0;
        }
    }
    return result;
}

variable_types:
source
struct QUEUE *

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


decompiled_code:
__int64 __fastcall prioqueue_peek_high(__int64 a1)
{
return fetch_queue_item(a1, 1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return func7(a1, 1, 0);
}

original_code:
void *prioqueue_peek_high(queue_t *queue)
{
    return _fetch_queue_item(queue, 1 , 0 );
}

canonical_original_code:
void *prioqueue_peek_high(struct QUEUE *queue)
{
    return _fetch_queue_item(queue, 1 , 0 );
}

return_type: void *


decompiled_code:
__int64 __fastcall prioqueue_pop_high(__int64 a1)
{
return fetch_queue_item(a1, 1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return func7(a1, 1, 1);
}

original_code:
void *prioqueue_pop_high(queue_t *queue)
{
    return _fetch_queue_item(queue, 1 , 1 );
}

canonical_original_code:
void *prioqueue_pop_high(struct QUEUE *queue)
{
    return _fetch_queue_item(queue, 1 , 1 );
}


decompiled_code:
__int64 __fastcall prioqueue_peek_low(__int64 a1)
{
return fetch_queue_item(a1, 0, 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return func7(a1, 0, 0);
}

original_code:
void *prioqueue_peek_low(queue_t *queue)
{
    return _fetch_queue_item(queue, 0 , 0 );
}

canonical_original_code:
void *prioqueue_peek_low(struct QUEUE *queue)
{
    return _fetch_queue_item(queue, 0 , 0 );
}


decompiled_code:
__int64 __fastcall prioqueue_pop_low(__int64 a1)
{
return fetch_queue_item(a1, 0, 1);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
return func7(a1, 0, 1);
}

original_code:
void *prioqueue_pop_low(queue_t *queue)
{
    return _fetch_queue_item(queue, 0 , 1 );
}

canonical_original_code:
void *prioqueue_pop_low(struct QUEUE *queue)
{
    return _fetch_queue_item(queue, 0 , 1 );
}


hash

original_code:
static unsigned long hash(const char *str)
{
    unsigned long hash = 5381;
    int c;
    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

canonical_original_code:
static unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    int c;
    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

variable_types:
str
char *

variable_types:
hash
unsigned long

variable_types: c int

return_type: unsigned long


decompiled_code:
__int64 __fastcall remove_pair(__int64 a1, __int64 a2, const char *a3)
{
__int64 result; // rax
unsigned int v5; // ebp
_QWORD *v6; // rbx
int v7; // er13
void (__fastcall *v8)(_QWORD); // rax
const char *v9; // rdi
void (__fastcall *v10)(_QWORD); // rax
int v11; // esi
void *v12; // rax
unsigned int v15; // [rsp+1Ch] [rbp-3Ch]

result = 0LL;
if ( *(_DWORD *)a2 )
{
v5 = *(_DWORD *)a2 - 1;
v6 = (_QWORD *)(*(_QWORD *)(a2 + 8) + 256LL);
v15 = 0;
v7 = 0;
do
{
v9 = (const char *)*(v6 - 32);
if ( v9 )
{
if ( !strcmp(v9, a3) )
{
v10 = *(void (__fastcall **)(_QWORD))(a1 + 8);
if ( v10 )
v10(*(v6 - 31));
free((void *)*(v6 - 32));
memmove(v6 - 32, v6, 16LL * v5);
++v7;
v15 = 1;
}
}
else
{
v8 = *(void (__fastcall **)(_QWORD))(a1 + 8);
if ( v8 )
v8(*(v6 - 31));
memmove(v6 - 32, v6, 16LL * v5);
++v7;
}
--v5;
v6 += 2;
}
while ( v5 != -1 );
v11 = *(_DWORD *)a2 - v7;
*(_DWORD *)a2 = v11;
v12 = realloc(*(void **)(a2 + 8), 16LL * (unsigned int)(v11 - v7));
if ( v12 )
*(_QWORD *)(a2 + 8) = v12;
result = v15;
}
return result;
}

original_code:
static int remove_pair(Map *map, Bucket *bucket, const char *key)
{
    unsigned int i, n, moves, found;
    Pair *pair;
    Pair *new_pairs;
    n = bucket->count;
    if (n == 0) {
        return 0;
    }
    pair = bucket->pairs;
    i = 0;
    moves = 0;
    found = 0;
    while (i < n) {
        if ((pair->key == ((void *)0))) {
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            memmove(pair, pair+sizeof(Pair), (n - i - 1) * sizeof(Pair));
            moves++;
        } else if (strcmp(pair->key, key) == 0) {
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            free(pair->key);
            memmove(pair, pair+sizeof(Pair), (n - i - 1) * sizeof(Pair));
            moves++;
            found = 1;
        }
        pair++;
        i++;
    }
    bucket->count -= moves;
    new_pairs = realloc(bucket->pairs, (bucket->count - moves) * sizeof(Pair));
    if (new_pairs != ((void *)0)) {
        bucket->pairs = new_pairs;
    }
    return found;
}

canonical_original_code:
static int remove_pair(struct Map *map, struct Bucket *bucket, char *key)
{
    unsigned int i; unsigned int n; unsigned int moves; unsigned int found;
    struct Pair *pair;
    struct Pair *new_pairs;
    n = bucket->count;
    if (n == 0) {
        return 0;
    }
    pair = bucket->pairs;
    i = 0;
    moves = 0;
    found = 0;
    while (i < n) {
        if ((pair->key == ((void *)0))) {
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            memmove(pair, pair+sizeof(Pair), (n - i - 1) * sizeof(Pair));
            moves++;
        } else if (strcmp(pair->key, key) == 0) {
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            free(pair->key);
            memmove(pair, pair+sizeof(Pair), (n - i - 1) * sizeof(Pair));
            moves++;
            found = 1;
        }
        pair++;
        i++;
    }
    bucket->count -= moves;
    new_pairs = realloc(bucket->pairs, (bucket->count - moves) * sizeof(Pair));
    if (new_pairs != ((void *)0)) {
        bucket->pairs = new_pairs;
    }
    return found;
}

variable_types: struct Map { unsigned int count; void (*free_func)(void *value); void *(*dup_func)(void *value); struct Bucket { unsigned int count; struct Pair { char *key; void *value; } *pairs; } *buckets; } *

variable_types:
bucket
struct Bucket *

variable_types:
key
char *

variable_types:
i
unsigned int

variable_types:
n
unsigned int

variable_types:
moves
unsigned int

variable_types:
found
unsigned int

user_defined_types: struct Map { unsigned int count; void (*free_func)(void *value); void *(*dup_func)(void *value); struct Bucket *buckets; }

user_defined_types: struct Bucket { unsigned int count; struct Pair *pairs; }


decompiled_code:
void __fastcall map_delete(void *ptr)
{
unsigned int *v1; // r13
unsigned int *v2; // r14
__int64 v3; // rbx
void (__fastcall *v4)(_QWORD); // rax
__int64 v5; // rbp

if ( ptr )
{
v1 = (unsigned int *)*((_QWORD *)ptr + 3);
if ( *(_DWORD *)ptr )
{
v2 = &v1[4 * *(unsigned int *)ptr];
do
{
v3 = *((_QWORD *)v1 + 1);
if ( *v1 )
{
v5 = v3 + 16LL * *v1;
do
{
free(*(void **)v3);
v4 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 1);
if ( v4 )
v4(*(_QWORD *)(v3 + 8));
v3 += 16LL;
}
while ( v5 != v3 );
}
free(*((void **)v1 + 1));
v1 += 4;
}
while ( v1 != v2 );
}
free(*((void **)ptr + 3));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
unsigned int *v1; // r13
unsigned int *v2; // r14
__int64 v3; // rbx
void (__fastcall *v4)(_QWORD); // rax
__int64 v5; // rbp

if ( ptr )
{
v1 = (unsigned int *)*((_QWORD *)ptr + 3);
if ( *(_DWORD *)ptr )
{
v2 = &v1[4 * *(unsigned int *)ptr];
do
{
v3 = *((_QWORD *)v1 + 1);
if ( *v1 )
{
v5 = v3 + 16LL * *v1;
do
{
free(*(void **)v3);
v4 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 1);
if ( v4 )
v4(*(_QWORD *)(v3 + 8));
v3 += 16LL;
}
while ( v5 != v3 );
}
free(*((void **)v1 + 1));
v1 += 4;
}
while ( v1 != v2 );
}
free(*((void **)ptr + 3));
free(ptr);
}
}

original_code:
void map_delete(Map *map)
{
    unsigned int i, j, n, m;
    Bucket *bucket;
    Pair *pair;
    if (map == ((void *)0)) {
        return;
    }
    n = map->count;
    bucket = map->buckets;
    i = 0;
    while (i < n) {
        m = bucket->count;
        pair = bucket->pairs;
        j = 0;
        while(j < m) {
            free(pair->key);
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            pair++;
            j++;
        }
        free(bucket->pairs);
        bucket++;
        i++;
    }
    free(map->buckets);
    free(map);
}

canonical_original_code:
void map_delete(struct Map *map)
{
    unsigned int i; unsigned int j; unsigned int n; unsigned int m;
    struct Bucket *bucket;
    struct Pair *pair;
    if (map == ((void *)0)) {
        return;
    }
    n = map->count;
    bucket = map->buckets;
    i = 0;
    while (i < n) {
        m = bucket->count;
        pair = bucket->pairs;
        j = 0;
        while(j < m) {
            free(pair->key);
            if (map->free_func != 0) {
                map->free_func(pair->value);
            }
            pair++;
            j++;
        }
        free(bucket->pairs);
        bucket++;
        i++;
    }
    free(map->buckets);
    free(map);
}

variable_types:
j
unsigned int

variable_types:
m
unsigned int

variable_types: struct Bucket { unsigned int count; struct Pair { char *key; void *value; } *pairs; } *


decompiled_code:
const char **__fastcall map_get(unsigned int *a1, char *a2)
{
unsigned __int64 v2; // rax
const char **result; // rax

if ( !a2 )
return 0LL;
if ( !a1 )
return 0LL;
v2 = hash(a2);
result = get_pair((__int64)a1, (unsigned int *)(*((_QWORD *)a1 + 3) + 16 * (v2 % *a1)), a2);
if ( result )
result = (const char **)result[1];
return result;
}

canonical_decompiled_code:
const char **__fastcall func4(unsigned int *a1, char *a2)
{
unsigned __int64 v2; // rax
const char **result; // rax

if ( !a2 )
return 0LL;
if ( !a1 )
return 0LL;
v2 = func0(a2);
result = func1((__int64)a1, (unsigned int *)(*((_QWORD *)a1 + 3) + 16 * (v2 % *a1)), a2);
if ( result )
result = (const char **)result[1];
return result;
}

original_code:
void *map_get(const Map *map, const char *key)
{
    unsigned int index;
    Bucket *bucket;
    Pair *pair;
    if (map == ((void *)0)) {
        return ((void *)0);
    }
    if (key == ((void *)0)) {
        return ((void *)0);
    }
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    pair = get_pair(map, bucket, key);
    if (pair == ((void *)0)) {
        return ((void *)0);
    }
    return pair->value;
}

canonical_original_code:
void *map_get(struct Map *map, char *key)
{
    unsigned int index;
    struct Bucket *bucket;
    struct Pair *pair;
    if (map == ((void *)0)) {
        return ((void *)0);
    }
    if (key == ((void *)0)) {
        return ((void *)0);
    }
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    pair = get_pair(map, bucket, key);
    if (pair == ((void *)0)) {
        return ((void *)0);
    }
    return pair->value;
}

variable_types:
index
unsigned int

variable_types: struct Pair { char *key; void *value; } *


decompiled_code:
_BOOL8 __fastcall map_contains(unsigned int *a1, char *a2)
{
return map_get(a1, a2) != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(unsigned int *a1, char *a2)
{
return func4(a1, a2) != 0LL;
}

original_code:
int map_contains(const Map *map, const char *key)
{
    return (map_get(map, key) != ((void *)0));
}

canonical_original_code:
int map_contains(struct Map *map, char *key)
{
    return (map_get(map, key) != ((void *)0));
}


decompiled_code:
__int64 __fastcall map_remove(unsigned int *a1, char *a2)
{
__int64 result; // rax
unsigned __int64 v3; // rax

if ( !a2 )
return 0LL;
result = 0LL;
if ( a1 )
{
v3 = hash(a2);
result = remove_pair((__int64)a1, *((_QWORD *)a1 + 3) + 16 * (v3 % *a1), a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1, char *a2)
{
__int64 result; // rax
unsigned __int64 v3; // rax

if ( !a2 )
return 0LL;
result = 0LL;
if ( a1 )
{
v3 = func0(a2);
result = func2((__int64)a1, *((_QWORD *)a1 + 3) + 16 * (v3 % *a1), a2);
}
return result;
}

original_code:
int map_remove(Map *map, const char *key)
{
    unsigned int index;
    Bucket *bucket;
    if (map == ((void *)0)) {
        return 0;
    }
    if (key == ((void *)0)) {
        return 0;
    }
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    return remove_pair(map, bucket, key);
}

canonical_original_code:
int map_remove(struct Map *map, char *key)
{
    unsigned int index;
    struct Bucket *bucket;
    if (map == ((void *)0)) {
        return 0;
    }
    if (key == ((void *)0)) {
        return 0;
    }
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    return remove_pair(map, bucket, key);
}


decompiled_code:
__int64 __fastcall map_put(_QWORD *a1, char *a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v5; // r13
const char **v6; // rax
const char **v7; // r14
const char *v8; // r12
__int64 (__fastcall *v9)(__int64); // rax
void (__fastcall *v10)(const char *); // rdx
int v11; // eax
void *v12; // r14
__int64 (__fastcall *v13)(__int64); // rax
void *v14; // rax
_QWORD *v15; // rax
void (__fastcall *v16)(__int64); // rax
void *v17; // rax

if ( !a2 )
return 0LL;
result = 0LL;
if ( !a1 )
return result;
v5 = a1[3] + 16 * (hash(a2) % (unsigned __int64)*(unsigned int *)a1);
v6 = get_pair((__int64)a1, (unsigned int *)v5, a2);
v7 = v6;
if ( !v6 )
{
v11 = strlen(a2);
v12 = malloc((unsigned int)(v11 + 1));
if ( !v12 )
return 0LL;
v13 = (__int64 (__fastcall *)(__int64))a1[2];
if ( v13 )
{
a3 = v13(a3);
if ( !a3 )
{
free(v12);
return 0LL;
}
}
if ( *(_DWORD *)v5 )
{
v17 = realloc(*(void **)(v5 + 8), 16LL * (unsigned int)(*(_DWORD *)v5 + 1));
if ( v17 )
{
*(_QWORD *)(v5 + 8) = v17;
++*(_DWORD *)v5;
goto LABEL_18;
}
free(v12);
v16 = (void (__fastcall *)(__int64))a1[1];
if ( !v16 )
return 0LL;
}
else
{
v14 = malloc(0x10uLL);
*(_QWORD *)(v5 + 8) = v14;
if ( v14 )
{
*(_DWORD *)v5 = 1;
LABEL_18:
v15 = (_QWORD *)(*(_QWORD *)(v5 + 8) + 16LL * (unsigned int)(*(_DWORD *)v5 - 1));
*v15 = v12;
v15[1] = a3;
strcpy((char *)v12, a2);
return 1LL;
}
free(v12);
v16 = (void (__fastcall *)(__int64))a1[1];
if ( !v16 )
return 0LL;
}
v16(a3);
return 0LL;
}
v8 = v6[1];
v9 = (__int64 (__fastcall *)(__int64))a1[2];
if ( !v9 )
{
v7[1] = (const char *)a3;
LABEL_8:
v10 = (void (__fastcall *)(const char *))a1[1];
result = 1LL;
if ( v10 )
{
v10(v8);
result = 1LL;
}
return result;
}
if ( !a3 || (a3 = v9(a3), result = 0LL, a3) )
{
v7[1] = (const char *)a3;
goto LABEL_8;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, char *a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v5; // r13
const char **v6; // rax
const char **v7; // r14
const char *v8; // r12
__int64 (__fastcall *v9)(__int64); // rax
void (__fastcall *v10)(const char *); // rdx
int v11; // eax
void *v12; // r14
__int64 (__fastcall *v13)(__int64); // rax
void *v14; // rax
_QWORD *v15; // rax
void (__fastcall *v16)(__int64); // rax
void *v17; // rax

if ( !a2 )
return 0LL;
result = 0LL;
if ( !a1 )
return result;
v5 = a1[3] + 16 * (func0(a2) % (unsigned __int64)*(unsigned int *)a1);
v6 = func1((__int64)a1, (unsigned int *)v5, a2);
v7 = v6;
if ( !v6 )
{
v11 = strlen(a2);
v12 = malloc((unsigned int)(v11 + 1));
if ( !v12 )
return 0LL;
v13 = (__int64 (__fastcall *)(__int64))a1[2];
if ( v13 )
{
a3 = v13(a3);
if ( !a3 )
{
free(v12);
return 0LL;
}
}
if ( *(_DWORD *)v5 )
{
v17 = realloc(*(void **)(v5 + 8), 16LL * (unsigned int)(*(_DWORD *)v5 + 1));
if ( v17 )
{
*(_QWORD *)(v5 + 8) = v17;
++*(_DWORD *)v5;
goto LABEL_18;
}
free(v12);
v16 = (void (__fastcall *)(__int64))a1[1];
if ( !v16 )
return 0LL;
}
else
{
v14 = malloc(0x10uLL);
*(_QWORD *)(v5 + 8) = v14;
if ( v14 )
{
*(_DWORD *)v5 = 1;
LABEL_18:
v15 = (_QWORD *)(*(_QWORD *)(v5 + 8) + 16LL * (unsigned int)(*(_DWORD *)v5 - 1));
*v15 = v12;
v15[1] = a3;
strcpy((char *)v12, a2);
return 1LL;
}
free(v12);
v16 = (void (__fastcall *)(__int64))a1[1];
if ( !v16 )
return 0LL;
}
v16(a3);
return 0LL;
}
v8 = v6[1];
v9 = (__int64 (__fastcall *)(__int64))a1[2];
if ( !v9 )
{
v7[1] = (const char *)a3;
LABEL_8:
v10 = (void (__fastcall *)(const char *))a1[1];
result = 1LL;
if ( v10 )
{
v10(v8);
result = 1LL;
}
return result;
}
if ( !a3 || (a3 = v9(a3), result = 0LL, a3) )
{
v7[1] = (const char *)a3;
goto LABEL_8;
}
return result;
}

original_code:
int map_put(Map *map, const char *key, void *value)
{
    unsigned int key_len, index;
    Bucket *bucket;
    Pair *tmp_pairs, *pair;
    char *new_key;
    void *new_value;
    void *old_value;
    if (map == ((void *)0)) {
        return 0;
    }
    if (key == ((void *)0)) {
        return 0;
    }
    key_len = strlen(key);
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    if ((pair = get_pair(map, bucket, key)) != ((void *)0)) {
        old_value = pair->value;
        if (map->dup_func != ((void *)0)) {
            if (value != ((void *)0)) {
                new_value = map->dup_func(value);
                if (new_value == ((void *)0)) {
                    return 0;
                }
            } else {
                new_value = ((void *)0);
            }
            pair->value = new_value;
        } else {
            pair->value = value;
        }
        if (map->free_func != ((void *)0)) {
            map->free_func(old_value);
        }
        return 1;
    }
    new_key = malloc((key_len + 1) * sizeof(char));
    if (new_key == ((void *)0)) {
        return 0;
    }
    if (map->dup_func != ((void *)0)) {
        new_value = map->dup_func(value);
        if (new_value == ((void *)0)) {
            free(new_key);
            return 0;
        }
    } else {
        new_value = value;
    }
    if (bucket->count == 0) {
        bucket->pairs = malloc(sizeof(Pair));
        if (bucket->pairs == ((void *)0)) {
            free(new_key);
            if (map->free_func != ((void *)0)) {
                map->free_func(new_value);
            }
            return 0;
        }
        bucket->count = 1;
    }
    else {
        tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
        if (tmp_pairs == ((void *)0)) {
            free(new_key);
            if (map->free_func != ((void *)0)) {
                map->free_func(new_value);
            }
            return 0;
        }
        bucket->pairs = tmp_pairs;
        bucket->count++;
    }
    pair = &(bucket->pairs[bucket->count - 1]);
    pair->key = new_key;
    pair->value = new_value;
    strcpy(pair->key, key);
    return 1;
}

canonical_original_code:
int map_put(struct Map *map, char *key, void *value)
{
    unsigned int key_len; unsigned int index;
    struct Bucket *bucket;
    struct Pair *tmp_pairs; struct Pair *pair;
    char *new_key;
    void *new_value;
    void *old_value;
    if (map == ((void *)0)) {
        return 0;
    }
    if (key == ((void *)0)) {
        return 0;
    }
    key_len = strlen(key);
    index = hash(key) % map->count;
    bucket = &(map->buckets[index]);
    if ((pair = get_pair(map, bucket, key)) != ((void *)0)) {
        old_value = pair->value;
        if (map->dup_func != ((void *)0)) {
            if (value != ((void *)0)) {
                new_value = map->dup_func(value);
                if (new_value == ((void *)0)) {
                    return 0;
                }
            } else {
                new_value = ((void *)0);
            }
            pair->value = new_value;
        } else {
            pair->value = value;
        }
        if (map->free_func != ((void *)0)) {
            map->free_func(old_value);
        }
        return 1;
    }
    new_key = malloc((key_len + 1) * sizeof(char));
    if (new_key == ((void *)0)) {
        return 0;
    }
    if (map->dup_func != ((void *)0)) {
        new_value = map->dup_func(value);
        if (new_value == ((void *)0)) {
            free(new_key);
            return 0;
        }
    } else {
        new_value = value;
    }
    if (bucket->count == 0) {
        bucket->pairs = malloc(sizeof(Pair));
        if (bucket->pairs == ((void *)0)) {
            free(new_key);
            if (map->free_func != ((void *)0)) {
                map->free_func(new_value);
            }
            return 0;
        }
        bucket->count = 1;
    }
    else {
        tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
        if (tmp_pairs == ((void *)0)) {
            free(new_key);
            if (map->free_func != ((void *)0)) {
                map->free_func(new_value);
            }
            return 0;
        }
        bucket->pairs = tmp_pairs;
        bucket->count++;
    }
    pair = &(bucket->pairs[bucket->count - 1]);
    pair->key = new_key;
    pair->value = new_value;
    strcpy(pair->key, key);
    return 1;
}

variable_types:
value
void *


decompiled_code:
__int64 __fastcall map_get_count(int *a1)
{
int *v1; // rcx
unsigned int v2; // esi
int *v3; // rdi
int v4; // edx
int v5; // eax

if ( !a1 )
return 0LL;
v1 = (int *)*((_QWORD *)a1 + 3);
v2 = *a1;
if ( *a1 )
{
v3 = &v1[4 * v2];
v2 = 0;
do
{
v4 = *v1;
if ( *v1 )
{
v5 = 0;
do
++v5;
while ( v4 != v5 );
v2 += v4;
}
v1 += 4;
}
while ( v3 != v1 );
}
return v2;
}

original_code:
int map_get_count(const Map *map)
{
    unsigned int i, j, n, m;
    unsigned int count;
    Bucket *bucket;
    Pair *pair;
    if (map == ((void *)0)) {
        return 0;
    }
    bucket = map->buckets;
    n = map->count;
    i = 0;
    count = 0;
    while (i < n) {
        pair = bucket->pairs;
        m = bucket->count;
        j = 0;
        while (j < m) {
            count++;
            pair++;
            j++;
        }
        bucket++;
        i++;
    }
    return count;
}

canonical_original_code:
int map_get_count(struct Map *map)
{
    unsigned int i; unsigned int j; unsigned int n; unsigned int m;
    unsigned int count;
    struct Bucket *bucket;
    struct Pair *pair;
    if (map == ((void *)0)) {
        return 0;
    }
    bucket = map->buckets;
    n = map->count;
    i = 0;
    count = 0;
    while (i < n) {
        pair = bucket->pairs;
        m = bucket->count;
        j = 0;
        while (j < m) {
            count++;
            pair++;
            j++;
        }
        bucket++;
        i++;
    }
    return count;
}

variable_types:
count
unsigned int


decompiled_code:
__int64 __fastcall map_enum(unsigned int *a1, void (__fastcall *a2)(_QWORD, _QWORD, __int64), __int64 a3)
{
unsigned int *v4; // r14
unsigned int *v5; // r15
_QWORD *v6; // rbx
_QWORD *v7; // rbp

if ( !a2 )
return 0LL;
if ( !a1 )
return 0LL;
v4 = (unsigned int *)*((_QWORD *)a1 + 3);
if ( !*a1 )
return 1LL;
v5 = &v4[4 * *a1];
do
{
v6 = (_QWORD *)*((_QWORD *)v4 + 1);
if ( *v4 )
{
v7 = &v6[2 * *v4];
do
{
a2(*v6, v6[1], a3);
v6 += 2;
}
while ( v7 != v6 );
}
v4 += 4;
}
while ( v4 != v5 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1, void (__fastcall *a2)(_QWORD, _QWORD, __int64), __int64 a3)
{
unsigned int *v4; // r14
unsigned int *v5; // r15
_QWORD *v6; // rbx
_QWORD *v7; // rbp

if ( !a2 )
return 0LL;
if ( !a1 )
return 0LL;
v4 = (unsigned int *)*((_QWORD *)a1 + 3);
if ( !*a1 )
return 1LL;
v5 = &v4[4 * *a1];
do
{
v6 = (_QWORD *)*((_QWORD *)v4 + 1);
if ( *v4 )
{
v7 = &v6[2 * *v4];
do
{
a2(*v6, v6[1], a3);
v6 += 2;
}
while ( v7 != v6 );
}
v4 += 4;
}
while ( v4 != v5 );
return 1LL;
}

original_code:
int map_enum(const Map *map, map_enum_func enum_func, const void *obj)
{
    unsigned int i, j, n, m;
    Bucket *bucket;
    Pair *pair;
    if (map == ((void *)0)) {
        return 0;
    }
    if (enum_func == ((void *)0)) {
        return 0;
    }
    bucket = map->buckets;
    n = map->count;
    i = 0;
    while (i < n) {
        pair = bucket->pairs;
        m = bucket->count;
        j = 0;
        while (j < m) {
            enum_func(pair->key, pair->value, obj);
            pair++;
            j++;
        }
        bucket++;
        i++;
    }
    return 1;
}

canonical_original_code:
int map_enum(struct Map *map, void (*enum_func)(char *key, void *value, void *obj), void *obj)
{
    unsigned int i; unsigned int j; unsigned int n; unsigned int m;
    struct Bucket *bucket;
    struct Pair *pair;
    if (map == ((void *)0)) {
        return 0;
    }
    if (enum_func == ((void *)0)) {
        return 0;
    }
    bucket = map->buckets;
    n = map->count;
    i = 0;
    while (i < n) {
        pair = bucket->pairs;
        m = bucket->count;
        j = 0;
        while (j < m) {
            enum_func(pair->key, pair->value, obj);
            pair++;
            j++;
        }
        bucket++;
        i++;
    }
    return 1;
}

variable_types: void (*)(char *key, void *value, void *obj)

variable_types:
obj
void *


decompiled_code:
_QWORD *buffer_init()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[2] = 0LL;
result[1] = 0LL;
result[3] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[2] = 0LL;
result[1] = 0LL;
result[3] = 0LL;
}
return result;
}

original_code:
buffer_t* buffer_init()
{
    buffer_t* buf = ((void *)0);
    buf = (buffer_t*)malloc(sizeof(buffer_t));
    if (buf) {
        buf->content = ((void *)0);
        buf->capacity = 0;
        buf->length = 0;
        buf->offset = 0;
    }
    return buf;
}

canonical_original_code:
struct _buffer *buffer_init()
{
    struct _buffer *buf = ((void *)0);
    buf = (struct _buffer *)malloc(sizeof(buffer_t));
    if (buf) {
        buf->content = ((void *)0);
        buf->capacity = 0;
        buf->length = 0;
        buf->offset = 0;
    }
    return buf;
}

variable_types: struct _buffer { unsigned char *content; unsigned long length; unsigned long capacity; unsigned long offset; } *


original_code:
void buffer_reset(buffer_t* buf)
{
    if (buf) {
        if (buf->content) {
            free(buf->content);
            buf->content = ((void *)0);
        }
        buf->capacity = 0;
        buf->length = 0;
        buf->offset = 0;
    }
}

canonical_original_code:
void buffer_reset(struct _buffer *buf)
{
    if (buf) {
        if (buf->content) {
            free(buf->content);
            buf->content = ((void *)0);
        }
        buf->capacity = 0;
        buf->length = 0;
        buf->offset = 0;
    }
}

user_defined_types: struct _buffer { unsigned char *content; unsigned long length; unsigned long capacity; unsigned long offset; }


decompiled_code:
__int64 __fastcall buffer_destroy(void *ptr)
{
if ( ptr )
{
buffer_reset((__int64)ptr);
free(ptr);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
if ( ptr )
{
func1((__int64)ptr);
free(ptr);
}
return 0LL;
}

original_code:
buffer_t* buffer_destroy(buffer_t* buf)
{
    if (buf) {
        buffer_reset(buf);
        free(buf);
        buf = ((void *)0);
    }
    return buf;
}

canonical_original_code:
struct _buffer *buffer_destroy(struct _buffer *buf)
{
    if (buf) {
        buffer_reset(buf);
        free(buf);
        buf = ((void *)0);
    }
    return buf;
}

variable_types:
buf
struct _buffer *

return_type: struct _buffer { unsigned char *content; unsigned long length; unsigned long capacity; unsigned long offset; } *


decompiled_code:
_BOOL8 __fastcall buffer_good(_QWORD *a1)
{
unsigned __int64 v1; // rdx
_BOOL8 result; // rax

if ( !a1 )
return 0LL;
v1 = a1[1];
result = 0LL;
if ( v1 <= a1[2] )
result = v1 >= a1[3];
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_QWORD *a1)
{
unsigned __int64 v1; // rdx
_BOOL8 result; // rax

if ( !a1 )
return 0LL;
v1 = a1[1];
result = 0LL;
if ( v1 <= a1[2] )
result = v1 >= a1[3];
return result;
}

original_code:
int buffer_good(const buffer_t* buf)
{
    if (!buf) return 0;
    if (buf->length > buf->capacity) return 0;
    if (buf->offset > buf->length) return 0;
    return 1;
}

canonical_original_code:
int buffer_good(struct _buffer *buf)
{
    if (!buf) return 0;
    if (buf->length > buf->capacity) return 0;
    if (buf->offset > buf->length) return 0;
    return 1;
}


decompiled_code:
unsigned __int64 __fastcall buffer_seek(_QWORD *a1, unsigned __int64 a2)
{
_BOOL4 v2; // edx
unsigned __int64 result; // rax

v2 = buffer_good(a1);
result = 0LL;
if ( v2 && a1[1] >= a2 )
{
a1[3] = a2;
result = a2;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(_QWORD *a1, unsigned __int64 a2)
{
_BOOL4 v2; // edx
unsigned __int64 result; // rax

v2 = func3(a1);
result = 0LL;
if ( v2 && a1[1] >= a2 )
{
a1[3] = a2;
result = a2;
}
return result;
}

original_code:
size_t buffer_seek(buffer_t* buf, size_t offset)
{
    size_t new_offset = 0;
    if (buffer_good(buf)) {
        if (offset <= buf->length) {
            new_offset = buf->offset = offset;
        }
    }
    return new_offset;
}

canonical_original_code:
unsigned long buffer_seek(struct _buffer *buf, unsigned long offset)
{
    unsigned long new_offset = 0;
    if (buffer_good(buf)) {
        if (offset <= buf->length) {
            new_offset = buf->offset = offset;
        }
    }
    return new_offset;
}

variable_types:
offset
unsigned long


decompiled_code:
unsigned __int64 __fastcall buffer_seek_start(_QWORD *a1)
{
return buffer_seek(a1, 0LL);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(_QWORD *a1)
{
return func4(a1, 0LL);
}

original_code:
size_t buffer_seek_start(buffer_t* buf)
{
    return buffer_seek(buf, 0);
}

canonical_original_code:
unsigned long buffer_seek_start(struct _buffer *buf)
{
    return buffer_seek(buf, 0);
}


decompiled_code:
__int64 __fastcall buffer_seek_end(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = buffer_good(a1);
result = 0LL;
if ( v1 )
{
result = a1[1];
a1[3] = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = func3(a1);
result = 0LL;
if ( v1 )
{
result = a1[1];
a1[3] = result;
}
return result;
}

original_code:
size_t buffer_seek_end(buffer_t* buf)
{
    size_t new_offset = 0;
    if (buffer_good(buf)) {
        new_offset = buf->offset = buf->length;
    }
    return new_offset;
}

canonical_original_code:
unsigned long buffer_seek_end(struct _buffer *buf)
{
    unsigned long new_offset = 0;
    if (buffer_good(buf)) {
        new_offset = buf->offset = buf->length;
    }
    return new_offset;
}


decompiled_code:
__int64 __fastcall buffer_erase(void **a1, unsigned __int64 a2)
{
_BOOL4 v3; // eax
char *v5; // rdi
_BYTE *v6; // rdx
void *v7; // rax
size_t v8; // r12
void *v9; // rax

v3 = buffer_good(a1);
if ( a2 )
{
if ( v3 )
{
v5 = (char *)*a1;
if ( *a1 )
{
v6 = a1[1];
v7 = a1[3];
if ( v6 - (_BYTE *)v7 >= a2 )
{
v8 = (size_t)&v6[-a2];
memmove(&v5[(_QWORD)v7], (char *)v7 + a2 + (_QWORD)v5, v6 - ((_BYTE *)v7 + a2));
v9 = realloc(*a1, v8);
if ( v9 )
{
*a1 = v9;
a1[2] = (void *)v8;
a1[1] = (void *)v8;
buffer_seek_end(a1);
}
}
}
}
}
return 0LL;
}

original_code:
size_t buffer_erase(buffer_t* buf, size_t length)
{
    size_t bytes_erased = 0;
    uint8_t* tmp_buf = ((void *)0);
    uint8_t* src = ((void *)0);
    uint8_t* dst = ((void *)0);
    size_t copy_length = 0;
    size_t new_length = 0;
    if ( buffer_good(buf) && length && buf->content &&
         ((buf->length - buf->offset) >= length) ) {
        dst = buf->content + buf->offset;
        src = buf->content + buf->offset + length;
        copy_length = buf->length - (buf->offset + length);
        new_length = buf->length - length;
        memmove(dst, src, copy_length);
        tmp_buf = (uint8_t*)realloc(buf->content, new_length);
        if (tmp_buf) {
            buf->content = tmp_buf;
            buf->capacity = new_length;
            buf->length = new_length;
            buffer_seek_end(buf);
        }
    }
    return bytes_erased;
}

canonical_original_code:
unsigned long buffer_erase(struct _buffer *buf, unsigned long length)
{
    unsigned long bytes_erased = 0;
    unsigned char *tmp_buf = ((void *)0);
    unsigned char *src = ((void *)0);
    unsigned char *dst = ((void *)0);
    unsigned long copy_length = 0;
    unsigned long new_length = 0;
    if ( buffer_good(buf) && length && buf->content &&
         ((buf->length - buf->offset) >= length) ) {
        dst = buf->content + buf->offset;
        src = buf->content + buf->offset + length;
        copy_length = buf->length - (buf->offset + length);
        new_length = buf->length - length;
        memmove(dst, src, copy_length);
        tmp_buf = (unsigned char *)realloc(buf->content, new_length);
        if (tmp_buf) {
            buf->content = tmp_buf;
            buf->capacity = new_length;
            buf->length = new_length;
            buffer_seek_end(buf);
        }
    }
    return bytes_erased;
}

variable_types:
length
unsigned long

variable_types:
src
unsigned char *

variable_types:
dst
unsigned char *


decompiled_code:
size_t __fastcall buffer_read(_QWORD *a1, void *a2, __int64 a3)
{
size_t v5; // r12
unsigned __int64 v6; // rax
unsigned __int64 v7; // rsi

v5 = 0LL;
if ( buffer_good(a1) )
{
if ( a2 && a3 )
{
v6 = a1[1];
v7 = a1[3];
if ( v6 > v7 )
{
v5 = v6 - v7;
if ( *a1 )
memcpy(a2, (const void *)(*a1 + v7), v5);
else
v5 = 0LL;
}
}
else
{
v5 = 0LL;
}
}
return v5;
}

canonical_decompiled_code:
size_t __fastcall func8(_QWORD *a1, void *a2, __int64 a3)
{
size_t v5; // r12
unsigned __int64 v6; // rax
unsigned __int64 v7; // rsi

v5 = 0LL;
if ( func3(a1) )
{
if ( a2 && a3 )
{
v6 = a1[1];
v7 = a1[3];
if ( v6 > v7 )
{
v5 = v6 - v7;
if ( *a1 )
memcpy(a2, (const void *)(*a1 + v7), v5);
else
v5 = 0LL;
}
}
else
{
v5 = 0LL;
}
}
return v5;
}

original_code:
size_t buffer_read(buffer_t* buf, uint8_t* dst, size_t length)
{
    size_t bytes_read = 0;
    uint8_t* src = ((void *)0);
    size_t src_len = 0;
    if (buffer_good(buf) && dst && length) {
        if (buf->length > buf->offset) {
            src_len = buf->length - buf->offset;
        }
        if (buf->content && src_len) {
            src = buf->content + buf->offset;
            memcpy(dst, src, src_len);
            bytes_read = src_len;
        }
    }
    return bytes_read;
}

canonical_original_code:
unsigned long buffer_read(struct _buffer *buf, unsigned char *dst, unsigned long length)
{
    unsigned long bytes_read = 0;
    unsigned char *src = ((void *)0);
    unsigned long src_len = 0;
    if (buffer_good(buf) && dst && length) {
        if (buf->length > buf->offset) {
            src_len = buf->length - buf->offset;
        }
        if (buf->content && src_len) {
            src = buf->content + buf->offset;
            memcpy(dst, src, src_len);
            bytes_read = src_len;
        }
    }
    return bytes_read;
}


decompiled_code:
size_t __fastcall buffer_write(char **a1, const void *a2, size_t a3)
{
_BOOL4 v5; // edx
size_t result; // rax
size_t v7; // r14
size_t v8; // r13
char *v9; // rdi

v5 = buffer_good(a1);
result = 0LL;
if ( v5 )
{
if ( a2 && a3 )
{
v7 = (size_t)a1[2];
v8 = (size_t)&a1[3][a3];
if ( (unsigned __int64)a1[1] >= v8 )
v8 = (size_t)a1[1];
v9 = *a1;
if ( v7 >= v8 && v9 )
goto LABEL_8;
v9 = (char *)realloc(v9, v8);
result = 0LL;
if ( v9 )
{
v7 = v8;
LABEL_8:
*a1 = v9;
memcpy(&v9[(_QWORD)a1[3]], a2, a3);
a1[1] = (char *)v8;
a1[3] += a3;
a1[2] = (char *)v7;
return a3;
}
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func9(char **a1, const void *a2, size_t a3)
{
_BOOL4 v5; // edx
size_t result; // rax
size_t v7; // r14
size_t v8; // r13
char *v9; // rdi

v5 = func3(a1);
result = 0LL;
if ( v5 )
{
if ( a2 && a3 )
{
v7 = (size_t)a1[2];
v8 = (size_t)&a1[3][a3];
if ( (unsigned __int64)a1[1] >= v8 )
v8 = (size_t)a1[1];
v9 = *a1;
if ( v7 >= v8 && v9 )
goto LABEL_8;
v9 = (char *)realloc(v9, v8);
result = 0LL;
if ( v9 )
{
v7 = v8;
LABEL_8:
*a1 = v9;
memcpy(&v9[(_QWORD)a1[3]], a2, a3);
a1[1] = (char *)v8;
a1[3] += a3;
a1[2] = (char *)v7;
return a3;
}
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
size_t buffer_write(buffer_t* buf, const uint8_t* src, size_t length)
{
    size_t bytes_written = 0;
    uint8_t* tmp_buf = ((void *)0);
    uint8_t* dst = ((void *)0);
    size_t new_length = 0;
    size_t new_capacity = 0;
    if (buffer_good(buf) && src && length) {
        new_capacity = buf->capacity;
        new_length = buf->offset + length;
        if (new_length < buf->length) {
            new_length = buf->length;
        }
        if (buf->content && (new_length <= buf->capacity)) {
            tmp_buf = buf->content;
        } else {
            new_capacity = new_length;
            tmp_buf = (uint8_t*)realloc(buf->content, new_capacity);
        }
        if (tmp_buf) {
            buf->content = tmp_buf;
            dst = buf->content + buf->offset;
            memcpy(dst, src, length);
            bytes_written = length;
            buf->length = new_length;
            buf->offset += bytes_written;
            buf->capacity = new_capacity;
        }
    }
    return bytes_written;
}

canonical_original_code:
unsigned long buffer_write(struct _buffer *buf, unsigned char *src, unsigned long length)
{
    unsigned long bytes_written = 0;
    unsigned char *tmp_buf = ((void *)0);
    unsigned char *dst = ((void *)0);
    unsigned long new_length = 0;
    unsigned long new_capacity = 0;
    if (buffer_good(buf) && src && length) {
        new_capacity = buf->capacity;
        new_length = buf->offset + length;
        if (new_length < buf->length) {
            new_length = buf->length;
        }
        if (buf->content && (new_length <= buf->capacity)) {
            tmp_buf = buf->content;
        } else {
            new_capacity = new_length;
            tmp_buf = (unsigned char *)realloc(buf->content, new_capacity);
        }
        if (tmp_buf) {
            buf->content = tmp_buf;
            dst = buf->content + buf->offset;
            memcpy(dst, src, length);
            bytes_written = length;
            buf->length = new_length;
            buf->offset += bytes_written;
            buf->capacity = new_capacity;
        }
    }
    return bytes_written;
}


decompiled_code:
__int64 __fastcall buffer_size(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = buffer_good(a1);
result = 0LL;
if ( v1 )
result = a1[1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = func3(a1);
result = 0LL;
if ( v1 )
result = a1[1];
return result;
}

original_code:
size_t buffer_size(const buffer_t* buf)
{
    size_t size = 0;
    if (buffer_good(buf)) {
        size = buf->length;
    }
    return size;
}

canonical_original_code:
unsigned long buffer_size(struct _buffer *buf)
{
    unsigned long size = 0;
    if (buffer_good(buf)) {
        size = buf->length;
    }
    return size;
}

variable_types:
size
unsigned long


decompiled_code:
size_t __fastcall buffer_terminate(char **a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
return buffer_write(a1, v2, 1uLL);
}

canonical_decompiled_code:
size_t __fastcall func11(char **a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
return func9(a1, v2, 1uLL);
}

original_code:
int buffer_terminate(buffer_t* buf) {
    int result = 0;
    uint8_t terminator = 0;
    result = (int)buffer_write(buf, &terminator, sizeof(terminator));
    return result;
}

canonical_original_code:
int buffer_terminate(struct _buffer *buf) {
    int result = 0;
    unsigned char terminator = 0;
    result = (int)buffer_write(buf, &terminator, sizeof(terminator));
    return result;
}

variable_types: result int

variable_types:
terminator
unsigned char


decompiled_code:
__int64 __fastcall buffer_detach(__int64 *a1)
{
__int64 v1; // rbp

v1 = 0LL;
if ( buffer_good(a1) )
{
v1 = *a1;
*a1 = 0LL;
buffer_reset((__int64)a1);
}
return v1;
}

original_code:
uint8_t* buffer_detach(buffer_t* buf)
{
    uint8_t* buf_content = ((void *)0);
    if (buffer_good(buf)) {
        buf_content = buf->content;
        buf->content = ((void *)0);
        buffer_reset(buf);
    }
    return buf_content;
}

canonical_original_code:
unsigned char *buffer_detach(struct _buffer *buf)
{
    unsigned char *buf_content = ((void *)0);
    if (buffer_good(buf)) {
        buf_content = buf->content;
        buf->content = ((void *)0);
        buffer_reset(buf);
    }
    return buf_content;
}

return_type: unsigned char *


decompiled_code:
_BOOL8 __fastcall is_probably_an_url(const char *a1)
{
return strstr(a1, "://") != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char *a1)
{
return strstr(a1, "://") != 0LL;
}

original_code:
int
is_probably_an_url(char *string)
{
    if( strstr(string, "://") )
        return 1;
    return 0;
}

variable_types:
string
char *


decompiled_code:
char *__fastcall get_service_type(char *s1, _QWORD *a2)
{
char *result; // rax

if ( strstr(s1, "http://") )
{
result = s1 + 7;
*((_DWORD *)a2 + 2) = 1;
}
else if ( strstr(s1, "ftp://") )
{
result = s1 + 6;
*((_DWORD *)a2 + 2) = 2;
}
else if ( strstr(s1, "gopher://") )
{
result = s1 + 9;
*((_DWORD *)a2 + 2) = 3;
}
else if ( !strncasecmp(s1, "www", 3uLL) )
{
*((_DWORD *)a2 + 2) = 1;
*a2 = strconcat("http://", *a2, 0LL);
result = s1;
}
else
{
if ( !strncasecmp(s1, "ftp", 3uLL) )
{
*((_DWORD *)a2 + 2) = 2;
*a2 = strconcat("ftp://", *a2, 0LL);
}
else
{
*a2 = strconcat("http://", *a2, 0LL);
*((_DWORD *)a2 + 2) = 1;
}
result = s1;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *s1, _QWORD *a2)
{
char *result; // rax

if ( strstr(s1, "http://") )
{
result = s1 + 7;
*((_DWORD *)a2 + 2) = 1;
}
else if ( strstr(s1, "ftp://") )
{
result = s1 + 6;
*((_DWORD *)a2 + 2) = 2;
}
else if ( strstr(s1, "gopher://") )
{
result = s1 + 9;
*((_DWORD *)a2 + 2) = 3;
}
else if ( !strncasecmp(s1, "www", 3uLL) )
{
*((_DWORD *)a2 + 2) = 1;
*a2 = strconcat("http://", *a2, 0LL);
result = s1;
}
else
{
if ( !strncasecmp(s1, "ftp", 3uLL) )
{
*((_DWORD *)a2 + 2) = 2;
*a2 = strconcat("ftp://", *a2, 0LL);
}
else
{
*a2 = strconcat("http://", *a2, 0LL);
*((_DWORD *)a2 + 2) = 1;
}
result = s1;
}
return result;
}

original_code:
char *
get_service_type(char *string, Url *u)
{
    if( strstr(string, "http://") ) {
        string += 7;
        u->service_type = SERVICE_HTTP;
        return string;
    }
    if( strstr(string, "ftp://") ) {
        string += 6;
        u->service_type = SERVICE_FTP;
        return string;
    }
    if( strstr(string, "gopher://") ) {
        string += 9;
        u->service_type = SERVICE_GOPHER;
        return string;
    }
    if( strncasecmp(string, "www", 3) == 0 ) {
        u->service_type = SERVICE_HTTP;
        u->full_url = strconcat("http://", u->full_url, ((void *)0));
        return string;
    }
    if( strncasecmp(string, "ftp", 3) == 0 ) {
        u->service_type = SERVICE_FTP;
        u->full_url = strconcat("ftp://", u->full_url, ((void *)0));
        return string;
    }
    u->full_url = strconcat("http://", u->full_url, ((void *)0));
    u->service_type = SERVICE_HTTP;
    return string;
}

canonical_original_code:
char *get_service_type(char *string, struct _Url *u)
{
    if( strstr(string, "http://") ) {
        string += 7;
        u->service_type = SERVICE_HTTP;
        return string;
    }
    if( strstr(string, "ftp://") ) {
        string += 6;
        u->service_type = SERVICE_FTP;
        return string;
    }
    if( strstr(string, "gopher://") ) {
        string += 9;
        u->service_type = SERVICE_GOPHER;
        return string;
    }
    if( strncasecmp(string, "www", 3) == 0 ) {
        u->service_type = SERVICE_HTTP;
        u->full_url = strconcat("http://", u->full_url, ((void *)0));
        return string;
    }
    if( strncasecmp(string, "ftp", 3) == 0 ) {
        u->service_type = SERVICE_FTP;
        u->full_url = strconcat("ftp://", u->full_url, ((void *)0));
        return string;
    }
    u->full_url = strconcat("http://", u->full_url, ((void *)0));
    u->service_type = SERVICE_HTTP;
    return string;
}

variable_types: struct _Url { char *full_url; int service_type; char *username; char *password; char *host; int port; char *path; char *file; } *

return_type: char *


original_code:
char *
get_username(char *string, Url *u)
{
    int i;
    char *username;
    char *at;
    char *slash;
    at = strchr(string, '@');
    slash = strchr(string, '/');
    if( (!at) || (slash && (at >= slash)) )
        return string;
    for( i = 0; string[i] && string[i] != ':' &&
         string[i] != '@' && string[i] != '/'; i++);
    if( string[i] != '@' && string[i] != ':' ) {
        return string;
    }
    username = (char*)malloc(i);
    memcpy(username, string, i + 1);
    username[i] = '\0';
    string += i + 1;
    u->username = username;
    return string;
}

canonical_original_code:
char *get_username(char *string, struct _Url *u)
{
    int i;
    char *username;
    char *at;
    char *slash;
    at = strchr(string, '@');
    slash = strchr(string, '/');
    if( (!at) || (slash && (at >= slash)) )
        return string;
    for( i = 0; string[i] && string[i] != ':' &&
         string[i] != '@' && string[i] != '/'; i++);
    if( string[i] != '@' && string[i] != ':' ) {
        return string;
    }
    username = (char *)malloc(i);
    memcpy(username, string, i + 1);
    username[i] = '\0';
    string += i + 1;
    u->username = username;
    return string;
}

variable_types: i int

variable_types:
username
char *

variable_types:
at
char *

variable_types:
slash
char *

user_defined_types: struct _Url { char *full_url; int service_type; char *username; char *password; char *host; int port; char *path; char *file; }


decompiled_code:
char *__fastcall get_password(void *src, __int64 a2)
{
char *v2; // rax
char *v3; // rbp
char *v4; // rax
char *v5; // rdx
__int64 v6; // rax
size_t v7; // rbp
void *v8; // r13

v2 = strchr((const char *)src, 64);
if ( !v2 )
return (char *)src;
v3 = v2;
v4 = strchr((const char *)src, 47);
if ( !v4 || (v5 = (char *)src, v3 < v4) )
{
if ( *(_BYTE *)src == 64 )
{
v7 = 0LL;
}
else
{
v6 = 1LL;
do
v7 = v6++;
while ( *((char *)src + v6 - 1) != 64 );
}
v8 = malloc(v7);
memcpy(v8, src, v7);
*((_BYTE *)v8 + v7) = 0;
v5 = (char *)src + v7 + 1;
*(_QWORD *)(a2 + 24) = v8;
}
return v5;
}

canonical_decompiled_code:
char *__fastcall func3(void *src, __int64 a2)
{
char *v2; // rax
char *v3; // rbp
char *v4; // rax
char *v5; // rdx
__int64 v6; // rax
size_t v7; // rbp
void *v8; // r13

v2 = strchr((const char *)src, 64);
if ( !v2 )
return (char *)src;
v3 = v2;
v4 = strchr((const char *)src, 47);
if ( !v4 || (v5 = (char *)src, v3 < v4) )
{
if ( *(_BYTE *)src == 64 )
{
v7 = 0LL;
}
else
{
v6 = 1LL;
do
v7 = v6++;
while ( *((char *)src + v6 - 1) != 64 );
}
v8 = malloc(v7);
memcpy(v8, src, v7);
*((_BYTE *)v8 + v7) = 0;
v5 = (char *)src + v7 + 1;
*(_QWORD *)(a2 + 24) = v8;
}
return v5;
}

original_code:
char *
get_password(char *string, Url *u)
{
    int i;
    char *password;
    char *at;
    char *slash;
    at = strchr(string, '@');
    slash = strchr(string, '/');
    if( (!at) || (slash && (at >= slash)) )
        return string;
    for(i = 0 ; string[i] != '@'; i++);
    password = (char*)malloc(i);
    memcpy(password, string, i);
    password[i] = '\0';
    string += i + 1;
    u->password = password;
    return string;
}

canonical_original_code:
char *get_password(char *string, struct _Url *u)
{
    int i;
    char *password;
    char *at;
    char *slash;
    at = strchr(string, '@');
    slash = strchr(string, '/');
    if( (!at) || (slash && (at >= slash)) )
        return string;
    for(i = 0 ; string[i] != '@'; i++);
    password = (char *)malloc(i);
    memcpy(password, string, i);
    password[i] = '\0';
    string += i + 1;
    u->password = password;
    return string;
}

variable_types:
password
char *


original_code:
char *
get_hostname(char *url, Url *u)
{
    char *hostname;
    int i;
    for( i = 0; url[i] && url[i] != '/' && url[i] != ':'; i++ );
    hostname = (char*)malloc(i + 1);
    memcpy(hostname, url, i);
    hostname[i] = '\0';
    if(url[i] == ':')
        url += i + 1;
    else
        url += i;
    u->host = hostname;
    return url;
}

canonical_original_code:
char *get_hostname(char *url, struct _Url *u)
{
    char *hostname;
    int i;
    for( i = 0; url[i] && url[i] != '/' && url[i] != ':'; i++ );
    hostname = (char *)malloc(i + 1);
    memcpy(hostname, url, i);
    hostname[i] = '\0';
    if(url[i] == ':')
        url += i + 1;
    else
        url += i;
    u->host = hostname;
    return url;
}

variable_types:
url
char *

variable_types:
hostname
char *


decompiled_code:
void *__fastcall get_port(void *src, __int64 a2)
{
void *v2; // rbx
const unsigned __int16 *v4; // rsi
__int64 v5; // rax
int v6; // ecx
char *v7; // rbp
__int64 v8; // rdx
__int64 v9; // r12
size_t v11; // r14
void *v12; // r15

v2 = src;
v4 = *_ctype_b_loc();
v5 = 0LL;
do
{
v6 = v5;
v7 = (char *)src + v5;
v8 = *((char *)src + v5);
v9 = v5++;
}
while ( (v4[v8] & 0x800) != 0 );
if ( v6 )
{
v11 = v6 + 1;
v12 = malloc(v11);
memcpy(v12, src, v11);
*((_BYTE *)v12 + v9) = 0;
*(_DWORD *)(a2 + 40) = atoi((const char *)v12);
free(v12);
v2 = v7;
}
return v2;
}

original_code:
char *
get_port(char *url, Url *u)
{
    char *port_string;
    int i;
    for(i = 0; ((*__ctype_b_loc ())[(int) ((url[i]))] & (unsigned short int) _ISdigit); i++);
    if(i == 0)
        return url;
    port_string = (char*)malloc(i + 1);
    memcpy(port_string, url, i + 1);
    port_string[i] = '\0';
    url += i;
    u->port = atoi(port_string);
    if(port_string) free(port_string);
    return url;
}

canonical_original_code:
char *get_port(char *url, struct _Url *u)
{
    char *port_string;
    int i;
    for(i = 0; ((*__ctype_b_loc ())[(int) ((url[i]))] & (unsigned short) _ISdigit); i++);
    if(i == 0)
        return url;
    port_string = (char *)malloc(i + 1);
    memcpy(port_string, url, i + 1);
    port_string[i] = '\0';
    url += i;
    u->port = atoi(port_string);
    if(port_string) free(port_string);
    return url;
}


decompiled_code:
char *__fastcall get_path(void *src, __int64 a2)
{
char *v2; // rbx
int v3; // eax
int v4; // er12
_BYTE *v5; // rax
__int64 v6; // r13
void *v8; // r14

v2 = (char *)src;
v3 = strlen((const char *)src);
v4 = v3;
if ( v3 > 0 )
{
v5 = (char *)src + v3;
while ( *v5 != 47 )
{
--v5;
if ( !--v4 )
goto LABEL_5;
}
v6 = v4;
goto LABEL_8;
}
LABEL_5:
v6 = v4;
if ( *((_BYTE *)src + v4) == 47 )
{
LABEL_8:
v8 = malloc(v4 + 2);
memcpy(v8, src, v4 + 1);
*((_BYTE *)v8 + v6) = 47;
*((_BYTE *)v8 + v6 + 1) = 0;
v2 = (char *)src + v6 + 1;
*(_QWORD *)(a2 + 48) = v8;
}
return v2;
}

original_code:
char *
get_path(char *url, Url *u)
{
    int i;
    char *path;
    for(i = strlen(url); i > 0 && url[i] != '/'; i--);
    if(url[i] != '/')
        return url;
    path = (char*)malloc(i + 2);
    memcpy(path, url, i + 1);
    path[i] = '/';
    path[i + 1] = '\0';
    url += i + 1;
    u->path = path;
    return url;
}

canonical_original_code:
char *get_path(char *url, struct _Url *u)
{
    int i;
    char *path;
    for(i = strlen(url); i > 0 && url[i] != '/'; i--);
    if(url[i] != '/')
        return url;
    path = (char *)malloc(i + 2);
    memcpy(path, url, i + 1);
    path[i] = '/';
    path[i + 1] = '\0';
    url += i + 1;
    u->path = path;
    return url;
}

variable_types:
path
char *


decompiled_code:
char *__fastcall get_file(char *s, __int64 a2)
{
char *result; // rax
size_t v3; // rax
void *v4; // r12
size_t v5; // rax

result = 0LL;
if ( *s )
{
v3 = strlen(s);
v4 = malloc(v3 + 1);
v5 = strlen(s);
memcpy(v4, s, v5 + 1);
*(_QWORD *)(a2 + 56) = v4;
result = s;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(char *s, __int64 a2)
{
char *result; // rax
size_t v3; // rax
void *v4; // r12
size_t v5; // rax

result = 0LL;
if ( *s )
{
v3 = strlen(s);
v4 = malloc(v3 + 1);
v5 = strlen(s);
memcpy(v4, s, v5 + 1);
*(_QWORD *)(a2 + 56) = v4;
result = s;
}
return result;
}

original_code:
char *
get_file(char *string, Url *u)
{
  char *file;
  if( !string[0] )
    return ((void *)0);
  file = (char*)malloc(strlen(string) + 1);
  memcpy(file, string, strlen(string) + 1);
  u->file = file;
  return string;
}

canonical_original_code:
char *get_file(char *string, struct _Url *u)
{
  char *file;
  if( !string[0] )
    return ((void *)0);
  file = (char *)malloc(strlen(string) + 1);
  memcpy(file, string, strlen(string) + 1);
  u->file = file;
  return string;
}

variable_types:
file
char *


decompiled_code:
_DWORD *url_new()
{
_DWORD *result; // rax

result = malloc(0x40uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
result[10] = 0;
*((_QWORD *)result + 6) = 0LL;
*((_QWORD *)result + 7) = 0LL;
return result;
}

canonical_decompiled_code:
_DWORD *func8()
{
_DWORD *result; // rax

result = malloc(0x40uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
result[10] = 0;
*((_QWORD *)result + 6) = 0LL;
*((_QWORD *)result + 7) = 0LL;
return result;
}

original_code:
Url *
url_new(void)
{
  Url *new_url;
  new_url = (Url*)malloc(sizeof(Url));
  new_url->full_url = ((void *)0);
  new_url->service_type = 0;
  new_url->username = ((void *)0);
  new_url->password = ((void *)0);
  new_url->host = ((void *)0);
  new_url->port = 0;
  new_url->path = ((void *)0);
  new_url->file = ((void *)0);
  return new_url;
}

canonical_original_code:
struct _Url *url_new()
{
  struct _Url *new_url;
  new_url = (struct _Url *)malloc(sizeof(Url));
  new_url->full_url = ((void *)0);
  new_url->service_type = 0;
  new_url->username = ((void *)0);
  new_url->password = ((void *)0);
  new_url->host = ((void *)0);
  new_url->port = 0;
  new_url->path = ((void *)0);
  new_url->file = ((void *)0);
  return new_url;
}

return_type: struct _Url { char *full_url; int service_type; char *username; char *password; char *host; int port; char *path; char *file; } *


decompiled_code:
void __fastcall url_destroy(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi
void *v7; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
v6 = (void *)*((_QWORD *)ptr + 6);
if ( v6 )
free(v6);
v7 = (void *)*((_QWORD *)ptr + 7);
if ( v7 )
free(v7);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi
void *v7; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
v6 = (void *)*((_QWORD *)ptr + 6);
if ( v6 )
free(v6);
v7 = (void *)*((_QWORD *)ptr + 7);
if ( v7 )
free(v7);
free(ptr);
}
}

original_code:
void
url_destroy(Url *u)
{
    if( u ) {
        if(u->full_url) free(u->full_url);
        if(u->username) free(u->username);
        if(u->password) free(u->password);
        if(u->host) free(u->host);
        if(u->path) free(u->path);
        if(u->file) free(u->file);
        memset(u, 0, sizeof(Url));
        if(u) free(u);
    }
}

canonical_original_code:
void url_destroy(struct _Url *u)
{
    if( u ) {
        if(u->full_url) free(u->full_url);
        if(u->username) free(u->username);
        if(u->password) free(u->password);
        if(u->host) free(u->host);
        if(u->path) free(u->path);
        if(u->file) free(u->file);
        memset(u, 0, sizeof(Url));
        if(u) free(u);
    }
}


decompiled_code:
_QWORD *url_resource_new()
{
_QWORD *result; // rax

result = malloc(0x40uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
*((_BYTE *)result + 40) = 0;
result[6] = 0LL;
result[7] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func10()
{
_QWORD *result; // rax

result = malloc(0x40uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
*((_BYTE *)result + 40) = 0;
result[6] = 0LL;
result[7] = 0LL;
return result;
}

original_code:
UrlResource *
url_resource_new(void)
{
    UrlResource *new_resource;
    new_resource = (UrlResource*)malloc(sizeof(UrlResource));
    new_resource->url = ((void *)0);
    new_resource->outfile = ((void *)0);
    new_resource->proxy = ((void *)0);
    new_resource->proxy_username = ((void *)0);
    new_resource->proxy_password = ((void *)0);
    new_resource->options = 0;
    new_resource->outfile_size = 0;
    new_resource->outfile_offset = 0;
    return new_resource;
}

canonical_original_code:
struct _UrlResource *url_resource_new()
{
    struct _UrlResource *new_resource;
    new_resource = (struct _UrlResource *)malloc(sizeof(UrlResource));
    new_resource->url = ((void *)0);
    new_resource->outfile = ((void *)0);
    new_resource->proxy = ((void *)0);
    new_resource->proxy_username = ((void *)0);
    new_resource->proxy_password = ((void *)0);
    new_resource->options = 0;
    new_resource->outfile_size = 0;
    new_resource->outfile_offset = 0;
    return new_resource;
}

variable_types: struct _UrlResource { struct _Url { char *full_url; int service_type; char *username; char *password; char *host; int port; char *path; char *file; } *url; char *outfile; char *proxy; char *proxy_username; char *proxy_password; unsigned char options; long outfile_size; long outfile_offset; } *

user_defined_types: struct _UrlResource { struct _Url *url; char *outfile; char *proxy; char *proxy_username; char *proxy_password; unsigned char options; long outfile_size; long outfile_offset; }


decompiled_code:
void __fastcall url_resource_destroy(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
url_destroy(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func11(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
func9(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

original_code:
void
url_resource_destroy(UrlResource *rsrc)
{
    if (rsrc) {
        if (rsrc->url) {
            url_destroy(rsrc->url);
            rsrc->url = ((void *)0);
        }
        if(rsrc->outfile) free(rsrc->outfile);
        if(rsrc) free(rsrc);
    }
}

canonical_original_code:
void url_resource_destroy(struct _UrlResource *rsrc)
{
    if (rsrc) {
        if (rsrc->url) {
            url_destroy(rsrc->url);
            rsrc->url = ((void *)0);
        }
        if(rsrc->outfile) free(rsrc->outfile);
        if(rsrc) free(rsrc);
    }
}