decompiled_code:
void *__fastcall hll_cnt_new(__int64 a1)
{
void *v1; // rax
void *v2; // r12

v1 = malloc(0x18uLL);
if ( !v1 )
{
fprintf(stderr, "Could not allocate memory successfully %s, %d", "hll_counter.c", 69LL);
exit(1);
}
v2 = v1;
hll_cnt_init((__int64)v1, a1);
return v2;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1)
{
void *v1; // rax
void *v2; // r12

v1 = malloc(0x18uLL);
if ( !v1 )
{
fprintf(stderr, "Could not allocate memory successfully %s, %d", "hll_counter.c", 69LL);
exit(1);
}
v2 = v1;
func1((__int64)v1, a1);
return v2;
}

original_code:
inline hll_counter_t * hll_cnt_new(size_t bits) {
  hll_counter_t *c = malloc(sizeof(hll_counter_t));
  if(c == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "hll_counter.c", 69); exit(1); };
  hll_cnt_init(c, bits);
  return c;
}

canonical_original_code:
inline struct hll_counter *hll_cnt_new(unsigned long bits) {
  struct hll_counter *c = malloc(sizeof(hll_counter_t));
  if(c == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "hll_counter.c", 69); exit(1); };
  hll_cnt_init(c, bits);
  return c;
}

return_type: struct hll_counter { unsigned long m; unsigned int b; unsigned int mask; unsigned char *registers; } *


repo: Cecca/gdem

decompiled_code:
void __fastcall hll_cnt_delete(void *ptr)
{
hll_cnt_free((__int64)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
func2((__int64)ptr);
free(ptr);
}

original_code:
inline void hll_cnt_delete(hll_counter_t * counter) {
  hll_cnt_free(counter);
  free(counter);
}

canonical_original_code:
inline void hll_cnt_delete(struct hll_counter *counter) {
  hll_cnt_free(counter);
  free(counter);
}

variable_types: struct hll_counter { unsigned long m; unsigned int b; unsigned int mask; unsigned char *registers; } *

return_type: void


decompiled_code:
void *__fastcall hll_cnt_copy_to(__int64 a1, __int64 a2)
{
return memcpy(*(void **)(a2 + 16), *(const void **)(a1 + 16), *(_QWORD *)a1);
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, __int64 a2)
{
return memcpy(*(void **)(a2 + 16), *(const void **)(a1 + 16), *(_QWORD *)a1);
}

original_code:
inline void hll_cnt_copy_to (hll_counter_t *from, hll_counter_t *to) {
  ((void) (0));
  ((void) (0));
  ((void) (0));
  memcpy(to->registers, from->registers, from->m*sizeof(hll_reg_t));
}

canonical_original_code:
inline void hll_cnt_copy_to(struct hll_counter *from, struct hll_counter *to) {
  ((void) (0));
  ((void) (0));
  ((void) (0));
  memcpy(to->registers, from->registers, from->m*sizeof(hll_reg_t));
}

variable_types:
to
struct hll_counter *

user_defined_types: struct hll_counter { unsigned long m; unsigned int b; unsigned int mask; unsigned char *registers; }


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

result = 32LL;
v3 = a1 & a2;
if ( v3 )
{
for ( result = 1LL; (v3 & 1) == 0; result = (unsigned int)(result + 1) )
v3 >>= 1;
}
return result;
}

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

result = 32LL;
v3 = a1 & a2;
if ( v3 )
{
for ( result = 1LL; (v3 & 1) == 0; result = (unsigned int)(result + 1) )
v3 >>= 1;
}
return result;
}

original_code:
inline hll_reg_t hll_cnt_rho(hll_hash_t x, hll_hash_t mask) {
  if( (x & mask) == 0 ) {
    return sizeof(hll_hash_t)*8;
  }
  hll_reg_t i = 1;
  hll_hash_t k;
  for(k = x & mask; (k & 1) != 1 ; k = k >> 1) {
    ++i;
  }
  return i;
}

canonical_original_code:
inline unsigned char hll_cnt_rho(unsigned int x, unsigned int mask) {
  if( (x & mask) == 0 ) {
    return sizeof(hll_hash_t)*8;
  }
  unsigned char i = 1;
  unsigned int k;
  for(k = x & mask; (k & 1) != 1 ; k = k >> 1) {
    ++i;
  }
  return i;
}

variable_types:
x
unsigned int

variable_types:
mask
unsigned int

variable_types:
i
unsigned char

variable_types:
k
unsigned int

return_type: unsigned char


decompiled_code:
unsigned __int8 *__fastcall hll_cnt_add(unsigned int a1)
{
int v1; // edi
__int64 v2; // r8
unsigned __int8 v3; // cl
__int64 v4; // r8
__int64 v5; // rdx
unsigned __int8 *result; // rax

v1 = jenkins(a1, HLL_SEED);
v3 = hll_cnt_rho(v1, *(_DWORD *)(v2 + 12));
result = (unsigned __int8 *)(v5 + *(_QWORD *)(v4 + 16));
if ( *result >= v3 )
v3 = *result;
*result = v3;
return result;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func8(unsigned int a1)
{
int v1; // edi
__int64 v2; // r8
unsigned __int8 v3; // cl
__int64 v4; // r8
__int64 v5; // rdx
unsigned __int8 *result; // rax

v1 = func0(a1, HLL_SEED);
v3 = func7(v1, *(_DWORD *)(v2 + 12));
result = (unsigned __int8 *)(v5 + *(_QWORD *)(v4 + 16));
if ( *result >= v3 )
v3 = *result;
*result = v3;
return result;
}

original_code:
inline void hll_cnt_add(hll_hash_t elem, hll_counter_t * cnt) {
  ((void) (0));
  hll_hash_t x = jenkins(elem, HLL_SEED);
  size_t j = (cnt->b == 0)? 0 : x >> (sizeof(hll_hash_t)*8 - cnt->b);
  hll_reg_t r = hll_cnt_rho(x, cnt->mask);
  cnt->registers[j] = (cnt->registers[j] > r)? cnt->registers[j] : r;
}

canonical_original_code:
inline void hll_cnt_add(unsigned int elem, struct hll_counter *cnt) {
  ((void) (0));
  unsigned int x = jenkins(elem, HLL_SEED);
  unsigned long j = (cnt->b == 0)? 0 : x >> (sizeof(hll_hash_t)*8 - cnt->b);
  unsigned char r = hll_cnt_rho(x, cnt->mask);
  cnt->registers[j] = (cnt->registers[j] > r)? cnt->registers[j] : r;
}

variable_types:
elem
unsigned int

variable_types:
j
unsigned long

variable_types:
r
unsigned char


original_code:
inline hll_cardinality_t hll_cnt_size(hll_counter_t * counter) {
  ((void) (0));
  double denominator = 0;
  int i;
  for (i = 0; i<counter->m; ++i) {
    denominator += 1.0 / (1ull << counter->registers[i]);
  }
  return counter->m*counter->m*0.72134/denominator;
}

canonical_original_code:
inline double hll_cnt_size(struct hll_counter *counter) {
  ((void) (0));
  double denominator = 0;
  int i;
  for (i = 0; i<counter->m; ++i) {
    denominator += 1.0 / (1ull << counter->registers[i]);
  }
  return counter->m*counter->m*0.72134/denominator;
}

variable_types: denominator double

variable_types: i int

return_type: double


decompiled_code:
void __fastcall hll_cnt_union_i(unsigned __int64 *a1, __int64 a2)
{
unsigned __int64 v2; // rax
unsigned __int8 *v3; // rcx
unsigned __int8 v4; // dl

if ( *a1 )
{
v2 = 0LL;
do
{
v3 = (unsigned __int8 *)(v2 + a1[2]);
v4 = *(_BYTE *)(*(_QWORD *)(a2 + 16) + v2);
if ( v4 < *v3 )
v4 = *v3;
++v2;
*v3 = v4;
}
while ( *a1 > v2 );
}
}

canonical_decompiled_code:
void __fastcall func10(unsigned __int64 *a1, __int64 a2)
{
unsigned __int64 v2; // rax
unsigned __int8 *v3; // rcx
unsigned __int8 v4; // dl

if ( *a1 )
{
v2 = 0LL;
do
{
v3 = (unsigned __int8 *)(v2 + a1[2]);
v4 = *(_BYTE *)(*(_QWORD *)(a2 + 16) + v2);
if ( v4 < *v3 )
v4 = *v3;
++v2;
*v3 = v4;
}
while ( *a1 > v2 );
}
}

original_code:
inline void hll_cnt_union_i(hll_counter_t *a, hll_counter_t *b) {
  ((void) (0));
  ((void) (0));
  ((void) (0));
  int i=0;
  for(; i<a->m; ++i) {
    a->registers[i] = (a->registers[i] >= b->registers[i])? a->registers[i] : b->registers[i];
  }
}

canonical_original_code:
inline void hll_cnt_union_i(struct hll_counter *a, struct hll_counter *b) {
  ((void) (0));
  ((void) (0));
  ((void) (0));
  int i = 0;
  for(; i<a->m; ++i) {
    a->registers[i] = (a->registers[i] >= b->registers[i])? a->registers[i] : b->registers[i];
  }
}

variable_types:
b
struct hll_counter *


decompiled_code:
_BOOL8 __fastcall hll_cnt_equals(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax

if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(a2 + 8) )
result = memcmp(*(const void **)(a1 + 16), *(const void **)(a2 + 16), *(_QWORD *)a1) == 0;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax

if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(a2 + 8) )
result = memcmp(*(const void **)(a1 + 16), *(const void **)(a2 + 16), *(_QWORD *)a1) == 0;
else
result = 0LL;
return result;
}

original_code:
inline int hll_cnt_equals(hll_counter_t * a, hll_counter_t * b) {
  ((void) (0));
  ((void) (0));
  if(a->b != b->b) {
    return 0;
  }
  if(memcmp(a->registers, b->registers, a->m*sizeof(hll_reg_t)) != 0) {
    return 0;
  }
  return 1;
}

canonical_original_code:
inline int hll_cnt_equals(struct hll_counter *a, struct hll_counter *b) {
  ((void) (0));
  ((void) (0));
  if(a->b != b->b) {
    return 0;
  }
  if(memcmp(a->registers, b->registers, a->m*sizeof(hll_reg_t)) != 0) {
    return 0;
  }
  return 1;
}

return_type: int


decompiled_code:
void __fastcall hll_cnt_replace_registers(__int64 a1, __int64 a2)
{
free(*(void **)(a1 + 16));
*(_QWORD *)(a1 + 16) = a2;
}

original_code:
inline void hll_cnt_replace_registers (hll_counter_t *cnt, hll_reg_t *reg) {
  free(cnt->registers);
  cnt->registers = reg;
}

canonical_original_code:
inline void hll_cnt_replace_registers(struct hll_counter *cnt, unsigned char *reg) {
  free(cnt->registers);
  cnt->registers = reg;
}

variable_types:
reg
unsigned char *


decompiled_code:
int __fastcall error_message(const char *a1)
{
return fprintf(stderr, "Error in format of description: < %s > SKIPPING\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "Error in format of description: < %s > SKIPPING\n", a1);
}

original_code:
void error_message(char* descr) {
  fprintf(stderr, "Error in format of description: < %s > SKIPPING\n", descr);
}

canonical_original_code:
void error_message(char *descr) {
  fprintf(stderr, "Error in format of description: < %s > SKIPPING\n", descr);
}

variable_types:
descr
char *


decompiled_code:
__int64 __fastcall count_numbers(const char *a1)
{
unsigned int v1; // er13
int v2; // er12
const char *v3; // rbx
char *v4; // rbp
unsigned __int16 v5; // ax

v1 = 0;
v2 = 0;
v3 = a1;
v4 = (char *)&a1[strlen(a1)];
while ( v3 != v4 )
{
v5 = (*_ctype_b_loc())[*v3];
if ( (v5 & 0x800) != 0 )
{
if ( !v2 )
{
++v1;
v2 = 1;
}
}
else
{
if ( (v5 & 0x2000) == 0 )
return (unsigned int)-1;
v2 = 0;
}
++v3;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
unsigned int v1; // er13
int v2; // er12
const char *v3; // rbx
char *v4; // rbp
unsigned __int16 v5; // ax

v1 = 0;
v2 = 0;
v3 = a1;
v4 = (char *)&a1[strlen(a1)];
while ( v3 != v4 )
{
v5 = (*_ctype_b_loc())[*v3];
if ( (v5 & 0x800) != 0 )
{
if ( !v2 )
{
++v1;
v2 = 1;
}
}
else
{
if ( (v5 & 0x2000) == 0 )
return (unsigned int)-1;
v2 = 0;
}
++v3;
}
return v1;
}

original_code:
int count_numbers (char *str) {
  int count = 0;
  int i=0;
  int was_digit = 0;
  for (; i<strlen(str); ++i) {
    if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISdigit)) {
      if (!was_digit) {
        ++count;
      }
      was_digit = 1;
    } else if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace)) {
      was_digit = 0;
    } else {
      return -1;
    }
  }
  return count;
}

canonical_original_code:
int count_numbers(char *str) {
  int count = 0;
  int i = 0;
  int was_digit = 0;
  for (; i<strlen(str); ++i) {
    if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISdigit)) {
      if (!was_digit) {
        ++count;
      }
      was_digit = 1;
    } else if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace)) {
      was_digit = 0;
    } else {
      return -1;
    }
  }
  return count;
}

variable_types:
str
char *

variable_types: count int


decompiled_code:
__int64 __fastcall populate_adjacency(char *a1, __int64 a2, int a3)
{
__int64 v4; // rbx
unsigned int v5; // er13
int v6; // eax
char *v7; // rbx
char *v8; // rbp
const unsigned __int16 **v9; // rax
__int64 v10; // rdx
char *endptr; // [rsp+18h] [rbp-30h] BYREF

endptr = a1;
if ( a1 && a3 > 0 )
{
v4 = 1LL;
do
{
v5 = v4;
v6 = strtol(a1, &endptr, 10);
a1 = endptr;
*(_DWORD *)(a2 + 4 * v4 - 4) = v6;
LOBYTE(v6) = a3 > (int)v4++;
}
while ( ((unsigned __int8)v6 & (a1 != 0LL)) != 0 );
}
else
{
v5 = 0;
}
if ( a1 && *a1 )
{
v7 = a1;
v8 = &a1[strlen(a1)];
while ( v8 != v7 )
{
v9 = _ctype_b_loc();
v10 = *v7++;
if ( ((*v9)[v10] & 0x2000) == 0 )
{
fprintf(
stderr,
"%s:%d: ERROR: trying to populate an andjacency list with more values than the array can host\n",
"parser.c",
117LL);
return 0xFFFFFFFFLL;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, __int64 a2, int a3)
{
__int64 v4; // rbx
unsigned int v5; // er13
int v6; // eax
char *v7; // rbx
char *v8; // rbp
const unsigned __int16 **v9; // rax
__int64 v10; // rdx
char *endptr; // [rsp+18h] [rbp-30h] BYREF

endptr = a1;
if ( a1 && a3 > 0 )
{
v4 = 1LL;
do
{
v5 = v4;
v6 = strtol(a1, &endptr, 10);
a1 = endptr;
*(_DWORD *)(a2 + 4 * v4 - 4) = v6;
LOBYTE(v6) = a3 > (int)v4++;
}
while ( ((unsigned __int8)v6 & (a1 != 0LL)) != 0 );
}
else
{
v5 = 0;
}
if ( a1 && *a1 )
{
v7 = a1;
v8 = &a1[strlen(a1)];
while ( v8 != v7 )
{
v9 = _ctype_b_loc();
v10 = *v7++;
if ( ((*v9)[v10] & 0x2000) == 0 )
{
fprintf(
stderr,
"%s:%d: ERROR: trying to populate an andjacency list with more values than the array can host\n",
"parser.c",
117LL);
return 0xFFFFFFFFLL;
}
}
}
return v5;
}

original_code:
int populate_adjacency(char *adj_str, node_id_t *adj, int n) {
  char *str = adj_str;
  node_id_t elem;
  int i = 0;
  while (str != ((void *)0) && i<n) {
    elem = strtol(str, &str, 10);
    adj[i] = elem;
    ++i;
  }
  if (str != ((void *)0) && strcmp(str, "") != 0) {
    int j = 0;
    for(; j< strlen(str); ++j) {
      if(!((*__ctype_b_loc ())[(int) ((str[j]))] & (unsigned short int) _ISspace)) {
        fprintf(stderr,
                "%s:%d: ERROR: trying to populate an andjacency list with more "
                "values than the array can host\n", "parser.c", 117);
        return -1;
      }
    }
  }
  return i;
}

canonical_original_code:
int populate_adjacency(char *adj_str, unsigned int *adj, int n) {
  char *str = adj_str;
  unsigned int elem;
  int i = 0;
  while (str != ((void *)0) && i<n) {
    elem = strtol(str, &str, 10);
    adj[i] = elem;
    ++i;
  }
  if (str != ((void *)0) && strcmp(str, "") != 0) {
    int j = 0;
    for(; j< strlen(str); ++j) {
      if(!((*__ctype_b_loc ())[(int) ((str[j]))] & (unsigned short) _ISspace)) {
        fprintf(stderr,
                "%s:%d: ERROR: trying to populate an andjacency list with more "
                "values than the array can host\n", "parser.c", 117);
        return -1;
      }
    }
  }
  return i;
}

variable_types:
adj
unsigned int *

variable_types: n int

variable_types: j int


decompiled_code:
__int64 __fastcall parse_node_descr_to(void *src, __int64 a2)
{
size_t v2; // rax
__int64 v3; // rbx
size_t v4; // rdx
signed __int64 v5; // rax
void *v6; // rsp
void *v7; // rsp
size_t v8; // rdx
void *v9; // rsp
__int64 v10; // rcx
int v11; // ebx
int v12; // eax
int v13; // er9
__int64 result; // rax
char v15[8]; // [rsp+0h] [rbp-50h] BYREF
__int64 v16; // [rsp+8h] [rbp-48h]
unsigned int v17; // [rsp+1Ch] [rbp-34h] BYREF

v2 = strlen((const char *)src);
v3 = (int)v2;
v4 = v2;
v5 = 16 * ((unsigned __int64)((int)v2 + 15LL) >> 4);
v6 = alloca(v5);
v7 = alloca(v5);
if ( *((char *)src + v3 - 1) == 32 )
{
_isoc99_sscanf(src, "%d |%[0123456789 ]|%[0123456789 ]", &v17, v15, v15);
}
else
{
v8 = v4 + 1;
v16 = (int)v8;
v9 = alloca((int)v8);
memcpy(v15, src, v8);
v10 = v16;
v15[v3] = 32;
v15[v10] = 0;
_isoc99_sscanf(v15, "%d |%[0123456789 ]|%[0123456789 ]", &v17, v15, v15);
}
v11 = count_numbers(v15);
v12 = count_numbers(v15);
if ( v12 < 0 || v11 < 0 )
{
error_message((const char *)src);
result = 2LL;
}
else
{
LODWORD(v16) = v12;
node_init(a2, v17, v11, v12);
if ( v11 == (unsigned int)populate_adjacency(v15, *(_QWORD *)(a2 + 24), v11) )
{
v13 = populate_adjacency(v15, *(_QWORD *)(a2 + 32), v16);
result = 0LL;
if ( (_DWORD)v16 != v13 )
{
error_message((const char *)src);
node_free(a2);
result = 4LL;
}
}
else
{
error_message((const char *)src);
node_free(a2);
result = 3LL;
}
}
return result;
}

original_code:
int parse_node_descr_to (char *descr, node_t *node) {
  int id;
  int len = strlen(descr);
  char out_s[len], in_s[len];
  int read;
  if(descr[len-1] != ' ') {
    char str[len+1];
    strcpy(str, descr);
    str[len] = ' ';
    str[len+1] = '\0';
    read =
        sscanf(str, "%d |%[0123456789 ]|%[0123456789 ]", &id, out_s, in_s);
  } else {
    read =
        sscanf(descr, "%d |%[0123456789 ]|%[0123456789 ]", &id, out_s, in_s);
  }
  int n_out = count_numbers(out_s);
  int n_in = count_numbers(in_s);
  if (n_in < 0 || n_out < 0) {
    error_message(descr);
    return 2;
  }
  node_init(node, id, n_out, n_in);
  int inserted = -1;
  inserted = populate_adjacency(out_s, node->out, n_out);
  if (inserted != n_out) {
    error_message(descr);
    node_free(node);
    return 3;
  }
  inserted = populate_adjacency(in_s, node->in, n_in);
  if (inserted != n_in) {
    error_message(descr);
    node_free(node);
    return 4;
  }
  return 0;
}

canonical_original_code:
int parse_node_descr_to(char *descr, struct _node *node) {
  int id;
  int len = strlen(descr);
  char out_s[-1]; char in_s[-1];
  int read;
  if(descr[len-1] != ' ') {
    char str[-1];
    strcpy(str, descr);
    str[len] = ' ';
    str[len+1] = '\0';
    read =
        sscanf(str, "%d |%[0123456789 ]|%[0123456789 ]", &id, out_s, in_s);
  } else {
    read =
        sscanf(descr, "%d |%[0123456789 ]|%[0123456789 ]", &id, out_s, in_s);
  }
  int n_out = count_numbers(out_s);
  int n_in = count_numbers(in_s);
  if (n_in < 0 || n_out < 0) {
    error_message(descr);
    return 2;
  }
  node_init(node, id, n_out, n_in);
  int inserted = -1;
  inserted = populate_adjacency(out_s, node->out, n_out);
  if (inserted != n_out) {
    error_message(descr);
    node_free(node);
    return 3;
  }
  inserted = populate_adjacency(in_s, node->in, n_in);
  if (inserted != n_in) {
    error_message(descr);
    node_free(node);
    return 4;
  }
  return 0;
}

variable_types: struct _node { unsigned int id; unsigned long num_out; unsigned long num_in; unsigned int *out; unsigned int *in; } *

variable_types: id int

variable_types: len int

variable_types: read int

variable_types:
str
char [-1]

variable_types: inserted int


decompiled_code:
void *__fastcall parse_node_descr(void *src)
{
void *v1; // rax
void *v2; // r12

v1 = malloc(0x28uLL);
if ( !v1 )
{
fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 73LL);
exit(1);
}
v2 = v1;
parse_node_descr_to(src, (__int64)v1);
return v2;
}

canonical_decompiled_code:
void *__fastcall func4(void *src)
{
void *v1; // rax
void *v2; // r12

v1 = malloc(0x28uLL);
if ( !v1 )
{
fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 73LL);
exit(1);
}
v2 = v1;
func3(src, (__int64)v1);
return v2;
}

original_code:
node_t * parse_node_descr (char *descr) {
  node_t * node = malloc(sizeof(node_t));
  if(node == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 73); exit(1); };
  parse_node_descr_to(descr, node);
  return node;
}

canonical_original_code:
struct _node *parse_node_descr(char *descr) {
  struct _node *node = malloc(sizeof(node_t));
  if(node == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 73); exit(1); };
  parse_node_descr_to(descr, node);
  return node;
}

return_type: struct _node { unsigned int id; unsigned long num_out; unsigned long num_in; unsigned int *out; unsigned int *in; } *


decompiled_code:
__int64 __fastcall count_lines(const char *a1)
{
int v1; // eax
int v2; // esi
__int64 v3; // rdx
const char *v4; // rax
unsigned int v5; // er8
__int64 v6; // rcx
int v7; // edx

v1 = strlen(a1);
v2 = v1;
if ( v1 <= 0 )
{
v5 = 0;
}
else
{
v3 = (unsigned int)(v1 - 1);
v4 = a1;
v5 = 0;
v6 = (__int64)&a1[v3 + 1];
do
{
v7 = *v4++ == 10;
v5 += v7;
}
while ( v4 != (const char *)v6 );
}
if ( a1[v2 - 1] != 10 )
++v5;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
int v1; // eax
int v2; // esi
__int64 v3; // rdx
const char *v4; // rax
unsigned int v5; // er8
__int64 v6; // rcx
int v7; // edx

v1 = strlen(a1);
v2 = v1;
if ( v1 <= 0 )
{
v5 = 0;
}
else
{
v3 = (unsigned int)(v1 - 1);
v4 = a1;
v5 = 0;
v6 = (__int64)&a1[v3 + 1];
do
{
v7 = *v4++ == 10;
v5 += v7;
}
while ( v4 != (const char *)v6 );
}
if ( a1[v2 - 1] != 10 )
++v5;
return v5;
}

original_code:
int count_lines (char *str) {
  int count = 0;
  int len = strlen(str);
  for (int i=0; i < len; ++i) {
    if (str[i] == '\n') {
      ++count;
    }
  }
  if(str[len-1] != '\n') {
    ++count;
  }
  return count;
}

canonical_original_code:
int count_lines(char *str) {
  int count = 0;
  int len = strlen(str);
  for (int i = 0; i < len; ++i) {
    if (str[i] == '\n') {
      ++count;
    }
  }
  if(str[len-1] != '\n') {
    ++count;
  }
  return count;
}


decompiled_code:
__int64 __fastcall parse_graph_string(char *s, __int64 *a2, _DWORD *a3)
{
int v5; // eax
unsigned int i; // er13
char *v7; // rdi
unsigned int v8; // eax
__int64 v9; // rbx
char *v10; // rdi
__int64 v11; // rsi
unsigned int v13; // [rsp+Ch] [rbp-2Ch]

v5 = count_lines(s);
*a3 = v5;
if ( v5 )
{
i = 0;
*a2 = (__int64)malloc(40LL * v5);
v7 = strtok(s, "\n");
if ( v7 )
{
v8 = parse_node_descr_to(v7, *a2);
if ( v8 )
{
LABEL_8:
v13 = v8;
i = -1;
free((void *)*a2);
fprintf(stderr, "Error in parsing node description: rc=%d\n", v13);
}
else
{
v9 = 40LL;
for ( i = 1; ; ++i )
{
v10 = strtok(0LL, "\n");
if ( !v10 )
break;
v11 = v9 + *a2;
v9 += 40LL;
v8 = parse_node_descr_to(v10, v11);
if ( v8 )
goto LABEL_8;
}
}
}
}
else
{
i = -1;
fprintf(stderr, "%s:%d: ERROR: trying to allocate 0 bytes.\n", "parser.c", 180LL);
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s, __int64 *a2, _DWORD *a3)
{
int v5; // eax
unsigned int i; // er13
char *v7; // rdi
unsigned int v8; // eax
__int64 v9; // rbx
char *v10; // rdi
__int64 v11; // rsi
unsigned int v13; // [rsp+Ch] [rbp-2Ch]

v5 = func6(s);
*a3 = v5;
if ( v5 )
{
i = 0;
*a2 = (__int64)malloc(40LL * v5);
v7 = strtok(s, "\n");
if ( v7 )
{
v8 = func3(v7, *a2);
if ( v8 )
{
LABEL_8:
v13 = v8;
i = -1;
free((void *)*a2);
fprintf(stderr, "Error in parsing node description: rc=%d\n", v13);
}
else
{
v9 = 40LL;
for ( i = 1; ; ++i )
{
v10 = strtok(0LL, "\n");
if ( !v10 )
break;
v11 = v9 + *a2;
v9 += 40LL;
v8 = func3(v10, v11);
if ( v8 )
goto LABEL_8;
}
}
}
}
else
{
i = -1;
fprintf(stderr, "%s:%d: ERROR: trying to allocate 0 bytes.\n", "parser.c", 180LL);
}
return i;
}

original_code:
int parse_graph_string (char *str, node_t **nodes, int *n) {
  *n = count_lines(str);
  int i = 0;
  int rc = 0;
  if (*n != 0) {
    *nodes = malloc(*n * sizeof(node_t));
    if(nodes == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 177); exit(1); };
  } else {
    fprintf(stderr, "%s:%d: ERROR: trying to allocate 0 bytes.\n",
            "parser.c", 180);
    return -1;
  }
  char *line = strtok(str, "\n");
  if (line != ((void *)0)) {
    rc = parse_node_descr_to(line, &((*nodes)[i]));
    if (rc != 0) {
      free(*nodes);
      fprintf(stderr, "Error in parsing node description: rc=%d\n", rc);
      return -1;
    }
    ++i;
  }
  while(line != ((void *)0)) {
    line = strtok(((void *)0), "\n");
    if(line != ((void *)0)) {
      rc = parse_node_descr_to(line, &((*nodes)[i]));
      if (rc != 0) {
        free(*nodes);
        fprintf(stderr, "Error in parsing node description: rc=%d\n", rc);
        return -1;
      }
      ++i;
    }
  }
  return i;
}

canonical_original_code:
int parse_graph_string(char *str, struct _node **nodes, int *n) {
  *n = count_lines(str);
  int i = 0;
  int rc = 0;
  if (*n != 0) {
    *nodes = malloc(*n * sizeof(node_t));
    if(nodes == 0) { fprintf(stderr, "Could not allocate memory successfully %s, %d", "parser.c", 177); exit(1); };
  } else {
    fprintf(stderr, "%s:%d: ERROR: trying to allocate 0 bytes.\n",
            "parser.c", 180);
    return -1;
  }
  char *line = strtok(str, "\n");
  if (line != ((void *)0)) {
    rc = parse_node_descr_to(line, &((*nodes)[i]));
    if (rc != 0) {
      free(*nodes);
      fprintf(stderr, "Error in parsing node description: rc=%d\n", rc);
      return -1;
    }
    ++i;
  }
  while(line != ((void *)0)) {
    line = strtok(((void *)0), "\n");
    if(line != ((void *)0)) {
      rc = parse_node_descr_to(line, &((*nodes)[i]));
      if (rc != 0) {
        free(*nodes);
        fprintf(stderr, "Error in parsing node description: rc=%d\n", rc);
        return -1;
      }
      ++i;
    }
  }
  return i;
}

variable_types: struct _node { unsigned int id; unsigned long num_out; unsigned long num_in; unsigned int *out; unsigned int *in; } **

variable_types:
n
int *

variable_types: rc int

variable_types:
line
char *


decompiled_code:
__int64 __fastcall parse_graph_file(const char *a1, __int64 *a2, _DWORD *a3)
{
char *v4; // rbp
unsigned int v5; // er12

v4 = (char *)read_file(a1);
v5 = parse_graph_string(v4, a2, a3);
free(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, __int64 *a2, _DWORD *a3)
{
char *v4; // rbp
unsigned int v5; // er12

v4 = (char *)func5(a1);
v5 = func7(v4, a2, a3);
free(v4);
return v5;
}

original_code:
int parse_graph_file (char *filename, node_t **nodes, int *n) {
  char * file_contents = read_file(filename);
  int num_parsed = parse_graph_string(file_contents, nodes, n);
  free(file_contents);
  return num_parsed;
}

canonical_original_code:
int parse_graph_file(char *filename, struct _node **nodes, int *n) {
  char *file_contents = read_file(filename);
  int num_parsed = parse_graph_string(file_contents, nodes, n);
  free(file_contents);
  return num_parsed;
}

variable_types:
filename
char *

user_defined_types: struct _node { unsigned int id; unsigned long num_out; unsigned long num_in; unsigned int *out; unsigned int *in; }


original_code:
void node_init (node_t *node, node_id_t id, size_t out_n, size_t in_n) {
  node->id = id;
  node->num_out = out_n;
  node->num_in = in_n;
  if (out_n == 0) {
    node->out = ((void *)0);
  } else {
    node->out = malloc(out_n*sizeof(node_id_t));
  }
  if (in_n == 0) {
    node->in = ((void *)0);
  } else {
    node->in = malloc(in_n*sizeof(node_id_t));
  }
}

canonical_original_code:
void node_init(struct _node *node, unsigned int id, unsigned long out_n, unsigned long in_n) {
  node->id = id;
  node->num_out = out_n;
  node->num_in = in_n;
  if (out_n == 0) {
    node->out = ((void *)0);
  } else {
    node->out = malloc(out_n*sizeof(node_id_t));
  }
  if (in_n == 0) {
    node->in = ((void *)0);
  } else {
    node->in = malloc(in_n*sizeof(node_id_t));
  }
}

variable_types:
id
unsigned int


decompiled_code:
void *__fastcall node_new(int a1, __int64 a2, __int64 a3)
{
void *v4; // r12

v4 = malloc(0x28uLL);
node_init((__int64)v4, a1, a2, a3);
return v4;
}

canonical_decompiled_code:
void *__fastcall func1(int a1, __int64 a2, __int64 a3)
{
void *v4; // r12

v4 = malloc(0x28uLL);
func0((__int64)v4, a1, a2, a3);
return v4;
}

original_code:
node_t * node_new (node_id_t id, size_t out_n, size_t in_n) {
  node_t *n = malloc(sizeof(node_t));
  node_init(n, id, out_n, in_n);
  return n;
}

canonical_original_code:
struct _node *node_new(unsigned int id, unsigned long out_n, unsigned long in_n) {
  struct _node *n = malloc(sizeof(node_t));
  node_init(n, id, out_n, in_n);
  return n;
}


decompiled_code:
void __fastcall node_free(__int64 a1)
{
free(*(void **)(a1 + 24));
free(*(void **)(a1 + 32));
}

original_code:
void node_free (node_t *node) {
  free(node->out);
  free(node->in);
}

canonical_original_code:
void node_free(struct _node *node) {
  free(node->out);
  free(node->in);
}


decompiled_code:
void __fastcall node_delete(void *ptr)
{
node_free((__int64)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
func2((__int64)ptr);
free(ptr);
}

original_code:
void node_delete (node_t *node) {
  node_free(node);
  free(node);
}

canonical_original_code:
void node_delete(struct _node *node) {
  node_free(node);
  free(node);
}


repo: Celelibi/Image

decompiled_code:
__m128i __fastcall C_new(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

canonical_decompiled_code:
__m128i __fastcall func3(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

original_code:
Color C_new(float red, float green, float blue)
{
 Color c;
 c._red = red;
 c._green = green;
 c._blue = blue;
 return c;
}

canonical_original_code:
struct Color C_new(float red, float green, float blue)
{
 struct Color c;
 c._red = red;
 c._green = green;
 c._blue = blue;
 return c;
}

variable_types: red float

variable_types: green float

variable_types: blue float

variable_types:
c
struct Color { float _red; }

return_type: struct Color { float _red; }


decompiled_code:
int __fastcall C_check(const char *a1, double a2, float a3)
{
return fprintf(stderr, "%s : %f %f %f\n", a1, *(float *)&a2, *((float *)&a2 + 1), a3);
}

canonical_decompiled_code:
int __fastcall func4(const char *a1, double a2, float a3)
{
return fprintf(stderr, "%s : %f %f %f\n", a1, *(float *)&a2, *((float *)&a2 + 1), a3);
}

original_code:
void C_check(Color c, char *message)
{
 fprintf(stderr, "%s : %f %f %f\n", message, c._red, c._green, c._blue);
}

canonical_original_code:
void C_check(struct Color c, char *message)
{
 fprintf(stderr, "%s : %f %f %f\n", message, c._red, c._green, c._blue);
}

variable_types:
message
char *


decompiled_code:
double *__fastcall I_fill(int *a1, double a2, float a3)
{
double *result; // rax
int v5; // er10
__int64 v6; // r8
__int64 v7; // rdi
__int64 v8; // r11
double *v9; // rsi

result = (double *)*a1;
if ( (int)result > 0 )
{
v5 = a1[1];
v6 = 8LL * (_QWORD)result;
v7 = 0LL;
result = (double *)(unsigned int)(v5 - 1);
v8 = 4 * (3LL * (_QWORD)result + 3);
do
{
if ( v5 > 0 )
{
result = *(double **)(*((_QWORD *)a1 + 6) + v7);
v9 = (double *)((char *)result + v8);
do
{
*result = a2;
result = (double *)((char *)result + 12);
*((float *)result - 1) = a3;
}
while ( v9 != result );
}
v7 += 8LL;
}
while ( v7 != v6 );
}
return result;
}

canonical_decompiled_code:
double *__fastcall func6(int *a1, double a2, float a3)
{
double *result; // rax
int v5; // er10
__int64 v6; // r8
__int64 v7; // rdi
__int64 v8; // r11
double *v9; // rsi

result = (double *)*a1;
if ( (int)result > 0 )
{
v5 = a1[1];
v6 = 8LL * (_QWORD)result;
v7 = 0LL;
result = (double *)(unsigned int)(v5 - 1);
v8 = 4 * (3LL * (_QWORD)result + 3);
do
{
if ( v5 > 0 )
{
result = *(double **)(*((_QWORD *)a1 + 6) + v7);
v9 = (double *)((char *)result + v8);
do
{
*result = a2;
result = (double *)((char *)result + 12);
*((float *)result - 1) = a3;
}
while ( v9 != result );
}
v7 += 8LL;
}
while ( v7 != v6 );
}
return result;
}

original_code:
void I_fill(Image *img, Color c)
{
 int x, y;
 for (x = 0; x < img->_width; x++)
 {
  for (y = 0; y < img->_height; y++)
   img->_buffer[x][y] = c;
 }
}

canonical_original_code:
void I_fill(struct Image *img, struct Color c)
{
 int x; int y;
 for (x = 0; x < img->_width; x++)
 {
  for (y = 0; y < img->_height; y++)
   img->_buffer[x][y] = c;
 }
}

variable_types: struct Image { int _width; int _xzoom; int _xoffset; double _zoom; struct Color { float _red; } _current_color; struct Color **_buffer; } *

variable_types:
c
struct Color

variable_types: x int

variable_types: y int

user_defined_types: struct Image { int _width; int _xzoom; int _xoffset; double _zoom; struct Color _current_color; struct Color **_buffer; }


decompiled_code:
__int64 __fastcall I_checker(__int64 a1, int a2, double a3, float a4)
{
int v4; // edx
int v7; // er11
__int64 result; // rax
int v9; // er8
int v10; // er10
double v11; // [rsp+0h] [rbp-10h]
float v12; // [rsp+8h] [rbp-8h]

v4 = *(_DWORD *)a1;
v11 = a3;
v7 = 0;
v12 = a4;
if ( v4 > 0 )
{
do
{
result = *(unsigned int *)(a1 + 4);
if ( (int)result > 0 )
{
v9 = 0;
v10 = v7 / a2;
do
{
while ( 1 )
{
result = (unsigned int)(v10 + v9 / a2);
if ( (((_BYTE)v10 + (unsigned __int8)(v9 / a2)) & 1) == 0 )
break;
if ( *(_DWORD *)(a1 + 4) <= ++v9 )
goto LABEL_7;
}
result = plot_isra_0(*(_QWORD *)(a1 + 48), v7, v9, v11, v12);
}
while ( *(_DWORD *)(a1 + 4) > v9 );
}
LABEL_7:
++v7;
}
while ( *(_DWORD *)a1 > v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2, double a3, float a4)
{
int v4; // edx
int v7; // er11
__int64 result; // rax
int v9; // er8
int v10; // er10
double v11; // [rsp+0h] [rbp-10h]
float v12; // [rsp+8h] [rbp-8h]

v4 = *(_DWORD *)a1;
v11 = a3;
v7 = 0;
v12 = a4;
if ( v4 > 0 )
{
do
{
result = *(unsigned int *)(a1 + 4);
if ( (int)result > 0 )
{
v9 = 0;
v10 = v7 / a2;
do
{
while ( 1 )
{
result = (unsigned int)(v10 + v9 / a2);
if ( (((_BYTE)v10 + (unsigned __int8)(v9 / a2)) & 1) == 0 )
break;
if ( *(_DWORD *)(a1 + 4) <= ++v9 )
goto LABEL_7;
}
result = func2(*(_QWORD *)(a1 + 48), v7, v9, v11, v12);
}
while ( *(_DWORD *)(a1 + 4) > v9 );
}
LABEL_7:
++v7;
}
while ( *(_DWORD *)a1 > v7 );
}
return result;
}

original_code:
void I_checker(Image *img, Color c, int step)
{
 int x, y;
 for (x = 0; x < img->_width; x++)
 {
  for (y = 0; y < img->_height; y++)
  {
   int n_x = x/step;
   int n_y = y/step;
   if ((n_x + n_y) % 2 == 0)
    _plot(img, x, y, c);
  }
 }
}

canonical_original_code:
void I_checker(struct Image *img, struct Color c, int step)
{
 int x; int y;
 for (x = 0; x < img->_width; x++)
 {
  for (y = 0; y < img->_height; y++)
  {
   int n_x = x/step;
   int n_y = y/step;
   if ((n_x + n_y) % 2 == 0)
    _plot(img, x, y, c);
  }
 }
}

variable_types: step int


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

result = LODWORD(a3);
*(double *)(a1 + 32) = a2;
*(float *)(a1 + 40) = a3;
return result;
}

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

result = LODWORD(a3);
*(double *)(a1 + 32) = a2;
*(float *)(a1 + 40) = a3;
return result;
}

original_code:
void I_changeColor(Image *img, Color c)
{
 img->_current_color = c;
}

canonical_original_code:
void I_changeColor(struct Image *img, struct Color c)
{
 img->_current_color = c;
}


decompiled_code:
int __fastcall I_plot(unsigned int *a1, int a2, int a3)
{
__int64 v4; // rax

if ( a2 < 0 || (int)*a1 <= a2 || a3 < 0 || (int)a1[1] <= a3 )
{
fwrite("I_plot : ERROR !!!\n", 1uLL, 0x13uLL, stderr);
fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", (unsigned int)a2, 0LL, *a1);
LODWORD(v4) = fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", (unsigned int)a3, 0LL, a1[1]);
}
else
{
v4 = *(_QWORD *)(*((_QWORD *)a1 + 6) + 8LL * a2) + 12LL * a3;
*(_QWORD *)v4 = *((_QWORD *)a1 + 4);
*(_DWORD *)(v4 + 8) = a1[10];
}
return v4;
}

canonical_decompiled_code:
int __fastcall func9(unsigned int *a1, int a2, int a3)
{
__int64 v4; // rax

if ( a2 < 0 || (int)*a1 <= a2 || a3 < 0 || (int)a1[1] <= a3 )
{
fwrite("I_plot : ERROR !!!\n", 1uLL, 0x13uLL, stderr);
fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", (unsigned int)a2, 0LL, *a1);
LODWORD(v4) = fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", (unsigned int)a3, 0LL, a1[1]);
}
else
{
v4 = *(_QWORD *)(*((_QWORD *)a1 + 6) + 8LL * a2) + 12LL * a3;
*(_QWORD *)v4 = *((_QWORD *)a1 + 4);
*(_DWORD *)(v4 + 8) = a1[10];
}
return v4;
}

original_code:
void I_plot(Image *img, int x, int y)
{
 if ((x >= 0) && (x < img->_width) &&
    (y >= 0) && (y < img->_height))
  img->_buffer[x][y] = img->_current_color;
 else
 {
  fprintf(stderr, "I_plot : ERROR !!!\n");
  fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", x, 0, img->_width);
  fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", y, 0, img->_height);
 }
}

canonical_original_code:
void I_plot(struct Image *img, int x, int y)
{
 if ((x >= 0) && (x < img->_width) &&
    (y >= 0) && (y < img->_height))
  img->_buffer[x][y] = img->_current_color;
 else
 {
  fprintf(stderr, "I_plot : ERROR !!!\n");
  fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", x, 0, img->_width);
  fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", y, 0, img->_height);
 }
}


decompiled_code:
int __fastcall I_plotColor(unsigned int *a1, int a2, int a3, double a4, float a5)
{
int result; // eax
__int64 v7; // rdx

if ( a2 < 0 || (int)*a1 <= a2 || a3 < 0 || (int)a1[1] <= a3 )
{
fwrite("I_plotColor : ERROR !!!\n", 1uLL, 0x18uLL, stderr);
fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", (unsigned int)a2, 0LL, *a1);
result = fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", (unsigned int)a3, 0LL, a1[1]);
}
else
{
result = LODWORD(a5);
v7 = *(_QWORD *)(*((_QWORD *)a1 + 6) + 8LL * a2) + 12LL * a3;
*(float *)(v7 + 8) = a5;
*(double *)v7 = a4;
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int *a1, int a2, int a3, double a4, float a5)
{
int result; // eax
__int64 v7; // rdx

if ( a2 < 0 || (int)*a1 <= a2 || a3 < 0 || (int)a1[1] <= a3 )
{
fwrite("I_plotColor : ERROR !!!\n", 1uLL, 0x18uLL, stderr);
fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", (unsigned int)a2, 0LL, *a1);
result = fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", (unsigned int)a3, 0LL, a1[1]);
}
else
{
result = LODWORD(a5);
v7 = *(_QWORD *)(*((_QWORD *)a1 + 6) + 8LL * a2) + 12LL * a3;
*(float *)(v7 + 8) = a5;
*(double *)v7 = a4;
}
return result;
}

original_code:
void I_plotColor(Image *img, int x, int y, Color c)
{
 if ((x >= 0) && (x < img->_width) &&
    (y >= 0) && (y < img->_height))
  img->_buffer[x][y] = c;
 else
 {
  fprintf(stderr, "I_plotColor : ERROR !!!\n");
  fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", x, 0, img->_width);
  fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", y, 0, img->_height);
 }
}

canonical_original_code:
void I_plotColor(struct Image *img, int x, int y, struct Color c)
{
 if ((x >= 0) && (x < img->_width) &&
    (y >= 0) && (y < img->_height))
  img->_buffer[x][y] = c;
 else
 {
  fprintf(stderr, "I_plotColor : ERROR !!!\n");
  fprintf(stderr, "x (=%d) must be in the [%d, %d] range and\n", x, 0, img->_width);
  fprintf(stderr, "y (=%d) must be in the [%d, %d] range\n", y, 0, img->_height);
 }
}


original_code:
void I_focusPoint(Image *img, int xwin, int ywin)
{
 int dx = xwin - img->_xzoom;
 int dy = ywin - img->_yzoom;
 img->_xoffset -= dx*(1-1.0/img->_zoom);
 img->_yoffset -= dy*(1-1.0/img->_zoom);
 img->_xzoom = xwin;
 img->_yzoom = ywin;
}

canonical_original_code:
void I_focusPoint(struct Image *img, int xwin, int ywin)
{
 int dx = xwin - img->_xzoom;
 int dy = ywin - img->_yzoom;
 img->_xoffset -= dx*(1-1.0/img->_zoom);
 img->_yoffset -= dy*(1-1.0/img->_zoom);
 img->_xzoom = xwin;
 img->_yzoom = ywin;
}

variable_types: xwin int

variable_types: ywin int

variable_types: dx int

variable_types: dy int


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

result = LC2;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = LC2;
return result;
}

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

result = LC2;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = LC2;
return result;
}

original_code:
void I_zoomInit(Image *img)
{
 img->_xoffset = 0;
 img->_yoffset = 0;
 img->_zoom = 1.0;
}

canonical_original_code:
void I_zoomInit(struct Image *img)
{
 img->_xoffset = 0;
 img->_yoffset = 0;
 img->_zoom = 1.0;
}


decompiled_code:
void __fastcall I_zoom(__int64 a1, double a2)
{
*(double *)(a1 + 24) = a2 * *(double *)(a1 + 24);
}

canonical_decompiled_code:
void __fastcall func13(__int64 a1, double a2)
{
*(double *)(a1 + 24) = a2 * *(double *)(a1 + 24);
}

original_code:
void I_zoom(Image *img, double zoom_coef)
{
 img->_zoom = img->_zoom * zoom_coef;
}

canonical_original_code:
void I_zoom(struct Image *img, double zoom_coef)
{
 img->_zoom = img->_zoom * zoom_coef;
}


original_code:
void I_move(Image *img, int x, int y)
{
 img->_xoffset += x;
 img->_yoffset += y;
}

canonical_original_code:
void I_move(struct Image *img, int x, int y)
{
 img->_xoffset += x;
 img->_yoffset += y;
}


original_code:
void I_draw(Image *img)
{
 glBegin(0x0000);
 int xwin, ywin, ximg, yimg;
 for (xwin = 0; xwin < img->_width; xwin++)
 {
  for (ywin = 0; ywin < img->_height; ywin++)
  {
   _windowToImage(img, xwin, ywin, &ximg, &yimg);
   Color c;
   if ((ximg >= 0) && (ximg < img->_width) &&
      (yimg >= 0) && (yimg < img->_height))
    c = img->_buffer[ximg][yimg];
   else
    c = C_new(0, 0, 0);
   glColor3f(c._red, c._green, c._blue);
   glVertex2i(xwin, ywin);
  }
 }
 glEnd();
}

canonical_original_code:
void I_draw(struct Image *img)
{
 glBegin(0x0000);
 int xwin; int ywin; int ximg; int yimg;
 for (xwin = 0; xwin < img->_width; xwin++)
 {
  for (ywin = 0; ywin < img->_height; ywin++)
  {
   _windowToImage(img, xwin, ywin, &ximg, &yimg);
   struct Color c;
   if ((ximg >= 0) && (ximg < img->_width) &&
      (yimg >= 0) && (yimg < img->_height))
    c = img->_buffer[ximg][yimg];
   else
    c = C_new(0, 0, 0);
   glColor3f(c._red, c._green, c._blue);
   glVertex2i(xwin, ywin);
  }
 }
 glEnd();
}

variable_types: ximg int

variable_types: yimg int


decompiled_code:
__int64 display_CB()
{
glClear(0x4000LL);
drawing_rasterize(&drawing, img, (unsigned int)current_mode);
I_draw(img);
return glutSwapBuffers();
}

canonical_decompiled_code:
__int64 func0()
{
glClear(0x4000LL);
drawing_rasterize(&drawing, img, (unsigned int)current_mode);
I_draw(img);
return glutSwapBuffers();
}

original_code:
void display_CB()
{
 glClear(0x00004000);
 drawing_rasterize(&drawing, img, current_mode);
 I_draw(img);
 glutSwapBuffers();
}


decompiled_code:
int __fastcall mouse_CB(int a1, int a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // er12
__int64 v7; // rdi
__int64 v8; // rsi
int v9; // eax

v5 = *(_DWORD *)(img + 4) - a4;
if ( a2 | a1 )
{
if ( a1 != 2 || a2 )
{
if ( a1 == 1 && !a2 && current_mode == 2 )
{
v7 = drawing[1];
if ( v7 )
{
v8 = drawing[2];
if ( v8 )
{
if ( *(_QWORD *)(v8 + 8) )
polygon_insert_vertex_after(v7, v8, a3, v5);
}
}
}
}
else
{
I_focusPoint(img, a3, a4);
}
return glutPostRedisplay();
}
printf(format, a3, v5);
v9 = current_mode;
if ( current_mode )
{
LABEL_18:
if ( v9 == 1 )
{
drawing[2] = closestVertex(drawing[1], a3, v5);
v9 = current_mode;
}
if ( v9 == 2 )
drawing[2] = closestEdge(drawing[1], a3, v5);
return glutPostRedisplay();
}
if ( !drawing[1] )
drawing_new_polygon(drawing);
if ( !(unsigned int)polygon_is_closed() )
{
printf("--> ajout du sommet %d, %d\n", a3, v5);
polygon_append_vertex(drawing[1], a3, v5);
v9 = current_mode;
goto LABEL_18;
}
return puts(aLePolygoneEstF);
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // er12
__int64 v7; // rdi
__int64 v8; // rsi
int v9; // eax

v5 = *(_DWORD *)(img + 4) - a4;
if ( a2 | a1 )
{
if ( a1 != 2 || a2 )
{
if ( a1 == 1 && !a2 && current_mode == 2 )
{
v7 = drawing[1];
if ( v7 )
{
v8 = drawing[2];
if ( v8 )
{
if ( *(_QWORD *)(v8 + 8) )
polygon_insert_vertex_after(v7, v8, a3, v5);
}
}
}
}
else
{
I_focusPoint(img, a3, a4);
}
return glutPostRedisplay();
}
printf(format, a3, v5);
v9 = current_mode;
if ( current_mode )
{
LABEL_18:
if ( v9 == 1 )
{
drawing[2] = closestVertex(drawing[1], a3, v5);
v9 = current_mode;
}
if ( v9 == 2 )
drawing[2] = closestEdge(drawing[1], a3, v5);
return glutPostRedisplay();
}
if ( !drawing[1] )
drawing_new_polygon(drawing);
if ( !(unsigned int)polygon_is_closed() )
{
printf("--> ajout du sommet %d, %d\n", a3, v5);
polygon_append_vertex(drawing[1], a3, v5);
v9 = current_mode;
goto LABEL_18;
}
return puts(aLePolygoneEstF);
}

original_code:
void mouse_CB(int button, int state, int x, int y)
{
 y = img->_height - y;
 if(button == 0x0000 && state == 0x0000)
 {
  printf("--> clic détecté à %d, %d\n", x, y);
  if(current_mode == APPEND)
  {
   if (drawing.p_active == ((void *)0))
    drawing_new_polygon(&drawing);
   if(polygon_is_closed(drawing.p_active))
   {
    printf("--> le polygone est fermé donc je ne fais rien\n");
    return;
   }
   printf("--> ajout du sommet %d, %d\n", x, y);
   polygon_append_vertex(drawing.p_active, x, y);
  }
  if(current_mode == VERTEX)
   drawing.v_selected = closestVertex(drawing.p_active, x, y);
  if(current_mode == EDGE)
   drawing.v_selected = closestEdge(drawing.p_active, x, y);
 }
 else if(button == 0x0002 && state == 0x0000)
 {
  I_focusPoint(img,x,img->_height-y);
 }
 else if(button == 0x0001 && state == 0x0000)
 {
  if(current_mode == EDGE && drawing.p_active != ((void *)0) && drawing.v_selected != ((void *)0) && drawing.v_selected->next != ((void *)0))
   polygon_insert_vertex_after(drawing.p_active, drawing.v_selected, x, y);
 }
 glutPostRedisplay();
}

variable_types: button int

variable_types: state int


decompiled_code:
__int64 __fastcall keyboard_CB(unsigned __int8 a1)
{
_QWORD *v2; // rax
_QWORD *v3; // rdi
int v4; // eax

if ( a1 == 27 )
{
glutLeaveMainLoop();
}
else
{
switch ( a1 )
{
case 'Z':
I_zoom(img, 0.5);
break;
case 'a':
current_mode = 0;
break;
case 'c':
if ( (unsigned int)polygon_toggle_close(drawing[1]) )
puts(s);
else
puts(aPolygoneR);
break;
case 'e':
v2 = (_QWORD *)drawing[1];
current_mode = 2;
if ( v2 )
goto LABEL_10;
break;
case 'f':
if ( (unsigned int)polygon_is_closed(drawing[1]) )
*(_DWORD *)(drawing[1] + 24LL) ^= 1u;
else
puts(aLePolygoneNEst);
break;
case 'i':
I_zoomInit(img);
break;
case 'v':
v2 = (_QWORD *)drawing[1];
current_mode = 1;
if ( v2 )
LABEL_10:
drawing[2] = *v2;
break;
case 'z':
I_zoom(img, 2.0);
break;
case '\x7F':
v3 = (_QWORD *)drawing[1];
if ( v3 && *v3 )
{
v4 = current_mode;
if ( !current_mode )
{
polygon_remove_vertex(v3, v3[1]);
v4 = current_mode;
}
if ( v4 == 1 )
{
polygon_remove_vertex(drawing[1], drawing[2]);
drawing[2] = *(_QWORD *)drawing[1];
}
}
break;
default:
fprintf(stderr, "keyboard_CB : %d : unknown key.\n", a1);
break;
}
}
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
_QWORD *v2; // rax
_QWORD *v3; // rdi
int v4; // eax

if ( a1 == 27 )
{
glutLeaveMainLoop();
}
else
{
switch ( a1 )
{
case 'Z':
I_zoom(img, 0.5);
break;
case 'a':
current_mode = 0;
break;
case 'c':
if ( (unsigned int)polygon_toggle_close(drawing[1]) )
puts(s);
else
puts(aPolygoneR);
break;
case 'e':
v2 = (_QWORD *)drawing[1];
current_mode = 2;
if ( v2 )
goto LABEL_10;
break;
case 'f':
if ( (unsigned int)polygon_is_closed(drawing[1]) )
*(_DWORD *)(drawing[1] + 24LL) ^= 1u;
else
puts(aLePolygoneNEst);
break;
case 'i':
I_zoomInit(img);
break;
case 'v':
v2 = (_QWORD *)drawing[1];
current_mode = 1;
if ( v2 )
LABEL_10:
drawing[2] = *v2;
break;
case 'z':
I_zoom(img, 2.0);
break;
case '\x7F':
v3 = (_QWORD *)drawing[1];
if ( v3 && *v3 )
{
v4 = current_mode;
if ( !current_mode )
{
polygon_remove_vertex(v3, v3[1]);
v4 = current_mode;
}
if ( v4 == 1 )
{
polygon_remove_vertex(drawing[1], drawing[2]);
drawing[2] = *(_QWORD *)drawing[1];
}
}
break;
default:
fprintf(stderr, "keyboard_CB : %d : unknown key.\n", a1);
break;
}
}
return glutPostRedisplay();
}

original_code:
void keyboard_CB(unsigned char key, int x, int y)
{
 (void)x;
 (void)y;
 switch(key)
 {
 case 27:
  glutLeaveMainLoop();
  break;
 case 127:
  if(drawing.p_active != ((void *)0) && drawing.p_active->v_list != ((void *)0))
  {
   if(current_mode == APPEND)
    polygon_remove_vertex(drawing.p_active, drawing.p_active->v_last);
   if(current_mode == VERTEX)
   {
    polygon_remove_vertex(drawing.p_active, drawing.v_selected);
    drawing.v_selected = drawing.p_active->v_list;
   }
  }
  break;
 case 'z':
  I_zoom(img, 2.0);
  break;
 case 'Z':
  I_zoom(img, 0.5);
  break;
 case 'i':
  I_zoomInit(img);
  break;
 case 'a':
  current_mode = APPEND;
  break;
 case 'v':
  current_mode = VERTEX;
  if(drawing.p_active != ((void *)0))
   drawing.v_selected = drawing.p_active->v_list;
  break;
 case 'e':
  current_mode = EDGE;
  if(drawing.p_active != ((void *)0))
   drawing.v_selected = drawing.p_active->v_list;
  break;
 case 'c':
  if (polygon_toggle_close(drawing.p_active))
   printf("Polygone fermé\n");
  else
   printf("Polygone réouvert\n");
  break;
 case 'f':
  if (!polygon_is_closed(drawing.p_active))
   printf("Le polygone n'est pas fermé\n");
  else
   drawing.p_active->is_filled ^= 1;
  break;
 default:
  fprintf(stderr,"keyboard_CB : %d : unknown key.\n",key);
 }
 glutPostRedisplay();
}

variable_types:
key
unsigned char


decompiled_code:
__int64 __fastcall special_CB(unsigned int a1)
{
__int64 v2; // rax
int v3; // eax
_DWORD *v4; // rax
int v5; // eax
_DWORD *v6; // rax
int v7; // eax
__int64 v8; // rax
int v9; // eax
__int64 v10; // rax
bool v11; // zf
int v12; // eax

switch ( a1 )
{
case 'd':
v3 = current_mode;
if ( !current_mode )
{
I_move(img, 10LL, 0LL);
v3 = current_mode;
}
if ( v3 == 1 )
{
v4 = (_DWORD *)drawing[2];
if ( v4 )
--*v4;
}
break;
case 'e':
v9 = current_mode;
if ( !current_mode )
{
I_move(img, 0LL, 10LL);
v9 = current_mode;
}
if ( v9 == 1 )
{
v10 = drawing[2];
if ( v10 )
++*(_DWORD *)(v10 + 4);
}
break;
case 'f':
v5 = current_mode;
if ( !current_mode )
{
I_move(img, 4294967286LL, 0LL);
v5 = current_mode;
}
if ( v5 == 1 )
{
v6 = (_DWORD *)drawing[2];
if ( v6 )
++*v6;
}
break;
case 'g':
v7 = current_mode;
if ( !current_mode )
{
I_move(img, 0LL, 4294967286LL);
v7 = current_mode;
}
if ( v7 == 1 )
{
v8 = drawing[2];
if ( v8 )
--*(_DWORD *)(v8 + 4);
}
break;
case 'h':
if ( current_mode == 1 )
{
v2 = *(_QWORD *)(drawing[2] + 8LL);
if ( v2 )
goto LABEL_7;
}
else if ( current_mode == 2 )
{
v11 = (unsigned int)polygon_is_closed(drawing[1]) == 0;
v12 = current_mode;
if ( !v11 )
drawing[2] = *(_QWORD *)(drawing[2] + 8LL);
if ( v12 == 2 && !(unsigned int)polygon_is_closed(drawing[1]) )
{
v2 = *(_QWORD *)(drawing[2] + 8LL);
if ( v2 != *(_QWORD *)(drawing[1] + 8LL) )
goto LABEL_7;
}
}
break;
case 'i':
if ( (unsigned int)(current_mode - 1) <= 1 )
{
v2 = *(_QWORD *)(drawing[2] + 16LL);
if ( v2 )
LABEL_7:
drawing[2] = v2;
}
break;
default:
fprintf(stderr, "special_CB : %d : unknown key.\n", a1);
break;
}
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
__int64 v2; // rax
int v3; // eax
_DWORD *v4; // rax
int v5; // eax
_DWORD *v6; // rax
int v7; // eax
__int64 v8; // rax
int v9; // eax
__int64 v10; // rax
bool v11; // zf
int v12; // eax

switch ( a1 )
{
case 'd':
v3 = current_mode;
if ( !current_mode )
{
I_move(img, 10LL, 0LL);
v3 = current_mode;
}
if ( v3 == 1 )
{
v4 = (_DWORD *)drawing[2];
if ( v4 )
--*v4;
}
break;
case 'e':
v9 = current_mode;
if ( !current_mode )
{
I_move(img, 0LL, 10LL);
v9 = current_mode;
}
if ( v9 == 1 )
{
v10 = drawing[2];
if ( v10 )
++*(_DWORD *)(v10 + 4);
}
break;
case 'f':
v5 = current_mode;
if ( !current_mode )
{
I_move(img, 4294967286LL, 0LL);
v5 = current_mode;
}
if ( v5 == 1 )
{
v6 = (_DWORD *)drawing[2];
if ( v6 )
++*v6;
}
break;
case 'g':
v7 = current_mode;
if ( !current_mode )
{
I_move(img, 0LL, 4294967286LL);
v7 = current_mode;
}
if ( v7 == 1 )
{
v8 = drawing[2];
if ( v8 )
--*(_DWORD *)(v8 + 4);
}
break;
case 'h':
if ( current_mode == 1 )
{
v2 = *(_QWORD *)(drawing[2] + 8LL);
if ( v2 )
goto LABEL_7;
}
else if ( current_mode == 2 )
{
v11 = (unsigned int)polygon_is_closed(drawing[1]) == 0;
v12 = current_mode;
if ( !v11 )
drawing[2] = *(_QWORD *)(drawing[2] + 8LL);
if ( v12 == 2 && !(unsigned int)polygon_is_closed(drawing[1]) )
{
v2 = *(_QWORD *)(drawing[2] + 8LL);
if ( v2 != *(_QWORD *)(drawing[1] + 8LL) )
goto LABEL_7;
}
}
break;
case 'i':
if ( (unsigned int)(current_mode - 1) <= 1 )
{
v2 = *(_QWORD *)(drawing[2] + 16LL);
if ( v2 )
LABEL_7:
drawing[2] = v2;
}
break;
default:
fprintf(stderr, "special_CB : %d : unknown key.\n", a1);
break;
}
return glutPostRedisplay();
}

original_code:
void special_CB(int key, int x, int y)
{
 (void)x;
 (void)y;
 int d = 10;
 switch(key)
 {
 case 0x0065:
  if(current_mode == APPEND)
   I_move(img,0,d);
  if(current_mode == VERTEX && drawing.v_selected != ((void *)0))
   (drawing.v_selected->y)++;
  break;
 case 0x0067:
  if(current_mode == APPEND)
   I_move(img,0,-d);
  if(current_mode == VERTEX && drawing.v_selected != ((void *)0))
   (drawing.v_selected->y)--;
  break;
 case 0x0064:
  if(current_mode == APPEND)
   I_move(img,d,0);
  if(current_mode == VERTEX && drawing.v_selected != ((void *)0))
   (drawing.v_selected->x)--;
  break;
 case 0x0066:
  if(current_mode == APPEND)
   I_move(img,-d,0);
  if(current_mode == VERTEX && drawing.v_selected != ((void *)0))
   (drawing.v_selected->x)++;
  break;
 case 0x0068:
  if((current_mode == VERTEX && drawing.v_selected->next != ((void *)0)) ||
    (current_mode == EDGE && polygon_is_closed(drawing.p_active)))
   drawing.v_selected = drawing.v_selected->next;
  if(current_mode == EDGE && !polygon_is_closed(drawing.p_active) &&
    drawing.v_selected->next != drawing.p_active->v_last)
   drawing.v_selected = drawing.v_selected->next;
  break;
 case 0x0069:
  if((current_mode == VERTEX || current_mode == EDGE) && drawing.v_selected->prev != ((void *)0))
   drawing.v_selected = drawing.v_selected->prev;
  break;
 default:
  fprintf(stderr,"special_CB : %d : unknown key.\n",key);
 }
 glutPostRedisplay();
}

variable_types: key int

variable_types: d int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rdi
unsigned int v5; // ebp
__int64 v6; // r12
unsigned int *v8; // rax
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v9[0] = argc;
if ( (unsigned int)(argc - 2) > 1 )
{
fprintf(stderr, "\n\nUsage \t: %s <width> <height>\nou", *argv);
fprintf(stderr, "\t: %s <ppmfilename> \n\n", *argv);
exit(1);
}
v4 = argv[1];
if ( argc == 2 )
{
v8 = (unsigned int *)I_read(v4);
v5 = *v8;
LODWORD(v6) = v8[1];
img = (__int64)v8;
}
else
{
v5 = atoi(v4);
v6 = (unsigned int)atoi(argv[2]);
img = I_new(v5, v6);
C_new(0.0, 0.0);
I_fill(img);
}
glutInitWindowSize(v5, (unsigned int)v6);
glutInitWindowPosition(100LL, 100LL);
glutInitDisplayMode(2LL);
glutInit(v9, argv);
glutCreateWindow(*argv);
glViewport(0LL, 0LL, v5, (unsigned int)v6);
glMatrixMode(5889LL);
glLoadIdentity();
glMatrixMode(5888LL);
glLoadIdentity();
glOrtho(0.0, (double)(int)v5, 0.0, (double)(int)v6, -1.0, 1.0);
glutDisplayFunc(display_CB);
glutKeyboardFunc(keyboard_CB);
glutSpecialFunc(special_CB);
glutMouseFunc(mouse_CB);
glutMainLoop();
drawing_free(drawing);
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int largeur, hauteur;
 int windowPosX = 100, windowPosY = 100;
 if (argc != 3 && argc != 2)
 {
  fprintf(stderr,"\n\nUsage \t: %s <width> <height>\nou",argv[0]);
  fprintf(stderr,"\t: %s <ppmfilename> \n\n",argv[0]);
  exit(1);
 }
 if (argc == 2)
 {
  img = I_read(argv[1]);
  largeur = img->_width;
  hauteur = img->_height;
 }
 else
 {
  largeur = atoi(argv[1]);
  hauteur = atoi(argv[2]);
  img = I_new(largeur,hauteur);
  Color black = C_new(0,0,0);
  I_fill(img, black);
 }
 glutInitWindowSize(largeur,hauteur);
 glutInitWindowPosition(windowPosX,windowPosY);
 glutInitDisplayMode(0x0000 | 0x0002 );
 glutInit(&argc, argv);
 glutCreateWindow(argv[0]);
 glViewport(0, 0, largeur, hauteur);
 glMatrixMode(0x1701);
 glLoadIdentity();
 glMatrixMode(0x1700);
 glLoadIdentity();
 glOrtho(0,largeur,0,hauteur,-1,1);
 glutDisplayFunc(display_CB);
 glutKeyboardFunc(keyboard_CB);
 glutSpecialFunc(special_CB);
 glutMouseFunc(mouse_CB);
 glutMainLoop();
 drawing_free(&drawing);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int largeur; int hauteur;
 int windowPosX = 100; int windowPosY = 100;
 if (argc != 3 && argc != 2)
 {
  fprintf(stderr,"\n\nUsage \t: %s <width> <height>\nou",argv[0]);
  fprintf(stderr,"\t: %s <ppmfilename> \n\n",argv[0]);
  exit(1);
 }
 if (argc == 2)
 {
  img = I_read(argv[1]);
  largeur = img->_width;
  hauteur = img->_height;
 }
 else
 {
  largeur = atoi(argv[1]);
  hauteur = atoi(argv[2]);
  img = I_new(largeur,hauteur);
  struct Color black = C_new(0,0,0);
  I_fill(img, black);
 }
 glutInitWindowSize(largeur,hauteur);
 glutInitWindowPosition(windowPosX,windowPosY);
 glutInitDisplayMode(0x0000 | 0x0002 );
 glutInit(&argc, argv);
 glutCreateWindow(argv[0]);
 glViewport(0, 0, largeur, hauteur);
 glMatrixMode(0x1701);
 glLoadIdentity();
 glMatrixMode(0x1700);
 glLoadIdentity();
 glOrtho(0,largeur,0,hauteur,-1,1);
 glutDisplayFunc(display_CB);
 glutKeyboardFunc(keyboard_CB);
 glutSpecialFunc(special_CB);
 glutMouseFunc(mouse_CB);
 glutMainLoop();
 drawing_free(&drawing);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: largeur int

variable_types: hauteur int

variable_types: windowPosX int

variable_types: windowPosY int

variable_types:
black
struct Color { float _red; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
__int64 v4; // rcx
_DWORD *v5; // rsi
_DWORD *v6; // rdi
__int64 v7; // rcx
_DWORD *v8; // rsi
_DWORD *v9; // rdi
_BYTE v10[48]; // [rsp-30h] [rbp-78h] BYREF
_DWORD v11[18]; // [rsp+0h] [rbp-48h] BYREF

if ( argc == 1 )
{
puts("Too few arguments, use -h to see how to use this program.");
result = -1;
}
else
{
if ( (unsigned int)init_opt(v11, argv, envp) == -1
|| (unsigned int)handle_options(v11, (unsigned int)argc, argv) == -1 )
{
goto LABEL_3;
}
if ( v11[4] )
{
v7 = 10LL;
v8 = v11;
v9 = v10;
while ( v7 )
{
*v9++ = *v8++;
--v7;
}
result = encode(v9, v8);
}
else
{
v4 = 10LL;
v5 = v11;
v6 = v10;
while ( v4 )
{
*v6++ = *v5++;
--v4;
}
result = decode(v6, v5);
}
if ( result == -1 )
{
LABEL_3:
puts("Some error occours, the software'll be terminated.");
result = -1;
}
}
return result;
}

original_code:
int main (int argc, char* argv[])
{
 struct options opt;
 int ret=0;
 if(argc == 1){
  printf("Too few arguments, use -h to see how to use this program.\n");
  return -1;
 }
 ret = init_opt(&opt);
 if( ret != -1){
  ret = handle_options(&opt,argc,argv);
  if( ret != -1){
   if (opt.mode){
    ret = encode(opt);
   }
   else{
    ret = decode(opt);
   }
  }
 }
 if(ret == -1)
  printf("Some error occours, the software'll be terminated.\n");
 return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct options opt;
 int ret = 0;
 if(argc == 1){
  printf("Too few arguments, use -h to see how to use this program.\n");
  return -1;
 }
 ret = init_opt(&opt);
 if( ret != -1){
  ret = handle_options(&opt,argc,argv);
  if( ret != -1){
   if (opt.mode){
    ret = encode(opt);
   }
   else{
    ret = decode(opt);
   }
  }
 }
 if(ret == -1)
  printf("Some error occours, the software'll be terminated.\n");
 return ret;
}

variable_types:
argv
char *[]

variable_types: struct options { char *file_in; char *file_out; int mode; int verbose; int window_len; int look_ahead_len; char *dict; }

variable_types: ret int


usage

decompiled_code:
void __noreturn usage()
{
puts("LZ77 Help");
puts("Usage: ./lz77 [options]\n");
puts("options ");
puts(" -c\tSet compression mode");
puts(" -d\tSet decompression mode");
puts(
" -i FILE\n"
"\tFilename input (source).\n"
"\tIn compression mode if is omitted the software use the standard input,\n"
"\tinstead in decompression mode you must specify it.");
puts(
" -o FILE\n"
"\tFile name output (destination).\n"
"\tIn decompression mode if is omitted the software use the standard output,\n"
"\tinstead in compression mode you must specify it.");
puts(
" -t DICTIONARY\n"
"\tSpecify which dictionary you want to use\n"
"\tThe default one is 'it'. The name must be of 2 characters.");
puts(" -l VALUE\n\tSet look-ahead length, must specify a positive value.\n\tMin value is 8 and the max value is 255");
puts(
" -w VALUE\n"
"\tSet window length, must specify a positive value.\n"
"\tMin value must be equal to look ahead length the max value is 32767.");
puts(" -v\tSet verbose mode");
puts("\nEXAMPLES");
puts(" Using files ./lz77 -c -o compress_file -i original_file -w 1024 -l 16 -t it");
puts(" Using STDIN ./lz77 -c -o compress_file -w 1024 -l 16 -t it");
puts(" Using STDOUT ./lz77 -i compress_file -w 1024 -l 16 -t it");
puts("\nNOTE");
puts(" If you're using the STDIN typing the text manually use Ctrl+D to do EOF.");
puts("\nDEFAULT VALUES");
puts(" Window length : 2048 bytes");
puts(" Look ahead length : 64 bytes");
puts(" Dictionary : it\n");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
puts("LZ77 Help");
puts("Usage: ./lz77 [options]\n");
puts("options ");
puts(" -c\tSet compression mode");
puts(" -d\tSet decompression mode");
puts(
" -i FILE\n"
"\tFilename input (source).\n"
"\tIn compression mode if is omitted the software use the standard input,\n"
"\tinstead in decompression mode you must specify it.");
puts(
" -o FILE\n"
"\tFile name output (destination).\n"
"\tIn decompression mode if is omitted the software use the standard output,\n"
"\tinstead in compression mode you must specify it.");
puts(
" -t DICTIONARY\n"
"\tSpecify which dictionary you want to use\n"
"\tThe default one is 'it'. The name must be of 2 characters.");
puts(" -l VALUE\n\tSet look-ahead length, must specify a positive value.\n\tMin value is 8 and the max value is 255");
puts(
" -w VALUE\n"
"\tSet window length, must specify a positive value.\n"
"\tMin value must be equal to look ahead length the max value is 32767.");
puts(" -v\tSet verbose mode");
puts("\nEXAMPLES");
puts(" Using files ./lz77 -c -o compress_file -i original_file -w 1024 -l 16 -t it");
puts(" Using STDIN ./lz77 -c -o compress_file -w 1024 -l 16 -t it");
puts(" Using STDOUT ./lz77 -i compress_file -w 1024 -l 16 -t it");
puts("\nNOTE");
puts(" If you're using the STDIN typing the text manually use Ctrl+D to do EOF.");
puts("\nDEFAULT VALUES");
puts(" Window length : 2048 bytes");
puts(" Look ahead length : 64 bytes");
puts(" Dictionary : it\n");
exit(0);
}

original_code:
void usage()
{
 printf("LZ77 Help\n");
 printf("Usage: ./lz77 [options]\n\n");
 printf("options \n");
 printf("  -c\tSet compression mode\n");
 printf("  -d\tSet decompression mode\n");
 printf("  -i FILE\n\tFilename input (source).\n\tIn compression mode if is omitted the software use the standard input,\n\tinstead in decompression mode you must specify it.\n");
 printf("  -o FILE\n\tFile name output (destination).\n\tIn decompression mode if is omitted the software use the standard output,\n\tinstead in compression mode you must specify it.\n");
 printf("  -t DICTIONARY\n\tSpecify which dictionary you want to use\n\tThe default one is 'it'. The name must be of 2 characters.\n");
 printf("  -l VALUE\n\tSet look-ahead length, must specify a positive value.\n\tMin value is 8 and the max value is 255\n");
 printf("  -w VALUE\n\tSet window length, must specify a positive value.\n\tMin value must be equal to look ahead length the max value is 32767.\n");
 printf("  -v\tSet verbose mode\n");
 printf("\nEXAMPLES\n");
 printf("  Using files  ./lz77 -c -o compress_file  -i original_file -w 1024 -l 16 -t it\n");
 printf("  Using STDIN  ./lz77 -c -o compress_file -w 1024 -l 16 -t it\n");
 printf("  Using STDOUT ./lz77 -i compress_file -w 1024 -l 16 -t it\n");
 printf("\nNOTE\n");
 printf("  If you're using the STDIN typing the text manually use Ctrl+D to do EOF.\n");
 printf("\nDEFAULT VALUES\n");
 printf("  Window length : 2048 bytes\n");
 printf("  Look ahead length : 64 bytes\n");
 printf("  Dictionary : it\n\n");
 exit(0);
}


original_code:
int init_opt(struct options *opt)
{
 bzero(opt, sizeof(struct options));
 if(opt == ((void *)0)){
  (*__errno_location ()) = 22;
  return -1;
 }
 opt->mode = 2;
 opt->window_len = 1024;
 opt->look_ahead_len = 64;
 opt->verbose = 0;
 opt->file_in = ((void *)0);
 opt->file_out = ((void *)0);
 opt->dict = calloc(2, sizeof(char));
 if(opt->dict == ((void *)0))
  return -1;
 strncpy(opt->dict,"it",2);
 return 0;
}

variable_types: struct options { char *file_in; char *file_out; int mode; int verbose; int window_len; int look_ahead_len; char *dict; } *


decompiled_code:
int __fastcall print_options(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, const char *a7, const char *a8, __int64 a9, __int64 a10, const char *a11)
{
if ( HIDWORD(a9) )
{
if ( (_DWORD)a9 )
{
puts("Mode : Compression");
printf("Dictionary : %s\n", a11);
}
else
{
puts("Mode : Decompression");
}
puts("Verbose : ON");
printf("Window size : %d\n", (unsigned int)a10);
printf("Look ahead buffer size : %d\n", HIDWORD(a10));
}
if ( a7 )
printf("File Input : %s\n", a7);
else
puts("Input : Standard Input");
if ( a8 )
printf("File Output : %s\n", a8);
else
puts("Output : Standard Output");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, const char *a7, const char *a8, __int64 a9, __int64 a10, const char *a11)
{
if ( HIDWORD(a9) )
{
if ( (_DWORD)a9 )
{
puts("Mode : Compression");
printf("Dictionary : %s\n", a11);
}
else
{
puts("Mode : Decompression");
}
puts("Verbose : ON");
printf("Window size : %d\n", (unsigned int)a10);
printf("Look ahead buffer size : %d\n", HIDWORD(a10));
}
if ( a7 )
printf("File Input : %s\n", a7);
else
puts("Input : Standard Input");
if ( a8 )
printf("File Output : %s\n", a8);
else
puts("Output : Standard Output");
return putchar(10);
}

original_code:
void print_options(struct options opt){
 if(opt.verbose){
  if(opt.mode){
   printf("Mode : Compression\n");
   printf("Dictionary : %s\n",opt.dict);
  }
  else
   printf("Mode : Decompression\n");
  if(opt.verbose)
   printf("Verbose : ON\n");
  printf("Window size : %d\n",opt.window_len);
  printf("Look ahead buffer size : %d\n",opt.look_ahead_len);
 }
 if(opt.file_in == ((void *)0))
  printf("Input : Standard Input\n");
 else
  printf("File Input : %s\n",opt.file_in);
 if(opt.file_out == ((void *)0))
  printf("Output : Standard Output\n");
 else
  printf("File Output : %s\n",opt.file_out);
 printf("\n");
}

user_defined_types: struct options { char *file_in; char *file_out; int mode; int verbose; int window_len; int look_ahead_len; char *dict; }


overwrite

decompiled_code:
__int64 __fastcall overwrite(char *name)
{
unsigned __int8 v1; // al

LABEL_4:
if ( !access(name, 4) )
{
printf("\n%s already exist, do you want overwrite it? [y/n] : ", name);
while ( 1 )
{
do
v1 = getchar();
while ( v1 == 10 );
if ( v1 == 121 )
break;
if ( v1 == 110 )
{
printf("Insert the new output filename : ");
if ( (unsigned int)_isoc99_scanf("%s", name) == 1 )
goto LABEL_4;
return 0xFFFFFFFFLL;
}
printf("\n%c isn't supported, please type 'y' or 'n' : ", v1);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *name)
{
unsigned __int8 v1; // al

LABEL_4:
if ( !access(name, 4) )
{
printf("\n%s already exist, do you want overwrite it? [y/n] : ", name);
while ( 1 )
{
do
v1 = getchar();
while ( v1 == 10 );
if ( v1 == 121 )
break;
if ( v1 == 110 )
{
printf("Insert the new output filename : ");
if ( (unsigned int)_isoc99_scanf("%s", name) == 1 )
goto LABEL_4;
return 0xFFFFFFFFLL;
}
printf("\n%c isn't supported, please type 'y' or 'n' : ", v1);
}
}
return 0LL;
}

original_code:
int overwrite(char* filename){
 unsigned char answer;
 int ret ;
 for(;;){
  if(access(filename, 4)==0){
   printf("\n%s already exist, do you want overwrite it? [y/n] : ",filename);
   for(;;){
    answer = getchar();
    if(answer == '\n')
     continue;
    if(answer!='y' && answer!= 'n'){
     printf("\n%c isn't supported, please type 'y' or 'n' : ",answer);
     continue;
    }
    if(answer == 'n')
     break;
    else
     return 0;
   }
  }else{
   break;
  }
  printf("Insert the new output filename : ");
  ret = scanf("%s",filename);
  if( ret !=1 ){
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int overwrite(char *filename){
 unsigned char answer;
 int ret;
 for(;;){
  if(access(filename, 4)==0){
   printf("\n%s already exist, do you want overwrite it? [y/n] : ",filename);
   for(;;){
    answer = getchar();
    if(answer == '\n')
     continue;
    if(answer!='y' && answer!= 'n'){
     printf("\n%c isn't supported, please type 'y' or 'n' : ",answer);
     continue;
    }
    if(answer == 'n')
     break;
    else
     return 0;
   }
  }else{
   break;
  }
  printf("Insert the new output filename : ");
  ret = scanf("%s",filename);
  if( ret !=1 ){
   return -1;
  }
 }
 return 0;
}

variable_types:
filename
char *

variable_types:
answer
unsigned char


decompiled_code:
__int64 __fastcall handle_options(__int64 a1, int a2, char *const *a3)
{
char v4; // al
int v5; // eax
unsigned int v6; // er13
char *v7; // r13
char *v8; // r13
size_t v9; // rax
char *v10; // rax
int v11; // eax
char *v12; // r13
size_t v13; // rax
char *v14; // rax
__int64 result; // rax
int v16; // eax

while ( 1 )
{
v4 = getopt(a2, a3, "hvcdi:o:w:l:t:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case '?':
if ( ((*_ctype_b_loc())[optopt] & 0x4000) != 0 )
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown character `\\x%x'.\n", optopt);
goto LABEL_21;
case 'c':
*(_DWORD *)(a1 + 16) = 1;
continue;
case 'd':
*(_DWORD *)(a1 + 16) = 0;
continue;
case 'h':
usage();
return result;
case 'i':
v6 = file_check(optarg, 4);
if ( v6 == -1 )
{
printf("ERROR!!! The input file %s not exists or you haven't the read permission\n", optarg);
return v6;
}
v12 = optarg;
v13 = strlen(optarg);
v14 = (char *)malloc(v13);
*(_QWORD *)a1 = v14;
strcpy(v14, v12);
continue;
case 'l':
v11 = atoi(optarg);
*(_DWORD *)(a1 + 28) = v11;
if ( v11 <= 7 )
{
v6 = -1;
puts("Error : look ahead parameter must be greater or equal to 8");
return v6;
}
if ( v11 > 255 )
{
v6 = -1;
puts("Error : look ahead parameter must be almost 255");
return v6;
}
continue;
case 'o':
v6 = file_check(optarg, 2);
if ( v6 == -1 )
{
printf("ERROR!!! The output file %s not exist or you haven't write permission on it.\n", optarg);
return v6;
}
v8 = optarg;
v9 = strlen(optarg);
v10 = (char *)malloc(v9);
*(_QWORD *)(a1 + 8) = v10;
strcpy(v10, v8);
continue;
case 't':
v7 = optarg;
if ( strlen(optarg) != 2 )
{
v6 = -1;
puts("Error : The dictionary will be only of 2 characters");
return v6;
}
v6 = file_check(v7, 4);
if ( v6 == -1 )
{
printf("Error : The dictionary %s doesnt exists or you haven't the read permission\n", optarg);
return v6;
}
strcpy(*(char **)(a1 + 32), optarg);
break;
case 'v':
*(_DWORD *)(a1 + 20) = 1;
continue;
case 'w':
v5 = atoi(optarg);
*(_DWORD *)(a1 + 24) = v5;
if ( v5 < 0 )
{
v6 = -1;
puts("Error : window parameter must be positive");
return v6;
}
if ( v5 > 0x7FFF )
{
v6 = -1;
puts("Error : window parameter must be at least 32767");
return v6;
}
continue;
default:
continue;
}
}
v16 = *(_DWORD *)(a1 + 16);
if ( v16 == 2 )
{
puts("Missing argument -c or -d");
LABEL_21:
v6 = -1;
puts("Use -h to see hot to use this program.");
return v6;
}
if ( !*(_QWORD *)a1 && !v16 )
{
v6 = -1;
puts("In decompression mode the file input can't be the standard input.");
return v6;
}
if ( !*(_QWORD *)(a1 + 8) && v16 == 1 )
{
v6 = -1;
puts("In compression mode the file output can't be the standard output");
return v6;
}
if ( *(_DWORD *)(a1 + 24) < *(_DWORD *)(a1 + 28) )
{
v6 = -1;
puts("Error : window length must be grater or equal to the look ahead length.");
return v6;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, char *const *a3)
{
char v4; // al
int v5; // eax
unsigned int v6; // er13
char *v7; // r13
char *v8; // r13
size_t v9; // rax
char *v10; // rax
int v11; // eax
char *v12; // r13
size_t v13; // rax
char *v14; // rax
__int64 result; // rax
int v16; // eax

while ( 1 )
{
v4 = getopt(a2, a3, "hvcdi:o:w:l:t:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case '?':
if ( ((*_ctype_b_loc())[optopt] & 0x4000) != 0 )
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown character `\\x%x'.\n", optopt);
goto LABEL_21;
case 'c':
*(_DWORD *)(a1 + 16) = 1;
continue;
case 'd':
*(_DWORD *)(a1 + 16) = 0;
continue;
case 'h':
func0();
return result;
case 'i':
v6 = func4(optarg, 4);
if ( v6 == -1 )
{
printf("ERROR!!! The input file %s not exists or you haven't the read permission\n", optarg);
return v6;
}
v12 = optarg;
v13 = strlen(optarg);
v14 = (char *)malloc(v13);
*(_QWORD *)a1 = v14;
strcpy(v14, v12);
continue;
case 'l':
v11 = atoi(optarg);
*(_DWORD *)(a1 + 28) = v11;
if ( v11 <= 7 )
{
v6 = -1;
puts("Error : look ahead parameter must be greater or equal to 8");
return v6;
}
if ( v11 > 255 )
{
v6 = -1;
puts("Error : look ahead parameter must be almost 255");
return v6;
}
continue;
case 'o':
v6 = func4(optarg, 2);
if ( v6 == -1 )
{
printf("ERROR!!! The output file %s not exist or you haven't write permission on it.\n", optarg);
return v6;
}
v8 = optarg;
v9 = strlen(optarg);
v10 = (char *)malloc(v9);
*(_QWORD *)(a1 + 8) = v10;
strcpy(v10, v8);
continue;
case 't':
v7 = optarg;
if ( strlen(optarg) != 2 )
{
v6 = -1;
puts("Error : The dictionary will be only of 2 characters");
return v6;
}
v6 = func4(v7, 4);
if ( v6 == -1 )
{
printf("Error : The dictionary %s doesnt exists or you haven't the read permission\n", optarg);
return v6;
}
strcpy(*(char **)(a1 + 32), optarg);
break;
case 'v':
*(_DWORD *)(a1 + 20) = 1;
continue;
case 'w':
v5 = atoi(optarg);
*(_DWORD *)(a1 + 24) = v5;
if ( v5 < 0 )
{
v6 = -1;
puts("Error : window parameter must be positive");
return v6;
}
if ( v5 > 0x7FFF )
{
v6 = -1;
puts("Error : window parameter must be at least 32767");
return v6;
}
continue;
default:
continue;
}
}
v16 = *(_DWORD *)(a1 + 16);
if ( v16 == 2 )
{
puts("Missing argument -c or -d");
LABEL_21:
v6 = -1;
puts("Use -h to see hot to use this program.");
return v6;
}
if ( !*(_QWORD *)a1 && !v16 )
{
v6 = -1;
puts("In decompression mode the file input can't be the standard input.");
return v6;
}
if ( !*(_QWORD *)(a1 + 8) && v16 == 1 )
{
v6 = -1;
puts("In compression mode the file output can't be the standard output");
return v6;
}
if ( *(_DWORD *)(a1 + 24) < *(_DWORD *)(a1 + 28) )
{
v6 = -1;
puts("Error : window length must be grater or equal to the look ahead length.");
return v6;
}
return 0LL;
}

original_code:
int handle_options(struct options *opt, int argc, char* argv[])
{
 char c;
 while ((c = getopt (argc, argv, "hvcdi:o:w:l:t:")) != -1){
   switch(c) {
    case 'c':
    opt->mode = 1;
    break;
   case 'd':
    opt->mode = 0;
    break;
             case 'i':
                  if(file_check(optarg,0|4) == -1){
     printf("ERROR!!! The input file %s not exists or you haven't the read permission\n",optarg);
                       return -1;
    }
    opt->file_in = malloc( strlen( optarg ) );
                  strcpy( opt->file_in, optarg );
                  break;
   case 'o':
    if(file_check(optarg,2) == -1){
     printf("ERROR!!! The output file %s not exist or you haven't write permission on it.\n",optarg);
     return -1;
                  }
    opt->file_out = malloc( strlen( optarg ) );
    strcpy( opt->file_out, optarg );
    break;
   case 'v':
    opt->verbose = 1;
    break;
              case 't':
                  if ( strlen(optarg) != 2){
                       printf("Error : The dictionary will be only of 2 characters\n");
                   return -1;
                  }
                  if(file_check(optarg,0|4) == -1){
     printf("Error : The dictionary %s doesnt exists or you haven't the read permission\n",optarg);
                   return -1;
    }
                  strcpy( opt->dict, optarg );
                  break;
   case 'w':
    opt->window_len = atoi(optarg);
    if (opt->window_len < 0){
            printf("Error : window parameter must be positive\n");
                   return -1;
                  }
    if (opt->window_len >= 32768){
            printf("Error : window parameter must be at least 32767\n");
                   return -1;
                  }
    break;
   case 'l':
    opt->look_ahead_len = atoi(optarg);
    if (opt->look_ahead_len < 8){
            printf("Error : look ahead parameter must be greater or equal to 8\n");
     return -1;
       }
    if(opt->look_ahead_len > 255){
            printf("Error : look ahead parameter must be almost 255\n");
     return -1;
    }
    break;
   case 'h':
    usage();
    break;
   case '?':
         if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint))
             fprintf (stderr, "Unknown option `-%c'.\n", optopt);
           else
                       fprintf (stderr, "Unknown character `\\x%x'.\n", optopt);
                  printf("Use -h to see hot to use this program.\n");
                  return -1;
   }
  }
 if( opt->mode == 2){
  printf("Missing argument -c or -d\n");
  printf("Use -h to see hot to use this program.\n");
  return -1;
        }
 if(( opt->file_in == ((void *)0)) & (opt->mode == 0)){
  printf("In decompression mode the file input can't be the standard input.\n");
  return -1;
 }
 if(( opt->file_out == ((void *)0)) & (opt->mode == 1)){
  printf("In compression mode the file output can't be the standard output\n");
  return -1;
 }
 if( opt->window_len < opt->look_ahead_len ){
  printf("Error : window length must be grater or equal to the look ahead length.\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int handle_options(struct options *opt, int argc, char *argv[])
{
 char c;
 while ((c = getopt (argc, argv, "hvcdi:o:w:l:t:")) != -1){
   switch(c) {
    case 'c':
    opt->mode = 1;
    break;
   case 'd':
    opt->mode = 0;
    break;
             case 'i':
                  if(file_check(optarg,0|4) == -1){
     printf("ERROR!!! The input file %s not exists or you haven't the read permission\n",optarg);
                       return -1;
    }
    opt->file_in = malloc( strlen( optarg ) );
                  strcpy( opt->file_in, optarg );
                  break;
   case 'o':
    if(file_check(optarg,2) == -1){
     printf("ERROR!!! The output file %s not exist or you haven't write permission on it.\n",optarg);
     return -1;
                  }
    opt->file_out = malloc( strlen( optarg ) );
    strcpy( opt->file_out, optarg );
    break;
   case 'v':
    opt->verbose = 1;
    break;
              case 't':
                  if ( strlen(optarg) != 2){
                       printf("Error : The dictionary will be only of 2 characters\n");
                   return -1;
                  }
                  if(file_check(optarg,0|4) == -1){
     printf("Error : The dictionary %s doesnt exists or you haven't the read permission\n",optarg);
                   return -1;
    }
                  strcpy( opt->dict, optarg );
                  break;
   case 'w':
    opt->window_len = atoi(optarg);
    if (opt->window_len < 0){
            printf("Error : window parameter must be positive\n");
                   return -1;
                  }
    if (opt->window_len >= 32768){
            printf("Error : window parameter must be at least 32767\n");
                   return -1;
                  }
    break;
   case 'l':
    opt->look_ahead_len = atoi(optarg);
    if (opt->look_ahead_len < 8){
            printf("Error : look ahead parameter must be greater or equal to 8\n");
     return -1;
       }
    if(opt->look_ahead_len > 255){
            printf("Error : look ahead parameter must be almost 255\n");
     return -1;
    }
    break;
   case 'h':
    usage();
    break;
   case '?':
         if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint))
             fprintf (stderr, "Unknown option `-%c'.\n", optopt);
           else
                       fprintf (stderr, "Unknown character `\\x%x'.\n", optopt);
                  printf("Use -h to see hot to use this program.\n");
                  return -1;
   }
  }
 if( opt->mode == 2){
  printf("Missing argument -c or -d\n");
  printf("Use -h to see hot to use this program.\n");
  return -1;
        }
 if(( opt->file_in == ((void *)0)) & (opt->mode == 0)){
  printf("In decompression mode the file input can't be the standard input.\n");
  return -1;
 }
 if(( opt->file_out == ((void *)0)) & (opt->mode == 1)){
  printf("In compression mode the file output can't be the standard output\n");
  return -1;
 }
 if( opt->window_len < opt->look_ahead_len ){
  printf("Error : window length must be grater or equal to the look ahead length.\n");
  return -1;
 }
 return 0;
}

variable_types: c char


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

if ( a2 == a3 )
result = a1;
else
result = _bswap_32(a1);
return result;
}

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

if ( a2 == a3 )
result = a1;
else
result = func0(a1);
return result;
}

original_code:
int convert_data(int data, uint8_t current_order, uint8_t file_order){
 if( current_order != file_order ){
  if(current_order)
   return htonl(data);
  else
   return ntohl(data);
 }
 return data;
}

canonical_original_code:
int convert_data(int data, unsigned char current_order, unsigned char file_order){
 if( current_order != file_order ){
  if(current_order)
   return htonl(data);
  else
   return ntohl(data);
 }
 return data;
}

variable_types: data int


wrap

decompiled_code:
__int64 __fastcall wrap(signed int a1, signed int a2, int a3)
{
__int64 result; // rax

result = (unsigned int)a1;
if ( a1 >= a2 )
result = (unsigned int)(a1 % a2 + a3);
return result;
}

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

result = (unsigned int)a1;
if ( a1 >= a2 )
result = (unsigned int)(a1 % a2 + a3);
return result;
}

original_code:
int wrap(int pos, int limit, int offset)
{
  if ( pos < limit )
   return pos;
  return ( pos % limit ) + offset;
}

variable_types: pos int

variable_types: limit int

variable_types: offset int


decompiled_code:
__int64 __fastcall number_of_bits(int a1)
{
if ( a1 >= 0 )
return (unsigned int)(int)(log((double)a1) / 0.6931471805599453 + 1.0);
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
if ( a1 >= 0 )
return (unsigned int)(int)(log((double)a1) / 0.6931471805599453 + 1.0);
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}

original_code:
int number_of_bits(int x){
 if(x<0){
  (*__errno_location ()) = 22;
  return -1;
 }
 return (int)(log(x)/log(2) +1);
}

variable_types: x int


original_code:
int bit_read(struct bitfile *fd,char* buf, int n_bits, int ofs){
 uint8_t w_mask, mask;
 int ret, bits_read;
 char *p = buf;
 if( (fd == ((void *)0) ) || (buf == ((void *)0)) || (ofs < 0) || (ofs > 7) || ( fd->mode != 0 ) || ( n_bits < 0) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 w_mask=1<<ofs;
 mask = 1<<fd->ofs;
 bits_read = 0;
 while(n_bits > 0){
  if(fd->n_bits == 0)
  {
   fd->ofs = 0;
   fd->w_inizio = 0;
   ret = read(fd->fd, fd->buf ,fd->bufsize/8);
   mask = 1;
   if(ret == -1 || ret == 0)
    return ret;
   fd->n_bits = ret * 8;
  }
  if(fd->buf[fd->w_inizio] & mask ){
   *p |= w_mask;
  }else{
   *p &= ~w_mask;
  }
  if(w_mask == 0x80){
   p++;
   w_mask = 1;
  }else{
   w_mask <<= 1;
  }
  if(mask == 0x80){
   fd->w_inizio++;
   mask = 1;
  }else{
   mask <<= 1;
  }
  fd->ofs = (fd->ofs+1)%8;
  fd->n_bits--;
  n_bits--;
  bits_read++;
 }
 return bits_read;
}

canonical_original_code:
int bit_read(struct bitfile *fd, char *buf, int n_bits, int ofs){
 unsigned char w_mask; unsigned char mask;
 int ret; int bits_read;
 char *p = buf;
 if( (fd == ((void *)0) ) || (buf == ((void *)0)) || (ofs < 0) || (ofs > 7) || ( fd->mode != 0 ) || ( n_bits < 0) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 w_mask=1<<ofs;
 mask = 1<<fd->ofs;
 bits_read = 0;
 while(n_bits > 0){
  if(fd->n_bits == 0)
  {
   fd->ofs = 0;
   fd->w_inizio = 0;
   ret = read(fd->fd, fd->buf ,fd->bufsize/8);
   mask = 1;
   if(ret == -1 || ret == 0)
    return ret;
   fd->n_bits = ret * 8;
  }
  if(fd->buf[fd->w_inizio] & mask ){
   *p |= w_mask;
  }else{
   *p &= ~w_mask;
  }
  if(w_mask == 0x80){
   p++;
   w_mask = 1;
  }else{
   w_mask <<= 1;
  }
  if(mask == 0x80){
   fd->w_inizio++;
   mask = 1;
  }else{
   mask <<= 1;
  }
  fd->ofs = (fd->ofs+1)%8;
  fd->n_bits--;
  n_bits--;
  bits_read++;
 }
 return bits_read;
}

variable_types: struct bitfile { int fd; int mode; int bufsize; int w_inizio; int ofs; int n_bits; char buf[]; } *

variable_types:
buf
char *

variable_types: ofs int

variable_types:
mask
unsigned char

variable_types:
p
char *


decompiled_code:
ssize_t __fastcall bit_flush(__int64 a1)
{
int v2; // ebp
ssize_t result; // rax
int v4; // edi
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( !a1 || *(_DWORD *)(a1 + 4) != 1 )
{
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}
v2 = *(_DWORD *)(a1 + 20);
if ( !(v2 / 8) )
{
if ( (v2 & 7) == 0 )
{
LABEL_5:
*(_QWORD *)(a1 + 12) = 0LL;
result = 0LL;
*(_DWORD *)(a1 + 20) = 0;
return result;
}
LABEL_9:
v4 = *(_DWORD *)a1;
buf[0] = *(_BYTE *)(a1 + *(int *)(a1 + 12) + 24) & ((1 << (v2 % 8)) - 1);
write(v4, buf, 1uLL);
goto LABEL_5;
}
result = write(*(_DWORD *)a1, (const void *)(a1 + 24), v2 / 8);
if ( (_DWORD)result != -1 )
{
if ( (v2 & 7) == 0 )
goto LABEL_5;
goto LABEL_9;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(__int64 a1)
{
int v2; // ebp
ssize_t result; // rax
int v4; // edi
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( !a1 || *(_DWORD *)(a1 + 4) != 1 )
{
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}
v2 = *(_DWORD *)(a1 + 20);
if ( !(v2 / 8) )
{
if ( (v2 & 7) == 0 )
{
LABEL_5:
*(_QWORD *)(a1 + 12) = 0LL;
result = 0LL;
*(_DWORD *)(a1 + 20) = 0;
return result;
}
LABEL_9:
v4 = *(_DWORD *)a1;
buf[0] = *(_BYTE *)(a1 + *(int *)(a1 + 12) + 24) & ((1 << (v2 % 8)) - 1);
write(v4, buf, 1uLL);
goto LABEL_5;
}
result = write(*(_DWORD *)a1, (const void *)(a1 + 24), v2 / 8);
if ( (_DWORD)result != -1 )
{
if ( (v2 & 7) == 0 )
goto LABEL_5;
goto LABEL_9;
}
return result;
}

original_code:
int bit_flush(struct bitfile *fp){
 int ret, bytes_left,bits_left;
 if ( (fp == ((void *)0)) || ( fp->mode != 1 ) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 bytes_left = fp->n_bits/8;
 bits_left = fp->n_bits%8;
 if(bytes_left){
  ret = write(fp->fd, fp->buf, bytes_left);
  if(ret == -1){
   return -1;
  }
 }
 if(bits_left)
 {
  int mask;
  char last_data;
  mask = (1<<bits_left)-1;
  last_data = fp->buf[fp->w_inizio] & mask;
  ret = write(fp->fd, &last_data, 1);
 }
 fp->ofs = 0;
 fp->w_inizio = 0;
 fp->n_bits = 0;
 return 0;
}

canonical_original_code:
int bit_flush(struct bitfile *fp){
 int ret; int bytes_left; int bits_left;
 if ( (fp == ((void *)0)) || ( fp->mode != 1 ) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 bytes_left = fp->n_bits/8;
 bits_left = fp->n_bits%8;
 if(bytes_left){
  ret = write(fp->fd, fp->buf, bytes_left);
  if(ret == -1){
   return -1;
  }
 }
 if(bits_left)
 {
  int mask;
  char last_data;
  mask = (1<<bits_left)-1;
  last_data = fp->buf[fp->w_inizio] & mask;
  ret = write(fp->fd, &last_data, 1);
 }
 fp->ofs = 0;
 fp->w_inizio = 0;
 fp->n_bits = 0;
 return 0;
}

variable_types: mask int

user_defined_types: struct bitfile { int fd; int mode; int bufsize; int w_inizio; int ofs; int n_bits; char buf[]; }


original_code:
int bit_write( struct bitfile *fd, const char *buf, int n_bits, int ofs)
{
 uint8_t w_mask;
 uint8_t mask;
 unsigned int bits_write = 0;
        const char *p;
 if( (fd == ((void *)0) ) || (buf == ((void *)0)) || (ofs < 0) || (ofs > 7) || ( fd->mode != 1 ) || ( n_bits < 0) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 p = buf;
        w_mask=1<<ofs;
 mask = 1<<fd->ofs;
       while(n_bits > 0){
  if( fd->n_bits == fd->bufsize )
   bit_flush(fd);
  if(*p & w_mask ){
   fd->buf[fd->w_inizio] |= mask;
  }else{
   fd->buf[fd->w_inizio] &= ~mask;
  }
  if(w_mask == 0x80){
   p++;
   w_mask = 1;
  }else{
   w_mask <<= 1;
  }
  if(mask == 0x80){
   fd->w_inizio++;
   mask = 1;
  }else{
   mask <<= 1;
  }
  n_bits--;
  bits_write++;
  fd->ofs = (fd->ofs+1)%8;
  fd->n_bits++;
   }
 return bits_write;
}

canonical_original_code:
int bit_write(struct bitfile *fd, char *buf, int n_bits, int ofs)
{
 unsigned char w_mask;
 unsigned char mask;
 unsigned int bits_write = 0;
        char *p;
 if( (fd == ((void *)0) ) || (buf == ((void *)0)) || (ofs < 0) || (ofs > 7) || ( fd->mode != 1 ) || ( n_bits < 0) ){
  (*__errno_location ()) = 22;
  return -1;
 }
 p = buf;
        w_mask=1<<ofs;
 mask = 1<<fd->ofs;
       while(n_bits > 0){
  if( fd->n_bits == fd->bufsize )
   bit_flush(fd);
  if(*p & w_mask ){
   fd->buf[fd->w_inizio] |= mask;
  }else{
   fd->buf[fd->w_inizio] &= ~mask;
  }
  if(w_mask == 0x80){
   p++;
   w_mask = 1;
  }else{
   w_mask <<= 1;
  }
  if(mask == 0x80){
   fd->w_inizio++;
   mask = 1;
  }else{
   mask <<= 1;
  }
  n_bits--;
  bits_write++;
  fd->ofs = (fd->ofs+1)%8;
  fd->n_bits++;
   }
 return bits_write;
}


decompiled_code:
__int64 __fastcall bit_close(void *ptr)
{
__int64 result; // rax

if ( ptr )
{
if ( *((_DWORD *)ptr + 1) == 1 )
bit_flush((__int64)ptr);
close(*(_DWORD *)ptr);
free(ptr);
result = 0LL;
}
else
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *ptr)
{
__int64 result; // rax

if ( ptr )
{
if ( *((_DWORD *)ptr + 1) == 1 )
func1((__int64)ptr);
close(*(_DWORD *)ptr);
free(ptr);
result = 0LL;
}
else
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int bit_close(struct bitfile * fp){
 if(fp == ((void *)0)){
  (*__errno_location ()) = 22;
  return -1;
 }
 if(fp->mode == 1 )
  bit_flush(fp);
 close(fp->fd);
 free(fp);
 return 0;
}

canonical_original_code:
int bit_close(struct bitfile *fp){
 if(fp == ((void *)0)){
  (*__errno_location ()) = 22;
  return -1;
 }
 if(fp->mode == 1 )
  bit_flush(fp);
 close(fp->fd);
 free(fp);
 return 0;
}


decompiled_code:
void *__fastcall build_tree(int a1)
{
if ( a1 > 0 )
return calloc(a1 + 1, 0x10uLL);
*_errno_location() = 22;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
if ( a1 > 0 )
return calloc(a1 + 1, 0x10uLL);
*_errno_location() = 22;
return 0LL;
}

original_code:
struct Node* build_tree(int length)
{
 if(length <= 0){
  (*__errno_location ()) = 22;
  return ((void *)0);
 }
 return calloc( (length + 1), sizeof(struct Node) );
}

canonical_original_code:
struct Node *build_tree(int length)
{
 if(length <= 0){
  (*__errno_location ()) = 22;
  return ((void *)0);
 }
 return calloc( (length + 1), sizeof(struct Node) );
}

variable_types: length int

return_type: struct Node { int position; int smaller; int greater; int father; } *


decompiled_code:
__int64 __fastcall find_max_son(__int64 a1, int a2)
{
unsigned int v2; // eax
unsigned int v3; // er8

v2 = *(_DWORD *)(a1 + 16LL * a2 + 4);
do
{
v3 = v2;
v2 = *(_DWORD *)(a1 + 16LL * (int)v2 + 8);
}
while ( v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
unsigned int v2; // eax
unsigned int v3; // er8

v2 = *(_DWORD *)(a1 + 16LL * a2 + 4);
do
{
v3 = v2;
v2 = *(_DWORD *)(a1 + 16LL * (int)v2 + 8);
}
while ( v2 );
return v3;
}

original_code:
int find_max_son(struct Node *tree, int node){
 int greatest = tree[node].smaller;
 while ( tree[greatest].greater != 0)
  greatest = tree[greatest].greater;
 return greatest;
}

variable_types: struct Node { int position; int smaller; int greater; int father; } *

variable_types: node int

variable_types: greatest int


original_code:
void empty_tree(struct Node *tree, int n_nodes){
 int i;
 tree[0].greater = 0;
 for(i = 0; i<=n_nodes; i++){
  tree[i].father = 0;
  tree[i].smaller = 0;
  tree[i].greater = 0;
  tree[i].position = 0;
 }
}

variable_types: i int

user_defined_types: struct Node { int position; int smaller; int greater; int father; }


decompiled_code:
__int64 __fastcall window_cmp(unsigned __int16 *a1, int a2, int a3)
{
int v4; // er12
__int64 v5; // rbp
__int64 result; // rax

if ( !*((_BYTE *)a1 + 2) )
return 0LL;
v4 = 0;
while ( 1 )
{
v5 = (int)wrap((unsigned int)(a2 + v4), *((_DWORD *)a1 + 2) + (unsigned int)*a1);
result = *(unsigned __int8 *)(*((_QWORD *)a1 + 2) + v5)
- (unsigned int)*(unsigned __int8 *)(*((_QWORD *)a1 + 2)
+ (int)wrap(
(unsigned int)(a3 + v4),
*((_DWORD *)a1 + 2) + (unsigned int)*a1));
if ( (_DWORD)result )
break;
if ( *((unsigned __int8 *)a1 + 2) <= ++v4 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 *a1, int a2, int a3)
{
int v4; // er12
__int64 v5; // rbp
__int64 result; // rax

if ( !*((_BYTE *)a1 + 2) )
return 0LL;
v4 = 0;
while ( 1 )
{
v5 = (int)wrap((unsigned int)(a2 + v4), *((_DWORD *)a1 + 2) + (unsigned int)*a1);
result = *(unsigned __int8 *)(*((_QWORD *)a1 + 2) + v5)
- (unsigned int)*(unsigned __int8 *)(*((_QWORD *)a1 + 2)
+ (int)wrap(
(unsigned int)(a3 + v4),
*((_DWORD *)a1 + 2) + (unsigned int)*a1));
if ( (_DWORD)result )
break;
if ( *((unsigned __int8 *)a1 + 2) <= ++v4 )
return 0LL;
}
return result;
}

original_code:
int window_cmp(const struct window *w, int s1_head, int s2_head)
{
 int i;
 int s1_i, s2_i;
 int diff;
 diff=0;
 for(i = 0; i < w->look_ah_length; i++){
  s1_i = wrap( s1_head + i, w->window_length + w->dict_position , w->dict_position);
  s2_i = wrap( s2_head + i, w->window_length + w->dict_position , w->dict_position);
  diff = w->window[ s1_i ] - w->window[ s2_i ];
  if (diff != 0)
   break;
 }
 return diff;
}

canonical_original_code:
int window_cmp(struct window *w, int s1_head, int s2_head)
{
 int i;
 int s1_i; int s2_i;
 int diff;
 diff=0;
 for(i = 0; i < w->look_ah_length; i++){
  s1_i = wrap( s1_head + i, w->window_length + w->dict_position , w->dict_position);
  s2_i = wrap( s2_head + i, w->window_length + w->dict_position , w->dict_position);
  diff = w->window[ s1_i ] - w->window[ s2_i ];
  if (diff != 0)
   break;
 }
 return diff;
}

variable_types: struct window { unsigned short window_length; unsigned char look_ah_length; int data_position; int dict_position; unsigned char *window; } *

variable_types: diff int


decompiled_code:
__int64 __fastcall add_node(__int64 a1, int a2, unsigned __int16 *a3)
{
int v4; // ebp
__int64 result; // rax
__int64 v6; // r14
int *v7; // rbx
int v8; // edx
unsigned int v9; // [rsp+Ch] [rbp-3Ch]

v4 = *(_DWORD *)(a1 + 8);
result = (unsigned int)(a2 % *a3 + 1);
v9 = result;
v6 = a1 + 16LL * (int)result;
if ( !v4 )
{
*(_DWORD *)(a1 + 8) = result;
*(_DWORD *)(v6 + 12) = 0;
*(_DWORD *)v6 = a2;
return result;
}
while ( 1 )
{
v7 = (int *)(a1 + 16LL * v4);
if ( (int)window_cmp(a3, *v7, a2) > 0 )
break;
v8 = v7[2];
if ( !v8 )
{
result = v9;
v7[2] = v9;
goto LABEL_8;
}
LABEL_4:
v4 = v8;
}
v8 = v7[1];
if ( v8 )
goto LABEL_4;
result = v9;
v7[1] = v9;
LABEL_8:
*(_DWORD *)(v6 + 12) = v4;
*(_DWORD *)v6 = a2;
*(_QWORD *)(v6 + 4) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, unsigned __int16 *a3)
{
int v4; // ebp
__int64 result; // rax
__int64 v6; // r14
int *v7; // rbx
int v8; // edx
unsigned int v9; // [rsp+Ch] [rbp-3Ch]

v4 = *(_DWORD *)(a1 + 8);
result = (unsigned int)(a2 % *a3 + 1);
v9 = result;
v6 = a1 + 16LL * (int)result;
if ( !v4 )
{
*(_DWORD *)(a1 + 8) = result;
*(_DWORD *)(v6 + 12) = 0;
*(_DWORD *)v6 = a2;
return result;
}
while ( 1 )
{
v7 = (int *)(a1 + 16LL * v4);
if ( (int)func4(a3, *v7, a2) > 0 )
break;
v8 = v7[2];
if ( !v8 )
{
result = v9;
v7[2] = v9;
goto LABEL_8;
}
LABEL_4:
v4 = v8;
}
v8 = v7[1];
if ( v8 )
goto LABEL_4;
result = v9;
v7[1] = v9;
LABEL_8:
*(_DWORD *)(v6 + 12) = v4;
*(_DWORD *)v6 = a2;
*(_QWORD *)(v6 + 4) = 0LL;
return result;
}

original_code:
void add_node( struct Node *tree, int new_node, const struct window *w)
{
 int current_node;
 int win_position;
 int new_node_position;
 new_node_position = ( new_node % w->window_length ) +1;
 if( tree[ 0 ].greater == 0)
 {
  tree[ 0 ].greater = new_node_position;
  tree[ new_node_position ].father = 0;
  tree[ new_node_position ].position = new_node;
  return;
 }
 current_node = tree[ 0 ].greater;
 for(;;)
 {
  win_position = tree[ current_node ].position;
  if ( window_cmp(w, win_position, new_node) > 0 )
  {
   if( tree[ current_node ].smaller == 0)
   {
    tree[ current_node ].smaller = new_node_position;
    break;
   }
   current_node = tree[ current_node ].smaller;
  }else{
   if(tree[ current_node ].greater == 0)
   {
    tree[ current_node ].greater = new_node_position;
    break;
   }
   current_node = tree[ current_node ].greater;
  }
 }
 tree[ new_node_position ].father = current_node;
 tree[ new_node_position ].position = new_node;
 tree[ new_node_position ].greater = 0;
 tree[ new_node_position ].smaller = 0;
}

canonical_original_code:
void add_node(struct Node *tree, int new_node, struct window *w)
{
 int current_node;
 int win_position;
 int new_node_position;
 new_node_position = ( new_node % w->window_length ) +1;
 if( tree[ 0 ].greater == 0)
 {
  tree[ 0 ].greater = new_node_position;
  tree[ new_node_position ].father = 0;
  tree[ new_node_position ].position = new_node;
  return;
 }
 current_node = tree[ 0 ].greater;
 for(;;)
 {
  win_position = tree[ current_node ].position;
  if ( window_cmp(w, win_position, new_node) > 0 )
  {
   if( tree[ current_node ].smaller == 0)
   {
    tree[ current_node ].smaller = new_node_position;
    break;
   }
   current_node = tree[ current_node ].smaller;
  }else{
   if(tree[ current_node ].greater == 0)
   {
    tree[ current_node ].greater = new_node_position;
    break;
   }
   current_node = tree[ current_node ].greater;
  }
 }
 tree[ new_node_position ].father = current_node;
 tree[ new_node_position ].position = new_node;
 tree[ new_node_position ].greater = 0;
 tree[ new_node_position ].smaller = 0;
}

user_defined_types: struct window { unsigned short window_length; unsigned char look_ah_length; int data_position; int dict_position; unsigned char *window; }


original_code:
struct match find_match(struct Node *tree, struct window w)
{
    struct match match;
 int i;
 int node;
 int string_head;
 int diff;
 int count;
 int type;
 node = tree[0].greater;
 string_head = tree[node].position;
 match.len = 0;
 match.position = 0;
 match.type = 0;
 for(;;){
  diff = w.window[ string_head ] - w.window[ w.data_position ];
  count = 0;
  i = 0;
  type = 0;
  while( (diff == 0) && (count != w.look_ah_length) && (w.data_position + i < w.window_length*2) ){
   count++;
   i++;
   diff = w.window[ wrap(string_head + i, w.window_length + w.dict_position , w.dict_position)] - w.window[ w.data_position + i ];
  }
  if( (string_head > (w.data_position - w.look_ah_length) ) && (count >= ( w.data_position - string_head) ) ){
   int count_forward;
   count_forward = w.data_position - string_head;
   diff = w.window[string_head + count_forward] - w.window[ w.data_position + count_forward];
   while( (diff == 0) && (count_forward != w.look_ah_length) && (w.data_position + count_forward < w.window_length*2) ){
    count_forward++;
    diff = w.window[string_head + count_forward ] - w.window[ w.data_position + count_forward ];
   }
   if(count_forward > count){
    count = count_forward;
    type = 1;
   }
  }
  if( match.len < count ){
   match.len = count;
   match.position = string_head;
   match.type = type;
  }
  if(diff < 0)
   node = tree[node].greater;
  else
   node = tree[node].smaller;
         if (( node == 0 ) || ( match.len == w.look_ah_length ) )
   break;
         string_head = tree[node].position;
 }
 return match;
}

variable_types: struct window { unsigned short window_length; unsigned char look_ah_length; int data_position; int dict_position; unsigned char *window; }

variable_types: struct match { unsigned char len; unsigned short position; unsigned char type; }

variable_types: count int

variable_types: type int