decompiled_code:
__int64 __fastcall decode_tagpacket(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v3; // r12
__int64 v4; // rbp
__int64 v5; // rbx
unsigned int v6; // eax
char v7; // al
unsigned __int8 *v8; // r13
unsigned int v9; // er15
__int64 v10; // rdx
__int64 v11; // r14
const unsigned __int16 *v12; // rdx
__int64 v13; // rax
__int64 v14; // r9
unsigned int v15; // er14
__int64 v17; // [rsp-8h] [rbp-50h]
unsigned __int64 v18; // [rsp+0h] [rbp-48h]

v3 = 8LL;
v4 = a2;
v5 = 0LL;
*(_BYTE *)(a1 + 2133) = 0;
v18 = a3;
if ( a3 <= 8 )
return 1LL;
while ( 1 )
{
v8 = (unsigned __int8 *)(v4 + v5);
v9 = read_32b((unsigned int *)(v4 + v5));
v11 = (unsigned int)read_32b((unsigned int *)(v4 + v5 + 4));
if ( verbosity > 2 )
{
v12 = *_ctype_b_loc();
v13 = *(unsigned __int8 *)(v4 + v5 + 3);
v14 = v13;
if ( (v12[v13] & 0x4000) == 0 )
{
v14 = (unsigned int)(v13 + 48);
if ( (v12[v13 + 48] & 0x4000) == 0 )
v14 = 46LL;
}
a2 = v9;
msg_Log(
"EDI: tag %08x (%c%c%c%c) len:%u",
v9,
*v8,
*(unsigned __int8 *)(v4 + v5 + 1),
*(unsigned __int8 *)(v4 + v5 + 2),
v14,
v11);
v10 = v17;
}
if ( (v11 & 7) != 0 )
break;
v15 = (unsigned int)v11 >> 3;
a2 = v4 + v3;
if ( v9 == 712012914 )
{
if ( !(unsigned __int8)decode_starptr(a1, a2, v15) )
{
LABEL_17:
msg_Log(
"EDI: Error decoding TAG %c%c%c%c",
*v8,
*(unsigned __int8 *)(v4 + v5 + 1),
*(unsigned __int8 *)(v4 + v5 + 2),
*(unsigned __int8 *)(v4 + v5 + 3));
return 0LL;
}
}
else
{
if ( v9 == 1684370537 )
{
v7 = decode_deti(a1, a2, v15);
}
else
{
v6 = v9;
LOBYTE(v6) = 0;
if ( v6 == 1702065152 )
{
v7 = decode_estn(a1, (unsigned __int8 *)a2, v15, v9);
}
else
{
if ( v9 != 711224697 )
{
msg_Log(
"EDI: Unknown TAG %c%c%c%c",
*v8,
*(unsigned __int8 *)(v4 + v5 + 1),
*(unsigned __int8 *)(v4 + v5 + 2),
*(unsigned __int8 *)(v4 + v5 + 3));
return 1LL;
}
v7 = decode_stardmy_constprop_0();
}
}
if ( !v7 )
goto LABEL_17;
}
v5 += v15 + 8;
v3 = v5 + 8;
if ( v18 <= v5 + 8 )
return 1LL;
}
msg_Log("EDI: Invalid tag length!", a2, v10);
return 1LL;
}

original_code:
int decode_tagpacket(edi_handler_t *h, uint8_t *tag_pkt, uint32_t tagsize)
{
    size_t i, length = 0;
    _Bool success = 1;
    h->eti.m_fc.nst=0;
    for (i = 0; i + 8 < tagsize; i += 8 + length) {
        uint32_t tagId = read_32b(tag_pkt + i);
        uint32_t taglength = read_32b(tag_pkt + i + 4);
        if(verbosity > 2)
         msg_Log("EDI: tag %08x (%c%c%c%c) len:%u",
           tagId, tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2],
     ((*__ctype_b_loc ())[(int) ((tag_pkt[i+3]))] & (unsigned short int) _ISprint) ? tag_pkt[i+3] : (((*__ctype_b_loc ())[(int) ((tag_pkt[i+3]+0x30))] & (unsigned short int) _ISprint) ? (tag_pkt[i+3]+0x30) : '.'),
     taglength);
        if (taglength % 8 != 0) {
         msg_Log("EDI: Invalid tag length!");
            break;
        }
        taglength /= 8;
        length = taglength;
        uint8_t *tag_value = tag_pkt + i+8;
        _Bool tagsuccess = 0;
        if (tagId == 0x2a707472 ) {
            tagsuccess = decode_starptr(h, tag_value, taglength);
        }
        else if (tagId == 0x64657469 ) {
            tagsuccess = decode_deti(h, tag_value, taglength);
        }
        else if ((tagId & 0xFFFFFF00) == 0x65737400 ) {
            uint8_t n = tagId & 0x000000FF;
            tagsuccess = decode_estn(h, tag_value, taglength, n);
        }
        else if (tagId == 0x2a646d79 ) {
            tagsuccess = decode_stardmy(h, tag_value, taglength);
        }
        else {
         msg_Log("EDI: Unknown TAG %c%c%c%c", tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2], tag_pkt[i+3]);
            break;
        }
        if (!tagsuccess) {
         msg_Log("EDI: Error decoding TAG %c%c%c%c",tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2], tag_pkt[i+3]);
            success = tagsuccess;
            break;
        }
    }
    return success;
}

canonical_original_code:
int decode_tagpacket(struct edi_handler_t *h, unsigned char *tag_pkt, unsigned int tagsize)
{
    unsigned long i; unsigned long length = 0;
    _Bool success = 1;
    h->eti.m_fc.nst=0;
    for (i = 0; i + 8 < tagsize; i += 8 + length) {
        unsigned int tagId = read_32b(tag_pkt + i);
        unsigned int taglength = read_32b(tag_pkt + i + 4);
        if(verbosity > 2)
         msg_Log("EDI: tag %08x (%c%c%c%c) len:%u",
           tagId, tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2],
     ((*__ctype_b_loc ())[(int) ((tag_pkt[i+3]))] & (unsigned short) _ISprint) ? tag_pkt[i+3] : (((*__ctype_b_loc ())[(int) ((tag_pkt[i+3]+0x30))] & (unsigned short) _ISprint) ? (tag_pkt[i+3]+0x30) : '.'),
     taglength);
        if (taglength % 8 != 0) {
         msg_Log("EDI: Invalid tag length!");
            break;
        }
        taglength /= 8;
        length = taglength;
        unsigned char *tag_value = tag_pkt + i+8;
        _Bool tagsuccess = 0;
        if (tagId == 0x2a707472 ) {
            tagsuccess = decode_starptr(h, tag_value, taglength);
        }
        else if (tagId == 0x64657469 ) {
            tagsuccess = decode_deti(h, tag_value, taglength);
        }
        else if ((tagId & 0xFFFFFF00) == 0x65737400 ) {
            unsigned char n = tagId & 0x000000FF;
            tagsuccess = decode_estn(h, tag_value, taglength, n);
        }
        else if (tagId == 0x2a646d79 ) {
            tagsuccess = decode_stardmy(h, tag_value, taglength);
        }
        else {
         msg_Log("EDI: Unknown TAG %c%c%c%c", tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2], tag_pkt[i+3]);
            break;
        }
        if (!tagsuccess) {
         msg_Log("EDI: Error decoding TAG %c%c%c%c",tag_pkt[i], tag_pkt[i+1], tag_pkt[i+2], tag_pkt[i+3]);
            success = tagsuccess;
            break;
        }
    }
    return success;
}

variable_types:
tagsize
unsigned int

variable_types:
length
unsigned long

variable_types:
tagId
unsigned int


repo: piratfm/eti-tools

decompiled_code:
__int64 __fastcall ts_get_transporterror(__int64 a1)
{
return *(_BYTE *)(a1 + 1) >> 7;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(_BYTE *)(a1 + 1) >> 7;
}

original_code:
static inline _Bool ts_get_transporterror(const uint8_t *p_ts)
{
    return !!(p_ts[1] & 0x80);
}

canonical_original_code:
static inline _Bool ts_get_transporterror(unsigned char *p_ts)
{
    return !!(p_ts[1] & 0x80);
}


decompiled_code:
__int64 __fastcall ts_get_pid(__int64 a1)
{
int v1; // eax

v1 = *(unsigned __int8 *)(a1 + 1) << 8;
LOWORD(v1) = v1 & 0x1F00;
return *(unsigned __int8 *)(a1 + 2) | (unsigned int)v1;
}

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

v1 = *(unsigned __int8 *)(a1 + 1) << 8;
LOWORD(v1) = v1 & 0x1F00;
return *(unsigned __int8 *)(a1 + 2) | (unsigned int)v1;
}

original_code:
static inline uint16_t ts_get_pid(const uint8_t *p_ts)
{
    return ((p_ts[1] & 0x1f) << 8) | p_ts[2];
}

canonical_original_code:
static inline unsigned short ts_get_pid(unsigned char *p_ts)
{
    return ((p_ts[1] & 0x1f) << 8) | p_ts[2];
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall ts_get_cc(__int64 a1)
{
return *(_BYTE *)(a1 + 3) & 0xF;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return *(_BYTE *)(a1 + 3) & 0xF;
}

original_code:
static inline uint8_t ts_get_cc(const uint8_t *p_ts)
{
    return p_ts[3] & 0xf;
}

canonical_original_code:
static inline unsigned char ts_get_cc(unsigned char *p_ts)
{
    return p_ts[3] & 0xf;
}

return_type: unsigned char


decompiled_code:
_BOOL8 __fastcall ts_has_payload(__int64 a1)
{
return (*(_BYTE *)(a1 + 3) & 0x10) != 0;
}

original_code:
static inline _Bool ts_has_payload(const uint8_t *p_ts)
{
    return !!(p_ts[3] & 0x10);
}

canonical_original_code:
static inline _Bool ts_has_payload(unsigned char *p_ts)
{
    return !!(p_ts[3] & 0x10);
}


decompiled_code:
_BOOL8 __fastcall ts_has_adaptation(__int64 a1)
{
return (*(_BYTE *)(a1 + 3) & 0x20) != 0;
}

original_code:
static inline _Bool ts_has_adaptation(const uint8_t *p_ts)
{
    return !!(p_ts[3] & 0x20);
}

canonical_original_code:
static inline _Bool ts_has_adaptation(unsigned char *p_ts)
{
    return !!(p_ts[3] & 0x20);
}


decompiled_code:
__int64 __fastcall ts_get_adaptation(__int64 a1)
{
return *(unsigned __int8 *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return *(unsigned __int8 *)(a1 + 4);
}

original_code:
static inline uint8_t ts_get_adaptation(const uint8_t *p_ts)
{
    return p_ts[4];
}

canonical_original_code:
static inline unsigned char ts_get_adaptation(unsigned char *p_ts)
{
    return p_ts[4];
}


decompiled_code:
__int64 __fastcall ts_payload(__int64 a1)
{
if ( !ts_has_payload(a1) )
return a1 + 188;
if ( ts_has_adaptation(a1) )
return a1 + (unsigned __int8)ts_get_adaptation(a1) + 5;
return a1 + 4;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
if ( !func10(a1) )
return a1 + 188;
if ( func11(a1) )
return a1 + (unsigned __int8)func12(a1) + 5;
return a1 + 4;
}

original_code:
static inline uint8_t *ts_payload(uint8_t *p_ts)
{
    if (!ts_has_payload(p_ts))
        return p_ts + 188;
    if (!ts_has_adaptation(p_ts))
        return p_ts + 4;
    return p_ts + 4 + 1 + ts_get_adaptation(p_ts);
}

canonical_original_code:
static inline unsigned char *ts_payload(unsigned char *p_ts)
{
    if (!ts_has_payload(p_ts))
        return p_ts + 188;
    if (!ts_has_adaptation(p_ts))
        return p_ts + 4;
    return p_ts + 4 + 1 + ts_get_adaptation(p_ts);
}

return_type: unsigned char *


decompiled_code:
__int64 __fastcall fig_0(int a1, _BYTE *a2)
{
((void (__fastcall *)(_QWORD, bool, bool, _QWORD, _BYTE *))fig_0_jtab[*a2 & 0x1F])(
(unsigned int)(a1 - 1),
(*a2 & 0x20) != 0,
(*a2 & 0x40) != 0,
*a2 >> 7,
a2 + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, _BYTE *a2)
{
((void (__fastcall *)(_QWORD, bool, bool, _QWORD, _BYTE *))fig_0_jtab[*a2 & 0x1F])(
(unsigned int)(a1 - 1),
(*a2 & 0x20) != 0,
(*a2 & 0x40) != 0,
*a2 >> 7,
a2 + 1);
return 0LL;
}

original_code:
int fig_0(int figlen, unsigned char *fig)
{
 struct fig_0 *f;
 f = (struct fig_0*)fig;
 (*fig_0_jtab[f->extn])(figlen-1, f->pd, f->oe, f->cn, fig + 1);
 return 0;
}

canonical_original_code:
int fig_0(int figlen, unsigned char *fig)
{
 struct fig_0 *f;
 f = (struct fig_0 *)fig;
 (*fig_0_jtab[f->extn])(figlen-1, f->pd, f->oe, f->cn, fig + 1);
 return 0;
}

variable_types: figlen int

variable_types:
fig
unsigned char *

variable_types: struct fig_0 { unsigned int extn; unsigned int pd; unsigned int oe; unsigned int cn; } *

return_type: int


decompiled_code:
__int64 __fastcall fig_1(int a1, _BYTE *a2)
{
((void (__fastcall *)(_QWORD, bool, _QWORD, _BYTE *, _QWORD))fig_1_jtab[*a2 & 7])(
(unsigned int)(a1 - 1),
(*a2 & 8) != 0,
*a2 >> 4,
a2 + 1,
*a2 & 7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, _BYTE *a2)
{
((void (__fastcall *)(_QWORD, bool, _QWORD, _BYTE *, _QWORD))fig_1_jtab[*a2 & 7])(
(unsigned int)(a1 - 1),
(*a2 & 8) != 0,
*a2 >> 4,
a2 + 1,
*a2 & 7);
return 0LL;
}

original_code:
int fig_1(int figlen, unsigned char *fig)
{
 struct fig_1 *f;
 f = (struct fig_1*)fig;
 (*fig_1_jtab[f->extn])(figlen-1, f->oe, f->charset, fig + 1);
 return 0;
}

canonical_original_code:
int fig_1(int figlen, unsigned char *fig)
{
 struct fig_1 *f;
 f = (struct fig_1 *)fig;
 (*fig_1_jtab[f->extn])(figlen-1, f->oe, f->charset, fig + 1);
 return 0;
}

variable_types: struct fig_1 { unsigned int extn; unsigned int oe; unsigned int charset; } *


decompiled_code:
__int64 fig_5()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 0LL;
}

original_code:
int fig_5(int figlen, unsigned char *fig)
{
 return 0;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall fig_ign(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
return fig_5(a1, a2, a3, a4, a5);
}

original_code:
int fig_ign(int figlen, unsigned char *fig)
{
 return 0;
}


canonical_decompiled_code:
__int64 func4()
{
return 0LL;
}

original_code:
int fig_0_10(int figlen, int pd, int oe, int cn, unsigned char* fig)
{
 return 0;
}

canonical_original_code:
int fig_0_10(int figlen, int pd, int oe, int cn, unsigned char *fig)
{
 return 0;
}

variable_types: pd int

variable_types: oe int

variable_types: cn int


original_code:
int fig_1_5(int figlen, int oe, int charset, unsigned char* fig)
{
 struct dsl *f;
 f = (struct dsl*)fig;
 f->label[16] = '\0';
 return 0;
}

canonical_original_code:
int fig_1_5(int figlen, int oe, int charset, unsigned char *fig)
{
 struct dsl *f;
 f = (struct dsl *)fig;
 f->label[16] = '\0';
 return 0;
}

variable_types: charset int

variable_types: struct dsl { int SId; char label[17]; short CharFlgFld; } *


original_code:
int fig_0_0(int figlen, int pd, int oe, int cn, unsigned char* fig)
{
 short f;
 struct ensinf ei;
 unsigned short EId;
 EId = spack(fig);
 f = spack(fig + 2);
 memcpy(&ei, &f, sizeof(struct ensinf));
 if (ei.ChgFlg > 0) {
 }
 einf.eid = EId;
 return 0;
}

canonical_original_code:
int fig_0_0(int figlen, int pd, int oe, int cn, unsigned char *fig)
{
 short f;
 struct ensinf ei;
 unsigned short EId;
 EId = spack(fig);
 f = spack(fig + 2);
 memcpy(&ei, &f, sizeof(struct ensinf));
 if (ei.ChgFlg > 0) {
 }
 einf.eid = EId;
 return 0;
}

variable_types: f short

variable_types: struct ensinf { unsigned int CIFCntL; unsigned int CIFCntH; unsigned int AlrmFlg; unsigned int ChgFlg; }

variable_types:
EId
unsigned short


canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
ipack(a5);
ipack(a5 + 3);
return 0LL;
}

original_code:
int fig_0_3(int figlen, int pd, int oe, int cn, unsigned char* fig)
{
 struct servcomp1 sc1;
 struct servcomp2 sc2;
 int f;
 f = ipack(fig);
 memcpy(&sc2, &f, sizeof(struct servcomp2));
 fig += 3;
 f = ipack(fig);
 memcpy(&sc1, &f, sizeof(struct servcomp1));
 return 0;
}

canonical_original_code:
int fig_0_3(int figlen, int pd, int oe, int cn, unsigned char *fig)
{
 struct servcomp1 sc1;
 struct servcomp2 sc2;
 int f;
 f = ipack(fig);
 memcpy(&sc2, &f, sizeof(struct servcomp2));
 fig += 3;
 f = ipack(fig);
 memcpy(&sc1, &f, sizeof(struct servcomp1));
 return 0;
}

variable_types: struct servcomp1 { unsigned int SCCA; unsigned int PktAddr; unsigned int SubChId; }

variable_types: struct servcomp2 { unsigned int; unsigned int DSCTy; unsigned int Rfu; unsigned int DGFlag; unsigned int SCCAFlg; unsigned int Rfa; unsigned int SCId; }

variable_types: f int


original_code:
int fig_1_0(int figlen, int oe, int charset, unsigned char* fig)
{
 struct el *f;
 f = (struct el*)fig;
 memcpy(einf.label,f->label, 16 * sizeof(char));
 einf.label[16] = '\0';
 einf.eid = f->eid;
 sswab((short*)&(einf.eid));
 return 0;
}

canonical_original_code:
int fig_1_0(int figlen, int oe, int charset, unsigned char *fig)
{
 struct el *f;
 f = (struct el *)fig;
 memcpy(einf.label,f->label, 16 * sizeof(char));
 einf.label[16] = '\0';
 einf.eid = f->eid;
 sswab((short *)&(einf.eid));
 return 0;
}

variable_types: struct el { short eid; char label[16]; unsigned short chflag; } *


decompiled_code:
__int64 __fastcall fig_1_1(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int16 v5; // ax
char *v6; // rax
unsigned __int16 v8; // [rsp+0h] [rbp-28h] BYREF
char dest[18]; // [rsp+2h] [rbp-26h] BYREF
__int16 v10[10]; // [rsp+14h] [rbp-14h] BYREF

v8 = *(_WORD *)a4;
sswab(&v8);
strncpy(dest, (const char *)(a4 + 2), 0x10uLL);
v5 = *(_WORD *)(a4 + 18);
dest[16] = 0;
v10[0] = v5;
sswab(v10);
v6 = (char *)find_service(einf, v8);
if ( v6 )
strncpy(v6, dest, 0x11uLL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int16 v5; // ax
char *v6; // rax
unsigned __int16 v8; // [rsp+0h] [rbp-28h] BYREF
char dest[18]; // [rsp+2h] [rbp-26h] BYREF
__int16 v10[10]; // [rsp+14h] [rbp-14h] BYREF

v8 = *(_WORD *)a4;
sswab(&v8);
strncpy(dest, (const char *)(a4 + 2), 0x10uLL);
v5 = *(_WORD *)(a4 + 18);
dest[16] = 0;
v10[0] = v5;
sswab(v10);
v6 = (char *)find_service(einf, v8);
if ( v6 )
strncpy(v6, dest, 0x11uLL);
return 0LL;
}

original_code:
int fig_1_1(int figlen, int oe, int charset, unsigned char* fig)
{
 struct {
  short SId;
  char label[17];
  short CharFlgFld;
 } psl;
 struct service *s;
 psl.SId = *(unsigned short*)fig;
 sswab(&psl.SId);
 fig += 2;
 strncpy(psl.label, (char*)fig, 16);
 psl.label[16] = '\0';
 fig += 16;
 psl.CharFlgFld = *(short*)fig;
 sswab(&(psl.CharFlgFld));
 if ((s = find_service(&einf, (unsigned short)psl.SId)) != ((void *)0))
  strncpy(s->label, psl.label, 17);
 return 0;
}

canonical_original_code:
int fig_1_1(int figlen, int oe, int charset, unsigned char *fig)
{
 struct { short SId; char label[17]; short CharFlgFld; } psl;
 struct service *s;
 psl.SId = *(unsigned short *)fig;
 sswab(&psl.SId);
 fig += 2;
 strncpy(psl.label, (char *)fig, 16);
 psl.label[16] = '\0';
 fig += 16;
 psl.CharFlgFld = *(short *)fig;
 sswab(&(psl.CharFlgFld));
 if ((s = find_service(&einf, (unsigned short)psl.SId)) != ((void *)0))
  strncpy(s->label, psl.label, 17);
 return 0;
}

variable_types: struct service { char label[17]; int sid; struct subch { int subchid; int startaddr; int subchsz; int bitrate; int eepprot; int protlvl; int uep_indx; int dabplus; } *pa; struct subch *sa; struct service *next; struct service *prev; } *

user_defined_types: struct service { char label[17]; int sid; struct subch *pa; struct subch *sa; struct service *next; struct service *prev; }


original_code:
int fig_1_4(int figlen, int oe, int charset, unsigned char* fig)
{
 struct scl s;
 struct sclf *p;
 p = (struct sclf*)fig;
 s.SCIds = p->SCIds;
 s.Rfa = p->Rfa;
 s.PD = p->PD;
 fig++;
 if (p->PD) {
  s.SId = *(int*)fig;
  fig += 4;
 } else {
  s.SId = *(short*)fig;
  fig += 2;
 }
 iswab(&(s.SId));
 strncpy(s.label, (char*)fig, 16);
 s.label[16] = '\0';
 fig += 16;
 s.CharFlgFld = *(short*)fig;
 sswab(&(s.CharFlgFld));
 return 0;
}

canonical_original_code:
int fig_1_4(int figlen, int oe, int charset, unsigned char *fig)
{
 struct scl s;
 struct sclf *p;
 p = (struct sclf *)fig;
 s.SCIds = p->SCIds;
 s.Rfa = p->Rfa;
 s.PD = p->PD;
 fig++;
 if (p->PD) {
  s.SId = *(int *)fig;
  fig += 4;
 } else {
  s.SId = *(short *)fig;
  fig += 2;
 }
 iswab(&(s.SId));
 strncpy(s.label, (char *)fig, 16);
 s.label[16] = '\0';
 fig += 16;
 s.CharFlgFld = *(short *)fig;
 sswab(&(s.CharFlgFld));
 return 0;
}

variable_types: figlen int

variable_types: struct scl { short CharFlgFld; char label[17]; int SId; int SCIds; int Rfa; int PD; }

variable_types: struct sclf { unsigned int SCIds; unsigned int Rfa; unsigned int PD; } *


decompiled_code:
// attributes: thunk
__int64 fig_0_21()
{
return fig_0_10_constprop_0();
}

canonical_decompiled_code:
__int64 func14()
{
return func13();
}

original_code:
int fig_0_21(int figlen, int pd, int oe, int cn, unsigned char* fig)
{
 return 0;
}

canonical_original_code:
int fig_0_21(int figlen, int pd, int oe, int cn, unsigned char *fig)
{
 return 0;
}

variable_types:
fig
unsigned char *


decompiled_code:
// attributes: thunk
__int64 fig_0_20()
{
return fig_0_10_constprop_0();
}

canonical_decompiled_code:
__int64 func15()
{
return func13();
}

original_code:
int fig_0_20(int figlen, int pd, int oe, int cn, unsigned char* fig)
{
 return 0;
}

canonical_original_code:
int fig_0_20(int figlen, int pd, int oe, int cn, unsigned char *fig)
{
 return 0;
}

variable_types: pd int


unpickfig

decompiled_code:
__int64 __fastcall unpickfig(_BYTE *a1, int a2)
{
unsigned int v2; // ebp
__int64 v3; // rbx

v2 = *a1 & 0x1F;
v3 = *a1 >> 5;
if ( v2 != a2 )
{
fwrite("Bitfield extracted values and masked/shifted values differ - fix\n", 1uLL, 0x41uLL, stderr);
fprintf(
stderr,
"Bitfield: type = %d length = %d Mask: type = %d length = %d\n",
*a1 >> 5,
*a1 & 0x1F,
(unsigned int)v3,
v2);
v3 = *a1 >> 5;
}
((void (__fastcall *)(int, _BYTE *))*(&fig_jtab + v3))(a2, a1 + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(_BYTE *a1, int a2)
{
unsigned int v2; // ebp
__int64 v3; // rbx

v2 = *a1 & 0x1F;
v3 = *a1 >> 5;
if ( v2 != a2 )
{
fwrite("Bitfield extracted values and masked/shifted values differ - fix\n", 1uLL, 0x41uLL, stderr);
fprintf(
stderr,
"Bitfield: type = %d length = %d Mask: type = %d length = %d\n",
*a1 >> 5,
*a1 & 0x1F,
(unsigned int)v3,
v2);
v3 = *a1 >> 5;
}
((void (__fastcall *)(int, _BYTE *))*(&fig_jtab + v3))(a2, a1 + 1);
return 0LL;
}

original_code:
int unpickfig(unsigned char* fig, int figlen)
{
 int len, typ;
 struct fig_hdr *f;
 typ = (*fig & 0xe0) >> 5;
 len = *fig & 0x1f;
 f = (struct fig_hdr*)fig;
 if ((len != figlen) || (typ != ((struct fig_hdr*)fig)->ftype)) {
  fprintf(stderr, "Bitfield extracted values and masked/shifted values differ - fix\n");
  fprintf(stderr, "Bitfield: type = %d length = %d Mask: type = %d length = %d\n",
   f->ftype,f->flen,typ,len);
 }
 (*fig_jtab[f->ftype])(figlen, fig + 1);
 return 0;
}

canonical_original_code:
int unpickfig(unsigned char *fig, int figlen)
{
 int len; int typ;
 struct fig_hdr *f;
 typ = (*fig & 0xe0) >> 5;
 len = *fig & 0x1f;
 f = (struct fig_hdr *)fig;
 if ((len != figlen) || (typ != ((struct fig_hdr *)fig)->ftype)) {
  fprintf(stderr, "Bitfield extracted values and masked/shifted values differ - fix\n");
  fprintf(stderr, "Bitfield: type = %d length = %d Mask: type = %d length = %d\n",
   f->ftype,f->flen,typ,len);
 }
 (*fig_jtab[f->ftype])(figlen, fig + 1);
 return 0;
}

variable_types: len int

variable_types: typ int

variable_types: struct fig_hdr { unsigned int flen; unsigned int ftype; } *


decompiled_code:
__int16 __fastcall read_16b(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int16 __fastcall func0(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

original_code:
static inline uint16_t read_16b(const uint8_t *buf){
    uint16_t value = ((uint16_t)buf[0]) << 8 | buf[1];
    return value;
}

canonical_original_code:
static inline unsigned short read_16b(unsigned char *buf){
    unsigned short value = ((uint16_t)buf[0]) << 8 | buf[1];
    return value;
}

variable_types:
buf
unsigned char *

variable_types:
value
unsigned short


decompiled_code:
__int64 __fastcall read_32b(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

original_code:
static inline uint32_t read_32b(const uint8_t *buf){
    uint32_t value = ((uint32_t)buf[0]) << 24 | ((uint32_t)buf[1]) << 16 | ((uint16_t)buf[2]) << 8 | buf[3];
    return value;
}

canonical_original_code:
static inline unsigned int read_32b(unsigned char *buf){
    unsigned int value = ((uint32_t)buf[0]) << 24 | ((uint32_t)buf[1]) << 16 | ((uint16_t)buf[2]) << 8 | buf[3];
    return value;
}

variable_types:
value
unsigned int

return_type: unsigned int


checkCRC

decompiled_code:
bool __fastcall checkCRC(__int64 a1, __int64 a2)
{
_WORD *v3; // rdi
__int64 v4; // rsi
__int16 v5; // ax

v3 = (_WORD *)(a1 + a2 - 2);
v4 = a1;
v5 = read_16b(v3);
return ~(unsigned __int16)crc16(0xFFFFLL, v4) == v5;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1, __int64 a2)
{
_WORD *v3; // rdi
__int64 v4; // rsi
__int16 v5; // ax

v3 = (_WORD *)(a1 + a2 - 2);
v4 = a1;
v5 = func0(v3);
return ~(unsigned __int16)crc16(0xFFFFLL, v4) == v5;
}

original_code:
static inline _Bool checkCRC(const uint8_t *buf, size_t size)
{
    const uint16_t crc_from_packet = read_16b(buf + size - 2);
    uint16_t crc_calc = 0xffff;
    crc_calc = crc16(crc_calc, buf, size - 2);
    crc_calc ^= 0xffff;
    return crc_from_packet == crc_calc;
}

canonical_original_code:
static inline _Bool checkCRC(unsigned char *buf, unsigned long size)
{
    unsigned short crc_from_packet = read_16b(buf + size - 2);
    unsigned short crc_calc = 0xffff;
    crc_calc = crc16(crc_calc, buf, size - 2);
    crc_calc ^= 0xffff;
    return crc_from_packet == crc_calc;
}

variable_types:
size
unsigned long


HandleAFPacket

decompiled_code:
__int64 __fastcall HandleAFPacket(__int64 a1, __int64 a2, unsigned int a3)
{
unsigned int v4; // er13
int v5; // er12
unsigned __int8 v6; // cl
__int64 v7; // r12
__int64 result; // rax

if ( a3 > 9 && *(_BYTE *)a2 == 65 && *(_BYTE *)(a2 + 1) == 70 )
{
v4 = read_32b((unsigned int *)(a2 + 2));
v5 = (unsigned __int16)read_16b((_WORD *)(a2 + 6));
if ( *(unsigned __int16 *)(a1 + 36) + 1 != v5 )
msg_Log("EDI-AF: Packet sequence error");
*(_WORD *)(a1 + 36) = v5;
v6 = *(_BYTE *)(a2 + 8) & 0xF;
if ( ((*(_BYTE *)(a2 + 8) >> 4) & 7) != 1 || v6 )
{
msg_Log("EDI-AF: Packet has wrong revision %u.%u", (*(_BYTE *)(a2 + 8) >> 4) & 7, v6);
result = 0xFFFFFFFFLL;
}
else if ( *(_BYTE *)(a2 + 9) == 84 )
{
v7 = v4 + 10;
if ( a3 < (unsigned __int64)(v7 + 2) )
{
msg_Log("EDI-AF: packet too small: %u < %u", a3, v7 + 2);
result = 0LL;
}
else if ( *(char *)(a2 + 8) >= 0 )
{
msg_Log("EDI-AF: packet not supported, has no CRC");
result = 0xFFFFFFFFLL;
}
else if ( checkCRC(a2, v7 + 2) )
{
result = decode_tagpacket(a1, a2 + 10, v4);
}
else
{
msg_Log("EDI-AF: Packet crc wrong pos:%u max:%u", (unsigned int)v7, a3);
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0LL;
}
}
else
{
msg_Log("EDI-AF: bad tag!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3)
{
unsigned int v4; // er13
int v5; // er12
unsigned __int8 v6; // cl
__int64 v7; // r12
__int64 result; // rax

if ( a3 > 9 && *(_BYTE *)a2 == 65 && *(_BYTE *)(a2 + 1) == 70 )
{
v4 = func1((unsigned int *)(a2 + 2));
v5 = (unsigned __int16)func0((_WORD *)(a2 + 6));
if ( *(unsigned __int16 *)(a1 + 36) + 1 != v5 )
msg_Log("EDI-AF: Packet sequence error");
*(_WORD *)(a1 + 36) = v5;
v6 = *(_BYTE *)(a2 + 8) & 0xF;
if ( ((*(_BYTE *)(a2 + 8) >> 4) & 7) != 1 || v6 )
{
msg_Log("EDI-AF: Packet has wrong revision %u.%u", (*(_BYTE *)(a2 + 8) >> 4) & 7, v6);
result = 0xFFFFFFFFLL;
}
else if ( *(_BYTE *)(a2 + 9) == 84 )
{
v7 = v4 + 10;
if ( a3 < (unsigned __int64)(v7 + 2) )
{
msg_Log("EDI-AF: packet too small: %u < %u", a3, v7 + 2);
result = 0LL;
}
else if ( *(char *)(a2 + 8) >= 0 )
{
msg_Log("EDI-AF: packet not supported, has no CRC");
result = 0xFFFFFFFFLL;
}
else if ( func2(a2, v7 + 2) )
{
result = decode_tagpacket(a1, a2 + 10, v4);
}
else
{
msg_Log("EDI-AF: Packet crc wrong pos:%u max:%u", (unsigned int)v7, a3);
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0LL;
}
}
else
{
msg_Log("EDI-AF: bad tag!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int HandleAFPacket(edi_handler_t *h, uint8_t *edi_pkt, uint32_t pktsize)
{
 if(pktsize < 10 || edi_pkt[0] != 'A' || edi_pkt[1] != 'F') {
  msg_Log("EDI-AF: bad tag!");
  return -1;
 }
    uint32_t taglength = read_32b(edi_pkt + 2);
    uint16_t seq = read_16b(edi_pkt + 6);
    if (h->af.m_last_seq + 1 != seq) {
     msg_Log("EDI-AF: Packet sequence error");
    }
    h->af.m_last_seq = seq;
    _Bool has_crc = (edi_pkt[8] & 0x80) ? 1 : 0;
    uint8_t major_revision = (edi_pkt[8] & 0x70) >> 4;
    uint8_t minor_revision = edi_pkt[8] & 0x0F;
    if (major_revision != 1 || minor_revision != 0) {
     msg_Log("EDI-AF: Packet has wrong revision %u.%u", major_revision, minor_revision);
     return -1;
    }
    uint8_t pt = edi_pkt[9];
    if (pt != 'T') {
     return 0;
    }
    const size_t crclength = 2;
    if (pktsize < 10 + taglength + crclength) {
     msg_Log("EDI-AF: packet too small: %u < %u", pktsize, 10 + taglength + crclength);
     return 0;
    }
    if (!has_crc) {
     msg_Log("EDI-AF: packet not supported, has no CRC");
        return -1;
    }
    _Bool ok = checkCRC(edi_pkt, 10 + taglength + crclength);
    if (!ok) {
     msg_Log("EDI-AF: Packet crc wrong pos:%u max:%u", 10 + taglength, pktsize);
     return -1;
    }
    return decode_tagpacket(h, edi_pkt + 10, taglength);
}

canonical_original_code:
static int HandleAFPacket(struct edi_handler_t *h, unsigned char *edi_pkt, unsigned int pktsize)
{
 if(pktsize < 10 || edi_pkt[0] != 'A' || edi_pkt[1] != 'F') {
  msg_Log("EDI-AF: bad tag!");
  return -1;
 }
    unsigned int taglength = read_32b(edi_pkt + 2);
    unsigned short seq = read_16b(edi_pkt + 6);
    if (h->af.m_last_seq + 1 != seq) {
     msg_Log("EDI-AF: Packet sequence error");
    }
    h->af.m_last_seq = seq;
    _Bool has_crc = (edi_pkt[8] & 0x80) ? 1 : 0;
    unsigned char major_revision = (edi_pkt[8] & 0x70) >> 4;
    unsigned char minor_revision = edi_pkt[8] & 0x0F;
    if (major_revision != 1 || minor_revision != 0) {
     msg_Log("EDI-AF: Packet has wrong revision %u.%u", major_revision, minor_revision);
     return -1;
    }
    unsigned char pt = edi_pkt[9];
    if (pt != 'T') {
     return 0;
    }
    unsigned long crclength = 2;
    if (pktsize < 10 + taglength + crclength) {
     msg_Log("EDI-AF: packet too small: %u < %u", pktsize, 10 + taglength + crclength);
     return 0;
    }
    if (!has_crc) {
     msg_Log("EDI-AF: packet not supported, has no CRC");
        return -1;
    }
    _Bool ok = checkCRC(edi_pkt, 10 + taglength + crclength);
    if (!ok) {
     msg_Log("EDI-AF: Packet crc wrong pos:%u max:%u", 10 + taglength, pktsize);
     return -1;
    }
    return decode_tagpacket(h, edi_pkt + 10, taglength);
}

variable_types: struct edi_handler_t { int eti_format; unsigned short m_last_seq; struct pfPkt { unsigned short sync; unsigned short _Pseq; unsigned int _Findex; unsigned int _Fcount; unsigned short _FEC; unsigned short _Addr; unsigned short _Plen; unsigned char _RSk; unsigned char _RSz; unsigned short _Source; unsigned short _Dest; _Bool _valid; unsigned char *_payload; unsigned short _MaxLen; } pf; struct afPkt { unsigned short m_last_seq; } af; struct afBuilders { unsigned short Pseq_newest; unsigned short Pseq_oldest; _Bool isInitial; unsigned int PktLost; unsigned int errorsCorrected; unsigned int PktCount; unsigned int Fcount; unsigned short Plen; _Bool FEC; _Bool Addr; unsigned char RSk; unsigned char RSz; unsigned short Source; unsigned short Dest; unsigned int _rxmin; unsigned int _cmax; struct afSingleCollector { unsigned short Pseq_this; unsigned int bytesCollected; unsigned int fragmentsCollected; unsigned char *packetReceived; unsigned char *pfPackets; _Bool packetsIsProcessed; } pfCollectors[8]; unsigned char *rs_block; unsigned char *afPacket; void *m_rs_handler; } afb; struct etiBuilder { _Bool is_eti; unsigned char m_err; unsigned short m_mnsc; _Bool m_time_valid; unsigned int m_utco; unsigned int m_seconds; unsigned char fic_length; unsigned char fic[128]; unsigned short m_rfu; struct eti_stc_data { unsigned char stream_index; unsigned char scid; unsigned short sad; unsigned char tpl; unsigned char *mst; unsigned short mst_size; } m_stc[64]; unsigned char NST; _Bool m_fc_valid; struct eti_fc_data { _Bool atstf; unsigned int tsta; _Bool ficf; unsigned char nst; unsigned short dflc; unsigned char mid; unsigned char fp; } m_fc; } eti; void (*write_cb)(void *privData, void *etiData, int etiLen); void *write_cb_priv; } *

variable_types:
pktsize
unsigned int

variable_types:
taglength
unsigned int

variable_types:
seq
unsigned short

variable_types:
pt
unsigned char

variable_types:
crclength
unsigned long

user_defined_types: struct edi_handler_t { int eti_format; unsigned short m_last_seq; struct pfPkt pf; struct afPkt af; struct afBuilders afb; struct etiBuilder eti; void (*write_cb)(void *privData, void *etiData, int etiLen); void *write_cb_priv; }

user_defined_types: struct afBuilders { unsigned short Pseq_newest; unsigned short Pseq_oldest; _Bool isInitial; unsigned int PktLost; unsigned int errorsCorrected; unsigned int PktCount; unsigned int Fcount; unsigned short Plen; _Bool FEC; _Bool Addr; unsigned char RSk; unsigned char RSz; unsigned short Source; unsigned short Dest; unsigned int _rxmin; unsigned int _cmax; struct afSingleCollector pfCollectors[8]; unsigned char *rs_block; unsigned char *afPacket; void *m_rs_handler; }

user_defined_types: struct etiBuilder { _Bool is_eti; unsigned char m_err; unsigned short m_mnsc; _Bool m_time_valid; unsigned int m_utco; unsigned int m_seconds; unsigned char fic_length; unsigned char fic[128]; unsigned short m_rfu; struct eti_stc_data m_stc[64]; unsigned char NST; _Bool m_fc_valid; struct eti_fc_data m_fc; }


closeEDIHandle

decompiled_code:
void __fastcall closeEDIHandle(void *ptr)
{
char *v2; // rbx
void *v3; // rdi

v2 = (char *)ptr + 592;
msg_Log(
"EDI: stats: packets lost: %u/%u errors:%u/%u",
*((unsigned int *)ptr + 12),
*((unsigned int *)ptr + 14),
*((unsigned int *)ptr + 13),
*((_DWORD *)ptr + 14) * (unsigned int)*((unsigned __int16 *)ptr + 32));
do
{
if ( *(_QWORD *)v2 )
{
free(*(void **)v2);
*(_QWORD *)v2 = 0LL;
*((_WORD *)v2 + 4) = 0;
}
v2 += 24;
}
while ( v2 != (char *)ptr + 2128 );
v3 = *(void **)((char *)ptr + 25);
if ( v3 )
free(v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
char *v2; // rbx
void *v3; // rdi

v2 = (char *)ptr + 592;
msg_Log(
"EDI: stats: packets lost: %u/%u errors:%u/%u",
*((unsigned int *)ptr + 12),
*((unsigned int *)ptr + 14),
*((unsigned int *)ptr + 13),
*((_DWORD *)ptr + 14) * (unsigned int)*((unsigned __int16 *)ptr + 32));
do
{
if ( *(_QWORD *)v2 )
{
free(*(void **)v2);
*(_QWORD *)v2 = 0LL;
*((_WORD *)v2 + 4) = 0;
}
v2 += 24;
}
while ( v2 != (char *)ptr + 2128 );
v3 = *(void **)((char *)ptr + 25);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void closeEDIHandle(edi_handler_t *h)
{
 int i;
 msg_Log("EDI: stats: packets lost: %u/%u errors:%u/%u",
   h->afb.PktLost, h->afb.PktCount, h->afb.errorsCorrected, h->afb.PktCount*h->afb.Plen);
 if(h) {
  for(i=0;i<64;i++) {
   if(h->eti.m_stc[i].mst) {
    free(h->eti.m_stc[i].mst);
    h->eti.m_stc[i].mst = ((void *)0);
    h->eti.m_stc[i].mst_size = 0;
   }
  }
  if(h->pf._payload)
   free(h->pf._payload);
  free(h);
 }
}

canonical_original_code:
void closeEDIHandle(struct edi_handler_t *h)
{
 int i;
 msg_Log("EDI: stats: packets lost: %u/%u errors:%u/%u",
   h->afb.PktLost, h->afb.PktCount, h->afb.errorsCorrected, h->afb.PktCount*h->afb.Plen);
 if(h) {
  for(i=0;i<64;i++) {
   if(h->eti.m_stc[i].mst) {
    free(h->eti.m_stc[i].mst);
    h->eti.m_stc[i].mst = ((void *)0);
    h->eti.m_stc[i].mst_size = 0;
   }
  }
  if(h->pf._payload)
   free(h->pf._payload);
  free(h);
 }
}

variable_types: i int

return_type: void

user_defined_types: struct pfPkt { unsigned short sync; unsigned short _Pseq; unsigned int _Findex; unsigned int _Fcount; unsigned short _FEC; unsigned short _Addr; unsigned short _Plen; unsigned char _RSk; unsigned char _RSz; unsigned short _Source; unsigned short _Dest; _Bool _valid; unsigned char *_payload; unsigned short _MaxLen; }

user_defined_types: struct afPkt { unsigned short m_last_seq; }

user_defined_types: struct eti_fc_data { _Bool atstf; unsigned int tsta; _Bool ficf; unsigned char nst; unsigned short dflc; unsigned char mid; unsigned char fp; }

user_defined_types: struct afSingleCollector { unsigned short Pseq_this; unsigned int bytesCollected; unsigned int fragmentsCollected; unsigned char *packetReceived; unsigned char *pfPackets; _Bool packetsIsProcessed; }

user_defined_types: struct eti_stc_data { unsigned char stream_index; unsigned char scid; unsigned short sad; unsigned char tpl; unsigned char *mst; unsigned short mst_size; }


HandleEDIPacket

decompiled_code:
__int64 __fastcall HandleEDIPacket(unsigned int *a1, unsigned __int8 *a2, unsigned __int64 a3)
{
unsigned __int8 v3; // bl
unsigned __int8 v4; // bp
const unsigned __int16 **v7; // rax
__int64 v9; // rsi
int v10; // ebx
signed int v11; // edx

if ( a3 <= 1 )
{
msg_Log("EDI: packet too small!");
return 0xFFFFFFFFLL;
}
v3 = *a2;
v4 = a2[1];
if ( *a2 == 80 )
{
if ( v4 == 70 )
{
v9 = 0LL;
do
{
v10 = HandlePFPacket(a1, &a2[v9], a3 - v9);
if ( v10 <= 0 )
break;
if ( *((_BYTE *)a1 + 24) )
{
v11 = pushPFTFrag((char *)a1 + 6, a1 + 10);
if ( v11 > 0 && (int)HandleAFPacket((__int64)a1, *((_QWORD *)a1 + 52), v11) > 0 )
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
}
v9 = v10;
}
while ( v10 < a3 );
return 0LL;
}
goto LABEL_4;
}
if ( v3 != 65 || v4 != 70 )
{
LABEL_4:
v7 = _ctype_b_loc();
if ( ((*v7)[v4] & 0x4000) == 0 )
v4 = 46;
if ( ((*v7)[v3] & 0x4000) == 0 )
v3 = 46;
msg_Log("EDI: packet is unknown: %c%c", v3, v4);
return 0LL;
}
if ( (int)HandleAFPacket((__int64)a1, (__int64)a2, a3) <= 0 )
return 0LL;
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, unsigned __int8 *a2, unsigned __int64 a3)
{
unsigned __int8 v3; // bl
unsigned __int8 v4; // bp
const unsigned __int16 **v7; // rax
__int64 v9; // rsi
int v10; // ebx
signed int v11; // edx

if ( a3 <= 1 )
{
msg_Log("EDI: packet too small!");
return 0xFFFFFFFFLL;
}
v3 = *a2;
v4 = a2[1];
if ( *a2 == 80 )
{
if ( v4 == 70 )
{
v9 = 0LL;
do
{
v10 = HandlePFPacket(a1, &a2[v9], a3 - v9);
if ( v10 <= 0 )
break;
if ( *((_BYTE *)a1 + 24) )
{
v11 = pushPFTFrag((char *)a1 + 6, a1 + 10);
if ( v11 > 0 && (int)func3((__int64)a1, *((_QWORD *)a1 + 52), v11) > 0 )
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
}
v9 = v10;
}
while ( v10 < a3 );
return 0LL;
}
goto LABEL_4;
}
if ( v3 != 65 || v4 != 70 )
{
LABEL_4:
v7 = _ctype_b_loc();
if ( ((*v7)[v4] & 0x4000) == 0 )
v4 = 46;
if ( ((*v7)[v3] & 0x4000) == 0 )
v3 = 46;
msg_Log("EDI: packet is unknown: %c%c", v3, v4);
return 0LL;
}
if ( (int)func3((__int64)a1, (__int64)a2, a3) <= 0 )
return 0LL;
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
return 0LL;
}

original_code:
int HandleEDIPacket(edi_handler_t *h, uint8_t *edi_pkt, size_t pktsize)
{
 int ret = 0;
 if(pktsize < 2) {
  msg_Log("EDI: packet too small!");
  goto edi_error;
 }
 if(edi_pkt[0]=='P' && edi_pkt[1]=='F') {
  int used_bytes = 0;
  do {
   used_bytes = HandlePFPacket(h, edi_pkt+used_bytes, pktsize - used_bytes);
   if(used_bytes > 0 && h->pf._valid) {
    int afBytes = pushPFTFrag(&h->pf, &h->afb);
    if(afBytes > 0) {
     ret = HandleAFPacket(h, h->afb.afPacket, afBytes);
     if(ret > 0)
      AssembleETIFrame(&h->eti, h->eti_format, h->write_cb, h->write_cb_priv);
    }
   }
  } while (used_bytes > 0 && pktsize > used_bytes);
 } else if(edi_pkt[0]=='A' && edi_pkt[1]=='F') {
  ret = HandleAFPacket(h, edi_pkt, pktsize);
  if(ret > 0)
   AssembleETIFrame(&h->eti, h->eti_format, h->write_cb, h->write_cb_priv);
 } else {
  msg_Log("EDI: packet is unknown: %c%c", ((*__ctype_b_loc ())[(int) ((edi_pkt[0]))] & (unsigned short int) _ISprint) ? edi_pkt[0] : '.', ((*__ctype_b_loc ())[(int) ((edi_pkt[1]))] & (unsigned short int) _ISprint) ? edi_pkt[1] : '.');
 }
 return 0;
edi_error:
 return -1;
}

canonical_original_code:
int HandleEDIPacket(struct edi_handler_t *h, unsigned char *edi_pkt, unsigned long pktsize)
{
 int ret = 0;
 if(pktsize < 2) {
  msg_Log("EDI: packet too small!");
  goto edi_error;
 }
 if(edi_pkt[0]=='P' && edi_pkt[1]=='F') {
  int used_bytes = 0;
  do {
   used_bytes = HandlePFPacket(h, edi_pkt+used_bytes, pktsize - used_bytes);
   if(used_bytes > 0 && h->pf._valid) {
    int afBytes = pushPFTFrag(&h->pf, &h->afb);
    if(afBytes > 0) {
     ret = HandleAFPacket(h, h->afb.afPacket, afBytes);
     if(ret > 0)
      AssembleETIFrame(&h->eti, h->eti_format, h->write_cb, h->write_cb_priv);
    }
   }
  } while (used_bytes > 0 && pktsize > used_bytes);
 } else if(edi_pkt[0]=='A' && edi_pkt[1]=='F') {
  ret = HandleAFPacket(h, edi_pkt, pktsize);
  if(ret > 0)
   AssembleETIFrame(&h->eti, h->eti_format, h->write_cb, h->write_cb_priv);
 } else {
  msg_Log("EDI: packet is unknown: %c%c", ((*__ctype_b_loc ())[(int) ((edi_pkt[0]))] & (unsigned short) _ISprint) ? edi_pkt[0] : '.', ((*__ctype_b_loc ())[(int) ((edi_pkt[1]))] & (unsigned short) _ISprint) ? edi_pkt[1] : '.');
 }
 return 0;
edi_error:
 return -1;
}

variable_types:
pktsize
unsigned long

variable_types: ret int

variable_types: afBytes int


decompiled_code:
__int64 __fastcall socket_open(char *cp, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int v6; // edi
int optval; // [rsp+0h] [rbp-2Ch] BYREF
struct in_addr inp[4]; // [rsp+4h] [rbp-28h] BYREF

optval = 1;
*(_OWORD *)&inp[0].s_addr = 0LL;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 >= 0 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) < 0 )
goto LABEL_7;
inp[1].s_addr = 0;
LOWORD(inp[0].s_addr) = 2;
if ( cp )
inet_aton(cp, &inp[1]);
HIWORD(inp[0].s_addr) = _bswap_16(a2);
if ( bind(v3, (const struct sockaddr *)inp, v4) )
{
LABEL_7:
v6 = v3;
v3 = -1;
close(v6);
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *cp, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int v6; // edi
int optval; // [rsp+0h] [rbp-2Ch] BYREF
struct in_addr inp[4]; // [rsp+4h] [rbp-28h] BYREF

optval = 1;
*(_OWORD *)&inp[0].s_addr = 0LL;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 >= 0 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) < 0 )
goto LABEL_7;
inp[1].s_addr = 0;
LOWORD(inp[0].s_addr) = 2;
if ( cp )
inet_aton(cp, &inp[1]);
HIWORD(inp[0].s_addr) = func0(a2);
if ( bind(v3, (const struct sockaddr *)inp, v4) )
{
LABEL_7:
v6 = v3;
v3 = -1;
close(v6);
}
}
return v3;
}

original_code:
static int socket_open(char *laddr, int port) {
 struct sockaddr_in lsin;
 int sock;
 int reuse=1;
 memset(&lsin, 0, sizeof(struct sockaddr_in));
 sock=socket(2, SOCK_DGRAM, IPPROTO_UDP);
 if (sock<0)
  return sock;
 if (setsockopt(sock, 1, 2,
                   (char *)&reuse, sizeof(reuse)) < 0) {
  close(sock);
  return -1;
 }
 lsin.sin_family=2;
 lsin.sin_addr.s_addr=((in_addr_t) 0x00000000);
 if (laddr)
  inet_aton(laddr, &lsin.sin_addr);
 lsin.sin_port=htons(port);
 if (bind(sock, (struct sockaddr *) &lsin,
   sizeof(struct sockaddr_in)) != 0) {
  close(sock);
  return -1;
 }
 return sock;
}

canonical_original_code:
static int socket_open(char *laddr, int port) {
 struct sockaddr_in lsin;
 int sock;
 int reuse = 1;
 memset(&lsin, 0, sizeof(struct sockaddr_in));
 sock=socket(2, SOCK_DGRAM, IPPROTO_UDP);
 if (sock<0)
  return sock;
 if (setsockopt(sock, 1, 2,
                   (char *)&reuse, sizeof(reuse)) < 0) {
  close(sock);
  return -1;
 }
 lsin.sin_family=2;
 lsin.sin_addr.s_addr=((unsigned int) 0x00000000);
 if (laddr)
  inet_aton(laddr, &lsin.sin_addr);
 lsin.sin_port=htons(port);
 if (bind(sock, (struct sockaddr *) &lsin,
   sizeof(struct sockaddr_in)) != 0) {
  close(sock);
  return -1;
 }
 return sock;
}

variable_types:
laddr
char *

variable_types: port int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: sock int

variable_types: reuse int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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


decompiled_code:
int __fastcall socket_set_nonblock(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
static int socket_set_nonblock(int sock)
{
 unsigned int flags;
 flags=fcntl(sock, 3);
 return fcntl(sock, 4, flags | 04000);
}

variable_types:
flags
unsigned int


decompiled_code:
__int64 __fastcall socket_join_multicast(int fd, char *cp)
{
unsigned int v2; // er8
struct in_addr inp[2]; // [rsp+8h] [rbp-10h] BYREF

*(_QWORD *)&inp[0].s_addr = 0LL;
if ( !inet_aton(cp, inp) )
return (unsigned int)-1;
if ( (_bswap_32(inp[0].s_addr) & 0xF0000000) != -536870912 )
return v2;
inp[1].s_addr = 0;
return (unsigned int)setsockopt(fd, 0, 35, inp, 8u);
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, char *cp)
{
unsigned int v2; // er8
struct in_addr inp[2]; // [rsp+8h] [rbp-10h] BYREF

*(_QWORD *)&inp[0].s_addr = 0LL;
if ( !inet_aton(cp, inp) )
return (unsigned int)-1;
if ( (func3(inp[0].s_addr) & 0xF0000000) != -536870912 )
return v2;
inp[1].s_addr = 0;
return (unsigned int)setsockopt(fd, 0, 35, inp, 8u);
}

original_code:
static int socket_join_multicast(int sock, char *addr) {
 struct ip_mreq mreq;
 memset(&mreq, 0, sizeof(struct ip_mreq));
 if (!inet_aton(addr, &mreq.imr_multiaddr))
  return -1;
 if (!((((in_addr_t)(ntohl(mreq.imr_multiaddr.s_addr))) & 0xf0000000) == 0xe0000000))
  return 0;
 mreq.imr_interface.s_addr=((in_addr_t) 0x00000000);
 return setsockopt(sock, IPPROTO_IP, 35, &mreq, sizeof(mreq));
}

canonical_original_code:
static int socket_join_multicast(int sock, char *addr) {
 struct ip_mreq mreq;
 memset(&mreq, 0, sizeof(struct ip_mreq));
 if (!inet_aton(addr, &mreq.imr_multiaddr))
  return -1;
 if (!((((in_addr_t)(ntohl(mreq.imr_multiaddr.s_addr))) & 0xf0000000) == 0xe0000000))
  return 0;
 mreq.imr_interface.s_addr=((unsigned int) 0x00000000);
 return setsockopt(sock, IPPROTO_IP, 35, &mreq, sizeof(mreq));
}

variable_types:
addr
char *

variable_types: struct ip_mreq { struct in_addr { unsigned int s_addr; } imr_multiaddr; struct in_addr imr_interface; }

user_defined_types: struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; }


decompiled_code:
int __fastcall socket_connect(int fd, char *cp, __int16 a3)
{
struct in_addr inp[4]; // [rsp+0h] [rbp-18h] BYREF

*(_OWORD *)&inp[0].s_addr = 0LL;
LOWORD(inp[0].s_addr) = 2;
HIWORD(inp[0].s_addr) = _bswap_16(a3);
if ( cp )
inet_aton(cp, &inp[1]);
return connect(fd, (const struct sockaddr *)inp, 0x10u);
}

canonical_decompiled_code:
int __fastcall func5(int fd, char *cp, __int16 a3)
{
struct in_addr inp[4]; // [rsp+0h] [rbp-18h] BYREF

*(_OWORD *)&inp[0].s_addr = 0LL;
LOWORD(inp[0].s_addr) = 2;
HIWORD(inp[0].s_addr) = func0(a3);
if ( cp )
inet_aton(cp, &inp[1]);
return connect(fd, (const struct sockaddr *)inp, 0x10u);
}

original_code:
static int socket_connect(int sock, char *addr, int port)
{
 struct sockaddr_in rsin;
 memset(&rsin, 0, sizeof(struct sockaddr_in));
 rsin.sin_family=2;
 rsin.sin_port=htons(port);
 rsin.sin_addr.s_addr=((in_addr_t) 0x00000000);
 if (addr)
  inet_aton(addr, &rsin.sin_addr);
 return connect(sock, (struct sockaddr *) &rsin, sizeof(struct sockaddr_in));
}

canonical_original_code:
static int socket_connect(int sock, char *addr, int port)
{
 struct sockaddr_in rsin;
 memset(&rsin, 0, sizeof(struct sockaddr_in));
 rsin.sin_family=2;
 rsin.sin_port=htons(port);
 rsin.sin_addr.s_addr=((unsigned int) 0x00000000);
 if (addr)
  inet_aton(addr, &rsin.sin_addr);
 return connect(sock, (struct sockaddr *) &rsin, sizeof(struct sockaddr_in));
}


decompiled_code:
__int64 __fastcall input_init_udp(char *cp, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
int v5; // edi

v2 = socket_open(cp, a2);
v3 = v2;
if ( v2 >= 0 && (socket_set_nonblock(v2) < 0 || (int)socket_join_multicast(v3, cp) < 0) )
{
v5 = v3;
v3 = -1;
close(v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *cp, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
int v5; // edi

v2 = func1(cp, a2);
v3 = v2;
if ( v2 >= 0 && (func2(v2) < 0 || (int)func4(v3, cp) < 0) )
{
v5 = v3;
v3 = -1;
close(v5);
}
return v3;
}

original_code:
int input_init_udp(char *ip, int port)
{
 int sock;
 sock=socket_open(ip, port);
 if(sock < 0)
  return sock;
 if(socket_set_nonblock(sock) < 0) {
  close(sock);
  return -1;
 }
 if (socket_join_multicast(sock, ip) < 0) {
  close(sock);
  return -1;
 }
 return sock;
}

variable_types:
ip
char *


decompiled_code:
__int64 __fastcall output_init_udp(char *cp, __int16 a2, int a3)
{
int v4; // eax
unsigned int v5; // er12
int v7; // edi

v4 = socket_open(0LL, 0);
v5 = v4;
if ( v4 < 0 )
return v5;
socket_set_nonblock(v4);
if ( socket_connect(v5, cp, a2) )
{
v7 = v5;
v5 = -1;
close(v7);
return v5;
}
socket_join_multicast(v5, cp);
if ( !a3 )
return v5;
socket_set_ttl_isra_0(v5, a3);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *cp, __int16 a2, int a3)
{
int v4; // eax
unsigned int v5; // er12
int v7; // edi

v4 = func1(0LL, 0);
v5 = v4;
if ( v4 < 0 )
return v5;
func2(v4);
if ( func5(v5, cp, a2) )
{
v7 = v5;
v5 = -1;
close(v7);
return v5;
}
func4(v5, cp);
if ( !a3 )
return v5;
func6(v5, a3);
return v5;
}

original_code:
int output_init_udp(char *ip, int port, int ttl)
{
 int sock;
 sock=socket_open(((void *)0), 0);
 if (sock < 0)
  return sock;
 socket_set_nonblock(sock);
 if (socket_connect(sock, ip, port)) {
  close(sock);
  return -1;
 }
 socket_join_multicast(sock, ip);
 if (ttl)
  socket_set_ttl(sock, ttl);
 return sock;
}

variable_types: ttl int


decompiled_code:
__int64 __fastcall udp_read_timeout(int fd, void *buf, ssize_t *a3, int a4)
{
ssize_t v5; // rax
__int64 result; // rax
struct pollfd fds; // [rsp+8h] [rbp-20h] BYREF

if ( a4 <= 0 )
_assert_fail("timeout_ms > 0", "network.c", 0xA9u, "udp_read_timeout");
fds.fd = fd;
*(_DWORD *)&fds.events = 1;
if ( poll(&fds, 1uLL, a4) )
{
v5 = recv(fd, buf, 0x2000uLL, 64);
*a3 = v5;
if ( v5 == -1 )
result = (unsigned int)*_errno_location();
else
result = v5 == 0 ? 0xFFFFFF98 : 0;
}
else
{
*a3 = 0LL;
result = 4294967186LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, void *buf, ssize_t *a3, int a4)
{
ssize_t v5; // rax
__int64 result; // rax
struct pollfd fds; // [rsp+8h] [rbp-20h] BYREF

if ( a4 <= 0 )
_assert_fail("timeout_ms > 0", "network.c", 0xA9u, "udp_read_timeout");
fds.fd = fd;
*(_DWORD *)&fds.events = 1;
if ( poll(&fds, 1uLL, a4) )
{
v5 = recv(fd, buf, 0x2000uLL, 64);
*a3 = v5;
if ( v5 == -1 )
result = (unsigned int)*_errno_location();
else
result = v5 == 0 ? 0xFFFFFF98 : 0;
}
else
{
*a3 = 0LL;
result = 4294967186LL;
}
return result;
}

original_code:
int udp_read_timeout(int fd, void *buf, size_t *len, int timeout_ms)
{
    int x;
    struct pollfd fds;
    ((void) sizeof ((timeout_ms > 0) ? 1 : 0), __extension__ ({ if (timeout_ms > 0) ; else __assert_fail ("timeout_ms > 0", "network.c", 169, __extension__ __PRETTY_FUNCTION__); }));
    fds.fd = fd;
    fds.events = 0x001;
    fds.revents = 0;
    x = poll(&fds, 1, timeout_ms);
    if(x == 0) {
        *len = 0;
        return -110;
    }
    *len = recv(fd, buf, 8192, MSG_DONTWAIT);
    if(*len == -1)
        return (*__errno_location ());
    if(*len == 0)
        return -104;
    return 0;
}

canonical_original_code:
int udp_read_timeout(int fd, void *buf, unsigned long *len, int timeout_ms)
{
    int x;
    struct pollfd fds;
    ((void) sizeof ((timeout_ms > 0) ? 1 : 0), __extension__ ({ if (timeout_ms > 0) ; else __assert_fail ("timeout_ms > 0", "network.c", 169, __extension__ __PRETTY_FUNCTION__); }));
    fds.fd = fd;
    fds.events = 0x001;
    fds.revents = 0;
    x = poll(&fds, 1, timeout_ms);
    if(x == 0) {
        *len = 0;
        return -110;
    }
    *len = recv(fd, buf, 8192, MSG_DONTWAIT);
    if(*len == -1)
        return (*__errno_location ());
    if(*len == 0)
        return -104;
    return 0;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
len
unsigned long *

variable_types: x int

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


decompiled_code:
__int64 __fastcall ts_set_pid(__int64 a1, unsigned __int16 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(unsigned __int8 *)(a1 + 1);
*(_BYTE *)(a1 + 2) = a2;
result = HIBYTE(a2) & 0x1F | v2 & 0xFFFFFFE0;
*(_BYTE *)(a1 + 1) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int16 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(unsigned __int8 *)(a1 + 1);
*(_BYTE *)(a1 + 2) = a2;
result = HIBYTE(a2) & 0x1F | v2 & 0xFFFFFFE0;
*(_BYTE *)(a1 + 1) = result;
return result;
}

original_code:
static inline void ts_set_pid(uint8_t *p_ts, uint16_t i_pid)
{
    p_ts[1] &= ~0x1f;
    p_ts[1] |= (i_pid >> 8) & 0x1f;
    p_ts[2] = i_pid & 0xff;
}

canonical_original_code:
static inline void ts_set_pid(unsigned char *p_ts, unsigned short i_pid)
{
    p_ts[1] &= ~0x1f;
    p_ts[1] |= (i_pid >> 8) & 0x1f;
    p_ts[2] = i_pid & 0xff;
}


decompiled_code:
__int64 __fastcall ts_set_cc(__int64 a1, char a2)
{
int v2; // esi
__int64 result; // rax

v2 = a2 & 0xF;
result = v2 | *(_BYTE *)(a1 + 3) & 0xF0u;
*(_BYTE *)(a1 + 3) = v2 | *(_BYTE *)(a1 + 3) & 0xF0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2)
{
int v2; // esi
__int64 result; // rax

v2 = a2 & 0xF;
result = v2 | *(_BYTE *)(a1 + 3) & 0xF0u;
*(_BYTE *)(a1 + 3) = v2 | *(_BYTE *)(a1 + 3) & 0xF0;
return result;
}

original_code:
static inline void ts_set_cc(uint8_t *p_ts, uint8_t i_cc)
{
    p_ts[3] &= ~0xf;
    p_ts[3] |= (i_cc & 0xf);
}

canonical_original_code:
static inline void ts_set_cc(unsigned char *p_ts, unsigned char i_cc)
{
    p_ts[3] &= ~0xf;
    p_ts[3] |= (i_cc & 0xf);
}


decompiled_code:
void __fastcall ts_set_payload(__int64 a1)
{
*(_BYTE *)(a1 + 3) |= 0x10u;
}

original_code:
static inline void ts_set_payload(uint8_t *p_ts)
{
    p_ts[3] |= 0x10;
}

canonical_original_code:
static inline void ts_set_payload(unsigned char *p_ts)
{
    p_ts[3] |= 0x10;
}


original_code:
static inline void ts_pad(uint8_t *p_ts)
{
    ts_init(p_ts);
    ts_set_pid(p_ts, 0x1fff);
    ts_set_cc(p_ts, 0);
    ts_set_payload(p_ts);
    memset(p_ts + 4, 0xff, 188 - 4);
}

canonical_original_code:
static inline void ts_pad(unsigned char *p_ts)
{
    ts_init(p_ts);
    ts_set_pid(p_ts, 0x1fff);
    ts_set_cc(p_ts, 0);
    ts_set_payload(p_ts);
    memset(p_ts + 4, 0xff, 188 - 4);
}


decompiled_code:
int msg_Dbg(__int64 a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

if ( verbosity > 2 )
{
va_start(arg, a2);
snprintf(s, 0x400uLL, "debug: %s\n", a2);
result = vfprintf(stderr, s, arg);
}
return result;
}

canonical_decompiled_code:
int func0(__int64 a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

if ( verbosity > 2 )
{
va_start(arg, a2);
snprintf(s, 0x400uLL, "debug: %s\n", a2);
result = vfprintf(stderr, s, arg);
}
return result;
}

original_code:
void msg_Dbg( void *_unused, const char *psz_format, ... )
{
    if (verbosity > 2)
    {
        va_list args;
        char psz_fmt[1024];
        __builtin_va_start(args,psz_format);
        snprintf( psz_fmt, 1024, "debug: %s\n", psz_format );
        vfprintf( stderr, psz_fmt, args );
    }
}

canonical_original_code:
void msg_Dbg(void *_unused, char *psz_format, ...)
{
    if (verbosity > 2)
    {
        __builtin_va_list args;
        char psz_fmt[1024];
        __builtin_va_start(args,psz_format);
        snprintf( psz_fmt, 1024, "debug: %s\n", psz_format );
        vfprintf( stderr, psz_fmt, args );
    }
}


decompiled_code:
int msg_Log(const char *a1, ...)
{
struct tm *v1; // rax
int v2; // eax
time_t v4; // [rsp+0h] [rbp-4D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a1);
time(&v4);
v1 = localtime(&v4);
v2 = strftime(s, 0x400uLL, "\b[%x %X]", v1);
snprintf(&s[v2], 1024 - v2, " %s", a1);
vfprintf(stderr, s, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func1(const char *a1, ...)
{
struct tm *v1; // rax
int v2; // eax
time_t v4; // [rsp+0h] [rbp-4D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a1);
time(&v4);
v1 = localtime(&v4);
v2 = strftime(s, 0x400uLL, "\b[%x %X]", v1);
snprintf(&s[v2], 1024 - v2, " %s", a1);
vfprintf(stderr, s, arg);
return fputc(10, stderr);
}

original_code:
void msg_Log(const char *psz_format, ...)
{
 va_list args;
 time_t rawtime;
 struct tm * timeinfo;
 char psz_fmt[1024];
 int size = 0;
 __builtin_va_start(args,psz_format);
 time(&rawtime);
 timeinfo = localtime(&rawtime);
 size = strftime(psz_fmt, 1024, "\x08[%x %X]", timeinfo);
 snprintf(psz_fmt + size, 1024 - size, " %s", psz_format);
 vfprintf(stderr, psz_fmt, args);
 fprintf(stderr, "\n");
}

canonical_original_code:
void msg_Log(char *psz_format, ...)
{
 __builtin_va_list args;
 long rawtime;
 struct tm *timeinfo;
 char psz_fmt[1024];
 int size = 0;
 __builtin_va_start(args,psz_format);
 time(&rawtime);
 timeinfo = localtime(&rawtime);
 size = strftime(psz_fmt, 1024, "\x08[%x %X]", timeinfo);
 snprintf(psz_fmt + size, 1024 - size, " %s", psz_format);
 vfprintf(stderr, psz_fmt, args);
 fprintf(stderr, "\n");
}

variable_types: rawtime long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: size int


decompiled_code:
int __fastcall msg_Dump(__int64 a1, int a2)
{
int result; // eax
int v3; // er14
const unsigned __int16 **v4; // r13
int v5; // er15
char *v6; // rbx
unsigned __int8 *v7; // rbp
unsigned __int8 *v8; // r12
__int64 v9; // rdx
int v10; // er12
int v11; // edi
int v12; // ebx
int v13; // [rsp+8h] [rbp-50h]
__int64 v15; // [rsp+18h] [rbp-40h]

result = fprintf(stderr, "dump %d bytes:\n", (unsigned int)a2);
if ( a2 > 0 )
{
v3 = 0;
v4 = _ctype_b_loc();
v15 = a1 + 1;
do
{
v5 = 32;
v13 = a2 - v3;
if ( a2 - v3 <= 32 )
v5 = a2 - v3;
fwrite("\t\t\t\t", 1uLL, 4uLL, stderr);
v6 = (char *)(a1 + v3);
v7 = (unsigned __int8 *)((unsigned int)(v5 - 1) + v15 + v3);
v8 = (unsigned __int8 *)v6;
do
{
v9 = *v8++;
fprintf(stderr, "%02x ", v9);
}
while ( v7 != v8 );
v10 = v5;
if ( v13 <= 31 )
{
do
{
++v10;
fwrite(" ", 1uLL, 3uLL, stderr);
}
while ( v10 != 32 );
}
fwrite("\t\"", 1uLL, 2uLL, stderr);
do
{
v11 = *v6;
if ( ((*v4)[*v6] & 0x4000) == 0 )
v11 = 46;
++v6;
fputc(v11, stderr);
}
while ( v6 != (char *)v7 );
v12 = v5;
if ( v13 <= 31 )
{
do
{
++v12;
fputc(32, stderr);
}
while ( v12 != 32 );
}
v3 += v5;
result = fwrite("\"\n", 1uLL, 2uLL, stderr);
}
while ( a2 > v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, int a2)
{
int result; // eax
int v3; // er14
const unsigned __int16 **v4; // r13
int v5; // er15
char *v6; // rbx
unsigned __int8 *v7; // rbp
unsigned __int8 *v8; // r12
__int64 v9; // rdx
int v10; // er12
int v11; // edi
int v12; // ebx
int v13; // [rsp+8h] [rbp-50h]
__int64 v15; // [rsp+18h] [rbp-40h]

result = fprintf(stderr, "dump %d bytes:\n", (unsigned int)a2);
if ( a2 > 0 )
{
v3 = 0;
v4 = _ctype_b_loc();
v15 = a1 + 1;
do
{
v5 = 32;
v13 = a2 - v3;
if ( a2 - v3 <= 32 )
v5 = a2 - v3;
fwrite("\t\t\t\t", 1uLL, 4uLL, stderr);
v6 = (char *)(a1 + v3);
v7 = (unsigned __int8 *)((unsigned int)(v5 - 1) + v15 + v3);
v8 = (unsigned __int8 *)v6;
do
{
v9 = *v8++;
fprintf(stderr, "%02x ", v9);
}
while ( v7 != v8 );
v10 = v5;
if ( v13 <= 31 )
{
do
{
++v10;
fwrite(" ", 1uLL, 3uLL, stderr);
}
while ( v10 != 32 );
}
fwrite("\t\"", 1uLL, 2uLL, stderr);
do
{
v11 = *v6;
if ( ((*v4)[*v6] & 0x4000) == 0 )
v11 = 46;
++v6;
fputc(v11, stderr);
}
while ( v6 != (char *)v7 );
v12 = v5;
if ( v13 <= 31 )
{
do
{
++v12;
fputc(32, stderr);
}
while ( v12 != 32 );
}
v3 += v5;
result = fwrite("\"\n", 1uLL, 2uLL, stderr);
}
while ( a2 > v3 );
}
return result;
}

original_code:
void msg_Dump(char *bytes, int len)
{
    int i;
    int count;
    int done = 0;
    fprintf(stderr, "dump %d bytes:\n", len);
    while (len > done) {
  if (len-done > 32){
   count = 32;
  } else {
   count = len-done;
  }
  fprintf(stderr, "\t\t\t\t");
  for (i=0; i<count; i++) {
      fprintf(stderr, "%02x ", (int)((unsigned char)bytes[done+i]));
  }
  for (; i<32; i++) {
      fprintf(stderr, "   ");
  }
  fprintf(stderr, "\t\"");
        for (i=0; i<count; i++) {
      fprintf(stderr, "%c", ((*__ctype_b_loc ())[(int) ((bytes[done+i]))] & (unsigned short int) _ISprint) ? bytes[done+i] : '.');
        }
        for (; i<32; i++) {
      fprintf(stderr, " ");
  }
  fprintf(stderr, "\"\n");
     done += count;
    }
}

canonical_original_code:
void msg_Dump(char *bytes, int len)
{
    int i;
    int count;
    int done = 0;
    fprintf(stderr, "dump %d bytes:\n", len);
    while (len > done) {
  if (len-done > 32){
   count = 32;
  } else {
   count = len-done;
  }
  fprintf(stderr, "\t\t\t\t");
  for (i=0; i<count; i++) {
      fprintf(stderr, "%02x ", (int)((unsigned char)bytes[done+i]));
  }
  for (; i<32; i++) {
      fprintf(stderr, "   ");
  }
  fprintf(stderr, "\t\"");
        for (i=0; i<count; i++) {
      fprintf(stderr, "%c", ((*__ctype_b_loc ())[(int) ((bytes[done+i]))] & (unsigned short) _ISprint) ? bytes[done+i] : '.');
        }
        for (; i<32; i++) {
      fprintf(stderr, " ");
  }
  fprintf(stderr, "\"\n");
     done += count;
    }
}

variable_types:
bytes
char *

variable_types: len int

variable_types: i int

variable_types: count int

variable_types: done int


ficinit

original_code:
int ficinit(struct ens_info *einf)
{
 int j;
 einf->num_schans = 0;
 einf->num_srvs = 0;
 einf->srv = ((void *)0);
 for (j=0; j < 64; j++)
  einf->schan[j].subchid = 0xffff;
 return 0;
}

variable_types: struct ens_info { char label[17]; unsigned short eid; int num_srvs; int num_schans; struct service { char label[17]; int sid; struct subch { int subchid; int startaddr; int subchsz; int bitrate; int eepprot; int protlvl; int uep_indx; int dabplus; } *pa; struct subch *sa; struct service *next; struct service *prev; } *srv; struct subch schan[64]; } *

variable_types: j int

user_defined_types: struct ens_info { char label[17]; unsigned short eid; int num_srvs; int num_schans; struct service *srv; struct subch schan[64]; }


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

for ( result = *(_QWORD *)(a1 + 32); result; result = *(_QWORD *)(result + 40) )
{
if ( *(_DWORD *)(result + 20) == a2 )
break;
}
return result;
}

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

for ( result = *(_QWORD *)(a1 + 32); result; result = *(_QWORD *)(result + 40) )
{
if ( *(_DWORD *)(result + 20) == a2 )
break;
}
return result;
}

original_code:
struct service* find_service(struct ens_info *e, int sid)
{
 char found = 0;
 struct service *s;
 s = e->srv;
 while (s != ((void *)0)) {
  if (s->sid == sid) {
   found = 1;
   break;
  } else
   s = s->next;
 };
 if (found)
  return s;
 else
  return (struct service*)((void *)0);
}

canonical_original_code:
struct service *find_service(struct ens_info *e, int sid)
{
 char found = 0;
 struct service *s;
 s = e->srv;
 while (s != ((void *)0)) {
  if (s->sid == sid) {
   found = 1;
   break;
  } else
   s = s->next;
 };
 if (found)
  return s;
 else
  return (struct service *)((void *)0);
}

variable_types: struct ens_info { char label[17]; unsigned short eid; int num_srvs; int num_schans; struct service *srv; struct subch schan[64]; } *

variable_types: sid int

variable_types: found char

variable_types: struct service { char label[17]; int sid; struct subch { int subchid; int startaddr; int subchsz; int bitrate; int eepprot; int protlvl; int uep_indx; int dabplus; } *pa; struct subch *sa; struct service *next; struct service *prev; } *


original_code:
int add_service(struct ens_info *e, struct mscstau *ac, int sid)
{
 struct service *s, *ptr;
 if ((s = find_service(e, sid)) == (struct service*)((void *)0)) {
  if ((s = (struct service*)malloc(sizeof(struct service))) == ((void *)0))
   perror("add_service: malloc failed");
  s->label[0] = '\0';
  s->sid = sid;
  s->next = ((void *)0);
  s->pa = ((void *)0);
  s->sa = ((void *)0);
  if ((ptr = e->srv) == ((void *)0)) {
   e->srv = s;
   s->next = ((void *)0);
   s->prev = ((void *)0);
  } else
   while (ptr != ((void *)0))
    if (ptr->next == ((void *)0)) {
     ptr->next = s;
     s->prev = ptr;
     break;
    } else
     ptr = ptr->next;
 }
 if (ac->ASCTy == 0x3f)
  e->schan[ac->SubChId].dabplus = 1;
 else
  e->schan[ac->SubChId].dabplus = 0;
 if ((ac->Primary && (s->pa == ((void *)0))) || (!(ac->Primary) && (s->sa == ((void *)0)))) {
  if (ac->Primary && (s->pa == ((void *)0))) {
   s->pa = &e->schan[ac->SubChId];
   e->num_srvs++;
  } else
   if (!(ac->Primary) && (s->sa == ((void *)0))) {
    s->sa = &e->schan[ac->SubChId];
    e->num_srvs++;
   }
 }
 return 0;
}

canonical_original_code:
int add_service(struct ens_info *e, struct mscstau *ac, int sid)
{
 struct service *s; struct service *ptr;
 if ((s = find_service(e, sid)) == (struct service *)((void *)0)) {
  if ((s = (struct service *)malloc(sizeof(struct service))) == ((void *)0))
   perror("add_service: malloc failed");
  s->label[0] = '\0';
  s->sid = sid;
  s->next = ((void *)0);
  s->pa = ((void *)0);
  s->sa = ((void *)0);
  if ((ptr = e->srv) == ((void *)0)) {
   e->srv = s;
   s->next = ((void *)0);
   s->prev = ((void *)0);
  } else
   while (ptr != ((void *)0))
    if (ptr->next == ((void *)0)) {
     ptr->next = s;
     s->prev = ptr;
     break;
    } else
     ptr = ptr->next;
 }
 if (ac->ASCTy == 0x3f)
  e->schan[ac->SubChId].dabplus = 1;
 else
  e->schan[ac->SubChId].dabplus = 0;
 if ((ac->Primary && (s->pa == ((void *)0))) || (!(ac->Primary) && (s->sa == ((void *)0)))) {
  if (ac->Primary && (s->pa == ((void *)0))) {
   s->pa = &e->schan[ac->SubChId];
   e->num_srvs++;
  } else
   if (!(ac->Primary) && (s->sa == ((void *)0))) {
    s->sa = &e->schan[ac->SubChId];
    e->num_srvs++;
   }
 }
 return 0;
}

variable_types: struct mscstau { unsigned int CAFlag; unsigned int Primary; unsigned int SubChId; unsigned int ASCTy; unsigned int TMId; } *

user_defined_types: struct subch { int subchid; int startaddr; int subchsz; int bitrate; int eepprot; int protlvl; int uep_indx; int dabplus; }


original_code:
int add_subchannel(struct ens_info *e, struct subch *s)
{
 if (e->schan[s->subchid].subchid > 64) {
  memcpy(&e->schan[s->subchid], s, sizeof(struct subch));
 }
 return 0;
}

variable_types:
s
struct subch *


labelled

decompiled_code:
__int64 __fastcall labelled(__int64 a1)
{
__int64 v1; // rax

v1 = *(_QWORD *)(a1 + 32);
if ( v1 )
{
while ( *(_BYTE *)v1 )
{
v1 = *(_QWORD *)(v1 + 40);
if ( !v1 )
return 1LL;
}
}
return 0LL;
}

original_code:
int labelled(struct ens_info* e)
{
 struct service *p;
 int labelled = 1;
 p = e->srv;
 if (p == ((void *)0))
  return 0;
 while (p != ((void *)0)) {
  if (strlen(p->label) == 0) {
   labelled = 0;
   break;
  }
  p = p->next;
 }
 return labelled;
}

canonical_original_code:
int labelled(struct ens_info *e)
{
 struct service *p;
 int labelled = 1;
 p = e->srv;
 if (p == ((void *)0))
  return 0;
 while (p != ((void *)0)) {
  if (strlen(p->label) == 0) {
   labelled = 0;
   break;
  }
  p = p->next;
 }
 return labelled;
}

variable_types: labelled int


decompiled_code:
__int64 __fastcall disp_ensemble(__int64 a1)
{
__int64 v1; // rcx
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int *v4; // rax
_DWORD *v5; // rax
__int64 v6; // rcx
_DWORD *v7; // rax
__int64 v8; // rcx
unsigned int v9; // er12
__int64 v11[11]; // [rsp+0h] [rbp-58h]

v1 = *(unsigned __int16 *)(a1 + 18);
v11[0] = (__int64)"eep-1a";
v11[1] = (__int64)"eep-2a";
v11[2] = (__int64)"eep-3a";
v11[3] = (__int64)"eep-4a";
v11[4] = (__int64)"eep-1b";
v11[5] = (__int64)"eep-2b";
v11[6] = (__int64)"eep-3b";
v11[7] = (__int64)"eep-4b";
fprintf(stderr, "%s (%#04hx)\n", (const char *)a1, v1);
v2 = *(_QWORD *)(a1 + 32);
if ( v2 )
{
v3 = 0;
do
{
v9 = v3 + 1;
fprintf(stderr, "%2d : ", v3);
if ( *(_BYTE *)v2 )
{
fprintf(stderr, "%16s (%#04x) ", (const char *)v2, *(unsigned int *)(v2 + 20));
v4 = *(unsigned int **)(v2 + 24);
if ( !v4 )
goto LABEL_12;
}
else
{
fprintf(stderr, "<No label> (%#04x) ", *(unsigned int *)(v2 + 20));
v4 = *(unsigned int **)(v2 + 24);
if ( !v4 )
{
LABEL_12:
++v3;
goto LABEL_9;
}
}
fprintf(stderr, "Pri subch=%2d start=%3d CUs=%3d ", *v4, v4[1], v4[2]);
v5 = *(_DWORD **)(v2 + 24);
v6 = (unsigned int)v5[3];
if ( v5[4] )
{
fprintf(stderr, "PL=%s bitrate=%d", (const char *)v11[v5[5]], v6);
if ( !*(_DWORD *)(*(_QWORD *)(v2 + 24) + 28LL) )
goto LABEL_6;
}
else
{
fprintf(stderr, "PL=uep %d bitrate=%d", (unsigned int)v5[5], v6);
if ( !*(_DWORD *)(*(_QWORD *)(v2 + 24) + 28LL) )
goto LABEL_6;
}
fwrite(" DAB+", 1uLL, 5uLL, stderr);
LABEL_6:
if ( !*(_QWORD *)(v2 + 32) )
goto LABEL_12;
v3 += 2;
fputc(10, stderr);
fprintf(stderr, "%2d : ", v9);
fprintf(
stderr,
"Sec subch=%2d start=%2d CUs=%3d ",
**(unsigned int **)(v2 + 32),
*(unsigned int *)(*(_QWORD *)(v2 + 32) + 4LL),
*(unsigned int *)(*(_QWORD *)(v2 + 32) + 8LL));
v7 = *(_DWORD **)(v2 + 32);
v8 = (unsigned int)v7[3];
if ( v7[4] )
{
fprintf(stderr, "PL=%s bitrate=%d", (const char *)v11[v7[5]], v8);
if ( *(_DWORD *)(*(_QWORD *)(v2 + 32) + 28LL) )
goto LABEL_16;
}
else
{
fprintf(stderr, "PL=uep %d bitrate=%d", (unsigned int)v7[5], v8);
if ( *(_DWORD *)(*(_QWORD *)(v2 + 32) + 28LL) )
{
LABEL_16:
fwrite(" DAB+", 1uLL, 5uLL, stderr);
goto LABEL_9;
}
}
LABEL_9:
v2 = *(_QWORD *)(v2 + 40);
fputc(10, stderr);
fflush(stderr);
}
while ( v2 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rcx
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int *v4; // rax
_DWORD *v5; // rax
__int64 v6; // rcx
_DWORD *v7; // rax
__int64 v8; // rcx
unsigned int v9; // er12
__int64 v11[11]; // [rsp+0h] [rbp-58h]

v1 = *(unsigned __int16 *)(a1 + 18);
v11[0] = (__int64)"eep-1a";
v11[1] = (__int64)"eep-2a";
v11[2] = (__int64)"eep-3a";
v11[3] = (__int64)"eep-4a";
v11[4] = (__int64)"eep-1b";
v11[5] = (__int64)"eep-2b";
v11[6] = (__int64)"eep-3b";
v11[7] = (__int64)"eep-4b";
fprintf(stderr, "%s (%#04hx)\n", (const char *)a1, v1);
v2 = *(_QWORD *)(a1 + 32);
if ( v2 )
{
v3 = 0;
do
{
v9 = v3 + 1;
fprintf(stderr, "%2d : ", v3);
if ( *(_BYTE *)v2 )
{
fprintf(stderr, "%16s (%#04x) ", (const char *)v2, *(unsigned int *)(v2 + 20));
v4 = *(unsigned int **)(v2 + 24);
if ( !v4 )
goto LABEL_12;
}
else
{
fprintf(stderr, "<No label> (%#04x) ", *(unsigned int *)(v2 + 20));
v4 = *(unsigned int **)(v2 + 24);
if ( !v4 )
{
LABEL_12:
++v3;
goto LABEL_9;
}
}
fprintf(stderr, "Pri subch=%2d start=%3d CUs=%3d ", *v4, v4[1], v4[2]);
v5 = *(_DWORD **)(v2 + 24);
v6 = (unsigned int)v5[3];
if ( v5[4] )
{
fprintf(stderr, "PL=%s bitrate=%d", (const char *)v11[v5[5]], v6);
if ( !*(_DWORD *)(*(_QWORD *)(v2 + 24) + 28LL) )
goto LABEL_6;
}
else
{
fprintf(stderr, "PL=uep %d bitrate=%d", (unsigned int)v5[5], v6);
if ( !*(_DWORD *)(*(_QWORD *)(v2 + 24) + 28LL) )
goto LABEL_6;
}
fwrite(" DAB+", 1uLL, 5uLL, stderr);
LABEL_6:
if ( !*(_QWORD *)(v2 + 32) )
goto LABEL_12;
v3 += 2;
fputc(10, stderr);
fprintf(stderr, "%2d : ", v9);
fprintf(
stderr,
"Sec subch=%2d start=%2d CUs=%3d ",
**(unsigned int **)(v2 + 32),
*(unsigned int *)(*(_QWORD *)(v2 + 32) + 4LL),
*(unsigned int *)(*(_QWORD *)(v2 + 32) + 8LL));
v7 = *(_DWORD **)(v2 + 32);
v8 = (unsigned int)v7[3];
if ( v7[4] )
{
fprintf(stderr, "PL=%s bitrate=%d", (const char *)v11[v7[5]], v8);
if ( *(_DWORD *)(*(_QWORD *)(v2 + 32) + 28LL) )
goto LABEL_16;
}
else
{
fprintf(stderr, "PL=uep %d bitrate=%d", (unsigned int)v7[5], v8);
if ( *(_DWORD *)(*(_QWORD *)(v2 + 32) + 28LL) )
{
LABEL_16:
fwrite(" DAB+", 1uLL, 5uLL, stderr);
goto LABEL_9;
}
}
LABEL_9:
v2 = *(_QWORD *)(v2 + 40);
fputc(10, stderr);
fflush(stderr);
}
while ( v2 );
}
return 0LL;
}

original_code:
int disp_ensemble(struct ens_info* e)
{
 struct service *p;
 int i = 0;
 char *eeptype[] = {"eep-1a","eep-2a","eep-3a","eep-4a",
      "eep-1b","eep-2b","eep-3b","eep-4b"};
 fprintf(stderr,"%s (%#04hx)\n",e->label,e->eid);
 p = e->srv;
 while (p != ((void *)0)) {
  fprintf(stderr,"%2d : ",i++);
  if (strlen(p->label) != 0)
   fprintf(stderr,"%16s (%#04x) ",p->label,p->sid);
  else
   fprintf(stderr,"<No label>       (%#04x) ",p->sid);
  if (p->pa != ((void *)0)) {
   fprintf(stderr,"Pri subch=%2d start=%3d CUs=%3d ",p->pa->subchid,p->pa->startaddr,p->pa->subchsz);
   if (p->pa->eepprot)
    fprintf(stderr,"PL=%s bitrate=%d",eeptype[p->pa->protlvl],p->pa->bitrate);
   else
    fprintf(stderr,"PL=uep %d bitrate=%d",p->pa->protlvl,p->pa->bitrate);
   if (p->pa->dabplus)
    fprintf(stderr," DAB+");
   if (p->sa != ((void *)0)) {
    fprintf(stderr,"\n");
    fprintf(stderr,"%2d :                           ",i++);
    fprintf(stderr,"Sec subch=%2d start=%2d CUs=%3d ",p->sa->subchid,p->sa->startaddr,p->sa->subchsz);
    if (p->sa->eepprot)
     fprintf(stderr,"PL=%s bitrate=%d",eeptype[p->sa->protlvl],p->sa->bitrate);
    else
     fprintf(stderr,"PL=uep %d bitrate=%d",p->sa->protlvl,p->sa->bitrate);
    if (p->sa->dabplus)
     fprintf(stderr," DAB+");
   }
  }
  p = p->next;
  fprintf(stderr,"\n");
  fflush(stderr);
 }
 return 0;
}

canonical_original_code:
int disp_ensemble(struct ens_info *e)
{
 struct service *p;
 int i = 0;
 char *eeptype[] = {"eep-1a","eep-2a","eep-3a","eep-4a",
      "eep-1b","eep-2b","eep-3b","eep-4b"};
 fprintf(stderr,"%s (%#04hx)\n",e->label,e->eid);
 p = e->srv;
 while (p != ((void *)0)) {
  fprintf(stderr,"%2d : ",i++);
  if (strlen(p->label) != 0)
   fprintf(stderr,"%16s (%#04x) ",p->label,p->sid);
  else
   fprintf(stderr,"<No label>       (%#04x) ",p->sid);
  if (p->pa != ((void *)0)) {
   fprintf(stderr,"Pri subch=%2d start=%3d CUs=%3d ",p->pa->subchid,p->pa->startaddr,p->pa->subchsz);
   if (p->pa->eepprot)
    fprintf(stderr,"PL=%s bitrate=%d",eeptype[p->pa->protlvl],p->pa->bitrate);
   else
    fprintf(stderr,"PL=uep %d bitrate=%d",p->pa->protlvl,p->pa->bitrate);
   if (p->pa->dabplus)
    fprintf(stderr," DAB+");
   if (p->sa != ((void *)0)) {
    fprintf(stderr,"\n");
    fprintf(stderr,"%2d :                           ",i++);
    fprintf(stderr,"Sec subch=%2d start=%2d CUs=%3d ",p->sa->subchid,p->sa->startaddr,p->sa->subchsz);
    if (p->sa->eepprot)
     fprintf(stderr,"PL=%s bitrate=%d",eeptype[p->sa->protlvl],p->sa->bitrate);
    else
     fprintf(stderr,"PL=uep %d bitrate=%d",p->sa->protlvl,p->sa->bitrate);
    if (p->sa->dabplus)
     fprintf(stderr," DAB+");
   }
  }
  p = p->next;
  fprintf(stderr,"\n");
  fflush(stderr);
 }
 return 0;
}

variable_types:
eeptype
char *[]


decompiled_code:
__int64 __fastcall user_select_service(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 v4; // rax
int v5; // edx
int v6; // esi
__int64 v7; // rcx
__int64 result; // rax

v2 = *((_DWORD *)&einf + 5);
if ( !rq_0 )
{
fprintf(stderr, "Please select a service (0 to %d): ", (unsigned int)(v2 - 1));
v2 = *((_DWORD *)&einf + 5);
rq_0 = 1;
}
if ( v2 > 1 )
{
v4 = *((_QWORD *)&einf + 4);
v5 = 0;
if ( !v4 )
return 0LL;
while ( 1 )
{
v7 = *(_QWORD *)(v4 + 32);
if ( v5 == 1 )
{
*(_DWORD *)(a2 + 64) = *(_DWORD *)(v4 + 20);
*(_QWORD *)a2 = *(_QWORD *)(v4 + 24);
LABEL_12:
if ( !v7 )
goto LABEL_13;
LABEL_9:
v4 = *(_QWORD *)(v4 + 40);
v5 += 2;
if ( !v4 )
return 0LL;
}
else
{
if ( v5 )
goto LABEL_12;
if ( v7 )
{
v6 = *(_DWORD *)(v4 + 20);
*(_QWORD *)a2 = v7;
*(_DWORD *)(a2 + 64) = v6;
goto LABEL_9;
}
LABEL_13:
v4 = *(_QWORD *)(v4 + 40);
++v5;
if ( !v4 )
return 0LL;
}
}
}
*(_QWORD *)a2 = 0LL;
result = 0LL;
rq_0 = 0;
return result;
}

original_code:
int user_select_service(struct ens_info* e, struct selsrv *sel_srv)
{
 struct service *p;
 int srvs = einf.num_srvs-1, i = -1, j = 0;
 static int rq = 0;
 if (!rq) {
  fprintf(stderr,"Please select a service (0 to %d): ",srvs);
  rq = 1;
 }
 i = 1;
 if (i != -1) {
  if ((i < einf.num_srvs) && (i >= 0)) {
   p = einf.srv;
   while (p != ((void *)0)) {
    if ((j == i) || ((j == i - 1) && (p->sa != ((void *)0)))) {
     sel_srv->sid = p->sid;
     if ((j == i - 1) && (p->sa != ((void *)0)))
      sel_srv->sch = p->sa;
     else
      sel_srv->sch = p->pa;
    }
    if (p->sa == ((void *)0))
     j += 1;
    else
     j += 2;
    p = p->next;
   }
  } else {
   rq = 0;
   sel_srv->sch = ((void *)0);
  }
 }
 return 0;
}

canonical_original_code:
int user_select_service(struct ens_info *e, struct selsrv *sel_srv)
{
 struct service *p;
 int srvs = einf.num_srvs-1; int i = -1; int j = 0;
 static int rq = 0;
 if (!rq) {
  fprintf(stderr,"Please select a service (0 to %d): ",srvs);
  rq = 1;
 }
 i = 1;
 if (i != -1) {
  if ((i < einf.num_srvs) && (i >= 0)) {
   p = einf.srv;
   while (p != ((void *)0)) {
    if ((j == i) || ((j == i - 1) && (p->sa != ((void *)0)))) {
     sel_srv->sid = p->sid;
     if ((j == i - 1) && (p->sa != ((void *)0)))
      sel_srv->sch = p->sa;
     else
      sel_srv->sch = p->pa;
    }
    if (p->sa == ((void *)0))
     j += 1;
    else
     j += 2;
    p = p->next;
   }
  } else {
   rq = 0;
   sel_srv->sch = ((void *)0);
  }
 }
 return 0;
}

variable_types: struct selsrv { struct subch *sch; struct symrange { int start[4]; int end[4]; int startcu; int endcu; int numsyms; struct symrange *next; } sr; int sid; } *

variable_types: srvs int

variable_types: rq int

user_defined_types: struct selsrv { struct subch *sch; struct symrange sr; int sid; }


original_code:
int process_fic2(unsigned char* rawfibs, int ficl, int mode_id)
{
 unsigned char *fig = rawfibs;
 int i,j,figlen;
 if(!ficl) return 0;
 for (i=0; i < ficl/32; i++) {
  fig = rawfibs + 32*i;
  j = 0;
  while ((j < 30) && (*fig != 0xff)) {
   figlen = ((struct fig_hdr*)fig)->flen;
   unpickfig(fig, figlen);
   j += figlen + 1;
   fig += figlen + 1;
  }
 }
 return 0;
}

canonical_original_code:
int process_fic2(unsigned char *rawfibs, int ficl, int mode_id)
{
 unsigned char *fig = rawfibs;
 int i; int j; int figlen;
 if(!ficl) return 0;
 for (i=0; i < ficl/32; i++) {
  fig = rawfibs + 32*i;
  j = 0;
  while ((j < 30) && (*fig != 0xff)) {
   figlen = ((struct fig_hdr *)fig)->flen;
   unpickfig(fig, figlen);
   j += figlen + 1;
   fig += figlen + 1;
  }
 }
 return 0;
}

variable_types:
rawfibs
unsigned char *

variable_types: ficl int

variable_types:
fig
unsigned char *

variable_types: figlen int

user_defined_types: struct fig_hdr { unsigned int flen; unsigned int ftype; }


ipack

decompiled_code:
__int64 __fastcall ipack(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

variable_types:
f
unsigned char *

variable_types: i int


spack

decompiled_code:
__int16 __fastcall spack(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int16 __fastcall func1(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

original_code:
short spack(unsigned char *f)
{
 short i;
 i = *f;
 i = i << 8;
 i |= *(f+1);
 return (i);
}

variable_types: i short

return_type: short


AssembleETIFrame

decompiled_code:
__int64 __fastcall AssembleETIFrame(__int64 a1, unsigned int a2, void (__fastcall *a3)(__int64, char *, __int64), __int64 a4)
{
bool v4; // zf
unsigned __int8 v6; // cl
__int16 v7; // di
__int64 result; // rax
unsigned __int16 v10; // ax
char v11; // dl
int v12; // eax
int v13; // er14
__int16 v14; // cx
unsigned __int16 *v15; // rax
unsigned __int16 v16; // dx
char *v17; // rsi
char *v18; // rax
__int16 v19; // bx
char v20; // dl
unsigned __int16 v21; // cx
char v22; // r9
__int16 v23; // dx
__int64 v24; // rsi
unsigned int v25; // eax
__int64 v26; // rdx
char *v27; // r15
int v28; // edi
int v29; // ebx
size_t v30; // rdx
__int64 v31; // r13
__int64 v32; // r14
size_t v33; // rsi
__int64 v34; // rsi
__int16 v35; // ax
__int16 v36; // dx
int v37; // eax
__int64 v38; // rdx
__int64 v39; // rcx
char *v40; // rdx
unsigned int v42; // [rsp+8h] [rbp-1840h]
int v43; // [rsp+Ch] [rbp-183Ch]
char v44; // [rsp+10h] [rbp-1838h] BYREF
__int16 v45; // [rsp+11h] [rbp-1837h]
char v46; // [rsp+13h] [rbp-1835h]
char v47[2]; // [rsp+14h] [rbp-1834h] BYREF
__int16 v48; // [rsp+16h] [rbp-1832h]
char v49[6192]; // [rsp+18h] [rbp-1830h] BYREF

v4 = *(_BYTE *)a1 == 0;
v42 = a2;
if ( v4 )
{
msg_Log("ETI: Cannot assemble ETI before protocol");
return 0LL;
}
if ( !*(_BYTE *)(a1 + 1689) )
{
msg_Log("ETI: Cannot assemble ETI without FC");
return 0LL;
}
v6 = *(_BYTE *)(a1 + 16);
if ( !v6 )
{
msg_Log("ETI: Cannot assemble ETI without FIC data");
return 0LL;
}
v7 = *(unsigned __int8 *)(a1 + 1704);
if ( (_BYTE)v7 == 3 )
{
if ( v6 != 0x80 )
goto LABEL_6;
}
else if ( v6 != 96 )
{
LABEL_6:
msg_Log("ETI: Invalid FIC length %u for MID %u", v6, (unsigned __int8)v7);
return 0LL;
}
v10 = *(_WORD *)(a1 + 1702);
v44 = -1;
if ( (v10 & 1) != 0 )
{
v46 = 73;
v45 = -14856;
}
else
{
v46 = -74;
v45 = 14855;
}
v11 = *(_BYTE *)(a1 + 1700);
v47[0] = v10 % 0xFAu;
v12 = *(unsigned __int8 *)(a1 + 1701);
v13 = v12;
v14 = (v6 >> 2) + v12 + 1;
v47[1] = v12 | (v11 << 7);
if ( v12 )
{
v15 = (unsigned __int16 *)(a1 + 168);
do
{
v16 = *v15;
v15 += 12;
v14 += v16 >> 2;
}
while ( (unsigned __int16 *)(a1 + 24LL * (unsigned int)(v13 - 1) + 192) != v15 );
}
v48 = __ROL2__(v14 | (*(unsigned __int8 *)(a1 + 1705) << 13) | (v7 << 11), 8);
if ( v13 )
{
v17 = (char *)(a1 + 153);
v18 = v49;
do
{
v19 = *(_WORD *)(v17 + 1);
v20 = *v17;
v18 += 4;
v17 += 24;
*(v18 - 3) = v19;
v21 = *(_WORD *)(v17 - 9);
*(v18 - 4) = (4 * v20) | HIBYTE(v19) & 3;
v21 >>= 3;
v22 = 4 * *(v17 - 21);
*(v18 - 1) = v21;
*(v18 - 2) = v22 | HIBYTE(v21) & 3;
}
while ( &v49[4 * (v13 - 1) + 4] != v18 );
}
v23 = *(_WORD *)(a1 + 2);
*(&v44 + 4 * v13 + 8) = HIBYTE(v23);
*(&v44 + 4 * v13 + 9) = v23;
v24 = a1 + 17;
v25 = ~(unsigned int)crc16(0xFFFFLL, v47, (unsigned int)(4 * v13 + 6));
*(&v44 + 4 * v13 + 10) = BYTE1(v25);
*(&v44 + 4 * v13 + 11) = v25;
v26 = *(unsigned __int8 *)(a1 + 16);
v43 = 4 * v13 + 12;
v27 = &v44 + v43;
if ( (unsigned int)v26 >= 8 )
{
*(_QWORD *)v27 = *(_QWORD *)(a1 + 17);
*(_QWORD *)&v27[v26 - 8] = *(_QWORD *)(v24 + v26 - 8);
qmemcpy(
(void *)((unsigned __int64)(v27 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v24 - (_QWORD)&v27[-((unsigned __int64)(v27 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v26 + (_DWORD)v27 - (((_DWORD)v27 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v26 & 4) != 0 )
{
*(_DWORD *)v27 = *(_DWORD *)(a1 + 17);
*(_DWORD *)&v27[v26 - 4] = *(_DWORD *)(v24 + v26 - 4);
}
else if ( (_DWORD)v26 )
{
*v27 = *(_BYTE *)(a1 + 17);
if ( (v26 & 2) != 0 )
*(_WORD *)&v27[v26 - 2] = *(_WORD *)(v24 + v26 - 2);
}
v28 = v43 + (unsigned __int8)v26;
v29 = v28;
if ( !v13 )
goto LABEL_28;
v29 = *(unsigned __int16 *)(a1 + 168) + v28;
v30 = *(unsigned __int16 *)(a1 + 168);
if ( v29 <= 6135 )
{
v31 = a1 + 160;
v32 = a1 + 160 + 24LL * (unsigned int)(v13 - 1);
while ( 1 )
{
memcpy(&v44 + v28, *(const void **)v31, v30);
if ( v32 == v31 )
break;
v33 = *(unsigned __int16 *)(v31 + 32);
v28 = v29;
v31 += 24LL;
v30 = v33;
v34 = (unsigned int)(v29 + v33);
if ( (int)v34 > 6135 )
goto LABEL_36;
v29 = v34;
}
LABEL_28:
v35 = ~(unsigned __int16)crc16(0xFFFFLL, v27, (unsigned int)(v29 - v43));
*(&v44 + v29) = HIBYTE(v35);
*(&v44 + v29 + 1) = v35;
v36 = *(_WORD *)(a1 + 146);
*(&v44 + v29 + 2) = HIBYTE(v36);
*(&v44 + v29 + 3) = v36;
v37 = *(_DWORD *)(a1 + 1696);
*(&v44 + v29 + 4) = HIBYTE(v37);
v4 = v42 == 2;
*(&v44 + v29 + 5) = BYTE2(v37);
*(&v44 + v29 + 6) = BYTE1(v37);
*(&v44 + v29 + 7) = v37;
v38 = (unsigned int)(v29 + 8);
if ( v4 )
{
v39 = (unsigned int)(6144 - v38);
v40 = &v44 + (int)v38;
if ( (unsigned int)v39 >= 8 )
{
*(_QWORD *)v40 = 0x5555555555555555LL;
*(_QWORD *)&v40[(unsigned int)v39 - 8] = 0x5555555555555555LL;
memset(
(void *)((unsigned __int64)(v40 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0x55u,
8LL * (((unsigned int)v40 - (((_DWORD)v40 + 8) & 0xFFFFFFF8) + (unsigned int)v39) >> 3));
}
else if ( (v39 & 4) != 0 )
{
*(_DWORD *)v40 = 1431655765;
*(_DWORD *)&v40[v39 - 4] = 1431655765;
}
else if ( (_DWORD)v39 )
{
*v40 = 85;
if ( (v39 & 2) != 0 )
*(_WORD *)&v40[v39 - 2] = 21845;
}
v38 = 6144LL;
}
result = 1LL;
if ( a3 )
{
v42 = 1;
a3(a4, &v44, v38);
result = v42;
}
return result;
}
v34 = (unsigned int)v29;
LABEL_36:
msg_Log("ETI: Invalid ETI length %u", v34);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, void (__fastcall *a3)(__int64, char *, __int64), __int64 a4)
{
bool v4; // zf
unsigned __int8 v6; // cl
__int16 v7; // di
__int64 result; // rax
unsigned __int16 v10; // ax
char v11; // dl
int v12; // eax
int v13; // er14
__int16 v14; // cx
unsigned __int16 *v15; // rax
unsigned __int16 v16; // dx
char *v17; // rsi
char *v18; // rax
__int16 v19; // bx
char v20; // dl
unsigned __int16 v21; // cx
char v22; // r9
__int16 v23; // dx
__int64 v24; // rsi
unsigned int v25; // eax
__int64 v26; // rdx
char *v27; // r15
int v28; // edi
int v29; // ebx
size_t v30; // rdx
__int64 v31; // r13
__int64 v32; // r14
size_t v33; // rsi
__int64 v34; // rsi
__int16 v35; // ax
__int16 v36; // dx
int v37; // eax
__int64 v38; // rdx
__int64 v39; // rcx
char *v40; // rdx
unsigned int v42; // [rsp+8h] [rbp-1840h]
int v43; // [rsp+Ch] [rbp-183Ch]
char v44; // [rsp+10h] [rbp-1838h] BYREF
__int16 v45; // [rsp+11h] [rbp-1837h]
char v46; // [rsp+13h] [rbp-1835h]
char v47[2]; // [rsp+14h] [rbp-1834h] BYREF
__int16 v48; // [rsp+16h] [rbp-1832h]
char v49[6192]; // [rsp+18h] [rbp-1830h] BYREF

v4 = *(_BYTE *)a1 == 0;
v42 = a2;
if ( v4 )
{
msg_Log("ETI: Cannot assemble ETI before protocol");
return 0LL;
}
if ( !*(_BYTE *)(a1 + 1689) )
{
msg_Log("ETI: Cannot assemble ETI without FC");
return 0LL;
}
v6 = *(_BYTE *)(a1 + 16);
if ( !v6 )
{
msg_Log("ETI: Cannot assemble ETI without FIC data");
return 0LL;
}
v7 = *(unsigned __int8 *)(a1 + 1704);
if ( (_BYTE)v7 == 3 )
{
if ( v6 != 0x80 )
goto LABEL_6;
}
else if ( v6 != 96 )
{
LABEL_6:
msg_Log("ETI: Invalid FIC length %u for MID %u", v6, (unsigned __int8)v7);
return 0LL;
}
v10 = *(_WORD *)(a1 + 1702);
v44 = -1;
if ( (v10 & 1) != 0 )
{
v46 = 73;
v45 = -14856;
}
else
{
v46 = -74;
v45 = 14855;
}
v11 = *(_BYTE *)(a1 + 1700);
v47[0] = v10 % 0xFAu;
v12 = *(unsigned __int8 *)(a1 + 1701);
v13 = v12;
v14 = (v6 >> 2) + v12 + 1;
v47[1] = v12 | (v11 << 7);
if ( v12 )
{
v15 = (unsigned __int16 *)(a1 + 168);
do
{
v16 = *v15;
v15 += 12;
v14 += v16 >> 2;
}
while ( (unsigned __int16 *)(a1 + 24LL * (unsigned int)(v13 - 1) + 192) != v15 );
}
v48 = __ROL2__(v14 | (*(unsigned __int8 *)(a1 + 1705) << 13) | (v7 << 11), 8);
if ( v13 )
{
v17 = (char *)(a1 + 153);
v18 = v49;
do
{
v19 = *(_WORD *)(v17 + 1);
v20 = *v17;
v18 += 4;
v17 += 24;
*(v18 - 3) = v19;
v21 = *(_WORD *)(v17 - 9);
*(v18 - 4) = (4 * v20) | HIBYTE(v19) & 3;
v21 >>= 3;
v22 = 4 * *(v17 - 21);
*(v18 - 1) = v21;
*(v18 - 2) = v22 | HIBYTE(v21) & 3;
}
while ( &v49[4 * (v13 - 1) + 4] != v18 );
}
v23 = *(_WORD *)(a1 + 2);
*(&v44 + 4 * v13 + 8) = HIBYTE(v23);
*(&v44 + 4 * v13 + 9) = v23;
v24 = a1 + 17;
v25 = ~(unsigned int)crc16(0xFFFFLL, v47, (unsigned int)(4 * v13 + 6));
*(&v44 + 4 * v13 + 10) = BYTE1(v25);
*(&v44 + 4 * v13 + 11) = v25;
v26 = *(unsigned __int8 *)(a1 + 16);
v43 = 4 * v13 + 12;
v27 = &v44 + v43;
if ( (unsigned int)v26 >= 8 )
{
*(_QWORD *)v27 = *(_QWORD *)(a1 + 17);
*(_QWORD *)&v27[v26 - 8] = *(_QWORD *)(v24 + v26 - 8);
qmemcpy(
(void *)((unsigned __int64)(v27 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v24 - (_QWORD)&v27[-((unsigned __int64)(v27 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v26 + (_DWORD)v27 - (((_DWORD)v27 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v26 & 4) != 0 )
{
*(_DWORD *)v27 = *(_DWORD *)(a1 + 17);
*(_DWORD *)&v27[v26 - 4] = *(_DWORD *)(v24 + v26 - 4);
}
else if ( (_DWORD)v26 )
{
*v27 = *(_BYTE *)(a1 + 17);
if ( (v26 & 2) != 0 )
*(_WORD *)&v27[v26 - 2] = *(_WORD *)(v24 + v26 - 2);
}
v28 = v43 + (unsigned __int8)v26;
v29 = v28;
if ( !v13 )
goto LABEL_28;
v29 = *(unsigned __int16 *)(a1 + 168) + v28;
v30 = *(unsigned __int16 *)(a1 + 168);
if ( v29 <= 6135 )
{
v31 = a1 + 160;
v32 = a1 + 160 + 24LL * (unsigned int)(v13 - 1);
while ( 1 )
{
memcpy(&v44 + v28, *(const void **)v31, v30);
if ( v32 == v31 )
break;
v33 = *(unsigned __int16 *)(v31 + 32);
v28 = v29;
v31 += 24LL;
v30 = v33;
v34 = (unsigned int)(v29 + v33);
if ( (int)v34 > 6135 )
goto LABEL_36;
v29 = v34;
}
LABEL_28:
v35 = ~(unsigned __int16)crc16(0xFFFFLL, v27, (unsigned int)(v29 - v43));
*(&v44 + v29) = HIBYTE(v35);
*(&v44 + v29 + 1) = v35;
v36 = *(_WORD *)(a1 + 146);
*(&v44 + v29 + 2) = HIBYTE(v36);
*(&v44 + v29 + 3) = v36;
v37 = *(_DWORD *)(a1 + 1696);
*(&v44 + v29 + 4) = HIBYTE(v37);
v4 = v42 == 2;
*(&v44 + v29 + 5) = BYTE2(v37);
*(&v44 + v29 + 6) = BYTE1(v37);
*(&v44 + v29 + 7) = v37;
v38 = (unsigned int)(v29 + 8);
if ( v4 )
{
v39 = (unsigned int)(6144 - v38);
v40 = &v44 + (int)v38;
if ( (unsigned int)v39 >= 8 )
{
*(_QWORD *)v40 = 0x5555555555555555LL;
*(_QWORD *)&v40[(unsigned int)v39 - 8] = 0x5555555555555555LL;
memset(
(void *)((unsigned __int64)(v40 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0x55u,
8LL * (((unsigned int)v40 - (((_DWORD)v40 + 8) & 0xFFFFFFF8) + (unsigned int)v39) >> 3));
}
else if ( (v39 & 4) != 0 )
{
*(_DWORD *)v40 = 1431655765;
*(_DWORD *)&v40[v39 - 4] = 1431655765;
}
else if ( (_DWORD)v39 )
{
*v40 = 85;
if ( (v39 & 2) != 0 )
*(_WORD *)&v40[v39 - 2] = 21845;
}
v38 = 6144LL;
}
result = 1LL;
if ( a3 )
{
v42 = 1;
a3(a4, &v44, v38);
result = v42;
}
return result;
}
v34 = (unsigned int)v29;
LABEL_36:
msg_Log("ETI: Invalid ETI length %u", v34);
return 0LL;
}

original_code:
int AssembleETIFrame(struct etiBuilder *h, int fmt, callback_t outCallback, void *priv_data)
{
    uint8_t eti[6144];
    int idx=0;
 int i;
    if (!h->is_eti) {
     msg_Log("ETI: Cannot assemble ETI before protocol");
     return 0;
    }
    if (!h->m_fc_valid) {
     msg_Log("ETI: Cannot assemble ETI without FC");
     return 0;
    }
    if (!h->fic_length) {
     msg_Log("ETI: Cannot assemble ETI without FIC data");
     return 0;
    }
    if ( (h->m_fc.mid == 3 && h->fic_length != 32 * 4) ||
            (h->m_fc.mid != 3 && h->fic_length != 24 * 4) ) {
        msg_Log("ETI: Invalid FIC length %u for MID %u",h->fic_length, h->m_fc.mid);
        return 0;
    }
    eti[0] = 0xff;
    uint8_t fct = h->m_fc.dflc % 250;
    if (fct % 2 == 1) {
     eti[1] = 0xf8;
     eti[2] = 0xc5;
     eti[3] = 0x49;
    } else {
     eti[1] = 0x07;
     eti[2] = 0x3a;
     eti[3] = 0xb6;
    }
    eti[4] = fct;
    const uint8_t NST = h->m_fc.nst;
    eti[5] = ((uint8_t)h->m_fc.ficf) << 7 | NST;
    uint16_t FL = NST + 1 + h->fic_length/4;
    for (i=0;i<NST;i++) {
        FL += h->m_stc[i].mst_size/4;
    }
    const uint16_t fp_mid_fl = (h->m_fc.fp << 13) | (h->m_fc.mid << 11) | FL;
    eti[6] = (uint16_t) (fp_mid_fl >> 8);
    eti[7] = (fp_mid_fl & 0xFF);
    for (i=0;i<NST;i++) {
     eti[8 + i*4] = (h->m_stc[i].scid << 2) | (uint16_t)((h->m_stc[i].sad & 0x300) >> 8);
        eti[8 + i*4 + 1] = h->m_stc[i].sad & 0xff;
        eti[8 + i*4 + 2] = (h->m_stc[i].tpl << 2) | (uint16_t)(((h->m_stc[i].mst_size/8) & 0x300) >> 8);
        eti[8 + i*4 + 3] = (uint16_t)(h->m_stc[i].mst_size/8) & 0xff;
    }
    idx+=8 + NST*4;
    eti[idx] = (((uint16_t)h->m_mnsc) >> 8);
    eti[idx+1] = (h->m_mnsc & 0xFF);
    uint16_t eti_crc = 0xFFFF;
    eti_crc = crc16(eti_crc, &eti[4], idx - 4 + 2);
    eti_crc ^= 0xffff;
    eti[idx+2] = (uint16_t)eti_crc >> 8;
    eti[idx+3] = eti_crc & 0xFF;
    idx+=4;
    const size_t mst_start = idx;
    memcpy(eti+idx, h->fic, h->fic_length);
    idx+=h->fic_length;
    for (i=0;i<NST;i++) {
     if(idx+h->m_stc[i].mst_size >= 6144-8) {
      msg_Log("ETI: Invalid ETI length %u",idx+h->m_stc[i].mst_size);
      return 0;
     }
     memcpy(eti+idx, h->m_stc[i].mst, h->m_stc[i].mst_size);
     idx+=h->m_stc[i].mst_size;
    }
    uint16_t mst_crc = 0xFFFF;
    mst_crc = crc16(mst_crc, &eti[mst_start], idx - mst_start);
    mst_crc ^= 0xffff;
    eti[idx] = (uint16_t)(mst_crc >> 8);
    eti[idx+1] = (mst_crc & 0xFF);
    eti[idx+2] = (uint16_t)(h->m_rfu >> 8);
    eti[idx+3] = (h->m_rfu & 0xFF);
    eti[idx+4] = (uint32_t)(h->m_fc.tsta >> 24);
    eti[idx+5] = ((uint32_t)(h->m_fc.tsta >> 16) & 0xFF);
    eti[idx+6] = ((uint32_t)(h->m_fc.tsta >> 8) & 0xFF);
    eti[idx+7] = (h->m_fc.tsta & 0xFF);
    idx+=8;
    if(idx>6144) {
     msg_Log("ETI: Invalid ETI length %u",idx);
     return 0;
    }
    if(fmt == ETI_FMT_RAW && idx<6144) {
     memset(eti+idx, 0x55, 6144-idx);
     idx=6144;
    }
    if(outCallback)
     outCallback(priv_data, eti, idx);
 return 1;
}

canonical_original_code:
int AssembleETIFrame(struct etiBuilder *h, int fmt, void (*outCallback)(void *privData, void *etiData, int etiLen), void *priv_data)
{
    unsigned char eti[6144];
    int idx = 0;
 int i;
    if (!h->is_eti) {
     msg_Log("ETI: Cannot assemble ETI before protocol");
     return 0;
    }
    if (!h->m_fc_valid) {
     msg_Log("ETI: Cannot assemble ETI without FC");
     return 0;
    }
    if (!h->fic_length) {
     msg_Log("ETI: Cannot assemble ETI without FIC data");
     return 0;
    }
    if ( (h->m_fc.mid == 3 && h->fic_length != 32 * 4) ||
            (h->m_fc.mid != 3 && h->fic_length != 24 * 4) ) {
        msg_Log("ETI: Invalid FIC length %u for MID %u",h->fic_length, h->m_fc.mid);
        return 0;
    }
    eti[0] = 0xff;
    unsigned char fct = h->m_fc.dflc % 250;
    if (fct % 2 == 1) {
     eti[1] = 0xf8;
     eti[2] = 0xc5;
     eti[3] = 0x49;
    } else {
     eti[1] = 0x07;
     eti[2] = 0x3a;
     eti[3] = 0xb6;
    }
    eti[4] = fct;
    unsigned char NST = h->m_fc.nst;
    eti[5] = ((unsigned char)h->m_fc.ficf) << 7 | NST;
    unsigned short FL = NST + 1 + h->fic_length/4;
    for (i=0;i<NST;i++) {
        FL += h->m_stc[i].mst_size/4;
    }
    unsigned short fp_mid_fl = (h->m_fc.fp << 13) | (h->m_fc.mid << 11) | FL;
    eti[6] = (unsigned short) (fp_mid_fl >> 8);
    eti[7] = (fp_mid_fl & 0xFF);
    for (i=0;i<NST;i++) {
     eti[8 + i*4] = (h->m_stc[i].scid << 2) | (unsigned short)((h->m_stc[i].sad & 0x300) >> 8);
        eti[8 + i*4 + 1] = h->m_stc[i].sad & 0xff;
        eti[8 + i*4 + 2] = (h->m_stc[i].tpl << 2) | (unsigned short)(((h->m_stc[i].mst_size/8) & 0x300) >> 8);
        eti[8 + i*4 + 3] = (unsigned short)(h->m_stc[i].mst_size/8) & 0xff;
    }
    idx+=8 + NST*4;
    eti[idx] = (((unsigned short)h->m_mnsc) >> 8);
    eti[idx+1] = (h->m_mnsc & 0xFF);
    unsigned short eti_crc = 0xFFFF;
    eti_crc = crc16(eti_crc, &eti[4], idx - 4 + 2);
    eti_crc ^= 0xffff;
    eti[idx+2] = (unsigned short)eti_crc >> 8;
    eti[idx+3] = eti_crc & 0xFF;
    idx+=4;
    unsigned long mst_start = idx;
    memcpy(eti+idx, h->fic, h->fic_length);
    idx+=h->fic_length;
    for (i=0;i<NST;i++) {
     if(idx+h->m_stc[i].mst_size >= 6144-8) {
      msg_Log("ETI: Invalid ETI length %u",idx+h->m_stc[i].mst_size);
      return 0;
     }
     memcpy(eti+idx, h->m_stc[i].mst, h->m_stc[i].mst_size);
     idx+=h->m_stc[i].mst_size;
    }
    unsigned short mst_crc = 0xFFFF;
    mst_crc = crc16(mst_crc, &eti[mst_start], idx - mst_start);
    mst_crc ^= 0xffff;
    eti[idx] = (unsigned short)(mst_crc >> 8);
    eti[idx+1] = (mst_crc & 0xFF);
    eti[idx+2] = (unsigned short)(h->m_rfu >> 8);
    eti[idx+3] = (h->m_rfu & 0xFF);
    eti[idx+4] = (unsigned int)(h->m_fc.tsta >> 24);
    eti[idx+5] = ((unsigned int)(h->m_fc.tsta >> 16) & 0xFF);
    eti[idx+6] = ((unsigned int)(h->m_fc.tsta >> 8) & 0xFF);
    eti[idx+7] = (h->m_fc.tsta & 0xFF);
    idx+=8;
    if(idx>6144) {
     msg_Log("ETI: Invalid ETI length %u",idx);
     return 0;
    }
    if(fmt == ETI_FMT_RAW && idx<6144) {
     memset(eti+idx, 0x55, 6144-idx);
     idx=6144;
    }
    if(outCallback)
     outCallback(priv_data, eti, idx);
 return 1;
}

variable_types: struct etiBuilder { _Bool is_eti; unsigned char m_err; unsigned short m_mnsc; _Bool m_time_valid; unsigned int m_utco; unsigned int m_seconds; unsigned char fic_length; unsigned char fic[128]; unsigned short m_rfu; struct eti_stc_data { unsigned char stream_index; unsigned char scid; unsigned short sad; unsigned char tpl; unsigned char *mst; unsigned short mst_size; } m_stc[64]; unsigned char NST; _Bool m_fc_valid; struct eti_fc_data { _Bool atstf; unsigned int tsta; _Bool ficf; unsigned char nst; unsigned short dflc; unsigned char mid; unsigned char fp; } m_fc; } *

variable_types: fmt int

variable_types: void (*)(void *privData, void *etiData, int etiLen)

variable_types:
eti
unsigned char [6144]

variable_types: idx int

variable_types:
fct
unsigned char

variable_types:
NST
unsigned char

variable_types:
FL
unsigned short