decompiled_code:
__int64 __fastcall us_test_osc_dispatch_test1_setup(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
int i; // er13
int v5; // ebx
unsigned int v6; // eax
int v7[4]; // [rsp+0h] [rbp-78h] BYREF
char s[104]; // [rsp+10h] [rbp-68h] BYREF

v2 = us_osc_dispatch_init(a2, a1, 1024LL, 0x2000LL);
if ( !(_BYTE)v2 )
goto LABEL_2;
us_osc_dispatch_index_init(v7);
v7[0] = 0;
for ( i = 0; ; v7[0] = i )
{
v5 = 0;
do
{
v7[1] = v5++;
sprintf(s, "/media/%s/%d", media_type[v7[0]], (unsigned int)v5);
(*(void (**)(_QWORD, const char *, ...))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"Adding address '%s'\n",
s);
v6 = us_osc_dispatch_add_table(a2, s, &us_test_osc_dispatch_media_table, v7);
v2 = v6;
}
while ( v5 <= 63 && (_BYTE)v6 );
if ( ++i > 1 || !(_BYTE)v6 )
break;
}
if ( us_log_level > 2 )
us_log_info_proc("trie uses %d nodes", (unsigned int)*(__int16 *)(*(_QWORD *)(a2 + 24) + 2LL));
if ( !(_BYTE)v2 )
LABEL_2:
(*(void (__fastcall **)(__int64))a2)(a2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
int i; // er13
int v5; // ebx
unsigned int v6; // eax
int v7[4]; // [rsp+0h] [rbp-78h] BYREF
char s[104]; // [rsp+10h] [rbp-68h] BYREF

v2 = us_osc_dispatch_init(a2, a1, 1024LL, 0x2000LL);
if ( !(_BYTE)v2 )
goto LABEL_2;
func0(v7);
v7[0] = 0;
for ( i = 0; ; v7[0] = i )
{
v5 = 0;
do
{
v7[1] = v5++;
sprintf(s, "/media/%s/%d", media_type[v7[0]], (unsigned int)v5);
(*(void (**)(_QWORD, const char *, ...))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"Adding address '%s'\n",
s);
v6 = us_osc_dispatch_add_table(a2, s, &us_test_osc_dispatch_media_table, v7);
v2 = v6;
}
while ( v5 <= 63 && (_BYTE)v6 );
if ( ++i > 1 || !(_BYTE)v6 )
break;
}
if ( us_log_level > 2 )
us_log_info_proc("trie uses %d nodes", (unsigned int)*(__int16 *)(*(_QWORD *)(a2 + 24) + 2LL));
if ( !(_BYTE)v2 )
LABEL_2:
(*(void (__fastcall **)(__int64))a2)(a2);
return v2;
}

original_code:
static _Bool us_test_osc_dispatch_test1_setup( us_allocator_t *allocator, us_osc_dispatch_t *osc_dispatch )
{
    _Bool r = 0;
    if ( us_osc_dispatch_init( osc_dispatch, allocator, 1024, 8192 ) )
    {
        char s[64];
        int mt, ch;
        us_osc_dispatch_index_t index;
        us_osc_dispatch_index_init( &index );
        r = 1;
        for ( mt = 0; mt < 2 && r == 1; ++mt )
        {
            index.axis[0] = mt;
            for ( ch = 0; ch < 64 && r == 1; ++ch )
            {
                index.axis[1] = ch;
                sprintf( s, "/media/%s/%d", media_type[index.axis[0]], ch + 1 );
                us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "Adding address '%s'\n", s );
                r &= us_osc_dispatch_add_table( osc_dispatch, s, us_test_osc_dispatch_media_table, &index );
            }
        }
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "trie uses %d nodes", osc_dispatch->trie->m_base.m_num_nodes ); } while ( 0 );
    }
    if ( !r )
    {
        osc_dispatch->destroy( osc_dispatch );
    }
    return r;
}

canonical_original_code:
static _Bool us_test_osc_dispatch_test1_setup(struct us_allocator_s *allocator, struct us_osc_dispatch_s *osc_dispatch)
{
    _Bool r = 0;
    if ( us_osc_dispatch_init( osc_dispatch, allocator, 1024, 8192 ) )
    {
        char s[64];
        int mt; int ch;
        struct us_osc_dispatch_index_s index;
        us_osc_dispatch_index_init( &index );
        r = 1;
        for ( mt = 0; mt < 2 && r == 1; ++mt )
        {
            index.axis[0] = mt;
            for ( ch = 0; ch < 64 && r == 1; ++ch )
            {
                index.axis[1] = ch;
                sprintf( s, "/media/%s/%d", media_type[index.axis[0]], ch + 1 );
                us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "Adding address '%s'\n", s );
                r &= us_osc_dispatch_add_table( osc_dispatch, s, us_test_osc_dispatch_media_table, &index );
            }
        }
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "trie uses %d nodes", osc_dispatch->trie->m_base.m_num_nodes ); } while ( 0 );
    }
    if ( !r )
    {
        osc_dispatch->destroy( osc_dispatch );
    }
    return r;
}

variable_types: struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *

variable_types: struct us_osc_dispatch_s { void (*destroy)(struct us_osc_dispatch_s *self); _Bool (*receive_msg)(struct us_osc_dispatch_s *self, struct us_osc_msg_s { void (*destroy)(struct us_osc_msg_s *self); struct us_osc_msg_element_s { void (*destroy)(struct us_osc_msg_element_s *self, struct us_allocator_s *allocator); _Bool (*print)(struct us_osc_msg_element_s *self, struct us_print_s { void (*destroy)(struct us_print_s *self); _Bool (*printf)(struct us_print_s *self, char *fmt, ...); _Bool (*vprintf)(struct us_print_s *self, char *fmt, __builtin_va_list ap); } *printer); _Bool (*flatten)(struct us_osc_msg_element_s *self, struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *buf, int *length); struct us_osc_msg_element_s *m_next; char m_code; } *(*append)(struct us_osc_msg_s *self, struct us_osc_msg_element_s *element); _Bool (*print)(struct us_osc_msg_s *self, struct us_print_s *printer); _Bool (*flatten)(struct us_osc_msg_s *self, struct us_buffer_s *buf, int *length); struct us_allocator_s *m_allocator; char *m_address; unsigned int m_address_code; struct us_osc_msg_element_s *m_first_element; struct us_osc_msg_element_s *m_last_element; struct us_osc_msg_s *m_next; } *msg, struct us_osc_sender_s { void (*destroy)(struct us_osc_sender_s *self); _Bool (*can_send)(struct us_osc_sender_s *self); _Bool (*send_msg)(struct us_osc_sender_s *self, struct us_osc_msg_s *msg); _Bool (*form_and_send_msg)(struct us_osc_sender_s *self, char *address, char *typetags, ...); } *sender, void *extra); struct us_allocator_s *allocator; struct us_trie_dyn_s { struct us_trie_s { short m_max_nodes; short m_num_nodes; struct us_trie_node_s { short m_parent; short m_sibling; short m_child; char m_value; unsigned short m_flags; } *m_nodes; short m_first_free; _Bool (*m_query_ignorer)(char); _Bool (*m_db_ignorer)(char); int (*m_db_skip)(char); int (*m_comparator)(char, char); } m_base; void (*destroy)(struct us_trie_dyn_s *); struct us_allocator_s *m_allocator; struct us_trie_node_s *m_nodes; } *trie; _Bool (*notify)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s { char *address; _Bool (*receive_msg_proc)(struct us_osc_dispatch_s *self, struct us_osc_msg_s *msg, struct us_osc_dispatch_index_s { int axis[-1]; } *index, struct us_osc_sender_s *sender, void *extra); struct us_osc_dispatch_index_s index; } *table_item, struct us_osc_dispatch_index_s *index); struct us_osc_dispatch_map_s { struct us_osc_dispatch_map_entry_s { struct us_osc_dispatch_table_s *table_entry; struct us_osc_dispatch_index_s final_index; } *entries; int max_entries; int num_entries; } map; } *

variable_types:
s
char [64]

variable_types: mt int

variable_types: ch int


repo: jdkoftinoff/microsupport

decompiled_code:
__int64 __fastcall us_test_osc_dispatch_test1_feed(__int64 a1, __int64 a2)
{
__int64 v3; // r12
__int64 v4; // r12
__int64 v5; // r12
__int64 v6; // r12
__int64 v7; // rbp

v3 = us_osc_msg_form(a1, "/media/in/2/level", ",f", 10.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v3 + 16))(v3, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v3, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v3)(v3);
v4 = us_osc_msg_form(a1, "/media/out/3/pan", ",f", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v4 + 16))(v4, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v4, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v4)(v4);
v5 = us_osc_msg_form(a1, "/media/in/2/mute", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v5 + 16))(v5, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v5, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v5)(v5);
v6 = us_osc_msg_form(a1, "/media/out/raw/SOMETHING", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v6 + 16))(v6, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v6, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v6)(v6);
v7 = us_osc_msg_form(a1, "/media/out/raw/OTHER", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v7 + 16))(v7, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v7, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v7)(v7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
__int64 v3; // r12
__int64 v4; // r12
__int64 v5; // r12
__int64 v6; // r12
__int64 v7; // rbp

v3 = us_osc_msg_form(a1, "/media/in/2/level", ",f", 10.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v3 + 16))(v3, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v3, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v3)(v3);
v4 = us_osc_msg_form(a1, "/media/out/3/pan", ",f", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v4 + 16))(v4, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v4, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v4)(v4);
v5 = us_osc_msg_form(a1, "/media/in/2/mute", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v5 + 16))(v5, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v5, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v5)(v5);
v6 = us_osc_msg_form(a1, "/media/out/raw/SOMETHING", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v6 + 16))(v6, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v6, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v6)(v6);
v7 = us_osc_msg_form(a1, "/media/out/raw/OTHER", ",T", -180.0);
(*(void (__fastcall **)(_QWORD, const char *))(us_testutil_printer_stdout + 8LL))(
us_testutil_printer_stdout,
"\n\nformed msg: \n");
(*(void (__fastcall **)(__int64, _QWORD))(v7 + 16))(v7, us_testutil_printer_stdout);
(*(void (__fastcall **)(__int64, __int64, _QWORD, _QWORD))(a2 + 8))(a2, v7, 0LL, 0LL);
(*(void (__fastcall **)(__int64))v7)(v7);
return 0LL;
}

original_code:
static _Bool us_test_osc_dispatch_test1_feed( us_allocator_t *allocator, us_osc_dispatch_t *osc_dispatch )
{
    _Bool r = 0;
    us_osc_msg_t *msg;
    msg = us_osc_msg_form( allocator, "/media/in/2/level", ",f", 10.0f );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/3/pan", ",f", -180.0f );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/in/2/mute", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/raw/SOMETHING", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/raw/OTHER", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    return r;
}

canonical_original_code:
static _Bool us_test_osc_dispatch_test1_feed(struct us_allocator_s *allocator, struct us_osc_dispatch_s *osc_dispatch)
{
    _Bool r = 0;
    struct us_osc_msg_s *msg;
    msg = us_osc_msg_form( allocator, "/media/in/2/level", ",f", 10.0f );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/3/pan", ",f", -180.0f );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/in/2/mute", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/raw/SOMETHING", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    msg = us_osc_msg_form( allocator, "/media/out/raw/OTHER", ",T" );
    us_testutil_printer_stdout->printf( us_testutil_printer_stdout, "\n\nformed msg: \n" );
    msg->print( msg, us_testutil_printer_stdout );
    osc_dispatch->receive_msg( osc_dispatch, msg, 0, 0 );
    msg->destroy( msg );
    return r;
}

variable_types: struct us_osc_msg_s { void (*destroy)(struct us_osc_msg_s *self); struct us_osc_msg_element_s { void (*destroy)(struct us_osc_msg_element_s *self, struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *allocator); _Bool (*print)(struct us_osc_msg_element_s *self, struct us_print_s { void (*destroy)(struct us_print_s *self); _Bool (*printf)(struct us_print_s *self, char *fmt, ...); _Bool (*vprintf)(struct us_print_s *self, char *fmt, __builtin_va_list ap); } *printer); _Bool (*flatten)(struct us_osc_msg_element_s *self, struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *buf, int *length); struct us_osc_msg_element_s *m_next; char m_code; } *(*append)(struct us_osc_msg_s *self, struct us_osc_msg_element_s *element); _Bool (*print)(struct us_osc_msg_s *self, struct us_print_s *printer); _Bool (*flatten)(struct us_osc_msg_s *self, struct us_buffer_s *buf, int *length); struct us_allocator_s *m_allocator; char *m_address; unsigned int m_address_code; struct us_osc_msg_element_s *m_first_element; struct us_osc_msg_element_s *m_last_element; struct us_osc_msg_s *m_next; } *

user_defined_types: struct us_osc_dispatch_s { void (*destroy)(struct us_osc_dispatch_s *self); _Bool (*receive_msg)(struct us_osc_dispatch_s *self, struct us_osc_msg_s *msg, struct us_osc_sender_s *sender, void *extra); struct us_allocator_s *allocator; struct us_trie_dyn_s *trie; _Bool (*notify)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s *table_item, struct us_osc_dispatch_index_s *index); struct us_osc_dispatch_map_s map; }

user_defined_types: struct us_osc_msg_s { void (*destroy)(struct us_osc_msg_s *self); struct us_osc_msg_element_s *(*append)(struct us_osc_msg_s *self, struct us_osc_msg_element_s *element); _Bool (*print)(struct us_osc_msg_s *self, struct us_print_s *printer); _Bool (*flatten)(struct us_osc_msg_s *self, struct us_buffer_s *buf, int *length); struct us_allocator_s *m_allocator; char *m_address; unsigned int m_address_code; struct us_osc_msg_element_s *m_first_element; struct us_osc_msg_element_s *m_last_element; struct us_osc_msg_s *m_next; }

user_defined_types: struct us_osc_dispatch_map_s { struct us_osc_dispatch_map_entry_s *entries; int max_entries; int num_entries; }

user_defined_types: struct us_trie_dyn_s { struct us_trie_s m_base; void (*destroy)(struct us_trie_dyn_s *); struct us_allocator_s *m_allocator; struct us_trie_node_s *m_nodes; }

user_defined_types: struct us_trie_s { short m_max_nodes; short m_num_nodes; struct us_trie_node_s *m_nodes; short m_first_free; _Bool (*m_query_ignorer)(char); _Bool (*m_db_ignorer)(char); int (*m_db_skip)(char); int (*m_comparator)(char, char); }

user_defined_types: struct us_osc_dispatch_map_entry_s { struct us_osc_dispatch_table_s *table_entry; struct us_osc_dispatch_index_s final_index; }

user_defined_types: struct us_osc_sender_s { void (*destroy)(struct us_osc_sender_s *self); _Bool (*can_send)(struct us_osc_sender_s *self); _Bool (*send_msg)(struct us_osc_sender_s *self, struct us_osc_msg_s *msg); _Bool (*form_and_send_msg)(struct us_osc_sender_s *self, char *address, char *typetags, ...); }

user_defined_types: struct us_osc_dispatch_table_s { char *address; _Bool (*receive_msg_proc)(struct us_osc_dispatch_s *self, struct us_osc_msg_s *msg, struct us_osc_dispatch_index_s *index, struct us_osc_sender_s *sender, void *extra); struct us_osc_dispatch_index_s index; }

user_defined_types: struct us_osc_msg_element_s { void (*destroy)(struct us_osc_msg_element_s *self, struct us_allocator_s *allocator); _Bool (*print)(struct us_osc_msg_element_s *self, struct us_print_s *printer); _Bool (*flatten)(struct us_osc_msg_element_s *self, struct us_buffer_s *buf, int *length); struct us_osc_msg_element_s *m_next; char m_code; }

user_defined_types: struct us_trie_node_s { short m_parent; short m_sibling; short m_child; char m_value; unsigned short m_flags; }

user_defined_types: struct us_osc_dispatch_index_s { int axis[-1]; }


decompiled_code:
__int64 us_test_osc_dispatch_test1()
{
unsigned int v0; // ebx
__int64 v2[9]; // [rsp+0h] [rbp-48h] BYREF

if ( us_log_level > 2 )
us_log_info_proc("us_test_osc_dispatch_test1 init");
v0 = us_test_osc_dispatch_test1_setup(us_testutil_sys_allocator, (__int64)v2);
if ( (_BYTE)v0 )
{
v0 = us_test_osc_dispatch_test1_feed(us_testutil_session_allocator, (__int64)v2);
((void (__fastcall *)(__int64 *))v2[0])(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // ebx
__int64 v2[9]; // [rsp+0h] [rbp-48h] BYREF

if ( us_log_level > 2 )
us_log_info_proc("us_test_osc_dispatch_test1 init");
v0 = func6(us_testutil_sys_allocator, (__int64)v2);
if ( (_BYTE)v0 )
{
v0 = func7(us_testutil_session_allocator, (__int64)v2);
((void (__fastcall *)(__int64 *))v2[0])(v2);
}
return v0;
}

original_code:
static _Bool us_test_osc_dispatch_test1( void )
{
    _Bool r = 0;
    us_osc_dispatch_t osc_dispatch;
    do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "us_test_osc_dispatch_test1 init" ); } while ( 0 );
    if ( us_test_osc_dispatch_test1_setup( us_testutil_sys_allocator, &osc_dispatch ) )
    {
        r = us_test_osc_dispatch_test1_feed( us_testutil_session_allocator, &osc_dispatch );
        osc_dispatch.destroy( &osc_dispatch );
    }
    return r;
}

canonical_original_code:
static _Bool us_test_osc_dispatch_test1()
{
    _Bool r = 0;
    struct us_osc_dispatch_s osc_dispatch;
    do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "us_test_osc_dispatch_test1 init" ); } while ( 0 );
    if ( us_test_osc_dispatch_test1_setup( us_testutil_sys_allocator, &osc_dispatch ) )
    {
        r = us_test_osc_dispatch_test1_feed( us_testutil_session_allocator, &osc_dispatch );
        osc_dispatch.destroy( &osc_dispatch );
    }
    return r;
}

variable_types: struct us_osc_dispatch_s { void (*destroy)(struct us_osc_dispatch_s *self); _Bool (*receive_msg)(struct us_osc_dispatch_s *self, struct us_osc_msg_s { void (*destroy)(struct us_osc_msg_s *self); struct us_osc_msg_element_s { void (*destroy)(struct us_osc_msg_element_s *self, struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *allocator); _Bool (*print)(struct us_osc_msg_element_s *self, struct us_print_s { void (*destroy)(struct us_print_s *self); _Bool (*printf)(struct us_print_s *self, char *fmt, ...); _Bool (*vprintf)(struct us_print_s *self, char *fmt, __builtin_va_list ap); } *printer); _Bool (*flatten)(struct us_osc_msg_element_s *self, struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *buf, int *length); struct us_osc_msg_element_s *m_next; char m_code; } *(*append)(struct us_osc_msg_s *self, struct us_osc_msg_element_s *element); _Bool (*print)(struct us_osc_msg_s *self, struct us_print_s *printer); _Bool (*flatten)(struct us_osc_msg_s *self, struct us_buffer_s *buf, int *length); struct us_allocator_s *m_allocator; char *m_address; unsigned int m_address_code; struct us_osc_msg_element_s *m_first_element; struct us_osc_msg_element_s *m_last_element; struct us_osc_msg_s *m_next; } *msg, struct us_osc_sender_s { void (*destroy)(struct us_osc_sender_s *self); _Bool (*can_send)(struct us_osc_sender_s *self); _Bool (*send_msg)(struct us_osc_sender_s *self, struct us_osc_msg_s *msg); _Bool (*form_and_send_msg)(struct us_osc_sender_s *self, char *address, char *typetags, ...); } *sender, void *extra); struct us_allocator_s *allocator; struct us_trie_dyn_s { struct us_trie_s { short m_max_nodes; short m_num_nodes; struct us_trie_node_s { short m_parent; short m_sibling; short m_child; char m_value; unsigned short m_flags; } *m_nodes; short m_first_free; _Bool (*m_query_ignorer)(char); _Bool (*m_db_ignorer)(char); int (*m_db_skip)(char); int (*m_comparator)(char, char); } m_base; void (*destroy)(struct us_trie_dyn_s *); struct us_allocator_s *m_allocator; struct us_trie_node_s *m_nodes; } *trie; _Bool (*notify)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s { char *address; _Bool (*receive_msg_proc)(struct us_osc_dispatch_s *self, struct us_osc_msg_s *msg, struct us_osc_dispatch_index_s { int axis[-1]; } *index, struct us_osc_sender_s *sender, void *extra); struct us_osc_dispatch_index_s index; } *table_item, struct us_osc_dispatch_index_s *index); struct us_osc_dispatch_map_s { struct us_osc_dispatch_map_entry_s { struct us_osc_dispatch_table_s *table_entry; struct us_osc_dispatch_index_s final_index; } *entries; int max_entries; int num_entries; } map; }


decompiled_code:
__int64 __fastcall us_test_osc_dispatch_main(unsigned int a1, __int64 a2)
{
unsigned __int8 v2; // bl

v2 = us_testutil_start(81920LL, 0x2000LL, a1, a2);
if ( v2 )
{
us_logger_printer_start(us_testutil_printer_stdout, us_testutil_printer_stderr);
if ( us_log_level > 2 )
us_log_info_proc("Hello world from %s compiled on %s", "../../../src/us_test_osc_dispatch_main.c", "May 23 2025");
v2 = us_test_osc_dispatch_test1();
us_logger_finish();
us_testutil_finish();
}
return v2 ^ 1u;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, __int64 a2)
{
unsigned __int8 v2; // bl

v2 = us_testutil_start(81920LL, 0x2000LL, a1, a2);
if ( v2 )
{
us_logger_printer_start(us_testutil_printer_stdout, us_testutil_printer_stderr);
if ( us_log_level > 2 )
us_log_info_proc("Hello world from %s compiled on %s", "../../../src/us_test_osc_dispatch_main.c", "May 23 2025");
v2 = func8();
us_logger_finish();
us_testutil_finish();
}
return v2 ^ 1u;
}

original_code:
int us_test_osc_dispatch_main( int argc, const char **argv )
{
    _Bool r = 1;
    r = us_testutil_start( 81920, 8192, argc, argv );
    if ( r )
    {
        us_logger_printer_start( us_testutil_printer_stdout, us_testutil_printer_stderr );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Hello world from %s compiled on %s", "../../../src/us_test_osc_dispatch_main.c", "Apr 24 2024" ); } while ( 0 );
        r &= us_test_osc_dispatch_test1();
        us_logger_finish();
        us_testutil_finish();
    }
    return r ? 0 : 1;
}

canonical_original_code:
int us_test_osc_dispatch_main(int argc, char **argv)
{
    _Bool r = 1;
    r = us_testutil_start( 81920, 8192, argc, argv );
    if ( r )
    {
        us_logger_printer_start( us_testutil_printer_stdout, us_testutil_printer_stderr );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Hello world from %s compiled on %s", "../../../src/us_test_osc_dispatch_main.c", "Apr 24 2024" ); } while ( 0 );
        r &= us_test_osc_dispatch_test1();
        us_logger_finish();
        us_testutil_finish();
    }
    return r ? 0 : 1;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
__int64 __fastcall us_test_allocator_main(unsigned int a1, __int64 a2)
{
char v2; // dl
__int64 result; // rax
const void *v4; // rax

v2 = us_testutil_start(2048LL, 2048LL, a1, a2);
result = 1LL;
if ( v2 )
{
us_logger_printer_start(us_testutil_printer_stdout, us_testutil_printer_stderr);
us_log_level = 4;
us_log_info_proc("Hello world from %s compiled on %s", "../../../src/us_test_allocator_main.c", "May 23 2025");
v4 = (const void *)(*(__int64 (__fastcall **)(_QWORD, __int64, __int64))(us_testutil_sys_allocator + 8LL))(
us_testutil_sys_allocator,
512LL,
1LL);
if ( us_log_level > 3 )
us_log_debug_proc("result from alloc of 512 bytes: %p", v4);
if ( us_log_level > 2 )
us_log_info_proc("Finishing us_test_allocator");
us_logger_finish();
us_testutil_finish();
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2)
{
char v2; // dl
__int64 result; // rax
const void *v4; // rax

v2 = us_testutil_start(2048LL, 2048LL, a1, a2);
result = 1LL;
if ( v2 )
{
us_logger_printer_start(us_testutil_printer_stdout, us_testutil_printer_stderr);
us_log_level = 4;
us_log_info_proc("Hello world from %s compiled on %s", "../../../src/us_test_allocator_main.c", "May 23 2025");
v4 = (const void *)(*(__int64 (__fastcall **)(_QWORD, __int64, __int64))(us_testutil_sys_allocator + 8LL))(
us_testutil_sys_allocator,
512LL,
1LL);
if ( us_log_level > 3 )
us_log_debug_proc("result from alloc of 512 bytes: %p", v4);
if ( us_log_level > 2 )
us_log_info_proc("Finishing us_test_allocator");
us_logger_finish();
us_testutil_finish();
result = 0LL;
}
return result;
}

original_code:
int us_test_allocator_main( int argc, const char **argv )
{
    int r = 1;
    if ( us_testutil_start( 2048, 2048, argc, argv ) )
    {
        char *b;
        us_logger_printer_start( us_testutil_printer_stdout, us_testutil_printer_stderr );
        do { us_log_level = ( ( 4 ) ); } while ( 0 );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Hello world from %s compiled on %s", "../../../src/us_test_allocator_main.c", "Apr 24 2024" ); } while ( 0 );
        b = (char *)us_testutil_sys_allocator->alloc( us_testutil_sys_allocator, 512, 1 );
        do { if ( us_log_level >= ( 4 ) ) us_log_debug_proc( "result from alloc of 512 bytes: %p", b ); } while ( 0 );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Finishing us_test_allocator" ); } while ( 0 );
        us_logger_finish();
        us_testutil_finish();
        r = 0;
    }
    return r;
}

canonical_original_code:
int us_test_allocator_main(int argc, char **argv)
{
    int r = 1;
    if ( us_testutil_start( 2048, 2048, argc, argv ) )
    {
        char *b;
        us_logger_printer_start( us_testutil_printer_stdout, us_testutil_printer_stderr );
        do { us_log_level = ( ( 4 ) ); } while ( 0 );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Hello world from %s compiled on %s", "../../../src/us_test_allocator_main.c", "Apr 24 2024" ); } while ( 0 );
        b = (char *)us_testutil_sys_allocator->alloc( us_testutil_sys_allocator, 512, 1 );
        do { if ( us_log_level >= ( 4 ) ) us_log_debug_proc( "result from alloc of 512 bytes: %p", b ); } while ( 0 );
        do { if ( us_log_level >= ( 3 ) ) us_log_info_proc( "Finishing us_test_allocator" ); } while ( 0 );
        us_logger_finish();
        us_testutil_finish();
        r = 0;
    }
    return r;
}

variable_types: r int

variable_types:
b
char *


decompiled_code:
void __fastcall us_reactor_handler_finish(__int64 a1)
{
*(_BYTE *)(a1 + 48) = 1;
}

original_code:
static inline void us_reactor_handler_finish( us_reactor_handler_t *self )
{
    self->m_finished = 1;
}

canonical_original_code:
static inline void us_reactor_handler_finish(struct us_reactor_handler_s *self)
{
    self->m_finished = 1;
}

variable_types: struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s *m_allocator; struct us_reactor_handler_s *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd { int fd; short events; short revents; } *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); } *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; } *

return_type: void

user_defined_types: struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; }

user_defined_types: struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s *m_allocator; struct us_reactor_handler_s *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); }


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

(*(void (**)(void))(a1 + 8))();
result = *(_QWORD *)(a1 + 40);
if ( result )
result = (*(__int64 (__fastcall **)(_QWORD, __int64))(result + 24))(*(_QWORD *)(a1 + 40), a1);
return result;
}

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

(*(void (**)(void))(a1 + 8))();
result = *(_QWORD *)(a1 + 40);
if ( result )
result = (*(__int64 (__fastcall **)(_QWORD, __int64))(result + 24))(*(_QWORD *)(a1 + 40), a1);
return result;
}

original_code:
void us_reactor_handler_destroy( us_reactor_handler_t *self )
{
    self->close( self );
    do { if ( ( self->m_allocator ) != 0 && ( self ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self ) ); } while ( 0 );
}

canonical_original_code:
void us_reactor_handler_destroy(struct us_reactor_handler_s *self)
{
    self->close( self );
    do { if ( ( self->m_allocator ) != 0 && ( self ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self ) ); } while ( 0 );
}

user_defined_types: struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); }

user_defined_types: struct pollfd { int fd; short events; short revents; }


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

while ( *(_QWORD *)(a1 + 16) )
(*(void (__fastcall **)(__int64))(a1 + 64))(a1);
result = *(_QWORD *)(a1 + 8);
if ( result && *(_QWORD *)(a1 + 40) )
result = (*(__int64 (__fastcall **)(_QWORD))(result + 24))(*(_QWORD *)(a1 + 8));
*(_QWORD *)(a1 + 40) = 0LL;
return result;
}

original_code:
void us_reactor_destroy( us_reactor_t *self )
{
    while ( self->m_handlers )
    {
        self->remove_item( self, self->m_handlers );
    }
    do { if ( ( self->m_allocator ) != 0 && ( self->m_poll_handlers ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self->m_poll_handlers ) ); } while ( 0 );
    self->m_poll_handlers = 0;
}

canonical_original_code:
void us_reactor_destroy(struct us_reactor_s *self)
{
    while ( self->m_handlers )
    {
        self->remove_item( self, self->m_handlers );
    }
    do { if ( ( self->m_allocator ) != 0 && ( self->m_poll_handlers ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self->m_poll_handlers ) ); } while ( 0 );
    self->m_poll_handlers = 0;
}

variable_types: struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; } *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd { int fd; short events; short revents; } *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); } *


original_code:
_Bool us_reactor_add_item( us_reactor_t *self, us_reactor_handler_t *item )
{
    _Bool r = 0;
    us_reactor_handler_t **node = &self->m_handlers;
    if ( self->m_num_handlers < self->m_max_handlers )
    {
        while ( *node )
        {
            node = &( *node )->m_next;
        }
        self->m_num_handlers++;
        *node = item;
        item->m_reactor = self;
        r = 1;
    }
    return r;
}

canonical_original_code:
_Bool us_reactor_add_item(struct us_reactor_s *self, struct us_reactor_handler_s *item)
{
    _Bool r = 0;
    struct us_reactor_handler_s **node = &self->m_handlers;
    if ( self->m_num_handlers < self->m_max_handlers )
    {
        while ( *node )
        {
            node = &( *node )->m_next;
        }
        self->m_num_handlers++;
        *node = item;
        item->m_reactor = self;
        r = 1;
    }
    return r;
}

variable_types:
item
struct us_reactor_handler_s *

variable_types: struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s *m_allocator; struct us_reactor_handler_s *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd { int fd; short events; short revents; } *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); } *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; } **


original_code:
_Bool us_reactor_remove_item( us_reactor_t *self, us_reactor_handler_t *item )
{
    _Bool r = 0;
    us_reactor_handler_t **node = &self->m_handlers;
    do { } while ( 0 );
    while ( *node )
    {
        if ( *node == item )
        {
            us_reactor_handler_t *next = ( *node )->m_next;
            self->m_num_handlers--;
            *node = next;
            r = 1;
        }
        else
        {
            node = &( *node )->m_next;
        }
    }
    item->destroy( item );
    return r;
}

canonical_original_code:
_Bool us_reactor_remove_item(struct us_reactor_s *self, struct us_reactor_handler_s *item)
{
    _Bool r = 0;
    struct us_reactor_handler_s **node = &self->m_handlers;
    do { } while ( 0 );
    while ( *node )
    {
        if ( *node == item )
        {
            struct us_reactor_handler_s *next = ( *node )->m_next;
            self->m_num_handlers--;
            *node = next;
            r = 1;
        }
        else
        {
            node = &( *node )->m_next;
        }
    }
    item->destroy( item );
    return r;
}


decompiled_code:
int __fastcall us_reactor_handler_close(__int64 a1)
{
int v2; // edi
int result; // eax

v2 = *(_DWORD *)(a1 + 80);
if ( v2 != -1 )
{
result = closesocket(v2);
*(_DWORD *)(a1 + 80) = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
int v2; // edi
int result; // eax

v2 = *(_DWORD *)(a1 + 80);
if ( v2 != -1 )
{
result = func5(v2);
*(_DWORD *)(a1 + 80) = -1;
}
return result;
}

original_code:
void us_reactor_handler_close( us_reactor_handler_t *self )
{
    if ( self->m_fd != -1 )
    {
        closesocket( self->m_fd );
        self->m_fd = -1;
    }
}

canonical_original_code:
void us_reactor_handler_close(struct us_reactor_handler_s *self)
{
    if ( self->m_fd != -1 )
    {
        closesocket( self->m_fd );
        self->m_fd = -1;
    }
}


original_code:
_Bool us_reactor_handler_init( us_reactor_handler_t *self, us_allocator_t *allocator, int fd, void *extra )
{
    self->m_allocator = allocator;
    self->m_finished = 0;
    self->m_next = 0;
    self->m_reactor = 0;
    self->m_extra = extra;
    self->m_fd = fd;
    self->m_wake_on_readable = 0;
    self->m_wake_on_writable = 0;
    self->close = us_reactor_handler_close;
    self->destroy = us_reactor_handler_destroy;
    self->tick = 0;
    self->readable = 0;
    self->writable = 0;
    return 1;
}

canonical_original_code:
_Bool us_reactor_handler_init(struct us_reactor_handler_s *self, struct us_allocator_s *allocator, int fd, void *extra)
{
    self->m_allocator = allocator;
    self->m_finished = 0;
    self->m_next = 0;
    self->m_reactor = 0;
    self->m_extra = extra;
    self->m_fd = fd;
    self->m_wake_on_readable = 0;
    self->m_wake_on_writable = 0;
    self->close = us_reactor_handler_close;
    self->destroy = us_reactor_handler_destroy;
    self->tick = 0;
    self->readable = 0;
    self->writable = 0;
    return 1;
}

variable_types:
allocator
struct us_allocator_s *

variable_types: fd int

variable_types:
extra
void *


original_code:
us_reactor_handler_t *us_reactor_handler_create( us_allocator_t *allocator )
{
    us_reactor_handler_t *item = 0;
    item = ( us_reactor_handler_t * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( us_reactor_handler_t ), 1 ) );
    if ( item != ((void *)0) )
    {
        if ( !us_reactor_handler_init( item, allocator, -1, 0 ) )
        {
            do { if ( ( allocator ) != 0 && ( item ) != 0 ) ( allocator )->free( ( allocator ), ( item ) ); } while ( 0 );
            item = 0;
        }
    }
    return item;
}

canonical_original_code:
struct us_reactor_handler_s *us_reactor_handler_create(struct us_allocator_s *allocator)
{
    struct us_reactor_handler_s *item = 0;
    item = ( struct us_reactor_handler_s * )( ( allocator )->alloc( ( allocator ), (int)sizeof( us_reactor_handler_t ), 1 ) );
    if ( item != ((void *)0) )
    {
        if ( !us_reactor_handler_init( item, allocator, -1, 0 ) )
        {
            do { if ( ( allocator ) != 0 && ( item ) != 0 ) ( allocator )->free( ( allocator ), ( item ) ); } while ( 0 );
            item = 0;
        }
    }
    return item;
}

return_type: struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s *m_allocator; struct us_reactor_handler_s *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd { int fd; short events; short revents; } *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); } *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; } *


original_code:
_Bool us_reactor_init( us_reactor_t *self, us_allocator_t *allocator, int max_handlers )
{
    self->m_allocator = allocator;
    self->destroy = us_reactor_destroy;
    self->m_handlers = 0;
    self->m_max_handlers = max_handlers;
    self->m_poll_handlers = ( struct pollfd * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( struct pollfd ), ( int32_t )( max_handlers ) ) );
    self->m_timeout = 0;
    self->add_item = us_reactor_add_item;
    self->remove_item = us_reactor_remove_item;
    self->poll = us_reactor_poll;
    self->m_num_handlers = 0;
    if ( !self->m_poll_handlers )
    {
        return 0;
    }
    return 1;
}

canonical_original_code:
_Bool us_reactor_init(struct us_reactor_s *self, struct us_allocator_s *allocator, int max_handlers)
{
    self->m_allocator = allocator;
    self->destroy = us_reactor_destroy;
    self->m_handlers = 0;
    self->m_max_handlers = max_handlers;
    self->m_poll_handlers = ( struct pollfd * )( ( allocator )->alloc( ( allocator ), (int)sizeof( struct pollfd ), ( int )( max_handlers ) ) );
    self->m_timeout = 0;
    self->add_item = us_reactor_add_item;
    self->remove_item = us_reactor_remove_item;
    self->poll = us_reactor_poll;
    self->m_num_handlers = 0;
    if ( !self->m_poll_handlers )
    {
        return 0;
    }
    return 1;
}


original_code:
void us_reactor_collect_finished( us_reactor_t *self )
{
    us_reactor_handler_t **item;
    item = &self->m_handlers;
    while ( *item )
    {
        if ( ( *item )->tick )
        {
            ( *item )->tick( *item );
        }
        if ( ( *item )->m_finished == 1 || ( *item )->m_fd == -1 )
        {
            us_reactor_handler_t *next = ( *item )->m_next;
            do { } while ( 0 );
            self->remove_item( self, *item );
            *item = next;
        }
        else
        {
            item = &( *item )->m_next;
        }
    }
}

canonical_original_code:
void us_reactor_collect_finished(struct us_reactor_s *self)
{
    struct us_reactor_handler_s **item;
    item = &self->m_handlers;
    while ( *item )
    {
        if ( ( *item )->tick )
        {
            ( *item )->tick( *item );
        }
        if ( ( *item )->m_finished == 1 || ( *item )->m_fd == -1 )
        {
            struct us_reactor_handler_s *next = ( *item )->m_next;
            do { } while ( 0 );
            self->remove_item( self, *item );
            *item = next;
        }
        else
        {
            item = &( *item )->m_next;
        }
    }
}


original_code:
void us_reactor_fill_poll( us_reactor_t *self )
{
    us_reactor_handler_t *item;
    int n = 0;
    item = self->m_handlers;
    while ( item )
    {
        if ( item->m_fd != -1 && !item->m_finished )
        {
            struct pollfd *p;
            p = &self->m_poll_handlers[n++];
            p->events = 0;
            p->revents = 0;
            p->fd = item->m_fd;
            if ( item->m_wake_on_readable && item->readable != 0 )
            {
                do { } while ( 0 );
                p->events |= 0x001;
            }
            if ( item->m_wake_on_writable && item->writable != 0 )
            {
                do { } while ( 0 );
                p->events |= 0x004;
            }
        }
        item = item->m_next;
    }
}

canonical_original_code:
void us_reactor_fill_poll(struct us_reactor_s *self)
{
    struct us_reactor_handler_s *item;
    int n = 0;
    item = self->m_handlers;
    while ( item )
    {
        if ( item->m_fd != -1 && !item->m_finished )
        {
            struct pollfd *p;
            p = &self->m_poll_handlers[n++];
            p->events = 0;
            p->revents = 0;
            p->fd = item->m_fd;
            if ( item->m_wake_on_readable && item->readable != 0 )
            {
                do { } while ( 0 );
                p->events |= 0x001;
            }
            if ( item->m_wake_on_writable && item->writable != 0 )
            {
                do { } while ( 0 );
                p->events |= 0x004;
            }
        }
        item = item->m_next;
    }
}

variable_types: n int

variable_types: struct pollfd { int fd; short events; short revents; } *


decompiled_code:
__int64 __fastcall us_reactor_poll(__int64 a1, unsigned int a2)
{
int v3; // esi
int v4; // eax
__int64 v5; // rbx
int v6; // er14
__int64 v7; // r14
__int64 v8; // r12
__int64 v9; // rbp

us_reactor_collect_finished(a1);
us_reactor_fill_poll(a1);
v3 = *(_DWORD *)(a1 + 32);
if ( v3 <= 0 )
{
a2 = 0;
}
else
{
v4 = poll(*(struct pollfd **)(a1 + 40), v3, a2);
if ( v4 <= 0 )
{
LOBYTE(a2) = v4 == 0;
}
else
{
v5 = *(_QWORD *)(a1 + 16);
v6 = *(_DWORD *)(a1 + 32);
if ( v6 > 0 )
{
v7 = 8LL * (unsigned int)v6;
v8 = 0LL;
do
{
v9 = *(_QWORD *)(a1 + 40) + v8;
if ( *(_DWORD *)v9 != *(_DWORD *)(v5 + 80) )
{
if ( us_log_level > 0 )
us_log_error_proc("item %p fd %d != p->fd", v5);
abort();
}
if ( *(_BYTE *)(v5 + 84) && (*(_BYTE *)(v9 + 6) & 1) != 0 )
(*(void (__fastcall **)(__int64))(v5 + 24))(v5);
if ( *(_BYTE *)(v5 + 85) && (*(_BYTE *)(v9 + 6) & 4) != 0 )
(*(void (__fastcall **)(__int64))(v5 + 32))(v5);
if ( (*(_BYTE *)(v9 + 6) & 0x10) != 0 )
us_reactor_handler_finish(v5);
if ( (*(_BYTE *)(v9 + 6) & 8) != 0 )
us_reactor_handler_finish(v5);
if ( (*(_WORD *)(v9 + 6) & 0x20) != 0 )
{
if ( us_log_level > 0 )
us_log_error_proc("poll item %p fd %d got NVAL: %d", v5);
us_reactor_handler_finish(v5);
}
v5 = *(_QWORD *)(v5 + 56);
v8 += 8LL;
}
while ( v8 != v7 );
}
a2 = 1;
}
}
us_reactor_collect_finished(a1);
return a2;
}

original_code:
_Bool us_reactor_poll( us_reactor_t *self, int timeout )
{
    _Bool r = 0;
    us_reactor_collect_finished( self );
    us_reactor_fill_poll( self );
    if ( self->m_num_handlers > 0 )
    {
        int n;
        n = poll( self->m_poll_handlers, self->m_num_handlers, timeout );
        if ( n < 0 && (*__errno_location ()) != 4 )
        {
            do { } while ( 0 );
            r = 0;
        }
        if ( n > 0 )
        {
            us_reactor_handler_t *item = self->m_handlers;
            int n;
            int num = self->m_num_handlers;
            for ( n = 0; n < num; ++n, item = item->m_next )
            {
                struct pollfd *p = &self->m_poll_handlers[n];
                if ( p->fd != item->m_fd )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "item %p fd %d != p->fd", (void *)item, item->m_fd, p->fd ); } while ( 0 );
                    abort();
                }
                if ( item->m_wake_on_readable && ( p->revents & 0x001 ) )
                {
                    do { } while ( 0 );
                    item->readable( item );
                }
                if ( item->m_wake_on_writable && ( p->revents & 0x004 ) )
                {
                    do { } while ( 0 );
                    item->writable( item );
                }
                if ( ( p->revents & 0x010 ) )
                {
                    do { } while ( 0 );
                    us_reactor_handler_finish( item );
                }
                if ( ( p->revents & 0x008 ) )
                {
                    do { } while ( 0 );
                    us_reactor_handler_finish( item );
                }
                if ( ( p->revents & 0x020 ) )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "poll item %p fd %d got NVAL: %d", (void *)item, item->m_fd, p->revents ); } while ( 0 );
                    us_reactor_handler_finish( item );
                }
            }
            r = 1;
        }
        if ( n == 0 )
        {
            r = 1;
        }
    }
    us_reactor_collect_finished( self );
    return r;
}

canonical_original_code:
_Bool us_reactor_poll(struct us_reactor_s *self, int timeout)
{
    _Bool r = 0;
    us_reactor_collect_finished( self );
    us_reactor_fill_poll( self );
    if ( self->m_num_handlers > 0 )
    {
        int n;
        n = poll( self->m_poll_handlers, self->m_num_handlers, timeout );
        if ( n < 0 && (*__errno_location ()) != 4 )
        {
            do { } while ( 0 );
            r = 0;
        }
        if ( n > 0 )
        {
            struct us_reactor_handler_s *item = self->m_handlers;
            int n;
            int num = self->m_num_handlers;
            for ( n = 0; n < num; ++n, item = item->m_next )
            {
                struct pollfd *p = &self->m_poll_handlers[n];
                if ( p->fd != item->m_fd )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "item %p fd %d != p->fd", (void *)item, item->m_fd, p->fd ); } while ( 0 );
                    abort();
                }
                if ( item->m_wake_on_readable && ( p->revents & 0x001 ) )
                {
                    do { } while ( 0 );
                    item->readable( item );
                }
                if ( item->m_wake_on_writable && ( p->revents & 0x004 ) )
                {
                    do { } while ( 0 );
                    item->writable( item );
                }
                if ( ( p->revents & 0x010 ) )
                {
                    do { } while ( 0 );
                    us_reactor_handler_finish( item );
                }
                if ( ( p->revents & 0x008 ) )
                {
                    do { } while ( 0 );
                    us_reactor_handler_finish( item );
                }
                if ( ( p->revents & 0x020 ) )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "poll item %p fd %d got NVAL: %d", (void *)item, item->m_fd, p->revents ); } while ( 0 );
                    us_reactor_handler_finish( item );
                }
            }
            r = 1;
        }
        if ( n == 0 )
        {
            r = 1;
        }
    }
    us_reactor_collect_finished( self );
    return r;
}

variable_types: timeout int

variable_types: num int


decompiled_code:
__int64 __fastcall us_reactor_create_tcp_client(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, unsigned __int8 a7, __int64 (__fastcall *a8)(__int64), __int64 (__fastcall *a9)(__int64, __int64, __int64, __int64, __int64, __int64, __int64, _QWORD))
{
__int64 v11; // rbx
__int64 result; // rax

v11 = a8(a2);
result = 0LL;
if ( v11 )
{
result = a9(v11, a2, 0xFFFFFFFFLL, a3, a4, a5, a6, a7);
if ( (_BYTE)result )
result = (*(__int64 (__fastcall **)(__int64, __int64))(a1 + 56))(a1, v11);
}
return result;
}

original_code:
_Bool us_reactor_create_tcp_client( us_reactor_t *self,
                                   us_allocator_t *allocator,
                                   void *extra,
                                   ssize_t queue_buf_size,
                                   const char *server_host,
                                   const char *server_port,
                                   _Bool keep_open,
                                   us_reactor_handler_create_proc_t client_handler_create,
                                   us_reactor_handler_tcp_client_init_proc_t client_handler_init )
{
    _Bool r = 0;
    us_reactor_handler_tcp_client_t *handler = (us_reactor_handler_tcp_client_t *)client_handler_create( allocator );
    if ( handler )
    {
        r = client_handler_init(
            &handler->m_base.m_base, allocator, -1, extra, queue_buf_size, server_host, server_port, keep_open );
        if ( r )
        {
            r = self->add_item( self, &handler->m_base.m_base );
        }
    }
    return r;
}

canonical_original_code:
_Bool us_reactor_create_tcp_client(struct us_reactor_s *self, struct us_allocator_s *allocator, void *extra, long queue_buf_size, char *server_host, char *server_port, _Bool keep_open, struct us_reactor_handler_s *(*client_handler_create)(struct us_allocator_s *allocator), _Bool (*client_handler_init)(struct us_reactor_handler_s *self, struct us_allocator_s *allocator, int fd, void *extra, long queue_buf_size, char *client_host, char *client_port, _Bool keep_open))
{
    _Bool r = 0;
    struct us_reactor_handler_tcp_client_s *handler = (us_reactor_handler_tcp_client_t *)client_handler_create( allocator );
    if ( handler )
    {
        r = client_handler_init(
            &handler->m_base.m_base, allocator, -1, extra, queue_buf_size, server_host, server_port, keep_open );
        if ( r )
        {
            r = self->add_item( self, &handler->m_base.m_base );
        }
    }
    return r;
}

variable_types: struct us_reactor_handler_s *(*)(struct us_allocator_s *allocator)

variable_types: _Bool (*)(struct us_reactor_handler_s *self, struct us_allocator_s *allocator, int fd, void *extra, long queue_buf_size, char *client_host, char *client_port, _Bool keep_open)

variable_types: struct us_reactor_handler_tcp_client_s { struct us_reactor_handler_tcp_s { struct us_reactor_handler_s { void (*destroy)(struct us_reactor_handler_s *self); void (*close)(struct us_reactor_handler_s *self); _Bool (*tick)(struct us_reactor_handler_s *self); _Bool (*readable)(struct us_reactor_handler_s *self); _Bool (*writable)(struct us_reactor_handler_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; _Bool m_finished; struct us_reactor_handler_s *m_next; struct us_reactor_s { void (*destroy)(struct us_reactor_s *self); struct us_allocator_s *m_allocator; struct us_reactor_handler_s *m_handlers; int m_timeout; int m_max_handlers; int m_num_handlers; struct pollfd { int fd; short events; short revents; } *m_poll_handlers; _Bool (*poll)(struct us_reactor_s *self, int timeout); _Bool (*add_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); _Bool (*remove_item)(struct us_reactor_s *self, struct us_reactor_handler_s *item); } *m_reactor; void *m_extra; int m_fd; _Bool m_wake_on_readable; _Bool m_wake_on_writable; } m_base; struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } m_outgoing_queue; struct us_buffer_s m_incoming_queue; void (*close)(struct us_reactor_handler_tcp_s *self); _Bool (*connected)(struct us_reactor_handler_tcp_s *self, struct sockaddr { unsigned short sa_family; char sa_data[14]; } *addr, unsigned int addrlen); _Bool (*tick)(struct us_reactor_handler_tcp_s *self); _Bool (*readable)(struct us_reactor_handler_tcp_s *self); _Bool (*writable)(struct us_reactor_handler_tcp_s *self); _Bool (*incoming_eof)(struct us_reactor_handler_tcp_s *self); void (*closed)(struct us_reactor_handler_tcp_s *self); } m_base; char *m_client_host; char *m_client_port; _Bool m_keep_open; _Bool m_is_connected; _Bool m_try_once; } *

user_defined_types: struct us_reactor_handler_tcp_client_s { struct us_reactor_handler_tcp_s m_base; char *m_client_host; char *m_client_port; _Bool m_keep_open; _Bool m_is_connected; _Bool m_try_once; }

user_defined_types: struct us_reactor_handler_tcp_s { struct us_reactor_handler_s m_base; struct us_buffer_s m_outgoing_queue; struct us_buffer_s m_incoming_queue; void (*close)(struct us_reactor_handler_tcp_s *self); _Bool (*connected)(struct us_reactor_handler_tcp_s *self, struct sockaddr *addr, unsigned int addrlen); _Bool (*tick)(struct us_reactor_handler_tcp_s *self); _Bool (*readable)(struct us_reactor_handler_tcp_s *self); _Bool (*writable)(struct us_reactor_handler_tcp_s *self); _Bool (*incoming_eof)(struct us_reactor_handler_tcp_s *self); void (*closed)(struct us_reactor_handler_tcp_s *self); }


decompiled_code:
__int64 __fastcall us_reactor_create_server(__int64 a1, __int64 a2, const char *a3, const char *a4, int a5, __int64 a6, __int64 (__fastcall *a7)(__int64), unsigned __int8 (__fastcall *a8)(__int64, __int64, _QWORD, __int64))
{
const char *v10; // rdi
const char *v11; // rsi
int v13; // eax
struct addrinfo *v14; // rbp
unsigned int v15; // edx
void (*v16)(const char *, ...); // rbx
char *v17; // rax
__int64 v18; // r12
void (*v19)(const char *, ...); // rbx
int *v20; // rax
char *v21; // rax
int v22; // edi
int v23; // eax
unsigned int v24; // ebx
int v25; // edi
void (*v27)(const char *, ...); // rbx
char *v28; // rax
void (*v29)(const char *, ...); // rbx
const char *v30; // rax
int optval; // [rsp+1Ch] [rbp-57Ch] BYREF
char serv[256]; // [rsp+20h] [rbp-578h] BYREF
char host[1024]; // [rsp+120h] [rbp-478h] BYREF
struct addrinfo req; // [rsp+520h] [rbp-78h] BYREF
struct addrinfo *pai; // [rsp+558h] [rbp-40h] BYREF

v10 = a3;
v11 = a4;
*(_QWORD *)&req.ai_flags = 33LL;
req.ai_protocol = 0;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_socktype = a5;
if ( a3 && !*a3 )
v10 = 0LL;
if ( a4 && !*a4 )
v11 = 0LL;
v13 = getaddrinfo(v10, v11, &req, &pai);
if ( v13 )
{
v15 = 0;
if ( us_log_level > 0 )
{
v29 = (void (*)(const char *, ...))us_log_error_proc;
v30 = gai_strerror(v13);
v29("getaddrinfo: %s", v30);
v15 = 0;
}
return v15;
}
v14 = pai;
if ( !pai )
{
LABEL_9:
freeaddrinfo(pai);
return 1;
}
while ( 1 )
{
optval = 1;
while ( 1 )
{
v23 = socket(v14->ai_family, v14->ai_socktype, v14->ai_protocol);
v24 = v23;
if ( v23 != -1 )
break;
v25 = *_errno_location();
if ( v25 != 4 )
{
if ( us_log_level > 0 )
{
v16 = (void (*)(const char *, ...))us_log_error_proc;
v17 = strerror(v25);
v16("socket: %s\n", v17);
}
goto LABEL_37;
}
}
setsockopt(v23, 1, 2, &optval, 4u);
if ( bind(v24, v14->ai_addr, v14->ai_addrlen) )
break;
v18 = a7(a2);
if ( !v18 )
{
if ( us_log_level > 0 )
us_log_error_proc("item create failed");
goto LABEL_37;
}
if ( a5 == 1 && listen(v24, 128) )
{
if ( us_log_level > 0 )
{
v19 = (void (*)(const char *, ...))us_log_error_proc;
v20 = _errno_location();
v21 = strerror(*v20);
v19("listen: %s", v21);
}
goto LABEL_37;
}
if ( !a8(v18, a2, v24, a6) )
{
if ( us_log_level > 0 )
us_log_error_proc("unable to init server item");
goto LABEL_37;
}
if ( !(*(unsigned __int8 (__fastcall **)(__int64, __int64))(a1 + 56))(a1, v18) )
{
if ( us_log_level > 0 )
us_log_error_proc("unable to add item to reactor");
goto LABEL_37;
}
getnameinfo(v14->ai_addr, v14->ai_addrlen, host, 0x400u, serv, 0x100u, 3);
LABEL_31:
v14 = v14->ai_next;
if ( !v14 )
goto LABEL_9;
}
v22 = *_errno_location();
if ( v22 == 98 )
{
closesocket(v24);
goto LABEL_31;
}
if ( us_log_level > 0 )
{
v27 = (void (*)(const char *, ...))us_log_error_proc;
v28 = strerror(v22);
v27("bind: %s", v28);
}
LABEL_37:
freeaddrinfo(pai);
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2, const char *a3, const char *a4, int a5, __int64 a6, __int64 (__fastcall *a7)(__int64), unsigned __int8 (__fastcall *a8)(__int64, __int64, _QWORD, __int64))
{
const char *v10; // rdi
const char *v11; // rsi
int v13; // eax
struct addrinfo *v14; // rbp
unsigned int v15; // edx
void (*v16)(const char *, ...); // rbx
char *v17; // rax
__int64 v18; // r12
void (*v19)(const char *, ...); // rbx
int *v20; // rax
char *v21; // rax
int v22; // edi
int v23; // eax
unsigned int v24; // ebx
int v25; // edi
void (*v27)(const char *, ...); // rbx
char *v28; // rax
void (*v29)(const char *, ...); // rbx
const char *v30; // rax
int optval; // [rsp+1Ch] [rbp-57Ch] BYREF
char serv[256]; // [rsp+20h] [rbp-578h] BYREF
char host[1024]; // [rsp+120h] [rbp-478h] BYREF
struct addrinfo req; // [rsp+520h] [rbp-78h] BYREF
struct addrinfo *pai; // [rsp+558h] [rbp-40h] BYREF

v10 = a3;
v11 = a4;
*(_QWORD *)&req.ai_flags = 33LL;
req.ai_protocol = 0;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_socktype = a5;
if ( a3 && !*a3 )
v10 = 0LL;
if ( a4 && !*a4 )
v11 = 0LL;
v13 = getaddrinfo(v10, v11, &req, &pai);
if ( v13 )
{
v15 = 0;
if ( us_log_level > 0 )
{
v29 = (void (*)(const char *, ...))us_log_error_proc;
v30 = gai_strerror(v13);
v29("getaddrinfo: %s", v30);
v15 = 0;
}
return v15;
}
v14 = pai;
if ( !pai )
{
LABEL_9:
freeaddrinfo(pai);
return 1;
}
while ( 1 )
{
optval = 1;
while ( 1 )
{
v23 = socket(v14->ai_family, v14->ai_socktype, v14->ai_protocol);
v24 = v23;
if ( v23 != -1 )
break;
v25 = *_errno_location();
if ( v25 != 4 )
{
if ( us_log_level > 0 )
{
v16 = (void (*)(const char *, ...))us_log_error_proc;
v17 = strerror(v25);
v16("socket: %s\n", v17);
}
goto LABEL_37;
}
}
setsockopt(v23, 1, 2, &optval, 4u);
if ( bind(v24, v14->ai_addr, v14->ai_addrlen) )
break;
v18 = a7(a2);
if ( !v18 )
{
if ( us_log_level > 0 )
us_log_error_proc("item create failed");
goto LABEL_37;
}
if ( a5 == 1 && listen(v24, 128) )
{
if ( us_log_level > 0 )
{
v19 = (void (*)(const char *, ...))us_log_error_proc;
v20 = _errno_location();
v21 = strerror(*v20);
v19("listen: %s", v21);
}
goto LABEL_37;
}
if ( !a8(v18, a2, v24, a6) )
{
if ( us_log_level > 0 )
us_log_error_proc("unable to init server item");
goto LABEL_37;
}
if ( !(*(unsigned __int8 (__fastcall **)(__int64, __int64))(a1 + 56))(a1, v18) )
{
if ( us_log_level > 0 )
us_log_error_proc("unable to add item to reactor");
goto LABEL_37;
}
getnameinfo(v14->ai_addr, v14->ai_addrlen, host, 0x400u, serv, 0x100u, 3);
LABEL_31:
v14 = v14->ai_next;
if ( !v14 )
goto LABEL_9;
}
v22 = *_errno_location();
if ( v22 == 98 )
{
func5(v24);
goto LABEL_31;
}
if ( us_log_level > 0 )
{
v27 = (void (*)(const char *, ...))us_log_error_proc;
v28 = strerror(v22);
v27("bind: %s", v28);
}
LABEL_37:
freeaddrinfo(pai);
return 0;
}

original_code:
_Bool us_reactor_create_server( us_reactor_t *self,
                               us_allocator_t *allocator,
                               const char *server_host,
                               const char *server_port,
                               int ai_socktype,
                               void *client_extra,
                               us_reactor_handler_create_proc_t server_handler_create,
                               us_reactor_handler_init_proc_t server_handler_init )
{
    int added_count = 0;
    _Bool r = 1;
    int e;
    struct addrinfo *ai;
    struct addrinfo hints;
    memset( &hints, '\0', sizeof( hints ) );
    hints.ai_flags = 0x0001 | 0x0020;
    hints.ai_socktype = ai_socktype;
    if ( server_host != 0 && *server_host == '\0' )
        server_host = 0;
    if ( server_port != 0 && *server_port == '\0' )
        server_port = 0;
    e = getaddrinfo( server_host, server_port, &hints, &ai );
    if ( e == 0 )
    {
        struct addrinfo *cur_addr = ai;
        while ( cur_addr != ((void *)0) )
        {
            const int opt = 1;
            int fd;
            do
            {
                fd = socket( cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol );
            } while ( fd == -1 && (*__errno_location ()) == 4 );
            if ( fd == -1 )
            {
                do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "socket: %s\n", strerror( (*__errno_location ()) ) ); } while ( 0 );
                freeaddrinfo( ai );
                return 0;
            }
            setsockopt( fd, 1, 2, (const char *)&opt, sizeof( opt ) );
            if ( bind( fd, cur_addr->ai_addr, cur_addr->ai_addrlen ) == 0 )
            {
                us_reactor_handler_t *item;
                item = server_handler_create( allocator );
                if ( !item )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "item create failed" ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
                if ( ai_socktype == SOCK_STREAM )
                {
                    if ( listen( fd, 128 ) != 0 )
                    {
                        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "listen: %s", strerror( (*__errno_location ()) ) ); } while ( 0 );
                        freeaddrinfo( ai );
                        return 0;
                    }
                }
                if ( server_handler_init( item, allocator, fd, client_extra ) )
                {
                    if ( self->add_item( self, item ) )
                    {
                        char host_buf[1024];
                        char port_buf[256];
                        int e1;
                        e1 = getnameinfo( cur_addr->ai_addr,
                                          cur_addr->ai_addrlen,
                                          host_buf,
                                          sizeof( host_buf ),
                                          port_buf,
                                          sizeof( port_buf ),
                                          1 | 2 );
                        if ( e1 == 0 )
                        {
                            do { } while ( 0 );
                        }
                        else
                        {
                            do { } while ( 0 );
                        }
                        added_count++;
                    }
                    else
                    {
                        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "unable to add item to reactor" ); } while ( 0 );
                        freeaddrinfo( ai );
                        return 0;
                    }
                }
                else
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "unable to init server item" ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
            }
            else
            {
                if ( (*__errno_location ()) != 98 )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "bind: %s", strerror( (*__errno_location ()) ) ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
                closesocket( fd );
            }
            cur_addr = cur_addr->ai_next;
        }
        freeaddrinfo( ai );
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "getaddrinfo: %s", gai_strerror( e ) ); } while ( 0 );
        return 0;
    }
    if ( added_count > 0 )
    {
        r = 1;
    }
    return r;
}

canonical_original_code:
_Bool us_reactor_create_server(struct us_reactor_s *self, struct us_allocator_s *allocator, char *server_host, char *server_port, int ai_socktype, void *client_extra, struct us_reactor_handler_s *(*server_handler_create)(struct us_allocator_s *allocator), _Bool (*server_handler_init)(struct us_reactor_handler_s *self, struct us_allocator_s *allocator, int fd, void *extra))
{
    int added_count = 0;
    _Bool r = 1;
    int e;
    struct addrinfo *ai;
    struct addrinfo hints;
    memset( &hints, '\0', sizeof( hints ) );
    hints.ai_flags = 0x0001 | 0x0020;
    hints.ai_socktype = ai_socktype;
    if ( server_host != 0 && *server_host == '\0' )
        server_host = 0;
    if ( server_port != 0 && *server_port == '\0' )
        server_port = 0;
    e = getaddrinfo( server_host, server_port, &hints, &ai );
    if ( e == 0 )
    {
        struct addrinfo *cur_addr = ai;
        while ( cur_addr != ((void *)0) )
        {
            int opt = 1;
            int fd;
            do
            {
                fd = socket( cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol );
            } while ( fd == -1 && (*__errno_location ()) == 4 );
            if ( fd == -1 )
            {
                do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "socket: %s\n", strerror( (*__errno_location ()) ) ); } while ( 0 );
                freeaddrinfo( ai );
                return 0;
            }
            setsockopt( fd, 1, 2, (char *)&opt, sizeof( opt ) );
            if ( bind( fd, cur_addr->ai_addr, cur_addr->ai_addrlen ) == 0 )
            {
                struct us_reactor_handler_s *item;
                item = server_handler_create( allocator );
                if ( !item )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "item create failed" ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
                if ( ai_socktype == SOCK_STREAM )
                {
                    if ( listen( fd, 128 ) != 0 )
                    {
                        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "listen: %s", strerror( (*__errno_location ()) ) ); } while ( 0 );
                        freeaddrinfo( ai );
                        return 0;
                    }
                }
                if ( server_handler_init( item, allocator, fd, client_extra ) )
                {
                    if ( self->add_item( self, item ) )
                    {
                        char host_buf[1024];
                        char port_buf[256];
                        int e1;
                        e1 = getnameinfo( cur_addr->ai_addr,
                                          cur_addr->ai_addrlen,
                                          host_buf,
                                          sizeof( host_buf ),
                                          port_buf,
                                          sizeof( port_buf ),
                                          1 | 2 );
                        if ( e1 == 0 )
                        {
                            do { } while ( 0 );
                        }
                        else
                        {
                            do { } while ( 0 );
                        }
                        added_count++;
                    }
                    else
                    {
                        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "unable to add item to reactor" ); } while ( 0 );
                        freeaddrinfo( ai );
                        return 0;
                    }
                }
                else
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "unable to init server item" ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
            }
            else
            {
                if ( (*__errno_location ()) != 98 )
                {
                    do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "bind: %s", strerror( (*__errno_location ()) ) ); } while ( 0 );
                    freeaddrinfo( ai );
                    return 0;
                }
                closesocket( fd );
            }
            cur_addr = cur_addr->ai_next;
        }
        freeaddrinfo( ai );
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "getaddrinfo: %s", gai_strerror( e ) ); } while ( 0 );
        return 0;
    }
    if ( added_count > 0 )
    {
        r = 1;
    }
    return r;
}

variable_types: _Bool (*)(struct us_reactor_handler_s *self, struct us_allocator_s *allocator, int fd, void *extra)

variable_types: e int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: opt int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
bool __fastcall us_buffer_can_write_byte(_QWORD *a1)
{
return (unsigned __int64)(a1[3] + 1LL) % a1[5] != a1[4];
}

canonical_decompiled_code:
bool __fastcall func0(_QWORD *a1)
{
return (unsigned __int64)(a1[3] + 1LL) % a1[5] != a1[4];
}

original_code:
static inline _Bool us_buffer_can_write_byte( const us_buffer_t *self )
{
    return ( ( self->m_next_in + 1 ) % self->m_max_length ) != self->m_next_out;
}

canonical_original_code:
static inline _Bool us_buffer_can_write_byte(struct us_buffer_s *self)
{
    return ( ( self->m_next_in + 1 ) % self->m_max_length ) != self->m_next_out;
}

variable_types: struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *

user_defined_types: struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; }


decompiled_code:
__int64 __fastcall us_midi_get_message_length(unsigned __int8 a1)
{
return (unsigned __int8)us_midi_lut_msglen[a1 >> 4];
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
return (unsigned __int8)us_midi_lut_msglen[a1 >> 4];
}

original_code:
static inline int8_t us_midi_get_message_length( uint8_t status )
{
    return us_midi_lut_msglen[status >> 4];
}

canonical_original_code:
static inline char us_midi_get_message_length(unsigned char status)
{
    return us_midi_lut_msglen[status >> 4];
}

variable_types:
status
unsigned char

return_type: char


decompiled_code:
__int64 __fastcall us_midi_get_system_message_length(unsigned __int8 a1)
{
return (unsigned __int8)us_midi_lut_sysmsglen[a1 - 240];
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
return (unsigned __int8)us_midi_lut_sysmsglen[a1 - 240];
}

original_code:
static inline int8_t us_midi_get_system_message_length( uint8_t status )
{
    return us_midi_lut_sysmsglen[status - 0xf0];
}

canonical_original_code:
static inline char us_midi_get_system_message_length(unsigned char status)
{
    return us_midi_lut_sysmsglen[status - 0xf0];
}


decompiled_code:
void __fastcall us_midi_msg_set_byte1(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 1) = a2;
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 1) = a2;
}

original_code:
static inline void us_midi_msg_set_byte1( us_midi_msg_t *self, uint8_t b )
{
    self->byte1 = b;
}

canonical_original_code:
static inline void us_midi_msg_set_byte1(struct us_midi_msg_s *self, unsigned char b)
{
    self->byte1 = b;
}

variable_types: struct us_midi_msg_s { unsigned char status; unsigned char byte1; unsigned char byte2; unsigned char byte3; } *

variable_types:
b
unsigned char


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

canonical_decompiled_code:
void __fastcall func9(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 2) = a2;
}

original_code:
static inline void us_midi_msg_set_byte2( us_midi_msg_t *self, uint8_t b )
{
    self->byte2 = b;
}

canonical_original_code:
static inline void us_midi_msg_set_byte2(struct us_midi_msg_s *self, unsigned char b)
{
    self->byte2 = b;
}

user_defined_types: struct us_midi_msg_s { unsigned char status; unsigned char byte1; unsigned char byte2; unsigned char byte3; }


decompiled_code:
_BOOL8 __fastcall us_midi_msg_is_all_notes_off(_BYTE *a1)
{
bool v1; // al

v1 = 0;
if ( (*a1 & 0xF0) == 0xB0 )
v1 = a1[1] > 0x79u;
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(_BYTE *a1)
{
bool v1; // al

v1 = 0;
if ( (*a1 & 0xF0) == 0xB0 )
v1 = a1[1] > 0x79u;
return v1;
}

original_code:
static inline _Bool us_midi_msg_is_all_notes_off( const us_midi_msg_t *self )
{
    return ( ( self->status & 0xf0 ) == US_MIDI_CONTROL_CHANGE ) && ( self->byte1 >= US_MIDI_C_ALL_NOTES_OFF );
}

canonical_original_code:
static inline _Bool us_midi_msg_is_all_notes_off(struct us_midi_msg_s *self)
{
    return ( ( self->status & 0xf0 ) == US_MIDI_CONTROL_CHANGE ) && ( self->byte1 >= US_MIDI_C_ALL_NOTES_OFF );
}


decompiled_code:
__int64 __fastcall us_midi_sysex_start(__int64 a1)
{
us_midi_sysex_clear(a1);
return us_midi_sysex_put_byte(a1, 240);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
func16(a1);
return func12(a1, 240);
}

original_code:
static inline void us_midi_sysex_start( us_midi_sysex_t *self )
{
    us_midi_sysex_clear( self );
    us_midi_sysex_put_byte( self, US_MIDI_SYSEX_START );
}

canonical_original_code:
static inline void us_midi_sysex_start(struct us_midi_sysex_s *self)
{
    us_midi_sysex_clear( self );
    us_midi_sysex_put_byte( self, US_MIDI_SYSEX_START );
}

variable_types: struct us_midi_sysex_s { void (*destroy)(struct us_midi_sysex_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *m_buffer; } *

user_defined_types: struct us_midi_sysex_s { void (*destroy)(struct us_midi_sysex_s *self); struct us_allocator_s *m_allocator; struct us_buffer_s *m_buffer; }


decompiled_code:
__int64 __fastcall us_midi_msg_get_length(unsigned __int8 *a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = *a1;
if ( (v1 & 0xF0) == 0xF0 )
result = us_midi_get_system_message_length(v1);
else
result = us_midi_get_message_length(v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int8 *a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = *a1;
if ( (v1 & 0xF0) == 0xF0 )
result = func4(v1);
else
result = func3(v1);
return result;
}

original_code:
int8_t us_midi_msg_get_length( const us_midi_msg_t *self )
{
    if ( ( self->status & 0xf0 ) == 0xf0 )
    {
        return us_midi_get_system_message_length( self->status );
    }
    else
    {
        return us_midi_get_message_length( self->status );
    }
}

canonical_original_code:
char us_midi_msg_get_length(struct us_midi_msg_s *self)
{
    if ( ( self->status & 0xf0 ) == 0xf0 )
    {
        return us_midi_get_system_message_length( self->status );
    }
    else
    {
        return us_midi_get_message_length( self->status );
    }
}


decompiled_code:
char *__fastcall us_midi_msg_to_text(unsigned __int8 *a1, char *a2, int a3)
{
char v4; // al
char v5; // r13
unsigned __int8 v6; // al
int v7; // er12
unsigned __int8 v8; // al
unsigned __int8 v9; // r12
int v10; // eax
char *v11; // r14
int v12; // er13
char v13; // al
unsigned __int8 v14; // al
int v15; // edx
char *v16; // rax
char *v17; // rdx
__int16 v19; // ax
unsigned __int8 v20; // r8
char v21[104]; // [rsp+0h] [rbp-68h] BYREF

v4 = us_midi_msg_get_length(a1);
if ( a3 <= 63 )
return 0LL;
v5 = v4;
*a2 = 0;
if ( !us_midi_msg_is_all_notes_off(a1) )
{
v6 = *a1 >> 4;
v7 = v6;
if ( v6 == 9 )
{
if ( !a1[2] )
v7 = 8;
}
else if ( v6 == 15 )
{
goto LABEL_6;
}
v8 = us_midi_msg_get_channel(a1);
snprintf(v21, 0x40uLL, "Ch %2d ", (unsigned int)v8 + 1);
strcat(a2, v21);
LABEL_6:
strcat(a2, (&us_midi_chan_msg_name)[v7]);
v9 = *a1;
if ( *a1 > 0xEFu )
{
strcat(a2, (&us_midi_sys_msg_name)[v9 - 240]);
if ( v5 > 1 )
{
snprintf(v21, 0x40uLL, "%02x", a1[1]);
strcat(a2, v21);
}
if ( v5 > 2 )
{
snprintf(v21, 0x40uLL, ",%02x", a1[2]);
strcat(a2, v21);
}
if ( v5 > 3 )
{
snprintf(v21, 0x40uLL, ",%02x", a1[3]);
strcat(a2, v21);
}
}
else
{
v10 = strlen(a2);
v11 = &a2[v10];
v12 = 64 - v10;
v13 = v9 & 0xF0;
if ( (v9 & 0xF0) == 0xB0 )
{
snprintf(v11, v12, "Ctrl %3d Val %3d ", a1[1], a1[2]);
}
else if ( (v9 & 0xF0u) > 0xB0 )
{
switch ( v13 )
{
case -48:
snprintf(v11, v12, "Pres %3d ", a1[1]);
break;
case -32:
v19 = us_midi_msg_get_bender_value((__int64)a1);
snprintf(v11, v12, "Val %5d", (unsigned int)v19);
break;
case -64:
snprintf(v11, v12, "PG %3d ", a1[1]);
break;
}
}
else
{
switch ( v13 )
{
case -112:
v20 = a1[2];
if ( v20 )
snprintf(v11, v12, "Note %3d Vel %3d ", a1[1], v20);
else
snprintf(v11, v12, "Note %3d", a1[1]);
break;
case -96:
snprintf(v11, v12, "Note %3d Pres %3d ", a1[1], a1[2]);
break;
case -128:
snprintf(v11, v12, "Note %3d Vel %3d ", a1[1], a1[2]);
break;
}
}
}
goto LABEL_13;
}
v14 = us_midi_msg_get_channel(a1);
snprintf(v21, 0x40uLL, "Ch %2d All Notes Off (ctrl=%3d)", (unsigned int)v14 + 1, a1[1]);
strcat(a2, v21);
LABEL_13:
v15 = strlen(a2);
v16 = &a2[v15];
if ( v15 > 44 )
{
v17 = &a2[v15];
}
else
{
v17 = &v16[44 - v15 + 1];
do
*v16++ = 32;
while ( v16 != v17 );
}
*v17 = 0;
return a2;
}

canonical_decompiled_code:
char *__fastcall func19(unsigned __int8 *a1, char *a2, int a3)
{
char v4; // al
char v5; // r13
unsigned __int8 v6; // al
int v7; // er12
unsigned __int8 v8; // al
unsigned __int8 v9; // r12
int v10; // eax
char *v11; // r14
int v12; // er13
char v13; // al
unsigned __int8 v14; // al
int v15; // edx
char *v16; // rax
char *v17; // rdx
__int16 v19; // ax
unsigned __int8 v20; // r8
char v21[104]; // [rsp+0h] [rbp-68h] BYREF

v4 = func18(a1);
if ( a3 <= 63 )
return 0LL;
v5 = v4;
*a2 = 0;
if ( !func11(a1) )
{
v6 = *a1 >> 4;
v7 = v6;
if ( v6 == 9 )
{
if ( !a1[2] )
v7 = 8;
}
else if ( v6 == 15 )
{
goto LABEL_6;
}
v8 = func6(a1);
snprintf(v21, 0x40uLL, "Ch %2d ", (unsigned int)v8 + 1);
strcat(a2, v21);
LABEL_6:
strcat(a2, (&us_midi_chan_msg_name)[v7]);
v9 = *a1;
if ( *a1 > 0xEFu )
{
strcat(a2, (&us_midi_sys_msg_name)[v9 - 240]);
if ( v5 > 1 )
{
snprintf(v21, 0x40uLL, "%02x", a1[1]);
strcat(a2, v21);
}
if ( v5 > 2 )
{
snprintf(v21, 0x40uLL, ",%02x", a1[2]);
strcat(a2, v21);
}
if ( v5 > 3 )
{
snprintf(v21, 0x40uLL, ",%02x", a1[3]);
strcat(a2, v21);
}
}
else
{
v10 = strlen(a2);
v11 = &a2[v10];
v12 = 64 - v10;
v13 = v9 & 0xF0;
if ( (v9 & 0xF0) == 0xB0 )
{
snprintf(v11, v12, "Ctrl %3d Val %3d ", a1[1], a1[2]);
}
else if ( (v9 & 0xF0u) > 0xB0 )
{
switch ( v13 )
{
case -48:
snprintf(v11, v12, "Pres %3d ", a1[1]);
break;
case -32:
v19 = func10((__int64)a1);
snprintf(v11, v12, "Val %5d", (unsigned int)v19);
break;
case -64:
snprintf(v11, v12, "PG %3d ", a1[1]);
break;
}
}
else
{
switch ( v13 )
{
case -112:
v20 = a1[2];
if ( v20 )
snprintf(v11, v12, "Note %3d Vel %3d ", a1[1], v20);
else
snprintf(v11, v12, "Note %3d", a1[1]);
break;
case -96:
snprintf(v11, v12, "Note %3d Pres %3d ", a1[1], a1[2]);
break;
case -128:
snprintf(v11, v12, "Note %3d Vel %3d ", a1[1], a1[2]);
break;
}
}
}
goto LABEL_13;
}
v14 = func6(a1);
snprintf(v21, 0x40uLL, "Ch %2d All Notes Off (ctrl=%3d)", (unsigned int)v14 + 1, a1[1]);
strcat(a2, v21);
LABEL_13:
v15 = strlen(a2);
v16 = &a2[v15];
if ( v15 > 44 )
{
v17 = &a2[v15];
}
else
{
v17 = &v16[44 - v15 + 1];
do
*v16++ = 32;
while ( v16 != v17 );
}
*v17 = 0;
return a2;
}

original_code:
const char *us_midi_msg_to_text( const us_midi_msg_t *self, char *txt, int max_len )
{
    char buf[64];
    int buf_len = sizeof( buf );
    int len = us_midi_msg_get_length( self );
    if ( max_len < 64 )
    {
        return 0;
    }
    *txt = 0;
    if ( us_midi_msg_is_all_notes_off( self ) )
    {
        snprintf(
            buf, buf_len, "Ch %2d  All Notes Off  (ctrl=%3d)", (int)us_midi_msg_get_channel( self ) + 1, (int)self->byte1 );
        strcat( txt, buf );
    }
    else
    {
        int type = ( self->status & 0xf0 ) >> 4;
        if ( type == 9 && self->byte2 == 0 )
            type = 8;
        if ( type != 0xf )
        {
            snprintf( buf, buf_len, "Ch %2d  ", (int)us_midi_msg_get_channel( self ) + 1 );
            strcat( txt, buf );
        }
        strcat( txt, us_midi_chan_msg_name[type] );
        if ( self->status >= 0xf0 )
        {
            strcat( txt, us_midi_sys_msg_name[self->status - 0xf0] );
            if ( len > 1 )
            {
                snprintf( buf, buf_len, "%02x", (int)self->byte1 );
                strcat( txt, buf );
            }
            if ( len > 2 )
            {
                snprintf( buf, buf_len, ",%02x", (int)self->byte2 );
                strcat( txt, buf );
            }
            if ( len > 3 )
            {
                snprintf( buf, buf_len, ",%02x", (int)self->byte3 );
                strcat( txt, buf );
            }
        }
        else
        {
            int32_t l = (int32_t)strlen( txt );
            char *endtxt = txt + l;
            int32_t mx = 64 - l;
            switch ( self->status & 0xf0 )
            {
            case US_MIDI_NOTE_ON:
                if ( self->byte2 == 0 )
                    snprintf( endtxt, mx, "Note %3d", (int)self->byte1 );
                else
                    snprintf( endtxt, mx, "Note %3d  Vel  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_NOTE_OFF:
                snprintf( endtxt, mx, "Note %3d  Vel  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_POLY_PRESSURE:
                snprintf( endtxt, mx, "Note %3d  Pres %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_CONTROL_CHANGE:
                snprintf( endtxt, mx, "Ctrl %3d  Val  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_PROGRAM_CHANGE:
                snprintf( endtxt, mx, "PG   %3d  ", (int)self->byte1 );
                break;
            case US_MIDI_CHANNEL_PRESSURE:
                snprintf( endtxt, mx, "Pres %3d  ", (int)self->byte1 );
                break;
            case US_MIDI_PITCH_BEND:
                snprintf( endtxt, mx, "Val %5d", (int)us_midi_msg_get_bender_value( self ) );
                break;
            }
        }
    }
    {
        int32_t l = (int32_t)strlen( txt );
        char *p = txt + l;
        while ( l < 45 )
        {
            *p++ = ' ';
            ++l;
        }
        *p = '\0';
    }
    return txt;
}

canonical_original_code:
char *us_midi_msg_to_text(struct us_midi_msg_s *self, char *txt, int max_len)
{
    char buf[64];
    int buf_len = sizeof( buf );
    int len = us_midi_msg_get_length( self );
    if ( max_len < 64 )
    {
        return 0;
    }
    *txt = 0;
    if ( us_midi_msg_is_all_notes_off( self ) )
    {
        snprintf(
            buf, buf_len, "Ch %2d  All Notes Off  (ctrl=%3d)", (int)us_midi_msg_get_channel( self ) + 1, (int)self->byte1 );
        strcat( txt, buf );
    }
    else
    {
        int type = ( self->status & 0xf0 ) >> 4;
        if ( type == 9 && self->byte2 == 0 )
            type = 8;
        if ( type != 0xf )
        {
            snprintf( buf, buf_len, "Ch %2d  ", (int)us_midi_msg_get_channel( self ) + 1 );
            strcat( txt, buf );
        }
        strcat( txt, us_midi_chan_msg_name[type] );
        if ( self->status >= 0xf0 )
        {
            strcat( txt, us_midi_sys_msg_name[self->status - 0xf0] );
            if ( len > 1 )
            {
                snprintf( buf, buf_len, "%02x", (int)self->byte1 );
                strcat( txt, buf );
            }
            if ( len > 2 )
            {
                snprintf( buf, buf_len, ",%02x", (int)self->byte2 );
                strcat( txt, buf );
            }
            if ( len > 3 )
            {
                snprintf( buf, buf_len, ",%02x", (int)self->byte3 );
                strcat( txt, buf );
            }
        }
        else
        {
            int l = (int32_t)strlen( txt );
            char *endtxt = txt + l;
            int mx = 64 - l;
            switch ( self->status & 0xf0 )
            {
            case US_MIDI_NOTE_ON:
                if ( self->byte2 == 0 )
                    snprintf( endtxt, mx, "Note %3d", (int)self->byte1 );
                else
                    snprintf( endtxt, mx, "Note %3d  Vel  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_NOTE_OFF:
                snprintf( endtxt, mx, "Note %3d  Vel  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_POLY_PRESSURE:
                snprintf( endtxt, mx, "Note %3d  Pres %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_CONTROL_CHANGE:
                snprintf( endtxt, mx, "Ctrl %3d  Val  %3d  ", (int)self->byte1, (int)self->byte2 );
                break;
            case US_MIDI_PROGRAM_CHANGE:
                snprintf( endtxt, mx, "PG   %3d  ", (int)self->byte1 );
                break;
            case US_MIDI_CHANNEL_PRESSURE:
                snprintf( endtxt, mx, "Pres %3d  ", (int)self->byte1 );
                break;
            case US_MIDI_PITCH_BEND:
                snprintf( endtxt, mx, "Val %5d", (int)us_midi_msg_get_bender_value( self ) );
                break;
            }
        }
    }
    {
        int l = (int32_t)strlen( txt );
        char *p = txt + l;
        while ( l < 45 )
        {
            *p++ = ' ';
            ++l;
        }
        *p = '\0';
    }
    return txt;
}

variable_types:
txt
char *

variable_types:
buf
char [64]

variable_types: len int

variable_types: type int

variable_types: l int

variable_types:
endtxt
char *

variable_types: mx int

variable_types:
p
char *

return_type: char *


decompiled_code:
__int64 __fastcall us_midi_parser_create(__int64 a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rbx
__int64 v4; // rbp

v2 = (*(__int64 (__fastcall **)(__int64, __int64, __int64))(a1 + 8))(a1, 40LL, 1LL);
v3 = v2;
if ( v2 )
{
*(_QWORD *)v2 = us_midi_parser_destroy;
*(_QWORD *)(v2 + 8) = a1;
us_midi_msg_init((_BYTE *)(v2 + 16));
v4 = us_midi_sysex_create(a1, a2);
*(_QWORD *)(v3 + 24) = v4;
*(_DWORD *)(v3 + 32) = 0;
if ( !v4 )
{
us_midi_parser_destroy(v3);
v3 = 0LL;
}
}
return v3;
}

original_code:
us_midi_parser_t *us_midi_parser_create( us_allocator_t *allocator, int32_t max_sysex_size )
{
    us_midi_parser_t *r = 0;
    us_midi_parser_t *self = 0;
    self = ( us_midi_parser_t * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( us_midi_parser_t ), 1 ) );
    if ( self )
    {
        self->destroy = us_midi_parser_destroy;
        self->m_allocator = allocator;
        us_midi_msg_init( &self->m_tmp_msg );
        self->m_sysex = us_midi_sysex_create( allocator, max_sysex_size );
        self->m_state = US_MIDI_PARSER_STATE_FIND_STATUS;
        if ( self->m_sysex )
        {
            r = self;
        }
        if ( r == 0 )
        {
            us_midi_parser_destroy( self );
        }
    }
    return r;
}

canonical_original_code:
struct us_midi_parser_s *us_midi_parser_create(struct us_allocator_s *allocator, int max_sysex_size)
{
    struct us_midi_parser_s *r = 0;
    struct us_midi_parser_s *self = 0;
    self = ( struct us_midi_parser_s * )( ( allocator )->alloc( ( allocator ), (int)sizeof( us_midi_parser_t ), 1 ) );
    if ( self )
    {
        self->destroy = us_midi_parser_destroy;
        self->m_allocator = allocator;
        us_midi_msg_init( &self->m_tmp_msg );
        self->m_sysex = us_midi_sysex_create( allocator, max_sysex_size );
        self->m_state = US_MIDI_PARSER_STATE_FIND_STATUS;
        if ( self->m_sysex )
        {
            r = self;
        }
        if ( r == 0 )
        {
            us_midi_parser_destroy( self );
        }
    }
    return r;
}

variable_types:
allocator
struct us_allocator_s *

variable_types: struct us_midi_parser_s { void (*destroy)(struct us_midi_parser_s *self); struct us_allocator_s { void (*destroy)(struct us_allocator_s *self); void *(*alloc)(struct us_allocator_s *self, int length, int count); void *(*realloc)(struct us_allocator_s *self, void *orig_ptr, int length, int count); void (*free)(struct us_allocator_s *self, void *ptr); } *m_allocator; struct us_midi_msg_s { unsigned char status; unsigned char byte1; unsigned char byte2; unsigned char byte3; } m_tmp_msg; struct us_midi_sysex_s { void (*destroy)(struct us_midi_sysex_s *self); struct us_allocator_s *m_allocator; struct us_buffer_s { void (*destroy)(struct us_buffer_s *self); struct us_allocator_s *m_allocator; unsigned char *m_buffer; unsigned long m_next_in; unsigned long m_next_out; unsigned long m_max_length; } *m_buffer; } *m_sysex; enum us_midi_parser_state_t { US_MIDI_PARSER_STATE_FIND_STATUS=0, US_MIDI_PARSER_STATE_FIRST_OF_ONE=1, US_MIDI_PARSER_STATE_FIRST_OF_TWO=2, US_MIDI_PARSER_STATE_SECOND_OF_TWO=3, US_MIDI_PARSER_STATE_FIRST_OF_ONE_NORUN=4, US_MIDI_PARSER_STATE_SYSEX_DATA=5 } m_state; } *

user_defined_types: struct us_midi_parser_s { void (*destroy)(struct us_midi_parser_s *self); struct us_allocator_s *m_allocator; struct us_midi_msg_s m_tmp_msg; struct us_midi_sysex_s *m_sysex; enum us_midi_parser_state_t m_state; }