decompiled_code:
__int64 sxc_map_strset(_QWORD *a1, __int64 a2, unsigned int a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-100h] BYREF
__int64 v5[4]; // [rsp+20h] [rbp-E8h] BYREF

va_start(va, a3);
v5[0] = a1[2];
sxc_value_setv(v5, a3, va);
sxc_value_snormalize(v5);
return (*(__int64 (__fastcall **)(_QWORD, __int64, __int64 *))(a1[1] + 24LL))(*a1, a2, v5);
}

canonical_decompiled_code:
__int64 func6(_QWORD *a1, __int64 a2, unsigned int a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-100h] BYREF
__int64 v5[4]; // [rsp+20h] [rbp-E8h] BYREF

va_start(va, a3);
v5[0] = a1[2];
sxc_value_setv(v5, a3, va);
sxc_value_snormalize(v5);
return (*(__int64 (__fastcall **)(_QWORD, __int64, __int64 *))(a1[1] + 24LL))(*a1, a2, v5);
}

original_code:
void sxc_map_strset(SxcMap* map, const char* key, SxcDataType type, ...) {
  va_list varg;
  SxcValue value;
  value.context = map->context;
  __builtin_va_start(varg,type);
  sxc_value_setv(&value, type, varg);
  __builtin_va_end(varg);
  sxc_value_snormalize(&value);
  (map->binding->strset)(map->underlying, key, &value);
}

canonical_original_code:
void sxc_map_strset(struct _SxcMap *map, char *key, enum _SxcDataType type, ...) {
  __builtin_va_list varg;
  struct _SxcValue value;
  value.context = map->context;
  __builtin_va_start(varg,type);
  sxc_value_setv(&value, type, varg);
  __builtin_va_end(varg);
  sxc_value_snormalize(&value);
  (map->binding->strset)(map->underlying, key, &value);
}


repo: jonathanhefner/sxc

decompiled_code:
__int64 __fastcall sxc_map_length(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax
int v3; // ebx
int v4; // er13
__int64 v5; // rsi
int v6; // eax
unsigned int v7; // ebx
bool v8; // cc
__int64 v9; // [rsp+0h] [rbp-68h] BYREF
int v10; // [rsp+8h] [rbp-60h]
int v11; // [rsp+10h] [rbp-58h]
__int64 v12; // [rsp+20h] [rbp-48h] BYREF
int v13; // [rsp+28h] [rbp-40h]

v1 = *(__int64 (__fastcall **)(_QWORD))(a1[1] + 32LL);
if ( v1 )
return v1(*a1);
v3 = -1;
v4 = 0;
v5 = 0LL;
v9 = a1[2];
v12 = v9;
while ( 1 )
{
v5 = map_iter(a1, v5, (__int64)&v9, (__int64)&v12);
if ( !v5 )
break;
if ( v10 == 7 )
{
if ( v13 != 9 )
return 0xFFFFFFFFLL;
}
else if ( v10 == 2 )
{
++v4;
if ( v3 < v11 )
v3 = v11;
}
}
v6 = v3;
v7 = v3 + 1;
v8 = v6 - v4 < 8 * v4;
result = 0xFFFFFFFFLL;
if ( v8 )
result = v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax
int v3; // ebx
int v4; // er13
__int64 v5; // rsi
int v6; // eax
unsigned int v7; // ebx
bool v8; // cc
__int64 v9; // [rsp+0h] [rbp-68h] BYREF
int v10; // [rsp+8h] [rbp-60h]
int v11; // [rsp+10h] [rbp-58h]
__int64 v12; // [rsp+20h] [rbp-48h] BYREF
int v13; // [rsp+28h] [rbp-40h]

v1 = *(__int64 (__fastcall **)(_QWORD))(a1[1] + 32LL);
if ( v1 )
return v1(*a1);
v3 = -1;
v4 = 0;
v5 = 0LL;
v9 = a1[2];
v12 = v9;
while ( 1 )
{
v5 = func0(a1, v5, (__int64)&v9, (__int64)&v12);
if ( !v5 )
break;
if ( v10 == 7 )
{
if ( v13 != 9 )
return 0xFFFFFFFFLL;
}
else if ( v10 == 2 )
{
++v4;
if ( v3 < v11 )
v3 = v11;
}
}
v6 = v3;
v7 = v3 + 1;
v8 = v6 - v4 < 8 * v4;
result = 0xFFFFFFFFLL;
if ( v8 )
result = v7;
return result;
}

original_code:
int sxc_map_length(SxcMap* map) {
  void* iter = ((void *)0);
  SxcValue key;
  SxcValue val;
  int key_count = 0;
  int key_max = -1;
  if (map->binding->length != ((void *)0)) {
    return (map->binding->length)(map->underlying);
  }
  else {
    key.context = map->context;
    val.context = map->context;
    while ((iter = map_iter(map, iter, &key, &val))) {
      if (key.type == sxc_sstring && val.type != sxc_sfunc) {
        return -1;
      }
      if (key.type == sxc_cint) {
        key_count += 1;
        key_max = key.data.cint > key_max ? key.data.cint : key_max;
      }
    }
    return ((key_max - key_count) >= (8 * key_count)) ? -1 : (key_max + 1);
  }
}

canonical_original_code:
int sxc_map_length(struct _SxcMap *map) {
  void *iter = ((void *)0);
  struct _SxcValue key;
  struct _SxcValue val;
  int key_count = 0;
  int key_max = -1;
  if (map->binding->length != ((void *)0)) {
    return (map->binding->length)(map->underlying);
  }
  else {
    key.context = map->context;
    val.context = map->context;
    while ((iter = map_iter(map, iter, &key, &val))) {
      if (key.type == sxc_sstring && val.type != sxc_sfunc) {
        return -1;
      }
      if (key.type == sxc_cint) {
        key_count += 1;
        key_max = key.data.cint > key_max ? key.data.cint : key_max;
      }
    }
    return ((key_max - key_count) >= (8 * key_count)) ? -1 : (key_max + 1);
  }
}

variable_types: struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *

variable_types:
iter
void *

variable_types: struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; }

return_type: int

user_defined_types: struct _SxcValue { struct _SxcContext *context; enum _SxcDataType type; union _SxcData data; }

user_defined_types: struct _SxcMap { void *underlying; struct _SxcMapBinding *binding; struct _SxcContext *context; }

user_defined_types: union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString *string; struct _SxcMap *map; struct _SxcFunc *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; }

user_defined_types: struct _SxcContext { void *underlying; struct _SxcContextBinding *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk _firstchunk; char _firstdata[-1]; }

user_defined_types: struct _SxcString { void *underlying; struct _SxcStringBinding *binding; struct _SxcContext *context; char *data; int length; }

user_defined_types: struct _SxcFunc { void *underlying; struct _SxcFunctionBinding *binding; struct _SxcContext *context; }

user_defined_types: struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod *methods, struct _SxcLibProperty *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); }


decompiled_code:
__int64 __fastcall sxc_map_iter(_QWORD *a1, __int64 a2, _QWORD *a3, _QWORD *a4)
{
__int64 v5; // rax
__int64 v7; // r13

v5 = a1[2];
*a3 = v5;
*a4 = v5;
v7 = map_iter(a1, a2, (__int64)a3, (__int64)a4);
if ( v7 )
{
sxc_value_cnormalize(a3);
sxc_value_cnormalize(a4);
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, __int64 a2, _QWORD *a3, _QWORD *a4)
{
__int64 v5; // rax
__int64 v7; // r13

v5 = a1[2];
*a3 = v5;
*a4 = v5;
v7 = func0(a1, a2, (__int64)a3, (__int64)a4);
if ( v7 )
{
sxc_value_cnormalize(a3);
sxc_value_cnormalize(a4);
}
return v7;
}

original_code:
void* sxc_map_iter(SxcMap* map, void* state, SxcValue* return_key, SxcValue* return_value) {
  return_key->context = map->context;
  return_value->context = map->context;
  state = map_iter(map, state, return_key, return_value);
  if (state != ((void *)0)) {
    sxc_value_cnormalize(return_key);
    sxc_value_cnormalize(return_value);
  }
  return state;
}

canonical_original_code:
void *sxc_map_iter(struct _SxcMap *map, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value) {
  return_key->context = map->context;
  return_value->context = map->context;
  state = map_iter(map, state, return_key, return_value);
  if (state != ((void *)0)) {
    sxc_value_cnormalize(return_key);
    sxc_value_cnormalize(return_value);
  }
  return state;
}

variable_types:
state
void *

return_type: void *

user_defined_types: struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); }

user_defined_types: enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 }

user_defined_types: struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; }

user_defined_types: struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; }

user_defined_types: struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); }

user_defined_types: struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); }

user_defined_types: struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); }


decompiled_code:
void __noreturn sxc_error(__int64 a1, const char *a2, ...)
{
__int64 v2; // rbp
char *v3; // r12
gcc_va_list arg; // [rsp+8h] [rbp-100h] BYREF

va_start(arg, a2);
v2 = 8 * (unsigned int)strlen(a2);
v3 = (char *)sxc_alloc(a1, v2);
vsnprintf(v3, (int)v2, a2, arg);
if ( (int)v2 <= 0 )
{
v3 = (char *)sxc_alloc(a1, 1LL);
va_end(arg);
va_start(arg, a2);
vsnprintf(v3, 1uLL, a2, arg);
}
sxc_value_set(a1 + 24, 13LL, v3, 0LL);
*(_DWORD *)(a1 + 56) = 1;
siglongjmp(*(struct __jmp_buf_tag **)(a1 + 64), 1);
}

canonical_decompiled_code:
void __noreturn func0(__int64 a1, const char *a2, ...)
{
__int64 v2; // rbp
char *v3; // r12
gcc_va_list arg; // [rsp+8h] [rbp-100h] BYREF

va_start(arg, a2);
v2 = 8 * (unsigned int)strlen(a2);
v3 = (char *)func1(a1, v2);
vsnprintf(v3, (int)v2, a2, arg);
if ( (int)v2 <= 0 )
{
v3 = (char *)func1(a1, 1LL);
va_end(arg);
va_start(arg, a2);
vsnprintf(v3, 1uLL, a2, arg);
}
sxc_value_set(a1 + 24, 13LL, v3, 0LL);
*(_DWORD *)(a1 + 56) = 1;
siglongjmp(*(struct __jmp_buf_tag **)(a1 + 64), 1);
}

original_code:
void* sxc_error(SxcContext* context, const char* message_format, ...) {
  va_list varg;
  char* buffer;
  int buffer_len;
  int actual_len;
    buffer_len = strlen(message_format) * 8;
    buffer = sxc_alloc(context, buffer_len);
    __builtin_va_start(varg,message_format);
    vsnprintf(buffer, buffer_len, message_format, varg);
    __builtin_va_end(varg);
    if ((actual_len + 1) > buffer_len) {
      buffer_len = actual_len + 1;
      buffer = sxc_alloc(context, buffer_len);
      __builtin_va_start(varg,message_format);
      vsnprintf(buffer, buffer_len, message_format, varg);
      __builtin_va_end(varg);
    } else {
      while (actual_len < 0) {
        buffer_len *= 4;
        buffer = sxc_alloc(context, buffer_len);
        __builtin_va_start(varg,message_format);
        actual_len = vsnprintf(buffer, buffer_len, message_format, varg);
        __builtin_va_end(varg);
      }
    }
  sxc_value_set(&context->return_value, sxc_cchars, buffer, actual_len);
  context->has_error = 1;
  (siglongjmp(*(sigjmp_buf*)(context->_jmpbuf), 1));
  return ((void *)0);
}

canonical_original_code:
void *sxc_error(struct _SxcContext *context, char *message_format, ...) {
  __builtin_va_list varg;
  char *buffer;
  int buffer_len;
  int actual_len;
    buffer_len = strlen(message_format) * 8;
    buffer = sxc_alloc(context, buffer_len);
    __builtin_va_start(varg,message_format);
    vsnprintf(buffer, buffer_len, message_format, varg);
    __builtin_va_end(varg);
    if ((actual_len + 1) > buffer_len) {
      buffer_len = actual_len + 1;
      buffer = sxc_alloc(context, buffer_len);
      __builtin_va_start(varg,message_format);
      vsnprintf(buffer, buffer_len, message_format, varg);
      __builtin_va_end(varg);
    } else {
      while (actual_len < 0) {
        buffer_len *= 4;
        buffer = sxc_alloc(context, buffer_len);
        __builtin_va_start(varg,message_format);
        actual_len = vsnprintf(buffer, buffer_len, message_format, varg);
        __builtin_va_end(varg);
      }
    }
  sxc_value_set(&context->return_value, sxc_cchars, buffer, actual_len);
  context->has_error = 1;
  (siglongjmp(*(struct __jmp_buf_tag (*)[1])(context->_jmpbuf), 1));
  return ((void *)0);
}

variable_types: struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue { struct _SxcContext *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *

variable_types:
buffer
char *

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 __fastcall sxc_alloc(__int64 a1, __int64 a2)
{
int *v2; // rbp
int v3; // ebx
__int64 v4; // rax
int v5; // edx
__int64 v6; // r13
__int64 v7; // rsi
int v8; // eax
int *v9; // rax

v2 = (int *)(a1 + 72);
printf("in sxc_context_alloc, size: %d, first chunk free: %d\n", a2, *(unsigned int *)(a1 + 72));
if ( (int)a2 <= 0 )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( *v2 >= (int)a2 )
{
v4 = v2[4];
v5 = a2 + v2[4];
goto LABEL_7;
}
if ( !*((_QWORD *)v2 + 1) )
break;
v2 = (int *)*((_QWORD *)v2 + 1);
}
puts("...allocating new node");
v8 = 0x3FFF;
v3 = v2[4] + *v2;
if ( (int)a2 <= 0x3FFF )
v8 = a2;
do
v3 *= 2;
while ( v3 <= v8 );
if ( (int)a2 >= v3 )
v3 = a2;
printf("...new node space: %d\n", (unsigned int)v3);
v9 = (int *)malloc(v3 + 23LL);
*((_QWORD *)v2 + 1) = v9;
if ( !v9 )
sxc_error(a1, "Error: out of memory");
*v9 = v3;
v2 = v9;
v5 = a2;
v9[4] = 0;
*((_QWORD *)v9 + 1) = 0LL;
v4 = 0LL;
LABEL_7:
v2[4] = v5;
v6 = (__int64)v2 + v4 + 20;
v7 = (unsigned int)(v3 - a2);
*v2 = v7;
printf("...done sxc_context_alloc, free space: %d\n", v7);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int *v2; // rbp
int v3; // ebx
__int64 v4; // rax
int v5; // edx
__int64 v6; // r13
__int64 v7; // rsi
int v8; // eax
int *v9; // rax

v2 = (int *)(a1 + 72);
printf("in sxc_context_alloc, size: %d, first chunk free: %d\n", a2, *(unsigned int *)(a1 + 72));
if ( (int)a2 <= 0 )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( *v2 >= (int)a2 )
{
v4 = v2[4];
v5 = a2 + v2[4];
goto LABEL_7;
}
if ( !*((_QWORD *)v2 + 1) )
break;
v2 = (int *)*((_QWORD *)v2 + 1);
}
puts("...allocating new node");
v8 = 0x3FFF;
v3 = v2[4] + *v2;
if ( (int)a2 <= 0x3FFF )
v8 = a2;
do
v3 *= 2;
while ( v3 <= v8 );
if ( (int)a2 >= v3 )
v3 = a2;
printf("...new node space: %d\n", (unsigned int)v3);
v9 = (int *)malloc(v3 + 23LL);
*((_QWORD *)v2 + 1) = v9;
if ( !v9 )
func0(a1, "Error: out of memory");
*v9 = v3;
v2 = v9;
v5 = a2;
v9[4] = 0;
*((_QWORD *)v9 + 1) = 0LL;
v4 = 0LL;
LABEL_7:
v2[4] = v5;
v6 = (__int64)v2 + v4 + 20;
v7 = (unsigned int)(v3 - a2);
*v2 = v7;
printf("...done sxc_context_alloc, free space: %d\n", v7);
return v6;
}

original_code:
void* sxc_alloc(SxcContext* context, int size) {
  SxcMemoryChunk* walker = &(context->_firstchunk);
  void* retval;
  int new_free_space;
printf("in sxc_context_alloc, size: %d, first chunk free: %d\n", size, walker->free_space);
  if (size <= 0) return ((void *)0);
  while (walker->free_space < size && walker->next_chunk != ((void *)0)) {
    walker = walker->next_chunk;
  }
  if (walker->free_space < size) {
printf("...allocating new node\n");
    new_free_space = (walker->offset + walker->free_space);
    do {
      new_free_space *= 2;
    } while (new_free_space <= size && new_free_space < (1024 * 16));
    new_free_space = size > new_free_space ? size : new_free_space;
printf("...new node space: %d\n", new_free_space);
    walker->next_chunk = malloc(sizeof(SxcMemoryChunk) + new_free_space - 1 );
    if (walker->next_chunk == ((void *)0)) {
      return sxc_error(context, "Error: out of memory");
    }
    walker = walker->next_chunk;
    walker->free_space = new_free_space;
    walker->offset = 0;
    walker->next_chunk = ((void *)0);
  }
  retval = &(walker->data) + walker->offset;
  walker->free_space -= size;
  walker->offset += size;
printf("...done sxc_context_alloc, free space: %d\n", walker->free_space);
  return retval;
}

canonical_original_code:
void *sxc_alloc(struct _SxcContext *context, int size) {
  struct _SxcMemoryChunk *walker = &(context->_firstchunk);
  void *retval;
  int new_free_space;
printf("in sxc_context_alloc, size: %d, first chunk free: %d\n", size, walker->free_space);
  if (size <= 0) return ((void *)0);
  while (walker->free_space < size && walker->next_chunk != ((void *)0)) {
    walker = walker->next_chunk;
  }
  if (walker->free_space < size) {
printf("...allocating new node\n");
    new_free_space = (walker->offset + walker->free_space);
    do {
      new_free_space *= 2;
    } while (new_free_space <= size && new_free_space < (1024 * 16));
    new_free_space = size > new_free_space ? size : new_free_space;
printf("...new node space: %d\n", new_free_space);
    walker->next_chunk = malloc(sizeof(SxcMemoryChunk) + new_free_space - 1 );
    if (walker->next_chunk == ((void *)0)) {
      return sxc_error(context, "Error: out of memory");
    }
    walker = walker->next_chunk;
    walker->free_space = new_free_space;
    walker->offset = 0;
    walker->next_chunk = ((void *)0);
  }
  retval = &(walker->data) + walker->offset;
  walker->free_space -= size;
  walker->offset += size;
printf("...done sxc_context_alloc, free space: %d\n", walker->free_space);
  return retval;
}

variable_types: size int

variable_types: struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } *

variable_types:
retval
void *


decompiled_code:
void __fastcall __noreturn sxc_typeerror(__int64 a1, const char *a2, unsigned int a3, __int64 a4)
{
__int64 v4; // rax
const char *v5; // r8
__int64 v6[18]; // [rsp+0h] [rbp-148h]
__int64 v7[23]; // [rsp+90h] [rbp-B8h]

v6[14] = (__int64)"a list of booleans";
v6[15] = (__int64)"a list of ints";
v6[4] = (__int64)"a string";
v6[7] = (__int64)"a string";
v6[10] = (__int64)"a string";
v6[11] = (__int64)"a reference to a C struct";
v6[13] = (__int64)"a string";
v6[16] = (__int64)"a list of doubles";
v7[4] = (__int64)"a string";
v7[7] = (__int64)"a string";
v6[0] = (__int64)"null";
v6[1] = (__int64)"a boolean";
v6[2] = (__int64)"an int";
v6[3] = (__int64)"a double";
v6[5] = (__int64)"a map";
v6[6] = (__int64)"a function";
v6[8] = (__int64)"a map";
v6[9] = (__int64)"a function";
v6[12] = (__int64)"a C function";
v6[17] = (__int64)"a list of strings";
v7[0] = (__int64)"null";
v7[1] = (__int64)"a boolean";
v7[2] = (__int64)"an int";
v7[3] = (__int64)"a double";
v7[5] = (__int64)"a map";
v7[6] = (__int64)"a function";
v7[8] = (__int64)"a map";
v7[9] = (__int64)"a function";
v7[10] = (__int64)"a C string";
v7[11] = (__int64)"a C pointer";
v7[13] = (__int64)"an array of chars";
v7[14] = (__int64)"an array of booleans";
v7[15] = (__int64)"an array of ints";
v7[16] = (__int64)"an array of doubles";
v7[12] = (__int64)"a C function";
v7[17] = (__int64)"an array of strings";
if ( a3 )
{
if ( a3 <= 0x11 )
{
v4 = *(unsigned int *)(a4 + 8);
v5 = "of unknown type";
if ( (unsigned int)v4 <= 0x11 )
v5 = (const char *)v7[v4];
sxc_error(a1, "Expected %s to be %s (or compatible). Actual value was %s.", a2, (const char *)v6[a3], v5);
}
sxc_error(a1, "Can not extract %s because destination type (%d) is unrecognized.", a2, a3);
}
sxc_error(a1, "Can not extract %s because destination type is sxc_null.", a2);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(__int64 a1, const char *a2, unsigned int a3, __int64 a4)
{
__int64 v4; // rax
const char *v5; // r8
__int64 v6[18]; // [rsp+0h] [rbp-148h]
__int64 v7[23]; // [rsp+90h] [rbp-B8h]

v6[14] = (__int64)"a list of booleans";
v6[15] = (__int64)"a list of ints";
v6[4] = (__int64)"a string";
v6[7] = (__int64)"a string";
v6[10] = (__int64)"a string";
v6[11] = (__int64)"a reference to a C struct";
v6[13] = (__int64)"a string";
v6[16] = (__int64)"a list of doubles";
v7[4] = (__int64)"a string";
v7[7] = (__int64)"a string";
v6[0] = (__int64)"null";
v6[1] = (__int64)"a boolean";
v6[2] = (__int64)"an int";
v6[3] = (__int64)"a double";
v6[5] = (__int64)"a map";
v6[6] = (__int64)"a function";
v6[8] = (__int64)"a map";
v6[9] = (__int64)"a function";
v6[12] = (__int64)"a C function";
v6[17] = (__int64)"a list of strings";
v7[0] = (__int64)"null";
v7[1] = (__int64)"a boolean";
v7[2] = (__int64)"an int";
v7[3] = (__int64)"a double";
v7[5] = (__int64)"a map";
v7[6] = (__int64)"a function";
v7[8] = (__int64)"a map";
v7[9] = (__int64)"a function";
v7[10] = (__int64)"a C string";
v7[11] = (__int64)"a C pointer";
v7[13] = (__int64)"an array of chars";
v7[14] = (__int64)"an array of booleans";
v7[15] = (__int64)"an array of ints";
v7[16] = (__int64)"an array of doubles";
v7[12] = (__int64)"a C function";
v7[17] = (__int64)"an array of strings";
if ( a3 )
{
if ( a3 <= 0x11 )
{
v4 = *(unsigned int *)(a4 + 8);
v5 = "of unknown type";
if ( (unsigned int)v4 <= 0x11 )
v5 = (const char *)v7[v4];
func0(a1, "Expected %s to be %s (or compatible). Actual value was %s.", a2, (const char *)v6[a3], v5);
}
func0(a1, "Can not extract %s because destination type (%d) is unrecognized.", a2, a3);
}
func0(a1, "Can not extract %s because destination type is sxc_null.", a2);
}

original_code:
void sxc_typeerror(SxcContext* context, char* value_name, SxcDataType expected_type, SxcValue* actual_value) {
  const char* expected_types[] = {
                          "null",
                          "a boolean",
                          "an int",
                          "a double",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a reference to a C struct",
                          "a C function",
                          "a string",
                          "a list of booleans",
                          "a list of ints",
                          "a list of doubles",
                          "a list of strings"
    };
  const char* actual_types[] = {
                          "null",
                          "a boolean",
                          "an int",
                          "a double",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a map",
                          "a function",
                          "a C string",
                          "a C pointer",
                          "a C function",
                          "an array of chars",
                          "an array of booleans",
                          "an array of ints",
                          "an array of doubles",
                          "an array of strings"
    };
  if (expected_type == sxc_null) {
    sxc_error(context, "Can not extract %s because destination type "
        "is sxc_null.", value_name);
  }
  else if (expected_type >= (sizeof(expected_types) / sizeof(expected_types[0]))) {
    sxc_error(context, "Can not extract %s because destination type "
        "(%d) is unrecognized.", value_name, expected_type);
  }
  else {
    sxc_error(context, "Expected %s to be %s (or compatible).  Actual value was %s.",
        value_name,
        expected_types[expected_type],
        actual_value->type >= (sizeof(actual_types) / sizeof(actual_types[0]))
          ? "of unknown type"
          : actual_types[actual_value->type]);
  }
}

canonical_original_code:
void sxc_typeerror(struct _SxcContext *context, char *value_name, enum _SxcDataType expected_type, struct _SxcValue *actual_value) {
  char *expected_types[] = {
                          "null",
                          "a boolean",
                          "an int",
                          "a double",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a reference to a C struct",
                          "a C function",
                          "a string",
                          "a list of booleans",
                          "a list of ints",
                          "a list of doubles",
                          "a list of strings"
    };
  char *actual_types[] = {
                          "null",
                          "a boolean",
                          "an int",
                          "a double",
                          "a string",
                          "a map",
                          "a function",
                          "a string",
                          "a map",
                          "a function",
                          "a C string",
                          "a C pointer",
                          "a C function",
                          "an array of chars",
                          "an array of booleans",
                          "an array of ints",
                          "an array of doubles",
                          "an array of strings"
    };
  if (expected_type == sxc_null) {
    sxc_error(context, "Can not extract %s because destination type "
        "is sxc_null.", value_name);
  }
  else if (expected_type >= (sizeof(expected_types) / sizeof(expected_types[0]))) {
    sxc_error(context, "Can not extract %s because destination type "
        "(%d) is unrecognized.", value_name, expected_type);
  }
  else {
    sxc_error(context, "Expected %s to be %s (or compatible).  Actual value was %s.",
        value_name,
        expected_types[expected_type],
        actual_value->type >= (sizeof(actual_types) / sizeof(actual_types[0]))
          ? "of unknown type"
          : actual_types[actual_value->type]);
  }
}

variable_types: enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 }

return_type: void


decompiled_code:
__int64 sxc_arg(__int64 a1, unsigned int a2, char a3, unsigned int a4, ...)
{
char v5; // r12
char v6; // al
unsigned int v7; // er14
void (__fastcall **v8)(__int64, _QWORD, __int64 *); // rax
char *v10; // r12
gcc_va_list va; // [rsp+8h] [rbp-110h] BYREF
__int64 v12[4]; // [rsp+20h] [rbp-F8h] BYREF

v5 = a3;
v6 = a3;
v7 = 0;
if ( *(_DWORD *)(a1 + 16) > (signed int)a2 )
{
v8 = *(void (__fastcall ***)(__int64, _QWORD, __int64 *))(a1 + 8);
v12[0] = a1;
(*v8)(a1, a2, v12);
if ( a4 == -1 )
sxc_value_cnormalize(v12);
va_start(va, a4);
v7 = sxc_value_getv(v12, a4, va);
v6 = v5 & (v7 != 1);
}
if ( v6 )
{
v10 = (char *)sxc_alloc(a1, 32LL);
sprintf(v10, "argument %d", a2 + 1);
sxc_typeerror(a1, v10, a4, (__int64)v12);
}
return v7;
}

canonical_decompiled_code:
__int64 func3(__int64 a1, unsigned int a2, char a3, unsigned int a4, ...)
{
char v5; // r12
char v6; // al
unsigned int v7; // er14
void (__fastcall **v8)(__int64, _QWORD, __int64 *); // rax
char *v10; // r12
gcc_va_list va; // [rsp+8h] [rbp-110h] BYREF
__int64 v12[4]; // [rsp+20h] [rbp-F8h] BYREF

v5 = a3;
v6 = a3;
v7 = 0;
if ( *(_DWORD *)(a1 + 16) > (signed int)a2 )
{
v8 = *(void (__fastcall ***)(__int64, _QWORD, __int64 *))(a1 + 8);
v12[0] = a1;
(*v8)(a1, a2, v12);
if ( a4 == -1 )
sxc_value_cnormalize(v12);
va_start(va, a4);
v7 = sxc_value_getv(v12, a4, va);
v6 = v5 & (v7 != 1);
}
if ( v6 )
{
v10 = (char *)func1(a1, 32LL);
sprintf(v10, "argument %d", a2 + 1);
func2(a1, v10, a4, (__int64)v12);
}
return v7;
}

original_code:
int sxc_arg(SxcContext* context, int index, _Bool is_required, SxcDataType type, ...) {
  va_list varg;
  int retval = (0);
  SxcValue value;
  const char* value_name_format = "argument %d";
  char* value_name;
  if (index < context->argcount) {
    value.context = context;
    (context->binding->get_arg)(context, index, &value);
    if (type == (-1)) {
      sxc_value_cnormalize(&value);
    }
    __builtin_va_start(varg,type);
    retval = sxc_value_getv(&value, type, varg);
    __builtin_va_end(varg);
  }
  if (is_required && retval != (1)) {
    value_name = sxc_alloc(context, sizeof(char) * (strlen(value_name_format) + 20 + 1));
    sprintf(value_name, value_name_format, index + 1 );
    sxc_typeerror(context, value_name, type, &value);
  }
  return retval;
}

canonical_original_code:
int sxc_arg(struct _SxcContext *context, int index, _Bool is_required, enum _SxcDataType type, ...) {
  __builtin_va_list varg;
  int retval = (0);
  struct _SxcValue value;
  char *value_name_format = "argument %d";
  char *value_name;
  if (index < context->argcount) {
    value.context = context;
    (context->binding->get_arg)(context, index, &value);
    if (type == (-1)) {
      sxc_value_cnormalize(&value);
    }
    __builtin_va_start(varg,type);
    retval = sxc_value_getv(&value, type, varg);
    __builtin_va_end(varg);
  }
  if (is_required && retval != (1)) {
    value_name = sxc_alloc(context, sizeof(char) * (strlen(value_name_format) + 20 + 1));
    sprintf(value_name, value_name_format, index + 1 );
    sxc_typeerror(context, value_name, type, &value);
  }
  return retval;
}

variable_types: index int

variable_types: retval int


decompiled_code:
__int64 sxc_return(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return sxc_value_setv(a1 + 24, a2, va);
}

canonical_decompiled_code:
__int64 func4(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return sxc_value_setv(a1 + 24, a2, va);
}

original_code:
void sxc_return(SxcContext* context, SxcDataType type, ...) {
  va_list varg;
  __builtin_va_start(varg,type);
  sxc_value_setv(&context->return_value, type, varg);
  __builtin_va_end(varg);
}

canonical_original_code:
void sxc_return(struct _SxcContext *context, enum _SxcDataType type, ...) {
  __builtin_va_list varg;
  __builtin_va_start(varg,type);
  sxc_value_setv(&context->return_value, type, varg);
  __builtin_va_end(varg);
}


original_code:
void sxc_try(SxcContext* context, void* underlying, SxcContextBinding* binding, int argcount, SxcLibFunc func) {
  sigjmp_buf jmpbuf;
  context->underlying = underlying;
  context->binding = binding;
  context->argcount = argcount;
  context->return_value = (SxcValue){context, sxc_null, {0}};
  context->_jmpbuf = &jmpbuf;
  context->_firstchunk = (SxcMemoryChunk){(1024 * 2), ((void *)0), 0, 0};
  if (!(context->has_error = (__sigsetjmp (jmpbuf, 0)))) {
    (func)(context);
  }
  sxc_value_snormalize(&context->return_value);
}

canonical_original_code:
void sxc_try(struct _SxcContext *context, void *underlying, struct _SxcContextBinding *binding, int argcount, void func(struct _SxcContext *context)) {
  struct __jmp_buf_tag jmpbuf[1];
  context->underlying = underlying;
  context->binding = binding;
  context->argcount = argcount;
  context->return_value = (struct _SxcValue){context, sxc_null, {0}};
  context->_jmpbuf = &jmpbuf;
  context->_firstchunk = (struct _SxcMemoryChunk){(1024 * 2), ((void *)0), 0, 0};
  if (!(context->has_error = (__sigsetjmp (jmpbuf, 0)))) {
    (func)(context);
  }
  sxc_value_snormalize(&context->return_value);
}

variable_types:
underlying
void *

variable_types:
binding
struct _SxcContextBinding *

variable_types: argcount int

variable_types:
func
void (struct _SxcContext *context)

variable_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1]


decompiled_code:
int sxc_func_invoke(__int64 a1, int a2, unsigned int a3, ...)
{
__int64 *v4; // r15
__int64 v5; // rax
char *v6; // r12
__int64 v7; // r13
unsigned int *v8; // rsi
__int64 v9; // rdi
__int64 v10; // rax
unsigned int v11; // er14
__int64 *v12; // rdi
int result; // eax
unsigned int v14; // er15
__int64 v15; // r14
int v16; // er12
int v17; // er14
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // [rsp-400h] [rbp-640h] BYREF
__int64 *v21; // [rsp+8h] [rbp-238h]
__va_list_tag *v22; // [rsp+10h] [rbp-230h]
unsigned int v23; // [rsp+18h] [rbp-228h]
int v24; // [rsp+1Ch] [rbp-224h]
gcc_va_list va; // [rsp+28h] [rbp-218h] BYREF
__int64 v26; // [rsp+40h] [rbp-200h] BYREF
int v27; // [rsp+48h] [rbp-1F8h]
char v28; // [rsp+60h] [rbp-1E0h] BYREF

v24 = a2;
v23 = a3;
puts("in sxc_function_invoke");
v4 = &v20;
if ( a2 > 32 )
{
v17 = v24;
v18 = sxc_alloc(*(_QWORD *)(a1 + 16), (unsigned int)(40 * v24));
v27 = 0;
v6 = (char *)v18;
v4 = (__int64 *)(v18 + 8LL * v17);
v19 = *(_QWORD *)(a1 + 16);
v22 = va;
v26 = v19;
va_start(va, a3);
v21 = &v26;
sxc_value_getv(&v26, v23);
puts("done skipping dest");
}
else
{
v5 = *(_QWORD *)(a1 + 16);
v27 = 0;
v22 = va;
v6 = &v28;
v26 = v5;
va_start(va, a3);
v21 = &v26;
sxc_value_getv(&v26, v23);
puts("done skipping dest");
if ( v24 <= 0 )
goto LABEL_8;
}
v7 = 0LL;
do
{
v10 = *(_QWORD *)(a1 + 16);
*(_QWORD *)&v6[8 * v7] = v4;
v11 = v7;
v12 = v4;
*v4 = v10;
if ( va[0].gp_offset <= 0x2F )
{
v8 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
else
{
v8 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
v12 = *(__int64 **)&v6[8 * v7];
}
v4 += 4;
sxc_value_setv(v12, *v8, v22);
printf("done setting arg %d\n", (unsigned int)v7);
v9 = *(_QWORD *)&v6[8 * v7++];
sxc_value_snormalize(v9);
printf("done interning arg %d\n", v11);
}
while ( v24 > (int)v7 );
LABEL_8:
(**(void (__fastcall ***)(_QWORD, char *, _QWORD, __int64 *))(a1 + 8))(*(_QWORD *)a1, v6, (unsigned int)v24, v21);
puts("done invoking func");
result = v23;
if ( v23 )
{
if ( v23 == -1 )
sxc_value_cnormalize(v21);
v14 = v23;
v15 = (__int64)v21;
va_end(va);
va_start(va, a3);
v16 = sxc_value_getv(v21, v23);
result = puts("done extracting return_value");
if ( v16 != 1 )
result = sxc_typeerror(*(_QWORD *)(a1 + 16), "return value", v14, v15);
}
return result;
}

canonical_decompiled_code:
int func0(__int64 a1, int a2, unsigned int a3, ...)
{
__int64 *v4; // r15
__int64 v5; // rax
char *v6; // r12
__int64 v7; // r13
unsigned int *v8; // rsi
__int64 v9; // rdi
__int64 v10; // rax
unsigned int v11; // er14
__int64 *v12; // rdi
int result; // eax
unsigned int v14; // er15
__int64 v15; // r14
int v16; // er12
int v17; // er14
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // [rsp-400h] [rbp-640h] BYREF
__int64 *v21; // [rsp+8h] [rbp-238h]
__va_list_tag *v22; // [rsp+10h] [rbp-230h]
unsigned int v23; // [rsp+18h] [rbp-228h]
int v24; // [rsp+1Ch] [rbp-224h]
gcc_va_list va; // [rsp+28h] [rbp-218h] BYREF
__int64 v26; // [rsp+40h] [rbp-200h] BYREF
int v27; // [rsp+48h] [rbp-1F8h]
char v28; // [rsp+60h] [rbp-1E0h] BYREF

v24 = a2;
v23 = a3;
puts("in sxc_function_invoke");
v4 = &v20;
if ( a2 > 32 )
{
v17 = v24;
v18 = sxc_alloc(*(_QWORD *)(a1 + 16), (unsigned int)(40 * v24));
v27 = 0;
v6 = (char *)v18;
v4 = (__int64 *)(v18 + 8LL * v17);
v19 = *(_QWORD *)(a1 + 16);
v22 = va;
v26 = v19;
va_start(va, a3);
v21 = &v26;
sxc_value_getv(&v26, v23);
puts("done skipping dest");
}
else
{
v5 = *(_QWORD *)(a1 + 16);
v27 = 0;
v22 = va;
v6 = &v28;
v26 = v5;
va_start(va, a3);
v21 = &v26;
sxc_value_getv(&v26, v23);
puts("done skipping dest");
if ( v24 <= 0 )
goto LABEL_8;
}
v7 = 0LL;
do
{
v10 = *(_QWORD *)(a1 + 16);
*(_QWORD *)&v6[8 * v7] = v4;
v11 = v7;
v12 = v4;
*v4 = v10;
if ( va[0].gp_offset <= 0x2F )
{
v8 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
else
{
v8 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
v12 = *(__int64 **)&v6[8 * v7];
}
v4 += 4;
sxc_value_setv(v12, *v8, v22);
printf("done setting arg %d\n", (unsigned int)v7);
v9 = *(_QWORD *)&v6[8 * v7++];
sxc_value_snormalize(v9);
printf("done interning arg %d\n", v11);
}
while ( v24 > (int)v7 );
LABEL_8:
(**(void (__fastcall ***)(_QWORD, char *, _QWORD, __int64 *))(a1 + 8))(*(_QWORD *)a1, v6, (unsigned int)v24, v21);
puts("done invoking func");
result = v23;
if ( v23 )
{
if ( v23 == -1 )
sxc_value_cnormalize(v21);
v14 = v23;
v15 = (__int64)v21;
va_end(va);
va_start(va, a3);
v16 = sxc_value_getv(v21, v23);
result = puts("done extracting return_value");
if ( v16 != 1 )
result = sxc_typeerror(*(_QWORD *)(a1 + 16), "return value", v14, v15);
}
return result;
}

original_code:
void sxc_func_invoke(SxcFunc* func, int argcount, SxcDataType return_type, ...) {
  va_list varg;
  const int default_argcount = 32;
  SxcValue values[default_argcount];
  SxcValue* arg_values = values;
  SxcValue* valueptrs[default_argcount];
  SxcValue** arg_valueptrs = valueptrs;
  SxcValue return_value;
  int i;
  SxcDataType type;
  int has_return_value;
printf("in sxc_function_invoke\n");
  if (argcount > default_argcount) {
    arg_valueptrs = sxc_alloc(func->context, argcount * (sizeof(SxcValue*) + sizeof(SxcValue)));
    arg_values = (SxcValue*)(arg_valueptrs + argcount);
  }
  return_value.context = func->context;
  return_value.type = sxc_null;
  __builtin_va_start(varg,return_type);
  sxc_value_getv(&return_value, return_type, varg);
printf("done skipping dest\n");
  for (i = 0; i < argcount; i += 1) {
    arg_valueptrs[i] = &arg_values[i];
    arg_valueptrs[i]->context = func->context;
    type = __builtin_va_arg(varg,SxcDataType);
    sxc_value_setv(arg_valueptrs[i], type, varg);
printf("done setting arg %d\n", i);
    sxc_value_snormalize(arg_valueptrs[i]);
printf("done interning arg %d\n", i);
  }
  __builtin_va_end(varg);
  (func->binding->invoke)(func->underlying, arg_valueptrs, argcount, &return_value);
printf("done invoking func\n");
  if (return_type != sxc_null) {
    if (return_type == (-1)) {
      sxc_value_cnormalize(&return_value);
    }
    __builtin_va_start(varg,return_type);
    has_return_value = sxc_value_getv(&return_value, return_type, varg);
printf("done extracting return_value\n");
    __builtin_va_end(varg);
    if (has_return_value != (1)) {
      sxc_typeerror(func->context, "return value", return_type, &return_value);
    }
  }
}

canonical_original_code:
void sxc_func_invoke(struct _SxcFunc *func, int argcount, enum _SxcDataType return_type, ...) {
  __builtin_va_list varg;
  int default_argcount = 32;
  struct _SxcValue values[-1];
  struct _SxcValue *arg_values = values;
  struct _SxcValue *valueptrs[-1];
  struct _SxcValue **arg_valueptrs = valueptrs;
  struct _SxcValue return_value;
  int i;
  enum _SxcDataType type;
  int has_return_value;
printf("in sxc_function_invoke\n");
  if (argcount > default_argcount) {
    arg_valueptrs = sxc_alloc(func->context, argcount * (sizeof(struct _SxcValue *) + sizeof(SxcValue)));
    arg_values = (struct _SxcValue *)(arg_valueptrs + argcount);
  }
  return_value.context = func->context;
  return_value.type = sxc_null;
  __builtin_va_start(varg,return_type);
  sxc_value_getv(&return_value, return_type, varg);
printf("done skipping dest\n");
  for (i = 0; i < argcount; i += 1) {
    arg_valueptrs[i] = &arg_values[i];
    arg_valueptrs[i]->context = func->context;
    type = __builtin_va_arg(varg,SxcDataType);
    sxc_value_setv(arg_valueptrs[i], type, varg);
printf("done setting arg %d\n", i);
    sxc_value_snormalize(arg_valueptrs[i]);
printf("done interning arg %d\n", i);
  }
  __builtin_va_end(varg);
  (func->binding->invoke)(func->underlying, arg_valueptrs, argcount, &return_value);
printf("done invoking func\n");
  if (return_type != sxc_null) {
    if (return_type == (-1)) {
      sxc_value_cnormalize(&return_value);
    }
    __builtin_va_start(varg,return_type);
    has_return_value = sxc_value_getv(&return_value, return_type, varg);
printf("done extracting return_value\n");
    __builtin_va_end(varg);
    if (has_return_value != (1)) {
      sxc_typeerror(func->context, "return value", return_type, &return_value);
    }
  }
}

variable_types: struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *

variable_types: struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } [-1]

variable_types: struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *[-1]

variable_types: struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } **

variable_types: i int


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

v2 = **a2;
luaL_checkstack(v2, 4LL, &LC0);
lua_pushlightuserdata(v2, a1);
lua_pushcclosure(v2, &l_libfunc_invoke, 1LL);
return get_value(0xFFFFFFFFLL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 **a2)
{
__int64 v2; // r12

v2 = **a2;
luaL_checkstack(v2, 4LL, &LC0);
lua_pushlightuserdata(v2, a1);
lua_pushcclosure(v2, &l_libfunc_invoke, 1LL);
return get_value(0xFFFFFFFFLL, a2);
}

original_code:
static int to_sfunc(SxcValue* value, void** dest, SxcFuncBinding** dest_binding) {
  SxcValue tmp_value;
  switch (value->type) {
    case sxc_sfunc:
      *dest = value->data.sfunc.underlying;
      *dest_binding = value->data.sfunc.binding;
      return (1);
    case sxc_func:
      *dest = value->data.func->underlying;
      *dest_binding = value->data.func->binding;
      return (1);
    case sxc_cfunc:
      tmp_value.context = value->context;
      (value->context->binding->to_sfunc)(value->data.cfunc, &tmp_value);
      *dest = tmp_value.data.sfunc.underlying;
      *dest_binding = tmp_value.data.sfunc.binding;
      return (1);
    default:
      return (0);
  }
}

canonical_original_code:
static int to_sfunc(struct _SxcValue *value, void **dest, struct _SxcFunctionBinding **dest_binding) {
  struct _SxcValue tmp_value;
  switch (value->type) {
    case sxc_sfunc:
      *dest = value->data.sfunc.underlying;
      *dest_binding = value->data.sfunc.binding;
      return (1);
    case sxc_func:
      *dest = value->data.func->underlying;
      *dest_binding = value->data.func->binding;
      return (1);
    case sxc_cfunc:
      tmp_value.context = value->context;
      (value->context->binding->to_sfunc)(value->data.cfunc, &tmp_value);
      *dest = tmp_value.data.sfunc.underlying;
      *dest_binding = tmp_value.data.sfunc.binding;
      return (1);
    default:
      return (0);
  }
}

variable_types: struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *

variable_types:
dest
void **


decompiled_code:
__int64 __fastcall get_arg(__int64 a1, int a2, __int64 a3)
{
return get_value((unsigned int)(a2 + 1), a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, __int64 a3)
{
return get_value((unsigned int)(a2 + 1), a3);
}

original_code:
static void get_arg(void* underlying, int index, SxcValue* return_value) {
  get_value(index + 1, return_value);
}

canonical_original_code:
static void get_arg(void *underlying, int index, struct _SxcValue *return_value) {
  get_value(index + 1, return_value);
}


decompiled_code:
__int64 __fastcall to_sstring(__int64 a1, int a2, __int64 **a3)
{
__int64 v4; // r13

v4 = **a3;
luaL_checkstack(v4, 3LL, &LC0);
lua_pushlstring(v4, a1, a2);
return get_value(0xFFFFFFFFLL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, __int64 **a3)
{
__int64 v4; // r13

v4 = **a3;
luaL_checkstack(v4, 3LL, &LC0);
lua_pushlstring(v4, a1, a2);
return get_value(0xFFFFFFFFLL, a3);
}

original_code:
static int to_sstring(SxcValue* value, void** dest, SxcStringBinding** dest_binding) {
  char* cchars;
  int length;
  char cpointer_bytes[sizeof(void*)];
  switch (value->type) {
    case sxc_sstring:
      *dest = value->data.sstring.underlying;
      *dest_binding = value->data.sstring.binding;
      return (1);
    case sxc_string:
      *dest = value->data.string->underlying;
      *dest_binding = value->data.string->binding;
      return (1);
    case sxc_cchars:
      return cchars_to_sstring(value->context, value->data.cchars.array,
              value->data.cchars.length, dest, dest_binding);
    case sxc_cstring:
      return cchars_to_sstring(value->context, value->data.cstring,
              strlen(value->data.cstring), dest, dest_binding);
    case sxc_cbool:
      return cbool_to_cchars(value->data.cbool, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cint:
      return cint_to_cchars(value->context, value->data.cint, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cdouble:
      return cdouble_to_cchars(value->context, value->data.cdouble, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cpointer:
      memcpy(cpointer_bytes, &(value->data.cpointer), sizeof(void*));
      return cchars_to_sstring(value->context, cpointer_bytes, sizeof(void*), dest, dest_binding);
    default:
      return (0);
  }
}

canonical_original_code:
static int to_sstring(struct _SxcValue *value, void **dest, struct _SxcStringBinding **dest_binding) {
  char *cchars;
  int length;
  char cpointer_bytes[-1];
  switch (value->type) {
    case sxc_sstring:
      *dest = value->data.sstring.underlying;
      *dest_binding = value->data.sstring.binding;
      return (1);
    case sxc_string:
      *dest = value->data.string->underlying;
      *dest_binding = value->data.string->binding;
      return (1);
    case sxc_cchars:
      return cchars_to_sstring(value->context, value->data.cchars.array,
              value->data.cchars.length, dest, dest_binding);
    case sxc_cstring:
      return cchars_to_sstring(value->context, value->data.cstring,
              strlen(value->data.cstring), dest, dest_binding);
    case sxc_cbool:
      return cbool_to_cchars(value->data.cbool, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cint:
      return cint_to_cchars(value->context, value->data.cint, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cdouble:
      return cdouble_to_cchars(value->context, value->data.cdouble, &cchars, &length)
          && cchars_to_sstring(value->context, cchars, length, dest, dest_binding);
    case sxc_cpointer:
      memcpy(cpointer_bytes, &(value->data.cpointer), sizeof(void *));
      return cchars_to_sstring(value->context, cpointer_bytes, sizeof(void *), dest, dest_binding);
    default:
      return (0);
  }
}

variable_types:
cchars
char *

variable_types: length int


decompiled_code:
void *__fastcall get_register_func(__int64 a1, const char *a2, int a3)
{
const char *v3; // r13
void *v4; // rax
void *result; // rax
char *v6; // rax

v3 = (const char *)fix_lib_name_constprop_0(a1, (__int64)a2, a3);
v4 = dlopen(v3, 2);
if ( !v4 || (result = dlsym(v4, "sxc_lib_register")) == 0LL )
{
v6 = dlerror();
result = (void *)sxc_error(a1, "Could not load library %s (%s): %s", a2, v3, v6);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1, const char *a2, int a3)
{
const char *v3; // r13
void *v4; // rax
void *result; // rax
char *v6; // rax

v3 = (const char *)func0(a1, (__int64)a2, a3);
v4 = dlopen(v3, 2);
if ( !v4 || (result = dlsym(v4, "sxc_lib_register")) == 0LL )
{
v6 = dlerror();
result = (void *)sxc_error(a1, "Could not load library %s (%s): %s", a2, v3, v6);
}
return result;
}

original_code:
static SxcLibFunc* get_register_func(SxcContext* context, char* lib_name, int lib_name_len) {
    char* lib_name_fixed = fix_lib_name(context, lib_name, lib_name_len, "libsxc_%s.so", '/');
    void* library;
    void* func;
    library = dlopen(lib_name_fixed, 0x00002);
    if (library != ((void *)0)) {
      func = dlsym(library, "sxc_lib_register");
      if (func != ((void *)0)) {
        return (SxcLibFunc*)func;
      }
    }
    return sxc_error(context, "Could not load library %s (%s): %s", lib_name, lib_name_fixed, dlerror());
  }

canonical_original_code:
static void (*get_register_func(struct _SxcContext *context, char *lib_name, int lib_name_len))(struct _SxcContext *context) {
    char *lib_name_fixed = fix_lib_name(context, lib_name, lib_name_len, "libsxc_%s.so", '/');
    void *library;
    void *func;
    library = dlopen(lib_name_fixed, 0x00002);
    if (library != ((void *)0)) {
      func = dlsym(library, "sxc_lib_register");
      if (func != ((void *)0)) {
        return (void (*)(struct _SxcContext *context))func;
      }
    }
    return sxc_error(context, "Could not load library %s (%s): %s", lib_name, lib_name_fixed, dlerror());
  }

variable_types:
context
struct _SxcContext *

variable_types:
library
void *

variable_types:
func
void *

return_type: void (*)(struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue { struct _SxcContext *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context)


decompiled_code:
int __fastcall sxc_load(__int64 a1)
{
size_t v1; // r12
const char *v2; // rbp
const char **v3; // rbx
unsigned int v4; // ebp
_QWORD *v5; // rbx
char *v6; // rsi
size_t v7; // rdx
__int64 v8; // rax
unsigned int v10; // [rsp+4h] [rbp-34h] BYREF
char *s2[6]; // [rsp+8h] [rbp-30h] BYREF

printf("in sxc_load, lib_name:%p lib_name_len:%p\n", s2, &v10);
sxc_arg(a1, 0LL, 1LL, 13LL, s2, &v10);
printf("done extract lib_name from args, name:%s, len:%d\n", s2[0], v10);
v1 = (int)v10;
v2 = s2[0];
v3 = (const char **)loaded_libs;
do
{
if ( !v3 )
{
puts("done find lib if it's already been loaded");
v4 = v10;
v5 = malloc((int)v10 + 25LL);
printf("done alloc new lib struct, name_len: %d\n", v4);
v6 = s2[0];
v7 = (int)v10;
*v5 = v5 + 3;
memcpy(v5 + 3, v6, v7);
puts("done set new lib name");
*(_BYTE *)(*v5 + (int)v10) = 0;
puts("done terminate new lib name");
v5[1] = get_register_func(a1, s2[0], v10);
puts("get new lib register_func");
v8 = loaded_libs;
loaded_libs = v5;
v5[2] = v8;
((void (__fastcall *)(__int64))v5[1])(a1);
return puts("done register_func()");
}
}
while ( strncmp(*v3, v2, v1) );
return puts("done find lib if it's already been loaded");
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
size_t v1; // r12
const char *v2; // rbp
const char **v3; // rbx
unsigned int v4; // ebp
_QWORD *v5; // rbx
char *v6; // rsi
size_t v7; // rdx
__int64 v8; // rax
unsigned int v10; // [rsp+4h] [rbp-34h] BYREF
char *s2[6]; // [rsp+8h] [rbp-30h] BYREF

printf("in sxc_load, lib_name:%p lib_name_len:%p\n", s2, &v10);
sxc_arg(a1, 0LL, 1LL, 13LL, s2, &v10);
printf("done extract lib_name from args, name:%s, len:%d\n", s2[0], v10);
v1 = (int)v10;
v2 = s2[0];
v3 = (const char **)loaded_libs;
do
{
if ( !v3 )
{
puts("done find lib if it's already been loaded");
v4 = v10;
v5 = malloc((int)v10 + 25LL);
printf("done alloc new lib struct, name_len: %d\n", v4);
v6 = s2[0];
v7 = (int)v10;
*v5 = v5 + 3;
memcpy(v5 + 3, v6, v7);
puts("done set new lib name");
*(_BYTE *)(*v5 + (int)v10) = 0;
puts("done terminate new lib name");
v5[1] = func1(a1, s2[0], v10);
puts("get new lib register_func");
v8 = loaded_libs;
loaded_libs = v5;
v5[2] = v8;
((void (__fastcall *)(__int64))v5[1])(a1);
return puts("done register_func()");
}
}
while ( strncmp(*v3, v2, v1) );
return puts("done find lib if it's already been loaded");
}

original_code:
void sxc_load(SxcContext* context) {
  char* lib_name;
  int lib_name_len;
  LoadedLib* lib;
printf("in sxc_load, lib_name:%p lib_name_len:%p\n", &lib_name, &lib_name_len);
  sxc_arg(context, 0, 1, sxc_cchars, &lib_name, &lib_name_len);
printf("done extract lib_name from args, name:%s, len:%d\n", lib_name, lib_name_len);
  lib = loaded_libs;
  while (lib != ((void *)0) && strncmp(lib->name, lib_name, lib_name_len) != 0) ;
printf("done find lib if it's already been loaded\n");
  if (lib == ((void *)0)) {
    lib = malloc(sizeof(LoadedLib) + lib_name_len + 1);
printf("done alloc new lib struct, name_len: %d\n", lib_name_len);
    lib->name = (char*)(lib + 1);
    memcpy(lib->name, lib_name, lib_name_len);
printf("done set new lib name\n");
    lib->name[lib_name_len] = '\0';
printf("done terminate new lib name\n");
    lib->register_func = get_register_func(context, lib_name, lib_name_len);
printf("get new lib register_func\n");
    lib->next = loaded_libs;
    loaded_libs = lib;
    (lib->register_func)(context);
printf("done register_func()\n");
  }
}

canonical_original_code:
void sxc_load(struct _SxcContext *context) {
  char *lib_name;
  int lib_name_len;
  struct _LoadedLib *lib;
printf("in sxc_load, lib_name:%p lib_name_len:%p\n", &lib_name, &lib_name_len);
  sxc_arg(context, 0, 1, sxc_cchars, &lib_name, &lib_name_len);
printf("done extract lib_name from args, name:%s, len:%d\n", lib_name, lib_name_len);
  lib = loaded_libs;
  while (lib != ((void *)0) && strncmp(lib->name, lib_name, lib_name_len) != 0) ;
printf("done find lib if it's already been loaded\n");
  if (lib == ((void *)0)) {
    lib = malloc(sizeof(LoadedLib) + lib_name_len + 1);
printf("done alloc new lib struct, name_len: %d\n", lib_name_len);
    lib->name = (char *)(lib + 1);
    memcpy(lib->name, lib_name, lib_name_len);
printf("done set new lib name\n");
    lib->name[lib_name_len] = '\0';
printf("done terminate new lib name\n");
    lib->register_func = get_register_func(context, lib_name, lib_name_len);
printf("get new lib register_func\n");
    lib->next = loaded_libs;
    loaded_libs = lib;
    (lib->register_func)(context);
printf("done register_func()\n");
  }
}

variable_types: struct _LoadedLib { char *name; void (*register_func)(struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue { struct _SxcContext *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context); struct _LoadedLib *next; } *

user_defined_types: struct _LoadedLib { char *name; void (*register_func)(struct _SxcContext *context); struct _LoadedLib *next; }


decompiled_code:
__int64 __fastcall map_iter(unsigned int a1, __int64 a2, __int64 a3, __int64 **a4)
{
__int64 v6; // rbp
unsigned int v7; // eax
const char *v8; // rax
__int64 v9; // r12
unsigned int v11; // er15
int v12; // eax

v6 = **a4;
v7 = lua_type(v6);
v8 = (const char *)lua_typename(v6, v7);
printf("in map_iter, mapindex: %d, state:%d, statetype:%s\n", a1, (unsigned int)a2, v8);
luaL_checkstack(v6, 4LL, &LC1);
if ( a2 )
{
if ( (int)lua_gettop(v6) > (int)a2 )
lua_pushvalue(v6, (unsigned int)a2);
}
else
{
lua_pushnil(v6);
}
v9 = 0LL;
if ( (unsigned int)lua_next(v6, a1) )
{
get_value(4294967294LL, a3);
v11 = lua_gettop(v6) - 1;
v12 = *(_DWORD *)(a3 + 8);
v9 = (int)v11;
if ( v12 == 2 )
{
--*(_DWORD *)(a3 + 16);
}
else if ( v12 != 7 )
{
sxc_value_set(a3, 0LL);
sxc_value_set(a4, 0LL);
lua_settop(v6, 4294967294LL);
printf("leaving map_iter nulled with new state:%d\n", v11);
return v9;
}
pop_value(a4);
printf("leaving map_iter with new state:%d\n", v11);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2, __int64 a3, __int64 **a4)
{
__int64 v6; // rbp
unsigned int v7; // eax
const char *v8; // rax
__int64 v9; // r12
unsigned int v11; // er15
int v12; // eax

v6 = **a4;
v7 = lua_type(v6);
v8 = (const char *)lua_typename(v6, v7);
printf("in map_iter, mapindex: %d, state:%d, statetype:%s\n", a1, (unsigned int)a2, v8);
luaL_checkstack(v6, 4LL, &LC1);
if ( a2 )
{
if ( (int)lua_gettop(v6) > (int)a2 )
lua_pushvalue(v6, (unsigned int)a2);
}
else
{
lua_pushnil(v6);
}
v9 = 0LL;
if ( (unsigned int)lua_next(v6, a1) )
{
get_value(4294967294LL, a3);
v11 = lua_gettop(v6) - 1;
v12 = *(_DWORD *)(a3 + 8);
v9 = (int)v11;
if ( v12 == 2 )
{
--*(_DWORD *)(a3 + 16);
}
else if ( v12 != 7 )
{
sxc_value_set(a3, 0LL);
sxc_value_set(a4, 0LL);
lua_settop(v6, 4294967294LL);
printf("leaving map_iter nulled with new state:%d\n", v11);
return v9;
}
pop_value(a4);
printf("leaving map_iter with new state:%d\n", v11);
}
return v9;
}

original_code:
static void* map_iter(SxcMap* map, void* state, SxcValue* return_key, SxcValue* return_value) {
  while ((state = (map->binding->iter)(map->underlying, state, return_key, return_value))) {
    switch (return_key->type) {
      case sxc_cint:
      case sxc_sstring:
        return state;
      case sxc_cdouble:
        if (return_key->data.cdouble == (double)(int)(return_key->data.cdouble)) {
          sxc_value_set(return_key, sxc_cint, (int)(return_key->data.cdouble));
          return state;
        }
      default:
        break;
    }
  }
  return ((void *)0);
}

canonical_original_code:
static void *map_iter(struct _SxcMap *map, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value) {
  while ((state = (map->binding->iter)(map->underlying, state, return_key, return_value))) {
    switch (return_key->type) {
      case sxc_cint:
      case sxc_sstring:
        return state;
      case sxc_cdouble:
        if (return_key->data.cdouble == (double)(int)(return_key->data.cdouble)) {
          sxc_value_set(return_key, sxc_cint, (int)(return_key->data.cdouble));
          return state;
        }
      default:
        break;
    }
  }
  return ((void *)0);
}

variable_types: struct _SxcMap { void *underlying; struct _SxcMapBinding { void (*intget)(void *underlying, int key, struct _SxcValue { struct _SxcContext { void *underlying; struct _SxcContextBinding { void (*get_arg)(void *underlying, int index, struct _SxcValue *return_value); void (*to_sstring)(char *data, int length, struct _SxcValue *return_value); void (*map_new)(void *map_type, struct _SxcValue *return_value); void *(*map_newtype)(struct _SxcContext *context, char *name, void (*initializer)(struct _SxcContext *context), struct _SxcLibMethod { char *name; int is_static; void (*func)(struct _SxcContext *context); } *methods, struct _SxcLibProperty { char *name; int is_static; void (*getter)(struct _SxcContext *context); void (*setter)(struct _SxcContext *context); } *properties); void (*to_sfunc)(void (*func)(struct _SxcContext *context), struct _SxcValue *return_value); } *binding; int argcount; struct _SxcValue return_value; int has_error; void *_jmpbuf; struct _SxcMemoryChunk { int free_space; struct _SxcMemoryChunk *next_chunk; int offset; char data; } _firstchunk; char _firstdata[-1]; } *context; enum _SxcDataType { sxc_null=0, sxc_cbool=1, sxc_cint=2, sxc_cdouble=3, sxc_string=4, sxc_map=5, sxc_func=6, sxc_sstring=7, sxc_smap=8, sxc_sfunc=9, sxc_cstring=10, sxc_cpointer=11, sxc_cfunc=12, sxc_cchars=13, sxc_cbools=14, sxc_cints=15, sxc_cdoubles=16, sxc_cstrings=17 } type; union _SxcData { _Bool cbool; int cint; double cdouble; void *_pointer_store; struct _SxcString { void *underlying; struct _SxcStringBinding { void (*to_cchars)(void *underlying, struct _SxcValue *return_value); int is_null_terminated; } *binding; struct _SxcContext *context; char *data; int length; } *string; struct _SxcMap *map; struct _SxcFunc { void *underlying; struct _SxcFunctionBinding { void (*invoke)(void *underlying, struct _SxcValue **args, int argcount, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *func; struct { void *underlying; void *binding; } _binding_store; struct { void *underlying; struct _SxcStringBinding *binding; } sstring; struct { void *underlying; struct _SxcMapBinding *binding; } smap; struct { void *underlying; struct _SxcFunctionBinding *binding; } sfunc; char *cstring; void (*cfunc)(struct _SxcContext *context); void *cpointer; struct { void *array; int length; } _array_store; struct { char *array; int length; } cchars; struct { _Bool *array; int length; } cbools; struct { int *array; int length; } cints; struct { double *array; int length; } cdoubles; struct { char **array; int length; } cstrings; } data; } *return_value); void (*intset)(void *underlying, int key, struct _SxcValue *value); void (*strget)(void *underlying, char *key, struct _SxcValue *return_value); void (*strset)(void *underlying, char *key, struct _SxcValue *value); int (*length)(void *underlying); void *(*iter)(void *underlying, void *state, struct _SxcValue *return_key, struct _SxcValue *return_value); } *binding; struct _SxcContext *context; } *

variable_types:
state
void *

user_defined_types: struct _SxcValue { struct _SxcContext *context; enum _SxcDataType type; union _SxcData data; }


mount

decompiled_code:
__int64 __fastcall mount(const char *a1, int a2)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 *v4; // rdx

v2 = 2LL * a2;
if ( drives[v2] )
{
printf("Cannot use drive %d. Already in use. Please dismount or select another drive\n", (unsigned int)drive);
result = 0LL;
}
else
{
if ( a2 > 7 )
return 0LL;
drives[2 * a2] = (__int64)fopen(a1, "r+b");
v4 = &drives[2 * drive];
if ( !*v4 )
{
printf("Unable to open file %s", a1);
return 0LL;
}
*((_WORD *)v4 + 4) = 0;
result = 1LL;
WORD1(drives[v2 + 1]) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, int a2)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 *v4; // rdx

v2 = 2LL * a2;
if ( drives[v2] )
{
printf("Cannot use drive %d. Already in use. Please dismount or select another drive\n", (unsigned int)drive);
result = 0LL;
}
else
{
if ( a2 > 7 )
return 0LL;
drives[2 * a2] = (__int64)fopen(a1, "r+b");
v4 = &drives[2 * drive];
if ( !*v4 )
{
printf("Unable to open file %s", a1);
return 0LL;
}
*((_WORD *)v4 + 4) = 0;
result = 1LL;
WORD1(drives[v2 + 1]) = 0;
}
return result;
}

original_code:
int mount(char *f, int driveNo) {
 if (drives[driveNo].file) {
  printf ("Cannot use drive %d. Already in use. Please dismount or select another drive\n", drive);
  return 0;
 }
 if (driveNo >= DRIVES)
  return 0;
 drives[driveNo].file = fopen(f, "r+b");
 if (!drives[drive].file) {
  printf("Unable to open file %s", f);
  return 0;
 }
 drives[drive].busy = 0;
 drives[driveNo].WPS = 0;
 return 1;
}

variable_types:
f
char *

variable_types: driveNo int


decompiled_code:
__int64 rk_read()
{
int v0; // eax
__int16 v1; // r13
int v2; // edx
__int64 result; // rax
__int64 v4; // r14
void *v5; // rsp
unsigned __int16 *v6; // rbx
__int64 v7; // rdi
unsigned __int16 v8; // si
__int64 v9; // rax
unsigned __int16 v10; // si
_WORD v11[16]; // [rsp+0h] [rbp-20h] BYREF

v0 = fseek((FILE *)drives[2 * drive], 2LL * position, 0);
v1 = 2048 - position;
v2 = v0;
result = (unsigned int)-(unsigned __int16)rkwc;
v4 = (unsigned int)(__int16)-rkwc;
if ( (int)v4 + position <= 1247231 )
v1 = -rkwc;
if ( v1 && !v2 )
{
v5 = alloca(2LL * (__int16)-rkwc);
v6 = v11;
result = fread(v11, 2uLL, v1, (FILE *)drives[2 * drive]);
if ( (int)v4 > (int)result )
result = (__int64)memset(&v11[(int)result], 0, 2LL * (unsigned int)(v4 - 1 - result) + 2);
if ( (rkcs & 0x400) != 0 )
{
v9 = v1 - 1;
v10 = v11[v9];
if ( BigEndian )
{
v10 = __ROL2__(v10, 8);
v11[v9] = v10;
}
result = setWord((unsigned int)ba, v10);
}
else
{
v7 = (unsigned int)ba;
if ( (int)v4 > 0 )
{
do
{
v8 = *v6;
if ( BigEndian )
{
v8 = __ROL2__(v8, 8);
*v6 = v8;
}
++v6;
setWord(v7, v8);
result = (unsigned int)ba;
v7 = (unsigned int)(ba + 2);
ba += 2;
}
while ( v6 != &v11[v4] );
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
__int16 v1; // r13
int v2; // edx
__int64 result; // rax
__int64 v4; // r14
void *v5; // rsp
unsigned __int16 *v6; // rbx
__int64 v7; // rdi
unsigned __int16 v8; // si
__int64 v9; // rax
unsigned __int16 v10; // si
_WORD v11[16]; // [rsp+0h] [rbp-20h] BYREF

v0 = fseek((FILE *)drives[2 * drive], 2LL * position, 0);
v1 = 2048 - position;
v2 = v0;
result = (unsigned int)-(unsigned __int16)rkwc;
v4 = (unsigned int)(__int16)-rkwc;
if ( (int)v4 + position <= 1247231 )
v1 = -rkwc;
if ( v1 && !v2 )
{
v5 = alloca(2LL * (__int16)-rkwc);
v6 = v11;
result = fread(v11, 2uLL, v1, (FILE *)drives[2 * drive]);
if ( (int)v4 > (int)result )
result = (__int64)memset(&v11[(int)result], 0, 2LL * (unsigned int)(v4 - 1 - result) + 2);
if ( (rkcs & 0x400) != 0 )
{
v9 = v1 - 1;
v10 = v11[v9];
if ( BigEndian )
{
v10 = __ROL2__(v10, 8);
v11[v9] = v10;
}
result = setWord((unsigned int)ba, v10);
}
else
{
v7 = (unsigned int)ba;
if ( (int)v4 > 0 )
{
do
{
v8 = *v6;
if ( BigEndian )
{
v8 = __ROL2__(v8, 8);
*v6 = v8;
}
++v6;
setWord(v7, v8);
result = (unsigned int)ba;
v7 = (unsigned int)(ba + 2);
ba += 2;
}
while ( v6 != &v11[v4] );
}
}
}
return result;
}

original_code:
void rk_read() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc, buffSize;
 wc = buffSize = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[buffSize];
  int read = fread (buffer, sizeof(unsigned short), wc, drives[drive].file);
  for ( ; read < buffSize; read++)
   buffer[read] = 0;
  if (rkcs.INH_BA) {
   if (BigEndian)
    buffer[wc-1] = ((buffer[wc-1] & 255) << 8) | ((buffer[wc-1]>>8) & 255);
   setWord(ba, buffer[wc-1]);
   return;
  }
  for (i = 0; i < buffSize; i++) {
   if (BigEndian)
    buffer[i] = ((buffer[i] & 255) << 8) | ((buffer[i]>>8) & 255);
   setWord(ba, buffer[i]);
   ba += 2;
  }
 }
}

canonical_original_code:
void rk_read() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc; short buffSize;
 wc = buffSize = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[-1];
  int read = fread (buffer, sizeof(unsigned short), wc, drives[drive].file);
  for ( ; read < buffSize; read++)
   buffer[read] = 0;
  if (rkcs.INH_BA) {
   if (BigEndian)
    buffer[wc-1] = ((buffer[wc-1] & 255) << 8) | ((buffer[wc-1]>>8) & 255);
   setWord(ba, buffer[wc-1]);
   return;
  }
  for (i = 0; i < buffSize; i++) {
   if (BigEndian)
    buffer[i] = ((buffer[i] & 255) << 8) | ((buffer[i]>>8) & 255);
   setWord(ba, buffer[i]);
   ba += 2;
  }
 }
}

variable_types: seek int

variable_types: wc short

variable_types: buffSize short

variable_types:
buffer
unsigned short [-1]

variable_types: read int


original_code:
void rk_write() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[wc];
  for (i = 0; i < wc; i++) {
   unsigned short word = getWord(ba);
   if (BigEndian) {
    word = ((word & 255) << 8) | ((word>>8) & 255);
   }
   buffer[i] = word;
   if (!rkcs.INH_BA) {
    ba += 2;
   }
  }
  int wl = fwrite (buffer, sizeof(unsigned short), wc, drives[drive].file);
  if (wl != wc) {
   return;
  }
  if ((wc % WORDS) != 0) {
   int need = WORDS - (wc % WORDS);
   unsigned short fill[WORDS] = { 0 };
   wl = fwrite (fill, sizeof(unsigned short), need, drives[drive].file);
  }
 }
}

canonical_original_code:
void rk_write() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[-1];
  for (i = 0; i < wc; i++) {
   unsigned short word = getWord(ba);
   if (BigEndian) {
    word = ((word & 255) << 8) | ((word>>8) & 255);
   }
   buffer[i] = word;
   if (!rkcs.INH_BA) {
    ba += 2;
   }
  }
  int wl = fwrite (buffer, sizeof(unsigned short), wc, drives[drive].file);
  if (wl != wc) {
   return;
  }
  if ((wc % WORDS) != 0) {
   int need = WORDS - (wc % WORDS);
   unsigned short fill[-1] = { 0 };
   wl = fwrite (fill, sizeof(unsigned short), need, drives[drive].file);
  }
 }
}

variable_types: i int

variable_types:
word
unsigned short

variable_types: wl int

variable_types: need int

variable_types:
fill
unsigned short [-1]