powerToChar

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

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

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

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

original_code:
char powerToChar(int power)
{
  switch(power)
  {
    case 0:
      return 'b';
    case 1:
      return 'k';
    case 2:
      return 'M';
    case 3:
      return 'G';
    case 4:
      return 'T';
    case 5:
      return 'P';
    case 6:
      return 'E';
  }
  return '?';
}

variable_types: power int

return_type: char


statusError

repo: norly/sysstatus

decompiled_code:
int __fastcall statusError(const char *a1, const char *a2, const char *a3)
{
fprintf(stderr, "%s: %s", a1, a2);
if ( a3 )
fprintf(stderr, " -- %s", a3);
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, const char *a2, const char *a3)
{
fprintf(stderr, "%s: %s", a1, a2);
if ( a3 )
fprintf(stderr, " -- %s", a3);
return fputc(10, stderr);
}

original_code:
void statusError(char *where, char *what, char *extra)
{
  fprintf(stderr, "%s: %s", where, what);
  if (extra) {
    fprintf(stderr, " -- %s", extra);
  }
  fputs("\n", stderr);
}

variable_types:
where
char *

variable_types:
what
char *

variable_types:
extra
char *

return_type: void


fileRead

decompiled_code:
__int64 __fastcall fileRead(void *buf, __int64 a2, const char *a3)
{
int v3; // eax
int v4; // ebp
int v5; // ebx
__int64 result; // rax

v3 = open(a3, 0);
if ( v3 < 0 )
return -1LL;
v4 = v3;
v5 = read(v3, buf, a2 - 1);
close(v4);
result = v5;
if ( v5 <= 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v5) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *buf, __int64 a2, const char *a3)
{
int v3; // eax
int v4; // ebp
int v5; // ebx
__int64 result; // rax

v3 = open(a3, 0);
if ( v3 < 0 )
return -1LL;
v4 = v3;
v5 = read(v3, buf, a2 - 1);
close(v4);
result = v5;
if ( v5 <= 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v5) = 0;
return result;
}

original_code:
ssize_t fileRead(char *buf, size_t bufsize, char *file)
{
  int fd;
  int readbytes;
  fd = open(file, 0);
  if (fd < 0) {
    return -1;
  }
  readbytes = read(fd, buf, bufsize - 1);
  close(fd);
  if (readbytes > 0) {
    buf[readbytes] = '\0';
  } else {
    buf[0] = '\0';
  }
  return readbytes;
}

canonical_original_code:
long fileRead(char *buf, unsigned long bufsize, char *file)
{
  int fd;
  int readbytes;
  fd = open(file, 0);
  if (fd < 0) {
    return -1;
  }
  readbytes = read(fd, buf, bufsize - 1);
  close(fd);
  if (readbytes > 0) {
    buf[readbytes] = '\0';
  } else {
    buf[0] = '\0';
  }
  return readbytes;
}

variable_types:
buf
char *

variable_types:
bufsize
unsigned long

variable_types:
file
char *

variable_types: fd int

variable_types: readbytes int

return_type: long


decompiled_code:
int __fastcall status_netif(__int64 a1, const char *a2)
{
size_t v2; // rbx
__int64 v3; // rax
unsigned int v4; // er14
char *v5; // rbx
double v6; // xmm1_8
double v7; // xmm0_8
int v8; // ebx
__int64 v9; // r9
double v11; // [rsp+0h] [rbp-170h]
__int64 v12[2]; // [rsp+8h] [rbp-168h] BYREF
char nptr[16]; // [rsp+18h] [rbp-158h] BYREF
char s[16]; // [rsp+28h] [rbp-148h] BYREF
int v15; // [rsp+38h] [rbp-138h]
__m128i dest; // [rsp+48h] [rbp-128h] BYREF

*(_OWORD *)s = 0LL;
v15 = 0;
statusitem_init(v12);
v12[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)((int)v2 + 16 + 21LL) > 0xFF )
{
LODWORD(v3) = statusError("status_netif", "ifpath buffer too small", a2);
return v3;
}
dest = _mm_load_si128((const __m128i *)&LC7);
memcpy((char *)&dest.m128i_u64[1] + 7, a2, v2 + 1);
LODWORD(v3) = access(dest.m128i_i8, 0);
v4 = v3;
if ( !(_DWORD)v3 )
{
v5 = &dest.m128i_i8[(int)v2 + 15];
strcpy(v5, "/carrier");
v3 = fileRead(nptr, 16LL, &dest);
if ( v3 > 0 && nptr[0] == 49 )
{
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC8);
v12[0] = (__int64)"#FFFF00";
if ( fileRead(nptr, 16LL, &dest) <= 0 )
{
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC9);
v6 = 0.0;
if ( fileRead(nptr, 16LL, &dest) <= 0 )
goto LABEL_15;
}
else
{
v11 = atof(nptr);
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC9);
v6 = v11;
if ( fileRead(nptr, 16LL, &dest) <= 0 )
{
v7 = *(double *)&LC4;
if ( v11 >= 1024.0 )
{
do
{
LABEL_9:
v6 = v6 * 0.0009765625;
v8 = v4++;
}
while ( v6 >= v7 );
v9 = (unsigned int)(char)powerToChar(v4);
goto LABEL_11;
}
LABEL_15:
v8 = 0;
v9 = (unsigned int)(char)powerToChar(0LL);
LABEL_11:
snprintf(s, 0x14uLL, "%s: %.*f %c", a2, v8, v6, v9);
LODWORD(v3) = line_append_item(a1, v12);
return v3;
}
}
v6 = v6 + atof(nptr);
v7 = *(double *)&LC4;
if ( v6 >= 1024.0 )
goto LABEL_9;
goto LABEL_15;
}
}
return v3;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
size_t v2; // rbx
__int64 v3; // rax
unsigned int v4; // er14
char *v5; // rbx
double v6; // xmm1_8
double v7; // xmm0_8
int v8; // ebx
__int64 v9; // r9
double v11; // [rsp+0h] [rbp-170h]
__int64 v12[2]; // [rsp+8h] [rbp-168h] BYREF
char nptr[16]; // [rsp+18h] [rbp-158h] BYREF
char s[16]; // [rsp+28h] [rbp-148h] BYREF
int v15; // [rsp+38h] [rbp-138h]
__m128i dest; // [rsp+48h] [rbp-128h] BYREF

*(_OWORD *)s = 0LL;
v15 = 0;
statusitem_init(v12);
v12[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)((int)v2 + 16 + 21LL) > 0xFF )
{
LODWORD(v3) = statusError("status_netif", "ifpath buffer too small", a2);
return v3;
}
dest = _mm_load_si128((const __m128i *)&LC7);
memcpy((char *)&dest.m128i_u64[1] + 7, a2, v2 + 1);
LODWORD(v3) = access(dest.m128i_i8, 0);
v4 = v3;
if ( !(_DWORD)v3 )
{
v5 = &dest.m128i_i8[(int)v2 + 15];
strcpy(v5, "/carrier");
v3 = fileRead(nptr, 16LL, &dest);
if ( v3 > 0 && nptr[0] == 49 )
{
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC8);
v12[0] = (__int64)"#FFFF00";
if ( fileRead(nptr, 16LL, &dest) <= 0 )
{
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC9);
v6 = 0.0;
if ( fileRead(nptr, 16LL, &dest) <= 0 )
goto LABEL_15;
}
else
{
v11 = atof(nptr);
strcpy(v5 + 16, "ytes");
*(__m128i *)v5 = _mm_load_si128((const __m128i *)&LC9);
v6 = v11;
if ( fileRead(nptr, 16LL, &dest) <= 0 )
{
v7 = *(double *)&LC4;
if ( v11 >= 1024.0 )
{
do
{
LABEL_9:
v6 = v6 * 0.0009765625;
v8 = v4++;
}
while ( v6 >= v7 );
v9 = (unsigned int)(char)powerToChar(v4);
goto LABEL_11;
}
LABEL_15:
v8 = 0;
v9 = (unsigned int)(char)powerToChar(0LL);
LABEL_11:
snprintf(s, 0x14uLL, "%s: %.*f %c", a2, v8, v6, v9);
LODWORD(v3) = line_append_item(a1, v12);
return v3;
}
}
v6 = v6 + atof(nptr);
v7 = *(double *)&LC4;
if ( v6 >= 1024.0 )
goto LABEL_9;
goto LABEL_15;
}
}
return v3;
}

original_code:
void status_netif(GlobalData *g, char *ifname)
{
  StatusItem s;
  char text[20] = { 0 };
  char ifpath[256];
  int ifpathlen;
  char stline[16];
  ssize_t stlen;
  double ifsum = 0.0;
  int ifsumpower;
  statusitem_init(&s);
  s.text = text;
  ifpathlen = sizeof("/sys/class/net/") - 1 + strlen(ifname);
  if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
    statusError("status_netif",
                "ifpath buffer too small",
                ifname);
    return;
  }
  strcpy(ifpath, "/sys/class/net/");
  strcat(ifpath, ifname);
  if (access(ifpath, 0)) {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/carrier");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    if (stline[0] == '1') {
      s.color = "#FFFF00";
    } else {
      return;
    }
  } else {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum = atof(stline);
  }
  strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum += atof(stline);
  }
  for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
    ifsum = ifsum / 1024;
  }
  snprintf(text, sizeof(text), "%s: %.*f %c",
            ifname,
            ifsumpower ? ifsumpower - 1 : ifsumpower,
            ifsum,
            powerToChar(ifsumpower));
  line_append_item(g, &s);
}

canonical_original_code:
void status_netif(struct GlobalData *g, char *ifname)
{
  struct StatusItem s;
  char text[20] = { 0 };
  char ifpath[256];
  int ifpathlen;
  char stline[16];
  long stlen;
  double ifsum = 0.0;
  int ifsumpower;
  statusitem_init(&s);
  s.text = text;
  ifpathlen = sizeof("/sys/class/net/") - 1 + strlen(ifname);
  if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
    statusError("status_netif",
                "ifpath buffer too small",
                ifname);
    return;
  }
  strcpy(ifpath, "/sys/class/net/");
  strcat(ifpath, ifname);
  if (access(ifpath, 0)) {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/carrier");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    if (stline[0] == '1') {
      s.color = "#FFFF00";
    } else {
      return;
    }
  } else {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum = atof(stline);
  }
  strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum += atof(stline);
  }
  for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
    ifsum = ifsum / 1024;
  }
  snprintf(text, sizeof(text), "%s: %.*f %c",
            ifname,
            ifsumpower ? ifsumpower - 1 : ifsumpower,
            ifsum,
            powerToChar(ifsumpower));
  line_append_item(g, &s);
}

variable_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; } *

variable_types:
ifname
char *

variable_types: struct StatusItem { char *color; char *text; }

variable_types:
text
char [20]

variable_types:
ifpath
char [256]

variable_types: ifpathlen int

variable_types:
stline
char [16]

variable_types: stlen long

variable_types: ifsum double

variable_types: ifsumpower int


decompiled_code:
__int64 __fastcall status_temp(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-58h] BYREF
char s[16]; // [rsp+10h] [rbp-48h] BYREF
char v7[56]; // [rsp+20h] [rbp-38h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v5);
v5[1] = (__int64)s;
result = fileRead(v7, 16LL, a3);
if ( result > 0 )
{
if ( (unsigned __int64)(result - 6) <= 1 )
{
s[result + 12] = 0;
v5[0] = (__int64)"#FF33FF";
snprintf(s, 0x10uLL, "%s%s C", a2, v7);
}
else
{
v5[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-58h] BYREF
char s[16]; // [rsp+10h] [rbp-48h] BYREF
char v7[56]; // [rsp+20h] [rbp-38h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v5);
v5[1] = (__int64)s;
result = fileRead(v7, 16LL, a3);
if ( result > 0 )
{
if ( (unsigned __int64)(result - 6) <= 1 )
{
s[result + 12] = 0;
v5[0] = (__int64)"#FF33FF";
snprintf(s, 0x10uLL, "%s%s C", a2, v7);
}
else
{
v5[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v5);
}
return result;
}

original_code:
void status_temp(GlobalData *g, char *title, char *sysfile)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen < 6 || stlen > 7) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 4] = '\0';
    s.color = "#FF33FF";
    snprintf(text, sizeof(text), "%s%s C", title, stline);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_temp(struct GlobalData *g, char *title, char *sysfile)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen < 6 || stlen > 7) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 4] = '\0';
    s.color = "#FF33FF";
    snprintf(text, sizeof(text), "%s%s C", title, stline);
  }
  line_append_item(g, &s);
}

variable_types:
title
char *

variable_types:
sysfile
char *

variable_types:
text
char [16]

user_defined_types: struct StatusItem { char *color; char *text; }

user_defined_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; }


decompiled_code:
__int64 __fastcall status_uptime(__int64 a1)
{
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rax
int v5; // eax
__int64 v6; // rdi
unsigned int v7; // er12
__int64 v8; // r8
int v9; // eax
unsigned int v10; // [rsp+0h] [rbp-4Ch]
__int64 v11[2]; // [rsp+4h] [rbp-48h] BYREF
char s[16]; // [rsp+14h] [rbp-38h] BYREF
char nptr[40]; // [rsp+24h] [rbp-28h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v11);
v11[1] = (__int64)s;
result = fileRead(nptr, 16LL, "/proc/uptime");
if ( result >= 0 )
{
if ( result )
{
v3 = result;
v4 = 0LL;
while ( nptr[v4] != 32 )
{
if ( v3 == ++v4 )
goto LABEL_7;
}
nptr[(int)v4] = 0;
}
LABEL_7:
v5 = atoi(nptr);
v6 = 4LL;
v11[0] = (__int64)"#AAAAAA";
strcpy(s, "up: ");
v7 = v5 % 86400 / 3600;
v8 = (unsigned int)(v5 % 86400 % 3600 / 60);
if ( v5 > 86399 )
{
v10 = v5 % 86400 % 3600 / 60;
v9 = snprintf(&s[4], 0xCuLL, "%dd ", (unsigned int)(v5 / 86400));
v8 = v10;
v6 = (unsigned int)(v9 + 4);
}
snprintf(&s[v6], 16 - v6, "%d:%.2d", v7, v8);
result = line_append_item(a1, v11);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rax
int v5; // eax
__int64 v6; // rdi
unsigned int v7; // er12
__int64 v8; // r8
int v9; // eax
unsigned int v10; // [rsp+0h] [rbp-4Ch]
__int64 v11[2]; // [rsp+4h] [rbp-48h] BYREF
char s[16]; // [rsp+14h] [rbp-38h] BYREF
char nptr[40]; // [rsp+24h] [rbp-28h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v11);
v11[1] = (__int64)s;
result = fileRead(nptr, 16LL, "/proc/uptime");
if ( result >= 0 )
{
if ( result )
{
v3 = result;
v4 = 0LL;
while ( nptr[v4] != 32 )
{
if ( v3 == ++v4 )
goto LABEL_7;
}
nptr[(int)v4] = 0;
}
LABEL_7:
v5 = atoi(nptr);
v6 = 4LL;
v11[0] = (__int64)"#AAAAAA";
strcpy(s, "up: ");
v7 = v5 % 86400 / 3600;
v8 = (unsigned int)(v5 % 86400 % 3600 / 60);
if ( v5 > 86399 )
{
v10 = v5 % 86400 % 3600 / 60;
v9 = snprintf(&s[4], 0xCuLL, "%dd ", (unsigned int)(v5 / 86400));
v8 = v10;
v6 = (unsigned int)(v9 + 4);
}
snprintf(&s[v6], 16 - v6, "%d:%.2d", v7, v8);
result = line_append_item(a1, v11);
}
return result;
}

original_code:
void status_uptime(GlobalData *g)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  int i;
  int upts, uptm, upth, uptd;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
  if (stlen < 0) {
    s.color = "#FF0000";
    s.text = "up: ERROR";
  } else {
    unsigned textlen = 0;
    for(i = 0; i < stlen; i++) {
      if (stline[i] == ' ') {
        stline[i] = '\0';
        break;
      }
    }
    upts = atoi(stline);
    uptd = upts / (24 * 60 * 60);
    upts -= uptd * (24 * 60 * 60);
    upth = upts / (60 * 60);
    upts -= upth * (60 * 60);
    uptm = upts / (60);
    upts -= uptm * (60);
    s.color = "#AAAAAA";
    textlen = snprintf(text, sizeof(text), "up: ");
    if (uptd > 0) {
      textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
    }
    snprintf(&text[textlen], sizeof(text) - textlen,
              "%d:%.2d"
              ""
              ,upth
              ,uptm
            );
    line_append_item(g, &s);
  }
}

canonical_original_code:
void status_uptime(struct GlobalData *g)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  int i;
  int upts; int uptm; int upth; int uptd;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
  if (stlen < 0) {
    s.color = "#FF0000";
    s.text = "up: ERROR";
  } else {
    unsigned int textlen = 0;
    for(i = 0; i < stlen; i++) {
      if (stline[i] == ' ') {
        stline[i] = '\0';
        break;
      }
    }
    upts = atoi(stline);
    uptd = upts / (24 * 60 * 60);
    upts -= uptd * (24 * 60 * 60);
    upth = upts / (60 * 60);
    upts -= upth * (60 * 60);
    uptm = upts / (60);
    upts -= uptm * (60);
    s.color = "#AAAAAA";
    textlen = snprintf(text, sizeof(text), "up: ");
    if (uptd > 0) {
      textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
    }
    snprintf(&text[textlen], sizeof(text) - textlen,
              "%d:%.2d"
              ""
              ,upth
              ,uptm
            );
    line_append_item(g, &s);
  }
}

variable_types: i int

variable_types: upts int

variable_types: uptm int

variable_types: upth int

variable_types: uptd int

variable_types:
textlen
unsigned int


decompiled_code:
int __fastcall status_battery(__int64 a1, const char *a2)
{
size_t v2; // r14
int result; // eax
int v4; // ebx
char *v5; // r15
int v6; // er14
int v7; // eax
__int64 v8; // r8
bool v9; // cc
const char *v10; // rax
int v11; // [rsp+0h] [rbp-180h]
int v12; // [rsp+4h] [rbp-17Ch]
__int64 v13[2]; // [rsp+8h] [rbp-178h] BYREF
char nptr[16]; // [rsp+18h] [rbp-168h] BYREF
char s[16]; // [rsp+28h] [rbp-158h] BYREF
__int128 v16; // [rsp+38h] [rbp-148h]
char name[16]; // [rsp+48h] [rbp-138h] BYREF
__int64 v18; // [rsp+58h] [rbp-128h]
char dest[288]; // [rsp+60h] [rbp-120h] BYREF

*(_OWORD *)s = 0LL;
v16 = 0LL;
statusitem_init(v13);
v13[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)((int)v2 + 25 + 13LL) > 0xFF )
return statusError("status_battery", "batpath buffer too small", a2);
*(__m128i *)name = _mm_load_si128((const __m128i *)&LC10);
v18 = 0x2F796C707075735FLL;
memcpy(dest, a2, v2 + 1);
result = access(name, 0);
v4 = result;
if ( result )
return result;
v5 = &name[(int)v2 + 24];
strcpy(v5, "/charge_now");
v12 = 0;
if ( fileRead(nptr, 16LL, name) > 0 )
v12 = atoi(nptr);
strcpy(v5, "/charge_full");
v6 = -1;
if ( fileRead(nptr, 16LL, name) > 0 )
v6 = atoi(nptr);
strcpy(v5, "/current_now");
v11 = 1;
if ( fileRead(nptr, 16LL, name) > 0 )
v11 = atoi(nptr);
strcpy(v5, "/voltage_now");
if ( fileRead(nptr, 16LL, name) > 0 )
v4 = atoi(nptr);
if ( v6 <= 0 )
{
v8 = 0xFFFFFFFFLL;
LABEL_20:
v13[0] = (__int64)"#FF0000";
goto LABEL_15;
}
v7 = v12 / (v6 / 100);
v8 = (unsigned int)v7;
if ( v7 > 40 )
{
v9 = v7 < 71;
v10 = "#22FF22";
if ( !v9 )
v10 = "#FFFFFF";
v13[0] = (__int64)v10;
goto LABEL_15;
}
if ( v7 <= 25 )
{
if ( v7 > 10 )
{
v13[0] = (__int64)"#FFA500";
goto LABEL_15;
}
goto LABEL_20;
}
v13[0] = (__int64)"#FFFF00";
LABEL_15:
if ( v11 )
snprintf(
s,
0x20uLL,
"%s: %d%% %.1fh %.1fW",
a2,
v8,
(float)((float)v12 / (float)v11),
(float)v4 / 1000000.0 * (float)v11 / 1000000.0);
else
snprintf(s, 0x20uLL, "%s: %d%% _ _", a2, v8);
return line_append_item(a1, v13);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
size_t v2; // r14
int result; // eax
int v4; // ebx
char *v5; // r15
int v6; // er14
int v7; // eax
__int64 v8; // r8
bool v9; // cc
const char *v10; // rax
int v11; // [rsp+0h] [rbp-180h]
int v12; // [rsp+4h] [rbp-17Ch]
__int64 v13[2]; // [rsp+8h] [rbp-178h] BYREF
char nptr[16]; // [rsp+18h] [rbp-168h] BYREF
char s[16]; // [rsp+28h] [rbp-158h] BYREF
__int128 v16; // [rsp+38h] [rbp-148h]
char name[16]; // [rsp+48h] [rbp-138h] BYREF
__int64 v18; // [rsp+58h] [rbp-128h]
char dest[288]; // [rsp+60h] [rbp-120h] BYREF

*(_OWORD *)s = 0LL;
v16 = 0LL;
statusitem_init(v13);
v13[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)((int)v2 + 25 + 13LL) > 0xFF )
return statusError("status_battery", "batpath buffer too small", a2);
*(__m128i *)name = _mm_load_si128((const __m128i *)&LC10);
v18 = 0x2F796C707075735FLL;
memcpy(dest, a2, v2 + 1);
result = access(name, 0);
v4 = result;
if ( result )
return result;
v5 = &name[(int)v2 + 24];
strcpy(v5, "/charge_now");
v12 = 0;
if ( fileRead(nptr, 16LL, name) > 0 )
v12 = atoi(nptr);
strcpy(v5, "/charge_full");
v6 = -1;
if ( fileRead(nptr, 16LL, name) > 0 )
v6 = atoi(nptr);
strcpy(v5, "/current_now");
v11 = 1;
if ( fileRead(nptr, 16LL, name) > 0 )
v11 = atoi(nptr);
strcpy(v5, "/voltage_now");
if ( fileRead(nptr, 16LL, name) > 0 )
v4 = atoi(nptr);
if ( v6 <= 0 )
{
v8 = 0xFFFFFFFFLL;
LABEL_20:
v13[0] = (__int64)"#FF0000";
goto LABEL_15;
}
v7 = v12 / (v6 / 100);
v8 = (unsigned int)v7;
if ( v7 > 40 )
{
v9 = v7 < 71;
v10 = "#22FF22";
if ( !v9 )
v10 = "#FFFFFF";
v13[0] = (__int64)v10;
goto LABEL_15;
}
if ( v7 <= 25 )
{
if ( v7 > 10 )
{
v13[0] = (__int64)"#FFA500";
goto LABEL_15;
}
goto LABEL_20;
}
v13[0] = (__int64)"#FFFF00";
LABEL_15:
if ( v11 )
snprintf(
s,
0x20uLL,
"%s: %d%% %.1fh %.1fW",
a2,
v8,
(float)((float)v12 / (float)v11),
(float)v4 / 1000000.0 * (float)v11 / 1000000.0);
else
snprintf(s, 0x20uLL, "%s: %d%% _ _", a2, v8);
return line_append_item(a1, v13);
}

original_code:
void status_battery(GlobalData *g, char *batname)
{
  StatusItem s;
  char text[32] = { 0 };
  char batpath[256];
  int batpathlen;
  char stline[16];
  ssize_t stlen;
  int chargeNow = 0;
  int chargeFull = -1;
  int chargePercent = -1;
  int currentNow = 1;
  int voltageNow = 0;
  float battTime = -1;
  statusitem_init(&s);
  s.text = text;
  batpathlen = sizeof("/sys/class/power_supply/") - 1 + strlen(batname);
  if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
    statusError("status_battery",
                "batpath buffer too small",
                batname);
    return;
  }
  strcpy(batpath, "/sys/class/power_supply/");
  strcat(batpath, batname);
  if (access(batpath, 0)) {
    return;
  }
  strcpy(&batpath[batpathlen], "/charge_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/charge_full");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeFull = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/current_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    currentNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/voltage_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    voltageNow = atoi(stline);
  }
  if (chargeFull > 0) {
    chargePercent = chargeNow / (chargeFull / 100);
  }
  if (chargePercent <= 40) {
    if (chargePercent <= 25) {
      if (chargePercent <= 10) {
        s.color = "#FF0000";
      } else {
        s.color = "#FFA500";
      }
    } else {
      s.color = "#FFFF00";
    }
  } else {
    if (chargePercent > 70) {
      s.color = "#FFFFFF";
    } else {
      s.color = "#22FF22";
    }
  }
  battTime = (float)chargeNow / (float)currentNow;
  if (currentNow == 0) {
    snprintf(text, sizeof(text), "%s: %d%% _ _",
              batname, chargePercent);
  } else {
    snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
              batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_battery(struct GlobalData *g, char *batname)
{
  struct StatusItem s;
  char text[32] = { 0 };
  char batpath[256];
  int batpathlen;
  char stline[16];
  long stlen;
  int chargeNow = 0;
  int chargeFull = -1;
  int chargePercent = -1;
  int currentNow = 1;
  int voltageNow = 0;
  float battTime = -1;
  statusitem_init(&s);
  s.text = text;
  batpathlen = sizeof("/sys/class/power_supply/") - 1 + strlen(batname);
  if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
    statusError("status_battery",
                "batpath buffer too small",
                batname);
    return;
  }
  strcpy(batpath, "/sys/class/power_supply/");
  strcat(batpath, batname);
  if (access(batpath, 0)) {
    return;
  }
  strcpy(&batpath[batpathlen], "/charge_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/charge_full");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeFull = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/current_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    currentNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/voltage_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    voltageNow = atoi(stline);
  }
  if (chargeFull > 0) {
    chargePercent = chargeNow / (chargeFull / 100);
  }
  if (chargePercent <= 40) {
    if (chargePercent <= 25) {
      if (chargePercent <= 10) {
        s.color = "#FF0000";
      } else {
        s.color = "#FFA500";
      }
    } else {
      s.color = "#FFFF00";
    }
  } else {
    if (chargePercent > 70) {
      s.color = "#FFFFFF";
    } else {
      s.color = "#22FF22";
    }
  }
  battTime = (float)chargeNow / (float)currentNow;
  if (currentNow == 0) {
    snprintf(text, sizeof(text), "%s: %d%% _ _",
              batname, chargePercent);
  } else {
    snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
              batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
  }
  line_append_item(g, &s);
}

variable_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; } *

variable_types:
batname
char *

variable_types: struct StatusItem { char *color; char *text; }

variable_types:
text
char [32]

variable_types:
batpath
char [256]

variable_types: batpathlen int

variable_types: chargeNow int

variable_types: chargeFull int

variable_types: chargePercent int

variable_types: currentNow int

variable_types: voltageNow int

variable_types: battTime float


decompiled_code:
__int64 __fastcall status_datetime(__int64 a1)
{
struct tm *v1; // rax
struct tm *v2; // rbx
__int64 v3; // rcx
__int64 v4; // r8
__int64 result; // rax
time_t timer; // [rsp+0h] [rbp-50h] BYREF
const char *v7; // [rsp+8h] [rbp-48h] BYREF
char *v8; // [rsp+10h] [rbp-40h]
char s[16]; // [rsp+18h] [rbp-38h] BYREF
__int128 v10; // [rsp+28h] [rbp-28h]

*(_OWORD *)s = 0LL;
v10 = 0LL;
statusitem_init(&v7);
v8 = s;
timer = time(0LL);
if ( timer == -1 )
{
v7 = "#FF0000";
v8 = "ERROR: DATETIME";
result = line_append_item(a1, &v7);
}
else
{
v1 = localtime(&timer);
v7 = "#666666";
v2 = v1;
snprintf(
s,
0x20uLL,
"%d.%d.%d",
(unsigned int)v1->tm_mday,
(unsigned int)(v1->tm_mon + 1),
(unsigned int)(v1->tm_year + 1900));
line_append_item(a1, &v7);
v3 = (unsigned int)v2->tm_hour;
v4 = (unsigned int)v2->tm_min;
v7 = "#BEBEBE";
snprintf(s, 0x20uLL, "%d:%.2d", v3, v4);
result = line_append_item(a1, &v7);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
struct tm *v1; // rax
struct tm *v2; // rbx
__int64 v3; // rcx
__int64 v4; // r8
__int64 result; // rax
time_t timer; // [rsp+0h] [rbp-50h] BYREF
const char *v7; // [rsp+8h] [rbp-48h] BYREF
char *v8; // [rsp+10h] [rbp-40h]
char s[16]; // [rsp+18h] [rbp-38h] BYREF
__int128 v10; // [rsp+28h] [rbp-28h]

*(_OWORD *)s = 0LL;
v10 = 0LL;
statusitem_init(&v7);
v8 = s;
timer = time(0LL);
if ( timer == -1 )
{
v7 = "#FF0000";
v8 = "ERROR: DATETIME";
result = line_append_item(a1, &v7);
}
else
{
v1 = localtime(&timer);
v7 = "#666666";
v2 = v1;
snprintf(
s,
0x20uLL,
"%d.%d.%d",
(unsigned int)v1->tm_mday,
(unsigned int)(v1->tm_mon + 1),
(unsigned int)(v1->tm_year + 1900));
line_append_item(a1, &v7);
v3 = (unsigned int)v2->tm_hour;
v4 = (unsigned int)v2->tm_min;
v7 = "#BEBEBE";
snprintf(s, 0x20uLL, "%d:%.2d", v3, v4);
result = line_append_item(a1, &v7);
}
return result;
}

original_code:
void status_datetime(GlobalData *g)
{
  StatusItem s;
  char text[32] = { 0 };
  time_t nows = 0;
  struct tm *nowtm;
  statusitem_init(&s);
  s.text = text;
  nows = time(((void *)0));
  if (nows == ((time_t) -1)) {
    s.color = "#FF0000";
    s.text = "ERROR: DATETIME";
    line_append_item(g, &s);
  } else {
    nowtm = localtime(&nows);
    s.color = "#666666";
    snprintf(text, sizeof(text),
              "%d.%d.%d",
              nowtm->tm_mday,
              (nowtm->tm_mon) + 1,
              (nowtm->tm_year) + 1900
            );
    line_append_item(g, &s);
    s.color = "#BEBEBE";
    snprintf(text, sizeof(text),
              "%d:%.2d"
              ,nowtm->tm_hour,
              nowtm->tm_min
            );
    line_append_item(g, &s);
  }
}

canonical_original_code:
void status_datetime(struct GlobalData *g)
{
  struct StatusItem s;
  char text[32] = { 0 };
  long nows = 0;
  struct tm *nowtm;
  statusitem_init(&s);
  s.text = text;
  nows = time(((void *)0));
  if (nows == ((time_t) -1)) {
    s.color = "#FF0000";
    s.text = "ERROR: DATETIME";
    line_append_item(g, &s);
  } else {
    nowtm = localtime(&nows);
    s.color = "#666666";
    snprintf(text, sizeof(text),
              "%d.%d.%d",
              nowtm->tm_mday,
              (nowtm->tm_mon) + 1,
              (nowtm->tm_year) + 1900
            );
    line_append_item(g, &s);
    s.color = "#BEBEBE";
    snprintf(text, sizeof(text),
              "%d:%.2d"
              ,nowtm->tm_hour,
              nowtm->tm_min
            );
    line_append_item(g, &s);
  }
}

variable_types: nows 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; } *


decompiled_code:
__int64 __fastcall status_fan(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-58h] BYREF
char s[16]; // [rsp+10h] [rbp-48h] BYREF
char v7[56]; // [rsp+20h] [rbp-38h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v5);
v5[1] = (__int64)s;
result = fileRead(v7, 16LL, a3);
if ( result > 0 )
{
if ( result > 5 )
{
v5[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
else
{
s[result + 15] = 0;
v5[0] = (__int64)"#CCCCCC";
snprintf(s, 0x10uLL, "%s%s rpm", a2, v7);
}
result = line_append_item(a1, v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-58h] BYREF
char s[16]; // [rsp+10h] [rbp-48h] BYREF
char v7[56]; // [rsp+20h] [rbp-38h] BYREF

*(_OWORD *)s = 0LL;
statusitem_init(v5);
v5[1] = (__int64)s;
result = fileRead(v7, 16LL, a3);
if ( result > 0 )
{
if ( result > 5 )
{
v5[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
else
{
s[result + 15] = 0;
v5[0] = (__int64)"#CCCCCC";
snprintf(s, 0x10uLL, "%s%s rpm", a2, v7);
}
result = line_append_item(a1, v5);
}
return result;
}

original_code:
void status_fan(GlobalData *g, char *title, char *sysfile)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen > 5) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 1] = '\0';
    s.color = "#CCCCCC";
    snprintf(text, sizeof(text), "%s%s rpm", title, stline);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_fan(struct GlobalData *g, char *title, char *sysfile)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen > 5) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 1] = '\0';
    s.color = "#CCCCCC";
    snprintf(text, sizeof(text), "%s%s rpm", title, stline);
  }
  line_append_item(g, &s);
}

variable_types:
title
char *

variable_types:
sysfile
char *

variable_types:
text
char [16]

variable_types:
stline
char [16]

variable_types: stlen long


repo: notfed/nonce

decompiled_code:
__int64 __fastcall nonce_next(__int64 a1, __int64 a2)
{
unsigned int v3; // ebp
int v4; // eax
const char *v5; // rsi
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
int v10; // eax
__m128i v11; // xmm0
int v12; // eax
__int64 v14; // [rsp-8h] [rbp-90h]
char buf[12]; // [rsp+4h] [rbp-84h] BYREF
__m128i v16; // [rsp+10h] [rbp-78h] BYREF
__int64 v17; // [rsp+20h] [rbp-68h]
__m128i v18; // [rsp+30h] [rbp-58h] BYREF
__int64 v19; // [rsp+40h] [rbp-48h]
char v20[8]; // [rsp+50h] [rbp-38h] BYREF
__int64 v21; // [rsp+58h] [rbp-30h]

byte_zero(buf, 12LL);
byte_zero(v20, 24LL);
v21 = 1LL;
LABEL_2:
while ( 1 )
{
v3 = open_rw(a1);
if ( v3 != -1 )
break;
while ( 1 )
{
v4 = *_errno_location();
if ( v4 == error_intr || v4 == error_again )
{
sleep(1u);
goto LABEL_2;
}
if ( v4 == error_noent )
break;
strerr_die(111LL, "trynonce: error opening nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
v3 = open_rw(a1);
if ( v3 != -1 )
goto LABEL_7;
}
nonce_create(a1);
}
LABEL_7:
while ( 1 )
{
v5 = buf;
v6 = read(v3, buf, 0xCuLL);
if ( v6 != -1 )
break;
v10 = *_errno_location();
if ( v10 == error_intr || v10 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error reading nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
if ( v6 != 12 )
{
v5 = "trynonce: error: short nonce";
strerr_die(111LL, "trynonce: error: short nonce", 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
v9 = v14;
}
taia_now(&v18, v5, v7, v8, v9);
taia_unpack(buf, &v16);
if ( (unsigned int)taia_less(&v18, &v16) )
{
v11 = _mm_loadu_si128(&v16);
v19 = v17;
v18 = v11;
}
taia_add(&v18, &v18, v20);
taia_pack(buf, &v18);
if ( (unsigned int)seek_set(v3, 0LL) == -1 )
strerr_die(111LL, "trynonce: error rewinding nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
while ( (unsigned int)write(v3, buf, 0xCuLL) == -1 )
{
v12 = *_errno_location();
if ( v12 == error_intr || v12 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error writing nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
close(v3);
return byte_copy(a2, 12LL, buf);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned int v3; // ebp
int v4; // eax
const char *v5; // rsi
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
int v10; // eax
__m128i v11; // xmm0
int v12; // eax
__int64 v14; // [rsp-8h] [rbp-90h]
char buf[12]; // [rsp+4h] [rbp-84h] BYREF
__m128i v16; // [rsp+10h] [rbp-78h] BYREF
__int64 v17; // [rsp+20h] [rbp-68h]
__m128i v18; // [rsp+30h] [rbp-58h] BYREF
__int64 v19; // [rsp+40h] [rbp-48h]
char v20[8]; // [rsp+50h] [rbp-38h] BYREF
__int64 v21; // [rsp+58h] [rbp-30h]

byte_zero(buf, 12LL);
byte_zero(v20, 24LL);
v21 = 1LL;
LABEL_2:
while ( 1 )
{
v3 = open_rw(a1);
if ( v3 != -1 )
break;
while ( 1 )
{
v4 = *_errno_location();
if ( v4 == error_intr || v4 == error_again )
{
sleep(1u);
goto LABEL_2;
}
if ( v4 == error_noent )
break;
strerr_die(111LL, "trynonce: error opening nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
v3 = open_rw(a1);
if ( v3 != -1 )
goto LABEL_7;
}
nonce_create(a1);
}
LABEL_7:
while ( 1 )
{
v5 = buf;
v6 = read(v3, buf, 0xCuLL);
if ( v6 != -1 )
break;
v10 = *_errno_location();
if ( v10 == error_intr || v10 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error reading nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
if ( v6 != 12 )
{
v5 = "trynonce: error: short nonce";
strerr_die(111LL, "trynonce: error: short nonce", 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
v9 = v14;
}
taia_now(&v18, v5, v7, v8, v9);
taia_unpack(buf, &v16);
if ( (unsigned int)taia_less(&v18, &v16) )
{
v11 = _mm_loadu_si128(&v16);
v19 = v17;
v18 = v11;
}
taia_add(&v18, &v18, v20);
taia_pack(buf, &v18);
if ( (unsigned int)seek_set(v3, 0LL) == -1 )
strerr_die(111LL, "trynonce: error rewinding nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
while ( (unsigned int)write(v3, buf, 0xCuLL) == -1 )
{
v12 = *_errno_location();
if ( v12 == error_intr || v12 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error writing nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
close(v3);
return byte_copy(a2, 12LL, buf);
}

original_code:
void nonce_next(const char *filepath, char *nonce)
{
 int fd;
 int i;
 int j;
        struct taia oldtemps;
        struct taia nowtemps;
        struct taia onenanosecond;
        char tmpnonce[12];
        byte_zero(tmpnonce,12);
        byte_zero((char*)&onenanosecond,sizeof(onenanosecond));
        onenanosecond.nano = 1;
 while((fd=open_rw(filepath))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else if((*__errno_location ())==error_noent)
   { nonce_create(filepath); continue; }
   else
   { strerr_die((111),("trynonce: error opening nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=read(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error reading nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 if(i!=12) {
   strerr_die((111),("trynonce: error: short nonce"),0,0,0,0,0,0);
 }
        taia_now(&nowtemps);
        taia_unpack(tmpnonce,&oldtemps);
        if(taia_less(&nowtemps,&oldtemps))
            nowtemps = oldtemps;
        taia_add(&nowtemps,&nowtemps,&onenanosecond);
        taia_pack(&tmpnonce,&nowtemps);
 if((seek_set((fd),(seek_pos) 0))==-1)
   strerr_die((111),("trynonce: error rewinding nonce: "),0,0,0,0,0,&strerr_sys);
 while((i=write(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error writing nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
 byte_copy(nonce,12,tmpnonce);
}

canonical_original_code:
void nonce_next(char *filepath, char *nonce)
{
 int fd;
 int i;
 int j;
        struct taia oldtemps;
        struct taia nowtemps;
        struct taia onenanosecond;
        char tmpnonce[12];
        byte_zero(tmpnonce,12);
        byte_zero((char *)&onenanosecond,sizeof(onenanosecond));
        onenanosecond.nano = 1;
 while((fd=open_rw(filepath))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else if((*__errno_location ())==error_noent)
   { nonce_create(filepath); continue; }
   else
   { strerr_die((111),("trynonce: error opening nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=read(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error reading nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 if(i!=12) {
   strerr_die((111),("trynonce: error: short nonce"),0,0,0,0,0,0);
 }
        taia_now(&nowtemps);
        taia_unpack(tmpnonce,&oldtemps);
        if(taia_less(&nowtemps,&oldtemps))
            nowtemps = oldtemps;
        taia_add(&nowtemps,&nowtemps,&onenanosecond);
        taia_pack(&tmpnonce,&nowtemps);
 if((seek_set((fd),(unsigned long) 0))==-1)
   strerr_die((111),("trynonce: error rewinding nonce: "),0,0,0,0,0,&strerr_sys);
 while((i=write(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error writing nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
 byte_copy(nonce,12,tmpnonce);
}

variable_types:
filepath
char *

variable_types:
nonce
char *

variable_types: fd int

variable_types: i int

variable_types: j int

variable_types: struct taia { struct tai { unsigned long long x; } sec; unsigned long nano; unsigned long atto; }

variable_types:
tmpnonce
char [12]

user_defined_types: struct taia { struct tai sec; unsigned long nano; unsigned long atto; }


decompiled_code:
int __fastcall nonce_create(__int64 a1)
{
int v1; // ebp
int result; // eax
int v3; // eax
char buf[36]; // [rsp+4h] [rbp-24h] BYREF

byte_zero(buf, 12LL);
while ( 1 )
{
v1 = open_excl(a1);
if ( v1 != -1 )
break;
while ( 1 )
{
result = *_errno_location();
if ( result == error_intr || result == error_again )
break;
if ( result == error_exist )
return result;
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL);
v1 = open_excl(a1);
if ( v1 != -1 )
goto LABEL_7;
}
sleep(1u);
}
LABEL_7:
while ( (unsigned int)write(v1, buf, 0xCuLL) == -1 )
{
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL);
}
return close(v1);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int v1; // ebp
int result; // eax
int v3; // eax
char buf[36]; // [rsp+4h] [rbp-24h] BYREF

byte_zero(buf, 12LL);
while ( 1 )
{
v1 = open_excl(a1);
if ( v1 != -1 )
break;
while ( 1 )
{
result = *_errno_location();
if ( result == error_intr || result == error_again )
break;
if ( result == error_exist )
return result;
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL);
v1 = open_excl(a1);
if ( v1 != -1 )
goto LABEL_7;
}
sleep(1u);
}
LABEL_7:
while ( (unsigned int)write(v1, buf, 0xCuLL) == -1 )
{
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL);
}
return close(v1);
}

original_code:
void nonce_create(const char *filepath)
{
 int fd;
 int i;
 char zerononce[12];
 byte_zero(zerononce,12);
        while((fd=open_excl(filepath))==-1) {
          if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else if((*__errno_location ())==error_exist)
   { return; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=write(fd,zerononce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
}

canonical_original_code:
void nonce_create(char *filepath)
{
 int fd;
 int i;
 char zerononce[12];
 byte_zero(zerononce,12);
        while((fd=open_excl(filepath))==-1) {
          if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else if((*__errno_location ())==error_exist)
   { return; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=write(fd,zerononce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
}

variable_types:
zerononce
char [12]


decompiled_code:
__int64 __fastcall nonce_show(unsigned __int8 *a1)
{
__int64 v1; // rbx
unsigned int v2; // eax
unsigned int v3; // eax
__int64 v4; // rdx
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = 0LL;
buffer_put(buffer_1, &unk_DF, 1LL);
while ( 1 )
{
if ( v1 )
{
buffer_put(buffer_1, &unk_E1, 1LL);
v3 = fmt_xlong(v6, a1[v1]);
v4 = v3;
if ( v3 != 1 )
goto LABEL_7;
}
else
{
v2 = fmt_xlong(v6, *a1);
if ( v2 != 1 )
{
buffer_put(buffer_1, v6, v2);
goto LABEL_4;
}
}
buffer_put(buffer_1, &unk_E3, 1LL);
v4 = 1LL;
LABEL_7:
buffer_put(buffer_1, v6, v4);
if ( v1 == 11 )
break;
LABEL_4:
++v1;
}
buffer_put(buffer_1, &unk_E5, 2LL);
return buffer_flush(buffer_1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
__int64 v1; // rbx
unsigned int v2; // eax
unsigned int v3; // eax
__int64 v4; // rdx
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = 0LL;
buffer_put(buffer_1, &unk_DF, 1LL);
while ( 1 )
{
if ( v1 )
{
buffer_put(buffer_1, &unk_E1, 1LL);
v3 = fmt_xlong(v6, a1[v1]);
v4 = v3;
if ( v3 != 1 )
goto LABEL_7;
}
else
{
v2 = fmt_xlong(v6, *a1);
if ( v2 != 1 )
{
buffer_put(buffer_1, v6, v2);
goto LABEL_4;
}
}
buffer_put(buffer_1, &unk_E3, 1LL);
v4 = 1LL;
LABEL_7:
buffer_put(buffer_1, v6, v4);
if ( v1 == 11 )
break;
LABEL_4:
++v1;
}
buffer_put(buffer_1, &unk_E5, 2LL);
return buffer_flush(buffer_1);
}

original_code:
void nonce_show(const char *nonce)
{
 unsigned int i;
 unsigned int len;
 unsigned char buf[40];
 buffer_put(buffer_1,"{",1);
 for(i=0;i<12;i++)
 {
   if(i!=0) buffer_put(buffer_1,",",1);
   len = fmt_xlong(buf,(unsigned char)nonce[i]);
   if(len==1) buffer_put(buffer_1,"0",1);
   buffer_put(buffer_1,buf,len);
 }
 buffer_put(buffer_1,"}\n",2);
 buffer_flush(buffer_1);
}

canonical_original_code:
void nonce_show(char *nonce)
{
 unsigned int i;
 unsigned int len;
 unsigned char buf[40];
 buffer_put(buffer_1,"{",1);
 for(i=0;i<12;i++)
 {
   if(i!=0) buffer_put(buffer_1,",",1);
   len = fmt_xlong(buf,(unsigned char)nonce[i]);
   if(len==1) buffer_put(buffer_1,"0",1);
   buffer_put(buffer_1,buf,len);
 }
 buffer_put(buffer_1,"}\n",2);
 buffer_flush(buffer_1);
}

variable_types:
i
unsigned int

variable_types:
len
unsigned int

variable_types:
buf
unsigned char [40]


numbits

decompiled_code:
__int64 __fastcall numbits(int a1)
{
__int64 result; // rax
unsigned int i; // edi

result = 0LL;
for ( i = a1 - 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 result; // rax
unsigned int i; // edi

result = 0LL;
for ( i = a1 - 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

original_code:
static int numbits(unsigned int num)
{
  register unsigned int c;
  register unsigned int n;
  c = 0;
  n = num-1;
  while(n) { ++c; n/=2; }
  return c;
}

variable_types:
num
unsigned int

variable_types:
c
unsigned int

variable_types:
n
unsigned int

return_type: int


main

canonical_name: main

repo: notfed/trybloop

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
const char *v4; // rdi
int result; // eax
int v6; // [rsp+10h] [rbp-28h] BYREF
__int64 v7; // [rsp+14h] [rbp-24h]
__int64 (__fastcall *v8)(); // [rsp+20h] [rbp-18h]

if ( argc == 2 && (v4 = argv[1], (int)__isoc99_sscanf() >= 0) )
{
portaudio_init(v4, "%f", v3);
v7 = 0x447A00003F800000LL;
v6 = 0;
v8 = beep_waveform_sine;
beep_note_play_constprop_0(&v6);
portaudio_teardown();
result = 0;
}
else
{
puts("beep: error: usage: beep freq");
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
const char *v4; // rdi
int result; // eax
int v6; // [rsp+10h] [rbp-28h] BYREF
__int64 v7; // [rsp+14h] [rbp-24h]
__int64 (__fastcall *v8)(); // [rsp+20h] [rbp-18h]

if ( argc == 2 && (v4 = argv[1], (int)__isoc99_sscanf() >= 0) )
{
func9(v4, "%f", v3);
v7 = 0x447A00003F800000LL;
v6 = 0;
v8 = beep_waveform_sine;
func11(&v6);
func10();
result = 0;
}
else
{
puts("beep: error: usage: beep freq");
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
    float freq = 0;
    if(argc != 2 || sscanf(argv[1],"%f",&freq)<0)
    {
       printf("beep: error: usage: beep freq\n");
       return 1;
    }
    portaudio_init();
    beep_note note = {
       .frequency = freq,
       .amplitude = 1.0,
       .duration = 1000,
       .waveform = beep_waveform_sine
    };
    beep_note_play(&note);
    portaudio_teardown();
}

canonical_original_code:
int main(int argc, char *argv[])
{
    float freq = 0;
    if(argc != 2 || sscanf(argv[1],"%f",&freq)<0)
    {
       printf("beep: error: usage: beep freq\n");
       return 1;
    }
    portaudio_init();
    struct beep_note note = {
       .frequency = freq,
       .amplitude = 1.0,
       .duration = 1000,
       .waveform = beep_waveform_sine
    };
    beep_note_play(&note);
    portaudio_teardown();
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: freq float

variable_types: struct beep_note { float frequency; float amplitude; float duration; float (*waveform)(struct beep_head { struct beep_note *note; int frame; } *); }

user_defined_types: struct beep_note { float frequency; float amplitude; float duration; float (*waveform)(struct beep_head *); }


decompiled_code:
__int64 __fastcall beep_head_callback(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, _DWORD *a6)
{
__int64 i; // rbp

if ( !a3 )
return 0LL;
for ( i = 0LL; i != a3; ++i )
{
*(float *)(a2 + 4 * i) = (*(float (__fastcall **)(_DWORD *))(*(_QWORD *)a6 + 16LL))(a6);
++a6[2];
}
return 0LL;
}

original_code:
static int beep_head_callback(
 const void *inputBuffer,
 void *outputBuffer,
 unsigned long framesPerBuffer,
 const PaStreamCallbackTimeInfo* timeInfo,
 PaStreamCallbackFlags statusFlags,
 void *data)
{
       beep_head* head = (beep_head*)(data);
       float *buffer = (float*)outputBuffer;
       for(int frame = 0; frame < framesPerBuffer; frame++)
       {
           buffer[frame] = head->note->waveform(head);
           head->frame++;
       }
       return paContinue;
}

canonical_original_code:
static int beep_head_callback(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, struct PaStreamCallbackTimeInfo *timeInfo, unsigned long statusFlags, void *data)
{
       struct beep_head *head = (beep_head*)(data);
       float *buffer = (float*)outputBuffer;
       for(int frame = 0; frame < framesPerBuffer; frame++)
       {
           buffer[frame] = head->note->waveform(head);
           head->frame++;
       }
       return paContinue;
}

variable_types:
inputBuffer
void *

variable_types:
outputBuffer
void *

variable_types:
framesPerBuffer
unsigned long

variable_types: struct PaStreamCallbackTimeInfo { double inputBufferAdcTime; double currentTime; double outputBufferDacTime; } *

variable_types:
statusFlags
unsigned long

variable_types:
data
void *

variable_types: struct beep_head { struct beep_note { float frequency; float amplitude; float duration; float (*waveform)(struct beep_head *); } *note; int frame; } *

variable_types:
buffer
float *

variable_types: frame int

user_defined_types: struct beep_head { struct beep_note *note; int frame; }


original_code:
static float beep_waveform_sine(beep_head *head)
{
   beep_note *note = head->note;
   return note->amplitude * sin(2 * 3.14159265358979323846 * note->frequency * head->frame / 44100);
}

canonical_original_code:
static float beep_waveform_sine(struct beep_head *head)
{
   struct beep_note *note = head->note;
   return note->amplitude * sin(2 * 3.14159265358979323846 * note->frequency * head->frame / 44100);
}

variable_types: struct beep_note { float frequency; float amplitude; float duration; float (*waveform)(struct beep_head { struct beep_note *note; int frame; } *); } *

return_type: float


decompiled_code:
// attributes: thunk
__int64 __fastcall portaudio_init(_QWORD a1, const char *a2)
{
return Pa_Initialize();
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD a1, const char *a2)
{
return Pa_Initialize();
}

original_code:
static void portaudio_init() {
    Pa_Initialize();
}


decompiled_code:
// attributes: thunk
__int64 portaudio_teardown(void)
{
return Pa_Terminate();
}

canonical_decompiled_code:
__int64 func10(void)
{
return Pa_Terminate();
}

original_code:
static void portaudio_teardown() {
    Pa_Terminate();
}


repo: novalis/cexpr

decompiled_code:
__int64 __fastcall dump_tree(char *s)
{
const char **v1; // rbx
size_t v3; // rax
const char *v4; // rbp

v1 = (const char **)parse(s, 0LL);
if ( *(_DWORD *)v1 )
{
printf("Error: %s\n", v1[1]);
}
else
{
v3 = strlen(s);
v4 = (const char *)malloc(3 * v3 + 1);
write_tree_to_string(v1[1], v4);
puts(v4);
}
return *(unsigned int *)v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s)
{
const char **v1; // rbx
size_t v3; // rax
const char *v4; // rbp

v1 = (const char **)parse(s, 0LL);
if ( *(_DWORD *)v1 )
{
printf("Error: %s\n", v1[1]);
}
else
{
v3 = strlen(s);
v4 = (const char *)malloc(3 * v3 + 1);
write_tree_to_string(v1[1], v4);
puts(v4);
}
return *(unsigned int *)v1;
}

original_code:
static int dump_tree(const char* expr) {
    struct parse_result* result = parse(expr, 0);
    if (result->is_error) {
        printf("Error: %s\n", result->error_message);
    } else {
        char* buf = malloc(strlen(expr) * 3 + 1);
        write_tree_to_string(result->node, buf);
        printf("%s\n", buf);
    }
    return result->is_error;
}

canonical_original_code:
static int dump_tree(char *expr) {
    struct parse_result *result = parse(expr, 0);
    if (result->is_error) {
        printf("Error: %s\n", result->error_message);
    } else {
        char *buf = malloc(strlen(expr) * 3 + 1);
        write_tree_to_string(result->node, buf);
        printf("%s\n", buf);
    }
    return result->is_error;
}

variable_types:
expr
char *

variable_types: struct parse_result { int is_error; union { struct parse_tree_node { enum token_type { LITERAL_OR_ID=1, OPEN_PAREN=2, CLOSE_PAREN=3, OPEN_BRACKET=4, CLOSE_BRACKET=5, OPEN_CURLY=6, CLOSE_CURLY=7, BANG=8, PERCENT=9, CARET=10, AMPERSAND=11, BAR=12, STAR=13, MINUS=14, PLUS=15, SLASH=16, LEFT_SHIFT=17, RIGHT_SHIFT=18, BANG_EQUAL=19, PERCENT_EQUAL=20, CARET_EQUAL=21, AMPERSAND_EQUAL=22, BAR_EQUAL=23, STAR_EQUAL=24, MINUS_EQUAL=25, PLUS_EQUAL=26, SLASH_EQUAL=27, LEFT_SHIFT_EQUAL=28, RIGHT_SHIFT_EQUAL=29, ARROW=30, DOT=31, ASSIGN=32, IS_EQUAL=33, LTE=34, GTE=35, LT=36, GT=37, DOUBLE_AMPERSAND=38, DOUBLE_BAR=39, DOUBLE_PLUS=40, DOUBLE_MINUS=41, QUESTION=42, COLON=43, SIZEOF=44, COMMA=45, TILDE=46, TYPECAST=47, FUNCTION_CALL=48, SUBSCRIPT=49, REFERENCE=50, DEREFERENCE=51, UNARY_MINUS=52, POSTINCREMENT=53, POSTDECREMENT=54, PREINCREMENT=55, PREDECREMENT=56, START_COMMENT=57, BOGUS=58, END_OF_EXPRESSION=59 } op; char *text; struct parse_tree_node *first_child; struct parse_tree_node *next_sibling; } *node; char *error_message; }; struct obstack { long chunk_size; struct _obstack_chunk { char *limit; struct _obstack_chunk *prev; char contents[4]; } *chunk; char *object_base; char *next_free; char *chunk_limit; union { long tempint; void *tempptr; } temp; int alignment_mask; struct _obstack_chunk *(*chunkfun)(void *, long); void (*freefun)(void *, struct _obstack_chunk *); void *extra_arg; unsigned int use_extra_arg; unsigned int maybe_empty_object; unsigned int alloc_failed; } *obstack; } *

variable_types:
buf
char *

user_defined_types: struct parse_result { int is_error; union { struct parse_tree_node *node; char *error_message; }; struct obstack *obstack; }

user_defined_types: struct obstack { long chunk_size; struct _obstack_chunk *chunk; char *object_base; char *next_free; char *chunk_limit; union { long tempint; void *tempptr; } temp; int alignment_mask; struct _obstack_chunk *(*chunkfun)(void *, long); void (*freefun)(void *, struct _obstack_chunk *); void *extra_arg; unsigned int use_extra_arg; unsigned int maybe_empty_object; unsigned int alloc_failed; }

user_defined_types: struct parse_tree_node { enum token_type op; char *text; struct parse_tree_node *first_child; struct parse_tree_node *next_sibling; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 2 )
return dump_tree((char *)argv[1]);
puts("Error: must supply a single argument");
return 2;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 2 )
return func0((char *)argv[1]);
puts("Error: must supply a single argument");
return 2;
}

original_code:
int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Error: must supply a single argument\n");
        return 2;
    }
    return dump_tree(argv[1]);
}

canonical_original_code:
int main(int argc, char **argv) {
    if (argc != 2) {
        printf("Error: must supply a single argument\n");
        return 2;
    }
    return dump_tree(argv[1]);
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall add_child_node(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v4; // rcx
int v5; // edx
int v6; // esi

result = a1;
v4 = *(_QWORD *)(result + 24);
if ( v4 )
{
result = *(_QWORD *)(v4 + 16);
*(_DWORD *)(a2 + 48) = 1;
if ( result )
{
v5 = 2;
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
v6 = v5++;
}
while ( result );
*(_DWORD *)(a2 + 48) = v6;
}
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)(v4 + 16) = a2;
}
else
{
*(_QWORD *)(result + 24) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v4; // rcx
int v5; // edx
int v6; // esi

result = a1;
v4 = *(_QWORD *)(result + 24);
if ( v4 )
{
result = *(_QWORD *)(v4 + 16);
*(_DWORD *)(a2 + 48) = 1;
if ( result )
{
v5 = 2;
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
v6 = v5++;
}
while ( result );
*(_DWORD *)(a2 + 48) = v6;
}
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)(v4 + 16) = a2;
}
else
{
*(_QWORD *)(result + 24) = a2;
}
return result;
}

original_code:
static void add_child_node(struct label *parent, struct label *child) {
    struct label* last_child = parent->first_child;
    if (!last_child) {
        parent->first_child = child;
        return;
    }
    child->number = 1;
    while (last_child->next_sibling) {
        last_child = last_child->next_sibling;
        child->number ++;
    }
    child->prev_sibling = last_child;
    last_child->next_sibling = child;
}

canonical_original_code:
static void add_child_node(struct label *parent, struct label *child) {
    struct label *last_child = parent->first_child;
    if (!last_child) {
        parent->first_child = child;
        return;
    }
    child->number = 1;
    while (last_child->next_sibling) {
        last_child = last_child->next_sibling;
        child->number ++;
    }
    child->prev_sibling = last_child;
    last_child->next_sibling = child;
}

variable_types: struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; } *

variable_types:
child
struct label *


next

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

result = a1[3];
if ( !result )
{
while ( 1 )
{
result = a1[2];
if ( result )
break;
a1 = (_QWORD *)*a1;
if ( !a1 )
return 0LL;
}
}
return result;
}

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

result = a1[3];
if ( !result )
{
while ( 1 )
{
result = a1[2];
if ( result )
break;
a1 = (_QWORD *)*a1;
if ( !a1 )
return 0LL;
}
}
return result;
}

original_code:
static struct label* next(struct label* node) {
    if (node->first_child) {
        return node->first_child;
    }
    if (node->next_sibling) {
        return node->next_sibling;
    }
    while (node->parent) {
        if (node->parent->next_sibling)
            return node->parent->next_sibling;
        node = node->parent;
    }
    return 0;
}

canonical_original_code:
static struct label *next(struct label *node) {
    if (node->first_child) {
        return node->first_child;
    }
    if (node->next_sibling) {
        return node->next_sibling;
    }
    while (node->parent) {
        if (node->parent->next_sibling)
            return node->parent->next_sibling;
        node = node->parent;
    }
    return 0;
}

variable_types:
node
struct label *

return_type: struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; } *


max

decompiled_code:
double __fastcall max(double a1, double a2)
{
return fmax(a1, a2);
}

canonical_decompiled_code:
double __fastcall func2(double a1, double a2)
{
return fmax(a1, a2);
}

original_code:
static double max(double a, double b) {
    return a > b ? a : b;
}

variable_types: a double

variable_types: b double

return_type: double


decompiled_code:
void __fastcall free_label_tree(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 3);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[2];
free(v3);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 3);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[2];
free(v3);
}
free(ptr);
}

original_code:
static void free_label_tree(struct label* label) {
    struct label* child = label->first_child;
    while (child) {
        struct label* next_child = child->next_sibling;
        free(child);
        child = next_child;
    }
    free(label);
}

canonical_original_code:
static void free_label_tree(struct label *label) {
    struct label *child = label->first_child;
    while (child) {
        struct label *next_child = child->next_sibling;
        free(child);
        child = next_child;
    }
    free(label);
}

user_defined_types: struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; }


original_code:
static struct label* make_label(struct parse_tree_node* node,
                         struct label *parent) {
    struct label* label = malloc(sizeof(struct label));
    label->parent = parent;
    label->first_child = 0;
    label->next_sibling = 0;
    label->prev_sibling = 0;
    label->thread = 0;
    label->ancestor = label;
    label->number = 0;
    label->modifier = label->change = label->shift = 0;
    switch (node->op) {
    case LITERAL_OR_ID:
        label->text = strdup(node->text);
        break;
    case TYPECAST:
        label->text = malloc(strlen(node->text) + 3);
        sprintf(label->text, "(%s)", node->text);
        break;
    case FUNCTION_CALL:
        label->text = strdup("function call");
        break;
    default:
        label->text = strdup(token_names[node->op]);
        break;
    }
    label->width = (strlen(label->text) + 2) * 8.4;
    char* old_text = label->text;
    label->text = xml_escape(old_text);
    free(old_text);
    label->xcoord = 500;
    label->ycoord = 0;
    label->modifier = 0;
    return label;
}

canonical_original_code:
static struct label *make_label(struct parse_tree_node *node, struct label *parent) {
    struct label *label = malloc(sizeof(struct label));
    label->parent = parent;
    label->first_child = 0;
    label->next_sibling = 0;
    label->prev_sibling = 0;
    label->thread = 0;
    label->ancestor = label;
    label->number = 0;
    label->modifier = label->change = label->shift = 0;
    switch (node->op) {
    case LITERAL_OR_ID:
        label->text = strdup(node->text);
        break;
    case TYPECAST:
        label->text = malloc(strlen(node->text) + 3);
        sprintf(label->text, "(%s)", node->text);
        break;
    case FUNCTION_CALL:
        label->text = strdup("function call");
        break;
    default:
        label->text = strdup(token_names[node->op]);
        break;
    }
    label->width = (strlen(label->text) + 2) * 8.4;
    char *old_text = label->text;
    label->text = xml_escape(old_text);
    free(old_text);
    label->xcoord = 500;
    label->ycoord = 0;
    label->modifier = 0;
    return label;
}

variable_types: struct parse_tree_node { enum token_type { LITERAL_OR_ID=1, OPEN_PAREN=2, CLOSE_PAREN=3, OPEN_BRACKET=4, CLOSE_BRACKET=5, OPEN_CURLY=6, CLOSE_CURLY=7, BANG=8, PERCENT=9, CARET=10, AMPERSAND=11, BAR=12, STAR=13, MINUS=14, PLUS=15, SLASH=16, LEFT_SHIFT=17, RIGHT_SHIFT=18, BANG_EQUAL=19, PERCENT_EQUAL=20, CARET_EQUAL=21, AMPERSAND_EQUAL=22, BAR_EQUAL=23, STAR_EQUAL=24, MINUS_EQUAL=25, PLUS_EQUAL=26, SLASH_EQUAL=27, LEFT_SHIFT_EQUAL=28, RIGHT_SHIFT_EQUAL=29, ARROW=30, DOT=31, ASSIGN=32, IS_EQUAL=33, LTE=34, GTE=35, LT=36, GT=37, DOUBLE_AMPERSAND=38, DOUBLE_BAR=39, DOUBLE_PLUS=40, DOUBLE_MINUS=41, QUESTION=42, COLON=43, SIZEOF=44, COMMA=45, TILDE=46, TYPECAST=47, FUNCTION_CALL=48, SUBSCRIPT=49, REFERENCE=50, DEREFERENCE=51, UNARY_MINUS=52, POSTINCREMENT=53, POSTDECREMENT=54, PREINCREMENT=55, PREDECREMENT=56, START_COMMENT=57, BOGUS=58, END_OF_EXPRESSION=59 } op; char *text; struct parse_tree_node *first_child; struct parse_tree_node *next_sibling; } *

variable_types:
parent
struct label *


decompiled_code:
__int64 __fastcall get_label_tree(__int64 a1, __int64 a2)
{
double *v3; // rax
__int64 v4; // rdi
__int64 v5; // r12
__int64 v6; // rax
__int64 i; // rbx
__int64 v8; // rax

v3 = make_label((unsigned int *)a1, a2);
v4 = *(_QWORD *)(a1 + 16);
v5 = (__int64)v3;
if ( v4 )
{
v6 = get_label_tree(v4, v3);
add_child_node(v5, v6);
for ( i = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 24LL); i; i = *(_QWORD *)(i + 24) )
{
v8 = get_label_tree(i, v5);
add_child_node(v5, v8);
}
}
return v5;
}

original_code:
static struct label* get_label_tree(struct parse_tree_node* node,
                             struct label *parent) {
    struct label* label = make_label(node, parent);
    if (node->first_child) {
        add_child_node(label, get_label_tree(node->first_child, label));
        struct parse_tree_node* cur = node->first_child->next_sibling;
        while (cur) {
            add_child_node(label, get_label_tree(cur, label));
            cur = cur->next_sibling;
        }
    }
    return label;
}

canonical_original_code:
static struct label *get_label_tree(struct parse_tree_node *node, struct label *parent) {
    struct label *label = make_label(node, parent);
    if (node->first_child) {
        add_child_node(label, get_label_tree(node->first_child, label));
        struct parse_tree_node *cur = node->first_child->next_sibling;
        while (cur) {
            add_child_node(label, get_label_tree(cur, label));
            cur = cur->next_sibling;
        }
    }
    return label;
}

user_defined_types: enum token_type { LITERAL_OR_ID=1, OPEN_PAREN=2, CLOSE_PAREN=3, OPEN_BRACKET=4, CLOSE_BRACKET=5, OPEN_CURLY=6, CLOSE_CURLY=7, BANG=8, PERCENT=9, CARET=10, AMPERSAND=11, BAR=12, STAR=13, MINUS=14, PLUS=15, SLASH=16, LEFT_SHIFT=17, RIGHT_SHIFT=18, BANG_EQUAL=19, PERCENT_EQUAL=20, CARET_EQUAL=21, AMPERSAND_EQUAL=22, BAR_EQUAL=23, STAR_EQUAL=24, MINUS_EQUAL=25, PLUS_EQUAL=26, SLASH_EQUAL=27, LEFT_SHIFT_EQUAL=28, RIGHT_SHIFT_EQUAL=29, ARROW=30, DOT=31, ASSIGN=32, IS_EQUAL=33, LTE=34, GTE=35, LT=36, GT=37, DOUBLE_AMPERSAND=38, DOUBLE_BAR=39, DOUBLE_PLUS=40, DOUBLE_MINUS=41, QUESTION=42, COLON=43, SIZEOF=44, COMMA=45, TILDE=46, TYPECAST=47, FUNCTION_CALL=48, SUBSCRIPT=49, REFERENCE=50, DEREFERENCE=51, UNARY_MINUS=52, POSTINCREMENT=53, POSTDECREMENT=54, PREINCREMENT=55, PREDECREMENT=56, START_COMMENT=57, BOGUS=58, END_OF_EXPRESSION=59 }


original_code:
static char* tree_to_svg(struct label* tree) {
    int allocated = 1024;
    char* svg = malloc(allocated);
    int used = strlen(svg_header);
    realloc_if_necessary(&svg, &allocated, used);
    strcpy(svg, svg_header);
    int rect_len = strlen(svg_rect);
    int text_len = strlen(svg_text);
    int line_len = strlen(svg_line);
    int buf_size = max(max(rect_len, text_len), line_len) + 5 * 15 + 1;
    char svg_buf[buf_size];
    struct label* label = tree;
    do {
        double width = label->width;
        double x = label->xcoord - width / 2;
        double y = label->ycoord;
        if (label->parent) {
            double parent_width = label->parent->width;
            double parent_x = label->parent->xcoord - parent_width / 2;
            double parent_y = label->parent->ycoord;
            int needed = sprintf(svg_buf, svg_line, x + width / 2,
                                 y, parent_x + parent_width / 2,
                                 parent_y + 17.0);
            realloc_if_necessary(&svg, &allocated, used + needed);
            strcpy(svg + used, svg_buf);
            used += needed;
        }
        int needed = sprintf(svg_buf, svg_rect, width, 17.0, x, y);
        realloc_if_necessary(&svg, &allocated, used + needed);
        strcpy(svg + used, svg_buf);
        used += needed;
        x += 8.4;
        y += 13.0;
        needed = sprintf(svg_buf, svg_text, 14.0, x, y, x, y, label->text);
        realloc_if_necessary(&svg, &allocated, used + needed);
        strcpy(svg + used, svg_buf);
        used += needed;
        label = next(label);
    } while (label);
    int needed = strlen(svg_footer);
    realloc_if_necessary(&svg, &allocated, used + needed);
    strcpy(svg + used, svg_footer);
    used += needed;
    return svg;
}

canonical_original_code:
static char *tree_to_svg(struct label *tree) {
    int allocated = 1024;
    char *svg = malloc(allocated);
    int used = strlen(svg_header);
    realloc_if_necessary(&svg, &allocated, used);
    strcpy(svg, svg_header);
    int rect_len = strlen(svg_rect);
    int text_len = strlen(svg_text);
    int line_len = strlen(svg_line);
    int buf_size = max(max(rect_len, text_len), line_len) + 5 * 15 + 1;
    char svg_buf[-1];
    struct label *label = tree;
    do {
        double width = label->width;
        double x = label->xcoord - width / 2;
        double y = label->ycoord;
        if (label->parent) {
            double parent_width = label->parent->width;
            double parent_x = label->parent->xcoord - parent_width / 2;
            double parent_y = label->parent->ycoord;
            int needed = sprintf(svg_buf, svg_line, x + width / 2,
                                 y, parent_x + parent_width / 2,
                                 parent_y + 17.0);
            realloc_if_necessary(&svg, &allocated, used + needed);
            strcpy(svg + used, svg_buf);
            used += needed;
        }
        int needed = sprintf(svg_buf, svg_rect, width, 17.0, x, y);
        realloc_if_necessary(&svg, &allocated, used + needed);
        strcpy(svg + used, svg_buf);
        used += needed;
        x += 8.4;
        y += 13.0;
        needed = sprintf(svg_buf, svg_text, 14.0, x, y, x, y, label->text);
        realloc_if_necessary(&svg, &allocated, used + needed);
        strcpy(svg + used, svg_buf);
        used += needed;
        label = next(label);
    } while (label);
    int needed = strlen(svg_footer);
    realloc_if_necessary(&svg, &allocated, used + needed);
    strcpy(svg + used, svg_footer);
    used += needed;
    return svg;
}

variable_types: allocated int

variable_types:
svg
char *

variable_types: used int

variable_types: width double

variable_types: x double

variable_types: y double

variable_types: needed int

return_type: char *


decompiled_code:
void *__fastcall parse_tree_to_svg(__int64 a1)
{
void *v1; // rbp
void *v2; // r12
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

v1 = (void *)get_label_tree(a1, 0LL);
v4[0] = LC18;
v4[1] = LC19;
v4[2] = LC20;
walker_layout(v1, v4);
v2 = tree_to_svg((__int64)v1);
free_label_tree(v1);
return v2;
}

original_code:
char* parse_tree_to_svg(struct parse_tree_node* node) {
    struct label* label = get_label_tree(node, 0);
    struct walker_layout_rules rules;
    rules.sibling_separation = 10;
    rules.subtree_separation = 20;
    rules.level_separation = 30;
    walker_layout(label, &rules);
    char* svg = tree_to_svg(label);
    free_label_tree(label);
    return svg;
}

canonical_original_code:
char *parse_tree_to_svg(struct parse_tree_node *node) {
    struct label *label = get_label_tree(node, 0);
    struct walker_layout_rules rules;
    rules.sibling_separation = 10;
    rules.subtree_separation = 20;
    rules.level_separation = 30;
    walker_layout(label, &rules);
    char *svg = tree_to_svg(label);
    free_label_tree(label);
    return svg;
}

variable_types: struct walker_layout_rules { double sibling_separation; double subtree_separation; double level_separation; }


decompiled_code:
char *__fastcall obstack_strdup(struct obstack *a1, char *a2)
{
char *v3; // rax
char v4; // r12
char *v5; // rax
char *v6; // r8
char *v7; // rdx
unsigned __int64 v8; // rax
char *v9; // rdx
char *v10; // rax

do
{
while ( 1 )
{
v3 = a1->next_free;
v4 = *a2++;
if ( v3 + 1 > a1->chunk_limit )
break;
a1->next_free = v3 + 1;
*v3 = v4;
if ( !v4 )
goto LABEL_5;
}
obstack_newchunk(a1, 1);
v5 = a1->next_free;
a1->next_free = v5 + 1;
*v5 = v4;
}
while ( v4 );
LABEL_5:
v6 = a1->object_base;
v7 = a1->next_free;
if ( v7 == v6 )
*((_BYTE *)a1 + 80) |= 2u;
v8 = (unsigned __int64)&v7[a1->alignment_mask];
v9 = a1->chunk_limit;
v10 = (char *)(~a1->alignment_mask & v8);
a1->next_free = v10;
if ( v10 > v9 )
{
a1->next_free = v9;
v10 = v9;
}
a1->object_base = v10;
return v6;
}

canonical_decompiled_code:
char *__fastcall func0(struct obstack *a1, char *a2)
{
char *v3; // rax
char v4; // r12
char *v5; // rax
char *v6; // r8
char *v7; // rdx
unsigned __int64 v8; // rax
char *v9; // rdx
char *v10; // rax

do
{
while ( 1 )
{
v3 = a1->next_free;
v4 = *a2++;
if ( v3 + 1 > a1->chunk_limit )
break;
a1->next_free = v3 + 1;
*v3 = v4;
if ( !v4 )
goto LABEL_5;
}
obstack_newchunk(a1, 1);
v5 = a1->next_free;
a1->next_free = v5 + 1;
*v5 = v4;
}
while ( v4 );
LABEL_5:
v6 = a1->object_base;
v7 = a1->next_free;
if ( v7 == v6 )
*((_BYTE *)a1 + 80) |= 2u;
v8 = (unsigned __int64)&v7[a1->alignment_mask];
v9 = a1->chunk_limit;
v10 = (char *)(~a1->alignment_mask & v8);
a1->next_free = v10;
if ( v10 > v9 )
{
a1->next_free = v9;
v10 = v9;
}
a1->object_base = v10;
return v6;
}

original_code:
char* obstack_strdup(struct obstack* obstack, const char* str) {
    while(1) {
        char c = *str++;
        __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (c)); (void) 0; });
        if (!c) {
            break;
        }
    }
    return __extension__ ({ struct obstack *__o1 = (obstack); void *__value = (void *) __o1->object_base; if (__o1->next_free == __value) __o1->maybe_empty_object = 1; __o1->next_free = ((sizeof (long int) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (((__o1->next_free) - (sizeof (long int) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (__o1->alignment_mask)) & ~(__o1->alignment_mask))); if (__o1->next_free - (char *) __o1->chunk > __o1->chunk_limit - (char *) __o1->chunk) __o1->next_free = __o1->chunk_limit; __o1->object_base = __o1->next_free; __value; });
}

canonical_original_code:
char *obstack_strdup(struct obstack *obstack, char *str) {
    while(1) {
        char c = *str++;
        __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (c)); (void) 0; });
        if (!c) {
            break;
        }
    }
    return __extension__ ({ struct obstack *__o1 = (obstack); void *__value = (void *) __o1->object_base; if (__o1->next_free == __value) __o1->maybe_empty_object = 1; __o1->next_free = ((sizeof (long) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (((__o1->next_free) - (sizeof (long) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (__o1->alignment_mask)) & ~(__o1->alignment_mask))); if (__o1->next_free - (char *) __o1->chunk > __o1->chunk_limit - (char *) __o1->chunk) __o1->next_free = __o1->chunk_limit; __o1->object_base = __o1->next_free; __value; });
}

variable_types: struct obstack { long chunk_size; struct _obstack_chunk { char *limit; struct _obstack_chunk *prev; char contents[4]; } *chunk; char *object_base; char *next_free; char *chunk_limit; union { long tempint; void *tempptr; } temp; int alignment_mask; struct _obstack_chunk *(*chunkfun)(void *, long); void (*freefun)(void *, struct _obstack_chunk *); void *extra_arg; unsigned int use_extra_arg; unsigned int maybe_empty_object; unsigned int alloc_failed; } *

variable_types:
str
char *

variable_types: c char


decompiled_code:
char *__fastcall obstack_strndup(struct obstack *a1, char *a2, __int64 a3)
{
char v3; // r12
_BYTE *v4; // rbp
__int64 v5; // r13
char *v6; // rax
char *v7; // rdx
char *v8; // rax
char *v9; // rdx
char *v10; // r8
char *v11; // rdx
unsigned __int64 v12; // rax
char *v13; // rdx
char *v14; // rax

v3 = *a2;
if ( *a2 && a3 )
{
v4 = a2 + 1;
v5 = (__int64)&a2[a3 + 1];
do
{
v6 = a1->next_free;
v7 = v6 + 1;
if ( v6 + 1 > a1->chunk_limit )
{
obstack_newchunk(a1, 1);
v6 = a1->next_free;
v7 = v6 + 1;
}
++v4;
a1->next_free = v7;
*v6 = v3;
v3 = *(v4 - 1);
}
while ( v3 && v4 != (_BYTE *)v5 );
}
v8 = a1->next_free;
v9 = v8 + 1;
if ( v8 + 1 > a1->chunk_limit )
{
obstack_newchunk(a1, 1);
v8 = a1->next_free;
v9 = v8 + 1;
}
a1->next_free = v9;
*v8 = 0;
v10 = a1->object_base;
v11 = a1->next_free;
if ( v11 == v10 )
*((_BYTE *)a1 + 80) |= 2u;
v12 = (unsigned __int64)&v11[a1->alignment_mask];
v13 = a1->chunk_limit;
v14 = (char *)(~a1->alignment_mask & v12);
a1->next_free = v14;
if ( v14 > v13 )
{
a1->next_free = v13;
v14 = v13;
}
a1->object_base = v14;
return v10;
}

canonical_decompiled_code:
char *__fastcall func1(struct obstack *a1, char *a2, __int64 a3)
{
char v3; // r12
_BYTE *v4; // rbp
__int64 v5; // r13
char *v6; // rax
char *v7; // rdx
char *v8; // rax
char *v9; // rdx
char *v10; // r8
char *v11; // rdx
unsigned __int64 v12; // rax
char *v13; // rdx
char *v14; // rax

v3 = *a2;
if ( *a2 && a3 )
{
v4 = a2 + 1;
v5 = (__int64)&a2[a3 + 1];
do
{
v6 = a1->next_free;
v7 = v6 + 1;
if ( v6 + 1 > a1->chunk_limit )
{
obstack_newchunk(a1, 1);
v6 = a1->next_free;
v7 = v6 + 1;
}
++v4;
a1->next_free = v7;
*v6 = v3;
v3 = *(v4 - 1);
}
while ( v3 && v4 != (_BYTE *)v5 );
}
v8 = a1->next_free;
v9 = v8 + 1;
if ( v8 + 1 > a1->chunk_limit )
{
obstack_newchunk(a1, 1);
v8 = a1->next_free;
v9 = v8 + 1;
}
a1->next_free = v9;
*v8 = 0;
v10 = a1->object_base;
v11 = a1->next_free;
if ( v11 == v10 )
*((_BYTE *)a1 + 80) |= 2u;
v12 = (unsigned __int64)&v11[a1->alignment_mask];
v13 = a1->chunk_limit;
v14 = (char *)(~a1->alignment_mask & v12);
a1->next_free = v14;
if ( v14 > v13 )
{
a1->next_free = v13;
v14 = v13;
}
a1->object_base = v14;
return v10;
}

original_code:
char* obstack_strndup(struct obstack* obstack, const char* str,
                             size_t n) {
    char c;
    size_t i = 0;
    while((c = *str++) && i++ < n) {
        __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (c)); (void) 0; });
    }
    __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (0)); (void) 0; });
    return __extension__ ({ struct obstack *__o1 = (obstack); void *__value = (void *) __o1->object_base; if (__o1->next_free == __value) __o1->maybe_empty_object = 1; __o1->next_free = ((sizeof (long int) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (((__o1->next_free) - (sizeof (long int) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (__o1->alignment_mask)) & ~(__o1->alignment_mask))); if (__o1->next_free - (char *) __o1->chunk > __o1->chunk_limit - (char *) __o1->chunk) __o1->next_free = __o1->chunk_limit; __o1->object_base = __o1->next_free; __value; });
}

canonical_original_code:
char *obstack_strndup(struct obstack *obstack, char *str, unsigned long n) {
    char c;
    unsigned long i = 0;
    while((c = *str++) && i++ < n) {
        __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (c)); (void) 0; });
    }
    __extension__ ({ struct obstack *__o = (obstack); if (__o->next_free + 1 > __o->chunk_limit) _obstack_newchunk (__o, 1); (*((__o)->next_free)++ = (0)); (void) 0; });
    return __extension__ ({ struct obstack *__o1 = (obstack); void *__value = (void *) __o1->object_base; if (__o1->next_free == __value) __o1->maybe_empty_object = 1; __o1->next_free = ((sizeof (long) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (((__o1->next_free) - (sizeof (long) < sizeof (void *) ? (__o1->object_base) : (char *) 0) + (__o1->alignment_mask)) & ~(__o1->alignment_mask))); if (__o1->next_free - (char *) __o1->chunk > __o1->chunk_limit - (char *) __o1->chunk) __o1->next_free = __o1->chunk_limit; __o1->object_base = __o1->next_free; __value; });
}

variable_types:
n
unsigned long

variable_types:
i
unsigned long

user_defined_types: struct _obstack_chunk { char *limit; struct _obstack_chunk *prev; char contents[4]; }


unescape

original_code:
static char* unescape(const char* str) {
    char* dup = strdup(str);
    const char* read = dup;
    char* write = dup;
    char c;
    while ((c = *read++)) {
        if (c == '%') {
            unsigned int x;
            int n = sscanf(read, "%2x", &x);
            if (n == 1) {
                read += 2;
            } else {
                return dup;
            }
            *write++ = x;
        } else if (c == '+') {
            *write++ = ' ';
        } else {
            *write++ = c;
        }
    }
    *write = 0;
    return dup;
}

canonical_original_code:
static char *unescape(char *str) {
    char *dup = strdup(str);
    char *read = dup;
    char *write = dup;
    char c;
    while ((c = *read++)) {
        if (c == '%') {
            unsigned int x;
            int n = sscanf(read, "%2x", &x);
            if (n == 1) {
                read += 2;
            } else {
                return dup;
            }
            *write++ = x;
        } else if (c == '+') {
            *write++ = ' ';
        } else {
            *write++ = c;
        }
    }
    *write = 0;
    return dup;
}

variable_types:
dup
char *

variable_types:
read
char *

variable_types:
write
char *

variable_types:
x
unsigned int

variable_types: n int


original_code:
static void add_value(struct cgi_var* var, const char* value) {
    if (var->n_values >= var->values_allocated) {
        var->values_allocated *= 2;
        var->values = realloc(var->values, var->values_allocated * sizeof(char*));
    }
    var->values[var->n_values++] = unescape(value);
}

canonical_original_code:
static void add_value(struct cgi_var *var, char *value) {
    if (var->n_values >= var->values_allocated) {
        var->values_allocated *= 2;
        var->values = realloc(var->values, var->values_allocated * sizeof(char *));
    }
    var->values[var->n_values++] = unescape(value);
}

variable_types: struct cgi_var { char *var; char **values; int n_values; int values_allocated; } *

variable_types:
value
char *


decompiled_code:
const char **__fastcall cgi_get_var(__int64 a1, const char *a2)
{
int v2; // eax
const char **v3; // r12
__int64 v4; // rbp

v2 = *(_DWORD *)(a1 + 8);
if ( v2 <= 0 )
return 0LL;
v3 = *(const char ***)a1;
v4 = *(_QWORD *)a1 + 24LL * (unsigned int)(v2 - 1) + 24;
while ( strcmp(*v3, a2) )
{
v3 += 3;
if ( v3 == (const char **)v4 )
return 0LL;
}
return v3;
}

canonical_decompiled_code:
const char **__fastcall func3(__int64 a1, const char *a2)
{
int v2; // eax
const char **v3; // r12
__int64 v4; // rbp

v2 = *(_DWORD *)(a1 + 8);
if ( v2 <= 0 )
return 0LL;
v3 = *(const char ***)a1;
v4 = *(_QWORD *)a1 + 24LL * (unsigned int)(v2 - 1) + 24;
while ( strcmp(*v3, a2) )
{
v3 += 3;
if ( v3 == (const char **)v4 )
return 0LL;
}
return v3;
}

original_code:
struct cgi_var* cgi_get_var(struct cgi* cgi, const char* var_name) {
    for (int i = 0; i < cgi->n_vars; ++i) {
        struct cgi_var* var = cgi->vars + i;
        if (!strcmp(var->var, var_name)) {
            return var;
        }
    }
    return 0;
}

canonical_original_code:
struct cgi_var *cgi_get_var(struct cgi *cgi, char *var_name) {
    for (int i = 0; i < cgi->n_vars; ++i) {
        struct cgi_var *var = cgi->vars + i;
        if (!strcmp(var->var, var_name)) {
            return var;
        }
    }
    return 0;
}

variable_types: struct cgi { struct cgi_var *vars; int n_vars; int vars_allocated; } *

variable_types: i int

return_type: struct cgi_var { char *var; char **values; int n_values; int values_allocated; } *


decompiled_code:
char **cgi_init()
{
char *v0; // r8
char **v1; // rdx
char *v2; // rax

v0 = *environ;
if ( !*environ )
return parse_query_string((char *)&s);
v1 = environ + 1;
while ( memcmp(v0, "QUERY_STRING", 0xCuLL) )
{
v0 = *v1++;
if ( !v0 )
return parse_query_string((char *)&s);
}
v2 = strchr(v0, 61);
return parse_query_string(v2 + 1);
}

canonical_decompiled_code:
char **func5()
{
char *v0; // r8
char **v1; // rdx
char *v2; // rax

v0 = *environ;
if ( !*environ )
return func4((char *)&s);
v1 = environ + 1;
while ( memcmp(v0, "QUERY_STRING", 0xCuLL) )
{
v0 = *v1++;
if ( !v0 )
return func4((char *)&s);
}
v2 = strchr(v0, 61);
return func4(v2 + 1);
}

original_code:
struct cgi* cgi_init() {
    const char* query_string = "QUERY_STRING";
    int query_string_len = strlen(query_string);
    int i = 0;
    while(environ[i]) {
        if (strncmp(environ[i], query_string, query_string_len) == 0) {
            struct cgi* cgi = parse_query_string(strchr(environ[i], '=') + 1);
            return cgi;
        }
        ++i;
    }
    return parse_query_string("");
}

canonical_original_code:
struct cgi *cgi_init() {
    char *query_string = "QUERY_STRING";
    int query_string_len = strlen(query_string);
    int i = 0;
    while(environ[i]) {
        if (strncmp(environ[i], query_string, query_string_len) == 0) {
            struct cgi *cgi = parse_query_string(strchr(environ[i], '=') + 1);
            return cgi;
        }
        ++i;
    }
    return parse_query_string("");
}

variable_types: struct cgi { struct cgi_var { char *var; char **values; int n_values; int values_allocated; } *vars; int n_vars; int vars_allocated; } *

user_defined_types: struct cgi { struct cgi_var *vars; int n_vars; int vars_allocated; }


decompiled_code:
double __fastcall execute_shifts(__int64 a1)
{
__int64 v1; // rax
double v2; // xmm1_8
double result; // xmm0_8
double v4; // xmm2_8

do
{
v1 = a1;
a1 = *(_QWORD *)(a1 + 16);
}
while ( a1 );
v2 = 0.0;
result = 0.0;
do
{
v2 = v2 + *(double *)(v1 + 56);
*(double *)(v1 + 88) = *(double *)(v1 + 88) + result;
*(double *)(v1 + 104) = *(double *)(v1 + 104) + result;
v4 = *(double *)(v1 + 64);
v1 = *(_QWORD *)(v1 + 8);
result = result + v4 + v2;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1)
{
__int64 v1; // rax
double v2; // xmm1_8
double result; // xmm0_8
double v4; // xmm2_8

do
{
v1 = a1;
a1 = *(_QWORD *)(a1 + 16);
}
while ( a1 );
v2 = 0.0;
result = 0.0;
do
{
v2 = v2 + *(double *)(v1 + 56);
*(double *)(v1 + 88) = *(double *)(v1 + 88) + result;
*(double *)(v1 + 104) = *(double *)(v1 + 104) + result;
v4 = *(double *)(v1 + 64);
v1 = *(_QWORD *)(v1 + 8);
result = result + v4 + v2;
}
while ( v1 );
return result;
}

original_code:
static void execute_shifts(struct label* node) {
    double shift = 0;
    double change = 0;
    struct label* child = node;
    while (child->next_sibling) {
        child = child->next_sibling;
    }
    while (child) {
        child->xcoord += shift;
        child->modifier += shift;
        change += child->change;
        shift += child->shift + change;
        child = child->prev_sibling;
    }
}

canonical_original_code:
static void execute_shifts(struct label *node) {
    double shift = 0;
    double change = 0;
    struct label *child = node;
    while (child->next_sibling) {
        child = child->next_sibling;
    }
    while (child) {
        child->xcoord += shift;
        child->modifier += shift;
        change += child->change;
        shift += child->shift + change;
        child = child->prev_sibling;
    }
}

variable_types: struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; } *

variable_types: shift double

variable_types: change double


original_code:
static void move_subtree(struct label* left, struct label* right, double shift) {
    int subtrees = right->number - left->number;
    right->change -= shift / subtrees;
    right->shift += shift;
    left->change += shift / subtrees;
    right->xcoord += shift;
    right->modifier += shift;
}

canonical_original_code:
static void move_subtree(struct label *left, struct label *right, double shift) {
    int subtrees = right->number - left->number;
    right->change -= shift / subtrees;
    right->shift += shift;
    left->change += shift / subtrees;
    right->xcoord += shift;
    right->modifier += shift;
}

variable_types:
right
struct label *

variable_types: subtrees int


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

result = *(_QWORD *)(a1 + 24);
if ( !result )
result = *(_QWORD *)(a1 + 32);
return result;
}

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

result = *(_QWORD *)(a1 + 24);
if ( !result )
result = *(_QWORD *)(a1 + 32);
return result;
}

original_code:
static struct label* next_left(struct label *node) {
    if (node->first_child) {
        return node->first_child;
    }
    return node->thread;
}

canonical_original_code:
static struct label *next_left(struct label *node) {
    if (node->first_child) {
        return node->first_child;
    }
    return node->thread;
}

variable_types:
node
struct label *


original_code:
static struct label* next_right(struct label *node) {
    if (node->first_child) {
        struct label* child = node->first_child;
        while(child->next_sibling) {
            child = child->next_sibling;
        }
        return child;
    }
    return node->thread;
}

canonical_original_code:
static struct label *next_right(struct label *node) {
    if (node->first_child) {
        struct label *child = node->first_child;
        while(child->next_sibling) {
            child = child->next_sibling;
        }
        return child;
    }
    return node->thread;
}


decompiled_code:
__int64 __fastcall second_walk(__int64 a1, __int64 a2, int a3, double a4)
{
unsigned int v4; // er12
double v5; // xmm2_8
__int64 result; // rax
__int64 v8; // rsi

v4 = a3 + 1;
v5 = (double)a3;
do
{
result = *(_QWORD *)(a1 + 24);
v8 = *(_QWORD *)(a2 + 24);
*(double *)(a2 + 88) = *(double *)a1 + *(double *)(a2 + 88) + a4;
*(double *)(a2 + 96) = *(double *)(result + 16) * v5 + *(double *)(a1 + 8);
if ( v8 )
result = second_walk(a1, v8, v4, a4 + *(double *)(a2 + 104));
a2 = *(_QWORD *)(a2 + 16);
}
while ( a2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, int a3, double a4)
{
unsigned int v4; // er12
double v5; // xmm2_8
__int64 result; // rax
__int64 v8; // rsi

v4 = a3 + 1;
v5 = (double)a3;
do
{
result = *(_QWORD *)(a1 + 24);
v8 = *(_QWORD *)(a2 + 24);
*(double *)(a2 + 88) = *(double *)a1 + *(double *)(a2 + 88) + a4;
*(double *)(a2 + 96) = *(double *)(result + 16) * v5 + *(double *)(a1 + 8);
if ( v8 )
result = func4(a1, v8, v4, a4 + *(double *)(a2 + 104));
a2 = *(_QWORD *)(a2 + 16);
}
while ( a2 );
return result;
}

original_code:
static void second_walk(struct layout_ctx* ctx, struct label *node, int level,
                        double modsum) {
    node->xcoord = ctx->x_top_adjustment + node->xcoord + modsum;
    node->ycoord = ctx->y_top_adjustment + level * ctx->rules->level_separation;
    if (node->first_child) {
        second_walk(ctx, node->first_child, level + 1,
                    modsum + node->modifier);
    }
    if (node->next_sibling) {
        second_walk(ctx, node->next_sibling, level, modsum);
    }
}

canonical_original_code:
static void second_walk(struct layout_ctx *ctx, struct label *node, int level, double modsum) {
    node->xcoord = ctx->x_top_adjustment + node->xcoord + modsum;
    node->ycoord = ctx->y_top_adjustment + level * ctx->rules->level_separation;
    if (node->first_child) {
        second_walk(ctx, node->first_child, level + 1,
                    modsum + node->modifier);
    }
    if (node->next_sibling) {
        second_walk(ctx, node->next_sibling, level, modsum);
    }
}

variable_types: struct layout_ctx { double x_top_adjustment; double y_top_adjustment; struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; } **prev_node_at_level; struct walker_layout_rules { double sibling_separation; double subtree_separation; double level_separation; } *rules; } *

variable_types: level int

variable_types: modsum double

user_defined_types: struct layout_ctx { double x_top_adjustment; double y_top_adjustment; struct label **prev_node_at_level; struct walker_layout_rules *rules; }


original_code:
static void first_walk(struct layout_ctx* ctx, struct label *node) {
    if (node->first_child) {
        struct label *default_ancestor = node->first_child;
        struct label *cur = node->first_child;
        struct label *last_child;
        while (cur) {
            first_walk(ctx, cur);
            default_ancestor = apportion(ctx, cur, default_ancestor);
            last_child = cur;
            cur = cur->next_sibling;
        }
        execute_shifts(node);
        double midpoint = (node->first_child->xcoord + last_child->xcoord) / 2;
        if (node->prev_sibling) {
            node->xcoord = node->prev_sibling->xcoord
                + spacing(ctx, node->prev_sibling, node, 1)
                + ctx->rules->sibling_separation;
            node->modifier = node->xcoord - midpoint;
        } else {
            node->xcoord = midpoint;
        }
    } else {
        if (node->prev_sibling) {
            node->xcoord = node->prev_sibling->xcoord
                + spacing(ctx, node, node->prev_sibling, 1);
        } else {
            node->xcoord = 0;
        }
    }
}

canonical_original_code:
static void first_walk(struct layout_ctx *ctx, struct label *node) {
    if (node->first_child) {
        struct label *default_ancestor = node->first_child;
        struct label *cur = node->first_child;
        struct label *last_child;
        while (cur) {
            first_walk(ctx, cur);
            default_ancestor = apportion(ctx, cur, default_ancestor);
            last_child = cur;
            cur = cur->next_sibling;
        }
        execute_shifts(node);
        double midpoint = (node->first_child->xcoord + last_child->xcoord) / 2;
        if (node->prev_sibling) {
            node->xcoord = node->prev_sibling->xcoord
                + spacing(ctx, node->prev_sibling, node, 1)
                + ctx->rules->sibling_separation;
            node->modifier = node->xcoord - midpoint;
        } else {
            node->xcoord = midpoint;
        }
    } else {
        if (node->prev_sibling) {
            node->xcoord = node->prev_sibling->xcoord
                + spacing(ctx, node, node->prev_sibling, 1);
        } else {
            node->xcoord = 0;
        }
    }
}

variable_types: midpoint double

user_defined_types: struct walker_layout_rules { double sibling_separation; double subtree_separation; double level_separation; }


original_code:
void walker_layout(struct label *node, struct walker_layout_rules* rules) {
    if (!node) {
        return;
    }
    struct layout_ctx ctx;
    ctx.prev_node_at_level = calloc(get_max_depth(node), sizeof(node));
    ctx.rules = rules;
    ctx.x_top_adjustment = node->xcoord;
    ctx.y_top_adjustment = node->ycoord;
    first_walk(&ctx, node);
    second_walk(&ctx, node, 0, -node->xcoord);
}

canonical_original_code:
void walker_layout(struct label *node, struct walker_layout_rules *rules) {
    if (!node) {
        return;
    }
    struct layout_ctx ctx;
    ctx.prev_node_at_level = calloc(get_max_depth(node), sizeof(node));
    ctx.rules = rules;
    ctx.x_top_adjustment = node->xcoord;
    ctx.y_top_adjustment = node->ycoord;
    first_walk(&ctx, node);
    second_walk(&ctx, node, 0, -node->xcoord);
}

variable_types: struct layout_ctx { double x_top_adjustment; double y_top_adjustment; struct label { struct label *parent; struct label *prev_sibling; struct label *next_sibling; struct label *first_child; struct label *thread; struct label *ancestor; int number; double change; double shift; char *text; double width; double xcoord; double ycoord; double modifier; } **prev_node_at_level; struct walker_layout_rules { double sibling_separation; double subtree_separation; double level_separation; } *rules; }


decompiled_code:
__int64 __fastcall read_integer_literal(unsigned __int8 **a1)
{
unsigned __int8 *v1; // rdx
__int64 result; // rax

v1 = *a1;
for ( result = **a1; (_BYTE)result; result = *++v1 )
{
result = (unsigned int)(result - 48);
if ( (unsigned __int8)result > 9u )
break;
}
*a1 = v1;
return result;
}

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

v1 = *a1;
for ( result = **a1; (_BYTE)result; result = *++v1 )
{
result = (unsigned int)(result - 48);
if ( (unsigned __int8)result > 9u )
break;
}
*a1 = v1;
return result;
}

original_code:
static void read_integer_literal(const char** pos_ref) {
    const char* pos = *pos_ref;
    char c;
    while ((c = *pos)) {
        if (c < '0' || c > '9') {
            break;
        }
        pos++;
    }
    *pos_ref = pos;
}

canonical_original_code:
static void read_integer_literal(char **pos_ref) {
    char *pos = *pos_ref;
    char c;
    while ((c = *pos)) {
        if (c < '0' || c > '9') {
            break;
        }
        pos++;
    }
    *pos_ref = pos;
}

variable_types:
pos
char *

variable_types: c char


original_code:
static _Bool read_float(const char** pos_ref) {
    char c;
    _Bool dot_seen = 0;
    _Bool exp_seen = 0;
    const char* pos = *pos_ref;
    while ((c = *pos)) {
        if (c == '.') {
            if (dot_seen || exp_seen) {
                *pos_ref = pos;
                return 0;
            }
            dot_seen = 1;
        } else if (c == 'E' || c == 'e') {
            if (exp_seen) {
                *pos_ref = pos;
                return 0;
            }
            if (pos[1] == '-' || pos[1] == '+') {
                pos++;
            }
            exp_seen = 1;
        } else if (c < '0' || c > '9') {
            break;
        }
        pos++;
    }
    *pos_ref = pos;
    return 1;
}

canonical_original_code:
static _Bool read_float(char **pos_ref) {
    char c;
    _Bool dot_seen = 0;
    _Bool exp_seen = 0;
    char *pos = *pos_ref;
    while ((c = *pos)) {
        if (c == '.') {
            if (dot_seen || exp_seen) {
                *pos_ref = pos;
                return 0;
            }
            dot_seen = 1;
        } else if (c == 'E' || c == 'e') {
            if (exp_seen) {
                *pos_ref = pos;
                return 0;
            }
            if (pos[1] == '-' || pos[1] == '+') {
                pos++;
            }
            exp_seen = 1;
        } else if (c < '0' || c > '9') {
            break;
        }
        pos++;
    }
    *pos_ref = pos;
    return 1;
}