decompiled_code:
__int64 __fastcall make_timer(sigval_t a1)
{
timer_t v2; // rdi
__time_t v3; // rax
__int64 result; // rax
unsigned int v5; // er12
char *v6; // rax
unsigned int v7; // er12
char *v8; // rax
timer_t timerid; // [rsp+8h] [rbp-E0h] BYREF
struct itimerspec value; // [rsp+10h] [rbp-D8h] BYREF
struct itimerspec ovalue; // [rsp+30h] [rbp-B8h] BYREF
pthread_attr_t attr; // [rsp+50h] [rbp-98h] BYREF
struct sigevent evp; // [rsp+90h] [rbp-58h] BYREF

pthread_attr_init(&attr);
pthread_mutex_init((pthread_mutex_t *)((char *)a1.sival_ptr + 56), 0LL);
evp.sigev_notify = 2;
evp._sigev_un._sigev_thread._function = (void (*)(sigval_t))timer_handler;
evp.sigev_value = a1;
evp._sigev_un._sigev_thread._attribute = &attr;
if ( timer_create(0, &evp, &timerid) )
{
v5 = *_errno_location();
v6 = strerror(v5);
Log(3LL, "Error in timer creation, Err(%s)", v6);
result = v5;
}
else
{
v2 = timerid;
v3 = *((int *)a1.sival_ptr + 9);
value.it_value.tv_nsec = 0LL;
*((_QWORD *)a1.sival_ptr + 1) = timerid;
value.it_value.tv_sec = v3;
value.it_interval.tv_sec = v3;
value.it_interval.tv_nsec = 0LL;
if ( timer_settime(v2, 0, &value, &ovalue) )
{
v7 = *_errno_location();
v8 = strerror(v7);
Log(3LL, "Error in setting timer, Err(%s)", v8);
result = v7;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(sigval_t a1)
{
timer_t v2; // rdi
__time_t v3; // rax
__int64 result; // rax
unsigned int v5; // er12
char *v6; // rax
unsigned int v7; // er12
char *v8; // rax
timer_t timerid; // [rsp+8h] [rbp-E0h] BYREF
struct itimerspec value; // [rsp+10h] [rbp-D8h] BYREF
struct itimerspec ovalue; // [rsp+30h] [rbp-B8h] BYREF
pthread_attr_t attr; // [rsp+50h] [rbp-98h] BYREF
struct sigevent evp; // [rsp+90h] [rbp-58h] BYREF

pthread_attr_init(&attr);
pthread_mutex_init((pthread_mutex_t *)((char *)a1.sival_ptr + 56), 0LL);
evp.sigev_notify = 2;
evp._sigev_un._sigev_thread._function = (void (*)(sigval_t))timer_handler;
evp.sigev_value = a1;
evp._sigev_un._sigev_thread._attribute = &attr;
if ( timer_create(0, &evp, &timerid) )
{
v5 = *_errno_location();
v6 = strerror(v5);
Log(3LL, "Error in timer creation, Err(%s)", v6);
result = v5;
}
else
{
v2 = timerid;
v3 = *((int *)a1.sival_ptr + 9);
value.it_value.tv_nsec = 0LL;
*((_QWORD *)a1.sival_ptr + 1) = timerid;
value.it_value.tv_sec = v3;
value.it_interval.tv_sec = v3;
value.it_interval.tv_nsec = 0LL;
if ( timer_settime(v2, 0, &value, &ovalue) )
{
v7 = *_errno_location();
v8 = strerror(v7);
Log(3LL, "Error in setting timer, Err(%s)", v8);
result = v7;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int make_timer(struct single_connection * connection)
{
 pthread_attr_t attr;
 struct sigevent sig;
 struct itimerspec in, out;
 int status;
 pthread_attr_init( &attr );
 pthread_mutex_init(&(connection->timer.chk_mutex),((void *)0));
 sig.sigev_notify = SIGEV_THREAD;
 sig._sigev_un._sigev_thread._function = timer_handler;
 sig.sigev_value.sival_ptr=connection;
 sig._sigev_un._sigev_thread._attribute = &attr;
 timer_t timerid;
 status= timer_create(0, &sig, &timerid);
 if (status)
 {
  status=(*__errno_location ());
  Log (3,"Error in timer creation, Err(%s)",strerror(status));
  return status;
 }
 connection->timer.timerid=timerid;
 in.it_value.tv_sec = connection->timer.check_interval;
 in.it_value.tv_nsec = 0;
 in.it_interval.tv_sec = connection->timer.check_interval;
 in.it_interval.tv_nsec = 0;
 status = timer_settime(timerid, 0, &in, &out);
 if (status)
 {
  status=(*__errno_location ());
  Log (3,"Error in setting timer, Err(%s)",strerror(status));
  return status;
 }
 return 0;
}

canonical_original_code:
int make_timer(struct single_connection *connection)
{
 union pthread_attr_t attr;
 struct sigevent sig;
 struct itimerspec in; struct itimerspec out;
 int status;
 pthread_attr_init( &attr );
 pthread_mutex_init(&(connection->timer.chk_mutex),((void *)0));
 sig.sigev_notify = SIGEV_THREAD;
 sig._sigev_un._sigev_thread._function = timer_handler;
 sig.sigev_value.sival_ptr=connection;
 sig._sigev_un._sigev_thread._attribute = &attr;
 void *timerid;
 status= timer_create(0, &sig, &timerid);
 if (status)
 {
  status=(*__errno_location ());
  Log (3,"Error in timer creation, Err(%s)",strerror(status));
  return status;
 }
 connection->timer.timerid=timerid;
 in.it_value.tv_sec = connection->timer.check_interval;
 in.it_value.tv_nsec = 0;
 in.it_interval.tv_sec = connection->timer.check_interval;
 in.it_interval.tv_nsec = 0;
 status = timer_settime(timerid, 0, &in, &out);
 if (status)
 {
  status=(*__errno_location ());
  Log (3,"Error in setting timer, Err(%s)",strerror(status));
  return status;
 }
 return 0;
}

variable_types: struct sigevent { union sigval { int sival_int; void *sival_ptr; } sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t { char __size[56]; long __align; } *_attribute; } _sigev_thread; } _sigev_un; }

variable_types: struct itimerspec { struct timespec { long tv_sec; long tv_nsec; } it_interval; struct timespec it_value; }

variable_types: status int

variable_types:
timerid
void *

user_defined_types: struct sigevent { union sigval sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t *_attribute; } _sigev_thread; } _sigev_un; }

user_defined_types: struct itimerspec { struct timespec it_interval; struct timespec it_value; }


spawn

repo: cesello/psentinel

decompiled_code:
__int64 __fastcall spawn(char *file, char **argv)
{
unsigned int v2; // er12
int *v3; // rax
char *v4; // rax

v2 = fork();
if ( !v2 )
{
execvp(file, argv);
v3 = _errno_location();
v4 = strerror(*v3);
Log(3LL, "Error in execvp, Err(%s)\n", v4);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *file, char **argv)
{
unsigned int v2; // er12
int *v3; // rax
char *v4; // rax

v2 = fork();
if ( !v2 )
{
execvp(file, argv);
v3 = _errno_location();
v4 = strerror(*v3);
Log(3LL, "Error in execvp, Err(%s)\n", v4);
}
return v2;
}

original_code:
int spawn (char* program, char** arg_list)
{
 int status;
 pid_t child_pid;
 child_pid = fork ();
 if (child_pid != 0)
  return child_pid;
 else
 {
  execvp (program, arg_list);
  status=(*__errno_location ());
  Log (3,"Error in execvp, Err(%s)\n",strerror(status));
 }
 return 0;
}

canonical_original_code:
int spawn(char *program, char **arg_list)
{
 int status;
 int child_pid;
 child_pid = fork ();
 if (child_pid != 0)
  return child_pid;
 else
 {
  execvp (program, arg_list);
  status=(*__errno_location ());
  Log (3,"Error in execvp, Err(%s)\n",strerror(status));
 }
 return 0;
}

variable_types:
program
char *

return_type: int


decompiled_code:
__int64 __fastcall call_script(const char **a1)
{
unsigned int v1; // er12
__pid_t v2; // eax
const char *v4; // rdx
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = 0;
Log(7LL, "call_script: Executing %s\n", *a1);
v2 = spawn((char *)*a1, (char **)a1[1]);
if ( !v2 )
return v1;
waitpid(v2, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 )
{
HIWORD(v1) = HIWORD(stat_loc);
LOWORD(v1) = BYTE1(stat_loc);
Log(7LL, "Script %s exited normally with code %d\n", *a1, BYTE1(stat_loc));
return v1;
}
v4 = *a1;
if ( (char)((stat_loc & 0x7F) + 1) <= 1 )
Log(3LL, "Script %s exited abnormally\n", v4);
else
Log(7LL, "Script %s exited by TERM signal\n", v4);
return 255LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char **a1)
{
unsigned int v1; // er12
__pid_t v2; // eax
const char *v4; // rdx
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = 0;
Log(7LL, "call_script: Executing %s\n", *a1);
v2 = func0((char *)*a1, (char **)a1[1]);
if ( !v2 )
return v1;
waitpid(v2, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 )
{
HIWORD(v1) = HIWORD(stat_loc);
LOWORD(v1) = BYTE1(stat_loc);
Log(7LL, "Script %s exited normally with code %d\n", *a1, BYTE1(stat_loc));
return v1;
}
v4 = *a1;
if ( (char)((stat_loc & 0x7F) + 1) <= 1 )
Log(3LL, "Script %s exited abnormally\n", v4);
else
Log(7LL, "Script %s exited by TERM signal\n", v4);
return 255LL;
}

original_code:
unsigned short int call_script(sigval_t args)
{
 pid_t child_pid;
 int child_status;
 unsigned short int exit_status;
 shell_args_t * shell_args =(shell_args_t * ) args.sival_ptr;
 Log (7,"call_script: Executing %s\n",shell_args->script_name);
 child_pid = spawn(shell_args->script_name,shell_args->script_args);
 if (child_pid)
 {
  waitpid(child_pid,&child_status,0);
  if ((((child_status) & 0x7f) == 0))
  {
   exit_status=(((child_status) & 0xff00) >> 8);
   Log (7,"Script %s exited normally with code %d\n",shell_args->script_name,exit_status);
   return exit_status;
  }
  else
  if ((((signed char) (((child_status) & 0x7f) + 1) >> 1) > 0))
  {
   if (((child_status) & 0x7f))
   {
    Log (7,"Script %s exited by TERM signal\n",shell_args->script_name);
    return 255;
   }
   if ((((child_status) & 0xff) == 0x7f))
   {
    Log (7,"Script %s exited by STOP signal\n",shell_args->script_name);
    return 255;
   }
   if (((child_status) & 0x80))
   {
    Log (7,"Script %s exited by CORE DUMP signal\n",shell_args->script_name);
    return 255;
   }
  }
  else
  {
   Log (3,"Script %s exited abnormally\n",shell_args->script_name);
   return 255;
  }
 }
 return 0;
}

canonical_original_code:
unsigned short call_script(union sigval args)
{
 int child_pid;
 int child_status;
 unsigned short exit_status;
 struct shell_args *shell_args = (shell_args_t * ) args.sival_ptr;
 Log (7,"call_script: Executing %s\n",shell_args->script_name);
 child_pid = spawn(shell_args->script_name,shell_args->script_args);
 if (child_pid)
 {
  waitpid(child_pid,&child_status,0);
  if ((((child_status) & 0x7f) == 0))
  {
   exit_status=(((child_status) & 0xff00) >> 8);
   Log (7,"Script %s exited normally with code %d\n",shell_args->script_name,exit_status);
   return exit_status;
  }
  else
  if ((((char) (((child_status) & 0x7f) + 1) >> 1) > 0))
  {
   if (((child_status) & 0x7f))
   {
    Log (7,"Script %s exited by TERM signal\n",shell_args->script_name);
    return 255;
   }
   if ((((child_status) & 0xff) == 0x7f))
   {
    Log (7,"Script %s exited by STOP signal\n",shell_args->script_name);
    return 255;
   }
   if (((child_status) & 0x80))
   {
    Log (7,"Script %s exited by CORE DUMP signal\n",shell_args->script_name);
    return 255;
   }
  }
  else
  {
   Log (3,"Script %s exited abnormally\n",shell_args->script_name);
   return 255;
  }
 }
 return 0;
}

variable_types: union sigval { int sival_int; void *sival_ptr; }

variable_types: struct shell_args { char *script_name; char **script_args; } *

return_type: unsigned short


rstrip

decompiled_code:
const char *__fastcall rstrip(const char *a1)
{
size_t v1; // rax
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
__int64 v4; // rdx

v1 = strlen(a1);
v2 = &a1[v1];
if ( a1 < &a1[v1] )
{
v3 = _ctype_b_loc();
do
{
v4 = *--v2;
if ( ((*v3)[v4] & 0x2000) == 0 )
break;
*v2 = 0;
}
while ( a1 != v2 );
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func2(const char *a1)
{
size_t v1; // rax
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
__int64 v4; // rdx

v1 = strlen(a1);
v2 = &a1[v1];
if ( a1 < &a1[v1] )
{
v3 = _ctype_b_loc();
do
{
v4 = *--v2;
if ( ((*v3)[v4] & 0x2000) == 0 )
break;
*v2 = 0;
}
while ( a1 != v2 );
}
return a1;
}

original_code:
static char* rstrip(char* s)
{
    char* p = s + strlen(s);
    while (p > s && ((*__ctype_b_loc ())[(int) ((*--p))] & (unsigned short int) _ISspace))
        *p = '\0';
    return s;
}

canonical_original_code:
static char *rstrip(char *s)
{
    char *p = s + strlen(s);
    while (p > s && ((*__ctype_b_loc ())[(int) ((*--p))] & (unsigned short) _ISspace))
        *p = '\0';
    return s;
}

variable_types:
s
char *

variable_types:
p
char *

return_type: char *


repo: cezarsa/liji

decompiled_code:
int __fastcall test_find_many(char *s, __int64 a2, unsigned int a3, __int64 a4, unsigned int a5, unsigned int a6)
{
__int64 v6; // r15
__int64 v7; // r14
_DWORD *v9; // rbx
size_t v10; // rax
_DWORD *v11; // r8
__int64 v12; // rbx
char *v13; // r12
char *v14; // rax
size_t v15; // r13
const char *v16; // r14
size_t v17; // rax
char *v18; // rbp
size_t v19; // r12
char v24[40]; // [rsp+10h] [rbp-488h] BYREF
char *string; // [rsp+38h] [rbp-460h]
int v26; // [rsp+40h] [rbp-458h]

v6 = 0LL;
v7 = (int)a3;
v9 = malloc(4LL * (int)a3);
if ( (int)v7 > 0 )
{
do
{
v9[v6] = strlen(*(const char **)(a2 + 8 * v6));
++v6;
}
while ( v7 != v6 );
}
v10 = strlen(s);
v11 = v9;
v12 = 0LL;
liji_init(v24, s, v10, a2, v11, a3);
while ( (unsigned int)liji_find_multi_state(v24) )
{
v15 = v26;
v16 = *(const char **)(a4 + 8 * v12);
v17 = strlen(v16);
v18 = string;
v19 = v17;
if ( (_DWORD)v15 == (_DWORD)v17 && !memcmp(string, v16, v15) )
{
puts("Yay!");
}
else
{
has_error = 1;
v13 = strndup(v16, v19);
v14 = strndup(v18, v15);
printf("Nope: '%s' - Expected: '%s'. Line: %d\n", v14, v13, a6);
}
++v12;
}
if ( (_DWORD)v12 == a5 )
return puts("Yay!");
has_error = 1;
return printf("Nope. Expected %d to equal %d. Line: %d\n", (unsigned int)v12, a5, a6);
}

canonical_decompiled_code:
int __fastcall func0(char *s, __int64 a2, unsigned int a3, __int64 a4, unsigned int a5, unsigned int a6)
{
__int64 v6; // r15
__int64 v7; // r14
_DWORD *v9; // rbx
size_t v10; // rax
_DWORD *v11; // r8
__int64 v12; // rbx
char *v13; // r12
char *v14; // rax
size_t v15; // r13
const char *v16; // r14
size_t v17; // rax
char *v18; // rbp
size_t v19; // r12
char v24[40]; // [rsp+10h] [rbp-488h] BYREF
char *string; // [rsp+38h] [rbp-460h]
int v26; // [rsp+40h] [rbp-458h]

v6 = 0LL;
v7 = (int)a3;
v9 = malloc(4LL * (int)a3);
if ( (int)v7 > 0 )
{
do
{
v9[v6] = strlen(*(const char **)(a2 + 8 * v6));
++v6;
}
while ( v7 != v6 );
}
v10 = strlen(s);
v11 = v9;
v12 = 0LL;
liji_init(v24, s, v10, a2, v11, a3);
while ( (unsigned int)liji_find_multi_state(v24) )
{
v15 = v26;
v16 = *(const char **)(a4 + 8 * v12);
v17 = strlen(v16);
v18 = string;
v19 = v17;
if ( (_DWORD)v15 == (_DWORD)v17 && !memcmp(string, v16, v15) )
{
puts("Yay!");
}
else
{
has_error = 1;
v13 = strndup(v16, v19);
v14 = strndup(v18, v15);
printf("Nope: '%s' - Expected: '%s'. Line: %d\n", v14, v13, a6);
}
++v12;
}
if ( (_DWORD)v12 == a5 )
return puts("Yay!");
has_error = 1;
return printf("Nope. Expected %d to equal %d. Line: %d\n", (unsigned int)v12, a5, a6);
}

original_code:
void test_find_many(char *json, char *to_find[], int number_of_keys, char *expected[], int number_of_expected, int line) {
    int *sizes = (int*)malloc(sizeof(int) * number_of_keys);
    for (int i = 0; i < number_of_keys; ++i) {
        sizes[i] = strlen(to_find[i]);
    }
    liji_state state = liji_init(json, strlen(json), to_find, sizes, number_of_keys);
    int i = 0;
    while (liji_find_multi_state(&state)) {
        if ((int)state.response.len == (int)strlen(expected[i]) && memcmp(state.response.result_start, expected[i], state.response.len) == 0) { printf("Yay!\n"); } else { has_error = 1; printf("Nope: '%s' - Expected: '%s'. Line: %d\n", strndup(state.response.result_start, state.response.len), strndup(expected[i], strlen(expected[i])), line); };
        ++i;
    }
    if (i == number_of_expected) { printf("Yay!\n"); } else { has_error = 1; printf("Nope. Expected %d to equal %d. Line: %d\n", i, number_of_expected, line); };
}

canonical_original_code:
void test_find_many(char *json, char *to_find[], int number_of_keys, char *expected[], int number_of_expected, int line) {
    int *sizes = (int*)malloc(sizeof(int) * number_of_keys);
    for (int i = 0; i < number_of_keys; ++i) {
        sizes[i] = strlen(to_find[i]);
    }
    struct liji_state state = liji_init(json, strlen(json), to_find, sizes, number_of_keys);
    int i = 0;
    while (liji_find_multi_state(&state)) {
        if ((int)state.response.len == (int)strlen(expected[i]) && memcmp(state.response.result_start, expected[i], state.response.len) == 0) { printf("Yay!\n"); } else { has_error = 1; printf("Nope: '%s' - Expected: '%s'. Line: %d\n", strndup(state.response.result_start, state.response.len), strndup(expected[i], strlen(expected[i])), line); };
        ++i;
    }
    if (i == number_of_expected) { printf("Yay!\n"); } else { has_error = 1; printf("Nope. Expected %d to equal %d. Line: %d\n", i, number_of_expected, line); };
}

variable_types:
json
char *

variable_types:
expected
char *[]

variable_types: line int

variable_types:
sizes
int *

variable_types: i int

variable_types: struct liji_state { char *json_str; int json_str_len; char **wanted_keys; int *wanted_lens; int number_of_keys; struct liji_response { char *result_start; int len; } response; struct liji_parse_state { int level; char level_wanted_map[1024]; } parse_state; }

return_type: void

user_defined_types: struct liji_state { char *json_str; int json_str_len; char **wanted_keys; int *wanted_lens; int number_of_keys; struct liji_response response; struct liji_parse_state parse_state; }


decompiled_code:
int __fastcall test_simple_find(char *a1, __int64 a2, __int64 a3, unsigned int a4)
{
__int64 v5; // [rsp+0h] [rbp-18h] BYREF
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = a2;
v5 = a3;
return test_find_many(a1, (__int64)&v6, 1u, (__int64)&v5, 1u, a4);
}

canonical_decompiled_code:
int __fastcall func1(char *a1, __int64 a2, __int64 a3, unsigned int a4)
{
__int64 v5; // [rsp+0h] [rbp-18h] BYREF
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = a2;
v5 = a3;
return func0(a1, (__int64)&v6, 1u, (__int64)&v5, 1u, a4);
}

original_code:
void test_simple_find(char *json, char *to_find, char *expected, int line) {
    test_find_many(json, &to_find, 1, &expected, 1, line);
}

variable_types:
expected
char *


decompiled_code:
int __fastcall test_find_multi(char *a1, __int64 a2, unsigned int a3, __int64 a4, unsigned int a5)
{
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = a4;
return test_find_many(a1, a2, a3, (__int64)&v6, 1u, a5);
}

canonical_decompiled_code:
int __fastcall func2(char *a1, __int64 a2, unsigned int a3, __int64 a4, unsigned int a5)
{
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = a4;
return func0(a1, a2, a3, (__int64)&v6, 1u, a5);
}

original_code:
void test_find_multi(char *json, char *to_find[], int number_of_keys, char *expected, int line) {
    test_find_many(json, to_find, number_of_keys, &expected, 1, line);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // r12
char *v5; // r13
char *v6; // r14
const char *v8; // [rsp+8h] [rbp-180h] BYREF
const char *v9; // [rsp+10h] [rbp-178h] BYREF
const char *v10; // [rsp+18h] [rbp-170h] BYREF
const char *v11; // [rsp+20h] [rbp-168h] BYREF
const char *v12; // [rsp+28h] [rbp-160h] BYREF
__int64 v13[2]; // [rsp+30h] [rbp-158h] BYREF
__int64 v14[2]; // [rsp+40h] [rbp-148h] BYREF
__int64 v15[2]; // [rsp+50h] [rbp-138h] BYREF
__int64 v16[2]; // [rsp+60h] [rbp-128h] BYREF
__int64 v17[2]; // [rsp+70h] [rbp-118h] BYREF
__int64 v18[2]; // [rsp+80h] [rbp-108h] BYREF
__int64 v19[2]; // [rsp+90h] [rbp-F8h] BYREF
__int64 v20[2]; // [rsp+A0h] [rbp-E8h] BYREF
__int64 v21[4]; // [rsp+B0h] [rbp-D8h] BYREF
__int64 v22[4]; // [rsp+D0h] [rbp-B8h] BYREF
__int64 v23[4]; // [rsp+F0h] [rbp-98h] BYREF
__int64 v24[4]; // [rsp+110h] [rbp-78h] BYREF
__int64 v25[11]; // [rsp+130h] [rbp-58h] BYREF

v3 = (char *)read_json("fixtures/edge.json");
v4 = (char *)read_json("fixtures/repeated_keys.json");
v5 = (char *)read_json("fixtures/arrays.json");
v6 = (char *)read_json("fixtures/other.json");
read_json("fixtures/bigone.json");
test_simple_find(v3, (__int64)"yet_another", (__int64)"999", 0x45u);
test_simple_find(v3, (__int64)"test", (__int64)"value", 0x46u);
test_simple_find(v3, (__int64)"s]omething", (__int64)"aaa", 0x47u);
test_simple_find(v3, (__int64)"other{", (__int64)"{\"s]omething\": \"aaa\", \"bbbb\": 1}", 0x48u);
test_simple_find(v3, (__int64)"deep", (__int64)"{\"really\":{\"deep\":7}}", 0x49u);
test_simple_find(v3, (__int64)"really", (__int64)"{\"deep\":7}", 0x4Au);
test_simple_find(v3, (__int64)"with\\\"quoted", (__int64)"66", 0x4Bu);
test_simple_find(v3, (__int64)"after_quoted", (__int64)"ok", 0x4Cu);
test_simple_find(v3, (__int64)"value_quoted", (__int64)"quo\\\"ted", 0x4Du);
v13[0] = (__int64)"other{";
v13[1] = (__int64)"s]omething";
test_find_multi(v3, (__int64)v13, 2u, (__int64)"aaa", 0x4Eu);
v14[0] = (__int64)"other{";
v14[1] = (__int64)"bbbb";
test_find_multi(v3, (__int64)v14, 2u, (__int64)"1", 0x4Fu);
v21[0] = (__int64)"deep";
v21[1] = (__int64)"really";
v21[2] = (__int64)"deep";
test_find_multi(v3, (__int64)v21, 3u, (__int64)"7", 0x50u);
v15[0] = (__int64)"other{";
v15[1] = (__int64)"yet_another";
test_find_many(v4, (__int64)v15, 2u, 0LL, 0, 0x52u);
v16[0] = (__int64)"1";
v16[1] = (__int64)"2";
v8 = "a";
test_find_many(v4, (__int64)&v8, 1u, (__int64)v16, 2u, 0x53u);
v18[0] = (__int64)"9";
v18[1] = (__int64)"8";
v17[0] = (__int64)"d";
v17[1] = (__int64)"x";
test_find_many(v4, (__int64)v17, 2u, (__int64)v18, 2u, 0x54u);
v19[0] = (__int64)"9";
v19[1] = (__int64)"8";
v22[0] = (__int64)"d";
v22[1] = (__int64)"e";
v22[2] = (__int64)"x";
test_find_many(v4, (__int64)v22, 3u, (__int64)v19, 2u, 0x55u);
v10 = "{ \"c\": 3 }";
v9 = "c";
test_find_many(v4, (__int64)&v9, 1u, (__int64)&v10, 1u, 0x58u);
v23[0] = (__int64)"1";
v23[1] = (__int64)"2";
v23[2] = (__int64)"3";
v11 = "title";
test_find_many(v5, (__int64)&v11, 1u, (__int64)v23, 3u, 0x5Au);
v24[0] = (__int64)"1";
v24[1] = (__int64)"2";
v24[2] = (__int64)"3";
v20[0] = (__int64)"key";
v20[1] = (__int64)"title";
test_find_many(v5, (__int64)v20, 2u, (__int64)v24, 3u, 0x5Bu);
v25[0] = (__int64)&unk_364;
v25[1] = (__int64)&unk_364;
v25[2] = (__int64)&unk_379;
v25[3] = (__int64)&unk_38E;
v25[4] = (__int64)&unk_3A3;
v25[5] = (__int64)&unk_3B8;
v12 = "created_at";
test_find_many(v6, (__int64)&v12, 1u, (__int64)v25, 6u, 0x63u);
return has_error;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
json
char *[]


original_code:
int liji_find_multi_state(liji_state *state)
{
    liji_parse_state *parse_state = &(state->parse_state);
    if (parse_state->position >= state->json_str_len) {
        return 0;
    }
    char *current_pos = (char *)(state->json_str + parse_state->position), val;
    char *key_start = 0, *tmp_result = 0, *non_ws = 0;
    int result_level = 0,
        key_len = 0,
        tmp_level;
    char in_string = 0,
         in_key = 0,
         skipping = 1,
         escaped = 0;
    memset(&(state->response), 0, sizeof(liji_response));
    while (parse_state->position++ < state->json_str_len) {
        val = *(current_pos++);
        if (val == '\\' && !escaped) {
            escaped = 1;
        } else if(escaped) {
            escaped = (escaped + 1) % 3;
        }
        if (val != '"' && in_string) {
            continue;
        }
        if (tmp_result) {
            if (val == ' ' || val == '\n' || val == '\r' || val == '\t') {
                if (skipping) {
                    tmp_result++;
                    continue;
                }
                if (!non_ws) {
                    non_ws = current_pos - 1;
                }
            } else {
                skipping = 0;
            }
        }
        switch (val) {
            case '{':
                key_start = 0;
                key_len = 0;
            case '[':
                ++parse_state->level;
                if (__builtin_expect(!!(parse_state->level >= 1024), 0)) {
                    fprintf(stderr, "[LIJI ERROR] Too deeply nested json\n");
                    return 0;
                }
                break;
            case '}':
            case ']':
                tmp_level = parse_state->level--;
                if (parse_state->level_wanted_map[parse_state->level]) {
                    --parse_state->current_key;
                }
                if (tmp_level == result_level && tmp_result) { if (!non_ws) { non_ws = current_pos - 1; } int tmp_len = non_ws - tmp_result; if (*tmp_result == '"') { ++tmp_result; tmp_len -= 2; } state->response.result_start = tmp_result; state->response.len = tmp_len; return 1; } non_ws = ((void *)0);;
                break;
            case ',':
                key_start = 0;
                key_len = 0;
                if (parse_state->level == result_level && tmp_result) { if (!non_ws) { non_ws = current_pos - 1; } int tmp_len = non_ws - tmp_result; if (*tmp_result == '"') { ++tmp_result; tmp_len -= 2; } state->response.result_start = tmp_result; state->response.len = tmp_len; return 1; } non_ws = ((void *)0);;
                break;
            case ':':
                if (in_key && !tmp_result) {
                    tmp_result = current_pos;
                    result_level = parse_state->level;
                }
                break;
            case '"':
                if (escaped) {
                    break;
                }
                in_string = !in_string;
                if (!key_start) {
                    key_start = current_pos;
                } else if (!key_len) {
                    key_len = current_pos - 1 - key_start;
                    if (key_len == state->wanted_lens[parse_state->current_key] && memcmp(key_start, state->wanted_keys[parse_state->current_key], key_len) == 0) {
                        ++parse_state->current_key;
                        parse_state->level_wanted_map[parse_state->level] = 1;
                        if (parse_state->current_key == state->number_of_keys) {
                            in_key = 1;
                            --parse_state->current_key;
                        }
                    }
                }
                break;
        }
    }
    return 0;
}

canonical_original_code:
int liji_find_multi_state(struct liji_state *state)
{
    struct liji_parse_state *parse_state = &(state->parse_state);
    if (parse_state->position >= state->json_str_len) {
        return 0;
    }
    char *current_pos = (char *)(state->json_str + parse_state->position); char val;
    char *key_start = 0; char *tmp_result = 0; char *non_ws = 0;
    int result_level = 0; int key_len = 0; int tmp_level;
    char in_string = 0; char in_key = 0; char skipping = 1; char escaped = 0;
    memset(&(state->response), 0, sizeof(liji_response));
    while (parse_state->position++ < state->json_str_len) {
        val = *(current_pos++);
        if (val == '\\' && !escaped) {
            escaped = 1;
        } else if(escaped) {
            escaped = (escaped + 1) % 3;
        }
        if (val != '"' && in_string) {
            continue;
        }
        if (tmp_result) {
            if (val == ' ' || val == '\n' || val == '\r' || val == '\t') {
                if (skipping) {
                    tmp_result++;
                    continue;
                }
                if (!non_ws) {
                    non_ws = current_pos - 1;
                }
            } else {
                skipping = 0;
            }
        }
        switch (val) {
            case '{':
                key_start = 0;
                key_len = 0;
            case '[':
                ++parse_state->level;
                if (__builtin_expect(!!(parse_state->level >= 1024), 0)) {
                    fprintf(stderr, "[LIJI ERROR] Too deeply nested json\n");
                    return 0;
                }
                break;
            case '}':
            case ']':
                tmp_level = parse_state->level--;
                if (parse_state->level_wanted_map[parse_state->level]) {
                    --parse_state->current_key;
                }
                if (tmp_level == result_level && tmp_result) { if (!non_ws) { non_ws = current_pos - 1; } int tmp_len = non_ws - tmp_result; if (*tmp_result == '"') { ++tmp_result; tmp_len -= 2; } state->response.result_start = tmp_result; state->response.len = tmp_len; return 1; } non_ws = ((void *)0);;
                break;
            case ',':
                key_start = 0;
                key_len = 0;
                if (parse_state->level == result_level && tmp_result) { if (!non_ws) { non_ws = current_pos - 1; } int tmp_len = non_ws - tmp_result; if (*tmp_result == '"') { ++tmp_result; tmp_len -= 2; } state->response.result_start = tmp_result; state->response.len = tmp_len; return 1; } non_ws = ((void *)0);;
                break;
            case ':':
                if (in_key && !tmp_result) {
                    tmp_result = current_pos;
                    result_level = parse_state->level;
                }
                break;
            case '"':
                if (escaped) {
                    break;
                }
                in_string = !in_string;
                if (!key_start) {
                    key_start = current_pos;
                } else if (!key_len) {
                    key_len = current_pos - 1 - key_start;
                    if (key_len == state->wanted_lens[parse_state->current_key] && memcmp(key_start, state->wanted_keys[parse_state->current_key], key_len) == 0) {
                        ++parse_state->current_key;
                        parse_state->level_wanted_map[parse_state->level] = 1;
                        if (parse_state->current_key == state->number_of_keys) {
                            in_key = 1;
                            --parse_state->current_key;
                        }
                    }
                }
                break;
        }
    }
    return 0;
}

variable_types: struct liji_state { char *json_str; int json_str_len; char **wanted_keys; int *wanted_lens; int number_of_keys; struct liji_response { char *result_start; int len; } response; struct liji_parse_state { int level; char level_wanted_map[1024]; } parse_state; } *

variable_types: struct liji_parse_state { int level; char level_wanted_map[1024]; } *

variable_types: val char

variable_types: skipping char

variable_types: escaped char


decompiled_code:
__int64 __fastcall bubble_sort_up(_DWORD *a1, int a2)
{
return do_bubble_sort(a1, a2, 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, int a2)
{
return func0(a1, a2, 1);
}

original_code:
static int bubble_sort_up(int *buf, int len)
{
 return _do_bubble_sort(buf, len, 1);
}

variable_types:
buf
int *

variable_types: len int


decompiled_code:
__int64 __fastcall bubble_sort_down(_DWORD *a1, int a2)
{
return do_bubble_sort(a1, a2, 0);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, int a2)
{
return func0(a1, a2, 0);
}

original_code:
static int bubble_sort_down(int *buf, int len)
{
 return _do_bubble_sort(buf, len, 0);
}


decompiled_code:
const char *__fastcall sort_alg_search(char *s1)
{
const char *i; // r12

for ( i = (const char *)g_alg_list; i; i = *(const char **)i )
{
if ( !strncmp(s1, i + 8, 0x20uLL) )
break;
}
return i;
}

canonical_decompiled_code:
const char *__fastcall func3(char *s1)
{
const char *i; // r12

for ( i = (const char *)g_alg_list; i; i = *(const char **)i )
{
if ( !strncmp(s1, i + 8, 0x20uLL) )
break;
}
return i;
}

original_code:
static struct sort_alg *sort_alg_search(char *name)
{
 struct sort_alg *cur;
 cur = g_alg_list.next;
 while(cur != ((void *)0))
 {
  if (0 == strncmp(name, cur->sname, 32))
  {
   break;
  }
  cur = cur->next;
 }
 return cur;
}

variable_types:
name
char *

variable_types: struct sort_alg { struct sort_alg *next; char sname[32]; int ascend; int (*sort)(int *buf, int len); } *


original_code:
int do_quick_sort_up(int *buf, int low, int high)
{
 int tmp;
 int i,j;
 if (high > low){
  i = low;
  j = high;
  tmp = buf[low];
  while (i < j){
   while((i < j) && (buf[j] >= tmp))
    j--;
   if (i < j){
    buf[i] = buf[j];
    i++;
   }
   while((i < j) && (buf[i] < tmp))
    i++;
   if (i < j){
    buf[j] = buf[i];
    j--;
   }
  }
  buf[i] = tmp;
  do_quick_sort_up(buf, low, i - 1);
  do_quick_sort_up(buf, i + 1, high);
 }
}

canonical_original_code:
int do_quick_sort_up(int *buf, int low, int high)
{
 int tmp;
 int i; int j;
 if (high > low){
  i = low;
  j = high;
  tmp = buf[low];
  while (i < j){
   while((i < j) && (buf[j] >= tmp))
    j--;
   if (i < j){
    buf[i] = buf[j];
    i++;
   }
   while((i < j) && (buf[i] < tmp))
    i++;
   if (i < j){
    buf[j] = buf[i];
    j--;
   }
  }
  buf[i] = tmp;
  do_quick_sort_up(buf, low, i - 1);
  do_quick_sort_up(buf, i + 1, high);
 }
}

variable_types: low int

variable_types: high int

variable_types: tmp int

variable_types: j int


decompiled_code:
__int64 __fastcall quick_sort_up(__int64 a1, int a2)
{
return do_quick_sort_up(a1, 0, a2 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
return func5(a1, 0, a2 - 1);
}

original_code:
int quick_sort_up(int *buf, int len)
{
 do_quick_sort_up(buf, 0, len - 1);
}


original_code:
int do_quick_sort_down(int *buf, int low, int high)
{
 int tmp;
 int i,j;
 if (high > low){
  i = low;
  j = high;
  tmp = buf[low];
  while (i < j){
   while((i < j) && (buf[j] <= tmp))
    j--;
   if (i < j){
    buf[i] = buf[j];
    i++;
   }
   while((i < j) && (buf[i] > tmp))
    i++;
   if (i < j){
    buf[j] = buf[i];
    j--;
   }
  }
  buf[i] = tmp;
  do_quick_sort_down(buf, low, i - 1);
  do_quick_sort_down(buf, i + 1, high);
 }
}

canonical_original_code:
int do_quick_sort_down(int *buf, int low, int high)
{
 int tmp;
 int i; int j;
 if (high > low){
  i = low;
  j = high;
  tmp = buf[low];
  while (i < j){
   while((i < j) && (buf[j] <= tmp))
    j--;
   if (i < j){
    buf[i] = buf[j];
    i++;
   }
   while((i < j) && (buf[i] > tmp))
    i++;
   if (i < j){
    buf[j] = buf[i];
    j--;
   }
  }
  buf[i] = tmp;
  do_quick_sort_down(buf, low, i - 1);
  do_quick_sort_down(buf, i + 1, high);
 }
}

variable_types: i int


decompiled_code:
__int64 __fastcall quick_sort_down(__int64 a1, int a2)
{
return do_quick_sort_down(a1, 0, a2 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
return func7(a1, 0, a2 - 1);
}

original_code:
int quick_sort_down(int *buf, int len)
{
 do_quick_sort_down(buf, 0, len - 1);
}


decompiled_code:
const char *sort_init()
{
*((_QWORD *)&g_alg_list + 6) = 0LL;
g_alg_list = 0LL;
*(&g_alg_list + 1) = 0LL;
*(&g_alg_list + 2) = 0LL;
sort_alg_register_isra_0((__int64)&bsort_u);
sort_alg_register_isra_0((__int64)&bsort_d);
sort_alg_register_isra_0((__int64)&qsort_u);
return sort_alg_register_isra_0((__int64)&qsort_d);
}

canonical_decompiled_code:
const char *func9()
{
*((_QWORD *)&g_alg_list + 6) = 0LL;
g_alg_list = 0LL;
*(&g_alg_list + 1) = 0LL;
*(&g_alg_list + 2) = 0LL;
func4((__int64)&bsort_u);
func4((__int64)&bsort_d);
func4((__int64)&qsort_u);
return func4((__int64)&qsort_d);
}

original_code:
void sort_init(void)
{
 memset(&g_alg_list, 0, sizeof(g_alg_list));
 sort_alg_register(&bsort_u);
 sort_alg_register(&bsort_d);
 sort_alg_register(&qsort_u);
 sort_alg_register(&qsort_d);
}

canonical_original_code:
void sort_init()
{
 memset(&g_alg_list, 0, sizeof(g_alg_list));
 sort_alg_register(&bsort_u);
 sort_alg_register(&bsort_d);
 sort_alg_register(&qsort_u);
 sort_alg_register(&qsort_d);
}


decompiled_code:
__int64 __fastcall sort_is_ascend(char *a1)
{
const char *v1; // rax
unsigned int v2; // er8

v1 = sort_alg_search(a1);
v2 = -1;
if ( v1 )
v2 = *((_DWORD *)v1 + 10) != 0;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1)
{
const char *v1; // rax
unsigned int v2; // er8

v1 = func3(a1);
v2 = -1;
if ( v1 )
v2 = *((_DWORD *)v1 + 10) != 0;
return v2;
}

original_code:
int sort_is_ascend(char *name)
{
 int ret;
 struct sort_alg *alg;
 alg = sort_alg_search(name);
 if (alg != ((void *)0)){
  return (alg->ascend != 0);
 }
 return -1;
}

variable_types: ret int

user_defined_types: struct sort_alg { struct sort_alg *next; char sname[32]; int ascend; int (*sort)(int *buf, int len); }


decompiled_code:
__int64 __fastcall sort_exec(__int64 a1, unsigned int a2, char *a3)
{
const char *v3; // rax
__int64 result; // rax

if ( a3 != 0LL && (int)a2 > 0 && a1 && (v3 = sort_alg_search(a3)) != 0LL )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))v3 + 6))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned int a2, char *a3)
{
const char *v3; // rax
__int64 result; // rax

if ( a3 != 0LL && (int)a2 > 0 && a1 && (v3 = func3(a3)) != 0LL )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))v3 + 6))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int sort_exec(int *buf, int len, char *name)
{
 struct sort_alg *alg;
 if ((((void *)0) == buf) || (len < 1) || (((void *)0) == name))
  return -1;
 alg = sort_alg_search(name);
 if (((void *)0) == alg)
  return -1;
 return alg->sort(buf, len);
}


decompiled_code:
__int64 sort_alg_test()
{
__int64 v0; // rbx
char *v1; // rax
_DWORD *v2; // rbp
char *v3; // r12
unsigned int v4; // eax
__int64 v5; // rbx
int v6; // eax
__int64 result; // rax
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = 0;
while ( 1 )
{
puts("input data length(10 to 32766) :");
_isoc99_scanf("%d", v8);
if ( v8[0] - 10 <= 0x4FFE8 )
break;
puts("error input, try again.");
}
v0 = (int)v8[0];
v1 = (char *)malloc(8LL * (int)v8[0]);
v2 = v1;
if ( v1 )
{
v3 = &v1[4 * v0];
v4 = time(0LL);
srand(v4);
if ( (int)v8[0] > 0 )
{
v5 = 0LL;
do
{
v6 = rand() % 32766;
v2[v5] = v6;
*(_DWORD *)&v3[4 * v5++] = v6;
}
while ( (int)v8[0] > (int)v5 );
}
sort_init();
do_sort_test("bubble_sort_up", v3, v2, v8[0]);
do_sort_test("bubble_sort_down", v3, v2, v8[0]);
do_sort_test("quick_sort_up", v3, v2, v8[0]);
do_sort_test("quick_sort_down", v3, v2, v8[0]);
free(v2);
result = 0LL;
}
else
{
puts("memory alloc fail.");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rbx
char *v1; // rax
_DWORD *v2; // rbp
char *v3; // r12
unsigned int v4; // eax
__int64 v5; // rbx
int v6; // eax
__int64 result; // rax
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = 0;
while ( 1 )
{
puts("input data length(10 to 32766) :");
_isoc99_scanf("%d", v8);
if ( v8[0] - 10 <= 0x4FFE8 )
break;
puts("error input, try again.");
}
v0 = (int)v8[0];
v1 = (char *)malloc(8LL * (int)v8[0]);
v2 = v1;
if ( v1 )
{
v3 = &v1[4 * v0];
v4 = time(0LL);
srand(v4);
if ( (int)v8[0] > 0 )
{
v5 = 0LL;
do
{
v6 = rand() % 32766;
v2[v5] = v6;
*(_DWORD *)&v3[4 * v5++] = v6;
}
while ( (int)v8[0] > (int)v5 );
}
func9();
do_sort_test("bubble_sort_up", v3, v2, v8[0]);
do_sort_test("bubble_sort_down", v3, v2, v8[0]);
do_sort_test("quick_sort_up", v3, v2, v8[0]);
do_sort_test("quick_sort_down", v3, v2, v8[0]);
free(v2);
result = 0LL;
}
else
{
puts("memory alloc fail.");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int sort_alg_test(void)
{
 int len = 0;
 int *buf = ((void *)0);
 int *sbuf = ((void *)0);
 int ret = 0;
 int i = 0;
 while(1)
 {
  printf("input data length(10 to 32766) :\n");
  scanf("%d",&len);
  if((len < 327667) && (len > 9))
  {
   break;
  }
  printf("error input, try again.\n");
 }
 buf = (int*)malloc(sizeof(int) * len * 2);
 if(buf == ((void *)0))
 {
  printf("memory alloc fail.\n");
  return -1;
 }
 sbuf = buf + len;
 srand((int)time(((void *)0)));
 for(i=0; i<len; i++)
 {
  sbuf[i] = buf[i] = rand() % 32766;
 }
 sort_init();
 do_sort_test("bubble_sort_up", sbuf, buf, len);
 do_sort_test("bubble_sort_down", sbuf, buf, len);
 do_sort_test("quick_sort_up", sbuf, buf, len);
 do_sort_test("quick_sort_down", sbuf, buf, len);
free_buf:
 free(buf);
 return ret;
}

canonical_original_code:
int sort_alg_test()
{
 int len = 0;
 int *buf = ((void *)0);
 int *sbuf = ((void *)0);
 int ret = 0;
 int i = 0;
 while(1)
 {
  printf("input data length(10 to 32766) :\n");
  scanf("%d",&len);
  if((len < 327667) && (len > 9))
  {
   break;
  }
  printf("error input, try again.\n");
 }
 buf = (int *)malloc(sizeof(int) * len * 2);
 if(buf == ((void *)0))
 {
  printf("memory alloc fail.\n");
  return -1;
 }
 sbuf = buf + len;
 srand((int)time(((void *)0)));
 for(i=0; i<len; i++)
 {
  sbuf[i] = buf[i] = rand() % 32766;
 }
 sort_init();
 do_sort_test("bubble_sort_up", sbuf, buf, len);
 do_sort_test("bubble_sort_down", sbuf, buf, len);
 do_sort_test("quick_sort_up", sbuf, buf, len);
 do_sort_test("quick_sort_down", sbuf, buf, len);
free_buf:
 free(buf);
 return ret;
}

variable_types:
sbuf
int *


decompiled_code:
// attributes: thunk
int __cdecl main(int argc, const char **argv, const char **envp)
{
return sort_alg_test(argc, argv, envp);
}

original_code:
int main(int argc,char* argv[])
{
 return sort_alg_test();
}

canonical_original_code:
int main(int argc, char *argv[])
{
 return sort_alg_test();
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int start_time_record()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
{
start_sec = v1.tv_sec;
start_usec = v1.tv_usec;
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
{
start_sec = v1.tv_sec;
start_usec = v1.tv_usec;
}
return result;
}

original_code:
int start_time_record(void)
{
 struct timeval tv;
 int ret;
 ret = 0;
 ret = gettimeofday(&tv, ((void *)0));
 if (ret == 0)
 {
  start_sec = tv.tv_sec;
  start_usec = tv.tv_usec;
 }
 return ret;
}

canonical_original_code:
int start_time_record()
{
 struct timeval tv;
 int ret;
 ret = 0;
 ret = gettimeofday(&tv, ((void *)0));
 if (ret == 0)
 {
  start_sec = tv.tv_sec;
  start_usec = tv.tv_usec;
 }
 return ret;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int end_time_record()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
{
end_sec = v1.tv_sec;
end_usec = v1.tv_usec;
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
{
end_sec = v1.tv_sec;
end_usec = v1.tv_usec;
}
return result;
}

original_code:
int end_time_record(void)
{
 struct timeval tv;
 int ret;
 ret = 0;
 ret = gettimeofday(&tv, ((void *)0));
 if (ret == 0)
 {
  end_sec = tv.tv_sec;
  end_usec = tv.tv_usec;
 }
 return ret;
}

canonical_original_code:
int end_time_record()
{
 struct timeval tv;
 int ret;
 ret = 0;
 ret = gettimeofday(&tv, ((void *)0));
 if (ret == 0)
 {
  end_sec = tv.tv_sec;
  end_usec = tv.tv_usec;
 }
 return ret;
}

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int __fastcall show_time(int a1)
{
__int64 v2; // rsi
__int64 v3; // rdi

v2 = end_sec - start_sec;
v3 = end_usec - start_usec;
if ( a1 == 1 )
return printf("%ld msecond elapsed.\n", 1000 * v2 + v3 / 1000);
if ( a1 == 2 )
return printf("%ld second elapsed.\n", v3 / 1000000 + v2);
return printf("%ld usecond elapsed.\n", v3 + 1000000 * v2);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
__int64 v2; // rsi
__int64 v3; // rdi

v2 = end_sec - start_sec;
v3 = end_usec - start_usec;
if ( a1 == 1 )
return printf("%ld msecond elapsed.\n", 1000 * v2 + v3 / 1000);
if ( a1 == 2 )
return printf("%ld second elapsed.\n", v3 / 1000000 + v2);
return printf("%ld usecond elapsed.\n", v3 + 1000000 * v2);
}

original_code:
void show_time(int format)
{
 long time_spend;
 switch(format){
 case 0:
  time_spend = (end_sec - start_sec) * 1000000 + (end_usec - start_usec);
  printf("%ld usecond elapsed.\n",time_spend);
  break;
 case 1:
  time_spend = (end_sec - start_sec) * 1000 + (end_usec - start_usec) / 1000;
  printf("%ld msecond elapsed.\n",time_spend);
  break;
 case 2:
  time_spend = (end_sec - start_sec) + (end_usec - start_usec) / 1000000;
  printf("%ld second elapsed.\n",time_spend);
  break;
 default:
  time_spend = (end_sec - start_sec) * 1000000 + (end_usec - start_usec);
  printf("%ld usecond elapsed.\n",time_spend);
  break;
 }
}

variable_types: format int


repo: cfrost/osm

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rax
_QWORD *v4; // rbp
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
unsigned int v12; // eax
unsigned int v13; // eax
int v15; // [rsp+4h] [rbp-14h] BYREF
int v16; // [rsp+8h] [rbp-10h] BYREF
int v17[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = malloc(8uLL);
if ( !v3 )
out_of_memory(8LL, argv);
v4 = v3;
v15 = 1;
v16 = 2;
v17[0] = 3;
enqueue(v3, &v15);
v5 = length(*v4);
printf("Total length is = %d\n", v5);
enqueue(v4, &v16);
v6 = length(*v4);
printf("Total length is = %d\n", v6);
enqueue(v4, v17);
v7 = length(*v4);
printf("Total length is = %d\n", v7);
v8 = sum(*v4, const_one);
printf("Total sum of function const_one is = %d\n", v8);
v9 = sum(*v4, mult_one);
printf("Total sum of function mult_one is = %d\n", v9);
dequeue(v4);
v10 = length(*v4);
printf("Total length is = %d\n", v10);
dequeue(v4);
v11 = length(*v4);
printf("Total length is = %d\n", v11);
dequeue(v4);
v12 = length(*v4);
printf("Total length is = %d\n", v12);
dequeue(v4);
v13 = length(*v4);
printf("Total length is = %d\n", v13);
free(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rax
_QWORD *v4; // rbp
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
unsigned int v12; // eax
unsigned int v13; // eax
int v15; // [rsp+4h] [rbp-14h] BYREF
int v16; // [rsp+8h] [rbp-10h] BYREF
int v17[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = malloc(8uLL);
if ( !v3 )
func13(8LL, argv);
v4 = v3;
v15 = 1;
v16 = 2;
v17[0] = 3;
enqueue(v3, &v15);
v5 = func8(*v4);
printf("Total length is = %d\n", v5);
enqueue(v4, &v16);
v6 = func8(*v4);
printf("Total length is = %d\n", v6);
enqueue(v4, v17);
v7 = func8(*v4);
printf("Total length is = %d\n", v7);
v8 = func10(*v4, const_one);
printf("Total sum of function const_one is = %d\n", v8);
v9 = func10(*v4, mult_one);
printf("Total sum of function mult_one is = %d\n", v9);
func9(v4);
v10 = func8(*v4);
printf("Total length is = %d\n", v10);
func9(v4);
v11 = func8(*v4);
printf("Total length is = %d\n", v11);
func9(v4);
v12 = func8(*v4);
printf("Total length is = %d\n", v12);
func9(v4);
v13 = func8(*v4);
printf("Total length is = %d\n", v13);
free(v4);
return 0;
}

original_code:
int main(){
    Data a_data,b_data,c_data;
    QNode** queue;
    if((queue = (QNode**)malloc(sizeof(QNode**))) == ((void *)0)){
        out_of_memory();
    }
    int a = 1;
    int b = 2;
    int c = 3;
    a_data = &a;
    b_data = &b;
    c_data = &c;
    enqueue(queue,a_data);
    printf("Total length is = %d\n",length(*queue));
    enqueue(queue,b_data);
    printf("Total length is = %d\n",length(*queue));
    enqueue(queue,c_data);
    printf("Total length is = %d\n",length(*queue));
    printf("Total sum of function const_one is = %d\n",sum(*queue,&const_one));
    printf("Total sum of function mult_one is = %d\n",sum(*queue,&mult_one));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    free(queue);
}

canonical_original_code:
int main(){
    void *a_data; void *b_data; void *c_data;
    struct QNode_ **queue;
    if((queue = (struct QNode_ **)malloc(sizeof(struct QNode_ **))) == ((void *)0)){
        out_of_memory();
    }
    int a = 1;
    int b = 2;
    int c = 3;
    a_data = &a;
    b_data = &b;
    c_data = &c;
    enqueue(queue,a_data);
    printf("Total length is = %d\n",length(*queue));
    enqueue(queue,b_data);
    printf("Total length is = %d\n",length(*queue));
    enqueue(queue,c_data);
    printf("Total length is = %d\n",length(*queue));
    printf("Total sum of function const_one is = %d\n",sum(*queue,&const_one));
    printf("Total sum of function mult_one is = %d\n",sum(*queue,&mult_one));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    dequeue(queue);
    printf("Total length is = %d\n",length(*queue));
    free(queue);
}

variable_types: struct QNode_ { void *content; struct QNode_ *link; } **

variable_types: a int

variable_types: b int

variable_types: c int


length

decompiled_code:
__int64 __fastcall length(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // er12

v1 = a1;
v2 = 0;
if ( a1 )
{
do
{
v1 = *(_QWORD *)(v1 + 8);
++v2;
}
while ( a1 != v1 );
}
else
{
puts("Queue is empty.");
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // er12

v1 = a1;
v2 = 0;
if ( a1 )
{
do
{
v1 = *(_QWORD *)(v1 + 8);
++v2;
}
while ( a1 != v1 );
}
else
{
puts("Queue is empty.");
}
return v2;
}

original_code:
int length(QNode* queue) {
    int sum = 0;
    QNode* temp = queue;
    if (queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return 0;
    }
    do {
        sum++;
        temp = temp->link;
    } while (temp != queue);
    return sum;
}

canonical_original_code:
int length(struct QNode_ *queue) {
    int sum = 0;
    struct QNode_ *temp = queue;
    if (queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return 0;
    }
    do {
        sum++;
        temp = temp->link;
    } while (temp != queue);
    return sum;
}

variable_types: struct QNode_ { void *content; struct QNode_ *link; } *

variable_types: sum int


dequeue

original_code:
Data dequeue(QNode** queue) {
    QNode* temp;
    Data tempdata;
    if (*queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return ((void *)0);
    }
    temp = (*queue)->link;
    tempdata = temp->content;
    if (*queue == (*queue)->link) {
        *queue = ((void *)0);
    } else {
        (*queue)->link = temp->link;
    }
    free(temp);
    return tempdata;
}

canonical_original_code:
void *dequeue(struct QNode_ **queue) {
    struct QNode_ *temp;
    void *tempdata;
    if (*queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return ((void *)0);
    }
    temp = (*queue)->link;
    tempdata = temp->content;
    if (*queue == (*queue)->link) {
        *queue = ((void *)0);
    } else {
        (*queue)->link = temp->link;
    }
    free(temp);
    return tempdata;
}

variable_types:
tempdata
void *

return_type: void *

user_defined_types: struct QNode_ { void *content; struct QNode_ *link; }


sum

decompiled_code:
__int64 __fastcall sum(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD))
{
unsigned int v2; // er12
_QWORD *v3; // rbx
int v4; // eax

v2 = 0;
v3 = a1;
if ( a1 )
{
do
{
v4 = a2(*v3);
v3 = (_QWORD *)v3[1];
v2 += v4;
}
while ( a1 != v3 );
}
else
{
puts("Queue is empty.");
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD))
{
unsigned int v2; // er12
_QWORD *v3; // rbx
int v4; // eax

v2 = 0;
v3 = a1;
if ( a1 )
{
do
{
v4 = a2(*v3);
v3 = (_QWORD *)v3[1];
v2 += v4;
}
while ( a1 != v3 );
}
else
{
puts("Queue is empty.");
}
return v2;
}

original_code:
int sum(QNode* queue, int (*val)(Data)) {
    int sum = 0;
    QNode* temp = queue;
    if (queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return 0;
    }
    do {
        sum = sum + (*val)(temp->content);
        temp = temp->link;
    } while (temp != queue);
    return sum;
}

canonical_original_code:
int sum(struct QNode_ *queue, int (*val)(void *)) {
    int sum = 0;
    struct QNode_ *temp = queue;
    if (queue == ((void *)0)) {
        printf("Queue is empty.\n");
        return 0;
    }
    do {
        sum = sum + (*val)(temp->content);
        temp = temp->link;
    } while (temp != queue);
    return sum;
}

variable_types:
val
int (*)(void *)


decompiled_code:
__int64 const_one()
{
return 1LL;
}

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

original_code:
static int const_one(){
    return 1;
}

canonical_original_code:
static int const_one(){
    return 1;
}


decompiled_code:
__int64 __fastcall mult_one(_DWORD *a1)
{
return (unsigned int)(2 * *a1);
}

canonical_decompiled_code:
__int64 __fastcall func12(_DWORD *a1)
{
return (unsigned int)(2 * *a1);
}

original_code:
static int mult_one(Data d){
    return *(int*)d * 2;
}

canonical_original_code:
static int mult_one(void *d){
    return *(int *)d * 2;
}

variable_types:
d
void *


EmuInitialize

repo: cgutman/PIC-EMULATOR

decompiled_code:
__int64 __fastcall EmuInitialize(__int64 a1)
{
int v1; // eax
int v2; // er12
__int64 result; // rax
unsigned int v4; // er12

v1 = CpuInitializeCore();
if ( v1 < 0 )
{
v4 = v1;
puts("Failed to initialize the CPU's core state");
result = v4;
}
else
{
v2 = AsmInitializeContext(a1 + 2200);
if ( v2 < 0 )
{
puts("Failed to initialize the assembler context");
result = (unsigned int)v2;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax
int v2; // er12
__int64 result; // rax
unsigned int v4; // er12

v1 = CpuInitializeCore();
if ( v1 < 0 )
{
v4 = v1;
puts("Failed to initialize the CPU's core state");
result = v4;
}
else
{
v2 = AsmInitializeContext(a1 + 2200);
if ( v2 < 0 )
{
puts("Failed to initialize the assembler context");
result = (unsigned int)v2;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int EmuInitialize(EMU_STATE *State)
{
    int err;
    err = CpuInitializeCore(&State->Cpu);
    if (err < 0)
    {
        printf("Failed to initialize the CPU's core state\n");
        return err;
    }
    err = AsmInitializeContext(&State->AsmContext);
    if (err < 0)
    {
        printf("Failed to initialize the assembler context\n");
        return err;
    }
    return 0;
}

canonical_original_code:
int EmuInitialize(struct _EMU_STATE *State)
{
    int err;
    err = CpuInitializeCore(&State->Cpu);
    if (err < 0)
    {
        printf("Failed to initialize the CPU's core state\n");
        return err;
    }
    err = AsmInitializeContext(&State->AsmContext);
    if (err < 0)
    {
        printf("Failed to initialize the assembler context\n");
        return err;
    }
    return 0;
}

variable_types: struct _EMU_STATE { struct _PIC_CPU { struct _REGISTER_FILE { unsigned char INDF; unsigned char TMR0; unsigned char PCL; unsigned char STATUS; unsigned char FSR; unsigned char PORTA; unsigned char PORTB; unsigned char Unused1; unsigned char EEDATA; unsigned char EEADR; unsigned char PCLATH; unsigned char INTCON; unsigned char SRAM[68]; unsigned char Unused2[48]; unsigned char Unused3; unsigned char OPTION_REG; unsigned char Unused4[3]; unsigned char TRISA; unsigned char TRISB; unsigned char Unused5; unsigned char EECON1; unsigned char EECON2; } Regs; unsigned char W; struct _PIC_STACK { unsigned char Entries[8]; unsigned char NextTop; } Stack; struct _PIC_OPCODE { unsigned short Opcode; } ProgMem[1024]; } Cpu; struct _ASM_CONTEXT { struct _ASM_STRINT_TUPLE { int Value; char *String; } TupleTable[255]; int TupleCount; } AsmContext; } *

variable_types: err int

user_defined_types: struct _EMU_STATE { struct _PIC_CPU Cpu; struct _ASM_CONTEXT AsmContext; }

user_defined_types: struct _PIC_CPU { struct _REGISTER_FILE Regs; unsigned char W; struct _PIC_STACK Stack; struct _PIC_OPCODE ProgMem[1024]; }

user_defined_types: struct _ASM_CONTEXT { struct _ASM_STRINT_TUPLE TupleTable[255]; int TupleCount; }


EmuExecuteOpcode

decompiled_code:
// attributes: thunk
__int64 EmuExecuteOpcode()
{
return CpuExec();
}

canonical_decompiled_code:
__int64 func1()
{
return CpuExec();
}

original_code:
int EmuExecuteOpcode(EMU_STATE *State)
{
    return CpuExec(&State->Cpu);
}

canonical_original_code:
int EmuExecuteOpcode(struct _EMU_STATE *State)
{
    return CpuExec(&State->Cpu);
}

user_defined_types: struct _REGISTER_FILE { unsigned char INDF; unsigned char TMR0; unsigned char PCL; unsigned char STATUS; unsigned char FSR; unsigned char PORTA; unsigned char PORTB; unsigned char Unused1; unsigned char EEDATA; unsigned char EEADR; unsigned char PCLATH; unsigned char INTCON; unsigned char SRAM[68]; unsigned char Unused2[48]; unsigned char Unused3; unsigned char OPTION_REG; unsigned char Unused4[3]; unsigned char TRISA; unsigned char TRISB; unsigned char Unused5; unsigned char EECON1; unsigned char EECON2; }

user_defined_types: struct _PIC_STACK { unsigned char Entries[8]; unsigned char NextTop; }

user_defined_types: struct _PIC_OPCODE { unsigned short Opcode; }

user_defined_types: struct _ASM_STRINT_TUPLE { int Value; char *String; }


EmuAssembleAndExecute

decompiled_code:
__int64 __fastcall EmuAssembleAndExecute(__int64 a1, unsigned int a2)
{
int v2; // eax
_DWORD *v3; // rax
int v4; // er12
unsigned int v5; // er12
__int64 result; // rax
unsigned int v7; // er12
_BYTE v8[2200]; // [rsp+0h] [rbp-18A8h] BYREF
char v9[4112]; // [rsp+898h] [rbp-1010h] BYREF

v2 = EmuInitialize((__int64)v8);
if ( v2 < 0 )
{
v7 = v2;
puts("Failed to initialize emulator");
result = v7;
}
else
{
v3 = (_DWORD *)AsmAssembleAscii(v9, a1, a2);
if ( !v3 )
{
v5 = -1;
puts("Assembly failed");
return v5;
}
v4 = CpuInitializeProgramMemory(v8, v3 + 1, (unsigned int)(2 * *v3));
if ( v4 >= 0 )
{
while ( (int)CpuExec(v8) >= 0 )
;
return 0;
}
printf("Failed to initialize the CPU's program memory");
result = (unsigned int)v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
int v2; // eax
_DWORD *v3; // rax
int v4; // er12
unsigned int v5; // er12
__int64 result; // rax
unsigned int v7; // er12
_BYTE v8[2200]; // [rsp+0h] [rbp-18A8h] BYREF
char v9[4112]; // [rsp+898h] [rbp-1010h] BYREF

v2 = func0((__int64)v8);
if ( v2 < 0 )
{
v7 = v2;
puts("Failed to initialize emulator");
result = v7;
}
else
{
v3 = (_DWORD *)AsmAssembleAscii(v9, a1, a2);
if ( !v3 )
{
v5 = -1;
puts("Assembly failed");
return v5;
}
v4 = CpuInitializeProgramMemory(v8, v3 + 1, (unsigned int)(2 * *v3));
if ( v4 >= 0 )
{
while ( (int)CpuExec(v8) >= 0 )
;
return 0;
}
printf("Failed to initialize the CPU's program memory");
result = (unsigned int)v4;
}
return result;
}

original_code:
int EmuAssembleAndExecute(char *fbuffer, int size)
{
    ASM_PROGRAM *program;
    EMU_STATE state;
    int err;
    err = EmuInitialize(&state);
    if (err < 0)
    {
        printf("Failed to initialize emulator\n");
        return err;
    }
    program = AsmAssembleAscii(&state.AsmContext, fbuffer, size);
    if (!program)
    {
        printf("Assembly failed\n");
        return -1;
    }
    err = CpuInitializeProgramMemory(&state.Cpu, (unsigned char*)program->Opcodes, program->OpcodeCount * sizeof(PIC_OPCODE));
    if (err < 0)
    {
        printf("Failed to initialize the CPU's program memory");
        return err;
    }
    for (;;)
    {
        err = CpuExec(&state.Cpu);
        if (err < 0)
            break;
    }
    return 0;
}

canonical_original_code:
int EmuAssembleAndExecute(char *fbuffer, int size)
{
    struct _ASM_PROGRAM *program;
    struct _EMU_STATE state;
    int err;
    err = EmuInitialize(&state);
    if (err < 0)
    {
        printf("Failed to initialize emulator\n");
        return err;
    }
    program = AsmAssembleAscii(&state.AsmContext, fbuffer, size);
    if (!program)
    {
        printf("Assembly failed\n");
        return -1;
    }
    err = CpuInitializeProgramMemory(&state.Cpu, (unsigned char *)program->Opcodes, program->OpcodeCount * sizeof(PIC_OPCODE));
    if (err < 0)
    {
        printf("Failed to initialize the CPU's program memory");
        return err;
    }
    for (;;)
    {
        err = CpuExec(&state.Cpu);
        if (err < 0)
            break;
    }
    return 0;
}

variable_types:
fbuffer
char *

variable_types: size int

variable_types: struct _ASM_PROGRAM { int OpcodeCount; struct _PIC_OPCODE { unsigned short Opcode; } Opcodes[1]; } *

variable_types: struct _EMU_STATE { struct _PIC_CPU { struct _REGISTER_FILE { unsigned char INDF; unsigned char TMR0; unsigned char PCL; unsigned char STATUS; unsigned char FSR; unsigned char PORTA; unsigned char PORTB; unsigned char Unused1; unsigned char EEDATA; unsigned char EEADR; unsigned char PCLATH; unsigned char INTCON; unsigned char SRAM[68]; unsigned char Unused2[48]; unsigned char Unused3; unsigned char OPTION_REG; unsigned char Unused4[3]; unsigned char TRISA; unsigned char TRISB; unsigned char Unused5; unsigned char EECON1; unsigned char EECON2; } Regs; unsigned char W; struct _PIC_STACK { unsigned char Entries[8]; unsigned char NextTop; } Stack; struct _PIC_OPCODE { unsigned short Opcode; } ProgMem[1024]; } Cpu; struct _ASM_CONTEXT { struct _ASM_STRINT_TUPLE { int Value; char *String; } TupleTable[255]; int TupleCount; } AsmContext; }

user_defined_types: struct _ASM_PROGRAM { int OpcodeCount; struct _PIC_OPCODE Opcodes[1]; }


EmuExecuteBytecode

decompiled_code:
__int64 __fastcall EmuExecuteBytecode(__int64 a1, unsigned int a2)
{
int v2; // eax
int v3; // er12
__int64 result; // rax
unsigned int v5; // er12
_BYTE v6[6312]; // [rsp+0h] [rbp-18A8h] BYREF

v2 = EmuInitialize((__int64)v6);
if ( v2 < 0 )
{
v5 = v2;
puts("Failed to initialize emulator");
result = v5;
}
else
{
v3 = CpuInitializeProgramMemory(v6, a1, a2);
if ( v3 < 0 )
{
printf("Failed to initialize the CPU's program memory");
result = (unsigned int)v3;
}
else
{
while ( (int)CpuExec(v6) >= 0 )
;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2)
{
int v2; // eax
int v3; // er12
__int64 result; // rax
unsigned int v5; // er12
_BYTE v6[6312]; // [rsp+0h] [rbp-18A8h] BYREF

v2 = func0((__int64)v6);
if ( v2 < 0 )
{
v5 = v2;
puts("Failed to initialize emulator");
result = v5;
}
else
{
v3 = CpuInitializeProgramMemory(v6, a1, a2);
if ( v3 < 0 )
{
printf("Failed to initialize the CPU's program memory");
result = (unsigned int)v3;
}
else
{
while ( (int)CpuExec(v6) >= 0 )
;
result = 0LL;
}
}
return result;
}

original_code:
int EmuExecuteBytecode(unsigned char *Bytecode, int BytecodeLength)
{
    int err;
    EMU_STATE state;
    err = EmuInitialize(&state);
    if (err < 0)
    {
        printf("Failed to initialize emulator\n");
        return err;
    }
    err = CpuInitializeProgramMemory(&state.Cpu, Bytecode, BytecodeLength);
    if (err < 0)
    {
        printf("Failed to initialize the CPU's program memory");
        return err;
    }
    for (;;)
    {
        err = CpuExec(&state.Cpu);
        if (err < 0)
            break;
    }
    return 0;
}

canonical_original_code:
int EmuExecuteBytecode(unsigned char *Bytecode, int BytecodeLength)
{
    int err;
    struct _EMU_STATE state;
    err = EmuInitialize(&state);
    if (err < 0)
    {
        printf("Failed to initialize emulator\n");
        return err;
    }
    err = CpuInitializeProgramMemory(&state.Cpu, Bytecode, BytecodeLength);
    if (err < 0)
    {
        printf("Failed to initialize the CPU's program memory");
        return err;
    }
    for (;;)
    {
        err = CpuExec(&state.Cpu);
        if (err < 0)
            break;
    }
    return 0;
}

variable_types:
Bytecode
unsigned char *

variable_types: BytecodeLength int


StkInitialize

decompiled_code:
void __fastcall StkInitialize(__int64 a1)
{
*(_BYTE *)(a1 + 8) = 0;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
*(_BYTE *)(a1 + 8) = 0;
}

original_code:
void StkInitialize(PIC_STACK *Stack)
{
    Stack->NextTop = 0;
}

canonical_original_code:
void StkInitialize(struct _PIC_STACK *Stack)
{
    Stack->NextTop = 0;
}

variable_types: struct _PIC_STACK { unsigned char Entries[8]; unsigned char NextTop; } *


StkPush

decompiled_code:
__int64 __fastcall StkPush(__int64 a1, char a2)
{
unsigned __int8 v2; // al
__int64 result; // rax

v2 = *(_BYTE *)(a1 + 8);
*(_BYTE *)(a1 + v2) = a2;
result = (v2 + 1) & 7;
*(_BYTE *)(a1 + 8) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char a2)
{
unsigned __int8 v2; // al
__int64 result; // rax

v2 = *(_BYTE *)(a1 + 8);
*(_BYTE *)(a1 + v2) = a2;
result = (v2 + 1) & 7;
*(_BYTE *)(a1 + 8) = result;
return result;
}

original_code:
void StkPush(PIC_STACK *Stack, unsigned char Data)
{
    Stack->Entries[Stack->NextTop] = Data;
    Stack->NextTop = Stack->NextTop + 1;
    Stack->NextTop %= 8;
}

canonical_original_code:
void StkPush(struct _PIC_STACK *Stack, unsigned char Data)
{
    Stack->Entries[Stack->NextTop] = Data;
    Stack->NextTop = Stack->NextTop + 1;
    Stack->NextTop %= 8;
}

variable_types:
Data
unsigned char


StkPop

original_code:
unsigned char StkPop(PIC_STACK *Stack)
{
    Stack->NextTop = Stack->NextTop - 1;
    Stack->NextTop %= 8;
    return Stack->Entries[Stack->NextTop];
}

canonical_original_code:
unsigned char StkPop(struct _PIC_STACK *Stack)
{
    Stack->NextTop = Stack->NextTop - 1;
    Stack->NextTop %= 8;
    return Stack->Entries[Stack->NextTop];
}

return_type: unsigned char


CpuInitializeCore

decompiled_code:
__int64 __fastcall CpuInitializeCore(__int64 a1)
{
RegsInitializeRegisterFile();
StkInitialize(a1 + 139);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
RegsInitializeRegisterFile();
StkInitialize(a1 + 139);
return 0LL;
}

original_code:
int CpuInitializeCore(PIC_CPU *Cpu)
{
    RegsInitializeRegisterFile(&Cpu->Regs);
    StkInitialize(&Cpu->Stack);
    return 0;
}

canonical_original_code:
int CpuInitializeCore(struct _PIC_CPU *Cpu)
{
    RegsInitializeRegisterFile(&Cpu->Regs);
    StkInitialize(&Cpu->Stack);
    return 0;
}

variable_types: struct _PIC_CPU { struct _REGISTER_FILE { unsigned char INDF; unsigned char TMR0; unsigned char PCL; unsigned char STATUS; unsigned char FSR; unsigned char PORTA; unsigned char PORTB; unsigned char Unused1; unsigned char EEDATA; unsigned char EEADR; unsigned char PCLATH; unsigned char INTCON; unsigned char SRAM[68]; unsigned char Unused2[48]; unsigned char Unused3; unsigned char OPTION_REG; unsigned char Unused4[3]; unsigned char TRISA; unsigned char TRISB; unsigned char Unused5; unsigned char EECON1; unsigned char EECON2; } Regs; unsigned char W; struct _PIC_STACK { unsigned char Entries[8]; unsigned char NextTop; } Stack; struct _PIC_OPCODE { unsigned short Opcode; } ProgMem[1024]; } *


CpuInitializeProgramMemory

decompiled_code:
__int64 __fastcall CpuInitializeProgramMemory(__int64 a1, _QWORD *a2, int a3)
{
unsigned __int64 v3; // rcx
__int64 result; // rax
__int64 v5; // rdx
__int64 v6; // rdi

if ( a3 > 1792 )
{
puts("Program is too large for the PIC");
result = 0xFFFFFFFFLL;
}
else
{
v3 = a3;
result = a3 & 1;
if ( (a3 & 1) != 0 )
{
puts("Program is not valid PIC bytecode");
result = 0xFFFFFFFFLL;
}
else
{
v5 = a1 + 148;
if ( v3 >= 8 )
{
v6 = a1 + 156;
*(_QWORD *)(v6 - 8) = *a2;
*(_QWORD *)(v5 + v3 - 8) = *(_QWORD *)((char *)a2 + v3 - 8);
qmemcpy(
(void *)(v6 & 0xFFFFFFFFFFFFFFF8LL),
(char *)a2 - (v5 - (v6 & 0xFFFFFFFFFFFFFFF8LL)),
8 * ((v5 - (v6 & 0xFFFFFFFFFFFFFFF8LL) + v3) >> 3));
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)(a1 + 148) = *(_DWORD *)a2;
*(_DWORD *)(v5 + v3 - 4) = *(_DWORD *)((char *)a2 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)(a1 + 148) = *(_BYTE *)a2;
if ( (v3 & 2) != 0 )
*(_WORD *)(v5 + v3 - 2) = *(_WORD *)((char *)a2 + v3 - 2);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, _QWORD *a2, int a3)
{
unsigned __int64 v3; // rcx
__int64 result; // rax
__int64 v5; // rdx
__int64 v6; // rdi

if ( a3 > 1792 )
{
puts("Program is too large for the PIC");
result = 0xFFFFFFFFLL;
}
else
{
v3 = a3;
result = a3 & 1;
if ( (a3 & 1) != 0 )
{
puts("Program is not valid PIC bytecode");
result = 0xFFFFFFFFLL;
}
else
{
v5 = a1 + 148;
if ( v3 >= 8 )
{
v6 = a1 + 156;
*(_QWORD *)(v6 - 8) = *a2;
*(_QWORD *)(v5 + v3 - 8) = *(_QWORD *)((char *)a2 + v3 - 8);
qmemcpy(
(void *)(v6 & 0xFFFFFFFFFFFFFFF8LL),
(char *)a2 - (v5 - (v6 & 0xFFFFFFFFFFFFFFF8LL)),
8 * ((v5 - (v6 & 0xFFFFFFFFFFFFFFF8LL) + v3) >> 3));
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)(a1 + 148) = *(_DWORD *)a2;
*(_DWORD *)(v5 + v3 - 4) = *(_DWORD *)((char *)a2 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)(a1 + 148) = *(_BYTE *)a2;
if ( (v3 & 2) != 0 )
*(_WORD *)(v5 + v3 - 2) = *(_WORD *)((char *)a2 + v3 - 2);
}
}
}
return result;
}

original_code:
int CpuInitializeProgramMemory(PIC_CPU *Cpu, unsigned char *buffer, int size)
{
    if (size > ((0x400 * 0xE) / 0x08))
    {
        printf("Program is too large for the PIC\n");
        return -1;
    }
    if ((size % sizeof(PIC_OPCODE)) != 0)
    {
        printf("Program is not valid PIC bytecode\n");
        return -1;
    }
    memcpy(Cpu->ProgMem, buffer, size);
    return 0;
}

canonical_original_code:
int CpuInitializeProgramMemory(struct _PIC_CPU *Cpu, unsigned char *buffer, int size)
{
    if (size > ((0x400 * 0xE) / 0x08))
    {
        printf("Program is too large for the PIC\n");
        return -1;
    }
    if ((size % sizeof(PIC_OPCODE)) != 0)
    {
        printf("Program is not valid PIC bytecode\n");
        return -1;
    }
    memcpy(Cpu->ProgMem, buffer, size);
    return 0;
}

variable_types:
buffer
unsigned char *


CpuGetPC

decompiled_code:
__int16 __fastcall CpuGetPC(__int64 a1)
{
return ((*(unsigned __int8 *)(a1 + 10) << 8) | *(unsigned __int8 *)(a1 + 2)) & 0x3FF;
}

original_code:
unsigned short CpuGetPC(PIC_CPU *Cpu)
{
    return ((Cpu->Regs.PCLATH << 8) | Cpu->Regs.PCL) & (0x400 - 1);
}

canonical_original_code:
unsigned short CpuGetPC(struct _PIC_CPU *Cpu)
{
    return ((Cpu->Regs.PCLATH << 8) | Cpu->Regs.PCL) & (0x400 - 1);
}


CpuSetPC

decompiled_code:
void __fastcall CpuSetPC(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 10) = 0;
*(_BYTE *)(a1 + 2) = a2;
}

original_code:
void CpuSetPC(PIC_CPU *Cpu, unsigned short PC)
{
    Cpu->Regs.PCLATH = (PC << 8) & 0x1F;
    Cpu->Regs.PCL = (PC & 0xFF);
}

canonical_original_code:
void CpuSetPC(struct _PIC_CPU *Cpu, unsigned short PC)
{
    Cpu->Regs.PCLATH = (PC << 8) & 0x1F;
    Cpu->Regs.PCL = (PC & 0xFF);
}

variable_types:
PC
unsigned short


CpuGetOpcode

decompiled_code:
__int16 __fastcall CpuGetOpcode(__int64 a1, unsigned __int16 a2)
{
return *(_WORD *)(a1 + 2LL * a2 + 148) & 0x3FFF;
}

canonical_decompiled_code:
__int16 __fastcall func4(__int64 a1, unsigned __int16 a2)
{
return *(_WORD *)(a1 + 2LL * a2 + 148) & 0x3FFF;
}

original_code:
unsigned short CpuGetOpcode(PIC_CPU *Cpu, unsigned short PC)
{
    return (Cpu->ProgMem[PC].Opcode) & 0x3FFF;
}

canonical_original_code:
unsigned short CpuGetOpcode(struct _PIC_CPU *Cpu, unsigned short PC)
{
    return (Cpu->ProgMem[PC].Opcode) & 0x3FFF;
}


CpuSetOpcode

decompiled_code:
__int16 __fastcall CpuSetOpcode(__int64 a1, unsigned __int16 a2, __int16 a3)
{
__int16 result; // ax

result = a3 & 0x3FFF;
*(_WORD *)(a1 + 2 * (a2 + 72LL) + 4) = a3 & 0x3FFF | *(_WORD *)(a1 + 2 * (a2 + 72LL) + 4) & 0xC000;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func5(__int64 a1, unsigned __int16 a2, __int16 a3)
{
__int16 result; // ax

result = a3 & 0x3FFF;
*(_WORD *)(a1 + 2 * (a2 + 72LL) + 4) = a3 & 0x3FFF | *(_WORD *)(a1 + 2 * (a2 + 72LL) + 4) & 0xC000;
return result;
}

original_code:
void CpuSetOpcode(PIC_CPU *Cpu, unsigned short PC, unsigned short Opcode)
{
    Cpu->ProgMem[PC].Opcode = Opcode & 0x3FFF;
}

canonical_original_code:
void CpuSetOpcode(struct _PIC_CPU *Cpu, unsigned short PC, unsigned short Opcode)
{
    Cpu->ProgMem[PC].Opcode = Opcode & 0x3FFF;
}

variable_types:
Opcode
unsigned short