decompiled_code:
char *__fastcall queue_new(char *src)
{
char *v1; // rax
char *v2; // r12

v1 = (char *)malloc(0x138uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, src);
*((_QWORD *)v2 + 32) = 0LL;
*((_QWORD *)v2 + 33) = 0LL;
pthread_mutex_init((pthread_mutex_t *)(v2 + 272), 0LL);
}
else
{
LOG("error: creation of queue failed.\n");
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(char *src)
{
char *v1; // rax
char *v2; // r12

v1 = (char *)malloc(0x138uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, src);
*((_QWORD *)v2 + 32) = 0LL;
*((_QWORD *)v2 + 33) = 0LL;
pthread_mutex_init((pthread_mutex_t *)(v2 + 272), 0LL);
}
else
{
LOG("error: creation of queue failed.\n");
}
return v2;
}

original_code:
queue_t* queue_new(char *name) {
    queue_t *queue;
    queue = (queue_t *)malloc(sizeof(queue_t));
    if (queue == ((void *)0)) {
        LOG("error: creation of queue failed.\n");
        return queue;
    } else {
        strcpy(queue->name, name);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        pthread_mutex_init(&queue->mtx, ((void *)0));
    }
    return queue;
}

canonical_original_code:
struct queue_ *queue_new(char *name) {
    struct queue_ *queue;
    queue = (struct queue_ *)malloc(sizeof(queue_t));
    if (queue == ((void *)0)) {
        LOG("error: creation of queue failed.\n");
        return queue;
    } else {
        strcpy(queue->name, name);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        pthread_mutex_init(&queue->mtx, ((void *)0));
    }
    return queue;
}

variable_types:
name
char *

variable_types: struct queue_ { char name[256]; struct queue_node_ { struct queue_node_ *prev; struct queue_node_ *next; void *data; _Bool signal; } *head; struct queue_node_ *tail; 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; } mtx; } *

user_defined_types: struct queue_ { char name[256]; struct queue_node_ *head; struct queue_node_ *tail; union pthread_mutex_t mtx; }

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


repo: vijkp/Remodel

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

if ( !a1 )
return 1LL;
result = 0LL;
if ( *(_QWORD *)(a1 + 256) )
return result;
if ( !*(_QWORD *)(a1 + 264) )
return 1LL;
LOG("queue: '%s' has something wrong.\n", (const char *)a1);
return 1LL;
}

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

if ( !a1 )
return 1LL;
result = 0LL;
if ( *(_QWORD *)(a1 + 256) )
return result;
if ( !*(_QWORD *)(a1 + 264) )
return 1LL;
LOG("queue: '%s' has something wrong.\n", (const char *)a1);
return 1LL;
}

original_code:
bool queue_is_empty(queue_t *queue) {
    if (queue == ((void *)0)) {
        return 1;
    }
    if ((queue->head == ((void *)0)) && (queue->tail == ((void *)0))) {
        return 1;
    }
    if ((queue->head == ((void *)0)) || (queue->head == ((void *)0))) {
        LOG("queue: '%s' has something wrong.\n", queue->name);
        return 1;
    }
    return 0;
}

canonical_original_code:
_Bool queue_is_empty(struct queue_ *queue) {
    if (queue == ((void *)0)) {
        return 1;
    }
    if ((queue->head == ((void *)0)) && (queue->tail == ((void *)0))) {
        return 1;
    }
    if ((queue->head == ((void *)0)) || (queue->head == ((void *)0))) {
        LOG("queue: '%s' has something wrong.\n", queue->name);
        return 1;
    }
    return 0;
}

user_defined_types: struct queue_node_ { struct queue_node_ *prev; struct queue_node_ *next; void *data; _Bool signal; }

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


decompiled_code:
__int64 __fastcall queue_add_node(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
_QWORD *v3; // rax

if ( a1 )
{
if ( a2 )
{
result = queue_is_empty(a1);
if ( (_BYTE)result )
{
*(_QWORD *)(a1 + 256) = a2;
*(_QWORD *)(a1 + 264) = a2;
a2[1] = 0LL;
}
else
{
v3 = *(_QWORD **)(a1 + 256);
a2[1] = v3;
*v3 = a2;
result = 1LL;
*(_QWORD *)(a1 + 256) = a2;
}
*a2 = 0LL;
}
else
{
LOG("error: node add to a queue failed: node doesn't exist.\n");
result = 0LL;
}
}
else
{
LOG("error: node add to a queue failed: queue doesn't exist.\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
_QWORD *v3; // rax

if ( a1 )
{
if ( a2 )
{
result = func1(a1);
if ( (_BYTE)result )
{
*(_QWORD *)(a1 + 256) = a2;
*(_QWORD *)(a1 + 264) = a2;
a2[1] = 0LL;
}
else
{
v3 = *(_QWORD **)(a1 + 256);
a2[1] = v3;
*v3 = a2;
result = 1LL;
*(_QWORD *)(a1 + 256) = a2;
}
*a2 = 0LL;
}
else
{
LOG("error: node add to a queue failed: node doesn't exist.\n");
result = 0LL;
}
}
else
{
LOG("error: node add to a queue failed: queue doesn't exist.\n");
result = 0LL;
}
return result;
}

original_code:
bool queue_add_node(queue_t* queue, queue_node_t *node) {
    if (queue == ((void *)0)) {
        LOG("error: node add to a queue failed: queue doesn't exist.\n");
        return 0;
    }
    if (node == ((void *)0)) {
        LOG("error: node add to a queue failed: node doesn't exist.\n");
        return 0;
    }
    if (queue_is_empty(queue)) {
        queue->head = node;
        queue->tail = node;
        node->next = ((void *)0);
        node->prev = ((void *)0);
    } else {
        node->next = queue->head;
        queue->head->prev = node;
        queue->head = node;
        node->prev = ((void *)0);
    }
    return 1;
}

canonical_original_code:
_Bool queue_add_node(struct queue_ *queue, struct queue_node_ *node) {
    if (queue == ((void *)0)) {
        LOG("error: node add to a queue failed: queue doesn't exist.\n");
        return 0;
    }
    if (node == ((void *)0)) {
        LOG("error: node add to a queue failed: node doesn't exist.\n");
        return 0;
    }
    if (queue_is_empty(queue)) {
        queue->head = node;
        queue->tail = node;
        node->next = ((void *)0);
        node->prev = ((void *)0);
    } else {
        node->next = queue->head;
        queue->head->prev = node;
        queue->head = node;
        node->prev = ((void *)0);
    }
    return 1;
}

variable_types:
node
struct queue_node_ *


decompiled_code:
int __fastcall queue_print_all_nodes(__int64 a1)
{
__int64 i; // rbx

if ( !a1 )
return LOG("error: queue doesn't exist.\n");
LOG("queue: '%s' head %x tail %x ", (const char *)a1, *(_QWORD *)(a1 + 256), *(_QWORD *)(a1 + 264));
if ( !(unsigned __int8)queue_is_empty(a1) )
{
for ( i = *(_QWORD *)(a1 + 256); i; i = *(_QWORD *)(i + 8) )
printf("%s -> ", *(const char **)(i + 16));
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 i; // rbx

if ( !a1 )
return LOG("error: queue doesn't exist.\n");
LOG("queue: '%s' head %x tail %x ", (const char *)a1, *(_QWORD *)(a1 + 256), *(_QWORD *)(a1 + 264));
if ( !(unsigned __int8)func1(a1) )
{
for ( i = *(_QWORD *)(a1 + 256); i; i = *(_QWORD *)(i + 8) )
printf("%s -> ", *(const char **)(i + 16));
}
return putchar(10);
}

original_code:
void queue_print_all_nodes(queue_t *queue) {
    queue_node_t *node = ((void *)0);
    remodel_node_t *rmnode = ((void *)0);
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    LOG("queue: '%s' head %x tail %x ", queue->name,
            queue->head, queue->tail);
    if (queue_is_empty(queue)) {
        printf("\n");
        return;
    }
    node = queue->head;
    while (node != ((void *)0)) {
        rmnode = (remodel_node_t *)node->data;
        printf("%s -> ", rmnode->name);
        node = node->next;
    }
    printf("\n");
}

canonical_original_code:
void queue_print_all_nodes(struct queue_ *queue) {
    struct queue_node_ *node = ((void *)0);
    struct remodel_node_ *rmnode = ((void *)0);
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    LOG("queue: '%s' head %x tail %x ", queue->name,
            queue->head, queue->tail);
    if (queue_is_empty(queue)) {
        printf("\n");
        return;
    }
    node = queue->head;
    while (node != ((void *)0)) {
        rmnode = (struct remodel_node_ *)node->data;
        printf("%s -> ", rmnode->name);
        node = node->next;
    }
    printf("\n");
}

variable_types: struct queue_node_ { struct queue_node_ *prev; struct queue_node_ *next; void *data; _Bool signal; } *

variable_types: struct remodel_node_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct target_ { char name[256]; char *command; struct dependency_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct dependency_ *next; } *dp_head; struct target_ *next; enum build_state_t { RM_BUILD_DONE=0, RM_BUILD_READY=1, RM_BUILD_ONQ=2, RM_BUILD_RUNNING=3 } build_state; int total_dp; int changed_dp; } *target; struct srcfile_ { char name[256]; char md5hash[-1]; _Bool md5_present; _Bool md5_changed; struct srcfile_ *next; } *srcfile; struct remodel_node_ *parent; struct remodel_node_ **children; int child_nodes; } *

return_type: void

user_defined_types: struct remodel_node_ { char name[256]; enum rm_file_type_t type; struct target_ *target; struct srcfile_ *srcfile; struct remodel_node_ *parent; struct remodel_node_ **children; int child_nodes; }

user_defined_types: struct target_ { char name[256]; char *command; struct dependency_ *dp_head; struct target_ *next; enum build_state_t build_state; int total_dp; int changed_dp; }

user_defined_types: struct dependency_ { char name[256]; enum rm_file_type_t type; struct dependency_ *next; }


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

v0 = malloc(0x20uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
*((_BYTE *)v0 + 24) = 0;
}
else
{
LOG("error: memory allocation failed.\n");
}
return v1;
}

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

v0 = malloc(0x20uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
*((_BYTE *)v0 + 24) = 0;
}
else
{
LOG("error: memory allocation failed.\n");
}
return v1;
}

original_code:
queue_node_t* queue_node_new() {
    queue_node_t *node = ((void *)0);
    node = (queue_node_t *)malloc(sizeof(queue_node_t));
    if (node == ((void *)0)) {
        LOG("error: memory allocation failed.\n");
        return node;
    }
    node->prev = ((void *)0);
    node->next = ((void *)0);
    node->data = ((void *)0);
    node->signal = 0;
    return node;
}

canonical_original_code:
struct queue_node_ *queue_node_new() {
    struct queue_node_ *node = ((void *)0);
    node = (struct queue_node_ *)malloc(sizeof(queue_node_t));
    if (node == ((void *)0)) {
        LOG("error: memory allocation failed.\n");
        return node;
    }
    node->prev = ((void *)0);
    node->next = ((void *)0);
    node->data = ((void *)0);
    node->signal = 0;
    return node;
}

return_type: struct queue_node_ { struct queue_node_ *prev; struct queue_node_ *next; void *data; _Bool signal; } *


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

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

original_code:
void queue_node_free(queue_node_t* node){
    do { if (node != ((void *)0)) { free(node); node = ((void *)0); } } while(0);
}

canonical_original_code:
void queue_node_free(struct queue_node_ *node){
    do { if (node != ((void *)0)) { free(node); node = ((void *)0); } } while(0);
}


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

while ( !(unsigned __int8)queue_is_empty((__int64)ptr) )
{
v1 = queue_get_node((__int64)ptr);
queue_node_free(v1);
}
if ( ptr )
free(ptr);
}

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

while ( !(unsigned __int8)func1((__int64)ptr) )
{
v1 = queue_get_node((__int64)ptr);
func5(v1);
}
if ( ptr )
free(ptr);
}

original_code:
void queue_free(queue_t *queue) {
    queue_node_t *node;
    while(!queue_is_empty(queue)) {
        node = queue_get_node(queue);
        queue_node_free(node);
    }
    do { if (queue != ((void *)0)) { free(queue); queue = ((void *)0); } } while(0);
}

canonical_original_code:
void queue_free(struct queue_ *queue) {
    struct queue_node_ *node;
    while(!queue_is_empty(queue)) {
        node = queue_get_node(queue);
        queue_node_free(node);
    }
    do { if (queue != ((void *)0)) { free(queue); queue = ((void *)0); } } while(0);
}


decompiled_code:
__int64 __fastcall queue_next_node(__int64 a1, __int64 a2)
{
if ( !a1 )
return LOG("error: queue doesn't exist.\n");
if ( a2 )
return *(_QWORD *)(a2 + 8);
return LOG("error: given node doesn't exist.\n");
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
if ( !a1 )
return LOG("error: queue doesn't exist.\n");
if ( a2 )
return *(_QWORD *)(a2 + 8);
return LOG("error: given node doesn't exist.\n");
}

original_code:
queue_node_t* queue_next_node(queue_t* queue, queue_node_t* node) {
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    if (node == ((void *)0)) {
        LOG("error: given node doesn't exist.\n");
        return;
    }
    return node->next;
}

canonical_original_code:
struct queue_node_ *queue_next_node(struct queue_ *queue, struct queue_node_ *node) {
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    if (node == ((void *)0)) {
        LOG("error: given node doesn't exist.\n");
        return;
    }
    return node->next;
}

variable_types: struct queue_ { char name[256]; struct queue_node_ *head; struct queue_node_ *tail; 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; } mtx; } *


decompiled_code:
__int64 __fastcall queue_prev_node(__int64 a1, __int64 a2)
{
if ( !a1 )
return LOG("error: queue doesn't exist.\n");
if ( a2 )
return *(_QWORD *)a2;
return LOG("error: given node doesn't exist.\n");
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
if ( !a1 )
return LOG("error: queue doesn't exist.\n");
if ( a2 )
return *(_QWORD *)a2;
return LOG("error: given node doesn't exist.\n");
}

original_code:
queue_node_t* queue_prev_node(queue_t* queue, queue_node_t* node) {
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    if (node == ((void *)0)) {
        LOG("error: given node doesn't exist.\n");
        return;
    }
    return node->prev;
}

canonical_original_code:
struct queue_node_ *queue_prev_node(struct queue_ *queue, struct queue_node_ *node) {
    if (queue == ((void *)0)) {
        LOG("error: queue doesn't exist.\n");
        return;
    }
    if (node == ((void *)0)) {
        LOG("error: given node doesn't exist.\n");
        return;
    }
    return node->prev;
}


decompiled_code:
void __fastcall __noreturn monitor_thread(void *a1)
{
__int64 v1; // rbp
__int64 v2; // rax

if ( a1 )
{
while ( 1 )
{
do
{
pthread_mutex_lock((pthread_mutex_t *)(monitor_queue + 272LL));
v1 = queue_get_node(monitor_queue);
pthread_mutex_unlock((pthread_mutex_t *)(monitor_queue + 272LL));
}
while ( !v1 );
if ( *(_BYTE *)(v1 + 24) )
break;
v2 = *(_QWORD *)(v1 + 16);
if ( *(_DWORD *)(v2 + 256) != 1 )
_assert_fail("rmnode->type == RM_TYPE_TARGET", "threads.c", 0x9Fu, "monitor_thread");
if ( *(_DWORD *)(*(_QWORD *)(v2 + 264) + 280LL) )
{
pthread_mutex_lock((pthread_mutex_t *)(monitor_queue + 272LL));
queue_add_node(monitor_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(monitor_queue + 272LL));
}
else
{
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
}
}
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
a1 = 0LL;
}
pthread_exit(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
__int64 v1; // rbp
__int64 v2; // rax

if ( a1 )
{
while ( 1 )
{
do
{
pthread_mutex_lock((pthread_mutex_t *)(monitor_queue + 272LL));
v1 = queue_get_node(monitor_queue);
pthread_mutex_unlock((pthread_mutex_t *)(monitor_queue + 272LL));
}
while ( !v1 );
if ( *(_BYTE *)(v1 + 24) )
break;
v2 = *(_QWORD *)(v1 + 16);
if ( *(_DWORD *)(v2 + 256) != 1 )
_assert_fail("rmnode->type == RM_TYPE_TARGET", "threads.c", 0x9Fu, "monitor_thread");
if ( *(_DWORD *)(*(_QWORD *)(v2 + 264) + 280LL) )
{
pthread_mutex_lock((pthread_mutex_t *)(monitor_queue + 272LL));
queue_add_node(monitor_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(monitor_queue + 272LL));
}
else
{
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
}
}
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v1);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
a1 = 0LL;
}
pthread_exit(a1);
}

original_code:
error_t monitor_thread(void *data) {
    thread_data_t *tdata = ((void *)0);
    queue_node_t *qnode = ((void *)0);
    remodel_node_t *rmnode = ((void *)0);
    target_t *target = ((void *)0);
    int tid;
    build_state_t build_state;
    tdata = (thread_data_t *)data;
    if (tdata == ((void *)0)) {
        pthread_exit(((void *)0));
    }
    tid = tdata->tid;
    while(1) {
        rmnode = ((void *)0);
        target = ((void *)0);
        qnode = ((void *)0);
        build_state = RM_BUILD_RUNNING;
        pthread_mutex_lock(&monitor_queue->mtx);
        qnode = queue_get_node(monitor_queue);
        pthread_mutex_unlock(&monitor_queue->mtx);
        if (qnode == ((void *)0)) {
            continue;
        }
        if (qnode->signal) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
            ;
            pthread_exit(((void *)0));
        }
        ((void) sizeof ((qnode != ((void *)0)) ? 1 : 0), __extension__ ({ if (qnode != ((void *)0)) ; else __assert_fail ("qnode != NULL", "threads.c", 157, __extension__ __PRETTY_FUNCTION__); }));
        rmnode = (remodel_node_t *)qnode->data;
        ((void) sizeof ((rmnode->type == RM_TYPE_TARGET) ? 1 : 0), __extension__ ({ if (rmnode->type == RM_TYPE_TARGET) ; else __assert_fail ("rmnode->type == RM_TYPE_TARGET", "threads.c", 159, __extension__ __PRETTY_FUNCTION__); }));
        target = rmnode->target;
        build_state = target->build_state;
        if (build_state == RM_BUILD_DONE) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
        } else {
            pthread_mutex_lock(&monitor_queue->mtx);
            queue_add_node(monitor_queue, qnode);
            pthread_mutex_unlock(&monitor_queue->mtx);
            continue;
        }
    }
}

canonical_original_code:
enum error_t monitor_thread(void *data) {
    struct thread_data_ *tdata = ((void *)0);
    struct queue_node_ *qnode = ((void *)0);
    struct remodel_node_ *rmnode = ((void *)0);
    struct target_ *target = ((void *)0);
    int tid;
    enum build_state_t build_state;
    tdata = (struct thread_data_ *)data;
    if (tdata == ((void *)0)) {
        pthread_exit(((void *)0));
    }
    tid = tdata->tid;
    while(1) {
        rmnode = ((void *)0);
        target = ((void *)0);
        qnode = ((void *)0);
        build_state = RM_BUILD_RUNNING;
        pthread_mutex_lock(&monitor_queue->mtx);
        qnode = queue_get_node(monitor_queue);
        pthread_mutex_unlock(&monitor_queue->mtx);
        if (qnode == ((void *)0)) {
            continue;
        }
        if (qnode->signal) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
            ;
            pthread_exit(((void *)0));
        }
        ((void) sizeof ((qnode != ((void *)0)) ? 1 : 0), __extension__ ({ if (qnode != ((void *)0)) ; else __assert_fail ("qnode != NULL", "threads.c", 157, __extension__ __PRETTY_FUNCTION__); }));
        rmnode = (struct remodel_node_ *)qnode->data;
        ((void) sizeof ((rmnode->type == RM_TYPE_TARGET) ? 1 : 0), __extension__ ({ if (rmnode->type == RM_TYPE_TARGET) ; else __assert_fail ("rmnode->type == RM_TYPE_TARGET", "threads.c", 159, __extension__ __PRETTY_FUNCTION__); }));
        target = rmnode->target;
        build_state = target->build_state;
        if (build_state == RM_BUILD_DONE) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
        } else {
            pthread_mutex_lock(&monitor_queue->mtx);
            queue_add_node(monitor_queue, qnode);
            pthread_mutex_unlock(&monitor_queue->mtx);
            continue;
        }
    }
}

variable_types:
data
void *

variable_types: struct thread_data_ { unsigned long ptp; int tid; char message[10]; } *

variable_types: struct target_ { char name[256]; char *command; struct dependency_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct dependency_ *next; } *dp_head; struct target_ *next; enum build_state_t { RM_BUILD_DONE=0, RM_BUILD_READY=1, RM_BUILD_ONQ=2, RM_BUILD_RUNNING=3 } build_state; int total_dp; int changed_dp; } *

variable_types: tid int

return_type: enum error_t { RM_ERROR=-1, RM_SUCCESS=0, RM_NOFILEENT=1, RM_FAIL=2, RM_PARSE_FAIL=3, SUCCESS=expr }

user_defined_types: struct srcfile_ { char name[256]; char md5hash[-1]; _Bool md5_present; _Bool md5_changed; struct srcfile_ *next; }


decompiled_code:
void __fastcall __noreturn build_thread(void *a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
const char *v5; // r13

if ( a1 )
{
v1 = *((_DWORD *)a1 + 2);
while ( 1 )
{
do
{
pthread_mutex_lock((pthread_mutex_t *)(dispatch_queue + 272LL));
v2 = queue_get_node(dispatch_queue);
pthread_mutex_unlock((pthread_mutex_t *)(dispatch_queue + 272LL));
}
while ( !v2 );
if ( *(_BYTE *)(v2 + 24) )
break;
v3 = *(_QWORD *)(v2 + 16);
if ( *(_DWORD *)(v3 + 256) != 1 )
_assert_fail("rmnode->type == RM_TYPE_TARGET", "threads.c", 0x5Fu, "build_thread");
v4 = *(_QWORD *)(v3 + 264);
if ( *(_DWORD *)(v4 + 280) != 2 )
_assert_fail("target->build_state == RM_BUILD_ONQ", "threads.c", 0x61u, "build_thread");
v5 = *(const char **)(v4 + 256);
*(_DWORD *)(v4 + 280) = 3;
if ( *v5 )
{
LOG("thread %d running \"%s\"\n", v1, v5);
system(v5);
}
*(_DWORD *)(v4 + 280) = 0;
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v2);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
}
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v2);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
a1 = 0LL;
}
pthread_exit(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
const char *v5; // r13

if ( a1 )
{
v1 = *((_DWORD *)a1 + 2);
while ( 1 )
{
do
{
pthread_mutex_lock((pthread_mutex_t *)(dispatch_queue + 272LL));
v2 = queue_get_node(dispatch_queue);
pthread_mutex_unlock((pthread_mutex_t *)(dispatch_queue + 272LL));
}
while ( !v2 );
if ( *(_BYTE *)(v2 + 24) )
break;
v3 = *(_QWORD *)(v2 + 16);
if ( *(_DWORD *)(v3 + 256) != 1 )
_assert_fail("rmnode->type == RM_TYPE_TARGET", "threads.c", 0x5Fu, "build_thread");
v4 = *(_QWORD *)(v3 + 264);
if ( *(_DWORD *)(v4 + 280) != 2 )
_assert_fail("target->build_state == RM_BUILD_ONQ", "threads.c", 0x61u, "build_thread");
v5 = *(const char **)(v4 + 256);
*(_DWORD *)(v4 + 280) = 3;
if ( *v5 )
{
LOG("thread %d running \"%s\"\n", v1, v5);
system(v5);
}
*(_DWORD *)(v4 + 280) = 0;
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v2);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
}
pthread_mutex_lock((pthread_mutex_t *)(response_queue + 272LL));
queue_add_node(response_queue, v2);
pthread_mutex_unlock((pthread_mutex_t *)(response_queue + 272LL));
a1 = 0LL;
}
pthread_exit(a1);
}

original_code:
error_t build_thread(void *data) {
    thread_data_t *tdata = ((void *)0);
    queue_node_t *qnode = ((void *)0);
    target_t *target = ((void *)0);
    remodel_node_t *rmnode = ((void *)0);
    char *command = ((void *)0);
    int tid;
    tdata = (thread_data_t *)data;
    if (tdata == ((void *)0)) {
        pthread_exit(((void *)0));
    }
    tid = tdata->tid;
    while(1) {
        rmnode = ((void *)0);
        target = ((void *)0);
        qnode = ((void *)0);
        pthread_mutex_lock(&dispatch_queue->mtx);
        qnode = queue_get_node(dispatch_queue);
        pthread_mutex_unlock(&dispatch_queue->mtx);
        if (qnode == ((void *)0)) {
            continue;
        }
        if (qnode->signal) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
            ;
            pthread_exit(((void *)0));
        }
        rmnode = (remodel_node_t *)qnode->data;
        ((void) sizeof ((rmnode->type == RM_TYPE_TARGET) ? 1 : 0), __extension__ ({ if (rmnode->type == RM_TYPE_TARGET) ; else __assert_fail ("rmnode->type == RM_TYPE_TARGET", "threads.c", 95, __extension__ __PRETTY_FUNCTION__); }));
        target = rmnode->target;
        ((void) sizeof ((target->build_state == RM_BUILD_ONQ) ? 1 : 0), __extension__ ({ if (target->build_state == RM_BUILD_ONQ) ; else __assert_fail ("target->build_state == RM_BUILD_ONQ", "threads.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        target->build_state = RM_BUILD_RUNNING;
        command = target->command;
        if (command[0] != '\0') {
            LOG("thread %d running \"%s\"\n", tid, target->command);
            system(command);
        }
        target->build_state = RM_BUILD_DONE;
        pthread_mutex_lock(&response_queue->mtx);
        queue_add_node(response_queue, qnode);
        pthread_mutex_unlock(&response_queue->mtx);
    }
}

canonical_original_code:
enum error_t build_thread(void *data) {
    struct thread_data_ *tdata = ((void *)0);
    struct queue_node_ *qnode = ((void *)0);
    struct target_ *target = ((void *)0);
    struct remodel_node_ *rmnode = ((void *)0);
    char *command = ((void *)0);
    int tid;
    tdata = (struct thread_data_ *)data;
    if (tdata == ((void *)0)) {
        pthread_exit(((void *)0));
    }
    tid = tdata->tid;
    while(1) {
        rmnode = ((void *)0);
        target = ((void *)0);
        qnode = ((void *)0);
        pthread_mutex_lock(&dispatch_queue->mtx);
        qnode = queue_get_node(dispatch_queue);
        pthread_mutex_unlock(&dispatch_queue->mtx);
        if (qnode == ((void *)0)) {
            continue;
        }
        if (qnode->signal) {
            pthread_mutex_lock(&response_queue->mtx);
            queue_add_node(response_queue, qnode);
            pthread_mutex_unlock(&response_queue->mtx);
            ;
            pthread_exit(((void *)0));
        }
        rmnode = (struct remodel_node_ *)qnode->data;
        ((void) sizeof ((rmnode->type == RM_TYPE_TARGET) ? 1 : 0), __extension__ ({ if (rmnode->type == RM_TYPE_TARGET) ; else __assert_fail ("rmnode->type == RM_TYPE_TARGET", "threads.c", 95, __extension__ __PRETTY_FUNCTION__); }));
        target = rmnode->target;
        ((void) sizeof ((target->build_state == RM_BUILD_ONQ) ? 1 : 0), __extension__ ({ if (target->build_state == RM_BUILD_ONQ) ; else __assert_fail ("target->build_state == RM_BUILD_ONQ", "threads.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        target->build_state = RM_BUILD_RUNNING;
        command = target->command;
        if (command[0] != '\0') {
            LOG("thread %d running \"%s\"\n", tid, target->command);
            system(command);
        }
        target->build_state = RM_BUILD_DONE;
        pthread_mutex_lock(&response_queue->mtx);
        queue_add_node(response_queue, qnode);
        pthread_mutex_unlock(&response_queue->mtx);
    }
}

variable_types:
command
char *

user_defined_types: enum error_t { RM_ERROR=-1, RM_SUCCESS=0, RM_NOFILEENT=1, RM_FAIL=2, RM_PARSE_FAIL=3, SUCCESS=expr }

user_defined_types: struct thread_data_ { unsigned long ptp; int tid; char message[10]; }

user_defined_types: enum build_state_t { RM_BUILD_DONE=0, RM_BUILD_READY=1, RM_BUILD_ONQ=2, RM_BUILD_RUNNING=3 }

user_defined_types: enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 }


decompiled_code:
__int64 __fastcall spawn_threads(int a1)
{
pthread_t *v2; // rbx
int v3; // ebp
pthread_t *v4; // rcx
pthread_t *v5; // rdi
pthread_t *v6; // rdi

if ( a1 > 0 )
{
v2 = thread_list;
v3 = 0;
do
{
v4 = v2;
v5 = v2;
*((_DWORD *)v2 + 2) = ++v3;
v2 += 3;
pthread_create(v5, 0LL, (void *(*)(void *))build_thread, v4);
}
while ( v3 != a1 );
}
v6 = &thread_list[3 * a1];
*((_DWORD *)v6 + 2) = a1 + 1;
pthread_create(v6, 0LL, (void *(*)(void *))monitor_thread, v6);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
pthread_t *v2; // rbx
int v3; // ebp
pthread_t *v4; // rcx
pthread_t *v5; // rdi
pthread_t *v6; // rdi

if ( a1 > 0 )
{
v2 = thread_list;
v3 = 0;
do
{
v4 = v2;
v5 = v2;
*((_DWORD *)v2 + 2) = ++v3;
v2 += 3;
pthread_create(v5, 0LL, (void *(*)(void *))build_thread, v4);
}
while ( v3 != a1 );
}
v6 = &thread_list[3 * a1];
*((_DWORD *)v6 + 2) = a1 + 1;
pthread_create(v6, 0LL, (void *(*)(void *))monitor_thread, v6);
return 0LL;
}

original_code:
error_t spawn_threads(int number) {
    error_t ret = SUCCESS;
    int i = 0;
    for (i = 0; i < number; i++) {
        thread_list[i].tid = i+1;
        pthread_create(&thread_list[i].ptp, ((void *)0),
                (void *)&build_thread, (void *)&thread_list[i]);
    }
    thread_list[number].tid = number+1;
    pthread_create(&thread_list[number].ptp, ((void *)0),
                (void *)&monitor_thread, (void *)&thread_list[number]);
end:
    return ret;
}

canonical_original_code:
enum error_t spawn_threads(int number) {
    enum error_t ret = SUCCESS;
    int i = 0;
    for (i = 0; i < number; i++) {
        thread_list[i].tid = i+1;
        pthread_create(&thread_list[i].ptp, ((void *)0),
                (void *)&build_thread, (void *)&thread_list[i]);
    }
    thread_list[number].tid = number+1;
    pthread_create(&thread_list[number].ptp, ((void *)0),
                (void *)&monitor_thread, (void *)&thread_list[number]);
end:
    return ret;
}

variable_types: number int

variable_types: enum error_t { RM_ERROR=-1, RM_SUCCESS=0, RM_NOFILEENT=1, RM_FAIL=2, RM_PARSE_FAIL=3, SUCCESS=expr }

variable_types: i int


decompiled_code:
void *new_target_node()
{
void *v0; // r12

v0 = malloc(0x128uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 32) = 0LL;
*((_QWORD *)v0 + 33) = 0LL;
*((_QWORD *)v0 + 34) = 0LL;
*((_QWORD *)v0 + 35) = 1LL;
*((_DWORD *)v0 + 72) = 0;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

canonical_decompiled_code:
void *func0()
{
void *v0; // r12

v0 = malloc(0x128uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 32) = 0LL;
*((_QWORD *)v0 + 33) = 0LL;
*((_QWORD *)v0 + 34) = 0LL;
*((_QWORD *)v0 + 35) = 1LL;
*((_DWORD *)v0 + 72) = 0;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

original_code:
target_t *new_target_node() {
    target_t *new_node;
    new_node = (target_t *)malloc(sizeof(target_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    new_node->dp_head = ((void *)0);
    new_node->command = ((void *)0);
    new_node->next = ((void *)0);
    new_node->build_state = RM_BUILD_READY;
    new_node->total_dp = 0;
    new_node->changed_dp = 0;
    return new_node;
}

canonical_original_code:
struct target_ *new_target_node() {
    struct target_ *new_node;
    new_node = (struct target_ *)malloc(sizeof(target_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    new_node->dp_head = ((void *)0);
    new_node->command = ((void *)0);
    new_node->next = ((void *)0);
    new_node->build_state = RM_BUILD_READY;
    new_node->total_dp = 0;
    new_node->changed_dp = 0;
    return new_node;
}

return_type: struct target_ { char name[256]; char *command; struct dependency_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct dependency_ *next; } *dp_head; struct target_ *next; enum build_state_t { RM_BUILD_DONE=0, RM_BUILD_READY=1, RM_BUILD_ONQ=2, RM_BUILD_RUNNING=3 } build_state; int total_dp; int changed_dp; } *


decompiled_code:
void free_target_node()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void free_target_node() {
}

canonical_original_code:
void free_target_node() {
}


decompiled_code:
void *new_dp_node()
{
void *v0; // r12

v0 = malloc(0x110uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 33) = 0LL;
*((_DWORD *)v0 + 64) = 0;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

canonical_decompiled_code:
void *func2()
{
void *v0; // r12

v0 = malloc(0x110uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 33) = 0LL;
*((_DWORD *)v0 + 64) = 0;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

original_code:
dependency_t *new_dp_node() {
    dependency_t *new_node;
    new_node = (dependency_t *)malloc(sizeof(dependency_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    new_node->next = ((void *)0);
    new_node->type = RM_TYPE_UNKNOWN;
    return new_node;
}

canonical_original_code:
struct dependency_ *new_dp_node() {
    struct dependency_ *new_node;
    new_node = (struct dependency_ *)malloc(sizeof(dependency_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    new_node->next = ((void *)0);
    new_node->type = RM_TYPE_UNKNOWN;
    return new_node;
}

variable_types: struct dependency_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct dependency_ *next; } *


decompiled_code:
void *new_remodel_node()
{
void *result; // rax

result = malloc(0x130uLL);
if ( !result )
{
LOG("error: system out of memory.\nremodel failed.\n");
result = 0LL;
}
return result;
}

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

result = malloc(0x130uLL);
if ( !result )
{
LOG("error: system out of memory.\nremodel failed.\n");
result = 0LL;
}
return result;
}

original_code:
remodel_node_t *new_remodel_node() {
    remodel_node_t *new_node;
    new_node = (remodel_node_t *)malloc(sizeof(remodel_node_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    new_node->type = RM_TYPE_UNKNOWN;
    new_node->parent = ((void *)0);
    new_node->children = ((void *)0);
    new_node->child_nodes = 0;
}

canonical_original_code:
struct remodel_node_ *new_remodel_node() {
    struct remodel_node_ *new_node;
    new_node = (struct remodel_node_ *)malloc(sizeof(remodel_node_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    new_node->type = RM_TYPE_UNKNOWN;
    new_node->parent = ((void *)0);
    new_node->children = ((void *)0);
    new_node->child_nodes = 0;
}

return_type: struct remodel_node_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct target_ { char name[256]; char *command; struct dependency_ { char name[256]; enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 } type; struct dependency_ *next; } *dp_head; struct target_ *next; enum build_state_t { RM_BUILD_DONE=0, RM_BUILD_READY=1, RM_BUILD_ONQ=2, RM_BUILD_RUNNING=3 } build_state; int total_dp; int changed_dp; } *target; struct srcfile_ { char name[256]; char md5hash[-1]; _Bool md5_present; _Bool md5_changed; struct srcfile_ *next; } *srcfile; struct remodel_node_ *parent; struct remodel_node_ **children; int child_nodes; } *


decompiled_code:
char *new_src_node()
{
char *v0; // r12

v0 = (char *)malloc(0x130uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 37) = 0LL;
*(_WORD *)(v0 + 289) = 0;
*((_OWORD *)v0 + 16) = 0LL;
*((_OWORD *)v0 + 17) = 0LL;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

canonical_decompiled_code:
char *func4()
{
char *v0; // r12

v0 = (char *)malloc(0x130uLL);
if ( v0 )
{
memset(v0, 0, 0x100uLL);
*((_QWORD *)v0 + 37) = 0LL;
*(_WORD *)(v0 + 289) = 0;
*((_OWORD *)v0 + 16) = 0LL;
*((_OWORD *)v0 + 17) = 0LL;
}
else
{
LOG("error: system out of memory.\nremodel failed.\n");
}
return v0;
}

original_code:
srcfile_t *new_src_node() {
    srcfile_t *new_node;
    new_node = (srcfile_t *)malloc(sizeof(srcfile_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    memset(new_node->md5hash, '\0', 32);
    new_node->next = ((void *)0);
    new_node->md5_present = 0;
    new_node->md5_changed = 0;
    return new_node;
}

canonical_original_code:
struct srcfile_ *new_src_node() {
    struct srcfile_ *new_node;
    new_node = (struct srcfile_ *)malloc(sizeof(srcfile_t));
    if (new_node == ((void *)0)) {
        LOG("error: system out of memory.\nremodel failed.\n");
        return new_node;
    }
    memset(new_node->name, '\0', 256);
    memset(new_node->md5hash, '\0', 32);
    new_node->next = ((void *)0);
    new_node->md5_present = 0;
    new_node->md5_changed = 0;
    return new_node;
}

variable_types: struct srcfile_ { char name[256]; char md5hash[-1]; _Bool md5_present; _Bool md5_changed; struct srcfile_ *next; } *


decompiled_code:
__int64 __fastcall add_src_node(char *s2)
{
__int64 v1; // r13
__int64 v2; // r12
__int64 v3; // rbx
int v4; // eax
unsigned int v5; // er8

v1 = srcfile_head;
v2 = *(_QWORD *)(srcfile_head + 296LL);
if ( !v2 )
goto LABEL_6;
v3 = *(_QWORD *)(srcfile_head + 296LL);
do
{
v4 = strcmp((const char *)v3, s2);
v3 = *(_QWORD *)(v3 + 296);
}
while ( v3 && v4 );
v5 = 2;
if ( v4 )
{
LABEL_6:
*((_QWORD *)s2 + 37) = v2;
v5 = 0;
*(_QWORD *)(v1 + 296) = s2;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s2)
{
__int64 v1; // r13
__int64 v2; // r12
__int64 v3; // rbx
int v4; // eax
unsigned int v5; // er8

v1 = srcfile_head;
v2 = *(_QWORD *)(srcfile_head + 296LL);
if ( !v2 )
goto LABEL_6;
v3 = *(_QWORD *)(srcfile_head + 296LL);
do
{
v4 = strcmp((const char *)v3, s2);
v3 = *(_QWORD *)(v3 + 296);
}
while ( v3 && v4 );
v5 = 2;
if ( v4 )
{
LABEL_6:
*((_QWORD *)s2 + 37) = v2;
v5 = 0;
*(_QWORD *)(v1 + 296) = s2;
}
return v5;
}

original_code:
error_t add_src_node(srcfile_t *src_node) {
    error_t ret = RM_FAIL;
    srcfile_t *tempnode;
    int src_found = 0;
    tempnode = srcfile_head->next;
    while((tempnode != ((void *)0)) && (src_found == 0)) {
        if (strcmp(tempnode->name, src_node->name) == 0) {
            src_found = 1;
        }
        tempnode = tempnode->next;
    }
    if (src_found == 0) {
        src_node->next = srcfile_head->next;
        srcfile_head->next = src_node;
        ret = RM_SUCCESS;
        goto end;
    } else {
        ret = RM_FAIL;
        goto end;
    }
end:
    return ret;
}

canonical_original_code:
enum error_t add_src_node(struct srcfile_ *src_node) {
    enum error_t ret = RM_FAIL;
    struct srcfile_ *tempnode;
    int src_found = 0;
    tempnode = srcfile_head->next;
    while((tempnode != ((void *)0)) && (src_found == 0)) {
        if (strcmp(tempnode->name, src_node->name) == 0) {
            src_found = 1;
        }
        tempnode = tempnode->next;
    }
    if (src_found == 0) {
        src_node->next = srcfile_head->next;
        srcfile_head->next = src_node;
        ret = RM_SUCCESS;
        goto end;
    } else {
        ret = RM_FAIL;
        goto end;
    }
end:
    return ret;
}


decompiled_code:
__int64 __fastcall check_dp_type(const char *a1)
{
char *v1; // rax
char *v2; // rsi
__int64 result; // rax

v1 = strchr(a1, 46);
if ( !v1 )
return 1LL;
v2 = v1;
if ( *v1 != 46 || (v1[1] != 99 || (result = 2LL, v2[2])) && (v2[1] != 104 || (result = 3LL, v2[2])) )
result = (unsigned int)(strcmp(v2, ".cpp") == 0) + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
char *v1; // rax
char *v2; // rsi
__int64 result; // rax

v1 = strchr(a1, 46);
if ( !v1 )
return 1LL;
v2 = v1;
if ( *v1 != 46 || (v1[1] != 99 || (result = 2LL, v2[2])) && (v2[1] != 104 || (result = 3LL, v2[2])) )
result = (unsigned int)(strcmp(v2, ".cpp") == 0) + 1;
return result;
}

original_code:
rm_file_type_t check_dp_type(char *name) {
    rm_file_type_t result = RM_TYPE_TARGET;
    char *extn;
    extn = strstr(name, ".");
    if (extn == ((void *)0)) return result;
    if (strcmp(extn, ".c") == 0) return RM_TYPE_SRC;
    if (strcmp(extn, ".h") == 0) return RM_TYPE_HEADER;
    if (strcmp(extn, ".cpp") == 0) return RM_TYPE_SRC;
    return result;
}

canonical_original_code:
enum rm_file_type_t check_dp_type(char *name) {
    enum rm_file_type_t result = RM_TYPE_TARGET;
    char *extn;
    extn = strstr(name, ".");
    if (extn == ((void *)0)) return result;
    if (strcmp(extn, ".c") == 0) return RM_TYPE_SRC;
    if (strcmp(extn, ".h") == 0) return RM_TYPE_HEADER;
    if (strcmp(extn, ".cpp") == 0) return RM_TYPE_SRC;
    return result;
}

variable_types: enum rm_file_type_t { RM_TYPE_UNKNOWN=0, RM_TYPE_TARGET=1, RM_TYPE_SRC=2, RM_TYPE_HEADER=3 }

variable_types:
extn
char *


decompiled_code:
__int64 __fastcall md5_to_string(__int64 a1, __int64 a2)
{
__int64 i; // rbx
__int64 result; // rax
char s; // [rsp+Eh] [rbp-1Ah] BYREF
unsigned __int8 v5; // [rsp+Fh] [rbp-19h]

for ( i = 0LL; i != 16; ++i )
{
sprintf(&s, "%02x", *(unsigned __int8 *)(a1 + i));
*(_BYTE *)(a2 + 2 * i) = s;
result = v5;
*(_BYTE *)(a2 + 2 * i + 1) = v5;
}
*(_BYTE *)(a2 + 32) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 i; // rbx
__int64 result; // rax
char s; // [rsp+Eh] [rbp-1Ah] BYREF
unsigned __int8 v5; // [rsp+Fh] [rbp-19h]

for ( i = 0LL; i != 16; ++i )
{
sprintf(&s, "%02x", *(unsigned __int8 *)(a1 + i));
*(_BYTE *)(a2 + 2 * i) = s;
result = v5;
*(_BYTE *)(a2 + 2 * i + 1) = v5;
}
*(_BYTE *)(a2 + 32) = 0;
return result;
}

original_code:
void md5_to_string(unsigned char *md, char *result) {
    int i;
    char tmp[2];
    for(i=0; i < 16; i++) {
        sprintf(tmp, "%02x",md[i]);
        result[2*i] = tmp[0];
        result[2*i+1] = tmp[1];
    }
    result[32] = '\0';
}

variable_types:
md
unsigned char *

variable_types:
result
char *

variable_types:
tmp
char [2]


repo: vikingeff/minitalk

decompiled_code:
__int64 __fastcall ft_putchar(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
write(1, buf, 1uLL);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
write(1, buf, 1uLL);
return 1LL;
}

original_code:
int ft_putchar(char c)
{
 write(1, &c, 1);
 return (1);
}

variable_types: c char

return_type: int


decompiled_code:
__int64 __fastcall ft_strlen(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // er8

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

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

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

original_code:
int ft_strlen(char *str)
{
 int size;
 size = 0;
 while (str[size])
  ++size;
 return (size);
}

variable_types:
str
char *

variable_types: size int


decompiled_code:
__int64 __fastcall ft_putstr(void *buf)
{
int v1; // eax

v1 = ft_strlen(buf);
write(1, buf, v1);
return ft_strlen(buf);
}

canonical_decompiled_code:
__int64 __fastcall func9(void *buf)
{
int v1; // eax

v1 = func8(buf);
write(1, buf, v1);
return func8(buf);
}

original_code:
int ft_putstr(char *str)
{
 write(1, str, ft_strlen(str));
 return (ft_strlen(str));
}


error

decompiled_code:
__int64 __fastcall error(void *buf)
{
int v1; // eax

v1 = ft_strlen(buf);
write(2, buf, v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *buf)
{
int v1; // eax

v1 = func8(buf);
write(2, buf, v1);
return 0xFFFFFFFFLL;
}

original_code:
int error(char *str)
{
 write(2, str, ft_strlen(str));
 return (-1);
}


decompiled_code:
__int64 __fastcall ft_putnbr(int a1)
{
int v1; // ebx

v1 = a1;
if ( a1 < 0 )
{
v1 = -a1;
ft_putchar(45);
}
if ( v1 > 9 )
ft_putnbr((unsigned int)v1 / 0xAuLL);
return ft_putchar((unsigned __int8)(v1 % 0xAu) + 48);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
int v1; // ebx

v1 = a1;
if ( a1 < 0 )
{
v1 = -a1;
func7(45);
}
if ( v1 > 9 )
func11((unsigned int)v1 / 0xAuLL);
return func7((unsigned __int8)(v1 % 0xAu) + 48);
}

original_code:
void ft_putnbr(int nb)
{
 if (nb < 0)
 {
  ft_putchar('-');
  nb = nb * -1;
 }
 if (nb >= 10)
  ft_putnbr(nb / 10);
 ft_putchar(nb % 10 + '0');
}

variable_types: nb int


decompiled_code:
__int64 __fastcall clean_str(_BYTE *a1)
{
__int64 v1; // rax
char v2; // dl
unsigned int v3; // er8

if ( (unsigned __int8)(*a1 - 9) > 4u && *a1 != 32 )
return 0;
v1 = 1LL;
do
{
do
{
v2 = a1[v1];
v3 = v1++;
}
while ( (unsigned __int8)(v2 - 9) <= 4u );
}
while ( v2 == 32 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(_BYTE *a1)
{
__int64 v1; // rax
char v2; // dl
unsigned int v3; // er8

if ( (unsigned __int8)(*a1 - 9) > 4u && *a1 != 32 )
return 0;
v1 = 1LL;
do
{
do
{
v2 = a1[v1];
v3 = v1++;
}
while ( (unsigned __int8)(v2 - 9) <= 4u );
}
while ( v2 == 32 );
return v3;
}

original_code:
static int clean_str(const char *str)
{
 int index;
 index = 0;
 while (str[index] == ' ' || str[index] == '\t' || str[index] == '\n'
  || str[index] == '\r' || str[index] == '\f' || str[index] == '\v')
  index++;
 return (index);
}

canonical_original_code:
static int clean_str(char *str)
{
 int index;
 index = 0;
 while (str[index] == ' ' || str[index] == '\t' || str[index] == '\n'
  || str[index] == '\r' || str[index] == '\f' || str[index] == '\v')
  index++;
 return (index);
}

variable_types: index int


decompiled_code:
_BOOL8 __fastcall ft_isdigit(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

original_code:
int ft_isdigit(int c)
{
 if (c >= '0' && c <= '9')
  return (1);
 else
  return (0);
}

variable_types: c int


original_code:
int ft_atoi(const char *str)
{
 int index;
 int res;
 int sign;
 index = clean_str(str);
 res = 0;
 sign = 1;
 if (*(str + index) == '-' || *(str + index) == '+')
  sign = (*(str + index++) == '-') ? -1 : 1;
 while (*(str + index) && *(str + index) >= '0' && *(str + index) <= '9')
 {
  res *= 10;
  res += (*(str + index) - '0');
  index++;
 }
 return (res * sign);
}

canonical_original_code:
int ft_atoi(char *str)
{
 int index;
 int res;
 int sign;
 index = clean_str(str);
 res = 0;
 sign = 1;
 if (*(str + index) == '-' || *(str + index) == '+')
  sign = (*(str + index++) == '-') ? -1 : 1;
 while (*(str + index) && *(str + index) >= '0' && *(str + index) <= '9')
 {
  res *= 10;
  res += (*(str + index) - '0');
  index++;
 }
 return (res * sign);
}

variable_types: res int

variable_types: sign int


decompiled_code:
__int64 __fastcall send_info(char a1, __pid_t a2)
{
int v3; // ebp
int v4; // ebx

v3 = 7;
v4 = 64;
do
{
if ( a1 >= v4 )
{
a1 -= v4;
if ( kill(a2, 12) == -1 )
return error("Error on kill\n");
}
else if ( kill(a2, 10) == -1 )
{
return error("Error on kill\n");
}
v4 /= 2;
usleep(0xAu);
--v3;
}
while ( v3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(char a1, __pid_t a2)
{
int v3; // ebp
int v4; // ebx

v3 = 7;
v4 = 64;
do
{
if ( a1 >= v4 )
{
a1 -= v4;
if ( kill(a2, 12) == -1 )
return func10("Error on kill\n");
}
else if ( kill(a2, 10) == -1 )
{
return func10("Error on kill\n");
}
v4 /= 2;
usleep(0xAu);
--v3;
}
while ( v3 );
return 0LL;
}

original_code:
int send_info(char c, int pid)
{
 int nb;
 nb = 64;
 while (nb != 0)
 {
  if (c >= nb)
  {
   c = c - nb;
   if (kill(pid, 12) == -1)
    return (error("Error on kill\n"));
  }
  else
  {
   if (kill(pid, 10) == -1)
    return (error("Error on kill\n"));
  }
  nb = nb / 2;
  usleep(10);
 }
 return (0);
}

variable_types: pid int


decompiled_code:
__int64 __fastcall check_pid(_BYTE *a1)
{
char *v1; // rbx
int v2; // eax
__int64 v3; // rbp

v1 = a1;
v2 = ft_strlen(a1);
if ( v2 <= 0 )
return 0LL;
v3 = (__int64)&a1[v2 - 1 + 1];
while ( ft_isdigit(*v1) )
{
if ( ++v1 == (char *)v3 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(_BYTE *a1)
{
char *v1; // rbx
int v2; // eax
__int64 v3; // rbp

v1 = a1;
v2 = func8(a1);
if ( v2 <= 0 )
return 0LL;
v3 = (__int64)&a1[v2 - 1 + 1];
while ( func13(*v1) )
{
if ( ++v1 == (char *)v3 )
return 0LL;
}
return 1LL;
}

original_code:
int check_pid(char *str)
{
 int size;
 int loop;
 loop = 0;
 size = ft_strlen(str);
 while (loop < size)
 {
  if (ft_isdigit(str[loop]))
  loop++;
 else
  return (1);
 }
 return (0);
}

variable_types: loop int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
__int64 v4; // rbx
unsigned int v5; // er12
const char *v6; // rax
__int64 v7; // rdi

if ( argc != 3 )
return -1;
if ( (unsigned int)check_pid(argv[1]) )
{
error("Le PID n'est pas conforme.\n");
result = 1;
}
else
{
v4 = 0LL;
v5 = ft_atoi(argv[1]);
do
{
v6 = argv[2];
v7 = (unsigned int)v6[v4];
if ( !v6[v4] )
return -((unsigned int)send_info(10LL, v5) == -1);
++v4;
}
while ( (unsigned int)send_info(v7, v5) != -1 );
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
__int64 v4; // rbx
unsigned int v5; // er12
const char *v6; // rax
__int64 v7; // rdi

if ( argc != 3 )
return -1;
if ( (unsigned int)func16(argv[1]) )
{
func10("Le PID n'est pas conforme.\n");
result = 1;
}
else
{
v4 = 0LL;
v5 = func14(argv[1]);
do
{
v6 = argv[2];
v7 = (unsigned int)v6[v4];
if ( !v6[v4] )
return -((unsigned int)func15(10LL, v5) == -1);
++v4;
}
while ( (unsigned int)func15(v7, v5) != -1 );
result = -1;
}
return result;
}

original_code:
int main(int ac, char **av)
{
 int pid;
 int index;
 index = 0;
 if (ac != 3)
  return (-1);
 if (check_pid(av[1]))
 {
  error("Le PID n'est pas conforme.\n");
  return (1);
 }
 else
 {
  pid = ft_atoi(av[1]);
  while (av[2][index])
  {
   if (send_info(av[2][index++], pid) == -1)
    return (-1);
  }
  if (send_info('\n', pid) == -1)
   return (-1);
 }
 return (0);
}

variable_types: ac int

variable_types:
av
char **


canonical_original_code:
int ft_putchar(char c)
{
 write(1, &c, 1);
 return (1);
}


canonical_original_code:
int ft_strlen(char *str)
{
 int size;
 size = 0;
 while (str[size])
  ++size;
 return (size);
}


canonical_original_code:
int ft_putstr(char *str)
{
 write(1, str, ft_strlen(str));
 return (ft_strlen(str));
}


error

canonical_original_code:
int error(char *str)
{
 write(2, str, ft_strlen(str));
 return (-1);
}


decompiled_code:
__int64 __fastcall ft_putnbr(int a1, __int64 a2)
{
int v2; // ebx

v2 = a1;
if ( a1 < 0 )
{
v2 = -a1;
ft_putchar(45);
}
if ( v2 > 9 )
ft_putnbr((unsigned int)v2 / 0xAuLL, a2);
return ft_putchar((unsigned __int8)(v2 % 0xAu) + 48);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, __int64 a2)
{
int v2; // ebx

v2 = a1;
if ( a1 < 0 )
{
v2 = -a1;
func7(45);
}
if ( v2 > 9 )
func11((unsigned int)v2 / 0xAuLL, a2);
return func7((unsigned __int8)(v2 % 0xAu) + 48);
}

canonical_original_code:
void ft_putnbr(int nb)
{
 if (nb < 0)
 {
  ft_putchar('-');
  nb = nb * -1;
 }
 if (nb >= 10)
  ft_putnbr(nb / 10);
 ft_putchar(nb % 10 + '0');
}


decompiled_code:
char __fastcall receive_signal(int a1)
{
char v1; // dl
char result; // al

v1 = value_0;
if ( a1 == 12 )
bit_1 += value_0;
result = value_0 / 2;
value_0 /= 2;
if ( !(v1 / 2) )
{
result = ft_putchar(bit_1);
value_0 = 64;
bit_1 = 0;
}
return result;
}

canonical_decompiled_code:
char __fastcall func12(int a1)
{
char v1; // dl
char result; // al

v1 = value_0;
if ( a1 == 12 )
bit_1 += value_0;
result = value_0 / 2;
value_0 /= 2;
if ( !(v1 / 2) )
{
result = func7(bit_1);
value_0 = 64;
bit_1 = 0;
}
return result;
}

original_code:
void receive_signal(int signal, siginfo_t *siginfo, void *unused)
{
 static char value = 64;
 static char bit = 0;
 (void)unused;
 (void)siginfo;
 if (signal == 12)
  bit = bit + value;
 value = value / 2;
 if (value == 0)
 {
  ft_putchar(bit);
  value = 64;
  bit = 0;
 }
}

canonical_original_code:
void receive_signal(int signal, struct siginfo_t *siginfo, void *unused)
{
 static char value = 64;
 static char bit = 0;
 (void)unused;
 (void)siginfo;
 if (signal == 12)
  bit = bit + value;
 value = value / 2;
 if (value == 0)
 {
  ft_putchar(bit);
  value = 64;
  bit = 0;
 }
}

variable_types: signal int

variable_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *

variable_types:
unused
void *

variable_types: value char

variable_types: bit char

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
__int64 __fastcall show_usage(void *buf)
{
error("Usage: ");
error(buf);
return error("\n");
}

canonical_decompiled_code:
__int64 __fastcall func13(void *buf)
{
func10("Usage: ");
func10(buf);
return func10("\n");
}

original_code:
void show_usage(char *prog_name)
{
 error("Usage: ");
 error(prog_name);
 error("\n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int result; // eax
struct sigaction v5; // [rsp+0h] [rbp-A8h] BYREF

if ( argc == 1 )
{
v3 = getpid();
if ( v3 == -1 )
{
result = error("Error on getpid\n");
}
else
{
ft_putstr("Le PID du serveur est : ");
ft_putnbr(v3, argv);
ft_putchar(10LL);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)receive_signal;
if ( sigaction(10, &v5, 0LL) != -1 && sigaction(12, &v5, 0LL) != -1 )
{
while ( 1 )
pause();
}
result = error("Error on sigaction\n");
}
}
else
{
show_usage((void *)*argv);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int result; // eax
struct sigaction v5; // [rsp+0h] [rbp-A8h] BYREF

if ( argc == 1 )
{
v3 = getpid();
if ( v3 == -1 )
{
result = func10("Error on getpid\n");
}
else
{
func9("Le PID du serveur est : ");
func11(v3, argv);
func7(10LL);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)receive_signal;
if ( sigaction(10, &v5, 0LL) != -1 && sigaction(12, &v5, 0LL) != -1 )
{
while ( 1 )
pause();
}
result = func10("Error on sigaction\n");
}
}
else
{
func13((void *)*argv);
result = 1;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 int pid;
 struct sigaction catch;
 if (argc == 1)
 {
  if ((pid = getpid()) == -1)
   return (error("Error on getpid\n"));
  ft_putstr("Le PID du serveur est : ");
  ft_putnbr(pid);
  ft_putchar('\n');
  catch.sa_flags = 4;
  catch.__sigaction_handler.sa_sigaction = receive_signal;
  if ((sigaction(10, &catch, 0)) == -1)
   return (error("Error on sigaction\n"));
  if ((sigaction(12, &catch, 0)) == -1)
   return (error("Error on sigaction\n"));
  while (42)
   pause();
 }
 else
 {
  show_usage(argv[0]);
  return (1);
 }
 return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }


repo: villavanschaeck/schaeckeling

decompiled_code:
__int64 __fastcall midi_to_input_index(unsigned int a1)
{
if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 0xBu, "midi_to_input_index");
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 0xBu, "midi_to_input_index");
return a1;
}

original_code:
static inline inputidx_t
midi_to_input_index(midichannel_t channel) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 11, __extension__ __PRETTY_FUNCTION__));
 return channel;
}

canonical_original_code:
static inline int midi_to_input_index(int channel) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 11, __extension__ __PRETTY_FUNCTION__));
 return channel;
}

variable_types: channel int


decompiled_code:
__int64 __fastcall dmx_to_input_index(int a1)
{
if ( (unsigned int)(a1 - 1) > 0x1FF )
_assert_fail("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 0x11u, "dmx_to_input_index");
return (unsigned int)(a1 + 127);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
if ( (unsigned int)(a1 - 1) > 0x1FF )
_assert_fail("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 0x11u, "dmx_to_input_index");
return (unsigned int)(a1 + 127);
}

original_code:
static inline inputidx_t
dmx_to_input_index(dmxchannel_t channel) {
 ((channel > 0 && channel <= 512) ? (void) (0) : __assert_fail ("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 17, __extension__ __PRETTY_FUNCTION__));
 return channel + 128 - 1;
}

canonical_original_code:
static inline int dmx_to_input_index(int channel) {
 ((channel > 0 && channel <= 512) ? (void) (0) : __assert_fail ("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 17, __extension__ __PRETTY_FUNCTION__));
 return channel + 128 - 1;
}


decompiled_code:
__int64 __fastcall dmx_changed(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3)
{
unsigned int v3; // er12
unsigned int v4; // eax

if ( a1 > 0x1FF )
_assert_fail("channel >= 0 && channel < DMX_CHANNELS", "input.c", 0x2Eu, "dmx_changed");
v3 = a3;
fprintf(stdout, "dmx_changed(%d, %d, %d)\n", a1 + 1, a2, a3);
receiving_changes = 1;
v4 = dmx_to_input_index(a1 + 1);
return update_input(v4, v3);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3)
{
unsigned int v3; // er12
unsigned int v4; // eax

if ( a1 > 0x1FF )
_assert_fail("channel >= 0 && channel < DMX_CHANNELS", "input.c", 0x2Eu, "dmx_changed");
v3 = a3;
fprintf(stdout, "dmx_changed(%d, %d, %d)\n", a1 + 1, a2, a3);
receiving_changes = 1;
v4 = func1(a1 + 1);
return update_input(v4, v3);
}

original_code:
void
dmx_changed(dmxchannel_t channel, unsigned char old, unsigned char new) {
 ((channel >= 0 && channel < 512) ? (void) (0) : __assert_fail ("channel >= 0 && channel < DMX_CHANNELS", "input.c", 46, __extension__ __PRETTY_FUNCTION__));
 ++channel;
 fprintf(stdout, "dmx_changed(%d, %d, %d)\n", channel, (int)old, (int)new);
 receiving_changes = 1;
 update_input(dmx_to_input_index(channel), new);
}

canonical_original_code:
void dmx_changed(int channel, unsigned char old, unsigned char new) {
 ((channel >= 0 && channel < 512) ? (void) (0) : __assert_fail ("channel >= 0 && channel < DMX_CHANNELS", "input.c", 46, __extension__ __PRETTY_FUNCTION__));
 ++channel;
 fprintf(stdout, "dmx_changed(%d, %d, %d)\n", channel, (int)old, (int)new);
 receiving_changes = 1;
 update_input(dmx_to_input_index(channel), new);
}

variable_types:
old
unsigned char

variable_types:
new
unsigned char


decompiled_code:
__int64 dmx_input_completed()
{
receiving_changes = 0;
flush_dmxout_sendbuf();
return update_websockets(0LL, 1LL);
}

canonical_decompiled_code:
__int64 func3()
{
receiving_changes = 0;
flush_dmxout_sendbuf();
return update_websockets(0LL, 1LL);
}

original_code:
void
dmx_input_completed(void) {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
 update_websockets(0, 1);
}

canonical_original_code:
void dmx_input_completed() {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
 update_websockets(0, 1);
}


decompiled_code:
__int64 __fastcall mk2c_error(unsigned int a1)
{
fprintf(stderr, "mk2c_error: %d\n", a1);
mk2c_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
fprintf(stderr, "mk2c_error: %d\n", a1);
mk2c_lost = 1;
return error_step();
}

original_code:
void
mk2c_error(int error) {
 fprintf(stderr, "mk2c_error: %d\n", error);
 mk2c_lost = 1;
 error_step();
}

variable_types: error int


decompiled_code:
__int64 __fastcall midi_changed(unsigned int a1, char a2)
{
unsigned int v2; // eax

if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 0x1Bu, "midi_changed");
if ( a2 < 0 )
_assert_fail("value <= 127", "input.c", 0x1Cu, "midi_changed");
fprintf(stdout, "midi_changed(%d, %d)\n", a1, (unsigned __int8)(2 * a2));
receiving_changes = 1;
v2 = midi_to_input_index(a1);
return update_input(v2, (unsigned __int8)(2 * a2));
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, char a2)
{
unsigned int v2; // eax

if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 0x1Bu, "midi_changed");
if ( a2 < 0 )
_assert_fail("value <= 127", "input.c", 0x1Cu, "midi_changed");
fprintf(stdout, "midi_changed(%d, %d)\n", a1, (unsigned __int8)(2 * a2));
receiving_changes = 1;
v2 = func0(a1);
return update_input(v2, (unsigned __int8)(2 * a2));
}

original_code:
void
midi_changed(midichannel_t channel, unsigned char value) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 27, __extension__ __PRETTY_FUNCTION__));
 ((value <= 127) ? (void) (0) : __assert_fail ("value <= 127", "input.c", 28, __extension__ __PRETTY_FUNCTION__));
 value *= 2;
 fprintf(stdout, "midi_changed(%d, %d)\n", channel, (int)value);
 receiving_changes = 1;
 update_input(midi_to_input_index(channel), value);
}

canonical_original_code:
void midi_changed(int channel, unsigned char value) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 27, __extension__ __PRETTY_FUNCTION__));
 ((value <= 127) ? (void) (0) : __assert_fail ("value <= 127", "input.c", 28, __extension__ __PRETTY_FUNCTION__));
 value *= 2;
 fprintf(stdout, "midi_changed(%d, %d)\n", channel, (int)value);
 receiving_changes = 1;
 update_input(midi_to_input_index(channel), value);
}

variable_types:
value
unsigned char


decompiled_code:
__int64 midi_input_completed()
{
receiving_changes = 0;
return flush_dmxout_sendbuf();
}

canonical_decompiled_code:
__int64 func6()
{
receiving_changes = 0;
return flush_dmxout_sendbuf();
}

original_code:
void
midi_input_completed(void) {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
}

canonical_original_code:
void midi_input_completed() {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
}


decompiled_code:
__int64 __fastcall nanokontrol_error(unsigned int a1)
{
fprintf(stderr, "nanokontrol_error: %d\n", a1);
nanokontrol_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
fprintf(stderr, "nanokontrol_error: %d\n", a1);
nanokontrol_lost = 1;
return error_step();
}

original_code:
void
nanokontrol_error(int error) {
 fprintf(stderr, "nanokontrol_error: %d\n", error);
 nanokontrol_lost = 1;
 error_step();
}


decompiled_code:
__int64 __fastcall generic_midi_error(unsigned int a1)
{
fprintf(stderr, "generic_midi_error: %d\n", a1);
midi_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
fprintf(stderr, "generic_midi_error: %d\n", a1);
midi_lost = 1;
return error_step();
}

original_code:
void
generic_midi_error(int error) {
 fprintf(stderr, "generic_midi_error: %d\n", error);
 midi_lost = 1;
 error_step();
}


decompiled_code:
__int64 reconnect_if_needed()
{
if ( !mk2c_lost )
return (unsigned int)nanokontrol_lost;
if ( mk2c )
teardown_dmx_usb_mk2_pro();
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( mk2c )
{
mk2c_lost = 0;
flush_dmxout_sendbuf();
}
return (unsigned int)nanokontrol_lost;
}

canonical_decompiled_code:
__int64 func9()
{
if ( !mk2c_lost )
return (unsigned int)nanokontrol_lost;
if ( mk2c )
teardown_dmx_usb_mk2_pro();
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( mk2c )
{
mk2c_lost = 0;
flush_dmxout_sendbuf();
}
return (unsigned int)nanokontrol_lost;
}

original_code:
void
reconnect_if_needed(void) {
 if (mk2c_lost) {
  if (mk2c != ((void *)0)) {
   teardown_dmx_usb_mk2_pro(mk2c);
  }
  mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
  if (mk2c != ((void *)0)) {
   mk2c_lost = 0;
   flush_dmxout_sendbuf();
  }
 }
 if (midi_lost) {
 }
 if (nanokontrol_lost) {
 }
}

canonical_original_code:
void reconnect_if_needed() {
 if (mk2c_lost) {
  if (mk2c != ((void *)0)) {
   teardown_dmx_usb_mk2_pro(mk2c);
  }
  mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
  if (mk2c != ((void *)0)) {
   mk2c_lost = 0;
   flush_dmxout_sendbuf();
  }
 }
 if (midi_lost) {
 }
 if (nanokontrol_lost) {
 }
}


decompiled_code:
__int64 init_communications()
{
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( !mk2c )
init_communications_cold();
nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
if ( !nanokontrol2 )
{
fwrite("init_communications: init_nanokontrol2 failed.", 1uLL, 0x2EuLL, stderr);
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
{
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
fwrite("No input devices available. Defaulting to preprogrammed output.\n", 1uLL, 0x40uLL, stderr);
}
return 0LL;
}
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( usbmidi )
return 0LL;
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( !mk2c )
func15();
nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
if ( !nanokontrol2 )
{
fwrite("init_communications: init_nanokontrol2 failed.", 1uLL, 0x2EuLL, stderr);
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
{
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
fwrite("No input devices available. Defaulting to preprogrammed output.\n", 1uLL, 0x40uLL, stderr);
}
return 0LL;
}
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( usbmidi )
return 0LL;
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
return 0LL;
}

original_code:
int
init_communications(void) {
 int has_any_input = 0;
 mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
 if (mk2c == ((void *)0)) {
  abort();
 }
 nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
 if (nanokontrol2 != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_nanokontrol2 failed.");
 }
 usbmidi = init_usbmidi("/dev/snd/midiC1D0");
 if (usbmidi != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_usbmidi failed.");
 }
 if (!has_any_input) {
  fprintf(stderr, "No input devices available. Defaulting to preprogrammed output.\n");
 }
 return 0;
}

canonical_original_code:
int init_communications() {
 int has_any_input = 0;
 mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
 if (mk2c == ((void *)0)) {
  abort();
 }
 nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
 if (nanokontrol2 != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_nanokontrol2 failed.");
 }
 usbmidi = init_usbmidi("/dev/snd/midiC1D0");
 if (usbmidi != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_usbmidi failed.");
 }
 if (!has_any_input) {
  fprintf(stderr, "No input devices available. Defaulting to preprogrammed output.\n");
 }
 return 0;
}


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

if ( mk2c )
result = mk2_send_dmx(mk2c, a1);
else
result = 4294967294LL;
return result;
}

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

if ( mk2c )
result = mk2_send_dmx(mk2c, a1);
else
result = 4294967294LL;
return result;
}

original_code:
int
send_dmx(unsigned char *dmxbytes) {
 int ret = -2;
 if (mk2c != ((void *)0)) {
  ret = mk2_send_dmx(mk2c, dmxbytes);
 }
 return ret;
}

variable_types:
dmxbytes
unsigned char *

variable_types: ret int


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

if ( nanokontrol2 )
{
result = nanokontrol2_set_led(nanokontrol2, 41LL, (unsigned int)(127 * a1));
if ( !a1 )
result = nanokontrol2_set_led(nanokontrol2, 46LL, 0LL);
}
return result;
}

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

if ( nanokontrol2 )
{
result = nanokontrol2_set_led(nanokontrol2, 41LL, (unsigned int)(127 * a1));
if ( !a1 )
result = nanokontrol2_set_led(nanokontrol2, 46LL, 0LL);
}
return result;
}

original_code:
void
set_feedback_running(int running) {
 if(nanokontrol2 != ((void *)0)) {
  nanokontrol2_set_led(nanokontrol2, 0x29, running * 127);
  if(!running) {
   nanokontrol2_set_led(nanokontrol2, 0x2E, 0);
  }
 }
}

variable_types: running int


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

if ( nanokontrol2 )
result = nanokontrol2_set_led(nanokontrol2, 45LL, (unsigned int)(127 * a1));
return result;
}

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

if ( nanokontrol2 )
result = nanokontrol2_set_led(nanokontrol2, 45LL, (unsigned int)(127 * a1));
return result;
}

original_code:
void
set_feedback_blackout(int blackout) {
 if(nanokontrol2 != ((void *)0)) {
  nanokontrol2_set_led(nanokontrol2, 0x2D, blackout * 127);
 }
}

variable_types: blackout int


decompiled_code:
__int64 set_feedback_step()
{
__int64 result; // rax

if ( nanokontrol2 )
{
prev_0 = 127 - prev_0;
result = nanokontrol2_set_led(nanokontrol2, 46LL, (unsigned int)prev_0);
}
return result;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 result; // rax

if ( nanokontrol2 )
{
prev_0 = 127 - prev_0;
result = nanokontrol2_set_led(nanokontrol2, 46LL, (unsigned int)prev_0);
}
return result;
}

original_code:
void
set_feedback_step() {
 if(nanokontrol2 != ((void *)0)) {
  static int prev = 0;
  prev = 127 - prev;
  nanokontrol2_set_led(nanokontrol2, 0x2E, prev);
 }
}

variable_types: prev int


decompiled_code:
void __fastcall handle_sigwinch(int a1)
{
if ( a1 != 28 )
__assert_fail("sig == SIGWINCH", "dmxdog.c", 0x1Bu, "handle_sigwinch");
got_sigwinch = 1;
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
if ( a1 != 28 )
__assert_fail("sig == SIGWINCH", "dmxdog.c", 0x1Bu, "handle_sigwinch");
got_sigwinch = 1;
}

original_code:
void
handle_sigwinch(int sig) {
 ((sig == 28) ? (void) (0) : __assert_fail ("sig == SIGWINCH", "dmxdog.c", 27, __extension__ __PRETTY_FUNCTION__));
 got_sigwinch = 1;
}

variable_types: sig int


decompiled_code:
void __fastcall __noreturn handle_sigterm(int a1)
{
if ( a1 == 15 )
{
if ( pid > 0 )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
exit(0);
}
__assert_fail("sig == SIGTERM", "dmxdog.c", 0x21u, "handle_sigterm");
}

canonical_decompiled_code:
void __fastcall __noreturn func8(int a1)
{
if ( a1 == 15 )
{
if ( pid > 0 )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
exit(0);
}
__assert_fail("sig == SIGTERM", "dmxdog.c", 0x21u, "handle_sigterm");
}

original_code:
void
handle_sigterm(int sig) {
 ((sig == 15) ? (void) (0) : __assert_fail ("sig == SIGTERM", "dmxdog.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(pid > 0) {
  kill(pid, sig);
  sleep(3);
  kill(pid, 9);
 }
 exit(0);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
char *const *v5; // rbp
int v6; // ebx
__pid_t v7; // edi
__pid_t v8; // eax
__pid_t v9; // eax
const char *v10; // rdi
int stat_loc; // [rsp+Ch] [rbp-2Ch] BYREF
time_t v12; // [rsp+10h] [rbp-28h] BYREF
time_t timer; // [rsp+18h] [rbp-20h] BYREF

v3 = 0;
v12 = 0LL;
signal(28, handle_sigwinch);
signal(15, handle_sigterm);
while ( 1 )
{
v4 = __posix_getopt();
if ( v4 == -1 )
break;
if ( v4 == 68 )
{
v3 = 1;
}
else
{
if ( v4 != 99 )
{
fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", *argv);
exit(64);
}
if ( chdir(optarg) == -1 )
warn("chdir", argv);
}
}
v5 = (char *const *)&argv[optind];
if ( v3 && daemon(1, 0) == -1 )
warn("daemon");
LABEL_10:
v6 = 0;
while ( 1 )
{
v7 = pid;
if ( pid <= 0 )
{
v8 = fork();
pid = v8;
v7 = v8;
if ( !v8 )
{
execve("./dmxmain", v5, environ);
err(1, "execve");
}
if ( v8 == -1 )
{
pid = 0;
v10 = "fork";
goto LABEL_27;
}
}
v9 = waitpid(v7, &stat_loc, 1);
if ( v9 == -1 )
{
v10 = "waitpid";
LABEL_27:
++v6;
warn(v10);
time(&v12);
goto LABEL_19;
}
if ( pid == v9 )
{
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
fprintf(stderr, "dmxd was killed by signal %d\n", stat_loc & 0x7F);
}
else
{
fprintf(stderr, "dmxd exited with exit code %d\n", BYTE1(stat_loc));
}
++v6;
pid = 0;
time(&v12);
LABEL_19:
time(&timer);
if ( timer - v12 > 300 )
goto LABEL_10;
if ( v6 > 4 )
system("reboot");
}
else
{
got_sigwinch = 0;
sleep(0x1Eu);
if ( !got_sigwinch )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
if ( v6 )
goto LABEL_19;
}
}
}

original_code:
int
main(int argc, char **argv) {
 pid_t wpid;
 int opt, status;
 int failures = 0;
 time_t last_fail = 0;
 int daemonize = 0;
 signal(28, handle_sigwinch);
 signal(15, handle_sigterm);
 while((opt = getopt(argc, argv, "Dc:")) != -1) {
  switch(opt) {
   case 'D':
    daemonize = 1;
    break;
   case 'c':
    if(chdir(optarg) == -1) {
     warn("chdir");
    }
    break;
   default:
    fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", argv[0]);
    exit(64);
  }
 }
 argc -= optind;
 argv += optind;
 if(daemonize) {
  if(daemon(1, 0) == -1) {
   warn("daemon");
  }
 }
 while(1) {
  if(failures > 0) {
   time_t now;
   time(&now);
   if(now - last_fail > 300) {
    failures = 0;
   }
   if(failures >= 5) {
    system("reboot");
   }
  }
  if(pid <= 0) {
   pid = fork();
   if(pid == 0) {
    execve("./dmxmain", argv, environ);
    err(1, "execve");
   } else if(pid == -1) {
    pid = 0;
    warn("fork"); failures++; time(&last_fail); continue;
   }
  }
  wpid = waitpid(pid, &status, 1);
  if(wpid == -1) {
   warn("waitpid"); failures++; time(&last_fail); continue;
  } else if(wpid == pid) {
   if((((status) & 0x7f) == 0)) {
    fprintf(stderr, "dmxd exited with exit code %d\n", (((status) & 0xff00) >> 8));
   } else if((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
    fprintf(stderr, "dmxd was killed by signal %d\n", ((status) & 0x7f));
   }
   pid = 0;
   failures++; time(&last_fail); continue;
  }
  got_sigwinch = 0;
  sleep(30);
  if(!got_sigwinch) {
   kill(pid, 15);
   sleep(3);
   kill(pid, 9);
  }
 }
}

canonical_original_code:
int main(int argc, char **argv) {
 int wpid;
 int opt; int status;
 int failures = 0;
 long last_fail = 0;
 int daemonize = 0;
 signal(28, handle_sigwinch);
 signal(15, handle_sigterm);
 while((opt = getopt(argc, argv, "Dc:")) != -1) {
  switch(opt) {
   case 'D':
    daemonize = 1;
    break;
   case 'c':
    if(chdir(optarg) == -1) {
     warn("chdir");
    }
    break;
   default:
    fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", argv[0]);
    exit(64);
  }
 }
 argc -= optind;
 argv += optind;
 if(daemonize) {
  if(daemon(1, 0) == -1) {
   warn("daemon");
  }
 }
 while(1) {
  if(failures > 0) {
   long now;
   time(&now);
   if(now - last_fail > 300) {
    failures = 0;
   }
   if(failures >= 5) {
    system("reboot");
   }
  }
  if(pid <= 0) {
   pid = fork();
   if(pid == 0) {
    execve("./dmxmain", argv, environ);
    err(1, "execve");
   } else if(pid == -1) {
    pid = 0;
    warn("fork"); failures++; time(&last_fail); continue;
   }
  }
  wpid = waitpid(pid, &status, 1);
  if(wpid == -1) {
   warn("waitpid"); failures++; time(&last_fail); continue;
  } else if(wpid == pid) {
   if((((status) & 0x7f) == 0)) {
    fprintf(stderr, "dmxd exited with exit code %d\n", (((status) & 0xff00) >> 8));
   } else if((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
    fprintf(stderr, "dmxd was killed by signal %d\n", ((status) & 0x7f));
   }
   pid = 0;
   failures++; time(&last_fail); continue;
  }
  got_sigwinch = 0;
  sleep(30);
  if(!got_sigwinch) {
   kill(pid, 15);
   sleep(3);
   kill(pid, 9);
  }
 }
}

variable_types: wpid int

variable_types: opt int

variable_types: status int

variable_types: failures int

variable_types: daemonize int

variable_types: now long


decompiled_code:
__int64 __fastcall usbmidi_read_data(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 v4; // rsi
__int64 v5; // rax
unsigned int v6; // er12
unsigned int v8; // [rsp+Ch] [rbp-1Ch]

if ( (int)a3 <= 0 )
return 0;
v4 = *a2;
if ( (_BYTE)v4 == 0xB0 )
{
v6 = 3;
goto LABEL_13;
}
if ( (_BYTE)v4 != 0xF0 )
{
v8 = a3;
printf("Wat de fuck is %02x?\n", v4);
midi_print_buf("buffer: ", a2, v8);
abort();
}
if ( (_DWORD)a3 == 1 )
return 0;
if ( (a2[1] & 0x80u) != 0 )
{
v6 = 2;
LABEL_14:
midi_print_buf("< ", a2, v6);
if ( *a2 == 0xB0 )
{
if ( a2[2] != 0xB0 )
midi_changed(a2[1]);
}
else if ( *a2 == 0xF0 && a2[v6 - 1] != 0xF7 )
{
_assert_fail("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 0x38u, "usbmidi_read_data");
}
return v6;
}
if ( (_DWORD)a3 == 2 )
return 0;
v5 = 3LL;
while ( 1 )
{
v6 = v5;
if ( (a2[v5 - 1] & 0x80u) != 0 )
break;
if ( ++v5 == (_DWORD)a3 + 1 )
return 0;
}
LABEL_13:
if ( (int)v6 <= (int)a3 )
goto LABEL_14;
printf("Not enough data for %02x: %d of %d\n", v4, a3, v6);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 v4; // rsi
__int64 v5; // rax
unsigned int v6; // er12
unsigned int v8; // [rsp+Ch] [rbp-1Ch]

if ( (int)a3 <= 0 )
return 0;
v4 = *a2;
if ( (_BYTE)v4 == 0xB0 )
{
v6 = 3;
goto LABEL_13;
}
if ( (_BYTE)v4 != 0xF0 )
{
v8 = a3;
printf("Wat de fuck is %02x?\n", v4);
midi_print_buf("buffer: ", a2, v8);
abort();
}
if ( (_DWORD)a3 == 1 )
return 0;
if ( (a2[1] & 0x80u) != 0 )
{
v6 = 2;
LABEL_14:
midi_print_buf("< ", a2, v6);
if ( *a2 == 0xB0 )
{
if ( a2[2] != 0xB0 )
midi_changed(a2[1]);
}
else if ( *a2 == 0xF0 && a2[v6 - 1] != 0xF7 )
{
_assert_fail("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 0x38u, "usbmidi_read_data");
}
return v6;
}
if ( (_DWORD)a3 == 2 )
return 0;
v5 = 3LL;
while ( 1 )
{
v6 = v5;
if ( (a2[v5 - 1] & 0x80u) != 0 )
break;
if ( ++v5 == (_DWORD)a3 + 1 )
return 0;
}
LABEL_13:
if ( (int)v6 <= (int)a3 )
goto LABEL_14;
printf("Not enough data for %02x: %d of %d\n", v4, a3, v6);
return 0LL;
}

original_code:
static int
usbmidi_read_data(void *_ctx, const unsigned char *buffer, int len) {
 struct usbmidi_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 2) {
    return 0;
   }
   for(cmdlen = 2; buffer[cmdlen-1] <= 0x7f; cmdlen++) {
    if(cmdlen >= len) {
     return 0;
    }
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
   if(buffer[2] == 0xb0) {
    break;
   }
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 56, __extension__ __PRETTY_FUNCTION__));
   break;
 }
 return cmdlen;
}

canonical_original_code:
static int usbmidi_read_data(void *_ctx, unsigned char *buffer, int len) {
 struct usbmidi_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 2) {
    return 0;
   }
   for(cmdlen = 2; buffer[cmdlen-1] <= 0x7f; cmdlen++) {
    if(cmdlen >= len) {
     return 0;
    }
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
   if(buffer[2] == 0xb0) {
    break;
   }
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 56, __extension__ __PRETTY_FUNCTION__));
   break;
 }
 return cmdlen;
}

variable_types:
buffer
unsigned char *

variable_types: len int

variable_types: struct usbmidi_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; } *

variable_types: cmdlen int

variable_types: dumpoffset int

user_defined_types: struct usbmidi_context { struct midi_context midictx; }


decompiled_code:
void *__fastcall init_usbmidi(__int64 a1)
{
void *v1; // rax
void *v2; // r12
void *v4; // rdi

v1 = malloc(0x20uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "usbmididriver.c", 0x43u, "init_usbmidi");
v2 = v1;
if ( !init_midi(v1, a1, usbmidi_read_data) )
{
v4 = v2;
v2 = 0LL;
free(v4);
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
void *v1; // rax
void *v2; // r12
void *v4; // rdi

v1 = malloc(0x20uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "usbmididriver.c", 0x43u, "init_usbmidi");
v2 = v1;
if ( !init_midi(v1, a1, usbmidi_read_data) )
{
v4 = v2;
v2 = 0LL;
free(v4);
}
return v2;
}

original_code:
struct usbmidi_context *
init_usbmidi(char *path) {
 struct usbmidi_context *ctx;
 ctx = malloc(sizeof(struct usbmidi_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "usbmididriver.c", 67, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, usbmidi_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 return ctx;
}

canonical_original_code:
struct usbmidi_context *init_usbmidi(char *path) {
 struct usbmidi_context *ctx;
 ctx = malloc(sizeof(struct usbmidi_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "usbmididriver.c", 67, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, usbmidi_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 return ctx;
}

variable_types:
path
char *

return_type: struct usbmidi_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; } *


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

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

original_code:
void
teardown_usbmidi(struct usbmidi_context *ctx) {
 teardown_midi(&ctx->midictx);
 free(ctx);
}

user_defined_types: struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); }


decompiled_code:
__int64 __fastcall nanokontrol2_read_data(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
unsigned __int8 v3; // al
int v4; // ebx
unsigned int v5; // er12
unsigned __int8 v6; // al
__int64 result; // rax
int v8; // er12
unsigned __int8 v9; // al
unsigned int v10; // [rsp+Ch] [rbp-2Ch]
unsigned int v11; // [rsp+Ch] [rbp-2Ch]

if ( (int)a3 <= 0 )
return 0LL;
v3 = *a2;
if ( *a2 != 0xF0 )
{
if ( v3 > 0xF0u || v3 != 0xB0 && v3 != 0xBF )
{
v11 = a3;
printf("Wat de fuck is %02x?\n", v3);
midi_print_buf("buffer: ", a2, v11);
abort();
}
v4 = -1;
v5 = 3;
LABEL_7:
if ( (int)v5 > (int)a3 )
{
printf("Not enough data for %02x: %d of %d\n", *a2, a3, v5);
return 0LL;
}
goto LABEL_8;
}
if ( (int)a3 <= 10 )
return 0LL;
v4 = -1;
v5 = 11;
if ( a2[7] == 127 )
{
v8 = a2[8];
if ( (_BYTE)v8 != 127 )
{
v5 = v8 + 11;
v4 = 9;
LABEL_16:
v10 = a3;
printf("supercmdlen: %d\n", v5);
a3 = v10;
goto LABEL_7;
}
if ( (_DWORD)a3 != 11 )
{
if ( a2[9] != 2 )
_assert_fail("buffer[9] == 0x02", "nanokontroldriver.c", 0x32u, "nanokontrol2_read_data");
v4 = 12;
v5 = (a2[10] << 7) + a2[11] + 13;
goto LABEL_16;
}
return 0LL;
}
LABEL_8:
midi_print_buf("< ", a2, v5);
v6 = *a2;
if ( *a2 != 0xBF )
{
if ( v6 == 0xF0 )
{
if ( a2[v5 - 1] != 0xF7 )
_assert_fail("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 0x56u, "nanokontrol2_read_data");
v9 = a2[7];
switch ( v9 )
{
case '_':
switch ( a2[8] )
{
case '!':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x5Fu, "nanokontrol2_read_data");
return result;
case '"':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x62u, "nanokontrol2_read_data");
return result;
case '#':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x64u, "nanokontrol2_read_data");
return result;
case '$':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x67u, "nanokontrol2_read_data");
return result;
case 'B':
*(_DWORD *)(a1 + 32) = a2[9];
break;
default:
return v5;
}
break;
case '\x7F':
if ( a2[v4] == 64 )
midi_print_buf("group 1: ", &a2[v4 + 4], 30LL);
break;
case '@':
if ( a2[8] )
_assert_fail("buffer[8] == 0x00", "nanokontroldriver.c", 0x59u, "nanokontrol2_read_data");
*(_DWORD *)(a1 + 32) = a2[9] != 2;
break;
}
return v5;
}
if ( v6 != 0xB0 )
return v5;
}
midi_changed(a2[1], a2[2]);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
unsigned __int8 v3; // al
int v4; // ebx
unsigned int v5; // er12
unsigned __int8 v6; // al
__int64 result; // rax
int v8; // er12
unsigned __int8 v9; // al
unsigned int v10; // [rsp+Ch] [rbp-2Ch]
unsigned int v11; // [rsp+Ch] [rbp-2Ch]

if ( (int)a3 <= 0 )
return 0LL;
v3 = *a2;
if ( *a2 != 0xF0 )
{
if ( v3 > 0xF0u || v3 != 0xB0 && v3 != 0xBF )
{
v11 = a3;
printf("Wat de fuck is %02x?\n", v3);
midi_print_buf("buffer: ", a2, v11);
abort();
}
v4 = -1;
v5 = 3;
LABEL_7:
if ( (int)v5 > (int)a3 )
{
printf("Not enough data for %02x: %d of %d\n", *a2, a3, v5);
return 0LL;
}
goto LABEL_8;
}
if ( (int)a3 <= 10 )
return 0LL;
v4 = -1;
v5 = 11;
if ( a2[7] == 127 )
{
v8 = a2[8];
if ( (_BYTE)v8 != 127 )
{
v5 = v8 + 11;
v4 = 9;
LABEL_16:
v10 = a3;
printf("supercmdlen: %d\n", v5);
a3 = v10;
goto LABEL_7;
}
if ( (_DWORD)a3 != 11 )
{
if ( a2[9] != 2 )
_assert_fail("buffer[9] == 0x02", "nanokontroldriver.c", 0x32u, "nanokontrol2_read_data");
v4 = 12;
v5 = (a2[10] << 7) + a2[11] + 13;
goto LABEL_16;
}
return 0LL;
}
LABEL_8:
midi_print_buf("< ", a2, v5);
v6 = *a2;
if ( *a2 != 0xBF )
{
if ( v6 == 0xF0 )
{
if ( a2[v5 - 1] != 0xF7 )
_assert_fail("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 0x56u, "nanokontrol2_read_data");
v9 = a2[7];
switch ( v9 )
{
case '_':
switch ( a2[8] )
{
case '!':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x5Fu, "nanokontrol2_read_data");
return result;
case '"':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x62u, "nanokontrol2_read_data");
return result;
case '#':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x64u, "nanokontrol2_read_data");
return result;
case '$':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x67u, "nanokontrol2_read_data");
return result;
case 'B':
*(_DWORD *)(a1 + 32) = a2[9];
break;
default:
return v5;
}
break;
case '\x7F':
if ( a2[v4] == 64 )
midi_print_buf("group 1: ", &a2[v4 + 4], 30LL);
break;
case '@':
if ( a2[8] )
_assert_fail("buffer[8] == 0x00", "nanokontroldriver.c", 0x59u, "nanokontrol2_read_data");
*(_DWORD *)(a1 + 32) = a2[9] != 2;
break;
}
return v5;
}
if ( v6 != 0xB0 )
return v5;
}
midi_changed(a2[1], a2[2]);
return v5;
}

original_code:
static int
nanokontrol2_read_data(void *_ctx, const unsigned char *buffer, int len) {
 struct nanokontrol2_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 11) {
    return 0;
   }
   if(buffer[7] == 0x7f) {
    if(buffer[8] == 0x7f) {
     if(len < 12) {
      return 0;
     }
     ((buffer[9] == 0x02) ? (void) (0) : __assert_fail ("buffer[9] == 0x02", "nanokontroldriver.c", 50, __extension__ __PRETTY_FUNCTION__));
     cmdlen = 11 + buffer[10]*128 + buffer[11] + 2;
     dumpoffset = 12;
    } else {
     cmdlen = 11 + buffer[8];
     dumpoffset = 9;
    }
    printf("supercmdlen: %d\n", cmdlen);
   } else {
    cmdlen = 11;
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 86, __extension__ __PRETTY_FUNCTION__));
   switch(buffer[7]) {
    case 0x40:
     ((buffer[8] == 0x00) ? (void) (0) : __assert_fail ("buffer[8] == 0x00", "nanokontroldriver.c", 89, __extension__ __PRETTY_FUNCTION__));
     ctx->active_mode = (buffer[9] == 02 ? 0 : 1);
     break;
    case 0x5f:
     switch(buffer[8]) {
      case 0x21:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 95, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x22:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 98, __extension__ __PRETTY_FUNCTION__));
      case 0x23:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 100, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x24:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 103, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x42:
       ctx->active_mode = buffer[9];
       break;
     }
     break;
    case 0x7f:
     switch(buffer[dumpoffset]) {
      case 0x40:
       midi_print_buf("group 1: ", buffer + dumpoffset + 1 + 3, 30);
       break;
     }
     break;
   }
   break;
 }
 return cmdlen;
}

canonical_original_code:
static int nanokontrol2_read_data(void *_ctx, unsigned char *buffer, int len) {
 struct nanokontrol2_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 11) {
    return 0;
   }
   if(buffer[7] == 0x7f) {
    if(buffer[8] == 0x7f) {
     if(len < 12) {
      return 0;
     }
     ((buffer[9] == 0x02) ? (void) (0) : __assert_fail ("buffer[9] == 0x02", "nanokontroldriver.c", 50, __extension__ __PRETTY_FUNCTION__));
     cmdlen = 11 + buffer[10]*128 + buffer[11] + 2;
     dumpoffset = 12;
    } else {
     cmdlen = 11 + buffer[8];
     dumpoffset = 9;
    }
    printf("supercmdlen: %d\n", cmdlen);
   } else {
    cmdlen = 11;
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 86, __extension__ __PRETTY_FUNCTION__));
   switch(buffer[7]) {
    case 0x40:
     ((buffer[8] == 0x00) ? (void) (0) : __assert_fail ("buffer[8] == 0x00", "nanokontroldriver.c", 89, __extension__ __PRETTY_FUNCTION__));
     ctx->active_mode = (buffer[9] == 02 ? 0 : 1);
     break;
    case 0x5f:
     switch(buffer[8]) {
      case 0x21:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 95, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x22:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 98, __extension__ __PRETTY_FUNCTION__));
      case 0x23:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 100, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x24:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 103, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x42:
       ctx->active_mode = buffer[9];
       break;
     }
     break;
    case 0x7f:
     switch(buffer[dumpoffset]) {
      case 0x40:
       midi_print_buf("group 1: ", buffer + dumpoffset + 1 + 3, 30);
       break;
     }
     break;
   }
   break;
 }
 return cmdlen;
}

variable_types: struct nanokontrol2_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; int active_mode; int requested_mode; } *

user_defined_types: struct nanokontrol2_context { struct midi_context midictx; int active_mode; int requested_mode; }


decompiled_code:
__int64 __fastcall nanokontrol2_set_led(__int64 a1, char a2, int a3)
{
char v4[11]; // [rsp+Dh] [rbp-Bh] BYREF

v4[1] = a2;
v4[0] = -65;
v4[2] = a3 == 0 ? 0 : 0x7F;
return midi_send_buf(a1, v4, 3LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char a2, int a3)
{
char v4[11]; // [rsp+Dh] [rbp-Bh] BYREF

v4[1] = a2;
v4[0] = -65;
v4[2] = a3 == 0 ? 0 : 0x7F;
return midi_send_buf(a1, v4, 3LL);
}

original_code:
void
nanokontrol2_set_led(struct nanokontrol2_context *ctx, int which, int value) {
 unsigned char msg[] = { 0xbf, which, value == 0 ? 0x00 : 0x7f };
 midi_send_buf(&ctx->midictx, msg, 3);
}

variable_types: which int

variable_types: value int

variable_types:
msg
unsigned char []


decompiled_code:
void *__fastcall init_nanokontrol2(__int64 a1)
{
void *v2; // rax
void *v3; // r12
void *v4; // rdi

v2 = malloc(0x28uLL);
if ( !v2 )
_assert_fail("ctx != NULL", "nanokontroldriver.c", 0x80u, "init_nanokontrol2");
v3 = v2;
v4 = v2;
if ( init_midi(v2, a1, nanokontrol2_read_data) )
{
nanokontrol2_switch_mode_constprop_0((__int64)v4);
}
else
{
free(v4);
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1)
{
void *v2; // rax
void *v3; // r12
void *v4; // rdi

v2 = malloc(0x28uLL);
if ( !v2 )
_assert_fail("ctx != NULL", "nanokontroldriver.c", 0x80u, "init_nanokontrol2");
v3 = v2;
v4 = v2;
if ( init_midi(v2, a1, nanokontrol2_read_data) )
{
func1((__int64)v4);
}
else
{
free(v4);
v3 = 0LL;
}
return v3;
}

original_code:
struct nanokontrol2_context *
init_nanokontrol2(char *path) {
 struct nanokontrol2_context *ctx;
 ctx = malloc(sizeof(struct nanokontrol2_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "nanokontroldriver.c", 128, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, nanokontrol2_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 nanokontrol2_switch_mode(ctx, 1);
 return ctx;
}

canonical_original_code:
struct nanokontrol2_context *init_nanokontrol2(char *path) {
 struct nanokontrol2_context *ctx;
 ctx = malloc(sizeof(struct nanokontrol2_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "nanokontroldriver.c", 128, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, nanokontrol2_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 nanokontrol2_switch_mode(ctx, 1);
 return ctx;
}

return_type: struct nanokontrol2_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; int active_mode; int requested_mode; } *