decompiled_code:
__int64 __fastcall fb_lookup(unsigned int a1, __int64 a2)
{
int v3; // edx
int v4; // eax
__int64 v5; // r9
int v6; // esi
int v7; // ecx
int v8; // ecx
__int64 v9; // r8
__int64 v10; // rcx
unsigned __int64 v11; // rdx
__int64 result; // rax

v3 = *(_DWORD *)(a2 + 32) - 1;
v4 = v3 / 2;
if ( v3 <= 1 )
{
v6 = 0;
LABEL_7:
if ( v6 >= v3 )
{
LABEL_12:
result = -a1;
}
else
{
v9 = *(_QWORD *)(a2 + 48);
v10 = v6 + 1;
v11 = v6 + 2 + (unsigned __int64)(unsigned int)(v3 - 1 - v6);
while ( 1 )
{
result = (unsigned int)v10;
if ( *(_DWORD *)(v9 + 4 * v10 - 4) == a1 )
break;
if ( ++v10 == v11 )
goto LABEL_12;
}
}
}
else
{
v5 = *(_QWORD *)(a2 + 48);
v6 = 0;
while ( 1 )
{
while ( *(_DWORD *)(v5 + 4LL * v4) > a1 )
{
v3 = v4;
v7 = v4 - v6;
v4 = (v6 + v4) / 2;
if ( v7 <= 1 )
goto LABEL_7;
}
if ( *(_DWORD *)(v5 + 4LL * v4) >= a1 )
break;
v6 = v4;
v8 = v3 - v4;
v4 = (v3 + v4) / 2;
if ( v8 <= 1 )
goto LABEL_7;
}
result = (unsigned int)(v4 + 1);
}
return result;
}

original_code:
int fb_lookup (uint32_t p, nsieve_t *ns){
 int low = 0;
 int high = ns->fb_len - 1;
 int guess = (low + high)/2;
 while (high - low > 1){
  if (p < ns->fb[guess]){
   high = guess;
   guess = (high + low)/2;
  } else if (p > ns->fb[guess]){
   low = guess;
   guess = (high + low)/2;
  } else {
   return guess + 1;
  }
 }
 for (int i=low; i<high; i++){
  if (p == ns->fb[i]){
   return i+1;
  }
 }
 return -p;
}

canonical_original_code:
int fb_lookup(unsigned int p, struct nsieve_t *ns){
 int low = 0;
 int high = ns->fb_len - 1;
 int guess = (low + high)/2;
 while (high - low > 1){
  if (p < ns->fb[guess]){
   high = guess;
   guess = (high + low)/2;
  } else if (p > ns->fb[guess]){
   low = guess;
   guess = (high + low)/2;
  } else {
   return guess + 1;
  }
 }
 for (int i = low; i<high; i++){
  if (p == ns->fb[i]){
   return i+1;
  }
 }
 return -p;
}

variable_types: struct nsieve_t { struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; } N[1]; unsigned char k; unsigned short bvals; unsigned int M; float T; unsigned int fb_bound; unsigned int fb_len; int extra_rels; unsigned int rels_needed; unsigned int *fb; unsigned char *fb_logs; unsigned int *roots; unsigned int multiplier; unsigned int nfull; unsigned int npartial; unsigned int row_len; struct matrel_t { struct relation { struct poly_t { struct poly_group_t { struct __mpz_struct a[1]; struct __mpz_struct (*bvals)[1]; unsigned int gvals[12]; unsigned int *ainverses; struct relation *victim; unsigned int nrels; struct relation **relns; } *group; struct __mpz_struct a[1]; struct __mpz_struct b[1]; struct __mpz_struct c[1]; unsigned int *bmodp; unsigned int M; } *poly; int x; unsigned int cofactor; struct flentry { unsigned int fac; struct flentry *next; } *factors; } *r1; struct relation *r2; unsigned long *row; } *relns; unsigned int lp_bound; struct hashtable_t { unsigned int nbuckets; unsigned int nentries; struct htentry { struct relation *rel; struct htentry *next; } **buckets; } partials; int nthreads; unsigned long *threads; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; int info_npoly; int info_npg; unsigned int tdiv_ct; unsigned long sieve_locs; struct time_data_t { long init_time; long sieve_time; long filter_time; long matsolve_time; long facdeduct_time; long total_time; } timing; } *

variable_types: low int

variable_types: high int

variable_types: guess int

variable_types: i int


repo: albinoBandicoot/nsieve

decompiled_code:
clock_t __fastcall combine_partials(__int64 a1)
{
clock_t v2; // r12
__int64 i; // rbp
_QWORD *v4; // rdi
clock_t result; // rax

v2 = clock();
if ( *(_DWORD *)(a1 + 104) )
{
for ( i = 0LL; *(_DWORD *)(a1 + 104) > (unsigned int)i; ++i )
{
if ( __ROR4__(-1030792151 * i, 2) > 0x28F5C28u )
{
if ( *(_DWORD *)(a1 + 76) >= *(_DWORD *)(a1 + 40) )
break;
}
else
{
printf("Combining bucket %d\r", (unsigned int)i);
fflush(stdout);
if ( *(_DWORD *)(a1 + 76) >= *(_DWORD *)(a1 + 40) )
break;
}
v4 = *(_QWORD **)(*(_QWORD *)(a1 + 112) + 8 * i);
combine_bucket(v4, a1);
}
}
result = clock() - v2;
*(_QWORD *)(a1 + 216) = result;
return result;
}

canonical_decompiled_code:
clock_t __fastcall func1(__int64 a1)
{
clock_t v2; // r12
__int64 i; // rbp
_QWORD *v4; // rdi
clock_t result; // rax

v2 = clock();
if ( *(_DWORD *)(a1 + 104) )
{
for ( i = 0LL; *(_DWORD *)(a1 + 104) > (unsigned int)i; ++i )
{
if ( __ROR4__(-1030792151 * i, 2) > 0x28F5C28u )
{
if ( *(_DWORD *)(a1 + 76) >= *(_DWORD *)(a1 + 40) )
break;
}
else
{
printf("Combining bucket %d\r", (unsigned int)i);
fflush(stdout);
if ( *(_DWORD *)(a1 + 76) >= *(_DWORD *)(a1 + 40) )
break;
}
v4 = *(_QWORD **)(*(_QWORD *)(a1 + 112) + 8 * i);
func0(v4, a1);
}
}
result = clock() - v2;
*(_QWORD *)(a1 + 216) = result;
return result;
}

original_code:
void combine_partials (nsieve_t *ns){
 long start = clock();
 for (int i=0; i < ns->partials.nbuckets; i++){
  if (i % 100 == 0){
   printf("Combining bucket %d\r", i);
   fflush(stdout);
  }
  if (ns->nfull >= ns->rels_needed){
   ns->timing.filter_time = clock() - start;
   return;
  }
  combine_bucket (ns->partials.buckets[i], ns);
 }
 ns->timing.filter_time = clock() - start;
}

canonical_original_code:
void combine_partials(struct nsieve_t *ns){
 long start = clock();
 for (int i = 0; i < ns->partials.nbuckets; i++){
  if (i % 100 == 0){
   printf("Combining bucket %d\r", i);
   fflush(stdout);
  }
  if (ns->nfull >= ns->rels_needed){
   ns->timing.filter_time = clock() - start;
   return;
  }
  combine_bucket (ns->partials.buckets[i], ns);
 }
 ns->timing.filter_time = clock() - start;
}

variable_types: start long

return_type: void

user_defined_types: struct nsieve_t { struct __mpz_struct N[1]; unsigned char k; unsigned short bvals; unsigned int M; float T; unsigned int fb_bound; unsigned int fb_len; int extra_rels; unsigned int rels_needed; unsigned int *fb; unsigned char *fb_logs; unsigned int *roots; unsigned int multiplier; unsigned int nfull; unsigned int npartial; unsigned int row_len; struct matrel_t *relns; unsigned int lp_bound; struct hashtable_t partials; int nthreads; unsigned long *threads; union pthread_mutex_t lock; int info_npoly; int info_npg; unsigned int tdiv_ct; unsigned long sieve_locs; struct time_data_t timing; }

user_defined_types: struct hashtable_t { unsigned int nbuckets; unsigned int nentries; struct htentry **buckets; }

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

user_defined_types: struct time_data_t { long init_time; long sieve_time; long filter_time; long matsolve_time; long facdeduct_time; long total_time; }

user_defined_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; }

user_defined_types: struct matrel_t { struct relation *r1; struct relation *r2; unsigned long *row; }

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

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

user_defined_types: struct relation { struct poly_t *poly; int x; unsigned int cofactor; struct flentry *factors; }

user_defined_types: struct htentry { struct relation *rel; struct htentry *next; }

user_defined_types: struct poly_t { struct poly_group_t *group; struct __mpz_struct a[1]; struct __mpz_struct b[1]; struct __mpz_struct c[1]; unsigned int *bmodp; unsigned int M; }

user_defined_types: struct flentry { unsigned int fac; struct flentry *next; }

user_defined_types: struct poly_group_t { struct __mpz_struct a[1]; struct __mpz_struct (*bvals)[1]; unsigned int gvals[12]; unsigned int *ainverses; struct relation *victim; unsigned int nrels; struct relation **relns; }


original_code:
void build_matrix (nsieve_t * ns){
 for (int i=0; i < ns->nfull; i++){
  ns->relns[i].row = (uint64_t *) calloc (ns->row_len, 8);
  fl_fillrow (ns->relns[i].r1, ns->relns[i].row, ns);
 }
 combine_partials (ns);
}

canonical_original_code:
void build_matrix(struct nsieve_t *ns){
 for (int i = 0; i < ns->nfull; i++){
  ns->relns[i].row = (unsigned long *) calloc (ns->row_len, 8);
  fl_fillrow (ns->relns[i].r1, ns->relns[i].row, ns);
 }
 combine_partials (ns);
}


filter

decompiled_code:
void filter()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void filter (nsieve_t *ns){
}

canonical_original_code:
void filter(struct nsieve_t *ns){
}


tdiv

decompiled_code:
__int64 __fastcall tdiv(__int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rbp

while ( 1 )
{
result = __gmpz_divisible_ui_p();
if ( !(_DWORD)result )
break;
__gmpz_divexact_ui();
puts("2");
}
v3 = 3LL;
if ( a2 > 3 )
{
do
{
while ( 1 )
{
result = __gmpz_divisible_ui_p();
if ( !(_DWORD)result )
break;
__gmpz_divexact_ui();
printf("%ld\n", v3);
}
v3 += 2LL;
}
while ( v3 < a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rbp

while ( 1 )
{
result = __gmpz_divisible_ui_p();
if ( !(_DWORD)result )
break;
__gmpz_divexact_ui();
puts("2");
}
v3 = 3LL;
if ( a2 > 3 )
{
do
{
while ( 1 )
{
result = __gmpz_divisible_ui_p();
if ( !(_DWORD)result )
break;
__gmpz_divexact_ui();
printf("%ld\n", v3);
}
v3 += 2LL;
}
while ( v3 < a2 );
}
return result;
}

original_code:
void tdiv (mpz_t n, int bound){
 while (__gmpz_divisible_ui_p (n, 2)){
  __gmpz_divexact_ui (n, n, 2);
  printf("2\n");
 }
 long t = 3;
 while (t < bound){
  while (__gmpz_divisible_ui_p (n, t)){
   __gmpz_divexact_ui (n, n, t);
   printf("%ld\n", t);
  }
  t += 2;
 }
}

canonical_original_code:
void tdiv(struct __mpz_struct n[1], int bound){
 while (__gmpz_divisible_ui_p (n, 2)){
  __gmpz_divexact_ui (n, n, 2);
  printf("2\n");
 }
 long t = 3;
 while (t < bound){
  while (__gmpz_divisible_ui_p (n, t)){
   __gmpz_divexact_ui (n, n, t);
   printf("%ld\n", t);
  }
  t += 2;
 }
}

variable_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; } [1]

variable_types: bound int

variable_types: t long


rho

decompiled_code:
__int64 __fastcall rho(__int64 a1, int a2)
{
int v2; // ebp
__int64 result; // rax
int v4; // ebx

v2 = 0;
__gmpz_inits();
__gmpz_set_ui();
result = __gmpz_set_ui();
if ( a2 )
{
while ( 1 )
{
v4 = 128;
__gmpz_set_ui();
do
{
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mod();
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mod();
__gmpz_sub();
__gmpz_mul();
__gmpz_mod();
--v4;
}
while ( v4 );
__gmpz_gcd();
if ( (unsigned int)__gmpz_cmp_ui() )
{
if ( (unsigned int)__gmpz_probab_prime_p() )
{
__gmpz_out_str();
putchar(10);
}
else
{
__gmpz_out_str();
puts(" (composite)");
}
__gmpz_divexact();
if ( !(unsigned int)__gmpz_cmp_ui() )
return __gmpz_clears();
if ( (unsigned int)__gmpz_probab_prime_p() )
break;
}
result = (unsigned int)(v2 + 1);
if ( v2 == (unsigned int)(a2 - 1) >> 7 )
return result;
++v2;
}
__gmpz_out_str();
putchar(10);
result = __gmpz_clears();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
int v2; // ebp
__int64 result; // rax
int v4; // ebx

v2 = 0;
__gmpz_inits();
__gmpz_set_ui();
result = __gmpz_set_ui();
if ( a2 )
{
while ( 1 )
{
v4 = 128;
__gmpz_set_ui();
do
{
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mod();
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mul();
__gmpz_add_ui();
__gmpz_mod();
__gmpz_sub();
__gmpz_mul();
__gmpz_mod();
--v4;
}
while ( v4 );
__gmpz_gcd();
if ( (unsigned int)__gmpz_cmp_ui() )
{
if ( (unsigned int)__gmpz_probab_prime_p() )
{
__gmpz_out_str();
putchar(10);
}
else
{
__gmpz_out_str();
puts(" (composite)");
}
__gmpz_divexact();
if ( !(unsigned int)__gmpz_cmp_ui() )
return __gmpz_clears();
if ( (unsigned int)__gmpz_probab_prime_p() )
break;
}
result = (unsigned int)(v2 + 1);
if ( v2 == (unsigned int)(a2 - 1) >> 7 )
return result;
++v2;
}
__gmpz_out_str();
putchar(10);
result = __gmpz_clears();
}
return result;
}

original_code:
void rho (mpz_t n, unsigned int steps, int printrem){
 mpz_t x, y, g, temp;
 __gmpz_inits (x, y, g, temp, ((void *)0));
 __gmpz_set_ui (x, 2);
 __gmpz_set_ui (y, 2);
 int ct = 0;
 while (ct < steps){
  __gmpz_set_ui (g, 1);
  for (int i=0; i < 128; i++){
   __gmpz_mul (temp, x, x);
   __gmpz_add_ui (temp, temp, 1);
   __gmpz_mod (x, temp, n);
   __gmpz_mul (temp, y, y);
   __gmpz_add_ui (temp, temp, 1);
   __gmpz_mul (y, temp, temp);
   __gmpz_add_ui (temp, y, 1);
   __gmpz_mod (y, temp, n);
   __gmpz_sub (temp, x, y);
   __gmpz_mul (g, g, temp);
   __gmpz_mod (g, g, n);
  }
  __gmpz_gcd (g, g, n);
  if ((__builtin_constant_p (1) && (1) == 0 ? ((g)->_mp_size < 0 ? -1 : (g)->_mp_size > 0) : __gmpz_cmp_ui (g,1)) != 0){
   if (__gmpz_probab_prime_p (g, 12)){
    __gmpz_out_str (stdout, 10, g);
    printf("\n");
    __gmpz_divexact (n, n, g);
   } else {
    __gmpz_out_str (stdout, 10, g);
    printf (" (composite)\n");
    __gmpz_divexact (n, n, g);
   }
   if ((__builtin_constant_p (1) && (1) == 0 ? ((n)->_mp_size < 0 ? -1 : (n)->_mp_size > 0) : __gmpz_cmp_ui (n,1)) == 0){
    __gmpz_clears (x, y, g, temp, ((void *)0));
    return;
   }
   if (__gmpz_probab_prime_p (n, 12)){
    __gmpz_out_str (stdout, 10, n);
    printf ("\n");
    __gmpz_clears (x, y, g, temp, ((void *)0));
    return;
   }
  }
  ct += 128;
 }
}

canonical_original_code:
void rho(struct __mpz_struct n[1], unsigned int steps, int printrem){
 struct __mpz_struct x[1]; struct __mpz_struct y[1]; struct __mpz_struct g[1]; struct __mpz_struct temp[1];
 __gmpz_inits (x, y, g, temp, ((void *)0));
 __gmpz_set_ui (x, 2);
 __gmpz_set_ui (y, 2);
 int ct = 0;
 while (ct < steps){
  __gmpz_set_ui (g, 1);
  for (int i = 0; i < 128; i++){
   __gmpz_mul (temp, x, x);
   __gmpz_add_ui (temp, temp, 1);
   __gmpz_mod (x, temp, n);
   __gmpz_mul (temp, y, y);
   __gmpz_add_ui (temp, temp, 1);
   __gmpz_mul (y, temp, temp);
   __gmpz_add_ui (temp, y, 1);
   __gmpz_mod (y, temp, n);
   __gmpz_sub (temp, x, y);
   __gmpz_mul (g, g, temp);
   __gmpz_mod (g, g, n);
  }
  __gmpz_gcd (g, g, n);
  if ((__builtin_constant_p (1) && (1) == 0 ? ((g)->_mp_size < 0 ? -1 : (g)->_mp_size > 0) : __gmpz_cmp_ui (g,1)) != 0){
   if (__gmpz_probab_prime_p (g, 12)){
    __gmpz_out_str (stdout, 10, g);
    printf("\n");
    __gmpz_divexact (n, n, g);
   } else {
    __gmpz_out_str (stdout, 10, g);
    printf (" (composite)\n");
    __gmpz_divexact (n, n, g);
   }
   if ((__builtin_constant_p (1) && (1) == 0 ? ((n)->_mp_size < 0 ? -1 : (n)->_mp_size > 0) : __gmpz_cmp_ui (n,1)) == 0){
    __gmpz_clears (x, y, g, temp, ((void *)0));
    return;
   }
   if (__gmpz_probab_prime_p (n, 12)){
    __gmpz_out_str (stdout, 10, n);
    printf ("\n");
    __gmpz_clears (x, y, g, temp, ((void *)0));
    return;
   }
  }
  ct += 128;
 }
}

variable_types:
steps
unsigned int

variable_types: printrem int

variable_types: ct int


crear

original_code:
void crear(servidor* lista){
    strcpy(lista->nombre,"NIL");
    strcpy(lista->dns,"NIL");
    lista->puerto = 0;
    lista->tiempoRespuesta = 0;
    lista->siguiente = ((void *)0);
}

canonical_original_code:
void crear(struct servidor *lista){
    strcpy(lista->nombre,"NIL");
    strcpy(lista->dns,"NIL");
    lista->puerto = 0;
    lista->tiempoRespuesta = 0;
    lista->siguiente = ((void *)0);
}

variable_types: struct servidor { char nombre[128]; char dns[128]; int puerto; int tiempoRespuesta; struct servidor *siguiente; } *


inicializar

decompiled_code:
char *__fastcall inicializar(char *src, char *a2, int a3, __int64 a4)
{
char *result; // rax

strcpy((char *)a4, src);
result = strcpy((char *)(a4 + 128), a2);
*(_DWORD *)(a4 + 256) = a3;
*(_DWORD *)(a4 + 260) = 0;
*(_QWORD *)(a4 + 264) = 0LL;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *src, char *a2, int a3, __int64 a4)
{
char *result; // rax

strcpy((char *)a4, src);
result = strcpy((char *)(a4 + 128), a2);
*(_DWORD *)(a4 + 256) = a3;
*(_DWORD *)(a4 + 260) = 0;
*(_QWORD *)(a4 + 264) = 0LL;
return result;
}

original_code:
void inicializar(char* nombre, char* dns, int puerto, servidor* lista){
    strcpy(lista->nombre,nombre);
    strcpy(lista->dns,dns);
    lista->puerto = puerto;
    lista->tiempoRespuesta = 0;
    lista->siguiente = ((void *)0);
}

canonical_original_code:
void inicializar(char *nombre, char *dns, int puerto, struct servidor *lista){
    strcpy(lista->nombre,nombre);
    strcpy(lista->dns,dns);
    lista->puerto = puerto;
    lista->tiempoRespuesta = 0;
    lista->siguiente = ((void *)0);
}

variable_types:
nombre
char *

variable_types:
dns
char *

variable_types: puerto int

user_defined_types: struct servidor { char nombre[128]; char dns[128]; int puerto; int tiempoRespuesta; struct servidor *siguiente; }


insertar

decompiled_code:
char *__fastcall insertar(const char *a1, __int64 a2)
{
const char *v2; // r8
const char *v3; // rbx
void *v4; // r12
char *result; // rax

v2 = a1;
if ( !strcmp(a1, "NIL") )
return inicializar((char *)a2, (char *)(a2 + 128), *(_DWORD *)(a2 + 256), (__int64)a1);
do
{
v3 = v2;
v2 = (const char *)*((_QWORD *)v2 + 33);
}
while ( v2 );
v4 = malloc(0x110uLL);
result = inicializar((char *)a2, (char *)(a2 + 128), *(_DWORD *)(a2 + 256), (__int64)v4);
*((_QWORD *)v3 + 33) = v4;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(const char *a1, __int64 a2)
{
const char *v2; // r8
const char *v3; // rbx
void *v4; // r12
char *result; // rax

v2 = a1;
if ( !strcmp(a1, "NIL") )
return func1((char *)a2, (char *)(a2 + 128), *(_DWORD *)(a2 + 256), (__int64)a1);
do
{
v3 = v2;
v2 = (const char *)*((_QWORD *)v2 + 33);
}
while ( v2 );
v4 = malloc(0x110uLL);
result = func1((char *)a2, (char *)(a2 + 128), *(_DWORD *)(a2 + 256), (__int64)v4);
*((_QWORD *)v3 + 33) = v4;
return result;
}

original_code:
void insertar(servidor* lista, servidor* servNuevo){
    servidor *temp = lista;
    if(strcmp(temp->nombre,"NIL")!=0){
        while(temp->siguiente != ((void *)0)){
            temp = temp->siguiente;
        }
        servidor *ele = (servidor *) malloc(sizeof(servidor));
        inicializar(servNuevo->nombre,servNuevo->dns,servNuevo->puerto,ele);
        temp->siguiente = ele;
    }else{
        inicializar(servNuevo->nombre,servNuevo->dns,servNuevo->puerto,lista);
    }
}

canonical_original_code:
void insertar(struct servidor *lista, struct servidor *servNuevo){
    struct servidor *temp = lista;
    if(strcmp(temp->nombre,"NIL")!=0){
        while(temp->siguiente != ((void *)0)){
            temp = temp->siguiente;
        }
        struct servidor *ele = (servidor *) malloc(sizeof(servidor));
        inicializar(servNuevo->nombre,servNuevo->dns,servNuevo->puerto,ele);
        temp->siguiente = ele;
    }else{
        inicializar(servNuevo->nombre,servNuevo->dns,servNuevo->puerto,lista);
    }
}

variable_types:
servNuevo
struct servidor *


imprimir

decompiled_code:
int __fastcall imprimir(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

v1 = a1;
for ( result = puts("Lista de Centros de Servicio:"); v1; v1 = *(_QWORD *)(v1 + 264) )
{
printf("Nombre centro: %s\n", (const char *)v1);
printf("DNS: %s\n", (const char *)(v1 + 128));
printf("Puerto: %d\n", *(unsigned int *)(v1 + 256));
printf("Tiempo de Respuesta: %d\n", *(unsigned int *)(v1 + 260));
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

v1 = a1;
for ( result = puts("Lista de Centros de Servicio:"); v1; v1 = *(_QWORD *)(v1 + 264) )
{
printf("Nombre centro: %s\n", (const char *)v1);
printf("DNS: %s\n", (const char *)(v1 + 128));
printf("Puerto: %d\n", *(unsigned int *)(v1 + 256));
printf("Tiempo de Respuesta: %d\n", *(unsigned int *)(v1 + 260));
result = putchar(10);
}
return result;
}

original_code:
void imprimir(servidor* lista){
    servidor *aux = lista;
    printf("Lista de Centros de Servicio:\n");
    while(aux != ((void *)0)){
        printf("Nombre centro: %s\n",aux->nombre);
        printf("DNS: %s\n",aux->dns);
        printf("Puerto: %d\n",aux->puerto);
        printf("Tiempo de Respuesta: %d\n",aux->tiempoRespuesta);
        printf("\n");
        aux = aux->siguiente;
    }
}

canonical_original_code:
void imprimir(struct servidor *lista){
    struct servidor *aux = lista;
    printf("Lista de Centros de Servicio:\n");
    while(aux != ((void *)0)){
        printf("Nombre centro: %s\n",aux->nombre);
        printf("DNS: %s\n",aux->dns);
        printf("Puerto: %d\n",aux->puerto);
        printf("Tiempo de Respuesta: %d\n",aux->tiempoRespuesta);
        printf("\n");
        aux = aux->siguiente;
    }
}


ordenarPorTiempo

decompiled_code:
// write access to const memory has been detected, the output may be wrong!
__int64 __fastcall ordenarPorTiempo(char *src)
{
__int64 v1; // rbx
char *v2; // rbp
__int64 result; // rax

v1 = *((_QWORD *)src + 33);
if ( v1 )
{
v2 = src;
while ( 1 )
{
while ( 1 )
{
result = *(unsigned int *)(v1 + 260);
if ( *((_DWORD *)v2 + 65) <= (int)result )
break;
inicializar(v2, v2 + 128, *((_DWORD *)v2 + 64), 0LL);
*(_DWORD *)((char *)&loc_102 + 2) = *((_DWORD *)v2 + 65);
strcpy(v2, (const char *)v1);
strcpy(v2 + 128, (const char *)(v1 + 128));
*((_DWORD *)v2 + 64) = *(_DWORD *)(v1 + 256);
*((_DWORD *)v2 + 65) = *(_DWORD *)(v1 + 260);
strcpy((char *)v1, 0LL);
strcpy((char *)(v1 + 128), (const char *)insertar);
*(_DWORD *)(v1 + 256) = *(_DWORD *)((char *)&loc_FE + 2);
result = *(unsigned int *)((char *)&loc_102 + 2);
*(_DWORD *)(v1 + 260) = *(_DWORD *)((char *)&loc_102 + 2);
v1 = *(_QWORD *)(v1 + 264);
if ( !v1 )
goto LABEL_7;
}
v1 = *(_QWORD *)(v1 + 264);
if ( !v1 )
{
LABEL_7:
v2 = (char *)*((_QWORD *)v2 + 33);
v1 = *((_QWORD *)v2 + 33);
if ( !v1 )
return result;
}
}
}
return result;
}

original_code:
void ordenarPorTiempo(servidor* lista){
    servidor *aux = lista;
    servidor *cont;
    for(; aux->siguiente != ((void *)0); aux = aux->siguiente){
        for(cont = aux->siguiente; cont != ((void *)0); cont = cont->siguiente){
            if(aux->tiempoRespuesta > cont->tiempoRespuesta){
                servidor *temp;
                inicializar(aux->nombre,aux->dns,aux->puerto,temp);
                temp->tiempoRespuesta = aux->tiempoRespuesta;
                strcpy(aux->nombre,cont->nombre);
                strcpy(aux->dns,cont->dns);
                aux->puerto = cont->puerto;
                aux->tiempoRespuesta = cont->tiempoRespuesta;
                strcpy(cont->nombre,temp->nombre);
                strcpy(cont->dns,temp->dns);
                cont->puerto = temp->puerto;
                cont->tiempoRespuesta = temp->tiempoRespuesta;
            }
        }
    }
}

canonical_original_code:
void ordenarPorTiempo(struct servidor *lista){
    struct servidor *aux = lista;
    struct servidor *cont;
    for(; aux->siguiente != ((void *)0); aux = aux->siguiente){
        for(cont = aux->siguiente; cont != ((void *)0); cont = cont->siguiente){
            if(aux->tiempoRespuesta > cont->tiempoRespuesta){
                struct servidor *temp;
                inicializar(aux->nombre,aux->dns,aux->puerto,temp);
                temp->tiempoRespuesta = aux->tiempoRespuesta;
                strcpy(aux->nombre,cont->nombre);
                strcpy(aux->dns,cont->dns);
                aux->puerto = cont->puerto;
                aux->tiempoRespuesta = cont->tiempoRespuesta;
                strcpy(cont->nombre,temp->nombre);
                strcpy(cont->dns,temp->dns);
                cont->puerto = temp->puerto;
                cont->tiempoRespuesta = temp->tiempoRespuesta;
            }
        }
    }
}


abrirConexion

decompiled_code:
__int64 __fastcall abrirConexion(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
int v4; // edi
int v5; // edi
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
v6.sa_family = 2;
*(_WORD *)v6.sa_data = a1;
*(_DWORD *)&v6.sa_data[2] = 0;
if ( bind(v1, &v6, 0x10u) < 0 )
{
v4 = v2;
v2 = -2;
close(v4);
}
else if ( listen(v2, 5) < 0 )
{
v5 = v2;
v2 = -3;
close(v5);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
int v4; // edi
int v5; // edi
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
v6.sa_family = 2;
*(_WORD *)v6.sa_data = a1;
*(_DWORD *)&v6.sa_data[2] = 0;
if ( bind(v1, &v6, 0x10u) < 0 )
{
v4 = v2;
v2 = -2;
close(v4);
}
else if ( listen(v2, 5) < 0 )
{
v5 = v2;
v2 = -3;
close(v5);
}
return v2;
}

original_code:
int abrirConexion(int puerto)
{
    struct sockaddr_in direccion;
    int descriptorS;
    descriptorS = socket (2, SOCK_STREAM, 0);
    if (descriptorS < 0){
        return -1;
    }
    direccion.sin_family = 2;
    direccion.sin_port = puerto;
    direccion.sin_addr.s_addr =((in_addr_t) 0x00000000);
    if( bind(descriptorS, (struct sockaddr *)&direccion, sizeof(direccion)) < 0){
        close(descriptorS);
        return -2;
    }
    if (listen(descriptorS, 5) < 0){
        close (descriptorS);
        return -3;
    }
    return descriptorS;
}

canonical_original_code:
int abrirConexion(int puerto)
{
    struct sockaddr_in direccion;
    int descriptorS;
    descriptorS = socket (2, SOCK_STREAM, 0);
    if (descriptorS < 0){
        return -1;
    }
    direccion.sin_family = 2;
    direccion.sin_port = puerto;
    direccion.sin_addr.s_addr =((unsigned int) 0x00000000);
    if( bind(descriptorS, (struct sockaddr *)&direccion, sizeof(direccion)) < 0){
        close(descriptorS);
        return -2;
    }
    if (listen(descriptorS, 5) < 0){
        close (descriptorS);
        return -3;
    }
    return descriptorS;
}

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

return_type: int

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

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


aceptarConexion

decompiled_code:
int __fastcall aceptarConexion(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
result = -1;
return result;
}

original_code:
int aceptarConexion(int descriptor)
{
    socklen_t logintudCliente;
    struct sockaddr cliente;
    int dSocket;
    logintudCliente = sizeof(cliente);
    dSocket = accept(descriptor, &cliente, &logintudCliente);
    if(dSocket < 0) {
        return -1;
    }
    return dSocket;
}

canonical_original_code:
int aceptarConexion(int descriptor)
{
    unsigned int logintudCliente;
    struct sockaddr cliente;
    int dSocket;
    logintudCliente = sizeof(cliente);
    dSocket = accept(descriptor, &cliente, &logintudCliente);
    if(dSocket < 0) {
        return -1;
    }
    return dSocket;
}

variable_types: descriptor int

variable_types:
logintudCliente
unsigned int

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

variable_types: dSocket int


escribirSocket

decompiled_code:
__int64 __fastcall escribirSocket(int fd, void *buf)
{
size_t v2; // rax
int v3; // er8
__int64 result; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
result = 1LL;
if ( v3 < 0 )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, void *buf)
{
size_t v2; // rax
int v3; // er8
__int64 result; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
result = 1LL;
if ( v3 < 0 )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int escribirSocket(int descriptor, char *datos)
{
    int n;
    n =write(descriptor,datos,strlen(datos));
    if (n < 0) {
        return (*__errno_location ());
    } else {
        return 1;
    }
}

variable_types:
datos
char *

variable_types: n int


fatalerror

decompiled_code:
void __fastcall __noreturn fatalerror(const char *a1, int a2)
{
fprintf(stderr, "%s: %s\n", (const char *)programname, a1);
fflush(stdout);
exit(a2);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, int a2)
{
fprintf(stderr, "%s: %s\n", (const char *)programname, a1);
fflush(stdout);
exit(a2);
}

original_code:
void fatalerror(char *message, int errortype) {
  fprintf(stderr, "%s: %s\n", programname, message);
  fflush(stdout);
  exit(errortype);
}

variable_types:
message
char *

variable_types: errortype int


trim

decompiled_code:
char *__fastcall trim(char *s)
{
__int64 v2; // rbx
int v3; // eax
__int64 v4; // rdx

while ( isspace(*s) )
++s;
v2 = (int)(strlen(s) - 1);
do
{
v3 = isspace(s[v2]);
v4 = v2--;
}
while ( v3 );
s[v4 + 1] = 0;
return s;
}

canonical_decompiled_code:
char *__fastcall func2(char *s)
{
__int64 v2; // rbx
int v3; // eax
__int64 v4; // rdx

while ( isspace(*s) )
++s;
v2 = (int)(strlen(s) - 1);
do
{
v3 = isspace(s[v2]);
v4 = v2--;
}
while ( v3 );
s[v4 + 1] = 0;
return s;
}

original_code:
char* trim(char *cadena){
    int i;
    while(isspace(*cadena)){
        cadena++;
    }
    for (i = strlen(cadena)- 1; (isspace(cadena[i])); i--) ;
    cadena[i + 1] = '\0';
    return cadena;
}

canonical_original_code:
char *trim(char *cadena){
    int i;
    while(isspace(*cadena)){
        cadena++;
    }
    for (i = strlen(cadena)- 1; (isspace(cadena[i])); i--) ;
    cadena[i + 1] = '\0';
    return cadena;
}

variable_types:
cadena
char *

variable_types: i int

return_type: char *


crearConexion

decompiled_code:
__int64 __fastcall crearConexion(char *name, __int16 a2)
{
struct hostent *v2; // rax
struct hostent *v3; // r12
int v4; // eax
int v5; // er12
struct sockaddr v7; // [rsp+0h] [rbp-28h] BYREF

puts("intentando conectarme");
v2 = gethostbyname(name);
if ( v2 )
{
v3 = v2;
printf("intentando conectarme 1: %d\n", v2);
v7.sa_family = 2;
v4 = **(_DWORD **)v3->h_addr_list;
*(_WORD *)v7.sa_data = a2;
*(_DWORD *)&v7.sa_data[2] = v4;
v5 = socket(2, 1, 0);
if ( v5 < 0 )
{
v5 = -2;
}
else
{
puts("intentando conectarme 2");
if ( connect(v5, &v7, 0x10u) < 0 )
v5 = -3;
else
printf("conexion %d\n", (unsigned int)v5);
}
}
else
{
v5 = -1;
}
return (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *name, __int16 a2)
{
struct hostent *v2; // rax
struct hostent *v3; // r12
int v4; // eax
int v5; // er12
struct sockaddr v7; // [rsp+0h] [rbp-28h] BYREF

puts("intentando conectarme");
v2 = gethostbyname(name);
if ( v2 )
{
v3 = v2;
printf("intentando conectarme 1: %d\n", v2);
v7.sa_family = 2;
v4 = **(_DWORD **)v3->h_addr_list;
*(_WORD *)v7.sa_data = a2;
*(_DWORD *)&v7.sa_data[2] = v4;
v5 = socket(2, 1, 0);
if ( v5 < 0 )
{
v5 = -2;
}
else
{
puts("intentando conectarme 2");
if ( connect(v5, &v7, 0x10u) < 0 )
v5 = -3;
else
printf("conexion %d\n", (unsigned int)v5);
}
}
else
{
v5 = -1;
}
return (unsigned int)v5;
}

original_code:
int crearConexion(char *nombre_host, int puerto){
    struct sockaddr_in direccionHost;
    struct hostent *host;
    int descSocket;
    host = ((void *)0);
    printf("intentando conectarme\n");
    host = gethostbyname (nombre_host);
    if (host == ((void *)0)) {
        return -1;
    }
    printf("intentando conectarme 1: %d\n",host);
    direccionHost.sin_family = 2;
    direccionHost.sin_addr.s_addr = ((struct in_addr *)(host->h_addr_list[0]))->s_addr;
    direccionHost.sin_port = puerto;
    descSocket = socket (2, SOCK_STREAM, 0);
    if (descSocket < 0){
        return -2;
    }
    printf("intentando conectarme 2\n");
    if (connect (descSocket,(struct sockaddr *)&direccionHost, sizeof (direccionHost)) < 0){
        return -3;
    }
    printf("conexion %d\n",descSocket);
    return descSocket;
}

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

variable_types: descSocket int


escribeSocket

decompiled_code:
_BOOL8 __fastcall escribeSocket(int fd, void *buf)
{
size_t v2; // rax

v2 = strlen((const char *)buf);
return (int)write(fd, buf, v2) >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int fd, void *buf)
{
size_t v2; // rax

v2 = strlen((const char *)buf);
return (int)write(fd, buf, v2) >= 0;
}

original_code:
int escribeSocket(int descriptor, char *mensaje){
    int n;
    n = write(descriptor,mensaje,strlen(mensaje));
    if (n < 0) {
        return 0;
    } else {
        return 1;
    }
}

variable_types:
mensaje
char *


escribirSocket

canonical_original_code:
int escribirSocket(int descriptor, char *datos)
{
    int n;
    n =write(descriptor,datos,strlen(datos));
    if (n < 0) {
        return (*__errno_location ());
    } else {
        return 1;
    }
}


makeTransientImg

repo: alebugariu/egioc

decompiled_code:
__int64 __fastcall makeTransientImg(unsigned int *a1)
{
unsigned int v1; // er12
__int64 result; // rax
unsigned int v3; // ebx

if ( a1[1] )
{
v1 = 0;
do
{
result = *a1;
v3 = 0;
if ( (_DWORD)result )
{
do
{
result = putXPMpixel((__int64)a1, v3, v1, v3);
++v3;
}
while ( *a1 > v3 );
}
++v1;
}
while ( a1[1] > v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int *a1)
{
unsigned int v1; // er12
__int64 result; // rax
unsigned int v3; // ebx

if ( a1[1] )
{
v1 = 0;
do
{
result = *a1;
v3 = 0;
if ( (_DWORD)result )
{
do
{
result = func8((__int64)a1, v3, v1, v3);
++v3;
}
while ( *a1 > v3 );
}
++v1;
}
while ( a1[1] > v1 );
}
return result;
}

original_code:
void
makeTransientImg(XPM *img){
 int ypix = 0;
 int xpix = 0;
 int colIndex = 0;
 for(ypix = 0; ypix < img->height; ++ypix){
  colIndex = 0;
  for(xpix = 0; xpix < img->width; ++xpix){
   putXPMpixel(img, xpix, ypix, colIndex);
   colIndex ++;
  }
 }
}

canonical_original_code:
void makeTransientImg(struct XPM *img){
 int ypix = 0;
 int xpix = 0;
 int colIndex = 0;
 for(ypix = 0; ypix < img->height; ++ypix){
  colIndex = 0;
  for(xpix = 0; xpix < img->width; ++xpix){
   putXPMpixel(img, xpix, ypix, colIndex);
   colIndex ++;
  }
 }
}

variable_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types: ypix int

variable_types: xpix int

variable_types: colIndex int

user_defined_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor *colta; unsigned short **data; }

user_defined_types: struct XPMColor { struct Color clr; char *chars; char *key; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v4 = newXPM(50LL, 50LL, 2LL, 50LL);
assignColorTable(v4);
makeTransientImg(v4);
saveXPMtofile(v4, "tema1.xpm");
freeXPM(&v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v4 = newXPM(50LL, 50LL, 2LL, 50LL);
func11(v4);
func12(v4);
func10(v4, "tema1.xpm");
func7(&v4);
return 0;
}

original_code:
int
main(int argc, char *argv[]){
 XPM *img = newXPM(50, 50, 2, 50);
 assignColorTable(img);
 makeTransientImg(img);
 saveXPMtofile(img, "tema1.xpm");
 freeXPM(&img);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
 struct XPM *img = newXPM(50, 50, 2, 50);
 assignColorTable(img);
 makeTransientImg(img);
 saveXPMtofile(img, "tema1.xpm");
 freeXPM(&img);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

user_defined_types: struct Color { unsigned short red; unsigned short green; unsigned short blue; }


assignColorTable

original_code:
void
assignColorTable(XPM *img){
 XPMColor clrData;
 unsigned short clrIndex = 0;
 clrData.clr.blue = 0;
 clrData.clr.green = 0;
 clrData.clr.red = 0;
 for(clrIndex = 0; clrIndex < img->ncolors; ++clrIndex){
  clrData.key = (char *)malloc(sizeof(char) + 1);
  strcpy(clrData.key, "c");
  clrData.chars = (char *)malloc(img->chrperpixel * sizeof(char));
  sprintf(clrData.chars, "%02X", clrIndex);
  setXPMColor(img, clrIndex, clrData);
  clrData.clr.red += 255/img->ncolors;
 }
}

canonical_original_code:
void assignColorTable(struct XPM *img){
 struct XPMColor clrData;
 unsigned short clrIndex = 0;
 clrData.clr.blue = 0;
 clrData.clr.green = 0;
 clrData.clr.red = 0;
 for(clrIndex = 0; clrIndex < img->ncolors; ++clrIndex){
  clrData.key = (char *)malloc(sizeof(char) + 1);
  strcpy(clrData.key, "c");
  clrData.chars = (char *)malloc(img->chrperpixel * sizeof(char));
  sprintf(clrData.chars, "%02X", clrIndex);
  setXPMColor(img, clrIndex, clrData);
  clrData.clr.red += 255/img->ncolors;
 }
}

variable_types: struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; }

variable_types:
clrIndex
unsigned short


degToRad

decompiled_code:
double __fastcall degToRad(int a1)
{
return (double)a1 * 3.14159265 / 180.0;
}

canonical_decompiled_code:
double __fastcall func1(int a1)
{
return (double)a1 * 3.14159265 / 180.0;
}

original_code:
double degToRad(int angle)
{
 return angle * 3.14159265/180;
}

variable_types: angle int

return_type: double


initTranslation

original_code:
void initTranslation(struct transMatrix * m, int tx, int ty)
{
  m->matrix[0][0] = 1; m->matrix[0][1] = 0; m->matrix[0][2] = tx;
  m->matrix[1][0] = 0; m->matrix[1][1] = 1; m->matrix[1][2] = ty;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

canonical_original_code:
void initTranslation(struct transMatrix *m, int tx, int ty)
{
  m->matrix[0][0] = 1; m->matrix[0][1] = 0; m->matrix[0][2] = tx;
  m->matrix[1][0] = 0; m->matrix[1][1] = 1; m->matrix[1][2] = ty;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

variable_types: struct transMatrix { double matrix[3][3]; } *

variable_types: tx int

variable_types: ty int


initRotation

decompiled_code:
__int64 __fastcall initRotation(__int64 *a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
__int64 v4; // xmm0_8
__int64 result; // rax
__int64 v6; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

v2 = degToRad(a2);
sincos(v2, sinx, (double *)&v6);
v3 = sinx[0];
v4 = v6;
a1[2] = 0LL;
result = LC7;
a1[5] = 0LL;
a1[6] = 0LL;
a1[7] = 0LL;
a1[8] = LC7;
*a1 = v4;
a1[1] = *(_QWORD *)&v3 ^ LC9;
*((double *)a1 + 3) = v3;
a1[4] = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
__int64 v4; // xmm0_8
__int64 result; // rax
__int64 v6; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

v2 = func1(a2);
sincos(v2, sinx, (double *)&v6);
v3 = sinx[0];
v4 = v6;
a1[2] = 0LL;
result = LC7;
a1[5] = 0LL;
a1[6] = 0LL;
a1[7] = 0LL;
a1[8] = LC7;
*a1 = v4;
a1[1] = *(_QWORD *)&v3 ^ LC9;
*((double *)a1 + 3) = v3;
a1[4] = v4;
return result;
}

original_code:
void initRotation(struct transMatrix * m, int angle)
{
  double radAngle = degToRad(angle);
  m->matrix[0][0] = cos(radAngle); m->matrix[0][1] = -1*sin(radAngle); m->matrix[0][2] = 0;
  m->matrix[1][0] = sin(radAngle); m->matrix[1][1] = cos(radAngle); m->matrix[1][2] = 0;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

canonical_original_code:
void initRotation(struct transMatrix *m, int angle)
{
  double radAngle = degToRad(angle);
  m->matrix[0][0] = cos(radAngle); m->matrix[0][1] = -1*sin(radAngle); m->matrix[0][2] = 0;
  m->matrix[1][0] = sin(radAngle); m->matrix[1][1] = cos(radAngle); m->matrix[1][2] = 0;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

variable_types: radAngle double

user_defined_types: struct transMatrix { double matrix[3][3]; }


initScale

original_code:
void initScale(struct transMatrix * m, double sx, double sy)
{
  m->matrix[0][0] = sx; m->matrix[0][1] = 0; m->matrix[0][2] = 0;
  m->matrix[1][0] = 0; m->matrix[1][1] = sy; m->matrix[1][2] = 0;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

canonical_original_code:
void initScale(struct transMatrix *m, double sx, double sy)
{
  m->matrix[0][0] = sx; m->matrix[0][1] = 0; m->matrix[0][2] = 0;
  m->matrix[1][0] = 0; m->matrix[1][1] = sy; m->matrix[1][2] = 0;
  m->matrix[2][0] = 0; m->matrix[2][1] = 0; m->matrix[2][2] = 1;
}

variable_types: sx double

variable_types: sy double


matrixProduct

decompiled_code:
char *__fastcall matrixProduct(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, char a16, int a17, int a18, char a19)
{
char *v19; // rcx
double v20; // xmm4_8
double v21; // xmm3_8
char *result; // rax
__int64 v23; // rdx
double v24; // xmm2_8
double v25; // xmm1_8
double v26; // xmm0_8

v19 = &a7;
do
{
v20 = *(double *)v19;
v21 = *((double *)v19 + 1);
result = &a16;
v23 = a1;
v24 = *((double *)v19 + 2);
do
{
v25 = *(double *)result;
v26 = *((double *)result + 3);
result += 8;
v23 += 8LL;
*(double *)(v23 - 8) = *((double *)result + 5) * v24 + v25 * v20 + 0.0 + v26 * v21;
}
while ( result != &a19 );
v19 += 24;
a1 += 24LL;
}
while ( v19 != &a16 );
return result;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, char a16, int a17, int a18, char a19)
{
char *v19; // rcx
double v20; // xmm4_8
double v21; // xmm3_8
char *result; // rax
__int64 v23; // rdx
double v24; // xmm2_8
double v25; // xmm1_8
double v26; // xmm0_8

v19 = &a7;
do
{
v20 = *(double *)v19;
v21 = *((double *)v19 + 1);
result = &a16;
v23 = a1;
v24 = *((double *)v19 + 2);
do
{
v25 = *(double *)result;
v26 = *((double *)result + 3);
result += 8;
v23 += 8LL;
*(double *)(v23 - 8) = *((double *)result + 5) * v24 + v25 * v20 + 0.0 + v26 * v21;
}
while ( result != &a19 );
v19 += 24;
a1 += 24LL;
}
while ( v19 != &a16 );
return result;
}

original_code:
void matrixProduct(struct transMatrix *prod, struct transMatrix a, struct transMatrix b)
{
 int lin, col, index;
        double sum;
        for(lin=0; lin<3; lin++)
            for(col=0; col<3; col++)
            {
                sum = 0;
                for(index=0; index<3; index++)
                    sum+= a.matrix[lin][index]*b.matrix[index][col];
                prod->matrix[lin][col] = sum;
            }
}

canonical_original_code:
void matrixProduct(struct transMatrix *prod, struct transMatrix a, struct transMatrix b)
{
 int lin; int col; int index;
        double sum;
        for(lin=0; lin<3; lin++)
            for(col=0; col<3; col++)
            {
                sum = 0;
                for(index=0; index<3; index++)
                    sum+= a.matrix[lin][index]*b.matrix[index][col];
                prod->matrix[lin][col] = sum;
            }
}

variable_types:
a
struct transMatrix

variable_types:
b
struct transMatrix

variable_types: lin int

variable_types: col int

variable_types: index int

variable_types: sum double


matrixVectorProduct

decompiled_code:
char *__fastcall matrixVectorProduct(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, char a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, int a23, double a24, double a25, double a26)
{
double v18; // xmm5_8
double v19; // xmm4_8
char *result; // rax
double v21; // xmm3_8
double v22; // xmm1_8
double v23; // xmm0_8

v18 = a24;
v19 = a25;
result = &a15;
v21 = a26;
do
{
v22 = *(double *)result;
v23 = *((double *)result + 1);
result += 24;
a1 += 8LL;
*(double *)(a1 - 8) = *((double *)result - 1) * v21 + v22 * v18 + 0.0 + v23 * v19;
}
while ( result != (char *)&a24 );
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, char a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, int a23, double a24, double a25, double a26)
{
double v18; // xmm5_8
double v19; // xmm4_8
char *result; // rax
double v21; // xmm3_8
double v22; // xmm1_8
double v23; // xmm0_8

v18 = a24;
v19 = a25;
result = &a15;
v21 = a26;
do
{
v22 = *(double *)result;
v23 = *((double *)result + 1);
result += 24;
a1 += 8LL;
*(double *)(a1 - 8) = *((double *)result - 1) * v21 + v22 * v18 + 0.0 + v23 * v19;
}
while ( result != (char *)&a24 );
return result;
}

original_code:
void matrixVectorProduct(struct homoCoord *prod, struct transMatrix m, struct homoCoord vect)
{
 int lin, col;
        double sum = 0;
        for(lin=0; lin<3; lin++)
           {
  sum = 0;
                for(col=0; col<3; col++)
                    sum+= m.matrix[lin][col]*vect.coord[col];
                prod->coord[lin] = sum;
           }
}

canonical_original_code:
void matrixVectorProduct(struct homoCoord *prod, struct transMatrix m, struct homoCoord vect)
{
 int lin; int col;
        double sum = 0;
        for(lin=0; lin<3; lin++)
           {
  sum = 0;
                for(col=0; col<3; col++)
                    sum+= m.matrix[lin][col]*vect.coord[col];
                prod->coord[lin] = sum;
           }
}

variable_types:
prod
struct homoCoord { double coord[3]; } *

variable_types: struct transMatrix { double matrix[3][3]; }

variable_types:
vect
struct homoCoord


initHomoVector

decompiled_code:
__int64 __fastcall initHomoVector(__int64 a1, double a2, double a3)
{
__int64 result; // rax

result = LC7;
*(double *)a1 = a2;
*(double *)(a1 + 8) = a3;
*(_QWORD *)(a1 + 16) = LC7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, double a2, double a3)
{
__int64 result; // rax

result = LC7;
*(double *)a1 = a2;
*(double *)(a1 + 8) = a3;
*(_QWORD *)(a1 + 16) = LC7;
return result;
}

original_code:
void initHomoVector(struct homoCoord *point, double x, double y)
{
 point->coord[0] = x;
        point->coord[1] = y;
        point->coord[2] = 1;
}

variable_types:
point
struct homoCoord { double coord[3]; } *

variable_types: x double

variable_types: y double


applyTransforms

original_code:
void
applyTransforms(struct trans *tlist, struct GENode *glist){
  struct trans *ptl = tlist;
  struct GENode *pgl = ((void *)0);
  struct transMatrix m, aux;
  struct homoCoord start;
  struct homoCoord end;
  while(((void *)0) != ptl){
    pgl = glist;
    while(((void *)0) != pgl){
      switch(pgl->el.type)
      {case LINE: initHomoVector(&start, pgl->el.data.line.st.x, pgl->el.data.line.st.y);
                  initHomoVector(&end, pgl->el.data.line.en.x, pgl->el.data.line.en.y);
                  break;
       default: break;
      }
      switch(ptl->tType){
      case TRANSLATION: initTranslation(&m, ptl->data.t.tx, ptl->data.t.ty);
                 break;
      case SCALING: initScale(&m, ptl->data.s.sx, ptl->data.s.sy);
                    if (!(ptl->data.s.px == 0 && ptl->data.s.py == 0))
                        {
                          initTranslation(&aux, -1*ptl->data.s.px, -1*ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                          initTranslation(&aux, ptl->data.s.px, ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                        }
              break;
      case ROTATION: initRotation(&m, ptl->data.r.u);
                     if (!(ptl->data.s.px == 0 && ptl->data.s.py == 0))
                        {
                          initTranslation(&aux, -1*ptl->data.r.px, -1*ptl->data.r.py);
                          matrixProduct(&m, m, aux);
                          initTranslation(&aux, ptl->data.s.px, ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                        }
              break;
      default: break;
      }
      matrixVectorProduct(&start, m, start);
      matrixVectorProduct(&end, m, end);
      twoDCoord(&pgl->el.data.line.st, start);
      twoDCoord(&pgl->el.data.line.en, end);
      pgl = pgl->next;
    }
    ptl = ptl->next;
  }
}

canonical_original_code:
void applyTransforms(struct trans *tlist, struct GENode *glist){
  struct trans *ptl = tlist;
  struct GENode *pgl = ((void *)0);
  struct transMatrix m; struct transMatrix aux;
  struct homoCoord start;
  struct homoCoord end;
  while(((void *)0) != ptl){
    pgl = glist;
    while(((void *)0) != pgl){
      switch(pgl->el.type)
      {case LINE: initHomoVector(&start, pgl->el.data.line.st.x, pgl->el.data.line.st.y);
                  initHomoVector(&end, pgl->el.data.line.en.x, pgl->el.data.line.en.y);
                  break;
       default: break;
      }
      switch(ptl->tType){
      case TRANSLATION: initTranslation(&m, ptl->data.t.tx, ptl->data.t.ty);
                 break;
      case SCALING: initScale(&m, ptl->data.s.sx, ptl->data.s.sy);
                    if (!(ptl->data.s.px == 0 && ptl->data.s.py == 0))
                        {
                          initTranslation(&aux, -1*ptl->data.s.px, -1*ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                          initTranslation(&aux, ptl->data.s.px, ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                        }
              break;
      case ROTATION: initRotation(&m, ptl->data.r.u);
                     if (!(ptl->data.s.px == 0 && ptl->data.s.py == 0))
                        {
                          initTranslation(&aux, -1*ptl->data.r.px, -1*ptl->data.r.py);
                          matrixProduct(&m, m, aux);
                          initTranslation(&aux, ptl->data.s.px, ptl->data.s.py);
                          matrixProduct(&m, m, aux);
                        }
              break;
      default: break;
      }
      matrixVectorProduct(&start, m, start);
      matrixVectorProduct(&end, m, end);
      twoDCoord(&pgl->el.data.line.st, start);
      twoDCoord(&pgl->el.data.line.en, end);
      pgl = pgl->next;
    }
    ptl = ptl->next;
  }
}

variable_types: struct trans { union { struct { int px; double sx; } s; struct { int tx; } t; struct { int px; int u; } r; } data; enum TransformType { TRANSLATION=0, SCALING=1, ROTATION=2 } tType; struct trans *next; } *

variable_types: struct GENode { struct GElement { union { struct Point { int x; int y; } point; struct { struct Point st; struct Point en; } line; } data; enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 } type; } el; struct GENode *next; } *

variable_types:
start
struct homoCoord { double coord[3]; }

variable_types:
end
struct homoCoord { double coord[3]; }

user_defined_types: struct trans { union { struct { int px; double sx; } s; struct { int tx; } t; struct { int px; int u; } r; } data; enum TransformType tType; struct trans *next; }

user_defined_types: struct GENode { struct GElement el; struct GENode *next; }

user_defined_types: struct GElement { union { struct Point point; struct { struct Point st; struct Point en; } line; } data; enum GElementType type; }


bresenhamY

decompiled_code:
__int64 __fastcall bresenhamY(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
unsigned int v5; // er10
__int64 result; // rax
int v8; // er9
int v9; // ebp
int v11; // er9
int v12; // er11

v5 = a2;
result = a3 >> 32;
if ( SHIDWORD(a2) <= SHIDWORD(a3) )
{
v8 = HIDWORD(a2);
v9 = HIDWORD(a3);
do
{
while ( 1 )
{
result = putXPMpixel(a1, v5, *(_DWORD *)(a1 + 4) - v8, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > v9 )
return result;
}
result = v5-- + 1;
if ( !a4 )
v5 = result;
v8 = v11 + 1;
}
while ( v8 <= v9 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
unsigned int v5; // er10
__int64 result; // rax
int v8; // er9
int v9; // ebp
int v11; // er9
int v12; // er11

v5 = a2;
result = a3 >> 32;
if ( SHIDWORD(a2) <= SHIDWORD(a3) )
{
v8 = HIDWORD(a2);
v9 = HIDWORD(a3);
do
{
while ( 1 )
{
result = func5(a1, v5, *(_DWORD *)(a1 + 4) - v8, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > v9 )
return result;
}
result = v5-- + 1;
if ( !a4 )
v5 = result;
v8 = v11 + 1;
}
while ( v8 <= v9 );
}
return result;
}

original_code:
static void
bresenhamY (XPM *canvas, Point q, Point r, int symmetry, unsigned short clrIndex)
{
    int dx, dy, D, x, y;
    if(symmetry == 0)
        dx = r.x - q.x;
    else dx = q.x - r.x;
    dy = r.y - q.y;
    D = 2*dx - dy;
    x = q.x;
    for (y = q.y; y <= r.y; y++)
    {
        putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dx;
        else
        {
            D += 2*(dx - dy);
            if(symmetry == 0)
                x++;
            else x--;
        }
    }
}

canonical_original_code:
static void bresenhamY(struct XPM *canvas, struct Point q, struct Point r, int symmetry, unsigned short clrIndex)
{
    int dx; int dy; int D; int x; int y;
    if(symmetry == 0)
        dx = r.x - q.x;
    else dx = q.x - r.x;
    dy = r.y - q.y;
    D = 2*dx - dy;
    x = q.x;
    for (y = q.y; y <= r.y; y++)
    {
        putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dx;
        else
        {
            D += 2*(dx - dy);
            if(symmetry == 0)
                x++;
            else x--;
        }
    }
}

variable_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types:
q
struct Point { int x; int y; }

variable_types:
r
struct Point

variable_types: symmetry int

variable_types:
clrIndex
unsigned short

variable_types: dx int

variable_types: dy int

variable_types: D int

variable_types: x int

variable_types: y int

user_defined_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor *colta; unsigned short **data; }


bresenhamX

decompiled_code:
__int64 __fastcall bresenhamX(__int64 a1, __int64 a2, int a3, int a4, unsigned __int16 a5)
{
__int64 result; // rax
int v7; // er10
signed int v8; // er9
int v11; // er9
int v12; // er11

result = (unsigned int)(a3 - a2);
v7 = HIDWORD(a2);
if ( (int)a2 <= a3 )
{
v8 = a2;
do
{
while ( 1 )
{
result = putXPMpixel(a1, v8, *(_DWORD *)(a1 + 4) - v7, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > a3 )
return result;
}
result = (unsigned int)(v7-- + 1);
if ( !a4 )
v7 = result;
v8 = v11 + 1;
}
while ( v8 <= a3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3, int a4, unsigned __int16 a5)
{
__int64 result; // rax
int v7; // er10
signed int v8; // er9
int v11; // er9
int v12; // er11

result = (unsigned int)(a3 - a2);
v7 = HIDWORD(a2);
if ( (int)a2 <= a3 )
{
v8 = a2;
do
{
while ( 1 )
{
result = func5(a1, v8, *(_DWORD *)(a1 + 4) - v7, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > a3 )
return result;
}
result = (unsigned int)(v7-- + 1);
if ( !a4 )
v7 = result;
v8 = v11 + 1;
}
while ( v8 <= a3 );
}
return result;
}

original_code:
static void
bresenhamX (XPM *canvas, Point q, Point r, int symmetry, unsigned short clrIndex)
{
    int dx, dy, D, x, y;
    dx = r.x - q.x;
    if (symmetry == 0)
        dy = r.y - q.y;
    else dy = q.y - r.y;
    D = 2*dy - dx;
    y = q.y;
    for (x = q.x; x <= r.x; x++)
    {
      putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dy;
        else
        {
            D += 2*(dy - dx);
            if(symmetry == 0)
                y++;
            else y--;
        }
    }
}

canonical_original_code:
static void bresenhamX(struct XPM *canvas, struct Point q, struct Point r, int symmetry, unsigned short clrIndex)
{
    int dx; int dy; int D; int x; int y;
    dx = r.x - q.x;
    if (symmetry == 0)
        dy = r.y - q.y;
    else dy = q.y - r.y;
    D = 2*dy - dx;
    y = q.y;
    for (x = q.x; x <= r.x; x++)
    {
      putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dy;
        else
        {
            D += 2*(dy - dx);
            if(symmetry == 0)
                y++;
            else y--;
        }
    }
}


assignXPMColorTable

original_code:
void
assignXPMColorTable(XPM *img, const unsigned char vColors[][3], int clrCnt){
  XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

canonical_original_code:
void assignXPMColorTable(struct XPM *img, unsigned char vColors[][3], int clrCnt){
  struct XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

variable_types:
vColors
unsigned char [][3]

variable_types: clrCnt int

variable_types: struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; }

variable_types: clrIndex int

variable_types:
charsFormat
char [10]


cohenSutherlandFrameLine

decompiled_code:
__int64 __fastcall cohenSutherlandFrameLine(_DWORD *a1, int *a2, int *a3)
{
int v6; // er13
int v7; // edi
int v8; // esi
int v9; // ecx
unsigned __int8 v10; // al
int v11; // er9
unsigned __int8 v12; // r11

while ( 1 )
{
v6 = a1[4];
v7 = a1[2];
v8 = a1[3];
getRegionCode_isra_0(v7, v8, v6, a1[5], *a2, a2[1]);
v10 = getRegionCode_isra_0(v7, v8, v6, v9, *a3, a3[1]);
if ( !(v10 | v12) )
return 1LL;
if ( (v10 & v12) != 0 )
break;
trimLineToRegion(a1, a2, a3, v12, v10, v11);
}
return 0LL;
}

original_code:
int
cohenSutherlandFrameLine(XPM *canvas, Point *pStart, Point *pEnd)
{
  unsigned char codeStart = getRegionCode(canvas, pStart);
  unsigned char codeEnd = getRegionCode(canvas,pEnd);
    if((codeStart | codeEnd) == 0) return 1;
    if((codeStart & codeEnd) != 0) return 0;
    trimLineToRegion(canvas, pStart, pEnd, codeStart, codeEnd);
    return cohenSutherlandFrameLine(canvas, pStart, pEnd);
}

canonical_original_code:
int cohenSutherlandFrameLine(struct XPM *canvas, struct Point *pStart, struct Point *pEnd)
{
  unsigned char codeStart = getRegionCode(canvas, pStart);
  unsigned char codeEnd = getRegionCode(canvas,pEnd);
    if((codeStart | codeEnd) == 0) return 1;
    if((codeStart & codeEnd) != 0) return 0;
    trimLineToRegion(canvas, pStart, pEnd, codeStart, codeEnd);
    return cohenSutherlandFrameLine(canvas, pStart, pEnd);
}

variable_types:
pStart
struct Point { int x; int y; } *

variable_types:
pEnd
struct Point *

variable_types:
codeStart
unsigned char

variable_types:
codeEnd
unsigned char


drawBresenhamLine

decompiled_code:
__int64 __fastcall drawBresenhamLine(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v5; // rdx
int v7; // ecx
int v9; // ecx

v5 = a3 >> 32;
if ( (int)a2 >= (int)a3 )
{
if ( (_DWORD)a2 == (_DWORD)a3 )
{
if ( SHIDWORD(a2) < (int)v5 )
return bresenhamY(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) <= (int)v5 )
return bresenhamX(a1, a2, a3, 1, a4);
}
else
{
v9 = a2 - a3;
if ( SHIDWORD(a2) < (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) > v9 )
return bresenhamY(a1, a2, a3, 1, a4);
return bresenhamX(a1, a3, a2, 1, a4);
}
if ( HIDWORD(a2) == (_DWORD)v5 )
return bresenhamX(a1, a3, a2, 1, a4);
if ( HIDWORD(a2) - (int)v5 <= v9 )
return bresenhamX(a1, a3, a2, 0, a4);
}
return bresenhamY(a1, a3, a2, 0, a4);
}
v7 = a3 - a2;
if ( SHIDWORD(a2) <= (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) <= v7 )
return bresenhamX(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v5 )
return bresenhamX(a1, a2, a3, 1, a4);
return bresenhamY(a1, a2, a3, 0, a4);
}
if ( HIDWORD(a2) - (int)v5 < v7 )
return bresenhamX(a1, a2, a3, 1, a4);
return bresenhamY(a1, a3, a2, 1, a4);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v5; // rdx
int v7; // ecx
int v9; // ecx

v5 = a3 >> 32;
if ( (int)a2 >= (int)a3 )
{
if ( (_DWORD)a2 == (_DWORD)a3 )
{
if ( SHIDWORD(a2) < (int)v5 )
return func6(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) <= (int)v5 )
return func7(a1, a2, a3, 1, a4);
}
else
{
v9 = a2 - a3;
if ( SHIDWORD(a2) < (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) > v9 )
return func6(a1, a2, a3, 1, a4);
return func7(a1, a3, a2, 1, a4);
}
if ( HIDWORD(a2) == (_DWORD)v5 )
return func7(a1, a3, a2, 1, a4);
if ( HIDWORD(a2) - (int)v5 <= v9 )
return func7(a1, a3, a2, 0, a4);
}
return func6(a1, a3, a2, 0, a4);
}
v7 = a3 - a2;
if ( SHIDWORD(a2) <= (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) <= v7 )
return func7(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v5 )
return func7(a1, a2, a3, 1, a4);
return func6(a1, a2, a3, 0, a4);
}
if ( HIDWORD(a2) - (int)v5 < v7 )
return func7(a1, a2, a3, 1, a4);
return func6(a1, a3, a2, 1, a4);
}

original_code:
void
drawBresenhamLine(XPM *canvas, Point pStart, Point pEnd, unsigned short colorIndex)
{
    if(pEnd.x > pStart.x &&
       pEnd.y >= pStart.y &&
       (pEnd.x - pStart.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x >= pStart.x &&
     pEnd.y > pStart.y &&
     (pEnd.x - pStart.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y > pStart.y &&
     (pStart.x - pEnd.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,1,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y >= pStart.y &&
     (pStart.x - pEnd.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pEnd,pStart,1,colorIndex);
    }
    else if (pEnd.x < pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) >= (pStart.y - pEnd.y))
    {
        bresenhamX(canvas,pEnd,pStart,0,colorIndex);
    }
    else if (pEnd.x <= pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) < (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,0,colorIndex);
    }
    else if(pEnd.x > pStart.x &&
     pEnd.y < pStart.y &&
     (pEnd.x - pStart.x) <= (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,1,colorIndex);
    }
    else
    {
        bresenhamX(canvas,pStart,pEnd,1,colorIndex);
    }
}

canonical_original_code:
void drawBresenhamLine(struct XPM *canvas, struct Point pStart, struct Point pEnd, unsigned short colorIndex)
{
    if(pEnd.x > pStart.x &&
       pEnd.y >= pStart.y &&
       (pEnd.x - pStart.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x >= pStart.x &&
     pEnd.y > pStart.y &&
     (pEnd.x - pStart.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y > pStart.y &&
     (pStart.x - pEnd.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,1,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y >= pStart.y &&
     (pStart.x - pEnd.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pEnd,pStart,1,colorIndex);
    }
    else if (pEnd.x < pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) >= (pStart.y - pEnd.y))
    {
        bresenhamX(canvas,pEnd,pStart,0,colorIndex);
    }
    else if (pEnd.x <= pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) < (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,0,colorIndex);
    }
    else if(pEnd.x > pStart.x &&
     pEnd.y < pStart.y &&
     (pEnd.x - pStart.x) <= (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,1,colorIndex);
    }
    else
    {
        bresenhamX(canvas,pStart,pEnd,1,colorIndex);
    }
}

variable_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types:
pStart
struct Point { int x; int y; }

variable_types:
pEnd
struct Point

variable_types:
colorIndex
unsigned short


renderGElements

decompiled_code:
void __fastcall renderGElements(__int64 a1, unsigned int *a2)
{
unsigned int *v2; // rbx
__int64 v3; // rdx

if ( a2 )
{
v2 = a2;
do
{
while ( 1 )
{
v3 = v2[4];
if ( (_DWORD)v3 == 2 )
break;
fprintf(stderr, "Unhandled type (%d) !\n", v3);
LABEL_4:
v2 = (unsigned int *)*((_QWORD *)v2 + 3);
if ( !v2 )
return;
}
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *v2, v2[1], v2[2], v2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, v2, v2 + 2) == 1 )
{
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *v2, v2[1], v2[2], v2[3]);
drawBresenhamLine(a1, *(_QWORD *)v2, *((_QWORD *)v2 + 1), 1LL);
goto LABEL_4;
}
fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
v2 = (unsigned int *)*((_QWORD *)v2 + 3);
}
while ( v2 );
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, unsigned int *a2)
{
unsigned int *v2; // rbx
__int64 v3; // rdx

if ( a2 )
{
v2 = a2;
do
{
while ( 1 )
{
v3 = v2[4];
if ( (_DWORD)v3 == 2 )
break;
fprintf(stderr, "Unhandled type (%d) !\n", v3);
LABEL_4:
v2 = (unsigned int *)*((_QWORD *)v2 + 3);
if ( !v2 )
return;
}
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *v2, v2[1], v2[2], v2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, v2, v2 + 2) == 1 )
{
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *v2, v2[1], v2[2], v2[3]);
drawBresenhamLine(a1, *(_QWORD *)v2, *((_QWORD *)v2 + 1), 1LL);
goto LABEL_4;
}
fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
v2 = (unsigned int *)*((_QWORD *)v2 + 3);
}
while ( v2 );
}
}

original_code:
void
renderGElements(XPM *canvas, struct GENode *glist){
  struct GENode *pgl = glist;
  GElement *pge = ((void *)0);
  while(((void *)0) != pgl){
    pge = &pgl->el;
    switch(pge->type){
    case LINE:
      printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
      if(cohenSutherlandFrameLine(canvas, &pge->data.line.st, &pge->data.line.en) == 1){
 printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
 drawBresenhamLine(canvas, pge->data.line.st, pge->data.line.en, 1);
      }
      else{
 fprintf(stderr, "*** Line was totally outside the canvas.\n");
      }
      break;
    default:
      fprintf(stderr, "Unhandled type (%d) !\n", pge->type);
      break;
    }
    pgl = pgl->next;
  }
}

canonical_original_code:
void renderGElements(struct XPM *canvas, struct GENode *glist){
  struct GENode *pgl = glist;
  struct GElement *pge = ((void *)0);
  while(((void *)0) != pgl){
    pge = &pgl->el;
    switch(pge->type){
    case LINE:
      printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
      if(cohenSutherlandFrameLine(canvas, &pge->data.line.st, &pge->data.line.en) == 1){
 printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
 drawBresenhamLine(canvas, pge->data.line.st, pge->data.line.en, 1);
      }
      else{
 fprintf(stderr, "*** Line was totally outside the canvas.\n");
      }
      break;
    default:
      fprintf(stderr, "Unhandled type (%d) !\n", pge->type);
      break;
    }
    pgl = pgl->next;
  }
}

variable_types: struct GENode { struct GElement { union { struct Point { int x; int y; } point; struct { struct Point st; struct Point en; } line; } data; enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 } type; } el; struct GENode *next; } *

variable_types: struct GElement { union { struct Point { int x; int y; } point; struct { struct Point st; struct Point en; } line; } data; enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 } type; } *


parsePSLine

decompiled_code:
__int64 __fastcall parsePSLine(__int64 a1, __int64 a2)
{
if ( (unsigned int)_isoc99_sscanf(a1, "%d %d %d %d line", a2, a2 + 4, a2 + 8, a2 + 12) == 4 )
*(_DWORD *)(a2 + 16) = 2;
return 0LL;
}

original_code:
procPSLineReturnVal
parsePSLine(const char *line, GElement *dest){
  procPSLineReturnVal returnCode = PSLINE_OK;
  if(sscanf(line, "%d %d %d %d line", &dest->data.line.st.x, &dest->data.line.st.y, &dest->data.line.en.x, &dest->data.line.en.y) == 4){
    dest->type = LINE;
  }
  return returnCode;
}

canonical_original_code:
enum procPSLineReturnVal parsePSLine(char *line, struct GElement *dest){
  enum procPSLineReturnVal returnCode = PSLINE_OK;
  if(sscanf(line, "%d %d %d %d line", &dest->data.line.st.x, &dest->data.line.st.y, &dest->data.line.en.x, &dest->data.line.en.y) == 4){
    dest->type = LINE;
  }
  return returnCode;
}

variable_types:
line
char *

variable_types: enum procPSLineReturnVal { PSLINE_OK=0, UNRECOGNIZED_ELEMENT=1 }

user_defined_types: enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 }


renderGElement

decompiled_code:
__int64 __fastcall renderGElement(__int64 a1, unsigned int *a2)
{
if ( a2[4] != 2 )
return fwrite("Unhandled type!\n", 1uLL, 0x10uLL, stderr);
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, a2, a2 + 2) != 1 )
return fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
return drawBresenhamLine(a1, *(_QWORD *)a2, *((_QWORD *)a2 + 1), 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int *a2)
{
if ( a2[4] != 2 )
return fwrite("Unhandled type!\n", 1uLL, 0x10uLL, stderr);
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, a2, a2 + 2) != 1 )
return fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
return drawBresenhamLine(a1, *(_QWORD *)a2, *((_QWORD *)a2 + 1), 1LL);
}

original_code:
void
renderGElement(XPM *canvas, GElement *el){
  switch(el->type){
  case LINE:
    printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
    if(cohenSutherlandFrameLine(canvas, &el->data.line.st, &el->data.line.en) == 1){
      printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
      drawBresenhamLine(canvas, el->data.line.st, el->data.line.en, 1);
    }
    else{
      fprintf(stderr, "*** Line was totally outside the canvas.\n");
    }
    break;
  default:
    fprintf(stderr, "Unhandled type!\n");
    break;
  }
}

canonical_original_code:
void renderGElement(struct XPM *canvas, struct GElement *el){
  switch(el->type){
  case LINE:
    printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
    if(cohenSutherlandFrameLine(canvas, &el->data.line.st, &el->data.line.en) == 1){
      printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
      drawBresenhamLine(canvas, el->data.line.st, el->data.line.en, 1);
    }
    else{
      fprintf(stderr, "*** Line was totally outside the canvas.\n");
    }
    break;
  default:
    fprintf(stderr, "Unhandled type!\n");
    break;
  }
}

variable_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *


parsePSLine

return_type: enum procPSLineReturnVal { PSLINE_OK=0, UNRECOGNIZED_ELEMENT=1 }


bresenhamY

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
unsigned int v5; // er10
__int64 result; // rax
int v8; // er9
int v9; // ebp
int v11; // er9
int v12; // er11

v5 = a2;
result = a3 >> 32;
if ( SHIDWORD(a2) <= SHIDWORD(a3) )
{
v8 = HIDWORD(a2);
v9 = HIDWORD(a3);
do
{
while ( 1 )
{
result = func3(a1, v5, *(_DWORD *)(a1 + 4) - v8, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > v9 )
return result;
}
result = v5-- + 1;
if ( !a4 )
v5 = result;
v8 = v11 + 1;
}
while ( v8 <= v9 );
}
return result;
}

variable_types:
q
struct Point { int x; int y; }

variable_types:
r
struct Point

variable_types: symmetry int

variable_types:
clrIndex
unsigned short

variable_types: dx int

variable_types: dy int

variable_types: D int

variable_types: x int

variable_types: y int


bresenhamX

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3, int a4, unsigned __int16 a5)
{
__int64 result; // rax
int v7; // er10
signed int v8; // er9
int v11; // er9
int v12; // er11

result = (unsigned int)(a3 - a2);
v7 = HIDWORD(a2);
if ( (int)a2 <= a3 )
{
v8 = a2;
do
{
while ( 1 )
{
result = func3(a1, v8, *(_DWORD *)(a1 + 4) - v7, a5);
if ( v12 > 0 )
break;
v8 = v11 + 1;
if ( v8 > a3 )
return result;
}
result = (unsigned int)(v7-- + 1);
if ( !a4 )
v7 = result;
v8 = v11 + 1;
}
while ( v8 <= a3 );
}
return result;
}

variable_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *


cohenSutherlandFrameLine

decompiled_code:
__int64 __fastcall cohenSutherlandFrameLine(unsigned int *a1, int *a2, int *a3)
{
unsigned int v5; // edi
unsigned int v6; // er8
unsigned __int8 v7; // al
int v8; // er9

while ( 1 )
{
v5 = *a1;
getRegionCode_isra_0(*a1, a1[1], *a2, a2[1]);
v7 = getRegionCode_isra_0(v5, v6, *a3, a3[1]);
if ( !(v7 | (unsigned __int8)v8) )
return 1LL;
if ( (v7 & (unsigned __int8)v8) != 0 )
break;
trimLineToRegion((int *)a1, a2, a3, v8, v7, v8);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1, int *a2, int *a3)
{
unsigned int v5; // edi
unsigned int v6; // er8
unsigned __int8 v7; // al
int v8; // er9

while ( 1 )
{
v5 = *a1;
func1(*a1, a1[1], *a2, a2[1]);
v7 = func1(v5, v6, *a3, a3[1]);
if ( !(v7 | (unsigned __int8)v8) )
return 1LL;
if ( (v7 & (unsigned __int8)v8) != 0 )
break;
func0((int *)a1, a2, a3, v8, v7, v8);
}
return 0LL;
}

variable_types:
pStart
struct Point { int x; int y; } *

variable_types:
pEnd
struct Point *

variable_types:
codeStart
unsigned char

variable_types:
codeEnd
unsigned char


drawBresenhamLine

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v5; // rdx
int v7; // ecx
int v9; // ecx

v5 = a3 >> 32;
if ( (int)a2 >= (int)a3 )
{
if ( (_DWORD)a2 == (_DWORD)a3 )
{
if ( SHIDWORD(a2) < (int)v5 )
return func4(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) <= (int)v5 )
return func5(a1, a2, a3, 1, a4);
}
else
{
v9 = a2 - a3;
if ( SHIDWORD(a2) < (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) > v9 )
return func4(a1, a2, a3, 1, a4);
return func5(a1, a3, a2, 1, a4);
}
if ( HIDWORD(a2) == (_DWORD)v5 )
return func5(a1, a3, a2, 1, a4);
if ( HIDWORD(a2) - (int)v5 <= v9 )
return func5(a1, a3, a2, 0, a4);
}
return func4(a1, a3, a2, 0, a4);
}
v7 = a3 - a2;
if ( SHIDWORD(a2) <= (int)v5 )
{
if ( (int)v5 - HIDWORD(a2) <= v7 )
return func5(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v5 )
return func5(a1, a2, a3, 1, a4);
return func4(a1, a2, a3, 0, a4);
}
if ( HIDWORD(a2) - (int)v5 < v7 )
return func5(a1, a2, a3, 1, a4);
return func4(a1, a3, a2, 1, a4);
}

variable_types:
pStart
struct Point { int x; int y; }

variable_types:
pEnd
struct Point

variable_types:
colorIndex
unsigned short


assignColorTable

original_code:
void
assignColorTable(XPM *img, const unsigned char vColors[][3], int clrCnt){
  XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

canonical_original_code:
void assignColorTable(struct XPM *img, unsigned char vColors[][3], int clrCnt){
  struct XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

variable_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types:
vColors
unsigned char [][3]

variable_types: clrCnt int

variable_types: clrIndex int

variable_types:
charsFormat
char [10]


repo: alech/alphamote

decompiled_code:
void __noreturn release_usb_device()
{
if ( (unsigned int)usb_release_interface(devh, 0LL) )
{
usb_close(devh);
}
else
{
printf("failed to release interface: %d\n", 0LL);
usb_close(devh);
printf("failed to close interface: %d\n", 0LL);
}
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
if ( (unsigned int)usb_release_interface(devh, 0LL) )
{
usb_close(devh);
}
else
{
printf("failed to release interface: %d\n", 0LL);
usb_close(devh);
printf("failed to close interface: %d\n", 0LL);
}
exit(1);
}

original_code:
void release_usb_device(int dummy) {
    int ret;
    ret = usb_release_interface(devh, 0);
    if (!ret)
 printf("failed to release interface: %d\n", ret);
    usb_close(devh);
    if (!ret)
 printf("failed to close interface: %d\n", ret);
    exit(1);
}

variable_types: dummy int

variable_types: ret int


decompiled_code:
__int64 init_usb()
{
__int64 **v0; // rax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v5[65544]; // [rsp+0h] [rbp-10008h] BYREF

usb_init();
usb_find_busses();
usb_find_devices();
v0 = find_device(1356, 743);
if ( !v0 )
{
fwrite("No camera found!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
devh = usb_open(v0);
if ( !devh )
_assert_fail("devh", "alphamote.c", 0x38u, "init_usb");
signal(15, (__sighandler_t)release_usb_device);
usb_get_driver_np(devh, 0LL, v5, 0xFFFFLL);
v1 = usb_claim_interface(devh, 0LL);
if ( v1 )
{
printf("claim failed with error %d\n", v1);
exit(1);
}
if ( (int)usb_set_altinterface(devh, 0LL) < 0 )
_assert_fail("ret >= 0", "alphamote.c", 0x44u, "init_usb");
usb_get_descriptor(devh, 1LL, 0LL, v5, 18LL);
usleep(0xFA0u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 9LL);
usleep(0xBB8u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 39LL);
usleep(0xFA0u);
v2 = usb_release_interface(devh, 0LL);
if ( v2 )
printf("failed to release interface before set_configuration: %d\n", v2);
usb_set_configuration(devh, 1LL);
v3 = usb_claim_interface(devh, 0LL);
if ( v3 )
printf("claim after set_configuration failed with error %d\n", v3);
return usb_set_altinterface(devh, 0LL);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 **v0; // rax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v5[65544]; // [rsp+0h] [rbp-10008h] BYREF

usb_init();
usb_find_busses();
usb_find_devices();
v0 = find_device(1356, 743);
if ( !v0 )
{
fwrite("No camera found!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
devh = usb_open(v0);
if ( !devh )
_assert_fail("devh", "alphamote.c", 0x38u, "init_usb");
signal(15, (__sighandler_t)release_usb_device);
usb_get_driver_np(devh, 0LL, v5, 0xFFFFLL);
v1 = usb_claim_interface(devh, 0LL);
if ( v1 )
{
printf("claim failed with error %d\n", v1);
exit(1);
}
if ( (int)usb_set_altinterface(devh, 0LL) < 0 )
_assert_fail("ret >= 0", "alphamote.c", 0x44u, "init_usb");
usb_get_descriptor(devh, 1LL, 0LL, v5, 18LL);
usleep(0xFA0u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 9LL);
usleep(0xBB8u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 39LL);
usleep(0xFA0u);
v2 = usb_release_interface(devh, 0LL);
if ( v2 )
printf("failed to release interface before set_configuration: %d\n", v2);
usb_set_configuration(devh, 1LL);
v3 = usb_claim_interface(devh, 0LL);
if ( v3 )
printf("claim after set_configuration failed with error %d\n", v3);
return usb_set_altinterface(devh, 0LL);
}

original_code:
void init_usb() {
    int vendor, product, ret;
    struct usb_device *dev;
    char buf[65535];
    usb_init();
    usb_find_busses();
    usb_find_devices();
    vendor = 0x054c;
    product = 0x02e7;
    dev = find_device(vendor, product);
    if (! dev) {
        fprintf(stderr, "No camera found!\n");
        exit(1);
    }
    devh = usb_open(dev);
    ((void) sizeof ((devh) ? 1 : 0), __extension__ ({ if (devh) ; else __assert_fail ("devh", "alphamote.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    signal(15, release_usb_device);
    ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) {
 printf("claim failed with error %d\n", ret);
 exit(1);
    }
    ret = usb_set_altinterface(devh, 0);
    ((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (ret >= 0) ; else __assert_fail ("ret >= 0", "alphamote.c", 68, __extension__ __PRETTY_FUNCTION__); }));
    ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
    usleep(4*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
    usleep(3*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
    usleep(4*1000);
    ret = usb_release_interface(devh, 0);
    if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
    ret = usb_set_configuration(devh, 0x0000001);
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
    ret = usb_set_altinterface(devh, 0);
    return;
}

canonical_original_code:
void init_usb() {
    int vendor; int product; int ret;
    struct usb_device *dev;
    char buf[65535];
    usb_init();
    usb_find_busses();
    usb_find_devices();
    vendor = 0x054c;
    product = 0x02e7;
    dev = find_device(vendor, product);
    if (! dev) {
        fprintf(stderr, "No camera found!\n");
        exit(1);
    }
    devh = usb_open(dev);
    ((void) sizeof ((devh) ? 1 : 0), __extension__ ({ if (devh) ; else __assert_fail ("devh", "alphamote.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    signal(15, release_usb_device);
    ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) {
 printf("claim failed with error %d\n", ret);
 exit(1);
    }
    ret = usb_set_altinterface(devh, 0);
    ((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (ret >= 0) ; else __assert_fail ("ret >= 0", "alphamote.c", 68, __extension__ __PRETTY_FUNCTION__); }));
    ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
    usleep(4*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
    usleep(3*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
    usleep(4*1000);
    ret = usb_release_interface(devh, 0);
    if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
    ret = usb_set_configuration(devh, 0x0000001);
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
    ret = usb_set_altinterface(devh, 0);
    return;
}

variable_types: vendor int

variable_types: product int

variable_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device *devices; unsigned int location; struct usb_device *root_dev; } *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *

variable_types:
buf
char [65535]

user_defined_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; }

user_defined_types: struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; int extralen; }

user_defined_types: struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }

user_defined_types: struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen; }