repo: luquanhong/tiny-openmax

decompiled_code:
int __fastcall my_listener(__int64 a1, int a2, const char *a3)
{
int result; // eax

if ( a2 == 8 || a2 == 999 )
result = printf("%s", a3);
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2, const char *a3)
{
int result; // eax

if ( a2 == 8 || a2 == 999 )
result = printf("%s", a3);
return result;
}

original_code:
static void my_listener(void* arg,int level, void* log, int bytes)
{
    switch(level){
        case 8:
        case 999:
            printf("%s", (char*)log);
        default:
            break;
    }
}

canonical_original_code:
static void my_listener(void *arg, int level, void *log, int bytes)
{
    switch(level){
        case 8:
        case 999:
            printf("%s", (char *)log);
        default:
            break;
    }
}

variable_types:
arg
void *

variable_types: level int

variable_types:
log
void *

variable_types: bytes int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char s[1032]; // [rsp+0h] [rbp-408h] BYREF

puts("hello world");
tiny_log_set_listener(my_listener, 0LL);
snprintf(s, 0x400uLL, "%s: %s--%s: %d", "DEBUG", "test/testcast-log.c", "main", 24LL);
tiny_log(s, 999LL, "hello tiny\n");
return 0;
}

original_code:
int main(int argc, char* arg[]){
    printf("hello world\n");
    tiny_log_set_listener(my_listener,((void *)0));
    do { char context[1024]; snprintf(context, 1024, "%s: %s--%s: %d", "DEBUG", "test/testcast-log.c", __FUNCTION__, 24); tiny_log(context, 999, "hello tiny\n"); } while(0);
    return 0;
}

canonical_original_code:
int main(int argc, char *arg[]){
    printf("hello world\n");
    tiny_log_set_listener(my_listener,((void *)0));
    do { char context[1024]; snprintf(context, 1024, "%s: %s--%s: %d", "DEBUG", "test/testcast-log.c", __FUNCTION__, 24); tiny_log(context, 999, "hello tiny\n"); } while(0);
    return 0;
}

variable_types: argc int

variable_types:
arg
char *[]

variable_types:
context
char [1024]

return_type: int


decompiled_code:
__int64 __fastcall tiny_log_set_level(int a1)
{
log_level = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
log_level = a1;
return 0LL;
}

original_code:
int tiny_log_set_level(int level)
{
    log_level = level;
    return 0;
}


decompiled_code:
__int64 __fastcall tiny_log_set_listener(__int64 (__fastcall *a1)(_QWORD, _QWORD, _QWORD, _QWORD), __int64 a2)
{
log_listener = a1;
log_listener_arg = a2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 (__fastcall *a1)(_QWORD, _QWORD, _QWORD, _QWORD), __int64 a2)
{
log_listener = a1;
log_listener_arg = a2;
return 0LL;
}

original_code:
int tiny_log_set_listener(tiny_log_listener_t listener, void* arg)
{
    log_listener = listener;
    log_listener_arg = arg;
    return 0;
}

canonical_original_code:
int tiny_log_set_listener(void (*listener)(void *arg, int level, void *log, int bytes), void *arg)
{
    log_listener = listener;
    log_listener_arg = arg;
    return 0;
}

variable_types: void (*)(void *arg, int level, void *log, int bytes)


decompiled_code:
__int64 tiny_log_init()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 0LL;
}

original_code:
int tiny_log_init(int bytes, int level, tiny_log_listener_t listener, void* arg)
{
    return 0;
}

canonical_original_code:
int tiny_log_init(int bytes, int level, void (*listener)(void *arg, int level, void *log, int bytes), void *arg)
{
    return 0;
}


repo: markusmo/epicminic

decompiled_code:
int __fastcall hashset_add_member(_QWORD *a1, const char *a2)
{
__int64 v3; // r13
__int64 v4; // rbx
__int64 v5; // r12
const char **v6; // rax
const char *v7; // rdi
int result; // eax

if ( !a2 )
return -1;
v3 = a1[1];
v4 = v3 & (73LL * (_QWORD)a2);
v5 = a1[3];
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
{
LABEL_5:
++a1[4];
*v6 = a2;
result = 1;
}
else
{
while ( 1 )
{
result = strcmp(v7, a2);
if ( !result )
break;
v4 = v3 & (v4 + 5009);
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
goto LABEL_5;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(_QWORD *a1, const char *a2)
{
__int64 v3; // r13
__int64 v4; // rbx
__int64 v5; // r12
const char **v6; // rax
const char *v7; // rdi
int result; // eax

if ( !a2 )
return -1;
v3 = a1[1];
v4 = v3 & (73LL * (_QWORD)a2);
v5 = a1[3];
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
{
LABEL_5:
++a1[4];
*v6 = a2;
result = 1;
}
else
{
while ( 1 )
{
result = strcmp(v7, a2);
if ( !result )
break;
v4 = v3 & (v4 + 5009);
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
goto LABEL_5;
}
}
return result;
}

original_code:
static int hashset_add_member(hashset_t set, char *item)
{
    size_t ii;
    if (item == ((void *)0)) {
        return -1;
    }
    ii = set->mask & (prime_1 * ((size_t)item));
    while ((size_t)set->items[ii] != 0 && (size_t)set->items[ii] != 1) {
        if (strcmp(set->items[ii], item) == 0) {
            return 0;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    set->nitems++;
    set->items[ii] = item;
    return 1;
}

canonical_original_code:
static int hashset_add_member(struct hashset_st *set, char *item)
{
    unsigned long ii;
    if (item == ((void *)0)) {
        return -1;
    }
    ii = set->mask & (prime_1 * ((unsigned long)item));
    while ((unsigned long)set->items[ii] != 0 && (unsigned long)set->items[ii] != 1) {
        if (strcmp(set->items[ii], item) == 0) {
            return 0;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    set->nitems++;
    set->items[ii] = item;
    return 1;
}

variable_types: struct hashset_st { unsigned long nbits; unsigned long mask; unsigned long capacity; char **items; unsigned long nitems; } *

variable_types:
item
char *

variable_types:
ii
unsigned long


original_code:
static void maybe_rehash(hashset_t set)
{
    char **old_items;
    size_t old_capacity, ii;
    if ((float)set->nitems >= (size_t)((double)set->capacity * 0.85)) {
        old_items = set->items;
        old_capacity = set->capacity;
        set->nbits++;
        set->capacity = (size_t)(1 << set->nbits);
        set->mask = set->capacity - 1;
        set->items = calloc(set->capacity, sizeof(char*));
        set->nitems = 0;
        ((set->items) ? (void) (0) : __assert_fail ("set->items", "hashset.c", 95, __extension__ __PRETTY_FUNCTION__));
        for (ii = 0; ii < old_capacity; ii++) {
            hashset_add_member(set, (void *)old_items[ii]);
        }
        free(old_items);
    }
}

canonical_original_code:
static void maybe_rehash(struct hashset_st *set)
{
    char **old_items;
    unsigned long old_capacity; unsigned long ii;
    if ((float)set->nitems >= (unsigned long)((double)set->capacity * 0.85)) {
        old_items = set->items;
        old_capacity = set->capacity;
        set->nbits++;
        set->capacity = (unsigned long)(1 << set->nbits);
        set->mask = set->capacity - 1;
        set->items = calloc(set->capacity, sizeof(char *));
        set->nitems = 0;
        ((set->items) ? (void) (0) : __assert_fail ("set->items", "hashset.c", 95, __extension__ __PRETTY_FUNCTION__));
        for (ii = 0; ii < old_capacity; ii++) {
            hashset_add_member(set, (void *)old_items[ii]);
        }
        free(old_items);
    }
}

return_type: void

user_defined_types: struct hashset_st { unsigned long nbits; unsigned long mask; unsigned long capacity; char **items; unsigned long nitems; }


decompiled_code:
__int64 __fastcall hashset_num_items(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

original_code:
size_t hashset_num_items(hashset_t set)
{
    return set->nitems;
}

canonical_original_code:
unsigned long hashset_num_items(struct hashset_st *set)
{
    return set->nitems;
}

return_type: unsigned long


decompiled_code:
void __fastcall hashset_destroy(void *ptr)
{
if ( ptr )
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
if ( ptr )
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void hashset_destroy(hashset_t set)
{
    if (set) {
        free(set->items);
    }
    free(set);
}

canonical_original_code:
void hashset_destroy(struct hashset_st *set)
{
    if (set) {
        free(set->items);
    }
    free(set);
}


decompiled_code:
_QWORD *hashset_create()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
void *v2; // rbp

v0 = calloc(1uLL, 0x28uLL);
v1 = v0;
if ( v0 )
{
*v0 = 3LL;
v0[2] = 8LL;
v0[1] = 7LL;
v2 = calloc(8uLL, 8uLL);
v1[3] = v2;
if ( v2 )
{
v1[4] = 0LL;
}
else
{
hashset_destroy(v1);
v1 = 0LL;
}
}
return v1;
}

original_code:
hashset_t hashset_create()
{
    hashset_t set = calloc(1, sizeof(struct hashset_st));
    if (set == ((void *)0)) {
        return ((void *)0);
    }
    set->nbits = 3;
    set->capacity = (size_t)(1 << set->nbits);
    set->mask = set->capacity - 1;
    set->items = calloc(set->capacity, sizeof(size_t));
    if (set->items == ((void *)0)) {
        hashset_destroy(set);
        return ((void *)0);
    }
    set->nitems = 0;
    return set;
}

canonical_original_code:
struct hashset_st *hashset_create()
{
    struct hashset_st *set = calloc(1, sizeof(struct hashset_st));
    if (set == ((void *)0)) {
        return ((void *)0);
    }
    set->nbits = 3;
    set->capacity = (unsigned long)(1 << set->nbits);
    set->mask = set->capacity - 1;
    set->items = calloc(set->capacity, sizeof(unsigned long));
    if (set->items == ((void *)0)) {
        hashset_destroy(set);
        return ((void *)0);
    }
    set->nitems = 0;
    return set;
}

return_type: struct hashset_st { unsigned long nbits; unsigned long mask; unsigned long capacity; char **items; unsigned long nitems; } *


decompiled_code:
__int64 __fastcall hashset_add(__int64 *a1, const char *a2)
{
unsigned int v2; // ebp

v2 = hashset_add_member(a1, a2);
maybe_rehash(a1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 *a1, const char *a2)
{
unsigned int v2; // ebp

v2 = func9(a1, a2);
func10(a1);
return v2;
}

original_code:
int hashset_add(hashset_t set, char *item)
{
    int rv = hashset_add_member(set, item);
    maybe_rehash(set);
    return rv;
}

canonical_original_code:
int hashset_add(struct hashset_st *set, char *item)
{
    int rv = hashset_add_member(set, item);
    maybe_rehash(set);
    return rv;
}

variable_types: rv int


decompiled_code:
__int64 __fastcall hashset_remove(_QWORD *a1, const char *a2)
{
__int64 v3; // r14
__int64 v4; // rbx
__int64 v5; // r13
const char **v6; // rbp
const char *v7; // rdi

v3 = a1[1];
v4 = v3 & (73LL * (_QWORD)a2);
v5 = a1[3];
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
return 0LL;
while ( strcmp(v7, a2) )
{
v4 = v3 & (v4 + 5009);
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
return 0LL;
}
*v6 = (const char *)1;
--a1[4];
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD *a1, const char *a2)
{
__int64 v3; // r14
__int64 v4; // rbx
__int64 v5; // r13
const char **v6; // rbp
const char *v7; // rdi

v3 = a1[1];
v4 = v3 & (73LL * (_QWORD)a2);
v5 = a1[3];
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
return 0LL;
while ( strcmp(v7, a2) )
{
v4 = v3 & (v4 + 5009);
v6 = (const char **)(v5 + 8 * v4);
v7 = *v6;
if ( (unsigned __int64)*v6 <= 1 )
return 0LL;
}
*v6 = (const char *)1;
--a1[4];
return 1LL;
}

original_code:
int hashset_remove(hashset_t set, char *item)
{
    size_t ii = set->mask & (prime_1 * ((size_t)item));
    while (set->items[ii] != ((void *)0) && set->items[ii] != 1) {
        if (strcmp(set->items[ii],item) == 0) {
            set->items[ii] = (char*)1;
            set->nitems--;
            return 1;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    return 0;
}

canonical_original_code:
int hashset_remove(struct hashset_st *set, char *item)
{
    unsigned long ii = set->mask & (prime_1 * ((size_t)item));
    while (set->items[ii] != ((void *)0) && set->items[ii] != 1) {
        if (strcmp(set->items[ii],item) == 0) {
            set->items[ii] = (char *)1;
            set->nitems--;
            return 1;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    return 0;
}


decompiled_code:
__int64 __fastcall hashset_is_member(__int64 a1, const char *a2)
{
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // r12
const char *v5; // rdi

v2 = *(_QWORD *)(a1 + 8);
v3 = v2 & (73LL * (_QWORD)a2);
v4 = *(_QWORD *)(a1 + 24);
v5 = *(const char **)(v4 + 8 * v3);
if ( (unsigned __int64)v5 <= 1 )
return 0LL;
while ( strcmp(v5, a2) )
{
v3 = v2 & (v3 + 5009);
v5 = *(const char **)(v4 + 8 * v3);
if ( (unsigned __int64)v5 <= 1 )
return 0LL;
}
return 1LL;
}

original_code:
int hashset_is_member(hashset_t set, char *item)
{
    size_t ii = set->mask & (prime_1 * ((size_t)item));
    while (set->items[ii] != ((void *)0) && set->items[ii] != 1) {
        if (strcmp(set->items[ii], item) == 0) {
            return 1;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    return 0;
}

canonical_original_code:
int hashset_is_member(struct hashset_st *set, char *item)
{
    unsigned long ii = set->mask & (prime_1 * ((size_t)item));
    while (set->items[ii] != ((void *)0) && set->items[ii] != 1) {
        if (strcmp(set->items[ii], item) == 0) {
            return 1;
        } else {
            ii = set->mask & (ii + prime_2);
        }
    }
    return 0;
}


decompiled_code:
void __fastcall hashset_union(__int64 *a1, __int64 a2)
{
unsigned __int64 v2; // rbx
__int64 v3; // rax
const char *v4; // rbp
size_t v5; // rax
char *v6; // rax

if ( *(_QWORD *)(a2 + 32) )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(a2 + 24);
v4 = *(const char **)(v3 + 8 * v2);
if ( (unsigned __int64)v4 > 1 )
{
v5 = strlen(*(const char **)(v3 + 8 * v2));
v6 = (char *)malloc(v5);
strcpy(v6, v4);
hashset_add(a1, v4);
}
++v2;
}
while ( *(_QWORD *)(a2 + 32) > v2 );
}
}

canonical_decompiled_code:
void __fastcall func17(__int64 *a1, __int64 a2)
{
unsigned __int64 v2; // rbx
__int64 v3; // rax
const char *v4; // rbp
size_t v5; // rax
char *v6; // rax

if ( *(_QWORD *)(a2 + 32) )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(a2 + 24);
v4 = *(const char **)(v3 + 8 * v2);
if ( (unsigned __int64)v4 > 1 )
{
v5 = strlen(*(const char **)(v3 + 8 * v2));
v6 = (char *)malloc(v5);
strcpy(v6, v4);
func14(a1, v4);
}
++v2;
}
while ( *(_QWORD *)(a2 + 32) > v2 );
}
}

original_code:
void hashset_union(hashset_t set, hashset_t toJoin)
{
    int i;
    for(i = 0;i < toJoin->nitems;i++)
    {
        char* value = toJoin->items[i];
        if(value != 0 && value != 1)
        {
            char * dest = malloc(strlen(value));
            strcpy(dest, value);
            hashset_add(set, value);
        }
    }
}

canonical_original_code:
void hashset_union(struct hashset_st *set, struct hashset_st *toJoin)
{
    int i;
    for(i = 0;i < toJoin->nitems;i++)
    {
        char *value = toJoin->items[i];
        if(value != 0 && value != 1)
        {
            char *dest = malloc(strlen(value));
            strcpy(dest, value);
            hashset_add(set, value);
        }
    }
}

variable_types:
toJoin
struct hashset_st *

variable_types: i int

variable_types:
value
char *

variable_types:
dest
char *


decompiled_code:
__int64 __fastcall hashset_equals(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 result; // rax
__int64 v4; // r13
__int64 v5; // rbx

v2 = *(_QWORD *)(a1 + 32);
result = 0LL;
if ( v2 == *(_QWORD *)(a2 + 32) )
{
if ( v2 )
{
v4 = *(_QWORD *)(a1 + 24);
v5 = 0LL;
while ( 1 )
{
result = hashset_is_member(a2, *(const char **)(v4 + 8 * v5));
if ( !(_DWORD)result )
break;
if ( v2 == ++v5 )
return 1LL;
}
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 result; // rax
__int64 v4; // r13
__int64 v5; // rbx

v2 = *(_QWORD *)(a1 + 32);
result = 0LL;
if ( v2 == *(_QWORD *)(a2 + 32) )
{
if ( v2 )
{
v4 = *(_QWORD *)(a1 + 24);
v5 = 0LL;
while ( 1 )
{
result = func16(a2, *(const char **)(v4 + 8 * v5));
if ( !(_DWORD)result )
break;
if ( v2 == ++v5 )
return 1LL;
}
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
int hashset_equals(hashset_t set, hashset_t other)
{
    if(set->nitems == other->nitems)
    {
        int i;
        int j;
        for(i = 0; i < set->nitems; i++)
        {
            char* v = set->items[i];
            if(!hashset_is_member(other, v))
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}

canonical_original_code:
int hashset_equals(struct hashset_st *set, struct hashset_st *other)
{
    if(set->nitems == other->nitems)
    {
        int i;
        int j;
        for(i = 0; i < set->nitems; i++)
        {
            char *v = set->items[i];
            if(!hashset_is_member(other, v))
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}

variable_types:
other
struct hashset_st *

variable_types: j int

variable_types:
v
char *


decompiled_code:
int *__fastcall imu_init(char *file, __int64 a2)
{
char *v2; // rax
int *v3; // rbx
int v4; // eax

if ( !file )
_assert_fail("device != 0", "src/imu.c", 0xA3u, "imu_init");
if ( !a2 )
_assert_fail("config != 0", "src/imu.c", 0xA4u, "imu_init");
v2 = (char *)malloc(0x80uLL);
v3 = (int *)v2;
if ( !v2 )
_assert_fail("imu != 0", "src/imu.c", 0xA7u, "imu_init");
*((_QWORD *)v2 + 15) = a2;
*(_QWORD *)(v2 + 4) = (unsigned int)LC3;
*((_DWORD *)v2 + 3) = 0;
*((_DWORD *)v2 + 4) = 0;
*(_QWORD *)(v2 + 20) = (unsigned int)LC3;
*((_DWORD *)v2 + 7) = 0;
*((_DWORD *)v2 + 8) = 0;
*((_DWORD *)v2 + 9) = LC3;
*((_DWORD *)v2 + 14) = 0;
*((_DWORD *)v2 + 15) = 0;
*((_DWORD *)v2 + 16) = 0;
*((_QWORD *)v2 + 6) = 0LL;
v4 = open(file, 256);
*v3 = v4;
if ( v4 == -1 )
{
debug_printf(2LL, "src/imu.c", 187LL, "Failed to open `%s`", file);
free(v3);
v3 = 0LL;
}
else if ( pthread_mutex_init((pthread_mutex_t *)v3 + 2, 0LL) || pthread_create((pthread_t *)v3 + 9, 0LL, worker, v3) )
{
debug_printf(2LL, "src/imu.c", 195LL, "Failed to create thread");
close(*v3);
free(v3);
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
int *__fastcall func1(char *file, __int64 a2)
{
char *v2; // rax
int *v3; // rbx
int v4; // eax

if ( !file )
_assert_fail("device != 0", "src/imu.c", 0xA3u, "imu_init");
if ( !a2 )
_assert_fail("config != 0", "src/imu.c", 0xA4u, "imu_init");
v2 = (char *)malloc(0x80uLL);
v3 = (int *)v2;
if ( !v2 )
_assert_fail("imu != 0", "src/imu.c", 0xA7u, "imu_init");
*((_QWORD *)v2 + 15) = a2;
*(_QWORD *)(v2 + 4) = (unsigned int)LC3;
*((_DWORD *)v2 + 3) = 0;
*((_DWORD *)v2 + 4) = 0;
*(_QWORD *)(v2 + 20) = (unsigned int)LC3;
*((_DWORD *)v2 + 7) = 0;
*((_DWORD *)v2 + 8) = 0;
*((_DWORD *)v2 + 9) = LC3;
*((_DWORD *)v2 + 14) = 0;
*((_DWORD *)v2 + 15) = 0;
*((_DWORD *)v2 + 16) = 0;
*((_QWORD *)v2 + 6) = 0LL;
v4 = open(file, 256);
*v3 = v4;
if ( v4 == -1 )
{
debug_printf(2LL, "src/imu.c", 187LL, "Failed to open `%s`", file);
free(v3);
v3 = 0LL;
}
else if ( pthread_mutex_init((pthread_mutex_t *)v3 + 2, 0LL) || pthread_create((pthread_t *)v3 + 9, 0LL, worker, v3) )
{
debug_printf(2LL, "src/imu.c", 195LL, "Failed to create thread");
close(*v3);
free(v3);
v3 = 0LL;
}
return v3;
}

original_code:
imu_t *imu_init(const char *device, const struct imu_config *config)
{
    ;
    ((void) sizeof ((device != 0) ? 1 : 0), __extension__ ({ if (device != 0) ; else __assert_fail ("device != 0", "src/imu.c", 163, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((config != 0) ? 1 : 0), __extension__ ({ if (config != 0) ; else __assert_fail ("config != 0", "src/imu.c", 164, __extension__ __PRETTY_FUNCTION__); }));
    imu_t *imu = malloc(sizeof(struct _imu));
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 167, __extension__ __PRETTY_FUNCTION__); }));
    imu->config = config;
    imu->dcm[0] = 1;
    imu->dcm[1] = 0;
    imu->dcm[2] = 0;
    imu->dcm[3] = 0;
    imu->dcm[4] = 1;
    imu->dcm[5] = 0;
    imu->dcm[6] = 0;
    imu->dcm[7] = 0;
    imu->dcm[8] = 1;
    imu->accsum[0] = 0;
    imu->accsum[1] = 0;
    imu->accsum[2] = 0;
    imu->reftime = 0;
    if((imu->fd = open(device, 00 | 0400)) == -1)
    {
        _debug_printf(2, "src/imu.c", 187, "Failed to open `%s`", device);
        free(imu);
        return ((void *)0);
    }
    if(pthread_mutex_init(&imu->mutex, ((void *)0)) || pthread_create(&imu->thread, ((void *)0), worker, imu))
    {
        _debug_printf(2, "src/imu.c", 195, "Failed to create thread");
        close(imu->fd);
        free(imu);
        return ((void *)0);
    }
    return imu;
}

canonical_original_code:
struct _imu *imu_init(char *device, struct imu_config *config)
{
    ;
    ((void) sizeof ((device != 0) ? 1 : 0), __extension__ ({ if (device != 0) ; else __assert_fail ("device != 0", "src/imu.c", 163, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((config != 0) ? 1 : 0), __extension__ ({ if (config != 0) ; else __assert_fail ("config != 0", "src/imu.c", 164, __extension__ __PRETTY_FUNCTION__); }));
    struct _imu *imu = malloc(sizeof(struct _imu));
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 167, __extension__ __PRETTY_FUNCTION__); }));
    imu->config = config;
    imu->dcm[0] = 1;
    imu->dcm[1] = 0;
    imu->dcm[2] = 0;
    imu->dcm[3] = 0;
    imu->dcm[4] = 1;
    imu->dcm[5] = 0;
    imu->dcm[6] = 0;
    imu->dcm[7] = 0;
    imu->dcm[8] = 1;
    imu->accsum[0] = 0;
    imu->accsum[1] = 0;
    imu->accsum[2] = 0;
    imu->reftime = 0;
    if((imu->fd = open(device, 00 | 0400)) == -1)
    {
        _debug_printf(2, "src/imu.c", 187, "Failed to open `%s`", device);
        free(imu);
        return ((void *)0);
    }
    if(pthread_mutex_init(&imu->mutex, ((void *)0)) || pthread_create(&imu->thread, ((void *)0), worker, imu))
    {
        _debug_printf(2, "src/imu.c", 195, "Failed to create thread");
        close(imu->fd);
        free(imu);
        return ((void *)0);
    }
    return imu;
}

variable_types:
device
char *

variable_types:
config
struct imu_config *

variable_types: struct _imu { int fd; float dcm[9]; unsigned long timestamp; unsigned long reftime; float accsum[3]; unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct imu_config { float gyro_offset[3]; float gyro_weight; float gyro_scale; float acc_scale; } *config; } *

user_defined_types: struct _imu { int fd; float dcm[9]; unsigned long timestamp; unsigned long reftime; float accsum[3]; unsigned long thread; union pthread_mutex_t mutex; struct imu_config *config; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __fastcall imu_get_attitude(__int64 a1, __int64 a2)
{
float v2; // xmm0_4

if ( !a1 )
_assert_fail("imu != 0", "src/imu.c", 0xCFu, "imu_get_attitude");
if ( !a2 )
_assert_fail("attitude != 0", "src/imu.c", 0xD0u, "imu_get_attitude");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 80));
*(_DWORD *)a2 = COERCE_UNSIGNED_INT(atan2f(*(float *)(a1 + 32), *(float *)(a1 + 36))) ^ LC2;
*(float *)(a2 + 4) = asinf(*(float *)(a1 + 28));
v2 = atan2(*(float *)(a1 + 16), *(float *)(a1 + 4));
*(_DWORD *)(a2 + 8) = LODWORD(v2) ^ LC2;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 80));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2)
{
float v2; // xmm0_4

if ( !a1 )
_assert_fail("imu != 0", "src/imu.c", 0xCFu, "imu_get_attitude");
if ( !a2 )
_assert_fail("attitude != 0", "src/imu.c", 0xD0u, "imu_get_attitude");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 80));
*(_DWORD *)a2 = COERCE_UNSIGNED_INT(atan2f(*(float *)(a1 + 32), *(float *)(a1 + 36))) ^ LC2;
*(float *)(a2 + 4) = asinf(*(float *)(a1 + 28));
v2 = atan2(*(float *)(a1 + 16), *(float *)(a1 + 4));
*(_DWORD *)(a2 + 8) = LODWORD(v2) ^ LC2;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 80));
}

original_code:
void imu_get_attitude(imu_t *imu, float attitude[3])
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 207, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((attitude != 0) ? 1 : 0), __extension__ ({ if (attitude != 0) ; else __assert_fail ("attitude != 0", "src/imu.c", 208, __extension__ __PRETTY_FUNCTION__); }));
    pthread_mutex_lock(&imu->mutex);
    attitude[0] = -atan2f(imu->dcm[7], imu->dcm[8]);
    attitude[1] = asinf(imu->dcm[6]);
    attitude[2] = -atan2(imu->dcm[3], imu->dcm[0]);
    pthread_mutex_unlock(&imu->mutex);
}

canonical_original_code:
void imu_get_attitude(struct _imu *imu, float attitude[3])
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 207, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((attitude != 0) ? 1 : 0), __extension__ ({ if (attitude != 0) ; else __assert_fail ("attitude != 0", "src/imu.c", 208, __extension__ __PRETTY_FUNCTION__); }));
    pthread_mutex_lock(&imu->mutex);
    attitude[0] = -atan2f(imu->dcm[7], imu->dcm[8]);
    attitude[1] = asinf(imu->dcm[6]);
    attitude[2] = -atan2(imu->dcm[3], imu->dcm[0]);
    pthread_mutex_unlock(&imu->mutex);
}

variable_types:
attitude
float [3]

user_defined_types: struct imu_config { float gyro_offset[3]; float gyro_weight; float gyro_scale; float acc_scale; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
int __fastcall imu_get_acceleration(__int64 a1, _DWORD *a2, float *a3)
{
__int64 v4; // rax
float v5; // xmm0_4

if ( !a1 )
_assert_fail("imu != 0", "src/imu.c", 0xDCu, "imu_get_acceleration");
if ( !a2 )
_assert_fail("accsum != 0", "src/imu.c", 0xDDu, "imu_get_acceleration");
if ( !a3 )
_assert_fail("difftime != 0", "src/imu.c", 0xDEu, "imu_get_acceleration");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 80));
*a2 = *(_DWORD *)(a1 + 56);
a2[1] = *(_DWORD *)(a1 + 60);
a2[2] = *(_DWORD *)(a1 + 64);
*(_DWORD *)(a1 + 56) = 0;
*(_DWORD *)(a1 + 60) = 0;
*(_DWORD *)(a1 + 64) = 0;
v4 = *(_QWORD *)(a1 + 40) - *(_QWORD *)(a1 + 48);
if ( v4 < 0 )
v5 = (float)(v4 & 1 | (unsigned int)((unsigned __int64)v4 >> 1))
+ (float)(v4 & 1 | (unsigned int)((unsigned __int64)v4 >> 1));
else
v5 = (float)(int)v4;
*a3 = v5 / 1000000000.0;
*(_QWORD *)(a1 + 48) = *(_QWORD *)(a1 + 40);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 80));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, _DWORD *a2, float *a3)
{
__int64 v4; // rax
float v5; // xmm0_4

if ( !a1 )
_assert_fail("imu != 0", "src/imu.c", 0xDCu, "imu_get_acceleration");
if ( !a2 )
_assert_fail("accsum != 0", "src/imu.c", 0xDDu, "imu_get_acceleration");
if ( !a3 )
_assert_fail("difftime != 0", "src/imu.c", 0xDEu, "imu_get_acceleration");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 80));
*a2 = *(_DWORD *)(a1 + 56);
a2[1] = *(_DWORD *)(a1 + 60);
a2[2] = *(_DWORD *)(a1 + 64);
*(_DWORD *)(a1 + 56) = 0;
*(_DWORD *)(a1 + 60) = 0;
*(_DWORD *)(a1 + 64) = 0;
v4 = *(_QWORD *)(a1 + 40) - *(_QWORD *)(a1 + 48);
if ( v4 < 0 )
v5 = (float)(v4 & 1 | (unsigned int)((unsigned __int64)v4 >> 1))
+ (float)(v4 & 1 | (unsigned int)((unsigned __int64)v4 >> 1));
else
v5 = (float)(int)v4;
*a3 = v5 / 1000000000.0;
*(_QWORD *)(a1 + 48) = *(_QWORD *)(a1 + 40);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 80));
}

original_code:
void imu_get_acceleration(imu_t *imu, float accsum[3], float *difftime)
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 220, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((accsum != 0) ? 1 : 0), __extension__ ({ if (accsum != 0) ; else __assert_fail ("accsum != 0", "src/imu.c", 221, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((difftime != 0) ? 1 : 0), __extension__ ({ if (difftime != 0) ; else __assert_fail ("difftime != 0", "src/imu.c", 222, __extension__ __PRETTY_FUNCTION__); }));
    pthread_mutex_lock(&imu->mutex);
    accsum[0] = imu->accsum[0];
    accsum[1] = imu->accsum[1];
    accsum[2] = imu->accsum[2];
    imu->accsum[0] = 0;
    imu->accsum[1] = 0;
    imu->accsum[2] = 0;
    *difftime = (float)(imu->timestamp - imu->reftime) / 1e9;
    imu->reftime = imu->timestamp;
    pthread_mutex_unlock(&imu->mutex);
}

canonical_original_code:
void imu_get_acceleration(struct _imu *imu, float accsum[3], float *difftime)
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 220, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((accsum != 0) ? 1 : 0), __extension__ ({ if (accsum != 0) ; else __assert_fail ("accsum != 0", "src/imu.c", 221, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((difftime != 0) ? 1 : 0), __extension__ ({ if (difftime != 0) ; else __assert_fail ("difftime != 0", "src/imu.c", 222, __extension__ __PRETTY_FUNCTION__); }));
    pthread_mutex_lock(&imu->mutex);
    accsum[0] = imu->accsum[0];
    accsum[1] = imu->accsum[1];
    accsum[2] = imu->accsum[2];
    imu->accsum[0] = 0;
    imu->accsum[1] = 0;
    imu->accsum[2] = 0;
    *difftime = (float)(imu->timestamp - imu->reftime) / 1e9;
    imu->reftime = imu->timestamp;
    pthread_mutex_unlock(&imu->mutex);
}

variable_types:
accsum
float [3]

variable_types:
difftime
float *


decompiled_code:
void __fastcall imu_free(void *ptr)
{
if ( !ptr )
_assert_fail("imu != 0", "src/imu.c", 0xEFu, "imu_free");
pthread_cancel(*((_QWORD *)ptr + 9));
pthread_join(*((_QWORD *)ptr + 9), 0LL);
pthread_mutex_destroy((pthread_mutex_t *)ptr + 2);
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
if ( !ptr )
_assert_fail("imu != 0", "src/imu.c", 0xEFu, "imu_free");
pthread_cancel(*((_QWORD *)ptr + 9));
pthread_join(*((_QWORD *)ptr + 9), 0LL);
pthread_mutex_destroy((pthread_mutex_t *)ptr + 2);
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
void imu_free(imu_t *imu)
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 239, __extension__ __PRETTY_FUNCTION__); }));
    pthread_cancel(imu->thread);
    pthread_join(imu->thread, ((void *)0));
    pthread_mutex_destroy(&imu->mutex);
    close(imu->fd);
    free(imu);
}

canonical_original_code:
void imu_free(struct _imu *imu)
{
    ;
    ((void) sizeof ((imu != 0) ? 1 : 0), __extension__ ({ if (imu != 0) ; else __assert_fail ("imu != 0", "src/imu.c", 239, __extension__ __PRETTY_FUNCTION__); }));
    pthread_cancel(imu->thread);
    pthread_join(imu->thread, ((void *)0));
    pthread_mutex_destroy(&imu->mutex);
    close(imu->fd);
    free(imu);
}


worker

decompiled_code:
void *__fastcall worker(void *a1)
{
__int64 v2; // rax
float v3; // xmm0_4
float v4; // xmm0_4
float v5; // xmm12_4
float v6; // xmm1_4
float v7; // xmm0_4
float v8; // xmm11_4
float v9; // xmm8_4
float v10; // xmm6_4
float v11; // xmm10_4
float v12; // xmm2_4
float v13; // xmm4_4
float v14; // xmm11_4
float v15; // xmm0_4
float v16; // xmm9_4
float v17; // xmm7_4
float v18; // xmm0_4
float v19; // xmm12_4
__int64 v20; // rax
float v21; // xmm6_4
float v22; // xmm0_4
float v23; // xmm4_4
float v24; // xmm1_4
float v25; // xmm2_4
float v26; // xmm12_4
float v27; // xmm9_4
float v28; // xmm7_4
float v29; // xmm3_4
float v30; // xmm15_4
float v31; // xmm14_4
float v32; // xmm13_4
float v33; // xmm0_4
__int64 v34; // rdx
int v35[3]; // [rsp+24h] [rbp-6Ch] BYREF
__int128 buf; // [rsp+30h] [rbp-60h] BYREF
__int64 v37; // [rsp+42h] [rbp-4Eh]
float v38; // [rsp+4Ch] [rbp-44h] BYREF
float v39; // [rsp+50h] [rbp-40h]
float v40; // [rsp+54h] [rbp-3Ch]
int v41[3]; // [rsp+58h] [rbp-38h] BYREF
float v42; // [rsp+64h] [rbp-2Ch] BYREF
float v43; // [rsp+68h] [rbp-28h]
float v44; // [rsp+6Ch] [rbp-24h]

if ( read(*(_DWORD *)a1, &buf, 0x1AuLL) == 26 )
{
pthread_mutex_lock((pthread_mutex_t *)a1 + 2);
dequantize(*((_QWORD *)a1 + 15), &v42, v41, &v38);
vect_norm(v41);
vect_norm(&v38);
vect_mult((char *)a1 + 28, v41, (char *)a1 + 16);
vect_mult((char *)a1 + 16, (char *)a1 + 28, (char *)a1 + 4);
v2 = v37;
*((_QWORD *)a1 + 5) = v37;
*((_QWORD *)a1 + 6) = v2;
pthread_mutex_unlock((pthread_mutex_t *)a1 + 2);
while ( read(*(_DWORD *)a1, &buf, 0x1AuLL) == 26 )
{
pthread_mutex_lock((pthread_mutex_t *)a1 + 2);
dequantize(*((_QWORD *)a1 + 15), &v42, v41, &v38);
v21 = *((float *)a1 + 1);
v22 = v38;
v23 = *((float *)a1 + 2);
v24 = v39;
v25 = *((float *)a1 + 3);
v26 = v40;
*((float *)a1 + 14) = (float)((float)((float)(v21 * v38) + (float)(v23 * v39)) + (float)(v25 * v40))
+ *((float *)a1 + 14);
v27 = *((float *)a1 + 4);
v28 = *((float *)a1 + 5);
v29 = *((float *)a1 + 6);
*((float *)a1 + 15) = (float)((float)((float)(v22 * v27) + (float)(v24 * v28)) + (float)(v26 * v29))
+ *((float *)a1 + 15);
v30 = *((float *)a1 + 7);
v31 = *((float *)a1 + 8);
v32 = *((float *)a1 + 9);
v33 = (float)((float)((float)(v22 * v30) + (float)(v24 * v31)) + (float)(v26 * v32)) - 9.81 + *((float *)a1 + 16);
*((float *)a1 + 16) = v33;
v34 = v37 - *((_QWORD *)a1 + 5);
if ( v34 < 0 )
v3 = (float)(v34 & 1 | (unsigned int)((unsigned __int64)v34 >> 1))
+ (float)(v34 & 1 | (unsigned int)((unsigned __int64)v34 >> 1));
else
v3 = (float)(int)v34;
v4 = v3 / 1000000000.0;
v42 = v4 * v42;
v5 = v42;
v43 = v4 * v43;
v6 = v43;
v7 = v4 * v44;
v44 = v7;
*((_QWORD *)a1 + 5) = v37;
v8 = v7 + (float)(v5 * v6);
v9 = (float)(v5 * v7) - v6;
v10 = (float)(v21 + (float)(v27 * v8)) + (float)(v30 * v9);
*((float *)a1 + 1) = v10;
v11 = (float)(v31 * v9) + (float)(v23 + (float)(v28 * v8));
*((float *)a1 + 2) = v11;
v12 = (float)(v25 + (float)(v8 * v29)) + (float)(v9 * v32);
*((float *)a1 + 3) = v12;
LODWORD(v13) = LODWORD(v7) ^ LC2;
v14 = 1.0 - (float)((float)(v5 * v6) * v7);
v15 = (float)(v7 * v6) + v5;
v16 = (float)((float)(v27 * v14) + (float)(v10 * v13)) + (float)(v30 * v15);
*((float *)a1 + 4) = v16;
v17 = (float)((float)(v28 * v14) + (float)(v11 * v13)) + (float)(v31 * v15);
*((float *)a1 + 5) = v17;
v18 = (float)(v15 * v32) + (float)((float)(v29 * v14) + (float)(v13 * v12));
*((float *)a1 + 6) = v18;
LODWORD(v19) = LODWORD(v5) ^ LC2;
*((float *)a1 + 7) = (float)((float)(v10 * v6) + (float)(v16 * v19)) + v30;
*((float *)a1 + 8) = (float)((float)(v11 * v6) + (float)(v17 * v19)) + v31;
*((float *)a1 + 9) = (float)((float)(v12 * v6) + (float)(v18 * v19)) + v32;
vect_norm(v41);
vect_norm(&v38);
vect_mult(&v38, v41, v35);
vect_mult(v35, &v38, v41);
v20 = *((_QWORD *)a1 + 15);
*((float *)a1 + 1) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)v41)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 1));
*((float *)a1 + 2) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v41[1])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 2));
*((float *)a1 + 3) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v41[2])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 3));
*((float *)a1 + 4) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)v35)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 4));
*((float *)a1 + 5) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v35[1])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 5));
*((float *)a1 + 6) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v35[2])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 6));
*((float *)a1 + 7) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v38)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 7));
*((float *)a1 + 8) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v39)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 8));
*((float *)a1 + 9) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v40)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 9));
pthread_mutex_unlock((pthread_mutex_t *)a1 + 2);
}
}
debug_printf(1LL, "src/imu.c", 156LL, "Broken pipe");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
__int64 v2; // rax
float v3; // xmm0_4
float v4; // xmm0_4
float v5; // xmm12_4
float v6; // xmm1_4
float v7; // xmm0_4
float v8; // xmm11_4
float v9; // xmm8_4
float v10; // xmm6_4
float v11; // xmm10_4
float v12; // xmm2_4
float v13; // xmm4_4
float v14; // xmm11_4
float v15; // xmm0_4
float v16; // xmm9_4
float v17; // xmm7_4
float v18; // xmm0_4
float v19; // xmm12_4
__int64 v20; // rax
float v21; // xmm6_4
float v22; // xmm0_4
float v23; // xmm4_4
float v24; // xmm1_4
float v25; // xmm2_4
float v26; // xmm12_4
float v27; // xmm9_4
float v28; // xmm7_4
float v29; // xmm3_4
float v30; // xmm15_4
float v31; // xmm14_4
float v32; // xmm13_4
float v33; // xmm0_4
__int64 v34; // rdx
int v35[3]; // [rsp+24h] [rbp-6Ch] BYREF
__int128 buf; // [rsp+30h] [rbp-60h] BYREF
__int64 v37; // [rsp+42h] [rbp-4Eh]
float v38; // [rsp+4Ch] [rbp-44h] BYREF
float v39; // [rsp+50h] [rbp-40h]
float v40; // [rsp+54h] [rbp-3Ch]
int v41[3]; // [rsp+58h] [rbp-38h] BYREF
float v42; // [rsp+64h] [rbp-2Ch] BYREF
float v43; // [rsp+68h] [rbp-28h]
float v44; // [rsp+6Ch] [rbp-24h]

if ( read(*(_DWORD *)a1, &buf, 0x1AuLL) == 26 )
{
pthread_mutex_lock((pthread_mutex_t *)a1 + 2);
dequantize(*((_QWORD *)a1 + 15), &v42, v41, &v38);
vect_norm(v41);
vect_norm(&v38);
vect_mult((char *)a1 + 28, v41, (char *)a1 + 16);
vect_mult((char *)a1 + 16, (char *)a1 + 28, (char *)a1 + 4);
v2 = v37;
*((_QWORD *)a1 + 5) = v37;
*((_QWORD *)a1 + 6) = v2;
pthread_mutex_unlock((pthread_mutex_t *)a1 + 2);
while ( read(*(_DWORD *)a1, &buf, 0x1AuLL) == 26 )
{
pthread_mutex_lock((pthread_mutex_t *)a1 + 2);
dequantize(*((_QWORD *)a1 + 15), &v42, v41, &v38);
v21 = *((float *)a1 + 1);
v22 = v38;
v23 = *((float *)a1 + 2);
v24 = v39;
v25 = *((float *)a1 + 3);
v26 = v40;
*((float *)a1 + 14) = (float)((float)((float)(v21 * v38) + (float)(v23 * v39)) + (float)(v25 * v40))
+ *((float *)a1 + 14);
v27 = *((float *)a1 + 4);
v28 = *((float *)a1 + 5);
v29 = *((float *)a1 + 6);
*((float *)a1 + 15) = (float)((float)((float)(v22 * v27) + (float)(v24 * v28)) + (float)(v26 * v29))
+ *((float *)a1 + 15);
v30 = *((float *)a1 + 7);
v31 = *((float *)a1 + 8);
v32 = *((float *)a1 + 9);
v33 = (float)((float)((float)(v22 * v30) + (float)(v24 * v31)) + (float)(v26 * v32)) - 9.81 + *((float *)a1 + 16);
*((float *)a1 + 16) = v33;
v34 = v37 - *((_QWORD *)a1 + 5);
if ( v34 < 0 )
v3 = (float)(v34 & 1 | (unsigned int)((unsigned __int64)v34 >> 1))
+ (float)(v34 & 1 | (unsigned int)((unsigned __int64)v34 >> 1));
else
v3 = (float)(int)v34;
v4 = v3 / 1000000000.0;
v42 = v4 * v42;
v5 = v42;
v43 = v4 * v43;
v6 = v43;
v7 = v4 * v44;
v44 = v7;
*((_QWORD *)a1 + 5) = v37;
v8 = v7 + (float)(v5 * v6);
v9 = (float)(v5 * v7) - v6;
v10 = (float)(v21 + (float)(v27 * v8)) + (float)(v30 * v9);
*((float *)a1 + 1) = v10;
v11 = (float)(v31 * v9) + (float)(v23 + (float)(v28 * v8));
*((float *)a1 + 2) = v11;
v12 = (float)(v25 + (float)(v8 * v29)) + (float)(v9 * v32);
*((float *)a1 + 3) = v12;
LODWORD(v13) = LODWORD(v7) ^ LC2;
v14 = 1.0 - (float)((float)(v5 * v6) * v7);
v15 = (float)(v7 * v6) + v5;
v16 = (float)((float)(v27 * v14) + (float)(v10 * v13)) + (float)(v30 * v15);
*((float *)a1 + 4) = v16;
v17 = (float)((float)(v28 * v14) + (float)(v11 * v13)) + (float)(v31 * v15);
*((float *)a1 + 5) = v17;
v18 = (float)(v15 * v32) + (float)((float)(v29 * v14) + (float)(v13 * v12));
*((float *)a1 + 6) = v18;
LODWORD(v19) = LODWORD(v5) ^ LC2;
*((float *)a1 + 7) = (float)((float)(v10 * v6) + (float)(v16 * v19)) + v30;
*((float *)a1 + 8) = (float)((float)(v11 * v6) + (float)(v17 * v19)) + v31;
*((float *)a1 + 9) = (float)((float)(v12 * v6) + (float)(v18 * v19)) + v32;
vect_norm(v41);
vect_norm(&v38);
vect_mult(&v38, v41, v35);
vect_mult(v35, &v38, v41);
v20 = *((_QWORD *)a1 + 15);
*((float *)a1 + 1) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)v41)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 1));
*((float *)a1 + 2) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v41[1])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 2));
*((float *)a1 + 3) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v41[2])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 3));
*((float *)a1 + 4) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)v35)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 4));
*((float *)a1 + 5) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v35[1])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 5));
*((float *)a1 + 6) = (float)((float)(1.0 - *(float *)(v20 + 12)) * *(float *)&v35[2])
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 6));
*((float *)a1 + 7) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v38)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 7));
*((float *)a1 + 8) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v39)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 8));
*((float *)a1 + 9) = (float)((float)(1.0 - *(float *)(v20 + 12)) * v40)
+ (float)(*(float *)(v20 + 12) * *((float *)a1 + 9));
pthread_mutex_unlock((pthread_mutex_t *)a1 + 2);
}
}
debug_printf(1LL, "src/imu.c", 156LL, "Broken pipe");
return 0LL;
}

original_code:
static void *worker(void *arg)
{
    ;
    float gyro[3], mag[3], acc[3];
    struct buffer buf;
    imu_t *imu = (imu_t*)arg;
    if(read(imu->fd, &buf, sizeof(struct buffer)) != sizeof(struct buffer)) goto finalize;
    pthread_mutex_lock(&imu->mutex);
    dequantize(imu->config, buf, gyro, mag, acc);
    ;
    vect_norm(mag, mag);
    vect_norm(acc, &imu->dcm[6]);
    vect_mult(&imu->dcm[6], mag, &imu->dcm[3]);
    vect_mult(&imu->dcm[3], &imu->dcm[6], &imu->dcm[0]);
    imu->reftime = imu->timestamp = buf.timestamp;
    pthread_mutex_unlock(&imu->mutex);
    while(read(imu->fd, &buf, sizeof(struct buffer)) == sizeof(struct buffer))
    {
        pthread_mutex_lock(&imu->mutex);
        dequantize(imu->config, buf, gyro, mag, acc);
        ;
        imu->accsum[0] += imu->dcm[0] * acc[0] + imu->dcm[1] * acc[1] + imu->dcm[2] * acc[2];
        imu->accsum[1] += imu->dcm[3] * acc[0] + imu->dcm[4] * acc[1] + imu->dcm[5] * acc[2];
        imu->accsum[2] += imu->dcm[6] * acc[0] + imu->dcm[7] * acc[1] + imu->dcm[8] * acc[2] - 9.81;
        float diff = (float)(buf.timestamp - imu->timestamp) / 1e9;
        gyro[0] *= diff;
        gyro[1] *= diff;
        gyro[2] *= diff;
        imu->timestamp = buf.timestamp;
        imu->dcm[0] = imu->dcm[0] + imu->dcm[3] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[6] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[1] = imu->dcm[1] + imu->dcm[4] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[7] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[2] = imu->dcm[2] + imu->dcm[5] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[8] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[3] = imu->dcm[0] * -gyro[2] + imu->dcm[3] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[6] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[4] = imu->dcm[1] * -gyro[2] + imu->dcm[4] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[7] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[5] = imu->dcm[2] * -gyro[2] + imu->dcm[5] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[8] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[6] = imu->dcm[0] * gyro[1] + imu->dcm[3] * -gyro[0] + imu->dcm[6];
        imu->dcm[7] = imu->dcm[1] * gyro[1] + imu->dcm[4] * -gyro[0] + imu->dcm[7];
        imu->dcm[8] = imu->dcm[2] * gyro[1] + imu->dcm[5] * -gyro[0] + imu->dcm[8];
        float tmp[3];
        vect_norm(mag, mag);
        vect_norm(acc, acc);
        vect_mult(acc, mag, tmp);
        vect_mult(tmp, acc, mag);
        imu->dcm[0] = imu->config->gyro_weight * imu->dcm[0] + (1 - imu->config->gyro_weight) * mag[0];
        imu->dcm[1] = imu->config->gyro_weight * imu->dcm[1] + (1 - imu->config->gyro_weight) * mag[1];
        imu->dcm[2] = imu->config->gyro_weight * imu->dcm[2] + (1 - imu->config->gyro_weight) * mag[2];
        imu->dcm[3] = imu->config->gyro_weight * imu->dcm[3] + (1 - imu->config->gyro_weight) * tmp[0];
        imu->dcm[4] = imu->config->gyro_weight * imu->dcm[4] + (1 - imu->config->gyro_weight) * tmp[1];
        imu->dcm[5] = imu->config->gyro_weight * imu->dcm[5] + (1 - imu->config->gyro_weight) * tmp[2];
        imu->dcm[6] = imu->config->gyro_weight * imu->dcm[6] + (1 - imu->config->gyro_weight) * acc[0];
        imu->dcm[7] = imu->config->gyro_weight * imu->dcm[7] + (1 - imu->config->gyro_weight) * acc[1];
        imu->dcm[8] = imu->config->gyro_weight * imu->dcm[8] + (1 - imu->config->gyro_weight) * acc[2];
        pthread_mutex_unlock(&imu->mutex);
    }
finalize:
    _debug_printf(1, "src/imu.c", 156, "Broken pipe");
    return ((void *)0);
}

canonical_original_code:
static void *worker(void *arg)
{
    ;
    float gyro[3]; float mag[3]; float acc[3];
    struct buffer buf;
    struct _imu *imu = (imu_t*)arg;
    if(read(imu->fd, &buf, sizeof(struct buffer)) != sizeof(struct buffer)) goto finalize;
    pthread_mutex_lock(&imu->mutex);
    dequantize(imu->config, buf, gyro, mag, acc);
    ;
    vect_norm(mag, mag);
    vect_norm(acc, &imu->dcm[6]);
    vect_mult(&imu->dcm[6], mag, &imu->dcm[3]);
    vect_mult(&imu->dcm[3], &imu->dcm[6], &imu->dcm[0]);
    imu->reftime = imu->timestamp = buf.timestamp;
    pthread_mutex_unlock(&imu->mutex);
    while(read(imu->fd, &buf, sizeof(struct buffer)) == sizeof(struct buffer))
    {
        pthread_mutex_lock(&imu->mutex);
        dequantize(imu->config, buf, gyro, mag, acc);
        ;
        imu->accsum[0] += imu->dcm[0] * acc[0] + imu->dcm[1] * acc[1] + imu->dcm[2] * acc[2];
        imu->accsum[1] += imu->dcm[3] * acc[0] + imu->dcm[4] * acc[1] + imu->dcm[5] * acc[2];
        imu->accsum[2] += imu->dcm[6] * acc[0] + imu->dcm[7] * acc[1] + imu->dcm[8] * acc[2] - 9.81;
        float diff = (float)(buf.timestamp - imu->timestamp) / 1e9;
        gyro[0] *= diff;
        gyro[1] *= diff;
        gyro[2] *= diff;
        imu->timestamp = buf.timestamp;
        imu->dcm[0] = imu->dcm[0] + imu->dcm[3] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[6] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[1] = imu->dcm[1] + imu->dcm[4] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[7] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[2] = imu->dcm[2] + imu->dcm[5] * (gyro[0] * gyro[1] + gyro[2]) + imu->dcm[8] * (gyro[0] * gyro[2] - gyro[1]);
        imu->dcm[3] = imu->dcm[0] * -gyro[2] + imu->dcm[3] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[6] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[4] = imu->dcm[1] * -gyro[2] + imu->dcm[4] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[7] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[5] = imu->dcm[2] * -gyro[2] + imu->dcm[5] * (1 - gyro[0] * gyro[1] * gyro[2]) + imu->dcm[8] * (gyro[0] + gyro[1] * gyro[2]);
        imu->dcm[6] = imu->dcm[0] * gyro[1] + imu->dcm[3] * -gyro[0] + imu->dcm[6];
        imu->dcm[7] = imu->dcm[1] * gyro[1] + imu->dcm[4] * -gyro[0] + imu->dcm[7];
        imu->dcm[8] = imu->dcm[2] * gyro[1] + imu->dcm[5] * -gyro[0] + imu->dcm[8];
        float tmp[3];
        vect_norm(mag, mag);
        vect_norm(acc, acc);
        vect_mult(acc, mag, tmp);
        vect_mult(tmp, acc, mag);
        imu->dcm[0] = imu->config->gyro_weight * imu->dcm[0] + (1 - imu->config->gyro_weight) * mag[0];
        imu->dcm[1] = imu->config->gyro_weight * imu->dcm[1] + (1 - imu->config->gyro_weight) * mag[1];
        imu->dcm[2] = imu->config->gyro_weight * imu->dcm[2] + (1 - imu->config->gyro_weight) * mag[2];
        imu->dcm[3] = imu->config->gyro_weight * imu->dcm[3] + (1 - imu->config->gyro_weight) * tmp[0];
        imu->dcm[4] = imu->config->gyro_weight * imu->dcm[4] + (1 - imu->config->gyro_weight) * tmp[1];
        imu->dcm[5] = imu->config->gyro_weight * imu->dcm[5] + (1 - imu->config->gyro_weight) * tmp[2];
        imu->dcm[6] = imu->config->gyro_weight * imu->dcm[6] + (1 - imu->config->gyro_weight) * acc[0];
        imu->dcm[7] = imu->config->gyro_weight * imu->dcm[7] + (1 - imu->config->gyro_weight) * acc[1];
        imu->dcm[8] = imu->config->gyro_weight * imu->dcm[8] + (1 - imu->config->gyro_weight) * acc[2];
        pthread_mutex_unlock(&imu->mutex);
    }
finalize:
    _debug_printf(1, "src/imu.c", 156, "Broken pipe");
    return ((void *)0);
}

variable_types:
arg
void *

variable_types:
gyro
float [3]

variable_types:
mag
float [3]

variable_types:
acc
float [3]

variable_types: struct buffer { short acc[3]; unsigned long timestamp; }

variable_types: diff float

variable_types:
tmp
float [3]

return_type: void *


decompiled_code:
__int64 __fastcall shader_compile(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rbx
unsigned int v5; // er12
char *v6; // rbp
int v7; // [rsp+4h] [rbp-34h] BYREF
__int64 v8; // [rsp+8h] [rbp-30h] BYREF
int v9; // [rsp+18h] [rbp-20h] BYREF
int v10[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v8 = a2;
v7 = a3;
v3 = (unsigned int)glCreateShader();
glShaderSource(v3, 1LL, &v8, &v7);
glCompileShader((unsigned int)v3);
glGetShaderiv((unsigned int)v3, 35713LL, v10);
if ( !v10[0] )
{
glGetShaderiv((unsigned int)v3, 35716LL, &v9);
v5 = v9;
v6 = (char *)malloc(v9);
glGetShaderInfoLog((unsigned int)v3, v5, 0LL, v6);
debug_printf(2LL, "src/graphics-core.c", 71LL, "Shader compiler error:\n%s", v6);
free(v6);
LODWORD(v3) = 0;
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rbx
unsigned int v5; // er12
char *v6; // rbp
int v7; // [rsp+4h] [rbp-34h] BYREF
__int64 v8; // [rsp+8h] [rbp-30h] BYREF
int v9; // [rsp+18h] [rbp-20h] BYREF
int v10[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v8 = a2;
v7 = a3;
v3 = (unsigned int)glCreateShader();
glShaderSource(v3, 1LL, &v8, &v7);
glCompileShader((unsigned int)v3);
glGetShaderiv((unsigned int)v3, 35713LL, v10);
if ( !v10[0] )
{
glGetShaderiv((unsigned int)v3, 35716LL, &v9);
v5 = v9;
v6 = (char *)malloc(v9);
glGetShaderInfoLog((unsigned int)v3, v5, 0LL, v6);
debug_printf(2LL, "src/graphics-core.c", 71LL, "Shader compiler error:\n%s", v6);
free(v6);
LODWORD(v3) = 0;
}
return (unsigned int)v3;
}

original_code:
static GLuint shader_compile(GLenum type, const GLchar *source, GLint length)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &source, &length);
    glCompileShader(shader);
    GLint status;
    glGetShaderiv(shader, 0x8B81, &status);
    if(!status)
    {
        int len;
        glGetShaderiv(shader, 0x8B84, &len);
        char* str = malloc(len);
        glGetShaderInfoLog(shader, len, ((void *)0), str);
        _debug_printf(2, "src/graphics-core.c", 71, "Shader compiler error:\n%s", str);
        free(str);
        return 0;
    }
    return shader;
}

canonical_original_code:
static unsigned int shader_compile(unsigned int type, char *source, int length)
{
    unsigned int shader = glCreateShader(type);
    glShaderSource(shader, 1, &source, &length);
    glCompileShader(shader);
    int status;
    glGetShaderiv(shader, 0x8B81, &status);
    if(!status)
    {
        int len;
        glGetShaderiv(shader, 0x8B84, &len);
        char *str = malloc(len);
        glGetShaderInfoLog(shader, len, ((void *)0), str);
        _debug_printf(2, "src/graphics-core.c", 71, "Shader compiler error:\n%s", str);
        free(str);
        return 0;
    }
    return shader;
}

variable_types:
type
unsigned int

variable_types:
source
char *

variable_types: length int

variable_types:
shader
unsigned int

variable_types: status int

variable_types: len int

variable_types:
str
char *

return_type: unsigned int


decompiled_code:
__int64 __fastcall shader_link(unsigned int a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v4; // er12
char *v5; // rbp
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned int)glCreateProgram();
glAttachShader(v2, a1);
glAttachShader((unsigned int)v2, a2);
glLinkProgram((unsigned int)v2);
glGetProgramiv((unsigned int)v2, 35714LL, v7);
if ( !v7[0] )
{
glGetProgramiv((unsigned int)v2, 35716LL, &v6);
v4 = v6;
v5 = (char *)malloc(v6);
glGetProgramInfoLog((unsigned int)v2, v4, 0LL, v5);
debug_printf(2LL, "src/graphics-core.c", 98LL, "Shader linker error:%s", v5);
free(v5);
LODWORD(v2) = 0;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v4; // er12
char *v5; // rbp
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned int)glCreateProgram();
glAttachShader(v2, a1);
glAttachShader((unsigned int)v2, a2);
glLinkProgram((unsigned int)v2);
glGetProgramiv((unsigned int)v2, 35714LL, v7);
if ( !v7[0] )
{
glGetProgramiv((unsigned int)v2, 35716LL, &v6);
v4 = v6;
v5 = (char *)malloc(v6);
glGetProgramInfoLog((unsigned int)v2, v4, 0LL, v5);
debug_printf(2LL, "src/graphics-core.c", 98LL, "Shader linker error:%s", v5);
free(v5);
LODWORD(v2) = 0;
}
return (unsigned int)v2;
}

original_code:
static GLuint shader_link(GLuint vertex, GLuint fragment)
{
    GLuint program = glCreateProgram();
    glAttachShader(program, vertex);
    glAttachShader(program, fragment);
    glLinkProgram(program);
    GLint status;
    glGetProgramiv(program, 0x8B82, &status);
    if(!status)
    {
        int len;
        glGetProgramiv(program, 0x8B84, &len);
        char* str = malloc(len);
        glGetProgramInfoLog(program, len, ((void *)0), str);
        _debug_printf(2, "src/graphics-core.c", 98, "Shader linker error:%s", str);
        free(str);
        return 0;
    }
    return program;
}

canonical_original_code:
static unsigned int shader_link(unsigned int vertex, unsigned int fragment)
{
    unsigned int program = glCreateProgram();
    glAttachShader(program, vertex);
    glAttachShader(program, fragment);
    glLinkProgram(program);
    int status;
    glGetProgramiv(program, 0x8B82, &status);
    if(!status)
    {
        int len;
        glGetProgramiv(program, 0x8B84, &len);
        char *str = malloc(len);
        glGetProgramInfoLog(program, len, ((void *)0), str);
        _debug_printf(2, "src/graphics-core.c", 98, "Shader linker error:%s", str);
        free(str);
        return 0;
    }
    return program;
}

variable_types:
vertex
unsigned int

variable_types:
fragment
unsigned int

variable_types:
program
unsigned int


decompiled_code:
__int64 __fastcall graphics_flush(_QWORD *a1, unsigned __int8 *a2)
{
int v2; // edx
__int64 result; // rax
float v4; // xmm0_4
float v5; // xmm2_4
float v6; // xmm1_4
__int64 v7; // r8

if ( !a1 )
_assert_fail("g != 0", "src/graphics-core.c", 0xE1u, "graphics_flush");
v2 = eglSwapBuffers(*a1, a1[1]);
result = 0LL;
if ( v2 )
{
if ( a2 )
{
v4 = (double)*a2 / 255.0;
v5 = (double)a2[2] / 255.0;
v6 = (double)a2[1] / 255.0;
glClearColor(v4, v6, v5, 0.0);
glClear(0x4000LL);
}
v7 = (unsigned int)glGetError();
result = 1LL;
if ( (_DWORD)v7 )
{
debug_printf(2LL, "src/graphics-core.c", 238LL, "OpenGL error %d", v7);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, unsigned __int8 *a2)
{
int v2; // edx
__int64 result; // rax
float v4; // xmm0_4
float v5; // xmm2_4
float v6; // xmm1_4
__int64 v7; // r8

if ( !a1 )
_assert_fail("g != 0", "src/graphics-core.c", 0xE1u, "graphics_flush");
v2 = eglSwapBuffers(*a1, a1[1]);
result = 0LL;
if ( v2 )
{
if ( a2 )
{
v4 = (double)*a2 / 255.0;
v5 = (double)a2[2] / 255.0;
v6 = (double)a2[1] / 255.0;
glClearColor(v4, v6, v5, 0.0);
glClear(0x4000LL);
}
v7 = (unsigned int)glGetError();
result = 1LL;
if ( (_DWORD)v7 )
{
debug_printf(2LL, "src/graphics-core.c", 238LL, "OpenGL error %d", v7);
result = 0LL;
}
}
return result;
}

original_code:
int graphics_flush(graphics_t *g, const uint8_t *color)
{
    ;
    ((void) sizeof ((g != 0) ? 1 : 0), __extension__ ({ if (g != 0) ; else __assert_fail ("g != 0", "src/graphics-core.c", 225, __extension__ __PRETTY_FUNCTION__); }));
    if(!eglSwapBuffers(g->display, g->surface)) return 0;
    if(color)
    {
        glClearColor(color[0] / 255.0, color[1] / 255.0, color[2] / 255.0, 0);
        glClear(0x00004000);
    }
    int err = glGetError();
    if(err != 0)
    {
        _debug_printf(2, "src/graphics-core.c", 238, "OpenGL error %d", err);
        return 0;
    }
    return 1;
}

canonical_original_code:
int graphics_flush(struct _graphics *g, unsigned char *color)
{
    ;
    ((void) sizeof ((g != 0) ? 1 : 0), __extension__ ({ if (g != 0) ; else __assert_fail ("g != 0", "src/graphics-core.c", 225, __extension__ __PRETTY_FUNCTION__); }));
    if(!eglSwapBuffers(g->display, g->surface)) return 0;
    if(color)
    {
        glClearColor(color[0] / 255.0, color[1] / 255.0, color[2] / 255.0, 0);
        glClear(0x00004000);
    }
    int err = glGetError();
    if(err != 0)
    {
        _debug_printf(2, "src/graphics-core.c", 238, "OpenGL error %d", err);
        return 0;
    }
    return 1;
}

variable_types: struct _graphics { void *display; void *surface; void *context; int width; unsigned int vert; int attr_coord; } *

variable_types:
color
unsigned char *

variable_types: err int


decompiled_code:
void __fastcall graphics_free(void *ptr)
{
if ( !ptr )
_assert_fail("g != 0", "src/graphics-core.c", 0xF8u, "graphics_free");
glDeleteProgram(*((unsigned int *)ptr + 10));
glDeleteShader(*((unsigned int *)ptr + 8));
glDeleteShader(*((unsigned int *)ptr + 9));
eglMakeCurrent(*(_QWORD *)ptr, 0LL, 0LL, 0LL);
eglDestroyContext(*(_QWORD *)ptr);
eglDestroySurface(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
if ( !ptr )
_assert_fail("g != 0", "src/graphics-core.c", 0xF8u, "graphics_free");
glDeleteProgram(*((unsigned int *)ptr + 10));
glDeleteShader(*((unsigned int *)ptr + 8));
glDeleteShader(*((unsigned int *)ptr + 9));
eglMakeCurrent(*(_QWORD *)ptr, 0LL, 0LL, 0LL);
eglDestroyContext(*(_QWORD *)ptr);
eglDestroySurface(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void graphics_free(graphics_t *g)
{
    ;
    ((void) sizeof ((g != 0) ? 1 : 0), __extension__ ({ if (g != 0) ; else __assert_fail ("g != 0", "src/graphics-core.c", 248, __extension__ __PRETTY_FUNCTION__); }));
    glDeleteProgram(g->prog);
    glDeleteShader(g->vert);
    glDeleteShader(g->frag);
    eglMakeCurrent(g->display, ((EGLSurface) (0)), ((EGLSurface) (0)), ((EGLContext) (0)));
    eglDestroyContext(g->display, g->context);
    eglDestroySurface(g->display, g->surface);
    free(g);
}

canonical_original_code:
void graphics_free(struct _graphics *g)
{
    ;
    ((void) sizeof ((g != 0) ? 1 : 0), __extension__ ({ if (g != 0) ; else __assert_fail ("g != 0", "src/graphics-core.c", 248, __extension__ __PRETTY_FUNCTION__); }));
    glDeleteProgram(g->prog);
    glDeleteShader(g->vert);
    glDeleteShader(g->frag);
    eglMakeCurrent(g->display, ((EGLSurface) (0)), ((EGLSurface) (0)), ((EGLContext) (0)));
    eglDestroyContext(g->display, g->context);
    eglDestroySurface(g->display, g->surface);
    free(g);
}

user_defined_types: struct _graphics { void *display; void *surface; void *context; int width; unsigned int vert; int attr_coord; }


decompiled_code:
__int64 __fastcall graphics_label_set_color(__int64 a1, unsigned __int8 *a2)
{
float v2; // xmm0_4
float v3; // xmm0_4
float v4; // xmm0_4
__int64 result; // rax
float v6; // xmm0_4

if ( !a1 )
_assert_fail("d != 0", "src/graphics-text.c", 0x13Du, "graphics_label_set_color");
if ( !a2 )
_assert_fail("color != 0", "src/graphics-text.c", 0x13Eu, "graphics_label_set_color");
if ( *(_DWORD *)a1 != 1 )
_assert_fail("d->type == DRAWABLE_LABEL", "src/graphics-text.c", 0x13Fu, "graphics_label_set_color");
v2 = (double)*a2 / 255.0;
*(float *)(a1 + 36) = v2;
v3 = (double)a2[1] / 255.0;
*(float *)(a1 + 40) = v3;
v4 = (double)a2[2] / 255.0;
*(float *)(a1 + 44) = v4;
result = a2[3];
v6 = (double)(int)result / 255.0;
*(float *)(a1 + 32) = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 *a2)
{
float v2; // xmm0_4
float v3; // xmm0_4
float v4; // xmm0_4
__int64 result; // rax
float v6; // xmm0_4

if ( !a1 )
_assert_fail("d != 0", "src/graphics-text.c", 0x13Du, "graphics_label_set_color");
if ( !a2 )
_assert_fail("color != 0", "src/graphics-text.c", 0x13Eu, "graphics_label_set_color");
if ( *(_DWORD *)a1 != 1 )
_assert_fail("d->type == DRAWABLE_LABEL", "src/graphics-text.c", 0x13Fu, "graphics_label_set_color");
v2 = (double)*a2 / 255.0;
*(float *)(a1 + 36) = v2;
v3 = (double)a2[1] / 255.0;
*(float *)(a1 + 40) = v3;
v4 = (double)a2[2] / 255.0;
*(float *)(a1 + 44) = v4;
result = a2[3];
v6 = (double)(int)result / 255.0;
*(float *)(a1 + 32) = v6;
return result;
}

original_code:
void graphics_label_set_color(drawable_t *d, const uint8_t color[4])
{
    ;
    ((void) sizeof ((d != 0) ? 1 : 0), __extension__ ({ if (d != 0) ; else __assert_fail ("d != 0", "src/graphics-text.c", 317, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((color != 0) ? 1 : 0), __extension__ ({ if (color != 0) ; else __assert_fail ("color != 0", "src/graphics-text.c", 318, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((d->type == DRAWABLE_LABEL) ? 1 : 0), __extension__ ({ if (d->type == DRAWABLE_LABEL) ; else __assert_fail ("d->type == DRAWABLE_LABEL", "src/graphics-text.c", 319, __extension__ __PRETTY_FUNCTION__); }));
    d->color[0] = color[0] / 255.0;
    d->color[1] = color[1] / 255.0;
    d->color[2] = color[2] / 255.0;
    d->mask[3] = color[3] / 255.0;
}

canonical_original_code:
void graphics_label_set_color(struct _drawable *d, unsigned char color[4])
{
    ;
    ((void) sizeof ((d != 0) ? 1 : 0), __extension__ ({ if (d != 0) ; else __assert_fail ("d != 0", "src/graphics-text.c", 317, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((color != 0) ? 1 : 0), __extension__ ({ if (color != 0) ; else __assert_fail ("color != 0", "src/graphics-text.c", 318, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((d->type == DRAWABLE_LABEL) ? 1 : 0), __extension__ ({ if (d->type == DRAWABLE_LABEL) ; else __assert_fail ("d->type == DRAWABLE_LABEL", "src/graphics-text.c", 319, __extension__ __PRETTY_FUNCTION__); }));
    d->color[0] = color[0] / 255.0;
    d->color[1] = color[1] / 255.0;
    d->color[2] = color[2] / 255.0;
    d->mask[3] = color[3] / 255.0;
}

variable_types: struct _drawable { enum { DRAWABLE_BASE=0, DRAWABLE_LABEL=1, DRAWABLE_IMAGE=2 } type; unsigned int vbo; unsigned int mode; float mask[4]; } *

variable_types:
color
unsigned char [4]


decompiled_code:
void __fastcall graphics_atlas_free(void *ptr)
{
if ( !ptr )
_assert_fail("atlas != 0", "src/graphics-text.c", 0x14Au, "graphics_atlas_free");
glDeleteTextures(1LL, ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( !ptr )
_assert_fail("atlas != 0", "src/graphics-text.c", 0x14Au, "graphics_atlas_free");
glDeleteTextures(1LL, ptr);
free(ptr);
}

original_code:
void graphics_atlas_free(atlas_t *atlas)
{
    ;
    ((void) sizeof ((atlas != 0) ? 1 : 0), __extension__ ({ if (atlas != 0) ; else __assert_fail ("atlas != 0", "src/graphics-text.c", 330, __extension__ __PRETTY_FUNCTION__); }));
    glDeleteTextures(1, &atlas->texture);
    free(atlas);
}

canonical_original_code:
void graphics_atlas_free(struct _atlas *atlas)
{
    ;
    ((void) sizeof ((atlas != 0) ? 1 : 0), __extension__ ({ if (atlas != 0) ; else __assert_fail ("atlas != 0", "src/graphics-text.c", 330, __extension__ __PRETTY_FUNCTION__); }));
    glDeleteTextures(1, &atlas->texture);
    free(atlas);
}

variable_types: struct _atlas { unsigned int texture; struct { float left; float tex_x; float advance_x; } chars[-1]; } *


decompiled_code:
void __fastcall video_close(void *ptr)
{
unsigned __int64 v1; // rbp
int v2[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( !ptr )
_assert_fail("video != 0", "src/video.c", 0xCFu, "video_close");
v2[0] = 1;
if ( ioctl(*(_DWORD *)ptr, 0x40045613uLL, v2) == -1 )
debug_printf(2LL, "src/video.c", 213LL, "Failed to stop stream");
if ( *((_QWORD *)ptr + 9) )
{
v1 = 0LL;
do
{
munmap(*((void **)ptr + 2 * v1 + 1), *((_QWORD *)ptr + 2 * v1 + 2));
++v1;
}
while ( *((_QWORD *)ptr + 9) > v1 );
}
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
unsigned __int64 v1; // rbp
int v2[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( !ptr )
_assert_fail("video != 0", "src/video.c", 0xCFu, "video_close");
v2[0] = 1;
if ( ioctl(*(_DWORD *)ptr, 0x40045613uLL, v2) == -1 )
debug_printf(2LL, "src/video.c", 213LL, "Failed to stop stream");
if ( *((_QWORD *)ptr + 9) )
{
v1 = 0LL;
do
{
munmap(*((void **)ptr + 2 * v1 + 1), *((_QWORD *)ptr + 2 * v1 + 2));
++v1;
}
while ( *((_QWORD *)ptr + 9) > v1 );
}
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
void video_close(video_t *video)
{
    ;
    ((void) sizeof ((video != 0) ? 1 : 0), __extension__ ({ if (video != 0) ; else __assert_fail ("video != 0", "src/video.c", 207, __extension__ __PRETTY_FUNCTION__); }));
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if(ioctl(video->fd, (((1U) << (((0 +8)+8)+14)) | ((('V')) << (0 +8)) | (((19)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &type) == -1)
    {
        _debug_printf(2, "src/video.c", 213, "Failed to stop stream");
    }
    int i;
    for(i = 0; i < video->count; i++)
    {
        munmap(video->buffers[i].start, video->buffers[i].length);
    }
    close(video->fd);
    free(video);
}

canonical_original_code:
void video_close(struct _video *video)
{
    ;
    ((void) sizeof ((video != 0) ? 1 : 0), __extension__ ({ if (video != 0) ; else __assert_fail ("video != 0", "src/video.c", 207, __extension__ __PRETTY_FUNCTION__); }));
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if(ioctl(video->fd, (((1U) << (((0 +8)+8)+14)) | ((('V')) << (0 +8)) | (((19)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &type) == -1)
    {
        _debug_printf(2, "src/video.c", 213, "Failed to stop stream");
    }
    int i;
    for(i = 0; i < video->count; i++)
    {
        munmap(video->buffers[i].start, video->buffers[i].length);
    }
    close(video->fd);
    free(video);
}

variable_types: struct _video { int fd; struct { void *start; unsigned long length; } buffers[4]; unsigned long count; int index; } *

variable_types: enum v4l2_buf_type { V4L2_BUF_TYPE_VIDEO_CAPTURE=1, V4L2_BUF_TYPE_VIDEO_OUTPUT=2, V4L2_BUF_TYPE_VIDEO_OVERLAY=3, V4L2_BUF_TYPE_VBI_CAPTURE=4, V4L2_BUF_TYPE_VBI_OUTPUT=5, V4L2_BUF_TYPE_SLICED_VBI_CAPTURE=6, V4L2_BUF_TYPE_SLICED_VBI_OUTPUT=7, V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY=8, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE=9, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE=10, V4L2_BUF_TYPE_SDR_CAPTURE=11, V4L2_BUF_TYPE_SDR_OUTPUT=12, V4L2_BUF_TYPE_META_CAPTURE=13, V4L2_BUF_TYPE_PRIVATE=128 }

variable_types: i int


original_code:
float gps_util_dem_get_alt(struct dem *dem, double lat, double lon)
{
    ;
    ((void) sizeof ((dem != 0) ? 1 : 0), __extension__ ({ if (dem != 0) ; else __assert_fail ("dem != 0", "src/gps-util.c", 156, __extension__ __PRETTY_FUNCTION__); }));
    int x = (int)((lon - dem->left) / (dem->right - dem->left) * dem->width + 0.5);
    int y = (int)((dem->top - lat) / (dem->top - dem->bottom) * dem->height + 0.5);
    if((x < 0) || (y < 0) || (x >= dem->width) || (y >= dem->height)) return 0;
    return (float)(((uint16_t)dem->lines[y][x * 2] << 8) | (uint16_t)dem->lines[y][x * 2 + 1]) * dem->pixel_scale;
}

canonical_original_code:
float gps_util_dem_get_alt(struct dem *dem, double lat, double lon)
{
    ;
    ((void) sizeof ((dem != 0) ? 1 : 0), __extension__ ({ if (dem != 0) ; else __assert_fail ("dem != 0", "src/gps-util.c", 156, __extension__ __PRETTY_FUNCTION__); }));
    int x = (int)((lon - dem->left) / (dem->right - dem->left) * dem->width + 0.5);
    int y = (int)((dem->top - lat) / (dem->top - dem->bottom) * dem->height + 0.5);
    if((x < 0) || (y < 0) || (x >= dem->width) || (y >= dem->height)) return 0;
    return (float)(((unsigned short)dem->lines[y][x * 2] << 8) | (unsigned short)dem->lines[y][x * 2 + 1]) * dem->pixel_scale;
}

variable_types: struct dem { unsigned char **lines; unsigned int width; double left; float pixel_scale; } *

variable_types: lat double

variable_types: lon double

variable_types: x int

variable_types: y int

return_type: float


original_code:
long bsearch_ex(char * k, char ** a, long n) {
  long length = strlen(k);
  if(strncmp(k,a[0],length) <= 0) return 0;
  if(strncmp(a[n-1],k,length) < 0) return n;
  long l = 0, r = n-1;
  while(r - l > 1) {
    long c = (l+r)/2;
    if(strncmp(k,a[c],length) <= 0) r = c;
    else l = c;
  }
  if(strncmp(k,a[r],strlen(k)) != 0) return -1;
  return r;
}

canonical_original_code:
long bsearch_ex(char *k, char **a, long n) {
  long length = strlen(k);
  if(strncmp(k,a[0],length) <= 0) return 0;
  if(strncmp(a[n-1],k,length) < 0) return n;
  long l = 0; long r = n-1;
  while(r - l > 1) {
    long c = (l+r)/2;
    if(strncmp(k,a[c],length) <= 0) r = c;
    else l = c;
  }
  if(strncmp(k,a[r],strlen(k)) != 0) return -1;
  return r;
}

variable_types:
k
char *

variable_types:
a
char **

variable_types: n long

variable_types: length long

variable_types: l long

variable_types: r long

variable_types: c long

return_type: long


decompiled_code:
int __fastcall search1(char *s1, const char **a2, __int64 a3)
{
size_t v4; // r13
__int64 v5; // rax
__int64 v6; // rbx
__int64 v7; // r12
const char **v8; // r14

v4 = strlen(s1);
v5 = bsearch_ex(s1, a2, a3);
v6 = 0LL;
if ( v5 >= 0 )
{
if ( a3 <= v5 )
{
v6 = 0LL;
}
else
{
v7 = a3 - v5;
v8 = &a2[v5];
while ( !strncmp(s1, v8[v6], v4) )
{
if ( ++v6 == v7 )
{
v6 = v7;
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, v4, v6);
}
}
}
}
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, v4, v6);
}

canonical_decompiled_code:
int __fastcall func12(char *s1, const char **a2, __int64 a3)
{
size_t v4; // r13
__int64 v5; // rax
__int64 v6; // rbx
__int64 v7; // r12
const char **v8; // r14

v4 = strlen(s1);
v5 = func11(s1, a2, a3);
v6 = 0LL;
if ( v5 >= 0 )
{
if ( a3 <= v5 )
{
v6 = 0LL;
}
else
{
v7 = a3 - v5;
v8 = &a2[v5];
while ( !strncmp(s1, v8[v6], v4) )
{
if ( ++v6 == v7 )
{
v6 = v7;
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, v4, v6);
}
}
}
}
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, v4, v6);
}

original_code:
void search1(char * k, char ** a,long n) {
  long length = strlen(k), f = -1;
  long count = 0;
  f = bsearch_ex(k,a,n);
  long f0 = f;
  if(f0 >= 0) {
    for(;f0 < n;f0++) {
      if(strncmp(k,a[f0],length) != 0) break;
      if(f0 < n) count++;
    }
  }
  printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n",k,length,count);
}

canonical_original_code:
void search1(char *k, char **a, long n) {
  long length = strlen(k); long f = -1;
  long count = 0;
  f = bsearch_ex(k,a,n);
  long f0 = f;
  if(f0 >= 0) {
    for(;f0 < n;f0++) {
      if(strncmp(k,a[f0],length) != 0) break;
      if(f0 < n) count++;
    }
  }
  printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n",k,length,count);
}

variable_types: f long

variable_types: count long


search

decompiled_code:
int __fastcall search(char *s1, const char **a2, __int64 a3, __int64 a4)
{
__int64 v6; // rax
__int64 v7; // rbx
__int64 v8; // rbp
__int64 v9; // r12
int result; // eax
size_t n; // [rsp+0h] [rbp-48h]
__int64 v12; // [rsp+8h] [rbp-40h]

n = strlen(s1);
v6 = bsearch_ex(s1, a2, a3);
if ( v6 < 0 )
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, 0LL);
v7 = v6;
if ( a3 <= v6 )
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, 0LL);
v12 = a3 - v6;
v8 = v6;
v9 = 0LL;
while ( !strncmp(s1, a2[v8], n) )
{
++v9;
++v8;
if ( v9 == v12 )
{
v9 = v12;
break;
}
}
result = printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, v9);
if ( v8 > v7 )
{
do
result = printf("%ld\n", *(_QWORD *)(a4 + 8 * v7++));
while ( v8 != v7 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(char *s1, const char **a2, __int64 a3, __int64 a4)
{
__int64 v6; // rax
__int64 v7; // rbx
__int64 v8; // rbp
__int64 v9; // r12
int result; // eax
size_t n; // [rsp+0h] [rbp-48h]
__int64 v12; // [rsp+8h] [rbp-40h]

n = strlen(s1);
v6 = func11(s1, a2, a3);
if ( v6 < 0 )
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, 0LL);
v7 = v6;
if ( a3 <= v6 )
return printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, 0LL);
v12 = a3 - v6;
v8 = v6;
v9 = 0LL;
while ( !strncmp(s1, a2[v8], n) )
{
++v9;
++v8;
if ( v9 == v12 )
{
v9 = v12;
break;
}
}
result = printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n", s1, n, v9);
if ( v8 > v7 )
{
do
result = printf("%ld\n", *(_QWORD *)(a4 + 8 * v7++));
while ( v8 != v7 );
}
return result;
}

original_code:
void search(char * k, char ** a,long n, long * sa) {
  long length = strlen(k), f = -1;
  long count = 0;
  f = bsearch_ex(k,a,n);
  long f0 = f;
  if(f0 >= 0) {
    for(;f0 < n;f0++) {
      if(strncmp(k,a[f0],length) != 0) break;
      if(f0 < n) count++;
    }
  }
  printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n",k,length,count);
  for(;f<f0;f++) printf("%ld\n",sa[f]);
}

canonical_original_code:
void search(char *k, char **a, long n, long *sa) {
  long length = strlen(k); long f = -1;
  long count = 0;
  f = bsearch_ex(k,a,n);
  long f0 = f;
  if(f0 >= 0) {
    for(;f0 < n;f0++) {
      if(strncmp(k,a[f0],length) != 0) break;
      if(f0 < n) count++;
    }
  }
  printf("Found keyword: %s(length = %ld byte(s)) in %ld place(s)\n",k,length,count);
  for(;f<f0;f++) printf("%ld\n",sa[f]);
}

variable_types:
sa
long *


decompiled_code:
__int64 content_read()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
static int
content_read(Content *this, char *buff, int len)
{
 return 0;
}

canonical_original_code:
static int content_read(struct _content *this, char *buff, int len)
{
 return 0;
}

variable_types: struct _content { void *m_private; int m_length; char *m_type; int m_status; int (*read)(struct _content *content, char *buf, int len); void (*destroy)(struct _content *content); } *

variable_types:
buff
char *

variable_types: len int


decompiled_code:
void __fastcall contentfile_destroy(void ***a1)
{
void **v2; // rbp
int v3; // edi
void **v4; // rdi

v2 = *a1;
v3 = *((_DWORD *)*a1 + 2);
if ( v3 > 0 )
close(v3);
if ( *v2 )
free(*v2);
free(v2);
v4 = a1[2];
if ( v4 )
free(v4);
}

canonical_decompiled_code:
void __fastcall func1(void ***a1)
{
void **v2; // rbp
int v3; // edi
void **v4; // rdi

v2 = *a1;
v3 = *((_DWORD *)*a1 + 2);
if ( v3 > 0 )
close(v3);
if ( *v2 )
free(*v2);
free(v2);
v4 = a1[2];
if ( v4 )
free(v4);
}

original_code:
static void
contentfile_destroy(Content *this)
{
 ContentfileData *data = this->m_private;
 if (data->fd > 0)
  close(data->fd);
 if (data->filepath)
  free(data->filepath);
 free(data);
 if (this->m_type)
  free(this->m_type);
}

canonical_original_code:
static void contentfile_destroy(struct _content *this)
{
 struct ContentfileData *data = this->m_private;
 if (data->fd > 0)
  close(data->fd);
 if (data->filepath)
  free(data->filepath);
 free(data);
 if (this->m_type)
  free(this->m_type);
}

variable_types: struct ContentfileData { char *filepath; int fd; } *

user_defined_types: struct _content { void *m_private; int m_length; char *m_type; int m_status; int (*read)(struct _content *content, char *buf, int len); void (*destroy)(struct _content *content); }


decompiled_code:
ssize_t __fastcall contentfile_read(__int64 a1, void *a2, int a3)
{
int v3; // edi
ssize_t result; // rax

v3 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
result = 0LL;
if ( v3 > 0 )
result = read(v3, a2, a3);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func2(__int64 a1, void *a2, int a3)
{
int v3; // edi
ssize_t result; // rax

v3 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
result = 0LL;
if ( v3 > 0 )
result = read(v3, a2, a3);
return result;
}

original_code:
static int
contentfile_read(Content *this, char *buff, int len)
{
 ContentfileData *data = this->m_private;
 int ret = 0;
 if (data->fd > 0)
  ret = read(data->fd, buff, len);
 return ret;
}

canonical_original_code:
static int contentfile_read(struct _content *this, char *buff, int len)
{
 struct ContentfileData *data = this->m_private;
 int ret = 0;
 if (data->fd > 0)
  ret = read(data->fd, buff, len);
 return ret;
}

variable_types: ret int

user_defined_types: struct ContentfileData { char *filepath; int fd; }


decompiled_code:
_QWORD *content_new()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[5] = 0LL;
result[4] = content_read;
return result;
}

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[5] = 0LL;
result[4] = content_read;
return result;
}

original_code:
Content *
content_new()
{
 Content *this = malloc(sizeof(*this));
 memset(this, 0, sizeof(*this));
 this->read = content_read;
 return this;
}

canonical_original_code:
struct _content *content_new()
{
 struct _content *this = malloc(sizeof(*this));
 memset(this, 0, sizeof(*this));
 this->read = content_read;
 return this;
}

return_type: struct _content { void *m_private; int m_length; char *m_type; int m_status; int (*read)(struct _content *content, char *buf, int len); void (*destroy)(struct _content *content); } *


decompiled_code:
void __fastcall content_destroy(void *ptr)
{
void (*v1)(void); // rax

v1 = (void (*)(void))*((_QWORD *)ptr + 5);
if ( v1 )
v1();
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
void (*v1)(void); // rax

v1 = (void (*)(void))*((_QWORD *)ptr + 5);
if ( v1 )
v1();
free(ptr);
}

original_code:
void
content_destroy(Content *this)
{
 if (this->destroy)
  this->destroy(this);
 free(this);
 this = ((void *)0);
}

canonical_original_code:
void content_destroy(struct _content *this)
{
 if (this->destroy)
  this->destroy(this);
 free(this);
 this = ((void *)0);
}


decompiled_code:
__int64 __fastcall content_length(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int
content_length(Content *this)
{
 return this->m_length;
}

canonical_original_code:
int content_length(struct _content *this)
{
 return this->m_length;
}


decompiled_code:
__int64 __fastcall content_type(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
char *
content_type(Content *this)
{
 return this->m_type;
}

canonical_original_code:
char *content_type(struct _content *this)
{
 return this->m_type;
}

return_type: char *


mimetype

decompiled_code:
char *__fastcall mimetype(const char *a1)
{
char *result; // rax

if ( strstr(a1, ".html") )
result = g_mimetype[1];
else
result = g_mimetype[0];
return result;
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
char *result; // rax

if ( strstr(a1, ".html") )
result = g_mimetype[1];
else
result = g_mimetype[0];
return result;
}

original_code:
char *
mimetype(char *filepath)
{
 if (strstr(filepath, ".html"))
  return g_mimetype[1];
 return g_mimetype[0];
}

canonical_original_code:
char *mimetype(char *filepath)
{
 if (strstr(filepath, ".html"))
  return g_mimetype[1];
 return g_mimetype[0];
}

variable_types:
filepath
char *


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

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

original_code:
void
httpresponse_destroy(HttpResponse *p_http)
{
 free(p_http);
}

canonical_original_code:
void httpresponse_destroy(struct _httpresponse *p_http)
{
 free(p_http);
}

variable_types: struct _httpresponse { enum { E_ResponseStatus=0, E_ResponseHeader=1, E_ResponseContent=2, E_ResponseEnd=3 } m_state; struct _content { void *m_private; int m_length; char *m_type; int m_status; int (*read)(struct _content *content, char *buf, int len); void (*destroy)(struct _content *content); } *m_Content; char *m_header; char *m_ptr; int m_status; } *

user_defined_types: struct _httpresponse { enum { E_ResponseStatus=0, E_ResponseHeader=1, E_ResponseContent=2, E_ResponseEnd=3 } m_state; struct _content *m_Content; char *m_header; char *m_ptr; int m_status; }


original_code:
int
httpresponse_format(HttpResponse *p_http, char *buff, int len)
{
 int ret = 0;
 switch (p_http->m_state)
 {
  case E_ResponseStatus:
   if (p_http->m_Content->m_status > -1)
   {
    if (p_http->m_ptr == ((void *)0))
     p_http->m_ptr = g_status[p_http->m_Content->m_status];
    p_http->m_ptr = stpncpy(buff, p_http->m_ptr, len);
   }
   if (!p_http->m_ptr)
    return -1;
   else if (*p_http->m_ptr != 0 )
    return len;
   else
   {
    p_http->m_ptr = ((void *)0);
    p_http->m_state = E_ResponseHeader;
    ret = strlen(g_status[p_http->m_Content->m_status]);
   }
  break;
  case E_ResponseHeader:
   if (p_http->m_Content != ((void *)0))
   {
    if (p_http->m_header == ((void *)0))
    {
     int len = 256;
     int ret = 0;
     do
     {
      p_http->m_header = malloc(len);
      ret = snprintf(p_http->m_header, len,
        "Content-Type: %s\n\r"
        "Content-Length: %d\n\r\n\r",
        (p_http->m_Content->m_type)?p_http->m_Content->m_type:"",
        p_http->m_Content->m_length);
      if (ret < len)
       break;
      len += 64;
      free(p_http->m_header);
     } while (1);
     p_http->m_ptr = p_http->m_header;
    }
    strncpy(buff, p_http->m_ptr, len);
   }
   if (!p_http->m_ptr)
    return -1;
   else if (*(p_http->m_ptr + len) != 0 )
   {
    p_http->m_ptr += len;
    return len;
   }
   else
   {
    ret = strlen(p_http->m_ptr);
    free(p_http->m_header);
    p_http->m_header = ((void *)0);
    p_http->m_ptr = ((void *)0);
    if (p_http->m_Content->m_status == 0)
     p_http->m_state = E_ResponseContent;
    else
     p_http->m_state = E_ResponseEnd;
   }
  break;
  case E_ResponseContent:
   if (p_http->m_Content != ((void *)0))
   {
    if (p_http->m_Content->read != ((void *)0))
     ret = p_http->m_Content->read(p_http->m_Content, buff, len);
    if (ret == 0)
     p_http->m_state = E_ResponseEnd;
   }
   else
    p_http->m_state = E_ResponseEnd;
  break;
 }
 return ret;
}

canonical_original_code:
int httpresponse_format(struct _httpresponse *p_http, char *buff, int len)
{
 int ret = 0;
 switch (p_http->m_state)
 {
  case E_ResponseStatus:
   if (p_http->m_Content->m_status > -1)
   {
    if (p_http->m_ptr == ((void *)0))
     p_http->m_ptr = g_status[p_http->m_Content->m_status];
    p_http->m_ptr = stpncpy(buff, p_http->m_ptr, len);
   }
   if (!p_http->m_ptr)
    return -1;
   else if (*p_http->m_ptr != 0 )
    return len;
   else
   {
    p_http->m_ptr = ((void *)0);
    p_http->m_state = E_ResponseHeader;
    ret = strlen(g_status[p_http->m_Content->m_status]);
   }
  break;
  case E_ResponseHeader:
   if (p_http->m_Content != ((void *)0))
   {
    if (p_http->m_header == ((void *)0))
    {
     int len = 256;
     int ret = 0;
     do
     {
      p_http->m_header = malloc(len);
      ret = snprintf(p_http->m_header, len,
        "Content-Type: %s\n\r"
        "Content-Length: %d\n\r\n\r",
        (p_http->m_Content->m_type)?p_http->m_Content->m_type:"",
        p_http->m_Content->m_length);
      if (ret < len)
       break;
      len += 64;
      free(p_http->m_header);
     } while (1);
     p_http->m_ptr = p_http->m_header;
    }
    strncpy(buff, p_http->m_ptr, len);
   }
   if (!p_http->m_ptr)
    return -1;
   else if (*(p_http->m_ptr + len) != 0 )
   {
    p_http->m_ptr += len;
    return len;
   }
   else
   {
    ret = strlen(p_http->m_ptr);
    free(p_http->m_header);
    p_http->m_header = ((void *)0);
    p_http->m_ptr = ((void *)0);
    if (p_http->m_Content->m_status == 0)
     p_http->m_state = E_ResponseContent;
    else
     p_http->m_state = E_ResponseEnd;
   }
  break;
  case E_ResponseContent:
   if (p_http->m_Content != ((void *)0))
   {
    if (p_http->m_Content->read != ((void *)0))
     ret = p_http->m_Content->read(p_http->m_Content, buff, len);
    if (ret == 0)
     p_http->m_state = E_ResponseEnd;
   }
   else
    p_http->m_state = E_ResponseEnd;
  break;
 }
 return ret;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
char *v4; // r12
int v5; // ebp
const char *v6; // r12
int v7; // ebp
int v8; // eax
int v9; // ebx
__int64 v10; // rbp
__int64 v11; // rbx
__int64 v12; // rdx
int v13; // eax
char v15[168]; // [rsp+0h] [rbp-A8h] BYREF

v3 = httprequest_new(argc, argv, envp);
v4 = v15;
v5 = 128;
LABEL_5:
do
{
v7 = read(0, v4, (unsigned int)(v5 - 1));
v4[v7] = 0;
if ( v7 <= 0 )
break;
v6 = v15;
while ( 1 )
{
v8 = httprequest_parse(v3, v6, (unsigned int)v7);
v9 = v8;
if ( v8 <= 0 )
break;
v6 += v8;
v7 -= v8;
if ( v7 <= 0 )
{
v5 = 128 - strlen(v6);
v4 = stpcpy(v15, v6);
goto LABEL_5;
}
}
v5 = 128 - strlen(v6);
v4 = stpcpy(v15, v6);
}
while ( v9 != -1 );
v10 = contentfile_new("/home/http", *(_QWORD *)(v3 + 8));
v11 = httpresponse_new(v10);
do
{
v13 = httpresponse_format(v11, v15, 128LL);
v12 = (unsigned int)v13;
}
while ( v13 > 0 && (int)write(1, v15, (unsigned int)v13) > 0 );
content_destroy(v10, v15, v12);
httpresponse_destroy(v11);
httprequest_destroy(v3);
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 int ret = 1;
 HttpRequest *req;
 req = httprequest_new();
 char buff[128];
 int len = sizeof(buff);
 char *ptr = buff;
 while ((ret != -1))
 {
  len = read(0, ptr, len - 1);
  ptr[len] = 0;
  if (len < 1)
   break;
  ptr = buff;
  ret = 1;
  while ((len > 0) && (ret > 0))
  {
   ret = httprequest_parse(req, ptr, len);
   if (ret > 0)
   {
    ptr += ret;
    len -= ret;
   }
  }
  len = sizeof(buff) - strlen(ptr);
  ptr = stpcpy(buff, ptr);
 }
 Content *content = contentfile_new("/home/http", req->m_filepath);
 HttpResponse *response = httpresponse_new(content);
 do
 {
  len = sizeof(buff);
  len = httpresponse_format(response, buff, len);
  if (len > 0)
   len = write(1, buff, len);
 } while (len > 0);
 content_destroy(content);
 httpresponse_destroy(response);
 httprequest_destroy(req);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int ret = 1;
 struct _httprequest *req;
 req = httprequest_new();
 char buff[128];
 int len = sizeof(buff);
 char *ptr = buff;
 while ((ret != -1))
 {
  len = read(0, ptr, len - 1);
  ptr[len] = 0;
  if (len < 1)
   break;
  ptr = buff;
  ret = 1;
  while ((len > 0) && (ret > 0))
  {
   ret = httprequest_parse(req, ptr, len);
   if (ret > 0)
   {
    ptr += ret;
    len -= ret;
   }
  }
  len = sizeof(buff) - strlen(ptr);
  ptr = stpcpy(buff, ptr);
 }
 struct _content *content = contentfile_new("/home/http", req->m_filepath);
 struct _httpresponse *response = httpresponse_new(content);
 do
 {
  len = sizeof(buff);
  len = httpresponse_format(response, buff, len);
  if (len > 0)
   len = write(1, buff, len);
 } while (len > 0);
 content_destroy(content);
 httpresponse_destroy(response);
 httprequest_destroy(req);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct _httprequest { enum { E_ParseEnd=0, E_ParseAction=1, E_ParseURL=2, E_ParseGet=3, E_ParseHeader=4, E_ParseBody=5 } m_state; enum { E_Get=0, E_Head=1, E_Post=2 } m_cmd; char *m_filepath; char *m_host; int m_port; char *m_useragent; struct { char *name; char *value; } m_get[50]; int m_get_nb; struct { enum { E_File=0, E_Form=1 } type; int length; char *data; char *ptr; } m_content; } *

variable_types:
buff
char [128]

variable_types:
ptr
char *


decompiled_code:
_DWORD *httprequest_new()
{
_DWORD *v0; // rdx

v0 = malloc(0x368uLL);
memset(v0, 0, 0x368uLL);
*v0 = 1;
return v0;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rdx

v0 = malloc(0x368uLL);
memset(v0, 0, 0x368uLL);
*v0 = 1;
return v0;
}

original_code:
HttpRequest *
httprequest_new()
{
 HttpRequest *http;
  http = malloc(sizeof(*http));
 memset(http, 0, sizeof(*http));
 http->m_state = E_ParseAction;
 return http;
}

canonical_original_code:
struct _httprequest *httprequest_new()
{
 struct _httprequest *http;
  http = malloc(sizeof(*http));
 memset(http, 0, sizeof(*http));
 http->m_state = E_ParseAction;
 return http;
}

return_type: struct _httprequest { enum { E_ParseEnd=0, E_ParseAction=1, E_ParseURL=2, E_ParseGet=3, E_ParseHeader=4, E_ParseBody=5 } m_state; enum { E_Get=0, E_Head=1, E_Post=2 } m_cmd; char *m_filepath; char *m_host; int m_port; char *m_useragent; struct { char *name; char *value; } m_get[50]; int m_get_nb; struct { enum { E_File=0, E_Form=1 } type; int length; char *data; char *ptr; } m_content; } *


decompiled_code:
void __fastcall httprequest_destroy(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 107);
if ( v5 )
free(v5);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 107);
if ( v5 )
free(v5);
free(ptr);
}

original_code:
void httprequest_destroy(HttpRequest *p_http)
{
 if (p_http->m_filepath)
 {
  free(p_http->m_filepath);
  p_http->m_filepath = ((void *)0);
 }
 if (p_http->m_host)
 {
  free(p_http->m_host);
 }
 if (p_http->m_useragent)
 {
  free(p_http->m_useragent);
  p_http->m_useragent = ((void *)0);
 }
 if (p_http->m_content.data)
 {
  free(p_http->m_content.data);
  p_http->m_content.data = ((void *)0);
  p_http->m_content.length = 0;
 }
 free(p_http);
 p_http = ((void *)0);
}

canonical_original_code:
void httprequest_destroy(struct _httprequest *p_http)
{
 if (p_http->m_filepath)
 {
  free(p_http->m_filepath);
  p_http->m_filepath = ((void *)0);
 }
 if (p_http->m_host)
 {
  free(p_http->m_host);
 }
 if (p_http->m_useragent)
 {
  free(p_http->m_useragent);
  p_http->m_useragent = ((void *)0);
 }
 if (p_http->m_content.data)
 {
  free(p_http->m_content.data);
  p_http->m_content.data = ((void *)0);
  p_http->m_content.length = 0;
 }
 free(p_http);
 p_http = ((void *)0);
}

user_defined_types: struct _httprequest { enum { E_ParseEnd=0, E_ParseAction=1, E_ParseURL=2, E_ParseGet=3, E_ParseHeader=4, E_ParseBody=5 } m_state; enum { E_Get=0, E_Head=1, E_Post=2 } m_cmd; char *m_filepath; char *m_host; int m_port; char *m_useragent; struct { char *name; char *value; } m_get[50]; int m_get_nb; struct { enum { E_File=0, E_Form=1 } type; int length; char *data; char *ptr; } m_content; }


original_code:
int
httprequest_parse(HttpRequest *p_http, char *p_buffer, int p_length)
{
 char *ptr = p_buffer;
 char *tmp;
 int ret = -1;
 if (p_length == 0)
  return 0;
 do
 {
  switch (p_http->m_state)
  {
   case E_ParseAction:
   {
    if (!strncmp(ptr, "GET ", 4))
    {
     p_http->m_state = E_ParseURL;
     p_http->m_cmd = E_Get;
     ptr += 4;
    }
    else if (!strncmp(ptr, "HEAD ", 5))
    {
     p_http->m_state = E_ParseHeader;
     p_http->m_cmd = E_Head;
     ptr += 5;
    }
    else if (!strncmp(ptr, "POST ", 5))
    {
     p_http->m_state = E_ParseURL;
     p_http->m_cmd = E_Post;
     ptr += 5;
    }
    else
     ret = ptr - p_buffer;
   break;
   case E_ParseURL:
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     while (*ptr++ != '/');
     ptr = strstore(&p_http->m_filepath, ptr, " ?\n\r");
     if (*ptr == '?')
     {
      ptr++;
      p_http->m_state = E_ParseGet;
     }
     else if (*ptr == ' ')
     {
      ptr = tmp;
      p_http->m_state = E_ParseHeader;
     }
     else if (*ptr == '\r'|| *ptr == '\n')
     {
      ptr = tmp;
      p_http->m_state = E_ParseHeader;
     }
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseGet:
   {
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     char *equal;
     p_http->m_get[p_http->m_get_nb].name = ((void *)0);
     ptr = strstore(&(p_http->m_get[p_http->m_get_nb].name), ptr, " &\n\r");
     equal = strchrnul(p_http->m_get[p_http->m_get_nb].name, '=');
     if (*equal != '\0')
     {
      p_http->m_get[p_http->m_get_nb].value = equal + 1;
      *equal = '\0';
     }
     p_http->m_get_nb++;
     ptr = tmp;
     p_http->m_state = E_ParseHeader;
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseHeader:
   {
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     if (!strncmp(ptr, "Host: ", 6))
     {
      ptr += 6;
      ptr = strstore(&p_http->m_host, ptr, " :\n\r");
      char *ddot = strchrnul(p_http->m_host, ':');
      if (*ddot == ':')
      {
       p_http->m_port = atoi(ddot + 1);
       *ddot = 0;
      }
      else
      {
       p_http->m_port = 80;
      }
     }
     else if (!strncmp(ptr, "User-Agent: ", 12))
     {
      ptr += 12;
      ptr = strstore(&p_http->m_useragent, ptr, "\n\r");
     }
     else if (!strncmp(ptr, "Content-Type: ", 14))
     {
      ptr += 14;
      if (!strncmp(ptr, "application/x-www-form-urlencoded", 32))
      {
       ptr += 32;
       p_http->m_content.type = E_Form;
      }
     }
     else if (!strncmp(ptr, "Content-Length: ", 16))
     {
      ptr += 16;
      p_http->m_content.length = atoi(ptr);
     }
     else if (*ptr == '\n' || *ptr == '\r')
     {
      p_http->m_state = E_ParseBody;
     }
     ptr = tmp;
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseBody:
   {
    if (p_http->m_content.data)
    {
     p_http->m_content.data = malloc(p_http->m_content.length + 1);
     p_http->m_content.ptr = p_http->m_content.data;
    }
    memcpy(p_http->m_content.ptr, ptr, p_length);
    p_http->m_content.ptr += p_length;
    if (p_http->m_content.ptr - p_http->m_content.data == p_http->m_content.length)
     p_http->m_state = E_ParseEnd;
   }
   break;
   case E_ParseEnd:
    ret = 0;
   break;
  }
 } while (ret < 0);
 ptr = stpncpy(p_buffer, ptr, ret);
 return ret;
}

canonical_original_code:
int httprequest_parse(struct _httprequest *p_http, char *p_buffer, int p_length)
{
 char *ptr = p_buffer;
 char *tmp;
 int ret = -1;
 if (p_length == 0)
  return 0;
 do
 {
  switch (p_http->m_state)
  {
   case E_ParseAction:
   {
    if (!strncmp(ptr, "GET ", 4))
    {
     p_http->m_state = E_ParseURL;
     p_http->m_cmd = E_Get;
     ptr += 4;
    }
    else if (!strncmp(ptr, "HEAD ", 5))
    {
     p_http->m_state = E_ParseHeader;
     p_http->m_cmd = E_Head;
     ptr += 5;
    }
    else if (!strncmp(ptr, "POST ", 5))
    {
     p_http->m_state = E_ParseURL;
     p_http->m_cmd = E_Post;
     ptr += 5;
    }
    else
     ret = ptr - p_buffer;
   break;
   case E_ParseURL:
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     while (*ptr++ != '/');
     ptr = strstore(&p_http->m_filepath, ptr, " ?\n\r");
     if (*ptr == '?')
     {
      ptr++;
      p_http->m_state = E_ParseGet;
     }
     else if (*ptr == ' ')
     {
      ptr = tmp;
      p_http->m_state = E_ParseHeader;
     }
     else if (*ptr == '\r'|| *ptr == '\n')
     {
      ptr = tmp;
      p_http->m_state = E_ParseHeader;
     }
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseGet:
   {
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     char *equal;
     p_http->m_get[p_http->m_get_nb].name = ((void *)0);
     ptr = strstore(&(p_http->m_get[p_http->m_get_nb].name), ptr, " &\n\r");
     equal = strchrnul(p_http->m_get[p_http->m_get_nb].name, '=');
     if (*equal != '\0')
     {
      p_http->m_get[p_http->m_get_nb].value = equal + 1;
      *equal = '\0';
     }
     p_http->m_get_nb++;
     ptr = tmp;
     p_http->m_state = E_ParseHeader;
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseHeader:
   {
    tmp = _endline(ptr);
    if (*tmp != 0)
    {
     if (!strncmp(ptr, "Host: ", 6))
     {
      ptr += 6;
      ptr = strstore(&p_http->m_host, ptr, " :\n\r");
      char *ddot = strchrnul(p_http->m_host, ':');
      if (*ddot == ':')
      {
       p_http->m_port = atoi(ddot + 1);
       *ddot = 0;
      }
      else
      {
       p_http->m_port = 80;
      }
     }
     else if (!strncmp(ptr, "User-Agent: ", 12))
     {
      ptr += 12;
      ptr = strstore(&p_http->m_useragent, ptr, "\n\r");
     }
     else if (!strncmp(ptr, "Content-Type: ", 14))
     {
      ptr += 14;
      if (!strncmp(ptr, "application/x-www-form-urlencoded", 32))
      {
       ptr += 32;
       p_http->m_content.type = E_Form;
      }
     }
     else if (!strncmp(ptr, "Content-Length: ", 16))
     {
      ptr += 16;
      p_http->m_content.length = atoi(ptr);
     }
     else if (*ptr == '\n' || *ptr == '\r')
     {
      p_http->m_state = E_ParseBody;
     }
     ptr = tmp;
    }
    else
     ret = ptr - p_buffer;
   }
   break;
   case E_ParseBody:
   {
    if (p_http->m_content.data)
    {
     p_http->m_content.data = malloc(p_http->m_content.length + 1);
     p_http->m_content.ptr = p_http->m_content.data;
    }
    memcpy(p_http->m_content.ptr, ptr, p_length);
    p_http->m_content.ptr += p_length;
    if (p_http->m_content.ptr - p_http->m_content.data == p_http->m_content.length)
     p_http->m_state = E_ParseEnd;
   }
   break;
   case E_ParseEnd:
    ret = 0;
   break;
  }
 } while (ret < 0);
 ptr = stpncpy(p_buffer, ptr, ret);
 return ret;
}

variable_types:
tmp
char *

variable_types:
equal
char *

variable_types:
ddot
char *


strstore

decompiled_code:
char *__fastcall strstore(char **a1, const char *a2, const char *a3)
{
char *v3; // rbx

v3 = strpbrk(a2, a3);
if ( !v3 )
{
v3 = (char *)a2;
do
++v3;
while ( *(v3 - 1) );
}
if ( a1 )
{
if ( !*a1 )
*a1 = (char *)malloc(v3 - a2 + 1);
*stpncpy(*a1, a2, v3 - a2) = 0;
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func0(char **a1, const char *a2, const char *a3)
{
char *v3; // rbx

v3 = strpbrk(a2, a3);
if ( !v3 )
{
v3 = (char *)a2;
do
++v3;
while ( *(v3 - 1) );
}
if ( a1 )
{
if ( !*a1 )
*a1 = (char *)malloc(v3 - a2 + 1);
*stpncpy(*a1, a2, v3 - a2) = 0;
}
return v3;
}

original_code:
char *strstore(char **p_output, char *p_input, char *p_tags)
{
 char *last;
 char *end = strpbrk(p_input, p_tags);
 if (!end)
 {
  end = p_input;
  while (*end++) ;
 }
 if (p_output)
 {
  if (!*p_output)
  {
   *p_output = malloc(end - p_input + 1);
  }
  last = stpncpy(*p_output, p_input, end - p_input) - 1;
  *(last+1) = 0;
 }
 return end;
}

variable_types:
last
char *

variable_types:
end
char *