main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er13
int v5; // er14
__int64 v6; // r15
int v7; // ebx
__int64 v8; // rbx
const char *v9; // rax
unsigned int *v10; // rbx
__int64 v11; // rsi
__int64 v12; // rsi
__int64 v13; // rsi
__int64 v14; // rsi
unsigned int v15; // eax
unsigned int v16; // ebx
unsigned int *v17; // rbx
__int64 v18; // rsi
signed int v19; // eax
signed int v20; // edx
int v21; // eax
unsigned int v22; // eax
int v23; // eax
unsigned int v24; // eax
int v25; // eax
unsigned int v26; // er12
int v27; // er12
char *v28; // rax
char *v29; // rax
int v30; // er12
char *v31; // rax
char *v32; // rax
int v33; // er12
char *v34; // rax
char *v35; // rax
int v36; // eax
const char *v37; // rax
int v38; // eax
unsigned int v39; // eax
int v40; // edx
signed int v41; // er12
int v42; // edi
int v43; // eax
signed int v44; // eax
signed int v45; // ecx
__int64 v46; // rdx
int v47; // edi
int v48; // eax
char *s; // [rsp+0h] [rbp-21E8h]
size_t n; // [rsp+8h] [rbp-21E0h]
int v52; // [rsp+14h] [rbp-21D4h] BYREF
char *src; // [rsp+18h] [rbp-21D0h] BYREF
char *v54; // [rsp+20h] [rbp-21C8h] BYREF
char *v55; // [rsp+28h] [rbp-21C0h] BYREF
struct timeval timeout; // [rsp+30h] [rbp-21B8h] BYREF
fd_set writefds; // [rsp+40h] [rbp-21A8h] BYREF
fd_set readfds; // [rsp+C0h] [rbp-2128h] BYREF
unsigned int *v59; // [rsp+148h] [rbp-20A0h] BYREF
char buf[8204]; // [rsp+150h] [rbp-2098h] BYREF
socklen_t addr_len; // [rsp+215Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+2160h] [rbp-88h] BYREF
struct addrinfo *pai; // [rsp+2178h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+2180h] [rbp-68h] BYREF

addr_len = 16;
v59 = 0LL;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_socktype = 1;
req.ai_protocol = 6;
if ( argc != 2 )
{
printf("Usage: %s PORT\n", *argv);
exit(1);
}
v3 = getaddrinfo(0LL, argv[1], &req, &pai);
if ( v3 )
{
v9 = gai_strerror(v3);
printf("getaddrinfo: %s\n", v9);
exit(1);
}
v4 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
if ( v4 == -1 )
{
perror("socket");
freeaddrinfo(pai);
exit(1);
}
if ( bind(v4, pai->ai_addr, pai->ai_addrlen) == -1 )
{
perror("bind");
freeaddrinfo(pai);
close(v4);
exit(1);
}
if ( listen(v4, 128) == -1 )
{
perror("listen");
freeaddrinfo(pai);
close(v4);
exit(1);
}
timeout.tv_sec = 180LL;
timeout.tv_usec = 0LL;
v5 = v4 / 64;
v6 = 1LL << (v4 % 64);
LABEL_7:
while ( 1 )
{
v7 = setfds(&readfds, &writefds, v59, (unsigned int)v4);
puts("wait for select");
v8 = (unsigned int)select(v7, &readfds, &writefds, 0LL, &timeout);
printf("got selected %d fds\n", v8);
if ( (int)v8 < 0 )
break;
if ( (_DWORD)v8 )
{
printf("Got %d cache pages\n", 0LL);
v10 = v59;
if ( v59 )
{
do
{
v11 = *v10;
if ( (readfds.fds_bits[(int)v11 / 64] & (1LL << ((int)v11 % 64))) != 0 )
printf("%d is ready to read\n", v11);
v12 = *v10;
if ( (writefds.fds_bits[(int)v12 / 64] & (1LL << ((int)v12 % 64))) != 0 )
printf("%d is ready to write\n", v12);
v13 = v10[1];
if ( (readfds.fds_bits[(int)v13 / 64] & (1LL << ((int)v13 % 64))) != 0 )
printf("%d is ready to read\n", v13);
v14 = v10[1];
if ( (writefds.fds_bits[(int)v14 / 64] & (1LL << ((int)v14 % 64))) != 0 )
printf("%d is ready to write\n", v14);
v10 = (unsigned int *)*((_QWORD *)v10 + 1034);
}
while ( v10 );
if ( (readfds.fds_bits[v5] & v6) == 0 )
goto LABEL_31;
goto LABEL_28;
}
if ( (readfds.fds_bits[v5] & v6) != 0 )
{
LABEL_28:
v15 = accept(v4, &addr, &addr_len);
v16 = v15;
if ( v15 == -1 )
{
perror("accept");
}
else if ( (unsigned int)fdladd(v15, &v59) == -1 )
{
perror("add socket");
}
else
{
printf("Accepted new %d\n", v16);
LABEL_31:
v17 = v59;
if ( v59 )
{
while ( 1 )
{
if ( !v17[2] )
goto LABEL_77;
if ( v17[3] == 1 )
{
if ( v17[5] )
goto LABEL_87;
if ( v17[8] )
goto LABEL_87;
v18 = v17[1];
if ( (readfds.fds_bits[(int)v18 / 64] & (1LL << ((int)v18 % 64))) == 0 )
goto LABEL_87;
printf("to read from sout %d\n", v18);
v19 = v17[6];
v20 = v17[7];
if ( v19 < v20 )
{
v23 = read(v17[1], (char *)v17 + v19 + 72, v20 - v19);
if ( v23 )
{
v24 = v17[6] + v23;
v17[6] = v24;
if ( v24 == v17[7] )
v17[5] = 1;
v17[4] = 0;
}
else
{
v17[8] = 1;
}
}
else
{
v21 = read(v17[1], (char *)v17 + v19 + 72, 0x2000LL - v19);
if ( v21 )
{
v22 = v17[6] + v21;
if ( v22 == 0x2000 )
v22 = 0;
v17[6] = v22;
if ( v22 == v17[7] )
v17[5] = 1;
v17[4] = 0;
}
else
{
v17[8] = 1;
}
}
if ( !v17[2] )
goto LABEL_77;
}
if ( !v17[3] )
{
v40 = *v17 + 63;
if ( (*v17 & 0x80000000) == 0 )
v40 = *v17;
if ( (readfds.fds_bits[v40 >> 6] & (1LL << ((int)*v17 % 64))) == 0 )
goto LABEL_84;
memset(buf, 0, 0x2000uLL);
printf("to read from sin %d\n", *v17);
v25 = read(*v17, buf, 0x2000uLL);
v26 = v25;
if ( v25 > 0 )
{
write(1, buf, v25);
if ( (unsigned int)parsereq(buf, v26, &src, &v52) == -1 )
{
close(*v17);
close(v17[1]);
v17[2] = 0;
}
else
{
v27 = v52;
v28 = (char *)malloc(v52 + 1);
*((_QWORD *)v17 + 6) = v28;
v29 = strncpy(v28, src, v27);
*((_QWORD *)v17 + 6) = v29;
v29[v27] = 0;
parseurl(*((_QWORD *)v17 + 6), &v52, &v54, &v55);
v30 = v52;
v31 = (char *)malloc(v52 + 1);
*((_QWORD *)v17 + 7) = v31;
v32 = strncpy(v31, v54, v30);
*((_QWORD *)v17 + 7) = v32;
v32[v30] = 0;
v33 = strlen(*((const char **)v17 + 6)) - v30;
v52 = v33;
v34 = (char *)malloc(v33 + 1);
*((_QWORD *)v17 + 8) = v34;
v35 = strncpy(v34, v55, v33);
*((_QWORD *)v17 + 8) = v35;
v35[v33] = 0;
v36 = getaddrinfo(*((const char **)v17 + 7), "http", &req, (struct addrinfo **)v17 + 1033);
if ( v36 )
{
v37 = gai_strerror(v36);
printf("getaddrinfo: %s\n", v37);
write(*v17, "HTTP/1.0 400\r\n\r\n", 0x11uLL);
close(*v17);
fdlrem(*v17, v59);
}
else if ( connect(
v17[1],
*(const struct sockaddr **)(*((_QWORD *)v17 + 1033) + 24LL),
*(_DWORD *)(*((_QWORD *)v17 + 1033) + 16LL)) == -1 )
{
if ( *_errno_location() == 115 )
{
printf("wait for connection on %d\n", *v17);
}
else
{
close(*v17);
fdlrem(*v17, v59);
}
}
}
goto LABEL_77;
}
if ( !v25 )
{
close(*v17);
fdlrem(*v17, v59);
printf("Closed %d\n", *v17);
}
if ( !v17[2] )
goto LABEL_77;
if ( !v17[3] )
{
LABEL_84:
v41 = v17[1];
if ( (writefds.fds_bits[v41 / 64] & (1LL << (v41 % 64))) == 0 )
goto LABEL_76;
n = (int)(strlen(*((const char **)v17 + 8)) + 17);
s = (char *)malloc(n);
printf("to write to sout %d\n", (unsigned int)v41);
sprintf(s, "GET %s HTTP/1.0\r\n\r\n", *((const char **)v17 + 8));
if ( write(v17[1], s, n) <= 0 )
{
perror("write sout");
close(*v17);
fdlrem(*v17, v59);
goto LABEL_77;
}
v17[3] = 1;
free(s);
printf("request on %d\n", v17[1]);
if ( !v17[2] )
goto LABEL_77;
}
}
LABEL_87:
if ( v17[3] != 1 )
goto LABEL_76;
if ( v17[9] || v17[4] )
goto LABEL_75;
v42 = *v17;
v43 = *v17 + 63;
if ( (*v17 & 0x80000000) == 0 )
v43 = *v17;
if ( (writefds.fds_bits[v43 >> 6] & (1LL << (v42 % 64))) == 0 )
goto LABEL_75;
v44 = v17[7];
v45 = v17[6];
v46 = (__int64)(v17 + 2066);
if ( v44 < v45 )
v46 = (__int64)v17 + v45 + 72;
v38 = send(v42, (char *)v17 + v44 + 72, v46 - ((_QWORD)v17 + v44 + 72), 0);
if ( v38 > 0 )
{
v39 = v17[7] + v38;
v17[7] = v39;
v17[5] = 0;
if ( v39 == 0x2000 )
v17[7] = 0;
if ( v17[7] == v17[6] )
v17[4] = 1;
if ( !v17[2] )
goto LABEL_77;
if ( v17[3] != 1 )
goto LABEL_76;
if ( v17[9] != 1 )
goto LABEL_76;
LABEL_75:
v47 = *v17;
v48 = *v17 + 63;
if ( (*v17 & 0x80000000) == 0 )
v48 = *v17;
if ( (writefds.fds_bits[v48 >> 6] & (1LL << (v47 % 64))) != 0 )
{
v17[7] += write(
v47,
(const void *)(*((_QWORD *)v17 + 5) + (int)v17[7] + 128LL),
(int)(*(_DWORD *)(*((_QWORD *)v17 + 5) + 51328LL) - v17[7]));
printf("work from cache for %s\n", *((const char **)v17 + 6));
if ( v17[7] != *(_DWORD *)(*((_QWORD *)v17 + 5) + 51328LL) )
{
if ( !v17[2] )
goto LABEL_77;
goto LABEL_76;
}
v17[8] = 1;
v17[4] = 1;
if ( !v17[2] )
goto LABEL_77;
}
else
{
LABEL_76:
if ( !v17[8] || !v17[4] )
goto LABEL_77;
}
fdlrem(*v17, v59);
printf("Completed %d\n", *v17);
goto LABEL_77;
}
perror("write sin");
close(*v17);
fdlrem(*v17, v59);
LABEL_77:
v17 = (unsigned int *)*((_QWORD *)v17 + 1034);
if ( !v17 )
goto LABEL_7;
}
}
}
}
}
else
{
puts(".");
}
}
perror("select");
printf("gotcha");
fdlfree(v59);
freeaddrinfo(pai);
close(v4);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 int status, new_fd;
 int listensock;
 struct addrinfo hints, *res;
 struct sockaddr saddr;
 socklen_t saddr_size = sizeof(saddr);
 char buf[8192];
 int ret;
 struct fdnode *fdl = ((void *)0), *writefdl = ((void *)0), *t;
 struct cnode *cache = ((void *)0);
 fd_set readfds, writefds;
 struct timeval timeout;
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 if (argc != 2) {
  printf("Usage: %s PORT\n", argv[0]);
  exit(1);
 }
 if (ret = getaddrinfo(((void *)0), argv[1], &hints, &res)) {
  printf("getaddrinfo: %s\n", gai_strerror(ret));
  exit(1);
 }
 if ((listensock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
  perror("socket");
  freeaddrinfo(res);
  exit(1);
 }
 if (bind(listensock, res->ai_addr, res->ai_addrlen) == -1) {
  perror("bind");
  freeaddrinfo(res);
  close(listensock);
  exit(1);
 }
 if (listen(listensock, 128) == -1) {
  perror("listen");
  freeaddrinfo(res);
  close(listensock);
  exit(1);
 }
 timeout.tv_sec = 60 * 3;
 timeout.tv_usec = 0;
 while (1) {
  struct cnode *c;
  int i = 0;
  ret = setfds(&readfds, &writefds, fdl, listensock);
  printf("wait for select\n");
  ret = select(ret, &readfds, &writefds, ((void *)0), &timeout);
  printf("got selected %d fds\n", ret);
  if (ret < 0) {
   perror("select");
   break;
  }
  if (ret == 0) {
   printf(".\n");
   continue;
  }
  for (c = cache; c != ((void *)0); c = c->next) {
   i++;
   printf("%s\n", c->url);
  }
  printf("Got %d cache pages\n", i);
  for (t = fdl; t != ((void *)0); t = t->next) {
   if (((((&readfds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to read\n", t->fd);
   if (((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to write\n", t->fd);
   if (((((&readfds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to read\n", t->sout);
   if (((((&writefds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to write\n", t->sout);
  }
  if (((((&readfds)->__fds_bits)[((listensock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listensock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   new_fd = accept(listensock, &saddr, &saddr_size);
   if (new_fd == -1) {
    perror("accept");
    continue;
   }
   ret = fdladd(new_fd, &fdl);
   if (ret == -1) {
    perror("add socket");
    continue;
   }
   printf("Accepted new %d\n", new_fd);
  }
  for (t = fdl; t != ((void *)0); t = t->next) {
   if (t->use && t->rw == 1 && !t->full && !t->eof && ((((&readfds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    printf("to read from sout %d\n", t->sout);
    if (t->nread >= t->nwrote) {
     ret = read(t->sout, t->buf + t->nread, sizeof(t->buf)-t->nread);
     if (ret == 0) {
      t->eof = 1;
     } else {
      t->nread += ret;
      if (t->nread == sizeof(t->buf)) t->nread = 0;
      if (t->nread == t->nwrote) t->full = 1;
      t->empty = 0;
     }
    } else {
     ret = read(t->sout, t->buf + t->nread, t->nwrote-t->nread);
     if (ret == 0) {
      t->eof = 1;
     } else {
      t->nread += ret;
      if (t->nread == t->nwrote) t->full = 1;
      t->empty = 0;
     }
    }
   }
   if (t->use && t->rw == 0 && ((((&readfds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    memset(buf, 0, 8192);
    printf("to read from sin %d\n", t->fd);
    ret = read(t->fd, buf, 8192);
    if (ret > 0) {
     int len;
     char *url;
     char *host;
     char *dir;
     write(1, buf, ret);
     if (parsereq(buf, ret, &url, &len) == -1) {
      close(t->fd);
      close(t->sout);
      t->use = 0;
      continue;
     }
     t->url = malloc((len+1)*sizeof(char));
     t->url = strncpy(t->url, url, len);
     t->url[len] = '\0';
     parseurl(t->url, &len, &host, &dir);
     t->host = malloc((len+1)*sizeof(char));
     t->host = strncpy(t->host, host, len);
     t->host[len] = '\0';
     len = strlen(t->url) - len;
     t->uri = malloc((len+1)*sizeof(char));
     t->uri = strncpy(t->uri, dir, len);
     t->uri[len] = '\0';
     if (ret = getaddrinfo(t->host, "http", &hints, &t->addr)) {
      printf("getaddrinfo: %s\n", gai_strerror(ret));
      write(t->fd, "HTTP/1.0 400\r\n\r\n", 17);
      close(t->fd);
      fdlrem(t->fd, fdl);
      continue;
     }
     if (connect(t->sout, t->addr->ai_addr, t->addr->ai_addrlen) == -1) {
      if ((*__errno_location ()) == 115) {
       printf("wait for connection on %d\n", t->fd);
       continue;
      }
      close(t->fd);
      fdlrem(t->fd, fdl);
     }
     continue;
    }
    if (ret == 0) {
     close(t->fd);
     fdlrem(t->fd, fdl);
     printf("Closed %d\n", t->fd);
    }
   }
   if (t->use && t->rw == 0 && ((((&writefds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    int len = strlen(t->uri) + 3 + 8 + 6;
    char *msg = malloc(len*sizeof(char));
    printf("to write to sout %d\n", t->sout);
    sprintf(msg, "GET %s HTTP/1.0\r\n\r\n", t->uri);
    if (write(t->sout, msg, len) <= 0) {
     perror("write sout");
     close(t->fd);
     fdlrem(t->fd, fdl);
     continue;
    }
    t->rw = 1;
    free(msg);
    printf("request on %d\n", t->sout);
   }
   if (t->use && t->rw == 1 && t->cached == 0 && !t->empty && ((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    char *l;
    if (t->nwrote < t->nread)
     l = t->buf + t->nread;
    else
     l = t->buf + sizeof(t->buf);
    ret = send(t->fd, t->buf + t->nwrote, l - (t->buf + t->nwrote), 0);
    if (ret <= 0) {
     perror("write sin");
     close(t->fd);
     fdlrem(t->fd, fdl);
     continue;
    }
    t->nwrote += ret;
    t->full = 0;
    if (t->nwrote == sizeof(t->buf)) t->nwrote = 0;
    if (t->nwrote == t->nread) t->empty = 1;
   }
   if (t->use && t->rw == 1 && t->cached == 1 && ((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    ret = write(t->fd, t->cache->content + t->nwrote, t->cache->size - t->nwrote);
    t->nwrote += ret;
    printf("work from cache for %s\n", t->url);
    if (t->nwrote == t->cache->size) {
     t->eof = 1;
     t->empty = 1;
    }
   }
   if (t->use && t->eof && t->empty) {
    fdlrem(t->fd, fdl);
    printf("Completed %d\n", t->fd);
    continue;
   }
  }
 }
 printf("gotcha");
 fdlfree(fdl);
 freeaddrinfo(res);
 close(listensock);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int status; int new_fd;
 int listensock;
 struct addrinfo hints; struct addrinfo *res;
 struct sockaddr saddr;
 unsigned int saddr_size = sizeof(saddr);
 char buf[8192];
 int ret;
 struct fdnode *fdl = ((void *)0); struct fdnode *writefdl = ((void *)0); struct fdnode *t;
 struct cnode *cache = ((void *)0);
 struct fd_set readfds; struct fd_set writefds;
 struct timeval timeout;
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 if (argc != 2) {
  printf("Usage: %s PORT\n", argv[0]);
  exit(1);
 }
 if (ret = getaddrinfo(((void *)0), argv[1], &hints, &res)) {
  printf("getaddrinfo: %s\n", gai_strerror(ret));
  exit(1);
 }
 if ((listensock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
  perror("socket");
  freeaddrinfo(res);
  exit(1);
 }
 if (bind(listensock, res->ai_addr, res->ai_addrlen) == -1) {
  perror("bind");
  freeaddrinfo(res);
  close(listensock);
  exit(1);
 }
 if (listen(listensock, 128) == -1) {
  perror("listen");
  freeaddrinfo(res);
  close(listensock);
  exit(1);
 }
 timeout.tv_sec = 60 * 3;
 timeout.tv_usec = 0;
 while (1) {
  struct cnode *c;
  int i = 0;
  ret = setfds(&readfds, &writefds, fdl, listensock);
  printf("wait for select\n");
  ret = select(ret, &readfds, &writefds, ((void *)0), &timeout);
  printf("got selected %d fds\n", ret);
  if (ret < 0) {
   perror("select");
   break;
  }
  if (ret == 0) {
   printf(".\n");
   continue;
  }
  for (c = cache; c != ((void *)0); c = c->next) {
   i++;
   printf("%s\n", c->url);
  }
  printf("Got %d cache pages\n", i);
  for (t = fdl; t != ((void *)0); t = t->next) {
   if (((((&readfds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to read\n", t->fd);
   if (((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to write\n", t->fd);
   if (((((&readfds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to read\n", t->sout);
   if (((((&writefds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    printf("%d is ready to write\n", t->sout);
  }
  if (((((&readfds)->__fds_bits)[((listensock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listensock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   new_fd = accept(listensock, &saddr, &saddr_size);
   if (new_fd == -1) {
    perror("accept");
    continue;
   }
   ret = fdladd(new_fd, &fdl);
   if (ret == -1) {
    perror("add socket");
    continue;
   }
   printf("Accepted new %d\n", new_fd);
  }
  for (t = fdl; t != ((void *)0); t = t->next) {
   if (t->use && t->rw == 1 && !t->full && !t->eof && ((((&readfds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    printf("to read from sout %d\n", t->sout);
    if (t->nread >= t->nwrote) {
     ret = read(t->sout, t->buf + t->nread, sizeof(t->buf)-t->nread);
     if (ret == 0) {
      t->eof = 1;
     } else {
      t->nread += ret;
      if (t->nread == sizeof(t->buf)) t->nread = 0;
      if (t->nread == t->nwrote) t->full = 1;
      t->empty = 0;
     }
    } else {
     ret = read(t->sout, t->buf + t->nread, t->nwrote-t->nread);
     if (ret == 0) {
      t->eof = 1;
     } else {
      t->nread += ret;
      if (t->nread == t->nwrote) t->full = 1;
      t->empty = 0;
     }
    }
   }
   if (t->use && t->rw == 0 && ((((&readfds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    memset(buf, 0, 8192);
    printf("to read from sin %d\n", t->fd);
    ret = read(t->fd, buf, 8192);
    if (ret > 0) {
     int len;
     char *url;
     char *host;
     char *dir;
     write(1, buf, ret);
     if (parsereq(buf, ret, &url, &len) == -1) {
      close(t->fd);
      close(t->sout);
      t->use = 0;
      continue;
     }
     t->url = malloc((len+1)*sizeof(char));
     t->url = strncpy(t->url, url, len);
     t->url[len] = '\0';
     parseurl(t->url, &len, &host, &dir);
     t->host = malloc((len+1)*sizeof(char));
     t->host = strncpy(t->host, host, len);
     t->host[len] = '\0';
     len = strlen(t->url) - len;
     t->uri = malloc((len+1)*sizeof(char));
     t->uri = strncpy(t->uri, dir, len);
     t->uri[len] = '\0';
     if (ret = getaddrinfo(t->host, "http", &hints, &t->addr)) {
      printf("getaddrinfo: %s\n", gai_strerror(ret));
      write(t->fd, "HTTP/1.0 400\r\n\r\n", 17);
      close(t->fd);
      fdlrem(t->fd, fdl);
      continue;
     }
     if (connect(t->sout, t->addr->ai_addr, t->addr->ai_addrlen) == -1) {
      if ((*__errno_location ()) == 115) {
       printf("wait for connection on %d\n", t->fd);
       continue;
      }
      close(t->fd);
      fdlrem(t->fd, fdl);
     }
     continue;
    }
    if (ret == 0) {
     close(t->fd);
     fdlrem(t->fd, fdl);
     printf("Closed %d\n", t->fd);
    }
   }
   if (t->use && t->rw == 0 && ((((&writefds)->__fds_bits)[((t->sout) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->sout) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    int len = strlen(t->uri) + 3 + 8 + 6;
    char *msg = malloc(len*sizeof(char));
    printf("to write to sout %d\n", t->sout);
    sprintf(msg, "GET %s HTTP/1.0\r\n\r\n", t->uri);
    if (write(t->sout, msg, len) <= 0) {
     perror("write sout");
     close(t->fd);
     fdlrem(t->fd, fdl);
     continue;
    }
    t->rw = 1;
    free(msg);
    printf("request on %d\n", t->sout);
   }
   if (t->use && t->rw == 1 && t->cached == 0 && !t->empty && ((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    char *l;
    if (t->nwrote < t->nread)
     l = t->buf + t->nread;
    else
     l = t->buf + sizeof(t->buf);
    ret = send(t->fd, t->buf + t->nwrote, l - (t->buf + t->nwrote), 0);
    if (ret <= 0) {
     perror("write sin");
     close(t->fd);
     fdlrem(t->fd, fdl);
     continue;
    }
    t->nwrote += ret;
    t->full = 0;
    if (t->nwrote == sizeof(t->buf)) t->nwrote = 0;
    if (t->nwrote == t->nread) t->empty = 1;
   }
   if (t->use && t->rw == 1 && t->cached == 1 && ((((&writefds)->__fds_bits)[((t->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    ret = write(t->fd, t->cache->content + t->nwrote, t->cache->size - t->nwrote);
    t->nwrote += ret;
    printf("work from cache for %s\n", t->url);
    if (t->nwrote == t->cache->size) {
     t->eof = 1;
     t->empty = 1;
    }
   }
   if (t->use && t->eof && t->empty) {
    fdlrem(t->fd, fdl);
    printf("Completed %d\n", t->fd);
    continue;
   }
  }
 }
 printf("gotcha");
 fdlfree(fdl);
 freeaddrinfo(res);
 close(listensock);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: status int

variable_types: listensock int

variable_types:
buf
char [8192]

variable_types: ret int

variable_types: struct fdnode { int fd; int sout; int use; int rw; int empty; int full; int nread; int nwrote; int eof; int cached; struct cnode { char url[128]; char content[51200]; int size; int completed; struct cnode *next; } *cache; char *url; char *host; char *uri; char buf[8192]; struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *addr; struct fdnode *next; } *

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

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

variable_types: len int

variable_types:
url
char *

variable_types:
host
char *

variable_types:
dir
char *

variable_types:
msg
char *

variable_types:
l
char *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

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

user_defined_types: struct fdnode { int fd; int sout; int use; int rw; int empty; int full; int nread; int nwrote; int eof; int cached; struct cnode *cache; char *url; char *host; char *uri; char buf[8192]; struct addrinfo *addr; struct fdnode *next; }


parsereq

repo: mkazantsev/os

decompiled_code:
__int64 __fastcall parsereq(char *haystack, __int64 a2, _QWORD *a3, _DWORD *a4)
{
char *v6; // rbx
_BYTE *v7; // rax

v6 = strstr(haystack, "GET");
if ( !strstr(haystack, "HTTP/1.0") )
return 0xFFFFFFFFLL;
if ( !v6 || haystack != v6 )
return 0xFFFFFFFFLL;
*a3 = v6 + 4;
if ( v6[4] == 32 )
{
LODWORD(v7) = (_DWORD)v6 + 4;
}
else
{
v7 = v6 + 4;
do
++v7;
while ( *v7 != 32 );
}
*a4 = (_DWORD)v7 - ((_DWORD)v6 + 4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *haystack, __int64 a2, _QWORD *a3, _DWORD *a4)
{
char *v6; // rbx
_BYTE *v7; // rax

v6 = strstr(haystack, "GET");
if ( !strstr(haystack, "HTTP/1.0") )
return 0xFFFFFFFFLL;
if ( !v6 || haystack != v6 )
return 0xFFFFFFFFLL;
*a3 = v6 + 4;
if ( v6[4] == 32 )
{
LODWORD(v7) = (_DWORD)v6 + 4;
}
else
{
v7 = v6 + 4;
do
++v7;
while ( *v7 != 32 );
}
*a4 = (_DWORD)v7 - ((_DWORD)v6 + 4);
return 0LL;
}

original_code:
int parsereq(char req[], int size, char **url, int *url_len) {
        char *pos = strstr(req, "GET");
        int len;
        if (strstr(req, "HTTP/1.0") == ((void *)0))
                return -1;
        if (pos == ((void *)0) || pos != req)
                return -1;
        pos+=3;
        pos++;
        *url = pos;
        while (*pos != ' ') {
                pos++;
        }
        *url_len = (int) pos - (int) *url;
        return 0;
}

variable_types:
req
char []

variable_types: size int

variable_types:
url
char **

variable_types:
pos
char *

return_type: int


cached

decompiled_code:
char *__fastcall cached(char *s, char *a2)
{
char *i; // rbx
size_t v3; // r12

for ( i = s; i; i = (char *)*((_QWORD *)i + 6417) )
{
if ( i == strstr(i, a2) )
{
v3 = strlen(i);
if ( v3 == strlen(a2) && *((_DWORD *)i + 12833) == 1 )
break;
}
}
return i;
}

canonical_decompiled_code:
char *__fastcall func0(char *s, char *a2)
{
char *i; // rbx
size_t v3; // r12

for ( i = s; i; i = (char *)*((_QWORD *)i + 6417) )
{
if ( i == strstr(i, a2) )
{
v3 = strlen(i);
if ( v3 == strlen(a2) && *((_DWORD *)i + 12833) == 1 )
break;
}
}
return i;
}

original_code:
struct cnode *cached(struct cnode *head, char url[]) {
        struct cnode *t = head;
        for (; t != ((void *)0); t = t->next) {
                if (strstr(t->url, url) == t->url && strlen(t->url) == strlen(url) && t->completed == 1) {
                        return t;
  }
        }
        return ((void *)0);
}

variable_types:
head
struct cnode *

variable_types:
url
char []

variable_types: struct cnode { char url[128]; char content[51200]; int size; int completed; struct cnode *next; } *


createcache

original_code:
int createcache(struct cnode **head, char url[]) {
        struct cnode *t = *head, *p;
        p = (struct cnode *) malloc(sizeof(struct cnode));
        strcpy(p->url, url);
        p->size = 0;
        p->completed = 0;
        p->next = ((void *)0);
        if (*head == ((void *)0)) {
                *head = p;
                return 0;
        }
        for (; t->next != ((void *)0); t = t->next)
                ;
        t->next = p;
        return 0;
}

canonical_original_code:
int createcache(struct cnode **head, char url[]) {
        struct cnode *t = *head; struct cnode *p;
        p = (struct cnode *) malloc(sizeof(struct cnode));
        strcpy(p->url, url);
        p->size = 0;
        p->completed = 0;
        p->next = ((void *)0);
        if (*head == ((void *)0)) {
                *head = p;
                return 0;
        }
        for (; t->next != ((void *)0); t = t->next)
                ;
        t->next = p;
        return 0;
}

variable_types: struct cnode { char url[128]; char content[51200]; int size; int completed; struct cnode *next; } **


addtocache

original_code:
int addtocache(struct cnode *node, char content[], int size) {
        int i;
        for (i = 0; i < size && node->size+i < 51200; i++) {
                node->content[i+node->size] = content[i];
        }
 node->size += size;
        return 0;
}

variable_types:
content
char []

variable_types: i int

user_defined_types: struct cnode { char url[128]; char content[51200]; int size; int completed; struct cnode *next; }


cacheit

decompiled_code:
__int64 __fastcall cacheit(char **a1, char *a2, __int64 a3, int a4)
{
char *v6; // rax

v6 = cached(*a1, a2);
if ( v6 )
{
if ( !*((_DWORD *)v6 + 12833) )
addtocache((__int64)v6, a3, a4);
}
else
{
createcache(a1, a2);
addtocache((__int64)*a1, a3, a4);
}
return 0LL;
}

original_code:
int cacheit(struct cnode **head, char url[], char content[], int size) {
        struct cnode *t;
        if ((t = cached(*head, url)) == ((void *)0)) {
                createcache(head, url);
                addtocache(*head, content, size);
        } else {
                if (!t->completed) {
                        addtocache(t, content, size);
                }
        }
        return 0;
}


completecache

decompiled_code:
void __fastcall completecache(char *s, char *a2)
{
char *i; // rbx
size_t v3; // r12

for ( i = s; i; i = (char *)*((_QWORD *)i + 6417) )
{
if ( i == strstr(i, a2) )
{
v3 = strlen(i);
if ( v3 == strlen(a2) )
*((_DWORD *)i + 12833) = 1;
}
}
}

canonical_decompiled_code:
void __fastcall func4(char *s, char *a2)
{
char *i; // rbx
size_t v3; // r12

for ( i = s; i; i = (char *)*((_QWORD *)i + 6417) )
{
if ( i == strstr(i, a2) )
{
v3 = strlen(i);
if ( v3 == strlen(a2) )
*((_DWORD *)i + 12833) = 1;
}
}
}

original_code:
int completecache(struct cnode *head, char url[]) {
        struct cnode *t = head;
        for (; t != ((void *)0); t = t->next) {
                if (strstr(t->url, url) == t->url && strlen(t->url) == strlen(url))
 t->completed = 1;
 }
}


repo: mkfifo/plot

decompiled_code:
__int64 __fastcall plot_hash_init(__int64 a1)
{
return lh_init(a1, 32LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return lh_init(a1, 32LL);
}

original_code:
int plot_hash_init(plot_hash *hash){
    return lh_init(&(hash->lht), 32);
}

canonical_original_code:
int plot_hash_init(struct plot_hash *hash){
    return lh_init(&(hash->lht), 32);
}

variable_types: struct plot_hash { struct lh_table { unsigned long size; unsigned long n_elems; unsigned int threshold; struct lh_entry { enum lh_entry_state { LH_ENTRY_EMPTY=0, LH_ENTRY_OCCUPIED=1, LH_ENTRY_DUMMY=2 } state; unsigned long hash; char *key; unsigned long key_len; void *data; } *entries; } lht; } *

user_defined_types: struct plot_hash { struct lh_table lht; }

user_defined_types: struct lh_table { unsigned long size; unsigned long n_elems; unsigned int threshold; struct lh_entry *entries; }

user_defined_types: struct lh_entry { enum lh_entry_state state; unsigned long hash; char *key; unsigned long key_len; void *data; }


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

if ( a1 )
result = lh_destroy(a1, 0LL, 0LL);
return result;
}

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

if ( a1 )
result = lh_destroy(a1, 0LL, 0LL);
return result;
}

original_code:
void plot_hash_cleanup(plot_hash *hash){
    if( ! hash )
        return;
    if( lh_destroy(&(hash->lht), 0, 0) ){
    } else {
    }
}

canonical_original_code:
void plot_hash_cleanup(struct plot_hash *hash){
    if( ! hash )
        return;
    if( lh_destroy(&(hash->lht), 0, 0) ){
    } else {
    }
}

return_type: void

user_defined_types: enum lh_entry_state { LH_ENTRY_EMPTY=0, LH_ENTRY_OCCUPIED=1, LH_ENTRY_DUMMY=2 }


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

result = 0LL;
if ( a1 )
result = lh_nelems();
return result;
}

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

result = 0LL;
if ( a1 )
result = lh_nelems();
return result;
}

original_code:
unsigned int plot_hash_nelems(plot_hash *hash){
    if( ! hash ){
        return 0;
    }
    return lh_nelems(&(hash->lht));
}

canonical_original_code:
unsigned int plot_hash_nelems(struct plot_hash *hash){
    if( ! hash ){
        return 0;
    }
    return lh_nelems(&(hash->lht));
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall plot_parse_comment(__int64 a1, __int64 *a2)
{
__int64 result; // rax
unsigned __int8 v3; // cl

while ( 1 )
{
result = *a2;
v3 = *(_BYTE *)(a1 + *a2);
if ( v3 <= 0xDu && ((1LL << v3) & 0x2401) != 0 )
break;
*a2 = result + 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 *a2)
{
__int64 result; // rax
unsigned __int8 v3; // cl

while ( 1 )
{
result = *a2;
v3 = *(_BYTE *)(a1 + *a2);
if ( v3 <= 0xDu && ((1LL << v3) & 0x2401) != 0 )
break;
*a2 = result + 1;
}
return result;
}

original_code:
void plot_parse_comment(const char *source, size_t *upto){
    while( 1 ){
        switch( source[ *upto ] ){
            case '\0':
            case '\r':
            case '\n':
                return;
            default:
                break;
        }
        ++ *upto;
    }
}

canonical_original_code:
void plot_parse_comment(char *source, unsigned long *upto){
    while( 1 ){
        switch( source[ *upto ] ){
            case '\0':
            case '\r':
            case '\n':
                return;
            default:
                break;
        }
        ++ *upto;
    }
}

variable_types:
source
char *

variable_types:
upto
unsigned long *


decompiled_code:
__int64 __fastcall plot_hash_char_func(char a1)
{
if ( a1 <= 32 )
{
printf("Invalid char '%c' (%d)\n", (unsigned int)a1, (unsigned int)a1);
plot_fatal_error("plot_hash_char_func: character our of range (<33)");
}
else if ( a1 == 127 )
{
printf("Invalid char '%c' (%d)\n", 127LL, 127LL);
plot_fatal_error("plot_hash_char_func: character our of range (>126)");
}
return a1 - 32;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
if ( a1 <= 32 )
{
printf("Invalid char '%c' (%d)\n", (unsigned int)a1, (unsigned int)a1);
plot_fatal_error("plot_hash_char_func: character our of range (<33)");
}
else if ( a1 == 127 )
{
printf("Invalid char '%c' (%d)\n", 127LL, 127LL);
plot_fatal_error("plot_hash_char_func: character our of range (>126)");
}
return a1 - 32;
}

original_code:
static unsigned long long plot_hash_char_func(char ch){
    unsigned long long v;
    if( ch < 33 ){
        printf("Invalid char '%c' (%d)\n", ch, ch);
        plot_fatal_error("plot_hash_char_func: character our of range (<33)");
    }
    if( ch > 126 ){
        printf("Invalid char '%c' (%d)\n", ch, ch);
        plot_fatal_error("plot_hash_char_func: character our of range (>126)");
    }
    v = ch - 32;
    return v;
}

variable_types: ch char

variable_types:
v
unsigned long long

return_type: unsigned long long


decompiled_code:
void __fastcall plot_hash_symbol(__int64 a1)
{
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx

if ( !a1 )
plot_fatal_error("plot_hash_symbol: Invalid plot_symbol given as argument");
v1 = *(_QWORD *)(a1 + 16);
if ( !v1 )
{
if ( *(int *)(a1 + 8) > 1 )
{
v2 = *(_QWORD *)(a1 + 16);
do
{
v3 = lpow(0x5FuLL, v2);
v1 += plot_hash_char_func(*(_BYTE *)(*(_QWORD *)a1 + v2++)) * v3;
}
while ( *(_DWORD *)(a1 + 8) - 1 > (int)v2 );
}
*(_QWORD *)(a1 + 16) = v1;
}
}

canonical_decompiled_code:
void __fastcall func19(__int64 a1)
{
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx

if ( !a1 )
plot_fatal_error("plot_hash_symbol: Invalid plot_symbol given as argument");
v1 = *(_QWORD *)(a1 + 16);
if ( !v1 )
{
if ( *(int *)(a1 + 8) > 1 )
{
v2 = *(_QWORD *)(a1 + 16);
do
{
v3 = func0(0x5FuLL, v2);
v1 += func1(*(_BYTE *)(*(_QWORD *)a1 + v2++)) * v3;
}
while ( *(_DWORD *)(a1 + 8) - 1 > (int)v2 );
}
*(_QWORD *)(a1 + 16) = v1;
}
}

original_code:
void plot_hash_symbol(plot_symbol *s){
    int i;
    unsigned long long hash = 0;
    if( !s ){
        plot_fatal_error("plot_hash_symbol: Invalid plot_symbol given as argument");
    }
    if( s->hash ){
        return;
    }
    for( i=0; i< (s->len - 1); ++i ){
        hash += lpow(95, i) * plot_hash_char_func( s->val[i] );
    }
    s->hash = hash;
}

canonical_original_code:
void plot_hash_symbol(struct plot_symbol *s){
    int i;
    unsigned long long hash = 0;
    if( !s ){
        plot_fatal_error("plot_hash_symbol: Invalid plot_symbol given as argument");
    }
    if( s->hash ){
        return;
    }
    for( i=0; i< (s->len - 1); ++i ){
        hash += lpow(95, i) * plot_hash_char_func( s->val[i] );
    }
    s->hash = hash;
}

variable_types: struct plot_symbol { char *val; int len; int size; unsigned long long hash; } *

variable_types:
hash
unsigned long long


decompiled_code:
__int64 __fastcall plot_env_init(_QWORD *a1, _QWORD *a2)
{
if ( !a1 )
return 0LL;
if ( a1 == a2 )
{
printf("TRYING TO SET PARENT TO SELF '%p' and '%p'\n", a1, a2);
exit(1);
}
if ( a2 )
{
a1[2] = a2;
plot_env_incr(a2);
}
plot_hash_init(a1 + 3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, _QWORD *a2)
{
if ( !a1 )
return 0LL;
if ( a1 == a2 )
{
printf("TRYING TO SET PARENT TO SELF '%p' and '%p'\n", a1, a2);
exit(1);
}
if ( a2 )
{
a1[2] = a2;
plot_env_incr(a2);
}
plot_hash_init(a1 + 3);
return 1LL;
}

original_code:
int plot_env_init(plot_env *env, plot_env *parent){
    if( ! env )
        return 0;
    if( env == parent ){
        printf("TRYING TO SET PARENT TO SELF '%p' and '%p'\n", (void*) env, (void*) parent);
        exit(1);
    }
    if( parent ){
        env->parent = parent;
        plot_env_incr(parent);
    }
    plot_hash_init(&(env->hash));
    return 1;
}

canonical_original_code:
int plot_env_init(struct plot_env *env, struct plot_env *parent){
    if( ! env )
        return 0;
    if( env == parent ){
        printf("TRYING TO SET PARENT TO SELF '%p' and '%p'\n", (void *) env, (void *) parent);
        exit(1);
    }
    if( parent ){
        env->parent = parent;
        plot_env_incr(parent);
    }
    plot_hash_init(&(env->hash));
    return 1;
}

variable_types: struct plot_env { struct plot_gc { int refcount; struct plot_gc *next; } gc; struct plot_env *parent; struct plot_hash { struct lh_table { unsigned long size; unsigned long n_elems; unsigned int threshold; struct lh_entry { enum lh_entry_state { LH_ENTRY_EMPTY=0, LH_ENTRY_OCCUPIED=1, LH_ENTRY_DUMMY=2 } state; unsigned long hash; char *key; unsigned long key_len; void *data; } *entries; } lht; } hash; } *

variable_types:
parent
struct plot_env *

user_defined_types: struct plot_env { struct plot_gc gc; struct plot_env *parent; struct plot_hash hash; }


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

if ( a1 )
{
if ( *(_QWORD *)(a1 + 16) )
{
plot_env_decr();
*(_QWORD *)(a1 + 16) = 0LL;
}
result = plot_hash_cleanup(a1 + 24);
}
return result;
}

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

if ( a1 )
{
if ( *(_QWORD *)(a1 + 16) )
{
plot_env_decr();
*(_QWORD *)(a1 + 16) = 0LL;
}
result = plot_hash_cleanup(a1 + 24);
}
return result;
}

original_code:
void plot_env_cleanup(plot_env *env){
    if( ! env )
        return;
    if( env->parent ){
        plot_env_decr(env->parent);
        env->parent = 0;
    }
    plot_hash_cleanup(&(env->hash));
}

canonical_original_code:
void plot_env_cleanup(struct plot_env *env){
    if( ! env )
        return;
    if( env->parent ){
        plot_env_decr(env->parent);
        env->parent = 0;
    }
    plot_hash_cleanup(&(env->hash));
}

user_defined_types: struct plot_gc { int refcount; struct plot_gc *next; }


error

repo: mkhau/cl-barebones

decompiled_code:
void __noreturn error(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
fprintf(stderr, "error on line %d: ", (unsigned int)stmt_line);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
exit(2);
}

canonical_decompiled_code:
void __noreturn func1(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
fprintf(stderr, "error on line %d: ", (unsigned int)stmt_line);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
exit(2);
}

original_code:
void error (const char *fmt, ...)
{
  va_list ap;
  fprintf (stderr, "error on line %d: ", stmt_line);
  __builtin_va_start(ap,fmt);
  vfprintf (stderr, fmt, ap);
  __builtin_va_end(ap);
  fprintf (stderr, "\n");
  exit (2);
}

canonical_original_code:
void error(char *fmt, ...)
{
  __builtin_va_list ap;
  fprintf (stderr, "error on line %d: ", stmt_line);
  __builtin_va_start(ap,fmt);
  vfprintf (stderr, fmt, ap);
  __builtin_va_end(ap);
  fprintf (stderr, "\n");
  exit (2);
}

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall find_proc(char *s1)
{
__int64 i; // rbx

for ( i = proc_head; i; i = *(_QWORD *)(i + 56) )
{
if ( !strcasecmp(s1, *(const char **)(i + 48)) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s1)
{
__int64 i; // rbx

for ( i = proc_head; i; i = *(_QWORD *)(i + 56) )
{
if ( !strcasecmp(s1, *(const char **)(i + 48)) )
break;
}
return i;
}

original_code:
stmt_t *find_proc (char *name){
  stmt_t *proc;
  for (proc = proc_head; proc; proc = proc->proc)
    {
      if (strcasecmp (name, proc->name) == 0)
 return proc;
    }
  return ((void *)0);
}

canonical_original_code:
struct stmt_t *find_proc(char *name){
  struct stmt_t *proc;
  for (proc = proc_head; proc; proc = proc->proc)
    {
      if (strcasecmp (name, proc->name) == 0)
 return proc;
    }
  return ((void *)0);
}

variable_types:
name
char *

variable_types: struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 } type; struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t *fonc; struct env_t { struct env_t *parent; struct var_t *first; int level; } *env_l; unsigned long val; } *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; } *

user_defined_types: struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t type; struct var_t *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; }

user_defined_types: struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t *fonc; struct env_t *env_l; unsigned long val; }


decompiled_code:
__int64 __fastcall create_env(__int64 a1)
{
__int64 result; // rax
int v2; // edx

result = alloc(24LL);
*(_QWORD *)result = a1;
*(_QWORD *)(result + 8) = 0LL;
v2 = 0;
if ( a1 )
v2 = *(_DWORD *)(a1 + 16) + 1;
*(_DWORD *)(result + 16) = v2;
return result;
}

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

result = alloc(24LL);
*(_QWORD *)result = a1;
*(_QWORD *)(result + 8) = 0LL;
v2 = 0;
if ( a1 )
v2 = *(_DWORD *)(a1 + 16) + 1;
*(_DWORD *)(result + 16) = v2;
return result;
}

original_code:
env_t *create_env (env_t *parent)
{
  env_t *env;
  env = alloc (sizeof (env_t));
  env->parent = parent;
  env->first = ((void *)0);
  if (parent){
  env->level = parent->level + 1;
  } else {
 env->level = 0;
  }
  return env;
}

canonical_original_code:
struct env_t *create_env(struct env_t *parent)
{
  struct env_t *env;
  env = alloc (sizeof (env_t));
  env->parent = parent;
  env->first = ((void *)0);
  if (parent){
  env->level = parent->level + 1;
  } else {
 env->level = 0;
  }
  return env;
}

variable_types:
parent
struct env_t *

variable_types: struct env_t { struct env_t *parent; struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 } type; struct var_t *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; } *fonc; struct env_t *env_l; unsigned long val; } *first; int level; } *

user_defined_types: struct env_t { struct env_t *parent; struct var_t *first; int level; }


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

result = alloc(48LL);
*(_QWORD *)(result + 40) = 0LL;
*(_QWORD *)(result + 8) = a1;
*(_BYTE *)(result + 16) = 1;
return result;
}

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

result = alloc(48LL);
*(_QWORD *)(result + 40) = 0LL;
*(_QWORD *)(result + 8) = a1;
*(_BYTE *)(result + 16) = 1;
return result;
}

original_code:
var_t *copy_find_var (char *name)
{
  var_t *res = alloc (sizeof (var_t));
  res->val = 0;
  res->name = name;
  res->init = 1;
  return res;
}

canonical_original_code:
struct var_t *copy_find_var(char *name)
{
  struct var_t *res = alloc (sizeof (var_t));
  res->val = 0;
  res->name = name;
  res->init = 1;
  return res;
}

variable_types: struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 } type; struct var_t *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; } *fonc; struct env_t { struct env_t *parent; struct var_t *first; int level; } *env_l; unsigned long val; } *


decompiled_code:
__int64 __fastcall find_var(char *s)
{
_QWORD *v1; // r12
__int64 v2; // rbx

v1 = (_QWORD *)current_env;
if ( current_env || (v1 = (_QWORD *)create_env(0LL), (current_env = (__int64)v1) != 0) )
{
while ( 1 )
{
v2 = v1[1];
if ( v2 )
break;
LABEL_7:
v1 = (_QWORD *)*v1;
if ( !v1 )
goto LABEL_3;
}
while ( strcasecmp(s, *(const char **)(v2 + 8)) )
{
v2 = *(_QWORD *)v2;
if ( !v2 )
goto LABEL_7;
}
}
else
{
LABEL_3:
v2 = alloc(48LL);
*(_QWORD *)(v2 + 8) = strdup(s);
if ( init_to_zero )
{
*(_QWORD *)(v2 + 40) = 0LL;
*(_BYTE *)(v2 + 16) = 1;
}
else
{
*(_BYTE *)(v2 + 16) = 0;
}
*(_QWORD *)v2 = *(_QWORD *)(current_env + 8);
*(_QWORD *)(current_env + 8) = v2;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s)
{
_QWORD *v1; // r12
__int64 v2; // rbx

v1 = (_QWORD *)current_env;
if ( current_env || (v1 = (_QWORD *)func3(0LL), (current_env = (__int64)v1) != 0) )
{
while ( 1 )
{
v2 = v1[1];
if ( v2 )
break;
LABEL_7:
v1 = (_QWORD *)*v1;
if ( !v1 )
goto LABEL_3;
}
while ( strcasecmp(s, *(const char **)(v2 + 8)) )
{
v2 = *(_QWORD *)v2;
if ( !v2 )
goto LABEL_7;
}
}
else
{
LABEL_3:
v2 = alloc(48LL);
*(_QWORD *)(v2 + 8) = strdup(s);
if ( init_to_zero )
{
*(_QWORD *)(v2 + 40) = 0LL;
*(_BYTE *)(v2 + 16) = 1;
}
else
{
*(_BYTE *)(v2 + 16) = 0;
}
*(_QWORD *)v2 = *(_QWORD *)(current_env + 8);
*(_QWORD *)(current_env + 8) = v2;
}
return v2;
}

original_code:
var_t *find_var (char *name)
{
  var_t *var;
  env_t *env;
  if (!current_env) {
    current_env = create_env(((void *)0));
  }
  for (env = current_env; env; env = env->parent)
    {
    for (var = env->first; var; var = var->next)
      {
        if (strcasecmp (name, var->name) == 0)
        {
    return var;
  }
      }
  }
  var = alloc (sizeof (var_t));
  var->name = strdup (name);
  if (init_to_zero)
    {
      var->val = 0;
      var->init = 1;
    }
  else
    {
      var->init = 0;
    }
  var->next = current_env->first;
  current_env->first = var;
  return var;
}

canonical_original_code:
struct var_t *find_var(char *name)
{
  struct var_t *var;
  struct env_t *env;
  if (!current_env) {
    current_env = create_env(((void *)0));
  }
  for (env = current_env; env; env = env->parent)
    {
    for (var = env->first; var; var = var->next)
      {
        if (strcasecmp (name, var->name) == 0)
        {
    return var;
  }
      }
  }
  var = alloc (sizeof (var_t));
  var->name = strdup (name);
  if (init_to_zero)
    {
      var->val = 0;
      var->init = 1;
    }
  else
    {
      var->init = 0;
    }
  var->next = current_env->first;
  current_env->first = var;
  return var;
}

return_type: struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 } type; struct var_t *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; } *fonc; struct env_t { struct env_t *parent; struct var_t *first; int level; } *env_l; unsigned long val; } *


decompiled_code:
__int64 __fastcall new_int_var(unsigned int a1)
{
__int64 v1; // rbx
char *v2; // r12

v1 = alloc(48LL);
v2 = (char *)alloc(16LL);
sprintf(v2, "%d", a1);
*(_QWORD *)(v1 + 8) = v2;
*(_QWORD *)(v1 + 40) = (int)a1;
*(_BYTE *)(v1 + 16) = 1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
__int64 v1; // rbx
char *v2; // r12

v1 = alloc(48LL);
v2 = (char *)alloc(16LL);
sprintf(v2, "%d", a1);
*(_QWORD *)(v1 + 8) = v2;
*(_QWORD *)(v1 + 40) = (int)a1;
*(_BYTE *)(v1 + 16) = 1;
return v1;
}

original_code:
var_t *new_int_var (int num)
{
  var_t *var = alloc (sizeof (var_t));
  char *c = alloc (sizeof (char) * 16);
  sprintf(c, "%d", num);
 var->name = c;
 var->val = num;
 var->init = 1;
  return var;
}

canonical_original_code:
struct var_t *new_int_var(int num)
{
  struct var_t *var = alloc (sizeof (var_t));
  char *c = alloc (sizeof (char) * 16);
  sprintf(c, "%d", num);
 var->name = c;
 var->val = num;
 var->init = 1;
  return var;
}

variable_types: num int

variable_types:
c
char *

user_defined_types: enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 }


decompiled_code:
__int64 __fastcall new_arg_var(char *src)
{
__int64 v1; // rbx
char *v2; // rax

v1 = alloc(48LL);
v2 = (char *)alloc(32LL);
*(_QWORD *)(v1 + 8) = v2;
strcpy(v2, src);
*(_QWORD *)(v1 + 40) = 0LL;
*(_BYTE *)(v1 + 16) = 0;
*(_QWORD *)v1 = 0LL;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *src)
{
__int64 v1; // rbx
char *v2; // rax

v1 = alloc(48LL);
v2 = (char *)alloc(32LL);
*(_QWORD *)(v1 + 8) = v2;
strcpy(v2, src);
*(_QWORD *)(v1 + 40) = 0LL;
*(_BYTE *)(v1 + 16) = 0;
*(_QWORD *)v1 = 0LL;
return v1;
}

original_code:
var_t *new_arg_var (char* s)
{
  var_t *var = alloc (sizeof (var_t));
  var->name = alloc (sizeof (char) * 32);
  strcpy(var->name, s);
  var->val = 0;
  var->init = 0;
  var->next = ((void *)0);
  return var;
}

canonical_original_code:
struct var_t *new_arg_var(char *s)
{
  struct var_t *var = alloc (sizeof (var_t));
  var->name = alloc (sizeof (char) * 32);
  strcpy(var->name, s);
  var->val = 0;
  var->init = 0;
  var->next = ((void *)0);
  return var;
}

variable_types:
s
char *


original_code:
void set_var (var_t *var, uintmax_t val)
{
  var->val = val;
  var->init = 1;
}

canonical_original_code:
void set_var(struct var_t *var, unsigned long val)
{
  var->val = val;
  var->init = 1;
}

variable_types:
val
unsigned long


decompiled_code:
void __fastcall define_var(const char *a1)
{
char *v1; // rbx
char *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
__int64 v5; // rax

v1 = strchr(a1, 61);
v2 = (char *)newstrn(a1, v1 - a1);
v3 = a_to_intmax(v1 + 1);
if ( v3 < 0 )
{
fatal(2LL, "negative values are not permitted");
print_vars();
}
else
{
v4 = v3;
v5 = find_var(v2);
set_var(v5, v4);
}
}

canonical_decompiled_code:
void __fastcall func9(const char *a1)
{
char *v1; // rbx
char *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
__int64 v5; // rax

v1 = strchr(a1, 61);
v2 = (char *)newstrn(a1, v1 - a1);
v3 = a_to_intmax(v1 + 1);
if ( v3 < 0 )
{
fatal(2LL, "negative values are not permitted");
func10();
}
else
{
v4 = v3;
v5 = func5(v2);
func8(v5, v4);
}
}

original_code:
static void define_var (char *s)
{
  var_t *var;
  char *p = strchr (s, '=');
  char *name = newstrn (s, p - s);
  intmax_t val;
  p++;
  val = a_to_intmax (p);
  if (val < 0)
    fatal (2, "negative values are not permitted");
  var = find_var (name);
  set_var (var, val);
}

canonical_original_code:
static void define_var(char *s)
{
  struct var_t *var;
  char *p = strchr (s, '=');
  char *name = newstrn (s, p - s);
  long val;
  p++;
  val = a_to_intmax (p);
  if (val < 0)
    fatal (2, "negative values are not permitted");
  var = find_var (name);
  set_var (var, val);
}

variable_types:
p
char *

variable_types: val long


decompiled_code:
int __fastcall print_vars(char a1)
{
__int64 i; // rbp
int result; // eax
__int64 *j; // rbx

for ( i = current_env; i; i = *(_QWORD *)i )
{
for ( j = *(__int64 **)(i + 8); j; j = (__int64 *)*j )
{
if ( *((_BYTE *)j + 16) || a1 )
{
printf("%s at level %d: ", (const char *)j[1], *(unsigned int *)(i + 16));
if ( *((_BYTE *)j + 16) )
printf("%lu", j[5]);
else
printf("uninitialized");
result = putchar(10);
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(char a1)
{
__int64 i; // rbp
int result; // eax
__int64 *j; // rbx

for ( i = current_env; i; i = *(_QWORD *)i )
{
for ( j = *(__int64 **)(i + 8); j; j = (__int64 *)*j )
{
if ( *((_BYTE *)j + 16) || a1 )
{
printf("%s at level %d: ", (const char *)j[1], *(unsigned int *)(i + 16));
if ( *((_BYTE *)j + 16) )
printf("%lu", j[5]);
else
printf("uninitialized");
result = putchar(10);
}
}
}
return result;
}

original_code:
void print_vars (_Bool show_uninitialized)
{
  var_t *var;
  env_t *env;
  for (env = current_env; env; env = env->parent){
  for (var = env->first; var; var = var->next)
    {
      if ((! var->init) && (! show_uninitialized))
 continue;
      printf ("%s at level %d: ", var->name, env->level);
      if (var->init)
 printf ("%" "l" "u", var->val);
      else
 printf ("uninitialized");
      printf ("\n");
    }
  }
}

canonical_original_code:
void print_vars(_Bool show_uninitialized)
{
  struct var_t *var;
  struct env_t *env;
  for (env = current_env; env; env = env->parent){
  for (var = env->first; var; var = var->next)
    {
      if ((! var->init) && (! show_uninitialized))
 continue;
      printf ("%s at level %d: ", var->name, env->level);
      if (var->init)
 printf ("%" "l" "u", var->val);
      else
 printf ("uninitialized");
      printf ("\n");
    }
  }
}


decompiled_code:
void __fastcall check_var_init(__int64 a1)
{
if ( !*(_BYTE *)(a1 + 16) )
error("unitialized variable %s", *(const char **)(a1 + 8));
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1)
{
if ( !*(_BYTE *)(a1 + 16) )
func1("unitialized variable %s", *(const char **)(a1 + 8));
}

original_code:
void check_var_init (var_t *var)
{
  if (! var->init)
    error ("unitialized variable %s", var->name);
}

canonical_original_code:
void check_var_init(struct var_t *var)
{
  if (! var->init)
    error ("unitialized variable %s", var->name);
}


decompiled_code:
int __fastcall print_stmt(__int64 a1)
{
int result; // eax
_QWORD *i; // rbx
__int64 **j; // rbx

switch ( *(_DWORD *)(a1 + 20) )
{
case 0:
result = printf("clear %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 1:
result = printf("incr %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 2:
result = printf("decr %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 3:
result = printf("while %s not 0 do\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
for ( i = *(_QWORD **)(a1 + 40); i; i = (_QWORD *)*i )
result = print_stmt(i);
break;
case 4:
result = printf(
"copy %s to %s\n",
*(const char **)(*(_QWORD *)(a1 + 24) + 8LL),
*(const char **)(*(_QWORD *)(a1 + 32) + 8LL));
break;
case 5:
result = printf("print %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 6:
result = printf("defproc %s\n", *(const char **)(a1 + 48));
break;
case 7:
printf("runproc %s (", *(const char **)(a1 + 48));
for ( j = *(__int64 ***)(a1 + 24); j; j = (__int64 **)*j )
printf(" %s", (const char *)j[1]);
result = puts(")");
break;
default:
result = puts("other");
break;
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
int result; // eax
_QWORD *i; // rbx
__int64 **j; // rbx

switch ( *(_DWORD *)(a1 + 20) )
{
case 0:
result = printf("clear %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 1:
result = printf("incr %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 2:
result = printf("decr %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 3:
result = printf("while %s not 0 do\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
for ( i = *(_QWORD **)(a1 + 40); i; i = (_QWORD *)*i )
result = func12(i);
break;
case 4:
result = printf(
"copy %s to %s\n",
*(const char **)(*(_QWORD *)(a1 + 24) + 8LL),
*(const char **)(*(_QWORD *)(a1 + 32) + 8LL));
break;
case 5:
result = printf("print %s\n", *(const char **)(*(_QWORD *)(a1 + 24) + 8LL));
break;
case 6:
result = printf("defproc %s\n", *(const char **)(a1 + 48));
break;
case 7:
printf("runproc %s (", *(const char **)(a1 + 48));
for ( j = *(__int64 ***)(a1 + 24); j; j = (__int64 **)*j )
printf(" %s", (const char *)j[1]);
result = puts(")");
break;
default:
result = puts("other");
break;
}
return result;
}

original_code:
void print_stmt (stmt_t *stmt){
 stmt_t *tmp;
 var_t *var;
switch (stmt->type)
    {
  case CLEAR_STMT:
  printf("clear %s\n", stmt->var->name);
  break;
  case INCR_STMT:
  printf("incr %s\n", stmt->var->name);
  break;
  case DECR_STMT:
  printf("decr %s\n", stmt->var->name);
  break;
  case WHILE_STMT:
  printf("while %s not 0 do\n", stmt->var->name);
  tmp = stmt->stmt_list;
  while (tmp){
   print_stmt(tmp);
   tmp = tmp->next;
  }
  break;
  case COPY_STMT:
  printf("copy %s to %s\n", stmt->var->name, stmt->dest->name);
  break;
  case PRINT_STMT:
  printf("print %s\n", stmt->var->name);
  break;
  case DEFPROC_STMT:
  printf("defproc %s\n", stmt->name);
  break;
  case RUNPROC_STMT:
  printf("runproc %s (", stmt->name);
  var = stmt->var;
  while (var){
   printf(" %s",var->name);
   var = var->next;
  }
  printf(")\n");
  break;
  default:
  printf("other\n");
  break;
 }
}

canonical_original_code:
void print_stmt(struct stmt_t *stmt){
 struct stmt_t *tmp;
 struct var_t *var;
switch (stmt->type)
    {
  case CLEAR_STMT:
  printf("clear %s\n", stmt->var->name);
  break;
  case INCR_STMT:
  printf("incr %s\n", stmt->var->name);
  break;
  case DECR_STMT:
  printf("decr %s\n", stmt->var->name);
  break;
  case WHILE_STMT:
  printf("while %s not 0 do\n", stmt->var->name);
  tmp = stmt->stmt_list;
  while (tmp){
   print_stmt(tmp);
   tmp = tmp->next;
  }
  break;
  case COPY_STMT:
  printf("copy %s to %s\n", stmt->var->name, stmt->dest->name);
  break;
  case PRINT_STMT:
  printf("print %s\n", stmt->var->name);
  break;
  case DEFPROC_STMT:
  printf("defproc %s\n", stmt->name);
  break;
  case RUNPROC_STMT:
  printf("runproc %s (", stmt->name);
  var = stmt->var;
  while (var){
   printf(" %s",var->name);
   var = var->next;
  }
  printf(")\n");
  break;
  default:
  printf("other\n");
  break;
 }
}


decompiled_code:
__int64 __fastcall new_stmt(int a1, __int64 a2)
{
__int64 result; // rax

result = alloc(64LL);
*(_DWORD *)(result + 20) = a1;
*(_QWORD *)(result + 24) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, __int64 a2)
{
__int64 result; // rax

result = alloc(64LL);
*(_DWORD *)(result + 20) = a1;
*(_QWORD *)(result + 24) = a2;
return result;
}

original_code:
stmt_t *new_stmt (stmt_type_t type, var_t *var)
{
  stmt_t *stmt = alloc (sizeof (stmt_t));
  stmt->type = type;
  stmt->var = var;
  return stmt;
}

canonical_original_code:
struct stmt_t *new_stmt(enum stmt_type_t type, struct var_t *var)
{
  struct stmt_t *stmt = alloc (sizeof (stmt_t));
  stmt->type = type;
  stmt->var = var;
  return stmt;
}

variable_types:
var
struct var_t *

return_type: struct stmt_t { struct stmt_t *next; struct stmt_t *tail; int line; enum stmt_type_t { CLEAR_STMT=0, INCR_STMT=1, DECR_STMT=2, WHILE_STMT=3, COPY_STMT=4, PRINT_STMT=5, DEFPROC_STMT=6, RUNPROC_STMT=7, EXIT_STMT=8, LAMBDA_STMT=9, ADD_CLEAR_STMT=10 } type; struct var_t { struct var_t *next; char *name; _Bool init; struct stmt_t *fonc; struct env_t { struct env_t *parent; struct var_t *first; int level; } *env_l; unsigned long val; } *var; struct var_t *dest; struct stmt_t *stmt_list; char *name; struct stmt_t *proc; } *


original_code:
int execute_stmt (stmt_t *stmt)
{
  int tmp;
  env_t *next_env;
  stmt_t *proc;
  var_t *var1;
  var_t *var2;
  var_t *modifvar;
  stmt_line = stmt->line;
  if (stmt->type != RUNPROC_STMT && stmt->type != DEFPROC_STMT && stmt->var){
   stmt->var = find_var(stmt->var->name);
  }
  switch (stmt->type)
    {
    case CLEAR_STMT:
      stmt->var->val = 0;
      stmt->var->init = 1;
      break;
    case INCR_STMT:
      check_var_init (stmt->var);
      if (stmt->var->val == (18446744073709551615UL))
 fatal (2, "overflow");
      stmt->var->val++;
      break;
    case DECR_STMT:
      check_var_init (stmt->var);
      if (stmt->var->val)
 stmt->var->val--;
      break;
    case WHILE_STMT:
      check_var_init (stmt->var);
      while (stmt->var->val){
  tmp = execute_stmt_list (stmt->stmt_list);
  if (tmp == 1){
   return 1;
  }
   }
      break;
    case COPY_STMT:
      check_var_init (stmt->var);
      stmt->dest->val = stmt->var->val;
      stmt->dest->fonc = stmt->var->fonc;
      stmt->dest->env_l = stmt->var->env_l;
      stmt->dest->init = 1;
      break;
    case ADD_CLEAR_STMT:
      check_var_init (stmt->var);
      check_var_init (stmt->dest);
      stmt->dest->val += stmt->var->val;
      stmt->var->val = 0;
      break;
    case PRINT_STMT:
      check_var_init (stmt->var);
      if (stmt->var->fonc){
    printf("%s : ", stmt->var->name);
    proc = stmt->var->fonc->stmt_list;
    while (proc){
    print_stmt(proc);
    proc = proc->next;
    }
    break;
      }
      printf("%s : %d ", stmt->var->name, (int)stmt->var->val);
      if (verbose){
  printf("at level %d\n", current_env->level);
   } else {
    printf("\n");
   }
      break;
    case DEFPROC_STMT:
      stmt->proc = proc_head;
      proc_head = stmt;
      printf("New Subroutine : %s\n", proc_head->name);
      var1 = stmt->var;
      if (verbose)
  while (var1){
   printf("\tArgument needed : %s\n", var1->name);
   var1 = var1->next;
  }
      break;
    case LAMBDA_STMT:
        var1 = stmt->var;
        var1->env_l = current_env;
        proc = new_stmt(DEFPROC_STMT, stmt->dest);
        var1->fonc = proc;
        proc->var = stmt->dest;
        proc->stmt_list = stmt->stmt_list;
        break;
    case RUNPROC_STMT:
      proc = find_proc(stmt->name);
      next_env = create_env(current_env);
      if (!proc){
    var1 = find_var(stmt->name);
    proc = var1->fonc;
    if (!proc){
      fatal(2, "subroutine doesn't exist");
    }
    merge_env(next_env, var1->env_l);
   }
      printf("Running subroutine : %s\n", stmt->name);
      if (proc) {
  var1 = proc->var;
  var2 = stmt->var;
  while (var1 != ((void *)0)){
   if (verbose)
    printf("\targ %s",var1->name);
   if (var2 != ((void *)0)) {
   modifvar = copy_find_var(var1->name);
   if (var2->val == 0){
    modifvar->val = find_var(var2->name)->val;
   } else {
    modifvar->val = var2->val;
   }
   if (verbose)
    printf(" => %d \n", (int) modifvar->val);
   modifvar->next = next_env->first;
   next_env->first = modifvar;
   var1 = var1->next;
   var2 = var2->next;
  } else {
    fatal(2, "not enough arguments");
  }
 }
 current_env = next_env;
 execute_stmt_list (proc->stmt_list);
   current_env = current_env->parent;
      } else {
 fatal(2, "subroutine doesn't exist");
      }
      break;
    case EXIT_STMT:
      return 1;
      break;
    }
    return 0;
}

canonical_original_code:
int execute_stmt(struct stmt_t *stmt)
{
  int tmp;
  struct env_t *next_env;
  struct stmt_t *proc;
  struct var_t *var1;
  struct var_t *var2;
  struct var_t *modifvar;
  stmt_line = stmt->line;
  if (stmt->type != RUNPROC_STMT && stmt->type != DEFPROC_STMT && stmt->var){
   stmt->var = find_var(stmt->var->name);
  }
  switch (stmt->type)
    {
    case CLEAR_STMT:
      stmt->var->val = 0;
      stmt->var->init = 1;
      break;
    case INCR_STMT:
      check_var_init (stmt->var);
      if (stmt->var->val == (18446744073709551615UL))
 fatal (2, "overflow");
      stmt->var->val++;
      break;
    case DECR_STMT:
      check_var_init (stmt->var);
      if (stmt->var->val)
 stmt->var->val--;
      break;
    case WHILE_STMT:
      check_var_init (stmt->var);
      while (stmt->var->val){
  tmp = execute_stmt_list (stmt->stmt_list);
  if (tmp == 1){
   return 1;
  }
   }
      break;
    case COPY_STMT:
      check_var_init (stmt->var);
      stmt->dest->val = stmt->var->val;
      stmt->dest->fonc = stmt->var->fonc;
      stmt->dest->env_l = stmt->var->env_l;
      stmt->dest->init = 1;
      break;
    case ADD_CLEAR_STMT:
      check_var_init (stmt->var);
      check_var_init (stmt->dest);
      stmt->dest->val += stmt->var->val;
      stmt->var->val = 0;
      break;
    case PRINT_STMT:
      check_var_init (stmt->var);
      if (stmt->var->fonc){
    printf("%s : ", stmt->var->name);
    proc = stmt->var->fonc->stmt_list;
    while (proc){
    print_stmt(proc);
    proc = proc->next;
    }
    break;
      }
      printf("%s : %d ", stmt->var->name, (int)stmt->var->val);
      if (verbose){
  printf("at level %d\n", current_env->level);
   } else {
    printf("\n");
   }
      break;
    case DEFPROC_STMT:
      stmt->proc = proc_head;
      proc_head = stmt;
      printf("New Subroutine : %s\n", proc_head->name);
      var1 = stmt->var;
      if (verbose)
  while (var1){
   printf("\tArgument needed : %s\n", var1->name);
   var1 = var1->next;
  }
      break;
    case LAMBDA_STMT:
        var1 = stmt->var;
        var1->env_l = current_env;
        proc = new_stmt(DEFPROC_STMT, stmt->dest);
        var1->fonc = proc;
        proc->var = stmt->dest;
        proc->stmt_list = stmt->stmt_list;
        break;
    case RUNPROC_STMT:
      proc = find_proc(stmt->name);
      next_env = create_env(current_env);
      if (!proc){
    var1 = find_var(stmt->name);
    proc = var1->fonc;
    if (!proc){
      fatal(2, "subroutine doesn't exist");
    }
    merge_env(next_env, var1->env_l);
   }
      printf("Running subroutine : %s\n", stmt->name);
      if (proc) {
  var1 = proc->var;
  var2 = stmt->var;
  while (var1 != ((void *)0)){
   if (verbose)
    printf("\targ %s",var1->name);
   if (var2 != ((void *)0)) {
   modifvar = copy_find_var(var1->name);
   if (var2->val == 0){
    modifvar->val = find_var(var2->name)->val;
   } else {
    modifvar->val = var2->val;
   }
   if (verbose)
    printf(" => %d \n", (int) modifvar->val);
   modifvar->next = next_env->first;
   next_env->first = modifvar;
   var1 = var1->next;
   var2 = var2->next;
  } else {
    fatal(2, "not enough arguments");
  }
 }
 current_env = next_env;
 execute_stmt_list (proc->stmt_list);
   current_env = current_env->parent;
      } else {
 fatal(2, "subroutine doesn't exist");
      }
      break;
    case EXIT_STMT:
      return 1;
      break;
    }
    return 0;
}

variable_types: tmp int


decompiled_code:
__int64 __fastcall execute_stmt_list(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
result = execute_stmt((__int64)v1);
if ( (_DWORD)result == 1 )
break;
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
result = func15((__int64)v1);
if ( (_DWORD)result == 1 )
break;
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
return result;
}

original_code:
int execute_stmt_list (stmt_t *list)
{
  int tmp;
  while (list)
    {
      tmp = execute_stmt (list);
      if (tmp == 1){
    return 1;
   }
      list = list->next;
    }
  return 0;
}

canonical_original_code:
int execute_stmt_list(struct stmt_t *list)
{
  int tmp;
  while (list)
    {
      tmp = execute_stmt (list);
      if (tmp == 1){
    return 1;
   }
      list = list->next;
    }
  return 0;
}


decompiled_code:
char __fastcall parse_prog(const char *a1)
{
int v1; // ebx
char result; // al

yyin = fopen(a1, "r");
if ( yyin )
{
v1 = yyparse();
fclose(yyin);
result = v1 == 0;
}
else
{
fatal(1LL, "can't read program\n");
result = optimize_stmt_list();
}
return result;
}

canonical_decompiled_code:
char __fastcall func18(const char *a1)
{
int v1; // ebx
char result; // al

yyin = fopen(a1, "r");
if ( yyin )
{
v1 = yyparse();
fclose(yyin);
result = v1 == 0;
}
else
{
fatal(1LL, "can't read program\n");
result = func19();
}
return result;
}

original_code:
_Bool parse_prog (char *fn)
{
  int status;
  yyin = fopen (fn, "r");
  if (! yyin)
    fatal (1, "can't read program\n");
  status = yyparse ();
  fclose (yyin);
  return status == 0;
}

canonical_original_code:
_Bool parse_prog(char *fn)
{
  int status;
  yyin = fopen (fn, "r");
  if (! yyin)
    fatal (1, "can't read program\n");
  status = yyparse ();
  fclose (yyin);
  return status == 0;
}

variable_types:
fn
char *

variable_types: status int


decompiled_code:
__int64 __fastcall optimize_stmt_list(__int64 *a1)
{
__int64 *i; // rbx
__int64 result; // rax

for ( i = a1; i; i = (__int64 *)*i )
{
if ( *((_DWORD *)i + 5) == 3 )
result = optimize_while_stmt(i);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 *a1)
{
__int64 *i; // rbx
__int64 result; // rax

for ( i = a1; i; i = (__int64 *)*i )
{
if ( *((_DWORD *)i + 5) == 3 )
result = func20(i);
}
return result;
}

original_code:
void optimize_stmt_list (stmt_t *list)
{
  while (list)
    {
      if (list->type == WHILE_STMT)
 optimize_while_stmt (list);
      list = list->next;
    }
}

canonical_original_code:
void optimize_stmt_list(struct stmt_t *list)
{
  while (list)
    {
      if (list->type == WHILE_STMT)
 optimize_while_stmt (list);
      list = list->next;
    }
}


original_code:
void optimize_while_stmt (stmt_t *stmt)
{
  _Bool match = 0;
  var_t *loop_var = stmt->var;
  stmt_t *first_stmt = stmt->stmt_list;
  stmt_t *second_stmt = first_stmt->next;
  var_t *src;
  var_t *dest;
  if (first_stmt && second_stmt && ! second_stmt->next)
    {
      if ((first_stmt->type == INCR_STMT) &&
   (first_stmt->var != loop_var) &&
   (second_stmt->type == DECR_STMT) &&
   (second_stmt->var == loop_var))
 {
   match = 1;
   src = second_stmt->var;
   dest = first_stmt->var;
 }
      else if ((second_stmt->type == INCR_STMT) &&
        (second_stmt->var != loop_var) &&
        (first_stmt->type == DECR_STMT) &&
        (first_stmt->var == loop_var))
 {
   match = 1;
   src = first_stmt->var;
   dest = second_stmt->var;
 }
    }
  if (match)
    {
      stmt->type = ADD_CLEAR_STMT;
      stmt->var = src;
      stmt->dest = dest;
    }
  else
    optimize_stmt_list (stmt->stmt_list);
}

canonical_original_code:
void optimize_while_stmt(struct stmt_t *stmt)
{
  _Bool match = 0;
  struct var_t *loop_var = stmt->var;
  struct stmt_t *first_stmt = stmt->stmt_list;
  struct stmt_t *second_stmt = first_stmt->next;
  struct var_t *src;
  struct var_t *dest;
  if (first_stmt && second_stmt && ! second_stmt->next)
    {
      if ((first_stmt->type == INCR_STMT) &&
   (first_stmt->var != loop_var) &&
   (second_stmt->type == DECR_STMT) &&
   (second_stmt->var == loop_var))
 {
   match = 1;
   src = second_stmt->var;
   dest = first_stmt->var;
 }
      else if ((second_stmt->type == INCR_STMT) &&
        (second_stmt->var != loop_var) &&
        (first_stmt->type == DECR_STMT) &&
        (first_stmt->var == loop_var))
 {
   match = 1;
   src = first_stmt->var;
   dest = second_stmt->var;
 }
    }
  if (match)
    {
      stmt->type = ADD_CLEAR_STMT;
      stmt->var = src;
      stmt->dest = dest;
    }
  else
    optimize_stmt_list (stmt->stmt_list);
}


main

canonical_name: main

decompiled_code:
// write access to const memory has been detected, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
char v4; // r14
const char *v5; // rbp

progname = *argv;
if ( argc == 1 )
{
if ( main_prog )
goto LABEL_21;
goto LABEL_27;
}
v3 = argv;
v4 = 0;
while ( 1 )
{
v5 = *++v3;
if ( **v3 == 45 )
{
if ( !strcmp(*v3, "-u") )
{
init_to_zero = 0;
goto LABEL_11;
}
if ( !strcmp(*v3, "-O") )
goto LABEL_17;
if ( !strcmp(*v3, "-v") )
{
verbose = 1;
goto LABEL_11;
}
fatal(1LL, "unrecognized option '%s'", *v3);
}
else if ( strchr(*v3, 61) )
{
define_var(v5);
goto LABEL_11;
}
if ( main_prog )
{
fatal(1LL, "only one program may be specified");
LABEL_16:
fatal(2LL, "parse failed");
LABEL_17:
v4 = 1;
goto LABEL_11;
}
if ( !parse_prog(v5) )
goto LABEL_16;
LABEL_11:
if ( v3 == &argv[argc - 1] )
{
if ( main_prog )
{
if ( v4 )
optimize_stmt_list((__int64 *)main_prog);
LABEL_21:
if ( verbose )
{
puts("initial values of variables:");
print_vars(0);
}
puts("\nbegin main_prog:\n");
execute_stmt_list((_QWORD *)main_prog);
puts("\nend of main_prog:\n");
if ( verbose )
{
puts("final values of variables:");
print_vars(1);
}
exit(0);
}
LABEL_27:
fatal(1LL, "no program found", envp);
JUMPOUT(0xD0DLL);
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
char v4; // r14
const char *v5; // rbp

progname = *argv;
if ( argc == 1 )
{
if ( main_prog )
goto LABEL_21;
goto LABEL_27;
}
v3 = argv;
v4 = 0;
while ( 1 )
{
v5 = *++v3;
if ( **v3 == 45 )
{
if ( !strcmp(*v3, "-u") )
{
init_to_zero = 0;
goto LABEL_11;
}
if ( !strcmp(*v3, "-O") )
goto LABEL_17;
if ( !strcmp(*v3, "-v") )
{
verbose = 1;
goto LABEL_11;
}
fatal(1LL, "unrecognized option '%s'", *v3);
}
else if ( strchr(*v3, 61) )
{
func9(v5);
goto LABEL_11;
}
if ( main_prog )
{
fatal(1LL, "only one program may be specified");
LABEL_16:
fatal(2LL, "parse failed");
LABEL_17:
v4 = 1;
goto LABEL_11;
}
if ( !func18(v5) )
goto LABEL_16;
LABEL_11:
if ( v3 == &argv[argc - 1] )
{
if ( main_prog )
{
if ( v4 )
func19((__int64 *)main_prog);
LABEL_21:
if ( verbose )
{
puts("initial values of variables:");
func10(0);
}
puts("\nbegin main_prog:\n");
func16((_QWORD *)main_prog);
puts("\nend of main_prog:\n");
if ( verbose )
{
puts("final values of variables:");
func10(1);
}
exit(0);
}
LABEL_27:
fatal(1LL, "no program found", envp);
JUMPOUT(0xD0DLL);
}
}
}

original_code:
int main (int argc, char *argv [])
{
  _Bool opt_flag = 0;
  progname = argv [0];
  while (--argc)
    {
      argv++;
      if (argv [0][0] == '-')
 {
   if (strcmp (argv [0], "-u") == 0)
     init_to_zero = 0;
   else if (strcmp (argv [0], "-O") == 0)
     opt_flag = 1;
   else if (strcmp (argv [0], "-v") == 0)
     verbose = 1;
   else
     fatal (1, "unrecognized option '%s'", argv [0]);
 }
      else if (strchr (argv [0], '='))
 define_var (argv [0]);
      else if (main_prog)
 fatal (1, "only one program may be specified");
      else if (! parse_prog (argv [0]))
 fatal (2, "parse failed");
      else
 {
   ;
 }
    }
  if (! main_prog)
    fatal (1, "no program found");
  if (opt_flag)
    optimize_stmt_list (main_prog);
  if (verbose){
  printf ("initial values of variables:\n");
  print_vars (0);
  }
  printf ("\nbegin main_prog:\n\n");
  execute_stmt_list (main_prog);
  printf ("\nend of main_prog:\n\n");
  if (verbose){
  printf ("final values of variables:\n");
  print_vars (1);
  }
  exit (0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
  _Bool opt_flag = 0;
  progname = argv [0];
  while (--argc)
    {
      argv++;
      if (argv [0][0] == '-')
 {
   if (strcmp (argv [0], "-u") == 0)
     init_to_zero = 0;
   else if (strcmp (argv [0], "-O") == 0)
     opt_flag = 1;
   else if (strcmp (argv [0], "-v") == 0)
     verbose = 1;
   else
     fatal (1, "unrecognized option '%s'", argv [0]);
 }
      else if (strchr (argv [0], '='))
 define_var (argv [0]);
      else if (main_prog)
 fatal (1, "only one program may be specified");
      else if (! parse_prog (argv [0]))
 fatal (2, "parse failed");
      else
 {
   ;
 }
    }
  if (! main_prog)
    fatal (1, "no program found");
  if (opt_flag)
    optimize_stmt_list (main_prog);
  if (verbose){
  printf ("initial values of variables:\n");
  print_vars (0);
  }
  printf ("\nbegin main_prog:\n\n");
  execute_stmt_list (main_prog);
  printf ("\nend of main_prog:\n\n");
  if (verbose){
  printf ("final values of variables:\n");
  print_vars (1);
  }
  exit (0);
}

variable_types: argc int

variable_types:
argv
char *[]


fatal

decompiled_code:
void __noreturn fatal(int status, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( format )
{
fwrite("fatal error: ", 1uLL, 0xDuLL, stderr);
va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
}
if ( status == 1 )
{
fwrite("usage:\n", 1uLL, 7uLL, stderr);
usage(stderr);
}
exit(status);
}

canonical_decompiled_code:
void __noreturn func0(int status, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( format )
{
fwrite("fatal error: ", 1uLL, 0xDuLL, stderr);
va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
}
if ( status == 1 )
{
fwrite("usage:\n", 1uLL, 7uLL, stderr);
usage(stderr);
}
exit(status);
}

original_code:
void fatal (int ret, char *format, ...)
{
  va_list ap;
  if (format)
    {
      fprintf (stderr, "fatal error: ");
      __builtin_va_start(ap,format);
      vfprintf (stderr, format, ap);
      __builtin_va_end(ap);
      fprintf (stderr, "\n");
    }
  if (ret == 1)
    {
      fprintf (stderr, "usage:\n");
      usage (stderr);
    }
  exit (ret);
}

canonical_original_code:
void fatal(int ret, char *format, ...)
{
  __builtin_va_list ap;
  if (format)
    {
      fprintf (stderr, "fatal error: ");
      __builtin_va_start(ap,format);
      vfprintf (stderr, format, ap);
      __builtin_va_end(ap);
      fprintf (stderr, "\n");
    }
  if (ret == 1)
    {
      fprintf (stderr, "usage:\n");
      usage (stderr);
    }
  exit (ret);
}

variable_types: ret int

variable_types:
format
char *


alloc

decompiled_code:
void *__fastcall alloc(size_t size)
{
void *result; // rax

result = calloc(1uLL, size);
if ( !result )
fatal(2, "Memory allocation failed\n");
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t size)
{
void *result; // rax

result = calloc(1uLL, size);
if ( !result )
func0(2, "Memory allocation failed\n");
return result;
}

original_code:
void *alloc (size_t size)
{
  void *p;
  p = calloc (1, size);
  if (! p)
    fatal (2, "Memory allocation failed\n");
  return (p);
}

canonical_original_code:
void *alloc(unsigned long size)
{
  void *p;
  p = calloc (1, size);
  if (! p)
    fatal (2, "Memory allocation failed\n");
  return (p);
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


newstr

decompiled_code:
void *__fastcall newstr(void *src)
{
size_t v1; // rbx
void *v2; // rax

v1 = (int)(strlen((const char *)src) + 1);
v2 = alloc(v1);
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func2(void *src)
{
size_t v1; // rbx
void *v2; // rax

v1 = (int)(strlen((const char *)src) + 1);
v2 = func1(v1);
return memcpy(v2, src, v1);
}

original_code:
char *newstr (char *orig)
{
  int len;
  char *r;
  len = strlen (orig);
  r = (char *) alloc (len + 1);
  memcpy (r, orig, len + 1);
  return (r);
}

canonical_original_code:
char *newstr(char *orig)
{
  int len;
  char *r;
  len = strlen (orig);
  r = (char *) alloc (len + 1);
  memcpy (r, orig, len + 1);
  return (r);
}

variable_types:
orig
char *

variable_types: len int

variable_types:
r
char *

return_type: char *


newstrn

decompiled_code:
void *__fastcall newstrn(void *src, int a2)
{
int v2; // ebx
int v3; // eax
void *v4; // rax

v2 = a2;
v3 = strlen((const char *)src);
if ( v3 <= a2 )
v2 = v3;
v4 = alloc(v2 + 1);
return memcpy(v4, src, v2);
}

canonical_decompiled_code:
void *__fastcall func3(void *src, int a2)
{
int v2; // ebx
int v3; // eax
void *v4; // rax

v2 = a2;
v3 = strlen((const char *)src);
if ( v3 <= a2 )
v2 = v3;
v4 = func1(v2 + 1);
return memcpy(v4, src, v2);
}

original_code:
char *newstrn (char *orig, int max_len)
{
  int len;
  char *r;
  len = strlen (orig);
  if (len > max_len)
    len = max_len;
  r = (char *) alloc (len + 1);
  memcpy (r, orig, len);
  return (r);
}

canonical_original_code:
char *newstrn(char *orig, int max_len)
{
  int len;
  char *r;
  len = strlen (orig);
  if (len > max_len)
    len = max_len;
  r = (char *) alloc (len + 1);
  memcpy (r, orig, len);
  return (r);
}


keyword

decompiled_code:
__int64 __fastcall keyword(char *s1, const char **a2)
{
const char **v2; // rbx
const char *v3; // rsi

v2 = a2;
v3 = *a2;
if ( !v3 )
return 0LL;
while ( strcasecmp(s1, v3) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return *((unsigned int *)v2 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s1, const char **a2)
{
const char **v2; // rbx
const char *v3; // rsi

v2 = a2;
v3 = *a2;
if ( !v3 )
return 0LL;
while ( strcasecmp(s1, v3) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return *((unsigned int *)v2 + 2);
}

original_code:
int keyword (char *string, keyword_t *table)
{
  while (table->name)
    {
      if (strcasecmp (string, table->name) == 0)
 return table->value;
      table++;
    }
  return 0;
}

canonical_original_code:
int keyword(char *string, struct keyword *table)
{
  while (table->name)
    {
      if (strcasecmp (string, table->name) == 0)
 return table->value;
      table++;
    }
  return 0;
}

variable_types:
string
char *

variable_types: struct keyword { char *name; int value; } *


decompiled_code:
__int64 __fastcall a_to_intmax(const char *a1)
{
size_t v1; // rax
int v3; // [rsp+4h] [rbp-14h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%li%n", &v4, &v3) != 1 || (v1 = strlen(a1), v3 != v1) )
fatal(2, "can't interpret '%s' as an integer", a1);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
size_t v1; // rax
int v3; // [rsp+4h] [rbp-14h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%li%n", &v4, &v3) != 1 || (v1 = strlen(a1), v3 != v1) )
func0(2, "can't interpret '%s' as an integer", a1);
return v4;
}

original_code:
intmax_t a_to_intmax (char *s)
{
  intmax_t val;
  int c;
  int i = sscanf (s, "%" "l" "i" "%n", & val, & c);
  if ((i != 1) || ((unsigned int)c != strlen (s)))
    fatal (2, "can't interpret '%s' as an integer", s);
  return val;
}

canonical_original_code:
long a_to_intmax(char *s)
{
  long val;
  int c;
  int i = sscanf (s, "%" "l" "i" "%n", & val, & c);
  if ((i != 1) || ((unsigned int)c != strlen (s)))
    fatal (2, "can't interpret '%s' as an integer", s);
  return val;
}

variable_types:
s
char *

variable_types: c int

variable_types: i int

return_type: long


main

repo: mknods/satellite

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printf("START HODOYOSHI_PI (DEVICE_ID=0x%x)\n", 32LL);
cmd_main();
return 0;
}

original_code:
int main(int argc, const char * argv[])
{
    printf("START HODOYOSHI_PI (DEVICE_ID=0x%x)\n",(0x20));
    cmd_main();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    printf("START HODOYOSHI_PI (DEVICE_ID=0x%x)\n",(0x20));
    cmd_main();
    return 0;
}


help

repo: mkreider/ftm

decompiled_code:
size_t help()
{
fprintf(stderr, "Usage: %s [OPTION] <filename>\n", (const char *)program);
fputc(10, stderr);
fwrite(" -w <width> width of values in bytes [1/2/4/8/16] (4)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -p <package> name of the output package (filename)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -s <size> pad the output up to size bytes (filesize)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -b big-endian operation (*)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -l little-endian operation \n", 1uLL, 0x42uLL, stderr);
fwrite(" -v verbose operation\n", 1uLL, 0x23uLL, stderr);
fwrite(" -h display this help and exit\n", 1uLL, 0x2CuLL, stderr);
fputc(10, stderr);
return fwrite("Report Etherbone bugs to <white-rabbit-dev@ohwr.org>\n", 1uLL, 0x35uLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
fprintf(stderr, "Usage: %s [OPTION] <filename>\n", (const char *)program);
fputc(10, stderr);
fwrite(" -w <width> width of values in bytes [1/2/4/8/16] (4)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -p <package> name of the output package (filename)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -s <size> pad the output up to size bytes (filesize)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -b big-endian operation (*)\n", 1uLL, 0x42uLL, stderr);
fwrite(" -l little-endian operation \n", 1uLL, 0x42uLL, stderr);
fwrite(" -v verbose operation\n", 1uLL, 0x23uLL, stderr);
fwrite(" -h display this help and exit\n", 1uLL, 0x2CuLL, stderr);
fputc(10, stderr);
return fwrite("Report Etherbone bugs to <white-rabbit-dev@ohwr.org>\n", 1uLL, 0x35uLL, stderr);
}

original_code:
void help() {
  fprintf(stderr, "Usage: %s [OPTION] <filename>\n", program);
  fprintf(stderr, "\n");
  fprintf(stderr, "  -w <width>     width of values in bytes [1/2/4/8/16]        (4)\n");
  fprintf(stderr, "  -p <package>   name of the output package            (filename)\n");
  fprintf(stderr, "  -s <size>      pad the output up to size bytes       (filesize)\n");
  fprintf(stderr, "  -b             big-endian operation                         (*)\n");
  fprintf(stderr, "  -l             little-endian operation                         \n");
  fprintf(stderr, "  -v             verbose operation\n");
  fprintf(stderr, "  -h             display this help and exit\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "Report Etherbone bugs to <white-rabbit-dev@ohwr.org>\n");
}


printk

repo: mlichvar/linux-tktest

decompiled_code:
int printk(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

original_code:
void printk(const char *format, ...) {
 va_list ap;
 __builtin_va_start(ap,format);
 vfprintf(stdout, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void printk(char *format, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,format);
 vfprintf(stdout, format, ap);
 __builtin_va_end(ap);
}


decompiled_code:
int printk_deferred(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

original_code:
void printk_deferred(const char *format, ...) {
 va_list ap;
 __builtin_va_start(ap,format);
 vfprintf(stdout, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void printk_deferred(char *format, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,format);
 vfprintf(stdout, format, ap);
 __builtin_va_end(ap);
}


decompiled_code:
__int64 get_random_u32()
{
return random();
}

canonical_decompiled_code:
__int64 func2()
{
return random();
}

original_code:
unsigned int get_random_u32(void)
{
 return random();
}

canonical_original_code:
unsigned int get_random_u32()
{
 return random();
}