decompiled_code:
__int64 __fastcall libcompel_pack_argv(char *src, int a2, const char **a3, _QWORD *a4, unsigned int *a5)
{
unsigned int v7; // ebp
const char **v8; // rbx
char *v9; // rax
char *v10; // rbx
__int64 v11; // rdi
char *v12; // rdi
__int64 i; // r14

v7 = strlen(src) + 13;
if ( a2 > 0 )
{
v8 = a3;
do
v7 += strlen(*v8++) + 9;
while ( v8 != &a3[(unsigned int)(a2 - 1) + 1] );
}
v9 = (char *)malloc(v7);
v10 = v9;
if ( !v9 )
return 0xFFFFFFFFLL;
*(_DWORD *)v9 = a2 + 1;
v11 = 8LL * a2 + 12;
*(_QWORD *)(v9 + 4) = v11;
v12 = stpcpy(&v9[v11], src) + 1;
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; v12 = stpcpy(v12, a3[i++]) + 1 )
*(_QWORD *)&v10[8 * i + 12] = v12 - v10;
}
*a4 = v10;
*a5 = v7;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, int a2, const char **a3, _QWORD *a4, unsigned int *a5)
{
unsigned int v7; // ebp
const char **v8; // rbx
char *v9; // rax
char *v10; // rbx
__int64 v11; // rdi
char *v12; // rdi
__int64 i; // r14

v7 = strlen(src) + 13;
if ( a2 > 0 )
{
v8 = a3;
do
v7 += strlen(*v8++) + 9;
while ( v8 != &a3[(unsigned int)(a2 - 1) + 1] );
}
v9 = (char *)malloc(v7);
v10 = v9;
if ( !v9 )
return 0xFFFFFFFFLL;
*(_DWORD *)v9 = a2 + 1;
v11 = 8LL * a2 + 12;
*(_QWORD *)(v9 + 4) = v11;
v12 = stpcpy(&v9[v11], src) + 1;
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; v12 = stpcpy(v12, a3[i++]) + 1 )
*(_QWORD *)&v10[8 * i + 12] = v12 - v10;
}
*a4 = v10;
*a5 = v7;
return 0LL;
}

original_code:
int libcompel_pack_argv(char *blob, int argc, char **argv, void **arg_p, unsigned int *arg_s)
{
 int i, *argc_packed;
 char *args_mem;
 unsigned total_len;
 unsigned long *argv_packed;
 total_len = sizeof(int);
 total_len += sizeof(unsigned long) + strlen(blob) + 1;
 for (i = 0; i < argc; i++)
  total_len += sizeof(unsigned long) + strlen(argv[i]) + 1;
 argc_packed = malloc(total_len);
 if (!argc_packed)
  return -1;
 *argc_packed = argc + 1;
 argv_packed = (unsigned long *)(argc_packed + 1);
 args_mem = (char *)(argv_packed + argc + 1);
 argv_packed[0] = (void *)args_mem - (void *)argc_packed;
 args_mem = stpcpy(args_mem, blob) + 1;
 for (i = 0; i < argc; i++) {
  argv_packed[i + 1] = (void *)args_mem - (void *)argc_packed;
  args_mem = stpcpy(args_mem, argv[i]) + 1;
 }
 *arg_p = argc_packed;
 *arg_s = total_len;
 return 0;
}

canonical_original_code:
int libcompel_pack_argv(char *blob, int argc, char **argv, void **arg_p, unsigned int *arg_s)
{
 int i; int *argc_packed;
 char *args_mem;
 unsigned int total_len;
 unsigned long *argv_packed;
 total_len = sizeof(int);
 total_len += sizeof(unsigned long) + strlen(blob) + 1;
 for (i = 0; i < argc; i++)
  total_len += sizeof(unsigned long) + strlen(argv[i]) + 1;
 argc_packed = malloc(total_len);
 if (!argc_packed)
  return -1;
 *argc_packed = argc + 1;
 argv_packed = (unsigned long *)(argc_packed + 1);
 args_mem = (char *)(argv_packed + argc + 1);
 argv_packed[0] = (void *)args_mem - (void *)argc_packed;
 args_mem = stpcpy(args_mem, blob) + 1;
 for (i = 0; i < argc; i++) {
  argv_packed[i + 1] = (void *)args_mem - (void *)argc_packed;
  args_mem = stpcpy(args_mem, argv[i]) + 1;
 }
 *arg_p = argc_packed;
 *arg_s = total_len;
 return 0;
}

variable_types:
blob
char *

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


repo: cyrillos/compel

decompiled_code:
void __fastcall loglevel_set(int a1)
{
current_loglevel = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
current_loglevel = a1;
}

original_code:
static void loglevel_set(unsigned int loglevel)
{
 current_loglevel = loglevel;
}

variable_types:
loglevel
unsigned int

return_type: void


decompiled_code:
_BOOL8 __fastcall pr_quelled(unsigned int a1)
{
bool v1; // al

v1 = 0;
if ( a1 )
v1 = current_loglevel < a1;
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(unsigned int a1)
{
bool v1; // al

v1 = 0;
if ( a1 )
v1 = current_loglevel < a1;
return v1;
}

original_code:
static inline _Bool pr_quelled(unsigned int loglevel)
{
 return loglevel != (0) && loglevel > current_loglevel;
}


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

if ( logfd != -1 )
result = close(logfd);
logfd = a1;
return result;
}

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

if ( logfd != -1 )
result = close(logfd);
logfd = a1;
return result;
}

original_code:
static void log_set_fd(int fd)
{
 if (logfd != -1)
  close(logfd);
 logfd = fd;
}

variable_types: fd int


decompiled_code:
void __fastcall libcompel_log_init(int a1, int a2)
{
log_set_fd(a1);
loglevel_set(a2);
}

canonical_decompiled_code:
void __fastcall func4(int a1, int a2)
{
func2(a1);
func0(a2);
}

original_code:
void libcompel_log_init(int fd, unsigned int level)
{
 log_set_fd(fd);
 loglevel_set(level);
}

variable_types:
level
unsigned int


decompiled_code:
void print_on_level(unsigned int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !pr_quelled(a1) )
{
va_start(va, a2);
_print_on_level(a1, a2, va);
}
}

canonical_decompiled_code:
void func5(unsigned int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !func1(a1) )
{
va_start(va, a2);
func3(a1, a2, va);
}
}

original_code:
void print_on_level(unsigned int loglevel, const char *format, ...)
{
 va_list params;
 if (pr_quelled(loglevel))
  return;
 __builtin_va_start(params,format);
 __print_on_level(loglevel, format, params);
 __builtin_va_end(params);
}

canonical_original_code:
void print_on_level(unsigned int loglevel, char *format, ...)
{
 __builtin_va_list params;
 if (pr_quelled(loglevel))
  return;
 __builtin_va_start(params,format);
 __print_on_level(loglevel, format, params);
 __builtin_va_end(params);
}

variable_types:
format
char *


decompiled_code:
__int64 __fastcall PTR_ERR(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1;
}

original_code:
static inline long PTR_ERR(const void *ptr)
{
 return (long)ptr;
}

canonical_original_code:
static inline long PTR_ERR(void *ptr)
{
 return (long)ptr;
}

variable_types:
ptr
void *

return_type: long


decompiled_code:
_BOOL8 __fastcall IS_ERR(unsigned __int64 a1)
{
return a1 > 0xFFFFFFFFFFFFF000LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(unsigned __int64 a1)
{
return a1 > 0xFFFFFFFFFFFFF000LL;
}

original_code:
static inline long IS_ERR(const void *ptr)
{
 return __builtin_expect(!!(((unsigned long)ptr) >= (unsigned long)-4095), 0);
}

canonical_original_code:
static inline long IS_ERR(void *ptr)
{
 return __builtin_expect(!!(((unsigned long)ptr) >= (unsigned long)-4095), 0);
}


decompiled_code:
__int64 libcompel_exec()
{
__int64 v0; // rbx
__int64 result; // rax

v0 = parasite_start();
if ( IS_ERR(v0) )
result = PTR_ERR(v0);
else
result = parasite_end(v0);
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx
__int64 result; // rax

v0 = parasite_start();
if ( func1(v0) )
result = func0(v0);
else
result = parasite_end(v0);
return result;
}

original_code:
int libcompel_exec(pid_t pid, char *path, void *arg_p, unsigned int arg_s)
{
 void *p;
 p = parasite_start(pid, path, arg_p, arg_s);
 if (IS_ERR(p))
  return PTR_ERR(p);
 return parasite_end(p);
}

canonical_original_code:
int libcompel_exec(int pid, char *path, void *arg_p, unsigned int arg_s)
{
 void *p;
 p = parasite_start(pid, path, arg_p, arg_s);
 if (IS_ERR(p))
  return PTR_ERR(p);
 return parasite_end(p);
}

variable_types: pid int

variable_types:
path
char *

variable_types:
p
void *

return_type: int


decompiled_code:
__int64 libcompel_verify_packed()
{
return verify_elf_packed();
}

canonical_decompiled_code:
__int64 func6()
{
return verify_elf_packed();
}

original_code:
int libcompel_verify_packed(char *path)
{
 return verify_elf_packed(path);
}


decompiled_code:
int __fastcall print_forwarding(unsigned int a1, unsigned int a2)
{
return printf("O %u %d\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, unsigned int a2)
{
return printf("O %u %d\n", a1, a2);
}

original_code:
static inline void print_forwarding(unsigned int packet_id, int nic) {
  printf("O %u %d\n", packet_id, nic);
}

variable_types: nic int


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

result = (unsigned int)a1 > (unsigned int)a2;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) > BYTE4(a2);
return result;
}

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

result = (unsigned int)a1 > (unsigned int)a2;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) > BYTE4(a2);
return result;
}

original_code:
int key_compare (node_key key1, node_key key2){
 if (key1.ip == key2.ip){
  return key1.netsize > key2.netsize;
 }
 return key1.ip > key2.ip;
}

canonical_original_code:
int key_compare(struct node_key key1, struct node_key key2){
 if (key1.ip == key2.ip){
  return key1.netsize > key2.netsize;
 }
 return key1.ip > key2.ip;
}

variable_types: struct node_key { unsigned int ip; unsigned char netsize; }


decompiled_code:
_BOOL8 __fastcall check_match(__int64 a1, unsigned int a2)
{
return *(_DWORD *)(a1 + 8) >> *(_BYTE *)(a1 + 12) == a2 >> *(_BYTE *)(a1 + 12);
}

original_code:
int check_match(node * n, uint32_t ip){
 node_key data = n->key;
 uint32_t block = data.ip;
 uint8_t bitshift = data.netsize;
 uint32_t shift_ip = ip >> bitshift;
 uint32_t shift_block = block >> bitshift;
 return shift_ip == shift_block;
}

canonical_original_code:
int check_match(struct node *n, unsigned int ip){
 struct node_key data = n->key;
 unsigned int block = data.ip;
 unsigned char bitshift = data.netsize;
 unsigned int shift_ip = ip >> bitshift;
 unsigned int shift_block = block >> bitshift;
 return shift_ip == shift_block;
}

variable_types: struct node { struct node *next; struct node_key { unsigned int ip; unsigned char netsize; } key; int value; } *

variable_types:
ip
unsigned int

variable_types:
block
unsigned int

variable_types:
bitshift
unsigned char

user_defined_types: struct node { struct node *next; struct node_key key; int value; }


decompiled_code:
__int64 __fastcall find_longest_prefix_match(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v3; // er12

if ( !a1 )
return (unsigned int)-1;
v2 = a1;
v3 = -1;
do
{
if ( check_match(v2, a2) )
v3 = *(_DWORD *)(v2 + 16);
v2 = *(_QWORD *)v2;
}
while ( v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v3; // er12

if ( !a1 )
return (unsigned int)-1;
v2 = a1;
v3 = -1;
do
{
if ( func8(v2, a2) )
v3 = *(_DWORD *)(v2 + 16);
v2 = *(_QWORD *)v2;
}
while ( v2 );
return v3;
}

original_code:
int find_longest_prefix_match(node * head, uint32_t ip){
 node * walker = head;
 int foundSomething = 0;
 int best_nic_found = -1;
 while(walker != ((void *)0)){
  if(check_match(walker, ip)){
   best_nic_found = walker->value;
  }else{
   if(foundSomething){
    return best_nic_found;
   }
  }
  walker = walker->next;
 }
 return best_nic_found;
}

canonical_original_code:
int find_longest_prefix_match(struct node *head, unsigned int ip){
 struct node *walker = head;
 int foundSomething = 0;
 int best_nic_found = -1;
 while(walker != ((void *)0)){
  if(check_match(walker, ip)){
   best_nic_found = walker->value;
  }else{
   if(foundSomething){
    return best_nic_found;
   }
  }
  walker = walker->next;
 }
 return best_nic_found;
}

variable_types: foundSomething int

user_defined_types: struct node_key { unsigned int ip; unsigned char netsize; }


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

v4 = find_longest_prefix_match(*a1, a2);
return print_forwarding(a3, v4);
}

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

v4 = func9(*a1, a2);
return func0(a3, v4);
}

original_code:
void forward_packet(router_state state, uint32_t ip, unsigned int packet_id) {
 int nic = find_longest_prefix_match(state->head, ip);
 print_forwarding(packet_id, nic);
}

canonical_original_code:
void forward_packet(struct router_state *state, unsigned int ip, unsigned int packet_id) {
 int nic = find_longest_prefix_match(state->head, ip);
 print_forwarding(packet_id, nic);
}

variable_types: struct router_state { struct node { struct node *next; struct node_key { unsigned int ip; unsigned char netsize; } key; int value; } *head; } *

user_defined_types: struct router_state { struct node *head; }


print

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

if ( a1 )
{
printf(
"BLOCK: %d, MASK: %d, PORT: %d\n",
*(unsigned int *)(a1 + 8),
*(unsigned __int8 *)(a1 + 12),
*(unsigned int *)(a1 + 16));
result = print(*(_QWORD *)a1);
}
return result;
}

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

if ( a1 )
{
printf(
"BLOCK: %d, MASK: %d, PORT: %d\n",
*(unsigned int *)(a1 + 8),
*(unsigned __int8 *)(a1 + 12),
*(unsigned int *)(a1 + 16));
result = func11(*(_QWORD *)a1);
}
return result;
}

original_code:
void print(node * n){
 if(n==((void *)0)){
  return;
 }
 printf("BLOCK: %d, MASK: %d, PORT: %d\n", n->key.ip, n->key.netsize, n->value);
 print(n->next);
}

canonical_original_code:
void print(struct node *n){
 if(n==((void *)0)){
  return;
 }
 printf("BLOCK: %d, MASK: %d, PORT: %d\n", n->key.ip, n->key.netsize, n->value);
 print(n->next);
}


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

result = 0LL;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) == BYTE4(a2);
return result;
}

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

result = 0LL;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) == BYTE4(a2);
return result;
}

original_code:
int key_equals(node_key key1, node_key key2){
 return key1.ip == key2.ip && key1.netsize == key2.netsize;
}

canonical_original_code:
int key_equals(struct node_key key1, struct node_key key2){
 return key1.ip == key2.ip && key1.netsize == key2.netsize;
}


decompiled_code:
__int64 __fastcall http_parse_method(char *s1)
{
const unsigned __int16 *v2; // r14
__int64 v3; // rbp
unsigned int v4; // er15
const char *v5; // r13
size_t v6; // r12

v2 = *_ctype_b_loc();
while ( (v2[*s1] & 0x2000) != 0 )
++s1;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
v5 = (&http_method_str)[v3];
v6 = strlen(v5);
if ( !strncasecmp(s1, v5, v6) && (v2[s1[v6]] & 0x2000) != 0 )
break;
if ( (unsigned int)++v3 > 7 )
return 8;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
const unsigned __int16 *v2; // r14
__int64 v3; // rbp
unsigned int v4; // er15
const char *v5; // r13
size_t v6; // r12

v2 = *_ctype_b_loc();
while ( (v2[*s1] & 0x2000) != 0 )
++s1;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
v5 = (&http_method_str)[v3];
v6 = strlen(v5);
if ( !strncasecmp(s1, v5, v6) && (v2[s1[v6]] & 0x2000) != 0 )
break;
if ( (unsigned int)++v3 > 7 )
return 8;
}
return v4;
}

original_code:
http_method http_parse_method(const char *request) {
    http_method m;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short int) _ISspace)) request++;
    for(m = 0; m < METHOD_UNKNOWN; m++)
        if (!strncasecmp(request, http_method_str[m], strlen(http_method_str[m])) &&
            ((*__ctype_b_loc ())[(int) ((request[strlen(http_method_str[m])]))] & (unsigned short int) _ISspace))
            return m;
    return METHOD_UNKNOWN;
}

canonical_original_code:
enum http_method http_parse_method(char *request) {
    enum http_method m;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short) _ISspace)) request++;
    for(m = 0; m < METHOD_UNKNOWN; m++)
        if (!strncasecmp(request, http_method_str[m], strlen(http_method_str[m])) &&
            ((*__ctype_b_loc ())[(int) ((request[strlen(http_method_str[m])]))] & (unsigned short) _ISspace))
            return m;
    return METHOD_UNKNOWN;
}

variable_types:
request
char *

variable_types: enum http_method { METHOD_GET=0, METHOD_POST=1, METHOD_HEAD=2, METHOD_OPTIONS=3, METHOD_PUT=4, METHOD_DELETE=5, METHOD_TRACE=6, METHOD_CONNECT=7, METHOD_UNKNOWN=8 }


original_code:
char *http_parse_uri(char *request) {
    char *start, *end;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short int) _ISspace)) request++;
    for (start = request; !((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short int) _ISspace); start++);
    for ( ; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short int) _ISspace); start++);
    for (end = start; *end && !((*__ctype_b_loc ())[(int) ((*end))] & (unsigned short int) _ISspace); end++);
    *end = '\0';
    return start;
}

canonical_original_code:
char *http_parse_uri(char *request) {
    char *start; char *end;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short) _ISspace)) request++;
    for (start = request; !((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short) _ISspace); start++);
    for ( ; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short) _ISspace); start++);
    for (end = start; *end && !((*__ctype_b_loc ())[(int) ((*end))] & (unsigned short) _ISspace); end++);
    *end = '\0';
    return start;
}

variable_types:
start
char *

variable_types:
end
char *

return_type: char *


decompiled_code:
const char *__fastcall http_parse_path(const char *a1)
{
const char *v1; // rbx
char *v2; // rax
char v3; // al

v1 = a1;
if ( *a1 != 47 )
{
v2 = strchr(a1, 58);
if ( v2 )
{
if ( !memcmp(v2, &unk_561, 3uLL) )
{
v1 = v2 + 3;
v3 = v2[3];
if ( v3 )
{
if ( v3 != 47 )
{
do
++v1;
while ( *v1 && *v1 != 47 );
}
}
}
}
}
return v1;
}

canonical_decompiled_code:
const char *__fastcall func2(const char *a1)
{
const char *v1; // rbx
char *v2; // rax
char v3; // al

v1 = a1;
if ( *a1 != 47 )
{
v2 = strchr(a1, 58);
if ( v2 )
{
if ( !memcmp(v2, &unk_561, 3uLL) )
{
v1 = v2 + 3;
v3 = v2[3];
if ( v3 )
{
if ( v3 != 47 )
{
do
++v1;
while ( *v1 && *v1 != 47 );
}
}
}
}
}
return v1;
}

original_code:
const char *http_parse_path(const char *uri) {
    if (*uri == '/') return uri;
    char *s = strchr(uri, ':');
    if (!s || strncmp(s, "://", 3)) return uri;
    for(s += 3; *s && *s != '/'; s++);
    return s;
}

canonical_original_code:
char *http_parse_path(char *uri) {
    if (*uri == '/') return uri;
    char *s = strchr(uri, ':');
    if (!s || strncmp(s, "://", 3)) return uri;
    for(s += 3; *s && *s != '/'; s++);
    return s;
}

variable_types:
uri
char *

variable_types:
s
char *


original_code:
char *http_parse_header_field(char *request, int length, const char *header_field) {
    char *oldlf, *newlf, *newnull, *start;
    int len = strlen(header_field);
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short int) _ISspace) && length > 0) request++, length--;
    for (oldlf = memchr(request, '\n', length); oldlf; oldlf = newlf) {
        newlf = memchr(oldlf + 1, '\n', request + length - 1 - oldlf);
        newnull = memchr(oldlf + 1, '\0', request + length - 1 - oldlf);
        if (newnull && newnull < newlf) newlf = newnull;
        if (newlf == oldlf + 1) return ((void *)0);
        if (newlf == oldlf + 2 && oldlf[1] == '\r') return ((void *)0);
        for(start = oldlf + 1; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short int) _ISspace); start++);
        if (!strncasecmp(start, header_field, len) && start[len] == ':') {
            for(start += len + 1; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short int) _ISspace); start++);
            for(; ((*__ctype_b_loc ())[(int) ((*newlf))] & (unsigned short int) _ISspace); newlf--) *newlf = '\0';
            return start;
        } else if (!*newlf) {
            for(; !newlf[1]; newlf++);
        }
    }
    return ((void *)0);
}

canonical_original_code:
char *http_parse_header_field(char *request, int length, char *header_field) {
    char *oldlf; char *newlf; char *newnull; char *start;
    int len = strlen(header_field);
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short) _ISspace) && length > 0) request++, length--;
    for (oldlf = memchr(request, '\n', length); oldlf; oldlf = newlf) {
        newlf = memchr(oldlf + 1, '\n', request + length - 1 - oldlf);
        newnull = memchr(oldlf + 1, '\0', request + length - 1 - oldlf);
        if (newnull && newnull < newlf) newlf = newnull;
        if (newlf == oldlf + 1) return ((void *)0);
        if (newlf == oldlf + 2 && oldlf[1] == '\r') return ((void *)0);
        for(start = oldlf + 1; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short) _ISspace); start++);
        if (!strncasecmp(start, header_field, len) && start[len] == ':') {
            for(start += len + 1; ((*__ctype_b_loc ())[(int) ((*start))] & (unsigned short) _ISspace); start++);
            for(; ((*__ctype_b_loc ())[(int) ((*newlf))] & (unsigned short) _ISspace); newlf--) *newlf = '\0';
            return start;
        } else if (!*newlf) {
            for(; !newlf[1]; newlf++);
        }
    }
    return ((void *)0);
}

variable_types: length int

variable_types:
oldlf
char *

variable_types:
newlf
char *

variable_types:
newnull
char *

variable_types: len int


original_code:
const char *http_parse_body(const char *request, int length) {
    const char *oldlf, *newlf, *newnull;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short int) _ISspace) && length > 0) request++, length--;
    for (oldlf = memchr(request, '\n', length);
         oldlf && (newlf = memchr(oldlf + 1, '\n', request + length - 1 - oldlf));
         oldlf = newlf) {
        newnull = memchr(oldlf + 1, '\0', request + length - 1 - oldlf);
        if (newnull && newnull < newlf) newlf = newnull;
        if (newlf == oldlf + 1) return newlf + 1;
        if (newlf == oldlf + 2 && oldlf[1] == '\r') return newlf + 1;
        if (!*newlf) {
            for(; !newlf[1]; newlf++);
        }
    }
    return ((void *)0);
}

canonical_original_code:
char *http_parse_body(char *request, int length) {
    char *oldlf; char *newlf; char *newnull;
    while (((*__ctype_b_loc ())[(int) ((*request))] & (unsigned short) _ISspace) && length > 0) request++, length--;
    for (oldlf = memchr(request, '\n', length);
         oldlf && (newlf = memchr(oldlf + 1, '\n', request + length - 1 - oldlf));
         oldlf = newlf) {
        newnull = memchr(oldlf + 1, '\0', request + length - 1 - oldlf);
        if (newnull && newnull < newlf) newlf = newnull;
        if (newlf == oldlf + 1) return newlf + 1;
        if (newlf == oldlf + 2 && oldlf[1] == '\r') return newlf + 1;
        if (!*newlf) {
            for(; !newlf[1]; newlf++);
        }
    }
    return ((void *)0);
}


decompiled_code:
__int64 __fastcall http_header_complete(void *s, int a2)
{
char *v2; // rax
unsigned int v3; // ebx
bool v4; // zf
__int64 result; // rax

v2 = http_parse_body(s, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = (_DWORD)v2 - (_DWORD)s;
v4 = memchr(s, 0, v2 - (_BYTE *)s) == 0LL;
result = 0xFFFFFFFFLL;
if ( v4 )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *s, int a2)
{
char *v2; // rax
unsigned int v3; // ebx
bool v4; // zf
__int64 result; // rax

v2 = func4(s, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = (_DWORD)v2 - (_DWORD)s;
v4 = memchr(s, 0, v2 - (_BYTE *)s) == 0LL;
result = 0xFFFFFFFFLL;
if ( v4 )
result = v3;
return result;
}

original_code:
int http_header_complete(const char *request, int length) {
    const char *body = http_parse_body(request, length);
    if (body && !memchr(request, '\0', body - request))
        return body - request;
    else
        return -1;
}

canonical_original_code:
int http_header_complete(char *request, int length) {
    char *body = http_parse_body(request, length);
    if (body && !memchr(request, '\0', body - request))
        return body - request;
    else
        return -1;
}

variable_types:
body
char *


encode

original_code:
char *encode(const char *original, char *encoded) {
    char *e = encoded;
    for(;*original; original++, encoded++) {
        if (((*__ctype_b_loc ())[(int) ((*original))] & (unsigned short int) _ISalnum))
            *encoded = *original;
        else if (*original == ' ')
            *encoded = '+';
        else {
            sprintf(encoded, "%%%02X", *original);
            encoded += 2;
        }
    }
    *encoded = '\0';
    return e;
}

canonical_original_code:
char *encode(char *original, char *encoded) {
    char *e = encoded;
    for(;*original; original++, encoded++) {
        if (((*__ctype_b_loc ())[(int) ((*original))] & (unsigned short) _ISalnum))
            *encoded = *original;
        else if (*original == ' ')
            *encoded = '+';
        else {
            sprintf(encoded, "%%%02X", *original);
            encoded += 2;
        }
    }
    *encoded = '\0';
    return e;
}

variable_types:
original
char *

variable_types:
encoded
char *

variable_types:
e
char *


decompiled_code:
__int64 __fastcall map_key_cmp(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

original_code:
int map_key_cmp(map_key key1, map_key key2) {
  return key1 - key2;
}

canonical_original_code:
int map_key_cmp(int key1, int key2) {
  return key1 - key2;
}


decompiled_code:
int __fastcall str_to_map_key(const char *a1)
{
return atoi(a1);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
return atoi(a1);
}

original_code:
map_key str_to_map_key(char *s) {
  return atoi(s);
}

canonical_original_code:
int str_to_map_key(char *s) {
  return atoi(s);
}


decompiled_code:
char *__fastcall str_to_map_value(char *src)
{
char *v1; // rbp
size_t v2; // rax

v1 = src;
if ( src )
{
v2 = strlen(src);
v1 = (char *)malloc(v2 + 1);
strcpy(v1, src);
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func9(char *src)
{
char *v1; // rbp
size_t v2; // rax

v1 = src;
if ( src )
{
v2 = strlen(src);
v1 = (char *)malloc(v2 + 1);
strcpy(v1, src);
}
return v1;
}

original_code:
map_value str_to_map_value(char *s) {
  if (!s) return s;
  map_value v = (map_value) malloc(strlen(s)+1);
  strcpy(v, s);
  return v;
}

canonical_original_code:
char *str_to_map_value(char *s) {
  if (!s) return s;
  char *v = (map_value) malloc(strlen(s)+1);
  strcpy(v, s);
  return v;
}

variable_types:
v
char *


decompiled_code:
int __fastcall print_mapping(unsigned int a1, const char *a2)
{
return printf("\t<%d, %s>\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1, const char *a2)
{
return printf("\t<%d, %s>\n", a1, a2);
}

original_code:
void print_mapping(map_key key, map_value value) {
  printf("\t<%d, %s>\n", key, value);
}

canonical_original_code:
void print_mapping(int key, char *value) {
  printf("\t<%d, %s>\n", key, value);
}

variable_types: key int

variable_types:
value
char *


decompiled_code:
_QWORD *map_create()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func11()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

original_code:
map map_create(void) {
 map myMap = malloc(sizeof(struct map));
 myMap->head = ((void *)0);
 return myMap;
}

canonical_original_code:
struct map *map_create() {
 struct map *myMap = malloc(sizeof(struct map));
 myMap->head = ((void *)0);
 return myMap;
}

variable_types: struct map { struct node { struct node *next; int key; char *value; } *head; } *


insertNode

decompiled_code:
__int64 __fastcall insertNode(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // eax
__int64 result; // rax

if ( !a1 )
return a2;
v2 = *(_DWORD *)(a2 + 8);
v3 = *(_DWORD *)(a1 + 8);
if ( v2 == v3 )
{
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a2 + 16);
result = a1;
}
else if ( v2 <= v3 )
{
*(_QWORD *)a1 = insertNode(*(_QWORD *)a1);
result = a1;
}
else
{
*(_QWORD *)a2 = a1;
result = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // eax
__int64 result; // rax

if ( !a1 )
return a2;
v2 = *(_DWORD *)(a2 + 8);
v3 = *(_DWORD *)(a1 + 8);
if ( v2 == v3 )
{
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a2 + 16);
result = a1;
}
else if ( v2 <= v3 )
{
*(_QWORD *)a1 = func12(*(_QWORD *)a1);
result = a1;
}
else
{
*(_QWORD *)a2 = a1;
result = a2;
}
return result;
}

original_code:
node * insertNode(node * n, node * newNode){
 if(n == ((void *)0)){
  return newNode;
 }
 if(newNode->key == n->key){
  n->value = newNode->value;
  return n;
 }
 if(newNode->key > n->key){
  newNode->next = n;
  return newNode;
 }
 n->next = insertNode(n->next, newNode);
 return n;
}

canonical_original_code:
struct node *insertNode(struct node *n, struct node *newNode){
 if(n == ((void *)0)){
  return newNode;
 }
 if(newNode->key == n->key){
  n->value = newNode->value;
  return n;
 }
 if(newNode->key > n->key){
  newNode->next = n;
  return newNode;
 }
 n->next = insertNode(n->next, newNode);
 return n;
}

variable_types:
n
struct node *

variable_types:
newNode
struct node *

return_type: struct node { struct node *next; int key; char *value; } *


decompiled_code:
__int64 __fastcall map_lookup(__int64 **a1, int a2, _QWORD *a3)
{
__int64 *v4; // rax
__int64 result; // rax
__int64 v6; // rdx

v4 = *a1;
if ( *a1 )
{
while ( 1 )
{
if ( *((_DWORD *)v4 + 2) == a2 )
{
v6 = v4[2];
if ( v6 )
break;
}
v4 = (__int64 *)*v4;
if ( !v4 )
goto LABEL_2;
}
*a3 = v6;
result = 1LL;
}
else
{
LABEL_2:
*a3 = 0LL;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 **a1, int a2, _QWORD *a3)
{
__int64 *v4; // rax
__int64 result; // rax
__int64 v6; // rdx

v4 = *a1;
if ( *a1 )
{
while ( 1 )
{
if ( *((_DWORD *)v4 + 2) == a2 )
{
v6 = v4[2];
if ( v6 )
break;
}
v4 = (__int64 *)*v4;
if ( !v4 )
goto LABEL_2;
}
*a3 = v6;
result = 1LL;
}
else
{
LABEL_2:
*a3 = 0LL;
result = 0LL;
}
return result;
}

original_code:
int map_lookup(map m, map_key key, map_value *value) {
 node * node = m->head;
 while(node != ((void *)0)){
  if(node->key == key){
   if(node->value != ((void *)0)){
    *value = node->value;
    return 1;
   }
  }
  node = node->next;
 }
 *value = ((void *)0);
 return 0;
}

canonical_original_code:
int map_lookup(struct map *m, int key, char **value) {
 struct node *node = m->head;
 while(node != ((void *)0)){
  if(node->key == key){
   if(node->value != ((void *)0)){
    *value = node->value;
    return 1;
   }
  }
  node = node->next;
 }
 *value = ((void *)0);
 return 0;
}

variable_types:
value
char **

variable_types: struct node { struct node *next; int key; char *value; } *


original_code:
int map_insert(map *m, map_key key, map_value value, map_value *old_value) {
 map_lookup(m, key, old_value);
 node * newNode = malloc(sizeof(struct node));
 newNode->key = key;
 newNode->value = value;
 node * head = (*m)->head;
 if(head == ((void *)0) || head->key > newNode->key){
  (*m)->head = newNode;
  newNode->next = head;
 }else if(head->key == newNode->key){
  head->value = newNode->value;
 }else{
  while(head->next != ((void *)0) && head->next->key < newNode->key && head->next->key != newNode->key){
   head = head->next;
  }
  if(head->next == ((void *)0)){
   head->next = newNode;
  }
  else if( head->next->key == newNode->key ){
    head->next->value = newNode->value;
  } else {
    newNode->next = head->next;
    head->next = newNode;
  }
 }
 return 1;
}

canonical_original_code:
int map_insert(struct map **m, int key, char *value, char **old_value) {
 map_lookup(m, key, old_value);
 struct node *newNode = malloc(sizeof(struct node));
 newNode->key = key;
 newNode->value = value;
 struct node *head = (*m)->head;
 if(head == ((void *)0) || head->key > newNode->key){
  (*m)->head = newNode;
  newNode->next = head;
 }else if(head->key == newNode->key){
  head->value = newNode->value;
 }else{
  while(head->next != ((void *)0) && head->next->key < newNode->key && head->next->key != newNode->key){
   head = head->next;
  }
  if(head->next == ((void *)0)){
   head->next = newNode;
  }
  else if( head->next->key == newNode->key ){
    head->next->value = newNode->value;
  } else {
    newNode->next = head->next;
    head->next = newNode;
  }
 }
 return 1;
}

variable_types: struct map { struct node { struct node *next; int key; char *value; } *head; } **


decompiled_code:
__int64 __fastcall map_delete(__int64 **a1, int a2, _QWORD *a3)
{
unsigned int v3; // er12
__int64 *v4; // rbp

v3 = map_lookup(a1, a2, a3);
v4 = *a1;
*v4 = (__int64)deleteNode((_DWORD *)**a1, a2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 **a1, int a2, _QWORD *a3)
{
unsigned int v3; // er12
__int64 *v4; // rbp

v3 = func13(a1, a2, a3);
v4 = *a1;
*v4 = (__int64)deleteNode((_DWORD *)**a1, a2);
return v3;
}

original_code:
int map_delete(map *m, map_key key, map_value *value) {
 int retValue = map_lookup(m, key, value);
 (*m)->head = deleteNode((*m)->head, key);
 return retValue;
}

canonical_original_code:
int map_delete(struct map **m, int key, char **value) {
 int retValue = map_lookup(m, key, value);
 (*m)->head = deleteNode((*m)->head, key);
 return retValue;
}

variable_types: retValue int

user_defined_types: struct node { struct node *next; int key; char *value; }


destroyNode

decompiled_code:
void __fastcall destroyNode(void *ptr)
{
if ( ptr )
{
destroyNode(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func17(void *ptr)
{
if ( ptr )
{
func17(*(void **)ptr);
free(ptr);
}
}

original_code:
void destroyNode(node * n){
 if(n==((void *)0)){
  return;
 }
 destroyNode(n->next);
 free(n);
}

canonical_original_code:
void destroyNode(struct node *n){
 if(n==((void *)0)){
  return;
 }
 destroyNode(n->next);
 free(n);
}


decompiled_code:
void __fastcall map_destroy(void *ptr)
{
destroyNode(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func18(void *ptr)
{
func17(*(void **)ptr);
free(ptr);
}

original_code:
void map_destroy(map m) {
 node * n = m->head;
 destroyNode(n);
 free(m);
}

canonical_original_code:
void map_destroy(struct map *m) {
 struct node *n = m->head;
 destroyNode(n);
 free(m);
}


decompiled_code:
int __fastcall print_advertisement(unsigned int a1, unsigned __int8 a2, unsigned int a3, unsigned int a4, __int64 a5)
{
return printf(
"A %u.%u.%u.%u/%u %u %u\nT %u.%u.%u.%u/%u %d\n",
HIBYTE(a1),
BYTE2(a1),
BYTE1(a1),
(unsigned __int8)a1,
a2,
a4,
a5,
HIBYTE(a1),
BYTE2(a1),
BYTE1(a1),
(unsigned __int8)a1,
a2,
a3);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, unsigned __int8 a2, unsigned int a3, unsigned int a4, __int64 a5)
{
return printf(
"A %u.%u.%u.%u/%u %u %u\nT %u.%u.%u.%u/%u %d\n",
HIBYTE(a1),
BYTE2(a1),
BYTE1(a1),
(unsigned __int8)a1,
a2,
a4,
a5,
HIBYTE(a1),
BYTE2(a1),
BYTE1(a1),
(unsigned __int8)a1,
a2,
a3);
}

original_code:
static inline void print_advertisement(uint32_t ip, uint8_t netsize, int nic,
                                       unsigned int metric, unsigned int update_id) {
  printf("A %u.%u.%u.%u/%u %u %u\n"
         "T %u.%u.%u.%u/%u %d\n",
         (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF, netsize, metric, update_id,
         (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF, netsize, nic);
}

canonical_original_code:
static inline void print_advertisement(unsigned int ip, unsigned char netsize, int nic, unsigned int metric, unsigned int update_id) {
  printf("A %u.%u.%u.%u/%u %u %u\n"
         "T %u.%u.%u.%u/%u %d\n",
         (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF, netsize, metric, update_id,
         (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF, netsize, nic);
}

variable_types:
ip
unsigned int

variable_types:
netsize
unsigned char

variable_types: nic int

variable_types:
metric
unsigned int


original_code:
int find_shortest_nic(node * n, int * best_nic){
 if(n == ((void *)0)){
  return -1;
 }
 node_value node_value = n->value;
 int i;
 int best = 1000;
 for(i= 0; i < 16; i++){
  if (node_value.nic_distances[i] < best) {
   best = node_value.nic_distances[i];
   *best_nic = i;
  }
 }
 return best;
}

canonical_original_code:
int find_shortest_nic(struct node *n, int *best_nic){
 if(n == ((void *)0)){
  return -1;
 }
 struct node_value node_value = n->value;
 int i;
 int best = 1000;
 for(i= 0; i < 16; i++){
  if (node_value.nic_distances[i] < best) {
   best = node_value.nic_distances[i];
   *best_nic = i;
  }
 }
 return best;
}

variable_types: struct node { struct node *next; struct node_key { unsigned int ip; unsigned char netsize; } key; struct node_value { int nic_distances[16]; } value; } *

variable_types: i int

variable_types: best int

user_defined_types: struct node { struct node *next; struct node_key key; struct node_value value; }


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

result = 0LL;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) == BYTE4(a2);
return result;
}

original_code:
int key_equals(node_key key1, node_key key2){
 return key1.ip == key2.ip && key1.netsize == key2.netsize;
}

canonical_original_code:
int key_equals(struct node_key key1, struct node_key key2){
 return key1.ip == key2.ip && key1.netsize == key2.netsize;
}

variable_types: struct node_key { unsigned int ip; unsigned char netsize; }


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

result = (unsigned int)a1 > (unsigned int)a2;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) > BYTE4(a2);
return result;
}

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

result = (unsigned int)a1 > (unsigned int)a2;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = BYTE4(a1) > BYTE4(a2);
return result;
}

original_code:
int key_compare (node_key key1, node_key key2){
 if (key1.ip == key2.ip){
  return key1.netsize > key2.netsize;
 }
 return key1.ip > key2.ip;
}

canonical_original_code:
int key_compare(struct node_key key1, struct node_key key2){
 if (key1.ip == key2.ip){
  return key1.netsize > key2.netsize;
 }
 return key1.ip > key2.ip;
}


decompiled_code:
unsigned int __fastcall process_update(__m128i ***a1, unsigned __int32 a2, unsigned __int8 a3, int a4, int a5, unsigned int a6)
{
__int64 v7; // r13
__m128i **v8; // r14
__m128i *v9; // r15
const __m128i *v10; // rax
const __m128i *v11; // rbp
int v12; // er13
__m128i **v13; // r12
unsigned int result; // eax
const __m128i *v15; // rax
unsigned int v16; // eax
unsigned int v20[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v7 = a2 | ((unsigned __int64)a3 << 32);
v8 = *a1;
v9 = **a1;
v10 = (const __m128i *)node_find_subnet(v9, v7);
if ( !v10 )
{
*v8 = node_insert(v9, a2, a3, a4, a5 + 1);
v15 = (const __m128i *)node_find_subnet(**a1, v7);
v16 = find_shortest_nic(v15, v20);
if ( v16 != 1000 )
return print_advertisement(a2, a3, v20[0], v16, a6);
return print_advertisement(a2, a3, 0xFFFFFFFF, 0x3E8u, a6);
}
v11 = v10;
v12 = find_shortest_nic(v10, v20);
v13 = *a1;
*v13 = node_insert(*v13, a2, a3, a4, a5 + 1);
result = find_shortest_nic(v11, v20);
if ( v12 == result )
return result;
if ( result == 1000 )
return print_advertisement(a2, a3, 0xFFFFFFFF, 0x3E8u, a6);
return print_advertisement(a2, a3, v20[0], result, a6);
}

canonical_decompiled_code:
unsigned int __fastcall func8(__m128i ***a1, unsigned __int32 a2, unsigned __int8 a3, int a4, int a5, unsigned int a6)
{
__int64 v7; // r13
__m128i **v8; // r14
__m128i *v9; // r15
const __m128i *v10; // rax
const __m128i *v11; // rbp
int v12; // er13
__m128i **v13; // r12
unsigned int result; // eax
const __m128i *v15; // rax
unsigned int v16; // eax
unsigned int v20[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v7 = a2 | ((unsigned __int64)a3 << 32);
v8 = *a1;
v9 = **a1;
v10 = (const __m128i *)func6(v9, v7);
if ( !v10 )
{
*v8 = node_insert(v9, a2, a3, a4, a5 + 1);
v15 = (const __m128i *)func6(**a1, v7);
v16 = func4(v15, v20);
if ( v16 != 1000 )
return func0(a2, a3, v20[0], v16, a6);
return func0(a2, a3, 0xFFFFFFFF, 0x3E8u, a6);
}
v11 = v10;
v12 = func4(v10, v20);
v13 = *a1;
*v13 = node_insert(*v13, a2, a3, a4, a5 + 1);
result = func4(v11, v20);
if ( v12 == result )
return result;
if ( result == 1000 )
return func0(a2, a3, 0xFFFFFFFF, 0x3E8u, a6);
return func0(a2, a3, v20[0], result, a6);
}

original_code:
void process_update(router_state *state, uint32_t ip, uint8_t netsize,
      int nic, unsigned int metric, unsigned int update_id) {
 int distance;
 int new_distance;
 int best_nic;
 node_key node_key;
 node_key.ip = ip;
 node_key.netsize = netsize;
 node * n = node_find_subnet((*state)->head, node_key);
 if(n) distance = find_shortest_nic(n, &best_nic);
 (*state)->head = node_insert((*state)->head, ip, netsize, nic, metric + 1);
 if(!n){
  node * new_node = node_find_subnet((*state)->head, node_key);
  new_distance = find_shortest_nic(new_node, &best_nic);
  if(new_distance == 1000){
   print_advertisement(ip, netsize, -1, new_distance, update_id);
   return;
  }
  print_advertisement(ip, netsize, best_nic, new_distance, update_id );
  return;
 }else{
 new_distance = find_shortest_nic(n, &best_nic);
  if (distance != new_distance){
   if(new_distance == 1000){
    print_advertisement(ip, netsize, -1, new_distance, update_id);
    return;
   }
   print_advertisement(ip, netsize, best_nic, new_distance, update_id );
  }
 }
}

canonical_original_code:
void process_update(struct router_state **state, unsigned int ip, unsigned char netsize, int nic, unsigned int metric, unsigned int update_id) {
 int distance;
 int new_distance;
 int best_nic;
 struct node_key node_key;
 node_key.ip = ip;
 node_key.netsize = netsize;
 struct node *n = node_find_subnet((*state)->head, node_key);
 if(n) distance = find_shortest_nic(n, &best_nic);
 (*state)->head = node_insert((*state)->head, ip, netsize, nic, metric + 1);
 if(!n){
  struct node *new_node = node_find_subnet((*state)->head, node_key);
  new_distance = find_shortest_nic(new_node, &best_nic);
  if(new_distance == 1000){
   print_advertisement(ip, netsize, -1, new_distance, update_id);
   return;
  }
  print_advertisement(ip, netsize, best_nic, new_distance, update_id );
  return;
 }else{
 new_distance = find_shortest_nic(n, &best_nic);
  if (distance != new_distance){
   if(new_distance == 1000){
    print_advertisement(ip, netsize, -1, new_distance, update_id);
    return;
   }
   print_advertisement(ip, netsize, best_nic, new_distance, update_id );
  }
 }
}

variable_types: struct router_state { struct node { struct node *next; struct node_key { unsigned int ip; unsigned char netsize; } key; struct node_value { int nic_distances[16]; } value; } *head; } **

variable_types: distance int


decompiled_code:
__pid_t sigchld_handler()
{
__pid_t result; // eax

do
result = waitpid(-1, 0LL, 1);
while ( result > 0 );
return result;
}

canonical_decompiled_code:
__pid_t func0()
{
__pid_t result; // eax

do
result = waitpid(-1, 0LL, 1);
while ( result > 0 );
return result;
}

original_code:
static void sigchld_handler(int s) {
    while(waitpid(-1, ((void *)0), 1) > 0);
}

variable_types: s int


decompiled_code:
__int64 __fastcall create_server_socket(char *service)
{
int v1; // eax
struct addrinfo *v2; // rbp
int v3; // eax
unsigned int v4; // ebx
const char *v6; // rax
int optval; // [rsp+4h] [rbp-54h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(0LL, service, &req, &pai);
if ( v1 )
{
v6 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v6);
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_11:
fwrite("server: failed to bind\n", 1uLL, 0x17uLL, stderr);
exit(2);
}
while ( 1 )
{
v3 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v4 = v3;
if ( v3 != -1 )
break;
perror("server: socket");
LABEL_10:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_11;
}
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v4, v2->ai_addr, v2->ai_addrlen) == -1 )
{
close(v4);
perror("server: bind");
goto LABEL_10;
}
freeaddrinfo(pai);
if ( listen(v4, 10) == -1 )
{
perror("listen");
exit(1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *service)
{
int v1; // eax
struct addrinfo *v2; // rbp
int v3; // eax
unsigned int v4; // ebx
const char *v6; // rax
int optval; // [rsp+4h] [rbp-54h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(0LL, service, &req, &pai);
if ( v1 )
{
v6 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v6);
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_11:
fwrite("server: failed to bind\n", 1uLL, 0x17uLL, stderr);
exit(2);
}
while ( 1 )
{
v3 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v4 = v3;
if ( v3 != -1 )
break;
perror("server: socket");
LABEL_10:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_11;
}
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v4, v2->ai_addr, v2->ai_addrlen) == -1 )
{
close(v4);
perror("server: bind");
goto LABEL_10;
}
freeaddrinfo(pai);
if ( listen(v4, 10) == -1 )
{
perror("listen");
exit(1);
}
return v4;
}

original_code:
static int create_server_socket(char *port) {
    int lst_socket;
    struct addrinfo hints, *servinfo, *p;
    int yes = 1;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((rv = getaddrinfo(((void *)0), port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((lst_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(lst_socket, 1, 2, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(lst_socket, p->ai_addr, p->ai_addrlen) == -1) {
            close(lst_socket);
            perror("server: bind");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "server: failed to bind\n");
        exit(2);
    }
    freeaddrinfo(servinfo);
    if (listen(lst_socket, 10) == -1) {
        perror("listen");
        exit(1);
    }
    return lst_socket;
}

canonical_original_code:
static int create_server_socket(char *port) {
    int lst_socket;
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
    int yes = 1;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((rv = getaddrinfo(((void *)0), port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((lst_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(lst_socket, 1, 2, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(lst_socket, p->ai_addr, p->ai_addrlen) == -1) {
            close(lst_socket);
            perror("server: bind");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "server: failed to bind\n");
        exit(2);
    }
    freeaddrinfo(servinfo);
    if (listen(lst_socket, 10) == -1) {
        perror("listen");
        exit(1);
    }
    return lst_socket;
}

variable_types:
port
char *

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

variable_types: yes int

variable_types: rv int

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


decompiled_code:
__int64 __fastcall send_string(int fd, __int64 a2, int a3)
{
int v3; // er12
int v4; // ebp
int v5; // ebx
unsigned int v6; // eax
unsigned int v7; // edx

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = a3;
v5 = 0;
while ( 1 )
{
v6 = send(fd, (const void *)(a2 + v5), v4, 0);
v7 = v6;
if ( v6 == -1 )
break;
v5 += v6;
v4 -= v6;
if ( v3 <= v5 )
return 1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2, int a3)
{
int v3; // er12
int v4; // ebp
int v5; // ebx
unsigned int v6; // eax
unsigned int v7; // edx

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = a3;
v5 = 0;
while ( 1 )
{
v6 = send(fd, (const void *)(a2 + v5), v4, 0);
v7 = v6;
if ( v6 == -1 )
break;
v5 += v6;
v4 -= v6;
if ( v3 <= v5 )
return 1;
}
return v7;
}

original_code:
int send_string(int socket, char * string, int size){
 int bytes_remaining = size;
 int bytes_sent = 0;
 while(bytes_sent < size){
  int bytes = send(socket, string + bytes_sent, bytes_remaining, 0);
  if(bytes == -1){
   return -1;
  }
  bytes_sent += bytes;
  bytes_remaining -= bytes;
 }
 return 1;
}

canonical_original_code:
int send_string(int socket, char *string, int size){
 int bytes_remaining = size;
 int bytes_sent = 0;
 while(bytes_sent < size){
  int bytes = send(socket, string + bytes_sent, bytes_remaining, 0);
  if(bytes == -1){
   return -1;
  }
  bytes_sent += bytes;
  bytes_remaining -= bytes;
 }
 return 1;
}

variable_types: socket int

variable_types:
string
char *

variable_types: size int

variable_types: bytes int


main

canonical_name: main

repo: d-j-a-y/Cratyle

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
unsigned int v12; // ebp
unsigned int v13; // eax
unsigned int v14; // er12
__pid_t v15; // eax
unsigned int v16; // ebp
unsigned int v17; // eax
unsigned int v19; // eax
unsigned int v20; // eax
unsigned int v21; // eax
int v22; // er13
int v23; // er12
int v24; // ebp
size_t v25; // rax
size_t v26; // rax
size_t v27; // rax
size_t v28; // rax
size_t v29; // rax
size_t v30; // rax
size_t v31; // rax
size_t v32; // rax
size_t v33; // rax
size_t v34; // rax
int v35; // er14
size_t v36; // rax
size_t v37; // rax
size_t v38; // rax
size_t v39; // rax
size_t v40; // rax
char v41; // [rsp+Dh] [rbp-9ABh] BYREF
char v42; // [rsp+Eh] [rbp-9AAh] BYREF
char v43; // [rsp+Fh] [rbp-9A9h] BYREF
char v44[64]; // [rsp+10h] [rbp-9A8h] BYREF
char s[8]; // [rsp+50h] [rbp-968h] BYREF
__int64 v46; // [rsp+58h] [rbp-960h]
char v47[239]; // [rsp+60h] [rbp-958h] BYREF
char s1[8]; // [rsp+150h] [rbp-868h] BYREF
__int64 v49; // [rsp+158h] [rbp-860h]
char v50[239]; // [rsp+160h] [rbp-858h] BYREF
char v51[8]; // [rsp+250h] [rbp-768h] BYREF
__int64 v52; // [rsp+258h] [rbp-760h]
char v53[239]; // [rsp+260h] [rbp-758h] BYREF
char src[8]; // [rsp+350h] [rbp-668h] BYREF
__int64 v55; // [rsp+358h] [rbp-660h]
char v56[239]; // [rsp+360h] [rbp-658h] BYREF
char v57[8]; // [rsp+450h] [rbp-568h] BYREF
__int64 v58; // [rsp+458h] [rbp-560h]
char v59[239]; // [rsp+460h] [rbp-558h] BYREF
char v60[8]; // [rsp+550h] [rbp-468h] BYREF
__int64 v61; // [rsp+558h] [rbp-460h]
char v62[239]; // [rsp+560h] [rbp-458h] BYREF
char dest[8]; // [rsp+650h] [rbp-368h] BYREF
__int64 v64; // [rsp+658h] [rbp-360h]
char v65[239]; // [rsp+660h] [rbp-358h] BYREF
char buf[8]; // [rsp+750h] [rbp-268h] BYREF
__int64 v67; // [rsp+758h] [rbp-260h]
char v68[239]; // [rsp+760h] [rbp-258h] BYREF
_QWORD v69[2]; // [rsp+850h] [rbp-168h] BYREF
char v70[239]; // [rsp+860h] [rbp-158h] BYREF
int v71[2]; // [rsp+950h] [rbp-68h] BYREF
int v72[2]; // [rsp+958h] [rbp-60h] BYREF
int v73[2]; // [rsp+960h] [rbp-58h] BYREF
int v74[2]; // [rsp+968h] [rbp-50h] BYREF
int v75[2]; // [rsp+970h] [rbp-48h] BYREF
int pipedes[2]; // [rsp+978h] [rbp-40h] BYREF

CratyleInit(argc, argv, envp);
v3 = time(0LL);
srand(v3);
if ( pipe(pipedes) == -1 || pipe(v75) == -1 || pipe(v74) == -1 )
{
perror("pipe failed!");
v5 = 1;
}
else
{
v4 = fcntl(pipedes[0], 3, 0LL);
BYTE1(v4) |= 8u;
v5 = fcntl(pipedes[0], 4, v4);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel1");
}
else
{
v6 = fcntl(v75[0], 3, 0LL);
BYTE1(v6) |= 8u;
v5 = fcntl(v75[0], 4, v6);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel2");
}
else
{
v7 = fcntl(v74[0], 3, 0LL);
BYTE1(v7) |= 8u;
v5 = fcntl(v74[0], 4, v7);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel3");
}
else if ( pipe(v73) == -1 || pipe(v72) == -1 || pipe(v71) == -1 )
{
perror("pipeOut failed!");
v5 = -1;
}
else
{
v8 = fcntl(v73[0], 3, 0LL);
BYTE1(v8) |= 8u;
v5 = fcntl(v73[0], 4, v8);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel1Out");
}
else
{
v9 = fcntl(v72[0], 3, 0LL);
BYTE1(v9) |= 8u;
v5 = fcntl(v72[0], 4, v9);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel2Out");
}
else
{
v10 = fcntl(v71[0], 3, 0LL);
BYTE1(v10) |= 8u;
v5 = fcntl(v71[0], 4, v10);
if ( v5 == -1 )
{
perror("fnctl : fdMinitel3Out");
}
else
{
puts(::s);
v11 = fork();
v12 = v11;
v5 = v11;
if ( v11 == -1 )
{
perror("Minitel1 fork failed!");
}
else if ( v11 )
{
printf("pid Minitel1 %d\n", v11);
sleep(1u);
puts("I am the CRATYLE Serveur");
puts("Cratyle,wait ok");
*(_QWORD *)s = 0LL;
v46 = 0LL;
memset(v47, 0, sizeof(v47));
*(_QWORD *)s1 = 0LL;
v49 = 0LL;
memset(v50, 0, sizeof(v50));
*(_QWORD *)v51 = 0LL;
v52 = 0LL;
memset(v53, 0, sizeof(v53));
*(_QWORD *)src = 0LL;
v55 = 0LL;
memset(v56, 0, sizeof(v56));
*(_QWORD *)v57 = 0LL;
v58 = 0LL;
memset(v59, 0, sizeof(v59));
*(_QWORD *)v60 = 0LL;
v61 = 0LL;
memset(v62, 0, sizeof(v62));
*(_QWORD *)dest = 0LL;
v64 = 0LL;
memset(v65, 0, sizeof(v65));
*(_QWORD *)buf = 0LL;
v67 = 0LL;
memset(v68, 0, sizeof(v68));
v69[0] = 0LL;
v69[1] = 0LL;
memset(v70, 0, sizeof(v70));
v21 = OuvrirConnectionImprimante("/dev/ttyUSB3", v44);
v5 = v21;
if ( v21 == -1 )
{
perror("OuvrirConnectionImprim 1 failed!");
}
else
{
MINITELEnvoyerCaractere(v21, 12LL);
v22 = 0;
v23 = 0;
v24 = 0;
while ( 1 )
{
v35 = STOP;
if ( STOP )
break;
if ( (unsigned int)read(pipedes[0], &v41, 1uLL) != -1 || *_errno_location() != 11 )
{
if ( v24 > 252 || v41 == 10 )
{
s[v24] = 0;
v25 = strlen(s);
printf("CRATYLE : strMinitel1 %s : %i : strlen %i\n", s, (unsigned int)v24, v25);
if ( !strncmp(s, "quitCRATYLE", 0xBuLL) )
{
v26 = strlen(s);
write(v72[1], s, v26);
v27 = strlen(s);
write(v71[1], s, v27);
STOP = 1;
printf(aCratyleMerciPo);
}
else
{
Cratylation(s, src, v5);
strcpy(dest, "Cratyle:");
strcat(dest, src);
v28 = strlen(dest);
write(v72[1], dest, v28);
v29 = strlen(dest);
write(v71[1], dest, v29);
v24 = v35;
}
}
else
{
s[v24++] = v41;
}
}
if ( (unsigned int)read(v75[0], &v42, 1uLL) != -1 || *_errno_location() != 11 )
{
if ( v23 > 252 || v42 == 10 )
{
s1[v23] = 0;
v30 = strlen(s1);
printf("CRATYLE : strMinitel2 %s : %i : strlen %i\n", s1, (unsigned int)v23, v30);
if ( !strncmp(s1, "quitCRATYLE", 0xBuLL) )
{
v31 = strlen(s1);
write(v73[1], s1, v31);
v32 = strlen(s1);
write(v71[1], s1, v32);
STOP = 1;
printf(aCratyleMerciPo);
}
else
{
Cratylation(s1, v57, v5);
strcpy(buf, "Hermogene:");
strcat(buf, v57);
v33 = strlen(buf);
write(v73[1], buf, v33);
v34 = strlen(buf);
write(v71[1], buf, v34);
v23 = v35;
}
}
else
{
s1[v23++] = v42;
}
}
if ( (unsigned int)read(v74[0], &v43, 1uLL) != -1 || *_errno_location() != 11 )
{
if ( v22 > 252 || v43 == 10 )
{
v51[v22] = 0;
v36 = strlen(v51);
printf("CRATYLE : strMinitel3 %s : %i : strlen %i\n", v51, (unsigned int)v22, v36);
if ( !strncmp(v51, "quitCRATYLE", 0xBuLL) )
{
v37 = strlen(v51);
write(v73[1], v51, v37);
v38 = strlen(v51);
write(v72[1], v51, v38);
STOP = 1;
printf(aCratyleMerciPo);
}
else
{
Cratylation(v51, v60, v5);
strcpy((char *)v69, "Socrate:");
strcat((char *)v69, v60);
v39 = strlen((const char *)v69);
write(v73[1], v69, v39);
v40 = strlen((const char *)v69);
write(v72[1], v69, v40);
v22 = v35;
}
}
else
{
v51[v22++] = v43;
}
}
}
if ( v5 )
FermerConnection(v5, v44);
puts("CRATYLE : Attente de le minitel.....");
usleep(0x61A80u);
puts("CRATYLE : Attente de le autre minitel.....");
usleep(0xC3500u);
puts("CRATYLE fin");
v5 = 0;
}
}
else
{
v13 = fork();
v14 = v13;
if ( v13 == -1 )
{
perror("Minitel2 fork failed!");
v5 = -1;
}
else if ( v13 )
{
printf("pid Minitel2 %d\n", v13);
puts("I am Minitel1");
usleep(0x9C4u);
puts("Minitel1, wait ok");
v20 = OuvrirConnectionMinitel("/dev/ttyUSB0", v69);
v5 = v20;
if ( v20 == -1 )
{
perror("OuvrirConnectionMinitel 1 failed!");
}
else
{
InitChat(v20);
OuvrirChat(v5, pipedes, v73, "Cratyle:");
FermerConnection(v5, v69);
write(pipedes[1], "quitCRATYLE", 0xBuLL);
close(pipedes[1]);
close(pipedes[0]);
puts("Minitel1, finito!");
v5 = v12;
}
}
else
{
v15 = fork();
v16 = v15;
v5 = v15;
if ( v15 == -1 )
{
perror("Minitel3 fork failed!");
}
else if ( v15 )
{
puts("I am Minitel2");
usleep(0x9C4u);
puts("Minitel2, wait ok");
v19 = OuvrirConnectionMinitel("/dev/ttyUSB1", v69);
v5 = v19;
if ( v19 == -1 )
{
perror("OuvrirConnectionMinitel 2 failed!");
}
else
{
InitChat(v19);
OuvrirChat(v5, v75, v72, "Hermogene:");
FermerConnection(v5, v69);
close(v75[1]);
close(v75[0]);
puts("Minitel2 exit normal");
v5 = v14;
}
}
else
{
puts("I am Minitel3");
usleep(0x9C4u);
puts("Minitel3, wait ok");
v17 = OuvrirConnectionMinitel("/dev/ttyUSB2", v69);
v5 = v17;
if ( v17 == -1 )
{
perror("OuvrirConnectionMinitel 3 failed!");
}
else
{
InitChat(v17);
puts("Minitel3, ouvrir chat");
OuvrirChat(v5, v74, v71, "Socrate:");
FermerConnection(v5, v69);
close(v74[1]);
close(v74[0]);
puts("Minitel3 exit normal");
v5 = v16;
}
}
}
}
}
}
}
}
}
}
}
return v5;
}

original_code:
int main()
{
    CratyleInit();
 srand(time(((void *)0)));
    pid_t pid_Minitel1, pid_Minitel2, pid_Minitel3;
    int fdMinitel1[2], fdMinitel2[2], fdMinitel3[2];
    int fdMinitel1Out[2], fdMinitel2Out[2], fdMinitel3Out[2];
    if(pipe(fdMinitel1)==-1 || pipe(fdMinitel2)==-1 || pipe(fdMinitel3)==-1)
    {
        perror("pipe failed!");
        return 1;
    }
    int flags = fcntl(fdMinitel1[0], 3, 0);
    if (fcntl (fdMinitel1[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel1");
  return -1;
    }
    flags = fcntl(fdMinitel2[0], 3, 0);
    if (fcntl (fdMinitel2[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel2");
  return -1;
    }
    flags = fcntl(fdMinitel3[0], 3, 0);
    if (fcntl (fdMinitel3[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel3");
  return -1;
    }
    if(pipe(fdMinitel1Out)==-1 || pipe(fdMinitel2Out)==-1 || pipe(fdMinitel3Out)==-1)
    {
        perror("pipeOut failed!");
        return -1;
    }
    flags = fcntl(fdMinitel1Out[0], 3, 0);
    if (fcntl (fdMinitel1Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel1Out");
  return -1;
    }
    flags = fcntl(fdMinitel2Out[0], 3, 0);
    if (fcntl (fdMinitel2Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel2Out");
  return -1;
    }
    flags = fcntl(fdMinitel3Out[0], 3, 0);
    if (fcntl (fdMinitel3Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel3Out");
  return -1;
    }
    printf("Démarrage du programme\n");
    pid_Minitel1=fork();
    if(pid_Minitel1==-1)
    {
        perror("Minitel1 fork failed!");
        return -1;
    }
    if(pid_Minitel1==0)
    {
        pid_Minitel2=fork();
        if(pid_Minitel2==-1)
        {
            perror("Minitel2 fork failed!" );
            return -1;
        }
        if(pid_Minitel2==0)
        {
            pid_Minitel3=fork();
            if(pid_Minitel3==-1)
            {
                perror("Minitel3 fork failed!" );
                return -1;
            }
            if(pid_Minitel3==0)
            {
                printf("I am Minitel3\n");
                usleep(2500);
                printf("Minitel3, wait ok\n");
                int fdMinitel3Modem;
                struct termios oldtioMinitel3;
                if((fdMinitel3Modem = OuvrirConnectionMinitel("/dev/ttyUSB2", &oldtioMinitel3)) == -1)
                {
                    perror("OuvrirConnectionMinitel 3 failed!");
                    return -1;
                }
                InitChat(fdMinitel3Modem);
                printf("Minitel3, ouvrir chat\n");
                OuvrirChat(fdMinitel3Modem, fdMinitel3, fdMinitel3Out, SOCRATE);
                FermerConnection(fdMinitel3Modem, &oldtioMinitel3);
                close(fdMinitel3[1]);
                close(fdMinitel3[0]);
                printf("Minitel3 exit normal\n");
                return 0;
            }
            else
            {
                printf("I am Minitel2\n");
                usleep(2500);
                printf("Minitel2, wait ok\n");
                int fdMinitel2Modem;
                struct termios oldtioMinitel2;
                if((fdMinitel2Modem = OuvrirConnectionMinitel("/dev/ttyUSB1", &oldtioMinitel2)) == -1)
                {
                    perror("OuvrirConnectionMinitel 2 failed!");
                    return -1;
                }
                InitChat(fdMinitel2Modem);
                OuvrirChat(fdMinitel2Modem, fdMinitel2, fdMinitel2Out, HERMOGENE);
                FermerConnection(fdMinitel2Modem, &oldtioMinitel2);
                close(fdMinitel2[1]);
                close(fdMinitel2[0]);
                printf("Minitel2 exit normal\n");
                return 0;
            }
        }
        else
        {
        printf("pid Minitel2 %d\n", pid_Minitel2);
            printf("I am Minitel1\n");
            usleep(2500);
            printf("Minitel1, wait ok\n");
            int fdMinitel1Modem;
            struct termios oldtioMinitel1;
            if((fdMinitel1Modem = OuvrirConnectionMinitel("/dev/ttyUSB0", &oldtioMinitel1)) == -1)
            {
                perror("OuvrirConnectionMinitel 1 failed!");
                return -1;
            }
            InitChat(fdMinitel1Modem);
            OuvrirChat(fdMinitel1Modem, fdMinitel1, fdMinitel1Out, CRATYLE);
            FermerConnection(fdMinitel1Modem, &oldtioMinitel1);
            write(fdMinitel1[1],"quitCRATYLE",strlen("quitCRATYLE"));
            close(fdMinitel1[1]);
            close(fdMinitel1[0]);
            printf("Minitel1, finito!\n");
            return 0;
        }
    }
    else
    {
        printf("pid Minitel1 %d\n", pid_Minitel1);
        sleep(1);
        printf("I am the CRATYLE Serveur\n");
        printf("Cratyle,wait ok\n");
        char bufferM1[1], bufferM2[1], bufferM3[1];
        char strMinitel1[255]= "", strMinitel2[255]= "", strMinitel3[255]= "";
        char strMinitel1Cratyle[255]= "", strMinitel2Cratyle[255]= "", strMinitel3Cratyle[255]= "";
        char strMinitel1CratyleAuteur[255]= "", strMinitel2CratyleAuteur[255]= "", strMinitel3CratyleAuteur[255]= "";
        int iIndexMinitel1, iIndexMinitel2, iIndexMinitel3;
        int iNbCaracteresRecus;
        int iFdImprim1;
        iFdImprim1 = 0;
        struct termios oldtioImprim1;
        if((iFdImprim1 = OuvrirConnectionImprimante("/dev/ttyUSB3", &oldtioImprim1)) == -1)
        {
            perror("OuvrirConnectionImprim 1 failed!");
            return -1;
        }
        MINITELEnvoyerCaractere(iFdImprim1, 0x0C);
        iIndexMinitel1 = iIndexMinitel2 = iIndexMinitel3 = 0;
        while (STOP == 0)
        {
            iNbCaracteresRecus = read(fdMinitel1[0],bufferM1,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM1[0] != 10 && (iIndexMinitel1 + 2) < 255)
                    strMinitel1[iIndexMinitel1++] = bufferM1[0];
                else
                {
                    strMinitel1[iIndexMinitel1] = '\0';
                    printf("CRATYLE : strMinitel1 %s : %i : strlen %i\n", strMinitel1, iIndexMinitel1, strlen(strMinitel1));
                    if (strncmp (strMinitel1, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel2Out[1], strMinitel1, strlen(strMinitel1));
                        write(fdMinitel3Out[1], strMinitel1, strlen(strMinitel1));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel1, strMinitel1Cratyle, iFdImprim1);
                        strcpy(strMinitel1CratyleAuteur, (const char*)CRATYLE);
                        strcat (strMinitel1CratyleAuteur, strMinitel1Cratyle );
                        write(fdMinitel2Out[1], strMinitel1CratyleAuteur, strlen(strMinitel1CratyleAuteur));
                        write(fdMinitel3Out[1], strMinitel1CratyleAuteur, strlen(strMinitel1CratyleAuteur));
                        iIndexMinitel1=0;
                    }
                }
            }
            iNbCaracteresRecus = read(fdMinitel2[0],bufferM2,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM2[0] != 10 && (iIndexMinitel2 + 2) < 255)
                    strMinitel2[iIndexMinitel2++] = bufferM2[0];
                else
                {
                    strMinitel2[iIndexMinitel2] = '\0';
                printf("CRATYLE : strMinitel2 %s : %i : strlen %i\n", strMinitel2, iIndexMinitel2, strlen(strMinitel2));
                    if (strncmp (strMinitel2, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel1Out[1], strMinitel2, strlen(strMinitel2));
                        write(fdMinitel3Out[1], strMinitel2, strlen(strMinitel2));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel2, strMinitel2Cratyle, iFdImprim1);
                        strcpy(strMinitel2CratyleAuteur, (const char*)HERMOGENE);
                        strcat (strMinitel2CratyleAuteur, strMinitel2Cratyle );
                        write(fdMinitel1Out[1], strMinitel2CratyleAuteur, strlen(strMinitel2CratyleAuteur));
                        write(fdMinitel3Out[1], strMinitel2CratyleAuteur, strlen(strMinitel2CratyleAuteur));
                        iIndexMinitel2=0;
                    }
                }
            }
            iNbCaracteresRecus = read(fdMinitel3[0],bufferM3,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM3[0] != 10 && (iIndexMinitel3 + 2) < 255)
                    strMinitel3[iIndexMinitel3++] = bufferM3[0];
                else
                {
                    strMinitel3[iIndexMinitel3] = '\0';
                    printf("CRATYLE : strMinitel3 %s : %i : strlen %i\n", strMinitel3, iIndexMinitel3, strlen(strMinitel3));
                    if (strncmp (strMinitel3, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel1Out[1], strMinitel3, strlen(strMinitel3));
                        write(fdMinitel2Out[1], strMinitel3, strlen(strMinitel3));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel3, strMinitel3Cratyle, iFdImprim1);
                        strcpy(strMinitel3CratyleAuteur, (const char*)SOCRATE);
                        strcat (strMinitel3CratyleAuteur, strMinitel3Cratyle );
                        write(fdMinitel1Out[1], strMinitel3CratyleAuteur, strlen(strMinitel3CratyleAuteur));
                        write(fdMinitel2Out[1], strMinitel3CratyleAuteur, strlen(strMinitel3CratyleAuteur));
                        iIndexMinitel3=0;
                    }
                }
            }
        }
        if (iFdImprim1) FermerConnection(iFdImprim1, &oldtioImprim1 );
        printf("CRATYLE : Attente de le minitel.....\n");
        usleep(400000);
        printf("CRATYLE : Attente de le autre minitel.....\n");
        usleep(800000);
        printf("CRATYLE fin\n");
    }
    return 0;
}

canonical_original_code:
int main()
{
    CratyleInit();
 srand(time(((void *)0)));
    int pid_Minitel1; int pid_Minitel2; int pid_Minitel3;
    int fdMinitel1[2]; int fdMinitel2[2]; int fdMinitel3[2];
    int fdMinitel1Out[2]; int fdMinitel2Out[2]; int fdMinitel3Out[2];
    if(pipe(fdMinitel1)==-1 || pipe(fdMinitel2)==-1 || pipe(fdMinitel3)==-1)
    {
        perror("pipe failed!");
        return 1;
    }
    int flags = fcntl(fdMinitel1[0], 3, 0);
    if (fcntl (fdMinitel1[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel1");
  return -1;
    }
    flags = fcntl(fdMinitel2[0], 3, 0);
    if (fcntl (fdMinitel2[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel2");
  return -1;
    }
    flags = fcntl(fdMinitel3[0], 3, 0);
    if (fcntl (fdMinitel3[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel3");
  return -1;
    }
    if(pipe(fdMinitel1Out)==-1 || pipe(fdMinitel2Out)==-1 || pipe(fdMinitel3Out)==-1)
    {
        perror("pipeOut failed!");
        return -1;
    }
    flags = fcntl(fdMinitel1Out[0], 3, 0);
    if (fcntl (fdMinitel1Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel1Out");
  return -1;
    }
    flags = fcntl(fdMinitel2Out[0], 3, 0);
    if (fcntl (fdMinitel2Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel2Out");
  return -1;
    }
    flags = fcntl(fdMinitel3Out[0], 3, 0);
    if (fcntl (fdMinitel3Out[0], 4, flags | 04000) == -1)
    {
  perror("fnctl : fdMinitel3Out");
  return -1;
    }
    printf("Démarrage du programme\n");
    pid_Minitel1=fork();
    if(pid_Minitel1==-1)
    {
        perror("Minitel1 fork failed!");
        return -1;
    }
    if(pid_Minitel1==0)
    {
        pid_Minitel2=fork();
        if(pid_Minitel2==-1)
        {
            perror("Minitel2 fork failed!" );
            return -1;
        }
        if(pid_Minitel2==0)
        {
            pid_Minitel3=fork();
            if(pid_Minitel3==-1)
            {
                perror("Minitel3 fork failed!" );
                return -1;
            }
            if(pid_Minitel3==0)
            {
                printf("I am Minitel3\n");
                usleep(2500);
                printf("Minitel3, wait ok\n");
                int fdMinitel3Modem;
                struct termios oldtioMinitel3;
                if((fdMinitel3Modem = OuvrirConnectionMinitel("/dev/ttyUSB2", &oldtioMinitel3)) == -1)
                {
                    perror("OuvrirConnectionMinitel 3 failed!");
                    return -1;
                }
                InitChat(fdMinitel3Modem);
                printf("Minitel3, ouvrir chat\n");
                OuvrirChat(fdMinitel3Modem, fdMinitel3, fdMinitel3Out, SOCRATE);
                FermerConnection(fdMinitel3Modem, &oldtioMinitel3);
                close(fdMinitel3[1]);
                close(fdMinitel3[0]);
                printf("Minitel3 exit normal\n");
                return 0;
            }
            else
            {
                printf("I am Minitel2\n");
                usleep(2500);
                printf("Minitel2, wait ok\n");
                int fdMinitel2Modem;
                struct termios oldtioMinitel2;
                if((fdMinitel2Modem = OuvrirConnectionMinitel("/dev/ttyUSB1", &oldtioMinitel2)) == -1)
                {
                    perror("OuvrirConnectionMinitel 2 failed!");
                    return -1;
                }
                InitChat(fdMinitel2Modem);
                OuvrirChat(fdMinitel2Modem, fdMinitel2, fdMinitel2Out, HERMOGENE);
                FermerConnection(fdMinitel2Modem, &oldtioMinitel2);
                close(fdMinitel2[1]);
                close(fdMinitel2[0]);
                printf("Minitel2 exit normal\n");
                return 0;
            }
        }
        else
        {
        printf("pid Minitel2 %d\n", pid_Minitel2);
            printf("I am Minitel1\n");
            usleep(2500);
            printf("Minitel1, wait ok\n");
            int fdMinitel1Modem;
            struct termios oldtioMinitel1;
            if((fdMinitel1Modem = OuvrirConnectionMinitel("/dev/ttyUSB0", &oldtioMinitel1)) == -1)
            {
                perror("OuvrirConnectionMinitel 1 failed!");
                return -1;
            }
            InitChat(fdMinitel1Modem);
            OuvrirChat(fdMinitel1Modem, fdMinitel1, fdMinitel1Out, CRATYLE);
            FermerConnection(fdMinitel1Modem, &oldtioMinitel1);
            write(fdMinitel1[1],"quitCRATYLE",strlen("quitCRATYLE"));
            close(fdMinitel1[1]);
            close(fdMinitel1[0]);
            printf("Minitel1, finito!\n");
            return 0;
        }
    }
    else
    {
        printf("pid Minitel1 %d\n", pid_Minitel1);
        sleep(1);
        printf("I am the CRATYLE Serveur\n");
        printf("Cratyle,wait ok\n");
        char bufferM1[1]; char bufferM2[1]; char bufferM3[1];
        char strMinitel1[255] = ""; char strMinitel2[255] = ""; char strMinitel3[255] = "";
        char strMinitel1Cratyle[255] = ""; char strMinitel2Cratyle[255] = ""; char strMinitel3Cratyle[255] = "";
        char strMinitel1CratyleAuteur[255] = ""; char strMinitel2CratyleAuteur[255] = ""; char strMinitel3CratyleAuteur[255] = "";
        int iIndexMinitel1; int iIndexMinitel2; int iIndexMinitel3;
        int iNbCaracteresRecus;
        int iFdImprim1;
        iFdImprim1 = 0;
        struct termios oldtioImprim1;
        if((iFdImprim1 = OuvrirConnectionImprimante("/dev/ttyUSB3", &oldtioImprim1)) == -1)
        {
            perror("OuvrirConnectionImprim 1 failed!");
            return -1;
        }
        MINITELEnvoyerCaractere(iFdImprim1, 0x0C);
        iIndexMinitel1 = iIndexMinitel2 = iIndexMinitel3 = 0;
        while (STOP == 0)
        {
            iNbCaracteresRecus = read(fdMinitel1[0],bufferM1,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM1[0] != 10 && (iIndexMinitel1 + 2) < 255)
                    strMinitel1[iIndexMinitel1++] = bufferM1[0];
                else
                {
                    strMinitel1[iIndexMinitel1] = '\0';
                    printf("CRATYLE : strMinitel1 %s : %i : strlen %i\n", strMinitel1, iIndexMinitel1, strlen(strMinitel1));
                    if (strncmp (strMinitel1, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel2Out[1], strMinitel1, strlen(strMinitel1));
                        write(fdMinitel3Out[1], strMinitel1, strlen(strMinitel1));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel1, strMinitel1Cratyle, iFdImprim1);
                        strcpy(strMinitel1CratyleAuteur, (char *)CRATYLE);
                        strcat (strMinitel1CratyleAuteur, strMinitel1Cratyle );
                        write(fdMinitel2Out[1], strMinitel1CratyleAuteur, strlen(strMinitel1CratyleAuteur));
                        write(fdMinitel3Out[1], strMinitel1CratyleAuteur, strlen(strMinitel1CratyleAuteur));
                        iIndexMinitel1=0;
                    }
                }
            }
            iNbCaracteresRecus = read(fdMinitel2[0],bufferM2,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM2[0] != 10 && (iIndexMinitel2 + 2) < 255)
                    strMinitel2[iIndexMinitel2++] = bufferM2[0];
                else
                {
                    strMinitel2[iIndexMinitel2] = '\0';
                printf("CRATYLE : strMinitel2 %s : %i : strlen %i\n", strMinitel2, iIndexMinitel2, strlen(strMinitel2));
                    if (strncmp (strMinitel2, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel1Out[1], strMinitel2, strlen(strMinitel2));
                        write(fdMinitel3Out[1], strMinitel2, strlen(strMinitel2));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel2, strMinitel2Cratyle, iFdImprim1);
                        strcpy(strMinitel2CratyleAuteur, (char *)HERMOGENE);
                        strcat (strMinitel2CratyleAuteur, strMinitel2Cratyle );
                        write(fdMinitel1Out[1], strMinitel2CratyleAuteur, strlen(strMinitel2CratyleAuteur));
                        write(fdMinitel3Out[1], strMinitel2CratyleAuteur, strlen(strMinitel2CratyleAuteur));
                        iIndexMinitel2=0;
                    }
                }
            }
            iNbCaracteresRecus = read(fdMinitel3[0],bufferM3,1);
            if (iNbCaracteresRecus == -1 && (*__errno_location ()) == 11)
            {
            }
            else
            {
                if((int)bufferM3[0] != 10 && (iIndexMinitel3 + 2) < 255)
                    strMinitel3[iIndexMinitel3++] = bufferM3[0];
                else
                {
                    strMinitel3[iIndexMinitel3] = '\0';
                    printf("CRATYLE : strMinitel3 %s : %i : strlen %i\n", strMinitel3, iIndexMinitel3, strlen(strMinitel3));
                    if (strncmp (strMinitel3, "quitCRATYLE", strlen("quitCRATYLE")) == 0)
                    {
                        write(fdMinitel1Out[1], strMinitel3, strlen(strMinitel3));
                        write(fdMinitel2Out[1], strMinitel3, strlen(strMinitel3));
                        STOP = 1;
                        printf("CRATYLE : merci pour la conversation.\nà la prochaine.... ");
                    }
                    else
                    {
                        Cratylation(strMinitel3, strMinitel3Cratyle, iFdImprim1);
                        strcpy(strMinitel3CratyleAuteur, (char *)SOCRATE);
                        strcat (strMinitel3CratyleAuteur, strMinitel3Cratyle );
                        write(fdMinitel1Out[1], strMinitel3CratyleAuteur, strlen(strMinitel3CratyleAuteur));
                        write(fdMinitel2Out[1], strMinitel3CratyleAuteur, strlen(strMinitel3CratyleAuteur));
                        iIndexMinitel3=0;
                    }
                }
            }
        }
        if (iFdImprim1) FermerConnection(iFdImprim1, &oldtioImprim1 );
        printf("CRATYLE : Attente de le minitel.....\n");
        usleep(400000);
        printf("CRATYLE : Attente de le autre minitel.....\n");
        usleep(800000);
        printf("CRATYLE fin\n");
    }
    return 0;
}

variable_types: flags int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: iNbCaracteresRecus int


OuvrirConnection

decompiled_code:
int __fastcall OuvrirConnection(int fd, struct termios *a2)
{
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(fd, a2);
*(_QWORD *)&v3.c_iflag = 272LL;
*(_QWORD *)&v3.c_line = 0x100040000000000LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
v3.c_cflag = 2537;
v3.c_lflag = 2;
tcflush(fd, 0);
return tcsetattr(fd, 0, &v3);
}

canonical_decompiled_code:
int __fastcall func0(int fd, struct termios *a2)
{
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(fd, a2);
*(_QWORD *)&v3.c_iflag = 272LL;
*(_QWORD *)&v3.c_line = 0x100040000000000LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
v3.c_cflag = 2537;
v3.c_lflag = 2;
tcflush(fd, 0);
return tcsetattr(fd, 0, &v3);
}

original_code:
void OuvrirConnection (int iFileDescriptor, struct termios* oldtio)
{
    struct termios newtio;
    tcgetattr(iFileDescriptor,oldtio);
    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = 0000011 | 0000040 | 0004000 | 0000200;
    newtio.c_iflag = 0000020 | 0000400;
 newtio.c_cflag |= 0000400;
 newtio.c_cflag |= 0000100;
     newtio.c_oflag = 0;
     newtio.c_lflag = 0000002;
     newtio.c_cc[0] = 0;
     newtio.c_cc[1] = 0;
     newtio.c_cc[2] = 0;
     newtio.c_cc[3] = 0;
     newtio.c_cc[4] = 4;
     newtio.c_cc[5] = 0;
     newtio.c_cc[6] = 1;
     newtio.c_cc[7] = 0;
     newtio.c_cc[8] = 0;
     newtio.c_cc[9] = 0;
     newtio.c_cc[10] = 0;
     newtio.c_cc[11] = 0;
     newtio.c_cc[12] = 0;
     newtio.c_cc[13] = 0;
     newtio.c_cc[14] = 0;
     newtio.c_cc[15] = 0;
     newtio.c_cc[16] = 0;
    tcflush(iFileDescriptor, 0);
    tcsetattr(iFileDescriptor,0,&newtio);
}

canonical_original_code:
void OuvrirConnection(int iFileDescriptor, struct termios *oldtio)
{
    struct termios newtio;
    tcgetattr(iFileDescriptor,oldtio);
    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = 0000011 | 0000040 | 0004000 | 0000200;
    newtio.c_iflag = 0000020 | 0000400;
 newtio.c_cflag |= 0000400;
 newtio.c_cflag |= 0000100;
     newtio.c_oflag = 0;
     newtio.c_lflag = 0000002;
     newtio.c_cc[0] = 0;
     newtio.c_cc[1] = 0;
     newtio.c_cc[2] = 0;
     newtio.c_cc[3] = 0;
     newtio.c_cc[4] = 4;
     newtio.c_cc[5] = 0;
     newtio.c_cc[6] = 1;
     newtio.c_cc[7] = 0;
     newtio.c_cc[8] = 0;
     newtio.c_cc[9] = 0;
     newtio.c_cc[10] = 0;
     newtio.c_cc[11] = 0;
     newtio.c_cc[12] = 0;
     newtio.c_cc[13] = 0;
     newtio.c_cc[14] = 0;
     newtio.c_cc[15] = 0;
     newtio.c_cc[16] = 0;
    tcflush(iFileDescriptor, 0);
    tcsetattr(iFileDescriptor,0,&newtio);
}

variable_types: iFileDescriptor int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } *


OuvrirConnectionMinitel

decompiled_code:
__int64 __fastcall OuvrirConnectionMinitel(const char *a1, struct termios *a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = open(a1, 258);
if ( v2 < 0 )
{
perror("OuvrirConnectionMinitel : strMinitelPort");
v3 = -1;
}
else
{
v3 = v2;
OuvrirConnection(v2, a2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, struct termios *a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = open(a1, 258);
if ( v2 < 0 )
{
perror("OuvrirConnectionMinitel : strMinitelPort");
v3 = -1;
}
else
{
v3 = v2;
func0(v2, a2);
}
return v3;
}

original_code:
int OuvrirConnectionMinitel (char* strMinitelPort, struct termios* oldtio)
{
    int fd;
    fd = open(strMinitelPort, 02 | 0400 );
    if(fd < 0)
 {
  perror("OuvrirConnectionMinitel : strMinitelPort");
  return -1;
 }
    OuvrirConnection(fd, oldtio);
    return fd;
}

canonical_original_code:
int OuvrirConnectionMinitel(char *strMinitelPort, struct termios *oldtio)
{
    int fd;
    fd = open(strMinitelPort, 02 | 0400 );
    if(fd < 0)
 {
  perror("OuvrirConnectionMinitel : strMinitelPort");
  return -1;
 }
    OuvrirConnection(fd, oldtio);
    return fd;
}

variable_types:
strMinitelPort
char *

variable_types: fd int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


OuvrirConnectionImprimante

decompiled_code:
__int64 __fastcall OuvrirConnectionImprimante(const char *a1, struct termios *a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = open(a1, 257);
if ( v2 < 0 )
{
perror("OuvrirConnectionImprimante : strImprimantePort");
v3 = -1;
}
else
{
v3 = v2;
OuvrirConnection(v2, a2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, struct termios *a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = open(a1, 257);
if ( v2 < 0 )
{
perror("OuvrirConnectionImprimante : strImprimantePort");
v3 = -1;
}
else
{
v3 = v2;
func0(v2, a2);
}
return v3;
}

original_code:
int OuvrirConnectionImprimante (char* strImprimantePort, struct termios* oldtio)
{
    int fd;
    fd = open(strImprimantePort, 01 | 0400 );
    if(fd < 0)
 {
  perror("OuvrirConnectionImprimante : strImprimantePort");
  return -1;
 }
    OuvrirConnection(fd, oldtio);
    return fd;
}

canonical_original_code:
int OuvrirConnectionImprimante(char *strImprimantePort, struct termios *oldtio)
{
    int fd;
    fd = open(strImprimantePort, 01 | 0400 );
    if(fd < 0)
 {
  perror("OuvrirConnectionImprimante : strImprimantePort");
  return -1;
 }
    OuvrirConnection(fd, oldtio);
    return fd;
}

variable_types:
strImprimantePort
char *


FermerConnection

decompiled_code:
int __fastcall FermerConnection(int a1, const struct termios *a2)
{
return tcsetattr(a1, 0, a2);
}

canonical_decompiled_code:
int __fastcall func3(int a1, const struct termios *a2)
{
return tcsetattr(a1, 0, a2);
}

original_code:
void FermerConnection (int fd, struct termios* oldtio)
{
    tcsetattr(fd,0,oldtio);
}

canonical_original_code:
void FermerConnection(int fd, struct termios *oldtio)
{
    tcsetattr(fd,0,oldtio);
}


MINITELEnvoyerCaractere

decompiled_code:
void __fastcall MINITELEnvoyerCaractere(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
if ( write(a1, buf, 1uLL) < 0 )
perror("write");
}

canonical_decompiled_code:
void __fastcall func4(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
if ( write(a1, buf, 1uLL) < 0 )
perror("write");
}

original_code:
void MINITELEnvoyerCaractere(int Fichier, unsigned char Caractere)
{
 if(write(Fichier, &Caractere, 1) < 0)
 {
  perror ("write");
 }
}

variable_types: Fichier int

variable_types:
Caractere
unsigned char


MINITELEnvoyerTexte

decompiled_code:
void __fastcall MINITELEnvoyerTexte(int a1, char *a2)
{
char v3; // si
char *v4; // rbx

v3 = *a2;
if ( v3 )
{
v4 = a2 + 1;
do
{
MINITELEnvoyerCaractere(a1, v3);
v3 = *v4++;
}
while ( v3 );
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, char *a2)
{
char v3; // si
char *v4; // rbx

v3 = *a2;
if ( v3 )
{
v4 = a2 + 1;
do
{
func4(a1, v3);
v3 = *v4++;
}
while ( v3 );
}
}

original_code:
void MINITELEnvoyerTexte(int Fichier, const unsigned char Texte[])
{
 int k = 0;
 while(Texte[k] != '\0')
 {
  MINITELEnvoyerCaractere(Fichier, Texte[k]);
  k++;
 }
}

canonical_original_code:
void MINITELEnvoyerTexte(int Fichier, unsigned char Texte[])
{
 int k = 0;
 while(Texte[k] != '\0')
 {
  MINITELEnvoyerCaractere(Fichier, Texte[k]);
  k++;
 }
}

variable_types:
Texte
unsigned char []

variable_types: k int


MINITELCurseurXY

decompiled_code:
void __fastcall MINITELCurseurXY(int a1, char a2, char a3)
{
MINITELEnvoyerCaractere(a1, 31);
MINITELEnvoyerCaractere(a1, a2 + 64);
MINITELEnvoyerCaractere(a1, a3 + 64);
}

canonical_decompiled_code:
void __fastcall func6(int a1, char a2, char a3)
{
func4(a1, 31);
func4(a1, a2 + 64);
func4(a1, a3 + 64);
}

original_code:
void MINITELCurseurXY(int Fichier, unsigned char X, unsigned char Y)
{
 MINITELEnvoyerCaractere(Fichier, 0x1F);
 MINITELEnvoyerCaractere(Fichier, (0x40 + X));
 MINITELEnvoyerCaractere(Fichier, (0x40 + Y));
}

variable_types:
X
unsigned char

variable_types:
Y
unsigned char


MINITELBloquerClavier

decompiled_code:
void __fastcall MINITELBloquerClavier(int a1, int a2)
{
puts("MINITELBloquerClavier");
MINITELEnvoyerCaractere(a1, 27);
MINITELEnvoyerCaractere(a1, 91);
MINITELEnvoyerCaractere(a1, 50);
if ( a2 == 1 )
MINITELEnvoyerCaractere(a1, 104);
else
MINITELEnvoyerCaractere(a1, 108);
}

canonical_decompiled_code:
void __fastcall func7(int a1, int a2)
{
puts("MINITELBloquerClavier");
func4(a1, 27);
func4(a1, 91);
func4(a1, 50);
if ( a2 == 1 )
func4(a1, 104);
else
func4(a1, 108);
}

original_code:
void MINITELBloquerClavier(int Fichier, int iBloque)
{
    printf("MINITELBloquerClavier\n");
 MINITELEnvoyerCaractere(Fichier, 0x1B);
 MINITELEnvoyerCaractere(Fichier, 0x5b);
 MINITELEnvoyerCaractere(Fichier, 0x32);
    if (iBloque == 1)
        MINITELEnvoyerCaractere(Fichier, 0x68);
    else
        MINITELEnvoyerCaractere(Fichier, 0x6c);
}

variable_types: iBloque int


MINITELChangerAttribut

decompiled_code:
void __fastcall MINITELChangerAttribut(int a1, char a2)
{
MINITELEnvoyerCaractere(a1, 27);
MINITELEnvoyerCaractere(a1, a2);
}

canonical_decompiled_code:
void __fastcall func8(int a1, char a2)
{
func4(a1, 27);
func4(a1, a2);
}

original_code:
void MINITELChangerAttribut(int Fichier, unsigned char Commande)
{
 MINITELEnvoyerCaractere(Fichier, 0x1B);
 MINITELEnvoyerCaractere(Fichier, Commande);
}

variable_types:
Commande
unsigned char


InitChat

decompiled_code:
void __fastcall InitChat(int a1)
{
int v1; // ebx

puts(aVidageDeL);
MINITELEnvoyerCaractere(a1, 12);
puts(" -");
puts("Intro...");
MINITELCurseurXY(a1, 1, 1);
MINITELCurseurXY(a1, 2, 1);
MINITELChangerAttribut(a1, 79);
MINITELEnvoyerTexte(a1, "// (ratyle //");
MINITELChangerAttribut(a1, 76);
MINITELCurseurXY(a1, 3, 1);
v1 = 40;
do
{
MINITELEnvoyerCaractere(a1, 95);
--v1;
}
while ( v1 );
}

original_code:
void InitChat (int iFdMinitel)
{
 printf ("Vidage de l'écran...\n");
 MINITELEnvoyerCaractere(iFdMinitel, 0x0C);
 printf (" -\n");
 printf ("Intro...\n");
 MINITELCurseurXY(iFdMinitel, 1, 1);
 int k =0;
   MINITELCurseurXY(iFdMinitel, 2, 1);
 MINITELChangerAttribut(iFdMinitel, 0x4F);
 MINITELEnvoyerTexte(iFdMinitel, (unsigned char*)"// (ratyle //");
 MINITELChangerAttribut(iFdMinitel, 0x4C);
 MINITELCurseurXY(iFdMinitel, 3, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMinitel, '_');
    }
}

canonical_original_code:
void InitChat(int iFdMinitel)
{
 printf ("Vidage de l'écran...\n");
 MINITELEnvoyerCaractere(iFdMinitel, 0x0C);
 printf (" -\n");
 printf ("Intro...\n");
 MINITELCurseurXY(iFdMinitel, 1, 1);
 int k = 0;
   MINITELCurseurXY(iFdMinitel, 2, 1);
 MINITELChangerAttribut(iFdMinitel, 0x4F);
 MINITELEnvoyerTexte(iFdMinitel, (unsigned char *)"// (ratyle //");
 MINITELChangerAttribut(iFdMinitel, 0x4C);
 MINITELCurseurXY(iFdMinitel, 3, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMinitel, '_');
    }
}

variable_types: iFdMinitel int


AfficherAttenteCratyle

decompiled_code:
void __fastcall AfficherAttenteCratyle(int a1)
{
int v1; // ebp
int v2; // ebp
int v3; // ebp

puts("AfficherAttenteCratyle");
MINITELCurseurXY(a1, 21, 1);
v1 = 40;
do
{
MINITELEnvoyerCaractere(a1, 95);
--v1;
}
while ( v1 );
MINITELCurseurXY(a1, 22, 1);
MINITELEnvoyerTexte(a1, " Entrez votre message : ");
MINITELCurseurXY(a1, 23, 1);
v2 = 40;
do
{
MINITELEnvoyerCaractere(a1, 32);
--v2;
}
while ( v2 );
MINITELCurseurXY(a1, 24, 1);
v3 = 40;
do
{
MINITELEnvoyerCaractere(a1, 32);
--v3;
}
while ( v3 );
MINITELCurseurXY(a1, 23, 3);
MINITELEnvoyerCaractere(a1, 62);
MINITELEnvoyerCaractere(a1, 32);
MINITELEnvoyerCaractere(a1, 17);
}

original_code:
void AfficherAttenteCratyle(int iFdMini)
{
    int k;
    printf("AfficherAttenteCratyle\n");
    MINITELCurseurXY(iFdMini, 21, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
    MINITELCurseurXY(iFdMini, 22, 1);
    MINITELEnvoyerTexte(iFdMini, (const unsigned char*)"  Entrez votre message :          ");
    MINITELCurseurXY(iFdMini, 23, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, ' ');
    }
    MINITELCurseurXY(iFdMini, 24, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, ' ');
    }
    MINITELCurseurXY(iFdMini, 23, 3);
    MINITELEnvoyerCaractere(iFdMini, '>');
    MINITELEnvoyerCaractere(iFdMini, ' ');
    MINITELEnvoyerCaractere(iFdMini, 0x11);
}

canonical_original_code:
void AfficherAttenteCratyle(int iFdMini)
{
    int k;
    printf("AfficherAttenteCratyle\n");
    MINITELCurseurXY(iFdMini, 21, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
    MINITELCurseurXY(iFdMini, 22, 1);
    MINITELEnvoyerTexte(iFdMini, (unsigned char *)"  Entrez votre message :          ");
    MINITELCurseurXY(iFdMini, 23, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, ' ');
    }
    MINITELCurseurXY(iFdMini, 24, 1);
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, ' ');
    }
    MINITELCurseurXY(iFdMini, 23, 3);
    MINITELEnvoyerCaractere(iFdMini, '>');
    MINITELEnvoyerCaractere(iFdMini, ' ');
    MINITELEnvoyerCaractere(iFdMini, 0x11);
}

variable_types: iFdMini int


AfficherAurevoirCratyle

decompiled_code:
void __fastcall AfficherAurevoirCratyle(unsigned int a1)
{
int v1; // ebx
int v2; // ebx

printf("AfficherAurevoirCratyle : %i\n", a1);
MINITELEnvoyerCaractere(a1, 12);
MINITELCurseurXY(a1, 1, 1);
v1 = 40;
do
{
MINITELEnvoyerCaractere(a1, 95);
--v1;
}
while ( v1 );
MINITELChangerAttribut(a1, 79);
MINITELEnvoyerTexte(a1, "*CratYle StOP*");
v2 = 45;
do
{
MINITELEnvoyerCaractere(a1, 95);
--v2;
}
while ( v2 );
}

canonical_decompiled_code:
void __fastcall func11(unsigned int a1)
{
int v1; // ebx
int v2; // ebx

printf("AfficherAurevoirCratyle : %i\n", a1);
func4(a1, 12);
func6(a1, 1, 1);
v1 = 40;
do
{
func4(a1, 95);
--v1;
}
while ( v1 );
func8(a1, 79);
func5(a1, "*CratYle StOP*");
v2 = 45;
do
{
func4(a1, 95);
--v2;
}
while ( v2 );
}

original_code:
void AfficherAurevoirCratyle(int iFdMini)
{
    printf ("AfficherAurevoirCratyle : %i\n", iFdMini);
    MINITELEnvoyerCaractere(iFdMini, 0x0C);
    MINITELCurseurXY(iFdMini, 1, 1);
    int k=0;
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
    MINITELChangerAttribut(iFdMini, 0x4F);
    MINITELEnvoyerTexte(iFdMini, (unsigned char*) "*CratYle StOP*");
    for(k = 0; k < 45; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
}

canonical_original_code:
void AfficherAurevoirCratyle(int iFdMini)
{
    printf ("AfficherAurevoirCratyle : %i\n", iFdMini);
    MINITELEnvoyerCaractere(iFdMini, 0x0C);
    MINITELCurseurXY(iFdMini, 1, 1);
    int k = 0;
    for(k = 0; k < 40; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
    MINITELChangerAttribut(iFdMini, 0x4F);
    MINITELEnvoyerTexte(iFdMini, (unsigned char *) "*CratYle StOP*");
    for(k = 0; k < 45; k++)
    {
        MINITELEnvoyerCaractere(iFdMini, '_');
    }
}