decompiled_code:
_BOOL8 __fastcall is_ipv4_addr(char *cp)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

return inet_pton(2, cp, buf) == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(char *cp)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

return inet_pton(2, cp, buf) == 1;
}

original_code:
int is_ipv4_addr(const char * url)
{
 struct in_addr result;
 if (inet_pton(2, url, &result) == 1)
 {
  return 1;
 }
 return 0;
}

canonical_original_code:
int is_ipv4_addr(char *url)
{
 struct in_addr result;
 if (inet_pton(2, url, &result) == 1)
 {
  return 1;
 }
 return 0;
}

variable_types:
url
char *

variable_types:
result
struct in_addr { unsigned int s_addr; }


decompiled_code:
_BOOL8 __fastcall is_ipv6_addr(char *cp)
{
__int64 v2; // [rsp+0h] [rbp-18h] BYREF

return inet_pton(10, cp, &v2) == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(char *cp)
{
__int64 v2; // [rsp+0h] [rbp-18h] BYREF

return inet_pton(10, cp, &v2) == 1;
}

original_code:
int is_ipv6_addr(const char * url)
{
 struct in6_addr result;
 if (inet_pton(10, url, &result) == 1)
 {
  return 1;
 }
 return 0;
}

canonical_original_code:
int is_ipv6_addr(char *url)
{
 struct in6_addr result;
 if (inet_pton(10, url, &result) == 1)
 {
  return 1;
 }
 return 0;
}

variable_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }

return_type: int


decompiled_code:
__int64 __fastcall is_allowed_url(char *s1)
{
_QWORD *v1; // rbx
int v2; // er13
int v4; // eax
const char *v5; // rsi

v1 = (_QWORD *)allowed_url;
v2 = strlen(s1);
if ( !v1 )
return 0LL;
while ( 1 )
{
v5 = (const char *)v1[1];
if ( *v5 != 42 )
{
if ( !strcmp(s1, v5) )
return 1LL;
goto LABEL_5;
}
v4 = strlen(v5 + 1);
if ( v2 >= v4 && !strcmp(v5 + 1, &s1[v2 - v4]) )
return 1LL;
LABEL_5:
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func13(char *s1)
{
_QWORD *v1; // rbx
int v2; // er13
int v4; // eax
const char *v5; // rsi

v1 = (_QWORD *)allowed_url;
v2 = strlen(s1);
if ( !v1 )
return 0LL;
while ( 1 )
{
v5 = (const char *)v1[1];
if ( *v5 != 42 )
{
if ( !strcmp(s1, v5) )
return 1LL;
goto LABEL_5;
}
v4 = strlen(v5 + 1);
if ( v2 >= v4 && !strcmp(v5 + 1, &s1[v2 - v4]) )
return 1LL;
LABEL_5:
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
}

original_code:
int is_allowed_url(const char * url)
{
 struct allowed_url_t * p = allowed_url;
 int len_lookup_url = strlen(url);
 while (p) {
  if (p->url[0] == '*')
  {
   int len_list_url = strlen(&p->url[1]);
   if (len_lookup_url >= len_list_url)
   {
    if (!strcmp(&p->url[1], &url[ len_lookup_url - len_list_url ]))
    {
     return 1;
    }
   }
  }else{
   if (!strcmp(url, p->url))
    return 1;
  }
  p = p->next;
 }
 return 0;
}

canonical_original_code:
int is_allowed_url(char *url)
{
 struct allowed_url_t *p = allowed_url;
 int len_lookup_url = strlen(url);
 while (p) {
  if (p->url[0] == '*')
  {
   int len_list_url = strlen(&p->url[1]);
   if (len_lookup_url >= len_list_url)
   {
    if (!strcmp(&p->url[1], &url[ len_lookup_url - len_list_url ]))
    {
     return 1;
    }
   }
  }else{
   if (!strcmp(url, p->url))
    return 1;
  }
  p = p->next;
 }
 return 0;
}

variable_types: struct allowed_url_t { struct allowed_url_t *next; char *url; } *


decompiled_code:
__int64 __fastcall is_allowed(char *cp)
{
__uid_t v1; // eax
unsigned int v2; // ebx

v1 = getuid();
v2 = 1;
if ( !(unsigned int)is_allowed_user(v1) && !(unsigned int)is_ipv4_addr(cp) && !(unsigned int)is_ipv6_addr(cp) )
{
v2 = is_allowed_url(cp);
if ( v2 )
v2 = 1;
else
log_blocked(cp);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *cp)
{
__uid_t v1; // eax
unsigned int v2; // ebx

v1 = getuid();
v2 = 1;
if ( !(unsigned int)func10(v1) && !(unsigned int)func11(cp) && !(unsigned int)func12(cp) )
{
v2 = func13(cp);
if ( v2 )
v2 = 1;
else
func9(cp);
}
return v2;
}

original_code:
int is_allowed(const char * url)
{
 if (is_allowed_user(getuid()))
  return 1;
 if (is_ipv4_addr(url))
  return 1;
 if (is_ipv6_addr(url))
  return 1;
 if (is_allowed_url(url))
  return 1;
 log_blocked(url);
 return 0;
}

canonical_original_code:
int is_allowed(char *url)
{
 if (is_allowed_user(getuid()))
  return 1;
 if (is_ipv4_addr(url))
  return 1;
 if (is_ipv6_addr(url))
  return 1;
 if (is_allowed_url(url))
  return 1;
 log_blocked(url);
 return 0;
}


gethostent

decompiled_code:
char **gethostent()
{
char **v0; // rbx

init();
if ( !func_4 )
func_4 = (__int64 (*)(void))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "gethostent");
v0 = (char **)func_4();
if ( !(unsigned int)is_allowed(*v0) )
v0 = (char **)invalid_host;
return v0;
}

canonical_decompiled_code:
char **func15()
{
char **v0; // rbx

func8();
if ( !func_4 )
func_4 = (__int64 (*)(void))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "gethostent");
v0 = (char **)func_4();
if ( !(unsigned int)func14(*v0) )
v0 = (char **)invalid_host;
return v0;
}

original_code:
struct hostent *gethostent(void)
{
 init();
 static void * (*func)();
 if (!func)
  func = (void *(*)()) dlsym(((void *) -1l), "gethostent");
 struct hostent * try = func();
 if (is_allowed(try->h_name))
  return try;
 else
  return &invalid_host;
}

canonical_original_code:
struct hostent *gethostent()
{
 init();
 static void *(*func)();
 if (!func)
  func = (void *(*)()) dlsym(((void *) -1l), "gethostent");
 struct hostent *try = func();
 if (is_allowed(try->h_name))
  return try;
 else
  return &invalid_host;
}

variable_types:
func
void *(*)()

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


decompiled_code:
__int64 __fastcall gethostbyname_r(char *cp, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

init();
if ( !func_3 )
func_3 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostbyname_r");
if ( (unsigned int)is_allowed(cp) )
result = func_3(cp, a2, a3, a4, a5, a6);
else
result = 4294967273LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(char *cp, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

func8();
if ( !func_3 )
func_3 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostbyname_r");
if ( (unsigned int)func14(cp) )
result = func_3(cp, a2, a3, a4, a5, a6);
else
result = 4294967273LL;
return result;
}

original_code:
int gethostbyname_r(const char *name,
  struct hostent *ret, char *buf, size_t buflen,
  struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostbyname_r");
 if (!is_allowed(name)) {
  result = ((void *)0);
  return -23;
 }
 return func(name, ret, buf, buflen, result, h_errnop);
}

canonical_original_code:
int gethostbyname_r(char *name, struct hostent *ret, char *buf, unsigned long buflen, struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostbyname_r");
 if (!is_allowed(name)) {
  result = ((void *)0);
  return -23;
 }
 return func(name, ret, buf, buflen, result, h_errnop);
}

variable_types:
name
char *

variable_types:
buf
char *

variable_types:
buflen
unsigned long

variable_types:
result
struct hostent **

variable_types:
func
int (*)()

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


decompiled_code:
__int64 __fastcall gethostbyname2_r(char *cp, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7)
{
__int64 result; // rax

init();
if ( !func_2 )
func_2 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostbyname2_r");
if ( (unsigned int)is_allowed(cp) )
result = func_2(cp, a2, a3, a4, a5, a6, a7);
else
result = 4294967273LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(char *cp, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7)
{
__int64 result; // rax

func8();
if ( !func_2 )
func_2 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostbyname2_r");
if ( (unsigned int)func14(cp) )
result = func_2(cp, a2, a3, a4, a5, a6, a7);
else
result = 4294967273LL;
return result;
}

original_code:
int gethostbyname2_r(const char *name, int af,
  struct hostent *ret, char *buf, size_t buflen,
  struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostbyname2_r");
 if (!is_allowed(name)) {
  result = ((void *)0);
  return -23;
 }
 return func(name, af, ret, buf, buflen, result, h_errnop);
}

canonical_original_code:
int gethostbyname2_r(char *name, int af, struct hostent *ret, char *buf, unsigned long buflen, struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostbyname2_r");
 if (!is_allowed(name)) {
  result = ((void *)0);
  return -23;
 }
 return func(name, af, ret, buf, buflen, result, h_errnop);
}

variable_types: af int


decompiled_code:
__int64 __fastcall gethostent_r(__int64 a1, char *a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 result; // rax

init();
if ( !func_1 )
func_1 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostent_r");
if ( (unsigned int)is_allowed(a2) )
result = func_1(a1, a2, a3, a4, a5);
else
result = 4294967273LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, char *a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 result; // rax

func8();
if ( !func_1 )
func_1 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD))dlsym(
(void *)0xFFFFFFFFFFFFFFFFLL,
"gethostent_r");
if ( (unsigned int)func14(a2) )
result = func_1(a1, a2, a3, a4, a5);
else
result = 4294967273LL;
return result;
}

original_code:
int gethostent_r(
  struct hostent *ret, char *buf, size_t buflen,
  struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostent_r");
 if (!is_allowed(buf)) {
  result = ((void *)0);
  return -23;
 }
 return func(ret, buf, buflen, result, h_errnop);
}

canonical_original_code:
int gethostent_r(struct hostent *ret, char *buf, unsigned long buflen, struct hostent **result, int *h_errnop)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "gethostent_r");
 if (!is_allowed(buf)) {
  result = ((void *)0);
  return -23;
 }
 return func(ret, buf, buflen, result, h_errnop);
}


getaddrinfo

decompiled_code:
__int64 __fastcall getaddrinfo(char *cp, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // edx
__int64 result; // rax

init();
if ( !func_0 )
func_0 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "getaddrinfo");
v6 = is_allowed(cp);
result = 4LL;
if ( v6 )
result = func_0(cp, a2, a3, a4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(char *cp, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // edx
__int64 result; // rax

func8();
if ( !func_0 )
func_0 = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD))dlsym((void *)0xFFFFFFFFFFFFFFFFLL, "getaddrinfo");
v6 = func14(cp);
result = 4LL;
if ( v6 )
result = func_0(cp, a2, a3, a4);
return result;
}

original_code:
int getaddrinfo(const char *node, const char *service,
  const struct addrinfo *hints,
  struct addrinfo **res)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "getaddrinfo");
 if (!is_allowed(node)) {
  res = ((void *)0);
  return - -4;
 }
 return func(node, service, hints, res);
}

canonical_original_code:
int getaddrinfo(char *node, char *service, struct addrinfo *hints, struct addrinfo **res)
{
 init();
 static int (*func)();
 if (!func)
  func = (int (*)()) dlsym(((void *) -1l), "getaddrinfo");
 if (!is_allowed(node)) {
  res = ((void *)0);
  return - -4;
 }
 return func(node, service, hints, res);
}

variable_types:
node
char *

variable_types:
service
char *

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

variable_types:
res
struct addrinfo **

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


repo: mbabic/PokerSim

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

result = *(_QWORD *)a1;
++*(_DWORD *)(*(_QWORD *)a1 + 4LL * a2 - 4);
a1[2] = a3 + a1[2];
++*((_DWORD *)a1 + 9);
return result;
}

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

result = *(_QWORD *)a1;
++*(_DWORD *)(*(_QWORD *)a1 + 4LL * a2 - 4);
a1[2] = a3 + a1[2];
++*((_DWORD *)a1 + 9);
return result;
}

original_code:
void
update_stats(StatsStruct *ss, int rank, double win)
{
 ss->ranks[rank-1] += 1;
 ss->wins += win;
 ss->nSimulations++;
}

canonical_original_code:
void update_stats(struct StatsStruct *ss, int rank, double win)
{
 ss->ranks[rank-1] += 1;
 ss->wins += win;
 ss->nSimulations++;
}

variable_types: struct StatsStruct { int *ranks; double *results; double wins; double equity; int nPlayers; int nSimulations; } *

variable_types: rank int

variable_types: win double

return_type: void


original_code:
void
merge_stats_structs(StatsStruct *gss, StatsStruct *ss)
{
 int i;
 for (i = 0; i < gss->nPlayers; i++) {
  gss->ranks[i] += ss->ranks[i];
 }
 gss->wins += ss->wins;
 gss->nSimulations += ss->nSimulations;
}

canonical_original_code:
void merge_stats_structs(struct StatsStruct *gss, struct StatsStruct *ss)
{
 int i;
 for (i = 0; i < gss->nPlayers; i++) {
  gss->ranks[i] += ss->ranks[i];
 }
 gss->wins += ss->wins;
 gss->nSimulations += ss->nSimulations;
}

variable_types:
ss
struct StatsStruct *

variable_types: i int

user_defined_types: struct StatsStruct { int *ranks; double *results; double wins; double equity; int nPlayers; int nSimulations; }


original_code:
void
calculate_results(StatsStruct *ss)
{
 int nSimulations, nPlayers, i;
 nPlayers = ss->nPlayers;
 nSimulations = ss->nSimulations;
 for(i = 0; i < nPlayers; i++) {
  ss->results[i] = ((double)ss->ranks[i]) / (double)nSimulations;
 }
 ss->equity = ss->wins/(double)nSimulations;
}

canonical_original_code:
void calculate_results(struct StatsStruct *ss)
{
 int nSimulations; int nPlayers; int i;
 nPlayers = ss->nPlayers;
 nSimulations = ss->nSimulations;
 for(i = 0; i < nPlayers; i++) {
  ss->results[i] = ((double)ss->ranks[i]) / (double)nSimulations;
 }
 ss->equity = ss->wins/(double)nSimulations;
}

variable_types: nSimulations int

variable_types: nPlayers int


decompiled_code:
int __fastcall print_stats_struct(__int64 a1)
{
int result; // eax
__int64 v2; // rbx

result = fprintf(stdout, "Equity: \t\t\t\t %f\n", *(double *)(a1 + 24));
if ( *(int *)(a1 + 32) > 0 )
{
v2 = 0LL;
do
{
result = fprintf(
stdout,
"Likelihood given hand is of rank %d:\t %f\n",
(unsigned int)(v2 + 1),
*(double *)(*(_QWORD *)(a1 + 8) + 8 * v2));
++v2;
}
while ( *(_DWORD *)(a1 + 32) > (int)v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
__int64 v2; // rbx

result = fprintf(stdout, "Equity: \t\t\t\t %f\n", *(double *)(a1 + 24));
if ( *(int *)(a1 + 32) > 0 )
{
v2 = 0LL;
do
{
result = fprintf(
stdout,
"Likelihood given hand is of rank %d:\t %f\n",
(unsigned int)(v2 + 1),
*(double *)(*(_QWORD *)(a1 + 8) + 8 * v2));
++v2;
}
while ( *(_DWORD *)(a1 + 32) > (int)v2 );
}
return result;
}

original_code:
void
print_stats_struct(StatsStruct *ss)
{
 int i;
 fprintf(stdout, "Equity: \t\t\t\t %f\n", ss->equity);
 for(i = 0; i < ss->nPlayers; i++) {
  fprintf(stdout, "Likelihood given hand is of rank %d:\t %f\n",
      i+1, ss->results[i]);
 }
}

canonical_original_code:
void print_stats_struct(struct StatsStruct *ss)
{
 int i;
 fprintf(stdout, "Equity: \t\t\t\t %f\n", ss->equity);
 for(i = 0; i < ss->nPlayers; i++) {
  fprintf(stdout, "Likelihood given hand is of rank %d:\t %f\n",
      i+1, ss->results[i]);
 }
}


decompiled_code:
void __fastcall free_stats_struct(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

original_code:
void
free_stats_struct(StatsStruct *ss)
{
 if (!ss) return;
 if (ss->ranks) free(ss->ranks);
 if (ss->results) free(ss->results);
 free(ss);
}

canonical_original_code:
void free_stats_struct(struct StatsStruct *ss)
{
 if (!ss) return;
 if (ss->ranks) free(ss->ranks);
 if (ss->results) free(ss->results);
 free(ss);
}


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

switch ( a1 )
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'J':
case 'K':
case 'Q':
case 'T':
result = 1LL;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'J':
case 'K':
case 'Q':
case 'T':
result = 1LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
static int is_valid_card_value(char c)
{
 switch (c) {
 case 'A':
 case 'K':
 case 'Q':
 case 'J':
 case 'T':
 case '9':
 case '8':
 case '7':
 case '6':
 case '5':
 case '4':
 case '3':
 case '2':
  return 1;
 default:
  return 0;
 }
}

variable_types: c char


decompiled_code:
_BOOL8 __fastcall is_valid_card_suite(char a1)
{
_BOOL8 result; // rax

if ( (unsigned __int8)(a1 - 99) > 0x10u )
result = 0LL;
else
result = ((1LL << (a1 - 99)) & 0x10023) != 0;
return result;
}

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

if ( (unsigned __int8)(a1 - 99) > 0x10u )
result = 0LL;
else
result = ((1LL << (a1 - 99)) & 0x10023) != 0;
return result;
}

original_code:
static int is_valid_card_suite(char c)
{
 switch (c) {
 case 's':
 case 'd':
 case 'h':
 case 'c':
  return 1;
 default:
  return 0;
 }
}


decompiled_code:
__int64 __fastcall parse_input_string(char *a1, int a2, unsigned int a3)
{
char *v3; // rbx
__int64 v5; // rdx
unsigned int v6; // ebp
__int64 result; // rax

if ( !a1 )
{
fwrite("Passed null string to parse_input_string.\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
v3 = a1;
if ( a3 == 2 )
{
v5 = 2LL;
}
else if ( a3 > 2 )
{
v5 = 2LL;
if ( a3 != 3 )
{
fwrite("Invalid input: not of known type.\n", 1uLL, 0x22uLL, stderr);
return 0LL;
}
}
else
{
v5 = a3 == 0 ? 4 : 6;
}
if ( (_DWORD)v5 == a2 )
{
while ( 1 )
{
v6 = *v3;
if ( !(unsigned int)is_valid_card_value(*v3) || !is_valid_card_suite(v3[1]) )
{
fprintf(stderr, "Invalid input: invalid character(s) among \"%c%c\".\n", v6, (unsigned int)v3[1]);
return 0LL;
}
if ( !v3[2] )
break;
v3 += 2;
}
result = 1LL;
}
else
{
fprintf(stderr, "Invalid input: not of expected length %d.\n", v5);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, int a2, unsigned int a3)
{
char *v3; // rbx
__int64 v5; // rdx
unsigned int v6; // ebp
__int64 result; // rax

if ( !a1 )
{
fwrite("Passed null string to parse_input_string.\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
v3 = a1;
if ( a3 == 2 )
{
v5 = 2LL;
}
else if ( a3 > 2 )
{
v5 = 2LL;
if ( a3 != 3 )
{
fwrite("Invalid input: not of known type.\n", 1uLL, 0x22uLL, stderr);
return 0LL;
}
}
else
{
v5 = a3 == 0 ? 4 : 6;
}
if ( (_DWORD)v5 == a2 )
{
while ( 1 )
{
v6 = *v3;
if ( !(unsigned int)func0(*v3) || !func1(v3[1]) )
{
fprintf(stderr, "Invalid input: invalid character(s) among \"%c%c\".\n", v6, (unsigned int)v3[1]);
return 0LL;
}
if ( !v3[2] )
break;
v3 += 2;
}
result = 1LL;
}
else
{
fprintf(stderr, "Invalid input: not of expected length %d.\n", v5);
result = 0LL;
}
return result;
}

original_code:
int
parse_input_string(char *str, int len, InputType inputType)
{
 int expectedLen;
 if (!str) {
  fprintf(stderr, "Passed null string to parse_input_string.\n");
  exit(1);
 }
 switch (inputType) {
 case PLAYER_HAND:
  expectedLen = 4;
  break;
 case FLOP:
  expectedLen = 6;
  break;
 case TURN:
  expectedLen = 2;
  break;
 case RIVER:
  expectedLen = 2;
  break;
 default:
  fprintf(stderr, "Invalid input: not of known type.\n");
  return 0;
 }
 if (len != expectedLen) {
  fprintf(stderr, "Invalid input: not of expected length %d.\n",
      expectedLen);
  return 0;
 }
 while (1) {
  if (!(is_valid_card_value(*str)) ||
      !(is_valid_card_suite(*(str + 1)))) {
   fprintf(stderr, "Invalid input: invalid character(s) "
       "among \"%c%c\".\n", *str, *(str+1));
   return 0;
  }
  if (*(str + 2) == 0) break;
  str += 2;
 }
 return 1;
}

canonical_original_code:
int parse_input_string(char *str, int len, enum InputType inputType)
{
 int expectedLen;
 if (!str) {
  fprintf(stderr, "Passed null string to parse_input_string.\n");
  exit(1);
 }
 switch (inputType) {
 case PLAYER_HAND:
  expectedLen = 4;
  break;
 case FLOP:
  expectedLen = 6;
  break;
 case TURN:
  expectedLen = 2;
  break;
 case RIVER:
  expectedLen = 2;
  break;
 default:
  fprintf(stderr, "Invalid input: not of known type.\n");
  return 0;
 }
 if (len != expectedLen) {
  fprintf(stderr, "Invalid input: not of expected length %d.\n",
      expectedLen);
  return 0;
 }
 while (1) {
  if (!(is_valid_card_value(*str)) ||
      !(is_valid_card_suite(*(str + 1)))) {
   fprintf(stderr, "Invalid input: invalid character(s) "
       "among \"%c%c\".\n", *str, *(str+1));
   return 0;
  }
  if (*(str + 2) == 0) break;
  str += 2;
 }
 return 1;
}

variable_types:
str
char *

variable_types: len int

variable_types: enum InputType { PLAYER_HAND=0, FLOP=1, TURN=2, RIVER=3 }

variable_types: expectedLen int


repo: mbellier/pheromones

decompiled_code:
_QWORD *map__successors_new()
{
_QWORD *v0; // rbx
_DWORD *v1; // rax
_DWORD *v2; // rdx

v0 = malloc(0x10uLL);
if ( !v0 )
{
fprintf(stderr, "[%s] Error : ", "MAP");
fwrite("new successor struct (malloc struct)", 1uLL, 0x24uLL, stderr);
fputc(10, stderr);
}
v1 = malloc(0x40uLL);
v0[1] = v1;
*(_DWORD *)v0 = 0;
v2 = v1 + 16;
do
{
*v1 = 0;
v1[1] = 0;
v1 += 2;
}
while ( v1 != v2 );
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx
_DWORD *v1; // rax
_DWORD *v2; // rdx

v0 = malloc(0x10uLL);
if ( !v0 )
{
fprintf(stderr, "[%s] Error : ", "MAP");
fwrite("new successor struct (malloc struct)", 1uLL, 0x24uLL, stderr);
fputc(10, stderr);
}
v1 = malloc(0x40uLL);
v0[1] = v1;
*(_DWORD *)v0 = 0;
v2 = v1 + 16;
do
{
*v1 = 0;
v1[1] = 0;
v1 += 2;
}
while ( v1 != v2 );
return v0;
}

original_code:
map__successors *map__successors_new(void){
    map__successors *s = malloc(sizeof(*s));
    if(((void *)0)==s) if(1){fprintf(stderr, "[%s] Error : ", "MAP"); fprintf(stderr, "new successor struct (malloc struct)"); fprintf(stderr, "\n");}
    s->successors = malloc(sizeof(*(s->successors))* 8);
    if(((void *)0)==s) if(1){fprintf(stderr, "[%s] Error : ", "MAP"); fprintf(stderr, "new successor struct (malloc array"); fprintf(stderr, "\n");}
    s->nb = 0;
    int i;
    for(i=0; i<8; i++){
        s->successors[i].x = 0;
        s->successors[i].y = 0;
    }
    return s;
}

canonical_original_code:
struct map__successors *map__successors_new(){
    struct map__successors *s = malloc(sizeof(*s));
    if(((void *)0)==s) if(1){fprintf(stderr, "[%s] Error : ", "MAP"); fprintf(stderr, "new successor struct (malloc struct)"); fprintf(stderr, "\n");}
    s->successors = malloc(sizeof(*(s->successors))* 8);
    if(((void *)0)==s) if(1){fprintf(stderr, "[%s] Error : ", "MAP"); fprintf(stderr, "new successor struct (malloc array"); fprintf(stderr, "\n");}
    s->nb = 0;
    int i;
    for(i=0; i<8; i++){
        s->successors[i].x = 0;
        s->successors[i].y = 0;
    }
    return s;
}

variable_types: struct map__successors { int nb; struct point2d { int x; int y; } *successors; } *

user_defined_types: struct map__successors { int nb; struct point2d *successors; }


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

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

original_code:
void map__successors_free(map__successors *s){
    free(s->successors);
    free(s);
}

canonical_original_code:
void map__successors_free(struct map__successors *s){
    free(s->successors);
    free(s);
}


decompiled_code:
__int64 __fastcall map__successors_nb(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1)
{
return *a1;
}

original_code:
int map__successors_nb(map__successors *successors){
    return successors->nb;
}

canonical_original_code:
int map__successors_nb(struct map__successors *successors){
    return successors->nb;
}


decompiled_code:
void __fastcall float_matrix__free(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 2) > 0 )
{
v1 = 0LL;
do
free(*(void **)(*(_QWORD *)ptr + 8 * v1++));
while ( *((_DWORD *)ptr + 2) > (int)v1 );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 2) > 0 )
{
v1 = 0LL;
do
free(*(void **)(*(_QWORD *)ptr + 8 * v1++));
while ( *((_DWORD *)ptr + 2) > (int)v1 );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void float_matrix__free(float_matrix *m) {
    int i;
    for (i=0; i < m->size_x; i++) {
        free(m->data[i]);
    }
    free(m->data);
    free(m);
}

canonical_original_code:
void float_matrix__free(struct float_matrix *m) {
    int i;
    for (i=0; i < m->size_x; i++) {
        free(m->data[i]);
    }
    free(m->data);
    free(m);
}

variable_types: struct float_matrix { float **data; int size_x; int size_y; } *


decompiled_code:
void __fastcall float_matrix__init(__int64 a1, float a2)
{
__int64 i; // rsi
__int64 j; // rax

for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
for ( j = 0LL; *(_DWORD *)(a1 + 12) > (int)j; ++j )
*(float *)(*(_QWORD *)(*(_QWORD *)a1 + 8 * i) + 4 * j) = a2;
}
}

original_code:
void float_matrix__init(float_matrix *m, float value) {
    int i, j;
    for (i=0; i < m->size_x; i++) {
        for (j=0; j < m->size_y; j++) {
            m->data[i][j] = value;
        }
    }
}

canonical_original_code:
void float_matrix__init(struct float_matrix *m, float value) {
    int i; int j;
    for (i=0; i < m->size_x; i++) {
        for (j=0; j < m->size_y; j++) {
            m->data[i][j] = value;
        }
    }
}

variable_types: value float

variable_types: j int

user_defined_types: struct float_matrix { float **data; int size_x; int size_y; }


decompiled_code:
float __fastcall float_matrix__get(__int64 a1, int a2, int a3)
{
float result; // xmm0_4

result = *(float *)&LC2;
if ( a2 >= 0 )
{
if ( *(_DWORD *)(a1 + 8) > a2 && a3 >= 0 )
{
if ( *(_DWORD *)(a1 + 12) > a3 )
result = *(float *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 4LL * a3);
}
else
{
result = *(float *)&LC2;
}
}
return result;
}

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

result = *(float *)&LC2;
if ( a2 >= 0 )
{
if ( *(_DWORD *)(a1 + 8) > a2 && a3 >= 0 )
{
if ( *(_DWORD *)(a1 + 12) > a3 )
result = *(float *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 4LL * a3);
}
else
{
result = *(float *)&LC2;
}
}
return result;
}

original_code:
float float_matrix__get(float_matrix *m, int x, int y) {
    if (x>=0 && x < m->size_x && y>=0 && y < m->size_y) {
        return m->data[x][y];
    }
    return -1;
}

canonical_original_code:
float float_matrix__get(struct float_matrix *m, int x, int y) {
    if (x>=0 && x < m->size_x && y>=0 && y < m->size_y) {
        return m->data[x][y];
    }
    return -1;
}

variable_types: x int

variable_types: y int

return_type: float


decompiled_code:
__int64 __fastcall float_matrix__set(__int64 a1, int a2, int a3, float a4)
{
if ( a2 < 0 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)(a1 + 8) <= a2 || a3 < 0 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)(a1 + 12) <= a3 )
return 0xFFFFFFFFLL;
*(float *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 4LL * a3) = a4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, int a3, float a4)
{
if ( a2 < 0 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)(a1 + 8) <= a2 || a3 < 0 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)(a1 + 12) <= a3 )
return 0xFFFFFFFFLL;
*(float *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 4LL * a3) = a4;
return 0LL;
}

original_code:
int float_matrix__set(float_matrix *m, int x, int y, float value) {
    if (x>=0 && x < m->size_x && y>=0 && y < m->size_y) {
        m->data[x][y] = value;
        return 0;
    }
    return -1;
}

canonical_original_code:
int float_matrix__set(struct float_matrix *m, int x, int y, float value) {
    if (x>=0 && x < m->size_x && y>=0 && y < m->size_y) {
        m->data[x][y] = value;
        return 0;
    }
    return -1;
}


decompiled_code:
__int64 __fastcall float_matrix__size_x(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int float_matrix__size_x(float_matrix *m) {
    return m->size_x;
}

canonical_original_code:
int float_matrix__size_x(struct float_matrix *m) {
    return m->size_x;
}


decompiled_code:
__int64 __fastcall float_matrix__size_y(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
int float_matrix__size_y(float_matrix *m) {
    return m->size_y;
}

canonical_original_code:
int float_matrix__size_y(struct float_matrix *m) {
    return m->size_y;
}


decompiled_code:
__int64 __fastcall float_matrix__copy(_QWORD *a1, __int64 a2)
{
__int64 v2; // r8
__int64 result; // rax
__int64 i; // rax

if ( *(_QWORD *)(a2 + 8) == a1[1] )
{
v2 = 0LL;
if ( *(int *)(a2 + 8) > 0 )
{
do
{
for ( i = 0LL; *(_DWORD *)(a2 + 12) > (int)i; ++i )
*(_DWORD *)(*(_QWORD *)(*a1 + 8 * v2) + 4 * i) = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a2 + 8 * v2) + 4 * i);
++v2;
}
while ( *(_DWORD *)(a2 + 8) > (int)v2 );
result = 0LL;
}
else
{
result = 0LL;
}
}
else
{
fprintf(stderr, "[%s] Error : ", "MATRIX");
fwrite("sizes mismatching during copy.", 1uLL, 0x1EuLL, stderr);
fputc(10, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, __int64 a2)
{
__int64 v2; // r8
__int64 result; // rax
__int64 i; // rax

if ( *(_QWORD *)(a2 + 8) == a1[1] )
{
v2 = 0LL;
if ( *(int *)(a2 + 8) > 0 )
{
do
{
for ( i = 0LL; *(_DWORD *)(a2 + 12) > (int)i; ++i )
*(_DWORD *)(*(_QWORD *)(*a1 + 8 * v2) + 4 * i) = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a2 + 8 * v2) + 4 * i);
++v2;
}
while ( *(_DWORD *)(a2 + 8) > (int)v2 );
result = 0LL;
}
else
{
result = 0LL;
}
}
else
{
fprintf(stderr, "[%s] Error : ", "MATRIX");
fwrite("sizes mismatching during copy.", 1uLL, 0x1EuLL, stderr);
fputc(10, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int float_matrix__copy(float_matrix *d, float_matrix *s) {
    if (s->size_x != d->size_x || s->size_y != d->size_y){
        if(1){fprintf(stderr, "[%s] Error : ", "MATRIX"); fprintf(stderr, "sizes mismatching during copy."); fprintf(stderr, "\n");}
        return -1;
    }
    int i, j;
    for (i=0; i < s->size_x; i++) {
        for (j=0; j < s->size_y; j++) {
            d->data[i][j] = s->data[i][j];
        }
    }
    return 0;
}

canonical_original_code:
int float_matrix__copy(struct float_matrix *d, struct float_matrix *s) {
    if (s->size_x != d->size_x || s->size_y != d->size_y){
        if(1){fprintf(stderr, "[%s] Error : ", "MATRIX"); fprintf(stderr, "sizes mismatching during copy."); fprintf(stderr, "\n");}
        return -1;
    }
    int i; int j;
    for (i=0; i < s->size_x; i++) {
        for (j=0; j < s->size_y; j++) {
            d->data[i][j] = s->data[i][j];
        }
    }
    return 0;
}

variable_types:
s
struct float_matrix *


decompiled_code:
__int64 __fastcall float_matrix__debug_print(__int64 a1)
{
int i; // er12
float v2; // xmm0_4
int j; // ebx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = float_matrix__size_x(a1);
if ( (int)result <= i )
break;
for ( j = 0; (int)float_matrix__size_y(a1) > j; ++j )
{
v2 = float_matrix__get(a1, i, j);
printf("%.2f ", v2);
}
putchar(10);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int i; // er12
float v2; // xmm0_4
int j; // ebx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = func5(a1);
if ( (int)result <= i )
break;
for ( j = 0; (int)func6(a1) > j; ++j )
{
v2 = func3(a1, i, j);
printf("%.2f ", v2);
}
putchar(10);
}
return result;
}

original_code:
void float_matrix__debug_print(float_matrix *m) {
    int i, j;
    for (i=0; i<float_matrix__size_x(m); i++) {
        for (j=0; j<float_matrix__size_y(m); j++) {
            printf("%.2f ", float_matrix__get(m, i, j));
        }
        printf("\n");
    }
}

canonical_original_code:
void float_matrix__debug_print(struct float_matrix *m) {
    int i; int j;
    for (i=0; i<float_matrix__size_x(m); i++) {
        for (j=0; j<float_matrix__size_y(m); j++) {
            printf("%.2f ", float_matrix__get(m, i, j));
        }
        printf("\n");
    }
}


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

result = a1;
if ( a2 > 1 )
{
v3 = a2 - 2;
do
{
result = (unsigned int)(result * result);
--v3;
}
while ( v3 != -1 );
}
return result;
}

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

result = a1;
if ( a2 > 1 )
{
v3 = a2 - 2;
do
{
result = (unsigned int)(result * result);
--v3;
}
while ( v3 != -1 );
}
return result;
}

original_code:
int maths__power(int x, int p){
    if (--p<=0) return x;
    while(p--) {x *= x;}
    return x;
}

variable_types: p int


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

if ( a1 <= 1 )
return 1LL;
LODWORD(result) = 1;
do
result = (unsigned int)(result + 1);
while ( (int)result * (int)result < a1 );
return result;
}

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

if ( a1 <= 1 )
return 1LL;
LODWORD(result) = 1;
do
result = (unsigned int)(result + 1);
while ( (int)result * (int)result < a1 );
return result;
}

original_code:
int maths__sqrt(int x){
    int r = 1;
    for (r = 1; r*r < x; r++);
    return r;
}

variable_types: r int


decompiled_code:
__int64 __fastcall maths__norm(int a1, int a2, int a3, int a4)
{
int v5; // ebx
int v6; // eax

v5 = maths__power(a3 - a1, 2);
v6 = maths__power(a4 - a2, 2);
return maths__sqrt(v5 + v6);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, int a3, int a4)
{
int v5; // ebx
int v6; // eax

v5 = func0(a3 - a1, 2);
v6 = func0(a4 - a2, 2);
return func1(v5 + v6);
}

original_code:
int maths__norm(int x1, int y1, int x2, int y2){
    return maths__sqrt(maths__power(x2 - x1, 2) + maths__power(y2 - y1, 2));
}


decompiled_code:
void __fastcall maths__permutation(__int64 a1, int a2)
{
int v2; // ebp
unsigned int v3; // er12
int v4; // ebx
int *v5; // rdx
int v6; // ecx
int *v7; // rax

v2 = 4 * a2;
if ( 4 * a2 )
{
v3 = a2 - 1;
do
{
v4 = random__range(0LL, v3);
v5 = (int *)(a1 + 4LL * (int)random__range(0LL, v3));
v6 = *v5;
v7 = (int *)(a1 + 4LL * v4);
*v5 = *v7;
*v7 = v6;
--v2;
}
while ( v2 );
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2)
{
int v2; // ebp
unsigned int v3; // er12
int v4; // ebx
int *v5; // rdx
int v6; // ecx
int *v7; // rax

v2 = 4 * a2;
if ( 4 * a2 )
{
v3 = a2 - 1;
do
{
v4 = random__range(0LL, v3);
v5 = (int *)(a1 + 4LL * (int)random__range(0LL, v3));
v6 = *v5;
v7 = (int *)(a1 + 4LL * v4);
*v5 = *v7;
*v7 = v6;
--v2;
}
while ( v2 );
}
}

original_code:
void maths__permutation(int *tab, int len){
    int i, j, tmp, n = 4*len;
    while (n--){
        i = random__range(0, len - 1);
        j = random__range(0, len - 1);
        tmp = tab[j];
        tab[j] = tab[i];
        tab[i] = tmp;
    }
}

canonical_original_code:
void maths__permutation(int *tab, int len){
    int i; int j; int tmp; int n = 4*len;
    while (n--){
        i = random__range(0, len - 1);
        j = random__range(0, len - 1);
        tmp = tab[j];
        tab[j] = tab[i];
        tab[i] = tmp;
    }
}

variable_types:
tab
int *

variable_types: i int

variable_types: tmp int

variable_types: n int


decompiled_code:
void random__init()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
}

canonical_decompiled_code:
void func0()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
}

original_code:
void random__init (void){
    srand(time(((void *)0)));
}

canonical_original_code:
void random__init(){
    srand(time(((void *)0)));
}


decompiled_code:
int random__int()
{
return rand();
}

canonical_decompiled_code:
int func1()
{
return rand();
}

original_code:
int random__int (void){
    return rand();
}

canonical_original_code:
int random__int(){
    return rand();
}


decompiled_code:
__int64 __fastcall random__range(int a1, int a2)
{
return (unsigned int)(rand() % (a2 - a1) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
return (unsigned int)(rand() % (a2 - a1) + a1);
}

original_code:
int random__range (int a, int b){
    return rand() % (b - a) + a;
}

canonical_original_code:
int random__range(int a, int b){
    return rand() % (b - a) + a;
}

variable_types: a int

variable_types: b int


decompiled_code:
_BOOL8 __fastcall random__proba(int a1)
{
return rand() % a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1)
{
return rand() % a1 == 0;
}

original_code:
int random__proba (int one_out_of){
    return rand() % one_out_of == 0;
}

canonical_original_code:
int random__proba(int one_out_of){
    return rand() % one_out_of == 0;
}


decompiled_code:
__int64 __fastcall point2d__dir_by_pos(_DWORD *a1, _DWORD *a2)
{
int v2; // edx
__int64 result; // rax

v2 = *a2 - *a1;
result = (unsigned int)(a2[1] - a1[1]);
if ( (_DWORD)result )
{
if ( (int)result <= 0 )
{
result = 6LL;
if ( v2 )
result = 2 * (unsigned int)(v2 > 0) + 5;
}
else
{
result = 2LL;
if ( v2 )
result = 2 * (unsigned int)(v2 <= 0) + 1;
}
}
else if ( v2 < 0 )
{
result = 4LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, _DWORD *a2)
{
int v2; // edx
__int64 result; // rax

v2 = *a2 - *a1;
result = (unsigned int)(a2[1] - a1[1]);
if ( (_DWORD)result )
{
if ( (int)result <= 0 )
{
result = 6LL;
if ( v2 )
result = 2 * (unsigned int)(v2 > 0) + 5;
}
else
{
result = 2LL;
if ( v2 )
result = 2 * (unsigned int)(v2 <= 0) + 1;
}
}
else if ( v2 < 0 )
{
result = 4LL;
}
return result;
}

original_code:
int point2d__dir_by_pos(point2d *from, point2d *to){
    int x = to->x - from->x;
    int y = to->y - from->y;
    if (y == 0){
        if (x >= 0) return 0;
        else return 4;
    }else if (y>0){
        if (x==0) return 2;
        else if (x>0) return 1;
        else return 3;
    }else{
        if (x==0) return 6;
        else if (x>0) return 7;
        else return 5;
    }
}

canonical_original_code:
int point2d__dir_by_pos(struct point2d *from, struct point2d *to){
    int x = to->x - from->x;
    int y = to->y - from->y;
    if (y == 0){
        if (x >= 0) return 0;
        else return 4;
    }else if (y>0){
        if (x==0) return 2;
        else if (x>0) return 1;
        else return 3;
    }else{
        if (x==0) return 6;
        else if (x>0) return 7;
        else return 5;
    }
}

variable_types:
from
struct point2d { int x; int y; } *

variable_types:
to
struct point2d *


decompiled_code:
_BOOL8 __fastcall point2d__equal(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

original_code:
int point2d__equal(point2d *a, point2d *b){
    if(0){printf("[%s] ","POINT2D"); printf("%d %d == %d %d", a->x,a->y , b->x, b->y); printf("\n");}
    return ((a->x == b->x) && (a->y == b->y));
}

canonical_original_code:
int point2d__equal(struct point2d *a, struct point2d *b){
    if(0){printf("[%s] ","POINT2D"); printf("%d %d == %d %d", a->x,a->y , b->x, b->y); printf("\n");}
    return ((a->x == b->x) && (a->y == b->y));
}

variable_types:
a
struct point2d { int x; int y; } *

variable_types:
b
struct point2d *


decompiled_code:
_QWORD *fifo__new()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
*((_DWORD *)v0 + 4) = 0;
}
else
{
fprintf(stderr, "[%s] Error : ", "FIFO");
fwrite("malloc fifo", 1uLL, 0xBuLL, stderr);
fputc(10, stderr);
}
return v1;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
*((_DWORD *)v0 + 4) = 0;
}
else
{
fprintf(stderr, "[%s] Error : ", "FIFO");
fwrite("malloc fifo", 1uLL, 0xBuLL, stderr);
fputc(10, stderr);
}
return v1;
}

original_code:
fifo *fifo__new(){
    fifo *q = malloc(sizeof(*q));
    if (((void *)0) == q){
        if(1){fprintf(stderr, "[%s] Error : ", "FIFO"); fprintf(stderr, "malloc fifo"); fprintf(stderr, "\n");}
        return ((void *)0);
    }
    q->head = ((void *)0);
    q->tail = ((void *)0);
    q->size = 0;
    return q;
}

canonical_original_code:
struct fifo *fifo__new(){
    struct fifo *q = malloc(sizeof(*q));
    if (((void *)0) == q){
        if(1){fprintf(stderr, "[%s] Error : ", "FIFO"); fprintf(stderr, "malloc fifo"); fprintf(stderr, "\n");}
        return ((void *)0);
    }
    q->head = ((void *)0);
    q->tail = ((void *)0);
    q->size = 0;
    return q;
}

variable_types: struct fifo { struct fifo__element { struct fifo__element *next; void *data; } *head; struct fifo__element *tail; int size; } *

user_defined_types: struct fifo { struct fifo__element *head; struct fifo__element *tail; int size; }


decompiled_code:
void __fastcall fifo__free(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

if ( ptr )
{
v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free(v3);
}
while ( v2 );
}
free(ptr);
}
else
{
fprintf(stderr, "[%s] Error : ", "FIFO");
fwrite("nothing to free", 1uLL, 0xFuLL, stderr);
fputc(10, stderr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

if ( ptr )
{
v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free(v3);
}
while ( v2 );
}
free(ptr);
}
else
{
fprintf(stderr, "[%s] Error : ", "FIFO");
fwrite("nothing to free", 1uLL, 0xFuLL, stderr);
fputc(10, stderr);
}
}

original_code:
void fifo__free(fifo *queue){
    if (((void *)0) == queue){
        if(1){fprintf(stderr, "[%s] Error : ", "FIFO"); fprintf(stderr, "nothing to free"); fprintf(stderr, "\n");}
        return;
    }
    struct fifo__element *e = queue->head;
    struct fifo__element *tmp;
    while (e != ((void *)0)){
        tmp = e->next;
        free(e);
        e = tmp;
    }
    free(queue);
}

canonical_original_code:
void fifo__free(struct fifo *queue){
    if (((void *)0) == queue){
        if(1){fprintf(stderr, "[%s] Error : ", "FIFO"); fprintf(stderr, "nothing to free"); fprintf(stderr, "\n");}
        return;
    }
    struct fifo__element *e = queue->head;
    struct fifo__element *tmp;
    while (e != ((void *)0)){
        tmp = e->next;
        free(e);
        e = tmp;
    }
    free(queue);
}

variable_types: struct fifo__element { struct fifo__element *next; void *data; } *


decompiled_code:
_BOOL8 __fastcall fifo__is_empty(__int64 a1)
{
return *(_DWORD *)(a1 + 16) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1)
{
return *(_DWORD *)(a1 + 16) == 0;
}

original_code:
int fifo__is_empty(fifo *queue){
    return queue->size == 0;
}

canonical_original_code:
int fifo__is_empty(struct fifo *queue){
    return queue->size == 0;
}

user_defined_types: struct fifo__element { struct fifo__element *next; void *data; }


original_code:
void *fifo__pop(fifo *queue){
    if (queue->size <= 0){
        if(1){printf("[%s] ","FIFO"); printf("fifo is empty"); printf("\n");}
        return ((void *)0);
    }
    struct fifo__element *e = queue->head;
    queue->head = queue->head->next;
    queue->size--;
    void *data = e->data;
    free(e);
    return data;
}

canonical_original_code:
void *fifo__pop(struct fifo *queue){
    if (queue->size <= 0){
        if(1){printf("[%s] ","FIFO"); printf("fifo is empty"); printf("\n");}
        return ((void *)0);
    }
    struct fifo__element *e = queue->head;
    queue->head = queue->head->next;
    queue->size--;
    void *data = e->data;
    free(e);
    return data;
}

variable_types:
data
void *

return_type: void *


original_code:
int fifo__push(fifo *queue, void *data){
    struct fifo__element *new_tail = malloc(sizeof(*new_tail));
    new_tail->next = ((void *)0);
    new_tail->data = data;
    if (queue->tail != ((void *)0)){
        queue->tail->next = new_tail;
    }
    queue->tail = new_tail;
    if (queue->size == 0){
        queue->head = queue->tail;
    }
    queue->size++;
    return 0;
}

canonical_original_code:
int fifo__push(struct fifo *queue, void *data){
    struct fifo__element *new_tail = malloc(sizeof(*new_tail));
    new_tail->next = ((void *)0);
    new_tail->data = data;
    if (queue->tail != ((void *)0)){
        queue->tail->next = new_tail;
    }
    queue->tail = new_tail;
    if (queue->size == 0){
        queue->head = queue->tail;
    }
    queue->size++;
    return 0;
}


decompiled_code:
void __fastcall Regex_destroy(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}
}

original_code:
void Regex_destroy(Regex *regex)
{
    if (regex != ((void *)0)) {
        if (regex->value != ((void *)0)) {
            free(regex->value);
            regex->value = ((void *)0);
        }
        free(regex);
        regex = ((void *)0);
    }
}

canonical_original_code:
void Regex_destroy(struct Regex *regex)
{
    if (regex != ((void *)0)) {
        if (regex->value != ((void *)0)) {
            free(regex->value);
            regex->value = ((void *)0);
        }
        free(regex);
        regex = ((void *)0);
    }
}

variable_types: struct Regex { char *value; unsigned long length; } *


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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

original_code:
const char *Regex_get_value(Regex *regex)
{
    if (regex == ((void *)0)) {
        return ((void *)0);
    }
    return regex->value;
}

canonical_original_code:
char *Regex_get_value(struct Regex *regex)
{
    if (regex == ((void *)0)) {
        return ((void *)0);
    }
    return regex->value;
}

return_type: char *

user_defined_types: struct Regex { char *value; unsigned long length; }


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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

original_code:
size_t Regex_get_length(Regex *regex)
{
    if (regex == ((void *)0)) {
        return 0;
    }
    return regex->length;
}

canonical_original_code:
unsigned long Regex_get_length(struct Regex *regex)
{
    if (regex == ((void *)0)) {
        return 0;
    }
    return regex->length;
}

return_type: unsigned long


decompiled_code:
void __fastcall Regex_set_value(__int64 a1, const char *a2)
{
size_t v2; // rbp
void *v3; // rax
unsigned int v4; // edx
__int64 v5; // rax

if ( a1 )
{
v2 = strlen(a2);
v3 = realloc(*(void **)a1, v2 + 1);
*(_QWORD *)a1 = v3;
if ( v3 )
{
if ( v2 )
{
v4 = 0;
v5 = 0LL;
do
{
*(_BYTE *)(*(_QWORD *)a1 + v5) = a2[v5];
v5 = ++v4;
}
while ( v4 < v2 );
}
*(_BYTE *)(*(_QWORD *)a1 + v2) = 0;
}
*(_QWORD *)(a1 + 8) = v2;
}
}

original_code:
void Regex_set_value(Regex *regex, const char *value)
{
    if (regex == ((void *)0)) {
        return;
    }
    size_t length = strlen(value);
    regex->value = realloc(regex->value, sizeof(char) * (length + 1));
    if (regex->value != ((void *)0)) {
        unsigned int i;
        for (i = 0; i < length; ++i) {
            regex->value[i] = value[i];
        }
        regex->value[length] = '\0';
    }
    regex->length = length;
}

canonical_original_code:
void Regex_set_value(struct Regex *regex, char *value)
{
    if (regex == ((void *)0)) {
        return;
    }
    unsigned long length = strlen(value);
    regex->value = realloc(regex->value, sizeof(char) * (length + 1));
    if (regex->value != ((void *)0)) {
        unsigned int i;
        for (i = 0; i < length; ++i) {
            regex->value[i] = value[i];
        }
        regex->value[length] = '\0';
    }
    regex->length = length;
}

variable_types:
value
char *

variable_types:
length
unsigned long

variable_types:
i
unsigned int


repo: igloocommunity/hdmiservice

decompiled_code:
__int64 __fastcall hdmieventfile_read(int fd)
{
unsigned int v1; // ebx
unsigned int v2; // ebx
char v4[152]; // [rsp+0h] [rbp-98h] BYREF

v1 = read(fd, v4, 0x80uLL);
lseek(fd, 0LL, 0);
printf("libhdmi:read_res:%d\r\n", v1);
if ( v1 != 1 )
return (unsigned int)-1;
v2 = v4[0];
printf("libhdmi:events read:%02x\r\n", (unsigned int)v4[0]);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
unsigned int v1; // ebx
unsigned int v2; // ebx
char v4[152]; // [rsp+0h] [rbp-98h] BYREF

v1 = read(fd, v4, 0x80uLL);
lseek(fd, 0LL, 0);
printf("libhdmi:read_res:%d\r\n", v1);
if ( v1 != 1 )
return (unsigned int)-1;
v2 = v4[0];
printf("libhdmi:events read:%02x\r\n", (unsigned int)v4[0]);
return v2;
}

original_code:
static int hdmieventfile_read(int fd)
{
 int read_res;
 char buf[128];
 int events;
 read_res = read(fd, buf, sizeof(buf));
 lseek(fd, 0, 0);
 printf("libhdmi:" "read_res:%d""\r\n", read_res);
 if (read_res != 1)
  return -1;
 events = *buf;
 printf("libhdmi:" "events read:%02x""\r\n", events);
 return events;
}

variable_types: fd int

variable_types:
buf
char [128]

variable_types: events int


hdmievclr

decompiled_code:
__int64 __fastcall hdmievclr(char a1)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
char buf[28]; // [rsp+Ch] [rbp-1Ch] BYREF

buf[0] = a1;
v1 = open("/sys/class/misc/hdmi/evclr", 1);
if ( v1 < 0 )
{
printf("libhdmi: failed to open %s\r\n", "/sys/class/misc/hdmi/evclr");
v3 = -1;
}
else
{
v2 = v1;
v3 = 2 * (write(v1, buf, 1uLL) == 1) - 2;
close(v2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
char buf[28]; // [rsp+Ch] [rbp-1Ch] BYREF

buf[0] = a1;
v1 = open("/sys/class/misc/hdmi/evclr", 1);
if ( v1 < 0 )
{
printf("libhdmi: failed to open %s\r\n", "/sys/class/misc/hdmi/evclr");
v3 = -1;
}
else
{
v2 = v1;
v3 = 2 * (write(v1, buf, 1uLL) == 1) - 2;
close(v2);
}
return v3;
}

original_code:
int hdmievclr(__u8 mask)
{
 int evclrfd;
 int ret = 0;
 evclrfd = open("/sys/class/misc/hdmi/evclr", 01);
 if (evclrfd < 0) {
  printf("libhdmi:" " failed to open %s""\r\n", "/sys/class/misc/hdmi/evclr");
  return -1;
 }
 if (write(evclrfd, &mask, 1) != 1)
  ret = -2;
 close(evclrfd);
 return ret;
}

canonical_original_code:
int hdmievclr(unsigned char mask)
{
 int evclrfd;
 int ret = 0;
 evclrfd = open("/sys/class/misc/hdmi/evclr", 01);
 if (evclrfd < 0) {
  printf("libhdmi:" " failed to open %s""\r\n", "/sys/class/misc/hdmi/evclr");
  return -1;
 }
 if (write(evclrfd, &mask, 1) != 1)
  ret = -2;
 close(evclrfd);
 return ret;
}

variable_types:
mask
unsigned char

variable_types: evclrfd int

variable_types: ret int


decompiled_code:
void __noreturn thread_kevent_fn()
{
__int64 v0; // rbx
struct pollfd fd[4]; // [rsp+8h] [rbp-20h] BYREF

printf("libhdmi:%s begin\r\n", "thread_kevent_fn");
hdmieventfile_open((__int64)fd);
while ( 1 )
{
v0 = (unsigned int)hdmieventfile_read(fd[0].fd);
printf("libhdmi:kevent:%x\r\n", v0);
if ( (_DWORD)v0 == -1 )
{
usleep(0x186A0u);
hdmieventfile_close(fd[0].fd);
hdmieventfile_open((__int64)fd);
}
else
{
hdmi_event((unsigned int)v0);
if ( (v0 & 0x20) != 0 )
{
hdmievclr(255);
hdmieventfile_close(fd[0].fd);
printf("libhdmi:%s end\r\n", "thread_kevent_fn");
pthread_exit(0LL);
}
}
hdmieventfile_poll(fd);
}
}

canonical_decompiled_code:
void __noreturn func5()
{
__int64 v0; // rbx
struct pollfd fd[4]; // [rsp+8h] [rbp-20h] BYREF

printf("libhdmi:%s begin\r\n", "thread_kevent_fn");
func0((__int64)fd);
while ( 1 )
{
v0 = (unsigned int)func2(fd[0].fd);
printf("libhdmi:kevent:%x\r\n", v0);
if ( (_DWORD)v0 == -1 )
{
usleep(0x186A0u);
func1(fd[0].fd);
func0((__int64)fd);
}
else
{
hdmi_event((unsigned int)v0);
if ( (v0 & 0x20) != 0 )
{
func4(255);
func1(fd[0].fd);
printf("libhdmi:%s end\r\n", "thread_kevent_fn");
pthread_exit(0LL);
}
}
func3(fd);
}
}

original_code:
void thread_kevent_fn(void *arg)
{
 int event;
 struct pollfd pollfds;
 printf("libhdmi:" "%s begin""\r\n", __func__);
 hdmieventfile_open(&pollfds);
 while (1) {
  event = hdmieventfile_read(pollfds.fd);
  printf("libhdmi:" "kevent:%x""\r\n", event);
  if (event == -1) {
   usleep(100000);
   hdmieventfile_close(pollfds.fd);
   hdmieventfile_open(&pollfds);
  } else {
   hdmi_event(event);
   if (event & 0x20)
    break;
  }
  hdmieventfile_poll(&pollfds);
 }
 hdmievclr(0xFF);
 hdmieventfile_close(pollfds.fd);
 printf("libhdmi:" "%s end""\r\n", __func__);
 pthread_exit(((void *)0));
}

variable_types:
arg
void *

variable_types: event int

variable_types: struct pollfd { int fd; short events; short revents; }


decompiled_code:
const char *__fastcall dbg_otp(int a1)
{
const char *result; // rax

result = "OTP IS NOT PROGRAMMED";
if ( a1 )
{
result = "OTP status UNKNOWN";
if ( a1 == 1 )
result = "OTP IS PROGRAMMED";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

result = "OTP IS NOT PROGRAMMED";
if ( a1 )
{
result = "OTP status UNKNOWN";
if ( a1 == 1 )
result = "OTP IS PROGRAMMED";
}
return result;
}

original_code:
static char *dbg_otp(int value)
{
 switch (value) {
 case 0:
  return "OTP IS NOT PROGRAMMED";
  break;
 case 1:
  return "OTP IS PROGRAMMED";
  break;
 default:
  return "OTP status UNKNOWN";
  break;
 }
}

variable_types: value int


decompiled_code:
const char *__fastcall dbg_loadaes(int a1)
{
const char *result; // rax

result = "LOAD AES FAILED\n";
if ( a1 != -1 )
{
if ( a1 >= 0 )
{
result = "LOAD AES result UNKNOWN\n";
if ( !a1 )
result = "LOAD AES OK\n";
}
else
{
result = "LOAD AES FAILED CRC MISMATCH\n";
if ( a1 != -3 )
{
result = "LOAD AES result UNKNOWN\n";
if ( a1 == -2 )
result = "LOAD AES FAILED NOT FUSED\n";
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(int a1)
{
const char *result; // rax

result = "LOAD AES FAILED\n";
if ( a1 != -1 )
{
if ( a1 >= 0 )
{
result = "LOAD AES result UNKNOWN\n";
if ( !a1 )
result = "LOAD AES OK\n";
}
else
{
result = "LOAD AES FAILED CRC MISMATCH\n";
if ( a1 != -3 )
{
result = "LOAD AES result UNKNOWN\n";
if ( a1 == -2 )
result = "LOAD AES FAILED NOT FUSED\n";
}
}
}
return result;
}

original_code:
static char *dbg_loadaes(int value)
{
 switch (value) {
 case 0:
  return "LOAD AES OK\n";
  break;
 case -1:
  return "LOAD AES FAILED\n";
  break;
 case -2:
  return "LOAD AES FAILED NOT FUSED\n";
  break;
 case -3:
  return "LOAD AES FAILED CRC MISMATCH\n";
  break;
 default:
  return "LOAD AES result UNKNOWN\n";
  break;
 }
}


decompiled_code:
__int64 __fastcall hdcp_init(void *buf)
{
int v1; // eax
int v2; // ebx
int v3; // ebp
int v4; // ebx
const char *v5; // rax
unsigned int v6; // ebx
int v8; // eax
int v9; // ebx
int v10; // ebp
int v11; // eax
int v12; // ebx
unsigned int v13; // ebp
int v14; // eax
int v15; // ebx
int v16; // ebp
const char *v17; // rax
int v18; // eax
int v19; // ebp
unsigned int v20; // er12
char v21[152]; // [rsp+0h] [rbp-98h] BYREF

v1 = open("/sys/class/misc/hdmi/hdcpchkaesotp", 0);
if ( v1 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
v6 = -2;
}
else
{
v2 = v1;
v3 = read(v1, v21, 0x80uLL);
close(v2);
if ( v3 == 1 )
{
v4 = v21[0];
v5 = dbg_otp(v21[0]);
printf("libhdmi:%s\r\n", v5);
if ( v4 == 1 )
{
v8 = open("/sys/class/misc/hdmi/hdcpeven", 1);
v9 = v8;
if ( v8 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpeven");
v6 = -2;
}
else
{
v10 = write(v8, &hdcp_even_val, 1uLL);
close(v9);
if ( v10 != 1 )
return (unsigned int)-2;
v11 = open("/sys/class/misc/hdmi/hdcploadaes", 1);
v12 = v11;
if ( v11 < 0 )
goto LABEL_19;
v13 = write(v11, buf, 0x129uLL);
close(v12);
if ( v13 != 297 )
{
printf("libhdmi:***** Failed to write hdcploadaes %d *****\r\n", v13);
return (unsigned int)-2;
}
usleep(0x3D090u);
v14 = open("/sys/class/misc/hdmi/hdcploadaes", 0);
v15 = v14;
if ( v14 < 0 )
{
LABEL_19:
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcploadaes");
v6 = -2;
}
else
{
v16 = read(v14, v21, 0x80uLL);
close(v15);
if ( v16 == 1 )
{
v6 = v21[0];
v17 = dbg_loadaes(v21[0]);
printf("libhdmi:%s\r\n", v17);
if ( v6 )
{
v6 = -1;
}
else
{
printf("libhdmi:%s\r\n", "--- LOAD AES keys OK ---");
usleep(0x3D090u);
v18 = open("/sys/class/misc/hdmi/hdcpauthencr", 1);
v19 = v18;
if ( v18 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpauthencr");
v6 = -3;
}
else
{
v20 = write(v18, &hdcp_encr_start_val, 2uLL);
close(v19);
if ( v20 == 2 )
{
usleep(0xF4240u);
}
else
{
printf("libhdmi:***** Failed to write hdcpauthencr %d *****\r\n", v20);
v6 = -3;
}
}
}
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcploadaes");
v6 = -2;
}
}
}
}
else
{
puts("***** Missing aes file or HDCP AES OTP is not fused. *****");
v6 = 0;
}
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
v6 = -2;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *buf)
{
int v1; // eax
int v2; // ebx
int v3; // ebp
int v4; // ebx
const char *v5; // rax
unsigned int v6; // ebx
int v8; // eax
int v9; // ebx
int v10; // ebp
int v11; // eax
int v12; // ebx
unsigned int v13; // ebp
int v14; // eax
int v15; // ebx
int v16; // ebp
const char *v17; // rax
int v18; // eax
int v19; // ebp
unsigned int v20; // er12
char v21[152]; // [rsp+0h] [rbp-98h] BYREF

v1 = open("/sys/class/misc/hdmi/hdcpchkaesotp", 0);
if ( v1 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
v6 = -2;
}
else
{
v2 = v1;
v3 = read(v1, v21, 0x80uLL);
close(v2);
if ( v3 == 1 )
{
v4 = v21[0];
v5 = func0(v21[0]);
printf("libhdmi:%s\r\n", v5);
if ( v4 == 1 )
{
v8 = open("/sys/class/misc/hdmi/hdcpeven", 1);
v9 = v8;
if ( v8 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpeven");
v6 = -2;
}
else
{
v10 = write(v8, &hdcp_even_val, 1uLL);
close(v9);
if ( v10 != 1 )
return (unsigned int)-2;
v11 = open("/sys/class/misc/hdmi/hdcploadaes", 1);
v12 = v11;
if ( v11 < 0 )
goto LABEL_19;
v13 = write(v11, buf, 0x129uLL);
close(v12);
if ( v13 != 297 )
{
printf("libhdmi:***** Failed to write hdcploadaes %d *****\r\n", v13);
return (unsigned int)-2;
}
usleep(0x3D090u);
v14 = open("/sys/class/misc/hdmi/hdcploadaes", 0);
v15 = v14;
if ( v14 < 0 )
{
LABEL_19:
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcploadaes");
v6 = -2;
}
else
{
v16 = read(v14, v21, 0x80uLL);
close(v15);
if ( v16 == 1 )
{
v6 = v21[0];
v17 = func1(v21[0]);
printf("libhdmi:%s\r\n", v17);
if ( v6 )
{
v6 = -1;
}
else
{
printf("libhdmi:%s\r\n", "--- LOAD AES keys OK ---");
usleep(0x3D090u);
v18 = open("/sys/class/misc/hdmi/hdcpauthencr", 1);
v19 = v18;
if ( v18 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpauthencr");
v6 = -3;
}
else
{
v20 = write(v18, &hdcp_encr_start_val, 2uLL);
close(v19);
if ( v20 == 2 )
{
usleep(0xF4240u);
}
else
{
printf("libhdmi:***** Failed to write hdcpauthencr %d *****\r\n", v20);
v6 = -3;
}
}
}
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcploadaes");
v6 = -2;
}
}
}
}
else
{
puts("***** Missing aes file or HDCP AES OTP is not fused. *****");
v6 = 0;
}
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
v6 = -2;
}
}
return v6;
}

original_code:
int hdcp_init(__u8 *aes)
{
 int hdcpchkaesotp;
 int hdcploadaes;
 int hdcpauthencr;
 int hdcpeven;
 int res;
 int value = 0;
 char buf[128];
 int result = 0;
 hdcpchkaesotp = open("/sys/class/misc/hdmi/hdcpchkaesotp", 00);
 if (hdcpchkaesotp < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
  result = -2;
  goto hdcp_end;
 }
 res = read(hdcpchkaesotp, buf, sizeof(buf));
 close(hdcpchkaesotp);
 if (res != 1) {
  printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
  result = -2;
  goto hdcp_end;
 }
 value = *buf;
 printf("libhdmi:" "%s""\r\n", dbg_otp(value));
 if (value == 1) {
  hdcpeven = open("/sys/class/misc/hdmi/hdcpeven", 01);
  if (hdcpeven < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpeven");
   result = -2;
   goto hdcp_end;
  }
  res = write(hdcpeven, hdcp_even_val, sizeof(hdcp_even_val));
  close(hdcpeven);
  if (res != sizeof(hdcp_even_val)) {
   result = -2;
   goto hdcp_end;
  }
  hdcploadaes = open("/sys/class/misc/hdmi/hdcploadaes", 01);
  if (hdcploadaes < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  res = write(hdcploadaes, aes, 297);
  close(hdcploadaes);
  if (res != 297) {
   printf("libhdmi:" "***** Failed to write hdcploadaes %d " "*****""\r\n", res);
   result = -2;
   goto hdcp_end;
  }
  usleep(250000);
  hdcploadaes = open("/sys/class/misc/hdmi/hdcploadaes", 00);
  if (hdcploadaes < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  res = read(hdcploadaes, buf, sizeof(buf));
  close(hdcploadaes);
  if (res != 1) {
   printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  value = *buf;
  printf("libhdmi:" "%s""\r\n", dbg_loadaes(value));
  if (value == 0) {
   printf("libhdmi:" "%s""\r\n", "--- LOAD AES keys OK ---");
  } else {
   result = -1;
   goto hdcp_end;
  }
  usleep(250000);
  hdcpauthencr = open("/sys/class/misc/hdmi/hdcpauthencr", 01);
  if (hdcpauthencr < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpauthencr");
   result = -3;
   goto hdcp_end;
  }
  res = write(hdcpauthencr, hdcp_encr_start_val,
    sizeof(hdcp_encr_start_val));
  close(hdcpauthencr);
  if (res != sizeof(hdcp_encr_start_val)) {
   printf("libhdmi:" "***** Failed to write hdcpauthencr %d " "*****""\r\n", res);
   result = -3;
   goto hdcp_end;
  }
  usleep(1000000);
 } else {
  printf("***** Missing aes file or HDCP AES OTP is not fused."
    " *****\n");
 }
hdcp_end:
 return result;
}

canonical_original_code:
int hdcp_init(unsigned char *aes)
{
 int hdcpchkaesotp;
 int hdcploadaes;
 int hdcpauthencr;
 int hdcpeven;
 int res;
 int value = 0;
 char buf[128];
 int result = 0;
 hdcpchkaesotp = open("/sys/class/misc/hdmi/hdcpchkaesotp", 00);
 if (hdcpchkaesotp < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
  result = -2;
  goto hdcp_end;
 }
 res = read(hdcpchkaesotp, buf, sizeof(buf));
 close(hdcpchkaesotp);
 if (res != 1) {
  printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcpchkaesotp");
  result = -2;
  goto hdcp_end;
 }
 value = *buf;
 printf("libhdmi:" "%s""\r\n", dbg_otp(value));
 if (value == 1) {
  hdcpeven = open("/sys/class/misc/hdmi/hdcpeven", 01);
  if (hdcpeven < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpeven");
   result = -2;
   goto hdcp_end;
  }
  res = write(hdcpeven, hdcp_even_val, sizeof(hdcp_even_val));
  close(hdcpeven);
  if (res != sizeof(hdcp_even_val)) {
   result = -2;
   goto hdcp_end;
  }
  hdcploadaes = open("/sys/class/misc/hdmi/hdcploadaes", 01);
  if (hdcploadaes < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  res = write(hdcploadaes, aes, 297);
  close(hdcploadaes);
  if (res != 297) {
   printf("libhdmi:" "***** Failed to write hdcploadaes %d " "*****""\r\n", res);
   result = -2;
   goto hdcp_end;
  }
  usleep(250000);
  hdcploadaes = open("/sys/class/misc/hdmi/hdcploadaes", 00);
  if (hdcploadaes < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  res = read(hdcploadaes, buf, sizeof(buf));
  close(hdcploadaes);
  if (res != 1) {
   printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcploadaes");
   result = -2;
   goto hdcp_end;
  }
  value = *buf;
  printf("libhdmi:" "%s""\r\n", dbg_loadaes(value));
  if (value == 0) {
   printf("libhdmi:" "%s""\r\n", "--- LOAD AES keys OK ---");
  } else {
   result = -1;
   goto hdcp_end;
  }
  usleep(250000);
  hdcpauthencr = open("/sys/class/misc/hdmi/hdcpauthencr", 01);
  if (hdcpauthencr < 0) {
   printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpauthencr");
   result = -3;
   goto hdcp_end;
  }
  res = write(hdcpauthencr, hdcp_encr_start_val,
    sizeof(hdcp_encr_start_val));
  close(hdcpauthencr);
  if (res != sizeof(hdcp_encr_start_val)) {
   printf("libhdmi:" "***** Failed to write hdcpauthencr %d " "*****""\r\n", res);
   result = -3;
   goto hdcp_end;
  }
  usleep(1000000);
 } else {
  printf("***** Missing aes file or HDCP AES OTP is not fused."
    " *****\n");
 }
hdcp_end:
 return result;
}

variable_types:
aes
unsigned char *

variable_types: hdcpchkaesotp int

variable_types: hdcploadaes int

variable_types: hdcpauthencr int

variable_types: hdcpeven int

variable_types: res int

variable_types: result int


decompiled_code:
__int64 hdcp_state()
{
int v0; // ebp
int v1; // eax
int v2; // ebx
int v3; // er12
unsigned int v4; // ebx
const char *v5; // rax
int v7[3]; // [rsp+0h] [rbp-98h] BYREF
char v8; // [rsp+Ch] [rbp-8Ch]

v0 = get_new_cmd_id_ind();
v1 = open("/sys/class/misc/hdmi/hdcpstateget", 0);
if ( v1 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpstateget");
v4 = -2;
}
else
{
v2 = v1;
v3 = read(v1, v7, 0x80uLL);
close(v2);
if ( v3 == 1 )
{
v7[0] = 133;
v7[1] = v0;
v7[2] = 1;
v8 = -123;
v4 = clientsocket_send(v7, 13LL);
if ( v4 )
v4 = -2;
v5 = dbg_hdcpstate(1);
printf("libhdmi:%s\r\n", v5);
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcpstateget");
v4 = -2;
}
}
return v4;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // ebp
int v1; // eax
int v2; // ebx
int v3; // er12
unsigned int v4; // ebx
const char *v5; // rax
int v7[3]; // [rsp+0h] [rbp-98h] BYREF
char v8; // [rsp+Ch] [rbp-8Ch]

v0 = get_new_cmd_id_ind();
v1 = open("/sys/class/misc/hdmi/hdcpstateget", 0);
if ( v1 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/hdcpstateget");
v4 = -2;
}
else
{
v2 = v1;
v3 = read(v1, v7, 0x80uLL);
close(v2);
if ( v3 == 1 )
{
v7[0] = 133;
v7[1] = v0;
v7[2] = 1;
v8 = -123;
v4 = clientsocket_send(v7, 13LL);
if ( v4 )
v4 = -2;
v5 = func2(1);
printf("libhdmi:%s\r\n", v5);
}
else
{
printf("libhdmi:***** %s read error *****\r\n", "/sys/class/misc/hdmi/hdcpstateget");
v4 = -2;
}
}
return v4;
}

original_code:
int hdcp_state(void)
{
 int hdcpstateget;
 int result = 0;
 int res;
 __u8 buf[128];
 int val;
 __u32 cmd_id;
 cmd_id = get_new_cmd_id_ind();
 hdcpstateget = open("/sys/class/misc/hdmi/hdcpstateget", 00);
 if (hdcpstateget < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpstateget");
  result = -2;
  goto hdcp_state_end;
 }
 res = read(hdcpstateget, buf, sizeof(buf));
 close(hdcpstateget);
 if (res != 1) {
  printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcpstateget");
  result = -2;
  goto hdcp_state_end;
 }
 val = 0x85;
 memcpy(&buf[0], &val, 4);
 memcpy(&buf[4], &cmd_id, 4);
 val = 1;
 memcpy(&buf[8], &val, 4);
 memcpy(&buf[12], buf, val);
 if (clientsocket_send(buf, 12 + val) != 0)
  result = -2;
 printf("libhdmi:" "%s""\r\n", dbg_hdcpstate(val));
hdcp_state_end:
 return result;
}

canonical_original_code:
int hdcp_state()
{
 int hdcpstateget;
 int result = 0;
 int res;
 unsigned char buf[128];
 int val;
 unsigned int cmd_id;
 cmd_id = get_new_cmd_id_ind();
 hdcpstateget = open("/sys/class/misc/hdmi/hdcpstateget", 00);
 if (hdcpstateget < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/hdcpstateget");
  result = -2;
  goto hdcp_state_end;
 }
 res = read(hdcpstateget, buf, sizeof(buf));
 close(hdcpstateget);
 if (res != 1) {
  printf("libhdmi:" "***** %s read error *****""\r\n", "/sys/class/misc/hdmi/hdcpstateget");
  result = -2;
  goto hdcp_state_end;
 }
 val = 0x85;
 memcpy(&buf[0], &val, 4);
 memcpy(&buf[4], &cmd_id, 4);
 val = 1;
 memcpy(&buf[8], &val, 4);
 memcpy(&buf[12], buf, val);
 if (clientsocket_send(buf, 12 + val) != 0)
  result = -2;
 printf("libhdmi:" "%s""\r\n", dbg_hdcpstate(val));
hdcp_state_end:
 return result;
}

variable_types: hdcpstateget int

variable_types:
buf
unsigned char [128]

variable_types: val int


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

v3 = a3;
printf("libhdmi:timing:%d bit:%d\r\n", a1, a3);
if ( a1 == 2 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 3 )
result = 16LL;
}
else if ( a1 == 3 )
{
if ( a2 == 7 )
{
result = 22LL;
if ( v3 != 6 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 7 )
result = 23LL;
}
}
else if ( a2 == 8 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 7 )
result = 39LL;
}
else
{
result = 0xFFFFFFFFLL;
if ( a2 == 6 && v3 == 3 )
result = 14LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 1 )
{
result = 9LL;
if ( v3 )
result = 5 * (unsigned int)(v3 == 5) - 1;
}
}
return result;
}

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

v3 = a3;
printf("libhdmi:timing:%d bit:%d\r\n", a1, a3);
if ( a1 == 2 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 3 )
result = 16LL;
}
else if ( a1 == 3 )
{
if ( a2 == 7 )
{
result = 22LL;
if ( v3 != 6 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 7 )
result = 23LL;
}
}
else if ( a2 == 8 )
{
result = 0xFFFFFFFFLL;
if ( v3 == 7 )
result = 39LL;
}
else
{
result = 0xFFFFFFFFLL;
if ( a2 == 6 && v3 == 3 )
result = 14LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 1 )
{
result = 9LL;
if ( v3 )
result = 5 * (unsigned int)(v3 == 5) - 1;
}
}
return result;
}

original_code:
static int get_vesanr_from_est_timing(int timing, int byte, int bit)
{
 int vesa_nr = -1;
 printf("libhdmi:" "timing:%d bit:%d""\r\n", timing, bit);
 switch (timing) {
 case 1:
  switch (bit) {
  case 5:
   vesa_nr = 4;
   break;
  case 0:
   vesa_nr = 9;
   break;
  default:
   break;
  }
  break;
 case 2:
  switch (bit) {
  case 3:
   vesa_nr = 16;
   break;
  default:
   break;
  }
  break;
 case 3:
  switch (byte) {
  case 6:
   switch (bit) {
   case 3:
    vesa_nr = 14;
    break;
   default:
    break;
   }
   break;
  case 7:
   switch (bit) {
   case 7:
    vesa_nr = 23;
    break;
   case 6:
    vesa_nr = 22;
    break;
   default:
    break;
   }
   break;
  case 8:
   switch (bit) {
   case 7:
    vesa_nr = 39;
    break;
   default:
    break;
   }
   break;
  }
  break;
 default:
  break;
 }
 return vesa_nr;
}

variable_types: timing int

variable_types: byte int

variable_types: bit int


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

v4 = vesa_modes;
for ( i = 0; i != 7; ++i )
{
if ( *v4 == a1 && v4[1] == a2 && v4[2] == a3 )
return vesa_modes[4 * i + 3];
v4 += 4;
}
return 0xFFFFFFFFLL;
}

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

v4 = vesa_modes;
for ( i = 0; i != 7; ++i )
{
if ( *v4 == a1 && v4[1] == a2 && v4[2] == a3 )
return vesa_modes[4 * i + 3];
v4 += 4;
}
return 0xFFFFFFFFLL;
}

original_code:
int get_vesanr_from_std_timing(int xres, int yres, int freq)
{
 int vesa_nr = -1;
 int nr_of_timings;
 int index;
 nr_of_timings = sizeof(vesa_modes)/sizeof(vesa_modes[0]);
 for (index = 0; index < nr_of_timings; index++) {
  if ((xres == vesa_modes[index].xres) &&
    (yres == vesa_modes[index].yres) &&
    (freq == vesa_modes[index].freq)) {
   vesa_nr = vesa_modes[index].vesa_nr;
   break;
  }
 }
 return vesa_nr;
}

variable_types: xres int

variable_types: yres int

variable_types: freq int

variable_types: index int


decompiled_code:
__int64 __fastcall edid_read(unsigned __int8 a1, void *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // ebp
__int16 v8[20]; // [rsp+0h] [rbp-28h] BYREF

printf("libhdmi:EDID read blk %d\r\n", a1);
v2 = open("/sys/class/misc/hdmi/edidread", 2);
if ( v2 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/edidread");
v6 = -1;
}
else
{
v3 = v2;
v4 = -(a1 == 0);
LOBYTE(v4) = 0;
v8[0] = v4 + 416;
if ( (int)write(v3, v8, 2uLL) < 0 )
{
printf("libhdmi:***** Failed to write %s *****\r\n", "/sys/class/misc/hdmi/edidread");
v6 = -2;
}
else
{
lseek(v3, 0LL, 0);
v5 = read(v3, a2, 0x80uLL);
v6 = 0;
if ( v5 != 128 )
{
printf("libhdmi:***** %s read error size: %d *****\r\n", "/sys/class/misc/hdmi/edidread", v5);
v6 = -3;
}
}
close(v3);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1, void *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // ebp
__int16 v8[20]; // [rsp+0h] [rbp-28h] BYREF

printf("libhdmi:EDID read blk %d\r\n", a1);
v2 = open("/sys/class/misc/hdmi/edidread", 2);
if ( v2 < 0 )
{
printf("libhdmi:***** Failed to open %s *****\r\n", "/sys/class/misc/hdmi/edidread");
v6 = -1;
}
else
{
v3 = v2;
v4 = -(a1 == 0);
LOBYTE(v4) = 0;
v8[0] = v4 + 416;
if ( (int)write(v3, v8, 2uLL) < 0 )
{
printf("libhdmi:***** Failed to write %s *****\r\n", "/sys/class/misc/hdmi/edidread");
v6 = -2;
}
else
{
lseek(v3, 0LL, 0);
v5 = read(v3, a2, 0x80uLL);
v6 = 0;
if ( v5 != 128 )
{
printf("libhdmi:***** %s read error size: %d *****\r\n", "/sys/class/misc/hdmi/edidread", v5);
v6 = -3;
}
}
close(v3);
}
return v6;
}

original_code:
int edid_read(__u8 block, __u8 *data)
{
 int edidread;
 int res;
 int result = 0;
 __u8 buf[16];
 int size;
 printf("libhdmi:" "EDID read blk %d""\r\n", block);
 edidread = open("/sys/class/misc/hdmi/edidread", 02);
 if (edidread < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/edidread");
  result = -1;
  goto edid_read_end2;
 }
 if (block == 0) {
  size = sizeof(edidreqbl0);
  memcpy(buf, edidreqbl0, size);
 } else {
  size = sizeof(edidreqbl1);
  memcpy(buf, edidreqbl1, size);
 }
 res = write(edidread, buf, size);
 if (res < 0) {
  printf("libhdmi:" "***** Failed to write %s *****""\r\n", "/sys/class/misc/hdmi/edidread");
  result = -2;
  goto edid_read_end1;
 }
 lseek(edidread, 0, 0);
 res = read(edidread, data, 0x80);
 if (res != 0x80) {
  printf("libhdmi:" "***** %s read error size: %d *****""\r\n", "/sys/class/misc/hdmi/edidread", res);
  result = -3;
  goto edid_read_end1;
 }
edid_read_end1:
 close(edidread);
edid_read_end2:
 return result;
}

canonical_original_code:
int edid_read(unsigned char block, unsigned char *data)
{
 int edidread;
 int res;
 int result = 0;
 unsigned char buf[16];
 int size;
 printf("libhdmi:" "EDID read blk %d""\r\n", block);
 edidread = open("/sys/class/misc/hdmi/edidread", 02);
 if (edidread < 0) {
  printf("libhdmi:" "***** Failed to open %s *****""\r\n", "/sys/class/misc/hdmi/edidread");
  result = -1;
  goto edid_read_end2;
 }
 if (block == 0) {
  size = sizeof(edidreqbl0);
  memcpy(buf, edidreqbl0, size);
 } else {
  size = sizeof(edidreqbl1);
  memcpy(buf, edidreqbl1, size);
 }
 res = write(edidread, buf, size);
 if (res < 0) {
  printf("libhdmi:" "***** Failed to write %s *****""\r\n", "/sys/class/misc/hdmi/edidread");
  result = -2;
  goto edid_read_end1;
 }
 lseek(edidread, 0, 0);
 res = read(edidread, data, 0x80);
 if (res != 0x80) {
  printf("libhdmi:" "***** %s read error size: %d *****""\r\n", "/sys/class/misc/hdmi/edidread", res);
  result = -3;
  goto edid_read_end1;
 }
edid_read_end1:
 close(edidread);
edid_read_end2:
 return result;
}

variable_types:
block
unsigned char

variable_types:
data
unsigned char *

variable_types: edidread int

variable_types:
buf
unsigned char [16]

variable_types: size int


decompiled_code:
__int64 __fastcall edid_parse0(unsigned __int8 *a1, _BYTE *a2, _BYTE *a3, int a4)
{
__int64 v4; // r13
__int64 i; // r15
_BYTE *v6; // rbx
unsigned int j; // ebp
__int64 v8; // r12
unsigned __int8 v9; // bl
int v10; // er14
__int64 k; // r12
_BYTE *v12; // rbx
unsigned int v13; // ebp
__int64 v14; // rdi
unsigned int v15; // ebx
unsigned int v16; // er15
int v17; // eax
unsigned int v18; // ebp
unsigned int v23; // [rsp+1Ch] [rbp-4Ch]

*a2 = 0;
v23 = memcmp(a1, &edid_block0_start, 8uLL);
if ( v23 )
{
printf(
"libhdmi:edid response:\n%02x %02x %02x %02x %02x %02x %02x %02x\r\n",
a1[1],
a1[2],
a1[3],
a1[4],
a1[5],
a1[6],
a1[7],
a1[8]);
v23 = -3;
}
else
{
printf("libhdmi:%s\r\n", "--- EDID block 0 start OK ---");
printf("libhdmi:Ver:%d Rev:%d\r\n", a1[18], a1[19]);
v4 = (__int64)&a3[4 * (a4 - 1) + 4];
for ( i = 1LL; i != 3; ++i )
{
v9 = a1[edid_esttim1_2_offset[i - 1]];
v10 = v9;
printf("libhdmi:EstTim%d:%02x\r\n", (unsigned int)i, v9);
if ( v9 )
{
for ( j = 7; j != -1; --j )
{
if ( _bittest(&v10, j) )
{
v8 = (unsigned int)get_vesanr_from_est_timing(i, 0, j);
printf("libhdmi:vesa_nr:%d\r\n", v8);
if ( (int)v8 > 0 )
{
printf("libhdmi:EstTim1&2 try vesa_nr:%d\r\n", (unsigned int)v8);
if ( a4 > 0 )
{
v6 = a3;
while ( 1 )
{
printf("libhdmi:with:%d\r\n", (unsigned __int8)v6[1]);
if ( !*v6 && (unsigned __int8)v6[1] == (_DWORD)v8 )
break;
v6 += 4;
if ( v6 == (_BYTE *)v4 )
goto LABEL_8;
}
v6[2] = 1;
printf("libhdmi:EstTim1&2 %d vesa_nr:%d\r\n", (unsigned int)i, (unsigned int)v8);
}
}
}
LABEL_8:
;
}
}
}
for ( k = 1LL; k != 9; ++k )
{
v13 = 8 * a1[2 * k + 36] + 248;
v14 = (a1[2 * k + 37] >> 6) & 3;
v15 = (int)(edid_stdtim_ar[2 * v14 + 1] * v13) / edid_stdtim_ar[2 * v14];
v16 = (a1[2 * k + 37] & 0x3F) + 60;
printf("libhdmi:xres:%d yres:%d freq:%d\r\n", v13, v15, v16);
v17 = get_vesanr_from_std_timing(v13, v15, v16);
v18 = v17;
if ( v17 > 0 )
{
printf("libhdmi:StdTim1to8 try vesa_nr:%d\r\n", (unsigned int)v17);
if ( a4 > 0 )
{
v12 = a3;
while ( 1 )
{
printf("libhdmi:with:%d\r\n", (unsigned __int8)v12[1]);
if ( !*v12 && (unsigned __int8)v12[1] == v18 )
break;
v12 += 4;
if ( v12 == (_BYTE *)v4 )
goto LABEL_21;
}
v12[2] = 1;
printf("libhdmi:StdTim1to8 %d vesa_nr:%d\r\n", (unsigned int)k, v18);
}
}
LABEL_21:
;
}
if ( a1[126] )
*a2 = 1;
}
return v23;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, _BYTE *a2, _BYTE *a3, int a4)
{
__int64 v4; // r13
__int64 i; // r15
_BYTE *v6; // rbx
unsigned int j; // ebp
__int64 v8; // r12
unsigned __int8 v9; // bl
int v10; // er14
__int64 k; // r12
_BYTE *v12; // rbx
unsigned int v13; // ebp
__int64 v14; // rdi
unsigned int v15; // ebx
unsigned int v16; // er15
int v17; // eax
unsigned int v18; // ebp
unsigned int v23; // [rsp+1Ch] [rbp-4Ch]

*a2 = 0;
v23 = memcmp(a1, &edid_block0_start, 8uLL);
if ( v23 )
{
printf(
"libhdmi:edid response:\n%02x %02x %02x %02x %02x %02x %02x %02x\r\n",
a1[1],
a1[2],
a1[3],
a1[4],
a1[5],
a1[6],
a1[7],
a1[8]);
v23 = -3;
}
else
{
printf("libhdmi:%s\r\n", "--- EDID block 0 start OK ---");
printf("libhdmi:Ver:%d Rev:%d\r\n", a1[18], a1[19]);
v4 = (__int64)&a3[4 * (a4 - 1) + 4];
for ( i = 1LL; i != 3; ++i )
{
v9 = a1[edid_esttim1_2_offset[i - 1]];
v10 = v9;
printf("libhdmi:EstTim%d:%02x\r\n", (unsigned int)i, v9);
if ( v9 )
{
for ( j = 7; j != -1; --j )
{
if ( _bittest(&v10, j) )
{
v8 = (unsigned int)func0(i, 0, j);
printf("libhdmi:vesa_nr:%d\r\n", v8);
if ( (int)v8 > 0 )
{
printf("libhdmi:EstTim1&2 try vesa_nr:%d\r\n", (unsigned int)v8);
if ( a4 > 0 )
{
v6 = a3;
while ( 1 )
{
printf("libhdmi:with:%d\r\n", (unsigned __int8)v6[1]);
if ( !*v6 && (unsigned __int8)v6[1] == (_DWORD)v8 )
break;
v6 += 4;
if ( v6 == (_BYTE *)v4 )
goto LABEL_8;
}
v6[2] = 1;
printf("libhdmi:EstTim1&2 %d vesa_nr:%d\r\n", (unsigned int)i, (unsigned int)v8);
}
}
}
LABEL_8:
;
}
}
}
for ( k = 1LL; k != 9; ++k )
{
v13 = 8 * a1[2 * k + 36] + 248;
v14 = (a1[2 * k + 37] >> 6) & 3;
v15 = (int)(edid_stdtim_ar[2 * v14 + 1] * v13) / edid_stdtim_ar[2 * v14];
v16 = (a1[2 * k + 37] & 0x3F) + 60;
printf("libhdmi:xres:%d yres:%d freq:%d\r\n", v13, v15, v16);
v17 = func1(v13, v15, v16);
v18 = v17;
if ( v17 > 0 )
{
printf("libhdmi:StdTim1to8 try vesa_nr:%d\r\n", (unsigned int)v17);
if ( a4 > 0 )
{
v12 = a3;
while ( 1 )
{
printf("libhdmi:with:%d\r\n", (unsigned __int8)v12[1]);
if ( !*v12 && (unsigned __int8)v12[1] == v18 )
break;
v12 += 4;
if ( v12 == (_BYTE *)v4 )
goto LABEL_21;
}
v12[2] = 1;
printf("libhdmi:StdTim1to8 %d vesa_nr:%d\r\n", (unsigned int)k, v18);
}
}
LABEL_21:
;
}
if ( a1[126] )
*a2 = 1;
}
return v23;
}

original_code:
int edid_parse0(__u8 *data, __u8 *extension, struct video_format formats[],
   int nr_formats)
{
 __u8 version;
 __u8 revision;
 __u8 est_timing;
 int vesa_nr;
 int bit;
 int cnt;
 int index;
 int xres;
 int yres;
 int byte;
 int ar_index;
 int freq;
 __u8 edidp;
 *extension = 0;
 if (memcmp(data + 0x00, edid_block0_start, 8) != 0) {
  printf("libhdmi:" "edid response:\n%02x %02x %02x %02x %02x %02x %02x " "%02x""\r\n", *(data + 1), *(data + 2), *(data + 3), *(data + 4), *(data + 5), *(data + 6), *(data + 7), *(data + 8));
  return -3;
 } else {
  printf("libhdmi:" "%s""\r\n", "--- EDID block 0 start OK ---");
 }
 version = *(data + 0x12);
 revision = *(data + 0x13);
 printf("libhdmi:" "Ver:%d Rev:%d""\r\n", version, revision);
 for (index = 0; index <= 1; index++) {
  est_timing = *(data + edid_esttim1_2_offset[index]);
  printf("libhdmi:" "EstTim%d:%02x""\r\n", index + 1, est_timing);
  if (est_timing == 0)
   continue;
  for (bit = 7; bit >= 0; bit--) {
   if (est_timing & (1 << bit)) {
    vesa_nr = get_vesanr_from_est_timing(index + 1,
         0, bit);
    printf("libhdmi:" "vesa_nr:%d""\r\n", vesa_nr);
    if (vesa_nr < 1)
     continue;
    printf("libhdmi:" "EstTim1&2 try vesa_nr:%d""\r\n", vesa_nr);
    for (cnt = 0; cnt < nr_formats; cnt++) {
     printf("libhdmi:" "with:%d""\r\n", formats[cnt].vesaceanr);
     if ((formats[cnt].cea == 0) &&
      (formats[cnt].vesaceanr ==
        vesa_nr)) {
      formats[cnt].sink_support = 1;
      printf("libhdmi:" "EstTim1&2 %d " "vesa_nr:%d""\r\n", index + 1, vesa_nr);
      break;
     }
    }
   }
  }
 }
 for (index = 0; index < 8; index++) {
  edidp = 0x26 + index * 2;
  xres = (*(data + edidp) + 31) * 8;
  byte = *(data + edidp + 1);
  ar_index = (byte & 0xC0) >> 6;
  yres = xres * edid_stdtim_ar[ar_index].y /
     edid_stdtim_ar[ar_index].x;
  freq = 60 + ((byte & 0x3F) >>
    0);
  printf("libhdmi:" "xres:%d yres:%d freq:%d""\r\n", xres, yres, freq);
  vesa_nr = get_vesanr_from_std_timing(xres, yres, freq);
  if (vesa_nr < 1)
   continue;
  printf("libhdmi:" "StdTim1to8 try vesa_nr:%d""\r\n", vesa_nr);
  for (cnt = 0; cnt < nr_formats; cnt++) {
   printf("libhdmi:" "with:%d""\r\n", formats[cnt].vesaceanr);
   if ((formats[cnt].cea == 0) &&
     (formats[cnt].vesaceanr ==
       vesa_nr)) {
    formats[cnt].sink_support = 1;
    printf("libhdmi:" "StdTim1to8 %d vesa_nr:%d""\r\n", index + 1, vesa_nr);
    break;
   }
  }
 }
 if (*(data + 0x7E) != 0)
  *extension = 1;
 return 0;
}

canonical_original_code:
int edid_parse0(unsigned char *data, unsigned char *extension, struct video_format formats[], int nr_formats)
{
 unsigned char version;
 unsigned char revision;
 unsigned char est_timing;
 int vesa_nr;
 int bit;
 int cnt;
 int index;
 int xres;
 int yres;
 int byte;
 int ar_index;
 int freq;
 unsigned char edidp;
 *extension = 0;
 if (memcmp(data + 0x00, edid_block0_start, 8) != 0) {
  printf("libhdmi:" "edid response:\n%02x %02x %02x %02x %02x %02x %02x " "%02x""\r\n", *(data + 1), *(data + 2), *(data + 3), *(data + 4), *(data + 5), *(data + 6), *(data + 7), *(data + 8));
  return -3;
 } else {
  printf("libhdmi:" "%s""\r\n", "--- EDID block 0 start OK ---");
 }
 version = *(data + 0x12);
 revision = *(data + 0x13);
 printf("libhdmi:" "Ver:%d Rev:%d""\r\n", version, revision);
 for (index = 0; index <= 1; index++) {
  est_timing = *(data + edid_esttim1_2_offset[index]);
  printf("libhdmi:" "EstTim%d:%02x""\r\n", index + 1, est_timing);
  if (est_timing == 0)
   continue;
  for (bit = 7; bit >= 0; bit--) {
   if (est_timing & (1 << bit)) {
    vesa_nr = get_vesanr_from_est_timing(index + 1,
         0, bit);
    printf("libhdmi:" "vesa_nr:%d""\r\n", vesa_nr);
    if (vesa_nr < 1)
     continue;
    printf("libhdmi:" "EstTim1&2 try vesa_nr:%d""\r\n", vesa_nr);
    for (cnt = 0; cnt < nr_formats; cnt++) {
     printf("libhdmi:" "with:%d""\r\n", formats[cnt].vesaceanr);
     if ((formats[cnt].cea == 0) &&
      (formats[cnt].vesaceanr ==
        vesa_nr)) {
      formats[cnt].sink_support = 1;
      printf("libhdmi:" "EstTim1&2 %d " "vesa_nr:%d""\r\n", index + 1, vesa_nr);
      break;
     }
    }
   }
  }
 }
 for (index = 0; index < 8; index++) {
  edidp = 0x26 + index * 2;
  xres = (*(data + edidp) + 31) * 8;
  byte = *(data + edidp + 1);
  ar_index = (byte & 0xC0) >> 6;
  yres = xres * edid_stdtim_ar[ar_index].y /
     edid_stdtim_ar[ar_index].x;
  freq = 60 + ((byte & 0x3F) >>
    0);
  printf("libhdmi:" "xres:%d yres:%d freq:%d""\r\n", xres, yres, freq);
  vesa_nr = get_vesanr_from_std_timing(xres, yres, freq);
  if (vesa_nr < 1)
   continue;
  printf("libhdmi:" "StdTim1to8 try vesa_nr:%d""\r\n", vesa_nr);
  for (cnt = 0; cnt < nr_formats; cnt++) {
   printf("libhdmi:" "with:%d""\r\n", formats[cnt].vesaceanr);
   if ((formats[cnt].cea == 0) &&
     (formats[cnt].vesaceanr ==
       vesa_nr)) {
    formats[cnt].sink_support = 1;
    printf("libhdmi:" "StdTim1to8 %d vesa_nr:%d""\r\n", index + 1, vesa_nr);
    break;
   }
  }
 }
 if (*(data + 0x7E) != 0)
  *extension = 1;
 return 0;
}

variable_types:
extension
unsigned char *

variable_types: struct video_format { unsigned char cea; unsigned char vesaceanr; unsigned char sink_support; unsigned char prio; } []

variable_types:
version
unsigned char

variable_types:
revision
unsigned char

variable_types: cnt int

variable_types:
edidp
unsigned char