decompiled_code:
void __noreturn pkt_process_thread()
{
__int64 v0; // rax
__int64 v1; // rbx
unsigned __int64 v2; // r12
__int64 v3; // r13
_QWORD *v4; // rax
int v5; // eax

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(0LL);
v1 = v0;
if ( v0 )
break;
usleep(0x3E8u);
}
if ( *(_DWORD *)(v0 + 8) <= 0xCu )
break;
v2 = pcr_get(v0);
if ( v2 )
{
v3 = sx_desc_get();
v4 = malloc(0x10uLL);
if ( !v4 )
_assert_fail("hdr != NULL", "./src/sx_mgmt_m2ts_decoder.c", 0xEBu, "pkt_process_thread");
*(_BYTE *)v4 = 0;
v4[1] = v2;
*(_QWORD *)v3 = v4;
*(_DWORD *)(v3 + 8) = 16;
sx_pipe_put(4LL, v3);
}
v5 = pkt_type_get(v1);
if ( v5 == 1 )
{
sx_pipe_put(1LL, v1);
}
else if ( v5 == 2 )
{
sx_desc_put(v1);
}
else
{
if ( v5 )
_assert_fail("0", "./src/sx_mgmt_m2ts_decoder.c", 0x10Eu, "pkt_process_thread");
sx_pipe_put(5LL, v1);
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_m2ts_decoder.c", 0xDFu, "pkt_process_thread");
}

canonical_decompiled_code:
void __noreturn func3()
{
__int64 v0; // rax
__int64 v1; // rbx
unsigned __int64 v2; // r12
__int64 v3; // r13
_QWORD *v4; // rax
int v5; // eax

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(0LL);
v1 = v0;
if ( v0 )
break;
usleep(0x3E8u);
}
if ( *(_DWORD *)(v0 + 8) <= 0xCu )
break;
v2 = func1(v0);
if ( v2 )
{
v3 = sx_desc_get();
v4 = malloc(0x10uLL);
if ( !v4 )
_assert_fail("hdr != NULL", "./src/sx_mgmt_m2ts_decoder.c", 0xEBu, "pkt_process_thread");
*(_BYTE *)v4 = 0;
v4[1] = v2;
*(_QWORD *)v3 = v4;
*(_DWORD *)(v3 + 8) = 16;
sx_pipe_put(4LL, v3);
}
v5 = func2(v1);
if ( v5 == 1 )
{
sx_pipe_put(1LL, v1);
}
else if ( v5 == 2 )
{
sx_desc_put(v1);
}
else
{
if ( v5 )
_assert_fail("0", "./src/sx_mgmt_m2ts_decoder.c", 0x10Eu, "pkt_process_thread");
sx_pipe_put(5LL, v1);
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_m2ts_decoder.c", 0xDFu, "pkt_process_thread");
}

original_code:
static void pkt_process_thread(
    void * arg
    )
{
    void * *queue;
    sSX_DESC *desc;
    sSX_DESC *h264_desc;
    sSX_DESC *lpcm_desc;
    unsigned int bytes_left;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(0);
            if(desc == ((void *)0))
            {
                break;
            }
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_m2ts_decoder.c", 223, __extension__ __PRETTY_FUNCTION__); }));
            unsigned long long pcr_ms = pcr_get(desc);
            if(pcr_ms > 0)
            {
                sSX_DESC *new_desc = sx_desc_get();
                sSLICE_HDR *hdr = malloc(sizeof(sSLICE_HDR));
                ((void) sizeof ((hdr != ((void *)0)) ? 1 : 0), __extension__ ({ if (hdr != ((void *)0)) ; else __assert_fail ("hdr != NULL", "./src/sx_mgmt_m2ts_decoder.c", 235, __extension__ __PRETTY_FUNCTION__); }));
                hdr->type = 0;
                hdr->timestamp = pcr_ms;
                new_desc->data = (void *) hdr;
                new_desc->data_len = sizeof(sSLICE_HDR);
                sx_pipe_put(4, new_desc);
            }
            ePKT_TYPE pkt_type = pkt_type_get(desc);
            switch(pkt_type)
            {
                case PKT_TYPE_VIDEO:
                {
                    sx_pipe_put(1, desc);
                    break;
                }
                case PKT_TYPE_AUDIO:
                {
                    sx_pipe_put(5, desc);
                    break;
                }
                case PKT_TYPE_NULL:
                {
                    sx_desc_put(desc);
                    break;
                }
                default:
                {
                    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "./src/sx_mgmt_m2ts_decoder.c", 270, __extension__ __PRETTY_FUNCTION__); }));
                    break;
                }
            }
        } while(1);
        usleep(1*1000);
    }
}

canonical_original_code:
static void pkt_process_thread(void *arg)
{
    void **queue;
    struct sSX_DESC *desc;
    struct sSX_DESC *h264_desc;
    struct sSX_DESC *lpcm_desc;
    unsigned int bytes_left;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(0);
            if(desc == ((void *)0))
            {
                break;
            }
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_m2ts_decoder.c", 223, __extension__ __PRETTY_FUNCTION__); }));
            unsigned long long pcr_ms = pcr_get(desc);
            if(pcr_ms > 0)
            {
                struct sSX_DESC *new_desc = sx_desc_get();
                struct sSLICE_HDR *hdr = malloc(sizeof(sSLICE_HDR));
                ((void) sizeof ((hdr != ((void *)0)) ? 1 : 0), __extension__ ({ if (hdr != ((void *)0)) ; else __assert_fail ("hdr != NULL", "./src/sx_mgmt_m2ts_decoder.c", 235, __extension__ __PRETTY_FUNCTION__); }));
                hdr->type = 0;
                hdr->timestamp = pcr_ms;
                new_desc->data = (void *) hdr;
                new_desc->data_len = sizeof(sSLICE_HDR);
                sx_pipe_put(4, new_desc);
            }
            enum ePKT_TYPE pkt_type = pkt_type_get(desc);
            switch(pkt_type)
            {
                case PKT_TYPE_VIDEO:
                {
                    sx_pipe_put(1, desc);
                    break;
                }
                case PKT_TYPE_AUDIO:
                {
                    sx_pipe_put(5, desc);
                    break;
                }
                case PKT_TYPE_NULL:
                {
                    sx_desc_put(desc);
                    break;
                }
                default:
                {
                    ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "./src/sx_mgmt_m2ts_decoder.c", 270, __extension__ __PRETTY_FUNCTION__); }));
                    break;
                }
            }
        } while(1);
        usleep(1*1000);
    }
}

variable_types:
queue
void **

variable_types: struct sSLICE_HDR { unsigned char type; unsigned char rsvd[3]; unsigned long long timestamp; } *

variable_types: enum ePKT_TYPE { PKT_TYPE_AUDIO=0, PKT_TYPE_VIDEO=1, PKT_TYPE_NULL=2 }


repo: codemonkeyricky/piracast

decompiled_code:
int sx_mgmt_m2ts_decoder_init()
{
return puts("(mgmt_m2ts): sx_mgmt_m2ts_decoder_init(): Initialized.");
}

canonical_decompiled_code:
int func4()
{
return puts("(mgmt_m2ts): sx_mgmt_m2ts_decoder_init(): Initialized.");
}

original_code:
void sx_mgmt_m2ts_decoder_init(
    void
    )
{
    printf("(mgmt_m2ts): sx_mgmt_m2ts_decoder_init(): Initialized.\n");
}

canonical_original_code:
void sx_mgmt_m2ts_decoder_init()
{
    printf("(mgmt_m2ts): sx_mgmt_m2ts_decoder_init(): Initialized.\n");
}

return_type: void


decompiled_code:
int sx_mgmt_m2ts_decoder_open()
{
pkt_process_thread_create();
return puts("(mgmt_m2ts_decoder_open): sx_mgmt_m2ts_decoder_open(): Invoked.");
}

canonical_decompiled_code:
int func5()
{
func0();
return puts("(mgmt_m2ts_decoder_open): sx_mgmt_m2ts_decoder_open(): Invoked.");
}

original_code:
void sx_mgmt_m2ts_decoder_open(
    void
    )
{
    pkt_process_thread_create();
    printf("(mgmt_m2ts_decoder_open): sx_mgmt_m2ts_decoder_open(): Invoked.\n");
}

canonical_original_code:
void sx_mgmt_m2ts_decoder_open()
{
    pkt_process_thread_create();
    printf("(mgmt_m2ts_decoder_open): sx_mgmt_m2ts_decoder_open(): Invoked.\n");
}


decompiled_code:
__int64 audio_total_remaining_ms()
{
int v0; // ebx

v0 = sx_pipe_len_get(6LL);
return (unsigned int)sx_audio_sink_ms_left_get() + 10 * v0;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // ebx

v0 = sx_pipe_len_get(6LL);
return (unsigned int)sx_audio_sink_ms_left_get() + 10 * v0;
}

original_code:
static unsigned int audio_total_remaining_ms(
    void
    )
{
    unsigned int data_left = sx_pipe_len_get(6) * 10;
    unsigned int queued_left = sx_audio_sink_ms_left_get();
    return (data_left + queued_left);
}

canonical_original_code:
static unsigned int audio_total_remaining_ms()
{
    unsigned int data_left = sx_pipe_len_get(6) * 10;
    unsigned int queued_left = sx_audio_sink_ms_left_get();
    return (data_left + queued_left);
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall audio_scheduler_slice_dump(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r15
unsigned int v3; // er14
int v4; // esi
_BYTE *v5; // rbx
unsigned int v6; // er12
__int64 v7; // r13
unsigned int v8; // er12
__int16 *v10; // [rsp+10h] [rbp-40h]

v1 = audio_total_remaining_ms();
if ( v1 <= 0xC8 )
{
if ( v1 > 0x95 )
{
if ( playback_speed_1 != 1 )
{
sx_audio_sink_playback_speed_reset();
playback_speed_1 = 1;
}
}
else if ( playback_speed_1 )
{
sx_audio_sink_playback_speed_dec();
playback_speed_1 = 0;
}
}
else if ( playback_speed_1 != 2 )
{
sx_audio_sink_playback_speed_inc();
playback_speed_1 = 2;
}
v10 = (__int16 *)sx_audio_sink_buffer_get();
v2 = a1;
v3 = 0;
do
{
v6 = *(_DWORD *)(v2 + 8);
if ( v6 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_audio_scheduler.c",
0x92u,
"audio_scheduler_slice_dump");
v7 = *(_QWORD *)v2 + 12LL;
v8 = v6 - 12;
if ( v8 != 188 * (v8 / 0xBC) )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_audio_scheduler.c",
0x9Cu,
"audio_scheduler_slice_dump");
v5 = (_BYTE *)(*(_QWORD *)v2 + 12LL);
do
{
if ( ((unsigned __int8)v5[2] | ((unsigned __int8)v5[1] << 8) & 0x1F00) == 4352 )
{
LOBYTE(v4) = 0;
if ( (v5[3] & 0x20) != 0 )
LOBYTE(v4) = v5[4] + 1;
v4 = (unsigned __int8)v4;
if ( (v5[1] & 0x40) != 0 )
v4 = (unsigned __int8)v4 + 20;
memcpy((char *)v10 + v3, &v5[v4 + 4], (unsigned int)(184 - v4));
v3 += 184 - v4;
}
v5 += 188;
}
while ( v8 + (_DWORD)v7 != (_DWORD)v5 );
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
sx_desc_put(a1);
if ( v3 != 1920 )
_assert_fail("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 0xF2u, "audio_scheduler_slice_dump");
audio_endianness_convert(v10, 960);
return sx_audio_sink_buffer_set(v10, 1920LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r15
unsigned int v3; // er14
int v4; // esi
_BYTE *v5; // rbx
unsigned int v6; // er12
__int64 v7; // r13
unsigned int v8; // er12
__int16 *v10; // [rsp+10h] [rbp-40h]

v1 = func0();
if ( v1 <= 0xC8 )
{
if ( v1 > 0x95 )
{
if ( playback_speed_1 != 1 )
{
sx_audio_sink_playback_speed_reset();
playback_speed_1 = 1;
}
}
else if ( playback_speed_1 )
{
sx_audio_sink_playback_speed_dec();
playback_speed_1 = 0;
}
}
else if ( playback_speed_1 != 2 )
{
sx_audio_sink_playback_speed_inc();
playback_speed_1 = 2;
}
v10 = (__int16 *)sx_audio_sink_buffer_get();
v2 = a1;
v3 = 0;
do
{
v6 = *(_DWORD *)(v2 + 8);
if ( v6 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_audio_scheduler.c",
0x92u,
"audio_scheduler_slice_dump");
v7 = *(_QWORD *)v2 + 12LL;
v8 = v6 - 12;
if ( v8 != 188 * (v8 / 0xBC) )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_audio_scheduler.c",
0x9Cu,
"audio_scheduler_slice_dump");
v5 = (_BYTE *)(*(_QWORD *)v2 + 12LL);
do
{
if ( ((unsigned __int8)v5[2] | ((unsigned __int8)v5[1] << 8) & 0x1F00) == 4352 )
{
LOBYTE(v4) = 0;
if ( (v5[3] & 0x20) != 0 )
LOBYTE(v4) = v5[4] + 1;
v4 = (unsigned __int8)v4;
if ( (v5[1] & 0x40) != 0 )
v4 = (unsigned __int8)v4 + 20;
memcpy((char *)v10 + v3, &v5[v4 + 4], (unsigned int)(184 - v4));
v3 += 184 - v4;
}
v5 += 188;
}
while ( v8 + (_DWORD)v7 != (_DWORD)v5 );
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
sx_desc_put(a1);
if ( v3 != 1920 )
_assert_fail("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 0xF2u, "audio_scheduler_slice_dump");
func2(v10, 960);
return sx_audio_sink_buffer_set(v10, 1920LL);
}

original_code:
static void audio_scheduler_slice_dump(
    sSX_DESC *slice_head
    )
{
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    unsigned int afc;
    unsigned int pid;
    unsigned int pes_byte_count;
    unsigned int payload_size;
    unsigned int start_offset;
    unsigned int copy_index;
    unsigned int samples_left;
    unsigned int ms_left;
    sSX_DESC *desc;
    static unsigned char playback_speed = 1;
    ms_left = audio_total_remaining_ms();
    if(ms_left > (100 + (100)))
    {
        if(playback_speed != 2)
        {
            sx_audio_sink_playback_speed_inc();
            playback_speed = 2;
        }
    }
    else if(ms_left < (50 + (100)))
    {
        if(playback_speed != 0)
        {
            sx_audio_sink_playback_speed_dec();
            playback_speed = 0;
        }
    }
    else
    {
        if(playback_speed != 1)
        {
            sx_audio_sink_playback_speed_reset();
            playback_speed = 1;
        }
    }
    unsigned char *buf = sx_audio_sink_buffer_get();
    copy_index = 0;
    desc = slice_head;
    do
    {
        curr_ptr = desc->data;
        bytes_left = desc->data_len;
        ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_scheduler.c", 146, __extension__ __PRETTY_FUNCTION__); }));
        curr_ptr += sizeof(sRTP_HDR);
        bytes_left -= sizeof(sRTP_HDR);
        ((void) sizeof (((bytes_left % sizeof(sMPEG2_TS)) == 0) ? 1 : 0), __extension__ ({ if ((bytes_left % sizeof(sMPEG2_TS)) == 0) ; else __assert_fail ("(bytes_left % sizeof(sMPEG2_TS)) == 0", "./src/sx_mgmt_audio_scheduler.c", 156, __extension__ __PRETTY_FUNCTION__); }));
        pes_byte_count = 0;
        do
        {
            sMPEG2_TS *ts = (sMPEG2_TS *) curr_ptr;
            afc = (((ts->hdr).tsc_afc_cc & 0x30) >> 4);
            pid = ((((ts->hdr).tei_pusi_tp_pid1 & 0x1F) << 8) | (ts->hdr).pid2);
            if(pid == 0x1100)
            {
                unsigned char stuffing = 0;
                if(afc & 0x02)
                {
                    stuffing = 1 + ts->payload.payload[0];
                }
                start_offset = stuffing;
                if((((ts->hdr).tei_pusi_tp_pid1 & 0x40) >> 6))
                {
                     start_offset += 20;
                }
                payload_size = sizeof(sMPEG2_TS_PAYLOAD) - start_offset;
                memcpy(&buf[copy_index],
                        &ts->payload.payload[start_offset],
                        payload_size);
                copy_index += payload_size;
            }
            curr_ptr += sizeof(sMPEG2_TS);
            bytes_left -= sizeof(sMPEG2_TS);
        } while (bytes_left > 0);
        desc = desc->next;
    } while (desc != ((void *)0));
    sx_desc_put(slice_head);
    ((void) sizeof ((copy_index == 1920) ? 1 : 0), __extension__ ({ if (copy_index == 1920) ; else __assert_fail ("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 242, __extension__ __PRETTY_FUNCTION__); }));
    audio_endianness_convert((unsigned short *) buf, 960);
    sx_audio_sink_buffer_set(buf, 1920);
}

canonical_original_code:
static void audio_scheduler_slice_dump(struct sSX_DESC *slice_head)
{
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    unsigned int afc;
    unsigned int pid;
    unsigned int pes_byte_count;
    unsigned int payload_size;
    unsigned int start_offset;
    unsigned int copy_index;
    unsigned int samples_left;
    unsigned int ms_left;
    struct sSX_DESC *desc;
    static unsigned char playback_speed = 1;
    ms_left = audio_total_remaining_ms();
    if(ms_left > (100 + (100)))
    {
        if(playback_speed != 2)
        {
            sx_audio_sink_playback_speed_inc();
            playback_speed = 2;
        }
    }
    else if(ms_left < (50 + (100)))
    {
        if(playback_speed != 0)
        {
            sx_audio_sink_playback_speed_dec();
            playback_speed = 0;
        }
    }
    else
    {
        if(playback_speed != 1)
        {
            sx_audio_sink_playback_speed_reset();
            playback_speed = 1;
        }
    }
    unsigned char *buf = sx_audio_sink_buffer_get();
    copy_index = 0;
    desc = slice_head;
    do
    {
        curr_ptr = desc->data;
        bytes_left = desc->data_len;
        ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_scheduler.c", 146, __extension__ __PRETTY_FUNCTION__); }));
        curr_ptr += sizeof(sRTP_HDR);
        bytes_left -= sizeof(sRTP_HDR);
        ((void) sizeof (((bytes_left % sizeof(sMPEG2_TS)) == 0) ? 1 : 0), __extension__ ({ if ((bytes_left % sizeof(sMPEG2_TS)) == 0) ; else __assert_fail ("(bytes_left % sizeof(sMPEG2_TS)) == 0", "./src/sx_mgmt_audio_scheduler.c", 156, __extension__ __PRETTY_FUNCTION__); }));
        pes_byte_count = 0;
        do
        {
            struct sMPEG2_TS *ts = (sMPEG2_TS *) curr_ptr;
            afc = (((ts->hdr).tsc_afc_cc & 0x30) >> 4);
            pid = ((((ts->hdr).tei_pusi_tp_pid1 & 0x1F) << 8) | (ts->hdr).pid2);
            if(pid == 0x1100)
            {
                unsigned char stuffing = 0;
                if(afc & 0x02)
                {
                    stuffing = 1 + ts->payload.payload[0];
                }
                start_offset = stuffing;
                if((((ts->hdr).tei_pusi_tp_pid1 & 0x40) >> 6))
                {
                     start_offset += 20;
                }
                payload_size = sizeof(sMPEG2_TS_PAYLOAD) - start_offset;
                memcpy(&buf[copy_index],
                        &ts->payload.payload[start_offset],
                        payload_size);
                copy_index += payload_size;
            }
            curr_ptr += sizeof(sMPEG2_TS);
            bytes_left -= sizeof(sMPEG2_TS);
        } while (bytes_left > 0);
        desc = desc->next;
    } while (desc != ((void *)0));
    sx_desc_put(slice_head);
    ((void) sizeof ((copy_index == 1920) ? 1 : 0), __extension__ ({ if (copy_index == 1920) ; else __assert_fail ("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 242, __extension__ __PRETTY_FUNCTION__); }));
    audio_endianness_convert((unsigned short *) buf, 960);
    sx_audio_sink_buffer_set(buf, 1920);
}

variable_types: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; } *

variable_types:
afc
unsigned int

variable_types:
pid
unsigned int

variable_types:
buf
unsigned char *

variable_types: struct sMPEG2_TS { struct sMPEG2_TS_HDR { unsigned char sync_byte; unsigned char tei_pusi_tp_pid1; unsigned char pid2; unsigned char tsc_afc_cc; } hdr; struct sMPEG2_TS_PAYLOAD { unsigned char payload[184]; } payload; } *

variable_types:
stuffing
unsigned char

user_defined_types: struct sMPEG2_TS { struct sMPEG2_TS_HDR hdr; struct sMPEG2_TS_PAYLOAD payload; }


decompiled_code:
void __noreturn audio_scheduler_thread()
{
int v0; // er12
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // er12
__int64 v4; // rdi

while ( 1 )
{
if ( dword_628 )
{
v0 = sx_pipe_len_get(6LL);
v1 = sx_audio_sink_ms_left_get();
if ( v1 + 10 * v0 )
{
if ( v1 <= 0xC7 )
{
v3 = (210 - v1) / 0xA;
do
{
v4 = sx_pipe_get(6LL);
if ( !v4 )
break;
audio_scheduler_slice_dump(v4);
--v3;
}
while ( v3 );
}
}
else
{
dword_628 = 0;
puts("(audio_scheduler): Transition to inactive.");
}
}
else
{
v2 = sx_pipe_len_get(6LL);
if ( v2 > 0xA )
{
dword_628 = 1;
printf("(audio_scheduler): Transition to active. [len = %u]\n", v2);
}
}
usleep(0x1388u);
}
}

canonical_decompiled_code:
void __noreturn func4()
{
int v0; // er12
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // er12
__int64 v4; // rdi

while ( 1 )
{
if ( dword_628 )
{
v0 = sx_pipe_len_get(6LL);
v1 = sx_audio_sink_ms_left_get();
if ( v1 + 10 * v0 )
{
if ( v1 <= 0xC7 )
{
v3 = (210 - v1) / 0xA;
do
{
v4 = sx_pipe_get(6LL);
if ( !v4 )
break;
func3(v4);
--v3;
}
while ( v3 );
}
}
else
{
dword_628 = 0;
puts("(audio_scheduler): Transition to inactive.");
}
}
else
{
v2 = sx_pipe_len_get(6LL);
if ( v2 > 0xA )
{
dword_628 = 1;
printf("(audio_scheduler): Transition to active. [len = %u]\n", v2);
}
}
usleep(0x1388u);
}
}

original_code:
void audio_scheduler_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    while(1)
    {
        if(f_cblk.state == STATE_INACTIVE)
        {
            unsigned int len = sx_pipe_len_get(6);
            if(len > ((100) / 10))
            {
                f_cblk.state = STATE_ACTIVE;
                printf("(audio_scheduler): Transition to active. [len = %u]\n", len);
                goto next_iter;
            }
        }
        else
        {
            unsigned int data_left_ms = sx_pipe_len_get(6) * 10;
            unsigned int queued_ms = sx_audio_sink_ms_left_get();
            if((data_left_ms + queued_ms) == 0)
            {
                f_cblk.state = STATE_INACTIVE;
                printf("(audio_scheduler): Transition to inactive.\n");
                goto next_iter;
            }
            if(queued_ms < 200)
            {
                unsigned int slices_to_queue = (200 - queued_ms + 10) / 10;
                do
                {
                    desc = sx_pipe_get(6);
                    if(desc == ((void *)0))
                    {
                        goto next_iter;
                    }
                    audio_scheduler_slice_dump(desc);
                    slices_to_queue--;
                } while(slices_to_queue > 0);
            }
        }
        next_iter:
        usleep(5*1000);
    }
}

canonical_original_code:
void audio_scheduler_thread(void *arg)
{
    struct sSX_DESC *desc;
    while(1)
    {
        if(f_cblk.state == STATE_INACTIVE)
        {
            unsigned int len = sx_pipe_len_get(6);
            if(len > ((100) / 10))
            {
                f_cblk.state = STATE_ACTIVE;
                printf("(audio_scheduler): Transition to active. [len = %u]\n", len);
                goto next_iter;
            }
        }
        else
        {
            unsigned int data_left_ms = sx_pipe_len_get(6) * 10;
            unsigned int queued_ms = sx_audio_sink_ms_left_get();
            if((data_left_ms + queued_ms) == 0)
            {
                f_cblk.state = STATE_INACTIVE;
                printf("(audio_scheduler): Transition to inactive.\n");
                goto next_iter;
            }
            if(queued_ms < 200)
            {
                unsigned int slices_to_queue = (200 - queued_ms + 10) / 10;
                do
                {
                    desc = sx_pipe_get(6);
                    if(desc == ((void *)0))
                    {
                        goto next_iter;
                    }
                    audio_scheduler_slice_dump(desc);
                    slices_to_queue--;
                } while(slices_to_queue > 0);
            }
        }
        next_iter:
        usleep(5*1000);
    }
}

variable_types:
arg
void *

variable_types:
len
unsigned int

user_defined_types: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; }


decompiled_code:
__int64 audio_scheduler_thread_create()
{
return sx_thread_create(&f_cblk, audio_scheduler_thread, 0LL, 60LL);
}

canonical_decompiled_code:
__int64 func6()
{
return sx_thread_create(&f_cblk, audio_scheduler_thread, 0LL, 60LL);
}

original_code:
static void audio_scheduler_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.audio_scheduler_thread,
                     &audio_scheduler_thread,
                     ((void *)0),
                     60);
}

canonical_original_code:
static void audio_scheduler_thread_create()
{
    sx_thread_create(&f_cblk.audio_scheduler_thread,
                     &audio_scheduler_thread,
                     ((void *)0),
                     60);
}


decompiled_code:
int sx_mgmt_audio_scheduler_init()
{
resources_init();
return puts("(audio_scheduler): Init.");
}

canonical_decompiled_code:
int func7()
{
func5();
return puts("(audio_scheduler): Init.");
}

original_code:
void sx_mgmt_audio_scheduler_init(
    void
    )
{
    resources_init();
    printf("(audio_scheduler): Init.\n");
}

canonical_original_code:
void sx_mgmt_audio_scheduler_init()
{
    resources_init();
    printf("(audio_scheduler): Init.\n");
}


decompiled_code:
int sx_mgmt_audio_scheduler_open()
{
audio_scheduler_thread_create();
return puts("(audio_scheduler): Open.");
}

canonical_decompiled_code:
int func8()
{
func6();
return puts("(audio_scheduler): Open.");
}

original_code:
void sx_mgmt_audio_scheduler_open(
    void
    )
{
    audio_scheduler_thread_create();
    printf("(audio_scheduler): Open.\n");
}

canonical_original_code:
void sx_mgmt_audio_scheduler_open()
{
    audio_scheduler_thread_create();
    printf("(audio_scheduler): Open.\n");
}


decompiled_code:
void sx_mgmt_audio_scheduler_close()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

original_code:
void sx_mgmt_audio_scheduler_close(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_audio_scheduler_close()
{
}


decompiled_code:
__int64 resources_init()
{
return sx_audio_sink_init();
}

canonical_decompiled_code:
__int64 func5()
{
return sx_audio_sink_init();
}

original_code:
static void resources_init(
    void
    )
{
    sx_audio_sink_init();
}

canonical_original_code:
static void resources_init()
{
    sx_audio_sink_init();
}


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

result = qword_710;
qword_710 = 0LL;
qword_718 = 0LL;
return result;
}

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

result = qword_710;
qword_710 = 0LL;
qword_718 = 0LL;
return result;
}

original_code:
static sSX_DESC * slice_get(
    void
    )
{
    sSX_DESC *head;
    head = f_cblk.slice_chain.head;
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    return head;
}

canonical_original_code:
static struct sSX_DESC *slice_get()
{
    struct sSX_DESC *head;
    head = f_cblk.slice_chain.head;
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    return head;
}

return_type: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; } *


decompiled_code:
__int64 __fastcall video_decoder_slice_dump(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 v4; // rax

v2 = malloc(0x10uLL);
v3 = v2;
if ( !v2 )
_assert_fail("hdr != NULL", "./src/sx_mgmt_video_decoder.c", 0x59u, "video_decoder_slice_dump");
*(_BYTE *)v2 = 1;
v2[1] = a1;
v4 = sx_desc_get();
*(_QWORD *)v4 = v3;
*(_DWORD *)(v4 + 8) = 16;
*(_QWORD *)(v4 + 16) = a2;
return sx_pipe_put(2LL, v4);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 v4; // rax

v2 = malloc(0x10uLL);
v3 = v2;
if ( !v2 )
_assert_fail("hdr != NULL", "./src/sx_mgmt_video_decoder.c", 0x59u, "video_decoder_slice_dump");
*(_BYTE *)v2 = 1;
v2[1] = a1;
v4 = sx_desc_get();
*(_QWORD *)v4 = v3;
*(_DWORD *)(v4 + 8) = 16;
*(_QWORD *)(v4 + 16) = a2;
return sx_pipe_put(2LL, v4);
}

original_code:
static void video_decoder_slice_dump(
    unsigned long long pts_ms,
    sSX_DESC *slice_head
    )
{
    sSLICE_HDR *hdr = malloc(sizeof(sSLICE_HDR));
    ((void) sizeof ((hdr != ((void *)0)) ? 1 : 0), __extension__ ({ if (hdr != ((void *)0)) ; else __assert_fail ("hdr != NULL", "./src/sx_mgmt_video_decoder.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    hdr->type = 1;
    hdr->timestamp = pts_ms;
    sSX_DESC *new_desc = sx_desc_get();
    new_desc->data = (void *) hdr;
    new_desc->data_len = sizeof(sSLICE_HDR);
    new_desc->next = slice_head;
    sx_pipe_put(2, new_desc);
}

canonical_original_code:
static void video_decoder_slice_dump(unsigned long long pts_ms, struct sSX_DESC *slice_head)
{
    struct sSLICE_HDR *hdr = malloc(sizeof(sSLICE_HDR));
    ((void) sizeof ((hdr != ((void *)0)) ? 1 : 0), __extension__ ({ if (hdr != ((void *)0)) ; else __assert_fail ("hdr != NULL", "./src/sx_mgmt_video_decoder.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    hdr->type = 1;
    hdr->timestamp = pts_ms;
    struct sSX_DESC *new_desc = sx_desc_get();
    new_desc->data = (void *) hdr;
    new_desc->data_len = sizeof(sSLICE_HDR);
    new_desc->next = slice_head;
    sx_pipe_put(2, new_desc);
}

user_defined_types: struct sSLICE_HDR { unsigned char type; unsigned char rsvd[3]; unsigned long long timestamp; }


decompiled_code:
int slice_drop()
{
sx_desc_put(qword_710);
qword_710 = 0LL;
qword_718 = 0LL;
return puts("slice_dropped() invoked");
}

canonical_decompiled_code:
int func5()
{
sx_desc_put(qword_710);
qword_710 = 0LL;
qword_718 = 0LL;
return puts("slice_dropped() invoked");
}

original_code:
static void slice_drop(
    void
    )
{
    sSX_DESC *curr;
    sSX_DESC *next;
    sx_desc_put(f_cblk.slice_chain.head);
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    printf("slice_dropped() invoked\n");
}

canonical_original_code:
static void slice_drop()
{
    struct sSX_DESC *curr;
    struct sSX_DESC *next;
    sx_desc_put(f_cblk.slice_chain.head);
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    printf("slice_dropped() invoked\n");
}


decompiled_code:
void __noreturn video_decoder_thread()
{
unsigned int *v0; // rax
unsigned int *v1; // rbx
__int64 v2; // r12
char v3; // al
char v4; // r8
__int64 v5; // rax
unsigned __int16 v6; // ax
unsigned __int64 v7; // [rsp+8h] [rbp-28h] BYREF
unsigned int v8; // [rsp+14h] [rbp-1Ch] BYREF
unsigned __int8 v9; // [rsp+1Ah] [rbp-16h] BYREF
unsigned __int8 v10; // [rsp+1Bh] [rbp-15h] BYREF
unsigned int v11[5]; // [rsp+1Ch] [rbp-14h] BYREF

while ( 1 )
{
while ( 1 )
{
v0 = (unsigned int *)sx_pipe_get(1LL);
v1 = v0;
if ( v0 )
break;
usleep(0x1F40u);
}
v2 = *(_QWORD *)v0;
if ( v0[2] <= 0xC )
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 0x1C0u, "video_decoder_thread");
v3 = slice_start_find(v0, v11, &v7);
if ( dword_6E8 )
break;
if ( v3 )
goto LABEL_13;
LABEL_4:
m2ts_data_interpret(v1, &v8, &v10, &v9, v4);
dword_6EC += v8;
if ( !dword_6F4 )
{
dword_6F0 = v9;
LABEL_6:
slice_pkt_add((__int64)v1);
++dword_6F4;
goto LABEL_7;
}
if ( (((_BYTE)dword_6F0 + 1) & 0xF) == v10 )
{
dword_6F0 = v9;
goto LABEL_6;
}
printf("(video_decoder): last rtp seq num = %d\n", (unsigned __int16)word_708);
v6 = _bswap_16(*(_WORD *)(v2 + 2));
printf("(video_decoder): curr rtp seq num = %d\n", v6);
printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n", (unsigned int)dword_6F0, v10);
slice_drop();
dword_6E8 = 1;
dword_6F4 = 0;
LABEL_7:
word_708 = _bswap_16(*(_WORD *)(v2 + 2));
}
if ( !v3 )
goto LABEL_7;
dword_6E8 = 0;
LABEL_13:
if ( !qword_700 )
qword_6F8 = v7;
if ( dword_6F4 )
{
v5 = slice_get();
video_decoder_slice_dump(qword_6F8, v5);
dword_6F4 = 0;
dword_6EC = 0;
qword_6F8 = v7;
}
++qword_700;
goto LABEL_4;
}

canonical_decompiled_code:
void __noreturn func8()
{
unsigned int *v0; // rax
unsigned int *v1; // rbx
__int64 v2; // r12
char v3; // al
char v4; // r8
__int64 v5; // rax
unsigned __int16 v6; // ax
unsigned __int64 v7; // [rsp+8h] [rbp-28h] BYREF
unsigned int v8; // [rsp+14h] [rbp-1Ch] BYREF
unsigned __int8 v9; // [rsp+1Ah] [rbp-16h] BYREF
unsigned __int8 v10; // [rsp+1Bh] [rbp-15h] BYREF
unsigned int v11[5]; // [rsp+1Ch] [rbp-14h] BYREF

while ( 1 )
{
while ( 1 )
{
v0 = (unsigned int *)sx_pipe_get(1LL);
v1 = v0;
if ( v0 )
break;
usleep(0x1F40u);
}
v2 = *(_QWORD *)v0;
if ( v0[2] <= 0xC )
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 0x1C0u, "video_decoder_thread");
v3 = func7(v0, v11, &v7);
if ( dword_6E8 )
break;
if ( v3 )
goto LABEL_13;
LABEL_4:
func2(v1, &v8, &v10, &v9, v4);
dword_6EC += v8;
if ( !dword_6F4 )
{
dword_6F0 = v9;
LABEL_6:
func3((__int64)v1);
++dword_6F4;
goto LABEL_7;
}
if ( (((_BYTE)dword_6F0 + 1) & 0xF) == v10 )
{
dword_6F0 = v9;
goto LABEL_6;
}
printf("(video_decoder): last rtp seq num = %d\n", (unsigned __int16)word_708);
v6 = func6(*(_WORD *)(v2 + 2));
printf("(video_decoder): curr rtp seq num = %d\n", v6);
printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n", (unsigned int)dword_6F0, v10);
func5();
dword_6E8 = 1;
dword_6F4 = 0;
LABEL_7:
word_708 = func6(*(_WORD *)(v2 + 2));
}
if ( !v3 )
goto LABEL_7;
dword_6E8 = 0;
LABEL_13:
if ( !qword_700 )
qword_6F8 = v7;
if ( dword_6F4 )
{
v5 = func0();
func4(qword_6F8, v5);
dword_6F4 = 0;
dword_6EC = 0;
qword_6F8 = v7;
}
++qword_700;
goto LABEL_4;
}

original_code:
void video_decoder_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    sSX_DESC *slice_start;
    sSX_DESC *slice_end;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    sMPEG2_TS *ts;
    unsigned int pes_payload_len;
    unsigned char slice_start_found;
    unsigned char cc_start;
    unsigned char cc_end;
    unsigned int pes_payload_size;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(1);
            if(desc == ((void *)0))
            {
                break;
            }
            curr_ptr = desc->data;
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 448, __extension__ __PRETTY_FUNCTION__); }));
            sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            curr_ptr += sizeof(sRTP_HDR);
            ts = (sMPEG2_TS *) curr_ptr;
            unsigned long long pts_ms;
            slice_start_found = slice_start_find(desc, &pes_payload_len, &pts_ms);
            if(f_cblk.look_for_new_slice)
            {
                if(!slice_start_found)
                {
                    goto cleanup;
                }
                f_cblk.look_for_new_slice = 0;
            }
            if(slice_start_found)
            {
                if(f_cblk.slice_count == 0)
                {
                    f_cblk.slice_pts_ms = pts_ms;
                }
                if(f_cblk.slice_pkt_count > 0)
                {
                    video_decoder_slice_dump(f_cblk.slice_pts_ms, slice_get());
                    f_cblk.slice_pkt_count = 0;
                    f_cblk.pes_payload_curr_len = 0;
                    f_cblk.slice_pts_ms = pts_ms;
                }
                f_cblk.slice_count++;
            }
            m2ts_data_interpret(desc, &pes_payload_size, &cc_start, &cc_end);
            f_cblk.pes_payload_curr_len += pes_payload_size;
            if(f_cblk.slice_pkt_count == 0)
            {
                f_cblk.last_cc = cc_end;
            }
            else
            {
                if(((f_cblk.last_cc + 1) & 0x0F) == cc_start)
                {
                    f_cblk.last_cc = cc_end;
                }
                else
                {
                    printf("(video_decoder): last rtp seq num = %d\n", f_cblk.last_seq_num);
                    printf("(video_decoder): curr rtp seq num = %d\n", ntohs(rtp_hdr->sequence_num));
                    printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n",
                            f_cblk.last_cc, cc_start);
                    slice_drop();
                    f_cblk.look_for_new_slice = 1;
                    f_cblk.slice_pkt_count = 0;
                    goto cleanup;
                }
            }
            slice_pkt_add(desc);
            f_cblk.slice_pkt_count++;
            cleanup:
            f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
        } while (1);
        usleep(8*1000);
    }
}

canonical_original_code:
void video_decoder_thread(void *arg)
{
    struct sSX_DESC *desc;
    struct sSX_DESC *slice_start;
    struct sSX_DESC *slice_end;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    struct sMPEG2_TS *ts;
    unsigned int pes_payload_len;
    unsigned char slice_start_found;
    unsigned char cc_start;
    unsigned char cc_end;
    unsigned int pes_payload_size;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(1);
            if(desc == ((void *)0))
            {
                break;
            }
            curr_ptr = desc->data;
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 448, __extension__ __PRETTY_FUNCTION__); }));
            struct sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            curr_ptr += sizeof(sRTP_HDR);
            ts = (struct sMPEG2_TS *) curr_ptr;
            unsigned long long pts_ms;
            slice_start_found = slice_start_find(desc, &pes_payload_len, &pts_ms);
            if(f_cblk.look_for_new_slice)
            {
                if(!slice_start_found)
                {
                    goto cleanup;
                }
                f_cblk.look_for_new_slice = 0;
            }
            if(slice_start_found)
            {
                if(f_cblk.slice_count == 0)
                {
                    f_cblk.slice_pts_ms = pts_ms;
                }
                if(f_cblk.slice_pkt_count > 0)
                {
                    video_decoder_slice_dump(f_cblk.slice_pts_ms, slice_get());
                    f_cblk.slice_pkt_count = 0;
                    f_cblk.pes_payload_curr_len = 0;
                    f_cblk.slice_pts_ms = pts_ms;
                }
                f_cblk.slice_count++;
            }
            m2ts_data_interpret(desc, &pes_payload_size, &cc_start, &cc_end);
            f_cblk.pes_payload_curr_len += pes_payload_size;
            if(f_cblk.slice_pkt_count == 0)
            {
                f_cblk.last_cc = cc_end;
            }
            else
            {
                if(((f_cblk.last_cc + 1) & 0x0F) == cc_start)
                {
                    f_cblk.last_cc = cc_end;
                }
                else
                {
                    printf("(video_decoder): last rtp seq num = %d\n", f_cblk.last_seq_num);
                    printf("(video_decoder): curr rtp seq num = %d\n", ntohs(rtp_hdr->sequence_num));
                    printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n",
                            f_cblk.last_cc, cc_start);
                    slice_drop();
                    f_cblk.look_for_new_slice = 1;
                    f_cblk.slice_pkt_count = 0;
                    goto cleanup;
                }
            }
            slice_pkt_add(desc);
            f_cblk.slice_pkt_count++;
            cleanup:
            f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
        } while (1);
        usleep(8*1000);
    }
}

variable_types: struct sRTP_HDR { unsigned char version_p_x_cc; unsigned char m_pt; unsigned short sequence_num; unsigned int timestamp; unsigned int ssrc_id; } *

user_defined_types: struct sMPEG2_TS_HDR { unsigned char sync_byte; unsigned char tei_pusi_tp_pid1; unsigned char pid2; unsigned char tsc_afc_cc; }

user_defined_types: struct sMPEG2_TS_PAYLOAD { unsigned char payload[184]; }


decompiled_code:
void sx_mgmt_video_decoder_init()
{
resources_init();
}

canonical_decompiled_code:
void func10()
{
func1();
}

original_code:
void sx_mgmt_video_decoder_init(
    void
    )
{
    resources_init();
}


decompiled_code:
__int64 sx_mgmt_video_decoder_open()
{
return decoder_thread_create();
}

canonical_decompiled_code:
__int64 func11()
{
return func9();
}

original_code:
void sx_mgmt_video_decoder_open(
    void
    )
{
    decoder_thread_create();
}

canonical_original_code:
void sx_mgmt_video_decoder_open()
{
    decoder_thread_create();
}


decompiled_code:
void sx_mgmt_video_decoder_close()
{
;
}

canonical_decompiled_code:
void func12()
{
;
}

original_code:
void sx_mgmt_video_decoder_close(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_video_decoder_close()
{
}


decompiled_code:
void sx_mgmt_ctl_init()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void sx_mgmt_ctl_init(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_ctl_init()
{
}


decompiled_code:
__int64 __fastcall connection_handler(int fd)
{
char buf[264]; // [rsp+0h] [rbp-110h] BYREF

puts("mgmt_ctl_wait(): Reading incoming message...");
buf[(int)read(fd, buf, 0x100uLL)] = 0;
printf("MESSAGE FROM CLIENT: %s\n", buf);
close(fd);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd)
{
char buf[264]; // [rsp+0h] [rbp-110h] BYREF

puts("mgmt_ctl_wait(): Reading incoming message...");
buf[(int)read(fd, buf, 0x100uLL)] = 0;
printf("MESSAGE FROM CLIENT: %s\n", buf);
close(fd);
return 0LL;
}

original_code:
int connection_handler(
    int connection_fd
    )
{
    int nbytes;
    char buffer[256];
    printf("mgmt_ctl_wait(): Reading incoming message...\n");
    nbytes = read(connection_fd, buffer, 256);
    buffer[nbytes] = 0;
    printf("MESSAGE FROM CLIENT: %s\n", buffer);
    close(connection_fd);
    return 0;
}

canonical_original_code:
int connection_handler(int connection_fd)
{
    int nbytes;
    char buffer[256];
    printf("mgmt_ctl_wait(): Reading incoming message...\n");
    nbytes = read(connection_fd, buffer, 256);
    buffer[nbytes] = 0;
    printf("MESSAGE FROM CLIENT: %s\n", buffer);
    close(connection_fd);
    return 0;
}

variable_types: nbytes int

variable_types:
buffer
char [256]

return_type: int


decompiled_code:
int sx_mgmt_ctl_wait()
{
int v0; // ebx
int v1; // er12
socklen_t addr_len; // [rsp+Ch] [rbp-84h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-80h] BYREF
char v5[88]; // [rsp+20h] [rbp-70h] BYREF
int v6; // [rsp+78h] [rbp-18h]
__int16 v7; // [rsp+7Ch] [rbp-14h]

v0 = socket(1, 1, 0);
if ( v0 < 0 )
return puts("socket() failed");
unlink("./demo_socket");
memset(v5, 0, sizeof(v5));
v6 = 0;
v7 = 0;
addr.sa_family = 1;
strcpy(addr.sa_data, "./demo_socket");
if ( bind(v0, &addr, 0x6Eu) )
return puts("bind() failed");
puts("mgmt_ctl_wait(): Waiting for incoming connection...");
if ( listen(v0, 5) )
return puts("listen() failed");
puts("mgmt_ctl_wait(): Accept new connection.");
v1 = accept(v0, &addr, &addr_len);
connection_handler(v1);
close(v1);
close(v0);
return unlink("./demo_socket");
}

canonical_decompiled_code:
int func2()
{
int v0; // ebx
int v1; // er12
socklen_t addr_len; // [rsp+Ch] [rbp-84h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-80h] BYREF
char v5[88]; // [rsp+20h] [rbp-70h] BYREF
int v6; // [rsp+78h] [rbp-18h]
__int16 v7; // [rsp+7Ch] [rbp-14h]

v0 = socket(1, 1, 0);
if ( v0 < 0 )
return puts("socket() failed");
unlink("./demo_socket");
memset(v5, 0, sizeof(v5));
v6 = 0;
v7 = 0;
addr.sa_family = 1;
strcpy(addr.sa_data, "./demo_socket");
if ( bind(v0, &addr, 0x6Eu) )
return puts("bind() failed");
puts("mgmt_ctl_wait(): Waiting for incoming connection...");
if ( listen(v0, 5) )
return puts("listen() failed");
puts("mgmt_ctl_wait(): Accept new connection.");
v1 = accept(v0, &addr, &addr_len);
func1(v1);
close(v1);
close(v0);
return unlink("./demo_socket");
}

original_code:
void sx_mgmt_ctl_wait(
    void
    )
{
    struct sockaddr_un address;
    int socket_fd, connection_fd;
    socklen_t address_length;
    pid_t child;
    socket_fd = socket(1, SOCK_STREAM, 0);
    if(socket_fd < 0)
    {
        printf("socket() failed\n");
        return;
    }
    unlink("./demo_socket");
    memset(&address, 0, sizeof(struct sockaddr_un));
    address.sun_family = 1;
    snprintf(address.sun_path, 108, "./demo_socket");
    if(bind(socket_fd,
            (struct sockaddr *) &address,
            sizeof(struct sockaddr_un)) != 0)
    {
        printf("bind() failed\n");
        return;
    }
    printf("mgmt_ctl_wait(): Waiting for incoming connection...\n");
    if(listen(socket_fd, 5) != 0)
    {
        printf("listen() failed\n");
        return;
    }
    printf("mgmt_ctl_wait(): Accept new connection.\n");
    connection_fd = accept(socket_fd,
                           (struct sockaddr *) &address,
                           &address_length);
    connection_handler(connection_fd);
    close(connection_fd);
    close(socket_fd);
    unlink("./demo_socket");
}

canonical_original_code:
void sx_mgmt_ctl_wait()
{
    struct sockaddr_un address;
    int socket_fd; int connection_fd;
    unsigned int address_length;
    int child;
    socket_fd = socket(1, SOCK_STREAM, 0);
    if(socket_fd < 0)
    {
        printf("socket() failed\n");
        return;
    }
    unlink("./demo_socket");
    memset(&address, 0, sizeof(struct sockaddr_un));
    address.sun_family = 1;
    snprintf(address.sun_path, 108, "./demo_socket");
    if(bind(socket_fd,
            (struct sockaddr *) &address,
            sizeof(struct sockaddr_un)) != 0)
    {
        printf("bind() failed\n");
        return;
    }
    printf("mgmt_ctl_wait(): Waiting for incoming connection...\n");
    if(listen(socket_fd, 5) != 0)
    {
        printf("listen() failed\n");
        return;
    }
    printf("mgmt_ctl_wait(): Accept new connection.\n");
    connection_fd = accept(socket_fd,
                           (struct sockaddr *) &address,
                           &address_length);
    connection_handler(connection_fd);
    close(connection_fd);
    close(socket_fd);
    unlink("./demo_socket");
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: child int

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 estimated_source_time_get()
{
__int64 v0; // rbx

pthread_mutex_lock(&mutex);
v0 = qword_6F8;
pthread_mutex_unlock(&mutex);
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx

pthread_mutex_lock(&mutex);
v0 = qword_6F8;
pthread_mutex_unlock(&mutex);
return v0;
}

original_code:
static unsigned long long estimated_source_time_get(
    void
    )
{
    unsigned long long time;
    pthread_mutex_lock(&f_cblk.lock);
    time = f_cblk.curr_time;
    pthread_mutex_unlock(&f_cblk.lock);
    return time;
}

canonical_original_code:
static unsigned long long estimated_source_time_get()
{
    unsigned long long time;
    pthread_mutex_lock(&f_cblk.lock);
    time = f_cblk.curr_time;
    pthread_mutex_unlock(&f_cblk.lock);
    return time;
}

variable_types:
time
unsigned long long

return_type: unsigned long long


decompiled_code:
void __fastcall __noreturn video_scheduler_thread()
{
unsigned __int64 v0; // r13
__int64 v1; // r12
_QWORD *v2; // rbx
void *v3; // rdi
__int64 v4; // rax

v1 = 0LL;
while ( 1 )
{
if ( !v1 )
goto LABEL_8;
while ( v0 < estimated_source_time_get() )
{
v2 = *(_QWORD **)(v1 + 16);
do
{
sx_video_sink_buf_set(*v2);
v3 = v2;
v2 = (_QWORD *)v2[2];
free(v3);
}
while ( v2 );
free(*(void **)v1);
free((void *)v1);
LABEL_8:
v4 = sx_pipe_get(3LL);
v1 = v4;
if ( !v4 )
break;
v0 = *(_QWORD *)(*(_QWORD *)v4 + 8LL);
}
usleep(0x3E8u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1()
{
unsigned __int64 v0; // r13
__int64 v1; // r12
_QWORD *v2; // rbx
void *v3; // rdi
__int64 v4; // rax

v1 = 0LL;
while ( 1 )
{
if ( !v1 )
goto LABEL_8;
while ( v0 < func0() )
{
v2 = *(_QWORD **)(v1 + 16);
do
{
sx_video_sink_buf_set(*v2);
v3 = v2;
v2 = (_QWORD *)v2[2];
free(v3);
}
while ( v2 );
free(*(void **)v1);
free((void *)v1);
LABEL_8:
v4 = sx_pipe_get(3LL);
v1 = v4;
if ( !v4 )
break;
v0 = *(_QWORD *)(*(_QWORD *)v4 + 8LL);
}
usleep(0x3E8u);
}
}

original_code:
void video_scheduler_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    unsigned long long slice_present_time;
    desc = ((void *)0);
    while(1)
    {
        while(1)
        {
            if(desc == ((void *)0))
            {
                desc = sx_pipe_get(3);
                if(desc == ((void *)0))
                {
                    goto next_iter;
                }
                sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data;
                slice_present_time = ((sSLICE_HDR *) desc->data)->timestamp;
            }
            unsigned long long estimated_source_time = estimated_source_time_get();
            unsigned char present = (estimated_source_time > slice_present_time) ? 1 : 0;
            if(!present)
            {
                goto next_iter;
            }
            sSX_DESC *curr = desc->next;
            sSX_DESC *next;
            do
            {
                sx_video_sink_buf_set((sDECODER_HW_BUFFER *) curr->data);
                next = curr->next;
                free(curr);
                curr = next;
            } while (curr != ((void *)0));
            free(desc->data);
            free(desc);
            desc = ((void *)0);
        }
        next_iter:
        usleep(1*1000);
    }
}

canonical_original_code:
void video_scheduler_thread(void *arg)
{
    struct sSX_DESC *desc;
    unsigned long long slice_present_time;
    desc = ((void *)0);
    while(1)
    {
        while(1)
        {
            if(desc == ((void *)0))
            {
                desc = sx_pipe_get(3);
                if(desc == ((void *)0))
                {
                    goto next_iter;
                }
                struct sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data;
                slice_present_time = ((struct sSLICE_HDR *) desc->data)->timestamp;
            }
            unsigned long long estimated_source_time = estimated_source_time_get();
            unsigned char present = (estimated_source_time > slice_present_time) ? 1 : 0;
            if(!present)
            {
                goto next_iter;
            }
            struct sSX_DESC *curr = desc->next;
            struct sSX_DESC *next;
            do
            {
                sx_video_sink_buf_set((struct sDECODER_HW_BUFFER *) curr->data);
                next = curr->next;
                free(curr);
                curr = next;
            } while (curr != ((void *)0));
            free(desc->data);
            free(desc);
            desc = ((void *)0);
        }
        next_iter:
        usleep(1*1000);
    }
}

variable_types:
present
unsigned char

user_defined_types: struct sDECODER_HW_BUFFER { unsigned int reserve1; unsigned int reserved2; unsigned char *buffer; unsigned int reserved3; unsigned int buffer_len; unsigned int reserved4; unsigned int reserved5; unsigned int reserved6; unsigned int reserved7; unsigned int reserved8; unsigned int reserved9; unsigned int reserved10; unsigned int reserved11; unsigned int reserved12; unsigned int reserved13; unsigned int reserved14; unsigned int reserved15; }


decompiled_code:
__int64 __fastcall video_scheduler_slice_dump(__int64 a1)
{
__int64 v1; // r14
unsigned int v2; // er15
_BYTE *v3; // rbx
int v4; // er12
unsigned int v5; // er13
__int64 *v6; // r15
__int64 v7; // rax
unsigned int v8; // eax
unsigned int v9; // er13
unsigned int v10; // eax
__int64 *v12; // [rsp+8h] [rbp-58h]
__int64 v13; // [rsp+10h] [rbp-50h]
__int64 *v14; // [rsp+18h] [rbp-48h]
unsigned int v15; // [rsp+20h] [rbp-40h]
unsigned int v16; // [rsp+24h] [rbp-3Ch]
__int64 v17; // [rsp+28h] [rbp-38h]

if ( !a1 )
_assert_fail("slice_head", "./src/sx_mgmt_video_scheduler.c", 0x42u, "video_scheduler_slice_dump");
v12 = (__int64 *)sx_desc_get();
v1 = sx_video_sink_buf_get();
if ( !v1 )
_assert_fail("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 0x4Cu, "video_scheduler_slice_dump");
*v12 = v1;
v14 = v12;
v13 = a1;
v2 = 0;
do
{
v13 = *(_QWORD *)(v13 + 16);
v8 = *(_DWORD *)(v13 + 8);
if ( v8 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_video_scheduler.c",
0x61u,
"video_scheduler_slice_dump");
v17 = *(_QWORD *)v13 + 12LL;
v9 = v8 - 12;
v10 = 188 * ((v8 - 12) / 0xBC);
v15 = v9 - v10;
if ( v9 != v10 )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_video_scheduler.c",
0x6Bu,
"video_scheduler_slice_dump");
v3 = (_BYTE *)(*(_QWORD *)v13 + 12LL);
v16 = v9;
do
{
if ( ((unsigned __int8)v3[2] | ((unsigned __int8)v3[1] << 8) & 0x1F00) == 4113 )
{
LOBYTE(v4) = 0;
if ( (v3[3] & 0x20) != 0 )
LOBYTE(v4) = v3[4] + 1;
v4 = (unsigned __int8)v4;
if ( (v3[1] & 0x40) != 0 )
v4 = (unsigned __int8)v4 + 14;
v5 = 184 - v4;
if ( v2 + 184 - v4 > 0x14000 )
{
*(_DWORD *)(v1 + 20) = v2;
v6 = (__int64 *)sx_desc_get();
v14[2] = (__int64)v6;
v7 = sx_video_sink_buf_get();
v1 = v7;
if ( !v7 )
_assert_fail("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 0x92u, "video_scheduler_slice_dump");
*v6 = v7;
v14 = v6;
v2 = v15;
}
memcpy((void *)(*(_QWORD *)(v1 + 8) + v2), &v3[v4 + 4], v5);
v2 += v5;
}
v3 += 188;
}
while ( (_DWORD)v17 + v16 != (_DWORD)v3 );
}
while ( *(_QWORD *)(v13 + 16) );
*(_DWORD *)(v1 + 20) = v2;
sx_desc_put(*(_QWORD *)(a1 + 16));
*(_QWORD *)(a1 + 16) = v12;
return sx_pipe_put(3LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r14
unsigned int v2; // er15
_BYTE *v3; // rbx
int v4; // er12
unsigned int v5; // er13
__int64 *v6; // r15
__int64 v7; // rax
unsigned int v8; // eax
unsigned int v9; // er13
unsigned int v10; // eax
__int64 *v12; // [rsp+8h] [rbp-58h]
__int64 v13; // [rsp+10h] [rbp-50h]
__int64 *v14; // [rsp+18h] [rbp-48h]
unsigned int v15; // [rsp+20h] [rbp-40h]
unsigned int v16; // [rsp+24h] [rbp-3Ch]
__int64 v17; // [rsp+28h] [rbp-38h]

if ( !a1 )
_assert_fail("slice_head", "./src/sx_mgmt_video_scheduler.c", 0x42u, "video_scheduler_slice_dump");
v12 = (__int64 *)sx_desc_get();
v1 = sx_video_sink_buf_get();
if ( !v1 )
_assert_fail("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 0x4Cu, "video_scheduler_slice_dump");
*v12 = v1;
v14 = v12;
v13 = a1;
v2 = 0;
do
{
v13 = *(_QWORD *)(v13 + 16);
v8 = *(_DWORD *)(v13 + 8);
if ( v8 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_video_scheduler.c",
0x61u,
"video_scheduler_slice_dump");
v17 = *(_QWORD *)v13 + 12LL;
v9 = v8 - 12;
v10 = 188 * ((v8 - 12) / 0xBC);
v15 = v9 - v10;
if ( v9 != v10 )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_video_scheduler.c",
0x6Bu,
"video_scheduler_slice_dump");
v3 = (_BYTE *)(*(_QWORD *)v13 + 12LL);
v16 = v9;
do
{
if ( ((unsigned __int8)v3[2] | ((unsigned __int8)v3[1] << 8) & 0x1F00) == 4113 )
{
LOBYTE(v4) = 0;
if ( (v3[3] & 0x20) != 0 )
LOBYTE(v4) = v3[4] + 1;
v4 = (unsigned __int8)v4;
if ( (v3[1] & 0x40) != 0 )
v4 = (unsigned __int8)v4 + 14;
v5 = 184 - v4;
if ( v2 + 184 - v4 > 0x14000 )
{
*(_DWORD *)(v1 + 20) = v2;
v6 = (__int64 *)sx_desc_get();
v14[2] = (__int64)v6;
v7 = sx_video_sink_buf_get();
v1 = v7;
if ( !v7 )
_assert_fail("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 0x92u, "video_scheduler_slice_dump");
*v6 = v7;
v14 = v6;
v2 = v15;
}
memcpy((void *)(*(_QWORD *)(v1 + 8) + v2), &v3[v4 + 4], v5);
v2 += v5;
}
v3 += 188;
}
while ( (_DWORD)v17 + v16 != (_DWORD)v3 );
}
while ( *(_QWORD *)(v13 + 16) );
*(_DWORD *)(v1 + 20) = v2;
sx_desc_put(*(_QWORD *)(a1 + 16));
*(_QWORD *)(a1 + 16) = v12;
return sx_pipe_put(3LL, a1);
}

original_code:
static void video_scheduler_slice_dump(
    sSX_DESC *slice_head
    )
{
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    unsigned int afc;
    unsigned int pes_byte_count;
    unsigned int payload_size;
    unsigned int start_offset;
    unsigned int copy_index;
    unsigned int pid;
    sSX_DESC *curr_desc;
    sSX_DESC *head_desc;
    ((void) sizeof ((slice_head) ? 1 : 0), __extension__ ({ if (slice_head) ; else __assert_fail ("slice_head", "./src/sx_mgmt_video_scheduler.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    sSX_DESC *hw_desc = sx_desc_get();
    sSX_DESC *hw_desc_head = hw_desc;
    sDECODER_HW_BUFFER * hw_buf = sx_video_sink_buf_get();
    ((void) sizeof ((hw_buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (hw_buf != ((void *)0)) ; else __assert_fail ("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 76, __extension__ __PRETTY_FUNCTION__); }));
    hw_desc->data = (unsigned char *) hw_buf;
    curr_desc = slice_head;
    copy_index = 0;
    do
    {
        curr_desc = curr_desc->next;
        curr_ptr = curr_desc->data;
        bytes_left = curr_desc->data_len;
        ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_scheduler.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        curr_ptr += sizeof(sRTP_HDR);
        bytes_left -= sizeof(sRTP_HDR);
        ((void) sizeof (((bytes_left % sizeof(sMPEG2_TS)) == 0) ? 1 : 0), __extension__ ({ if ((bytes_left % sizeof(sMPEG2_TS)) == 0) ; else __assert_fail ("(bytes_left % sizeof(sMPEG2_TS)) == 0", "./src/sx_mgmt_video_scheduler.c", 107, __extension__ __PRETTY_FUNCTION__); }));
        pes_byte_count = 0;
        do
        {
            sMPEG2_TS *ts = (sMPEG2_TS *) curr_ptr;
            afc = (((ts->hdr).tsc_afc_cc & 0x30) >> 4);
            pid = ((((ts->hdr).tei_pusi_tp_pid1 & 0x1F) << 8) | (ts->hdr).pid2);
            if(pid == 0x1011)
            {
                unsigned char stuffing = 0;
                if(afc & 0x02)
                {
                    stuffing = 1 + ts->payload.payload[0];
                }
                start_offset = stuffing;
                if((((ts->hdr).tei_pusi_tp_pid1 & 0x40) >> 6))
                {
                     start_offset += 14;
                }
                payload_size = sizeof(sMPEG2_TS_PAYLOAD) - start_offset;
                if((copy_index + payload_size) > 81920)
                {
                    hw_buf->buffer_len = copy_index;
                    hw_desc->next = sx_desc_get();
                    hw_desc = hw_desc->next;
                    hw_buf = sx_video_sink_buf_get();
                    ((void) sizeof ((hw_buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (hw_buf != ((void *)0)) ; else __assert_fail ("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 146, __extension__ __PRETTY_FUNCTION__); }));
                    hw_desc->data = (unsigned char *) hw_buf;
                    copy_index = 0;
                }
                memcpy(&hw_buf->buffer[copy_index],
                        &ts->payload.payload[start_offset],
                        payload_size);
                copy_index += payload_size;
            }
            curr_ptr += sizeof(sMPEG2_TS);
            bytes_left -= sizeof(sMPEG2_TS);
        } while (bytes_left > 0);
    } while (curr_desc->next != ((void *)0));
    hw_buf->buffer_len = copy_index;
    sx_desc_put(slice_head->next);
    slice_head->next = hw_desc_head;
    sx_pipe_put(3, slice_head);
}

canonical_original_code:
static void video_scheduler_slice_dump(struct sSX_DESC *slice_head)
{
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    unsigned int afc;
    unsigned int pes_byte_count;
    unsigned int payload_size;
    unsigned int start_offset;
    unsigned int copy_index;
    unsigned int pid;
    struct sSX_DESC *curr_desc;
    struct sSX_DESC *head_desc;
    ((void) sizeof ((slice_head) ? 1 : 0), __extension__ ({ if (slice_head) ; else __assert_fail ("slice_head", "./src/sx_mgmt_video_scheduler.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    struct sSX_DESC *hw_desc = sx_desc_get();
    struct sSX_DESC *hw_desc_head = hw_desc;
    struct sDECODER_HW_BUFFER *hw_buf = sx_video_sink_buf_get();
    ((void) sizeof ((hw_buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (hw_buf != ((void *)0)) ; else __assert_fail ("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 76, __extension__ __PRETTY_FUNCTION__); }));
    hw_desc->data = (unsigned char *) hw_buf;
    curr_desc = slice_head;
    copy_index = 0;
    do
    {
        curr_desc = curr_desc->next;
        curr_ptr = curr_desc->data;
        bytes_left = curr_desc->data_len;
        ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_scheduler.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        curr_ptr += sizeof(sRTP_HDR);
        bytes_left -= sizeof(sRTP_HDR);
        ((void) sizeof (((bytes_left % sizeof(sMPEG2_TS)) == 0) ? 1 : 0), __extension__ ({ if ((bytes_left % sizeof(sMPEG2_TS)) == 0) ; else __assert_fail ("(bytes_left % sizeof(sMPEG2_TS)) == 0", "./src/sx_mgmt_video_scheduler.c", 107, __extension__ __PRETTY_FUNCTION__); }));
        pes_byte_count = 0;
        do
        {
            struct sMPEG2_TS *ts = (sMPEG2_TS *) curr_ptr;
            afc = (((ts->hdr).tsc_afc_cc & 0x30) >> 4);
            pid = ((((ts->hdr).tei_pusi_tp_pid1 & 0x1F) << 8) | (ts->hdr).pid2);
            if(pid == 0x1011)
            {
                unsigned char stuffing = 0;
                if(afc & 0x02)
                {
                    stuffing = 1 + ts->payload.payload[0];
                }
                start_offset = stuffing;
                if((((ts->hdr).tei_pusi_tp_pid1 & 0x40) >> 6))
                {
                     start_offset += 14;
                }
                payload_size = sizeof(sMPEG2_TS_PAYLOAD) - start_offset;
                if((copy_index + payload_size) > 81920)
                {
                    hw_buf->buffer_len = copy_index;
                    hw_desc->next = sx_desc_get();
                    hw_desc = hw_desc->next;
                    hw_buf = sx_video_sink_buf_get();
                    ((void) sizeof ((hw_buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (hw_buf != ((void *)0)) ; else __assert_fail ("hw_buf != NULL", "./src/sx_mgmt_video_scheduler.c", 146, __extension__ __PRETTY_FUNCTION__); }));
                    hw_desc->data = (unsigned char *) hw_buf;
                    copy_index = 0;
                }
                memcpy(&hw_buf->buffer[copy_index],
                        &ts->payload.payload[start_offset],
                        payload_size);
                copy_index += payload_size;
            }
            curr_ptr += sizeof(sMPEG2_TS);
            bytes_left -= sizeof(sMPEG2_TS);
        } while (bytes_left > 0);
    } while (curr_desc->next != ((void *)0));
    hw_buf->buffer_len = copy_index;
    sx_desc_put(slice_head->next);
    slice_head->next = hw_desc_head;
    sx_pipe_put(3, slice_head);
}

variable_types: struct sDECODER_HW_BUFFER { unsigned int reserve1; unsigned int reserved2; unsigned char *buffer; unsigned int reserved3; unsigned int buffer_len; unsigned int reserved4; unsigned int reserved5; unsigned int reserved6; unsigned int reserved7; unsigned int reserved8; unsigned int reserved9; unsigned int reserved10; unsigned int reserved11; unsigned int reserved12; unsigned int reserved13; unsigned int reserved14; unsigned int reserved15; } *


decompiled_code:
void __noreturn slice_packing_thread()
{
unsigned int v0; // eax
unsigned int v1; // ebx
__int64 v2; // rdi

while ( 1 )
{
v0 = sx_pipe_len_get(3LL);
if ( v0 <= 9 )
{
v1 = 10 - v0;
do
{
v2 = sx_pipe_get(2LL);
if ( !v2 )
break;
video_scheduler_slice_dump(v2);
--v1;
}
while ( v1 );
}
usleep(0x7D0u);
}
}

canonical_decompiled_code:
void __noreturn func3()
{
unsigned int v0; // eax
unsigned int v1; // ebx
__int64 v2; // rdi

while ( 1 )
{
v0 = sx_pipe_len_get(3LL);
if ( v0 <= 9 )
{
v1 = 10 - v0;
do
{
v2 = sx_pipe_get(2LL);
if ( !v2 )
break;
func2(v2);
--v1;
}
while ( v1 );
}
usleep(0x7D0u);
}
}

original_code:
void slice_packing_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    desc = ((void *)0);
    while(1)
    {
        unsigned int len = sx_pipe_len_get(3);
        if(len >= 10)
        {
            goto next_iter;
        }
        unsigned int slices_to_dump = 10 - len;
        do
        {
            desc = sx_pipe_get(2);
            if(desc == ((void *)0))
            {
                goto next_iter;
            }
            video_scheduler_slice_dump(desc);
            slices_to_dump--;
        } while (slices_to_dump > 0);
        next_iter:
        usleep(2*1000);
    }
}

canonical_original_code:
void slice_packing_thread(void *arg)
{
    struct sSX_DESC *desc;
    desc = ((void *)0);
    while(1)
    {
        unsigned int len = sx_pipe_len_get(3);
        if(len >= 10)
        {
            goto next_iter;
        }
        unsigned int slices_to_dump = 10 - len;
        do
        {
            desc = sx_pipe_get(2);
            if(desc == ((void *)0))
            {
                goto next_iter;
            }
            video_scheduler_slice_dump(desc);
            slices_to_dump--;
        } while (slices_to_dump > 0);
        next_iter:
        usleep(2*1000);
    }
}


decompiled_code:
__time_t sink_time_get()
{
struct timeval tv; // [rsp+0h] [rbp-10h] BYREF

gettimeofday(&tv, 0LL);
return 1000 * tv.tv_sec + tv.tv_usec / 1000;
}

canonical_decompiled_code:
__time_t func4()
{
struct timeval tv; // [rsp+0h] [rbp-10h] BYREF

gettimeofday(&tv, 0LL);
return 1000 * tv.tv_sec + tv.tv_usec / 1000;
}

original_code:
static unsigned long long sink_time_get(
    void
    )
{
    struct timeval curr_time;
    gettimeofday(&curr_time, ((void *)0));
    unsigned long long temp = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
    return temp;
}

canonical_original_code:
static unsigned long long sink_time_get()
{
    struct timeval curr_time;
    gettimeofday(&curr_time, ((void *)0));
    unsigned long long temp = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
    return temp;
}

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

variable_types:
temp
unsigned long long


decompiled_code:
void __fastcall __noreturn pcr_update_thread()
{
__time_t v0; // rbx
__int64 v1; // r12
__time_t v2; // r13
__int64 v3; // rax

while ( 1 )
{
v3 = sx_pipe_get(4LL);
if ( v3 )
{
v1 = *(_QWORD *)(*(_QWORD *)v3 + 8LL);
sx_desc_put(v3);
v0 = sink_time_get();
}
v2 = sink_time_get();
pthread_mutex_lock(&mutex);
qword_6F8 = v1 + v2 - 500 - v0;
pthread_mutex_unlock(&mutex);
usleep(0x7D0u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func5()
{
__time_t v0; // rbx
__int64 v1; // r12
__time_t v2; // r13
__int64 v3; // rax

while ( 1 )
{
v3 = sx_pipe_get(4LL);
if ( v3 )
{
v1 = *(_QWORD *)(*(_QWORD *)v3 + 8LL);
sx_desc_put(v3);
v0 = func4();
}
v2 = func4();
pthread_mutex_lock(&mutex);
qword_6F8 = v1 + v2 - 500 - v0;
pthread_mutex_unlock(&mutex);
usleep(0x7D0u);
}
}

original_code:
void pcr_update_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    unsigned long long pcr_time;
    unsigned long long pcr_received_time;
    unsigned long long curr_time;
    while(1)
    {
        desc = sx_pipe_get(4);
        if(desc == ((void *)0))
        {
            goto cleanup;
        }
        sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data;
        pcr_time = hdr->timestamp;
        sx_desc_put(desc);
        pcr_received_time = sink_time_get();
        cleanup:
        curr_time = sink_time_get();
        pthread_mutex_lock(&f_cblk.lock);
        f_cblk.curr_time = pcr_time + (curr_time - pcr_received_time) - (400) - (100);
        pthread_mutex_unlock(&f_cblk.lock);
        usleep(2*1000);
    }
}

canonical_original_code:
void pcr_update_thread(void *arg)
{
    struct sSX_DESC *desc;
    unsigned long long pcr_time;
    unsigned long long pcr_received_time;
    unsigned long long curr_time;
    while(1)
    {
        desc = sx_pipe_get(4);
        if(desc == ((void *)0))
        {
            goto cleanup;
        }
        struct sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data;
        pcr_time = hdr->timestamp;
        sx_desc_put(desc);
        pcr_received_time = sink_time_get();
        cleanup:
        curr_time = sink_time_get();
        pthread_mutex_lock(&f_cblk.lock);
        f_cblk.curr_time = pcr_time + (curr_time - pcr_received_time) - (400) - (100);
        pthread_mutex_unlock(&f_cblk.lock);
        usleep(2*1000);
    }
}

variable_types: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; } *

variable_types: struct sSLICE_HDR { unsigned char type; unsigned char rsvd[3]; unsigned long long timestamp; } *


decompiled_code:
__int64 video_scheduler_thread_create()
{
sx_thread_create(&f_cblk, video_scheduler_thread, 0LL, 70LL);
sx_thread_create(&unk_6E8, slice_packing_thread, 0LL, 70LL);
return sx_thread_create(&unk_6F0, pcr_update_thread, 0LL, 70LL);
}

canonical_decompiled_code:
__int64 func7()
{
sx_thread_create(&f_cblk, video_scheduler_thread, 0LL, 70LL);
sx_thread_create(&unk_6E8, slice_packing_thread, 0LL, 70LL);
return sx_thread_create(&unk_6F0, pcr_update_thread, 0LL, 70LL);
}

original_code:
static void video_scheduler_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.video_scheduler_thread,
                     &video_scheduler_thread,
                     ((void *)0),
                     70);
    sx_thread_create(&f_cblk.slice_packing_thread,
                     &slice_packing_thread,
                     ((void *)0),
                     70);
    sx_thread_create(&f_cblk.pcr_update_thread,
                     &pcr_update_thread,
                     ((void *)0),
                     70);
}

canonical_original_code:
static void video_scheduler_thread_create()
{
    sx_thread_create(&f_cblk.video_scheduler_thread,
                     &video_scheduler_thread,
                     ((void *)0),
                     70);
    sx_thread_create(&f_cblk.slice_packing_thread,
                     &slice_packing_thread,
                     ((void *)0),
                     70);
    sx_thread_create(&f_cblk.pcr_update_thread,
                     &pcr_update_thread,
                     ((void *)0),
                     70);
}


decompiled_code:
int sx_mgmt_video_scheduler_init()
{
return resources_init();
}

canonical_decompiled_code:
int func8()
{
return func6();
}

original_code:
void sx_mgmt_video_scheduler_init(
    void
    )
{
    resources_init();
}

canonical_original_code:
void sx_mgmt_video_scheduler_init()
{
    resources_init();
}


decompiled_code:
__int64 sx_mgmt_video_scheduler_open()
{
return video_scheduler_thread_create();
}

canonical_decompiled_code:
__int64 func9()
{
return func7();
}

original_code:
void sx_mgmt_video_scheduler_open(
    void
    )
{
    video_scheduler_thread_create();
}

canonical_original_code:
void sx_mgmt_video_scheduler_open()
{
    video_scheduler_thread_create();
}


decompiled_code:
void sx_mgmt_video_scheduler_close()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
void sx_mgmt_video_scheduler_close(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_video_scheduler_close()
{
}


decompiled_code:
int resources_init()
{
sx_video_sink_init();
return pthread_mutex_init(&mutex, 0LL);
}

canonical_decompiled_code:
int func6()
{
sx_video_sink_init();
return pthread_mutex_init(&mutex, 0LL);
}

original_code:
static void resources_init(
    void
    )
{
    sx_video_sink_init();
    pthread_mutex_init(&f_cblk.lock, ((void *)0));
}

canonical_original_code:
static void resources_init()
{
    sx_video_sink_init();
    pthread_mutex_init(&f_cblk.lock, ((void *)0));
}


decompiled_code:
__int64 sx_pipe_init()
{
__int64 *v0; // rbx
__int64 result; // rax

v0 = &f_cblk;
do
{
result = sx_queue_create();
*v0++ = result;
}
while ( v0 != (__int64 *)&unk_B8 );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 *v0; // rbx
__int64 result; // rax

v0 = &f_cblk;
do
{
result = sx_queue_create();
*v0++ = result;
}
while ( v0 != (__int64 *)&unk_B8 );
return result;
}

original_code:
void sx_pipe_init(
    void
    )
{
    unsigned int i;
    for(i = 0; i < 7; i++)
    {
        f_cblk.queue[i] = sx_queue_create();
    }
}

canonical_original_code:
void sx_pipe_init()
{
    unsigned int i;
    for(i = 0; i < 7; i++)
    {
        f_cblk.queue[i] = sx_queue_create();
    }
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall sx_pipe_put(int a1)
{
return sx_queue_push(f_cblk[a1]);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return sx_queue_push(f_cblk[a1]);
}

original_code:
void sx_pipe_put(
    unsigned int index,
    void *data
    )
{
    sx_queue_push(f_cblk.queue[index], data);
}

canonical_original_code:
void sx_pipe_put(unsigned int index, void *data)
{
    sx_queue_push(f_cblk.queue[index], data);
}

variable_types:
index
unsigned int

variable_types:
data
void *


decompiled_code:
__int64 __fastcall sx_pipe_get(int a1)
{
return sx_queue_pull(f_cblk[a1]);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return sx_queue_pull(f_cblk[a1]);
}

original_code:
void * sx_pipe_get(
    unsigned int index
    )
{
    return sx_queue_pull(f_cblk.queue[index]);
}

canonical_original_code:
void *sx_pipe_get(unsigned int index)
{
    return sx_queue_pull(f_cblk.queue[index]);
}

return_type: void *


decompiled_code:
__int64 __fastcall sx_pipe_len_get(int a1)
{
return sx_queue_len_get(f_cblk[a1]);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
return sx_queue_len_get(f_cblk[a1]);
}

original_code:
unsigned int sx_pipe_len_get(
    unsigned int index
    )
{
    return sx_queue_len_get(f_cblk.queue[index]);
}

canonical_original_code:
unsigned int sx_pipe_len_get(unsigned int index)
{
    return sx_queue_len_get(f_cblk.queue[index]);
}


decompiled_code:
void sx_mgmt_env_open()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void sx_mgmt_env_open(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_env_open()
{
}


decompiled_code:
__int64 __fastcall sx_mgmt_env_get(int a1)
{
printf("(sx_mgmt_env_get(): var = %u\n", (unsigned int)f_env_vars[a1]);
return (unsigned int)f_env_vars[a1];
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
printf("(sx_mgmt_env_get(): var = %u\n", (unsigned int)f_env_vars[a1]);
return (unsigned int)f_env_vars[a1];
}

original_code:
unsigned int sx_mgmt_env_get(
    eMGMT_ENV_VAR var
    )
{
    printf("(sx_mgmt_env_get(): var = %u\n", f_env_vars[var]);
    return f_env_vars[var];
}

canonical_original_code:
unsigned int sx_mgmt_env_get(enum eMGMT_ENV_VAR var)
{
    printf("(sx_mgmt_env_get(): var = %u\n", f_env_vars[var]);
    return f_env_vars[var];
}

variable_types: enum eMGMT_ENV_VAR { MGMT_ENV_VAR_WIDTH=0, MGMT_ENV_VAR_HEIGHT=1, MGMT_ENV_VAR_SESSION_WIDTH=2, MGMT_ENV_VAR_SESSION_HEIGHT=3, MGMT_ENV_VAR_MAX=4 }


decompiled_code:
_DWORD *sx_desc_get()
{
_DWORD *result; // rax

result = malloc(0x18uLL);
if ( !result )
_assert_fail("desc != NULL", "./src/sx_desc.c", 0xFu, "sx_desc_get");
*(_QWORD *)result = 0LL;
result[2] = 0;
*((_QWORD *)result + 2) = 0LL;
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

result = malloc(0x18uLL);
if ( !result )
_assert_fail("desc != NULL", "./src/sx_desc.c", 0xFu, "sx_desc_get");
*(_QWORD *)result = 0LL;
result[2] = 0;
*((_QWORD *)result + 2) = 0LL;
return result;
}

original_code:
sSX_DESC * sx_desc_get(
    void
    )
{
    sSX_DESC *desc = malloc(sizeof(sSX_DESC));
    ((void) sizeof ((desc != ((void *)0)) ? 1 : 0), __extension__ ({ if (desc != ((void *)0)) ; else __assert_fail ("desc != NULL", "./src/sx_desc.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    desc->data = ((void *)0);
    desc->data_len = 0;
    desc->next = ((void *)0);
    return desc;
}

canonical_original_code:
struct sSX_DESC *sx_desc_get()
{
    struct sSX_DESC *desc = malloc(sizeof(sSX_DESC));
    ((void) sizeof ((desc != ((void *)0)) ? 1 : 0), __extension__ ({ if (desc != ((void *)0)) ; else __assert_fail ("desc != NULL", "./src/sx_desc.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    desc->data = ((void *)0);
    desc->data_len = 0;
    desc->next = ((void *)0);
    return desc;
}


decompiled_code:
void __fastcall sx_desc_put(void *ptr)
{
void **v1; // rbx
void **v2; // r12

v1 = (void **)ptr;
if ( !ptr )
_assert_fail("desc != NULL", "./src/sx_desc.c", 0x26u, "sx_desc_put");
do
{
v2 = v1;
v1 = (void **)v1[2];
free(*v2);
free(v2);
}
while ( v1 );
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void **v1; // rbx
void **v2; // r12

v1 = (void **)ptr;
if ( !ptr )
_assert_fail("desc != NULL", "./src/sx_desc.c", 0x26u, "sx_desc_put");
do
{
v2 = v1;
v1 = (void **)v1[2];
free(*v2);
free(v2);
}
while ( v1 );
}

original_code:
void sx_desc_put(
     sSX_DESC *desc
     )
{
     sSX_DESC *curr;
     sSX_DESC *next;
     ((void) sizeof ((desc != ((void *)0)) ? 1 : 0), __extension__ ({ if (desc != ((void *)0)) ; else __assert_fail ("desc != NULL", "./src/sx_desc.c", 38, __extension__ __PRETTY_FUNCTION__); }));
     curr = desc;
     while(curr != ((void *)0))
     {
         next = curr->next;
         free(curr->data);
         free(curr);
         curr = next;
     }
}

canonical_original_code:
void sx_desc_put(struct sSX_DESC *desc)
{
     struct sSX_DESC *curr;
     struct sSX_DESC *next;
     ((void) sizeof ((desc != ((void *)0)) ? 1 : 0), __extension__ ({ if (desc != ((void *)0)) ; else __assert_fail ("desc != NULL", "./src/sx_desc.c", 38, __extension__ __PRETTY_FUNCTION__); }));
     curr = desc;
     while(curr != ((void *)0))
     {
         next = curr->next;
         free(curr->data);
         free(curr);
         curr = next;
     }
}


cacheCreate

decompiled_code:
__int64 __fastcall cacheCreate(unsigned int a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, _QWORD *a6)
{
__int64 result; // rax

if ( (unsigned int)memAlloc(16LL, a6) == 1 )
{
if ( (unsigned int)createHashtable(a2, a3, a4, a5, *a6) == 1 )
{
result = createLimCache(a1, *a6 + 8LL);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = createLimCache(a1, *a6 + 8LL);
}
}
else
{
fflush(stderr);
result = createHashtable(a2, a3, a4, a5, *a6);
}
}
else
{
fflush(stderr);
result = memAlloc(16LL, a6);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, _QWORD *a6)
{
__int64 result; // rax

if ( (unsigned int)memAlloc(16LL, a6) == 1 )
{
if ( (unsigned int)createHashtable(a2, a3, a4, a5, *a6) == 1 )
{
result = createLimCache(a1, *a6 + 8LL);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = createLimCache(a1, *a6 + 8LL);
}
}
else
{
fflush(stderr);
result = createHashtable(a2, a3, a4, a5, *a6);
}
}
else
{
fflush(stderr);
result = memAlloc(16LL, a6);
}
return result;
}

original_code:
errorCode cacheCreate(int sizeCache, int HTnumberEntries, keyCompareFunction HTcompare, deallocKeyFunction HTdeallocK, deallocDataFunction HTdeallocD, cache ** c){
  if(memAlloc(sizeof(cache), (void **)c) != 1){ fflush(stderr); return memAlloc(sizeof(cache), (void **)c); };
  if(createHashtable(HTnumberEntries, HTcompare, HTdeallocK, HTdeallocD, (&((*c) -> ht))) != 1){ fflush(stderr); return createHashtable(HTnumberEntries, HTcompare, HTdeallocK, HTdeallocD, (&((*c) -> ht))); };
  if(createLimCache(sizeCache, (&((*c) -> lsc))) != 1){ fflush(stderr); return createLimCache(sizeCache, (&((*c) -> lsc))); };
  return 1;
}

canonical_original_code:
int cacheCreate(int sizeCache, int HTnumberEntries, int (*HTcompare)(void *, void *, int *), int (*HTdeallocK)(void *), int (*HTdeallocD)(void *), struct cache_tag **c){
  if(memAlloc(sizeof(cache), (void **)c) != 1){ fflush(stderr); return memAlloc(sizeof(cache), (void **)c); };
  if(createHashtable(HTnumberEntries, HTcompare, HTdeallocK, HTdeallocD, (&((*c) -> ht))) != 1){ fflush(stderr); return createHashtable(HTnumberEntries, HTcompare, HTdeallocK, HTdeallocD, (&((*c) -> ht))); };
  if(createLimCache(sizeCache, (&((*c) -> lsc))) != 1){ fflush(stderr); return createLimCache(sizeCache, (&((*c) -> lsc))); };
  return 1;
}

variable_types: sizeCache int

variable_types: HTnumberEntries int

variable_types:
HTcompare
int (*)(void *, void *, int *)

variable_types:
HTdeallocK
int (*)(void *)

variable_types:
HTdeallocD
int (*)(void *)

variable_types: struct cache_tag { struct hashtable_tag { int size; int nr_rows; int busy; int maxsize; struct hashtableEntry_tag { unsigned int key; unsigned int data; int busy; struct hashtableEntry_tag *next; } *cur_entry; int scan_active; int cur_row; struct hashtableEntry_tag *free_pool; int next_free; struct hashtableEntry_tag **table; } *ht; struct limSizeCache_tag { int size; int pinpointed; int usage; void *dataSpace; struct accnt_tag { int used; int counter; struct timeval { long tv_sec; long tv_usec; } tv; void *key; void *data; int evict; void *bh; } *accntArray; } *lsc; } **

user_defined_types: struct cache_tag { struct hashtable_tag *ht; struct limSizeCache_tag *lsc; }

user_defined_types: struct hashtable_tag { int size; int nr_rows; int busy; int maxsize; struct hashtableEntry_tag *cur_entry; int scan_active; int cur_row; struct hashtableEntry_tag *free_pool; int next_free; struct hashtableEntry_tag **table; }

user_defined_types: struct limSizeCache_tag { int size; int pinpointed; int usage; void *dataSpace; struct accnt_tag *accntArray; }

user_defined_types: struct accnt_tag { int used; int counter; struct timeval tv; void *key; void *data; int evict; void *bh; }


cachePinpoint

decompiled_code:
__int64 __fastcall cachePinpoint(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)HTlookup(*a1, a2, v3) == 1 )
{
result = pinpoint(a1[1], (unsigned int)v3[0]);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = pinpoint(a1[1], (unsigned int)v3[0]);
}
}
else
{
fflush(stderr);
result = HTlookup(*a1, a2, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)HTlookup(*a1, a2, v3) == 1 )
{
result = pinpoint(a1[1], (unsigned int)v3[0]);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = pinpoint(a1[1], (unsigned int)v3[0]);
}
}
else
{
fflush(stderr);
result = HTlookup(*a1, a2, v3);
}
return result;
}

original_code:
errorCode cachePinpoint(cache * c, void * key){
  int slot;
  if(HTlookup(c -> ht, (void*)key, (void **)&slot) != 1){ fflush(stderr); return HTlookup(c -> ht, (void*)key, (void **)&slot); };
  if(pinpoint(c -> lsc, slot) != 1){ fflush(stderr); return pinpoint(c -> lsc, slot); };
  return 1;
}

canonical_original_code:
int cachePinpoint(struct cache_tag *c, void *key){
  int slot;
  if(HTlookup(c -> ht, (void *)key, (void **)&slot) != 1){ fflush(stderr); return HTlookup(c -> ht, (void *)key, (void **)&slot); };
  if(pinpoint(c -> lsc, slot) != 1){ fflush(stderr); return pinpoint(c -> lsc, slot); };
  return 1;
}

variable_types: struct cache_tag { struct hashtable_tag { int size; int nr_rows; int busy; int maxsize; struct hashtableEntry_tag { unsigned int key; unsigned int data; int busy; struct hashtableEntry_tag *next; } *cur_entry; int scan_active; int cur_row; struct hashtableEntry_tag *free_pool; int next_free; struct hashtableEntry_tag **table; } *ht; struct limSizeCache_tag { int size; int pinpointed; int usage; void *dataSpace; struct accnt_tag { int used; int counter; struct timeval { long tv_sec; long tv_usec; } tv; void *key; void *data; int evict; void *bh; } *accntArray; } *lsc; } *

variable_types:
key
void *

variable_types: slot int


cacheLookupFailedInsert

decompiled_code:
__int64 __fastcall cacheLookupFailedInsert(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
_BYTE v5[12]; // [rsp+0h] [rbp-28h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v6) == 1 )
{
result = HTinsert(*a1, a2, v6[0]);
if ( (_DWORD)result == 1 )
{
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
}
}
}
else
{
findEvictionCandidate(a1[1], v6);
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v6[0] + 24), v5) == 1 )
{
result = HTinsert(*a1, a2, v6[0]);
if ( (_DWORD)result == 1 )
{
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
}
}
}
else
{
fflush(stderr);
result = HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v6[0] + 24), v5);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
_BYTE v5[12]; // [rsp+0h] [rbp-28h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v6) == 1 )
{
result = HTinsert(*a1, a2, v6[0]);
if ( (_DWORD)result == 1 )
{
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
}
}
}
else
{
findEvictionCandidate(a1[1], v6);
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v6[0] + 24), v5) == 1 )
{
result = HTinsert(*a1, a2, v6[0]);
if ( (_DWORD)result == 1 )
{
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElementNoDataCopy(a1[1], (unsigned int)v6[0], a2, a3);
}
}
}
else
{
fflush(stderr);
result = HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v6[0] + 24), v5);
}
}
return result;
}

original_code:
errorCode cacheLookupFailedInsert(cache * c, void * key, void ** data){
  int res;
  int slot;
  void * extractedData;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    if ((res = HTinsert(c -> ht, (void *)key, (void *)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    if(HTextract(c -> ht, (((c -> lsc -> accntArray) + slot) -> key ), &extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (((c -> lsc -> accntArray) + slot) -> key ), &extractedData); };
    if ((res = HTinsert(c -> ht, (void*)key, (void*)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  return 1;
}

canonical_original_code:
int cacheLookupFailedInsert(struct cache_tag *c, void *key, void **data){
  int res;
  int slot;
  void *extractedData;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    if ((res = HTinsert(c -> ht, (void *)key, (void *)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    if(HTextract(c -> ht, (((c -> lsc -> accntArray) + slot) -> key ), &extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (((c -> lsc -> accntArray) + slot) -> key ), &extractedData); };
    if ((res = HTinsert(c -> ht, (void *)key, (void *)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  return 1;
}

variable_types:
data
void **

variable_types: res int

variable_types:
extractedData
void *

user_defined_types: struct hashtableEntry_tag { unsigned int key; unsigned int data; int busy; struct hashtableEntry_tag *next; }

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


cacheLookupFailedInsertSlot

decompiled_code:
__int64 __fastcall cacheLookupFailedInsertSlot(_QWORD *a1, __int64 a2, __int64 a3, _DWORD *a4)
{
__int64 result; // rax
_BYTE v7[12]; // [rsp+0h] [rbp-38h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v8) == 1 )
{
result = HTinsert(*a1, a2, v8[0]);
if ( (_DWORD)result == 1 )
{
if ( (unsigned int)insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3) != 1 )
{
fflush(stderr);
return insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3);
}
goto LABEL_7;
}
}
else
{
findEvictionCandidate(a1[1], v8);
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v8[0] + 24), v7) != 1 )
{
fflush(stderr);
return HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v8[0] + 24), v7);
}
result = HTinsert(*a1, a2, v8[0]);
if ( (_DWORD)result == 1 )
{
if ( (unsigned int)insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3) != 1 )
{
fflush(stderr);
return insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3);
}
LABEL_7:
*a4 = v8[0];
return 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2, __int64 a3, _DWORD *a4)
{
__int64 result; // rax
_BYTE v7[12]; // [rsp+0h] [rbp-38h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v8) == 1 )
{
result = HTinsert(*a1, a2, v8[0]);
if ( (_DWORD)result == 1 )
{
if ( (unsigned int)insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3) != 1 )
{
fflush(stderr);
return insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3);
}
goto LABEL_7;
}
}
else
{
findEvictionCandidate(a1[1], v8);
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v8[0] + 24), v7) != 1 )
{
fflush(stderr);
return HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v8[0] + 24), v7);
}
result = HTinsert(*a1, a2, v8[0]);
if ( (_DWORD)result == 1 )
{
if ( (unsigned int)insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3) != 1 )
{
fflush(stderr);
return insertNewElementNoDataCopy(a1[1], (unsigned int)v8[0], a2, a3);
}
LABEL_7:
*a4 = v8[0];
return 1LL;
}
}
return result;
}

original_code:
errorCode cacheLookupFailedInsertSlot(cache * c, void * key, void ** data, int * resSlot){
  int res;
  int slot;
  void * extractedData;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    if ((res = HTinsert(c -> ht, (void*)key, (void*)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    if(HTextract(c -> ht, (void*)((((c -> lsc -> accntArray) + slot) -> key )), (void**)&extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (void*)((((c -> lsc -> accntArray) + slot) -> key )), (void**)&extractedData); };
    if ((res = HTinsert(c -> ht, (void*)key, (void*)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  *resSlot = slot;
  return 1;
}

canonical_original_code:
int cacheLookupFailedInsertSlot(struct cache_tag *c, void *key, void **data, int *resSlot){
  int res;
  int slot;
  void *extractedData;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    if ((res = HTinsert(c -> ht, (void *)key, (void *)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    if(HTextract(c -> ht, (void *)((((c -> lsc -> accntArray) + slot) -> key )), (void **)&extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (void *)((((c -> lsc -> accntArray) + slot) -> key )), (void **)&extractedData); };
    if ((res = HTinsert(c -> ht, (void *)key, (void *)slot)) != 1)
      return res;
    if(insertNewElementNoDataCopy(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElementNoDataCopy(c -> lsc, slot, key, data); };
  }
  *resSlot = slot;
  return 1;
}

variable_types:
resSlot
int *


cacheAllPinpointed

decompiled_code:
__int64 __fastcall cacheAllPinpointed(__int64 a1)
{
return allPinpointed(*(_QWORD *)(a1 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return allPinpointed(*(_QWORD *)(a1 + 8));
}

original_code:
errorCode cacheAllPinpointed(cache * c){
  return allPinpointed(c -> lsc);
}

canonical_original_code:
int cacheAllPinpointed(struct cache_tag *c){
  return allPinpointed(c -> lsc);
}


cacheUnPinpoint

decompiled_code:
__int64 __fastcall cacheUnPinpoint(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)HTlookup(*a1, a2, v3) == 1 )
{
result = unPinpoint(a1[1], (unsigned int)v3[0]);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = unPinpoint(a1[1], (unsigned int)v3[0]);
}
}
else
{
fflush(stderr);
result = HTlookup(*a1, a2, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)HTlookup(*a1, a2, v3) == 1 )
{
result = unPinpoint(a1[1], (unsigned int)v3[0]);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = unPinpoint(a1[1], (unsigned int)v3[0]);
}
}
else
{
fflush(stderr);
result = HTlookup(*a1, a2, v3);
}
return result;
}

original_code:
errorCode cacheUnPinpoint(cache * c, void * key){
  int slot;
  if(HTlookup(c -> ht, (void*)key, (void**)&slot) != 1){ fflush(stderr); return HTlookup(c -> ht, (void*)key, (void**)&slot); };
  if(unPinpoint(c -> lsc, slot) != 1){ fflush(stderr); return unPinpoint(c -> lsc, slot); };
  return 1;
}

canonical_original_code:
int cacheUnPinpoint(struct cache_tag *c, void *key){
  int slot;
  if(HTlookup(c -> ht, (void *)key, (void **)&slot) != 1){ fflush(stderr); return HTlookup(c -> ht, (void *)key, (void **)&slot); };
  if(unPinpoint(c -> lsc, slot) != 1){ fflush(stderr); return unPinpoint(c -> lsc, slot); };
  return 1;
}


cacheInsert

decompiled_code:
__int64 __fastcall cacheInsert(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v7) == 1 )
{
result = HTupdate(*a1, a2, v7[0], &v6);
if ( (_DWORD)result != -1 )
{
if ( (_DWORD)result == 1 )
{
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
}
}
else
{
result = insertNewElement(a1[1], v6, a2, a3);
if ( (_DWORD)result != 1 )
goto LABEL_18;
}
}
}
else
{
findEvictionCandidate(a1[1], v7);
result = HTupdate(*a1, a2, v7[0], &v6);
switch ( (_DWORD)result )
{
case 0xFFFFFFFF:
return result;
case 1:
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v7[0] + 24), &v5) == 1 )
{
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
}
}
else
{
fflush(stderr);
result = HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v7[0] + 24), &v5);
}
break;
case 0xFFFFFFFE:
result = insertNewElement(a1[1], v6, a2, a3);
if ( (_DWORD)result != 1 )
{
LABEL_18:
fflush(stderr);
return insertNewElement(a1[1], v6, a2, a3);
}
break;
default:
return 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)findEmptySlot(a1[1], v7) == 1 )
{
result = HTupdate(*a1, a2, v7[0], &v6);
if ( (_DWORD)result != -1 )
{
if ( (_DWORD)result == 1 )
{
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
}
}
else
{
result = insertNewElement(a1[1], v6, a2, a3);
if ( (_DWORD)result != 1 )
goto LABEL_18;
}
}
}
else
{
findEvictionCandidate(a1[1], v7);
result = HTupdate(*a1, a2, v7[0], &v6);
switch ( (_DWORD)result )
{
case 0xFFFFFFFF:
return result;
case 1:
if ( (unsigned int)HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v7[0] + 24), &v5) == 1 )
{
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
if ( (_DWORD)result != 1 )
{
fflush(stderr);
result = insertNewElement(a1[1], (unsigned int)v7[0], a2, a3);
}
}
else
{
fflush(stderr);
result = HTextract(*a1, *(_QWORD *)(*(_QWORD *)(a1[1] + 24LL) + 56LL * v7[0] + 24), &v5);
}
break;
case 0xFFFFFFFE:
result = insertNewElement(a1[1], v6, a2, a3);
if ( (_DWORD)result != 1 )
{
LABEL_18:
fflush(stderr);
return insertNewElement(a1[1], v6, a2, a3);
}
break;
default:
return 1LL;
}
}
return result;
}

original_code:
errorCode cacheInsert(cache * c, void * key, void * data){
  int slot, old_slot;
  void * extractedData;
  int res = -1;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    res = HTupdate(c -> ht, (void*)key, (void*)slot, (void**)&old_slot);
    if (res == -1)
      return res;
    if (res == 1){
      if(insertNewElement(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, slot, key, data); };
    }
    else{
      if(insertNewElement(c -> lsc, old_slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, old_slot, key, data); };
    }
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    res = HTupdate(c -> ht, (void*)key, (void*)slot, (void**)&old_slot);
    if (res == -1)
      return res;
    if (res == 1){
      if(HTextract(c -> ht, (void*)((((c -> lsc -> accntArray) + slot) -> key )), (void**)&extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (void*)((((c -> lsc -> accntArray) + slot) -> key )), (void**)&extractedData); };
      if(insertNewElement(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, slot, key, data); };
    }
    if (res == -2){
      if(insertNewElement(c -> lsc, old_slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, old_slot, key, data); };
    }
  }
  return 1;
}

canonical_original_code:
int cacheInsert(struct cache_tag *c, void *key, void *data){
  int slot; int old_slot;
  void *extractedData;
  int res = -1;
  if (findEmptySlot(c -> lsc, &slot) == 1){
    res = HTupdate(c -> ht, (void *)key, (void *)slot, (void **)&old_slot);
    if (res == -1)
      return res;
    if (res == 1){
      if(insertNewElement(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, slot, key, data); };
    }
    else{
      if(insertNewElement(c -> lsc, old_slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, old_slot, key, data); };
    }
  }
  else {
    findEvictionCandidate(c -> lsc, &slot);
    res = HTupdate(c -> ht, (void *)key, (void *)slot, (void **)&old_slot);
    if (res == -1)
      return res;
    if (res == 1){
      if(HTextract(c -> ht, (void *)((((c -> lsc -> accntArray) + slot) -> key )), (void **)&extractedData) != 1){ fflush(stderr); return HTextract(c -> ht, (void *)((((c -> lsc -> accntArray) + slot) -> key )), (void **)&extractedData); };
      if(insertNewElement(c -> lsc, slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, slot, key, data); };
    }
    if (res == -2){
      if(insertNewElement(c -> lsc, old_slot, key, data) != 1){ fflush(stderr); return insertNewElement(c -> lsc, old_slot, key, data); };
    }
  }
  return 1;
}