flipPecas

decompiled_code:
char __fastcall flipPecas(__int64 a1, unsigned int a2, int a3, unsigned __int8 a4)
{
char result; // al
char v6; // cl

result = a2 > 7 || (unsigned int)a1 > 7;
if ( result )
return 0;
v6 = tabuleiro[8 * (int)a1 + a2];
if ( v6 == a4 )
return 1;
if ( v6 )
{
if ( a3 == 2 )
{
result = flipPecas(a1, a2 + 1, 2LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
else if ( a3 > 2 )
{
if ( a3 == 3 )
{
result = flipPecas((unsigned int)(a1 - 1), a2, 3LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
else if ( a3 )
{
if ( a3 == 1 )
{
result = flipPecas((unsigned int)(a1 + 1), a2, 1LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
else
{
result = flipPecas(a1, a2 - 1, 0LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
return result;
}

canonical_decompiled_code:
char __fastcall func14(__int64 a1, unsigned int a2, int a3, unsigned __int8 a4)
{
char result; // al
char v6; // cl

result = a2 > 7 || (unsigned int)a1 > 7;
if ( result )
return 0;
v6 = tabuleiro[8 * (int)a1 + a2];
if ( v6 == a4 )
return 1;
if ( v6 )
{
if ( a3 == 2 )
{
result = func14(a1, a2 + 1, 2LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
else if ( a3 > 2 )
{
if ( a3 == 3 )
{
result = func14((unsigned int)(a1 - 1), a2, 3LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
else if ( a3 )
{
if ( a3 == 1 )
{
result = func14((unsigned int)(a1 + 1), a2, 1LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
else
{
result = func14(a1, a2 - 1, 0LL, a4);
if ( result )
tabuleiro[8 * (int)a1 + a2] = a4;
}
}
return result;
}

original_code:
_Bool flipPecas(int i, int j, int dir, byte tipo)
{
 if(i < 0 || i > 7 || j < 0 || j > 7)
  return 0;
 else if (tabuleiro[i][j] == tipo)
  return 1;
 else if(tabuleiro[i][j] == 0x00u)
  return 0;
 switch(dir)
 {
 case 0:
  if(flipPecas(i, j - 1, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 1:
  if(flipPecas(i + 1, j, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 2:
  if(flipPecas(i, j + 1, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 3:
  if(flipPecas(i - 1, j, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 }
}

canonical_original_code:
_Bool flipPecas(int i, int j, int dir, unsigned char tipo)
{
 if(i < 0 || i > 7 || j < 0 || j > 7)
  return 0;
 else if (tabuleiro[i][j] == tipo)
  return 1;
 else if(tabuleiro[i][j] == 0x00u)
  return 0;
 switch(dir)
 {
 case 0:
  if(flipPecas(i, j - 1, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 1:
  if(flipPecas(i + 1, j, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 2:
  if(flipPecas(i, j + 1, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 case 3:
  if(flipPecas(i - 1, j, dir, tipo))
   tabuleiro[i][j] = tipo;
  break;
 }
}

variable_types: dir int

variable_types:
tipo
unsigned char


checkPecaPosta

repo: luksamuk/opengames

decompiled_code:
char __fastcall checkPecaPosta(__int64 a1, unsigned int a2)
{
unsigned __int8 *v2; // r12

v2 = (unsigned __int8 *)&tabuleiro[8 * (int)a1 + a2];
flipPecas(a1, a2 - 1, 0, *v2);
flipPecas((unsigned int)(a1 + 1), a2, 1, *v2);
flipPecas((unsigned int)a1, a2 + 1, 2, *v2);
return flipPecas((unsigned int)(a1 - 1), a2, 3, *v2);
}

canonical_decompiled_code:
char __fastcall func15(__int64 a1, unsigned int a2)
{
unsigned __int8 *v2; // r12

v2 = (unsigned __int8 *)&tabuleiro[8 * (int)a1 + a2];
func14(a1, a2 - 1, 0, *v2);
func14((unsigned int)(a1 + 1), a2, 1, *v2);
func14((unsigned int)a1, a2 + 1, 2, *v2);
return func14((unsigned int)(a1 - 1), a2, 3, *v2);
}

original_code:
void checkPecaPosta(int i, int j)
{
 flipPecas(i, j - 1, 0, tabuleiro[i][j]);
 flipPecas(i + 1, j, 1, tabuleiro[i][j]);
 flipPecas(i, j + 1, 2, tabuleiro[i][j]);
 flipPecas(i - 1, j, 3, tabuleiro[i][j]);
}

variable_types: i int

variable_types: j int

return_type: void


checkPeca

decompiled_code:
bool __fastcall checkPeca(unsigned int a1, unsigned int a2)
{
bool result; // al

if ( a1 > 7 || a2 > 7 )
result = 0;
else
result = tabuleiro[8 * a1 + a2] != 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func16(unsigned int a1, unsigned int a2)
{
bool result; // al

if ( a1 > 7 || a2 > 7 )
result = 0;
else
result = tabuleiro[8 * a1 + a2] != 0;
return result;
}

original_code:
_Bool checkPeca(int i, int j)
{
 if(i < 0 || i > 7 || j < 0 || j > 7)
  return 0;
 else if(tabuleiro[i][j] == 0x00u)
  return 0;
 return 1;
}


putPeca

decompiled_code:
char __fastcall putPeca(unsigned int a1, unsigned int a2, char a3)
{
bool v4; // r12
bool v5; // r15
bool v6; // r13
char result; // al

v4 = checkPeca(a1, a2 - 1);
v5 = checkPeca(a1, a2 + 1);
v6 = checkPeca(a1 - 1, a2);
result = checkPeca(a1 + 1, a2);
if ( v6 || v5 || v4 || result )
{
tabuleiro[8 * a1 + a2] = a3;
result = checkPecaPosta(a1, a2);
turn ^= 1u;
}
return result;
}

canonical_decompiled_code:
char __fastcall func17(unsigned int a1, unsigned int a2, char a3)
{
bool v4; // r12
bool v5; // r15
bool v6; // r13
char result; // al

v4 = func16(a1, a2 - 1);
v5 = func16(a1, a2 + 1);
v6 = func16(a1 - 1, a2);
result = func16(a1 + 1, a2);
if ( v6 || v5 || v4 || result )
{
tabuleiro[8 * a1 + a2] = a3;
result = func15(a1, a2);
turn ^= 1u;
}
return result;
}

original_code:
void putPeca(int i, int j, byte peca)
{
 _Bool cima = checkPeca(i, j - 1),
      baixo = checkPeca(i, j + 1),
      esquerda = checkPeca(i - 1, j),
      direita = checkPeca(i + 1, j);
 if(cima || baixo || esquerda || direita)
 {
  tabuleiro[i][j] = peca;
  checkPecaPosta(i, j);
  turn = !turn;
 }
}

canonical_original_code:
void putPeca(int i, int j, unsigned char peca)
{
 _Bool cima = checkPeca(i, j - 1); _Bool baixo = checkPeca(i, j + 1); _Bool esquerda = checkPeca(i - 1, j); _Bool direita = checkPeca(i + 1, j);
 if(cima || baixo || esquerda || direita)
 {
  tabuleiro[i][j] = peca;
  checkPecaPosta(i, j);
  turn = !turn;
 }
}

variable_types:
peca
unsigned char


OnIdle

decompiled_code:
__int64 OnIdle()
{
double v0; // xmm1_8
double v1; // xmm0_8
unsigned int v2; // edi
unsigned int v3; // esi
const char *v4; // rsi

*(_QWORD *)&v0 = (unsigned int)mousepressX;
*(_QWORD *)&v1 = (unsigned int)mousepressY;
strcpy(msg, "none");
if ( !oldMouseLButton )
{
if ( MouseLButton )
{
*(float *)&v0 = *(float *)&mousepressX * 4.0;
v2 = (int)(float)(*(float *)&mousepressX * 4.0);
*(float *)&v1 = *(float *)&mousepressY * 4.0;
v3 = (int)(float)(*(float *)&mousepressY * 4.0);
if ( !tabuleiro[8 * v2 + v3] )
{
if ( turn )
putPeca(v2, v3, 2);
else
putPeca(v2, v3, 1);
}
}
}
v4 = "Vez das PECAS BRANCAS";
if ( !turn )
v4 = "Vez das PECAS PRETAS";
sprintf(msg, v4, v1, v0);
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 func18()
{
double v0; // xmm1_8
double v1; // xmm0_8
unsigned int v2; // edi
unsigned int v3; // esi
const char *v4; // rsi

*(_QWORD *)&v0 = (unsigned int)mousepressX;
*(_QWORD *)&v1 = (unsigned int)mousepressY;
strcpy(msg, "none");
if ( !oldMouseLButton )
{
if ( MouseLButton )
{
*(float *)&v0 = *(float *)&mousepressX * 4.0;
v2 = (int)(float)(*(float *)&mousepressX * 4.0);
*(float *)&v1 = *(float *)&mousepressY * 4.0;
v3 = (int)(float)(*(float *)&mousepressY * 4.0);
if ( !tabuleiro[8 * v2 + v3] )
{
if ( turn )
func17(v2, v3, 2);
else
func17(v2, v3, 1);
}
}
}
v4 = "Vez das PECAS BRANCAS";
if ( !turn )
v4 = "Vez das PECAS PRETAS";
sprintf(msg, v4, v1, v0);
return glutPostRedisplay();
}

original_code:
void OnIdle()
{
 int i = (mousepressX / 0.25f);
 int j = (mousepressY / 0.25f);
 sprintf(msg, "none");
 if(!oldMouseLButton && MouseLButton)
 {
  if(tabuleiro[i][j] == 0x00u)
  {
   if(turn == 1)
    putPeca(i, j, 0x02u);
   else putPeca(i, j, 0x01u);
  }
 }
 sprintf(msg, (turn ? "Vez das PECAS BRANCAS" : "Vez das PECAS PRETAS"));
 glutPostRedisplay();
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
glutInit();
glutInitDisplayMode();
glutInitWindowPosition();
vwprt_x = 640;
vwprt_y = 480;
glutInitWindowSize();
glutCreateWindow();
glutIdleFunc();
glutDisplayFunc();
glutReshapeFunc();
glutKeyboardFunc();
glutMouseFunc();
glutPassiveMotionFunc();
byte_404144 = 2;
byte_40413B = 2;
byte_404143 = 1;
byte_40413C = 1;
turn = 0;
strcpy(msg, "Vez das PECAS PRETAS");
oldMouseLButton = 0;
MouseLButton = 0;
glutMainLoop();
return 0;
}

original_code:
int main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(0x0000 | 0x0002);
 glutInitWindowPosition(0, 0);
 vwprt_x = 640; vwprt_y = 480;
 glutInitWindowSize(vwprt_x, vwprt_y);
 glutCreateWindow("Reversi");
 glutIdleFunc(OnIdle);
 glutDisplayFunc(OnDisplay);
 glutReshapeFunc(OnReshape);
 glutKeyboardFunc(OnKeyPress);
 glutMouseFunc(OnMouseClick);
 glutPassiveMotionFunc(OnMouseMove);
 tabuleiro[3][3] = tabuleiro[4][4] = 0x02u;
 tabuleiro[3][4] = tabuleiro[4][3] = 0x01u;
 turn = 0;
 sprintf(msg, "Vez das PECAS PRETAS");
 MouseLButton = oldMouseLButton = 0;
 glutMainLoop();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(0x0000 | 0x0002);
 glutInitWindowPosition(0, 0);
 vwprt_x = 640; vwprt_y = 480;
 glutInitWindowSize(vwprt_x, vwprt_y);
 glutCreateWindow("Reversi");
 glutIdleFunc(OnIdle);
 glutDisplayFunc(OnDisplay);
 glutReshapeFunc(OnReshape);
 glutKeyboardFunc(OnKeyPress);
 glutMouseFunc(OnMouseClick);
 glutPassiveMotionFunc(OnMouseMove);
 tabuleiro[3][3] = tabuleiro[4][4] = 0x02u;
 tabuleiro[3][4] = tabuleiro[4][3] = 0x01u;
 turn = 0;
 sprintf(msg, "Vez das PECAS PRETAS");
 MouseLButton = oldMouseLButton = 0;
 glutMainLoop();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


repo: luksow/Coconut

decompiled_code:
void __fastcall list_del(__int64 a1)
{
_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = (void *) 0;
 entry->prev = (void *) 0;
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
void __fastcall free_event(void *ptr)
{
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 56));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 56));
free(ptr);
}

original_code:
static void free_event(event_t *event)
{
 pthread_mutex_destroy(&event->cond_mutex);
 pthread_cond_destroy(&event->cond);
 free(event);
}

canonical_original_code:
static void free_event(struct event_t *event)
{
 pthread_mutex_destroy(&event->cond_mutex);
 pthread_cond_destroy(&event->cond);
 free(event);
}

variable_types: struct event_t { struct list_head { struct list_head *next; } head; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; char *id; _Bool published; } *

user_defined_types: struct event_t { struct list_head head; union pthread_mutex_t cond_mutex; union pthread_cond_t cond; char *id; _Bool published; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __fastcall publish_event(__int64 a1)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
*(_BYTE *)(a1 + 112) = 1;
pthread_cond_broadcast((pthread_cond_t *)(a1 + 56));
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
*(_BYTE *)(a1 + 112) = 1;
pthread_cond_broadcast((pthread_cond_t *)(a1 + 56));
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
static void publish_event(event_t *event)
{
 pthread_mutex_lock(&event->cond_mutex);
 event->published = 1;
 pthread_cond_broadcast(&event->cond);
 pthread_mutex_unlock(&event->cond_mutex);
}

canonical_original_code:
static void publish_event(struct event_t *event)
{
 pthread_mutex_lock(&event->cond_mutex);
 event->published = 1;
 pthread_cond_broadcast(&event->cond);
 pthread_mutex_unlock(&event->cond_mutex);
}

user_defined_types: struct list_head { struct list_head *next; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
const char *__fastcall find_event(char *s2)
{
const char *v1; // rbx

v1 = (const char *)events_list;
if ( events_list == &events_list )
return 0LL;
while ( strcmp(*((const char **)v1 + 13), s2) )
{
v1 = *(const char **)v1;
if ( v1 == (const char *)&events_list )
return 0LL;
}
return v1;
}

canonical_decompiled_code:
const char *__fastcall func6(char *s2)
{
const char *v1; // rbx

v1 = (const char *)events_list;
if ( events_list == &events_list )
return 0LL;
while ( strcmp(*((const char **)v1 + 13), s2) )
{
v1 = *(const char **)v1;
if ( v1 == (const char *)&events_list )
return 0LL;
}
return v1;
}

original_code:
static event_t *find_event(const char *id)
{
 event_t *event;
 list_t *it;
 for (it = (&events_list.head)->next; it != (&events_list.head); it = it->next)
 {
  event = ((event_t *)((char *)(it)-(unsigned long)(&((event_t *)0)->head)));
  if (strcmp(event->id, id) == 0)
   return event;
 }
 return ((void *)0);
}

canonical_original_code:
static struct event_t *find_event(char *id)
{
 struct event_t *event;
 struct list_head *it;
 for (it = (&events_list.head)->next; it != (&events_list.head); it = it->next)
 {
  event = ((struct event_t *)((char *)(it)-(unsigned long)(&((struct event_t *)0)->head)));
  if (strcmp(event->id, id) == 0)
   return event;
 }
 return ((void *)0);
}

variable_types:
id
char *

return_type: struct event_t { struct list_head { struct list_head *next; } head; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; char *id; _Bool published; } *


decompiled_code:
char *__fastcall create_add_event(__int64 a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x78uLL);
*((_QWORD *)v1 + 13) = a1;
v1[112] = 0;
pthread_cond_init((pthread_cond_t *)(v1 + 56), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v1 + 16), 0LL);
list_add_tail(v1, (__int64)&events_list);
return v1;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 a1)
{
char *v1; // rbx

v1 = (char *)malloc(0x78uLL);
*((_QWORD *)v1 + 13) = a1;
v1[112] = 0;
pthread_cond_init((pthread_cond_t *)(v1 + 56), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v1 + 16), 0LL);
func1(v1, (__int64)&events_list);
return v1;
}

original_code:
static event_t *create_add_event(const char *id)
{
 event_t *event = malloc(sizeof(event_t));
 event->id = id;
 event->published = 0;
 pthread_cond_init(&event->cond, ((void *)0));
 pthread_mutex_init(&event->cond_mutex, ((void *)0));
 list_add_tail(&event->head, &events_list.head);
 return event;
}

canonical_original_code:
static struct event_t *create_add_event(char *id)
{
 struct event_t *event = malloc(sizeof(event_t));
 event->id = id;
 event->published = 0;
 pthread_cond_init(&event->cond, ((void *)0));
 pthread_mutex_init(&event->cond_mutex, ((void *)0));
 list_add_tail(&event->head, &events_list.head);
 return event;
}


decompiled_code:
void free_events_list()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)events_list;
v1 = *(void ***)events_list;
if ( events_list != &events_list )
{
do
{
list_del((__int64)v0);
free_event(v0);
v0 = v1;
v1 = (void **)*v1;
}
while ( v0 != &events_list );
}
}

canonical_decompiled_code:
void func8()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)events_list;
v1 = *(void ***)events_list;
if ( events_list != &events_list )
{
do
{
func3((__int64)v0);
func4(v0);
v0 = v1;
v1 = (void **)*v1;
}
while ( v0 != &events_list );
}
}

original_code:
void free_events_list()
{
 list_t *it, *tmp_it;
 event_t *event;
 for (it = (&events_list.head)->next, tmp_it = it->next; it != (&events_list.head); it = tmp_it, tmp_it = it->next)
 {
  event = ((event_t *)((char *)(it)-(unsigned long)(&((event_t *)0)->head)));
  list_del(it);
  free_event(event);
 }
}

canonical_original_code:
void free_events_list()
{
 struct list_head *it; struct list_head *tmp_it;
 struct event_t *event;
 for (it = (&events_list.head)->next, tmp_it = it->next; it != (&events_list.head); it = tmp_it, tmp_it = it->next)
 {
  event = ((struct event_t *)((char *)(it)-(unsigned long)(&((struct event_t *)0)->head)));
  list_del(it);
  free_event(event);
 }
}


decompiled_code:
int publish_all_events()
{
void **v0; // rbx
int result; // eax

v0 = (void **)events_list;
if ( events_list != &events_list )
{
do
{
result = publish_event((__int64)v0);
v0 = (void **)*v0;
}
while ( v0 != &events_list );
}
return result;
}

canonical_decompiled_code:
int func9()
{
void **v0; // rbx
int result; // eax

v0 = (void **)events_list;
if ( events_list != &events_list )
{
do
{
result = func5((__int64)v0);
v0 = (void **)*v0;
}
while ( v0 != &events_list );
}
return result;
}

original_code:
void publish_all_events()
{
 event_t *event;
 list_t *it;
 for (it = (&events_list.head)->next; it != (&events_list.head); it = it->next)
 {
  event = ((event_t *)((char *)(it)-(unsigned long)(&((event_t *)0)->head)));
  publish_event(event);
 }
}

canonical_original_code:
void publish_all_events()
{
 struct event_t *event;
 struct list_head *it;
 for (it = (&events_list.head)->next; it != (&events_list.head); it = it->next)
 {
  event = ((struct event_t *)((char *)(it)-(unsigned long)(&((struct event_t *)0)->head)));
  publish_event(event);
 }
}


decompiled_code:
__int64 __fastcall c_is_event_published(char *s2)
{
__int64 result; // rax
const char *v2; // rbx

result = running;
if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v2 = find_event(s2);
pthread_mutex_unlock(&events_list_mutex);
if ( v2 )
result = *((unsigned __int8 *)v2 + 112);
else
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *s2)
{
__int64 result; // rax
const char *v2; // rbx

result = running;
if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v2 = func6(s2);
pthread_mutex_unlock(&events_list_mutex);
if ( v2 )
result = *((unsigned __int8 *)v2 + 112);
else
result = 0LL;
}
return result;
}

original_code:
_Bool c_is_event_published(const char *id)
{
 event_t *event;
 if (!running)
  return 0;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 pthread_mutex_unlock(&events_list_mutex);
 if (!event)
  return 0;
 return event->published == 1;
}

canonical_original_code:
_Bool c_is_event_published(char *id)
{
 struct event_t *event;
 if (!running)
  return 0;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 pthread_mutex_unlock(&events_list_mutex);
 if (!event)
  return 0;
 return event->published == 1;
}


decompiled_code:
void __fastcall c_wait_event(char *s2)
{
char *v1; // rbx

if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v1 = (char *)find_event(s2);
if ( !v1 )
v1 = create_add_event((__int64)s2);
pthread_mutex_unlock(&events_list_mutex);
mark_self_blocked();
pthread_mutex_lock((pthread_mutex_t *)(v1 + 16));
while ( !v1[112] )
pthread_cond_wait((pthread_cond_t *)(v1 + 56), (pthread_mutex_t *)(v1 + 16));
pthread_mutex_unlock((pthread_mutex_t *)(v1 + 16));
mark_self_unblocked();
}
}

canonical_decompiled_code:
void __fastcall func11(char *s2)
{
char *v1; // rbx

if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v1 = (char *)func6(s2);
if ( !v1 )
v1 = func7((__int64)s2);
pthread_mutex_unlock(&events_list_mutex);
mark_self_blocked();
pthread_mutex_lock((pthread_mutex_t *)(v1 + 16));
while ( !v1[112] )
pthread_cond_wait((pthread_cond_t *)(v1 + 56), (pthread_mutex_t *)(v1 + 16));
pthread_mutex_unlock((pthread_mutex_t *)(v1 + 16));
mark_self_unblocked();
}
}

original_code:
void c_wait_event(const char *id)
{
 event_t *event;
 if (!running)
  return;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 if (!event)
  event = create_add_event(id);
 pthread_mutex_unlock(&events_list_mutex);
 mark_self_blocked();
 pthread_mutex_lock(&event->cond_mutex);
 while (!event->published)
  pthread_cond_wait(&event->cond, &event->cond_mutex);
 pthread_mutex_unlock(&event->cond_mutex);
 mark_self_unblocked();
}

canonical_original_code:
void c_wait_event(char *id)
{
 struct event_t *event;
 if (!running)
  return;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 if (!event)
  event = create_add_event(id);
 pthread_mutex_unlock(&events_list_mutex);
 mark_self_blocked();
 pthread_mutex_lock(&event->cond_mutex);
 while (!event->published)
  pthread_cond_wait(&event->cond, &event->cond_mutex);
 pthread_mutex_unlock(&event->cond_mutex);
 mark_self_unblocked();
}


decompiled_code:
void __fastcall c_publish_event(char *s2)
{
_BYTE *v1; // rax

if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v1 = find_event(s2);
if ( !v1 )
v1 = create_add_event((__int64)s2);
if ( !v1[112] )
publish_event((__int64)v1);
pthread_mutex_unlock(&events_list_mutex);
}
}

canonical_decompiled_code:
void __fastcall func12(char *s2)
{
_BYTE *v1; // rax

if ( running )
{
pthread_mutex_lock(&events_list_mutex);
v1 = func6(s2);
if ( !v1 )
v1 = func7((__int64)s2);
if ( !v1[112] )
func5((__int64)v1);
pthread_mutex_unlock(&events_list_mutex);
}
}

original_code:
void c_publish_event(const char *id)
{
 event_t *event;
 if (!running)
  return;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 if (!event)
  event = create_add_event(id);
 if (!event->published)
  publish_event(event);
 pthread_mutex_unlock(&events_list_mutex);
}

canonical_original_code:
void c_publish_event(char *id)
{
 struct event_t *event;
 if (!running)
  return;
 pthread_mutex_lock(&events_list_mutex);
 event = find_event(id);
 if (!event)
  event = create_add_event(id);
 if (!event->published)
  publish_event(event);
 pthread_mutex_unlock(&events_list_mutex);
}


decompiled_code:
void __fastcall free_tokenized(void *ptr)
{
void *v2; // rdi
char *v3; // rbx

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
{
v3 = (char *)ptr + 8;
do
{
free(v2);
v3 += 8;
v2 = (void *)*((_QWORD *)v3 - 1);
}
while ( v2 );
}
free(ptr);
}
}

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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
{
v3 = (char *)ptr + 8;
do
{
free(v2);
v3 += 8;
v2 = (void *)*((_QWORD *)v3 - 1);
}
while ( v2 );
}
free(ptr);
}
}

original_code:
void free_tokenized(char *array[])
{
 int i;
 if (!array)
  return;
 for (i = 0; array[i]; ++i)
  free(array[i]);
 free(array);
}

variable_types:
array
char *[]


canonical_original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = (void *) 0;
 entry->prev = (void *) 0;
}


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

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

original_code:
static void free_thread(thread_t *thread)
{
 free(thread);
}

canonical_original_code:
static void free_thread(struct thread_t *thread)
{
 free(thread);
}

variable_types: struct thread_t { struct list_head { struct list_head *next; } head; unsigned long id; _Bool blocked; } *

user_defined_types: struct thread_t { struct list_head head; unsigned long id; _Bool blocked; }


decompiled_code:
void free_threads_list()
{
__int64 *v0; // rbx
__int64 *i; // rbp

v0 = (__int64 *)threads_list;
for ( i = *(__int64 **)threads_list; v0 != &threads_list; i = (__int64 *)*i )
{
list_del((__int64)v0);
free_thread(v0);
v0 = i;
}
}

canonical_decompiled_code:
void func6()
{
__int64 *v0; // rbx
__int64 *i; // rbp

v0 = (__int64 *)threads_list;
for ( i = *(__int64 **)threads_list; v0 != &threads_list; i = (__int64 *)*i )
{
func3((__int64)v0);
func4(v0);
v0 = i;
}
}

original_code:
void free_threads_list()
{
 list_t *it, *tmp_it;
 thread_t *tmp;
 for (it = (&threads_list.head)->next, tmp_it = it->next; it != (&threads_list.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((thread_t *)((char *)(it)-(unsigned long)(&((thread_t *)0)->head)));
  list_del(it);
  free_thread(tmp);
 }
}

canonical_original_code:
void free_threads_list()
{
 struct list_head *it; struct list_head *tmp_it;
 struct thread_t *tmp;
 for (it = (&threads_list.head)->next, tmp_it = it->next; it != (&threads_list.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((struct thread_t *)((char *)(it)-(unsigned long)(&((struct thread_t *)0)->head)));
  list_del(it);
  free_thread(tmp);
 }
}


decompiled_code:
__int64 __fastcall find_thread(pthread_t thread2)
{
__int64 v1; // rbx

v1 = threads_list;
if ( (__int64 *)threads_list == &threads_list )
return 0LL;
while ( !pthread_equal(*(_QWORD *)(v1 + 16), thread2) )
{
v1 = *(_QWORD *)v1;
if ( (__int64 *)v1 == &threads_list )
return 0LL;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(pthread_t thread2)
{
__int64 v1; // rbx

v1 = threads_list;
if ( (__int64 *)threads_list == &threads_list )
return 0LL;
while ( !pthread_equal(*(_QWORD *)(v1 + 16), thread2) )
{
v1 = *(_QWORD *)v1;
if ( (__int64 *)v1 == &threads_list )
return 0LL;
}
return v1;
}

original_code:
thread_t *find_thread(const pthread_t id)
{
 thread_t *thread;
 list_t *it;
 for (it = (&threads_list.head)->next; it != (&threads_list.head); it = it->next)
 {
  thread = ((thread_t *)((char *)(it)-(unsigned long)(&((thread_t *)0)->head)));
  if (pthread_equal(thread->id, id))
   return thread;
 }
 return ((void *)0);
}

canonical_original_code:
struct thread_t *find_thread(unsigned long id)
{
 struct thread_t *thread;
 struct list_head *it;
 for (it = (&threads_list.head)->next; it != (&threads_list.head); it = it->next)
 {
  thread = ((struct thread_t *)((char *)(it)-(unsigned long)(&((struct thread_t *)0)->head)));
  if (pthread_equal(thread->id, id))
   return thread;
 }
 return ((void *)0);
}

variable_types:
id
unsigned long

return_type: struct thread_t { struct list_head { struct list_head *next; } head; unsigned long id; _Bool blocked; } *


decompiled_code:
__int64 check_liveness()
{
__int64 v0; // rbx
unsigned int v1; // ebp
unsigned int v2; // eax

v0 = threads_list;
if ( (__int64 *)threads_list != &threads_list )
{
v1 = 1;
while ( 1 )
{
LOBYTE(v2) = is_thread_alive(v0);
if ( (_BYTE)v2 )
{
if ( !*(_BYTE *)(v0 + 24) )
return v2;
v1 = 0;
}
v0 = *(_QWORD *)v0;
if ( (__int64 *)v0 == &threads_list )
return v1;
}
}
return 1;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rbx
unsigned int v1; // ebp
unsigned int v2; // eax

v0 = threads_list;
if ( (__int64 *)threads_list != &threads_list )
{
v1 = 1;
while ( 1 )
{
LOBYTE(v2) = func5(v0);
if ( (_BYTE)v2 )
{
if ( !*(_BYTE *)(v0 + 24) )
return v2;
v1 = 0;
}
v0 = *(_QWORD *)v0;
if ( (__int64 *)v0 == &threads_list )
return v1;
}
}
return 1;
}

original_code:
_Bool check_liveness()
{
 _Bool all_dead = 1;
 thread_t *thread;
 list_t *it;
 for (it = (&threads_list.head)->next; it != (&threads_list.head); it = it->next)
 {
  thread = ((thread_t *)((char *)(it)-(unsigned long)(&((thread_t *)0)->head)));
  if (is_thread_alive(thread))
  {
   all_dead = 0;
   if (!thread->blocked)
    return 1;
  }
 }
 return all_dead;
}

canonical_original_code:
_Bool check_liveness()
{
 _Bool all_dead = 1;
 struct thread_t *thread;
 struct list_head *it;
 for (it = (&threads_list.head)->next; it != (&threads_list.head); it = it->next)
 {
  thread = ((struct thread_t *)((char *)(it)-(unsigned long)(&((struct thread_t *)0)->head)));
  if (is_thread_alive(thread))
  {
   all_dead = 0;
   if (!thread->blocked)
    return 1;
  }
 }
 return all_dead;
}


decompiled_code:
int mark_self_blocked()
{
pthread_t v0; // rbx
_QWORD *v1; // rax

_InterlockedAdd64(&blocked_counter, 1uLL);
pthread_mutex_lock(&threads_list_mutex);
v0 = pthread_self();
v1 = (_QWORD *)find_thread(v0);
if ( !v1 )
v1 = create_add_thread(v0);
*((_BYTE *)v1 + 24) = 1;
return pthread_mutex_unlock(&threads_list_mutex);
}

canonical_decompiled_code:
int func9()
{
pthread_t v0; // rbx
_QWORD *v1; // rax

_InterlockedAdd64(&blocked_counter, 1uLL);
pthread_mutex_lock(&threads_list_mutex);
v0 = pthread_self();
v1 = (_QWORD *)func7(v0);
if ( !v1 )
v1 = create_add_thread(v0);
*((_BYTE *)v1 + 24) = 1;
return pthread_mutex_unlock(&threads_list_mutex);
}

original_code:
void mark_self_blocked()
{
 thread_t *thread;
 __sync_fetch_and_add(&blocked_counter, 1);
 pthread_mutex_lock(&threads_list_mutex);
 thread = find_thread(pthread_self());
 if (thread == ((void *)0))
  thread = create_add_thread(pthread_self());
 thread->blocked = 1;
 pthread_mutex_unlock(&threads_list_mutex);
}

canonical_original_code:
void mark_self_blocked()
{
 struct thread_t *thread;
 __sync_fetch_and_add(&blocked_counter, 1);
 pthread_mutex_lock(&threads_list_mutex);
 thread = find_thread(pthread_self());
 if (thread == ((void *)0))
  thread = create_add_thread(pthread_self());
 thread->blocked = 1;
 pthread_mutex_unlock(&threads_list_mutex);
}


decompiled_code:
int mark_self_unblocked()
{
pthread_t v0; // rax

_InterlockedAdd64(&blocked_counter, 1uLL);
pthread_mutex_lock(&threads_list_mutex);
v0 = pthread_self();
*(_BYTE *)(find_thread(v0) + 24) = 0;
return pthread_mutex_unlock(&threads_list_mutex);
}

canonical_decompiled_code:
int func10()
{
pthread_t v0; // rax

_InterlockedAdd64(&blocked_counter, 1uLL);
pthread_mutex_lock(&threads_list_mutex);
v0 = pthread_self();
*(_BYTE *)(func7(v0) + 24) = 0;
return pthread_mutex_unlock(&threads_list_mutex);
}

original_code:
void mark_self_unblocked()
{
 thread_t *thread;
 __sync_fetch_and_add(&blocked_counter, 1);
 pthread_mutex_lock(&threads_list_mutex);
 thread = find_thread(pthread_self());
 thread->blocked = 0;
 pthread_mutex_unlock(&threads_list_mutex);
}

canonical_original_code:
void mark_self_unblocked()
{
 struct thread_t *thread;
 __sync_fetch_and_add(&blocked_counter, 1);
 pthread_mutex_lock(&threads_list_mutex);
 thread = find_thread(pthread_self());
 thread->blocked = 0;
 pthread_mutex_unlock(&threads_list_mutex);
}


decompiled_code:
void __fastcall free_block(void *ptr)
{
_QWORD *v1; // rbx
_QWORD *i; // rbp

v1 = (_QWORD *)*((_QWORD *)ptr + 3);
for ( i = (_QWORD *)*v1; (char *)ptr + 24 != (char *)v1; i = (_QWORD *)*i )
{
list_del((__int64)v1);
free(v1);
v1 = i;
}
pthread_cond_destroy((pthread_cond_t *)ptr + 1);
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 96));
free(*((void **)ptr + 17));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
_QWORD *v1; // rbx
_QWORD *i; // rbp

v1 = (_QWORD *)*((_QWORD *)ptr + 3);
for ( i = (_QWORD *)*v1; (char *)ptr + 24 != (char *)v1; i = (_QWORD *)*i )
{
func3((__int64)v1);
free(v1);
v1 = i;
}
pthread_cond_destroy((pthread_cond_t *)ptr + 1);
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 96));
free(*((void **)ptr + 17));
free(ptr);
}

original_code:
static void free_block(block_t *block)
{
 list_t *it, *tmp_it;
 waiting_t *tmp;
 for (it = (&block->preds.head)->next, tmp_it = it->next; it != (&block->preds.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((waiting_t *)((char *)(it)-(unsigned long)(&((waiting_t *)0)->head)));
  list_del(it);
  free(tmp);
 }
 pthread_cond_destroy(&block->cond);
 pthread_mutex_destroy(&block->cond_mutex);
 free(block->id);
 free(block);
}

canonical_original_code:
static void free_block(struct block *block)
{
 struct list_head *it; struct list_head *tmp_it;
 struct waiting_t *tmp;
 for (it = (&block->preds.head)->next, tmp_it = it->next; it != (&block->preds.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((struct waiting_t *)((char *)(it)-(unsigned long)(&((struct waiting_t *)0)->head)));
  list_del(it);
  free(tmp);
 }
 pthread_cond_destroy(&block->cond);
 pthread_mutex_destroy(&block->cond_mutex);
 free(block->id);
 free(block);
}

variable_types: struct block { struct list_head { struct list_head *next; } head; unsigned long owner; struct waiting_t { struct list_head head; struct block *block; } preds; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; char *id; enum BLOCK_STATE { CREATED=0, ENABLED=1, STARTED=2, FINISHED=3 } state; unsigned long counter; } *

variable_types: struct waiting_t { struct list_head { struct list_head *next; } head; struct block { struct list_head head; unsigned long owner; struct waiting_t preds; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; char *id; enum BLOCK_STATE { CREATED=0, ENABLED=1, STARTED=2, FINISHED=3 } state; unsigned long counter; } *block; } *

user_defined_types: struct block { struct list_head head; unsigned long owner; struct waiting_t preds; union pthread_cond_t cond; union pthread_mutex_t cond_mutex; char *id; enum BLOCK_STATE state; unsigned long counter; }


decompiled_code:
int __fastcall finish_block(__int64 a1)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 96));
*(_DWORD *)(a1 + 144) = 3;
pthread_cond_broadcast((pthread_cond_t *)(a1 + 48));
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 96));
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 96));
*(_DWORD *)(a1 + 144) = 3;
pthread_cond_broadcast((pthread_cond_t *)(a1 + 48));
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 96));
}

original_code:
static void finish_block(block_t *block)
{
 pthread_mutex_lock(&block->cond_mutex);
 block->state = FINISHED;
 pthread_cond_broadcast(&block->cond);
 pthread_mutex_unlock(&block->cond_mutex);
}

canonical_original_code:
static void finish_block(struct block *block)
{
 pthread_mutex_lock(&block->cond_mutex);
 block->state = FINISHED;
 pthread_cond_broadcast(&block->cond);
 pthread_mutex_unlock(&block->cond_mutex);
}

user_defined_types: struct waiting_t { struct list_head head; struct block *block; }

user_defined_types: enum BLOCK_STATE { CREATED=0, ENABLED=1, STARTED=2, FINISHED=3 }


decompiled_code:
const char *__fastcall find_block(char *s2)
{
const char *v1; // rbx

v1 = (const char *)blocks_list;
if ( blocks_list == &blocks_list )
return 0LL;
while ( strcmp(*((const char **)v1 + 17), s2) )
{
v1 = *(const char **)v1;
if ( v1 == (const char *)&blocks_list )
return 0LL;
}
return v1;
}

canonical_decompiled_code:
const char *__fastcall func6(char *s2)
{
const char *v1; // rbx

v1 = (const char *)blocks_list;
if ( blocks_list == &blocks_list )
return 0LL;
while ( strcmp(*((const char **)v1 + 17), s2) )
{
v1 = *(const char **)v1;
if ( v1 == (const char *)&blocks_list )
return 0LL;
}
return v1;
}

original_code:
static block_t *find_block(const char *id)
{
 block_t *block;
 list_t *it;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  block = ((block_t *)((char *)(it)-(unsigned long)(&((block_t *)0)->head)));
  if (strcmp(block->id, id) == 0)
   return block;
 }
 return ((void *)0);
}

canonical_original_code:
static struct block *find_block(char *id)
{
 struct block *block;
 struct list_head *it;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  block = ((struct block *)((char *)(it)-(unsigned long)(&((struct block *)0)->head)));
  if (strcmp(block->id, id) == 0)
   return block;
 }
 return ((void *)0);
}

return_type: struct block { struct list_head { struct list_head *next; } head; unsigned long owner; struct waiting_t { struct list_head head; struct block *block; } preds; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; char *id; enum BLOCK_STATE { CREATED=0, ENABLED=1, STARTED=2, FINISHED=3 } state; unsigned long counter; } *


decompiled_code:
void free_blocks_list()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)blocks_list;
v1 = *(void ***)blocks_list;
if ( blocks_list != &blocks_list )
{
do
{
list_del((__int64)v0);
free_block(v0);
v0 = v1;
v1 = (void **)*v1;
}
while ( v0 != &blocks_list );
}
}

canonical_decompiled_code:
void func8()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)blocks_list;
v1 = *(void ***)blocks_list;
if ( blocks_list != &blocks_list )
{
do
{
func3((__int64)v0);
func4(v0);
v0 = v1;
v1 = (void **)*v1;
}
while ( v0 != &blocks_list );
}
}

original_code:
void free_blocks_list()
{
 list_t *it, *tmp_it;
 block_t *tmp;
 for (it = (&blocks_list.head)->next, tmp_it = it->next; it != (&blocks_list.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((block_t *)((char *)(it)-(unsigned long)(&((block_t *)0)->head)));
  list_del(it);
  free_block(tmp);
 }
}

canonical_original_code:
void free_blocks_list()
{
 struct list_head *it; struct list_head *tmp_it;
 struct block *tmp;
 for (it = (&blocks_list.head)->next, tmp_it = it->next; it != (&blocks_list.head); it = tmp_it, tmp_it = it->next)
 {
  tmp = ((struct block *)((char *)(it)-(unsigned long)(&((struct block *)0)->head)));
  list_del(it);
  free_block(tmp);
 }
}


decompiled_code:
int finish_all_blocks()
{
void **v0; // rbx
int result; // eax

v0 = (void **)blocks_list;
if ( blocks_list != &blocks_list )
{
do
{
result = finish_block((__int64)v0);
v0 = (void **)*v0;
}
while ( v0 != &blocks_list );
}
return result;
}

canonical_decompiled_code:
int func9()
{
void **v0; // rbx
int result; // eax

v0 = (void **)blocks_list;
if ( blocks_list != &blocks_list )
{
do
{
result = func5((__int64)v0);
v0 = (void **)*v0;
}
while ( v0 != &blocks_list );
}
return result;
}

original_code:
void finish_all_blocks()
{
 block_t *block;
 list_t *it;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  block = ((block_t *)((char *)(it)-(unsigned long)(&((block_t *)0)->head)));
  finish_block(block);
 }
}

canonical_original_code:
void finish_all_blocks()
{
 struct block *block;
 struct list_head *it;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  block = ((struct block *)((char *)(it)-(unsigned long)(&((struct block *)0)->head)));
  finish_block(block);
 }
}


decompiled_code:
__int64 __fastcall c_set_blocks_interleaving(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // r14
__int64 v3; // rdi
_QWORD *v4; // r13
char **v5; // rbp
char **v6; // rbx
char *v7; // rax
char **v8; // r12

result = get_tokenized(a1, ";");
if ( running )
{
v2 = (__int64 *)result;
free_blocks_list();
v3 = *v2;
if ( *v2 )
{
v4 = v2 + 1;
v5 = 0LL;
do
{
v8 = v5;
v5 = (char **)get_tokenized(v3, ",");
v7 = *v5;
if ( *v5 )
{
v6 = v5;
do
{
if ( *v7 )
{
pthread_mutex_lock(&blocks_list_mutex);
create_add_block(*v6, v8);
pthread_mutex_unlock(&blocks_list_mutex);
}
v7 = *++v6;
}
while ( *v6 );
}
free_tokenized(v8);
v3 = *v4++;
}
while ( v3 );
}
else
{
v5 = 0LL;
}
free_tokenized(v5);
result = free_tokenized(v2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // r14
__int64 v3; // rdi
_QWORD *v4; // r13
char **v5; // rbp
char **v6; // rbx
char *v7; // rax
char **v8; // r12

result = get_tokenized(a1, ";");
if ( running )
{
v2 = (__int64 *)result;
func8();
v3 = *v2;
if ( *v2 )
{
v4 = v2 + 1;
v5 = 0LL;
do
{
v8 = v5;
v5 = (char **)get_tokenized(v3, ",");
v7 = *v5;
if ( *v5 )
{
v6 = v5;
do
{
if ( *v7 )
{
pthread_mutex_lock(&blocks_list_mutex);
func7(*v6, v8);
pthread_mutex_unlock(&blocks_list_mutex);
}
v7 = *++v6;
}
while ( *v6 );
}
free_tokenized(v8);
v3 = *v4++;
}
while ( v3 );
}
else
{
v5 = 0LL;
}
free_tokenized(v5);
result = free_tokenized(v2);
}
return result;
}

original_code:
void c_set_blocks_interleaving(const char *interleaving)
{
 int i;
 int j;
 char **groups = get_tokenized(interleaving, ";");
 char **prev_elems = ((void *)0);
 char **elems;
 if (!running)
  return;
 free_blocks_list();
 for (i = 0; groups[i]; ++i)
 {
  elems = get_tokenized(groups[i], ",");
  for (j = 0; elems[j]; ++j)
  {
   if (strlen(elems[j]) == 0)
    continue;
   pthread_mutex_lock(&blocks_list_mutex);
   create_add_block(elems[j], prev_elems);
   pthread_mutex_unlock(&blocks_list_mutex);
  }
  free_tokenized(prev_elems);
  prev_elems = elems;
 }
 free_tokenized(prev_elems);
 free_tokenized(groups);
}

canonical_original_code:
void c_set_blocks_interleaving(char *interleaving)
{
 int i;
 int j;
 char **groups = get_tokenized(interleaving, ";");
 char **prev_elems = ((void *)0);
 char **elems;
 if (!running)
  return;
 free_blocks_list();
 for (i = 0; groups[i]; ++i)
 {
  elems = get_tokenized(groups[i], ",");
  for (j = 0; elems[j]; ++j)
  {
   if (strlen(elems[j]) == 0)
    continue;
   pthread_mutex_lock(&blocks_list_mutex);
   create_add_block(elems[j], prev_elems);
   pthread_mutex_unlock(&blocks_list_mutex);
  }
  free_tokenized(prev_elems);
  prev_elems = elems;
 }
 free_tokenized(prev_elems);
 free_tokenized(groups);
}

variable_types:
interleaving
char *

variable_types:
groups
char **

variable_types:
elems
char **


decompiled_code:
void __fastcall c_begin_block(char *s2)
{
const char *v1; // rax
const char *v2; // rbp
const char *i; // rbx
__int64 j; // rdi

if ( running )
{
pthread_mutex_lock(&blocks_list_mutex);
v1 = find_block(s2);
v2 = v1;
if ( v1 )
{
if ( *((_DWORD *)v1 + 36) )
{
c_output("Block %s already owned by other thread. Possible malfunctions.\n", s2);
pthread_mutex_unlock(&blocks_list_mutex);
}
else
{
*((_DWORD *)v1 + 36) = 1;
*((_QWORD *)v1 + 2) = pthread_self();
*((_QWORD *)v2 + 19) = ++block_counter;
pthread_mutex_unlock(&blocks_list_mutex);
mark_self_blocked();
for ( i = (const char *)*((_QWORD *)v2 + 3); i != v2 + 24; i = *(const char **)i )
{
pthread_mutex_lock((pthread_mutex_t *)(*((_QWORD *)i + 2) + 96LL));
for ( j = *((_QWORD *)i + 2); *(_DWORD *)(j + 144) != 3; j = *((_QWORD *)i + 2) )
pthread_cond_wait((pthread_cond_t *)(j + 48), (pthread_mutex_t *)(j + 96));
pthread_mutex_unlock((pthread_mutex_t *)(j + 96));
}
*((_DWORD *)v2 + 36) = 2;
mark_self_unblocked();
}
}
else
{
c_output("Block %s to begin not found in interleaving list. Possible malfunctions.\n", s2);
pthread_mutex_unlock(&blocks_list_mutex);
}
}
}

canonical_decompiled_code:
void __fastcall func11(char *s2)
{
const char *v1; // rax
const char *v2; // rbp
const char *i; // rbx
__int64 j; // rdi

if ( running )
{
pthread_mutex_lock(&blocks_list_mutex);
v1 = func6(s2);
v2 = v1;
if ( v1 )
{
if ( *((_DWORD *)v1 + 36) )
{
c_output("Block %s already owned by other thread. Possible malfunctions.\n", s2);
pthread_mutex_unlock(&blocks_list_mutex);
}
else
{
*((_DWORD *)v1 + 36) = 1;
*((_QWORD *)v1 + 2) = pthread_self();
*((_QWORD *)v2 + 19) = ++block_counter;
pthread_mutex_unlock(&blocks_list_mutex);
mark_self_blocked();
for ( i = (const char *)*((_QWORD *)v2 + 3); i != v2 + 24; i = *(const char **)i )
{
pthread_mutex_lock((pthread_mutex_t *)(*((_QWORD *)i + 2) + 96LL));
for ( j = *((_QWORD *)i + 2); *(_DWORD *)(j + 144) != 3; j = *((_QWORD *)i + 2) )
pthread_cond_wait((pthread_cond_t *)(j + 48), (pthread_mutex_t *)(j + 96));
pthread_mutex_unlock((pthread_mutex_t *)(j + 96));
}
*((_DWORD *)v2 + 36) = 2;
mark_self_unblocked();
}
}
else
{
c_output("Block %s to begin not found in interleaving list. Possible malfunctions.\n", s2);
pthread_mutex_unlock(&blocks_list_mutex);
}
}
}

original_code:
void c_begin_block(const char *id)
{
 block_t *block;
 list_t *it;
 waiting_t *tmp;
 if (!running)
  return;
 pthread_mutex_lock(&blocks_list_mutex);
 block = find_block(id);
 if (!block)
 {
  c_output("Block %s to begin not found in interleaving list. Possible malfunctions.\n", id);
  pthread_mutex_unlock(&blocks_list_mutex);
  return;
 }
 if (block->state != CREATED)
 {
  c_output("Block %s already owned by other thread. Possible malfunctions.\n", id);
  pthread_mutex_unlock(&blocks_list_mutex);
  return;
 }
 block->state = ENABLED;
 block->owner = pthread_self();
 block->counter = ++block_counter;
 pthread_mutex_unlock(&blocks_list_mutex);
 mark_self_blocked();
 for (it = (&block->preds.head)->next; it != (&block->preds.head); it = it->next)
 {
  tmp = ((waiting_t *)((char *)(it)-(unsigned long)(&((waiting_t *)0)->head)));
  pthread_mutex_lock(&tmp->block->cond_mutex);
  while (tmp->block->state != FINISHED)
   pthread_cond_wait(&tmp->block->cond, &tmp->block->cond_mutex);
  pthread_mutex_unlock(&tmp->block->cond_mutex);
 }
 block->state = STARTED;
 mark_self_unblocked();
}

canonical_original_code:
void c_begin_block(char *id)
{
 struct block *block;
 struct list_head *it;
 struct waiting_t *tmp;
 if (!running)
  return;
 pthread_mutex_lock(&blocks_list_mutex);
 block = find_block(id);
 if (!block)
 {
  c_output("Block %s to begin not found in interleaving list. Possible malfunctions.\n", id);
  pthread_mutex_unlock(&blocks_list_mutex);
  return;
 }
 if (block->state != CREATED)
 {
  c_output("Block %s already owned by other thread. Possible malfunctions.\n", id);
  pthread_mutex_unlock(&blocks_list_mutex);
  return;
 }
 block->state = ENABLED;
 block->owner = pthread_self();
 block->counter = ++block_counter;
 pthread_mutex_unlock(&blocks_list_mutex);
 mark_self_blocked();
 for (it = (&block->preds.head)->next; it != (&block->preds.head); it = it->next)
 {
  tmp = ((struct waiting_t *)((char *)(it)-(unsigned long)(&((struct waiting_t *)0)->head)));
  pthread_mutex_lock(&tmp->block->cond_mutex);
  while (tmp->block->state != FINISHED)
   pthread_cond_wait(&tmp->block->cond, &tmp->block->cond_mutex);
  pthread_mutex_unlock(&tmp->block->cond_mutex);
 }
 block->state = STARTED;
 mark_self_unblocked();
}


decompiled_code:
void c_end_block()
{
void **v0; // rbx
unsigned __int64 v1; // r12
__int64 v2; // rbp
pthread_t v3; // rsi

if ( running )
{
v0 = (void **)blocks_list;
if ( blocks_list == &blocks_list )
goto LABEL_12;
v1 = -1LL;
v2 = 0LL;
do
{
if ( *((_DWORD *)v0 + 36) == 2 )
{
v3 = pthread_self();
if ( pthread_equal((pthread_t)v0[2], v3) )
{
if ( (unsigned __int64)v0[19] < v1 )
{
v1 = (unsigned __int64)v0[19];
v2 = (__int64)v0;
}
}
}
v0 = (void **)*v0;
}
while ( v0 != &blocks_list );
if ( !v2 )
LABEL_12:
c_output("No begin block for end block. Skipping...\n");
else
finish_block(v2);
}
}

canonical_decompiled_code:
void func12()
{
void **v0; // rbx
unsigned __int64 v1; // r12
__int64 v2; // rbp
pthread_t v3; // rsi

if ( running )
{
v0 = (void **)blocks_list;
if ( blocks_list == &blocks_list )
goto LABEL_12;
v1 = -1LL;
v2 = 0LL;
do
{
if ( *((_DWORD *)v0 + 36) == 2 )
{
v3 = pthread_self();
if ( pthread_equal((pthread_t)v0[2], v3) )
{
if ( (unsigned __int64)v0[19] < v1 )
{
v1 = (unsigned __int64)v0[19];
v2 = (__int64)v0;
}
}
}
v0 = (void **)*v0;
}
while ( v0 != &blocks_list );
if ( !v2 )
LABEL_12:
c_output("No begin block for end block. Skipping...\n");
else
func5(v2);
}
}

original_code:
void c_end_block()
{
 block_t *block = ((void *)0);
 block_t *tmp;
 list_t *it;
 unsigned long min_counter = (0x7fffffffffffffffL * 2UL + 1UL);
 if (!running)
  return;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  tmp = ((block_t *)((char *)(it)-(unsigned long)(&((block_t *)0)->head)));
  if (tmp->state == STARTED && pthread_equal(tmp->owner, pthread_self()) && tmp->counter < min_counter)
  {
   min_counter = tmp->counter;
   block = tmp;
  }
 }
 if (!block)
 {
  c_output("No begin block for end block. Skipping...\n");
  return;
 }
 finish_block(block);
}

canonical_original_code:
void c_end_block()
{
 struct block *block = ((void *)0);
 struct block *tmp;
 struct list_head *it;
 unsigned long min_counter = (0x7fffffffffffffffL * 2UL + 1UL);
 if (!running)
  return;
 for (it = (&blocks_list.head)->next; it != (&blocks_list.head); it = it->next)
 {
  tmp = ((struct block *)((char *)(it)-(unsigned long)(&((struct block *)0)->head)));
  if (tmp->state == STARTED && pthread_equal(tmp->owner, pthread_self()) && tmp->counter < min_counter)
  {
   min_counter = tmp->counter;
   block = tmp;
  }
 }
 if (!block)
 {
  c_output("No begin block for end block. Skipping...\n");
  return;
 }
 finish_block(block);
}


decompiled_code:
_BOOL8 __fastcall c_is_before_block(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = find_block(a1);
v2 = 1;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) <= 1u;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = func6(a1);
v2 = 1;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) <= 1u;
return v2;
}

original_code:
_Bool c_is_before_block(const char *id)
{
 block_t *block = find_block(id);
 return !block || block->state == CREATED || block->state == ENABLED;
}

canonical_original_code:
_Bool c_is_before_block(char *id)
{
 struct block *block = find_block(id);
 return !block || block->state == CREATED || block->state == ENABLED;
}


decompiled_code:
_BOOL8 __fastcall c_is_during_block(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = find_block(a1);
v2 = 0;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) == 2;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = func6(a1);
v2 = 0;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) == 2;
return v2;
}

original_code:
_Bool c_is_during_block(const char *id)
{
 block_t *block = find_block(id);
 return block && block->state == STARTED;
}

canonical_original_code:
_Bool c_is_during_block(char *id)
{
 struct block *block = find_block(id);
 return block && block->state == STARTED;
}


decompiled_code:
_BOOL8 __fastcall c_is_after_block(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = find_block(a1);
v2 = 0;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) == 3;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(char *a1)
{
const char *v1; // rdx
bool v2; // al

v1 = func6(a1);
v2 = 0;
if ( v1 )
v2 = *((_DWORD *)v1 + 36) == 3;
return v2;
}

original_code:
_Bool c_is_after_block(const char *id)
{
 block_t *block = find_block(id);
 return block && block->state == FINISHED;
}

canonical_original_code:
_Bool c_is_after_block(char *id)
{
 struct block *block = find_block(id);
 return block && block->state == FINISHED;
}


decompiled_code:
__int64 __fastcall c_begin_block_bool(char *a1, unsigned int a2)
{
c_begin_block(a1);
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func16(char *a1, unsigned int a2)
{
func11(a1);
return a2;
}

original_code:
_Bool c_begin_block_bool(const char *block, _Bool cond)
{
 c_begin_block(block);
 return cond;
}

canonical_original_code:
_Bool c_begin_block_bool(char *block, _Bool cond)
{
 c_begin_block(block);
 return cond;
}

variable_types:
block
char *


decompiled_code:
__int64 __fastcall c_end_block_bool(unsigned int a1)
{
c_end_block();
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int a1)
{
func12();
return a1;
}

original_code:
_Bool c_end_block_bool(_Bool cond)
{
 c_end_block();
 return cond;
}


decompiled_code:
int c_output(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( running )
{
pthread_mutex_lock(&output_mutex);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = pthread_mutex_unlock(&output_mutex);
}
return result;
}

canonical_decompiled_code:
int func1(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( running )
{
pthread_mutex_lock(&output_mutex);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = pthread_mutex_unlock(&output_mutex);
}
return result;
}

original_code:
void c_output(const char *format, ...)
{
 if (!running)
  return;
 pthread_mutex_lock(&output_mutex);
 va_list args;
 __builtin_va_start(args,format);
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
 pthread_mutex_unlock(&output_mutex);
}

canonical_original_code:
void c_output(char *format, ...)
{
 if (!running)
  return;
 pthread_mutex_lock(&output_mutex);
 __builtin_va_list args;
 __builtin_va_start(args,format);
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
 pthread_mutex_unlock(&output_mutex);
}

variable_types:
format
char *


watchdog

decompiled_code:
void *__fastcall watchdog(void *a1)
{
__int64 v1; // rbx

v1 = blocked_counter - 1LL;
while ( running )
{
pthread_mutex_lock(&threads_list_mutex);
if ( list_empty(&threads_list) || blocked_counter != v1 || (unsigned __int8)check_liveness() )
{
pthread_mutex_unlock(&threads_list_mutex);
}
else
{
pthread_mutex_unlock(&threads_list_mutex);
c_output("Deadlock detected, fixed interleaving is probably impossible. Perhaps synchronization is correct or you s"
"hould adjust watchdog tick with $C_WATCHDOG_TICK. Publishing all events, finishing all blocks...\n");
pthread_mutex_lock(&events_list_mutex);
publish_all_events();
pthread_mutex_unlock(&events_list_mutex);
pthread_mutex_lock(&blocks_list_mutex);
finish_all_blocks();
pthread_mutex_unlock(&blocks_list_mutex);
}
v1 = blocked_counter;
sleep(watchdog_tick);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
__int64 v1; // rbx

v1 = blocked_counter - 1LL;
while ( running )
{
pthread_mutex_lock(&threads_list_mutex);
if ( func0(&threads_list) || blocked_counter != v1 || (unsigned __int8)check_liveness() )
{
pthread_mutex_unlock(&threads_list_mutex);
}
else
{
pthread_mutex_unlock(&threads_list_mutex);
func1("Deadlock detected, fixed interleaving is probably impossible. Perhaps synchronization is correct or you s"
"hould adjust watchdog tick with $C_WATCHDOG_TICK. Publishing all events, finishing all blocks...\n");
pthread_mutex_lock(&events_list_mutex);
publish_all_events();
pthread_mutex_unlock(&events_list_mutex);
pthread_mutex_lock(&blocks_list_mutex);
finish_all_blocks();
pthread_mutex_unlock(&blocks_list_mutex);
}
v1 = blocked_counter;
sleep(watchdog_tick);
}
return 0LL;
}

original_code:
static void *watchdog(void *dummy)
{
 unsigned long last_blocked_counter = blocked_counter - 1;
 _Bool terminate;
 while (running)
 {
  terminate = 1;
  pthread_mutex_lock(&threads_list_mutex);
  if (terminate && list_empty(&threads_list.head))
   terminate = 0;
  if (terminate && last_blocked_counter != blocked_counter)
   terminate = 0;
  if (terminate && check_liveness())
    terminate = 0;
  pthread_mutex_unlock(&threads_list_mutex);
  if (terminate)
  {
   c_output("Deadlock detected, fixed interleaving is probably impossible. Perhaps synchronization is correct or you should adjust watchdog tick with $C_WATCHDOG_TICK. Publishing all events, finishing all blocks...\n");
   pthread_mutex_lock(&events_list_mutex);
   publish_all_events();
   pthread_mutex_unlock(&events_list_mutex);
   pthread_mutex_lock(&blocks_list_mutex);
   finish_all_blocks();
   pthread_mutex_unlock(&blocks_list_mutex);
  }
  last_blocked_counter = blocked_counter;
  sleep(watchdog_tick);
 }
 return ((void *)0);
}

variable_types:
dummy
void *

return_type: void *


decompiled_code:
void __fastcall c_set_watchdog_tick(unsigned int a1)
{
watchdog_tick = a1;
}

canonical_decompiled_code:
void __fastcall func3(unsigned int a1)
{
watchdog_tick = a1;
}

original_code:
void c_set_watchdog_tick(unsigned int tick)
{
 watchdog_tick = tick;
}

variable_types:
tick
unsigned int


decompiled_code:
int c_init()
{
char *v0; // rax
int result; // eax
char *v2; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v0 = getenv("C_DISABLE");
if ( !v0 || (result = _isoc99_sscanf(v0, "%d", &v4), result != 1) || !v4 )
{
running = 1;
events_list[0] = events_list;
events_list[1] = events_list;
threads_list[0] = threads_list;
threads_list[1] = threads_list;
blocks_list[0] = blocks_list;
blocks_list[1] = blocks_list;
v2 = getenv("C_WATCHDOG_TICK");
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%u", &v3) == 1 )
c_set_watchdog_tick(v3);
}
result = pthread_create(&watchdog_thread, 0LL, watchdog, 0LL);
}
return result;
}

canonical_decompiled_code:
int func4()
{
char *v0; // rax
int result; // eax
char *v2; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v0 = getenv("C_DISABLE");
if ( !v0 || (result = _isoc99_sscanf(v0, "%d", &v4), result != 1) || !v4 )
{
running = 1;
events_list[0] = events_list;
events_list[1] = events_list;
threads_list[0] = threads_list;
threads_list[1] = threads_list;
blocks_list[0] = blocks_list;
blocks_list[1] = blocks_list;
v2 = getenv("C_WATCHDOG_TICK");
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%u", &v3) == 1 )
func3(v3);
}
result = pthread_create(&watchdog_thread, 0LL, watchdog, 0LL);
}
return result;
}

original_code:
void c_init()
{
 char *disable_str;
 int disable_val;
 char *watchdog_tick_str;
 unsigned int new_watchdog_tick;
 disable_str = getenv("C_DISABLE");
 if (disable_str && sscanf(disable_str, "%d", &disable_val) == 1)
  if (disable_val)
   return;
 running = 1;
 do { (&events_list.head)->next = (&events_list.head); (&events_list.head)->prev = (&events_list.head); } while (0);
 do { (&threads_list.head)->next = (&threads_list.head); (&threads_list.head)->prev = (&threads_list.head); } while (0);
 do { (&blocks_list.head)->next = (&blocks_list.head); (&blocks_list.head)->prev = (&blocks_list.head); } while (0);
 watchdog_tick_str = getenv("C_WATCHDOG_TICK");
 if (watchdog_tick_str && sscanf(watchdog_tick_str, "%u", &new_watchdog_tick) == 1)
  c_set_watchdog_tick(new_watchdog_tick);
 pthread_create(&watchdog_thread, ((void *)0), watchdog, ((void *)0));
}


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

if ( running )
{
running = 0;
pthread_join(watchdog_thread, 0LL);
free_threads_list();
free_events_list();
result = free_blocks_list();
}
return result;
}

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

if ( running )
{
running = 0;
pthread_join(watchdog_thread, 0LL);
free_threads_list();
free_events_list();
result = free_blocks_list();
}
return result;
}

original_code:
void c_free()
{
 if (!running)
  return;
 running = 0;
 pthread_join(watchdog_thread, ((void *)0));
 free_threads_list();
 free_events_list();
 free_blocks_list();
}


repo: lumirayz/Terragen

variable_types: struct SDL_PixelFormat { struct SDL_Palette { int ncolors; struct SDL_Color { unsigned char r; unsigned char g; unsigned char b; unsigned char unused; } *colors; } *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; } *

variable_types: value double

variable_types: v double

variable_types: a int

variable_types: r double

variable_types: g double

variable_types: b double

variable_types: p double

return_type: unsigned int

user_defined_types: struct SDL_PixelFormat { struct SDL_Palette *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; }

user_defined_types: struct SDL_Palette { int ncolors; struct SDL_Color *colors; }


decompiled_code:
void qqsocket_init()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void qqsocket_init()
{
}

canonical_original_code:
void qqsocket_init()
{
}


decompiled_code:
void qqsocket_end()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void qqsocket_end()
{
}

canonical_original_code:
void qqsocket_end()
{
}


decompiled_code:
int __fastcall qqsocket_close(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
return close(a1);
}

original_code:
void qqsocket_close( int fd )
{
 close( fd );
}

canonical_original_code:
void qqsocket_close(int fd)
{
 close( fd );
}

variable_types: fd int


decompiled_code:
__int64 __fastcall qqsocket_connect2(int fd, unsigned int a2, __int16 a3)
{
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)_bswap_32(a2);
*(_WORD *)v5.sa_data = _bswap_16(a3);
if ( connect(fd, &v5, 0x10u) >= 0 )
return 0LL;
print_error("qqsocket.c", "qqsocket_connect2", 137LL, "qqsocket connect failed.");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, unsigned int a2, __int16 a3)
{
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)func1(a2);
*(_WORD *)v5.sa_data = func0(a3);
if ( connect(fd, &v5, 0x10u) >= 0 )
return 0LL;
print_error("qqsocket.c", "qqsocket_connect2", 137LL, "qqsocket connect failed.");
return 0xFFFFFFFFLL;
}

original_code:
int qqsocket_connect2( int fd, uint ip, ushort port )
{
 struct sockaddr_in addr;
 memset( &addr, 0, sizeof(struct sockaddr_in) );
 addr.sin_family = 2;
 addr.sin_addr.s_addr = htonl( ip );
 addr.sin_port = htons( port );
 if( connect( fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0 )
 {
  print_error( "qqsocket.c", (char*)__func__, 137,"qqsocket connect failed." );
  return -1;
 }
 return 0;
}

canonical_original_code:
int qqsocket_connect2(int fd, unsigned int ip, unsigned short port)
{
 struct sockaddr_in addr;
 memset( &addr, 0, sizeof(struct sockaddr_in) );
 addr.sin_family = 2;
 addr.sin_addr.s_addr = htonl( ip );
 addr.sin_port = htons( port );
 if( connect( fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0 )
 {
  print_error( "qqsocket.c", (char *)__func__, 137,"qqsocket connect failed." );
  return -1;
 }
 return 0;
}

variable_types:
ip
unsigned int

variable_types:
port
unsigned short

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

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

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


decompiled_code:
in_addr_t __fastcall netaddr_set(char *name, __int64 a2)
{
in_addr_t result; // eax
struct hostent *v3; // rax
__int64 v4; // rdi
char *v5; // rax

result = inet_addr(name);
*(_DWORD *)(a2 + 4) = result;
if ( result == -1 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v4 = **(_QWORD **)v3->h_addr_list;
*(_DWORD *)(a2 + 4) = v4;
v5 = inet_ntoa((struct in_addr)v4);
result = print_error("qqsocket.c", "netaddr_set", 154LL, "Get IP: %s", v5);
}
else
{
result = print_error("qqsocket.c", "netaddr_set", 156LL, "Failed to get ip by %s", name);
}
}
return result;
}

canonical_decompiled_code:
in_addr_t __fastcall func6(char *name, __int64 a2)
{
in_addr_t result; // eax
struct hostent *v3; // rax
__int64 v4; // rdi
char *v5; // rax

result = inet_addr(name);
*(_DWORD *)(a2 + 4) = result;
if ( result == -1 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v4 = **(_QWORD **)v3->h_addr_list;
*(_DWORD *)(a2 + 4) = v4;
v5 = inet_ntoa((struct in_addr)v4);
result = print_error("qqsocket.c", "netaddr_set", 154LL, "Get IP: %s", v5);
}
else
{
result = print_error("qqsocket.c", "netaddr_set", 156LL, "Failed to get ip by %s", name);
}
}
return result;
}

original_code:
void netaddr_set( char* name, struct sockaddr_in* addr )
{
 if( (addr->sin_addr.s_addr = inet_addr( name ) ) == -1 )
 {
  struct hostent *host;
  host = gethostbyname( name );
  if( host )
  {
   addr->sin_addr.s_addr = *(size_t*) host->h_addr_list[0];
   print_error( "qqsocket.c", (char*)__func__, 154,"Get IP: %s", inet_ntoa( addr->sin_addr ) );
  }else{
   print_error( "qqsocket.c", (char*)__func__, 156,"Failed to get ip by %s", name );
  }
 }
}

canonical_original_code:
void netaddr_set(char *name, struct sockaddr_in *addr)
{
 if( (addr->sin_addr.s_addr = inet_addr( name ) ) == -1 )
 {
  struct hostent *host;
  host = gethostbyname( name );
  if( host )
  {
   addr->sin_addr.s_addr = *(unsigned long *) host->h_addr_list[0];
   print_error( "qqsocket.c", (char *)__func__, 154,"Get IP: %s", inet_ntoa( addr->sin_addr ) );
  }else{
   print_error( "qqsocket.c", (char *)__func__, 156,"Failed to get ip by %s", name );
  }
 }
}

variable_types:
name
char *

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

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


decompiled_code:
__int64 __fastcall qqsocket_create(int a1, char *a2, __int16 a3)
{
unsigned int v4; // ebp
struct sockaddr v6[2]; // [rsp+0h] [rbp-28h] BYREF

if ( a1 )
{
v4 = 0;
if ( a1 == 1 )
{
v4 = socket(2, 1, 6);
*(_QWORD *)&v6[0].sa_family = 2LL;
*(_QWORD *)&v6[0].sa_data[6] = 0LL;
if ( a2 )
netaddr_set(a2, (__int64)v6);
*(_WORD *)v6[0].sa_data = _bswap_16(a3);
if ( bind(v4, v6, 0x10u) < 0 )
{
print_error("qqsocket.c", "qqsocket_create", 76LL, "bind tcp socket error!");
close(v4);
v4 = -1;
}
}
}
else
{
v4 = socket(2, 2, 0);
*(_QWORD *)&v6[0].sa_family = 2LL;
*(_QWORD *)&v6[0].sa_data[6] = 0LL;
if ( a2 )
netaddr_set(a2, (__int64)v6);
*(_WORD *)v6[0].sa_data = _bswap_16(a3);
if ( bind(v4, v6, 0x10u) < 0 )
{
print_error("qqsocket.c", "qqsocket_create", 92LL, "bind udp socket error!");
close(v4);
v4 = -1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, char *a2, __int16 a3)
{
unsigned int v4; // ebp
struct sockaddr v6[2]; // [rsp+0h] [rbp-28h] BYREF

if ( a1 )
{
v4 = 0;
if ( a1 == 1 )
{
v4 = socket(2, 1, 6);
*(_QWORD *)&v6[0].sa_family = 2LL;
*(_QWORD *)&v6[0].sa_data[6] = 0LL;
if ( a2 )
func6(a2, (__int64)v6);
*(_WORD *)v6[0].sa_data = func0(a3);
if ( bind(v4, v6, 0x10u) < 0 )
{
print_error("qqsocket.c", "qqsocket_create", 76LL, "bind tcp socket error!");
close(v4);
v4 = -1;
}
}
}
else
{
v4 = socket(2, 2, 0);
*(_QWORD *)&v6[0].sa_family = 2LL;
*(_QWORD *)&v6[0].sa_data[6] = 0LL;
if ( a2 )
func6(a2, (__int64)v6);
*(_WORD *)v6[0].sa_data = func0(a3);
if ( bind(v4, v6, 0x10u) < 0 )
{
print_error("qqsocket.c", "qqsocket_create", 92LL, "bind udp socket error!");
close(v4);
v4 = -1;
}
}
return v4;
}

original_code:
int qqsocket_create( int type, char* ip, ushort port )
{
 int fd = 0;
 struct sockaddr_in addr;
 switch( type )
 {
 case 1:
  fd = socket( 2, SOCK_STREAM, IPPROTO_TCP );
  memset( &addr, 0, sizeof(struct sockaddr_in) );
  addr.sin_family = 2;
  if( ip )
   netaddr_set( ip, &addr );
  else
   addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
  addr.sin_port = htons( port );
  if( bind( fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in) ) < 0 )
  {
   print_error( "qqsocket.c", (char*)__func__, 76,"bind tcp socket error!" );
   close( fd );
   return -1;
  }
  break;
 case 0:
  fd = socket( 2, SOCK_DGRAM, IPPROTO_IP );
  memset( &addr, 0, sizeof(struct sockaddr) );
  addr.sin_family = 2;
  if( ip )
   netaddr_set( ip, &addr );
  else
   addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
  addr.sin_port = htons( port );
  if( bind( fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in) ) < 0 )
  {
   print_error( "qqsocket.c", (char*)__func__, 92,"bind udp socket error!" );
   close( fd );
   return -1;
  }
  break;
 }
 return fd;
}

canonical_original_code:
int qqsocket_create(int type, char *ip, unsigned short port)
{
 int fd = 0;
 struct sockaddr_in addr;
 switch( type )
 {
 case 1:
  fd = socket( 2, SOCK_STREAM, IPPROTO_TCP );
  memset( &addr, 0, sizeof(struct sockaddr_in) );
  addr.sin_family = 2;
  if( ip )
   netaddr_set( ip, &addr );
  else
   addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
  addr.sin_port = htons( port );
  if( bind( fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in) ) < 0 )
  {
   print_error( "qqsocket.c", (char *)__func__, 76,"bind tcp socket error!" );
   close( fd );
   return -1;
  }
  break;
 case 0:
  fd = socket( 2, SOCK_DGRAM, IPPROTO_IP );
  memset( &addr, 0, sizeof(struct sockaddr) );
  addr.sin_family = 2;
  if( ip )
   netaddr_set( ip, &addr );
  else
   addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
  addr.sin_port = htons( port );
  if( bind( fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in) ) < 0 )
  {
   print_error( "qqsocket.c", (char *)__func__, 92,"bind udp socket error!" );
   close( fd );
   return -1;
  }
  break;
 }
 return fd;
}

variable_types: type int

variable_types:
ip
char *


decompiled_code:
__int64 __fastcall qqsocket_connect(int fd, char *name, __int16 a3)
{
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&v5[0].sa_family = 2LL;
*(_QWORD *)&v5[0].sa_data[6] = 0LL;
netaddr_set(name, (__int64)v5);
*(_WORD *)v5[0].sa_data = _bswap_16(a3);
if ( connect(fd, v5, 0x10u) >= 0 )
return 0LL;
print_error("qqsocket.c", "qqsocket_connect", 121LL, "qqsocket connect failed.");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, char *name, __int16 a3)
{
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&v5[0].sa_family = 2LL;
*(_QWORD *)&v5[0].sa_data[6] = 0LL;
func6(name, (__int64)v5);
*(_WORD *)v5[0].sa_data = func0(a3);
if ( connect(fd, v5, 0x10u) >= 0 )
return 0LL;
print_error("qqsocket.c", "qqsocket_connect", 121LL, "qqsocket connect failed.");
return 0xFFFFFFFFLL;
}

original_code:
int qqsocket_connect( int fd, char* ip, ushort port )
{
 struct sockaddr_in addr;
 memset( &addr, 0, sizeof(struct sockaddr_in) );
 addr.sin_family = 2;
 netaddr_set( ip, &addr );
 addr.sin_port = htons( port );
 if( connect( fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0 )
 {
  print_error( "qqsocket.c", (char*)__func__, 121,"qqsocket connect failed." );
  return -1;
 }
 return 0;
}

canonical_original_code:
int qqsocket_connect(int fd, char *ip, unsigned short port)
{
 struct sockaddr_in addr;
 memset( &addr, 0, sizeof(struct sockaddr_in) );
 addr.sin_family = 2;
 netaddr_set( ip, &addr );
 addr.sin_port = htons( port );
 if( connect( fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0 )
 {
  print_error( "qqsocket.c", (char *)__func__, 121,"qqsocket connect failed." );
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall qqsocket_send(int fd, void *buf, size_t n)
{
unsigned int v3; // er13
size_t v5; // rbx
int v6; // eax
unsigned int v7; // edx

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = send(fd, buf, v5, 0);
v7 = v6;
if ( v6 <= 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, void *buf, size_t n)
{
unsigned int v3; // er13
size_t v5; // rbx
int v6; // eax
unsigned int v7; // edx

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = send(fd, buf, v5, 0);
v7 = v6;
if ( v6 <= 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
return v7;
}

original_code:
int qqsocket_send( int fd, uchar* buf, size_t size )
{
 int ret;
 size_t rest;
 rest = size;
 while( rest > 0 )
 {
  ret = send( fd, (char*)buf, rest, 0);
  if(ret <= 0 )
  {
   return ret;
  }
  rest -= ret;
  buf += ret;
 }
 return size;
}

canonical_original_code:
int qqsocket_send(int fd, unsigned char *buf, unsigned long size)
{
 int ret;
 unsigned long rest;
 rest = size;
 while( rest > 0 )
 {
  ret = send( fd, (char *)buf, rest, 0);
  if(ret <= 0 )
  {
   return ret;
  }
  rest -= ret;
  buf += ret;
 }
 return size;
}

variable_types:
buf
unsigned char *

variable_types:
size
unsigned long

variable_types: ret int

variable_types:
rest
unsigned long


decompiled_code:
ssize_t __fastcall qqsocket_recv(int a1, void *a2, size_t a3)
{
return recv(a1, a2, a3, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func10(int a1, void *a2, size_t a3)
{
return recv(a1, a2, a3, 0);
}

original_code:
int qqsocket_recv( int fd, uchar* buf, size_t size )
{
 int ret;
 ret = recv( fd, (char*)buf, size, 0 );
 return ret;
}

canonical_original_code:
int qqsocket_recv(int fd, unsigned char *buf, unsigned long size)
{
 int ret;
 ret = recv( fd, (char *)buf, size, 0 );
 return ret;
}


decompiled_code:
FILE *init_file_path()
{
struct tm *v0; // rbx
FILE *result; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
char s[72]; // [rsp+10h] [rbp-48h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
if ( !v0 )
perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
log_day = v0->tm_mday;
strftime(s, 0x40uLL, "/%Y-%m-%d.txt", v0);
if ( access(dir, 0) )
mkdir_recursive(dir);
strcpy(filename, dir);
strcat(filename, s);
if ( fp_log )
fclose(fp_log);
result = fopen(filename, "aw");
fp_log = result;
return result;
}

canonical_decompiled_code:
FILE *func0()
{
struct tm *v0; // rbx
FILE *result; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
char s[72]; // [rsp+10h] [rbp-48h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
if ( !v0 )
perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
log_day = v0->tm_mday;
strftime(s, 0x40uLL, "/%Y-%m-%d.txt", v0);
if ( access(dir, 0) )
mkdir_recursive(dir);
strcpy(filename, dir);
strcat(filename, s);
if ( fp_log )
fclose(fp_log);
result = fopen(filename, "aw");
fp_log = result;
return result;
}

original_code:
void init_file_path()
{
 char tmp[64];
 time_t t = time( ((void *)0) );
 struct tm* tm1 = localtime(&t);
 if( !tm1 ){
  perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
 }
 log_day = tm1->tm_mday;
 strftime( tmp, 64, "/%Y-%m-%d.txt", tm1 );
 if( access( dir, 0 )!=0 ){
  mkdir_recursive( dir );
 }
 strcpy( filename, dir );
 strcat( filename, tmp );
 if( fp_log )
  fclose( fp_log );
 fp_log = fopen( filename, "aw" );
}

canonical_original_code:
void init_file_path()
{
 char tmp[64];
 long t = time( ((void *)0) );
 struct tm *tm1 = localtime(&t);
 if( !tm1 ){
  perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
 }
 log_day = tm1->tm_mday;
 strftime( tmp, 64, "/%Y-%m-%d.txt", tm1 );
 if( access( dir, 0 )!=0 ){
  mkdir_recursive( dir );
 }
 strcpy( filename, dir );
 strcat( filename, tmp );
 if( fp_log )
  fclose( fp_log );
 fp_log = fopen( filename, "aw" );
}

variable_types:
tmp
char [64]

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
int print_error(const char *a1, const char *a2, unsigned int a3, const char *a4, ...)
{
struct tm *v5; // rax
const struct tm *v6; // r13
time_t timer; // [rsp+8h] [rbp-310h] BYREF
char v9[16]; // [rsp+10h] [rbp-308h] BYREF
char s[520]; // [rsp+20h] [rbp-2F8h] BYREF
gcc_va_list arg; // [rsp+228h] [rbp-F0h] BYREF

LODWORD(v5) = dbg_file | dbg_term;
if ( __PAIR64__(dbg_term, dbg_file) )
{
va_start(arg, a4);
LODWORD(v5) = vsnprintf(s, 0x1F4uLL, a4, arg);
s[(int)v5] = 0;
if ( dbg_term )
LODWORD(v5) = printf("%s(%d): %s\n", a2, a3, s);
if ( dbg_file )
{
timer = time(0LL);
v5 = localtime(&timer);
v6 = v5;
if ( v5 )
{
if ( v5->tm_mday != log_day )
init_file_path();
strftime(v9, 0xFuLL, "%X", v6);
fprintf(fp_log, "%s [%s]%s(%d): %s\n", v9, a1, a2, a3, s);
LODWORD(v5) = fflush(fp_log);
}
}
}
return (int)v5;
}

canonical_decompiled_code:
int func1(const char *a1, const char *a2, unsigned int a3, const char *a4, ...)
{
struct tm *v5; // rax
const struct tm *v6; // r13
time_t timer; // [rsp+8h] [rbp-310h] BYREF
char v9[16]; // [rsp+10h] [rbp-308h] BYREF
char s[520]; // [rsp+20h] [rbp-2F8h] BYREF
gcc_va_list arg; // [rsp+228h] [rbp-F0h] BYREF

LODWORD(v5) = dbg_file | dbg_term;
if ( __PAIR64__(dbg_term, dbg_file) )
{
va_start(arg, a4);
LODWORD(v5) = vsnprintf(s, 0x1F4uLL, a4, arg);
s[(int)v5] = 0;
if ( dbg_term )
LODWORD(v5) = printf("%s(%d): %s\n", a2, a3, s);
if ( dbg_file )
{
timer = time(0LL);
v5 = localtime(&timer);
v6 = v5;
if ( v5 )
{
if ( v5->tm_mday != log_day )
func0();
strftime(v9, 0xFuLL, "%X", v6);
fprintf(fp_log, "%s [%s]%s(%d): %s\n", v9, a1, a2, a3, s);
LODWORD(v5) = fflush(fp_log);
}
}
}
return (int)v5;
}

original_code:
void print_error(char* file, char* function, int line, const char *fmt, ...)
{
 va_list args;
 char printbuf[512];
 int i;
 if( !dbg_term && !dbg_file )
  return;
 __builtin_va_start(args,fmt);
 i=vsnprintf( printbuf, 500, fmt, args );
 printbuf[i] = 0;
 __builtin_va_end(args);
 if( dbg_term ){
  printf("%s(%d): %s\n", function, line, printbuf);
 }
 if( dbg_file ){
  time_t t = time( ((void *)0) );
  struct tm* tm1 = localtime(&t);
  if( !tm1 ) return;
  if( tm1->tm_mday != log_day ){
   init_file_path();
  }
  char tmp[16];
  strftime( tmp, 15, "%X", tm1 );
  fprintf( fp_log, "%s [%s]%s(%d): %s\n", tmp, file, function, line, printbuf);
  fflush( fp_log );
 }
}

canonical_original_code:
void print_error(char *file, char *function, int line, char *fmt, ...)
{
 __builtin_va_list args;
 char printbuf[512];
 int i;
 if( !dbg_term && !dbg_file )
  return;
 __builtin_va_start(args,fmt);
 i=vsnprintf( printbuf, 500, fmt, args );
 printbuf[i] = 0;
 __builtin_va_end(args);
 if( dbg_term ){
  printf("%s(%d): %s\n", function, line, printbuf);
 }
 if( dbg_file ){
  long t = time( ((void *)0) );
  struct tm *tm1 = localtime(&t);
  if( !tm1 ) return;
  if( tm1->tm_mday != log_day ){
   init_file_path();
  }
  char tmp[16];
  strftime( tmp, 15, "%X", tm1 );
  fprintf( fp_log, "%s [%s]%s(%d): %s\n", tmp, file, function, line, printbuf);
  fflush( fp_log );
 }
}

variable_types:
file
char *

variable_types:
function
char *

variable_types: line int

variable_types:
fmt
char *

variable_types:
printbuf
char [512]

variable_types: i int

variable_types:
tmp
char [16]

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
void __fastcall hex_dump(_BYTE *a1, int a2)
{
const char *v2; // rax
FILE *v3; // r12
const char *v4; // rax
_BYTE v5[4120]; // [rsp+0h] [rbp-1018h] BYREF

if ( dbg_term )
{
v2 = hex_str(a1, a2, v5);
puts(v2);
}
if ( dbg_file )
{
v3 = fp_log;
v4 = hex_str(a1, a2, v5);
fputs(v4, v3);
fputc(10, fp_log);
fflush(fp_log);
}
}

canonical_decompiled_code:
void __fastcall func2(_BYTE *a1, int a2)
{
const char *v2; // rax
FILE *v3; // r12
const char *v4; // rax
_BYTE v5[4120]; // [rsp+0h] [rbp-1018h] BYREF

if ( dbg_term )
{
v2 = hex_str(a1, a2, v5);
puts(v2);
}
if ( dbg_file )
{
v3 = fp_log;
v4 = hex_str(a1, a2, v5);
fputs(v4, v3);
fputc(10, fp_log);
fflush(fp_log);
}
}

original_code:
void hex_dump( unsigned char * buf, int len )
{
 char str[4*1024];
 if( dbg_term )
  puts( hex_str( buf, len, str ) );
 if( dbg_file ){
  fputs( hex_str( buf, len, str ), fp_log );
  fprintf( fp_log, "\n" );
  fflush( fp_log );
 }
}

canonical_original_code:
void hex_dump(unsigned char *buf, int len)
{
 char str[-1];
 if( dbg_term )
  puts( hex_str( buf, len, str ) );
 if( dbg_file ){
  fputs( hex_str( buf, len, str ), fp_log );
  fprintf( fp_log, "\n" );
  fflush( fp_log );
 }
}

variable_types: len int

variable_types:
str
char [-1]


decompiled_code:
void debug_term_on()
{
dbg_term = 1;
}

canonical_decompiled_code:
void func3()
{
dbg_term = 1;
}

original_code:
void debug_term_on()
{
 dbg_term = 1;
}

canonical_original_code:
void debug_term_on()
{
 dbg_term = 1;
}


decompiled_code:
void debug_term_off()
{
dbg_term = 0;
}

canonical_decompiled_code:
void func4()
{
dbg_term = 0;
}

original_code:
void debug_term_off()
{
 dbg_term = 0;
}

canonical_original_code:
void debug_term_off()
{
 dbg_term = 0;
}


decompiled_code:
FILE *debug_file_on()
{
FILE *result; // rax

if ( !dbg_file )
{
result = init_file_path();
dbg_file = 1;
}
return result;
}

canonical_decompiled_code:
FILE *func5()
{
FILE *result; // rax

if ( !dbg_file )
{
result = func0();
dbg_file = 1;
}
return result;
}

original_code:
void debug_file_on()
{
 if( dbg_file )
  return;
 init_file_path();
 dbg_file = 1;
}


decompiled_code:
int debug_file_off()
{
int result; // eax

if ( dbg_file )
{
dbg_file = 0;
if ( fp_log )
result = fclose(fp_log);
}
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax

if ( dbg_file )
{
dbg_file = 0;
if ( fp_log )
result = fclose(fp_log);
}
return result;
}

original_code:
void debug_file_off()
{
 if( !dbg_file )
  return;
 dbg_file = 0;
 if( fp_log )
  fclose( fp_log );
}


decompiled_code:
char *__fastcall debug_set_dir(char *src)
{
return strcpy(dir, src);
}

canonical_decompiled_code:
char *__fastcall func7(char *src)
{
return strcpy(dir, src);
}

original_code:
void debug_set_dir(char* str)
{
 strcpy( dir, str );
}

canonical_original_code:
void debug_set_dir(char *str)
{
 strcpy( dir, str );
}

variable_types:
str
char *


decompiled_code:
__int64 init_table()
{
__int64 i; // rdi
__int64 result; // rax
int v2; // ecx
unsigned int v3; // esi
bool v4; // zf

for ( i = 0LL; i != 256; ++i )
{
LODWORD(result) = i;
v2 = 8;
do
{
v3 = (unsigned int)result >> 1;
v4 = (result & 1) == 0;
result = ((unsigned int)result >> 1) ^ 0xEDB88320;
if ( v4 )
result = v3;
--v2;
}
while ( v2 );
CRC32[i] = result;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 i; // rdi
__int64 result; // rax
int v2; // ecx
unsigned int v3; // esi
bool v4; // zf

for ( i = 0LL; i != 256; ++i )
{
LODWORD(result) = i;
v2 = 8;
do
{
v3 = (unsigned int)result >> 1;
v4 = (result & 1) == 0;
result = ((unsigned int)result >> 1) ^ 0xEDB88320;
if ( v4 )
result = v3;
--v2;
}
while ( v2 );
CRC32[i] = result;
}
return result;
}

original_code:
static void init_table()
{
 int i,j;
 uint crc;
 for(i = 0;i < 256;i++)
 {
  crc = i;
  for(j = 0;j < 8;j++)
  {
   if(crc & 1){
    crc = (crc >> 1) ^ 0xEDB88320;
   }else{
    crc = crc >> 1;
   }
  }
  CRC32[i] = crc;
 }
}

canonical_original_code:
static void init_table()
{
 int i; int j;
 unsigned int crc;
 for(i = 0;i < 256;i++)
 {
  crc = i;
  for(j = 0;j < 8;j++)
  {
   if(crc & 1){
    crc = (crc >> 1) ^ 0xEDB88320;
   }else{
    crc = crc >> 1;
   }
  }
  CRC32[i] = crc;
 }
}

variable_types: j int

variable_types:
crc
unsigned int


decompiled_code:
__int64 __fastcall crc32(_BYTE *a1, int a2)
{
_BYTE *v2; // rdx
unsigned int v3; // eax

if ( !init )
{
init_table();
init = 1;
}
if ( a2 <= 0 )
{
v3 = -1;
}
else
{
v2 = a1;
v3 = -1;
do
v3 = CRC32[(unsigned __int8)(*v2++ ^ v3)] ^ (v3 >> 8);
while ( v2 != &a1[a2 - 1 + 1] );
}
return ~v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, int a2)
{
_BYTE *v2; // rdx
unsigned int v3; // eax

if ( !init )
{
func0();
init = 1;
}
if ( a2 <= 0 )
{
v3 = -1;
}
else
{
v2 = a1;
v3 = -1;
do
v3 = CRC32[(unsigned __int8)(*v2++ ^ v3)] ^ (v3 >> 8);
while ( v2 != &a1[a2 - 1 + 1] );
}
return ~v3;
}

original_code:
uint crc32( uchar *buf, int len)
{
 uint ret = 0xFFFFFFFF;
 int i;
 if( !init ){
  init_table();
  init = 1;
 }
 for(i = 0; i < len;i++)
 {
  ret = CRC32[((ret & 0xFF) ^ buf[i])] ^ (ret >> 8);
 }
 ret = ~ret;
 return ret;
}

canonical_original_code:
unsigned int crc32(unsigned char *buf, int len)
{
 unsigned int ret = 0xFFFFFFFF;
 int i;
 if( !init ){
  init_table();
  init = 1;
 }
 for(i = 0; i < len;i++)
 {
  ret = CRC32[((ret & 0xFF) ^ buf[i])] ^ (ret >> 8);
 }
 ret = ~ret;
 return ret;
}

variable_types:
buf
unsigned char *

variable_types:
ret
unsigned int


repo: luqmana/RemoteJoyPlus

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

if ( a1 == 1 )
{
g_context[261133] |= a2;
}
else
{
if ( a1 != 2 )
return result;
g_context[261133] &= ~a2;
}
return rj_send_event();
}

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

if ( a1 == 1 )
{
g_context[261133] |= a2;
}
else
{
if ( a1 != 2 )
return result;
g_context[261133] &= ~a2;
}
return rj_send_event();
}

original_code:
void forward_button_input(int type, unsigned int keymap) {
 if (type == 1)
  g_context.button_state |= keymap;
 else if (type == 2)
  g_context.button_state &= ~keymap;
 else
  return;
 rj_send_event(type, keymap);
}

variable_types: type int

variable_types:
keymap
unsigned int


setError

decompiled_code:
void __fastcall setError(__int64 a1)
{
error = a1;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
error = a1;
}

original_code:
static __inline__ void setError(char *err)
{
  error = err;
}

variable_types:
err
char *


timerCallback

decompiled_code:
__int64 __fastcall timerCallback(unsigned int a1)
{
SDL_CondBroadcast(eventWait);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
SDL_CondBroadcast(eventWait);
return a1;
}

original_code:
static Uint32 timerCallback(Uint32 interval, void *param)
{
  SDL_CondBroadcast(eventWait);
  return interval;
}

canonical_original_code:
static unsigned int timerCallback(unsigned int interval, void *param)
{
  SDL_CondBroadcast(eventWait);
  return interval;
}

variable_types:
interval
unsigned int

variable_types:
param
void *


decompiled_code:
__int64 FE_GetError()
{
return error;
}

canonical_decompiled_code:
__int64 func2()
{
return error;
}

original_code:
char *FE_GetError()
{
  return error;
}

canonical_original_code:
char *FE_GetError()
{
  return error;
}

return_type: char *


decompiled_code:
__int64 FE_PumpEvents()
{
SDL_mutexP(eventLock);
SDL_PumpEvents();
return SDL_mutexV(eventLock);
}

canonical_decompiled_code:
__int64 func4()
{
SDL_mutexP(eventLock);
SDL_PumpEvents();
return SDL_mutexV(eventLock);
}

original_code:
void FE_PumpEvents()
{
  SDL_mutexP(eventLock);
  SDL_PumpEvents();
  SDL_mutexV(eventLock);
}


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

if ( (SDL_WasInit(1LL) & 1) == 0 )
SDL_InitSubSystem(1LL);
eventLock = SDL_CreateMutex();
if ( eventLock )
{
eventWait = SDL_CreateCond();
if ( eventWait )
{
eventTimer = SDL_AddTimer(10LL, timerCallback, 0LL);
if ( eventTimer )
{
result = 0LL;
}
else
{
setError((__int64)"FE: can't add a timer");
result = 0xFFFFFFFFLL;
}
}
else
{
setError((__int64)"FE: can't create a condition variable");
result = 0xFFFFFFFFLL;
}
}
else
{
setError((__int64)"FE: can't create a mutex");
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( (SDL_WasInit(1LL) & 1) == 0 )
SDL_InitSubSystem(1LL);
eventLock = SDL_CreateMutex();
if ( eventLock )
{
eventWait = SDL_CreateCond();
if ( eventWait )
{
eventTimer = SDL_AddTimer(10LL, timerCallback, 0LL);
if ( eventTimer )
{
result = 0LL;
}
else
{
func0((__int64)"FE: can't add a timer");
result = 0xFFFFFFFFLL;
}
}
else
{
func0((__int64)"FE: can't create a condition variable");
result = 0xFFFFFFFFLL;
}
}
else
{
func0((__int64)"FE: can't create a mutex");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int FE_Init()
{
  if (0 == (0x00000001 & SDL_WasInit(0x00000001)))
  {
    SDL_InitSubSystem(0x00000001);
  }
  eventLock = SDL_CreateMutex();
  if (((void *)0) == eventLock)
  {
    setError("FE: can't create a mutex");
    return -1;
  }
  eventWait = SDL_CreateCond();
  if (((void *)0) == eventWait)
  {
    setError("FE: can't create a condition variable");
    return -1;
  }
  eventTimer = SDL_AddTimer(10, timerCallback, ((void *)0));
  if (((void *)0) == eventTimer)
  {
    setError("FE: can't add a timer");
    return -1;
  }
  return 0;
}


decompiled_code:
__int64 FE_Quit()
{
SDL_DestroyMutex(eventLock);
eventLock = 0LL;
SDL_DestroyCond(eventWait);
eventWait = 0LL;
return SDL_RemoveTimer(eventTimer);
}

canonical_decompiled_code:
__int64 func8()
{
SDL_DestroyMutex(eventLock);
eventLock = 0LL;
SDL_DestroyCond(eventWait);
eventWait = 0LL;
return SDL_RemoveTimer(eventTimer);
}

original_code:
void FE_Quit()
{
  SDL_DestroyMutex(eventLock);
  eventLock = ((void *)0);
  SDL_DestroyCond(eventWait);
  eventWait = ((void *)0);
  SDL_RemoveTimer(eventTimer);
}


decompiled_code:
__int64 rj_reader_start()
{
const char *v1; // rax

rj_reader_thread = SDL_CreateThread(rj_reader_handler, &rj_sock);
if ( rj_reader_thread )
return 0LL;
v1 = (const char *)SDL_GetError();
fprintf(stderr, "%s\n", v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
const char *v1; // rax

rj_reader_thread = SDL_CreateThread(rj_reader_handler, &rj_sock);
if ( rj_reader_thread )
return 0LL;
v1 = (const char *)SDL_GetError();
fprintf(stderr, "%s\n", v1);
return 0xFFFFFFFFLL;
}

original_code:
int rj_reader_start() {
 rj_reader_thread = SDL_CreateThread(rj_reader_handler, (void *) &rj_sock);
 if (rj_reader_thread == ((void *)0)) {
  fprintf(stderr, "%s\n", SDL_GetError());
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall rj_send_event(int a1, int a2)
{
int v2; // ebx
int v3; // eax
int buf[9]; // [rsp+0h] [rbp-24h] BYREF

if ( rj_sock < 0 )
return 0xFFFFFFFFLL;
buf[0] = -1868903185;
buf[1] = a1;
buf[2] = a2;
v2 = 0;
do
{
v3 = write(rj_sock, (char *)buf + v2, 12 - v2);
if ( v3 >= 0 )
{
v2 += v3;
}
else if ( *_errno_location() != 4 )
{
perror("write");
LABEL_8:
fwrite("Could not write out data to socket.\n", 1uLL, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
}
while ( v2 <= 11 );
if ( v2 != 12 )
goto LABEL_8;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
int v2; // ebx
int v3; // eax
int buf[9]; // [rsp+0h] [rbp-24h] BYREF

if ( rj_sock < 0 )
return 0xFFFFFFFFLL;
buf[0] = -1868903185;
buf[1] = a1;
buf[2] = a2;
v2 = 0;
do
{
v3 = write(rj_sock, (char *)buf + v2, 12 - v2);
if ( v3 >= 0 )
{
v2 += v3;
}
else if ( *_errno_location() != 4 )
{
perror("write");
LABEL_8:
fwrite("Could not write out data to socket.\n", 1uLL, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
}
while ( v2 <= 11 );
if ( v2 != 12 )
goto LABEL_8;
return 0LL;
}

original_code:
int rj_send_event(int type, unsigned int value) {
 struct JoyEvent event;
 if (rj_sock < 0)
  return -1;
 event.magic = (0x909ACCEF);
 event.type = (type);
 event.value = (value);
 const void *buf = (void *) &event;
 int written = 0;
 int len = sizeof(event);
 while (written < len) {
  int ret = write(rj_sock, buf + written, len - written);
  if (ret < 0) {
   if ((*__errno_location ()) != 4) {
    perror("write");
    written = -1;
    break;
   }
  } else
   written += ret;
 }
 if (written != len) {
  fprintf(stderr, "Could not write out data to socket.\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int rj_send_event(int type, unsigned int value) {
 struct JoyEvent event;
 if (rj_sock < 0)
  return -1;
 event.magic = (0x909ACCEF);
 event.type = (type);
 event.value = (value);
 void *buf = (void *) &event;
 int written = 0;
 int len = sizeof(event);
 while (written < len) {
  int ret = write(rj_sock, buf + written, len - written);
  if (ret < 0) {
   if ((*__errno_location ()) != 4) {
    perror("write");
    written = -1;
    break;
   }
  } else
   written += ret;
 }
 if (written != len) {
  fprintf(stderr, "Could not write out data to socket.\n");
  return -1;
 }
 return 0;
}

variable_types:
value
unsigned int

variable_types: struct JoyEvent { unsigned int magic; int type; unsigned int value; }

variable_types:
buf
void *

variable_types: written int

variable_types: ret int


decompiled_code:
__int64 rj_interface_init()
{
struct hostent *v0; // rax
__int64 result; // rax
char optval[4]; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

rj_sock = socket(2, 1, 0);
if ( rj_sock < 0 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(10004);
v0 = gethostbyname("localhost");
if ( v0 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v0->h_addr_list;
if ( connect(rj_sock, &addr, 0x10u) < 0 )
{
perror("connect");
close(rj_sock);
result = 0xFFFFFFFFLL;
}
else
{
setsockopt(rj_sock, 6, 1, optval, 4u);
rj_send_event(5, g_context[261130] | 1);
result = 0LL;
}
}
else
{
fprintf(stderr, "Unknown host %s.\n", "localhost");
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
struct hostent *v0; // rax
__int64 result; // rax
char optval[4]; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

rj_sock = socket(2, 1, 0);
if ( rj_sock < 0 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func1(10004);
v0 = gethostbyname("localhost");
if ( v0 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v0->h_addr_list;
if ( connect(rj_sock, &addr, 0x10u) < 0 )
{
perror("connect");
close(rj_sock);
result = 0xFFFFFFFFLL;
}
else
{
setsockopt(rj_sock, 6, 1, optval, 4u);
func3(5, g_context[261130] | 1);
result = 0LL;
}
}
else
{
fprintf(stderr, "Unknown host %s.\n", "localhost");
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int rj_interface_init() {
 struct sockaddr_in name;
 int flag;
 rj_sock = socket(2, SOCK_STREAM, 0);
 if (rj_sock < 0) {
  perror("socket");
  return -1;
 }
 struct hostent *hostinfo;
 name.sin_family = 2;
 name.sin_port = htons(10004);
 hostinfo = gethostbyname("localhost");
 if (hostinfo == ((void *)0)) {
  fprintf(stderr, "Unknown host %s.\n", "localhost");
  return -1;
 }
 name.sin_addr = *(struct in_addr *) hostinfo->h_addr_list[0];
 if (connect(rj_sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  perror("connect");
  close(rj_sock);
  return -1;
 }
 setsockopt(rj_sock, 6, 1, &flag, sizeof(int));
 rj_send_event(5, 1 | g_context.psp_flags);
 return 0;
}

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

variable_types: flag int

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


decompiled_code:
int rj_interface_cleanup()
{
int result; // eax

if ( rj_reader_thread )
result = SDL_KillThread();
if ( rj_sock >= 0 )
result = close(rj_sock);
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

if ( rj_reader_thread )
result = SDL_KillThread();
if ( rj_sock >= 0 )
result = close(rj_sock);
return result;
}

original_code:
void rj_interface_cleanup() {
 if (rj_reader_thread)
  SDL_KillThread(rj_reader_thread);
 if (rj_sock >= 0)
  close(rj_sock);
}


decompiled_code:
char *__fastcall remove_wsp(void *src)
{
int v1; // er13
int v2; // ebp
const unsigned __int16 **v3; // rbx
char *result; // rax
int i; // edx
char *v6; // rsi
__int64 v7; // rbp
char *v8; // rdx

v1 = strlen((const char *)src);
v2 = v1;
v3 = _ctype_b_loc();
result = (char *)src;
for ( i = 0; ; ++i )
{
v6 = result++;
if ( ((*v3)[*v6] & 0x2000) == 0 )
break;
}
if ( i > 0 )
{
v2 = v1 - i;
result = (char *)memmove(src, v6, v1 - i + 1);
}
if ( v2 > 0 )
{
v7 = v2 - 1;
result = (char *)src + v7;
if ( ((*v3)[*((char *)src + v7)] & 0x2000) != 0 )
{
v8 = (char *)src + v7 - 1;
do
{
*result = 0;
result = v8--;
}
while ( ((*v3)[*result] & 0x2000) != 0 );
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(void *src)
{
int v1; // er13
int v2; // ebp
const unsigned __int16 **v3; // rbx
char *result; // rax
int i; // edx
char *v6; // rsi
__int64 v7; // rbp
char *v8; // rdx

v1 = strlen((const char *)src);
v2 = v1;
v3 = _ctype_b_loc();
result = (char *)src;
for ( i = 0; ; ++i )
{
v6 = result++;
if ( ((*v3)[*v6] & 0x2000) == 0 )
break;
}
if ( i > 0 )
{
v2 = v1 - i;
result = (char *)memmove(src, v6, v1 - i + 1);
}
if ( v2 > 0 )
{
v7 = v2 - 1;
result = (char *)src + v7;
if ( ((*v3)[*((char *)src + v7)] & 0x2000) != 0 )
{
v8 = (char *)src + v7 - 1;
do
{
*result = 0;
result = v8--;
}
while ( ((*v3)[*result] & 0x2000) != 0 );
}
}
return result;
}

original_code:
void remove_wsp(char* buf) {
 int len = strlen(buf);
 int i = 0;
 while (((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace))
  i++;
 if (i > 0) {
  len -= i;
  memmove(buf, &buf[i], len + 1);
 }
 if (len <= 0)
  return;
 i = len - 1;
 while(((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace))
  buf[i--] = 0;
}

canonical_original_code:
void remove_wsp(char *buf) {
 int len = strlen(buf);
 int i = 0;
 while (((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace))
  i++;
 if (i > 0) {
  len -= i;
  memmove(buf, &buf[i], len + 1);
 }
 if (len <= 0)
  return;
 i = len - 1;
 while(((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace))
  buf[i--] = 0;
}

variable_types:
buf
char *

variable_types: len int

variable_types: i int