decompiled_code:
__int64 __fastcall STEIM_Decode_Small(char *a1, __int64 a2, __int64 a3, __int16 a4, _DWORD *a5, __int16 a6)
{
__int64 v11; // rax
__int64 v12; // r15
__int64 v13; // rax
__int16 v14; // si
__int16 v15; // di
int v16; // edx
__int64 v17; // rbx
__int64 v18; // r12
__int16 v20; // [rsp+6h] [rbp-42h] BYREF
__int16 v21; // [rsp+8h] [rbp-40h] BYREF
__int16 v22; // [rsp+Ah] [rbp-3Eh] BYREF
__int16 v23; // [rsp+Ch] [rbp-3Ch] BYREF
__int16 v24[29]; // [rsp+Eh] [rbp-3Ah] BYREF

v11 = init_generic_decompression();
if ( !v11 )
exit(12);
v12 = v11;
if ( a6 <= 0 )
a6 = 63;
v23 = a4;
v24[0] = a6;
v13 = decode_SEED_micro_header((__int64)a1, &v21, &v23, &v22, v24);
v14 = v23;
v15 = v21;
v16 = v24[0];
v17 = v13;
*a5 = v23;
validate_frames(v15, v14, v16);
v18 = decompress_generic_record(
a1,
a2,
&v20,
v12,
(unsigned int)v21,
v17,
(unsigned int)v23,
(unsigned int)v22,
(unsigned int)v24[0]);
if ( (unsigned int)dferrorfatal((unsigned int)v20, stdout) )
{
printf("\n%c%c%c = %ld\n", (unsigned int)a1[15], (unsigned int)a1[16], (unsigned int)a1[17], v18);
puts("decompress: decompress_SEED_record reports errors!");
exit(3);
}
return v18;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, __int64 a2, __int64 a3, __int16 a4, _DWORD *a5, __int16 a6)
{
__int64 v11; // rax
__int64 v12; // r15
__int64 v13; // rax
__int16 v14; // si
__int16 v15; // di
int v16; // edx
__int64 v17; // rbx
__int64 v18; // r12
__int16 v20; // [rsp+6h] [rbp-42h] BYREF
__int16 v21; // [rsp+8h] [rbp-40h] BYREF
__int16 v22; // [rsp+Ah] [rbp-3Eh] BYREF
__int16 v23; // [rsp+Ch] [rbp-3Ch] BYREF
__int16 v24[29]; // [rsp+Eh] [rbp-3Ah] BYREF

v11 = init_generic_decompression();
if ( !v11 )
exit(12);
v12 = v11;
if ( a6 <= 0 )
a6 = 63;
v23 = a4;
v24[0] = a6;
v13 = func1((__int64)a1, &v21, &v23, &v22, v24);
v14 = v23;
v15 = v21;
v16 = v24[0];
v17 = v13;
*a5 = v23;
func2(v15, v14, v16);
v18 = decompress_generic_record(
a1,
a2,
&v20,
v12,
(unsigned int)v21,
v17,
(unsigned int)v23,
(unsigned int)v22,
(unsigned int)v24[0]);
if ( (unsigned int)dferrorfatal((unsigned int)v20, stdout) )
{
printf("\n%c%c%c = %ld\n", (unsigned int)a1[15], (unsigned int)a1[16], (unsigned int)a1[17], v18);
puts("decompress: decompress_SEED_record reports errors!");
exit(3);
}
return v18;
}

original_code:
long STEIM_Decode_Small(SEED_data_record *sdr, long *udata, int numsams, int steimlevel, int *actuallevel, int frames)
{
  long rectotal, headertotal ;
  short dstat ;
  short firstframe, flip, level ;
  dcptype dcp;
  short dframes ;
  if ((dcp = init_generic_decompression ()) == ((void *)0) )
      exit (12) ;
  level = steimlevel ;
  dframes = frames;
  if (dframes<=0) dframes = 63 ;
  headertotal = decode_SEED_micro_header (sdr, &firstframe, &level, &flip, &dframes) ;
  *actuallevel = level;
  validate_frames (firstframe, level, dframes) ;
  rectotal = decompress_generic_record ((generic_data_record *)sdr, udata, &dstat, dcp,
     firstframe, headertotal, level, flip, dframes) ;
  if (dferrorfatal(dstat, stdout))
    {
      printf ("\n%c%c%c = %ld\n",sdr->Head.channel_ID[0],sdr->Head.channel_ID[1],sdr->Head.channel_ID[2],rectotal);
      printf ("decompress: decompress_SEED_record reports errors!\n") ;
      exit(3) ;
    } ;
  return(rectotal);
}

canonical_original_code:
long STEIM_Decode_Small(struct SEED_data_record *sdr, long *udata, int numsams, int steimlevel, int *actuallevel, int frames)
{
  long rectotal; long headertotal;
  short dstat;
  short firstframe; short flip; short level;
  struct decompression_continuity *dcp;
  short dframes;
  if ((dcp = init_generic_decompression ()) == ((void *)0) )
      exit (12) ;
  level = steimlevel ;
  dframes = frames;
  if (dframes<=0) dframes = 63 ;
  headertotal = decode_SEED_micro_header (sdr, &firstframe, &level, &flip, &dframes) ;
  *actuallevel = level;
  validate_frames (firstframe, level, dframes) ;
  rectotal = decompress_generic_record ((struct generic_data_record *)sdr, udata, &dstat, dcp,
     firstframe, headertotal, level, flip, dframes) ;
  if (dferrorfatal(dstat, stdout))
    {
      printf ("\n%c%c%c = %ld\n",sdr->Head.channel_ID[0],sdr->Head.channel_ID[1],sdr->Head.channel_ID[2],rectotal);
      printf ("decompress: decompress_SEED_record reports errors!\n") ;
      exit(3) ;
    } ;
  return(rectotal);
}

variable_types: struct SEED_data_record { struct SEED_fixed_data_record_header { char sequence[6]; char seed_record_type; char continuation_record; char station_ID_call_letters[5]; char location_id[2]; char channel_ID[3]; char seednet[2]; struct SEED_time_struc { short yr; short jday; char hr; char minute; char seconds; char unused; short tenth_millisec; } starting_time; short samples_in_record; short sample_rate_factor; short sample_rate_multiplier; char activity_flags; char IO_flags; char data_quality_flags; char number_of_following_blockettes; long tenth_msec_correction; short first_data_byte; short first_blockette_byte; struct data_only_blockette { short blockette_type; short next_offset; char encoding_format; char word_order; char rec_length; char dob_reserved; } DOB; struct data_extension_blockette { short blockette_type; short next_offset; char qual; char usec99; char state_of_health; char frame_count; } DEB; } Head; union COMPWORD { char b[4]; short s[2]; long l; } Frames[-1][16]; } *

variable_types: frames int

user_defined_types: struct decompression_continuity { long samp_1; long samp_2; short numdecomp; }


repo: joeledwards/asl-station-processor

decompiled_code:
const char *get_modules()
{
return "Globals";
}

canonical_decompiled_code:
const char *func0()
{
return "Globals";
}

original_code:
pmodules get_modules (void)
{
  return &(sen_modules) ;
}

canonical_original_code:
struct tmodule (*get_modules())[31]
{
  return &(sen_modules) ;
}

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


decompiled_code:
__int64 __fastcall ms_samplesize(char a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = a1 - 97;
result = 0LL;
if ( v1 <= 8u )
result = (unsigned __int8)CSWTCH_1[v1];
return result;
}

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

v1 = a1 - 97;
result = 0LL;
if ( v1 <= 8u )
result = (unsigned __int8)CSWTCH_1[v1];
return result;
}

original_code:
uint8_t
ms_samplesize (const char sampletype)
{
  switch (sampletype)
    {
    case 'a':
      return 1;
    case 'i':
    case 'f':
      return 4;
    case 'd':
      return 8;
    default:
      return 0;
    }
}

canonical_original_code:
unsigned char ms_samplesize(char sampletype)
{
  switch (sampletype)
    {
    case 'a':
      return 1;
    case 'i':
    case 'f':
      return 4;
    case 'd':
      return 8;
    default:
      return 0;
    }
}

variable_types: sampletype char

return_type: unsigned char


decompiled_code:
const char *__fastcall ms_encodingstr(unsigned __int8 a1)
{
const char *result; // rax

result = "Unknown format code";
if ( a1 <= 0x21u )
result = CSWTCH_3[a1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(unsigned __int8 a1)
{
const char *result; // rax

result = "Unknown format code";
if ( a1 <= 0x21u )
result = CSWTCH_3[a1];
return result;
}

original_code:
char *
ms_encodingstr (const char encoding)
{
  switch (encoding)
    {
    case 0:
      return "ASCII text";
    case 1:
      return "16 bit integers";
    case 2:
      return "24 bit integers";
    case 3:
      return "32 bit integers";
    case 4:
      return "IEEE floating point";
    case 5:
      return "IEEE double precision float";
    case 10:
      return "STEIM 1 Compression";
    case 11:
      return "STEIM 2 Compression";
    case 12:
      return "GEOSCOPE Muxed 24 bit int";
    case 13:
      return "GEOSCOPE Muxed 16/3 bit gain/exp";
    case 14:
      return "GEOSCOPE Muxed 16/4 bit gain/exp";
    case 15:
      return "US National Network compression";
    case 16:
      return "CDSN 16 bit gain ranged";
    case 17:
      return "Graefenberg 16 bit gain ranged";
    case 18:
      return "IPG - Strasbourg 16 bit gain";
    case 19:
      return "STEIM 3 Compression";
    case 30:
      return "SRO Gain Ranged Format";
    case 31:
      return "HGLP Format";
    case 32:
      return "DWWSSN Format";
    case 33:
      return "RSTN 16 bit gain ranged";
    default:
      return "Unknown format code";
    }
}

canonical_original_code:
char *ms_encodingstr(char encoding)
{
  switch (encoding)
    {
    case 0:
      return "ASCII text";
    case 1:
      return "16 bit integers";
    case 2:
      return "24 bit integers";
    case 3:
      return "32 bit integers";
    case 4:
      return "IEEE floating point";
    case 5:
      return "IEEE double precision float";
    case 10:
      return "STEIM 1 Compression";
    case 11:
      return "STEIM 2 Compression";
    case 12:
      return "GEOSCOPE Muxed 24 bit int";
    case 13:
      return "GEOSCOPE Muxed 16/3 bit gain/exp";
    case 14:
      return "GEOSCOPE Muxed 16/4 bit gain/exp";
    case 15:
      return "US National Network compression";
    case 16:
      return "CDSN 16 bit gain ranged";
    case 17:
      return "Graefenberg 16 bit gain ranged";
    case 18:
      return "IPG - Strasbourg 16 bit gain";
    case 19:
      return "STEIM 3 Compression";
    case 30:
      return "SRO Gain Ranged Format";
    case 31:
      return "HGLP Format";
    case 32:
      return "DWWSSN Format";
    case 33:
      return "RSTN 16 bit gain ranged";
    default:
      return "Unknown format code";
    }
}

variable_types: encoding char

return_type: char *


decompiled_code:
const char *__fastcall ms_blktdesc(unsigned __int16 a1)
{
const char *result; // rax

if ( a1 == 390 )
return "Generic Calibration";
if ( a1 <= 0x186u )
{
if ( a1 == 300 )
{
result = "Step Calibration";
}
else if ( a1 <= 0x12Cu )
{
result = "Generic Event Detection";
if ( a1 != 200 )
{
if ( a1 == 201 )
{
result = "Murdock Event Detection";
}
else
{
result = "Sample Rate";
if ( a1 != 100 )
result = 0LL;
}
}
}
else
{
result = "Sine Calibration";
if ( a1 != 310 )
{
result = "Pseudo-random Calibration";
if ( a1 != 320 )
result = 0LL;
}
}
}
else if ( a1 == 1000 )
{
result = "Data Only SEED";
}
else if ( a1 <= 0x3E8u )
{
result = "Beam";
if ( a1 != 400 )
{
if ( a1 == 500 )
{
result = "Timing";
}
else
{
result = "Calibration Abort";
if ( a1 != 395 )
result = 0LL;
}
}
}
else
{
result = "Data Extension";
if ( a1 != 1001 )
{
result = "Opaque Data";
if ( a1 != 2000 )
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func2(unsigned __int16 a1)
{
const char *result; // rax

if ( a1 == 390 )
return "Generic Calibration";
if ( a1 <= 0x186u )
{
if ( a1 == 300 )
{
result = "Step Calibration";
}
else if ( a1 <= 0x12Cu )
{
result = "Generic Event Detection";
if ( a1 != 200 )
{
if ( a1 == 201 )
{
result = "Murdock Event Detection";
}
else
{
result = "Sample Rate";
if ( a1 != 100 )
result = 0LL;
}
}
}
else
{
result = "Sine Calibration";
if ( a1 != 310 )
{
result = "Pseudo-random Calibration";
if ( a1 != 320 )
result = 0LL;
}
}
}
else if ( a1 == 1000 )
{
result = "Data Only SEED";
}
else if ( a1 <= 0x3E8u )
{
result = "Beam";
if ( a1 != 400 )
{
if ( a1 == 500 )
{
result = "Timing";
}
else
{
result = "Calibration Abort";
if ( a1 != 395 )
result = 0LL;
}
}
}
else
{
result = "Data Extension";
if ( a1 != 1001 )
{
result = "Opaque Data";
if ( a1 != 2000 )
result = 0LL;
}
}
return result;
}

original_code:
char *
ms_blktdesc (uint16_t blkttype)
{
  switch (blkttype)
    {
    case 100:
      return "Sample Rate";
    case 200:
      return "Generic Event Detection";
    case 201:
      return "Murdock Event Detection";
    case 300:
      return "Step Calibration";
    case 310:
      return "Sine Calibration";
    case 320:
      return "Pseudo-random Calibration";
    case 390:
      return "Generic Calibration";
    case 395:
      return "Calibration Abort";
    case 400:
      return "Beam";
    case 500:
      return "Timing";
    case 1000:
      return "Data Only SEED";
    case 1001:
      return "Data Extension";
    case 2000:
      return "Opaque Data";
    }
  return ((void *)0);
}

canonical_original_code:
char *ms_blktdesc(unsigned short blkttype)
{
  switch (blkttype)
    {
    case 100:
      return "Sample Rate";
    case 200:
      return "Generic Event Detection";
    case 201:
      return "Murdock Event Detection";
    case 300:
      return "Step Calibration";
    case 310:
      return "Sine Calibration";
    case 320:
      return "Pseudo-random Calibration";
    case 390:
      return "Generic Calibration";
    case 395:
      return "Calibration Abort";
    case 400:
      return "Beam";
    case 500:
      return "Timing";
    case 1000:
      return "Data Only SEED";
    case 1001:
      return "Data Extension";
    case 2000:
      return "Opaque Data";
    }
  return ((void *)0);
}

variable_types:
blkttype
unsigned short


decompiled_code:
__int64 __fastcall ms_blktlen(unsigned __int16 a1, __int64 a2, char a3)
{
__int64 result; // rax
unsigned __int16 v4[5]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 == 390 )
return 28LL;
if ( a1 <= 0x186u )
{
if ( a1 == 300 )
{
result = 32LL;
}
else if ( a1 <= 0x12Cu )
{
result = 28LL;
if ( a1 != 200 )
{
if ( a1 == 201 )
{
result = 36LL;
}
else
{
result = 0LL;
if ( a1 == 100 )
result = 12LL;
}
}
}
else
{
result = 32LL;
if ( a1 != 310 )
{
result = 0LL;
if ( a1 == 320 )
result = 28LL;
}
}
}
else
{
result = 8LL;
if ( a1 != 500 )
{
if ( a1 <= 0x1F4u )
{
result = 16LL;
if ( a1 != 395 && a1 != 400 )
result = 0LL;
}
else if ( a1 > 0x3E9u )
{
result = 0LL;
if ( a1 == 2000 && a2 )
{
result = *(unsigned __int16 *)(a2 + 4);
v4[0] = *(_WORD *)(a2 + 4);
if ( a3 )
{
ms_gswap2(v4);
result = v4[0];
}
}
}
else
{
result = 8 * (unsigned int)(a1 > 0x3E7u);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, __int64 a2, char a3)
{
__int64 result; // rax
unsigned __int16 v4[5]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 == 390 )
return 28LL;
if ( a1 <= 0x186u )
{
if ( a1 == 300 )
{
result = 32LL;
}
else if ( a1 <= 0x12Cu )
{
result = 28LL;
if ( a1 != 200 )
{
if ( a1 == 201 )
{
result = 36LL;
}
else
{
result = 0LL;
if ( a1 == 100 )
result = 12LL;
}
}
}
else
{
result = 32LL;
if ( a1 != 310 )
{
result = 0LL;
if ( a1 == 320 )
result = 28LL;
}
}
}
else
{
result = 8LL;
if ( a1 != 500 )
{
if ( a1 <= 0x1F4u )
{
result = 16LL;
if ( a1 != 395 && a1 != 400 )
result = 0LL;
}
else if ( a1 > 0x3E9u )
{
result = 0LL;
if ( a1 == 2000 && a2 )
{
result = *(unsigned __int16 *)(a2 + 4);
v4[0] = *(_WORD *)(a2 + 4);
if ( a3 )
{
ms_gswap2(v4);
result = v4[0];
}
}
}
else
{
result = 8 * (unsigned int)(a1 > 0x3E7u);
}
}
}
return result;
}

original_code:
uint16_t
ms_blktlen (uint16_t blkttype, const char *blkt, flag swapflag)
{
  uint16_t blktlen = 0;
  switch (blkttype)
    {
    case 100:
      blktlen = 12;
      break;
    case 200:
      blktlen = 28;
      break;
    case 201:
      blktlen = 36;
      break;
    case 300:
      blktlen = 32;
      break;
    case 310:
      blktlen = 32;
      break;
    case 320:
      blktlen = 28;
      break;
    case 390:
      blktlen = 28;
      break;
    case 395:
      blktlen = 16;
      break;
    case 400:
      blktlen = 16;
      break;
    case 500:
      blktlen = 8;
      break;
    case 1000:
      blktlen = 8;
      break;
    case 1001:
      blktlen = 8;
      break;
    case 2000:
      if ( blkt )
 {
   memcpy ((void *) &blktlen, blkt+4, sizeof (int16_t));
   if ( swapflag ) ms_gswap2 (&blktlen);
 }
      break;
    }
  return blktlen;
}

canonical_original_code:
unsigned short ms_blktlen(unsigned short blkttype, char *blkt, char swapflag)
{
  unsigned short blktlen = 0;
  switch (blkttype)
    {
    case 100:
      blktlen = 12;
      break;
    case 200:
      blktlen = 28;
      break;
    case 201:
      blktlen = 36;
      break;
    case 300:
      blktlen = 32;
      break;
    case 310:
      blktlen = 32;
      break;
    case 320:
      blktlen = 28;
      break;
    case 390:
      blktlen = 28;
      break;
    case 395:
      blktlen = 16;
      break;
    case 400:
      blktlen = 16;
      break;
    case 500:
      blktlen = 8;
      break;
    case 1000:
      blktlen = 8;
      break;
    case 1001:
      blktlen = 8;
      break;
    case 2000:
      if ( blkt )
 {
   memcpy ((void *) &blktlen, blkt+4, sizeof (short));
   if ( swapflag ) ms_gswap2 (&blktlen);
 }
      break;
    }
  return blktlen;
}

variable_types:
blkt
char *

variable_types: swapflag char

variable_types:
blktlen
unsigned short

return_type: unsigned short


decompiled_code:
char *__fastcall ms_errorstr(int a1)
{
__int64 v1; // rdi
char *result; // rax

v1 = (unsigned int)(a1 + 6);
result = 0LL;
if ( (unsigned int)v1 <= 7 )
result = CSWTCH_7[v1];
return result;
}

canonical_decompiled_code:
char *__fastcall func4(int a1)
{
__int64 v1; // rdi
char *result; // rax

v1 = (unsigned int)(a1 + 6);
result = 0LL;
if ( (unsigned int)v1 <= 7 )
result = CSWTCH_7[v1];
return result;
}

original_code:
char *
ms_errorstr (int errorcode)
{
  switch (errorcode)
    {
    case 1:
      return "End of file reached";
    case 0:
      return "No error";
    case -1:
      return "Generic error";
    case -2:
      return "No SEED data detected";
    case -3:
      return "Length of data read does not match record length";
    case -4:
      return "SEED record length out of range";
    case -5:
      return "Unknown data encoding format";
    case -6:
      return "Bad Steim compression flag(s) detected";
    }
  return ((void *)0);
}

canonical_original_code:
char *ms_errorstr(int errorcode)
{
  switch (errorcode)
    {
    case 1:
      return "End of file reached";
    case 0:
      return "No error";
    case -1:
      return "Generic error";
    case -2:
      return "No SEED data detected";
    case -3:
      return "Length of data read does not match record length";
    case -4:
      return "SEED record length out of range";
    case -5:
      return "Unknown data encoding format";
    case -6:
      return "Bad Steim compression flag(s) detected";
    }
  return ((void *)0);
}

variable_types: errorcode int


decompiled_code:
__int64 __fastcall ST_TimeMin2(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 result; // rax

if ( (int)ST_TimeComp2(a1, a2, a3, a4) <= 0 )
result = a1;
else
result = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 result; // rax

if ( (int)ST_TimeComp2(a1, a2, a3, a4) <= 0 )
result = a1;
else
result = a3;
return result;
}

original_code:
STDTIME2 ST_TimeMin2(STDTIME2 time_arg1, STDTIME2 time_arg2) {
  if (ST_TimeComp2(time_arg1, time_arg2) <= 0)
    return(time_arg1);
  else
    return(time_arg2);
}

canonical_original_code:
struct __sjtim2 ST_TimeMin2(struct __sjtim2 time_arg1, struct __sjtim2 time_arg2) {
  if (ST_TimeComp2(time_arg1, time_arg2) <= 0)
    return(time_arg1);
  else
    return(time_arg2);
}

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


decompiled_code:
__int64 __fastcall ST_TimeMax2(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 result; // rax

if ( (int)ST_TimeComp2(a1, a2, a3, a4) < 0 )
result = a3;
else
result = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 result; // rax

if ( (int)ST_TimeComp2(a1, a2, a3, a4) < 0 )
result = a3;
else
result = a1;
return result;
}

original_code:
STDTIME2 ST_TimeMax2(STDTIME2 time_arg1, STDTIME2 time_arg2) {
  if (ST_TimeComp2(time_arg1, time_arg2) >= 0)
    return(time_arg1);
  else
    return(time_arg2);
}

canonical_original_code:
struct __sjtim2 ST_TimeMax2(struct __sjtim2 time_arg1, struct __sjtim2 time_arg2) {
  if (ST_TimeComp2(time_arg1, time_arg2) >= 0)
    return(time_arg1);
  else
    return(time_arg2);
}

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


decompiled_code:
__int64 __fastcall ST_GetJulian2(int a1)
{
return julday2((__int16)a1, 1LL, SHIWORD(a1));
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return julday2((__int16)a1, 1LL, SHIWORD(a1));
}

original_code:
long ST_GetJulian2(STDTIME2 intime) {
  long jul1;
  jul1 = _julday2(intime.year, 1, intime.day);
  return(jul1);
}

canonical_original_code:
long ST_GetJulian2(struct __sjtim2 intime) {
  long jul1;
  jul1 = _julday2(intime.year, 1, intime.day);
  return(jul1);
}

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

return_type: long


MapStatusMem

decompiled_code:
char *__fastcall MapStatusMem(_QWORD *a1)
{
__int64 v2; // r12
int v3; // edi
void *v4; // rax
char *result; // rax
int *v6; // rbx
char *v7; // rax
int *v8; // rbx
char *v9; // rax

v2 = unixadr_2;
if ( unixadr_2 )
{
*a1 = unixadr_2;
result = 0LL;
}
else
{
v3 = shmget(11, 0x5A80uLL, 944);
if ( v3 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
sprintf(errbuf_1, "shmget SHMKEY_STATUS failure, error %d: %s.", (unsigned int)*v6, v7);
}
else
{
v4 = shmat(v3, 0LL, 48);
unixadr_2 = (__int64)v4;
if ( (_DWORD)v4 != -1 )
{
*a1 = v4;
return (char *)v2;
}
v8 = _errno_location();
v9 = strerror(*v8);
sprintf(errbuf_1, "%s: shmat LOG330 failure, error %d: %s.", "statusutil", (unsigned int)*v8, v9);
}
fprintf(stderr, "%s\n", errbuf_1);
result = errbuf_1;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(_QWORD *a1)
{
__int64 v2; // r12
int v3; // edi
void *v4; // rax
char *result; // rax
int *v6; // rbx
char *v7; // rax
int *v8; // rbx
char *v9; // rax

v2 = unixadr_2;
if ( unixadr_2 )
{
*a1 = unixadr_2;
result = 0LL;
}
else
{
v3 = shmget(11, 0x5A80uLL, 944);
if ( v3 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
sprintf(errbuf_1, "shmget SHMKEY_STATUS failure, error %d: %s.", (unsigned int)*v6, v7);
}
else
{
v4 = shmat(v3, 0LL, 48);
unixadr_2 = (__int64)v4;
if ( (_DWORD)v4 != -1 )
{
*a1 = v4;
return (char *)v2;
}
v8 = _errno_location();
v9 = strerror(*v8);
sprintf(errbuf_1, "%s: shmat LOG330 failure, error %d: %s.", "statusutil", (unsigned int)*v8, v9);
}
fprintf(stderr, "%s\n", errbuf_1);
result = errbuf_1;
}
return result;
}

original_code:
char *MapStatusMem(void **mapstatus)
{
   int shmid;
   static char *unixadr = ((void *)0);
   static char errbuf[1024];
   if (unixadr != ((void *)0) && unixadr != 0)
   {
     *mapstatus = unixadr;
     return ((void *)0);
   }
   if ((shmid = shmget(11, sizeof (struct s_mapstatus),
         01000 | 00660))==-1)
   {
      sprintf(errbuf, "shmget SHMKEY_STATUS failure, error %d: %s.",
            (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
   if ((int)(unixadr = (char *)shmat(shmid,0,060))==-1)
   {
      sprintf(errbuf, "%s: shmat LOG330 failure, error %d: %s.",
            "statusutil", (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
  *mapstatus = unixadr;
  return ((void *)0);
}

variable_types:
mapstatus
void **

variable_types: shmid int

variable_types:
unixadr
char *

variable_types:
errbuf
char [1024]

user_defined_types: struct s_mapstatus { int iNumDlg; enum tlibstate libstate[8]; int ixWriteData[8]; int ixReadData[8]; int ixWriteStatus[8]; int ixReadStatus[8]; struct s_dlgstatus dlg[3][8]; int ixWriteArch; int ixReadArch; struct __sjtim2 archWatchdog[3]; int ixWriteServ[8]; int ixReadServ[8]; struct __sjtim2 servWatchdog[3][8]; }

user_defined_types: enum tlibstate { LIBSTATE_IDLE=0, LIBSTATE_TERM=1, LIBSTATE_PING=2, LIBSTATE_CONN=3, LIBSTATE_ANNC=4, LIBSTATE_REG=5, LIBSTATE_READCFG=6, LIBSTATE_READTOK=7, LIBSTATE_DECTOK=8, LIBSTATE_RUNWAIT=9, LIBSTATE_RUN=10, LIBSTATE_DEALLOC=11, LIBSTATE_DEREG=12, LIBSTATE_WAIT=13 }

user_defined_types: struct s_dlgstatus { struct __sjtim2 timeLastData; unsigned long property_tag; int sys_temp; float analog_voltage; float input_voltage; int main_cur; int boom_pos[6]; struct tstat_gps stat_gps; int pll_state; float pll_time_error; int clock_quality; long vacuum[3]; long pressure; long dummyl[94]; }

user_defined_types: struct tstat_gps { unsigned short gpstime; unsigned short gpson; unsigned short sat_used; unsigned short sat_view; char time[10]; char date[12]; char fix[6]; char height[12]; char lat[14]; char longt[14]; unsigned int last_good; unsigned int check_err; }


GetDlgIndex

original_code:
int GetDlgIndex(const char *configstr)
{
  int iDig;
  int i;
  static int rememberdig=-2;
  if (rememberdig > -2)
    return rememberdig;
  iDig = 0;
  rememberdig = 0;
  for (i=0; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "etc", 3) != 0)
    rememberdig;
  i += 3;
  if (configstr[i] != '/') return rememberdig;
  for (; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "q330", 4) != 0)
    return rememberdig;
  i += 4;
  if (configstr[i] != '/') return rememberdig;
  for (; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "DLG", 3) != 0)
    return rememberdig;
  i += 3;
  if (!((*__ctype_b_loc ())[(int) ((configstr[i]))] & (unsigned short int) _ISdigit))
    return rememberdig;
  iDig = configstr[i] - '0';
  iDig = iDig-1;
  if (iDig < 0) iDig = 0;
  rememberdig = iDig;
  return rememberdig;
}

canonical_original_code:
int GetDlgIndex(char *configstr)
{
  int iDig;
  int i;
  static int rememberdig = -2;
  if (rememberdig > -2)
    return rememberdig;
  iDig = 0;
  rememberdig = 0;
  for (i=0; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "etc", 3) != 0)
    rememberdig;
  i += 3;
  if (configstr[i] != '/') return rememberdig;
  for (; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "q330", 4) != 0)
    return rememberdig;
  i += 4;
  if (configstr[i] != '/') return rememberdig;
  for (; configstr[i] == '/'; i++)
    ;
  if (strncmp(&configstr[i], "DLG", 3) != 0)
    return rememberdig;
  i += 3;
  if (!((*__ctype_b_loc ())[(int) ((configstr[i]))] & (unsigned short) _ISdigit))
    return rememberdig;
  iDig = configstr[i] - '0';
  iDig = iDig-1;
  if (iDig < 0) iDig = 0;
  rememberdig = iDig;
  return rememberdig;
}

variable_types:
configstr
char *

variable_types: iDig int

variable_types: i int

variable_types: rememberdig int

return_type: int


ServerReadThread

decompiled_code:
void __fastcall __noreturn ServerReadThread(void *a1)
{
__int64 v1; // rbx
_DWORD *v2; // rax
int v3; // ebp
__int64 i; // rdx
__int64 v5; // rax
int v6; // edx
int v7; // er13
int v8; // er15
int v9; // eax
__int64 v10; // rsi
int v11; // eax
size_t v12; // rax
__int64 j; // [rsp+0h] [rbp-C8h]
char s[16]; // [rsp+10h] [rbp-B8h] BYREF
__int64 v15; // [rsp+20h] [rbp-A8h]

v1 = *(int *)a1;
v2 = (_DWORD *)*((_QWORD *)a1 + 1);
v3 = *((_DWORD *)a1 + 1);
mapshm = (__int64)v2;
for ( i = *(_QWORD *)&v2[2 * v1 + 70]; !i; i = *(_QWORD *)(mapshm + 8 * (v1 + 34) + 8) )
{
sleep(1u);
v2 = (_DWORD *)mapshm;
}
if ( *v2 )
printf("New read connection %d, thread id=%lu\n", (unsigned int)v1, i);
else
syslog(6, "New read connection %d, thread id=%lu\n", (unsigned int)v1, i);
for ( j = 2 * v1; ; send(v3, (const void *)(v10 + ((j + v7) << 13) + 1053), 1uLL, 0) )
{
while ( 1 )
{
v5 = mapshm;
v6 = iSeedRecordSize;
v7 = 1 - *(_DWORD *)(mapshm + 4 * v1 + 536);
if ( iSeedRecordSize > 0 )
{
v8 = 0;
do
{
v9 = read(v3, (void *)(v5 + ((j + v7) << 13) + 1048 + v8), v6 - v8);
if ( !v9 )
{
if ( *(_DWORD *)mapshm )
printf(
"Closing client read thread %d, thread id %lu\n",
(unsigned int)v1,
*(_QWORD *)(mapshm + 8 * v1 + 280));
else
syslog(
6,
"Closing client connection %d, thread id=%lu\n",
(unsigned int)v1,
*(_QWORD *)(mapshm + 8 * v1 + 280));
*(_QWORD *)(mapshm + 8 * v1 + 280) = 0LL;
close(v3);
pthread_exit(0LL);
}
v6 = iSeedRecordSize;
v8 += v9;
v5 = mapshm;
}
while ( iSeedRecordSize > v8 );
}
v10 = v5;
if ( *(_DWORD *)(v5 + 4 * v1 + 536) != *(_DWORD *)(v5 + 4 * v1 + 664) )
{
do
{
usleep(0x64u);
v10 = mapshm;
}
while ( *(_DWORD *)(mapshm + 4 * (v1 + 132) + 8) != *(_DWORD *)(mapshm + 4 * v1 + 664) );
}
*(_DWORD *)(v10 + 4 * (v1 + 132) + 8) = v7;
*(_QWORD *)(v10 + 8 * v1 + 24) = time(0LL);
if ( *(_DWORD *)(v10 + 4 * (v1 + 132) + 8) != *(_DWORD *)(v10 + 4 * v1 + 664) )
{
do
{
usleep(0x64u);
v10 = mapshm;
}
while ( *(_DWORD *)(mapshm + 4 * (v1 + 132) + 8) != *(_DWORD *)(mapshm + 4 * v1 + 664) );
}
v11 = *(_DWORD *)(v10 + 8 * v1 + 796);
if ( v11 == 2 )
{
sprintf(s, "CHANNELCONTROL-%d-FAIL.", *(unsigned int *)(v10 + 8 * v1 + 792));
goto LABEL_24;
}
if ( v11 != 3 )
break;
v15 = 0x2E44494C41564ELL;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC5);
LABEL_24:
v12 = strlen(s);
send(v3, s, v12 + 1, 0);
}
if ( v11 == 1 )
{
sprintf(s, "CHANNELCONTROL-%d-OKAY.", *(unsigned int *)(v10 + 8 * v1 + 792));
goto LABEL_24;
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
__int64 v1; // rbx
_DWORD *v2; // rax
int v3; // ebp
__int64 i; // rdx
__int64 v5; // rax
int v6; // edx
int v7; // er13
int v8; // er15
int v9; // eax
__int64 v10; // rsi
int v11; // eax
size_t v12; // rax
__int64 j; // [rsp+0h] [rbp-C8h]
char s[16]; // [rsp+10h] [rbp-B8h] BYREF
__int64 v15; // [rsp+20h] [rbp-A8h]

v1 = *(int *)a1;
v2 = (_DWORD *)*((_QWORD *)a1 + 1);
v3 = *((_DWORD *)a1 + 1);
mapshm = (__int64)v2;
for ( i = *(_QWORD *)&v2[2 * v1 + 70]; !i; i = *(_QWORD *)(mapshm + 8 * (v1 + 34) + 8) )
{
sleep(1u);
v2 = (_DWORD *)mapshm;
}
if ( *v2 )
printf("New read connection %d, thread id=%lu\n", (unsigned int)v1, i);
else
syslog(6, "New read connection %d, thread id=%lu\n", (unsigned int)v1, i);
for ( j = 2 * v1; ; send(v3, (const void *)(v10 + ((j + v7) << 13) + 1053), 1uLL, 0) )
{
while ( 1 )
{
v5 = mapshm;
v6 = iSeedRecordSize;
v7 = 1 - *(_DWORD *)(mapshm + 4 * v1 + 536);
if ( iSeedRecordSize > 0 )
{
v8 = 0;
do
{
v9 = read(v3, (void *)(v5 + ((j + v7) << 13) + 1048 + v8), v6 - v8);
if ( !v9 )
{
if ( *(_DWORD *)mapshm )
printf(
"Closing client read thread %d, thread id %lu\n",
(unsigned int)v1,
*(_QWORD *)(mapshm + 8 * v1 + 280));
else
syslog(
6,
"Closing client connection %d, thread id=%lu\n",
(unsigned int)v1,
*(_QWORD *)(mapshm + 8 * v1 + 280));
*(_QWORD *)(mapshm + 8 * v1 + 280) = 0LL;
close(v3);
pthread_exit(0LL);
}
v6 = iSeedRecordSize;
v8 += v9;
v5 = mapshm;
}
while ( iSeedRecordSize > v8 );
}
v10 = v5;
if ( *(_DWORD *)(v5 + 4 * v1 + 536) != *(_DWORD *)(v5 + 4 * v1 + 664) )
{
do
{
usleep(0x64u);
v10 = mapshm;
}
while ( *(_DWORD *)(mapshm + 4 * (v1 + 132) + 8) != *(_DWORD *)(mapshm + 4 * v1 + 664) );
}
*(_DWORD *)(v10 + 4 * (v1 + 132) + 8) = v7;
*(_QWORD *)(v10 + 8 * v1 + 24) = time(0LL);
if ( *(_DWORD *)(v10 + 4 * (v1 + 132) + 8) != *(_DWORD *)(v10 + 4 * v1 + 664) )
{
do
{
usleep(0x64u);
v10 = mapshm;
}
while ( *(_DWORD *)(mapshm + 4 * (v1 + 132) + 8) != *(_DWORD *)(mapshm + 4 * v1 + 664) );
}
v11 = *(_DWORD *)(v10 + 8 * v1 + 796);
if ( v11 == 2 )
{
sprintf(s, "CHANNELCONTROL-%d-FAIL.", *(unsigned int *)(v10 + 8 * v1 + 792));
goto LABEL_24;
}
if ( v11 != 3 )
break;
v15 = 0x2E44494C41564ELL;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC5);
LABEL_24:
v12 = strlen(s);
send(v3, s, v12 + 1, 0);
}
if ( v11 == 1 )
{
sprintf(s, "CHANNELCONTROL-%d-OKAY.", *(unsigned int *)(v10 + 8 * v1 + 792));
goto LABEL_24;
}
}
}

original_code:
void *ServerReadThread(void *params)
{
  int iClient;
  int iSocket;
  int iCount;
  int j;
  int iBuf;
  struct s_readthread *thread_args;
  char msgBuf[128];
  thread_args = (struct s_readthread *) params;
  iClient = thread_args->iClient;
  iSocket = thread_args->iSocket;
  mapshm = (struct s_mapshm *) thread_args->mapshm;
  while (mapshm->client_tid[iClient] == 0)
    sleep(1);
  if (mapshm->bDebug)
  {
    printf("New read connection %d, thread id=%lu\n",
        iClient, mapshm->client_tid[iClient]);
  }
  else
  {
    syslog(6, "New read connection %d, thread id=%lu\n",
        iClient, mapshm->client_tid[iClient]);
  }
  while (1)
  {
    iCount = 0;
    iBuf = 1 - mapshm->read_index[iClient];
    while (iCount < iSeedRecordSize)
    {
      j = read(iSocket, &mapshm->buffer[iClient][iBuf][iCount],
              iSeedRecordSize-iCount);
      if (j == 0)
      {
        if (mapshm->bDebug)
        {
          printf("Closing client read thread %d, thread id %lu\n",
              iClient, mapshm->client_tid[iClient]);
        }
        else
        {
          syslog(6, "Closing client connection %d, thread id=%lu\n",
              iClient, mapshm->client_tid[iClient]);
        }
        mapshm->client_tid[iClient] = 0;
        close(iSocket);
        pthread_exit(((void *)0));
      }
      iCount = iCount + j;
    }
    while (mapshm->read_index[iClient] != mapshm->write_index[iClient])
    {
      usleep(100);
    }
    mapshm->read_index[iClient] = iBuf;
    mapshm->timetag[iClient] = time(((void *)0));
    while (mapshm->read_index[iClient] != mapshm->write_index[iClient])
    {
      usleep(100);
    }
    switch (mapshm->result[iClient][1]) {
      case 1:
        sprintf(msgBuf, "CHANNELCONTROL-%d-OKAY.", mapshm->result[iClient][0]);
        send(iSocket, msgBuf, strlen(msgBuf)+1, 0);
        break;
      case 2:
        sprintf(msgBuf, "CHANNELCONTROL-%d-FAIL.", mapshm->result[iClient][0]);
        send(iSocket, msgBuf, strlen(msgBuf)+1, 0);
        break;
      case 3:
        sprintf(msgBuf, "CHANNELCONTROL-INVALID.");
        send(iSocket, msgBuf, strlen(msgBuf)+1, 0);
        break;
      default:
        send(iSocket, &mapshm->buffer[iClient][iBuf][5], 1, 0);
    }
  }
}

variable_types:
params
void *

variable_types: iClient int

variable_types: iSocket int

variable_types: iCount int

variable_types: j int

variable_types: iBuf int

variable_types: struct s_readthread { int iClient; int iSocket; struct s_mapshm { int bDebug; int bQuit; int iPort; unsigned long listen_tid; long timetag[32]; unsigned long client_tid[32]; int read_index[32]; int write_index[32]; int result[32][2]; char buffer[32][2][8192]; } *mapshm; } *

variable_types:
msgBuf
char [128]

return_type: void *

user_defined_types: struct s_readthread { int iClient; int iSocket; struct s_mapshm *mapshm; }


MapSharedMem

decompiled_code:
char *__fastcall MapSharedMem(_QWORD *a1)
{
__int64 v2; // r12
int v3; // edi
void *v4; // rax
int *v6; // rbx
char *v7; // rax
int *v8; // rbx
char *v9; // rax

v2 = unixadr_1;
if ( unixadr_1 )
{
*a1 = unixadr_1;
pthread_exit(0LL);
}
v3 = shmget(0, 0x80418uLL, 950);
if ( v3 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
sprintf(errbuf_0, "shmget Q330ARCH failure, error %d: %s.", (unsigned int)*v6, v7);
}
else
{
v4 = shmat(v3, 0LL, 384);
unixadr_1 = (__int64)v4;
if ( (_DWORD)v4 != -1 )
{
*a1 = v4;
return (char *)v2;
}
v8 = _errno_location();
v9 = strerror(*v8);
sprintf(errbuf_0, "%s: shmat Q330ARCH failure, error %d: %s.", "q330arch", (unsigned int)*v8, v9);
}
fprintf(stderr, "%s\n", errbuf_0);
return errbuf_0;
}

canonical_decompiled_code:
char *__fastcall func3(_QWORD *a1)
{
__int64 v2; // r12
int v3; // edi
void *v4; // rax
int *v6; // rbx
char *v7; // rax
int *v8; // rbx
char *v9; // rax

v2 = unixadr_1;
if ( unixadr_1 )
{
*a1 = unixadr_1;
pthread_exit(0LL);
}
v3 = shmget(0, 0x80418uLL, 950);
if ( v3 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
sprintf(errbuf_0, "shmget Q330ARCH failure, error %d: %s.", (unsigned int)*v6, v7);
}
else
{
v4 = shmat(v3, 0LL, 384);
unixadr_1 = (__int64)v4;
if ( (_DWORD)v4 != -1 )
{
*a1 = v4;
return (char *)v2;
}
v8 = _errno_location();
v9 = strerror(*v8);
sprintf(errbuf_0, "%s: shmat Q330ARCH failure, error %d: %s.", "q330arch", (unsigned int)*v8, v9);
}
fprintf(stderr, "%s\n", errbuf_0);
return errbuf_0;
}

original_code:
char *MapSharedMem(void **mapshm)
{
   int shmid;
   static char *unixadr = ((void *)0);
   static char errbuf[1024];
   if (unixadr != ((void *)0) && unixadr != 0)
   {
     *mapshm = unixadr;
     pthread_exit(((void *)0));
   }
   if ((shmid = shmget(((__key_t) 0), sizeof (struct s_mapshm),
         01000 | 00666))==-1)
   {
      sprintf(errbuf, "shmget Q330ARCH failure, error %d: %s.",
            (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
   if ((int)(unixadr = (char *)shmat(shmid,0,0400 | 0200))==-1)
   {
      sprintf(errbuf, "%s: shmat Q330ARCH failure, error %d: %s.",
            "q330arch", (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
  *mapshm = unixadr;
  return ((void *)0);
}

canonical_original_code:
char *MapSharedMem(void **mapshm)
{
   int shmid;
   static char *unixadr = ((void *)0);
   static char errbuf[1024];
   if (unixadr != ((void *)0) && unixadr != 0)
   {
     *mapshm = unixadr;
     pthread_exit(((void *)0));
   }
   if ((shmid = shmget(((int) 0), sizeof (struct s_mapshm),
         01000 | 00666))==-1)
   {
      sprintf(errbuf, "shmget Q330ARCH failure, error %d: %s.",
            (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
   if ((int)(unixadr = (char *)shmat(shmid,0,0400 | 0200))==-1)
   {
      sprintf(errbuf, "%s: shmat Q330ARCH failure, error %d: %s.",
            "q330arch", (*__errno_location ()), strerror((*__errno_location ())));
      fprintf(stderr, "%s\n", errbuf);
      return errbuf;
   }
  *mapshm = unixadr;
  return ((void *)0);
}

variable_types:
mapshm
void **

user_defined_types: struct s_mapshm { int bDebug; int bQuit; int iPort; unsigned long listen_tid; long timetag[32]; unsigned long client_tid[32]; int read_index[32]; int write_index[32]; int result[32][2]; char buffer[32][2][8192]; }


StartServer

decompiled_code:
void __fastcall __noreturn StartServer(unsigned int *a1)
{
const char *v2; // rax
__int64 v3; // rdx
int v4; // er12
int *v5; // rax
unsigned int *v6; // r13
int v7; // eax
__int16 v8; // di
char *v9; // rax
__int64 v10; // rdx
bool v11; // zf
int v12; // eax
unsigned int v13; // er14
__int64 v14; // rax
int v15; // ebp
pthread_t v16; // rdx
char *v17; // rax
char *v18; // rax
socklen_t addr_len; // [rsp+4h] [rbp-74h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-70h] BYREF
int arg[2]; // [rsp+10h] [rbp-68h] BYREF
unsigned int *v22; // [rsp+18h] [rbp-60h]
struct sockaddr addr; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr v24; // [rsp+30h] [rbp-48h] BYREF

v2 = (const char *)LoopRecordSize(&iSeedRecordSize);
if ( v2 )
{
fprintf(stderr, "%s\n", v2);
a1[1] = 1;
pthread_exit(0LL);
}
v3 = *a1;
if ( (_DWORD)v3 )
printf("StartServer(port=%d, debug=%d) size %d\n", a1[2], v3, (unsigned int)iSeedRecordSize);
*((_QWORD *)a1 + 35) = 0LL;
*((_QWORD *)a1 + 66) = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 72) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 288) & 0xFFFFFFF8) + 536) >> 3));
v4 = socket(2, 1, 0);
v5 = _errno_location();
v6 = (unsigned int *)v5;
if ( v4 < 0 )
{
fprintf(stderr, "StartServer: Cannot open socket (err %d,%d)\n", (unsigned int)*v5, (unsigned int)v4);
}
else
{
v7 = _bswap_32_constprop_0();
v8 = *((_WORD *)a1 + 4);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = v7;
*(_WORD *)addr.sa_data = _bswap_16(v8);
while ( bind(v4, &addr, 0x10u) < 0 )
{
if ( *v6 != 98 )
{
v9 = strerror(*v6);
fprintf(stderr, "Cannot bind (err %d - %s)\n", *v6, v9);
a1[1] = 1;
pthread_exit(0LL);
}
v10 = a1[2];
if ( *a1 )
fprintf(stderr, "Port %d in use? Trying again in 15 sec\n", v10);
else
syslog(6, "Port %d in use? Trying again in 15 sec", v10);
sleep(0xFu);
}
if ( listen(v4, 32) >= 0 )
{
while ( 1 )
{
v11 = *a1 == 0;
addr_len = 16;
if ( !v11 )
printf("Waiting for connection on port %d\n", a1[2]);
v12 = accept(v4, &v24, &addr_len);
v13 = v12;
if ( v12 < 0 )
{
fprintf(stderr, "accept failed (%d,%d)\n", *v6, (unsigned int)v12);
sleep(0xAu);
}
else if ( getpeername(v12, &v24, &addr_len) < 0 )
{
v17 = strerror(*v6);
fprintf(stderr, "BAD ERROR encountered - error %d - %s\n", *v6, v17);
close(v13);
sleep(0x14u);
}
else
{
v14 = 0LL;
v15 = -1;
do
{
if ( !*(_QWORD *)&a1[2 * v14 + 70] )
v15 = v14;
++v14;
}
while ( v14 != 32 );
if ( v15 == -1 )
{
close(v13);
if ( *a1 )
puts("All client connections are used up, waiting for one to free up...");
sleep(0x3Cu);
}
else
{
v11 = *a1 == 0;
arg[1] = v13;
*(_QWORD *)&a1[2 * v15 + 6] = 0LL;
arg[0] = v15;
a1[v15 + 134] = 0;
a1[v15 + 166] = 0;
v22 = a1;
if ( !v11 )
printf("q330serv connected and attached to client handler %d, socket %d\n", (unsigned int)v15, v13);
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))ServerReadThread, arg) )
{
v18 = strerror(*v6);
fprintf(stderr, "%s:StartServer pthread_create ServerReadThread: %s\n", "q330arch", v18);
a1[1] = 1;
pthread_exit(0LL);
}
pthread_detach(newthread);
v16 = newthread;
v11 = *a1 == 0;
*(_QWORD *)&a1[2 * v15 + 70] = newthread;
if ( !v11 )
printf("New connectionon, started read client %d, thread id %lud\n", (unsigned int)v15, v16);
sleep(2u);
}
}
}
}
fprintf(stderr, "dnetport: cannot set listen depth to %d (%d)\n", 32LL, *v6);
}
a1[1] = 1;
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(unsigned int *a1)
{
const char *v2; // rax
__int64 v3; // rdx
int v4; // er12
int *v5; // rax
unsigned int *v6; // r13
int v7; // eax
__int16 v8; // di
char *v9; // rax
__int64 v10; // rdx
bool v11; // zf
int v12; // eax
unsigned int v13; // er14
__int64 v14; // rax
int v15; // ebp
pthread_t v16; // rdx
char *v17; // rax
char *v18; // rax
socklen_t addr_len; // [rsp+4h] [rbp-74h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-70h] BYREF
int arg[2]; // [rsp+10h] [rbp-68h] BYREF
unsigned int *v22; // [rsp+18h] [rbp-60h]
struct sockaddr addr; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr v24; // [rsp+30h] [rbp-48h] BYREF

v2 = (const char *)LoopRecordSize(&iSeedRecordSize);
if ( v2 )
{
fprintf(stderr, "%s\n", v2);
a1[1] = 1;
pthread_exit(0LL);
}
v3 = *a1;
if ( (_DWORD)v3 )
printf("StartServer(port=%d, debug=%d) size %d\n", a1[2], v3, (unsigned int)iSeedRecordSize);
*((_QWORD *)a1 + 35) = 0LL;
*((_QWORD *)a1 + 66) = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 72) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 288) & 0xFFFFFFF8) + 536) >> 3));
v4 = socket(2, 1, 0);
v5 = _errno_location();
v6 = (unsigned int *)v5;
if ( v4 < 0 )
{
fprintf(stderr, "StartServer: Cannot open socket (err %d,%d)\n", (unsigned int)*v5, (unsigned int)v4);
}
else
{
v7 = func2();
v8 = *((_WORD *)a1 + 4);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = v7;
*(_WORD *)addr.sa_data = func1(v8);
while ( bind(v4, &addr, 0x10u) < 0 )
{
if ( *v6 != 98 )
{
v9 = strerror(*v6);
fprintf(stderr, "Cannot bind (err %d - %s)\n", *v6, v9);
a1[1] = 1;
pthread_exit(0LL);
}
v10 = a1[2];
if ( *a1 )
fprintf(stderr, "Port %d in use? Trying again in 15 sec\n", v10);
else
syslog(6, "Port %d in use? Trying again in 15 sec", v10);
sleep(0xFu);
}
if ( listen(v4, 32) >= 0 )
{
while ( 1 )
{
v11 = *a1 == 0;
addr_len = 16;
if ( !v11 )
printf("Waiting for connection on port %d\n", a1[2]);
v12 = accept(v4, &v24, &addr_len);
v13 = v12;
if ( v12 < 0 )
{
fprintf(stderr, "accept failed (%d,%d)\n", *v6, (unsigned int)v12);
sleep(0xAu);
}
else if ( getpeername(v12, &v24, &addr_len) < 0 )
{
v17 = strerror(*v6);
fprintf(stderr, "BAD ERROR encountered - error %d - %s\n", *v6, v17);
close(v13);
sleep(0x14u);
}
else
{
v14 = 0LL;
v15 = -1;
do
{
if ( !*(_QWORD *)&a1[2 * v14 + 70] )
v15 = v14;
++v14;
}
while ( v14 != 32 );
if ( v15 == -1 )
{
close(v13);
if ( *a1 )
puts("All client connections are used up, waiting for one to free up...");
sleep(0x3Cu);
}
else
{
v11 = *a1 == 0;
arg[1] = v13;
*(_QWORD *)&a1[2 * v15 + 6] = 0LL;
arg[0] = v15;
a1[v15 + 134] = 0;
a1[v15 + 166] = 0;
v22 = a1;
if ( !v11 )
printf("q330serv connected and attached to client handler %d, socket %d\n", (unsigned int)v15, v13);
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))ServerReadThread, arg) )
{
v18 = strerror(*v6);
fprintf(stderr, "%s:StartServer pthread_create ServerReadThread: %s\n", "q330arch", v18);
a1[1] = 1;
pthread_exit(0LL);
}
pthread_detach(newthread);
v16 = newthread;
v11 = *a1 == 0;
*(_QWORD *)&a1[2 * v15 + 70] = newthread;
if ( !v11 )
printf("New connectionon, started read client %d, thread id %lud\n", (unsigned int)v15, v16);
sleep(2u);
}
}
}
}
fprintf(stderr, "dnetport: cannot set listen depth to %d (%d)\n", 32LL, *v6);
}
a1[1] = 1;
pthread_exit(0LL);
}

original_code:
void *StartServer(void *params)
{
  int listen_socket;
  int i;
  char *errstr;
  struct s_readthread read_params;
  struct s_mapshm *mapshm;
  struct sockaddr_in listen_addr;
  mapshm = (struct s_mapshm *) params;
  if ((errstr=LoopRecordSize(&iSeedRecordSize)) != ((void *)0))
  {
    fprintf(stderr, "%s\n", errstr);
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  if (mapshm->bDebug)
    printf("StartServer(port=%d, debug=%d) size %d\n",
        mapshm->iPort, mapshm->bDebug, iSeedRecordSize);
  for (i=0; i<32; i++)
    mapshm->client_tid[i] = 0;
  listen_socket = socket(2, SOCK_STREAM, 0);
  if (listen_socket<0)
  {
    fprintf(stderr,"StartServer: Cannot open socket (err %d,%d)\n",
     (*__errno_location ()),listen_socket);
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  listen_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x7f000001));
  listen_addr.sin_family = 2;
  listen_addr.sin_port = htons(mapshm->iPort);
  while (1)
  {
    if (bind(listen_socket, (struct sockaddr *) &listen_addr,
      sizeof(listen_addr)) < 0)
    {
      if ((*__errno_location ())==98)
      {
        if (mapshm->bDebug)
          fprintf(stderr,"Port %d in use? Trying again in 15 sec\n",
                  mapshm->iPort);
        else
          syslog(6,"Port %d in use? Trying again in 15 sec",
                 mapshm->iPort);
        sleep(15);
        continue;
      }
      fprintf(stderr,"Cannot bind (err %d - %s)\n",(*__errno_location ()),strerror((*__errno_location ())));
      mapshm->bQuit = 1;
      pthread_exit(((void *)0));
    }
    break;
  }
  if (listen(listen_socket, 32) < 0) {
    fprintf(stderr,"dnetport: cannot set listen depth to %d (%d)\n",
     32,(*__errno_location ()));
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  while(1)
  {
    int client_sock;
    struct sockaddr_in addr;
    unsigned int addr_len = sizeof(struct sockaddr_in);
    int client;
    pthread_t child_tid;
    if (mapshm->bDebug)
    {
      printf("Waiting for connection on port %d\n", mapshm->iPort);
    }
    client_sock = accept(listen_socket, (struct sockaddr *) &addr, &addr_len);
    if (client_sock<0)
    {
      fprintf(stderr,"accept failed (%d,%d)\n",
       (*__errno_location ()),client_sock);
      sleep(10);
      continue;
    }
    if (getpeername(client_sock,
       (struct sockaddr *) &addr,
       &addr_len) < 0) {
      fprintf(stderr,"BAD ERROR encountered - error %d - %s\n",
       (*__errno_location ()),
       strerror((*__errno_location ())));
      close(client_sock);
      sleep(20);
      continue;
    }
    client = -1;
    for (i=0; i<32; i++)
      if (mapshm->client_tid[i] == 0) client = i;
    if (client<0)
    {
      close(client_sock);
      if (mapshm->bDebug)
      {
        printf("All client connections are used up, waiting for one to free up...\n");
      }
      sleep(60);
      continue;
    }
    mapshm->timetag[client] = 0;
    mapshm->read_index[client] = 0;
    mapshm->write_index[client] = 0;
    read_params.iSocket = client_sock;
    read_params.iClient = client;
    read_params.mapshm = mapshm;
    if (mapshm->bDebug)
    {
      printf("q330serv connected and attached to client handler %d, socket %d\n",
        read_params.iClient, read_params.iSocket);
    }
    if (pthread_create(&child_tid, ((void *)0), ServerReadThread, &read_params))
    {
      fprintf(stderr, "%s:StartServer pthread_create ServerReadThread: %s\n",
        "q330arch", strerror((*__errno_location ())));
      mapshm->bQuit = 1;
      pthread_exit(((void *)0));
    }
    pthread_detach(child_tid);
    mapshm->client_tid[client] = child_tid;
    if (mapshm->bDebug)
    {
      printf("New connectionon, started read client %d, thread id %lud\n",
         client, child_tid);
    }
    sleep(2);
  }
}

canonical_original_code:
void *StartServer(void *params)
{
  int listen_socket;
  int i;
  char *errstr;
  struct s_readthread read_params;
  struct s_mapshm *mapshm;
  struct sockaddr_in listen_addr;
  mapshm = (struct s_mapshm *) params;
  if ((errstr=LoopRecordSize(&iSeedRecordSize)) != ((void *)0))
  {
    fprintf(stderr, "%s\n", errstr);
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  if (mapshm->bDebug)
    printf("StartServer(port=%d, debug=%d) size %d\n",
        mapshm->iPort, mapshm->bDebug, iSeedRecordSize);
  for (i=0; i<32; i++)
    mapshm->client_tid[i] = 0;
  listen_socket = socket(2, SOCK_STREAM, 0);
  if (listen_socket<0)
  {
    fprintf(stderr,"StartServer: Cannot open socket (err %d,%d)\n",
     (*__errno_location ()),listen_socket);
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  listen_addr.sin_addr.s_addr = htonl(((unsigned int) 0x7f000001));
  listen_addr.sin_family = 2;
  listen_addr.sin_port = htons(mapshm->iPort);
  while (1)
  {
    if (bind(listen_socket, (struct sockaddr *) &listen_addr,
      sizeof(listen_addr)) < 0)
    {
      if ((*__errno_location ())==98)
      {
        if (mapshm->bDebug)
          fprintf(stderr,"Port %d in use? Trying again in 15 sec\n",
                  mapshm->iPort);
        else
          syslog(6,"Port %d in use? Trying again in 15 sec",
                 mapshm->iPort);
        sleep(15);
        continue;
      }
      fprintf(stderr,"Cannot bind (err %d - %s)\n",(*__errno_location ()),strerror((*__errno_location ())));
      mapshm->bQuit = 1;
      pthread_exit(((void *)0));
    }
    break;
  }
  if (listen(listen_socket, 32) < 0) {
    fprintf(stderr,"dnetport: cannot set listen depth to %d (%d)\n",
     32,(*__errno_location ()));
    mapshm->bQuit = 1;
    pthread_exit(((void *)0));
  }
  while(1)
  {
    int client_sock;
    struct sockaddr_in addr;
    unsigned int addr_len = sizeof(struct sockaddr_in);
    int client;
    unsigned long child_tid;
    if (mapshm->bDebug)
    {
      printf("Waiting for connection on port %d\n", mapshm->iPort);
    }
    client_sock = accept(listen_socket, (struct sockaddr *) &addr, &addr_len);
    if (client_sock<0)
    {
      fprintf(stderr,"accept failed (%d,%d)\n",
       (*__errno_location ()),client_sock);
      sleep(10);
      continue;
    }
    if (getpeername(client_sock,
       (struct sockaddr *) &addr,
       &addr_len) < 0) {
      fprintf(stderr,"BAD ERROR encountered - error %d - %s\n",
       (*__errno_location ()),
       strerror((*__errno_location ())));
      close(client_sock);
      sleep(20);
      continue;
    }
    client = -1;
    for (i=0; i<32; i++)
      if (mapshm->client_tid[i] == 0) client = i;
    if (client<0)
    {
      close(client_sock);
      if (mapshm->bDebug)
      {
        printf("All client connections are used up, waiting for one to free up...\n");
      }
      sleep(60);
      continue;
    }
    mapshm->timetag[client] = 0;
    mapshm->read_index[client] = 0;
    mapshm->write_index[client] = 0;
    read_params.iSocket = client_sock;
    read_params.iClient = client;
    read_params.mapshm = mapshm;
    if (mapshm->bDebug)
    {
      printf("q330serv connected and attached to client handler %d, socket %d\n",
        read_params.iClient, read_params.iSocket);
    }
    if (pthread_create(&child_tid, ((void *)0), ServerReadThread, &read_params))
    {
      fprintf(stderr, "%s:StartServer pthread_create ServerReadThread: %s\n",
        "q330arch", strerror((*__errno_location ())));
      mapshm->bQuit = 1;
      pthread_exit(((void *)0));
    }
    pthread_detach(child_tid);
    mapshm->client_tid[client] = child_tid;
    if (mapshm->bDebug)
    {
      printf("New connectionon, started read client %d, thread id %lud\n",
         client, child_tid);
    }
    sleep(2);
  }
}

variable_types:
errstr
char *

variable_types: struct s_readthread { int iClient; int iSocket; struct s_mapshm { int bDebug; int bQuit; int iPort; unsigned long listen_tid; long timetag[32]; unsigned long client_tid[32]; int read_index[32]; int write_index[32]; int result[32][2]; char buffer[32][2][8192]; } *mapshm; }

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

variable_types: client int

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

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


decompiled_code:
void __fastcall alarm_archive(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v5; // rax
_QWORD *v6; // rbp
__int64 v7; // r12
__int64 v8; // rbx
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // r15
const char *v12; // rax
__int64 v13; // rax
__int64 v14; // rax
const char *v15; // rdx
__int64 v16; // rax
char *s; // [rsp+8h] [rbp-60h]
__int16 v18; // [rsp+1Eh] [rbp-4Ah]
char v19; // [rsp+29h] [rbp-3Fh] BYREF
__int16 v20; // [rsp+2Ah] [rbp-3Eh] BYREF
int v21[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v20 = 0;
v19 = 0;
v21[0] = 0;
v5 = buffer_init();
if ( v5 )
{
v6 = (_QWORD *)v5;
v7 = 0LL;
v8 = 0LL;
list_iterator_stop(a1);
list_iterator_start(a1);
v18 = 0;
LABEL_3:
v9 = a1;
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v10 = list_iterator_next(v9);
v11 = v10;
if ( *(_BYTE *)(v10 + 12) )
goto LABEL_3;
if ( !v8 )
v8 = *(_QWORD *)(v10 + 16);
if ( !v7 )
v7 = *(_QWORD *)(v10 + 16);
if ( gDebug )
fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n", "alarm.c", 81LL, "May 23 2025", *(const char **)v10);
v12 = (const char *)q330LogMsg(*(_QWORD *)v11, *a3, a3[1], "LOG", a3[4]);
if ( v12 )
{
if ( gDebug )
fprintf(stderr, "falcon: failed to write alarms to log: %s\n", v12);
else
syslog(3, "falcon: failed to write alarms to log: %s\n", v12);
exit(1);
}
if ( gDebug )
{
s = (char *)(v11 + 27);
fprintf(stdout, "Alarm '%s':\n", (const char *)(v11 + 27));
fprintf(stdout, " Channel : %02x\n", *(unsigned __int16 *)(v11 + 24));
fprintf(stdout, " Timestamp : %li\n", *(_QWORD *)(v11 + 16));
fprintf(stdout, " Event Code : 0x%02x\n", *(unsigned __int8 *)(v11 + 26));
v15 = (const char *)&unk_CC7;
if ( !*(_BYTE *)(v11 + 12) )
v15 = (const char *)&unk_CCB;
fprintf(stdout, " Sent : %s\n", v15);
fprintf(stdout, " Hash : 0x%08lx\n", *(unsigned int *)(v11 + 8));
fprintf(stdout, " Text : %s\n", *(const char **)v11);
v16 = *(_QWORD *)(v11 + 16);
if ( v8 > v16 )
v8 = *(_QWORD *)(v11 + 16);
if ( v7 < v16 )
v7 = *(_QWORD *)(v11 + 16);
if ( !*(_BYTE *)(v11 + 27) )
{
if ( gDebug )
{
fwrite("falcon: description code not found\n", 1uLL, 0x23uLL, stderr);
goto LABEL_3;
}
LABEL_38:
syslog(3, "falcon: description code not found\n");
goto LABEL_3;
}
if ( !v18 )
goto LABEL_40;
}
else
{
v13 = *(_QWORD *)(v11 + 16);
if ( v8 > v13 )
v8 = *(_QWORD *)(v11 + 16);
if ( v7 < v13 )
v7 = *(_QWORD *)(v11 + 16);
if ( !*(_BYTE *)(v11 + 27) )
goto LABEL_38;
if ( !v18 )
{
s = (char *)(v11 + 27);
goto LABEL_37;
}
}
if ( v6[1] > 0x190uLL )
{
buffer_seek(v6, 2LL);
v21[0] = _bswap_32(v8);
buffer_write(v6, v21);
v21[0] = _bswap_32(v7);
buffer_write(v6, v21);
v20 = _bswap_16(v18);
buffer_write(v6, &v20);
if ( gDebug )
{
fwrite("falcon: [MID] alarms were found\n", 1uLL, 0x20uLL, stdout);
fwrite("[MID] raw buffer:\n", 1uLL, 0x12uLL, stdout);
format_data(*v6, v6[1], 0LL, 0LL);
}
QueueOpaque(*v6, v6[1], *a3, a3[1], a3[2], a3[4], "FALC~");
buffer_destroy(v6);
v8 = *(_QWORD *)(v11 + 16);
v6 = (_QWORD *)buffer_init();
if ( !v6 )
goto LABEL_51;
v7 = v8;
s = (char *)(v11 + 27);
LABEL_40:
if ( gDebug )
fwrite("falcon: Writing alarm header info.\n", 1uLL, 0x23uLL, stderr);
LABEL_37:
v20 = _bswap_16(32769);
buffer_write(v6, &v20);
v21[0] = _bswap_32(v8);
buffer_write(v6, v21);
v21[0] = _bswap_32(v7);
buffer_write(v6, v21);
v20 = _bswap_16(0);
buffer_write(v6, &v20);
v18 = 1;
goto LABEL_21;
}
++v18;
s = (char *)(v11 + 27);
LABEL_21:
v20 = _bswap_16(*(_WORD *)(v11 + 24));
buffer_write(v6, &v20);
v21[0] = _bswap_32(*(_DWORD *)(v11 + 16));
buffer_write(v6, v21);
buffer_write(v6, v11 + 26);
v19 = strlen(s);
buffer_write(v6, &v19);
buffer_write(v6, s);
buffer_terminate(v6);
*(_BYTE *)(v11 + 12) = 1;
v9 = a1;
}
list_iterator_stop(v9);
if ( v18 && *v6 && v6[1] )
{
buffer_seek(v6, 2LL);
v21[0] = _bswap_32(v8);
buffer_write(v6, v21);
v21[0] = _bswap_32(v7);
buffer_write(v6, v21);
v20 = _bswap_16(v18);
buffer_write(v6, &v20);
if ( gDebug )
{
fwrite("falcon: [END] alarms were found\n", 1uLL, 0x20uLL, stderr);
fwrite("[END] raw buffer:\n", 1uLL, 0x12uLL, stderr);
format_data(*v6, v6[1], 0LL, 0LL);
}
QueueOpaque(*v6, v6[1], *a3, a3[1], a3[2], a3[4], "FALC~");
}
buffer_destroy(v6);
while ( (unsigned int)list_size(a1) > 0x64 )
{
v14 = list_fetch(a1);
alarm_line_destroy(v14);
}
}
else
{
LABEL_51:
if ( gDebug )
fwrite("falcon: unable to allocate space for alarm data\n", 1uLL, 0x30uLL, stderr);
else
syslog(3, "falcon: unable to allocate space for alarm data\n");
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v5; // rax
_QWORD *v6; // rbp
__int64 v7; // r12
__int64 v8; // rbx
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // r15
const char *v12; // rax
__int64 v13; // rax
__int64 v14; // rax
const char *v15; // rdx
__int64 v16; // rax
char *s; // [rsp+8h] [rbp-60h]
__int16 v18; // [rsp+1Eh] [rbp-4Ah]
char v19; // [rsp+29h] [rbp-3Fh] BYREF
__int16 v20; // [rsp+2Ah] [rbp-3Eh] BYREF
int v21[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v20 = 0;
v19 = 0;
v21[0] = 0;
v5 = buffer_init();
if ( v5 )
{
v6 = (_QWORD *)v5;
v7 = 0LL;
v8 = 0LL;
list_iterator_stop(a1);
list_iterator_start(a1);
v18 = 0;
LABEL_3:
v9 = a1;
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v10 = list_iterator_next(v9);
v11 = v10;
if ( *(_BYTE *)(v10 + 12) )
goto LABEL_3;
if ( !v8 )
v8 = *(_QWORD *)(v10 + 16);
if ( !v7 )
v7 = *(_QWORD *)(v10 + 16);
if ( gDebug )
fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n", "alarm.c", 81LL, "May 23 2025", *(const char **)v10);
v12 = (const char *)q330LogMsg(*(_QWORD *)v11, *a3, a3[1], "LOG", a3[4]);
if ( v12 )
{
if ( gDebug )
fprintf(stderr, "falcon: failed to write alarms to log: %s\n", v12);
else
syslog(3, "falcon: failed to write alarms to log: %s\n", v12);
exit(1);
}
if ( gDebug )
{
s = (char *)(v11 + 27);
fprintf(stdout, "Alarm '%s':\n", (const char *)(v11 + 27));
fprintf(stdout, " Channel : %02x\n", *(unsigned __int16 *)(v11 + 24));
fprintf(stdout, " Timestamp : %li\n", *(_QWORD *)(v11 + 16));
fprintf(stdout, " Event Code : 0x%02x\n", *(unsigned __int8 *)(v11 + 26));
v15 = (const char *)&unk_CC7;
if ( !*(_BYTE *)(v11 + 12) )
v15 = (const char *)&unk_CCB;
fprintf(stdout, " Sent : %s\n", v15);
fprintf(stdout, " Hash : 0x%08lx\n", *(unsigned int *)(v11 + 8));
fprintf(stdout, " Text : %s\n", *(const char **)v11);
v16 = *(_QWORD *)(v11 + 16);
if ( v8 > v16 )
v8 = *(_QWORD *)(v11 + 16);
if ( v7 < v16 )
v7 = *(_QWORD *)(v11 + 16);
if ( !*(_BYTE *)(v11 + 27) )
{
if ( gDebug )
{
fwrite("falcon: description code not found\n", 1uLL, 0x23uLL, stderr);
goto LABEL_3;
}
LABEL_38:
syslog(3, "falcon: description code not found\n");
goto LABEL_3;
}
if ( !v18 )
goto LABEL_40;
}
else
{
v13 = *(_QWORD *)(v11 + 16);
if ( v8 > v13 )
v8 = *(_QWORD *)(v11 + 16);
if ( v7 < v13 )
v7 = *(_QWORD *)(v11 + 16);
if ( !*(_BYTE *)(v11 + 27) )
goto LABEL_38;
if ( !v18 )
{
s = (char *)(v11 + 27);
goto LABEL_37;
}
}
if ( v6[1] > 0x190uLL )
{
buffer_seek(v6, 2LL);
v21[0] = func0(v8);
buffer_write(v6, v21);
v21[0] = func0(v7);
buffer_write(v6, v21);
v20 = func1(v18);
buffer_write(v6, &v20);
if ( gDebug )
{
fwrite("falcon: [MID] alarms were found\n", 1uLL, 0x20uLL, stdout);
fwrite("[MID] raw buffer:\n", 1uLL, 0x12uLL, stdout);
format_data(*v6, v6[1], 0LL, 0LL);
}
QueueOpaque(*v6, v6[1], *a3, a3[1], a3[2], a3[4], "FALC~");
buffer_destroy(v6);
v8 = *(_QWORD *)(v11 + 16);
v6 = (_QWORD *)buffer_init();
if ( !v6 )
goto LABEL_51;
v7 = v8;
s = (char *)(v11 + 27);
LABEL_40:
if ( gDebug )
fwrite("falcon: Writing alarm header info.\n", 1uLL, 0x23uLL, stderr);
LABEL_37:
v20 = func1(32769);
buffer_write(v6, &v20);
v21[0] = func0(v8);
buffer_write(v6, v21);
v21[0] = func0(v7);
buffer_write(v6, v21);
v20 = func1(0);
buffer_write(v6, &v20);
v18 = 1;
goto LABEL_21;
}
++v18;
s = (char *)(v11 + 27);
LABEL_21:
v20 = func1(*(_WORD *)(v11 + 24));
buffer_write(v6, &v20);
v21[0] = func0(*(_DWORD *)(v11 + 16));
buffer_write(v6, v21);
buffer_write(v6, v11 + 26);
v19 = strlen(s);
buffer_write(v6, &v19);
buffer_write(v6, s);
buffer_terminate(v6);
*(_BYTE *)(v11 + 12) = 1;
v9 = a1;
}
list_iterator_stop(v9);
if ( v18 && *v6 && v6[1] )
{
buffer_seek(v6, 2LL);
v21[0] = func0(v8);
buffer_write(v6, v21);
v21[0] = func0(v7);
buffer_write(v6, v21);
v20 = func1(v18);
buffer_write(v6, &v20);
if ( gDebug )
{
fwrite("falcon: [END] alarms were found\n", 1uLL, 0x20uLL, stderr);
fwrite("[END] raw buffer:\n", 1uLL, 0x12uLL, stderr);
format_data(*v6, v6[1], 0LL, 0LL);
}
QueueOpaque(*v6, v6[1], *a3, a3[1], a3[2], a3[4], "FALC~");
}
buffer_destroy(v6);
while ( (unsigned int)list_size(a1) > 0x64 )
{
v14 = list_fetch(a1);
alarm_line_destroy(v14);
}
}
else
{
LABEL_51:
if ( gDebug )
fwrite("falcon: unable to allocate space for alarm data\n", 1uLL, 0x30uLL, stderr);
else
syslog(3, "falcon: unable to allocate space for alarm data\n");
}
}

original_code:
void alarm_archive( alarm_context_t* alarm_list, buffer_t* url_str,
                    st_info_t* st_info )
{
    time_t start_time = 0;
    time_t end_time = 0;
    uint8_t buf_byte = 0;
    uint16_t buf_word = 0;
    uint32_t buf_dword = 0L;
    uint16_t version_type = 0x8000 | 1;
    uint16_t alarm_count = 0;
    char* retmsg = ((void *)0);
    buffer_t* alarm_data = ((void *)0);
    alarm_line_t* alarm = ((void *)0);
    alarm_data = buffer_init();
    if (!alarm_data) {
        if (gDebug)
            fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
        else
            syslog(3, "falcon: unable to allocate space for alarm data\n");
        return;
    }
    list_iterator_stop(alarm_list);
    list_iterator_start(alarm_list);
    while (list_iterator_hasnext(alarm_list))
    {
        alarm = (alarm_line_t*)list_iterator_next(alarm_list);
        if (alarm->sent)
            continue;
        if (!start_time)
          start_time = alarm->timestamp;
        if (!end_time)
          end_time = alarm->timestamp;
        if (gDebug)
            fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n",
                    "alarm.c", 81, "Apr 24 2024", alarm->text);
        if ((retmsg = q330LogMsg(alarm->text, st_info->station,
                                 st_info->network, "LOG",
                                 st_info->location)) != ((void *)0))
        {
            if (gDebug)
                fprintf(stderr, "falcon: failed to write alarms to log: %s\n", retmsg);
            else
                syslog(3, "falcon: failed to write alarms to log: %s\n", retmsg);
            exit(1);
        }
        if (gDebug)
        {
            fprintf(stdout, "Alarm '%s':\n", alarm->description);
            fprintf(stdout, "    Channel    : %02x\n", (alarm->channel));
            fprintf(stdout, "    Timestamp  : %li\n", (long)(alarm->timestamp));
            fprintf(stdout, "    Event Code : 0x%02x\n", (alarm->event));
            fprintf(stdout, "    Sent       : %s\n", alarm->sent ? "Yes" : "No");
            fprintf(stdout, "    Hash       : 0x%08lx\n", (unsigned long)(alarm->hash));
            fprintf(stdout, "    Text       : %s\n", alarm->text);
        }
        if (start_time > alarm->timestamp)
            start_time = alarm->timestamp;
        if (end_time < alarm->timestamp)
            end_time = alarm->timestamp;
        if (!alarm->description[0])
        {
            if (gDebug)
                fprintf(stderr, "falcon: description code not found\n");
            else
                syslog(3, "falcon: description code not found\n");
            continue;
        }
        if (alarm_count && (alarm_data->length > 400))
        {
            buffer_seek(alarm_data, 2);
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
            if (gDebug) {
                fprintf(stdout, "falcon: [MID] alarms were found\n");
                fprintf(stdout, "[MID] raw buffer:\n");
                format_data(alarm_data->content, alarm_data->length, 0, 0);
            }
            QueueOpaque(alarm_data->content, (int)alarm_data->length,
                        st_info->station, st_info->network,
                        st_info->alarm_chan, st_info->location,
                        "FALC~");
            alarm_data = buffer_destroy(alarm_data);
            alarm_count = 0;
            start_time = alarm->timestamp;
            end_time = alarm->timestamp;
            alarm_data = buffer_init();
            if (!alarm_data) {
                if (gDebug)
                    fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
                else
                    syslog(3, "falcon: unable to allocate space for alarm data\n");
                return;
            }
        }
        if (!alarm_count)
        {
            if (gDebug)
            {
                fprintf(stderr, "falcon: Writing alarm header info.\n");
            }
            buf_word = htons(version_type);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        }
        buf_word = htons(alarm->channel);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        buf_dword = htonl(alarm->timestamp);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buffer_write(alarm_data, &(alarm->event), sizeof(alarm->event));
        buf_byte = (uint8_t)strlen(alarm->description);
        buffer_write(alarm_data, &buf_byte, sizeof(buf_byte));
        buffer_write(alarm_data, (uint8_t*)(alarm->description), (size_t)buf_byte);
        buffer_terminate(alarm_data);
        alarm_count++;
        alarm->sent = 1;
    }
    list_iterator_stop(alarm_list);
    if (alarm_count && alarm_data && alarm_data->content && alarm_data->length)
    {
        buffer_seek(alarm_data, 2);
        buf_dword = htonl(start_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_dword = htonl(end_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_word = htons(alarm_count);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        if (gDebug) {
            fprintf(stderr, "falcon: [END] alarms were found\n");
            fprintf(stderr, "[END] raw buffer:\n");
            format_data(alarm_data->content, alarm_data->length, 0, 0);
        }
        QueueOpaque(alarm_data->content, (int)alarm_data->length,
                    st_info->station, st_info->network,
                    st_info->alarm_chan, st_info->location,
                    "FALC~");
    }
    alarm_data = buffer_destroy(alarm_data);
    while (list_size(alarm_list) > 100)
    {
        alarm = list_fetch(alarm_list);
        alarm = alarm_line_destroy(alarm);
    }
}

canonical_original_code:
void alarm_archive(struct list_t *alarm_list, struct _buffer *url_str, struct st_info_t *st_info)
{
    long start_time = 0;
    long end_time = 0;
    unsigned char buf_byte = 0;
    unsigned short buf_word = 0;
    unsigned int buf_dword = 0L;
    unsigned short version_type = 0x8000 | 1;
    unsigned short alarm_count = 0;
    char *retmsg = ((void *)0);
    struct _buffer *alarm_data = ((void *)0);
    struct alarm_line_t *alarm = ((void *)0);
    alarm_data = buffer_init();
    if (!alarm_data) {
        if (gDebug)
            fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
        else
            syslog(3, "falcon: unable to allocate space for alarm data\n");
        return;
    }
    list_iterator_stop(alarm_list);
    list_iterator_start(alarm_list);
    while (list_iterator_hasnext(alarm_list))
    {
        alarm = (struct alarm_line_t *)list_iterator_next(alarm_list);
        if (alarm->sent)
            continue;
        if (!start_time)
          start_time = alarm->timestamp;
        if (!end_time)
          end_time = alarm->timestamp;
        if (gDebug)
            fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n",
                    "alarm.c", 81, "Apr 24 2024", alarm->text);
        if ((retmsg = q330LogMsg(alarm->text, st_info->station,
                                 st_info->network, "LOG",
                                 st_info->location)) != ((void *)0))
        {
            if (gDebug)
                fprintf(stderr, "falcon: failed to write alarms to log: %s\n", retmsg);
            else
                syslog(3, "falcon: failed to write alarms to log: %s\n", retmsg);
            exit(1);
        }
        if (gDebug)
        {
            fprintf(stdout, "Alarm '%s':\n", alarm->description);
            fprintf(stdout, "    Channel    : %02x\n", (alarm->channel));
            fprintf(stdout, "    Timestamp  : %li\n", (long)(alarm->timestamp));
            fprintf(stdout, "    Event Code : 0x%02x\n", (alarm->event));
            fprintf(stdout, "    Sent       : %s\n", alarm->sent ? "Yes" : "No");
            fprintf(stdout, "    Hash       : 0x%08lx\n", (unsigned long)(alarm->hash));
            fprintf(stdout, "    Text       : %s\n", alarm->text);
        }
        if (start_time > alarm->timestamp)
            start_time = alarm->timestamp;
        if (end_time < alarm->timestamp)
            end_time = alarm->timestamp;
        if (!alarm->description[0])
        {
            if (gDebug)
                fprintf(stderr, "falcon: description code not found\n");
            else
                syslog(3, "falcon: description code not found\n");
            continue;
        }
        if (alarm_count && (alarm_data->length > 400))
        {
            buffer_seek(alarm_data, 2);
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (unsigned char *)(&buf_word), sizeof(buf_word));
            if (gDebug) {
                fprintf(stdout, "falcon: [MID] alarms were found\n");
                fprintf(stdout, "[MID] raw buffer:\n");
                format_data(alarm_data->content, alarm_data->length, 0, 0);
            }
            QueueOpaque(alarm_data->content, (int)alarm_data->length,
                        st_info->station, st_info->network,
                        st_info->alarm_chan, st_info->location,
                        "FALC~");
            alarm_data = buffer_destroy(alarm_data);
            alarm_count = 0;
            start_time = alarm->timestamp;
            end_time = alarm->timestamp;
            alarm_data = buffer_init();
            if (!alarm_data) {
                if (gDebug)
                    fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
                else
                    syslog(3, "falcon: unable to allocate space for alarm data\n");
                return;
            }
        }
        if (!alarm_count)
        {
            if (gDebug)
            {
                fprintf(stderr, "falcon: Writing alarm header info.\n");
            }
            buf_word = htons(version_type);
            buffer_write(alarm_data, (unsigned char *)(&buf_word), sizeof(buf_word));
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (unsigned char *)(&buf_word), sizeof(buf_word));
        }
        buf_word = htons(alarm->channel);
        buffer_write(alarm_data, (unsigned char *)(&buf_word), sizeof(buf_word));
        buf_dword = htonl(alarm->timestamp);
        buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
        buffer_write(alarm_data, &(alarm->event), sizeof(alarm->event));
        buf_byte = (unsigned char)strlen(alarm->description);
        buffer_write(alarm_data, &buf_byte, sizeof(buf_byte));
        buffer_write(alarm_data, (unsigned char *)(alarm->description), (unsigned long)buf_byte);
        buffer_terminate(alarm_data);
        alarm_count++;
        alarm->sent = 1;
    }
    list_iterator_stop(alarm_list);
    if (alarm_count && alarm_data && alarm_data->content && alarm_data->length)
    {
        buffer_seek(alarm_data, 2);
        buf_dword = htonl(start_time);
        buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
        buf_dword = htonl(end_time);
        buffer_write(alarm_data, (unsigned char *)(&buf_dword), sizeof(buf_dword));
        buf_word = htons(alarm_count);
        buffer_write(alarm_data, (unsigned char *)(&buf_word), sizeof(buf_word));
        if (gDebug) {
            fprintf(stderr, "falcon: [END] alarms were found\n");
            fprintf(stderr, "[END] raw buffer:\n");
            format_data(alarm_data->content, alarm_data->length, 0, 0);
        }
        QueueOpaque(alarm_data->content, (int)alarm_data->length,
                    st_info->station, st_info->network,
                    st_info->alarm_chan, st_info->location,
                    "FALC~");
    }
    alarm_data = buffer_destroy(alarm_data);
    while (list_size(alarm_list) > 100)
    {
        alarm = list_fetch(alarm_list);
        alarm = alarm_line_destroy(alarm);
    }
}

variable_types: struct list_t { struct list_entry_s { void *data; struct list_entry_s *next; struct list_entry_s *prev; } *head_sentinel; struct list_entry_s *tail_sentinel; struct list_entry_s *mid; unsigned int numels; struct list_entry_s **spareels; unsigned int spareelsnum; int iter_active; unsigned int iter_pos; struct list_entry_s *iter_curentry; struct list_attributes_s { int (*comparator)(void *a, void *b); int (*seeker)(void *el, void *indicator); unsigned long (*meter)(void *el); int copy_data; int (*hasher)(void *el); void *(*serializer)(void *el, unsigned int *serializ_len); void *(*unserializer)(void *data, unsigned int *data_len); } attrs; } *

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

variable_types: struct st_info_t { char *station; char *network; char *alarm_chan; char *csv_chan; char *location; } *

variable_types:
retmsg
char *

variable_types: struct alarm_line_t { char *text; unsigned int hash; unsigned char sent; long timestamp; unsigned short channel; unsigned char event; char description[9]; } *

return_type: void

user_defined_types: struct list_t { struct list_entry_s *head_sentinel; struct list_entry_s *tail_sentinel; struct list_entry_s *mid; unsigned int numels; struct list_entry_s **spareels; unsigned int spareelsnum; int iter_active; unsigned int iter_pos; struct list_entry_s *iter_curentry; struct list_attributes_s attrs; }


decompiled_code:
__int64 __fastcall alarm_poll(__int64 a1, _QWORD *a2, __int64 a3, __int64 a4)
{
_QWORD *v5; // rbp
const char **v6; // r12

v5 = (_QWORD *)buffer_init();
buffer_write(v5, *a2);
buffer_write(v5, "/data/alarmhistory.txt");
buffer_terminate(v5);
v6 = (const char **)buffer_init();
get_page(*v5, v6);
alarm_filter_lines(a1, v6, a4);
list_sort(a1, 1LL);
buffer_destroy(v5);
return buffer_destroy(v6);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _QWORD *a2, __int64 a3, __int64 a4)
{
_QWORD *v5; // rbp
const char **v6; // r12

v5 = (_QWORD *)buffer_init();
buffer_write(v5, *a2);
buffer_write(v5, "/data/alarmhistory.txt");
buffer_terminate(v5);
v6 = (const char **)buffer_init();
get_page(*v5, v6);
func3(a1, v6, a4);
list_sort(a1, 1LL);
buffer_destroy(v5);
return buffer_destroy(v6);
}

original_code:
void alarm_poll( alarm_context_t* alarm_list, buffer_t* url_str,
                 st_info_t* st_info, time_t last_alarm_time )
{
    buffer_t* url = ((void *)0);
    buffer_t* buf = ((void *)0);
    const char* file_path = "/data/alarmhistory.txt";
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (uint8_t*)file_path, strlen(file_path));
    buffer_terminate(url);
    buf = buffer_init();
    get_page((char*)url->content, buf);
    alarm_filter_lines( alarm_list, buf, last_alarm_time );
    list_sort(alarm_list, 1);
    url = buffer_destroy(url);
    buf = buffer_destroy(buf);
}

canonical_original_code:
void alarm_poll(struct list_t *alarm_list, struct _buffer *url_str, struct st_info_t *st_info, long last_alarm_time)
{
    struct _buffer *url = ((void *)0);
    struct _buffer *buf = ((void *)0);
    char *file_path = "/data/alarmhistory.txt";
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (unsigned char *)file_path, strlen(file_path));
    buffer_terminate(url);
    buf = buffer_init();
    get_page((char *)url->content, buf);
    alarm_filter_lines( alarm_list, buf, last_alarm_time );
    list_sort(alarm_list, 1);
    url = buffer_destroy(url);
    buf = buffer_destroy(buf);
}

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

user_defined_types: struct st_info_t { char *station; char *network; char *alarm_chan; char *csv_chan; char *location; }

user_defined_types: struct list_attributes_s { int (*comparator)(void *a, void *b); int (*seeker)(void *el, void *indicator); unsigned long (*meter)(void *el); int copy_data; int (*hasher)(void *el); void *(*serializer)(void *el, unsigned int *serializ_len); void *(*unserializer)(void *data, unsigned int *data_len); }

user_defined_types: struct list_entry_s { void *data; struct list_entry_s *next; struct list_entry_s *prev; }


original_code:
long ST_DeltaToMS2(DELTA_T2 indelta) {
  long totalms;
  BOOL minf = 0;
  if (indelta.nday > 2) return((20L*86400L*1000L));
  if (indelta.nday < -2) return(-(20L*86400L*1000L));
  if (indelta.nday < 0) {
    minf = 1;
    indelta = ST_MinusDelta2(indelta);
  }
  totalms = ((long) indelta.nday) * 86400L * 10000L;
  totalms += ((long) indelta.nhour) * 3600L * 10000L;
  totalms += ((long) indelta.nmin) * 60L * 10000L;
  totalms += ((long) indelta.nsec) * 10000L;
  totalms += ((long) indelta.ntenth_msecs);
  if (minf) totalms = -totalms;
  if (totalms > (20L*86400L*1000L)) totalms = (20L*86400L*1000L);
  if (totalms < -(20L*86400L*1000L)) totalms = - (20L*86400L*1000L);
  return(totalms);
}

canonical_original_code:
long ST_DeltaToMS2(struct __deltatime2 indelta) {
  long totalms;
  unsigned char minf = 0;
  if (indelta.nday > 2) return((20L*86400L*1000L));
  if (indelta.nday < -2) return(-(20L*86400L*1000L));
  if (indelta.nday < 0) {
    minf = 1;
    indelta = ST_MinusDelta2(indelta);
  }
  totalms = ((long) indelta.nday) * 86400L * 10000L;
  totalms += ((long) indelta.nhour) * 3600L * 10000L;
  totalms += ((long) indelta.nmin) * 60L * 10000L;
  totalms += ((long) indelta.nsec) * 10000L;
  totalms += ((long) indelta.ntenth_msecs);
  if (minf) totalms = -totalms;
  if (totalms > (20L*86400L*1000L)) totalms = (20L*86400L*1000L);
  if (totalms < -(20L*86400L*1000L)) totalms = - (20L*86400L*1000L);
  return(totalms);
}

variable_types: struct __deltatime2 { long nday; unsigned char nhour; unsigned char nmin; unsigned char nsec; long ntenth_msecs; }

variable_types: totalms long

variable_types:
minf
unsigned char


original_code:
STDTIME2 ST_AddToTime2(STDTIME2 intime, long inday, long inhour,
                       long inminute, long insecond, long intenthmsec) {
  STDTIME2 outtime;
  long dy;
  dy = intime.year;
  inday = intime.day + inday - 1;
  inhour = intime.hour + inhour;
  inminute = intime.minute + inminute;
  insecond = intime.second + insecond;
  intenthmsec = intime.tenth_msec + intenthmsec;
  timenorm2(&dy, &inday, &inhour, &inminute, &insecond, &intenthmsec);
  outtime.year = dy;
  outtime.day = inday + 1;
  outtime.hour = inhour;
  outtime.minute = inminute;
  outtime.second = insecond;
  outtime.tenth_msec = intenthmsec;
  return(outtime);
}

canonical_original_code:
struct __sjtim2 ST_AddToTime2(struct __sjtim2 intime, long inday, long inhour, long inminute, long insecond, long intenthmsec) {
  struct __sjtim2 outtime;
  long dy;
  dy = intime.year;
  inday = intime.day + inday - 1;
  inhour = intime.hour + inhour;
  inminute = intime.minute + inminute;
  insecond = intime.second + insecond;
  intenthmsec = intime.tenth_msec + intenthmsec;
  timenorm2(&dy, &inday, &inhour, &inminute, &insecond, &intenthmsec);
  outtime.year = dy;
  outtime.day = inday + 1;
  outtime.hour = inhour;
  outtime.minute = inminute;
  outtime.second = insecond;
  outtime.tenth_msec = intenthmsec;
  return(outtime);
}

variable_types:
intime
struct __sjtim2

variable_types: inday long

variable_types: inhour long

variable_types: inminute long

variable_types: insecond long

variable_types: intenthmsec long

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

variable_types: dy long


decompiled_code:
void __fastcall timeload2(char a1, char a2, char a3, __int16 a4)
{
BYTE4(setuptime) = a1;
BYTE5(setuptime) = a2;
BYTE6(setuptime) = a3;
word_238 = a4;
}

canonical_decompiled_code:
void __fastcall func0(char a1, char a2, char a3, __int16 a4)
{
BYTE4(setuptime) = a1;
BYTE5(setuptime) = a2;
BYTE6(setuptime) = a3;
word_238 = a4;
}

original_code:
void timeload2(int hour, int minute, int second, long tenth_msec) {
  timeset = 1;
  setuptime.hour = hour;
  setuptime.minute = minute;
  setuptime.second = second;
  setuptime.tenth_msec = tenth_msec;
}

variable_types: hour int

variable_types: minute int

variable_types: second int


decompiled_code:
void __fastcall juldateset2(__int16 a1, __int16 a2)
{
LOWORD(setuptime) = a1;
setyear2();
WORD1(setuptime) = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int16 a1, __int16 a2)
{
LOWORD(setuptime) = a1;
func1();
WORD1(setuptime) = a2;
}

original_code:
void juldateset2(int year, int day) {
  dateset = 1;
  setuptime.year = year;
  _setyear2();
  setuptime.day = day;
}

variable_types: year int

variable_types: day int


decompiled_code:
char __fastcall nordate2(int a1, __int16 a2, __int16 a3)
{
__int16 v3; // bx
__int64 v4; // rax
__int16 v5; // dx
__int64 v6; // rcx

v3 = a2;
LOWORD(setuptime) = a3;
setyear2();
LOBYTE(v4) = 28 - (((unsigned int)tleap((unsigned int)(__int16)setuptime) == 0) - 1);
*((_BYTE *)&dmsize + 1) = v4;
if ( a1 > 1 )
{
v4 = 0LL;
v5 = 0;
do
{
v5 += *((unsigned __int8 *)&dmsize + v4);
v6 = v4++;
}
while ( a1 - 2 != v6 );
v3 = v5 + a2;
}
WORD1(setuptime) = v3;
return v4;
}

canonical_decompiled_code:
char __fastcall func3(int a1, __int16 a2, __int16 a3)
{
__int16 v3; // bx
__int64 v4; // rax
__int16 v5; // dx
__int64 v6; // rcx

v3 = a2;
LOWORD(setuptime) = a3;
func1();
LOBYTE(v4) = 28 - (((unsigned int)tleap((unsigned int)(__int16)setuptime) == 0) - 1);
*((_BYTE *)&dmsize + 1) = v4;
if ( a1 > 1 )
{
v4 = 0LL;
v5 = 0;
do
{
v5 += *((unsigned __int8 *)&dmsize + v4);
v6 = v4++;
}
while ( a1 - 2 != v6 );
v3 = v5 + a2;
}
WORD1(setuptime) = v3;
return v4;
}

original_code:
void nordate2(int month, int day, int year) {
  int ct, i;
  dateset = 1;
  setuptime.year = year;
  _setyear2();
  _dmsize[1] = _tleap(setuptime.year)?29:28;
  ct = 0;
  for (i = 0; i < month-1; i++) {
    ct += _dmsize[i];
  }
  ct += day;
  setuptime.day = ct;
}

canonical_original_code:
void nordate2(int month, int day, int year) {
  int ct; int i;
  dateset = 1;
  setuptime.year = year;
  _setyear2();
  _dmsize[1] = _tleap(setuptime.year)?29:28;
  ct = 0;
  for (i = 0; i < month-1; i++) {
    ct += _dmsize[i];
  }
  ct += day;
  setuptime.day = ct;
}

variable_types: month int

variable_types: ct int

variable_types: i int


decompiled_code:
__int64 __fastcall ST_ParseTime2(char *s)
{
size_t i; // rbx
const unsigned __int16 **v2; // rax
__int64 v3; // rbp
const unsigned __int16 *v4; // rdx
char v5; // al
int v7; // [rsp+1Ch] [rbp-BCh] BYREF
int v8; // [rsp+20h] [rbp-B8h] BYREF
int v9; // [rsp+24h] [rbp-B4h] BYREF
int v10; // [rsp+28h] [rbp-B0h] BYREF
int v11; // [rsp+2Ch] [rbp-ACh] BYREF
unsigned int v12; // [rsp+30h] [rbp-A8h] BYREF
char v13[152]; // [rsp+40h] [rbp-98h] BYREF

for ( i = 0LL; strlen(s) > i; ++i )
{
v2 = _ctype_b_loc();
v3 = (unsigned __int8)s[i];
v4 = *v2;
v5 = s[i];
if ( (v4[v3] & 0x100) != 0 )
v5 = (*_ctype_tolower_loc())[v3];
v13[i] = v5;
}
v13[(int)i] = 0;
v7 = 0;
v8 = 0;
v9 = 0;
v10 = 0;
v11 = 0;
v12 = 0;
_isoc99_sscanf(v13, "%d,%d,%d:%d:%d.%d", &v7, &v8, &v9, &v10, &v11, &v12);
LOWORD(setuptime) = v7;
word_238 = v12;
WORD1(setuptime) = v8;
BYTE4(setuptime) = v9;
BYTE5(setuptime) = v10;
BYTE6(setuptime) = v11;
return ST_CleanDate2(setuptime, v12, 0LL, 1000LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s)
{
size_t i; // rbx
const unsigned __int16 **v2; // rax
__int64 v3; // rbp
const unsigned __int16 *v4; // rdx
char v5; // al
int v7; // [rsp+1Ch] [rbp-BCh] BYREF
int v8; // [rsp+20h] [rbp-B8h] BYREF
int v9; // [rsp+24h] [rbp-B4h] BYREF
int v10; // [rsp+28h] [rbp-B0h] BYREF
int v11; // [rsp+2Ch] [rbp-ACh] BYREF
unsigned int v12; // [rsp+30h] [rbp-A8h] BYREF
char v13[152]; // [rsp+40h] [rbp-98h] BYREF

for ( i = 0LL; strlen(s) > i; ++i )
{
v2 = _ctype_b_loc();
v3 = (unsigned __int8)s[i];
v4 = *v2;
v5 = s[i];
if ( (v4[v3] & 0x100) != 0 )
v5 = (*_ctype_tolower_loc())[v3];
v13[i] = v5;
}
v13[(int)i] = 0;
v7 = 0;
v8 = 0;
v9 = 0;
v10 = 0;
v11 = 0;
v12 = 0;
_isoc99_sscanf(v13, "%d,%d,%d:%d:%d.%d", &v7, &v8, &v9, &v10, &v11, &v12);
LOWORD(setuptime) = v7;
word_238 = v12;
WORD1(setuptime) = v8;
BYTE4(setuptime) = v9;
BYTE5(setuptime) = v10;
BYTE6(setuptime) = v11;
return ST_CleanDate2(setuptime, v12, 0LL, 1000LL);
}

original_code:
STDTIME2 ST_ParseTime2(UBYTE *inbuff) {
  int getbuf,i;
  int year, doy, hour, minute, second, tmsec;
  int iArg;
  UBYTE trsbuf[120];
  for (i = 0; i < strlen(inbuff); i++) {
    if (((*__ctype_b_loc ())[(int) ((inbuff[i]))] & (unsigned short int) _ISupper))
      trsbuf[i] = tolower(inbuff[i]);
    else
      trsbuf[i] = inbuff[i];
  }
  trsbuf[i++] = '\0';
  year = 0;
  doy = 0;
  hour = 0;
  minute = 0;
  second = 0;
  tmsec = 0;
  iArg = sscanf(trsbuf, "%d,%d,%d:%d:%d.%d",
      &year, &doy, &hour, &minute, &second, &tmsec);
  setuptime.year = year;
  setuptime.day = doy;
  setuptime.hour = hour;
  setuptime.minute = minute;
  setuptime.second = second;
  setuptime.tenth_msec = tmsec;
  dateset = timeset = 0;
  return(ST_CleanDate2(setuptime, 0, 1000));
}

canonical_original_code:
struct __sjtim2 ST_ParseTime2(unsigned char *inbuff) {
  int getbuf; int i;
  int year; int doy; int hour; int minute; int second; int tmsec;
  int iArg;
  unsigned char trsbuf[120];
  for (i = 0; i < strlen(inbuff); i++) {
    if (((*__ctype_b_loc ())[(int) ((inbuff[i]))] & (unsigned short) _ISupper))
      trsbuf[i] = tolower(inbuff[i]);
    else
      trsbuf[i] = inbuff[i];
  }
  trsbuf[i++] = '\0';
  year = 0;
  doy = 0;
  hour = 0;
  minute = 0;
  second = 0;
  tmsec = 0;
  iArg = sscanf(trsbuf, "%d,%d,%d:%d:%d.%d",
      &year, &doy, &hour, &minute, &second, &tmsec);
  setuptime.year = year;
  setuptime.day = doy;
  setuptime.hour = hour;
  setuptime.minute = minute;
  setuptime.second = second;
  setuptime.tenth_msec = tmsec;
  dateset = timeset = 0;
  return(ST_CleanDate2(setuptime, 0, 1000));
}

variable_types:
inbuff
unsigned char *

variable_types: getbuf int

variable_types: doy int

variable_types: tmsec int

variable_types: iArg int

variable_types:
trsbuf
unsigned char [120]


original_code:
UBYTE *ST_DeltaPrint2(DELTA_T2 delta, BOOL printplusflag, BOOL fixms) {
  long tenth_ms;
  BOOL minus;
  DELTA_T2 tdelta;
  UBYTE osign[3], msprt[8];
  minus = 0;
  if (delta.nday < 0) {
    minus = 1;
    tdelta = ST_MinusDelta2(delta);
  } else {
    tdelta = delta;
  }
  tenth_ms = tdelta.ntenth_msecs;
  if (tenth_ms == 0)
    msprt[0] = '\0';
  else if (tenth_ms % 1000 == 0)
    sprintf(msprt, ".%01d", (int)tenth_ms/1000);
  else if (tenth_ms % 100 == 0)
    sprintf(msprt, ".%02d", (int)tenth_ms/100);
  else if (tenth_ms % 10 == 0)
    sprintf(msprt, ".%03d", (int)tenth_ms/10);
  else
    sprintf(msprt, ".%04d", (int)tenth_ms);
  if (fixms)
    sprintf(msprt, ".%04d", (int)tenth_ms);
  osign[0] = '\0';
  osign[1] = '\0';
  if (minus)
    osign[0] = '-';
  else if (printplusflag)
    osign[0] = '+';
  if (tdelta.nday > 0) {
    sprintf(_odata, "%s%d,%d:%02d:%02d%s", osign, (int)tdelta.nday, (int)tdelta.nhour, (int)tdelta.nmin, (int) delta.nsec, msprt);
  } else if (tdelta.nhour > 0) {
    sprintf(_odata, "%s%d:%02d:%02d%s", osign, (int)tdelta.nhour, (int)tdelta.nmin, (int)tdelta.nsec, msprt);
  } else if (tdelta.nmin > 0) {
    sprintf(_odata, "%s%d:%02d%s", osign, (int)tdelta.nmin, (int)tdelta.nsec, msprt);
  } else {
    sprintf(_odata, "%s%d%s", osign, (int)tdelta.nsec, msprt);
  }
  return(_odata);
}

canonical_original_code:
unsigned char *ST_DeltaPrint2(struct __deltatime2 delta, unsigned char printplusflag, unsigned char fixms) {
  long tenth_ms;
  unsigned char minus;
  struct __deltatime2 tdelta;
  unsigned char osign[3]; unsigned char msprt[8];
  minus = 0;
  if (delta.nday < 0) {
    minus = 1;
    tdelta = ST_MinusDelta2(delta);
  } else {
    tdelta = delta;
  }
  tenth_ms = tdelta.ntenth_msecs;
  if (tenth_ms == 0)
    msprt[0] = '\0';
  else if (tenth_ms % 1000 == 0)
    sprintf(msprt, ".%01d", (int)tenth_ms/1000);
  else if (tenth_ms % 100 == 0)
    sprintf(msprt, ".%02d", (int)tenth_ms/100);
  else if (tenth_ms % 10 == 0)
    sprintf(msprt, ".%03d", (int)tenth_ms/10);
  else
    sprintf(msprt, ".%04d", (int)tenth_ms);
  if (fixms)
    sprintf(msprt, ".%04d", (int)tenth_ms);
  osign[0] = '\0';
  osign[1] = '\0';
  if (minus)
    osign[0] = '-';
  else if (printplusflag)
    osign[0] = '+';
  if (tdelta.nday > 0) {
    sprintf(_odata, "%s%d,%d:%02d:%02d%s", osign, (int)tdelta.nday, (int)tdelta.nhour, (int)tdelta.nmin, (int) delta.nsec, msprt);
  } else if (tdelta.nhour > 0) {
    sprintf(_odata, "%s%d:%02d:%02d%s", osign, (int)tdelta.nhour, (int)tdelta.nmin, (int)tdelta.nsec, msprt);
  } else if (tdelta.nmin > 0) {
    sprintf(_odata, "%s%d:%02d%s", osign, (int)tdelta.nmin, (int)tdelta.nsec, msprt);
  } else {
    sprintf(_odata, "%s%d%s", osign, (int)tdelta.nsec, msprt);
  }
  return(_odata);
}

variable_types: struct __deltatime2 { long nday; unsigned char nhour; unsigned char nmin; unsigned char nsec; long ntenth_msecs; }

variable_types:
printplusflag
unsigned char

variable_types:
fixms
unsigned char

variable_types:
minus
unsigned char

variable_types:
osign
unsigned char [3]

variable_types:
msprt
unsigned char [8]

return_type: unsigned char *


decompiled_code:
__int64 ST_GetCurrentTime2()
{
struct tm *v0; // rax
int v1; // er14
int v2; // er13
int v3; // er12
__int16 v4; // r15
int v5; // ebx
int v6; // edi
int v7; // esi
int v8; // ecx
__int64 v9; // rax
int v10; // edx
int v11; // edx
time_t timer; // [rsp+18h] [rbp-50h] BYREF
__int64 v14; // [rsp+26h] [rbp-42h]

time(&timer);
v0 = gmtime(&timer);
v1 = v0->tm_hour;
v2 = v0->tm_min;
v3 = v0->tm_sec;
v4 = LOWORD(v0->tm_year) + 1900;
v5 = v0->tm_mday;
v6 = (__int16)(LOWORD(v0->tm_mon) + 1) - 1;
dmsize[1] = 28 - (((unsigned int)tleap((unsigned int)v4) == 0) - 1);
if ( v6 <= 0 )
{
LOWORD(v11) = 0;
}
else
{
v7 = 0;
v8 = 0;
v9 = 0LL;
do
{
v10 = (unsigned __int8)dmsize[v9];
v9 = (__int16)++v8;
v11 = v7 + v10;
v7 = v11;
}
while ( (__int16)v8 < v6 );
}
LOWORD(v14) = v4;
WORD1(v14) = v5 + v11;
BYTE4(v14) = v1;
BYTE5(v14) = v2;
BYTE6(v14) = v3;
return v14;
}

canonical_decompiled_code:
__int64 func0()
{
struct tm *v0; // rax
int v1; // er14
int v2; // er13
int v3; // er12
__int16 v4; // r15
int v5; // ebx
int v6; // edi
int v7; // esi
int v8; // ecx
__int64 v9; // rax
int v10; // edx
int v11; // edx
time_t timer; // [rsp+18h] [rbp-50h] BYREF
__int64 v14; // [rsp+26h] [rbp-42h]

time(&timer);
v0 = gmtime(&timer);
v1 = v0->tm_hour;
v2 = v0->tm_min;
v3 = v0->tm_sec;
v4 = LOWORD(v0->tm_year) + 1900;
v5 = v0->tm_mday;
v6 = (__int16)(LOWORD(v0->tm_mon) + 1) - 1;
dmsize[1] = 28 - (((unsigned int)tleap((unsigned int)v4) == 0) - 1);
if ( v6 <= 0 )
{
LOWORD(v11) = 0;
}
else
{
v7 = 0;
v8 = 0;
v9 = 0LL;
do
{
v10 = (unsigned __int8)dmsize[v9];
v9 = (__int16)++v8;
v11 = v7 + v10;
v7 = v11;
}
while ( (__int16)v8 < v6 );
}
LOWORD(v14) = v4;
WORD1(v14) = v5 + v11;
BYTE4(v14) = v1;
BYTE5(v14) = v2;
BYTE6(v14) = v3;
return v14;
}

original_code:
STDTIME2 ST_GetCurrentTime2(void) {
  struct tm *intime;
  time_t tloc;
  STDTIME2 rettime;
  WORD i,ct,mon,day;
  time(&tloc);
  intime = gmtime(&tloc);
  rettime.year = intime->tm_year + 1900;
  mon = intime->tm_mon + 1;
  day = intime->tm_mday;
  rettime.hour = intime->tm_hour;
  rettime.minute = intime->tm_min;
  rettime.second = intime->tm_sec;
  rettime.tenth_msec = 0;
  _dmsize[1] = _tleap(rettime.year)?29:28;
  ct = 0;
  for (i = 0; i < (mon-1); i++)
    ct+=_dmsize[i];
  ct += day;
  rettime.day = ct;
  return(rettime);
}

canonical_original_code:
struct __sjtim2 ST_GetCurrentTime2() {
  struct tm *intime;
  long tloc;
  struct __sjtim2 rettime;
  short i; short ct; short mon; short day;
  time(&tloc);
  intime = gmtime(&tloc);
  rettime.year = intime->tm_year + 1900;
  mon = intime->tm_mon + 1;
  day = intime->tm_mday;
  rettime.hour = intime->tm_hour;
  rettime.minute = intime->tm_min;
  rettime.second = intime->tm_sec;
  rettime.tenth_msec = 0;
  _dmsize[1] = _tleap(rettime.year)?29:28;
  ct = 0;
  for (i = 0; i < (mon-1); i++)
    ct+=_dmsize[i];
  ct += day;
  rettime.day = ct;
  return(rettime);
}

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

variable_types: tloc long

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

variable_types: i short

variable_types: ct short

variable_types: mon short

variable_types: day short