main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx
int v5; // eax
unsigned int v6; // ebx
int v7; // ebx
char *v8; // rax
char *v9; // rax
const char *v10; // rbp
char *v11; // rbp
char *v12; // r12
char *v13; // rbp
char *v14; // r12
char *v15; // rax
char *v16; // rax
const char *v17; // rbx
double v18; // rbp
double v19; // xmm0_8
char *v20; // rax
char *v21; // rax
const char *v22; // rbp
char v24; // al
char v25[11]; // [rsp+7h] [rbp-561h] BYREF
char v26[9]; // [rsp+12h] [rbp-556h] BYREF
char v27[21]; // [rsp+1Bh] [rbp-54Dh] BYREF
_QWORD v28[10]; // [rsp+30h] [rbp-538h]
char dest[480]; // [rsp+80h] [rbp-4E8h] BYREF
_WORD buf[288]; // [rsp+260h] [rbp-308h] BYREF
struct timeval timeout; // [rsp+4A0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+4B0h] [rbp-B8h] BYREF
struct in_addr inp; // [rsp+53Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+540h] [rbp-28h] BYREF

v28[0] = "unspecified";
v28[1] = "atomic clock";
v28[2] = "VLF radio";
v28[3] = "HF radio";
v28[4] = &unk_40202B;
v28[5] = "local net";
v28[6] = "NTP server";
v28[7] = "UDP/TIME";
v28[8] = "wristwatch";
v28[9] = "modem";
strcpy(&v27[11], "rootdisp=");
strcpy(v27, "rootdelay=");
strcpy(v26, "stratum=");
strcpy(&v25[7], "tc=");
strcpy(v25, "refid=");
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
inet_aton("127.0.0.1", &inp);
addr.sa_family = 2;
*(struct in_addr *)&addr.sa_data[2] = inp;
*(_WORD *)addr.sa_data = _bswap_16(123);
memset(buf, 0, sizeof(buf));
buf[0] = 534;
buf[1] = _bswap_16(1);
v3 = socket(2, 2, 0);
if ( v3 < 0 )
die("unable to open socket");
v4 = v3;
if ( connect(v3, &addr, 0x10u) < 0 )
{
perror("On connect");
die("unable to connect to socket");
}
readfds.fds_bits[v4 >> 6] |= 1LL << (v4 % 64);
if ( send(v4, buf, 0x240uLL, 0) < 0 )
{
perror("On send");
die("unable to send command to NTP port");
}
v5 = select(v4 + 1, &readfds, 0LL, 0LL, &timeout);
if ( !v5 )
die("timeout");
if ( v5 == -1 )
die("error on select");
if ( (int)recv(v4, buf, 0x240uLL, 0) < 0 )
die("Unable to talk to NTP daemon. Is it running?");
if ( (buf[0] & 0x3F) != 22 || (HIBYTE(buf[0]) & 0xDF) != 130 )
{
fprintf(stderr, "status word is 0x%02x%02x\n", LOBYTE(buf[0]), HIBYTE(buf[0]));
die("return data appears to be invalid based on status word");
}
if ( LOBYTE(buf[2]) >> 6 == 3 )
{
puts("unsynchronised");
v7 = 1;
if ( (HIBYTE(buf[2]) & 0xF) == 1 )
puts(" time server re-starting");
}
else
{
printf("synchronised to ");
v6 = buf[2] & 0x3F;
if ( v6 > 9 )
{
printf("unknown source");
v7 = 0;
}
else
{
printf("%s", (const char *)v28[buf[2] & 0x3F]);
if ( v6 == 6 )
{
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v15 = strstr(dest, v25);
if ( v15 )
{
v16 = strtok(v15 + 6, ",");
v17 = v16;
if ( *v16 && strlen(v16) <= 0x10 )
{
printf(" (%s)", v17);
v7 = 0;
}
else
{
printf(" <IP unreadable>");
v7 = 0;
}
}
else
{
printf(" <IP unknown>");
v7 = 1;
}
}
else
{
v7 = 0;
}
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v8 = strstr(dest, v26);
if ( v8 )
{
v9 = strtok(v8 + 8, ",");
v10 = v9;
if ( *v9 && strlen(v9) <= 2 )
printf(" at stratum %s \n", v10);
else
puts(", stratum unreadable");
}
else
{
puts(", stratum unknown");
v7 = 1;
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v11 = strstr(dest, &v27[11]);
if ( v11 && (v12 = strstr(dest, v27)) != 0LL )
{
v13 = strtok(v11 + 9, ",");
v14 = strtok(v12 + 10, ",");
if ( *v13 && strlen(v13) <= 0xA && *v14 && strlen(v14) <= 0xA )
{
v18 = atof(v13);
v19 = atof(v14);
printf(" time correct to within %.0f ms\n", v19 * 0.5 + v18);
}
else
{
puts("accuracy unreadable");
}
}
else
{
puts("accuracy unknown");
v7 = 1;
}
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v20 = strstr(dest, &v25[7]);
if ( v20 )
{
v21 = strtok(v20 + 3, ",");
v22 = v21;
if ( *v21 && strlen(v21) <= 2 )
{
v24 = atoi(v22);
printf(" polling server every %d s\n", (unsigned int)(1 << v24));
}
else
{
puts("poll interval unreadable");
}
}
else
{
puts("poll interval unknown");
v7 = 1;
}
return v7;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx
int v5; // eax
unsigned int v6; // ebx
int v7; // ebx
char *v8; // rax
char *v9; // rax
const char *v10; // rbp
char *v11; // rbp
char *v12; // r12
char *v13; // rbp
char *v14; // r12
char *v15; // rax
char *v16; // rax
const char *v17; // rbx
double v18; // rbp
double v19; // xmm0_8
char *v20; // rax
char *v21; // rax
const char *v22; // rbp
char v24; // al
char v25[11]; // [rsp+7h] [rbp-561h] BYREF
char v26[9]; // [rsp+12h] [rbp-556h] BYREF
char v27[21]; // [rsp+1Bh] [rbp-54Dh] BYREF
_QWORD v28[10]; // [rsp+30h] [rbp-538h]
char dest[480]; // [rsp+80h] [rbp-4E8h] BYREF
_WORD buf[288]; // [rsp+260h] [rbp-308h] BYREF
struct timeval timeout; // [rsp+4A0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+4B0h] [rbp-B8h] BYREF
struct in_addr inp; // [rsp+53Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+540h] [rbp-28h] BYREF

v28[0] = "unspecified";
v28[1] = "atomic clock";
v28[2] = "VLF radio";
v28[3] = "HF radio";
v28[4] = &unk_40202B;
v28[5] = "local net";
v28[6] = "NTP server";
v28[7] = "UDP/TIME";
v28[8] = "wristwatch";
v28[9] = "modem";
strcpy(&v27[11], "rootdisp=");
strcpy(v27, "rootdelay=");
strcpy(v26, "stratum=");
strcpy(&v25[7], "tc=");
strcpy(v25, "refid=");
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
inet_aton("127.0.0.1", &inp);
addr.sa_family = 2;
*(struct in_addr *)&addr.sa_data[2] = inp;
*(_WORD *)addr.sa_data = func7(123);
memset(buf, 0, sizeof(buf));
buf[0] = 534;
buf[1] = func7(1);
v3 = socket(2, 2, 0);
if ( v3 < 0 )
func8("unable to open socket");
v4 = v3;
if ( connect(v3, &addr, 0x10u) < 0 )
{
perror("On connect");
func8("unable to connect to socket");
}
readfds.fds_bits[v4 >> 6] |= 1LL << (v4 % 64);
if ( send(v4, buf, 0x240uLL, 0) < 0 )
{
perror("On send");
func8("unable to send command to NTP port");
}
v5 = select(v4 + 1, &readfds, 0LL, 0LL, &timeout);
if ( !v5 )
func8("timeout");
if ( v5 == -1 )
func8("error on select");
if ( (int)recv(v4, buf, 0x240uLL, 0) < 0 )
func8("Unable to talk to NTP daemon. Is it running?");
if ( (buf[0] & 0x3F) != 22 || (HIBYTE(buf[0]) & 0xDF) != 130 )
{
fprintf(stderr, "status word is 0x%02x%02x\n", LOBYTE(buf[0]), HIBYTE(buf[0]));
func8("return data appears to be invalid based on status word");
}
if ( LOBYTE(buf[2]) >> 6 == 3 )
{
puts("unsynchronised");
v7 = 1;
if ( (HIBYTE(buf[2]) & 0xF) == 1 )
puts(" time server re-starting");
}
else
{
printf("synchronised to ");
v6 = buf[2] & 0x3F;
if ( v6 > 9 )
{
printf("unknown source");
v7 = 0;
}
else
{
printf("%s", (const char *)v28[buf[2] & 0x3F]);
if ( v6 == 6 )
{
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v15 = strstr(dest, v25);
if ( v15 )
{
v16 = strtok(v15 + 6, ",");
v17 = v16;
if ( *v16 && strlen(v16) <= 0x10 )
{
printf(" (%s)", v17);
v7 = 0;
}
else
{
printf(" <IP unreadable>");
v7 = 0;
}
}
else
{
printf(" <IP unknown>");
v7 = 1;
}
}
else
{
v7 = 0;
}
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v8 = strstr(dest, v26);
if ( v8 )
{
v9 = strtok(v8 + 8, ",");
v10 = v9;
if ( *v9 && strlen(v9) <= 2 )
printf(" at stratum %s \n", v10);
else
puts(", stratum unreadable");
}
else
{
puts(", stratum unknown");
v7 = 1;
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v11 = strstr(dest, &v27[11]);
if ( v11 && (v12 = strstr(dest, v27)) != 0LL )
{
v13 = strtok(v11 + 9, ",");
v14 = strtok(v12 + 10, ",");
if ( *v13 && strlen(v13) <= 0xA && *v14 && strlen(v14) <= 0xA )
{
v18 = atof(v13);
v19 = atof(v14);
printf(" time correct to within %.0f ms\n", v19 * 0.5 + v18);
}
else
{
puts("accuracy unreadable");
}
}
else
{
puts("accuracy unknown");
v7 = 1;
}
}
strncpy(dest, (const char *)&buf[6], 0x1D4uLL);
v20 = strstr(dest, &v25[7]);
if ( v20 )
{
v21 = strtok(v20 + 3, ",");
v22 = v21;
if ( *v21 && strlen(v21) <= 2 )
{
v24 = atoi(v22);
printf(" polling server every %d s\n", (unsigned int)(1 << v24));
}
else
{
puts("poll interval unreadable");
}
}
else
{
puts("poll interval unknown");
v7 = 1;
}
return v7;
}

original_code:
int main (void) {
  int rc;
  struct sockaddr_in sock;
  struct in_addr address;
  int sd;
  fd_set fds;
  struct timeval tv;
  int n;
  unsigned char byte1ok;
  unsigned char byte2ok;
  struct {
    unsigned char byte1;
    unsigned char byte2;
    unsigned short sequence;
    unsigned char status1;
    unsigned char status2;
    unsigned short AssocID;
    unsigned short Offset;
    unsigned short Count;
    char payload[468];
    char authenticator[96];
  } ntpmsg;
  char buff[468];
  unsigned int clksrc;
  char *clksrcname[] = {
    "unspecified",
    "atomic clock",
    "VLF radio",
    "HF radio",
    "UHF radio",
    "local net",
    "NTP server",
    "UDP/TIME",
    "wristwatch",
    "modem"};
  char *newstr;
  char *dispstr;
  char *delaystr;
  const char DISP[] = "rootdisp=";
  const char DELAY[] = "rootdelay=";
  const char STRATUM[] = "stratum=";
  const char POLL[] = "tc=";
  const char REFID[] = "refid=";
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  inet_aton("127.0.0.1", &address);
  sock.sin_family = 2;
  sock.sin_addr = address;
  sock.sin_port = htons(123);
  memset ( &ntpmsg, 0, sizeof(ntpmsg));
  ntpmsg.byte1 = 0x16;
  ntpmsg.byte2 = 2; ntpmsg.sequence=htons(1);
  if ((sd = socket (2, SOCK_DGRAM, 0)) < 0)
    die("unable to open socket");
  if (connect(sd, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
    perror ("On connect");
    die("unable to connect to socket");
  }
  ((void) (((&fds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  if (send(sd, &ntpmsg, sizeof(ntpmsg), 0) < 0) {
    perror ("On send");
    die ("unable to send command to NTP port");
  }
  n = select (sd+1, &fds, (fd_set *)0, (fd_set *)0 , &tv);
  if (n == 0)
    die ("timeout");
  if (n == -1)
    die ("error on select");
  if ((n = recv (sd, &ntpmsg, sizeof(ntpmsg), 0)) < 0)
    die ("Unable to talk to NTP daemon. Is it running?");
  byte1ok = ((ntpmsg.byte1&0x3F) == 0x16);
  byte2ok = ((ntpmsg.byte2 & ~0x20) == (2|0x80));
  if (!(byte1ok && byte2ok)) {
    fprintf (stderr,"status word is 0x%02x%02x\n", ntpmsg.byte1,ntpmsg.byte2 );
    die ("return data appears to be invalid based on status word");
  }
  if (!(ntpmsg.byte2 | 0x40)) {
    fprintf (stderr,"status byte2 is %02x\n", ntpmsg.byte2 );
    die ("error bit is set in reply");
  }
  if (!(ntpmsg.byte2 | 0x20)) {
    fprintf (stderr,"status byte2 is %02x\n", ntpmsg.byte2 );
    fprintf (stderr,"More bit unexpected in reply");
  }
  if ((ntpmsg.status1 >> 6) == 3) {
    printf ("unsynchronised\n");
    if ((ntpmsg.status2 & 0x0F) == 1)
      printf ("  time server re-starting\n");
      rc=1;
  }
  else {
    rc=0;
    printf ("synchronised to ");
    clksrc = (ntpmsg.status1 & 0x3F);
    if (clksrc < 10)
      printf("%s", clksrcname[clksrc]);
    else
      printf("unknown source");
    if (clksrc == 6) {
      strncpy(buff, ntpmsg.payload, sizeof(buff));
      if ((newstr = strstr (buff, REFID))) {
 newstr += sizeof(REFID) - 1;
 dispstr = strtok(newstr,",");
 if ((strlen (dispstr) == 0) || (strlen (dispstr) > 16)) {
   printf (" <IP unreadable>");
 }
 else {
   printf(" (%s)",dispstr);
 }
      } else {
 rc=1;
 printf (" <IP unknown>");
      }
    }
    strncpy(buff, ntpmsg.payload, sizeof(buff));
    if ((newstr = strstr (buff, STRATUM))) {
      newstr += sizeof(STRATUM) - 1;
      dispstr = strtok(newstr,",");
      if ((strlen (dispstr) == 0) || (strlen (dispstr) > 2)) {
 printf (", stratum unreadable\n");
      }
      else {
 printf(" at stratum %s \n",dispstr);
      }
    } else {
      rc=1;
      printf (", stratum unknown\n");
    }
    strncpy(buff, ntpmsg.payload, sizeof(buff));
    if ((dispstr = strstr (buff, DISP)) && (delaystr = strstr (buff, DELAY))) {
      dispstr += sizeof(DISP) - 1;
      dispstr = strtok(dispstr,",");
      delaystr += sizeof(DELAY) - 1;
      delaystr = strtok(delaystr,",");
      if ((strlen (dispstr) == 0) || (strlen (dispstr) > 10) ||
       (strlen (delaystr) == 0) || (strlen (delaystr) > 10)) {
 printf ("accuracy unreadable\n");
      }
      else {
 printf("   time correct to within %.0f ms\n", atof(dispstr) + atof(delaystr) / 2.0);
      }
    } else {
      rc=1;
      printf ("accuracy unknown\n");
    }
  }
  strncpy(buff, ntpmsg.payload, sizeof(buff));
  if ((newstr = strstr (buff, POLL))) {
    newstr += sizeof(POLL) - 1;
    dispstr = strtok(newstr,",");
    if ((strlen (dispstr) == 0) || (strlen (dispstr) > 2)) {
      printf ("poll interval unreadable\n");
    }
    else {
      printf("   polling server every %d s\n",1 << atoi(dispstr));
    }
  } else {
    rc=1;
    printf ("poll interval unknown\n");
  }
  return rc;
}

canonical_original_code:
int main() {
  int rc;
  struct sockaddr_in sock;
  struct in_addr address;
  int sd;
  struct fd_set fds;
  struct timeval tv;
  int n;
  unsigned char byte1ok;
  unsigned char byte2ok;
  struct { unsigned char byte1; unsigned char byte2; unsigned short sequence; unsigned char status1; unsigned char status2; unsigned short AssocID; unsigned short Offset; unsigned short Count; char payload[468]; char authenticator[96]; } ntpmsg;
  char buff[468];
  unsigned int clksrc;
  char *clksrcname[] = {
    "unspecified",
    "atomic clock",
    "VLF radio",
    "HF radio",
    "UHF radio",
    "local net",
    "NTP server",
    "UDP/TIME",
    "wristwatch",
    "modem"};
  char *newstr;
  char *dispstr;
  char *delaystr;
  char DISP[] = "rootdisp=";
  char DELAY[] = "rootdelay=";
  char STRATUM[] = "stratum=";
  char POLL[] = "tc=";
  char REFID[] = "refid=";
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  inet_aton("127.0.0.1", &address);
  sock.sin_family = 2;
  sock.sin_addr = address;
  sock.sin_port = htons(123);
  memset ( &ntpmsg, 0, sizeof(ntpmsg));
  ntpmsg.byte1 = 0x16;
  ntpmsg.byte2 = 2; ntpmsg.sequence=htons(1);
  if ((sd = socket (2, SOCK_DGRAM, 0)) < 0)
    die("unable to open socket");
  if (connect(sd, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
    perror ("On connect");
    die("unable to connect to socket");
  }
  ((void) (((&fds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  if (send(sd, &ntpmsg, sizeof(ntpmsg), 0) < 0) {
    perror ("On send");
    die ("unable to send command to NTP port");
  }
  n = select (sd+1, &fds, (struct fd_set *)0, (struct fd_set *)0 , &tv);
  if (n == 0)
    die ("timeout");
  if (n == -1)
    die ("error on select");
  if ((n = recv (sd, &ntpmsg, sizeof(ntpmsg), 0)) < 0)
    die ("Unable to talk to NTP daemon. Is it running?");
  byte1ok = ((ntpmsg.byte1&0x3F) == 0x16);
  byte2ok = ((ntpmsg.byte2 & ~0x20) == (2|0x80));
  if (!(byte1ok && byte2ok)) {
    fprintf (stderr,"status word is 0x%02x%02x\n", ntpmsg.byte1,ntpmsg.byte2 );
    die ("return data appears to be invalid based on status word");
  }
  if (!(ntpmsg.byte2 | 0x40)) {
    fprintf (stderr,"status byte2 is %02x\n", ntpmsg.byte2 );
    die ("error bit is set in reply");
  }
  if (!(ntpmsg.byte2 | 0x20)) {
    fprintf (stderr,"status byte2 is %02x\n", ntpmsg.byte2 );
    fprintf (stderr,"More bit unexpected in reply");
  }
  if ((ntpmsg.status1 >> 6) == 3) {
    printf ("unsynchronised\n");
    if ((ntpmsg.status2 & 0x0F) == 1)
      printf ("  time server re-starting\n");
      rc=1;
  }
  else {
    rc=0;
    printf ("synchronised to ");
    clksrc = (ntpmsg.status1 & 0x3F);
    if (clksrc < 10)
      printf("%s", clksrcname[clksrc]);
    else
      printf("unknown source");
    if (clksrc == 6) {
      strncpy(buff, ntpmsg.payload, sizeof(buff));
      if ((newstr = strstr (buff, REFID))) {
 newstr += sizeof(REFID) - 1;
 dispstr = strtok(newstr,",");
 if ((strlen (dispstr) == 0) || (strlen (dispstr) > 16)) {
   printf (" <IP unreadable>");
 }
 else {
   printf(" (%s)",dispstr);
 }
      } else {
 rc=1;
 printf (" <IP unknown>");
      }
    }
    strncpy(buff, ntpmsg.payload, sizeof(buff));
    if ((newstr = strstr (buff, STRATUM))) {
      newstr += sizeof(STRATUM) - 1;
      dispstr = strtok(newstr,",");
      if ((strlen (dispstr) == 0) || (strlen (dispstr) > 2)) {
 printf (", stratum unreadable\n");
      }
      else {
 printf(" at stratum %s \n",dispstr);
      }
    } else {
      rc=1;
      printf (", stratum unknown\n");
    }
    strncpy(buff, ntpmsg.payload, sizeof(buff));
    if ((dispstr = strstr (buff, DISP)) && (delaystr = strstr (buff, DELAY))) {
      dispstr += sizeof(DISP) - 1;
      dispstr = strtok(dispstr,",");
      delaystr += sizeof(DELAY) - 1;
      delaystr = strtok(delaystr,",");
      if ((strlen (dispstr) == 0) || (strlen (dispstr) > 10) ||
       (strlen (delaystr) == 0) || (strlen (delaystr) > 10)) {
 printf ("accuracy unreadable\n");
      }
      else {
 printf("   time correct to within %.0f ms\n", atof(dispstr) + atof(delaystr) / 2.0);
      }
    } else {
      rc=1;
      printf ("accuracy unknown\n");
    }
  }
  strncpy(buff, ntpmsg.payload, sizeof(buff));
  if ((newstr = strstr (buff, POLL))) {
    newstr += sizeof(POLL) - 1;
    dispstr = strtok(newstr,",");
    if ((strlen (dispstr) == 0) || (strlen (dispstr) > 2)) {
      printf ("poll interval unreadable\n");
    }
    else {
      printf("   polling server every %d s\n",1 << atoi(dispstr));
    }
  } else {
    rc=1;
    printf ("poll interval unknown\n");
  }
  return rc;
}

variable_types: rc int

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

variable_types:
address
struct in_addr { unsigned int s_addr; }

variable_types: sd int

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

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

variable_types: n int

variable_types:
buff
char [468]

variable_types:
clksrc
unsigned int

variable_types:
clksrcname
char *[]

variable_types:
newstr
char *

variable_types:
dispstr
char *

variable_types:
delaystr
char *

variable_types:
DISP
char []

variable_types:
DELAY
char []

variable_types:
STRATUM
char []

variable_types:
POLL
char []

variable_types:
REFID
char []

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


repo: darpa-xdata/xlang

decompiled_code:
void __fastcall __noreturn td_error(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(1);
}

original_code:
void td_error(char *msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(1);
}

variable_types:
msg
char *

return_type: void


decompiled_code:
__int64 __fastcall td_typeof(_QWORD *a1)
{
__int64 result; // rax

result = *(unsigned int *)a1;
if ( (unsigned int)result > 0x10 )
result = (*(__int64 (__fastcall **)(_QWORD))(*a1 + 112LL))(a1[1]);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
__int64 result; // rax

result = *(unsigned int *)a1;
if ( (unsigned int)result > 0x10 )
result = (*(__int64 (__fastcall **)(_QWORD))(*a1 + 112LL))(a1[1]);
return result;
}

original_code:
td_tag_t td_typeof(td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_type(v->object);
    return v->tag;
}

canonical_original_code:
enum td_tag_t td_typeof(struct td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_type(v->object);
    return v->tag;
}

variable_types: struct td_val_t { union { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; struct _td_env_t { char *name; void (*eval)(struct td_val_t *out, char *str); void (*invoke0)(struct td_val_t *out, char *f); void (*invoke1)(struct td_val_t *out, char *f, struct td_val_t *); void (*invoke2)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *); void (*invoke3)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke4)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke5)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invokeGraph0)(struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; } *out, char *f); void (*invokeGraph1)(struct graph_t *out, char *f, struct graph_t *in); void (*invokeGraph2)(struct graph_t *out, char *f, struct graph_t *in, int k); void (*invokeGraphAndCSC)(struct td_val_t *out, char *f, struct graph_t *in_graph, int *in_csc_offsets, int *in_csc_indics); void (*retain)(void *obj); void (*release)(void *obj); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_type)(void *); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_eltype)(void *); void *(*get_dataptr)(void *); unsigned long (*get_length)(void *); unsigned long (*get_ndims)(void *); void (*get_dims)(void *, unsigned long *dims); void (*get_strides)(void *, unsigned long *strides); } *owner; }; union { char int8_val; unsigned char uint8_val; short int16_val; unsigned short uint16_val; int int32_val; unsigned int uint32_val; long int64_val; unsigned long uint64_val; float float_val; double double_val; void *ptr_val; void *object; }; } *

user_defined_types: struct td_val_t { union { enum td_tag_t tag; struct _td_env_t *owner; }; union { char int8_val; unsigned char uint8_val; short int16_val; unsigned short uint16_val; int int32_val; unsigned int uint32_val; long int64_val; unsigned long uint64_val; float float_val; double double_val; void *ptr_val; void *object; }; }

user_defined_types: struct _td_env_t { char *name; void (*eval)(struct td_val_t *out, char *str); void (*invoke0)(struct td_val_t *out, char *f); void (*invoke1)(struct td_val_t *out, char *f, struct td_val_t *); void (*invoke2)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *); void (*invoke3)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke4)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke5)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invokeGraph0)(struct graph_t *out, char *f); void (*invokeGraph1)(struct graph_t *out, char *f, struct graph_t *in); void (*invokeGraph2)(struct graph_t *out, char *f, struct graph_t *in, int k); void (*invokeGraphAndCSC)(struct td_val_t *out, char *f, struct graph_t *in_graph, int *in_csc_offsets, int *in_csc_indics); void (*retain)(void *obj); void (*release)(void *obj); enum td_tag_t (*get_type)(void *); enum td_tag_t (*get_eltype)(void *); void *(*get_dataptr)(void *); unsigned long (*get_length)(void *); unsigned long (*get_ndims)(void *); void (*get_dims)(void *, unsigned long *dims); void (*get_strides)(void *, unsigned long *strides); }


decompiled_code:
__int64 __fastcall td_eltype(_QWORD *a1)
{
__int64 result; // rax

if ( *(_DWORD *)a1 == 12 )
result = *(unsigned int *)(a1[1] + 16LL);
else
result = (*(__int64 (__fastcall **)(_QWORD))(*a1 + 120LL))(a1[1]);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
__int64 result; // rax

if ( *(_DWORD *)a1 == 12 )
result = *(unsigned int *)(a1[1] + 16LL);
else
result = (*(__int64 (__fastcall **)(_QWORD))(*a1 + 120LL))(a1[1]);
return result;
}

original_code:
td_tag_t td_eltype(td_val_t *v)
{
    if (v->tag == TD_ARRAY)
        return ((td_array_t*)v->object)->eltype;
    return v->owner->get_eltype(v->object);
}

canonical_original_code:
enum td_tag_t td_eltype(struct td_val_t *v)
{
    if (v->tag == TD_ARRAY)
        return ((struct td_array_t *)v->object)->eltype;
    return v->owner->get_eltype(v->object);
}

return_type: enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 }

user_defined_types: struct td_array_t { void *data; unsigned long length; enum td_tag_t eltype; unsigned long ndims; unsigned long size1; unsigned long stride1; }

user_defined_types: struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; }


original_code:
void *td_dataptr(td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_dataptr(v->object);
    if (v->tag == TD_ARRAY)
        return ((td_array_t*)v->object)->data;
    if (v->tag == TD_UTF8)
        return ((td_string_t*)v->object)->data;
    return (void*)&(v->object);
}

canonical_original_code:
void *td_dataptr(struct td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_dataptr(v->object);
    if (v->tag == TD_ARRAY)
        return ((struct td_array_t *)v->object)->data;
    if (v->tag == TD_UTF8)
        return ((struct td_string_t *)v->object)->data;
    return (void *)&(v->object);
}

return_type: void *

user_defined_types: struct td_string_t { void *data; unsigned long length; }

user_defined_types: enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 }


original_code:
size_t td_length(td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_length(v->object);
    if (v->tag == TD_ARRAY)
        return ((td_array_t*)v->object)->length;
    if (v->tag == TD_UTF8)
        return ((td_string_t*)v->object)->length;
    return 1;
}

canonical_original_code:
unsigned long td_length(struct td_val_t *v)
{
    if (((v)->tag > TD_MAX_TAG))
        return v->owner->get_length(v->object);
    if (v->tag == TD_ARRAY)
        return ((struct td_array_t *)v->object)->length;
    if (v->tag == TD_UTF8)
        return ((struct td_string_t *)v->object)->length;
    return 1;
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall td_int8(_QWORD *a1)
{
return *(unsigned __int8 *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
return *(unsigned __int8 *)func3(a1);
}

original_code: int8_t td_int8(td_val_t *v) { return *(int8_t*)td_dataptr(v); }

canonical_original_code: char td_int8(struct td_val_t *v) { return *(char *)td_dataptr(v); }

return_type: char


decompiled_code:
__int64 __fastcall td_uint8(_QWORD *a1)
{
return *(unsigned __int8 *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
return *(unsigned __int8 *)func3(a1);
}

original_code: uint8_t td_uint8(td_val_t *v) { return *(uint8_t*)td_dataptr(v); }

canonical_original_code: unsigned char td_uint8(struct td_val_t *v) { return *(unsigned char *)td_dataptr(v); }

return_type: unsigned char


decompiled_code:
__int64 __fastcall td_int16(_QWORD *a1)
{
return *(unsigned __int16 *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
return *(unsigned __int16 *)func3(a1);
}

original_code: int16_t td_int16(td_val_t *v) { return *(int16_t*)td_dataptr(v); }

canonical_original_code: short td_int16(struct td_val_t *v) { return *(short *)td_dataptr(v); }

return_type: short


decompiled_code:
__int64 __fastcall td_uint16(_QWORD *a1)
{
return *(unsigned __int16 *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
return *(unsigned __int16 *)func3(a1);
}

original_code: uint16_t td_uint16(td_val_t *v) { return *(uint16_t*)td_dataptr(v); }

canonical_original_code: unsigned short td_uint16(struct td_val_t *v) { return *(unsigned short *)td_dataptr(v); }

return_type: unsigned short


decompiled_code:
__int64 __fastcall td_int32(_QWORD *a1)
{
return *(unsigned int *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1)
{
return *(unsigned int *)func3(a1);
}

original_code: int32_t td_int32(td_val_t *v) { return *(int32_t*)td_dataptr(v); }

canonical_original_code: int td_int32(struct td_val_t *v) { return *(int *)td_dataptr(v); }

return_type: int


decompiled_code:
__int64 __fastcall td_uint32(_QWORD *a1)
{
return *(unsigned int *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
return *(unsigned int *)func3(a1);
}

original_code: uint32_t td_uint32(td_val_t *v) { return *(uint32_t*)td_dataptr(v); }

canonical_original_code: unsigned int td_uint32(struct td_val_t *v) { return *(unsigned int *)td_dataptr(v); }

return_type: unsigned int


decompiled_code:
__int64 __fastcall td_int64(_QWORD *a1)
{
return *(_QWORD *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD *a1)
{
return *(_QWORD *)func3(a1);
}

original_code: int64_t td_int64(td_val_t *v) { return *(int64_t*)td_dataptr(v); }

canonical_original_code: long td_int64(struct td_val_t *v) { return *(long *)td_dataptr(v); }

return_type: long


decompiled_code:
__int64 __fastcall td_uint64(_QWORD *a1)
{
return *(_QWORD *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
return *(_QWORD *)func3(a1);
}

original_code: uint64_t td_uint64(td_val_t *v) { return *(uint64_t*)td_dataptr(v); }

canonical_original_code: unsigned long td_uint64(struct td_val_t *v) { return *(unsigned long *)td_dataptr(v); }


decompiled_code:
float __fastcall td_float(_QWORD *a1)
{
return *(float *)td_dataptr(a1);
}

canonical_decompiled_code:
float __fastcall func13(_QWORD *a1)
{
return *(float *)func3(a1);
}

original_code: float td_float(td_val_t *v) { return *(float*)td_dataptr(v); }

canonical_original_code: float td_float(struct td_val_t *v) { return *(float *)td_dataptr(v); }

return_type: float


decompiled_code:
double __fastcall td_double(_QWORD *a1)
{
return *(double *)td_dataptr(a1);
}

canonical_decompiled_code:
double __fastcall func14(_QWORD *a1)
{
return *(double *)func3(a1);
}

original_code: double td_double(td_val_t *v) { return *(double*)td_dataptr(v); }

canonical_original_code: double td_double(struct td_val_t *v) { return *(double *)td_dataptr(v); }

return_type: double


decompiled_code:
__int64 __fastcall td_pointer(_QWORD *a1)
{
return *(_QWORD *)td_dataptr(a1);
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD *a1)
{
return *(_QWORD *)func3(a1);
}

original_code: void *td_pointer(td_val_t *v) { return *(void**)td_dataptr(v); }

canonical_original_code: void *td_pointer(struct td_val_t *v) { return *(void **)td_dataptr(v); }


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

result = 8LL;
if ( a1 <= 0xA )
result = type_sizes[a1];
return result;
}

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

result = 8LL;
if ( a1 <= 0xA )
result = type_sizes[a1];
return result;
}

original_code:
size_t td_type_size(td_tag_t tag)
{
    if (tag < TD_UTF8) return type_sizes[tag];
    return sizeof(void*);
}

canonical_original_code:
unsigned long td_type_size(enum td_tag_t tag)
{
    if (tag < TD_UTF8) return type_sizes[tag];
    return sizeof(void *);
}

variable_types: enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 }


decompiled_code:
__int64 __fastcall td_env_julia(const char *a1, __int64 a2)
{
void *v3; // rax
void (__fastcall *v4)(__int64); // rax
char *v5; // rax
char v6[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_julia_env )
return cached_julia_env;
snprintf(v6, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_julia", ".so");
v3 = dlopen(v6, 258);
if ( !v3 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
td_error("could not load libjulia");
}
v4 = (void (__fastcall *)(__int64))dlsym(v3, "td_jl_init");
v4(a2);
if ( !cached_julia_env )
_assert_fail("cached_julia_env", "td.c", 0x69u, "td_env_julia");
return cached_julia_env;
}

canonical_decompiled_code:
__int64 __fastcall func17(const char *a1, __int64 a2)
{
void *v3; // rax
void (__fastcall *v4)(__int64); // rax
char *v5; // rax
char v6[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_julia_env )
return cached_julia_env;
snprintf(v6, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_julia", ".so");
v3 = dlopen(v6, 258);
if ( !v3 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
func0("could not load libjulia");
}
v4 = (void (__fastcall *)(__int64))dlsym(v3, "td_jl_init");
v4(a2);
if ( !cached_julia_env )
_assert_fail("cached_julia_env", "td.c", 0x69u, "td_env_julia");
return cached_julia_env;
}

original_code:
td_env_t *td_env_julia(char *plugin_path, char *julia_path)
{
    char path[512];
    if (cached_julia_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_julia", ".so");
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
            fprintf(stderr, "%s\n", dlerror());
            td_error("could not load libjulia");
        }
        void (*init)(char*) = dlsym(h, "td_jl_init");
        init(julia_path);
        ((void) sizeof ((cached_julia_env) ? 1 : 0), __extension__ ({ if (cached_julia_env) ; else __assert_fail ("cached_julia_env", "td.c", 105, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_julia_env;
}

canonical_original_code:
struct _td_env_t *td_env_julia(char *plugin_path, char *julia_path)
{
    char path[512];
    if (cached_julia_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_julia", ".so");
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
            fprintf(stderr, "%s\n", dlerror());
            td_error("could not load libjulia");
        }
        void (*init)(char *) = dlsym(h, "td_jl_init");
        init(julia_path);
        ((void) sizeof ((cached_julia_env) ? 1 : 0), __extension__ ({ if (cached_julia_env) ; else __assert_fail ("cached_julia_env", "td.c", 105, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_julia_env;
}

variable_types:
path
char [512]

variable_types:
h
void *

variable_types:
init
void (*)(char *)

return_type: struct _td_env_t { char *name; void (*eval)(struct td_val_t { union { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; struct _td_env_t *owner; }; union { char int8_val; unsigned char uint8_val; short int16_val; unsigned short uint16_val; int int32_val; unsigned int uint32_val; long int64_val; unsigned long uint64_val; float float_val; double double_val; void *ptr_val; void *object; }; } *out, char *str); void (*invoke0)(struct td_val_t *out, char *f); void (*invoke1)(struct td_val_t *out, char *f, struct td_val_t *); void (*invoke2)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *); void (*invoke3)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke4)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke5)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invokeGraph0)(struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; } *out, char *f); void (*invokeGraph1)(struct graph_t *out, char *f, struct graph_t *in); void (*invokeGraph2)(struct graph_t *out, char *f, struct graph_t *in, int k); void (*invokeGraphAndCSC)(struct td_val_t *out, char *f, struct graph_t *in_graph, int *in_csc_offsets, int *in_csc_indics); void (*retain)(void *obj); void (*release)(void *obj); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_type)(void *); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_eltype)(void *); void *(*get_dataptr)(void *); unsigned long (*get_length)(void *); unsigned long (*get_ndims)(void *); void (*get_dims)(void *, unsigned long *dims); void (*get_strides)(void *, unsigned long *strides); } *


decompiled_code:
void __fastcall td_provide_julia(__int64 a1)
{
cached_julia_env = a1;
}

canonical_decompiled_code:
void __fastcall func18(__int64 a1)
{
cached_julia_env = a1;
}

original_code:
void td_provide_julia(td_env_t *e)
{
    cached_julia_env = e;
}

canonical_original_code:
void td_provide_julia(struct _td_env_t *e)
{
    cached_julia_env = e;
}

variable_types: struct _td_env_t { char *name; void (*eval)(struct td_val_t { union { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; struct _td_env_t *owner; }; union { char int8_val; unsigned char uint8_val; short int16_val; unsigned short uint16_val; int int32_val; unsigned int uint32_val; long int64_val; unsigned long uint64_val; float float_val; double double_val; void *ptr_val; void *object; }; } *out, char *str); void (*invoke0)(struct td_val_t *out, char *f); void (*invoke1)(struct td_val_t *out, char *f, struct td_val_t *); void (*invoke2)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *); void (*invoke3)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke4)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke5)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invokeGraph0)(struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; } *out, char *f); void (*invokeGraph1)(struct graph_t *out, char *f, struct graph_t *in); void (*invokeGraph2)(struct graph_t *out, char *f, struct graph_t *in, int k); void (*invokeGraphAndCSC)(struct td_val_t *out, char *f, struct graph_t *in_graph, int *in_csc_offsets, int *in_csc_indics); void (*retain)(void *obj); void (*release)(void *obj); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_type)(void *); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_eltype)(void *); void *(*get_dataptr)(void *); unsigned long (*get_length)(void *); unsigned long (*get_ndims)(void *); void (*get_dims)(void *, unsigned long *dims); void (*get_strides)(void *, unsigned long *strides); } *


decompiled_code:
__int64 __fastcall td_env_python(const char *a1, __int64 a2)
{
void *v3; // rax
void (__fastcall *v4)(__int64); // rax
char *v5; // rax
char v6[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_python_env )
return cached_python_env;
snprintf(v6, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_python", ".so");
v3 = dlopen(v6, 258);
if ( !v3 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
td_error("could not load libpython");
}
v4 = (void (__fastcall *)(__int64))dlsym(v3, "td_py_init");
v4(a2);
if ( !cached_python_env )
_assert_fail("cached_python_env", "td.c", 0x84u, "td_env_python");
return cached_python_env;
}

canonical_decompiled_code:
__int64 __fastcall func19(const char *a1, __int64 a2)
{
void *v3; // rax
void (__fastcall *v4)(__int64); // rax
char *v5; // rax
char v6[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_python_env )
return cached_python_env;
snprintf(v6, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_python", ".so");
v3 = dlopen(v6, 258);
if ( !v3 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
func0("could not load libpython");
}
v4 = (void (__fastcall *)(__int64))dlsym(v3, "td_py_init");
v4(a2);
if ( !cached_python_env )
_assert_fail("cached_python_env", "td.c", 0x84u, "td_env_python");
return cached_python_env;
}

original_code:
td_env_t *td_env_python(char *plugin_path, char *python_path)
{
    char path[512];
    if (cached_python_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_python", ".so");
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
            fprintf(stderr, "%s\n", dlerror());
            td_error("could not load libpython");
        }
        void (*init)(char*) = dlsym(h, "td_py_init");
        init(python_path);
        ((void) sizeof ((cached_python_env) ? 1 : 0), __extension__ ({ if (cached_python_env) ; else __assert_fail ("cached_python_env", "td.c", 132, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_python_env;
}

canonical_original_code:
struct _td_env_t *td_env_python(char *plugin_path, char *python_path)
{
    char path[512];
    if (cached_python_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_python", ".so");
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
            fprintf(stderr, "%s\n", dlerror());
            td_error("could not load libpython");
        }
        void (*init)(char *) = dlsym(h, "td_py_init");
        init(python_path);
        ((void) sizeof ((cached_python_env) ? 1 : 0), __extension__ ({ if (cached_python_env) ; else __assert_fail ("cached_python_env", "td.c", 132, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_python_env;
}


decompiled_code:
void __fastcall td_provide_python(__int64 a1)
{
cached_python_env = a1;
}

canonical_decompiled_code:
void __fastcall func20(__int64 a1)
{
cached_python_env = a1;
}

original_code:
void td_provide_python(td_env_t *e)
{
    cached_python_env = e;
}

canonical_original_code:
void td_provide_python(struct _td_env_t *e)
{
    cached_python_env = e;
}


decompiled_code:
__int64 __fastcall td_env_java(const char *a1, __int64 a2, __int64 a3)
{
void *v5; // r12
void (__fastcall *v6)(__int64, __int64); // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
char v10[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_java_env )
return cached_java_env;
snprintf(v10, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_java", ".so");
printf("looking for lib at %s\n", v10);
v5 = dlopen(v10, 258);
if ( !v5 )
{
v7 = dlerror();
fprintf(stderr, "%s\n", v7);
v5 = dlopen("libtd_java.dll", 258);
if ( !v5 )
{
v8 = dlerror();
fprintf(stderr, "%s\n", v8);
v5 = dlopen("libtd_java.dylib", 258);
if ( !v5 )
{
v9 = dlerror();
fprintf(stderr, "%s\n", v9);
td_error("could not load libjava - did you set LD_LIBRARY_PATH to include the libjvm location?\n");
}
}
}
v6 = (void (__fastcall *)(__int64, __int64))dlsym(v5, "td_java_init");
v6(a2, a3);
if ( !cached_java_env )
_assert_fail("cached_java_env", "td.c", 0xABu, "td_env_java");
return cached_java_env;
}

canonical_decompiled_code:
__int64 __fastcall func21(const char *a1, __int64 a2, __int64 a3)
{
void *v5; // r12
void (__fastcall *v6)(__int64, __int64); // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
char v10[536]; // [rsp-218h] [rbp-218h] BYREF

if ( cached_java_env )
return cached_java_env;
snprintf(v10, 0x200uLL, "%s%s%s%s", a1, "/", "libtd_java", ".so");
printf("looking for lib at %s\n", v10);
v5 = dlopen(v10, 258);
if ( !v5 )
{
v7 = dlerror();
fprintf(stderr, "%s\n", v7);
v5 = dlopen("libtd_java.dll", 258);
if ( !v5 )
{
v8 = dlerror();
fprintf(stderr, "%s\n", v8);
v5 = dlopen("libtd_java.dylib", 258);
if ( !v5 )
{
v9 = dlerror();
fprintf(stderr, "%s\n", v9);
func0("could not load libjava - did you set LD_LIBRARY_PATH to include the libjvm location?\n");
}
}
}
v6 = (void (__fastcall *)(__int64, __int64))dlsym(v5, "td_java_init");
v6(a2, a3);
if ( !cached_java_env )
_assert_fail("cached_java_env", "td.c", 0xABu, "td_env_java");
return cached_java_env;
}

original_code:
td_env_t *td_env_java(char *plugin_path, char *classpath, char *java_path)
{
    char path[512];
    if (cached_java_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_java", ".so");
        printf("looking for lib at %s\n", path);
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         h = dlopen("libtd_java.dll", 0x00100 | 0x00002);
        }
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         h = dlopen("libtd_java.dylib", 0x00100 | 0x00002);
        }
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         td_error("could not load libjava - did you set LD_LIBRARY_PATH to include the libjvm location?\n");
         td_error("for instance something like : .:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/\n");
        }
        void (*init)(char*,char*) = dlsym(h, "td_java_init");
        init(classpath, java_path);
        ((void) sizeof ((cached_java_env) ? 1 : 0), __extension__ ({ if (cached_java_env) ; else __assert_fail ("cached_java_env", "td.c", 171, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_java_env;
}

canonical_original_code:
struct _td_env_t *td_env_java(char *plugin_path, char *classpath, char *java_path)
{
    char path[512];
    if (cached_java_env == ((void *)0)) {
        snprintf(path, sizeof(path), "%s%s%s%s",
                 plugin_path, "/", "libtd_java", ".so");
        printf("looking for lib at %s\n", path);
        void *h = dlopen(path, 0x00100 | 0x00002);
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         h = dlopen("libtd_java.dll", 0x00100 | 0x00002);
        }
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         h = dlopen("libtd_java.dylib", 0x00100 | 0x00002);
        }
        if (h == ((void *)0)) {
         fprintf(stderr, "%s\n", dlerror());
         td_error("could not load libjava - did you set LD_LIBRARY_PATH to include the libjvm location?\n");
         td_error("for instance something like : .:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/\n");
        }
        void (*init)(char *, char *) = dlsym(h, "td_java_init");
        init(classpath, java_path);
        ((void) sizeof ((cached_java_env) ? 1 : 0), __extension__ ({ if (cached_java_env) ; else __assert_fail ("cached_java_env", "td.c", 171, __extension__ __PRETTY_FUNCTION__); }));
    }
    return cached_java_env;
}

variable_types:
classpath
char *

variable_types:
init
void (*)(char *, char *)


decompiled_code:
void __fastcall td_provide_java(__int64 a1)
{
cached_java_env = a1;
}

canonical_decompiled_code:
void __fastcall func22(__int64 a1)
{
cached_java_env = a1;
}

original_code:
void td_provide_java(td_env_t *e)
{
    cached_java_env = e;
}

canonical_original_code:
void td_provide_java(struct _td_env_t *e)
{
    cached_java_env = e;
}


decompiled_code:
__int64 __fastcall td_env_r(__int64 a1)
{
void *v2; // rbp
void (__fastcall *v3)(__int64); // rbp
char *v4; // rax
char *v5; // rax
char *v6; // rax

if ( cached_r_env )
return cached_r_env;
v2 = dlopen("libtd_r.so", 258);
if ( !v2 )
{
v4 = dlerror();
fprintf(stderr, "%s\n", v4);
v2 = dlopen("libtd_r.dll", 258);
if ( !v2 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
v2 = dlopen("libtd_r.dylib", 258);
if ( !v2 )
{
v6 = dlerror();
fprintf(stderr, "%s\n", v6);
}
}
}
fprintf(stderr, "h: 0x%px\n", v2);
v3 = (void (__fastcall *)(__int64))dlsym(v2, "td_r_init");
fprintf(stderr, "got here: 0x%px\n", v3);
v3(a1);
fwrite("after init\n", 1uLL, 0xBuLL, stderr);
if ( !cached_r_env )
_assert_fail("cached_r_env", "td.c", 0xCEu, "td_env_r");
return cached_r_env;
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1)
{
void *v2; // rbp
void (__fastcall *v3)(__int64); // rbp
char *v4; // rax
char *v5; // rax
char *v6; // rax

if ( cached_r_env )
return cached_r_env;
v2 = dlopen("libtd_r.so", 258);
if ( !v2 )
{
v4 = dlerror();
fprintf(stderr, "%s\n", v4);
v2 = dlopen("libtd_r.dll", 258);
if ( !v2 )
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
v2 = dlopen("libtd_r.dylib", 258);
if ( !v2 )
{
v6 = dlerror();
fprintf(stderr, "%s\n", v6);
}
}
}
fprintf(stderr, "h: 0x%px\n", v2);
v3 = (void (__fastcall *)(__int64))dlsym(v2, "td_r_init");
fprintf(stderr, "got here: 0x%px\n", v3);
v3(a1);
fwrite("after init\n", 1uLL, 0xBuLL, stderr);
if ( !cached_r_env )
_assert_fail("cached_r_env", "td.c", 0xCEu, "td_env_r");
return cached_r_env;
}

original_code:
td_env_t *td_env_r(char *homedir) {
  if (cached_r_env == ((void *)0)) {
    void *h = dlopen("libtd_r.so", 0x00100 | 0x00002);
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
      h = dlopen("libtd_r.dll", 0x00100 | 0x00002);
    }
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
      h = dlopen("libtd_r.dylib", 0x00100 | 0x00002);
    }
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
    }
    fprintf(stderr, "h: 0x%px\n", h);
    td_env_t *(*init)(char*) = dlsym(h, "td_r_init");
    fprintf(stderr, "got here: 0x%px\n", init);
    init(homedir);
    fprintf(stderr, "after init\n");
    ((void) sizeof ((cached_r_env) ? 1 : 0), __extension__ ({ if (cached_r_env) ; else __assert_fail ("cached_r_env", "td.c", 206, __extension__ __PRETTY_FUNCTION__); }));
  }
  return cached_r_env;
}

canonical_original_code:
struct _td_env_t *td_env_r(char *homedir) {
  if (cached_r_env == ((void *)0)) {
    void *h = dlopen("libtd_r.so", 0x00100 | 0x00002);
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
      h = dlopen("libtd_r.dll", 0x00100 | 0x00002);
    }
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
      h = dlopen("libtd_r.dylib", 0x00100 | 0x00002);
    }
    if (h == ((void *)0)) {
      fprintf(stderr, "%s\n", dlerror());
    }
    fprintf(stderr, "h: 0x%px\n", h);
    struct _td_env_t *(*init)(char *) = dlsym(h, "td_r_init");
    fprintf(stderr, "got here: 0x%px\n", init);
    init(homedir);
    fprintf(stderr, "after init\n");
    ((void) sizeof ((cached_r_env) ? 1 : 0), __extension__ ({ if (cached_r_env) ; else __assert_fail ("cached_r_env", "td.c", 206, __extension__ __PRETTY_FUNCTION__); }));
  }
  return cached_r_env;
}

variable_types:
homedir
char *

variable_types: struct _td_env_t { char *name; void (*eval)(struct td_val_t { union { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; struct _td_env_t *owner; }; union { char int8_val; unsigned char uint8_val; short int16_val; unsigned short uint16_val; int int32_val; unsigned int uint32_val; long int64_val; unsigned long uint64_val; float float_val; double double_val; void *ptr_val; void *object; }; } *out, char *str); void (*invoke0)(struct td_val_t *out, char *f); void (*invoke1)(struct td_val_t *out, char *f, struct td_val_t *); void (*invoke2)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *); void (*invoke3)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke4)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invoke5)(struct td_val_t *out, char *f, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *, struct td_val_t *); void (*invokeGraph0)(struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; } *out, char *f); void (*invokeGraph1)(struct graph_t *out, char *f, struct graph_t *in); void (*invokeGraph2)(struct graph_t *out, char *f, struct graph_t *in, int k); void (*invokeGraphAndCSC)(struct td_val_t *out, char *f, struct graph_t *in_graph, int *in_csc_offsets, int *in_csc_indics); void (*retain)(void *obj); void (*release)(void *obj); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_type)(void *); enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } (*get_eltype)(void *); void *(*get_dataptr)(void *); unsigned long (*get_length)(void *); unsigned long (*get_ndims)(void *); void (*get_dims)(void *, unsigned long *dims); void (*get_strides)(void *, unsigned long *strides); } *(*)(char *)


decompiled_code:
void __fastcall td_provide_r(__int64 a1)
{
cached_r_env = a1;
}

canonical_decompiled_code:
void __fastcall func24(__int64 a1)
{
cached_r_env = a1;
}

original_code:
void td_provide_r(td_env_t *e) {
  cached_r_env = e;
}

canonical_original_code:
void td_provide_r(struct _td_env_t *e) {
  cached_r_env = e;
}


decompiled_code:
__int64 __fastcall td_create_simple_graph(__int64 a1)
{
*(_DWORD *)a1 = 7;
*(_DWORD *)(a1 + 4) = 15;
*(_QWORD *)(a1 + 8) = &names_2;
*(_QWORD *)(a1 + 32) = &row_offsets_1;
*(_QWORD *)(a1 + 40) = &col_indices_0;
return 0LL;
}

original_code:
int td_create_simple_graph(graph_t *graph)
{
  size_t num_nodes = 7;
  size_t num_edges = 15;
  size_t top_nodes = 3;
  static char* names[7] = {"a", "b", "c", "d", "e", "f", "g"};
  static unsigned int row_offsets[8] = {0,3,6,9,11,14,15,15};
  static int col_indices[15] = {1,2,3,0,2,4,3,4,5,5,6,2,5,6,6};
  graph->numNodes = num_nodes;
  graph->numEdges = num_edges;
  graph->nodeNames = names;
  graph->rowOffsets = row_offsets;
  graph->colIndices = col_indices;
  return 0;
}

canonical_original_code:
int td_create_simple_graph(struct graph_t *graph)
{
  unsigned long num_nodes = 7;
  unsigned long num_edges = 15;
  unsigned long top_nodes = 3;
  static char *names[7] = {"a", "b", "c", "d", "e", "f", "g"};
  static unsigned int row_offsets[8] = {0,3,6,9,11,14,15,15};
  static int col_indices[15] = {1,2,3,0,2,4,3,4,5,5,6,2,5,6,6};
  graph->numNodes = num_nodes;
  graph->numEdges = num_edges;
  graph->nodeNames = names;
  graph->rowOffsets = row_offsets;
  graph->colIndices = col_indices;
  return 0;
}

variable_types: struct graph_t { int numNodes; char **nodeNames; double *edgeValues; double *nodeValues; int *rowOffsets; int *colIndices; } *

variable_types:
names
char *[7]


decompiled_code:
__int64 __fastcall parse_char_token(_BYTE **a1, __int64 a2, char a3)
{
_BYTE *v3; // rax
unsigned int v4; // ecx

v3 = *a1;
v4 = 0;
if ( a2 - (__int64)*a1 > 0 && *v3 == a3 )
{
*a1 = v3 + 1;
v4 = 1;
}
return v4;
}

original_code:
static int parse_char_token(const char **pbegin, const char *end, char token)
{
    const char *begin = *pbegin;
    if (1 <= end - begin && *begin == token) {
        *pbegin = begin + 1;
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
static int parse_char_token(char **pbegin, char *end, char token)
{
    char *begin = *pbegin;
    if (1 <= end - begin && *begin == token) {
        *pbegin = begin + 1;
        return 1;
    } else {
        return 0;
    }
}

variable_types:
pbegin
char **

variable_types:
end
char *

variable_types: token char

variable_types:
begin
char *


original_code:
static int parse_size_t(const char **pbegin, const char *end, size_t *out)
{
    const char *begin = *pbegin;
    if (begin == end || !(*begin >= '0' && *begin <= '9')) {
        return 0;
    }
    size_t result = 0;
    while (*begin >= '0' && *begin <= '9') {
        result = 10 * result + *begin++ - '0';
    }
    *out = result;
    *pbegin = begin;
    return 0;
}

canonical_original_code:
static int parse_size_t(char **pbegin, char *end, unsigned long *out)
{
    char *begin = *pbegin;
    if (begin == end || !(*begin >= '0' && *begin <= '9')) {
        return 0;
    }
    unsigned long result = 0;
    while (*begin >= '0' && *begin <= '9') {
        result = 10 * result + *begin++ - '0';
    }
    *out = result;
    *pbegin = begin;
    return 0;
}

variable_types:
out
unsigned long *

variable_types:
result
unsigned long


decompiled_code:
__int64 __fastcall parse_name(_BYTE **a1, unsigned __int64 a2, _QWORD *a3, _QWORD *a4)
{
_BYTE *v5; // r8
__int64 result; // rax
_BYTE *v7; // rax
char v8; // cl

v5 = *a1;
if ( *a1 == (_BYTE *)a2 )
return 0LL;
if ( (unsigned __int8)((*v5 & 0xDF) - 65) <= 0x19u || (result = 0LL, *v5 == 95) )
{
v7 = v5 + 1;
if ( a2 > (unsigned __int64)(v5 + 1) )
{
do
{
v8 = *v7;
if ( (unsigned __int8)((*v7 & 0xDF) - 65) > 0x19u && (unsigned __int8)(v8 - 48) > 9u && v8 != 95 )
break;
++v7;
}
while ( (_BYTE *)a2 != v7 );
}
*a3 = v5;
*a4 = v7;
*a1 = v7;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE **a1, unsigned __int64 a2, _QWORD *a3, _QWORD *a4)
{
_BYTE *v5; // r8
__int64 result; // rax
_BYTE *v7; // rax
char v8; // cl

v5 = *a1;
if ( *a1 == (_BYTE *)a2 )
return 0LL;
if ( (unsigned __int8)((*v5 & 0xDF) - 65) <= 0x19u || (result = 0LL, *v5 == 95) )
{
v7 = v5 + 1;
if ( a2 > (unsigned __int64)(v5 + 1) )
{
do
{
v8 = *v7;
if ( (unsigned __int8)((*v7 & 0xDF) - 65) > 0x19u && (unsigned __int8)(v8 - 48) > 9u && v8 != 95 )
break;
++v7;
}
while ( (_BYTE *)a2 != v7 );
}
*a3 = v5;
*a4 = v7;
*a1 = v7;
result = 1LL;
}
return result;
}

original_code:
static int parse_name(const char **pbegin, const char *end,
                      const char **out_strbegin, const char **out_strend)
{
    const char *begin = *pbegin;
    if (begin == end) {
        return 0;
    }
    if (('a' <= *begin && *begin <= 'z') || ('A' <= *begin && *begin <= 'Z') ||
            *begin == '_') {
        ++begin;
    } else {
        return 0;
    }
    while (begin < end && (('a' <= *begin && *begin <= 'z') ||
                           ('A' <= *begin && *begin <= 'Z') ||
                           ('0' <= *begin && *begin <= '9') || *begin == '_')) {
        ++begin;
    }
    *out_strbegin = *pbegin;
    *out_strend = begin;
    *pbegin = begin;
    return 1;
}

canonical_original_code:
static int parse_name(char **pbegin, char *end, char **out_strbegin, char **out_strend)
{
    char *begin = *pbegin;
    if (begin == end) {
        return 0;
    }
    if (('a' <= *begin && *begin <= 'z') || ('A' <= *begin && *begin <= 'Z') ||
            *begin == '_') {
        ++begin;
    } else {
        return 0;
    }
    while (begin < end && (('a' <= *begin && *begin <= 'z') ||
                           ('A' <= *begin && *begin <= 'Z') ||
                           ('0' <= *begin && *begin <= '9') || *begin == '_')) {
        ++begin;
    }
    *out_strbegin = *pbegin;
    *out_strend = begin;
    *pbegin = begin;
    return 1;
}


decompiled_code:
__int64 __fastcall parse_token(const void **a1, __int64 a2, const char *a3)
{
char *v4; // r14
size_t v5; // rbp
__int64 result; // rax
int v7; // edx

v4 = (char *)*a1;
v5 = strlen(a3);
result = 0LL;
if ( a2 - (__int64)v4 >= v5 )
{
v7 = memcmp(v4, a3, v5);
result = 0LL;
if ( !v7 )
{
*a1 = &v4[v5];
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const void **a1, __int64 a2, const char *a3)
{
char *v4; // r14
size_t v5; // rbp
__int64 result; // rax
int v7; // edx

v4 = (char *)*a1;
v5 = strlen(a3);
result = 0LL;
if ( a2 - (__int64)v4 >= v5 )
{
v7 = memcmp(v4, a3, v5);
result = 0LL;
if ( !v7 )
{
*a1 = &v4[v5];
result = 1LL;
}
}
return result;
}

original_code:
static int parse_token(const char **pbegin, const char *end, const char *token)
{
    const char *begin = *pbegin;
    size_t tokenlen = strlen(token);
    if (tokenlen <= end - begin && memcmp(begin, token, tokenlen) == 0) {
        *pbegin = begin + tokenlen;
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
static int parse_token(char **pbegin, char *end, char *token)
{
    char *begin = *pbegin;
    unsigned long tokenlen = strlen(token);
    if (tokenlen <= end - begin && memcmp(begin, token, tokenlen) == 0) {
        *pbegin = begin + tokenlen;
        return 1;
    } else {
        return 0;
    }
}

variable_types:
token
char *

variable_types:
tokenlen
unsigned long


decompiled_code:
_DWORD *create_struct_ds()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x20uLL);
v1 = v0;
if ( v0 )
{
*v0 = 13;
v0[1] = 0;
}
else
{
td_error("out of memory");
}
return v1;
}

canonical_decompiled_code:
_DWORD *func4()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x20uLL);
v1 = v0;
if ( v0 )
{
*v0 = 13;
v0[1] = 0;
}
else
{
td_error("out of memory");
}
return v1;
}

original_code:
static td_ds_struct_t *create_struct_ds()
{
    td_ds_struct_t *res = malloc(sizeof(td_ds_struct_t));
    if (res == ((void *)0)) {
        td_error("out of memory");
        return ((void *)0);
    }
    res->ds.tag = TD_STRUCT;
    res->size = 0;
    return res;
}

canonical_original_code:
static struct td_ds_struct_t *create_struct_ds()
{
    struct td_ds_struct_t *res = malloc(sizeof(td_ds_struct_t));
    if (res == ((void *)0)) {
        td_error("out of memory");
        return ((void *)0);
    }
    res->ds.tag = TD_STRUCT;
    res->size = 0;
    return res;
}

variable_types: struct td_ds_struct_t { struct td_ds_t { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; } ds; int size; struct td_struct_field_t { char *name; struct td_ds_t *type; long offset; } fields[1]; } *

user_defined_types: struct td_ds_struct_t { struct td_ds_t ds; int size; struct td_struct_field_t fields[1]; }


decompiled_code:
__int64 __fastcall skip_whitespace(_BYTE **a1, unsigned __int64 a2)
{
_BYTE *v2; // rbx
const unsigned __int16 *v3; // rdx
__int64 result; // rax
char *v5; // rax
__int64 v6[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = *a1;
v6[0] = (__int64)v2;
if ( (unsigned __int64)v2 < a2 )
{
v3 = *_ctype_b_loc();
while ( (v3[(char)*v2] & 0x2000) != 0 )
{
v6[0] = (__int64)++v2;
if ( v2 == (_BYTE *)a2 )
goto LABEL_5;
}
if ( a2 > (unsigned __int64)v2 && *v2 == 35 )
{
v5 = (char *)memchr(v2, 10, a2 - (_QWORD)v2);
if ( v5 )
{
v6[0] = (__int64)(v5 + 1);
skip_whitespace(v6, a2);
}
else
{
v6[0] = a2;
}
}
}
LABEL_5:
result = v6[0];
*a1 = (_BYTE *)v6[0];
return result;
}

original_code:
static void skip_whitespace(const char **pbegin, const char *end)
{
    const char *begin = *pbegin;
    while (begin < end && ((*__ctype_b_loc ())[(int) ((*begin))] & (unsigned short int) _ISspace)) {
        ++begin;
    }
    if (begin < end && *begin == '#') {
        const char *line_end = (const char *)memchr(begin, '\n', end - begin);
        if (line_end == ((void *)0)) {
            begin = end;
        } else {
            begin = line_end + 1;
            skip_whitespace(&begin, end);
        }
    }
    *pbegin = begin;
}

canonical_original_code:
static void skip_whitespace(char **pbegin, char *end)
{
    char *begin = *pbegin;
    while (begin < end && ((*__ctype_b_loc ())[(int) ((*begin))] & (unsigned short) _ISspace)) {
        ++begin;
    }
    if (begin < end && *begin == '#') {
        char *line_end = (const char *)memchr(begin, '\n', end - begin);
        if (line_end == ((void *)0)) {
            begin = end;
        } else {
            begin = line_end + 1;
            skip_whitespace(&begin, end);
        }
    }
    *pbegin = begin;
}


decompiled_code:
char *__fastcall td_parse_datashape(_BYTE *a1, _BYTE *a2)
{
char *v2; // rax
char *v3; // rbx
_BYTE *v5[4]; // [rsp+8h] [rbp-20h] BYREF

v5[0] = a1;
v2 = parse_datashape((const void **)v5, (unsigned __int64)a2);
if ( !v2 )
goto LABEL_4;
v3 = v2;
skip_whitespace(v5, (unsigned __int64)a2);
if ( v5[0] != a2 )
{
td_free_datashape_localalias(v3);
LABEL_4:
td_error("unrecognized datashape");
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func12(_BYTE *a1, _BYTE *a2)
{
char *v2; // rax
char *v3; // rbx
_BYTE *v5[4]; // [rsp+8h] [rbp-20h] BYREF

v5[0] = a1;
v2 = func10((const void **)v5, (unsigned __int64)a2);
if ( !v2 )
goto LABEL_4;
v3 = v2;
func5(v5, (unsigned __int64)a2);
if ( v5[0] != a2 )
{
func6(v3);
LABEL_4:
td_error("unrecognized datashape");
v3 = 0LL;
}
return v3;
}

original_code:
td_ds_t *td_parse_datashape(const char *strbegin,
                            const char *strend)
{
    td_ds_t *res = parse_datashape(&strbegin, strend);
    if (res != ((void *)0)) {
        skip_whitespace(&strbegin, strend);
        if (strbegin == strend) {
            return res;
        } else {
            td_free_datashape(res);
        }
    }
    td_error("unrecognized datashape");
    return 0;
}

canonical_original_code:
struct td_ds_t *td_parse_datashape(char *strbegin, char *strend)
{
    struct td_ds_t *res = parse_datashape(&strbegin, strend);
    if (res != ((void *)0)) {
        skip_whitespace(&strbegin, strend);
        if (strbegin == strend) {
            return res;
        } else {
            td_free_datashape(res);
        }
    }
    td_error("unrecognized datashape");
    return 0;
}

variable_types:
strbegin
char *

variable_types:
strend
char *

variable_types: struct td_ds_t { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; } *


decompiled_code:
__int64 __fastcall parse_struct_field(_BYTE **a1, unsigned __int64 a2, _QWORD *a3, _QWORD *a4, char **a5)
{
__int64 result; // rax
char *v7; // rax
_BYTE *v8[6]; // [rsp+8h] [rbp-30h] BYREF

v8[0] = *a1;
result = parse_name(v8, a2, a3, a4);
if ( (_DWORD)result )
{
skip_whitespace(v8, a2);
result = parse_char_token(v8, a2, 58);
if ( (_DWORD)result )
{
v7 = parse_datashape((const void **)v8, a2);
*a5 = v7;
if ( v7 )
{
*a1 = v8[0];
result = 1LL;
}
else
{
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(_BYTE **a1, unsigned __int64 a2, _QWORD *a3, _QWORD *a4, char **a5)
{
__int64 result; // rax
char *v7; // rax
_BYTE *v8[6]; // [rsp+8h] [rbp-30h] BYREF

v8[0] = *a1;
result = func2(v8, a2, a3, a4);
if ( (_DWORD)result )
{
func5(v8, a2);
result = func0(v8, a2, 58);
if ( (_DWORD)result )
{
v7 = func10((const void **)v8, a2);
*a5 = v7;
if ( v7 )
{
*a1 = v8[0];
result = 1LL;
}
else
{
result = 0LL;
}
}
}
return result;
}

original_code:
static int parse_struct_field(const char **pbegin, const char *end,
                              const char **pnamebegin, const char **pnameend,
                              td_ds_t **type)
{
    const char *begin = *pbegin;
    if (!parse_name(&begin, end, pnamebegin, pnameend)) {
        return 0;
    }
    skip_whitespace(&begin, end);
    if (!parse_char_token(&begin, end, ':')) {
        return 0;
    }
    *type = parse_datashape(&begin, end);
    if (*type == ((void *)0)) {
        return 0;
    }
    *pbegin = begin;
    return 1;
}

canonical_original_code:
static int parse_struct_field(char **pbegin, char *end, char **pnamebegin, char **pnameend, struct td_ds_t **type)
{
    char *begin = *pbegin;
    if (!parse_name(&begin, end, pnamebegin, pnameend)) {
        return 0;
    }
    skip_whitespace(&begin, end);
    if (!parse_char_token(&begin, end, ':')) {
        return 0;
    }
    *type = parse_datashape(&begin, end);
    if (*type == ((void *)0)) {
        return 0;
    }
    *pbegin = begin;
    return 1;
}

variable_types:
pnamebegin
char **

variable_types:
pnameend
char **

variable_types: struct td_ds_t { enum td_tag_t { TD_INT8=0, TD_UINT8=1, TD_INT16=2, TD_UINT16=3, TD_INT32=4, TD_UINT32=5, TD_INT64=6, TD_UINT64=7, TD_FLOAT=8, TD_DOUBLE=9, TD_PTR=10, TD_UTF8=11, TD_ARRAY=12, TD_STRUCT=13, TD_OBJECT=14, TD_UNKNOWN=15, TD_MAX_TAG=16 } tag; } **


intHandler

repo: dasca/datalogger

decompiled_code:
void intHandler()
{
strcpy(log_buf, "SIGINT caught, cleaning up");
log_entry(log_buf, 1LL);
}

canonical_decompiled_code:
void func0()
{
strcpy(log_buf, "SIGINT caught, cleaning up");
log_entry(log_buf, 1LL);
}

original_code:
void intHandler() {
    sprintf(log_buf, "SIGINT caught, cleaning up");
    log_entry(log_buf, 1);
}


onPowerPulse

decompiled_code:
__suseconds_t onPowerPulse()
{
__int64 v0; // rsi
__suseconds_t result; // rax
__int64 v2; // rdx
double v3; // xmm1_8
double v4; // xmm0_8
unsigned __int64 v5; // rsi

gettimeofday(&tv, 0LL);
v0 = pulseTime;
lastTime = pulseTime;
result = tv.tv_usec + 1000000 * tv.tv_sec;
pulseTime = result;
if ( firstPulse == 1 )
{
firstPulse = 0;
elapsedTicks = 0;
power = 0LL;
}
else
{
v2 = ++pulseCount;
result -= v0;
if ( result < 0 )
{
v5 = (unsigned __int64)result >> 1;
result &= 1u;
v3 = (double)(int)(result | v5) + (double)(int)(result | v5);
}
else
{
v3 = (double)(int)result;
}
*(double *)&power = 3600000000.0 / v3 / (double)ppwh + *(double *)&power;
if ( v2 < 0 )
{
result = v2 & 1 | ((unsigned __int64)v2 >> 1);
v4 = (double)(int)result + (double)(int)result;
}
else
{
v4 = (double)(int)v2;
}
*(double *)&elapsedkWh = v4 / (double)(1000 * ppwh);
++elapsedTicks;
}
return result;
}

canonical_decompiled_code:
__suseconds_t func3()
{
__int64 v0; // rsi
__suseconds_t result; // rax
__int64 v2; // rdx
double v3; // xmm1_8
double v4; // xmm0_8
unsigned __int64 v5; // rsi

gettimeofday(&tv, 0LL);
v0 = pulseTime;
lastTime = pulseTime;
result = tv.tv_usec + 1000000 * tv.tv_sec;
pulseTime = result;
if ( firstPulse == 1 )
{
firstPulse = 0;
elapsedTicks = 0;
power = 0LL;
}
else
{
v2 = ++pulseCount;
result -= v0;
if ( result < 0 )
{
v5 = (unsigned __int64)result >> 1;
result &= 1u;
v3 = (double)(int)(result | v5) + (double)(int)(result | v5);
}
else
{
v3 = (double)(int)result;
}
*(double *)&power = 3600000000.0 / v3 / (double)ppwh + *(double *)&power;
if ( v2 < 0 )
{
result = v2 & 1 | ((unsigned __int64)v2 >> 1);
v4 = (double)(int)result + (double)(int)result;
}
else
{
v4 = (double)(int)v2;
}
*(double *)&elapsedkWh = v4 / (double)(1000 * ppwh);
++elapsedTicks;
}
return result;
}

original_code:
void onPowerPulse() {
    gettimeofday(&tv, ((void *)0));
    lastTime = pulseTime;
    pulseTime = 1000000 * tv.tv_sec + tv.tv_usec;
    if (firstPulse == 1) {
        firstPulse = 0;
        elapsedTicks = 0;
        power = 0;
        return;
    }
    pulseCount++;
    power += (3600000000.0 / (pulseTime - lastTime)) / ppwh;
    elapsedkWh = (1.0 * pulseCount / (ppwh * 1000));
    elapsedTicks++;
}


cPoll

decompiled_code:
void *__fastcall cPoll(void *a1)
{
__int64 v2; // [rsp+0h] [rbp-58h] BYREF
struct pollfd fds; // [rsp+48h] [rbp-10h] BYREF

fds.fd = open("/sys/class/gpio/gpio71/value", 0);
while ( KEEP_RUNNING == 1 )
{
fds.events = 2;
if ( poll(&fds, 1uLL, -1) < 0 )
log_entry("poll() failed!", 1LL);
if ( (fds.revents & 2) != 0 )
{
read(fds.fd, &v2, 0x40uLL);
onPowerPulse();
}
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
__int64 v2; // [rsp+0h] [rbp-58h] BYREF
struct pollfd fds; // [rsp+48h] [rbp-10h] BYREF

fds.fd = open("/sys/class/gpio/gpio71/value", 0);
while ( KEEP_RUNNING == 1 )
{
fds.events = 2;
if ( poll(&fds, 1uLL, -1) < 0 )
log_entry("poll() failed!", 1LL);
if ( (fds.revents & 2) != 0 )
{
read(fds.fd, &v2, 0x40uLL);
func3();
}
}
return 0LL;
}

original_code:
void *cPoll(void *notUsed) {
    struct pollfd fd[1];
    char buf[64];
    int i = 0, len = 0;
    fd[0].fd = open("/sys/class/gpio/gpio71/value", 00);
    while (KEEP_RUNNING == 1) {
        fd[0].events = 0x002;
        i = poll(fd, 1, -1);
        if (i < 0) {
            log_entry("poll() failed!", 1);
        }
        if (fd[0].revents & 0x002) {
            len = read(fd[0].fd, buf, 64);
            onPowerPulse();
        }
    }
    return 0;
}

canonical_original_code:
void *cPoll(void *notUsed) {
    struct pollfd fd[1];
    char buf[64];
    int i = 0; int len = 0;
    fd[0].fd = open("/sys/class/gpio/gpio71/value", 00);
    while (KEEP_RUNNING == 1) {
        fd[0].events = 0x002;
        i = poll(fd, 1, -1);
        if (i < 0) {
            log_entry("poll() failed!", 1);
        }
        if (fd[0].revents & 0x002) {
            len = read(fd[0].fd, buf, 64);
            onPowerPulse();
        }
    }
    return 0;
}

variable_types:
notUsed
void *

variable_types: struct pollfd { int fd; short events; short revents; } [1]

variable_types:
buf
char [64]

variable_types: i int

variable_types: len int


decompiled_code:
__int64 log_device()
{
return addReading();
}

canonical_decompiled_code:
__int64 func5()
{
return addReading();
}

original_code:
int log_device(struct sOneWireDevice *device_to_log) {
    return (addReading(device_to_log));
}

variable_types: struct sOneWireDevice { char *adress; char *type; float value; struct sOneWireDevice *next; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__time_t v3; // rbx
__int64 v4; // rbp
void *v5; // rdi
__int64 v7; // rbx
__time_t v8; // r12
struct timeval v9; // [rsp+0h] [rbp-38h] BYREF
pthread_t newthread[4]; // [rsp+18h] [rbp-20h] BYREF

log_entry("Starting up", 0LL);
KEEP_RUNNING = 1;
zExitCode = 0;
signal(2, (__sighandler_t)intHandler);
initDB();
initGPIO();
if ( pthread_create(newthread, 0LL, cPoll, 0LL) )
log_entry("Error creating thread", 1LL);
gettimeofday(&v9, 0LL);
v3 = v9.tv_sec;
v4 = list_devices();
while ( KEEP_RUNNING == 1 )
{
gettimeofday(&v9, 0LL);
v8 = v9.tv_sec;
if ( (unsigned __int64)(v9.tv_sec - v3) > 0x3B )
{
if ( v4 )
{
v7 = v4;
do
{
if ( updateValue(v7) )
log_device(v7);
v7 = *(_QWORD *)(v7 + 24);
}
while ( v7 );
}
power = 0LL;
elapsedTicks = 0;
v3 = v8;
}
sleep(1u);
}
if ( pthread_join(newthread[0], 0LL) )
log_entry("Error joining thread\n", 1LL);
while ( v4 )
{
v5 = (void *)v4;
v4 = *(_QWORD *)(v4 + 24);
free(v5);
}
restoreGPIO();
log_entry("Cleanup finished, exiting", 0LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__time_t v3; // rbx
__int64 v4; // rbp
void *v5; // rdi
__int64 v7; // rbx
__time_t v8; // r12
struct timeval v9; // [rsp+0h] [rbp-38h] BYREF
pthread_t newthread[4]; // [rsp+18h] [rbp-20h] BYREF

log_entry("Starting up", 0LL);
KEEP_RUNNING = 1;
zExitCode = 0;
signal(2, (__sighandler_t)intHandler);
initDB();
func2();
if ( pthread_create(newthread, 0LL, cPoll, 0LL) )
log_entry("Error creating thread", 1LL);
gettimeofday(&v9, 0LL);
v3 = v9.tv_sec;
v4 = list_devices();
while ( KEEP_RUNNING == 1 )
{
gettimeofday(&v9, 0LL);
v8 = v9.tv_sec;
if ( (unsigned __int64)(v9.tv_sec - v3) > 0x3B )
{
if ( v4 )
{
v7 = v4;
do
{
if ( updateValue(v7) )
func5(v7);
v7 = *(_QWORD *)(v7 + 24);
}
while ( v7 );
}
power = 0LL;
elapsedTicks = 0;
v3 = v8;
}
sleep(1u);
}
if ( pthread_join(newthread[0], 0LL) )
log_entry("Error joining thread\n", 1LL);
while ( v4 )
{
v5 = (void *)v4;
v4 = *(_QWORD *)(v4 + 24);
free(v5);
}
func1();
log_entry("Cleanup finished, exiting", 0LL);
return 0;
}

original_code:
int main() {
    struct sOneWireDevice *devices, *device;
    pthread_t poll_thread;
    struct timeval tick;
    unsigned long last_tick;
    log_entry("Starting up", 0);
    KEEP_RUNNING = 1;
    zExitCode = 0;
    signal(2, intHandler);
    initDB();
    initGPIO();
    if (pthread_create(&poll_thread, ((void *)0), cPoll, ((void *)0))) {
        log_entry("Error creating thread", 1);
    }
    gettimeofday(&tick, ((void *)0));
    last_tick = tick.tv_sec;
    devices = list_devices();
    while (KEEP_RUNNING == 1) {
        gettimeofday(&tick, ((void *)0));
        if ((tick.tv_sec - last_tick) >= 60) {
            last_tick = tick.tv_sec;
            device = devices;
            while (device != ((void *)0)) {
                if (updateValue(device) != ((void *)0))
                    log_device(device);
                device = device->next;
            }
            power = 0;
            elapsedTicks = 0;
        }
        sleep(1);
    }
    if (pthread_join(poll_thread, ((void *)0))) {
        log_entry("Error joining thread\n", 1);
    }
    device = devices;
    while (device != ((void *)0)) {
        devices = device->next;
        free(device);
        device = devices;
    }
    restoreGPIO();
    log_entry("Cleanup finished, exiting", 0);
    return (0);
}

canonical_original_code:
int main() {
    struct sOneWireDevice *devices; struct sOneWireDevice *device;
    unsigned long poll_thread;
    struct timeval tick;
    unsigned long last_tick;
    log_entry("Starting up", 0);
    KEEP_RUNNING = 1;
    zExitCode = 0;
    signal(2, intHandler);
    initDB();
    initGPIO();
    if (pthread_create(&poll_thread, ((void *)0), cPoll, ((void *)0))) {
        log_entry("Error creating thread", 1);
    }
    gettimeofday(&tick, ((void *)0));
    last_tick = tick.tv_sec;
    devices = list_devices();
    while (KEEP_RUNNING == 1) {
        gettimeofday(&tick, ((void *)0));
        if ((tick.tv_sec - last_tick) >= 60) {
            last_tick = tick.tv_sec;
            device = devices;
            while (device != ((void *)0)) {
                if (updateValue(device) != ((void *)0))
                    log_device(device);
                device = device->next;
            }
            power = 0;
            elapsedTicks = 0;
        }
        sleep(1);
    }
    if (pthread_join(poll_thread, ((void *)0))) {
        log_entry("Error joining thread\n", 1);
    }
    device = devices;
    while (device != ((void *)0)) {
        devices = device->next;
        free(device);
        device = devices;
    }
    restoreGPIO();
    log_entry("Cleanup finished, exiting", 0);
    return (0);
}

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

user_defined_types: struct sOneWireDevice { char *adress; char *type; float value; struct sOneWireDevice *next; }


decompiled_code:
__int64 __fastcall get_file_size(const char *a1)
{
int *v2; // rax
char *v3; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v4) )
{
v2 = _errno_location();
v3 = strerror(*v2);
sprintf(log_buf, "'stat' failed for '%s': %s", a1, v3);
log_entry(log_buf, 1LL);
}
return LODWORD(v4.st_size);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int *v2; // rax
char *v3; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v4) )
{
v2 = _errno_location();
v3 = strerror(*v2);
sprintf(log_buf, "'stat' failed for '%s': %s", a1, v3);
log_entry(log_buf, 1LL);
}
return LODWORD(v4.st_size);
}

original_code:
unsigned get_file_size(const char * file_name) {
    struct stat sb;
    if (stat(file_name, & sb) != 0) {
        sprintf(log_buf, "'stat' failed for '%s': %s",
                file_name, strerror((*__errno_location ())));
        log_entry(log_buf, 1);
    }
    return sb.st_size;
}

canonical_original_code:
unsigned int get_file_size(char *file_name) {
    struct stat sb;
    if (stat(file_name, & sb) != 0) {
        sprintf(log_buf, "'stat' failed for '%s': %s",
                file_name, strerror((*__errno_location ())));
        log_entry(log_buf, 1);
    }
    return sb.st_size;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
int __fastcall log_entry(const char *a1, int a2)
{
struct tm *v2; // rax
int result; // eax
char v4[104]; // [rsp+0h] [rbp-88h] BYREF
time_t timer; // [rsp+68h] [rbp-20h] BYREF

time(&timer);
v2 = localtime(&timer);
strftime(v4, 0x64uLL, "%y-%m-%d %H:%M:%S", v2);
result = fprintf(stderr, "%s:%s\r\n", v4, a1);
if ( a2 )
{
KEEP_RUNNING = 0;
zExitCode = a2;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, int a2)
{
struct tm *v2; // rax
int result; // eax
char v4[104]; // [rsp+0h] [rbp-88h] BYREF
time_t timer; // [rsp+68h] [rbp-20h] BYREF

time(&timer);
v2 = localtime(&timer);
strftime(v4, 0x64uLL, "%y-%m-%d %H:%M:%S", v2);
result = fprintf(stderr, "%s:%s\r\n", v4, a1);
if ( a2 )
{
KEEP_RUNNING = 0;
zExitCode = a2;
}
return result;
}

original_code:
void log_entry(char *text, int doexit) {
    time_t rawtime;
    struct tm *timeinfo;
    char buf[100];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buf, 100, "%y-%m-%d %H:%M:%S", timeinfo);
    fprintf(stderr, "%s:%s\r\n", buf, text);
    if (doexit != 0) {
        KEEP_RUNNING = 0;
        zExitCode = doexit;
    }
}

canonical_original_code:
void log_entry(char *text, int doexit) {
    long rawtime;
    struct tm *timeinfo;
    char buf[100];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buf, 100, "%y-%m-%d %H:%M:%S", timeinfo);
    fprintf(stderr, "%s:%s\r\n", buf, text);
    if (doexit != 0) {
        KEEP_RUNNING = 0;
        zExitCode = doexit;
    }
}

variable_types:
text
char *

variable_types: doexit int

variable_types: rawtime long

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:
buf
char [100]


deviceFindCallback

decompiled_code:
__int64 __fastcall deviceFindCallback(_DWORD *a1, __int64 a2, _QWORD *a3)
{
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(*a3, &unk_444, v4);
*a1 = v4[0];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, __int64 a2, _QWORD *a3)
{
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(*a3, &unk_444, v4);
*a1 = v4[0];
return 0LL;
}

original_code:
static int deviceFindCallback(void *retval, int numCols, char **colText,
char **colName) {
    int i;
    sscanf(colText[0], "%i", &i);
    *(int*) retval = i;
    return 0;
}

canonical_original_code:
static int deviceFindCallback(void *retval, int numCols, char **colText, char **colName) {
    int i;
    sscanf(colText[0], "%i", &i);
    *(int *) retval = i;
    return 0;
}

variable_types:
retval
void *

variable_types: numCols int

variable_types:
colText
char **

variable_types:
colName
char **


main

repo: daschaich/susy

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // r12
double v4; // r13
int v5; // ebp
double v6; // rbx
double v7; // xmm0_8
__int64 v8; // rbx
int result; // eax
double v10[1002]; // [rsp+0h] [rbp-1F88h] BYREF
double v11; // [rsp+1F50h] [rbp-38h] BYREF
int v12[11]; // [rsp+1F5Ch] [rbp-2Ch] BYREF

if ( argc == 3 )
{
v3 = atof(argv[1]);
v4 = atof(argv[2]);
squareroot((__int64)v10, v12, &v11, v4, v3);
printf("order = %d;\n", (unsigned int)v12[0]);
printf("epsilon = %.8g;\n", v3);
printf("err = %.8g;\n\n", v11);
v5 = 7;
v6 = *(double *)&LC1;
do
{
v7 = star(v12[0], (__int64)v10, v4, v3, v6);
printf("star(%.2g) = %.8g;\n", v6, v7);
v6 = v6 + 0.5;
--v5;
}
while ( v5 );
putchar(10);
if ( v12[0] < 0 )
{
result = 0;
}
else
{
v8 = 0LL;
do
{
printf("coeffs[%d] = %.16g;\n", (unsigned int)v8, v10[v8]);
++v8;
}
while ( v12[0] >= (int)v8 );
result = 0;
}
}
else
{
printf("Usage: %s epsilon delta\n", *argv);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // r12
double v4; // r13
int v5; // ebp
double v6; // rbx
double v7; // xmm0_8
__int64 v8; // rbx
int result; // eax
double v10[1002]; // [rsp+0h] [rbp-1F88h] BYREF
double v11; // [rsp+1F50h] [rbp-38h] BYREF
int v12[11]; // [rsp+1F5Ch] [rbp-2Ch] BYREF

if ( argc == 3 )
{
v3 = atof(argv[1]);
v4 = atof(argv[2]);
func6((__int64)v10, v12, &v11, v4, v3);
printf("order = %d;\n", (unsigned int)v12[0]);
printf("epsilon = %.8g;\n", v3);
printf("err = %.8g;\n\n", v11);
v5 = 7;
v6 = *(double *)&LC1;
do
{
v7 = func7(v12[0], (__int64)v10, v4, v3, v6);
printf("star(%.2g) = %.8g;\n", v6, v7);
v6 = v6 + 0.5;
--v5;
}
while ( v5 );
putchar(10);
if ( v12[0] < 0 )
{
result = 0;
}
else
{
v8 = 0LL;
do
{
printf("coeffs[%d] = %.16g;\n", (unsigned int)v8, v10[v8]);
++v8;
}
while ( v12[0] >= (int)v8 );
result = 0;
}
}
else
{
printf("Usage: %s epsilon delta\n", *argv);
result = -1;
}
return result;
}

original_code:
int main(int argc, char* argv[]) {
  int i, order;
  double alpha, epsilon, delta, err, c[1000 + 1];
  if (argc != 3) {
    printf("Usage: %s epsilon delta\n", argv[0]);
    return -1;
  }
  epsilon = atof(argv[1]);
  delta = atof(argv[2]);
  squareroot(delta, epsilon, c, &order, &err);
  printf("order = %d;\n", order);
  printf("epsilon = %.8g;\n", epsilon);
  printf("err = %.8g;\n\n", err);
  for (alpha = 0; alpha <= 3.0; alpha += 0.5) {
    printf("star(%.2g) = %.8g;\n", alpha,
           star(delta, epsilon, order, c, alpha));
  }
  printf("\n");
  for (i = 0; i <= order; i++)
    printf("coeffs[%d] = %.16g;\n", i, c[i]);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int i; int order;
  double alpha; double epsilon; double delta; double err; double c[-1];
  if (argc != 3) {
    printf("Usage: %s epsilon delta\n", argv[0]);
    return -1;
  }
  epsilon = atof(argv[1]);
  delta = atof(argv[2]);
  squareroot(delta, epsilon, c, &order, &err);
  printf("order = %d;\n", order);
  printf("epsilon = %.8g;\n", epsilon);
  printf("err = %.8g;\n\n", err);
  for (alpha = 0; alpha <= 3.0; alpha += 0.5) {
    printf("star(%.2g) = %.8g;\n", alpha,
           star(delta, epsilon, order, c, alpha));
  }
  printf("\n");
  for (i = 0; i <= order; i++)
    printf("coeffs[%d] = %.16g;\n", i, c[i]);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: order int

variable_types: alpha double

variable_types: epsilon double

variable_types: delta double

variable_types: err double

variable_types:
c
double [-1]


worker

original_code:
void *worker(void *arg)
{
 struct Work *work = arg;
 work->sum = 0;
 for (int i = work->start; i < work->start + work->len; i++) {
  work->sum += i;
 }
 return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct Work { int start; int len; int sum; } *

variable_types: i int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t *v3; // rbx
int *v4; // rbp
pthread_t *v5; // r12
_BYTE v7[32]; // [rsp+0h] [rbp-88h] BYREF
int arg[12]; // [rsp+20h] [rbp-68h] BYREF
char v9; // [rsp+50h] [rbp-38h] BYREF
int v10; // [rsp+5Ch] [rbp-2Ch]

printf("Max int: ");
__isoc99_scanf();
arg[0] = 1;
arg[1] = v10 / 4;
arg[3] = v10 / 4 + 1;
arg[4] = v10 / 4;
arg[6] = 2 * (v10 / 4) + 1;
arg[7] = v10 / 4;
arg[9] = 3 * (v10 / 4) + 1;
arg[10] = v10 / 4 + v10 % 4;
v3 = (pthread_t *)v7;
v4 = arg;
v5 = (pthread_t *)v7;
do
{
pthread_create(v5++, 0LL, worker, v4);
v4 += 3;
}
while ( v4 != (int *)&v9 );
puts("Threads started");
do
pthread_join(*v3++, 0LL);
while ( v3 != (pthread_t *)arg );
puts("Threads finished");
printf("Sum is %i\n", (unsigned int)(arg[11] + arg[8] + arg[2] + arg[5]));
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int max;
 printf("Max int: " );
 scanf("%i", &max);
 struct Work workList[4];
 pthread_t threadList[4];
 int chunkSize = max / 4;
 for (int i = 0; i < 4; i++) {
  workList[i].start = 1 + (i * chunkSize);
  workList[i].len = chunkSize;
  if (i == 4 - 1) {
   workList[i].len += (max % 4);
  }
 }
 for (int i = 0; i < 4; i++) {
  pthread_create(&threadList[i], ((void *)0), &worker, &workList[i]);
 }
 printf("Threads started\n");
 for (int i = 0; i < 4; i++) {
  pthread_join(threadList[i], ((void *)0));
 }
 printf("Threads finished\n");
 int global_sum = 0;
 for (int i = 0; i < 4; i++) {
  global_sum += workList[i].sum;
 }
 printf("Sum is %i\n", global_sum);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int max;
 printf("Max int: " );
 scanf("%i", &max);
 struct Work workList[4];
 unsigned long threadList[4];
 int chunkSize = max / 4;
 for (int i = 0; i < 4; i++) {
  workList[i].start = 1 + (i * chunkSize);
  workList[i].len = chunkSize;
  if (i == 4 - 1) {
   workList[i].len += (max % 4);
  }
 }
 for (int i = 0; i < 4; i++) {
  pthread_create(&threadList[i], ((void *)0), &worker, &workList[i]);
 }
 printf("Threads started\n");
 for (int i = 0; i < 4; i++) {
  pthread_join(threadList[i], ((void *)0));
 }
 printf("Threads finished\n");
 int global_sum = 0;
 for (int i = 0; i < 4; i++) {
  global_sum += workList[i].sum;
 }
 printf("Sum is %i\n", global_sum);
 return 0;
}

variable_types:
argv
char **

variable_types: max int

variable_types: struct Work { int start; int len; int sum; } [4]

variable_types:
threadList
unsigned long [4]

variable_types: chunkSize int


setTime

repo: davidamaro/sw

decompiled_code:
__int64 __fastcall setTime(unsigned int *a1, _BYTE *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v4; // r8

result = (unsigned __int8)*a2;
if ( (unsigned __int8)(result - 104) <= 0xBu && (v4 = 2081LL, _bittest64(&v4, (unsigned int)(result - 104))) )
{
if ( (_BYTE)result == 115 )
{
result = *a1;
*a3 += result;
}
if ( *a2 == 109 )
{
result = *a1;
a3[1] += result;
}
if ( *a2 == 104 )
{
result = *a1;
a3[2] += result;
}
}
else
{
result = *a1;
*a3 += result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1, _BYTE *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v4; // r8

result = (unsigned __int8)*a2;
if ( (unsigned __int8)(result - 104) <= 0xBu && (v4 = 2081LL, _bittest64(&v4, (unsigned int)(result - 104))) )
{
if ( (_BYTE)result == 115 )
{
result = *a1;
*a3 += result;
}
if ( *a2 == 109 )
{
result = *a1;
a3[1] += result;
}
if ( *a2 == 104 )
{
result = *a1;
a3[2] += result;
}
}
else
{
result = *a1;
*a3 += result;
}
return result;
}

original_code:
void setTime(int *valor, char *tipo, struct tm *tiempo) {
  if (*tipo == 's' || *tipo == 'm' || *tipo == 'h') {
    if (*tipo == 's') tiempo->tm_sec += *valor;
    if (*tipo == 'm') tiempo->tm_min += *valor;
    if (*tipo == 'h') tiempo->tm_hour += *valor;
  }
  else tiempo->tm_sec += *valor;
}

variable_types:
valor
int *

variable_types:
tipo
char *

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


eleccion

decompiled_code:
int __fastcall eleccion(_DWORD *a1, int *a2, void *a3)
{
int result; // eax

result = *a2;
if ( *a2 == 32 )
{
if ( *a1 )
{
result = mvprintw(3, 0, " ");
*a1 = 0;
}
else
{
*a1 = 1;
result = mvprintw(3, 0, "Pausado");
}
}
else if ( result == 115 )
{
endwin();
if ( a3 )
free(a3);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(_DWORD *a1, int *a2, void *a3)
{
int result; // eax

result = *a2;
if ( *a2 == 32 )
{
if ( *a1 )
{
result = mvprintw(3, 0, " ");
*a1 = 0;
}
else
{
*a1 = 1;
result = mvprintw(3, 0, "Pausado");
}
}
else if ( result == 115 )
{
endwin();
if ( a3 )
free(a3);
exit(1);
}
return result;
}

original_code:
void eleccion(int *menu, int *c, time_t *actual) {
  switch (*c) {
    case 's':
      endwin();
      if (actual != ((void *)0)) free(actual);
      exit(1);
    case ' ':
      if (*menu) {
        mvprintw(3,0,"     ");
        *menu = 0;
      }
      else {
        *menu = 1;
        mvprintw(3,0,"Pausado");
      }
      break;
    default:
      break;
  }
}

canonical_original_code:
void eleccion(int *menu, int *c, long *actual) {
  switch (*c) {
    case 's':
      endwin();
      if (actual != ((void *)0)) free(actual);
      exit(1);
    case ' ':
      if (*menu) {
        mvprintw(3,0,"     ");
        *menu = 0;
      }
      else {
        *menu = 1;
        mvprintw(3,0,"Pausado");
      }
      break;
    default:
      break;
  }
}

variable_types:
menu
int *

variable_types:
c
int *

variable_types:
actual
long *