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 obj { int ref; int type; void *priv; unsigned int lock; }


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

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

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

v0 = cnet_tcp_server("0.0.0.0", port, v3, 1024LL);
if ( v0 < 0 )
{
log_print(4LL, "%s", v3);
v1 = -1;
}
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 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 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 clist { struct clist_item *head; unsigned long count; }

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:
_QWORD *create_thr_server()
{
_QWORD *v0; // rbx
unsigned int v1; // eax
__int64 v2; // rax
__int64 v3; // rax
int v4; // eax

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

canonical_decompiled_code:
_QWORD *func9()
{
_QWORD *v0; // rbx
unsigned int v1; // eax
__int64 v2; // rax
__int64 v3; // rax
int v4; // eax

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

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 v4; // 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;
v4 = used_mem();
log_print(2LL, "total connections:%lu, memory used:%lu\n", *(unsigned int *)(a1 + 16), v4);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rdx
int v2; // er12
int i; // ebp
__int64 v4; // 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;
v4 = used_mem();
log_print(2LL, "total connections:%lu, memory used:%lu\n", *(unsigned int *)(a1 + 16), v4);
}
}
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 cevent_fired { int mask; int fd; }

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

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


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, __int64 a2)
{
__int64 result; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

*((_QWORD *)arg + 10) = a2;
if ( pthread_cond_init((pthread_cond_t *)((char *)arg + 8), 0LL) < 0 )
{
log_print(4LL, "init thread cond error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_attr_init(&v3) < 0 )
{
log_print(4LL, "init thread attr error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_create((pthread_t *)arg, &v3, 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, __int64 a2)
{
__int64 result; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

*((_QWORD *)arg + 10) = a2;
if ( pthread_cond_init((pthread_cond_t *)((char *)arg + 8), 0LL) < 0 )
{
log_print(4LL, "init thread cond error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_attr_init(&v3) < 0 )
{
log_print(4LL, "init thread attr error\n");
result = 0xFFFFFFFFLL;
}
else if ( pthread_create((pthread_t *)arg, &v3, 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; }


decompiled_code:
__int64 __fastcall cthr_pool_destroy(__int64 a1)
{
unsigned __int64 v1; // rbx
pthread_mutex_t *v2; // r15
__int64 v3; // r14
int v4; // ebp
int *v5; // rax
char *v6; // rax
void *thread_return; // [rsp+8h] [rbp-40h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned __int64 v1; // rbx
pthread_mutex_t *v2; // r15
__int64 v3; // r14
int v4; // ebp
int *v5; // rax
char *v6; // rax
void *thread_return; // [rsp+8h] [rbp-40h] BYREF

*(_DWORD *)(a1 + 48) = 1;
if ( *(_QWORD *)(a1 + 56) )
{
v1 = 0LL;
v2 = (pthread_mutex_t *)(a1 + 8);
do
{
v3 = *(_QWORD *)a1 + 88 * v1;
pthread_mutex_lock(v2);
v4 = pthread_cond_signal((pthread_cond_t *)(v3 + 8));
pthread_mutex_unlock(v2);
if ( v4 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
log_print(4LL, "%s\n", v6);
}
pthread_join(*(_QWORD *)v3, &thread_return);
pthread_cond_destroy((pthread_cond_t *)(v3 + 8));
++v1;
}
while ( *(_QWORD *)(a1 + 56) > v1 );
}
pthread_mutex_destroy((pthread_mutex_t *)(a1 + 8));
jfree(*(_QWORD *)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:
__int64 __fastcall cthr_pool_create(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rbx
__int64 v5; // rbp
__int64 i; // r12
__int64 v7; // 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);
v4 = 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 *)(*(_QWORD *)v4 + v5), v4) < 0 )
{
cthr_pool_destroy(v4);
return 0LL;
}
v5 += 88LL;
}
v7 = *(_QWORD *)v4;
do
v7 += 88LL;
while ( v7 != *(_QWORD *)v4 + 88 * a1 );
}
*(_DWORD *)(v4 + 48) = 0;
}
return v4;
}

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, __int64 a3)
{
pthread_mutex_t *v5; // r12
__int64 v6; // rsi
__int64 v7; // rax
__int64 i; // rdx
__int64 v9; // rdi

v5 = (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;
*(_QWORD *)(v9 + 72) = a3;
pthread_cond_signal((pthread_cond_t *)(v9 + 8));
pthread_mutex_unlock(v5);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, __int64 a2, __int64 a3)
{
pthread_mutex_t *v5; // r12
__int64 v6; // rsi
__int64 v7; // rax
__int64 i; // rdx
__int64 v9; // rdi

v5 = (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;
*(_QWORD *)(v9 + 72) = a3;
pthread_cond_signal((pthread_cond_t *)(v9 + 8));
pthread_mutex_unlock(v5);
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 v5; // edx
int *v6; // rdi
int v7; // esi
char v8; // dl
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

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

canonical_decompiled_code:
int __fastcall func3(__int64 a1, int a2, char a3)
{
int v5; // edx
int *v6; // rdi
int v7; // esi
char v8; // dl
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

v5 = *(_DWORD *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
*(_QWORD *)&event.events = 0LL;
HIDWORD(event.data.u64) = 0;
v6 = *(int **)(a1 + 120);
v7 = v5 == 0 ? 1 : 3;
v8 = a3 | v5;
if ( (v8 & 1) != 0 )
event.events = 1;
if ( (v8 & 2) != 0 )
event.events |= 4u;
event.data.fd = a2;
return epoll_ctl(*v6, v7, 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]; }


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; // rbx
__int64 result; // rax

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

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

spinlock_lock(a1 + 32);
v2 = (_QWORD *)clist_rpop(*(_QWORD *)(a1 + 24));
func0((_DWORD *)(a1 + 32));
result = 0LL;
if ( v2 )
{
*a2 = *v2;
jfree(v2);
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; } *


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(__int64 a1)
{
__int64 v2; // rdi
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
jfree(v2);
v3 = *(_QWORD *)(a1 + 16);
if ( v3 )
jfree(v3);
if ( *(_QWORD *)(a1 + 24) )
clist_destroy();
*(_DWORD *)(a1 + 36) = 0;
*(_DWORD *)(a1 + 32) = 0;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
cevents_destroy_priv_impl(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 v7; // rbp
int v8; // er14
int v9; // ebx
int *v11; // rax
char *v12; // rax

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

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:
__int64 __fastcall cevents_add_event(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // ebx

spinlock_lock(a1 + 36);
v8 = cevents_add_event_inner(a1, a2, a3, a4, a5);
spinlock_unlock((_DWORD *)(a1 + 36));
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // ebx

spinlock_lock(a1 + 36);
v8 = func10(a1, a2, a3, a4, a5);
func0((_DWORD *)(a1 + 36));
return v8;
}

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


decompiled_code:
__int64 __fastcall cevents_del_event_inner(__int64 a1, int a2, int a3)
{
int *v4; // rbx
__int64 result; // rax
int v6; // ebp
int v7; // edx
int *v8; // rax
char *v9; // rax
int v10; // ecx
__int64 i; // rdx

if ( a2 > 204800 )
return 0xFFFFFFFFLL;
v4 = (int *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
result = (unsigned int)*v4;
if ( (_DWORD)result )
{
if ( cevents_del_event_impl(a1, a2, a3) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(4LL, "del event error:%s\n", v9);
}
v6 = *v4 & ~a3;
*v4 = v6;
v7 = *(_DWORD *)a1;
if ( !v6 && v7 == a2 )
{
v10 = v7 - 1;
if ( v7 - 1 < 0 )
{
result = 0LL;
}
else
{
for ( i = *(_QWORD *)(a1 + 8) + 32LL * v7; ; i -= 32LL )
{
result = *(unsigned int *)(i - 32);
if ( (_DWORD)result )
break;
if ( --v10 == -1 )
return result;
}
*(_DWORD *)a1 = v10;
result = 0LL;
}
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2, int a3)
{
int *v4; // rbx
__int64 result; // rax
int v6; // ebp
int v7; // edx
int *v8; // rax
char *v9; // rax
int v10; // ecx
__int64 i; // rdx

if ( a2 > 204800 )
return 0xFFFFFFFFLL;
v4 = (int *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
result = (unsigned int)*v4;
if ( (_DWORD)result )
{
if ( func4(a1, a2, a3) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(4LL, "del event error:%s\n", v9);
}
v6 = *v4 & ~a3;
*v4 = v6;
v7 = *(_DWORD *)a1;
if ( !v6 && v7 == a2 )
{
v10 = v7 - 1;
if ( v7 - 1 < 0 )
{
result = 0LL;
}
else
{
for ( i = *(_QWORD *)(a1 + 8) + 32LL * v7; ; i -= 32LL )
{
result = *(unsigned int *)(i - 32);
if ( (_DWORD)result )
break;
if ( --v10 == -1 )
return result;
}
*(_DWORD *)a1 = v10;
result = 0LL;
}
}
else
{
result = 0LL;
}
}
return result;
}

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:
__int64 __fastcall cevents_del_event(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebx

spinlock_lock(a1 + 36);
v4 = cevents_del_event_inner(a1, a2, a3);
spinlock_unlock((_DWORD *)(a1 + 36));
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebx

spinlock_lock(a1 + 36);
v4 = func12(a1, a2, a3);
func0((_DWORD *)(a1 + 36));
return v4;
}

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)
{
int v2; // er14
unsigned int v3; // er15
__int64 v4; // r14
__int64 v5; // r12
_QWORD *v6; // rax
_QWORD *v7; // rax
int *v8; // r13
__int64 v9; // rsi
__int64 v10; // rbx
int v11; // eax

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

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)
{
unsigned int v9; // eax
unsigned int v10; // ebx
int v11; // edi
char *v12; // rax

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

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

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

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)
{
__int64 result; // rax
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);
result = 0xFFFFFFFFLL;
}
else if ( listen(fd, 511) < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
cnet_fmt_err(a4, a5, "listen error: %s\n", v11);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, const struct sockaddr *a2, socklen_t a3, char *a4, size_t a5)
{
__int64 result; // rax
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);
result = 0xFFFFFFFFLL;
}
else if ( listen(fd, 511) < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
func0(a4, a5, "listen error: %s\n", v11);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

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; // ebx
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

optval[0] = 1;
v6 = socket(a1, a2, 0);
if ( v6 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
cnet_fmt_err(a3, a4, "socket create error: %s\n", v10);
v7 = -1;
}
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);
close(v7);
v7 = -1;
}
}
return v7;
}

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

optval[0] = 1;
v6 = socket(a1, a2, 0);
if ( v6 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
func0(a3, a4, "socket create error: %s\n", v10);
v7 = -1;
}
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);
close(v7);
v7 = -1;
}
}
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(char *cp, __int16 a2, char *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // ebx
int *v9; // rax
char *v10; // rax
struct sockaddr v11[2]; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func7(char *cp, __int16 a2, char *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // ebx
int *v9; // rax
char *v10; // rax
struct sockaddr v11[2]; // [rsp+0h] [rbp-28h] BYREF

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

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; // ebx
int *v8; // rax
char *v9; // rax
struct sockaddr v10[10]; // [rsp+0h] [rbp-A8h] BYREF

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

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

v6 = func6(1, 1, a3, a4);
if ( v6 >= 0 )
{
v10[0].sa_family = 1;
strncpy(v10[0].sa_data, src, 0x6BuLL);
if ( (int)func3(v6, v10, 0x6Eu, a3, a4) < 0 )
{
close(v6);
v6 = -1;
}
else if ( fchmod(v6, mode) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
func0(a3, a4, "socket set reuse error: %s\n", v9);
close(v6);
v6 = -1;
}
}
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; // ebp
__int64 v6; // rax
int v7; // eax

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

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

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v7 = write(fd, buf, a3 - v6);
if ( !v7 )
break;
if ( v7 == -1 )
return (unsigned int)-1;
buf = (char *)buf + v7;
v5 += v7;
v6 = (int)v5;
}
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; // ebp
__int64 v6; // rax
int v7; // eax

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

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

if ( !a3 )
return 0;
v5 = 0;
v6 = 0LL;
do
{
v7 = read(fd, buf, a3 - v6);
if ( !v7 )
break;
if ( v7 == -1 )
return (unsigned int)-1;
buf = (char *)buf + v7;
v5 += v7;
v6 = (int)v5;
}
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 v1; // rbx

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

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
unsigned __int64 v1; // rbx

cstr_destroy(a1[2]);
cstr_destroy(a1[3]);
if ( a1[24] )
{
v1 = 0LL;
do
obj_decr(*(_QWORD *)(a1[23] + 8 * v1++));
while ( a1[24] > v1 );
}
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:
__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; } *

user_defined_types: struct clist { struct clist_item *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)
{
_QWORD *v4; // rbx
unsigned int v5; // er13
_QWORD *v7; // rbp
__int64 v8; // rax

v4 = *(_QWORD **)a1;
v5 = 0;
while ( v4 || *(_QWORD *)(a1 + 8) != v5 )
{
if ( a2(v4[2], a3) )
{
v4 = (_QWORD *)v4[1];
}
else
{
v7 = (_QWORD *)v4[1];
v8 = *(_QWORD *)(a1 + 8) - 1LL;
*(_QWORD *)(a1 + 8) = v8;
if ( v8 )
{
*(_QWORD *)(*v4 + 8LL) = v7;
*v7 = *v4;
}
else
{
*(_QWORD *)a1 = 0LL;
}
++v5;
if ( v4 == v7 )
{
clist_item_destroy(v7);
return v5;
}
clist_item_destroy(v4);
v4 = v7;
}
}
return v5;
}

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)
{
atomic_add_uint64(&used_mem, -4LL - *(unsigned int *)(a1 - 4));
free((void *)(a1 - 4));
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
func0(&used_mem, -4LL - *(unsigned int *)(a1 - 4));
free((void *)(a1 - 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 func2()
{
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()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ebx
int v3; // ebp
char *v4; // rbx
__int64 result; // rax
char *v6; // rax
int v7; // ebp
char v8[4096]; // [rsp+0h] [rbp-1418h] BYREF
char s[1048]; // [rsp+1000h] [rbp-418h] BYREF

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

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ebx
int v3; // ebp
char *v4; // rbx
__int64 result; // rax
char *v6; // rax
int v7; // ebp
char v8[4096]; // [rsp+0h] [rbp-1418h] BYREF
char s[1048]; // [rsp+1000h] [rbp-418h] BYREF

memset(s, 0, 0x400uLL);
memset(v8, 0, sizeof(v8));
v0 = getpid();
snprintf(s, 0x400uLL, "/proc/%d/stat", v0);
v1 = open(s, 0);
if ( v1 < 0 )
{
fwrite("error open stat\n", 1uLL, 0x10uLL, stderr);
result = 0LL;
}
else
{
v2 = v1;
if ( read(v1, v8, 0x1000uLL) < 0 )
{
close(v2);
result = 0LL;
}
else
{
close(v2);
v3 = 23;
v4 = v8;
do
{
v6 = strchr(v4, 32);
if ( !v6 )
return 0LL;
v4 = v6 + 1;
--v3;
}
while ( v3 );
*strchr(v6 + 1, 32) = 0;
v7 = sysconf(30);
result = strtoll(v4, 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:
__int64 __fastcall obj_decr(__int64 a1)
{
_DWORD *v1; // rbp
int v3; // eax
__int64 result; // rax

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);
result = 1LL;
}
else
{
spinlock_unlock(v1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
_DWORD *v1; // rbp
int v3; // eax
__int64 result; // rax

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));
}
func0(v1);
*(_DWORD *)(a1 + 16) = 0;
jfree(a1);
result = 1LL;
}
else
{
func0(v1);
result = 0LL;
}
return result;
}

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 func1(__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 *


original_code:
char ** cstr_split(char *s, size_t len, const char *b, size_t slen, size_t *l) {
 char * *array = ((void *)0);
 size_t i, j, cap = 0, size = 0, beg = 0;
 array = jmalloc(sizeof(char *)*cap);
 for(i = 0; i < len - (slen - 1); i++) {
  if(size + 1 >= cap ) {
   cap += 5;
   array = jrealloc(array, sizeof(char *)*cap);
  }
  if(s[i] == b[0] && memcmp(s + i, b, slen) == 0) {
   array[size] = cstr_new(s + beg, i - beg);
   beg = i + slen;
   size++;
  }
 }
 array[size++] = cstr_new(s + beg, len - beg);
 *l = size;
 return array;
}

canonical_original_code:
char **cstr_split(char *s, unsigned long len, char *b, unsigned long slen, unsigned long *l) {
 char **array = ((void *)0);
 unsigned long i; unsigned long j; unsigned long cap = 0; unsigned long size = 0; unsigned long beg = 0;
 array = jmalloc(sizeof(char *)*cap);
 for(i = 0; i < len - (slen - 1); i++) {
  if(size + 1 >= cap ) {
   cap += 5;
   array = jrealloc(array, sizeof(char *)*cap);
  }
  if(s[i] == b[0] && memcmp(s + i, b, slen) == 0) {
   array[size] = cstr_new(s + beg, i - beg);
   beg = i + slen;
   size++;
  }
 }
 array[size++] = cstr_new(s + beg, len - beg);
 *l = size;
 return array;
}

variable_types:
len
unsigned long

variable_types:
b
char *

variable_types:
slen
unsigned long

variable_types:
l
unsigned long *

variable_types:
array
char **

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
cap
unsigned long

variable_types:
beg
unsigned long

return_type: char **


decompiled_code:
unsigned __int64 __fastcall cstr_tolower(__int64 a1)
{
unsigned __int64 result; // rax
const __int32_t **v2; // rsi

result = *(unsigned int *)(a1 - 4);
if ( *(_DWORD *)(a1 - 8) != (_DWORD)result )
{
v2 = _ctype_tolower_loc();
result = 0LL;
do
{
*(_BYTE *)(a1 + result) = (*v2)[*(char *)(a1 + result)];
++result;
}
while ( result < (unsigned int)(*(_DWORD *)(a1 - 8) - *(_DWORD *)(a1 - 4)) );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(__int64 a1)
{
unsigned __int64 result; // rax
const __int32_t **v2; // rsi

result = *(unsigned int *)(a1 - 4);
if ( *(_DWORD *)(a1 - 8) != (_DWORD)result )
{
v2 = _ctype_tolower_loc();
result = 0LL;
do
{
*(_BYTE *)(a1 + result) = (*v2)[*(char *)(a1 + result)];
++result;
}
while ( result < (unsigned int)(*(_DWORD *)(a1 - 8) - *(_DWORD *)(a1 - 4)) );
}
return result;
}

original_code:
void cstr_tolower(char * s) {
 size_t i;
 for(i = 0; i < ((((cstrhdr*)(s - sizeof(cstrhdr)))->len - ((cstrhdr*)(s - sizeof(cstrhdr)))->free)); i++) s[i] = tolower(s[i]);
}

canonical_original_code:
void cstr_tolower(char *s) {
 unsigned long i;
 for(i = 0; i < ((((struct cstrhdr *)(s - sizeof(cstrhdr)))->len - ((struct cstrhdr *)(s - sizeof(cstrhdr)))->free)); i++) s[i] = tolower(s[i]);
}

user_defined_types: struct cstrhdr { unsigned int len; unsigned int free; char buf[]; }


decompiled_code:
unsigned __int64 __fastcall cstr_toupper(__int64 a1)
{
unsigned __int64 result; // rax
const __int32_t **v2; // rsi

result = *(unsigned int *)(a1 - 4);
if ( *(_DWORD *)(a1 - 8) != (_DWORD)result )
{
v2 = _ctype_toupper_loc();
result = 0LL;
do
{
*(_BYTE *)(a1 + result) = (*v2)[*(char *)(a1 + result)];
++result;
}
while ( result < (unsigned int)(*(_DWORD *)(a1 - 8) - *(_DWORD *)(a1 - 4)) );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(__int64 a1)
{
unsigned __int64 result; // rax
const __int32_t **v2; // rsi

result = *(unsigned int *)(a1 - 4);
if ( *(_DWORD *)(a1 - 8) != (_DWORD)result )
{
v2 = _ctype_toupper_loc();
result = 0LL;
do
{
*(_BYTE *)(a1 + result) = (*v2)[*(char *)(a1 + result)];
++result;
}
while ( result < (unsigned int)(*(_DWORD *)(a1 - 8) - *(_DWORD *)(a1 - 4)) );
}
return result;
}

original_code:
void cstr_toupper(char * s) {
 size_t i;
 for(i = 0; i < ((((cstrhdr*)(s - sizeof(cstrhdr)))->len - ((cstrhdr*)(s - sizeof(cstrhdr)))->free)); i++) s[i] = toupper(s[i]);
}

canonical_original_code:
void cstr_toupper(char *s) {
 unsigned long i;
 for(i = 0; i < ((((struct cstrhdr *)(s - sizeof(cstrhdr)))->len - ((struct cstrhdr *)(s - sizeof(cstrhdr)))->free)); i++) s[i] = toupper(s[i]);
}


decompiled_code:
__int64 __fastcall str2ll(_BYTE *a1, __int64 a2, __int64 *a3)
{
_BYTE *v4; // rsi
int v5; // er9
__int64 v6; // rax
__int64 v7; // rdx
unsigned __int64 v8; // rdx
__int64 v9; // rax
__int64 result; // rax

if ( *a1 == 45 )
{
v4 = &a1[a2];
if ( a1 + 1 == v4 )
{
v7 = 0LL;
LABEL_9:
*a3 = -v7;
return 0LL;
}
++a1;
v5 = 1;
}
else
{
v4 = &a1[a2];
v5 = 0;
v6 = 0LL;
if ( a1 == v4 )
{
LABEL_14:
*a3 = v6;
return 0LL;
}
}
v7 = 0LL;
do
{
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v8 = 10 * v7;
v9 = (char)*a1 - 48;
if ( ~v9 < v8 )
return 0xFFFFFFFFLL;
v7 = v9 + v8;
++a1;
}
while ( a1 != v4 );
if ( v5 )
{
if ( (unsigned __int64)v7 <= 0x8000000000000000LL )
goto LABEL_9;
result = 0xFFFFFFFFLL;
}
else
{
v6 = v7;
if ( v7 >= 0 )
goto LABEL_14;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, __int64 a2, __int64 *a3)
{
_BYTE *v4; // rsi
int v5; // er9
__int64 v6; // rax
__int64 v7; // rdx
unsigned __int64 v8; // rdx
__int64 v9; // rax
__int64 result; // rax

if ( *a1 == 45 )
{
v4 = &a1[a2];
if ( a1 + 1 == v4 )
{
v7 = 0LL;
LABEL_9:
*a3 = -v7;
return 0LL;
}
++a1;
v5 = 1;
}
else
{
v4 = &a1[a2];
v5 = 0;
v6 = 0LL;
if ( a1 == v4 )
{
LABEL_14:
*a3 = v6;
return 0LL;
}
}
v7 = 0LL;
do
{
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v8 = 10 * v7;
v9 = (char)*a1 - 48;
if ( ~v9 < v8 )
return 0xFFFFFFFFLL;
v7 = v9 + v8;
++a1;
}
while ( a1 != v4 );
if ( v5 )
{
if ( (unsigned __int64)v7 <= 0x8000000000000000LL )
goto LABEL_9;
result = 0xFFFFFFFFLL;
}
else
{
v6 = v7;
if ( v7 >= 0 )
goto LABEL_14;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int str2ll(char *p, size_t len, long long *l) {
 unsigned long long v = 0;
 char *ptr = p, c;
 int negtive = 0, i;
 if(ptr[0] == '-') {
  ptr++;
  negtive = 1;
 }
 while(ptr != p + len) {
  c = ptr[0];
  if(c < '0' || c > '9')
   return -1;
  v *= 10;
  i = ptr[0] - '0';
  if(v > (0x7fffffffffffffffLL * 2ULL + 1ULL) - i)
   return -1;
  v += i;
  ptr++;
 }
 if(negtive) {
  if(v > (unsigned long long)(-0x7fffffffffffffffLL - 1LL))
   return -1;
  *l = -v;
 } else {
  if(v > 0x7fffffffffffffffLL)
   return -1;
  *l = v;
 }
 return 0;
}

canonical_original_code:
int str2ll(char *p, unsigned long len, long long *l) {
 unsigned long long v = 0;
 char *ptr = p; char c;
 int negtive = 0; int i;
 if(ptr[0] == '-') {
  ptr++;
  negtive = 1;
 }
 while(ptr != p + len) {
  c = ptr[0];
  if(c < '0' || c > '9')
   return -1;
  v *= 10;
  i = ptr[0] - '0';
  if(v > (0x7fffffffffffffffLL * 2ULL + 1ULL) - i)
   return -1;
  v += i;
  ptr++;
 }
 if(negtive) {
  if(v > (unsigned long long)(-0x7fffffffffffffffLL - 1LL))
   return -1;
  *l = -v;
 } else {
  if(v > 0x7fffffffffffffffLL)
   return -1;
  *l = v;
 }
 return 0;
}

variable_types:
p
char *

variable_types:
l
long long *

variable_types:
v
unsigned long long

variable_types: c char

variable_types: negtive int

variable_types: i int


decompiled_code:
__int64 __fastcall str2l(_BYTE *a1, __int64 a2, _QWORD *a3)
{
__int64 result; // rax
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

result = str2ll(a1, a2, &v5);
if ( (int)result >= 0 )
{
*a3 = v5;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1, __int64 a2, _QWORD *a3)
{
__int64 result; // rax
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

result = func1(a1, a2, &v5);
if ( (int)result >= 0 )
{
*a3 = v5;
result = 0LL;
}
return result;
}

original_code:
int str2l(char *p, size_t len, long *l) {
 int rs;
 long long ll;
 rs = str2ll(p, len, &ll);
 if(rs < 0) return rs;
 if(ll > (long long)0x7fffffffffffffffL) return -1;
 if(ll < (long long)(-0x7fffffffffffffffL - 1L)) return -1;
 *l = ll;
 return 0;
}

canonical_original_code:
int str2l(char *p, unsigned long len, long *l) {
 int rs;
 long long ll;
 rs = str2ll(p, len, &ll);
 if(rs < 0) return rs;
 if(ll > (long long)0x7fffffffffffffffL) return -1;
 if(ll < (long long)(-0x7fffffffffffffffL - 1L)) return -1;
 *l = ll;
 return 0;
}

variable_types:
l
long *

variable_types: rs int

variable_types:
ll
long long