comstemtypes

original_code:
int
comstemtypes(char *stem, char *stemkeys, char *endkeys)
{
 register char * s, *p;
 char tmp[1024];
 char cstemtype[1024];
 char cstem[60];
 char clemma[60];
 char stembuf[1024];
 s = stemkeys;
 p = tmp;
 stembuf[0] = tmp[0] = 0;
 while(*s) {
  setstemvars(s,cstem,clemma,cstemtype,stembuf);
  if( wantcurstemtype(cstemtype,endkeys) ) {
   if( tmp[0] ) Xstrncat(tmp," ",(int)1024);
   Xstrncat(tmp,clemma,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   if( cstem[0] ) Xstrncat(tmp,cstem,(int)1024);
   else Xstrncat(tmp,stem,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   Xstrncat(tmp,cstemtype,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   if(*stembuf)
    Xstrncat(tmp,stembuf,(int)1024);
  }
  while(! ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISspace) && *s ) s++;
  while( ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISspace) ) s++;
 }
 Xstrncpy((char *)stemkeys,(char *)p,(size_t)1024);
 if( *stemkeys )
  return(1);
 else
  return(0);
}

canonical_original_code:
int comstemtypes(char *stem, char *stemkeys, char *endkeys)
{
 register char *s; register char *p;
 char tmp[1024];
 char cstemtype[1024];
 char cstem[60];
 char clemma[60];
 char stembuf[1024];
 s = stemkeys;
 p = tmp;
 stembuf[0] = tmp[0] = 0;
 while(*s) {
  setstemvars(s,cstem,clemma,cstemtype,stembuf);
  if( wantcurstemtype(cstemtype,endkeys) ) {
   if( tmp[0] ) Xstrncat(tmp," ",(int)1024);
   Xstrncat(tmp,clemma,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   if( cstem[0] ) Xstrncat(tmp,cstem,(int)1024);
   else Xstrncat(tmp,stem,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   Xstrncat(tmp,cstemtype,(int)1024);
   Xstrncat(tmp,":",(int)1024);
   if(*stembuf)
    Xstrncat(tmp,stembuf,(int)1024);
  }
  while(! ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISspace) && *s ) s++;
  while( ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISspace) ) s++;
 }
 Xstrncpy((char *)stemkeys,(char *)p,(unsigned long)1024);
 if( *stemkeys )
  return(1);
 else
  return(0);
}

variable_types:
stem
char *

variable_types:
stemkeys
char *

variable_types:
endkeys
char *

variable_types:
p
char *

variable_types:
tmp
char [1024]

variable_types:
cstemtype
char [1024]

variable_types:
cstem
char [60]

variable_types:
clemma
char [60]

variable_types:
stembuf
char [1024]


getsyll

repo: PerseusDL/morpheus

decompiled_code:
unsigned __int64 __fastcall getsyll(unsigned __int64 a1, unsigned int a2)
{
unsigned __int64 v2; // rbx
int v4; // ebp
__int64 v5; // r14

v2 = -1LL;
if ( a2 <= 2 )
{
v2 = a1 + (int)Xstrlen() - 1;
if ( a1 > v2 )
{
v2 = -1LL;
}
else
{
v4 = 0;
v5 = 0x50419100504191LL;
do
{
if ( (unsigned __int8)(*(_BYTE *)v2 - 65) <= 0x36u && _bittest64(&v5, (unsigned int)*(unsigned __int8 *)v2 - 65) )
{
if ( a2 == v4 )
return v2;
v4 += is_diphth(v2, a1) == 0;
}
--v2;
}
while ( a1 <= v2 );
v2 = -1LL;
}
}
return v2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int64 a1, unsigned int a2)
{
unsigned __int64 v2; // rbx
int v4; // ebp
__int64 v5; // r14

v2 = -1LL;
if ( a2 <= 2 )
{
v2 = a1 + (int)Xstrlen() - 1;
if ( a1 > v2 )
{
v2 = -1LL;
}
else
{
v4 = 0;
v5 = 0x50419100504191LL;
do
{
if ( (unsigned __int8)(*(_BYTE *)v2 - 65) <= 0x36u && _bittest64(&v5, (unsigned int)*(unsigned __int8 *)v2 - 65) )
{
if ( a2 == v4 )
return v2;
v4 += is_diphth(v2, a1) == 0;
}
--v2;
}
while ( a1 <= v2 );
v2 = -1LL;
}
}
return v2;
}

variable_types:
word
char *

variable_types: syll int

variable_types: syllcount int

return_type: char *


decompiled_code:
__int64 __fastcall getsyll2(unsigned __int64 a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned __int64 v4; // rax

v2 = a1;
if ( (unsigned int)nsylls() )
{
v4 = getsyll(a1, a2);
if ( v4 == -1LL )
v2 = -1LL;
else
v2 = ((unsigned int)is_diphth(v4, a1) == 0) + v4 - 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned __int64 v4; // rax

v2 = a1;
if ( (unsigned int)nsylls() )
{
v4 = func0(a1, a2);
if ( v4 == -1LL )
v2 = -1LL;
else
v2 = ((unsigned int)is_diphth(v4, a1) == 0) + v4 - 1;
}
return v2;
}

original_code:
char *getsyll2(char *word, int syll)
{
 char * p;
 if( nsylls(word) == 0 ) return(word);
 p = getsyll(word,syll);
 if( p == (char *) -1 ) return(p);
 if( is_diphth(p,word) )
  return(p-1);
 else
  return(p);
}

canonical_original_code:
char *getsyll2(char *word, int syll)
{
 char *p;
 if( nsylls(word) == 0 ) return(word);
 p = getsyll(word,syll);
 if( p == (char *) -1 ) return(p);
 if( is_diphth(p,word) )
  return(p-1);
 else
  return(p);
}


decompiled_code:
__int64 __fastcall load_ccontr(__int64 a1)
{
return load_euph_tab("rule_files/conseuph.table", a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return load_euph_tab("rule_files/conseuph.table", a1, 1LL);
}

original_code:
gk_string *
load_ccontr(int *gotno)
{
 return(load_euph_tab("rule_files/conseuph.table",gotno,1));
}

canonical_original_code:
struct gk_string *load_ccontr(int *gotno)
{
 return(load_euph_tab("rule_files/conseuph.table",gotno,1));
}

variable_types:
gotno
int *

return_type: struct gk_string { struct word_form { unsigned int f_voice; unsigned int f_mood; unsigned int f_tense; unsigned int f_person; unsigned int f_number; unsigned int f_case; unsigned int f_degree; unsigned int f_gender; } gs_forminfo; unsigned int gs_steminfo; unsigned int gs_derivtype; short gs_dialect; unsigned int gs_geogregion; unsigned char gs_morphflags[12]; char st_domains[-1]; char gs_gkstring[60]; } *

user_defined_types: struct gk_string { struct word_form gs_forminfo; unsigned int gs_steminfo; unsigned int gs_derivtype; short gs_dialect; unsigned int gs_geogregion; unsigned char gs_morphflags[12]; char st_domains[-1]; char gs_gkstring[60]; }


decompiled_code:
__int64 __fastcall load_vcontr(__int64 a1)
{
return load_euph_tab("rule_files/vowcontr.table", a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return load_euph_tab("rule_files/vowcontr.table", a1, 1LL);
}

original_code:
gk_string *
load_vcontr(int *gotno)
{
 return(load_euph_tab("rule_files/vowcontr.table",gotno,1));
}

canonical_original_code:
struct gk_string *load_vcontr(int *gotno)
{
 return(load_euph_tab("rule_files/vowcontr.table",gotno,1));
}

user_defined_types: struct word_form { unsigned int f_voice; unsigned int f_mood; unsigned int f_tense; unsigned int f_person; unsigned int f_number; unsigned int f_case; unsigned int f_degree; unsigned int f_gender; }


decompiled_code:
__int64 init_dict()
{
if ( Use_hqdict )
{
vbindex = "hqdict/indices/stindex";
nomindex = "hqdict/indices/stindex";
}
return init_preind();
}

canonical_decompiled_code:
__int64 func0()
{
if ( Use_hqdict )
{
vbindex = "hqdict/indices/stindex";
nomindex = "hqdict/indices/stindex";
}
return init_preind();
}

original_code:
endtags *
init_dict(char *fname, int *ntags)
{
 if( Use_hqdict ) {
  fname = "hqdict/indices/stindex";
  vbindex = "hqdict/indices/stindex";
  nomindex = "hqdict/indices/stindex";
 }
 return(init_preind(fname,ntags));
}

canonical_original_code:
struct endtags *init_dict(char *fname, int *ntags)
{
 if( Use_hqdict ) {
  fname = "hqdict/indices/stindex";
  vbindex = "hqdict/indices/stindex";
  nomindex = "hqdict/indices/stindex";
 }
 return(init_preind(fname,ntags));
}

variable_types:
fname
char *

variable_types:
ntags
int *

return_type: struct endtags { char tagstring[-1]; long tagoffset; } *


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

v1 = a1;
if ( (int)nsylls(a1) <= 3 && (unsigned int)nsylls(a1) != 1 )
{
if ( (unsigned int)nsylls(a1) == 3 )
{
v1 = getsyll2(a1, 1LL);
}
else if ( (unsigned int)nsylls(a1) == 2 )
{
v1 = getsyll2(a1, 0LL);
}
}
return v1;
}

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

v1 = a1;
if ( (int)nsylls(a1) <= 3 && (unsigned int)nsylls(a1) != 1 )
{
if ( (unsigned int)nsylls(a1) == 3 )
{
v1 = getsyll2(a1, 1LL);
}
else if ( (unsigned int)nsylls(a1) == 2 )
{
v1 = getsyll2(a1, 0LL);
}
}
return v1;
}

original_code:
char *
skip_to_syll(char *s, int nsyll)
{
 if( nsylls(s) > 3 || nsylls(s) == 1) {
  return(s);
 } else if( nsylls(s) == 3 ) {
  return(getsyll2(s,1));
 }
 else if( nsylls(s) == 2 ) {
  return(getsyll2(s,0));
 } else
  return(s);
}

canonical_original_code:
char *skip_to_syll(char *s, int nsyll)
{
 if( nsylls(s) > 3 || nsylls(s) == 1) {
  return(s);
 } else if( nsylls(s) == 3 ) {
  return(getsyll2(s,1));
 }
 else if( nsylls(s) == 2 ) {
  return(getsyll2(s,0));
 } else
  return(s);
}

variable_types:
s
char *

variable_types: nsyll int


variable_types: changed int

return_type: int


getquantity

decompiled_code:
__int64 __fastcall getquantity(__int64 a1, unsigned int a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 v7; // rax
char v8; // al
__int64 result; // rax

v7 = getsyll();
if ( v7 == -1 )
return 0xFFFFFFFFLL;
v8 = *(_BYTE *)(v7 + 1);
if ( (unsigned __int8)(v8 - 94) > 1u )
result = quantprim(a1, a2, a4, a5);
else
result = v8 == 95;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 v7; // rax
char v8; // al
__int64 result; // rax

v7 = getsyll();
if ( v7 == -1 )
return 0xFFFFFFFFLL;
v8 = *(_BYTE *)(v7 + 1);
if ( (unsigned __int8)(v8 - 94) > 1u )
result = quantprim(a1, a2, a4, a5);
else
result = v8 == 95;
return result;
}

original_code:
int getquantity(char *word, int syll, char *nom, bool is_ending, bool is_oblique)
{
 register char *p;
 p = getsyll(word,syll);
 if (p == (char *) -1)
  return (-1);
 p++;
 if ((((*p)=='_') || ((*p)=='^'))) {
  if ( (*p)=='_')
   return (1);
  else if ( (*p)=='^')
   return (0);
  else {
   if (nom == 0)
    return (0);
   else if (getaccent(nom,1)=='/')
    return (1);
   else
    return (0);
   }
  }
 else
  return (quantprim(word,syll,is_ending,is_oblique));
}

canonical_original_code:
int getquantity(char *word, int syll, char *nom, int is_ending, int is_oblique)
{
 register char *p;
 p = getsyll(word,syll);
 if (p == (char *) -1)
  return (-1);
 p++;
 if ((((*p)=='_') || ((*p)=='^'))) {
  if ( (*p)=='_')
   return (1);
  else if ( (*p)=='^')
   return (0);
  else {
   if (nom == 0)
    return (0);
   else if (getaccent(nom,1)=='/')
    return (1);
   else
    return (0);
   }
  }
 else
  return (quantprim(word,syll,is_ending,is_oblique));
}

variable_types:
nom
char *


getaccent

decompiled_code:
__int64 getaccent()
{
unsigned __int8 *v0; // rax
int v1; // edx
__int64 result; // rax
__int64 v3; // rax

v0 = (unsigned __int8 *)getaccp();
if ( v0 == (unsigned __int8 *)-1LL )
return 0xFFFFFFFFLL;
v1 = *v0;
result = 32LL;
if ( (unsigned __int8)(v1 - 47) <= 0x2Du )
{
v3 = 0x200000004001LL;
if ( _bittest64(&v3, (unsigned int)(v1 - 47)) )
result = (unsigned int)(char)v1;
else
result = 32LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned __int8 *v0; // rax
int v1; // edx
__int64 result; // rax
__int64 v3; // rax

v0 = (unsigned __int8 *)getaccp();
if ( v0 == (unsigned __int8 *)-1LL )
return 0xFFFFFFFFLL;
v1 = *v0;
result = 32LL;
if ( (unsigned __int8)(v1 - 47) <= 0x2Du )
{
v3 = 0x200000004001LL;
if ( _bittest64(&v3, (unsigned int)(v1 - 47)) )
result = (unsigned int)(char)v1;
else
result = 32LL;
}
return result;
}

original_code:
int getaccent(char *word, int syll)
{
 register char *p;
 p = getaccp(word,syll);
 if (p == (char *) -1)
  return ((char) -1);
 if (((*p)=='/')||((*p)=='\\')||((*p)=='='))
  return (*p);
 return (' ');
}


CheckEndCache

decompiled_code:
__int64 __fastcall CheckEndCache(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rdx

cacheconsistent();
v1 = EndingCache;
while ( 1 )
{
v2 = *v1;
if ( !*v1 || *(_DWORD *)(v2 + 4) == *(_DWORD *)(a1 + 4) )
break;
if ( ++v1 == (__int64 *)(unsigned int)((_DWORD)EndingCache + 360) )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rdx

func0();
v1 = EndingCache;
while ( 1 )
{
v2 = *v1;
if ( !*v1 || *(_DWORD *)(v2 + 4) == *(_DWORD *)(a1 + 4) )
break;
if ( ++v1 == (__int64 *)(unsigned int)((_DWORD)EndingCache + 360) )
return 0LL;
}
return v2;
}

original_code:
gk_string *
CheckEndCache(gk_string *gstr)
{
 int i;
cacheconsistent();
 for(i=0;i<45;i++) {
  if(! EndingCache[i] ) break;
  if( (EndingCache[i])->gs_steminfo == (gstr)->gs_steminfo)
   return(EndingCache[i]);
 }
 return(((void *)0));
}

canonical_original_code:
struct gk_string *CheckEndCache(struct gk_string *gstr)
{
 int i;
cacheconsistent();
 for(i=0;i<45;i++) {
  if(! EndingCache[i] ) break;
  if( (EndingCache[i])->gs_steminfo == (gstr)->gs_steminfo)
   return(EndingCache[i]);
 }
 return(((void *)0));
}

variable_types:
gstr
struct gk_string *

variable_types: i int


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

for ( result = (unsigned __int8)*a1; (_BYTE)result; result = (unsigned __int8)*a1 )
{
if ( (_BYTE)result == 61 )
*a1 = 95;
++a1;
}
return result;
}

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

for ( result = (unsigned __int8)*a1; (_BYTE)result; result = (unsigned __int8)*a1 )
{
if ( (_BYTE)result == 61 )
*a1 = 95;
++a1;
}
return result;
}

original_code:
void mark_long(char* word)
{
 char* p_pos = word;
 for ( ; *p_pos; p_pos ++)
 {
   if (*p_pos == '=')
     *p_pos = '_';
 }
}

canonical_original_code:
void mark_long(char *word)
{
 char *p_pos = word;
 for ( ; *p_pos; p_pos ++)
 {
   if (*p_pos == '=')
     *p_pos = '_';
 }
}

return_type: void


decompiled_code:
__int64 __fastcall get_all_syllables(unsigned __int64 a1, int a2)
{
unsigned __int8 *v3; // rbx
int v4; // ebp
__int64 v5; // r14
int v6; // edi

v3 = (unsigned __int8 *)(a1 + (int)Xstrlen() - 1);
if ( a1 > (unsigned __int64)v3 )
return -1LL;
v4 = 0;
v5 = 0x50419100504191LL;
do
{
v6 = *v3;
if ( (unsigned __int8)(v6 - 65) <= 0x36u && _bittest64(&v5, (unsigned int)(v6 - 65)) )
{
if ( a2 == v4 )
return (__int64)v3;
v4 += is_diphth((unsigned int)(char)v6, a1) == 0;
}
--v3;
}
while ( a1 <= (unsigned __int64)v3 );
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 a1, int a2)
{
unsigned __int8 *v3; // rbx
int v4; // ebp
__int64 v5; // r14
int v6; // edi

v3 = (unsigned __int8 *)(a1 + (int)Xstrlen() - 1);
if ( a1 > (unsigned __int64)v3 )
return -1LL;
v4 = 0;
v5 = 0x50419100504191LL;
do
{
v6 = *v3;
if ( (unsigned __int8)(v6 - 65) <= 0x36u && _bittest64(&v5, (unsigned int)(v6 - 65)) )
{
if ( a2 == v4 )
return (__int64)v3;
v4 += is_diphth((unsigned int)(char)v6, a1) == 0;
}
--v3;
}
while ( a1 <= (unsigned __int64)v3 );
return -1LL;
}

variable_types: which int


fullquantity

decompiled_code:
__int64 __fastcall fullquantity(unsigned __int64 a1, int a2)
{
__int64 v2; // rax
unsigned __int8 *v3; // rsi
char v4; // al
__int64 result; // rax

v2 = get_all_syllables(a1, a2);
if ( v2 == -1 )
return 0xFFFFFFFFLL;
v3 = (unsigned __int8 *)v2;
v4 = *(_BYTE *)(v2 + 1);
if ( (unsigned __int8)(v4 - 94) > 1u )
result = basic_quantity(a1, v3);
else
result = v4 == 95;
return result;
}

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

v2 = func1(a1, a2);
if ( v2 == -1 )
return 0xFFFFFFFFLL;
v3 = (unsigned __int8 *)v2;
v4 = *(_BYTE *)(v2 + 1);
if ( (unsigned __int8)(v4 - 94) > 1u )
result = func2(a1, v3);
else
result = v4 == 95;
return result;
}

original_code:
int fullquantity(char* word, int syll, bool is_ending, bool is_oblique)
{
 char* p_syll;
 p_syll = get_all_syllables(word, syll);
 if (p_syll == (char *) -1)
   return (-1);
 p_syll++;
 if (*p_syll && (((*p_syll)=='_') || ((*p_syll)=='^')))
 {
   if ( (*p_syll) == '_')
     return (1);
   else if ( (*p_syll) == '^')
     return (0);
   else
     return(-1);
 }
 else
 {
   -- p_syll;
   return(basic_quantity(word, p_syll));
 }
}

canonical_original_code:
int fullquantity(char *word, int syll, int is_ending, int is_oblique)
{
 char *p_syll;
 p_syll = get_all_syllables(word, syll);
 if (p_syll == (char *) -1)
   return (-1);
 p_syll++;
 if (*p_syll && (((*p_syll)=='_') || ((*p_syll)=='^')))
 {
   if ( (*p_syll) == '_')
     return (1);
   else if ( (*p_syll) == '^')
     return (0);
   else
     return(-1);
 }
 else
 {
   -- p_syll;
   return(basic_quantity(word, p_syll));
 }
}


naccents

decompiled_code:
__int64 __fastcall naccents(char *a1)
{
char v1; // cl
unsigned int v2; // edx
unsigned __int8 v3; // cl

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v3 = v1 - 47;
if ( v3 <= 0x2Du )
v2 += (0x200000004001uLL >> v3) & 1;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // cl
unsigned int v2; // edx
unsigned __int8 v3; // cl

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v3 = v1 - 47;
if ( v3 <= 0x2Du )
v2 += (0x200000004001uLL >> v3) & 1;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

variable_types:
p
char *

variable_types: r int


decompiled_code:
_BOOL8 __fastcall is_diphth(char *a1, unsigned __int64 a2)
{
_BOOL8 result; // rax
char v3; // r12
int v4; // ebp
const unsigned __int16 **v5; // rax
const unsigned __int16 *v6; // r13
char v7; // al
int v8; // edi
unsigned __int64 v9; // rax
__int64 v10; // rcx
unsigned __int64 v11; // rdx
__int64 v12; // rax

result = 0LL;
if ( (unsigned __int64)(a1 - 1) >= a2 && a1[1] != 43 )
{
v3 = *a1;
v4 = *a1;
v5 = _ctype_b_loc();
v6 = *v5;
if ( ((*v5)[v3] & 0x100) != 0 )
v4 = tolower(v4);
if ( v4 == 105 || (result = 0LL, v4 == 117) )
{
v7 = *(a1 - 1);
v8 = v7;
if ( (v6[v7] & 0x100) != 0 )
v8 = tolower(v7);
v9 = (unsigned int)(v8 - 97);
if ( (unsigned int)v9 > 0xE )
{
v11 = (unsigned int)(v8 - 65);
result = 0LL;
if ( (unsigned int)v11 > 0x36 )
return result;
}
else
{
v10 = 16657LL;
if ( _bittest64(&v10, v9) )
{
if ( (v8 & 0xFFFFFFFB) == 97 || v8 == 111 )
return 1LL;
LABEL_14:
if ( v4 == 105 )
result = v8 == 117;
else
result = v8 == 104;
return result;
}
v11 = (unsigned int)(v8 - 65);
}
v12 = 0x50008000504191LL;
if ( !_bittest64(&v12, v11) )
return 0LL;
goto LABEL_14;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(char *a1, unsigned __int64 a2)
{
_BOOL8 result; // rax
char v3; // r12
int v4; // ebp
const unsigned __int16 **v5; // rax
const unsigned __int16 *v6; // r13
char v7; // al
int v8; // edi
unsigned __int64 v9; // rax
__int64 v10; // rcx
unsigned __int64 v11; // rdx
__int64 v12; // rax

result = 0LL;
if ( (unsigned __int64)(a1 - 1) >= a2 && a1[1] != 43 )
{
v3 = *a1;
v4 = *a1;
v5 = _ctype_b_loc();
v6 = *v5;
if ( ((*v5)[v3] & 0x100) != 0 )
v4 = tolower(v4);
if ( v4 == 105 || (result = 0LL, v4 == 117) )
{
v7 = *(a1 - 1);
v8 = v7;
if ( (v6[v7] & 0x100) != 0 )
v8 = tolower(v7);
v9 = (unsigned int)(v8 - 97);
if ( (unsigned int)v9 > 0xE )
{
v11 = (unsigned int)(v8 - 65);
result = 0LL;
if ( (unsigned int)v11 > 0x36 )
return result;
}
else
{
v10 = 16657LL;
if ( _bittest64(&v10, v9) )
{
if ( (v8 & 0xFFFFFFFB) == 97 || v8 == 111 )
return 1LL;
LABEL_14:
if ( v4 == 105 )
result = v8 == 117;
else
result = v8 == 104;
return result;
}
v11 = (unsigned int)(v8 - 65);
}
v12 = 0x50008000504191LL;
if ( !_bittest64(&v12, v11) )
return 0LL;
goto LABEL_14;
}
}
return result;
}


Xstrncpy

decompiled_code:
__int64 __fastcall Xstrncpy(char *a1, const char *a2)
{
strcpy(a1, a2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, const char *a2)
{
strcpy(a1, a2);
return 1LL;
}

original_code:
int
Xstrncpy(char *s1, const char *s2, size_t len)
{
strcpy(s1,s2);
return(1);
}

canonical_original_code:
int Xstrncpy(char *s1, char *s2, unsigned long len)
{
strcpy(s1,s2);
return(1);
}

variable_types:
len
unsigned long


Ystrncpy

decompiled_code:
int __fastcall Ystrncpy(char *dest, char *src, size_t n)
{
char *v4; // rax
const char *v5; // r12
unsigned int v6; // eax
int result; // eax

if ( (int)Xstrlen(src) < n )
return (unsigned int)strcpy(dest, src);
if ( n - 5 > 0x7FFB )
fprintf(stderr, "Xstrncpy: hey! len %d for [%s] \n", n, src);
v4 = (char *)malloc(n + 1);
v5 = v4;
if ( v4 )
{
strncpy(v4, src, n);
v5[n - 1] = 0;
strcpy(dest, v5);
xFree(v5, "Xstrncpy buffer");
v6 = Xstrlen(src);
result = fprintf(stderr, "%d bytes into %d:%s\n", v6, n, src);
}
else
{
result = fprintf(stderr, "could not allocate %d byte buf in Xstrncpy!\n", n + 1);
*dest = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(char *dest, char *src, size_t n)
{
char *v4; // rax
const char *v5; // r12
unsigned int v6; // eax
int result; // eax

if ( (int)func2(src) < n )
return (unsigned int)strcpy(dest, src);
if ( n - 5 > 0x7FFB )
fprintf(stderr, "Xstrncpy: hey! len %d for [%s] \n", n, src);
v4 = (char *)malloc(n + 1);
v5 = v4;
if ( v4 )
{
strncpy(v4, src, n);
v5[n - 1] = 0;
strcpy(dest, v5);
xFree(v5, "Xstrncpy buffer");
v6 = func2(src);
result = fprintf(stderr, "%d bytes into %d:%s\n", v6, n, src);
}
else
{
result = fprintf(stderr, "could not allocate %d byte buf in Xstrncpy!\n", n + 1);
*dest = 0;
}
return result;
}

original_code:
int
Ystrncpy(char *s1, const char *s2, size_t len)
{
 if( Xstrlen(s2) >= len ) {
  char * p;
  if( len < 5 || len > 8192*4 ) {
   fprintf(stderr,"Xstrncpy: hey! len %d for [%s] \n", len, s2 );
  }
  p = (char *)malloc((size_t)(len+1));
  if( ! p ) {
   fprintf(stderr,"could not allocate %d byte buf in Xstrncpy!\n", len+1);
   *s1 = 0;
   return;
  }
  strncpy(p,s2,(size_t)len);
  *(p+len-1) = 0;
  strcpy(s1,p);
  xFree(p,"Xstrncpy buffer");
  fprintf(stderr,"%d bytes into %d:%s\n", Xstrlen(s2), len ,s2);
 } else
  strcpy(s1,s2);
}

canonical_original_code:
int Ystrncpy(char *s1, char *s2, unsigned long len)
{
 if( Xstrlen(s2) >= len ) {
  char *p;
  if( len < 5 || len > 8192*4 ) {
   fprintf(stderr,"Xstrncpy: hey! len %d for [%s] \n", len, s2 );
  }
  p = (char *)malloc((unsigned long)(len+1));
  if( ! p ) {
   fprintf(stderr,"could not allocate %d byte buf in Xstrncpy!\n", len+1);
   *s1 = 0;
   return;
  }
  strncpy(p,s2,(unsigned long)len);
  *(p+len-1) = 0;
  strcpy(s1,p);
  xFree(p,"Xstrncpy buffer");
  fprintf(stderr,"%d bytes into %d:%s\n", Xstrlen(s2), len ,s2);
 } else
  strcpy(s1,s2);
}


decompiled_code:
__int64 __fastcall client_get_socket(__int64 a1)
{
return *(_QWORD *)a1;
}

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

original_code:
t_socket *client_get_socket(t_client *client)
{
  return client->socket;
}

canonical_original_code:
struct s_socket *client_get_socket(struct s_client *client)
{
  return client->socket;
}

variable_types: struct s_client { struct s_socket *socket; struct s_player { int id; struct s_team { char name[-1]; struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } players; int available_slots; int free_slots; } *team; struct s_tile { struct s_list players; struct s_pos { int x; int y; } coord; struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; } ressources; } *tile; enum t_orientation { OR_NORTH=0, OR_EAST=1, OR_SOUTH=2, OR_WEST=3 } orientation; struct s_inventory inventory; int level; int life; } *player; struct s_list actions; _Bool busy; void (*exec)(struct s_client *, char *); } *

return_type: struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *

user_defined_types: struct s_client { struct s_socket *socket; struct s_player *player; struct s_list actions; _Bool busy; void (*exec)(struct s_client *, char *); }

user_defined_types: struct s_socket { int fd; struct sockaddr_in addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; }

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 s_player { int id; struct s_team *team; struct s_tile *tile; enum t_orientation orientation; struct s_inventory inventory; int level; int life; }

user_defined_types: struct s_team { char name[-1]; struct s_list players; int available_slots; int free_slots; }

user_defined_types: struct s_tile { struct s_list players; struct s_pos coord; struct s_inventory ressources; }

user_defined_types: struct s_list { unsigned int size; struct s_list_node *head; struct s_list_node *tail; }


decompiled_code:
__int64 __fastcall client_get_player(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
t_player *client_get_player(t_client *client)
{
  return client->player;
}

canonical_original_code:
struct s_player *client_get_player(struct s_client *client)
{
  return client->player;
}

variable_types: struct s_client { struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *socket; struct s_player *player; struct s_list actions; _Bool busy; void (*exec)(struct s_client *, char *); } *

return_type: struct s_player { int id; struct s_team { char name[-1]; struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } players; int available_slots; int free_slots; } *team; struct s_tile { struct s_list players; struct s_pos { int x; int y; } coord; struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; } ressources; } *tile; enum t_orientation { OR_NORTH=0, OR_EAST=1, OR_SOUTH=2, OR_WEST=3 } orientation; struct s_inventory inventory; int level; int life; } *


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

result = 0LL;
if ( !*(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = a2;
result = 1LL;
}
return result;
}

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

result = 0LL;
if ( !*(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = a2;
result = 1LL;
}
return result;
}

original_code:
_Bool client_set_player(t_client *client, t_player *player)
{
  if (client->player)
    return 0;
  client->player = player;
  return 1;
}

canonical_original_code:
_Bool client_set_player(struct s_client *client, struct s_player *player)
{
  if (client->player)
    return 0;
  client->player = player;
  return 1;
}

variable_types: struct s_client { struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *socket; struct s_player { int id; struct s_team { char name[-1]; struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } players; int available_slots; int free_slots; } *team; struct s_tile { struct s_list players; struct s_pos { int x; int y; } coord; struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; } ressources; } *tile; enum t_orientation { OR_NORTH=0, OR_EAST=1, OR_SOUTH=2, OR_WEST=3 } orientation; struct s_inventory inventory; int level; int life; } *player; struct s_list actions; _Bool busy; void (*exec)(struct s_client *, char *); } *

variable_types:
player
struct s_player *


decompiled_code:
__int64 __fastcall player_expulse_end_cb(__int64 a1, int a2)
{
_QWORD *v2; // rbx
_QWORD *v4; // rbp
__int64 v5; // rax
__int64 v6; // rax

if ( a2 )
return client_send(a1, "ko");
v2 = *(_QWORD **)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL) + 8LL);
while ( v2 )
{
v4 = v2;
v2 = (_QWORD *)v2[2];
v5 = *(_QWORD *)(a1 + 8);
if ( *v4 != v5 )
{
world_move_to(*v4, *(unsigned int *)(v5 + 24));
v6 = clm_retrieve_from_player(*v4);
if ( v6 )
client_send(v6, "deplacement: %i", 0LL);
}
}
return client_send(a1, "ok");
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
_QWORD *v2; // rbx
_QWORD *v4; // rbp
__int64 v5; // rax
__int64 v6; // rax

if ( a2 )
return client_send(a1, "ko");
v2 = *(_QWORD **)(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL) + 8LL);
while ( v2 )
{
v4 = v2;
v2 = (_QWORD *)v2[2];
v5 = *(_QWORD *)(a1 + 8);
if ( *v4 != v5 )
{
world_move_to(*v4, *(unsigned int *)(v5 + 24));
v6 = clm_retrieve_from_player(*v4);
if ( v6 )
client_send(v6, "deplacement: %i", 0LL);
}
}
return client_send(a1, "ok");
}

original_code:
void player_expulse_end_cb(t_client *client, int error)
{
  t_client *client_player;
  t_tile *tile;
  t_list_node *node;
  t_list_node *next;
  if (!error)
    {
      tile = client->player->tile;
      node = tile->players.head;
      while (node)
 {
   next = node->next;
   if (node->data != client->player)
     {
       world_move_to(node->data, client->player->orientation);
       client_player = clm_retrieve_from_player(node->data);
       if (client_player)
  {
    client_send(client_player, "deplacement: %i", 0);
  }
     }
   node = next;
 }
      client_send(client, "ok");
    }
  else
    client_send(client, "ko");
}

canonical_original_code:
void player_expulse_end_cb(struct s_client *client, int error)
{
  struct s_client *client_player;
  struct s_tile *tile;
  struct s_list_node *node;
  struct s_list_node *next;
  if (!error)
    {
      tile = client->player->tile;
      node = tile->players.head;
      while (node)
 {
   next = node->next;
   if (node->data != client->player)
     {
       world_move_to(node->data, client->player->orientation);
       client_player = clm_retrieve_from_player(node->data);
       if (client_player)
  {
    client_send(client_player, "deplacement: %i", 0);
  }
     }
   node = next;
 }
      client_send(client, "ok");
    }
  else
    client_send(client, "ko");
}

variable_types: error int

variable_types: struct s_tile { struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } players; struct s_pos { int x; int y; } coord; struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; } ressources; } *

user_defined_types: enum t_orientation { OR_NORTH=0, OR_EAST=1, OR_SOUTH=2, OR_WEST=3 }


decompiled_code:
__int64 __fastcall player_expulse_start_cb(__int64 a1)
{
return kernel_register_wakeup(7LL, player_expulse_end_cb, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return kernel_register_wakeup(7LL, player_expulse_end_cb, a1);
}

original_code:
void player_expulse_start_cb(t_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(7, (kn_wakeup_cb) player_expulse_end_cb, client);
}

canonical_original_code:
void player_expulse_start_cb(struct s_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(7, (void (*)(void *, int)) player_expulse_end_cb, client);
}

variable_types:
command
char *

user_defined_types: struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; }

user_defined_types: struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; }


decompiled_code:
void *__fastcall socket_init(void *ptr)
{
int v1; // eax
void *result; // rax

if ( !ptr )
return 0LL;
if ( !g_network )
return 0LL;
++*(_QWORD *)(g_network + 32LL);
v1 = socket(2, 1, 6);
*(_DWORD *)ptr = v1;
if ( v1 == -1 )
{
free(ptr);
result = 0LL;
}
else
{
*((_DWORD *)ptr + 5) = 16;
result = ptr;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(void *ptr)
{
int v1; // eax
void *result; // rax

if ( !ptr )
return 0LL;
if ( !g_network )
return 0LL;
++*(_QWORD *)(g_network + 32LL);
v1 = socket(2, 1, 6);
*(_DWORD *)ptr = v1;
if ( v1 == -1 )
{
free(ptr);
result = 0LL;
}
else
{
*((_DWORD *)ptr + 5) = 16;
result = ptr;
}
return result;
}

original_code:
t_socket *socket_init(t_socket *sock)
{
  if (!g_network || !sock)
    return (((void *)0));
  g_network->opened_socket++;
  if ((sock->fd = socket(2, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
    {
      free(sock);
      return (((void *)0));
    }
  sock->length = sizeof(sock->addr);
  return (sock);
}

canonical_original_code:
struct s_socket *socket_init(struct s_socket *sock)
{
  if (!g_network || !sock)
    return (((void *)0));
  g_network->opened_socket++;
  if ((sock->fd = socket(2, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
    {
      free(sock);
      return (((void *)0));
    }
  sock->length = sizeof(sock->addr);
  return (sock);
}

variable_types:
sock
struct s_socket *


decompiled_code:
void *socket_create()
{
void *v0; // rax
void *v1; // rbx

v0 = malloc(0x28uLL);
v1 = v0;
if ( v0 )
socket_init(v0);
return v1;
}

canonical_decompiled_code:
void *func1()
{
void *v0; // rax
void *v1; // rbx

v0 = malloc(0x28uLL);
v1 = v0;
if ( v0 )
func0(v0);
return v1;
}

original_code:
t_socket *socket_create()
{
  t_socket *socket;
  if (!(socket = (malloc(sizeof(t_socket)))))
    return (((void *)0));
  socket_init(socket);
  return (socket);
}

canonical_original_code:
struct s_socket *socket_create()
{
  struct s_socket *socket;
  if (!(socket = (malloc(sizeof(t_socket)))))
    return (((void *)0));
  socket_init(socket);
  return (socket);
}

variable_types: struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *


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

if ( a1 )
{
if ( g_network )
{
logger_verbose("[NETWORK] close socket %d", (unsigned int)*a1);
++*(_QWORD *)(g_network + 40LL);
result = close(*a1);
}
}
return result;
}

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

if ( a1 )
{
if ( g_network )
{
logger_verbose("[NETWORK] close socket %d", (unsigned int)*a1);
++*(_QWORD *)(g_network + 40LL);
result = close(*a1);
}
}
return result;
}

original_code:
void socket_close(t_socket *socket)
{
  if (!socket || !g_network)
    return;
  logger_verbose("[NETWORK] close socket %d", socket->fd);
  g_network->closed_socket++;
  if (socket)
    close(socket->fd);;
}

canonical_original_code:
void socket_close(struct s_socket *socket)
{
  if (!socket || !g_network)
    return;
  logger_verbose("[NETWORK] close socket %d", socket->fd);
  g_network->closed_socket++;
  if (socket)
    close(socket->fd);;
}


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

if ( a1 )
{
if ( g_network )
result = socket_close(*a1);
}
return result;
}

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

if ( a1 )
{
if ( g_network )
result = func2(*a1);
}
return result;
}

original_code:
void listener_close(t_listener *listener)
{
  if (!listener || !g_network)
    return;
  if (listener)
    socket_close(listener->socket);
}

canonical_original_code:
void listener_close(struct s_listener *listener)
{
  if (!listener || !g_network)
    return;
  if (listener)
    socket_close(listener->socket);
}

variable_types: struct s_listener { struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *socket; void (*create)(struct s_socket *); } *

user_defined_types: struct s_listener { struct s_socket *socket; void (*create)(struct s_socket *); }


decompiled_code:
void __fastcall free_socket(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func4(void *a1)
{
free(a1);
}

original_code:
void free_socket(t_socket *socket)
{
  free(socket);
}

canonical_original_code:
void free_socket(struct s_socket *socket)
{
  free(socket);
}


decompiled_code:
__int64 network_create()
{
__int64 result; // rax
_QWORD *v1; // rbx
__int64 v2; // rdx
_QWORD *v3; // rbx
__int64 v4; // rdx
_QWORD *v5; // rbx
__int64 v6; // rdx

result = 1LL;
if ( !g_network )
{
v1 = malloc(0x30uLL);
g_network = v1;
if ( v1 )
{
v2 = list_create();
*v1 = v2;
result = 0LL;
if ( v2 )
{
v3 = g_network;
v4 = list_create();
v3[2] = v4;
result = 0LL;
if ( v4 )
{
v5 = g_network;
v6 = list_create();
v5[1] = v6;
result = 0LL;
if ( v6 )
{
logger_debug("[NETWORK] allocation ...ok");
result = 1LL;
}
}
}
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
_QWORD *v1; // rbx
__int64 v2; // rdx
_QWORD *v3; // rbx
__int64 v4; // rdx
_QWORD *v5; // rbx
__int64 v6; // rdx

result = 1LL;
if ( !g_network )
{
v1 = malloc(0x30uLL);
g_network = v1;
if ( v1 )
{
v2 = list_create();
*v1 = v2;
result = 0LL;
if ( v2 )
{
v3 = g_network;
v4 = list_create();
v3[2] = v4;
result = 0LL;
if ( v4 )
{
v5 = g_network;
v6 = list_create();
v5[1] = v6;
result = 0LL;
if ( v6 )
{
logger_debug("[NETWORK] allocation ...ok");
result = 1LL;
}
}
}
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
_Bool network_create()
{
  if (g_network)
    return (1);
  if (!(g_network = malloc(sizeof(*g_network))))
    return (0);
  if (!(g_network->listened = list_create()))
    return (0);
  if (!(g_network->destroy = list_create()))
    return (0);
  if (!(g_network->read = list_create()))
    return (0);
  logger_debug("[NETWORK] allocation ...ok");
  return (1);
}


decompiled_code:
__int64 network_init()
{
unsigned int v0; // ebx
_DWORD *v2; // rax

logger_message("[NETWORK] starting network service");
v0 = 1;
if ( !g_network )
{
v0 = network_create();
if ( (_BYTE)v0 )
{
v2 = g_network;
*((_DWORD *)g_network + 6) = 0;
v2[7] = 0;
*((_QWORD *)v2 + 4) = 0LL;
*((_QWORD *)v2 + 5) = 0LL;
logger_message("[NETWORK] service started");
}
else
{
logger_error("[NETWORK] critical fail on allocation");
}
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx
_DWORD *v2; // rax

logger_message("[NETWORK] starting network service");
v0 = 1;
if ( !g_network )
{
v0 = func0();
if ( (_BYTE)v0 )
{
v2 = g_network;
*((_DWORD *)g_network + 6) = 0;
v2[7] = 0;
*((_QWORD *)v2 + 4) = 0LL;
*((_QWORD *)v2 + 5) = 0LL;
logger_message("[NETWORK] service started");
}
else
{
logger_error("[NETWORK] critical fail on allocation");
}
}
return v0;
}

original_code:
_Bool network_init()
{
  logger_message("[NETWORK] starting network service");
  if (g_network)
    return (1);
  if (network_create() == 0)
    {
      logger_error("[NETWORK] critical fail on allocation");
      return (0);
    }
  g_network->nfds = 0;
  g_network->usec_timeout = 0;
  g_network->opened_socket = 0;
  g_network->closed_socket = 0;
  logger_message("[NETWORK] service started");
  return (1);
}


decompiled_code:
__int64 network_destroy()
{
if ( g_network )
{
logger_message("[NETWORK] Network destroy");
list_foreach(*(_QWORD *)g_network, &listener_close);
list_foreach(*((_QWORD *)g_network + 1), &socket_close);
list_free(*(_QWORD *)g_network);
list_free(*((_QWORD *)g_network + 1));
logger_message(
"[NETWORK] destroyed : %lu socket not closed",
*((_QWORD *)g_network + 4) - *((_QWORD *)g_network + 5));
free(*(void **)g_network);
free(*((void **)g_network + 2));
free(*((void **)g_network + 1));
free(g_network);
g_network = 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( g_network )
{
logger_message("[NETWORK] Network destroy");
list_foreach(*(_QWORD *)g_network, &listener_close);
list_foreach(*((_QWORD *)g_network + 1), &socket_close);
list_free(*(_QWORD *)g_network);
list_free(*((_QWORD *)g_network + 1));
logger_message(
"[NETWORK] destroyed : %lu socket not closed",
*((_QWORD *)g_network + 4) - *((_QWORD *)g_network + 5));
free(*(void **)g_network);
free(*((void **)g_network + 2));
free(*((void **)g_network + 1));
free(g_network);
g_network = 0LL;
}
return 1LL;
}

original_code:
_Bool network_destroy()
{
  if (!g_network)
    return (1);
  logger_message("[NETWORK] Network destroy");
  list_foreach(g_network->listened, (feach) listener_close);
  list_foreach(g_network->read, (feach) socket_close);
  list_free(g_network->listened);
  list_free(g_network->read);
  logger_message("[NETWORK] destroyed : %lu socket not closed",
   g_network->opened_socket - g_network->closed_socket);
  free(g_network->listened);
  free(g_network->destroy);
  free(g_network->read);
  free(g_network);
  g_network = ((void *)0);
  return (1);
}

canonical_original_code:
_Bool network_destroy()
{
  if (!g_network)
    return (1);
  logger_message("[NETWORK] Network destroy");
  list_foreach(g_network->listened, (void (*)(void *)) listener_close);
  list_foreach(g_network->read, (void (*)(void *)) socket_close);
  list_free(g_network->listened);
  list_free(g_network->read);
  logger_message("[NETWORK] destroyed : %lu socket not closed",
   g_network->opened_socket - g_network->closed_socket);
  free(g_network->listened);
  free(g_network->destroy);
  free(g_network->read);
  free(g_network);
  g_network = ((void *)0);
  return (1);
}


decompiled_code:
__int64 __fastcall socket_connect(__int64 a1, const char *a2, int a3)
{
unsigned int v3; // ebx
in_addr_t v5; // eax

LOBYTE(v3) = a3 == 0 || a2 == 0LL || a1 == 0;
if ( (_BYTE)v3 )
return 0;
*(_WORD *)(a1 + 4) = 2;
*(_WORD *)(a1 + 6) = _bswap_16(a3);
v5 = inet_addr(a2);
*(_DWORD *)(a1 + 8) = v5;
if ( v5 != -1 )
LOBYTE(v3) = connect(*(_DWORD *)a1, (const struct sockaddr *)(a1 + 4), *(_DWORD *)(a1 + 20)) != -1;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, int a3)
{
unsigned int v3; // ebx
in_addr_t v5; // eax

LOBYTE(v3) = a3 == 0 || a2 == 0LL || a1 == 0;
if ( (_BYTE)v3 )
return 0;
*(_WORD *)(a1 + 4) = 2;
*(_WORD *)(a1 + 6) = func0(a3);
v5 = inet_addr(a2);
*(_DWORD *)(a1 + 8) = v5;
if ( v5 != -1 )
LOBYTE(v3) = connect(*(_DWORD *)a1, (const struct sockaddr *)(a1 + 4), *(_DWORD *)(a1 + 20)) != -1;
return v3;
}

original_code:
_Bool socket_connect(t_socket *socket, char *ip, int port)
{
  if (!socket || !ip || !port)
    return (0);
  socket->addr.sin_family = 2;
  socket->addr.sin_port = htons(port);
  if ((socket->addr.sin_addr.s_addr = inet_addr(ip)) == ((in_addr_t) 0xffffffff))
    return (0);
  if (connect(socket->fd,
              (SOCKADDR*)&socket->addr,
              socket->length) == SOCKET_ERROR)
    return (0);
  return (1);
}

canonical_original_code:
_Bool socket_connect(struct s_socket *socket, char *ip, int port)
{
  if (!socket || !ip || !port)
    return (0);
  socket->addr.sin_family = 2;
  socket->addr.sin_port = htons(port);
  if ((socket->addr.sin_addr.s_addr = inet_addr(ip)) == ((unsigned int) 0xffffffff))
    return (0);
  if (connect(socket->fd,
              (struct sockaddr *)&socket->addr,
              socket->length) == SOCKET_ERROR)
    return (0);
  return (1);
}

variable_types:
ip
char *

variable_types: port int

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


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

original_code:
void socket_set_reader(t_socket *socket, t_nt_read_cb read)
{
  socket->read = read;
}

canonical_original_code:
void socket_set_reader(struct s_socket *socket, void (*read)(struct s_socket *, void *))
{
  socket->read = read;
}

variable_types:
read
void (*)(struct s_socket *, void *)


decompiled_code:
__int64 __fastcall player_fork_end_cb(__int64 a1)
{
return client_send(a1, &unk_35);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return client_send(a1, &unk_35);
}

original_code:
void player_fork_end_cb(t_client *client, int error)
{
  (void)error;
  client_send(client, "ko");
}

canonical_original_code:
void player_fork_end_cb(struct s_client *client, int error)
{
  (void)error;
  client_send(client, "ko");
}


decompiled_code:
__int64 __fastcall player_fork_start_cb(__int64 a1)
{
return kernel_register_wakeup(42LL, player_fork_end_cb, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return kernel_register_wakeup(42LL, player_fork_end_cb, a1);
}

original_code:
void player_fork_start_cb(t_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(42, (kn_wakeup_cb) player_fork_end_cb, client);
}

canonical_original_code:
void player_fork_start_cb(struct s_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(42, (void (*)(void *, int)) player_fork_end_cb, client);
}


decompiled_code:
__int64 __fastcall player_drop_end_cb(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // eax
__int64 v4; // rcx

logger_debug("[GAME] ");
if ( a2 )
return client_send(a1, "ko");
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_DWORD *)(v2 + 28);
if ( v3 <= 0 )
return client_send(a1, "ko");
v4 = *(_QWORD *)(v2 + 16);
*(_DWORD *)(v2 + 28) = v3 - 1;
++*(_DWORD *)(v4 + 32);
return client_send(a1, "ok");
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // eax
__int64 v4; // rcx

logger_debug("[GAME] ");
if ( a2 )
return client_send(a1, "ko");
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_DWORD *)(v2 + 28);
if ( v3 <= 0 )
return client_send(a1, "ko");
v4 = *(_QWORD *)(v2 + 16);
*(_DWORD *)(v2 + 28) = v3 - 1;
++*(_DWORD *)(v4 + 32);
return client_send(a1, "ok");
}

original_code:
void player_drop_end_cb(t_client *client, int error)
{
  int *ressources;
  int *inventory;
  t_tile *tile;
  logger_debug("[GAME] ");
  if (!error)
    {
      tile = client->player->tile;
      ressources = (int *)&tile->ressources;
      inventory = (int *)&client->player->inventory;
      if (inventory[item] > 0)
 {
   inventory[item] -= 1;
   ressources[item] += 1;
   client_send(client, "ok");
 }
      else
 client_send(client, "ko");
    }
  else
    client_send(client, "ko");
}

canonical_original_code:
void player_drop_end_cb(struct s_client *client, int error)
{
  int *ressources;
  int *inventory;
  struct s_tile *tile;
  logger_debug("[GAME] ");
  if (!error)
    {
      tile = client->player->tile;
      ressources = (int *)&tile->ressources;
      inventory = (int *)&client->player->inventory;
      if (inventory[item] > 0)
 {
   inventory[item] -= 1;
   ressources[item] += 1;
   client_send(client, "ok");
 }
      else
 client_send(client, "ko");
    }
  else
    client_send(client, "ko");
}

variable_types:
ressources
int *

variable_types:
inventory
int *


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

if ( *(int *)(*(_QWORD *)(a1 + 8) + 28LL) <= 0 )
result = client_send(a1, "ko");
else
result = kernel_register_wakeup(7LL, player_drop_end_cb, a1);
return result;
}

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

if ( *(int *)(*(_QWORD *)(a1 + 8) + 28LL) <= 0 )
result = client_send(a1, "ko");
else
result = kernel_register_wakeup(7LL, player_drop_end_cb, a1);
return result;
}

original_code:
void player_drop_start_cb(t_client *client, char *command)
{
  int *inventory;
  (void) command;
  inventory = (int *)&client->player->inventory;
  if (inventory[item] > 0)
    kernel_register_wakeup(7, (kn_wakeup_cb) player_drop_end_cb, client);
  else
    client_send(client, "ko");
}

canonical_original_code:
void player_drop_start_cb(struct s_client *client, char *command)
{
  int *inventory;
  (void) command;
  inventory = (int *)&client->player->inventory;
  if (inventory[item] > 0)
    kernel_register_wakeup(7, (void (*)(void *, int)) player_drop_end_cb, client);
  else
    client_send(client, "ko");
}


decompiled_code:
int __fastcall clock_set_time_to_current(struct timeval *a1)
{
return gettimeofday(a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(struct timeval *a1)
{
return gettimeofday(a1, 0LL);
}

original_code:
void clock_set_time_to_current(struct timeval *time)
{
  gettimeofday(time , ((void *)0));
}

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


decompiled_code:
__int64 client_send(_QWORD *a1, const char *a2, ...)
{
int v2; // ebx
unsigned int *v3; // rax
__int64 v4; // rsi
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
v2 = vsnprintf(buffer, 0x400uLL, a2, arg);
v3 = (unsigned int *)a1[1];
v4 = 0xFFFFFFFFLL;
if ( v3 )
v4 = *v3;
logger_verbose("[CLIENT] Client %i send %i bytes '%s'", v4, (unsigned int)v2, buffer);
buffer[v2] = 10;
buffer[v2 + 1] = 0;
return socket_write(*a1, buffer);
}

canonical_decompiled_code:
__int64 func0(_QWORD *a1, const char *a2, ...)
{
int v2; // ebx
unsigned int *v3; // rax
__int64 v4; // rsi
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
v2 = vsnprintf(buffer, 0x400uLL, a2, arg);
v3 = (unsigned int *)a1[1];
v4 = 0xFFFFFFFFLL;
if ( v3 )
v4 = *v3;
logger_verbose("[CLIENT] Client %i send %i bytes '%s'", v4, (unsigned int)v2, buffer);
buffer[v2] = 10;
buffer[v2 + 1] = 0;
return socket_write(*a1, buffer);
}

original_code:
int client_send(t_client *client, char *mask, ...)
{
  va_list vl;
  int size;
  __builtin_va_start(vl,mask);
  size = vsnprintf(buffer, 1024, mask, vl);
  logger_verbose("[CLIENT] Client %i send %i bytes '%s'", ((client)->player ? (client)->player->id : -1), size, buffer);
  buffer[size++] = '\n';
  buffer[size] = '\0';
  size = socket_write(client->socket, buffer, size);
  __builtin_va_end(vl);
  return size;
}

canonical_original_code:
int client_send(struct s_client *client, char *mask, ...)
{
  __builtin_va_list vl;
  int size;
  __builtin_va_start(vl,mask);
  size = vsnprintf(buffer, 1024, mask, vl);
  logger_verbose("[CLIENT] Client %i send %i bytes '%s'", ((client)->player ? (client)->player->id : -1), size, buffer);
  buffer[size++] = '\n';
  buffer[size] = '\0';
  size = socket_write(client->socket, buffer, size);
  __builtin_va_end(vl);
  return size;
}

variable_types:
mask
char *

variable_types: size int


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

while ( 1 )
{
result = list_empty(*(_QWORD *)(g_network + 16LL));
if ( (_DWORD)result )
break;
v0 = list_get_begin(*(_QWORD *)(g_network + 16LL));
list_pop_begin(*(_QWORD *)(g_network + 16LL));
socket_close(v0);
network_del_socket(v0);
}
return result;
}

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

while ( 1 )
{
result = list_empty(*(_QWORD *)(g_network + 16LL));
if ( (_DWORD)result )
break;
v0 = list_get_begin(*(_QWORD *)(g_network + 16LL));
list_pop_begin(*(_QWORD *)(g_network + 16LL));
socket_close(v0);
network_del_socket(v0);
}
return result;
}

original_code:
static void flush_list()
{
  t_socket *socket;
  while ((!list_empty(g_network->destroy)))
    {
      socket = list_get_begin(g_network->destroy);
      list_pop_begin(g_network->destroy);
      socket_close(socket);
      network_del_socket(socket);
    }
}

canonical_original_code:
static void flush_list()
{
  struct s_socket *socket;
  while ((!list_empty(g_network->destroy)))
    {
      socket = list_get_begin(g_network->destroy);
      list_pop_begin(g_network->destroy);
      socket_close(socket);
      network_del_socket(socket);
    }
}


decompiled_code:
char __fastcall setfd_list(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64))
{
char result; // al
__int64 v5; // r13
unsigned int v6; // eax
int v7; // ebx
int v8; // eax
__int64 v9; // r15

result = a2 == 0;
if ( a2 != 0 && a1 != 0 && a3 && g_network )
{
v5 = list_iterator_begin();
if ( !(unsigned int)list_empty(a1) )
{
do
{
v9 = list_iterator_get(v5);
if ( (int)a3(v9) >= *(_DWORD *)(g_network + 24LL) )
*(_DWORD *)(g_network + 24LL) = a3(v9) + 1;
v6 = a3(v9);
logger_verbose("[NETWORK] adding fd %d", v6);
v7 = a3(v9);
v8 = a3(v9);
*(_QWORD *)(a2 + 8LL * (v8 / 64)) |= 1LL << (v7 % 64);
}
while ( (unsigned int)list_iterator_next(v5) != 1 );
}
result = list_iterator_destroy(v5);
}
return result;
}

canonical_decompiled_code:
char __fastcall func1(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64))
{
char result; // al
__int64 v5; // r13
unsigned int v6; // eax
int v7; // ebx
int v8; // eax
__int64 v9; // r15

result = a2 == 0;
if ( a2 != 0 && a1 != 0 && a3 && g_network )
{
v5 = list_iterator_begin();
if ( !(unsigned int)list_empty(a1) )
{
do
{
v9 = list_iterator_get(v5);
if ( (int)a3(v9) >= *(_DWORD *)(g_network + 24LL) )
*(_DWORD *)(g_network + 24LL) = a3(v9) + 1;
v6 = a3(v9);
logger_verbose("[NETWORK] adding fd %d", v6);
v7 = a3(v9);
v8 = a3(v9);
*(_QWORD *)(a2 + 8LL * (v8 / 64)) |= 1LL << (v7 % 64);
}
while ( (unsigned int)list_iterator_next(v5) != 1 );
}
result = list_iterator_destroy(v5);
}
return result;
}

original_code:
static void setfd_list(t_list *list,
       fd_set *set,
       SOCKET (*extract)(void *))
{
  t_list_iterator *it;
  int ret;
  void *tmp;
  if (!list || !set || !extract || !g_network)
    return;
  it = list_iterator_begin(list);
  ret = (list_empty(list))?1:0;
  while (ret != 1)
    {
      tmp = list_iterator_get(it);
      if (extract(tmp) >= g_network->nfds)
 g_network->nfds = extract(tmp) + 1;
      logger_verbose("[NETWORK] adding fd %d", extract(tmp));
      ((void) (((set)->__fds_bits)[((extract(tmp)) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((extract(tmp)) % (8 * (int) sizeof (__fd_mask)))))));
      ret = list_iterator_next(it);
    }
  list_iterator_destroy(it);
}

canonical_original_code:
static void setfd_list(struct s_list *list, struct fd_set *set, int (*extract)(void *))
{
  struct s_list_iterator *it;
  int ret;
  void *tmp;
  if (!list || !set || !extract || !g_network)
    return;
  it = list_iterator_begin(list);
  ret = (list_empty(list))?1:0;
  while (ret != 1)
    {
      tmp = list_iterator_get(it);
      if (extract(tmp) >= g_network->nfds)
 g_network->nfds = extract(tmp) + 1;
      logger_verbose("[NETWORK] adding fd %d", extract(tmp));
      ((void) (((set)->__fds_bits)[((extract(tmp)) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((extract(tmp)) % (8 * (int) sizeof (__fd_mask)))))));
      ret = list_iterator_next(it);
    }
  list_iterator_destroy(it);
}

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

variable_types:
extract
int (*)(void *)

variable_types: struct s_list_iterator { struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } *list; struct s_list_node *node; unsigned int pos; } *

variable_types: ret int

variable_types:
tmp
void *

user_defined_types: struct s_list_iterator { struct s_list *list; struct s_list_node *node; unsigned int pos; }


setfd

decompiled_code:
char __fastcall setfd(void *a1)
{
char result; // al

if ( a1 )
{
if ( g_network )
{
memset(a1, 0, 0x80uLL);
setfd_list(*(_QWORD *)(g_network + 8LL), (__int64)a1, (__int64 (__fastcall *)(__int64))&extract_from_socket);
result = setfd_list(*g_network, (__int64)a1, (__int64 (__fastcall *)(__int64))&extract_from_listener);
}
}
return result;
}

canonical_decompiled_code:
char __fastcall func2(void *a1)
{
char result; // al

if ( a1 )
{
if ( g_network )
{
memset(a1, 0, 0x80uLL);
func1(*(_QWORD *)(g_network + 8LL), (__int64)a1, (__int64 (__fastcall *)(__int64))&extract_from_socket);
result = func1(*g_network, (__int64)a1, (__int64 (__fastcall *)(__int64))&extract_from_listener);
}
}
return result;
}

original_code:
static void setfd(fd_set *set)
{
  if (!set || !g_network)
    return;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((set)->__fds_bits)[0]) : "memory"); } while (0);
  setfd_list(g_network->read, set, &extract_from_socket);
  setfd_list(g_network->listened, set, &extract_from_listener);
}

canonical_original_code:
static void setfd(struct fd_set *set)
{
  if (!set || !g_network)
    return;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((set)->__fds_bits)[0]) : "memory"); } while (0);
  setfd_list(g_network->read, set, &extract_from_socket);
  setfd_list(g_network->listened, set, &extract_from_listener);
}


decompiled_code:
void __fastcall find_speaker(__int64 a1, __int64 a2, int (__fastcall *a3)(__int64), void (__fastcall *a4)(__int64))
{
__int64 v5; // r14
int v6; // ebp
__int64 v7; // rbx
__int64 v8; // rbp
unsigned int v9; // eax

if ( a1 && a2 && a3 && a4 )
{
v5 = list_iterator_begin(a2);
if ( !(unsigned int)list_empty(a2) )
{
do
{
v7 = list_iterator_get(v5);
v8 = *(_QWORD *)(a1 + 8LL * (a3(v7) / 64));
if ( (v8 & (1LL << (a3(v7) % 64))) != 0 && v7 )
{
v9 = a3(v7);
logger_verbose("[NETWORK] message from fd : %d", v9);
v6 = list_iterator_next(v5);
a4(v7);
}
else
{
v6 = list_iterator_next(v5);
}
}
while ( v6 != 1 );
}
flush_list();
list_iterator_destroy(v5);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 a2, int (__fastcall *a3)(__int64), void (__fastcall *a4)(__int64))
{
__int64 v5; // r14
int v6; // ebp
__int64 v7; // rbx
__int64 v8; // rbp
unsigned int v9; // eax

if ( a1 && a2 && a3 && a4 )
{
v5 = list_iterator_begin(a2);
if ( !(unsigned int)list_empty(a2) )
{
do
{
v7 = list_iterator_get(v5);
v8 = *(_QWORD *)(a1 + 8LL * (a3(v7) / 64));
if ( (v8 & (1LL << (a3(v7) % 64))) != 0 && v7 )
{
v9 = a3(v7);
logger_verbose("[NETWORK] message from fd : %d", v9);
v6 = list_iterator_next(v5);
a4(v7);
}
else
{
v6 = list_iterator_next(v5);
}
}
while ( v6 != 1 );
}
func0();
list_iterator_destroy(v5);
}
}

original_code:
static void find_speaker(fd_set *set,
        t_list *list,
        SOCKET (*extract)(void *),
        void (*execute)(void *))
{
  t_list_iterator *it;
  int ret;
  void *tmp;
  if (!set || !list || !extract || !execute)
    return;
  it = list_iterator_begin(list);
  ret = (list_empty(list))?1:0;
  while (ret != 1)
    {
      tmp = list_iterator_get(it);
      if (((((set)->__fds_bits)[((extract(tmp)) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((extract(tmp)) % (8 * (int) sizeof (__fd_mask)))))) != 0) && tmp)
 {
   logger_verbose("[NETWORK] message from fd : %d", extract(tmp));
   ret = list_iterator_next(it);
   (*execute)(tmp);
 }
      else
 ret = list_iterator_next(it);
    }
  flush_list();
  list_iterator_destroy(it);
}

canonical_original_code:
static void find_speaker(struct fd_set *set, struct s_list *list, int (*extract)(void *), void (*execute)(void *))
{
  struct s_list_iterator *it;
  int ret;
  void *tmp;
  if (!set || !list || !extract || !execute)
    return;
  it = list_iterator_begin(list);
  ret = (list_empty(list))?1:0;
  while (ret != 1)
    {
      tmp = list_iterator_get(it);
      if (((((set)->__fds_bits)[((extract(tmp)) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((extract(tmp)) % (8 * (int) sizeof (__fd_mask)))))) != 0) && tmp)
 {
   logger_verbose("[NETWORK] message from fd : %d", extract(tmp));
   ret = list_iterator_next(it);
   (*execute)(tmp);
 }
      else
 ret = list_iterator_next(it);
    }
  flush_list();
  list_iterator_destroy(it);
}

variable_types: struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } *

variable_types:
execute
void (*)(void *)


decompiled_code:
__int64 __fastcall network_listen(struct timeval *timeout)
{
int v1; // ebx
fd_set v3; // [rsp+0h] [rbp-88h] BYREF

flush_list();
setfd(&v3);
if ( timeout )
logger_debug("[NETWORK] waiting for next action in %lu:%lu", timeout->tv_sec, timeout->tv_usec);
else
logger_debug("[NETWORK] waiting for extern action. nothing planified");
v1 = select(*(_DWORD *)(g_network + 24LL), &v3, 0LL, 0LL, timeout);
if ( v1 > 0 )
{
find_speaker(
(__int64)&v3,
*(_QWORD *)(g_network + 8LL),
(int (__fastcall *)(__int64))&extract_from_socket,
(void (__fastcall *)(__int64))&execute_from_socket);
find_speaker(
(__int64)&v3,
*g_network,
(int (__fastcall *)(__int64))&extract_from_listener,
(void (__fastcall *)(__int64))&execute_from_listener);
}
*(_DWORD *)(g_network + 24LL) = 0;
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(struct timeval *timeout)
{
int v1; // ebx
fd_set v3; // [rsp+0h] [rbp-88h] BYREF

func0();
func2(&v3);
if ( timeout )
logger_debug("[NETWORK] waiting for next action in %lu:%lu", timeout->tv_sec, timeout->tv_usec);
else
logger_debug("[NETWORK] waiting for extern action. nothing planified");
v1 = select(*(_DWORD *)(g_network + 24LL), &v3, 0LL, 0LL, timeout);
if ( v1 > 0 )
{
func3(
(__int64)&v3,
*(_QWORD *)(g_network + 8LL),
(int (__fastcall *)(__int64))&extract_from_socket,
(void (__fastcall *)(__int64))&execute_from_socket);
func3(
(__int64)&v3,
*g_network,
(int (__fastcall *)(__int64))&extract_from_listener,
(void (__fastcall *)(__int64))&execute_from_listener);
}
*(_DWORD *)(g_network + 24LL) = 0;
return (unsigned int)v1;
}

original_code:
int network_listen(struct timeval *timeout)
{
  fd_set set;
  int ret;
  flush_list();
  setfd(&set);
  if (timeout)
    logger_debug("[NETWORK] waiting for next action in %lu:%lu",
    timeout->tv_sec,
    timeout->tv_usec);
  else
    logger_debug("[NETWORK] waiting for extern action. nothing planified");
  if ((ret = (select(g_network->nfds, &set, ((void *)0), ((void *)0), timeout))) > 0)
    {
      find_speaker(&set,
     g_network->read,
     &extract_from_socket,
     &execute_from_socket);
      find_speaker(&set,
     g_network->listened,
     &extract_from_listener,
     &execute_from_listener);
    }
  g_network->nfds = 0;
  return (ret);
}

canonical_original_code:
int network_listen(struct timeval *timeout)
{
  struct fd_set set;
  int ret;
  flush_list();
  setfd(&set);
  if (timeout)
    logger_debug("[NETWORK] waiting for next action in %lu:%lu",
    timeout->tv_sec,
    timeout->tv_usec);
  else
    logger_debug("[NETWORK] waiting for extern action. nothing planified");
  if ((ret = (select(g_network->nfds, &set, ((void *)0), ((void *)0), timeout))) > 0)
    {
      find_speaker(&set,
     g_network->read,
     &extract_from_socket,
     &execute_from_socket);
      find_speaker(&set,
     g_network->listened,
     &extract_from_listener,
     &execute_from_listener);
    }
  g_network->nfds = 0;
  return (ret);
}

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

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


decompiled_code:
__int64 __fastcall parser_argument_setter_str(int a1, const char *a2, __int64 a3)
{
char *v4; // rax

*(_DWORD *)a3 = a1;
if ( !a2 )
return 0xFFFFFFFFLL;
v4 = strdup(a2);
if ( !v4 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a3 + 8) = v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, const char *a2, __int64 a3)
{
char *v4; // rax

*(_DWORD *)a3 = a1;
if ( !a2 )
return 0xFFFFFFFFLL;
v4 = strdup(a2);
if ( !v4 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a3 + 8) = v4;
return 0LL;
}

original_code:
int parser_argument_setter_str(int type, char *buffer,
       t_argument *argument)
{
  char *tmp;
  argument->type = type;
  if (!buffer)
    return (-1);
  if (!(tmp = strdup(buffer)))
    return (-1);
  argument->data = (void *)tmp;
  return (0);
}

canonical_original_code:
int parser_argument_setter_str(int type, char *buffer, struct s_argument *argument)
{
  char *tmp;
  argument->type = type;
  if (!buffer)
    return (-1);
  if (!(tmp = strdup(buffer)))
    return (-1);
  argument->data = (void *)tmp;
  return (0);
}

variable_types: type int

variable_types:
buffer
char *

variable_types: struct s_argument { int type; void *data; } *

variable_types:
tmp
char *


decompiled_code:
__int64 __fastcall parser_argument_setter_int(int a1, const char *a2, __int64 a3)
{
_DWORD *v4; // rax
__int64 result; // rax

*(_DWORD *)a3 = a1;
v4 = malloc(4uLL);
*(_QWORD *)(a3 + 8) = v4;
if ( v4 )
{
*v4 = atoi(a2);
result = -((unsigned __int8)parser_str_check_contain(a2, "0123456789") ^ 1);
}
else
{
perror("malloc");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, const char *a2, __int64 a3)
{
_DWORD *v4; // rax
__int64 result; // rax

*(_DWORD *)a3 = a1;
v4 = malloc(4uLL);
*(_QWORD *)(a3 + 8) = v4;
if ( v4 )
{
*v4 = atoi(a2);
result = -((unsigned __int8)parser_str_check_contain(a2, "0123456789") ^ 1);
}
else
{
perror("malloc");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int parser_argument_setter_int(int type, char *buffer,
                                   t_argument *argument)
{
  argument->type = type;
  if (!(argument->data = malloc(sizeof(int))))
    {
      perror("malloc");
      return (-1);
    }
  *(int *)argument->data = atoi(buffer);
  if (parser_str_check_contain(buffer, "0123456789") == 0)
    return (-1);
  return (0);
}

canonical_original_code:
int parser_argument_setter_int(int type, char *buffer, struct s_argument *argument)
{
  argument->type = type;
  if (!(argument->data = malloc(sizeof(int))))
    {
      perror("malloc");
      return (-1);
    }
  *(int *)argument->data = atoi(buffer);
  if (parser_str_check_contain(buffer, "0123456789") == 0)
    return (-1);
  return (0);
}

user_defined_types: struct s_argument { int type; void *data; }


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

v4 = (__int64 (__fastcall *)(_QWORD, __int64, __int64))parser_get_argument_setter_int();
if ( v4 )
result = v4(a1, a2, a3);
else
result = 0xFFFFFFFFLL;
return result;
}

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

v4 = (__int64 (__fastcall *)(_QWORD, __int64, __int64))parser_get_argument_setter_int();
if ( v4 )
result = v4(a1, a2, a3);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int parser_argument_setter(int type, char *buffer,
           t_argument *argument)
{
  t_f_arg_set argument_setter;
  if ((argument_setter = parser_get_argument_setter_int(type)))
    return (argument_setter(type, buffer, argument));
  return (-1);
}

canonical_original_code:
int parser_argument_setter(int type, char *buffer, struct s_argument *argument)
{
  int (*argument_setter)(int, char *, struct s_argument *);
  if ((argument_setter = parser_get_argument_setter_int(type)))
    return (argument_setter(type, buffer, argument));
  return (-1);
}

variable_types: int (*)(int, char *, struct s_argument { int type; void *data; } *)


decompiled_code:
__int64 __fastcall gui_execute(__int64 a1, const char *a2)
{
return logger_verbose("[GUI] Recieve command '%s' for gui", a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
return logger_verbose("[GUI] Recieve command '%s' for gui", a2);
}

original_code:
void gui_execute(t_gui *gui, char *command)
{
  (void) gui;
  (void) command;
  logger_verbose("[GUI] Recieve command '%s' for gui", command);
}

canonical_original_code:
void gui_execute(struct s_client *gui, char *command)
{
  (void) gui;
  (void) command;
  logger_verbose("[GUI] Recieve command '%s' for gui", command);
}

variable_types: struct s_client { struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *socket; struct s_player { int id; struct s_team { char name[-1]; struct s_list { unsigned int size; struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *head; struct s_list_node *tail; } players; int available_slots; int free_slots; } *team; struct s_tile { struct s_list players; struct s_pos { int x; int y; } coord; struct s_inventory { int food; int linemate; int deraumere; int sibur; int mendiane; int phiras; int thystame; } ressources; } *tile; enum t_orientation { OR_NORTH=0, OR_EAST=1, OR_SOUTH=2, OR_WEST=3 } orientation; struct s_inventory inventory; int level; int life; } *player; struct s_list actions; _Bool busy; void (*exec)(struct s_client *, char *); } *

variable_types:
command
char *


decompiled_code:
__int64 gui_init()
{
void *v0; // rax

v0 = malloc(0x18uLL);
g_gui_manager = v0;
if ( !v0 )
return 0LL;
list_init(v0);
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
void *v0; // rax

v0 = malloc(0x18uLL);
g_gui_manager = v0;
if ( !v0 )
return 0LL;
list_init(v0);
return 1LL;
}

original_code:
_Bool gui_init()
{
  if (!(g_gui_manager = malloc(sizeof(t_gui_manager))))
    return 0;
  list_init(&g_gui_manager->guis);
  return 1;
}


decompiled_code:
__int64 __fastcall gui_connect(__int64 a1)
{
logger_verbose("[GUI] New Client GUI connected");
*(_QWORD *)(a1 + 48) = gui_execute;
return list_add_end(g_gui_manager, a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
logger_verbose("[GUI] New Client GUI connected");
*(_QWORD *)(a1 + 48) = gui_execute;
return list_add_end(g_gui_manager, a1);
}

original_code:
void gui_connect(t_gui *gui)
{
  logger_verbose("[GUI] New Client GUI connected");
  gui->exec = &gui_execute;
  list_add_end(&g_gui_manager->guis, gui);
}

canonical_original_code:
void gui_connect(struct s_client *gui)
{
  logger_verbose("[GUI] New Client GUI connected");
  gui->exec = &gui_execute;
  list_add_end(&g_gui_manager->guis, gui);
}


decompiled_code:
void gui_destroy()
{
list_clear(g_gui_manager);
free(g_gui_manager);
g_gui_manager = 0LL;
}

canonical_decompiled_code:
void func3()
{
list_clear(g_gui_manager);
free(g_gui_manager);
g_gui_manager = 0LL;
}

original_code:
void gui_destroy()
{
  list_clear(&g_gui_manager->guis);
  free(g_gui_manager);
  g_gui_manager = ((void *)0);
}


decompiled_code:
void kernel_init()
{
char *v0; // rax

if ( !g_kernel )
{
v0 = (char *)malloc(0x20uLL);
g_kernel = v0;
if ( v0 )
{
list_init(v0 + 8);
*((_DWORD *)g_kernel + 1) = 0;
*(_BYTE *)g_kernel = 0;
kernel_signal();
logger_message("[KERNEL] Kernel init");
}
}
}

canonical_decompiled_code:
void func0()
{
char *v0; // rax

if ( !g_kernel )
{
v0 = (char *)malloc(0x20uLL);
g_kernel = v0;
if ( v0 )
{
list_init(v0 + 8);
*((_DWORD *)g_kernel + 1) = 0;
*(_BYTE *)g_kernel = 0;
kernel_signal();
logger_message("[KERNEL] Kernel init");
}
}
}

original_code:
void kernel_init()
{
  if (g_kernel != ((void *)0))
    return;
  if ((g_kernel = malloc(sizeof(t_kernel))) == ((void *)0))
    return;
  list_init(&g_kernel->callbacks);
  g_kernel->init = 0;
  g_kernel->run = 0;
  kernel_signal();
  logger_message("[KERNEL] Kernel init");
}


decompiled_code:
__int64 __fastcall kernel_init_with_argv(unsigned int a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // ebx
unsigned int v5; // er13
unsigned int v6; // ebx
unsigned int v7; // eax
unsigned int v8; // eax
int v9; // eax
__int64 v10; // rdx
_BYTE **v11; // r13
int v12; // ebp
int v13; // er12

kernel_init();
v2 = kernel_getopt_int(a1, a2, "-p", 3945LL);
v3 = kernel_client_init(v2);
if ( (_BYTE)v3 )
{
v5 = kernel_getopt_int(a1, a2, "-c", 10LL);
v6 = kernel_getopt_int(a1, a2, "-y", 10LL);
v7 = kernel_getopt_int(a1, a2, "-x", 10LL);
v3 = kernel_game_init(v7, v6, v5);
if ( (_BYTE)v3 )
{
v3 = kernel_gui_init();
if ( (_BYTE)v3 )
{
v8 = kernel_getopt_int(a1, a2, "-t", 100LL);
v3 = kernel_clock_init(v8);
if ( (_BYTE)v3 )
{
logger_verbose("[KERNEL] Search team definition");
v9 = kernel_retrieve_flag_index(a1, a2, "-n");
v10 = (unsigned int)(v9 + 1);
if ( v9 == -1 || (int)a1 <= (int)v10 )
goto LABEL_13;
v10 = (int)v10;
v11 = (_BYTE **)(a2 + 8LL * (int)v10);
v12 = a1 - 1 - v9;
v13 = 0;
while ( **v11 != 45 )
{
kernel_add_team();
++v13;
++v11;
if ( v13 == v12 )
{
v13 = v12;
break;
}
}
if ( v13 <= 0 )
{
LABEL_13:
logger_error("[KERNEL] No team has created", a2, v10);
v3 = 0;
}
}
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // ebx
unsigned int v5; // er13
unsigned int v6; // ebx
unsigned int v7; // eax
unsigned int v8; // eax
int v9; // eax
__int64 v10; // rdx
_BYTE **v11; // r13
int v12; // ebp
int v13; // er12

func0();
v2 = kernel_getopt_int(a1, a2, "-p", 3945LL);
v3 = kernel_client_init(v2);
if ( (_BYTE)v3 )
{
v5 = kernel_getopt_int(a1, a2, "-c", 10LL);
v6 = kernel_getopt_int(a1, a2, "-y", 10LL);
v7 = kernel_getopt_int(a1, a2, "-x", 10LL);
v3 = kernel_game_init(v7, v6, v5);
if ( (_BYTE)v3 )
{
v3 = kernel_gui_init();
if ( (_BYTE)v3 )
{
v8 = kernel_getopt_int(a1, a2, "-t", 100LL);
v3 = kernel_clock_init(v8);
if ( (_BYTE)v3 )
{
logger_verbose("[KERNEL] Search team definition");
v9 = kernel_retrieve_flag_index(a1, a2, "-n");
v10 = (unsigned int)(v9 + 1);
if ( v9 == -1 || (int)a1 <= (int)v10 )
goto LABEL_13;
v10 = (int)v10;
v11 = (_BYTE **)(a2 + 8LL * (int)v10);
v12 = a1 - 1 - v9;
v13 = 0;
while ( **v11 != 45 )
{
kernel_add_team();
++v13;
++v11;
if ( v13 == v12 )
{
v13 = v12;
break;
}
}
if ( v13 <= 0 )
{
LABEL_13:
logger_error("[KERNEL] No team has created", a2, v10);
v3 = 0;
}
}
}
}
}
return v3;
}

original_code:
_Bool kernel_init_with_argv(int argc, char **argv)
{
  int index;
  int team;
  kernel_init();
  if (kernel_client_init(kernel_getopt_int(argc, argv, "-p", 3945)) &&
      kernel_game_init(kernel_getopt_int(argc, argv, "-x", 10),
         kernel_getopt_int(argc, argv, "-y", 10),
         kernel_getopt_int(argc, argv, "-c", 10)) &&
      kernel_gui_init() &&
      kernel_clock_init(kernel_getopt_int(argc, argv, "-t", 100)))
    {
      logger_verbose("[KERNEL] Search team definition");
      index = kernel_retrieve_flag_index(argc, argv, "-n") + 1;
      if (index != 0)
 {
   team = 0;
   while (index < argc && (!((argv[index])[0] == '-')))
     {
       kernel_add_team(argv[index++]);
       team++;
     }
   if (team > 0)
     return 1;
 }
      logger_error("[KERNEL] No team has created");
    }
  return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: index int

variable_types: team int


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

result = logger_message("[KERNEL] Kernel destroy");
if ( g_kernel )
{
list_free((char *)g_kernel + 8);
gui_destroy();
client_manager_destroy();
game_destroy();
clock_destroy();
free(g_kernel);
g_kernel = 0LL;
result = logger_destroy();
}
return result;
}

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

result = logger_message("[KERNEL] Kernel destroy");
if ( g_kernel )
{
list_free((char *)g_kernel + 8);
gui_destroy();
client_manager_destroy();
game_destroy();
clock_destroy();
free(g_kernel);
g_kernel = 0LL;
result = logger_destroy();
}
return result;
}

original_code:
void kernel_destroy()
{
  logger_message("[KERNEL] Kernel destroy");
  if (!g_kernel)
    return;
  list_free(&g_kernel->callbacks);
  gui_destroy();
  client_manager_destroy();
  game_destroy();
  clock_destroy();
  free(g_kernel);
  g_kernel = ((void *)0);
  logger_destroy();
}


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

logger_message("[KERNEL] Kernel start gui");
result = gui_init();
if ( (_BYTE)result )
*(_DWORD *)(g_kernel + 4LL) |= 4u;
return result;
}

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

logger_message("[KERNEL] Kernel start gui");
result = gui_init();
if ( (_BYTE)result )
*(_DWORD *)(g_kernel + 4LL) |= 4u;
return result;
}

original_code:
_Bool kernel_gui_init()
{
  logger_message("[KERNEL] Kernel start gui");
  if (gui_init())
    {
      g_kernel->init = g_kernel->init | 0x4;
      return 1;
    }
  return 0;
}


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

v1 = 0;
if ( g_kernel )
v1 = (*(_DWORD *)(g_kernel + 4LL) & a1) != 0;
return v1;
}

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

v1 = 0;
if ( g_kernel )
v1 = (*(_DWORD *)(g_kernel + 4LL) & a1) != 0;
return v1;
}

original_code:
_Bool kernel_is_init(int service)
{
  return(g_kernel && g_kernel->init & service);
}

variable_types: service int


decompiled_code:
__int64 __fastcall kernel_client_init(unsigned int a1)
{
__int64 result; // rax

if ( !kernel_is_init(1)
&& (logger_message("[KERNEL] Kernel client init on port : %i", a1), result = client_manager_init(a1), (_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 1u;
}
else
{
logger_error("[KERNEL] Kernel client error!!");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 result; // rax

if ( !func1(1)
&& (logger_message("[KERNEL] Kernel client init on port : %i", a1), result = client_manager_init(a1), (_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 1u;
}
else
{
logger_error("[KERNEL] Kernel client error!!");
result = 0LL;
}
return result;
}

original_code:
_Bool kernel_client_init(int port)
{
  if (!kernel_is_init(0x1))
    {
      logger_message("[KERNEL] Kernel client init on port : %i", port);
      if (client_manager_init(port))
 {
   g_kernel->init = g_kernel->init | 0x1;
   return 1;
 }
    }
  logger_error("[KERNEL] Kernel client error!!");
  return 0;
}

variable_types: port int


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

if ( !kernel_is_init(2)
&& (logger_message("[Kernel] Kernel game init with %i:%i and %i connection(s)", a1, a2, a3),
result = game_init(a1, a2, a3),
(_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 2u;
}
else
{
logger_error("[KERNEL] Kernel game error!!");
result = 0LL;
}
return result;
}

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

if ( !func1(2)
&& (logger_message("[Kernel] Kernel game init with %i:%i and %i connection(s)", a1, a2, a3),
result = game_init(a1, a2, a3),
(_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 2u;
}
else
{
logger_error("[KERNEL] Kernel game error!!");
result = 0LL;
}
return result;
}

original_code:
_Bool kernel_game_init(int x, int y, int nb_per_team)
{
  if (!kernel_is_init(0x2))
    {
      logger_message("[Kernel] Kernel game init with %i:%i and %i connection(s)", x, y, nb_per_team);
      if (game_init(x, y, nb_per_team))
 {
   g_kernel->init = g_kernel->init | 0x2;
   return 1;
 }
    }
  logger_error("[KERNEL] Kernel game error!!");
  return(0);
}

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall kernel_clock_init(unsigned int a1)
{
__int64 result; // rax

if ( !kernel_is_init(8)
&& (logger_message("[KERNEL] Kernel clock init with frequency at %i", a1), (unsigned __int8)clock_init())
&& (result = clock_set_frequency(a1), (_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 8u;
}
else
{
logger_error("[KERNEL] Kernel clock error!!");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 result; // rax

if ( !func1(8)
&& (logger_message("[KERNEL] Kernel clock init with frequency at %i", a1), (unsigned __int8)clock_init())
&& (result = clock_set_frequency(a1), (_BYTE)result) )
{
*(_DWORD *)(g_kernel + 4LL) |= 8u;
}
else
{
logger_error("[KERNEL] Kernel clock error!!");
result = 0LL;
}
return result;
}

original_code:
_Bool kernel_clock_init(int frequence)
{
  if (!kernel_is_init(0x8))
    {
      logger_message("[KERNEL] Kernel clock init with frequency at %i", frequence);
      if (clock_init())
 {
   if (clock_set_frequency(frequence))
     {
       g_kernel->init = g_kernel->init | 0x8;
       return 1;
     }
 }
    }
  logger_error("[KERNEL] Kernel clock error!!");
  return 0;
}

variable_types: frequence int


decompiled_code:
__int64 __fastcall client_manager_init(unsigned int a1)
{
void *v1; // rax
unsigned int v2; // ebx

logger_message("[CLIENT] Manager starting ...");
v1 = malloc(0x58uLL);
g_client_manager = v1;
if ( !v1 )
return 0;
list_init(v1);
list_init((char *)g_client_manager + 24);
v2 = network_init();
if ( (_BYTE)v2 )
{
v2 = 0;
if ( socket_init((char *)g_client_manager + 48) )
{
v2 = network_listen_to((char *)g_client_manager + 48, a1, &client_connect);
if ( (_BYTE)v2 )
logger_message("[CLIENT] Manager started");
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
void *v1; // rax
unsigned int v2; // ebx

logger_message("[CLIENT] Manager starting ...");
v1 = malloc(0x58uLL);
g_client_manager = v1;
if ( !v1 )
return 0;
list_init(v1);
list_init((char *)g_client_manager + 24);
v2 = network_init();
if ( (_BYTE)v2 )
{
v2 = 0;
if ( socket_init((char *)g_client_manager + 48) )
{
v2 = network_listen_to((char *)g_client_manager + 48, a1, &client_connect);
if ( (_BYTE)v2 )
logger_message("[CLIENT] Manager started");
}
}
return v2;
}

original_code:
_Bool client_manager_init(int port)
{
  logger_message("[CLIENT] Manager starting ...");
  if (!(g_client_manager = malloc(sizeof(t_client_manager))))
    return 0;
  list_init(&g_client_manager->clients);
  list_init(&g_client_manager->commands);
  if (!network_init()
      || !socket_init(&g_client_manager->listen)
      || !network_listen_to(&g_client_manager->listen, port, &client_connect))
    return 0;
  logger_message("[CLIENT] Manager started");
  return 1;
}


decompiled_code:
void client_manager_destroy()
{
if ( g_client_manager )
{
logger_message("[CLIENT] Manager destroy");
list_foreach(g_client_manager, &client_destroy);
list_clear(g_client_manager);
list_free((char *)g_client_manager + 24);
network_destroy();
free(g_client_manager);
g_client_manager = 0LL;
}
}

canonical_decompiled_code:
void func1()
{
if ( g_client_manager )
{
logger_message("[CLIENT] Manager destroy");
list_foreach(g_client_manager, &client_destroy);
list_clear(g_client_manager);
list_free((char *)g_client_manager + 24);
network_destroy();
free(g_client_manager);
g_client_manager = 0LL;
}
}

original_code:
void client_manager_destroy()
{
  if (!g_client_manager)
    return;
  logger_message("[CLIENT] Manager destroy");
  list_foreach(&g_client_manager->clients, (feach) &client_destroy);
  list_clear(&g_client_manager->clients);
  list_free(&g_client_manager->commands);
  network_destroy();
  free(g_client_manager);
  g_client_manager = ((void *)0);
}


decompiled_code:
__int64 client_manager_count()
{
return list_size(g_client_manager);
}

canonical_decompiled_code:
__int64 func2()
{
return list_size(g_client_manager);
}

original_code:
int client_manager_count()
{
  return list_size(&g_client_manager->clients);
}


decompiled_code:
__int64 __fastcall player_right_end_cb(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // ecx
int v4; // eax

if ( a2 )
return client_send(a1, &unk_65);
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_DWORD *)(v2 + 24);
v4 = v3 + 1;
if ( v3 == 3 )
v4 = 0;
*(_DWORD *)(v2 + 24) = v4;
return client_send(a1, &unk_62);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // ecx
int v4; // eax

if ( a2 )
return client_send(a1, &unk_65);
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_DWORD *)(v2 + 24);
v4 = v3 + 1;
if ( v3 == 3 )
v4 = 0;
*(_DWORD *)(v2 + 24) = v4;
return client_send(a1, &unk_62);
}

original_code:
void player_right_end_cb(t_client *client, int error)
{
  if (!error)
    {
      if (client->player->orientation != OR_WEST)
 ++client->player->orientation;
      else
 client->player->orientation = OR_NORTH;
      client_send(client, "ok");
    }
  else
    client_send(client, "ko");
}

canonical_original_code:
void player_right_end_cb(struct s_client *client, int error)
{
  if (!error)
    {
      if (client->player->orientation != OR_WEST)
 ++client->player->orientation;
      else
 client->player->orientation = OR_NORTH;
      client_send(client, "ok");
    }
  else
    client_send(client, "ko");
}

variable_types: error int


decompiled_code:
__int64 __fastcall player_right_start_cb(__int64 a1)
{
return kernel_register_wakeup(7LL, player_right_end_cb, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return kernel_register_wakeup(7LL, player_right_end_cb, a1);
}

original_code:
void player_right_start_cb(t_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(7, (kn_wakeup_cb) player_right_end_cb, client);
}

canonical_original_code:
void player_right_start_cb(struct s_client *client, char *command)
{
  (void)command;
  kernel_register_wakeup(7, (void (*)(void *, int)) player_right_end_cb, client);
}


decompiled_code:
__int64 __fastcall kernel_wakeup_dump(__int64 a1)
{
const char *v1; // r8
unsigned int *v2; // rax
__int64 v3; // rcx

v1 = (const char *)debug_get_callback_name(*(_QWORD *)(a1 + 24));
v2 = *(unsigned int **)(*(_QWORD *)(a1 + 32) + 8LL);
v3 = 0xFFFFFFFFLL;
if ( v2 )
v3 = *v2;
return logger_verbose(
"[DUMP] Callback at %u (%i) from client %i call '%s'",
*(_QWORD *)a1,
*(unsigned int *)(a1 + 16),
v3,
v1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
const char *v1; // r8
unsigned int *v2; // rax
__int64 v3; // rcx

v1 = (const char *)debug_get_callback_name(*(_QWORD *)(a1 + 24));
v2 = *(unsigned int **)(*(_QWORD *)(a1 + 32) + 8LL);
v3 = 0xFFFFFFFFLL;
if ( v2 )
v3 = *v2;
return logger_verbose(
"[DUMP] Callback at %u (%i) from client %i call '%s'",
*(_QWORD *)a1,
*(unsigned int *)(a1 + 16),
v3,
v1);
}

original_code:
void kernel_wakeup_dump(t_kernel_callback *callback)
{
  logger_verbose("[DUMP] Callback at %u (%i) from client %i call '%s'",
   callback->begin.tv_sec, callback->time,
   (((t_client *) callback->param)->player ? ((t_client *) callback->param)->player->id : -1),
   debug_get_callback_name(callback->callback));
}

canonical_original_code:
void kernel_wakeup_dump(struct s_kernel_callback *callback)
{
  logger_verbose("[DUMP] Callback at %u (%i) from client %i call '%s'",
   callback->begin.tv_sec, callback->time,
   (((struct s_client *) callback->param)->player ? ((struct s_client *) callback->param)->player->id : -1),
   debug_get_callback_name(callback->callback));
}

variable_types: struct s_kernel_callback { struct timeval { long tv_sec; long tv_usec; } begin; unsigned int time; void (*callback)(void *, int); void *param; } *

user_defined_types: struct s_kernel_callback { struct timeval begin; unsigned int time; void (*callback)(void *, int); void *param; }


decompiled_code:
__int64 __fastcall kernel_wakeup_insert(const __m128i *a1, const __m128i *a2)
{
__m128i v3; // [rsp+0h] [rbp-28h] BYREF
__m128i v4; // [rsp+10h] [rbp-18h] BYREF

v4 = _mm_loadu_si128(a1);
clock_move_date(&v4, a1[1].m128i_u32[0]);
v3 = _mm_loadu_si128(a2);
clock_move_date(&v3, a2[1].m128i_u32[0]);
return (unsigned int)-clock_compare(&v4, &v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(const __m128i *a1, const __m128i *a2)
{
__m128i v3; // [rsp+0h] [rbp-28h] BYREF
__m128i v4; // [rsp+10h] [rbp-18h] BYREF

v4 = _mm_loadu_si128(a1);
clock_move_date(&v4, a1[1].m128i_u32[0]);
v3 = _mm_loadu_si128(a2);
clock_move_date(&v3, a2[1].m128i_u32[0]);
return (unsigned int)-clock_compare(&v4, &v3);
}

original_code:
int kernel_wakeup_insert(t_kernel_callback *first, t_kernel_callback *second)
{
  struct timeval first_end;
  struct timeval second_end;
  first_end = first->begin;
  clock_move_date(&first_end, first->time);
  second_end = second->begin;
  clock_move_date(&second_end, second->time);
  return clock_compare(&first_end, &second_end) * -1;
}

canonical_original_code:
int kernel_wakeup_insert(struct s_kernel_callback *first, struct s_kernel_callback *second)
{
  struct timeval first_end;
  struct timeval second_end;
  first_end = first->begin;
  clock_move_date(&first_end, first->time);
  second_end = second->begin;
  clock_move_date(&second_end, second->time);
  return clock_compare(&first_end, &second_end) * -1;
}

variable_types:
second
struct s_kernel_callback *

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


decompiled_code:
__int64 __fastcall kernel_register_wakeup(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v4; // ebx
const char *v5; // rax
_DWORD *v6; // rax
_DWORD *v7; // rbx
unsigned int v8; // eax

v4 = list_size(g_kernel + 8LL);
v5 = (const char *)debug_get_callback_name(a2);
logger_debug("[KERNEL] Register wakeup '%s' in %i time, %i in standby", v5, a1, v4);
v6 = malloc(0x28uLL);
if ( !v6 )
return 0LL;
v7 = v6;
clock_set_time_to_current(v6);
v7[4] = a1;
*((_QWORD *)v7 + 3) = a2;
*((_QWORD *)v7 + 4) = a3;
list_add_to(g_kernel + 8LL, kernel_wakeup_insert, v7);
v8 = list_size(g_kernel + 8LL);
logger_verbose("[DUMP] %i callback in standby", v8);
list_foreach(g_kernel + 8LL, kernel_wakeup_dump);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v4; // ebx
const char *v5; // rax
_DWORD *v6; // rax
_DWORD *v7; // rbx
unsigned int v8; // eax

v4 = list_size(g_kernel + 8LL);
v5 = (const char *)debug_get_callback_name(a2);
logger_debug("[KERNEL] Register wakeup '%s' in %i time, %i in standby", v5, a1, v4);
v6 = malloc(0x28uLL);
if ( !v6 )
return 0LL;
v7 = v6;
clock_set_time_to_current(v6);
v7[4] = a1;
*((_QWORD *)v7 + 3) = a2;
*((_QWORD *)v7 + 4) = a3;
list_add_to(g_kernel + 8LL, kernel_wakeup_insert, v7);
v8 = list_size(g_kernel + 8LL);
logger_verbose("[DUMP] %i callback in standby", v8);
list_foreach(g_kernel + 8LL, kernel_wakeup_dump);
return 1LL;
}

original_code:
_Bool kernel_register_wakeup(unsigned int time, kn_wakeup_cb callback, void* param)
{
  t_kernel_callback *wakeup;
  logger_debug("[KERNEL] Register wakeup '%s' in %i time, %i in standby", debug_get_callback_name(callback), time, list_size(&g_kernel->callbacks));
  if (!(wakeup = malloc(sizeof(t_kernel_callback))))
    return 0;
  clock_set_time_to_current(&wakeup->begin);
  wakeup->time = time;
  wakeup->callback = callback;
  wakeup->param = param;
  list_add_to(&g_kernel->callbacks, (fcmp) &kernel_wakeup_insert, wakeup);
  logger_verbose("[DUMP] %i callback in standby", list_size(&g_kernel->callbacks));
  list_foreach(&g_kernel->callbacks, (feach) &kernel_wakeup_dump);
  return 1;
}

canonical_original_code:
_Bool kernel_register_wakeup(unsigned int time, void (*callback)(void *, int), void *param)
{
  struct s_kernel_callback *wakeup;
  logger_debug("[KERNEL] Register wakeup '%s' in %i time, %i in standby", debug_get_callback_name(callback), time, list_size(&g_kernel->callbacks));
  if (!(wakeup = malloc(sizeof(t_kernel_callback))))
    return 0;
  clock_set_time_to_current(&wakeup->begin);
  wakeup->time = time;
  wakeup->callback = callback;
  wakeup->param = param;
  list_add_to(&g_kernel->callbacks, (fcmp) &kernel_wakeup_insert, wakeup);
  logger_verbose("[DUMP] %i callback in standby", list_size(&g_kernel->callbacks));
  list_foreach(&g_kernel->callbacks, (feach) &kernel_wakeup_dump);
  return 1;
}

variable_types:
time
unsigned int

variable_types:
callback
void (*)(void *, int)

variable_types:
param
void *


decompiled_code:
__int64 __fastcall kernel_unregister_wakeup(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
void **v3; // rbp

result = g_kernel;
v2 = *(_QWORD *)(g_kernel + 16LL);
while ( v2 )
{
v3 = (void **)v2;
v2 = *(_QWORD *)(v2 + 16);
if ( *((_QWORD *)*v3 + 4) == a1 )
{
free(*v3);
result = list_pop_node(g_kernel + 8LL, v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
void **v3; // rbp

result = g_kernel;
v2 = *(_QWORD *)(g_kernel + 16LL);
while ( v2 )
{
v3 = (void **)v2;
v2 = *(_QWORD *)(v2 + 16);
if ( *((_QWORD *)*v3 + 4) == a1 )
{
free(*v3);
result = list_pop_node(g_kernel + 8LL, v3);
}
}
return result;
}

original_code:
void kernel_unregister_wakeup(void *param)
{
  t_list_node *current;
  t_list_node *next;
  current = g_kernel->callbacks.head;
  while (current)
    {
      next = current->next;
      if (((t_kernel_callback *) current->data)->param == param)
 {
   free(current->data);
   _list_pop_node(&g_kernel->callbacks, current);
 }
      current = next;
    }
}

canonical_original_code:
void kernel_unregister_wakeup(void *param)
{
  struct s_list_node *current;
  struct s_list_node *next;
  current = g_kernel->callbacks.head;
  while (current)
    {
      next = current->next;
      if (((struct s_kernel_callback *) current->data)->param == param)
 {
   free(current->data);
   _list_pop_node(&g_kernel->callbacks, current);
 }
      current = next;
    }
}

variable_types: struct s_list_node { void *data; struct s_list_node *prev; struct s_list_node *next; } *


decompiled_code:
__int64 kernel_wakeup()
{
__m128i *v0; // rbx
__int64 v1; // rax
__int64 v2; // r12
unsigned int v3; // ebp
const char *v4; // rax
__int128 v6[2]; // [rsp+0h] [rbp-28h] BYREF

v0 = (__m128i *)list_get_begin(g_kernel + 8LL);
v1 = clock_get_time();
if ( !v0 )
return 0;
v2 = v1;
v3 = 0;
do
{
v6[0] = (__int128)_mm_loadu_si128(v0);
clock_move_date(v6, v0[1].m128i_u32[0]);
if ( (int)clock_compare(v2, v6) < 0 )
break;
list_pop_begin(g_kernel + 8LL);
v4 = (const char *)debug_get_callback_name(v0[1].m128i_i64[1]);
logger_debug("[KERNEL] Wakeup call '%s' callback", v4);
((void (__fastcall *)(__int64, _QWORD))v0[1].m128i_i64[1])(v0[2].m128i_i64[0], 0LL);
free(v0);
++v3;
v0 = (__m128i *)list_get_begin(g_kernel + 8LL);
}
while ( v0 );
return v3;
}

canonical_decompiled_code:
__int64 func4()
{
__m128i *v0; // rbx
__int64 v1; // rax
__int64 v2; // r12
unsigned int v3; // ebp
const char *v4; // rax
__int128 v6[2]; // [rsp+0h] [rbp-28h] BYREF

v0 = (__m128i *)list_get_begin(g_kernel + 8LL);
v1 = clock_get_time();
if ( !v0 )
return 0;
v2 = v1;
v3 = 0;
do
{
v6[0] = (__int128)_mm_loadu_si128(v0);
clock_move_date(v6, v0[1].m128i_u32[0]);
if ( (int)clock_compare(v2, v6) < 0 )
break;
list_pop_begin(g_kernel + 8LL);
v4 = (const char *)debug_get_callback_name(v0[1].m128i_i64[1]);
logger_debug("[KERNEL] Wakeup call '%s' callback", v4);
((void (__fastcall *)(__int64, _QWORD))v0[1].m128i_i64[1])(v0[2].m128i_i64[0], 0LL);
free(v0);
++v3;
v0 = (__m128i *)list_get_begin(g_kernel + 8LL);
}
while ( v0 );
return v3;
}

original_code:
int kernel_wakeup()
{
  t_kernel_callback *wakeup;
  struct timeval end;
  struct timeval *time;
  int count;
  count = 0;
  wakeup = list_get_begin(&g_kernel->callbacks);
  time = clock_get_time();
  while (wakeup)
    {
      end = wakeup->begin;
      clock_move_date(&end, wakeup->time);
      if (clock_compare(time, &end) >= 0)
 {
   list_pop_begin(&g_kernel->callbacks);
   logger_debug("[KERNEL] Wakeup call '%s' callback", debug_get_callback_name(wakeup->callback));
   (*wakeup->callback)(wakeup->param, 0);
   free(wakeup);
   count++;
 }
      else
 return count;
      wakeup = list_get_begin(&g_kernel->callbacks);
    }
  return count;
}

canonical_original_code:
int kernel_wakeup()
{
  struct s_kernel_callback *wakeup;
  struct timeval end;
  struct timeval *time;
  int count;
  count = 0;
  wakeup = list_get_begin(&g_kernel->callbacks);
  time = clock_get_time();
  while (wakeup)
    {
      end = wakeup->begin;
      clock_move_date(&end, wakeup->time);
      if (clock_compare(time, &end) >= 0)
 {
   list_pop_begin(&g_kernel->callbacks);
   logger_debug("[KERNEL] Wakeup call '%s' callback", debug_get_callback_name(wakeup->callback));
   (*wakeup->callback)(wakeup->param, 0);
   free(wakeup);
   count++;
 }
      else
 return count;
      wakeup = list_get_begin(&g_kernel->callbacks);
    }
  return count;
}

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

variable_types: count int


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

if ( a3 <= 0 || a2 == 0LL || !a1 )
result = 0xFFFFFFFFLL;
else
result = recv(*a1, a2, a3, 0);
return result;
}

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

if ( a3 <= 0 || a2 == 0LL || !a1 )
result = 0xFFFFFFFFLL;
else
result = recv(*a1, a2, a3, 0);
return result;
}

original_code:
int socket_read(t_socket *socket, void *buffer, int size)
{
  if (!socket || !buffer || size < 1)
    return (-1);
  return (recv(socket->fd, buffer, size, 0));
}

canonical_original_code:
int socket_read(struct s_socket *socket, void *buffer, int size)
{
  if (!socket || !buffer || size < 1)
    return (-1);
  return (recv(socket->fd, buffer, size, 0));
}

variable_types: struct s_socket { int fd; 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]; } addr; unsigned int length; void (*read)(struct s_socket *, void *); void *param; } *

variable_types:
buffer
void *

variable_types: size int


decompiled_code:
ssize_t __fastcall socket_write(int *a1, const void *a2, int a3)
{
ssize_t result; // rax

if ( a3 <= 0 || a2 == 0LL || !a1 )
result = 0xFFFFFFFFLL;
else
result = send(*a1, a2, a3, 0);
return result;
}

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

if ( a3 <= 0 || a2 == 0LL || !a1 )
result = 0xFFFFFFFFLL;
else
result = send(*a1, a2, a3, 0);
return result;
}

original_code:
int socket_write(t_socket *socket, void *buffer, int size)
{
  if (!socket || !buffer || size < 1)
    return (-1);
  return (send(socket->fd, buffer, size, 0));
}

canonical_original_code:
int socket_write(struct s_socket *socket, void *buffer, int size)
{
  if (!socket || !buffer || size < 1)
    return (-1);
  return (send(socket->fd, buffer, size, 0));
}


decompiled_code:
void __fastcall parser_command_list_elem_destroy(void *ptr)
{
if ( ptr )
{
free(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
free(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
static void parser_command_list_elem_destroy(void *del)
{
  t_argument *elem;
  if (!del)
    return ;
  elem = (t_argument *)del;
  free(elem->data);
  free(elem);
}

canonical_original_code:
static void parser_command_list_elem_destroy(void *del)
{
  struct s_argument *elem;
  if (!del)
    return ;
  elem = (struct s_argument *)del;
  free(elem->data);
  free(elem);
}

variable_types:
del
void *

variable_types: struct s_argument { int type; void *data; } *