ServerReadThread

decompiled_code:
void __fastcall __noreturn ServerReadThread(void *a1)
{
unsigned int v1; // er14
int v2; // er12
_DWORD *v3; // rax
__int64 i; // rdx
__int64 v5; // rbp
__int64 v6; // r13
int v7; // edx
int v8; // ebx
int v9; // eax
__int64 j; // rbx
__int64 k; // rax
int v12; // edx
size_t v13; // rax
size_t v14; // rax
size_t v15; // rax
__int64 v16; // [rsp+8h] [rbp-D0h]
int v17; // [rsp+14h] [rbp-C4h]
__int64 v18; // [rsp+18h] [rbp-C0h]
char s[184]; // [rsp+20h] [rbp-B8h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
unsigned int v1; // er14
int v2; // er12
_DWORD *v3; // rax
__int64 i; // rdx
__int64 v5; // rbp
__int64 v6; // r13
int v7; // edx
int v8; // ebx
int v9; // eax
__int64 j; // rbx
__int64 k; // rax
int v12; // edx
size_t v13; // rax
size_t v14; // rax
size_t v15; // rax
__int64 v16; // [rsp+8h] [rbp-D0h]
int v17; // [rsp+14h] [rbp-C4h]
__int64 v18; // [rsp+18h] [rbp-C0h]
char s[184]; // [rsp+20h] [rbp-B8h] BYREF

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

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

repo: joeledwards/asl-station-processor

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

v2 = (char *)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);
fprintf(stderr, "%s\n", errbuf_0);
v2 = errbuf_0;
}
else
{
v4 = shmat(v3, 0LL, 384);
unixadr_1 = (__int64)v4;
if ( (_DWORD)v4 == -1 )
{
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);
v2 = errbuf_0;
}
else
{
*a1 = v4;
}
}
return v2;
}

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

v2 = (char *)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);
fprintf(stderr, "%s\n", errbuf_0);
v2 = errbuf_0;
}
else
{
v4 = shmat(v3, 0LL, 384);
unixadr_1 = (__int64)v4;
if ( (_DWORD)v4 == -1 )
{
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);
v2 = errbuf_0;
}
else
{
*a1 = v4;
}
}
return v2;
}

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

variable_types: shmid int

variable_types:
unixadr
char *

variable_types:
errbuf
char [1024]

return_type: char *

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 *v1; // rax
__int64 v2; // rdx
unsigned int *v3; // rax
int v4; // er12
int *v5; // rbx
char *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rbx
char *v11; // rax
int *v12; // rax
char *v13; // rax
int v14; // eax
unsigned int v15; // er13
__int64 v16; // rax
int v17; // ebx
pthread_t v18; // rdx
socklen_t addr_len; // [rsp+4h] [rbp-64h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-60h] BYREF
struct sockaddr v21; // [rsp+10h] [rbp-58h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-48h] BYREF
int arg[2]; // [rsp+30h] [rbp-38h] BYREF
unsigned int *v24; // [rsp+38h] [rbp-30h]

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

canonical_decompiled_code:
void __fastcall __noreturn func4(unsigned int *a1)
{
const char *v1; // rax
__int64 v2; // rdx
unsigned int *v3; // rax
int v4; // er12
int *v5; // rbx
char *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rbx
char *v11; // rax
int *v12; // rax
char *v13; // rax
int v14; // eax
unsigned int v15; // er13
__int64 v16; // rax
int v17; // ebx
pthread_t v18; // rdx
socklen_t addr_len; // [rsp+4h] [rbp-64h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-60h] BYREF
struct sockaddr v21; // [rsp+10h] [rbp-58h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-48h] BYREF
int arg[2]; // [rsp+30h] [rbp-38h] BYREF
unsigned int *v24; // [rsp+38h] [rbp-30h]

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

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 v4; // rax
_QWORD *v5; // rbp
__int64 v6; // r13
__int64 v7; // r12
const char *v8; // rdx
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rbx
const char *v12; // rax
__int64 v13; // rax
__int64 v14; // rax
__int16 v15; // [rsp+6h] [rbp-52h]
int v16; // [rsp+18h] [rbp-40h] BYREF
__int16 v17; // [rsp+1Ch] [rbp-3Ch] BYREF
char v18[57]; // [rsp+1Fh] [rbp-39h] BYREF

v18[0] = 0;
v17 = 0;
v16 = 0;
v4 = buffer_init();
if ( !v4 )
{
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");
return;
}
v5 = (_QWORD *)v4;
list_iterator_stop(a1);
list_iterator_start(a1);
v15 = 0;
v6 = 0LL;
v7 = 0LL;
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v10 = list_iterator_next(a1);
v11 = v10;
if ( !*(_BYTE *)(v10 + 12) )
{
if ( !v7 )
v7 = *(_QWORD *)(v10 + 16);
if ( !v6 )
v6 = *(_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 )
{
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));
v8 = (const char *)&unk_C05;
if ( !*(_BYTE *)(v11 + 12) )
v8 = (const char *)&unk_C09;
fprintf(stdout, " Sent : %s\n", v8);
fprintf(stdout, " Hash : 0x%08lx\n", *(unsigned int *)(v11 + 8));
fprintf(stdout, " Text : %s\n", *(const char **)v11);
v9 = *(_QWORD *)(v11 + 16);
if ( v7 > v9 )
v7 = *(_QWORD *)(v11 + 16);
if ( v6 < v9 )
v6 = *(_QWORD *)(v11 + 16);
if ( *(_BYTE *)(v11 + 27) )
{
LABEL_41:
if ( v15 )
{
if ( v5[1] <= 0x190uLL )
goto LABEL_25;
buffer_seek(v5, 2LL);
v16 = _bswap_32(v7);
buffer_write(v5, &v16, 4LL);
v16 = _bswap_32(v6);
buffer_write(v5, &v16, 4LL);
v17 = _bswap_16(v15);
buffer_write(v5, &v17, 2LL);
if ( gDebug )
{
fwrite("falcon: [MID] alarms were found\n", 1uLL, 0x20uLL, stdout);
fwrite("[MID] raw buffer:\n", 1uLL, 0x12uLL, stdout);
format_data(*v5, v5[1], 0LL, 0LL);
}
QueueOpaque(*v5, v5[1], *a3, a3[1], a3[2], a3[4], "FALC~");
buffer_destroy(v5);
v7 = *(_QWORD *)(v11 + 16);
v5 = (_QWORD *)buffer_init();
if ( !v5 )
{
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");
return;
}
v6 = v7;
}
if ( gDebug )
fwrite("falcon: Writing alarm header info.\n", 1uLL, 0x23uLL, stderr);
v17 = _bswap_16(32769);
buffer_write(v5, &v17, 2LL);
v16 = _bswap_32(v7);
buffer_write(v5, &v16, 4LL);
v16 = _bswap_32(v6);
buffer_write(v5, &v16, 4LL);
v17 = _bswap_16(0);
buffer_write(v5, &v17, 2LL);
v15 = 0;
LABEL_25:
v17 = _bswap_16(*(_WORD *)(v11 + 24));
buffer_write(v5, &v17, 2LL);
v16 = _bswap_32(*(_DWORD *)(v11 + 16));
buffer_write(v5, &v16, 4LL);
buffer_write(v5, v11 + 26, 1LL);
v18[0] = strlen((const char *)(v11 + 27));
buffer_write(v5, v18, 1LL);
buffer_write(v5, v11 + 27, (unsigned __int8)v18[0]);
buffer_terminate(v5);
++v15;
*(_BYTE *)(v11 + 12) = 1;
}
else
{
if ( !gDebug )
goto LABEL_19;
fwrite("falcon: description code not found\n", 1uLL, 0x23uLL, stderr);
}
}
else
{
v13 = *(_QWORD *)(v11 + 16);
if ( v7 > v13 )
v7 = *(_QWORD *)(v11 + 16);
if ( v6 < v13 )
v6 = *(_QWORD *)(v11 + 16);
if ( *(_BYTE *)(v11 + 27) )
goto LABEL_41;
LABEL_19:
syslog(3, "falcon: description code not found\n");
}
}
}
list_iterator_stop(a1);
if ( v15 && *v5 && v5[1] )
{
buffer_seek(v5, 2LL);
v16 = _bswap_32(v7);
buffer_write(v5, &v16, 4LL);
v16 = _bswap_32(v6);
buffer_write(v5, &v16, 4LL);
v17 = _bswap_16(v15);
buffer_write(v5, &v17, 2LL);
if ( gDebug )
{
fwrite("falcon: [END] alarms were found\n", 1uLL, 0x20uLL, stderr);
fwrite("[END] raw buffer:\n", 1uLL, 0x12uLL, stderr);
format_data(*v5, v5[1], 0LL, 0LL);
}
QueueOpaque(*v5, v5[1], *a3, a3[1], a3[2], a3[4], "FALC~");
}
buffer_destroy(v5);
while ( (unsigned int)list_size(a1) > 0x64 )
{
v14 = list_fetch(a1);
alarm_line_destroy(v14);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v4; // rax
_QWORD *v5; // rbp
__int64 v6; // r13
__int64 v7; // r12
const char *v8; // rdx
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rbx
const char *v12; // rax
__int64 v13; // rax
__int64 v14; // rax
__int16 v15; // [rsp+6h] [rbp-52h]
int v16; // [rsp+18h] [rbp-40h] BYREF
__int16 v17; // [rsp+1Ch] [rbp-3Ch] BYREF
char v18[57]; // [rsp+1Fh] [rbp-39h] BYREF

v18[0] = 0;
v17 = 0;
v16 = 0;
v4 = buffer_init();
if ( !v4 )
{
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");
return;
}
v5 = (_QWORD *)v4;
list_iterator_stop(a1);
list_iterator_start(a1);
v15 = 0;
v6 = 0LL;
v7 = 0LL;
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v10 = list_iterator_next(a1);
v11 = v10;
if ( !*(_BYTE *)(v10 + 12) )
{
if ( !v7 )
v7 = *(_QWORD *)(v10 + 16);
if ( !v6 )
v6 = *(_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 )
{
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));
v8 = (const char *)&unk_C05;
if ( !*(_BYTE *)(v11 + 12) )
v8 = (const char *)&unk_C09;
fprintf(stdout, " Sent : %s\n", v8);
fprintf(stdout, " Hash : 0x%08lx\n", *(unsigned int *)(v11 + 8));
fprintf(stdout, " Text : %s\n", *(const char **)v11);
v9 = *(_QWORD *)(v11 + 16);
if ( v7 > v9 )
v7 = *(_QWORD *)(v11 + 16);
if ( v6 < v9 )
v6 = *(_QWORD *)(v11 + 16);
if ( *(_BYTE *)(v11 + 27) )
{
LABEL_41:
if ( v15 )
{
if ( v5[1] <= 0x190uLL )
goto LABEL_25;
buffer_seek(v5, 2LL);
v16 = func0(v7);
buffer_write(v5, &v16, 4LL);
v16 = func0(v6);
buffer_write(v5, &v16, 4LL);
v17 = func1(v15);
buffer_write(v5, &v17, 2LL);
if ( gDebug )
{
fwrite("falcon: [MID] alarms were found\n", 1uLL, 0x20uLL, stdout);
fwrite("[MID] raw buffer:\n", 1uLL, 0x12uLL, stdout);
format_data(*v5, v5[1], 0LL, 0LL);
}
QueueOpaque(*v5, v5[1], *a3, a3[1], a3[2], a3[4], "FALC~");
buffer_destroy(v5);
v7 = *(_QWORD *)(v11 + 16);
v5 = (_QWORD *)buffer_init();
if ( !v5 )
{
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");
return;
}
v6 = v7;
}
if ( gDebug )
fwrite("falcon: Writing alarm header info.\n", 1uLL, 0x23uLL, stderr);
v17 = func1(32769);
buffer_write(v5, &v17, 2LL);
v16 = func0(v7);
buffer_write(v5, &v16, 4LL);
v16 = func0(v6);
buffer_write(v5, &v16, 4LL);
v17 = func1(0);
buffer_write(v5, &v17, 2LL);
v15 = 0;
LABEL_25:
v17 = func1(*(_WORD *)(v11 + 24));
buffer_write(v5, &v17, 2LL);
v16 = func0(*(_DWORD *)(v11 + 16));
buffer_write(v5, &v16, 4LL);
buffer_write(v5, v11 + 26, 1LL);
v18[0] = strlen((const char *)(v11 + 27));
buffer_write(v5, v18, 1LL);
buffer_write(v5, v11 + 27, (unsigned __int8)v18[0]);
buffer_terminate(v5);
++v15;
*(_BYTE *)(v11 + 12) = 1;
}
else
{
if ( !gDebug )
goto LABEL_19;
fwrite("falcon: description code not found\n", 1uLL, 0x23uLL, stderr);
}
}
else
{
v13 = *(_QWORD *)(v11 + 16);
if ( v7 > v13 )
v7 = *(_QWORD *)(v11 + 16);
if ( v6 < v13 )
v6 = *(_QWORD *)(v11 + 16);
if ( *(_BYTE *)(v11 + 27) )
goto LABEL_41;
LABEL_19:
syslog(3, "falcon: description code not found\n");
}
}
}
list_iterator_stop(a1);
if ( v15 && *v5 && v5[1] )
{
buffer_seek(v5, 2LL);
v16 = func0(v7);
buffer_write(v5, &v16, 4LL);
v16 = func0(v6);
buffer_write(v5, &v16, 4LL);
v17 = func1(v15);
buffer_write(v5, &v17, 2LL);
if ( gDebug )
{
fwrite("falcon: [END] alarms were found\n", 1uLL, 0x20uLL, stderr);
fwrite("[END] raw buffer:\n", 1uLL, 0x12uLL, stderr);
format_data(*v5, v5[1], 0LL, 0LL);
}
QueueOpaque(*v5, v5[1], *a3, a3[1], a3[2], a3[4], "FALC~");
}
buffer_destroy(v5);
while ( (unsigned int)list_size(a1) > 0x64 )
{
v14 = list_fetch(a1);
alarm_line_destroy(v14);
}
}

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; // rbx
const char **v6; // rbp

v5 = (_QWORD *)buffer_init();
buffer_write(v5, *a2, a2[1]);
buffer_write(v5, "/data/alarmhistory.txt", 22LL);
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; // rbx
const char **v6; // rbp

v5 = (_QWORD *)buffer_init();
buffer_write(v5, *a2, a2[1]);
buffer_write(v5, "/data/alarmhistory.txt", 22LL);
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

return_type: long


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_258 = 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_258 = 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)
{
__int64 v3; // rax
__int16 v4; // dx
__int64 v5; // rcx

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

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

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

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; // rdx
unsigned __int8 v3; // al
char v5[124]; // [rsp+10h] [rbp-B8h] BYREF
unsigned int v6; // [rsp+8Ch] [rbp-3Ch] BYREF
int v7; // [rsp+90h] [rbp-38h] BYREF
int v8; // [rsp+94h] [rbp-34h] BYREF
int v9; // [rsp+98h] [rbp-30h] BYREF
int v10; // [rsp+9Ch] [rbp-2Ch] BYREF
int v11; // [rsp+A0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func4(char *s)
{
size_t i; // rbx
const unsigned __int16 **v2; // rdx
unsigned __int8 v3; // al
char v5[124]; // [rsp+10h] [rbp-B8h] BYREF
unsigned int v6; // [rsp+8Ch] [rbp-3Ch] BYREF
int v7; // [rsp+90h] [rbp-38h] BYREF
int v8; // [rsp+94h] [rbp-34h] BYREF
int v9; // [rsp+98h] [rbp-30h] BYREF
int v10; // [rsp+9Ch] [rbp-2Ch] BYREF
int v11; // [rsp+A0h] [rbp-28h] BYREF

for ( i = 0LL; strlen(s) > i; ++i )
{
v2 = _ctype_b_loc();
v3 = s[i];
if ( ((*v2)[v3] & 0x100) != 0 )
v3 = (*_ctype_tolower_loc())[v3];
v5[i] = v3;
}
v5[(int)i] = 0;
v11 = 0;
v10 = 0;
v9 = 0;
v8 = 0;
v7 = 0;
v6 = 0;
_isoc99_sscanf(v5, "%d,%d,%d:%d:%d.%d", &v11, &v10, &v9, &v8, &v7, &v6);
LOWORD(setuptime) = v11;
WORD1(setuptime) = v10;
BYTE4(setuptime) = v9;
BYTE5(setuptime) = v8;
BYTE6(setuptime) = v7;
word_258 = v6;
return ST_CleanDate2(setuptime, v6, 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]

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


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
__int16 v1; // bx
__int16 v2; // r15
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
__int16 v7; // cx
int v8; // edx
int v9; // eax
time_t timer; // [rsp+18h] [rbp-50h] BYREF
__int64 v12; // [rsp+26h] [rbp-42h]

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

canonical_decompiled_code:
__int64 func0()
{
struct tm *v0; // rax
__int16 v1; // bx
__int16 v2; // r15
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
__int16 v7; // cx
int v8; // edx
int v9; // eax
time_t timer; // [rsp+18h] [rbp-50h] BYREF
__int64 v12; // [rsp+26h] [rbp-42h]

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

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


decompiled_code:
__int64 __fastcall csv_archive(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // eax
__int64 v7; // rbx
struct tm *v8; // rax
char v10[40]; // [rsp+0h] [rbp-68h] BYREF
__int64 v11; // [rsp+28h] [rbp-40h] BYREF
__int64 v12; // [rsp+30h] [rbp-38h] BYREF
void *ptr; // [rsp+38h] [rbp-30h] BYREF

v12 = 0LL;
v11 = 0LL;
((void (*)(void))list_iterator_stop)();
list_iterator_start(a1);
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v4 = list_iterator_next(a1);
v5 = v4;
if ( gDebug )
printf(
"%s[%d]: elapsed time %d <> %d TM_HOUR\n",
*(const char **)(*(_QWORD *)(v4 + 8) + 8LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 8) + 24LL),
(unsigned int)*(_QWORD *)(v4 + 32) - *(_DWORD *)(v4 + 24),
3600LL);
while ( (__int64)(*(_QWORD *)(v5 + 32) - *(_QWORD *)(v5 + 24)) > 3599 )
{
fmash_csv_to_msh(v5, &ptr, &v12);
if ( gDebug )
{
fprintf(stdout, "compressed data size is %lu bytes\n", v12);
format_data(ptr, v12, 0LL, 0LL);
if ( (unsigned int)fmash_msh_to_csv(&v11, ptr, v12) )
{
if ( v11 )
{
if ( *(_QWORD *)(v11 + 8) )
{
if ( *(_QWORD *)(v11 + 16) )
{
fwrite("=== FMASH VERIFICATION =================================\n", 1uLL, 0x39uLL, stdout);
fprintf(stdout, " file: %s\n", *(const char **)v5);
fprintf(stdout, " channel: %d\n", *(unsigned __int16 *)(*(_QWORD *)(v5 + 8) + 24LL));
fprintf(stdout, " description: %s\n", *(const char **)(*(_QWORD *)(v5 + 8) + 8LL));
v6 = list_size(*(_QWORD *)(v11 + 16));
fprintf(stdout, " lines: %d\n", v6);
list_iterator_stop(*(_QWORD *)(v11 + 16));
list_iterator_start(*(_QWORD *)(v11 + 16));
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
fwrite(" | Timestamp | Average | High | Low |\n", 1uLL, 0x44uLL, stdout);
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
while ( (unsigned int)list_iterator_hasnext(*(_QWORD *)(v11 + 16)) )
{
v7 = list_iterator_next(*(_QWORD *)(v11 + 16));
v8 = gmtime((const time_t *)v7);
strftime(v10, 0x1FuLL, "%Y/%m/%d %H:%M:%S", v8);
printf(
" | %s | % 9d | % 9d | % 9d |\n",
v10,
*(unsigned int *)(v7 + 8),
*(unsigned int *)(v7 + 12),
*(unsigned int *)(v7 + 16));
}
putchar(10);
list_iterator_stop(*(_QWORD *)(v11 + 16));
v11 = csv_buffer_destroy(v11);
fflush(stdout);
}
else
{
fwrite("final_csv->list not created\n", 1uLL, 0x1CuLL, stdout);
}
}
else
{
fwrite("final_csv->header not created\n", 1uLL, 0x1EuLL, stdout);
}
}
else
{
fwrite("final_csv not created\n", 1uLL, 0x16uLL, stdout);
}
}
else
{
fwrite("FMash to CSV conversion failed\n", 1uLL, 0x1FuLL, stdout);
}
}
if ( ptr )
{
if ( v12 )
{
QueueOpaque(ptr, v12, *a3, a3[1], a3[3], a3[4], "FALC~");
free(ptr);
ptr = 0LL;
}
}
}
}
return list_iterator_stop(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // eax
__int64 v7; // rbx
struct tm *v8; // rax
char v10[40]; // [rsp+0h] [rbp-68h] BYREF
__int64 v11; // [rsp+28h] [rbp-40h] BYREF
__int64 v12; // [rsp+30h] [rbp-38h] BYREF
void *ptr; // [rsp+38h] [rbp-30h] BYREF

v12 = 0LL;
v11 = 0LL;
((void (*)(void))list_iterator_stop)();
list_iterator_start(a1);
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v4 = list_iterator_next(a1);
v5 = v4;
if ( gDebug )
printf(
"%s[%d]: elapsed time %d <> %d TM_HOUR\n",
*(const char **)(*(_QWORD *)(v4 + 8) + 8LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 8) + 24LL),
(unsigned int)*(_QWORD *)(v4 + 32) - *(_DWORD *)(v4 + 24),
3600LL);
while ( (__int64)(*(_QWORD *)(v5 + 32) - *(_QWORD *)(v5 + 24)) > 3599 )
{
fmash_csv_to_msh(v5, &ptr, &v12);
if ( gDebug )
{
fprintf(stdout, "compressed data size is %lu bytes\n", v12);
format_data(ptr, v12, 0LL, 0LL);
if ( (unsigned int)fmash_msh_to_csv(&v11, ptr, v12) )
{
if ( v11 )
{
if ( *(_QWORD *)(v11 + 8) )
{
if ( *(_QWORD *)(v11 + 16) )
{
fwrite("=== FMASH VERIFICATION =================================\n", 1uLL, 0x39uLL, stdout);
fprintf(stdout, " file: %s\n", *(const char **)v5);
fprintf(stdout, " channel: %d\n", *(unsigned __int16 *)(*(_QWORD *)(v5 + 8) + 24LL));
fprintf(stdout, " description: %s\n", *(const char **)(*(_QWORD *)(v5 + 8) + 8LL));
v6 = list_size(*(_QWORD *)(v11 + 16));
fprintf(stdout, " lines: %d\n", v6);
list_iterator_stop(*(_QWORD *)(v11 + 16));
list_iterator_start(*(_QWORD *)(v11 + 16));
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
fwrite(" | Timestamp | Average | High | Low |\n", 1uLL, 0x44uLL, stdout);
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
while ( (unsigned int)list_iterator_hasnext(*(_QWORD *)(v11 + 16)) )
{
v7 = list_iterator_next(*(_QWORD *)(v11 + 16));
v8 = gmtime((const time_t *)v7);
strftime(v10, 0x1FuLL, "%Y/%m/%d %H:%M:%S", v8);
printf(
" | %s | % 9d | % 9d | % 9d |\n",
v10,
*(unsigned int *)(v7 + 8),
*(unsigned int *)(v7 + 12),
*(unsigned int *)(v7 + 16));
}
putchar(10);
list_iterator_stop(*(_QWORD *)(v11 + 16));
v11 = csv_buffer_destroy(v11);
fflush(stdout);
}
else
{
fwrite("final_csv->list not created\n", 1uLL, 0x1CuLL, stdout);
}
}
else
{
fwrite("final_csv->header not created\n", 1uLL, 0x1EuLL, stdout);
}
}
else
{
fwrite("final_csv not created\n", 1uLL, 0x16uLL, stdout);
}
}
else
{
fwrite("FMash to CSV conversion failed\n", 1uLL, 0x1FuLL, stdout);
}
}
if ( ptr )
{
if ( v12 )
{
QueueOpaque(ptr, v12, *a3, a3[1], a3[3], a3[4], "FALC~");
free(ptr);
ptr = 0LL;
}
}
}
}
return list_iterator_stop(a1);
}

original_code:
void csv_archive( csv_context_t* csv_buffer_list, buffer_t* url_str,
                  st_info_t* st_info )
{
    csv_buffer_t* csv_buffer;
    csv_row_t* csv_row;
    uint8_t* msh_data;
    size_t msh_length = 0;
    csv_buffer_t* final_csv = ((void *)0);
    char time_string[32];
    list_iterator_stop(csv_buffer_list);
    list_iterator_start(csv_buffer_list);
    while (list_iterator_hasnext(csv_buffer_list))
    {
        csv_buffer = (csv_buffer_t*)list_iterator_next(csv_buffer_list);
        if (gDebug)
        {
          printf("%s[%d]: elapsed time %d <> %d TM_HOUR\n",
            csv_buffer->header->description, csv_buffer->header->channel,
            (int)(csv_buffer->end_time - csv_buffer->start_time), 3600);
        }
        while ((csv_buffer->end_time - csv_buffer->start_time) >= 3600)
        {
            fmash_csv_to_msh(csv_buffer, &msh_data, &msh_length);
            if (gDebug) {
                fprintf(stdout, "compressed data size is %lu bytes\n", (unsigned long)msh_length);
                format_data(msh_data, msh_length, 0, 0);
                if (!fmash_msh_to_csv( &final_csv, msh_data, msh_length )) {
                    fprintf(stdout, "FMash to CSV conversion failed\n");
                    goto queue_it;
                }
                if (!final_csv) {
                    fprintf(stdout, "final_csv not created\n");
                    goto queue_it;
                }
                if (!final_csv->header) {
                    fprintf(stdout, "final_csv->header not created\n");
                    goto queue_it;
                }
                if (!final_csv->list) {
                    fprintf(stdout, "final_csv->list not created\n");
                    goto queue_it;
                }
                fprintf(stdout, "=== FMASH VERIFICATION =================================\n");
                fprintf(stdout, "    file:         %s\n", csv_buffer->file_name);
                fprintf(stdout, "    channel:      %d\n", csv_buffer->header->channel);
                fprintf(stdout, "    description:  %s\n", csv_buffer->header->description);
                fprintf(stdout, "    lines:        %d\n", list_size(final_csv->list));
                list_iterator_stop(final_csv->list);
                list_iterator_start(final_csv->list);
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                    fprintf(stdout, "       | Timestamp            | Average   | High      | Low       |\n");
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                while (list_iterator_hasnext(final_csv->list))
                {
                    csv_row = (csv_row_t*)list_iterator_next(final_csv->list);
                    strftime(time_string, 31, "%Y/%m/%d %H:%M:%S", gmtime(&(csv_row->timestamp)));
                    printf("       | %s | % 9d | % 9d | % 9d |\n", time_string,
                           csv_row->average, csv_row->high, csv_row->low );
                }
                printf("\n");
                list_iterator_stop(final_csv->list);
                final_csv = csv_buffer_destroy(final_csv);
                fflush(stdout);
            }
 queue_it:
            if (msh_data && msh_length) {
                QueueOpaque(msh_data, msh_length, st_info->station,
                            st_info->network, st_info->csv_chan,
                            st_info->location, "FALC~");
                free(msh_data);
                msh_data = ((void *)0);
            }
        }
        csv_buffer = ((void *)0);
    }
    list_iterator_stop(csv_buffer_list);
}

canonical_original_code:
void csv_archive(struct list_t *csv_buffer_list, struct _buffer *url_str, struct st_info_t *st_info)
{
    struct csv_buffer_t *csv_buffer;
    struct csv_row_t *csv_row;
    unsigned char *msh_data;
    unsigned long msh_length = 0;
    struct csv_buffer_t *final_csv = ((void *)0);
    char time_string[32];
    list_iterator_stop(csv_buffer_list);
    list_iterator_start(csv_buffer_list);
    while (list_iterator_hasnext(csv_buffer_list))
    {
        csv_buffer = (struct csv_buffer_t *)list_iterator_next(csv_buffer_list);
        if (gDebug)
        {
          printf("%s[%d]: elapsed time %d <> %d TM_HOUR\n",
            csv_buffer->header->description, csv_buffer->header->channel,
            (int)(csv_buffer->end_time - csv_buffer->start_time), 3600);
        }
        while ((csv_buffer->end_time - csv_buffer->start_time) >= 3600)
        {
            fmash_csv_to_msh(csv_buffer, &msh_data, &msh_length);
            if (gDebug) {
                fprintf(stdout, "compressed data size is %lu bytes\n", (unsigned long)msh_length);
                format_data(msh_data, msh_length, 0, 0);
                if (!fmash_msh_to_csv( &final_csv, msh_data, msh_length )) {
                    fprintf(stdout, "FMash to CSV conversion failed\n");
                    goto queue_it;
                }
                if (!final_csv) {
                    fprintf(stdout, "final_csv not created\n");
                    goto queue_it;
                }
                if (!final_csv->header) {
                    fprintf(stdout, "final_csv->header not created\n");
                    goto queue_it;
                }
                if (!final_csv->list) {
                    fprintf(stdout, "final_csv->list not created\n");
                    goto queue_it;
                }
                fprintf(stdout, "=== FMASH VERIFICATION =================================\n");
                fprintf(stdout, "    file:         %s\n", csv_buffer->file_name);
                fprintf(stdout, "    channel:      %d\n", csv_buffer->header->channel);
                fprintf(stdout, "    description:  %s\n", csv_buffer->header->description);
                fprintf(stdout, "    lines:        %d\n", list_size(final_csv->list));
                list_iterator_stop(final_csv->list);
                list_iterator_start(final_csv->list);
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                    fprintf(stdout, "       | Timestamp            | Average   | High      | Low       |\n");
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                while (list_iterator_hasnext(final_csv->list))
                {
                    csv_row = (struct csv_row_t *)list_iterator_next(final_csv->list);
                    strftime(time_string, 31, "%Y/%m/%d %H:%M:%S", gmtime(&(csv_row->timestamp)));
                    printf("       | %s | % 9d | % 9d | % 9d |\n", time_string,
                           csv_row->average, csv_row->high, csv_row->low );
                }
                printf("\n");
                list_iterator_stop(final_csv->list);
                final_csv = csv_buffer_destroy(final_csv);
                fflush(stdout);
            }
 queue_it:
            if (msh_data && msh_length) {
                QueueOpaque(msh_data, msh_length, st_info->station,
                            st_info->network, st_info->csv_chan,
                            st_info->location, "FALC~");
                free(msh_data);
                msh_data = ((void *)0);
            }
        }
        csv_buffer = ((void *)0);
    }
    list_iterator_stop(csv_buffer_list);
}

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: struct csv_buffer_t { char *file_name; struct csv_header_t { char *name; char *description; char *unit_of_measure; unsigned short channel; } *header; 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; } *list; long start_time; long end_time; long last_flush; } *

variable_types: struct csv_row_t { long timestamp; int average; int high; int low; unsigned char empty; } *

user_defined_types: struct csv_buffer_t { char *file_name; struct csv_header_t *header; struct list_t *list; long start_time; long end_time; long last_flush; }


decompiled_code:
void __fastcall csv_poll(__int64 a1, _QWORD *a2, __int64 a3, time_t a4)
{
const char **v4; // r12
void *v5; // rax
void *v6; // rbx
__int64 *v7; // r13
void *v8; // rax
const char **v9; // rbp
__int64 v10; // r14
int v11; // eax
const char *v12; // r14
size_t v13; // rax
__int64 v14; // rax
char v15; // [rsp+Fh] [rbp-79h]
void *ptr; // [rsp+18h] [rbp-70h]
__int64 v18[13]; // [rsp+20h] [rbp-68h] BYREF

v4 = (const char **)buffer_init();
buffer_write(v4, *a2, a2[1]);
buffer_write(v4, "/data/minute", 12LL);
buffer_terminate(v4);
v5 = malloc(0x78uLL);
if ( v5 )
{
v6 = v5;
list_init(v5);
list_attributes_seeker(v6, file_list_seeker);
list_attributes_comparator(v6, file_list_comparator);
v7 = (__int64 *)buffer_init();
get_page(*v4, v7);
if ( (unsigned int)csv_get_file_list((__int64)v6, (const char **)v7) )
{
buffer_reset(v7);
buffer_reset(v4);
while ( !(unsigned int)list_empty(v6) )
{
v10 = list_fetch(v6);
ptr = (void *)v10;
v18[1] = 0LL;
v18[2] = 0LL;
v18[3] = 0LL;
v18[4] = 0LL;
v18[5] = 0LL;
v18[0] = v10;
v11 = list_locate(a1, v18);
if ( v11 >= 0 )
{
v9 = (const char **)list_get_at(a1, (unsigned int)v11);
v15 = 1;
}
else
{
v9 = (const char **)csv_buffer_init();
*v9 = (const char *)v10;
list_append(a1, v9);
v15 = 0;
}
buffer_write(v4, *a2, a2[1]);
v12 = *v9;
v13 = strlen(*v9);
buffer_write(v4, v12, v13);
buffer_terminate(v4);
if ( gDebug )
printf("getting page %s\n", *v4);
get_page(*v4, v7);
v14 = murmur_64_b(*v7, v7[1], 0xDE62D51808ECB849LL);
if ( gDebug )
{
fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n", *v9, v14, v7[1]);
if ( !strcmp("/data/minute/logm1.csv", *v9) )
fprintf(stderr, "'%s'\n", (const char *)*v7);
}
csv_parse_file(v9, v7, a4);
if ( gDebug )
fprintf(stderr, "The CSV buffer contains %u rows\n", *((unsigned int *)v9[2] + 6));
buffer_reset(v7);
buffer_reset(v4);
if ( v15 )
free(ptr);
}
}
buffer_destroy(v7);
buffer_destroy(v4);
while ( !(unsigned int)list_empty(v6) )
{
v8 = (void *)list_fetch(v6);
if ( v8 )
free(v8);
}
list_destroy(v6);
free(v6);
}
else
{
buffer_destroy(0LL);
buffer_destroy(v4);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, _QWORD *a2, __int64 a3, time_t a4)
{
const char **v4; // r12
void *v5; // rax
void *v6; // rbx
__int64 *v7; // r13
void *v8; // rax
const char **v9; // rbp
__int64 v10; // r14
int v11; // eax
const char *v12; // r14
size_t v13; // rax
__int64 v14; // rax
char v15; // [rsp+Fh] [rbp-79h]
void *ptr; // [rsp+18h] [rbp-70h]
__int64 v18[13]; // [rsp+20h] [rbp-68h] BYREF

v4 = (const char **)buffer_init();
buffer_write(v4, *a2, a2[1]);
buffer_write(v4, "/data/minute", 12LL);
buffer_terminate(v4);
v5 = malloc(0x78uLL);
if ( v5 )
{
v6 = v5;
list_init(v5);
list_attributes_seeker(v6, file_list_seeker);
list_attributes_comparator(v6, file_list_comparator);
v7 = (__int64 *)buffer_init();
get_page(*v4, v7);
if ( (unsigned int)func4((__int64)v6, (const char **)v7) )
{
buffer_reset(v7);
buffer_reset(v4);
while ( !(unsigned int)list_empty(v6) )
{
v10 = list_fetch(v6);
ptr = (void *)v10;
v18[1] = 0LL;
v18[2] = 0LL;
v18[3] = 0LL;
v18[4] = 0LL;
v18[5] = 0LL;
v18[0] = v10;
v11 = list_locate(a1, v18);
if ( v11 >= 0 )
{
v9 = (const char **)list_get_at(a1, (unsigned int)v11);
v15 = 1;
}
else
{
v9 = (const char **)csv_buffer_init();
*v9 = (const char *)v10;
list_append(a1, v9);
v15 = 0;
}
buffer_write(v4, *a2, a2[1]);
v12 = *v9;
v13 = strlen(*v9);
buffer_write(v4, v12, v13);
buffer_terminate(v4);
if ( gDebug )
printf("getting page %s\n", *v4);
get_page(*v4, v7);
v14 = murmur_64_b(*v7, v7[1], 0xDE62D51808ECB849LL);
if ( gDebug )
{
fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n", *v9, v14, v7[1]);
if ( !strcmp("/data/minute/logm1.csv", *v9) )
fprintf(stderr, "'%s'\n", (const char *)*v7);
}
func3(v9, v7, a4);
if ( gDebug )
fprintf(stderr, "The CSV buffer contains %u rows\n", *((unsigned int *)v9[2] + 6));
buffer_reset(v7);
buffer_reset(v4);
if ( v15 )
free(ptr);
}
}
buffer_destroy(v7);
buffer_destroy(v4);
while ( !(unsigned int)list_empty(v6) )
{
v8 = (void *)list_fetch(v6);
if ( v8 )
free(v8);
}
list_destroy(v6);
free(v6);
}
else
{
buffer_destroy(0LL);
buffer_destroy(v4);
}
}

original_code:
void csv_poll( csv_context_t* csv_buffer_list, buffer_t* url_str,
               st_info_t* st_info, time_t initial_time )
{
    list_t* file_list = ((void *)0);
    buffer_t* buf = ((void *)0);
    buffer_t* url = ((void *)0);
    char* file_name = ((void *)0);
    const char* path = "/data/minute";
    csv_buffer_t csv_tmp;
    csv_buffer_t* csv_buffer;
    int location = 0;
    uint64_t file_hash = 0LL;
    uint8_t buffer_found = 0;
    int tally = 0;
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (uint8_t*)path, strlen(path));
    buffer_terminate(url);
    file_list = (list_t*)malloc(sizeof(list_t));
    if (!file_list)
        goto clean;
    list_init(file_list);
    list_attributes_seeker( file_list, _file_list_seeker );
    list_attributes_comparator( file_list, _file_list_comparator );
    buf = buffer_init();
    get_page((char*)url->content, buf);
    if (!csv_get_file_list(file_list, buf))
        goto clean;
    buffer_reset(buf);
    buffer_reset(url);
    while (!list_empty(file_list))
    {
        tally++;
        file_name = (char*)list_fetch(file_list);
        memset(&csv_tmp, 0, sizeof(csv_tmp));
        csv_tmp.file_name = file_name;
        if ((location = list_locate(csv_buffer_list, &csv_tmp)) < 0)
        {
            csv_buffer = csv_buffer_init();
            csv_buffer->file_name = file_name;
            list_append(csv_buffer_list, csv_buffer);
            buffer_found = 0;
        } else {
            csv_buffer = (csv_buffer_t*)list_get_at(csv_buffer_list, location);
            buffer_found = 1;
        }
        buffer_write(url, url_str->content, url_str->length);
        buffer_write(url, (uint8_t*)csv_buffer->file_name,
                     strlen(csv_buffer->file_name));
        buffer_terminate(url);
        if (gDebug) {
            printf("getting page %s\n", url->content);
        }
        get_page((char*)url->content, buf);
        file_hash = murmur_64_b( buf->content, buf->length, 0xde62d51808ecb849LL );
        if (gDebug) {
            fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n",
                   csv_buffer->file_name, (unsigned long long)file_hash,
                   (unsigned long)buf->length);
            if (strcmp("/data/minute/logm1.csv", csv_buffer->file_name) == 0)
              fprintf(stderr, "'%s'\n", buf->content);
        }
        csv_parse_file(csv_buffer, buf, initial_time);
        if (gDebug) {
            fprintf(stderr, "The CSV buffer contains %u rows\n", csv_buffer->list->numels);
        }
        buffer_reset(buf);
        buffer_reset(url);
        if (buffer_found) {
            free(file_name);
        }
        file_name = ((void *)0);
        csv_buffer = ((void *)0);
    }
clean:
    buf = buffer_destroy(buf);
    url = buffer_destroy(url);
    if (file_list)
    {
        while(!list_empty(file_list))
        {
            file_name = list_fetch(file_list);
            if (file_name)
            {
                free(file_name);
            }
        }
        list_destroy(file_list);
        free(file_list);
    }
}

canonical_original_code:
void csv_poll(struct list_t *csv_buffer_list, struct _buffer *url_str, struct st_info_t *st_info, long initial_time)
{
    struct list_t *file_list = ((void *)0);
    struct _buffer *buf = ((void *)0);
    struct _buffer *url = ((void *)0);
    char *file_name = ((void *)0);
    char *path = "/data/minute";
    struct csv_buffer_t csv_tmp;
    struct csv_buffer_t *csv_buffer;
    int location = 0;
    unsigned long file_hash = 0LL;
    unsigned char buffer_found = 0;
    int tally = 0;
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (unsigned char *)path, strlen(path));
    buffer_terminate(url);
    file_list = (struct list_t *)malloc(sizeof(list_t));
    if (!file_list)
        goto clean;
    list_init(file_list);
    list_attributes_seeker( file_list, _file_list_seeker );
    list_attributes_comparator( file_list, _file_list_comparator );
    buf = buffer_init();
    get_page((char *)url->content, buf);
    if (!csv_get_file_list(file_list, buf))
        goto clean;
    buffer_reset(buf);
    buffer_reset(url);
    while (!list_empty(file_list))
    {
        tally++;
        file_name = (char *)list_fetch(file_list);
        memset(&csv_tmp, 0, sizeof(csv_tmp));
        csv_tmp.file_name = file_name;
        if ((location = list_locate(csv_buffer_list, &csv_tmp)) < 0)
        {
            csv_buffer = csv_buffer_init();
            csv_buffer->file_name = file_name;
            list_append(csv_buffer_list, csv_buffer);
            buffer_found = 0;
        } else {
            csv_buffer = (struct csv_buffer_t *)list_get_at(csv_buffer_list, location);
            buffer_found = 1;
        }
        buffer_write(url, url_str->content, url_str->length);
        buffer_write(url, (unsigned char *)csv_buffer->file_name,
                     strlen(csv_buffer->file_name));
        buffer_terminate(url);
        if (gDebug) {
            printf("getting page %s\n", url->content);
        }
        get_page((char *)url->content, buf);
        file_hash = murmur_64_b( buf->content, buf->length, 0xde62d51808ecb849LL );
        if (gDebug) {
            fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n",
                   csv_buffer->file_name, (unsigned long long)file_hash,
                   (unsigned long)buf->length);
            if (strcmp("/data/minute/logm1.csv", csv_buffer->file_name) == 0)
              fprintf(stderr, "'%s'\n", buf->content);
        }
        csv_parse_file(csv_buffer, buf, initial_time);
        if (gDebug) {
            fprintf(stderr, "The CSV buffer contains %u rows\n", csv_buffer->list->numels);
        }
        buffer_reset(buf);
        buffer_reset(url);
        if (buffer_found) {
            free(file_name);
        }
        file_name = ((void *)0);
        csv_buffer = ((void *)0);
    }
clean:
    buf = buffer_destroy(buf);
    url = buffer_destroy(url);
    if (file_list)
    {
        while(!list_empty(file_list))
        {
            file_name = list_fetch(file_list);
            if (file_name)
            {
                free(file_name);
            }
        }
        list_destroy(file_list);
        free(file_list);
    }
}

variable_types:
path
char *

variable_types: struct csv_buffer_t { char *file_name; struct csv_header_t { char *name; char *description; char *unit_of_measure; unsigned short channel; } *header; 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; } *list; long start_time; long end_time; long last_flush; }

variable_types: location int

variable_types: tally int