decompiled_code:
unsigned __int64 __fastcall keymgr_x509_msg(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 v9[4]; // [rsp+0h] [rbp-158h] BYREF
__int64 v10[33]; // [rsp+20h] [rbp-138h] BYREF
unsigned __int64 v11; // [rsp+128h] [rbp-30h]

v11 = __readfsqword(0x28u);
memset(v10, 0, sizeof(v10));
v10[32] = a3;
if ( (unsigned __int64)kore_strlcpy(v10, a1, 256LL) > 0xFF )
fatalx("%s: domain truncated", "keymgr_x509_msg");
kore_buf_init(v9, a3 + 264);
kore_buf_append(v9, v10, 264LL);
kore_buf_append(v9, a2, a3);
kore_msg_send(a4, a5, v9[0], v9[3]);
kore_buf_cleanup(v9);
return v11 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 v9[4]; // [rsp+0h] [rbp-158h] BYREF
__int64 v10[33]; // [rsp+20h] [rbp-138h] BYREF
unsigned __int64 v11; // [rsp+128h] [rbp-30h]

v11 = __readfsqword(0x28u);
memset(v10, 0, sizeof(v10));
v10[32] = a3;
if ( (unsigned __int64)kore_strlcpy(v10, a1, 256LL) > 0xFF )
fatalx("%s: domain truncated", "keymgr_x509_msg");
kore_buf_init(v9, a3 + 264);
kore_buf_append(v9, v10, 264LL);
kore_buf_append(v9, a2, a3);
kore_msg_send(a4, a5, v9[0], v9[3]);
kore_buf_cleanup(v9);
return v11 - __readfsqword(0x28u);
}

original_code:
static void
keymgr_x509_msg(const char *domain, const void *data, size_t len,
    int target, int msg)
{
 struct kore_buf buf;
 struct kore_x509_msg hdr;
 memset(&hdr, 0, sizeof(hdr));
 hdr.data_len = len;
 if (kore_strlcpy(hdr.domain, domain, sizeof(hdr.domain)) >=
     sizeof(hdr.domain))
  fatalx("%s: domain truncated", __func__);
 kore_buf_init(&buf, sizeof(hdr) + len);
 kore_buf_append(&buf, &hdr, sizeof(hdr));
 kore_buf_append(&buf, data, len);
 kore_msg_send(target, msg, buf.data, buf.offset);
 kore_buf_cleanup(&buf);
}

canonical_original_code:
static void keymgr_x509_msg(char *domain, void *data, unsigned long len, int target, int msg)
{
 struct kore_buf buf;
 struct kore_x509_msg hdr;
 memset(&hdr, 0, sizeof(hdr));
 hdr.data_len = len;
 if (kore_strlcpy(hdr.domain, domain, sizeof(hdr.domain)) >=
     sizeof(hdr.domain))
  fatalx("%s: domain truncated", __func__);
 kore_buf_init(&buf, sizeof(hdr) + len);
 kore_buf_append(&buf, &hdr, sizeof(hdr));
 kore_buf_append(&buf, data, len);
 kore_msg_send(target, msg, buf.data, buf.offset);
 kore_buf_cleanup(&buf);
}

variable_types:
domain
char *

variable_types:
len
unsigned long

variable_types: target int

variable_types: msg int

variable_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; }

variable_types: struct kore_x509_msg { char domain[-1]; unsigned long data_len; unsigned int data[]; }


repo: jorisvink/kore

decompiled_code:
__int64 kore_keymgr_run()
{
__int64 v0; // rbx
__int64 v1; // rbp
__int64 v2; // rax
__int64 v4; // rax

if ( !kore_keymgr_active )
{
fatalx("%s: called with kore_keymgr_active == 0", "kore_keymgr_run");
JUMPOUT(0xE98LL);
}
kore_server_closeall();
kore_module_cleanup();
net_init();
kore_timer_init();
kore_connection_init();
kore_platform_event_init();
kore_msg_worker_init();
kore_msg_register(2LL, keymgr_msg_recv);
kore_msg_register(5LL, keymgr_entropy_request);
kore_msg_register(8LL, keymgr_certificate_request);
kore_seccomp_drop();
kore_seccomp_filter("keymgr", &filter_keymgr, 70LL);
kore_worker_privsep();
if ( kore_rand_file )
{
keymgr_load_randfile();
keymgr_save_randfile();
}
else if ( !kore_quiet )
{
kore_log(4LL, "no rand_file location specified");
}
RAND_poll();
v0 = 0LL;
initialized = 1;
keymgr_reload();
kore_worker_started();
while ( 1 )
{
v1 = kore_time_ms();
if ( (unsigned __int64)(v1 - v0) > 0x1B7740 )
{
RAND_poll();
v0 = v1;
}
v2 = kore_timer_next_run(v1);
kore_platform_event_wait(v2);
if ( sig_recv )
break;
LABEL_14:
v4 = kore_time_ms();
kore_timer_run(v4);
kore_connection_prune(0LL);
}
if ( sig_recv == 10 )
{
keymgr_reload();
goto LABEL_13;
}
if ( sig_recv <= 10 )
{
if ( (unsigned int)(sig_recv - 2) <= 1 )
goto LABEL_11;
goto LABEL_13;
}
if ( sig_recv != 15 )
{
LABEL_13:
sig_recv = 0;
goto LABEL_14;
}
LABEL_11:
sig_recv = 0;
kore_keymgr_cleanup(1LL);
kore_platform_event_cleanup();
kore_connection_cleanup();
return net_cleanup();
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rbx
__int64 v1; // rbp
__int64 v2; // rax
__int64 v4; // rax

if ( !kore_keymgr_active )
{
fatalx("%s: called with kore_keymgr_active == 0", "kore_keymgr_run");
JUMPOUT(0xE98LL);
}
kore_server_closeall();
kore_module_cleanup();
net_init();
kore_timer_init();
kore_connection_init();
kore_platform_event_init();
kore_msg_worker_init();
kore_msg_register(2LL, keymgr_msg_recv);
kore_msg_register(5LL, keymgr_entropy_request);
kore_msg_register(8LL, keymgr_certificate_request);
kore_seccomp_drop();
kore_seccomp_filter("keymgr", &filter_keymgr, 70LL);
kore_worker_privsep();
if ( kore_rand_file )
{
func1();
func2();
}
else if ( !kore_quiet )
{
kore_log(4LL, "no rand_file location specified");
}
RAND_poll();
v0 = 0LL;
initialized = 1;
func11();
kore_worker_started();
while ( 1 )
{
v1 = kore_time_ms();
if ( (unsigned __int64)(v1 - v0) > 0x1B7740 )
{
RAND_poll();
v0 = v1;
}
v2 = kore_timer_next_run(v1);
kore_platform_event_wait(v2);
if ( sig_recv )
break;
LABEL_14:
v4 = kore_time_ms();
kore_timer_run(v4);
kore_connection_prune(0LL);
}
if ( sig_recv == 10 )
{
func11();
goto LABEL_13;
}
if ( sig_recv <= 10 )
{
if ( (unsigned int)(sig_recv - 2) <= 1 )
goto LABEL_11;
goto LABEL_13;
}
if ( sig_recv != 15 )
{
LABEL_13:
sig_recv = 0;
goto LABEL_14;
}
LABEL_11:
sig_recv = 0;
func10(1LL);
kore_platform_event_cleanup();
kore_connection_cleanup();
return net_cleanup();
}

original_code:
void
kore_keymgr_run(void)
{
 int quit;
 u_int64_t now, netwait, last_seed;
 if (kore_keymgr_active == 0)
  fatalx("%s: called with kore_keymgr_active == 0", __func__);
 quit = 0;
 kore_server_closeall();
 kore_module_cleanup();
 net_init();
 kore_timer_init();
 kore_connection_init();
 kore_platform_event_init();
 kore_msg_worker_init();
 kore_msg_register(2, keymgr_msg_recv);
 kore_msg_register(5, keymgr_entropy_request);
 kore_msg_register(8, keymgr_certificate_request);
 kore_seccomp_drop();
 kore_seccomp_filter("keymgr", filter_keymgr,
     (sizeof(filter_keymgr) / sizeof(filter_keymgr[0])));
 kore_worker_privsep();
 if (kore_rand_file != ((void *)0)) {
  keymgr_load_randfile();
  keymgr_save_randfile();
 } else if (!kore_quiet) {
  kore_log(4, "no rand_file location specified");
 }
 RAND_poll();
 last_seed = 0;
 initialized = 1;
 keymgr_reload();
 kore_worker_started();
 while (quit != 1) {
  now = kore_time_ms();
  if ((now - last_seed) > (1800 * 1000)) {
   RAND_poll();
   last_seed = now;
  }
  netwait = kore_timer_next_run(now);
  kore_platform_event_wait(netwait);
  if (sig_recv != 0) {
   switch (sig_recv) {
   case 3:
   case 2:
   case 15:
    quit = 1;
    break;
   case 10:
    keymgr_reload();
    break;
   default:
    break;
   }
   sig_recv = 0;
  }
  if (quit)
   break;
  now = kore_time_ms();
  kore_timer_run(now);
  kore_connection_prune(0);
 }
 kore_keymgr_cleanup(1);
 kore_platform_event_cleanup();
 kore_connection_cleanup();
 net_cleanup();
}

canonical_original_code:
void kore_keymgr_run()
{
 int quit;
 unsigned int now; unsigned int netwait; unsigned int last_seed;
 if (kore_keymgr_active == 0)
  fatalx("%s: called with kore_keymgr_active == 0", __func__);
 quit = 0;
 kore_server_closeall();
 kore_module_cleanup();
 net_init();
 kore_timer_init();
 kore_connection_init();
 kore_platform_event_init();
 kore_msg_worker_init();
 kore_msg_register(2, keymgr_msg_recv);
 kore_msg_register(5, keymgr_entropy_request);
 kore_msg_register(8, keymgr_certificate_request);
 kore_seccomp_drop();
 kore_seccomp_filter("keymgr", filter_keymgr,
     (sizeof(filter_keymgr) / sizeof(filter_keymgr[0])));
 kore_worker_privsep();
 if (kore_rand_file != ((void *)0)) {
  keymgr_load_randfile();
  keymgr_save_randfile();
 } else if (!kore_quiet) {
  kore_log(4, "no rand_file location specified");
 }
 RAND_poll();
 last_seed = 0;
 initialized = 1;
 keymgr_reload();
 kore_worker_started();
 while (quit != 1) {
  now = kore_time_ms();
  if ((now - last_seed) > (1800 * 1000)) {
   RAND_poll();
   last_seed = now;
  }
  netwait = kore_timer_next_run(now);
  kore_platform_event_wait(netwait);
  if (sig_recv != 0) {
   switch (sig_recv) {
   case 3:
   case 2:
   case 15:
    quit = 1;
    break;
   case 10:
    keymgr_reload();
    break;
   default:
    break;
   }
   sig_recv = 0;
  }
  if (quit)
   break;
  now = kore_time_ms();
  kore_timer_run(now);
  kore_connection_prune(0);
 }
 kore_keymgr_cleanup(1);
 kore_platform_event_cleanup();
 kore_connection_cleanup();
 net_cleanup();
}

variable_types: quit int

variable_types:
now
unsigned int

variable_types:
netwait
unsigned int

return_type: void


decompiled_code:
unsigned __int64 fileref_timer_prime()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
if ( !ref_timer )
ref_timer = kore_timer_add(fileref_expiration_check, 10000LL, 0LL, 0LL);
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func0()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
if ( !ref_timer )
ref_timer = kore_timer_add(fileref_expiration_check, 10000LL, 0LL, 0LL);
return v1 - __readfsqword(0x28u);
}

original_code:
static void
fileref_timer_prime(void)
{
 if (ref_timer != ((void *)0))
  return;
 ref_timer = kore_timer_add(fileref_expiration_check, 10000, ((void *)0), 0);
}

canonical_original_code:
static void fileref_timer_prime()
{
 if (ref_timer != ((void *)0))
  return;
 ref_timer = kore_timer_add(fileref_expiration_check, 10000, ((void *)0), 0);
}


original_code:
static void
fileref_drop(struct kore_fileref *ref)
{
 if (!(ref->flags & 0x1000))
  do { if (((ref)->list.tqe_next) != ((void *)0)) (ref)->list.tqe_next->list.tqe_prev = (ref)->list.tqe_prev; else (&refs)->tqh_last = (ref)->list.tqe_prev; *(ref)->list.tqe_prev = (ref)->list.tqe_next; } while ( 0);
 kore_free(ref->path);
 close(ref->fd);
 kore_pool_put(&ref_pool, ref);
}

variable_types: struct kore_fileref { int cnt; int flags; int ontls; long size; char *path; unsigned int mtime; long mtime_sec; unsigned int expiration; void *base; int fd; struct { struct kore_fileref *tqe_next; struct kore_fileref **tqe_prev; } list; } *


decompiled_code:
unsigned __int64 __fastcall fileref_expiration_check(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rdi
__int64 v5; // rax
unsigned __int64 v6; // [rsp+8h] [rbp-20h]

v2 = refs;
v6 = __readfsqword(0x28u);
if ( !refs )
goto LABEL_8;
do
{
v3 = v2;
v2 = *(_QWORD *)(v2 + 72);
if ( !*(_DWORD *)v3 && *(_QWORD *)(v3 + 48) <= a2 )
fileref_drop(v3);
}
while ( v2 );
if ( !refs )
{
LABEL_8:
v5 = ref_timer;
ref_timer = 0LL;
*(_DWORD *)(v5 + 16) |= 1u;
}
return v6 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rdi
__int64 v5; // rax
unsigned __int64 v6; // [rsp+8h] [rbp-20h]

v2 = refs;
v6 = __readfsqword(0x28u);
if ( !refs )
goto LABEL_8;
do
{
v3 = v2;
v2 = *(_QWORD *)(v2 + 72);
if ( !*(_DWORD *)v3 && *(_QWORD *)(v3 + 48) <= a2 )
func1(v3);
}
while ( v2 );
if ( !refs )
{
LABEL_8:
v5 = ref_timer;
ref_timer = 0LL;
*(_DWORD *)(v5 + 16) |= 1u;
}
return v6 - __readfsqword(0x28u);
}

original_code:
static void
fileref_expiration_check(void *arg, u_int64_t now)
{
 struct kore_fileref *ref, *next;
 for (ref = ((&refs)->tqh_first); ref != ((void *)0); ref = next) {
  next = ((ref)->list.tqe_next);
  if (ref->cnt != 0)
   continue;
  if (ref->expiration > now)
   continue;
  fileref_drop(ref);
 }
 if (((&refs)->tqh_first == ((void *)0))) {
  ref_timer->flags |= 0x01;
  ref_timer = ((void *)0);
 }
}

canonical_original_code:
static void fileref_expiration_check(void *arg, unsigned int now)
{
 struct kore_fileref *ref; struct kore_fileref *next;
 for (ref = ((&refs)->tqh_first); ref != ((void *)0); ref = next) {
  next = ((ref)->list.tqe_next);
  if (ref->cnt != 0)
   continue;
  if (ref->expiration > now)
   continue;
  fileref_drop(ref);
 }
 if (((&refs)->tqh_first == ((void *)0))) {
  ref_timer->flags |= 0x01;
  ref_timer = ((void *)0);
 }
}

variable_types:
arg
void *

user_defined_types: struct kore_fileref { int cnt; int flags; int ontls; long size; char *path; unsigned int mtime; long mtime_sec; unsigned int expiration; void *base; int fd; struct { struct kore_fileref *tqe_next; struct kore_fileref **tqe_prev; } list; }


decompiled_code:
__int64 __fastcall fileref_soft_remove(_DWORD *a1)
{
int v1; // eax
__int64 v2; // rdx
_QWORD *v3; // rcx
__int64 result; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
v1 = a1[1];
if ( (v1 & 0x1000) != 0 )
fatal("fileref_soft_remove: %p already removed", a1);
v2 = *((_QWORD *)a1 + 9);
v3 = (_QWORD *)*((_QWORD *)a1 + 10);
if ( v2 )
*(_QWORD *)(v2 + 80) = v3;
else
qword_678 = *((_QWORD *)a1 + 10);
BYTE1(v1) |= 0x10u;
*v3 = v2;
a1[1] = v1;
if ( *a1 )
result = v5 - __readfsqword(0x28u);
else
result = fileref_drop((__int64)a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1)
{
int v1; // eax
__int64 v2; // rdx
_QWORD *v3; // rcx
__int64 result; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
v1 = a1[1];
if ( (v1 & 0x1000) != 0 )
fatal("fileref_soft_remove: %p already removed", a1);
v2 = *((_QWORD *)a1 + 9);
v3 = (_QWORD *)*((_QWORD *)a1 + 10);
if ( v2 )
*(_QWORD *)(v2 + 80) = v3;
else
qword_678 = *((_QWORD *)a1 + 10);
BYTE1(v1) |= 0x10u;
*v3 = v2;
a1[1] = v1;
if ( *a1 )
result = v5 - __readfsqword(0x28u);
else
result = func1((__int64)a1);
return result;
}

original_code:
static void
fileref_soft_remove(struct kore_fileref *ref)
{
 if (ref->flags & 0x1000)
  fatal("fileref_soft_remove: %p already removed", (void *)ref);
 do { if (((ref)->list.tqe_next) != ((void *)0)) (ref)->list.tqe_next->list.tqe_prev = (ref)->list.tqe_prev; else (&refs)->tqh_last = (ref)->list.tqe_prev; *(ref)->list.tqe_prev = (ref)->list.tqe_next; } while ( 0);
 ref->flags |= 0x1000;
 if (ref->cnt == 0)
  fileref_drop(ref);
}


decompiled_code:
__int64 kore_fileref_init()
{
refs = 0LL;
qword_678 = (__int64)&refs;
return kore_pool_init(&ref_pool, "ref_pool", 88LL, 100LL);
}

canonical_decompiled_code:
__int64 func4()
{
refs = 0LL;
qword_678 = (__int64)&refs;
return kore_pool_init(&ref_pool, "ref_pool", 88LL, 100LL);
}

original_code:
void
kore_fileref_init(void)
{
 do { (&refs)->tqh_first = ((void *)0); (&refs)->tqh_last = &(&refs)->tqh_first; } while ( 0);
 kore_pool_init(&ref_pool, "ref_pool", sizeof(struct kore_fileref), 100);
}

canonical_original_code:
void kore_fileref_init()
{
 do { (&refs)->tqh_first = ((void *)0); (&refs)->tqh_last = &(&refs)->tqh_first; } while ( 0);
 kore_pool_init(&ref_pool, "ref_pool", sizeof(struct kore_fileref), 100);
}


decompiled_code:
__int64 __fastcall kore_fileref_get(char *s2, int a2)
{
__int64 v2; // r12
const char *v3; // rbp
__int64 v5; // rdx
_QWORD *v6; // rax
__int64 v7; // rax
int v8; // edi
char *v9; // rax
_DWORD *v10; // rdi
struct stat v11; // [rsp+0h] [rbp-C8h] BYREF
unsigned __int64 v12; // [rsp+98h] [rbp-30h]

v2 = refs;
v12 = __readfsqword(0x28u);
if ( !refs )
return v2;
while ( 1 )
{
v3 = *(const char **)(v2 + 24);
if ( !strcmp(v3, s2) && *(_DWORD *)(v2 + 8) == a2 )
break;
v2 = *(_QWORD *)(v2 + 72);
if ( !v2 )
return v2;
}
if ( stat(v3, &v11) == -1 )
{
v8 = *_errno_location();
if ( v8 != 2 )
{
v9 = strerror(v8);
kore_log(3LL, "stat(%s): %s", *(const char **)(v2 + 24), v9);
}
goto LABEL_16;
}
if ( *(_QWORD *)(v2 + 32) != 1000 * v11.st_mtim.tv_sec + v11.st_mtim.tv_nsec / 1000000 )
{
LABEL_16:
v10 = (_DWORD *)v2;
v2 = 0LL;
fileref_soft_remove(v10);
return v2;
}
v5 = *(_QWORD *)(v2 + 72);
++*(_DWORD *)v2;
v6 = *(_QWORD **)(v2 + 80);
if ( v5 )
{
*(_QWORD *)(v5 + 80) = v6;
v6 = *(_QWORD **)(v2 + 80);
}
else
{
qword_678 = *(_QWORD *)(v2 + 80);
}
*v6 = v5;
v7 = refs;
*(_QWORD *)(v2 + 72) = refs;
if ( v7 )
*(_QWORD *)(v7 + 80) = v2 + 72;
else
qword_678 = v2 + 72;
refs = v2;
*(_QWORD *)(v2 + 80) = &refs;
return v2;
}

original_code:
struct kore_fileref *
kore_fileref_get(const char *path, int ontls)
{
 struct stat st;
 struct kore_fileref *ref;
 u_int64_t mtime;
 for ((ref) = ((&refs)->tqh_first); (ref); (ref) = ((ref)->list.tqe_next)) {
  if (!strcmp(ref->path, path) && ref->ontls == ontls) {
   if (stat(ref->path, &st) == -1) {
    if ((*__errno_location ()) != 2) {
     kore_log(3, "stat(%s): %s",
         ref->path, strerror((*__errno_location ())));
    }
    fileref_soft_remove(ref);
    return (((void *)0));
   }
   mtime = ((u_int64_t)(st.st_mtim.tv_sec * 1000 +
       (st.st_mtim.tv_nsec / 1000000)));
   if (ref->mtime != mtime) {
    fileref_soft_remove(ref);
    return (((void *)0));
   }
   ref->cnt++;
   do { if (((ref)->list.tqe_next) != ((void *)0)) (ref)->list.tqe_next->list.tqe_prev = (ref)->list.tqe_prev; else (&refs)->tqh_last = (ref)->list.tqe_prev; *(ref)->list.tqe_prev = (ref)->list.tqe_next; } while ( 0);
   do { if (((ref)->list.tqe_next = (&refs)->tqh_first) != ((void *)0)) (&refs)->tqh_first->list.tqe_prev = &(ref)->list.tqe_next; else (&refs)->tqh_last = &(ref)->list.tqe_next; (&refs)->tqh_first = (ref); (ref)->list.tqe_prev = &(&refs)->tqh_first; } while ( 0);
   return (ref);
  }
 }
 return (((void *)0));
}

canonical_original_code:
struct kore_fileref *kore_fileref_get(char *path, int ontls)
{
 struct stat st;
 struct kore_fileref *ref;
 unsigned int mtime;
 for ((ref) = ((&refs)->tqh_first); (ref); (ref) = ((ref)->list.tqe_next)) {
  if (!strcmp(ref->path, path) && ref->ontls == ontls) {
   if (stat(ref->path, &st) == -1) {
    if ((*__errno_location ()) != 2) {
     kore_log(3, "stat(%s): %s",
         ref->path, strerror((*__errno_location ())));
    }
    fileref_soft_remove(ref);
    return (((void *)0));
   }
   mtime = ((u_int64_t)(st.st_mtim.tv_sec * 1000 +
       (st.st_mtim.tv_nsec / 1000000)));
   if (ref->mtime != mtime) {
    fileref_soft_remove(ref);
    return (((void *)0));
   }
   ref->cnt++;
   do { if (((ref)->list.tqe_next) != ((void *)0)) (ref)->list.tqe_next->list.tqe_prev = (ref)->list.tqe_prev; else (&refs)->tqh_last = (ref)->list.tqe_prev; *(ref)->list.tqe_prev = (ref)->list.tqe_next; } while ( 0);
   do { if (((ref)->list.tqe_next = (&refs)->tqh_first) != ((void *)0)) (&refs)->tqh_first->list.tqe_prev = &(ref)->list.tqe_next; else (&refs)->tqh_last = &(ref)->list.tqe_next; (&refs)->tqh_first = (ref); (ref)->list.tqe_prev = &(&refs)->tqh_first; } while ( 0);
   return (ref);
  }
 }
 return (((void *)0));
}

variable_types:
path
char *

variable_types: ontls int

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

variable_types:
mtime
unsigned int

return_type: struct kore_fileref { int cnt; int flags; int ontls; long size; char *path; unsigned int mtime; long mtime_sec; unsigned int expiration; void *base; int fd; struct { struct kore_fileref *tqe_next; struct kore_fileref **tqe_prev; } list; } *

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


decompiled_code:
__int64 __fastcall kore_fileref_release(_DWORD *a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v1 = *a1;
v2 = (unsigned int)(*a1 - 1);
*a1 = v2;
if ( (int)v2 < 0 )
fatal("kore_fileref_release: cnt < 0 (%p:%d)", a1, v2);
if ( v1 == 1 )
{
if ( (*((_BYTE *)a1 + 5) & 0x10) != 0 )
return fileref_drop((__int64)a1);
*((_QWORD *)a1 + 6) = kore_time_ms() + 30000;
}
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v1 = *a1;
v2 = (unsigned int)(*a1 - 1);
*a1 = v2;
if ( (int)v2 < 0 )
fatal("kore_fileref_release: cnt < 0 (%p:%d)", a1, v2);
if ( v1 == 1 )
{
if ( (*((_BYTE *)a1 + 5) & 0x10) != 0 )
return func1((__int64)a1);
*((_QWORD *)a1 + 6) = kore_time_ms() + 30000;
}
return v4 - __readfsqword(0x28u);
}

original_code:
void
kore_fileref_release(struct kore_fileref *ref)
{
 ref->cnt--;
 if (ref->cnt < 0) {
  fatal("kore_fileref_release: cnt < 0 (%p:%d)",
      (void *)ref, ref->cnt);
 }
 if (ref->cnt == 0) {
  if (ref->flags & 0x1000)
   fileref_drop(ref);
  else
   ref->expiration = kore_time_ms() + (1000 * 30);
 }
}


decompiled_code:
unsigned __int64 kore_domain_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
memset(&cached, 0, 0x80uLL);
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func0()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
memset(&cached, 0, 0x80uLL);
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_domain_init(void)
{
 int i;
 for (i = 0; i < 16; i++)
  cached[i] = ((void *)0);
}

canonical_original_code:
void kore_domain_init()
{
 int i;
 for (i = 0; i < 16; i++)
  cached[i] = ((void *)0);
}

variable_types: i int


decompiled_code:
__int64 kore_domain_cleanup()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
void
kore_domain_cleanup(void)
{
}

canonical_original_code:
void kore_domain_cleanup()
{
}


decompiled_code:
__int64 kore_accesslog_worker_init()
{
return kore_domain_closelogs();
}

canonical_decompiled_code:
__int64 func4()
{
return kore_domain_closelogs();
}

original_code:
void
kore_accesslog_worker_init(void)
{
 kore_domain_closelogs();
}

canonical_original_code:
void kore_accesslog_worker_init()
{
 kore_domain_closelogs();
}


decompiled_code:
__int64 __fastcall kore_accesslog_run(__int64 a1, __int64 a2)
{
int v2; // eax

v2 = ticks_0++;
return kore_accesslog_gather(a1, a2, __ROR4__(-1030792151 * v2 + 85899344, 2) <= 0x28F5C28u);
}

original_code:
void
kore_accesslog_run(void *arg, u_int64_t now)
{
 static int ticks = 0;
 kore_accesslog_gather(arg, now, ticks++ % 100 ? 0 : 1);
}

canonical_original_code:
void kore_accesslog_run(void *arg, unsigned int now)
{
 static int ticks = 0;
 kore_accesslog_gather(arg, now, ticks++ % 100 ? 0 : 1);
}

variable_types: ticks int


decompiled_code:
__int64 net_cleanup()
{
return kore_pool_cleanup(&nb_pool);
}

canonical_decompiled_code:
__int64 func4()
{
return kore_pool_cleanup(&nb_pool);
}

original_code:
void
net_cleanup(void)
{
 kore_pool_cleanup(&nb_pool);
}

canonical_original_code:
void net_cleanup()
{
 kore_pool_cleanup(&nb_pool);
}


decompiled_code:
__int16 __fastcall net_read16(__int16 *a1)
{
return _bswap_16(*a1);
}

canonical_decompiled_code:
__int16 __fastcall func18(__int16 *a1)
{
return func0(*a1);
}

original_code:
u_int16_t
net_read16(u_int8_t *b)
{
 u_int16_t r;
 r = *(u_int16_t *)b;
 return (ntohs(r));
}

canonical_original_code:
unsigned int net_read16(unsigned int *b)
{
 unsigned int r;
 r = *(unsigned int *)b;
 return (ntohs(r));
}

variable_types:
b
unsigned int *

variable_types:
r
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall net_read32(unsigned int *a1)
{
return _bswap_32(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func19(unsigned int *a1)
{
return func1(*a1);
}

original_code:
u_int32_t
net_read32(u_int8_t *b)
{
 u_int32_t r;
 r = *(u_int32_t *)b;
 return (ntohl(r));
}

canonical_original_code:
unsigned int net_read32(unsigned int *b)
{
 unsigned int r;
 r = *(unsigned int *)b;
 return (ntohl(r));
}


decompiled_code:
unsigned __int64 __fastcall net_write16(__int16 *a1, __int16 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = _bswap_16(a2);
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func20(__int16 *a1, __int16 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = func0(a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
net_write16(u_int8_t *p, u_int16_t n)
{
 u_int16_t r;
 r = htons(n);
 memcpy(p, &r, sizeof(r));
}

canonical_original_code:
void net_write16(unsigned int *p, unsigned int n)
{
 unsigned int r;
 r = htons(n);
 memcpy(p, &r, sizeof(r));
}

variable_types:
p
unsigned int *

variable_types:
n
unsigned int


decompiled_code:
unsigned __int64 __fastcall net_write32(_DWORD *a1, unsigned int a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = _bswap_32(a2);
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func21(_DWORD *a1, unsigned int a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = func1(a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
net_write32(u_int8_t *p, u_int32_t n)
{
 u_int32_t r;
 r = htonl(n);
 memcpy(p, &r, sizeof(r));
}

canonical_original_code:
void net_write32(unsigned int *p, unsigned int n)
{
 unsigned int r;
 r = htonl(n);
 memcpy(p, &r, sizeof(r));
}


decompiled_code:
unsigned __int64 __fastcall net_read64(unsigned __int64 *a1)
{
return _bswap_64(*a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func22(unsigned __int64 *a1)
{
return func2(*a1);
}

original_code:
u_int64_t
net_read64(u_int8_t *b)
{
 u_int64_t r;
 r = *(u_int64_t *)b;
 return (__bswap_64 (r));
}

canonical_original_code:
unsigned int net_read64(unsigned int *b)
{
 unsigned int r;
 r = *(unsigned int *)b;
 return (__bswap_64 (r));
}


decompiled_code:
unsigned __int64 __fastcall net_write64(unsigned __int64 *a1, unsigned __int64 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = _bswap_64(a2);
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func23(unsigned __int64 *a1, unsigned __int64 a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a1 = func2(a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
net_write64(u_int8_t *p, u_int64_t n)
{
 u_int64_t r;
 r = __bswap_64 (n);
 memcpy(p, &r, sizeof(r));
}

canonical_original_code:
void net_write64(unsigned int *p, unsigned int n)
{
 unsigned int r;
 r = __bswap_64 (n);
 memcpy(p, &r, sizeof(r));
}


decompiled_code:
__int64 kore_connection_cleanup()
{
kore_connection_prune(1);
return kore_pool_cleanup(&connection_pool);
}

canonical_decompiled_code:
__int64 func5()
{
func4(1);
return kore_pool_cleanup(&connection_pool);
}

original_code:
void
kore_connection_cleanup(void)
{
 kore_connection_prune(1);
 kore_pool_cleanup(&connection_pool);
}

canonical_original_code:
void kore_connection_cleanup()
{
 kore_connection_prune(1);
 kore_pool_cleanup(&connection_pool);
}


decompiled_code:
__int64 __fastcall kore_connection_nonblock(int fd, int a2)
{
unsigned int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 == -1 )
return 0LL;
BYTE1(v2) |= 8u;
if ( fcntl(fd, 4, v2) == -1 )
return 0LL;
if ( a2 )
{
if ( !(unsigned int)kore_sockopt((unsigned int)fd, 6LL, 1LL) )
kore_log(5LL, "failed to set TCP_NODELAY on %d", (unsigned int)fd);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd, int a2)
{
unsigned int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 == -1 )
return 0LL;
BYTE1(v2) |= 8u;
if ( fcntl(fd, 4, v2) == -1 )
return 0LL;
if ( a2 )
{
if ( !(unsigned int)kore_sockopt((unsigned int)fd, 6LL, 1LL) )
kore_log(5LL, "failed to set TCP_NODELAY on %d", (unsigned int)fd);
}
return 1LL;
}

original_code:
int
kore_connection_nonblock(int fd, int nodelay)
{
 int flags;
 if ((flags = fcntl(fd, 3, 0)) == -1)
  return (0);
 flags |= 04000;
 if (fcntl(fd, 4, flags) == -1)
  return (0);
 if (nodelay) {
  if (!kore_sockopt(fd, IPPROTO_TCP, 1)) {
   kore_log(5,
       "failed to set TCP_NODELAY on %d", fd);
  }
 }
 return (1);
}

variable_types: fd int

variable_types: nodelay int

variable_types: flags int

return_type: int


decompiled_code:
unsigned __int64 log_print(__int64 a1, __int64 a2, ...)
{
struct tm *v2; // rax
struct timespec v4; // [rsp+0h] [rbp-118h] BYREF
gcc_va_list va; // [rsp+18h] [rbp-100h] BYREF
char s[40]; // [rsp+30h] [rbp-E8h] BYREF
unsigned __int64 v7; // [rsp+58h] [rbp-C0h]

va_start(va, a2);
v7 = __readfsqword(0x28u);
clock_gettime(0, &v4);
v2 = gmtime(&v4.tv_sec);
if ( strftime(s, 0x20uLL, "%Y-%m-%d %H:%M:%S", v2) )
_fprintf_chk(fp, 1LL, "%s.%03ld UTC ", s, v4.tv_nsec / 1000000);
_vfprintf_chk(fp, 1LL, a2, va);
fflush(fp);
return v7 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func0(__int64 a1, __int64 a2, ...)
{
struct tm *v2; // rax
struct timespec v4; // [rsp+0h] [rbp-118h] BYREF
gcc_va_list va; // [rsp+18h] [rbp-100h] BYREF
char s[40]; // [rsp+30h] [rbp-E8h] BYREF
unsigned __int64 v7; // [rsp+58h] [rbp-C0h]

va_start(va, a2);
v7 = __readfsqword(0x28u);
clock_gettime(0, &v4);
v2 = gmtime(&v4.tv_sec);
if ( strftime(s, 0x20uLL, "%Y-%m-%d %H:%M:%S", v2) )
_fprintf_chk(fp, 1LL, "%s.%03ld UTC ", s, v4.tv_nsec / 1000000);
_vfprintf_chk(fp, 1LL, a2, va);
fflush(fp);
return v7 - __readfsqword(0x28u);
}

original_code:
static void
log_print(int prio, const char *fmt, ...)
{
 struct tm *t;
 struct timespec ts;
 va_list args;
 char tbuf[32];
 __builtin_va_start(args,fmt);
 switch (prio) {
 case 3:
 case 4:
 case 5:
 case 6:
 case 7:
  break;
 }
 (void)clock_gettime(0, &ts);
 t = gmtime(&ts.tv_sec);
 if (strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", t) > 0)
  fprintf(fp, "%s.%03ld UTC ", tbuf, ts.tv_nsec / 1000000);
 vfprintf(fp, fmt, args);
 fflush(fp);
 __builtin_va_end(args);
}

canonical_original_code:
static void log_print(int prio, char *fmt, ...)
{
 struct tm *t;
 struct timespec ts;
 __builtin_va_list args;
 char tbuf[32];
 __builtin_va_start(args,fmt);
 switch (prio) {
 case 3:
 case 4:
 case 5:
 case 6:
 case 7:
  break;
 }
 (void)clock_gettime(0, &ts);
 t = gmtime(&ts.tv_sec);
 if (strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", t) > 0)
  fprintf(fp, "%s.%03ld UTC ", tbuf, ts.tv_nsec / 1000000);
 vfprintf(fp, fmt, args);
 fflush(fp);
 __builtin_va_end(args);
}

variable_types: prio int

variable_types:
fmt
char *

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

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

variable_types:
tbuf
char [32]


decompiled_code:
__int64 kore_log_init()
{
fp = stdout;
if ( !kore_foreground )
openlog("kore", 9, 24);
return kore_msg_register(12LL, log_from_worker);
}

canonical_decompiled_code:
__int64 func1()
{
fp = stdout;
if ( !kore_foreground )
openlog("kore", 9, 24);
return kore_msg_register(12LL, log_from_worker);
}

original_code:
void
kore_log_init(void)
{
 const char *name = "kore";
 fp = stdout;
 if (!kore_foreground)
  openlog(name, 0x08 | 0x01, (3<<3));
 kore_msg_register(12, log_from_worker);
}

canonical_original_code:
void kore_log_init()
{
 char *name = "kore";
 fp = stdout;
 if (!kore_foreground)
  openlog(name, 0x08 | 0x01, (3<<3));
 kore_msg_register(12, log_from_worker);
}

variable_types:
name
char *


decompiled_code:
unsigned __int64 __fastcall kore_log_file(const char *a1)
{
int *v2; // rax
char *v3; // rax
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
fp = fopen(a1, "a");
if ( !fp )
{
fp = stdout;
v2 = _errno_location();
v3 = strerror(*v2);
fatal("fopen(%s): %s", a1, v3);
}
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(const char *a1)
{
int *v2; // rax
char *v3; // rax
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
fp = fopen(a1, "a");
if ( !fp )
{
fp = stdout;
v2 = _errno_location();
v3 = strerror(*v2);
fatal("fopen(%s): %s", a1, v3);
}
return v4 - __readfsqword(0x28u);
}

original_code:
void
kore_log_file(const char *path)
{
 if ((fp = fopen(path, "a")) == ((void *)0)) {
  fp = stdout;
  fatal("fopen(%s): %s", path, strerror((*__errno_location ())));
 }
}

canonical_original_code:
void kore_log_file(char *path)
{
 if ((fp = fopen(path, "a")) == ((void *)0)) {
  fp = stdout;
  fatal("fopen(%s): %s", path, strerror((*__errno_location ())));
 }
}

variable_types:
path
char *


decompiled_code:
unsigned __int64 kore_log(unsigned int a1, __int64 a2, ...)
{
__int16 v2; // ax
const char *v4; // rdx
__int64 v5[3]; // [rsp+0h] [rbp-138h] BYREF
__int64 v6; // [rsp+18h] [rbp-120h]
__int64 v7[4]; // [rsp+20h] [rbp-118h] BYREF
__int128 v8; // [rsp+40h] [rbp-F8h] BYREF
gcc_va_list va; // [rsp+50h] [rbp-E8h] BYREF
unsigned __int64 v10; // [rsp+68h] [rbp-D0h]

va_start(va, a2);
v10 = __readfsqword(0x28u);
kore_buf_init(v5, 128LL);
kore_buf_appendv(v5, a2, va);
if ( worker )
{
kore_buf_init(v7, v6 + 16);
v8 = 0LL;
v2 = *worker;
LODWORD(v8) = a1;
WORD2(v8) = v2;
*((_QWORD *)&v8 + 1) = v6;
kore_buf_append(v7, &v8, 16LL);
kore_buf_append(v7, v5[0], v6);
kore_msg_send(1000LL, 12LL, v7[0], v7[3]);
kore_buf_cleanup(v7);
}
else
{
v4 = (const char *)kore_buf_stringify(v5, 0LL);
if ( !kore_foreground && fp == stdout )
_syslog_chk(a1, 1LL, "proc=[parent] log=[%s]", v4);
else
log_print(a1, (__int64)"proc=[parent] log=[%s]\n", v4);
}
kore_buf_cleanup(v5);
return v10 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func3(unsigned int a1, __int64 a2, ...)
{
__int16 v2; // ax
const char *v4; // rdx
__int64 v5[3]; // [rsp+0h] [rbp-138h] BYREF
__int64 v6; // [rsp+18h] [rbp-120h]
__int64 v7[4]; // [rsp+20h] [rbp-118h] BYREF
__int128 v8; // [rsp+40h] [rbp-F8h] BYREF
gcc_va_list va; // [rsp+50h] [rbp-E8h] BYREF
unsigned __int64 v10; // [rsp+68h] [rbp-D0h]

va_start(va, a2);
v10 = __readfsqword(0x28u);
kore_buf_init(v5, 128LL);
kore_buf_appendv(v5, a2, va);
if ( worker )
{
kore_buf_init(v7, v6 + 16);
v8 = 0LL;
v2 = *worker;
LODWORD(v8) = a1;
WORD2(v8) = v2;
*((_QWORD *)&v8 + 1) = v6;
kore_buf_append(v7, &v8, 16LL);
kore_buf_append(v7, v5[0], v6);
kore_msg_send(1000LL, 12LL, v7[0], v7[3]);
kore_buf_cleanup(v7);
}
else
{
v4 = (const char *)kore_buf_stringify(v5, 0LL);
if ( !kore_foreground && fp == stdout )
_syslog_chk(a1, 1LL, "proc=[parent] log=[%s]", v4);
else
func0(a1, (__int64)"proc=[parent] log=[%s]\n", v4);
}
kore_buf_cleanup(v5);
return v10 - __readfsqword(0x28u);
}

original_code:
void
kore_log(int prio, const char *fmt, ...)
{
 va_list args;
 const char *str;
 struct kore_wlog wlog;
 struct kore_buf buf, pkt;
 kore_buf_init(&buf, 128);
 __builtin_va_start(args,fmt);
 kore_buf_appendv(&buf, fmt, args);
 __builtin_va_end(args);
 if (worker != ((void *)0)) {
  kore_buf_init(&pkt, sizeof(wlog) + buf.offset);
  memset(&wlog, 0, sizeof(wlog));
  wlog.prio = prio;
  wlog.wid = worker->id;
  wlog.loglen = buf.offset;
  kore_buf_append(&pkt, &wlog, sizeof(wlog));
  kore_buf_append(&pkt, buf.data, buf.offset);
  kore_msg_send(1000, 12,
      pkt.data, pkt.offset);
  kore_buf_cleanup(&pkt);
 } else {
  str = kore_buf_stringify(&buf, ((void *)0));
  if (kore_foreground || fp != stdout)
   log_print(prio, "proc=[parent] log=[%s]\n", str);
  else
   syslog(prio, "proc=[parent] log=[%s]", str);
 }
 kore_buf_cleanup(&buf);
}

canonical_original_code:
void kore_log(int prio, char *fmt, ...)
{
 __builtin_va_list args;
 char *str;
 struct kore_wlog wlog;
 struct kore_buf buf; struct kore_buf pkt;
 kore_buf_init(&buf, 128);
 __builtin_va_start(args,fmt);
 kore_buf_appendv(&buf, fmt, args);
 __builtin_va_end(args);
 if (worker != ((void *)0)) {
  kore_buf_init(&pkt, sizeof(wlog) + buf.offset);
  memset(&wlog, 0, sizeof(wlog));
  wlog.prio = prio;
  wlog.wid = worker->id;
  wlog.loglen = buf.offset;
  kore_buf_append(&pkt, &wlog, sizeof(wlog));
  kore_buf_append(&pkt, buf.data, buf.offset);
  kore_msg_send(1000, 12,
      pkt.data, pkt.offset);
  kore_buf_cleanup(&pkt);
 } else {
  str = kore_buf_stringify(&buf, ((void *)0));
  if (kore_foreground || fp != stdout)
   log_print(prio, "proc=[parent] log=[%s]\n", str);
  else
   syslog(prio, "proc=[parent] log=[%s]", str);
 }
 kore_buf_cleanup(&buf);
}

variable_types:
str
char *

variable_types: struct kore_wlog { int prio; unsigned int wid; unsigned long loglen; char logmsg[]; }

variable_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; }


decompiled_code:
__int64 __fastcall log_from_worker(__int64 a1, unsigned int *a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 8);
if ( v2 <= 0xF )
return kore_log(5u, (__int64)"too short worker log received (%zu < %zu)", v2, 0x10uLL);
v3 = kore_worker_name(*((unsigned __int16 *)a2 + 2));
if ( !kore_foreground && fp == stdout )
result = _syslog_chk(*a2, 1LL, "proc=%s log=[%.*s]", v3, a2[2], a2 + 4);
else
result = log_print(*a2, (__int64)"proc=%s log=[%.*s]\n", v3, a2[2], a2 + 4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int *a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 8);
if ( v2 <= 0xF )
return func3(5u, (__int64)"too short worker log received (%zu < %zu)", v2, 0x10uLL);
v3 = kore_worker_name(*((unsigned __int16 *)a2 + 2));
if ( !kore_foreground && fp == stdout )
result = _syslog_chk(*a2, 1LL, "proc=%s log=[%.*s]", v3, a2[2], a2 + 4);
else
result = func0(*a2, (__int64)"proc=%s log=[%.*s]\n", v3, a2[2], a2 + 4);
return result;
}

original_code:
static void
log_from_worker(struct kore_msg *msg, const void *data)
{
 const char *name;
 const struct kore_wlog *wlog;
 if (msg->length < sizeof(*wlog)) {
  kore_log(5,
      "too short worker log received (%zu < %zu)",
      msg->length, sizeof(*wlog));
  return;
 }
 wlog = data;
 name = kore_worker_name(wlog->wid);
 if (kore_foreground || fp != stdout) {
  log_print(wlog->prio, "proc=%s log=[%.*s]\n",
      name, (int)wlog->loglen, wlog->logmsg);
 } else {
  syslog(wlog->prio, "proc=%s log=[%.*s]",
      name, (int)wlog->loglen, wlog->logmsg);
 }
}

canonical_original_code:
static void log_from_worker(struct kore_msg *msg, void *data)
{
 char *name;
 struct kore_wlog *wlog;
 if (msg->length < sizeof(*wlog)) {
  kore_log(5,
      "too short worker log received (%zu < %zu)",
      msg->length, sizeof(*wlog));
  return;
 }
 wlog = data;
 name = kore_worker_name(wlog->wid);
 if (kore_foreground || fp != stdout) {
  log_print(wlog->prio, "proc=%s log=[%.*s]\n",
      name, (int)wlog->loglen, wlog->logmsg);
 } else {
  syslog(wlog->prio, "proc=%s log=[%.*s]",
      name, (int)wlog->loglen, wlog->logmsg);
 }
}

variable_types: struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *

variable_types:
data
void *

variable_types: struct kore_wlog { int prio; unsigned int wid; unsigned long loglen; char logmsg[]; } *


decompiled_code:
unsigned __int64 __fastcall websocket_frame_build(__int64 a1, char a2, __int64 a3, unsigned __int64 a4)
{
char v7[17]; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned __int8 v8; // [rsp+1Dh] [rbp-2Bh] BYREF
char v9[2]; // [rsp+1Eh] [rbp-2Ah] BYREF
char v10[8]; // [rsp+20h] [rbp-28h] BYREF
unsigned __int64 v11; // [rsp+28h] [rbp-20h]

v7[0] = a2;
v11 = __readfsqword(0x28u);
if ( a4 <= 0x7D )
v8 = a4;
else
v8 = (a4 > 0xFFFF) + 126;
v7[0] |= 0x80u;
kore_buf_append(a1, v7, 1LL);
v8 &= 0x7Fu;
kore_buf_append(a1, &v8, 1LL);
if ( v8 > 0x7Du )
{
if ( v8 == 126 )
{
net_write16(v9, (unsigned __int16)a4);
kore_buf_append(a1, v9, 2LL);
}
else if ( v8 == 127 )
{
net_write64(v10, a4);
kore_buf_append(a1, v10, 8LL);
}
}
if ( a3 && a4 )
kore_buf_append(a1, a3, a4);
return v11 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1, char a2, __int64 a3, unsigned __int64 a4)
{
char v7[17]; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned __int8 v8; // [rsp+1Dh] [rbp-2Bh] BYREF
char v9[2]; // [rsp+1Eh] [rbp-2Ah] BYREF
char v10[8]; // [rsp+20h] [rbp-28h] BYREF
unsigned __int64 v11; // [rsp+28h] [rbp-20h]

v7[0] = a2;
v11 = __readfsqword(0x28u);
if ( a4 <= 0x7D )
v8 = a4;
else
v8 = (a4 > 0xFFFF) + 126;
v7[0] |= 0x80u;
kore_buf_append(a1, v7, 1LL);
v8 &= 0x7Fu;
kore_buf_append(a1, &v8, 1LL);
if ( v8 > 0x7Du )
{
if ( v8 == 126 )
{
net_write16(v9, (unsigned __int16)a4);
kore_buf_append(a1, v9, 2LL);
}
else if ( v8 == 127 )
{
net_write64(v10, a4);
kore_buf_append(a1, v10, 8LL);
}
}
if ( a3 && a4 )
kore_buf_append(a1, a3, a4);
return v11 - __readfsqword(0x28u);
}

original_code:
static void
websocket_frame_build(struct kore_buf *frame, u_int8_t op, const void *data,
    size_t len)
{
 u_int8_t len_1;
 u_int16_t len16;
 u_int64_t len64;
 if (len > 125) {
  if (len <= (0x7fff * 2 + 1))
   len_1 = 126;
  else
   len_1 = 127;
 } else {
  len_1 = len;
 }
 op |= (1 << 7);
 kore_buf_append(frame, &op, sizeof(op));
 len_1 &= ~(1 << 7);
 kore_buf_append(frame, &len_1, sizeof(len_1));
 if (len_1 > 125) {
  switch (len_1) {
  case 126:
   net_write16((u_int8_t *)&len16, len);
   kore_buf_append(frame, &len16, sizeof(len16));
   break;
  case 127:
   net_write64((u_int8_t *)&len64, len);
   kore_buf_append(frame, &len64, sizeof(len64));
   break;
  }
 }
 if (data != ((void *)0) && len > 0)
  kore_buf_append(frame, data, len);
}

canonical_original_code:
static void websocket_frame_build(struct kore_buf *frame, unsigned int op, void *data, unsigned long len)
{
 unsigned int len_1;
 unsigned int len16;
 unsigned int len64;
 if (len > 125) {
  if (len <= (0x7fff * 2 + 1))
   len_1 = 126;
  else
   len_1 = 127;
 } else {
  len_1 = len;
 }
 op |= (1 << 7);
 kore_buf_append(frame, &op, sizeof(op));
 len_1 &= ~(1 << 7);
 kore_buf_append(frame, &len_1, sizeof(len_1));
 if (len_1 > 125) {
  switch (len_1) {
  case 126:
   net_write16((unsigned int *)&len16, len);
   kore_buf_append(frame, &len16, sizeof(len16));
   break;
  case 127:
   net_write64((unsigned int *)&len64, len);
   kore_buf_append(frame, &len64, sizeof(len64));
   break;
  }
 }
 if (data != ((void *)0) && len > 0)
  kore_buf_append(frame, data, len);
}

variable_types: struct kore_buf { unsigned int *data; int flags; unsigned long length; unsigned long offset; } *

variable_types:
op
unsigned int

variable_types:
len
unsigned long


decompiled_code:
unsigned __int64 kore_auth_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
auth_list = 0LL;
*(&auth_list + 1) = (__int64)&auth_list;
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func0()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
auth_list = 0LL;
*(&auth_list + 1) = (__int64)&auth_list;
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_auth_init(void)
{
 do { (&auth_list)->tqh_first = ((void *)0); (&auth_list)->tqh_last = &(&auth_list)->tqh_first; } while ( 0);
}

canonical_original_code:
void kore_auth_init()
{
 do { (&auth_list)->tqh_first = ((void *)0); (&auth_list)->tqh_last = &(&auth_list)->tqh_first; } while ( 0);
}


decompiled_code:
unsigned __int64 kore_module_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
modules = 0LL;
qword_718 = (__int64)&modules;
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func4()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
modules = 0LL;
qword_718 = (__int64)&modules;
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_module_init(void)
{
 do { (&modules)->tqh_first = ((void *)0); (&modules)->tqh_last = &(&modules)->tqh_first; } while ( 0);
}

canonical_original_code:
void kore_module_init()
{
 do { (&modules)->tqh_first = ((void *)0); (&modules)->tqh_last = &(&modules)->tqh_first; } while ( 0);
}


decompiled_code:
_BOOL8 kore_module_loaded()
{
return modules != 0;
}

canonical_decompiled_code:
_BOOL8 func9()
{
return modules != 0;
}

original_code:
int
kore_module_loaded(void)
{
 if (((&modules)->tqh_first == ((void *)0)))
  return (0);
 return (1);
}

canonical_original_code:
int kore_module_loaded()
{
 if (((&modules)->tqh_first == ((void *)0)))
  return (0);
 return (1);
}


usage

decompiled_code:
size_t usage()
{
return fwrite("usage: caches [-d] cache-descriptions-file memory-trace-file\n", 1uLL, 0x3EuLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
return fwrite("usage: caches [-d] cache-descriptions-file memory-trace-file\n", 1uLL, 0x3EuLL, stderr);
}

original_code:
void usage(void)
{
    fprintf (stderr,"usage: caches [-d] cache-descriptions-file  memory-trace-file\n");
}

canonical_original_code:
void usage()
{
    fprintf (stderr,"usage: caches [-d] cache-descriptions-file  memory-trace-file\n");
}


scanargs

decompiled_code:
__int64 __fastcall scanargs(__int64 a1)
{
__int64 result; // rax
unsigned __int8 *v2; // rbx

result = *(unsigned __int8 *)(a1 + 1);
if ( (_BYTE)result )
{
v2 = (unsigned __int8 *)(a1 + 1);
do
{
while ( 1 )
{
if ( (_BYTE)result != 68 )
{
fprintf(stderr, "caches: Bad option %c\n", (unsigned int)(char)result);
usage();
exit(1);
}
debug = 1;
debug_file = (__int64)fopen("DEBUG_LOG", "w");
if ( !debug_file )
break;
result = *++v2;
if ( !(_BYTE)result )
return result;
}
++v2;
fwrite("Cannot open DEBUG_LOG\n", 1uLL, 0x16uLL, stderr);
debug = 0;
result = *v2;
}
while ( (_BYTE)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 result; // rax
unsigned __int8 *v2; // rbx

result = *(unsigned __int8 *)(a1 + 1);
if ( (_BYTE)result )
{
v2 = (unsigned __int8 *)(a1 + 1);
do
{
while ( 1 )
{
if ( (_BYTE)result != 68 )
{
fprintf(stderr, "caches: Bad option %c\n", (unsigned int)(char)result);
func0();
exit(1);
}
debug = 1;
debug_file = (__int64)fopen("DEBUG_LOG", "w");
if ( !debug_file )
break;
result = *++v2;
if ( !(_BYTE)result )
return result;
}
++v2;
fwrite("Cannot open DEBUG_LOG\n", 1uLL, 0x16uLL, stderr);
debug = 0;
result = *v2;
}
while ( (_BYTE)result );
}
return result;
}

original_code:
void scanargs(String s)
{
    while (*++s != '\0')
        switch (*s)
            {
            case 'D':
                debug = 1;
                if (debug)
                    {
                        debug_file = fopen("DEBUG_LOG", "w");
                        if (debug_file == ((void *)0))
                            {
                                fprintf(stderr, "Cannot open DEBUG_LOG\n");
                                debug = 0;
                            }
                    }
                break;
            default:
                fprintf (stderr,"caches: Bad option %c\n", *s);
                usage();
                exit(1);
            }
}

canonical_original_code:
void scanargs(char *s)
{
    while (*++s != '\0')
        switch (*s)
            {
            case 'D':
                debug = 1;
                if (debug)
                    {
                        debug_file = fopen("DEBUG_LOG", "w");
                        if (debug_file == ((void *)0))
                            {
                                fprintf(stderr, "Cannot open DEBUG_LOG\n");
                                debug = 0;
                            }
                    }
                break;
            default:
                fprintf (stderr,"caches: Bad option %c\n", *s);
                usage();
                exit(1);
            }
}

variable_types:
s
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int v4; // ebx
const char *v5; // rdi

v3 = argv + 1;
v4 = argc - 1;
if ( argc - 1 > 1 )
{
while ( 1 )
{
v5 = *v3;
if ( **v3 != 45 )
break;
scanargs((__int64)v5);
--v4;
++v3;
if ( v4 == 1 )
goto LABEL_7;
}
if ( v4 == 2 )
{
Read_Cache_Descriptions(v5, argv, envp);
Init_Caches();
Simulate_Caches(v3[1]);
Print_Cache_Statistics();
Delete_Caches();
exit(0);
}
}
LABEL_7:
usage();
exit(-1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int v4; // ebx
const char *v5; // rdi

v3 = argv + 1;
v4 = argc - 1;
if ( argc - 1 > 1 )
{
while ( 1 )
{
v5 = *v3;
if ( **v3 != 45 )
break;
func1((__int64)v5);
--v4;
++v3;
if ( v4 == 1 )
goto LABEL_7;
}
if ( v4 == 2 )
{
Read_Cache_Descriptions(v5, argv, envp);
Init_Caches();
Simulate_Caches(v3[1]);
Print_Cache_Statistics();
Delete_Caches();
exit(0);
}
}
LABEL_7:
func0();
exit(-1);
}

original_code:
int main(int argc, String *argv)
{
    argc--, argv++;
    while ((argc > 1) && (**argv == '-'))
        {
            scanargs(*argv);
            argc--, argv++;
        }
    if (argc != 2)
        {
            usage();
            exit(-1);
        }
    Read_Cache_Descriptions(argv[0]);
    Init_Caches();
    Simulate_Caches(argv[1]);
    Print_Cache_Statistics();
    Delete_Caches();
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    argc--, argv++;
    while ((argc > 1) && (**argv == '-'))
        {
            scanargs(*argv);
            argc--, argv++;
        }
    if (argc != 2)
        {
            usage();
            exit(-1);
        }
    Read_Cache_Descriptions(argv[0]);
    Init_Caches();
    Simulate_Caches(argv[1]);
    Print_Cache_Statistics();
    Delete_Caches();
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
char *__fastcall print_sets_and_ways(__int64 a1)
{
__int64 v1; // rcx
const char *v2; // r8
int v3; // eax

v1 = *(unsigned int *)(a1 + 24);
v2 = "direct-mapped";
if ( (_DWORD)v1 == 1 )
return (char *)v2;
v3 = *(_DWORD *)(a1 + 20);
v2 = "fully associative";
if ( (_DWORD)v1 == v3 )
return (char *)v2;
sprintf(buffer_1, "%d sets of %d ways", (unsigned int)(v3 / (int)v1), v1);
return buffer_1;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1)
{
__int64 v1; // rcx
const char *v2; // r8
int v3; // eax

v1 = *(unsigned int *)(a1 + 24);
v2 = "direct-mapped";
if ( (_DWORD)v1 == 1 )
return (char *)v2;
v3 = *(_DWORD *)(a1 + 20);
v2 = "fully associative";
if ( (_DWORD)v1 == v3 )
return (char *)v2;
sprintf(buffer_1, "%d sets of %d ways", (unsigned int)(v3 / (int)v1), v1);
return buffer_1;
}

original_code:
String print_sets_and_ways(CDS *cds)
{
    if (cds->number_of_ways == 1) return("direct-mapped");
    if (cds->number_of_ways == cds->number_of_cache_entries) return("fully associative");
    static char buffer[64];
    sprintf (buffer, "%d sets of %d ways",
             cds->number_of_cache_entries/cds->number_of_ways, cds->number_of_ways);
    return(buffer);
}

canonical_original_code:
char *print_sets_and_ways(struct CDS *cds)
{
    if (cds->number_of_ways == 1) return("direct-mapped");
    if (cds->number_of_ways == cds->number_of_cache_entries) return("fully associative");
    static char buffer[64];
    sprintf (buffer, "%d sets of %d ways",
             cds->number_of_cache_entries/cds->number_of_ways, cds->number_of_ways);
    return(buffer);
}

variable_types: struct CDS { struct CDS *next; char *name; int cache_line_size; int number_of_cache_entries; int number_of_ways; short write_back; enum CRP { CRP_FIFO=0, CRP_LRU=1, CRP_LFU=2, CRP_RANDOM=3 } replacement_policy; int LFU_Decay_Interval; struct cache_line { short valid; int tag; short dirty; int replacement_data; unsigned char *actual_data; } *c; int number_of_memory_reference; int number_of_type[3]; int number_total_cache_access; int number_cache_hits; int number_cache_misses; int number_memory_reads; int number_memory_writes; } *

variable_types:
buffer
char [64]

return_type: char *

user_defined_types: struct CDS { struct CDS *next; char *name; int cache_line_size; int number_of_cache_entries; int number_of_ways; short write_back; enum CRP replacement_policy; int LFU_Decay_Interval; struct cache_line *c; int number_of_memory_reference; int number_of_type[3]; int number_total_cache_access; int number_cache_hits; int number_cache_misses; int number_memory_reads; int number_memory_writes; }


decompiled_code:
const char *__fastcall memory_reference_type_name(unsigned int a1)
{
const char *result; // rax

result = "invalid";
if ( a1 <= 2 )
result = CSWTCH_14[a1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(unsigned int a1)
{
const char *result; // rax

result = "invalid";
if ( a1 <= 2 )
result = CSWTCH_14[a1];
return result;
}

original_code:
String memory_reference_type_name(enum memory_access_type type)
{
    switch(type)
        {
        case MAT_FETCH: return("Fetch");
        case MAT_LOAD: return("Load");
        case MAT_STORE: return("Store");
        }
    return("invalid");
}

canonical_original_code:
char *memory_reference_type_name(enum memory_access_type type)
{
    switch(type)
        {
        case MAT_FETCH: return("Fetch");
        case MAT_LOAD: return("Load");
        case MAT_STORE: return("Store");
        }
    return("invalid");
}

variable_types: enum memory_access_type { MAT_LOAD=0, MAT_STORE=1, MAT_FETCH=2 }


decompiled_code:
const char *__fastcall CRP_name(__int64 a1)
{
unsigned int v1; // eax
bool v2; // zf
const char *result; // rax

v1 = *(_DWORD *)(a1 + 32);
if ( v1 == 2 )
{
sprintf(buffer_0, "LFU (decay=%d)", *(unsigned int *)(a1 + 36));
result = buffer_0;
}
else if ( v1 > 2 )
{
v2 = v1 == 3;
result = "Invalid policy";
if ( v2 )
result = "RANDOM";
}
else
{
v2 = v1 == 0;
result = "LRU";
if ( v2 )
result = "FIFO";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func2(__int64 a1)
{
unsigned int v1; // eax
bool v2; // zf
const char *result; // rax

v1 = *(_DWORD *)(a1 + 32);
if ( v1 == 2 )
{
sprintf(buffer_0, "LFU (decay=%d)", *(unsigned int *)(a1 + 36));
result = buffer_0;
}
else if ( v1 > 2 )
{
v2 = v1 == 3;
result = "Invalid policy";
if ( v2 )
result = "RANDOM";
}
else
{
v2 = v1 == 0;
result = "LRU";
if ( v2 )
result = "FIFO";
}
return result;
}

original_code:
String CRP_name(CDS *cds)
{
    switch(cds->replacement_policy)
        {
        case CRP_FIFO: return("FIFO");
        case CRP_LRU: return("LRU");
        case CRP_RANDOM: return("RANDOM");
        case CRP_LFU:
            {
                static char buffer[64];
                sprintf(buffer, "LFU (decay=%d)", cds->LFU_Decay_Interval);
                return(buffer);
            }
        };
    return("Invalid policy");
}

canonical_original_code:
char *CRP_name(struct CDS *cds)
{
    switch(cds->replacement_policy)
        {
        case CRP_FIFO: return("FIFO");
        case CRP_LRU: return("LRU");
        case CRP_RANDOM: return("RANDOM");
        case CRP_LFU:
            {
                static char buffer[64];
                sprintf(buffer, "LFU (decay=%d)", cds->LFU_Decay_Interval);
                return(buffer);
            }
        };
    return("Invalid policy");
}

user_defined_types: enum CRP { CRP_FIFO=0, CRP_LRU=1, CRP_LFU=2, CRP_RANDOM=3 }

user_defined_types: struct cache_line { short valid; int tag; short dirty; int replacement_data; unsigned char *actual_data; }


decompiled_code:
int __fastcall debug_print_cds(__int64 a1)
{
char *v1; // rax
const char *v2; // rcx
const char *v3; // rax

fprintf(debug_file, "%s: Total number of entries: %d\n", *(const char **)(a1 + 8), *(unsigned int *)(a1 + 20));
v1 = print_sets_and_ways(a1);
fprintf(debug_file, "%s: %s\n", *(const char **)(a1 + 8), v1);
fprintf(debug_file, "%s: Each cache line is %d bytes\n", *(const char **)(a1 + 8), *(unsigned int *)(a1 + 16));
v2 = "write-back";
if ( !*(_WORD *)(a1 + 28) )
v2 = "write-thru";
fprintf(debug_file, "%s: Cache is %s\n", *(const char **)(a1 + 8), v2);
v3 = CRP_name(a1);
return fprintf(debug_file, "%s: With a %s replacement policy\n", *(const char **)(a1 + 8), v3);
}

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

fprintf(debug_file, "%s: Total number of entries: %d\n", *(const char **)(a1 + 8), *(unsigned int *)(a1 + 20));
v1 = func0(a1);
fprintf(debug_file, "%s: %s\n", *(const char **)(a1 + 8), v1);
fprintf(debug_file, "%s: Each cache line is %d bytes\n", *(const char **)(a1 + 8), *(unsigned int *)(a1 + 16));
v2 = "write-back";
if ( !*(_WORD *)(a1 + 28) )
v2 = "write-thru";
fprintf(debug_file, "%s: Cache is %s\n", *(const char **)(a1 + 8), v2);
v3 = func2(a1);
return fprintf(debug_file, "%s: With a %s replacement policy\n", *(const char **)(a1 + 8), v3);
}

original_code:
void debug_print_cds(CDS *cds)
{
    fprintf(debug_file, "%s: Total number of entries: %d\n", cds->name, cds->number_of_cache_entries);
    fprintf(debug_file, "%s: %s\n", cds->name, print_sets_and_ways(cds));
    fprintf(debug_file, "%s: Each cache line is %d bytes\n", cds->name, cds->cache_line_size);
    fprintf(debug_file, "%s: Cache is %s\n", cds->name, cds->write_back ? "write-back" : "write-thru");
    fprintf(debug_file, "%s: With a %s replacement policy\n", cds->name, CRP_name(cds));
}

canonical_original_code:
void debug_print_cds(struct CDS *cds)
{
    fprintf(debug_file, "%s: Total number of entries: %d\n", cds->name, cds->number_of_cache_entries);
    fprintf(debug_file, "%s: %s\n", cds->name, print_sets_and_ways(cds));
    fprintf(debug_file, "%s: Each cache line is %d bytes\n", cds->name, cds->cache_line_size);
    fprintf(debug_file, "%s: Cache is %s\n", cds->name, cds->write_back ? "write-back" : "write-thru");
    fprintf(debug_file, "%s: With a %s replacement policy\n", cds->name, CRP_name(cds));
}


percent

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

result = 0LL;
if ( a2 )
result = (unsigned int)(100 * a1 / a2);
return result;
}

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

result = 0LL;
if ( a2 )
result = (unsigned int)(100 * a1 / a2);
return result;
}

original_code:
int percent(int a, int b)
{
    if (b == 0) return(0);
    int n = (a*100/b);
    return(n);
}

variable_types: a int

variable_types: b int

variable_types: n int


decompiled_code:
int __fastcall Print_Cache_Statistics_for_one_cache(__int64 a1)
{
const char *v1; // rbx
const char *v2; // r12
char *v3; // rax
const char *v4; // rax
const char *v5; // rdx
const char *v6; // rcx
unsigned int v7; // eax
unsigned int v8; // er10
unsigned int v10; // eax
__int64 v11; // [rsp+8h] [rbp-20h]

v1 = "write-back";
v2 = CRP_name(a1);
if ( !*(_WORD *)(a1 + 28) )
v1 = "write-thru";
v3 = print_sets_and_ways(a1);
fprintf(
stdout,
"%s: %d entries of lines of %d bytes; %s, %s, %s\n",
*(const char **)(a1 + 8),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16),
v3,
v1,
v2);
memory_reference_type_name(1u);
memory_reference_type_name(0);
v4 = memory_reference_type_name(2u);
fprintf(
stdout,
" %d addresses (%d %s, %d %s, %d %s)\n",
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 60),
v4,
*(unsigned int *)(a1 + 52),
v5,
*(unsigned int *)(a1 + 56),
v6);
v7 = percent(*(_DWORD *)(a1 + 68), *(_DWORD *)(a1 + 64));
fprintf(
stdout,
" %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n",
v8,
v7,
*(unsigned int *)(a1 + 72),
*(unsigned int *)(a1 + 76),
*(unsigned int *)(a1 + 80));
if ( *(_WORD *)(a1 + 28) )
{
v10 = number_dirty_lines(a1, " %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n", v11);
fprintf(stdout, " %d dirty cache lines remain\n", v10);
}
return fputc(10, stdout);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
const char *v1; // rbx
const char *v2; // r12
char *v3; // rax
const char *v4; // rax
const char *v5; // rdx
const char *v6; // rcx
unsigned int v7; // eax
unsigned int v8; // er10
unsigned int v10; // eax
__int64 v11; // [rsp+8h] [rbp-20h]

v1 = "write-back";
v2 = func2(a1);
if ( !*(_WORD *)(a1 + 28) )
v1 = "write-thru";
v3 = func0(a1);
fprintf(
stdout,
"%s: %d entries of lines of %d bytes; %s, %s, %s\n",
*(const char **)(a1 + 8),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16),
v3,
v1,
v2);
func1(1u);
func1(0);
v4 = func1(2u);
fprintf(
stdout,
" %d addresses (%d %s, %d %s, %d %s)\n",
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 60),
v4,
*(unsigned int *)(a1 + 52),
v5,
*(unsigned int *)(a1 + 56),
v6);
v7 = func4(*(_DWORD *)(a1 + 68), *(_DWORD *)(a1 + 64));
fprintf(
stdout,
" %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n",
v8,
v7,
*(unsigned int *)(a1 + 72),
*(unsigned int *)(a1 + 76),
*(unsigned int *)(a1 + 80));
if ( *(_WORD *)(a1 + 28) )
{
v10 = number_dirty_lines(a1, " %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n", v11);
fprintf(stdout, " %d dirty cache lines remain\n", v10);
}
return fputc(10, stdout);
}

original_code:
void Print_Cache_Statistics_for_one_cache(CDS *cds)
{
    fprintf(stdout, "%s: %d entries of lines of %d bytes; %s, %s, %s\n",
            cds->name, cds->number_of_cache_entries, cds->cache_line_size,
            print_sets_and_ways(cds),
            cds->write_back ? "write-back" : "write-thru",
            CRP_name(cds));
    fprintf(stdout, "      %d addresses (%d %s, %d %s, %d %s)\n",
            cds->number_of_memory_reference,
            cds->number_of_type[MAT_FETCH], memory_reference_type_name(MAT_FETCH),
            cds->number_of_type[MAT_LOAD], memory_reference_type_name(MAT_LOAD),
            cds->number_of_type[MAT_STORE], memory_reference_type_name(MAT_STORE));
    fprintf(stdout, "      %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n",
            cds->number_cache_hits, percent(cds->number_cache_hits, cds->number_total_cache_access),
            cds->number_cache_misses,
            cds->number_memory_reads, cds->number_memory_writes);
    if (cds->write_back)
        fprintf(stdout, "      %d dirty cache lines remain\n", number_dirty_lines(cds));
    fprintf(stdout, "\n");
}

canonical_original_code:
void Print_Cache_Statistics_for_one_cache(struct CDS *cds)
{
    fprintf(stdout, "%s: %d entries of lines of %d bytes; %s, %s, %s\n",
            cds->name, cds->number_of_cache_entries, cds->cache_line_size,
            print_sets_and_ways(cds),
            cds->write_back ? "write-back" : "write-thru",
            CRP_name(cds));
    fprintf(stdout, "      %d addresses (%d %s, %d %s, %d %s)\n",
            cds->number_of_memory_reference,
            cds->number_of_type[MAT_FETCH], memory_reference_type_name(MAT_FETCH),
            cds->number_of_type[MAT_LOAD], memory_reference_type_name(MAT_LOAD),
            cds->number_of_type[MAT_STORE], memory_reference_type_name(MAT_STORE));
    fprintf(stdout, "      %d hits (%d%%), %d misses, %d memory reads, %d memory writes\n",
            cds->number_cache_hits, percent(cds->number_cache_hits, cds->number_total_cache_access),
            cds->number_cache_misses,
            cds->number_memory_reads, cds->number_memory_writes);
    if (cds->write_back)
        fprintf(stdout, "      %d dirty cache lines remain\n", number_dirty_lines(cds));
    fprintf(stdout, "\n");
}


decompiled_code:
int Print_Cache_Statistics()
{
_QWORD *v0; // rbx
int result; // eax

v0 = CDS_root;
if ( CDS_root )
{
do
{
result = Print_Cache_Statistics_for_one_cache((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
return result;
}

canonical_decompiled_code:
int func6()
{
_QWORD *v0; // rbx
int result; // eax

v0 = CDS_root;
if ( CDS_root )
{
do
{
result = func5((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
return result;
}

original_code:
void Print_Cache_Statistics(void)
{
    CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            Print_Cache_Statistics_for_one_cache(cds);
            cds = cds->next;
        }
}

canonical_original_code:
void Print_Cache_Statistics()
{
    struct CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            Print_Cache_Statistics_for_one_cache(cds);
            cds = cds->next;
        }
}


decompiled_code:
void *__fastcall init_cache(__int64 a1)
{
void *result; // rax

result = calloc(*(int *)(a1 + 20), 0x18uLL);
*(_QWORD *)(a1 + 40) = result;
return result;
}

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

result = calloc(*(int *)(a1 + 20), 0x18uLL);
*(_QWORD *)(a1 + 40) = result;
return result;
}

original_code:
void init_cache(CDS *cds)
{
    cds->c = calloc(cds->number_of_cache_entries, sizeof(cache_line));
}

canonical_original_code:
void init_cache(struct CDS *cds)
{
    cds->c = calloc(cds->number_of_cache_entries, sizeof(cache_line));
}


decompiled_code:
void *Init_Caches()
{
_QWORD *v0; // rbx
void *result; // rax

v0 = CDS_root;
if ( CDS_root )
{
do
{
result = init_cache((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
return result;
}

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

v0 = CDS_root;
if ( CDS_root )
{
do
{
result = func7((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
return result;
}

original_code:
void Init_Caches(void)
{
    CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            init_cache(cds);
            cds = cds->next;
        }
}

canonical_original_code:
void Init_Caches()
{
    struct CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            init_cache(cds);
            cds = cds->next;
        }
}


decompiled_code:
void *Init_caches_for_trace()
{
void *result; // rax
void **v1; // rax

for ( result = CDS_root; result; result = *v1 )
init_cache_for_trace((__int64)result);
return result;
}

canonical_decompiled_code:
void *func10()
{
void *result; // rax
void **v1; // rax

for ( result = CDS_root; result; result = *v1 )
func9((__int64)result);
return result;
}

original_code:
void Init_caches_for_trace(void)
{
    CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            init_cache_for_trace(cds);
            cds = cds->next;
        }
}

canonical_original_code:
void Init_caches_for_trace()
{
    struct CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            init_cache_for_trace(cds);
            cds = cds->next;
        }
}


decompiled_code:
void __fastcall delete_cache(void *ptr)
{
free(*((void **)ptr + 5));
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func11(void *ptr)
{
free(*((void **)ptr + 5));
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void delete_cache(CDS *cds)
{
    free(cds->c);
    free(cds->name);
    free(cds);
}

canonical_original_code:
void delete_cache(struct CDS *cds)
{
    free(cds->c);
    free(cds->name);
    free(cds);
}


decompiled_code:
void Delete_Caches()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = CDS_root;
if ( CDS_root )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
delete_cache(v1);
}
while ( v0 );
}
}

canonical_decompiled_code:
void func12()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = CDS_root;
if ( CDS_root )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
func11(v1);
}
while ( v0 );
}
}

original_code:
void Delete_Caches(void)
{
    CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            CDS *old = cds;
            cds = cds->next;
            delete_cache(old);
        }
}

canonical_original_code:
void Delete_Caches()
{
    struct CDS *cds = CDS_root;
    while (cds != ((void *)0))
        {
            struct CDS *old = cds;
            cds = cds->next;
            delete_cache(old);
        }
}


decompiled_code:
_DWORD *new_token()
{
_DWORD *v0; // rax
_DWORD *v1; // r12

v0 = malloc(0x10uLL);
v0[2] = 128;
v1 = v0;
*(_QWORD *)v0 = malloc(0x80uLL);
return v1;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rax
_DWORD *v1; // r12

v0 = malloc(0x10uLL);
v0[2] = 128;
v1 = v0;
*(_QWORD *)v0 = malloc(0x80uLL);
return v1;
}

original_code:
Token *new_token(void)
{
    Token *t = ((Token *)(malloc(sizeof(Token))));
    t->length = 128;
    t->string = ((String)(malloc(t->length)));
    return(t);
}

canonical_original_code:
struct Token *new_token()
{
    struct Token *t = ((Token *)(malloc(sizeof(Token))));
    t->length = 128;
    t->string = ((String)(malloc(t->length)));
    return(t);
}

variable_types: struct Token { char *string; int length; } *


decompiled_code:
void __fastcall delete_token(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void delete_token(Token *t)
{
    free(t->string);
    free(t);
}

canonical_original_code:
void delete_token(struct Token *t)
{
    free(t->string);
    free(t);
}

user_defined_types: struct Token { char *string; int length; }


decompiled_code:
void __fastcall put_char_in_token_at(__int64 a1, char a2, int a3)
{
int v5; // esi
__int64 v6; // rbx
void *v7; // rdi
int v8; // esi
void *v9; // rax

v5 = *(_DWORD *)(a1 + 8);
v6 = a3;
v7 = *(void **)a1;
if ( v5 <= a3 )
{
v8 = 2 * v5;
*(_DWORD *)(a1 + 8) = v8;
v9 = realloc(v7, v8);
*(_QWORD *)a1 = v9;
v7 = v9;
if ( !v9 )
{
fwrite("Hell has frozen over!!!\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
}
*((_BYTE *)v7 + v6) = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, char a2, int a3)
{
int v5; // esi
__int64 v6; // rbx
void *v7; // rdi
int v8; // esi
void *v9; // rax

v5 = *(_DWORD *)(a1 + 8);
v6 = a3;
v7 = *(void **)a1;
if ( v5 <= a3 )
{
v8 = 2 * v5;
*(_DWORD *)(a1 + 8) = v8;
v9 = realloc(v7, v8);
*(_QWORD *)a1 = v9;
v7 = v9;
if ( !v9 )
{
fwrite("Hell has frozen over!!!\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
}
*((_BYTE *)v7 + v6) = a2;
}

original_code:
void put_char_in_token_at(Token *t, char c, int i)
{
    if (i >= t->length)
        {
            t->length = 2 * t->length;
            t->string = realloc(t->string, t->length);
            if (t->string == ((void *)0))
                {
                    fprintf(stderr, "Hell has frozen over!!!\n");
                    exit(-1);
                }
        }
    t->string[i] = c;
}

canonical_original_code:
void put_char_in_token_at(struct Token *t, char c, int i)
{
    if (i >= t->length)
        {
            t->length = 2 * t->length;
            t->string = realloc(t->string, t->length);
            if (t->string == ((void *)0))
                {
                    fprintf(stderr, "Hell has frozen over!!!\n");
                    exit(-1);
                }
        }
    t->string[i] = c;
}

variable_types: c char

variable_types: i int


decompiled_code:
int __fastcall define_key_value_pair(__int64 a1, const char **a2, const char **a3)
{
const char *v5; // r13
void *v6; // rdi
char *v7; // rax
char *v8; // rax
const char *v9; // rbp

if ( debug )
fprintf(debug_file, "define %s = %s \n", *a2, *a3);
v5 = *a2;
if ( strcasestr(*a2, "name") )
{
v6 = *(void **)(a1 + 8);
if ( v6 )
free(v6);
v7 = (char *)remember_string(*a3);
*(_QWORD *)(a1 + 8) = v7;
return (int)v7;
}
v8 = strcasestr(v5, "line");
v9 = *a3;
if ( v8 && strcasestr(v5, "size") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 16) = (_DWORD)v7;
return (int)v7;
}
if ( strcasestr(v5, "entries") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 20) = (_DWORD)v7;
return (int)v7;
}
if ( strcasestr(v5, "ways") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 24) = (_DWORD)v7;
return (int)v7;
}
if ( !strcasestr(v5, "write") )
goto LABEL_19;
if ( !strcasestr(v5, "back") )
{
if ( !strcasestr(v5, "thru") )
goto LABEL_19;
if ( !strcasestr(v9, "true") )
{
v7 = strcasestr(v9, "false");
if ( v7 )
goto LABEL_17;
goto LABEL_19;
}
LABEL_24:
LODWORD(v7) = 0;
*(_WORD *)(a1 + 28) = 0;
return (int)v7;
}
v7 = strcasestr(v9, "true");
if ( v7 )
{
LABEL_17:
*(_WORD *)(a1 + 28) = 1;
return (int)v7;
}
if ( strcasestr(v9, "false") )
goto LABEL_24;
LABEL_19:
if ( strcasestr(v5, "policy") || strcasestr(v5, "replace") )
{
v7 = strcasestr(v9, "LRU");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 1;
return (int)v7;
}
v7 = strcasestr(v9, "LFU");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 2;
return (int)v7;
}
v7 = strcasestr(v9, "random");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 3;
return (int)v7;
}
v7 = strcasestr(v9, "FIFO");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 0;
return (int)v7;
}
}
if ( strcasestr(v5, "decay") && strcasestr(v5, "interval") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 36) = (_DWORD)v7;
}
else
{
LODWORD(v7) = fprintf(stderr, "don't understand %s = %s\n", v5, v9);
}
return (int)v7;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, const char **a2, const char **a3)
{
const char *v5; // r13
void *v6; // rdi
char *v7; // rax
char *v8; // rax
const char *v9; // rbp

if ( debug )
fprintf(debug_file, "define %s = %s \n", *a2, *a3);
v5 = *a2;
if ( strcasestr(*a2, "name") )
{
v6 = *(void **)(a1 + 8);
if ( v6 )
free(v6);
v7 = (char *)remember_string(*a3);
*(_QWORD *)(a1 + 8) = v7;
return (int)v7;
}
v8 = strcasestr(v5, "line");
v9 = *a3;
if ( v8 && strcasestr(v5, "size") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 16) = (_DWORD)v7;
return (int)v7;
}
if ( strcasestr(v5, "entries") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 20) = (_DWORD)v7;
return (int)v7;
}
if ( strcasestr(v5, "ways") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 24) = (_DWORD)v7;
return (int)v7;
}
if ( !strcasestr(v5, "write") )
goto LABEL_19;
if ( !strcasestr(v5, "back") )
{
if ( !strcasestr(v5, "thru") )
goto LABEL_19;
if ( !strcasestr(v9, "true") )
{
v7 = strcasestr(v9, "false");
if ( v7 )
goto LABEL_17;
goto LABEL_19;
}
LABEL_24:
LODWORD(v7) = 0;
*(_WORD *)(a1 + 28) = 0;
return (int)v7;
}
v7 = strcasestr(v9, "true");
if ( v7 )
{
LABEL_17:
*(_WORD *)(a1 + 28) = 1;
return (int)v7;
}
if ( strcasestr(v9, "false") )
goto LABEL_24;
LABEL_19:
if ( strcasestr(v5, "policy") || strcasestr(v5, "replace") )
{
v7 = strcasestr(v9, "LRU");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 1;
return (int)v7;
}
v7 = strcasestr(v9, "LFU");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 2;
return (int)v7;
}
v7 = strcasestr(v9, "random");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 3;
return (int)v7;
}
v7 = strcasestr(v9, "FIFO");
if ( v7 )
{
*(_DWORD *)(a1 + 32) = 0;
return (int)v7;
}
}
if ( strcasestr(v5, "decay") && strcasestr(v5, "interval") )
{
LODWORD(v7) = atoi(v9);
*(_DWORD *)(a1 + 36) = (_DWORD)v7;
}
else
{
LODWORD(v7) = fprintf(stderr, "don't understand %s = %s\n", v5, v9);
}
return (int)v7;
}

original_code:
void define_key_value_pair(CDS *cds, Token *key, Token *value)
{
    if (debug) fprintf(debug_file, "define %s = %s \n", key->string, value->string);
    if (strcasestr(key->string, "name") != ((void *)0))
        {
            if (cds->name != ((void *)0)) free(cds->name);
            cds->name = remember_string(value->string);
            return;
        }
    if ((strcasestr(key->string, "line") != ((void *)0)) && (strcasestr(key->string, "size") != ((void *)0)))
        {
            int n = atoi(value->string);
            cds->cache_line_size = n;
            return;
        }
    if (strcasestr(key->string, "entries") != ((void *)0))
        {
            int n = atoi(value->string);
            cds->number_of_cache_entries = n;
            return;
        }
    if (strcasestr(key->string, "ways") != ((void *)0))
        {
            int n = atoi(value->string);
            cds->number_of_ways = n;
            return;
        }
    if ((strcasestr(key->string, "write") != ((void *)0)) && (strcasestr(key->string, "back") != ((void *)0)))
        {
            if (strcasestr(value->string, "true") != ((void *)0))
                {
                    cds->write_back = 1;
                    return;
                }
            if (strcasestr(value->string, "false") != ((void *)0))
                {
                    cds->write_back = 0;
                    return;
                }
        }
    if ((strcasestr(key->string, "write") != ((void *)0)) && (strcasestr(key->string, "thru") != ((void *)0)))
        {
            if (strcasestr(value->string, "true") != ((void *)0))
                {
                    cds->write_back = 0;
                    return;
                }
            if (strcasestr(value->string, "false") != ((void *)0))
                {
                    cds->write_back = 1;
                    return;
                }
        }
    if ((strcasestr(key->string, "policy") != ((void *)0)) || (strcasestr(key->string, "replace") != ((void *)0)))
        {
            if (strcasestr(value->string, "LRU") != ((void *)0))
                {
                    cds->replacement_policy = CRP_LRU;
                    return;
                }
            if (strcasestr(value->string, "LFU") != ((void *)0))
                {
                    cds->replacement_policy = CRP_LFU;
                    return;
                }
            if (strcasestr(value->string, "random") != ((void *)0))
                {
                    cds->replacement_policy = CRP_RANDOM;
                    return;
                }
            if (strcasestr(value->string, "FIFO") != ((void *)0))
                {
                    cds->replacement_policy = CRP_FIFO;
                    return;
                }
        }
    if ((strcasestr(key->string, "decay") != ((void *)0)) && (strcasestr(key->string, "interval") != ((void *)0)))
        {
            int n = atoi(value->string);
            cds->LFU_Decay_Interval = n;
            return;
        }
    fprintf(stderr, "don't understand %s = %s\n",key->string, value->string);
}

canonical_original_code:
void define_key_value_pair(struct CDS *cds, struct Token *key, struct Token *value)
{
    if (debug) fprintf(debug_file, "define %s = %s \n", key->string, value->string);
    if (strcasestr(key->string, "name") != ((void *)0))
        {
            if (cds->name != ((void *)0)) free(cds->name);
            cds->name = remember_string(value->string);
            return;
        }
    if ((strcasestr(key->string, "line") != ((void *)0)) && (strcasestr(key->string, "size") != ((void *)0)))
        {
            int n = atoi(value->string);
            cds->cache_line_size = n;
            return;
        }
    if (strcasestr(key->string, "entries") != ((void *)0))
        {
            int n = atoi(value->string);
            cds->number_of_cache_entries = n;
            return;
        }
    if (strcasestr(key->string, "ways") != ((void *)0))
        {
            int n = atoi(value->string);
            cds->number_of_ways = n;
            return;
        }
    if ((strcasestr(key->string, "write") != ((void *)0)) && (strcasestr(key->string, "back") != ((void *)0)))
        {
            if (strcasestr(value->string, "true") != ((void *)0))
                {
                    cds->write_back = 1;
                    return;
                }
            if (strcasestr(value->string, "false") != ((void *)0))
                {
                    cds->write_back = 0;
                    return;
                }
        }
    if ((strcasestr(key->string, "write") != ((void *)0)) && (strcasestr(key->string, "thru") != ((void *)0)))
        {
            if (strcasestr(value->string, "true") != ((void *)0))
                {
                    cds->write_back = 0;
                    return;
                }
            if (strcasestr(value->string, "false") != ((void *)0))
                {
                    cds->write_back = 1;
                    return;
                }
        }
    if ((strcasestr(key->string, "policy") != ((void *)0)) || (strcasestr(key->string, "replace") != ((void *)0)))
        {
            if (strcasestr(value->string, "LRU") != ((void *)0))
                {
                    cds->replacement_policy = CRP_LRU;
                    return;
                }
            if (strcasestr(value->string, "LFU") != ((void *)0))
                {
                    cds->replacement_policy = CRP_LFU;
                    return;
                }
            if (strcasestr(value->string, "random") != ((void *)0))
                {
                    cds->replacement_policy = CRP_RANDOM;
                    return;
                }
            if (strcasestr(value->string, "FIFO") != ((void *)0))
                {
                    cds->replacement_policy = CRP_FIFO;
                    return;
                }
        }
    if ((strcasestr(key->string, "decay") != ((void *)0)) && (strcasestr(key->string, "interval") != ((void *)0)))
        {
            int n = atoi(value->string);
            cds->LFU_Decay_Interval = n;
            return;
        }
    fprintf(stderr, "don't understand %s = %s\n",key->string, value->string);
}

variable_types:
value
struct Token *


decompiled_code:
__int64 __fastcall which_power(int a1)
{
int v1; // eax
unsigned int v2; // er8

if ( a1 <= 1 )
return 0LL;
v1 = 1;
v2 = 0;
do
{
v1 *= 2;
++v2;
}
while ( a1 > v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // eax
unsigned int v2; // er8

if ( a1 <= 1 )
return 0LL;
v1 = 1;
v2 = 0;
do
{
v1 *= 2;
++v2;
}
while ( a1 > v1 );
return v2;
}

original_code:
int which_power(int n)
{
    int i = 0;
    int t = 1;
    while (n > t)
        {
            t += t;
            i += 1;
        }
    return(i);
}

variable_types: t int


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

result = 0LL;
if ( a1 > 0 )
result = (unsigned int)((1 << a1) - 1);
return result;
}

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

result = 0LL;
if ( a1 > 0 )
result = (unsigned int)((1 << a1) - 1);
return result;
}

original_code:
unsigned int mask_of(int n)
{
    int m;
    if (n <= 0) return(0);
    m = (1 << n) - 1;
    return(m);
}

variable_types: m int


ishex

decompiled_code:
_BOOL8 __fastcall ishex(int a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( (unsigned int)(a1 - 48) > 9 )
result = (a1 & 0xFFFFFFDF) - 65 <= 5;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(int a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( (unsigned int)(a1 - 48) > 9 )
result = (a1 & 0xFFFFFFDF) - 65 <= 5;
return result;
}

variable_types: c int

return_type: short


hexvalue

decompiled_code:
__int64 __fastcall hexvalue(int a1)
{
__int64 result; // rax
unsigned int v2; // eax
unsigned int v3; // edi
bool v4; // cf

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
{
if ( (unsigned int)(a1 - 97) <= 5 )
{
result = (unsigned int)(a1 - 87);
}
else
{
v2 = a1 - 65;
v3 = a1 - 55;
v4 = v2 < 6;
result = 0xFFFFFFFFLL;
if ( v4 )
result = v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
__int64 result; // rax
unsigned int v2; // eax
unsigned int v3; // edi
bool v4; // cf

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
{
if ( (unsigned int)(a1 - 97) <= 5 )
{
result = (unsigned int)(a1 - 87);
}
else
{
v2 = a1 - 65;
v3 = a1 - 55;
v4 = v2 < 6;
result = 0xFFFFFFFFLL;
if ( v4 )
result = v3;
}
}
return result;
}


decvalue

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

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
result = 0xFFFFFFFFLL;
return result;
}

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

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
result = 0xFFFFFFFFLL;
return result;
}


decompiled_code:
void *__fastcall remember_string(void *src)
{
size_t v1; // r12
void *v2; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func5(void *src)
{
size_t v1; // r12
void *v2; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
return memcpy(v2, src, v1);
}

original_code:
String remember_string(const String name)
{
    size_t n;
    String p;
    if (name == ((void *)0)) return(((void *)0));
    n = strlen(name) + 1;
    p = ((String)(malloc(n)));
    strcpy(p, name);
    return(p);
}

canonical_original_code:
char *remember_string(char *name)
{
    unsigned long n;
    char *p;
    if (name == ((void *)0)) return(((void *)0));
    n = strlen(name) + 1;
    p = ((String)(malloc(n)));
    strcpy(p, name);
    return(p);
}

variable_types:
name
char *

variable_types:
n
unsigned long

variable_types:
p
char *