decompiled_code:
__int64 __fastcall us_serial_setup(int fd, char *s)
{
const char *v2; // r13
speed_t v3; // ebp
unsigned int v4; // er14
char *v5; // rax
char v6; // al
char v7; // al
char v8; // al
char v9; // al
tcflag_t v11; // eax
struct termios v12; // [rsp+0h] [rbp-68h] BYREF

do
{
if ( tcgetattr(fd, &v12) >= 0 )
{
v12.c_oflag &= 0xFFFFFFFE;
v2 = "8N1,N";
v12.c_lflag &= 0xFFFF7FF4;
*(_WORD *)&v12.c_cc[5] = 256;
v12.c_iflag = v12.c_iflag & 0xFFFFFACD | 5;
v12.c_cflag = v12.c_cflag & 0xFFFFFFCF | 0x880;
if ( !s )
goto LABEL_36;
v3 = 13;
if ( !*s )
goto LABEL_17;
v4 = strtoul(s, 0LL, 10);
if ( !v4 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid baud rate");
return 0LL;
}
v5 = strchr(s, 44);
if ( !v5 || (v2 = v5 + 1, strlen(v5 + 1) != 5) )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid serial format: for example use 8N1,H or 8N1,N ");
return 0LL;
}
if ( v4 != 31250 )
{
if ( v4 > 0x7A12 )
{
if ( v4 == 115200 )
{
v3 = 4098;
goto LABEL_17;
}
if ( v4 <= 0x1C200 )
{
if ( v4 == 38400 )
{
v3 = 15;
goto LABEL_17;
}
if ( v4 == 57600 )
{
v3 = 4097;
goto LABEL_17;
}
}
else
{
v3 = 4099;
if ( v4 == 230400 )
goto LABEL_17;
}
goto LABEL_62;
}
if ( v4 != 4800 )
{
if ( v4 <= 0x12C0 )
{
if ( v4 == 1200 )
{
v3 = 9;
goto LABEL_17;
}
if ( v4 == 2400 )
{
v3 = 11;
goto LABEL_17;
}
goto LABEL_62;
}
if ( v4 != 9600 )
{
if ( v4 == 19200 )
{
v3 = 14;
goto LABEL_17;
}
LABEL_62:
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid baud rate: %d", v4);
return 0LL;
}
LABEL_36:
v3 = 13;
goto LABEL_17;
}
v3 = 12;
LABEL_17:
while ( cfsetspeed(&v12, v3) < 0 )
{
if ( *_errno_location() != 4 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("error setting cfsetspeed");
return 0LL;
}
}
}
v6 = *v2;
if ( *v2 == 55 )
{
v12.c_cflag &= 0xFFFFFFCF;
}
else
{
if ( v6 != 56 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid bits: %c", (unsigned int)v6);
return 0LL;
}
v12.c_cflag |= 0x30u;
}
v7 = v2[1];
switch ( v7 )
{
case 'N':
v12.c_cflag &= 0xFFFFFEFF;
break;
case 'E':
v11 = v12.c_cflag;
BYTE1(v11) = BYTE1(v12.c_cflag) & 0xFD | 1;
v12.c_cflag = v11;
break;
case 'O':
v12.c_cflag |= 0x300u;
break;
default:
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid parity: %c", (unsigned int)v7);
return 0LL;
}
v8 = v2[2];
if ( v8 == 49 )
{
v12.c_cflag &= 0xFFFFFFBF;
}
else
{
if ( v8 != 50 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid stop bits: %c", (unsigned int)v8);
return 0LL;
}
v12.c_cflag |= 0x40u;
}
v9 = v2[4];
switch ( v9 )
{
case 'H':
v12.c_cflag |= 0x80000000;
break;
case 'N':
v12.c_cflag &= 0x7FFFFFFFu;
break;
case 'S':
v12.c_iflag |= 0x1C00u;
break;
}
do
{
if ( tcsetattr(fd, 0, &v12) >= 0 )
{
do
{
if ( fcntl(fd, 4, 2048LL) >= 0 )
return 1LL;
}
while ( *_errno_location() == 4 );
if ( us_log_level > 0 )
us_log_error_proc("fcntl F_SETFL O_NONBLOCK failed");
return 0LL;
}
}
while ( *_errno_location() == 4 );
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("ioctl TCSETS failed");
return 0LL;
}
}
while ( *_errno_location() == 4 );
if ( us_log_level > 0 )
{
us_log_error_proc("ioctl TCGETS failed");
return 0LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, char *s)
{
const char *v2; // r13
speed_t v3; // ebp
unsigned int v4; // er14
char *v5; // rax
char v6; // al
char v7; // al
char v8; // al
char v9; // al
tcflag_t v11; // eax
struct termios v12; // [rsp+0h] [rbp-68h] BYREF

do
{
if ( tcgetattr(fd, &v12) >= 0 )
{
v12.c_oflag &= 0xFFFFFFFE;
v2 = "8N1,N";
v12.c_lflag &= 0xFFFF7FF4;
*(_WORD *)&v12.c_cc[5] = 256;
v12.c_iflag = v12.c_iflag & 0xFFFFFACD | 5;
v12.c_cflag = v12.c_cflag & 0xFFFFFFCF | 0x880;
if ( !s )
goto LABEL_36;
v3 = 13;
if ( !*s )
goto LABEL_17;
v4 = strtoul(s, 0LL, 10);
if ( !v4 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid baud rate");
return 0LL;
}
v5 = strchr(s, 44);
if ( !v5 || (v2 = v5 + 1, strlen(v5 + 1) != 5) )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid serial format: for example use 8N1,H or 8N1,N ");
return 0LL;
}
if ( v4 != 31250 )
{
if ( v4 > 0x7A12 )
{
if ( v4 == 115200 )
{
v3 = 4098;
goto LABEL_17;
}
if ( v4 <= 0x1C200 )
{
if ( v4 == 38400 )
{
v3 = 15;
goto LABEL_17;
}
if ( v4 == 57600 )
{
v3 = 4097;
goto LABEL_17;
}
}
else
{
v3 = 4099;
if ( v4 == 230400 )
goto LABEL_17;
}
goto LABEL_62;
}
if ( v4 != 4800 )
{
if ( v4 <= 0x12C0 )
{
if ( v4 == 1200 )
{
v3 = 9;
goto LABEL_17;
}
if ( v4 == 2400 )
{
v3 = 11;
goto LABEL_17;
}
goto LABEL_62;
}
if ( v4 != 9600 )
{
if ( v4 == 19200 )
{
v3 = 14;
goto LABEL_17;
}
LABEL_62:
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid baud rate: %d", v4);
return 0LL;
}
LABEL_36:
v3 = 13;
goto LABEL_17;
}
v3 = 12;
LABEL_17:
while ( cfsetspeed(&v12, v3) < 0 )
{
if ( *_errno_location() != 4 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("error setting cfsetspeed");
return 0LL;
}
}
}
v6 = *v2;
if ( *v2 == 55 )
{
v12.c_cflag &= 0xFFFFFFCF;
}
else
{
if ( v6 != 56 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid bits: %c", (unsigned int)v6);
return 0LL;
}
v12.c_cflag |= 0x30u;
}
v7 = v2[1];
switch ( v7 )
{
case 'N':
v12.c_cflag &= 0xFFFFFEFF;
break;
case 'E':
v11 = v12.c_cflag;
BYTE1(v11) = BYTE1(v12.c_cflag) & 0xFD | 1;
v12.c_cflag = v11;
break;
case 'O':
v12.c_cflag |= 0x300u;
break;
default:
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid parity: %c", (unsigned int)v7);
return 0LL;
}
v8 = v2[2];
if ( v8 == 49 )
{
v12.c_cflag &= 0xFFFFFFBF;
}
else
{
if ( v8 != 50 )
{
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("invalid stop bits: %c", (unsigned int)v8);
return 0LL;
}
v12.c_cflag |= 0x40u;
}
v9 = v2[4];
switch ( v9 )
{
case 'H':
v12.c_cflag |= 0x80000000;
break;
case 'N':
v12.c_cflag &= 0x7FFFFFFFu;
break;
case 'S':
v12.c_iflag |= 0x1C00u;
break;
}
do
{
if ( tcsetattr(fd, 0, &v12) >= 0 )
{
do
{
if ( fcntl(fd, 4, 2048LL) >= 0 )
return 1LL;
}
while ( *_errno_location() == 4 );
if ( us_log_level > 0 )
us_log_error_proc("fcntl F_SETFL O_NONBLOCK failed");
return 0LL;
}
}
while ( *_errno_location() == 4 );
if ( us_log_level <= 0 )
return 0LL;
us_log_error_proc("ioctl TCSETS failed");
return 0LL;
}
}
while ( *_errno_location() == 4 );
if ( us_log_level > 0 )
{
us_log_error_proc("ioctl TCGETS failed");
return 0LL;
}
return 0LL;
}

original_code:
_Bool us_serial_setup( int fd, const char *serial_options )
{
    uint32_t requested_baud = 9600;
    uint32_t baud_code;
    const char *serial_fmt = 0;
    struct termios term;
    int e;
    do
    {
        e = tcgetattr( fd, &term );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "ioctl TCGETS failed" ); } while ( 0 );
        return 0;
    }
    term.c_cc[6] = 1;
    term.c_cc[5] = 0;
    term.c_iflag &= ~( 0000002 | 0000400 | 0000020 | 0000040 | 0002000 );
    term.c_iflag |= ( 0000001 | 0000004 );
    term.c_oflag &= ~0000001;
    term.c_lflag &= ~( 0000010 | 0000002 | 0100000 | 0000001 );
    term.c_cflag &= ~( 0000060 );
    term.c_cflag |= ( 0000200 | 0004000 );
    if ( serial_options && *serial_options )
    {
        requested_baud = ( uint32_t )( strtoul( serial_options, 0, 10 ) );
        if ( requested_baud == 0 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid baud rate" ); } while ( 0 );
            return 0;
        }
        serial_fmt = strchr( serial_options, ',' );
        if ( serial_fmt )
        {
            serial_fmt++;
        }
        if ( serial_fmt == 0 || strlen( serial_fmt ) != 5 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid serial format: for example use 8N1,H or 8N1,N " ); } while ( 0 );
            return 0;
        }
    }
    if ( serial_fmt == 0 )
    {
        serial_fmt = "8N1,N";
    }
    switch ( requested_baud )
    {
    case 31250:
        baud_code = 0;
        break;
    case 1200:
        baud_code = 0000011;
        break;
    case 2400:
        baud_code = 0000013;
        break;
    case 4800:
        baud_code = 0000014;
        break;
    case 9600:
        baud_code = 0000015;
        break;
    case 19200:
        baud_code = 0000016;
        break;
    case 38400:
        baud_code = 0000017;
        break;
    case 57600:
        baud_code = 0010001;
        break;
    case 115200:
        baud_code = 0010002;
        break;
    case 230400:
        baud_code = 0010003;
        break;
    default:
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid baud rate: %d", requested_baud ); } while ( 0 );
        return 0;
        break;
    }
    if ( baud_code != 0 )
    {
        do
        {
            e = cfsetspeed( &term, baud_code );
        } while ( e < 0 && (*__errno_location ()) == 4 );
        if ( e < 0 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "error setting cfsetspeed" ); } while ( 0 );
            return 0;
        }
    }
    if ( serial_fmt[0] == '7' )
    {
        term.c_cflag |= 0000040;
        term.c_cflag &= ~0000060;
    }
    else if ( serial_fmt[0] == '8' )
    {
        term.c_cflag &= ~0000040;
        term.c_cflag |= 0000060;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid bits: %c", serial_fmt[0] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[1] == 'N' )
    {
        term.c_cflag &= ~0000400;
    }
    else if ( serial_fmt[1] == 'E' )
    {
        term.c_cflag |= 0000400;
        term.c_cflag &= ~0001000;
    }
    else if ( serial_fmt[1] == 'O' )
    {
        term.c_cflag |= 0000400;
        term.c_cflag |= 0001000;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid parity: %c", serial_fmt[1] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[2] == '1' )
    {
        term.c_cflag &= ~0000100;
    }
    else if ( serial_fmt[2] == '2' )
    {
        term.c_cflag |= 0000100;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid stop bits: %c", serial_fmt[2] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[4] == 'H' )
    {
        term.c_cflag |= 020000000000;
    }
    else if ( serial_fmt[4] == 'N' )
    {
        term.c_cflag &= ~020000000000;
    }
    else if ( serial_fmt[4] == 'S' )
    {
        term.c_iflag |= ( 0002000 | 0010000 | 0004000 );
    }
    do
    {
        e = tcsetattr( fd, 0, &term );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "ioctl TCSETS failed" ); } while ( 0 );
        return 0;
    }
    do
    {
        e = fcntl( fd, 4, 04000 );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "fcntl F_SETFL O_NONBLOCK failed" ); } while ( 0 );
        return 0;
    }
    return 1;
}

canonical_original_code:
_Bool us_serial_setup(int fd, char *serial_options)
{
    unsigned int requested_baud = 9600;
    unsigned int baud_code;
    char *serial_fmt = 0;
    struct termios term;
    int e;
    do
    {
        e = tcgetattr( fd, &term );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "ioctl TCGETS failed" ); } while ( 0 );
        return 0;
    }
    term.c_cc[6] = 1;
    term.c_cc[5] = 0;
    term.c_iflag &= ~( 0000002 | 0000400 | 0000020 | 0000040 | 0002000 );
    term.c_iflag |= ( 0000001 | 0000004 );
    term.c_oflag &= ~0000001;
    term.c_lflag &= ~( 0000010 | 0000002 | 0100000 | 0000001 );
    term.c_cflag &= ~( 0000060 );
    term.c_cflag |= ( 0000200 | 0004000 );
    if ( serial_options && *serial_options )
    {
        requested_baud = ( unsigned int )( strtoul( serial_options, 0, 10 ) );
        if ( requested_baud == 0 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid baud rate" ); } while ( 0 );
            return 0;
        }
        serial_fmt = strchr( serial_options, ',' );
        if ( serial_fmt )
        {
            serial_fmt++;
        }
        if ( serial_fmt == 0 || strlen( serial_fmt ) != 5 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid serial format: for example use 8N1,H or 8N1,N " ); } while ( 0 );
            return 0;
        }
    }
    if ( serial_fmt == 0 )
    {
        serial_fmt = "8N1,N";
    }
    switch ( requested_baud )
    {
    case 31250:
        baud_code = 0;
        break;
    case 1200:
        baud_code = 0000011;
        break;
    case 2400:
        baud_code = 0000013;
        break;
    case 4800:
        baud_code = 0000014;
        break;
    case 9600:
        baud_code = 0000015;
        break;
    case 19200:
        baud_code = 0000016;
        break;
    case 38400:
        baud_code = 0000017;
        break;
    case 57600:
        baud_code = 0010001;
        break;
    case 115200:
        baud_code = 0010002;
        break;
    case 230400:
        baud_code = 0010003;
        break;
    default:
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid baud rate: %d", requested_baud ); } while ( 0 );
        return 0;
        break;
    }
    if ( baud_code != 0 )
    {
        do
        {
            e = cfsetspeed( &term, baud_code );
        } while ( e < 0 && (*__errno_location ()) == 4 );
        if ( e < 0 )
        {
            do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "error setting cfsetspeed" ); } while ( 0 );
            return 0;
        }
    }
    if ( serial_fmt[0] == '7' )
    {
        term.c_cflag |= 0000040;
        term.c_cflag &= ~0000060;
    }
    else if ( serial_fmt[0] == '8' )
    {
        term.c_cflag &= ~0000040;
        term.c_cflag |= 0000060;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid bits: %c", serial_fmt[0] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[1] == 'N' )
    {
        term.c_cflag &= ~0000400;
    }
    else if ( serial_fmt[1] == 'E' )
    {
        term.c_cflag |= 0000400;
        term.c_cflag &= ~0001000;
    }
    else if ( serial_fmt[1] == 'O' )
    {
        term.c_cflag |= 0000400;
        term.c_cflag |= 0001000;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid parity: %c", serial_fmt[1] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[2] == '1' )
    {
        term.c_cflag &= ~0000100;
    }
    else if ( serial_fmt[2] == '2' )
    {
        term.c_cflag |= 0000100;
    }
    else
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid stop bits: %c", serial_fmt[2] ); } while ( 0 );
        return 0;
    }
    if ( serial_fmt[4] == 'H' )
    {
        term.c_cflag |= 020000000000;
    }
    else if ( serial_fmt[4] == 'N' )
    {
        term.c_cflag &= ~020000000000;
    }
    else if ( serial_fmt[4] == 'S' )
    {
        term.c_iflag |= ( 0002000 | 0010000 | 0004000 );
    }
    do
    {
        e = tcsetattr( fd, 0, &term );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "ioctl TCSETS failed" ); } while ( 0 );
        return 0;
    }
    do
    {
        e = fcntl( fd, 4, 04000 );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "fcntl F_SETFL O_NONBLOCK failed" ); } while ( 0 );
        return 0;
    }
    return 1;
}

variable_types: fd int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: e int


repo: jdkoftinoff/microsupport

decompiled_code:
__int64 __fastcall us_serial_net_open(__int64 a1)
{
bool v1; // zf
struct addrinfo *v2; // r13
struct addrinfo *v3; // rbp
int v4; // er12
char *v6; // rax
__int64 v7[2]; // [rsp+0h] [rbp-158h] BYREF
__int128 v8; // [rsp+10h] [rbp-148h]
__int128 v9; // [rsp+20h] [rbp-138h]
__int128 v10; // [rsp+30h] [rbp-128h]
char dest[16]; // [rsp+40h] [rbp-118h] BYREF
__int128 v12; // [rsp+50h] [rbp-108h]
__int128 v13; // [rsp+60h] [rbp-F8h]
__int128 v14; // [rsp+70h] [rbp-E8h]
__int128 v15; // [rsp+80h] [rbp-D8h]
__int128 v16; // [rsp+90h] [rbp-C8h]
__int128 v17; // [rsp+A0h] [rbp-B8h]
__int128 v18; // [rsp+B0h] [rbp-A8h]
__int128 v19; // [rsp+C0h] [rbp-98h]
__int128 v20; // [rsp+D0h] [rbp-88h]
__int128 v21; // [rsp+E0h] [rbp-78h]
__int128 v22; // [rsp+F0h] [rbp-68h]
__int128 v23; // [rsp+100h] [rbp-58h]
__int128 v24; // [rsp+110h] [rbp-48h]
__int128 v25; // [rsp+120h] [rbp-38h]
__int128 v26; // [rsp+130h] [rbp-28h]

v1 = *(_BYTE *)(a1 + 4) == 0;
v7[0] = 926365495LL;
*(__m128i *)dest = _mm_load_si128((const __m128i *)&LC17);
v7[1] = 0LL;
v12 = 0LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v20 = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
if ( !v1 )
{
strncpy(dest, (const char *)(a1 + 4), 0xFFuLL);
v6 = strchr(dest, 44);
if ( v6 )
{
*v6 = 0;
strncpy((char *)v7, v6 + 1, 0x3FuLL);
}
}
v2 = (struct addrinfo *)us_net_get_addrinfo("0.0.0.0", "0", 1LL, 0LL);
if ( !v2 )
{
perror("src getaddrinfo:");
abort();
}
v3 = (struct addrinfo *)us_net_get_addrinfo(dest, v7, 1LL, 0LL);
if ( !v3 )
{
perror("dest getaddrinfo:");
abort();
}
v4 = us_net_create_tcp_socket(v2, 0LL);
if ( v4 < 0 )
{
perror("socket:");
abort();
}
while ( connect(v4, v3->ai_addr, v3->ai_addrlen) < 0 )
{
if ( *_errno_location() != 4 )
{
perror("connect to serial net");
close(v4);
abort();
}
}
while ( fcntl(v4, 4, 2048LL) < 0 )
{
if ( *_errno_location() != 4 )
{
perror("fcntl F_SETFL O_NONBLOCK failed");
close(v4);
abort();
}
}
freeaddrinfo(v2);
freeaddrinfo(v3);
return (unsigned int)v4;
}

original_code:
int us_serial_net_open( const char *serial_device, const char *serial_options )
{
    int fd = -1;
    int e;
    const char *connect_addr = serial_device + 4;
    struct addrinfo *src_ai;
    struct addrinfo *dest_ai;
    char dest_ip[256] = "127.0.0.1";
    char dest_port[64] = "7777";
    (void)serial_options;
    if ( strlen( connect_addr ) > 0 )
    {
        char *end_ip;
        strncpy( dest_ip, connect_addr, sizeof( dest_ip ) );
        dest_ip[sizeof( dest_ip ) - 1] = '\0';
        end_ip = strchr( dest_ip, ',' );
        if ( end_ip )
        {
            *end_ip = '\0';
            strncpy( dest_port, end_ip + 1, sizeof( dest_port ) );
            dest_port[sizeof( dest_port ) - 1] = '\0';
        }
    }
    src_ai = us_net_get_addrinfo( "0.0.0.0", "0", SOCK_STREAM, 0 );
    if ( src_ai == 0 )
    {
        perror( "src getaddrinfo:" );
        abort();
    }
    dest_ai = us_net_get_addrinfo( dest_ip, dest_port, SOCK_STREAM, 0 );
    if ( dest_ai == 0 )
    {
        perror( "dest getaddrinfo:" );
        abort();
    }
    fd = us_net_create_tcp_socket( src_ai, 0 );
    if ( fd < 0 )
    {
        perror( "socket:" );
        abort();
    }
    do
    {
        e = connect( fd, dest_ai->ai_addr, dest_ai->ai_addrlen );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        perror( "connect to serial net" );
        close( fd );
        abort();
    }
    do
    {
        e = fcntl( fd, 4, 04000 );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        perror( "fcntl F_SETFL O_NONBLOCK failed" );
        close( fd );
        abort();
    }
    freeaddrinfo( src_ai );
    freeaddrinfo( dest_ai );
    return fd;
}

canonical_original_code:
int us_serial_net_open(char *serial_device, char *serial_options)
{
    int fd = -1;
    int e;
    char *connect_addr = serial_device + 4;
    struct addrinfo *src_ai;
    struct addrinfo *dest_ai;
    char dest_ip[256] = "127.0.0.1";
    char dest_port[64] = "7777";
    (void)serial_options;
    if ( strlen( connect_addr ) > 0 )
    {
        char *end_ip;
        strncpy( dest_ip, connect_addr, sizeof( dest_ip ) );
        dest_ip[sizeof( dest_ip ) - 1] = '\0';
        end_ip = strchr( dest_ip, ',' );
        if ( end_ip )
        {
            *end_ip = '\0';
            strncpy( dest_port, end_ip + 1, sizeof( dest_port ) );
            dest_port[sizeof( dest_port ) - 1] = '\0';
        }
    }
    src_ai = us_net_get_addrinfo( "0.0.0.0", "0", SOCK_STREAM, 0 );
    if ( src_ai == 0 )
    {
        perror( "src getaddrinfo:" );
        abort();
    }
    dest_ai = us_net_get_addrinfo( dest_ip, dest_port, SOCK_STREAM, 0 );
    if ( dest_ai == 0 )
    {
        perror( "dest getaddrinfo:" );
        abort();
    }
    fd = us_net_create_tcp_socket( src_ai, 0 );
    if ( fd < 0 )
    {
        perror( "socket:" );
        abort();
    }
    do
    {
        e = connect( fd, dest_ai->ai_addr, dest_ai->ai_addrlen );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        perror( "connect to serial net" );
        close( fd );
        abort();
    }
    do
    {
        e = fcntl( fd, 4, 04000 );
    } while ( e < 0 && (*__errno_location ()) == 4 );
    if ( e < 0 )
    {
        perror( "fcntl F_SETFL O_NONBLOCK failed" );
        close( fd );
        abort();
    }
    freeaddrinfo( src_ai );
    freeaddrinfo( dest_ai );
    return fd;
}

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

return_type: 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:
__int64 __fastcall us_serial_open(char *file, char *s)
{
int v5; // eax
unsigned int v6; // er12

if ( !memcmp(file, "/dev/", 5uLL) )
{
v5 = open(file, 2);
v6 = v5;
if ( v5 < 0 || !s || (unsigned __int8)us_serial_setup(v5, s) )
return v6;
}
else if ( !memcmp(file, "tcp:", 4uLL) )
{
return us_serial_net_open((__int64)file);
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *file, char *s)
{
int v5; // eax
unsigned int v6; // er12

if ( !memcmp(file, "/dev/", 5uLL) )
{
v5 = open(file, 2);
v6 = v5;
if ( v5 < 0 || !s || (unsigned __int8)func0(v5, s) )
return v6;
}
else if ( !memcmp(file, "tcp:", 4uLL) )
{
return func1((__int64)file);
}
return (unsigned int)-1;
}

original_code:
int us_serial_open( const char *serial_device, const char *serial_options )
{
    int fd = -1;
    if ( strncmp( serial_device, "/dev/", 5 ) == 0 )
    {
        fd = open( serial_device, 02 );
        if ( fd >= 0 && serial_options )
        {
            if ( !us_serial_setup( fd, serial_options ) )
            {
                fd = -1;
            }
        }
    }
    else if ( strncmp( serial_device, "tcp:", 4 ) == 0 )
    {
        fd = us_serial_net_open( serial_device, serial_options );
    }
    return fd;
}

canonical_original_code:
int us_serial_open(char *serial_device, char *serial_options)
{
    int fd = -1;
    if ( strncmp( serial_device, "/dev/", 5 ) == 0 )
    {
        fd = open( serial_device, 02 );
        if ( fd >= 0 && serial_options )
        {
            if ( !us_serial_setup( fd, serial_options ) )
            {
                fd = -1;
            }
        }
    }
    else if ( strncmp( serial_device, "tcp:", 4 ) == 0 )
    {
        fd = us_serial_net_open( serial_device, serial_options );
    }
    return fd;
}


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

if ( a1 != -1 )
result = close(a1);
return result;
}

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

if ( a1 != -1 )
result = close(a1);
return result;
}

original_code:
void us_serial_close( int fd )
{
    if ( fd != -1 )
        close( fd );
}

canonical_original_code:
void us_serial_close(int fd)
{
    if ( fd != -1 )
        close( fd );
}

return_type: void


decompiled_code:
__int64 __fastcall us_osc_dispatch_index_add(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

for ( result = 0LL; result != 16; result += 4LL )
*(_DWORD *)(a1 + result) = *(_DWORD *)(a2 + result) + *(_DWORD *)(a3 + result);
return result;
}

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

for ( result = 0LL; result != 16; result += 4LL )
*(_DWORD *)(a1 + result) = *(_DWORD *)(a2 + result) + *(_DWORD *)(a3 + result);
return result;
}

original_code:
static inline void us_osc_dispatch_index_add( us_osc_dispatch_index_t *self,
                                              const us_osc_dispatch_index_t *src1,
                                              const us_osc_dispatch_index_t *src2 )
{
    int i;
    for ( i = 0; i < ( 4 ); ++i )
        self->axis[i] = src1->axis[i] + src2->axis[i];
}

canonical_original_code:
static inline void us_osc_dispatch_index_add(struct us_osc_dispatch_index_s *self, struct us_osc_dispatch_index_s *src1, struct us_osc_dispatch_index_s *src2)
{
    int i;
    for ( i = 0; i < ( 4 ); ++i )
        self->axis[i] = src1->axis[i] + src2->axis[i];
}

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

variable_types: i int


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

if ( a1 == 63 || a1 == 42 || a2 == 42 )
result = 0LL;
else
result = (unsigned int)(a1 - a2);
return result;
}

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

if ( a1 == 63 || a1 == 42 || a2 == 42 )
result = 0LL;
else
result = (unsigned int)(a1 - a2);
return result;
}

original_code:
int us_osc_dispatch_trie_star_comparator( us_trie_node_value_t a, us_trie_node_value_t b )
{
    if ( a == ( us_trie_node_value_t )( '*' ) || b == ( us_trie_node_value_t )( '*' ) )
    {
        return 0;
    }
    else if ( a == ( us_trie_node_value_t )( '?' ) )
    {
        return 0;
    }
    else
    {
        return (int)( a - b );
    }
}

canonical_original_code:
int us_osc_dispatch_trie_star_comparator(char a, char b)
{
    if ( a == ( us_trie_node_value_t )( '*' ) || b == ( us_trie_node_value_t )( '*' ) )
    {
        return 0;
    }
    else if ( a == ( us_trie_node_value_t )( '?' ) )
    {
        return 0;
    }
    else
    {
        return (int)( a - b );
    }
}

variable_types: a char

variable_types: b char


decompiled_code:
__int64 __fastcall us_osc_dispatch_trie_star_skip(char a1)
{
return (unsigned int)-(a1 == 42);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
return (unsigned int)-(a1 == 42);
}

original_code:
int us_osc_dispatch_trie_star_skip( us_trie_node_value_t a )
{
    int r;
    if ( a == ( us_trie_node_value_t )( '*' ) )
    {
        r = -1;
    }
    else
    {
        r = 0;
    }
    return r;
}

canonical_original_code:
int us_osc_dispatch_trie_star_skip(char a)
{
    int r;
    if ( a == ( us_trie_node_value_t )( '*' ) )
    {
        r = -1;
    }
    else
    {
        r = 0;
    }
    return r;
}

variable_types: r int


decompiled_code:
__int64 __fastcall us_osc_dispatch_destroy(__int64 a1)
{
__int64 v1; // rdi
__int64 result; // rax

if ( *(_QWORD *)(a1 + 16) )
{
v1 = *(_QWORD *)(a1 + 24);
if ( v1 )
result = (*(__int64 (**)(void))(v1 + 56))();
}
return result;
}

original_code:
void us_osc_dispatch_destroy( us_osc_dispatch_t *self )
{
    if ( self->allocator )
    {
        if ( self->trie )
        {
            self->trie->destroy( self->trie );
        }
    }
}

canonical_original_code:
void us_osc_dispatch_destroy(struct us_osc_dispatch_s *self)
{
    if ( self->allocator )
    {
        if ( self->trie )
        {
            self->trie->destroy( self->trie );
        }
    }
}

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

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


decompiled_code:
__int64 __fastcall us_osc_dispatch_receive_msg(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
const char *v6; // r14
__int16 v7; // ax
__int64 result; // rax
__int64 v9; // rax
unsigned __int16 v10; // [rsp+Ah] [rbp-2Eh] BYREF
char v11[2]; // [rsp+Ch] [rbp-2Ch] BYREF
char v12[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v6 = *(const char **)(a2 + 40);
v7 = strlen(v6);
result = us_trie_find(*(_QWORD *)(a1 + 24), v6, (unsigned int)v7, &v10, v11, v12, 0LL, 0LL);
if ( (_BYTE)result )
{
v9 = *(_QWORD *)(a1 + 40) + 24LL * v10;
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, __int64, __int64))(*(_QWORD *)v9 + 8LL))(
a1,
a2,
v9 + 8,
a3,
a4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
const char *v6; // r14
__int16 v7; // ax
__int64 result; // rax
__int64 v9; // rax
unsigned __int16 v10; // [rsp+Ah] [rbp-2Eh] BYREF
char v11[2]; // [rsp+Ch] [rbp-2Ch] BYREF
char v12[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v6 = *(const char **)(a2 + 40);
v7 = strlen(v6);
result = us_trie_find(*(_QWORD *)(a1 + 24), v6, (unsigned int)v7, &v10, v11, v12, 0LL, 0LL);
if ( (_BYTE)result )
{
v9 = *(_QWORD *)(a1 + 40) + 24LL * v10;
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, __int64, __int64))(*(_QWORD *)v9 + 8LL))(
a1,
a2,
v9 + 8,
a3,
a4);
}
return result;
}

original_code:
_Bool us_osc_dispatch_receive_msg( us_osc_dispatch_t *self, const us_osc_msg_t *msg, us_osc_sender_t *sender, void *extra )
{
    _Bool r = 0;
    us_trie_node_flags_t flags;
    int16_t match_len;
    us_trie_node_id_t match_item;
    if ( us_trie_find( &self->trie->m_base,
                       (us_trie_node_value_t *)msg->m_address,
                       strlen( msg->m_address ),
                       &flags,
                       &match_len,
                       &match_item,
                       0,
                       0 ) )
    {
        us_osc_dispatch_map_entry_t *entry = &self->map.entries[flags];
        r = entry->table_entry->receive_msg_proc( self, msg, &entry->final_index, sender, extra );
    }
    return r;
}

canonical_original_code:
_Bool us_osc_dispatch_receive_msg(struct us_osc_dispatch_s *self, struct us_osc_msg_s *msg, struct us_osc_sender_s *sender, void *extra)
{
    _Bool r = 0;
    unsigned short flags;
    short match_len;
    short match_item;
    if ( us_trie_find( &self->trie->m_base,
                       (char *)msg->m_address,
                       strlen( msg->m_address ),
                       &flags,
                       &match_len,
                       &match_item,
                       0,
                       0 ) )
    {
        struct us_osc_dispatch_map_entry_s *entry = &self->map.entries[flags];
        r = entry->table_entry->receive_msg_proc( self, msg, &entry->final_index, sender, extra );
    }
    return r;
}

variable_types:
msg
struct us_osc_msg_s *

variable_types:
sender
struct us_osc_sender_s *

variable_types:
extra
void *

variable_types:
flags
unsigned short

variable_types: struct us_osc_dispatch_map_entry_s { struct us_osc_dispatch_table_s { char *address; _Bool (*receive_msg_proc)(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 *table_item, struct us_osc_dispatch_index_s { int axis[-1]; } *index); struct us_osc_dispatch_map_s { struct us_osc_dispatch_map_entry_s *entries; int max_entries; int num_entries; } map; } *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; } *table_entry; struct us_osc_dispatch_index_s final_index; } *

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


decompiled_code:
__int64 __fastcall us_osc_dispatch_init(__int64 a1, __int64 a2, unsigned int a3, unsigned __int16 a4)
{
__int64 v5; // rax
unsigned int v6; // er8

*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 24) = us_trie_dyn_create(
a2,
a4,
&us_trie_basic_ignorer,
&us_trie_basic_ignorer,
us_osc_dispatch_trie_star_skip,
us_osc_dispatch_trie_star_comparator);
*(_QWORD *)a1 = us_osc_dispatch_destroy;
*(_QWORD *)(a1 + 8) = us_osc_dispatch_receive_msg;
v5 = (*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(a2 + 8))(a2, 24LL, a3);
*(_QWORD *)(a1 + 32) = 0LL;
v6 = 0;
*(_QWORD *)(a1 + 40) = v5;
if ( v5 )
{
*(_DWORD *)(a1 + 48) = a3;
v6 = 1;
*(_DWORD *)(a1 + 52) = 0;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, unsigned int a3, unsigned __int16 a4)
{
__int64 v5; // rax
unsigned int v6; // er8

*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 24) = us_trie_dyn_create(
a2,
a4,
&us_trie_basic_ignorer,
&us_trie_basic_ignorer,
us_osc_dispatch_trie_star_skip,
us_osc_dispatch_trie_star_comparator);
*(_QWORD *)a1 = us_osc_dispatch_destroy;
*(_QWORD *)(a1 + 8) = us_osc_dispatch_receive_msg;
v5 = (*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(a2 + 8))(a2, 24LL, a3);
*(_QWORD *)(a1 + 32) = 0LL;
v6 = 0;
*(_QWORD *)(a1 + 40) = v5;
if ( v5 )
{
*(_DWORD *)(a1 + 48) = a3;
v6 = 1;
*(_DWORD *)(a1 + 52) = 0;
}
return v6;
}

original_code:
_Bool us_osc_dispatch_init( us_osc_dispatch_t *self, us_allocator_t *allocator, int max_table_entries, int max_trie_elements )
{
    _Bool r = 0;
    self->allocator = allocator;
    self->trie = us_trie_dyn_create( allocator,
                                     max_trie_elements,
                                     us_trie_basic_ignorer,
                                     us_trie_basic_ignorer,
                                     us_osc_dispatch_trie_star_skip,
                                     us_osc_dispatch_trie_star_comparator );
    self->destroy = us_osc_dispatch_destroy;
    self->receive_msg = us_osc_dispatch_receive_msg;
    self->map.entries = ( us_osc_dispatch_map_entry_t * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( us_osc_dispatch_map_entry_t ), ( int32_t )( max_table_entries ) ) );
    self->notify = 0;
    if ( self->map.entries )
    {
        self->map.max_entries = max_table_entries;
        self->map.num_entries = 0;
        r = 1;
    }
    return r;
}

canonical_original_code:
_Bool us_osc_dispatch_init(struct us_osc_dispatch_s *self, struct us_allocator_s *allocator, int max_table_entries, int max_trie_elements)
{
    _Bool r = 0;
    self->allocator = allocator;
    self->trie = us_trie_dyn_create( allocator,
                                     max_trie_elements,
                                     us_trie_basic_ignorer,
                                     us_trie_basic_ignorer,
                                     us_osc_dispatch_trie_star_skip,
                                     us_osc_dispatch_trie_star_comparator );
    self->destroy = us_osc_dispatch_destroy;
    self->receive_msg = us_osc_dispatch_receive_msg;
    self->map.entries = ( struct us_osc_dispatch_map_entry_s * )( ( allocator )->alloc( ( allocator ), (int)sizeof( us_osc_dispatch_map_entry_t ), ( int )( max_table_entries ) ) );
    self->notify = 0;
    if ( self->map.entries )
    {
        self->map.max_entries = max_table_entries;
        self->map.num_entries = 0;
        r = 1;
    }
    return r;
}

variable_types:
allocator
struct us_allocator_s *

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

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

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:
bool __fastcall us_osc_dispatch_add_entry_with_notify(__int64 a1, __int64 a2, const char **a3, __int64 a4, unsigned __int8 (__fastcall *a5)(__int64, char *, const char **, _QWORD *))
{
bool result; // al
int v6; // er13
const char ***v9; // rax
_QWORD *v10; // r14
const char *v11; // r9
char *v12; // rax
size_t v13; // rax
_WORD *v14; // rax
char v15[296]; // [rsp+0h] [rbp-128h] BYREF

result = 0;
v6 = *(_DWORD *)(a1 + 52);
if ( v6 < *(_DWORD *)(a1 + 48) )
{
v9 = (const char ***)(*(_QWORD *)(a1 + 40) + 24LL * v6);
*v9 = a3;
v10 = v9 + 1;
us_osc_dispatch_index_add((__int64)(v9 + 1), a4, (__int64)(a3 + 2));
v12 = stpcpy(v15, v11);
strcpy(v12, *a3);
if ( (a5 || (a5 = *(unsigned __int8 (__fastcall **)(__int64, char *, const char **, _QWORD *))(a1 + 32)) != 0LL)
&& !a5(a1, v15, a3, v10) )
{
result = 1;
}
else
{
v13 = strlen(v15);
us_trie_add(*(_QWORD *)(a1 + 24), v15, v13, (unsigned __int16)v6);
v14 = *(_WORD **)(a1 + 24);
++*(_DWORD *)(a1 + 52);
result = v14[1] < *v14;
}
}
return result;
}

original_code:
_Bool us_osc_dispatch_add_entry_with_notify( us_osc_dispatch_t *self,
                                            const char *address_prefix,
                                            const us_osc_dispatch_table_t *dispatch_table,
                                            const us_osc_dispatch_index_t *index_offset,
                                            us_osc_dispatch_notify_proc_t notify_proc )
{
    _Bool r = 0;
    if ( self->map.num_entries < self->map.max_entries )
    {
        char resultant_address[256];
        int item = self->map.num_entries;
        us_osc_dispatch_map_entry_t *entry = &self->map.entries[item];
        r = 1;
        entry->table_entry = dispatch_table;
        us_osc_dispatch_index_add( &entry->final_index, index_offset, &dispatch_table->index );
        strcpy( resultant_address, address_prefix );
        strcat( resultant_address, dispatch_table->address );
        if ( !notify_proc )
        {
            notify_proc = self->notify;
        }
        if ( !notify_proc || notify_proc( self, resultant_address, dispatch_table, &entry->final_index ) == 1 )
        {
            us_trie_add( &self->trie->m_base,
                         (us_trie_node_value_t *)resultant_address,
                         strlen( resultant_address ),
                         (us_trie_node_flags_t)item );
            self->map.num_entries++;
            if ( self->trie->m_base.m_num_nodes >= self->trie->m_base.m_max_nodes )
            {
                r = 0;
            }
        }
    }
    return r;
}

canonical_original_code:
_Bool us_osc_dispatch_add_entry_with_notify(struct us_osc_dispatch_s *self, char *address_prefix, struct us_osc_dispatch_table_s *dispatch_table, struct us_osc_dispatch_index_s *index_offset, _Bool (*notify_proc)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s *table_item, struct us_osc_dispatch_index_s *index))
{
    _Bool r = 0;
    if ( self->map.num_entries < self->map.max_entries )
    {
        char resultant_address[256];
        int item = self->map.num_entries;
        struct us_osc_dispatch_map_entry_s *entry = &self->map.entries[item];
        r = 1;
        entry->table_entry = dispatch_table;
        us_osc_dispatch_index_add( &entry->final_index, index_offset, &dispatch_table->index );
        strcpy( resultant_address, address_prefix );
        strcat( resultant_address, dispatch_table->address );
        if ( !notify_proc )
        {
            notify_proc = self->notify;
        }
        if ( !notify_proc || notify_proc( self, resultant_address, dispatch_table, &entry->final_index ) == 1 )
        {
            us_trie_add( &self->trie->m_base,
                         (char *)resultant_address,
                         strlen( resultant_address ),
                         (unsigned short)item );
            self->map.num_entries++;
            if ( self->trie->m_base.m_num_nodes >= self->trie->m_base.m_max_nodes )
            {
                r = 0;
            }
        }
    }
    return r;
}

variable_types: _Bool (*)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s *table_item, struct us_osc_dispatch_index_s *index)

variable_types: item int


decompiled_code:
bool __fastcall us_osc_dispatch_add_entry(__int64 a1, __int64 a2, const char **a3, __int64 a4)
{
return us_osc_dispatch_add_entry_with_notify(a1, a2, a3, a4, 0LL);
}

canonical_decompiled_code:
bool __fastcall func7(__int64 a1, __int64 a2, const char **a3, __int64 a4)
{
return func6(a1, a2, a3, a4, 0LL);
}

original_code:
_Bool us_osc_dispatch_add_entry( us_osc_dispatch_t *self,
                                const char *address_prefix,
                                const us_osc_dispatch_table_t *dispatch_table,
                                const us_osc_dispatch_index_t *index_offset )
{
    return us_osc_dispatch_add_entry_with_notify( self, address_prefix, dispatch_table, index_offset, 0 );
}

canonical_original_code:
_Bool us_osc_dispatch_add_entry(struct us_osc_dispatch_s *self, char *address_prefix, struct us_osc_dispatch_table_s *dispatch_table, struct us_osc_dispatch_index_s *index_offset)
{
    return us_osc_dispatch_add_entry_with_notify( self, address_prefix, dispatch_table, index_offset, 0 );
}


decompiled_code:
bool __fastcall us_osc_dispatch_add_table_with_notify(__int64 a1, __int64 a2, const char **a3, __int64 a4, unsigned __int8 (__fastcall *a5)(__int64, char *, const char **, _QWORD *))
{
const char **v7; // rbx
bool result; // al

if ( !*a3 )
return 1;
v7 = a3;
while ( 1 )
{
result = us_osc_dispatch_add_entry_with_notify(a1, a2, v7, a4, a5);
if ( !result )
break;
v7 += 4;
if ( !*v7 )
return 1;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func8(__int64 a1, __int64 a2, const char **a3, __int64 a4, unsigned __int8 (__fastcall *a5)(__int64, char *, const char **, _QWORD *))
{
const char **v7; // rbx
bool result; // al

if ( !*a3 )
return 1;
v7 = a3;
while ( 1 )
{
result = func6(a1, a2, v7, a4, a5);
if ( !result )
break;
v7 += 4;
if ( !*v7 )
return 1;
}
return result;
}

original_code:
_Bool us_osc_dispatch_add_table_with_notify( us_osc_dispatch_t *self,
                                            const char *address_prefix,
                                            const us_osc_dispatch_table_t dispatch_table[],
                                            const us_osc_dispatch_index_t *index_offset,
                                            us_osc_dispatch_notify_proc_t notify_proc )
{
    _Bool r = 1;
    while ( dispatch_table->address != 0 )
    {
        if ( !us_osc_dispatch_add_entry_with_notify( self, address_prefix, dispatch_table, index_offset, notify_proc ) )
        {
            r = 0;
            break;
        }
        dispatch_table++;
    }
    return r;
}

canonical_original_code:
_Bool us_osc_dispatch_add_table_with_notify(struct us_osc_dispatch_s *self, char *address_prefix, struct us_osc_dispatch_table_s dispatch_table[], struct us_osc_dispatch_index_s *index_offset, _Bool (*notify_proc)(struct us_osc_dispatch_s *self, char *address, struct us_osc_dispatch_table_s *table_item, struct us_osc_dispatch_index_s *index))
{
    _Bool r = 1;
    while ( dispatch_table->address != 0 )
    {
        if ( !us_osc_dispatch_add_entry_with_notify( self, address_prefix, dispatch_table, index_offset, notify_proc ) )
        {
            r = 0;
            break;
        }
        dispatch_table++;
    }
    return r;
}


decompiled_code:
bool __fastcall us_osc_dispatch_add_table(__int64 a1, __int64 a2, const char **a3, __int64 a4)
{
return us_osc_dispatch_add_table_with_notify(a1, a2, a3, a4, 0LL);
}

canonical_decompiled_code:
bool __fastcall func9(__int64 a1, __int64 a2, const char **a3, __int64 a4)
{
return func8(a1, a2, a3, a4, 0LL);
}

original_code:
_Bool us_osc_dispatch_add_table( us_osc_dispatch_t *self,
                                const char *address_prefix,
                                const us_osc_dispatch_table_t dispatch_table[],
                                const us_osc_dispatch_index_t *index_offset )
{
    return us_osc_dispatch_add_table_with_notify( self, address_prefix, dispatch_table, index_offset, 0 );
}

canonical_original_code:
_Bool us_osc_dispatch_add_table(struct us_osc_dispatch_s *self, char *address_prefix, struct us_osc_dispatch_table_s dispatch_table[], struct us_osc_dispatch_index_s *index_offset)
{
    return us_osc_dispatch_add_table_with_notify( self, address_prefix, dispatch_table, index_offset, 0 );
}


decompiled_code:
__int64 __fastcall us_buffer_readable_count(_QWORD *a1)
{
return (a1[5] + a1[3] - a1[4]) % a1[5];
}

original_code:
static inline size_t us_buffer_readable_count( const us_buffer_t *self )
{
    return ( self->m_next_in - self->m_next_out + self->m_max_length ) % self->m_max_length;
}

canonical_original_code:
static inline unsigned long us_buffer_readable_count(struct us_buffer_s *self)
{
    return ( self->m_next_in - self->m_next_out + self->m_max_length ) % self->m_max_length;
}

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

return_type: unsigned long


decompiled_code:
__int64 __fastcall us_buffer_peek(_QWORD *a1, __int64 a2)
{
return *(unsigned __int8 *)(a1[2] + (unsigned __int64)(a2 + a1[4]) % a1[5]);
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2)
{
return *(unsigned __int8 *)(a1[2] + (unsigned __int64)(a2 + a1[4]) % a1[5]);
}

original_code:
static inline uint8_t us_buffer_peek( const us_buffer_t *self, size_t offset )
{
    return self->m_buffer[( self->m_next_out + offset ) % self->m_max_length];
}

canonical_original_code:
static inline unsigned char us_buffer_peek(struct us_buffer_s *self, unsigned long offset)
{
    return self->m_buffer[( self->m_next_out + offset ) % self->m_max_length];
}

variable_types:
offset
unsigned long

return_type: unsigned char


decompiled_code:
unsigned __int64 __fastcall us_buffer_writable_count(_QWORD *a1)
{
return (unsigned __int64)(a1[5] + a1[4] - 1LL - a1[3]) % a1[5];
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1)
{
return (unsigned __int64)(a1[5] + a1[4] - 1LL - a1[3]) % a1[5];
}

original_code:
static inline size_t us_buffer_writable_count( const us_buffer_t *self )
{
    return ( ( self->m_next_out - self->m_next_in - 1 + self->m_max_length ) % self->m_max_length );
}

canonical_original_code:
static inline unsigned long us_buffer_writable_count(struct us_buffer_s *self)
{
    return ( ( self->m_next_out - self->m_next_in - 1 + self->m_max_length ) % self->m_max_length );
}


decompiled_code:
__int64 __fastcall us_buffer_write_buffer(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // r9
unsigned __int64 v3; // r8
_QWORD *v4; // r10
unsigned __int64 v5; // r11
__int64 result; // rax
__int64 v7; // rcx
char v8; // al
_QWORD *v9; // r9
__int64 v10; // r8

us_buffer_readable_count(a2);
v5 = us_buffer_writable_count(v2);
result = 0LL;
if ( v5 >= v3 )
{
v7 = 0LL;
if ( v3 )
{
do
{
v8 = us_buffer_peek(v4, v7);
us_buffer_write_byte(v9, v8);
}
while ( v10 != v7 );
}
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // r9
unsigned __int64 v3; // r8
_QWORD *v4; // r10
unsigned __int64 v5; // r11
__int64 result; // rax
__int64 v7; // rcx
char v8; // al
_QWORD *v9; // r9
__int64 v10; // r8

func0(a2);
v5 = func2(v2);
result = 0LL;
if ( v5 >= v3 )
{
v7 = 0LL;
if ( v3 )
{
do
{
v8 = func1(v4, v7);
func3(v9, v8);
}
while ( v10 != v7 );
}
result = 1LL;
}
return result;
}

original_code:
static inline _Bool us_buffer_write_buffer( us_buffer_t *self, const us_buffer_t *buf )
{
    _Bool r = 0;
    size_t rc = us_buffer_readable_count( buf );
    if ( us_buffer_writable_count( self ) >= rc )
    {
        size_t i;
        for ( i = 0; i < rc; ++i )
        {
            us_buffer_write_byte( self, us_buffer_peek( buf, i ) );
        }
        r = 1;
    }
    return r;
}

canonical_original_code:
static inline _Bool us_buffer_write_buffer(struct us_buffer_s *self, struct us_buffer_s *buf)
{
    _Bool r = 0;
    unsigned long rc = us_buffer_readable_count( buf );
    if ( us_buffer_writable_count( self ) >= rc )
    {
        unsigned long i;
        for ( i = 0; i < rc; ++i )
        {
            us_buffer_write_byte( self, us_buffer_peek( buf, i ) );
        }
        r = 1;
    }
    return r;
}

variable_types:
buf
struct us_buffer_s *

variable_types:
rc
unsigned long

variable_types:
i
unsigned long


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

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result == 1 )
{
result = us_json_destroy(*(_QWORD *)(a1 + 16));
*(_DWORD *)(a1 + 24) = 0;
}
else
{
if ( (_DWORD)result == 4 )
result = (**(__int64 (__fastcall ***)(_QWORD))(a1 + 16))(*(_QWORD *)(a1 + 16));
*(_DWORD *)(a1 + 24) = 0;
}
return result;
}

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

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result == 1 )
{
result = func8(*(_QWORD *)(a1 + 16));
*(_DWORD *)(a1 + 24) = 0;
}
else
{
if ( (_DWORD)result == 4 )
result = (**(__int64 (__fastcall ***)(_QWORD))(a1 + 16))(*(_QWORD *)(a1 + 16));
*(_DWORD *)(a1 + 24) = 0;
}
return result;
}

original_code:
void us_json_entry_remove_value( us_json_entry_t *self )
{
    if ( self->m_type == us_json_type_json )
    {
        us_json_destroy( self->value.m_value_json );
    }
    else if ( self->m_type == us_json_type_string_buffer )
    {
        self->value.m_value_string_buffer->destroy( self->value.m_value_string_buffer );
    }
    self->m_type = us_json_type_none;
}

canonical_original_code:
void us_json_entry_remove_value(struct us_json_entry_s *self)
{
    if ( self->m_type == us_json_type_json )
    {
        us_json_destroy( self->value.m_value_json );
    }
    else if ( self->m_type == us_json_type_string_buffer )
    {
        self->value.m_value_string_buffer->destroy( self->value.m_value_string_buffer );
    }
    self->m_type = us_json_type_none;
}

variable_types: struct us_json_entry_s { 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; char *m_key; union { struct us_json_s { void (*destroy)(struct us_json_s *self); struct us_allocator_s *m_allocator; struct us_json_entry_s *m_first_item; struct us_json_entry_s *m_last_item; _Bool m_is_array; } *m_value_json; char *m_value_string_ptr; int *m_value_int32_ptr; 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_value_string_buffer; int m_value_int32; } value; enum us_json_type_t { us_json_type_none=0, us_json_type_json=1, us_json_type_string_ptr=2, us_json_type_int32_ptr=3, us_json_type_string_buffer=4, us_json_type_int32=5 } m_type; struct us_json_entry_s *m_next; } *

user_defined_types: struct us_json_entry_s { struct us_allocator_s *m_allocator; char *m_key; union { struct us_json_s *m_value_json; char *m_value_string_ptr; int *m_value_int32_ptr; struct us_buffer_s *m_value_string_buffer; int m_value_int32; } value; enum us_json_type_t m_type; struct us_json_entry_s *m_next; }


decompiled_code:
__int64 __fastcall us_json_entry_destroy(__int64 *a1)
{
__int64 result; // rax
__int64 v3; // rdi

if ( a1[4] )
us_json_entry_destroy();
result = us_json_entry_remove_value((__int64)a1);
v3 = *a1;
if ( *a1 )
result = (*(__int64 (__fastcall **)(__int64, __int64 *))(v3 + 24))(v3, a1);
return result;
}

original_code:
void us_json_entry_destroy( us_json_entry_t *self )
{
    if ( self->m_next )
    {
        us_json_entry_destroy( self->m_next );
    }
    us_json_entry_remove_value( self );
    do { if ( ( self->m_allocator ) != 0 && ( self ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self ) ); } while ( 0 );
}

canonical_original_code:
void us_json_entry_destroy(struct us_json_entry_s *self)
{
    if ( self->m_next )
    {
        us_json_entry_destroy( self->m_next );
    }
    us_json_entry_remove_value( self );
    do { if ( ( self->m_allocator ) != 0 && ( self ) != 0 ) ( self->m_allocator )->free( ( self->m_allocator ), ( self ) ); } while ( 0 );
}

user_defined_types: enum us_json_type_t { us_json_type_none=0, us_json_type_json=1, us_json_type_string_ptr=2, us_json_type_int32_ptr=3, us_json_type_string_buffer=4, us_json_type_int32=5 }

user_defined_types: struct us_json_s { void (*destroy)(struct us_json_s *self); struct us_allocator_s *m_allocator; struct us_json_entry_s *m_first_item; struct us_json_entry_s *m_last_item; _Bool m_is_array; }


decompiled_code:
__int64 __fastcall us_json_destroy(__int64 a1)
{
__int64 *v2; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = *(__int64 **)(a1 + 16);
if ( v2 )
result = us_json_entry_destroy(v2);
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
}
return result;
}

original_code:
void us_json_destroy( us_json_t *self )
{
    if ( self )
    {
        us_json_entry_t *cur = self->m_first_item;
        if ( cur )
        {
            us_json_entry_destroy( cur );
        }
        self->m_first_item = 0;
        self->m_last_item = 0;
    }
}

canonical_original_code:
void us_json_destroy(struct us_json_s *self)
{
    if ( self )
    {
        struct us_json_entry_s *cur = self->m_first_item;
        if ( cur )
        {
            us_json_entry_destroy( cur );
        }
        self->m_first_item = 0;
        self->m_last_item = 0;
    }
}

variable_types: struct us_json_s { void (*destroy)(struct us_json_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_json_entry_s { struct us_allocator_s *m_allocator; char *m_key; union { struct us_json_s *m_value_json; char *m_value_string_ptr; int *m_value_int32_ptr; 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_value_string_buffer; int m_value_int32; } value; enum us_json_type_t { us_json_type_none=0, us_json_type_json=1, us_json_type_string_ptr=2, us_json_type_int32_ptr=3, us_json_type_string_buffer=4, us_json_type_int32=5 } m_type; struct us_json_entry_s *m_next; } *m_first_item; struct us_json_entry_s *m_last_item; _Bool m_is_array; } *


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

result = us_json_entry_remove_value(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 1;
return result;
}

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

result = func6(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 1;
return result;
}

original_code:
void us_json_entry_set_value_json( us_json_entry_t *self, struct us_json_s *value )
{
    us_json_entry_remove_value( self );
    self->value.m_value_json = value;
    self->m_type = us_json_type_json;
}

canonical_original_code:
void us_json_entry_set_value_json(struct us_json_entry_s *self, struct us_json_s *value)
{
    us_json_entry_remove_value( self );
    self->value.m_value_json = value;
    self->m_type = us_json_type_json;
}

variable_types:
value
struct us_json_s *


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

result = us_json_entry_remove_value(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 2;
return result;
}

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

result = func6(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 2;
return result;
}

original_code:
void us_json_entry_set_value_string_ptr( us_json_entry_t *self, const char *s )
{
    us_json_entry_remove_value( self );
    self->value.m_value_string_ptr = s;
    self->m_type = us_json_type_string_ptr;
}

canonical_original_code:
void us_json_entry_set_value_string_ptr(struct us_json_entry_s *self, char *s)
{
    us_json_entry_remove_value( self );
    self->value.m_value_string_ptr = s;
    self->m_type = us_json_type_string_ptr;
}

variable_types:
s
char *


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

result = us_json_entry_remove_value(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 3;
return result;
}

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

result = func6(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 3;
return result;
}

original_code:
void us_json_entry_set_value_int32_ptr( us_json_entry_t *self, int32_t *value )
{
    us_json_entry_remove_value( self );
    self->value.m_value_int32_ptr = value;
    self->m_type = us_json_type_int32_ptr;
}

canonical_original_code:
void us_json_entry_set_value_int32_ptr(struct us_json_entry_s *self, int *value)
{
    us_json_entry_remove_value( self );
    self->value.m_value_int32_ptr = value;
    self->m_type = us_json_type_int32_ptr;
}

variable_types:
value
int *


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

result = us_json_entry_remove_value(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 4;
return result;
}

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

result = func6(a1);
*(_QWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 4;
return result;
}

original_code:
void us_json_entry_set_value_string_buffer( us_json_entry_t *self, us_buffer_t *buf )
{
    us_json_entry_remove_value( self );
    self->value.m_value_string_buffer = buf;
    self->m_type = us_json_type_string_buffer;
}

canonical_original_code:
void us_json_entry_set_value_string_buffer(struct us_json_entry_s *self, struct us_buffer_s *buf)
{
    us_json_entry_remove_value( self );
    self->value.m_value_string_buffer = buf;
    self->m_type = us_json_type_string_buffer;
}


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

result = us_json_entry_remove_value(a1);
*(_DWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 5;
return result;
}

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

result = func6(a1);
*(_DWORD *)(a1 + 16) = a2;
*(_DWORD *)(a1 + 24) = 5;
return result;
}

original_code:
void us_json_entry_set_value_int32( us_json_entry_t *self, int32_t value )
{
    us_json_entry_remove_value( self );
    self->value.m_value_int32 = value;
    self->m_type = us_json_type_int32;
}

canonical_original_code:
void us_json_entry_set_value_int32(struct us_json_entry_s *self, int value)
{
    us_json_entry_remove_value( self );
    self->value.m_value_int32 = value;
    self->m_type = us_json_type_int32;
}

variable_types: value int


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

result = (*(__int64 (__fastcall **)(__int64, __int64, __int64))(a1 + 8))(a1, 40LL, 1LL);
if ( result )
{
*(_QWORD *)(result + 8) = a1;
*(_BYTE *)(result + 32) = 0;
*(_QWORD *)result = us_json_destroy;
*(_QWORD *)(result + 16) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
}
return result;
}

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

result = (*(__int64 (__fastcall **)(__int64, __int64, __int64))(a1 + 8))(a1, 40LL, 1LL);
if ( result )
{
*(_QWORD *)(result + 8) = a1;
*(_BYTE *)(result + 32) = 0;
*(_QWORD *)result = us_json_destroy;
*(_QWORD *)(result + 16) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
}
return result;
}

original_code:
us_json_t *us_json_create( us_allocator_t *allocator )
{
    us_json_t *self = ( us_json_t * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( us_json_t ), 1 ) );
    if ( self )
    {
        self->m_allocator = allocator;
        self->m_is_array = 0;
        self->destroy = us_json_destroy;
        self->m_first_item = 0;
        self->m_last_item = 0;
    }
    return self;
}

canonical_original_code:
struct us_json_s *us_json_create(struct us_allocator_s *allocator)
{
    struct us_json_s *self = ( us_json_t * )( ( allocator )->alloc( ( allocator ), (int32_t)sizeof( us_json_t ), 1 ) );
    if ( self )
    {
        self->m_allocator = allocator;
        self->m_is_array = 0;
        self->destroy = us_json_destroy;
        self->m_first_item = 0;
        self->m_last_item = 0;
    }
    return self;
}

return_type: struct us_json_s { void (*destroy)(struct us_json_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_json_entry_s { struct us_allocator_s *m_allocator; char *m_key; union { struct us_json_s *m_value_json; char *m_value_string_ptr; int *m_value_int32_ptr; 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_value_string_buffer; int m_value_int32; } value; enum us_json_type_t { us_json_type_none=0, us_json_type_json=1, us_json_type_string_ptr=2, us_json_type_int32_ptr=3, us_json_type_string_buffer=4, us_json_type_int32=5 } m_type; struct us_json_entry_s *m_next; } *m_first_item; struct us_json_entry_s *m_last_item; _Bool m_is_array; } *


decompiled_code:
__int64 __fastcall us_json_append_object(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 *v3; // r13
__int64 v4; // rax

v2 = 0LL;
v3 = (__int64 *)us_json_entry_create(*(_QWORD *)(a1 + 8), a2);
if ( v3 )
{
v4 = us_json_create(*(_QWORD *)(a1 + 8));
v2 = v4;
if ( v4 )
us_json_entry_set_value_json((__int64)v3, v4);
}
if ( !us_json_append_entry(a1, v3) )
v2 = 0LL;
return v2;
}

original_code:
us_json_t *us_json_append_object( us_json_t *self, const char *key )
{
    us_json_t *j = 0;
    us_json_entry_t *entry = us_json_entry_create( self->m_allocator, key );
    if ( entry )
    {
        j = us_json_create( self->m_allocator );
        if ( j )
        {
            us_json_entry_set_value_json( entry, j );
        }
    }
    if ( !us_json_append_entry( self, entry ) )
    {
        j = 0;
    }
    return j;
}

canonical_original_code:
struct us_json_s *us_json_append_object(struct us_json_s *self, char *key)
{
    struct us_json_s *j = 0;
    struct us_json_entry_s *entry = us_json_entry_create( self->m_allocator, key );
    if ( entry )
    {
        j = us_json_create( self->m_allocator );
        if ( j )
        {
            us_json_entry_set_value_json( entry, j );
        }
    }
    if ( !us_json_append_entry( self, entry ) )
    {
        j = 0;
    }
    return j;
}

variable_types:
self
struct us_json_s *

variable_types:
key
char *


decompiled_code:
__int64 __fastcall us_json_flatten_to_buffer(__int64 a1, __int64 a2)
{
void *v3; // rsi
__int64 v4; // rbx
void *v5; // rsi

v3 = &unk_663;
v4 = *(_QWORD *)(a1 + 16);
if ( !*(_BYTE *)(a1 + 32) )
v3 = &unk_666;
us_buffer_append_string(a2, v3);
for ( ; v4; v4 = *(_QWORD *)(v4 + 32) )
{
us_json_entry_flatten_to_buffer(v4, a2);
if ( !*(_QWORD *)(v4 + 32) )
break;
us_buffer_append_string(a2, &unk_671);
}
v5 = &unk_669;
if ( !*(_BYTE *)(a1 + 32) )
v5 = &unk_66D;
us_buffer_append_string(a2, v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2)
{
void *v3; // rsi
__int64 v4; // rbx
void *v5; // rsi

v3 = &unk_663;
v4 = *(_QWORD *)(a1 + 16);
if ( !*(_BYTE *)(a1 + 32) )
v3 = &unk_666;
us_buffer_append_string(a2, v3);
for ( ; v4; v4 = *(_QWORD *)(v4 + 32) )
{
func17(v4, a2);
if ( !*(_QWORD *)(v4 + 32) )
break;
us_buffer_append_string(a2, &unk_671);
}
v5 = &unk_669;
if ( !*(_BYTE *)(a1 + 32) )
v5 = &unk_66D;
us_buffer_append_string(a2, v5);
return 1LL;
}

original_code:
_Bool us_json_flatten_to_buffer( const us_json_t *self, us_buffer_t *buffer )
{
    _Bool r = 1;
    us_json_entry_t *entry = self->m_first_item;
    us_buffer_append_string( buffer, self->m_is_array ? "[ " : "{ " );
    while ( entry )
    {
        us_json_entry_flatten_to_buffer( entry, buffer );
        if ( entry->m_next )
        {
            us_buffer_append_string( buffer, ",\n" );
        }
        entry = entry->m_next;
    }
    us_buffer_append_string( buffer, self->m_is_array ? " ]\n" : " }\n" );
    return r;
}

canonical_original_code:
_Bool us_json_flatten_to_buffer(struct us_json_s *self, struct us_buffer_s *buffer)
{
    _Bool r = 1;
    struct us_json_entry_s *entry = self->m_first_item;
    us_buffer_append_string( buffer, self->m_is_array ? "[ " : "{ " );
    while ( entry )
    {
        us_json_entry_flatten_to_buffer( entry, buffer );
        if ( entry->m_next )
        {
            us_buffer_append_string( buffer, ",\n" );
        }
        entry = entry->m_next;
    }
    us_buffer_append_string( buffer, self->m_is_array ? " ]\n" : " }\n" );
    return r;
}

variable_types:
buffer
struct us_buffer_s *


decompiled_code:
__int64 __fastcall us_json_entry_flatten_to_buffer(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
unsigned int v5; // er12
int v7; // er12
int v8; // eax
char v9[72]; // [rsp+0h] [rbp-48h] BYREF

v2 = 1;
if ( *(_QWORD *)(a1 + 8) )
{
v3 = us_buffer_append_string(a2, &unk_674);
v4 = us_buffer_append_string(a2, *(_QWORD *)(a1 + 8)) & v3;
v2 = us_buffer_append_string(a2, "\" : ") & v4;
}
switch ( *(_DWORD *)(a1 + 24) )
{
case 0:
return (unsigned int)us_buffer_append_string(a2, "null") & v2;
case 1:
return (unsigned int)us_json_flatten_to_buffer(*(_QWORD *)(a1 + 16), a2) & v2;
case 2:
v7 = us_buffer_append_string(a2, &unk_674);
v8 = us_buffer_append_string(a2, *(_QWORD *)(a1 + 16));
return (unsigned int)us_buffer_append_string(a2, &unk_674) & v2 & v8 & v7;
case 3:
snprintf(v9, 0x20uLL, "%d", **(unsigned int **)(a1 + 16));
v2 &= us_buffer_append_string(a2, v9);
goto LABEL_4;
case 4:
v7 = us_buffer_append_string(a2, &unk_674);
v8 = us_buffer_write_buffer(a2, *(_QWORD **)(a1 + 16));
return (unsigned int)us_buffer_append_string(a2, &unk_674) & v2 & v8 & v7;
case 5:
LABEL_4:
snprintf(v9, 0x20uLL, "%d", *(unsigned int *)(a1 + 16));
v5 = us_buffer_append_string(a2, v9) & v2;
break;
default:
v5 = 0;
if ( us_log_level > 0 )
us_log_error_proc("invalid json entry type");
break;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
unsigned int v5; // er12
int v7; // er12
int v8; // eax
char v9[72]; // [rsp+0h] [rbp-48h] BYREF

v2 = 1;
if ( *(_QWORD *)(a1 + 8) )
{
v3 = us_buffer_append_string(a2, &unk_674);
v4 = us_buffer_append_string(a2, *(_QWORD *)(a1 + 8)) & v3;
v2 = us_buffer_append_string(a2, "\" : ") & v4;
}
switch ( *(_DWORD *)(a1 + 24) )
{
case 0:
return (unsigned int)us_buffer_append_string(a2, "null") & v2;
case 1:
return (unsigned int)func16(*(_QWORD *)(a1 + 16), a2) & v2;
case 2:
v7 = us_buffer_append_string(a2, &unk_674);
v8 = us_buffer_append_string(a2, *(_QWORD *)(a1 + 16));
return (unsigned int)us_buffer_append_string(a2, &unk_674) & v2 & v8 & v7;
case 3:
snprintf(v9, 0x20uLL, "%d", **(unsigned int **)(a1 + 16));
v2 &= us_buffer_append_string(a2, v9);
goto LABEL_4;
case 4:
v7 = us_buffer_append_string(a2, &unk_674);
v8 = func4(a2, *(_QWORD **)(a1 + 16));
return (unsigned int)us_buffer_append_string(a2, &unk_674) & v2 & v8 & v7;
case 5:
LABEL_4:
snprintf(v9, 0x20uLL, "%d", *(unsigned int *)(a1 + 16));
v5 = us_buffer_append_string(a2, v9) & v2;
break;
default:
v5 = 0;
if ( us_log_level > 0 )
us_log_error_proc("invalid json entry type");
break;
}
return v5;
}

original_code:
_Bool us_json_entry_flatten_to_buffer( const us_json_entry_t *self, us_buffer_t *buffer )
{
    _Bool r = 1;
    if ( self->m_key )
    {
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_append_string( buffer, self->m_key );
        r &= us_buffer_append_string( buffer, "\" : " );
    }
    switch ( self->m_type )
    {
    case us_json_type_none:
        r &= us_buffer_append_string( buffer, "null" );
        break;
    case us_json_type_json:
        r &= us_json_flatten_to_buffer( self->value.m_value_json, buffer );
        break;
    case us_json_type_string_ptr:
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_append_string( buffer, self->value.m_value_string_ptr );
        r &= us_buffer_append_string( buffer, "\"" );
        break;
    case us_json_type_int32_ptr:
    {
        char s[32];
        snprintf( s, sizeof( s ), "%d", *self->value.m_value_int32_ptr );
        r &= us_buffer_append_string( buffer, s );
    }
    case us_json_type_int32:
    {
        char s[32];
        snprintf( s, sizeof( s ), "%d", self->value.m_value_int32 );
        r &= us_buffer_append_string( buffer, s );
    }
    break;
    case us_json_type_string_buffer:
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_write_buffer( buffer, self->value.m_value_string_buffer );
        r &= us_buffer_append_string( buffer, "\"" );
        break;
    default:
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid json entry type" ); } while ( 0 );
        r = 0;
        break;
    }
    return r;
}

canonical_original_code:
_Bool us_json_entry_flatten_to_buffer(struct us_json_entry_s *self, struct us_buffer_s *buffer)
{
    _Bool r = 1;
    if ( self->m_key )
    {
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_append_string( buffer, self->m_key );
        r &= us_buffer_append_string( buffer, "\" : " );
    }
    switch ( self->m_type )
    {
    case us_json_type_none:
        r &= us_buffer_append_string( buffer, "null" );
        break;
    case us_json_type_json:
        r &= us_json_flatten_to_buffer( self->value.m_value_json, buffer );
        break;
    case us_json_type_string_ptr:
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_append_string( buffer, self->value.m_value_string_ptr );
        r &= us_buffer_append_string( buffer, "\"" );
        break;
    case us_json_type_int32_ptr:
    {
        char s[32];
        snprintf( s, sizeof( s ), "%d", *self->value.m_value_int32_ptr );
        r &= us_buffer_append_string( buffer, s );
    }
    case us_json_type_int32:
    {
        char s[32];
        snprintf( s, sizeof( s ), "%d", self->value.m_value_int32 );
        r &= us_buffer_append_string( buffer, s );
    }
    break;
    case us_json_type_string_buffer:
        r &= us_buffer_append_string( buffer, "\"" );
        r &= us_buffer_write_buffer( buffer, self->value.m_value_string_buffer );
        r &= us_buffer_append_string( buffer, "\"" );
        break;
    default:
        do { if ( us_log_level >= ( 1 ) ) us_log_error_proc( "invalid json entry type" ); } while ( 0 );
        r = 0;
        break;
    }
    return r;
}

variable_types:
s
char [32]


decompiled_code:
__int64 __fastcall us_parse_hexdig(_BYTE *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v4; // rax
char v5; // dl
__int64 result; // rax
char v7; // dl

v4 = *a4;
if ( *a4 >= a3 )
return 0LL;
v5 = *(_BYTE *)(a2 + v4);
*a4 = v4 + 1;
if ( (unsigned __int8)(v5 - 48) <= 9u )
{
*a1 = v5 - 48;
return 1LL;
}
v7 = v5 & 0xDF;
if ( (unsigned __int8)(v7 - 65) > 5u )
return 0LL;
result = 1LL;
*a1 = v7 - 55;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v4; // rax
char v5; // dl
__int64 result; // rax
char v7; // dl

v4 = *a4;
if ( *a4 >= a3 )
return 0LL;
v5 = *(_BYTE *)(a2 + v4);
*a4 = v4 + 1;
if ( (unsigned __int8)(v5 - 48) <= 9u )
{
*a1 = v5 - 48;
return 1LL;
}
v7 = v5 & 0xDF;
if ( (unsigned __int8)(v7 - 65) > 5u )
return 0LL;
result = 1LL;
*a1 = v7 - 55;
return result;
}

original_code:
_Bool us_parse_hexdig( uint8_t *val, const char *buf, size_t buf_len, size_t *buf_pos )
{
    _Bool r = 0;
    if ( *buf_pos < buf_len )
    {
        char c = buf[*buf_pos];
        ++( *buf_pos );
        if ( c >= '0' && c <= '9' )
        {
            *val = c - '0';
            r = 1;
        }
        else
        {
            c = c & 0xdf;
            if ( c >= 'A' && c <= 'F' )
            {
                *val = c - 'A' + 10;
                r = 1;
            }
        }
    }
    return r;
}

canonical_original_code:
_Bool us_parse_hexdig(unsigned char *val, char *buf, unsigned long buf_len, unsigned long *buf_pos)
{
    _Bool r = 0;
    if ( *buf_pos < buf_len )
    {
        char c = buf[*buf_pos];
        ++( *buf_pos );
        if ( c >= '0' && c <= '9' )
        {
            *val = c - '0';
            r = 1;
        }
        else
        {
            c = c & 0xdf;
            if ( c >= 'A' && c <= 'F' )
            {
                *val = c - 'A' + 10;
                r = 1;
            }
        }
    }
    return r;
}

variable_types:
val
unsigned char *

variable_types:
buf
char *

variable_types: c char


decompiled_code:
char __fastcall us_parse_hexoctet(__int64 a1, __int64 a2, unsigned __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 *v4; // rcx
unsigned __int64 v5; // r9
char v6; // al
_BYTE *v7; // r10
char v8; // r8
char result; // al
char v10; // [rsp+Eh] [rbp-2h] BYREF
char v11; // [rsp+Fh] [rbp-1h] BYREF

v10 = 0;
v11 = 0;
us_parse_hexdig(&v10, a2, a3, a4);
v6 = us_parse_hexdig(&v11, a2, v5, v4);
result = v8 & v6;
if ( result )
*v7 = v11 + 16 * v10;
return result;
}

canonical_decompiled_code:
char __fastcall func1(__int64 a1, __int64 a2, unsigned __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 *v4; // rcx
unsigned __int64 v5; // r9
char v6; // al
_BYTE *v7; // r10
char v8; // r8
char result; // al
char v10; // [rsp+Eh] [rbp-2h] BYREF
char v11; // [rsp+Fh] [rbp-1h] BYREF

v10 = 0;
v11 = 0;
func0(&v10, a2, a3, a4);
v6 = func0(&v11, a2, v5, v4);
result = v8 & v6;
if ( result )
*v7 = v11 + 16 * v10;
return result;
}

original_code:
_Bool us_parse_hexoctet( uint8_t *val, const char *buf, size_t buf_len, size_t *buf_pos )
{
    _Bool r = 1;
    uint8_t high = 0;
    uint8_t low = 0;
    r &= us_parse_hexdig( &high, buf, buf_len, buf_pos );
    r &= us_parse_hexdig( &low, buf, buf_len, buf_pos );
    if ( r )
    {
        *val = ( high << 4 ) + low;
    }
    return r;
}

canonical_original_code:
_Bool us_parse_hexoctet(unsigned char *val, char *buf, unsigned long buf_len, unsigned long *buf_pos)
{
    _Bool r = 1;
    unsigned char high = 0;
    unsigned char low = 0;
    r &= us_parse_hexdig( &high, buf, buf_len, buf_pos );
    r &= us_parse_hexdig( &low, buf, buf_len, buf_pos );
    if ( r )
    {
        *val = ( high << 4 ) + low;
    }
    return r;
}

variable_types:
high
unsigned char

variable_types:
low
unsigned char


decompiled_code:
int us_logger_stdio_finish()
{
int result; // eax

if ( us_logger_stdio_files )
{
if ( us_logger_stdio_out )
{
fflush(us_logger_stdio_out);
result = fclose(us_logger_stdio_out);
us_logger_stdio_out = 0LL;
}
if ( us_logger_stdio_err )
{
fflush(us_logger_stdio_err);
result = fclose(us_logger_stdio_err);
us_logger_stdio_err = 0LL;
}
}
us_log_error_proc = &us_log_null;
us_log_warn_proc = &us_log_null;
us_log_info_proc = &us_log_null;
us_log_debug_proc = &us_log_null;
us_logger_finish = &us_logger_null_finish;
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

if ( us_logger_stdio_files )
{
if ( us_logger_stdio_out )
{
fflush(us_logger_stdio_out);
result = fclose(us_logger_stdio_out);
us_logger_stdio_out = 0LL;
}
if ( us_logger_stdio_err )
{
fflush(us_logger_stdio_err);
result = fclose(us_logger_stdio_err);
us_logger_stdio_err = 0LL;
}
}
us_log_error_proc = &us_log_null;
us_log_warn_proc = &us_log_null;
us_log_info_proc = &us_log_null;
us_log_debug_proc = &us_log_null;
us_logger_finish = &us_logger_null_finish;
return result;
}

original_code:
void us_logger_stdio_finish( void )
{
    if ( us_logger_stdio_files )
    {
        if ( us_logger_stdio_out )
        {
            fflush( us_logger_stdio_out );
            fclose( us_logger_stdio_out );
            us_logger_stdio_out = 0;
        }
        if ( us_logger_stdio_err )
        {
            fflush( us_logger_stdio_err );
            fclose( us_logger_stdio_err );
            us_logger_stdio_err = 0;
        }
    }
    us_log_error_proc = us_log_null;
    us_log_warn_proc = us_log_null;
    us_log_info_proc = us_log_null;
    us_log_debug_proc = us_log_null;
    us_logger_finish = us_logger_null_finish;
}

canonical_original_code:
void us_logger_stdio_finish()
{
    if ( us_logger_stdio_files )
    {
        if ( us_logger_stdio_out )
        {
            fflush( us_logger_stdio_out );
            fclose( us_logger_stdio_out );
            us_logger_stdio_out = 0;
        }
        if ( us_logger_stdio_err )
        {
            fflush( us_logger_stdio_err );
            fclose( us_logger_stdio_err );
            us_logger_stdio_err = 0;
        }
    }
    us_log_error_proc = us_log_null;
    us_log_warn_proc = us_log_null;
    us_log_info_proc = us_log_null;
    us_log_debug_proc = us_log_null;
    us_logger_finish = us_logger_null_finish;
}


decompiled_code:
int us_log_error_stdio(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "ERROR (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "ERROR (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

original_code:
void us_log_error_stdio( const char *fmt, ... )
{
    struct timeval tv;
    va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "ERROR (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stderr, fmt, ap );
    fprintf( stderr, "\n" );
    __builtin_va_end(ap);
    fflush( stderr );
}

canonical_original_code:
void us_log_error_stdio(char *fmt, ...)
{
    struct timeval tv;
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "ERROR (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stderr, fmt, ap );
    fprintf( stderr, "\n" );
    __builtin_va_end(ap);
    fflush( stderr );
}

variable_types:
fmt
char *

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int us_log_warn_stdio(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "WARNING (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

canonical_decompiled_code:
int func2(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "WARNING (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

original_code:
void us_log_warn_stdio( const char *fmt, ... )
{
    struct timeval tv;
    va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "WARNING (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stderr, fmt, ap );
    fprintf( stderr, "\n" );
    __builtin_va_end(ap);
    fflush( stderr );
}

canonical_original_code:
void us_log_warn_stdio(char *fmt, ...)
{
    struct timeval tv;
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "WARNING (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stderr, fmt, ap );
    fprintf( stderr, "\n" );
    __builtin_va_end(ap);
    fflush( stderr );
}

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int us_log_info_stdio(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "INFO (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

canonical_decompiled_code:
int func3(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "INFO (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

original_code:
void us_log_info_stdio( const char *fmt, ... )
{
    struct timeval tv;
    va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "INFO (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}

canonical_original_code:
void us_log_info_stdio(char *fmt, ...)
{
    struct timeval tv;
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "INFO (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}


decompiled_code:
int us_log_debug_stdio(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "DEBUG (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

canonical_decompiled_code:
int func4(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "DEBUG (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

original_code:
void us_log_debug_stdio( const char *fmt, ... )
{
    struct timeval tv;
    va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "DEBUG (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}

canonical_original_code:
void us_log_debug_stdio(char *fmt, ...)
{
    struct timeval tv;
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "DEBUG (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}


decompiled_code:
int us_log_trace_stdio(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "TRACE (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

canonical_decompiled_code:
int func5(char *format, ...)
{
__int64 v2[3]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-D0h] BYREF

va_start(arg, format);
us_gettimeofday(v2);
fprintf(stderr, "TRACE (@%ld.%ld):\t", v2[0], v2[1]);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

original_code:
void us_log_trace_stdio( const char *fmt, ... )
{
    struct timeval tv;
    va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "TRACE (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}

canonical_original_code:
void us_log_trace_stdio(char *fmt, ...)
{
    struct timeval tv;
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    us_gettimeofday( &tv );
    fprintf( stderr, "TRACE (@%ld.%ld):\t", (long)tv.tv_sec, (long)tv.tv_usec );
    vfprintf( stdout, fmt, ap );
    fprintf( stdout, "\n" );
    __builtin_va_end(ap);
    fflush( stdout );
}


decompiled_code:
__int64 __fastcall us_logger_init_stdio_files(const char *a1, const char *a2)
{
us_logger_stdio_out = fopen(a1, "at");
if ( us_logger_stdio_out )
{
us_logger_stdio_err = fopen(a2, "at");
if ( us_logger_stdio_err )
{
us_logger_stdio_files = 1;
us_log_error_proc = us_log_error_stdio;
us_log_warn_proc = us_log_warn_stdio;
us_log_info_proc = us_log_info_stdio;
us_log_debug_proc = us_log_debug_stdio;
us_logger_finish = us_logger_stdio_finish;
}
else
{
fclose(us_logger_stdio_out);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, const char *a2)
{
us_logger_stdio_out = fopen(a1, "at");
if ( us_logger_stdio_out )
{
us_logger_stdio_err = fopen(a2, "at");
if ( us_logger_stdio_err )
{
us_logger_stdio_files = 1;
us_log_error_proc = us_log_error_stdio;
us_log_warn_proc = us_log_warn_stdio;
us_log_info_proc = us_log_info_stdio;
us_log_debug_proc = us_log_debug_stdio;
us_logger_finish = us_logger_stdio_finish;
}
else
{
fclose(us_logger_stdio_out);
}
}
return 0LL;
}

original_code:
_Bool us_logger_init_stdio_files( const char *outfilename, const char *errfilename )
{
    _Bool r = 0;
    us_logger_stdio_out = fopen( outfilename, "at" );
    if ( us_logger_stdio_out )
    {
        us_logger_stdio_err = fopen( errfilename, "at" );
        if ( us_logger_stdio_err )
        {
            us_logger_stdio_files = 1;
            us_log_error_proc = us_log_error_stdio;
            us_log_warn_proc = us_log_warn_stdio;
            us_log_info_proc = us_log_info_stdio;
            us_log_debug_proc = us_log_debug_stdio;
            us_logger_finish = us_logger_stdio_finish;
        }
        else
        {
            fclose( us_logger_stdio_out );
        }
    }
    return r;
}

canonical_original_code:
_Bool us_logger_init_stdio_files(char *outfilename, char *errfilename)
{
    _Bool r = 0;
    us_logger_stdio_out = fopen( outfilename, "at" );
    if ( us_logger_stdio_out )
    {
        us_logger_stdio_err = fopen( errfilename, "at" );
        if ( us_logger_stdio_err )
        {
            us_logger_stdio_files = 1;
            us_log_error_proc = us_log_error_stdio;
            us_log_warn_proc = us_log_warn_stdio;
            us_log_info_proc = us_log_info_stdio;
            us_log_debug_proc = us_log_debug_stdio;
            us_logger_finish = us_logger_stdio_finish;
        }
        else
        {
            fclose( us_logger_stdio_out );
        }
    }
    return r;
}

variable_types:
outfilename
char *

variable_types:
errfilename
char *


decompiled_code:
__int64 __fastcall us_simple_allocator_print(__int64 a1, __int64 a2)
{
return (*(__int64 (**)(__int64, const char *, ...))(a2 + 8))(
a2,
"Allocator:\n current_position: %d\n raw_memory: 0x%08p\n raw_memory_length: 0x%08lx\n",
*(unsigned int *)(a1 + 44),
*(const void **)(a1 + 32),
*(unsigned int *)(a1 + 40));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return (*(__int64 (**)(__int64, const char *, ...))(a2 + 8))(
a2,
"Allocator:\n current_position: %d\n raw_memory: 0x%08p\n raw_memory_length: 0x%08lx\n",
*(unsigned int *)(a1 + 44),
*(const void **)(a1 + 32),
*(unsigned int *)(a1 + 40));
}

original_code:
_Bool us_simple_allocator_print( us_simple_allocator_t *self, us_print_t *printer )
{
    _Bool r = 1;
    r &= printer->printf( printer,
                          "Allocator:\n current_position: %d\n"
                          " raw_memory: 0x%08p\n"
                          " raw_memory_length: 0x%08lx\n",
                          self->m_current_position,
                          self->m_raw_memory,
                          self->m_raw_memory_length );
    return r;
}

canonical_original_code:
_Bool us_simple_allocator_print(struct us_simple_allocator_s *self, struct us_print_s *printer)
{
    _Bool r = 1;
    r &= printer->printf( printer,
                          "Allocator:\n current_position: %d\n"
                          " raw_memory: 0x%08p\n"
                          " raw_memory_length: 0x%08lx\n",
                          self->m_current_position,
                          self->m_raw_memory,
                          self->m_raw_memory_length );
    return r;
}

variable_types: struct us_simple_allocator_s { 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_base; void *m_raw_memory; int m_raw_memory_length; int m_current_position; } *

variable_types: 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); } *

user_defined_types: struct us_simple_allocator_s { struct us_allocator_s m_base; void *m_raw_memory; int m_raw_memory_length; int m_current_position; }


decompiled_code:
int print_indention()
{
unsigned __int64 v0; // rbx
int result; // eax

if ( s_Level )
{
v0 = 0LL;
do
{
++v0;
result = printf("%s", " ");
}
while ( s_Level > v0 );
}
return result;
}

canonical_decompiled_code:
int func0()
{
unsigned __int64 v0; // rbx
int result; // eax

if ( s_Level )
{
v0 = 0LL;
do
{
++v0;
result = printf("%s", " ");
}
while ( s_Level > v0 );
}
return result;
}

original_code:
static void print_indention()
{
    size_t i;
    for ( i = 0; i < s_Level; ++i )
    {
        printf ( "%s", s_pIndention );
    }
}

canonical_original_code:
static void print_indention()
{
    unsigned long i;
    for ( i = 0; i < s_Level; ++i )
    {
        printf ( "%s", s_pIndention );
    }
}

variable_types:
i
unsigned long