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

if ( !(bwas_flag | ras_flag | eas_flag | as_flag) )
{
if ( !mmas_flag )
goto LABEL_16;
if ( ls_flag )
goto LABEL_4;
goto LABEL_24;
}
if ( !ls_flag )
{
LABEL_24:
evaporation();
LABEL_14:
if ( as_flag )
goto LABEL_5;
goto LABEL_15;
}
if ( !mmas_flag )
{
evaporation_nn_list();
goto LABEL_14;
}
LABEL_4:
mmas_evaporation_nn_list();
if ( as_flag )
{
LABEL_5:
as_update();
goto LABEL_6;
}
LABEL_15:
if ( eas_flag )
{
eas_update();
goto LABEL_6;
}
LABEL_16:
if ( ras_flag )
{
ras_update();
}
else if ( mmas_flag )
{
mmas_update();
}
else if ( bwas_flag )
{
bwas_update();
}
else
{
if ( !acs_flag )
goto LABEL_20;
acs_global_update();
}
LABEL_6:
if ( mmas_flag )
{
if ( ls_flag )
{
if ( as_flag )
return compute_nn_list_total_information();
LABEL_9:
if ( !ls_flag )
return compute_total_information();
return compute_nn_list_total_information();
}
check_pheromone_trail_limits();
}
LABEL_20:
if ( as_flag )
goto LABEL_9;
result = mmas_flag | bwas_flag | ras_flag | eas_flag;
if ( result )
goto LABEL_9;
return result;
}

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

if ( !(bwas_flag | ras_flag | eas_flag | as_flag) )
{
if ( !mmas_flag )
goto LABEL_16;
if ( ls_flag )
goto LABEL_4;
goto LABEL_24;
}
if ( !ls_flag )
{
LABEL_24:
evaporation();
LABEL_14:
if ( as_flag )
goto LABEL_5;
goto LABEL_15;
}
if ( !mmas_flag )
{
evaporation_nn_list();
goto LABEL_14;
}
LABEL_4:
mmas_evaporation_nn_list();
if ( as_flag )
{
LABEL_5:
func6();
goto LABEL_6;
}
LABEL_15:
if ( eas_flag )
{
func7();
goto LABEL_6;
}
LABEL_16:
if ( ras_flag )
{
func8();
}
else if ( mmas_flag )
{
func9();
}
else if ( bwas_flag )
{
func10();
}
else
{
if ( !acs_flag )
goto LABEL_20;
func11();
}
LABEL_6:
if ( mmas_flag )
{
if ( ls_flag )
{
if ( as_flag )
return compute_nn_list_total_information();
LABEL_9:
if ( !ls_flag )
return compute_total_information();
return compute_nn_list_total_information();
}
check_pheromone_trail_limits();
}
LABEL_20:
if ( as_flag )
goto LABEL_9;
result = mmas_flag | bwas_flag | ras_flag | eas_flag;
if ( result )
goto LABEL_9;
return result;
}

original_code:
void pheromone_trail_update( void )
{
    if ( as_flag || eas_flag || ras_flag || bwas_flag || mmas_flag ) {
 if ( ls_flag ) {
     if ( mmas_flag )
  mmas_evaporation_nn_list();
     else
  evaporation_nn_list();
 } else {
     evaporation();
 }
    }
    if ( as_flag )
 as_update();
    else if ( eas_flag )
 eas_update();
    else if ( ras_flag )
 ras_update();
    else if ( mmas_flag )
 mmas_update();
    else if ( bwas_flag )
 bwas_update();
    else if ( acs_flag )
 acs_global_update();
    if ( mmas_flag && !ls_flag )
 check_pheromone_trail_limits();
    if ( as_flag || eas_flag || ras_flag || mmas_flag || bwas_flag ) {
 if ( ls_flag ) {
     compute_nn_list_total_information();
 } else {
     compute_total_information();
 }
    }
}

canonical_original_code:
void pheromone_trail_update()
{
    if ( as_flag || eas_flag || ras_flag || bwas_flag || mmas_flag ) {
 if ( ls_flag ) {
     if ( mmas_flag )
  mmas_evaporation_nn_list();
     else
  evaporation_nn_list();
 } else {
     evaporation();
 }
    }
    if ( as_flag )
 as_update();
    else if ( eas_flag )
 eas_update();
    else if ( ras_flag )
 ras_update();
    else if ( mmas_flag )
 mmas_update();
    else if ( bwas_flag )
 bwas_update();
    else if ( acs_flag )
 acs_global_update();
    if ( mmas_flag && !ls_flag )
 check_pheromone_trail_limits();
    if ( as_flag || eas_flag || ras_flag || mmas_flag || bwas_flag ) {
 if ( ls_flag ) {
     compute_nn_list_total_information();
 } else {
     compute_total_information();
 }
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rbx
__int64 v7; // rbp
void *v8; // rdi

start_timers(argc, argv, envp);
init_program(argc, argv);
seed = time(0LL);
v3 = compute_nn_lists();
v4 = n;
*((_QWORD *)&instance + 30) = v3;
pheromone = (void *)generate_double_matrix(v4, v4);
total = (void *)generate_double_matrix(n, n);
time_used = elapsed_time(1LL);
printf("Initialization took %.10f seconds\n", time_used);
n_try = 0LL;
if ( max_tries > 0 )
{
v5 = 0LL;
do
{
init_try(v5);
while ( !termination_condition() )
{
construct_solutions();
if ( ls_flag > 0 )
local_search();
update_statistics();
pheromone_trail_update();
search_control_and_statistics();
++iteration;
}
exit_try(n_try);
v5 = ++n_try;
}
while ( n_try < max_tries );
}
exit_program();
free(*((void **)&instance + 29));
free(*((void **)&instance + 30));
free(pheromone);
free(total);
free(best_in_try);
free(best_found_at);
free(time_best_found);
free(time_total_run);
if ( n_ants > 0 )
{
v6 = 0LL;
v7 = 0LL;
do
{
++v7;
free(*(void **)((char *)ant + v6));
v8 = *(void **)((char *)ant + v6 + 8);
v6 += 24LL;
free(v8);
}
while ( n_ants > v7 );
}
free(ant);
free((void *)*best_so_far_ant);
free(*(void **)(best_so_far_ant + 8LL));
free(prob_of_selection);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rbx
__int64 v7; // rbp
void *v8; // rdi

start_timers(argc, argv, envp);
init_program(argc, argv);
seed = time(0LL);
v3 = compute_nn_lists();
v4 = n;
*((_QWORD *)&instance + 30) = v3;
pheromone = (void *)generate_double_matrix(v4, v4);
total = (void *)generate_double_matrix(n, n);
time_used = elapsed_time(1LL);
printf("Initialization took %.10f seconds\n", time_used);
n_try = 0LL;
if ( max_tries > 0 )
{
v5 = 0LL;
do
{
func2(v5);
while ( !func0() )
{
func1();
if ( ls_flag > 0 )
func3();
func4();
func12();
func5();
++iteration;
}
exit_try(n_try);
v5 = ++n_try;
}
while ( n_try < max_tries );
}
exit_program();
free(*((void **)&instance + 29));
free(*((void **)&instance + 30));
free(pheromone);
free(total);
free(best_in_try);
free(best_found_at);
free(time_best_found);
free(time_total_run);
if ( n_ants > 0 )
{
v6 = 0LL;
v7 = 0LL;
do
{
++v7;
free(*(void **)((char *)ant + v6));
v8 = *(void **)((char *)ant + v6 + 8);
v6 += 24LL;
free(v8);
}
while ( n_ants > v7 );
}
free(ant);
free((void *)*best_so_far_ant);
free(*(void **)(best_so_far_ant + 8LL));
free(prob_of_selection);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
    long int i;
    start_timers();
    init_program(argc, argv);
    seed = (long int) time(((void *)0));
    instance.nn_list = compute_nn_lists();
    pheromone = generate_double_matrix( n, n );
    total = generate_double_matrix( n, n );
    time_used = elapsed_time( VIRTUAL );
    printf("Initialization took %.10f seconds\n",time_used);
    for ( n_try = 0 ; n_try < max_tries ; n_try++ ) {
 init_try(n_try);
 while ( !termination_condition() ) {
     construct_solutions();
     if ( ls_flag > 0 )
  local_search();
     update_statistics();
     pheromone_trail_update();
     search_control_and_statistics();
     iteration++;
 }
 exit_try(n_try);
    }
    exit_program();
    free( instance.distance );
    free( instance.nn_list );
    free( pheromone );
    free( total );
    free( best_in_try );
    free( best_found_at );
    free( time_best_found );
    free( time_total_run );
    for ( i = 0 ; i < n_ants ; i++ ) {
 free( ant[i].tour );
 free( ant[i].visited );
    }
    free( ant );
    free( (*best_so_far_ant).tour );
    free( (*best_so_far_ant).visited );
    free( prob_of_selection );
    return(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
    long i;
    start_timers();
    init_program(argc, argv);
    seed = (long) time(((void *)0));
    instance.nn_list = compute_nn_lists();
    pheromone = generate_double_matrix( n, n );
    total = generate_double_matrix( n, n );
    time_used = elapsed_time( VIRTUAL );
    printf("Initialization took %.10f seconds\n",time_used);
    for ( n_try = 0 ; n_try < max_tries ; n_try++ ) {
 init_try(n_try);
 while ( !termination_condition() ) {
     construct_solutions();
     if ( ls_flag > 0 )
  local_search();
     update_statistics();
     pheromone_trail_update();
     search_control_and_statistics();
     iteration++;
 }
 exit_try(n_try);
    }
    exit_program();
    free( instance.distance );
    free( instance.nn_list );
    free( pheromone );
    free( total );
    free( best_in_try );
    free( best_found_at );
    free( time_best_found );
    free( time_total_run );
    for ( i = 0 ; i < n_ants ; i++ ) {
 free( ant[i].tour );
 free( ant[i].visited );
    }
    free( ant );
    free( (*best_so_far_ant).tour );
    free( (*best_so_far_ant).visited );
    free( prob_of_selection );
    return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i long

return_type: int


decompiled_code:
clock_t start_timers()
{
clock_t result; // rax

result = clock();
start_time = result;
return result;
}

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

result = clock();
start_time = result;
return result;
}

original_code:
void start_timers()
{
    start_time = clock();
}

return_type: void


dtrunc

decompiled_code:
double __fastcall dtrunc(double a1)
{
return (double)(int)a1;
}

canonical_decompiled_code:
double __fastcall func0(double a1)
{
return (double)(int)a1;
}

original_code:
static double dtrunc (double x)
{
    int k;
    k = (int) x;
    x = (double) k;
    return x;
}

canonical_original_code:
static double dtrunc(double x)
{
    int k;
    k = (int) x;
    x = (double) k;
    return x;
}

variable_types: x double

variable_types: k int

return_type: double


decompiled_code:
__int64 __fastcall round_distance(__int64 a1, __int64 a2)
{
double *v2; // rdi
double v3; // xmm0_8
__int64 result; // rax

v2 = (double *)(instance[28] + 16 * a1);
v3 = (v2[1] - *(double *)(instance[28] + 16 * a2 + 8)) * (v2[1] - *(double *)(instance[28] + 16 * a2 + 8))
+ (*v2 - *(double *)(instance[28] + 16 * a2)) * (*v2 - *(double *)(instance[28] + 16 * a2));
if ( v3 < 0.0 )
result = (unsigned int)(int)(sqrt(v3) + 0.5);
else
result = (unsigned int)(int)(sqrt(v3) + 0.5);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
double *v2; // rdi
double v3; // xmm0_8
__int64 result; // rax

v2 = (double *)(instance[28] + 16 * a1);
v3 = (v2[1] - *(double *)(instance[28] + 16 * a2 + 8)) * (v2[1] - *(double *)(instance[28] + 16 * a2 + 8))
+ (*v2 - *(double *)(instance[28] + 16 * a2)) * (*v2 - *(double *)(instance[28] + 16 * a2));
if ( v3 < 0.0 )
result = (unsigned int)(int)(sqrt(v3) + 0.5);
else
result = (unsigned int)(int)(sqrt(v3) + 0.5);
return result;
}

original_code:
long int round_distance (long int i, long int j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double r = sqrt(xd*xd + yd*yd) + 0.5;
    return (long int) r;
}

canonical_original_code:
long round_distance(long i, long j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double r = sqrt(xd*xd + yd*yd) + 0.5;
    return (long) r;
}

variable_types: j long

variable_types: xd double

variable_types: yd double

variable_types: r double

return_type: long


decompiled_code:
__int64 __fastcall ceil_distance(__int64 a1, __int64 a2)
{
double *v2; // rdi
double v3; // xmm0_8
__int64 result; // rax

v2 = (double *)(instance[28] + 16 * a1);
v3 = (v2[1] - *(double *)(instance[28] + 16 * a2 + 8)) * (v2[1] - *(double *)(instance[28] + 16 * a2 + 8))
+ (*v2 - *(double *)(instance[28] + 16 * a2)) * (*v2 - *(double *)(instance[28] + 16 * a2));
if ( v3 < 0.0 )
result = (unsigned int)(int)(sqrt(v3) + 0.000000001);
else
result = (unsigned int)(int)(sqrt(v3) + 0.000000001);
return result;
}

original_code:
long int ceil_distance (long int i, long int j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double r = sqrt(xd*xd + yd*yd) + 0.000000001;
    return (long int)r;
}

canonical_original_code:
long ceil_distance(long i, long j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double r = sqrt(xd*xd + yd*yd) + 0.000000001;
    return (long)r;
}


original_code:
long int geo_distance (long int i, long int j)
{
    double deg, min;
    double lati, latj, longi, longj;
    double q1, q2, q3;
    long int dd;
    double x1 = instance.nodeptr[i].x, x2 = instance.nodeptr[j].x,
 y1 = instance.nodeptr[i].y, y2 = instance.nodeptr[j].y;
    deg = dtrunc (x1);
    min = x1 - deg;
    lati = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (x2);
    min = x2 - deg;
    latj = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (y1);
    min = y1 - deg;
    longi = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (y2);
    min = y2 - deg;
    longj = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    q1 = cos (longi - longj);
    q2 = cos (lati - latj);
    q3 = cos (lati + latj);
    dd = (int) (6378.388 * acos (0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
    return dd;
}

canonical_original_code:
long geo_distance(long i, long j)
{
    double deg; double min;
    double lati; double latj; double longi; double longj;
    double q1; double q2; double q3;
    long dd;
    double x1 = instance.nodeptr[i].x; double x2 = instance.nodeptr[j].x; double y1 = instance.nodeptr[i].y; double y2 = instance.nodeptr[j].y;
    deg = dtrunc (x1);
    min = x1 - deg;
    lati = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (x2);
    min = x2 - deg;
    latj = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (y1);
    min = y1 - deg;
    longi = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    deg = dtrunc (y2);
    min = y2 - deg;
    longj = 3.14159265358979323846264 * (deg + 5.0 * min / 3.0) / 180.0;
    q1 = cos (longi - longj);
    q2 = cos (lati - latj);
    q3 = cos (lati + latj);
    dd = (int) (6378.388 * acos (0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
    return dd;
}

variable_types: deg double

variable_types: min double

variable_types: lati double

variable_types: latj double

variable_types: longi double

variable_types: longj double

variable_types: dd long


decompiled_code:
__int64 __fastcall att_distance(__int64 a1, __int64 a2)
{
double *v2; // rdi
double *v3; // rsi
double v4; // xmm0_8
double v5; // xmm1_8
double v6; // xmm1_8
double v7; // xmm0_8
__int64 result; // rax

v2 = (double *)(instance[28] + 16 * a1);
v3 = (double *)(instance[28] + 16 * a2);
v4 = v2[1] - v3[1];
v5 = ((*v2 - *v3) * (*v2 - *v3) + v4 * v4) / 10.0;
if ( v5 < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
v7 = dtrunc(v6);
result = (int)v7 + 1;
if ( v6 <= v7 )
result = (int)v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
double *v2; // rdi
double *v3; // rsi
double v4; // xmm0_8
double v5; // xmm1_8
double v6; // xmm1_8
double v7; // xmm0_8
__int64 result; // rax

v2 = (double *)(instance[28] + 16 * a1);
v3 = (double *)(instance[28] + 16 * a2);
v4 = v2[1] - v3[1];
v5 = ((*v2 - *v3) * (*v2 - *v3) + v4 * v4) / 10.0;
if ( v5 < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
v7 = func0(v6);
result = (int)v7 + 1;
if ( v6 <= v7 )
result = (int)v7;
return result;
}

original_code:
long int att_distance (long int i, long int j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double rij = sqrt ((xd * xd + yd * yd) / 10.0);
    double tij = dtrunc (rij);
    long int dij;
    if (tij < rij)
        dij = (int) tij + 1;
    else
        dij = (int) tij;
    return dij;
}

canonical_original_code:
long att_distance(long i, long j)
{
    double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
    double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
    double rij = sqrt ((xd * xd + yd * yd) / 10.0);
    double tij = dtrunc (rij);
    long dij;
    if (tij < rij)
        dij = (int) tij + 1;
    else
        dij = (int) tij;
    return dij;
}

variable_types: rij double

variable_types: tij double

variable_types: dij long


decompiled_code:
_QWORD *compute_distances()
{
__int64 v0; // rbp
_QWORD *v1; // rax
_QWORD *v2; // r13
__int64 v3; // r12
char *v4; // rax
__int64 v5; // rbx
char *v6; // rbp
__int64 v7; // rsi

v0 = n;
v1 = malloc(8 * n * (n + 1));
if ( !v1 )
{
fwrite("Out of memory, exit.", 1uLL, 0x14uLL, stderr);
exit(1);
}
v2 = v1;
v3 = 0LL;
if ( v0 > 0 )
{
do
{
v4 = (char *)&v2[v0 + v3 * v0];
v2[v3] = v4;
if ( v0 <= 0 )
break;
v5 = 0LL;
while ( 1 )
{
v6 = &v4[8 * v5];
v7 = v5++;
*(_QWORD *)v6 = distance(v3, v7);
v0 = n;
if ( n <= v5 )
break;
v4 = (char *)v2[v3];
}
++v3;
}
while ( n > v3 );
}
return v2;
}

original_code:
long int ** compute_distances(void)
{
    long int i, j;
    long int **matrix;
    if((matrix = malloc(sizeof(long int) * n * n +
   sizeof(long int *) * n )) == ((void *)0)){
 fprintf(stderr,"Out of memory, exit.");
 exit(1);
    }
    for ( i = 0 ; i < n ; i++ ) {
 matrix[i] = (long int *)(matrix + n) + i*n;
 for ( j = 0 ; j < n ; j++ ) {
     matrix[i][j] = distance(i, j);
 }
    }
    return matrix;
}

canonical_original_code:
long **compute_distances()
{
    long i; long j;
    long **matrix;
    if((matrix = malloc(sizeof(long) * n * n +
   sizeof(long *) * n )) == ((void *)0)){
 fprintf(stderr,"Out of memory, exit.");
 exit(1);
    }
    for ( i = 0 ; i < n ; i++ ) {
 matrix[i] = (long *)(matrix + n) + i*n;
 for ( j = 0 ; j < n ; j++ ) {
     matrix[i][j] = distance(i, j);
 }
    }
    return matrix;
}

variable_types: i long

variable_types:
matrix
long **

return_type: long **


decompiled_code:
__int64 __fastcall compute_tour_length(__int64 *a1)
{
__int64 *v1; // rsi
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rcx

if ( n <= 0 )
return 0LL;
v1 = &a1[n];
result = 0LL;
do
{
v3 = *a1;
v4 = a1[1];
++a1;
result += *(_QWORD *)(*(_QWORD *)(instance[29] + 8 * v3) + 8 * v4);
}
while ( v1 != a1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1)
{
__int64 *v1; // rsi
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rcx

if ( n <= 0 )
return 0LL;
v1 = &a1[n];
result = 0LL;
do
{
v3 = *a1;
v4 = a1[1];
++a1;
result += *(_QWORD *)(*(_QWORD *)(instance[29] + 8 * v3) + 8 * v4);
}
while ( v1 != a1 );
return result;
}

original_code:
long int compute_tour_length( long int *t )
{
    int i;
    long int tour_length = 0;
    for ( i = 0 ; i < n ; i++ ) {
 tour_length += instance.distance[t[i]][t[i+1]];
    }
    return tour_length;
}

canonical_original_code:
long compute_tour_length(long *t)
{
    int i;
    long tour_length = 0;
    for ( i = 0 ; i < n ; i++ ) {
 tour_length += instance.distance[t[i]][t[i+1]];
    }
    return tour_length;
}

variable_types:
t
long *

variable_types: i int


repo: thiagorcdl/SimpleDES

original_code:
char ip_1(char charmeleon){
 char c = 0;
 c |= (charmeleon << 3) & 0x80;
 c |= (charmeleon >> 1) & 0x40;
 c |= (charmeleon ) & 0x20;
 c |= (charmeleon << 1) & 0x10;
 c |= (charmeleon << 2) & 0x08;
 c |= (charmeleon >> 4) & 0x04;
 c |= (charmeleon << 1) & 0x02;
 c |= (charmeleon >> 2) & 0x01;
 return c;
}

variable_types: charmeleon char

variable_types: c char

return_type: char


expansion

original_code:
char expansion(char charizard){
 char c = 0;
 c |= (charizard << 7) & 0x80;
 c |= (charizard << 3) & 0x40;
 c |= (charizard << 3) & 0x20;
 c |= (charizard << 3) & 0x10;
 c |= (charizard << 1) & 0x08;
 c |= (charizard << 1) & 0x04;
 c |= (charizard << 1) & 0x02;
 c |= (charizard >> 3) & 0x01;
 return c;
}

variable_types: charizard char


variable_types: seila short

variable_types: c short

return_type: short


fk

variable_types: exp char

variable_types: d char

variable_types: a char

variable_types: b char

variable_types: lin char

variable_types: col char


getArgs

decompiled_code:
__int64 __fastcall getArgs(int a1)
{
__int64 result; // rax
unsigned int v2; // er12

if ( a1 != 2 )
return 0LL;
v2 = read(0, plainText, (size_t)&unk_40000000);
__isoc99_sscanf();
result = v2;
key &= 0x3FFu;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
__int64 result; // rax
unsigned int v2; // er12

if ( a1 != 2 )
return 0LL;
v2 = read(0, plainText, (size_t)&unk_40000000);
__isoc99_sscanf();
result = v2;
key &= 0x3FFu;
return result;
}

original_code:
int getArgs(int argc, char **argv){
 int size=0;
 if(argc != 2)
  return 0;
 else {
  size = read(0, plainText, (1<<30));
  sscanf(argv[1], "%hx", &key);
  key = key & 0x03ff;
  return size;
 }
}

canonical_original_code:
int getArgs(int argc, char **argv){
 int size = 0;
 if(argc != 2)
  return 0;
 else {
  size = read(0, plainText, (1<<30));
  sscanf(argv[1], "%hx", &key);
  key = key & 0x03ff;
  return size;
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: size int


verbose

repo: thiakil/rb-download

decompiled_code:
int verbose(const char *a1, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = verbose;
if ( verbose )
{
va_start(arg, a1);
vprintf(a1, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func2(const char *a1, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = verbose;
if ( verbose )
{
va_start(arg, a1);
vprintf(a1, arg);
result = putchar(10);
}
return result;
}

original_code:
void
verbose(char *message, ...)
{
 va_list ap;
 if(_verbose)
 {
         __builtin_va_start(ap,message);
         vprintf(message, ap);
  printf("\n");
         __builtin_va_end(ap);
 }
 return;
}

canonical_original_code:
void verbose(char *message, ...)
{
 __builtin_va_list ap;
 if(_verbose)
 {
         __builtin_va_start(ap,message);
         vprintf(message, ap);
  printf("\n");
         __builtin_va_end(ap);
 }
 return;
}

variable_types:
message
char *


vverbose

decompiled_code:
int vverbose(const char *a1, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( verbose > 1 )
{
va_start(arg, a1);
vprintf(a1, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func3(const char *a1, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( verbose > 1 )
{
va_start(arg, a1);
vprintf(a1, arg);
result = putchar(10);
}
return result;
}

original_code:
void
vverbose(char *message, ...)
{
 va_list ap;
 if(_verbose > 1)
 {
         __builtin_va_start(ap,message);
         vprintf(message, ap);
  printf("\n");
         __builtin_va_end(ap);
 }
 return;
}

canonical_original_code:
void vverbose(char *message, ...)
{
 __builtin_va_list ap;
 if(_verbose > 1)
 {
         __builtin_va_start(ap,message);
         vprintf(message, ap);
  printf("\n");
         __builtin_va_end(ap);
 }
 return;
}


vhexdump

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

if ( verbose > 1 )
result = hexdump();
return result;
}

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

if ( verbose > 1 )
result = hexdump();
return result;
}

original_code:
void
vhexdump(unsigned char *data, size_t nbytes)
{
 if(_verbose > 1)
  hexdump(data, nbytes);
 return;
}

canonical_original_code:
void vhexdump(unsigned char *data, unsigned long nbytes)
{
 if(_verbose > 1)
  hexdump(data, nbytes);
 return;
}

variable_types:
data
unsigned char *

variable_types:
nbytes
unsigned long


usage

decompiled_code:
__int64 __fastcall usage(const char *a1)
{
return fatal(
"Usage: %s [-v] [-a <adapter>] [-x <frontend>] [-y <demux>] [-z <dvr>] [-b <base_dir>] [-t <timeout>] [-f <cha"
"nnels_file>] [-l <listen_addr>] [-c carousel_id] [<service_id>|any[:default_service_id]]",
a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
return fatal(
"Usage: %s [-v] [-a <adapter>] [-x <frontend>] [-y <demux>] [-z <dvr>] [-b <base_dir>] [-t <timeout>] [-f <cha"
"nnels_file>] [-l <listen_addr>] [-c carousel_id] [<service_id>|any[:default_service_id]]",
a1);
}

original_code:
void
usage(char *prog_name)
{
 fatal("Usage: %s [-v] "
   "[-a <adapter>] "
   "[-x <frontend>] "
   "[-y <demux>] "
   "[-z <dvr>] "
   "[-b <base_dir>] "
   "[-t <timeout>] "
   "[-f <channels_file>] "
   "[-l <listen_addr>] "
   "[-c carousel_id] "
   "[<service_id>|any[:default_service_id]]", prog_name);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
char *v4; // r14
unsigned int v5; // er13
unsigned int v6; // er12
int v7; // eax
__int64 v8; // rax
const char *v9; // rdx
int v10; // er14
const char *v11; // r15
unsigned __int16 v12; // ax
__int64 v13; // rbx
const char *v14; // rbx
int *v16; // rax
char *v17; // rax
unsigned int v18; // [rsp+8h] [rbp-60h]
unsigned int v19; // [rsp+Ch] [rbp-5Ch]
const char *v20; // [rsp+10h] [rbp-58h]
unsigned int v21; // [rsp+18h] [rbp-50h]
unsigned int v22; // [rsp+1Ch] [rbp-4Ch]
char v23[2]; // [rsp+20h] [rbp-48h] BYREF
__int16 v24; // [rsp+22h] [rbp-46h] BYREF
int v25[17]; // [rsp+24h] [rbp-44h] BYREF

v3 = 0LL;
v4 = 0LL;
v5 = 0;
v6 = 0;
v20 = *argv;
v25[0] = _bswap_32_constprop_0(argc, argv, envp);
v21 = -1;
v24 = _bswap_16_constprop_0();
v18 = 10;
v22 = 0;
v19 = 0;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "a:x:y:z:b:f:t:l:c:v");
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'a':
v6 = strtoul(optarg, 0LL, 0);
break;
case 'b':
v4 = optarg;
break;
case 'c':
v21 = strtoul(optarg, 0LL, 0);
break;
case 'f':
v3 = optarg;
break;
case 'l':
if ( (int)parse_addr(optarg, v25, &v24) < 0 )
fatal("Unable to resolve host %s", optarg);
break;
case 't':
v18 = strtoul(optarg, 0LL, 0);
break;
case 'v':
++verbose;
break;
case 'x':
v5 = strtoul(optarg, 0LL, 0);
break;
case 'y':
v19 = strtoul(optarg, 0LL, 0);
break;
case 'z':
v22 = strtoul(optarg, 0LL, 0);
break;
default:
usage(v20);
break;
}
}
v8 = zap_name(v6, v5);
if ( !(unsigned __int8)init_channels_conf(v8, v3) )
error((int)"Unable to open channels.conf file", (int)v3, v9);
if ( v4 && chdir(v4) < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
fatal("Unable to cd to '%s': %s", v4, v17);
}
if ( !(unsigned __int8)cache_init() )
fatal("Unable to initialise cache");
if ( optind == argc )
{
v10 = 0;
list_channels(v6, v19, v18);
return v10;
}
v10 = argc - optind;
if ( argc - optind != 1 )
{
v10 = 0;
usage(v20);
return v10;
}
v11 = argv[optind];
if ( strncmp(v11, "any", 3uLL) && strncmp(v11, "ANY", 3uLL) )
{
v13 = (unsigned __int16)strtoul(v11, 0LL, 0);
goto LABEL_30;
}
v12 = get_first_service_id(v6, v19, v18);
v13 = v12;
if ( v12 )
{
verbose("got serviceid from already tuned channel");
goto LABEL_30;
}
v14 = argv[optind];
if ( strlen(v14) > 4 )
{
v13 = (unsigned __int16)strtoul(v14 + 4, 0LL, 0);
LABEL_30:
v10 = 0;
start_listener(v23, v6, v5, v19, v22, v18, v13, v21);
}
return v10;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
char *v4; // r14
unsigned int v5; // er13
unsigned int v6; // er12
int v7; // eax
__int64 v8; // rax
const char *v9; // rdx
int v10; // er14
const char *v11; // r15
unsigned __int16 v12; // ax
__int64 v13; // rbx
const char *v14; // rbx
int *v16; // rax
char *v17; // rax
unsigned int v18; // [rsp+8h] [rbp-60h]
unsigned int v19; // [rsp+Ch] [rbp-5Ch]
const char *v20; // [rsp+10h] [rbp-58h]
unsigned int v21; // [rsp+18h] [rbp-50h]
unsigned int v22; // [rsp+1Ch] [rbp-4Ch]
char v23[2]; // [rsp+20h] [rbp-48h] BYREF
__int16 v24; // [rsp+22h] [rbp-46h] BYREF
int v25[17]; // [rsp+24h] [rbp-44h] BYREF

v3 = 0LL;
v4 = 0LL;
v5 = 0;
v6 = 0;
v20 = *argv;
v25[0] = func0(argc, argv, envp);
v21 = -1;
v24 = func1();
v18 = 10;
v22 = 0;
v19 = 0;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "a:x:y:z:b:f:t:l:c:v");
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'a':
v6 = strtoul(optarg, 0LL, 0);
break;
case 'b':
v4 = optarg;
break;
case 'c':
v21 = strtoul(optarg, 0LL, 0);
break;
case 'f':
v3 = optarg;
break;
case 'l':
if ( (int)parse_addr(optarg, v25, &v24) < 0 )
fatal("Unable to resolve host %s", optarg);
break;
case 't':
v18 = strtoul(optarg, 0LL, 0);
break;
case 'v':
++verbose;
break;
case 'x':
v5 = strtoul(optarg, 0LL, 0);
break;
case 'y':
v19 = strtoul(optarg, 0LL, 0);
break;
case 'z':
v22 = strtoul(optarg, 0LL, 0);
break;
default:
func5(v20);
break;
}
}
v8 = zap_name(v6, v5);
if ( !(unsigned __int8)init_channels_conf(v8, v3) )
error((int)"Unable to open channels.conf file", (int)v3, v9);
if ( v4 && chdir(v4) < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
fatal("Unable to cd to '%s': %s", v4, v17);
}
if ( !(unsigned __int8)cache_init() )
fatal("Unable to initialise cache");
if ( optind == argc )
{
v10 = 0;
list_channels(v6, v19, v18);
return v10;
}
v10 = argc - optind;
if ( argc - optind != 1 )
{
v10 = 0;
func5(v20);
return v10;
}
v11 = argv[optind];
if ( strncmp(v11, "any", 3uLL) && strncmp(v11, "ANY", 3uLL) )
{
v13 = (unsigned __int16)strtoul(v11, 0LL, 0);
goto LABEL_30;
}
v12 = get_first_service_id(v6, v19, v18);
v13 = v12;
if ( v12 )
{
func2("got serviceid from already tuned channel");
goto LABEL_30;
}
v14 = argv[optind];
if ( strlen(v14) > 4 )
{
v13 = (unsigned __int16)strtoul(v14 + 4, 0LL, 0);
LABEL_30:
v10 = 0;
start_listener(v23, v6, v5, v19, v22, v18, v13, v21);
}
return v10;
}

original_code:
int
main(int argc, char *argv[])
{
 char *prog_name = argv[0];
 unsigned int adapter;
 unsigned int frontend;
 unsigned int demux;
 unsigned int dvr;
 char *base_dir;
 unsigned int timeout;
 char *channels_file;
 struct sockaddr_in listen_addr;
 int carousel_id;
 uint16_t service_id;
 int arg;
 adapter = 0;
 frontend = 0;
 demux = 0;
 dvr = 0;
 base_dir = ((void *)0);
 timeout = 10;
 channels_file = ((void *)0);
 listen_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 listen_addr.sin_port = htons(10101);
 carousel_id = -1;
 while((arg = getopt(argc, argv, "a:x:y:z:b:f:t:l:c:v")) != (-1))
 {
  switch(arg)
  {
  case 'a':
   adapter = strtoul(optarg, ((void *)0), 0);
   break;
  case 'x':
   frontend = strtoul(optarg, ((void *)0), 0);
   break;
  case 'y':
   demux = strtoul(optarg, ((void *)0), 0);
   break;
  case 'z':
   dvr = strtoul(optarg, ((void *)0), 0);
   break;
  case 'b':
   base_dir = optarg;
   break;
  case 'f':
   channels_file = optarg;
   break;
  case 't':
   timeout = strtoul(optarg, ((void *)0), 0);
   break;
  case 'l':
   if(parse_addr(optarg, &listen_addr.sin_addr, &listen_addr.sin_port) < 0)
    fatal("Unable to resolve host %s", optarg);
   break;
  case 'c':
   carousel_id = strtoul(optarg, ((void *)0), 0);
   break;
  case 'v':
   _verbose ++;
   break;
  default:
   usage(prog_name);
   break;
  }
 }
 if(!init_channels_conf(zap_name(adapter, frontend), channels_file))
  error("Unable to open channels.conf file");
 if(base_dir != ((void *)0)
 && chdir(base_dir) < 0)
  fatal("Unable to cd to '%s': %s", base_dir, strerror((*__errno_location ())));
 if(!cache_init())
  fatal("Unable to initialise cache");
 if(argc == optind)
 {
  list_channels(adapter, demux, timeout);
 }
 else if(argc - optind == 1)
 {
  if (strncmp(argv[optind], "any", 3) == 0 || strncmp(argv[optind], "ANY", 3) == 0)
    {
      service_id = get_first_service_id(adapter, demux, timeout);
      if (service_id == 0 && strlen(argv[optind]) > 4)
        service_id = strtoul(&argv[optind][4], ((void *)0), 0);
      else if (service_id > 0)
        verbose("got serviceid from already tuned channel");
      else
        return 1;
    }
    else
      service_id = strtoul(argv[optind], ((void *)0), 0);
  start_listener(&listen_addr, adapter, frontend, demux, dvr, timeout, service_id, carousel_id);
 }
 else
 {
  usage(prog_name);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char *prog_name = argv[0];
 unsigned int adapter;
 unsigned int frontend;
 unsigned int demux;
 unsigned int dvr;
 char *base_dir;
 unsigned int timeout;
 char *channels_file;
 struct sockaddr_in listen_addr;
 int carousel_id;
 unsigned short service_id;
 int arg;
 adapter = 0;
 frontend = 0;
 demux = 0;
 dvr = 0;
 base_dir = ((void *)0);
 timeout = 10;
 channels_file = ((void *)0);
 listen_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 listen_addr.sin_port = htons(10101);
 carousel_id = -1;
 while((arg = getopt(argc, argv, "a:x:y:z:b:f:t:l:c:v")) != (-1))
 {
  switch(arg)
  {
  case 'a':
   adapter = strtoul(optarg, ((void *)0), 0);
   break;
  case 'x':
   frontend = strtoul(optarg, ((void *)0), 0);
   break;
  case 'y':
   demux = strtoul(optarg, ((void *)0), 0);
   break;
  case 'z':
   dvr = strtoul(optarg, ((void *)0), 0);
   break;
  case 'b':
   base_dir = optarg;
   break;
  case 'f':
   channels_file = optarg;
   break;
  case 't':
   timeout = strtoul(optarg, ((void *)0), 0);
   break;
  case 'l':
   if(parse_addr(optarg, &listen_addr.sin_addr, &listen_addr.sin_port) < 0)
    fatal("Unable to resolve host %s", optarg);
   break;
  case 'c':
   carousel_id = strtoul(optarg, ((void *)0), 0);
   break;
  case 'v':
   _verbose ++;
   break;
  default:
   usage(prog_name);
   break;
  }
 }
 if(!init_channels_conf(zap_name(adapter, frontend), channels_file))
  error("Unable to open channels.conf file");
 if(base_dir != ((void *)0)
 && chdir(base_dir) < 0)
  fatal("Unable to cd to '%s': %s", base_dir, strerror((*__errno_location ())));
 if(!cache_init())
  fatal("Unable to initialise cache");
 if(argc == optind)
 {
  list_channels(adapter, demux, timeout);
 }
 else if(argc - optind == 1)
 {
  if (strncmp(argv[optind], "any", 3) == 0 || strncmp(argv[optind], "ANY", 3) == 0)
    {
      service_id = get_first_service_id(adapter, demux, timeout);
      if (service_id == 0 && strlen(argv[optind]) > 4)
        service_id = strtoul(&argv[optind][4], ((void *)0), 0);
      else if (service_id > 0)
        verbose("got serviceid from already tuned channel");
      else
        return 1;
    }
    else
      service_id = strtoul(argv[optind], ((void *)0), 0);
  start_listener(&listen_addr, adapter, frontend, demux, dvr, timeout, service_id, carousel_id);
 }
 else
 {
  usage(prog_name);
 }
 return 0;
}

variable_types:
argv
char *[]

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 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: arg int

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


decompiled_code:
__int64 __fastcall read_table(const char *a1, __int16 a2, char a3, unsigned int a4, _BYTE *a5, char a6)
{
char v6; // r15
char v8; // r13
int v10; // eax
int v11; // ebp
__int64 v12; // r14
__int64 v13; // r12
const char *v14; // rdx
int v16; // edi
unsigned int v17; // eax
const char *v18; // rdi
int v19; // esi
int *v20; // rax
char *v21; // rax
int *v22; // rax
int *v23; // rax
int errnum; // [rsp+8h] [rbp-110h]
struct timeval timeout; // [rsp+10h] [rbp-108h] BYREF
__int128 v28; // [rsp+20h] [rbp-F8h] BYREF
__int128 v29; // [rsp+30h] [rbp-E8h]
__int128 v30; // [rsp+40h] [rbp-D8h]
__int64 v31; // [rsp+50h] [rbp-C8h]
int v32; // [rsp+58h] [rbp-C0h]
fd_set readfds; // [rsp+60h] [rbp-B8h] BYREF

v6 = a6;
v8 = a3;
errnum = (int)a1;
v10 = open(a1, 2);
if ( v10 < 0 )
{
v20 = _errno_location();
v21 = strerror(*v20);
error((int)"open '%s': %s", (int)a1, v21);
return 0LL;
}
v11 = v10;
v28 = 0LL;
v29 = 0LL;
v31 = 0LL;
LOWORD(v28) = a2;
v32 = 4;
BYTE2(v28) = v8;
BYTE2(v29) = -1;
BYTE6(v28) = v6;
BYTE6(v29) = -1;
v30 = 0LL;
if ( ioctl(v10, 0x403C6F2BuLL, &v28) < 0 )
{
v22 = _errno_location();
v18 = "ioctl DMX_SET_FILTER: %s";
v19 = (unsigned int)strerror(*v22);
goto LABEL_12;
}
timeout.tv_sec = a4;
v12 = 1LL << v11;
v13 = v11 >> 6;
timeout.tv_usec = 0LL;
while ( 1 )
{
*a5 = ~v8;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v13] |= v12;
if ( select(v11 + 1, &readfds, 0LL, 0LL, &timeout) < 0 )
{
v16 = *_errno_location();
if ( v16 != 4 )
{
v17 = (unsigned int)strerror(v16);
v18 = "read_table: select: %s";
v19 = v17;
goto LABEL_12;
}
goto LABEL_7;
}
if ( (readfds.fds_bits[v13] & v12) == 0 )
{
v19 = errnum;
v18 = "Timeout reading %s";
goto LABEL_12;
}
if ( (int)read(v11, a5, 0x1000uLL) < 0 )
break;
LABEL_7:
if ( *a5 == a3 && a5[6] == a6 )
{
close(v11);
return 1LL;
}
}
v23 = _errno_location();
v18 = "read: %s";
v19 = (unsigned int)strerror(*v23);
LABEL_12:
error((int)v18, v19, v14);
close(v11);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, __int16 a2, char a3, unsigned int a4, _BYTE *a5, char a6)
{
char v6; // r15
char v8; // r13
int v10; // eax
int v11; // ebp
__int64 v12; // r14
__int64 v13; // r12
const char *v14; // rdx
int v16; // edi
unsigned int v17; // eax
const char *v18; // rdi
int v19; // esi
int *v20; // rax
char *v21; // rax
int *v22; // rax
int *v23; // rax
int errnum; // [rsp+8h] [rbp-110h]
struct timeval timeout; // [rsp+10h] [rbp-108h] BYREF
__int128 v28; // [rsp+20h] [rbp-F8h] BYREF
__int128 v29; // [rsp+30h] [rbp-E8h]
__int128 v30; // [rsp+40h] [rbp-D8h]
__int64 v31; // [rsp+50h] [rbp-C8h]
int v32; // [rsp+58h] [rbp-C0h]
fd_set readfds; // [rsp+60h] [rbp-B8h] BYREF

v6 = a6;
v8 = a3;
errnum = (int)a1;
v10 = open(a1, 2);
if ( v10 < 0 )
{
v20 = _errno_location();
v21 = strerror(*v20);
error((int)"open '%s': %s", (int)a1, v21);
return 0LL;
}
v11 = v10;
v28 = 0LL;
v29 = 0LL;
v31 = 0LL;
LOWORD(v28) = a2;
v32 = 4;
BYTE2(v28) = v8;
BYTE2(v29) = -1;
BYTE6(v28) = v6;
BYTE6(v29) = -1;
v30 = 0LL;
if ( ioctl(v10, 0x403C6F2BuLL, &v28) < 0 )
{
v22 = _errno_location();
v18 = "ioctl DMX_SET_FILTER: %s";
v19 = (unsigned int)strerror(*v22);
goto LABEL_12;
}
timeout.tv_sec = a4;
v12 = 1LL << v11;
v13 = v11 >> 6;
timeout.tv_usec = 0LL;
while ( 1 )
{
*a5 = ~v8;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v13] |= v12;
if ( select(v11 + 1, &readfds, 0LL, 0LL, &timeout) < 0 )
{
v16 = *_errno_location();
if ( v16 != 4 )
{
v17 = (unsigned int)strerror(v16);
v18 = "read_table: select: %s";
v19 = v17;
goto LABEL_12;
}
goto LABEL_7;
}
if ( (readfds.fds_bits[v13] & v12) == 0 )
{
v19 = errnum;
v18 = "Timeout reading %s";
goto LABEL_12;
}
if ( (int)read(v11, a5, 0x1000uLL) < 0 )
break;
LABEL_7:
if ( *a5 == a3 && a5[6] == a6 )
{
close(v11);
return 1LL;
}
}
v23 = _errno_location();
v18 = "read: %s";
v19 = (unsigned int)strerror(*v23);
LABEL_12:
error((int)v18, v19, v14);
close(v11);
return 0LL;
}

original_code:
_Bool
read_table(char *device, uint16_t pid, uint8_t tid, unsigned int secs, unsigned char *out, unsigned char sn)
{
 int fd_data;
 struct dmx_sct_filter_params sctFilterParams;
 fd_set readfds;
 struct timeval timeout;
 int n;
 if((fd_data = open(device, 02)) < 0)
 {
  error("open '%s': %s", device, strerror((*__errno_location ())));
  return 0;
 }
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = tid;
 sctFilterParams.filter.mask[0] = 0xff;
 sctFilterParams.filter.filter[4] = sn;
 sctFilterParams.filter.mask[4] = 0xff;
 if(ioctl(fd_data, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
 {
  error("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
  close(fd_data);
  return 0;
 }
 timeout.tv_sec = secs;
 timeout.tv_usec = 0;
 do
 {
  out[0] = ~tid;
  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);
  ((void) (((&readfds)->__fds_bits)[((fd_data) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd_data) % (8 * (int) sizeof (__fd_mask)))))));
  if(select(fd_data + 1, &readfds, ((void *)0), ((void *)0), &timeout) < 0)
  {
   if((*__errno_location ()) == 4)
    continue;
   error("read_table: select: %s", strerror((*__errno_location ())));
   close(fd_data);
   return 0;
  }
  if(((((&readfds)->__fds_bits)[((fd_data) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd_data) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   if((n = read(fd_data, out, 4096)) < 0)
   {
    error("read: %s", strerror((*__errno_location ())));
    close(fd_data);
    return 0;
   }
  }
  else
  {
   error("Timeout reading %s", device);
   close(fd_data);
   return 0;
  }
 }
 while(out[0] != tid || out[6] != sn);
 close(fd_data);
 return 1;
}

canonical_original_code:
_Bool read_table(char *device, unsigned short pid, unsigned char tid, unsigned int secs, unsigned char *out, unsigned char sn)
{
 int fd_data;
 struct dmx_sct_filter_params sctFilterParams;
 struct fd_set readfds;
 struct timeval timeout;
 int n;
 if((fd_data = open(device, 02)) < 0)
 {
  error("open '%s': %s", device, strerror((*__errno_location ())));
  return 0;
 }
 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
 sctFilterParams.pid = pid;
 sctFilterParams.timeout = 0;
 sctFilterParams.flags = 4;
 sctFilterParams.filter.filter[0] = tid;
 sctFilterParams.filter.mask[0] = 0xff;
 sctFilterParams.filter.filter[4] = sn;
 sctFilterParams.filter.mask[4] = 0xff;
 if(ioctl(fd_data, (((1U) << (((0 +8)+8)+14)) | ((('o')) << (0 +8)) | (((43)) << 0) | ((((sizeof(struct dmx_sct_filter_params)))) << ((0 +8)+8))), &sctFilterParams) < 0)
 {
  error("ioctl DMX_SET_FILTER: %s", strerror((*__errno_location ())));
  close(fd_data);
  return 0;
 }
 timeout.tv_sec = secs;
 timeout.tv_usec = 0;
 do
 {
  out[0] = ~tid;
  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);
  ((void) (((&readfds)->__fds_bits)[((fd_data) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd_data) % (8 * (int) sizeof (__fd_mask)))))));
  if(select(fd_data + 1, &readfds, ((void *)0), ((void *)0), &timeout) < 0)
  {
   if((*__errno_location ()) == 4)
    continue;
   error("read_table: select: %s", strerror((*__errno_location ())));
   close(fd_data);
   return 0;
  }
  if(((((&readfds)->__fds_bits)[((fd_data) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd_data) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   if((n = read(fd_data, out, 4096)) < 0)
   {
    error("read: %s", strerror((*__errno_location ())));
    close(fd_data);
    return 0;
   }
  }
  else
  {
   error("Timeout reading %s", device);
   close(fd_data);
   return 0;
  }
 }
 while(out[0] != tid || out[6] != sn);
 close(fd_data);
 return 1;
}

variable_types:
device
char *

variable_types:
pid
unsigned short

variable_types:
tid
unsigned char

variable_types:
secs
unsigned int

variable_types:
out
unsigned char *

variable_types:
sn
unsigned char

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

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

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

variable_types: n int

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


decompiled_code:
__int64 __fastcall read_pat(const char *a1, unsigned int a2, _BYTE *a3)
{
unsigned int v4; // er12
const char *v6; // rdx
unsigned int v7; // er12

v4 = cache_load("pat", a3);
if ( (_BYTE)v4 )
return v4;
v7 = read_table(a1, 0, 0, a2, a3, 0);
if ( (_BYTE)v7 )
cache_save("pat", a3);
else
error((int)"Unable to read PAT", 0, v6);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, unsigned int a2, _BYTE *a3)
{
unsigned int v4; // er12
const char *v6; // rdx
unsigned int v7; // er12

v4 = cache_load("pat", a3);
if ( (_BYTE)v4 )
return v4;
v7 = func0(a1, 0, 0, a2, a3, 0);
if ( (_BYTE)v7 )
cache_save("pat", a3);
else
error((int)"Unable to read PAT", 0, v6);
return v7;
}

original_code:
_Bool
read_pat(char *demux, unsigned int timeout, unsigned char *out)
{
 if(cache_load("pat", out))
  return 1;
 if(!read_table(demux, 0x0000, 0x00, timeout, out, 0))
 {
  error("Unable to read PAT");
  return 0;
 }
 cache_save("pat", out);
 return 1;
}

variable_types:
demux
char *


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

v4 = a2;
snprintf(v16, 0x1000uLL, "pmt-%u", a2);
v8 = cache_load(v16, a4);
if ( !(_BYTE)v8 )
{
v8 = read_pat(a1, a3, v17);
if ( (_BYTE)v8 )
{
v10 = (unsigned __int16)((((unsigned __int8)v17[1] << 8) & 0xF00) + (unsigned __int8)v17[2] + 3) - 4;
if ( v10 <= 8 )
{
LABEL_8:
v13 = 0;
fatal("Unable to find PMT PID for service_id %u", v4);
}
else
{
v11 = 8;
v12 = 8;
while ( v4 != (unsigned __int8)v17[v12 + 1] + ((unsigned __int8)v17[v12] << 8) )
{
v11 += 4;
v12 = (unsigned __int16)v11;
if ( v10 <= (unsigned __int16)v11 )
goto LABEL_8;
}
v13 = (unsigned __int16)((unsigned __int8)v17[v12 + 3] + (((unsigned __int8)v17[v12 + 2] << 8) & 0x1F00));
}
vverbose("PMT PID: %u", v13);
v14 = v13;
v8 = read_table(a1, v13, 2, a3, a4, 0);
if ( (_BYTE)v8 )
cache_save(v16, 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 v4; // er15
unsigned int v8; // er14
int v10; // esi
int v11; // edi
int v12; // edx
unsigned int v13; // er14
int v14; // esi
const char *v15; // rdx
char v16[4096]; // [rsp+0h] [rbp-2028h] BYREF
char v17[4136]; // [rsp+1000h] [rbp-1028h] BYREF

v4 = a2;
snprintf(v16, 0x1000uLL, "pmt-%u", a2);
v8 = cache_load(v16, a4);
if ( !(_BYTE)v8 )
{
v8 = func1(a1, a3, v17);
if ( (_BYTE)v8 )
{
v10 = (unsigned __int16)((((unsigned __int8)v17[1] << 8) & 0xF00) + (unsigned __int8)v17[2] + 3) - 4;
if ( v10 <= 8 )
{
LABEL_8:
v13 = 0;
fatal("Unable to find PMT PID for service_id %u", v4);
}
else
{
v11 = 8;
v12 = 8;
while ( v4 != (unsigned __int8)v17[v12 + 1] + ((unsigned __int8)v17[v12] << 8) )
{
v11 += 4;
v12 = (unsigned __int16)v11;
if ( v10 <= (unsigned __int16)v11 )
goto LABEL_8;
}
v13 = (unsigned __int16)((unsigned __int8)v17[v12 + 3] + (((unsigned __int8)v17[v12 + 2] << 8) & 0x1F00));
}
vverbose("PMT PID: %u", v13);
v14 = v13;
v8 = func0(a1, v13, 2, a3, a4, 0);
if ( (_BYTE)v8 )
cache_save(v16, 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


decompiled_code:
__int64 __fastcall read_sdt(const char *a1, unsigned int a2, _BYTE *a3, unsigned __int8 a4)
{
unsigned int v6; // er12
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, 17, 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; // er12
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, 17, 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;
}


decompiled_code:
__int64 __fastcall read_dsmcc_tables(__int64 errnum, void *buf)
{
__time_t v4; // rax
int v5; // eax
__int64 v6; // rcx
int v7; // edi
int *v8; // rdx
__int64 v9; // rsi
int v10; // eax
int v11; // er8
int v12; // edi
unsigned int v13; // er8
const char *v14; // rdx
__int64 v15; // rsi
int v16; // edi
int v17; // eax
int v18; // eax
int *v20; // rax
int v21; // eax
const char *v22; // rdx
int *v23; // rax
int v24; // eax
const char *v25; // rdx
struct timeval v26; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF

v4 = *(unsigned int *)(errnum + 0x2000);
v26.tv_usec = 0LL;
v26.tv_sec = v4;
while ( 1 )
{
*(_BYTE *)buf = 0;
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)(errnum + 8256);
if ( v5 )
{
v6 = *(_QWORD *)(errnum + 8264);
v7 = 0;
v8 = (int *)(v6 + 4);
v9 = v6 + 12LL * (unsigned int)(v5 - 1) + 16;
do
{
v10 = v8[1];
v11 = v10;
if ( *v8 >= v10 )
v11 = *v8;
if ( v7 < v11 )
v7 = v11;
readfds.fds_bits[*v8 / 64] |= 1LL << (*v8 % 64);
v8 += 3;
readfds.fds_bits[v10 / 64] |= 1LL << (v10 % 64);
}
while ( (int *)v9 != v8 );
v12 = v7 + 1;
}
else
{
v12 = 1;
}
if ( select(v12, &readfds, 0LL, 0LL, &v26) < 0 )
break;
v13 = *(_DWORD *)(errnum + 8256);
v14 = 0LL;
do
{
if ( v13 <= (unsigned int)v14 )
{
LABEL_21:
error((int)"Timeout reading %s", errnum, v14);
return 0LL;
}
v15 = *(_QWORD *)(errnum + 8264) + 12LL * (unsigned int)v14;
while ( 1 )
{
v16 = *(_DWORD *)(v15 + 4);
v14 = (const char *)(unsigned int)((_DWORD)v14 + 1);
v17 = v16 + 63;
if ( v16 >= 0 )
v17 = *(_DWORD *)(v15 + 4);
if ( (readfds.fds_bits[v17 >> 6] & (1LL << (*(_DWORD *)(v15 + 4) % 64))) != 0 )
break;
v16 = *(_DWORD *)(v15 + 8);
v18 = v16 + 63;
if ( v16 >= 0 )
v18 = *(_DWORD *)(v15 + 8);
if ( (readfds.fds_bits[v18 >> 6] & (1LL << (*(_DWORD *)(v15 + 8) % 64))) != 0 )
break;
v15 += 12LL;
if ( v13 == (_DWORD)v14 )
goto LABEL_21;
}
*(_WORD *)(errnum + 8218) = *(_WORD *)v15;
}
while ( v16 == -1 );
if ( (int)read(v16, buf, 0x1000uLL) < 0 )
{
v20 = _errno_location();
v21 = (unsigned int)strerror(*v20);
error((int)"read: %s", v21, v22);
}
else if ( (unsigned __int8)(*(_BYTE *)buf - 59) <= 1u )
{
return 1LL;
}
}
v23 = _errno_location();
v24 = (unsigned int)strerror(*v23);
error((int)"read_dsmcc_tables: select: %s", v24, v25);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 errnum, void *buf)
{
__time_t v4; // rax
int v5; // eax
__int64 v6; // rcx
int v7; // edi
int *v8; // rdx
__int64 v9; // rsi
int v10; // eax
int v11; // er8
int v12; // edi
unsigned int v13; // er8
const char *v14; // rdx
__int64 v15; // rsi
int v16; // edi
int v17; // eax
int v18; // eax
int *v20; // rax
int v21; // eax
const char *v22; // rdx
int *v23; // rax
int v24; // eax
const char *v25; // rdx
struct timeval v26; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF

v4 = *(unsigned int *)(errnum + 0x2000);
v26.tv_usec = 0LL;
v26.tv_sec = v4;
while ( 1 )
{
*(_BYTE *)buf = 0;
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)(errnum + 8256);
if ( v5 )
{
v6 = *(_QWORD *)(errnum + 8264);
v7 = 0;
v8 = (int *)(v6 + 4);
v9 = v6 + 12LL * (unsigned int)(v5 - 1) + 16;
do
{
v10 = v8[1];
v11 = v10;
if ( *v8 >= v10 )
v11 = *v8;
if ( v7 < v11 )
v7 = v11;
readfds.fds_bits[*v8 / 64] |= 1LL << (*v8 % 64);
v8 += 3;
readfds.fds_bits[v10 / 64] |= 1LL << (v10 % 64);
}
while ( (int *)v9 != v8 );
v12 = v7 + 1;
}
else
{
v12 = 1;
}
if ( select(v12, &readfds, 0LL, 0LL, &v26) < 0 )
break;
v13 = *(_DWORD *)(errnum + 8256);
v14 = 0LL;
do
{
if ( v13 <= (unsigned int)v14 )
{
LABEL_21:
error((int)"Timeout reading %s", errnum, v14);
return 0LL;
}
v15 = *(_QWORD *)(errnum + 8264) + 12LL * (unsigned int)v14;
while ( 1 )
{
v16 = *(_DWORD *)(v15 + 4);
v14 = (const char *)(unsigned int)((_DWORD)v14 + 1);
v17 = v16 + 63;
if ( v16 >= 0 )
v17 = *(_DWORD *)(v15 + 4);
if ( (readfds.fds_bits[v17 >> 6] & (1LL << (*(_DWORD *)(v15 + 4) % 64))) != 0 )
break;
v16 = *(_DWORD *)(v15 + 8);
v18 = v16 + 63;
if ( v16 >= 0 )
v18 = *(_DWORD *)(v15 + 8);
if ( (readfds.fds_bits[v18 >> 6] & (1LL << (*(_DWORD *)(v15 + 8) % 64))) != 0 )
break;
v15 += 12LL;
if ( v13 == (_DWORD)v14 )
goto LABEL_21;
}
*(_WORD *)(errnum + 8218) = *(_WORD *)v15;
}
while ( v16 == -1 );
if ( (int)read(v16, buf, 0x1000uLL) < 0 )
{
v20 = _errno_location();
v21 = (unsigned int)strerror(*v20);
error((int)"read: %s", v21, v22);
}
else if ( (unsigned __int8)(*(_BYTE *)buf - 59) <= 1u )
{
return 1LL;
}
}
v23 = _errno_location();
v24 = (unsigned int)strerror(*v23);
error((int)"read_dsmcc_tables: select: %s", v24, v25);
return 0LL;
}

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:
i
unsigned int

variable_types: max int

variable_types: fd int

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

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: struct pid_fds { unsigned short pid; int fd_ctrl; int fd_data; } *

user_defined_types: struct dmx_filter { unsigned char filter[16]; unsigned char mask[16]; unsigned char mode[16]; }

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 func1(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 func2(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 func3(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 func4(int a1)
{
__int64 result; // rax

if ( (unsigned __int8)a1 > 0xFu )
func3("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


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 func5(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
func3("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 func6(void *a1, size_t a2)
{
void *result; // rax

if ( !a1 )
return func5(a2);
result = realloc(a1, a2);
if ( !result )
func3("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; // r13
__int64 result; // rax
unsigned int v7; // ebx
__int64 v8; // r12
unsigned __int16 v9; // ax
unsigned int v10; // eax
unsigned __int16 v11; // 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
{
while ( 1 )
{
v8 = DII_module(a2, 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);
_bswap_32(*a2);
v11 = _bswap_16(*(_WORD *)v8);
result = find_module(a1, v11, *(unsigned __int8 *)(v8 + 6));
if ( !result )
break;
if ( (_DWORD)v5 == ++v7 )
return result;
}
++v7;
result = add_module(a1, a2, v8);
}
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; // r13
__int64 result; // rax
unsigned int v7; // ebx
__int64 v8; // r12
unsigned __int16 v9; // ax
unsigned int v10; // eax
unsigned __int16 v11; // 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
{
while ( 1 )
{
v8 = DII_module(a2, 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);
func0(*a2);
v11 = func1(*(_WORD *)v8);
result = find_module(a1, v11, *(unsigned __int8 *)(v8 + 6));
if ( !result )
break;
if ( (_DWORD)v5 == ++v7 )
return result;
}
++v7;
result = add_module(a1, a2, v8);
}
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 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: 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; }

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


decompiled_code:
__int64 __fastcall process_dsi(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned __int16 v3; // bx
__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; // bx
__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 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 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; }


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

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

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

verbose("DownloadDataBlock");
vverbose("downloadId: %u", a3);
v7 = func1(*(_WORD *)a2);
vverbose("moduleId: %u", v7);
vverbose("moduleVersion: %u", *(unsigned __int8 *)(a2 + 2));
v8 = func1(*(_WORD *)(a2 + 4));
vverbose("blockNumber: %u", v8);
vverbose("blockLength: %u", a4);
v9 = DDB_blockDataByte(a2);
vhexdump(v9, a4);
v10 = func1(*(_WORD *)a2);
result = find_module(a1, v10, *(unsigned __int8 *)(a2 + 2));
if ( result )
{
v12 = result;
v13 = func1(*(_WORD *)(a2 + 4));
result = download_block(a1, v12, v13);
}
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 v3; // ax
const char *v4; // rdx
unsigned int v5; // er14
unsigned int v6; // er13
__int64 v7; // rax
unsigned int v8; // er13
unsigned int *v9; // rax
__int64 v10; // rax
struct timeval v11; // [rsp+0h] [rbp-1048h] BYREF
struct timeval tv; // [rsp+10h] [rbp-1038h] BYREF
char v13[8]; // [rsp+20h] [rbp-1028h] BYREF
char v14[2]; // [rsp+28h] [rbp-1020h] BYREF
__int16 v15; // [rsp+2Ah] [rbp-101Eh]
unsigned int v16; // [rsp+2Ch] [rbp-101Ch]

v1 = 0;
*(_DWORD *)(a1 + 8276) = 0;
*(_QWORD *)(a1 + 8280) = 0LL;
gettimeofday(&tv, 0LL);
gettimeofday(&v11, 0LL);
while ( 1 )
{
while ( 1 )
{
if ( !(unsigned __int8)read_dsmcc_tables(a1, v13) )
fatal("Unable to read PID");
if ( v14[0] == 17 && v14[1] == 3 )
{
v3 = _bswap_16(v15);
switch ( v3 )
{
case 0x1002u:
v8 = _bswap_32(v16);
v9 = (unsigned int *)dsmccMessage(v14);
process_dii(a1, v9, v8);
break;
case 0x1006u:
v10 = dsmccMessage(v14);
process_dsi(a1, v10);
break;
case 0x1003u:
v5 = DDB_blockDataLength(v14);
v6 = _bswap_32(v16);
v7 = dsmccMessage(v14);
process_ddb(a1, v7, v6, v5);
break;
default:
error((int)"Unknown DSMCC messageId: 0x%x", v3, v4);
break;
}
}
if ( *(_DWORD *)(a1 + 8276) == v1 )
break;
v1 = *(_DWORD *)(a1 + 8276);
gettimeofday(&v11, 0LL);
}
if ( v11.tv_sec > 0 )
{
gettimeofday(&tv, 0LL);
if ( tv.tv_sec - v11.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 v3; // ax
const char *v4; // rdx
unsigned int v5; // er14
unsigned int v6; // er13
__int64 v7; // rax
unsigned int v8; // er13
unsigned int *v9; // rax
__int64 v10; // rax
struct timeval v11; // [rsp+0h] [rbp-1048h] BYREF
struct timeval tv; // [rsp+10h] [rbp-1038h] BYREF
char v13[8]; // [rsp+20h] [rbp-1028h] BYREF
char v14[2]; // [rsp+28h] [rbp-1020h] BYREF
__int16 v15; // [rsp+2Ah] [rbp-101Eh]
unsigned int v16; // [rsp+2Ch] [rbp-101Ch]

v1 = 0;
*(_DWORD *)(a1 + 8276) = 0;
*(_QWORD *)(a1 + 8280) = 0LL;
gettimeofday(&tv, 0LL);
gettimeofday(&v11, 0LL);
while ( 1 )
{
while ( 1 )
{
if ( !(unsigned __int8)read_dsmcc_tables(a1, v13) )
fatal("Unable to read PID");
if ( v14[0] == 17 && v14[1] == 3 )
{
v3 = func1(v15);
switch ( v3 )
{
case 0x1002u:
v8 = func0(v16);
v9 = (unsigned int *)dsmccMessage(v14);
func2(a1, v9, v8);
break;
case 0x1006u:
v10 = dsmccMessage(v14);
func3(a1, v10);
break;
case 0x1003u:
v5 = DDB_blockDataLength(v14);
v6 = func0(v16);
v7 = dsmccMessage(v14);
func4(a1, v7, v6, v5);
break;
default:
error((int)"Unknown DSMCC messageId: 0x%x", v3, v4);
break;
}
}
if ( *(_DWORD *)(a1 + 8276) == v1 )
break;
v1 = *(_DWORD *)(a1 + 8276);
gettimeofday(&v11, 0LL);
}
if ( v11.tv_sec > 0 )
{
gettimeofday(&tv, 0LL);
if ( tv.tv_sec - v11.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)
{
__int64 v2; // rdx
const char *v3; // rax
__int64 result; // rax

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

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

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

variable_types:
i
unsigned int

return_type: unsigned short


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

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

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

v2 = (const char *)*a1;
if ( (_DWORD)v2 )
{
v3 = 0LL;
while ( *(_BYTE *)(*((_QWORD *)a1 + 3) + v3) != a2 )
{
if ( (const char *)++v3 == v2 )
goto LABEL_6;
}
result = *(unsigned __int16 *)(*((_QWORD *)a1 + 1) + 2 * v3);
}
else
{
LABEL_6:
error((int)"Unknown PID for stream type %u", a2, 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)
{
const char *v2; // rdx
__int64 v3; // rax
__int64 result; // rax

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

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

v2 = (const char *)*a1;
if ( (_DWORD)v2 )
{
v3 = 0LL;
while ( *(_WORD *)(*((_QWORD *)a1 + 2) + 2 * v3) != a2 )
{
if ( (const char *)++v3 == v2 )
goto LABEL_6;
}
result = *(unsigned __int8 *)(*((_QWORD *)a1 + 3) + v3);
}
else
{
LABEL_6:
error((int)"Unknown stream type for association tag %u", a2, 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
__int64 v2; // rcx

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

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

v1 = func0(*(_WORD *)(a1 + 16));
return (*(unsigned __int8 *)(v2 + (unsigned int)v1 + 18) << 8)
+ (unsigned int)*(unsigned __int8 *)(v2 + (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
__int64 v3; // rax

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

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

v2 = a2;
if ( (unsigned __int16)func2(a1) <= a2 )
fatal("DII_module: index too large");
v3 = (unsigned int)(unsigned __int16)func0(*(_WORD *)(a1 + 16)) + 20;
if ( a2 )
{
do
{
v3 = (unsigned int)v3 + *(unsigned __int8 *)(a1 + (unsigned int)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)
{
unsigned __int16 v1; // ax
__int64 v2; // rdx

v1 = _bswap_16(*(_WORD *)(a1 + 10));
return v1 - (unsigned int)*(unsigned __int8 *)(v2 + 9) - 6;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
unsigned __int16 v1; // ax
__int64 v2; // rdx

v1 = func0(*(_WORD *)(a1 + 10));
return v1 - (unsigned int)*(unsigned __int8 *)(v2 + 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