decompiled_code:
__int64 __fastcall kore_buf_alloc(__int64 a1)
{
__int64 v1; // r12

v1 = kore_malloc(32LL);
kore_buf_init(v1, a1);
*(_DWORD *)(v1 + 8) = 1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // r12

v1 = kore_malloc(32LL);
func0(v1, a1);
*(_DWORD *)(v1 + 8) = 1;
return v1;
}

original_code:
struct kore_buf *
kore_buf_alloc(size_t initial)
{
 struct kore_buf *buf;
 buf = kore_malloc(sizeof(*buf));
 kore_buf_init(buf, initial);
 buf->flags = 0x0001;
 return (buf);
}

canonical_original_code:
struct kore_buf *kore_buf_alloc(unsigned long initial)
{
 struct kore_buf *buf;
 buf = kore_malloc(sizeof(*buf));
 kore_buf_init(buf, initial);
 buf->flags = 0x0001;
 return (buf);
}

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


repo: jorisvink/kore

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

v2 = __readfsqword(0x28u);
kore_free(*a1);
*a1 = 0LL;
a1[3] = 0LL;
a1[2] = 0LL;
return v2 - __readfsqword(0x28u);
}

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

v2 = __readfsqword(0x28u);
kore_free(*a1);
*a1 = 0LL;
a1[3] = 0LL;
a1[2] = 0LL;
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_cleanup(struct kore_buf *buf)
{
 kore_free(buf->data);
 buf->data = ((void *)0);
 buf->offset = 0;
 buf->length = 0;
}

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

return_type: void


decompiled_code:
__int64 __fastcall kore_buf_free(__int64 a1)
{
__int64 result; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
kore_buf_cleanup((_QWORD *)a1);
if ( (*(_BYTE *)(a1 + 8) & 1) != 0 )
result = kore_free(a1);
else
result = v2 - __readfsqword(0x28u);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
func2((_QWORD *)a1);
if ( (*(_BYTE *)(a1 + 8) & 1) != 0 )
result = kore_free(a1);
else
result = v2 - __readfsqword(0x28u);
return result;
}

original_code:
void
kore_buf_free(struct kore_buf *buf)
{
 kore_buf_cleanup(buf);
 if (buf->flags & 0x0001)
  kore_free(buf);
}

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


decompiled_code:
unsigned __int64 __fastcall kore_buf_append(__int64 *a1, const void *a2, size_t a3)
{
__int64 v4; // rax
size_t v5; // rdx
unsigned __int64 v7; // rsi
__int64 v9; // rdi
__int64 v11; // rax
unsigned __int64 v12; // [rsp+8h] [rbp-20h]

v12 = __readfsqword(0x28u);
v4 = a1[3];
v5 = a3 + v4;
if ( __CFADD__(a3, v4) )
fatal("overflow in kore_buf_append", a2, v5);
v7 = a1[2];
v9 = *a1;
if ( v5 > v7 )
{
a1[2] = a3 + v7;
v11 = kore_realloc(v9);
*a1 = v11;
v9 = v11;
v4 = a1[3];
}
memcpy((void *)(v4 + v9), a2, a3);
a1[3] += a3;
return v12 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 *a1, const void *a2, size_t a3)
{
__int64 v4; // rax
size_t v5; // rdx
unsigned __int64 v7; // rsi
__int64 v9; // rdi
__int64 v11; // rax
unsigned __int64 v12; // [rsp+8h] [rbp-20h]

v12 = __readfsqword(0x28u);
v4 = a1[3];
v5 = a3 + v4;
if ( __CFADD__(a3, v4) )
fatal("overflow in kore_buf_append", a2, v5);
v7 = a1[2];
v9 = *a1;
if ( v5 > v7 )
{
a1[2] = a3 + v7;
v11 = kore_realloc(v9);
*a1 = v11;
v9 = v11;
v4 = a1[3];
}
memcpy((void *)(v4 + v9), a2, a3);
a1[3] += a3;
return v12 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_append(struct kore_buf *buf, const void *data, size_t len)
{
 if ((buf->offset + len) < len)
  fatal("overflow in kore_buf_append");
 if ((buf->offset + len) > buf->length) {
  buf->length += len;
  buf->data = kore_realloc(buf->data, buf->length);
 }
 memcpy((buf->data + buf->offset), data, len);
 buf->offset += len;
}

canonical_original_code:
void kore_buf_append(struct kore_buf *buf, void *data, unsigned long len)
{
 if ((buf->offset + len) < len)
  fatal("overflow in kore_buf_append");
 if ((buf->offset + len) > buf->length) {
  buf->length += len;
  buf->data = kore_realloc(buf->data, buf->length);
 }
 memcpy((buf->data + buf->offset), data, len);
 buf->offset += len;
}

variable_types:
data
void *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall kore_buf_appendv(__int64 *a1, __int64 a2, const __m128i *a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
unsigned int v7; // eax
__int64 v8; // rdx
char *v9; // rsi
__int64 v11; // rdx
int v12; // edx
int v13; // ecx
int v14; // er8
int v15; // er9
void *ptr; // [rsp+0h] [rbp-2048h] BYREF
__m128i v17; // [rsp+8h] [rbp-2040h] BYREF
__int64 v18; // [rsp+18h] [rbp-2030h]
char v19[8200]; // [rsp+20h] [rbp-2028h] BYREF
unsigned __int64 v20; // [rsp+2028h] [rbp-20h]

v20 = __readfsqword(0x28u);
v17 = _mm_loadu_si128(a3);
v18 = a3[1].m128i_i64[0];
v7 = _vsnprintf_chk(v19, 0x2000LL, 1LL, 0x2000LL, a2, a3);
if ( v7 == -1 )
fatal("kore_buf_appendv(): vsnprintf error", 0x2000LL, v8);
if ( v7 > 0x1FFF )
{
v7 = _vasprintf_chk(&ptr, 1LL, a2, &v17);
if ( v7 == -1 )
{
fatal("kore_buf_appendv(): error or truncation", 1LL, v11);
return kore_buf_appendf((unsigned int)"kore_buf_appendv(): error or truncation", 1, v12, v13, v14, v15, a7);
}
v9 = (char *)ptr;
}
else
{
ptr = v19;
v9 = v19;
}
kore_buf_append(a1, v9, (int)v7);
if ( ptr != v19 )
free(ptr);
return v20 - __readfsqword(0x28u);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, __int64 a2, const __m128i *a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
unsigned int v7; // eax
__int64 v8; // rdx
char *v9; // rsi
__int64 v11; // rdx
int v12; // edx
int v13; // ecx
int v14; // er8
int v15; // er9
void *ptr; // [rsp+0h] [rbp-2048h] BYREF
__m128i v17; // [rsp+8h] [rbp-2040h] BYREF
__int64 v18; // [rsp+18h] [rbp-2030h]
char v19[8200]; // [rsp+20h] [rbp-2028h] BYREF
unsigned __int64 v20; // [rsp+2028h] [rbp-20h]

v20 = __readfsqword(0x28u);
v17 = _mm_loadu_si128(a3);
v18 = a3[1].m128i_i64[0];
v7 = _vsnprintf_chk(v19, 0x2000LL, 1LL, 0x2000LL, a2, a3);
if ( v7 == -1 )
fatal("kore_buf_appendv(): vsnprintf error", 0x2000LL, v8);
if ( v7 > 0x1FFF )
{
v7 = _vasprintf_chk(&ptr, 1LL, a2, &v17);
if ( v7 == -1 )
{
fatal("kore_buf_appendv(): error or truncation", 1LL, v11);
return func6((unsigned int)"kore_buf_appendv(): error or truncation", 1, v12, v13, v14, v15, a7);
}
v9 = (char *)ptr;
}
else
{
ptr = v19;
v9 = v19;
}
func4(a1, v9, (int)v7);
if ( ptr != v19 )
free(ptr);
return v20 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args)
{
 int l;
 va_list copy;
 char *b, sb[8192];
 __builtin_va_copy(copy,args);
 l = vsnprintf(sb, sizeof(sb), fmt, args);
 if (l == -1)
  fatal("kore_buf_appendv(): vsnprintf error");
 if ((size_t)l >= sizeof(sb)) {
  l = vasprintf(&b, fmt, copy);
  if (l == -1)
   fatal("kore_buf_appendv(): error or truncation");
 } else {
  b = sb;
 }
 kore_buf_append(buf, b, l);
 if (b != sb)
  free(b);
 __builtin_va_end(copy);
}

canonical_original_code:
void kore_buf_appendv(struct kore_buf *buf, char *fmt, __builtin_va_list args)
{
 int l;
 __builtin_va_list copy;
 char *b; char sb[8192];
 __builtin_va_copy(copy,args);
 l = vsnprintf(sb, sizeof(sb), fmt, args);
 if (l == -1)
  fatal("kore_buf_appendv(): vsnprintf error");
 if ((unsigned long)l >= sizeof(sb)) {
  l = vasprintf(&b, fmt, copy);
  if (l == -1)
   fatal("kore_buf_appendv(): error or truncation");
 } else {
  b = sb;
 }
 kore_buf_append(buf, b, l);
 if (b != sb)
  free(b);
 __builtin_va_end(copy);
}

variable_types:
fmt
char *

variable_types: l int

variable_types:
b
char *

variable_types:
sb
char [8192]


original_code:
void
kore_buf_appendf(struct kore_buf *buf, const char *fmt, ...)
{
 va_list args;
 __builtin_va_start(args,fmt);
 kore_buf_appendv(buf, fmt, args);
 __builtin_va_end(args);
}

canonical_original_code:
void kore_buf_appendf(struct kore_buf *buf, char *fmt, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 kore_buf_appendv(buf, fmt, args);
 __builtin_va_end(args);
}


decompiled_code:
__int64 __fastcall kore_buf_stringify(__int64 *a1, _QWORD *a2)
{
char v3; // [rsp+7h] [rbp-11h] BYREF
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
if ( a2 )
*a2 = a1[3];
v3 = 0;
kore_buf_append(a1, &v3, 1uLL);
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1, _QWORD *a2)
{
char v3; // [rsp+7h] [rbp-11h] BYREF
unsigned __int64 v4; // [rsp+8h] [rbp-10h]

v4 = __readfsqword(0x28u);
if ( a2 )
*a2 = a1[3];
v3 = 0;
func4(a1, &v3, 1uLL);
return *a1;
}

original_code:
char *
kore_buf_stringify(struct kore_buf *buf, size_t *len)
{
 char c;
 if (len != ((void *)0))
  *len = buf->offset;
 c = '\0';
 kore_buf_append(buf, &c, sizeof(c));
 return ((char *)buf->data);
}

canonical_original_code:
char *kore_buf_stringify(struct kore_buf *buf, unsigned long *len)
{
 char c;
 if (len != ((void *)0))
  *len = buf->offset;
 c = '\0';
 kore_buf_append(buf, &c, sizeof(c));
 return ((char *)buf->data);
}

variable_types:
len
unsigned long *

variable_types: c char

return_type: char *


decompiled_code:
__int64 __fastcall kore_buf_release(__int64 *a1, _QWORD *a2)
{
__int64 v2; // r12

v2 = *a1;
*a2 = a1[3];
*a1 = 0LL;
kore_buf_free((__int64)a1);
return v2;
}

original_code:
u_int8_t *
kore_buf_release(struct kore_buf *buf, size_t *len)
{
 u_int8_t *p;
 p = buf->data;
 *len = buf->offset;
 buf->data = ((void *)0);
 kore_buf_free(buf);
 return (p);
}

canonical_original_code:
unsigned int *kore_buf_release(struct kore_buf *buf, unsigned long *len)
{
 unsigned int *p;
 p = buf->data;
 *len = buf->offset;
 buf->data = ((void *)0);
 kore_buf_free(buf);
 return (p);
}

variable_types:
p
unsigned int *

return_type: unsigned int *


decompiled_code:
unsigned __int64 __fastcall kore_buf_replace_string(_QWORD *a1, const char *a2, const void *a3, size_t a4)
{
size_t v4; // rbx
size_t v5; // rax
size_t v6; // rsi
char *v7; // r15
size_t v8; // rbp
size_t v9; // rbx
char *v10; // r14
const void *v11; // r9
size_t v12; // r13
__int64 v13; // rax
void *v16; // [rsp+10h] [rbp-68h]
size_t v18; // [rsp+20h] [rbp-58h]
unsigned __int64 v20; // [rsp+38h] [rbp-40h]

v4 = 0LL;
v20 = __readfsqword(0x28u);
v5 = strlen(a2);
v6 = a1[3];
v7 = (char *)*a1;
v18 = v5;
while ( 1 )
{
v12 = a4 + v6;
v13 = kore_mem_find(&v7[v4], v6 - v4, a2, v18);
if ( !v13 )
break;
v8 = *a1 + a1[3] - (v13 + v18);
v16 = (void *)(v13 + v18);
v9 = v13 - (_QWORD)v7;
v10 = (char *)kore_malloc(v12);
memcpy(v10, v7, v9);
v11 = v16;
if ( a3 )
{
memcpy(&v10[v9], a3, a4);
v11 = v16;
}
v4 = a4 + v9;
v7 = v10;
memcpy(&v10[v4], v11, v8);
kore_free(*a1);
v6 = v4 + v8;
*a1 = v10;
a1[3] = v4 + v8;
a1[2] = v12;
}
return v20 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_replace_string(struct kore_buf *b, const char *src,
    const void *dst, size_t len)
{
 char *key, *end, *tmp, *p;
 size_t blen, off, off2, nlen, klen;
 off = 0;
 klen = strlen(src);
 for (;;) {
  blen = b->offset;
  nlen = blen + len;
  p = (char *)b->data;
  key = kore_mem_find(p + off, b->offset - off, src, klen);
  if (key == ((void *)0))
   break;
  end = key + klen;
  off = key - p;
  off2 = ((char *)(b->data + b->offset) - end);
  tmp = kore_malloc(nlen);
  memcpy(tmp, p, off);
  if (dst != ((void *)0))
   memcpy((tmp + off), dst, len);
  memcpy((tmp + off + len), end, off2);
  kore_free(b->data);
  b->data = (u_int8_t *)tmp;
  b->offset = off + len + off2;
  b->length = nlen;
  off = off + len;
 }
}

canonical_original_code:
void kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, unsigned long len)
{
 char *key; char *end; char *tmp; char *p;
 unsigned long blen; unsigned long off; unsigned long off2; unsigned long nlen; unsigned long klen;
 off = 0;
 klen = strlen(src);
 for (;;) {
  blen = b->offset;
  nlen = blen + len;
  p = (char *)b->data;
  key = kore_mem_find(p + off, b->offset - off, src, klen);
  if (key == ((void *)0))
   break;
  end = key + klen;
  off = key - p;
  off2 = ((char *)(b->data + b->offset) - end);
  tmp = kore_malloc(nlen);
  memcpy(tmp, p, off);
  if (dst != ((void *)0))
   memcpy((tmp + off), dst, len);
  memcpy((tmp + off + len), end, off2);
  kore_free(b->data);
  b->data = (unsigned int *)tmp;
  b->offset = off + len + off2;
  b->length = nlen;
  off = off + len;
 }
}

variable_types:
src
char *

variable_types:
dst
void *

variable_types:
key
char *

variable_types:
end
char *

variable_types:
tmp
char *

variable_types:
p
char *

variable_types:
blen
unsigned long

variable_types:
off
unsigned long

variable_types:
nlen
unsigned long

variable_types:
klen
unsigned long


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

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 24) = 0LL;
return v2 - __readfsqword(0x28u);
}

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

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 24) = 0LL;
return v2 - __readfsqword(0x28u);
}

original_code:
void
kore_buf_reset(struct kore_buf *buf)
{
 buf->offset = 0;
}

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


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

for ( result = msg_types; result; result = *(_QWORD *)(result + 16) )
{
if ( *(_BYTE *)result == a1 )
break;
}
return result;
}

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

for ( result = msg_types; result; result = *(_QWORD *)(result + 16) )
{
if ( *(_BYTE *)result == a1 )
break;
}
return result;
}

original_code:
static struct msg_type *
msg_type_lookup(u_int8_t id)
{
 struct msg_type *type;
 for ((type) = ((&msg_types)->tqh_first); (type); (type) = ((type)->list.tqe_next)) {
  if (type->id == id)
   return (type);
 }
 return (((void *)0));
}

canonical_original_code:
static struct msg_type *msg_type_lookup(unsigned int id)
{
 struct msg_type *type;
 for ((type) = ((&msg_types)->tqh_first); (type); (type) = ((type)->list.tqe_next)) {
  if (type->id == id)
   return (type);
 }
 return (((void *)0));
}

variable_types:
id
unsigned int

variable_types: struct msg_type { unsigned int id; void (*cb)(struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *, void *); struct { struct msg_type *tqe_next; struct msg_type **tqe_prev; } list; } *

user_defined_types: struct msg_type { unsigned int id; void (*cb)(struct kore_msg *, void *); struct { struct msg_type *tqe_next; struct msg_type **tqe_prev; } list; }


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

v2 = __readfsqword(0x28u);
if ( !kore_quiet )
kore_log(5LL, "shutdown requested by worker %u, going down", *(unsigned __int16 *)(a1 + 2));
kore_quit = *(_BYTE *)a1 == 13;
return v2 - __readfsqword(0x28u);
}

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

v2 = __readfsqword(0x28u);
if ( !kore_quiet )
kore_log(5LL, "shutdown requested by worker %u, going down", *(unsigned __int16 *)(a1 + 2));
kore_quit = *(_BYTE *)a1 == 13;
return v2 - __readfsqword(0x28u);
}

original_code:
static void
msg_type_shutdown(struct kore_msg *msg, const void *data)
{
 if (!kore_quiet) {
  kore_log(5,
      "shutdown requested by worker %u, going down", msg->src);
 }
 if (msg->id == 13)
  kore_quit = 1;
 else
  kore_quit = 0;
}

canonical_original_code:
static void msg_type_shutdown(struct kore_msg *msg, void *data)
{
 if (!kore_quiet) {
  kore_log(5,
      "shutdown requested by worker %u, going down", msg->src);
 }
 if (msg->id == 13)
  kore_quit = 1;
 else
  kore_quit = 0;
}

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

variable_types:
data
void *


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

v1 = __readfsqword(0x28u);
msg_types = 0LL;
qword_898 = (__int64)&msg_types;
return v1 - __readfsqword(0x28u);
}

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

v1 = __readfsqword(0x28u);
msg_types = 0LL;
qword_898 = (__int64)&msg_types;
return v1 - __readfsqword(0x28u);
}

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

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


decompiled_code:
__int64 __fastcall kore_msg_unregister(char a1)
{
__int64 v1; // rax
__int64 v2; // rdi
__int64 v3; // rax
_QWORD *v4; // rdx
unsigned __int64 v6; // [rsp+8h] [rbp-10h]

v6 = __readfsqword(0x28u);
v1 = msg_type_lookup(a1);
if ( !v1 )
return v6 - __readfsqword(0x28u);
v2 = v1;
v3 = *(_QWORD *)(v1 + 16);
v4 = *(_QWORD **)(v2 + 24);
if ( v3 )
*(_QWORD *)(v3 + 24) = v4;
else
qword_898 = *(_QWORD *)(v2 + 24);
*v4 = v3;
return kore_free();
}

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

v6 = __readfsqword(0x28u);
v1 = func1(a1);
if ( !v1 )
return v6 - __readfsqword(0x28u);
v2 = v1;
v3 = *(_QWORD *)(v1 + 16);
v4 = *(_QWORD **)(v2 + 24);
if ( v3 )
*(_QWORD *)(v3 + 24) = v4;
else
qword_898 = *(_QWORD *)(v2 + 24);
*v4 = v3;
return kore_free();
}

original_code:
void
kore_msg_unregister(u_int8_t id)
{
 struct msg_type *type;
 if ((type = msg_type_lookup(id)) == ((void *)0))
  return;
 do { if (((type)->list.tqe_next) != ((void *)0)) (type)->list.tqe_next->list.tqe_prev = (type)->list.tqe_prev; else (&msg_types)->tqh_last = (type)->list.tqe_prev; *(type)->list.tqe_prev = (type)->list.tqe_next; } while ( 0);
 kore_free(type);
}

canonical_original_code:
void kore_msg_unregister(unsigned int id)
{
 struct msg_type *type;
 if ((type = msg_type_lookup(id)) == ((void *)0))
  return;
 do { if (((type)->list.tqe_next) != ((void *)0)) (type)->list.tqe_next->list.tqe_prev = (type)->list.tqe_prev; else (&msg_types)->tqh_last = (type)->list.tqe_prev; *(type)->list.tqe_prev = (type)->list.tqe_next; } while ( 0);
 kore_free(type);
}

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


decompiled_code:
__int64 __fastcall kore_msg_register(char a1, __int64 a2)
{
__int64 v2; // r8
__int64 result; // rax
__int64 v4; // rax
__int64 *v5; // rdx

v2 = msg_type_lookup(a1);
result = 0LL;
if ( !v2 )
{
v4 = kore_malloc(32LL);
*(_BYTE *)v4 = a1;
v5 = (__int64 *)qword_898;
*(_QWORD *)(v4 + 8) = a2;
*(_QWORD *)(v4 + 16) = 0LL;
*(_QWORD *)(v4 + 24) = v5;
*v5 = v4;
qword_898 = v4 + 16;
result = 1LL;
}
return result;
}

original_code:
int
kore_msg_register(u_int8_t id, void (*cb)(struct kore_msg *, const void *))
{
 struct msg_type *type;
 if (msg_type_lookup(id) != ((void *)0))
  return (0);
 type = kore_malloc(sizeof(*type));
 type->id = id;
 type->cb = cb;
 do { (type)->list.tqe_next = ((void *)0); (type)->list.tqe_prev = (&msg_types)->tqh_last; *(&msg_types)->tqh_last = (type); (&msg_types)->tqh_last = &(type)->list.tqe_next; } while ( 0);
 return (1);
}

canonical_original_code:
int kore_msg_register(unsigned int id, void (*cb)(struct kore_msg *, void *))
{
 struct msg_type *type;
 if (msg_type_lookup(id) != ((void *)0))
  return (0);
 type = kore_malloc(sizeof(*type));
 type->id = id;
 type->cb = cb;
 do { (type)->list.tqe_next = ((void *)0); (type)->list.tqe_prev = (&msg_types)->tqh_last; *(&msg_types)->tqh_last = (type); (&msg_types)->tqh_last = &(type)->list.tqe_next; } while ( 0);
 return (1);
}

variable_types: void (*)(struct kore_msg { unsigned int id; unsigned int src; unsigned int dst; unsigned long length; } *, void *)

return_type: int


decompiled_code:
__int64 kore_msg_worker_init()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx
__int64 v3; // rax
_QWORD *v4; // rcx
__int64 v5; // rsi
__int64 v6; // rdi

kore_msg_register(1, (__int64)msg_type_websocket);
v0 = worker;
v1 = kore_connection_new(0LL);
v2 = worker;
*(_QWORD *)(v0 + 40) = v1;
v3 = *(_QWORD *)(v2 + 40);
*(_DWORD *)(v3 + 16) = *(_DWORD *)(v2 + 24);
*(_WORD *)(v3 + 20) = 770;
v4 = (_QWORD *)connections[1];
*(_DWORD *)(v3 + 4) = 2;
*(_QWORD *)(v3 + 88) = &net_read;
*(_QWORD *)(v3 + 96) = &net_write;
*(_QWORD *)(v3 + 72) = &kore_connection_handle;
*(_QWORD *)(v3 + 328) = 0LL;
*(_QWORD *)(v3 + 336) = v4;
*v4 = v3;
v5 = *(_QWORD *)(v2 + 40);
v6 = *(unsigned int *)(v5 + 16);
connections[1] = v5 + 328;
kore_platform_event_all(v6);
return net_recv_queue(*(_QWORD *)(worker + 40LL), 16LL, 0LL, msg_recv_packet);
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx
__int64 v3; // rax
_QWORD *v4; // rcx
__int64 v5; // rsi
__int64 v6; // rdi

func10(1, (__int64)msg_type_websocket);
v0 = worker;
v1 = kore_connection_new(0LL);
v2 = worker;
*(_QWORD *)(v0 + 40) = v1;
v3 = *(_QWORD *)(v2 + 40);
*(_DWORD *)(v3 + 16) = *(_DWORD *)(v2 + 24);
*(_WORD *)(v3 + 20) = 770;
v4 = (_QWORD *)connections[1];
*(_DWORD *)(v3 + 4) = 2;
*(_QWORD *)(v3 + 88) = &net_read;
*(_QWORD *)(v3 + 96) = &net_write;
*(_QWORD *)(v3 + 72) = &kore_connection_handle;
*(_QWORD *)(v3 + 328) = 0LL;
*(_QWORD *)(v3 + 336) = v4;
*v4 = v3;
v5 = *(_QWORD *)(v2 + 40);
v6 = *(unsigned int *)(v5 + 16);
connections[1] = v5 + 328;
kore_platform_event_all(v6);
return net_recv_queue(*(_QWORD *)(worker + 40LL), 16LL, 0LL, msg_recv_packet);
}

original_code:
void
kore_msg_worker_init(void)
{
 kore_msg_register(1, msg_type_websocket);
 worker->msg[1] = kore_connection_new(((void *)0));
 worker->msg[1]->fd = worker->pipe[1];
 worker->msg[1]->read = net_read;
 worker->msg[1]->write = net_write;
 worker->msg[1]->proto = 3;
 worker->msg[1]->state = 2;
 worker->msg[1]->handle = kore_connection_handle;
 worker->msg[1]->evt.flags = 0x02;
 do { (worker->msg[1])->list.tqe_next = ((void *)0); (worker->msg[1])->list.tqe_prev = (&connections)->tqh_last; *(&connections)->tqh_last = (worker->msg[1]); (&connections)->tqh_last = &(worker->msg[1])->list.tqe_next; } while ( 0);
 kore_platform_event_all(worker->msg[1]->fd, worker->msg[1]);
 net_recv_queue(worker->msg[1],
     sizeof(struct kore_msg), 0, msg_recv_packet);
}

canonical_original_code:
void kore_msg_worker_init()
{
 kore_msg_register(1, msg_type_websocket);
 worker->msg[1] = kore_connection_new(((void *)0));
 worker->msg[1]->fd = worker->pipe[1];
 worker->msg[1]->read = net_read;
 worker->msg[1]->write = net_write;
 worker->msg[1]->proto = 3;
 worker->msg[1]->state = 2;
 worker->msg[1]->handle = kore_connection_handle;
 worker->msg[1]->evt.flags = 0x02;
 do { (worker->msg[1])->list.tqe_next = ((void *)0); (worker->msg[1])->list.tqe_prev = (&connections)->tqh_last; *(&connections)->tqh_last = (worker->msg[1]); (&connections)->tqh_last = &(worker->msg[1])->list.tqe_next; } while ( 0);
 kore_platform_event_all(worker->msg[1]->fd, worker->msg[1]);
 net_recv_queue(worker->msg[1],
     sizeof(struct kore_msg), 0, msg_recv_packet);
}


decompiled_code:
unsigned __int64 __fastcall pool_mark_entry_none(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 0) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_none", v4);
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 0) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_none", v4);
}
return v5 - __readfsqword(0x28u);
}

original_code:
static void
pool_mark_entry_none(struct kore_pool *pool, void *ptr)
{
 if (mprotect(ptr, pool->memsz - pool->pagesz, 0x0) == -1)
  fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
}

variable_types: struct kore_pool { unsigned long memsz; unsigned long growth; unsigned long pagesz; unsigned long elmlen; unsigned long uselen; unsigned int canary; int lock; char *name; struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; } *freelist; } *

variable_types:
ptr
void *

user_defined_types: struct kore_pool { unsigned long memsz; unsigned long growth; unsigned long pagesz; unsigned long elmlen; unsigned long uselen; unsigned int canary; int lock; char *name; struct kore_pool_entry *freelist; }


decompiled_code:
unsigned __int64 __fastcall pool_mark_entry_rw(_QWORD *a1, void *a2)
{
int *v3; // rax
char *v4; // rax
unsigned __int64 v5; // [rsp+8h] [rbp-10h]

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 3) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_rw", v4);
}
return v5 - __readfsqword(0x28u);
}

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

v5 = __readfsqword(0x28u);
if ( mprotect(a2, *a1 - a1[2], 3) == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fatal("%s: mprotect: %s", "pool_mark_entry_rw", v4);
}
return v5 - __readfsqword(0x28u);
}

original_code:
static void
pool_mark_entry_rw(struct kore_pool *pool, void *ptr)
{
 if (mprotect(ptr, pool->memsz - pool->pagesz,
     0x1 | 0x2) == -1)
  fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
}

user_defined_types: struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; }


decompiled_code:
unsigned __int64 __fastcall pool_grow(_QWORD *a1, __int64 a2)
{
__int64 v2; // r15
_QWORD *v4; // r12
int v5; // eax
__int64 v6; // rbp
__int64 v7; // rdi
__int64 v8; // rdx
__int64 v9; // rcx
unsigned __int64 v10; // r13
size_t v11; // rsi
char *v13; // rbx
__int64 v14; // rdx
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
unsigned __int64 v20; // [rsp+18h] [rbp-40h]

v2 = 0LL;
v4 = (_QWORD *)a1[8];
v20 = __readfsqword(0x28u);
if ( !kore_mem_guard )
v2 = kore_mmap_region(a2 * (a1[3] + 32LL));
if ( a2 )
{
v5 = kore_mem_guard;
v6 = 0LL;
LABEL_5:
if ( !v5 )
{
v7 = a1[3];
v8 = v2 + v6 * (v7 + 32);
*(_BYTE *)v8 = 1;
v9 = a1[4];
v10 = v8 + 32;
*(_QWORD *)(v8 + 8) = v8 + 32;
*(_QWORD *)(v8 + 24) = 0LL;
*(_QWORD *)(v8 + 16) = v8 + 32 + v9;
if ( v4 )
{
v4[3] = v8;
v4 = (_QWORD *)(v2 + v6 * (v7 + 32));
goto LABEL_8;
}
v4 = (_QWORD *)(v2 + v6 * (v7 + 32));
goto LABEL_9;
}
v2 = kore_mmap_region(*a1);
v5 = kore_mem_guard;
v13 = (char *)(*a1 + v2 - a1[3] - a1[2]);
*(_BYTE *)v2 = 1;
v14 = a1[4];
*(_QWORD *)(v2 + 8) = v13;
v10 = (unsigned __int64)v13;
*(_QWORD *)(v2 + 24) = 0LL;
*(_QWORD *)(v2 + 16) = &v13[v14];
if ( !v4 )
goto LABEL_21;
v4[3] = v2;
if ( v5 )
{
pool_mark_entry_none(a1, v4);
v5 = kore_mem_guard;
LABEL_21:
v4 = (_QWORD *)v2;
if ( a1[8] )
goto LABEL_10;
LABEL_22:
a1[8] = v4;
goto LABEL_10;
}
while ( 1 )
{
v4 = (_QWORD *)v2;
LABEL_8:
v5 = 0;
LABEL_9:
if ( !a1[8] )
goto LABEL_22;
LABEL_10:
if ( !v5 )
goto LABEL_15;
v11 = a1[2];
v10 += a1[3];
if ( !(v10 % v11) )
{
if ( mprotect((void *)v10, v11, 0) != -1 )
{
if ( madvise((void *)v10, a1[2], 8) != -1 )
{
v5 = kore_mem_guard;
LABEL_15:
if ( a2 == ++v6 )
{
LABEL_16:
if ( !v5 )
return v20 - __readfsqword(0x28u);
return pool_mark_entry_none(a1, v4);
}
goto LABEL_5;
}
v15 = _errno_location();
v16 = strerror(*v15);
fatal("%s: madvise: %s", "pool_grow", v16);
}
v17 = _errno_location();
v18 = strerror(*v17);
fatal("%s: mprotect: %s", "pool_grow", v18);
}
fatal("%s: misaligned page", "pool_grow");
}
}
if ( v4 )
{
v5 = kore_mem_guard;
goto LABEL_16;
}
return v20 - __readfsqword(0x28u);
}

original_code:
static void
pool_grow(struct kore_pool *pool, size_t elms)
{
 size_t i;
 u_int8_t *base, *p;
 struct kore_pool_entry *entry, *prev;
 prev = pool->freelist;
 if (kore_mem_guard == 0)
  base = kore_mmap_region(elms * (sizeof(*entry) + pool->elmlen));
 else
  base = ((void *)0);
 for (i = 0; i < elms; i++) {
  if (kore_mem_guard) {
   base = kore_mmap_region(pool->memsz);
   p = base + (pool->memsz - pool->pagesz - pool->elmlen);
   entry = (struct kore_pool_entry *)base;
  } else {
   p = base + ((sizeof(*entry) + pool->elmlen) * i);
   entry = (struct kore_pool_entry *)p;
   p += sizeof(*entry);
  }
  entry->uptr = p;
  entry->nextfree = ((void *)0);
  entry->state = 1;
  entry->canary = p + pool->uselen;
  if (prev != ((void *)0)) {
   prev->nextfree = entry;
   if (kore_mem_guard)
    pool_mark_entry_none(pool, prev);
  }
  prev = entry;
  if (pool->freelist == ((void *)0))
   pool->freelist = entry;
  if (kore_mem_guard) {
   p += pool->elmlen;
   if (((uintptr_t)p % pool->pagesz) != 0)
    fatal("%s: misaligned page", __func__);
   if (mprotect(p, pool->pagesz, 0x0) == -1)
    fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
   if (madvise(p, pool->pagesz, 8) == -1)
    fatal("%s: madvise: %s", __func__, strerror((*__errno_location ())));
  }
 }
 if (prev != ((void *)0) && kore_mem_guard)
  pool_mark_entry_none(pool, prev);
}

canonical_original_code:
static void pool_grow(struct kore_pool *pool, unsigned long elms)
{
 unsigned long i;
 unsigned int *base; unsigned int *p;
 struct kore_pool_entry *entry; struct kore_pool_entry *prev;
 prev = pool->freelist;
 if (kore_mem_guard == 0)
  base = kore_mmap_region(elms * (sizeof(*entry) + pool->elmlen));
 else
  base = ((void *)0);
 for (i = 0; i < elms; i++) {
  if (kore_mem_guard) {
   base = kore_mmap_region(pool->memsz);
   p = base + (pool->memsz - pool->pagesz - pool->elmlen);
   entry = (struct kore_pool_entry *)base;
  } else {
   p = base + ((sizeof(*entry) + pool->elmlen) * i);
   entry = (struct kore_pool_entry *)p;
   p += sizeof(*entry);
  }
  entry->uptr = p;
  entry->nextfree = ((void *)0);
  entry->state = 1;
  entry->canary = p + pool->uselen;
  if (prev != ((void *)0)) {
   prev->nextfree = entry;
   if (kore_mem_guard)
    pool_mark_entry_none(pool, prev);
  }
  prev = entry;
  if (pool->freelist == ((void *)0))
   pool->freelist = entry;
  if (kore_mem_guard) {
   p += pool->elmlen;
   if (((unsigned long)p % pool->pagesz) != 0)
    fatal("%s: misaligned page", __func__);
   if (mprotect(p, pool->pagesz, 0x0) == -1)
    fatal("%s: mprotect: %s", __func__, strerror((*__errno_location ())));
   if (madvise(p, pool->pagesz, 8) == -1)
    fatal("%s: madvise: %s", __func__, strerror((*__errno_location ())));
  }
 }
 if (prev != ((void *)0) && kore_mem_guard)
  pool_mark_entry_none(pool, prev);
}

variable_types:
elms
unsigned long

variable_types:
i
unsigned long

variable_types:
base
unsigned int *

variable_types:
p
unsigned int *

variable_types: struct kore_pool_entry { unsigned int state; void *uptr; void *canary; struct kore_pool_entry *nextfree; } *


original_code:
void
kore_pool_init(struct kore_pool *pool, const char *name,
    size_t len, size_t elm)
{
 long pagesz;
 if (elm < 16)
  elm = 16;
 if ((pagesz = sysconf(_SC_PAGESIZE)) == -1)
  fatal("%s: sysconf: %s", __func__, strerror((*__errno_location ())));
 if ((pool->name = strdup(name)) == ((void *)0))
  fatal("kore_pool_init: strdup %s", strerror((*__errno_location ())));
 pool->uselen = len;
 len = len + sizeof(u_int64_t);
 len = (len + (16 - 1)) & ~(16 - 1);
 pool->elmlen = len;
 pool->lock = 0;
 pool->freelist = ((void *)0);
 pool->pagesz = pagesz;
 pool->growth = elm * 0.25f;
 pool->canary = (u_int64_t)kore_platform_random_uint32() << 32 |
     kore_platform_random_uint32();
 if (kore_mem_guard) {
  pool->memsz = pool->pagesz * 2;
  while (pool->elmlen >
      pool->pagesz - sizeof(struct kore_pool_entry)) {
   pool->memsz += pool->pagesz;
   pool->elmlen -= (((pool->elmlen)<(pool->pagesz))?(pool->elmlen):(pool->pagesz));
  }
  pool->elmlen = len;
 } else {
  pool->memsz = pool->elmlen;
 }
 pool_grow(pool, elm);
}

canonical_original_code:
void kore_pool_init(struct kore_pool *pool, char *name, unsigned long len, unsigned long elm)
{
 long pagesz;
 if (elm < 16)
  elm = 16;
 if ((pagesz = sysconf(_SC_PAGESIZE)) == -1)
  fatal("%s: sysconf: %s", __func__, strerror((*__errno_location ())));
 if ((pool->name = strdup(name)) == ((void *)0))
  fatal("kore_pool_init: strdup %s", strerror((*__errno_location ())));
 pool->uselen = len;
 len = len + sizeof(u_int64_t);
 len = (len + (16 - 1)) & ~(16 - 1);
 pool->elmlen = len;
 pool->lock = 0;
 pool->freelist = ((void *)0);
 pool->pagesz = pagesz;
 pool->growth = elm * 0.25f;
 pool->canary = (unsigned int)kore_platform_random_uint32() << 32 |
     kore_platform_random_uint32();
 if (kore_mem_guard) {
  pool->memsz = pool->pagesz * 2;
  while (pool->elmlen >
      pool->pagesz - sizeof(struct kore_pool_entry)) {
   pool->memsz += pool->pagesz;
   pool->elmlen -= (((pool->elmlen)<(pool->pagesz))?(pool->elmlen):(pool->pagesz));
  }
  pool->elmlen = len;
 } else {
  pool->memsz = pool->elmlen;
 }
 pool_grow(pool, elm);
}

variable_types:
name
char *

variable_types:
len
unsigned long

variable_types:
elm
unsigned long

variable_types: pagesz long


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

if ( kore_mem_guard )
{
v2 = *(_QWORD **)(a1 + 64);
while ( v2 )
{
pool_mark_entry_rw((_QWORD *)a1, v2);
v3 = v2;
v2 = (_QWORD *)v2[3];
munmap(v3, *(_QWORD *)a1);
}
}
free(*(void **)(a1 + 56));
}

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

if ( kore_mem_guard )
{
v2 = *(_QWORD **)(a1 + 64);
while ( v2 )
{
func1((_QWORD *)a1, v2);
v3 = v2;
v2 = (_QWORD *)v2[3];
munmap(v3, *(_QWORD *)a1);
}
}
free(*(void **)(a1 + 56));
}

original_code:
void
kore_pool_cleanup(struct kore_pool *pool)
{
 struct kore_pool_entry *entry, *next;
 if (kore_mem_guard) {
  for (entry = pool->freelist; entry != ((void *)0); entry = next) {
   pool_mark_entry_rw(pool, entry);
   next = entry->nextfree;
   (void)munmap(entry, pool->memsz);
  }
 }
 free(pool->name);
}

canonical_original_code:
void kore_pool_cleanup(struct kore_pool *pool)
{
 struct kore_pool_entry *entry; struct kore_pool_entry *next;
 if (kore_mem_guard) {
  for (entry = pool->freelist; entry != ((void *)0); entry = next) {
   pool_mark_entry_rw(pool, entry);
   next = entry->nextfree;
   (void)munmap(entry, pool->memsz);
  }
 }
 free(pool->name);
}


original_code:
void *
kore_pool_get(struct kore_pool *pool)
{
 u_int64_t canary;
 struct kore_pool_entry *entry;
 if (pool->freelist == ((void *)0))
  pool_grow(pool, pool->growth);
 entry = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_rw(pool, entry);
 pool->freelist = entry->nextfree;
 if (entry->state != 1)
  fatal("%s: element %p was not free", pool->name, (void *)entry);
 entry->nextfree = ((void *)0);
 entry->state = 0;
 canary = pool->canary;
 canary ^= (uintptr_t)entry;
 canary ^= (uintptr_t)entry->uptr;
 memcpy(entry->canary, &canary, sizeof(canary));
 return (entry->uptr);
}

canonical_original_code:
void *kore_pool_get(struct kore_pool *pool)
{
 unsigned int canary;
 struct kore_pool_entry *entry;
 if (pool->freelist == ((void *)0))
  pool_grow(pool, pool->growth);
 entry = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_rw(pool, entry);
 pool->freelist = entry->nextfree;
 if (entry->state != 1)
  fatal("%s: element %p was not free", pool->name, (void *)entry);
 entry->nextfree = ((void *)0);
 entry->state = 0;
 canary = pool->canary;
 canary ^= (unsigned long)entry;
 canary ^= (unsigned long)entry->uptr;
 memcpy(entry->canary, &canary, sizeof(canary));
 return (entry->uptr);
}

variable_types:
canary
unsigned int

return_type: void *


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

v2 = (unsigned __int64)(a2 - 32);
v3 = kore_mem_guard;
v6 = __readfsqword(0x28u);
if ( kore_mem_guard )
v2 = (unsigned __int64)&a2[-((unsigned __int64)a2 % *(_QWORD *)(a1 + 16))];
v4 = *(const char **)(v2 + 8);
if ( v4 != a2 )
LABEL_11:
fatal("%s: uptr mismatch %p != %p", *(const char **)(a1 + 56), v4, a2);
if ( (**(_QWORD **)(v2 + 16) ^ (unsigned __int64)a2 ^ v2) != *(_QWORD *)(a1 + 40) )
{
LABEL_10:
a2 = *(const char **)(a1 + 56);
fatal("%s: memory corruption detected", a2);
goto LABEL_11;
}
if ( *(_BYTE *)v2 )
{
fatal("%s: element %p was not busy", *(const char **)(a1 + 56), a2);
goto LABEL_10;
}
*(_BYTE *)v2 = 1;
*(_QWORD *)(v2 + 24) = *(_QWORD *)(a1 + 64);
if ( v3 )
pool_mark_entry_none((_QWORD *)a1, (void *)v2);
*(_QWORD *)(a1 + 64) = v2;
return v6 - __readfsqword(0x28u);
}

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

v2 = (unsigned __int64)(a2 - 32);
v3 = kore_mem_guard;
v6 = __readfsqword(0x28u);
if ( kore_mem_guard )
v2 = (unsigned __int64)&a2[-((unsigned __int64)a2 % *(_QWORD *)(a1 + 16))];
v4 = *(const char **)(v2 + 8);
if ( v4 != a2 )
LABEL_11:
fatal("%s: uptr mismatch %p != %p", *(const char **)(a1 + 56), v4, a2);
if ( (**(_QWORD **)(v2 + 16) ^ (unsigned __int64)a2 ^ v2) != *(_QWORD *)(a1 + 40) )
{
LABEL_10:
a2 = *(const char **)(a1 + 56);
fatal("%s: memory corruption detected", a2);
goto LABEL_11;
}
if ( *(_BYTE *)v2 )
{
fatal("%s: element %p was not busy", *(const char **)(a1 + 56), a2);
goto LABEL_10;
}
*(_BYTE *)v2 = 1;
*(_QWORD *)(v2 + 24) = *(_QWORD *)(a1 + 64);
if ( v3 )
func0((_QWORD *)a1, (void *)v2);
*(_QWORD *)(a1 + 64) = v2;
return v6 - __readfsqword(0x28u);
}

original_code:
void
kore_pool_put(struct kore_pool *pool, void *ptr)
{
 void *base;
 u_int64_t canary;
 struct kore_pool_entry *entry;
 if (kore_mem_guard) {
  base = (u_int8_t *)ptr - ((uintptr_t)ptr % pool->pagesz);
 } else {
  base = (u_int8_t *)ptr - sizeof(*entry);
 }
 entry = (struct kore_pool_entry *)base;
 if (entry->uptr != ptr) {
  fatal("%s: uptr mismatch %p != %p",
      pool->name, entry->uptr, ptr);
 }
 memcpy(&canary, entry->canary, sizeof(canary));
 canary ^= (uintptr_t)entry;
 canary ^= (uintptr_t)ptr;
 if (canary != pool->canary)
  fatal("%s: memory corruption detected", pool->name);
 if (entry->state != 0)
  fatal("%s: element %p was not busy", pool->name, ptr);
 entry->state = 1;
 entry->nextfree = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_none(pool, entry);
 pool->freelist = entry;
}

canonical_original_code:
void kore_pool_put(struct kore_pool *pool, void *ptr)
{
 void *base;
 unsigned int canary;
 struct kore_pool_entry *entry;
 if (kore_mem_guard) {
  base = (unsigned int *)ptr - ((unsigned long)ptr % pool->pagesz);
 } else {
  base = (unsigned int *)ptr - sizeof(*entry);
 }
 entry = (struct kore_pool_entry *)base;
 if (entry->uptr != ptr) {
  fatal("%s: uptr mismatch %p != %p",
      pool->name, entry->uptr, ptr);
 }
 memcpy(&canary, entry->canary, sizeof(canary));
 canary ^= (unsigned long)entry;
 canary ^= (unsigned long)ptr;
 if (canary != pool->canary)
  fatal("%s: memory corruption detected", pool->name);
 if (entry->state != 0)
  fatal("%s: element %p was not busy", pool->name, ptr);
 entry->state = 1;
 entry->nextfree = pool->freelist;
 if (kore_mem_guard)
  pool_mark_entry_none(pool, entry);
 pool->freelist = entry;
}

variable_types:
base
void *


meminfo

decompiled_code:
__int64 __fastcall meminfo(__int64 a1)
{
return a1 - 16;
}

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

original_code:
static inline struct meminfo *
meminfo(void *ptr)
{
 return ((struct meminfo *)((u_int8_t *)ptr - sizeof(struct meminfo)));
}

canonical_original_code:
static inline struct meminfo *meminfo(void *ptr)
{
 return ((struct meminfo *)((unsigned int *)ptr - sizeof(struct meminfo)));
}

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


decompiled_code:
unsigned __int64 __fastcall mem_index(unsigned __int64 a1)
{
unsigned __int64 v1; // r8
unsigned __int64 i; // rax

v1 = 0LL;
if ( a1 > 8 )
{
for ( i = 8LL; i < a1; i *= 2LL )
++v1;
if ( v1 > 0xA )
fatal("mem_index: idx too high");
}
return v1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned __int64 a1)
{
unsigned __int64 v1; // r8
unsigned __int64 i; // rax

v1 = 0LL;
if ( a1 > 8 )
{
for ( i = 8LL; i < a1; i *= 2LL )
++v1;
if ( v1 > 0xA )
fatal("mem_index: idx too high");
}
return v1;
}

original_code:
static size_t
mem_index(size_t len)
{
 size_t mlen, idx;
 idx = 0;
 mlen = 8;
 while (mlen < len) {
  idx++;
  mlen = mlen << 1;
 }
 if (idx > (11 - 1))
  fatal("mem_index: idx too high");
 return (idx);
}

canonical_original_code:
static unsigned long mem_index(unsigned long len)
{
 unsigned long mlen; unsigned long idx;
 idx = 0;
 mlen = 8;
 while (mlen < len) {
  idx++;
  mlen = mlen << 1;
 }
 if (idx > (11 - 1))
  fatal("mem_index: idx too high");
 return (idx);
}

variable_types:
mlen
unsigned long

variable_types:
idx
unsigned long

return_type: unsigned long


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

v8 = __readfsqword(0x28u);
v0 = getenv("KORE_MEM_GUARD");
if ( v0 && *v0 == 49 && !v0[1] )
kore_mem_guard = 1;
v1 = (char *)&mempools;
v2 = 8LL;
tags = 0LL;
qword_BD8 = (__int64)&tags;
kore_pool_init(&tag_pool, "tag_pool", 32LL, 4LL);
do
{
_snprintf_chk(v7, 32LL, 1LL, 32LL, "block-%zu", v2);
v3 = v1;
v4 = 0x8000 / v2;
v5 = v2 + 16;
v1 += 72;
v2 *= 2LL;
kore_pool_init(v3, v7, v5, v4);
}
while ( v1 != (char *)&unk_B78 );
return v8 - __readfsqword(0x28u);
}

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

v8 = __readfsqword(0x28u);
v0 = getenv("KORE_MEM_GUARD");
if ( v0 && *v0 == 49 && !v0[1] )
kore_mem_guard = 1;
v1 = (char *)&mempools;
v2 = 8LL;
tags = 0LL;
qword_BD8 = (__int64)&tags;
kore_pool_init(&tag_pool, "tag_pool", 32LL, 4LL);
do
{
_snprintf_chk(v7, 32LL, 1LL, 32LL, "block-%zu", v2);
v3 = v1;
v4 = 0x8000 / v2;
v5 = v2 + 16;
v1 += 72;
v2 *= 2LL;
kore_pool_init(v3, v7, v5, v4);
}
while ( v1 != (char *)&unk_B78 );
return v8 - __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; }


decompiled_code:
unsigned __int64 kore_mem_cleanup()
{
char *v0; // rbx
char *v1; // rdi
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

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

canonical_decompiled_code:
unsigned __int64 func3()
{
char *v0; // rbx
char *v1; // rdi
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v0 = (char *)&mempools;
v3 = __readfsqword(0x28u);
do
{
v1 = v0;
v0 += 72;
kore_pool_cleanup(v1);
}
while ( v0 != (char *)&unk_B78 );
return v3 - __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]);
 }
}


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:
ptr
void *


decompiled_code:
void *__fastcall kore_calloc(unsigned __int64 a1, unsigned __int64 a2)
{
_QWORD *v2; // rax

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

canonical_decompiled_code:
void *__fastcall func5(unsigned __int64 a1, unsigned __int64 a2)
{
_QWORD *v2; // rax

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

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:
__int64 __fastcall kore_mem_untag(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rdx
_QWORD *v3; // rsi
_QWORD *v5; // rdx
unsigned __int64 v6; // [rsp+8h] [rbp-10h]

v6 = __readfsqword(0x28u);
v1 = (_QWORD *)tags;
do
{
if ( !v1 )
return v6 - __readfsqword(0x28u);
v2 = *v1;
v3 = v1;
v1 = (_QWORD *)v1[2];
}
while ( v2 != a1 );
v5 = (_QWORD *)v3[3];
if ( v1 )
{
v1[3] = v5;
v5 = (_QWORD *)v3[3];
}
else
{
qword_BD8 = v3[3];
}
*v5 = v1;
return kore_pool_put(&tag_pool);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rdx
_QWORD *v3; // rsi
_QWORD *v5; // rdx
unsigned __int64 v6; // [rsp+8h] [rbp-10h]

v6 = __readfsqword(0x28u);
v1 = (_QWORD *)tags;
do
{
if ( !v1 )
return v6 - __readfsqword(0x28u);
v2 = *v1;
v3 = v1;
v1 = (_QWORD *)v1[2];
}
while ( v2 != a1 );
v5 = (_QWORD *)v3[3];
if ( v1 )
{
v1[3] = v5;
v5 = (_QWORD *)v3[3];
}
else
{
qword_BD8 = v3[3];
}
*v5 = v1;
return kore_pool_put(&tag_pool);
}

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:
__int64 __fastcall kore_free(__int64 a1)
{
__int64 v2; // r12
unsigned __int64 v3; // rdi
void *v4; // rbp
unsigned __int64 v5; // rax
int *v7; // rax
char *v8; // rax
unsigned __int64 v9; // [rsp+8h] [rbp-20h]

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

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v2; // r12
unsigned __int64 v3; // rdi
void *v4; // rbp
unsigned __int64 v5; // rax
int *v7; // rax
char *v8; // rax
unsigned __int64 v9; // [rsp+8h] [rbp-20h]

v9 = __readfsqword(0x28u);
if ( a1 )
{
v2 = func0(a1);
if ( (*(_BYTE *)(v2 + 8) & 1) != 0 )
{
func6(a1);
*(_WORD *)(v2 + 8) &= 0xFFFEu;
}
v3 = *(_QWORD *)v2;
v4 = (void *)(a1 - 16);
if ( *(_QWORD *)v2 <= 0x2000uLL )
{
v5 = func1(v3);
return kore_pool_put((char *)&mempools + 72 * v5);
}
if ( munmap(v4, v3 + 16) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
fatal("%s: munmap: %s", "kore_free", v8);
}
}
return v9 - __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: 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 func8(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);
v4 = (__int64 *)qword_BD8;
*(_DWORD *)(v3 + 8) = a2;
*(_QWORD *)v3 = a1;
*(_QWORD *)(v3 + 16) = 0LL;
*(_QWORD *)(v3 + 24) = v4;
*v4 = v3;
qword_BD8 = v3 + 16;
return v6 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__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 ( func8(a2) )
fatal("kore_mem_tag: tag %u taken", a2);
v2 = func0(a1);
*(_WORD *)(v2 + 8) |= 1u;
v3 = kore_pool_get(&tag_pool);
v4 = (__int64 *)qword_BD8;
*(_DWORD *)(v3 + 8) = a2;
*(_QWORD *)v3 = a1;
*(_QWORD *)(v3 + 16) = 0LL;
*(_QWORD *)(v3 + 24) = v4;
*v4 = v3;
qword_BD8 = 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 ( v2 != a1 );
}
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(_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 ( v2 != a1 );
}
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:
len
unsigned long

variable_types:
p
char *


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

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

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

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

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_848 = (__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_848 = (__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:
__int64 __fastcall native_runtime_execute(__int64 (*a1)(void))
{
return a1();
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 (*a1)(void))
{
return a1();
}

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:
__int64 __fastcall native_runtime_configure(__int64 (__fastcall *a1)(_QWORD, __int64), unsigned int a2, __int64 a3)
{
return a1(a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 (__fastcall *a1)(_QWORD, __int64), unsigned int a2, __int64 a3)
{
return a1(a2, a3);
}

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), unsigned int a2)
{
return a1(a2);
}

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

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)


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

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

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


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:
__int64 __fastcall kore_timer_remove(__int64 a1)
{
_QWORD *v1; // rdx
__int64 v2; // rax

v1 = *(_QWORD **)(a1 + 48);
v2 = *(_QWORD *)(a1 + 40);
if ( v2 )
*(_QWORD *)(v2 + 48) = v1;
else
*(&kore_timers + 1) = (__int64)v1;
*v1 = v2;
return kore_free();
}

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 func2(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; // rbp
__int64 v2; // r12
__int64 v3; // rax
_QWORD *v4; // rdx
unsigned __int64 *v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
__int64 v8; // rdx
__int64 *v10; // rax
unsigned __int64 v11; // [rsp+8h] [rbp-20h]

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

canonical_decompiled_code:
unsigned __int64 __fastcall func3(unsigned __int64 a1)
{
__int64 v1; // rbp
__int64 v2; // r12
__int64 v3; // rax
_QWORD *v4; // rdx
unsigned __int64 *v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rdx
__int64 v8; // rdx
__int64 *v10; // rax
unsigned __int64 v11; // [rsp+8h] [rbp-20h]

v1 = kore_timers;
v11 = __readfsqword(0x28u);
if ( kore_timers )
{
v2 = 0LL;
while ( 1 )
{
if ( *(_QWORD *)v1 > a1 )
return v11 - __readfsqword(0x28u);
v3 = *(_QWORD *)(v1 + 40);
v4 = *(_QWORD **)(v1 + 48);
if ( v3 )
*(_QWORD *)(v3 + 48) = v4;
else
*(&kore_timers + 1) = (__int64)v4;
*v4 = v3;
(*(void (__fastcall **)(_QWORD, unsigned __int64))(v1 + 32))(*(_QWORD *)(v1 + 24), a1);
if ( (*(_BYTE *)(v1 + 16) & 1) != 0 )
break;
v5 = (unsigned __int64 *)kore_timers;
v6 = a1 + *(_QWORD *)(v1 + 8);
*(_QWORD *)v1 = v6;
if ( !v5 )
{
LABEL_17:
v10 = (__int64 *)*(&kore_timers + 1);
*(_QWORD *)(v1 + 40) = 0LL;
v2 = v1;
*(_QWORD *)(v1 + 48) = v10;
*v10 = v1;
*(&kore_timers + 1) = v1 + 40;
LABEL_4:
v1 = kore_timers;
if ( v2 == kore_timers )
return v11 - __readfsqword(0x28u);
goto LABEL_5;
}
while ( v6 >= *v5 )
{
v5 = (unsigned __int64 *)v5[5];
if ( !v5 )
goto LABEL_17;
}
v7 = v5[6];
v2 = v1;
*(_QWORD *)(v1 + 40) = v5;
*(_QWORD *)(v1 + 48) = v7;
*(_QWORD *)v5[6] = v1;
v8 = v1 + 40;
v1 = kore_timers;
v5[6] = v8;
if ( v2 == v1 )
return v11 - __readfsqword(0x28u);
LABEL_5:
if ( !v1 )
return v11 - __readfsqword(0x28u);
}
kore_free(v1);
goto LABEL_4;
}
return v11 - __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(worker_max_connections + (unsigned int)nlisteners, 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(worker_max_connections + (unsigned int)nlisteners, 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
int v2; // ecx
int v3; // eax
__int64 v4; // rbp
struct epoll_event *v5; // rdx
epoll_data_t v6; // rax
uint32_t v7; // edx
int v9; // edi
char *v10; // rax
unsigned __int64 v11; // [rsp+8h] [rbp-20h]

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

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

v2 = -1;
v11 = __readfsqword(0x28u);
if ( timeout != -1 )
v2 = timeout;
v3 = epoll_wait(efd, events, event_count, v2);
if ( v3 == -1 )
{
v9 = *_errno_location();
if ( v9 != 4 )
{
v10 = strerror(v9);
fatal("epoll_wait(): %s", v10);
goto LABEL_14;
}
}
else
{
v4 = v3;
v1 = 0LL;
if ( v3 > 0 )
{
while ( 1 )
{
v5 = &events[v1];
v6.ptr = (void *)v5->data;
if ( !v6.u64 )
break;
v7 = v5->events;
if ( (v7 & 1) != 0 )
*(_DWORD *)(v6.u64 + 4) |= 1u;
if ( (v7 & 4) != 0 )
*(_DWORD *)(v6.u64 + 4) |= 2u;
++v1;
(*(void (__fastcall **)(epoll_data_t, bool, _QWORD))(v6.u64 + 8))(v6, (v7 & 0x2018) != 0, v7 & 0x2018);
if ( v4 == v1 )
return v11 - __readfsqword(0x28u);
}
LABEL_14:
fatal("events[%d].data.ptr == NULL", (unsigned int)v1);
}
}
return v11 - __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:
__int64 __fastcall kore_platform_event_schedule(int fd, uint32_t a2, __int64 a3, epoll_data_t a4)
{
int v4; // ebp
int *v6; // rax
int *v7; // rbx
char *v8; // rax
char *v9; // rax
struct epoll_event event; // [rsp+Ch] [rbp-2Ch] BYREF
unsigned __int64 v11; // [rsp+18h] [rbp-20h]

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

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

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

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:
__int64 __fastcall kore_platform_event_level_all(int a1, epoll_data_t a2)
{
return kore_platform_event_schedule(a1, 0x2005u, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, epoll_data_t a2)
{
return func5(a1, 0x2005u, 0LL, a2);
}

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:
__int64 __fastcall kore_platform_event_level_read(int a1, epoll_data_t a2)
{
return kore_platform_event_schedule(a1, 0x2001u, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, epoll_data_t a2)
{
return func5(a1, 0x2001u, 0LL, a2);
}

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


decompiled_code:
__int64 __fastcall kore_platform_event_all(int a1, epoll_data_t a2)
{
return kore_platform_event_schedule(a1, 0x80002005, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, epoll_data_t a2)
{
return func5(a1, 0x80002005, 0LL, a2);
}

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


decompiled_code:
__int64 __fastcall kore_platform_schedule_read(int a1, epoll_data_t a2)
{
return kore_platform_event_schedule(a1, 0x80000001, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, epoll_data_t a2)
{
return func5(a1, 0x80000001, 0LL, a2);
}

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:
__int64 __fastcall kore_platform_schedule_write(int a1, epoll_data_t a2)
{
return kore_platform_event_schedule(a1, 0x80000004, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, epoll_data_t a2)
{
return func5(a1, 0x80000004, 0LL, a2);
}

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:
__int64 kore_platform_proctitle()
{
return kore_proctitle();
}

canonical_decompiled_code:
__int64 func14()
{
return kore_proctitle();
}

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:
__int64 kore_platform_sandbox()
{
return kore_seccomp_enable();
}

canonical_decompiled_code:
__int64 func16()
{
return kore_seccomp_enable();
}

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);
fatalx("getrandom(): %s", v3);
}
if ( v0 != 4 )
{
fatalx("getrandom() %zd != %zu", v0, 4uLL);
JUMPOUT(0x89ELL);
}
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);
fatalx("getrandom(): %s", v3);
}
if ( v0 != 4 )
{
fatalx("getrandom() %zd != %zu", v0, 4uLL);
JUMPOUT(0x89ELL);
}
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:
__int64 keymgr_load_randfile()
{
int v0; // eax
int v1; // ebp
__int64 v2; // rbx
ssize_t v3; // rax
int v4; // edi
char *v5; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // 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);
v0 = open(kore_rand_file, 0);
v1 = v0;
if ( v0 == -1 )
goto LABEL_16;
if ( fstat(v0, &v13) != -1 )
{
if ( (v13.st_mode & 0xF000) == 0x8000 )
{
if ( v13.st_size == 1024 )
{
v2 = 0LL;
while ( 1 )
{
while ( 1 )
{
v3 = read(v1, buf, 0x400uLL);
if ( !v3 )
{
fatalx("EOF on %s", kore_rand_file);
LABEL_16:
v9 = _errno_location();
v10 = strerror(*v9);
fatalx("open(%s): %s", kore_rand_file, v10);
}
if ( v3 == -1 )
break;
LABEL_11:
v2 += v3;
RAND_seed(buf, (unsigned int)v3);
OPENSSL_cleanse(buf, 1024LL);
if ( v2 == 1024 )
{
close(v1);
if ( unlink(kore_rand_file) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "failed to unlink %s: %s", kore_rand_file, v8);
}
return v15 - __readfsqword(0x28u);
}
}
v4 = *_errno_location();
if ( v4 != 4 )
{
v5 = strerror(v4);
v3 = fatalx("read(%s): %s", kore_rand_file, v5);
goto LABEL_11;
}
}
}
fatalx("%s has an invalid size", kore_rand_file);
}
fatalx("%s is not a file", kore_rand_file);
}
v11 = _errno_location();
v12 = strerror(*v11);
fatalx("stat(%s): %s", kore_rand_file, v12);
return keymgr_save_randfile();
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // eax
int v1; // ebp
__int64 v2; // rbx
ssize_t v3; // rax
int v4; // edi
char *v5; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // 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);
v0 = open(kore_rand_file, 0);
v1 = v0;
if ( v0 == -1 )
goto LABEL_16;
if ( fstat(v0, &v13) != -1 )
{
if ( (v13.st_mode & 0xF000) == 0x8000 )
{
if ( v13.st_size == 1024 )
{
v2 = 0LL;
while ( 1 )
{
while ( 1 )
{
v3 = read(v1, buf, 0x400uLL);
if ( !v3 )
{
fatalx("EOF on %s", kore_rand_file);
LABEL_16:
v9 = _errno_location();
v10 = strerror(*v9);
fatalx("open(%s): %s", kore_rand_file, v10);
}
if ( v3 == -1 )
break;
LABEL_11:
v2 += v3;
RAND_seed(buf, (unsigned int)v3);
OPENSSL_cleanse(buf, 1024LL);
if ( v2 == 1024 )
{
close(v1);
if ( unlink(kore_rand_file) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "failed to unlink %s: %s", kore_rand_file, v8);
}
return v15 - __readfsqword(0x28u);
}
}
v4 = *_errno_location();
if ( v4 != 4 )
{
v5 = strerror(v4);
v3 = fatalx("read(%s): %s", kore_rand_file, v5);
goto LABEL_11;
}
}
}
fatalx("%s has an invalid size", kore_rand_file);
}
fatalx("%s is not a file", kore_rand_file);
}
v11 = _errno_location();
v12 = strerror(*v11);
fatalx("stat(%s): %s", kore_rand_file, v12);
return func2();
}

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; // ebp
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 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "failed to open %s: %s - random data not written", "rnd.tmp", v8);
}
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 )
{
v5 = _errno_location();
v6 = strerror(*v5);
kore_log(4LL, "rename(%s, %s): %s", "rnd.tmp", kore_rand_file, v6);
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 func2()
{
__int64 v0; // rax
const char *v1; // rax
int v3; // eax
int v4; // ebp
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 )
{
v7 = _errno_location();
v8 = strerror(*v7);
kore_log(4LL, "failed to open %s: %s - random data not written", "rnd.tmp", v8);
}
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 )
{
v5 = _errno_location();
v6 = strerror(*v5);
kore_log(4LL, "rename(%s, %s): %s", "rnd.tmp", kore_rand_file, v6);
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; }