decompiled_code:
__int64 __fastcall opcode_name(unsigned __int16 a1)
{
char *v2; // rbx
__int16 v3; // r12
unsigned __int16 v4; // di
unsigned __int8 v5; // bl
char v6; // r14
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rax
int v10; // er12
char v11; // al
unsigned int v12; // er8
int v13; // er8
__int64 v14; // rax
char v15; // al
int v16; // ecx
__m128i v17; // xmm1
char v18; // r9
__int64 v19; // rdx
unsigned __int8 v20; // r9
int v21; // er8
char v22; // si
char v23; // si
char *i; // rax
__int64 j; // r14
__int64 v26; // rax
__int64 v27; // rax
char v28; // r9
int v29; // er8
char *v30; // rsi
unsigned int v31; // edx
char *v32; // rax
__int64 v33; // rcx
__int64 v35; // rax
__int64 v36; // rcx
unsigned __int8 v37; // r12
char v38; // al
size_t v39; // rax
const char *v40; // rsi
unsigned __int8 v41; // r12
char v42; // al
unsigned __int8 v43; // r12
char *v44; // rdi
unsigned __int8 v45; // [rsp+Ch] [rbp-7Ch] BYREF
unsigned __int8 v46; // [rsp+Dh] [rbp-7Bh]
unsigned __int8 v47; // [rsp+Eh] [rbp-7Ah]
unsigned __int8 v48; // [rsp+Fh] [rbp-79h]
char dest[16]; // [rsp+10h] [rbp-78h] BYREF
char v50[8]; // [rsp+20h] [rbp-68h] BYREF

v2 = (char *)&v45;
v3 = a1 - (unsigned __int16)&v45;
do
{
v4 = v3 + (_WORD)v2++;
*(v2 - 1) = z80_acc(v4);
}
while ( v2 != dest );
v5 = v45;
v6 = v45 & 0xDF;
if ( v45 == 0xCB )
{
v37 = v46;
v38 = v46 >> 6;
if ( v46 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v38 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( v38 != 1 )
{
sprintf(dest, "%s ", &oc_shift[4 * ((v46 >> 3) & 7)]);
LABEL_69:
v39 = strlen(dest);
v40 = &oc_op8[5 * (v37 & 7)];
v10 = 2;
strcpy(&dest[v39], v40);
v12 = -1;
LABEL_17:
v15 = dest[0];
if ( !dest[0] )
{
if ( v6 == -35 )
goto LABEL_85;
qmemcpy(debug_ins, " ", 9);
LABEL_60:
if ( v12 != -1 )
goto LABEL_39;
goto LABEL_72;
}
goto LABEL_18;
}
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v46 >> 3) & 7);
goto LABEL_69;
}
if ( v6 != -35 )
{
strcpy(dest, &oc_base[12 * v45]);
goto LABEL_10;
}
v7 = v46;
if ( v46 != 0xCB )
{
strcpy(dest, &oc_base[12 * v45]);
if ( (v7 & 0xDF) != 0xDD && (_BYTE)v7 != 0xED )
{
v9 = 3 * v7;
v10 = 2;
strcpy(dest, &oc_ix[4 * v9]);
LABEL_11:
if ( v5 == 0xED )
{
if ( (char)(v46 - 64) >= 0 )
{
strcpy(dest, &oc_ed[12 * v46 - 768]);
}
else if ( v46 )
{
if ( v46 == 1 )
strcpy(dest, "pti_printf");
else
strcpy(dest, "nop");
}
else
{
v8 = 29554LL;
strcpy(v50, "rs");
*(__m128i *)dest = _mm_load_si128((const __m128i *)&LC4);
}
++v10;
}
v11 = dest[0];
if ( dest[0] )
{
v8 = (__int64)dest;
v12 = -1;
while ( 2 )
{
switch ( v11 )
{
case 'A':
case 'W':
insert_hex(v8, 4);
v13 = *(&v45 + v10 + 1);
v14 = v10;
v10 += 2;
v12 = *(&v45 + v14) + (v13 << 8);
goto LABEL_16;
case 'B':
case 'P':
insert_hex(v8, 2);
v26 = v10++;
v12 = *(&v45 + v26);
v11 = *(_BYTE *)v8;
if ( *(_BYTE *)v8 )
continue;
goto LABEL_17;
case 'R':
insert_hex(v8, 4);
v27 = v10++;
v12 = (char)*(&v45 + v27) + a1 + 2;
v11 = *(_BYTE *)v8;
if ( *(_BYTE *)v8 )
continue;
goto LABEL_17;
default:
LABEL_16:
v11 = *(_BYTE *)++v8;
if ( !v11 )
goto LABEL_17;
continue;
}
}
}
if ( v6 != -35 )
{
qmemcpy(debug_ins, " ", 9);
LABEL_72:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8);
goto LABEL_40;
}
v12 = -1;
LABEL_85:
v16 = -1;
goto LABEL_28;
}
LABEL_10:
v10 = 1;
goto LABEL_11;
}
v41 = v48;
v42 = v48 >> 6;
if ( v48 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v42 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( v42 != 1 )
{
sprintf(dest, "%s ", &oc_shift[4 * ((v48 >> 3) & 7)]);
goto LABEL_82;
}
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v48 >> 3) & 7);
LABEL_82:
v43 = v41 & 7;
v44 = &dest[strlen(dest)];
if ( v43 == 6 )
*(_DWORD *)v44 = 2709544;
else
sprintf(v44, "(X)->%s", &oc_op8[5 * v43]);
v15 = dest[0];
v10 = 2;
v12 = -1;
if ( !dest[0] )
goto LABEL_85;
LABEL_18:
v8 = (__int64)&dest[3];
v16 = -1;
do
{
while ( v15 != 88 )
{
v15 = *(_BYTE *)(v8 - 2);
++v8;
if ( !v15 )
goto LABEL_27;
}
v17 = _mm_loadu_si128((const __m128i *)(v8 - 2));
*(_BYTE *)(v8 - 3) = 105;
v18 = *(&v45 + v10);
*(_BYTE *)(v8 - 2) = 120;
*(__m128i *)(v8 + 1) = v17;
if ( v18 < 0 )
{
*(_BYTE *)(v8 - 1) = 45;
insert_hex(v8, 2);
if ( v29 == -1 )
{
v12 = -v28;
goto LABEL_26;
}
}
else
{
*(_BYTE *)(v8 - 1) = 43;
insert_hex(v8, 2);
if ( v21 == -1 )
{
v12 = v20;
goto LABEL_26;
}
}
v12 = v47;
v16 = v48;
v22 = 43;
if ( (v47 & 0x80u) != 0 )
{
v22 = 45;
v12 = -(char)v47;
}
*(_BYTE *)(v19 - 1) = v22;
LABEL_26:
v15 = *(_BYTE *)(v19 - 2);
v8 = v19 + 1;
++v10;
}
while ( v15 );
LABEL_27:
if ( v6 == -35 )
{
LABEL_28:
if ( v46 == 0xCB )
v10 = 4;
if ( v5 == 0xFD )
{
v23 = dest[0];
if ( dest[0] )
{
for ( i = &dest[1]; ; ++i )
{
v8 = (unsigned __int8)*i;
if ( v23 == 105 && (_BYTE)v8 == 120 )
{
v8 = (unsigned __int8)i[1];
*i++ = 121;
if ( !(_BYTE)v8 )
break;
}
else if ( !(_BYTE)v8 )
{
break;
}
v23 = v8;
}
}
}
}
qmemcpy(debug_ins, " ", 9);
if ( v16 == -1 )
goto LABEL_60;
LABEL_39:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v12);
LABEL_40:
for ( j = 0LL; ; v5 = *(&v45 + j) )
{
sprintf(&dest[2 * j++], "%02x", v5);
if ( v10 <= (int)j )
break;
}
v30 = debug_ins;
v31 = strlen(dest);
v32 = dest;
if ( v31 >= 8 )
{
LODWORD(v35) = 0;
do
{
v36 = (unsigned int)v35;
v35 = (unsigned int)(v35 + 8);
*(_QWORD *)&debug_ins[v36] = *(_QWORD *)&dest[v36];
}
while ( (unsigned int)v35 < (v31 & 0xFFFFFFF8) );
v30 = &debug_ins[v35];
v32 = &dest[v35];
}
v33 = 0LL;
if ( (v31 & 4) != 0 )
{
*(_DWORD *)v30 = *(_DWORD *)v32;
v33 = 4LL;
}
if ( (v31 & 2) != 0 )
{
*(_WORD *)&v30[v33] = *(_WORD *)&v32[v33];
v33 += 2LL;
}
if ( (v31 & 1) != 0 )
v30[v33] = v32[v33];
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
char *v2; // rbx
__int16 v3; // r12
unsigned __int16 v4; // di
unsigned __int8 v5; // bl
char v6; // r14
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rax
int v10; // er12
char v11; // al
unsigned int v12; // er8
int v13; // er8
__int64 v14; // rax
char v15; // al
int v16; // ecx
__m128i v17; // xmm1
char v18; // r9
__int64 v19; // rdx
unsigned __int8 v20; // r9
int v21; // er8
char v22; // si
char v23; // si
char *i; // rax
__int64 j; // r14
__int64 v26; // rax
__int64 v27; // rax
char v28; // r9
int v29; // er8
char *v30; // rsi
unsigned int v31; // edx
char *v32; // rax
__int64 v33; // rcx
__int64 v35; // rax
__int64 v36; // rcx
unsigned __int8 v37; // r12
char v38; // al
size_t v39; // rax
const char *v40; // rsi
unsigned __int8 v41; // r12
char v42; // al
unsigned __int8 v43; // r12
char *v44; // rdi
unsigned __int8 v45; // [rsp+Ch] [rbp-7Ch] BYREF
unsigned __int8 v46; // [rsp+Dh] [rbp-7Bh]
unsigned __int8 v47; // [rsp+Eh] [rbp-7Ah]
unsigned __int8 v48; // [rsp+Fh] [rbp-79h]
char dest[16]; // [rsp+10h] [rbp-78h] BYREF
char v50[8]; // [rsp+20h] [rbp-68h] BYREF

v2 = (char *)&v45;
v3 = a1 - (unsigned __int16)&v45;
do
{
v4 = v3 + (_WORD)v2++;
*(v2 - 1) = z80_acc(v4);
}
while ( v2 != dest );
v5 = v45;
v6 = v45 & 0xDF;
if ( v45 == 0xCB )
{
v37 = v46;
v38 = v46 >> 6;
if ( v46 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v38 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( v38 != 1 )
{
sprintf(dest, "%s ", &oc_shift[4 * ((v46 >> 3) & 7)]);
LABEL_69:
v39 = strlen(dest);
v40 = &oc_op8[5 * (v37 & 7)];
v10 = 2;
strcpy(&dest[v39], v40);
v12 = -1;
LABEL_17:
v15 = dest[0];
if ( !dest[0] )
{
if ( v6 == -35 )
goto LABEL_85;
qmemcpy(debug_ins, " ", 9);
LABEL_60:
if ( v12 != -1 )
goto LABEL_39;
goto LABEL_72;
}
goto LABEL_18;
}
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v46 >> 3) & 7);
goto LABEL_69;
}
if ( v6 != -35 )
{
strcpy(dest, &oc_base[12 * v45]);
goto LABEL_10;
}
v7 = v46;
if ( v46 != 0xCB )
{
strcpy(dest, &oc_base[12 * v45]);
if ( (v7 & 0xDF) != 0xDD && (_BYTE)v7 != 0xED )
{
v9 = 3 * v7;
v10 = 2;
strcpy(dest, &oc_ix[4 * v9]);
LABEL_11:
if ( v5 == 0xED )
{
if ( (char)(v46 - 64) >= 0 )
{
strcpy(dest, &oc_ed[12 * v46 - 768]);
}
else if ( v46 )
{
if ( v46 == 1 )
strcpy(dest, "pti_printf");
else
strcpy(dest, "nop");
}
else
{
v8 = 29554LL;
strcpy(v50, "rs");
*(__m128i *)dest = _mm_load_si128((const __m128i *)&LC4);
}
++v10;
}
v11 = dest[0];
if ( dest[0] )
{
v8 = (__int64)dest;
v12 = -1;
while ( 2 )
{
switch ( v11 )
{
case 'A':
case 'W':
func0(v8, 4);
v13 = *(&v45 + v10 + 1);
v14 = v10;
v10 += 2;
v12 = *(&v45 + v14) + (v13 << 8);
goto LABEL_16;
case 'B':
case 'P':
func0(v8, 2);
v26 = v10++;
v12 = *(&v45 + v26);
v11 = *(_BYTE *)v8;
if ( *(_BYTE *)v8 )
continue;
goto LABEL_17;
case 'R':
func0(v8, 4);
v27 = v10++;
v12 = (char)*(&v45 + v27) + a1 + 2;
v11 = *(_BYTE *)v8;
if ( *(_BYTE *)v8 )
continue;
goto LABEL_17;
default:
LABEL_16:
v11 = *(_BYTE *)++v8;
if ( !v11 )
goto LABEL_17;
continue;
}
}
}
if ( v6 != -35 )
{
qmemcpy(debug_ins, " ", 9);
LABEL_72:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8);
goto LABEL_40;
}
v12 = -1;
LABEL_85:
v16 = -1;
goto LABEL_28;
}
LABEL_10:
v10 = 1;
goto LABEL_11;
}
v41 = v48;
v42 = v48 >> 6;
if ( v48 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v42 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( v42 != 1 )
{
sprintf(dest, "%s ", &oc_shift[4 * ((v48 >> 3) & 7)]);
goto LABEL_82;
}
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v48 >> 3) & 7);
LABEL_82:
v43 = v41 & 7;
v44 = &dest[strlen(dest)];
if ( v43 == 6 )
*(_DWORD *)v44 = 2709544;
else
sprintf(v44, "(X)->%s", &oc_op8[5 * v43]);
v15 = dest[0];
v10 = 2;
v12 = -1;
if ( !dest[0] )
goto LABEL_85;
LABEL_18:
v8 = (__int64)&dest[3];
v16 = -1;
do
{
while ( v15 != 88 )
{
v15 = *(_BYTE *)(v8 - 2);
++v8;
if ( !v15 )
goto LABEL_27;
}
v17 = _mm_loadu_si128((const __m128i *)(v8 - 2));
*(_BYTE *)(v8 - 3) = 105;
v18 = *(&v45 + v10);
*(_BYTE *)(v8 - 2) = 120;
*(__m128i *)(v8 + 1) = v17;
if ( v18 < 0 )
{
*(_BYTE *)(v8 - 1) = 45;
func0(v8, 2);
if ( v29 == -1 )
{
v12 = -v28;
goto LABEL_26;
}
}
else
{
*(_BYTE *)(v8 - 1) = 43;
func0(v8, 2);
if ( v21 == -1 )
{
v12 = v20;
goto LABEL_26;
}
}
v12 = v47;
v16 = v48;
v22 = 43;
if ( (v47 & 0x80u) != 0 )
{
v22 = 45;
v12 = -(char)v47;
}
*(_BYTE *)(v19 - 1) = v22;
LABEL_26:
v15 = *(_BYTE *)(v19 - 2);
v8 = v19 + 1;
++v10;
}
while ( v15 );
LABEL_27:
if ( v6 == -35 )
{
LABEL_28:
if ( v46 == 0xCB )
v10 = 4;
if ( v5 == 0xFD )
{
v23 = dest[0];
if ( dest[0] )
{
for ( i = &dest[1]; ; ++i )
{
v8 = (unsigned __int8)*i;
if ( v23 == 105 && (_BYTE)v8 == 120 )
{
v8 = (unsigned __int8)i[1];
*i++ = 121;
if ( !(_BYTE)v8 )
break;
}
else if ( !(_BYTE)v8 )
{
break;
}
v23 = v8;
}
}
}
}
qmemcpy(debug_ins, " ", 9);
if ( v16 == -1 )
goto LABEL_60;
LABEL_39:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v12);
LABEL_40:
for ( j = 0LL; ; v5 = *(&v45 + j) )
{
sprintf(&dest[2 * j++], "%02x", v5);
if ( v10 <= (int)j )
break;
}
v30 = debug_ins;
v31 = strlen(dest);
v32 = dest;
if ( v31 >= 8 )
{
LODWORD(v35) = 0;
do
{
v36 = (unsigned int)v35;
v35 = (unsigned int)(v35 + 8);
*(_QWORD *)&debug_ins[v36] = *(_QWORD *)&dest[v36];
}
while ( (unsigned int)v35 < (v31 & 0xFFFFFFF8) );
v30 = &debug_ins[v35];
v32 = &dest[v35];
}
v33 = 0LL;
if ( (v31 & 4) != 0 )
{
*(_DWORD *)v30 = *(_DWORD *)v32;
v33 = 4LL;
}
if ( (v31 & 2) != 0 )
{
*(_WORD *)&v30[v33] = *(_WORD *)&v32[v33];
v33 += 2LL;
}
if ( (v31 & 1) != 0 )
v30[v33] = v32[v33];
return (unsigned int)v10;
}

original_code:
int opcode_name(WORD adr) {
 BYTE oc[4];
 char tmp[64];
 int i, l = 1, par1 = -1, par2 = -1;
 for (i = 0; i < 4; i++) oc[i] = z80_acc(adr + i);
 if (oc[0] == 0xcb) {
  l = 2;
  switch (oc[1] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[1] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[1] >= 0x40) sprintf(tmp + 3, " %d,", (oc[1] >> 3) & 0x07);
  sprintf(tmp + strlen(tmp), "%s", oc_op8[oc[1] & 0x07]);
 } else if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) {
  l = 2;
  switch (oc[3] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[3] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[3] >= 0x40) sprintf(tmp + 3, " %d,", (oc[3] >> 3) & 0x07);
  if ((oc[3] & 0x07) == 0x06) sprintf(tmp + strlen(tmp), "(X)");
  else sprintf(tmp + strlen(tmp), "(X)->%s", oc_op8[oc[3] & 0x07]);
 } else {
  strcpy(tmp, oc_base[oc[0]]);
  if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] != 0xdd) && (oc[1] != 0xed) && (oc[1] != 0xfd)) {
   strcpy(tmp, oc_ix[oc[1]]);
   l++;
  }
  if (oc[0] == 0xed) {
   if ((oc[1] < 0x40) || (oc[1] >= 0xc0))
    switch (oc[1]) {
    case 0x00: strcpy(tmp, "pti_dump_registers"); break;
    case 0x01: strcpy(tmp, "pti_printf"); break;
    default: strcpy(tmp, "nop");
    }
   else strcpy(tmp, oc_ed[oc[1] - 0x40]);
   l++;
  }
  for (i = 0; tmp[i]; i++)
   switch (tmp[i]) {
    case 'A':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
    case 'B':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'P':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'R':
     insert_hex(tmp + i, 4);
     par1 = adr + 2 + (char)oc[l];
     l += 1;
    break;
    case 'W':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
   }
 }
 for (i = 0; tmp[i]; i++)
  if (tmp[i] == 'X') {
   memmove(tmp + i + 4, tmp + i + 1, 16);
   tmp[i] = 'i';
   tmp[i + 1] = 'x';
   tmp[i + 2] = ((char)oc[l] < 0) ? '-' : '+';
   insert_hex(tmp + i + 3, 2);
   if (par1 != -1) {
    par1 = ((char)oc[2] < 0) ? -(char)oc[2] : oc[2];
    par2 = oc[3];
    tmp[i + 2] = ((char)oc[2] < 0) ? '-' : '+';
   } else par1 = ((char)oc[l] < 0) ? -(char)oc[l] : oc[l];
   l++;
  }
 if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) l = 4;
 if (oc[0] == 0xfd) for (i = 0; tmp[i]; i++) if ((tmp[i] == 'i') && (tmp[i + 1] == 'x')) tmp[i + 1] = 'y';
 memset(debug_ins, ' ', 9);
 if (par2 != -1) sprintf(debug_ins + 9, tmp, par1, par2);
 else if (par1 != -1) sprintf(debug_ins + 9, tmp, par1);
 else sprintf(debug_ins + 9, tmp);
 for (i = 0; i < l; i++) sprintf(tmp + i * 2, "%02x", oc[i]);
 memcpy(debug_ins, tmp, strlen(tmp));
 return l;
}

canonical_original_code:
int opcode_name(unsigned short adr) {
 unsigned char oc[4];
 char tmp[64];
 int i; int l = 1; int par1 = -1; int par2 = -1;
 for (i = 0; i < 4; i++) oc[i] = z80_acc(adr + i);
 if (oc[0] == 0xcb) {
  l = 2;
  switch (oc[1] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[1] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[1] >= 0x40) sprintf(tmp + 3, " %d,", (oc[1] >> 3) & 0x07);
  sprintf(tmp + strlen(tmp), "%s", oc_op8[oc[1] & 0x07]);
 } else if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) {
  l = 2;
  switch (oc[3] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[3] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[3] >= 0x40) sprintf(tmp + 3, " %d,", (oc[3] >> 3) & 0x07);
  if ((oc[3] & 0x07) == 0x06) sprintf(tmp + strlen(tmp), "(X)");
  else sprintf(tmp + strlen(tmp), "(X)->%s", oc_op8[oc[3] & 0x07]);
 } else {
  strcpy(tmp, oc_base[oc[0]]);
  if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] != 0xdd) && (oc[1] != 0xed) && (oc[1] != 0xfd)) {
   strcpy(tmp, oc_ix[oc[1]]);
   l++;
  }
  if (oc[0] == 0xed) {
   if ((oc[1] < 0x40) || (oc[1] >= 0xc0))
    switch (oc[1]) {
    case 0x00: strcpy(tmp, "pti_dump_registers"); break;
    case 0x01: strcpy(tmp, "pti_printf"); break;
    default: strcpy(tmp, "nop");
    }
   else strcpy(tmp, oc_ed[oc[1] - 0x40]);
   l++;
  }
  for (i = 0; tmp[i]; i++)
   switch (tmp[i]) {
    case 'A':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
    case 'B':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'P':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'R':
     insert_hex(tmp + i, 4);
     par1 = adr + 2 + (char)oc[l];
     l += 1;
    break;
    case 'W':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
   }
 }
 for (i = 0; tmp[i]; i++)
  if (tmp[i] == 'X') {
   memmove(tmp + i + 4, tmp + i + 1, 16);
   tmp[i] = 'i';
   tmp[i + 1] = 'x';
   tmp[i + 2] = ((char)oc[l] < 0) ? '-' : '+';
   insert_hex(tmp + i + 3, 2);
   if (par1 != -1) {
    par1 = ((char)oc[2] < 0) ? -(char)oc[2] : oc[2];
    par2 = oc[3];
    tmp[i + 2] = ((char)oc[2] < 0) ? '-' : '+';
   } else par1 = ((char)oc[l] < 0) ? -(char)oc[l] : oc[l];
   l++;
  }
 if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) l = 4;
 if (oc[0] == 0xfd) for (i = 0; tmp[i]; i++) if ((tmp[i] == 'i') && (tmp[i + 1] == 'x')) tmp[i + 1] = 'y';
 memset(debug_ins, ' ', 9);
 if (par2 != -1) sprintf(debug_ins + 9, tmp, par1, par2);
 else if (par1 != -1) sprintf(debug_ins + 9, tmp, par1);
 else sprintf(debug_ins + 9, tmp);
 for (i = 0; i < l; i++) sprintf(tmp + i * 2, "%02x", oc[i]);
 memcpy(debug_ins, tmp, strlen(tmp));
 return l;
}

variable_types:
adr
unsigned short

variable_types:
oc
unsigned char [4]

variable_types:
tmp
char [64]


repo: cobbpg/pindurti

decompiled_code:
__int64 __fastcall prev_address(unsigned __int16 a1)
{
unsigned int i; // ebx

for ( i = a1 - 12; a1 > (int)(i + opcode_name(i)); i += opcode_name(i) )
;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
unsigned int i; // ebx

for ( i = a1 - 12; a1 > (int)(i + func1(i)); i += func1(i) )
;
return i;
}

original_code:
WORD prev_address(WORD a) {
 int i;
 for (i = a - 12; i + opcode_name(i) < a; i += opcode_name(i));
 return i;
}

canonical_original_code:
unsigned short prev_address(unsigned short a) {
 int i;
 for (i = a - 12; i + opcode_name(i) < a; i += opcode_name(i));
 return i;
}

variable_types:
a
unsigned short

variable_types: i int

return_type: unsigned short


decompiled_code:
__int64 __fastcall calculate_crc32(unsigned int *a1, int a2)
{
signed __int32 v3; // er8
unsigned __int8 *v4; // rdi
__int64 v5; // r9
unsigned __int8 v6; // si
int v7; // ecx
int v8; // eax

if ( a2 <= 3 )
{
v3 = -1;
}
else
{
v3 = ~_byteswap_ulong(*a1);
if ( a2 != 4 )
{
v4 = (unsigned __int8 *)(a1 + 1);
v5 = (__int64)a1 + (unsigned int)(a2 - 5) + 5;
do
{
v6 = *v4;
v7 = 8;
do
{
v8 = (v6 >> 7) | (2 * v3);
if ( v3 < 0 )
v8 = ((v6 >> 7) | (2 * v3)) ^ 0x4C11DB7;
v6 *= 2;
v3 = v8;
--v7;
}
while ( v7 );
++v4;
}
while ( v4 != (unsigned __int8 *)v5 );
}
}
return (unsigned int)v3;
}

original_code:
int calculate_crc32(unsigned char *tmp, int len) {
 unsigned char tb;
 int i, j, crc;
 if (len < 4) return -1;
 crc = ~((tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3]);
 for (i = 4; i < len; i++) {
  tb = tmp[i];
  for (j = 0; j < 8; j++) {
   crc = ((crc << 1) | (tb >> 7)) ^ ((crc & 0x80000000) ? 0x04c11db7 : 0);
   tb <<= 1;
  }
 }
 return crc;
}

canonical_original_code:
int calculate_crc32(unsigned char *tmp, int len) {
 unsigned char tb;
 int i; int j; int crc;
 if (len < 4) return -1;
 crc = ~((tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3]);
 for (i = 4; i < len; i++) {
  tb = tmp[i];
  for (j = 0; j < 8; j++) {
   crc = ((crc << 1) | (tb >> 7)) ^ ((crc & 0x80000000) ? 0x04c11db7 : 0);
   tb <<= 1;
  }
 }
 return crc;
}

variable_types:
tmp
unsigned char *

variable_types: len int

variable_types:
tb
unsigned char

variable_types: j int

variable_types: crc int

return_type: int


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

result = flash;
*(_DWORD *)flash = 0;
*(_QWORD *)(result + 8) = a2;
if ( !a1 )
{
*(_QWORD *)(result + 24) = &flash_83p;
*(_BYTE *)(result + 20) = 11;
*(_DWORD *)(result + 16) = 0x7FFFF;
}
return result;
}

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

result = flash;
*(_DWORD *)flash = 0;
*(_QWORD *)(result + 8) = a2;
if ( !a1 )
{
*(_QWORD *)(result + 24) = &flash_83p;
*(_BYTE *)(result + 20) = 11;
*(_DWORD *)(result + 16) = 0x7FFFF;
}
return result;
}

original_code:
void flash_reset(int type, BYTE *mem) {
 flash->phase = 0x00;
 flash->mem = mem;
 switch (type) {
  case 0x00:
   flash->pages = flash_83p;
   flash->pnum = 11;
   flash->mask = 0x7ffff;
  break;
 }
}

canonical_original_code:
void flash_reset(int type, unsigned char *mem) {
 flash->phase = 0x00;
 flash->mem = mem;
 switch (type) {
  case 0x00:
   flash->pages = flash_83p;
   flash->pnum = 11;
   flash->mask = 0x7ffff;
  break;
 }
}

variable_types: type int

variable_types:
mem
unsigned char *

return_type: void


original_code:
void flash_write(int adr, BYTE val) {
 adr &= flash->mask;
 switch (flash->phase & 0x03) {
  case 0:
   if ((val == 0xaa) && ((adr & 0xfff) == 0xaaa))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 1:
   if ((val == 0x55) && ((adr & 0xfff) == 0x555))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 2:
   switch (val) {
    case 0x10:
     if ((flash->phase == 0x22) && ((adr & 0xfff) == 0xaaa)) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (!flash->pages[i].flags)
        memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
     }
     flash->phase = 0x00;
    break;
    case 0x30:
     if (flash->phase == 0x22) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (adr < (flash->pages[i].start + flash->pages[i].size)) {
        if (!flash->pages[i].flags)
         memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
        break;
       }
     }
     flash->phase = 0x00;
    break;
    case 0x80:
     flash->phase = 0x20;
    break;
    case 0x90:
     flash->phase = 0x03;
    break;
    case 0xa0:
     flash->phase = 0x13;
    break;
   }
  break;
  case 3:
   if (flash->phase == 0x13)
    flash->mem[adr] &= val;
   flash->phase = 0x00;
  break;
 }
}

canonical_original_code:
void flash_write(int adr, unsigned char val) {
 adr &= flash->mask;
 switch (flash->phase & 0x03) {
  case 0:
   if ((val == 0xaa) && ((adr & 0xfff) == 0xaaa))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 1:
   if ((val == 0x55) && ((adr & 0xfff) == 0x555))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 2:
   switch (val) {
    case 0x10:
     if ((flash->phase == 0x22) && ((adr & 0xfff) == 0xaaa)) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (!flash->pages[i].flags)
        memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
     }
     flash->phase = 0x00;
    break;
    case 0x30:
     if (flash->phase == 0x22) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (adr < (flash->pages[i].start + flash->pages[i].size)) {
        if (!flash->pages[i].flags)
         memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
        break;
       }
     }
     flash->phase = 0x00;
    break;
    case 0x80:
     flash->phase = 0x20;
    break;
    case 0x90:
     flash->phase = 0x03;
    break;
    case 0xa0:
     flash->phase = 0x13;
    break;
   }
  break;
  case 3:
   if (flash->phase == 0x13)
    flash->mem[adr] &= val;
   flash->phase = 0x00;
  break;
 }
}

variable_types: adr int

variable_types:
val
unsigned char


repo: codemonkeyricky/piracast

decompiled_code:
int __fastcall sx_thread_create(pthread_t *a1, void *(*a2)(void *), __int64 a3, int a4)
{
pthread_t v5; // rdi
struct sched_param param; // [rsp+Ch] [rbp-14h] BYREF

pthread_create(a1, 0LL, a2, 0LL);
v5 = *(int *)a1;
param.__sched_priority = a4;
return pthread_setschedparam(v5, 1, &param);
}

canonical_decompiled_code:
int __fastcall func0(pthread_t *a1, void *(*a2)(void *), __int64 a3, int a4)
{
pthread_t v5; // rdi
struct sched_param param; // [rsp+Ch] [rbp-14h] BYREF

pthread_create(a1, 0LL, a2, 0LL);
v5 = *(int *)a1;
param.__sched_priority = a4;
return pthread_setschedparam(v5, 1, &param);
}

original_code:
void sx_thread_create(
    void *thread_id,
    void *thread_func,
    void *thread_arg,
    unsigned int thread_priority
    )
{
    struct sched_param param;
    pthread_create(thread_id, ((void *)0), (void *) thread_func, ((void *)0));
    memset(&param, 0, sizeof(param));
    param.sched_priority = thread_priority;
    pthread_setschedparam(*((int *) thread_id), 1, &param);
}

canonical_original_code:
void sx_thread_create(void *thread_id, void *thread_func, void *thread_arg, unsigned int thread_priority)
{
    struct sched_param param;
    pthread_create(thread_id, ((void *)0), (void *) thread_func, ((void *)0));
    memset(&param, 0, sizeof(param));
    param.sched_priority = thread_priority;
    pthread_setschedparam(*((int *) thread_id), 1, &param);
}

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
void sx_udp_destroy()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void sx_udp_destroy(
    void * id
    )
{
}

canonical_original_code:
void sx_udp_destroy(void *id)
{
}

variable_types:
id
void *


decompiled_code:
char *sx_queue_create()
{
char *v0; // r12
char *result; // rax

v0 = (char *)malloc(0x60uLL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
sem_init((sem_t *)(v0 + 56), 0, 0);
*(_QWORD *)v0 = 0LL;
result = v0;
*((_QWORD *)v0 + 1) = 0LL;
*((_DWORD *)v0 + 22) = 0;
return result;
}

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

v0 = (char *)malloc(0x60uLL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
sem_init((sem_t *)(v0 + 56), 0, 0);
*(_QWORD *)v0 = 0LL;
result = v0;
*((_QWORD *)v0 + 1) = 0LL;
*((_DWORD *)v0 + 22) = 0;
return result;
}

original_code:
void * sx_queue_create(
    void
    )
{
    sQUEUE *queue = malloc(sizeof(sQUEUE));
    pthread_mutex_init(&queue->lock, ((void *)0));
    sem_init(&queue->sem, 0, 0);
    queue->head = ((void *)0);
    queue->tail = ((void *)0);
    queue->len = 0;
    return queue;
}

canonical_original_code:
void *sx_queue_create()
{
    struct sQUEUE *queue = malloc(sizeof(sQUEUE));
    pthread_mutex_init(&queue->lock, ((void *)0));
    sem_init(&queue->sem, 0, 0);
    queue->head = ((void *)0);
    queue->tail = ((void *)0);
    queue->len = 0;
    return queue;
}

variable_types: struct sQUEUE { struct sNODE { struct sNODE *next; void *data; } *head; struct sNODE *tail; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; union sem_t { char __size[32]; long __align; } sem; unsigned int len; unsigned int high_water_mark; } *

return_type: void *

user_defined_types: struct sQUEUE { struct sNODE *head; struct sNODE *tail; union pthread_mutex_t lock; union sem_t sem; unsigned int len; unsigned int high_water_mark; }

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

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


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

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1;
v1 = (void **)*v1;
free(v2[1]);
free(v2);
}
while ( v1 );
}
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

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

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1;
v1 = (void **)*v1;
free(v2[1]);
free(v2);
}
while ( v1 );
}
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

original_code:
void sx_queue_destroy(
    void * queue_id
    )
{
    sQUEUE *queue;
    sNODE *curr;
    sNODE *next;
    queue = queue_id;
    curr = queue->head;
    while(curr)
    {
        next = curr->next;
        free(curr->data);
        free(curr);
        curr = next;
    }
    pthread_mutex_destroy(&queue->lock);
    free(queue);
}

canonical_original_code:
void sx_queue_destroy(void *queue_id)
{
    struct sQUEUE *queue;
    struct sNODE *curr;
    struct sNODE *next;
    queue = queue_id;
    curr = queue->head;
    while(curr)
    {
        next = curr->next;
        free(curr->data);
        free(curr);
        curr = next;
    }
    pthread_mutex_destroy(&queue->lock);
    free(queue);
}

variable_types: struct sNODE { struct sNODE *next; void *data; } *

user_defined_types: union sem_t { char __size[32]; long __align; }

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


decompiled_code:
int __fastcall sx_queue_push(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r12

if ( !a2 )
_assert_fail("data != NULL", "./src/sx_queue.c", 0x68u, "sx_queue_push");
v2 = malloc(0x10uLL);
v2[1] = a2;
v3 = v2;
*v2 = 0LL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_QWORD *)a1 )
**(_QWORD **)(a1 + 8) = v3;
else
*(_QWORD *)a1 = v3;
*(_QWORD *)(a1 + 8) = v3;
++*(_DWORD *)(a1 + 88);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r12

if ( !a2 )
_assert_fail("data != NULL", "./src/sx_queue.c", 0x68u, "sx_queue_push");
v2 = malloc(0x10uLL);
v2[1] = a2;
v3 = v2;
*v2 = 0LL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_QWORD *)a1 )
**(_QWORD **)(a1 + 8) = v3;
else
*(_QWORD *)a1 = v3;
*(_QWORD *)(a1 + 8) = v3;
++*(_DWORD *)(a1 + 88);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void sx_queue_push(
    void * queue_id,
    void *data
    )
{
    sNODE *node;
    sQUEUE *queue;
    ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "./src/sx_queue.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    queue = queue_id;
    node = malloc(sizeof(sNODE));
    node->data = data;
    node->next = ((void *)0);
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        queue->head = node;
        queue->tail = node;
        goto cleanup;
    }
    queue->tail->next = node;
    queue->tail = node;
cleanup:
    queue->len++;
    pthread_mutex_unlock(&queue->lock);
}

canonical_original_code:
void sx_queue_push(void *queue_id, void *data)
{
    struct sNODE *node;
    struct sQUEUE *queue;
    ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "./src/sx_queue.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    queue = queue_id;
    node = malloc(sizeof(sNODE));
    node->data = data;
    node->next = ((void *)0);
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        queue->head = node;
        queue->tail = node;
        goto cleanup;
    }
    queue->tail->next = node;
    queue->tail = node;
cleanup:
    queue->len++;
    pthread_mutex_unlock(&queue->lock);
}

variable_types:
data
void *

user_defined_types: struct sNODE { struct sNODE *next; void *data; }


decompiled_code:
__int64 __fastcall sx_queue_pull(__int64 a1)
{
pthread_mutex_t *v1; // r12
__int64 *v3; // rdi
__int64 v4; // r14
__int64 v5; // r13

v1 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v3 = *(__int64 **)a1;
if ( v3 )
{
v4 = *v3;
if ( v3 == *(__int64 **)(a1 + 8) )
{
if ( v4 )
_assert_fail("queue->head->next == NULL", "./src/sx_queue.c", 0xC6u, "sx_queue_pull");
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = v4;
}
if ( v5 )
--*(_DWORD *)(a1 + 88);
}
else
{
v5 = *(_QWORD *)(a1 + 8);
if ( v5 )
_assert_fail("queue->tail == NULL", "./src/sx_queue.c", 0xBCu, "sx_queue_pull");
}
pthread_mutex_unlock(v1);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
pthread_mutex_t *v1; // r12
__int64 *v3; // rdi
__int64 v4; // r14
__int64 v5; // r13

v1 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v3 = *(__int64 **)a1;
if ( v3 )
{
v4 = *v3;
if ( v3 == *(__int64 **)(a1 + 8) )
{
if ( v4 )
_assert_fail("queue->head->next == NULL", "./src/sx_queue.c", 0xC6u, "sx_queue_pull");
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = v4;
}
if ( v5 )
--*(_DWORD *)(a1 + 88);
}
else
{
v5 = *(_QWORD *)(a1 + 8);
if ( v5 )
_assert_fail("queue->tail == NULL", "./src/sx_queue.c", 0xBCu, "sx_queue_pull");
}
pthread_mutex_unlock(v1);
return v5;
}

original_code:
void * sx_queue_pull(
    void * queue_id
    )
{
    sQUEUE *queue;
    unsigned char *data;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        ((void) sizeof ((queue->tail == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail == ((void *)0)) ; else __assert_fail ("queue->tail == NULL", "./src/sx_queue.c", 188, __extension__ __PRETTY_FUNCTION__); }));
        data = ((void *)0);
        goto cleanup;
    }
    if(queue->head == queue->tail)
    {
        ((void) sizeof ((queue->head->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->head->next == ((void *)0)) ; else __assert_fail ("queue->head->next == NULL", "./src/sx_queue.c", 198, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((queue->tail->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail->next == ((void *)0)) ; else __assert_fail ("queue->tail->next == NULL", "./src/sx_queue.c", 199, __extension__ __PRETTY_FUNCTION__); }));
        data = queue->head->data;
        free(queue->head);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        goto cleanup;
    }
    data = queue->head->data;
    sNODE *next = queue->head->next;
    free(queue->head);
    queue->head = next;
cleanup:
    if(data != ((void *)0))
    {
        queue->len--;
    }
    pthread_mutex_unlock(&queue->lock);
    return data;
}

canonical_original_code:
void *sx_queue_pull(void *queue_id)
{
    struct sQUEUE *queue;
    unsigned char *data;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        ((void) sizeof ((queue->tail == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail == ((void *)0)) ; else __assert_fail ("queue->tail == NULL", "./src/sx_queue.c", 188, __extension__ __PRETTY_FUNCTION__); }));
        data = ((void *)0);
        goto cleanup;
    }
    if(queue->head == queue->tail)
    {
        ((void) sizeof ((queue->head->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->head->next == ((void *)0)) ; else __assert_fail ("queue->head->next == NULL", "./src/sx_queue.c", 198, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((queue->tail->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail->next == ((void *)0)) ; else __assert_fail ("queue->tail->next == NULL", "./src/sx_queue.c", 199, __extension__ __PRETTY_FUNCTION__); }));
        data = queue->head->data;
        free(queue->head);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        goto cleanup;
    }
    data = queue->head->data;
    struct sNODE *next = queue->head->next;
    free(queue->head);
    queue->head = next;
cleanup:
    if(data != ((void *)0))
    {
        queue->len--;
    }
    pthread_mutex_unlock(&queue->lock);
    return data;
}

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall sx_queue_len_get(__int64 a1)
{
unsigned int v1; // er13

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 88);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned int v1; // er13

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 88);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v1;
}

original_code:
unsigned int sx_queue_len_get(
    void * queue_id
    )
{
    sQUEUE *queue;
    unsigned int len;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    len = queue->len;
    pthread_mutex_unlock(&queue->lock);
    return len;
}

canonical_original_code:
unsigned int sx_queue_len_get(void *queue_id)
{
    struct sQUEUE *queue;
    unsigned int len;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    len = queue->len;
    pthread_mutex_unlock(&queue->lock);
    return len;
}

variable_types:
len
unsigned int

return_type: unsigned int


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

result = qword_540;
qword_548 = 0LL;
qword_540 = 0LL;
return result;
}

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

result = qword_540;
qword_548 = 0LL;
qword_540 = 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;
}

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


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

canonical_decompiled_code:
int func2()
{
sx_desc_put(qword_540);
qword_540 = 0LL;
qword_548 = 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");
}

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


decompiled_code:
__int64 __fastcall audio_decoder_slice_dump(__int64 a1)
{
return sx_pipe_put(6LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return sx_pipe_put(6LL, a1);
}

original_code:
static void audio_decoder_slice_dump(
    sSX_DESC *slice_head
    )
{
    sx_pipe_put(6, slice_head);
}

canonical_original_code:
static void audio_decoder_slice_dump(struct sSX_DESC *slice_head)
{
    sx_pipe_put(6, slice_head);
}


decompiled_code:
void __noreturn audio_decoder_thread()
{
__int64 v0; // rax
__int64 v1; // r12
unsigned int v2; // esi
__int64 v3; // rbx
int v4; // eax
uint16_t v5; // di
int v6; // eax
int v7; // eax
__int64 v8; // rdi
__int64 v9; // rax

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(5LL);
v1 = v0;
if ( v0 )
break;
usleep(0x3E8u);
}
v2 = *(_DWORD *)(v0 + 8);
v3 = *(_QWORD *)v0;
if ( v2 <= 0xC )
break;
if ( (_DWORD)qword_528 && (unsigned __int8)slice_start_find_constprop_0_isra_0(*(_QWORD *)v0, v2) )
{
v5 = *(_WORD *)(v3 + 2);
qword_528 = 0x100000000LL;
dword_534 = 0;
LOWORD(v6) = ntohs(v5);
dword_538 = v6 - 1;
}
if ( HIDWORD(qword_528) )
{
LOWORD(v4) = ntohs(*(_WORD *)(v3 + 2));
if ( v4 == dword_538 + 1 )
{
LOWORD(v7) = ntohs(*(_WORD *)(v3 + 2));
v8 = *(_QWORD *)v1;
dword_538 = v7;
dword_534 += pes_payload_size_isra_0(v8, *(_DWORD *)(v1 + 8));
if ( dword_534 > (unsigned int)dword_530 )
_assert_fail(
"f_cblk.pes_curr_byte_count <= f_cblk.pes_len",
"./src/sx_mgmt_audio_decoder.c",
0x15Fu,
"audio_decoder_thread");
slice_pkt_add(v1);
if ( dword_534 == dword_530 )
{
qword_528 = 1LL;
v9 = slice_get();
audio_decoder_slice_dump(v9);
}
}
else
{
slice_drop();
qword_528 = 1LL;
}
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 0x133u, "audio_decoder_thread");
}

canonical_decompiled_code:
void __noreturn func8()
{
__int64 v0; // rax
__int64 v1; // r12
unsigned int v2; // esi
__int64 v3; // rbx
int v4; // eax
uint16_t v5; // di
int v6; // eax
int v7; // eax
__int64 v8; // rdi
__int64 v9; // rax

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(5LL);
v1 = v0;
if ( v0 )
break;
usleep(0x3E8u);
}
v2 = *(_DWORD *)(v0 + 8);
v3 = *(_QWORD *)v0;
if ( v2 <= 0xC )
break;
if ( (_DWORD)qword_528 && (unsigned __int8)func6(*(_QWORD *)v0, v2) )
{
v5 = *(_WORD *)(v3 + 2);
qword_528 = 0x100000000LL;
dword_534 = 0;
LOWORD(v6) = ntohs(v5);
dword_538 = v6 - 1;
}
if ( HIDWORD(qword_528) )
{
LOWORD(v4) = ntohs(*(_WORD *)(v3 + 2));
if ( v4 == dword_538 + 1 )
{
LOWORD(v7) = ntohs(*(_WORD *)(v3 + 2));
v8 = *(_QWORD *)v1;
dword_538 = v7;
dword_534 += func7(v8, *(_DWORD *)(v1 + 8));
if ( dword_534 > (unsigned int)dword_530 )
_assert_fail(
"f_cblk.pes_curr_byte_count <= f_cblk.pes_len",
"./src/sx_mgmt_audio_decoder.c",
0x15Fu,
"audio_decoder_thread");
func1(v1);
if ( dword_534 == dword_530 )
{
qword_528 = 1LL;
v9 = func0();
func3(v9);
}
}
else
{
func2();
qword_528 = 1LL;
}
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 0x133u, "audio_decoder_thread");
}

original_code:
void audio_decoder_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    sMPEG2_TS *ts;
    unsigned char eos;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(5);
            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_audio_decoder.c", 307, __extension__ __PRETTY_FUNCTION__); }));
            sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            if(f_cblk.look_for_new_slice)
            {
                if(slice_start_find(desc, &f_cblk.pes_len))
                {
                    f_cblk.look_for_new_slice = 0;
                    f_cblk.continue_current_slice = 1;
                    f_cblk.pes_curr_byte_count = 0;
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num) - 1;
                }
            }
            if(f_cblk.continue_current_slice)
            {
                if(ntohs(rtp_hdr->sequence_num) != (f_cblk.last_seq_num + 1))
                {
                    slice_drop();
                    f_cblk.continue_current_slice = 0;
                    f_cblk.look_for_new_slice = 1;
                }
                else
                {
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
                    unsigned int pes_size = pes_payload_size(desc);
                    f_cblk.pes_curr_byte_count += pes_size;
                    ((void) sizeof ((f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ? 1 : 0), __extension__ ({ if (f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ; else __assert_fail ("f_cblk.pes_curr_byte_count <= f_cblk.pes_len", "./src/sx_mgmt_audio_decoder.c", 351, __extension__ __PRETTY_FUNCTION__); }));
                    slice_pkt_add(desc);
                    if(f_cblk.pes_curr_byte_count == f_cblk.pes_len)
                    {
                        f_cblk.continue_current_slice = 0;
                        f_cblk.look_for_new_slice = 1;
                        audio_decoder_slice_dump(slice_get());
                    }
                }
            }
        } while (1);
        usleep(1000);
    }
}

canonical_original_code:
void audio_decoder_thread(void *arg)
{
    struct sSX_DESC *desc;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    struct sMPEG2_TS *ts;
    unsigned char eos;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(5);
            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_audio_decoder.c", 307, __extension__ __PRETTY_FUNCTION__); }));
            struct sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            if(f_cblk.look_for_new_slice)
            {
                if(slice_start_find(desc, &f_cblk.pes_len))
                {
                    f_cblk.look_for_new_slice = 0;
                    f_cblk.continue_current_slice = 1;
                    f_cblk.pes_curr_byte_count = 0;
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num) - 1;
                }
            }
            if(f_cblk.continue_current_slice)
            {
                if(ntohs(rtp_hdr->sequence_num) != (f_cblk.last_seq_num + 1))
                {
                    slice_drop();
                    f_cblk.continue_current_slice = 0;
                    f_cblk.look_for_new_slice = 1;
                }
                else
                {
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
                    unsigned int pes_size = pes_payload_size(desc);
                    f_cblk.pes_curr_byte_count += pes_size;
                    ((void) sizeof ((f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ? 1 : 0), __extension__ ({ if (f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ; else __assert_fail ("f_cblk.pes_curr_byte_count <= f_cblk.pes_len", "./src/sx_mgmt_audio_decoder.c", 351, __extension__ __PRETTY_FUNCTION__); }));
                    slice_pkt_add(desc);
                    if(f_cblk.pes_curr_byte_count == f_cblk.pes_len)
                    {
                        f_cblk.continue_current_slice = 0;
                        f_cblk.look_for_new_slice = 1;
                        audio_decoder_slice_dump(slice_get());
                    }
                }
            }
        } while (1);
        usleep(1000);
    }
}

variable_types:
arg
void *

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:
eos
unsigned char

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 { struct sMPEG2_TS_HDR hdr; struct sMPEG2_TS_PAYLOAD payload; }


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

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

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

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


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

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

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

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


decompiled_code:
void sx_mgmt_audio_decoder_close()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void sx_mgmt_audio_decoder_close(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_audio_decoder_close()
{
}


decompiled_code:
__int64 pkt_rx_thread_create()
{
return sx_thread_create(&f_cblk, pkt_rx_thread, 0LL, 90LL);
}

canonical_decompiled_code:
__int64 func0()
{
return sx_thread_create(&f_cblk, pkt_rx_thread, 0LL, 90LL);
}

original_code:
static void pkt_rx_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.pkt_rx_thread, &pkt_rx_thread, ((void *)0), 90);
}

canonical_original_code:
static void pkt_rx_thread_create()
{
    sx_thread_create(&f_cblk.pkt_rx_thread, &pkt_rx_thread, ((void *)0), 90);
}


decompiled_code:
void __fastcall __noreturn pkt_rx_thread()
{
uint16_t v0; // r13
_WORD *v1; // rax
_WORD *v2; // rbx
uint16_t v3; // ax
uint16_t v4; // r12
__int64 v5; // rax
int v6[9]; // [rsp+Ch] [rbp-24h] BYREF

while ( 1 )
{
v1 = malloc(0x5C0uLL);
v6[0] = 1472;
v2 = v1;
sx_udp_recv(qword_118, v1, v6);
v3 = ntohs(v2[1]);
v4 = v3;
if ( v0 + 1 != v3 )
printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n", v0, v3, v3 - ((unsigned int)v0 + 1));
v5 = sx_desc_get();
v0 = v4;
*(_QWORD *)v5 = v2;
*(_DWORD *)(v5 + 8) = v6[0];
sx_pipe_put(0LL);
++dword_120;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1()
{
uint16_t v0; // r13
_WORD *v1; // rax
_WORD *v2; // rbx
uint16_t v3; // ax
uint16_t v4; // r12
__int64 v5; // rax
int v6[9]; // [rsp+Ch] [rbp-24h] BYREF

while ( 1 )
{
v1 = malloc(0x5C0uLL);
v6[0] = 1472;
v2 = v1;
sx_udp_recv(qword_118, v1, v6);
v3 = ntohs(v2[1]);
v4 = v3;
if ( v0 + 1 != v3 )
printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n", v0, v3, v3 - ((unsigned int)v0 + 1));
v5 = sx_desc_get();
v0 = v4;
*(_QWORD *)v5 = v2;
*(_DWORD *)(v5 + 8) = v6[0];
sx_pipe_put(0LL);
++dword_120;
}
}

original_code:
static void pkt_rx_thread(
    void * arg
    )
{
    sPI_PORTAL_PKT *pkt;
    unsigned short last_seq_num;
    unsigned short curr_seq_num;
    while(1)
    {
        pkt = malloc(sizeof(sPI_PORTAL_PKT));
        unsigned int pkt_len = sizeof(sPI_PORTAL_PKT);
        sx_udp_recv(f_cblk.udp_sock, (char *) pkt, &pkt_len);
        sRTP_HDR *rtp_hdr = (sRTP_HDR *) pkt;
        curr_seq_num = ntohs(rtp_hdr->sequence_num);
        if((unsigned short) (last_seq_num + 1) != curr_seq_num)
        {
            printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n",
                    last_seq_num,
                    curr_seq_num,
                    curr_seq_num - (last_seq_num + 1));
        }
        last_seq_num = curr_seq_num;
        sSX_DESC *desc = sx_desc_get();
        desc->data = (void *) pkt;
        desc->data_len = pkt_len;
        sx_pipe_put(0, desc);
        f_cblk.rx_pkt++;
    }
}

canonical_original_code:
static void pkt_rx_thread(void *arg)
{
    struct sPI_PORTAL_PKT *pkt;
    unsigned short last_seq_num;
    unsigned short curr_seq_num;
    while(1)
    {
        pkt = malloc(sizeof(sPI_PORTAL_PKT));
        unsigned int pkt_len = sizeof(sPI_PORTAL_PKT);
        sx_udp_recv(f_cblk.udp_sock, (char *) pkt, &pkt_len);
        struct sRTP_HDR *rtp_hdr = (sRTP_HDR *) pkt;
        curr_seq_num = ntohs(rtp_hdr->sequence_num);
        if((unsigned short) (last_seq_num + 1) != curr_seq_num)
        {
            printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n",
                    last_seq_num,
                    curr_seq_num,
                    curr_seq_num - (last_seq_num + 1));
        }
        last_seq_num = curr_seq_num;
        struct sSX_DESC *desc = sx_desc_get();
        desc->data = (void *) pkt;
        desc->data_len = pkt_len;
        sx_pipe_put(0, desc);
        f_cblk.rx_pkt++;
    }
}

variable_types: struct sPI_PORTAL_PKT { unsigned int hdr; unsigned char payload[-1]; } *

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


decompiled_code:
int sx_mgmt_data_init()
{
qword_118 = sx_udp_create(50000LL);
return puts("(mgmt_data): mgmt_data_init(): Initialized.");
}

canonical_decompiled_code:
int func2()
{
qword_118 = sx_udp_create(50000LL);
return puts("(mgmt_data): mgmt_data_init(): Initialized.");
}

original_code:
void sx_mgmt_data_init(
    void
    )
{
    f_cblk.udp_sock = sx_udp_create(50000);
    printf("(mgmt_data): mgmt_data_init(): Initialized.\n");
}

canonical_original_code:
void sx_mgmt_data_init()
{
    f_cblk.udp_sock = sx_udp_create(50000);
    printf("(mgmt_data): mgmt_data_init(): Initialized.\n");
}


decompiled_code:
int sx_mgmt_data_open()
{
pkt_rx_thread_create();
return puts("(mgmt_data_open): Opened.");
}

canonical_decompiled_code:
int func3()
{
func0();
return puts("(mgmt_data_open): Opened.");
}

original_code:
void sx_mgmt_data_open(
    void
    )
{
    pkt_rx_thread_create();
    printf("(mgmt_data_open): Opened.\n");
}

canonical_original_code:
void sx_mgmt_data_open()
{
    pkt_rx_thread_create();
    printf("(mgmt_data_open): Opened.\n");
}


decompiled_code:
void __noreturn mgmt_sys_thread()
{
while ( 1 )
usleep(0xF4240u);
}

canonical_decompiled_code:
void __noreturn func0()
{
while ( 1 )
usleep(0xF4240u);
}

original_code:
static void mgmt_sys_thread(
    void * arg
    )
{
    do
    {
        usleep(1*1000*1000);
    } while(1);
}

canonical_original_code:
static void mgmt_sys_thread(void *arg)
{
    do
    {
        usleep(1*1000*1000);
    } while(1);
}


decompiled_code:
int wait_forever()
{
return pthread_join(f_mgmt_sys_thread, 0LL);
}

canonical_decompiled_code:
int func1()
{
return pthread_join(f_mgmt_sys_thread, 0LL);
}

original_code:
static void wait_forever(
    void
    )
{
    pthread_join(f_mgmt_sys_thread, ((void *)0));
}

canonical_original_code:
static void wait_forever()
{
    pthread_join(f_mgmt_sys_thread, ((void *)0));
}


decompiled_code:
int mgmt_sys_init()
{
sx_pipe_init();
sx_mgmt_data_init();
sx_mgmt_m2ts_decoder_init();
sx_mgmt_video_decoder_init();
sx_mgmt_video_scheduler_init();
sx_mgmt_audio_decoder_init();
sx_mgmt_audio_scheduler_init();
return puts("(mgmt_sys): (mgmt_sys_init): Done.");
}

canonical_decompiled_code:
int func2()
{
sx_pipe_init();
sx_mgmt_data_init();
sx_mgmt_m2ts_decoder_init();
sx_mgmt_video_decoder_init();
sx_mgmt_video_scheduler_init();
sx_mgmt_audio_decoder_init();
sx_mgmt_audio_scheduler_init();
return puts("(mgmt_sys): (mgmt_sys_init): Done.");
}

original_code:
void mgmt_sys_init(
    void
    )
{
    resource_init();
    sx_pipe_init();
    sx_mgmt_data_init();
    sx_mgmt_m2ts_decoder_init();
    sx_mgmt_video_decoder_init();
    sx_mgmt_video_scheduler_init();
    sx_mgmt_audio_decoder_init();
    sx_mgmt_audio_scheduler_init();
    printf("(mgmt_sys): (mgmt_sys_init): Done.\n");
}

canonical_original_code:
void mgmt_sys_init()
{
    resource_init();
    sx_pipe_init();
    sx_mgmt_data_init();
    sx_mgmt_m2ts_decoder_init();
    sx_mgmt_video_decoder_init();
    sx_mgmt_video_scheduler_init();
    sx_mgmt_audio_decoder_init();
    sx_mgmt_audio_scheduler_init();
    printf("(mgmt_sys): (mgmt_sys_init): Done.\n");
}


decompiled_code:
int mgmt_sys_open()
{
sx_mgmt_data_open();
sx_mgmt_m2ts_decoder_open();
sx_mgmt_video_decoder_open();
sx_mgmt_video_scheduler_open();
sx_mgmt_audio_decoder_open();
sx_mgmt_audio_scheduler_open();
puts("(mgmt_sys): (mgmt_sys_open): Done.");
sx_thread_create(&f_mgmt_sys_thread, mgmt_sys_thread, 0LL, 50LL);
return wait_forever();
}

canonical_decompiled_code:
int func3()
{
sx_mgmt_data_open();
sx_mgmt_m2ts_decoder_open();
sx_mgmt_video_decoder_open();
sx_mgmt_video_scheduler_open();
sx_mgmt_audio_decoder_open();
sx_mgmt_audio_scheduler_open();
puts("(mgmt_sys): (mgmt_sys_open): Done.");
sx_thread_create(&f_mgmt_sys_thread, mgmt_sys_thread, 0LL, 50LL);
return func1();
}

original_code:
void mgmt_sys_open(
    void
    )
{
    sx_mgmt_data_open();
    sx_mgmt_m2ts_decoder_open();
    sx_mgmt_video_decoder_open();
    sx_mgmt_video_scheduler_open();
    sx_mgmt_audio_decoder_open();
    sx_mgmt_audio_scheduler_open();
    printf("(mgmt_sys): (mgmt_sys_open): Done.\n");
    sx_thread_create(&f_mgmt_sys_thread, &mgmt_sys_thread, ((void *)0), 50);
    wait_forever();
}

canonical_original_code:
void mgmt_sys_open()
{
    sx_mgmt_data_open();
    sx_mgmt_m2ts_decoder_open();
    sx_mgmt_video_decoder_open();
    sx_mgmt_video_scheduler_open();
    sx_mgmt_audio_decoder_open();
    sx_mgmt_audio_scheduler_open();
    printf("(mgmt_sys): (mgmt_sys_open): Done.\n");
    sx_thread_create(&f_mgmt_sys_thread, &mgmt_sys_thread, ((void *)0), 50);
    wait_forever();
}


decompiled_code:
__int64 pkt_process_thread_create()
{
return sx_thread_create(&f_cblk, pkt_process_thread, 0LL, 80LL);
}

canonical_decompiled_code:
__int64 func0()
{
return sx_thread_create(&f_cblk, pkt_process_thread, 0LL, 80LL);
}

original_code:
static void pkt_process_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.pkt_process_thread, &pkt_process_thread, ((void *)0), 80);
}

canonical_original_code:
static void pkt_process_thread_create()
{
    sx_thread_create(&f_cblk.pkt_process_thread, &pkt_process_thread, ((void *)0), 80);
}


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

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

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

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

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 }


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


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


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

canonical_decompiled_code:
__int64 func3()
{
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:
__int64 __fastcall audio_scheduler_slice_dump(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r12
unsigned int v3; // er15
unsigned int v4; // eax
unsigned int v5; // eax
unsigned __int8 *v6; // rbx
unsigned int v7; // er14
int v8; // eax
char *v9; // rdi
unsigned __int8 *v10; // rsi
__int64 v12; // [rsp+0h] [rbp-40h]
__int16 *v13; // [rsp+8h] [rbp-38h]

v12 = a1;
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;
}
v2 = a1;
v3 = 0;
v13 = (__int16 *)sx_audio_sink_buffer_get();
do
{
v4 = *(_DWORD *)(v2 + 8);
if ( v4 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_audio_scheduler.c",
0x92u,
"audio_scheduler_slice_dump");
v5 = v4 - 12;
v6 = (unsigned __int8 *)(*(_QWORD *)v2 + 12LL);
if ( __ROR4__(1736263375 * v5, 2) > 0x15C9882u )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_audio_scheduler.c",
0x9Cu,
"audio_scheduler_slice_dump");
v7 = v5 + (_DWORD)v6;
do
{
while ( (v6[2] | (v6[1] << 8) & 0x1F00) != 4352 )
{
v6 += 188;
if ( v7 == (_DWORD)v6 )
goto LABEL_15;
}
v8 = ((int)v6[3] >> 4) & 2;
if ( v8 )
v8 = (unsigned __int8)(v6[4] + 1);
if ( (v6[1] & 0x40) != 0 )
v8 += 20;
v9 = (char *)v13 + v3;
v10 = &v6[v8 + 4];
v6 += 188;
v3 += 184 - v8;
memcpy(v9, v10, (unsigned int)(184 - v8));
}
while ( v7 != (_DWORD)v6 );
LABEL_15:
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
sx_desc_put(v12);
if ( v3 != 1920 )
_assert_fail("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 0xF2u, "audio_scheduler_slice_dump");
audio_endianness_convert_constprop_0(v13);
return sx_audio_sink_buffer_set(v13, 1920LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r12
unsigned int v3; // er15
unsigned int v4; // eax
unsigned int v5; // eax
unsigned __int8 *v6; // rbx
unsigned int v7; // er14
int v8; // eax
char *v9; // rdi
unsigned __int8 *v10; // rsi
__int64 v12; // [rsp+0h] [rbp-40h]
__int16 *v13; // [rsp+8h] [rbp-38h]

v12 = a1;
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;
}
v2 = a1;
v3 = 0;
v13 = (__int16 *)sx_audio_sink_buffer_get();
do
{
v4 = *(_DWORD *)(v2 + 8);
if ( v4 <= 0xC )
_assert_fail(
"bytes_left > sizeof(sRTP_HDR)",
"./src/sx_mgmt_audio_scheduler.c",
0x92u,
"audio_scheduler_slice_dump");
v5 = v4 - 12;
v6 = (unsigned __int8 *)(*(_QWORD *)v2 + 12LL);
if ( __ROR4__(1736263375 * v5, 2) > 0x15C9882u )
_assert_fail(
"(bytes_left % sizeof(sMPEG2_TS)) == 0",
"./src/sx_mgmt_audio_scheduler.c",
0x9Cu,
"audio_scheduler_slice_dump");
v7 = v5 + (_DWORD)v6;
do
{
while ( (v6[2] | (v6[1] << 8) & 0x1F00) != 4352 )
{
v6 += 188;
if ( v7 == (_DWORD)v6 )
goto LABEL_15;
}
v8 = ((int)v6[3] >> 4) & 2;
if ( v8 )
v8 = (unsigned __int8)(v6[4] + 1);
if ( (v6[1] & 0x40) != 0 )
v8 += 20;
v9 = (char *)v13 + v3;
v10 = &v6[v8 + 4];
v6 += 188;
v3 += 184 - v8;
memcpy(v9, v10, (unsigned int)(184 - v8));
}
while ( v7 != (_DWORD)v6 );
LABEL_15:
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
sx_desc_put(v12);
if ( v3 != 1920 )
_assert_fail("copy_index == 1920", "./src/sx_mgmt_audio_scheduler.c", 0xF2u, "audio_scheduler_slice_dump");
func4(v13);
return sx_audio_sink_buffer_set(v13, 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:
afc
unsigned int

variable_types:
pid
unsigned int

variable_types:
buf
unsigned char *

variable_types:
stuffing
unsigned char

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 __noreturn audio_scheduler_thread()
{
unsigned int v0; // eax
int v1; // er12
unsigned int v2; // eax
unsigned int v3; // er12
__int64 v4; // rdi

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

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

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


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

canonical_decompiled_code:
int func7()
{
func2();
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()
{
func3();
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 func2()
{
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_B90;
qword_B98 = 0LL;
qword_B90 = 0LL;
return result;
}

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

result = qword_B90;
qword_B98 = 0LL;
qword_B90 = 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");
v2[1] = a1;
*(_BYTE *)v2 = 1;
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 func3(__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");
v2[1] = a1;
*(_BYTE *)v2 = 1;
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_B90);
qword_B90 = 0LL;
qword_B98 = 0LL;
return puts("slice_dropped() invoked");
}

canonical_decompiled_code:
int func4()
{
sx_desc_put(qword_B90);
qword_B90 = 0LL;
qword_B98 = 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()
{
__int64 v0; // rax
__int64 v1; // r12
unsigned int v2; // esi
__int64 v3; // rbx
char v4; // al
__int64 v5; // rax
__int64 v6; // rax
unsigned __int16 v7; // ax
unsigned __int8 v8; // [rsp+Eh] [rbp-22h] BYREF
unsigned __int8 v9; // [rsp+Fh] [rbp-21h] BYREF
unsigned int v10; // [rsp+10h] [rbp-20h] BYREF
unsigned int v11; // [rsp+14h] [rbp-1Ch] BYREF
unsigned __int64 v12[3]; // [rsp+18h] [rbp-18h] BYREF

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(1LL);
v1 = v0;
if ( v0 )
break;
usleep(0x1F40u);
}
v2 = *(_DWORD *)(v0 + 8);
v3 = *(_QWORD *)v0;
if ( v2 <= 0xC )
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 0x1C0u, "video_decoder_thread");
v4 = slice_start_find_isra_0(*(_QWORD *)v0, v2, &v10, v12);
if ( dword_B68 )
break;
if ( v4 )
goto LABEL_6;
LABEL_11:
m2ts_data_interpret_isra_0(*(_QWORD *)v1, *(_DWORD *)(v1 + 8), &v11, &v8, &v9);
dword_B6C += v11;
if ( dword_B74 && (((_BYTE)dword_B70 + 1) & 0xF) != v8 )
{
printf("(video_decoder): last rtp seq num = %d\n", (unsigned __int16)word_B88);
v7 = _bswap_16(*(_WORD *)(v3 + 2));
printf("(video_decoder): curr rtp seq num = %d\n", v7);
printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n", (unsigned int)dword_B70, v8);
slice_drop();
dword_B68 = 1;
dword_B74 = 0;
}
else
{
dword_B70 = v9;
slice_pkt_add(v1);
++dword_B74;
}
LABEL_14:
word_B88 = _bswap_16(*(_WORD *)(v3 + 2));
}
if ( !v4 )
goto LABEL_14;
dword_B68 = 0;
LABEL_6:
v5 = qword_B80;
if ( !qword_B80 )
qword_B78 = v12[0];
if ( dword_B74 )
{
v6 = slice_get();
video_decoder_slice_dump(qword_B78, v6);
dword_B74 = 0;
dword_B6C = 0;
qword_B78 = v12[0];
v5 = qword_B80;
}
qword_B80 = v5 + 1;
goto LABEL_11;
}

canonical_decompiled_code:
void __noreturn func8()
{
__int64 v0; // rax
__int64 v1; // r12
unsigned int v2; // esi
__int64 v3; // rbx
char v4; // al
__int64 v5; // rax
__int64 v6; // rax
unsigned __int16 v7; // ax
unsigned __int8 v8; // [rsp+Eh] [rbp-22h] BYREF
unsigned __int8 v9; // [rsp+Fh] [rbp-21h] BYREF
unsigned int v10; // [rsp+10h] [rbp-20h] BYREF
unsigned int v11; // [rsp+14h] [rbp-1Ch] BYREF
unsigned __int64 v12[3]; // [rsp+18h] [rbp-18h] BYREF

while ( 1 )
{
while ( 1 )
{
v0 = sx_pipe_get(1LL);
v1 = v0;
if ( v0 )
break;
usleep(0x1F40u);
}
v2 = *(_DWORD *)(v0 + 8);
v3 = *(_QWORD *)v0;
if ( v2 <= 0xC )
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_video_decoder.c", 0x1C0u, "video_decoder_thread");
v4 = func7(*(_QWORD *)v0, v2, &v10, v12);
if ( dword_B68 )
break;
if ( v4 )
goto LABEL_6;
LABEL_11:
m2ts_data_interpret_isra_0(*(_QWORD *)v1, *(_DWORD *)(v1 + 8), &v11, &v8, &v9);
dword_B6C += v11;
if ( dword_B74 && (((_BYTE)dword_B70 + 1) & 0xF) != v8 )
{
printf("(video_decoder): last rtp seq num = %d\n", (unsigned __int16)word_B88);
v7 = func5(*(_WORD *)(v3 + 2));
printf("(video_decoder): curr rtp seq num = %d\n", v7);
printf("(video_decoder): Packet loss detected! last_cc = %u, start_cc = %d\n", (unsigned int)dword_B70, v8);
func4();
dword_B68 = 1;
dword_B74 = 0;
}
else
{
dword_B70 = v9;
func2(v1);
++dword_B74;
}
LABEL_14:
word_B88 = func5(*(_WORD *)(v3 + 2));
}
if ( !v4 )
goto LABEL_14;
dword_B68 = 0;
LABEL_6:
v5 = qword_B80;
if ( !qword_B80 )
qword_B78 = v12[0];
if ( dword_B74 )
{
v6 = func0();
func3(qword_B78, v6);
dword_B74 = 0;
dword_B6C = 0;
qword_B78 = v12[0];
v5 = qword_B80;
}
qword_B80 = v5 + 1;
goto LABEL_11;
}

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


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

canonical_decompiled_code:
void func9()
{
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 func10()
{
return func6();
}

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 func11()
{
;
}

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]