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

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

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

variable_types: figlen int

variable_types:
fig
unsigned char *


repo: piratfm/eti-tools

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: 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 *))*(&fig_1_jtab + (*a2 & 7)))(
(unsigned int)(a1 - 1),
(*a2 & 8) != 0,
*a2 >> 4,
a2 + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, _BYTE *a2)
{
((void (__fastcall *)(_QWORD, bool, _QWORD, _BYTE *))*(&fig_1_jtab + (*a2 & 7)))(
(unsigned int)(a1 - 1),
(*a2 & 8) != 0,
*a2 >> 4,
a2 + 1);
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 func3()
{
return 0LL;
}

original_code:
int fig_5(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


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

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


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

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


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 func9(__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


decompiled_code:
__int64 __fastcall fig_1_0(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
*(__m128i *)einf = _mm_loadu_si128((const __m128i *)(a4 + 2));
LOBYTE(einf[8]) = 0;
einf[9] = *(_WORD *)a4;
sswab((unsigned int)((_DWORD)einf + 18));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
*(__m128i *)einf = _mm_loadu_si128((const __m128i *)(a4 + 2));
LOBYTE(einf[8]) = 0;
einf[9] = *(_WORD *)a4;
sswab((unsigned int)((_DWORD)einf + 18));
return 0LL;
}

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)
{
char *v5; // rax
unsigned __int16 v7; // [rsp+0h] [rbp-28h] BYREF
char dest[18]; // [rsp+2h] [rbp-26h] BYREF
__int16 v9[10]; // [rsp+14h] [rbp-14h] BYREF

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

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

v7 = *(_WORD *)a4;
sswab(&v7);
strncpy(dest, (const char *)(a4 + 2), 0x10uLL);
dest[16] = 0;
v9[0] = *(_WORD *)(a4 + 18);
sswab(v9);
v5 = (char *)find_service(einf, v7);
if ( v5 )
strncpy(v5, 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; } *


unpickfig

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

v2 = *a1 >> 5;
v3 = *a1 & 0x1F;
if ( v3 != 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, v2, v3);
}
((void (__fastcall *)(int, _BYTE *))*(&fig_jtab + ((*a1 >> 5) & 7)))(a2, a1 + 1);
return 0LL;
}

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

v2 = *a1 >> 5;
v3 = *a1 & 0x1F;
if ( v3 != 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, v2, v3);
}
((void (__fastcall *)(int, _BYTE *))*(&fig_jtab + ((*a1 >> 5) & 7)))(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:
fig
unsigned char *

variable_types: len int

variable_types: typ int

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


decompiled_code:
__int64 __fastcall read_16b(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

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

return_type: 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)
{
__int16 v2; // r12

v2 = read_16b((unsigned __int8 *)(a1 + a2 - 2));
return ~(unsigned __int16)crc16(0xFFFFLL, a1, (unsigned int)(a2 - 2)) == v2;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1, __int64 a2)
{
__int16 v2; // r12

v2 = func0((unsigned __int8 *)(a1 + a2 - 2));
return ~(unsigned __int16)crc16(0xFFFFLL, a1, (unsigned int)(a2 - 2)) == v2;
}

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 v5; // er14
unsigned __int16 v6; // r12
unsigned __int8 v7; // dl
unsigned __int8 v8; // cl
__int64 result; // rax
unsigned __int64 v10; // rsi

if ( a3 > 9 && *(_BYTE *)a2 == 65 && *(_BYTE *)(a2 + 1) == 70 )
{
v5 = read_32b((unsigned int *)(a2 + 2));
v6 = read_16b((unsigned __int8 *)(a2 + 6));
if ( *(unsigned __int16 *)(a1 + 36) + 1 != v6 )
msg_Log("EDI-AF: Packet sequence error");
*(_WORD *)(a1 + 36) = v6;
v7 = *(_BYTE *)(a2 + 8);
v8 = v7 & 0xF;
if ( ((v7 >> 4) & 7) != 1 || v8 )
{
msg_Log("EDI-AF: Packet has wrong revision %u.%u", (v7 >> 4) & 7, v8);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
if ( *(_BYTE *)(a2 + 9) == 84 )
{
v10 = v5 + 10 + 2LL;
if ( a3 < v10 )
{
msg_Log("EDI-AF: packet too small: %u < %u", a3, v5 + 10 + 2LL);
result = 0LL;
}
else if ( (v7 & 0x80u) == 0 )
{
msg_Log("EDI-AF: packet not supported, has no CRC");
result = 0xFFFFFFFFLL;
}
else if ( checkCRC(a2, v10) )
{
result = decode_tagpacket(a1, a2 + 10, v5);
}
else
{
msg_Log("EDI-AF: Packet crc wrong pos:%u max:%u", v5 + 10, a3);
result = 0xFFFFFFFFLL;
}
}
}
}
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 v5; // er14
unsigned __int16 v6; // r12
unsigned __int8 v7; // dl
unsigned __int8 v8; // cl
__int64 result; // rax
unsigned __int64 v10; // rsi

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

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));
v2 = (char *)ptr + 592;
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

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));
v2 = (char *)ptr + 592;
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 v4; // bl
const unsigned __int16 *v5; // rax
__int64 v6; // rdx
__int64 result; // rax
int v8; // ebx
int v9; // edx
signed int v10; // eax

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

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, unsigned __int8 *a2, unsigned __int64 a3)
{
unsigned __int8 v4; // bl
const unsigned __int16 *v5; // rax
__int64 v6; // rdx
__int64 result; // rax
int v8; // ebx
int v9; // edx
signed int v10; // eax

if ( a3 <= 1 )
{
msg_Log("EDI: packet too small!");
return 0xFFFFFFFFLL;
}
v4 = *a2;
if ( *a2 != 80 )
{
if ( v4 == 65 && a2[1] == 70 )
{
v9 = func3((__int64)a1, (__int64)a2, a3);
result = 0LL;
if ( v9 > 0 )
{
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
result = 0LL;
}
return result;
}
goto LABEL_4;
}
if ( a2[1] != 70 )
{
LABEL_4:
v5 = *_ctype_b_loc();
LOBYTE(v6) = a2[1];
if ( (v5[(unsigned __int8)v6] & 0x4000) == 0 )
LOBYTE(v6) = 46;
v6 = (unsigned __int8)v6;
if ( (v5[v4] & 0x4000) == 0 )
v4 = 46;
msg_Log("EDI: packet is unknown: %c%c", v4, v6);
return 0LL;
}
v8 = 0;
do
{
v8 = HandlePFPacket(a1, &a2[v8], a3 - v8);
if ( v8 <= 0 )
return 0LL;
if ( *((_BYTE *)a1 + 24) )
{
v10 = pushPFTFrag((char *)a1 + 6, a1 + 10);
if ( v10 > 0 && (int)func3((__int64)a1, *((_QWORD *)a1 + 52), v10) > 0 )
AssembleETIFrame(a1 + 108, *a1, *((_QWORD *)a1 + 268), *((_QWORD *)a1 + 269));
}
}
while ( v8 < a3 );
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; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr inp[2]; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
*(_QWORD *)&inp[0].sa_family = 0LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 >= 0 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) < 0 )
goto LABEL_6;
inp[0].sa_family = 2;
*(_DWORD *)&inp[0].sa_data[2] = 0;
if ( cp )
inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]);
*(_WORD *)inp[0].sa_data = _bswap_16(a2);
if ( bind(v3, inp, 0x10u) )
{
LABEL_6:
close(v3);
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *cp, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr inp[2]; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
*(_QWORD *)&inp[0].sa_family = 0LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 >= 0 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) < 0 )
goto LABEL_6;
inp[0].sa_family = 2;
*(_DWORD *)&inp[0].sa_data[2] = 0;
if ( cp )
inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]);
*(_WORD *)inp[0].sa_data = func1(a2);
if ( bind(v3, inp, 0x10u) )
{
LABEL_6:
close(v3);
v3 = -1;
}
}
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 func3(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)
{
int v2; // eax
unsigned int v3; // edx
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;
v2 = _bswap_32(inp[0].s_addr);
v3 = 0;
if ( (v2 & 0xF0000000) == -536870912 )
{
inp[1].s_addr = 0;
v3 = setsockopt(fd, 0, 35, inp, 8u);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, char *cp)
{
int v2; // eax
unsigned int v3; // edx
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;
v2 = func4(inp[0].s_addr);
v3 = 0;
if ( (v2 & 0xF0000000) == -536870912 )
{
inp[1].s_addr = 0;
v3 = setsockopt(fd, 0, 35, inp, 8u);
}
return v3;
}

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 sockaddr inp[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = _bswap_16(a3);
if ( cp )
inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]);
return connect(fd, inp, 0x10u);
}

canonical_decompiled_code:
int __fastcall func6(int fd, char *cp, __int16 a3)
{
struct sockaddr inp[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = func1(a3);
if ( cp )
inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]);
return connect(fd, 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; // ebx

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

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

v2 = func2(cp, a2);
v3 = v2;
if ( v2 >= 0 && (func3(v2) < 0 || (int)func5(v3, cp) < 0) )
{
close(v3);
v3 = -1;
}
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; // ebx

v4 = socket_open(0LL, 0);
v5 = v4;
if ( v4 >= 0 )
{
socket_set_nonblock(v4);
if ( socket_connect(v5, cp, a2) )
{
close(v5);
v5 = -1;
}
else
{
socket_join_multicast(v5, cp);
if ( a3 )
socket_set_ttl(v5, a3);
}
}
return v5;
}

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

v4 = func2(0LL, 0);
v5 = v4;
if ( v4 >= 0 )
{
func3(v4);
if ( func6(v5, cp, a2) )
{
close(v5);
v5 = -1;
}
else
{
func5(v5, cp);
if ( a3 )
func0(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;
fds.events = 1;
fds.revents = 0;
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;
fds.events = 1;
fds.revents = 0;
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)
{
__int64 result; // rax

result = HIBYTE(a2) & 0x1F | *(_BYTE *)(a1 + 1) & 0xE0u;
*(_BYTE *)(a1 + 1) = HIBYTE(a2) & 0x1F | *(_BYTE *)(a1 + 1) & 0xE0;
*(_BYTE *)(a1 + 2) = a2;
return result;
}

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

result = HIBYTE(a2) & 0x1F | *(_BYTE *)(a1 + 1) & 0xE0u;
*(_BYTE *)(a1 + 1) = HIBYTE(a2) & 0x1F | *(_BYTE *)(a1 + 1) & 0xE0;
*(_BYTE *)(a1 + 2) = a2;
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
char s[1024]; // [rsp+0h] [rbp-4D8h] BYREF
time_t timer; // [rsp+400h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+408h] [rbp-D0h] BYREF

va_start(arg, a1);
time(&timer);
v1 = localtime(&timer);
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
char s[1024]; // [rsp+0h] [rbp-4D8h] BYREF
time_t timer; // [rsp+400h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+408h] [rbp-D0h] BYREF

va_start(arg, a1);
time(&timer);
v1 = localtime(&timer);
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; // ebx
const unsigned __int16 **v4; // r13
__int64 v5; // rbx
char v6; // di
int j; // ebx
int v8; // er15
int v9; // ebp
__int64 v10; // rbx
int i; // [rsp+0h] [rbp-48h]

result = fprintf(stderr, "dump %d bytes:\n", (unsigned int)a2);
if ( a2 > 0 )
{
for ( i = 0; i < a2; result = i )
{
v8 = a2 - i;
v9 = 32;
if ( a2 - i <= 32 )
v9 = a2 - i;
fwrite("\t\t\t\t", 1uLL, 4uLL, stderr);
if ( v8 <= 0 )
{
v3 = 0;
}
else
{
v10 = 0LL;
do
fprintf(stderr, "%02x ", *(unsigned __int8 *)(a1 + i + v10++));
while ( v9 > (int)v10 );
v3 = 1;
if ( v9 > 0 )
v3 = v9;
if ( v8 > 31 )
{
fwrite("\t\"", 1uLL, 2uLL, stderr);
LABEL_6:
v4 = _ctype_b_loc();
v5 = 0LL;
do
{
v6 = *(_BYTE *)(a1 + i + v5);
if ( ((*v4)[v6] & 0x4000) == 0 )
v6 = 46;
fputc(v6, stderr);
++v5;
}
while ( v9 > (int)v5 );
j = 1;
if ( v9 > 0 )
j = v9;
if ( v8 > 31 )
goto LABEL_14;
goto LABEL_13;
}
}
do
{
fwrite(" ", 1uLL, 3uLL, stderr);
++v3;
}
while ( v3 <= 31 );
fwrite("\t\"", 1uLL, 2uLL, stderr);
if ( v8 > 0 )
goto LABEL_6;
for ( j = 0; j <= 31; ++j )
LABEL_13:
fputc(32, stderr);
LABEL_14:
fwrite("\"\n", 1uLL, 2uLL, stderr);
i += v9;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, int a2)
{
int result; // eax
int v3; // ebx
const unsigned __int16 **v4; // r13
__int64 v5; // rbx
char v6; // di
int j; // ebx
int v8; // er15
int v9; // ebp
__int64 v10; // rbx
int i; // [rsp+0h] [rbp-48h]

result = fprintf(stderr, "dump %d bytes:\n", (unsigned int)a2);
if ( a2 > 0 )
{
for ( i = 0; i < a2; result = i )
{
v8 = a2 - i;
v9 = 32;
if ( a2 - i <= 32 )
v9 = a2 - i;
fwrite("\t\t\t\t", 1uLL, 4uLL, stderr);
if ( v8 <= 0 )
{
v3 = 0;
}
else
{
v10 = 0LL;
do
fprintf(stderr, "%02x ", *(unsigned __int8 *)(a1 + i + v10++));
while ( v9 > (int)v10 );
v3 = 1;
if ( v9 > 0 )
v3 = v9;
if ( v8 > 31 )
{
fwrite("\t\"", 1uLL, 2uLL, stderr);
LABEL_6:
v4 = _ctype_b_loc();
v5 = 0LL;
do
{
v6 = *(_BYTE *)(a1 + i + v5);
if ( ((*v4)[v6] & 0x4000) == 0 )
v6 = 46;
fputc(v6, stderr);
++v5;
}
while ( v9 > (int)v5 );
j = 1;
if ( v9 > 0 )
j = v9;
if ( v8 > 31 )
goto LABEL_14;
goto LABEL_13;
}
}
do
{
fwrite(" ", 1uLL, 3uLL, stderr);
++v3;
}
while ( v3 <= 31 );
fwrite("\t\"", 1uLL, 2uLL, stderr);
if ( v8 > 0 )
goto LABEL_6;
for ( j = 0; j <= 31; ++j )
LABEL_13:
fputc(32, stderr);
LABEL_14:
fwrite("\"\n", 1uLL, 2uLL, stderr);
i += v9;
}
}
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 )
return 0LL;
while ( *(_BYTE *)v1 )
{
v1 = *(_QWORD *)(v1 + 40);
if ( !v1 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rax

v1 = *(_QWORD *)(a1 + 32);
if ( !v1 )
return 0LL;
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; // rbx
unsigned int v2; // ebp
unsigned int v3; // er12
unsigned int *v4; // rax
_DWORD *v5; // rax
_DWORD *v6; // rax
__int64 v8[11]; // [rsp+0h] [rbp-58h]

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

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

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


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:
__int64 __fastcall spack(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

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

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


block

repo: piwai/qmail-helo-check-plugin

decompiled_code:
void __fastcall block(const char *a1, const char *a2, const char *a3)
{
printf("R553 sorry, %s (#5.7.1)\n", a3);
syslog(7, "ip=%s:helo=%s:block (%s)\n", a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func7(const char *a1, const char *a2, const char *a3)
{
printf("R553 sorry, %s (#5.7.1)\n", a3);
syslog(7, "ip=%s:helo=%s:block (%s)\n", a1, a2, a3);
}

original_code:
void block(const char *ip, const char *domain, const char *message) {
  printf("R553 sorry, %s (#5.7.1)\n", message);
  syslog(7, "ip=%s:helo=%s:block (%s)\n", ip, domain, message);
}

canonical_original_code:
void block(char *ip, char *domain, char *message) {
  printf("R553 sorry, %s (#5.7.1)\n", message);
  syslog(7, "ip=%s:helo=%s:block (%s)\n", ip, domain, message);
}

variable_types:
ip
char *

variable_types:
domain
char *

variable_types:
message
char *


decompiled_code:
void __fastcall add_header(const char *a1, const char *a2, const char *a3)
{
puts("HX-Spam-Flag: YES");
syslog(7, "ip=%s:helo=%s:allow,add_header (%s)\n", a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func8(const char *a1, const char *a2, const char *a3)
{
puts("HX-Spam-Flag: YES");
syslog(7, "ip=%s:helo=%s:allow,add_header (%s)\n", a1, a2, a3);
}

original_code:
void add_header(const char *ip, const char *domain, const char *message) {
  printf("HX-Spam-Flag: YES\n");
  syslog(7, "ip=%s:helo=%s:allow,add_header (%s)\n", ip, domain, message);
}

canonical_original_code:
void add_header(char *ip, char *domain, char *message) {
  printf("HX-Spam-Flag: YES\n");
  syslog(7, "ip=%s:helo=%s:allow,add_header (%s)\n", ip, domain, message);
}


allow

decompiled_code:
void __fastcall allow(const char *a1, const char *a2, const char *a3)
{
syslog(7, "ip=%s:helo=%s:allow (%s)\n", a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func9(const char *a1, const char *a2, const char *a3)
{
syslog(7, "ip=%s:helo=%s:allow (%s)\n", a1, a2, a3);
}

original_code:
void allow(const char *ip, const char *domain, const char *message) {
  syslog(7, "ip=%s:helo=%s:allow (%s)\n", ip, domain, message);
}

canonical_original_code:
void allow(char *ip, char *domain, char *message) {
  syslog(7, "ip=%s:helo=%s:allow (%s)\n", ip, domain, message);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
char *v4; // rbp
__int64 v5; // rcx
struct addrinfo *v6; // rdi
int v7; // eax
struct addrinfo *v8; // rbx
const char *v10; // rax
char v11[24]; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

v3 = getenv("SMTPHELOHOST");
v4 = getenv("TCPREMOTEIP");
openlog("qmail-helo", 1, 128);
if ( v3 )
{
if ( (unsigned int)check_whitelisted(v3) )
{
allow(v4, v3, "Whitelisted");
}
else
{
v5 = 12LL;
v6 = &req;
while ( v5 )
{
v6->ai_flags = 0;
v6 = (struct addrinfo *)((char *)v6 + 4);
--v5;
}
req.ai_family = 2;
req.ai_socktype = 1;
v7 = getaddrinfo(v3, 0LL, &req, &pai);
if ( v7 )
{
if ( v7 == -3 )
{
allow(v4, v3, "temporary DNS failure");
}
else
{
v10 = gai_strerror(v7);
block(v4, v3, v10);
}
}
else
{
v8 = pai;
if ( pai )
{
while ( 1 )
{
inet_ntop(v8->ai_family, &v8->ai_addr->sa_data[2], v11, 0x10u);
if ( !strncmp(v11, v4, 0x10uLL) )
break;
v8 = v8->ai_next;
if ( !v8 )
goto LABEL_10;
}
allow(v4, v3, "DNS record match");
}
else
{
LABEL_10:
add_header(v4, v3, "DNS mismatch");
}
}
}
}
else
{
block(v4, 0LL, "no HELO/EHLO hostname has been sent");
}
closelog();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
char *v4; // rbp
__int64 v5; // rcx
struct addrinfo *v6; // rdi
int v7; // eax
struct addrinfo *v8; // rbx
const char *v10; // rax
char v11[24]; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

v3 = getenv("SMTPHELOHOST");
v4 = getenv("TCPREMOTEIP");
openlog("qmail-helo", 1, 128);
if ( v3 )
{
if ( (unsigned int)func10(v3) )
{
func9(v4, v3, "Whitelisted");
}
else
{
v5 = 12LL;
v6 = &req;
while ( v5 )
{
v6->ai_flags = 0;
v6 = (struct addrinfo *)((char *)v6 + 4);
--v5;
}
req.ai_family = 2;
req.ai_socktype = 1;
v7 = getaddrinfo(v3, 0LL, &req, &pai);
if ( v7 )
{
if ( v7 == -3 )
{
func9(v4, v3, "temporary DNS failure");
}
else
{
v10 = gai_strerror(v7);
func7(v4, v3, v10);
}
}
else
{
v8 = pai;
if ( pai )
{
while ( 1 )
{
inet_ntop(v8->ai_family, &v8->ai_addr->sa_data[2], v11, 0x10u);
if ( !strncmp(v11, v4, 0x10uLL) )
break;
v8 = v8->ai_next;
if ( !v8 )
goto LABEL_10;
}
func9(v4, v3, "DNS record match");
}
else
{
LABEL_10:
func8(v4, v3, "DNS mismatch");
}
}
}
}
else
{
func7(v4, 0LL, "no HELO/EHLO hostname has been sent");
}
closelog();
return 0;
}

original_code:
int main(void) {
  char *helo_domain = getenv("SMTPHELOHOST");
  char *remote_ip = getenv("TCPREMOTEIP");
  struct addrinfo hints, *res, *result;
  char ipstr[16];
  int ret = 0;
  openlog("qmail-helo", 0x01, (16<<3));
  if (!helo_domain) {
    block(remote_ip, helo_domain, "no HELO/EHLO hostname has been sent");
    goto _end;
  }
  if (check_whitelisted(helo_domain)) {
    allow(remote_ip, helo_domain, "Whitelisted");
    goto _end;
  }
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(helo_domain, ((void *)0), &hints, &result);
  if (ret) {
    if (ret == -3) {
      allow(remote_ip, helo_domain, "temporary DNS failure");
    } else {
      block(remote_ip, helo_domain, gai_strerror(ret));
    }
    goto _end;
  }
  for (res = result; res != ((void *)0); res = res->ai_next) {
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
    void * addr = &(ipv4->sin_addr);
    inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
    if (!strncmp(ipstr, remote_ip, 16)) {
      allow(remote_ip, helo_domain, "DNS record match");
      goto _end;
    }
  }
  add_header(remote_ip, helo_domain, "DNS mismatch");
_end:
  closelog();
  return 0;
}

canonical_original_code:
int main() {
  char *helo_domain = getenv("SMTPHELOHOST");
  char *remote_ip = getenv("TCPREMOTEIP");
  struct addrinfo hints; struct addrinfo *res; struct addrinfo *result;
  char ipstr[16];
  int ret = 0;
  openlog("qmail-helo", 0x01, (16<<3));
  if (!helo_domain) {
    block(remote_ip, helo_domain, "no HELO/EHLO hostname has been sent");
    goto _end;
  }
  if (check_whitelisted(helo_domain)) {
    allow(remote_ip, helo_domain, "Whitelisted");
    goto _end;
  }
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(helo_domain, ((void *)0), &hints, &result);
  if (ret) {
    if (ret == -3) {
      allow(remote_ip, helo_domain, "temporary DNS failure");
    } else {
      block(remote_ip, helo_domain, gai_strerror(ret));
    }
    goto _end;
  }
  for (res = result; res != ((void *)0); res = res->ai_next) {
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
    void *addr = &(ipv4->sin_addr);
    inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
    if (!strncmp(ipstr, remote_ip, 16)) {
      allow(remote_ip, helo_domain, "DNS record match");
      goto _end;
    }
  }
  add_header(remote_ip, helo_domain, "DNS mismatch");
_end:
  closelog();
  return 0;
}

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

variable_types:
ipstr
char [16]

variable_types: ret 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:
addr
void *

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


usage

repo: piyus/btkernel

decompiled_code:
void __noreturn usage()
{
puts("Usage: launch_bt [<mode>]. mode=0 (none), 1 (inc_icount), 2 (shadowmem)");
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
puts("Usage: launch_bt [<mode>]. mode=0 (none), 1 (inc_icount), 2 (shadowmem)");
exit(1);
}

original_code:
static void
usage(void)
{
  printf("Usage: launch_bt [<mode>]. mode=0 (none), 1 (inc_icount), 2 (shadowmem)\n");
  exit(1);
}

canonical_original_code:
static void usage()
{
  printf("Usage: launch_bt [<mode>]. mode=0 (none), 1 (inc_icount), 2 (shadowmem)\n");
  exit(1);
}


decompiled_code:
void *__fastcall start_bt(void *a1)
{
if ( ioctl(*(_DWORD *)a1, 0x80046403uLL, *((unsigned int *)a1 + 1)) )
{
perror("ioctl TRANSLATE_IDT error");
abort();
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
if ( ioctl(*(_DWORD *)a1, 0x80046403uLL, *((unsigned int *)a1 + 1)) )
{
perror("ioctl TRANSLATE_IDT error");
abort();
}
return 0LL;
}

variable_types:
arg
void *

variable_types: fd int

variable_types: id int

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
int v5; // er15
const char *v6; // r12
unsigned int v7; // ebp
unsigned __int64 i; // rbx
pthread_t *v9; // r14
_DWORD *v10; // rbp
pthread_attr_t *v11; // rax
pthread_attr_t *v12; // r12
__int64 v14; // [rsp+8h] [rbp-930h]
cpu_set_t cpuset; // [rsp+10h] [rbp-928h] BYREF
char s[112]; // [rsp+90h] [rbp-8A8h] BYREF
pthread_t v17[263]; // [rsp+100h] [rbp-838h] BYREF

snprintf(s, 0x64uLL, "/dev/%s", "bt");
v3 = sysconf(84);
v4 = open(s, 0);
if ( v4 < 0 )
{
perror("unable to open dev");
}
else
{
v5 = v4;
if ( ioctl(v4, 0x80046401uLL, (unsigned int)v3) )
{
perror("ioctl INIT_BT error");
}
else
{
if ( argc <= 1 )
goto LABEL_8;
if ( argc != 2 )
usage();
v6 = argv[1];
v7 = atoi(v6);
if ( !v7 && strcmp(v6, "0") )
usage();
if ( ioctl(v5, 0x80046406uLL, v7) )
{
perror("ioctl BT_SET_MODE");
}
else
{
LABEL_8:
if ( v3 > 0 )
{
v14 = (unsigned int)(v3 - 1);
for ( i = 0LL; ; ++i )
{
v10 = malloc(8uLL);
v11 = (pthread_attr_t *)malloc(0x38uLL);
v12 = v11;
if ( !v10 )
__assert_fail("arg", "launch_bt.c", 0x4Fu, "main");
*v10 = v5;
v10[1] = i;
if ( !v11 )
__assert_fail("tattr", "launch_bt.c", 0x52u, "main");
pthread_attr_init(v11);
memset(&cpuset, 0, sizeof(cpuset));
if ( i <= 0x3FF )
cpuset.__bits[i >> 6] |= 1LL << i;
pthread_attr_setaffinity_np(v12, 0x80uLL, &cpuset);
v9 = v17;
if ( pthread_create(&v17[i], v12, start_bt, v10) )
fwrite("ERROR: Creating Pthread\n", 1uLL, 0x18uLL, stderr);
if ( i == v14 )
break;
}
do
pthread_join(*v9++, 0LL);
while ( v9 != &v17[v14 + 1] );
}
close(v5);
}
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
int v5; // er15
const char *v6; // r12
unsigned int v7; // ebp
unsigned __int64 i; // rbx
pthread_t *v9; // r14
_DWORD *v10; // rbp
pthread_attr_t *v11; // rax
pthread_attr_t *v12; // r12
__int64 v14; // [rsp+8h] [rbp-930h]
cpu_set_t cpuset; // [rsp+10h] [rbp-928h] BYREF
char s[112]; // [rsp+90h] [rbp-8A8h] BYREF
pthread_t v17[263]; // [rsp+100h] [rbp-838h] BYREF

snprintf(s, 0x64uLL, "/dev/%s", "bt");
v3 = sysconf(84);
v4 = open(s, 0);
if ( v4 < 0 )
{
perror("unable to open dev");
}
else
{
v5 = v4;
if ( ioctl(v4, 0x80046401uLL, (unsigned int)v3) )
{
perror("ioctl INIT_BT error");
}
else
{
if ( argc <= 1 )
goto LABEL_8;
if ( argc != 2 )
func8();
v6 = argv[1];
v7 = atoi(v6);
if ( !v7 && strcmp(v6, "0") )
func8();
if ( ioctl(v5, 0x80046406uLL, v7) )
{
perror("ioctl BT_SET_MODE");
}
else
{
LABEL_8:
if ( v3 > 0 )
{
v14 = (unsigned int)(v3 - 1);
for ( i = 0LL; ; ++i )
{
v10 = malloc(8uLL);
v11 = (pthread_attr_t *)malloc(0x38uLL);
v12 = v11;
if ( !v10 )
__assert_fail("arg", "launch_bt.c", 0x4Fu, "main");
*v10 = v5;
v10[1] = i;
if ( !v11 )
__assert_fail("tattr", "launch_bt.c", 0x52u, "main");
pthread_attr_init(v11);
memset(&cpuset, 0, sizeof(cpuset));
if ( i <= 0x3FF )
cpuset.__bits[i >> 6] |= 1LL << i;
pthread_attr_setaffinity_np(v12, 0x80uLL, &cpuset);
v9 = v17;
if ( pthread_create(&v17[i], v12, start_bt, v10) )
fwrite("ERROR: Creating Pthread\n", 1uLL, 0x18uLL, stderr);
if ( i == v14 )
break;
}
do
pthread_join(*v9++, 0LL);
while ( v9 != &v17[v14 + 1] );
}
close(v5);
}
}
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
  pthread_t threads[255];
  char name[100];
  int fd;
  int n_procs;
  int i;
  snprintf(name, sizeof(name), "/dev/%s", "bt");
  n_procs = sysconf(_SC_NPROCESSORS_ONLN);
  fd = open(name, 00);
  if (fd < 0) {
    perror("unable to open dev");
    return 0;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), n_procs) != 0) {
    perror("ioctl INIT_BT error");
    return 0;
  }
  if (argc > 1) {
    int mode;
    if (argc != 2) {
      usage();
    }
    mode = atoi(argv[1]);
    if (mode == 0 && strcmp(argv[1], "0")) {
      usage();
    }
    if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((6)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), mode) != 0) {
      perror("ioctl BT_SET_MODE");
      return 0;
    }
  }
  for (i = 0; i < n_procs; i++) {
    int *arg = malloc(sizeof(int)*2);
    pthread_attr_t *tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
    cpu_set_t cpuset;
    ((void) sizeof ((arg) ? 1 : 0), __extension__ ({ if (arg) ; else __assert_fail ("arg", "launch_bt.c", 79, __extension__ __PRETTY_FUNCTION__); }));
    *arg = fd;
    *(arg + 1) = i;
    ((void) sizeof ((tattr) ? 1 : 0), __extension__ ({ if (tattr) ; else __assert_fail ("tattr", "launch_bt.c", 82, __extension__ __PRETTY_FUNCTION__); }));
    pthread_attr_init(tattr);
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ size_t __cpu = (i); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    pthread_attr_setaffinity_np (tattr, sizeof(cpuset), &cpuset);
    if (pthread_create(&threads[i], tattr, start_bt, arg)) {
      fprintf(stderr, "ERROR: Creating Pthread\n");
    }
  }
  for (i = 0; i < n_procs; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  close(fd);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  unsigned long threads[255];
  char name[100];
  int fd;
  int n_procs;
  int i;
  snprintf(name, sizeof(name), "/dev/%s", "bt");
  n_procs = sysconf(_SC_NPROCESSORS_ONLN);
  fd = open(name, 00);
  if (fd < 0) {
    perror("unable to open dev");
    return 0;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), n_procs) != 0) {
    perror("ioctl INIT_BT error");
    return 0;
  }
  if (argc > 1) {
    int mode;
    if (argc != 2) {
      usage();
    }
    mode = atoi(argv[1]);
    if (mode == 0 && strcmp(argv[1], "0")) {
      usage();
    }
    if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((6)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), mode) != 0) {
      perror("ioctl BT_SET_MODE");
      return 0;
    }
  }
  for (i = 0; i < n_procs; i++) {
    int *arg = malloc(sizeof(int)*2);
    union pthread_attr_t *tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
    struct cpu_set_t cpuset;
    ((void) sizeof ((arg) ? 1 : 0), __extension__ ({ if (arg) ; else __assert_fail ("arg", "launch_bt.c", 79, __extension__ __PRETTY_FUNCTION__); }));
    *arg = fd;
    *(arg + 1) = i;
    ((void) sizeof ((tattr) ? 1 : 0), __extension__ ({ if (tattr) ; else __assert_fail ("tattr", "launch_bt.c", 82, __extension__ __PRETTY_FUNCTION__); }));
    pthread_attr_init(tattr);
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ unsigned long __cpu = (i); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    pthread_attr_setaffinity_np (tattr, sizeof(cpuset), &cpuset);
    if (pthread_create(&threads[i], tattr, start_bt, arg)) {
      fprintf(stderr, "ERROR: Creating Pthread\n");
    }
  }
  for (i = 0; i < n_procs; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  close(fd);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
threads
unsigned long [255]

variable_types:
name
char [100]

variable_types: i int

variable_types: mode int

variable_types:
arg
int *

variable_types: union pthread_attr_t { char __size[56]; long __align; } *

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
void *__fastcall load_native_idt(void *a1)
{
if ( ioctl(*(_DWORD *)a1, 0x80046404uLL, *((unsigned int *)a1 + 1)) )
perror("ioctl LOAD_NATIVE_IDT");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
if ( ioctl(*(_DWORD *)a1, 0x80046404uLL, *((unsigned int *)a1 + 1)) )
perror("ioctl LOAD_NATIVE_IDT");
return 0LL;
}

variable_types: idx int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
int v5; // er15
int v6; // eax
unsigned __int64 v7; // rbx
pthread_t *v8; // r14
_DWORD *v9; // rbp
pthread_attr_t *v10; // rax
pthread_attr_t *v11; // r12
__int64 v13; // [rsp+0h] [rbp-938h]
unsigned int v14; // [rsp+8h] [rbp-930h]
cpu_set_t cpuset; // [rsp+10h] [rbp-928h] BYREF
char s[112]; // [rsp+90h] [rbp-8A8h] BYREF
pthread_t v17[263]; // [rsp+100h] [rbp-838h] BYREF

snprintf(s, 0x64uLL, "/dev/%s", "bt");
v3 = sysconf(84);
v14 = v3;
v4 = open(s, 0);
if ( v4 < 0 )
{
perror("unable to open dev");
}
else
{
v5 = v4;
v6 = v3;
v13 = (unsigned int)(v3 - 1);
v7 = 0LL;
if ( v6 > 0 )
{
while ( 1 )
{
v9 = malloc(8uLL);
v10 = (pthread_attr_t *)malloc(0x38uLL);
v11 = v10;
if ( !v9 )
__assert_fail("arg", "terminate_bt.c", 0x30u, "main");
*v9 = v5;
v9[1] = v7;
if ( !v10 )
__assert_fail("tattr", "terminate_bt.c", 0x33u, "main");
pthread_attr_init(v10);
memset(&cpuset, 0, sizeof(cpuset));
if ( v7 <= 0x3FF )
cpuset.__bits[v7 >> 6] |= 1LL << v7;
pthread_attr_setaffinity_np(v11, 0x80uLL, &cpuset);
v8 = v17;
if ( pthread_create(&v17[v7], v11, load_native_idt, v9) )
{
fwrite("ERROR: Creating Pthread\n", 1uLL, 0x18uLL, stderr);
close(v5);
return 0;
}
if ( v7 == v13 )
break;
++v7;
}
do
pthread_join(*v8++, 0LL);
while ( v8 != &v17[v13 + 1] );
}
if ( ioctl(v5, 0x80046402uLL, v14) )
perror("ioctl STOP_BT");
close(v5);
}
return 0;
}

original_code:
int
main(void)
{
  pthread_t threads[255];
  char name[100];
  int fd;
  int n_procs;
  int i;
  snprintf(name, sizeof(name), "/dev/%s", "bt");
  n_procs = sysconf(_SC_NPROCESSORS_ONLN);
  fd = open(name, 00);
  if (fd < 0) {
    perror("unable to open dev");
    return 0;
  }
  for (i = 0; i < n_procs; i++) {
    int *arg = malloc(sizeof(int)*2);
    pthread_attr_t *tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
    cpu_set_t cpuset;
    ((void) sizeof ((arg) ? 1 : 0), __extension__ ({ if (arg) ; else __assert_fail ("arg", "terminate_bt.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    *arg = fd;
    *(arg + 1) = i;
    ((void) sizeof ((tattr) ? 1 : 0), __extension__ ({ if (tattr) ; else __assert_fail ("tattr", "terminate_bt.c", 51, __extension__ __PRETTY_FUNCTION__); }));
    pthread_attr_init(tattr);
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ size_t __cpu = (i); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    pthread_attr_setaffinity_np (tattr, sizeof(cpuset), &cpuset);
    if (pthread_create(&threads[i], tattr, load_native_idt, arg)) {
      fprintf(stderr, "ERROR: Creating Pthread\n");
      close(fd);
      return 0;
    }
  }
  for (i = 0; i < n_procs; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), n_procs) != 0) {
    perror("ioctl STOP_BT");
  }
  close(fd);
  return 0;
}

canonical_original_code:
int main()
{
  unsigned long threads[255];
  char name[100];
  int fd;
  int n_procs;
  int i;
  snprintf(name, sizeof(name), "/dev/%s", "bt");
  n_procs = sysconf(_SC_NPROCESSORS_ONLN);
  fd = open(name, 00);
  if (fd < 0) {
    perror("unable to open dev");
    return 0;
  }
  for (i = 0; i < n_procs; i++) {
    int *arg = malloc(sizeof(int)*2);
    union pthread_attr_t *tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
    struct cpu_set_t cpuset;
    ((void) sizeof ((arg) ? 1 : 0), __extension__ ({ if (arg) ; else __assert_fail ("arg", "terminate_bt.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    *arg = fd;
    *(arg + 1) = i;
    ((void) sizeof ((tattr) ? 1 : 0), __extension__ ({ if (tattr) ; else __assert_fail ("tattr", "terminate_bt.c", 51, __extension__ __PRETTY_FUNCTION__); }));
    pthread_attr_init(tattr);
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ unsigned long __cpu = (i); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    pthread_attr_setaffinity_np (tattr, sizeof(cpuset), &cpuset);
    if (pthread_create(&threads[i], tattr, load_native_idt, arg)) {
      fprintf(stderr, "ERROR: Creating Pthread\n");
      close(fd);
      return 0;
    }
  }
  for (i = 0; i < n_procs; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((100)) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), n_procs) != 0) {
    perror("ioctl STOP_BT");
  }
  close(fd);
  return 0;
}

user_defined_types: struct cpu_set_t { unsigned long __bits[-1]; }

user_defined_types: union pthread_attr_t { char __size[56]; long __align; }


repo: pizzamig/gdb-test-suite

decompiled_code:
int __fastcall timer_thread(const void *a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
char *v5; // rax
char *v6; // rax
char *v7; // rax

v1 = pthread_mutex_lock(&mutex);
if ( v1 )
{
v5 = strerror(v1);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "sigev_thread.c", 38LL, v5);
abort();
}
if ( ++counter > 4 )
{
v2 = pthread_cond_signal(&cond);
if ( v2 )
{
v6 = strerror(v2);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Signal condition", "sigev_thread.c", 42LL, v6);
abort();
}
}
v3 = pthread_mutex_unlock(&mutex);
if ( v3 )
{
v7 = strerror(v3);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "sigev_thread.c", 46LL, v7);
abort();
}
return printf("Timer (0x%p) %d\n", a1, (unsigned int)counter);
}

canonical_decompiled_code:
int __fastcall func7(const void *a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
char *v5; // rax
char *v6; // rax
char *v7; // rax

v1 = pthread_mutex_lock(&mutex);
if ( v1 )
{
v5 = strerror(v1);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "sigev_thread.c", 38LL, v5);
abort();
}
if ( ++counter > 4 )
{
v2 = pthread_cond_signal(&cond);
if ( v2 )
{
v6 = strerror(v2);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Signal condition", "sigev_thread.c", 42LL, v6);
abort();
}
}
v3 = pthread_mutex_unlock(&mutex);
if ( v3 )
{
v7 = strerror(v3);
fprintf(stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "sigev_thread.c", 46LL, v7);
abort();
}
return printf("Timer (0x%p) %d\n", a1, (unsigned int)counter);
}

original_code:
void
timer_thread (union sigval si)
{
    int status;
    status = pthread_mutex_lock (&mutex);
    if (status != 0)
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "sigev_thread.c", 38, strerror (status)); abort (); } while (0);
    if (++counter >= 5) {
        status = pthread_cond_signal (&cond);
        if (status != 0)
            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Signal condition", "sigev_thread.c", 42, strerror (status)); abort (); } while (0);
    }
    status = pthread_mutex_unlock (&mutex);
    if (status != 0)
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "sigev_thread.c", 46, strerror (status)); abort (); } while (0);
    printf ("Timer (0x%p) %d\n", si.sival_ptr, counter);
}

canonical_original_code:
void timer_thread(union sigval si)
{
    int status;
    status = pthread_mutex_lock (&mutex);
    if (status != 0)
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "sigev_thread.c", 38, strerror (status)); abort (); } while (0);
    if (++counter >= 5) {
        status = pthread_cond_signal (&cond);
        if (status != 0)
            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Signal condition", "sigev_thread.c", 42, strerror (status)); abort (); } while (0);
    }
    status = pthread_mutex_unlock (&mutex);
    if (status != 0)
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "sigev_thread.c", 46, strerror (status)); abort (); } while (0);
    printf ("Timer (0x%p) %d\n", si.sival_ptr, counter);
}

variable_types: union sigval { int sival_int; void *sival_ptr; }

variable_types: status int