decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r12
char *v4; // rbp
void *ptr; // [rsp+0h] [rbp-28h] BYREF
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

puts(
"\n"
"\n"
"\n"
"Welcome to task!\n"
"Here are your commands:\n"
"n(ew) - Begin a new task\n"
"a(dd) - add a note to the current task\n"
"f(inish) - finish the current task\n"
"s(how) - show current task\n"
"p(rint) - print tasks\n"
"c(hange) - change tasks\n"
"l(oad) - load a task file\n"
"w(rite) - save a task file\n"
"q(uit) - quit\n");
ptr = 0LL;
v7[0] = 0LL;
v3 = List(10LL, 0LL);
get_user_input_msg(&ptr, "\nWhat would you like to do? ");
while ( 1 )
{
v4 = (char *)ptr;
if ( ptr )
{
if ( *(_BYTE *)ptr )
break;
}
LABEL_3:
get_user_input_msg(&ptr, "\nWhat would you like to do? ");
}
if ( !(unsigned int)do_quit((char *)ptr) )
{
execute_command(v4, v3, (__int64)v7);
goto LABEL_3;
}
if ( *(int *)(v3 + 16) > 0 )
{
get_user_input_msg(&ptr, "\nSave your tasks? (y)/n: ");
v4 = (char *)ptr;
if ( !*(_BYTE *)ptr || strcmp((const char *)ptr, "n") && strcmp(v4, "N") )
{
execute_save(v4, v3, v7);
v4 = (char *)ptr;
}
}
free(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r12
char *v4; // rbp
void *ptr; // [rsp+0h] [rbp-28h] BYREF
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

puts(
"\n"
"\n"
"\n"
"Welcome to task!\n"
"Here are your commands:\n"
"n(ew) - Begin a new task\n"
"a(dd) - add a note to the current task\n"
"f(inish) - finish the current task\n"
"s(how) - show current task\n"
"p(rint) - print tasks\n"
"c(hange) - change tasks\n"
"l(oad) - load a task file\n"
"w(rite) - save a task file\n"
"q(uit) - quit\n");
ptr = 0LL;
v7[0] = 0LL;
v3 = List(10LL, 0LL);
get_user_input_msg(&ptr, "\nWhat would you like to do? ");
while ( 1 )
{
v4 = (char *)ptr;
if ( ptr )
{
if ( *(_BYTE *)ptr )
break;
}
LABEL_3:
get_user_input_msg(&ptr, "\nWhat would you like to do? ");
}
if ( !(unsigned int)func2((char *)ptr) )
{
func1(v4, v3, (__int64)v7);
goto LABEL_3;
}
if ( *(int *)(v3 + 16) > 0 )
{
get_user_input_msg(&ptr, "\nSave your tasks? (y)/n: ");
v4 = (char *)ptr;
if ( !*(_BYTE *)ptr || strcmp((const char *)ptr, "n") && strcmp(v4, "N") )
{
execute_save(v4, v3, v7);
v4 = (char *)ptr;
}
}
free(v4);
return 0;
}

original_code:
int main() {
    puts(kWelcomeMessage);
    char * command;
    ListT* tasks;
    TaskT* current;
    tasks = List(10, ((void *)0));
    command = ((void *)0);
    current = ((void *)0);
    get_user_input_msg(&command, "\nWhat would you like to do? ");
    while (command == ((void *)0) || strlen(command) == 0 || !do_quit(command)) {
        if (command != ((void *)0) && strlen(command) > 0) {
            execute_command(command, tasks, &current);
        }
        get_user_input_msg(&command, "\nWhat would you like to do? ");
    }
    if (tasks->size > 0) {
        get_user_input_msg(&command, "\nSave your tasks? (y)/n: ");
        if (strlen(command) == 0 ||
            (strcmp(command, "n") != 0 && strcmp(command, "N") != 0)) {
            execute_save(command, tasks, &current);
        }
    }
    free(command);
}

canonical_original_code:
int main() {
    puts(kWelcomeMessage);
    char *command;
    struct ListT *tasks;
    struct TaskT *current;
    tasks = List(10, ((void *)0));
    command = ((void *)0);
    current = ((void *)0);
    get_user_input_msg(&command, "\nWhat would you like to do? ");
    while (command == ((void *)0) || strlen(command) == 0 || !do_quit(command)) {
        if (command != ((void *)0) && strlen(command) > 0) {
            execute_command(command, tasks, &current);
        }
        get_user_input_msg(&command, "\nWhat would you like to do? ");
    }
    if (tasks->size > 0) {
        get_user_input_msg(&command, "\nSave your tasks? (y)/n: ");
        if (strlen(command) == 0 ||
            (strcmp(command, "n") != 0 && strcmp(command, "N") != 0)) {
            execute_save(command, tasks, &current);
        }
    }
    free(command);
}


repo: pjherring/task

decompiled_code:
char *__fastcall get_text_from_command(char *s)
{
unsigned int v1; // ebx
size_t v2; // r12
char *v3; // rax

v1 = (unsigned int)strchr(s, 32) - (_DWORD)s;
v2 = (int)(strlen(s) - v1);
v3 = (char *)malloc(v2);
return strncpy(v3, &s[v1 + 1], v2);
}

canonical_decompiled_code:
char *__fastcall func0(char *s)
{
unsigned int v1; // ebx
size_t v2; // r12
char *v3; // rax

v1 = (unsigned int)strchr(s, 32) - (_DWORD)s;
v2 = (int)(strlen(s) - v1);
v3 = (char *)malloc(v2);
return strncpy(v3, &s[v1 + 1], v2);
}

original_code:
static char* get_text_from_command(char* command) {
    int first_space_idx, command_strlen;
    char* text;;
    first_space_idx = strstr(command, " ") - command;
    command_strlen = strlen(command);
    text = malloc(sizeof(char) * (command_strlen - first_space_idx));
    strncpy(text, (command + first_space_idx + 1), (command_strlen - first_space_idx));
    return text;
}

canonical_original_code:
static char *get_text_from_command(char *command) {
    int first_space_idx; int command_strlen;
    char *text;;
    first_space_idx = strstr(command, " ") - command;
    command_strlen = strlen(command);
    text = malloc(sizeof(char) * (command_strlen - first_space_idx));
    strncpy(text, (command + first_space_idx + 1), (command_strlen - first_space_idx));
    return text;
}

variable_types:
command
char *

variable_types:
text
char *

return_type: char *


decompiled_code:
_BOOL8 __fastcall is_command_only(__int64 a1)
{
return (unsigned int)stroccur(a1, &unk_902) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1)
{
return (unsigned int)stroccur(a1, &unk_902) == 0;
}

original_code:
static int is_command_only(char* command) {
    return stroccur(command, " ") == 0;
}

canonical_original_code:
static int is_command_only(char *command) {
    return stroccur(command, " ") == 0;
}

return_type: int


decompiled_code:
__int64 __fastcall is_loadable(unsigned __int8 *a1)
{
__int64 result; // rax
int v2; // ecx
int v3; // edx

if ( *a1 != 46 || (result = (unsigned int)-a1[1], a1[1]) )
{
v2 = *a1;
v3 = 46 - v2;
if ( v2 != 46 || (v3 = 46 - a1[1]) != 0 )
result = v3 != 0;
else
result = a1[2] != 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1)
{
__int64 result; // rax
int v2; // ecx
int v3; // edx

if ( *a1 != 46 || (result = (unsigned int)-a1[1], a1[1]) )
{
v2 = *a1;
v3 = 46 - v2;
if ( v2 != 46 || (v3 = 46 - a1[1]) != 0 )
result = v3 != 0;
else
result = a1[2] != 0;
}
return result;
}

original_code:
int is_loadable(char* filename) {
    return strcmp(".", filename) != 0 && strcmp("..", filename) != 0;
}

canonical_original_code:
int is_loadable(char *filename) {
    return strcmp(".", filename) != 0 && strcmp("..", filename) != 0;
}

variable_types:
filename
char *


decompiled_code:
char *make_file_name()
{
struct tm *v0; // r12
char *v1; // rbp
size_t v2; // rax
char *v3; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
v1 = (char *)malloc(0x14uLL);
strftime(v1, 0x14uLL, "%Y_%m_%d", v0);
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 21);
sprintf(v3, "tasks/task-%s.js", v1);
free(v1);
return v3;
}

canonical_decompiled_code:
char *func3()
{
struct tm *v0; // r12
char *v1; // rbp
size_t v2; // rax
char *v3; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
v1 = (char *)malloc(0x14uLL);
strftime(v1, 0x14uLL, "%Y_%m_%d", v0);
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 21);
sprintf(v3, "tasks/task-%s.js", v1);
free(v1);
return v3;
}

original_code:
static char* make_file_name() {
    time_t current_time;
    struct tm* local_time;
    char* time_str;
    char* filename;
    current_time = time(((void *)0));
    local_time = localtime(&current_time);
    time_str = malloc(kFileTimeFormatSize);
    strftime(time_str, kFileTimeFormatSize, kFileTimeFormat, local_time);
    filename = malloc(kFileTimeFormatSize + strlen(time_str) + 1);
    sprintf(filename, kFileNameFormat, time_str);
    free(time_str);
    return filename;
}

canonical_original_code:
static char *make_file_name() {
    long current_time;
    struct tm *local_time;
    char *time_str;
    char *filename;
    current_time = time(((void *)0));
    local_time = localtime(&current_time);
    time_str = malloc(kFileTimeFormatSize);
    strftime(time_str, kFileTimeFormatSize, kFileTimeFormat, local_time);
    filename = malloc(kFileTimeFormatSize + strlen(time_str) + 1);
    sprintf(filename, kFileNameFormat, time_str);
    free(time_str);
    return filename;
}

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


decompiled_code:
__int64 __fastcall execute_new(char *s, __int64 a2, __int64 *a3)
{
unsigned int v4; // er13
size_t v5; // r14
char *v6; // rdi
__int64 v7; // rax
char *v9; // [rsp+8h] [rbp-30h] BYREF

if ( !*s )
_assert_fail("strlen(command) > 0", "src/execute.c", 0xAu, "execute_new");
if ( !a2 )
_assert_fail("tasks != NULL", "src/execute.c", 0xBu, "execute_new");
if ( is_command_only((__int64)s) )
{
get_user_input_msg(&v9, "What do you need to get done? ");
v6 = v9;
}
else
{
v4 = (unsigned int)strchr(s, 32) - (_DWORD)s;
v5 = (int)(strlen(s) - v4);
v9 = (char *)malloc(v5);
v6 = strncpy(v9, &s[v4 + 1], v5);
}
v7 = Task(v6);
*a3 = v7;
return list_append(a2, v7);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s, __int64 a2, __int64 *a3)
{
unsigned int v4; // er13
size_t v5; // r14
char *v6; // rdi
__int64 v7; // rax
char *v9; // [rsp+8h] [rbp-30h] BYREF

if ( !*s )
_assert_fail("strlen(command) > 0", "src/execute.c", 0xAu, "execute_new");
if ( !a2 )
_assert_fail("tasks != NULL", "src/execute.c", 0xBu, "execute_new");
if ( func1((__int64)s) )
{
get_user_input_msg(&v9, "What do you need to get done? ");
v6 = v9;
}
else
{
v4 = (unsigned int)strchr(s, 32) - (_DWORD)s;
v5 = (int)(strlen(s) - v4);
v9 = (char *)malloc(v5);
v6 = strncpy(v9, &s[v4 + 1], v5);
}
v7 = Task(v6);
*a3 = v7;
return list_append(a2, v7);
}

original_code:
void execute_new(char* command, ListT* tasks, TaskT** current_task) {
    ((void) sizeof ((strlen(command) > 0) ? 1 : 0), __extension__ ({ if (strlen(command) > 0) ; else __assert_fail ("strlen(command) > 0", "src/execute.c", 10, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((tasks != ((void *)0)) ? 1 : 0), __extension__ ({ if (tasks != ((void *)0)) ; else __assert_fail ("tasks != NULL", "src/execute.c", 11, __extension__ __PRETTY_FUNCTION__); }));
    char* task_str;
    TaskT* task;
    if (is_command_only(command)) {
        get_user_input_msg(&task_str, "What do you need to get done? ");
    } else {
        int first_space_idx, command_strlen;
        first_space_idx = strstr(command, " ") - command;
        command_strlen = strlen(command);
        task_str = malloc(sizeof(char) * (command_strlen - first_space_idx));
        strncpy(task_str, (command + first_space_idx + 1), (command_strlen - first_space_idx));
    }
    task = Task(task_str);
    *current_task = task;
    list_append(tasks, task);
}

canonical_original_code:
void execute_new(char *command, struct ListT *tasks, struct TaskT **current_task) {
    ((void) sizeof ((strlen(command) > 0) ? 1 : 0), __extension__ ({ if (strlen(command) > 0) ; else __assert_fail ("strlen(command) > 0", "src/execute.c", 10, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((tasks != ((void *)0)) ? 1 : 0), __extension__ ({ if (tasks != ((void *)0)) ; else __assert_fail ("tasks != NULL", "src/execute.c", 11, __extension__ __PRETTY_FUNCTION__); }));
    char *task_str;
    struct TaskT *task;
    if (is_command_only(command)) {
        get_user_input_msg(&task_str, "What do you need to get done? ");
    } else {
        int first_space_idx; int command_strlen;
        first_space_idx = strstr(command, " ") - command;
        command_strlen = strlen(command);
        task_str = malloc(sizeof(char) * (command_strlen - first_space_idx));
        strncpy(task_str, (command + first_space_idx + 1), (command_strlen - first_space_idx));
    }
    task = Task(task_str);
    *current_task = task;
    list_append(tasks, task);
}

variable_types: struct TaskT { char *text; struct ListT *notes; struct ListT *children; int is_complete; struct TaskT *parent; } **

variable_types: struct TaskT { char *text; struct ListT { void **values; void **last_value; int size; int capacity; } *notes; struct ListT *children; int is_complete; struct TaskT *parent; } *

return_type: void


decompiled_code:
int __fastcall execute_finish(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // rbx
int v5; // ebx
__int64 v6; // rax

v3 = *a3;
if ( *a3 )
{
printf("Finsihing '%s'", *(const char **)v3);
*(_DWORD *)(v3 + 24) = 1;
v5 = *(_DWORD *)(a2 + 16) - 1;
if ( v5 >= 0 )
{
while ( 1 )
{
v6 = list_obj_at_idx(a2, (unsigned int)v5);
if ( v5-- == 0 )
break;
if ( *(_DWORD *)(v6 + 24) != 1 )
goto LABEL_5;
}
if ( *(_DWORD *)(v6 + 24) != 1 )
{
LABEL_5:
*a3 = v6;
return v6;
}
}
LODWORD(v6) = puts("\nAll tasks completed!");
*a3 = 0LL;
}
else
{
LODWORD(v6) = puts("No current task.");
}
return v6;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // rbx
int v5; // ebx
__int64 v6; // rax

v3 = *a3;
if ( *a3 )
{
printf("Finsihing '%s'", *(const char **)v3);
*(_DWORD *)(v3 + 24) = 1;
v5 = *(_DWORD *)(a2 + 16) - 1;
if ( v5 >= 0 )
{
while ( 1 )
{
v6 = list_obj_at_idx(a2, (unsigned int)v5);
if ( v5-- == 0 )
break;
if ( *(_DWORD *)(v6 + 24) != 1 )
goto LABEL_5;
}
if ( *(_DWORD *)(v6 + 24) != 1 )
{
LABEL_5:
*a3 = v6;
return v6;
}
}
LODWORD(v6) = puts("\nAll tasks completed!");
*a3 = 0LL;
}
else
{
LODWORD(v6) = puts("No current task.");
}
return v6;
}

original_code:
void execute_finish(char* command, ListT* tasks, TaskT** current_task) {
    if (*current_task != ((void *)0)) {
        TaskT* _current;
        int task_idx;
        _current = *current_task;
        printf("Finsihing '%s'", _current->text);
        _current->is_complete = 1;
        task_idx = tasks->size - 1;
        for (task_idx = tasks->size - 1;
            task_idx >= 0 && _current->is_complete == 1;
            task_idx--) {
            _current = list_obj_at_idx(tasks, task_idx);
        }
        if (task_idx < -1 || _current->is_complete == 1) {
            puts("\nAll tasks completed!");
            *current_task = ((void *)0);
        } else {
            *current_task = _current;
        }
    } else {
        printf("No current task.\n");
    }
}

canonical_original_code:
void execute_finish(char *command, struct ListT *tasks, struct TaskT **current_task) {
    if (*current_task != ((void *)0)) {
        struct TaskT *_current;
        int task_idx;
        _current = *current_task;
        printf("Finsihing '%s'", _current->text);
        _current->is_complete = 1;
        task_idx = tasks->size - 1;
        for (task_idx = tasks->size - 1;
            task_idx >= 0 && _current->is_complete == 1;
            task_idx--) {
            _current = list_obj_at_idx(tasks, task_idx);
        }
        if (task_idx < -1 || _current->is_complete == 1) {
            puts("\nAll tasks completed!");
            *current_task = ((void *)0);
        } else {
            *current_task = _current;
        }
    } else {
        printf("No current task.\n");
    }
}

variable_types: struct ListT { void **values; void **last_value; int size; int capacity; } *

user_defined_types: struct TaskT { char *text; struct ListT *notes; struct ListT *children; int is_complete; struct TaskT *parent; }


decompiled_code:
int __fastcall execute_show(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // rax
const char *v4; // rdx

v3 = *a3;
if ( !*a3 )
return puts("No current task.");
v4 = "Finished";
if ( !*(_DWORD *)(v3 + 24) )
v4 = "Not Finished";
return printf("\nYour current task is: '%s'. \nIts status is '%s'.\n", *(_QWORD *)v3, v4);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // rax
const char *v4; // rdx

v3 = *a3;
if ( !*a3 )
return puts("No current task.");
v4 = "Finished";
if ( !*(_DWORD *)(v3 + 24) )
v4 = "Not Finished";
return printf("\nYour current task is: '%s'. \nIts status is '%s'.\n", *(_QWORD *)v3, v4);
}

original_code:
void execute_show(char* command, ListT* tasks, TaskT** current_task) {
    TaskT* _current;
    _current = *current_task;
    if (_current != ((void *)0)) {
        char* status;
        status = _current->is_complete ? "Finished" : "Not Finished";
        printf("\nYour current task is: '%s'. \nIts status is '%s'.\n", _current->text, status);
    } else {
        printf("No current task.\n");
    }
}

canonical_original_code:
void execute_show(char *command, struct ListT *tasks, struct TaskT **current_task) {
    struct TaskT *_current;
    _current = *current_task;
    if (_current != ((void *)0)) {
        char *status;
        status = _current->is_complete ? "Finished" : "Not Finished";
        printf("\nYour current task is: '%s'. \nIts status is '%s'.\n", _current->text, status);
    } else {
        printf("No current task.\n");
    }
}

variable_types:
status
char *

user_defined_types: struct ListT { void **values; void **last_value; int size; int capacity; }


decompiled_code:
int __fastcall execute_add_note(char *s, __int64 a2, _QWORD *a3)
{
char *v4; // r12
size_t v5; // rax
char *v6; // r8
char *v8; // [rsp+8h] [rbp-20h] BYREF

if ( !s )
_assert_fail("command != NULL", "src/execute.c", 0x59u, "execute_add_note");
if ( !*a3 )
return puts("No current task");
if ( !*(_DWORD *)(a2 + 16) )
return puts("No tasks");
if ( is_command_only((__int64)s) )
{
get_multiline_input(&v8, "What is your note? (hit return twice to end)\n");
v6 = v8;
}
else
{
v4 = strchr(s, 32);
v5 = strlen(s);
v8 = (char *)malloc(v5 - (s - v4));
v6 = strcpy(v8, v4 + 1);
}
return task_add_note(*a3, v6);
}

canonical_decompiled_code:
int __fastcall func8(char *s, __int64 a2, _QWORD *a3)
{
char *v4; // r12
size_t v5; // rax
char *v6; // r8
char *v8; // [rsp+8h] [rbp-20h] BYREF

if ( !s )
_assert_fail("command != NULL", "src/execute.c", 0x59u, "execute_add_note");
if ( !*a3 )
return puts("No current task");
if ( !*(_DWORD *)(a2 + 16) )
return puts("No tasks");
if ( func1((__int64)s) )
{
get_multiline_input(&v8, "What is your note? (hit return twice to end)\n");
v6 = v8;
}
else
{
v4 = strchr(s, 32);
v5 = strlen(s);
v8 = (char *)malloc(v5 - (s - v4));
v6 = strcpy(v8, v4 + 1);
}
return task_add_note(*a3, v6);
}

original_code:
void execute_add_note(char* command, ListT* tasks, TaskT** current_task) {
    ((void) sizeof ((command != ((void *)0)) ? 1 : 0), __extension__ ({ if (command != ((void *)0)) ; else __assert_fail ("command != NULL", "src/execute.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    if (*current_task != ((void *)0)) {
        if (tasks->size == 0) {
            puts("No tasks");
            return;
        }
        char* note;
        if (is_command_only(command)) {
            get_multiline_input(&note, "What is your note? (hit return twice to end)\n");
        } else {
            char *after_space, *first_space;
            int space_idx;
            first_space = strchr(command, ' ');
            note = malloc(sizeof(char) * (strlen(command) - (command - first_space)));
            strcpy(note, first_space + 1);
        }
        task_add_note(*current_task, note);
    } else {
        puts("No current task");
    }
}

canonical_original_code:
void execute_add_note(char *command, struct ListT *tasks, struct TaskT **current_task) {
    ((void) sizeof ((command != ((void *)0)) ? 1 : 0), __extension__ ({ if (command != ((void *)0)) ; else __assert_fail ("command != NULL", "src/execute.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    if (*current_task != ((void *)0)) {
        if (tasks->size == 0) {
            puts("No tasks");
            return;
        }
        char *note;
        if (is_command_only(command)) {
            get_multiline_input(&note, "What is your note? (hit return twice to end)\n");
        } else {
            char *after_space; char *first_space;
            int space_idx;
            first_space = strchr(command, ' ');
            note = malloc(sizeof(char) * (strlen(command) - (command - first_space)));
            strcpy(note, first_space + 1);
        }
        task_add_note(*current_task, note);
    } else {
        puts("No current task");
    }
}

variable_types:
note
char *


decompiled_code:
int __fastcall execute_print(__int64 a1, __int64 a2)
{
int result; // eax
unsigned int v3; // ebx
unsigned int v4; // er12
__int64 v5; // rax

if ( !a2 )
_assert_fail("tasks != NULL", "src/execute.c", 0x76u, "execute_print");
if ( !*(_DWORD *)(a2 + 16) )
return puts("No tasks.");
puts(&byte_9EC);
result = *(_DWORD *)(a2 + 16);
if ( result > 0 )
{
v3 = 0;
do
{
v4 = v3++;
printf("Task #%d: ", v3);
v5 = list_obj_at_idx(a2, v4);
task_print(v5);
result = printf("\n");
}
while ( (signed int)v3 < *(_DWORD *)(a2 + 16) );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, __int64 a2)
{
int result; // eax
unsigned int v3; // ebx
unsigned int v4; // er12
__int64 v5; // rax

if ( !a2 )
_assert_fail("tasks != NULL", "src/execute.c", 0x76u, "execute_print");
if ( !*(_DWORD *)(a2 + 16) )
return puts("No tasks.");
puts(&byte_9EC);
result = *(_DWORD *)(a2 + 16);
if ( result > 0 )
{
v3 = 0;
do
{
v4 = v3++;
printf("Task #%d: ", v3);
v5 = list_obj_at_idx(a2, v4);
task_print(v5);
result = printf("\n");
}
while ( (signed int)v3 < *(_DWORD *)(a2 + 16) );
}
return result;
}

original_code:
void execute_print(char* command, ListT* tasks, TaskT** current_task) {
    ((void) sizeof ((tasks != ((void *)0)) ? 1 : 0), __extension__ ({ if (tasks != ((void *)0)) ; else __assert_fail ("tasks != NULL", "src/execute.c", 118, __extension__ __PRETTY_FUNCTION__); }));
    if (tasks->size == 0) {
        puts("No tasks.");
    } else {
        puts("");
        int task_idx;
        for (task_idx = 0; task_idx < tasks->size; task_idx++) {
            printf("Task #%d: ", task_idx + 1);
            TaskT* task;
            task = list_obj_at_idx(tasks, task_idx);
            task_print(task);
            printf("\n", ((void *)0));
        }
    }
}

canonical_original_code:
void execute_print(char *command, struct ListT *tasks, struct TaskT **current_task) {
    ((void) sizeof ((tasks != ((void *)0)) ? 1 : 0), __extension__ ({ if (tasks != ((void *)0)) ; else __assert_fail ("tasks != NULL", "src/execute.c", 118, __extension__ __PRETTY_FUNCTION__); }));
    if (tasks->size == 0) {
        puts("No tasks.");
    } else {
        puts("");
        int task_idx;
        for (task_idx = 0; task_idx < tasks->size; task_idx++) {
            printf("Task #%d: ", task_idx + 1);
            struct TaskT *task;
            task = list_obj_at_idx(tasks, task_idx);
            task_print(task);
            printf("\n", ((void *)0));
        }
    }
}


decompiled_code:
void __fastcall execute_save(__int64 a1, __int64 a2)
{
char *v2; // r12
size_t v3; // rax
char *v4; // r13
char *v5; // r13
size_t v6; // rax
char *v7; // rbx
char *v8; // rdi
char *s; // [rsp+0h] [rbp-30h] BYREF

if ( a2 && *(int *)(a2 + 16) > 0 )
{
s = 0LL;
v2 = make_file_name();
v3 = strlen(v2);
v4 = (char *)malloc(v3 + 49);
sprintf(v4, "Enter a filename or hit return to save as '%s'.\n", v2);
get_user_input_msg(&s, v4);
v5 = s;
if ( s )
{
if ( *s )
{
v6 = strlen(s);
v7 = (char *)malloc(v6 + 7);
sprintf(v7, "tasks/%s", v5);
v8 = v2;
v2 = v7;
free(v8);
free(s);
}
}
task_write(a2, v2);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, __int64 a2)
{
char *v2; // r12
size_t v3; // rax
char *v4; // r13
char *v5; // r13
size_t v6; // rax
char *v7; // rbx
char *v8; // rdi
char *s; // [rsp+0h] [rbp-30h] BYREF

if ( a2 && *(int *)(a2 + 16) > 0 )
{
s = 0LL;
v2 = func3();
v3 = strlen(v2);
v4 = (char *)malloc(v3 + 49);
sprintf(v4, "Enter a filename or hit return to save as '%s'.\n", v2);
get_user_input_msg(&s, v4);
v5 = s;
if ( s )
{
if ( *s )
{
v6 = strlen(s);
v7 = (char *)malloc(v6 + 7);
sprintf(v7, "tasks/%s", v5);
v8 = v2;
v2 = v7;
free(v8);
free(s);
}
}
task_write(a2, v2);
free(v2);
}
}

original_code:
void execute_save(char* command, ListT* tasks, TaskT** current_task) {
    char* filename;
    int write_result;
    if (tasks != ((void *)0) && tasks->size > 0) {
        char* user_filename = ((void *)0);
        char* input_msg = ((void *)0);
        filename = make_file_name();
        input_msg = malloc(strlen(filename) + strlen(kFilenamePromptFormat) + 1);
        sprintf(input_msg, kFilenamePromptFormat, filename);
        get_user_input_msg(&user_filename, input_msg);
        if (user_filename != ((void *)0) && strlen(user_filename) > 0) {
            char* user_filename_with_dir;
            user_filename_with_dir = malloc(6 + 1 + strlen(user_filename));
            sprintf(user_filename_with_dir, "tasks/%s", user_filename);
            free(filename);
            free(user_filename);
            filename = user_filename_with_dir;
        }
        write_result = task_write(tasks, filename);
        if (task_write < 0) {
            printf("Failed to write tasks to %s\n", filename);
        }
        free(filename);
    }
}

canonical_original_code:
void execute_save(char *command, struct ListT *tasks, struct TaskT **current_task) {
    char *filename;
    int write_result;
    if (tasks != ((void *)0) && tasks->size > 0) {
        char *user_filename = ((void *)0);
        char *input_msg = ((void *)0);
        filename = make_file_name();
        input_msg = malloc(strlen(filename) + strlen(kFilenamePromptFormat) + 1);
        sprintf(input_msg, kFilenamePromptFormat, filename);
        get_user_input_msg(&user_filename, input_msg);
        if (user_filename != ((void *)0) && strlen(user_filename) > 0) {
            char *user_filename_with_dir;
            user_filename_with_dir = malloc(6 + 1 + strlen(user_filename));
            sprintf(user_filename_with_dir, "tasks/%s", user_filename);
            free(filename);
            free(user_filename);
            filename = user_filename_with_dir;
        }
        write_result = task_write(tasks, filename);
        if (task_write < 0) {
            printf("Failed to write tasks to %s\n", filename);
        }
        free(filename);
    }
}


decompiled_code:
__int64 __fastcall execute_load(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // r13
__int64 v5; // r12
int v6; // eax
unsigned int v7; // ebp
const char *v8; // r14
size_t v9; // rax
char *v10; // rbp
__int64 v11; // r14
char v13; // al
__int64 v14[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = a2;
if ( a2 && *(int *)(a2 + 16) > 0 )
{
printf("Save before load? (y)/n? ");
get_user_input(v14);
if ( v14[0] )
{
v13 = *(_BYTE *)v14[0];
if ( *(_BYTE *)v14[0] == 121 )
goto LABEL_14;
}
else
{
v13 = *(_BYTE *)get_text_from_command;
}
if ( v13 != 89 )
goto LABEL_3;
LABEL_14:
puts("saving");
execute_save(a1, a2);
v5 = get_loadables_constprop_0();
if ( *(int *)(v5 + 16) > 0 )
goto LABEL_4;
LABEL_15:
puts("No files to load.");
return list_destroy(v5);
}
LABEL_3:
v5 = get_loadables_constprop_0();
if ( *(int *)(v5 + 16) <= 0 )
goto LABEL_15;
do
{
do
{
LABEL_4:
v6 = get_numeric_input("Choose which file to load? ");
v7 = v6;
}
while ( v6 < 0 );
}
while ( *(_DWORD *)(v5 + 16) <= v6 );
tk_log("Debug", "src/execute.c", "execute_load", 180LL, "user_selection %d", (unsigned int)v6);
puts("\n");
v8 = (const char *)list_obj_at_idx(v5, v7);
v9 = strlen(v8);
v10 = (char *)malloc(v9 + 20);
sprintf(v10, "tasks/%s", v8);
list_empty(a2, 1LL);
v11 = task_read(v10);
list_copy(a2, v11);
if ( a2 )
{
if ( *(int *)(a2 + 16) > 0 )
v3 = list_obj_at_idx(a2, 0LL);
else
v3 = 0LL;
}
*a3 = v3;
list_destroy(v11);
free(v10);
return list_destroy(v5);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 *a3)
{
__int64 v3; // r13
__int64 v5; // r12
int v6; // eax
unsigned int v7; // ebp
const char *v8; // r14
size_t v9; // rax
char *v10; // rbp
__int64 v11; // r14
char v13; // al
__int64 v14[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = a2;
if ( a2 && *(int *)(a2 + 16) > 0 )
{
printf("Save before load? (y)/n? ");
get_user_input(v14);
if ( v14[0] )
{
v13 = *(_BYTE *)v14[0];
if ( *(_BYTE *)v14[0] == 121 )
goto LABEL_14;
}
else
{
v13 = *(_BYTE *)get_text_from_command;
}
if ( v13 != 89 )
goto LABEL_3;
LABEL_14:
puts("saving");
func11(a1, a2);
v5 = func4();
if ( *(int *)(v5 + 16) > 0 )
goto LABEL_4;
LABEL_15:
puts("No files to load.");
return list_destroy(v5);
}
LABEL_3:
v5 = func4();
if ( *(int *)(v5 + 16) <= 0 )
goto LABEL_15;
do
{
do
{
LABEL_4:
v6 = get_numeric_input("Choose which file to load? ");
v7 = v6;
}
while ( v6 < 0 );
}
while ( *(_DWORD *)(v5 + 16) <= v6 );
tk_log("Debug", "src/execute.c", "execute_load", 180LL, "user_selection %d", (unsigned int)v6);
puts("\n");
v8 = (const char *)list_obj_at_idx(v5, v7);
v9 = strlen(v8);
v10 = (char *)malloc(v9 + 20);
sprintf(v10, "tasks/%s", v8);
list_empty(a2, 1LL);
v11 = task_read(v10);
list_copy(a2, v11);
if ( a2 )
{
if ( *(int *)(a2 + 16) > 0 )
v3 = list_obj_at_idx(a2, 0LL);
else
v3 = 0LL;
}
*a3 = v3;
list_destroy(v11);
free(v10);
return list_destroy(v5);
}

original_code:
void execute_load(char* command, ListT* tasks, TaskT** current_task) {
    ListT* files;
    if (tasks != ((void *)0) && tasks->size > 0) {
        char* save_before_load;
        printf("Save before load? (y)/n? ");
        get_user_input(&save_before_load);
        if (save_before_load != ((void *)0) && strlen(save_before_load) > 0
            && save_before_load[0] == 'y' || save_before_load[0] == 'Y') {
            printf("saving\n");
            execute_save(command, tasks, current_task);
        }
    }
    files = get_loadables(1);
    if (files->size > 0) {
        ListT* _tasks;
        TaskT* loaded_task;
        int user_selection;
        char *filename, *abs_file;;
        while ((user_selection = get_numeric_input("Choose which file to load? ")) < 0 || user_selection >= files->size) ;
        do { tk_log("Debug", "src/execute.c", __FUNCTION__, 180, "user_selection %d", user_selection); } while(0);;
        printf("\n\n");
        filename = list_obj_at_idx(files, user_selection);
        abs_file = malloc(strlen(filename) + 20);
        sprintf(abs_file, "tasks/%s", filename);
        list_empty(tasks, 1);
        _tasks = task_read(abs_file);
        list_copy(tasks, _tasks);
        *current_task = tasks != ((void *)0) && tasks->size > 0 ? list_obj_at_idx(tasks, 0) : ((void *)0);
        list_destroy(_tasks);
        free(abs_file);
    } else {
        puts("No files to load.");
    }
    list_destroy(files);
}

canonical_original_code:
void execute_load(char *command, struct ListT *tasks, struct TaskT **current_task) {
    struct ListT *files;
    if (tasks != ((void *)0) && tasks->size > 0) {
        char *save_before_load;
        printf("Save before load? (y)/n? ");
        get_user_input(&save_before_load);
        if (save_before_load != ((void *)0) && strlen(save_before_load) > 0
            && save_before_load[0] == 'y' || save_before_load[0] == 'Y') {
            printf("saving\n");
            execute_save(command, tasks, current_task);
        }
    }
    files = get_loadables(1);
    if (files->size > 0) {
        struct ListT *_tasks;
        struct TaskT *loaded_task;
        int user_selection;
        char *filename; char *abs_file;;
        while ((user_selection = get_numeric_input("Choose which file to load? ")) < 0 || user_selection >= files->size) ;
        do { tk_log("Debug", "src/execute.c", __FUNCTION__, 180, "user_selection %d", user_selection); } while(0);;
        printf("\n\n");
        filename = list_obj_at_idx(files, user_selection);
        abs_file = malloc(strlen(filename) + 20);
        sprintf(abs_file, "tasks/%s", filename);
        list_empty(tasks, 1);
        _tasks = task_read(abs_file);
        list_copy(tasks, _tasks);
        *current_task = tasks != ((void *)0) && tasks->size > 0 ? list_obj_at_idx(tasks, 0) : ((void *)0);
        list_destroy(_tasks);
        free(abs_file);
    } else {
        puts("No files to load.");
    }
    list_destroy(files);
}


decompiled_code:
int __fastcall execute_change(__int64 a1, __int64 a2, __int64 *a3)
{
size_t v5; // rdi
_DWORD *v6; // rax
_DWORD *v7; // r13
_DWORD *v8; // r12
int v9; // er14
unsigned int v10; // ebx
__int64 v11; // rsi
__int64 v12; // rax
const char *v13; // rdx
int v14; // eax
__int64 v15; // rax

v5 = *(int *)(a2 + 16);
v6 = calloc(v5, 4uLL);
if ( (int)v5 <= 0 )
goto LABEL_11;
v7 = v6;
v8 = v6;
v9 = 0;
v10 = 0;
do
{
while ( 1 )
{
v11 = v10++;
v12 = list_obj_at_idx(a2, v11);
if ( !*(_DWORD *)(v12 + 24) )
break;
++v8;
if ( *(_DWORD *)(a2 + 16) <= (signed int)v10 )
goto LABEL_6;
}
v13 = *(const char **)v12;
*v8 = 1;
v9 = 1;
++v8;
printf("%d) %s\n", v10, v13);
}
while ( *(_DWORD *)(a2 + 16) > (signed int)v10 );
LABEL_6:
if ( v9 == 1 )
{
while ( 1 )
{
v14 = get_numeric_input("Which task do you want to switch to? ");
if ( v7[v14 - 1] )
break;
printf("Invalid choice! ");
}
v15 = list_obj_at_idx(a2, (unsigned int)(v14 - 1));
*a3 = v15;
}
else
{
LABEL_11:
LODWORD(v15) = puts("No incomplete tasks to switch to.");
}
return v15;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, __int64 a2, __int64 *a3)
{
size_t v5; // rdi
_DWORD *v6; // rax
_DWORD *v7; // r13
_DWORD *v8; // r12
int v9; // er14
unsigned int v10; // ebx
__int64 v11; // rsi
__int64 v12; // rax
const char *v13; // rdx
int v14; // eax
__int64 v15; // rax

v5 = *(int *)(a2 + 16);
v6 = calloc(v5, 4uLL);
if ( (int)v5 <= 0 )
goto LABEL_11;
v7 = v6;
v8 = v6;
v9 = 0;
v10 = 0;
do
{
while ( 1 )
{
v11 = v10++;
v12 = list_obj_at_idx(a2, v11);
if ( !*(_DWORD *)(v12 + 24) )
break;
++v8;
if ( *(_DWORD *)(a2 + 16) <= (signed int)v10 )
goto LABEL_6;
}
v13 = *(const char **)v12;
*v8 = 1;
v9 = 1;
++v8;
printf("%d) %s\n", v10, v13);
}
while ( *(_DWORD *)(a2 + 16) > (signed int)v10 );
LABEL_6:
if ( v9 == 1 )
{
while ( 1 )
{
v14 = get_numeric_input("Which task do you want to switch to? ");
if ( v7[v14 - 1] )
break;
printf("Invalid choice! ");
}
v15 = list_obj_at_idx(a2, (unsigned int)(v14 - 1));
*a3 = v15;
}
else
{
LABEL_11:
LODWORD(v15) = puts("No incomplete tasks to switch to.");
}
return v15;
}

original_code:
void execute_change(char* command, ListT* tasks, TaskT** current_task) {
    int choice = -1, task_idx, one_displayed = 0;
    int* choices;
    choices = calloc(tasks->size, sizeof(int));
    for (task_idx = 0; task_idx < tasks->size; task_idx++) {
        TaskT* task;
        task = list_obj_at_idx(tasks, task_idx);
        if (task->is_complete == 0) {
            choices[task_idx] = 1;
            one_displayed = 1;
            printf("%d) %s\n", task_idx + 1, task->text);
        }
    }
    if (one_displayed == 1) {
        char* msg;
        msg = "Which task do you want to switch to? ";
        while (choices[(choice = get_numeric_input(msg)) - 1] == 0) {
            printf("Invalid choice! ", ((void *)0));
        }
        *current_task = list_obj_at_idx(tasks, choice - 1);
    } else {
        puts("No incomplete tasks to switch to.");
    }
}

canonical_original_code:
void execute_change(char *command, struct ListT *tasks, struct TaskT **current_task) {
    int choice = -1; int task_idx; int one_displayed = 0;
    int *choices;
    choices = calloc(tasks->size, sizeof(int));
    for (task_idx = 0; task_idx < tasks->size; task_idx++) {
        struct TaskT *task;
        task = list_obj_at_idx(tasks, task_idx);
        if (task->is_complete == 0) {
            choices[task_idx] = 1;
            one_displayed = 1;
            printf("%d) %s\n", task_idx + 1, task->text);
        }
    }
    if (one_displayed == 1) {
        char *msg;
        msg = "Which task do you want to switch to? ";
        while (choices[(choice = get_numeric_input(msg)) - 1] == 0) {
            printf("Invalid choice! ", ((void *)0));
        }
        *current_task = list_obj_at_idx(tasks, choice - 1);
    } else {
        puts("No incomplete tasks to switch to.");
    }
}

variable_types: choice int

variable_types:
choices
int *

variable_types:
msg
char *


substr

decompiled_code:
void *__fastcall substr(char *s, int a2, int a3)
{
size_t v3; // rbx
void *v4; // r12
size_t v5; // r14
void *result; // rax
__int64 v7; // [rsp+0h] [rbp-78h] BYREF

v3 = a3;
v4 = malloc(0xAuLL);
v5 = (int)v3 + a2;
if ( v5 >= strlen(s) )
{
putchar(10);
print_stack(&v7, 10LL);
putchar(10);
printf("the length of the substring plus its start index must be less than the length of the total string");
putchar(10);
if ( v5 >= strlen(s) )
_assert_fail("(substr_len + start) < strlen(str)", "src/tk_string.c", 7u, "substr");
}
result = memcpy(v4, &s[a2], v3);
*((_BYTE *)v4 + v3) = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(char *s, int a2, int a3)
{
size_t v3; // rbx
void *v4; // r12
size_t v5; // r14
void *result; // rax
__int64 v7; // [rsp+0h] [rbp-78h] BYREF

v3 = a3;
v4 = malloc(0xAuLL);
v5 = (int)v3 + a2;
if ( v5 >= strlen(s) )
{
putchar(10);
print_stack(&v7, 10LL);
putchar(10);
printf("the length of the substring plus its start index must be less than the length of the total string");
putchar(10);
if ( v5 >= strlen(s) )
_assert_fail("(substr_len + start) < strlen(str)", "src/tk_string.c", 7u, "substr");
}
result = memcpy(v4, &s[a2], v3);
*((_BYTE *)v4 + v3) = 0;
return result;
}

original_code:
char* substr(char * str, int start, int substr_len) {
    char* dest;
    dest = malloc(sizeof(char) * (10));
    if (!((substr_len + start) < strlen(str))) { printf("\n"); do { void *arr[10]; print_stack(arr, 10); } while(0);; printf("\n"); printf("the length of the substring plus its start index must be less " "than the length of the total string"); printf("\n"); ((void) sizeof (((substr_len + start) < strlen(str)) ? 1 : 0), __extension__ ({ if ((substr_len + start) < strlen(str)) ; else __assert_fail ("(substr_len + start) < strlen(str)", "src/tk_string.c", 7, __extension__ __PRETTY_FUNCTION__); })); };
    memcpy(dest, &str[start], substr_len);
    dest[substr_len] = '\0';
    return dest;
}

canonical_original_code:
char *substr(char *str, int start, int substr_len) {
    char *dest;
    dest = malloc(sizeof(char) * (10));
    if (!((substr_len + start) < strlen(str))) { printf("\n"); do { void *arr[10]; print_stack(arr, 10); } while(0);; printf("\n"); printf("the length of the substring plus its start index must be less " "than the length of the total string"); printf("\n"); ((void) sizeof (((substr_len + start) < strlen(str)) ? 1 : 0), __extension__ ({ if ((substr_len + start) < strlen(str)) ; else __assert_fail ("(substr_len + start) < strlen(str)", "src/tk_string.c", 7, __extension__ __PRETTY_FUNCTION__); })); };
    memcpy(dest, &str[start], substr_len);
    dest[substr_len] = '\0';
    return dest;
}

variable_types:
str
char *

variable_types: start int

variable_types:
dest
char *

variable_types:
arr
void *[10]


stroccur

decompiled_code:
__int64 __fastcall stroccur(const char *a1, const char *a2)
{
unsigned int v2; // er12
char *i; // rax

v2 = 0;
for ( i = strstr(a1, a2); i; i = strstr(i + 1, a2) )
++v2;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
unsigned int v2; // er12
char *i; // rax

v2 = 0;
for ( i = strstr(a1, a2); i; i = strstr(i + 1, a2) )
++v2;
return v2;
}

original_code:
int stroccur(char * str, char * substr) {
    int count = 0;
    int offset = 0;
    char * occurance = strstr(str, substr);
    while (occurance) {
        count++;
        occurance = strstr(occurance + 1, substr);
    }
    return count;
}

canonical_original_code:
int stroccur(char *str, char *substr) {
    int count = 0;
    int offset = 0;
    char *occurance = strstr(str, substr);
    while (occurance) {
        count++;
        occurance = strstr(occurance + 1, substr);
    }
    return count;
}

variable_types:
substr
char *

variable_types: count int

variable_types: offset int

variable_types:
occurance
char *


decompiled_code:
__int64 __fastcall split_arr_size(const char *a1, const char *a2)
{
return 8LL * (int)(stroccur(a1, a2) + 2);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
return 8LL * (int)(func1(a1, a2) + 2);
}

original_code:
size_t split_arr_size(char * str, char * delim) {
    return sizeof(char *) * (stroccur(str, delim) + 2);
}

canonical_original_code:
unsigned long split_arr_size(char *str, char *delim) {
    return sizeof(char *) * (stroccur(str, delim) + 2);
}

variable_types:
delim
char *

return_type: unsigned long


strlower

decompiled_code:
__int64 __fastcall strlower(_BYTE *a1)
{
__int64 result; // rax

while ( 1 )
{
result = (unsigned __int8)*a1;
if ( !(_BYTE)result )
break;
if ( (unsigned __int8)(result - 65) <= 0x19u )
*a1 = result + 32;
++a1;
}
return result;
}

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

while ( 1 )
{
result = (unsigned __int8)*a1;
if ( !(_BYTE)result )
break;
if ( (unsigned __int8)(result - 65) <= 0x19u )
*a1 = result + 32;
++a1;
}
return result;
}

original_code:
void strlower(char * to_lower) {
    int i;
    for (i= 0; to_lower[i] != '\0'; i++) {
        char c = to_lower[i];
        if (c >= 'A' && c <= 'Z') {
            c += kAsciiDiff;
            to_lower[i] = c;
        }
    }
}

canonical_original_code:
void strlower(char *to_lower) {
    int i;
    for (i= 0; to_lower[i] != '\0'; i++) {
        char c = to_lower[i];
        if (c >= 'A' && c <= 'Z') {
            c += kAsciiDiff;
            to_lower[i] = c;
        }
    }
}

variable_types: i int

variable_types: c char


strtodigit

decompiled_code:
__int64 __fastcall strtodigit(char *a1)
{
char v1; // al
char *v2; // rdi
int v3; // edx
unsigned int v4; // er8
unsigned __int8 v5; // al

v1 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = a1 + 1;
v3 = 0;
v4 = 0;
do
{
v5 = v1 - 48;
if ( v5 <= 9u )
{
v4 = (char)v5 + 10 * v4;
v3 = 1;
}
v1 = *v2++;
}
while ( v1 );
if ( !v3 )
v4 = -1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1)
{
char v1; // al
char *v2; // rdi
int v3; // edx
unsigned int v4; // er8
unsigned __int8 v5; // al

v1 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = a1 + 1;
v3 = 0;
v4 = 0;
do
{
v5 = v1 - 48;
if ( v5 <= 9u )
{
v4 = (char)v5 + 10 * v4;
v3 = 1;
}
v1 = *v2++;
}
while ( v1 );
if ( !v3 )
v4 = -1;
return v4;
}

original_code:
int strtodigit(char* str) {
    int str_idx;
    int digit;
    int found_digit;
    digit = 0;
    found_digit = 0;
    for (str_idx = 0; str[str_idx] != '\0'; str_idx++) {
        char c;
        c = str[str_idx];
        if (c >= '0' && c <= '9') {
            found_digit = 1;
            digit *= 10;
            digit += (c - '0');
        }
    }
    return found_digit ? digit : -1;
}

canonical_original_code:
int strtodigit(char *str) {
    int str_idx;
    int digit;
    int found_digit;
    digit = 0;
    found_digit = 0;
    for (str_idx = 0; str[str_idx] != '\0'; str_idx++) {
        char c;
        c = str[str_idx];
        if (c >= '0' && c <= '9') {
            found_digit = 1;
            digit *= 10;
            digit += (c - '0');
        }
    }
    return found_digit ? digit : -1;
}

variable_types: digit int


trim

decompiled_code:
char *__fastcall trim(char *s)
{
char *v1; // rbp
char v2; // r12
int v3; // eax
__int64 v4; // r13
size_t v5; // rax
__int64 v6; // rdx
size_t v7; // rcx
char *v8; // rax
size_t v9; // rbx
char *v10; // rax
char *result; // rax

v1 = s;
v2 = *s;
if ( *s == 32 )
{
v3 = 0;
do
{
v2 = *++v1;
++v3;
}
while ( v2 == 32 );
v4 = v3;
}
else
{
v4 = 0LL;
}
v5 = strlen(s);
v6 = 0LL;
v7 = v5;
if ( v2 )
{
v8 = &s[(int)v5 - 1];
if ( *v8 == 32 )
{
do
{
--v8;
LODWORD(v6) = v6 + 1;
}
while ( *v8 == 32 );
v6 = (int)v6;
}
}
v9 = v7 - v4 - v6;
v10 = (char *)malloc(v9 + 1);
result = strncpy(v10, v1, v9);
result[v9] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func5(char *s)
{
char *v1; // rbp
char v2; // r12
int v3; // eax
__int64 v4; // r13
size_t v5; // rax
__int64 v6; // rdx
size_t v7; // rcx
char *v8; // rax
size_t v9; // rbx
char *v10; // rax
char *result; // rax

v1 = s;
v2 = *s;
if ( *s == 32 )
{
v3 = 0;
do
{
v2 = *++v1;
++v3;
}
while ( v2 == 32 );
v4 = v3;
}
else
{
v4 = 0LL;
}
v5 = strlen(s);
v6 = 0LL;
v7 = v5;
if ( v2 )
{
v8 = &s[(int)v5 - 1];
if ( *v8 == 32 )
{
do
{
--v8;
LODWORD(v6) = v6 + 1;
}
while ( *v8 == 32 );
v6 = (int)v6;
}
}
v9 = v7 - v4 - v6;
v10 = (char *)malloc(v9 + 1);
result = strncpy(v10, v1, v9);
result[v9] = 0;
return result;
}

original_code:
char* trim(char* str) {
    char* trimmed;
    char* strp;
    int back_cnt = 0, front_cnt = 0, str_idx = 0, len;
    size_t trimmed_size;
    strp = str;
    while (*strp == ' ' && *strp != '\0') {
        strp++;
        front_cnt++;
    }
    if (*strp != '\0') {
        for (str_idx = strlen(str) - 1; str[str_idx] == ' '; str_idx--) {
            back_cnt++;
        }
    }
    trimmed_size = strlen(str) - back_cnt - front_cnt + 1;
    trimmed = malloc(trimmed_size);
    strncpy(trimmed, strp, trimmed_size - 1);
    trimmed[trimmed_size - 1] = '\0';
    return trimmed;
}

canonical_original_code:
char *trim(char *str) {
    char *trimmed;
    char *strp;
    int back_cnt = 0; int front_cnt = 0; int str_idx = 0; int len;
    unsigned long trimmed_size;
    strp = str;
    while (*strp == ' ' && *strp != '\0') {
        strp++;
        front_cnt++;
    }
    if (*strp != '\0') {
        for (str_idx = strlen(str) - 1; str[str_idx] == ' '; str_idx--) {
            back_cnt++;
        }
    }
    trimmed_size = strlen(str) - back_cnt - front_cnt + 1;
    trimmed = malloc(trimmed_size);
    strncpy(trimmed, strp, trimmed_size - 1);
    trimmed[trimmed_size - 1] = '\0';
    return trimmed;
}

variable_types:
trimmed
char *

variable_types:
strp
char *

variable_types: len int


decompiled_code:
__int64 __fastcall list_obj_at_idx(__int64 a1, int a2)
{
if ( *(_DWORD *)(a1 + 16) <= a2 )
_assert_fail("idx < self->size", "src/list.c", 0x33u, "list_obj_at_idx");
return *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
if ( *(_DWORD *)(a1 + 16) <= a2 )
_assert_fail("idx < self->size", "src/list.c", 0x33u, "list_obj_at_idx");
return *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
}

original_code:
void * list_obj_at_idx(ListT* self, int idx) {
    ((void) sizeof ((idx < self->size) ? 1 : 0), __extension__ ({ if (idx < self->size) ; else __assert_fail ("idx < self->size", "src/list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
    return self->values[idx];
}

canonical_original_code:
void *list_obj_at_idx(struct ListT *self, int idx) {
    ((void) sizeof ((idx < self->size) ? 1 : 0), __extension__ ({ if (idx < self->size) ; else __assert_fail ("idx < self->size", "src/list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
    return self->values[idx];
}

variable_types: struct ListT { void **values; void **last_value; int size; int capacity; } *

variable_types: idx int

return_type: void *


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

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

original_code:
void list_destroy(ListT* list) {
    int value_idx;
    free(list->values);
    free(list);
}

canonical_original_code:
void list_destroy(struct ListT *list) {
    int value_idx;
    free(list->values);
    free(list);
}


decompiled_code:
__int64 __fastcall list_append(char **a1, __int64 a2)
{
int v3; // eax
char *v4; // rdi
__int64 v5; // rdx
__int64 result; // rax
char *v7; // rcx
int v8; // ebp
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-68h] BYREF

if ( !a1 )
{
putchar(10);
print_stack(&v10, 10LL);
putchar(10);
printf("Assertion failed");
putchar(10);
_assert_fail("self != ((void *)0)", "src/list.c", 0x3Fu, "list_append");
}
v3 = *((_DWORD *)a1 + 4);
v4 = *a1;
if ( *((_DWORD *)a1 + 5) == v3 )
{
v8 = 2 * v3;
v9 = (char *)realloc(v4, kVoidPointerSize * 2 * v3);
*((_DWORD *)a1 + 5) = v8;
*a1 = v9;
v4 = v9;
v3 = *((_DWORD *)a1 + 4);
}
v5 = v3;
result = (unsigned int)(v3 + 1);
*(_QWORD *)&v4[8 * v5] = a2;
v7 = *a1;
*((_DWORD *)a1 + 4) = result;
a1[1] = &v7[8 * v5];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char **a1, __int64 a2)
{
int v3; // eax
char *v4; // rdi
__int64 v5; // rdx
__int64 result; // rax
char *v7; // rcx
int v8; // ebp
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-68h] BYREF

if ( !a1 )
{
putchar(10);
print_stack(&v10, 10LL);
putchar(10);
printf("Assertion failed");
putchar(10);
_assert_fail("self != ((void *)0)", "src/list.c", 0x3Fu, "list_append");
}
v3 = *((_DWORD *)a1 + 4);
v4 = *a1;
if ( *((_DWORD *)a1 + 5) == v3 )
{
v8 = 2 * v3;
v9 = (char *)realloc(v4, kVoidPointerSize * 2 * v3);
*((_DWORD *)a1 + 5) = v8;
*a1 = v9;
v4 = v9;
v3 = *((_DWORD *)a1 + 4);
}
v5 = v3;
result = (unsigned int)(v3 + 1);
*(_QWORD *)&v4[8 * v5] = a2;
v7 = *a1;
*((_DWORD *)a1 + 4) = result;
a1[1] = &v7[8 * v5];
return result;
}

original_code:
void list_append(ListT* self, void * obj) {
    if (!(self != ((void *)0))) { printf("\n"); do { void *arr[10]; print_stack(arr, 10); } while(0);; printf("\n"); printf("Assertion failed", ((void *)0)); printf("\n"); ((void) sizeof ((self != ((void *)0)) ? 1 : 0), __extension__ ({ if (self != ((void *)0)) ; else __assert_fail ("self != ((void *)0)", "src/list.c", 63, __extension__ __PRETTY_FUNCTION__); })); };;
    if (self->capacity == self->size) {
        int new_capacity;
        new_capacity = self->capacity * 2;
        self->values = realloc(self->values, kVoidPointerSize * new_capacity);
        self->capacity = new_capacity;
    }
    self->values[self->size] = obj;
    self->last_value = &self->values[self->size];
    self->size++;
}

canonical_original_code:
void list_append(struct ListT *self, void *obj) {
    if (!(self != ((void *)0))) { printf("\n"); do { void *arr[10]; print_stack(arr, 10); } while(0);; printf("\n"); printf("Assertion failed", ((void *)0)); printf("\n"); ((void) sizeof ((self != ((void *)0)) ? 1 : 0), __extension__ ({ if (self != ((void *)0)) ; else __assert_fail ("self != ((void *)0)", "src/list.c", 63, __extension__ __PRETTY_FUNCTION__); })); };;
    if (self->capacity == self->size) {
        int new_capacity;
        new_capacity = self->capacity * 2;
        self->values = realloc(self->values, kVoidPointerSize * new_capacity);
        self->capacity = new_capacity;
    }
    self->values[self->size] = obj;
    self->last_value = &self->values[self->size];
    self->size++;
}

variable_types:
obj
void *


decompiled_code:
__int64 __fastcall list_index_of(__int64 a1, __int64 a2)
{
int v2; // ecx
_QWORD *v3; // rdx
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 16);
if ( v2 <= 0 )
return 0LL;
v3 = *(_QWORD **)a1;
result = 0LL;
do
{
if ( *v3 == a2 )
break;
result = (unsigned int)(result + 1);
++v3;
}
while ( (_DWORD)result != v2 );
return result;
}

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

v2 = *(_DWORD *)(a1 + 16);
if ( v2 <= 0 )
return 0LL;
v3 = *(_QWORD **)a1;
result = 0LL;
do
{
if ( *v3 == a2 )
break;
result = (unsigned int)(result + 1);
++v3;
}
while ( (_DWORD)result != v2 );
return result;
}

original_code:
int list_index_of(ListT* self, void* obj) {
    int value_idx;
    for (value_idx = 0; value_idx < self->size && self->values[value_idx] != obj; value_idx++) ;
    return value_idx;
}

canonical_original_code:
int list_index_of(struct ListT *self, void *obj) {
    int value_idx;
    for (value_idx = 0; value_idx < self->size && self->values[value_idx] != obj; value_idx++) ;
    return value_idx;
}


original_code:
void* list_pop(ListT* self) {
    void* to_return;
    if (self->size > 0) {
        to_return = *self->last_value;
        self->values[self->size - 1] = ((void *)0);
        self->size--;
        self->last_value = self->size == 0 ? ((void *)0) : &self->values[self->size - 1];
        return to_return;
    }
    return ((void *)0);
}

canonical_original_code:
void *list_pop(struct ListT *self) {
    void *to_return;
    if (self->size > 0) {
        to_return = *self->last_value;
        self->values[self->size - 1] = ((void *)0);
        self->size--;
        self->last_value = self->size == 0 ? ((void *)0) : &self->values[self->size - 1];
        return to_return;
    }
    return ((void *)0);
}


decompiled_code:
void *__fastcall list_empty(__int64 a1, int a2)
{
void *result; // rax

while ( 1 )
{
result = (void *)list_pop(a1);
if ( !result )
break;
if ( a2 )
free(result);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1, int a2)
{
void *result; // rax

while ( 1 )
{
result = (void *)func5(a1);
if ( !result )
break;
if ( a2 )
free(result);
}
return result;
}

original_code:
void list_empty(ListT* self, int do_free) {
    void* popped;
    while ((popped = list_pop(self)) != ((void *)0)) {
        if (do_free) {
            free(popped);
        }
    }
}

canonical_original_code:
void list_empty(struct ListT *self, int do_free) {
    void *popped;
    while ((popped = list_pop(self)) != ((void *)0)) {
        if (do_free) {
            free(popped);
        }
    }
}

variable_types:
popped
void *


decompiled_code:
__int64 __fastcall list_iterate(__int64 a1)
{
__int64 *v1; // rdx
__int64 v2; // rax
__int64 result; // rax
__int64 v4; // rdx

v1 = (__int64 *)current_3;
if ( !a1 )
{
if ( !current_3 )
_assert_fail("self != NULL || current != NULL", "src/list.c", 0x77u, "list_iterate");
LABEL_6:
v2 = list_idx_1;
if ( *(_DWORD *)(current_3 + 16) == list_idx_1 )
goto LABEL_4;
goto LABEL_7;
}
if ( a1 == current_3 )
goto LABEL_6;
v1 = (__int64 *)a1;
current_3 = a1;
v2 = 0LL;
if ( !*(_DWORD *)(a1 + 16) )
{
LABEL_4:
list_idx_1 = -1;
result = 0LL;
current_3 = 0LL;
return result;
}
LABEL_7:
v4 = *v1;
list_idx_1 = v2 + 1;
return *(_QWORD *)(v4 + 8 * v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 *v1; // rdx
__int64 v2; // rax
__int64 result; // rax
__int64 v4; // rdx

v1 = (__int64 *)current_3;
if ( !a1 )
{
if ( !current_3 )
_assert_fail("self != NULL || current != NULL", "src/list.c", 0x77u, "list_iterate");
LABEL_6:
v2 = list_idx_1;
if ( *(_DWORD *)(current_3 + 16) == list_idx_1 )
goto LABEL_4;
goto LABEL_7;
}
if ( a1 == current_3 )
goto LABEL_6;
v1 = (__int64 *)a1;
current_3 = a1;
v2 = 0LL;
if ( !*(_DWORD *)(a1 + 16) )
{
LABEL_4:
list_idx_1 = -1;
result = 0LL;
current_3 = 0LL;
return result;
}
LABEL_7:
v4 = *v1;
list_idx_1 = v2 + 1;
return *(_QWORD *)(v4 + 8 * v2);
}

original_code:
void* list_iterate(ListT* self) {
    static ListT* current = ((void *)0);
    static int list_idx = -1;
    ((void) sizeof ((self != ((void *)0) || current != ((void *)0)) ? 1 : 0), __extension__ ({ if (self != ((void *)0) || current != ((void *)0)) ; else __assert_fail ("self != NULL || current != NULL", "src/list.c", 119, __extension__ __PRETTY_FUNCTION__); }));
    if (self != ((void *)0) && self != current) {
        current = self;
        list_idx = 0;
    }
    ((void) sizeof ((current != ((void *)0)) ? 1 : 0), __extension__ ({ if (current != ((void *)0)) ; else __assert_fail ("current != NULL", "src/list.c", 126, __extension__ __PRETTY_FUNCTION__); }));
    if (list_idx == current->size) {
        list_idx = -1;
        current = ((void *)0);
        return ((void *)0);
    } else {
        return current->values[list_idx++];
    }
}

canonical_original_code:
void *list_iterate(struct ListT *self) {
    static struct ListT *current = ((void *)0);
    static int list_idx = -1;
    ((void) sizeof ((self != ((void *)0) || current != ((void *)0)) ? 1 : 0), __extension__ ({ if (self != ((void *)0) || current != ((void *)0)) ; else __assert_fail ("self != NULL || current != NULL", "src/list.c", 119, __extension__ __PRETTY_FUNCTION__); }));
    if (self != ((void *)0) && self != current) {
        current = self;
        list_idx = 0;
    }
    ((void) sizeof ((current != ((void *)0)) ? 1 : 0), __extension__ ({ if (current != ((void *)0)) ; else __assert_fail ("current != NULL", "src/list.c", 126, __extension__ __PRETTY_FUNCTION__); }));
    if (list_idx == current->size) {
        list_idx = -1;
        current = ((void *)0);
        return ((void *)0);
    } else {
        return current->values[list_idx++];
    }
}


decompiled_code:
__int64 __fastcall list_copy(char **a1, __int64 a2)
{
__int64 result; // rax

if ( !a1 || !a2 )
_assert_fail("dest != NULL && source != NULL", "src/list.c", 0x8Cu, "list_copy");
while ( 1 )
{
result = list_iterate(a2);
if ( !result )
break;
list_append(a1, result);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char **a1, __int64 a2)
{
__int64 result; // rax

if ( !a1 || !a2 )
_assert_fail("dest != NULL && source != NULL", "src/list.c", 0x8Cu, "list_copy");
while ( 1 )
{
result = func7(a2);
if ( !result )
break;
func3(a1, result);
}
return result;
}

original_code:
void list_copy(ListT* dest, ListT* source) {
    ((void) sizeof ((dest != ((void *)0) && source != ((void *)0)) ? 1 : 0), __extension__ ({ if (dest != ((void *)0) && source != ((void *)0)) ; else __assert_fail ("dest != NULL && source != NULL", "src/list.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    void* item;
    while ( (item = list_iterate(source)) != ((void *)0)) {
        list_append(dest, item);
    }
}

canonical_original_code:
void list_copy(struct ListT *dest, struct ListT *source) {
    ((void) sizeof ((dest != ((void *)0) && source != ((void *)0)) ? 1 : 0), __extension__ ({ if (dest != ((void *)0) && source != ((void *)0)) ; else __assert_fail ("dest != NULL && source != NULL", "src/list.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    void *item;
    while ( (item = list_iterate(source)) != ((void *)0)) {
        list_append(dest, item);
    }
}

variable_types:
source
struct ListT *

variable_types:
item
void *


hash

decompiled_code:
__int64 __fastcall hash(_BYTE *a1, unsigned int a2)
{
int v2; // edx
_BYTE *v3; // rdi
unsigned int v4; // eax

v2 = (char)*a1;
if ( !*a1 )
return 0LL;
v3 = a1 + 1;
v4 = 0;
do
{
++v3;
v4 = v2 + 31 * v4;
v2 = (char)*(v3 - 1);
}
while ( *(v3 - 1) );
return v4 % a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, unsigned int a2)
{
int v2; // edx
_BYTE *v3; // rdi
unsigned int v4; // eax

v2 = (char)*a1;
if ( !*a1 )
return 0LL;
v3 = a1 + 1;
v4 = 0;
do
{
++v3;
v4 = v2 + 31 * v4;
v2 = (char)*(v3 - 1);
}
while ( *(v3 - 1) );
return v4 % a2;
}

original_code:
static uint32_t hash(char* key, int capacity) {
    uint32_t hash_value;
    int key_idx;
    for (key_idx = 0, hash_value = 0; key[key_idx] != '\0'; key_idx++) {
        hash_value = key[key_idx] + 31 * hash_value;
    }
    return hash_value % capacity;
}

canonical_original_code:
static unsigned int hash(char *key, int capacity) {
    unsigned int hash_value;
    int key_idx;
    for (key_idx = 0, hash_value = 0; key[key_idx] != '\0'; key_idx++) {
        hash_value = key[key_idx] + 31 * hash_value;
    }
    return hash_value % capacity;
}

variable_types:
key
char *

variable_types: capacity int

return_type: unsigned int


Dict

decompiled_code:
_QWORD *Dict()
{
_QWORD *v0; // r12
void *v1; // rax

v0 = malloc(0x10uLL);
v1 = calloc(0x64uLL, 0x20uLL);
v0[1] = 100LL;
*v0 = v1;
return v0;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *v0; // r12
void *v1; // rax

v0 = malloc(0x10uLL);
v1 = calloc(0x64uLL, 0x20uLL);
v0[1] = 100LL;
*v0 = v1;
return v0;
}

original_code:
DictionaryT* Dict() {
    DictionaryT *self = malloc(kDictSize);
    self->list = calloc(initCapacity, kNodeSize);
    self->capacity = initCapacity;
    self->size = 0;
    return self;
}

canonical_original_code:
struct DictionaryT *Dict() {
    struct DictionaryT *self = malloc(kDictSize);
    self->list = calloc(initCapacity, kNodeSize);
    self->capacity = initCapacity;
    self->size = 0;
    return self;
}

variable_types: struct DictionaryT { struct DictionaryNodeT { char *key; void *value; unsigned int hash; struct DictionaryNodeT *next; } **list; int capacity; int size; } *

user_defined_types: struct DictionaryT { struct DictionaryNodeT **list; int capacity; int size; }


decompiled_code:
void __fastcall dict_destroy(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(ptr);
}

original_code:
void dict_destroy(DictionaryT* self) {
    DictionaryNodeT *node;
    int i;
    for (i = 0; i < self->size; i++) {
        free(self->list[i]);
    }
    free(self);
}

canonical_original_code:
void dict_destroy(struct DictionaryT *self) {
    struct DictionaryNodeT *node;
    int i;
    for (i = 0; i < self->size; i++) {
        free(self->list[i]);
    }
    free(self);
}

variable_types: struct DictionaryNodeT { char *key; void *value; unsigned int hash; struct DictionaryNodeT *next; } *


decompiled_code:
__int64 __fastcall dict_obj_at(__int64 a1, _BYTE *a2)
{
unsigned int v2; // eax
_QWORD *v3; // r9
__int64 result; // rax

v2 = hash(a2, *(_DWORD *)(a1 + 8));
result = *(_QWORD *)(*v3 + 8LL * v2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _BYTE *a2)
{
unsigned int v2; // eax
_QWORD *v3; // r9
__int64 result; // rax

v2 = func0(a2, *(_DWORD *)(a1 + 8));
result = *(_QWORD *)(*v3 + 8LL * v2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void* dict_obj_at(DictionaryT* self, char* key) {
    uint32_t hash_value = hash(key, self->capacity);
    if (!self->list[hash_value]) {
        return ((void *)0);
    }
    DictionaryNodeT *node = self->list[hash_value];
    return node->value;
}

canonical_original_code:
void *dict_obj_at(struct DictionaryT *self, char *key) {
    unsigned int hash_value = hash(key, self->capacity);
    if (!self->list[hash_value]) {
        return ((void *)0);
    }
    struct DictionaryNodeT *node = self->list[hash_value];
    return node->value;
}

user_defined_types: struct DictionaryNodeT { char *key; void *value; unsigned int hash; struct DictionaryNodeT *next; }


decompiled_code:
int tk_log(const char *a1, const char *a2, const char *a3, unsigned int a4, const char *a5, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a5);
printf("[%s: %s, %s (Line %d)] ", a1, a2, a3, a4);
vprintf(a5, arg);
return putchar(10);
}

canonical_decompiled_code:
int func0(const char *a1, const char *a2, const char *a3, unsigned int a4, const char *a5, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a5);
printf("[%s: %s, %s (Line %d)] ", a1, a2, a3, a4);
vprintf(a5, arg);
return putchar(10);
}

original_code:
void tk_log(char level[], char file[], const char function_name[], int line_num, char * const fmt, ...) {
    va_list args;
    __builtin_va_start(args,fmt);
    printf(kLogPrepend, level, file, function_name, line_num);
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

canonical_original_code:
void tk_log(char level[], char file[], char function_name[], int line_num, char *fmt, ...) {
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    printf(kLogPrepend, level, file, function_name, line_num);
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

variable_types:
level
char []

variable_types:
file
char []

variable_types:
fmt
char *


decompiled_code:
void __fastcall print_stack(void **array, int a2)
{
int v2; // eax
__int64 v3; // r12
char **v4; // rbp
__int64 i; // rbx
const char *v6; // rdi

v2 = backtrace(array, a2);
v3 = v2;
v4 = backtrace_symbols(array, v2);
printf("Obtained %zd stack frames.\n", v3);
if ( v3 )
{
for ( i = 0LL; i != v3; ++i )
{
v6 = v4[i];
puts(v6);
}
}
free(v4);
}

canonical_decompiled_code:
void __fastcall func0(void **array, int a2)
{
int v2; // eax
__int64 v3; // r12
char **v4; // rbp
__int64 i; // rbx
const char *v6; // rdi

v2 = backtrace(array, a2);
v3 = v2;
v4 = backtrace_symbols(array, v2);
printf("Obtained %zd stack frames.\n", v3);
if ( v3 )
{
for ( i = 0LL; i != v3; ++i )
{
v6 = v4[i];
puts(v6);
}
}
free(v4);
}

original_code:
void print_stack(void ** arr, int stack_size) {
    size_t i;
    char **strings;
    size_t size;
    size = backtrace(arr, stack_size);
    strings = backtrace_symbols(arr, size);
    printf("Obtained %zd stack frames.\n", size);
    for (i = 0; i < size; i++) {
        printf("%s\n", strings[i]);
    }
    free(strings);
}

canonical_original_code:
void print_stack(void **arr, int stack_size) {
    unsigned long i;
    char **strings;
    unsigned long size;
    size = backtrace(arr, stack_size);
    strings = backtrace_symbols(arr, size);
    printf("Obtained %zd stack frames.\n", size);
    for (i = 0; i < size; i++) {
        printf("%s\n", strings[i]);
    }
    free(strings);
}

variable_types:
arr
void **

variable_types:
i
unsigned long

variable_types:
strings
char **

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall get_filetype(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(haystack, "image/gif");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(haystack, "image/jpeg");
}
else
{
strcpy(haystack, "text/plain");
result = 28265LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(haystack, "image/gif");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(haystack, "image/jpeg");
}
else
{
strcpy(haystack, "text/plain");
result = 28265LL;
}
return result;
}

original_code:
static void get_filetype(char *filename, char *filetype)
{
 if(strstr(filename, ".html"))
  strcpy(filetype,"text/html");
 else if(strstr(filename, ".gif"))
  strcpy(filename,"image/gif");
 else if(strstr(filename, ".jpg"))
  strcpy(filename, "image/jpeg");
 else
  strcpy(filename, "text/plain");
}

variable_types:
filename
char *

variable_types:
filetype
char *


decompiled_code:
int __fastcall put_static_info(__int64 a1, char *a2, int a3)
{
size_t v3; // rbp
size_t v4; // rax
int v5; // er13
void *v6; // r12
char v8[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

v3 = a3;
get_filetype(a2, v8);
sprintf(s, "%s 200 OK\r\n", (const char *)(a1 + 24592));
sprintf(s, "%sContent-length:%d\r\n", s, (unsigned int)v3);
sprintf(s, "%sContent-type: %s\r\n\r\n", s, v8);
v4 = strlen(s);
io_writen(*(_QWORD *)(a1 + 8), s, v4);
v5 = open(a2, 0, 0LL);
v6 = mmap(0LL, v3, 1, 2, v5, 0LL);
close(v5);
io_writen(*(_QWORD *)(a1 + 8), v6, v3);
return munmap(v6, v3);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, char *a2, int a3)
{
size_t v3; // rbp
size_t v4; // rax
int v5; // er13
void *v6; // r12
char v8[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

v3 = a3;
func0(a2, v8);
sprintf(s, "%s 200 OK\r\n", (const char *)(a1 + 24592));
sprintf(s, "%sContent-length:%d\r\n", s, (unsigned int)v3);
sprintf(s, "%sContent-type: %s\r\n\r\n", s, v8);
v4 = strlen(s);
io_writen(*(_QWORD *)(a1 + 8), s, v4);
v5 = open(a2, 0, 0LL);
v6 = mmap(0LL, v3, 1, 2, v5, 0LL);
close(v5);
io_writen(*(_QWORD *)(a1 + 8), v6, v3);
return munmap(v6, v3);
}

original_code:
void put_static_info(request_t *q, char *filename, int filesize)
{
 int srcfd;
 char *srcp, filetype[8192], buf[8192];
 get_filetype(filename, filetype);
 sprintf(buf, "%s 200 OK\r\n",q->ver);
 sprintf(buf, "%sContent-length:%d\r\n",buf,filesize);
 sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
 io_writen(q->io, buf, strlen(buf));
 srcfd = open(filename, 00, 0);
 srcp = mmap(0, filesize, 0x1, 0x02, srcfd, 0);
 close(srcfd);
 io_writen(q->io, srcp, filesize);
 munmap(srcp, filesize);
}

canonical_original_code:
void put_static_info(struct __request_t *q, char *filename, int filesize)
{
 int srcfd;
 char *srcp; char filetype[8192]; char buf[8192];
 get_filetype(filename, filetype);
 sprintf(buf, "%s 200 OK\r\n",q->ver);
 sprintf(buf, "%sContent-length:%d\r\n",buf,filesize);
 sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
 io_writen(q->io, buf, strlen(buf));
 srcfd = open(filename, 00, 0);
 srcp = mmap(0, filesize, 0x1, 0x02, srcfd, 0);
 close(srcfd);
 io_writen(q->io, srcp, filesize);
 munmap(srcp, filesize);
}

variable_types: struct __request_t { int fd; struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *io; char resource[8192]; char method[8192]; char uri[8192]; char ver[8192]; char host; int need_cgi; char cgi_args[8192]; char header[8192]; } *

variable_types: filesize int

variable_types: srcfd int

variable_types:
srcp
char *

variable_types:
filetype
char [8192]

variable_types:
buf
char [8192]

user_defined_types: struct __request_t { int fd; struct io_t *io; char resource[8192]; char method[8192]; char uri[8192]; char ver[8192]; char host; int need_cgi; char cgi_args[8192]; char header[8192]; }


decompiled_code:
__pid_t __fastcall run_cgi(__int64 a1, const char *a2, const char *a3)
{
size_t v4; // rax
__pid_t result; // eax
char *argv; // [rsp+8h] [rbp-2020h] BYREF
char s[8216]; // [rsp+10h] [rbp-2018h] BYREF

argv = 0LL;
sprintf(s, "%s 200 OK\r\n", (const char *)(a1 + 24592));
v4 = strlen(s);
io_writen(*(_QWORD *)(a1 + 8), s, v4);
result = fork();
if ( !result )
{
setenv("QUERY_STRING", a3, 1);
dup2(**(_DWORD **)(a1 + 8), 1);
result = execve(a2, &argv, environ);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func2(__int64 a1, const char *a2, const char *a3)
{
size_t v4; // rax
__pid_t result; // eax
char *argv; // [rsp+8h] [rbp-2020h] BYREF
char s[8216]; // [rsp+10h] [rbp-2018h] BYREF

argv = 0LL;
sprintf(s, "%s 200 OK\r\n", (const char *)(a1 + 24592));
v4 = strlen(s);
io_writen(*(_QWORD *)(a1 + 8), s, v4);
result = fork();
if ( !result )
{
setenv("QUERY_STRING", a3, 1);
dup2(**(_DWORD **)(a1 + 8), 1);
result = execve(a2, &argv, environ);
}
return result;
}

original_code:
void run_cgi(request_t *q, char *filename, char *cgi_args)
{
 char buf[8192], *emptylist[]={((void *)0)};
 sprintf(buf, "%s 200 OK\r\n",q->ver);
 io_writen(q->io, buf, strlen(buf));
 if(fork() == 0)
 {
  setenv("QUERY_STRING", cgi_args, 1);
  dup2(q->io->io_fd, 1);
  execve(filename, emptylist, environ);
 }
}

canonical_original_code:
void run_cgi(struct __request_t *q, char *filename, char *cgi_args)
{
 char buf[8192]; char *emptylist[] = {((void *)0)};
 sprintf(buf, "%s 200 OK\r\n",q->ver);
 io_writen(q->io, buf, strlen(buf));
 if(fork() == 0)
 {
  setenv("QUERY_STRING", cgi_args, 1);
  dup2(q->io->io_fd, 1);
  execve(filename, emptylist, environ);
 }
}

variable_types:
emptylist
char *[]

user_defined_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; }


original_code:
static ssize_t io_read(io_t *rp, void *buf, size_t n)
{
 int cnt;
 while(rp->io_cnt <= 0)
 {
  rp->io_cnt = read(rp->io_fd, rp->io_buf, sizeof(rp->io_buf));
  if(rp->io_cnt < 0)
  {
   if((*__errno_location ()) != 4)
    return -1;
  }
  else if(rp->io_cnt == 0)
   return 0;
  else
   rp->io_bufptr = rp->io_buf;
 }
 cnt = n;
 if(rp->io_cnt < n)
  cnt = rp->io_cnt;
 memcpy(buf, rp->io_bufptr, cnt);
 rp->io_bufptr += cnt;
 rp->io_cnt -= cnt;
 return cnt;
}

canonical_original_code:
static long io_read(struct io_t *rp, void *buf, unsigned long n)
{
 int cnt;
 while(rp->io_cnt <= 0)
 {
  rp->io_cnt = read(rp->io_fd, rp->io_buf, sizeof(rp->io_buf));
  if(rp->io_cnt < 0)
  {
   if((*__errno_location ()) != 4)
    return -1;
  }
  else if(rp->io_cnt == 0)
   return 0;
  else
   rp->io_bufptr = rp->io_buf;
 }
 cnt = n;
 if(rp->io_cnt < n)
  cnt = rp->io_cnt;
 memcpy(buf, rp->io_bufptr, cnt);
 rp->io_bufptr += cnt;
 rp->io_cnt -= cnt;
 return cnt;
}

variable_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *

variable_types:
buf
void *

variable_types:
n
unsigned long

variable_types: cnt int

return_type: long


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

result = a1 + 16;
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
*(_QWORD *)(a1 + 8) = a1 + 16;
return result;
}

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

result = a1 + 16;
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
*(_QWORD *)(a1 + 8) = a1 + 16;
return result;
}

original_code:
void io_readinit(io_t *rp, int fd)
{
 rp->io_fd = fd;
 rp->io_cnt = 0;
 rp->io_bufptr = rp->io_buf;
}

canonical_original_code:
void io_readinit(struct io_t *rp, int fd)
{
 rp->io_fd = fd;
 rp->io_cnt = 0;
 rp->io_bufptr = rp->io_buf;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall io_readn(__int64 a1, char *a2, __int64 a3)
{
__int64 v3; // r13
signed __int64 v5; // rbx
__int64 v6; // rax

v3 = a3;
v5 = a3;
if ( a3 <= 0 )
return 0LL;
do
{
while ( 1 )
{
v6 = io_read(a1, a2, v5);
if ( v6 < 0 )
break;
if ( v6 )
{
v5 -= v6;
a2 += v6;
if ( v5 > 0 )
continue;
}
return v3 - v5;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char *a2, __int64 a3)
{
__int64 v3; // r13
signed __int64 v5; // rbx
__int64 v6; // rax

v3 = a3;
v5 = a3;
if ( a3 <= 0 )
return 0LL;
do
{
while ( 1 )
{
v6 = func0(a1, a2, v5);
if ( v6 < 0 )
break;
if ( v6 )
{
v5 -= v6;
a2 += v6;
if ( v5 > 0 )
continue;
}
return v3 - v5;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

original_code:
ssize_t io_readn(io_t *rp, void *buf, size_t n)
{
 ssize_t nleft = n;
 ssize_t nread;
 char *ptr = buf;
 while(nleft > 0)
 {
  if((nread = io_read(rp, ptr, nleft))<0)
  {
   if((*__errno_location ()) == 4)
    nread = 0;
   else
    return(-1);
  }
  else if(nread == 0)
  {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return(n-nleft);
}

canonical_original_code:
long io_readn(struct io_t *rp, void *buf, unsigned long n)
{
 long nleft = n;
 long nread;
 char *ptr = buf;
 while(nleft > 0)
 {
  if((nread = io_read(rp, ptr, nleft))<0)
  {
   if((*__errno_location ()) == 4)
    nread = 0;
   else
    return(-1);
  }
  else if(nread == 0)
  {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return(n-nleft);
}

variable_types: nleft long

variable_types: nread long

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall io_writen(int *a1, char *a2, __int64 a3)
{
__int64 v3; // r13
int v4; // er12
signed __int64 v6; // rbx
ssize_t v7; // rax

v3 = a3;
v4 = *a1;
if ( a3 <= 0 )
return v3;
v6 = a3;
do
{
while ( 1 )
{
v7 = write(v4, a2, v6);
if ( v7 <= 0 )
break;
v6 -= v7;
a2 += v7;
if ( v6 <= 0 )
return v3;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, char *a2, __int64 a3)
{
__int64 v3; // r13
int v4; // er12
signed __int64 v6; // rbx
ssize_t v7; // rax

v3 = a3;
v4 = *a1;
if ( a3 <= 0 )
return v3;
v6 = a3;
do
{
while ( 1 )
{
v7 = write(v4, a2, v6);
if ( v7 <= 0 )
break;
v6 -= v7;
a2 += v7;
if ( v6 <= 0 )
return v3;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

original_code:
ssize_t io_writen(io_t *rp, const void *buf, size_t n)
{
 ssize_t nleft;
 ssize_t nwriten;
 char *ptr;
 int fd = rp->io_fd;
 ptr = (char *)buf;
 nleft = n;
 while(nleft>0)
 {
  if((nwriten = write(fd, ptr, nleft))<=0)
  {
   if((*__errno_location ()) == 4)
   {
    nwriten = 0;
   }
   else
    return(-1);
  }
  nleft -= nwriten;
  ptr += nwriten;
 }
 return (n);
}

canonical_original_code:
long io_writen(struct io_t *rp, void *buf, unsigned long n)
{
 long nleft;
 long nwriten;
 char *ptr;
 int fd = rp->io_fd;
 ptr = (char *)buf;
 nleft = n;
 while(nleft>0)
 {
  if((nwriten = write(fd, ptr, nleft))<=0)
  {
   if((*__errno_location ()) == 4)
   {
    nwriten = 0;
   }
   else
    return(-1);
  }
  nleft -= nwriten;
  ptr += nwriten;
 }
 return (n);
}

variable_types: nwriten long


original_code:
ssize_t io_readline(io_t *rp, void *buf, size_t len)
{
 ssize_t rc,n;
 char c,*ptr=buf;
 for(n=1;n<len;n++)
 {
  if((rc = io_read(rp, &c, 1)) ==1)
  {
   *ptr++ =c;
   if(c == '\n')
    break;
  }
  else if(rc == 0)
  {
   if(n == 1)
    return 0;
   else
    break;
  }
  else
  {
   return -1;
  }
 }
 *ptr=0;
 return n;
}

canonical_original_code:
long io_readline(struct io_t *rp, void *buf, unsigned long len)
{
 long rc; long n;
 char c; char *ptr = buf;
 for(n=1;n<len;n++)
 {
  if((rc = io_read(rp, &c, 1)) ==1)
  {
   *ptr++ =c;
   if(c == '\n')
    break;
  }
  else if(rc == 0)
  {
   if(n == 1)
    return 0;
   else
    break;
  }
  else
  {
   return -1;
  }
 }
 *ptr=0;
 return n;
}

variable_types:
len
unsigned long

variable_types: rc long

variable_types: n long

variable_types: c char


setnonblocking

decompiled_code:
__int64 __fastcall setnonblocking(int fd)
{
int v1; // eax
__int64 result; // rax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
{
fwrite("fcntl(sock,GETFL)", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
BYTE1(v1) |= 8u;
if ( fcntl(fd, 4, (unsigned int)v1) < 0 )
{
fwrite("fcntl(sock,SETFL,opts)", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
int v1; // eax
__int64 result; // rax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
{
fwrite("fcntl(sock,GETFL)", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
BYTE1(v1) |= 8u;
if ( fcntl(fd, 4, (unsigned int)v1) < 0 )
{
fwrite("fcntl(sock,SETFL,opts)", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int setnonblocking(int sock)
{
     int opts;
     opts=fcntl(sock,3);
     if(opts<0)
        {
     fprintf(stderr,"fcntl(sock,GETFL)");
     return -1;
  }
  opts = opts|04000;
  if(fcntl(sock,4,opts)<0)
  {
   fprintf(stderr,"fcntl(sock,SETFL,opts)");
       return -1;
  }
  return 0;
}

variable_types: sock int

variable_types: opts int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
void *v4; // rax
void *v5; // r12
unsigned int v6; // eax
int v7; // er13
int v8; // eax
int v9; // er15
int v10; // eax
epoll_data_t *v11; // rbx
epoll_data_t *v12; // r14
int v13; // eax
socklen_t v14; // [rsp+0h] [rbp-508h] BYREF
struct epoll_event event; // [rsp+4h] [rbp-504h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-4F8h] BYREF
struct epoll_event events; // [rsp+20h] [rbp-4E8h] BYREF
_DWORD v18[310]; // [rsp+30h] [rbp-4D8h] BYREF

v4 = malloc(0xC018uLL);
if ( argc > 1 )
{
v5 = v4;
v6 = atoi(argv[1]);
v7 = open_listenfd(v6);
if ( v7 >= 0 )
{
v8 = epoll_create(100);
v9 = v8;
if ( v8 != -1 )
{
event.events = 1;
event.data.fd = v7;
if ( epoll_ctl(v8, 1, v7, &event) != -1 )
{
while ( 1 )
{
v14 = 16;
v10 = epoll_wait(v9, &events, 100, -1);
if ( v10 == -1 )
break;
if ( v10 > 0 )
{
v11 = &events.data;
v12 = (epoll_data_t *)&v18[3 * (v10 - 1)];
do
{
if ( v11->fd == v7 )
{
v13 = accept(v7, &addr, &v14);
v3 = v13;
if ( v13 == -1 )
{
fwrite("accept error !\n", 1uLL, 0xFuLL, stderr);
exit(-1);
}
setnonblocking(v13);
event.events = -2147483647;
event.data.fd = v3;
if ( epoll_ctl(v9, 1, v3, &event) == -1 )
{
fwrite("epoll_ctl: conn_sock error !\n", 1uLL, 0x1DuLL, stderr);
exit(-1);
}
}
else
{
recv_request(v5);
handle_request(v5);
close(v3);
}
v11 = (epoll_data_t *)((char *)v11 + 12);
}
while ( v11 != v12 );
}
}
fwrite("epoll_wait error !\n", 1uLL, 0x13uLL, stderr);
exit(-1);
}
fwrite("epoll_ctl: listen_sock\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
fwrite("epoll_create error\n", 1uLL, 0x13uLL, stderr);
exit(-1);
}
fwrite("open listen sock failed!\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
fprintf(stderr, "no enough arguments!\n for %s\n,plz use %s <port> to run the program\n", *argv, *argv);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
void *v4; // rax
void *v5; // r12
unsigned int v6; // eax
int v7; // er13
int v8; // eax
int v9; // er15
int v10; // eax
epoll_data_t *v11; // rbx
epoll_data_t *v12; // r14
int v13; // eax
socklen_t v14; // [rsp+0h] [rbp-508h] BYREF
struct epoll_event event; // [rsp+4h] [rbp-504h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-4F8h] BYREF
struct epoll_event events; // [rsp+20h] [rbp-4E8h] BYREF
_DWORD v18[310]; // [rsp+30h] [rbp-4D8h] BYREF

v4 = malloc(0xC018uLL);
if ( argc > 1 )
{
v5 = v4;
v6 = atoi(argv[1]);
v7 = open_listenfd(v6);
if ( v7 >= 0 )
{
v8 = epoll_create(100);
v9 = v8;
if ( v8 != -1 )
{
event.events = 1;
event.data.fd = v7;
if ( epoll_ctl(v8, 1, v7, &event) != -1 )
{
while ( 1 )
{
v14 = 16;
v10 = epoll_wait(v9, &events, 100, -1);
if ( v10 == -1 )
break;
if ( v10 > 0 )
{
v11 = &events.data;
v12 = (epoll_data_t *)&v18[3 * (v10 - 1)];
do
{
if ( v11->fd == v7 )
{
v13 = accept(v7, &addr, &v14);
v3 = v13;
if ( v13 == -1 )
{
fwrite("accept error !\n", 1uLL, 0xFuLL, stderr);
exit(-1);
}
func0(v13);
event.events = -2147483647;
event.data.fd = v3;
if ( epoll_ctl(v9, 1, v3, &event) == -1 )
{
fwrite("epoll_ctl: conn_sock error !\n", 1uLL, 0x1DuLL, stderr);
exit(-1);
}
}
else
{
recv_request(v5);
handle_request(v5);
close(v3);
}
v11 = (epoll_data_t *)((char *)v11 + 12);
}
while ( v11 != v12 );
}
}
fwrite("epoll_wait error !\n", 1uLL, 0x13uLL, stderr);
exit(-1);
}
fwrite("epoll_ctl: listen_sock\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
fwrite("epoll_create error\n", 1uLL, 0x13uLL, stderr);
exit(-1);
}
fwrite("open listen sock failed!\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
fprintf(stderr, "no enough arguments!\n for %s\n,plz use %s <port> to run the program\n", *argv, *argv);
exit(1);
}

original_code:
int main(int argc, char **argv)
{
 int listenfd, epollfd, connfd, port, i;
 socklen_t clientlen;
 int nready;
 struct sockaddr_in clientaddr;
 struct epoll_event ev, events[100];
 request_t *q=(request_t *)malloc(sizeof(request_t));
 if(argc <2)
 {
  fprintf(stderr, "no enough arguments!\n for %s\n,plz use %s <port> to run the program\n",argv[0],argv[0]);
  exit(1);
 }
 port = atoi(argv[1]);
 if((listenfd = open_listenfd(port))<0)
 {
  fprintf(stderr,"open listen sock failed!\n");
  exit(1);
 }
 epollfd = epoll_create(100);
 if (epollfd == -1) {
     fprintf(stderr,"epoll_create error\n");
     exit(-1);
 }
 ev.events = EPOLLIN;
 ev.data.fd = listenfd;
 if (epoll_ctl(epollfd, 1, listenfd, &ev) == -1) {
     fprintf(stderr, "epoll_ctl: listen_sock\n");
     exit(-1);
 }
 for(;;)
 {
  clientlen = sizeof(clientaddr);
  nready = epoll_wait(epollfd, events, 100, -1);
  if (nready == -1) {
         fprintf(stderr,"epoll_wait error !\n");
         exit(-1);
     }
  for (i = 0; i < nready; ++i) {
   if (events[i].data.fd == listenfd) {
         connfd = accept(listenfd,
            (struct sockaddr *) &clientaddr, &clientlen);
         if (connfd == -1) {
          fprintf(stderr,"accept error !\n");
               exit(-1);
            }
            setnonblocking(connfd);
            ev.events = EPOLLIN | EPOLLET;
            ev.data.fd = connfd;
            if (epoll_ctl(epollfd, 1, connfd, &ev) == -1) {
                fprintf(stderr,"epoll_ctl: conn_sock error !\n");
             exit(-1);
      }
   }
   else{
   recv_request(q,events[i].data.fd);
   handle_request(q);
   close(connfd);
   }
  }
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int listenfd; int epollfd; int connfd; int port; int i;
 unsigned int clientlen;
 int nready;
 struct sockaddr_in clientaddr;
 struct epoll_event ev; struct epoll_event events[100];
 struct __request_t *q = (request_t *)malloc(sizeof(request_t));
 if(argc <2)
 {
  fprintf(stderr, "no enough arguments!\n for %s\n,plz use %s <port> to run the program\n",argv[0],argv[0]);
  exit(1);
 }
 port = atoi(argv[1]);
 if((listenfd = open_listenfd(port))<0)
 {
  fprintf(stderr,"open listen sock failed!\n");
  exit(1);
 }
 epollfd = epoll_create(100);
 if (epollfd == -1) {
     fprintf(stderr,"epoll_create error\n");
     exit(-1);
 }
 ev.events = EPOLLIN;
 ev.data.fd = listenfd;
 if (epoll_ctl(epollfd, 1, listenfd, &ev) == -1) {
     fprintf(stderr, "epoll_ctl: listen_sock\n");
     exit(-1);
 }
 for(;;)
 {
  clientlen = sizeof(clientaddr);
  nready = epoll_wait(epollfd, events, 100, -1);
  if (nready == -1) {
         fprintf(stderr,"epoll_wait error !\n");
         exit(-1);
     }
  for (i = 0; i < nready; ++i) {
   if (events[i].data.fd == listenfd) {
         connfd = accept(listenfd,
            (struct sockaddr *) &clientaddr, &clientlen);
         if (connfd == -1) {
          fprintf(stderr,"accept error !\n");
               exit(-1);
            }
            setnonblocking(connfd);
            ev.events = EPOLLIN | EPOLLET;
            ev.data.fd = connfd;
            if (epoll_ctl(epollfd, 1, connfd, &ev) == -1) {
                fprintf(stderr,"epoll_ctl: conn_sock error !\n");
             exit(-1);
      }
   }
   else{
   recv_request(q,events[i].data.fd);
   handle_request(q);
   close(connfd);
   }
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: listenfd int

variable_types: epollfd int

variable_types: connfd int

variable_types: port int

variable_types: i int

variable_types:
clientlen
unsigned int

variable_types: nready int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

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

variable_types: struct __request_t { int fd; struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *io; char resource[8192]; char method[8192]; char uri[8192]; char ver[8192]; char host; int need_cgi; char cgi_args[8192]; char header[8192]; } *

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
int __fastcall is_support_method(char *s1)
{
int result; // eax

result = strcasecmp(s1, "GET");
if ( result )
result = -(strcasecmp(s1, "HEAD") != 0);
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *s1)
{
int result; // eax

result = strcasecmp(s1, "GET");
if ( result )
result = -(strcasecmp(s1, "HEAD") != 0);
return result;
}

original_code:
static int is_support_method(char *method)
{
 if(!strcasecmp(method, "GET") || !strcasecmp(method, "HEAD"))
 {
  return 0;
 }
 else
  return -1;
}

variable_types:
method
char *


decompiled_code:
__int64 __fastcall open_listenfd(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int v5; // edi
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v7[2]; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 < 0
|| setsockopt(v1, 1, 2, &optval, 4u) < 0
|| (*(_QWORD *)&v7[0].sa_data[6] = 0LL,
v7[0].sa_family = 2,
*(_QWORD *)&v7[0].sa_data[2] = (unsigned int)_bswap_32_constprop_0(a1, v7, 16LL),
*(_WORD *)v7[0].sa_data = _bswap_16(a1),
bind(v2, v7, v3) < 0)
|| listen(v2, 1024) < 0 )
{
v5 = v2;
v2 = -1;
close(v5);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int v5; // edi
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v7[2]; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 < 0
|| setsockopt(v1, 1, 2, &optval, 4u) < 0
|| (*(_QWORD *)&v7[0].sa_data[6] = 0LL,
v7[0].sa_family = 2,
*(_QWORD *)&v7[0].sa_data[2] = (unsigned int)func3(a1, v7, 16LL),
*(_WORD *)v7[0].sa_data = func0(a1),
bind(v2, v7, v3) < 0)
|| listen(v2, 1024) < 0 )
{
v5 = v2;
v2 = -1;
close(v5);
}
return v2;
}

original_code:
int open_listenfd(int port)
{
 int listenfd, optval = 1;
 struct sockaddr_in serveraddr;
 if((listenfd = socket(2, SOCK_STREAM, 0))<0)
 {
  close(listenfd);
  return -1;
 }
 if(setsockopt(listenfd, 1, 2,
    (const void*)&optval, sizeof(int))<0)
 {
  close(listenfd);
  return -1;
 }
 bzero((char*)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 serveraddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 serveraddr.sin_port = htons((unsigned short)port);
 if(bind(listenfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr))<0)
 {
  close(listenfd);
  return -1;
 }
 if(listen(listenfd, 1024)<0)
 {
  close(listenfd);
  return -1;
 }
 return listenfd;
}

canonical_original_code:
int open_listenfd(int port)
{
 int listenfd; int optval = 1;
 struct sockaddr_in serveraddr;
 if((listenfd = socket(2, SOCK_STREAM, 0))<0)
 {
  close(listenfd);
  return -1;
 }
 if(setsockopt(listenfd, 1, 2,
    (void *)&optval, sizeof(int))<0)
 {
  close(listenfd);
  return -1;
 }
 bzero((char *)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 serveraddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 serveraddr.sin_port = htons((unsigned short)port);
 if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr))<0)
 {
  close(listenfd);
  return -1;
 }
 if(listen(listenfd, 1024)<0)
 {
  close(listenfd);
  return -1;
 }
 return listenfd;
}

variable_types: optval int


decompiled_code:
char *__fastcall recv_request(__int64 a1, unsigned int a2)
{
void *v2; // rax
_BYTE v4[8192]; // [rsp+0h] [rbp-8018h] BYREF
char src[8192]; // [rsp+2000h] [rbp-6018h] BYREF
char v6[8192]; // [rsp+4000h] [rbp-4018h] BYREF
char v7[8216]; // [rsp+6000h] [rbp-2018h] BYREF

v2 = malloc(0x2010uLL);
*(_QWORD *)(a1 + 8) = v2;
io_readinit(v2, a2);
io_readline(*(_QWORD *)(a1 + 8), v4, 0x2000LL);
_isoc99_sscanf(v4, "%s %s %s", src, v6, v7);
strcpy((char *)(a1 + 8208), src);
strcpy((char *)(a1 + 24592), v7);
return strcpy((char *)(a1 + 16400), v6);
}

original_code:
void recv_request(request_t *q, int fd)
{
 char buf[8192], method[8192], uri[8192], ver[8192];
 q->io=(io_t*)malloc(sizeof(io_t));
 io_readinit(q->io,fd);
 io_readline(q->io, buf, 8192);
 sscanf(buf, "%s %s %s",method, uri, ver);
 strcpy(q->method, method);
 strcpy(q->ver, ver);
 strcpy(q->uri, uri);
}

canonical_original_code:
void recv_request(struct __request_t *q, int fd)
{
 char buf[8192]; char method[8192]; char uri[8192]; char ver[8192];
 q->io=(struct io_t *)malloc(sizeof(io_t));
 io_readinit(q->io,fd);
 io_readline(q->io, buf, 8192);
 sscanf(buf, "%s %s %s",method, uri, ver);
 strcpy(q->method, method);
 strcpy(q->ver, ver);
 strcpy(q->uri, uri);
}

variable_types: fd int

variable_types:
buf
char [8192]

variable_types:
method
char [8192]

variable_types:
uri
char [8192]

variable_types:
ver
char [8192]


decompiled_code:
char *__fastcall read_request_header(__int64 a1)
{
char *result; // rax
__int16 v2; // [rsp+0h] [rbp-2018h] BYREF
char v3; // [rsp+2h] [rbp-2016h]

result = (char *)io_readline(*(_QWORD *)(a1 + 8), &v2, 0x2000LL);
while ( v2 != 2573 || v3 )
{
io_readline(*(_QWORD *)(a1 + 8), &v2, 0x2000LL);
result = strcpy((char *)(a1 + 40984), (const char *)&v2);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 a1)
{
char *result; // rax
__int16 v2; // [rsp+0h] [rbp-2018h] BYREF
char v3; // [rsp+2h] [rbp-2016h]

result = (char *)io_readline(*(_QWORD *)(a1 + 8), &v2, 0x2000LL);
while ( v2 != 2573 || v3 )
{
io_readline(*(_QWORD *)(a1 + 8), &v2, 0x2000LL);
result = strcpy((char *)(a1 + 40984), (const char *)&v2);
}
return result;
}

original_code:
void read_request_header(request_t *q)
{
 char buf[8192];
 io_readline(q->io, buf, 8192);
 while(strcmp(buf, "\r\n"))
 {
  io_readline(q->io, buf, 8192);
  strcpy(q->header, buf);
 }
 return ;
}

canonical_original_code:
void read_request_header(struct __request_t *q)
{
 char buf[8192];
 io_readline(q->io, buf, 8192);
 while(strcmp(buf, "\r\n"))
 {
  io_readline(q->io, buf, 8192);
  strcpy(q->header, buf);
 }
 return ;
}


decompiled_code:
__int64 __fastcall parse_uri(char *s, __int64 a2, char *a3)
{
char *v4; // rax
char *v5; // r12
__int64 result; // rax
char *v7; // rbx
size_t v8; // r8

if ( strstr(s, "cgi-bin") )
{
v4 = strchr(s, 63);
v5 = v4;
if ( v4 )
{
strcpy(a3, v4 + 1);
*v5 = 0;
}
else
{
*a3 = 0;
}
*(_BYTE *)a2 = 46;
strcpy((char *)(a2 + 1), s);
result = 1LL;
}
else
{
*a3 = 0;
*(_WORD *)(a2 + 4) = 29816;
*(_DWORD *)a2 = 1702113070;
v7 = stpcpy((char *)(a2 + 6), s);
v8 = strlen(s);
result = 0LL;
if ( v8 == 1 && *s == 47 )
strcpy(v7, "index.html");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s, __int64 a2, char *a3)
{
char *v4; // rax
char *v5; // r12
__int64 result; // rax
char *v7; // rbx
size_t v8; // r8

if ( strstr(s, "cgi-bin") )
{
v4 = strchr(s, 63);
v5 = v4;
if ( v4 )
{
strcpy(a3, v4 + 1);
*v5 = 0;
}
else
{
*a3 = 0;
}
*(_BYTE *)a2 = 46;
strcpy((char *)(a2 + 1), s);
result = 1LL;
}
else
{
*a3 = 0;
*(_WORD *)(a2 + 4) = 29816;
*(_DWORD *)a2 = 1702113070;
v7 = stpcpy((char *)(a2 + 6), s);
v8 = strlen(s);
result = 0LL;
if ( v8 == 1 && *s == 47 )
strcpy(v7, "index.html");
}
return result;
}

original_code:
int parse_uri(char *uri, char *filename, char *cgi_args)
{
 char *ptr;
 if(!strstr(uri, "cgi-bin"))
 {
  strcpy(cgi_args, "");
  strcpy(filename, "./text");
  strcat(filename, uri);
  if(strlen(uri)==1&&uri[strlen(uri)-1] == '/')
   strcat(filename, "index.html");
  return 0;
 }
 else
 {
  ptr = strchr(uri, '?');
  if(ptr)
  {
   strcpy(cgi_args, ptr+1);
   *ptr = '\0';
  }
  else
   strcpy(cgi_args,"");
  strcpy(filename, ".");
  strcat(filename, uri);
  return 1;
 }
}

variable_types:
uri
char *

variable_types:
filename
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall handle_request(__int64 a1)
{
int v1; // eax
int v2; // ebx
__int64 result; // rax
struct stat v4; // [rsp+0h] [rbp-40A8h] BYREF
char file[8192]; // [rsp+90h] [rbp-4018h] BYREF
char src[8216]; // [rsp+2090h] [rbp-2018h] BYREF

if ( is_support_method((char *)(a1 + 8208)) )
return handle_error(a1, a1 + 8208, 1LL);
read_request_header(a1);
v1 = parse_uri((char *)(a1 + 16400), (__int64)file, src);
*(_DWORD *)(a1 + 32788) = v1;
v2 = v1;
if ( stat(file, &v4) == -1 || (unsigned int)has_the_resource_constprop_0_isra_0(v4.st_mode) )
return handle_error(a1, file, 2LL);
if ( v2 )
{
result = has_the_right_constprop_0((__int64)&v4, 1);
if ( !(_DWORD)result )
{
run_cgi(a1, file, src);
result = (__int64)strcpy((char *)(a1 + 32792), src);
}
}
else
{
result = has_the_right_constprop_0((__int64)&v4, 0);
if ( !(_DWORD)result )
result = put_static_info(a1, file, LODWORD(v4.st_size));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
int v1; // eax
int v2; // ebx
__int64 result; // rax
struct stat v4; // [rsp+0h] [rbp-40A8h] BYREF
char file[8192]; // [rsp+90h] [rbp-4018h] BYREF
char src[8216]; // [rsp+2090h] [rbp-2018h] BYREF

if ( func1((char *)(a1 + 8208)) )
return handle_error(a1, a1 + 8208, 1LL);
func7(a1);
v1 = func8((char *)(a1 + 16400), (__int64)file, src);
*(_DWORD *)(a1 + 32788) = v1;
v2 = v1;
if ( stat(file, &v4) == -1 || (unsigned int)func4(v4.st_mode) )
return handle_error(a1, file, 2LL);
if ( v2 )
{
result = func2((__int64)&v4, 1);
if ( !(_DWORD)result )
{
run_cgi(a1, file, src);
result = (__int64)strcpy((char *)(a1 + 32792), src);
}
}
else
{
result = func2((__int64)&v4, 0);
if ( !(_DWORD)result )
result = put_static_info(a1, file, LODWORD(v4.st_size));
}
return result;
}

original_code:
void handle_request(request_t *q)
{
 int need_cgi;
 struct stat mode;
 char filename[8192], cgi_args[8192];
 if(is_support_method(q->method))
 {
  handle_error(q, q->method , NO501);
  return ;
 }
 read_request_header(q);
 need_cgi = parse_uri(q->uri, filename, cgi_args);
 q->need_cgi = need_cgi;
 if(stat(filename, &mode) == -1 || has_the_resource(filename, &mode))
 {
  handle_error(q, filename, NO404);
  return;
 }
 if(need_cgi)
 {
  if(!has_the_right(filename, &mode, RUN))
  {
   run_cgi(q, filename, cgi_args);
   strcpy(q->cgi_args, cgi_args);
  }
 }
 else
 {
  if(!has_the_right(filename, &mode, READ))
   put_static_info(q, filename, mode.st_size);
 }
}

canonical_original_code:
void handle_request(struct __request_t *q)
{
 int need_cgi;
 struct stat mode;
 char filename[8192]; char cgi_args[8192];
 if(is_support_method(q->method))
 {
  handle_error(q, q->method , NO501);
  return ;
 }
 read_request_header(q);
 need_cgi = parse_uri(q->uri, filename, cgi_args);
 q->need_cgi = need_cgi;
 if(stat(filename, &mode) == -1 || has_the_resource(filename, &mode))
 {
  handle_error(q, filename, NO404);
  return;
 }
 if(need_cgi)
 {
  if(!has_the_right(filename, &mode, RUN))
  {
   run_cgi(q, filename, cgi_args);
   strcpy(q->cgi_args, cgi_args);
  }
 }
 else
 {
  if(!has_the_right(filename, &mode, READ))
   put_static_info(q, filename, mode.st_size);
 }
}

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

variable_types:
filename
char [8192]

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