decompiled_code:
__int64 __fastcall read_search(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 result; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v4 = a2;
while ( 1 )
{
result = get_next_line(0LL, &v4);
if ( (int)result <= 0 )
break;
if ( find_value(a1, v4) )
{
v2 = find_value(a1, v4);
ft_putstr(v2);
}
else
{
ft_putstr(v4);
ft_putstr(": Not found.");
}
ft_putstr("\n");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 result; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v4 = a2;
while ( 1 )
{
result = get_next_line(0LL, &v4);
if ( (int)result <= 0 )
break;
if ( find_value(a1, v4) )
{
v2 = find_value(a1, v4);
ft_putstr(v2);
}
else
{
ft_putstr(v4);
ft_putstr(": Not found.");
}
ft_putstr("\n");
}
return result;
}

original_code:
void read_search(t_search *head, char *line)
{
 while (get_next_line(0, &line) > 0)
 {
  if (find_value(head, line) == ((void *)0))
  {
   ft_putstr(line);
   ft_putstr(": Not found.");
  }
  else
   ft_putstr(find_value(head, line));
  ft_putstr("\n");
 }
}

canonical_original_code:
void read_search(struct s_search *head, char *line)
{
 while (get_next_line(0, &line) > 0)
 {
  if (find_value(head, line) == ((void *)0))
  {
   ft_putstr(line);
   ft_putstr(": Not found.");
  }
  else
   ft_putstr(find_value(head, line));
  ft_putstr("\n");
 }
}

variable_types: struct s_search { char *keyword; char *value; int nb_next; short is_leef; struct s_search *before; struct s_search **next; } *

variable_types:
line
char *


decompiled_code:
__int64 __fastcall define_thehead(__int64 a1, __int64 a2, void *a3)
{
char v4; // bl
__int64 v5; // rax
__int64 result; // rax
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v4 = 0;
v7[0] = a2;
while ( 1 )
{
result = get_next_line(0LL, v7);
if ( (int)result <= 0 )
break;
if ( (unsigned int)ft_strcmp(v7[0], &unk_25F) )
{
if ( (v4 & 1) != 0 )
{
v5 = new_t_search(a3);
insert_new(a1, v5);
free(a3);
}
else
{
a3 = (void *)ft_strdup(v7[0]);
}
}
else
{
read_search(a1, v7[0]);
}
++v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, void *a3)
{
char v4; // bl
__int64 v5; // rax
__int64 result; // rax
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v4 = 0;
v7[0] = a2;
while ( 1 )
{
result = get_next_line(0LL, v7);
if ( (int)result <= 0 )
break;
if ( (unsigned int)ft_strcmp(v7[0], &unk_25F) )
{
if ( (v4 & 1) != 0 )
{
v5 = new_t_search(a3);
insert_new(a1, v5);
free(a3);
}
else
{
a3 = (void *)ft_strdup(v7[0]);
}
}
else
{
func0(a1, v7[0]);
}
++v4;
}
return result;
}

original_code:
void define_thehead(t_search *head, char *line, char *key)
{
 int i;
 i = 0;
 while (get_next_line(0, &line) > 0)
 {
  if (ft_strcmp(line, "") == 0)
   read_search(head, line);
  else if ((i % 2) != 0)
  {
   insert_new(head , new_t_search(key, line));
   free(key);
  }
  else
   key = ft_strdup(line);
  i++;
 }
}

canonical_original_code:
void define_thehead(struct s_search *head, char *line, char *key)
{
 int i;
 i = 0;
 while (get_next_line(0, &line) > 0)
 {
  if (ft_strcmp(line, "") == 0)
   read_search(head, line);
  else if ((i % 2) != 0)
  {
   insert_new(head , new_t_search(key, line));
   free(key);
  }
  else
   key = ft_strdup(line);
  i++;
 }
}

variable_types:
key
char *

variable_types: i int

return_type: void

user_defined_types: struct s_search { char *keyword; char *value; int nb_next; short is_leef; struct s_search *before; struct s_search **next; }


decompiled_code:
__int64 read_standard()
{
__int64 v0; // rax
void *v1; // r12
__int64 v2; // rbx
__int64 v3; // rdi
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = new_t_search(0LL);
*(_WORD *)(v0 + 20) = 1;
the_head = v0;
get_next_line(0LL, v5);
v1 = (void *)ft_strdup(v5[0]);
get_next_line(0LL, v5);
v2 = new_t_search(v1);
insert_new(the_head, v2);
v3 = the_head;
*(_WORD *)(the_head + 20) = 0;
*(_WORD *)(v2 + 20) = 1;
return define_thehead(v3, v5[0], v1);
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
void *v1; // r12
__int64 v2; // rbx
__int64 v3; // rdi
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = new_t_search(0LL);
*(_WORD *)(v0 + 20) = 1;
the_head = v0;
get_next_line(0LL, v5);
v1 = (void *)ft_strdup(v5[0]);
get_next_line(0LL, v5);
v2 = new_t_search(v1);
insert_new(the_head, v2);
v3 = the_head;
*(_WORD *)(the_head + 20) = 0;
*(_WORD *)(v2 + 20) = 1;
return func1(v3, v5[0], v1);
}

original_code:
void read_standard()
{
 char *line;
 char *key;
 t_search *head;
 head = new_t_search(((void *)0), ((void *)0));
 the_head = head;
 the_head->is_leef = 1;
 get_next_line(0, &line);
 key = ft_strdup(line);
 get_next_line(0, &line);
 head = new_t_search(key, line);
 insert_new(the_head, head);
 the_head->is_leef = 0;
 head->is_leef = 1;
 define_thehead(the_head, line, key);
}

canonical_original_code:
void read_standard()
{
 char *line;
 char *key;
 struct s_search *head;
 head = new_t_search(((void *)0), ((void *)0));
 the_head = head;
 the_head->is_leef = 1;
 get_next_line(0, &line);
 key = ft_strdup(line);
 get_next_line(0, &line);
 head = new_t_search(key, line);
 insert_new(the_head, head);
 the_head->is_leef = 0;
 head->is_leef = 1;
 define_thehead(the_head, line, key);
}


decompiled_code:
__int64 __fastcall print_t_search(__int64 a1)
{
__int64 v2; // rbx
_QWORD *v3; // rax
__int64 result; // rax
__int64 v5; // rbx
__int64 v6; // rdi

ft_putstr(*(_QWORD *)a1);
ft_putstr(" ");
ft_putstr(*(_QWORD *)(a1 + 8));
ft_putchar(10LL);
if ( *(int *)(a1 + 16) > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * v2++);
ft_putstr(*v3);
ft_putchar(32LL);
}
while ( *(_DWORD *)(a1 + 16) > (int)v2 );
}
ft_putstr("\n\n\n");
result = *(unsigned int *)(a1 + 16);
if ( (int)result > 0 )
{
v5 = 0LL;
do
{
v6 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8 * v5++);
result = print_t_search(v6);
}
while ( *(_DWORD *)(a1 + 16) > (int)v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v2; // rbx
_QWORD *v3; // rax
__int64 result; // rax
__int64 v5; // rbx
__int64 v6; // rdi

ft_putstr(*(_QWORD *)a1);
ft_putstr(" ");
ft_putstr(*(_QWORD *)(a1 + 8));
ft_putchar(10LL);
if ( *(int *)(a1 + 16) > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * v2++);
ft_putstr(*v3);
ft_putchar(32LL);
}
while ( *(_DWORD *)(a1 + 16) > (int)v2 );
}
ft_putstr("\n\n\n");
result = *(unsigned int *)(a1 + 16);
if ( (int)result > 0 )
{
v5 = 0LL;
do
{
v6 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8 * v5++);
result = func3(v6);
}
while ( *(_DWORD *)(a1 + 16) > (int)v5 );
}
return result;
}

original_code:
void print_t_search(t_search *head)
{
 int i;
 ft_putstr(head->keyword);
 ft_putstr("    ");
 ft_putstr(head->value);
 ft_putchar('\n');
 i = 0;
 while (i < head->nb_next)
 {
  ft_putstr((head->next[i])->keyword);
  ft_putchar(' ');
  i++;
 }
 ft_putstr("\n\n\n");
 i = 0;
 while (i < head->nb_next)
 {
  print_t_search(head->next[i]);
  i++;
 }
}

canonical_original_code:
void print_t_search(struct s_search *head)
{
 int i;
 ft_putstr(head->keyword);
 ft_putstr("    ");
 ft_putstr(head->value);
 ft_putchar('\n');
 i = 0;
 while (i < head->nb_next)
 {
  ft_putstr((head->next[i])->keyword);
  ft_putchar(' ');
  i++;
 }
 ft_putstr("\n\n\n");
 i = 0;
 while (i < head->nb_next)
 {
  print_t_search(head->next[i]);
  i++;
 }
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
read_standard(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func2(argc, argv, envp);
return 0;
}

original_code:
int main()
{
 read_standard();
 return (0);
}

return_type: int


decompiled_code:
__int64 __fastcall ft_strcmp(_BYTE *a1, _BYTE *a2)
{
int v2; // edx
int v3; // ecx
__int64 v4; // rax

if ( !a1 || !a2 )
return 1LL;
v2 = (char)*a1;
v3 = (char)*a2;
v4 = 1LL;
if ( *a1 != *a2 )
return (unsigned int)(v2 - v3);
while ( 1 )
{
if ( !(_BYTE)v2 )
return (unsigned int)-(char)v3;
if ( !(_BYTE)v3 )
break;
v2 = (char)a1[v4++];
v3 = (char)a2[v4 - 1];
if ( (_BYTE)v2 != a2[v4 - 1] )
return (unsigned int)(v2 - v3);
}
return (unsigned int)(char)v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, _BYTE *a2)
{
int v2; // edx
int v3; // ecx
__int64 v4; // rax

if ( !a1 || !a2 )
return 1LL;
v2 = (char)*a1;
v3 = (char)*a2;
v4 = 1LL;
if ( *a1 != *a2 )
return (unsigned int)(v2 - v3);
while ( 1 )
{
if ( !(_BYTE)v2 )
return (unsigned int)-(char)v3;
if ( !(_BYTE)v3 )
break;
v2 = (char)a1[v4++];
v3 = (char)a2[v4 - 1];
if ( (_BYTE)v2 != a2[v4 - 1] )
return (unsigned int)(v2 - v3);
}
return (unsigned int)(char)v2;
}

original_code:
int ft_strcmp(const char *s1, const char *s2)
{
 int count;
 count = 0;
 if (s1 == ((void *)0) || s2 == ((void *)0))
  return (1);
 while (s1[count] == s2[count] && s1[count] != 0 && s2[count] != 0)
  count++;
 return (s1[count] - s2[count]);
}

canonical_original_code:
int ft_strcmp(char *s1, char *s2)
{
 int count;
 count = 0;
 if (s1 == ((void *)0) || s2 == ((void *)0))
  return (1);
 while (s1[count] == s2[count] && s1[count] != 0 && s2[count] != 0)
  count++;
 return (s1[count] - s2[count]);
}

variable_types: count int


decompiled_code:
void *__fastcall move_elem(__int64 a1, int a2, int a3)
{
void *result; // rax

if ( a3 > a2 )
result = memmove(
(void *)(-8LL * (unsigned int)(a3 - a2 - 1) + 8LL * a3 + a1),
(const void *)(a1 + 8LL * a3 - 8LL * (unsigned int)(a3 - a2 - 1) - 8),
8LL * (unsigned int)(a3 - a2 - 1) + 8);
return result;
}

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

if ( a3 > a2 )
result = memmove(
(void *)(-8LL * (unsigned int)(a3 - a2 - 1) + 8LL * a3 + a1),
(const void *)(a1 + 8LL * a3 - 8LL * (unsigned int)(a3 - a2 - 1) - 8),
8LL * (unsigned int)(a3 - a2 - 1) + 8);
return result;
}

original_code:
void move_elem(t_search **tab, int i, int nb)
{
 while (nb > i)
 {
  tab[nb] = tab[nb - 1];
  nb--;
 }
}

canonical_original_code:
void move_elem(struct s_search **tab, int i, int nb)
{
 while (nb > i)
 {
  tab[nb] = tab[nb - 1];
  nb--;
 }
}

variable_types: struct s_search { char *keyword; char *value; int nb_next; short is_leef; struct s_search *before; struct s_search **next; } **

variable_types: nb int


decompiled_code:
__int64 __fastcall reduce_t_search(__int64 a1)
{
char *v1; // rax
_QWORD *v3; // rsi
_BYTE *v4; // rdi
signed __int64 v5; // rax
char *v6; // rsi
__int64 result; // rax

v1 = *(char **)(a1 + 32);
v3 = v1 + 1208;
v4 = (_BYTE *)((unsigned __int64)(v1 + 8) & 0xFFFFFFFFFFFFFFF8LL);
*(_QWORD *)v1 = *((_QWORD *)v1 + 151);
*((_QWORD *)v1 + 149) = *((_QWORD *)v1 + 300);
v5 = v1 - v4;
v6 = (char *)v3 - v5;
result = (unsigned int)(v5 + 1200) >> 3;
qmemcpy(v4, v6, 8LL * (unsigned int)result);
*(_DWORD *)(a1 + 16) = 150;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
char *v1; // rax
_QWORD *v3; // rsi
_BYTE *v4; // rdi
signed __int64 v5; // rax
char *v6; // rsi
__int64 result; // rax

v1 = *(char **)(a1 + 32);
v3 = v1 + 1208;
v4 = (_BYTE *)((unsigned __int64)(v1 + 8) & 0xFFFFFFFFFFFFFFF8LL);
*(_QWORD *)v1 = *((_QWORD *)v1 + 151);
*((_QWORD *)v1 + 149) = *((_QWORD *)v1 + 300);
v5 = v1 - v4;
v6 = (char *)v3 - v5;
result = (unsigned int)(v5 + 1200) >> 3;
qmemcpy(v4, v6, 8LL * (unsigned int)result);
*(_DWORD *)(a1 + 16) = 150;
return result;
}

original_code:
void reduce_t_search(t_search *head)
{
 int i;
 i = 0;
 while (i < 301 / 2)
 {
  head->next[i] = head->next[i + 301 / 2 + 1];
  i++;
 }
 head->nb_next = 301 / 2;
}

canonical_original_code:
void reduce_t_search(struct s_search *head)
{
 int i;
 i = 0;
 while (i < 301 / 2)
 {
  head->next[i] = head->next[i + 301 / 2 + 1];
  i++;
 }
 head->nb_next = 301 / 2;
}


decompiled_code:
__int64 __fastcall split_t_search(__int64 a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

sub_t_search(a1, &v2);
reduce_t_search(a1);
return insert_tab(*(_QWORD *)(a1 + 24), v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

sub_t_search(a1, &v2);
func2(a1);
return insert_tab(*(_QWORD *)(a1 + 24), v2);
}

original_code:
void split_t_search(t_search *head)
{
 t_search *t1;
 sub_t_search(head, &t1);
 reduce_t_search(head);
 insert_tab(head->before, t1);
}

canonical_original_code:
void split_t_search(struct s_search *head)
{
 struct s_search *t1;
 sub_t_search(head, &t1);
 reduce_t_search(head);
 insert_tab(head->before, t1);
}


decompiled_code:
__int64 __fastcall insert_tab(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
__int64 i; // rbx
__int64 v4; // r14
int v5[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = *(unsigned int *)(a1 + 16);
v5[0] = 1;
if ( (_DWORD)result == 301 )
{
split_t_search();
insert_new(the_head, a2);
v5[0] = 0;
result = *(unsigned int *)(a1 + 16);
}
for ( i = 0LL; (int)result >= (int)i; result = *(unsigned int *)(a1 + 16) )
{
while ( 1 )
{
if ( !v5[0] )
return result;
if ( (_DWORD)result != (_DWORD)i )
{
v4 = 8 * i;
if ( (int)ft_strcmp(*a2, **(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * i)) >= 0 )
break;
}
sub_insert(a1, a2, (unsigned int)i, v5);
LABEL_6:
result = *(unsigned int *)(a1 + 16);
if ( (int)result < (int)++i )
return result;
}
if ( (unsigned int)ft_strcmp(*a2, **(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * i)) )
goto LABEL_6;
++i;
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 32) + v4) + 8LL) = a2[1];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
__int64 i; // rbx
__int64 v4; // r14
int v5[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = *(unsigned int *)(a1 + 16);
v5[0] = 1;
if ( (_DWORD)result == 301 )
{
split_t_search();
func1(the_head, a2);
v5[0] = 0;
result = *(unsigned int *)(a1 + 16);
}
for ( i = 0LL; (int)result >= (int)i; result = *(unsigned int *)(a1 + 16) )
{
while ( 1 )
{
if ( !v5[0] )
return result;
if ( (_DWORD)result != (_DWORD)i )
{
v4 = 8 * i;
if ( (int)ft_strcmp(*a2, **(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * i)) >= 0 )
break;
}
sub_insert(a1, a2, (unsigned int)i, v5);
LABEL_6:
result = *(unsigned int *)(a1 + 16);
if ( (int)result < (int)++i )
return result;
}
if ( (unsigned int)ft_strcmp(*a2, **(_QWORD **)(*(_QWORD *)(a1 + 32) + 8 * i)) )
goto LABEL_6;
++i;
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 32) + v4) + 8LL) = a2[1];
}
return result;
}

original_code:
void insert_tab(t_search *head, t_search *new)
{
 int i;
 int out;
 i = 0;
 out = 1;
 if (head->nb_next == 301 && out)
 {
  split_t_search(head);
  insert_new(the_head, new);
  out = 0;
 }
 while (i <= head->nb_next && out)
 {
  if (i == head->nb_next
   || ft_strcmp(new->keyword, (head->next[i])->keyword) < 0)
   sub_insert(head, new, i, &out);
  else if (ft_strcmp(new->keyword, (head->next[i])->keyword) == 0)
   (head->next[i])->value = new->value;
  i++;
 }
}

canonical_original_code:
void insert_tab(struct s_search *head, struct s_search *new)
{
 int i;
 int out;
 i = 0;
 out = 1;
 if (head->nb_next == 301 && out)
 {
  split_t_search(head);
  insert_new(the_head, new);
  out = 0;
 }
 while (i <= head->nb_next && out)
 {
  if (i == head->nb_next
   || ft_strcmp(new->keyword, (head->next[i])->keyword) < 0)
   sub_insert(head, new, i, &out);
  else if (ft_strcmp(new->keyword, (head->next[i])->keyword) == 0)
   (head->next[i])->value = new->value;
  i++;
 }
}

variable_types:
new
struct s_search *

variable_types: out int


decompiled_code:
void __fastcall insert_new(_QWORD *a1, _QWORD *a2)
{
while ( 1 )
{
if ( !(unsigned int)ft_strcmp(*a1, *a2) )
{
free((void *)a1[1]);
a1[1] = a2[1];
free(a2);
return;
}
if ( *((_WORD *)a1 + 10) == 1 )
{
insert_tab((__int64)a1, a2);
return;
}
if ( (int)ft_strcmp(*a1, *a2) >= 0 )
break;
swap_t_search(a1, a2);
}
what_branch(a1, a2);
}

canonical_decompiled_code:
void __fastcall func1(_QWORD *a1, _QWORD *a2)
{
while ( 1 )
{
if ( !(unsigned int)ft_strcmp(*a1, *a2) )
{
free((void *)a1[1]);
a1[1] = a2[1];
free(a2);
return;
}
if ( *((_WORD *)a1 + 10) == 1 )
{
func0((__int64)a1, a2);
return;
}
if ( (int)ft_strcmp(*a1, *a2) >= 0 )
break;
swap_t_search(a1, a2);
}
func2(a1, a2);
}

original_code:
void insert_new(t_search *head, t_search *new)
{
 if (ft_strcmp(head->keyword, new->keyword) == 0)
 {
  free(head->value);
  head->value = new->value;
  free(new);
 }
 else if (head->is_leef == 1)
  insert_tab(head, new);
 else if (ft_strcmp(head->keyword, new->keyword) < 0)
 {
  swap_t_search(head, new);
  insert_new(head, new);
 }
 else
 {
  what_branch(head, new);
 }
}

canonical_original_code:
void insert_new(struct s_search *head, struct s_search *new)
{
 if (ft_strcmp(head->keyword, new->keyword) == 0)
 {
  free(head->value);
  head->value = new->value;
  free(new);
 }
 else if (head->is_leef == 1)
  insert_tab(head, new);
 else if (ft_strcmp(head->keyword, new->keyword) < 0)
 {
  swap_t_search(head, new);
  insert_new(head, new);
 }
 else
 {
  what_branch(head, new);
 }
}


original_code:
void what_branch(t_search *head, t_search *new)
{
 int i;
 i = 0;
 while (i < head->nb_next)
 {
  if (ft_strcmp(((head->next)[i])->keyword,
     new->keyword) >= 0)
  {
   insert_new(head->next[i], new);
   i = head->nb_next + 1;
  }
  i++;
 }
 if (i == head->nb_next)
 {
  swap_t_search(head->next[i - 1], new);
  insert_new(head, new);
 }
}

canonical_original_code:
void what_branch(struct s_search *head, struct s_search *new)
{
 int i;
 i = 0;
 while (i < head->nb_next)
 {
  if (ft_strcmp(((head->next)[i])->keyword,
     new->keyword) >= 0)
  {
   insert_new(head->next[i], new);
   i = head->nb_next + 1;
  }
  i++;
 }
 if (i == head->nb_next)
 {
  swap_t_search(head->next[i - 1], new);
  insert_new(head, new);
 }
}


original_code:
void sub_t_search(t_search *head, t_search **t1)
{
 int i;
 i = 0;
 *t1 = new_t_search(head->next[head->nb_next / 2]->keyword,
  head->next[head->nb_next / 2]->value);
 (*t1)->before = head->before;
 (*t1)->is_leef = 1;
 while (i < head->nb_next / 2)
 {
  (*t1)->next[i] = head->next[i];
  (*t1)->nb_next++;
  i++;
 }
}

canonical_original_code:
void sub_t_search(struct s_search *head, struct s_search **t1)
{
 int i;
 i = 0;
 *t1 = new_t_search(head->next[head->nb_next / 2]->keyword,
  head->next[head->nb_next / 2]->value);
 (*t1)->before = head->before;
 (*t1)->is_leef = 1;
 while (i < head->nb_next / 2)
 {
  (*t1)->next[i] = head->next[i];
  (*t1)->nb_next++;
  i++;
 }
}


decompiled_code:
const char *__fastcall ft_putstr(const char *a1)
{
const char *v1; // rbx
const char *result; // rax
_BYTE *v3; // rbx

v1 = a1;
result = "NULL";
if ( !a1 )
v1 = "NULL";
if ( *v1 )
{
v3 = v1 + 1;
do
result = (const char *)ft_putchar();
while ( *v3++ );
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(const char *a1)
{
const char *v1; // rbx
const char *result; // rax
_BYTE *v3; // rbx

v1 = a1;
result = "NULL";
if ( !a1 )
v1 = "NULL";
if ( *v1 )
{
v3 = v1 + 1;
do
result = (const char *)ft_putchar();
while ( *v3++ );
}
return result;
}

original_code:
void ft_putstr(const char *s)
{
 int count;
 count = 0;
 if (s == ((void *)0))
  ft_putstr("NULL");
 else
 {
  while (s[count])
  {
   ft_putchar(s[count]);
   count++;
  }
 }
}

canonical_original_code:
void ft_putstr(char *s)
{
 int count;
 count = 0;
 if (s == ((void *)0))
  ft_putstr("NULL");
 else
 {
  while (s[count])
  {
   ft_putchar(s[count]);
   count++;
  }
 }
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall find_next(char *a1)
{
char *v1; // rax
char v2; // dl

if ( a1 )
{
v1 = a1;
while ( 1 )
{
v2 = *v1;
if ( !*v1 )
break;
++v1;
if ( v2 == 10 )
return (unsigned int)((_DWORD)v1 - (_DWORD)a1 - 1);
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char *v1; // rax
char v2; // dl

if ( a1 )
{
v1 = a1;
while ( 1 )
{
v2 = *v1;
if ( !*v1 )
break;
++v1;
if ( v2 == 10 )
return (unsigned int)((_DWORD)v1 - (_DWORD)a1 - 1);
}
}
return 0xFFFFFFFFLL;
}

original_code:
static int find_next(char *str)
{
 char *stock;
 stock = str;
 if (str)
 while (*str)
  if (*str++ == '\n')
  return (str - stock - 1);
 return (-1);
}

variable_types:
str
char *

variable_types:
stock
char *


len

decompiled_code:
__int64 __fastcall len(_BYTE *a1)
{
__int64 result; // rax
_BYTE *v2; // rax

result = 0LL;
if ( a1 && *a1 )
{
v2 = a1;
do
++v2;
while ( *v2 );
result = (unsigned int)((_DWORD)v2 - (_DWORD)a1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1)
{
__int64 result; // rax
_BYTE *v2; // rax

result = 0LL;
if ( a1 && *a1 )
{
v2 = a1;
do
++v2;
while ( *v2 );
result = (unsigned int)((_DWORD)v2 - (_DWORD)a1);
}
return result;
}

original_code:
static int len(char *str)
{
 char *stock;
 if (!str || !*str)
 return (0);
 stock = str;
 while (*str)
 str++;
 return (str - stock);
}


decompiled_code:
ssize_t __fastcall get_next_line(int fd, _QWORD *a2)
{
ssize_t result; // rax
_BYTE *v3; // rax
_BYTE v4[1048]; // [rsp+0h] [rbp-418h] BYREF

do
{
v3 = get_next_next_constprop_0();
if ( v3 )
{
*a2 = v3;
return 1LL;
}
result = read(fd, v4, 0x400uLL);
if ( !(_DWORD)result )
return result;
old_1 = concat(old_1, (__int64)v4, result);
}
while ( old_1 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int fd, _QWORD *a2)
{
ssize_t result; // rax
_BYTE *v3; // rax
_BYTE v4[1048]; // [rsp+0h] [rbp-418h] BYREF

do
{
v3 = func2();
if ( v3 )
{
*a2 = v3;
return 1LL;
}
result = read(fd, v4, 0x400uLL);
if ( !(_DWORD)result )
return result;
old_1 = concat(old_1, (__int64)v4, result);
}
while ( old_1 );
return 0xFFFFFFFFLL;
}

original_code:
int get_next_line(int fd, char **line)
{
 char *ret;
 int n_read;
 static char *old;
 static int i;
 char buf[1024];
 while (!(ret = get_next_next(&old)))
 {
  n_read = read(fd, buf, 1024);
  if (!n_read)
   return (0);
  old = concat(old, buf, n_read);
  if (old == ((void *)0))
   return (-1);
 }
 line[i] = ret;
 return (1);
}

variable_types: fd int

variable_types:
line
char **

variable_types:
ret
char *

variable_types:
old
char *

variable_types:
buf
char [1024]


decompiled_code:
__int64 __fastcall ft_strlen(_BYTE *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

original_code:
size_t ft_strlen(const char *s)
{
 size_t count;
 count = 0;
 while (s[count])
 {
  count++;
 }
 return (count);
}

canonical_original_code:
unsigned long ft_strlen(char *s)
{
 unsigned long count;
 count = 0;
 while (s[count])
 {
  count++;
 }
 return (count);
}

variable_types:
count
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall find_value(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx

v2 = a1;
if ( !(unsigned int)ft_strcmp(*a1, a2) )
return v2[1];
while ( *((int *)v2 + 4) > 0 )
{
v3 = 0LL;
while ( (int)ft_strcmp(**(_QWORD **)(v2[4] + 8 * v3), a2) < 0 )
{
if ( *((_DWORD *)v2 + 4) <= (int)++v3 )
return 0LL;
}
v2 = *(_QWORD **)(v2[4] + 8 * v3);
if ( !(unsigned int)ft_strcmp(*v2, a2) )
return v2[1];
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 v3; // rbx

v2 = a1;
if ( !(unsigned int)ft_strcmp(*a1, a2) )
return v2[1];
while ( *((int *)v2 + 4) > 0 )
{
v3 = 0LL;
while ( (int)ft_strcmp(**(_QWORD **)(v2[4] + 8 * v3), a2) < 0 )
{
if ( *((_DWORD *)v2 + 4) <= (int)++v3 )
return 0LL;
}
v2 = *(_QWORD **)(v2[4] + 8 * v3);
if ( !(unsigned int)ft_strcmp(*v2, a2) )
return v2[1];
}
return 0LL;
}

original_code:
char *find_value(t_search *head, char *keyword)
{
 int i;
 i = 0;
 if (ft_strcmp(head->keyword, keyword) != 0)
 {
  while (i < head->nb_next)
  {
   if (ft_strcmp(head->next[i]->keyword, keyword) >= 0)
    return (find_value(head->next[i], keyword));
   i++;
  }
 }
 else
  return (head->value);
 return (((void *)0));
}

canonical_original_code:
char *find_value(struct s_search *head, char *keyword)
{
 int i;
 i = 0;
 if (ft_strcmp(head->keyword, keyword) != 0)
 {
  while (i < head->nb_next)
  {
   if (ft_strcmp(head->next[i]->keyword, keyword) >= 0)
    return (find_value(head->next[i], keyword));
   i++;
  }
 }
 else
  return (head->value);
 return (((void *)0));
}

variable_types: struct s_search { char *keyword; char *value; int nb_next; short is_leef; struct s_search *before; struct s_search **next; } *

variable_types:
keyword
char *

return_type: char *


original_code:
void *ft_memcpy(void *s1, const void *s2, size_t n)
{
 size_t count;
 unsigned char *str1;
 unsigned char *str2;
 count = 0;
 str1 = (unsigned char *) s1;
 str2 = (unsigned char *) s2;
 while (count < n)
 {
  str1[count] = str2[count];
  count++;
 }
 return (s1);
}

canonical_original_code:
void *ft_memcpy(void *s1, void *s2, unsigned long n)
{
 unsigned long count;
 unsigned char *str1;
 unsigned char *str2;
 count = 0;
 str1 = (unsigned char *) s1;
 str2 = (unsigned char *) s2;
 while (count < n)
 {
  str1[count] = str2[count];
  count++;
 }
 return (s1);
}

variable_types:
n
unsigned long

return_type: void *


decompiled_code:
ssize_t __fastcall ft_putchar(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func0(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

original_code:
void ft_putchar(char c)
{
 write(1, &c, 1);
}

variable_types: c char


loadWord

decompiled_code:
__int64 __fastcall loadWord(char *src, int a2)
{
__int64 v2; // r12
const char *v3; // rbp
int v4; // ebx
__int64 result; // rax
__int64 v6; // rcx

v2 = curWord;
if ( curWord > 2 )
{
v3 = s1;
v4 = 2;
while ( strcmp(v3, src) )
{
++v4;
v3 += 40;
if ( (_DWORD)v2 == v4 )
{
if ( (_DWORD)v2 == 1000 )
{
puts("\nToo may words, increase MAX_WORDS\n");
exit(-1);
}
goto LABEL_10;
}
}
LABEL_5:
result = v4;
if ( a2 )
goto LABEL_6;
LABEL_8:
lastIndex_0 = v4;
++bigramArray[result];
++sumVector[0];
return result;
}
v4 = 2;
if ( curWord != 2 )
goto LABEL_5;
LABEL_10:
curWord = v2 + 1;
strcpy(&wordVector[40 * v2], src);
result = v4;
if ( !a2 )
goto LABEL_8;
LABEL_6:
if ( a2 == 2 )
{
lastIndex_0 = v4;
++dword_406020[result];
++dword_4040E4;
++dword_405084[1000 * result];
}
else
{
v6 = lastIndex_0;
lastIndex_0 = v4;
++sumVector[v6];
result += 1000 * v6;
++bigramArray[result];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *src, int a2)
{
__int64 v2; // r12
const char *v3; // rbp
int v4; // ebx
__int64 result; // rax
__int64 v6; // rcx

v2 = curWord;
if ( curWord > 2 )
{
v3 = s1;
v4 = 2;
while ( strcmp(v3, src) )
{
++v4;
v3 += 40;
if ( (_DWORD)v2 == v4 )
{
if ( (_DWORD)v2 == 1000 )
{
puts("\nToo may words, increase MAX_WORDS\n");
exit(-1);
}
goto LABEL_10;
}
}
LABEL_5:
result = v4;
if ( a2 )
goto LABEL_6;
LABEL_8:
lastIndex_0 = v4;
++bigramArray[result];
++sumVector[0];
return result;
}
v4 = 2;
if ( curWord != 2 )
goto LABEL_5;
LABEL_10:
curWord = v2 + 1;
strcpy(&wordVector[40 * v2], src);
result = v4;
if ( !a2 )
goto LABEL_8;
LABEL_6:
if ( a2 == 2 )
{
lastIndex_0 = v4;
++dword_406020[result];
++dword_4040E4;
++dword_405084[1000 * result];
}
else
{
v6 = lastIndex_0;
lastIndex_0 = v4;
++sumVector[v6];
result += 1000 * v6;
++bigramArray[result];
}
return result;
}

original_code:
void loadWord( char *word, int order )
{
  int wordIndex;
  static int lastIndex = 0;
  for (wordIndex = 2 ; wordIndex < curWord ; wordIndex++) {
    if (!strcmp(wordVector[wordIndex], word)) {
      break;
    }
  }
  if (wordIndex == curWord) {
    if (curWord == 1000) {
      printf("\nToo may words, increase MAX_WORDS\n\n");
      exit(-1);
    }
    strcpy(wordVector[curWord++], word);
  }
  if (order == 0) {
    bigramArray[0][wordIndex]++;
    sumVector[0]++;
  } else if (order == 2) {
    bigramArray[wordIndex][1]++;
    bigramArray[1][wordIndex]++;
    sumVector[1]++;
  } else {
    bigramArray[lastIndex][wordIndex]++;
    sumVector[lastIndex]++;
  }
  lastIndex = wordIndex;
  return;
}

canonical_original_code:
void loadWord(char *word, int order)
{
  int wordIndex;
  static int lastIndex = 0;
  for (wordIndex = 2 ; wordIndex < curWord ; wordIndex++) {
    if (!strcmp(wordVector[wordIndex], word)) {
      break;
    }
  }
  if (wordIndex == curWord) {
    if (curWord == 1000) {
      printf("\nToo may words, increase MAX_WORDS\n\n");
      exit(-1);
    }
    strcpy(wordVector[curWord++], word);
  }
  if (order == 0) {
    bigramArray[0][wordIndex]++;
    sumVector[0]++;
  } else if (order == 2) {
    bigramArray[wordIndex][1]++;
    bigramArray[1][wordIndex]++;
    sumVector[1]++;
  } else {
    bigramArray[lastIndex][wordIndex]++;
    sumVector[lastIndex]++;
  }
  lastIndex = wordIndex;
  return;
}

variable_types:
word
char *

variable_types: order int

variable_types: wordIndex int

variable_types: lastIndex int


nextWord

decompiled_code:
__int64 __fastcall nextWord(int a1)
{
int v1; // eax
int v2; // edx
int v3; // er8
int i; // ecx
float v6; // [rsp+0h] [rbp-1Ch]

v6 = (float)sumVector[a1];
v1 = rand();
v2 = (a1 + 1) % curWord;
v3 = (int)((float)((float)v1 * v6) * 4.656612873077393e-10);
for ( i = bigramArray[1000 * a1 + v2]; v3 >= i; i += bigramArray[1000 * a1 + v2] )
{
if ( a1 == ++v2 )
break;
v2 %= curWord;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
int v1; // eax
int v2; // edx
int v3; // er8
int i; // ecx
float v6; // [rsp+0h] [rbp-1Ch]

v6 = (float)sumVector[a1];
v1 = rand();
v2 = (a1 + 1) % curWord;
v3 = (int)((float)((float)v1 * v6) * 4.656612873077393e-10);
for ( i = bigramArray[1000 * a1 + v2]; v3 >= i; i += bigramArray[1000 * a1 + v2] )
{
if ( a1 == ++v2 )
break;
v2 %= curWord;
}
return (unsigned int)v2;
}

original_code:
int nextWord( int word )
{
  int nextWord = (word + 1);
  int max = sumVector[word];
  int lim, sum = 0;
  lim = (int)((float)(max)*rand()/(2147483647 +1.0))+1;
  while (nextWord != word) {
    nextWord = nextWord % curWord;
    sum += bigramArray[word][nextWord];
    if (sum >= lim) {
      return nextWord;
    }
    nextWord++;
  }
  return nextWord;
}

canonical_original_code:
int nextWord(int word)
{
  int nextWord = (word + 1);
  int max = sumVector[word];
  int lim; int sum = 0;
  lim = (int)((float)(max)*rand()/(2147483647 +1.0))+1;
  while (nextWord != word) {
    nextWord = nextWord % curWord;
    sum += bigramArray[word][nextWord];
    if (sum >= lim) {
      return nextWord;
    }
    nextWord++;
  }
  return nextWord;
}

variable_types: word int

variable_types: nextWord int

variable_types: max int

variable_types: lim int

variable_types: sum int


buildSentence

decompiled_code:
__int64 buildSentence()
{
int v0; // ebx
int v1; // ebp

v0 = 0;
putchar(10);
v1 = nextWord(0);
do
{
if ( v1 == 1 )
break;
printf("%s ", &wordVector[40 * v1]);
v1 = nextWord(v1);
v0 += (int)((float)((float)rand() * 12.0) * 4.656612873077393e-10) + 1;
}
while ( v0 <= 99 );
printf("%c.\n\n", 8LL);
return 0LL;
}

original_code:
int buildSentence( void )
{
  int word = 0;
  int max = 0;
  printf("\n");
  word = nextWord(word);
  while (word != 1) {
    printf("%s ", wordVector[word]);
    word = nextWord(word);
    max += (int)((float)(12)*rand()/(2147483647 +1.0)) + 1;
    if (max >= 100) break;
  }
  printf("%c.\n\n", 8);
  return 0;
}

canonical_original_code:
int buildSentence()
{
  int word = 0;
  int max = 0;
  printf("\n");
  word = nextWord(word);
  while (word != 1) {
    printf("%s ", wordVector[word]);
    word = nextWord(word);
    max += (int)((float)(12)*rand()/(2147483647 +1.0)) + 1;
    if (max >= 100) break;
  }
  printf("%c.\n\n", 8);
  return 0;
}


parseOptions

decompiled_code:
int __fastcall parseOptions(char **argv, int argc, _DWORD *a3, char *a4)
{
int v6; // er12
int result; // eax

v6 = 1;
*a3 = 0;
if ( argc <= 1 )
goto LABEL_9;
while ( 1 )
{
result = getopt(argc, argv, "vf:");
if ( result == -1 )
break;
if ( result == 102 )
{
v6 = 0;
strcpy(a4, optarg);
}
else if ( result == 118 )
{
*a3 = 1;
}
else
{
v6 = 1;
}
}
if ( v6 )
{
LABEL_9:
puts("\nUsage is : \n");
printf("\t%s -f <filename> -v\n\n", *argv);
puts("\t\t -f corpus filename\n\t\t -v verbose mode\n");
exit(0);
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(char **argv, int argc, _DWORD *a3, char *a4)
{
int v6; // er12
int result; // eax

v6 = 1;
*a3 = 0;
if ( argc <= 1 )
goto LABEL_9;
while ( 1 )
{
result = getopt(argc, argv, "vf:");
if ( result == -1 )
break;
if ( result == 102 )
{
v6 = 0;
strcpy(a4, optarg);
}
else if ( result == 118 )
{
*a3 = 1;
}
else
{
v6 = 1;
}
}
if ( v6 )
{
LABEL_9:
puts("\nUsage is : \n");
printf("\t%s -f <filename> -v\n\n", *argv);
puts("\t\t -f corpus filename\n\t\t -v verbose mode\n");
exit(0);
}
return result;
}

original_code:
void parseOptions( char *argv[], int argc, int *dbg, char *fname )
{
  int opt, error = 1;
  *dbg = 0;
  if (argc > 1) {
    while ((opt = getopt(argc, argv, "vf:")) != -1) {
      switch(opt) {
        case 'v':
          *dbg = 1;
          break;
        case 'f':
          strcpy(fname, optarg);
          error = 0;
          break;
        default:
          error = 1;
      }
    }
  }
  if (error) {
    printf("\nUsage is : \n\n");
    printf("\t%s -f <filename> -v\n\n", argv[0]);
    printf("\t\t -f corpus filename\n\t\t -v verbose mode\n\n");
    exit(0);
  }
  return;
}

canonical_original_code:
void parseOptions(char *argv[], int argc, int *dbg, char *fname)
{
  int opt; int error = 1;
  *dbg = 0;
  if (argc > 1) {
    while ((opt = getopt(argc, argv, "vf:")) != -1) {
      switch(opt) {
        case 'v':
          *dbg = 1;
          break;
        case 'f':
          strcpy(fname, optarg);
          error = 0;
          break;
        default:
          error = 1;
      }
    }
  }
  if (error) {
    printf("\nUsage is : \n\n");
    printf("\t%s -f <filename> -v\n\n", argv[0]);
    printf("\t\t -f corpus filename\n\t\t -v verbose mode\n\n");
    exit(0);
  }
  return;
}

variable_types:
argv
char *[]

variable_types: argc int

variable_types:
dbg
int *

variable_types:
fname
char *

variable_types: opt int

variable_types: error int


emitMatrix

decompiled_code:
int emitMatrix()
{
int result; // eax
__int64 v1; // rbp
__int64 v2; // r12
__int64 v3; // rbx
__int64 v4; // rsi
__int64 v5; // rsi

result = putchar(10);
if ( curWord > 0 )
{
v1 = 0LL;
v2 = 0LL;
do
{
printf("%20s : ", &wordVector[40 * v2]);
if ( curWord > 0 )
{
v3 = 0LL;
do
{
v4 = (unsigned int)bigramArray[v3++ + v1];
printf("%d ", v4);
}
while ( curWord > (int)v3 );
}
v5 = (unsigned int)sumVector[v2++];
v1 += 1000LL;
result = printf(" : %d\n", v5);
}
while ( curWord > (int)v2 );
}
return result;
}

canonical_decompiled_code:
int func12()
{
int result; // eax
__int64 v1; // rbp
__int64 v2; // r12
__int64 v3; // rbx
__int64 v4; // rsi
__int64 v5; // rsi

result = putchar(10);
if ( curWord > 0 )
{
v1 = 0LL;
v2 = 0LL;
do
{
printf("%20s : ", &wordVector[40 * v2]);
if ( curWord > 0 )
{
v3 = 0LL;
do
{
v4 = (unsigned int)bigramArray[v3++ + v1];
printf("%d ", v4);
}
while ( curWord > (int)v3 );
}
v5 = (unsigned int)sumVector[v2++];
v1 += 1000LL;
result = printf(" : %d\n", v5);
}
while ( curWord > (int)v2 );
}
return result;
}

original_code:
void emitMatrix( void )
{
  int x, y;
  printf("\n");
  for (x = 0 ; x < curWord ; x++) {
    printf("%20s : ", wordVector[x]);
    for ( y = 0 ; y < curWord ; y++) {
      printf("%d ", bigramArray[x][y]);
    }
    printf(" : %d\n", sumVector[x]);
  }
}

canonical_original_code:
void emitMatrix()
{
  int x; int y;
  printf("\n");
  for (x = 0 ; x < curWord ; x++) {
    printf("%20s : ", wordVector[x]);
    for ( y = 0 ; y < curWord ; y++) {
      printf("%d ", bigramArray[x][y]);
    }
    printf(" : %d\n", sumVector[x]);
  }
}

variable_types: x int

variable_types: y int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[56]; // [rsp+10h] [rbp-38h] BYREF

parseOptions((char **)argv, argc);
v3 = time(0LL);
srand(v3);
memset(&bigramArray, 0, 0x3D0900uLL);
strcpy((char *)&dword_7D59A8, "<END>");
strcpy(wordVector, "<START>");
parseFile(v5);
printf("unique = %d\n", (unsigned int)curWord);
buildSentence();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[56]; // [rsp+10h] [rbp-38h] BYREF

func10((char **)argv, argc);
v3 = time(0LL);
srand(v3);
memset(&bigramArray, 0, 0x3D0900uLL);
strcpy((char *)&dword_7D59A8, "<END>");
strcpy(wordVector, "<START>");
func11(v5);
printf("unique = %d\n", (unsigned int)curWord);
func9();
return 0;
}

original_code:
int main( int argc, char *argv[] )
{
  char filename[40];
  int debug = 0;
  parseOptions( argv, argc, &debug, filename );
  srand(time(((void *)0)));
  bzero(bigramArray, sizeof(bigramArray));
  strcpy(wordVector[0], "<START>");
  strcpy(wordVector[1], "<END>");
  parseFile( filename );
  if (debug) emitMatrix();
  printf("unique = %d\n", curWord);
  buildSentence();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char filename[40];
  int debug = 0;
  parseOptions( argv, argc, &debug, filename );
  srand(time(((void *)0)));
  bzero(bigramArray, sizeof(bigramArray));
  strcpy(wordVector[0], "<START>");
  strcpy(wordVector[1], "<END>");
  parseFile( filename );
  if (debug) emitMatrix();
  printf("unique = %d\n", curWord);
  buildSentence();
  return 0;
}

variable_types:
filename
char [40]

variable_types: debug int


listPrint

decompiled_code:
int __fastcall listPrint(__int64 a1)
{
__int64 v1; // rbx

v1 = a1 + 8;
do
{
if ( *(_QWORD *)v1 )
printf("(%d,%d) ", *(unsigned int *)(*(_QWORD *)v1 + 32LL), *(unsigned int *)(*(_QWORD *)v1 + 36LL));
v1 += 8LL;
}
while ( v1 != a1 + 4008 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
__int64 v1; // rbx

v1 = a1 + 8;
do
{
if ( *(_QWORD *)v1 )
printf("(%d,%d) ", *(unsigned int *)(*(_QWORD *)v1 + 32LL), *(unsigned int *)(*(_QWORD *)v1 + 36LL));
v1 += 8LL;
}
while ( v1 != a1 + 4008 );
return putchar(10);
}

original_code:
void listPrint( list_t *list_p )
{
  int i;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      printf("(%d,%d) ", list_p->elem[i]->x, list_p->elem[i]->y);
    }
  }
  printf("\n");
}

canonical_original_code:
void listPrint(struct list_t *list_p)
{
  int i;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      printf("(%d,%d) ", list_p->elem[i]->x, list_p->elem[i]->y);
    }
  }
  printf("\n");
}

variable_types: struct list_t { int numElems; struct node_s { struct node_s *parent; double g; double h; double f; int x; int y; } *elem[500]; } *

variable_types: i int

user_defined_types: struct list_t { int numElems; struct node_s *elem[500]; }


listEmpty

decompiled_code:
_BOOL8 __fastcall listEmpty(_DWORD *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_DWORD *a1)
{
return *a1 == 0;
}

original_code:
int listEmpty( list_t *list_p )
{
  if (list_p->numElems == 0) return 1;
  else return 0;
}

canonical_original_code:
int listEmpty(struct list_t *list_p)
{
  if (list_p->numElems == 0) return 1;
  else return 0;
}

user_defined_types: struct node_s { struct node_s *parent; double g; double h; double f; int x; int y; }


listFindBest

original_code:
node_t *listFindBest( list_t *list_p )
{
  int i;
  int best = -1;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      best = i++;
      break;
    }
  }
  for ( ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      if (list_p->elem[i]->f < list_p->elem[best]->f) {
        best = i;
      }
    }
  }
  return list_p->elem[best];
}

canonical_original_code:
struct node_s *listFindBest(struct list_t *list_p)
{
  int i;
  int best = -1;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      best = i++;
      break;
    }
  }
  for ( ; i < 500 ; i++) {
    if (list_p->elem[i] != 0) {
      if (list_p->elem[i]->f < list_p->elem[best]->f) {
        best = i;
      }
    }
  }
  return list_p->elem[best];
}

variable_types: struct list_t { int numElems; struct node_s *elem[500]; } *

variable_types: best int

return_type: struct node_s { struct node_s *parent; double g; double h; double f; int x; int y; } *


listPresent

original_code:
int listPresent( list_t *list_p, int x, int y )
{
  int i;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i]) {
      if ((list_p->elem[i]->x == x) && (list_p->elem[i]->y == y)) return 1;
    }
  }
  return 0;
}

canonical_original_code:
int listPresent(struct list_t *list_p, int x, int y)
{
  int i;
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i]) {
      if ((list_p->elem[i]->x == x) && (list_p->elem[i]->y == y)) return 1;
    }
  }
  return 0;
}


listGet

original_code:
node_t *listGet( list_t *list_p, int x, int y, int remove )
{
  int i;
  node_t *node = (node_t *)((void *)0);
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i]) {
      if ((list_p->elem[i]->x == x) && (list_p->elem[i]->y == y)) {
        node = list_p->elem[i];
        if (remove) {
          list_p->elem[i] = (node_t *)((void *)0);
          list_p->numElems--;
        }
        break;
      }
    }
  }
  return node;
}

canonical_original_code:
struct node_s *listGet(struct list_t *list_p, int x, int y, int remove)
{
  int i;
  struct node_s *node = (node_t *)((void *)0);
  for (i = 0 ; i < 500 ; i++) {
    if (list_p->elem[i]) {
      if ((list_p->elem[i]->x == x) && (list_p->elem[i]->y == y)) {
        node = list_p->elem[i];
        if (remove) {
          list_p->elem[i] = (struct node_s *)((void *)0);
          list_p->numElems--;
        }
        break;
      }
    }
  }
  return node;
}

variable_types: remove int

variable_types: struct node_s { struct node_s *parent; double g; double h; double f; int x; int y; } *


decompiled_code:
double __fastcall calc_h(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm1_8

if ( !a1 )
__assert_fail("node_p", "misc.c", 0x3Bu, "calc_h");
v1 = (double)*(int *)(a1 + 32) - (double)goal_x;
if ( v1 < 0.0 )
v1 = -v1;
v2 = (double)*(int *)(a1 + 36) - (double)goal_y;
if ( v2 < 0.0 )
v2 = -v2;
return v1 + v2;
}

canonical_decompiled_code:
double __fastcall func14(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm1_8

if ( !a1 )
__assert_fail("node_p", "misc.c", 0x3Bu, "calc_h");
v1 = (double)*(int *)(a1 + 32) - (double)goal_x;
if ( v1 < 0.0 )
v1 = -v1;
v2 = (double)*(int *)(a1 + 36) - (double)goal_y;
if ( v2 < 0.0 )
v2 = -v2;
return v1 + v2;
}

original_code:
double calc_h( node_t *node_p )
{
  double h;
  extern const int goal_x, goal_y;
  ((void) sizeof ((node_p) ? 1 : 0), __extension__ ({ if (node_p) ; else __assert_fail ("node_p", "misc.c", 59, __extension__ __PRETTY_FUNCTION__); }));
  h = (double)( (double)1.0 *
                (((((double)node_p->x-(double)goal_x) >= 0.0) ? ((double)node_p->x-(double)goal_x) : -((double)node_p->x-(double)goal_x)) +
                 ((((double)node_p->y-(double)goal_y) >= 0.0) ? ((double)node_p->y-(double)goal_y) : -((double)node_p->y-(double)goal_y))) );
  return h;
}

canonical_original_code:
double calc_h(struct node_s *node_p)
{
  double h;
  extern int goal_x; extern int goal_y;
  ((void) sizeof ((node_p) ? 1 : 0), __extension__ ({ if (node_p) ; else __assert_fail ("node_p", "misc.c", 59, __extension__ __PRETTY_FUNCTION__); }));
  h = (double)( (double)1.0 *
                (((((double)node_p->x-(double)goal_x) >= 0.0) ? ((double)node_p->x-(double)goal_x) : -((double)node_p->x-(double)goal_x)) +
                 ((((double)node_p->y-(double)goal_y) >= 0.0) ? ((double)node_p->y-(double)goal_y) : -((double)node_p->y-(double)goal_y))) );
  return h;
}

variable_types: h double

return_type: double


decompiled_code:
double __fastcall calc_g(__int64 a1)
{
if ( !a1 )
__assert_fail("node_p", "misc.c", 0x49u, "calc_g");
return (*(double *)(a1 + 8) - 1.0) * 0.5 + 1.0;
}

canonical_decompiled_code:
double __fastcall func15(__int64 a1)
{
if ( !a1 )
__assert_fail("node_p", "misc.c", 0x49u, "calc_g");
return (*(double *)(a1 + 8) - 1.0) * 0.5 + 1.0;
}

original_code:
double calc_g( node_t *node_p )
{
  double g;
  ((void) sizeof ((node_p) ? 1 : 0), __extension__ ({ if (node_p) ; else __assert_fail ("node_p", "misc.c", 73, __extension__ __PRETTY_FUNCTION__); }));
  g = 1.0 + (double)0.5 * (node_p->g - 1.0);
  return g;
}

canonical_original_code:
double calc_g(struct node_s *node_p)
{
  double g;
  ((void) sizeof ((node_p) ? 1 : 0), __extension__ ({ if (node_p) ; else __assert_fail ("node_p", "misc.c", 73, __extension__ __PRETTY_FUNCTION__); }));
  g = 1.0 + (double)0.5 * (node_p->g - 1.0);
  return g;
}

variable_types: g double


showBestPath

decompiled_code:
int __fastcall showBestPath(_QWORD *a1)
{
_QWORD *v1; // rdx
_QWORD *v2; // rcx
__int64 v3; // rsi
__int64 v4; // rax
char *v5; // rbp
char *v6; // rbx
__int64 v7; // rsi

v1 = (_QWORD *)*a1;
grid[15 * goal_y + goal_x] = 71;
v2 = (_QWORD *)*v1;
if ( *v1 )
{
while ( 1 )
{
v3 = *((int *)v1 + 8);
v4 = 15LL * *((int *)v1 + 9);
v1 = v2;
grid[v3 + v4] = 46;
if ( !*v2 )
break;
v2 = (_QWORD *)*v2;
}
}
v5 = (char *)&unk_4040BC;
grid[15 * *(int *)(start_node_p + 36) + *(int *)(start_node_p + 32)] = 83;
putchar(10);
do
{
v6 = v5 - 60;
do
{
v7 = *(unsigned int *)v6;
v6 += 4;
printf("%c ", v7);
}
while ( v6 != v5 );
v5 = v6 + 60;
putchar(10);
}
while ( v6 != (char *)&_bss_start );
return putchar(10);
}

original_code:
void showBestPath( node_t *walker )
{
  int x, y;
  extern int grid[15][15];
  extern const int goal_x, goal_y;
  grid[goal_y][goal_x] = 'G';
  walker = (node_t *)walker->parent;
  while (walker->parent != (node_t *)((void *)0)) {
    grid[walker->y][walker->x] = '.';
    walker = (node_t *)walker->parent;
  }
  grid[start_node_p->y][start_node_p->x] = 'S';
  printf("\n");
  for (y = 0 ; y < 15 ; y++) {
    for (x = 0 ; x < 15 ; x++) {
      printf("%c ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  return;
}

canonical_original_code:
void showBestPath(struct node_s *walker)
{
  int x; int y;
  extern int grid[15][15];
  extern int goal_x; extern int goal_y;
  grid[goal_y][goal_x] = 'G';
  walker = (struct node_s *)walker->parent;
  while (walker->parent != (struct node_s *)((void *)0)) {
    grid[walker->y][walker->x] = '.';
    walker = (struct node_s *)walker->parent;
  }
  grid[start_node_p->y][start_node_p->x] = 'S';
  printf("\n");
  for (y = 0 ; y < 15 ; y++) {
    for (x = 0 ; x < 15 ; x++) {
      printf("%c ", grid[y][x]);
    }
    printf("\n");
  }
  printf("\n");
  return;
}


cleanup

decompiled_code:
void cleanup()
{
__int64 i; // rbx
void *v1; // rdi
void *v2; // rdi

for ( i = 1LL; i != 501; ++i )
{
v1 = (void *)openList_p[i];
if ( v1 )
free(v1);
v2 = (void *)closedList_p[i];
if ( v2 )
free(v2);
}
}

canonical_decompiled_code:
void func17()
{
__int64 i; // rbx
void *v1; // rdi
void *v2; // rdi

for ( i = 1LL; i != 501; ++i )
{
v1 = (void *)openList_p[i];
if ( v1 )
free(v1);
v2 = (void *)closedList_p[i];
if ( v2 )
free(v2);
}
}

original_code:
void cleanup( void )
{
  int i;
  extern list_t openList_p;
  extern list_t closedList_p;
  for (i = 0 ; i < 500 ; i++) {
    if (openList_p.elem[i]) free(openList_p.elem[i]);
    if (closedList_p.elem[i]) free(closedList_p.elem[i]);
  }
  return;
}

canonical_original_code:
void cleanup()
{
  int i;
  extern struct list_t openList_p;
  extern struct list_t closedList_p;
  for (i = 0 ; i < 500 ; i++) {
    if (openList_p.elem[i]) free(openList_p.elem[i]);
    if (closedList_p.elem[i]) free(closedList_p.elem[i]);
  }
  return;
}

variable_types: struct list_t { int numElems; struct node_s { struct node_s *parent; double g; double h; double f; int x; int y; } *elem[500]; }


printStructure

decompiled_code:
int printStructure()
{
unsigned int v0; // er12
int *v1; // rbp
int result; // eax
__int64 i; // rbx
__int64 j; // rbx

v0 = 0;
v1 = (int *)&ruleSet;
printf("Parsed %d rules\n", (unsigned int)ruleIndex);
do
{
while ( 1 )
{
result = *v1;
if ( *v1 )
break;
++v0;
v1 += 26;
if ( v0 == 40 )
return result;
}
printf("Rule %d :\n", v0);
puts("Antecedents : ");
for ( i = *((_QWORD *)v1 + 11); i; i = *(_QWORD *)(i + 88) )
printf(" %s\n", (const char *)(i + 4));
puts("Consequents : ");
for ( j = *((_QWORD *)v1 + 12); j; j = *(_QWORD *)(j + 88) )
printf(" %s\n", (const char *)(j + 4));
++v0;
v1 += 26;
result = putchar(10);
}
while ( v0 != 40 );
return result;
}

canonical_decompiled_code:
int func0()
{
unsigned int v0; // er12
int *v1; // rbp
int result; // eax
__int64 i; // rbx
__int64 j; // rbx

v0 = 0;
v1 = (int *)&ruleSet;
printf("Parsed %d rules\n", (unsigned int)ruleIndex);
do
{
while ( 1 )
{
result = *v1;
if ( *v1 )
break;
++v0;
v1 += 26;
if ( v0 == 40 )
return result;
}
printf("Rule %d :\n", v0);
puts("Antecedents : ");
for ( i = *((_QWORD *)v1 + 11); i; i = *(_QWORD *)(i + 88) )
printf(" %s\n", (const char *)(i + 4));
puts("Consequents : ");
for ( j = *((_QWORD *)v1 + 12); j; j = *(_QWORD *)(j + 88) )
printf(" %s\n", (const char *)(j + 4));
++v0;
v1 += 26;
result = putchar(10);
}
while ( v0 != 40 );
return result;
}

original_code:
void printStructure( void )
{
  memoryElementType *ptr;
  int i;
  printf("Parsed %d rules\n", ruleIndex);
  for (i = 0 ; i < 40 ; i++) {
    if (ruleSet[i].active) {
      printf("Rule %d :\n", i);
      printf("Antecedents : \n");
      ptr = ruleSet[i].antecedent;
      while (ptr) {
        printf("  %s\n", ptr->element);
        ptr = ptr->next;
      }
      printf("Consequents : \n");
      ptr = ruleSet[i].consequent;
      while (ptr) {
        printf("  %s\n", ptr->element);
        ptr = ptr->next;
      }
      printf("\n");
    }
  }
}

canonical_original_code:
void printStructure()
{
  struct memoryElementStruct *ptr;
  int i;
  printf("Parsed %d rules\n", ruleIndex);
  for (i = 0 ; i < 40 ; i++) {
    if (ruleSet[i].active) {
      printf("Rule %d :\n", i);
      printf("Antecedents : \n");
      ptr = ruleSet[i].antecedent;
      while (ptr) {
        printf("  %s\n", ptr->element);
        ptr = ptr->next;
      }
      printf("Consequents : \n");
      ptr = ruleSet[i].consequent;
      while (ptr) {
        printf("  %s\n", ptr->element);
        ptr = ptr->next;
      }
      printf("\n");
    }
  }
}

variable_types: struct memoryElementStruct { int active; char element[-1]; struct memoryElementStruct *next; } *


printWorkingMemory

decompiled_code:
int printWorkingMemory()
{
unsigned __int64 v0; // rbx
const char *v1; // rsi

v0 = (unsigned int)((_DWORD)&workingMemory + 4);
puts("\tWorking Memory:");
do
{
while ( !*(_DWORD *)(v0 - 4) )
{
v0 += 96LL;
if ( (_UNKNOWN *)v0 == (_UNKNOWN *)((char *)&workingMemory + 3844) )
return putchar(10);
}
v1 = (const char *)v0;
v0 += 96LL;
printf("\t\t%s\n", v1);
}
while ( (_UNKNOWN *)v0 != (_UNKNOWN *)((char *)&workingMemory + 3844) );
return putchar(10);
}

canonical_decompiled_code:
int func1()
{
unsigned __int64 v0; // rbx
const char *v1; // rsi

v0 = (unsigned int)((_DWORD)&workingMemory + 4);
puts("\tWorking Memory:");
do
{
while ( !*(_DWORD *)(v0 - 4) )
{
v0 += 96LL;
if ( (_UNKNOWN *)v0 == (_UNKNOWN *)((char *)&workingMemory + 3844) )
return putchar(10);
}
v1 = (const char *)v0;
v0 += 96LL;
printf("\t\t%s\n", v1);
}
while ( (_UNKNOWN *)v0 != (_UNKNOWN *)((char *)&workingMemory + 3844) );
return putchar(10);
}

original_code:
void printWorkingMemory( void )
{
  int slot;
  printf("\tWorking Memory:\n");
  for (slot = 0 ; slot < 40 ; slot++) {
    if (workingMemory[slot].active) {
      printf("\t\t%s\n", workingMemory[slot].element);
    }
  }
  printf("\n");
}

canonical_original_code:
void printWorkingMemory()
{
  int slot;
  printf("\tWorking Memory:\n");
  for (slot = 0 ; slot < 40 ; slot++) {
    if (workingMemory[slot].active) {
      printf("\t\t%s\n", workingMemory[slot].element);
    }
  }
  printf("\n");
}

variable_types: slot int


emitHelp

decompiled_code:
void __noreturn emitHelp()
{
puts("\n\trbs -r <rule file> [-hd]\n");
puts("\t\t -r specify the rules file");
puts("\t\t -d debug mode");
puts("\t\t -h this help\n");
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
puts("\n\trbs -r <rule file> [-hd]\n");
puts("\t\t -r specify the rules file");
puts("\t\t -d debug mode");
puts("\t\t -h this help\n");
exit(0);
}

original_code:
void emitHelp( void )
{
  printf("\n\trbs -r <rule file> [-hd]\n\n");
  printf("\t\t -r  specify the rules file\n");
  printf("\t\t -d  debug mode\n");
  printf("\t\t -h  this help\n\n");
  exit(0);
}

canonical_original_code:
void emitHelp()
{
  printf("\n\trbs -r <rule file> [-hd]\n\n");
  printf("\t\t -r  specify the rules file\n");
  printf("\t\t -d  debug mode\n");
  printf("\t\t -h  this help\n\n");
  exit(0);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int128 v5[6]; // [rsp+0h] [rbp-68h] BYREF

debug = 0;
v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v5[3] = 0LL;
v5[4] = 0LL;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hdr:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'h':
goto LABEL_16;
case 'r':
strcpy((char *)v5, optarg);
break;
case 'd':
debug = 1;
puts("Debugging enabled");
break;
}
}
if ( !LOBYTE(v5[0]) )
LABEL_16:
emitHelp();
timers[0] = 0LL;
memset(&workingMemory, 0, 0xF00uLL);
timers[1] = 0LL;
memset(&ruleSet, 0, 0x1040uLL);
timers[2] = 0LL;
timers[3] = 0LL;
timers[4] = 0LL;
if ( (int)parseFile(v5) < 0 )
{
puts("\nCould not open file, or parse error\n");
exit(0);
}
while ( 1 )
{
interpret();
if ( debug )
break;
processTimers();
if ( endRun )
return 0;
LABEL_12:
sleep(1u);
}
printWorkingMemory();
processTimers();
if ( !endRun )
goto LABEL_12;
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int128 v5[6]; // [rsp+0h] [rbp-68h] BYREF

debug = 0;
v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v5[3] = 0LL;
v5[4] = 0LL;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hdr:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'h':
goto LABEL_16;
case 'r':
strcpy((char *)v5, optarg);
break;
case 'd':
debug = 1;
puts("Debugging enabled");
break;
}
}
if ( !LOBYTE(v5[0]) )
LABEL_16:
func2();
timers[0] = 0LL;
memset(&workingMemory, 0, 0xF00uLL);
timers[1] = 0LL;
memset(&ruleSet, 0, 0x1040uLL);
timers[2] = 0LL;
timers[3] = 0LL;
timers[4] = 0LL;
if ( (int)parseFile(v5) < 0 )
{
puts("\nCould not open file, or parse error\n");
exit(0);
}
while ( 1 )
{
interpret();
if ( debug )
break;
processTimers();
if ( endRun )
return 0;
LABEL_12:
sleep(1u);
}
func1();
processTimers();
if ( !endRun )
goto LABEL_12;
return 0;
}

original_code:
int main( int argc, char *argv[] )
{
  int opt, ret;
  char inpfile[80]={0};
  extern void processTimers( void );
  extern int parseFile( char * );
  extern void interpret( void );
  debug = 0;
  while ((opt = getopt(argc, argv, "hdr:")) != -1) {
    switch( opt ) {
      case 'r':
        strcpy(inpfile, optarg);
        break;
      case 'd':
        debug = 1;
        printf("Debugging enabled\n");
        break;
      case 'h':
        emitHelp();
        break;
    }
  }
  if (inpfile[0] == 0) emitHelp();
  bzero( (void *)workingMemory, sizeof(workingMemory) );
  bzero( (void *)ruleSet, sizeof(ruleSet) );
  bzero( (void *)timers, sizeof(timers) );
  ret = parseFile( inpfile );
  if (ret < 0) {
    printf("\nCould not open file, or parse error\n\n");
    exit(0);
  }
  while (1) {
    interpret();
    if (debug) {
      printWorkingMemory();
    }
    processTimers();
    if (endRun) break;
    sleep(1);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int opt; int ret;
  char inpfile[80] = {0};
  extern void processTimers();
  extern int parseFile(char *);
  extern void interpret();
  debug = 0;
  while ((opt = getopt(argc, argv, "hdr:")) != -1) {
    switch( opt ) {
      case 'r':
        strcpy(inpfile, optarg);
        break;
      case 'd':
        debug = 1;
        printf("Debugging enabled\n");
        break;
      case 'h':
        emitHelp();
        break;
    }
  }
  if (inpfile[0] == 0) emitHelp();
  bzero( (void *)workingMemory, sizeof(workingMemory) );
  bzero( (void *)ruleSet, sizeof(ruleSet) );
  bzero( (void *)timers, sizeof(timers) );
  ret = parseFile( inpfile );
  if (ret < 0) {
    printf("\nCould not open file, or parse error\n\n");
    exit(0);
  }
  while (1) {
    interpret();
    if (debug) {
      printWorkingMemory();
    }
    processTimers();
    if (endRun) break;
    sleep(1);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: ret int

variable_types:
inpfile
char [80]

variable_types:
processTimers
void ()

variable_types:
parseFile
int (char *)

variable_types:
interpret
void ()


getRandomCharacter

original_code:
unsigned char getRandomCharacter( void )
{
  const unsigned char alphabet[3] = {'0', '1', '#'};
  return( alphabet[(int)((double)(3)*rand()/(2147483647 +1.0))] );
}

canonical_original_code:
unsigned char getRandomCharacter()
{
  unsigned char alphabet[3] = {'0', '1', '#'};
  return( alphabet[(int)((double)(3)*rand()/(2147483647 +1.0))] );
}

variable_types:
alphabet
unsigned char [3]

return_type: unsigned char


readDetectors

decompiled_code:
__int64 __fastcall readDetectors(__int64 a1)
{
__int64 v1; // rcx
int v2; // edx
int v3; // eax
__int64 v4; // rax
__int64 result; // rax

v1 = 0LL;
v2 = 0;
v3 = -1;
while ( 1 )
{
v4 = 5LL * (agent_y + v3);
LOBYTE(v4) = *((_BYTE *)&world + 4 * v4 + agent_x + v2) == 42;
result = (unsigned int)(v4 + 48);
*(_BYTE *)(a1 + v1++) = result;
if ( v1 == 4 )
break;
v3 = dir[2 * v1];
v2 = dword_402064[2 * v1];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 v1; // rcx
int v2; // edx
int v3; // eax
__int64 v4; // rax
__int64 result; // rax

v1 = 0LL;
v2 = 0;
v3 = -1;
while ( 1 )
{
v4 = 5LL * (agent_y + v3);
LOBYTE(v4) = *((_BYTE *)&world + 4 * v4 + agent_x + v2) == 42;
result = (unsigned int)(v4 + 48);
*(_BYTE *)(a1 + v1++) = result;
if ( v1 == 4 )
break;
v3 = dir[2 * v1];
v2 = dword_402064[2 * v1];
}
return result;
}

original_code:
void readDetectors( unsigned char *condition )
{
  int i;
  for (i = 0 ; i < 4 ; i++) {
    int y = agent_y + dir[i].loc_y;
    int x = agent_x + dir[i].loc_x;
    condition[i] = (world[y][x] == '*') ? '1' : '0';
  }
  return;
}

canonical_original_code:
void readDetectors(unsigned char *condition)
{
  int i;
  for (i = 0 ; i < 4 ; i++) {
    int y = agent_y + dir[i].loc_y;
    int x = agent_x + dir[i].loc_x;
    condition[i] = (world[y][x] == '*') ? '1' : '0';
  }
  return;
}

variable_types:
condition
unsigned char *

variable_types: i int

variable_types: y int

variable_types: x int


match

decompiled_code:
__int64 __fastcall match(char a1, char a2)
{
return (a1 == 35) | (unsigned __int8)(a1 == a2);
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1, char a2)
{
return (a1 == 35) | (unsigned __int8)(a1 == a2);
}

original_code:
int match( unsigned char cl, unsigned char cn )
{
  if (cl == '#') return 1;
  else if (cl == cn) return 1;
  return 0;
}

canonical_original_code:
int match(unsigned char cl, unsigned char cn)
{
  if (cl == '#') return 1;
  else if (cl == cn) return 1;
  return 0;
}

variable_types:
cl
unsigned char

variable_types:
cn
unsigned char


calculateSpecificity

decompiled_code:
__int64 __fastcall calculateSpecificity(int a1)
{
__int64 v1; // rax
double v2; // xmm0_8
__int64 v3; // rdx
__int64 result; // rax

v1 = 0LL;
v2 = 0.0;
v3 = 40LL * a1;
*(_QWORD *)&list[v3 + 16] = 0LL;
do
{
if ( list[v3 + v1] != 35 )
{
v2 = v2 + 1.0;
*(double *)&list[v3 + 16] = v2;
}
++v1;
}
while ( v1 != 4 );
result = 5LL * a1;
*(double *)&qword_404250[5 * a1] = v2 * 0.25;
return result;
}

original_code:
void calculateSpecificity( int classifier )
{
  int i;
  list[classifier].specificity = 0.0;
  for (i = 0 ; i < 4 ; i++) {
    if (list[classifier].condition[i] != '#') {
      list[classifier].specificity++;
    }
  }
  list[classifier].specificity /= (double)4;
  return;
}

canonical_original_code:
void calculateSpecificity(int classifier)
{
  int i;
  list[classifier].specificity = 0.0;
  for (i = 0 ; i < 4 ; i++) {
    if (list[classifier].condition[i] != '#') {
      list[classifier].specificity++;
    }
  }
  list[classifier].specificity /= (double)4;
  return;
}

variable_types: classifier int


createCoveringClassifier

original_code:
int createCoveringClassifier( int classifier, unsigned char *condition )
{
  int c, act;
  double minStrength = 99.0;
  if (classifier == -1) {
    for (c = 0 ; c < 32 ; c++) {
      if (list[c].strength < minStrength) {
        classifier = c;
        minStrength = list[c].strength;
      }
    }
  }
  for (c = 0 ; c < 4 ; c++) {
    if (((float)rand() / (float)2147483647) > 0.75) {
      list[classifier].condition[c] = condition[c];
    } else {
      list[classifier].condition[c] = '#';
    }
  }
  for (act = 0 ; act < 2 ; act++) {
    list[classifier].action[act] = 0x30 + (int)((double)(2)*rand()/(2147483647 +1.0));
  }
  list[classifier].strength = 1.0;
  list[classifier].episode = 0;
  calculateSpecificity( classifier );
  return classifier;
}

canonical_original_code:
int createCoveringClassifier(int classifier, unsigned char *condition)
{
  int c; int act;
  double minStrength = 99.0;
  if (classifier == -1) {
    for (c = 0 ; c < 32 ; c++) {
      if (list[c].strength < minStrength) {
        classifier = c;
        minStrength = list[c].strength;
      }
    }
  }
  for (c = 0 ; c < 4 ; c++) {
    if (((float)rand() / (float)2147483647) > 0.75) {
      list[classifier].condition[c] = condition[c];
    } else {
      list[classifier].condition[c] = '#';
    }
  }
  for (act = 0 ; act < 2 ; act++) {
    list[classifier].action[act] = 0x30 + (int)((double)(2)*rand()/(2147483647 +1.0));
  }
  list[classifier].strength = 1.0;
  list[classifier].episode = 0;
  calculateSpecificity( classifier );
  return classifier;
}

variable_types: c int

variable_types: act int

variable_types: minStrength double


matchClassifiers

original_code:
int matchClassifiers( unsigned char *condition )
{
  int c;
  int i;
  double bidSum = 0.0;
  int matchFound = 0;
  for (c = 0 ; c < 32 ; c++) {
    list[c].match = 0;
    for (i = 0 ; i < 4 ; i++) {
      if (!match(list[c].condition[i], condition[i])) break;
    }
    if (i == 4) {
      list[c].match = 1;
      list[c].bid = ((double)0.5) * list[c].strength * list[c].specificity;
      bidSum += list[c].bid;
      matchFound = 1;
    }
  }
  if (matchFound) {
    int tries = 32;
    c = (int)((double)(32)*rand()/(2147483647 +1.0));
    while (1) {
      if (list[c].match) {
        if ( (list[c].bid / bidSum) > ((float)rand() / (float)2147483647)) break;
        if ( --tries == 0) break;
      }
      if (++c >= 32) c = 0;
    }
  } else {
    if (c == 32) {
      c = createCoveringClassifier( -1, condition );
    }
  }
  return c;
}

canonical_original_code:
int matchClassifiers(unsigned char *condition)
{
  int c;
  int i;
  double bidSum = 0.0;
  int matchFound = 0;
  for (c = 0 ; c < 32 ; c++) {
    list[c].match = 0;
    for (i = 0 ; i < 4 ; i++) {
      if (!match(list[c].condition[i], condition[i])) break;
    }
    if (i == 4) {
      list[c].match = 1;
      list[c].bid = ((double)0.5) * list[c].strength * list[c].specificity;
      bidSum += list[c].bid;
      matchFound = 1;
    }
  }
  if (matchFound) {
    int tries = 32;
    c = (int)((double)(32)*rand()/(2147483647 +1.0));
    while (1) {
      if (list[c].match) {
        if ( (list[c].bid / bidSum) > ((float)rand() / (float)2147483647)) break;
        if ( --tries == 0) break;
      }
      if (++c >= 32) c = 0;
    }
  } else {
    if (c == 32) {
      c = createCoveringClassifier( -1, condition );
    }
  }
  return c;
}

variable_types: bidSum double

variable_types: matchFound int

variable_types: tries int


initClassifier

original_code:
void initClassifier( int classifier )
{
  int cond, act;
  for (cond = 0 ; cond < 4 ; cond++) {
    list[classifier].condition[cond] = getRandomCharacter();
  }
  for (act = 0 ; act < 2 ; act++) {
    list[classifier].action[act] = 0x30 + (int)((double)(2)*rand()/(2147483647 +1.0));
  }
  list[classifier].strength = 1.0;
  list[classifier].episode = 0;
  calculateSpecificity( classifier );
  return;
}

canonical_original_code:
void initClassifier(int classifier)
{
  int cond; int act;
  for (cond = 0 ; cond < 4 ; cond++) {
    list[classifier].condition[cond] = getRandomCharacter();
  }
  for (act = 0 ; act < 2 ; act++) {
    list[classifier].action[act] = 0x30 + (int)((double)(2)*rand()/(2147483647 +1.0));
  }
  list[classifier].strength = 1.0;
  list[classifier].episode = 0;
  calculateSpecificity( classifier );
  return;
}

variable_types: cond int


printAgent

decompiled_code:
int printAgent()
{
unsigned int v0; // er12
__int64 v1; // rbp
__int64 v2; // rbx
int v3; // edi
int result; // eax

v0 = 0;
v1 = 0LL;
puts("\nClassifier List:");
do
{
v2 = 0LL;
printf("C%02d ", v0);
do
{
v3 = (unsigned __int8)list[v1 + v2++];
putchar(v3);
}
while ( v2 != 4 );
++v0;
v1 += 40LL;
printf(" : ");
putchar(*((unsigned __int8 *)&xmmword_40421C + v1));
putchar(*(unsigned __int8 *)(v1 + 4211229));
result = printf(" Strength %g\n", *(double *)(v1 + 4211232));
}
while ( v0 != 32 );
return result;
}

canonical_decompiled_code:
int func16()
{
unsigned int v0; // er12
__int64 v1; // rbp
__int64 v2; // rbx
int v3; // edi
int result; // eax

v0 = 0;
v1 = 0LL;
puts("\nClassifier List:");
do
{
v2 = 0LL;
printf("C%02d ", v0);
do
{
v3 = (unsigned __int8)list[v1 + v2++];
putchar(v3);
}
while ( v2 != 4 );
++v0;
v1 += 40LL;
printf(" : ");
putchar(*((unsigned __int8 *)&xmmword_40421C + v1));
putchar(*(unsigned __int8 *)(v1 + 4211229));
result = printf(" Strength %g\n", *(double *)(v1 + 4211232));
}
while ( v0 != 32 );
return result;
}

original_code:
void printAgent( void )
{
  int c, cond, act;
  printf("\nClassifier List:\n");
  for (c = 0 ; c < 32 ; c++) {
    printf( "C%02d ", c );
    for (cond = 0 ; cond < 4 ; cond++) {
      printf("%c", list[c].condition[cond]);
    }
    printf(" : ");
    for (act = 0 ; act < 2 ; act++) {
      printf("%c", list[c].action[act]);
    }
    printf("  Strength %g\n", list[c].strength);
  }
  return;
}

canonical_original_code:
void printAgent()
{
  int c; int cond; int act;
  printf("\nClassifier List:\n");
  for (c = 0 ; c < 32 ; c++) {
    printf( "C%02d ", c );
    for (cond = 0 ; cond < 4 ; cond++) {
      printf("%c", list[c].condition[cond]);
    }
    printf(" : ");
    for (act = 0 ; act < 2 ; act++) {
      printf("%c", list[c].action[act]);
    }
    printf("  Strength %g\n", list[c].strength);
  }
  return;
}


printWorld

decompiled_code:
int printWorld()
{
char *v0; // rbp
char *v1; // rbx
__int64 v2; // rsi
int result; // eax

v0 = (char *)&unk_4040B4;
puts("World:");
do
{
v1 = v0 - 20;
putchar(9);
do
{
v2 = (unsigned __int8)*v1++;
printf("%c ", v2);
}
while ( v1 != v0 );
v0 = v1 + 20;
result = putchar(10);
}
while ( v1 != (char *)&unk_404230 );
return result;
}

canonical_decompiled_code:
int func17()
{
char *v0; // rbp
char *v1; // rbx
__int64 v2; // rsi
int result; // eax

v0 = (char *)&unk_4040B4;
puts("World:");
do
{
v1 = v0 - 20;
putchar(9);
do
{
v2 = (unsigned __int8)*v1++;
printf("%c ", v2);
}
while ( v1 != v0 );
v0 = v1 + 20;
result = putchar(10);
}
while ( v1 != (char *)&unk_404230 );
return result;
}

original_code:
void printWorld( void )
{
  int y, x;
  printf("World:\n");
  for (y = 0 ; y < 20 ; y++) {
    printf("\t");
    for (x = 0 ; x < 20 ; x++) {
      printf("%c ", world[y][x]);
    }
    printf("\n");
  }
  return;
}

canonical_original_code:
void printWorld()
{
  int y; int x;
  printf("World:\n");
  for (y = 0 ; y < 20 ; y++) {
    printf("\t");
    for (x = 0 ; x < 20 ; x++) {
      printf("%c ", world[y][x]);
    }
    printf("\n");
  }
  return;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm1_8
unsigned int v5; // er12
time_t v6; // rdi
__int64 v7; // rbp
unsigned int v8; // eax
char *v9; // rdx
__m128d v10; // xmm0
__int64 *v11; // rdx
char v13[28]; // [rsp+Ch] [rbp-1Ch] BYREF

v5 = 0;
v6 = time(0LL);
srand(v6);
initWorld(v6, argv);
initAgent();
do
{
while ( 1 )
{
readDetectors(v13);
v7 = (unsigned int)matchClassifiers();
sendEffectors(v7);
system("clear");
printWorld();
v8 = v5 % 0xA;
if ( v5 == 10 * (v5 / 0xA) )
break;
if ( ++v5 == 1000 )
goto LABEL_10;
}
v9 = &list[40 * (int)v7];
v4 = *((double *)v9 + 3);
v10.m128d_f64[0] = *((double *)v9 + 1) - v4;
*(_QWORD *)&v3 = ~(unsigned __int64)*(_OWORD *)&_mm_cmplt_sd(v10, (__m128d)0LL) & COERCE_UNSIGNED_INT64(*((double *)v9 + 1) - v4);
*((double *)v9 + 1) = v3;
v11 = &qword_404248;
do
{
if ( *((_DWORD *)v11 + 7) && (_DWORD)v7 != v8 )
{
*((_DWORD *)v11 + 7) = 0;
v3 = v4 / 10.0 + *(double *)v11;
*(double *)v11 = v3;
}
++v8;
v11 += 5;
}
while ( v8 != 32 );
++v5;
}
while ( v5 != 1000 );
LABEL_10:
printAgent(v3, v4, 10.0);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm1_8
unsigned int v5; // er12
time_t v6; // rdi
__int64 v7; // rbp
unsigned int v8; // eax
char *v9; // rdx
__m128d v10; // xmm0
__int64 *v11; // rdx
char v13[28]; // [rsp+Ch] [rbp-1Ch] BYREF

v5 = 0;
v6 = time(0LL);
srand(v6);
func13(v6, argv);
func15();
do
{
while ( 1 )
{
func8(v13);
v7 = (unsigned int)func12();
sendEffectors(v7);
system("clear");
func17();
v8 = v5 % 0xA;
if ( v5 == 10 * (v5 / 0xA) )
break;
if ( ++v5 == 1000 )
goto LABEL_10;
}
v9 = &list[40 * (int)v7];
v4 = *((double *)v9 + 3);
v10.m128d_f64[0] = *((double *)v9 + 1) - v4;
*(_QWORD *)&v3 = ~(unsigned __int64)*(_OWORD *)&_mm_cmplt_sd(v10, (__m128d)0LL) & COERCE_UNSIGNED_INT64(*((double *)v9 + 1) - v4);
*((double *)v9 + 1) = v3;
v11 = &qword_404248;
do
{
if ( *((_DWORD *)v11 + 7) && (_DWORD)v7 != v8 )
{
*((_DWORD *)v11 + 7) = 0;
v3 = v4 / 10.0 + *(double *)v11;
*(double *)v11 = v3;
}
++v8;
v11 += 5;
}
while ( v8 != 32 );
++v5;
}
while ( v5 != 1000 );
LABEL_10:
func16(v3, v4, 10.0);
return 0;
}

original_code:
int main()
{
  int t, i, c;
  unsigned char detectors[4];
  srand( time(((void *)0)) );
  initWorld();
  initAgent();
  for (t = 0 ; t < 1000 ; t++) {
    readDetectors( detectors );
    c = matchClassifiers( detectors );
    sendEffectors( c );
    system("clear");
    printWorld();
    if ( (t % 10) == 0 ) {
      list[c].strength -= list[c].bid;
      if (list[c].strength < 0.0) list[c].strength = 0.0;
      for (i = 0 ; i < 32 ; i++) {
        if ((list[i].episode) && (i != c)) {
          list[i].strength += ( list[c].bid / (double)10 );
          list[i].episode = 0;
        }
      }
    }
  }
  printAgent();
  return 0;
}

canonical_original_code:
int main()
{
  int t; int i; int c;
  unsigned char detectors[4];
  srand( time(((void *)0)) );
  initWorld();
  initAgent();
  for (t = 0 ; t < 1000 ; t++) {
    readDetectors( detectors );
    c = matchClassifiers( detectors );
    sendEffectors( c );
    system("clear");
    printWorld();
    if ( (t % 10) == 0 ) {
      list[c].strength -= list[c].bid;
      if (list[c].strength < 0.0) list[c].strength = 0.0;
      for (i = 0 ; i < 32 ; i++) {
        if ((list[i].episode) && (i != c)) {
          list[i].strength += ( list[c].bid / (double)10 );
          list[i].episode = 0;
        }
      }
    }
  }
  printAgent();
  return 0;
}

variable_types: t int

variable_types:
detectors
unsigned char [4]


initAgent

original_code:
void initAgent( void )
{
  int c, i;
  for (c = 0 ; c < 32 ; c++) {
    initClassifier( c );
  }
  while (1) {
    agent_y = 1+(int)((double)(20 -2)*rand()/(2147483647 +1.0));
    agent_x = 1+(int)((double)(20 -2)*rand()/(2147483647 +1.0));
    if (world[agent_y][agent_x] == '*') continue;
    for (i = 0 ; i < 4 ; i++) {
      int y = agent_y + dir[i].loc_y;
      int x = agent_x + dir[i].loc_x;
      if (world[y][x] == '*') break;
    }
    if (i == 4) break;
  }
  world[agent_y][agent_x] = '@';
  return;
}

canonical_original_code:
void initAgent()
{
  int c; int i;
  for (c = 0 ; c < 32 ; c++) {
    initClassifier( c );
  }
  while (1) {
    agent_y = 1+(int)((double)(20 -2)*rand()/(2147483647 +1.0));
    agent_x = 1+(int)((double)(20 -2)*rand()/(2147483647 +1.0));
    if (world[agent_y][agent_x] == '*') continue;
    for (i = 0 ; i < 4 ; i++) {
      int y = agent_y + dir[i].loc_y;
      int x = agent_x + dir[i].loc_x;
      if (world[y][x] == '*') break;
    }
    if (i == 4) break;
  }
  world[agent_y][agent_x] = '@';
  return;
}


fireTimer

decompiled_code:
__int64 __fastcall fireTimer(unsigned int a1)
{
__int64 result; // rax
char v2[104]; // [rsp+0h] [rbp-68h] BYREF

sprintf(v2, "(timer-triggered %d)", a1);
result = performAddCommand(v2);
timers[2 * a1] = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
__int64 result; // rax
char v2[104]; // [rsp+0h] [rbp-68h] BYREF

sprintf(v2, "(timer-triggered %d)", a1);
result = performAddCommand(v2);
timers[2 * a1] = 0;
return result;
}

original_code:
int fireTimer( int timerIndex )
{
  int ret;
  char element[80 +1];
  extern int performAddCommand( char *mem );
  sprintf( element, "(timer-triggered %d)", timerIndex );
  ret = performAddCommand( element );
  timers[timerIndex].active = 0;
  return ret;
}

canonical_original_code:
int fireTimer(int timerIndex)
{
  int ret;
  char element[-1];
  extern int performAddCommand(char *mem);
  sprintf( element, "(timer-triggered %d)", timerIndex );
  ret = performAddCommand( element );
  timers[timerIndex].active = 0;
  return ret;
}

variable_types: timerIndex int

variable_types: ret int

variable_types:
element
char [-1]

variable_types:
performAddCommand
int (char *mem)


startTimer

decompiled_code:
void __fastcall startTimer(int a1, int a2)
{
timers[2 * a1 + 1] = a2;
timers[2 * a1] = 1;
}

canonical_decompiled_code:
void __fastcall func1(int a1, int a2)
{
timers[2 * a1 + 1] = a2;
timers[2 * a1] = 1;
}

original_code:
void startTimer( int index, int expiration )
{
  timers[index].expiration = expiration;
  timers[index].active = 1;
  return;
}

canonical_original_code:
void startTimer(int index, int expiration)
{
  timers[index].expiration = expiration;
  timers[index].active = 1;
  return;
}

variable_types: index int

variable_types: expiration int


processTimers

decompiled_code:
__int64 processTimers()
{
__int64 i; // rbx
__int64 result; // rax
unsigned int v3; // edi

for ( i = 0LL; i != 10; ++i )
{
while ( 1 )
{
result = (unsigned int)timers[2 * i];
if ( (_DWORD)result )
{
if ( timers[2 * i + 1]-- == 1 )
break;
}
if ( ++i == 10 )
return result;
}
v3 = i;
result = fireTimer(v3);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 i; // rbx
__int64 result; // rax
unsigned int v3; // edi

for ( i = 0LL; i != 10; ++i )
{
while ( 1 )
{
result = (unsigned int)timers[2 * i];
if ( (_DWORD)result )
{
if ( timers[2 * i + 1]-- == 1 )
break;
}
if ( ++i == 10 )
return result;
}
v3 = i;
result = func0(v3);
}
return result;
}

original_code:
void processTimers( void )
{
  int i;
  for (i = 0 ; i < 10 ; i++) {
    if (timers[i].active) {
      if (--timers[i].expiration == 0) {
        fireTimer( i );
      }
    }
  }
  return;
}

canonical_original_code:
void processTimers()
{
  int i;
  for (i = 0 ; i < 10 ; i++) {
    if (timers[i].active) {
      if (--timers[i].expiration == 0) {
        fireTimer( i );
      }
    }
  }
  return;
}

variable_types: i int


dialog

decompiled_code:
__int64 __fastcall dialog(int fd, void *buf, char *s2, int a4)
{
__int64 result; // rax
int v7; // eax
size_t v8; // r14

if ( fd == -1 )
return 0xFFFFFFFFLL;
if ( !buf )
return 0xFFFFFFFFLL;
if ( *(_BYTE *)buf )
{
v8 = (int)strlen((const char *)buf);
if ( write(fd, buf, v8) != v8 )
return 0xFFFFFFFFLL;
}
result = 0LL;
if ( s2 )
{
v7 = read(fd, buf, 0xC8uLL);
if ( v7 < 0 )
return 0xFFFFFFFFLL;
*((_BYTE *)buf + v7) = 0;
result = (unsigned int)-(strncmp((const char *)buf, s2, a4) != 0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, char *s2, int a4)
{
__int64 result; // rax
int v7; // eax
size_t v8; // r14

if ( fd == -1 )
return 0xFFFFFFFFLL;
if ( !buf )
return 0xFFFFFFFFLL;
if ( *(_BYTE *)buf )
{
v8 = (int)strlen((const char *)buf);
if ( write(fd, buf, v8) != v8 )
return 0xFFFFFFFFLL;
}
result = 0LL;
if ( s2 )
{
v7 = read(fd, buf, 0xC8uLL);
if ( v7 < 0 )
return 0xFFFFFFFFLL;
*((_BYTE *)buf + v7) = 0;
result = (unsigned int)-(strncmp((const char *)buf, s2, a4) != 0);
}
return result;
}

original_code:
int dialog( int sd, char *buffer, char *resp, int rlen )
{
  int ret, len;
  if ((sd == -1) || (!buffer)) return -1;
  if (strlen(buffer) > 0) {
    len = strlen( buffer );
    if ( write( sd, buffer, len ) != len ) return -1;
  }
  if (resp != ((void *)0)) {
    ret = read( sd, buffer, 200 );
    if (ret >= 0) {
      buffer[ret] = 0;
      if (strncmp( buffer, resp, rlen )) return -1;
    } else {
      return -1;
    }
  }
  return 0;
}

canonical_original_code:
int dialog(int sd, char *buffer, char *resp, int rlen)
{
  int ret; int len;
  if ((sd == -1) || (!buffer)) return -1;
  if (strlen(buffer) > 0) {
    len = strlen( buffer );
    if ( write( sd, buffer, len ) != len ) return -1;
  }
  if (resp != ((void *)0)) {
    ret = read( sd, buffer, 200 );
    if (ret >= 0) {
      buffer[ret] = 0;
      if (strncmp( buffer, resp, rlen )) return -1;
    } else {
      return -1;
    }
  }
  return 0;
}

variable_types: sd int

variable_types:
buffer
char *

variable_types:
resp
char *

variable_types: rlen int

variable_types: len int


nntpSetGroup

decompiled_code:
__int64 __fastcall nntpSetGroup(const char *a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = -1;
if ( !a1 )
return 0xFFFFFFFFLL;
result = (unsigned int)sock;
if ( sock != -1 )
{
snprintf(&buffer, 0x50uLL, "group %s\n", a1);
if ( !(unsigned int)dialog(sock, &buffer, "211", 3) )
{
_isoc99_sscanf(&buffer, "211 %d %d %d ", &v3, &firstMessage, &lastMessage);
if ( a2 == -1 )
{
curMessage = firstMessage;
}
else
{
curMessage = a2 + 1;
v3 = lastMessage - a2;
}
printf("Set news group to %s\n", a1);
}
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = -1;
if ( !a1 )
return 0xFFFFFFFFLL;
result = (unsigned int)sock;
if ( sock != -1 )
{
snprintf(&buffer, 0x50uLL, "group %s\n", a1);
if ( !(unsigned int)func1(sock, &buffer, "211", 3) )
{
_isoc99_sscanf(&buffer, "211 %d %d %d ", &v3, &firstMessage, &lastMessage);
if ( a2 == -1 )
{
curMessage = firstMessage;
}
else
{
curMessage = a2 + 1;
v3 = lastMessage - a2;
}
printf("Set news group to %s\n", a1);
}
result = v3;
}
return result;
}

original_code:
int nntpSetGroup( char *group, int lastRead )
{
  int result = -1;
  int numMessages = -1;
  if ((!group) || (sock == -1)) return -1;
  snprintf( buffer, 80, "group %s\n", group );
  result = dialog( sock, buffer, "211", 3 );
  if (result == 0) {
    sscanf( buffer, "211 %d %d %d ",
             &numMessages, &firstMessage, &lastMessage );
    if (lastRead == -1) {
      curMessage = firstMessage;
    } else {
      curMessage = lastRead+1;
      numMessages = lastMessage - lastRead;
    }
    printf("Set news group to %s\n", group);
  }
  return( numMessages );
}

canonical_original_code:
int nntpSetGroup(char *group, int lastRead)
{
  int result = -1;
  int numMessages = -1;
  if ((!group) || (sock == -1)) return -1;
  snprintf( buffer, 80, "group %s\n", group );
  result = dialog( sock, buffer, "211", 3 );
  if (result == 0) {
    sscanf( buffer, "211 %d %d %d ",
             &numMessages, &firstMessage, &lastMessage );
    if (lastRead == -1) {
      curMessage = firstMessage;
    } else {
      curMessage = lastRead+1;
      numMessages = lastMessage - lastRead;
    }
    printf("Set news group to %s\n", group);
  }
  return( numMessages );
}

variable_types:
group
char *

variable_types: lastRead int

variable_types: result int

variable_types: numMessages int


nntpPeek

decompiled_code:
__int64 __fastcall nntpPeek(__int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rcx
size_t v5; // rax
int v6; // esi
int v7; // er8
__int64 v8; // rcx
__int64 i; // rax
int v10; // ebx
_BYTE *v11; // rsi
int v12; // er13
__int64 v13; // r15
__int64 v14; // r14
int v15; // eax
int v16; // er8
int v17; // eax
char v18; // dl
int v19; // [rsp+0h] [rbp-3Ch]

if ( !a1 )
return 0xFFFFFFFFLL;
result = (unsigned int)sock;
if ( sock != -1 )
{
v3 = (unsigned int)curMessage;
if ( curMessage == -1 || curMessage > lastMessage )
{
result = 4294967294LL;
}
else
{
*(_DWORD *)(a1 + 12) = curMessage;
snprintf(buffer, 0x50uLL, "head %d\n", v3);
if ( (int)dialog(sock, buffer, "221", 3) < 0 )
{
result = 4294967293LL;
}
else
{
v5 = strlen(buffer);
v6 = v5;
v7 = v5;
if ( v5 <= 1 )
{
LABEL_13:
v10 = 0;
}
else
{
v8 = (unsigned int)(v5 - 2);
for ( i = 0LL; buffer[i] != 13 || buffer[i + 1] != 10; ++i )
{
if ( v8 == i )
goto LABEL_13;
}
v19 = v6 - (i - 2);
memmove(*(void **)a1, &buffer[(int)i + 2], v19);
v7 = v19;
v10 = v19;
}
v11 = *(_BYTE **)a1;
v12 = 0;
if ( v10 + v7 < a2 - 79 )
{
do
{
v13 = v10;
v14 = v10;
v15 = read(sock, &v11[v10], a2 - v10);
v11 = *(_BYTE **)a1;
v16 = v15;
v17 = v10 + v15;
if ( v17 <= v10 )
goto LABEL_34;
switch ( v12 )
{
case 0:
goto LABEL_21;
case 1:
if ( v11[v10] == 10 )
goto LABEL_24;
while ( 1 )
{
LABEL_19:
if ( v17 <= ++v10 )
{
v12 = 0;
goto LABEL_34;
}
v14 = v10;
LABEL_21:
if ( v11[v14] == 13 )
goto LABEL_22;
}
case 2:
while ( 1 )
{
v18 = v11[v13];
if ( v18 != 13 )
break;
LABEL_22:
if ( v17 <= ++v10 )
{
v12 = 1;
v10 = v17;
goto LABEL_35;
}
if ( v11[v10] != 10 )
goto LABEL_19;
LABEL_24:
++v10;
v12 = 2;
v13 = v10;
if ( v17 <= v10 )
goto LABEL_34;
}
if ( v18 != 46 )
goto LABEL_19;
++v10;
v12 = 3;
v13 = v10;
if ( v17 <= v10 )
goto LABEL_34;
break;
case 3:
break;
default:
goto LABEL_30;
}
if ( v11[v13] != 13 )
goto LABEL_19;
++v10;
v12 = 4;
v13 = v10;
if ( v17 > v10 )
{
LABEL_30:
if ( v11[v13] == 10 )
goto LABEL_31;
goto LABEL_19;
}
LABEL_34:
v10 = v17;
LABEL_35:
;
}
while ( v10 + v16 < a2 - 79 );
}
v17 = v10;
LABEL_31:
result = (unsigned int)(v17 - 3);
v11[(int)result] = 0;
*(_DWORD *)(a1 + 8) = result;
}
}
}
return result;
}

original_code:
int nntpPeek ( news_t *news, int totalLen )
{
  int result = -1, i, len=0, stop, state, bufIdx=0;
  if ((!news) || (sock == -1)) return -1;
  if ((curMessage == -1) || (curMessage > lastMessage)) return -2;
  news->msgId = curMessage;
  snprintf( buffer, 80, "head %d\n", curMessage );
  result = dialog( sock, buffer, "221", 3 );
  if (result < 0) return -3;
  len = strlen( buffer );
  for ( i = 0 ; i < len-1 ; i++ ) {
    if ( (buffer[i] == 0x0d) && (buffer[i+1] == 0x0a) ) {
      len -= i-2;
      memmove( news->msg, &buffer[i+2], len );
      bufIdx = len;
      break;
    }
  }
  state = stop = 0;
  while (!stop) {
    if (bufIdx+len > totalLen - 80) break;
    len = read( sock, &news->msg[bufIdx], (totalLen-bufIdx) );
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (news->msg[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 2) && (news->msg[i] == '.') ) state = 3;
      else if ( (state == 3) && (news->msg[i] == 0x0d) ) state = 4;
      else if ( (state == 4) && (news->msg[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += len;
  }
  bufIdx -= 3;
  news->msg[bufIdx] = 0;
  news->msgLen = bufIdx;
  return bufIdx;
}

canonical_original_code:
int nntpPeek(struct news_t *news, int totalLen)
{
  int result = -1; int i; int len = 0; int stop; int state; int bufIdx = 0;
  if ((!news) || (sock == -1)) return -1;
  if ((curMessage == -1) || (curMessage > lastMessage)) return -2;
  news->msgId = curMessage;
  snprintf( buffer, 80, "head %d\n", curMessage );
  result = dialog( sock, buffer, "221", 3 );
  if (result < 0) return -3;
  len = strlen( buffer );
  for ( i = 0 ; i < len-1 ; i++ ) {
    if ( (buffer[i] == 0x0d) && (buffer[i+1] == 0x0a) ) {
      len -= i-2;
      memmove( news->msg, &buffer[i+2], len );
      bufIdx = len;
      break;
    }
  }
  state = stop = 0;
  while (!stop) {
    if (bufIdx+len > totalLen - 80) break;
    len = read( sock, &news->msg[bufIdx], (totalLen-bufIdx) );
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (news->msg[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 2) && (news->msg[i] == '.') ) state = 3;
      else if ( (state == 3) && (news->msg[i] == 0x0d) ) state = 4;
      else if ( (state == 4) && (news->msg[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += len;
  }
  bufIdx -= 3;
  news->msg[bufIdx] = 0;
  news->msgLen = bufIdx;
  return bufIdx;
}

variable_types: struct news_t { char *msg; int msgLen; int msgId; char subject[-1]; char sender[-1]; char msgDate[-1]; char *bodyStart; } *

variable_types: totalLen int

variable_types: stop int

variable_types: state int

variable_types: bufIdx int


nntpSkip

decompiled_code:
void nntpSkip()
{
++curMessage;
}

canonical_decompiled_code:
void func4()
{
++curMessage;
}

original_code:
void nntpSkip( void )
{
  curMessage++;
}

canonical_original_code:
void nntpSkip()
{
  curMessage++;
}


nntpRetrieve

decompiled_code:
__int64 __fastcall nntpRetrieve(__int64 a1, int a2)
{
__int64 v2; // rcx
size_t v4; // rax
int v5; // esi
int v6; // er15
__int64 v7; // rcx
__int64 i; // rax
int v9; // ebx
int v10; // er14
int v11; // edx
_BYTE *v12; // rsi
int v13; // eax
int v14; // eax
__int64 result; // rax
char v16; // cl

if ( !a1 )
return 0xFFFFFFFFLL;
if ( sock == -1 )
return 0xFFFFFFFFLL;
v2 = (unsigned int)curMessage;
if ( curMessage == -1 || curMessage > lastMessage )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 12) = curMessage;
curMessage = v2 + 1;
snprintf(buffer, 0x50uLL, "article %d\n", v2);
if ( (int)dialog(sock, buffer, "220", 3) < 0 )
return 0xFFFFFFFFLL;
v4 = strlen(buffer);
v5 = v4;
v6 = v4;
if ( v4 > 1 )
{
v7 = (unsigned int)(v4 - 2);
for ( i = 0LL; buffer[i] != 13 || buffer[i + 1] != 10; ++i )
{
if ( v7 == i )
goto LABEL_13;
}
v6 = v5 - (i - 2);
memmove(*(void **)a1, &buffer[(int)i + 2], v6);
}
LABEL_13:
v9 = 0;
v10 = 0;
do
{
v11 = v6 + v9;
v12 = *(_BYTE **)a1;
if ( v6 + v9 >= a2 - 79 )
goto LABEL_27;
if ( v11 <= v9 )
goto LABEL_24;
if ( !v10 )
goto LABEL_21;
if ( v10 == 1 )
goto LABEL_18;
if ( v10 != 2 )
{
if ( v10 == 3 )
{
LABEL_34:
if ( v12[v9] != 13 )
{
LABEL_19:
v13 = v9 + 1;
goto LABEL_20;
}
++v9;
v10 = 4;
if ( v11 <= v9 )
goto LABEL_24;
}
if ( v12[v9] == 10 )
goto LABEL_27;
if ( v11 > ++v9 )
{
LABEL_21:
while ( 1 )
{
v13 = v9 + 1;
if ( v12[v9] == 13 )
break;
LABEL_20:
v9 = v13;
if ( v11 <= v13 )
goto LABEL_38;
}
++v9;
if ( v11 > v13 )
{
LABEL_18:
if ( v12[v9] != 10 )
goto LABEL_19;
goto LABEL_30;
}
LABEL_23:
v10 = 1;
}
else
{
LABEL_38:
v10 = 0;
}
goto LABEL_24;
}
while ( 1 )
{
v16 = v12[v9];
v13 = v9 + 1;
if ( v16 != 13 )
break;
if ( v11 <= v13 )
{
++v9;
goto LABEL_23;
}
if ( v12[v13] != 10 )
{
v13 = v9 + 2;
goto LABEL_20;
}
++v9;
LABEL_30:
++v9;
v10 = 2;
if ( v11 <= v9 )
goto LABEL_24;
}
if ( v16 != 46 )
goto LABEL_20;
++v9;
v10 = 3;
if ( v11 > v13 )
goto LABEL_34;
LABEL_24:
v14 = read(sock, &v12[v9], a2 - v9);
v6 = v14;
}
while ( v14 > 0 && v9 + v14 <= a2 );
v12 = *(_BYTE **)a1;
LABEL_27:
result = (unsigned int)(v9 - 3);
v12[(int)result] = 0;
*(_DWORD *)(a1 + 8) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
__int64 v2; // rcx
size_t v4; // rax
int v5; // esi
int v6; // er15
__int64 v7; // rcx
__int64 i; // rax
int v9; // ebx
int v10; // er14
int v11; // edx
_BYTE *v12; // rsi
int v13; // eax
int v14; // eax
__int64 result; // rax
char v16; // cl

if ( !a1 )
return 0xFFFFFFFFLL;
if ( sock == -1 )
return 0xFFFFFFFFLL;
v2 = (unsigned int)curMessage;
if ( curMessage == -1 || curMessage > lastMessage )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 12) = curMessage;
curMessage = v2 + 1;
snprintf(buffer, 0x50uLL, "article %d\n", v2);
if ( (int)func1(sock, buffer, "220", 3) < 0 )
return 0xFFFFFFFFLL;
v4 = strlen(buffer);
v5 = v4;
v6 = v4;
if ( v4 > 1 )
{
v7 = (unsigned int)(v4 - 2);
for ( i = 0LL; buffer[i] != 13 || buffer[i + 1] != 10; ++i )
{
if ( v7 == i )
goto LABEL_13;
}
v6 = v5 - (i - 2);
memmove(*(void **)a1, &buffer[(int)i + 2], v6);
}
LABEL_13:
v9 = 0;
v10 = 0;
do
{
v11 = v6 + v9;
v12 = *(_BYTE **)a1;
if ( v6 + v9 >= a2 - 79 )
goto LABEL_27;
if ( v11 <= v9 )
goto LABEL_24;
if ( !v10 )
goto LABEL_21;
if ( v10 == 1 )
goto LABEL_18;
if ( v10 != 2 )
{
if ( v10 == 3 )
{
LABEL_34:
if ( v12[v9] != 13 )
{
LABEL_19:
v13 = v9 + 1;
goto LABEL_20;
}
++v9;
v10 = 4;
if ( v11 <= v9 )
goto LABEL_24;
}
if ( v12[v9] == 10 )
goto LABEL_27;
if ( v11 > ++v9 )
{
LABEL_21:
while ( 1 )
{
v13 = v9 + 1;
if ( v12[v9] == 13 )
break;
LABEL_20:
v9 = v13;
if ( v11 <= v13 )
goto LABEL_38;
}
++v9;
if ( v11 > v13 )
{
LABEL_18:
if ( v12[v9] != 10 )
goto LABEL_19;
goto LABEL_30;
}
LABEL_23:
v10 = 1;
}
else
{
LABEL_38:
v10 = 0;
}
goto LABEL_24;
}
while ( 1 )
{
v16 = v12[v9];
v13 = v9 + 1;
if ( v16 != 13 )
break;
if ( v11 <= v13 )
{
++v9;
goto LABEL_23;
}
if ( v12[v13] != 10 )
{
v13 = v9 + 2;
goto LABEL_20;
}
++v9;
LABEL_30:
++v9;
v10 = 2;
if ( v11 <= v9 )
goto LABEL_24;
}
if ( v16 != 46 )
goto LABEL_20;
++v9;
v10 = 3;
if ( v11 > v13 )
goto LABEL_34;
LABEL_24:
v14 = read(sock, &v12[v9], a2 - v9);
v6 = v14;
}
while ( v14 > 0 && v9 + v14 <= a2 );
v12 = *(_BYTE **)a1;
LABEL_27:
result = (unsigned int)(v9 - 3);
v12[(int)result] = 0;
*(_DWORD *)(a1 + 8) = result;
return result;
}

original_code:
int nntpRetrieve ( news_t *news, int totalLen )
{
  int result = -1, i, len=0, stop, state, bufIdx=0;
  if ((!news) || (sock == -1)) return -1;
  if ((curMessage == -1) || (curMessage > lastMessage)) return -1;
  news->msgId = curMessage;
  snprintf( buffer, 80, "article %d\n", curMessage++ );
  result = dialog( sock, buffer, "220", 3 );
  if (result < 0) return -1;
  len = strlen(buffer);
  for ( i = 0 ; i < len-1 ; i++ ) {
    if ( (buffer[i] == 0x0d) && (buffer[i+1] == 0x0a) ) {
      len -= i-2;
      memmove( news->msg, &buffer[i+2], len );
      break;
    }
  }
  state = stop = 0;
  while (!stop) {
    if (bufIdx+len > totalLen - 80) break;
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (news->msg[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 2) && (news->msg[i] == '.') ) state = 3;
      else if ( (state == 3) && (news->msg[i] == 0x0d) ) state = 4;
      else if ( (state == 4) && (news->msg[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += (i-bufIdx);
    if (!stop) {
      len = read( sock, &news->msg[bufIdx], (totalLen-bufIdx) );
      if ( (len <= 0) || (bufIdx+len > totalLen) ) {
        break;
      }
    }
  }
  bufIdx -= 3;
  news->msg[bufIdx] = 0;
  news->msgLen = bufIdx;
  return bufIdx;
}

canonical_original_code:
int nntpRetrieve(struct news_t *news, int totalLen)
{
  int result = -1; int i; int len = 0; int stop; int state; int bufIdx = 0;
  if ((!news) || (sock == -1)) return -1;
  if ((curMessage == -1) || (curMessage > lastMessage)) return -1;
  news->msgId = curMessage;
  snprintf( buffer, 80, "article %d\n", curMessage++ );
  result = dialog( sock, buffer, "220", 3 );
  if (result < 0) return -1;
  len = strlen(buffer);
  for ( i = 0 ; i < len-1 ; i++ ) {
    if ( (buffer[i] == 0x0d) && (buffer[i+1] == 0x0a) ) {
      len -= i-2;
      memmove( news->msg, &buffer[i+2], len );
      break;
    }
  }
  state = stop = 0;
  while (!stop) {
    if (bufIdx+len > totalLen - 80) break;
    for ( i = bufIdx ; i < bufIdx+len ; i++ ) {
      if ( (state == 0) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 1) && (news->msg[i] == 0x0a) ) state = 2;
      else if ( (state == 2) && (news->msg[i] == 0x0d) ) state = 1;
      else if ( (state == 2) && (news->msg[i] == '.') ) state = 3;
      else if ( (state == 3) && (news->msg[i] == 0x0d) ) state = 4;
      else if ( (state == 4) && (news->msg[i] == 0x0a) ) { stop = 1; break; }
      else state = 0;
    }
    bufIdx += (i-bufIdx);
    if (!stop) {
      len = read( sock, &news->msg[bufIdx], (totalLen-bufIdx) );
      if ( (len <= 0) || (bufIdx+len > totalLen) ) {
        break;
      }
    }
  }
  bufIdx -= 3;
  news->msg[bufIdx] = 0;
  news->msgLen = bufIdx;
  return bufIdx;
}

user_defined_types: struct news_t { char *msg; int msgLen; int msgId; char subject[-1]; char sender[-1]; char msgDate[-1]; char *bodyStart; }


nntpParse

decompiled_code:
__int64 __fastcall nntpParse(__int64 a1, int a2)
{
int v2; // er12

if ( !a1 || (int)parseEntry(a1, "Subject:", a1 + 16) < 0 )
return (unsigned int)-1;
if ( (int)parseEntry(a1, "Date:", a1 + 218) < 0 )
return (unsigned int)-2;
v2 = parseEntry(a1, "From:", a1 + 117);
if ( v2 < 0 )
return (unsigned int)-3;
fixAddress(a1 + 117);
if ( a2 )
return (unsigned int)v2;
return findBody(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
int v2; // er12

if ( !a1 || (int)parseEntry(a1, "Subject:", a1 + 16) < 0 )
return (unsigned int)-1;
if ( (int)parseEntry(a1, "Date:", a1 + 218) < 0 )
return (unsigned int)-2;
v2 = parseEntry(a1, "From:", a1 + 117);
if ( v2 < 0 )
return (unsigned int)-3;
fixAddress(a1 + 117);
if ( a2 )
return (unsigned int)v2;
return findBody(a1);
}

original_code:
int nntpParse( news_t *news, unsigned int flags )
{
  int result;
  if (!news) return -1;
  result = parseEntry( news, "Subject:", news->subject );
  if (result < 0) return -1;
  result = parseEntry( news, "Date:", news->msgDate );
  if (result < 0) return -2;
  result = parseEntry( news, "From:", news->sender );
  if (result < 0) return -3;
  fixAddress( news->sender );
  if (flags == 0) {
    result = findBody( news );
  }
  return result;
}

canonical_original_code:
int nntpParse(struct news_t *news, unsigned int flags)
{
  int result;
  if (!news) return -1;
  result = parseEntry( news, "Subject:", news->subject );
  if (result < 0) return -1;
  result = parseEntry( news, "Date:", news->msgDate );
  if (result < 0) return -2;
  result = parseEntry( news, "From:", news->sender );
  if (result < 0) return -3;
  fixAddress( news->sender );
  if (flags == 0) {
    result = findBody( news );
  }
  return result;
}

variable_types:
flags
unsigned int


nntpDisconnect

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

result = (unsigned int)sock;
if ( sock != -1 )
{
close(sock);
result = 0LL;
lastMessage = -1;
firstMessage = -1;
curMessage = -1;
sock = -1;
}
return result;
}

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

result = (unsigned int)sock;
if ( sock != -1 )
{
close(sock);
result = 0LL;
lastMessage = -1;
firstMessage = -1;
curMessage = -1;
sock = -1;
}
return result;
}

original_code:
int nntpDisconnect ( void )
{
  if (sock == -1) return -1;
  close(sock);
  sock = curMessage = firstMessage = lastMessage = -1;
  return 0;
}

canonical_original_code:
int nntpDisconnect()
{
  if (sock == -1) return -1;
  close(sock);
  sock = curMessage = firstMessage = lastMessage = -1;
  return 0;
}


nntpConnect

decompiled_code:
__int64 __fastcall nntpConnect(char *name)
{
int v1; // er12
struct hostent *v3; // rax
struct sockaddr v4; // [rsp+0h] [rbp-18h] BYREF

if ( !name )
return (unsigned int)-1;
sock = socket(2, 1, 0);
v4 = 0LL;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = _bswap_16_constprop_0(name);
*(_DWORD *)&v4.sa_data[2] = inet_addr(name);
if ( *(_DWORD *)&v4.sa_data[2] == -1 )
{
v3 = gethostbyname(name);
if ( v3 )
{
*(_DWORD *)&v4.sa_data[2] = **(_DWORD **)v3->h_addr_list;
goto LABEL_3;
}
return (unsigned int)-1;
}
LABEL_3:
printf("Connecting to %s\n", name);
v1 = connect(sock, &v4, 0x10u);
if ( v1 < 0 )
return (unsigned int)v1;
buffer[0] = 0;
v1 = dialog(sock, buffer, "20", 2);
if ( v1 >= 0 )
return (unsigned int)v1;
nntpDisconnect();
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *name)
{
int v1; // er12
struct hostent *v3; // rax
struct sockaddr v4; // [rsp+0h] [rbp-18h] BYREF

if ( !name )
return (unsigned int)-1;
sock = socket(2, 1, 0);
v4 = 0LL;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = func0(name);
*(_DWORD *)&v4.sa_data[2] = inet_addr(name);
if ( *(_DWORD *)&v4.sa_data[2] == -1 )
{
v3 = gethostbyname(name);
if ( v3 )
{
*(_DWORD *)&v4.sa_data[2] = **(_DWORD **)v3->h_addr_list;
goto LABEL_3;
}
return (unsigned int)-1;
}
LABEL_3:
printf("Connecting to %s\n", name);
v1 = connect(sock, &v4, 0x10u);
if ( v1 < 0 )
return (unsigned int)v1;
buffer[0] = 0;
v1 = func1(sock, buffer, "20", 2);
if ( v1 >= 0 )
return (unsigned int)v1;
func7();
return (unsigned int)v1;
}

original_code:
int nntpConnect ( char *nntpServer )
{
  int result = -1;
  struct sockaddr_in servaddr;
  if (!nntpServer) return -1;
  sock = socket( 2, SOCK_STREAM, 0 );
  bzero( &servaddr, sizeof(servaddr) );
  servaddr.sin_family = 2;
  servaddr.sin_port = htons( 119 );
  servaddr.sin_addr.s_addr = inet_addr( nntpServer );
  if ( servaddr.sin_addr.s_addr == 0xffffffff ) {
    struct hostent *hptr = (struct hostent *)gethostbyname( nntpServer );
    if ( hptr == ((void *)0) ) {
      return -1;
    } else {
      struct in_addr **addrs;
      addrs = (struct in_addr **)hptr->h_addr_list;
      memcpy( &servaddr.sin_addr, *addrs, sizeof(struct in_addr) );
    }
  }
  printf( "Connecting to %s\n", nntpServer );
  result = connect( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
  if ( result >= 0 ) {
    buffer[0] = 0;
    result = dialog( sock, buffer, "20", 2 );
    if (result < 0) nntpDisconnect();
  }
  return ( result );
}

canonical_original_code:
int nntpConnect(char *nntpServer)
{
  int result = -1;
  struct sockaddr_in servaddr;
  if (!nntpServer) return -1;
  sock = socket( 2, SOCK_STREAM, 0 );
  bzero( &servaddr, sizeof(servaddr) );
  servaddr.sin_family = 2;
  servaddr.sin_port = htons( 119 );
  servaddr.sin_addr.s_addr = inet_addr( nntpServer );
  if ( servaddr.sin_addr.s_addr == 0xffffffff ) {
    struct hostent *hptr = (struct hostent *)gethostbyname( nntpServer );
    if ( hptr == ((void *)0) ) {
      return -1;
    } else {
      struct in_addr **addrs;
      addrs = (struct in_addr **)hptr->h_addr_list;
      memcpy( &servaddr.sin_addr, *addrs, sizeof(struct in_addr) );
    }
  }
  printf( "Connecting to %s\n", nntpServer );
  result = connect( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
  if ( result >= 0 ) {
    buffer[0] = 0;
    result = dialog( sock, buffer, "20", 2 );
    if (result < 0) nntpDisconnect();
  }
  return ( result );
}

variable_types:
nntpServer
char *

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

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

variable_types: struct in_addr { unsigned int s_addr; } **

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

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