decompiled_code:
int __fastcall execute_change(__int64 a1, __int64 a2, __int64 *a3)
{
int v4; // ebx
_DWORD *v5; // rax
_DWORD *v6; // r12
__int64 v7; // rbx
int v8; // er13
__int64 v9; // rax
int v10; // eax
__int64 v11; // rax

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

canonical_decompiled_code:
int __fastcall func13(__int64 a1, __int64 a2, __int64 *a3)
{
int v4; // ebx
_DWORD *v5; // rax
_DWORD *v6; // r12
__int64 v7; // rbx
int v8; // er13
__int64 v9; // rax
int v10; // eax
__int64 v11; // rax

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

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 *


substr

repo: pjherring/task

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

v4 = malloc(0xAuLL);
v5 = a3 + 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], a3);
*((_BYTE *)v4 + a3) = 0;
return result;
}

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

v4 = malloc(0xAuLL);
v5 = a3 + 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], a3);
*((_BYTE *)v4 + a3) = 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]

return_type: char *


stroccur

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

v2 = strstr(a1, a2);
if ( !v2 )
return 0;
v3 = 0;
do
{
++v3;
v2 = strstr(v2 + 1, a2);
}
while ( v2 );
return v3;
}

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

v2 = strstr(a1, a2);
if ( !v2 )
return 0;
v3 = 0;
do
{
++v3;
v2 = strstr(v2 + 1, a2);
}
while ( v2 );
return v3;
}

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 *

return_type: int


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

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

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

for ( result = (unsigned __int8)*a1; (_BYTE)result; result = (unsigned __int8)*a1 )
{
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

return_type: void


strtodigit

decompiled_code:
__int64 __fastcall strtodigit(char *a1)
{
char v1; // al
char *v2; // rdi
int v3; // esi
unsigned int v4; // ecx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = a1 + 1;
v3 = 0;
v4 = 0;
do
{
if ( (unsigned __int8)(v1 - 48) <= 9u )
{
v4 = v1 + 10 * v4 - 48;
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; // esi
unsigned int v4; // ecx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = a1 + 1;
v3 = 0;
v4 = 0;
do
{
if ( (unsigned __int8)(v1 - 48) <= 9u )
{
v4 = v1 + 10 * v4 - 48;
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; // al
char *v2; // rbx
int v3; // ebp
int v4; // er12
char *v5; // rax
size_t v6; // rbp
char *v7; // r12
char *result; // rax

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

canonical_decompiled_code:
char *__fastcall func5(char *s)
{
char v1; // al
char *v2; // rbx
int v3; // ebp
int v4; // er12
char *v5; // rax
size_t v6; // rbp
char *v7; // r12
char *result; // rax

v1 = *s;
if ( *s == 32 )
{
v2 = s;
v3 = 0;
do
{
++v2;
++v3;
v1 = *v2;
}
while ( *v2 == 32 );
}
else
{
v2 = s;
v3 = 0;
}
v4 = 0;
if ( v1 )
{
v5 = &s[(int)(strlen(s) - 1)];
if ( *v5 == 32 )
{
do
{
++v4;
--v5;
}
while ( *v5 == 32 );
}
else
{
v4 = 0;
}
}
v6 = strlen(s) - v3 - v4;
v7 = (char *)malloc(v6 + 1);
result = strncpy(v7, v2, v6);
v7[v6] = 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);
}

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


decompiled_code:
__int64 __fastcall list_append(__int64 a1, __int64 a2)
{
int v2; // eax
int v3; // eax
__int64 result; // rax
int v5; // ebp
__int64 v6; // [rsp+0h] [rbp-68h] BYREF

if ( !a1 )
{
putchar(10);
print_stack(&v6, 10LL);
putchar(10);
printf("Assertion failed");
putchar(10);
_assert_fail("self != ((void *)0)", "src/list.c", 0x3Fu, "list_append");
}
v2 = *(_DWORD *)(a1 + 20);
if ( v2 == *(_DWORD *)(a1 + 16) )
{
v5 = 2 * v2;
*(_QWORD *)a1 = realloc(*(void **)a1, kVoidPointerSize * 2 * v2);
*(_DWORD *)(a1 + 20) = v5;
}
*(_QWORD *)(*(_QWORD *)a1 + 8LL * *(int *)(a1 + 16)) = a2;
v3 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = *(_QWORD *)a1 + 8LL * v3;
result = (unsigned int)(v3 + 1);
*(_DWORD *)(a1 + 16) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // eax
int v3; // eax
__int64 result; // rax
int v5; // ebp
__int64 v6; // [rsp+0h] [rbp-68h] BYREF

if ( !a1 )
{
putchar(10);
print_stack(&v6, 10LL);
putchar(10);
printf("Assertion failed");
putchar(10);
_assert_fail("self != ((void *)0)", "src/list.c", 0x3Fu, "list_append");
}
v2 = *(_DWORD *)(a1 + 20);
if ( v2 == *(_DWORD *)(a1 + 16) )
{
v5 = 2 * v2;
*(_QWORD *)a1 = realloc(*(void **)a1, kVoidPointerSize * 2 * v2);
*(_DWORD *)(a1 + 20) = v5;
}
*(_QWORD *)(*(_QWORD *)a1 + 8LL * *(int *)(a1 + 16)) = a2;
v3 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = *(_QWORD *)a1 + 8LL * v3;
result = (unsigned int)(v3 + 1);
*(_DWORD *)(a1 + 16) = result;
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;
while ( *v3 != a2 )
{
result = (unsigned int)(result + 1);
++v3;
if ( (_DWORD)result == v2 )
return (unsigned int)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;
while ( *v3 != a2 )
{
result = (unsigned int)(result + 1);
++v3;
if ( (_DWORD)result == v2 )
return (unsigned int)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
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v1; // rdx
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

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

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(__int64 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(__int64 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(char *a1, unsigned int a2)
{
char v2; // dl
char *v3; // rdi
unsigned int v4; // eax

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

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

v2 = *a1;
if ( *a1 )
{
v3 = a1 + 1;
v4 = 0;
do
{
v4 = v2 + 31 * v4;
v2 = *v3++;
}
while ( v2 );
}
else
{
v4 = 0;
}
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:
_DWORD *Dict()
{
_DWORD *v0; // rbx

v0 = malloc(0x10uLL);
*(_QWORD *)v0 = calloc(0x64uLL, 0x20uLL);
v0[2] = 100;
v0[3] = 0;
return v0;
}

canonical_decompiled_code:
_DWORD *func1()
{
_DWORD *v0; // rbx

v0 = malloc(0x10uLL);
*(_QWORD *)v0 = calloc(0x64uLL, 0x20uLL);
v0[2] = 100;
v0[3] = 0;
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 v1; // rbx

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

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 3) > 0 )
{
v1 = 0LL;
do
free(*(void **)(*(_QWORD *)ptr + 8 * v1++));
while ( *((_DWORD *)ptr + 3) > (int)v1 );
}
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, char *a2)
{
__int64 result; // rax

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

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

result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * (unsigned int)func0(a2, *(_DWORD *)(a1 + 8)));
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-D0h] 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-D0h] 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

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 )
puts(v4[i]);
}
free(v4);
}

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

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 )
puts(v4[i]);
}
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;
qmemcpy(haystack, "image/jpeg", 10);
}
else
{
result = 0x616C702F74786574LL;
qmemcpy(haystack, "text/plain", 10);
}
haystack[10] = 0;
}
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;
qmemcpy(haystack, "image/jpeg", 10);
}
else
{
result = 0x616C702F74786574LL;
qmemcpy(haystack, "text/plain", 10);
}
haystack[10] = 0;
}
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, unsigned int a3)
{
size_t v4; // rax
int v5; // er13
void *v6; // r12
char v8[8192]; // [rsp+0h] [rbp-4028h] BYREF
char v9[8232]; // [rsp+2000h] [rbp-2028h] BYREF

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

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

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

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


decompiled_code:
__int64 __fastcall io_read(__int64 a1, void *a2, unsigned __int64 a3)
{
int v4; // er14
void *v5; // rbp
int v6; // eax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 4);
if ( v4 > 0 )
{
LABEL_7:
if ( v4 >= a3 )
v4 = a3;
memcpy(a2, *(const void **)(a1 + 8), v4);
*(_QWORD *)(a1 + 8) += v4;
*(_DWORD *)(a1 + 4) -= v4;
result = v4;
}
else
{
v5 = (void *)(a1 + 16);
while ( 1 )
{
v6 = read(*(_DWORD *)a1, v5, 0x2000uLL);
v4 = v6;
*(_DWORD *)(a1 + 4) = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( v6 )
{
*(_QWORD *)(a1 + 8) = v5;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, void *a2, unsigned __int64 a3)
{
int v4; // er14
void *v5; // rbp
int v6; // eax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 4);
if ( v4 > 0 )
{
LABEL_7:
if ( v4 >= a3 )
v4 = a3;
memcpy(a2, *(const void **)(a1 + 8), v4);
*(_QWORD *)(a1 + 8) += v4;
*(_DWORD *)(a1 + 4) -= v4;
result = v4;
}
else
{
v5 = (void *)(a1 + 16);
while ( 1 )
{
v6 = read(*(_DWORD *)a1, v5, 0x2000uLL);
v4 = v6;
*(_DWORD *)(a1 + 4) = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( v6 )
{
*(_QWORD *)(a1 + 8) = v5;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

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

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

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

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
result = a1 + 16;
*(_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 v4; // rbx
__int64 v6; // rax

v3 = a3;
v4 = a3;
if ( a3 <= 0 )
return v3 - v4;
while ( 1 )
{
v6 = io_read(a1, a2, v4);
if ( v6 < 0 )
break;
if ( !v6 )
return v3 - v4;
LABEL_5:
v4 -= v6;
a2 += v6;
if ( v4 <= 0 )
return v3 - v4;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_5;
}
return -1LL;
}

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

v3 = a3;
v4 = a3;
if ( a3 <= 0 )
return v3 - v4;
while ( 1 )
{
v6 = func0(a1, a2, v4);
if ( v6 < 0 )
break;
if ( !v6 )
return v3 - v4;
LABEL_5:
v4 -= v6;
a2 += v6;
if ( v4 <= 0 )
return v3 - v4;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_5;
}
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)
{
int v3; // er12
__int64 v4; // r14
signed __int64 v6; // rbx
ssize_t v7; // rax

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

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

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

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
struct epoll_event v14[100]; // [rsp+0h] [rbp-518h] BYREF
struct epoll_event event; // [rsp+4B4h] [rbp-64h] BYREF
struct sockaddr addr; // [rsp+4C0h] [rbp-58h] BYREF
socklen_t addr_len[15]; // [rsp+4DCh] [rbp-3Ch] 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 )
{
addr_len[0] = 16;
v10 = epoll_wait(v9, v14, 100, -1);
if ( v10 == -1 )
break;
if ( v10 > 0 )
{
v11 = &v14[0].data;
v12 = &v14[(unsigned int)(v10 - 1) + 1].data;
do
{
if ( v11->fd == v7 )
{
v13 = accept(v7, &addr, addr_len);
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
struct epoll_event v14[100]; // [rsp+0h] [rbp-518h] BYREF
struct epoll_event event; // [rsp+4B4h] [rbp-64h] BYREF
struct sockaddr addr; // [rsp+4C0h] [rbp-58h] BYREF
socklen_t addr_len[15]; // [rsp+4DCh] [rbp-3Ch] 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 )
{
addr_len[0] = 16;
v10 = epoll_wait(v9, v14, 100, -1);
if ( v10 == -1 )
break;
if ( v10 > 0 )
{
v11 = &v14[0].data;
v12 = &v14[(unsigned int)(v10 - 1) + 1].data;
do
{
if ( v11->fd == v7 )
{
v13 = accept(v7, &addr, addr_len);
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 func4(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(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 < 0 )
{
close(v1);
v2 = -1;
}
else if ( setsockopt(v1, 1, 2, optval, 4u) < 0 )
{
close(v2);
v2 = -1;
}
else
{
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_QWORD *)&v4.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)v4.sa_data = _bswap_16(a1);
if ( bind(v2, &v4, 0x10u) < 0 || listen(v2, 1024) < 0 )
{
close(v2);
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 < 0 )
{
close(v1);
v2 = -1;
}
else if ( setsockopt(v1, 1, 2, optval, 4u) < 0 )
{
close(v2);
v2 = -1;
}
else
{
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_QWORD *)&v4.sa_data[2] = (unsigned int)func2(0);
*(_WORD *)v4.sa_data = func3(a1);
if ( bind(v2, &v4, 0x10u) < 0 || listen(v2, 1024) < 0 )
{
close(v2);
v2 = -1;
}
}
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
char v4[8192]; // [rsp+0h] [rbp-8018h] BYREF
char v5[8192]; // [rsp+2000h] [rbp-6018h] BYREF
char src[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), v7, 0x2000LL);
_isoc99_sscanf(v7, "%s %s %s", src, v5, v4);
strcpy((char *)(a1 + 8208), src);
strcpy((char *)(a1 + 24592), v4);
return strcpy((char *)(a1 + 16400), v5);
}

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:
bool __fastcall read_request_header(__int64 a1)
{
bool v1; // zf
bool result; // al
char v3[8216]; // [rsp+0h] [rbp-2018h] BYREF

io_readline(*(_QWORD *)(a1 + 8), v3, 0x2000LL);
while ( 1 )
{
v1 = strcmp(v3, "\r\n") == 0;
result = !v1;
if ( v1 )
break;
io_readline(*(_QWORD *)(a1 + 8), v3, 0x2000LL);
strcpy((char *)(a1 + 40984), v3);
}
return result;
}

canonical_decompiled_code:
bool __fastcall func7(__int64 a1)
{
bool v1; // zf
bool result; // al
char v3[8216]; // [rsp+0h] [rbp-2018h] BYREF

io_readline(*(_QWORD *)(a1 + 8), v3, 0x2000LL);
while ( 1 )
{
v1 = strcmp(v3, "\r\n") == 0;
result = !v1;
if ( v1 )
break;
io_readline(*(_QWORD *)(a1 + 8), v3, 0x2000LL);
strcpy((char *)(a1 + 40984), v3);
}
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, char *a2, char *dest)
{
char *v4; // rax
char *v5; // r12
__int64 result; // rax
size_t v7; // rdx

if ( strstr(s, "cgi-bin") )
{
v4 = strchr(s, 63);
v5 = v4;
if ( v4 )
{
strcpy(dest, v4 + 1);
*v5 = 0;
}
else
{
*dest = 0;
}
*(_WORD *)a2 = 46;
strcat(a2, s);
result = 1LL;
}
else
{
*dest = 0;
strcpy(a2, "./text");
strcat(a2, s);
v7 = strlen(s);
result = 0LL;
if ( v7 == 1 && *s == 47 )
{
strcat(a2, "index.html");
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s, char *a2, char *dest)
{
char *v4; // rax
char *v5; // r12
__int64 result; // rax
size_t v7; // rdx

if ( strstr(s, "cgi-bin") )
{
v4 = strchr(s, 63);
v5 = v4;
if ( v4 )
{
strcpy(dest, v4 + 1);
*v5 = 0;
}
else
{
*dest = 0;
}
*(_WORD *)a2 = 46;
strcat(a2, s);
result = 1LL;
}
else
{
*dest = 0;
strcpy(a2, "./text");
strcat(a2, s);
v7 = strlen(s);
result = 0LL;
if ( v7 == 1 && *s == 47 )
{
strcat(a2, "index.html");
result = 0LL;
}
}
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; // ebp
__int64 result; // rax
char v3[8192]; // [rsp+0h] [rbp-40A8h] BYREF
char file[8192]; // [rsp+2000h] [rbp-20A8h] BYREF
struct stat v5; // [rsp+4000h] [rbp-A8h] 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), file, v3);
*(_DWORD *)(a1 + 32788) = v1;
if ( stat(file, &v5) == -1 || (unsigned int)has_the_resource((__int64)file, (__int64)&v5) )
return handle_error(a1, file, 2LL);
if ( v1 )
{
result = has_the_right((__int64)file, (__int64)&v5, 1);
if ( !(_DWORD)result )
{
run_cgi(a1, file, v3);
result = (__int64)strcpy((char *)(a1 + 32792), v3);
}
}
else
{
result = has_the_right((__int64)file, (__int64)&v5, 0);
if ( !(_DWORD)result )
result = put_static_info(a1, file, LODWORD(v5.st_size));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
int v1; // ebp
__int64 result; // rax
char v3[8192]; // [rsp+0h] [rbp-40A8h] BYREF
char file[8192]; // [rsp+2000h] [rbp-20A8h] BYREF
struct stat v5; // [rsp+4000h] [rbp-A8h] BYREF

if ( func4((char *)(a1 + 8208)) )
return handle_error(a1, a1 + 8208, 1LL);
func7(a1);
v1 = func8((char *)(a1 + 16400), file, v3);
*(_DWORD *)(a1 + 32788) = v1;
if ( stat(file, &v5) == -1 || (unsigned int)func0((__int64)file, (__int64)&v5) )
return handle_error(a1, file, 2LL);
if ( v1 )
{
result = func1((__int64)file, (__int64)&v5, 1);
if ( !(_DWORD)result )
{
run_cgi(a1, file, v3);
result = (__int64)strcpy((char *)(a1 + 32792), v3);
}
}
else
{
result = func1((__int64)file, (__int64)&v5, 0);
if ( !(_DWORD)result )
result = put_static_info(a1, file, LODWORD(v5.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]; }


decompiled_code:
__int64 __fastcall handle_error(__int64 a1, const char *a2, int a3)
{
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v9[8192]; // [rsp+0h] [rbp-A018h] BYREF
char v10[8192]; // [rsp+2000h] [rbp-8018h] BYREF
int v11[2048]; // [rsp+4000h] [rbp-6018h] BYREF
char s[8192]; // [rsp+6000h] [rbp-4018h] BYREF
char v13[8216]; // [rsp+8000h] [rbp-2018h] BYREF

strcpy(s, "<html><title>XMJ error</title>\r\n");
switch ( a3 )
{
case 2:
v11[0] = 3420212;
strcpy(v10, "Counldnt find the resource file");
strcpy(v9, "Not found");
break;
case 3:
v11[0] = 3354676;
strcpy(v10, "The read/run operation is denied");
strcpy(v9, "Forbidden");
break;
case 1:
v11[0] = 3223605;
strcpy(v10, "We doesnt implement the method");
strcpy(v9, "Not Implemented");
break;
}
sprintf(s, "%s%s: %s\r\n", s, (const char *)v11, v9);
sprintf(s, "%s<p>%s : %s</p>\r\n", s, v10, a2);
sprintf(v13, "%s %s %s\r\n", (const char *)(a1 + 24592), (const char *)v11, v9);
v3 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v3);
strcpy(v13, "Content-type: text/html\r\n");
v4 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v4);
v5 = strlen(s);
sprintf(v13, "Content-length: %d\r\n\r\n", v5);
v6 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v6);
v7 = strlen(s);
return io_writen(*(_QWORD *)(a1 + 8), s, v7);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, int a3)
{
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v9[8192]; // [rsp+0h] [rbp-A018h] BYREF
char v10[8192]; // [rsp+2000h] [rbp-8018h] BYREF
int v11[2048]; // [rsp+4000h] [rbp-6018h] BYREF
char s[8192]; // [rsp+6000h] [rbp-4018h] BYREF
char v13[8216]; // [rsp+8000h] [rbp-2018h] BYREF

strcpy(s, "<html><title>XMJ error</title>\r\n");
switch ( a3 )
{
case 2:
v11[0] = 3420212;
strcpy(v10, "Counldnt find the resource file");
strcpy(v9, "Not found");
break;
case 3:
v11[0] = 3354676;
strcpy(v10, "The read/run operation is denied");
strcpy(v9, "Forbidden");
break;
case 1:
v11[0] = 3223605;
strcpy(v10, "We doesnt implement the method");
strcpy(v9, "Not Implemented");
break;
}
sprintf(s, "%s%s: %s\r\n", s, (const char *)v11, v9);
sprintf(s, "%s<p>%s : %s</p>\r\n", s, v10, a2);
sprintf(v13, "%s %s %s\r\n", (const char *)(a1 + 24592), (const char *)v11, v9);
v3 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v3);
strcpy(v13, "Content-type: text/html\r\n");
v4 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v4);
v5 = strlen(s);
sprintf(v13, "Content-length: %d\r\n\r\n", v5);
v6 = strlen(v13);
io_writen(*(_QWORD *)(a1 + 8), v13, v6);
v7 = strlen(s);
return io_writen(*(_QWORD *)(a1 + 8), s, v7);
}

original_code:
void handle_error(request_t *q, char *name, int errno)
{
 char buf[8192], body[8192],errnum[8192],msg[8192],title[8192];
 sprintf(body, "<html><title>XMJ error</title>\r\n");
 switch(errno)
 {
 case NO501:
  strcpy(errnum,"501");
  strcpy(msg,"We doesnt implement the method");
  strcpy(title,"Not Implemented");
  break;
 case NO403:
  strcpy(errnum,"403");
  strcpy(msg,"The read/run operation is denied");
  strcpy(title,"Forbidden");
  break;
 case NO404:
  strcpy(errnum,"404");
  strcpy(msg,"Counldnt find the resource file");
  strcpy(title,"Not found");
  break;
 }
 sprintf(body,"%s%s: %s\r\n",body,errnum,title);
 sprintf(body,"%s<p>%s : %s</p>\r\n",body, msg, name);
 sprintf(buf, "%s %s %s\r\n",q->ver,errnum,title);
 io_writen(q->io, buf, strlen(buf));
 sprintf(buf, "Content-type: text/html\r\n");
 io_writen(q->io, buf, strlen(buf));
 sprintf(buf, "Content-length: %d\r\n\r\n",(int)strlen(body));
 io_writen(q->io, buf, strlen(buf));
 io_writen(q->io, body, strlen(body));
}

canonical_original_code:
void handle_error(struct __request_t *q, char *name, int errno)
{
 char buf[8192]; char body[8192]; char errnum[8192]; char msg[8192]; char title[8192];
 sprintf(body, "<html><title>XMJ error</title>\r\n");
 switch(errno)
 {
 case NO501:
  strcpy(errnum,"501");
  strcpy(msg,"We doesnt implement the method");
  strcpy(title,"Not Implemented");
  break;
 case NO403:
  strcpy(errnum,"403");
  strcpy(msg,"The read/run operation is denied");
  strcpy(title,"Forbidden");
  break;
 case NO404:
  strcpy(errnum,"404");
  strcpy(msg,"Counldnt find the resource file");
  strcpy(title,"Not found");
  break;
 }
 sprintf(body,"%s%s: %s\r\n",body,errnum,title);
 sprintf(body,"%s<p>%s : %s</p>\r\n",body, msg, name);
 sprintf(buf, "%s %s %s\r\n",q->ver,errnum,title);
 io_writen(q->io, buf, strlen(buf));
 sprintf(buf, "Content-type: text/html\r\n");
 io_writen(q->io, buf, strlen(buf));
 sprintf(buf, "Content-length: %d\r\n\r\n",(int)strlen(body));
 io_writen(q->io, buf, strlen(buf));
 io_writen(q->io, body, strlen(body));
}

variable_types:
name
char *

variable_types: errno int

variable_types:
body
char [8192]

variable_types:
errnum
char [8192]

variable_types:
msg
char [8192]

variable_types:
title
char [8192]


debugPrint

repo: pkuderov/farach-suffix-tree

decompiled_code:
int __fastcall debugPrint(char *format)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, format, 0LL);
result = fflush(stdout);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *format)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, format, 0LL);
result = fflush(stdout);
}
return result;
}

original_code:
inline void debugPrint(const char *s)
{
    do { if (DEBUG) { fprintf(stdout, s, ((void *)0)); fflush(stdout); }} while (0);
}

canonical_original_code:
inline void debugPrint(char *s)
{
    do { if (DEBUG) { fprintf(stdout, s, ((void *)0)); fflush(stdout); }} while (0);
}

variable_types:
s
char *


debugArr

decompiled_code:
int __fastcall debugArr(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

debugPrint("[");
if ( a2 > 0 )
{
if ( DEBUG )
{
fprintf(stdout, "%d", *a1);
fflush(stdout);
}
if ( a2 > 1 )
{
v2 = a1 + 1;
do
{
if ( DEBUG )
{
fprintf(stdout, ", %d", *v2);
fflush(stdout);
}
++v2;
}
while ( v2 != &a1[a2 - 2 + 2] );
}
}
return debugPrint("]\n");
}

canonical_decompiled_code:
int __fastcall func1(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

func0("[");
if ( a2 > 0 )
{
if ( DEBUG )
{
fprintf(stdout, "%d", *a1);
fflush(stdout);
}
if ( a2 > 1 )
{
v2 = a1 + 1;
do
{
if ( DEBUG )
{
fprintf(stdout, ", %d", *v2);
fflush(stdout);
}
++v2;
}
while ( v2 != &a1[a2 - 2 + 2] );
}
}
return func0("]\n");
}

original_code:
void debugArr(int *a, int n)
{
    debugPrint("[");
    if (n > 0)
    {
        do { if (DEBUG) { fprintf(stdout, "%d", a[0]); fflush(stdout); }} while (0);
        for (int i = 1; i < n; i++)
        {
            do { if (DEBUG) { fprintf(stdout, ", %d", a[i]); fflush(stdout); }} while (0);
        }
    }
    debugPrint("]\n");
}

variable_types:
a
int *

variable_types: n int

variable_types: i int


debugInt

decompiled_code:
int __fastcall debugInt(unsigned int a1)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, "%d\n", a1);
result = fflush(stdout);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, "%d\n", a1);
result = fflush(stdout);
}
return result;
}

original_code:
inline void debugInt(int x)
{
    do { if (DEBUG) { fprintf(stdout, "%d\n", x); fflush(stdout); }} while (0);
}

variable_types: x int


debugIntVar

decompiled_code:
int __fastcall debugIntVar(unsigned int a1, const char *a2)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, "%s = %d\n", a2, a1);
result = fflush(stdout);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(unsigned int a1, const char *a2)
{
int result; // eax

if ( DEBUG )
{
fprintf(stdout, "%s = %d\n", a2, a1);
result = fflush(stdout);
}
return result;
}

original_code:
inline void debugIntVar(int x, const char *s)
{
    do { if (DEBUG) { fprintf(stdout, "%s = %d\n", s, x); fflush(stdout); }} while (0);
}

canonical_original_code:
inline void debugIntVar(int x, char *s)
{
    do { if (DEBUG) { fprintf(stdout, "%s = %d\n", s, x); fflush(stdout); }} while (0);
}


ConcatStrings

decompiled_code:
char *__fastcall ConcatStrings(char *src, char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx
size_t v5; // rax

v2 = strlen(src);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
strcpy(v4, src);
v5 = strlen(src);
strcpy(&v4[v5], a2);
return v4;
}

canonical_decompiled_code:
char *__fastcall func4(char *src, char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx
size_t v5; // rax

v2 = strlen(src);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
strcpy(v4, src);
v5 = strlen(src);
strcpy(&v4[v5], a2);
return v4;
}

original_code:
char *ConcatStrings(const char *s1, const char *s2)
{
    char *s = malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(s, s1);
    strcpy(s + strlen(s1), s2);
    return s;
}

canonical_original_code:
char *ConcatStrings(char *s1, char *s2)
{
    char *s = malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(s, s1);
    strcpy(s + strlen(s1), s2);
    return s;
}


MemFree

decompiled_code:
void __fastcall MemFree(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func5(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
inline void MemFree(void *a)
{
    if (a) free(a);
}

variable_types:
a
void *


decompiled_code:
__int64 __fastcall Log2(int a1)
{
unsigned int v1; // ecx

if ( a1 <= 1 )
return 0;
v1 = 0;
do
++v1;
while ( 1 << v1 < a1 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
unsigned int v1; // ecx

if ( a1 <= 1 )
return 0;
v1 = 0;
do
++v1;
while ( 1 << v1 < a1 );
return v1;
}

original_code:
int Log2(int x)
{
 int res = 0;
 while (1 << res < x)
        res++;
 return res;
}

variable_types: res int


Swap

decompiled_code:
__int64 __fastcall Swap(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void Swap(int *x, int *y)
{
    int t = *x;
    *x = *y;
    *y = t;
}

variable_types:
x
int *

variable_types:
y
int *

variable_types: t int


FreeDynamicArray

decompiled_code:
void __fastcall FreeDynamicArray(void **a1)
{
if ( a1 )
{
MemFree(a1[1]);
MemFree(a1);
}
}

canonical_decompiled_code:
void __fastcall func8(void **a1)
{
if ( a1 )
{
func5(a1[1]);
func5(a1);
}
}

original_code:
void FreeDynamicArray(DynamicArray *arr)
{
    if (!arr)
        return;
    MemFree(arr->a);
    MemFree(arr);
}

canonical_original_code:
void FreeDynamicArray(struct _dynamicArray *arr)
{
    if (!arr)
        return;
    MemFree(arr->a);
    MemFree(arr);
}

variable_types: struct _dynamicArray { int count; int *a; } *


LastInDynamicArray

decompiled_code:
__int64 __fastcall LastInDynamicArray(int *a1)
{
return *((_QWORD *)a1 + 1) + 4LL * *a1 - 4;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1)
{
return *((_QWORD *)a1 + 1) + 4LL * *a1 - 4;
}

original_code:
inline int *LastInDynamicArray(DynamicArray *arr)
{
    return (arr->a + arr->count - 1);
}

canonical_original_code:
inline int *LastInDynamicArray(struct _dynamicArray *arr)
{
    return (arr->a + arr->count - 1);
}

return_type: int *

user_defined_types: struct _dynamicArray { int count; int *a; }


PushToDynamicArray

decompiled_code:
__int64 __fastcall PushToDynamicArray(__int64 a1, int a2)
{
int v2; // eax
__int64 result; // rax
void *v4; // rax

v2 = *(_DWORD *)(a1 + 4);
if ( *(_DWORD *)a1 == v2 )
{
v4 = realloc(*(void **)(a1 + 8), 8LL * v2);
if ( v4 )
{
*(_QWORD *)(a1 + 8) = v4;
*(_DWORD *)(a1 + 4) *= 2;
}
}
result = *(_QWORD *)(a1 + 8);
*(_DWORD *)(result + 4LL * (int)(*(_DWORD *)a1)++) = a2;
return result;
}

original_code:
void PushToDynamicArray(DynamicArray *arr, int x)
{
    if (arr->count == arr->capacity)
    {
        int *t = realloc(arr->a, 2 * arr->capacity * sizeof *t);
        if (t)
        {
            arr->a = t;
            arr->capacity = arr->capacity << 1;
        }
    }
    arr->a[arr->count] = x;
    ++(arr->count);
}

canonical_original_code:
void PushToDynamicArray(struct _dynamicArray *arr, int x)
{
    if (arr->count == arr->capacity)
    {
        int *t = realloc(arr->a, 2 * arr->capacity * sizeof *t);
        if (t)
        {
            arr->a = t;
            arr->capacity = arr->capacity << 1;
        }
    }
    arr->a[arr->count] = x;
    ++(arr->count);
}

variable_types:
t
int *


PopFromDynamicArray

decompiled_code:
__int64 __fastcall PopFromDynamicArray(__int64 a1)
{
__int64 v1; // rdx
int v2; // eax

v1 = *(_QWORD *)(a1 + 8);
v2 = *(_DWORD *)a1 - 1;
*(_DWORD *)a1 = v2;
return *(unsigned int *)(v1 + 4LL * v2);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
__int64 v1; // rdx
int v2; // eax

v1 = *(_QWORD *)(a1 + 8);
v2 = *(_DWORD *)a1 - 1;
*(_DWORD *)a1 = v2;
return *(unsigned int *)(v1 + 4LL * v2);
}

original_code:
int PopFromDynamicArray(DynamicArray *arr)
{
    return arr->a[--(arr->count)];
}

canonical_original_code:
int PopFromDynamicArray(struct _dynamicArray *arr)
{
    return arr->a[--(arr->count)];
}


FreeDynamicQueue

decompiled_code:
void __fastcall FreeDynamicQueue(void **a1)
{
if ( a1 )
{
MemFree(a1[2]);
MemFree(a1);
}
}

canonical_decompiled_code:
void __fastcall func12(void **a1)
{
if ( a1 )
{
func5(a1[2]);
func5(a1);
}
}

original_code:
void FreeDynamicQueue(DynamicQueue *queue)
{
    if (!queue)
        return;
    MemFree(queue->a);
    MemFree(queue);
}

canonical_original_code:
void FreeDynamicQueue(struct _dynamicQueue *queue)
{
    if (!queue)
        return;
    MemFree(queue->a);
    MemFree(queue);
}

variable_types: struct _dynamicQueue { int top; int *a; } *


PushToDynamicQueue

original_code:
void PushToDynamicQueue(DynamicQueue *queue, int x)
{
    if (queue->count == queue->capacity)
    {
        int *t = realloc(queue->a, 2 * queue->capacity * sizeof *t);
        if (t)
        {
            queue->a = t;
            if (queue->top <= queue->bottom)
            {
                if (queue->top < (queue->capacity - queue->bottom))
                {
                    memcpy(queue->a + queue->capacity, queue->a, queue->top * sizeof *queue->a);
                    queue->top += queue->capacity;
                }
                else
                {
                    memcpy(queue->a + queue->bottom + queue->capacity, queue->a + queue->bottom, (queue->capacity - queue->bottom) * sizeof *queue->a);
                    queue->bottom += queue->capacity;
                }
            }
            queue->capacity = queue->capacity << 1;
        }
    }
    queue->a[queue->top] = x;
    ++(queue->top);
    ++(queue->count);
    if (queue->top == queue->capacity)
        queue->top = 0;
}

canonical_original_code:
void PushToDynamicQueue(struct _dynamicQueue *queue, int x)
{
    if (queue->count == queue->capacity)
    {
        int *t = realloc(queue->a, 2 * queue->capacity * sizeof *t);
        if (t)
        {
            queue->a = t;
            if (queue->top <= queue->bottom)
            {
                if (queue->top < (queue->capacity - queue->bottom))
                {
                    memcpy(queue->a + queue->capacity, queue->a, queue->top * sizeof *queue->a);
                    queue->top += queue->capacity;
                }
                else
                {
                    memcpy(queue->a + queue->bottom + queue->capacity, queue->a + queue->bottom, (queue->capacity - queue->bottom) * sizeof *queue->a);
                    queue->bottom += queue->capacity;
                }
            }
            queue->capacity = queue->capacity << 1;
        }
    }
    queue->a[queue->top] = x;
    ++(queue->top);
    ++(queue->count);
    if (queue->top == queue->capacity)
        queue->top = 0;
}

user_defined_types: struct _dynamicQueue { int top; int *a; }


PeekToDynamicQueue

decompiled_code:
__int64 __fastcall PeekToDynamicQueue(__int64 a1)
{
return *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * *(int *)(a1 + 4));
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * *(int *)(a1 + 4));
}

original_code:
inline int PeekToDynamicQueue(DynamicQueue *queue)
{
    return queue->a[queue->bottom];
}

canonical_original_code:
inline int PeekToDynamicQueue(struct _dynamicQueue *queue)
{
    return queue->a[queue->bottom];
}


PopFromDynamicQueue

decompiled_code:
__int64 __fastcall PopFromDynamicQueue(_DWORD *a1)
{
unsigned int v1; // edx
int v2; // eax

v1 = PeekToDynamicQueue((__int64)a1);
v2 = a1[1] + 1;
a1[1] = v2;
--a1[2];
if ( v2 == a1[3] )
a1[1] = 0;
return v1;
}

original_code:
int PopFromDynamicQueue(DynamicQueue *queue)
{
    int x = PeekToDynamicQueue(queue);
    ++(queue->bottom);
    --(queue->count);
    if (queue->bottom == queue->capacity)
        queue->bottom = 0;
    return x;
}

canonical_original_code:
int PopFromDynamicQueue(struct _dynamicQueue *queue)
{
    int x = PeekToDynamicQueue(queue);
    ++(queue->bottom);
    --(queue->count);
    if (queue->bottom == queue->capacity)
        queue->bottom = 0;
    return x;
}


FreeDfsPosition

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

if ( a1 )
{
FreeDynamicArray(*(_QWORD *)(a1 + 32));
result = MemFree(a1);
}
return result;
}

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

if ( a1 )
{
FreeDynamicArray(*(_QWORD *)(a1 + 32));
result = MemFree(a1);
}
return result;
}

original_code:
void FreeDfsPosition(DfsPosition *p)
{
    if (!p)
        return;
    FreeDynamicArray(p->lastChild);
    MemFree(p);
}

canonical_original_code:
void FreeDfsPosition(struct _dfsPosition *p)
{
    if (!p)
        return;
    FreeDynamicArray(p->lastChild);
    MemFree(p);
}

variable_types: struct _dfsPosition { struct _suffixTree { int count; struct _suffixTreeNode { int parent; int *children; int childrenCount; } *nodes; struct _suffixArray { int n; int *lcp; int *suffixToRank; } *suffixArray; } *tree; int treeType; int *s; int ind; struct _dynamicArray { int count; int *a; } *lastChild; int lastDfsLeaf; } *

user_defined_types: struct _dfsPosition { struct _suffixTree *tree; int treeType; int *s; int ind; struct _dynamicArray *lastChild; int lastDfsLeaf; }

user_defined_types: struct _suffixTree { int count; struct _suffixTreeNode *nodes; struct _suffixArray *suffixArray; }


EndOfDfs

decompiled_code:
_BOOL8 __fastcall EndOfDfs(__int64 a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( !*(_DWORD *)(a1 + 28) )
result = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 24LL) == *(_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
return result;
}

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

result = 0LL;
if ( !*(_DWORD *)(a1 + 28) )
result = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 24LL) == *(_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
return result;
}

original_code:
inline int EndOfDfs(DfsPosition *p)
{
    return 0 == p->ind && *LastInDynamicArray(p->lastChild) == p->tree->nodes[0].childrenCount;
}

canonical_original_code:
inline int EndOfDfs(struct _dfsPosition *p)
{
    return 0 == p->ind && *LastInDynamicArray(p->lastChild) == p->tree->nodes[0].childrenCount;
}

user_defined_types: struct _suffixTreeNode { int parent; int *children; int childrenCount; }

user_defined_types: struct _suffixArray { int n; int *lcp; int *suffixToRank; }


GetChildIndex

decompiled_code:
__int64 __fastcall GetChildIndex(__int64 a1)
{
__int64 v1; // rbx

v1 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 32LL * *(int *)(a1 + 28) + 16);
return *(unsigned int *)(v1 + 4LL * *(int *)LastInDynamicArray(*(_QWORD *)(a1 + 32)));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbx

v1 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 32LL * *(int *)(a1 + 28) + 16);
return *(unsigned int *)(v1 + 4LL * *(int *)LastInDynamicArray(*(_QWORD *)(a1 + 32)));
}

original_code:
inline int GetChildIndex(DfsPosition *p)
{
    return p->tree->nodes[p->ind].children[*LastInDynamicArray(p->lastChild)];
}

canonical_original_code:
inline int GetChildIndex(struct _dfsPosition *p)
{
    return p->tree->nodes[p->ind].children[*LastInDynamicArray(p->lastChild)];
}


GetEdgeLength

decompiled_code:
__int64 __fastcall GetEdgeLength(int *a1)
{
__int64 v1; // rbp

v1 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
return (unsigned int)(*(_DWORD *)(v1 + 32LL * (int)GetChildIndex((__int64)a1) + 8) - *(_DWORD *)(v1 + 32LL * a1[7] + 8));
}

original_code:
inline int GetEdgeLength(DfsPosition *p)
{
    return p->tree->nodes[GetChildIndex(p)].depth - p->tree->nodes[p->ind].depth;
}

canonical_original_code:
inline int GetEdgeLength(struct _dfsPosition *p)
{
    return p->tree->nodes[GetChildIndex(p)].depth - p->tree->nodes[p->ind].depth;
}


GetFirstCharOfChildEdge

decompiled_code:
__int64 __fastcall GetFirstCharOfChildEdge(__int64 a1)
{
_DWORD *v1; // rax
__int64 v2; // rbp
__int64 v4; // r12

v1 = (_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
v2 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
if ( *v1 >= *(_DWORD *)(v2 + 32LL * *(int *)(a1 + 28) + 24) )
return *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * *(int *)(a1 + 24));
v4 = *(_QWORD *)(a1 + 16);
return *(unsigned int *)(v4 + 4LL * *(int *)(v2 + 32LL * (int)GetChildIndex(a1) + 4));
}

original_code:
inline int GetFirstCharOfChildEdge(DfsPosition *p)
{
    return *LastInDynamicArray(p->lastChild) < p->tree->nodes[p->ind].childrenCount
        ? p->s[p->tree->nodes[GetChildIndex(p)].from]
        : p->s[p->n];
}

canonical_original_code:
inline int GetFirstCharOfChildEdge(struct _dfsPosition *p)
{
    return *LastInDynamicArray(p->lastChild) < p->tree->nodes[p->ind].childrenCount
        ? p->s[p->tree->nodes[GetChildIndex(p)].from]
        : p->s[p->n];
}


CompareAndSwapDfsPositions

decompiled_code:
_BOOL8 __fastcall CompareAndSwapDfsPositions(__int64 *a1, __int64 *a2)
{
int *v3; // rdi
__int64 v4; // r12
int v5; // edx
int v6; // eax
_BOOL4 v7; // ecx
int v9; // er13
int v10; // er12

v3 = (int *)*a1;
v4 = *a2;
v5 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)v3 + 16LL) + 32LL * v3[7] + 8);
v6 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)*a2 + 16LL) + 32LL * *(int *)(*a2 + 28) + 8);
if ( v5 == v6 )
{
v9 = GetFirstCharOfChildEdge((__int64)v3);
v10 = GetFirstCharOfChildEdge(v4);
if ( v9 > v10 )
SwapPositions(a1, a2);
v7 = v9 != v10;
}
else
{
v7 = 1;
if ( v5 < v6 )
{
SwapPositions(a1, a2);
v7 = 1;
}
}
return v7;
}

original_code:
int CompareAndSwapDfsPositions(DfsPosition **ppx, DfsPosition **ppy)
{
    DfsPosition *px = *ppx, *py = *ppy;
    if (px->tree->nodes[px->ind].depth == py->tree->nodes[py->ind].depth)
    {
        int cx = GetFirstCharOfChildEdge(px);
        int cy = GetFirstCharOfChildEdge(py);
        if (cx > cy)
            SwapPositions(ppx, ppy);
        return cx != cy;
    }
    else
    {
        if (px->tree->nodes[px->ind].depth < py->tree->nodes[py->ind].depth)
            SwapPositions(ppx, ppy);
        return 1;
    }
}

canonical_original_code:
int CompareAndSwapDfsPositions(struct _dfsPosition **ppx, struct _dfsPosition **ppy)
{
    struct _dfsPosition *px = *ppx; struct _dfsPosition *py = *ppy;
    if (px->tree->nodes[px->ind].depth == py->tree->nodes[py->ind].depth)
    {
        int cx = GetFirstCharOfChildEdge(px);
        int cy = GetFirstCharOfChildEdge(py);
        if (cx > cy)
            SwapPositions(ppx, ppy);
        return cx != cy;
    }
    else
    {
        if (px->tree->nodes[px->ind].depth < py->tree->nodes[py->ind].depth)
            SwapPositions(ppx, ppy);
        return 1;
    }
}

variable_types: struct _dfsPosition { struct _suffixTree { int count; struct _suffixTreeNode { int parent; int *children; int childrenCount; } *nodes; struct _suffixArray { int n; int *lcp; int *suffixToRank; } *suffixArray; } *tree; int treeType; int *s; int ind; struct _dynamicArray { int count; int *a; } *lastChild; int lastDfsLeaf; } **

variable_types:
ppy
struct _dfsPosition **

variable_types: cx int

variable_types: cy int


NextStepOfDfs

decompiled_code:
__int64 __fastcall NextStepOfDfs(__int64 a1, int a2)
{
_DWORD *v3; // rax
_DWORD *v4; // rsi
__int64 v5; // rcx
unsigned int v6; // edx
int *v7; // rax

if ( *(_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32)) < *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL)
+ 32LL * *(int *)(a1 + 28)
+ 24) )
{
*(_DWORD *)(a1 + 28) = GetChildIndex(a1);
PushToDynamicArray(*(_QWORD *)(a1 + 32), 0LL);
}
while ( 1 )
{
v4 = (_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
v5 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
v6 = *(_DWORD *)(a1 + 28);
v7 = (int *)(v5 + 32LL * (int)v6);
if ( *v4 < v7[6] )
break;
if ( !v6 )
return v6;
if ( *(_DWORD *)(v5 + 32LL * *v7 + 8) < a2 )
return 0;
*(_DWORD *)(a1 + 28) = *v7;
--**(_DWORD **)(a1 + 32);
v3 = (_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
++*v3;
}
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
_DWORD *v3; // rax
_DWORD *v4; // rsi
__int64 v5; // rcx
unsigned int v6; // edx
int *v7; // rax

if ( *(_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32)) < *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 16LL)
+ 32LL * *(int *)(a1 + 28)
+ 24) )
{
*(_DWORD *)(a1 + 28) = func2(a1);
PushToDynamicArray(*(_QWORD *)(a1 + 32), 0LL);
}
while ( 1 )
{
v4 = (_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
v5 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
v6 = *(_DWORD *)(a1 + 28);
v7 = (int *)(v5 + 32LL * (int)v6);
if ( *v4 < v7[6] )
break;
if ( !v6 )
return v6;
if ( *(_DWORD *)(v5 + 32LL * *v7 + 8) < a2 )
return 0;
*(_DWORD *)(a1 + 28) = *v7;
--**(_DWORD **)(a1 + 32);
v3 = (_DWORD *)LastInDynamicArray(*(_QWORD *)(a1 + 32));
++*v3;
}
return 1;
}

original_code:
int NextStepOfDfs(DfsPosition *p, int minDepth)
{
    if (*LastInDynamicArray(p->lastChild) < p->tree->nodes[p->ind].childrenCount)
    {
        p->ind = GetChildIndex(p);
        PushToDynamicArray(p->lastChild, 0);
    }
    while (*LastInDynamicArray(p->lastChild) >= p->tree->nodes[p->ind].childrenCount)
    {
        if (p->ind == 0 || p->tree->nodes[p->tree->nodes[p->ind].parent].depth < minDepth)
            return 0;
        p->ind = p->tree->nodes[p->ind].parent;
        --(p->lastChild->count);
        ++(*LastInDynamicArray(p->lastChild));
    }
    return 1;
}

canonical_original_code:
int NextStepOfDfs(struct _dfsPosition *p, int minDepth)
{
    if (*LastInDynamicArray(p->lastChild) < p->tree->nodes[p->ind].childrenCount)
    {
        p->ind = GetChildIndex(p);
        PushToDynamicArray(p->lastChild, 0);
    }
    while (*LastInDynamicArray(p->lastChild) >= p->tree->nodes[p->ind].childrenCount)
    {
        if (p->ind == 0 || p->tree->nodes[p->tree->nodes[p->ind].parent].depth < minDepth)
            return 0;
        p->ind = p->tree->nodes[p->ind].parent;
        --(p->lastChild->count);
        ++(*LastInDynamicArray(p->lastChild));
    }
    return 1;
}

variable_types: minDepth int


FreePrefixTree

decompiled_code:
__int64 __fastcall FreePrefixTree(__int64 a1)
{
return MemFree(*(_QWORD *)(a1 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return MemFree(*(_QWORD *)(a1 + 8));
}

original_code:
void FreePrefixTree(PrefixTree *pTree)
{
    MemFree(pTree->nodes);
}

canonical_original_code:
void FreePrefixTree(struct _prefixTree *pTree)
{
    MemFree(pTree->nodes);
}

variable_types: struct _prefixTree { int count; struct _prefixTreeNode { int rank; int children[28]; } *nodes; } *

user_defined_types: struct _prefixTree { int count; struct _prefixTreeNode *nodes; }


AllocateNextIndexInPrefixTree

decompiled_code:
__int64 __fastcall AllocateNextIndexInPrefixTree(void **a1)
{
int v1; // ebp
int v2; // eax
__int64 v3; // rcx
int i; // edx
__int64 result; // rax
void *v6; // rax

v1 = *(_DWORD *)a1;
v2 = *((_DWORD *)a1 + 1);
if ( v2 == *(_DWORD *)a1 )
{
v6 = realloc(a1[1], 232LL * v2);
if ( v6 )
{
a1[1] = v6;
*((_DWORD *)a1 + 1) *= 2;
}
else
{
puts("bad prefix tree nodes realloc");
}
}
v3 = 116LL * v1;
*(_DWORD *)((char *)a1[1] + v3) = -1;
for ( i = 0; i != 28; ++i )
{
result = (__int64)a1[1] + 4 * i;
*(_DWORD *)(v3 + result + 4) = -1;
}
++*(_DWORD *)a1;
return result;
}

original_code:
void AllocateNextIndexInPrefixTree(PrefixTree *pTree)
{
    int i = pTree->count;
    if (i == pTree->capacity)
    {
        PrefixTreeNode *t = realloc(pTree->nodes, 2 * pTree->capacity * sizeof *pTree->nodes);
        if (t)
        {
            pTree->nodes = t;
            pTree->capacity = pTree->capacity << 1;
        }
        else
            printf("bad prefix tree nodes realloc\n");
    }
    pTree->nodes[i].rank = -1;
    for (int j = 0; j < 28; j++)
        pTree->nodes[i].children[j] = -1;
    pTree->count++;
}

canonical_original_code:
void AllocateNextIndexInPrefixTree(struct _prefixTree *pTree)
{
    int i = pTree->count;
    if (i == pTree->capacity)
    {
        struct _prefixTreeNode *t = realloc(pTree->nodes, 2 * pTree->capacity * sizeof *pTree->nodes);
        if (t)
        {
            pTree->nodes = t;
            pTree->capacity = pTree->capacity << 1;
        }
        else
            printf("bad prefix tree nodes realloc\n");
    }
    pTree->nodes[i].rank = -1;
    for (int j = 0; j < 28; j++)
        pTree->nodes[i].children[j] = -1;
    pTree->count++;
}

variable_types: i int

variable_types: struct _prefixTreeNode { int rank; int children[28]; } *

variable_types: j int


InitializePrefixTree

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

*(_DWORD *)a1 = 0;
*(_DWORD *)(a1 + 4) = a2;
v2 = malloc(116LL * a2);
*(_QWORD *)(a1 + 8) = v2;
if ( v2 )
result = AllocateNextIndexInPrefixTree((void **)a1);
else
result = puts("bad malloc in InitializePrefixTree");
return result;
}

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

*(_DWORD *)a1 = 0;
*(_DWORD *)(a1 + 4) = a2;
v2 = malloc(116LL * a2);
*(_QWORD *)(a1 + 8) = v2;
if ( v2 )
result = func1((void **)a1);
else
result = puts("bad malloc in InitializePrefixTree");
return result;
}

original_code:
void InitializePrefixTree(PrefixTree *pTree, int capacity)
{
    pTree->count = 0;
    pTree->capacity = capacity;
    pTree->nodes = malloc(capacity * sizeof *pTree->nodes);
    if (pTree->nodes)
    {
        AllocateNextIndexInPrefixTree(pTree);
    }
    else
        printf("bad malloc in InitializePrefixTree\n");
}

canonical_original_code:
void InitializePrefixTree(struct _prefixTree *pTree, int capacity)
{
    pTree->count = 0;
    pTree->capacity = capacity;
    pTree->nodes = malloc(capacity * sizeof *pTree->nodes);
    if (pTree->nodes)
    {
        AllocateNextIndexInPrefixTree(pTree);
    }
    else
        printf("bad malloc in InitializePrefixTree\n");
}

variable_types: capacity int

user_defined_types: struct _prefixTreeNode { int rank; int children[28]; }


CompleteBuildingPrefixTree

decompiled_code:
__int64 __fastcall CompleteBuildingPrefixTree(__int64 a1, int a2, _DWORD *a3)
{
__int64 v4; // r12
int i; // ebx
int v6; // eax
__int64 result; // rax
__int64 v8; // rsi

v4 = 116LL * a2;
if ( *(_DWORD *)(*(_QWORD *)(a1 + 8) + v4) == 1 )
{
v6 = (*a3)++;
*(_DWORD *)(*(_QWORD *)(a1 + 8) + 116LL * a2) = v6;
}
for ( i = 0; i != 28; ++i )
{
result = *(_QWORD *)(a1 + 8) + 4LL * i;
v8 = *(unsigned int *)(v4 + result + 4);
if ( (int)v8 >= 0 )
result = CompleteBuildingPrefixTree(a1, v8, a3);
}
return result;
}

original_code:
void CompleteBuildingPrefixTree(PrefixTree *pTree, int i, int *rank)
{
    if (1 == pTree->nodes[i].rank)
        pTree->nodes[i].rank = (*rank)++;
    for (int j = 0; j < 28; j++)
    {
        if (pTree->nodes[i].children[j] >= 0)
            CompleteBuildingPrefixTree(pTree, pTree->nodes[i].children[j], rank);
    }
}

canonical_original_code:
void CompleteBuildingPrefixTree(struct _prefixTree *pTree, int i, int *rank)
{
    if (1 == pTree->nodes[i].rank)
        pTree->nodes[i].rank = (*rank)++;
    for (int j = 0; j < 28; j++)
    {
        if (pTree->nodes[i].children[j] >= 0)
            CompleteBuildingPrefixTree(pTree, pTree->nodes[i].children[j], rank);
    }
}

variable_types:
rank
int *