decompiled_code:
__int64 get_method()
{
_QWORD *v0; // rax
const char *v1; // rax
const char *v2; // rbx
__int64 v3; // rdi

v0 = (_QWORD *)client[4];
if ( !v0 || !v0[9] || !v0[10] )
return error_handler(404LL);
v1 = (const char *)socket_peer(*v0);
log_msg(2LL, "GET %s%s from %s", *(const char **)(client[4] + 72LL), *(const char **)(client[4] + 80LL), v1);
v2 = (const char *)deauth_path(*(_QWORD *)(client[4] + 72LL), *(_QWORD *)(client[4] + 80LL));
log_msg(3LL, "Found Path: %s", v2);
if ( (unsigned int)is_directory(v2) )
v3 = get_index(v2);
else
v3 = load(v2);
return mimetype_handler(v3);
}

canonical_decompiled_code:
__int64 func5()
{
_QWORD *v0; // rax
const char *v1; // rax
const char *v2; // rbx
__int64 v3; // rdi

v0 = (_QWORD *)client[4];
if ( !v0 || !v0[9] || !v0[10] )
return error_handler(404LL);
v1 = (const char *)socket_peer(*v0);
log_msg(2LL, "GET %s%s from %s", *(const char **)(client[4] + 72LL), *(const char **)(client[4] + 80LL), v1);
v2 = (const char *)deauth_path(*(_QWORD *)(client[4] + 72LL), *(_QWORD *)(client[4] + 80LL));
log_msg(3LL, "Found Path: %s", v2);
if ( (unsigned int)is_directory(v2) )
v3 = get_index(v2);
else
v3 = load(v2);
return mimetype_handler(v3);
}

original_code:
int
get_method()
{
 File fc = ((void *)0);
 str filename;
 if (! client.request || !client.request->host || !client.request->path) return error_handler(404);
 log_msg(2,"GET %s%s from %s",client.request->host,client.request->path,socket_peer(client.request->socket));
 filename = deauth_path(client.request->host,client.request->path);
 log_msg(3,"Found Path: %s",filename);
 fc = is_directory(filename) ?
  get_index(filename) :
  load(filename);
 return mimetype_handler(fc);
}

canonical_original_code:
int get_method()
{
 struct file_cache_struct *fc = ((void *)0);
 struct str_struct *filename;
 if (! client.request || !client.request->host || !client.request->path) return error_handler(404);
 log_msg(2,"GET %s%s from %s",client.request->host,client.request->path,socket_peer(client.request->socket));
 filename = deauth_path(client.request->host,client.request->path);
 log_msg(3,"Found Path: %s",filename);
 fc = is_directory(filename) ?
  get_index(filename) :
  load(filename);
 return mimetype_handler(fc);
}

variable_types: struct file_cache_struct { struct file_cache_struct *next; 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]; } st; struct parsed_struct { enum { TEXT=1, SCRIPT=2, EMIT=3 } kind; int offset; int length; } *parsed; struct mime_struct { struct { int len; char *data; } ending; struct { int len; char *data; } type; int (*handler)(struct file_cache_struct *); } *mime; char *data; int fd; struct str_struct { int length; char *data; char contents[]; } name; } *

user_defined_types: struct file_cache_struct { struct file_cache_struct *next; struct stat st; struct parsed_struct *parsed; struct mime_struct *mime; char *data; int fd; struct str_struct name; }

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


repo: cthulhuology/Jawas

decompiled_code:
__int64 __fastcall parse_json(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
const char *v5; // rax
int v6; // eax
__int64 v7; // rbx
__int64 v8; // rax

v4 = len(a2);
v5 = (const char *)ref(*(_QWORD *)(a2 + 8) + a3, (unsigned int)(v4 - a3));
log_msg(3LL, "Found json [%s]", v5);
v6 = len(a2);
v7 = ref(*(_QWORD *)(a2 + 8) + a3, (unsigned int)(v6 - a3));
v8 = _("json");
return append_header(a1, v8, v7);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
const char *v5; // rax
int v6; // eax
__int64 v7; // rbx
__int64 v8; // rax

v4 = len(a2);
v5 = (const char *)ref(*(_QWORD *)(a2 + 8) + a3, (unsigned int)(v4 - a3));
log_msg(3LL, "Found json [%s]", v5);
v6 = len(a2);
v7 = ref(*(_QWORD *)(a2 + 8) + a3, (unsigned int)(v6 - a3));
v8 = _("json");
return append_header(a1, v8, v7);
}

original_code:
Headers
parse_json(Headers hd, str buf, int pos)
{
 log_msg(3,"Found json [%s]",ref(buf->data+pos,len(buf)-pos));
 return append_header(hd,_("json"),ref(buf->data + pos,len(buf)-pos));
}

canonical_original_code:
struct headers_struct *parse_json(struct headers_struct *hd, struct str_struct *buf, int pos)
{
 log_msg(3,"Found json [%s]",ref(buf->data+pos,len(buf)-pos));
 return append_header(hd,_("json"),ref(buf->data + pos,len(buf)-pos));
}

variable_types:
hd
struct headers_struct *

variable_types:
buf
struct str_struct *

variable_types: pos int

return_type: struct headers_struct { int nslots; struct headers_data_struct { struct str_struct { int length; char *data; char contents[]; } *key; struct str_struct *value; } slots[250]; } *

user_defined_types: struct headers_struct { int nslots; struct headers_data_struct slots[250]; }

user_defined_types: struct headers_data_struct { struct str_struct *key; struct str_struct *value; }


decompiled_code:
__int64 post_method()
{
__int64 v0; // rax
const char *v1; // rbx
const char *v2; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax

v0 = _("Content-Type");
v1 = (const char *)find_header(*(_QWORD *)(client[4] + 16LL), v0);
v2 = (const char *)socket_peer(*(_QWORD *)client[4]);
log_msg(2LL, "POST %s%s %s from %s", *(const char **)(client[4] + 72LL), *(const char **)(client[4] + 80LL), v1, v2);
if ( v1 )
{
v3 = _("multipart/form-data");
if ( (unsigned int)ncmp(v1, v3, 19LL) )
{
v5 = parse_multipart_body(*(_QWORD *)(client[4] + 24LL), v1);
goto LABEL_5;
}
v4 = _("text/json");
if ( (unsigned int)ncmp(v1, v4, 9LL) )
{
v5 = parse_json(*(_QWORD *)(client[4] + 24LL), *(_QWORD *)(client[4] + 48LL), *(_DWORD *)(client[4] + 92LL));
goto LABEL_5;
}
}
v5 = parse_uri_encoded(
*(_QWORD *)(client[4] + 24LL),
*(_QWORD *)(client[4] + 48LL),
*(unsigned int *)(client[4] + 92LL));
LABEL_5:
*(_QWORD *)(client[4] + 24LL) = v5;
return get_method();
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rax
const char *v1; // rbx
const char *v2; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax

v0 = _("Content-Type");
v1 = (const char *)find_header(*(_QWORD *)(client[4] + 16LL), v0);
v2 = (const char *)socket_peer(*(_QWORD *)client[4]);
log_msg(2LL, "POST %s%s %s from %s", *(const char **)(client[4] + 72LL), *(const char **)(client[4] + 80LL), v1, v2);
if ( v1 )
{
v3 = _("multipart/form-data");
if ( (unsigned int)ncmp(v1, v3, 19LL) )
{
v5 = parse_multipart_body(*(_QWORD *)(client[4] + 24LL), v1);
goto LABEL_5;
}
v4 = _("text/json");
if ( (unsigned int)ncmp(v1, v4, 9LL) )
{
v5 = func6(*(_QWORD *)(client[4] + 24LL), *(_QWORD *)(client[4] + 48LL), *(_DWORD *)(client[4] + 92LL));
goto LABEL_5;
}
}
v5 = parse_uri_encoded(
*(_QWORD *)(client[4] + 24LL),
*(_QWORD *)(client[4] + 48LL),
*(unsigned int *)(client[4] + 92LL));
LABEL_5:
*(_QWORD *)(client[4] + 24LL) = v5;
return func5();
}

original_code:
int
post_method()
{
 str enctype = find_header(client.request->headers,_("Content-Type"));
 log_msg(2,"POST %s%s %s from %s",client.request->host,client.request->path,enctype, socket_peer(client.request->socket));
 client.request->query_vars = enctype && ncmp(enctype,_("multipart/form-data"),19) ?
   parse_multipart_body(client.request->query_vars,enctype) :
  enctype && ncmp(enctype,_("text/json"),9) ?
   parse_json(client.request->query_vars,client.request->contents,client.request->body):
  parse_uri_encoded(client.request->query_vars,client.request->contents,client.request->body);
 return get_method();
}

canonical_original_code:
int post_method()
{
 struct str_struct *enctype = find_header(client.request->headers,_("Content-Type"));
 log_msg(2,"POST %s%s %s from %s",client.request->host,client.request->path,enctype, socket_peer(client.request->socket));
 client.request->query_vars = enctype && ncmp(enctype,_("multipart/form-data"),19) ?
   parse_multipart_body(client.request->query_vars,enctype) :
  enctype && ncmp(enctype,_("text/json"),9) ?
   parse_json(client.request->query_vars,client.request->contents,client.request->body):
  parse_uri_encoded(client.request->query_vars,client.request->contents,client.request->body);
 return get_method();
}

variable_types: struct str_struct { int length; char *data; char contents[]; } *

return_type: int


decompiled_code:
__int64 __fastcall dispatch_method(__int64 a1)
{
__int64 *v1; // rbx
int v2; // ebp
int v3; // er12
__int64 result; // rax

if ( gdispatch[1] )
{
v1 = gdispatch;
v2 = 0;
while ( 1 )
{
v3 = *(_DWORD *)v1;
if ( v3 == (unsigned int)len(a1) && !strncasecmp((const char *)v1[1], *(const char **)(a1 + 8), *(int *)v1) )
break;
++v2;
v1 += 3;
if ( !v1[1] )
goto LABEL_7;
}
result = ((__int64 (*)(void))gdispatch[3 * v2 + 2])();
}
else
{
LABEL_7:
log_msg(0LL, "Bad request: %s", (const char *)a1);
result = error_handler(400LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 *v1; // rbx
int v2; // ebp
int v3; // er12
__int64 result; // rax

if ( gdispatch[1] )
{
v1 = gdispatch;
v2 = 0;
while ( 1 )
{
v3 = *(_DWORD *)v1;
if ( v3 == (unsigned int)len(a1) && !strncasecmp((const char *)v1[1], *(const char **)(a1 + 8), *(int *)v1) )
break;
++v2;
v1 += 3;
if ( !v1[1] )
goto LABEL_7;
}
result = ((__int64 (*)(void))gdispatch[3 * v2 + 2])();
}
else
{
LABEL_7:
log_msg(0LL, "Bad request: %s", (const char *)a1);
result = error_handler(400LL);
}
return result;
}

original_code:
int
dispatch_method(str method)
{
 int i;
 for (i = 0; gdispatch[i].name; ++i )
  if (gdispatch[i].len == len(method) && !strncasecmp(gdispatch[i].name,method->data,gdispatch[i].len))
   return gdispatch[i].handler();
 log_msg(0,"Bad request: %s",method);
 return error_handler(400);
}

canonical_original_code:
int dispatch_method(struct str_struct *method)
{
 int i;
 for (i = 0; gdispatch[i].name; ++i )
  if (gdispatch[i].len == len(method) && !strncasecmp(gdispatch[i].name,method->data,gdispatch[i].len))
   return gdispatch[i].handler();
 log_msg(0,"Bad request: %s",method);
 return error_handler(400);
}

variable_types: i int

user_defined_types: struct str_struct { int length; char *data; char contents[]; }


original_code:
int
mark_file(File fc, int i, int t, int o, int l)
{
 fc->parsed[i].kind = t;
 fc->parsed[i].offset = o;
 fc->parsed[i].length = l;
 return ++i;
}

canonical_original_code:
int mark_file(struct file_cache_struct *fc, int i, int t, int o, int l)
{
 fc->parsed[i].kind = t;
 fc->parsed[i].offset = o;
 fc->parsed[i].length = l;
 return ++i;
}

variable_types: t int

variable_types: o int

variable_types: l int

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

user_defined_types: struct parsed_struct { enum { TEXT=1, SCRIPT=2, EMIT=3 } kind; int offset; int length; }

user_defined_types: struct mime_struct { struct { int len; char *data; } ending; struct { int len; char *data; } type; int (*handler)(struct file_cache_struct *); }


original_code:
File
open_file(str filename)
{
 int fl = len(filename);
 File fc = (File)reserve(sizeof(struct file_cache_struct) + fl + 1);
 memcpy(&fc->name.contents[0],filename->data,fl);
 fc->name.data = fc->name.contents;
 fc->name.length = fl;
 fc->name.data[fl] = '\0';
 fc->fd = open(fc->name.data,00,0400);
 if (fc->fd < 0 || fstat(fc->fd,&fc->st)) {
  log_msg(0,"Failed to open file %s",filename);
  return ((void *)0);
 }
 if (!(fc->data = mmap(((void *)0),fc->st.st_size,0x1,0x01,fc->fd,0))) {
  log_msg(0,"Failed to memory map file %s",filename);
  close(fc->fd);
  return ((void *)0);
 }
 add_file_monitor(fc->fd, filename);
 fc->parsed = ((void *)0);
 fc->mime = ((void *)0);
 if (parseable_file(fc)) parse_file(fc);
 return fc;
}

canonical_original_code:
struct file_cache_struct *open_file(struct str_struct *filename)
{
 int fl = len(filename);
 struct file_cache_struct *fc = (File)reserve(sizeof(struct file_cache_struct) + fl + 1);
 memcpy(&fc->name.contents[0],filename->data,fl);
 fc->name.data = fc->name.contents;
 fc->name.length = fl;
 fc->name.data[fl] = '\0';
 fc->fd = open(fc->name.data,00,0400);
 if (fc->fd < 0 || fstat(fc->fd,&fc->st)) {
  log_msg(0,"Failed to open file %s",filename);
  return ((void *)0);
 }
 if (!(fc->data = mmap(((void *)0),fc->st.st_size,0x1,0x01,fc->fd,0))) {
  log_msg(0,"Failed to memory map file %s",filename);
  close(fc->fd);
  return ((void *)0);
 }
 add_file_monitor(fc->fd, filename);
 fc->parsed = ((void *)0);
 fc->mime = ((void *)0);
 if (parseable_file(fc)) parse_file(fc);
 return fc;
}

variable_types:
filename
struct str_struct *

variable_types: fl int

return_type: struct file_cache_struct { struct file_cache_struct *next; 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]; } st; struct parsed_struct { enum { TEXT=1, SCRIPT=2, EMIT=3 } kind; int offset; int length; } *parsed; struct mime_struct { struct { int len; char *data; } ending; struct { int len; char *data; } type; int (*handler)(struct file_cache_struct *); } *mime; char *data; int fd; struct str_struct { int length; char *data; char contents[]; } name; } *


reload

original_code:
File
reload(int fd)
{
 File fc = ((void *)0);
 for (int i = 0; i < server.file_index; ++i)
  if (server.files[i]->fd == fd)
   fc = server.files[i];
 if (! fc) return ((void *)0);
 munmap(fc->data,fc->st.st_size);
 close(fc->fd);
 fc->fd = open(fc->name.data,00,0400);
 if (fc->fd < 0 || fstat(fc->fd,&fc->st)) {
  log_msg(0,"Failed to open file %c",fc->name.data);
  return ((void *)0);
 }
 if (!(fc->data = mmap(((void *)0),fc->st.st_size,0x1,0x01,fc->fd,0))) {
  log_msg(0,"Failed to memory map file %c",fc->name.data);
  close(fc->fd);
  return ((void *)0);
 }
 return fc;
}

canonical_original_code:
struct file_cache_struct *reload(int fd)
{
 struct file_cache_struct *fc = ((void *)0);
 for (int i = 0; i < server.file_index; ++i)
  if (server.files[i]->fd == fd)
   fc = server.files[i];
 if (! fc) return ((void *)0);
 munmap(fc->data,fc->st.st_size);
 close(fc->fd);
 fc->fd = open(fc->name.data,00,0400);
 if (fc->fd < 0 || fstat(fc->fd,&fc->st)) {
  log_msg(0,"Failed to open file %c",fc->name.data);
  return ((void *)0);
 }
 if (!(fc->data = mmap(((void *)0),fc->st.st_size,0x1,0x01,fc->fd,0))) {
  log_msg(0,"Failed to memory map file %c",fc->name.data);
  close(fc->fd);
  return ((void *)0);
 }
 return fc;
}

variable_types: fd int


decompiled_code:
int close_files()
{
__int64 v0; // rbx
int result; // eax

if ( *((int *)&server + 2064) > 0 )
{
v0 = 0LL;
do
{
munmap(*(void **)(*((_QWORD *)&server + v0 + 8) + 168LL), *(_QWORD *)(*((_QWORD *)&server + v0 + 8) + 56LL));
result = close(*(_DWORD *)(*((_QWORD *)&server + v0++ + 8) + 176LL));
}
while ( *((_DWORD *)&server + 2064) > (int)v0 );
}
return result;
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rbx
int result; // eax

if ( *((int *)&server + 2064) > 0 )
{
v0 = 0LL;
do
{
munmap(*(void **)(*((_QWORD *)&server + v0 + 8) + 168LL), *(_QWORD *)(*((_QWORD *)&server + v0 + 8) + 56LL));
result = close(*(_DWORD *)(*((_QWORD *)&server + v0++ + 8) + 176LL));
}
while ( *((_DWORD *)&server + 2064) > (int)v0 );
}
return result;
}

original_code:
void
close_files()
{
 for (int i = 0; i < server.file_index; ++i) {
  munmap(server.files[i]->data,server.files[i]->st.st_size);
  close(server.files[i]->fd);
 }
}

return_type: void


decompiled_code:
__int64 __fastcall query_cache(__int64 a1)
{
__int64 v1; // rbx

if ( *((int *)&server + 2064) <= 0 )
return 0LL;
v1 = 0LL;
while ( !(unsigned int)cmp(a1, *((_QWORD *)&server + v1 + 8) + 184LL) )
{
if ( *((_DWORD *)&server + 2064) <= (int)++v1 )
return 0LL;
}
return *((_QWORD *)&server + (int)v1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rbx

if ( *((int *)&server + 2064) <= 0 )
return 0LL;
v1 = 0LL;
while ( !(unsigned int)cmp(a1, *((_QWORD *)&server + v1 + 8) + 184LL) )
{
if ( *((_DWORD *)&server + 2064) <= (int)++v1 )
return 0LL;
}
return *((_QWORD *)&server + (int)v1 + 8);
}

original_code:
File
query_cache(str filename)
{
 for (int i = 0; i < server.file_index; ++i)
  if (cmp(filename,&server.files[i]->name))
   return server.files[i];
 return ((void *)0);
}

canonical_original_code:
struct file_cache_struct *query_cache(struct str_struct *filename)
{
 for (int i = 0; i < server.file_index; ++i)
  if (cmp(filename,&server.files[i]->name))
   return server.files[i];
 return ((void *)0);
}


decompiled_code:
__int64 set_cwd()
{
char *v0; // rbx
size_t v1; // rax
__int64 result; // rax

v0 = getcwd(0LL, 0LL);
v1 = strlen(v0);
result = ref(v0, v1);
server[7] = result;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rbx
size_t v1; // rax
__int64 result; // rax

v0 = getcwd(0LL, 0LL);
v1 = strlen(v0);
result = ref(v0, v1);
server[7] = result;
return result;
}

original_code:
void
set_cwd()
{
 char* tmp = getcwd(((void *)0),0);
 server.cwd = ref(tmp,strlen(tmp));
}

canonical_original_code:
void set_cwd()
{
 char *tmp = getcwd(((void *)0),0);
 server.cwd = ref(tmp,strlen(tmp));
}

variable_types:
tmp
char *


decompiled_code:
__int64 temp_file()
{
time_t v0; // rax

v0 = time(0LL);
return _("/tmp/%i%i", server[2064], v0);
}

canonical_decompiled_code:
__int64 func6()
{
time_t v0; // rax

v0 = time(0LL);
return _("/tmp/%i%i", server[2064], v0);
}

original_code:
str
temp_file()
{
 return _("/tmp/%i%i",server.file_index,time(((void *)0)));
}

canonical_original_code:
struct str_struct *temp_file()
{
 return _("/tmp/%i%i",server.file_index,time(((void *)0)));
}

return_type: struct str_struct { int length; char *data; char contents[]; } *


load

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

if ( a1 )
result = query_cache(a1);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = func4(a1);
else
result = 0LL;
return result;
}

original_code:
File
load(str filename)
{
 if (!filename) return ((void *)0);
 return query_cache(filename);
}

canonical_original_code:
struct file_cache_struct *load(struct str_struct *filename)
{
 if (!filename) return ((void *)0);
 return query_cache(filename);
}


decompiled_code:
int __fastcall load_directory(__int64 a1)
{
FTS *v1; // rbp
FTSENT *i; // rbx
char *v4; // rbx
size_t v5; // rax
const char *v6; // r12
char *v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[0] = *(char **)(a1 + 8);
v7[1] = 0LL;
v1 = fts_open(v7, 2, 0LL);
log_msg(3LL, "Opening directory %s", (const char *)a1);
for ( i = fts_read(v1); i; i = fts_read(v1) )
{
log_msg(3LL, "Testing %c", i->fts_name);
if ( (!i->fts_namelen || i->fts_name[0] != 46) && (i->fts_info & 8) != 0 )
{
v4 = i->fts_accpath;
v5 = strlen(v4);
v6 = (const char *)ref(v4, v5);
log_msg(3LL, "Opening file %s", v6);
LODWORD(v4) = server[2064];
server[2064] = (_DWORD)v4 + 1;
*(_QWORD *)&server[2 * (int)v4 + 16] = open_file((__int64)v6);
}
}
return fts_close(v1);
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
FTS *v1; // rbp
FTSENT *i; // rbx
char *v4; // rbx
size_t v5; // rax
const char *v6; // r12
char *v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[0] = *(char **)(a1 + 8);
v7[1] = 0LL;
v1 = fts_open(v7, 2, 0LL);
log_msg(3LL, "Opening directory %s", (const char *)a1);
for ( i = fts_read(v1); i; i = fts_read(v1) )
{
log_msg(3LL, "Testing %c", i->fts_name);
if ( (!i->fts_namelen || i->fts_name[0] != 46) && (i->fts_info & 8) != 0 )
{
v4 = i->fts_accpath;
v5 = strlen(v4);
v6 = (const char *)ref(v4, v5);
log_msg(3LL, "Opening file %s", v6);
LODWORD(v4) = server[2064];
server[2064] = (_DWORD)v4 + 1;
*(_QWORD *)&server[2 * (int)v4 + 16] = func1((__int64)v6);
}
}
return fts_close(v1);
}

original_code:
void
load_directory(str directory)
{
 char *path[2] = { directory->data, ((void *)0) };
 FTS* f = fts_open(path,0x0002,((void *)0));
 FTSENT* fe;
 log_msg(3,"Opening directory %s", directory);
 for (fe = fts_read(f); fe; fe = fts_read(f)) {
  log_msg(3,"Testing %c", fe->fts_name);
  if (fe->fts_namelen > 0 && fe->fts_name[0] == '.') continue;
  if (fe->fts_info & 8) {
   str filename = ref(fe->fts_accpath,strlen(fe->fts_accpath));
   log_msg(3,"Opening file %s",filename);
   server.files[server.file_index++] = open_file(filename);
  }
 }
 fts_close(f);
}

canonical_original_code:
void load_directory(struct str_struct *directory)
{
 char *path[2] = { directory->data, ((void *)0) };
 struct FTS *f = fts_open(path,0x0002,((void *)0));
 struct _ftsent *fe;
 log_msg(3,"Opening directory %s", directory);
 for (fe = fts_read(f); fe; fe = fts_read(f)) {
  log_msg(3,"Testing %c", fe->fts_name);
  if (fe->fts_namelen > 0 && fe->fts_name[0] == '.') continue;
  if (fe->fts_info & 8) {
   struct str_struct *filename = ref(fe->fts_accpath,strlen(fe->fts_accpath));
   log_msg(3,"Opening file %s",filename);
   server.files[server.file_index++] = open_file(filename);
  }
 }
 fts_close(f);
}

variable_types:
path
char *[2]

variable_types: struct FTS { struct _ftsent { struct _ftsent *fts_cycle; struct _ftsent *fts_parent; struct _ftsent *fts_link; long fts_number; void *fts_pointer; char *fts_accpath; char *fts_path; int fts_errno; int fts_symfd; unsigned short fts_pathlen; unsigned short fts_namelen; unsigned long fts_ino; unsigned long fts_dev; unsigned long fts_nlink; short fts_level; unsigned short fts_info; unsigned short fts_flags; unsigned short fts_instr; 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]; } *fts_statp; char fts_name[1]; } *fts_cur; struct _ftsent *fts_child; struct _ftsent **fts_array; unsigned long fts_dev; char *fts_path; int fts_rfd; int fts_pathlen; int fts_nitems; int (*fts_compar)(void *, void *); int fts_options; } *

user_defined_types: struct FTS { struct _ftsent *fts_cur; struct _ftsent *fts_child; struct _ftsent **fts_array; unsigned long fts_dev; char *fts_path; int fts_rfd; int fts_pathlen; int fts_nitems; int (*fts_compar)(void *, void *); int fts_options; }

user_defined_types: struct _ftsent { struct _ftsent *fts_cycle; struct _ftsent *fts_parent; struct _ftsent *fts_link; long fts_number; void *fts_pointer; char *fts_accpath; char *fts_path; int fts_errno; int fts_symfd; unsigned short fts_pathlen; unsigned short fts_namelen; unsigned long fts_ino; unsigned long fts_dev; unsigned long fts_nlink; short fts_level; unsigned short fts_info; unsigned short fts_flags; unsigned short fts_instr; struct stat *fts_statp; char fts_name[1]; }


decompiled_code:
int load_files()
{
__int64 v0; // rax

set_cwd();
v0 = _("%s", *(const char **)&server[14]);
return load_directory(v0);
}

canonical_decompiled_code:
int func9()
{
__int64 v0; // rax

func5();
v0 = _("%s", *(const char **)&server[14]);
return func8(v0);
}

original_code:
void
load_files()
{
 set_cwd();
 load_directory(_("%s",server.cwd));
}


Date

decompiled_code:
char **__fastcall Date(time_t a1)
{
char **v1; // rbx
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char **)blank(32LL);
v2 = gmtime(&timer);
*(_DWORD *)v1 = strftime(v1[1], *(int *)v1, "%a, %d %b %Y %H:%M:%S +0000", v2);
return v1;
}

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

timer = a1;
v1 = (char **)blank(32LL);
v2 = gmtime(&timer);
*(_DWORD *)v1 = strftime(v1[1], *(int *)v1, "%a, %d %b %Y %H:%M:%S +0000", v2);
return v1;
}

original_code:
str
Date(time_t now)
{
 str retval = blank(32);
 struct tm* now_tm = gmtime(&now);
 retval->length = strftime(retval->data,retval->length,"%a, %d %b %Y %H:%M:%S +0000",now_tm);
 return retval;
}

canonical_original_code:
struct str_struct *Date(long now)
{
 struct str_struct *retval = blank(32);
 struct tm *now_tm = gmtime(&now);
 retval->length = strftime(retval->data,retval->length,"%a, %d %b %Y %H:%M:%S +0000",now_tm);
 return retval;
}

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


Expires

decompiled_code:
char **Expires()
{
time_t v0; // rax

v0 = time(0LL);
return Date(v0 + 86400);
}

canonical_decompiled_code:
char **func1()
{
time_t v0; // rax

v0 = time(0LL);
return func0(v0 + 86400);
}

original_code:
str
Expires()
{
 time_t now = 86400 + time(((void *)0));
 return Date(now);
}

canonical_original_code:
struct str_struct *Expires()
{
 long now = 86400 + time(((void *)0));
 return Date(now);
}


MicroTime

decompiled_code:
char **MicroTime()
{
char **result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
result = Expires();
else
result = (char **)_("%i", v1.tv_usec + (v1.tv_sec << 20));
return result;
}

canonical_decompiled_code:
char **func2()
{
char **result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
result = func1();
else
result = (char **)_("%i", v1.tv_usec + (v1.tv_sec << 20));
return result;
}

original_code:
str
MicroTime()
{
 struct timeval tv;
 if (gettimeofday(&tv,((void *)0))) return Expires();
 return _("%i", (tv.tv_sec << 20) + tv.tv_usec);
}

canonical_original_code:
struct str_struct *MicroTime()
{
 struct timeval tv;
 if (gettimeofday(&tv,((void *)0))) return Expires();
 return _("%i", (tv.tv_sec << 20) + tv.tv_usec);
}

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


disconnect

decompiled_code:
void __noreturn disconnect()
{
close_socket(*((_QWORD *)&client + 3));
close(client);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
close_socket(*((_QWORD *)&client + 3));
close(client);
exit(0);
}

original_code:
void
disconnect()
{
 close_socket(client.socket);
 close(client.kq);
 exit(0);
}


decompiled_code:
__int64 retry_request()
{
__int64 v0; // rax

v0 = *((_QWORD *)&client + 4);
if ( *(int *)(v0 + 108) > 19 )
{
log_msg(0LL, "Failed to read request\n");
disconnect();
}
return add_read_socket(**(_QWORD **)v0);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rax

v0 = *((_QWORD *)&client + 4);
if ( *(int *)(v0 + 108) > 19 )
{
log_msg(0LL, "Failed to read request\n");
func0();
}
return add_read_socket(**(_QWORD **)v0);
}

original_code:
void
retry_request()
{
 if (client.request->retries >= 20) {
  log_msg(0,"Failed to read request\n");
  disconnect();
  return;
 }
 add_read_socket(client.request->socket->fd);
}


decompiled_code:
__int64 read_request()
{
__int64 v0; // rax

if ( (unsigned int)process_request() )
return retry_request();
v0 = *((_QWORD *)&client + 4);
*(_DWORD *)(v0 + 108) = 0;
if ( !*(_DWORD *)(v0 + 96) )
return add_read_socket(**((_QWORD **)&client + 3));
*(_QWORD *)(*((_QWORD *)&client + 3) + 16LL) = 0LL;
*((_QWORD *)&client + 5) = new_response(*((_QWORD *)&client + 4));
parse_path(*((_QWORD *)&client + 4));
return add_write_socket(***((_QWORD ***)&client + 5));
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax

if ( (unsigned int)process_request() )
return func1();
v0 = *((_QWORD *)&client + 4);
*(_DWORD *)(v0 + 108) = 0;
if ( !*(_DWORD *)(v0 + 96) )
return add_read_socket(**((_QWORD **)&client + 3));
*(_QWORD *)(*((_QWORD *)&client + 3) + 16LL) = 0LL;
*((_QWORD *)&client + 5) = new_response(*((_QWORD *)&client + 4));
parse_path(*((_QWORD *)&client + 4));
return add_write_socket(***((_QWORD ***)&client + 5));
}

original_code:
void
read_request()
{
 if (process_request()) {
  retry_request();
  return;
 }
 client.request->retries = 0;
 if (!client.request->done) {
  add_read_socket(client.socket->fd);
  return;
 }
 client.socket->buf = ((void *)0);
 client.response = new_response(client.request);
 parse_path(client.request);
 add_write_socket(client.response->socket->fd);
 return;
}


decompiled_code:
void __noreturn send_response()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v2; // rbp
__int64 v3; // rbx
int v4; // eax

v0 = _("Host");
v1 = find_header(*(_QWORD *)(*((_QWORD *)&client + 4) + 16LL), v0);
v2 = parse_host(*((_QWORD *)&client + 4), v1);
v3 = parse_method(*((_QWORD *)&client + 4));
begin_response();
if ( v2 && v3 )
v4 = dispatch_method(v3);
else
v4 = error_handler(400LL);
*(_DWORD *)(*((_QWORD *)&client + 5) + 40LL) = v4;
end_response();
disconnect();
}

canonical_decompiled_code:
void __noreturn func3()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v2; // rbp
__int64 v3; // rbx
int v4; // eax

v0 = _("Host");
v1 = find_header(*(_QWORD *)(*((_QWORD *)&client + 4) + 16LL), v0);
v2 = parse_host(*((_QWORD *)&client + 4), v1);
v3 = parse_method(*((_QWORD *)&client + 4));
begin_response();
if ( v2 && v3 )
v4 = dispatch_method(v3);
else
v4 = error_handler(400LL);
*(_DWORD *)(*((_QWORD *)&client + 5) + 40LL) = v4;
end_response();
func0();
}

original_code:
void
send_response()
{
 str host = parse_host(client.request,find_header(client.request->headers,_("Host")));
 str method = parse_method(client.request);
 begin_response();
 client.response->status = host && method ? dispatch_method(method) : error_handler(400);
 end_response();
 disconnect();
}

canonical_original_code:
void send_response()
{
 struct str_struct *host = parse_host(client.request,find_header(client.request->headers,_("Host")));
 struct str_struct *method = parse_method(client.request);
 begin_response();
 client.response->status = host && method ? dispatch_method(method) : error_handler(400);
 end_response();
 disconnect();
}


decompiled_code:
__int64 write_request()
{
_QWORD **v0; // rax

if ( (unsigned int)send_request(*((_QWORD *)&client + 4)) )
return add_req_socket(***((_QWORD ***)&client + 4));
v0 = (_QWORD **)new_response(*((_QWORD *)&client + 4));
*((_QWORD *)&client + 5) = v0;
return add_resp_socket(**v0);
}

canonical_decompiled_code:
__int64 func5()
{
_QWORD **v0; // rax

if ( (unsigned int)send_request(*((_QWORD *)&client + 4)) )
return add_req_socket(***((_QWORD ***)&client + 4));
v0 = (_QWORD **)new_response(*((_QWORD *)&client + 4));
*((_QWORD *)&client + 5) = v0;
return add_resp_socket(**v0);
}

original_code:
void
write_request()
{
 if (send_request(client.request)) {
  add_req_socket(client.request->socket->fd);
  return;
 }
 client.response = new_response(client.request);
 add_resp_socket(client.response->socket->fd);
}


decompiled_code:
__int64 client_poll()
{
unsigned int v0; // eax
__int64 result; // rax

v0 = *(&client + 4);
if ( v0 == 2 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Response failed") )
disconnect();
result = read_response();
}
else if ( v0 > 2 )
{
if ( v0 == 3 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Request failed") )
disconnect();
result = write_request();
}
else
{
result = log_msg(3LL, "UNKNOWN EVENT");
}
}
else
{
if ( v0 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Write failed") )
disconnect();
send_response();
}
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Read failed") )
disconnect();
result = read_request();
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int v0; // eax
__int64 result; // rax

v0 = *(&client + 4);
if ( v0 == 2 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Response failed") )
func0();
result = func4();
}
else if ( v0 > 2 )
{
if ( v0 == 3 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Request failed") )
func0();
result = func5();
}
else
{
result = log_msg(3LL, "UNKNOWN EVENT");
}
}
else
{
if ( v0 )
{
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Write failed") )
func0();
func3();
}
if ( (unsigned int)closed_socket(*((_QWORD *)&client + 3), "Read failed") )
func0();
result = func2();
}
return result;
}

original_code:
void
client_poll(uint64_t fd, enum event_types t)
{
 switch (client.event) {
 case READ:
  closed_socket(client.socket,"Read failed") ? disconnect() : read_request();
  break;
 case WRITE:
  closed_socket(client.socket,"Write failed") ? disconnect() : send_response();
  break;
 case REQ:
  closed_socket(client.socket,"Request failed") ? disconnect() : write_request();
  break;
 case RESP:
  closed_socket(client.socket,"Response failed") ? disconnect() : read_response();
  break;
 default:
  log_msg(3,"UNKNOWN EVENT");
 }
}

canonical_original_code:
void client_poll(unsigned long fd, enum event_types t)
{
 switch (client.event) {
 case READ:
  closed_socket(client.socket,"Read failed") ? disconnect() : read_request();
  break;
 case WRITE:
  closed_socket(client.socket,"Write failed") ? disconnect() : send_response();
  break;
 case REQ:
  closed_socket(client.socket,"Request failed") ? disconnect() : write_request();
  break;
 case RESP:
  closed_socket(client.socket,"Response failed") ? disconnect() : read_response();
  break;
 default:
  log_msg(3,"UNKNOWN EVENT");
 }
}

variable_types:
fd
unsigned long

variable_types: enum event_types { READ=0, WRITE=1, RESP=2, REQ=3, NODE=4, PROC=5 }


run

decompiled_code:
__int64 run()
{
return poll_events(*(_QWORD *)client, client_poll);
}

canonical_decompiled_code:
__int64 func7()
{
return poll_events(*(_QWORD *)client, client_poll);
}

original_code:
void
run()
{
 poll_events(client.kq, client_poll);
}


handle

decompiled_code:
void __fastcall __noreturn handle(__int64 fd)
{
__int64 v1; // rsi

v1 = 0LL;
if ( server[2] != fd )
v1 = server[5];
*(_QWORD *)&client[6] = accept_socket(fd, v1);
close(fd);
nodelay(**(WINDOW ***)&client[6], v1);
*(_QWORD *)client = epoll_create1(0);
*(_QWORD *)&client[8] = open_request(*(_QWORD *)&client[6]);
add_read_socket(**(_QWORD **)&client[6]);
timeout(5);
timer(5LL, 0LL);
while ( !*(_DWORD *)(*(_QWORD *)&client[6] + 40LL) )
{
if ( *(_QWORD *)&client[2] )
break;
run();
}
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(__int64 fd)
{
__int64 v1; // rsi

v1 = 0LL;
if ( server[2] != fd )
v1 = server[5];
*(_QWORD *)&client[6] = accept_socket(fd, v1);
close(fd);
nodelay(**(WINDOW ***)&client[6], v1);
*(_QWORD *)client = epoll_create1(0);
*(_QWORD *)&client[8] = open_request(*(_QWORD *)&client[6]);
add_read_socket(**(_QWORD **)&client[6]);
timeout(5);
timer(5LL, 0LL);
while ( !*(_DWORD *)(*(_QWORD *)&client[6] + 40LL) )
{
if ( *(_QWORD *)&client[2] )
break;
func7();
}
exit(0);
}

original_code:
void
handle(uint64_t fd)
{
 client.socket = accept_socket(fd,(server.http_sock == fd ? ((void *)0) : server.tls));
 close(fd);
 nodelay(client.socket->fd);
 client.kq = epoll_create1(0);
 client.request = open_request(client.socket);
 add_read_socket(client.socket->fd);
 timeout(5,0);
 timer();
 while (!client.socket->closed && !client.alarm) run();
 exit(0);
}

canonical_original_code:
void handle(unsigned long fd)
{
 client.socket = accept_socket(fd,(server.http_sock == fd ? ((void *)0) : server.tls));
 close(fd);
 nodelay(client.socket->fd);
 client.kq = epoll_create1(0);
 client.request = open_request(client.socket);
 add_read_socket(client.socket->fd);
 timeout(5,0);
 timer();
 while (!client.socket->closed && !client.alarm) run();
 exit(0);
}


decompiled_code:
void signal_handler()
{
__int64 v0; // rax

v0 = **((_QWORD **)&client + 5);
if ( v0 )
*(_DWORD *)(v0 + 40) = 1;
}

original_code:
void
signal_handler(int sig)
{
 if (client.response->socket) client.response->socket->closed = 1;
}

variable_types: sig int


decompiled_code:
__sighandler_t socket_signal_handlers()
{
return signal(13, (__sighandler_t)signal_handler);
}

canonical_decompiled_code:
__sighandler_t func1()
{
return signal(13, (__sighandler_t)signal_handler);
}

original_code:
void
socket_signal_handlers()
{
 signal(13,signal_handler);
}


nonblock

decompiled_code:
int __fastcall nonblock(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
int
nonblock(uint64_t fd)
{
 int flags = fcntl(fd,3,0);
 return fcntl(fd,4,flags | 04000);
}

canonical_original_code:
int nonblock(unsigned long fd)
{
 int flags = fcntl(fd,3,0);
 return fcntl(fd,4,flags | 04000);
}

variable_types: flags int


keepalive

decompiled_code:
int __fastcall keepalive(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 9, optval, 4u);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 9, optval, 4u);
}

original_code:
int
keepalive(uint64_t fd)
{
 int value = 1 ;
 return setsockopt(fd,1,9,&value,sizeof(int));
}

canonical_original_code:
int keepalive(unsigned long fd)
{
 int value = 1;
 return setsockopt(fd,1,9,&value,sizeof(int));
}

variable_types: value int


nodelay

decompiled_code:
int __fastcall nodelay(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 6, 1, optval, 4u);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 6, 1, optval, 4u);
}

original_code:
int
nodelay(uint64_t fd)
{
 int value = 1;
 return setsockopt(fd,IPPROTO_TCP,1,&value,sizeof(int));
}

canonical_original_code:
int nodelay(unsigned long fd)
{
 int value = 1;
 return setsockopt(fd,IPPROTO_TCP,1,&value,sizeof(int));
}


decompiled_code:
int __fastcall socket_timeout(int fd, int a2)
{
int result; // eax

timeout(a2);
result = setsockopt(fd, 1, 20, &Timeout, 0x20u);
if ( result )
result = setsockopt(fd, 1, 21, &Timeout, 0x20u) != 0;
return result;
}

canonical_decompiled_code:
int __fastcall func5(int fd, int a2)
{
int result; // eax

timeout(a2);
result = setsockopt(fd, 1, 20, &Timeout, 0x20u);
if ( result )
result = setsockopt(fd, 1, 21, &Timeout, 0x20u) != 0;
return result;
}

original_code:
int
socket_timeout(uint64_t fd, size_t seconds)
{
 timeout(seconds,0);
 return setsockopt(fd,1,20,&Timeout,sizeof(Timeout))
  && setsockopt(fd,1,21,&Timeout,sizeof(Timeout));
}

canonical_original_code:
int socket_timeout(unsigned long fd, unsigned long seconds)
{
 timeout(seconds,0);
 return setsockopt(fd,1,20,&Timeout,sizeof(Timeout))
  && setsockopt(fd,1,21,&Timeout,sizeof(Timeout));
}

variable_types:
seconds
unsigned long


decompiled_code:
__int64 __fastcall new_socket(int a1)
{
unsigned int v1; // ebx
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = socket(2, (a1 == 0) + 1, 0);
if ( setsockopt(v1, 1, 2, optval, 4u) )
{
perror("setsockopt");
exit(0);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
unsigned int v1; // ebx
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = socket(2, (a1 == 0) + 1, 0);
if ( setsockopt(v1, 1, 2, optval, 4u) )
{
perror("setsockopt");
exit(0);
}
return v1;
}

original_code:
int
new_socket(int stream)
{
 int one = 1;
 uint64_t fd = socket(2,stream ? SOCK_STREAM : SOCK_DGRAM,0);
 if (0 > fd) return 0;
 if (setsockopt(fd,1,2,&one,sizeof(one))) {
  perror("setsockopt");
  exit(0);
 }
 return fd;
}

canonical_original_code:
int new_socket(int stream)
{
 int one = 1;
 unsigned long fd = socket(2,stream ? SOCK_STREAM : SOCK_DGRAM,0);
 if (0 > fd) return 0;
 if (setsockopt(fd,1,2,&one,sizeof(one))) {
  perror("setsockopt");
  exit(0);
 }
 return fd;
}

variable_types: stream int

variable_types: one int


decompiled_code:
__int64 __fastcall bind_socket(int fd, unsigned int a2, unsigned int a3)
{
unsigned int v4; // er12

attachTo(a2, a3);
v4 = fd;
if ( bind(fd, &Address, 0x10u) )
{
log_msg(0LL, "Failed to bind to port %i\n", a3);
close(fd);
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, unsigned int a2, unsigned int a3)
{
unsigned int v4; // er12

attachTo(a2, a3);
v4 = fd;
if ( bind(fd, &Address, 0x10u) )
{
log_msg(0LL, "Failed to bind to port %i\n", a3);
close(fd);
v4 = 0;
}
return v4;
}

original_code:
int
bind_socket(uint64_t fd, IPAddress addr, int port)
{
 attachTo(addr,port);
 if (bind(fd,(struct sockaddr*)&Address,sizeof(Address))) {
  log_msg(0,"Failed to bind to port %i\n",port);
  close(fd);
  return 0;
 }
 return fd;
}

canonical_original_code:
int bind_socket(unsigned long fd, unsigned int addr, int port)
{
 attachTo(addr,port);
 if (bind(fd,(struct sockaddr *)&Address,sizeof(Address))) {
  log_msg(0,"Failed to bind to port %i\n",port);
  close(fd);
  return 0;
 }
 return fd;
}

variable_types:
addr
unsigned int

variable_types: port int

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


decompiled_code:
__int64 __fastcall listen_socket(int fd)
{
unsigned int v1; // ebp

v1 = fd;
if ( listen(fd, 128) )
{
close(fd);
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd)
{
unsigned int v1; // ebp

v1 = fd;
if ( listen(fd, 128) )
{
close(fd);
v1 = 0;
}
return v1;
}

original_code:
int
listen_socket(uint64_t fd)
{
 if (listen(fd,128)) {
  close(fd);
  return 0;
 }
 return fd;
}

canonical_original_code:
int listen_socket(unsigned long fd)
{
 if (listen(fd,128)) {
  close(fd);
  return 0;
 }
 return fd;
}


decompiled_code:
__int64 __fastcall open_socket(unsigned int a1)
{
unsigned int v1; // ebx

v1 = new_socket(1);
if ( !(unsigned int)bind_socket(v1, 0, a1) )
log_msg(0LL, "Failed to bind to port %i", a1);
if ( !(unsigned int)listen_socket(v1) )
log_msg(0LL, "Failed to listen to port %i", a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
unsigned int v1; // ebx

v1 = func6(1);
if ( !(unsigned int)func7(v1, 0, a1) )
log_msg(0LL, "Failed to bind to port %i", a1);
if ( !(unsigned int)func8(v1) )
log_msg(0LL, "Failed to listen to port %i", a1);
return v1;
}

original_code:
int
open_socket(int port)
{
 uint64_t fd = new_socket(1);
 if (!bind_socket(fd,(IPAddress)0,port)) log_msg(0,"Failed to bind to port %i",port);
 if (!listen_socket(fd)) log_msg(0,"Failed to listen to port %i",port);
 return fd;
}

canonical_original_code:
int open_socket(int port)
{
 unsigned long fd = new_socket(1);
 if (!bind_socket(fd,(unsigned int)0,port)) log_msg(0,"Failed to bind to port %i",port);
 if (!listen_socket(fd)) log_msg(0,"Failed to listen to port %i",port);
 return fd;
}


fields

decompiled_code:
__int64 fields()
{
__int64 result; // rax

result = 0LL;
if ( *(_QWORD *)(db + 8) )
result = PQnfields();
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax

result = 0LL;
if ( *(_QWORD *)(db + 8) )
result = PQnfields();
return result;
}

original_code:
int
fields()
{
 if (!db->res) return 0;
 return PQnfields(db->res);
}


field

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

result = *(_QWORD *)(db + 8);
if ( result )
{
v2 = PQfname(*(_QWORD *)(db + 8), a1);
result = _(&unk_348, v2);
}
return result;
}

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

result = *(_QWORD *)(db + 8);
if ( result )
{
v2 = PQfname(*(_QWORD *)(db + 8), a1);
result = _(&unk_348, v2);
}
return result;
}

original_code:
str
field(int i)
{
 if (!db->res) return ((void *)0);
 return _("%c",PQfname(db->res,i));
}

canonical_original_code:
struct str_struct *field(int i)
{
 if (!db->res) return ((void *)0);
 return _("%c",PQfname(db->res,i));
}

variable_types: i int


decompiled_code:
__int64 db_error()
{
__int64 result; // rax
__int64 v1; // rax

result = *(_QWORD *)(db + 8);
if ( result )
{
v1 = PQresultErrorMessage(*(_QWORD *)(db + 8));
result = _(&unk_348, v1);
}
return result;
}

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

result = *(_QWORD *)(db + 8);
if ( result )
{
v1 = PQresultErrorMessage(*(_QWORD *)(db + 8));
result = _(&unk_348, v1);
}
return result;
}

original_code:
str
db_error()
{
 if (!db->res) return ((void *)0);
 return _("%c",PQresultErrorMessage(db->res));
}

canonical_original_code:
struct str_struct *db_error()
{
 if (!db->res) return ((void *)0);
 return _("%c",PQresultErrorMessage(db->res));
}


fetch

decompiled_code:
__int64 __fastcall fetch(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
__int64 v3; // rax

result = *(_QWORD *)(db + 8);
if ( result )
{
v3 = PQgetvalue(*(_QWORD *)(db + 8), a1, a2);
result = _(&unk_348, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
__int64 v3; // rax

result = *(_QWORD *)(db + 8);
if ( result )
{
v3 = PQgetvalue(*(_QWORD *)(db + 8), a1, a2);
result = _(&unk_348, v3);
}
return result;
}

original_code:
str
fetch(int row, int col)
{
 if (!db->res) return ((void *)0);
 return _("%c",PQgetvalue(db->res,row,col));
}

canonical_original_code:
struct str_struct *fetch(int row, int col)
{
 if (!db->res) return ((void *)0);
 return _("%c",PQgetvalue(db->res,row,col));
}

variable_types: row int

variable_types: col int


reset

decompiled_code:
__int64 reset()
{
__int64 result; // rax

if ( *(_QWORD *)(db + 8) )
PQclear();
result = db;
*(_QWORD *)(db + 8) = 0LL;
return result;
}

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

if ( *(_QWORD *)(db + 8) )
PQclear();
result = db;
*(_QWORD *)(db + 8) = 0LL;
return result;
}

original_code:
void
reset()
{
 if (db->res) PQclear(db->res);
 db->res = ((void *)0);
}


query

decompiled_code:
__int64 __fastcall query(const void **a1)
{
void **v1; // rbp
__int64 v2; // r12
unsigned int v3; // eax
__int64 result; // rax

if ( *(_QWORD *)(db + 8) )
reset();
log_msg(1LL, &unk_34B, a1);
v1 = (void **)blank((unsigned int)(*(_DWORD *)a1 + 1));
memset(v1[1], 0, *(int *)v1);
memcpy(v1[1], a1[1], *(int *)a1);
v2 = db;
*(_QWORD *)(v2 + 8) = PQexec(*(_QWORD *)db, v1[1]);
v3 = PQresultStatus(*(_QWORD *)(db + 8));
if ( v3 <= 1 )
{
PQclear(*(_QWORD *)(db + 8));
*(_QWORD *)(db + 8) = 0LL;
result = 0LL;
}
else if ( v3 == 2 )
{
result = PQntuples(*(_QWORD *)(db + 8));
}
else
{
PQresultErrorMessage(*(_QWORD *)(db + 8));
log_msg(1LL, "%s failed %c", a1);
PQclear(*(_QWORD *)(db + 8));
*(_QWORD *)(db + 8) = 0LL;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(const void **a1)
{
void **v1; // rbp
__int64 v2; // r12
unsigned int v3; // eax
__int64 result; // rax

if ( *(_QWORD *)(db + 8) )
func4();
log_msg(1LL, &unk_34B, a1);
v1 = (void **)blank((unsigned int)(*(_DWORD *)a1 + 1));
memset(v1[1], 0, *(int *)v1);
memcpy(v1[1], a1[1], *(int *)a1);
v2 = db;
*(_QWORD *)(v2 + 8) = PQexec(*(_QWORD *)db, v1[1]);
v3 = PQresultStatus(*(_QWORD *)(db + 8));
if ( v3 <= 1 )
{
PQclear(*(_QWORD *)(db + 8));
*(_QWORD *)(db + 8) = 0LL;
result = 0LL;
}
else if ( v3 == 2 )
{
result = PQntuples(*(_QWORD *)(db + 8));
}
else
{
PQresultErrorMessage(*(_QWORD *)(db + 8));
log_msg(1LL, "%s failed %c", a1);
PQclear(*(_QWORD *)(db + 8));
*(_QWORD *)(db + 8) = 0LL;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
query(str q)
{
 if (db->res) reset();
 log_msg(1,"%s",q);
 str qry = blank(q->length + 1);
 memset(qry->data,0,qry->length);
 memcpy(qry->data,q->data,q->length);
 db->res = PQexec(db->conn,qry->data);
 switch(PQresultStatus(db->res)) {
  case PGRES_EMPTY_QUERY:
  case PGRES_COMMAND_OK:
   PQclear(db->res);
   db->res = ((void *)0);
   return 0;
  case PGRES_TUPLES_OK:
   return PQntuples(db->res);
  case PGRES_BAD_RESPONSE:
  case PGRES_NONFATAL_ERROR:
  case PGRES_FATAL_ERROR:
  default:
   log_msg(1,"%s failed %c", q, PQresultErrorMessage(db->res));
   PQclear(db->res);
   db->res = ((void *)0);
 }
 return -1;
}

canonical_original_code:
int query(struct str_struct *q)
{
 if (db->res) reset();
 log_msg(1,"%s",q);
 struct str_struct *qry = blank(q->length + 1);
 memset(qry->data,0,qry->length);
 memcpy(qry->data,q->data,q->length);
 db->res = PQexec(db->conn,qry->data);
 switch(PQresultStatus(db->res)) {
  case PGRES_EMPTY_QUERY:
  case PGRES_COMMAND_OK:
   PQclear(db->res);
   db->res = ((void *)0);
   return 0;
  case PGRES_TUPLES_OK:
   return PQntuples(db->res);
  case PGRES_BAD_RESPONSE:
  case PGRES_NONFATAL_ERROR:
  case PGRES_FATAL_ERROR:
  default:
   log_msg(1,"%s failed %c", q, PQresultErrorMessage(db->res));
   PQclear(db->res);
   db->res = ((void *)0);
 }
 return -1;
}

variable_types: struct str_struct { int length; char *data; char contents[]; } *


singlequote

decompiled_code:
__int64 __fastcall singlequote(__int64 a1)
{
__int64 v1; // rbx
int v3; // eax
__int64 v4; // rbp
int v5; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
v6[0] = 0;
if ( (unsigned int)((__int64 (*)(void))len)() )
{
v3 = len(a1);
v4 = blank((unsigned int)(2 * v3));
v5 = len(a1);
*(_DWORD *)v4 = PQescapeStringConn(*(_QWORD *)db, *(_QWORD *)(v4 + 8), *(_QWORD *)(a1 + 8), v5, v6);
if ( v6[0] )
log_msg(1LL, "Failed to escape string %s", (const char *)a1);
v1 = v4;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 v1; // rbx
int v3; // eax
__int64 v4; // rbp
int v5; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
v6[0] = 0;
if ( (unsigned int)((__int64 (*)(void))len)() )
{
v3 = len(a1);
v4 = blank((unsigned int)(2 * v3));
v5 = len(a1);
*(_DWORD *)v4 = PQescapeStringConn(*(_QWORD *)db, *(_QWORD *)(v4 + 8), *(_QWORD *)(a1 + 8), v5, v6);
if ( v6[0] )
log_msg(1LL, "Failed to escape string %s", (const char *)a1);
v1 = v4;
}
return v1;
}

original_code:
str
singlequote(str s)
{
 int err = 0;
 if (len(s) == 0) return s;
 str retval = blank(len(s)*2);
 retval->length = PQescapeStringConn(db->conn,retval->data,s->data,len(s),&err);
 if (err) log_msg(1,"Failed to escape string %s",s);
 return retval;
}

canonical_original_code:
struct str_struct *singlequote(struct str_struct *s)
{
 int err = 0;
 if (len(s) == 0) return s;
 struct str_struct *retval = blank(len(s)*2);
 retval->length = PQescapeStringConn(db->conn,retval->data,s->data,len(s),&err);
 if (err) log_msg(1,"Failed to escape string %s",s);
 return retval;
}

variable_types:
s
struct str_struct *

variable_types: err int


attachTo

decompiled_code:
__int64 __fastcall attachTo(int a1, __int16 a2)
{
*((_QWORD *)&Address + 1) = 0LL;
*(&Address + 1) = _bswap_16(a2);
*((_DWORD *)&Address + 1) = a1;
Address = 2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int16 a2)
{
*((_QWORD *)&Address + 1) = 0LL;
*(&Address + 1) = func0(a2);
*((_DWORD *)&Address + 1) = a1;
Address = 2;
return 0LL;
}

original_code:
int
attachTo(IPAddress addr, int port)
{
 memset(&Address,0,sizeof(Address));
 Address.sin_port = htons(port);
 Address.sin_addr.s_addr = addr;
 Address.sin_family = 2;
 return 0;
}

canonical_original_code:
int attachTo(unsigned int addr, int port)
{
 memset(&Address,0,sizeof(Address));
 Address.sin_port = htons(port);
 Address.sin_addr.s_addr = addr;
 Address.sin_family = 2;
 return 0;
}


ipaddress

decompiled_code:
__int64 __fastcall ipaddress(unsigned int a1, unsigned int a2)
{
return _("%i.%i.%i.%i:%i", (unsigned __int8)a1, BYTE1(a1), BYTE2(a1), HIBYTE(a1), a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2)
{
return _("%i.%i.%i.%i:%i", (unsigned __int8)a1, BYTE1(a1), BYTE2(a1), HIBYTE(a1), a2);
}

original_code:
str
ipaddress(IPAddress addr, int port)
{
 return _("%i.%i.%i.%i:%i",
  (0xff & addr),
  (0xff00 & addr) >> 8,
  (0xff0000 & addr) >> 16,
  (0xff000000 & addr) >> 24,
  port);
}

canonical_original_code:
struct str_struct *ipaddress(unsigned int addr, int port)
{
 return _("%i.%i.%i.%i:%i",
  (0xff & addr),
  (0xff00 & addr) >> 8,
  (0xff0000 & addr) >> 16,
  (0xff000000 & addr) >> 24,
  port);
}


lookup

decompiled_code:
__int64 __fastcall lookup(__int64 a1)
{
struct hostent *v1; // rbp
char **v2; // rax
__int64 v3; // rdi
__int64 v4; // r13
unsigned int *v5; // rax
__int64 v6; // rbx
__int64 v7; // r12
const char *v8; // rax

v1 = gethostbyname(*(const char **)(a1 + 8));
v2 = v1->h_addr_list;
if ( *v2 )
{
v3 = 0LL;
do
++v3;
while ( v2[v3] );
}
else
{
v3 = 0LL;
}
v4 = reserve(v1->h_length * v3);
if ( v1->h_length != 4 )
log_msg(0LL, "Host name address to large!");
v5 = (unsigned int *)*v1->h_addr_list;
if ( v5 )
{
v6 = 0LL;
do
{
v7 = v6;
v8 = (const char *)ipaddress(*v5, 0x50u);
log_msg(3LL, "Found ip address: %s", v8);
*(_DWORD *)(v4 + 4 * v6) = *(_DWORD *)v1->h_addr_list[v6];
++v6;
v5 = (unsigned int *)v1->h_addr_list[v7 + 1];
}
while ( v5 );
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
struct hostent *v1; // rbp
char **v2; // rax
__int64 v3; // rdi
__int64 v4; // r13
unsigned int *v5; // rax
__int64 v6; // rbx
__int64 v7; // r12
const char *v8; // rax

v1 = gethostbyname(*(const char **)(a1 + 8));
v2 = v1->h_addr_list;
if ( *v2 )
{
v3 = 0LL;
do
++v3;
while ( v2[v3] );
}
else
{
v3 = 0LL;
}
v4 = reserve(v1->h_length * v3);
if ( v1->h_length != 4 )
log_msg(0LL, "Host name address to large!");
v5 = (unsigned int *)*v1->h_addr_list;
if ( v5 )
{
v6 = 0LL;
do
{
v7 = v6;
v8 = (const char *)func2(*v5, 0x50u);
log_msg(3LL, "Found ip address: %s", v8);
*(_DWORD *)(v4 + 4 * v6) = *(_DWORD *)v1->h_addr_list[v6];
++v6;
v5 = (unsigned int *)v1->h_addr_list[v7 + 1];
}
while ( v5 );
}
return v4;
}

original_code:
IPAddress*
lookup(str host)
{
 size_t i;
 struct hostent* hst = gethostbyname(host->data);
 for (i = 0; hst->h_addr_list[i];++i);
 IPAddress* retval = (IPAddress*)reserve(hst->h_length * i);
 if (hst->h_length != sizeof(IPAddress)) log_msg(0,"Host name address to large!");
 for (i = 0; hst->h_addr_list[i]; ++i) {
  log_msg(3,"Found ip address: %s",ipaddress(*(IPAddress*)hst->h_addr_list[i],80));
  memcpy(&retval[i],hst->h_addr_list[i],sizeof(IPAddress));
 }
 return retval;
}

canonical_original_code:
unsigned int *lookup(struct str_struct *host)
{
 unsigned long i;
 struct hostent *hst = gethostbyname(host->data);
 for (i = 0; hst->h_addr_list[i];++i);
 unsigned int *retval = (IPAddress*)reserve(hst->h_length * i);
 if (hst->h_length != sizeof(IPAddress)) log_msg(0,"Host name address to large!");
 for (i = 0; hst->h_addr_list[i]; ++i) {
  log_msg(3,"Found ip address: %s",ipaddress(*(unsigned int *)hst->h_addr_list[i],80));
  memcpy(&retval[i],hst->h_addr_list[i],sizeof(IPAddress));
 }
 return retval;
}

variable_types:
i
unsigned long

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
retval
unsigned int *

return_type: unsigned int *


decompiled_code:
__int64 init_strings()
{
OK = _(&unk_128);
ERROR = _("ERROR");
CMSS = _("CMSS");
CMGW = _("CMGW");
CMGR = _("CMGR");
CMGD = _("CMGD");
CMTI = _("CMTI");
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
OK = _(&unk_128);
ERROR = _("ERROR");
CMSS = _("CMSS");
CMGW = _("CMGW");
CMGR = _("CMGR");
CMGD = _("CMGD");
CMTI = _("CMTI");
return 0LL;
}

original_code:
int
init_strings()
{
 OK = _("OK");
 ERROR = _("ERROR");
 CMSS = _("CMSS");
 CMGW = _("CMGW");
 CMGR = _("CMGR");
 CMGD = _("CMGD");
 CMTI = _("CMTI");
 return 0;
}


decompiled_code:
__int64 dechunk_request()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rbp

result = is_chunked(*(_QWORD *)(client[4] + 16LL));
if ( (_DWORD)result )
{
v1 = from(*(_QWORD *)(client[4] + 48LL), 0LL, *(unsigned int *)(client[4] + 92LL));
v2 = dechunk(*(_QWORD *)(client[4] + 48LL));
v3 = client[4];
result = append(v1, v2);
*(_QWORD *)(v3 + 48) = result;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rbp

result = is_chunked(*(_QWORD *)(client[4] + 16LL));
if ( (_DWORD)result )
{
v1 = from(*(_QWORD *)(client[4] + 48LL), 0LL, *(unsigned int *)(client[4] + 92LL));
v2 = dechunk(*(_QWORD *)(client[4] + 48LL));
v3 = client[4];
result = append(v1, v2);
*(_QWORD *)(v3 + 48) = result;
}
return result;
}

original_code:
void
dechunk_request()
{
 if (!is_chunked(client.request->headers)) return;
 str hed = from(client.request->contents,0,client.request->body);
 str con = dechunk(client.request->contents);
 client.request->contents = append(hed,con);
}

canonical_original_code:
void dechunk_request()
{
 if (!is_chunked(client.request->headers)) return;
 struct str_struct *hed = from(client.request->contents,0,client.request->body);
 struct str_struct *con = dechunk(client.request->contents);
 client.request->contents = append(hed,con);
}


decompiled_code:
__int64 request_retry()
{
__int64 v0; // rax
__int64 v1; // rdx

log_msg(0LL, "No request contents on request %i\n", client[4]);
v0 = client[4];
v1 = (unsigned int)(*(_DWORD *)(v0 + 108) + 1);
*(_DWORD *)(v0 + 108) = v1;
log_msg(3LL, "retries %i", v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rax
__int64 v1; // rdx

log_msg(0LL, "No request contents on request %i\n", client[4]);
v0 = client[4];
v1 = (unsigned int)(*(_DWORD *)(v0 + 108) + 1);
*(_DWORD *)(v0 + 108) = v1;
log_msg(3LL, "retries %i", v1);
return 0xFFFFFFFFLL;
}

original_code:
int
request_retry()
{
 log_msg(0,"No request contents on request %i\n",client.request);
 ++client.request->retries;
 log_msg(3,"retries %i",client.request->retries);
 return -1;
}


decompiled_code:
__int64 process_request_contents()
{
__int64 v0; // rbx
const char *v1; // rax
_QWORD *v2; // rdx
__int64 result; // rax

v0 = client[4];
*(_QWORD *)(v0 + 16) = parse_headers(*(_QWORD *)(v0 + 48), v0 + 92);
v1 = (const char *)print_headers(0LL, *(_QWORD *)(client[4] + 16LL));
log_msg(3LL, "Headers:\n%s", v1);
v2 = (_QWORD *)client[4];
if ( v2[2] )
{
socket_peer(*v2);
_("peer");
request_headers(client[4]);
result = 0LL;
}
else
{
log_msg(0LL, "No request headers on request %i\n", v2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rbx
const char *v1; // rax
_QWORD *v2; // rdx
__int64 result; // rax

v0 = client[4];
*(_QWORD *)(v0 + 16) = parse_headers(*(_QWORD *)(v0 + 48), v0 + 92);
v1 = (const char *)print_headers(0LL, *(_QWORD *)(client[4] + 16LL));
log_msg(3LL, "Headers:\n%s", v1);
v2 = (_QWORD *)client[4];
if ( v2[2] )
{
socket_peer(*v2);
_("peer");
func1(client[4]);
result = 0LL;
}
else
{
log_msg(0LL, "No request headers on request %i\n", v2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
process_request_contents()
{
 client.request->headers = parse_headers(client.request->contents,&client.request->body);
 log_msg(3,"Headers:\n%s",print_headers(((void *)0),client.request->headers));
 if (!client.request->headers) {
  log_msg(0,"No request headers on request %i\n",client.request);
  return -1;
 }
 request_headers(client.request, _("peer"),socket_peer(client.request->socket));
 return 0;
}


original_code:
int
request_done()
{
 if (client.request->body)
  client.request->done = (len(client.request->contents) - client.request->body) >= inbound_content_length(client.request->contents,client.request->headers);
 return client.request->done;
}


decompiled_code:
__int64 process_request()
{
_QWORD *v0; // rbx
__int64 v1; // rax
__int64 result; // rax

v0 = (_QWORD *)client[4];
v0[6] = read_socket(*v0);
v1 = client[4];
if ( !*(_QWORD *)(v1 + 48) )
return request_retry();
if ( !*(_DWORD *)(v1 + 92) && (unsigned int)process_request_contents() )
return 0xFFFFFFFFLL;
result = request_done();
if ( (_DWORD)result )
{
dechunk_request();
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
_QWORD *v0; // rbx
__int64 v1; // rax
__int64 result; // rax

v0 = (_QWORD *)client[4];
v0[6] = read_socket(*v0);
v1 = client[4];
if ( !*(_QWORD *)(v1 + 48) )
return func5();
if ( !*(_DWORD *)(v1 + 92) && (unsigned int)func6() )
return 0xFFFFFFFFLL;
result = func7();
if ( (_DWORD)result )
{
func4();
result = 0LL;
}
return result;
}

original_code:
int
process_request()
{
 client.request->contents = read_socket(client.request->socket);
 if (!client.request->contents) return request_retry();
 if (!client.request->body && process_request_contents()) return -1;
 if (request_done()) dechunk_request();
 return 0;
}


decompiled_code:
__int64 parse_method()
{
__int64 v0; // r12
const unsigned __int16 **v1; // r13
unsigned int i; // ebx
const unsigned __int16 *v3; // rbp
unsigned int j; // ebp
const unsigned __int16 *v5; // r14
__int64 v6; // r13

v0 = *(_QWORD *)(client[4] + 48LL);
if ( v0 )
{
v1 = _ctype_b_loc();
for ( i = 0; ; ++i )
{
v3 = *v1;
if ( (v3[(char)at(v0, i)] & 0x2000) == 0 )
break;
}
for ( j = 1; ; ++j )
{
v5 = *v1;
if ( (v5[(char)at(v0, i + j)] & 0x2000) != 0 )
break;
}
v6 = client[4];
*(_QWORD *)(v6 + 64) = from(v0, i, j);
v0 = *(_QWORD *)(client[4] + 64LL);
}
return v0;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // r12
const unsigned __int16 **v1; // r13
unsigned int i; // ebx
const unsigned __int16 *v3; // rbp
unsigned int j; // ebp
const unsigned __int16 *v5; // r14
__int64 v6; // r13

v0 = *(_QWORD *)(client[4] + 48LL);
if ( v0 )
{
v1 = _ctype_b_loc();
for ( i = 0; ; ++i )
{
v3 = *v1;
if ( (v3[(char)at(v0, i)] & 0x2000) == 0 )
break;
}
for ( j = 1; ; ++j )
{
v5 = *v1;
if ( (v5[(char)at(v0, i + j)] & 0x2000) != 0 )
break;
}
v6 = client[4];
*(_QWORD *)(v6 + 64) = from(v0, i, j);
v0 = *(_QWORD *)(client[4] + 64LL);
}
return v0;
}

original_code:
str
parse_method()
{
 int i,l;
 str tmp = client.request->contents;
 if (!tmp) return ((void *)0);
 for (i=0;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short int) _ISspace);++i);
 for (l = 1; !((*__ctype_b_loc ())[(int) ((at(tmp,i+l)))] & (unsigned short int) _ISspace); ++l);
 client.request->method = from(tmp,i,l);
 return client.request->method;
}

canonical_original_code:
struct str_struct *parse_method()
{
 int i; int l;
 struct str_struct *tmp = client.request->contents;
 if (!tmp) return ((void *)0);
 for (i=0;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short) _ISspace);++i);
 for (l = 1; !((*__ctype_b_loc ())[(int) ((at(tmp,i+l)))] & (unsigned short) _ISspace); ++l);
 client.request->method = from(tmp,i,l);
 return client.request->method;
}

variable_types: l int


decompiled_code:
__int64 parse_path()
{
__int64 v0; // rbx
const unsigned __int16 **v1; // r13
unsigned int i; // ebp
const unsigned __int16 *v3; // r12
unsigned int j; // ebp
const unsigned __int16 *v5; // r12
const unsigned __int16 *v6; // r12
unsigned int k; // er12
const unsigned __int16 *v8; // r14
__int64 v9; // r13
unsigned int l; // er14
const unsigned __int16 *v12; // r15
__int64 v13; // rax
__int64 v14; // r13

v0 = *(_QWORD *)(client[4] + 48LL);
if ( v0 )
{
v1 = _ctype_b_loc();
for ( i = 0; ; ++i )
{
v3 = *v1;
if ( (v3[(char)at(v0, i)] & 0x2000) == 0 )
break;
}
for ( j = 0; (unsigned __int8)at(v0, j); ++j )
{
v5 = *v1;
if ( (v5[(char)at(v0, j)] & 0x2000) != 0 )
break;
}
while ( 1 )
{
v6 = *v1;
if ( (v6[(char)at(v0, j)] & 0x2000) == 0 )
break;
++j;
}
for ( k = j; (unsigned __int8)at(v0, k); ++k )
{
v8 = *v1;
if ( (v8[(char)at(v0, k)] & 0x2000) != 0 || (unsigned __int8)at(v0, k) == 63 )
break;
}
*(_QWORD *)(client[4] + 24LL) = 0LL;
if ( (unsigned __int8)at(v0, k) == 63 )
{
for ( l = 1; ; ++l )
{
v12 = *v1;
if ( (v12[(char)at(v0, k + l)] & 0x2000) != 0 )
break;
}
v13 = from(v0, k, l);
v14 = client[4];
*(_QWORD *)(v14 + 24) = parse_uri_encoded(0LL, v13, 1LL);
}
v9 = client[4];
v0 = from(v0, j, k - j);
*(_QWORD *)(v9 + 80) = v0;
}
return v0;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rbx
const unsigned __int16 **v1; // r13
unsigned int i; // ebp
const unsigned __int16 *v3; // r12
unsigned int j; // ebp
const unsigned __int16 *v5; // r12
const unsigned __int16 *v6; // r12
unsigned int k; // er12
const unsigned __int16 *v8; // r14
__int64 v9; // r13
unsigned int l; // er14
const unsigned __int16 *v12; // r15
__int64 v13; // rax
__int64 v14; // r13

v0 = *(_QWORD *)(client[4] + 48LL);
if ( v0 )
{
v1 = _ctype_b_loc();
for ( i = 0; ; ++i )
{
v3 = *v1;
if ( (v3[(char)at(v0, i)] & 0x2000) == 0 )
break;
}
for ( j = 0; (unsigned __int8)at(v0, j); ++j )
{
v5 = *v1;
if ( (v5[(char)at(v0, j)] & 0x2000) != 0 )
break;
}
while ( 1 )
{
v6 = *v1;
if ( (v6[(char)at(v0, j)] & 0x2000) == 0 )
break;
++j;
}
for ( k = j; (unsigned __int8)at(v0, k); ++k )
{
v8 = *v1;
if ( (v8[(char)at(v0, k)] & 0x2000) != 0 || (unsigned __int8)at(v0, k) == 63 )
break;
}
*(_QWORD *)(client[4] + 24LL) = 0LL;
if ( (unsigned __int8)at(v0, k) == 63 )
{
for ( l = 1; ; ++l )
{
v12 = *v1;
if ( (v12[(char)at(v0, k + l)] & 0x2000) != 0 )
break;
}
v13 = from(v0, k, l);
v14 = client[4];
*(_QWORD *)(v14 + 24) = parse_uri_encoded(0LL, v13, 1LL);
}
v9 = client[4];
v0 = from(v0, j, k - j);
*(_QWORD *)(v9 + 80) = v0;
}
return v0;
}

original_code:
str
parse_path()
{
 int i,l,end;
 str qs;
 str tmp = client.request->contents;
 if (! tmp) return ((void *)0);
 for (i = 0;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short int) _ISspace);++i);
 for (i = 0; at(tmp,i) && !((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short int) _ISspace); ++i);
 for (;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short int) _ISspace);++i);
 for (end = i; at(tmp,end) && !((*__ctype_b_loc ())[(int) ((at(tmp,end)))] & (unsigned short int) _ISspace) && at(tmp,end) != '?'; ++end);
 client.request->query_vars = ((void *)0);
 if (at(tmp,end) == '?') {
  for (l = 1; !((*__ctype_b_loc ())[(int) ((at(tmp,end + l)))] & (unsigned short int) _ISspace); ++l);
  qs = from(tmp,end,l);
  client.request->query_vars = parse_uri_encoded(((void *)0),qs,1);
 }
 return client.request->path = from(tmp,i,end - i);
}

canonical_original_code:
struct str_struct *parse_path()
{
 int i; int l; int end;
 struct str_struct *qs;
 struct str_struct *tmp = client.request->contents;
 if (! tmp) return ((void *)0);
 for (i = 0;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short) _ISspace);++i);
 for (i = 0; at(tmp,i) && !((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short) _ISspace); ++i);
 for (;((*__ctype_b_loc ())[(int) ((at(tmp,i)))] & (unsigned short) _ISspace);++i);
 for (end = i; at(tmp,end) && !((*__ctype_b_loc ())[(int) ((at(tmp,end)))] & (unsigned short) _ISspace) && at(tmp,end) != '?'; ++end);
 client.request->query_vars = ((void *)0);
 if (at(tmp,end) == '?') {
  for (l = 1; !((*__ctype_b_loc ())[(int) ((at(tmp,end + l)))] & (unsigned short) _ISspace); ++l);
  qs = from(tmp,end,l);
  client.request->query_vars = parse_uri_encoded(((void *)0),qs,1);
 }
 return client.request->path = from(tmp,i,end - i);
}

variable_types: end int


decompiled_code:
__int64 __fastcall script_handler(__int64 a1)
{
__int64 *v2; // rax
__int64 v3; // rdi
unsigned int v4; // ebx
__int64 *v5; // rax

send_status(**((_QWORD **)&client + 5), 200LL);
send_headers(**((_QWORD **)&client + 5), *(_QWORD *)(*((_QWORD *)&client + 5) + 16LL));
v2 = (__int64 *)*((_QWORD *)&client + 5);
v3 = *v2;
v4 = *(_DWORD *)(*v2 + 40);
if ( !v4 )
{
do
{
if ( (int)v4 >= *(_QWORD *)(a1 + 56) )
break;
v4 += send_raw_contents(v3, a1, v4, 1LL);
v5 = (__int64 *)*((_QWORD *)&client + 5);
v3 = *v5;
}
while ( !*(_DWORD *)(*v5 + 40) );
}
return 200LL;
}

original_code:
int
script_handler(File fc)
{
 int total = 0;
 send_status(client.response->socket,200);
 send_headers(client.response->socket,client.response->headers);
 while (!client.response->socket->closed && total < fc->st.st_size)
  total += send_raw_contents(client.response->socket,fc,total,1);
 return 200;
}

canonical_original_code:
int script_handler(struct file_cache_struct *fc)
{
 int total = 0;
 send_status(client.response->socket,200);
 send_headers(client.response->socket,client.response->headers);
 while (!client.response->socket->closed && total < fc->st.st_size)
  total += send_raw_contents(client.response->socket,fc,total,1);
 return 200;
}

variable_types: struct file_cache_struct { struct file_cache_struct *next; 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]; } st; struct parsed_struct { enum { TEXT=1, SCRIPT=2, EMIT=3 } kind; int offset; int length; } *parsed; struct mime_struct { struct { int len; char *data; } ending; struct { int len; char *data; } type; int (*handler)(struct file_cache_struct *); } *mime; char *data; int fd; struct str_struct { int length; char *data; char contents[]; } name; } *

variable_types: total int


timeout

decompiled_code:
void __fastcall timeout(int a1, int a2)
{
Timeout.it_interval.tv_usec = 0LL;
Timeout.it_interval.tv_sec = 0LL;
Timeout.it_value.tv_usec = a2;
Timeout.it_value.tv_sec = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
Timeout.it_interval.tv_usec = 0LL;
Timeout.it_interval.tv_sec = 0LL;
Timeout.it_value.tv_usec = a2;
Timeout.it_value.tv_sec = a1;
}

original_code:
void
timeout(int seconds, int useconds) {
 Timeout.it_interval.tv_usec = 0;
 Timeout.it_interval.tv_sec = 0;
 Timeout.it_value.tv_usec = useconds;
 Timeout.it_value.tv_sec = seconds;
}

variable_types: seconds int

variable_types: useconds int


timer

decompiled_code:
int timer()
{
int result; // eax

result = setitimer(ITIMER_REAL, &Timeout, &Timeout);
if ( result )
result = log_msg(0LL, "Failed to set timeout");
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

result = setitimer(ITIMER_REAL, &Timeout, &Timeout);
if ( result )
result = log_msg(0LL, "Failed to set timeout");
return result;
}

original_code:
void
timer()
{
 if (setitimer(0, &Timeout, &Timeout))
  log_msg(0,"Failed to set timeout");
}


timediff

decompiled_code:
__int64 __fastcall timediff(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(a1[2] + ((*a1 - *a2) << 20) - a2[2]);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(a1[2] + ((*a1 - *a2) << 20) - a2[2]);
}

original_code:
int
timediff(struct timeval* tva,struct timeval* tvb)
{
 size_t sec = tva->tv_sec - tvb->tv_sec;
 size_t usec = tva->tv_usec - tvb->tv_usec;
 return (sec << 20) + usec;
}

canonical_original_code:
int timediff(struct timeval *tva, struct timeval *tvb)
{
 unsigned long sec = tva->tv_sec - tvb->tv_sec;
 unsigned long usec = tva->tv_usec - tvb->tv_usec;
 return (sec << 20) + usec;
}

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

variable_types:
tvb
struct timeval *

variable_types:
sec
unsigned long

variable_types:
usec
unsigned long


decompiled_code:
__int64 new_usage()
{
return reserve(304LL);
}

canonical_decompiled_code:
__int64 func1()
{
return reserve(304LL);
}

original_code:
Usage
new_usage()
{
 return (Usage)reserve(sizeof(struct usage_struct));
}

canonical_original_code:
struct usage_struct *new_usage()
{
 return (struct usage_struct *)reserve(sizeof(struct usage_struct));
}

return_type: struct usage_struct { struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; } start; struct rusage end; int requests; int hits; int time; } *

user_defined_types: struct usage_struct { struct rusage start; struct rusage end; int requests; int hits; int time; }

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


decompiled_code:
int __fastcall start_usage(struct rusage *usage)
{
int result; // eax

result = getrusage(RUSAGE_SELF, usage);
if ( result )
result = log_msg(0LL, "Failed to calculate usage");
return result;
}

canonical_decompiled_code:
int __fastcall func2(struct rusage *usage)
{
int result; // eax

result = getrusage(RUSAGE_SELF, usage);
if ( result )
result = log_msg(0LL, "Failed to calculate usage");
return result;
}

original_code:
void
start_usage(Usage u)
{
 if (getrusage(RUSAGE_SELF,&u->start))
  log_msg(0,"Failed to calculate usage");
}

canonical_original_code:
void start_usage(struct usage_struct *u)
{
 if (getrusage(RUSAGE_SELF,&u->start))
  log_msg(0,"Failed to calculate usage");
}

variable_types: struct usage_struct { struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; } start; struct rusage end; int requests; int hits; int time; } *


decompiled_code:
__int64 __fastcall stop_usage(__int64 a1)
{
int v1; // ebp
int v2; // ebp
int v3; // eax
__int64 result; // rax

if ( getrusage(RUSAGE_SELF, (struct rusage *)(a1 + 144)) )
log_msg(0LL, "Failed to calculate usage");
v1 = timediff((_DWORD *)(a1 + 144), (_DWORD *)a1);
v2 = timediff((_DWORD *)(a1 + 160), (_DWORD *)(a1 + 16)) + v1;
++*(_DWORD *)(a1 + 288);
v3 = *(_DWORD *)(a1 + 292);
if ( v3 <= 999 )
*(_DWORD *)(a1 + 292) = v3 + 1;
result = (unsigned int)((v2 + *(_DWORD *)(a1 + 296) * (*(_DWORD *)(a1 + 292) - 1)) / *(_DWORD *)(a1 + 292));
*(_DWORD *)(a1 + 296) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
int v1; // ebp
int v2; // ebp
int v3; // eax
__int64 result; // rax

if ( getrusage(RUSAGE_SELF, (struct rusage *)(a1 + 144)) )
log_msg(0LL, "Failed to calculate usage");
v1 = func0((_DWORD *)(a1 + 144), (_DWORD *)a1);
v2 = func0((_DWORD *)(a1 + 160), (_DWORD *)(a1 + 16)) + v1;
++*(_DWORD *)(a1 + 288);
v3 = *(_DWORD *)(a1 + 292);
if ( v3 <= 999 )
*(_DWORD *)(a1 + 292) = v3 + 1;
result = (unsigned int)((v2 + *(_DWORD *)(a1 + 296) * (*(_DWORD *)(a1 + 292) - 1)) / *(_DWORD *)(a1 + 292));
*(_DWORD *)(a1 + 296) = result;
return result;
}

original_code:
void
stop_usage(Usage u)
{
 if (getrusage(RUSAGE_SELF,&u->end))
  log_msg(0,"Failed to calculate usage");
 int delta = timediff(&u->end.ru_utime,&u->start.ru_utime) + timediff(&u->end.ru_stime,&u->start.ru_stime);
 ++u->requests;
 if (u->hits < 1000)
  ++u->hits;
 u->time = ((u->hits-1) * u->time + delta) / u->hits;
}

canonical_original_code:
void stop_usage(struct usage_struct *u)
{
 if (getrusage(RUSAGE_SELF,&u->end))
  log_msg(0,"Failed to calculate usage");
 int delta = timediff(&u->end.ru_utime,&u->start.ru_utime) + timediff(&u->end.ru_stime,&u->start.ru_stime);
 ++u->requests;
 if (u->hits < 1000)
  ++u->hits;
 u->time = ((u->hits-1) * u->time + delta) / u->hits;
}

variable_types: delta int

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


decompiled_code:
__int64 __fastcall dump_usage(_DWORD *a1)
{
timediff(a1 + 36, a1);
timediff(a1 + 40, a1 + 4);
log_msg(3LL, &unk_19C);
timediff(a1 + 36, a1);
log_msg(3LL, &unk_1AE);
timediff(a1 + 40, a1 + 4);
return log_msg(3LL, &unk_1C0);
}

canonical_decompiled_code:
__int64 __fastcall func4(_DWORD *a1)
{
func0(a1 + 36, a1);
func0(a1 + 40, a1 + 4);
log_msg(3LL, &unk_19C);
func0(a1 + 36, a1);
log_msg(3LL, &unk_1AE);
func0(a1 + 40, a1 + 4);
return log_msg(3LL, &unk_1C0);
}

original_code:
void
dump_usage(Usage u)
{
 log_msg(3,"Total Time: %iµs", timediff(&u->end.ru_utime,&u->start.ru_utime) + timediff(&u->end.ru_stime,&u->start.ru_stime));
 log_msg(3,"\tUser Time: %iµs", timediff(&u->end.ru_utime,&u->start.ru_utime));
 log_msg(3,"\tSystem Time: %iµs", timediff(&u->end.ru_stime,&u->start.ru_stime));
}

canonical_original_code:
void dump_usage(struct usage_struct *u)
{
 log_msg(3,"Total Time: %iµs", timediff(&u->end.ru_utime,&u->start.ru_utime) + timediff(&u->end.ru_stime,&u->start.ru_stime));
 log_msg(3,"\tUser Time: %iµs", timediff(&u->end.ru_utime,&u->start.ru_utime));
 log_msg(3,"\tSystem Time: %iµs", timediff(&u->end.ru_stime,&u->start.ru_stime));
}


decompiled_code:
__int64 __fastcall find_boundary(__int64 a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // ebx
__int64 v4; // r13
__int64 v5; // rax
const char *v7; // rbx

v1 = len();
if ( v1 <= 19 )
return 0LL;
v2 = v1;
v3 = 19;
while ( 1 )
{
v4 = from(a1, v3, v2 - v3);
v5 = _("boundary=");
if ( (unsigned int)ncmp(v5, v4, 9LL) )
break;
if ( v2 == ++v3 )
return 0LL;
}
v7 = (const char *)from(a1, v3 + 9, v2 - (v3 + 9));
log_msg(3LL, "Boundary is %s", v7);
return _("--%s", v7);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // ebx
__int64 v4; // r13
__int64 v5; // rax
const char *v7; // rbx

v1 = len();
if ( v1 <= 19 )
return 0LL;
v2 = v1;
v3 = 19;
while ( 1 )
{
v4 = from(a1, v3, v2 - v3);
v5 = _("boundary=");
if ( (unsigned int)ncmp(v5, v4, 9LL) )
break;
if ( v2 == ++v3 )
return 0LL;
}
v7 = (const char *)from(a1, v3 + 9, v2 - (v3 + 9));
log_msg(3LL, "Boundary is %s", v7);
return _("--%s", v7);
}

original_code:
str
find_boundary(str enc)
{
 int i;
 str t;
 int el = len(enc);
 for (i = 19; i < el; ++i) {
  t = from(enc, i, el - i);
  if (ncmp(_("boundary="),t,9)) {
   t = from(enc, i + 9, el - (i+9));
   log_msg(3,"Boundary is %s",t);
   return _("--%s",t);
  }
 }
 return ((void *)0);
}

canonical_original_code:
struct str_struct *find_boundary(struct str_struct *enc)
{
 int i;
 struct str_struct *t;
 int el = len(enc);
 for (i = 19; i < el; ++i) {
  t = from(enc, i, el - i);
  if (ncmp(_("boundary="),t,9)) {
   t = from(enc, i + 9, el - (i+9));
   log_msg(3,"Boundary is %s",t);
   return _("--%s",t);
  }
 }
 return ((void *)0);
}

variable_types:
enc
struct str_struct *

variable_types: i int

variable_types: struct str_struct { int length; char *data; char contents[]; } *

variable_types: el int


decompiled_code:
__int64 __fastcall parse_name(__int64 a1, unsigned int a2)
{
int v2; // ebp
__int64 v3; // rax
int v4; // eax
unsigned int v5; // er12
__int64 v6; // rdx
int v7; // eax

v2 = len();
v3 = _("name=\"");
v4 = search(a1, a2, v3);
v5 = v4 + 6;
v6 = 0LL;
if ( v2 > v4 + 6 )
{
v7 = find(a1, (unsigned int)(v4 + 7), "\"", 1LL) - v5;
if ( v7 > 4000 )
{
log_msg(0LL, "Filename exceeds legit size", (unsigned int)v7);
v6 = 0LL;
}
else
{
v6 = from(a1, v5, (unsigned int)v7);
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
int v2; // ebp
__int64 v3; // rax
int v4; // eax
unsigned int v5; // er12
__int64 v6; // rdx
int v7; // eax

v2 = len();
v3 = _("name=\"");
v4 = search(a1, a2, v3);
v5 = v4 + 6;
v6 = 0LL;
if ( v2 > v4 + 6 )
{
v7 = find(a1, (unsigned int)(v4 + 7), "\"", 1LL) - v5;
if ( v7 > 4000 )
{
log_msg(0LL, "Filename exceeds legit size", (unsigned int)v7);
v6 = 0LL;
}
else
{
v6 = from(a1, v5, (unsigned int)v7);
}
}
return v6;
}

original_code:
str
parse_name(str src, int pos)
{
 str retval = ((void *)0);
 int bl = len(src);
 int off = 6 + search(src,pos,_("name=\""));
 if (off >= bl) return ((void *)0);
 int end = find(src,off+1,"\"",1);
 if (end - off > 4000) {
  log_msg(0,"Filename exceeds legit size");
  return ((void *)0);
 }
 retval = from(src,off,end-off);
 return retval;
}

canonical_original_code:
struct str_struct *parse_name(struct str_struct *src, int pos)
{
 struct str_struct *retval = ((void *)0);
 int bl = len(src);
 int off = 6 + search(src,pos,_("name=\""));
 if (off >= bl) return ((void *)0);
 int end = find(src,off+1,"\"",1);
 if (end - off > 4000) {
  log_msg(0,"Filename exceeds legit size");
  return ((void *)0);
 }
 retval = from(src,off,end-off);
 return retval;
}

variable_types:
src
struct str_struct *

variable_types: pos int

variable_types: bl int

variable_types: off int


decompiled_code:
_BOOL8 __fastcall found_file(__int64 a1, unsigned int a2, int a3)
{
int v4; // ebx
__int64 v5; // rax
int v6; // eax

v4 = len();
v5 = _("filename=\"");
v6 = search(a1, a2, v5) + 6;
if ( v4 > a3 )
v4 = a3;
return v6 < v4;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, unsigned int a2, int a3)
{
int v4; // ebx
__int64 v5; // rax
int v6; // eax

v4 = len();
v5 = _("filename=\"");
v6 = search(a1, a2, v5) + 6;
if ( v4 > a3 )
v4 = a3;
return v6 < v4;
}

original_code:
int
found_file(str src, int pos, int end)
{
 int l = len(src);
 int off = 6 + search(src,pos,_("filename=\""));
 return off < l && off < end;
}

canonical_original_code:
int found_file(struct str_struct *src, int pos, int end)
{
 int l = len(src);
 int off = 6 + search(src,pos,_("filename=\""));
 return off < l && off < end;
}

variable_types: end int

variable_types: l int


decompiled_code:
__int64 __fastcall skip_content_headers(__int64 a1, unsigned int a2)
{
__int64 v2; // rax

v2 = _("\r\n\r\n");
return (unsigned int)search(a1, a2, v2) + 4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2)
{
__int64 v2; // rax

v2 = _("\r\n\r\n");
return (unsigned int)search(a1, a2, v2) + 4;
}

original_code:
int
skip_content_headers(str src, int pos)
{
 return 4 + search(src,pos,_("\r\n\r\n"));
}

canonical_original_code:
int skip_content_headers(struct str_struct *src, int pos)
{
 return 4 + search(src,pos,_("\r\n\r\n"));
}


decompiled_code:
__int64 __fastcall save_contents(__int64 a1, int a2, int a3)
{
__int64 v4; // r13
int v5; // eax
int v6; // er12

v4 = temp_file();
v5 = open(*(const char **)(v4 + 8), 65, 420LL);
if ( v5 < 0 )
{
log_msg(0LL, "Failed to open file for writing %s", (const char *)v4);
perror("open");
}
else
{
v6 = v5;
if ( write(v5, (const void *)(*(_QWORD *)(a1 + 8) + a2), a3 - a2) < 0 )
log_msg(0LL, "Failed to write to %s", (const char *)v4);
close(v6);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, int a3)
{
__int64 v4; // r13
int v5; // eax
int v6; // er12

v4 = temp_file();
v5 = open(*(const char **)(v4 + 8), 65, 420LL);
if ( v5 < 0 )
{
log_msg(0LL, "Failed to open file for writing %s", (const char *)v4);
perror("open");
}
else
{
v6 = v5;
if ( write(v5, (const void *)(*(_QWORD *)(a1 + 8) + a2), a3 - a2) < 0 )
log_msg(0LL, "Failed to write to %s", (const char *)v4);
close(v6);
}
return v4;
}

original_code:
str
save_contents(str src, int pos, int end)
{
 str filename = temp_file();
 char* fname = filename->data;
 int fd = open(fname,01|0100,0644);
 if (fd < 0 ) {
  log_msg(0,"Failed to open file for writing %s",filename);
  perror("open");
  return filename;
 }
 if (write(fd,&src->data[pos],end - pos) < 0)
  log_msg(0,"Failed to write to %s", filename);
 close(fd);
 return filename;
}

canonical_original_code:
struct str_struct *save_contents(struct str_struct *src, int pos, int end)
{
 struct str_struct *filename = temp_file();
 char *fname = filename->data;
 int fd = open(fname,01|0100,0644);
 if (fd < 0 ) {
  log_msg(0,"Failed to open file for writing %s",filename);
  perror("open");
  return filename;
 }
 if (write(fd,&src->data[pos],end - pos) < 0)
  log_msg(0,"Failed to write to %s", filename);
 close(fd);
 return filename;
}

variable_types:
fname
char *

variable_types: fd int


decompiled_code:
__int64 __fastcall parse_multipart_body(__int64 a1, __int64 a2)
{
signed int v2; // er15
__int64 v3; // rax
__int64 v4; // r14
signed int v5; // ebx
__int64 v7; // rdx
int v8; // ebp
__int64 v9; // r12
unsigned int v10; // er13
int v12; // [rsp+Ch] [rbp-3Ch]

v2 = len(*(_QWORD *)(client[4] + 48LL));
v3 = find_boundary(a2);
if ( v3 )
{
v4 = v3;
v12 = len(v3);
v5 = search(*(_QWORD *)(client[4] + 48LL), *(unsigned int *)(client[4] + 92LL), v4);
if ( v2 > v5 )
{
do
{
v8 = search(*(_QWORD *)(client[4] + 48LL), (unsigned int)(v5 + 1), v4);
v9 = parse_name(*(_QWORD *)(client[4] + 48LL), v5);
if ( v9 )
{
v10 = skip_content_headers(*(_QWORD *)(client[4] + 48LL), v5);
if ( found_file(*(_QWORD *)(client[4] + 48LL), v5, v10) )
v7 = save_contents(*(_QWORD *)(client[4] + 48LL), v10, v8 - 2);
else
v7 = from(*(_QWORD *)(client[4] + 48LL), v10, v8 - 2 - v10);
a1 = append_header(a1, v9, v7);
}
else
{
skip_content_headers(*(_QWORD *)(client[4] + 48LL), v5);
}
v5 = v12 + v8;
}
while ( v2 > v12 + v8 );
}
}
else
{
log_msg(0LL, "multipart/form-data without boundary");
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
signed int v2; // er15
__int64 v3; // rax
__int64 v4; // r14
signed int v5; // ebx
__int64 v7; // rdx
int v8; // ebp
__int64 v9; // r12
unsigned int v10; // er13
int v12; // [rsp+Ch] [rbp-3Ch]

v2 = len(*(_QWORD *)(client[4] + 48LL));
v3 = func0(a2);
if ( v3 )
{
v4 = v3;
v12 = len(v3);
v5 = search(*(_QWORD *)(client[4] + 48LL), *(unsigned int *)(client[4] + 92LL), v4);
if ( v2 > v5 )
{
do
{
v8 = search(*(_QWORD *)(client[4] + 48LL), (unsigned int)(v5 + 1), v4);
v9 = func1(*(_QWORD *)(client[4] + 48LL), v5);
if ( v9 )
{
v10 = func3(*(_QWORD *)(client[4] + 48LL), v5);
if ( func2(*(_QWORD *)(client[4] + 48LL), v5, v10) )
v7 = func4(*(_QWORD *)(client[4] + 48LL), v10, v8 - 2);
else
v7 = from(*(_QWORD *)(client[4] + 48LL), v10, v8 - 2 - v10);
a1 = append_header(a1, v9, v7);
}
else
{
func3(*(_QWORD *)(client[4] + 48LL), v5);
}
v5 = v12 + v8;
}
while ( v2 > v12 + v8 );
}
}
else
{
log_msg(0LL, "multipart/form-data without boundary");
}
return a1;
}

original_code:
Headers
parse_multipart_body(Headers headers, str enctype)
{
 str dstname;
 int i, e, n, l = len(client.request->contents);
 str boundary = find_boundary(enctype);
 if (!boundary) {
  log_msg(0,"multipart/form-data without boundary");
  return headers;
 }
 int bl = len(boundary);
 for (i = search(client.request->contents,client.request->body,boundary);
  i < l;
  i = n + bl) {
  n = search(client.request->contents,i+1,boundary);
  str srcname = parse_name(client.request->contents,i);
  if (! srcname) {
   i = skip_content_headers(client.request->contents,i);
   continue;
  }
  e = skip_content_headers(client.request->contents,i);
  dstname = (found_file(client.request->contents,i,e)) ?
   save_contents(client.request->contents,e,n-2):
   from(client.request->contents,e,n-2-e);
  headers = append_header(headers,srcname,dstname);
 }
 return headers;
}

canonical_original_code:
struct headers_struct *parse_multipart_body(struct headers_struct *headers, struct str_struct *enctype)
{
 struct str_struct *dstname;
 int i; int e; int n; int l = len(client.request->contents);
 struct str_struct *boundary = find_boundary(enctype);
 if (!boundary) {
  log_msg(0,"multipart/form-data without boundary");
  return headers;
 }
 int bl = len(boundary);
 for (i = search(client.request->contents,client.request->body,boundary);
  i < l;
  i = n + bl) {
  n = search(client.request->contents,i+1,boundary);
  struct str_struct *srcname = parse_name(client.request->contents,i);
  if (! srcname) {
   i = skip_content_headers(client.request->contents,i);
   continue;
  }
  e = skip_content_headers(client.request->contents,i);
  dstname = (found_file(client.request->contents,i,e)) ?
   save_contents(client.request->contents,e,n-2):
   from(client.request->contents,e,n-2-e);
  headers = append_header(headers,srcname,dstname);
 }
 return headers;
}

variable_types:
headers
struct headers_struct *

variable_types:
enctype
struct str_struct *

variable_types: e int

variable_types: n int


decompiled_code:
void restart_signal_handler()
{
server[2065] = 1;
server[2066] = 1;
}

original_code:
void
restart_signal_handler()
{
 server.done = 1;
 server.restart = 1;
}


decompiled_code:
void alrm_signal_handler()
{
client[1] = 1LL;
}

canonical_decompiled_code:
void func1()
{
client[1] = 1LL;
}

original_code:
void
alrm_signal_handler()
{
 client.alarm = 1;
}


decompiled_code:
void __noreturn gen_signal_handler()
{
server[2065] = 1;
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
server[2065] = 1;
exit(0);
}

original_code:
void
gen_signal_handler()
{
 server.done = 1;
 exit(0);
}


decompiled_code:
__sighandler_t general_signal_handlers()
{
signal(1, (__sighandler_t)restart_signal_handler);
signal(2, (__sighandler_t)gen_signal_handler);
signal(15, (__sighandler_t)gen_signal_handler);
return signal(14, (__sighandler_t)alrm_signal_handler);
}

canonical_decompiled_code:
__sighandler_t func3()
{
signal(1, (__sighandler_t)restart_signal_handler);
signal(2, (__sighandler_t)gen_signal_handler);
signal(15, (__sighandler_t)gen_signal_handler);
return signal(14, (__sighandler_t)alrm_signal_handler);
}

original_code:
void
general_signal_handlers()
{
 signal(1,restart_signal_handler);
 signal(2,gen_signal_handler);
 signal(15,gen_signal_handler);
 signal(14,alrm_signal_handler);
}


decompiled_code:
__int64 dechunk_response()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rbp

result = is_chunked(*(_QWORD *)(client[5] + 16LL));
if ( (_DWORD)result )
{
v1 = ref(*(_QWORD *)(*(_QWORD *)(client[5] + 24LL) + 8LL), *(unsigned int *)(client[5] + 52LL));
v2 = dechunk(*(_QWORD *)(client[5] + 24LL));
v3 = client[5];
result = append(v1, v2);
*(_QWORD *)(v3 + 24) = result;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rbp

result = is_chunked(*(_QWORD *)(client[5] + 16LL));
if ( (_DWORD)result )
{
v1 = ref(*(_QWORD *)(*(_QWORD *)(client[5] + 24LL) + 8LL), *(unsigned int *)(client[5] + 52LL));
v2 = dechunk(*(_QWORD *)(client[5] + 24LL));
v3 = client[5];
result = append(v1, v2);
*(_QWORD *)(v3 + 24) = result;
}
return result;
}

original_code:
void
dechunk_response()
{
 if (is_chunked(client.response->headers)) {
  str hed = ref(client.response->contents->data,client.response->body);
  str con = dechunk(client.response->contents);
  client.response->contents = append(hed,con);
 }
}

canonical_original_code:
void dechunk_response()
{
 if (is_chunked(client.response->headers)) {
  struct str_struct *hed = ref(client.response->contents->data,client.response->body);
  struct str_struct *con = dechunk(client.response->contents);
  client.response->contents = append(hed,con);
 }
}


decompiled_code:
__int64 process_response()
{
_QWORD *v0; // rbx
__int64 v1; // rbx
__int64 v2; // rdi
int v3; // eax
__int64 v4; // rdx
int v5; // ebx
int v6; // eax
__int64 result; // rax

v0 = (_QWORD *)client[5];
v0[3] = read_socket(*v0);
v1 = client[5];
v2 = *(_QWORD *)(v1 + 24);
if ( !v2 )
{
log_msg(0LL, "No response contents on Response <%p>\n", (const void *)client[5]);
return 0xFFFFFFFFLL;
}
if ( !*(_DWORD *)(v1 + 52) )
{
*(_QWORD *)(v1 + 16) = parse_headers(v2, v1 + 52);
v1 = client[5];
if ( !*(_QWORD *)(v1 + 16) )
{
log_msg(0LL, "No response headers on Response <%p>\n", (const void *)client[5]);
return 0xFFFFFFFFLL;
}
result = *(unsigned int *)(v1 + 52);
if ( !(_DWORD)result )
{
*(_DWORD *)(v1 + 56) = 0;
return result;
}
}
v3 = len(*(_QWORD *)(v1 + 24));
v4 = client[5];
v5 = v3 - *(_DWORD *)(v4 + 52);
v6 = inbound_content_length(*(_QWORD *)(v4 + 24), *(_QWORD *)(v4 + 16));
*(_DWORD *)(client[5] + 56LL) = v5 >= v6;
if ( v5 >= v6 )
dechunk_response();
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
_QWORD *v0; // rbx
__int64 v1; // rbx
__int64 v2; // rdi
int v3; // eax
__int64 v4; // rdx
int v5; // ebx
int v6; // eax
__int64 result; // rax

v0 = (_QWORD *)client[5];
v0[3] = read_socket(*v0);
v1 = client[5];
v2 = *(_QWORD *)(v1 + 24);
if ( !v2 )
{
log_msg(0LL, "No response contents on Response <%p>\n", (const void *)client[5]);
return 0xFFFFFFFFLL;
}
if ( !*(_DWORD *)(v1 + 52) )
{
*(_QWORD *)(v1 + 16) = parse_headers(v2, v1 + 52);
v1 = client[5];
if ( !*(_QWORD *)(v1 + 16) )
{
log_msg(0LL, "No response headers on Response <%p>\n", (const void *)client[5]);
return 0xFFFFFFFFLL;
}
result = *(unsigned int *)(v1 + 52);
if ( !(_DWORD)result )
{
*(_DWORD *)(v1 + 56) = 0;
return result;
}
}
v3 = len(*(_QWORD *)(v1 + 24));
v4 = client[5];
v5 = v3 - *(_DWORD *)(v4 + 52);
v6 = inbound_content_length(*(_QWORD *)(v4 + 24), *(_QWORD *)(v4 + 16));
*(_DWORD *)(client[5] + 56LL) = v5 >= v6;
if ( v5 >= v6 )
func1();
return 0LL;
}

original_code:
int
process_response()
{
 client.response->contents = read_socket(client.response->socket);
 if (! client.response->contents) {
  log_msg(0,"No response contents on Response <%p>\n",client.response);
  return -1;
 }
 if (! client.response->body) {
  client.response->headers = parse_headers(client.response->contents,&client.response->body);
  if (! client.response->headers) {
   log_msg(0,"No response headers on Response <%p>\n",client.response);
   return -1;
  }
 }
 client.response->done = client.response->body &&
  (len(client.response->contents) - client.response->body) >= inbound_content_length(client.response->contents,client.response->headers);
 if (client.response->done) dechunk_response();
 return 0;
}


decompiled_code:
_BOOL8 begin_response()
{
__int64 v0; // rdx
_BOOL8 result; // rax
__int64 v2; // rbx

if ( !*(_QWORD *)(client[5] + 16LL) )
{
log_msg(0LL, "Missing headers, adding new ones");
v2 = client[5];
*(_QWORD *)(v2 + 16) = new_headers();
}
connection(*(_QWORD *)(client[5] + 16LL), "close");
transfer_encoding(*(_QWORD *)(client[5] + 16LL), "chunked");
server_name(*(_QWORD *)(client[5] + 16LL), "Jawas/1.0");
v0 = client[5];
result = 1LL;
if ( !*(_QWORD *)(v0 + 24) )
result = *(_QWORD *)(v0 + 32) != 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 func3()
{
__int64 v0; // rdx
_BOOL8 result; // rax
__int64 v2; // rbx

if ( !*(_QWORD *)(client[5] + 16LL) )
{
log_msg(0LL, "Missing headers, adding new ones");
v2 = client[5];
*(_QWORD *)(v2 + 16) = new_headers();
}
connection(*(_QWORD *)(client[5] + 16LL), "close");
transfer_encoding(*(_QWORD *)(client[5] + 16LL), "chunked");
server_name(*(_QWORD *)(client[5] + 16LL), "Jawas/1.0");
v0 = client[5];
result = 1LL;
if ( !*(_QWORD *)(v0 + 24) )
result = *(_QWORD *)(v0 + 32) != 0LL;
return result;
}

original_code:
int
begin_response()
{
 if (!client.response->headers) {
  log_msg(0,"Missing headers, adding new ones");
  client.response->headers = new_headers();
 }
 connection(client.response->headers,"close");
 transfer_encoding(client.response->headers,"chunked");
 server_name(client.response->headers,"Jawas/1.0");
 return client.response->contents || client.response->raw_contents;
}


decompiled_code:
__int64 end_response()
{
write_chunk(*(_QWORD *)client[5], 0LL, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
write_chunk(*(_QWORD *)client[5], 0LL, 0LL);
return 0LL;
}

original_code:
int
end_response()
{
 write_chunk(client.response->socket,((void *)0),0);
 return 0;
}


decompiled_code:
_QWORD *new_region()
{
__int64 v0; // rbx
size_t v1; // rbx
_QWORD *v2; // rax
_QWORD *result; // rax

v0 = sysconf(30);
if ( memory )
{
munmap(memory, *(_QWORD *)memory);
memory = 0LL;
}
v1 = 10000 * v0;
v2 = mmap(0LL, v1, 3, 34, -1, 0LL);
memory = v2;
if ( v2 == (_QWORD *)-1LL )
{
perror("mmap");
fwrite("Failed to allocate memory\n", 1uLL, 0x1AuLL, stderr);
exit(0);
}
*v2 = v1;
result = memory;
*((_QWORD *)memory + 1) = *(_QWORD *)memory - 24LL;
result[2] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
__int64 v0; // rbx
size_t v1; // rbx
_QWORD *v2; // rax
_QWORD *result; // rax

v0 = sysconf(30);
if ( memory )
{
munmap(memory, *(_QWORD *)memory);
memory = 0LL;
}
v1 = 10000 * v0;
v2 = mmap(0LL, v1, 3, 34, -1, 0LL);
memory = v2;
if ( v2 == (_QWORD *)-1LL )
{
perror("mmap");
fwrite("Failed to allocate memory\n", 1uLL, 0x1AuLL, stderr);
exit(0);
}
*v2 = v1;
result = memory;
*((_QWORD *)memory + 1) = *(_QWORD *)memory - 24LL;
result[2] = 0LL;
return result;
}

original_code:
void
new_region()
{
 size_t page_size = sysconf(_SC_PAGESIZE);
 if (memory) {
  munmap(memory,memory->size);
  memory = ((void *)0);
 }
 memory = (Region)mmap(((void *)0),page_size*10000,0x1|0x2,0x02|0x20,-1,0);
 if (memory == ((void *) -1)) {
  perror("mmap");
  fprintf(stderr, "Failed to allocate memory\n");
  exit(0);
 }
 memory->size = page_size * 10000;
 memory->allocated = memory->size - sizeof(struct region_struct);
 memory->offset = 0;
}

canonical_original_code:
void new_region()
{
 unsigned long page_size = sysconf(_SC_PAGESIZE);
 if (memory) {
  munmap(memory,memory->size);
  memory = ((void *)0);
 }
 memory = (struct region_struct *)mmap(((void *)0),page_size*10000,0x1|0x2,0x02|0x20,-1,0);
 if (memory == ((void *) -1)) {
  perror("mmap");
  fprintf(stderr, "Failed to allocate memory\n");
  exit(0);
 }
 memory->size = page_size * 10000;
 memory->allocated = memory->size - sizeof(struct region_struct);
 memory->offset = 0;
}

user_defined_types: struct region_struct { unsigned long size; unsigned long allocated; unsigned long offset; char data[]; }


advance

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

result = a1;
*((_QWORD *)memory + 2) += a1;
return result;
}

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

result = a1;
*((_QWORD *)memory + 2) += a1;
return result;
}

original_code:
size_t
advance(size_t length)
{
 memory->offset += length;
 return length;
}

canonical_original_code:
unsigned long advance(unsigned long length)
{
 memory->offset += length;
 return length;
}

variable_types:
length
unsigned long

return_type: unsigned long


reserve

decompiled_code:
char *__fastcall reserve(__int64 a1)
{
char *v1; // rbx

v1 = (char *)memory + *((_QWORD *)memory + 2) + 24;
advance(a1);
return v1;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
char *v1; // rbx

v1 = (char *)memory + *((_QWORD *)memory + 2) + 24;
func1(a1);
return v1;
}

original_code:
char*
reserve(size_t length)
{
 char* retval = &memory->data[memory->offset];
 advance(length);
 return retval;
}

canonical_original_code:
char *reserve(unsigned long length)
{
 char *retval = &memory->data[memory->offset];
 advance(length);
 return retval;
}

variable_types:
retval
char *

return_type: char *