decompiled_code:
__int64 __fastcall poll_process(int fd)
{
char *v1; // rax
int v2; // er13
int v3; // ebp
int v4; // eax
int v5; // ebx
char *v6; // rbx
char *v7; // r15
unsigned int v8; // ebp
int v9; // ebp
__int64 v10; // rax
unsigned int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-204Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-2048h] BYREF
struct pollfd fds; // [rsp+20h] [rbp-2038h] BYREF
char v16[8184]; // [rsp+28h] [rbp-2030h] BYREF
char v17; // [rsp+2020h] [rbp-38h] BYREF

fds.fd = fd;
v1 = v16;
fds.events = 1;
do
{
*(_DWORD *)v1 = -1;
v1 += 8;
}
while ( v1 != &v17 );
addr_len = 16;
v2 = 0;
v3 = 1;
while ( 1 )
{
v4 = poll(&fds, v3, 1000);
v5 = v4;
if ( v4 < 0 )
break;
if ( v4 )
{
if ( (fds.revents & 1) == 0 )
goto LABEL_7;
v9 = accept(fd, &addr, &addr_len);
if ( v9 == -1 )
perror("accept error");
v10 = 1LL;
while ( *(&fds.fd + 2 * v10) >= 0 )
{
if ( ++v10 == 1024 )
{
perror("too many clients!\n");
return 0LL;
}
}
if ( v2 < (int)v10 )
v2 = v10;
*(&fds.fd + 2 * (int)v10) = v9;
*(&fds.events + 4 * (int)v10) = 1;
v3 = v2 + 1;
if ( v5 != 1 )
{
LABEL_7:
if ( v2 > 1 )
{
v6 = v16;
v7 = &v16[8 * (v2 - 2) + 8];
do
{
while ( 1 )
{
v8 = *(_DWORD *)v6;
if ( *(int *)v6 >= 0 && (v6[6] & 1) != 0 )
break;
v6 += 8;
if ( v6 == v7 )
goto LABEL_14;
}
if ( !fork() )
{
close(fd);
v11 = getpid();
printf("process %d forked\n", v11);
handleRequest(v8);
exit(0);
}
v6 += 8;
close(v8);
waitpid(-1, 0LL, 1);
}
while ( v6 != v7 );
}
LABEL_14:
v3 = v2 + 1;
}
}
else
{
perror("time out\n");
}
}
perror("poll error!\n");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
char *v1; // rax
int v2; // er13
int v3; // ebp
int v4; // eax
int v5; // ebx
char *v6; // rbx
char *v7; // r15
unsigned int v8; // ebp
int v9; // ebp
__int64 v10; // rax
unsigned int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-204Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-2048h] BYREF
struct pollfd fds; // [rsp+20h] [rbp-2038h] BYREF
char v16[8184]; // [rsp+28h] [rbp-2030h] BYREF
char v17; // [rsp+2020h] [rbp-38h] BYREF

fds.fd = fd;
v1 = v16;
fds.events = 1;
do
{
*(_DWORD *)v1 = -1;
v1 += 8;
}
while ( v1 != &v17 );
addr_len = 16;
v2 = 0;
v3 = 1;
while ( 1 )
{
v4 = poll(&fds, v3, 1000);
v5 = v4;
if ( v4 < 0 )
break;
if ( v4 )
{
if ( (fds.revents & 1) == 0 )
goto LABEL_7;
v9 = accept(fd, &addr, &addr_len);
if ( v9 == -1 )
perror("accept error");
v10 = 1LL;
while ( *(&fds.fd + 2 * v10) >= 0 )
{
if ( ++v10 == 1024 )
{
perror("too many clients!\n");
return 0LL;
}
}
if ( v2 < (int)v10 )
v2 = v10;
*(&fds.fd + 2 * (int)v10) = v9;
*(&fds.events + 4 * (int)v10) = 1;
v3 = v2 + 1;
if ( v5 != 1 )
{
LABEL_7:
if ( v2 > 1 )
{
v6 = v16;
v7 = &v16[8 * (v2 - 2) + 8];
do
{
while ( 1 )
{
v8 = *(_DWORD *)v6;
if ( *(int *)v6 >= 0 && (v6[6] & 1) != 0 )
break;
v6 += 8;
if ( v6 == v7 )
goto LABEL_14;
}
if ( !fork() )
{
close(fd);
v11 = getpid();
printf("process %d forked\n", v11);
handleRequest(v8);
exit(0);
}
v6 += 8;
close(v8);
waitpid(-1, 0LL, 1);
}
while ( v6 != v7 );
}
LABEL_14:
v3 = v2 + 1;
}
}
else
{
perror("time out\n");
}
}
perror("poll error!\n");
return 0LL;
}

original_code:
int poll_process(int fd)
{
 int i;
 int max;
 int len;
 int conn;
 int flag;
 int sockfd;
 int nready;
    pid_t pid;
 struct pollfd client[1024];
 struct sockaddr_in client_addr;
 ssize_t n;
 struct pool *m_pool;
 client[0].fd = fd;
 client[0].events = 0x001;
 for(i = 1; i < 1024; ++i){
  client[i].fd = -1;
 }
 max = 0;
 len = sizeof(struct sockaddr);
 while(1){
  nready = poll(client, max+1, 1000);
        if (nready < 0) {
            perror("poll error!\n");
            break;
        }
        else if (nready == 0) {
            perror("time out\n");
            continue;
        }
  if(client[0].revents & 0x001){
   conn = accept(fd, (struct sockaddr*)&client_addr, (unsigned int*)&len);
            if (conn == -1) {
                perror("accept error");
            }
   for(i = 1; i < 1024; ++i){
    if(client[i].fd < 0){
     client[i].fd = conn;
     break;
    }
   }
   if(i == 1024){
    perror("too many clients!\n");
    break;
   }
   client[i].events = 0x001;
   if(i > max)
    max = i;
   if(--nready <= 0)
    continue;
  }
  for(i = 1; i < max; ++i){
   if((sockfd = client[i].fd) < 0)
    continue;
   if(client[i].revents & 0x001){
          pid = fork();
          if(pid == 0){
           close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(sockfd);
           exit(0);
          }
          close(sockfd);
          waitpid(-1, ((void *)0), 1);
       if(--nready <= 0)
        continue;
   }
  }
 }
 return 0;
}

canonical_original_code:
int poll_process(int fd)
{
 int i;
 int max;
 int len;
 int conn;
 int flag;
 int sockfd;
 int nready;
    int pid;
 struct pollfd client[1024];
 struct sockaddr_in client_addr;
 long n;
 struct pool *m_pool;
 client[0].fd = fd;
 client[0].events = 0x001;
 for(i = 1; i < 1024; ++i){
  client[i].fd = -1;
 }
 max = 0;
 len = sizeof(struct sockaddr);
 while(1){
  nready = poll(client, max+1, 1000);
        if (nready < 0) {
            perror("poll error!\n");
            break;
        }
        else if (nready == 0) {
            perror("time out\n");
            continue;
        }
  if(client[0].revents & 0x001){
   conn = accept(fd, (struct sockaddr *)&client_addr, (unsigned int *)&len);
            if (conn == -1) {
                perror("accept error");
            }
   for(i = 1; i < 1024; ++i){
    if(client[i].fd < 0){
     client[i].fd = conn;
     break;
    }
   }
   if(i == 1024){
    perror("too many clients!\n");
    break;
   }
   client[i].events = 0x001;
   if(i > max)
    max = i;
   if(--nready <= 0)
    continue;
  }
  for(i = 1; i < max; ++i){
   if((sockfd = client[i].fd) < 0)
    continue;
   if(client[i].revents & 0x001){
          pid = fork();
          if(pid == 0){
           close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(sockfd);
           exit(0);
          }
          close(sockfd);
          waitpid(-1, ((void *)0), 1);
       if(--nready <= 0)
        continue;
   }
  }
 }
 return 0;
}

variable_types: fd int

variable_types: i int

variable_types: max int

variable_types: conn int

variable_types: sockfd int

variable_types: nready int

variable_types: pid int

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

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

variable_types: struct pool { int n; struct pool_block { char *space; char *last; char *end; struct pool_block *next; } *head; } *

user_defined_types: struct pool { int n; struct pool_block *head; }


setnonblocking

decompiled_code:
int __fastcall setnonblocking(int fd)
{
int v1; // eax
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
{
perror("fcntl(sock,GETFL)");
exit(1);
}
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
{
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
int v1; // eax
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
{
perror("fcntl(sock,GETFL)");
exit(1);
}
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
{
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
return result;
}

original_code:
void setnonblocking(int sock)
{
 int opts;
 opts = fcntl(sock, 3);
  if(opts < 0){
  perror("fcntl(sock,GETFL)");
  exit(1);
 }
 opts = opts | 04000;
 if(fcntl(sock, 4, opts)<0){
  perror("fcntl(sock,SETFL,opts)");
  exit(1);
 }
}

variable_types: sock int

variable_types: opts int

return_type: void


decompiled_code:
__int64 __fastcall epoll_process(int fd)
{
int v1; // er12
int v2; // er15
int v3; // eax
struct epoll_event *v4; // rbx
struct epoll_event *v5; // r14
int v6; // ebp
int v7; // eax
unsigned int v8; // eax
socklen_t v10; // [rsp+0h] [rbp-3058h] BYREF
struct epoll_event event; // [rsp+4h] [rbp-3054h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-3048h] BYREF
struct epoll_event events; // [rsp+20h] [rbp-3038h] BYREF
_DWORD v14[3083]; // [rsp+2Ch] [rbp-302Ch] BYREF

event.data.fd = fd;
v2 = epoll_create(1024);
event.events = -2147483647;
if ( epoll_ctl(v2, 1, fd, &event) >= 0 )
{
v10 = 16;
do
LABEL_3:
v3 = epoll_wait(v2, &events, 512, 500);
while ( v3 <= 0 );
v4 = &events;
v5 = (struct epoll_event *)&v14[3 * (v3 - 1)];
while ( 1 )
{
v6 = v4->data.fd;
if ( v6 == fd )
{
v7 = accept(fd, &addr, &v10);
v1 = v7;
if ( v7 < 0 )
{
perror("accept error!\n");
goto LABEL_9;
}
setnonblocking(v7);
event.data.fd = v1;
event.events = -2147483647;
}
else
{
if ( (v4->events & 0x18) == 0 )
{
if ( v6 >= 0 && (v4->events & 1) != 0 )
{
if ( !fork() )
{
close(fd);
v8 = getpid();
printf("process %d forked\n", v8);
handleRequest((unsigned int)v6);
exit(0);
}
close(v6);
waitpid(-1, 0LL, 1);
event.data.fd = -1;
epoll_ctl(v2, 2, v6, &event);
}
goto LABEL_9;
}
close(v6);
}
epoll_ctl(v2, 1, v1, &event);
LABEL_9:
if ( ++v4 == v5 )
goto LABEL_3;
}
}
perror("epoll_ctl error !\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd)
{
int v1; // er12
int v2; // er15
int v3; // eax
struct epoll_event *v4; // rbx
struct epoll_event *v5; // r14
int v6; // ebp
int v7; // eax
unsigned int v8; // eax
socklen_t v10; // [rsp+0h] [rbp-3058h] BYREF
struct epoll_event event; // [rsp+4h] [rbp-3054h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-3048h] BYREF
struct epoll_event events; // [rsp+20h] [rbp-3038h] BYREF
_DWORD v14[3083]; // [rsp+2Ch] [rbp-302Ch] BYREF

event.data.fd = fd;
v2 = epoll_create(1024);
event.events = -2147483647;
if ( epoll_ctl(v2, 1, fd, &event) >= 0 )
{
v10 = 16;
do
LABEL_3:
v3 = epoll_wait(v2, &events, 512, 500);
while ( v3 <= 0 );
v4 = &events;
v5 = (struct epoll_event *)&v14[3 * (v3 - 1)];
while ( 1 )
{
v6 = v4->data.fd;
if ( v6 == fd )
{
v7 = accept(fd, &addr, &v10);
v1 = v7;
if ( v7 < 0 )
{
perror("accept error!\n");
goto LABEL_9;
}
func0(v7);
event.data.fd = v1;
event.events = -2147483647;
}
else
{
if ( (v4->events & 0x18) == 0 )
{
if ( v6 >= 0 && (v4->events & 1) != 0 )
{
if ( !fork() )
{
close(fd);
v8 = getpid();
printf("process %d forked\n", v8);
handleRequest((unsigned int)v6);
exit(0);
}
close(v6);
waitpid(-1, 0LL, 1);
event.data.fd = -1;
epoll_ctl(v2, 2, v6, &event);
}
goto LABEL_9;
}
close(v6);
}
epoll_ctl(v2, 1, v1, &event);
LABEL_9:
if ( ++v4 == v5 )
goto LABEL_3;
}
}
perror("epoll_ctl error !\n");
return 0xFFFFFFFFLL;
}

original_code:
int epoll_process(int fd)
{
 int i;
 int n;
 int nfds;
 int epfd;
 int len;
 int flag;
 int newfd;
 int conn;
    pid_t pid;
 char buffer[1024];
 struct pool *m_pool;
 struct epoll_event ev;
 struct epoll_event events[1024];
 struct sockaddr_in client_addr;
 epfd = epoll_create(1024);
 ev.data.fd = fd;
 ev.events = EPOLLIN|EPOLLET;
 flag = epoll_ctl(epfd, 1, fd, &ev);
    if(flag < 0){
  perror("epoll_ctl error !\n");
  return -1;
 }
 len = sizeof(struct sockaddr);
 while(1){
  nfds = epoll_wait(epfd, events, 512, 500);
  for(i = 0; i < nfds; ++i){
            if(events[i].data.fd == fd){
    conn = accept(fd, (struct sockaddr*)&client_addr, (unsigned int*)&len);
                if(conn < 0){
                    perror("accept error!\n");
                        continue;
                }
    setnonblocking(conn);
                ev.data.fd = conn;
                ev.events = EPOLLIN|EPOLLET;
                epoll_ctl(epfd, 1, conn, &ev);
            }
   else if((events[i].events & EPOLLERR) ||
     (events[i].events & EPOLLHUP)){
    close(events[i].data.fd);
    epoll_ctl(epfd, 1, conn, &ev);
    continue;
   }
   else if(events[i].events&EPOLLIN){
                if((newfd = events[i].data.fd) < 0)
                    continue;
          pid = fork();
          if(pid == 0){
           close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(newfd);
           exit(0);
          }
          close(newfd);
          waitpid(-1, ((void *)0), 1);
    ev.data.fd = -1;
    epoll_ctl(epfd, 2, newfd, &ev);
            }
  }
 }
 return 0;
}

canonical_original_code:
int epoll_process(int fd)
{
 int i;
 int n;
 int nfds;
 int epfd;
 int len;
 int flag;
 int newfd;
 int conn;
    int pid;
 char buffer[1024];
 struct pool *m_pool;
 struct epoll_event ev;
 struct epoll_event events[1024];
 struct sockaddr_in client_addr;
 epfd = epoll_create(1024);
 ev.data.fd = fd;
 ev.events = EPOLLIN|EPOLLET;
 flag = epoll_ctl(epfd, 1, fd, &ev);
    if(flag < 0){
  perror("epoll_ctl error !\n");
  return -1;
 }
 len = sizeof(struct sockaddr);
 while(1){
  nfds = epoll_wait(epfd, events, 512, 500);
  for(i = 0; i < nfds; ++i){
            if(events[i].data.fd == fd){
    conn = accept(fd, (struct sockaddr *)&client_addr, (unsigned int *)&len);
                if(conn < 0){
                    perror("accept error!\n");
                        continue;
                }
    setnonblocking(conn);
                ev.data.fd = conn;
                ev.events = EPOLLIN|EPOLLET;
                epoll_ctl(epfd, 1, conn, &ev);
            }
   else if((events[i].events & EPOLLERR) ||
     (events[i].events & EPOLLHUP)){
    close(events[i].data.fd);
    epoll_ctl(epfd, 1, conn, &ev);
    continue;
   }
   else if(events[i].events&EPOLLIN){
                if((newfd = events[i].data.fd) < 0)
                    continue;
          pid = fork();
          if(pid == 0){
           close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(newfd);
           exit(0);
          }
          close(newfd);
          waitpid(-1, ((void *)0), 1);
    ev.data.fd = -1;
    epoll_ctl(epfd, 2, newfd, &ev);
            }
  }
 }
 return 0;
}

variable_types: n int

variable_types: nfds int

variable_types: epfd int

variable_types: len int

variable_types: flag int

variable_types: newfd int

variable_types:
buffer
char [1024]

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } [1024]

return_type: int

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

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

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

user_defined_types: struct pool_block { char *space; char *last; char *end; struct pool_block *next; }


destroyPool

decompiled_code:
int __fastcall destroyPool(__int64 a1)
{
__int64 v1; // rbx
int v2; // ebp
void **v3; // r12

v1 = *(_QWORD *)(a1 + 8);
if ( *(int *)a1 > 0 )
{
v2 = 0;
do
{
v3 = (void **)v1;
v1 = *(_QWORD *)(v1 + 24);
++v2;
free(*v3);
free(v3);
}
while ( *(_DWORD *)a1 > v2 );
}
return puts("destroy success.");
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
int v2; // ebp
void **v3; // r12

v1 = *(_QWORD *)(a1 + 8);
if ( *(int *)a1 > 0 )
{
v2 = 0;
do
{
v3 = (void **)v1;
v1 = *(_QWORD *)(v1 + 24);
++v2;
free(*v3);
free(v3);
}
while ( *(_DWORD *)a1 > v2 );
}
return puts("destroy success.");
}

original_code:
void destroyPool(struct pool* m_pool)
{
 struct pool_block* p;
 struct pool_block* ptr;
 int i;
 p = m_pool->head;
 for(i = 0; i < m_pool->n; ++i){
  ptr = p->next;
  free(p->space);
  free(p);
  p = ptr;
 }
 m_pool = ((void *)0);
 free(m_pool);
 printf("destroy success.\n");
}

canonical_original_code:
void destroyPool(struct pool *m_pool)
{
 struct pool_block *p;
 struct pool_block *ptr;
 int i;
 p = m_pool->head;
 for(i = 0; i < m_pool->n; ++i){
  ptr = p->next;
  free(p->space);
  free(p);
  p = ptr;
 }
 m_pool = ((void *)0);
 free(m_pool);
 printf("destroy success.\n");
}

variable_types: struct pool_block { char *space; char *last; char *end; struct pool_block *next; } *

variable_types: i int


CheckResource

decompiled_code:
int __fastcall CheckResource(__int64 a1)
{
const char *v1; // rsi
__int128 v3[63]; // [rsp+0h] [rbp-3F8h] BYREF

v1 = *(const char **)(a1 + 16);
if ( !v1 )
return -1;
memset(v3, 0, 0x3E8uLL);
v3[0] = (__int128)_mm_load_si128((const __m128i *)&LC0);
qmemcpy(&v3[1], "r/www", 5);
strcpy((char *)&v3[1] + 5, v1);
return open((const char *)v3, 0);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
const char *v1; // rsi
__int128 v3[63]; // [rsp+0h] [rbp-3F8h] BYREF

v1 = *(const char **)(a1 + 16);
if ( !v1 )
return -1;
memset(v3, 0, 0x3E8uLL);
v3[0] = (__int128)_mm_load_si128((const __m128i *)&LC0);
qmemcpy(&v3[1], "r/www", 5);
strcpy((char *)&v3[1] + 5, v1);
return open((const char *)v3, 0);
}

original_code:
int CheckResource(struct ReqInfo* reqInfo)
{
 if(reqInfo->resource == ((void *)0))
  return -1;
 char path[1000];
 bzero(path, 1000);
 memcpy(path, "/home/y/SimServer/www", sizeof("/home/y/SimServer/www"));
 strcat(path, reqInfo->resource);
 return open(path, 00);
}

canonical_original_code:
int CheckResource(struct ReqInfo *reqInfo)
{
 if(reqInfo->resource == ((void *)0))
  return -1;
 char path[1000];
 bzero(path, 1000);
 memcpy(path, "/home/y/SimServer/www", sizeof("/home/y/SimServer/www"));
 strcat(path, reqInfo->resource);
 return open(path, 00);
}

variable_types: struct ReqInfo { enum Req_Method { GET=0, HEAD=1, UNSUPPORTED=2 } method; enum Req_Type { SIMPLE=0, FULL=1 } type; enum Req_PageType { STATIC=0, DYNAMIC=1 } pageType; char *resource; int status; int fd; } *

variable_types:
path
char [1000]

user_defined_types: struct ReqInfo { enum Req_Method method; enum Req_Type type; enum Req_PageType pageType; char *resource; int status; int fd; }


ReturnResource

decompiled_code:
ssize_t __fastcall ReturnResource(int fd, int a2)
{
ssize_t result; // rax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

while ( 1 )
{
result = read(a2, buf, 1uLL);
if ( !(_DWORD)result )
break;
if ( (int)result < 0 )
{
perror("Read file error !\n");
return 0xFFFFFFFFLL;
}
if ( (int)write(fd, buf, 1uLL) <= 0 )
{
perror("Send file error !\n");
return 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(int fd, int a2)
{
ssize_t result; // rax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

while ( 1 )
{
result = read(a2, buf, 1uLL);
if ( !(_DWORD)result )
break;
if ( (int)result < 0 )
{
perror("Read file error !\n");
return 0xFFFFFFFFLL;
}
if ( (int)write(fd, buf, 1uLL) <= 0 )
{
perror("Send file error !\n");
return 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int ReturnResource(int client_fd, int res_fd, struct ReqInfo* reqInfo)
{
 char c;
 int n;
 while((n = read(res_fd, &c, 1))){
  if(n < 0){
   perror("Read file error !\n");
   return -1;
  }
  if(write(client_fd, &c, 1) <= 0){
   perror("Send file error !\n");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int ReturnResource(int client_fd, int res_fd, struct ReqInfo *reqInfo)
{
 char c;
 int n;
 while((n = read(res_fd, &c, 1))){
  if(n < 0){
   perror("Read file error !\n");
   return -1;
  }
  if(write(client_fd, &c, 1) <= 0){
   perror("Send file error !\n");
   return -1;
  }
 }
 return 0;
}

variable_types: c char

user_defined_types: enum Req_Method { GET=0, HEAD=1, UNSUPPORTED=2 }

user_defined_types: enum Req_PageType { STATIC=0, DYNAMIC=1 }


WriteLine

decompiled_code:
unsigned __int64 __fastcall WriteLine(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v5; // rbx
int v6; // eax

v5 = 0LL;
while ( a3 > v5 )
{
v6 = write(fd, buf, a3 - v5);
if ( v6 > 0 )
{
v5 += v6;
buf = (char *)buf + v6;
}
else if ( *_errno_location() != 4 )
{
perror("Write error !\n");
return -1LL;
}
}
return a3;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v5; // rbx
int v6; // eax

v5 = 0LL;
while ( a3 > v5 )
{
v6 = write(fd, buf, a3 - v5);
if ( v6 > 0 )
{
v5 += v6;
buf = (char *)buf + v6;
}
else if ( *_errno_location() != 4 )
{
perror("Write error !\n");
return -1LL;
}
}
return a3;
}

original_code:
ssize_t WriteLine(int fd, char* buffer, size_t n)
{
 size_t start = 0;
 ssize_t m;
    while(start < n){
  m = write(fd, buffer, n - start);
  if(m <= 0){
   if((*__errno_location ()) == 4)
    m = 0;
   else{
    perror("Write error !\n");
    return -1;
   }
  }
  start += m;
  buffer += m;
 }
 return n;
}

canonical_original_code:
long WriteLine(int fd, char *buffer, unsigned long n)
{
 unsigned long start = 0;
 long m;
    while(start < n){
  m = write(fd, buffer, n - start);
  if(m <= 0){
   if((*__errno_location ()) == 4)
    m = 0;
   else{
    perror("Write error !\n");
    return -1;
   }
  }
  start += m;
  buffer += m;
 }
 return n;
}

variable_types: fd int

variable_types:
buffer
char *

variable_types:
n
unsigned long

variable_types:
start
unsigned long

variable_types: m long

return_type: long


ReturnErrorMsg

decompiled_code:
__int64 __fastcall ReturnErrorMsg(int fd, __int64 a2)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
__int128 v6[3]; // [rsp+0h] [rbp-88h] BYREF
char v7[16]; // [rsp+30h] [rbp-58h] BYREF

sprintf((char *)v6, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", *(unsigned int *)(a2 + 24));
v2 = strlen((const char *)v6);
WriteLine(fd, v6, v2);
sprintf((char *)v6, "\n
Server Error %d
\n", *(unsigned int *)(a2 + 24));
v3 = strlen((const char *)v6);
WriteLine(fd, v6, v3);
strcpy(v7, "Y>\n\n");
v6[0] = (__int128)_mm_load_si128((const __m128i *)&LC6);
v6[1] = (__int128)_mm_load_si128((const __m128i *)&LC7);
v6[2] = (__int128)_mm_load_si128((const __m128i *)&LC8);
v4 = strlen((const char *)v6);
WriteLine(fd, v6, v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rax
__int128 v6[3]; // [rsp+0h] [rbp-88h] BYREF
char v7[16]; // [rsp+30h] [rbp-58h] BYREF

sprintf((char *)v6, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", *(unsigned int *)(a2 + 24));
v2 = strlen((const char *)v6);
func2(fd, v6, v2);
sprintf((char *)v6, "\n
Server Error %d
\n", *(unsigned int *)(a2 + 24));
v3 = strlen((const char *)v6);
func2(fd, v6, v3);
strcpy(v7, "Y>\n\n");
v6[0] = (__int128)_mm_load_si128((const __m128i *)&LC6);
v6[1] = (__int128)_mm_load_si128((const __m128i *)&LC7);
v6[2] = (__int128)_mm_load_si128((const __m128i *)&LC8);
v4 = strlen((const char *)v6);
func2(fd, v6, v4);
return 0LL;
}

original_code:
int ReturnErrorMsg(int conn, struct ReqInfo * reqinfo)
{
 char buffer[100];
 sprintf(buffer, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
 sprintf(buffer, "\n
Server Error %d
\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
    sprintf(buffer, "
The request could not be completed.
\n\n\n");
   WriteLine(conn, buffer, strlen(buffer));
 return 0;
}

canonical_original_code:
int ReturnErrorMsg(int conn, struct ReqInfo *reqinfo)
{
 char buffer[100];
 sprintf(buffer, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
 sprintf(buffer, "\n
Server Error %d
\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
    sprintf(buffer, "
The request could not be completed.
\n\n\n");
   WriteLine(conn, buffer, strlen(buffer));
 return 0;
}

variable_types: conn int

variable_types:
buffer
char [100]


handleRequest

decompiled_code:
int __fastcall handleRequest(int fd)
{
_QWORD *v1; // rbp

puts("\n\n===========================");
v1 = malloc(0x20uLL);
InitReqInfo(v1);
GetReqContent((unsigned int)fd, v1);
if ( v1[2] )
{
printf("status: %d\n", *((unsigned int *)v1 + 6));
writeLog(v1[2]);
printf("recv buffer: %s\n", (const char *)v1[2]);
if ( *((_DWORD *)v1 + 2) )
puts("dynamic page..");
else
puts("static page..");
ReturnResponse((unsigned int)fd, v1);
}
close(fd);
return puts("===========================\n");
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
_QWORD *v1; // rbp

puts("\n\n===========================");
v1 = malloc(0x20uLL);
InitReqInfo(v1);
GetReqContent((unsigned int)fd, v1);
if ( v1[2] )
{
printf("status: %d\n", *((unsigned int *)v1 + 6));
writeLog(v1[2]);
printf("recv buffer: %s\n", (const char *)v1[2]);
if ( *((_DWORD *)v1 + 2) )
puts("dynamic page..");
else
puts("static page..");
ReturnResponse((unsigned int)fd, v1);
}
close(fd);
return puts("===========================\n");
}

original_code:
void handleRequest(int fd)
{
 printf("\n\n===========================\n");
 struct pool* m_pool;
 int ret;
 struct ReqInfo* reqInfo;
    reqInfo = (struct ReqInfo*)malloc(sizeof(struct ReqInfo));
 InitReqInfo(reqInfo);
 GetReqContent(fd, reqInfo);
 if (reqInfo->resource != ((void *)0)) {
  printf("status: %d\n", reqInfo->status);
  writeLog(reqInfo->resource);
  printf("recv buffer: %s\n", reqInfo->resource);
  if(reqInfo->pageType == STATIC)
   printf("static page..\n");
  else
   printf("dynamic page..\n");
  ReturnResponse(fd, reqInfo);
 }
 close(fd);
 printf("===========================\n\n");
}

canonical_original_code:
void handleRequest(int fd)
{
 printf("\n\n===========================\n");
 struct pool *m_pool;
 int ret;
 struct ReqInfo *reqInfo;
    reqInfo = (struct ReqInfo *)malloc(sizeof(struct ReqInfo));
 InitReqInfo(reqInfo);
 GetReqContent(fd, reqInfo);
 if (reqInfo->resource != ((void *)0)) {
  printf("status: %d\n", reqInfo->status);
  writeLog(reqInfo->resource);
  printf("recv buffer: %s\n", reqInfo->resource);
  if(reqInfo->pageType == STATIC)
   printf("static page..\n");
  else
   printf("dynamic page..\n");
  ReturnResponse(fd, reqInfo);
 }
 close(fd);
 printf("===========================\n\n");
}

variable_types: struct pool { int n; struct pool_block { char *space; char *last; char *end; struct pool_block *next; } *head; } *

variable_types: ret int


original_code:
int select_process(int fd)
{
 int i;
 int len;
 int flag;
 int conn;
 int maxi;
 int maxfd;
 int sockfd;
 int nready;
    pid_t pid;
 fd_set rset, allset;
 int client[1024];
 struct sockaddr_in client_addr;
 len = sizeof(struct sockaddr);
 maxfd = fd;
 for(i = 0; i < 1024; ++i){
  client[i] = -1;
 }
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&allset)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&allset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 for(;;){
  rset = allset;
  nready = select(maxfd+1, &rset, ((void *)0), ((void *)0), ((void *)0));
  if(((((&rset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
   conn = accept(fd, (struct sockaddr*)&client_addr, (unsigned int*)&len);
   for(i = 0; i < 1024; ++i){
    if(client[i] < 0)
     client[i] = conn;
     break;
   }
   if(i == 1024){
    perror("too many clients!\n");
    break;
   }
   ((void) (((&allset)->__fds_bits)[((conn) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((conn) % (8 * (int) sizeof (__fd_mask)))))));
   if(conn > maxfd)
    maxfd = conn;
   if(i > maxi)
    maxi = i;
   if(--nready <= 0)
    continue;
  }
  for(i = 0; i <= maxi; ++i){
   if((sockfd = client[i]) < 0)
    continue;
   if(((((&rset)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
                pid = fork();
                if (pid == 0) {
                    close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(sockfd);
           exit(0);
                }
                close(sockfd);
          waitpid(-1, ((void *)0), 1);
    ((void) (((&allset)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if(--nready <= 0)
     break;
   }
  }
 }
 return 0;
}

canonical_original_code:
int select_process(int fd)
{
 int i;
 int len;
 int flag;
 int conn;
 int maxi;
 int maxfd;
 int sockfd;
 int nready;
    int pid;
 struct fd_set rset; struct fd_set allset;
 int client[1024];
 struct sockaddr_in client_addr;
 len = sizeof(struct sockaddr);
 maxfd = fd;
 for(i = 0; i < 1024; ++i){
  client[i] = -1;
 }
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&allset)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&allset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 for(;;){
  rset = allset;
  nready = select(maxfd+1, &rset, ((void *)0), ((void *)0), ((void *)0));
  if(((((&rset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
   conn = accept(fd, (struct sockaddr *)&client_addr, (unsigned int *)&len);
   for(i = 0; i < 1024; ++i){
    if(client[i] < 0)
     client[i] = conn;
     break;
   }
   if(i == 1024){
    perror("too many clients!\n");
    break;
   }
   ((void) (((&allset)->__fds_bits)[((conn) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((conn) % (8 * (int) sizeof (__fd_mask)))))));
   if(conn > maxfd)
    maxfd = conn;
   if(i > maxi)
    maxi = i;
   if(--nready <= 0)
    continue;
  }
  for(i = 0; i <= maxi; ++i){
   if((sockfd = client[i]) < 0)
    continue;
   if(((((&rset)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
                pid = fork();
                if (pid == 0) {
                    close(fd);
                    printf("process %d forked\n", getpid());
           handleRequest(sockfd);
           exit(0);
                }
                close(sockfd);
          waitpid(-1, ((void *)0), 1);
    ((void) (((&allset)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if(--nready <= 0)
     break;
   }
  }
 }
 return 0;
}

variable_types: len int

variable_types: flag int

variable_types: maxi int

variable_types: maxfd int

variable_types: sockfd int

variable_types: nready int

variable_types: pid int

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

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

variable_types:
client
int [1024]

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
sem_t **v3; // r12
sem_t **v4; // rbx
sem_t *v5; // rax
sem_t *v7; // rdi
pthread_t th; // [rsp+0h] [rbp-78h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-70h] BYREF
pthread_t v10; // [rsp+10h] [rbp-68h] BYREF
pthread_t v11; // [rsp+18h] [rbp-60h] BYREF
char arg[24]; // [rsp+20h] [rbp-58h] BYREF
sem_t *v13; // [rsp+38h] [rbp-40h] BYREF
sem_t sem; // [rsp+40h] [rbp-38h] BYREF

v3 = (sem_t **)arg;
v4 = (sem_t **)arg;
do
{
v5 = (sem_t *)malloc(0x78uLL);
*v4 = v5;
if ( !v5 )
{
puts("Error initializing queue.");
return -1;
}
sq_init(v5, argv);
++v4;
}
while ( v4 != &v13 );
if ( sem_init(&sem, 0, 1u) < 0 )
{
puts("Error initializing semaphore");
}
else
{
v13 = &sem;
if ( pthread_create(&th, 0LL, reader, arg) )
{
puts("Could not create reader");
}
else if ( pthread_create(&newthread, 0LL, munch1, arg) )
{
puts("Could not create munch1");
}
else if ( pthread_create(&v10, 0LL, munch2, arg) )
{
puts("Could not create munch2");
}
else if ( pthread_create(&v11, 0LL, writer, arg) )
{
puts("Could not create writer");
}
else if ( pthread_join(th, 0LL) )
{
puts("Could not join reader");
}
else if ( pthread_join(newthread, 0LL) )
{
puts("Could not join munch1");
}
else if ( pthread_join(v10, 0LL) )
{
puts("Could not join munch2");
}
else
{
if ( !pthread_join(v11, 0LL) )
{
do
{
v7 = *v3++;
sq_destroy(v7);
free(*(v3 - 1));
}
while ( v3 != &v13 );
sem_destroy(v13);
free(v13);
pthread_exit(0LL);
}
puts("Could not join writer");
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
sem_t **v3; // r12
sem_t **v4; // rbx
sem_t *v5; // rax
sem_t *v7; // rdi
pthread_t th; // [rsp+0h] [rbp-78h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-70h] BYREF
pthread_t v10; // [rsp+10h] [rbp-68h] BYREF
pthread_t v11; // [rsp+18h] [rbp-60h] BYREF
char arg[24]; // [rsp+20h] [rbp-58h] BYREF
sem_t *v13; // [rsp+38h] [rbp-40h] BYREF
sem_t sem; // [rsp+40h] [rbp-38h] BYREF

v3 = (sem_t **)arg;
v4 = (sem_t **)arg;
do
{
v5 = (sem_t *)malloc(0x78uLL);
*v4 = v5;
if ( !v5 )
{
puts("Error initializing queue.");
return -1;
}
func9(v5, argv);
++v4;
}
while ( v4 != &v13 );
if ( sem_init(&sem, 0, 1u) < 0 )
{
puts("Error initializing semaphore");
}
else
{
v13 = &sem;
if ( pthread_create(&th, 0LL, reader, arg) )
{
puts("Could not create reader");
}
else if ( pthread_create(&newthread, 0LL, munch1, arg) )
{
puts("Could not create munch1");
}
else if ( pthread_create(&v10, 0LL, munch2, arg) )
{
puts("Could not create munch2");
}
else if ( pthread_create(&v11, 0LL, writer, arg) )
{
puts("Could not create writer");
}
else if ( pthread_join(th, 0LL) )
{
puts("Could not join reader");
}
else if ( pthread_join(newthread, 0LL) )
{
puts("Could not join munch1");
}
else if ( pthread_join(v10, 0LL) )
{
puts("Could not join munch2");
}
else
{
if ( !pthread_join(v11, 0LL) )
{
do
{
v7 = *v3++;
func13(v7);
free(*(v3 - 1));
}
while ( v3 != &v13 );
sem_destroy(v13);
free(v13);
pthread_exit(0LL);
}
puts("Could not join writer");
}
}
return -1;
}

original_code:
int main(int argc, char **argv)
{
  int i;
  pthread_t thr[4];
  void* q[4];
  sem_t input_ready;
  i = 0;
  while (i < 4 - 1)
  {
    if (((void *)0) == (q[i] = (sq*)malloc(sizeof(sq))))
    {
        printf("Error initializing queue.\n");
        return -1;
    }
    sq_init(q[i]);
    i++;
  }
  if(0 > sem_init(&input_ready, 0, 1))
  {
    printf("Error initializing semaphore\n");
    return -1;
  }
  q[3] = &input_ready;
  if(pthread_create(&thr[0], ((void *)0), &reader, q))
  {
    printf("Could not create reader\n");
    return -1;
  }
  if(pthread_create(&thr[1], ((void *)0), &munch1, q))
  {
    printf("Could not create munch1\n");
    return -1;
  }
  if(pthread_create(&thr[2], ((void *)0), &munch2, q))
  {
    printf("Could not create munch2\n");
    return -1;
  }
  if(pthread_create(&thr[3], ((void *)0), &writer, q))
  {
    printf("Could not create writer\n");
    return -1;
  }
  if(pthread_join(thr[0], ((void *)0)))
  {
    printf("Could not join reader\n");
    return -1;
  }
  if(pthread_join(thr[1], ((void *)0)))
  {
    printf("Could not join munch1\n");
    return -1;
  }
  if(pthread_join(thr[2], ((void *)0)))
  {
    printf("Could not join munch2\n");
    return -1;
  }
  if(pthread_join(thr[3], ((void *)0)))
  {
    printf("Could not join writer\n");
    return -1;
  }
  i = 0;
  while(i < 3)
  {
    sq_destroy(q[i]);
    free((void*)q[i]);
    i++;
  }
  sem_destroy(q[3]);
  free((void*)q[3]);
  pthread_exit(((void *)0));
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int i;
  unsigned long thr[4];
  void *q[4];
  union sem_t input_ready;
  i = 0;
  while (i < 4 - 1)
  {
    if (((void *)0) == (q[i] = (struct synchq *)malloc(sizeof(sq))))
    {
        printf("Error initializing queue.\n");
        return -1;
    }
    sq_init(q[i]);
    i++;
  }
  if(0 > sem_init(&input_ready, 0, 1))
  {
    printf("Error initializing semaphore\n");
    return -1;
  }
  q[3] = &input_ready;
  if(pthread_create(&thr[0], ((void *)0), &reader, q))
  {
    printf("Could not create reader\n");
    return -1;
  }
  if(pthread_create(&thr[1], ((void *)0), &munch1, q))
  {
    printf("Could not create munch1\n");
    return -1;
  }
  if(pthread_create(&thr[2], ((void *)0), &munch2, q))
  {
    printf("Could not create munch2\n");
    return -1;
  }
  if(pthread_create(&thr[3], ((void *)0), &writer, q))
  {
    printf("Could not create writer\n");
    return -1;
  }
  if(pthread_join(thr[0], ((void *)0)))
  {
    printf("Could not join reader\n");
    return -1;
  }
  if(pthread_join(thr[1], ((void *)0)))
  {
    printf("Could not join munch1\n");
    return -1;
  }
  if(pthread_join(thr[2], ((void *)0)))
  {
    printf("Could not join munch2\n");
    return -1;
  }
  if(pthread_join(thr[3], ((void *)0)))
  {
    printf("Could not join writer\n");
    return -1;
  }
  i = 0;
  while(i < 3)
  {
    sq_destroy(q[i]);
    free((void *)q[i]);
    i++;
  }
  sem_destroy(q[3]);
  free((void *)q[3]);
  pthread_exit(((void *)0));
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types:
thr
unsigned long [4]

variable_types:
q
void *[4]

variable_types: union sem_t { char __size[32]; long __align; }

user_defined_types: struct synchq { char **data; int head; int size; int done; union sem_t open; union sem_t filled; union sem_t mutex; }


writer

decompiled_code:
void *__fastcall writer(void *a1)
{
unsigned int v1; // er12
char *v2; // rbp

v1 = 0;
while ( !(unsigned int)sq_isDone(*((_QWORD *)a1 + 2)) || !(unsigned int)sq_isEmpty(*((_QWORD *)a1 + 2)) )
{
++v1;
v2 = (char *)sq_deq(*((_QWORD *)a1 + 2));
printf("Output: %s\n", v2);
free(v2);
sem_post(*((sem_t **)a1 + 3));
}
printf("Total number of lines: %d \n", v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
unsigned int v1; // er12
char *v2; // rbp

v1 = 0;
while ( !(unsigned int)func15(*((_QWORD *)a1 + 2)) || !(unsigned int)func12(*((_QWORD *)a1 + 2)) )
{
++v1;
v2 = (char *)func11(*((_QWORD *)a1 + 2));
printf("Output: %s\n", v2);
free(v2);
sem_post(*((sem_t **)a1 + 3));
}
printf("Total number of lines: %d \n", v1);
return 0LL;
}

original_code:
void* writer(void *q)
{
    sq** queues = (sq**)q;
    int count = 0;
    char *inputString;
    while (!sq_isDone(queues[2]) || !sq_isEmpty(queues[2]))
    {
        inputString = sq_deq(queues[2]);
        printf("Output: %s\n", inputString);
        count++;
        free(inputString);
        sem_post((sem_t*)queues[3]);
    }
    printf("Total number of lines: %d \n", count);
    return ((void *)0);
}

canonical_original_code:
void *writer(void *q)
{
    struct synchq **queues = (sq**)q;
    int count = 0;
    char *inputString;
    while (!sq_isDone(queues[2]) || !sq_isEmpty(queues[2]))
    {
        inputString = sq_deq(queues[2]);
        printf("Output: %s\n", inputString);
        count++;
        free(inputString);
        sem_post((union sem_t *)queues[3]);
    }
    printf("Total number of lines: %d \n", count);
    return ((void *)0);
}

variable_types:
q
void *

variable_types: struct synchq { char **data; int head; int size; int done; union sem_t { char __size[32]; long __align; } open; union sem_t filled; union sem_t mutex; } **

variable_types: count int

variable_types:
inputString
char *

return_type: void *


decompiled_code:
void *__fastcall munch1(void *a1)
{
const char *v1; // rbp
char *v2; // rax

while ( !(unsigned int)sq_isDone(*(_QWORD *)a1) || !(unsigned int)sq_isEmpty(*(_QWORD *)a1) )
{
v1 = (const char *)sq_deq(*(_QWORD *)a1);
if ( !v1 )
{
printf("ERROR: Failed to get string from q1.");
return 0LL;
}
while ( 1 )
{
v2 = index(v1, 32);
if ( !v2 )
break;
*v2 = 42;
}
sq_enq(*((_QWORD *)a1 + 1), v1);
}
sq_done(*((_QWORD *)a1 + 1));
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
const char *v1; // rbp
char *v2; // rax

while ( !(unsigned int)func15(*(_QWORD *)a1) || !(unsigned int)func12(*(_QWORD *)a1) )
{
v1 = (const char *)func11(*(_QWORD *)a1);
if ( !v1 )
{
printf("ERROR: Failed to get string from q1.");
return 0LL;
}
while ( 1 )
{
v2 = index(v1, 32);
if ( !v2 )
break;
*v2 = 42;
}
func10(*((_QWORD *)a1 + 1), v1);
}
func14(*((_QWORD *)a1 + 1));
return 0LL;
}

original_code:
void *munch1 (void* q)
{
    sq** queues = (sq**)q;
    char *str, *curr;
    while(!sq_isDone(queues[0]) || !sq_isEmpty(queues[0]))
    {
        if (((void *)0) == (str = sq_deq(queues[0])))
        {
            printf("ERROR: Failed to get string from q1.");
            return ((void *)0);
        }
        curr = index(str, ' ');
        while (((void *)0) != curr)
        {
            *curr = '*';
            curr = index(str, ' ');
        }
        sq_enq(queues[1], str);
    }
    sq_done(queues[1]);
    return ((void *)0);
}

canonical_original_code:
void *munch1(void *q)
{
    struct synchq **queues = (sq**)q;
    char *str; char *curr;
    while(!sq_isDone(queues[0]) || !sq_isEmpty(queues[0]))
    {
        if (((void *)0) == (str = sq_deq(queues[0])))
        {
            printf("ERROR: Failed to get string from q1.");
            return ((void *)0);
        }
        curr = index(str, ' ');
        while (((void *)0) != curr)
        {
            *curr = '*';
            curr = index(str, ' ');
        }
        sq_enq(queues[1], str);
    }
    sq_done(queues[1]);
    return ((void *)0);
}

variable_types:
str
char *

variable_types:
curr
char *

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
int __fastcall sq_init(__int64 a1)
{
void *v1; // rax
int result; // eax

v1 = malloc(0x50uLL);
*(_QWORD *)a1 = v1;
if ( !v1 )
return puts("Error initializing queue.");
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 16) = 0;
if ( sem_init((sem_t *)(a1 + 24), 0, 0xAu) < 0 )
return puts("Error initializing sem open");
if ( sem_init((sem_t *)(a1 + 56), 0, 0) < 0 )
return puts("Error initializing sem filled");
result = sem_init((sem_t *)(a1 + 88), 0, 1u);
if ( result < 0 )
result = puts("Error initializing sem mutex");
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1)
{
void *v1; // rax
int result; // eax

v1 = malloc(0x50uLL);
*(_QWORD *)a1 = v1;
if ( !v1 )
return puts("Error initializing queue.");
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 16) = 0;
if ( sem_init((sem_t *)(a1 + 24), 0, 0xAu) < 0 )
return puts("Error initializing sem open");
if ( sem_init((sem_t *)(a1 + 56), 0, 0) < 0 )
return puts("Error initializing sem filled");
result = sem_init((sem_t *)(a1 + 88), 0, 1u);
if ( result < 0 )
result = puts("Error initializing sem mutex");
return result;
}

original_code:
void sq_init(sq* q)
{
  if(((void *)0) == (q->data = (char**)malloc(10 * sizeof(char*))))
  {
    printf("Error initializing queue.\n");
    return;
  }
  q->head = 0;
  q->size = 0;
  q->done = 0;
  if(0 > sem_init(&q->open, 0, 10))
  {
    printf("Error initializing sem open\n");
    return;
  }
  if (0 > sem_init(&q->filled, 0, 0))
  {
    printf("Error initializing sem filled\n");
    return;
  }
  if (0 > sem_init(&q->mutex, 0, 1))
  {
    printf("Error initializing sem mutex\n");
    return;
  }
}

canonical_original_code:
void sq_init(struct synchq *q)
{
  if(((void *)0) == (q->data = (char **)malloc(10 * sizeof(char *))))
  {
    printf("Error initializing queue.\n");
    return;
  }
  q->head = 0;
  q->size = 0;
  q->done = 0;
  if(0 > sem_init(&q->open, 0, 10))
  {
    printf("Error initializing sem open\n");
    return;
  }
  if (0 > sem_init(&q->filled, 0, 0))
  {
    printf("Error initializing sem filled\n");
    return;
  }
  if (0 > sem_init(&q->mutex, 0, 1))
  {
    printf("Error initializing sem mutex\n");
    return;
  }
}

variable_types: struct synchq { char **data; int head; int size; int done; union sem_t { char __size[32]; long __align; } open; union sem_t filled; union sem_t mutex; } *


original_code:
void sq_enq(sq* q, char* new_item)
{
  sem_wait(&q->open);
  sem_wait(&q->mutex);
  q->data[(q->head + q->size) % 10] = new_item;
  q->size++;
  sem_post(&q->mutex);
  sem_post(&q->filled);
}

canonical_original_code:
void sq_enq(struct synchq *q, char *new_item)
{
  sem_wait(&q->open);
  sem_wait(&q->mutex);
  q->data[(q->head + q->size) % 10] = new_item;
  q->size++;
  sem_post(&q->mutex);
  sem_post(&q->filled);
}


original_code:
void* sq_deq(sq* q)
{
  int loc;
  sem_wait(&q->filled);
  sem_wait(&q->mutex);
  loc = q->head;
  q->size--;
  q->head = (q->head + 1) % 10;
  sem_post(&q->mutex);
  sem_post(&q->open);
  return (void*)q->data[loc];
}

canonical_original_code:
void *sq_deq(struct synchq *q)
{
  int loc;
  sem_wait(&q->filled);
  sem_wait(&q->mutex);
  loc = q->head;
  q->size--;
  q->head = (q->head + 1) % 10;
  sem_post(&q->mutex);
  sem_post(&q->open);
  return (void *)q->data[loc];
}

variable_types: loc int


decompiled_code:
_BOOL8 __fastcall sq_isEmpty(__int64 a1)
{
return *(_DWORD *)(a1 + 12) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(__int64 a1)
{
return *(_DWORD *)(a1 + 12) == 0;
}

original_code:
int sq_isEmpty(sq* q)
{
  return 0 == q->size;
}

canonical_original_code:
int sq_isEmpty(struct synchq *q)
{
  return 0 == q->size;
}


decompiled_code:
void __fastcall sq_destroy(void *ptr)
{
sem_destroy((sem_t *)((char *)ptr + 88));
sem_destroy((sem_t *)((char *)ptr + 24));
sem_destroy((sem_t *)((char *)ptr + 56));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func13(void *ptr)
{
sem_destroy((sem_t *)((char *)ptr + 88));
sem_destroy((sem_t *)((char *)ptr + 24));
sem_destroy((sem_t *)((char *)ptr + 56));
free(ptr);
}

original_code:
void sq_destroy(sq* q)
{
  sem_destroy(&q->mutex);
  sem_destroy(&q->open);
  sem_destroy(&q->filled);
  free(q);
}

canonical_original_code:
void sq_destroy(struct synchq *q)
{
  sem_destroy(&q->mutex);
  sem_destroy(&q->open);
  sem_destroy(&q->filled);
  free(q);
}


decompiled_code:
void __fastcall sq_done(__int64 a1)
{
*(_DWORD *)(a1 + 16) = 1;
}

original_code:
void sq_done(sq* q)
{
  q->done = 1;
}

canonical_original_code:
void sq_done(struct synchq *q)
{
  q->done = 1;
}


decompiled_code:
__int64 __fastcall sq_isDone(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int sq_isDone(sq* q)
{
  return q->done;
}

canonical_original_code:
int sq_isDone(struct synchq *q)
{
  return q->done;
}


decompiled_code:
void *__fastcall munch2(void *a1)
{
char *v1; // rax
__int64 v2; // r12
__int64 v3; // rbx
const __int32_t **v4; // rcx
char *v5; // rax

while ( !(unsigned int)sq_isDone(*((_QWORD *)a1 + 1)) || !sq_isEmpty(*((_QWORD *)a1 + 1)) )
{
v1 = (char *)sq_deq(*((_QWORD *)a1 + 1));
v2 = (__int64)v1;
if ( !v1 )
{
printf("ERROR: Input is empty.");
return 0LL;
}
v3 = *v1;
if ( (_BYTE)v3 )
{
v4 = __ctype_toupper_loc();
v5 = (char *)v2;
do
{
*v5++ = (*v4)[v3];
v3 = *v5;
}
while ( (_BYTE)v3 );
}
sq_enq(*((_QWORD *)a1 + 2), v2);
}
sq_done(*((_QWORD *)a1 + 2));
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func16(void *a1)
{
char *v1; // rax
__int64 v2; // r12
__int64 v3; // rbx
const __int32_t **v4; // rcx
char *v5; // rax

while ( !(unsigned int)func15(*((_QWORD *)a1 + 1)) || !func12(*((_QWORD *)a1 + 1)) )
{
v1 = (char *)func11(*((_QWORD *)a1 + 1));
v2 = (__int64)v1;
if ( !v1 )
{
printf("ERROR: Input is empty.");
return 0LL;
}
v3 = *v1;
if ( (_BYTE)v3 )
{
v4 = __ctype_toupper_loc();
v5 = (char *)v2;
do
{
*v5++ = (*v4)[v3];
v3 = *v5;
}
while ( (_BYTE)v3 );
}
func10(*((_QWORD *)a1 + 2), v2);
}
func14(*((_QWORD *)a1 + 2));
return 0LL;
}

original_code:
void * munch2 (void * q)
{
    sq** queues = (sq**)q;
    char *inputString, *curr;
    while(!sq_isDone(queues[1]) || !sq_isEmpty(queues[1]))
    {
        if (((void *)0) == (inputString = sq_deq(queues[1])))
        {
            printf("ERROR: Input is empty.");
            return ((void *)0);
        }
        curr = inputString;
        while (*curr != '\0'){
            *curr = toupper(*curr);
            curr = curr + (sizeof(char));
        }
        sq_enq(queues[2], inputString);
    }
    sq_done(queues[2]);
    return ((void *)0);
}

canonical_original_code:
void *munch2(void *q)
{
    struct synchq **queues = (sq**)q;
    char *inputString; char *curr;
    while(!sq_isDone(queues[1]) || !sq_isEmpty(queues[1]))
    {
        if (((void *)0) == (inputString = sq_deq(queues[1])))
        {
            printf("ERROR: Input is empty.");
            return ((void *)0);
        }
        curr = inputString;
        while (*curr != '\0'){
            *curr = toupper(*curr);
            curr = curr + (sizeof(char));
        }
        sq_enq(queues[2], inputString);
    }
    sq_done(queues[2]);
    return ((void *)0);
}


repo: yyyyyao/sandbox

decompiled_code:
__int64 USB_init()
{
usb_init();
usb_find_busses();
usb_find_devices();
return usb_get_busses();
}

canonical_decompiled_code:
__int64 func7()
{
usb_init();
usb_find_busses();
usb_find_devices();
return usb_get_busses();
}

original_code:
struct usb_bus *USB_init()
{
        usb_init();
        usb_find_busses();
        usb_find_devices();
        return(usb_get_busses());
}

return_type: struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *devices; unsigned int location; struct usb_device *root_dev; } *

user_defined_types: struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device *devices; unsigned int location; struct usb_device *root_dev; }

user_defined_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; }

user_defined_types: struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; int extralen; }

user_defined_types: struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }

user_defined_types: struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen; }


decompiled_code:
void __noreturn print_usage_and_exit()
{
fwrite("usage ./chktap [ON/OFF]\n", 1uLL, 0x18uLL, _bss_start);
exit(1);
}

canonical_decompiled_code:
void __noreturn func11()
{
fwrite("usage ./chktap [ON/OFF]\n", 1uLL, 0x18uLL, _bss_start);
exit(1);
}

original_code:
void print_usage_and_exit(void) {
  fprintf(stderr, "usage ./chktap [ON/OFF]\n");
  exit(1);
}

canonical_original_code:
void print_usage_and_exit() {
  fprintf(stderr, "usage ./chktap [ON/OFF]\n");
  exit(1);
}


repo: yzzyx/codewiki

decompiled_code:
int __fastcall mtime_cmp(const char *a1)
{
int v1; // er8
int result; // eax
struct stat v3; // [rsp+0h] [rbp-2128h] BYREF
struct stat v4; // [rsp+90h] [rbp-2098h] BYREF
char s[4096]; // [rsp+120h] [rbp-2008h] BYREF
char file[4104]; // [rsp+1120h] [rbp-1008h] BYREF

snprintf(s, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
snprintf(file, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
v1 = stat(s, &v3);
result = 1;
if ( v1 != -1 )
{
result = stat(file, &v4);
if ( result != -1 )
result = LODWORD(v4.st_mtim.tv_sec) - LODWORD(v3.st_mtim.tv_sec);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int v1; // er8
int result; // eax
struct stat v3; // [rsp+0h] [rbp-2128h] BYREF
struct stat v4; // [rsp+90h] [rbp-2098h] BYREF
char s[4096]; // [rsp+120h] [rbp-2008h] BYREF
char file[4104]; // [rsp+1120h] [rbp-1008h] BYREF

snprintf(s, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
snprintf(file, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
v1 = stat(s, &v3);
result = 1;
if ( v1 != -1 )
{
result = stat(file, &v4);
if ( result != -1 )
result = LODWORD(v4.st_mtim.tv_sec) - LODWORD(v3.st_mtim.tv_sec);
}
return result;
}

original_code:
static int
mtime_cmp(const char *page)
{
 struct stat st1, st2;
 char generated[4096];
 char latest[4096];
 snprintf(generated, sizeof generated, "%s/%s/generated.html",
     config.contents_dir, page);
 snprintf(latest, sizeof latest, "%s/%s/latest",
     config.contents_dir, page);
 if (stat(generated, &st1) == -1)
  return (1);
 if (stat(latest, &st2) == -1)
  return (-1);
 return (st2.st_mtim.tv_sec - st1.st_mtim.tv_sec);
}

canonical_original_code:
static int mtime_cmp(char *page)
{
 struct stat st1; struct stat st2;
 char generated[4096];
 char latest[4096];
 snprintf(generated, sizeof generated, "%s/%s/generated.html",
     config.contents_dir, page);
 snprintf(latest, sizeof latest, "%s/%s/latest",
     config.contents_dir, page);
 if (stat(generated, &st1) == -1)
  return (1);
 if (stat(latest, &st2) == -1)
  return (-1);
 return (st2.st_mtim.tv_sec - st1.st_mtim.tv_sec);
}

variable_types:
page
char *

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

variable_types:
generated
char [4096]

variable_types:
latest
char [4096]

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


decompiled_code:
__int64 __fastcall wiki_stat_page(const char *a1)
{
unsigned int v1; // er12

v1 = mtime_cmp(a1) > 0;
if ( mtime_cmp("_header") > 0 )
v1 |= 2u;
if ( mtime_cmp("_footer") > 0 )
v1 = 3;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
unsigned int v1; // er12

v1 = func0(a1) > 0;
if ( func0("_header") > 0 )
v1 |= 2u;
if ( func0("_footer") > 0 )
v1 = 3;
return v1;
}

original_code:
int
wiki_stat_page(const char *page)
{
 int pstat;
 pstat = 0;
 if (mtime_cmp(page) > 0)
  pstat |= (1);
 if (mtime_cmp("_header") > 0)
  pstat |= (2);
 if (mtime_cmp("_footer") > 0)
  pstat |= (3);
 return (pstat);
}

canonical_original_code:
int wiki_stat_page(char *page)
{
 int pstat;
 pstat = 0;
 if (mtime_cmp(page) > 0)
  pstat |= (1);
 if (mtime_cmp("_header") > 0)
  pstat |= (2);
 if (mtime_cmp("_footer") > 0)
  pstat |= (3);
 return (pstat);
}

variable_types: pstat int


decompiled_code:
__int64 __fastcall wiki_save_generated(const char *a1, char *a2)
{
size_t v2; // rax
char v4[4104]; // [rsp+0h] [rbp-1008h] BYREF

snprintf(v4, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
v2 = strlen(a2);
return file_set_contents(v4, a2, v2);
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, char *a2)
{
size_t v2; // rax
char v4[4104]; // [rsp+0h] [rbp-1008h] BYREF

snprintf(v4, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
v2 = strlen(a2);
return func1(v4, a2, v2);
}

original_code:
int
wiki_save_generated(const char *page, const char *contents)
{
 char filename[4096];
 snprintf(filename, sizeof filename, "%s/%s/generated.html",
     config.contents_dir, page);
 return file_set_contents(filename, contents, strlen(contents));
}

canonical_original_code:
int wiki_save_generated(char *page, char *contents)
{
 char filename[4096];
 snprintf(filename, sizeof filename, "%s/%s/generated.html",
     config.contents_dir, page);
 return file_set_contents(filename, contents, strlen(contents));
}

variable_types:
contents
char *

variable_types:
filename
char [4096]


decompiled_code:
__int64 __fastcall wiki_load_generated(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-1010h] BYREF
char s[4104]; // [rsp+10h] [rbp-1008h] BYREF

snprintf(s, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
file_get_contents(s, &v2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-1010h] BYREF
char s[4104]; // [rsp+10h] [rbp-1008h] BYREF

snprintf(s, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
func2(s, &v2);
return v2;
}

original_code:
char *
wiki_load_generated(const char *page)
{
 char filename[4096];
 char *result;
 snprintf(filename, sizeof filename, "%s/%s/generated.html",
     config.contents_dir, page);
 file_get_contents(filename, &result);
 return (result);
}

canonical_original_code:
char *wiki_load_generated(char *page)
{
 char filename[4096];
 char *result;
 snprintf(filename, sizeof filename, "%s/%s/generated.html",
     config.contents_dir, page);
 file_get_contents(filename, &result);
 return (result);
}

variable_types:
result
char *

return_type: char *


decompiled_code:
__int64 __fastcall wiki_save_data(const char *a1, char *a2, int a3)
{
int v3; // ebx
struct stat v5; // [rsp+0h] [rbp-20A8h] BYREF
char s[4096]; // [rsp+90h] [rbp-2018h] BYREF
char newa[4120]; // [rsp+1090h] [rbp-1018h] BYREF

v3 = a3;
if ( a3 == -1 )
v3 = strlen(a2);
snprintf(s, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
stat(s, &v5);
snprintf(newa, 0x1000uLL, "%s/%s/%ld", *((const char **)&config + 2), a1, v5.st_mtim.tv_sec);
rename(s, newa);
fprintf(stderr, "saving file %s\n", s);
return file_set_contents(s, a2, v3);
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, char *a2, int a3)
{
int v3; // ebx
struct stat v5; // [rsp+0h] [rbp-20A8h] BYREF
char s[4096]; // [rsp+90h] [rbp-2018h] BYREF
char newa[4120]; // [rsp+1090h] [rbp-1018h] BYREF

v3 = a3;
if ( a3 == -1 )
v3 = strlen(a2);
snprintf(s, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
stat(s, &v5);
snprintf(newa, 0x1000uLL, "%s/%s/%ld", *((const char **)&config + 2), a1, v5.st_mtim.tv_sec);
rename(s, newa);
fprintf(stderr, "saving file %s\n", s);
return func1(s, a2, v3);
}

original_code:
int
wiki_save_data(const char *page, const char *data, int len)
{
 char filename[4096];
 char new_file[4096];
 struct stat st;
 if (len == -1)
  len = strlen(data);
 snprintf(filename, sizeof filename, "%s/%s/latest",
     config.contents_dir, page);
 stat(filename, &st);
 snprintf(new_file, sizeof new_file, "%s/%s/%ld",
     config.contents_dir, page, st.st_mtim.tv_sec);
 rename(filename, new_file);
 fprintf(stderr, "saving file %s\n", filename);
 return file_set_contents(filename, data, len);
}

canonical_original_code:
int wiki_save_data(char *page, char *data, int len)
{
 char filename[4096];
 char new_file[4096];
 struct stat st;
 if (len == -1)
  len = strlen(data);
 snprintf(filename, sizeof filename, "%s/%s/latest",
     config.contents_dir, page);
 stat(filename, &st);
 snprintf(new_file, sizeof new_file, "%s/%s/%ld",
     config.contents_dir, page, st.st_mtim.tv_sec);
 rename(filename, new_file);
 fprintf(stderr, "saving file %s\n", filename);
 return file_set_contents(filename, data, len);
}

variable_types:
data
char *

variable_types: len int

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
__int64 __fastcall wiki_load_data(const char *a1, _QWORD *a2)
{
char v3[4104]; // [rsp+0h] [rbp-1008h] BYREF

snprintf(v3, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
fprintf(stderr, "reading file %s\n", v3);
return file_get_contents(v3, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, _QWORD *a2)
{
char v3[4104]; // [rsp+0h] [rbp-1008h] BYREF

snprintf(v3, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
fprintf(stderr, "reading file %s\n", v3);
return func2(v3, a2);
}

original_code:
int
wiki_load_data(const char *page, char **result)
{
 char filename[4096];
 snprintf(filename, sizeof filename, "%s/%s/latest",
     config.contents_dir, page);
 fprintf(stderr, "reading file %s\n", filename);
 return file_get_contents(filename, result);
}

canonical_original_code:
int wiki_load_data(char *page, char **result)
{
 char filename[4096];
 snprintf(filename, sizeof filename, "%s/%s/latest",
     config.contents_dir, page);
 fprintf(stderr, "reading file %s\n", filename);
 return file_get_contents(filename, result);
}

variable_types:
result
char **


decompiled_code:
__int64 __fastcall wiki_get_data_filename(const char *a1)
{
return printf_strdup("%s/%s/latest", *((const char **)&config + 2), a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
return printf_strdup("%s/%s/latest", *((const char **)&config + 2), a1);
}

original_code:
char *
wiki_get_data_filename(const char *page)
{
 char *filename;
 filename = printf_strdup("%s/%s/latest", config.contents_dir, page);
 return (filename);
}

canonical_original_code:
char *wiki_get_data_filename(char *page)
{
 char *filename;
 filename = printf_strdup("%s/%s/latest", config.contents_dir, page);
 return (filename);
}

variable_types:
filename
char *


decompiled_code:
__int64 __fastcall wiki_get_generated_filename(const char *a1)
{
return printf_strdup("%s/%s/generated.html", *((const char **)&config + 2), a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
return printf_strdup("%s/%s/generated.html", *((const char **)&config + 2), a1);
}

original_code:
char *
wiki_get_generated_filename(const char *page)
{
 char *filename;
 filename = printf_strdup("%s/%s/generated.html", config.contents_dir,
     page);
 return (filename);
}

canonical_original_code:
char *wiki_get_generated_filename(char *page)
{
 char *filename;
 filename = printf_strdup("%s/%s/generated.html", config.contents_dir,
     page);
 return (filename);
}


isescaped

decompiled_code:
__int64 __fastcall isescaped(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

original_code:
static int
isescaped(const char *sp, const char *p, int esc)
{
 const char *cp;
 size_t ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

canonical_original_code:
static int isescaped(char *sp, char *p, int esc)
{
 char *cp;
 unsigned long ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

variable_types:
sp
char *

variable_types:
p
char *

variable_types: esc int

variable_types:
cp
char *

variable_types:
ne
unsigned long


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 char *d = dst;
 char *s = src;
 unsigned long n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long

return_type: unsigned long


strtonum

decompiled_code:
__int64 __fastcall strtonum(char *nptr, __int64 a2, __int64 a3, _QWORD *a4)
{
int *v6; // rax
int v7; // er15
int *v8; // rbx
int v9; // edx
__int64 v10; // rax
__int64 v11; // r8
char *endptr[16]; // [rsp+8h] [rbp-80h] BYREF

endptr[3] = "invalid";
endptr[5] = "too small";
endptr[7] = "too large";
v6 = _errno_location();
v7 = *v6;
v8 = v6;
*v6 = 0;
if ( a2 > a3 || (v11 = strtoll(nptr, endptr, 10), endptr[0] == nptr) || *endptr[0] )
{
v9 = 22;
v10 = 1LL;
goto LABEL_3;
}
if ( v11 == 0x8000000000000000LL )
{
v9 = *v8;
if ( *v8 == 34 )
{
LABEL_19:
v10 = 2LL;
goto LABEL_3;
}
if ( a2 == 0x8000000000000000LL )
goto LABEL_12;
LABEL_18:
v9 = 34;
goto LABEL_19;
}
if ( a2 > v11 )
goto LABEL_18;
if ( v11 == 0x7FFFFFFFFFFFFFFFLL )
{
v9 = *v8;
if ( *v8 == 34 )
{
v10 = 3LL;
LABEL_3:
if ( a4 )
{
v11 = 0LL;
*a4 = endptr[2 * v10 + 1];
*v8 = v9;
}
else
{
*v8 = v9;
v11 = 0LL;
}
return v11;
}
}
v9 = 34;
v10 = 3LL;
if ( a3 < v11 )
goto LABEL_3;
LABEL_12:
if ( a4 )
*a4 = 0LL;
*v8 = v7;
return v11;
}

original_code:
long long
strtonum(const char *numstr, long long minval, long long maxval,
    const char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval {
  const char *errstr;
  int err;
 } ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

canonical_original_code:
long long strtonum(char *numstr, long long minval, long long maxval, char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

variable_types:
numstr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstrp
char **

variable_types:
ll
long long

variable_types:
ep
char *

variable_types: error int

variable_types: struct errval { char *errstr; int err; } [4]

return_type: long long


original_code:
int
scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i, ndigits = 0, fract_digits = 0;
 long long scale_fact = 1, whole = 0, fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short int) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

canonical_original_code:
int scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i; unsigned int ndigits = 0; unsigned int fract_digits = 0;
 long long scale_fact = 1; long long whole = 0; long long fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

variable_types:
scaled
char *

variable_types:
result
long long *

variable_types:
p
char *

variable_types: sign int

variable_types:
i
unsigned int

variable_types:
ndigits
unsigned int

variable_types:
whole
long long

variable_types:
fpart
long long


original_code:
int
fmt_scaled(long long number, char *result)
{
 long long abval, fract = 0;
 unsigned int i;
 unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

canonical_original_code:
int fmt_scaled(long long number, char *result)
{
 long long abval; long long fract = 0;
 unsigned int i;
 enum unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

variable_types:
number
long long

variable_types:
result
char *

variable_types:
abval
long long

variable_types:
fract
long long

variable_types: enum unit_type { NONE=0, KILO=1, MEGA=2, GIGA=3, TERA=4, PETA=5, EXA=6 }


decompiled_code:
void *__fastcall mime_get_key(size_t a1, const char *a2)
{
void *v2; // r12
char v3; // al
char *v4; // rbp
char v5; // cl
char *v6; // rax
char v7; // dl
int v8; // eax
size_t v9; // rbx
size_t v10; // rdi

while ( 1 )
{
v2 = strstr((const char *)a1, a2);
if ( !v2 )
break;
a1 = (size_t)v2 + strlen(a2);
if ( *(_BYTE *)a1 == 61 )
{
v3 = *(_BYTE *)(a1 + 1);
if ( v3 == 34 )
{
v3 = *(_BYTE *)(a1 + 2);
v4 = (char *)(a1 + 2);
v5 = 34;
}
else
{
v4 = (char *)(a1 + 1);
v5 = 0;
}
if ( !v3 || v5 == v3 )
{
v9 = 0LL;
v10 = 1LL;
}
else
{
v6 = v4;
do
v7 = *++v6;
while ( v7 != v5 && v7 );
v8 = (_DWORD)v6 - (_DWORD)v4;
v9 = v8;
v10 = v8 + 1;
}
v2 = malloc(v10);
memcpy(v2, v4, v9);
*((_BYTE *)v2 + v9) = 0;
return v2;
}
}
return v2;
}

original_code:
char *
mime_get_key(const char *buf, const char *key)
{
 char *ptr, *st_ptr;
 char *value;
 char end_ch;
 int len;
 end_ch = '\0';
 value = ((void *)0);
 ptr = (char *)buf;
 for (;;) {
  ptr = strstr(ptr, key);
  if (ptr == ((void *)0))
   break;
  ptr += strlen(key);
  if (*ptr != '=')
   continue;
  ptr ++;
  if (*ptr == '"') {
   end_ch = '"';
   ptr ++;
  }
  st_ptr = ptr;
  for (; *ptr != end_ch && *ptr != '\0'; ptr ++);
  len = ptr - st_ptr;
  value = malloc(len+1);
  memcpy(value, st_ptr, len);
  value[len] = '\0';
  break;
 }
 return (value);
}

canonical_original_code:
char *mime_get_key(char *buf, char *key)
{
 char *ptr; char *st_ptr;
 char *value;
 char end_ch;
 int len;
 end_ch = '\0';
 value = ((void *)0);
 ptr = (char *)buf;
 for (;;) {
  ptr = strstr(ptr, key);
  if (ptr == ((void *)0))
   break;
  ptr += strlen(key);
  if (*ptr != '=')
   continue;
  ptr ++;
  if (*ptr == '"') {
   end_ch = '"';
   ptr ++;
  }
  st_ptr = ptr;
  for (; *ptr != end_ch && *ptr != '\0'; ptr ++);
  len = ptr - st_ptr;
  value = malloc(len+1);
  memcpy(value, st_ptr, len);
  value[len] = '\0';
  break;
 }
 return (value);
}

variable_types:
buf
char *

variable_types:
key
char *

variable_types:
ptr
char *

variable_types:
value
char *

variable_types: len int


decompiled_code:
char *mime_getline()
{
int v0; // er13
__int64 v1; // rbp
char *v2; // r12
int v3; // ebx
char *v4; // rax

v0 = 1024;
v1 = 0LL;
v2 = (char *)malloc(0x400uLL);
while ( 1 )
{
v3 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
free(v2);
return 0LL;
}
v4 = &v2[v1];
if ( v3 == 10 || v3 == 13 )
break;
*v4 = v3;
if ( v0 == (_DWORD)v1 + 1 )
{
v0 = v1 + 1025;
v2 = (char *)realloc(v2, (int)v1 + 1025);
}
++v1;
}
*v4 = 0;
return v2;
}

canonical_decompiled_code:
char *func1()
{
int v0; // er13
__int64 v1; // rbp
char *v2; // r12
int v3; // ebx
char *v4; // rax

v0 = 1024;
v1 = 0LL;
v2 = (char *)malloc(0x400uLL);
while ( 1 )
{
v3 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
free(v2);
return 0LL;
}
v4 = &v2[v1];
if ( v3 == 10 || v3 == 13 )
break;
*v4 = v3;
if ( v0 == (_DWORD)v1 + 1 )
{
v0 = v1 + 1025;
v2 = (char *)realloc(v2, (int)v1 + 1025);
}
++v1;
}
*v4 = 0;
return v2;
}

original_code:
char *
mime_getline()
{
 char *buf;
 int bufsz;
 int ch, nb;
 bufsz = 1024;
 nb = 0;
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   free(buf);
   return (((void *)0));
  }
  if (ch == '\n' || ch == '\r')
   break;
  buf[nb++] = ch;
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb] = '\0';
 return (buf);
}

canonical_original_code:
char *mime_getline()
{
 char *buf;
 int bufsz;
 int ch; int nb;
 bufsz = 1024;
 nb = 0;
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   free(buf);
   return (((void *)0));
  }
  if (ch == '\n' || ch == '\r')
   break;
  buf[nb++] = ch;
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb] = '\0';
 return (buf);
}

variable_types: bufsz int

variable_types: ch int

variable_types: nb int


decompiled_code:
int __fastcall mime_getdata(const char *a1, char **a2, _DWORD *a3)
{
int v3; // er14
__int64 v4; // rbx
size_t v5; // rax
size_t v6; // r13
char *v7; // r12
char v8; // bp
int v9; // eax
char *v10; // rdx
int v11; // er15
const char *v12; // rbp
int result; // eax
int v14; // [rsp+Ch] [rbp-14Ch]
char s[128]; // [rsp+20h] [rbp-138h] BYREF
char s2[184]; // [rsp+A0h] [rbp-B8h] BYREF

v3 = 1024;
v4 = 0LL;
snprintf(s, 0x80uLL, "\r\n%s\r\n", a1);
snprintf(s2, 0x80uLL, "\r\n%s--", a1);
v5 = strlen(s);
v14 = v5;
v6 = v5;
v7 = (char *)malloc(0x400uLL);
while ( 1 )
{
v8 = webserver_getc();
v9 = webserver_eof();
v10 = &v7[v4];
if ( v9 )
{
*v10 = 0;
printf("buf: %s\n", v7);
free(v7);
return -1;
}
*v10 = v8;
v11 = v4 + 1;
if ( v14 < (int)v4 + 1 )
{
v12 = &v7[v4 + 1 - v6];
result = strncmp(v12, s, v6);
if ( !result )
goto LABEL_11;
if ( !strncmp(v12, s2, v6) )
break;
}
if ( v3 == v11 )
{
v3 += 1024;
v7 = (char *)realloc(v7, v3);
}
++v4;
}
result = 1;
LABEL_11:
*v12 = 0;
*a2 = v7;
*a3 = v11;
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, char **a2, _DWORD *a3)
{
int v3; // er14
__int64 v4; // rbx
size_t v5; // rax
size_t v6; // r13
char *v7; // r12
char v8; // bp
int v9; // eax
char *v10; // rdx
int v11; // er15
const char *v12; // rbp
int result; // eax
int v14; // [rsp+Ch] [rbp-14Ch]
char s[128]; // [rsp+20h] [rbp-138h] BYREF
char s2[184]; // [rsp+A0h] [rbp-B8h] BYREF

v3 = 1024;
v4 = 0LL;
snprintf(s, 0x80uLL, "\r\n%s\r\n", a1);
snprintf(s2, 0x80uLL, "\r\n%s--", a1);
v5 = strlen(s);
v14 = v5;
v6 = v5;
v7 = (char *)malloc(0x400uLL);
while ( 1 )
{
v8 = webserver_getc();
v9 = webserver_eof();
v10 = &v7[v4];
if ( v9 )
{
*v10 = 0;
printf("buf: %s\n", v7);
free(v7);
return -1;
}
*v10 = v8;
v11 = v4 + 1;
if ( v14 < (int)v4 + 1 )
{
v12 = &v7[v4 + 1 - v6];
result = strncmp(v12, s, v6);
if ( !result )
goto LABEL_11;
if ( !strncmp(v12, s2, v6) )
break;
}
if ( v3 == v11 )
{
v3 += 1024;
v7 = (char *)realloc(v7, v3);
}
++v4;
}
result = 1;
LABEL_11:
*v12 = 0;
*a2 = v7;
*a3 = v11;
return result;
}

original_code:
int
mime_getdata(const char *boundary, char **result, int *result_sz)
{
 char *buf;
 int bufsz;
 char next_boundary[128];
 char end_boundary[128];
 int boundary_len;
 int nb, ch, retval;
 snprintf(next_boundary, sizeof next_boundary, "\r\n%s\r\n", boundary);
 snprintf(end_boundary, sizeof end_boundary, "\r\n%s--", boundary);
 bufsz = 1024;
 nb = 0;
 boundary_len = strlen(next_boundary);
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   buf[nb] = '\0';
   printf("buf: %s\n",buf);
   free(buf);
   return (-1);
  }
  buf[nb++] = ch;
  if (nb > boundary_len) {
   if (strncmp(buf + nb - boundary_len,
    next_boundary, boundary_len) == 0) {
    retval = 0;
    break;
   } else if (strncmp(buf + nb - boundary_len,
     end_boundary, boundary_len) == 0) {
    retval = 1;
    break;
   }
  }
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb - boundary_len] = '\0';
 *result = buf;
 *result_sz = nb;
 return (retval);
}

canonical_original_code:
int mime_getdata(char *boundary, char **result, int *result_sz)
{
 char *buf;
 int bufsz;
 char next_boundary[128];
 char end_boundary[128];
 int boundary_len;
 int nb; int ch; int retval;
 snprintf(next_boundary, sizeof next_boundary, "\r\n%s\r\n", boundary);
 snprintf(end_boundary, sizeof end_boundary, "\r\n%s--", boundary);
 bufsz = 1024;
 nb = 0;
 boundary_len = strlen(next_boundary);
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   buf[nb] = '\0';
   printf("buf: %s\n",buf);
   free(buf);
   return (-1);
  }
  buf[nb++] = ch;
  if (nb > boundary_len) {
   if (strncmp(buf + nb - boundary_len,
    next_boundary, boundary_len) == 0) {
    retval = 0;
    break;
   } else if (strncmp(buf + nb - boundary_len,
     end_boundary, boundary_len) == 0) {
    retval = 1;
    break;
   }
  }
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb - boundary_len] = '\0';
 *result = buf;
 *result_sz = nb;
 return (retval);
}

variable_types:
boundary
char *

variable_types:
result
char **

variable_types: retval int


decompiled_code:
__int64 __fastcall mime_parse(__int64 *a1, const char *a2)
{
void *v2; // rsp
char *v3; // r14
char *v4; // r13
char *v5; // rax
const char *v6; // rbx
size_t v7; // r12
char *v8; // rax
char *v9; // r12
const char *v10; // r12
size_t v11; // rsi
int v12; // er13
_QWORD *v13; // rax
char *v14; // rdx
__int64 *v15; // rcx
__int64 v16; // rdx
__int64 *v17; // rcx
__int64 *v19; // [rsp+0h] [rbp-60h] BYREF
char *v20; // [rsp+8h] [rbp-58h]
int v21; // [rsp+1Ch] [rbp-44h] BYREF
char *haystack; // [rsp+20h] [rbp-40h] BYREF
char *v23; // [rsp+28h] [rbp-38h] BYREF

v19 = a1;
v2 = alloca(strlen(a2) + 11);
sprintf((char *)&v19, "--%s", a2);
do
{
if ( mime_getdata((const char *)&unk_4FF, &haystack, &v21) == -1 )
{
LODWORD(v20) = -1;
perror("mime_parse()");
return (unsigned int)v20;
}
v3 = 0LL;
printf("Headers: %s\n", haystack);
v4 = haystack;
v5 = strstr(haystack, "Content-Disposition: ");
v6 = v5;
if ( v5 )
{
v7 = (size_t)(v5 + 21);
v6 = (const char *)mime_get_key((size_t)(v5 + 21), "name");
v3 = (char *)mime_get_key(v7, "filename");
printf("name: %s\n", v6);
printf("filename: %s\n", v3);
v4 = haystack;
}
v8 = strstr(v4, "Content-Type: ");
v9 = v8;
if ( v8 )
{
v10 = v8 + 14;
v21 = (unsigned int)strstr(v8 + 14, "\r\n") - ((_DWORD)v8 + 14);
v11 = v21;
if ( v21 < 0 )
{
v21 = strlen(v10);
v11 = v21;
}
v9 = strndup(v10, v11);
}
free(v4);
puts("End of headers");
v12 = mime_getdata((const char *)&v19, &v23, &v21);
if ( v12 == -1 )
{
perror("mime_getdata():");
return 0LL;
}
printf("data: %s\n", v23);
v20 = v23;
if ( v23 )
{
v13 = malloc(0x28uLL);
v14 = v20;
v15 = v19;
v13[2] = v6;
v13[3] = v14;
*((_DWORD *)v13 + 8) = v21;
v16 = *v15;
*v13 = *v15;
if ( v16 )
*(_QWORD *)(v16 + 8) = v13;
v17 = v19;
*v19 = (__int64)v13;
v13[1] = v17;
}
if ( v3 )
free(v3);
if ( v9 )
free(v9);
}
while ( v12 != 1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, const char *a2)
{
void *v2; // rsp
char *v3; // r14
char *v4; // r13
char *v5; // rax
const char *v6; // rbx
size_t v7; // r12
char *v8; // rax
char *v9; // r12
const char *v10; // r12
size_t v11; // rsi
int v12; // er13
_QWORD *v13; // rax
char *v14; // rdx
__int64 *v15; // rcx
__int64 v16; // rdx
__int64 *v17; // rcx
__int64 *v19; // [rsp+0h] [rbp-60h] BYREF
char *v20; // [rsp+8h] [rbp-58h]
int v21; // [rsp+1Ch] [rbp-44h] BYREF
char *haystack; // [rsp+20h] [rbp-40h] BYREF
char *v23; // [rsp+28h] [rbp-38h] BYREF

v19 = a1;
v2 = alloca(strlen(a2) + 11);
sprintf((char *)&v19, "--%s", a2);
do
{
if ( func2((const char *)&unk_4FF, &haystack, &v21) == -1 )
{
LODWORD(v20) = -1;
perror("mime_parse()");
return (unsigned int)v20;
}
v3 = 0LL;
printf("Headers: %s\n", haystack);
v4 = haystack;
v5 = strstr(haystack, "Content-Disposition: ");
v6 = v5;
if ( v5 )
{
v7 = (size_t)(v5 + 21);
v6 = (const char *)func0((size_t)(v5 + 21), "name");
v3 = (char *)func0(v7, "filename");
printf("name: %s\n", v6);
printf("filename: %s\n", v3);
v4 = haystack;
}
v8 = strstr(v4, "Content-Type: ");
v9 = v8;
if ( v8 )
{
v10 = v8 + 14;
v21 = (unsigned int)strstr(v8 + 14, "\r\n") - ((_DWORD)v8 + 14);
v11 = v21;
if ( v21 < 0 )
{
v21 = strlen(v10);
v11 = v21;
}
v9 = strndup(v10, v11);
}
free(v4);
puts("End of headers");
v12 = func2((const char *)&v19, &v23, &v21);
if ( v12 == -1 )
{
perror("mime_getdata():");
return 0LL;
}
printf("data: %s\n", v23);
v20 = v23;
if ( v23 )
{
v13 = malloc(0x28uLL);
v14 = v20;
v15 = v19;
v13[2] = v6;
v13[3] = v14;
*((_DWORD *)v13 + 8) = v21;
v16 = *v15;
*v13 = *v15;
if ( v16 )
*(_QWORD *)(v16 + 8) = v13;
v17 = v19;
*v19 = (__int64)v13;
v13[1] = v17;
}
if ( v3 )
free(v3);
if ( v9 )
free(v9);
}
while ( v12 != 1 );
return 0LL;
}

original_code:
int
mime_parse(struct cgi_var_list *cgi_list, const char *boundary)
{
 char *part_name,
    *part_filename,
    *part_type;
 int len;
 struct cgi_var *cv;
 char *headers;
 char *ptr;
 char *data;
 int ret;
 char *actual_boundary;
 actual_boundary = __builtin_alloca (strlen(boundary)+3);
 sprintf(actual_boundary, "--%s", boundary);
 for (;;) {
  part_name = ((void *)0);
  part_filename = ((void *)0);
  part_type = ((void *)0);
  ret = mime_getdata("", &headers, &len);
  if (ret == -1) {
   perror("mime_parse()");
   return (-1);
  }
  printf("Headers: %s\n", headers);
  if ((ptr = strstr(headers, "Content-Disposition: ")) != ((void *)0)) {
   ptr += strlen("Content-Disposition: ");
   part_name = mime_get_key(ptr, "name");
   part_filename = mime_get_key(ptr, "filename");
   printf("name: %s\n", part_name);
   printf("filename: %s\n", part_filename);
  }
  if ((ptr = strstr(headers, "Content-Type: ")) != ((void *)0)) {
   ptr += strlen("Content-Type: ");
   len = strstr(ptr, "\r\n") - ptr;
   if (len < 0)
    len = strlen(ptr);
   part_type = strndup(ptr, len);
  }
  free(headers);
  printf("End of headers\n");
  ret = mime_getdata(actual_boundary, &data, &len);
  if (ret == -1) {
   perror("mime_getdata():");
   return (0);
  }
  printf("data: %s\n", data);
  if (data != ((void *)0)) {
   cv = malloc(sizeof *cv);
   cv->name = part_name;
   cv->value = data;
   cv->value_len = len;
   do { if (((cv)->entry.le_next = (cgi_list)->lh_first) != ((void *)0)) (cgi_list)->lh_first->entry.le_prev = &(cv)->entry.le_next; (cgi_list)->lh_first = (cv); (cv)->entry.le_prev = &(cgi_list)->lh_first; } while ( 0);
  }
  if (part_filename)
   free(part_filename);
  if (part_type)
   free(part_type);
  if (ret == 1)
   break;
 }
 return (0);
}

canonical_original_code:
int mime_parse(struct cgi_var_list *cgi_list, char *boundary)
{
 char *part_name; char *part_filename; char *part_type;
 int len;
 struct cgi_var *cv;
 char *headers;
 char *ptr;
 char *data;
 int ret;
 char *actual_boundary;
 actual_boundary = __builtin_alloca (strlen(boundary)+3);
 sprintf(actual_boundary, "--%s", boundary);
 for (;;) {
  part_name = ((void *)0);
  part_filename = ((void *)0);
  part_type = ((void *)0);
  ret = mime_getdata("", &headers, &len);
  if (ret == -1) {
   perror("mime_parse()");
   return (-1);
  }
  printf("Headers: %s\n", headers);
  if ((ptr = strstr(headers, "Content-Disposition: ")) != ((void *)0)) {
   ptr += strlen("Content-Disposition: ");
   part_name = mime_get_key(ptr, "name");
   part_filename = mime_get_key(ptr, "filename");
   printf("name: %s\n", part_name);
   printf("filename: %s\n", part_filename);
  }
  if ((ptr = strstr(headers, "Content-Type: ")) != ((void *)0)) {
   ptr += strlen("Content-Type: ");
   len = strstr(ptr, "\r\n") - ptr;
   if (len < 0)
    len = strlen(ptr);
   part_type = strndup(ptr, len);
  }
  free(headers);
  printf("End of headers\n");
  ret = mime_getdata(actual_boundary, &data, &len);
  if (ret == -1) {
   perror("mime_getdata():");
   return (0);
  }
  printf("data: %s\n", data);
  if (data != ((void *)0)) {
   cv = malloc(sizeof *cv);
   cv->name = part_name;
   cv->value = data;
   cv->value_len = len;
   do { if (((cv)->entry.le_next = (cgi_list)->lh_first) != ((void *)0)) (cgi_list)->lh_first->entry.le_prev = &(cv)->entry.le_next; (cgi_list)->lh_first = (cv); (cv)->entry.le_prev = &(cgi_list)->lh_first; } while ( 0);
  }
  if (part_filename)
   free(part_filename);
  if (part_type)
   free(part_type);
  if (ret == 1)
   break;
 }
 return (0);
}

variable_types: struct cgi_var_list { struct cgi_var { struct { struct cgi_var *le_next; struct cgi_var **le_prev; } entry; char *name; char *value; int value_len; } *lh_first; } *

variable_types:
headers
char *

variable_types:
data
char *

variable_types: ret int

user_defined_types: struct cgi_var_list { struct cgi_var *lh_first; }


repo: zPatrickz/Game

decompiled_code:
unsigned __int64 init_game()
{
unsigned __int64 result; // rax

result = (unsigned int)((_DWORD)pool + 40);
do
{
*(_QWORD *)(result - 8) = result;
result += 40LL;
}
while ( (_QWORD *)result != &pool[5000] );
pool[4999] = 0LL;
top = (__int64)pool;
active = 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 func2()
{
unsigned __int64 result; // rax

result = (unsigned int)((_DWORD)pool + 40);
do
{
*(_QWORD *)(result - 8) = result;
result += 40LL;
}
while ( (_QWORD *)result != &pool[5000] );
pool[4999] = 0LL;
top = (__int64)pool;
active = 0LL;
return result;
}

original_code:
void
init_game(void) {
 int i;
 for (i = 0; i < 1000 - 1; i ++) {
  pool[i].next = pool + (i + 1);
 }
 pool[1000 - 1].next = ((void*)0);
 top = pool;
 active = ((void*)0);
}

canonical_original_code:
void init_game() {
 int i;
 for (i = 0; i < 1000 - 1; i ++) {
  pool[i].next = pool + (i + 1);
 }
 pool[1000 - 1].next = ((void *)0);
 top = pool;
 active = ((void *)0);
}

variable_types: i int


original_code:
void
key_stroke(int code) {
 struct Firework *ptr;
 int i;
 int x = rand() % 100 + 50;
 int y = rand() % 160 + 80;
 int color = rand() % 256 & 0x6c;
 if (code > 128) return;
 for (i = 0; i < 10; i ++) {
  if (top == ((void*)0)) {
   return;
  }
  ptr = top;
  top = top->next;
  ptr->x = x;
  ptr->y = y;
  ptr->vx = vx[i];
  ptr->vy = vy[i];
  ptr->color = color;
  ptr->timestamp = 0;
  ptr->next = active;
  ptr->prev = ((void*)0);
  if (active != ((void*)0)) active->prev = ptr;
  active = ptr;
 }
}

canonical_original_code:
void key_stroke(int code) {
 struct Firework *ptr;
 int i;
 int x = rand() % 100 + 50;
 int y = rand() % 160 + 80;
 int color = rand() % 256 & 0x6c;
 if (code > 128) return;
 for (i = 0; i < 10; i ++) {
  if (top == ((void *)0)) {
   return;
  }
  ptr = top;
  top = top->next;
  ptr->x = x;
  ptr->y = y;
  ptr->vx = vx[i];
  ptr->vy = vy[i];
  ptr->color = color;
  ptr->timestamp = 0;
  ptr->next = active;
  ptr->prev = ((void *)0);
  if (active != ((void *)0)) active->prev = ptr;
  active = ptr;
 }
}

variable_types: code int

variable_types: struct Firework { int x; float vx; int color; int timestamp; struct Firework *prev; } *

variable_types: x int

variable_types: y int

variable_types: color int


original_code:
void
start_frame(void) {
 static int count = 0;
 struct Firework *ptr;
 int i;
 count = (count + 1) % 50;
 if (count == 0) {
  key_stroke(0);
 }
 for (ptr = active; ptr != ((void*)0); ptr = ptr->next) {
  for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
   draw_pixel( get_x(ptr, i), get_y(ptr, i), 0 );
  }
 }
 ptr = active;
 while (ptr != ((void*)0)) {
  struct Firework *prev = ptr->prev, *next = ptr->next;
  ptr->timestamp ++;
  if (ptr->timestamp > 200) {
   if (prev != ((void*)0)) prev->next = next;
   if (next != ((void*)0)) next->prev = prev;
   if (prev == ((void*)0) && next == ((void*)0)) active = ((void*)0);
   ptr->next = top;
   ptr->prev = ((void*)0);
   top = ptr;
  } else {
   for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
    draw_pixel( get_x(ptr, i), get_y(ptr, i), ptr->color );
   }
  }
  ptr = next;
 }
}

canonical_original_code:
void start_frame() {
 static int count = 0;
 struct Firework *ptr;
 int i;
 count = (count + 1) % 50;
 if (count == 0) {
  key_stroke(0);
 }
 for (ptr = active; ptr != ((void *)0); ptr = ptr->next) {
  for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
   draw_pixel( get_x(ptr, i), get_y(ptr, i), 0 );
  }
 }
 ptr = active;
 while (ptr != ((void *)0)) {
  struct Firework *prev = ptr->prev; struct Firework *next = ptr->next;
  ptr->timestamp ++;
  if (ptr->timestamp > 200) {
   if (prev != ((void *)0)) prev->next = next;
   if (next != ((void *)0)) next->prev = prev;
   if (prev == ((void *)0) && next == ((void *)0)) active = ((void *)0);
   ptr->next = top;
   ptr->prev = ((void *)0);
   top = ptr;
  } else {
   for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
    draw_pixel( get_x(ptr, i), get_y(ptr, i), ptr->color );
   }
  }
  ptr = next;
 }
}

variable_types: count int

user_defined_types: struct Firework { int x; float vx; int color; int timestamp; struct Firework *prev; }