decompiled_code:
__int64 __fastcall read_pmt(const char *a1, unsigned __int16 a2, unsigned int a3, _BYTE *a4)
{
unsigned int v7; // ebx
unsigned int v8; // er14
int v10; // edi
int v11; // esi
int v12; // edx
unsigned __int16 v13; // ax
int v14; // ebx
const char *v15; // rdx
char v16[4096]; // [rsp+0h] [rbp-2028h] BYREF
char s[4136]; // [rsp+1000h] [rbp-1028h] BYREF

v7 = a2;
snprintf(s, 0x1000uLL, "pmt-%u", a2);
v8 = cache_load(s, a4);
if ( !(_BYTE)v8 )
{
v8 = read_pat(a1, a3, v16);
if ( (_BYTE)v8 )
{
v10 = (unsigned __int16)((((unsigned __int8)v16[1] << 8) & 0xF00) + (unsigned __int8)v16[2] + 3) - 4;
if ( v10 <= 8 )
{
LABEL_11:
fatal("Unable to find PMT PID for service_id %u", v7);
v13 = 0;
}
else
{
v11 = 8;
v12 = 8;
while ( v7 != (unsigned __int8)v16[v12 + 1] + ((unsigned __int8)v16[v12] << 8) )
{
v11 += 4;
v12 = (unsigned __int16)v11;
if ( (unsigned __int16)v11 >= v10 )
goto LABEL_11;
}
v13 = (unsigned __int8)v16[v12 + 3] + (((unsigned __int8)v16[v12 + 2] << 8) & 0x1F00);
}
v14 = v13;
vverbose("PMT PID: %u", v13);
v8 = read_table(a1, v14, 2, a3, a4, 0);
if ( (_BYTE)v8 )
cache_save(s, a4);
else
error((int)"Unable to read PMT", v14, v15);
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, unsigned __int16 a2, unsigned int a3, _BYTE *a4)
{
unsigned int v7; // ebx
unsigned int v8; // er14
int v10; // edi
int v11; // esi
int v12; // edx
unsigned __int16 v13; // ax
int v14; // ebx
const char *v15; // rdx
char v16[4096]; // [rsp+0h] [rbp-2028h] BYREF
char s[4136]; // [rsp+1000h] [rbp-1028h] BYREF

v7 = a2;
snprintf(s, 0x1000uLL, "pmt-%u", a2);
v8 = cache_load(s, a4);
if ( !(_BYTE)v8 )
{
v8 = func1(a1, a3, v16);
if ( (_BYTE)v8 )
{
v10 = (unsigned __int16)((((unsigned __int8)v16[1] << 8) & 0xF00) + (unsigned __int8)v16[2] + 3) - 4;
if ( v10 <= 8 )
{
LABEL_11:
fatal("Unable to find PMT PID for service_id %u", v7);
v13 = 0;
}
else
{
v11 = 8;
v12 = 8;
while ( v7 != (unsigned __int8)v16[v12 + 1] + ((unsigned __int8)v16[v12] << 8) )
{
v11 += 4;
v12 = (unsigned __int16)v11;
if ( (unsigned __int16)v11 >= v10 )
goto LABEL_11;
}
v13 = (unsigned __int8)v16[v12 + 3] + (((unsigned __int8)v16[v12 + 2] << 8) & 0x1F00);
}
v14 = v13;
vverbose("PMT PID: %u", v13);
v8 = func0(a1, v14, 2, a3, a4, 0);
if ( (_BYTE)v8 )
cache_save(s, a4);
else
error((int)"Unable to read PMT", v14, v15);
}
}
return v8;
}

original_code:
_Bool
read_pmt(char *demux, uint16_t service_id, unsigned int timeout, unsigned char *out)
{
 char cache_item[4096];
 unsigned char pat[4096];
 uint16_t section_length;
 uint16_t offset;
 uint16_t map_pid = 0;
 _Bool found;
 _Bool rc;
 snprintf(cache_item, sizeof(cache_item), "pmt-%u", service_id);
 if(cache_load(cache_item, out))
  return 1;
 if(!read_pat(demux, timeout, pat))
  return 0;
 section_length = 3 + (((pat[1] & 0x0f) << 8) + pat[2]);
 found = 0;
 offset = 8;
 while((offset < (section_length - 4)) && !found)
 {
  if((pat[offset] << 8) + pat[offset+1] == service_id)
  {
   map_pid = ((pat[offset+2] & 0x1f) << 8) + pat[offset+3];
   found = 1;
  }
  else
  {
   offset += 4;
  }
 }
 if(!found)
  fatal("Unable to find PMT PID for service_id %u", service_id);
 vverbose("PMT PID: %u", map_pid);
 rc = read_table(demux, map_pid, 0x02, timeout, out, 0);
 if(rc)
  cache_save(cache_item, out);
 else
  error("Unable to read PMT");
 return rc;
}

canonical_original_code:
_Bool read_pmt(char *demux, unsigned short service_id, unsigned int timeout, unsigned char *out)
{
 char cache_item[4096];
 unsigned char pat[4096];
 unsigned short section_length;
 unsigned short offset;
 unsigned short map_pid = 0;
 _Bool found;
 _Bool rc;
 snprintf(cache_item, sizeof(cache_item), "pmt-%u", service_id);
 if(cache_load(cache_item, out))
  return 1;
 if(!read_pat(demux, timeout, pat))
  return 0;
 section_length = 3 + (((pat[1] & 0x0f) << 8) + pat[2]);
 found = 0;
 offset = 8;
 while((offset < (section_length - 4)) && !found)
 {
  if((pat[offset] << 8) + pat[offset+1] == service_id)
  {
   map_pid = ((pat[offset+2] & 0x1f) << 8) + pat[offset+3];
   found = 1;
  }
  else
  {
   offset += 4;
  }
 }
 if(!found)
  fatal("Unable to find PMT PID for service_id %u", service_id);
 vverbose("PMT PID: %u", map_pid);
 rc = read_table(demux, map_pid, 0x02, timeout, out, 0);
 if(rc)
  cache_save(cache_item, out);
 else
  error("Unable to read PMT");
 return rc;
}

variable_types:
pat
unsigned char [4096]

variable_types:
offset
unsigned short


repo: thiakil/rb-download

decompiled_code:
__int64 __fastcall read_sdt(const char *a1, unsigned int a2, _BYTE *a3, unsigned __int8 a4)
{
unsigned int v6; // ebx
char v8[4136]; // [rsp+0h] [rbp-1028h] BYREF

snprintf(v8, 0x1000uLL, "sdt-%u", a4);
v6 = cache_load(v8, a3);
if ( !(_BYTE)v6 )
{
v6 = read_table(a1, 0x11u, 66, a2, a3, a4);
if ( (_BYTE)v6 )
cache_save(v8, a3);
else
vverbose("Unable to read SDT");
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, unsigned int a2, _BYTE *a3, unsigned __int8 a4)
{
unsigned int v6; // ebx
char v8[4136]; // [rsp+0h] [rbp-1028h] BYREF

snprintf(v8, 0x1000uLL, "sdt-%u", a4);
v6 = cache_load(v8, a3);
if ( !(_BYTE)v6 )
{
v6 = func0(a1, 0x11u, 66, a2, a3, a4);
if ( (_BYTE)v6 )
cache_save(v8, a3);
else
vverbose("Unable to read SDT");
}
return v6;
}

original_code:
_Bool
read_sdt(char *demux, unsigned int timeout, unsigned char *out, unsigned char sn)
{
 char cache_item[4096];
 snprintf(cache_item, sizeof(cache_item), "sdt-%u", sn);
 if(cache_load(cache_item, out))
  return 1;
 if(!read_table(demux, 0x0011, 0x42, timeout, out, sn))
 {
  vverbose("Unable to read SDT");
  return 0;
 }
 cache_save(cache_item, out);
 return 1;
}

variable_types:
demux
char *

variable_types:
timeout
unsigned int

variable_types:
out
unsigned char *

variable_types:
sn
unsigned char


original_code:
_Bool
read_dsmcc_tables(struct carousel *car, unsigned char *out)
{
 struct timeval timeout;
 unsigned int i;
 fd_set readfds;
 int max;
 int fd;
 int n;
 timeout.tv_sec = car->timeout;
 timeout.tv_usec = 0;
 do
 {
  out[0] = 0;
  max = 0;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
  for(i=0; i<car->npids; i++)
  {
   max = ((max) > (car->pids[i].fd_ctrl) ? (max) : (car->pids[i].fd_ctrl));
   max = ((max) > (car->pids[i].fd_data) ? (max) : (car->pids[i].fd_data));
   ((void) (((&readfds)->__fds_bits)[((car->pids[i].fd_ctrl) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((car->pids[i].fd_ctrl) % (8 * (int) sizeof (__fd_mask)))))));
   ((void) (((&readfds)->__fds_bits)[((car->pids[i].fd_data) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((car->pids[i].fd_data) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if(select(max + 1, &readfds, ((void *)0), ((void *)0), &timeout) < 0)
  {
   error("read_dsmcc_tables: select: %s", strerror((*__errno_location ())));
   return 0;
  }
  fd = -1;
  for(i=0; fd==-1 && i<car->npids; i++)
  {
   if(((((&readfds)->__fds_bits)[((car->pids[i].fd_ctrl) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((car->pids[i].fd_ctrl) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fd = car->pids[i].fd_ctrl;
    car->current_pid = car->pids[i].pid;
   }
   else if(((((&readfds)->__fds_bits)[((car->pids[i].fd_data) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((car->pids[i].fd_data) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fd = car->pids[i].fd_data;
    car->current_pid = car->pids[i].pid;
   }
  }
  if(fd == -1)
  {
   error("Timeout reading %s", car->demux_device);
   return 0;
  }
  if((n = read(fd, out, 4096)) < 0)
  {
   error("read: %s", strerror((*__errno_location ())));
   out[0] = 0;
  }
 }
 while(out[0] != 0x3b && out[0] != 0x3c);
 return out;
}

canonical_original_code:
_Bool read_dsmcc_tables(struct carousel *car, unsigned char *out)
{
 struct timeval timeout;
 unsigned int i;
 struct fd_set readfds;
 int max;
 int fd;
 int n;
 timeout.tv_sec = car->timeout;
 timeout.tv_usec = 0;
 do
 {
  out[0] = 0;
  max = 0;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
  for(i=0; i<car->npids; i++)
  {
   max = ((max) > (car->pids[i].fd_ctrl) ? (max) : (car->pids[i].fd_ctrl));
   max = ((max) > (car->pids[i].fd_data) ? (max) : (car->pids[i].fd_data));
   ((void) (((&readfds)->__fds_bits)[((car->pids[i].fd_ctrl) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((car->pids[i].fd_ctrl) % (8 * (int) sizeof (__fd_mask)))))));
   ((void) (((&readfds)->__fds_bits)[((car->pids[i].fd_data) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((car->pids[i].fd_data) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if(select(max + 1, &readfds, ((void *)0), ((void *)0), &timeout) < 0)
  {
   error("read_dsmcc_tables: select: %s", strerror((*__errno_location ())));
   return 0;
  }
  fd = -1;
  for(i=0; fd==-1 && i<car->npids; i++)
  {
   if(((((&readfds)->__fds_bits)[((car->pids[i].fd_ctrl) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((car->pids[i].fd_ctrl) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fd = car->pids[i].fd_ctrl;
    car->current_pid = car->pids[i].pid;
   }
   else if(((((&readfds)->__fds_bits)[((car->pids[i].fd_data) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((car->pids[i].fd_data) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fd = car->pids[i].fd_data;
    car->current_pid = car->pids[i].pid;
   }
  }
  if(fd == -1)
  {
   error("Timeout reading %s", car->demux_device);
   return 0;
  }
  if((n = read(fd, out, 4096)) < 0)
  {
   error("read: %s", strerror((*__errno_location ())));
   out[0] = 0;
  }
 }
 while(out[0] != 0x3b && out[0] != 0x3c);
 return out;
}

variable_types: struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } assoc; int npids; struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *pids; _Bool got_dsi; unsigned int nmodules; struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *modules; } *

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

variable_types:
i
unsigned int

variable_types:
readfds
struct fd_set { long __fds_bits[-1]; }

variable_types: max int

variable_types: fd int

variable_types: n int

user_defined_types: struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc assoc; int npids; struct pid_fds *pids; _Bool got_dsi; unsigned int nmodules; struct module *modules; }


original_code:
void
add_dsmcc_pid(struct carousel *car, uint16_t pid)
{
 unsigned int i;
 struct pid_fds *fds;
 struct dmx_sct_filter_params sctFilterParams;
 for(i=0; i<car->npids; i++)
  if(car->pids[i].pid == pid)
   return;
 verbose("Adding PID %u to filter", pid);
 car->npids ++;
 car->pids = safe_realloc(car->pids, car->npids * sizeof(struct pid_fds));
 fds = &car->pids[car->npids - 1];
 fds->pid = pid;
 if((fds->fd_ctrl = open(car->demux_device, 02)) < 0)
  fatal("open '%s': %s", car->demux_device, strerror((*__errno_location ())));
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = 0x3b;
 sctFilterParams.filter.mask[0] = 0xff;
 if(ioctl(fds->fd_ctrl, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
  fatal("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
 if((fds->fd_data = open(car->demux_device, 02)) < 0)
  fatal("open '%s': %s", car->demux_device, strerror((*__errno_location ())));
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = 0x3c;
 sctFilterParams.filter.mask[0] = 0xff;
 if(ioctl(fds->fd_data, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
  fatal("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
 return;
}

canonical_original_code:
void add_dsmcc_pid(struct carousel *car, unsigned short pid)
{
 unsigned int i;
 struct pid_fds *fds;
 struct dmx_sct_filter_params sctFilterParams;
 for(i=0; i<car->npids; i++)
  if(car->pids[i].pid == pid)
   return;
 verbose("Adding PID %u to filter", pid);
 car->npids ++;
 car->pids = safe_realloc(car->pids, car->npids * sizeof(struct pid_fds));
 fds = &car->pids[car->npids - 1];
 fds->pid = pid;
 if((fds->fd_ctrl = open(car->demux_device, 02)) < 0)
  fatal("open '%s': %s", car->demux_device, strerror((*__errno_location ())));
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = 0x3b;
 sctFilterParams.filter.mask[0] = 0xff;
 if(ioctl(fds->fd_ctrl, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
  fatal("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
 if((fds->fd_data = open(car->demux_device, 02)) < 0)
  fatal("open '%s': %s", car->demux_device, strerror((*__errno_location ())));
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = 0x3c;
 sctFilterParams.filter.mask[0] = 0xff;
 if(ioctl(fds->fd_data, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
  fatal("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
 return;
}

variable_types:
pid
unsigned short

variable_types: struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *

variable_types: struct dmx_sct_filter_params { unsigned short pid; struct dmx_filter { unsigned char filter[16]; unsigned char mask[16]; unsigned char mode[16]; } filter; unsigned int timeout; unsigned int flags; }

return_type: void

user_defined_types: struct dmx_sct_filter_params { unsigned short pid; struct dmx_filter filter; unsigned int timeout; unsigned int flags; }

user_defined_types: struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; }

user_defined_types: struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; }


decompiled_code:
void __fastcall safe_free(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void
safe_free(void *buf)
{
 if(buf != ((void *)0))
  free(buf);
 return;
}

variable_types:
buf
void *


error

decompiled_code:
int error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

original_code:
void
error(char *message, ...)
{
 va_list ap;
 __builtin_va_start(ap,message);
 vfprintf(stderr, message, ap);
 fprintf(stderr, "\n");
 __builtin_va_end(ap);
 return;
}

canonical_original_code:
void error(char *message, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,message);
 vfprintf(stderr, message, ap);
 fprintf(stderr, "\n");
 __builtin_va_end(ap);
 return;
}

variable_types:
message
char *


fatal

decompiled_code:
void __noreturn fatal(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func2(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void
fatal(char *message, ...)
{
 va_list ap;
 __builtin_va_start(ap,message);
 vfprintf(stderr, message, ap);
 fprintf(stderr, "\n");
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void fatal(char *message, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,message);
 vfprintf(stderr, message, ap);
 fprintf(stderr, "\n");
 __builtin_va_end(ap);
 exit(1);
}


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

if ( (unsigned __int8)a1 > 0xFu )
fatal("Not a single hex digit");
result = (unsigned int)(a1 + 87);
if ( (unsigned __int8)a1 <= 9u )
result = (unsigned int)(a1 + 48);
return result;
}

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

if ( (unsigned __int8)a1 > 0xFu )
func2("Not a single hex digit");
result = (unsigned int)(a1 + 87);
if ( (unsigned __int8)a1 <= 9u )
result = (unsigned int)(a1 + 48);
return result;
}

original_code:
char
hex_digit(uint8_t val)
{
 if(val > 0xf)
  fatal("Not a single hex digit");
 return (val < 10) ? '0' + val : 'a' + (val - 10);
}

canonical_original_code:
char hex_digit(unsigned char val)
{
 if(val > 0xf)
  fatal("Not a single hex digit");
 return (val < 10) ? '0' + val : 'a' + (val - 10);
}

variable_types:
val
unsigned char

return_type: char


decompiled_code:
void *__fastcall safe_malloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
fatal("Out of memory");
return result;
}

canonical_decompiled_code:
void *__fastcall func4(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
func2("Out of memory");
return result;
}

original_code:
void *
safe_malloc(size_t nbytes)
{
 void *buf;
 if((buf = malloc(nbytes)) == ((void *)0))
  fatal("Out of memory");
 return buf;
}

canonical_original_code:
void *safe_malloc(unsigned long nbytes)
{
 void *buf;
 if((buf = malloc(nbytes)) == ((void *)0))
  fatal("Out of memory");
 return buf;
}

variable_types:
nbytes
unsigned long

return_type: void *


decompiled_code:
void *__fastcall safe_realloc(void *a1, size_t a2)
{
void *result; // rax

if ( !a1 )
return safe_malloc(a2);
result = realloc(a1, a2);
if ( !result )
fatal("Out of memory");
return result;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1, size_t a2)
{
void *result; // rax

if ( !a1 )
return func4(a2);
result = realloc(a1, a2);
if ( !result )
func2("Out of memory");
return result;
}

original_code:
void *
safe_realloc(void *oldbuf, size_t nbytes)
{
 void *newbuf;
 if(oldbuf == ((void *)0))
  return safe_malloc(nbytes);
 if((newbuf = realloc(oldbuf, nbytes)) == ((void *)0))
  fatal("Out of memory");
 return newbuf;
}

canonical_original_code:
void *safe_realloc(void *oldbuf, unsigned long nbytes)
{
 void *newbuf;
 if(oldbuf == ((void *)0))
  return safe_malloc(nbytes);
 if((newbuf = realloc(oldbuf, nbytes)) == ((void *)0))
  fatal("Out of memory");
 return newbuf;
}

variable_types:
oldbuf
void *

variable_types:
newbuf
void *


hexdump

original_code:
void
hexdump(unsigned char *data, size_t nbytes)
{
 size_t nout;
 int i;
 nout = 0;
 while(nout < nbytes)
 {
  if((nout % 16) == 0)
   printf("0x%.8x  ", nout);
  printf("%.2x ", data[nout]);
  if((nout % 16) == (16 - 1))
  {
   printf(" ");
   for(i=16 -1; i>=0; i--)
    printf("%c", ((*__ctype_b_loc ())[(int) ((data[nout - i]))] & (unsigned short int) _ISprint) ? data[nout - i] : '.');
   printf("\n");
  }
  nout ++;
 }
 if((nout % 16) != 0)
 {
  for(i=(nout % 16); i<16; i++)
   printf("   ");
  printf(" ");
  nout --;
  for(i=(nout % 16); i>=0; i--)
   printf("%c", ((*__ctype_b_loc ())[(int) ((data[nout - i]))] & (unsigned short int) _ISprint) ? data[nout - i] : '.');
  printf("\n");
 }
 return;
}

canonical_original_code:
void hexdump(unsigned char *data, unsigned long nbytes)
{
 unsigned long nout;
 int i;
 nout = 0;
 while(nout < nbytes)
 {
  if((nout % 16) == 0)
   printf("0x%.8x  ", nout);
  printf("%.2x ", data[nout]);
  if((nout % 16) == (16 - 1))
  {
   printf(" ");
   for(i=16 -1; i>=0; i--)
    printf("%c", ((*__ctype_b_loc ())[(int) ((data[nout - i]))] & (unsigned short) _ISprint) ? data[nout - i] : '.');
   printf("\n");
  }
  nout ++;
 }
 if((nout % 16) != 0)
 {
  for(i=(nout % 16); i<16; i++)
   printf("   ");
  printf(" ");
  nout --;
  for(i=(nout % 16); i>=0; i--)
   printf("%c", ((*__ctype_b_loc ())[(int) ((data[nout - i]))] & (unsigned short) _ISprint) ? data[nout - i] : '.');
  printf("\n");
 }
 return;
}

variable_types:
data
unsigned char *

variable_types:
nout
unsigned long

variable_types: i int


decompiled_code:
__int64 __fastcall process_dii(__int64 a1, unsigned int *a2, unsigned int a3)
{
unsigned int v4; // eax
__int64 v5; // r14
__int64 result; // rax
unsigned int v7; // ebp
__int64 v8; // rbx
unsigned __int16 v9; // ax
unsigned int v10; // eax
unsigned int v11; // er12
unsigned __int16 v12; // ax

verbose("DownloadInfoIndication");
vverbose("transactionId: %u", a3);
v4 = _bswap_32(*a2);
vverbose("downloadId: %u", v4);
v5 = (unsigned __int16)DII_numberOfModules(a2);
result = vverbose("numberOfModules: %u", v5);
if ( (_DWORD)v5 )
{
v7 = 0;
do
{
v8 = DII_module(a2, (unsigned __int16)v7);
vverbose("Module %u", v7);
v9 = _bswap_16(*(_WORD *)v8);
vverbose(" moduleId: %u", v9);
vverbose(" moduleVersion: %u", *(unsigned __int8 *)(v8 + 6));
v10 = _bswap_32(*(_DWORD *)(v8 + 2));
vverbose(" moduleSize: %u", v10);
v11 = _bswap_32(*a2);
v12 = _bswap_16(*(_WORD *)v8);
result = find_module(a1, v12, *(unsigned __int8 *)(v8 + 6), v11);
if ( !result )
result = add_module(a1, a2, v8);
++v7;
}
while ( (_DWORD)v5 != v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int *a2, unsigned int a3)
{
unsigned int v4; // eax
__int64 v5; // r14
__int64 result; // rax
unsigned int v7; // ebp
__int64 v8; // rbx
unsigned __int16 v9; // ax
unsigned int v10; // eax
unsigned int v11; // er12
unsigned __int16 v12; // ax

verbose("DownloadInfoIndication");
vverbose("transactionId: %u", a3);
v4 = func0(*a2);
vverbose("downloadId: %u", v4);
v5 = (unsigned __int16)DII_numberOfModules(a2);
result = vverbose("numberOfModules: %u", v5);
if ( (_DWORD)v5 )
{
v7 = 0;
do
{
v8 = DII_module(a2, (unsigned __int16)v7);
vverbose("Module %u", v7);
v9 = func1(*(_WORD *)v8);
vverbose(" moduleId: %u", v9);
vverbose(" moduleVersion: %u", *(unsigned __int8 *)(v8 + 6));
v10 = func0(*(_DWORD *)(v8 + 2));
vverbose(" moduleSize: %u", v10);
v11 = func0(*a2);
v12 = func1(*(_WORD *)v8);
result = find_module(a1, v12, *(unsigned __int8 *)(v8 + 6), v11);
if ( !result )
result = add_module(a1, a2, v8);
++v7;
}
while ( (_DWORD)v5 != v7 );
}
return result;
}

original_code:
void
process_dii(struct carousel *car, struct DownloadInfoIndication *dii, uint32_t transactionId)
{
 unsigned int nmodules;
 unsigned int i;
 verbose("DownloadInfoIndication");
 vverbose("transactionId: %u", transactionId);
 vverbose("downloadId: %u", ntohl(dii->downloadId));
 nmodules = DII_numberOfModules(dii);
 vverbose("numberOfModules: %u", nmodules);
 for(i=0; i<nmodules; i++)
 {
  struct DIIModule *mod;
  mod = DII_module(dii, i);
  vverbose("Module %u", i);
  vverbose(" moduleId: %u", ntohs(mod->moduleId));
  vverbose(" moduleVersion: %u", mod->moduleVersion);
  vverbose(" moduleSize: %u", ntohl(mod->moduleSize));
  if(find_module(car, ntohs(mod->moduleId), mod->moduleVersion, ntohl(dii->downloadId)) == ((void *)0))
   add_module(car, dii, mod);
 }
 return;
}

canonical_original_code:
void process_dii(struct carousel *car, struct DownloadInfoIndication *dii, unsigned int transactionId)
{
 unsigned int nmodules;
 unsigned int i;
 verbose("DownloadInfoIndication");
 vverbose("transactionId: %u", transactionId);
 vverbose("downloadId: %u", ntohl(dii->downloadId));
 nmodules = DII_numberOfModules(dii);
 vverbose("numberOfModules: %u", nmodules);
 for(i=0; i<nmodules; i++)
 {
  struct DIIModule *mod;
  mod = DII_module(dii, i);
  vverbose("Module %u", i);
  vverbose(" moduleId: %u", ntohs(mod->moduleId));
  vverbose(" moduleVersion: %u", mod->moduleVersion);
  vverbose(" moduleSize: %u", ntohl(mod->moduleSize));
  if(find_module(car, ntohs(mod->moduleId), mod->moduleVersion, ntohl(dii->downloadId)) == ((void *)0))
   add_module(car, dii, mod);
 }
 return;
}

variable_types: struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } assoc; int npids; struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *pids; _Bool got_dsi; unsigned int nmodules; struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *modules; } *

variable_types: struct DownloadInfoIndication { unsigned int downloadId; unsigned short blockSize; unsigned char windowSize; unsigned char ackPeriod; unsigned int tCDownloadWindow; unsigned int tCDownloadScenario; struct CompatibilityDescriptor { unsigned short compatibilityDescriptorLength; } compatibilityDescriptor; } *

variable_types:
transactionId
unsigned int

variable_types:
nmodules
unsigned int

variable_types:
i
unsigned int

variable_types: struct DIIModule { unsigned short moduleId; unsigned int moduleSize; unsigned char moduleVersion; unsigned char moduleInfoLength; } *

user_defined_types: struct DownloadInfoIndication { unsigned int downloadId; unsigned short blockSize; unsigned char windowSize; unsigned char ackPeriod; unsigned int tCDownloadWindow; unsigned int tCDownloadScenario; struct CompatibilityDescriptor compatibilityDescriptor; }


decompiled_code:
__int64 __fastcall process_dsi(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned __int16 v3; // r12
__int64 v4; // rax
unsigned __int16 v5; // ax

verbose("DownloadServerInitiate");
result = *(unsigned __int16 *)(a1 + 8208);
if ( *(_WORD *)(a1 + 8218) == (_WORD)result && !*(_BYTE *)(a1 + 8272) )
{
*(_BYTE *)(a1 + 8272) = 1;
v3 = _bswap_16(*(_WORD *)(a2 + 22));
v4 = DSI_privateDataByte(a2);
v5 = process_biop_service_gateway_info(*(unsigned __int16 *)(a1 + 8202), a1 + 8224, v4, v3);
result = add_dsmcc_pid(a1, v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned __int16 v3; // r12
__int64 v4; // rax
unsigned __int16 v5; // ax

verbose("DownloadServerInitiate");
result = *(unsigned __int16 *)(a1 + 8208);
if ( *(_WORD *)(a1 + 8218) == (_WORD)result && !*(_BYTE *)(a1 + 8272) )
{
*(_BYTE *)(a1 + 8272) = 1;
v3 = func1(*(_WORD *)(a2 + 22));
v4 = DSI_privateDataByte(a2);
v5 = process_biop_service_gateway_info(*(unsigned __int16 *)(a1 + 8202), a1 + 8224, v4, v3);
result = add_dsmcc_pid(a1, v5);
}
return result;
}

original_code:
void
process_dsi(struct carousel *car, struct DownloadServerInitiate *dsi)
{
 uint16_t elementary_pid;
 verbose("DownloadServerInitiate");
 if(car->current_pid != car->boot_pid
 || car->got_dsi)
  return;
 car->got_dsi = 1;
 elementary_pid = process_biop_service_gateway_info(car->service_id, &car->assoc, DSI_privateDataByte(dsi), ntohs(dsi->privateDataLength));
 add_dsmcc_pid(car, elementary_pid);
 return;
}

canonical_original_code:
void process_dsi(struct carousel *car, struct DownloadServerInitiate *dsi)
{
 unsigned short elementary_pid;
 verbose("DownloadServerInitiate");
 if(car->current_pid != car->boot_pid
 || car->got_dsi)
  return;
 car->got_dsi = 1;
 elementary_pid = process_biop_service_gateway_info(car->service_id, &car->assoc, DSI_privateDataByte(dsi), ntohs(dsi->privateDataLength));
 add_dsmcc_pid(car, elementary_pid);
 return;
}

variable_types: struct DownloadServerInitiate { unsigned char serverId[20]; struct CompatibilityDescriptor { unsigned short compatibilityDescriptorLength; } compatibilityDescriptor; unsigned short privateDataLength; } *

user_defined_types: struct DownloadServerInitiate { unsigned char serverId[20]; struct CompatibilityDescriptor compatibilityDescriptor; unsigned short privateDataLength; }

user_defined_types: struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; }


decompiled_code:
__int64 __fastcall process_ddb(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned __int16 v6; // ax
unsigned __int16 v7; // ax
__int64 v8; // r14
unsigned __int16 v9; // ax
__int64 result; // rax
__int64 v11; // r12
unsigned __int16 v12; // ax

verbose("DownloadDataBlock");
vverbose("downloadId: %u", a3);
v6 = _bswap_16(*(_WORD *)a2);
vverbose("moduleId: %u", v6);
vverbose("moduleVersion: %u", *(unsigned __int8 *)(a2 + 2));
v7 = _bswap_16(*(_WORD *)(a2 + 4));
vverbose("blockNumber: %u", v7);
vverbose("blockLength: %u", a4);
v8 = DDB_blockDataByte(a2);
vhexdump(v8, a4);
v9 = _bswap_16(*(_WORD *)a2);
result = find_module(a1, v9, *(unsigned __int8 *)(a2 + 2), a3);
if ( result )
{
v11 = result;
v12 = _bswap_16(*(_WORD *)(a2 + 4));
result = download_block(a1, v11, v12, v8, a4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned __int16 v6; // ax
unsigned __int16 v7; // ax
__int64 v8; // r14
unsigned __int16 v9; // ax
__int64 result; // rax
__int64 v11; // r12
unsigned __int16 v12; // ax

verbose("DownloadDataBlock");
vverbose("downloadId: %u", a3);
v6 = func1(*(_WORD *)a2);
vverbose("moduleId: %u", v6);
vverbose("moduleVersion: %u", *(unsigned __int8 *)(a2 + 2));
v7 = func1(*(_WORD *)(a2 + 4));
vverbose("blockNumber: %u", v7);
vverbose("blockLength: %u", a4);
v8 = DDB_blockDataByte(a2);
vhexdump(v8, a4);
v9 = func1(*(_WORD *)a2);
result = find_module(a1, v9, *(unsigned __int8 *)(a2 + 2), a3);
if ( result )
{
v11 = result;
v12 = func1(*(_WORD *)(a2 + 4));
result = download_block(a1, v11, v12, v8, a4);
}
return result;
}

original_code:
void
process_ddb(struct carousel *car, struct DownloadDataBlock *ddb, uint32_t downloadId, uint32_t blockLength)
{
 unsigned char *block;
 struct module *mod;
 verbose("DownloadDataBlock");
 vverbose("downloadId: %u", downloadId);
 vverbose("moduleId: %u", ntohs(ddb->moduleId));
 vverbose("moduleVersion: %u", ddb->moduleVersion);
 vverbose("blockNumber: %u", ntohs(ddb->blockNumber));
 vverbose("blockLength: %u", blockLength);
 block = DDB_blockDataByte(ddb);
 vhexdump(block, blockLength);
 if((mod = find_module(car, ntohs(ddb->moduleId), ddb->moduleVersion, downloadId)) != ((void *)0))
  download_block(car, mod, ntohs(ddb->blockNumber), block, blockLength);
 return;
}

canonical_original_code:
void process_ddb(struct carousel *car, struct DownloadDataBlock *ddb, unsigned int downloadId, unsigned int blockLength)
{
 unsigned char *block;
 struct module *mod;
 verbose("DownloadDataBlock");
 vverbose("downloadId: %u", downloadId);
 vverbose("moduleId: %u", ntohs(ddb->moduleId));
 vverbose("moduleVersion: %u", ddb->moduleVersion);
 vverbose("blockNumber: %u", ntohs(ddb->blockNumber));
 vverbose("blockLength: %u", blockLength);
 block = DDB_blockDataByte(ddb);
 vhexdump(block, blockLength);
 if((mod = find_module(car, ntohs(ddb->moduleId), ddb->moduleVersion, downloadId)) != ((void *)0))
  download_block(car, mod, ntohs(ddb->blockNumber), block, blockLength);
 return;
}

variable_types: struct DownloadDataBlock { unsigned short moduleId; unsigned char moduleVersion; unsigned char reserved; unsigned short blockNumber; } *

variable_types:
downloadId
unsigned int

variable_types:
blockLength
unsigned int

variable_types:
block
unsigned char *

variable_types: struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *


decompiled_code:
int __fastcall load_carousel(__int64 a1)
{
unsigned int v1; // er12
unsigned __int16 v2; // ax
const char *v3; // rdx
unsigned int v4; // ebp
unsigned int *v5; // rax
__int64 v6; // rax
unsigned int v7; // er13
unsigned int v8; // ebp
__int64 v9; // rax
unsigned int v10; // ebp
struct timeval v12; // [rsp+0h] [rbp-1048h] BYREF
struct timeval tv; // [rsp+10h] [rbp-1038h] BYREF
char v14[8]; // [rsp+20h] [rbp-1028h] BYREF
char v15[2]; // [rsp+28h] [rbp-1020h] BYREF
__int16 v16; // [rsp+2Ah] [rbp-101Eh]
unsigned int v17; // [rsp+2Ch] [rbp-101Ch]

*(_DWORD *)(a1 + 8276) = 0;
*(_QWORD *)(a1 + 8280) = 0LL;
gettimeofday(&v12, 0LL);
gettimeofday(&tv, 0LL);
v1 = 0;
while ( 1 )
{
while ( 1 )
{
if ( !(unsigned __int8)read_dsmcc_tables(a1, v14) )
fatal("Unable to read PID");
if ( v15[0] == 17 && v15[1] == 3 )
{
v2 = _bswap_16(v16);
switch ( v2 )
{
case 0x1002u:
v4 = _bswap_32(v17);
v5 = (unsigned int *)dsmccMessage(v15);
process_dii(a1, v5, v4);
break;
case 0x1006u:
v6 = dsmccMessage(v15);
process_dsi(a1, v6);
break;
case 0x1003u:
v7 = DDB_blockDataLength(v15);
v8 = _bswap_32(v17);
v9 = dsmccMessage(v15);
process_ddb(a1, v9, v8, v7);
break;
default:
error((int)"Unknown DSMCC messageId: 0x%x", v2, v3);
break;
}
}
v10 = *(_DWORD *)(a1 + 8276);
if ( v10 == v1 )
break;
gettimeofday(&tv, 0LL);
v1 = v10;
}
if ( tv.tv_sec > 0 )
{
gettimeofday(&v12, 0LL);
if ( v12.tv_sec - tv.tv_sec > 180 )
break;
}
}
return printf("Downloaded %d modules\n", v1);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
unsigned int v1; // er12
unsigned __int16 v2; // ax
const char *v3; // rdx
unsigned int v4; // ebp
unsigned int *v5; // rax
__int64 v6; // rax
unsigned int v7; // er13
unsigned int v8; // ebp
__int64 v9; // rax
unsigned int v10; // ebp
struct timeval v12; // [rsp+0h] [rbp-1048h] BYREF
struct timeval tv; // [rsp+10h] [rbp-1038h] BYREF
char v14[8]; // [rsp+20h] [rbp-1028h] BYREF
char v15[2]; // [rsp+28h] [rbp-1020h] BYREF
__int16 v16; // [rsp+2Ah] [rbp-101Eh]
unsigned int v17; // [rsp+2Ch] [rbp-101Ch]

*(_DWORD *)(a1 + 8276) = 0;
*(_QWORD *)(a1 + 8280) = 0LL;
gettimeofday(&v12, 0LL);
gettimeofday(&tv, 0LL);
v1 = 0;
while ( 1 )
{
while ( 1 )
{
if ( !(unsigned __int8)read_dsmcc_tables(a1, v14) )
fatal("Unable to read PID");
if ( v15[0] == 17 && v15[1] == 3 )
{
v2 = func1(v16);
switch ( v2 )
{
case 0x1002u:
v4 = func0(v17);
v5 = (unsigned int *)dsmccMessage(v15);
func2(a1, v5, v4);
break;
case 0x1006u:
v6 = dsmccMessage(v15);
func3(a1, v6);
break;
case 0x1003u:
v7 = DDB_blockDataLength(v15);
v8 = func0(v17);
v9 = dsmccMessage(v15);
func4(a1, v9, v8, v7);
break;
default:
error((int)"Unknown DSMCC messageId: 0x%x", v2, v3);
break;
}
}
v10 = *(_DWORD *)(a1 + 8276);
if ( v10 == v1 )
break;
gettimeofday(&tv, 0LL);
v1 = v10;
}
if ( tv.tv_sec > 0 )
{
gettimeofday(&v12, 0LL);
if ( v12.tv_sec - tv.tv_sec > 180 )
break;
}
}
return printf("Downloaded %d modules\n", v1);
}

original_code:
void
load_carousel(struct carousel *car)
{
 unsigned char table[4096];
 _Bool done;
 car->nmodules = 0;
 car->modules = ((void *)0);
  uint32_t last_modules_n = 0;
  struct timeval lasttime, curtime;
  gettimeofday(&curtime, ((void *)0));
  gettimeofday(&lasttime, ((void *)0));
 done = 0;
 do
 {
  struct dsmccMessageHeader *dsmcc;
  if(!read_dsmcc_tables(car, table))
   fatal("Unable to read PID");
  dsmcc = (struct dsmccMessageHeader *) &table[8];
  if(dsmcc->protocolDiscriminator == 0x11
  && dsmcc->dsmccType == 0x03)
  {
   if(ntohs(dsmcc->messageId) == 0x1002)
    process_dii(car, (struct DownloadInfoIndication *) dsmccMessage(dsmcc), ntohl(dsmcc->transactionId));
   else if(ntohs(dsmcc->messageId) == 0x1006)
    process_dsi(car, (struct DownloadServerInitiate *) dsmccMessage(dsmcc));
   else if(ntohs(dsmcc->messageId) == 0x1003)
    process_ddb(car, (struct DownloadDataBlock *) dsmccMessage(dsmcc), ntohl(dsmcc->transactionId), DDB_blockDataLength(dsmcc));
   else
    error("Unknown DSMCC messageId: 0x%x", ntohs(dsmcc->messageId));
  }
    if (car->nmodules != last_modules_n)
    {
      last_modules_n = car->nmodules;
      gettimeofday(&lasttime, ((void *)0));
    } else if (lasttime.tv_sec > 0) {
      gettimeofday(&curtime, ((void *)0));
      if (curtime.tv_sec - lasttime.tv_sec > 180){
        done = 1;
        printf("Downloaded %d modules\n", last_modules_n);
      }
    }
 }
 while(!done);
 return;
}

canonical_original_code:
void load_carousel(struct carousel *car)
{
 unsigned char table[4096];
 _Bool done;
 car->nmodules = 0;
 car->modules = ((void *)0);
  unsigned int last_modules_n = 0;
  struct timeval lasttime; struct timeval curtime;
  gettimeofday(&curtime, ((void *)0));
  gettimeofday(&lasttime, ((void *)0));
 done = 0;
 do
 {
  struct dsmccMessageHeader *dsmcc;
  if(!read_dsmcc_tables(car, table))
   fatal("Unable to read PID");
  dsmcc = (struct dsmccMessageHeader *) &table[8];
  if(dsmcc->protocolDiscriminator == 0x11
  && dsmcc->dsmccType == 0x03)
  {
   if(ntohs(dsmcc->messageId) == 0x1002)
    process_dii(car, (struct DownloadInfoIndication *) dsmccMessage(dsmcc), ntohl(dsmcc->transactionId));
   else if(ntohs(dsmcc->messageId) == 0x1006)
    process_dsi(car, (struct DownloadServerInitiate *) dsmccMessage(dsmcc));
   else if(ntohs(dsmcc->messageId) == 0x1003)
    process_ddb(car, (struct DownloadDataBlock *) dsmccMessage(dsmcc), ntohl(dsmcc->transactionId), DDB_blockDataLength(dsmcc));
   else
    error("Unknown DSMCC messageId: 0x%x", ntohs(dsmcc->messageId));
  }
    if (car->nmodules != last_modules_n)
    {
      last_modules_n = car->nmodules;
      gettimeofday(&lasttime, ((void *)0));
    } else if (lasttime.tv_sec > 0) {
      gettimeofday(&curtime, ((void *)0));
      if (curtime.tv_sec - lasttime.tv_sec > 180){
        done = 1;
        printf("Downloaded %d modules\n", last_modules_n);
      }
    }
 }
 while(!done);
 return;
}

variable_types:
table
unsigned char [4096]

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

variable_types: struct dsmccMessageHeader { unsigned char protocolDiscriminator; unsigned char dsmccType; unsigned short messageId; unsigned int transactionId; unsigned char reserved; unsigned char adaptionLength; unsigned short messageLength; } *

user_defined_types: struct DownloadDataBlock { unsigned short moduleId; unsigned char moduleVersion; unsigned char reserved; unsigned short blockNumber; }

user_defined_types: struct CompatibilityDescriptor { unsigned short compatibilityDescriptorLength; }


original_code:
void
init_assoc(struct assoc *a)
{
 a->nassocs = 0;
 a->pids = ((void *)0);
 a->sids = ((void *)0);
 a->types = ((void *)0);
 return;
}

variable_types: struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } *


original_code:
void
add_assoc(struct assoc *a, uint16_t elementary_pid, uint16_t stream_id, uint8_t stream_type)
{
 a->nassocs ++;
 a->pids = safe_realloc(a->pids, a->nassocs * sizeof(uint16_t));
 a->sids = safe_realloc(a->sids, a->nassocs * sizeof(uint16_t));
 a->types = safe_realloc(a->types, a->nassocs * sizeof(uint8_t));
 a->pids[a->nassocs - 1] = elementary_pid;
 a->sids[a->nassocs - 1] = stream_id;
 a->types[a->nassocs - 1] = stream_type;
 return;
}

canonical_original_code:
void add_assoc(struct assoc *a, unsigned short elementary_pid, unsigned short stream_id, unsigned char stream_type)
{
 a->nassocs ++;
 a->pids = safe_realloc(a->pids, a->nassocs * sizeof(unsigned short));
 a->sids = safe_realloc(a->sids, a->nassocs * sizeof(unsigned short));
 a->types = safe_realloc(a->types, a->nassocs * sizeof(unsigned char));
 a->pids[a->nassocs - 1] = elementary_pid;
 a->sids[a->nassocs - 1] = stream_id;
 a->types[a->nassocs - 1] = stream_type;
 return;
}


decompiled_code:
__int64 __fastcall stream2pid(unsigned int *a1, unsigned __int16 a2)
{
const char *v2; // rdx
const char *v3; // rax
__int64 result; // rax

v2 = (const char *)*a1;
if ( (_DWORD)v2 )
{
v2 = (const char *)(2LL * (unsigned int)v2);
v3 = 0LL;
while ( *(_WORD *)&v3[*((_QWORD *)a1 + 2)] != a2 )
{
v3 += 2;
if ( v3 == v2 )
goto LABEL_5;
}
result = *(unsigned __int16 *)&v3[*((_QWORD *)a1 + 1)];
}
else
{
LABEL_5:
error((int)"Unknown PID for association tag %u", a2, v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, unsigned __int16 a2)
{
const char *v2; // rdx
const char *v3; // rax
__int64 result; // rax

v2 = (const char *)*a1;
if ( (_DWORD)v2 )
{
v2 = (const char *)(2LL * (unsigned int)v2);
v3 = 0LL;
while ( *(_WORD *)&v3[*((_QWORD *)a1 + 2)] != a2 )
{
v3 += 2;
if ( v3 == v2 )
goto LABEL_5;
}
result = *(unsigned __int16 *)&v3[*((_QWORD *)a1 + 1)];
}
else
{
LABEL_5:
error((int)"Unknown PID for association tag %u", a2, v2);
result = 0LL;
}
return result;
}

original_code:
uint16_t
stream2pid(struct assoc *a, uint16_t stream_id)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->sids[i] == stream_id)
   return a->pids[i];
 }
 error("Unknown PID for association tag %u", stream_id);
 return 0;
}

canonical_original_code:
unsigned short stream2pid(struct assoc *a, unsigned short stream_id)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->sids[i] == stream_id)
   return a->pids[i];
 }
 error("Unknown PID for association tag %u", stream_id);
 return 0;
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall streamType2pid(unsigned int *a1, unsigned __int8 a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *a1;
if ( (_DWORD)v2 )
{
v2 = (unsigned int)v2;
v3 = 0LL;
while ( *(_BYTE *)(*((_QWORD *)a1 + 3) + v3) != a2 )
{
if ( ++v3 == (unsigned int)v2 )
goto LABEL_5;
}
result = *(unsigned __int16 *)(*((_QWORD *)a1 + 1) + 2 * v3);
}
else
{
LABEL_5:
error((int)"Unknown PID for stream type %u", a2, (const char *)v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1, unsigned __int8 a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *a1;
if ( (_DWORD)v2 )
{
v2 = (unsigned int)v2;
v3 = 0LL;
while ( *(_BYTE *)(*((_QWORD *)a1 + 3) + v3) != a2 )
{
if ( ++v3 == (unsigned int)v2 )
goto LABEL_5;
}
result = *(unsigned __int16 *)(*((_QWORD *)a1 + 1) + 2 * v3);
}
else
{
LABEL_5:
error((int)"Unknown PID for stream type %u", a2, (const char *)v2);
result = 0LL;
}
return result;
}

original_code:
uint16_t
streamType2pid(struct assoc *a, uint8_t stream_type)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->types[i] == stream_type)
   return a->pids[i];
 }
 error("Unknown PID for stream type %u", stream_type);
 return 0;
}

canonical_original_code:
unsigned short streamType2pid(struct assoc *a, unsigned char stream_type)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->types[i] == stream_type)
   return a->pids[i];
 }
 error("Unknown PID for stream type %u", stream_type);
 return 0;
}


decompiled_code:
__int64 __fastcall stream2type(unsigned int *a1, unsigned __int16 a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *a1;
if ( (_DWORD)v2 )
{
v2 = (unsigned int)v2;
v3 = 0LL;
while ( *(_WORD *)(*((_QWORD *)a1 + 2) + 2 * v3) != a2 )
{
if ( ++v3 == (unsigned int)v2 )
goto LABEL_5;
}
result = *(unsigned __int8 *)(*((_QWORD *)a1 + 3) + v3);
}
else
{
LABEL_5:
error((int)"Unknown stream type for association tag %u", a2, (const char *)v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, unsigned __int16 a2)
{
unsigned __int64 v2; // rdx
__int64 v3; // rax
__int64 result; // rax

v2 = *a1;
if ( (_DWORD)v2 )
{
v2 = (unsigned int)v2;
v3 = 0LL;
while ( *(_WORD *)(*((_QWORD *)a1 + 2) + 2 * v3) != a2 )
{
if ( ++v3 == (unsigned int)v2 )
goto LABEL_5;
}
result = *(unsigned __int8 *)(*((_QWORD *)a1 + 3) + v3);
}
else
{
LABEL_5:
error((int)"Unknown stream type for association tag %u", a2, (const char *)v2);
result = 0LL;
}
return result;
}

original_code:
uint8_t
stream2type(struct assoc *a, uint16_t stream_id)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->sids[i] == stream_id)
   return a->types[i];
 }
 error("Unknown stream type for association tag %u", stream_id);
 return 0;
}

canonical_original_code:
unsigned char stream2type(struct assoc *a, unsigned short stream_id)
{
 unsigned int i;
 for(i=0; i<a->nassocs; i++)
 {
  if(a->sids[i] == stream_id)
   return a->types[i];
 }
 error("Unknown stream type for association tag %u", stream_id);
 return 0;
}

return_type: unsigned char


dsmccMessage

decompiled_code:
__int64 __fastcall dsmccMessage(__int64 a1)
{
return *(unsigned __int8 *)(a1 + 9) + a1 + 12;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(unsigned __int8 *)(a1 + 9) + a1 + 12;
}

original_code:
unsigned char *
dsmccMessage(struct dsmccMessageHeader *hdr)
{
 unsigned char *byte = (unsigned char *) hdr;
 unsigned int offset;
 offset = sizeof(struct dsmccMessageHeader);
 offset += hdr->adaptionLength;
 return &byte[offset];
}

canonical_original_code:
unsigned char *dsmccMessage(struct dsmccMessageHeader *hdr)
{
 unsigned char *byte = (unsigned char *) hdr;
 unsigned int offset;
 offset = sizeof(struct dsmccMessageHeader);
 offset += hdr->adaptionLength;
 return &byte[offset];
}

variable_types:
byte
unsigned char *

variable_types:
offset
unsigned int

return_type: unsigned char *

user_defined_types: struct dsmccMessageHeader { unsigned char protocolDiscriminator; unsigned char dsmccType; unsigned short messageId; unsigned int transactionId; unsigned char reserved; unsigned char adaptionLength; unsigned short messageLength; }


decompiled_code:
__int64 __fastcall DII_numberOfModules(__int64 a1)
{
unsigned __int16 v1; // ax

v1 = _bswap_16(*(_WORD *)(a1 + 16));
return (*(unsigned __int8 *)(a1 + (unsigned int)v1 + 18) << 8)
+ (unsigned int)*(unsigned __int8 *)(a1 + (unsigned int)v1 + 19);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned __int16 v1; // ax

v1 = func0(*(_WORD *)(a1 + 16));
return (*(unsigned __int8 *)(a1 + (unsigned int)v1 + 18) << 8)
+ (unsigned int)*(unsigned __int8 *)(a1 + (unsigned int)v1 + 19);
}

original_code:
uint16_t
DII_numberOfModules(struct DownloadInfoIndication *dii)
{
 unsigned char *byte = (unsigned char *) dii;
 unsigned int offset;
 offset = sizeof(struct DownloadInfoIndication);
 offset += ntohs(dii->compatibilityDescriptor.compatibilityDescriptorLength);
 return (byte[offset] << 8) + byte[offset + 1];
}

canonical_original_code:
unsigned short DII_numberOfModules(struct DownloadInfoIndication *dii)
{
 unsigned char *byte = (unsigned char *) dii;
 unsigned int offset;
 offset = sizeof(struct DownloadInfoIndication);
 offset += ntohs(dii->compatibilityDescriptor.compatibilityDescriptorLength);
 return (byte[offset] << 8) + byte[offset + 1];
}

variable_types: struct DownloadInfoIndication { unsigned int downloadId; unsigned short blockSize; unsigned char windowSize; unsigned char ackPeriod; unsigned int tCDownloadWindow; unsigned int tCDownloadScenario; struct CompatibilityDescriptor { unsigned short compatibilityDescriptorLength; } compatibilityDescriptor; } *


decompiled_code:
__int64 __fastcall DII_module(__int64 a1, unsigned __int16 a2)
{
unsigned __int16 v2; // bx
unsigned int v3; // eax

v2 = a2;
if ( (unsigned __int16)DII_numberOfModules(a1) <= a2 )
fatal("DII_module: index too large");
v3 = (unsigned __int16)_bswap_16(*(_WORD *)(a1 + 16)) + 20;
if ( a2 )
{
do
{
v3 += *(unsigned __int8 *)(a1 + v3 + 7) + 8;
--v2;
}
while ( v2 );
}
return a1 + v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int16 a2)
{
unsigned __int16 v2; // bx
unsigned int v3; // eax

v2 = a2;
if ( (unsigned __int16)func2(a1) <= a2 )
fatal("DII_module: index too large");
v3 = (unsigned __int16)func0(*(_WORD *)(a1 + 16)) + 20;
if ( a2 )
{
do
{
v3 += *(unsigned __int8 *)(a1 + v3 + 7) + 8;
--v2;
}
while ( v2 );
}
return a1 + v3;
}

original_code:
struct DIIModule *
DII_module(struct DownloadInfoIndication *dii, uint16_t n)
{
 unsigned char *byte = (unsigned char *) dii;
 unsigned int offset;
 struct DIIModule *mod;
 if(n >= DII_numberOfModules(dii))
  fatal("DII_module: index too large");
 offset = sizeof(struct DownloadInfoIndication);
 offset += ntohs(dii->compatibilityDescriptor.compatibilityDescriptorLength);
 offset += sizeof(uint16_t);
 while(n != 0)
 {
  n --;
  mod = (struct DIIModule *) &byte[offset];
  offset += sizeof(struct DIIModule);
  offset += mod->moduleInfoLength;
 }
 return (struct DIIModule *) &byte[offset];
}

canonical_original_code:
struct DIIModule *DII_module(struct DownloadInfoIndication *dii, unsigned short n)
{
 unsigned char *byte = (unsigned char *) dii;
 unsigned int offset;
 struct DIIModule *mod;
 if(n >= DII_numberOfModules(dii))
  fatal("DII_module: index too large");
 offset = sizeof(struct DownloadInfoIndication);
 offset += ntohs(dii->compatibilityDescriptor.compatibilityDescriptorLength);
 offset += sizeof(unsigned short);
 while(n != 0)
 {
  n --;
  mod = (struct DIIModule *) &byte[offset];
  offset += sizeof(struct DIIModule);
  offset += mod->moduleInfoLength;
 }
 return (struct DIIModule *) &byte[offset];
}

variable_types:
n
unsigned short

return_type: struct DIIModule { unsigned short moduleId; unsigned int moduleSize; unsigned char moduleVersion; unsigned char moduleInfoLength; } *


decompiled_code:
__int64 __fastcall DDB_blockDataLength(__int64 a1)
{
return (unsigned __int16)_bswap_16(*(_WORD *)(a1 + 10)) - (unsigned int)*(unsigned __int8 *)(a1 + 9) - 6;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return (unsigned __int16)func0(*(_WORD *)(a1 + 10)) - (unsigned int)*(unsigned __int8 *)(a1 + 9) - 6;
}

original_code:
uint32_t
DDB_blockDataLength(struct dsmccMessageHeader *hdr)
{
 uint32_t length;
 length = ntohs(hdr->messageLength);
 length -= hdr->adaptionLength;
 length -= sizeof(struct DownloadDataBlock);
 return length;
}

canonical_original_code:
unsigned int DDB_blockDataLength(struct dsmccMessageHeader *hdr)
{
 unsigned int length;
 length = ntohs(hdr->messageLength);
 length -= hdr->adaptionLength;
 length -= sizeof(struct DownloadDataBlock);
 return length;
}

variable_types:
length
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall DDB_blockDataByte(__int64 a1)
{
return a1 + 6;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return a1 + 6;
}

original_code:
unsigned char *
DDB_blockDataByte(struct DownloadDataBlock *ddb)
{
 return ((unsigned char *) ddb) + sizeof(struct DownloadDataBlock);
}

canonical_original_code:
unsigned char *DDB_blockDataByte(struct DownloadDataBlock *ddb)
{
 return ((unsigned char *) ddb) + sizeof(struct DownloadDataBlock);
}


decompiled_code:
__int64 __fastcall DSI_privateDataByte(__int64 a1)
{
if ( *(_WORD *)(a1 + 20) )
fatal("Unexpected DSI format");
return a1 + 24;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
if ( *(_WORD *)(a1 + 20) )
fatal("Unexpected DSI format");
return a1 + 24;
}

original_code:
unsigned char *
DSI_privateDataByte(struct DownloadServerInitiate *dsi)
{
 if(dsi->compatibilityDescriptor.compatibilityDescriptorLength != 0)
  fatal("Unexpected DSI format");
 return ((unsigned char *) dsi) + sizeof(struct DownloadServerInitiate);
}

canonical_original_code:
unsigned char *DSI_privateDataByte(struct DownloadServerInitiate *dsi)
{
 if(dsi->compatibilityDescriptor.compatibilityDescriptorLength != 0)
  fatal("Unexpected DSI format");
 return ((unsigned char *) dsi) + sizeof(struct DownloadServerInitiate);
}


decompiled_code:
char *__fastcall make_carousel_root(unsigned __int16 a1, unsigned int a2)
{
int v4; // edi
char *v5; // rax
int v6; // edi
char *v7; // rax
int v8; // edi
char *v9; // rax

strcpy(carousel_root, "carousels");
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v4 = *_errno_location();
if ( v4 != 17 )
{
v5 = strerror(v4);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v5);
}
}
snprintf(carousel_root, 0x1000uLL, "%s/%u", "carousels", a1);
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v6 = *_errno_location();
if ( v6 != 17 )
{
v7 = strerror(v6);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v7);
}
}
snprintf(carousel_root, 0x1000uLL, "%s/%u/%u", "carousels", a1, a2);
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v8 = *_errno_location();
if ( v8 != 17 )
{
v9 = strerror(v8);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v9);
}
}
return carousel_root;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned __int16 a1, unsigned int a2)
{
int v4; // edi
char *v5; // rax
int v6; // edi
char *v7; // rax
int v8; // edi
char *v9; // rax

strcpy(carousel_root, "carousels");
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v4 = *_errno_location();
if ( v4 != 17 )
{
v5 = strerror(v4);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v5);
}
}
snprintf(carousel_root, 0x1000uLL, "%s/%u", "carousels", a1);
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v6 = *_errno_location();
if ( v6 != 17 )
{
v7 = strerror(v6);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v7);
}
}
snprintf(carousel_root, 0x1000uLL, "%s/%u/%u", "carousels", a1, a2);
if ( mkdir(carousel_root, 0x1EDu) < 0 )
{
v8 = *_errno_location();
if ( v8 != 17 )
{
v9 = strerror(v8);
fatal("Unable to create carousel directory '%s': %s", carousel_root, v9);
}
}
return carousel_root;
}

original_code:
char *
make_carousel_root(uint16_t elementary_pid, uint32_t carousel_id)
{
 snprintf(_carousel_root, sizeof(_carousel_root), "%s", "carousels");
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 snprintf(_carousel_root, sizeof(_carousel_root), "%s/%u", "carousels", elementary_pid);
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 snprintf(_carousel_root, sizeof(_carousel_root), "%s/%u/%u", "carousels", elementary_pid, carousel_id);
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 return _carousel_root;
}

canonical_original_code:
char *make_carousel_root(unsigned short elementary_pid, unsigned int carousel_id)
{
 snprintf(_carousel_root, sizeof(_carousel_root), "%s", "carousels");
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 snprintf(_carousel_root, sizeof(_carousel_root), "%s/%u", "carousels", elementary_pid);
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 snprintf(_carousel_root, sizeof(_carousel_root), "%s/%u/%u", "carousels", elementary_pid, carousel_id);
 if(mkdir(_carousel_root, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create carousel directory '%s': %s", _carousel_root, strerror((*__errno_location ())));
 return _carousel_root;
}

return_type: char *


decompiled_code:
char *__fastcall convert_key(__int64 a1, int a2)
{
__int64 i; // rbx

if ( (unsigned int)(2 * a2 + 1) > 0x1000 )
fatal("objectKey too long");
if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
ascii_key[2 * i] = hex_digit(*(_BYTE *)(a1 + i) >> 4);
byte_621[2 * i] = hex_digit(*(_BYTE *)(a1 + i) & 0xF);
}
}
ascii_key[2 * a2] = 0;
return ascii_key;
}

canonical_decompiled_code:
char *__fastcall func1(__int64 a1, int a2)
{
__int64 i; // rbx

if ( (unsigned int)(2 * a2 + 1) > 0x1000 )
fatal("objectKey too long");
if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
ascii_key[2 * i] = hex_digit(*(_BYTE *)(a1 + i) >> 4);
byte_621[2 * i] = hex_digit(*(_BYTE *)(a1 + i) & 0xF);
}
}
ascii_key[2 * a2] = 0;
return ascii_key;
}

original_code:
char *
convert_key(char *key, uint32_t size)
{
 uint32_t i;
 if(sizeof(_ascii_key) < (size * 2) + 1)
  fatal("objectKey too long");
 for(i=0; i<size; i++)
 {
  _ascii_key[i * 2] = hex_digit((key[i] >> 4) & 0x0f);
  _ascii_key[(i * 2) + 1] = hex_digit(key[i] & 0x0f);
 }
 _ascii_key[size * 2] = '\0';
 return _ascii_key;
}

canonical_original_code:
char *convert_key(char *key, unsigned int size)
{
 unsigned int i;
 if(sizeof(_ascii_key) < (size * 2) + 1)
  fatal("objectKey too long");
 for(i=0; i<size; i++)
 {
  _ascii_key[i * 2] = hex_digit((key[i] >> 4) & 0x0f);
  _ascii_key[(i * 2) + 1] = hex_digit(key[i] & 0x0f);
 }
 _ascii_key[size * 2] = '\0';
 return _ascii_key;
}

variable_types:
key
char *

variable_types:
size
unsigned int

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall make_service_root(unsigned __int16 a1, const char *a2, unsigned __int16 a3, unsigned int a4, unsigned __int16 a5, __int64 a6, int a7)
{
char *v12; // rax
int v14; // edi
char *v15; // rax
int v16; // edi
char *v17; // rax
char v18[4096]; // [rsp+0h] [rbp-3038h] BYREF
char s[4096]; // [rsp+1000h] [rbp-2038h] BYREF
char path[4152]; // [rsp+2000h] [rbp-1038h] BYREF

strcpy(path, "services");
if ( mkdir(path, 0x1EDu) < 0 )
{
v14 = *_errno_location();
if ( v14 != 17 )
{
v15 = strerror(v14);
fatal("Unable to create services directory '%s': %s", path, v15);
}
}
v12 = convert_key(a6, a7);
snprintf(s, 0x1000uLL, "../%s/%u/%u/%s-%u-%s", "carousels", a3, a4, a2, a5, v12);
snprintf(v18, 0x1000uLL, "%s/%u", path, a1);
unlink(v18);
if ( symlink(s, v18) < 0 )
{
v16 = *_errno_location();
if ( v16 != 17 )
{
v17 = strerror(v16);
fatal("Unable to create link '%s' to '%s': %s", v18, s, v17);
}
}
return verbose("Added service root '%s' -> '%s'", v18, s);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, const char *a2, unsigned __int16 a3, unsigned int a4, unsigned __int16 a5, __int64 a6, int a7)
{
char *v12; // rax
int v14; // edi
char *v15; // rax
int v16; // edi
char *v17; // rax
char v18[4096]; // [rsp+0h] [rbp-3038h] BYREF
char s[4096]; // [rsp+1000h] [rbp-2038h] BYREF
char path[4152]; // [rsp+2000h] [rbp-1038h] BYREF

strcpy(path, "services");
if ( mkdir(path, 0x1EDu) < 0 )
{
v14 = *_errno_location();
if ( v14 != 17 )
{
v15 = strerror(v14);
fatal("Unable to create services directory '%s': %s", path, v15);
}
}
v12 = func1(a6, a7);
snprintf(s, 0x1000uLL, "../%s/%u/%u/%s-%u-%s", "carousels", a3, a4, a2, a5, v12);
snprintf(v18, 0x1000uLL, "%s/%u", path, a1);
unlink(v18);
if ( symlink(s, v18) < 0 )
{
v16 = *_errno_location();
if ( v16 != 17 )
{
v17 = strerror(v16);
fatal("Unable to create link '%s' to '%s': %s", v18, s, v17);
}
}
return verbose("Added service root '%s' -> '%s'", v18, s);
}

original_code:
void
make_service_root(uint16_t service_id, char *kind, uint16_t elementary_pid, uint32_t carousel_id, uint16_t module_id, char *key, uint32_t key_size)
{
 char dirname[4096];
 char *ascii_key;
 char realfile[4096];
 char linkfile[4096];
 snprintf(dirname, sizeof(dirname), "%s", "services");
 if(mkdir(dirname, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create services directory '%s': %s", dirname, strerror((*__errno_location ())));
 ascii_key = convert_key(key, key_size);
 snprintf(realfile, sizeof(realfile), "../%s/%u/%u/%s-%u-%s", "carousels", elementary_pid, carousel_id, kind, module_id, ascii_key);
 snprintf(linkfile, sizeof(linkfile), "%s/%u", dirname, service_id);
 unlink(linkfile);
 if(symlink(realfile, linkfile) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create link '%s' to '%s': %s", linkfile, realfile, strerror((*__errno_location ())));
 verbose("Added service root '%s' -> '%s'", linkfile, realfile);
 return;
}

canonical_original_code:
void make_service_root(unsigned short service_id, char *kind, unsigned short elementary_pid, unsigned int carousel_id, unsigned short module_id, char *key, unsigned int key_size)
{
 char dirname[4096];
 char *ascii_key;
 char realfile[4096];
 char linkfile[4096];
 snprintf(dirname, sizeof(dirname), "%s", "services");
 if(mkdir(dirname, 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create services directory '%s': %s", dirname, strerror((*__errno_location ())));
 ascii_key = convert_key(key, key_size);
 snprintf(realfile, sizeof(realfile), "../%s/%u/%u/%s-%u-%s", "carousels", elementary_pid, carousel_id, kind, module_id, ascii_key);
 snprintf(linkfile, sizeof(linkfile), "%s/%u", dirname, service_id);
 unlink(linkfile);
 if(symlink(realfile, linkfile) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create link '%s' to '%s': %s", linkfile, realfile, strerror((*__errno_location ())));
 verbose("Added service root '%s' -> '%s'", linkfile, realfile);
 return;
}

variable_types:
kind
char *

variable_types:
dirname
char [4096]

variable_types:
realfile
char [4096]

variable_types:
linkfile
char [4096]


decompiled_code:
__int64 __fastcall add_dir_entry(const char *a1, const char *a2, int a3, const char *a4, unsigned __int16 a5, unsigned int a6, unsigned __int16 a7, __int64 a8, int a9)
{
char *v13; // rax
int *v15; // rax
char *v16; // rax
char name[4096]; // [rsp+10h] [rbp-2038h] BYREF
char s[4152]; // [rsp+1010h] [rbp-1038h] BYREF

v13 = convert_key(a8, a9);
snprintf(s, 0x1000uLL, "../../../../%s/%u/%u/%s-%u-%s", "carousels", a5, a6, a4, a7, v13);
snprintf(name, 0x1000uLL, "%s/%.*s", a1, a3, a2);
unlink(name);
if ( symlink(s, name) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
fatal("Unable to create link '%s' to '%s': %s", name, s, v16);
}
return verbose("Added directory entry '%s' -> '%s'", name, s);
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, const char *a2, int a3, const char *a4, unsigned __int16 a5, unsigned int a6, unsigned __int16 a7, __int64 a8, int a9)
{
char *v13; // rax
int *v15; // rax
char *v16; // rax
char name[4096]; // [rsp+10h] [rbp-2038h] BYREF
char s[4152]; // [rsp+1010h] [rbp-1038h] BYREF

v13 = func1(a8, a9);
snprintf(s, 0x1000uLL, "../../../../%s/%u/%u/%s-%u-%s", "carousels", a5, a6, a4, a7, v13);
snprintf(name, 0x1000uLL, "%s/%.*s", a1, a3, a2);
unlink(name);
if ( symlink(s, name) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
fatal("Unable to create link '%s' to '%s': %s", name, s, v16);
}
return verbose("Added directory entry '%s' -> '%s'", name, s);
}

original_code:
void
add_dir_entry(char *dir, char *entry, uint32_t entry_size, char *kind, uint16_t elementary_pid, uint32_t carousel_id, uint16_t module_id, char *key, uint32_t key_size)
{
 char *ascii_key;
 char realfile[4096];
 char linkfile[4096];
 ascii_key = convert_key(key, key_size);
 snprintf(realfile, sizeof(realfile), "../../../../%s/%u/%u/%s-%u-%s", "carousels", elementary_pid, carousel_id, kind, module_id, ascii_key);
 snprintf(linkfile, sizeof(linkfile), "%s/%.*s", dir, entry_size, entry);
 unlink(linkfile);
 if(symlink(realfile, linkfile) < 0)
  fatal("Unable to create link '%s' to '%s': %s", linkfile, realfile, strerror((*__errno_location ())));
 verbose("Added directory entry '%s' -> '%s'", linkfile, realfile);
 return;
}

canonical_original_code:
void add_dir_entry(char *dir, char *entry, unsigned int entry_size, char *kind, unsigned short elementary_pid, unsigned int carousel_id, unsigned short module_id, char *key, unsigned int key_size)
{
 char *ascii_key;
 char realfile[4096];
 char linkfile[4096];
 ascii_key = convert_key(key, key_size);
 snprintf(realfile, sizeof(realfile), "../../../../%s/%u/%u/%s-%u-%s", "carousels", elementary_pid, carousel_id, kind, module_id, ascii_key);
 snprintf(linkfile, sizeof(linkfile), "%s/%.*s", dir, entry_size, entry);
 unlink(linkfile);
 if(symlink(realfile, linkfile) < 0)
  fatal("Unable to create link '%s' to '%s': %s", linkfile, realfile, strerror((*__errno_location ())));
 verbose("Added directory entry '%s' -> '%s'", linkfile, realfile);
 return;
}

variable_types:
dir
char *

variable_types:
entry
char *


decompiled_code:
__int64 __fastcall get_host_addr(const char *a1, _DWORD *a2)
{
struct hostent *v2; // rax

v2 = gethostbyname(a1);
if ( !v2 )
return 0xFFFFFFFFLL;
if ( v2->h_addrtype != 2 )
return 0xFFFFFFFFLL;
*a2 = **(_DWORD **)v2->h_addr_list;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, _DWORD *a2)
{
struct hostent *v2; // rax

v2 = gethostbyname(a1);
if ( !v2 )
return 0xFFFFFFFFLL;
if ( v2->h_addrtype != 2 )
return 0xFFFFFFFFLL;
*a2 = **(_DWORD **)v2->h_addr_list;
return 0LL;
}

original_code:
static int
get_host_addr(char *host, struct in_addr *output)
{
 struct hostent *he;
 int error = 0;
 if(((he = gethostbyname(host)) != ((void *)0)) && (he->h_addrtype == 2))
  memcpy(output, he->h_addr_list[0], sizeof(struct in_addr));
 else
  error = -1;
 return error;
}

variable_types:
host
char *

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: error int

return_type: int


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

if ( a1 == 17 )
result = wait(0LL);
return result;
}

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

if ( a1 == 17 )
result = wait(0LL);
return result;
}

original_code:
static void
dead_child(int signo)
{
 if(signo == 17)
  wait(((void *)0));
 return;
}

variable_types: signo int


decompiled_code:
__int64 __fastcall parse_addr(char *nptr, _DWORD *a2, __int16 *a3)
{
char *v4; // rax
char *v5; // rbx
const char *v6; // r14
__int16 v7; // ax
__int64 result; // rax
char v9; // bl
const unsigned __int16 *v10; // rcx
char *v11; // rdx
unsigned __int16 v12; // ax
__int16 v13; // ax
__int16 v14; // ax

if ( !nptr )
return 0LL;
v4 = strchr(nptr, 58);
v5 = v4;
if ( v4 )
{
v6 = v4 + 1;
*v4 = 0;
if ( (int)get_host_addr(nptr, a2) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v7 = atoi(v6);
*a3 = _bswap_16(v7);
*v5 = 58;
result = 0LL;
}
}
else
{
v9 = *nptr;
if ( !*nptr )
goto LABEL_11;
v10 = *_ctype_b_loc();
v11 = nptr;
do
{
v12 = v10[v9];
v9 = *++v11;
v13 = v12 & 0x800;
}
while ( v13 && v9 );
if ( !v13 )
{
result = (unsigned int)((int)get_host_addr(nptr, a2) >> 31);
}
else
{
LABEL_11:
v14 = atoi(nptr);
*a3 = _bswap_16(v14);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *nptr, _DWORD *a2, __int16 *a3)
{
char *v4; // rax
char *v5; // rbx
const char *v6; // r14
__int16 v7; // ax
__int64 result; // rax
char v9; // bl
const unsigned __int16 *v10; // rcx
char *v11; // rdx
unsigned __int16 v12; // ax
__int16 v13; // ax
__int16 v14; // ax

if ( !nptr )
return 0LL;
v4 = strchr(nptr, 58);
v5 = v4;
if ( v4 )
{
v6 = v4 + 1;
*v4 = 0;
if ( (int)func0(nptr, a2) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v7 = atoi(v6);
*a3 = func1(v7);
*v5 = 58;
result = 0LL;
}
}
else
{
v9 = *nptr;
if ( !*nptr )
goto LABEL_11;
v10 = *_ctype_b_loc();
v11 = nptr;
do
{
v12 = v10[v9];
v9 = *++v11;
v13 = v12 & 0x800;
}
while ( v13 && v9 );
if ( !v13 )
{
result = (unsigned int)((int)func0(nptr, a2) >> 31);
}
else
{
LABEL_11:
v14 = atoi(nptr);
*a3 = func1(v14);
result = 0LL;
}
}
return result;
}

original_code:
int
parse_addr(char *str, struct in_addr *ip, in_port_t *port)
{
 char *p;
 int ishost;
 if(str == ((void *)0))
  return 0;
 if((p = strchr(str, ':')) != ((void *)0))
 {
  *(p++) = '\0';
  if(get_host_addr(str, ip) < 0)
   return -1;
  *port = htons(atoi(p));
  *(--p) = ':';
 }
 else
 {
  ishost = 0;
  for(p=str; *p && !ishost; p++)
   ishost = !((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit);
  if(ishost)
  {
   if(get_host_addr(str, ip) < 0)
    return -1;
  }
  else
  {
   *port = htons(atoi(str));
  }
 }
 return 0;
}

canonical_original_code:
int parse_addr(char *str, struct in_addr *ip, unsigned short *port)
{
 char *p;
 int ishost;
 if(str == ((void *)0))
  return 0;
 if((p = strchr(str, ':')) != ((void *)0))
 {
  *(p++) = '\0';
  if(get_host_addr(str, ip) < 0)
   return -1;
  *port = htons(atoi(p));
  *(--p) = ':';
 }
 else
 {
  ishost = 0;
  for(p=str; *p && !ishost; p++)
   ishost = !((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit);
  if(ishost)
  {
   if(get_host_addr(str, ip) < 0)
    return -1;
  }
  else
  {
   *port = htons(atoi(str));
  }
 }
 return 0;
}

variable_types:
str
char *

variable_types:
port
unsigned short *

variable_types:
p
char *

variable_types: ishost int


decompiled_code:
void __fastcall __noreturn start_downloader(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned __int16 a6, unsigned int a7)
{
unsigned int v10; // er12
const char *v11; // rdx
__int64 v12; // rbx
int v13[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v13[0] = 0;
v10 = a6;
if ( !(unsigned __int8)tune_service_id(a1, a2, a5, a6, v13) )
error((int)"Unable to retune; let's hope you're already tuned to the right frequency...", a2, v11);
v12 = find_mheg((unsigned int)a1, a3, a4, a5, v10, a7);
verbose("Carousel ID=%u", *(unsigned int *)(v12 + 8204));
verbose("Boot PID=%u", *(unsigned __int16 *)(v12 + 8208));
verbose("Video PID=%u", *(unsigned __int16 *)(v12 + 8214));
verbose("Audio PID=%u", *(unsigned __int16 *)(v12 + 8210));
load_carousel(v12);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned __int16 a6, unsigned int a7)
{
unsigned int v10; // er12
const char *v11; // rdx
__int64 v12; // rbx
int v13[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v13[0] = 0;
v10 = a6;
if ( !(unsigned __int8)tune_service_id(a1, a2, a5, a6, v13) )
error((int)"Unable to retune; let's hope you're already tuned to the right frequency...", a2, v11);
v12 = find_mheg((unsigned int)a1, a3, a4, a5, v10, a7);
verbose("Carousel ID=%u", *(unsigned int *)(v12 + 8204));
verbose("Boot PID=%u", *(unsigned __int16 *)(v12 + 8208));
verbose("Video PID=%u", *(unsigned __int16 *)(v12 + 8214));
verbose("Audio PID=%u", *(unsigned __int16 *)(v12 + 8210));
load_carousel(v12);
exit(0);
}

original_code:
struct carousel *
start_downloader(unsigned int adapter, unsigned int frontend, unsigned int demux, unsigned int dvr, unsigned int timeout, uint16_t service_id, int carousel_id)
{
 struct carousel *car;
 pid_t child;
  int tuner_fd = 0;
  if(!tune_service_id(adapter, frontend, timeout, service_id, &tuner_fd))
    error("Unable to retune; let's hope you're already tuned to the right frequency...");
  car = find_mheg(adapter, demux, dvr, timeout, service_id, carousel_id);
  verbose("Carousel ID=%u", car->carousel_id);
  verbose("Boot PID=%u", car->boot_pid);
  verbose("Video PID=%u", car->video_pid);
  verbose("Audio PID=%u", car->audio_pid);
    load_carousel(car);
    exit(0);
 car->downloader = child;
 return car;
}

canonical_original_code:
struct carousel *start_downloader(unsigned int adapter, unsigned int frontend, unsigned int demux, unsigned int dvr, unsigned int timeout, unsigned short service_id, int carousel_id)
{
 struct carousel *car;
 int child;
  int tuner_fd = 0;
  if(!tune_service_id(adapter, frontend, timeout, service_id, &tuner_fd))
    error("Unable to retune; let's hope you're already tuned to the right frequency...");
  car = find_mheg(adapter, demux, dvr, timeout, service_id, carousel_id);
  verbose("Carousel ID=%u", car->carousel_id);
  verbose("Boot PID=%u", car->boot_pid);
  verbose("Video PID=%u", car->video_pid);
  verbose("Audio PID=%u", car->audio_pid);
    load_carousel(car);
    exit(0);
 car->downloader = child;
 return car;
}

variable_types:
adapter
unsigned int

variable_types:
frontend
unsigned int

variable_types:
demux
unsigned int

variable_types:
dvr
unsigned int

variable_types:
timeout
unsigned int

variable_types: struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } assoc; int npids; struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *pids; _Bool got_dsi; unsigned int nmodules; struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *modules; } *

variable_types: child int


decompiled_code:
void __fastcall __noreturn start_listener(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int a6, unsigned __int16 a7, unsigned int a8)
{
int *v12; // rax
char *v13; // rax
struct sigaction v14; // [rsp+0h] [rbp-D8h] BYREF

v14.sa_handler = (__sighandler_t)dead_child;
sigemptyset(&v14.sa_mask);
v14.sa_flags = 0;
if ( sigaction(17, &v14, 0LL) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fatal("signal: SIGCHLD: %s", v13);
}
start_downloader(a2, a3, a4, a5, a6, a7, a8);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int a6, unsigned __int16 a7, unsigned int a8)
{
int *v12; // rax
char *v13; // rax
struct sigaction v14; // [rsp+0h] [rbp-D8h] BYREF

v14.sa_handler = (__sighandler_t)dead_child;
sigemptyset(&v14.sa_mask);
v14.sa_flags = 0;
if ( sigaction(17, &v14, 0LL) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fatal("signal: SIGCHLD: %s", v13);
}
func4(a2, a3, a4, a5, a6, a7, a8);
}

original_code:
void
start_listener(struct sockaddr_in *listen_addr, unsigned int adapter, unsigned int frontend, unsigned int demux, unsigned int dvr, unsigned int timeout, uint16_t service_id, int carousel_id)
{
 struct listen_data listen_data;
 struct sigaction action;
 int sockopt;
 int listen_sock;
 int accept_sock;
 fd_set read_fds;
 socklen_t addr_len;
 struct sockaddr_in client_addr;
 pid_t child;
 action.__sigaction_handler.sa_handler = dead_child;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if(sigaction(17, &action, ((void *)0)) < 0)
  fatal("signal: SIGCHLD: %s", strerror((*__errno_location ())));
 listen_data.carousel = start_downloader(adapter, frontend, demux, dvr, timeout, service_id, carousel_id);
 action.__sigaction_handler.sa_sigaction = hup_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 4;
 if(sigaction(1, &action, ((void *)0)) < 0)
  fatal("signal: SIGHUP: %s", strerror((*__errno_location ())));
 verbose("Listening on %s:%u", inet_ntoa(listen_addr->sin_addr), ntohs(listen_addr->sin_port));
 if((listen_sock = socket(2, SOCK_STREAM, 0)) < 0)
  fatal("socket: %s", strerror((*__errno_location ())));
 sockopt = 1;
 if(setsockopt(listen_sock, 1, 2, &sockopt, sizeof(sockopt)) < 0)
  fatal("setsockopt: SO_REUSEADDR: %s", strerror((*__errno_location ())));
 if(bind(listen_sock, (struct sockaddr *) listen_addr, sizeof(struct sockaddr_in)) < 0)
  fatal("bind: %s", strerror((*__errno_location ())));
 if(listen(listen_sock, 5) < 0)
  fatal("listen: %s", strerror((*__errno_location ())));
 while(1)
 {
  if(retune_id != -1)
  {
   verbose("Retune to service_id %d", retune_id);
   kill(listen_data.carousel->downloader, 9);
   cache_flush();
   listen_data.carousel = start_downloader(adapter, frontend, demux, dvr, timeout, retune_id, -1);
   retune_id = -1;
  }
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_fds)->__fds_bits)[((listen_sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listen_sock) % (8 * (int) sizeof (__fd_mask)))))));
  if(select(listen_sock + 1, &read_fds, ((void *)0), ((void *)0), ((void *)0)) < 0)
  {
   if((*__errno_location ()) != 4)
    error("select: %s", strerror((*__errno_location ())));
   continue;
  }
  if(!((((&read_fds)->__fds_bits)[((listen_sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listen_sock) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   continue;
  addr_len = sizeof(client_addr);
  if((accept_sock = accept(listen_sock, (struct sockaddr *) &client_addr, &addr_len)) < 0)
  {
   error("accept: %s", strerror((*__errno_location ())));
   continue;
  }
  if((child = fork()) < 0)
  {
   fatal("fork: %s", strerror((*__errno_location ())));
  }
  else if(child == 0)
  {
   close(listen_sock);
   handle_connection(&listen_data, accept_sock, &client_addr);
   close(accept_sock);
   _exit(0);
  }
  else
  {
   close(accept_sock);
  }
 }
 close(listen_sock);
 return;
}

canonical_original_code:
void start_listener(struct sockaddr_in *listen_addr, unsigned int adapter, unsigned int frontend, unsigned int demux, unsigned int dvr, unsigned int timeout, unsigned short service_id, int carousel_id)
{
 struct listen_data listen_data;
 struct sigaction action;
 int sockopt;
 int listen_sock;
 int accept_sock;
 struct fd_set read_fds;
 unsigned int addr_len;
 struct sockaddr_in client_addr;
 int child;
 action.__sigaction_handler.sa_handler = dead_child;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if(sigaction(17, &action, ((void *)0)) < 0)
  fatal("signal: SIGCHLD: %s", strerror((*__errno_location ())));
 listen_data.carousel = start_downloader(adapter, frontend, demux, dvr, timeout, service_id, carousel_id);
 action.__sigaction_handler.sa_sigaction = hup_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 4;
 if(sigaction(1, &action, ((void *)0)) < 0)
  fatal("signal: SIGHUP: %s", strerror((*__errno_location ())));
 verbose("Listening on %s:%u", inet_ntoa(listen_addr->sin_addr), ntohs(listen_addr->sin_port));
 if((listen_sock = socket(2, SOCK_STREAM, 0)) < 0)
  fatal("socket: %s", strerror((*__errno_location ())));
 sockopt = 1;
 if(setsockopt(listen_sock, 1, 2, &sockopt, sizeof(sockopt)) < 0)
  fatal("setsockopt: SO_REUSEADDR: %s", strerror((*__errno_location ())));
 if(bind(listen_sock, (struct sockaddr *) listen_addr, sizeof(struct sockaddr_in)) < 0)
  fatal("bind: %s", strerror((*__errno_location ())));
 if(listen(listen_sock, 5) < 0)
  fatal("listen: %s", strerror((*__errno_location ())));
 while(1)
 {
  if(retune_id != -1)
  {
   verbose("Retune to service_id %d", retune_id);
   kill(listen_data.carousel->downloader, 9);
   cache_flush();
   listen_data.carousel = start_downloader(adapter, frontend, demux, dvr, timeout, retune_id, -1);
   retune_id = -1;
  }
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_fds)->__fds_bits)[((listen_sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listen_sock) % (8 * (int) sizeof (__fd_mask)))))));
  if(select(listen_sock + 1, &read_fds, ((void *)0), ((void *)0), ((void *)0)) < 0)
  {
   if((*__errno_location ()) != 4)
    error("select: %s", strerror((*__errno_location ())));
   continue;
  }
  if(!((((&read_fds)->__fds_bits)[((listen_sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listen_sock) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   continue;
  addr_len = sizeof(client_addr);
  if((accept_sock = accept(listen_sock, (struct sockaddr *) &client_addr, &addr_len)) < 0)
  {
   error("accept: %s", strerror((*__errno_location ())));
   continue;
  }
  if((child = fork()) < 0)
  {
   fatal("fork: %s", strerror((*__errno_location ())));
  }
  else if(child == 0)
  {
   close(listen_sock);
   handle_connection(&listen_data, accept_sock, &client_addr);
   close(accept_sock);
   _exit(0);
  }
  else
  {
   close(accept_sock);
  }
 }
 close(listen_sock);
 return;
}

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: struct listen_data { struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } assoc; int npids; struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *pids; _Bool got_dsi; unsigned int nmodules; struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *modules; } *carousel; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: sockopt int

user_defined_types: struct listen_data { struct carousel *carousel; }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

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

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
bool __fastcall is_audio_stream(unsigned __int8 a1)
{
bool result; // al

if ( a1 > 4u )
result = a1 == 17;
else
result = a1 > 2u;
return result;
}

canonical_decompiled_code:
bool __fastcall func2(unsigned __int8 a1)
{
bool result; // al

if ( a1 > 4u )
result = a1 == 17;
else
result = a1 > 2u;
return result;
}

original_code:
_Bool
is_audio_stream(uint8_t stream_type)
{
 switch(stream_type)
 {
 case 0x03:
 case 0x04:
 case 0x11:
  return 1;
 default:
  return 0;
 }
}

canonical_original_code:
_Bool is_audio_stream(unsigned char stream_type)
{
 switch(stream_type)
 {
 case 0x03:
 case 0x04:
 case 0x11:
  return 1;
 default:
  return 0;
 }
}


decompiled_code:
__int64 __fastcall free_module(__int64 a1)
{
safe_free(*(_QWORD *)(a1 + 40));
return safe_free(*(_QWORD *)(a1 + 24));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
safe_free(*(_QWORD *)(a1 + 40));
return safe_free(*(_QWORD *)(a1 + 24));
}

original_code:
void
free_module(struct module *mod)
{
 safe_free(mod->data);
 safe_free(mod->got_block);
 return;
}

variable_types: struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *


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

free_module(*(_QWORD *)(a1 + 8280) + 48LL * a2);
result = *(_DWORD *)(a1 + 8276) - a2;
if ( 48 * (_DWORD)result != 48 )
result = (unsigned __int64)memmove(
(void *)(*(_QWORD *)(a1 + 8280) + 48LL * a2),
(const void *)(*(_QWORD *)(a1 + 8280) + 48LL * (a2 + 1)),
(unsigned int)(48 * result - 48));
--*(_DWORD *)(a1 + 8276);
return result;
}

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

func2(*(_QWORD *)(a1 + 8280) + 48LL * a2);
result = *(_DWORD *)(a1 + 8276) - a2;
if ( 48 * (_DWORD)result != 48 )
result = (unsigned __int64)memmove(
(void *)(*(_QWORD *)(a1 + 8280) + 48LL * a2),
(const void *)(*(_QWORD *)(a1 + 8280) + 48LL * (a2 + 1)),
(unsigned int)(48 * result - 48));
--*(_DWORD *)(a1 + 8276);
return result;
}

original_code:
void
delete_module(struct carousel *car, uint32_t num)
{
 uint32_t size;
 free_module(&car->modules[num]);
 size = ((car->nmodules - 1) - num) * sizeof(struct module);
 if(size > 0)
  memmove(&car->modules[num], &car->modules[num + 1], size);
 car->nmodules --;
 return;
}

canonical_original_code:
void delete_module(struct carousel *car, unsigned int num)
{
 unsigned int size;
 free_module(&car->modules[num]);
 size = ((car->nmodules - 1) - num) * sizeof(struct module);
 if(size > 0)
  memmove(&car->modules[num], &car->modules[num + 1], size);
 car->nmodules --;
 return;
}

variable_types:
num
unsigned int

variable_types:
size
unsigned int


decompiled_code:
__int64 __fastcall find_module(__int64 a1, __int16 a2, unsigned __int8 a3, int a4)
{
int v5; // edi
__int64 v9; // rax
unsigned int i; // esi
__int64 v11; // rdx
unsigned __int8 v12; // r10

v5 = *(_DWORD *)(a1 + 8276);
if ( !v5 )
return 0LL;
v9 = *(_QWORD *)(a1 + 8280);
for ( i = 0; i != v5; ++i )
{
v11 = v9;
if ( *(_WORD *)v9 == a2 && *(_DWORD *)(v9 + 4) == a4 )
{
v12 = *(_BYTE *)(v9 + 8);
if ( v12 == a3 )
return v11;
if ( v12 < a3 )
{
delete_module(a1, i);
return 0LL;
}
}
v9 += 48LL;
}
return 0LL;
}

original_code:
struct module *
find_module(struct carousel *car, uint16_t module_id, uint8_t version, uint32_t download_id)
{
 unsigned int i;
 for(i=0; i<car->nmodules; i++)
 {
  if(car->modules[i].module_id == module_id
  && car->modules[i].download_id == download_id)
  {
   if(car->modules[i].version == version)
   {
    return &car->modules[i];
   }
   else if(car->modules[i].version < version)
   {
    delete_module(car, i);
    return ((void *)0);
   }
  }
 }
 return ((void *)0);
}

canonical_original_code:
struct module *find_module(struct carousel *car, unsigned short module_id, unsigned char version, unsigned int download_id)
{
 unsigned int i;
 for(i=0; i<car->nmodules; i++)
 {
  if(car->modules[i].module_id == module_id
  && car->modules[i].download_id == download_id)
  {
   if(car->modules[i].version == version)
   {
    return &car->modules[i];
   }
   else if(car->modules[i].version < version)
   {
    delete_module(car, i);
    return ((void *)0);
   }
  }
 }
 return ((void *)0);
}

variable_types: struct carousel { char demux_device[4096]; char dvr_device[4096]; unsigned int timeout; int downloader; unsigned short network_id; unsigned short service_id; unsigned int carousel_id; unsigned short boot_pid; unsigned short audio_pid; unsigned char audio_type; unsigned short video_pid; unsigned char video_type; unsigned short current_pid; struct assoc { unsigned int nassocs; unsigned short *pids; unsigned short *sids; unsigned char *types; } assoc; int npids; struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *pids; _Bool got_dsi; unsigned int nmodules; struct module *modules; } *

variable_types:
version
unsigned char

variable_types:
i
unsigned int

return_type: struct module { unsigned short module_id; unsigned int download_id; unsigned char version; unsigned short block_size; unsigned short nblocks; unsigned int blocks_left; _Bool *got_block; unsigned int size; unsigned char *data; } *


decompiled_code:
void __fastcall download_block(__int64 a1, unsigned __int16 *a2, unsigned __int16 a3, const void *a4, unsigned int a5)
{
unsigned __int16 v7; // cx
_BYTE *v9; // rax
unsigned int v10; // ebp
__int64 v11; // rcx
unsigned int v12; // eax

v7 = a2[6];
if ( v7 <= a3 )
{
error((int)"download_block: moduleId=%u block=%u nblocks=%u", *a2, (const char *)a3, v7);
}
else
{
v9 = (_BYTE *)(*((_QWORD *)a2 + 3) + a3);
if ( !*v9 )
{
*v9 = 1;
v10 = a3;
memcpy((void *)(*((_QWORD *)a2 + 5) + a3 * a2[5]), a4, a5);
v11 = (unsigned int)(*((_DWORD *)a2 + 4) - 1);
*((_DWORD *)a2 + 4) = v11;
verbose("download_block: module=%u block=%u left=%u", *a2, v10, v11);
if ( !*((_DWORD *)a2 + 4) )
{
verbose("got module %u (size=%u)", *a2, *((unsigned int *)a2 + 8));
if ( memcmp(*((const void **)a2 + 5), "BIOP", 4uLL) )
{
vhexdump(*((_QWORD *)a2 + 5), *((unsigned int *)a2 + 8));
uncompress_module((__int64)a2);
verbose("uncompressed size=%u", *((unsigned int *)a2 + 8));
}
if ( (unsigned __int8)process_biop(a1, a2, *((_QWORD *)a2 + 5), *((unsigned int *)a2 + 8)) )
{
safe_free(*((_QWORD *)a2 + 5));
*((_QWORD *)a2 + 5) = 0LL;
}
else
{
v12 = a2[6];
*((_DWORD *)a2 + 4) = v12;
memset(*((void **)a2 + 3), 0, v12);
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, unsigned __int16 *a2, unsigned __int16 a3, const void *a4, unsigned int a5)
{
unsigned __int16 v7; // cx
_BYTE *v9; // rax
unsigned int v10; // ebp
__int64 v11; // rcx
unsigned int v12; // eax

v7 = a2[6];
if ( v7 <= a3 )
{
error((int)"download_block: moduleId=%u block=%u nblocks=%u", *a2, (const char *)a3, v7);
}
else
{
v9 = (_BYTE *)(*((_QWORD *)a2 + 3) + a3);
if ( !*v9 )
{
*v9 = 1;
v10 = a3;
memcpy((void *)(*((_QWORD *)a2 + 5) + a3 * a2[5]), a4, a5);
v11 = (unsigned int)(*((_DWORD *)a2 + 4) - 1);
*((_DWORD *)a2 + 4) = v11;
verbose("download_block: module=%u block=%u left=%u", *a2, v10, v11);
if ( !*((_DWORD *)a2 + 4) )
{
verbose("got module %u (size=%u)", *a2, *((unsigned int *)a2 + 8));
if ( memcmp(*((const void **)a2 + 5), "BIOP", 4uLL) )
{
vhexdump(*((_QWORD *)a2 + 5), *((unsigned int *)a2 + 8));
func5((__int64)a2);
verbose("uncompressed size=%u", *((unsigned int *)a2 + 8));
}
if ( (unsigned __int8)process_biop(a1, a2, *((_QWORD *)a2 + 5), *((unsigned int *)a2 + 8)) )
{
safe_free(*((_QWORD *)a2 + 5));
*((_QWORD *)a2 + 5) = 0LL;
}
else
{
v12 = a2[6];
*((_DWORD *)a2 + 4) = v12;
memset(*((void **)a2 + 3), 0, v12);
}
}
}
}
}

original_code:
void
download_block(struct carousel *car, struct module *mod, uint16_t block, unsigned char *data, uint32_t length)
{
 if(block >= mod->nblocks)
 {
  error("download_block: moduleId=%u block=%u nblocks=%u", mod->module_id, block, mod->nblocks);
  return;
 }
 if(mod->got_block[block])
  return;
 mod->got_block[block] = 1;
 memcpy(mod->data + (block * mod->block_size), data, length);
 mod->blocks_left --;
 verbose("download_block: module=%u block=%u left=%u", mod->module_id, block, mod->blocks_left);
 if(mod->blocks_left == 0)
 {
  verbose("got module %u (size=%u)", mod->module_id, mod->size);
  if(strncmp((char *) mod->data, "BIOP", 4) != 0)
  {
   vhexdump(mod->data, mod->size);
   uncompress_module(mod);
   verbose("uncompressed size=%u", mod->size);
  }
  if(process_biop(car, mod, (struct BIOPMessageHeader *) mod->data, mod->size))
  {
   safe_free(mod->data);
   mod->data = ((void *)0);
  }
  else
  {
   mod->blocks_left = mod->nblocks;
   bzero(mod->got_block, mod->nblocks * sizeof(_Bool));
  }
 }
 return;
}

canonical_original_code:
void download_block(struct carousel *car, struct module *mod, unsigned short block, unsigned char *data, unsigned int length)
{
 if(block >= mod->nblocks)
 {
  error("download_block: moduleId=%u block=%u nblocks=%u", mod->module_id, block, mod->nblocks);
  return;
 }
 if(mod->got_block[block])
  return;
 mod->got_block[block] = 1;
 memcpy(mod->data + (block * mod->block_size), data, length);
 mod->blocks_left --;
 verbose("download_block: module=%u block=%u left=%u", mod->module_id, block, mod->blocks_left);
 if(mod->blocks_left == 0)
 {
  verbose("got module %u (size=%u)", mod->module_id, mod->size);
  if(strncmp((char *) mod->data, "BIOP", 4) != 0)
  {
   vhexdump(mod->data, mod->size);
   uncompress_module(mod);
   verbose("uncompressed size=%u", mod->size);
  }
  if(process_biop(car, mod, (struct BIOPMessageHeader *) mod->data, mod->size))
  {
   safe_free(mod->data);
   mod->data = ((void *)0);
  }
  else
  {
   mod->blocks_left = mod->nblocks;
   bzero(mod->got_block, mod->nblocks * sizeof(_Bool));
  }
 }
 return;
}

variable_types:
mod
struct module *

variable_types:
block
unsigned short

variable_types:
data
unsigned char *

variable_types:
length
unsigned int

user_defined_types: struct BIOPMessageHeader { char magic[4]; struct BIOPVersion biop_version; unsigned char byte_order; unsigned char message_type; unsigned int message_size; }

user_defined_types: struct BIOPVersion { unsigned char major; unsigned char minor; }


decompiled_code:
__int64 cache_init()
{
int v1; // edi
char *v2; // rax

if ( mkdir("cache", 0x1EDu) < 0 )
{
v1 = *_errno_location();
if ( v1 != 17 )
{
v2 = strerror(v1);
fatal("Unable to create cache directory '%s': %s", "cache", v2);
}
}
cache_flush();
return 1LL;
}

canonical_decompiled_code:
__int64 func3()
{
int v1; // edi
char *v2; // rax

if ( mkdir("cache", 0x1EDu) < 0 )
{
v1 = *_errno_location();
if ( v1 != 17 )
{
v2 = strerror(v1);
fatal("Unable to create cache directory '%s': %s", "cache", v2);
}
}
func2();
return 1LL;
}

original_code:
_Bool
cache_init(void)
{
 if(mkdir("cache", 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create cache directory '%s': %s", "cache", strerror((*__errno_location ())));
 cache_flush();
 return 1;
}

canonical_original_code:
_Bool cache_init()
{
 if(mkdir("cache", 0755) < 0 && (*__errno_location ()) != 17)
  fatal("Unable to create cache directory '%s': %s", "cache", strerror((*__errno_location ())));
 cache_flush();
 return 1;
}


decompiled_code:
__int64 __fastcall add_demux_filter(const char *errnum, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
int v8; // eax
const char *v9; // rdx
__int16 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+2h] [rbp-36h]
__int16 v12; // [rsp+6h] [rbp-32h]
int v13; // [rsp+8h] [rbp-30h]
int v14; // [rsp+Ch] [rbp-2Ch]
int v15; // [rsp+10h] [rbp-28h]

v2 = open(errnum, 2);
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
error((int)"open '%s': %s", (int)errnum, v6);
v3 = -1;
}
else
{
v3 = v2;
v11 = 0;
v12 = 0;
v13 = 2;
v15 = 4;
v10 = a2;
v14 = 20;
if ( ioctl(v2, 0x40146F2CuLL, &v10) < 0 )
{
v7 = _errno_location();
v8 = (unsigned int)strerror(*v7);
error((int)"ioctl DMX_SET_PES_FILTER: %s", v8, v9);
close(v3);
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *errnum, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
int v8; // eax
const char *v9; // rdx
__int16 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+2h] [rbp-36h]
__int16 v12; // [rsp+6h] [rbp-32h]
int v13; // [rsp+8h] [rbp-30h]
int v14; // [rsp+Ch] [rbp-2Ch]
int v15; // [rsp+10h] [rbp-28h]

v2 = open(errnum, 2);
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
error((int)"open '%s': %s", (int)errnum, v6);
v3 = -1;
}
else
{
v3 = v2;
v11 = 0;
v12 = 0;
v13 = 2;
v15 = 4;
v10 = a2;
v14 = 20;
if ( ioctl(v2, 0x40146F2CuLL, &v10) < 0 )
{
v7 = _errno_location();
v8 = (unsigned int)strerror(*v7);
error((int)"ioctl DMX_SET_PES_FILTER: %s", v8, v9);
close(v3);
v3 = -1;
}
}
return v3;
}

original_code:
int
add_demux_filter(char *demux_dev, uint16_t pid, dmx_pes_type_t pes_type)
{
 int fd;
 struct dmx_pes_filter_params pes_filter;
 if((fd = open(demux_dev, 02)) < 0)
 {
  error("open '%s': %s", demux_dev, strerror((*__errno_location ())));
  return -1;
 }
 bzero(&pes_filter, sizeof(pes_filter));
 pes_filter.input = DMX_IN_FRONTEND;
 pes_filter.output = DMX_OUT_TS_TAP;
 pes_filter.flags = 4;
 pes_filter.pid = pid;
 pes_type = DMX_PES_OTHER;
 pes_filter.pes_type = pes_type;
 if(ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((44)) << 0) | ((((sizeof(struct dmx_pes_filter_params)))) << ((0 +8)+8))), &pes_filter) < 0)
 {
  error("ioctl DMX_SET_PES_FILTER: %s", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int add_demux_filter(char *demux_dev, unsigned short pid, enum dmx_ts_pes pes_type)
{
 int fd;
 struct dmx_pes_filter_params pes_filter;
 if((fd = open(demux_dev, 02)) < 0)
 {
  error("open '%s': %s", demux_dev, strerror((*__errno_location ())));
  return -1;
 }
 bzero(&pes_filter, sizeof(pes_filter));
 pes_filter.input = DMX_IN_FRONTEND;
 pes_filter.output = DMX_OUT_TS_TAP;
 pes_filter.flags = 4;
 pes_filter.pid = pid;
 pes_type = DMX_PES_OTHER;
 pes_filter.pes_type = pes_type;
 if(ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((44)) << 0) | ((((sizeof(struct dmx_pes_filter_params)))) << ((0 +8)+8))), &pes_filter) < 0)
 {
  error("ioctl DMX_SET_PES_FILTER: %s", strerror((*__errno_location ())));
  close(fd);
  return -1;
 }
 return fd;
}

variable_types:
pid
unsigned short

variable_types: fd int

variable_types: struct dmx_pes_filter_params { unsigned short pid; enum dmx_input { DMX_IN_FRONTEND=0, DMX_IN_DVR=1 } input; enum dmx_output { DMX_OUT_DECODER=0, DMX_OUT_TAP=1, DMX_OUT_TS_TAP=2, DMX_OUT_TSDEMUX_TAP=3 } output; enum dmx_ts_pes { DMX_PES_AUDIO0=0, DMX_PES_VIDEO0=1, DMX_PES_TELETEXT0=2, DMX_PES_SUBTITLE0=3, DMX_PES_PCR0=4, DMX_PES_AUDIO1=5, DMX_PES_VIDEO1=6, DMX_PES_TELETEXT1=7, DMX_PES_SUBTITLE1=8, DMX_PES_PCR1=9, DMX_PES_AUDIO2=10, DMX_PES_VIDEO2=11, DMX_PES_TELETEXT2=12, DMX_PES_SUBTITLE2=13, DMX_PES_PCR2=14, DMX_PES_AUDIO3=15, DMX_PES_VIDEO3=16, DMX_PES_TELETEXT3=17, DMX_PES_SUBTITLE3=18, DMX_PES_PCR3=19, DMX_PES_OTHER=20 } pes_type; unsigned int flags; }

user_defined_types: struct dmx_pes_filter_params { unsigned short pid; enum dmx_input input; enum dmx_output output; enum dmx_ts_pes pes_type; unsigned int flags; }