decompiled_code:
unsigned __int64 kore_mem_init()
{
char *v0; // rax
char *v1; // rbp
unsigned __int64 v2; // rbx
_BYTE v4[40]; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v5; // [rsp+28h] [rbp-20h]

v5 = __readfsqword(0x28u);
v0 = getenv("KORE_MEM_GUARD");
if ( v0 && !strcmp(v0, "1") )
kore_mem_guard = 1;
tags = 0LL;
qword_AF8 = (__int64)&tags;
kore_pool_init(&tag_pool, "tag_pool", 32LL, 4LL);
v1 = (char *)&mempools;
v2 = 8LL;
do
{
if ( (unsigned int)_snprintf_chk(v4, 32LL, 1LL, 32LL, "block-%zu", v2) > 0x1F )
fatal("kore_mem_init: snprintf");
kore_pool_init(v1, v4, v2 + 16, 0x8000 / v2);
v2 *= 2LL;
v1 += 72;
}
while ( v1 != (char *)&unk_A98 );
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func2()
{
char *v0; // rax
char *v1; // rbp
unsigned __int64 v2; // rbx
_BYTE v4[40]; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v5; // [rsp+28h] [rbp-20h]

v5 = __readfsqword(0x28u);
v0 = getenv("KORE_MEM_GUARD");
if ( v0 && !strcmp(v0, "1") )
kore_mem_guard = 1;
tags = 0LL;
qword_AF8 = (__int64)&tags;
kore_pool_init(&tag_pool, "tag_pool", 32LL, 4LL);
v1 = (char *)&mempools;
v2 = 8LL;
do
{
if ( (unsigned int)_snprintf_chk(v4, 32LL, 1LL, 32LL, "block-%zu", v2) > 0x1F )
fatal("kore_mem_init: snprintf");
kore_pool_init(v1, v4, v2 + 16, 0x8000 / v2);
v2 *= 2LL;
v1 += 72;
}
while ( v1 != (char *)&unk_A98 );
return v5 - __readfsqword(0x28u);
}

original_code:
void
kore_mem_init(void)
{
 const char *opt;
 int i, len;
 char name[32];
 size_t size, elm, mlen;
 if ((opt = getenv("KORE_MEM_GUARD")) != ((void *)0) && !strcmp(opt, "1"))
  kore_mem_guard = 1;
 size = 8;
 do { (&tags)->tqh_first = ((void *)0); (&tags)->tqh_last = &(&tags)->tqh_first; } while ( 0);
 kore_pool_init(&tag_pool, "tag_pool", sizeof(struct tag), 4);
 for (i = 0; i < 11; i++) {
  len = snprintf(name, sizeof(name), "block-%zu", size);
  if (len == -1 || (size_t)len >= sizeof(name))
   fatal("kore_mem_init: snprintf");
  elm = (32 * 1024) / size;
  mlen = sizeof(struct meminfo) + size;
  kore_pool_init(&mempools[i], name, mlen, elm);
  size = size << 1;
 }
}

canonical_original_code:
void kore_mem_init()
{
 char *opt;
 int i; int len;
 char name[32];
 unsigned long size; unsigned long elm; unsigned long mlen;
 if ((opt = getenv("KORE_MEM_GUARD")) != ((void *)0) && !strcmp(opt, "1"))
  kore_mem_guard = 1;
 size = 8;
 do { (&tags)->tqh_first = ((void *)0); (&tags)->tqh_last = &(&tags)->tqh_first; } while ( 0);
 kore_pool_init(&tag_pool, "tag_pool", sizeof(struct tag), 4);
 for (i = 0; i < 11; i++) {
  len = snprintf(name, sizeof(name), "block-%zu", size);
  if (len == -1 || (unsigned long)len >= sizeof(name))
   fatal("kore_mem_init: snprintf");
  elm = (32 * 1024) / size;
  mlen = sizeof(struct meminfo) + size;
  kore_pool_init(&mempools[i], name, mlen, elm);
  size = size << 1;
 }
}

variable_types:
opt
char *

variable_types: i int

variable_types: len int

variable_types:
name
char [32]

variable_types:
size
unsigned long

user_defined_types: struct tag { void *ptr; unsigned int id; struct { struct tag *tqe_next; struct tag **tqe_prev; } list; }

user_defined_types: struct meminfo { unsigned long len; unsigned int flags; }


repo: jorisvink/kore

decompiled_code:
unsigned __int64 kore_mem_cleanup()
{
char *v0; // rbx
unsigned __int64 v2; // [rsp+8h] [rbp-20h]

v2 = __readfsqword(0x28u);
v0 = (char *)&mempools;
do
{
kore_pool_cleanup(v0);
v0 += 72;
}
while ( v0 != (char *)&unk_A98 );
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func3()
{
char *v0; // rbx
unsigned __int64 v2; // [rsp+8h] [rbp-20h]

v2 = __readfsqword(0x28u);
v0 = (char *)&mempools;
do
{
kore_pool_cleanup(v0);
v0 += 72;
}
while ( v0 != (char *)&unk_A98 );
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_mem_cleanup(void)
{
 int i;
 for (i = 0; i < 11; i++) {
  kore_pool_cleanup(&mempools[i]);
 }
}

canonical_original_code:
void kore_mem_cleanup()
{
 int i;
 for (i = 0; i < 11; i++) {
  kore_pool_cleanup(&mempools[i]);
 }
}

return_type: void


decompiled_code:
void *__fastcall kore_mmap_region(size_t len)
{
void *result; // rax
int *v2; // rax
char *v3; // rax

result = mmap(0LL, len, 3, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
v2 = _errno_location();
v3 = strerror(*v2);
fatal("%s: mmap: %s", "kore_mmap_region", v3);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(size_t len)
{
void *result; // rax
int *v2; // rax
char *v3; // rax

result = mmap(0LL, len, 3, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
v2 = _errno_location();
v3 = strerror(*v2);
fatal("%s: mmap: %s", "kore_mmap_region", v3);
}
return result;
}

original_code:
void *
kore_mmap_region(size_t len)
{
 void *ptr;
 if ((ptr = mmap(((void *)0), len, 0x1 | 0x2,
     0x20 | 0x02, -1, 0)) == ((void *) -1))
  fatal("%s: mmap: %s", __func__, strerror((*__errno_location ())));
 return (ptr);
}

canonical_original_code:
void *kore_mmap_region(unsigned long len)
{
 void *ptr;
 if ((ptr = mmap(((void *)0), len, 0x1 | 0x2,
     0x20 | 0x02, -1, 0)) == ((void *) -1))
  fatal("%s: mmap: %s", __func__, strerror((*__errno_location ())));
 return (ptr);
}

variable_types:
len
unsigned long

variable_types:
ptr
void *

return_type: void *


decompiled_code:
char *__fastcall kore_calloc(unsigned __int64 a1, unsigned __int64 a2)
{
char *v2; // rbp

if ( !is_mul_ok(a1, a2) )
fatal("kore_calloc(): memb * len > SIZE_MAX", a2, (_QWORD)((a1 * (unsigned __int128)a2) >> 64));
v2 = mem_alloc(a2 * a1);
memset(v2, 0, a2 * a1);
return v2;
}

canonical_decompiled_code:
char *__fastcall func7(unsigned __int64 a1, unsigned __int64 a2)
{
char *v2; // rbp

if ( !is_mul_ok(a1, a2) )
fatal("kore_calloc(): memb * len > SIZE_MAX", a2, (_QWORD)((a1 * (unsigned __int128)a2) >> 64));
v2 = func5(a2 * a1);
memset(v2, 0, a2 * a1);
return v2;
}

original_code:
void *
kore_calloc(size_t memb, size_t len)
{
 void *ptr;
 size_t total;
 if ((18446744073709551615UL) / memb < len)
  fatal("kore_calloc(): memb * len > SIZE_MAX");
 total = memb * len;
 ptr = mem_alloc(total);
 memset(ptr, 0, total);
 return (ptr);
}

canonical_original_code:
void *kore_calloc(unsigned long memb, unsigned long len)
{
 void *ptr;
 unsigned long total;
 if ((18446744073709551615UL) / memb < len)
  fatal("kore_calloc(): memb * len > SIZE_MAX");
 total = memb * len;
 ptr = mem_alloc(total);
 memset(ptr, 0, total);
 return (ptr);
}

variable_types:
memb
unsigned long

variable_types:
total
unsigned long


decompiled_code:
unsigned __int64 __fastcall kore_mem_untag(__int64 a1)
{
_QWORD *v1; // rsi
__int64 v2; // rax
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v1 = (_QWORD *)tags;
if ( tags )
{
while ( *v1 != a1 )
{
v1 = (_QWORD *)v1[2];
if ( !v1 )
return v4 - __readfsqword(0x28u);
}
v2 = v1[2];
if ( v2 )
*(_QWORD *)(v2 + 24) = v1[3];
else
qword_AF8 = v1[3];
*(_QWORD *)v1[3] = v1[2];
kore_pool_put(&tag_pool);
}
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__int64 a1)
{
_QWORD *v1; // rsi
__int64 v2; // rax
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v1 = (_QWORD *)tags;
if ( tags )
{
while ( *v1 != a1 )
{
v1 = (_QWORD *)v1[2];
if ( !v1 )
return v4 - __readfsqword(0x28u);
}
v2 = v1[2];
if ( v2 )
*(_QWORD *)(v2 + 24) = v1[3];
else
qword_AF8 = v1[3];
*(_QWORD *)v1[3] = v1[2];
kore_pool_put(&tag_pool);
}
return v4 - __readfsqword(0x28u);
}

original_code:
void
kore_mem_untag(void *ptr)
{
 struct tag *tag;
 for ((tag) = ((&tags)->tqh_first); (tag); (tag) = ((tag)->list.tqe_next)) {
  if (tag->ptr == ptr) {
   do { if (((tag)->list.tqe_next) != ((void *)0)) (tag)->list.tqe_next->list.tqe_prev = (tag)->list.tqe_prev; else (&tags)->tqh_last = (tag)->list.tqe_prev; *(tag)->list.tqe_prev = (tag)->list.tqe_next; } while ( 0);
   kore_pool_put(&tag_pool, tag);
   break;
  }
 }
}

variable_types: struct tag { void *ptr; unsigned int id; struct { struct tag *tqe_next; struct tag **tqe_prev; } list; } *


decompiled_code:
unsigned __int64 __fastcall kore_free(__int64 a1)
{
__int64 v1; // rbp
void *v2; // rbx
unsigned __int64 v3; // rdi
unsigned __int64 v4; // rax
int *v6; // rax
char *v7; // rax
unsigned __int64 v8; // [rsp+8h] [rbp-20h]

v8 = __readfsqword(0x28u);
if ( a1 )
{
v1 = meminfo(a1);
if ( (*(_BYTE *)(v1 + 8) & 1) != 0 )
{
kore_mem_untag(a1);
*(_WORD *)(v1 + 8) &= 0xFFFEu;
}
v2 = (void *)(a1 - 16);
v3 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 > 0x2000uLL )
{
if ( munmap(v2, v3 + 16) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
fatal("%s: munmap: %s", "kore_free", v7);
}
}
else
{
v4 = mem_index(v3);
kore_pool_put((char *)&mempools + 72 * v4);
}
}
return v8 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rbp
void *v2; // rbx
unsigned __int64 v3; // rdi
unsigned __int64 v4; // rax
int *v6; // rax
char *v7; // rax
unsigned __int64 v8; // [rsp+8h] [rbp-20h]

v8 = __readfsqword(0x28u);
if ( a1 )
{
v1 = func0(a1);
if ( (*(_BYTE *)(v1 + 8) & 1) != 0 )
{
func9(a1);
*(_WORD *)(v1 + 8) &= 0xFFFEu;
}
v2 = (void *)(a1 - 16);
v3 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 > 0x2000uLL )
{
if ( munmap(v2, v3 + 16) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
fatal("%s: munmap: %s", "kore_free", v7);
}
}
else
{
v4 = func1(v3);
kore_pool_put((char *)&mempools + 72 * v4);
}
}
return v8 - __readfsqword(0x28u);
}

original_code:
void
kore_free(void *ptr)
{
 size_t idx;
 struct meminfo *mem;
 u_int8_t *addr;
 if (ptr == ((void *)0))
  return;
 mem = meminfo(ptr);
 if (mem->flags & 0x0001) {
  kore_mem_untag(ptr);
  mem->flags &= ~0x0001;
 }
 addr = (u_int8_t *)ptr - sizeof(struct meminfo);
 if (mem->len <= 8192) {
  idx = mem_index(mem->len);
  kore_pool_put(&mempools[idx], addr);
 } else {
  if (munmap(addr, sizeof(*mem) + mem->len) == -1)
   fatal("%s: munmap: %s", __func__, strerror((*__errno_location ())));
 }
}

canonical_original_code:
void kore_free(void *ptr)
{
 unsigned long idx;
 struct meminfo *mem;
 unsigned int *addr;
 if (ptr == ((void *)0))
  return;
 mem = meminfo(ptr);
 if (mem->flags & 0x0001) {
  kore_mem_untag(ptr);
  mem->flags &= ~0x0001;
 }
 addr = (unsigned int *)ptr - sizeof(struct meminfo);
 if (mem->len <= 8192) {
  idx = mem_index(mem->len);
  kore_pool_put(&mempools[idx], addr);
 } else {
  if (munmap(addr, sizeof(*mem) + mem->len) == -1)
   fatal("%s: munmap: %s", __func__, strerror((*__errno_location ())));
 }
}

variable_types:
idx
unsigned long

variable_types: struct meminfo { unsigned long len; unsigned int flags; } *

variable_types:
addr
unsigned int *


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

result = tags;
if ( tags )
{
while ( *(_DWORD *)(result + 8) != a1 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
result = *(_QWORD *)result;
}
return result;
}

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

result = tags;
if ( tags )
{
while ( *(_DWORD *)(result + 8) != a1 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
result = *(_QWORD *)result;
}
return result;
}

original_code:
void *
kore_mem_lookup(u_int32_t id)
{
 struct tag *tag;
 for ((tag) = ((&tags)->tqh_first); (tag); (tag) = ((tag)->list.tqe_next)) {
  if (tag->id == id)
   return (tag->ptr);
 }
 return (((void *)0));
}

canonical_original_code:
void *kore_mem_lookup(unsigned int id)
{
 struct tag *tag;
 for ((tag) = ((&tags)->tqh_first); (tag); (tag) = ((tag)->list.tqe_next)) {
  if (tag->id == id)
   return (tag->ptr);
 }
 return (((void *)0));
}

variable_types:
id
unsigned int


decompiled_code:
unsigned __int64 __fastcall kore_mem_tag(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 *v4; // rdx
unsigned __int64 v6; // [rsp+8h] [rbp-20h]

v6 = __readfsqword(0x28u);
if ( kore_mem_lookup(a2) )
fatal("kore_mem_tag: tag %u taken", a2);
v2 = meminfo(a1);
*(_WORD *)(v2 + 8) |= 1u;
v3 = kore_pool_get(&tag_pool);
*(_DWORD *)(v3 + 8) = a2;
*(_QWORD *)v3 = a1;
*(_QWORD *)(v3 + 16) = 0LL;
v4 = (__int64 *)qword_AF8;
*(_QWORD *)(v3 + 24) = qword_AF8;
*v4 = v3;
qword_AF8 = v3 + 16;
return v6 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func12(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 *v4; // rdx
unsigned __int64 v6; // [rsp+8h] [rbp-20h]

v6 = __readfsqword(0x28u);
if ( func11(a2) )
fatal("kore_mem_tag: tag %u taken", a2);
v2 = func0(a1);
*(_WORD *)(v2 + 8) |= 1u;
v3 = kore_pool_get(&tag_pool);
*(_DWORD *)(v3 + 8) = a2;
*(_QWORD *)v3 = a1;
*(_QWORD *)(v3 + 16) = 0LL;
v4 = (__int64 *)qword_AF8;
*(_QWORD *)(v3 + 24) = qword_AF8;
*v4 = v3;
qword_AF8 = v3 + 16;
return v6 - __readfsqword(0x28u);
}

original_code:
void
kore_mem_tag(void *ptr, u_int32_t id)
{
 struct tag *tag;
 struct meminfo *mem;
 if (kore_mem_lookup(id) != ((void *)0))
  fatal("kore_mem_tag: tag %u taken", id);
 mem = meminfo(ptr);
 mem->flags |= 0x0001;
 tag = kore_pool_get(&tag_pool);
 tag->id = id;
 tag->ptr = ptr;
 do { (tag)->list.tqe_next = ((void *)0); (tag)->list.tqe_prev = (&tags)->tqh_last; *(&tags)->tqh_last = (tag); (&tags)->tqh_last = &(tag)->list.tqe_next; } while ( 0);
}

canonical_original_code:
void kore_mem_tag(void *ptr, unsigned int id)
{
 struct tag *tag;
 struct meminfo *mem;
 if (kore_mem_lookup(id) != ((void *)0))
  fatal("kore_mem_tag: tag %u taken", id);
 mem = meminfo(ptr);
 mem->flags |= 0x0001;
 tag = kore_pool_get(&tag_pool);
 tag->id = id;
 tag->ptr = ptr;
 do { (tag)->list.tqe_next = ((void *)0); (tag)->list.tqe_prev = (&tags)->tqh_last; *(&tags)->tqh_last = (tag); (&tags)->tqh_last = &(tag)->list.tqe_next; } while ( 0);
}


decompiled_code:
unsigned __int64 __fastcall kore_mem_zero(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
_BYTE *v3; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( a1 && a2 )
{
v2 = &a1[a2];
do
{
v3 = a1++;
*v3 = 0;
}
while ( a1 != v2 );
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func14(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
_BYTE *v3; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( a1 && a2 )
{
v2 = &a1[a2];
do
{
v3 = a1++;
*v3 = 0;
}
while ( a1 != v2 );
}
return v5 - __readfsqword(0x28u);
}

original_code:
void
kore_mem_zero(void *ptr, size_t len)
{
 volatile char *p;
 p = (volatile char *)ptr;
 if (p != ((void *)0)) {
  while (len-- > 0)
   *(p)++ = 0x00;
 }
}

canonical_original_code:
void kore_mem_zero(void *ptr, unsigned long len)
{
 char *p;
 p = (char *)ptr;
 if (p != ((void *)0)) {
  while (len-- > 0)
   *(p)++ = 0x00;
 }
}

variable_types:
p
char *


decompiled_code:
unsigned __int64 __fastcall kore_free_zero(_BYTE *a1)
{
__int64 *v1; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
if ( a1 )
{
v1 = (__int64 *)meminfo((__int64)a1);
kore_mem_zero(a1, *v1);
kore_free((__int64)a1);
}
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func15(_BYTE *a1)
{
__int64 *v1; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
if ( a1 )
{
v1 = (__int64 *)func0((__int64)a1);
func14(a1, *v1);
func10((__int64)a1);
}
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_free_zero(void *ptr)
{
 struct meminfo *mem;
 if (ptr == ((void *)0))
  return;
 mem = meminfo(ptr);
 kore_mem_zero(ptr, mem->len);
 kore_free(ptr);
}


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

v1 = __readfsqword(0x28u);
maps = 0LL;
qword_958 = (__int64)&maps;
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
maps = 0LL;
qword_958 = (__int64)&maps;
return v1 - __readfsqword(0x28u);
}

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

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


decompiled_code:
unsigned __int64 __fastcall native_runtime_execute(void (*a1)(void))
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
a1();
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(void (*a1)(void))
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
a1();
return v2 - __readfsqword(0x28u);
}

original_code:
static void
native_runtime_execute(void *addr)
{
 void (*cb)(void);
 *(void **)&(cb) = addr;
 cb();
}

canonical_original_code:
static void native_runtime_execute(void *addr)
{
 void (*cb)();
 *(void **)&(cb) = addr;
 cb();
}

variable_types:
addr
void *

variable_types:
cb
void (*)()


decompiled_code:
unsigned __int64 __fastcall native_runtime_configure(void (__fastcall *a1)(_QWORD, __int64, __int64, _QWORD), unsigned int a2, __int64 a3)
{
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
a1(a2, a3, a3, 0LL);
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(void (__fastcall *a1)(_QWORD, __int64, __int64, _QWORD), unsigned int a2, __int64 a3)
{
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
a1(a2, a3, a3, 0LL);
return v4 - __readfsqword(0x28u);
}

original_code:
static void
native_runtime_configure(void *addr, int argc, char **argv)
{
 void (*cb)(int, char **);
 *(void **)&(cb) = addr;
 cb(argc, argv);
}

variable_types: argc int

variable_types:
argv
char **

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


decompiled_code:
__int64 __fastcall native_runtime_onload(__int64 (__fastcall *a1)(_QWORD, __int64, _QWORD), __int64 a2)
{
return a1((unsigned int)a2, a2, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 (__fastcall *a1)(_QWORD, __int64, _QWORD), __int64 a2)
{
return a1((unsigned int)a2, a2, 0LL);
}

original_code:
static int
native_runtime_onload(void *addr, int action)
{
 int (*cb)(int);
 *(void **)&(cb) = addr;
 return (cb(action));
}

variable_types: action int

variable_types:
cb
int (*)(int)

return_type: int


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

v3 = __readfsqword(0x28u);
a1((unsigned int)a2, a2, 0LL);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
a1((unsigned int)a2, a2, 0LL);
return v3 - __readfsqword(0x28u);
}

original_code:
static void
native_runtime_signal(void *addr, int sig)
{
 void (*cb)(int);
 *(void **)&(cb) = addr;
 cb(sig);
}

variable_types: sig int

variable_types:
cb
void (*)(int)


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

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

original_code:
const size_t
kore_runtime_count(void)
{
 return ((sizeof(runtimes) / sizeof(runtimes[0])) - 1);
}

canonical_original_code:
unsigned long kore_runtime_count()
{
 return ((sizeof(runtimes) / sizeof(runtimes[0])) - 1);
}

return_type: unsigned long


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

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

original_code:
void
kore_runtime_resolve(const char *module, const struct stat *st)
{
 int i;
 if (runtimes[0] == ((void *)0))
  return;
 for (i = 0; runtimes[i] != ((void *)0); i++) {
  if (runtimes[i]->resolve == ((void *)0))
   continue;
  if (runtimes[i]->resolve(module, st))
   break;
 }
 if (runtimes[i] == ((void *)0))
  fatal("No runtime available to run '%s'", module);
}

canonical_original_code:
void kore_runtime_resolve(char *module, struct stat *st)
{
 int i;
 if (runtimes[0] == ((void *)0))
  return;
 for (i = 0; runtimes[i] != ((void *)0); i++) {
  if (runtimes[i]->resolve == ((void *)0))
   continue;
  if (runtimes[i]->resolve(module, st))
   break;
 }
 if (runtimes[i] == ((void *)0))
  fatal("No runtime available to run '%s'", module);
}

variable_types:
module
char *

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

variable_types: i int

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:
unsigned __int64 kore_timer_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

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

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

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

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

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


decompiled_code:
unsigned __int64 __fastcall kore_timer_remove(__int64 a1)
{
__int64 v1; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
v1 = *(_QWORD *)(a1 + 40);
if ( v1 )
*(_QWORD *)(v1 + 48) = *(_QWORD *)(a1 + 48);
else
*(&kore_timers + 1) = *(_QWORD *)(a1 + 48);
**(_QWORD **)(a1 + 48) = *(_QWORD *)(a1 + 40);
kore_free();
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_timer_remove(struct kore_timer *timer)
{
 do { if (((timer)->list.tqe_next) != ((void *)0)) (timer)->list.tqe_next->list.tqe_prev = (timer)->list.tqe_prev; else (&kore_timers)->tqh_last = (timer)->list.tqe_prev; *(timer)->list.tqe_prev = (timer)->list.tqe_next; } while ( 0);
 kore_free(timer);
}

variable_types: struct kore_timer { unsigned int nextrun; unsigned int interval; int flags; void *arg; void (*cb)(void *, unsigned int); struct { struct kore_timer *tqe_next; struct kore_timer **tqe_prev; } list; } *


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

if ( !kore_timers )
return -1LL;
result = *(_QWORD *)kore_timers - a1;
if ( *(_QWORD *)kore_timers <= a1 )
result = 0LL;
return result;
}

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

if ( !kore_timers )
return -1LL;
result = *(_QWORD *)kore_timers - a1;
if ( *(_QWORD *)kore_timers <= a1 )
result = 0LL;
return result;
}

original_code:
u_int64_t
kore_timer_next_run(u_int64_t now)
{
 struct kore_timer *timer;
 if ((timer = ((&kore_timers)->tqh_first)) != ((void *)0)) {
  if (timer->nextrun > now)
   return (timer->nextrun - now);
  return (0);
 }
 return ((u_int64_t)-1);
}

canonical_original_code:
unsigned int kore_timer_next_run(unsigned int now)
{
 struct kore_timer *timer;
 if ((timer = ((&kore_timers)->tqh_first)) != ((void *)0)) {
  if (timer->nextrun > now)
   return (timer->nextrun - now);
  return (0);
 }
 return ((u_int64_t)-1);
}

variable_types:
now
unsigned int

return_type: unsigned int

user_defined_types: struct kore_timer { unsigned int nextrun; unsigned int interval; int flags; void *arg; void (*cb)(void *, unsigned int); struct { struct kore_timer *tqe_next; struct kore_timer **tqe_prev; } list; }


decompiled_code:
unsigned __int64 __fastcall kore_timer_run(unsigned __int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
unsigned __int64 v3; // rdx
unsigned __int64 *v4; // rax
__int64 *v5; // rax
__int64 v6; // rax
unsigned __int64 v8; // [rsp+8h] [rbp-20h]

v8 = __readfsqword(0x28u);
v1 = kore_timers;
if ( kore_timers )
{
v2 = 0LL;
do
{
if ( *(_QWORD *)v1 > a1 )
break;
v6 = *(_QWORD *)(v1 + 40);
if ( v6 )
*(_QWORD *)(v6 + 48) = *(_QWORD *)(v1 + 48);
else
*(&kore_timers + 1) = *(_QWORD *)(v1 + 48);
**(_QWORD **)(v1 + 48) = *(_QWORD *)(v1 + 40);
(*(void (__fastcall **)(_QWORD, unsigned __int64))(v1 + 32))(*(_QWORD *)(v1 + 24), a1);
if ( (*(_BYTE *)(v1 + 16) & 1) != 0 )
{
kore_free(v1);
}
else
{
v3 = *(_QWORD *)(v1 + 8) + a1;
*(_QWORD *)v1 = v3;
v4 = (unsigned __int64 *)kore_timers;
if ( kore_timers )
{
while ( v3 >= *v4 )
{
v4 = (unsigned __int64 *)v4[5];
if ( !v4 )
goto LABEL_7;
}
*(_QWORD *)(v1 + 48) = v4[6];
*(_QWORD *)(v1 + 40) = v4;
*(_QWORD *)v4[6] = v1;
v4[6] = v1 + 40;
v2 = v1;
}
else
{
LABEL_7:
*(_QWORD *)(v1 + 40) = 0LL;
v5 = (__int64 *)*(&kore_timers + 1);
*(_QWORD *)(v1 + 48) = v5;
*v5 = v1;
*(&kore_timers + 1) = v1 + 40;
v2 = v1;
}
}
v1 = kore_timers;
if ( v2 == kore_timers )
break;
}
while ( kore_timers );
}
return v8 - __readfsqword(0x28u);
}

original_code:
void
kore_timer_run(u_int64_t now)
{
 struct kore_timer *timer, *t, *prev;
 prev = ((void *)0);
 while ((timer = ((&kore_timers)->tqh_first)) != ((void *)0)) {
  if (timer == prev)
   break;
  if (timer->nextrun > now)
   break;
  do { if (((timer)->list.tqe_next) != ((void *)0)) (timer)->list.tqe_next->list.tqe_prev = (timer)->list.tqe_prev; else (&kore_timers)->tqh_last = (timer)->list.tqe_prev; *(timer)->list.tqe_prev = (timer)->list.tqe_next; } while ( 0);
  timer->cb(timer->arg, now);
  if (timer->flags & 0x01) {
   kore_free(timer);
  } else {
   prev = timer;
   timer->nextrun = now + timer->interval;
   for ((t) = ((&kore_timers)->tqh_first); (t); (t) = ((t)->list.tqe_next)) {
    if (t->nextrun > timer->nextrun) {
     do { (timer)->list.tqe_prev = (t)->list.tqe_prev; (timer)->list.tqe_next = (t); *(t)->list.tqe_prev = (timer); (t)->list.tqe_prev = &(timer)->list.tqe_next; } while ( 0);
     break;
    }
   }
   if (t == ((void *)0))
    do { (timer)->list.tqe_next = ((void *)0); (timer)->list.tqe_prev = (&kore_timers)->tqh_last; *(&kore_timers)->tqh_last = (timer); (&kore_timers)->tqh_last = &(timer)->list.tqe_next; } while ( 0);
  }
 }
}

canonical_original_code:
void kore_timer_run(unsigned int now)
{
 struct kore_timer *timer; struct kore_timer *t; struct kore_timer *prev;
 prev = ((void *)0);
 while ((timer = ((&kore_timers)->tqh_first)) != ((void *)0)) {
  if (timer == prev)
   break;
  if (timer->nextrun > now)
   break;
  do { if (((timer)->list.tqe_next) != ((void *)0)) (timer)->list.tqe_next->list.tqe_prev = (timer)->list.tqe_prev; else (&kore_timers)->tqh_last = (timer)->list.tqe_prev; *(timer)->list.tqe_prev = (timer)->list.tqe_next; } while ( 0);
  timer->cb(timer->arg, now);
  if (timer->flags & 0x01) {
   kore_free(timer);
  } else {
   prev = timer;
   timer->nextrun = now + timer->interval;
   for ((t) = ((&kore_timers)->tqh_first); (t); (t) = ((t)->list.tqe_next)) {
    if (t->nextrun > timer->nextrun) {
     do { (timer)->list.tqe_prev = (t)->list.tqe_prev; (timer)->list.tqe_next = (t); *(t)->list.tqe_prev = (timer); (t)->list.tqe_prev = &(timer)->list.tqe_next; } while ( 0);
     break;
    }
   }
   if (t == ((void *)0))
    do { (timer)->list.tqe_next = ((void *)0); (timer)->list.tqe_prev = (&kore_timers)->tqh_last; *(&kore_timers)->tqh_last = (timer); (&kore_timers)->tqh_last = &(timer)->list.tqe_next; } while ( 0);
  }
 }
}


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

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

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

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

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

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


decompiled_code:
unsigned __int64 kore_platform_init()
{
__int64 v0; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
kore_seccomp_init();
v0 = sysconf(84);
if ( v0 == -1 )
LOWORD(v0) = 1;
cpu_count = v0;
return v2 - __readfsqword(0x28u);
}

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

v2 = __readfsqword(0x28u);
kore_seccomp_init();
v0 = sysconf(84);
if ( v0 == -1 )
LOWORD(v0) = 1;
cpu_count = v0;
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_init(void)
{
 long n;
 kore_seccomp_init();
 if ((n = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
  cpu_count = 1;
 } else {
  cpu_count = (u_int16_t)n;
 }
}

canonical_original_code:
void kore_platform_init()
{
 long n;
 kore_seccomp_init();
 if ((n = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
  cpu_count = 1;
 } else {
  cpu_count = (unsigned int)n;
 }
}

variable_types: n long


decompiled_code:
unsigned __int64 kore_platform_event_init()
{
int *v1; // rax
char *v2; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
if ( efd != -1 )
close(efd);
if ( events )
kore_free();
efd = epoll_create(10000);
if ( efd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
fatal("epoll_create(): %s", v2);
}
event_count = worker_max_connections + nlisteners;
events = (struct epoll_event *)kore_calloc((unsigned int)event_count, 12LL);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
if ( efd != -1 )
close(efd);
if ( events )
kore_free();
efd = epoll_create(10000);
if ( efd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
fatal("epoll_create(): %s", v2);
}
event_count = worker_max_connections + nlisteners;
events = (struct epoll_event *)kore_calloc((unsigned int)event_count, 12LL);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_init(void)
{
 if (efd != -1)
  close(efd);
 if (events != ((void *)0))
  kore_free(events);
 if ((efd = epoll_create(10000)) == -1)
  fatal("epoll_create(): %s", strerror((*__errno_location ())));
 event_count = worker_max_connections + nlisteners;
 events = kore_calloc(event_count, sizeof(struct epoll_event));
}

canonical_original_code:
void kore_platform_event_init()
{
 if (efd != -1)
  close(efd);
 if (events != ((void *)0))
  kore_free(events);
 if ((efd = epoll_create(10000)) == -1)
  fatal("epoll_create(): %s", strerror((*__errno_location ())));
 event_count = worker_max_connections + nlisteners;
 events = kore_calloc(event_count, sizeof(struct epoll_event));
}

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

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


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

v1 = __readfsqword(0x28u);
if ( efd != -1 )
{
close(efd);
efd = -1;
}
if ( events )
{
kore_free();
events = 0LL;
}
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
if ( efd != -1 )
{
close(efd);
efd = -1;
}
if ( events )
{
kore_free();
events = 0LL;
}
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_cleanup(void)
{
 if (efd != -1) {
  close(efd);
  efd = -1;
 }
 if (events != ((void *)0)) {
  kore_free(events);
  events = ((void *)0);
 }
}

canonical_original_code:
void kore_platform_event_cleanup()
{
 if (efd != -1) {
  close(efd);
  efd = -1;
 }
 if (events != ((void *)0)) {
  kore_free(events);
  events = ((void *)0);
 }
}


decompiled_code:
unsigned __int64 __fastcall kore_platform_event_wait(__int64 timeout)
{
__int64 v1; // rbx
__int64 v2; // rbp
int v3; // ecx
int v4; // eax
int v5; // edi
char *v7; // rsi
struct epoll_event *v8; // rax
epoll_data_t v9; // rdx
unsigned __int64 v10; // [rsp+8h] [rbp-20h]

v10 = __readfsqword(0x28u);
v3 = -1;
if ( timeout != -1 )
v3 = timeout;
v4 = epoll_wait(efd, events, event_count, v3);
if ( v4 == -1 )
{
v5 = *_errno_location();
if ( v5 != 4 )
{
v7 = strerror(v5);
fatal("epoll_wait(): %s", v7);
goto LABEL_9;
}
}
else
{
v2 = (unsigned int)v4;
v1 = 0LL;
if ( v4 > 0 )
{
do
{
v7 = (char *)(unsigned int)v1;
v8 = &events[v1];
v9.ptr = (void *)v8->data;
if ( v9.u64 )
{
if ( (v8->events & 1) != 0 )
*(_DWORD *)(v9.u64 + 4) |= 1u;
if ( (v8->events & 4) != 0 )
*(_DWORD *)(v9.u64 + 4) |= 2u;
}
else
{
LABEL_9:
v8 = (struct epoll_event *)fatal("events[%d].data.ptr == NULL", v7);
}
(*(void (__fastcall **)(epoll_data_t, bool))(v9.u64 + 8))(v8->data, (v8->events & 0x2018) != 0);
++v1;
}
while ( v1 != v2 );
}
}
return v10 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 timeout)
{
__int64 v1; // rbx
__int64 v2; // rbp
int v3; // ecx
int v4; // eax
int v5; // edi
char *v7; // rsi
struct epoll_event *v8; // rax
epoll_data_t v9; // rdx
unsigned __int64 v10; // [rsp+8h] [rbp-20h]

v10 = __readfsqword(0x28u);
v3 = -1;
if ( timeout != -1 )
v3 = timeout;
v4 = epoll_wait(efd, events, event_count, v3);
if ( v4 == -1 )
{
v5 = *_errno_location();
if ( v5 != 4 )
{
v7 = strerror(v5);
fatal("epoll_wait(): %s", v7);
goto LABEL_9;
}
}
else
{
v2 = (unsigned int)v4;
v1 = 0LL;
if ( v4 > 0 )
{
do
{
v7 = (char *)(unsigned int)v1;
v8 = &events[v1];
v9.ptr = (void *)v8->data;
if ( v9.u64 )
{
if ( (v8->events & 1) != 0 )
*(_DWORD *)(v9.u64 + 4) |= 1u;
if ( (v8->events & 4) != 0 )
*(_DWORD *)(v9.u64 + 4) |= 2u;
}
else
{
LABEL_9:
v8 = (struct epoll_event *)fatal("events[%d].data.ptr == NULL", v7);
}
(*(void (__fastcall **)(epoll_data_t, bool))(v9.u64 + 8))(v8->data, (v8->events & 0x2018) != 0);
++v1;
}
while ( v1 != v2 );
}
}
return v10 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_wait(u_int64_t timer)
{
 u_int32_t r;
 struct kore_event *evt;
 int n, i, timeo;
 if (timer == (u_int64_t)-1)
  timeo = -1;
 else
  timeo = timer;
 n = epoll_wait(efd, events, event_count, timeo);
 if (n == -1) {
  if ((*__errno_location ()) == 4)
   return;
  fatal("epoll_wait(): %s", strerror((*__errno_location ())));
 }
 r = 0;
 for (i = 0; i < n; i++) {
  if (events[i].data.ptr == ((void *)0))
   fatal("events[%d].data.ptr == NULL", i);
  r = 0;
  evt = (struct kore_event *)events[i].data.ptr;
  if (events[i].events & EPOLLIN)
   evt->flags |= 0x01;
  if (events[i].events & EPOLLOUT)
   evt->flags |= 0x02;
  if (events[i].events & EPOLLERR ||
      events[i].events & EPOLLHUP ||
      events[i].events & EPOLLRDHUP)
   r = 1;
  evt->handle(events[i].data.ptr, r);
 }
}

canonical_original_code:
void kore_platform_event_wait(unsigned int timer)
{
 unsigned int r;
 struct kore_event *evt;
 int n; int i; int timeo;
 if (timer == (u_int64_t)-1)
  timeo = -1;
 else
  timeo = timer;
 n = epoll_wait(efd, events, event_count, timeo);
 if (n == -1) {
  if ((*__errno_location ()) == 4)
   return;
  fatal("epoll_wait(): %s", strerror((*__errno_location ())));
 }
 r = 0;
 for (i = 0; i < n; i++) {
  if (events[i].data.ptr == ((void *)0))
   fatal("events[%d].data.ptr == NULL", i);
  r = 0;
  evt = (struct kore_event *)events[i].data.ptr;
  if (events[i].events & EPOLLIN)
   evt->flags |= 0x01;
  if (events[i].events & EPOLLOUT)
   evt->flags |= 0x02;
  if (events[i].events & EPOLLERR ||
      events[i].events & EPOLLHUP ||
      events[i].events & EPOLLRDHUP)
   r = 1;
  evt->handle(events[i].data.ptr, r);
 }
}

variable_types:
timer
unsigned int

variable_types:
r
unsigned int

variable_types: struct kore_event { int type; int flags; void (*handle)(void *, int); } *

variable_types: n int

variable_types: timeo int


decompiled_code:
unsigned __int64 __fastcall kore_platform_event_schedule(int fd, uint32_t a2, __int64 a3, epoll_data_t a4)
{
int v5; // ebp
int *v7; // r12
const char *v8; // rdi
char *v9; // rax
char *v10; // rax
struct epoll_event event; // [rsp+Ch] [rbp-2Ch] BYREF
unsigned __int64 v12; // [rsp+18h] [rbp-20h]

v12 = __readfsqword(0x28u);
event.events = a2;
event.data = a4;
v5 = efd;
if ( epoll_ctl(efd, 1, fd, &event) == -1 )
{
v7 = _errno_location();
LODWORD(v8) = *v7;
if ( *v7 == 17 )
{
if ( epoll_ctl(v5, 3, fd, &event) != -1 )
return v12 - __readfsqword(0x28u);
v9 = strerror(*v7);
v8 = "epoll_ctl() MOD: %s";
fatal("epoll_ctl() MOD: %s", v9);
}
v10 = strerror((int)v8);
fatal("epoll_ctl() ADD: %s", v10);
}
return v12 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(int fd, uint32_t a2, __int64 a3, epoll_data_t a4)
{
int v5; // ebp
int *v7; // r12
const char *v8; // rdi
char *v9; // rax
char *v10; // rax
struct epoll_event event; // [rsp+Ch] [rbp-2Ch] BYREF
unsigned __int64 v12; // [rsp+18h] [rbp-20h]

v12 = __readfsqword(0x28u);
event.events = a2;
event.data = a4;
v5 = efd;
if ( epoll_ctl(efd, 1, fd, &event) == -1 )
{
v7 = _errno_location();
LODWORD(v8) = *v7;
if ( *v7 == 17 )
{
if ( epoll_ctl(v5, 3, fd, &event) != -1 )
return v12 - __readfsqword(0x28u);
v9 = strerror(*v7);
v8 = "epoll_ctl() MOD: %s";
fatal("epoll_ctl() MOD: %s", v9);
}
v10 = strerror((int)v8);
fatal("epoll_ctl() ADD: %s", v10);
}
return v12 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_schedule(int fd, int type, int flags, void *udata)
{
 struct epoll_event evt;
 evt.events = type;
 evt.data.ptr = udata;
 if (epoll_ctl(efd, 1, fd, &evt) == -1) {
  if ((*__errno_location ()) == 17) {
   if (epoll_ctl(efd, 3, fd, &evt) == -1)
    fatal("epoll_ctl() MOD: %s", strerror((*__errno_location ())));
  } else {
   fatal("epoll_ctl() ADD: %s", strerror((*__errno_location ())));
  }
 }
}

variable_types: fd int

variable_types: type int

variable_types: flags int

variable_types:
udata
void *

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


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

v3 = __readfsqword(0x28u);
kore_platform_event_schedule(a1, 0x2005u, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
func5(a1, 0x2005u, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_level_all(int fd, void *c)
{
 kore_platform_event_schedule(fd, EPOLLIN | EPOLLOUT | EPOLLRDHUP, 0, c);
}

variable_types:
c
void *


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

v3 = __readfsqword(0x28u);
kore_platform_event_schedule(a1, 0x2001u, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
func5(a1, 0x2001u, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_level_read(int fd, void *c)
{
 kore_platform_event_schedule(fd, EPOLLIN | EPOLLRDHUP, 0, c);
}


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

v3 = __readfsqword(0x28u);
kore_platform_event_schedule(a1, 0x80002005, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
func5(a1, 0x80002005, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_event_all(int fd, void *c)
{
 kore_platform_event_schedule(fd,
     EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET, 0, c);
}


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

v3 = __readfsqword(0x28u);
kore_platform_event_schedule(a1, 0x80000001, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
func5(a1, 0x80000001, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_schedule_read(int fd, void *data)
{
 kore_platform_event_schedule(fd, EPOLLIN | EPOLLET, 0, data);
}

variable_types:
data
void *


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

v3 = __readfsqword(0x28u);
kore_platform_event_schedule(a1, 0x80000004, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

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

v3 = __readfsqword(0x28u);
func5(a1, 0x80000004, 0LL, a2);
return v3 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_schedule_write(int fd, void *data)
{
 kore_platform_event_schedule(fd, EPOLLOUT | EPOLLET, 0, data);
}


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

v4 = __readfsqword(0x28u);
if ( epoll_ctl(efd, 2, fd, 0LL) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fatal("kore_platform_disable_read: %s", v3);
}
return v4 - __readfsqword(0x28u);
}

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

v4 = __readfsqword(0x28u);
if ( epoll_ctl(efd, 2, fd, 0LL) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fatal("kore_platform_disable_read: %s", v3);
}
return v4 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_disable_read(int fd)
{
 if (epoll_ctl(efd, 2, fd, ((void *)0)) == -1)
  fatal("kore_platform_disable_read: %s", strerror((*__errno_location ())));
}


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

v1 = __readfsqword(0x28u);
kore_proctitle();
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
kore_proctitle();
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_proctitle(const char *title)
{
 kore_proctitle(title);
}

canonical_original_code:
void kore_platform_proctitle(char *title)
{
 kore_proctitle(title);
}

variable_types:
title
char *


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

v1 = __readfsqword(0x28u);
kore_seccomp_enable();
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
kore_seccomp_enable();
return v1 - __readfsqword(0x28u);
}

original_code:
void
kore_platform_sandbox(void)
{
 kore_seccomp_enable();
}

canonical_original_code:
void kore_platform_sandbox()
{
 kore_seccomp_enable();
}


decompiled_code:
__int64 kore_platform_random_uint32()
{
__int64 v0; // rax
int *v2; // rax
char *v3; // rax
unsigned int v4; // [rsp+4h] [rbp-14h] BYREF
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
v0 = getrandom(&v4, 4LL, 0LL);
if ( v0 == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
v0 = fatalx("getrandom(): %s", v3);
LABEL_5:
fatalx("getrandom() %zd != %zu", v0, 4uLL);
}
if ( v0 != 4 )
goto LABEL_5;
return v4;
}

canonical_decompiled_code:
__int64 func17()
{
__int64 v0; // rax
int *v2; // rax
char *v3; // rax
unsigned int v4; // [rsp+4h] [rbp-14h] BYREF
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
v0 = getrandom(&v4, 4LL, 0LL);
if ( v0 == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
v0 = fatalx("getrandom(): %s", v3);
LABEL_5:
fatalx("getrandom() %zd != %zu", v0, 4uLL);
}
if ( v0 != 4 )
goto LABEL_5;
return v4;
}

original_code:
u_int32_t
kore_platform_random_uint32(void)
{
 ssize_t ret;
 u_int32_t val;
 if ((ret = getrandom(&val, sizeof(val), 0)) == -1)
  fatalx("getrandom(): %s", strerror((*__errno_location ())));
 if ((size_t)ret != sizeof(val))
  fatalx("getrandom() %zd != %zu", ret, sizeof(val));
 return (val);
}

canonical_original_code:
unsigned int kore_platform_random_uint32()
{
 long ret;
 unsigned int val;
 if ((ret = getrandom(&val, sizeof(val), 0)) == -1)
  fatalx("getrandom(): %s", strerror((*__errno_location ())));
 if ((unsigned long)ret != sizeof(val))
  fatalx("getrandom() %zd != %zu", ret, sizeof(val));
 return (val);
}

variable_types: ret long

variable_types:
val
unsigned int


decompiled_code:
unsigned __int64 __fastcall keymgr_entropy_request(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rax
_BYTE v4[1032]; // [rsp+0h] [rbp-418h] BYREF
unsigned __int64 v5; // [rsp+408h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( (unsigned int)RAND_bytes(v4, 1024LL) == 1 )
{
kore_msg_send(*(unsigned __int16 *)(a1 + 2), 6LL, v4, 1024LL);
}
else
{
v1 = ERR_get_error();
v2 = (const char *)ERR_error_string(v1, 0LL);
kore_log(4LL, "failed to generate entropy for worker %u: %s", *(unsigned __int16 *)(a1 + 2), v2);
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rax
_BYTE v4[1032]; // [rsp+0h] [rbp-418h] BYREF
unsigned __int64 v5; // [rsp+408h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( (unsigned int)RAND_bytes(v4, 1024LL) == 1 )
{
kore_msg_send(*(unsigned __int16 *)(a1 + 2), 6LL, v4, 1024LL);
}
else
{
v1 = ERR_get_error();
v2 = (const char *)ERR_error_string(v1, 0LL);
kore_log(4LL, "failed to generate entropy for worker %u: %s", *(unsigned __int16 *)(a1 + 2), v2);
}
return v5 - __readfsqword(0x28u);
}

original_code:
static void
keymgr_entropy_request(struct kore_msg *msg, const void *data)
{
 u_int8_t buf[1024];
 if (RAND_bytes(buf, sizeof(buf)) != 1) {
  kore_log(4,
      "failed to generate entropy for worker %u: %s",
      msg->src, ERR_error_string(ERR_get_error(), ((void *)0)));
  return;
 }
 kore_msg_send(msg->src, 6, buf, sizeof(buf));
}

canonical_original_code:
static void keymgr_entropy_request(struct kore_msg *msg, void *data)
{
 unsigned int buf[1024];
 if (RAND_bytes(buf, sizeof(buf)) != 1) {
  kore_log(4,
      "failed to generate entropy for worker %u: %s",
      msg->src, ERR_error_string(ERR_get_error(), ((void *)0)));
  return;
 }
 kore_msg_send(msg->src, 6, buf, sizeof(buf));
}

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

variable_types:
buf
unsigned int [1024]


decompiled_code:
unsigned __int64 __fastcall keymgr_load_randfile()
{
__int64 v0; // rbx
int v1; // eax
int v2; // ebp
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
ssize_t v7; // rax
int v8; // edi
char *v9; // rax
int *v11; // rax
char *v12; // rax
struct stat v13; // [rsp+0h] [rbp-4B8h] BYREF
char buf[1032]; // [rsp+90h] [rbp-428h] BYREF
unsigned __int64 v15; // [rsp+498h] [rbp-20h]

v15 = __readfsqword(0x28u);
if ( !kore_rand_file )
return v15 - __readfsqword(0x28u);
v1 = open(kore_rand_file, 0);
v2 = v1;
if ( v1 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatalx("open(%s): %s", kore_rand_file, v4);
goto LABEL_8;
}
if ( fstat(v1, &v13) == -1 )
{
LABEL_8:
v5 = _errno_location();
v6 = strerror(*v5);
fatalx("stat(%s): %s", kore_rand_file, v6);
goto LABEL_9;
}
if ( (v13.st_mode & 0xF000) != 0x8000 )
{
LABEL_9:
fatalx("%s is not a file", kore_rand_file);
goto LABEL_10;
}
if ( v13.st_size != 1024 )
{
LABEL_10:
fatalx("%s has an invalid size", kore_rand_file);
goto LABEL_11;
}
v0 = 0LL;
while ( 1 )
{
v7 = read(v2, buf, 0x400uLL);
if ( v7 )
break;
LABEL_11:
v7 = fatalx("EOF on %s", kore_rand_file);
LABEL_12:
v0 += v7;
RAND_seed(buf, (unsigned int)v7);
OPENSSL_cleanse(buf, 1024LL);
LABEL_13:
if ( v0 == 1024 )
goto LABEL_18;
}
if ( v7 != -1 )
goto LABEL_12;
v8 = *_errno_location();
if ( v8 == 4 )
goto LABEL_13;
v9 = strerror(v8);
fatalx("read(%s): %s", kore_rand_file, v9);
LABEL_18:
close(v2);
if ( unlink(kore_rand_file) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
kore_log(4LL, "failed to unlink %s: %s", kore_rand_file, v12);
}
return v15 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3()
{
__int64 v0; // rbx
int v1; // eax
int v2; // ebp
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
ssize_t v7; // rax
int v8; // edi
char *v9; // rax
int *v11; // rax
char *v12; // rax
struct stat v13; // [rsp+0h] [rbp-4B8h] BYREF
char buf[1032]; // [rsp+90h] [rbp-428h] BYREF
unsigned __int64 v15; // [rsp+498h] [rbp-20h]

v15 = __readfsqword(0x28u);
if ( !kore_rand_file )
return v15 - __readfsqword(0x28u);
v1 = open(kore_rand_file, 0);
v2 = v1;
if ( v1 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatalx("open(%s): %s", kore_rand_file, v4);
goto LABEL_8;
}
if ( fstat(v1, &v13) == -1 )
{
LABEL_8:
v5 = _errno_location();
v6 = strerror(*v5);
fatalx("stat(%s): %s", kore_rand_file, v6);
goto LABEL_9;
}
if ( (v13.st_mode & 0xF000) != 0x8000 )
{
LABEL_9:
fatalx("%s is not a file", kore_rand_file);
goto LABEL_10;
}
if ( v13.st_size != 1024 )
{
LABEL_10:
fatalx("%s has an invalid size", kore_rand_file);
goto LABEL_11;
}
v0 = 0LL;
while ( 1 )
{
v7 = read(v2, buf, 0x400uLL);
if ( v7 )
break;
LABEL_11:
v7 = fatalx("EOF on %s", kore_rand_file);
LABEL_12:
v0 += v7;
RAND_seed(buf, (unsigned int)v7);
OPENSSL_cleanse(buf, 1024LL);
LABEL_13:
if ( v0 == 1024 )
goto LABEL_18;
}
if ( v7 != -1 )
goto LABEL_12;
v8 = *_errno_location();
if ( v8 == 4 )
goto LABEL_13;
v9 = strerror(v8);
fatalx("read(%s): %s", kore_rand_file, v9);
LABEL_18:
close(v2);
if ( unlink(kore_rand_file) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
kore_log(4LL, "failed to unlink %s: %s", kore_rand_file, v12);
}
return v15 - __readfsqword(0x28u);
}

original_code:
static void
keymgr_load_randfile(void)
{
 int fd;
 struct stat st;
 ssize_t ret;
 size_t total;
 u_int8_t buf[1024];
 if (kore_rand_file == ((void *)0))
  return;
 if ((fd = open(kore_rand_file, 00)) == -1)
  fatalx("open(%s): %s", kore_rand_file, strerror((*__errno_location ())));
 if (fstat(fd, &st) == -1)
  fatalx("stat(%s): %s", kore_rand_file, strerror((*__errno_location ())));
 if (!((((st.st_mode)) & 0170000) == (0100000)))
  fatalx("%s is not a file", kore_rand_file);
 if (st.st_size != 1024)
  fatalx("%s has an invalid size", kore_rand_file);
 total = 0;
 while (total != 1024) {
  ret = read(fd, buf, sizeof(buf));
  if (ret == 0)
   fatalx("EOF on %s", kore_rand_file);
  if (ret == -1) {
   if ((*__errno_location ()) == 4)
    continue;
   fatalx("read(%s): %s", kore_rand_file, strerror((*__errno_location ())));
  }
  total += (size_t)ret;
  RAND_seed(buf, (int)ret);
  OPENSSL_cleanse(buf, sizeof(buf));
 }
 (void)close(fd);
 if (unlink(kore_rand_file) == -1) {
  kore_log(4, "failed to unlink %s: %s",
      kore_rand_file, strerror((*__errno_location ())));
 }
}

canonical_original_code:
static void keymgr_load_randfile()
{
 int fd;
 struct stat st;
 long ret;
 unsigned long total;
 unsigned int buf[1024];
 if (kore_rand_file == ((void *)0))
  return;
 if ((fd = open(kore_rand_file, 00)) == -1)
  fatalx("open(%s): %s", kore_rand_file, strerror((*__errno_location ())));
 if (fstat(fd, &st) == -1)
  fatalx("stat(%s): %s", kore_rand_file, strerror((*__errno_location ())));
 if (!((((st.st_mode)) & 0170000) == (0100000)))
  fatalx("%s is not a file", kore_rand_file);
 if (st.st_size != 1024)
  fatalx("%s has an invalid size", kore_rand_file);
 total = 0;
 while (total != 1024) {
  ret = read(fd, buf, sizeof(buf));
  if (ret == 0)
   fatalx("EOF on %s", kore_rand_file);
  if (ret == -1) {
   if ((*__errno_location ()) == 4)
    continue;
   fatalx("read(%s): %s", kore_rand_file, strerror((*__errno_location ())));
  }
  total += (unsigned long)ret;
  RAND_seed(buf, (int)ret);
  OPENSSL_cleanse(buf, sizeof(buf));
 }
 (void)close(fd);
 if (unlink(kore_rand_file) == -1) {
  kore_log(4, "failed to unlink %s: %s",
      kore_rand_file, strerror((*__errno_location ())));
 }
}

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:
total
unsigned long


decompiled_code:
unsigned __int64 keymgr_save_randfile()
{
__int64 v0; // rax
const char *v1; // rax
int v3; // eax
int v4; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
struct stat v11; // [rsp+0h] [rbp-4A8h] BYREF
char buf[1032]; // [rsp+90h] [rbp-418h] BYREF
unsigned __int64 v13; // [rsp+498h] [rbp-10h]

v13 = __readfsqword(0x28u);
if ( kore_rand_file )
{
if ( stat("rnd.tmp", &v11) != -1 )
{
kore_log(4LL, "removing stale %s file", "rnd.tmp");
unlink("rnd.tmp");
}
if ( (unsigned int)RAND_bytes(buf, 1024LL) == 1 )
{
v3 = open("rnd.tmp", 577, 256LL);
v4 = v3;
if ( v3 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
kore_log(4LL, "failed to open %s: %s - random data not written", "rnd.tmp", v6);
}
else if ( write(v3, buf, 0x400uLL) == 1024 )
{
if ( close(v4) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
kore_log(4LL, "close(%s): %s", "rnd.tmp", v10);
}
if ( rename("rnd.tmp", kore_rand_file) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "rename(%s, %s): %s", "rnd.tmp", kore_rand_file, v8);
unlink(kore_rand_file);
unlink("rnd.tmp");
}
}
else
{
kore_log(4LL, "failed to write random data");
close(v4);
unlink("rnd.tmp");
}
}
else
{
v0 = ERR_get_error();
v1 = (const char *)ERR_error_string(v0, 0LL);
kore_log(4LL, "RAND_bytes: %s", v1);
}
OPENSSL_cleanse(buf, 1024LL);
}
return v13 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func4()
{
__int64 v0; // rax
const char *v1; // rax
int v3; // eax
int v4; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
struct stat v11; // [rsp+0h] [rbp-4A8h] BYREF
char buf[1032]; // [rsp+90h] [rbp-418h] BYREF
unsigned __int64 v13; // [rsp+498h] [rbp-10h]

v13 = __readfsqword(0x28u);
if ( kore_rand_file )
{
if ( stat("rnd.tmp", &v11) != -1 )
{
kore_log(4LL, "removing stale %s file", "rnd.tmp");
unlink("rnd.tmp");
}
if ( (unsigned int)RAND_bytes(buf, 1024LL) == 1 )
{
v3 = open("rnd.tmp", 577, 256LL);
v4 = v3;
if ( v3 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
kore_log(4LL, "failed to open %s: %s - random data not written", "rnd.tmp", v6);
}
else if ( write(v3, buf, 0x400uLL) == 1024 )
{
if ( close(v4) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
kore_log(4LL, "close(%s): %s", "rnd.tmp", v10);
}
if ( rename("rnd.tmp", kore_rand_file) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "rename(%s, %s): %s", "rnd.tmp", kore_rand_file, v8);
unlink(kore_rand_file);
unlink("rnd.tmp");
}
}
else
{
kore_log(4LL, "failed to write random data");
close(v4);
unlink("rnd.tmp");
}
}
else
{
v0 = ERR_get_error();
v1 = (const char *)ERR_error_string(v0, 0LL);
kore_log(4LL, "RAND_bytes: %s", v1);
}
OPENSSL_cleanse(buf, 1024LL);
}
return v13 - __readfsqword(0x28u);
}

original_code:
static void
keymgr_save_randfile(void)
{
 int fd;
 struct stat st;
 ssize_t ret;
 u_int8_t buf[1024];
 if (kore_rand_file == ((void *)0))
  return;
 if (stat("rnd.tmp", &st) != -1) {
  kore_log(4, "removing stale %s file", "rnd.tmp");
  (void)unlink("rnd.tmp");
 }
 if (RAND_bytes(buf, sizeof(buf)) != 1) {
  kore_log(4, "RAND_bytes: %s", ERR_error_string(ERR_get_error(), ((void *)0)));
  goto cleanup;
 }
 if ((fd = open("rnd.tmp",
     0100 | 01000 | 01, 0400)) == -1) {
  kore_log(4,
      "failed to open %s: %s - random data not written",
      "rnd.tmp", strerror((*__errno_location ())));
  goto cleanup;
 }
 ret = write(fd, buf, sizeof(buf));
 if (ret == -1 || (size_t)ret != sizeof(buf)) {
  kore_log(4, "failed to write random data");
  (void)close(fd);
  (void)unlink("rnd.tmp");
  goto cleanup;
 }
 if (close(fd) == -1)
  kore_log(4, "close(%s): %s", "rnd.tmp", strerror((*__errno_location ())));
 if (rename("rnd.tmp", kore_rand_file) == -1) {
  kore_log(4, "rename(%s, %s): %s",
      "rnd.tmp", kore_rand_file, strerror((*__errno_location ())));
  (void)unlink(kore_rand_file);
  (void)unlink("rnd.tmp");
 }
cleanup:
 OPENSSL_cleanse(buf, sizeof(buf));
}

canonical_original_code:
static void keymgr_save_randfile()
{
 int fd;
 struct stat st;
 long ret;
 unsigned int buf[1024];
 if (kore_rand_file == ((void *)0))
  return;
 if (stat("rnd.tmp", &st) != -1) {
  kore_log(4, "removing stale %s file", "rnd.tmp");
  (void)unlink("rnd.tmp");
 }
 if (RAND_bytes(buf, sizeof(buf)) != 1) {
  kore_log(4, "RAND_bytes: %s", ERR_error_string(ERR_get_error(), ((void *)0)));
  goto cleanup;
 }
 if ((fd = open("rnd.tmp",
     0100 | 01000 | 01, 0400)) == -1) {
  kore_log(4,
      "failed to open %s: %s - random data not written",
      "rnd.tmp", strerror((*__errno_location ())));
  goto cleanup;
 }
 ret = write(fd, buf, sizeof(buf));
 if (ret == -1 || (unsigned long)ret != sizeof(buf)) {
  kore_log(4, "failed to write random data");
  (void)close(fd);
  (void)unlink("rnd.tmp");
  goto cleanup;
 }
 if (close(fd) == -1)
  kore_log(4, "close(%s): %s", "rnd.tmp", strerror((*__errno_location ())));
 if (rename("rnd.tmp", kore_rand_file) == -1) {
  kore_log(4, "rename(%s, %s): %s",
      "rnd.tmp", kore_rand_file, strerror((*__errno_location ())));
  (void)unlink(kore_rand_file);
  (void)unlink("rnd.tmp");
 }
cleanup:
 OPENSSL_cleanse(buf, sizeof(buf));
}

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


decompiled_code:
unsigned __int64 __fastcall keymgr_x509_msg(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4, unsigned __int8 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 func6(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4, unsigned __int8 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[]; }


decompiled_code:
unsigned __int64 kore_keymgr_run()
{
__int64 v0; // rbp
__int64 v2; // rax
__int64 v3; // rbx
__int64 v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( !kore_keymgr_active )
{
fatalx("%s: called with kore_keymgr_active == 0", "kore_keymgr_run");
LABEL_6:
if ( !kore_quiet )
kore_log(4LL, "no rand_file location specified");
goto LABEL_4;
}
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 )
goto LABEL_6;
keymgr_load_randfile();
keymgr_save_randfile();
LABEL_4:
RAND_poll();
initialized = 1;
keymgr_reload();
kore_worker_started();
v0 = 0LL;
while ( 1 )
{
v3 = kore_time_ms();
if ( (unsigned __int64)(v3 - v0) > 0x1B7740 )
{
RAND_poll();
v0 = v3;
}
v4 = kore_timer_next_run(v3);
kore_platform_event_wait(v4);
if ( sig_recv )
break;
LABEL_12:
v2 = kore_time_ms();
kore_timer_run(v2);
kore_connection_prune(0LL);
}
if ( sig_recv == 10 )
{
keymgr_reload();
goto LABEL_11;
}
if ( sig_recv <= 10 )
{
if ( (unsigned int)(sig_recv - 2) <= 1 )
goto LABEL_9;
goto LABEL_11;
}
if ( sig_recv != 15 )
{
LABEL_11:
sig_recv = 0;
goto LABEL_12;
}
LABEL_9:
sig_recv = 0;
kore_keymgr_cleanup();
kore_platform_event_cleanup(1LL);
kore_connection_cleanup();
net_cleanup();
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func12()
{
__int64 v0; // rbp
__int64 v2; // rax
__int64 v3; // rbx
__int64 v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( !kore_keymgr_active )
{
fatalx("%s: called with kore_keymgr_active == 0", "kore_keymgr_run");
LABEL_6:
if ( !kore_quiet )
kore_log(4LL, "no rand_file location specified");
goto LABEL_4;
}
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 )
goto LABEL_6;
func3();
func4();
LABEL_4:
RAND_poll();
initialized = 1;
func11();
kore_worker_started();
v0 = 0LL;
while ( 1 )
{
v3 = kore_time_ms();
if ( (unsigned __int64)(v3 - v0) > 0x1B7740 )
{
RAND_poll();
v0 = v3;
}
v4 = kore_timer_next_run(v3);
kore_platform_event_wait(v4);
if ( sig_recv )
break;
LABEL_12:
v2 = kore_time_ms();
kore_timer_run(v2);
kore_connection_prune(0LL);
}
if ( sig_recv == 10 )
{
func11();
goto LABEL_11;
}
if ( sig_recv <= 10 )
{
if ( (unsigned int)(sig_recv - 2) <= 1 )
goto LABEL_9;
goto LABEL_11;
}
if ( sig_recv != 15 )
{
LABEL_11:
sig_recv = 0;
goto LABEL_12;
}
LABEL_9:
sig_recv = 0;
func10();
kore_platform_event_cleanup(1LL);
kore_connection_cleanup();
net_cleanup();
return v5 - __readfsqword(0x28u);
}

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


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
unsigned __int64 v5; // [rsp+8h] [rbp-20h]

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

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

v5 = __readfsqword(0x28u);
v2 = refs;
if ( !refs )
goto LABEL_2;
do
{
v3 = v2;
v2 = *(_QWORD *)(v2 + 72);
if ( !*(_DWORD *)v3 && *(_QWORD *)(v3 + 48) <= a2 )
func1(v3);
}
while ( v2 );
if ( !refs )
{
LABEL_2:
*(_DWORD *)(ref_timer + 16) |= 1u;
ref_timer = 0LL;
}
return v5 - __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:
unsigned __int64 __fastcall fileref_soft_remove(const char *a1)
{
__int64 v1; // rax
const char *v3; // rsi
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
if ( (a1[5] & 0x10) != 0 )
{
v3 = a1;
a1 = "fileref_soft_remove: %p already removed";
fatal("fileref_soft_remove: %p already removed", v3);
}
else
{
v1 = *((_QWORD *)a1 + 9);
if ( v1 )
{
*(_QWORD *)(v1 + 80) = *((_QWORD *)a1 + 10);
goto LABEL_4;
}
}
qword_658 = *((_QWORD *)a1 + 10);
LABEL_4:
**((_QWORD **)a1 + 10) = *((_QWORD *)a1 + 9);
*((_DWORD *)a1 + 1) |= 0x1000u;
if ( !*(_DWORD *)a1 )
fileref_drop((__int64)a1);
return v4 - __readfsqword(0x28u);
}

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

v4 = __readfsqword(0x28u);
if ( (a1[5] & 0x10) != 0 )
{
v3 = a1;
a1 = "fileref_soft_remove: %p already removed";
fatal("fileref_soft_remove: %p already removed", v3);
}
else
{
v1 = *((_QWORD *)a1 + 9);
if ( v1 )
{
*(_QWORD *)(v1 + 80) = *((_QWORD *)a1 + 10);
goto LABEL_4;
}
}
qword_658 = *((_QWORD *)a1 + 10);
LABEL_4:
**((_QWORD **)a1 + 10) = *((_QWORD *)a1 + 9);
*((_DWORD *)a1 + 1) |= 0x1000u;
if ( !*(_DWORD *)a1 )
func1((__int64)a1);
return v4 - __readfsqword(0x28u);
}

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:
unsigned __int64 kore_fileref_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
refs = 0LL;
qword_658 = (__int64)&refs;
kore_pool_init(&ref_pool, "ref_pool", 88LL, 100LL);
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
refs = 0LL;
qword_658 = (__int64)&refs;
kore_pool_init(&ref_pool, "ref_pool", 88LL, 100LL);
return v1 - __readfsqword(0x28u);
}

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; // rbx
int v3; // edi
char *v4; // rax
const char *v5; // rbp
__int64 v6; // rax
__int64 v7; // rax
struct stat v9; // [rsp+0h] [rbp-C8h] BYREF
unsigned __int64 v10; // [rsp+98h] [rbp-30h]

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

canonical_decompiled_code:
__int64 __fastcall func5(char *s2, int a2)
{
__int64 v2; // rbx
int v3; // edi
char *v4; // rax
const char *v5; // rbp
__int64 v6; // rax
__int64 v7; // rax
struct stat v9; // [rsp+0h] [rbp-C8h] BYREF
unsigned __int64 v10; // [rsp+98h] [rbp-30h]

v10 = __readfsqword(0x28u);
v2 = refs;
if ( !refs )
return v2;
while ( 1 )
{
v5 = *(const char **)(v2 + 24);
if ( !strcmp(v5, s2) && *(_DWORD *)(v2 + 8) == a2 )
break;
v2 = *(_QWORD *)(v2 + 72);
if ( !v2 )
return v2;
}
if ( stat(v5, &v9) == -1 )
{
v3 = *_errno_location();
if ( v3 != 2 )
{
v4 = strerror(v3);
kore_log(3LL, "stat(%s): %s", *(const char **)(v2 + 24), v4);
}
goto LABEL_5;
}
if ( *(_QWORD *)(v2 + 32) != 1000 * v9.st_mtim.tv_sec + v9.st_mtim.tv_nsec / 1000000 )
{
LABEL_5:
func3((const char *)v2);
return 0LL;
}
++*(_DWORD *)v2;
v6 = *(_QWORD *)(v2 + 72);
if ( v6 )
*(_QWORD *)(v6 + 80) = *(_QWORD *)(v2 + 80);
else
qword_658 = *(_QWORD *)(v2 + 80);
**(_QWORD **)(v2 + 80) = *(_QWORD *)(v2 + 72);
v7 = refs;
*(_QWORD *)(v2 + 72) = refs;
if ( v7 )
*(_QWORD *)(v7 + 80) = v2 + 72;
else
qword_658 = 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:
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; } *


decompiled_code:
unsigned __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);
goto LABEL_7;
}
if ( v1 == 1 )
{
if ( (*((_BYTE *)a1 + 5) & 0x10) != 0 )
{
fileref_drop((__int64)a1);
return v4 - __readfsqword(0x28u);
}
LABEL_7:
*((_QWORD *)a1 + 6) = kore_time_ms() + 30000;
}
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __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);
goto LABEL_7;
}
if ( v1 == 1 )
{
if ( (*((_BYTE *)a1 + 5) & 0x10) != 0 )
{
func1((__int64)a1);
return v4 - __readfsqword(0x28u);
}
LABEL_7:
*((_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()
{
__int16 *v0; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
v0 = (__int16 *)&cached;
do
{
*(_QWORD *)v0 = 0LL;
v0 += 4;
}
while ( v0 != &domain_id );
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func0()
{
__int16 *v0; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
v0 = (__int16 *)&cached;
do
{
*(_QWORD *)v0 = 0LL;
v0 += 4;
}
while ( v0 != &domain_id );
return v2 - __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:
unsigned __int64 kore_accesslog_worker_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
kore_domain_closelogs();
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
kore_domain_closelogs();
return v1 - __readfsqword(0x28u);
}

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

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


decompiled_code:
unsigned __int64 __fastcall kore_accesslog_run(__int64 a1, __int64 a2)
{
int v2; // edx
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v2 = ticks_0++;
kore_accesslog_gather(a1, a2, v2 == 100 * (v2 / 100));
return v4 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(__int64 a1, __int64 a2)
{
int v2; // edx
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
v2 = ticks_0++;
func6(a1, a2, v2 == 100 * (v2 / 100));
return v4 - __readfsqword(0x28u);
}

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:
unsigned __int64 net_cleanup()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
kore_pool_cleanup(&nb_pool);
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
kore_pool_cleanup(&nb_pool);
return v1 - __readfsqword(0x28u);
}

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


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:
unsigned __int64 kore_connection_cleanup()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
kore_connection_prune(1);
kore_pool_cleanup(&connection_pool);
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
func5(1);
kore_pool_cleanup(&connection_pool);
return v1 - __readfsqword(0x28u);
}

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
unsigned int v3; // edx
int v4; // eax
int v6; // eax

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

canonical_decompiled_code:
__int64 __fastcall func12(int fd, int a2)
{
unsigned int v2; // eax
unsigned int v3; // edx
int v4; // eax
int v6; // eax

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

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


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:
unsigned __int64 kore_log_init()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
fp = stdout;
if ( !kore_foreground )
openlog("kore", 9, 24);
kore_msg_register(12LL, log_from_worker);
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
fp = stdout;
if ( !kore_foreground )
openlog("kore", 9, 24);
kore_msg_register(12LL, log_from_worker);
return v1 - __readfsqword(0x28u);
}

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, ...)
{
const char *v3; // rdx
__int64 v4[3]; // [rsp+0h] [rbp-138h] BYREF
__int64 v5; // [rsp+18h] [rbp-120h]
__int64 v6[4]; // [rsp+20h] [rbp-118h] BYREF
__int128 v7; // [rsp+40h] [rbp-F8h] BYREF
gcc_va_list va; // [rsp+50h] [rbp-E8h] BYREF
unsigned __int64 v9; // [rsp+68h] [rbp-D0h]

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

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

va_start(va, a2);
v9 = __readfsqword(0x28u);
kore_buf_init(v4, 128LL);
kore_buf_appendv(v4, a2, va);
if ( worker )
{
kore_buf_init(v6, v5 + 16);
v7 = a1;
*(_QWORD *)((char *)&v7 + 4) = (unsigned __int16)*worker;
*((_QWORD *)&v7 + 1) = v5;
kore_buf_append(v6, &v7, 16LL);
kore_buf_append(v6, v4[0], v5);
kore_msg_send(1000LL, 12LL, v6[0], v6[3]);
kore_buf_cleanup(v6);
}
else
{
v3 = (const char *)kore_buf_stringify(v4, 0LL);
if ( !kore_foreground && fp == stdout )
_syslog_chk(a1, 1LL, "proc=[parent] log=[%s]", v3);
else
func0(a1, (__int64)"proc=[parent] log=[%s]\n", v3);
}
kore_buf_cleanup(v4);
return v9 - __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; }