decompiled_code:
__int64 __fastcall process_commond(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r14
__int64 v3; // rdx
__int64 v4; // rax
unsigned __int64 v5; // rdx
unsigned int v6; // er12
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = *(_QWORD *)(a1 + 184);
if ( !strcasecmp(*(const char **)(*(_QWORD *)v1 + 8LL), "quit") )
{
v6 = 0;
*(_DWORD *)(a1 + 180) |= 1u;
reply_obj(a1, shared[2]);
}
else
{
v2 = *(_QWORD *)(a1 + 208);
memset(v8, 0, 0x400uLL);
cstr_tolower(*(_QWORD *)(*(_QWORD *)v1 + 8LL));
v3 = dict_find(*(_QWORD *)(v2 + 40), *(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
memset(v8, 0, 0x400uLL);
if ( !v3 )
{
snprintf(v8, 0x400uLL, "-ERR unknown command '%s'\r\n", *(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
return (unsigned int)reply_str(a1, v8);
}
v4 = *(_QWORD *)(v3 + 8);
v5 = *(int *)(v4 + 20);
if ( (v5 & 0x80000000) == 0LL )
{
switch ( *(_DWORD *)(v4 + 16) )
{
case 0:
if ( v5 == *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 1:
if ( v5 < *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 2:
if ( v5 > *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 3:
if ( v5 <= *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 4:
if ( v5 >= *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
default:
goto LABEL_6;
}
snprintf(
v8,
0x400uLL,
"-ERR wrong number arguments for command '%s'\r\n",
*(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
return (unsigned int)reply_str(a1, v8);
}
LABEL_6:
v6 = 0;
(*(void (__fastcall **)(__int64))(v4 + 8))(a1);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r14
__int64 v3; // rdx
__int64 v4; // rax
unsigned __int64 v5; // rdx
unsigned int v6; // er12
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = *(_QWORD *)(a1 + 184);
if ( !strcasecmp(*(const char **)(*(_QWORD *)v1 + 8LL), "quit") )
{
v6 = 0;
*(_DWORD *)(a1 + 180) |= 1u;
reply_obj(a1, shared[2]);
}
else
{
v2 = *(_QWORD *)(a1 + 208);
memset(v8, 0, 0x400uLL);
cstr_tolower(*(_QWORD *)(*(_QWORD *)v1 + 8LL));
v3 = dict_find(*(_QWORD *)(v2 + 40), *(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
memset(v8, 0, 0x400uLL);
if ( !v3 )
{
snprintf(v8, 0x400uLL, "-ERR unknown command '%s'\r\n", *(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
return (unsigned int)reply_str(a1, v8);
}
v4 = *(_QWORD *)(v3 + 8);
v5 = *(int *)(v4 + 20);
if ( (v5 & 0x80000000) == 0LL )
{
switch ( *(_DWORD *)(v4 + 16) )
{
case 0:
if ( v5 == *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 1:
if ( v5 < *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 2:
if ( v5 > *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 3:
if ( v5 <= *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
case 4:
if ( v5 >= *(_QWORD *)(a1 + 192) )
goto LABEL_6;
break;
default:
goto LABEL_6;
}
snprintf(
v8,
0x400uLL,
"-ERR wrong number arguments for command '%s'\r\n",
*(_QWORD *)(**(_QWORD **)(a1 + 184) + 8LL));
return (unsigned int)reply_str(a1, v8);
}
LABEL_6:
v6 = 0;
(*(void (__fastcall **)(__int64))(v4 + 8))(a1);
}
return v6;
}

original_code:
int process_commond(cio *io) {
 char buf[1024];
 thr_socket_svr *svr = (thr_socket_svr*)io->priv;
 dict_entry *cmd_entry;
 struct command *cmd;
 if(strcasecmp((char *)io->argv[0]->priv, "quit") == 0) {
  io->flag |= 0x1;
  reply_obj(io, shared.ok);
  return 0;
 }
 memset(buf, 0, sizeof(buf));
 cstr_tolower((char *)io->argv[0]->priv);
 cmd_entry = dict_find(svr->commands, (char *)io->argv[0]->priv);
 memset(buf, 0, sizeof(buf));
 if(cmd_entry != ((void *)0)) {
  cmd = (struct command*)cmd_entry->value;
  if(cmd->argc >= 0) {
   if(cmd->argc != -1) {
    switch(cmd->symbolic) {
    case EQ:
     if(cmd->argc != io->argc)
      goto args_err;
     break;
    case LT:
     if(cmd->argc <= io->argc)
      goto args_err;
     break;
    case GT:
     if(cmd->argc >= io->argc)
      goto args_err;
     break;
    case LE:
     if(cmd->argc < io->argc)
      goto args_err;
     break;
    case GE:
     if(cmd->argc > io->argc)
      goto args_err;
     break;
    }
   }
  }
  cmd->call(io);
  return 0;
 }
 snprintf(buf,sizeof(buf), "-ERR unknown command '%s'\r\n", (char *)io->argv[0]->priv);
 return reply_str(io, buf);
args_err:
 snprintf(buf,sizeof(buf), "-ERR wrong number arguments for command '%s'\r\n", (char *)io->argv[0]->priv);
 return reply_str(io, buf);
}

canonical_original_code:
int process_commond(struct cio *io) {
 char buf[1024];
 struct thr_socket_svr *svr = (thr_socket_svr*)io->priv;
 struct dict_entry *cmd_entry;
 struct command *cmd;
 if(strcasecmp((char *)io->argv[0]->priv, "quit") == 0) {
  io->flag |= 0x1;
  reply_obj(io, shared.ok);
  return 0;
 }
 memset(buf, 0, sizeof(buf));
 cstr_tolower((char *)io->argv[0]->priv);
 cmd_entry = dict_find(svr->commands, (char *)io->argv[0]->priv);
 memset(buf, 0, sizeof(buf));
 if(cmd_entry != ((void *)0)) {
  cmd = (struct command *)cmd_entry->value;
  if(cmd->argc >= 0) {
   if(cmd->argc != -1) {
    switch(cmd->symbolic) {
    case EQ:
     if(cmd->argc != io->argc)
      goto args_err;
     break;
    case LT:
     if(cmd->argc <= io->argc)
      goto args_err;
     break;
    case GT:
     if(cmd->argc >= io->argc)
      goto args_err;
     break;
    case LE:
     if(cmd->argc < io->argc)
      goto args_err;
     break;
    case GE:
     if(cmd->argc > io->argc)
      goto args_err;
     break;
    }
   }
  }
  cmd->call(io);
  return 0;
 }
 snprintf(buf,sizeof(buf), "-ERR unknown command '%s'\r\n", (char *)io->argv[0]->priv);
 return reply_str(io, buf);
args_err:
 snprintf(buf,sizeof(buf), "-ERR wrong number arguments for command '%s'\r\n", (char *)io->argv[0]->priv);
 return reply_str(io, buf);
}

variable_types: struct cio { int fd; int type; int mask; char *rbuf; char *wbuf; int wcount; char ip[128]; int nbulk; int bulk_len; int reqtype; int port; int flag; struct obj { int ref; int type; void *priv; unsigned int lock; } **argv; unsigned long argc; int tabidx; void *priv; void *thr_priv; } *

variable_types:
buf
char [1024]

variable_types: struct dict_entry { void *key; void *value; int ref; struct dict_entry *next; } *

user_defined_types: struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; }

user_defined_types: struct cevent_fired { int mask; int fd; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }

user_defined_types: struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


decompiled_code:
__int64 __fastcall process_event(__int64 *a1)
{
__int64 v2; // rbp
char v3; // cl
__int64 v4; // rbx
__int64 v6; // rdi
__int64 v7; // rax
int v8; // [rsp+8h] [rbp-30h] BYREF
unsigned int v9; // [rsp+Ch] [rbp-2Ch]

v2 = a1[1];
LABEL_2:
while ( (unsigned int)cevents_pop_fired(v2, &v8) )
{
while ( 1 )
{
v3 = v8;
v4 = *(_QWORD *)(v2 + 8) + 32LL * (int)v9;
if ( (v8 & 4) != 0 )
break;
if ( (v8 & 1) != 0 )
{
(*(void (__fastcall **)(__int64, _QWORD, _QWORD))(v4 + 8))(v2, (int)v9, *(_QWORD *)(v4 + 24));
v3 = v8;
}
if ( (v3 & 2) == 0 )
goto LABEL_2;
(*(void (__fastcall **)(__int64, _QWORD, _QWORD))(v4 + 16))(v2, v9, *(_QWORD *)(v4 + 24));
if ( !(unsigned int)cevents_pop_fired(v2, &v8) )
return 0LL;
}
v6 = *(_QWORD *)(v4 + 24);
v7 = *a1;
*(_DWORD *)(v6 + 8) = v8;
*(_QWORD *)(v6 + 216) = v7;
process_commond(v6);
}
return 0LL;
}

original_code:
void *process_event(void *priv) {
 int rs;
 pair *pr = (pair*)priv;
 cevents *cevts = (cevents*)pr->second;
 cevent_fired fired;
 cevent *evt;
 while(1) {
  if(cevents_pop_fired(cevts, &fired) == 0)
   return ((void *)0);
  evt = cevts->events + fired.fd;
  if(fired.mask & 0x1<<2) {
   cio *io = (cio*)evt->priv;
   io->mask = fired.mask;
   io->thr_priv = pr->first;
   process_commond(io);
  } else {
   if(fired.mask & 0x1) {
    evt->read_proc(cevts, fired.fd, evt->priv, fired.mask);
   }
   if(fired.mask & 0x1<<1) {
    evt->write_proc(cevts, fired.fd, evt->priv, fired.mask);
   }
  }
 }
 return ((void *)0);
}

canonical_original_code:
void *process_event(void *priv) {
 int rs;
 struct pair *pr = (pair*)priv;
 struct cevents *cevts = (cevents*)pr->second;
 struct cevent_fired fired;
 struct cevent *evt;
 while(1) {
  if(cevents_pop_fired(cevts, &fired) == 0)
   return ((void *)0);
  evt = cevts->events + fired.fd;
  if(fired.mask & 0x1<<2) {
   struct cio *io = (cio*)evt->priv;
   io->mask = fired.mask;
   io->thr_priv = pr->first;
   process_commond(io);
  } else {
   if(fired.mask & 0x1) {
    evt->read_proc(cevts, fired.fd, evt->priv, fired.mask);
   }
   if(fired.mask & 0x1<<1) {
    evt->write_proc(cevts, fired.fd, evt->priv, fired.mask);
   }
  }
 }
 return ((void *)0);
}

variable_types:
priv
void *

variable_types: rs int

variable_types: struct pair { void *first; void *second; } *

variable_types: struct cevents { int maxfd; struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *

variable_types: struct cevent { int mask; int (*read_proc)(struct cevents { int maxfd; struct cevent *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *

return_type: void *

user_defined_types: struct cevents { int maxfd; struct cevent *events; struct cevent_fired *fired; struct clist *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; }

user_defined_types: struct cio { int fd; int type; int mask; char *rbuf; char *wbuf; int wcount; char ip[128]; int nbulk; int bulk_len; int reqtype; int port; int flag; struct obj **argv; unsigned long argc; int tabidx; void *priv; void *thr_priv; }

user_defined_types: struct clist { struct clist_item *head; unsigned long count; }

user_defined_types: struct obj { int ref; int type; void *priv; unsigned int lock; }


decompiled_code:
__int64 create_tcp_server()
{
int v0; // eax
unsigned int v1; // er12
char v3[1048]; // [rsp+0h] [rbp-418h] BYREF

v0 = cnet_tcp_server("0.0.0.0", port, v3, 1024LL);
if ( v0 < 0 )
{
v1 = -1;
log_print(4LL, "%s", v3);
}
else
{
v1 = v0;
cio_set_noblock((unsigned int)v0);
}
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
unsigned int v1; // er12
char v3[1048]; // [rsp+0h] [rbp-418h] BYREF

v0 = cnet_tcp_server("0.0.0.0", port, v3, 1024LL);
if ( v0 < 0 )
{
v1 = -1;
log_print(4LL, "%s", v3);
}
else
{
v1 = v0;
cio_set_noblock((unsigned int)v0);
}
return v1;
}

original_code:
int create_tcp_server() {
 int fd;
 char buff[1024];
 fd = cnet_tcp_server("0.0.0.0", port, buff, sizeof(buff));
 if(fd < 0) {
  log_print(LEVEL_ERR, "%s", buff);
  return -1;
 }
 cio_set_noblock(fd);
 return fd;
}

variable_types: fd int

variable_types:
buff
char [1024]

return_type: int


decompiled_code:
__int64 __fastcall destroy_thr_server(__int64 a1)
{
int v2; // edi
int v3; // edi

v2 = *(_DWORD *)a1;
if ( v2 > 0 )
close(v2);
v3 = *(_DWORD *)(a1 + 8);
if ( v3 > 0 )
close(v3);
if ( *(_QWORD *)(a1 + 56) )
cthr_pool_destroy();
if ( *(_QWORD *)(a1 + 32) )
cevents_destroy();
if ( *(_QWORD *)(a1 + 40) )
dict_destroy();
return jfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int v2; // edi
int v3; // edi

v2 = *(_DWORD *)a1;
if ( v2 > 0 )
close(v2);
v3 = *(_DWORD *)(a1 + 8);
if ( v3 > 0 )
close(v3);
if ( *(_QWORD *)(a1 + 56) )
cthr_pool_destroy();
if ( *(_QWORD *)(a1 + 32) )
cevents_destroy();
if ( *(_QWORD *)(a1 + 40) )
dict_destroy();
return jfree(a1);
}

original_code:
void destroy_thr_server(thr_socket_svr *svr) {
 if(svr->in_fd > 0)
  close(svr->in_fd);
 if(svr->logfd > 0)
  close(svr->logfd);
 if(svr->thr_pool)
  cthr_pool_destroy(svr->thr_pool);
 if(svr->evts)
  cevents_destroy(svr->evts);
 if(svr->commands)
  dict_destroy(svr->commands);
 jfree(svr);
}

canonical_original_code:
void destroy_thr_server(struct thr_socket_svr *svr) {
 if(svr->in_fd > 0)
  close(svr->in_fd);
 if(svr->logfd > 0)
  close(svr->logfd);
 if(svr->thr_pool)
  cthr_pool_destroy(svr->thr_pool);
 if(svr->evts)
  cevents_destroy(svr->evts);
 if(svr->commands)
  dict_destroy(svr->commands);
 jfree(svr);
}

variable_types: struct thr_socket_svr { int in_fd; int un_fd; int logfd; int pid; unsigned int connections; long last_info_time; struct cevents { int maxfd; struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *evts; struct dict { struct dict_opts { unsigned int (*hash)(void *key); void *(*key_dup)(void *key); void *(*value_dup)(void *key); int (*key_compare)(void *k1, void *k2); void (*key_destroy)(void *key); void (*value_destroy)(void *v); } *opts; struct dict_table { struct dict_entry { void *key; void *value; int ref; struct dict_entry *next; } **entries; unsigned int size; unsigned int mask; unsigned int used; } dt[2]; int rehashidx; int iterators; } *commands; int running; struct _thr_pool { struct cthread { unsigned long thrid; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool *pool; } *thrs; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); } *thr_pool; } *

return_type: void

user_defined_types: struct thr_socket_svr { int in_fd; int un_fd; int logfd; int pid; unsigned int connections; long last_info_time; struct cevents *evts; struct dict *commands; int running; struct _thr_pool *thr_pool; }

user_defined_types: struct dict { struct dict_opts *opts; struct dict_table dt[2]; int rehashidx; int iterators; }

user_defined_types: struct _thr_pool { struct cthread *thrs; union pthread_mutex_t mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct dict_table { struct dict_entry **entries; unsigned int size; unsigned int mask; unsigned int used; }

user_defined_types: struct cthread { unsigned long thrid; union pthread_cond_t cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool *pool; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }


decompiled_code:
__int64 create_thr_server()
{
_OWORD *v0; // rax
__int64 v1; // r12
unsigned int v2; // eax
__int64 v3; // rax
__int64 v4; // rax
int v5; // eax
__int64 result; // rax

shared_obj_create();
v0 = (_OWORD *)jmalloc(64LL);
*v0 = 0LL;
v1 = (__int64)v0;
v0[1] = 0LL;
v0[2] = 0LL;
v0[3] = 0LL;
v2 = fileno(stdout);
*(_DWORD *)(v1 + 8) = v2;
log_init(v2);
*(_QWORD *)(v1 + 40) = dict_create(command_opts);
regist_commands(v1);
v3 = cthr_pool_create(thread_num, &db_ctx_init, &db_ctx_uninit);
*(_QWORD *)(v1 + 56) = v3;
if ( v3
&& (v4 = cevents_create(),
*(_QWORD *)(v1 + 32) = v4,
log_print(2LL, "server used %s for event,\n", (const char *)(v4 + 52)),
*(_QWORD *)(v1 + 24) = 0LL,
v5 = create_tcp_server(),
*(_DWORD *)v1 = v5,
v5 > 0) )
{
*(_DWORD *)(v1 + 48) = 0;
result = v1;
}
else
{
destroy_thr_server(v1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
_OWORD *v0; // rax
__int64 v1; // r12
unsigned int v2; // eax
__int64 v3; // rax
__int64 v4; // rax
int v5; // eax
__int64 result; // rax

func4();
v0 = (_OWORD *)jmalloc(64LL);
*v0 = 0LL;
v1 = (__int64)v0;
v0[1] = 0LL;
v0[2] = 0LL;
v0[3] = 0LL;
v2 = fileno(stdout);
*(_DWORD *)(v1 + 8) = v2;
log_init(v2);
*(_QWORD *)(v1 + 40) = dict_create(command_opts);
func3(v1);
v3 = cthr_pool_create(thread_num, &db_ctx_init, &db_ctx_uninit);
*(_QWORD *)(v1 + 56) = v3;
if ( v3
&& (v4 = cevents_create(),
*(_QWORD *)(v1 + 32) = v4,
log_print(2LL, "server used %s for event,\n", (const char *)(v4 + 52)),
*(_QWORD *)(v1 + 24) = 0LL,
v5 = func7(),
*(_DWORD *)v1 = v5,
v5 > 0) )
{
*(_DWORD *)(v1 + 48) = 0;
result = v1;
}
else
{
func8(v1);
result = 0LL;
}
return result;
}

original_code:
thr_socket_svr *create_thr_server() {
 thr_socket_svr *svr;
 shared_obj_create();
 svr = jmalloc(sizeof(thr_socket_svr));
 memset(svr, 0, sizeof(thr_socket_svr));
 svr->logfd = fileno(stdout);
 log_init(svr->logfd);
 svr->commands = dict_create(&command_opts);
 regist_commands(svr);
 svr->thr_pool = cthr_pool_create(thread_num, db_ctx_init, db_ctx_uninit);
 if(svr->thr_pool == ((void *)0)) {
  destroy_thr_server(svr);
  return ((void *)0);
 }
 svr->evts = cevents_create();
 log_print(LEVEL_INFO, "server used %s for event,\n", svr->evts->impl_name);
 svr->last_info_time = 0;
 svr->in_fd = create_tcp_server();
 if(svr->in_fd <= 0) {
  destroy_thr_server(svr);
  return ((void *)0);
 }
 svr->running = 0;
 return svr;
}

canonical_original_code:
struct thr_socket_svr *create_thr_server() {
 struct thr_socket_svr *svr;
 shared_obj_create();
 svr = jmalloc(sizeof(thr_socket_svr));
 memset(svr, 0, sizeof(thr_socket_svr));
 svr->logfd = fileno(stdout);
 log_init(svr->logfd);
 svr->commands = dict_create(&command_opts);
 regist_commands(svr);
 svr->thr_pool = cthr_pool_create(thread_num, db_ctx_init, db_ctx_uninit);
 if(svr->thr_pool == ((void *)0)) {
  destroy_thr_server(svr);
  return ((void *)0);
 }
 svr->evts = cevents_create();
 log_print(LEVEL_INFO, "server used %s for event,\n", svr->evts->impl_name);
 svr->last_info_time = 0;
 svr->in_fd = create_tcp_server();
 if(svr->in_fd <= 0) {
  destroy_thr_server(svr);
  return ((void *)0);
 }
 svr->running = 0;
 return svr;
}

return_type: struct thr_socket_svr { int in_fd; int un_fd; int logfd; int pid; unsigned int connections; long last_info_time; struct cevents { int maxfd; struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *evts; struct dict { struct dict_opts { unsigned int (*hash)(void *key); void *(*key_dup)(void *key); void *(*value_dup)(void *key); int (*key_compare)(void *k1, void *k2); void (*key_destroy)(void *key); void (*value_destroy)(void *v); } *opts; struct dict_table { struct dict_entry { void *key; void *value; int ref; struct dict_entry *next; } **entries; unsigned int size; unsigned int mask; unsigned int used; } dt[2]; int rehashidx; int iterators; } *commands; int running; struct _thr_pool { struct cthread { unsigned long thrid; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool *pool; } *thrs; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); } *thr_pool; } *


mainLoop

decompiled_code:
__int64 __fastcall mainLoop(__int64 a1)
{
__int64 v1; // rdx
int v2; // er12
int i; // ebp
__int64 v5; // rax

*(_DWORD *)(a1 + 48) = 1;
while ( *(_DWORD *)(a1 + 48) )
{
v2 = cevents_poll(*(_QWORD *)(a1 + 32), 10LL);
if ( v2 > 0 )
{
for ( i = 0; i != v2; ++i )
{
if ( (unsigned int)cthr_pool_run_task(*(_QWORD *)(a1 + 56), process_event, *(_QWORD *)(a1 + 32)) == -1 )
break;
}
}
v1 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 40LL);
if ( *(_QWORD *)(a1 + 24) + 1LL < v1 )
{
*(_QWORD *)(a1 + 24) = v1;
v5 = used_mem();
log_print(2LL, "total connections:%lu, memory used:%lu\n", *(unsigned int *)(a1 + 16), v5);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rdx
int v2; // er12
int i; // ebp
__int64 v5; // rax

*(_DWORD *)(a1 + 48) = 1;
while ( *(_DWORD *)(a1 + 48) )
{
v2 = cevents_poll(*(_QWORD *)(a1 + 32), 10LL);
if ( v2 > 0 )
{
for ( i = 0; i != v2; ++i )
{
if ( (unsigned int)cthr_pool_run_task(*(_QWORD *)(a1 + 56), process_event, *(_QWORD *)(a1 + 32)) == -1 )
break;
}
}
v1 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 40LL);
if ( *(_QWORD *)(a1 + 24) + 1LL < v1 )
{
*(_QWORD *)(a1 + 24) = v1;
v5 = used_mem();
log_print(2LL, "total connections:%lu, memory used:%lu\n", *(unsigned int *)(a1 + 16), v5);
}
}
return 0LL;
}

original_code:
void *mainLoop(void *p) {
 thr_socket_svr *svr = (thr_socket_svr*)p;
 int ev_num, i, ret;
 svr->running = 1;
 while(svr->running) {
  ev_num = cevents_poll(svr->evts, 10);
  if(ev_num > 0) {
   for(i = 0; i < ev_num; i++) {
    if(cthr_pool_run_task(svr->thr_pool, process_event, svr->evts) == -1) {
     break;
    }
   }
  }
  if(svr->last_info_time + 2 <= svr->evts->poll_sec) {
    svr->last_info_time = svr->evts->poll_sec;
    log_print(LEVEL_INFO, "total connections:%lu, memory used:%lu\n", svr->connections, used_mem());
  }
 }
 return ((void *)0);
}

canonical_original_code:
void *mainLoop(void *p) {
 struct thr_socket_svr *svr = (thr_socket_svr*)p;
 int ev_num; int i; int ret;
 svr->running = 1;
 while(svr->running) {
  ev_num = cevents_poll(svr->evts, 10);
  if(ev_num > 0) {
   for(i = 0; i < ev_num; i++) {
    if(cthr_pool_run_task(svr->thr_pool, process_event, svr->evts) == -1) {
     break;
    }
   }
  }
  if(svr->last_info_time + 2 <= svr->evts->poll_sec) {
    svr->last_info_time = svr->evts->poll_sec;
    log_print(LEVEL_INFO, "total connections:%lu, memory used:%lu\n", svr->connections, used_mem());
  }
 }
 return ((void *)0);
}

variable_types:
p
void *

variable_types: i int

variable_types: ret int

user_defined_types: struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; }

user_defined_types: struct dict_opts { unsigned int (*hash)(void *key); void *(*key_dup)(void *key); void *(*value_dup)(void *key); int (*key_compare)(void *k1, void *k2); void (*key_destroy)(void *key); void (*value_destroy)(void *v); }

user_defined_types: struct dict_entry { void *key; void *value; int ref; struct dict_entry *next; }


decompiled_code:
__int64 __fastcall server_init(unsigned int *a1)
{
a1[4] = 0;
cevents_add_event(*((_QWORD *)a1 + 4), *a1, 5LL, &tcp_accept_event_proc);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1)
{
a1[4] = 0;
cevents_add_event(*((_QWORD *)a1 + 4), *a1, 5LL, &tcp_accept_event_proc);
return 0LL;
}

original_code:
int server_init(thr_socket_svr *svr) {
 svr->connections = 0;
 cevents_add_event(svr->evts, svr->in_fd, 0x1|0x1<<2, tcp_accept_event_proc, svr);
 return 0;
}

canonical_original_code:
int server_init(struct thr_socket_svr *svr) {
 svr->connections = 0;
 cevents_add_event(svr->evts, svr->in_fd, 0x1|0x1<<2, tcp_accept_event_proc, svr);
 return 0;
}

user_defined_types: struct cevent_fired { int mask; int fd; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }

user_defined_types: struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; }


decompiled_code:
__int64 thrs_used_mem()
{
return used_mem();
}

canonical_decompiled_code:
__int64 func12()
{
return used_mem();
}

original_code:
long long thrs_used_mem() {
 return used_mem();
}

return_type: long long


original_code:
void *thread_loop(void *data) {
 long stack_bottom;
 cthread *thr = (cthread*)data;
 cthr_pool *pool = thr->pool;
 void *priv = pool->thr_init_cb(&stack_bottom);
 for(;;) {
  pair p;
  pthread_mutex_lock(&pool->mutex);
  thr->state = 0x1;
  if(pthread_cond_wait(&thr->cond, &pool->mutex) < 0) {
   log_print(LEVEL_ERR, "wait error\n");
   pool->thr_uninit_cb(priv);
   return ((void *)0);
  }
  pthread_mutex_unlock(&pool->mutex);
  if(pool->state == 0x1) {
   thr->state = 0x1<<2;
   pool->thr_uninit_cb(priv);
   return ((void *)0);
  }
  thr->state = 0x1<<1;
  p.first = priv;
  p.second = thr->proc_data;
  if(thr->proc)
   thr->proc(&p);
  thr->proc = ((void *)0);
  thr->proc_data = ((void *)0);
 }
}

canonical_original_code:
void *thread_loop(void *data) {
 long stack_bottom;
 struct cthread *thr = (cthread*)data;
 struct _thr_pool *pool = thr->pool;
 void *priv = pool->thr_init_cb(&stack_bottom);
 for(;;) {
  struct pair p;
  pthread_mutex_lock(&pool->mutex);
  thr->state = 0x1;
  if(pthread_cond_wait(&thr->cond, &pool->mutex) < 0) {
   log_print(LEVEL_ERR, "wait error\n");
   pool->thr_uninit_cb(priv);
   return ((void *)0);
  }
  pthread_mutex_unlock(&pool->mutex);
  if(pool->state == 0x1) {
   thr->state = 0x1<<2;
   pool->thr_uninit_cb(priv);
   return ((void *)0);
  }
  thr->state = 0x1<<1;
  p.first = priv;
  p.second = thr->proc_data;
  if(thr->proc)
   thr->proc(&p);
  thr->proc = ((void *)0);
  thr->proc_data = ((void *)0);
 }
}

variable_types:
data
void *

variable_types: struct cthread { unsigned long thrid; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool { struct cthread *thrs; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); } *pool; } *

variable_types: struct _thr_pool { struct cthread { unsigned long thrid; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool *pool; } *thrs; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); } *

variable_types: struct pair { void *first; void *second; }


decompiled_code:
__int64 __fastcall cthread_init(void *arg, unsigned __int64 a2)
{
pthread_cond_t *v3; // rdi
__int64 result; // rax
pthread_attr_t v5; // [rsp+0h] [rbp-58h] BYREF

v3 = (pthread_cond_t *)((char *)arg + 8);
v3[1].__woken_seq = a2;
if ( pthread_cond_init(v3, 0LL) < 0 )
{
log_print(4LL, "init thread cond error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_attr_init(&v5) < 0 )
{
log_print(4LL, "init thread attr error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_create((pthread_t *)arg, &v5, thread_loop, arg) < 0 )
{
log_print(4LL, "create thread error\n");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *arg, unsigned __int64 a2)
{
pthread_cond_t *v3; // rdi
__int64 result; // rax
pthread_attr_t v5; // [rsp+0h] [rbp-58h] BYREF

v3 = (pthread_cond_t *)((char *)arg + 8);
v3[1].__woken_seq = a2;
if ( pthread_cond_init(v3, 0LL) < 0 )
{
log_print(4LL, "init thread cond error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_attr_init(&v5) < 0 )
{
log_print(4LL, "init thread attr error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_create((pthread_t *)arg, &v5, thread_loop, arg) < 0 )
{
log_print(4LL, "create thread error\n");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

original_code:
static int cthread_init(cthread *thr, cthr_pool *pool) {
 pthread_attr_t thr_attr;
 thr->pool = pool;
 if(pthread_cond_init(&thr->cond, ((void *)0)) < 0) {
  log_print(LEVEL_ERR, "init thread cond error\n");
  return -1;
 }
 if(pthread_attr_init(&thr_attr) < 0) {
  log_print(LEVEL_ERR, "init thread attr error\n");
  return -1;
 }
 if(pthread_create(&thr->thrid, &thr_attr, thread_loop, thr) < 0) {
  log_print(LEVEL_ERR, "create thread error\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
static int cthread_init(struct cthread *thr, struct _thr_pool *pool) {
 union pthread_attr_t thr_attr;
 thr->pool = pool;
 if(pthread_cond_init(&thr->cond, ((void *)0)) < 0) {
  log_print(LEVEL_ERR, "init thread cond error\n");
  return -1;
 }
 if(pthread_attr_init(&thr_attr) < 0) {
  log_print(LEVEL_ERR, "init thread attr error\n");
  return -1;
 }
 if(pthread_create(&thr->thrid, &thr_attr, thread_loop, thr) < 0) {
  log_print(LEVEL_ERR, "create thread error\n");
  return -1;
 }
 return 0;
}

variable_types:
pool
struct _thr_pool *

variable_types: union pthread_attr_t { char __size[56]; long __align; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


decompiled_code:
__int64 __fastcall cthr_pool_destroy(__int64 *a1)
{
pthread_mutex_t *v1; // r15
__int64 v2; // rbp
unsigned __int64 v3; // rbx
bool v4; // zf
__int64 v5; // r13
__int64 v6; // r13
int *v7; // rax
char *v8; // rax
int v10; // [rsp+4h] [rbp-54h]
void *v11; // [rsp+18h] [rbp-40h] BYREF

v1 = (pthread_mutex_t *)(a1 + 1);
v2 = 0LL;
v3 = 0LL;
v4 = a1[7] == 0;
*((_DWORD *)a1 + 12) = 1;
if ( !v4 )
{
do
{
v5 = *a1;
pthread_mutex_lock(v1);
v6 = v2 + v5;
v10 = pthread_cond_signal((pthread_cond_t *)(v6 + 8));
pthread_mutex_unlock(v1);
if ( v10 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_print(4LL, "%s\n", v8);
}
++v3;
v2 += 88LL;
pthread_join(*(_QWORD *)v6, &v11);
pthread_cond_destroy((pthread_cond_t *)(v6 + 8));
}
while ( a1[7] > v3 );
}
pthread_mutex_destroy(v1);
jfree(*a1);
return jfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1)
{
pthread_mutex_t *v1; // r15
__int64 v2; // rbp
unsigned __int64 v3; // rbx
bool v4; // zf
__int64 v5; // r13
__int64 v6; // r13
int *v7; // rax
char *v8; // rax
int v10; // [rsp+4h] [rbp-54h]
void *v11; // [rsp+18h] [rbp-40h] BYREF

v1 = (pthread_mutex_t *)(a1 + 1);
v2 = 0LL;
v3 = 0LL;
v4 = a1[7] == 0;
*((_DWORD *)a1 + 12) = 1;
if ( !v4 )
{
do
{
v5 = *a1;
pthread_mutex_lock(v1);
v6 = v2 + v5;
v10 = pthread_cond_signal((pthread_cond_t *)(v6 + 8));
pthread_mutex_unlock(v1);
if ( v10 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_print(4LL, "%s\n", v8);
}
++v3;
v2 += 88LL;
pthread_join(*(_QWORD *)v6, &v11);
pthread_cond_destroy((pthread_cond_t *)(v6 + 8));
}
while ( a1[7] > v3 );
}
pthread_mutex_destroy(v1);
jfree(*a1);
return jfree(a1);
}

original_code:
void cthr_pool_destroy(cthr_pool *pool) {
 cthread *thr;
 void *thr_ret;
 int i, ret;
 pool->state = 0x1;
 for(i = 0; i < pool->size; i++) {
  thr = pool->thrs + i;
  pthread_mutex_lock(&pool->mutex);
  ret = pthread_cond_signal(&thr->cond);
  pthread_mutex_unlock(&pool->mutex);
  if(ret < 0) {
   log_print(LEVEL_ERR, "%s\n", strerror((*__errno_location ())));
  }
  pthread_join(thr->thrid, &thr_ret);
  pthread_cond_destroy(&thr->cond);
 }
 pthread_mutex_destroy(&pool->mutex);
 jfree(pool->thrs);
 jfree(pool);
}

canonical_original_code:
void cthr_pool_destroy(struct _thr_pool *pool) {
 struct cthread *thr;
 void *thr_ret;
 int i; int ret;
 pool->state = 0x1;
 for(i = 0; i < pool->size; i++) {
  thr = pool->thrs + i;
  pthread_mutex_lock(&pool->mutex);
  ret = pthread_cond_signal(&thr->cond);
  pthread_mutex_unlock(&pool->mutex);
  if(ret < 0) {
   log_print(LEVEL_ERR, "%s\n", strerror((*__errno_location ())));
  }
  pthread_join(thr->thrid, &thr_ret);
  pthread_cond_destroy(&thr->cond);
 }
 pthread_mutex_destroy(&pool->mutex);
 jfree(pool->thrs);
 jfree(pool);
}


decompiled_code:
unsigned __int64 __fastcall cthr_pool_create(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v4; // r12
__int64 v5; // rbx
__int64 i; // rbp
unsigned __int64 result; // rax
__int64 v8; // rax

v4 = jmalloc(80LL);
*(_QWORD *)v4 = jmalloc(88 * a1);
if ( pthread_mutex_init((pthread_mutex_t *)(v4 + 8), 0LL) < 0 )
{
log_print(4LL, "create thread pool error\n");
jfree(v4);
LABEL_7:
result = 0LL;
}
else
{
*(_QWORD *)(v4 + 56) = a1;
*(_QWORD *)(v4 + 64) = a2;
*(_QWORD *)(v4 + 72) = a3;
*(_DWORD *)(v4 + 48) = 0;
if ( a1 )
{
v5 = 0LL;
for ( i = 0LL; i != a1; ++i )
{
if ( (int)cthread_init((void *)(v5 + *(_QWORD *)v4), v4) < 0 )
{
cthr_pool_destroy((__int64 *)v4);
goto LABEL_7;
}
v5 += 88LL;
}
v8 = *(_QWORD *)v4;
do
v8 += 88LL;
while ( *(_QWORD *)v4 + 88 * a1 != v8 );
}
*(_DWORD *)(v4 + 48) = 0;
result = v4;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v4; // r12
__int64 v5; // rbx
__int64 i; // rbp
unsigned __int64 result; // rax
__int64 v8; // rax

v4 = jmalloc(80LL);
*(_QWORD *)v4 = jmalloc(88 * a1);
if ( pthread_mutex_init((pthread_mutex_t *)(v4 + 8), 0LL) < 0 )
{
log_print(4LL, "create thread pool error\n");
jfree(v4);
LABEL_7:
result = 0LL;
}
else
{
*(_QWORD *)(v4 + 56) = a1;
*(_QWORD *)(v4 + 64) = a2;
*(_QWORD *)(v4 + 72) = a3;
*(_DWORD *)(v4 + 48) = 0;
if ( a1 )
{
v5 = 0LL;
for ( i = 0LL; i != a1; ++i )
{
if ( (int)func1((void *)(v5 + *(_QWORD *)v4), v4) < 0 )
{
func2((__int64 *)v4);
goto LABEL_7;
}
v5 += 88LL;
}
v8 = *(_QWORD *)v4;
do
v8 += 88LL;
while ( *(_QWORD *)v4 + 88 * a1 != v8 );
}
*(_DWORD *)(v4 + 48) = 0;
result = v4;
}
return result;
}

original_code:
cthr_pool *cthr_pool_create(size_t size, thread_cb *init_cb, thread_cb *uninit_cb) {
 int i, ret;
 cthread *thr;
 cthr_pool *pool = (cthr_pool *)jmalloc(sizeof(cthr_pool));
 pool->thrs = jmalloc(sizeof(cthread)*size);
 ret = pthread_mutex_init(&pool->mutex, ((void *)0));
 if(ret < 0) {
  log_print(LEVEL_ERR, "create thread pool error\n");
  jfree(pool);
  return ((void *)0);
 }
 pool->size = size;
 pool->thr_init_cb = init_cb;
 pool->thr_uninit_cb = uninit_cb;
 pool->state = 0x0;
 for(i = 0; i < size; i++) {
  thr = pool->thrs + i;
  if(cthread_init(thr, pool) < 0) {
   cthr_pool_destroy(pool);
   return ((void *)0);
  }
 }
 for(i = 0; i < size; i++) {
  thr = pool->thrs + i;
  if(thr->state != 0x1)
   continue;
 }
 pool->state = 0x0;
 return pool;
}

canonical_original_code:
struct _thr_pool *cthr_pool_create(unsigned long size, void *(*init_cb)(void *), void *(*uninit_cb)(void *)) {
 int i; int ret;
 struct cthread *thr;
 struct _thr_pool *pool = (cthr_pool *)jmalloc(sizeof(cthr_pool));
 pool->thrs = jmalloc(sizeof(cthread)*size);
 ret = pthread_mutex_init(&pool->mutex, ((void *)0));
 if(ret < 0) {
  log_print(LEVEL_ERR, "create thread pool error\n");
  jfree(pool);
  return ((void *)0);
 }
 pool->size = size;
 pool->thr_init_cb = init_cb;
 pool->thr_uninit_cb = uninit_cb;
 pool->state = 0x0;
 for(i = 0; i < size; i++) {
  thr = pool->thrs + i;
  if(cthread_init(thr, pool) < 0) {
   cthr_pool_destroy(pool);
   return ((void *)0);
  }
 }
 for(i = 0; i < size; i++) {
  thr = pool->thrs + i;
  if(thr->state != 0x1)
   continue;
 }
 pool->state = 0x0;
 return pool;
}

variable_types:
size
unsigned long

return_type: struct _thr_pool { struct cthread { unsigned long thrid; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int state; void *(*proc)(void *); void *proc_data; struct _thr_pool *pool; } *thrs; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int state; unsigned long size; void *(*thr_init_cb)(void *); void *(*thr_uninit_cb)(void *); } *


decompiled_code:
__int64 __fastcall cthr_pool_run_task(__int64 *a1, __int64 a2, unsigned __int64 a3)
{
pthread_mutex_t *v3; // r13
__int64 v6; // rsi
__int64 v7; // rax
__int64 i; // rdx
__int64 v9; // rdi
pthread_cond_t *v10; // rdi

v3 = (pthread_mutex_t *)(a1 + 1);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 1));
v6 = a1[7];
if ( !v6 )
return 0xFFFFFFFFLL;
v7 = *a1;
for ( i = 0LL; i != v6; ++i )
{
v9 = v7;
if ( *(_DWORD *)(v7 + 56) == 1 )
break;
v7 += 88LL;
}
*(_QWORD *)(v9 + 64) = a2;
v10 = (pthread_cond_t *)(v9 + 8);
v10[1].__wakeup_seq = a3;
pthread_cond_signal(v10);
pthread_mutex_unlock(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, __int64 a2, unsigned __int64 a3)
{
pthread_mutex_t *v3; // r13
__int64 v6; // rsi
__int64 v7; // rax
__int64 i; // rdx
__int64 v9; // rdi
pthread_cond_t *v10; // rdi

v3 = (pthread_mutex_t *)(a1 + 1);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 1));
v6 = a1[7];
if ( !v6 )
return 0xFFFFFFFFLL;
v7 = *a1;
for ( i = 0LL; i != v6; ++i )
{
v9 = v7;
if ( *(_DWORD *)(v7 + 56) == 1 )
break;
v7 += 88LL;
}
*(_QWORD *)(v9 + 64) = a2;
v10 = (pthread_cond_t *)(v9 + 8);
v10[1].__wakeup_seq = a3;
pthread_cond_signal(v10);
pthread_mutex_unlock(v3);
return 0LL;
}

original_code:
int cthr_pool_run_task(cthr_pool *pool, cthread_proc *proc, void *proc_data) {
 cthread *thr = ((void *)0);
 int i;
 pthread_mutex_lock(&pool->mutex);
 for(i = 0; i < pool->size; i++) {
  thr = pool->thrs + i;
  if(thr->state == 0x1)
   break;
 }
 if(thr == ((void *)0))
  return -1;
 thr->proc = proc;
 thr->proc_data = proc_data;
 pthread_cond_signal(&thr->cond);
 pthread_mutex_unlock(&pool->mutex);
 return 0;
}

canonical_original_code:
int cthr_pool_run_task(struct _thr_pool *pool, void *(*proc)(void *), void *proc_data) {
 struct cthread *thr = ((void *)0);
 int i;
 pthread_mutex_lock(&pool->mutex);
 for(i = 0; i < pool->size; i++) {
  thr = pool->thrs + i;
  if(thr->state == 0x1)
   break;
 }
 if(thr == ((void *)0))
  return -1;
 thr->proc = proc;
 thr->proc_data = proc_data;
 pthread_cond_signal(&thr->cond);
 pthread_mutex_unlock(&pool->mutex);
 return 0;
}

variable_types:
proc
void *(*)(void *)


decompiled_code:
int __fastcall cevents_add_event_impl(__int64 a1, int a2, char a3)
{
int *v4; // rcx
int *v5; // rdi
int v6; // eax
int v7; // esi
char v8; // al
int v9; // esi
int v10; // edi
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

v4 = (int *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
v5 = *(int **)(a1 + 120);
v6 = *v4;
HIDWORD(event.data.u64) = 0;
*(_QWORD *)&event.events = 0LL;
v7 = -(v6 == 0);
v8 = a3 | v6;
v9 = (v7 & 0xFFFFFFFE) + 3;
if ( (v8 & 1) != 0 )
event.events = 1;
if ( (v8 & 2) != 0 )
event.events |= 4u;
v10 = *v5;
event.data.fd = a2;
return epoll_ctl(v10, v9, a2, &event);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, int a2, char a3)
{
int *v4; // rcx
int *v5; // rdi
int v6; // eax
int v7; // esi
char v8; // al
int v9; // esi
int v10; // edi
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

v4 = (int *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
v5 = *(int **)(a1 + 120);
v6 = *v4;
HIDWORD(event.data.u64) = 0;
*(_QWORD *)&event.events = 0LL;
v7 = -(v6 == 0);
v8 = a3 | v6;
v9 = (v7 & 0xFFFFFFFE) + 3;
if ( (v8 & 1) != 0 )
event.events = 1;
if ( (v8 & 2) != 0 )
event.events |= 4u;
v10 = *v5;
event.data.fd = a2;
return epoll_ctl(v10, v9, a2, &event);
}

original_code:
static int cevents_add_event_impl(cevents *cevts, int fd, int mask) {
 int operation = 1;
 int old_mask = cevts->events[fd].mask;
 struct epoll_event ep_event;
 memset(&ep_event, 0, sizeof(struct epoll_event));
 epoll_priv *priv = (epoll_priv*)cevts->priv_data;
 if(old_mask != 0x0)
  operation = 3;
 mask |= old_mask;
 if(mask & 0x1) ep_event.events |= EPOLLIN;
 if(mask & 0x1<<1) ep_event.events |= EPOLLOUT;
 ep_event.data.fd = fd;
 return epoll_ctl(priv->epfd, operation, fd, &ep_event);
}

canonical_original_code:
static int cevents_add_event_impl(struct cevents *cevts, int fd, int mask) {
 int operation = 1;
 int old_mask = cevts->events[fd].mask;
 struct epoll_event ep_event;
 memset(&ep_event, 0, sizeof(struct epoll_event));
 struct epoll_priv *priv = (epoll_priv*)cevts->priv_data;
 if(old_mask != 0x0)
  operation = 3;
 mask |= old_mask;
 if(mask & 0x1) ep_event.events |= EPOLLIN;
 if(mask & 0x1<<1) ep_event.events |= EPOLLOUT;
 ep_event.data.fd = fd;
 return epoll_ctl(priv->epfd, operation, fd, &ep_event);
}

variable_types: mask int

variable_types: operation int

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

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

user_defined_types: struct cevents { int maxfd; struct cevent *events; struct cevent_fired *fired; struct clist *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; }

user_defined_types: struct epoll_priv { int epfd; struct epoll_event events[-1]; }

user_defined_types: struct clist { struct clist_item *head; unsigned long count; }


original_code:
static int cevents_poll_impl(cevents *cevts, msec_t ms) {
 epoll_priv *priv = (epoll_priv*)cevts->priv_data;
 int rs, i, mask, count = 0;
 struct epoll_event *ep_event;
 cevent_fired *fired;
 rs = epoll_wait(priv->epfd, priv->events, (10240*20), ms);
 if(rs > 0) {
  for(i = 0; i < rs; i++) {
   mask = 0x0;
   ep_event = priv->events + i;
   fired = cevts->fired + count;
   if (ep_event->events & EPOLLIN)
    mask |= 0x1;
   if (ep_event->events & EPOLLOUT)
    mask |= 0x1<<1;
   fired->fd = ep_event->data.fd;
   fired->mask = mask;
   count++;
  }
 }
 return count;
}

canonical_original_code:
static int cevents_poll_impl(struct cevents *cevts, unsigned long ms) {
 struct epoll_priv *priv = (epoll_priv*)cevts->priv_data;
 int rs; int i; int mask; int count = 0;
 struct epoll_event *ep_event;
 struct cevent_fired *fired;
 rs = epoll_wait(priv->epfd, priv->events, (10240*20), ms);
 if(rs > 0) {
  for(i = 0; i < rs; i++) {
   mask = 0x0;
   ep_event = priv->events + i;
   fired = cevts->fired + count;
   if (ep_event->events & EPOLLIN)
    mask |= 0x1;
   if (ep_event->events & EPOLLOUT)
    mask |= 0x1<<1;
   fired->fd = ep_event->data.fd;
   fired->mask = mask;
   count++;
  }
 }
 return count;
}

variable_types: struct cevents { int maxfd; struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *

variable_types:
ms
unsigned long

variable_types: rs int

variable_types: count int

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


decompiled_code:
__int64 __fastcall cevents_pop_fired(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r8
__int64 result; // rax

spinlock_lock(a1 + 32);
clist_rpop(*(_QWORD *)(a1 + 24));
spinlock_unlock((_DWORD *)(a1 + 32));
v3 = v2;
result = 0LL;
if ( v3 )
{
*a2 = *v3;
jfree(v3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r8
__int64 result; // rax

spinlock_lock(a1 + 32);
clist_rpop(*(_QWORD *)(a1 + 24));
func0((_DWORD *)(a1 + 32));
v3 = v2;
result = 0LL;
if ( v3 )
{
*a2 = *v3;
jfree(v3);
result = 1LL;
}
return result;
}

original_code:
int cevents_pop_fired(cevents *cevts, cevent_fired *fired) {
 cevent_fired *fevt;
 spinlock_lock(&cevts->qlock);;
 fevt = (cevent_fired*)clist_rpop(cevts->fired_queue);
 spinlock_unlock(&cevts->qlock);;
 if(fevt == ((void *)0)) return 0;
 *fired = *fevt;
 jfree(fevt);
 return 1;
}

canonical_original_code:
int cevents_pop_fired(struct cevents *cevts, struct cevent_fired *fired) {
 struct cevent_fired *fevt;
 spinlock_lock(&cevts->qlock);;
 fevt = (struct cevent_fired *)clist_rpop(cevts->fired_queue);
 spinlock_unlock(&cevts->qlock);;
 if(fevt == ((void *)0)) return 0;
 *fired = *fevt;
 jfree(fevt);
 return 1;
}

variable_types:
fired
struct cevent_fired *

variable_types: struct cevent_fired { int mask; int fd; } *


decompiled_code:
__int64 cevents_create()
{
__int64 v0; // r12
__int64 v1; // rax
__int64 result; // rax

v0 = jmalloc(128LL);
*(_QWORD *)(v0 + 8) = jmalloc(6553600LL);
*(_QWORD *)(v0 + 16) = jmalloc(1638400LL);
v1 = clist_create();
*(_QWORD *)(v0 + 32) = 0LL;
*(_QWORD *)(v0 + 24) = v1;
cevents_create_priv_impl_constprop_0_isra_0(v0);
result = v0;
*(_QWORD *)(v0 + 40) = 0LL;
*(_DWORD *)(v0 + 48) = 0;
return result;
}

original_code:
cevents *cevents_create() {
 cevents *evts;
 int len;
 len = sizeof(cevents);
 evts = (cevents *)jmalloc(len);
 memset((void *)evts, len, 0);
 evts->events = jmalloc(sizeof(cevent) * (10240*20));
 evts->fired = jmalloc(sizeof(cevent_fired) * (10240*20));
 evts->fired_queue = clist_create();
 (*&evts->qlock)=0;
 (*&evts->lock)=0;
 cevents_create_priv_impl(evts);
 evts->poll_sec = 0;
 evts->poll_ms = 0;
 return evts;
}

canonical_original_code:
struct cevents *cevents_create() {
 struct cevents *evts;
 int len;
 len = sizeof(cevents);
 evts = (struct cevents *)jmalloc(len);
 memset((void *)evts, len, 0);
 evts->events = jmalloc(sizeof(cevent) * (10240*20));
 evts->fired = jmalloc(sizeof(cevent_fired) * (10240*20));
 evts->fired_queue = clist_create();
 (*&evts->qlock)=0;
 (*&evts->lock)=0;
 cevents_create_priv_impl(evts);
 evts->poll_sec = 0;
 evts->poll_ms = 0;
 return evts;
}

variable_types: len int

return_type: struct cevents { int maxfd; struct cevent { int mask; int (*read_proc)(struct cevents *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *


decompiled_code:
__int64 __fastcall cevents_destroy(_QWORD *a1)
{
__int64 v2; // rdi
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = a1[1];
if ( v2 )
jfree(v2);
v3 = a1[2];
if ( v3 )
jfree(v3);
if ( a1[3] )
clist_destroy();
a1[1] = 0LL;
a1[2] = 0LL;
a1[3] = 0LL;
a1[4] = 0LL;
cevents_destroy_priv_impl_isra_0((__int64)a1);
result = jfree(a1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1)
{
__int64 v2; // rdi
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = a1[1];
if ( v2 )
jfree(v2);
v3 = a1[2];
if ( v3 )
jfree(v3);
if ( a1[3] )
clist_destroy();
a1[1] = 0LL;
a1[2] = 0LL;
a1[3] = 0LL;
a1[4] = 0LL;
func5((__int64)a1);
result = jfree(a1);
}
return result;
}

original_code:
void cevents_destroy(cevents *cevts) {
 if(cevts == ((void *)0))
  return;
 if(cevts->events != ((void *)0))
  jfree(cevts->events);
 if(cevts->fired != ((void *)0))
  jfree(cevts->fired);
 if(cevts->fired_queue != ((void *)0))
  clist_destroy(cevts->fired_queue);
 (*&cevts->lock)=0;
 (*&cevts->qlock)=0;
 cevts->events = ((void *)0);
 cevts->fired = ((void *)0);
 cevts->fired_queue = ((void *)0);
 cevents_destroy_priv_impl(cevts);
 jfree(cevts);
}

canonical_original_code:
void cevents_destroy(struct cevents *cevts) {
 if(cevts == ((void *)0))
  return;
 if(cevts->events != ((void *)0))
  jfree(cevts->events);
 if(cevts->fired != ((void *)0))
  jfree(cevts->fired);
 if(cevts->fired_queue != ((void *)0))
  clist_destroy(cevts->fired_queue);
 (*&cevts->lock)=0;
 (*&cevts->qlock)=0;
 cevts->events = ((void *)0);
 cevts->fired = ((void *)0);
 cevts->fired_queue = ((void *)0);
 cevents_destroy_priv_impl(cevts);
 jfree(cevts);
}


decompiled_code:
__int64 __fastcall cevents_add_event_inner(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
__int64 v8; // rbp
int v9; // eax
int v10; // ebx
unsigned int v11; // er8
int *v13; // rax
char *v14; // rax
int v15; // [rsp+0h] [rbp-3Ch]

if ( a2 > 204800 )
return 0xFFFFFFFFLL;
v8 = *(_QWORD *)(a1 + 8) + 32LL * a2;
v9 = cevents_add_event_impl(a1, a2, a3);
if ( v9 < 0 )
{
v15 = v9;
v13 = _errno_location();
v14 = strerror(*v13);
log_print(4LL, "fd %d add event error:%s\n", (unsigned int)a2, v14);
v11 = v15;
}
else
{
if ( (a3 & 1) != 0 )
*(_QWORD *)(v8 + 8) = a4;
if ( (a3 & 2) != 0 )
*(_QWORD *)(v8 + 16) = a4;
v10 = *(_DWORD *)v8 | a3;
*(_DWORD *)v8 = v10;
if ( *(_DWORD *)a1 < a2 )
{
if ( v10 )
*(_DWORD *)a1 = a2;
}
*(_QWORD *)(v8 + 24) = a5;
v11 = 0;
}
return v11;
}

original_code:
int cevents_add_event_inner(cevents *cevts, int fd, int mask, event_proc *proc, void *priv) {
 int rs;
 cevent *evt;
 if(fd > (10240*20))
  return -1;
 evt = cevts->events + fd;
 if((rs = cevents_add_event_impl(cevts, fd, mask)) < 0) {
  log_print(LEVEL_ERR, "fd %d add event error:%s\n", fd, strerror((*__errno_location ())));
  return rs;
 }
 if(mask & 0x1) evt->read_proc = proc;
 if(mask & 0x1<<1) evt->write_proc = proc;
 evt->mask |= mask;
 if(fd > cevts->maxfd && evt->mask != 0x0)
  cevts->maxfd = fd;
 evt->priv = priv;
 return 0;
}

canonical_original_code:
int cevents_add_event_inner(struct cevents *cevts, int fd, int mask, int (*proc)(struct cevents *evts, int fd, void *priv, int mask), void *priv) {
 int rs;
 struct cevent *evt;
 if(fd > (10240*20))
  return -1;
 evt = cevts->events + fd;
 if((rs = cevents_add_event_impl(cevts, fd, mask)) < 0) {
  log_print(LEVEL_ERR, "fd %d add event error:%s\n", fd, strerror((*__errno_location ())));
  return rs;
 }
 if(mask & 0x1) evt->read_proc = proc;
 if(mask & 0x1<<1) evt->write_proc = proc;
 evt->mask |= mask;
 if(fd > cevts->maxfd && evt->mask != 0x0)
  cevts->maxfd = fd;
 evt->priv = priv;
 return 0;
}

variable_types: fd int

variable_types: int (*)(struct cevents *evts, int fd, void *priv, int mask)

variable_types:
priv
void *

variable_types: struct cevent { int mask; int (*read_proc)(struct cevents { int maxfd; struct cevent *events; struct cevent_fired { int mask; int fd; } *fired; struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *fired_queue; unsigned int qlock; unsigned int lock; long poll_sec; int poll_ms; char impl_name[64]; void *priv_data; } *evts, int fd, void *priv, int mask); int (*write_proc)(struct cevents *evts, int fd, void *priv, int mask); void *priv; } *


decompiled_code:
void __fastcall cevents_add_event(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
spinlock_lock(a1 + 36);
cevents_add_event_inner(a1, a2, a3, a4, a5);
spinlock_unlock((_DWORD *)(a1 + 36));
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
spinlock_lock(a1 + 36);
func10(a1, a2, a3, a4, a5);
func0((_DWORD *)(a1 + 36));
}

original_code:
int cevents_add_event(cevents *cevts, int fd, int mask, event_proc *proc, void *priv) {
 int rs;
 spinlock_lock(&cevts->lock);;
 rs = cevents_add_event_inner(cevts, fd, mask, proc, priv);
 spinlock_unlock(&cevts->lock);;
 return rs;
}

canonical_original_code:
int cevents_add_event(struct cevents *cevts, int fd, int mask, int (*proc)(struct cevents *evts, int fd, void *priv, int mask), void *priv) {
 int rs;
 spinlock_lock(&cevts->lock);;
 rs = cevents_add_event_inner(cevts, fd, mask, proc, priv);
 spinlock_unlock(&cevts->lock);;
 return rs;
}


original_code:
int cevents_del_event_inner(cevents *cevts, int fd, int mask) {
 int j;
 if(fd > (10240*20))
  return -1;
 cevent *evt = cevts->events + fd;
 if (evt->mask == 0x0) return 0;
 if(cevents_del_event_impl(cevts, fd, mask) < 0) {
  log_print(LEVEL_ERR, "del event error:%s\n", strerror((*__errno_location ())));
 }
 evt->mask &= ~mask;
 if(cevts->maxfd == fd && evt->mask == 0x0) {
  for(j = cevts->maxfd - 1; j >= 0; j--) {
   if(cevts->events[j].mask != 0x0) {
    cevts->maxfd = j;
    break;
   }
  }
 }
 return 0;
}

canonical_original_code:
int cevents_del_event_inner(struct cevents *cevts, int fd, int mask) {
 int j;
 if(fd > (10240*20))
  return -1;
 struct cevent *evt = cevts->events + fd;
 if (evt->mask == 0x0) return 0;
 if(cevents_del_event_impl(cevts, fd, mask) < 0) {
  log_print(LEVEL_ERR, "del event error:%s\n", strerror((*__errno_location ())));
 }
 evt->mask &= ~mask;
 if(cevts->maxfd == fd && evt->mask == 0x0) {
  for(j = cevts->maxfd - 1; j >= 0; j--) {
   if(cevts->events[j].mask != 0x0) {
    cevts->maxfd = j;
    break;
   }
  }
 }
 return 0;
}

variable_types: j int


decompiled_code:
void __fastcall cevents_del_event(int *a1, int a2, int a3)
{
spinlock_lock(a1 + 9);
cevents_del_event_inner(a1, a2, a3);
spinlock_unlock(a1 + 9);
}

canonical_decompiled_code:
void __fastcall func13(int *a1, int a2, int a3)
{
spinlock_lock(a1 + 9);
func12(a1, a2, a3);
func0(a1 + 9);
}

original_code:
int cevents_del_event(cevents *cevts, int fd, int mask) {
 int rs;
 spinlock_lock(&cevts->lock);;
 rs = cevents_del_event_inner(cevts, fd, mask);
 spinlock_unlock(&cevts->lock);;
 return rs;
}

canonical_original_code:
int cevents_del_event(struct cevents *cevts, int fd, int mask) {
 int rs;
 spinlock_lock(&cevts->lock);;
 rs = cevents_del_event_inner(cevts, fd, mask);
 spinlock_unlock(&cevts->lock);;
 return rs;
}


decompiled_code:
__int64 __fastcall cevents_poll(__int64 a1, int a2)
{
unsigned int v2; // er13
int v3; // eax
__int64 v4; // r12
__int64 v5; // r12
__int64 v6; // r14
int *v7; // r15
__int64 v8; // rsi
__int64 v9; // rbx
int v10; // edx
int v11; // ecx
_QWORD *v13; // rax
_QWORD *v14; // rax

if ( !a1 )
{
log_print(4LL, "can't be happend\n");
abort();
}
spinlock_lock(a1 + 36);
cevents_poll_impl(a1, a2);
v2 = 0;
spinlock_unlock((_DWORD *)(a1 + 36));
v4 = v3;
time_now(a1 + 40, a1 + 48);
if ( (int)v4 > 0 )
{
v5 = 8 * v4;
v6 = 0LL;
while ( 1 )
{
v7 = (int *)(v6 + *(_QWORD *)(a1 + 16));
v8 = v7[1];
v9 = *(_QWORD *)(a1 + 8) + 32 * v8;
if ( !*(_DWORD *)v9 )
goto LABEL_7;
v10 = *v7;
if ( (*(_DWORD *)v9 & 4) == 0 )
{
++v2;
cevents_del_event((int *)a1, v8, v10);
v13 = clone_cevent_fired_constprop_0(v7);
cevents_push_fired(a1, (__int64)v13);
goto LABEL_7;
}
LOBYTE(v11) = v10 | 4;
*v7 = v10 | 4;
if ( (v10 & 1) != 0 )
{
if ( !(*(unsigned int (__fastcall **)(__int64, __int64, _QWORD))(v9 + 8))(a1, v8, *(_QWORD *)(v9 + 24)) )
{
++v2;
cevents_del_event((int *)a1, v7[1], 1);
v14 = clone_cevent_fired_constprop_0(v7);
cevents_push_fired(a1, (__int64)v14);
}
if ( !*(_DWORD *)v9 )
goto LABEL_7;
v11 = *v7;
}
if ( (v11 & 2) != 0 )
{
v6 += 8LL;
(*(void (__fastcall **)(__int64, _QWORD, _QWORD))(v9 + 16))(a1, (unsigned int)v7[1], *(_QWORD *)(v9 + 24));
if ( v5 == v6 )
break;
}
else
{
LABEL_7:
v6 += 8LL;
if ( v5 == v6 )
break;
}
}
}
usleep(2u);
return v2;
}

original_code:
int cevents_poll(cevents *cevts, msec_t ms) {
 int rs, i, count = 0, flag = 0;
 cevent_fired *fired;
 cevent *evt;
 if(cevts == ((void *)0)) {
  log_print(LEVEL_ERR, "can't be happend\n");
  abort();
 }
 spinlock_lock(&cevts->lock);;
 rs = cevents_poll_impl(cevts, ms);
 spinlock_unlock(&cevts->lock);;
 time_now(&cevts->poll_sec, &cevts->poll_ms);
 if(rs > 0) {
  for(i = 0; i < rs; i++) {
   fired = cevts->fired + i;
   evt = cevts->events + fired->fd;
   if(!evt->mask)
    continue;
   if(evt->mask & 0x1<<2) {
    fired->mask |= 0x1<<2;
    if(evt->mask && (fired->mask & 0x1)) {
     if(evt->read_proc(cevts, fired->fd, evt->priv, fired->mask) == 0) {
      cevents_del_event(cevts, fired->fd, 0x1);
      cevents_push_fired(cevts, clone_cevent_fired(cevts, fired));
      count++;
     }
    }
    if(evt->mask && (fired->mask & 0x1<<1)) {
     evt->write_proc(cevts, fired->fd, evt->priv, fired->mask);
    }
   } else {
    cevents_del_event(cevts, fired->fd, fired->mask);
    cevents_push_fired(cevts, clone_cevent_fired(cevts, fired));
    count++;
   }
  }
 }
 usleep(2);
 return count;
}

canonical_original_code:
int cevents_poll(struct cevents *cevts, unsigned long ms) {
 int rs; int i; int count = 0; int flag = 0;
 struct cevent_fired *fired;
 struct cevent *evt;
 if(cevts == ((void *)0)) {
  log_print(LEVEL_ERR, "can't be happend\n");
  abort();
 }
 spinlock_lock(&cevts->lock);;
 rs = cevents_poll_impl(cevts, ms);
 spinlock_unlock(&cevts->lock);;
 time_now(&cevts->poll_sec, &cevts->poll_ms);
 if(rs > 0) {
  for(i = 0; i < rs; i++) {
   fired = cevts->fired + i;
   evt = cevts->events + fired->fd;
   if(!evt->mask)
    continue;
   if(evt->mask & 0x1<<2) {
    fired->mask |= 0x1<<2;
    if(evt->mask && (fired->mask & 0x1)) {
     if(evt->read_proc(cevts, fired->fd, evt->priv, fired->mask) == 0) {
      cevents_del_event(cevts, fired->fd, 0x1);
      cevents_push_fired(cevts, clone_cevent_fired(cevts, fired));
      count++;
     }
    }
    if(evt->mask && (fired->mask & 0x1<<1)) {
     evt->write_proc(cevts, fired->fd, evt->priv, fired->mask);
    }
   } else {
    cevents_del_event(cevts, fired->fd, fired->mask);
    cevents_push_fired(cevts, clone_cevent_fired(cevts, fired));
    count++;
   }
  }
 }
 usleep(2);
 return count;
}

variable_types: i int

variable_types: flag int


decompiled_code:
int cnet_fmt_err(char *a1, size_t a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
return vsnprintf(a1, a2, a3, arg);
}

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

va_start(arg, a3);
return vsnprintf(a1, a2, a3, arg);
}

original_code:
static void cnet_fmt_err(char *err, size_t len, const char *fmt, ...) {
 va_list list;
 __builtin_va_start(list,fmt);
 vsnprintf(err, len, fmt, list);
 __builtin_va_end(list);
}

canonical_original_code:
static void cnet_fmt_err(char *err, unsigned long len, char *fmt, ...) {
 __builtin_va_list list;
 __builtin_va_start(list,fmt);
 vsnprintf(err, len, fmt, list);
 __builtin_va_end(list);
}

variable_types:
err
char *

variable_types:
len
unsigned long

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall cnet_accept_impl(int fd, struct sockaddr *addr, socklen_t *addr_len, char *a4, size_t a5)
{
int v9; // edi
unsigned int v10; // eax
unsigned int v11; // er12
char *v13; // rax

do
{
v10 = accept(fd, addr, addr_len);
v11 = v10;
if ( v10 != -1 )
{
cio_set_noblock(v10);
return v11;
}
v9 = *_errno_location();
}
while ( v9 == 4 );
v13 = strerror(v9);
cnet_fmt_err(a4, a5, "accept error: %s\n", v13);
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, struct sockaddr *addr, socklen_t *addr_len, char *a4, size_t a5)
{
int v9; // edi
unsigned int v10; // eax
unsigned int v11; // er12
char *v13; // rax

do
{
v10 = accept(fd, addr, addr_len);
v11 = v10;
if ( v10 != -1 )
{
cio_set_noblock(v10);
return v11;
}
v9 = *_errno_location();
}
while ( v9 == 4 );
v13 = strerror(v9);
func0(a4, a5, "accept error: %s\n", v13);
return v11;
}

original_code:
static int cnet_accept_impl(int fd, struct sockaddr *sa, socklen_t *len,char *ebuf, size_t ebuflen) {
 int clifd;
 while(1) {
  clifd = accept(fd, sa, len);
  if(clifd == -1) {
   if((*__errno_location ()) == 4)
    continue;
   else {
    cnet_fmt_err(ebuf, ebuflen, "accept error: %s\n", strerror((*__errno_location ())));
    return -1;
   }
  }
  cio_set_noblock(clifd);
  return clifd;
 }
}

canonical_original_code:
static int cnet_accept_impl(int fd, struct sockaddr *sa, unsigned int *len, char *ebuf, unsigned long ebuflen) {
 int clifd;
 while(1) {
  clifd = accept(fd, sa, len);
  if(clifd == -1) {
   if((*__errno_location ()) == 4)
    continue;
   else {
    cnet_fmt_err(ebuf, ebuflen, "accept error: %s\n", strerror((*__errno_location ())));
    return -1;
   }
  }
  cio_set_noblock(clifd);
  return clifd;
 }
}

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

variable_types:
len
unsigned int *

variable_types:
ebuf
char *

variable_types:
ebuflen
unsigned long

variable_types: clifd int


decompiled_code:
__int64 __fastcall cnet_bind_listen(int fd, const struct sockaddr *a2, socklen_t a3, char *a4, size_t a5)
{
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax

if ( bind(fd, a2, a3) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
cnet_fmt_err(a4, a5, "bind error: %s\n", v9);
}
else
{
if ( listen(fd, 511) >= 0 )
return 0LL;
v10 = _errno_location();
v11 = strerror(*v10);
cnet_fmt_err(a4, a5, "listen error: %s\n", v11);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, const struct sockaddr *a2, socklen_t a3, char *a4, size_t a5)
{
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax

if ( bind(fd, a2, a3) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
func0(a4, a5, "bind error: %s\n", v9);
}
else
{
if ( listen(fd, 511) >= 0 )
return 0LL;
v10 = _errno_location();
v11 = strerror(*v10);
func0(a4, a5, "listen error: %s\n", v11);
}
return 0xFFFFFFFFLL;
}

original_code:
static int cnet_bind_listen(int fd, struct sockaddr *sa, socklen_t slen, char *ebuf, size_t ebuflen) {
 int ret;
 if(bind(fd, sa, slen) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "bind error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if(listen(fd, 511) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "listen error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 return 0;
}

canonical_original_code:
static int cnet_bind_listen(int fd, struct sockaddr *sa, unsigned int slen, char *ebuf, unsigned long ebuflen) {
 int ret;
 if(bind(fd, sa, slen) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "bind error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if(listen(fd, 511) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "listen error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 return 0;
}

variable_types:
slen
unsigned int

variable_types: ret int

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


decompiled_code:
__int64 __fastcall cnet_tcp_accept(int a1, char *a2, _DWORD *a3, char *a4, size_t a5)
{
unsigned int v6; // er12
char *v7; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
v6 = cnet_accept_impl(a1, &addr, &addr_len, a4, a5);
if ( a3 )
*a3 = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
if ( a2 )
{
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(a2, v7);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, char *a2, _DWORD *a3, char *a4, size_t a5)
{
unsigned int v6; // er12
char *v7; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
v6 = func1(a1, &addr, &addr_len, a4, a5);
if ( a3 )
*a3 = (unsigned __int16)func2(*(__int16 *)addr.sa_data);
if ( a2 )
{
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(a2, v7);
}
return v6;
}

original_code:
int cnet_tcp_accept(int fd, char *ip, int *port, char *ebuf, size_t ebuflen) {
 struct sockaddr_in sa;
 socklen_t slen = sizeof(sa);
 int ret;
 ret = cnet_accept_impl(fd, (struct sockaddr*)&sa, &slen, ebuf, ebuflen);
 if (port) *port = ntohs(sa.sin_port);
 if (ip) strcpy(ip,inet_ntoa(sa.sin_addr));
 return ret;
}

canonical_original_code:
int cnet_tcp_accept(int fd, char *ip, int *port, char *ebuf, unsigned long ebuflen) {
 struct sockaddr_in sa;
 unsigned int slen = sizeof(sa);
 int ret;
 ret = cnet_accept_impl(fd, (struct sockaddr *)&sa, &slen, ebuf, ebuflen);
 if (port) *port = ntohs(sa.sin_port);
 if (ip) strcpy(ip,inet_ntoa(sa.sin_addr));
 return ret;
}

variable_types: fd int

variable_types:
ip
char *

variable_types:
port
int *

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

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


decompiled_code:
__int64 __fastcall cnet_unix_accept(int a1, char *a2, size_t a3)
{
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
return cnet_accept_impl(a1, &addr, &addr_len, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, char *a2, size_t a3)
{
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
return func1(a1, &addr, &addr_len, a2, a3);
}

original_code:
int cnet_unix_accept(int fd, char *ebuf, size_t ebuflen) {
 struct sockaddr_un sa;
 socklen_t slen = sizeof(sa);
 return cnet_accept_impl(fd, (struct sockaddr*)&sa, &slen, ebuf, ebuflen);
}

canonical_original_code:
int cnet_unix_accept(int fd, char *ebuf, unsigned long ebuflen) {
 struct sockaddr_un sa;
 unsigned int slen = sizeof(sa);
 return cnet_accept_impl(fd, (struct sockaddr *)&sa, &slen, ebuf, ebuflen);
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall cnet_create_sock(int a1, int a2, char *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // er12
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // edi
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v6 = socket(a1, a2, 0);
if ( v6 < 0 )
{
v9 = _errno_location();
v7 = -1;
v10 = strerror(*v9);
cnet_fmt_err(a3, a4, "socket create error: %s\n", v10);
}
else
{
v7 = v6;
if ( setsockopt(v6, 1, 2, optval, 4u) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
cnet_fmt_err(a3, a4, "socket set reuse error: %s\n", v12);
v13 = v7;
v7 = -1;
close(v13);
}
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2, char *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // er12
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // edi
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v6 = socket(a1, a2, 0);
if ( v6 < 0 )
{
v9 = _errno_location();
v7 = -1;
v10 = strerror(*v9);
func0(a3, a4, "socket create error: %s\n", v10);
}
else
{
v7 = v6;
if ( setsockopt(v6, 1, 2, optval, 4u) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
func0(a3, a4, "socket set reuse error: %s\n", v12);
v13 = v7;
v7 = -1;
close(v13);
}
}
return v7;
}

original_code:
int cnet_create_sock(int domain, int type, char *ebuf, size_t ebuflen) {
 int fd, on = 1;
 fd = socket(domain, type, 0);
 if(fd < 0) {
  cnet_fmt_err(ebuf, ebuflen, "socket create error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if(setsockopt(fd, 1, 2, &on, sizeof(on)) == -1) {
  cnet_fmt_err(ebuf, ebuflen, "socket set reuse error: %s\n", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int cnet_create_sock(int domain, int type, char *ebuf, unsigned long ebuflen) {
 int fd; int on = 1;
 fd = socket(domain, type, 0);
 if(fd < 0) {
  cnet_fmt_err(ebuf, ebuflen, "socket create error: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if(setsockopt(fd, 1, 2, &on, sizeof(on)) == -1) {
  cnet_fmt_err(ebuf, ebuflen, "socket set reuse error: %s\n", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: domain int

variable_types: type int

variable_types: on int


decompiled_code:
__int64 __fastcall cnet_tcp_server(__int64 a1, __int16 a2, char *a3, size_t a4)
{
const char *v6; // r8
int v7; // eax
unsigned int v8; // er12
int *v10; // rax
char *v11; // rax
int v12; // edi
struct sockaddr v13[3]; // [rsp+0h] [rbp-38h] BYREF

v13[0].sa_family = 2;
*(_WORD *)v13[0].sa_data = _bswap_16(a2);
if ( inet_aton(v6, (struct in_addr *)&v13[0].sa_data[2]) )
{
v7 = cnet_create_sock(2, 1, a3, a4);
v8 = v7;
if ( v7 >= 0 && (int)cnet_bind_listen(v7, v13, 0x10u, a3, a4) < 0 )
{
v12 = v8;
v8 = -1;
close(v12);
}
}
else
{
v10 = _errno_location();
v8 = -1;
v11 = strerror(*v10);
cnet_fmt_err(a3, a4, "invalidate addr: %s\n", v11);
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int16 a2, char *a3, size_t a4)
{
const char *v6; // r8
int v7; // eax
unsigned int v8; // er12
int *v10; // rax
char *v11; // rax
int v12; // edi
struct sockaddr v13[3]; // [rsp+0h] [rbp-38h] BYREF

v13[0].sa_family = 2;
*(_WORD *)v13[0].sa_data = func2(a2);
if ( inet_aton(v6, (struct in_addr *)&v13[0].sa_data[2]) )
{
v7 = func6(2, 1, a3, a4);
v8 = v7;
if ( v7 >= 0 && (int)func3(v7, v13, 0x10u, a3, a4) < 0 )
{
v12 = v8;
v8 = -1;
close(v12);
}
}
else
{
v10 = _errno_location();
v8 = -1;
v11 = strerror(*v10);
func0(a3, a4, "invalidate addr: %s\n", v11);
}
return v8;
}

original_code:
int cnet_tcp_server(char *ip, int port, char *ebuf, size_t ebuflen) {
 int fd;
 struct sockaddr_in sa;
 memset(&sa, sizeof(sa), 0);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if(!inet_aton(ip, &sa.sin_addr)) {
  cnet_fmt_err(ebuf, ebuflen, "invalidate addr: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if((fd = cnet_create_sock(2, SOCK_STREAM, ebuf, ebuflen)) < 0)
  return fd;
 if(cnet_bind_listen(fd, (struct sockaddr*)&sa, sizeof(sa), ebuf, ebuflen) < 0) {
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int cnet_tcp_server(char *ip, int port, char *ebuf, unsigned long ebuflen) {
 int fd;
 struct sockaddr_in sa;
 memset(&sa, sizeof(sa), 0);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if(!inet_aton(ip, &sa.sin_addr)) {
  cnet_fmt_err(ebuf, ebuflen, "invalidate addr: %s\n", strerror((*__errno_location ())));
  return -1;
 }
 if((fd = cnet_create_sock(2, SOCK_STREAM, ebuf, ebuflen)) < 0)
  return fd;
 if(cnet_bind_listen(fd, (struct sockaddr *)&sa, sizeof(sa), ebuf, ebuflen) < 0) {
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: port int


decompiled_code:
__int64 __fastcall cnet_unix_server(char *src, __mode_t mode, char *a3, size_t a4)
{
int v6; // er12
int *v8; // rax
char *v9; // rax
int v10; // edi
struct sockaddr v11[10]; // [rsp+0h] [rbp-A8h] BYREF

v6 = cnet_create_sock(1, 1, a3, a4);
if ( v6 >= 0 )
{
v11[0].sa_family = 1;
strncpy(v11[0].sa_data, src, 0x6BuLL);
if ( (int)cnet_bind_listen(v6, v11, 0x6Eu, a3, a4) >= 0 )
{
if ( fchmod(v6, mode) >= 0 )
return (unsigned int)v6;
v8 = _errno_location();
v9 = strerror(*v8);
cnet_fmt_err(a3, a4, "socket set reuse error: %s\n", v9);
}
v10 = v6;
v6 = -1;
close(v10);
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *src, __mode_t mode, char *a3, size_t a4)
{
int v6; // er12
int *v8; // rax
char *v9; // rax
int v10; // edi
struct sockaddr v11[10]; // [rsp+0h] [rbp-A8h] BYREF

v6 = func6(1, 1, a3, a4);
if ( v6 >= 0 )
{
v11[0].sa_family = 1;
strncpy(v11[0].sa_data, src, 0x6BuLL);
if ( (int)func3(v6, v11, 0x6Eu, a3, a4) >= 0 )
{
if ( fchmod(v6, mode) >= 0 )
return (unsigned int)v6;
v8 = _errno_location();
v9 = strerror(*v8);
func0(a3, a4, "socket set reuse error: %s\n", v9);
}
v10 = v6;
v6 = -1;
close(v10);
}
return (unsigned int)v6;
}

original_code:
int cnet_unix_server(char *path, mode_t mode,char *ebuf, size_t ebuflen) {
 int fd;
 struct sockaddr_un sa;
 memset(&sa, sizeof(sa), 0);
 if((fd = cnet_create_sock(1, SOCK_STREAM, ebuf, ebuflen)) < 0)
  return fd;
 sa.sun_family = 1;
 strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
 if(cnet_bind_listen(fd, (struct sockaddr*)&sa, sizeof(sa), ebuf, ebuflen) < 0) {
  close(fd);
  return -1;
 }
 if(fchmod(fd, mode) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "socket set reuse error: %s\n", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int cnet_unix_server(char *path, unsigned int mode, char *ebuf, unsigned long ebuflen) {
 int fd;
 struct sockaddr_un sa;
 memset(&sa, sizeof(sa), 0);
 if((fd = cnet_create_sock(1, SOCK_STREAM, ebuf, ebuflen)) < 0)
  return fd;
 sa.sun_family = 1;
 strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
 if(cnet_bind_listen(fd, (struct sockaddr *)&sa, sizeof(sa), ebuf, ebuflen) < 0) {
  close(fd);
  return -1;
 }
 if(fchmod(fd, mode) < 0) {
  cnet_fmt_err(ebuf, ebuflen, "socket set reuse error: %s\n", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

variable_types:
path
char *

variable_types:
mode
unsigned int

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


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

v2[0] = 1;
return ioctl(a1, 0x5421uLL, v2);
}

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

v2[0] = 1;
return ioctl(a1, 0x5421uLL, v2);
}

original_code:
int cio_set_noblock(int fd) {
 int val;
 val = 1;
 return ioctl(fd, 0x5421, &val);
}

variable_types: val int


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

v2[0] = 0;
return ioctl(a1, 0x5421uLL, v2);
}

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

v2[0] = 0;
return ioctl(a1, 0x5421uLL, v2);
}

original_code:
int cio_set_block(int fd) {
 int val;
 val = 0;
 return ioctl(fd, 0x5421, &val);
}


decompiled_code:
__int64 __fastcall cio_write(int fd, void *buf, unsigned __int64 a3)
{
unsigned int v5; // er12
__int64 v6; // rax
__int64 v7; // rdx
int v8; // eax

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v8 = write(fd, buf, a3 - v6);
if ( !v8 )
break;
if ( v8 == -1 )
return 0xFFFFFFFFLL;
v5 += v8;
v7 = v8;
v6 = (int)v5;
buf = (char *)buf + v7;
}
while ( (int)v5 < a3 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, unsigned __int64 a3)
{
unsigned int v5; // er12
__int64 v6; // rax
__int64 v7; // rdx
int v8; // eax

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v8 = write(fd, buf, a3 - v6);
if ( !v8 )
break;
if ( v8 == -1 )
return 0xFFFFFFFFLL;
v5 += v8;
v7 = v8;
v6 = (int)v5;
buf = (char *)buf + v7;
}
while ( (int)v5 < a3 );
return v5;
}

original_code:
int cio_write(int fd, char *ptr, size_t len) {
 int count = 0;
 int wn = 0;
 while(count < len ) {
  wn = write(fd, ptr, len - count);
  if(wn == 0) return count;
  if(wn == -1) return -1;
  ptr += wn;
  count += wn;
 }
 return count;
}

canonical_original_code:
int cio_write(int fd, char *ptr, unsigned long len) {
 int count = 0;
 int wn = 0;
 while(count < len ) {
  wn = write(fd, ptr, len - count);
  if(wn == 0) return count;
  if(wn == -1) return -1;
  ptr += wn;
  count += wn;
 }
 return count;
}

variable_types:
ptr
char *

variable_types:
len
unsigned long

variable_types: count int

variable_types: wn int


decompiled_code:
__int64 __fastcall cio_read(int fd, void *buf, unsigned __int64 a3)
{
unsigned int v5; // er12
__int64 v6; // rax
__int64 v7; // rdx
int v8; // eax

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v8 = read(fd, buf, a3 - v6);
if ( !v8 )
break;
if ( v8 == -1 )
return 0xFFFFFFFFLL;
v5 += v8;
v7 = v8;
v6 = (int)v5;
buf = (char *)buf + v7;
}
while ( (int)v5 < a3 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, void *buf, unsigned __int64 a3)
{
unsigned int v5; // er12
__int64 v6; // rax
__int64 v7; // rdx
int v8; // eax

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v8 = read(fd, buf, a3 - v6);
if ( !v8 )
break;
if ( v8 == -1 )
return 0xFFFFFFFFLL;
v5 += v8;
v7 = v8;
v6 = (int)v5;
buf = (char *)buf + v7;
}
while ( (int)v5 < a3 );
return v5;
}

original_code:
int cio_read(int fd, char *ptr, size_t len) {
 int count = 0;
 int rn = 0;
 while(count < len ) {
  rn = read(fd, ptr, len - count);
  if(rn == 0) return count;
  if(rn == -1) return -1;
  ptr += rn;
  count += rn;
 }
 return count;
}

canonical_original_code:
int cio_read(int fd, char *ptr, unsigned long len) {
 int count = 0;
 int rn = 0;
 while(count < len ) {
  rn = read(fd, ptr, len - count);
  if(rn == 0) return count;
  if(rn == -1) return -1;
  ptr += rn;
  count += rn;
 }
 return count;
}

variable_types: rn int


original_code:
cio *cio_create() {
 cio *io = jmalloc(sizeof(cio));
 memset(io, 0, sizeof(cio));
 io->rbuf = cstr_create(1024);
 io->wbuf = cstr_create(1024);
 io->wcount = 0;
 io->priv = ((void *)0);
 io->flag = 0;
 io->argc = 0;
 io->tabidx = 0;
 io->bulk_len = 0;
 io->nbulk = 0;
 io->reqtype = 0x0;
 io->argv = ((void *)0);
 return io;
}

canonical_original_code:
struct cio *cio_create() {
 struct cio *io = jmalloc(sizeof(cio));
 memset(io, 0, sizeof(cio));
 io->rbuf = cstr_create(1024);
 io->wbuf = cstr_create(1024);
 io->wcount = 0;
 io->priv = ((void *)0);
 io->flag = 0;
 io->argc = 0;
 io->tabidx = 0;
 io->bulk_len = 0;
 io->nbulk = 0;
 io->reqtype = 0x0;
 io->argv = ((void *)0);
 return io;
}

variable_types: struct cio { int fd; int type; int mask; char *rbuf; char *wbuf; int wcount; char ip[128]; int nbulk; int bulk_len; int reqtype; int port; int flag; struct obj { int ref; int type; void *priv; unsigned int lock; } **argv; unsigned long argc; int tabidx; void *priv; void *thr_priv; } *

user_defined_types: struct cio { int fd; int type; int mask; char *rbuf; char *wbuf; int wcount; char ip[128]; int nbulk; int bulk_len; int reqtype; int port; int flag; struct obj **argv; unsigned long argc; int tabidx; void *priv; void *thr_priv; }


decompiled_code:
__int64 __fastcall cio_destroy(_QWORD *a1)
{
unsigned __int64 v2; // rbx
__int64 v3; // rdi

cstr_destroy(a1[2]);
cstr_destroy(a1[3]);
if ( a1[24] )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(a1[23] + 8 * v2++);
obj_decr(v3);
}
while ( a1[24] > v2 );
}
if ( a1[23] )
((void (*)(void))jfree)();
return jfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
unsigned __int64 v2; // rbx
__int64 v3; // rdi

cstr_destroy(a1[2]);
cstr_destroy(a1[3]);
if ( a1[24] )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(a1[23] + 8 * v2++);
obj_decr(v3);
}
while ( a1[24] > v2 );
}
if ( a1[23] )
((void (*)(void))jfree)();
return jfree(a1);
}

original_code:
void cio_destroy(cio *io) {
 size_t i;
 cstr_destroy(io->rbuf);
 cstr_destroy(io->wbuf);
 for(i = 0; i < io->argc; i++) {
  obj_decr(io->argv[i]);
 }
 if(io->argv != ((void *)0)) jfree(io->argv);
 jfree(io);
}

canonical_original_code:
void cio_destroy(struct cio *io) {
 unsigned long i;
 cstr_destroy(io->rbuf);
 cstr_destroy(io->wbuf);
 for(i = 0; i < io->argc; i++) {
  obj_decr(io->argv[i]);
 }
 if(io->argv != ((void *)0)) jfree(io->argv);
 jfree(io);
}

variable_types:
i
unsigned long

user_defined_types: struct obj { int ref; int type; void *priv; unsigned int lock; }


original_code:
void cio_clear(cio *io) {
 size_t i;
 cstr_clear(io->rbuf);
 cstr_clear(io->wbuf);
 for(i = 0; i < io->argc; i++) {
  obj_decr(io->argv[i]);
 }
 if(io->argv != ((void *)0)) jfree(io->argv);
 io->mask = 0;
 io->reqtype = 0x0;
 io->argc = 0;
 io->nbulk = 0;
 io->bulk_len = 0;
 io->argv = ((void *)0);
 io->flag = 0;
 io->wcount = 0;
}

canonical_original_code:
void cio_clear(struct cio *io) {
 unsigned long i;
 cstr_clear(io->rbuf);
 cstr_clear(io->wbuf);
 for(i = 0; i < io->argc; i++) {
  obj_decr(io->argv[i]);
 }
 if(io->argv != ((void *)0)) jfree(io->argv);
 io->mask = 0;
 io->reqtype = 0x0;
 io->argc = 0;
 io->nbulk = 0;
 io->bulk_len = 0;
 io->argv = ((void *)0);
 io->flag = 0;
 io->wcount = 0;
}


decompiled_code:
_QWORD *clist_item_create()
{
_QWORD *result; // rax

result = (_QWORD *)jmalloc(24LL);
if ( result )
{
result[1] = 0LL;
*result = 0LL;
result[2] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = (_QWORD *)jmalloc(24LL);
if ( result )
{
result[1] = 0LL;
*result = 0LL;
result[2] = 0LL;
}
return result;
}

original_code:
static clist_item *clist_item_create() {
 clist_item *l_item;
 size_t len = sizeof(struct clist_item);
 l_item = jmalloc(len);
 if(l_item == ((void *)0))
  return ((void *)0);
 l_item->next = ((void *)0);
 l_item->prev = ((void *)0);
 l_item->data = ((void *)0);
 return l_item;
}

canonical_original_code:
static struct clist_item *clist_item_create() {
 struct clist_item *l_item;
 unsigned long len = sizeof(struct clist_item);
 l_item = jmalloc(len);
 if(l_item == ((void *)0))
  return ((void *)0);
 l_item->next = ((void *)0);
 l_item->prev = ((void *)0);
 l_item->data = ((void *)0);
 return l_item;
}

variable_types: struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *


decompiled_code:
// attributes: thunk
__int64 clist_item_destroy()
{
return jfree();
}

canonical_decompiled_code:
__int64 func1()
{
return jfree();
}

original_code:
static void clist_item_destroy(clist_item *item) {
 jfree(item);
}

canonical_original_code:
static void clist_item_destroy(struct clist_item *item) {
 jfree(item);
}


decompiled_code:
_QWORD *clist_create()
{
_QWORD *result; // rax

result = (_QWORD *)jmalloc(16LL);
*result = 0LL;
result[1] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *result; // rax

result = (_QWORD *)jmalloc(16LL);
*result = 0LL;
result[1] = 0LL;
return result;
}

original_code:
clist *clist_create() {
 clist *cl;
 size_t len = sizeof(clist);
 cl = jmalloc(len);
 cl->head = ((void *)0);
 cl->count = 0;
 return cl;
}

canonical_original_code:
struct clist *clist_create() {
 struct clist *cl;
 unsigned long len = sizeof(clist);
 cl = jmalloc(len);
 cl->head = ((void *)0);
 cl->count = 0;
 return cl;
}

variable_types: struct clist { struct clist_item { struct clist_item *prev; struct clist_item *next; void *data; } *head; unsigned long count; } *


original_code:
void *clist_rpop(clist *cl) {
 void *data;
 clist_item *item;
 if(cl->count == 0) {
  return ((void *)0);
 }
 item = cl->head->prev;
 data = item->data;
 if(--cl->count) { item->prev->next = item->next; item->next->prev = item->prev; } else { cl->head = ((void *)0); } clist_item_destroy(item);
 return data;
}

canonical_original_code:
void *clist_rpop(struct clist *cl) {
 void *data;
 struct clist_item *item;
 if(cl->count == 0) {
  return ((void *)0);
 }
 item = cl->head->prev;
 data = item->data;
 if(--cl->count) { item->prev->next = item->next; item->next->prev = item->prev; } else { cl->head = ((void *)0); } clist_item_destroy(item);
 return data;
}

variable_types:
data
void *


original_code:
void *clist_lpop(clist *cl) {
 void *data;
 clist_item *item;
 if(cl->count == 0) {
  return ((void *)0);
 }
 item = cl->head;
 data = item->data;
 cl->head = item->next;
 if(--cl->count) { item->prev->next = item->next; item->next->prev = item->prev; } else { cl->head = ((void *)0); } clist_item_destroy(item);
 return data;
}

canonical_original_code:
void *clist_lpop(struct clist *cl) {
 void *data;
 struct clist_item *item;
 if(cl->count == 0) {
  return ((void *)0);
 }
 item = cl->head;
 data = item->data;
 cl->head = item->next;
 if(--cl->count) { item->prev->next = item->next; item->next->prev = item->prev; } else { cl->head = ((void *)0); } clist_item_destroy(item);
 return data;
}


decompiled_code:
__int64 __fastcall clist_walk_remove(__int64 a1, unsigned int (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
unsigned int v4; // er12
_QWORD *v6; // rbp
_QWORD *v7; // r15
__int64 v9; // rax
_QWORD *v10; // rdi

v4 = 0;
v6 = *(_QWORD **)a1;
LABEL_2:
if ( !v6 )
{
LABEL_7:
if ( *(_QWORD *)(a1 + 8) == v4 )
return v4;
}
while ( 1 )
{
if ( a2(v6[2], a3) )
{
v6 = (_QWORD *)v6[1];
goto LABEL_2;
}
v7 = (_QWORD *)v6[1];
if ( (*(_QWORD *)(a1 + 8))-- != 1LL )
{
v9 = *v6;
++v4;
v10 = v6;
*(_QWORD *)(v9 + 8) = v7;
*v7 = v9;
if ( v6 == v7 )
break;
goto LABEL_6;
}
*(_QWORD *)a1 = 0LL;
++v4;
v10 = v6;
if ( v6 == v7 )
break;
LABEL_6:
clist_item_destroy(v10);
v6 = v7;
if ( !v7 )
goto LABEL_7;
}
clist_item_destroy(v10);
return v4;
}

original_code:
int clist_walk_remove(clist *cl, int (*cb)(void *, void *priv), void *priv) {
 int count = 0;
 clist_item *item, *next;
 item = cl->head;
 while(item != ((void *)0) || cl->count != count) {
  if(!cb(item->data, priv)) {
   next = item->next;
   if(--cl->count) {
    item->prev->next = next;
    next->prev = item->prev;
   } else {
    cl->head = ((void *)0);
   }
   count++;
   if(next == item) {
    clist_item_destroy(item);
    break;
   }
   clist_item_destroy(item);
  } else {
   next = item->next;
  }
  item = next;
 }
 return count;
}

canonical_original_code:
int clist_walk_remove(struct clist *cl, int (*cb)(void *, void *priv), void *priv) {
 int count = 0;
 struct clist_item *item; struct clist_item *next;
 item = cl->head;
 while(item != ((void *)0) || cl->count != count) {
  if(!cb(item->data, priv)) {
   next = item->next;
   if(--cl->count) {
    item->prev->next = next;
    next->prev = item->prev;
   } else {
    cl->head = ((void *)0);
   }
   count++;
   if(next == item) {
    clist_item_destroy(item);
    break;
   }
   clist_item_destroy(item);
  } else {
   next = item->next;
  }
  item = next;
 }
 return count;
}

variable_types:
cb
int (*)(void *, void *priv)

variable_types:
priv
void *


decompiled_code:
__int64 __fastcall clist_destroy(__int64 a1)
{
while ( *(_QWORD *)(a1 + 8) )
clist_rpop(a1);
return jfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
while ( *(_QWORD *)(a1 + 8) )
func3(a1);
return jfree(a1);
}

original_code:
void clist_destroy(clist *cl) {
 while(cl->count > 0) {
  clist_rpop(cl);
 }
 jfree(cl);
}

canonical_original_code:
void clist_destroy(struct clist *cl) {
 while(cl->count > 0) {
  clist_rpop(cl);
 }
 jfree(cl);
}


jfree

decompiled_code:
void __fastcall jfree(__int64 a1)
{
__int64 v1; // rax

atomic_add_uint64_constprop_0_isra_0(-4LL - *(unsigned int *)(a1 - 4));
free((void *)(v1 - 4));
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
__int64 v1; // rax

func0(-4LL - *(unsigned int *)(a1 - 4));
free((void *)(v1 - 4));
}

original_code:
void jfree(void* ptr) {
 size_t size;
 void *real_ptr;
 real_ptr = (char*)ptr - 4;
 size = *((uint32_t*)real_ptr);
 atomic_add_uint64(&_used_mem, -(size + 4));
 free(real_ptr);
}

canonical_original_code:
void jfree(void *ptr) {
 unsigned long size;
 void *real_ptr;
 real_ptr = (char *)ptr - 4;
 size = *((unsigned int *)real_ptr);
 atomic_add_uint64(&_used_mem, -(size + 4));
 free(real_ptr);
}

variable_types:
ptr
void *

variable_types:
size
unsigned long


decompiled_code:
__int64 used_mem()
{
return used_mem;
}

canonical_decompiled_code:
__int64 func4()
{
return used_mem;
}

original_code:
uint64_t used_mem() {
 return _used_mem;
}

canonical_original_code:
unsigned long used_mem() {
 return _used_mem;
}

return_type: unsigned long


decompiled_code:
__int64 total_mem()
{
const char *v0; // rbp
unsigned int v1; // eax
int v2; // eax
int v3; // edi
int v4; // ebx
char *v5; // rax
__int64 result; // rax
int v7; // ebx
char v8[1024]; // [rsp+0h] [rbp-1418h] BYREF
char buf[4120]; // [rsp+400h] [rbp-1018h] BYREF

v0 = buf;
memset(v8, 0, sizeof(v8));
memset(buf, 0, 0x1000uLL);
v1 = getpid();
snprintf(v8, 0x400uLL, "/proc/%d/stat", v1);
v2 = open(v8, 0);
if ( v2 < 0 )
{
fwrite("error open stat\n", 1uLL, 0x10uLL, stderr);
result = 0LL;
}
else
{
v3 = v2;
if ( read(v2, buf, 0x1000uLL) < 0 )
{
close(v3);
result = 0LL;
}
else
{
close(v3);
v4 = 23;
do
{
v5 = strchr(v0, 32);
if ( !v5 )
return 0LL;
v0 = v5 + 1;
--v4;
}
while ( v4 );
*strchr(v5 + 1, 32) = 0;
v7 = sysconf(30);
result = strtoll(v0, 0LL, 10) * v7;
}
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
const char *v0; // rbp
unsigned int v1; // eax
int v2; // eax
int v3; // edi
int v4; // ebx
char *v5; // rax
__int64 result; // rax
int v7; // ebx
char v8[1024]; // [rsp+0h] [rbp-1418h] BYREF
char buf[4120]; // [rsp+400h] [rbp-1018h] BYREF

v0 = buf;
memset(v8, 0, sizeof(v8));
memset(buf, 0, 0x1000uLL);
v1 = getpid();
snprintf(v8, 0x400uLL, "/proc/%d/stat", v1);
v2 = open(v8, 0);
if ( v2 < 0 )
{
fwrite("error open stat\n", 1uLL, 0x10uLL, stderr);
result = 0LL;
}
else
{
v3 = v2;
if ( read(v2, buf, 0x1000uLL) < 0 )
{
close(v3);
result = 0LL;
}
else
{
close(v3);
v4 = 23;
do
{
v5 = strchr(v0, 32);
if ( !v5 )
return 0LL;
v0 = v5 + 1;
--v4;
}
while ( v4 );
*strchr(v5 + 1, 32) = 0;
v7 = sysconf(30);
result = strtoll(v0, 0LL, 10) * v7;
}
}
return result;
}

original_code:
uint64_t total_mem() {
 char path[1024], data[4096], *ptr, *end;
 int fd, c;
 uint64_t size;
 char val;
 int page;
 memset(path, 0, sizeof(path));
 memset(data, 0, sizeof(data));
 snprintf(path, sizeof(path), "/proc/%d/stat", getpid());
 if((fd = open(path, 00)) < 0) {
  fprintf(stderr, "error open stat\n");
  return 0;
 }
 if(read(fd, data, sizeof(data)) < 0) {
  close(fd);
  return 0;
 }
 close(fd);
 c = 23;
 ptr = data;
 while(ptr && c--) {
  ptr = strchr(ptr, ' ');
  if(ptr)
   ptr++;
 }
 if(!ptr)
  return 0;
 end = strchr(ptr, ' ');
 end[0] = 0x0;
 page = sysconf(_SC_PAGESIZE);
 size = strtoll(ptr, ((void *)0), 10);
 return size * page;
}

canonical_original_code:
unsigned long total_mem() {
 char path[1024]; char data[4096]; char *ptr; char *end;
 int fd; int c;
 unsigned long size;
 char val;
 int page;
 memset(path, 0, sizeof(path));
 memset(data, 0, sizeof(data));
 snprintf(path, sizeof(path), "/proc/%d/stat", getpid());
 if((fd = open(path, 00)) < 0) {
  fprintf(stderr, "error open stat\n");
  return 0;
 }
 if(read(fd, data, sizeof(data)) < 0) {
  close(fd);
  return 0;
 }
 close(fd);
 c = 23;
 ptr = data;
 while(ptr && c--) {
  ptr = strchr(ptr, ' ');
  if(ptr)
   ptr++;
 }
 if(!ptr)
  return 0;
 end = strchr(ptr, ' ');
 end[0] = 0x0;
 page = sysconf(_SC_PAGESIZE);
 size = strtoll(ptr, ((void *)0), 10);
 return size * page;
}

variable_types:
path
char [1024]

variable_types:
data
char [4096]

variable_types:
end
char *

variable_types: fd int

variable_types: c int

variable_types: val char

variable_types: page int


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

result = jmalloc(24LL);
*(_DWORD *)(result + 4) = a1;
*(_QWORD *)(result + 8) = a2;
*(_DWORD *)result = 1;
*(_DWORD *)(result + 16) = 0;
return result;
}

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

result = jmalloc(24LL);
*(_DWORD *)(result + 4) = a1;
*(_QWORD *)(result + 8) = a2;
*(_DWORD *)result = 1;
*(_DWORD *)(result + 16) = 0;
return result;
}

original_code:
obj* obj_create(int type, void *priv) {
 obj *o = jmalloc(sizeof(struct obj));
 o->type = type;
 o->priv = priv;
 o->ref = 1;
 (*&o->lock)=0;
 return o;
}

canonical_original_code:
struct obj *obj_create(int type, void *priv) {
 struct obj *o = jmalloc(sizeof(struct obj));
 o->type = type;
 o->priv = priv;
 o->ref = 1;
 (*&o->lock)=0;
 return o;
}

variable_types: type int

variable_types: struct obj { int ref; int type; void *priv; unsigned int lock; } *


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

v0 = dict_create();
return obj_create(1, v0);
}

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

v0 = dict_create();
return func1(1, v0);
}

original_code:
obj* dict_obj_create(dict_opts *opts) {
 dict *d = dict_create(opts);
 return obj_create(OBJ_TYPE_DICT, d);
}

canonical_original_code:
struct obj *dict_obj_create(struct dict_opts *opts) {
 struct dict *d = dict_create(opts);
 return obj_create(OBJ_TYPE_DICT, d);
}

variable_types: struct dict { struct dict_opts { unsigned int (*hash)(void *key); void *(*key_dup)(void *key); void *(*value_dup)(void *key); int (*key_compare)(void *k1, void *k2); void (*key_destroy)(void *key); void (*value_destroy)(void *v); } *opts; struct dict_table { struct dict_entry { void *key; void *value; int ref; struct dict_entry *next; } **entries; unsigned int size; unsigned int mask; unsigned int used; } dt[2]; int rehashidx; int iterators; } *

return_type: struct obj { int ref; int type; void *priv; unsigned int lock; } *

user_defined_types: struct dict { struct dict_opts *opts; struct dict_table dt[2]; int rehashidx; int iterators; }

user_defined_types: struct dict_table { struct dict_entry **entries; unsigned int size; unsigned int mask; unsigned int used; }


decompiled_code:
__int64 __fastcall cstr_obj_create(const char *a1)
{
size_t v1; // rax
__int64 v2; // rax

v1 = strlen(a1);
v2 = cstr_new(a1, v1);
return obj_create(0, v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
size_t v1; // rax
__int64 v2; // rax

v1 = strlen(a1);
v2 = cstr_new(a1, v1);
return func1(0, v2);
}

original_code:
obj* cstr_obj_create(const char *s) {
 char * cs = cstr_new(s, strlen(s));
 obj *o = obj_create(OBJ_TYPE_STR, cs);
 return o;
}

canonical_original_code:
struct obj *cstr_obj_create(char *s) {
 char *cs = cstr_new(s, strlen(s));
 struct obj *o = obj_create(OBJ_TYPE_STR, cs);
 return o;
}

variable_types:
s
char *

variable_types:
cs
char *


decompiled_code:
__int64 __fastcall obj_incr(unsigned int *a1)
{
spinlock_lock(a1 + 4);
++*a1;
spinlock_unlock(a1 + 4);
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
spinlock_lock(a1 + 4);
++*a1;
func0(a1 + 4);
return *a1;
}

original_code:
int obj_incr(obj *o) {
 int rs;
 spinlock_lock(&o->lock);;
 o->ref++;
 spinlock_unlock(&o->lock);;
 return o->ref;
}

canonical_original_code:
int obj_incr(struct obj *o) {
 int rs;
 spinlock_lock(&o->lock);;
 o->ref++;
 spinlock_unlock(&o->lock);;
 return o->ref;
}

variable_types: rs int


decompiled_code:
void __fastcall obj_decr(__int64 a1)
{
_DWORD *v1; // r12
int v3; // eax

v1 = (_DWORD *)(a1 + 16);
spinlock_lock(a1 + 16);
if ( (*(_DWORD *)a1)-- == 1 )
{
v3 = *(_DWORD *)(a1 + 4);
if ( v3 )
{
if ( v3 == 1 )
dict_destroy(*(_QWORD *)(a1 + 8));
}
else
{
cstr_destroy(*(_QWORD *)(a1 + 8));
}
spinlock_unlock(v1);
*(_DWORD *)(a1 + 16) = 0;
jfree(a1);
}
else
{
spinlock_unlock(v1);
}
}

original_code:
int obj_decr(obj *o) {
 spinlock_lock(&o->lock);;
 o->ref--;
 if(o->ref == 0) {
  switch(o->type) {
  case OBJ_TYPE_DICT:
   dict_destroy((dict*)o->priv);
   break;
  case OBJ_TYPE_STR:
   cstr_destroy((char *)o->priv);
   break;
  }
  spinlock_unlock(&o->lock);;
  (*&o->lock)=0;
  jfree(o);
  return 1;
 }
 spinlock_unlock(&o->lock);;
 return 0;
}

canonical_original_code:
int obj_decr(struct obj *o) {
 spinlock_lock(&o->lock);;
 o->ref--;
 if(o->ref == 0) {
  switch(o->type) {
  case OBJ_TYPE_DICT:
   dict_destroy((struct dict *)o->priv);
   break;
  case OBJ_TYPE_STR:
   cstr_destroy((char *)o->priv);
   break;
  }
  spinlock_unlock(&o->lock);;
  (*&o->lock)=0;
  jfree(o);
  return 1;
 }
 spinlock_unlock(&o->lock);;
 return 0;
}

user_defined_types: struct dict_opts { unsigned int (*hash)(void *key); void *(*key_dup)(void *key); void *(*value_dup)(void *key); int (*key_compare)(void *k1, void *k2); void (*key_destroy)(void *key); void (*value_destroy)(void *v); }


decompiled_code:
__int64 __fastcall cstr_destroy(__int64 a1)
{
return jfree(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return jfree(a1 - 8);
}

original_code:
void cstr_destroy(char * s) {
 char *ptr = ((char*)(s - sizeof(cstrhdr)));
 jfree(ptr);
}

canonical_original_code:
void cstr_destroy(char *s) {
 char *ptr = ((char*)(s - sizeof(cstrhdr)));
 jfree(ptr);
}

variable_types:
ptr
char *