decompiled_code:
size_t __fastcall list_ins(__int64 a1, int a2, int a3)
{
int v4; // er13
size_t result; // rax
_QWORD *v6; // rax
_QWORD *v7; // r12
__int64 v8; // rax

if ( a2 <= 0 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v4 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 + 1 < a2 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v6 = malloc(0x10uLL);
v7 = v6;
if ( v6 )
{
*(_DWORD *)v6 = a3;
if ( v4 >= a2 )
{
v8 = get_node(a1, a2);
if ( a2 == 1 )
{
v7[1] = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v7;
}
else
{
v7[1] = *(_QWORD *)(v8 + 8);
*(_QWORD *)(v8 + 8) = v7;
}
}
else
{
if ( !v4 )
{
*(_QWORD *)(a1 + 8) = v6;
*(_QWORD *)(a1 + 16) = v6;
}
*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL) = v6;
v6[1] = 0LL;
*(_QWORD *)(a1 + 16) = v6;
}
++*(_DWORD *)a1;
result = 0LL;
}
else
{
fwrite("malloc function error\n", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func9(__int64 a1, int a2, int a3)
{
int v4; // er13
size_t result; // rax
_QWORD *v6; // rax
_QWORD *v7; // r12
__int64 v8; // rax

if ( a2 <= 0 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v4 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 + 1 < a2 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v6 = malloc(0x10uLL);
v7 = v6;
if ( v6 )
{
*(_DWORD *)v6 = a3;
if ( v4 >= a2 )
{
v8 = func8(a1, a2);
if ( a2 == 1 )
{
v7[1] = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v7;
}
else
{
v7[1] = *(_QWORD *)(v8 + 8);
*(_QWORD *)(v8 + 8) = v7;
}
}
else
{
if ( !v4 )
{
*(_QWORD *)(a1 + 8) = v6;
*(_QWORD *)(a1 + 16) = v6;
}
*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL) = v6;
v6[1] = 0LL;
*(_QWORD *)(a1 + 16) = v6;
}
++*(_DWORD *)a1;
result = 0LL;
}
else
{
fwrite("malloc function error\n", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int list_ins(List *list, int pos, Datatype data)
{
 if (pos <=0 || pos > (list->size) + 1) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 ListNode *new_node;
 if ((new_node = (ListNode *)malloc(sizeof(ListNode))) == ((void *)0)) {
  fprintf(stderr, "malloc function error\n");
  return -1;
 }
 new_node->data = data;
 if (pos <= (list->size)) {
  ListNode *node = get_node(list, pos);
  if (pos == 1) {
   new_node->next = list->head;
   list->head = new_node;
  }
  else {
   new_node->next = node->next;
   node->next = new_node;
  }
 }
 else {
  if (list->size == 0) {
   list->head = new_node;
   list->tail = new_node;
   new_node->next = ((void *)0);
  }
  list->tail->next = new_node;
  new_node->next = ((void *)0);
  list->tail = new_node;
 }
 list->size++;
 return 0;
}

canonical_original_code:
int list_ins(struct List_ *list, int pos, int data)
{
 if (pos <=0 || pos > (list->size) + 1) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 struct ListNode_ *new_node;
 if ((new_node = (struct ListNode_ *)malloc(sizeof(ListNode))) == ((void *)0)) {
  fprintf(stderr, "malloc function error\n");
  return -1;
 }
 new_node->data = data;
 if (pos <= (list->size)) {
  struct ListNode_ *node = get_node(list, pos);
  if (pos == 1) {
   new_node->next = list->head;
   list->head = new_node;
  }
  else {
   new_node->next = node->next;
   node->next = new_node;
  }
 }
 else {
  if (list->size == 0) {
   list->head = new_node;
   list->tail = new_node;
   new_node->next = ((void *)0);
  }
  list->tail->next = new_node;
  new_node->next = ((void *)0);
  list->tail = new_node;
 }
 list->size++;
 return 0;
}

variable_types: data int


repo: ywllx/study

decompiled_code:
size_t __fastcall list_rem(__int64 a1, int a2, _DWORD *a3)
{
_QWORD *v4; // r12

if ( a2 <= 0 || *(_DWORD *)a1 < a2 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v4 = (_QWORD *)get_node(a1, a2);
*a3 = *(_DWORD *)v4;
if ( *(_QWORD **)(a1 + 8) == v4 )
{
*(_QWORD *)(a1 + 8) = v4[1];
if ( *(_DWORD *)a1 == 1 )
*(_QWORD *)(a1 + 16) = 0LL;
}
else
{
*(_QWORD *)(get_node(a1, a2 - 1) + 8) = v4[1];
}
free(v4);
--*(_DWORD *)a1;
return 0LL;
}

canonical_decompiled_code:
size_t __fastcall func10(__int64 a1, int a2, _DWORD *a3)
{
_QWORD *v4; // r12

if ( a2 <= 0 || *(_DWORD *)a1 < a2 )
return fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
v4 = (_QWORD *)func8(a1, a2);
*a3 = *(_DWORD *)v4;
if ( *(_QWORD **)(a1 + 8) == v4 )
{
*(_QWORD *)(a1 + 8) = v4[1];
if ( *(_DWORD *)a1 == 1 )
*(_QWORD *)(a1 + 16) = 0LL;
}
else
{
*(_QWORD *)(func8(a1, a2 - 1) + 8) = v4[1];
}
free(v4);
--*(_DWORD *)a1;
return 0LL;
}

original_code:
int list_rem(List *list, int pos, Datatype *data)
{
 if (pos <=0 || pos > (list->size)) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 if (list->size <= 0) {
  fprintf(stderr, "list size is less than 1)\n");
  return -1;
 }
 ListNode *node = get_node(list, pos);
 *data = node->data;
 if (node == list->head) {
  list->head = list->head->next;
  if (list->size == 1)
   list->tail = ((void *)0);
 }
 else {
  ListNode *prenode = get_node(list, pos-1);
  prenode->next = node->next;
  if (node->next = ((void *)0))
   list->tail = prenode;
 }
 free(node);
 list->size--;
 return 0;
}

canonical_original_code:
int list_rem(struct List_ *list, int pos, int *data)
{
 if (pos <=0 || pos > (list->size)) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 if (list->size <= 0) {
  fprintf(stderr, "list size is less than 1)\n");
  return -1;
 }
 struct ListNode_ *node = get_node(list, pos);
 *data = node->data;
 if (node == list->head) {
  list->head = list->head->next;
  if (list->size == 1)
   list->tail = ((void *)0);
 }
 else {
  struct ListNode_ *prenode = get_node(list, pos-1);
  prenode->next = node->next;
  if (node->next = ((void *)0))
   list->tail = prenode;
 }
 free(node);
 list->size--;
 return 0;
}

variable_types: struct List_ { int size; struct ListNode_ { int data; struct ListNode_ *next; } *head; struct ListNode_ *tail; } *

variable_types: pos int

variable_types:
data
int *

return_type: int

user_defined_types: struct List_ { int size; struct ListNode_ *head; struct ListNode_ *tail; }


decompiled_code:
size_t __fastcall stack_push(__int64 a1, int a2)
{
return list_ins(a1, 1, a2);
}

canonical_decompiled_code:
size_t __fastcall func11(__int64 a1, int a2)
{
return func9(a1, 1, a2);
}

original_code:
int stack_push(Stack *stack, Datatype data)
{
 return list_ins(stack, 1, data);
}

canonical_original_code:
int stack_push(struct List_ *stack, int data)
{
 return list_ins(stack, 1, data);
}

user_defined_types: struct ListNode_ { int data; struct ListNode_ *next; }


decompiled_code:
size_t __fastcall stack_pop(__int64 a1, _DWORD *a2)
{
return list_rem(a1, 1, a2);
}

canonical_decompiled_code:
size_t __fastcall func12(__int64 a1, _DWORD *a2)
{
return func10(a1, 1, a2);
}

original_code:
int stack_pop(Stack *stack, Datatype* data)
{
 return list_rem(stack, 1, data);
}

canonical_original_code:
int stack_pop(struct List_ *stack, int *data)
{
 return list_rem(stack, 1, data);
}


decompiled_code:
size_t __fastcall queue_enqueue(_DWORD *a1, int a2)
{
return list_ins((__int64)a1, *a1 + 1, a2);
}

canonical_decompiled_code:
size_t __fastcall func13(_DWORD *a1, int a2)
{
return func9((__int64)a1, *a1 + 1, a2);
}

original_code:
int queue_enqueue(Queue *queue, Datatype data)
{
 list_ins(queue, (queue->size)+1, data);
}

canonical_original_code:
int queue_enqueue(struct List_ *queue, int data)
{
 list_ins(queue, (queue->size)+1, data);
}


decompiled_code:
size_t __fastcall queue_dequeue(__int64 a1, _DWORD *a2)
{
return list_rem(a1, 1, a2);
}

canonical_decompiled_code:
size_t __fastcall func14(__int64 a1, _DWORD *a2)
{
return func10(a1, 1, a2);
}

original_code:
int queue_dequeue(Queue *queue, Datatype* data)
{
 list_rem(queue, 1, data);
}

canonical_original_code:
int queue_dequeue(struct List_ *queue, int *data)
{
 list_rem(queue, 1, data);
}


decompiled_code:
__mode_t init_daemon()
{
int v0; // ebx

if ( fork() )
exit(0);
setsid();
v0 = fork();
if ( v0 )
exit(0);
do
close(v0++);
while ( v0 != 256 );
chdir("./");
return umask(0);
}

canonical_decompiled_code:
__mode_t func0()
{
int v0; // ebx

if ( fork() )
exit(0);
setsid();
v0 = fork();
if ( v0 )
exit(0);
do
close(v0++);
while ( v0 != 256 );
chdir("./");
return umask(0);
}

original_code:
void init_daemon(void)
{
    int pid;
    int i;
    if (pid=fork())
        exit(0);
    else if (pid< 0)
        exit(1);
    setsid();
    if (pid=fork())
        exit(0);
    else if (pid< 0)
        exit(1);
    for (i=0;i< 256;++i)
        close(i);
    chdir("./");
    umask(0);
    return;
}

canonical_original_code:
void init_daemon()
{
    int pid;
    int i;
    if (pid=fork())
        exit(0);
    else if (pid< 0)
        exit(1);
    setsid();
    if (pid=fork())
        exit(0);
    else if (pid< 0)
        exit(1);
    for (i=0;i< 256;++i)
        close(i);
    chdir("./");
    umask(0);
    return;
}

variable_types: pid int

variable_types: i int

return_type: void


getTime

decompiled_code:
size_t __fastcall getTime(char *s)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v1 = localtime(&timer);
return strftime(s, 0x50uLL, "%Y-%m-%d %I:%M:%S ", v1);
}

canonical_decompiled_code:
size_t __fastcall func0(char *s)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v1 = localtime(&timer);
return strftime(s, 0x50uLL, "%Y-%m-%d %I:%M:%S ", v1);
}

original_code:
void getTime(char* tm)
{
 time_t timeval;
 struct tm* timeInfo;
 (void)time(&timeval);
 timeInfo = localtime(&timeval);
 strftime(tm, 80 , "%Y-%m-%d %I:%M:%S ", timeInfo );
}

canonical_original_code:
void getTime(char *tm)
{
 long timeval;
 struct tm *timeInfo;
 (void)time(&timeval);
 timeInfo = localtime(&timeval);
 strftime(tm, 80 , "%Y-%m-%d %I:%M:%S ", timeInfo );
}

variable_types:
tm
char *

variable_types: timeval long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


OutputHttpHeaders

decompiled_code:
__int64 __fastcall OutputHttpHeaders(unsigned int a1, __int64 a2)
{
size_t v2; // rax
char v4[120]; // [rsp+0h] [rbp-78h] BYREF

sprintf(v4, "HTTP/1.0 %d OK\r\nServer: WebServ v0.1\r\nContent-Type: text/html\r\n\r\n", *(unsigned int *)(a2 + 24));
v2 = strlen(v4);
WriteLine(a1, v4, v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2)
{
size_t v2; // rax
char v4[120]; // [rsp+0h] [rbp-78h] BYREF

sprintf(v4, "HTTP/1.0 %d OK\r\nServer: WebServ v0.1\r\nContent-Type: text/html\r\n\r\n", *(unsigned int *)(a2 + 24));
v2 = strlen(v4);
WriteLine(a1, v4, v2);
return 0LL;
}

original_code:
int OutputHttpHeaders(int conn, struct ReqInfo* reqinfo)
{
 char buffer[100];
 sprintf(buffer, "HTTP/1.0 %d OK\r\nServer: WebServ v0.1\r\nContent-Type: text/html\r\n\r\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
 return 0;
}

canonical_original_code:
int OutputHttpHeaders(int conn, struct ReqInfo *reqinfo)
{
 char buffer[100];
 sprintf(buffer, "HTTP/1.0 %d OK\r\nServer: WebServ v0.1\r\nContent-Type: text/html\r\n\r\n", reqinfo->status);
 WriteLine(conn, buffer, strlen(buffer));
 return 0;
}

variable_types: conn int

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

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


getDynamic

decompiled_code:
__int64 __fastcall getDynamic(int fd, __int64 a2)
{
char *v2; // rbx
char *argv; // [rsp+8h] [rbp-20h] BYREF

argv = 0LL;
v2 = strstr(*(const char **)(a2 + 16), "getAuth?");
puts("---execv getAuth----");
if ( !fork() )
{
setenv("QUERY_STRING", v2 + 8, 1);
dup2(fd, 1);
execve("cgi-bin/getAuth", &argv, environ);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2)
{
char *v2; // rbx
char *argv; // [rsp+8h] [rbp-20h] BYREF

argv = 0LL;
v2 = strstr(*(const char **)(a2 + 16), "getAuth?");
puts("---execv getAuth----");
if ( !fork() )
{
setenv("QUERY_STRING", v2 + 8, 1);
dup2(fd, 1);
execve("cgi-bin/getAuth", &argv, environ);
}
return 0LL;
}

original_code:
int getDynamic(int fd, struct ReqInfo* reqInfo)
{
 char* emptylist[] = { ((void *)0) };
 char* p = strstr(reqInfo->resource, "getAuth?");
 p += 8;
 printf("---execv getAuth----\n");
 if(fork() == 0){
  setenv("QUERY_STRING", p, 1);
  dup2(fd, 1);
  execve("cgi-bin/getAuth", emptylist, environ);
 }
 return 0;
}

canonical_original_code:
int getDynamic(int fd, struct ReqInfo *reqInfo)
{
 char *emptylist[] = { ((void *)0) };
 char *p = strstr(reqInfo->resource, "getAuth?");
 p += 8;
 printf("---execv getAuth----\n");
 if(fork() == 0){
  setenv("QUERY_STRING", p, 1);
  dup2(fd, 1);
  execve("cgi-bin/getAuth", emptylist, environ);
 }
 return 0;
}

variable_types: fd int

variable_types:
emptylist
char *[]

variable_types:
p
char *

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

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


postDynamic

decompiled_code:
__int64 __fastcall postDynamic(int fd, __int64 a2)
{
char *v2; // rbx
char *argv; // [rsp+8h] [rbp-20h] BYREF

argv = 0LL;
v2 = strstr(*(const char **)(a2 + 16), "postAuth?");
puts("---execv postAuth----");
if ( !fork() )
{
setenv("QUERY_STRING", v2 + 9, 1);
dup2(fd, 1);
execve("cgi-bin/postAuth", &argv, environ);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2)
{
char *v2; // rbx
char *argv; // [rsp+8h] [rbp-20h] BYREF

argv = 0LL;
v2 = strstr(*(const char **)(a2 + 16), "postAuth?");
puts("---execv postAuth----");
if ( !fork() )
{
setenv("QUERY_STRING", v2 + 9, 1);
dup2(fd, 1);
execve("cgi-bin/postAuth", &argv, environ);
}
return 0LL;
}

original_code:
int postDynamic(int fd, struct ReqInfo* reqInfo)
{
 char* emptylist[] = { ((void *)0) };
 char* p = strstr(reqInfo->resource, "postAuth?");
 p += 9;
 printf("---execv postAuth----\n");
 if(fork() == 0){
  setenv("QUERY_STRING", p, 1);
  dup2(fd, 1);
  execve("cgi-bin/postAuth", emptylist, environ);
 }
 return 0;
}

canonical_original_code:
int postDynamic(int fd, struct ReqInfo *reqInfo)
{
 char *emptylist[] = { ((void *)0) };
 char *p = strstr(reqInfo->resource, "postAuth?");
 p += 9;
 printf("---execv postAuth----\n");
 if(fork() == 0){
  setenv("QUERY_STRING", p, 1);
  dup2(fd, 1);
  execve("cgi-bin/postAuth", emptylist, environ);
 }
 return 0;
}


servDynamic

decompiled_code:
__int64 __fastcall servDynamic(int fd, _DWORD *a2)
{
size_t v2; // rax
char v4[232]; // [rsp+0h] [rbp-E8h] BYREF

sprintf(v4, "HTTP/1.0 %d OK\r\n", (unsigned int)a2[6]);
v2 = strlen(v4);
WriteLine((unsigned int)fd, v4, v2);
WriteLine((unsigned int)fd, "Server: WebServ v0.1\r\n", 24LL);
if ( *a2 )
postDynamic(fd, (__int64)a2);
else
getDynamic(fd, (__int64)a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, _DWORD *a2)
{
size_t v2; // rax
char v4[232]; // [rsp+0h] [rbp-E8h] BYREF

sprintf(v4, "HTTP/1.0 %d OK\r\n", (unsigned int)a2[6]);
v2 = strlen(v4);
WriteLine((unsigned int)fd, v4, v2);
WriteLine((unsigned int)fd, "Server: WebServ v0.1\r\n", 24LL);
if ( *a2 )
func2(fd, (__int64)a2);
else
func1(fd, (__int64)a2);
return 0LL;
}

original_code:
int servDynamic(int fd, struct ReqInfo* reqInfo)
{
 char buffer[200];
 sprintf(buffer, "HTTP/1.0 %d OK\r\n", reqInfo->status);
 WriteLine(fd, buffer, strlen(buffer));
 WriteLine(fd, "Server: WebServ v0.1\r\n", 24);
 if(reqInfo->method == GET)
  getDynamic(fd, reqInfo);
 else
  postDynamic(fd, reqInfo);
 return 0;
}

canonical_original_code:
int servDynamic(int fd, struct ReqInfo *reqInfo)
{
 char buffer[200];
 sprintf(buffer, "HTTP/1.0 %d OK\r\n", reqInfo->status);
 WriteLine(fd, buffer, strlen(buffer));
 WriteLine(fd, "Server: WebServ v0.1\r\n", 24);
 if(reqInfo->method == GET)
  getDynamic(fd, reqInfo);
 else
  postDynamic(fd, reqInfo);
 return 0;
}

variable_types:
buffer
char [200]


ReturnResponse

decompiled_code:
__int64 __fastcall ReturnResponse(unsigned int a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebp
unsigned int v4; // er12

if ( a2[2] == 1 )
{
servDynamic(a1, a2);
return 0;
}
v2 = CheckResource(a2);
v3 = v2;
if ( a2[6] == 200 && v2 < 0 )
{
a2[6] = 3 * (*_errno_location() != 13) + 401;
if ( a2[1] != 1 )
goto LABEL_6;
}
else if ( a2[1] != 1 )
{
goto LABEL_5;
}
OutputHttpHeaders(a1, (__int64)a2);
LABEL_5:
if ( a2[6] == 200 )
{
v4 = ReturnResource(a1, (unsigned int)v3, a2);
if ( v4 == -1 )
{
perror("Return Resouce fail !\n");
return v4;
}
goto LABEL_7;
}
LABEL_6:
ReturnErrorMsg(a1, a2);
LABEL_7:
if ( v3 > 0 )
close(v3);
free(a2);
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebp
unsigned int v4; // er12

if ( a2[2] == 1 )
{
func3(a1, a2);
return 0;
}
v2 = CheckResource(a2);
v3 = v2;
if ( a2[6] == 200 && v2 < 0 )
{
a2[6] = 3 * (*_errno_location() != 13) + 401;
if ( a2[1] != 1 )
goto LABEL_6;
}
else if ( a2[1] != 1 )
{
goto LABEL_5;
}
func0(a1, (__int64)a2);
LABEL_5:
if ( a2[6] == 200 )
{
v4 = ReturnResource(a1, (unsigned int)v3, a2);
if ( v4 == -1 )
{
perror("Return Resouce fail !\n");
return v4;
}
goto LABEL_7;
}
LABEL_6:
ReturnErrorMsg(a1, a2);
LABEL_7:
if ( v3 > 0 )
close(v3);
free(a2);
return 0;
}

original_code:
int ReturnResponse(int fd, struct ReqInfo* reqInfo)
{
 char c;
 int res_fd;
 int flag;
 if (reqInfo->pageType == DYNAMIC) {
  servDynamic(fd, reqInfo);
  return 0;
 }
 res_fd = CheckResource(reqInfo);
 if (reqInfo->status == 200 && res_fd < 0) {
  if((*__errno_location ()) == 13)
   reqInfo->status = 401;
  else
   reqInfo->status = 404;
 }
 if (reqInfo->type == FULL)
  OutputHttpHeaders(fd, reqInfo);
 if (reqInfo->status == 200) {
  flag = ReturnResource(fd, res_fd, reqInfo);
  if(flag == -1){
   perror("Return Resouce fail !\n");
   return -1;
  }
 }
 else
  ReturnErrorMsg(fd, reqInfo);
 if (res_fd > 0)
  close(res_fd);
    if (reqInfo != ((void *)0)) {
        free(reqInfo);
        reqInfo = ((void *)0);
    }
 return 0;
}

canonical_original_code:
int ReturnResponse(int fd, struct ReqInfo *reqInfo)
{
 char c;
 int res_fd;
 int flag;
 if (reqInfo->pageType == DYNAMIC) {
  servDynamic(fd, reqInfo);
  return 0;
 }
 res_fd = CheckResource(reqInfo);
 if (reqInfo->status == 200 && res_fd < 0) {
  if((*__errno_location ()) == 13)
   reqInfo->status = 401;
  else
   reqInfo->status = 404;
 }
 if (reqInfo->type == FULL)
  OutputHttpHeaders(fd, reqInfo);
 if (reqInfo->status == 200) {
  flag = ReturnResource(fd, res_fd, reqInfo);
  if(flag == -1){
   perror("Return Resouce fail !\n");
   return -1;
  }
 }
 else
  ReturnErrorMsg(fd, reqInfo);
 if (res_fd > 0)
  close(res_fd);
    if (reqInfo != ((void *)0)) {
        free(reqInfo);
        reqInfo = ((void *)0);
    }
 return 0;
}

variable_types: c char

variable_types: flag int


ParseHttpHeader

decompiled_code:
__int64 __fastcall ParseHttpHeader(const char *a1, __int64 a2)
{
__int64 result; // rax
const char *v5; // rbx
char *v6; // rdx
int v7; // eax
size_t v8; // r12
char *v9; // rax

if ( strstr(a1, "cgi-bin") )
*(_DWORD *)(a2 + 8) = 1;
result = 0LL;
if ( firstHeader == 1 )
{
if ( !memcmp(a1, "GET ", 4uLL) )
{
*(_DWORD *)a2 = 0;
v5 = a1 + 4;
}
else
{
if ( memcmp(a1, "HEAD ", 5uLL) )
{
*(_DWORD *)a2 = 2;
*(_DWORD *)(a2 + 24) = 501;
puts("not supported...");
return 0xFFFFFFFFLL;
}
*(_DWORD *)a2 = 1;
v5 = a1 + 5;
}
v6 = strchr(v5, 32);
v7 = (_DWORD)v6 - (_DWORD)v5;
if ( !v6 )
v7 = strlen(v5);
if ( v7 )
{
v8 = v7;
v9 = (char *)malloc(v7 + 1LL);
*(_QWORD *)(a2 + 16) = v9;
strncpy(v9, v5, v8);
*(_DWORD *)(a2 + 4) = strstr(v5, "HTTP/") != 0LL;
firstHeader = 0;
result = 0LL;
}
else
{
*(_DWORD *)(a2 + 24) = 400;
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, __int64 a2)
{
__int64 result; // rax
const char *v5; // rbx
char *v6; // rdx
int v7; // eax
size_t v8; // r12
char *v9; // rax

if ( strstr(a1, "cgi-bin") )
*(_DWORD *)(a2 + 8) = 1;
result = 0LL;
if ( firstHeader == 1 )
{
if ( !memcmp(a1, "GET ", 4uLL) )
{
*(_DWORD *)a2 = 0;
v5 = a1 + 4;
}
else
{
if ( memcmp(a1, "HEAD ", 5uLL) )
{
*(_DWORD *)a2 = 2;
*(_DWORD *)(a2 + 24) = 501;
puts("not supported...");
return 0xFFFFFFFFLL;
}
*(_DWORD *)a2 = 1;
v5 = a1 + 5;
}
v6 = strchr(v5, 32);
v7 = (_DWORD)v6 - (_DWORD)v5;
if ( !v6 )
v7 = strlen(v5);
if ( v7 )
{
v8 = v7;
v9 = (char *)malloc(v7 + 1LL);
*(_QWORD *)(a2 + 16) = v9;
strncpy(v9, v5, v8);
*(_DWORD *)(a2 + 4) = strstr(v5, "HTTP/") != 0LL;
firstHeader = 0;
result = 0LL;
}
else
{
*(_DWORD *)(a2 + 24) = 400;
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int ParseHttpHeader(char* buffer, struct ReqInfo* reqInfo)
{
 char* ptr;
 int len;
 if (strstr(buffer, "cgi-bin") != ((void *)0)) {
  reqInfo->pageType = DYNAMIC;
 }
 if (firstHeader == 1) {
 if(!strncmp(buffer, "GET ", 4)){
  reqInfo->method = GET;
  buffer += 4;
 }
 else if (!strncmp(buffer, "HEAD ", 5)) {
  reqInfo->method = HEAD;
  buffer += 5;
 }
 else {
  reqInfo->method = UNSUPPORTED;
  reqInfo->status = 501;
  printf("not supported...\n");
  return -1;
 }
 ptr = strchr(buffer, ' ');
 if (((void *)0) == ptr)
  len = strlen(buffer);
 else
  len = ptr - buffer;
 if (0 == len) {
  reqInfo->status = 400;
  return -1;
 }
 reqInfo->resource = malloc(len+1*sizeof(char));
 strncpy(reqInfo->resource, buffer, len);
 if(strstr(buffer, "HTTP/"))
  reqInfo->type = FULL;
 else
  reqInfo->type = SIMPLE;
 firstHeader = 0;
 }
 return 0;
}

canonical_original_code:
int ParseHttpHeader(char *buffer, struct ReqInfo *reqInfo)
{
 char *ptr;
 int len;
 if (strstr(buffer, "cgi-bin") != ((void *)0)) {
  reqInfo->pageType = DYNAMIC;
 }
 if (firstHeader == 1) {
 if(!strncmp(buffer, "GET ", 4)){
  reqInfo->method = GET;
  buffer += 4;
 }
 else if (!strncmp(buffer, "HEAD ", 5)) {
  reqInfo->method = HEAD;
  buffer += 5;
 }
 else {
  reqInfo->method = UNSUPPORTED;
  reqInfo->status = 501;
  printf("not supported...\n");
  return -1;
 }
 ptr = strchr(buffer, ' ');
 if (((void *)0) == ptr)
  len = strlen(buffer);
 else
  len = ptr - buffer;
 if (0 == len) {
  reqInfo->status = 400;
  return -1;
 }
 reqInfo->resource = malloc(len+1*sizeof(char));
 strncpy(reqInfo->resource, buffer, len);
 if(strstr(buffer, "HTTP/"))
  reqInfo->type = FULL;
 else
  reqInfo->type = SIMPLE;
 firstHeader = 0;
 }
 return 0;
}

variable_types:
buffer
char *

variable_types:
ptr
char *

variable_types: len int


ReadLine

decompiled_code:
__int64 __fastcall ReadLine(int fd, _BYTE *a2, __int64 a3)
{
__int64 v4; // rdx
__int64 v5; // rbp
__int64 v6; // rbx
char v7; // al
int v8; // eax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v4 = a3 - 1;
if ( v4 )
{
v5 = v4;
v6 = 0LL;
while ( 1 )
{
v8 = read(fd, buf, 1uLL);
if ( v8 == 1 )
{
v7 = buf[0];
*a2 = buf[0];
if ( v7 == 10 )
{
v5 = v6;
++a2;
goto LABEL_11;
}
++a2;
}
else
{
if ( !v8 )
{
v5 = v6;
if ( !v6 )
return v6;
goto LABEL_11;
}
if ( *_errno_location() != 4 )
{
perror("ReadLine fail !\n");
exit(1);
}
}
if ( ++v6 == v5 )
goto LABEL_11;
}
}
v5 = 0LL;
LABEL_11:
*a2 = 0;
return v5 + 1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, _BYTE *a2, __int64 a3)
{
__int64 v4; // rdx
__int64 v5; // rbp
__int64 v6; // rbx
char v7; // al
int v8; // eax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v4 = a3 - 1;
if ( v4 )
{
v5 = v4;
v6 = 0LL;
while ( 1 )
{
v8 = read(fd, buf, 1uLL);
if ( v8 == 1 )
{
v7 = buf[0];
*a2 = buf[0];
if ( v7 == 10 )
{
v5 = v6;
++a2;
goto LABEL_11;
}
++a2;
}
else
{
if ( !v8 )
{
v5 = v6;
if ( !v6 )
return v6;
goto LABEL_11;
}
if ( *_errno_location() != 4 )
{
perror("ReadLine fail !\n");
exit(1);
}
}
if ( ++v6 == v5 )
goto LABEL_11;
}
}
v5 = 0LL;
LABEL_11:
*a2 = 0;
return v5 + 1;
}

original_code:
ssize_t ReadLine(int fd, char* buffer, size_t len)
{
 ssize_t n;
 ssize_t rc;
 char c;
 for (n = 0; n < len-1 ; ++n) {
  if ((rc = read(fd, &c, 1)) == 1) {
   *buffer++ = c;
   if(c == '\n')
    break;
  }
  else if(rc == 0){
   if(n == 0)
    return 0;
   else
    break;
  }
  else{
   if((*__errno_location ()) == 4)
    continue;
   perror("ReadLine fail !\n");
   exit(1);
  }
 }
 *buffer = '\0';
 return n+1;
}

canonical_original_code:
long ReadLine(int fd, char *buffer, unsigned long len)
{
 long n;
 long rc;
 char c;
 for (n = 0; n < len-1 ; ++n) {
  if ((rc = read(fd, &c, 1)) == 1) {
   *buffer++ = c;
   if(c == '\n')
    break;
  }
  else if(rc == 0){
   if(n == 0)
    return 0;
   else
    break;
  }
  else{
   if((*__errno_location ()) == 4)
    continue;
   perror("ReadLine fail !\n");
   exit(1);
  }
 }
 *buffer = '\0';
 return n+1;
}

variable_types:
len
unsigned long

variable_types: n long

variable_types: rc long

return_type: long


GetReqContent

decompiled_code:
__int64 __fastcall GetReqContent(int fd, __int64 a2)
{
int v3; // eax
struct timeval v4; // [rsp+0h] [rbp-4B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-4A8h] BYREF
char v6[1064]; // [rsp+90h] [rbp-428h] BYREF

v4.tv_sec = 2LL;
v4.tv_usec = 0LL;
while ( 1 )
{
memset(v6, 0, 0x400uLL);
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
v3 = select(fd + 1, &readfds, 0LL, 0LL, &v4);
if ( v3 < 0 )
{
perror("select fail !\n");
exit(1);
}
if ( !v3 )
{
puts("select timeout...");
return 0xFFFFFFFFLL;
}
ReadLine(fd, v6, 1024LL);
Trim(v6);
printf("Recv: %s", v6);
if ( v6[0] == 13 && v6[1] == 10 )
break;
if ( (unsigned int)ParseHttpHeader(v6, a2) || !*(_DWORD *)(a2 + 4) )
goto LABEL_11;
}
puts("buffer[0] == '0'");
LABEL_11:
firstHeader = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2)
{
int v3; // eax
struct timeval v4; // [rsp+0h] [rbp-4B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-4A8h] BYREF
char v6[1064]; // [rsp+90h] [rbp-428h] BYREF

v4.tv_sec = 2LL;
v4.tv_usec = 0LL;
while ( 1 )
{
memset(v6, 0, 0x400uLL);
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
v3 = select(fd + 1, &readfds, 0LL, 0LL, &v4);
if ( v3 < 0 )
{
perror("select fail !\n");
exit(1);
}
if ( !v3 )
{
puts("select timeout...");
return 0xFFFFFFFFLL;
}
func1(fd, v6, 1024LL);
Trim(v6);
printf("Recv: %s", v6);
if ( v6[0] == 13 && v6[1] == 10 )
break;
if ( (unsigned int)func0(v6, a2) || !*(_DWORD *)(a2 + 4) )
goto LABEL_11;
}
puts("buffer[0] == '0'");
LABEL_11:
firstHeader = 1;
return 0LL;
}

original_code:
int GetReqContent(int fd, struct ReqInfo* reqInfo)
{
 char buffer[1024];
 int flag;
 fd_set fds;
 struct timeval tv;
 tv.tv_sec = 2;
 tv.tv_usec = 0;
 do {
  memset(buffer, '\0', 1024);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  flag = select(fd + 1, &fds, ((void *)0), ((void *)0), &tv);
  if (flag < 0) {
   perror("select fail !\n");
   exit(1);
  }
  else if (flag == 0) {
   printf("select timeout...\n");
   return -1;
  }
  else{
   ReadLine(fd, buffer, 1024);
   Trim(buffer);
            printf("Recv: %s", buffer);
   if ((buffer[0] == '\r')&&(buffer[1] == '\n')) {
    printf("buffer[0] == '0'\n");
    break;
   }
   if (ParseHttpHeader(buffer, reqInfo))
    break;
  }
 } while (reqInfo->type != SIMPLE);
 firstHeader = 1;
 return 0;
}

canonical_original_code:
int GetReqContent(int fd, struct ReqInfo *reqInfo)
{
 char buffer[1024];
 int flag;
 struct fd_set fds;
 struct timeval tv;
 tv.tv_sec = 2;
 tv.tv_usec = 0;
 do {
  memset(buffer, '\0', 1024);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  flag = select(fd + 1, &fds, ((void *)0), ((void *)0), &tv);
  if (flag < 0) {
   perror("select fail !\n");
   exit(1);
  }
  else if (flag == 0) {
   printf("select timeout...\n");
   return -1;
  }
  else{
   ReadLine(fd, buffer, 1024);
   Trim(buffer);
            printf("Recv: %s", buffer);
   if ((buffer[0] == '\r')&&(buffer[1] == '\n')) {
    printf("buffer[0] == '0'\n");
    break;
   }
   if (ParseHttpHeader(buffer, reqInfo))
    break;
  }
 } while (reqInfo->type != SIMPLE);
 firstHeader = 1;
 return 0;
}

variable_types:
buffer
char [1024]

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

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


InitReqInfo

original_code:
void InitReqInfo(struct ReqInfo* reqInfo)
{
 reqInfo->method = UNSUPPORTED;
 reqInfo->type = SIMPLE;
 reqInfo->resource = ((void *)0);
 reqInfo->status = 200;
 reqInfo->pageType = STATIC;
}

canonical_original_code:
void InitReqInfo(struct ReqInfo *reqInfo)
{
 reqInfo->method = UNSUPPORTED;
 reqInfo->type = SIMPLE;
 reqInfo->resource = ((void *)0);
 reqInfo->status = 200;
 reqInfo->pageType = STATIC;
}

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


FreeReqInfo

decompiled_code:
void __fastcall FreeReqInfo(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

original_code:
void FreeReqInfo(struct ReqInfo* reqInfo)
{
 if (reqInfo->resource != ((void *)0)) {
  free(reqInfo->resource);
 }
}

canonical_original_code:
void FreeReqInfo(struct ReqInfo *reqInfo)
{
 if (reqInfo->resource != ((void *)0)) {
  free(reqInfo->resource);
 }
}


Trim

decompiled_code:
_BOOL8 __fastcall Trim(const char *a1)
{
__int64 v1; // rbx
int v2; // edx
_BOOL8 result; // rax

v1 = (int)(strlen(a1) - 1);
do
{
v2 = isalnum(a1[v1]);
result = (int)v1-- >= 0;
}
while ( result && v2 == 0 );
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1)
{
__int64 v1; // rbx
int v2; // edx
_BOOL8 result; // rax

v1 = (int)(strlen(a1) - 1);
do
{
v2 = isalnum(a1[v1]);
result = (int)v1-- >= 0;
}
while ( result && v2 == 0 );
return result;
}

original_code:
void Trim(char* buffer)
{
 int n = 0;
 n = strlen(buffer) - 1;
 while(!isalnum(buffer[n]) && n >= 0){
  buffer[n--] == '\0';
 }
}

canonical_original_code:
void Trim(char *buffer)
{
 int n = 0;
 n = strlen(buffer) - 1;
 while(!isalnum(buffer[n]) && n >= 0){
  buffer[n--] == '\0';
 }
}

variable_types: n int


Pipe

decompiled_code:
_BOOL8 __fastcall Pipe(int *a1)
{
return pipe(a1) != -1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int *a1)
{
return pipe(a1) != -1;
}

original_code:
int Pipe(int pipefd[2])
{
 if (pipe(pipefd) == -1)
  return 0;
 return 1;
}

variable_types:
pipefd
int [2]


decompiled_code:
__int64 __fastcall poll_process(int fd)
{
char *v1; // rax
int v2; // er13
int v4; // eax
int v5; // ebx
char *v6; // rbx
int v7; // ebp
int *v8; // rdx
int v9; // eax
unsigned int v10; // eax
unsigned int v11; // ebp
struct sockaddr v12; // [rsp+0h] [rbp-2048h] BYREF
struct pollfd fds; // [rsp+10h] [rbp-2038h] BYREF
char v14[8184]; // [rsp+18h] [rbp-2030h] BYREF
char v15; // [rsp+2010h] [rbp-38h] BYREF
socklen_t addr_len[11]; // [rsp+201Ch] [rbp-2Ch] BYREF

fds.fd = fd;
fds.events = 1;
v1 = v14;
do
{
*(_DWORD *)v1 = -1;
v1 += 8;
}
while ( v1 != &v15 );
addr_len[0] = 16;
v2 = 0;
while ( 1 )
{
v4 = poll(&fds, v2 + 1, 1000);
v5 = v4;
if ( v4 < 0 )
break;
if ( v4 )
{
if ( (fds.revents & 1) == 0 )
goto LABEL_10;
v7 = accept(fd, &v12, addr_len);
if ( v7 == -1 )
perror("accept error");
v8 = (int *)v14;
v9 = 1;
while ( *v8 >= 0 )
{
++v9;
v8 += 2;
if ( v9 == 1024 )
{
perror("too many clients!\n");
return 0LL;
}
}
*(&fds.fd + 2 * v9) = v7;
*(&fds.events + 4 * v9) = 1;
if ( v2 < v9 )
v2 = v9;
if ( v5 > 1 )
{
LABEL_10:
if ( v2 > 1 )
{
v6 = v14;
do
{
v11 = *(_DWORD *)v6;
if ( *(int *)v6 >= 0 && (v6[6] & 1) != 0 )
{
if ( !fork() )
{
close(fd);
v10 = getpid();
printf("process %d forked\n", v10);
handleRequest(v11);
exit(0);
}
close(v11);
waitpid(-1, 0LL, 1);
}
v6 += 8;
}
while ( v6 != &v14[8 * (v2 - 2) + 8] );
}
}
}
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 v4; // eax
int v5; // ebx
char *v6; // rbx
int v7; // ebp
int *v8; // rdx
int v9; // eax
unsigned int v10; // eax
unsigned int v11; // ebp
struct sockaddr v12; // [rsp+0h] [rbp-2048h] BYREF
struct pollfd fds; // [rsp+10h] [rbp-2038h] BYREF
char v14[8184]; // [rsp+18h] [rbp-2030h] BYREF
char v15; // [rsp+2010h] [rbp-38h] BYREF
socklen_t addr_len[11]; // [rsp+201Ch] [rbp-2Ch] BYREF

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


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; // eax
int v7; // ebp
unsigned int v8; // eax
struct sockaddr v10; // [rsp+0h] [rbp-3058h] BYREF
struct epoll_event events; // [rsp+10h] [rbp-3048h] BYREF
_DWORD v12[3069]; // [rsp+1Ch] [rbp-303Ch] BYREF
struct epoll_event event; // [rsp+3010h] [rbp-48h] BYREF
socklen_t addr_len[15]; // [rsp+301Ch] [rbp-3Ch] BYREF

v2 = epoll_create(1024);
event.data.fd = fd;
event.events = -2147483647;
if ( epoll_ctl(v2, 1, fd, &event) >= 0 )
{
addr_len[0] = 16;
while ( 1 )
{
do
v3 = epoll_wait(v2, &events, 512, 500);
while ( v3 <= 0 );
v4 = &events;
v5 = (struct epoll_event *)&v12[3 * (v3 - 1)];
do
{
v7 = v4->data.fd;
if ( v7 == fd )
{
v6 = accept(fd, &v10, addr_len);
v1 = v6;
if ( v6 < 0 )
{
perror("accept error!\n");
}
else
{
setnonblocking(v6);
event.data.fd = v1;
event.events = -2147483647;
epoll_ctl(v2, 1, v1, &event);
}
}
else if ( (v4->events & 0x18) != 0 )
{
close(v7);
epoll_ctl(v2, 1, v1, &event);
}
else if ( (v4->events & 1) != 0 && v7 >= 0 )
{
if ( !fork() )
{
close(fd);
v8 = getpid();
printf("process %d forked\n", v8);
handleRequest((unsigned int)v7);
exit(0);
}
close(v7);
waitpid(-1, 0LL, 1);
event.data.fd = -1;
epoll_ctl(v2, 2, v7, &event);
}
++v4;
}
while ( v4 != v5 );
}
}
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; // eax
int v7; // ebp
unsigned int v8; // eax
struct sockaddr v10; // [rsp+0h] [rbp-3058h] BYREF
struct epoll_event events; // [rsp+10h] [rbp-3048h] BYREF
_DWORD v12[3069]; // [rsp+1Ch] [rbp-303Ch] BYREF
struct epoll_event event; // [rsp+3010h] [rbp-48h] BYREF
socklen_t addr_len[15]; // [rsp+301Ch] [rbp-3Ch] BYREF

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

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

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);
free(*v3);
free(v3);
++v2;
}
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);
free(*v3);
free(v3);
++v2;
}
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
char v3[1016]; // [rsp+0h] [rbp-3F8h] BYREF

v1 = *(const char **)(a1 + 16);
if ( !v1 )
return -1;
memset(v3, 0, 0x3E8uLL);
strcpy(v3, "/home/y/SimServer/www");
strcat(v3, v1);
return open(v3, 0);
}

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

v1 = *(const char **)(a1 + 16);
if ( !v1 )
return -1;
memset(v3, 0, 0x3E8uLL);
strcpy(v3, "/home/y/SimServer/www");
strcat(v3, v1);
return open(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]


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

variable_types: n int


WriteLine

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

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

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

if ( a3 )
{
v5 = 0LL;
do
{
v7 = (int)write(fd, buf, a3 - v5);
if ( (int)v7 <= 0 )
{
if ( *_errno_location() != 4 )
{
perror("Write error !\n");
return -1LL;
}
v7 = 0LL;
}
v5 += v7;
buf = (char *)buf + v7;
}
while ( a3 > v5 );
}
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


ReturnErrorMsg

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

sprintf(v6, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", *(unsigned int *)(a2 + 24));
v2 = strlen(v6);
WriteLine(fd, v6, v2);
sprintf(v6, "\n
Server Error %d
\n", *(unsigned int *)(a2 + 24));
v3 = strlen(v6);
WriteLine(fd, v6, v3);
strcpy(v6, "
The request could not be completed.
\n\n\n");
v4 = strlen(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
char v6[136]; // [rsp+0h] [rbp-88h] BYREF

sprintf(v6, "\n\n<TITLE>Server Error %d</TITLE>\n\n\n", *(unsigned int *)(a2 + 24));
v2 = strlen(v6);
func2(fd, v6, v2);
sprintf(v6, "\n
Server Error %d
\n", *(unsigned int *)(a2 + 24));
v3 = strlen(v6);
func2(fd, v6, v3);
strcpy(v6, "
The request could not be completed.
\n\n\n");
v4 = strlen(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; // rbx

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; // rbx

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


writer

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

v1 = 0;
while ( !(unsigned int)sq_isDone(*((_QWORD *)a1 + 2)) || !(unsigned int)sq_isEmpty(*((_QWORD *)a1 + 2)) )
{
v2 = (char *)sq_deq(*((_QWORD *)a1 + 2));
printf("Output: %s\n", v2);
++v1;
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; // rbx

v1 = 0;
while ( !(unsigned int)func15(*((_QWORD *)a1 + 2)) || !(unsigned int)func12(*((_QWORD *)a1 + 2)) )
{
v2 = (char *)func11(*((_QWORD *)a1 + 2));
printf("Output: %s\n", v2);
++v1;
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 *

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void **v3; // r12
sem_t **v4; // rbx
sem_t *v5; // rdi
sem_t v7; // [rsp+0h] [rbp-78h] BYREF
char arg[24]; // [rsp+20h] [rbp-58h] BYREF
sem_t *sem; // [rsp+38h] [rbp-40h] BYREF
pthread_t newthread; // [rsp+40h] [rbp-38h] BYREF
pthread_t th; // [rsp+48h] [rbp-30h] BYREF
pthread_t v12; // [rsp+50h] [rbp-28h] BYREF
pthread_t v13[4]; // [rsp+58h] [rbp-20h] BYREF

v3 = (void **)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 != &sem );
if ( sem_init(&v7, 0, 1u) < 0 )
{
puts("Error initializing semaphore");
}
else
{
sem = &v7;
if ( pthread_create(&newthread, 0LL, reader, arg) )
{
puts("Could not create reader");
}
else if ( pthread_create(&th, 0LL, munch1, arg) )
{
puts("Could not create munch1");
}
else if ( pthread_create(&v12, 0LL, munch2, arg) )
{
puts("Could not create munch2");
}
else if ( pthread_create(v13, 0LL, writer, arg) )
{
puts("Could not create writer");
}
else if ( pthread_join(newthread, 0LL) )
{
puts("Could not join reader");
}
else if ( pthread_join(th, 0LL) )
{
puts("Could not join munch1");
}
else if ( pthread_join(v12, 0LL) )
{
puts("Could not join munch2");
}
else
{
if ( !pthread_join(v13[0], 0LL) )
{
do
{
sq_destroy(*v3);
free(*v3++);
}
while ( v3 != (void **)&sem );
sem_destroy(sem);
free(sem);
pthread_exit(0LL);
}
puts("Could not join writer");
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void **v3; // r12
sem_t **v4; // rbx
sem_t *v5; // rdi
sem_t v7; // [rsp+0h] [rbp-78h] BYREF
char arg[24]; // [rsp+20h] [rbp-58h] BYREF
sem_t *sem; // [rsp+38h] [rbp-40h] BYREF
pthread_t newthread; // [rsp+40h] [rbp-38h] BYREF
pthread_t th; // [rsp+48h] [rbp-30h] BYREF
pthread_t v12; // [rsp+50h] [rbp-28h] BYREF
pthread_t v13[4]; // [rsp+58h] [rbp-20h] BYREF

v3 = (void **)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 != &sem );
if ( sem_init(&v7, 0, 1u) < 0 )
{
puts("Error initializing semaphore");
}
else
{
sem = &v7;
if ( pthread_create(&newthread, 0LL, reader, arg) )
{
puts("Could not create reader");
}
else if ( pthread_create(&th, 0LL, munch1, arg) )
{
puts("Could not create munch1");
}
else if ( pthread_create(&v12, 0LL, munch2, arg) )
{
puts("Could not create munch2");
}
else if ( pthread_create(v13, 0LL, writer, arg) )
{
puts("Could not create writer");
}
else if ( pthread_join(newthread, 0LL) )
{
puts("Could not join reader");
}
else if ( pthread_join(th, 0LL) )
{
puts("Could not join munch1");
}
else if ( pthread_join(v12, 0LL) )
{
puts("Could not join munch2");
}
else
{
if ( !pthread_join(v13[0], 0LL) )
{
do
{
func13(*v3);
free(*v3++);
}
while ( v3 != (void **)&sem );
sem_destroy(sem);
free(sem);
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; }


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

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

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

while ( 1 )
{
if ( (unsigned int)func15(*(_QWORD *)a1) && (unsigned int)func12(*(_QWORD *)a1) )
{
func14(*((_QWORD *)a1 + 1));
return 0LL;
}
v1 = (const char *)func11(*(_QWORD *)a1);
v2 = v1;
if ( !v1 )
break;
for ( i = index(v1, 32); i; i = index(v2, 32) )
*i = 42;
func10(*((_QWORD *)a1 + 1), v2);
}
printf("ERROR: Failed to get string from q1.");
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.");
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 12) = 0;
*(_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.");
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 12) = 0;
*(_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; // rbp
char v3; // bl
const __int32_t **v4; // rcx
char *v5; // rax

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

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

while ( 1 )
{
if ( (unsigned int)func15(*((_QWORD *)a1 + 1)) && func12(*((_QWORD *)a1 + 1)) )
{
func14(*((_QWORD *)a1 + 2));
return 0LL;
}
v1 = (char *)func11(*((_QWORD *)a1 + 1));
v2 = (__int64)v1;
if ( !v1 )
break;
v3 = *v1;
if ( *v1 )
{
v4 = __ctype_toupper_loc();
v5 = (char *)v2;
do
{
*v5++ = (*v4)[v3];
v3 = *v5;
}
while ( *v5 );
}
func10(*((_QWORD *)a1 + 2), v2);
}
printf("ERROR: Input is empty.");
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; // edx
int result; // eax
char v3[4096]; // [rsp+0h] [rbp-2128h] BYREF
char s[4096]; // [rsp+1000h] [rbp-1128h] BYREF
struct stat v5; // [rsp+2000h] [rbp-128h] BYREF
struct stat v6; // [rsp+2090h] [rbp-98h] BYREF

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

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int v1; // edx
int result; // eax
char v3[4096]; // [rsp+0h] [rbp-2128h] BYREF
char s[4096]; // [rsp+1000h] [rbp-1128h] BYREF
struct stat v5; // [rsp+2000h] [rbp-128h] BYREF
struct stat v6; // [rsp+2090h] [rbp-98h] BYREF

snprintf(s, 0x1000uLL, "%s/%s/generated.html", *((const char **)&config + 2), a1);
snprintf(v3, 0x1000uLL, "%s/%s/latest", *((const char **)&config + 2), a1);
v1 = stat(s, &v6);
result = 1;
if ( v1 != -1 )
{
result = stat(v3, &v5);
if ( result != -1 )
result = LODWORD(v5.st_mtim.tv_sec) - LODWORD(v6.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; // ebx

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; // ebx

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 newa[4096]; // [rsp+90h] [rbp-2018h] BYREF
char s[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 newa[4096]; // [rsp+90h] [rbp-2018h] BYREF
char s[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
__int64 v6; // rax

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
if ( a1 > a2 - 1 )
{
LOBYTE(v6) = 0;
}
else
{
v5 = a2 - a1;
v6 = 0LL;
while ( *v4 == a3 )
{
++v6;
--v4;
if ( v6 == v5 )
{
LOBYTE(v6) = v5;
break;
}
}
}
result = v6 & 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
__int64 v6; // rax

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
if ( a1 > a2 - 1 )
{
LOBYTE(v6) = 0;
}
else
{
v5 = a2 - a1;
v6 = 0LL;
while ( *v4 == a3 )
{
++v6;
--v4;
if ( v6 == v5 )
{
LOBYTE(v6) = v5;
break;
}
}
}
result = v6 & 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

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

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