prompt

decompiled_code:
char *__fastcall prompt(int a1)
{
int v1; // ebx
char *result; // rax
size_t v3; // [rsp+0h] [rbp-18h] BYREF
char *lineptr; // [rsp+8h] [rbp-10h] BYREF

if ( a1 )
fprintf(stdout, "%sSERVER_SHELL$\n", MenuString);
fflush(stdout);
v3 = 0LL;
lineptr = (char *)malloc(1uLL);
v1 = getline(&lineptr, &v3, stdin);
*strchr(lineptr, 10) = 0;
result = 0LL;
if ( v1 > 0 )
result = lineptr;
return result;
}

canonical_decompiled_code:
char *__fastcall func12(int a1)
{
int v1; // ebx
char *result; // rax
size_t v3; // [rsp+0h] [rbp-18h] BYREF
char *lineptr; // [rsp+8h] [rbp-10h] BYREF

if ( a1 )
fprintf(stdout, "%sSERVER_SHELL$\n", MenuString);
fflush(stdout);
v3 = 0LL;
lineptr = (char *)malloc(1uLL);
v1 = getline(&lineptr, &v3, stdin);
*strchr(lineptr, 10) = 0;
result = 0LL;
if ( v1 > 0 )
result = lineptr;
return result;
}

original_code:
char* prompt(int menu)
{
  if (menu) fprintf(stdout,"%sSERVER_SHELL$\n", MenuString);
  fflush(stdout);
  int bytes_read;
  size_t nbytes = 0;
  char *my_string = (char*) malloc(1);
  bytes_read = getline (&my_string, &nbytes, stdin);
  char* pch;
  pch = strchr(my_string,'\n');
  *pch = '\0';
  if(bytes_read>0)
 return my_string;
  else
 return 0;
}

canonical_original_code:
char *prompt(int menu)
{
  if (menu) fprintf(stdout,"%sSERVER_SHELL$\n", MenuString);
  fflush(stdout);
  int bytes_read;
  unsigned long nbytes = 0;
  char *my_string = (char*) malloc(1);
  bytes_read = getline (&my_string, &nbytes, stdin);
  char *pch;
  pch = strchr(my_string,'\n');
  *pch = '\0';
  if(bytes_read>0)
 return my_string;
  else
 return 0;
}

variable_types: menu int

variable_types:
nbytes
unsigned long

variable_types:
pch
char *


shell

repo: samirahmed/bogosort

decompiled_code:
__int64 shell()
{
int v0; // er12
int i; // ebx
char *v2; // rax
char *v3; // rbp
int v4; // eax
int v5; // er12

v0 = 1;
for ( i = 1; ; v0 = i == 1 )
{
v2 = prompt(v0);
v3 = v2;
if ( v2 )
break;
LABEL_5:
;
}
while ( 1 )
{
v4 = docmd(v2);
i = v4;
if ( v4 < 0 )
break;
v5 = v4 == 1;
free(v3);
v2 = prompt(v5);
v3 = v2;
if ( !v2 )
goto LABEL_5;
}
free(v3);
fflush(stdout);
fwrite("terminating\n", 1uLL, 0xCuLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 func13()
{
int v0; // er12
int i; // ebx
char *v2; // rax
char *v3; // rbp
int v4; // eax
int v5; // er12

v0 = 1;
for ( i = 1; ; v0 = i == 1 )
{
v2 = func12(v0);
v3 = v2;
if ( v2 )
break;
LABEL_5:
;
}
while ( 1 )
{
v4 = func11(v2);
i = v4;
if ( v4 < 0 )
break;
v5 = v4 == 1;
free(v3);
v2 = func12(v5);
v3 = v2;
if ( !v2 )
goto LABEL_5;
}
free(v3);
fflush(stdout);
fwrite("terminating\n", 1uLL, 0xCuLL, stderr);
return 0LL;
}

original_code:
void * shell(void *arg)
{
  char* c;
  int rc=1;
  int menu=1;
  while (1)
  {
    if ((c=prompt(menu))!=0) rc=docmd(c);
    if (rc<0) break;
    if (rc==1) menu=1; else menu=0;
    if(c!=0) free(c);
  }
  if(c!=0) free(c);
  fflush(stdout);
  fprintf(stderr, "terminating\n");
  return ((void *)0);
}

canonical_original_code:
void *shell(void *arg)
{
  char *c;
  int rc = 1;
  int menu = 1;
  while (1)
  {
    if ((c=prompt(menu))!=0) rc=docmd(c);
    if (rc<0) break;
    if (rc==1) menu=1; else menu=0;
    if(c!=0) free(c);
  }
  if(c!=0) free(c);
  fflush(stdout);
  fprintf(stderr, "terminating\n");
  return ((void *)0);
}

variable_types:
arg
void *

variable_types:
c
char *

variable_types: rc int

return_type: void *


randint

decompiled_code:
int randint()
{
unsigned int v0; // eax

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

canonical_decompiled_code:
int func3()
{
unsigned int v0; // eax

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

original_code:
extern int randint()
{
   srand (time(((void *)0)));
   return rand();
}

return_type: int


decompiled_code:
int __fastcall cell_dump(unsigned __int16 *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax

fputc(10, stderr);
fprintf(stderr, "COLUMN - (x) - %d \n", *a1);
fprintf(stderr, "ROW - (y) - %d \n", a1[1]);
v1 = *((_DWORD *)a1 + 1);
if ( v1 == 2 )
{
fwrite("CELL TYPE: CELL_JAIL", 1uLL, 0x14uLL, stderr);
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
fwrite("CELL TYPE: CELL_HOME", 1uLL, 0x14uLL, stderr);
else
fwrite("CELL TYPE: UNKWOWN", 1uLL, 0x12uLL, stderr);
}
else if ( v1 )
{
fwrite("CELL TYPE: CELL_FLOOR", 1uLL, 0x15uLL, stderr);
}
else
{
fwrite("CELL TYPE: CELL_WALL", 1uLL, 0x14uLL, stderr);
}
fputc(10, stderr);
fprintf(stderr, "CELL TURF: %d", (unsigned int)(*((_DWORD *)a1 + 3) + 1));
fputc(10, stderr);
v2 = *((_DWORD *)a1 + 2);
if ( v2 == 2 )
{
fwrite("CELLSTATE: HOLDING", 1uLL, 0x12uLL, stderr);
}
else if ( v2 > 2 )
{
if ( v2 == 3 )
fwrite("CELLSTATE: OCCUPIED AND HOLDING", 1uLL, 0x1FuLL, stderr);
else
fwrite("CELLSTATE: UNKWOWN", 1uLL, 0x12uLL, stderr);
}
else if ( v2 )
{
fwrite("CELLSTATE: OCCUPIED\n", 1uLL, 0x14uLL, stderr);
}
else
{
fwrite("CELLSTATE: EMPTY", 1uLL, 0x10uLL, stderr);
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax

fputc(10, stderr);
fprintf(stderr, "COLUMN - (x) - %d \n", *a1);
fprintf(stderr, "ROW - (y) - %d \n", a1[1]);
v1 = *((_DWORD *)a1 + 1);
if ( v1 == 2 )
{
fwrite("CELL TYPE: CELL_JAIL", 1uLL, 0x14uLL, stderr);
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
fwrite("CELL TYPE: CELL_HOME", 1uLL, 0x14uLL, stderr);
else
fwrite("CELL TYPE: UNKWOWN", 1uLL, 0x12uLL, stderr);
}
else if ( v1 )
{
fwrite("CELL TYPE: CELL_FLOOR", 1uLL, 0x15uLL, stderr);
}
else
{
fwrite("CELL TYPE: CELL_WALL", 1uLL, 0x14uLL, stderr);
}
fputc(10, stderr);
fprintf(stderr, "CELL TURF: %d", (unsigned int)(*((_DWORD *)a1 + 3) + 1));
fputc(10, stderr);
v2 = *((_DWORD *)a1 + 2);
if ( v2 == 2 )
{
fwrite("CELLSTATE: HOLDING", 1uLL, 0x12uLL, stderr);
}
else if ( v2 > 2 )
{
if ( v2 == 3 )
fwrite("CELLSTATE: OCCUPIED AND HOLDING", 1uLL, 0x1FuLL, stderr);
else
fwrite("CELLSTATE: UNKWOWN", 1uLL, 0x12uLL, stderr);
}
else if ( v2 )
{
fwrite("CELLSTATE: OCCUPIED\n", 1uLL, 0x14uLL, stderr);
}
else
{
fwrite("CELLSTATE: EMPTY", 1uLL, 0x10uLL, stderr);
}
return fputc(10, stderr);
}

original_code:
extern void
cell_dump(Cell *cell)
{
  fprintf(stderr, "\n");
  fprintf(stderr, "COLUMN - (x) - %d \n",cell->pos.x);
  fprintf(stderr, "ROW    - (y) - %d \n",cell->pos.y);
  switch(cell->type)
  {
    case CELL_WALL:
      fprintf(stderr, "CELL TYPE: CELL_WALL");
      break;
    case CELL_FLOOR:
      fprintf(stderr, "CELL TYPE: CELL_FLOOR");
      break;
    case CELL_HOME:
      fprintf(stderr, "CELL TYPE: CELL_HOME");
      break;
    case CELL_JAIL:
      fprintf(stderr, "CELL TYPE: CELL_JAIL");
      break;
    default:
      fprintf(stderr, "CELL TYPE: UNKWOWN");
      break;
  }
  fprintf(stderr, "\n");
  fprintf(stderr, "CELL TURF: %d",cell->turf+1);
  fprintf(stderr, "\n");
  switch(cell->cell_state)
  {
    case CELLSTATE_EMPTY:
      fprintf(stderr, "CELLSTATE: EMPTY");
      break;
    case CELLSTATE_HOLDING:
      fprintf(stderr, "CELLSTATE: HOLDING");
      break;
    case CELLSTATE_OCCUPIED:
      fprintf(stderr, "CELLSTATE: OCCUPIED\n");
      break;
    case CELLSTATE_OCCUPIED_HOLDING:
      fprintf(stderr, "CELLSTATE: OCCUPIED AND HOLDING");
      break;
    default:
      fprintf(stderr, "CELLSTATE: UNKWOWN");
      break;
  }
  fprintf(stderr, "\n");
}

canonical_original_code:
extern void cell_dump(struct CellStruct *cell)
{
  fprintf(stderr, "\n");
  fprintf(stderr, "COLUMN - (x) - %d \n",cell->pos.x);
  fprintf(stderr, "ROW    - (y) - %d \n",cell->pos.y);
  switch(cell->type)
  {
    case CELL_WALL:
      fprintf(stderr, "CELL TYPE: CELL_WALL");
      break;
    case CELL_FLOOR:
      fprintf(stderr, "CELL TYPE: CELL_FLOOR");
      break;
    case CELL_HOME:
      fprintf(stderr, "CELL TYPE: CELL_HOME");
      break;
    case CELL_JAIL:
      fprintf(stderr, "CELL TYPE: CELL_JAIL");
      break;
    default:
      fprintf(stderr, "CELL TYPE: UNKWOWN");
      break;
  }
  fprintf(stderr, "\n");
  fprintf(stderr, "CELL TURF: %d",cell->turf+1);
  fprintf(stderr, "\n");
  switch(cell->cell_state)
  {
    case CELLSTATE_EMPTY:
      fprintf(stderr, "CELLSTATE: EMPTY");
      break;
    case CELLSTATE_HOLDING:
      fprintf(stderr, "CELLSTATE: HOLDING");
      break;
    case CELLSTATE_OCCUPIED:
      fprintf(stderr, "CELLSTATE: OCCUPIED\n");
      break;
    case CELLSTATE_OCCUPIED_HOLDING:
      fprintf(stderr, "CELLSTATE: OCCUPIED AND HOLDING");
      break;
    default:
      fprintf(stderr, "CELLSTATE: UNKWOWN");
      break;
  }
  fprintf(stderr, "\n");
}

variable_types: struct CellStruct { struct Pos { unsigned short x; unsigned short y; } pos; enum Cell_Types { CELL_WALL=0, CELL_FLOOR=1, CELL_JAIL=2, CELL_HOME=3 } type; enum Cell_State_Types { CELLSTATE_EMPTY=0, CELLSTATE_OCCUPIED=1, CELLSTATE_HOLDING=2, CELLSTATE_OCCUPIED_HOLDING=3 } cell_state; enum Team_Types { TEAM_RED=0, TEAM_BLUE=1 } turf; enum Mutable_Types { CELLTYPE_IMMUTABLE=0, CELLTYPE_MUTABLE=1 } is_mutable; struct PlayerStruct { struct CellStruct *cell; struct Object { struct CellStruct *cell; struct PlayerStruct *player; enum Object_Types { OBJECT_FLAG=0, OBJECT_SHOVEL=1 } type; enum Team_Types { TEAM_RED=0, TEAM_BLUE=1 } team; enum Visible_Types { OBJECT_VISIBLE=0, OBJECT_INVISIBLE=1 } visiblity; struct Pos client_position; int client_player_id; enum Team_Types { TEAM_RED=0, TEAM_BLUE=1 } client_player_team; int client_has_player; 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; } lock; unsigned int thread; } *shovel; struct Object *flag; enum Player_State_Types { PLAYER_FREE=0, PLAYER_JAILED=1 } state; enum Team_Types { TEAM_RED=0, TEAM_BLUE=1 } team; unsigned short id; int fd; struct Pos client_position; union pthread_mutex_t lock; unsigned int thread; } *player; struct Object *object; union pthread_mutex_t lock; unsigned int thread; } *

return_type: void

user_defined_types: struct CellStruct { struct Pos pos; enum Cell_Types type; enum Cell_State_Types cell_state; enum Team_Types turf; enum Mutable_Types is_mutable; struct PlayerStruct *player; struct Object *object; union pthread_mutex_t lock; unsigned int thread; }

user_defined_types: struct PlayerStruct { struct CellStruct *cell; struct Object *shovel; struct Object *flag; enum Player_State_Types state; enum Team_Types team; unsigned short id; int fd; struct Pos client_position; union pthread_mutex_t lock; unsigned int thread; }

user_defined_types: struct Object { struct CellStruct *cell; struct PlayerStruct *player; enum Object_Types type; enum Team_Types team; enum Visible_Types visiblity; struct Pos client_position; int client_player_id; enum Team_Types client_player_team; int client_has_player; union pthread_mutex_t lock; unsigned int thread; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; 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 proto_dump_mt(unsigned int a1)
{
int result; // eax

switch ( a1 )
{
case 0u:
result = fwrite("PROTO_MT_REQ_BASE_RESERVED_FIRST", 1uLL, 0x20uLL, stderr);
break;
case 1u:
result = fwrite("PROTO_MT_REQ_BASE_HELLO", 1uLL, 0x17uLL, stderr);
break;
case 2u:
result = fwrite("PROTO_MT_REQ_BASE_GOODBYE", 1uLL, 0x19uLL, stderr);
break;
case 3u:
result = fwrite("PROTO_MT_REQ_ACTION", 1uLL, 0x13uLL, stderr);
break;
case 4u:
result = fwrite("PROTO_MT_REQ_SYNC", 1uLL, 0x11uLL, stderr);
break;
case 5u:
result = fwrite("PROTO_MT_REQ_BASE_RESERVED_LAST", 1uLL, 0x1FuLL, stderr);
break;
case 6u:
result = fwrite("PROTO_MT_REP_BASE_RESERVED_FIRST", 1uLL, 0x20uLL, stderr);
break;
case 7u:
result = fwrite("PROTO_MT_REP_BASE_HELLO", 1uLL, 0x17uLL, stderr);
break;
case 8u:
result = fwrite("PROTO_MT_REP_BASE_GOODBYE", 1uLL, 0x19uLL, stderr);
break;
case 9u:
result = fwrite("PROTO_MT_REP_ACTION", 1uLL, 0x13uLL, stderr);
break;
case 0xAu:
result = fwrite("PROTO_MT_REP_SYNC", 1uLL, 0x11uLL, stderr);
break;
case 0xBu:
result = fwrite("PROTO_MT_REP_BASE_RESERVED_LAST", 1uLL, 0x1FuLL, stderr);
break;
case 0xCu:
case 0xEu:
result = fwrite("PROTO_MT_EVENT_BASE_RESERVED_LAST", 1uLL, 0x21uLL, stderr);
break;
case 0xDu:
result = fwrite("PROTO_MT_EVENT_BASE_UPDATE", 1uLL, 0x1AuLL, stderr);
break;
default:
result = fprintf(stderr, "UNKNOWN=%d", a1);
break;
}
return result;
}

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

switch ( a1 )
{
case 0u:
result = fwrite("PROTO_MT_REQ_BASE_RESERVED_FIRST", 1uLL, 0x20uLL, stderr);
break;
case 1u:
result = fwrite("PROTO_MT_REQ_BASE_HELLO", 1uLL, 0x17uLL, stderr);
break;
case 2u:
result = fwrite("PROTO_MT_REQ_BASE_GOODBYE", 1uLL, 0x19uLL, stderr);
break;
case 3u:
result = fwrite("PROTO_MT_REQ_ACTION", 1uLL, 0x13uLL, stderr);
break;
case 4u:
result = fwrite("PROTO_MT_REQ_SYNC", 1uLL, 0x11uLL, stderr);
break;
case 5u:
result = fwrite("PROTO_MT_REQ_BASE_RESERVED_LAST", 1uLL, 0x1FuLL, stderr);
break;
case 6u:
result = fwrite("PROTO_MT_REP_BASE_RESERVED_FIRST", 1uLL, 0x20uLL, stderr);
break;
case 7u:
result = fwrite("PROTO_MT_REP_BASE_HELLO", 1uLL, 0x17uLL, stderr);
break;
case 8u:
result = fwrite("PROTO_MT_REP_BASE_GOODBYE", 1uLL, 0x19uLL, stderr);
break;
case 9u:
result = fwrite("PROTO_MT_REP_ACTION", 1uLL, 0x13uLL, stderr);
break;
case 0xAu:
result = fwrite("PROTO_MT_REP_SYNC", 1uLL, 0x11uLL, stderr);
break;
case 0xBu:
result = fwrite("PROTO_MT_REP_BASE_RESERVED_LAST", 1uLL, 0x1FuLL, stderr);
break;
case 0xCu:
case 0xEu:
result = fwrite("PROTO_MT_EVENT_BASE_RESERVED_LAST", 1uLL, 0x21uLL, stderr);
break;
case 0xDu:
result = fwrite("PROTO_MT_EVENT_BASE_UPDATE", 1uLL, 0x1AuLL, stderr);
break;
default:
result = fprintf(stderr, "UNKNOWN=%d", a1);
break;
}
return result;
}

original_code:
extern void
proto_dump_mt(Proto_Msg_Types type)
{
  switch (type) {
  case PROTO_MT_REQ_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_REQ_BASE_RESERVED_FIRST");
    break;
  case PROTO_MT_REQ_HELLO:
    fprintf(stderr, "PROTO_MT_REQ_BASE_HELLO");
    break;
  case PROTO_MT_REQ_ACTION:
    fprintf(stderr, "PROTO_MT_REQ_ACTION");
    break;
  case PROTO_MT_REQ_SYNC:
    fprintf(stderr, "PROTO_MT_REQ_SYNC");
    break;
  case PROTO_MT_REQ_GOODBYE:
    fprintf(stderr, "PROTO_MT_REQ_BASE_GOODBYE");
    break;
  case PROTO_MT_REQ_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_REQ_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_REP_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_REP_BASE_RESERVED_FIRST");
    break;
  case PROTO_MT_REP_HELLO:
    fprintf(stderr, "PROTO_MT_REP_BASE_HELLO");
    break;
  case PROTO_MT_REP_ACTION:
    fprintf(stderr, "PROTO_MT_REP_ACTION");
    break;
  case PROTO_MT_REP_SYNC:
    fprintf(stderr, "PROTO_MT_REP_SYNC");
    break;
  case PROTO_MT_REP_GOODBYE:
    fprintf(stderr, "PROTO_MT_REP_BASE_GOODBYE");
    break;
  case PROTO_MT_REP_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_REP_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_EVENT_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_EVENT_UPDATE:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_UPDATE");
    break;
  case PROTO_MT_EVENT_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_RESERVED_LAST");
    break;
  default:
    fprintf(stderr, "UNKNOWN=%d", type);
  }
}

canonical_original_code:
extern void proto_dump_mt(enum Proto_Msg_Types type)
{
  switch (type) {
  case PROTO_MT_REQ_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_REQ_BASE_RESERVED_FIRST");
    break;
  case PROTO_MT_REQ_HELLO:
    fprintf(stderr, "PROTO_MT_REQ_BASE_HELLO");
    break;
  case PROTO_MT_REQ_ACTION:
    fprintf(stderr, "PROTO_MT_REQ_ACTION");
    break;
  case PROTO_MT_REQ_SYNC:
    fprintf(stderr, "PROTO_MT_REQ_SYNC");
    break;
  case PROTO_MT_REQ_GOODBYE:
    fprintf(stderr, "PROTO_MT_REQ_BASE_GOODBYE");
    break;
  case PROTO_MT_REQ_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_REQ_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_REP_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_REP_BASE_RESERVED_FIRST");
    break;
  case PROTO_MT_REP_HELLO:
    fprintf(stderr, "PROTO_MT_REP_BASE_HELLO");
    break;
  case PROTO_MT_REP_ACTION:
    fprintf(stderr, "PROTO_MT_REP_ACTION");
    break;
  case PROTO_MT_REP_SYNC:
    fprintf(stderr, "PROTO_MT_REP_SYNC");
    break;
  case PROTO_MT_REP_GOODBYE:
    fprintf(stderr, "PROTO_MT_REP_BASE_GOODBYE");
    break;
  case PROTO_MT_REP_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_REP_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_EVENT_BASE_RESERVED_FIRST:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_RESERVED_LAST");
    break;
  case PROTO_MT_EVENT_UPDATE:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_UPDATE");
    break;
  case PROTO_MT_EVENT_BASE_RESERVED_LAST:
    fprintf(stderr, "PROTO_MT_EVENT_BASE_RESERVED_LAST");
    break;
  default:
    fprintf(stderr, "UNKNOWN=%d", type);
  }
}

variable_types: enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 }


decompiled_code:
int __fastcall proto_dump_pstate(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
__int64 v3; // rdx
unsigned int v4; // eax
__int64 v5; // rdx
__int64 v6; // rcx
__int64 v7; // r8

_bswap_32(*a1);
_bswap_32(*(_DWORD *)(v1 + 4));
_bswap_32(*(_DWORD *)(v2 + 8));
v4 = _bswap_32(*(_DWORD *)(v3 + 12));
return fprintf(stderr, "v0=x0%x v1=0x%x v2=0x%x v3=0x%x\n", v5, v6, v7, v4);
}

canonical_decompiled_code:
int __fastcall func6(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
__int64 v3; // rdx
unsigned int v4; // eax
__int64 v5; // rdx
__int64 v6; // rcx
__int64 v7; // r8

func2(*a1);
func2(*(_DWORD *)(v1 + 4));
func2(*(_DWORD *)(v2 + 8));
v4 = func2(*(_DWORD *)(v3 + 12));
return fprintf(stderr, "v0=x0%x v1=0x%x v2=0x%x v3=0x%x\n", v5, v6, v7, v4);
}

original_code:
extern void
proto_dump_pstate(Proto_Player_State *ps)
{
  int v0, v1, v2, v3;
  v0 = ntohl(ps->v0.raw);
  v1 = ntohl(ps->v1.raw);
  v2 = ntohl(ps->v2.raw);
  v3 = ntohl(ps->v3.raw);
  fprintf(stderr, "v0=x0%x v1=0x%x v2=0x%x v3=0x%x\n",
   v0, v1, v2, v3);
}

canonical_original_code:
extern void proto_dump_pstate(struct Proto_Player_State *ps)
{
  int v0; int v1; int v2; int v3;
  v0 = ntohl(ps->v0.raw);
  v1 = ntohl(ps->v1.raw);
  v2 = ntohl(ps->v2.raw);
  v3 = ntohl(ps->v3.raw);
  fprintf(stderr, "v0=x0%x v1=0x%x v2=0x%x v3=0x%x\n",
   v0, v1, v2, v3);
}

variable_types: struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } *

user_defined_types: struct Proto_Player_State { union Proto_PV0 v0; union Proto_PV1 v1; union Proto_PV2 v2; union Proto_PV3 v3; }


decompiled_code:
int __fastcall proto_dump_gstate(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
unsigned int v3; // eax
__int64 v4; // rdx
__int64 v5; // rcx

_bswap_32(*a1);
_bswap_32(*(_DWORD *)(v1 + 4));
v3 = _bswap_32(*(_DWORD *)(v2 + 8));
return fprintf(stderr, "v0=0x%x v1=0x%x v2=0x%x\n", v4, v5, v3);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
unsigned int v3; // eax
__int64 v4; // rdx
__int64 v5; // rcx

func2(*a1);
func2(*(_DWORD *)(v1 + 4));
v3 = func2(*(_DWORD *)(v2 + 8));
return fprintf(stderr, "v0=0x%x v1=0x%x v2=0x%x\n", v4, v5, v3);
}

original_code:
extern void
proto_dump_gstate(Proto_Game_State *gs)
{
  int v0, v1, v2;
  v0 = ntohl(gs->v0.raw);
  v1 = ntohl(gs->v1.raw);
  v2 = ntohl(gs->v2.raw);
  fprintf(stderr, "v0=0x%x v1=0x%x v2=0x%x\n",
   v0, v1, v2);
}

canonical_original_code:
extern void proto_dump_gstate(struct Proto_Game_State *gs)
{
  int v0; int v1; int v2;
  v0 = ntohl(gs->v0.raw);
  v1 = ntohl(gs->v1.raw);
  v2 = ntohl(gs->v2.raw);
  fprintf(stderr, "v0=0x%x v1=0x%x v2=0x%x\n",
   v0, v1, v2);
}

variable_types: struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } *

user_defined_types: struct Proto_Game_State { union Proto_GV0 v0; union Proto_GV1 v1; union Proto_GV2 v2; }


decompiled_code:
int __fastcall proto_dump_msghdr(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned __int64 v3; // rax
unsigned int v4; // eax

v1 = _bswap_32(*(_DWORD *)a1);
fprintf(stderr, "ver=%d type=", v1);
v2 = _bswap_32(*(_DWORD *)(a1 + 4));
proto_dump_mt(v2);
v3 = _fswab64(*(_QWORD *)(a1 + 8));
fprintf(stderr, " sver=%llx", v3);
fwrite(" pstate:", 1uLL, 8uLL, stderr);
proto_dump_pstate((unsigned int *)(a1 + 16));
fwrite(" gstate:", 1uLL, 8uLL, stderr);
proto_dump_gstate((unsigned int *)(a1 + 32));
v4 = _bswap_32(*(_DWORD *)(a1 + 44));
return fprintf(stderr, " blen=%d\n", v4);
}

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

v1 = func2(*(_DWORD *)a1);
fprintf(stderr, "ver=%d type=", v1);
v2 = func2(*(_DWORD *)(a1 + 4));
func5(v2);
v3 = func1(*(_QWORD *)(a1 + 8));
fprintf(stderr, " sver=%llx", v3);
fwrite(" pstate:", 1uLL, 8uLL, stderr);
func6((unsigned int *)(a1 + 16));
fwrite(" gstate:", 1uLL, 8uLL, stderr);
func7((unsigned int *)(a1 + 32));
v4 = func2(*(_DWORD *)(a1 + 44));
return fprintf(stderr, " blen=%d\n", v4);
}

canonical_original_code:
extern void proto_dump_msghdr(struct Proto_Msg_Hdr *hdr)
{
  fprintf(stderr, "ver=%d type=", ntohl(hdr->version));
  proto_dump_mt(ntohl(hdr->type));
  fprintf(stderr, " sver=%llx", (__builtin_constant_p((__u64)((__u64)(__be64)(hdr->sver.raw))) ? ((__u64)( (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x00000000000000ffULL) << 56) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x000000000000ff00ULL) << 40) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x0000000000ff0000ULL) << 24) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x00000000ff000000ULL) << 8) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x000000ff00000000ULL) >> 8) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x0000ff0000000000ULL) >> 24) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0x00ff000000000000ULL) >> 40) | (((__u64)((__u64)(__be64)(hdr->sver.raw)) & (unsigned long long)0xff00000000000000ULL) >> 56))) : __fswab64((__u64)(__be64)(hdr->sver.raw))));
  fprintf(stderr, " pstate:");
  proto_dump_pstate(&(hdr->pstate));
  fprintf(stderr, " gstate:");
  proto_dump_gstate(&(hdr->gstate));
  fprintf(stderr, " blen=%d\n", ntohl(hdr->blen));
}

variable_types: struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } *

user_defined_types: struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types type; union Proto_StateVersion sver; struct Proto_Player_State pstate; struct Proto_Game_State gstate; int blen; }


decompiled_code:
__int64 proto_debug()
{
return PROTO_DEBUG;
}

canonical_decompiled_code:
__int64 func0()
{
return PROTO_DEBUG;
}

original_code: static inline int proto_debug(void) {return PROTO_DEBUG; }

canonical_original_code: static inline int proto_debug() {return PROTO_DEBUG; }


decompiled_code:
__int64 __fastcall proto_server_record_event_subscriber(int a1, int *a2)
{
int v2; // edx
__int64 i; // rax
unsigned int v4; // er12

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v2 = *((_DWORD *)&Proto_Server + 16);
if ( v2 > 1023 )
{
for ( i = 0LL; i != 1024; ++i )
{
LABEL_5:
if ( *((_DWORD *)&Proto_Server + i + 18) == -1 )
{
++*((_DWORD *)&Proto_Server + 17);
v4 = 1;
*((_DWORD *)&Proto_Server + (int)i + 18) = a1;
*((_DWORD *)&Proto_Server + 16) = i + 1;
*a2 = i;
goto LABEL_7;
}
}
v4 = -1;
}
else
{
i = 0LL;
if ( *((_DWORD *)&Proto_Server + v2 + 18) != -1 )
goto LABEL_5;
++*((_DWORD *)&Proto_Server + 17);
v4 = 1;
*((_DWORD *)&Proto_Server + v2 + 18) = a1;
*a2 = v2;
++*((_DWORD *)&Proto_Server + 16);
}
LABEL_7:
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
return v4;
}

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

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v2 = *((_DWORD *)&Proto_Server + 16);
if ( v2 > 1023 )
{
for ( i = 0LL; i != 1024; ++i )
{
LABEL_5:
if ( *((_DWORD *)&Proto_Server + i + 18) == -1 )
{
++*((_DWORD *)&Proto_Server + 17);
v4 = 1;
*((_DWORD *)&Proto_Server + (int)i + 18) = a1;
*((_DWORD *)&Proto_Server + 16) = i + 1;
*a2 = i;
goto LABEL_7;
}
}
v4 = -1;
}
else
{
i = 0LL;
if ( *((_DWORD *)&Proto_Server + v2 + 18) != -1 )
goto LABEL_5;
++*((_DWORD *)&Proto_Server + 17);
v4 = 1;
*((_DWORD *)&Proto_Server + v2 + 18) = a1;
*a2 = v2;
++*((_DWORD *)&Proto_Server + 16);
}
LABEL_7:
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
return v4;
}

original_code:
static int
proto_server_record_event_subscriber(int fd, int *num)
{
  int rc=-1;
  pthread_mutex_lock(&Proto_Server.EventSubscribersLock);
  if (Proto_Server.EventLastSubscriber < 1024
      && Proto_Server.EventSubscribers[Proto_Server.EventLastSubscriber]
      ==-1) {
   Proto_Server.EventSubscribers[ Proto_Server.EventLastSubscriber] = fd;
   Proto_Server.EventNumSubscribers++;
   *num = Proto_Server.EventLastSubscriber;
   Proto_Server.EventLastSubscriber++;
    rc = 1;
  } else {
    int i;
    for (i=0; i< 1024; i++) {
      if (Proto_Server.EventSubscribers[i]==-1) {
   Proto_Server.EventLastSubscriber = i ;
   Proto_Server.EventSubscribers[ Proto_Server.EventLastSubscriber] = fd;
   Proto_Server.EventNumSubscribers++;
   Proto_Server.EventLastSubscriber++;
   *num=i;
   rc=1;
   break;
      }
    }
  }
  pthread_mutex_unlock(&Proto_Server.EventSubscribersLock);
  return rc;
}

canonical_original_code:
static int proto_server_record_event_subscriber(int fd, int *num)
{
  int rc = -1;
  pthread_mutex_lock(&Proto_Server.EventSubscribersLock);
  if (Proto_Server.EventLastSubscriber < 1024
      && Proto_Server.EventSubscribers[Proto_Server.EventLastSubscriber]
      ==-1) {
   Proto_Server.EventSubscribers[ Proto_Server.EventLastSubscriber] = fd;
   Proto_Server.EventNumSubscribers++;
   *num = Proto_Server.EventLastSubscriber;
   Proto_Server.EventLastSubscriber++;
    rc = 1;
  } else {
    int i;
    for (i=0; i< 1024; i++) {
      if (Proto_Server.EventSubscribers[i]==-1) {
   Proto_Server.EventLastSubscriber = i ;
   Proto_Server.EventSubscribers[ Proto_Server.EventLastSubscriber] = fd;
   Proto_Server.EventNumSubscribers++;
   Proto_Server.EventLastSubscriber++;
   *num=i;
   rc=1;
   break;
      }
    }
  }
  pthread_mutex_unlock(&Proto_Server.EventSubscribersLock);
  return rc;
}

variable_types: fd int

variable_types:
num
int *

variable_types: i int


decompiled_code:
void __fastcall __noreturn proto_server_rpc_listen(void *a1)
{
unsigned int v1; // ebx
int v2; // er12
int *v3; // rax
unsigned int *v4; // rbp
int i; // eax
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = Proto_Server.__lock;
v2 = net_listen((unsigned int)Proto_Server.__lock);
v3 = _errno_location();
v4 = (unsigned int *)v3;
if ( v2 >= 0 )
{
while ( 1 )
{
for ( i = net_accept(v1); i < 0; i = net_accept(v1) )
fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", *v4);
pthread_create(newthread, 0LL, proto_server_req_dispatcher, (void *)i);
}
}
fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", (unsigned int)*v3);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
unsigned int v1; // ebx
int v2; // er12
int *v3; // rax
unsigned int *v4; // rbp
int i; // eax
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = Proto_Server.__lock;
v2 = net_listen((unsigned int)Proto_Server.__lock);
v3 = _errno_location();
v4 = (unsigned int *)v3;
if ( v2 >= 0 )
{
while ( 1 )
{
for ( i = net_accept(v1); i < 0; i = net_accept(v1) )
fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", *v4);
pthread_create(newthread, 0LL, proto_server_req_dispatcher, (void *)i);
}
}
fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", (unsigned int)*v3);
exit(-1);
}

original_code:
static
void *
proto_server_rpc_listen(void *arg)
{
  int fd = Proto_Server.RPCListenFD;
  int connfd;
  pthread_t tid;
  if (net_listen(fd) < 0) {
    fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", (*__errno_location ()));
    exit(-1);
  }
  for (;;) {
   connfd = net_accept(fd);
    if (connfd < 0) {
      fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", (*__errno_location ()));
    } else {
      pthread_create(&tid, ((void *)0), &proto_server_req_dispatcher,
       (void *)(size_t)connfd);
    }
  }
}

canonical_original_code:
static void *proto_server_rpc_listen(void *arg)
{
  int fd = Proto_Server.RPCListenFD;
  int connfd;
  unsigned long tid;
  if (net_listen(fd) < 0) {
    fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", (*__errno_location ()));
    exit(-1);
  }
  for (;;) {
   connfd = net_accept(fd);
    if (connfd < 0) {
      fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", (*__errno_location ()));
    } else {
      pthread_create(&tid, ((void *)0), &proto_server_req_dispatcher,
       (void *)(unsigned long)connfd);
    }
  }
}

variable_types: connfd int

variable_types:
tid
unsigned long


decompiled_code:
void __fastcall __noreturn proto_server_event_listen(void *a1)
{
unsigned int v1; // ebx
int *v2; // r12
int v3; // ebp
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = Proto_Server.__owner;
if ( (int)net_listen(v1) < 0 )
exit(-1);
v2 = _errno_location();
while ( 1 )
{
LABEL_3:
while ( 1 )
{
v3 = net_accept(v1);
if ( v3 >= 0 )
break;
LABEL_8:
fprintf(stderr, "Error: EventListen accept failed (%d)\n", (unsigned int)*v2);
}
while ( 1 )
{
if ( !(unsigned int)proto_debug() )
{
if ( (int)proto_server_record_event_subscriber(v3, v4) < 0 )
break;
goto LABEL_6;
}
fprintf(stderr, "EventListen: connfd=%d -> ", (unsigned int)v3);
if ( (int)proto_server_record_event_subscriber(v3, v4) < 0 )
break;
LABEL_6:
if ( !(unsigned int)proto_debug() )
goto LABEL_3;
fprintf(stderr, "subscriber num %d\n", (unsigned int)v4[0]);
v3 = net_accept(v1);
if ( v3 < 0 )
goto LABEL_8;
}
fwrite("oops no space for any more event subscribers\n", 1uLL, 0x2DuLL, stderr);
close(v3);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
unsigned int v1; // ebx
int *v2; // r12
int v3; // ebp
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = Proto_Server.__owner;
if ( (int)net_listen(v1) < 0 )
exit(-1);
v2 = _errno_location();
while ( 1 )
{
LABEL_3:
while ( 1 )
{
v3 = net_accept(v1);
if ( v3 >= 0 )
break;
LABEL_8:
fprintf(stderr, "Error: EventListen accept failed (%d)\n", (unsigned int)*v2);
}
while ( 1 )
{
if ( !(unsigned int)func0() )
{
if ( (int)func1(v3, v4) < 0 )
break;
goto LABEL_6;
}
fprintf(stderr, "EventListen: connfd=%d -> ", (unsigned int)v3);
if ( (int)func1(v3, v4) < 0 )
break;
LABEL_6:
if ( !(unsigned int)func0() )
goto LABEL_3;
fprintf(stderr, "subscriber num %d\n", (unsigned int)v4[0]);
v3 = net_accept(v1);
if ( v3 < 0 )
goto LABEL_8;
}
fwrite("oops no space for any more event subscribers\n", 1uLL, 0x2DuLL, stderr);
close(v3);
}
}

original_code:
static
void *
proto_server_event_listen(void *arg)
{
  int fd = Proto_Server.EventListenFD;
  int connfd;
  if (net_listen(fd)<0) {
    exit(-1);
  }
  for (;;) {
    connfd = net_accept(fd);
    if (connfd < 0) {
      fprintf(stderr, "Error: EventListen accept failed (%d)\n", (*__errno_location ()));
    } else {
      int i;
      if (proto_debug()) fprintf(stderr, "EventListen: connfd=%d -> ", connfd);
   if ( proto_server_record_event_subscriber( connfd, &i )<0) {
 fprintf(stderr, "oops no space for any more event subscribers\n");
 close(connfd);
      } else {
 if ( proto_debug() ) fprintf(stderr, "subscriber num %d\n", i);
      }
    }
  }
}

canonical_original_code:
static void *proto_server_event_listen(void *arg)
{
  int fd = Proto_Server.EventListenFD;
  int connfd;
  if (net_listen(fd)<0) {
    exit(-1);
  }
  for (;;) {
    connfd = net_accept(fd);
    if (connfd < 0) {
      fprintf(stderr, "Error: EventListen accept failed (%d)\n", (*__errno_location ()));
    } else {
      int i;
      if (proto_debug()) fprintf(stderr, "EventListen: connfd=%d -> ", connfd);
   if ( proto_server_record_event_subscriber( connfd, &i )<0) {
 fprintf(stderr, "oops no space for any more event subscribers\n");
 close(connfd);
      } else {
 if ( proto_debug() ) fprintf(stderr, "subscriber num %d\n", i);
      }
    }
  }
}


decompiled_code:
void *__fastcall proto_server_req_dispatcher(void *a1)
{
int (__fastcall *v1)(int *); // rbp
const void *v2; // r12
__int64 v3; // rax
unsigned int v4; // ebx
unsigned int v5; // eax
int fd[32806]; // [rsp+0h] [rbp-20098h] BYREF

v2 = (const void *)pthread_self();
pthread_detach((pthread_t)v2);
proto_session_init(fd);
fd[0] = (int)a1;
if ( (unsigned int)proto_debug() )
fprintf(stderr, "proto_rpc_dispatcher: %p: Started: fd=%d\n", v2, (unsigned int)a1);
do
{
if ( (unsigned int)proto_session_rcv_msg(fd) != 1 )
break;
v4 = proto_session_hdr_unmarshall_type(fd);
if ( (unsigned int)proto_debug() )
{
fprintf(stderr, "proto_rpc_dispatcher: mt=%d \n", v4);
if ( (unsigned int)proto_debug() )
{
v5 = proto_session_hdr_unmarshall_type(fd);
fprintf(stderr, "proto_rpc_dispatcher: type=%d \n", v5);
}
}
v3 = v4 - 1;
if ( (unsigned int)v3 <= 3 )
v1 = (int (__fastcall *)(int *))*((_QWORD *)&Proto_Server + v3 + 16922);
}
while ( v1(fd) >= 0 );
(*((void (__fastcall **)(int *))&Proto_Server + 16921))(fd);
close(fd[0]);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
int (__fastcall *v1)(int *); // rbp
const void *v2; // r12
__int64 v3; // rax
unsigned int v4; // ebx
unsigned int v5; // eax
int fd[32806]; // [rsp+0h] [rbp-20098h] BYREF

v2 = (const void *)pthread_self();
pthread_detach((pthread_t)v2);
proto_session_init(fd);
fd[0] = (int)a1;
if ( (unsigned int)func0() )
fprintf(stderr, "proto_rpc_dispatcher: %p: Started: fd=%d\n", v2, (unsigned int)a1);
do
{
if ( (unsigned int)proto_session_rcv_msg(fd) != 1 )
break;
v4 = proto_session_hdr_unmarshall_type(fd);
if ( (unsigned int)func0() )
{
fprintf(stderr, "proto_rpc_dispatcher: mt=%d \n", v4);
if ( (unsigned int)func0() )
{
v5 = proto_session_hdr_unmarshall_type(fd);
fprintf(stderr, "proto_rpc_dispatcher: type=%d \n", v5);
}
}
v3 = v4 - 1;
if ( (unsigned int)v3 <= 3 )
v1 = (int (__fastcall *)(int *))*((_QWORD *)&Proto_Server + v3 + 16922);
}
while ( v1(fd) >= 0 );
(*((void (__fastcall **)(int *))&Proto_Server + 16921))(fd);
close(fd[0]);
return 0LL;
}

original_code:
static void *
proto_server_req_dispatcher(void * arg)
{
  Proto_Session s;
  Proto_Msg_Types mt;
  Proto_MT_Handler hdlr;
  unsigned long arg_value = (unsigned long) arg;
  pthread_detach(pthread_self());
  proto_session_init(&s);
  s.fd = (FDType) arg_value;
  if(proto_debug()) fprintf(stderr, "proto_rpc_dispatcher: %p: Started: fd=%d\n",
   (void *)pthread_self(), s.fd);
  for (;;) {
    if (proto_session_rcv_msg(&s)==1) {
   mt = (Proto_Msg_Types) proto_session_hdr_unmarshall_type(&s);
  if (proto_debug() ) fprintf(stderr,"proto_rpc_dispatcher: mt=%d \n",mt);
  if (proto_debug() ) fprintf(stderr,"proto_rpc_dispatcher: type=%d \n", proto_session_hdr_unmarshall_type(&s));
  if ( PROTO_MT_REQ_BASE_RESERVED_FIRST < mt && mt < PROTO_MT_REQ_BASE_RESERVED_LAST )
  {
    hdlr = Proto_Server.base_req_handlers[mt-PROTO_MT_REQ_BASE_RESERVED_FIRST-1];
  }
 if (hdlr(&s)<0) goto leave;
      }
     else {
      goto leave;
    }
  }
 leave:
  Proto_Server.session_lost_handler(&s);
  close(s.fd);
  return ((void *)0);
}

canonical_original_code:
static void *proto_server_req_dispatcher(void *arg)
{
  struct Proto_Session s;
  enum Proto_Msg_Types mt;
  int (*hdlr)(struct Proto_Session *);
  unsigned long arg_value = (unsigned long) arg;
  pthread_detach(pthread_self());
  proto_session_init(&s);
  s.fd = (int) arg_value;
  if(proto_debug()) fprintf(stderr, "proto_rpc_dispatcher: %p: Started: fd=%d\n",
   (void *)pthread_self(), s.fd);
  for (;;) {
    if (proto_session_rcv_msg(&s)==1) {
   mt = (enum Proto_Msg_Types) proto_session_hdr_unmarshall_type(&s);
  if (proto_debug() ) fprintf(stderr,"proto_rpc_dispatcher: mt=%d \n",mt);
  if (proto_debug() ) fprintf(stderr,"proto_rpc_dispatcher: type=%d \n", proto_session_hdr_unmarshall_type(&s));
  if ( PROTO_MT_REQ_BASE_RESERVED_FIRST < mt && mt < PROTO_MT_REQ_BASE_RESERVED_LAST )
  {
    hdlr = Proto_Server.base_req_handlers[mt-PROTO_MT_REQ_BASE_RESERVED_FIRST-1];
  }
 if (hdlr(&s)<0) goto leave;
      }
     else {
      goto leave;
    }
  }
 leave:
  Proto_Server.session_lost_handler(&s);
  close(s.fd);
  return ((void *)0);
}

variable_types: int (*)(struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; } *)

user_defined_types: struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; }


decompiled_code:
__int64 __fastcall proto_session_lost_default_handler(__int64 a1)
{
fwrite("Session lost...:\n", 1uLL, 0x11uLL, stderr);
proto_session_dump(a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
fwrite("Session lost...:\n", 1uLL, 0x11uLL, stderr);
proto_session_dump(a1);
return 0xFFFFFFFFLL;
}

original_code:
static int
proto_session_lost_default_handler(Proto_Session *s)
{
  fprintf(stderr, "Session lost...:\n");
  proto_session_dump(s);
  return -1;
}

canonical_original_code:
static int proto_session_lost_default_handler(struct Proto_Session *s)
{
  fprintf(stderr, "Session lost...:\n");
  proto_session_dump(s);
  return -1;
}

variable_types: struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; } *


decompiled_code:
__int64 __fastcall proto_server_mt_null_handler(__int64 a1)
{
__int64 v2; // [rsp+0h] [rbp-38h] BYREF

fwrite("proto_server_mt_null_handler: invoked for session:\n", 1uLL, 0x33uLL, stderr);
proto_session_dump(a1);
LODWORD(v2) = 0;
HIDWORD(v2) = proto_session_hdr_unmarshall_type(a1) + 6;
proto_session_hdr_marshall(a1, &v2);
proto_session_body_marshall_int(a1, 3735928559LL);
return proto_session_send_msg(a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v2; // [rsp+0h] [rbp-38h] BYREF

fwrite("proto_server_mt_null_handler: invoked for session:\n", 1uLL, 0x33uLL, stderr);
proto_session_dump(a1);
LODWORD(v2) = 0;
HIDWORD(v2) = proto_session_hdr_unmarshall_type(a1) + 6;
proto_session_hdr_marshall(a1, &v2);
proto_session_body_marshall_int(a1, 3735928559LL);
return proto_session_send_msg(a1, 1LL);
}

original_code:
static int
proto_server_mt_null_handler(Proto_Session *s)
{
  int rc=1;
  Proto_Msg_Hdr h;
  fprintf(stderr, "proto_server_mt_null_handler: invoked for session:\n");
  proto_session_dump(s);
  bzero(&h, sizeof(s));
  h.type = proto_session_hdr_unmarshall_type(s);
  h.type += PROTO_MT_REP_BASE_RESERVED_FIRST;
  proto_session_hdr_marshall(s, &h);
  proto_session_body_marshall_int(s, 0xdeadbeef);
  rc=proto_session_send_msg(s,1);
  return rc;
}

canonical_original_code:
static int proto_server_mt_null_handler(struct Proto_Session *s)
{
  int rc = 1;
  struct Proto_Msg_Hdr h;
  fprintf(stderr, "proto_server_mt_null_handler: invoked for session:\n");
  proto_session_dump(s);
  bzero(&h, sizeof(s));
  h.type = proto_session_hdr_unmarshall_type(s);
  h.type += PROTO_MT_REP_BASE_RESERVED_FIRST;
  proto_session_hdr_marshall(s, &h);
  proto_session_body_marshall_int(s, 0xdeadbeef);
  rc=proto_session_send_msg(s,1);
  return rc;
}

variable_types: struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; }


decompiled_code:
__int64 proto_server_rpcport()
{
return Proto_Server.__count;
}

canonical_decompiled_code:
__int64 func7()
{
return Proto_Server.__count;
}

original_code: extern PortType proto_server_rpcport(void) { return Proto_Server.RPCPort; }

canonical_original_code: extern int proto_server_rpcport() { return Proto_Server.RPCPort; }


decompiled_code:
__int64 proto_server_eventport()
{
return Proto_Server.__nusers;
}

canonical_decompiled_code:
__int64 func8()
{
return Proto_Server.__nusers;
}

original_code: extern PortType proto_server_eventport(void) { return Proto_Server.EventPort; }

canonical_original_code: extern int proto_server_eventport() { return Proto_Server.EventPort; }


decompiled_code:
__int64 proto_server_event_session()
{
return (unsigned int)((_DWORD)&Proto_Server + 4168);
}

canonical_decompiled_code:
__int64 func9()
{
return (unsigned int)((_DWORD)&Proto_Server + 4168);
}

original_code:
extern Proto_Session *
proto_server_event_session(void)
{
  return &Proto_Server.EventSession;
}

canonical_original_code:
extern struct Proto_Session *proto_server_event_session()
{
  return &Proto_Server.EventSession;
}

return_type: struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; } *


decompiled_code:
__int64 __fastcall proto_server_set_session_lost_handler(__int64 a1)
{
*((_QWORD *)&Proto_Server + 16921) = a1;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
*((_QWORD *)&Proto_Server + 16921) = a1;
return 1LL;
}

original_code:
extern int
proto_server_set_session_lost_handler(Proto_MT_Handler h)
{
  Proto_Server.session_lost_handler = h;
  return 1;
}

canonical_original_code:
extern int proto_server_set_session_lost_handler(int (*h)(struct Proto_Session *))
{
  Proto_Server.session_lost_handler = h;
  return 1;
}

user_defined_types: enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 }

user_defined_types: union Proto_StateVersion { unsigned long long raw; }


decompiled_code:
__int64 __fastcall proto_server_set_req_handler(int a1, __int64 a2)
{
unsigned int v2; // edi
__int64 result; // rax

v2 = a1 - 1;
result = 0xFFFFFFFFLL;
if ( v2 <= 3 )
{
result = 1LL;
*((_QWORD *)&Proto_Server + (int)v2 + 16922) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, __int64 a2)
{
unsigned int v2; // edi
__int64 result; // rax

v2 = a1 - 1;
result = 0xFFFFFFFFLL;
if ( v2 <= 3 )
{
result = 1LL;
*((_QWORD *)&Proto_Server + (int)v2 + 16922) = a2;
}
return result;
}

original_code:
extern int
proto_server_set_req_handler(Proto_Msg_Types mt, Proto_MT_Handler h)
{
  int i;
  if (mt>PROTO_MT_REQ_BASE_RESERVED_FIRST &&
      mt<PROTO_MT_REQ_BASE_RESERVED_LAST) {
    i = mt - PROTO_MT_REQ_BASE_RESERVED_FIRST - 1;
 Proto_Server.base_req_handlers[i] =h;
    return 1;
  } else {
    return -1;
  }
}

canonical_original_code:
extern int proto_server_set_req_handler(enum Proto_Msg_Types mt, int (*h)(struct Proto_Session *))
{
  int i;
  if (mt>PROTO_MT_REQ_BASE_RESERVED_FIRST &&
      mt<PROTO_MT_REQ_BASE_RESERVED_LAST) {
    i = mt - PROTO_MT_REQ_BASE_RESERVED_FIRST - 1;
 Proto_Server.base_req_handlers[i] =h;
    return 1;
  } else {
    return -1;
  }
}


decompiled_code:
__int64 proto_server_lost_event_helper()
{
return (*((__int64 (__fastcall **)(_QWORD))&Proto_Server + 16921))((unsigned int)((_DWORD)&Proto_Server + 4168));
}

canonical_decompiled_code:
__int64 func12()
{
return (*((__int64 (__fastcall **)(_QWORD))&Proto_Server + 16921))((unsigned int)((_DWORD)&Proto_Server + 4168));
}

original_code:
void
proto_server_lost_event_helper(int i)
{
 Proto_Server.session_lost_handler( &Proto_Server.EventSession);
}


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

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v0 = *((_DWORD *)&Proto_Server + 17);
if ( v0 )
{
v1 = (unsigned int)((_DWORD)&Proto_Server + 72);
do
{
while ( 1 )
{
v2 = *(_DWORD *)v1;
*((_DWORD *)&Proto_Server + 1042) = *(_DWORD *)v1;
if ( v2 != -1 )
break;
v1 += 4LL;
}
--v0;
if ( (int)proto_session_send_msg((unsigned int)((_DWORD)&Proto_Server + 4168), 0LL) < 0 )
{
close(*((_DWORD *)&Proto_Server + 1042));
*(_DWORD *)v1 = -1;
--*((_DWORD *)&Proto_Server + 17);
}
v1 += 4LL;
}
while ( v0 );
}
proto_session_reset_send((unsigned int)((_DWORD)&Proto_Server + 4168));
return pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
}

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

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v0 = *((_DWORD *)&Proto_Server + 17);
if ( v0 )
{
v1 = (unsigned int)((_DWORD)&Proto_Server + 72);
do
{
while ( 1 )
{
v2 = *(_DWORD *)v1;
*((_DWORD *)&Proto_Server + 1042) = *(_DWORD *)v1;
if ( v2 != -1 )
break;
v1 += 4LL;
}
--v0;
if ( (int)proto_session_send_msg((unsigned int)((_DWORD)&Proto_Server + 4168), 0LL) < 0 )
{
close(*((_DWORD *)&Proto_Server + 1042));
*(_DWORD *)v1 = -1;
--*((_DWORD *)&Proto_Server + 17);
}
v1 += 4LL;
}
while ( v0 );
}
proto_session_reset_send((unsigned int)((_DWORD)&Proto_Server + 4168));
return pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
}

original_code:
void
proto_server_post_event(void)
{
  int i;
  int num;
  pthread_mutex_lock(&Proto_Server.EventSubscribersLock);
  i = 0;
  num = Proto_Server.EventNumSubscribers;
  while (num) {
    Proto_Server.EventSession.fd = Proto_Server.EventSubscribers[i];
    if (Proto_Server.EventSession.fd != -1) {
      num--;
      if (proto_session_send_msg(&Proto_Server.EventSession,0)<0) {
        close(Proto_Server.EventSession.fd);
        Proto_Server.EventSubscribers[i]=-1;
        Proto_Server.EventNumSubscribers--;
   }
 }
    i++;
  }
  proto_session_reset_send(&Proto_Server.EventSession);
  pthread_mutex_unlock(&Proto_Server.EventSubscribersLock);
}

canonical_original_code:
void proto_server_post_event()
{
  int i;
  int num;
  pthread_mutex_lock(&Proto_Server.EventSubscribersLock);
  i = 0;
  num = Proto_Server.EventNumSubscribers;
  while (num) {
    Proto_Server.EventSession.fd = Proto_Server.EventSubscribers[i];
    if (Proto_Server.EventSession.fd != -1) {
      num--;
      if (proto_session_send_msg(&Proto_Server.EventSession,0)<0) {
        close(Proto_Server.EventSession.fd);
        Proto_Server.EventSubscribers[i]=-1;
        Proto_Server.EventNumSubscribers--;
   }
 }
    i++;
  }
  proto_session_reset_send(&Proto_Server.EventSession);
  pthread_mutex_unlock(&Proto_Server.EventSubscribersLock);
}

variable_types: num int


decompiled_code:
__int64 proto_server_start_rpc_loop()
{
if ( !pthread_create(
(pthread_t *)(unsigned int)((_DWORD)&Proto_Server + 135360),
0LL,
(void *(*)(void *))proto_server_rpc_listen,
0LL) )
return 1LL;
fwrite("proto_server_rpc_listen: pthread_create: create RPCListen thread failed\n", 1uLL, 0x48uLL, stderr);
perror("pthread_create:");
return 4294967293LL;
}

canonical_decompiled_code:
__int64 func14()
{
if ( !pthread_create(
(pthread_t *)(unsigned int)((_DWORD)&Proto_Server + 135360),
0LL,
(void *(*)(void *))proto_server_rpc_listen,
0LL) )
return 1LL;
fwrite("proto_server_rpc_listen: pthread_create: create RPCListen thread failed\n", 1uLL, 0x48uLL, stderr);
perror("pthread_create:");
return 4294967293LL;
}

original_code:
extern int
proto_server_start_rpc_loop(void)
{
  if (pthread_create(&(Proto_Server.RPCListenTid), ((void *)0),
       &proto_server_rpc_listen, ((void *)0)) !=0) {
    fprintf(stderr,
     "proto_server_rpc_listen: pthread_create: create RPCListen thread failed\n");
    perror("pthread_create:");
    return -3;
  }
  return 1;
}

canonical_original_code:
extern int proto_server_start_rpc_loop()
{
  if (pthread_create(&(Proto_Server.RPCListenTid), ((void *)0),
       &proto_server_rpc_listen, ((void *)0)) !=0) {
    fprintf(stderr,
     "proto_server_rpc_listen: pthread_create: create RPCListen thread failed\n");
    perror("pthread_create:");
    return -3;
  }
  return 1;
}


decompiled_code:
int proto_server_init()
{
int v0; // edx
int result; // eax

proto_session_init((unsigned int)((_DWORD)&Proto_Server + 4168));
proto_server_set_session_lost_handler((__int64)proto_session_lost_default_handler);
do
proto_server_set_req_handler(v0, (__int64)proto_server_mt_null_handler);
while ( v0 != 5 );
*((_QWORD *)&Proto_Server + 8) = 0LL;
memset((void *)(unsigned int)((_DWORD)&Proto_Server + 72), 0xFFu, 0x1000uLL);
pthread_mutex_init((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24), 0LL);
if ( (unsigned int)net_setup_listen_socket(&Proto_Server, (unsigned int)((_DWORD)&Proto_Server + 4)) )
{
Proto_Server.__nusers = Proto_Server.__count + 1;
if ( (unsigned int)net_setup_listen_socket(
(unsigned int)((_DWORD)&Proto_Server + 8),
(unsigned int)((_DWORD)&Proto_Server + 12)) )
{
result = pthread_create(
(pthread_t *)(unsigned int)((_DWORD)&Proto_Server + 16),
0LL,
(void *(*)(void *))proto_server_event_listen,
0LL);
if ( result )
{
fwrite("proto_server_init: pthread_create: create EventListen thread failed\n", 1uLL, 0x44uLL, stderr);
perror("pthread_createt:");
result = -3;
}
}
else
{
fprintf(stderr, "proto_server_init: net_setup_listen_socket: FAILED for EventPort=%d\n", Proto_Server.__nusers);
result = -2;
}
}
else
{
fwrite("prot_server_init: net_setup_listen_socket: FAILED for RPCPort\n", 1uLL, 0x3EuLL, stderr);
result = -1;
}
return result;
}

canonical_decompiled_code:
int func15()
{
int v0; // edx
int result; // eax

proto_session_init((unsigned int)((_DWORD)&Proto_Server + 4168));
func10((__int64)proto_session_lost_default_handler);
do
func11(v0, (__int64)proto_server_mt_null_handler);
while ( v0 != 5 );
*((_QWORD *)&Proto_Server + 8) = 0LL;
memset((void *)(unsigned int)((_DWORD)&Proto_Server + 72), 0xFFu, 0x1000uLL);
pthread_mutex_init((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24), 0LL);
if ( (unsigned int)net_setup_listen_socket(&Proto_Server, (unsigned int)((_DWORD)&Proto_Server + 4)) )
{
Proto_Server.__nusers = Proto_Server.__count + 1;
if ( (unsigned int)net_setup_listen_socket(
(unsigned int)((_DWORD)&Proto_Server + 8),
(unsigned int)((_DWORD)&Proto_Server + 12)) )
{
result = pthread_create(
(pthread_t *)(unsigned int)((_DWORD)&Proto_Server + 16),
0LL,
(void *(*)(void *))proto_server_event_listen,
0LL);
if ( result )
{
fwrite("proto_server_init: pthread_create: create EventListen thread failed\n", 1uLL, 0x44uLL, stderr);
perror("pthread_createt:");
result = -3;
}
}
else
{
fprintf(stderr, "proto_server_init: net_setup_listen_socket: FAILED for EventPort=%d\n", Proto_Server.__nusers);
result = -2;
}
}
else
{
fwrite("prot_server_init: net_setup_listen_socket: FAILED for RPCPort\n", 1uLL, 0x3EuLL, stderr);
result = -1;
}
return result;
}

original_code:
extern int
proto_server_init(void)
{
  int i;
  int rc;
  proto_session_init(&Proto_Server.EventSession);
  proto_server_set_session_lost_handler(
         proto_session_lost_default_handler);
  for (i=PROTO_MT_REQ_BASE_RESERVED_FIRST+1;
       i<PROTO_MT_REQ_BASE_RESERVED_LAST; i++) {
     proto_server_set_req_handler(i,&proto_server_mt_null_handler);
  }
  for (i=0; i<1024; i++) {
    Proto_Server.EventSubscribers[i]=-1;
  }
  Proto_Server.EventNumSubscribers=0;
  Proto_Server.EventLastSubscriber=0;
  pthread_mutex_init(&Proto_Server.EventSubscribersLock, 0);
  rc=net_setup_listen_socket(&(Proto_Server.RPCListenFD),
        &(Proto_Server.RPCPort));
  if (rc==0) {
    fprintf(stderr, "prot_server_init: net_setup_listen_socket: FAILED for RPCPort\n");
    return -1;
  }
  Proto_Server.EventPort = Proto_Server.RPCPort + 1;
  rc=net_setup_listen_socket(&(Proto_Server.EventListenFD),
        &(Proto_Server.EventPort));
  if (rc==0) {
    fprintf(stderr, "proto_server_init: net_setup_listen_socket: FAILED for EventPort=%d\n",
     Proto_Server.EventPort);
    return -2;
  }
  if (pthread_create(&(Proto_Server.EventListenTid), ((void *)0),
       &proto_server_event_listen, ((void *)0)) !=0) {
    fprintf(stderr,
     "proto_server_init: pthread_create: create EventListen thread failed\n");
    perror("pthread_createt:");
    return -3;
  }
  return 0;
}

canonical_original_code:
extern int proto_server_init()
{
  int i;
  int rc;
  proto_session_init(&Proto_Server.EventSession);
  proto_server_set_session_lost_handler(
         proto_session_lost_default_handler);
  for (i=PROTO_MT_REQ_BASE_RESERVED_FIRST+1;
       i<PROTO_MT_REQ_BASE_RESERVED_LAST; i++) {
     proto_server_set_req_handler(i,&proto_server_mt_null_handler);
  }
  for (i=0; i<1024; i++) {
    Proto_Server.EventSubscribers[i]=-1;
  }
  Proto_Server.EventNumSubscribers=0;
  Proto_Server.EventLastSubscriber=0;
  pthread_mutex_init(&Proto_Server.EventSubscribersLock, 0);
  rc=net_setup_listen_socket(&(Proto_Server.RPCListenFD),
        &(Proto_Server.RPCPort));
  if (rc==0) {
    fprintf(stderr, "prot_server_init: net_setup_listen_socket: FAILED for RPCPort\n");
    return -1;
  }
  Proto_Server.EventPort = Proto_Server.RPCPort + 1;
  rc=net_setup_listen_socket(&(Proto_Server.EventListenFD),
        &(Proto_Server.EventPort));
  if (rc==0) {
    fprintf(stderr, "proto_server_init: net_setup_listen_socket: FAILED for EventPort=%d\n",
     Proto_Server.EventPort);
    return -2;
  }
  if (pthread_create(&(Proto_Server.EventListenTid), ((void *)0),
       &proto_server_event_listen, ((void *)0)) !=0) {
    fprintf(stderr,
     "proto_server_init: pthread_create: create EventListen thread failed\n");
    perror("pthread_createt:");
    return -3;
  }
  return 0;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall put_int(__int64 a1, __int64 a2)
{
return proto_session_body_marshall_int(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2)
{
return proto_session_body_marshall_int(a1, a2);
}

original_code:
extern void put_int(Proto_Session* s, int value)
{
  proto_session_body_marshall_int(s, value);
}

canonical_original_code:
extern void put_int(struct Proto_Session *s, int value)
{
  proto_session_body_marshall_int(s, value);
}

variable_types: value int


decompiled_code:
// attributes: thunk
__int64 __fastcall put_hdr(__int64 a1, __int64 a2)
{
return proto_session_hdr_marshall(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, __int64 a2)
{
return proto_session_hdr_marshall(a1, a2);
}

original_code:
extern void put_hdr(Proto_Session*s, Proto_Msg_Hdr *hdr)
{
  proto_session_hdr_marshall(s,hdr);
}

canonical_original_code:
extern void put_hdr(struct Proto_Session *s, struct Proto_Msg_Hdr *hdr)
{
  proto_session_hdr_marshall(s,hdr);
}

variable_types:
hdr
struct Proto_Msg_Hdr *


reply

decompiled_code:
__int64 __fastcall reply(__int64 a1, int a2, int a3, unsigned int a4)
{
unsigned int v5; // er12
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int128 v11[2]; // [rsp+0h] [rbp-48h] BYREF
__int128 v12; // [rsp+20h] [rbp-28h]

if ( !a2 )
{
if ( !a4 )
goto LABEL_3;
goto LABEL_9;
}
v11[0] = 0LL;
v11[1] = 0LL;
DWORD1(v11[0]) = a2;
v12 = 0LL;
if ( a3 )
DWORD1(v12) = a3;
proto_session_hdr_marshall(a1, v11);
if ( a4 )
LABEL_9:
proto_session_body_marshall_int(a1, a4);
LABEL_3:
v5 = proto_session_send_msg(a1, 1LL);
if ( (unsigned int)proto_debug(a1, 1LL, v6, v7, v8, v9) )
proto_session_dump(a1);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, int a2, int a3, unsigned int a4)
{
unsigned int v5; // er12
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int128 v11[2]; // [rsp+0h] [rbp-48h] BYREF
__int128 v12; // [rsp+20h] [rbp-28h]

if ( !a2 )
{
if ( !a4 )
goto LABEL_3;
goto LABEL_9;
}
v11[0] = 0LL;
v11[1] = 0LL;
DWORD1(v11[0]) = a2;
v12 = 0LL;
if ( a3 )
DWORD1(v12) = a3;
proto_session_hdr_marshall(a1, v11);
if ( a4 )
LABEL_9:
proto_session_body_marshall_int(a1, a4);
LABEL_3:
v5 = proto_session_send_msg(a1, 1LL);
if ( (unsigned int)func0(a1, 1LL, v6, v7, v8, v9) )
proto_session_dump(a1);
return v5;
}

original_code:
extern int reply( Proto_Session * s, unsigned int mt , int response, int timestamp)
{
  int rc=1;
  Proto_Msg_Hdr h;
  if ( (void*)(size_t) mt != ((void *)0))
  {
    bzero(&h, sizeof(Proto_Msg_Hdr));
    h.type = (Proto_Msg_Types) mt;
    if ( (void*)(size_t)response != ((void *)0)) h.gstate.v1.raw = response;
    proto_session_hdr_marshall(s, &h);
  }
  if ((void*)(size_t) timestamp != ((void *)0) ) proto_session_body_marshall_int(s,timestamp);
  rc=proto_session_send_msg(s,1);
  if (proto_debug()) proto_session_dump(s);
  return rc;
}

canonical_original_code:
extern int reply(struct Proto_Session *s, unsigned int mt, int response, int timestamp)
{
  int rc = 1;
  struct Proto_Msg_Hdr h;
  if ( (void *)(unsigned long) mt != ((void *)0))
  {
    bzero(&h, sizeof(Proto_Msg_Hdr));
    h.type = (enum Proto_Msg_Types) mt;
    if ( (void *)(unsigned long)response != ((void *)0)) h.gstate.v1.raw = response;
    proto_session_hdr_marshall(s, &h);
  }
  if ((void *)(unsigned long) timestamp != ((void *)0) ) proto_session_body_marshall_int(s,timestamp);
  rc=proto_session_send_msg(s,1);
  if (proto_debug()) proto_session_dump(s);
  return rc;
}

variable_types:
mt
unsigned int

variable_types: response int

variable_types: timestamp int

variable_types: rc int


decompiled_code:
__int64 __fastcall proto_client_event_null_handler(__int64 a1)
{
fwrite("proto_client_event_null_handler: invoked for session:\n", 1uLL, 0x36uLL, stderr);
proto_session_dump(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
fwrite("proto_client_event_null_handler: invoked for session:\n", 1uLL, 0x36uLL, stderr);
proto_session_dump(a1);
return 1LL;
}

original_code:
static int
proto_client_event_null_handler(Proto_Session *s)
{
  fprintf(stderr,
   "proto_client_event_null_handler: invoked for session:\n");
  proto_session_dump(s);
  return 1;
}

canonical_original_code:
static int proto_client_event_null_handler(struct Proto_Session *s)
{
  fprintf(stderr,
   "proto_client_event_null_handler: invoked for session:\n");
  proto_session_dump(s);
  return 1;
}


decompiled_code:
__int64 __fastcall proto_client_session_lost_default_hdlr(__int64 a1)
{
fwrite("Session lost...:\n", 1uLL, 0x11uLL, stderr);
proto_session_dump(a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
fwrite("Session lost...:\n", 1uLL, 0x11uLL, stderr);
proto_session_dump(a1);
return 0xFFFFFFFFLL;
}

original_code:
static int
proto_client_session_lost_default_hdlr(Proto_Session *s)
{
  fprintf(stderr, "Session lost...:\n");
  proto_session_dump(s);
  return -1;
}

canonical_original_code:
static int proto_client_session_lost_default_hdlr(struct Proto_Session *s)
{
  fprintf(stderr, "Session lost...:\n");
  proto_session_dump(s);
  return -1;
}


decompiled_code:
void *__fastcall proto_client_event_dispatcher(void *a1)
{
char *v1; // rbp
pthread_t v2; // rax

v1 = (char *)a1 + 131192;
v2 = pthread_self();
pthread_detach(v2);
while ( (unsigned int)proto_session_rcv_msg(v1) == 1 )
{
if ( (unsigned int)proto_session_hdr_unmarshall_type(v1) == 13
&& (*((int (__fastcall **)(char *))a1 + 32800))(v1) < 0 )
{
goto LABEL_5;
}
}
(*((void (__fastcall **)(char *))a1 + 32799))(v1);
LABEL_5:
close(*((_DWORD *)a1 + 32798));
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
char *v1; // rbp
pthread_t v2; // rax

v1 = (char *)a1 + 131192;
v2 = pthread_self();
pthread_detach(v2);
while ( (unsigned int)proto_session_rcv_msg(v1) == 1 )
{
if ( (unsigned int)proto_session_hdr_unmarshall_type(v1) == 13
&& (*((int (__fastcall **)(char *))a1 + 32800))(v1) < 0 )
{
goto LABEL_5;
}
}
(*((void (__fastcall **)(char *))a1 + 32799))(v1);
LABEL_5:
close(*((_DWORD *)a1 + 32798));
return 0LL;
}

original_code:
static void *
proto_client_event_dispatcher(void * arg)
{
  Proto_Client *c;
  Proto_Session *s;
  Proto_Msg_Types mt;
  Proto_MT_Handler hdlr;
  pthread_detach(pthread_self());
  c = arg;
  s = &(c->event_session);
  for (;;) {
    if (proto_session_rcv_msg(s)==1) {
      mt = proto_session_hdr_unmarshall_type(s);
      if (mt > PROTO_MT_EVENT_BASE_RESERVED_FIRST &&
   mt < PROTO_MT_EVENT_BASE_RESERVED_LAST) {
  hdlr = c->base_event_handlers[mt-PROTO_MT_EVENT_BASE_RESERVED_FIRST-1];
 if (hdlr(s)<0) goto leave;
      }
    } else {
      c->session_lost_handler(s);
      goto leave;
    }
  }
 leave:
  close(s->fd);
  return ((void *)0);
}

canonical_original_code:
static void *proto_client_event_dispatcher(void *arg)
{
  struct Proto_Client *c;
  struct Proto_Session *s;
  enum Proto_Msg_Types mt;
  int (*hdlr)(struct Proto_Session *);
  pthread_detach(pthread_self());
  c = arg;
  s = &(c->event_session);
  for (;;) {
    if (proto_session_rcv_msg(s)==1) {
      mt = proto_session_hdr_unmarshall_type(s);
      if (mt > PROTO_MT_EVENT_BASE_RESERVED_FIRST &&
   mt < PROTO_MT_EVENT_BASE_RESERVED_LAST) {
  hdlr = c->base_event_handlers[mt-PROTO_MT_EVENT_BASE_RESERVED_FIRST-1];
 if (hdlr(s)<0) goto leave;
      }
    } else {
      c->session_lost_handler(s);
      goto leave;
    }
  }
 leave:
  close(s->fd);
  return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct Proto_Client { struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; } rpc_session; struct Proto_Session event_session; unsigned long EventHandlerTid; int (*session_lost_handler)(struct Proto_Session *); int (*base_event_handlers[-1])(struct Proto_Session *); } *

user_defined_types: struct Proto_Client { struct Proto_Session rpc_session; struct Proto_Session event_session; unsigned long EventHandlerTid; int (*session_lost_handler)(struct Proto_Session *); int (*base_event_handlers[-1])(struct Proto_Session *); }


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

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

original_code:
extern Proto_Session *
proto_client_rpc_session(Proto_Client_Handle ch)
{
  Proto_Client *c = ch;
  return &(c->rpc_session);
}

canonical_original_code:
extern struct Proto_Session *proto_client_rpc_session(void *ch)
{
  struct Proto_Client *c = ch;
  return &(c->rpc_session);
}

variable_types:
ch
void *


decompiled_code:
__int64 __fastcall proto_client_event_session(__int64 a1)
{
return a1 + 131192;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return a1 + 131192;
}

original_code:
extern Proto_Session *
proto_client_event_session(Proto_Client_Handle ch)
{
  Proto_Client *c = ch;
  return &(c->event_session);
}

canonical_original_code:
extern struct Proto_Session *proto_client_event_session(void *ch)
{
  struct Proto_Client *c = ch;
  return &(c->event_session);
}


decompiled_code:
__int64 __fastcall proto_client_set_session_lost_handler(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 262392) = a2;
return 1LL;
}

original_code:
extern int
proto_client_set_session_lost_handler(Proto_Client_Handle ch, Proto_MT_Handler h)
{
  Proto_Client *c = ch;
  c->session_lost_handler = h;
  return 1;
}

canonical_original_code:
extern int proto_client_set_session_lost_handler(void *ch, int (*h)(struct Proto_Session *))
{
  struct Proto_Client *c = ch;
  c->session_lost_handler = h;
  return 1;
}


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

result = 0xFFFFFFFFLL;
if ( a2 == 13 )
{
*(_QWORD *)(a1 + 262400) = a3;
result = 1LL;
}
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a2 == 13 )
{
*(_QWORD *)(a1 + 262400) = a3;
result = 1LL;
}
return result;
}

original_code:
extern int
proto_client_set_event_handler(Proto_Client_Handle ch, Proto_Msg_Types mt,
          Proto_MT_Handler h)
{
  int i;
  Proto_Client *c = ch;
  if (mt>PROTO_MT_EVENT_BASE_RESERVED_FIRST &&
      mt<PROTO_MT_EVENT_BASE_RESERVED_LAST) {
    i=mt - PROTO_MT_EVENT_BASE_RESERVED_FIRST - 1;
    c->base_event_handlers[i] = h;
    return 1;
  } else {
    return -1;
  }
}

canonical_original_code:
extern int proto_client_set_event_handler(void *ch, enum Proto_Msg_Types mt, int (*h)(struct Proto_Session *))
{
  int i;
  struct Proto_Client *c = ch;
  if (mt>PROTO_MT_EVENT_BASE_RESERVED_FIRST &&
      mt<PROTO_MT_EVENT_BASE_RESERVED_LAST) {
    i=mt - PROTO_MT_EVENT_BASE_RESERVED_FIRST - 1;
    c->base_event_handlers[i] = h;
    return 1;
  } else {
    return -1;
  }
}

variable_types: enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 }


decompiled_code:
__int64 __fastcall proto_client_init(_QWORD *a1)
{
void *v2; // rax
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rcx

v2 = calloc(0x40108uLL, 1uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = (__int64)v2;
proto_client_set_session_lost_handler((__int64)v2, (__int64)proto_client_session_lost_default_hdlr);
proto_client_set_event_handler(v3, 13, v4);
*a1 = v5;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
void *v2; // rax
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rcx

v2 = calloc(0x40108uLL, 1uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = (__int64)v2;
func5((__int64)v2, (__int64)proto_client_session_lost_default_hdlr);
func6(v3, 13, v4);
*a1 = v5;
return 1LL;
}

original_code:
extern int
proto_client_init(Proto_Client_Handle *ch)
{
  Proto_Msg_Types mt;
  Proto_Client *c;
  c = (Proto_Client *)malloc(sizeof(Proto_Client));
  if (c==((void *)0)) return -1;
  bzero(c, sizeof(Proto_Client));
  proto_client_set_session_lost_handler(c,
          proto_client_session_lost_default_hdlr);
  for (mt=PROTO_MT_EVENT_BASE_RESERVED_FIRST+1;
       mt<PROTO_MT_EVENT_BASE_RESERVED_LAST; mt++)
    proto_client_set_event_handler(c, mt, &proto_client_event_null_handler );
  *ch = c;
  return 1;
}

canonical_original_code:
extern int proto_client_init(void **ch)
{
  enum Proto_Msg_Types mt;
  struct Proto_Client *c;
  c = (struct Proto_Client *)malloc(sizeof(Proto_Client));
  if (c==((void *)0)) return -1;
  bzero(c, sizeof(Proto_Client));
  proto_client_set_session_lost_handler(c,
          proto_client_session_lost_default_hdlr);
  for (mt=PROTO_MT_EVENT_BASE_RESERVED_FIRST+1;
       mt<PROTO_MT_EVENT_BASE_RESERVED_LAST; mt++)
    proto_client_set_event_handler(c, mt, &proto_client_event_null_handler );
  *ch = c;
  return 1;
}

variable_types:
ch
void **


decompiled_code:
int __fastcall proto_client_connect(void *arg, __int64 a2, int a3)
{
int result; // eax

if ( (int)((__int64 (*)(void))net_setup_connection)() < 0 )
return -1;
if ( (int)net_setup_connection((char *)arg + 131192, a2, (unsigned int)(a3 + 1)) < 0 )
return -2;
result = pthread_create((pthread_t *)arg + 32798, 0LL, proto_client_event_dispatcher, arg);
if ( result )
{
fwrite("proto_client_init: create EventHandler thread failed\n", 1uLL, 0x35uLL, stderr);
perror("proto_client_init:");
result = -3;
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(void *arg, __int64 a2, int a3)
{
int result; // eax

if ( (int)((__int64 (*)(void))net_setup_connection)() < 0 )
return -1;
if ( (int)net_setup_connection((char *)arg + 131192, a2, (unsigned int)(a3 + 1)) < 0 )
return -2;
result = pthread_create((pthread_t *)arg + 32798, 0LL, proto_client_event_dispatcher, arg);
if ( result )
{
fwrite("proto_client_init: create EventHandler thread failed\n", 1uLL, 0x35uLL, stderr);
perror("proto_client_init:");
result = -3;
}
return result;
}

original_code:
int
proto_client_connect(Proto_Client_Handle ch, char *host, PortType port)
{
  Proto_Client *c = (Proto_Client *)ch;
  if (net_setup_connection(&(c->rpc_session.fd), host, port)<0)
    return -1;
  if (net_setup_connection(&(c->event_session.fd), host, port+1)<0)
    return -2;
  if (pthread_create(&(c->EventHandlerTid), ((void *)0),
       &proto_client_event_dispatcher, c) !=0) {
    fprintf(stderr,
     "proto_client_init: create EventHandler thread failed\n");
    perror("proto_client_init:");
    return -3;
  }
  return 0;
}

canonical_original_code:
int proto_client_connect(void *ch, char *host, int port)
{
  struct Proto_Client *c = (Proto_Client *)ch;
  if (net_setup_connection(&(c->rpc_session.fd), host, port)<0)
    return -1;
  if (net_setup_connection(&(c->event_session.fd), host, port+1)<0)
    return -2;
  if (pthread_create(&(c->EventHandlerTid), ((void *)0),
       &proto_client_event_dispatcher, c) !=0) {
    fprintf(stderr,
     "proto_client_init: create EventHandler thread failed\n");
    perror("proto_client_init:");
    return -3;
  }
  return 0;
}

variable_types:
host
char *

variable_types: port int


decompiled_code:
// attributes: thunk
__int64 get_int()
{
return proto_session_body_unmarshall_int();
}

canonical_decompiled_code:
__int64 func9()
{
return proto_session_body_unmarshall_int();
}

original_code:
extern int get_int(Proto_Client_Handle ch, int offset, int* result)
{
  int rc;
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  rc = proto_session_body_unmarshall_int(s, offset, result);
  return rc;
}

canonical_original_code:
extern int get_int(void *ch, int offset, int *result)
{
  int rc;
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  rc = proto_session_body_unmarshall_int(s, offset, result);
  return rc;
}

variable_types: offset int

variable_types:
result
int *


decompiled_code:
// attributes: thunk
__int64 get_hdr()
{
return proto_session_hdr_unmarshall();
}

canonical_decompiled_code:
__int64 func10()
{
return proto_session_hdr_unmarshall();
}

original_code:
extern void get_hdr(Proto_Client_Handle ch, Proto_Msg_Hdr * hdr)
{
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_unmarshall(s,hdr);
}

canonical_original_code:
extern void get_hdr(void *ch, struct Proto_Msg_Hdr *hdr)
{
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_unmarshall(s,hdr);
}

variable_types: struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } *

variable_types: struct Proto_Session { int fd; void *extra; int slen; int rlen; struct Proto_Msg_Hdr { int version; enum Proto_Msg_Types { PROTO_MT_REQ_BASE_RESERVED_FIRST=0, PROTO_MT_REQ_HELLO=1, PROTO_MT_REQ_GOODBYE=2, PROTO_MT_REQ_ACTION=3, PROTO_MT_REQ_SYNC=4, PROTO_MT_REQ_BASE_RESERVED_LAST=5, PROTO_MT_REP_BASE_RESERVED_FIRST=6, PROTO_MT_REP_HELLO=7, PROTO_MT_REP_GOODBYE=8, PROTO_MT_REP_ACTION=9, PROTO_MT_REP_SYNC=10, PROTO_MT_REP_BASE_RESERVED_LAST=11, PROTO_MT_EVENT_BASE_RESERVED_FIRST=12, PROTO_MT_EVENT_UPDATE=13, PROTO_MT_EVENT_BASE_RESERVED_LAST=14 } type; union Proto_StateVersion { unsigned long long raw; } sver; struct Proto_Player_State { union Proto_PV0 { int raw; } v0; union Proto_PV1 { int raw; } v1; union Proto_PV2 { int raw; } v2; union Proto_PV3 { int raw; } v3; } pstate; struct Proto_Game_State { union Proto_GV0 { int raw; } v0; union Proto_GV1 { int raw; } v1; union Proto_GV2 { int raw; } v2; } gstate; int blen; } shdr; struct Proto_Msg_Hdr rhdr; char sbuf[-1]; char rbuf[-1]; } *


decompiled_code:
__int64 __fastcall get_pos(__int64 a1, __int64 a2)
{
return proto_session_body_unmarshall_bytes(a1, 0LL, 4LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
return proto_session_body_unmarshall_bytes(a1, 0LL, 4LL, a2);
}

original_code:
extern void get_pos(Proto_Client_Handle ch, Pos* current)
{
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_body_unmarshall_bytes(s,0,sizeof(Pos),(char*)current);
}

canonical_original_code:
extern void get_pos(void *ch, struct Pos *current)
{
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_body_unmarshall_bytes(s,0,sizeof(Pos),(char *)current);
}

variable_types: struct Pos { unsigned short x; unsigned short y; } *


decompiled_code:
__int64 __fastcall get_compress_from_body(__int64 a1, __int64 a2, int a3, __int64 a4)
{
__int64 v4; // r12
__int64 v5; // rbx
__int64 v6; // rdx
__int64 result; // rax

if ( a3 <= 0 )
return (unsigned int)a2;
v4 = a4 + 4LL * a3;
v5 = a4;
do
{
v6 = v5;
v5 += 4LL;
result = proto_session_body_unmarshall_int(a1, a2, v6);
a2 = (unsigned int)result;
}
while ( v5 != v4 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, int a3, __int64 a4)
{
__int64 v4; // r12
__int64 v5; // rbx
__int64 v6; // rdx
__int64 result; // rax

if ( a3 <= 0 )
return (unsigned int)a2;
v4 = a4 + 4LL * a3;
v5 = a4;
do
{
v6 = v5;
v5 += 4LL;
result = proto_session_body_unmarshall_int(a1, a2, v6);
a2 = (unsigned int)result;
}
while ( v5 != v4 );
return result;
}

original_code:
extern int get_compress_from_body(Proto_Client_Handle ch,int offset ,int num_elements,int* alloc_pointer)
{
  int ii, current_offset;
  int* ptr_to_array;
  current_offset = offset;
  ptr_to_array = alloc_pointer;
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  for(ii = 0; ii<num_elements;ii++)
  {
      current_offset = proto_session_body_unmarshall_int(s,current_offset,ptr_to_array);
      ptr_to_array++;
  }
  return current_offset;
}

canonical_original_code:
extern int get_compress_from_body(void *ch, int offset, int num_elements, int *alloc_pointer)
{
  int ii; int current_offset;
  int *ptr_to_array;
  current_offset = offset;
  ptr_to_array = alloc_pointer;
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  for(ii = 0; ii<num_elements;ii++)
  {
      current_offset = proto_session_body_unmarshall_int(s,current_offset,ptr_to_array);
      ptr_to_array++;
  }
  return current_offset;
}

variable_types: ii int


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

proto_session_hdr_marshall();
proto_session_send_msg(a1, 1LL);
result = proto_session_rcv_msg(a1);
if ( (int)result <= 0 )
result = 0xFFFFFFFFLL;
return result;
}

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

proto_session_hdr_marshall();
proto_session_send_msg(a1, 1LL);
result = proto_session_rcv_msg(a1);
if ( (int)result <= 0 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
extern int
do_no_body_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * h)
{
  int rc;
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,h);
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  if(rc<=0) rc =-1;
  return rc;
}

canonical_original_code:
extern int do_no_body_rpc(void *ch, struct Proto_Msg_Hdr *h)
{
  int rc;
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,h);
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  if(rc<=0) rc =-1;
  return rc;
}


decompiled_code:
__int64 __fastcall do_action_request_rpc(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 result; // rax
int v5; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6[0] = a3;
v5 = a4;
proto_session_hdr_marshall();
if ( (unsigned int)(*(_DWORD *)(a2 + 36) - 1) <= 4 )
{
proto_session_body_marshall_bytes(a1, 4LL, v6);
proto_session_body_marshall_bytes(a1, 4LL, &v5);
}
proto_session_send_msg(a1, 1LL);
result = proto_session_rcv_msg(a1);
if ( (int)result <= 0 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 result; // rax
int v5; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6[0] = a3;
v5 = a4;
proto_session_hdr_marshall();
if ( (unsigned int)(*(_DWORD *)(a2 + 36) - 1) <= 4 )
{
proto_session_body_marshall_bytes(a1, 4LL, v6);
proto_session_body_marshall_bytes(a1, 4LL, &v5);
}
proto_session_send_msg(a1, 1LL);
result = proto_session_rcv_msg(a1);
if ( (int)result <= 0 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
extern int
do_action_request_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * hdr,Pos current, Pos next)
{
  int rc;
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,hdr);
  if( hdr->gstate.v1.raw >= ACTION_MOVE &&
      hdr->gstate.v1.raw <= ACTION_PICKUP_SHOVEL)
  {
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)&current);
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)&next);
  }
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  if(rc<=0) rc =-1;
  return rc;
}

canonical_original_code:
extern int do_action_request_rpc(void *ch, struct Proto_Msg_Hdr *hdr, struct Pos current, struct Pos next)
{
  int rc;
  struct Proto_Session *s;
  struct Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,hdr);
  if( hdr->gstate.v1.raw >= ACTION_MOVE &&
      hdr->gstate.v1.raw <= ACTION_PICKUP_SHOVEL)
  {
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char *)&current);
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char *)&next);
  }
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  if(rc<=0) rc =-1;
  return rc;
}

variable_types: struct Pos { unsigned short x; unsigned short y; }

variable_types:
next
struct Pos


canonical_decompiled_code:
void func0()
{
PROTO_DEBUG = 1;
}

original_code: static inline void proto_debug_on(void) { PROTO_DEBUG = 1; }

canonical_original_code: static inline void proto_debug_on() { PROTO_DEBUG = 1; }


decompiled_code:
__int64 proto_debug()
{
return PROTO_DEBUG;
}

canonical_decompiled_code:
__int64 func1()
{
return PROTO_DEBUG;
}

original_code: static inline int proto_debug(void) {return PROTO_DEBUG; }

canonical_original_code: static inline int proto_debug() {return PROTO_DEBUG; }


decompiled_code:
void __fastcall __noreturn threaded_task(void *a1)
{
int v1; // ebp
void (__fastcall **v2)(void *); // rbx
int v3; // eax
int v4; // er12
struct timespec v5; // [rsp+0h] [rbp-38h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-28h] BYREF

v1 = 1;
v2 = (void (__fastcall **)(void *))a1;
v3 = *((_DWORD *)a1 + 14);
v5.tv_nsec = 0LL;
if ( v3 > 0 )
v1 = *((_DWORD *)a1 + 14);
v4 = 0;
do
{
v5.tv_sec = 0LL;
++v4;
v5.tv_nsec = (int)randint(a1) % 10;
v2[6](v2);
a1 = &v5;
nanosleep(&v5, &remaining);
}
while ( v1 != v4 );
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // ebp
void (__fastcall **v2)(void *); // rbx
int v3; // eax
int v4; // er12
struct timespec v5; // [rsp+0h] [rbp-38h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-28h] BYREF

v1 = 1;
v2 = (void (__fastcall **)(void *))a1;
v3 = *((_DWORD *)a1 + 14);
v5.tv_nsec = 0LL;
if ( v3 > 0 )
v1 = *((_DWORD *)a1 + 14);
v4 = 0;
do
{
v5.tv_sec = 0LL;
++v4;
v5.tv_nsec = (int)randint(a1) % 10;
v2[6](v2);
a1 = &v5;
nanosleep(&v5, &remaining);
}
while ( v1 != v4 );
pthread_exit(0LL);
}

original_code:
void * threaded_task(void* task_ptr)
{
  int reps,ii;
  struct timespec desired, diff;
  bzero(&desired,sizeof(struct timespec));
  Task* task = (Task*) task_ptr;
  reps = task->reps < 1 ? 1 : task->reps;
  for (ii = 0; ii<reps; ii++)
  {
    desired.tv_sec = 0;
    desired.tv_nsec = (randint() % 10);
    TaskFunction function = (TaskFunction) task->func;
    (*function)(task);
    nanosleep(&desired,&diff);
  }
  pthread_exit( (void*) 0);
}

canonical_original_code:
void *threaded_task(void *task_ptr)
{
  int reps; int ii;
  struct timespec desired; struct timespec diff;
  bzero(&desired,sizeof(struct timespec));
  struct Task *task = (Task*) task_ptr;
  reps = task->reps < 1 ? 1 : task->reps;
  for (ii = 0; ii<reps; ii++)
  {
    desired.tv_sec = 0;
    desired.tv_nsec = (randint() % 10);
    void (*function)(struct Task *) = (TaskFunction) task->func;
    (*function)(task);
    nanosleep(&desired,&diff);
  }
  pthread_exit( (void *) 0);
}

variable_types: reps int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: void (*)(struct Task { void *arg0; void *arg1; void *arg2; void *arg3; void *arg4; void *arg5; void (*func)(void *); int reps; } *)


decompiled_code:
void __fastcall __noreturn threaded_test(void *a1)
{
*((_DWORD *)a1 + 6) = 0;
(*((void (**)(void))a1 + 4))();
*((_DWORD *)a1 + 6) = 1;
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
*((_DWORD *)a1 + 6) = 0;
(*((void (**)(void))a1 + 4))();
*((_DWORD *)a1 + 6) = 1;
pthread_exit(0LL);
}

original_code:
void* threaded_test( void* arg)
{
  TestContext* tc = (TestContext*) arg;
  TestFunction test_function;
  test_function = (TestFunction) tc->test_function;
  tc->current_test_status = 0;
  (*test_function)(tc);
  tc->current_test_status = 1;
  pthread_exit( (void*) 0);
}

canonical_original_code:
void *threaded_test(void *arg)
{
  struct TestContext *tc = (TestContext*) arg;
  void (*test_function)(struct TestContext *);
  test_function = (void (*)(struct TestContext *)) tc->test_function;
  tc->current_test_status = 0;
  (*test_function)(tc);
  tc->current_test_status = 1;
  pthread_exit( (void *) 0);
}

variable_types:
arg
void *

variable_types: void (*)(struct TestContext { int pass; int fail; int num; int verbose; char *current; int current_test_status; int blocking; void (*test_function)(void *); 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; } lock; long start_time; long stop_time; int single_test; } *)

user_defined_types: struct TestContext { int pass; int fail; int num; int verbose; char *current; int current_test_status; int blocking; void (*test_function)(void *); union pthread_mutex_t lock; long start_time; long stop_time; int single_test; }


original_code:
extern void test_task_init(Task *task, Proc func, int reps, void* a0,void*a1, void*a2, void*a3,void*a4,void*a5)
{
  task->func = func;
  task->reps = reps;
  task->arg0 = a0;
  task->arg1 = a1;
  task->arg2 = a2;
  task->arg3 = a3;
  task->arg4 = a4;
  task->arg5 = a5;
}

canonical_original_code:
extern void test_task_init(struct Task *task, void (*func)(void *), int reps, void *a0, void *a1, void *a2, void *a3, void *a4, void *a5)
{
  task->func = func;
  task->reps = reps;
  task->arg0 = a0;
  task->arg1 = a1;
  task->arg2 = a2;
  task->arg3 = a3;
  task->arg4 = a4;
  task->arg5 = a5;
}

variable_types: struct Task { void *arg0; void *arg1; void *arg2; void *arg3; void *arg4; void *arg5; void (*func)(void *); int reps; } *

variable_types:
func
void (*)(void *)


decompiled_code:
int __fastcall test_nanosleep(__int64 a1)
{
struct timespec v2; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = (int)randint(a1) % 5;
return nanosleep(&v2, &remaining);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
struct timespec v2; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = (int)randint(a1) % 5;
return nanosleep(&v2, &remaining);
}

original_code:
void test_nanosleep(void)
{
  struct timespec desired, diff;
  bzero(&desired,sizeof(struct timespec));
  desired.tv_sec = 0;
  desired.tv_nsec = (randint() % 5);
  nanosleep(&desired,&diff);
}

canonical_original_code:
void test_nanosleep()
{
  struct timespec desired; struct timespec diff;
  bzero(&desired,sizeof(struct timespec));
  desired.tv_sec = 0;
  desired.tv_nsec = (randint() % 5);
  nanosleep(&desired,&diff);
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


should

decompiled_code:
int __fastcall should(__int64 a1, int a2, __int64 a3)
{
int result; // eax

if ( !a2 )
{
fprintf(stderr, format, *(_QWORD *)(a3 + 16), a1);
*(_DWORD *)(a3 + 24) = -1;
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
result = *(_DWORD *)(a3 + 12);
if ( result )
result = fprintf(stderr, a92m, *(_QWORD *)(a3 + 16), a1);
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, int a2, __int64 a3)
{
int result; // eax

if ( !a2 )
{
fprintf(stderr, format, *(_QWORD *)(a3 + 16), a1);
*(_DWORD *)(a3 + 24) = -1;
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
result = *(_DWORD *)(a3 + 12);
if ( result )
result = fprintf(stderr, a92m, *(_QWORD *)(a3 + 16), a1);
return result;
}

original_code:
extern void should(const char* message, int valid, TestContext *tc)
{
    if (!valid)
    {
      fprintf(stderr,"  ""\033[91m" "\xe2\x9c\x97" "\033[0m" " %s should %s\n" , tc->current, message );
      tc->current_test_status=-1;
      pthread_exit( (void*)-1 );
    }
    if (valid && tc->verbose)
    {
      fprintf(stderr,"  ""\033[92m" "\xe2\x9c\x94" "\033[0m" " %s should %s\n", tc->current, message );
    }
}

canonical_original_code:
extern void should(char *message, int valid, struct TestContext *tc)
{
    if (!valid)
    {
      fprintf(stderr,"  ""\033[91m" "\xe2\x9c\x97" "\033[0m" " %s should %s\n" , tc->current, message );
      tc->current_test_status=-1;
      pthread_exit( (void *)-1 );
    }
    if (valid && tc->verbose)
    {
      fprintf(stderr,"  ""\033[92m" "\xe2\x9c\x94" "\033[0m" " %s should %s\n", tc->current, message );
    }
}

variable_types:
message
char *

variable_types: valid int

variable_types: struct TestContext { int pass; int fail; int num; int verbose; char *current; int current_test_status; int blocking; void (*test_function)(void *); 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; } lock; long start_time; long stop_time; int single_test; } *


run

decompiled_code:
int __fastcall run(__int64 a1, const char *a2, _DWORD *a3)
{
__int64 v4; // rdx
int result; // eax
FILE *v6; // rdi
unsigned int v7; // eax
void *v8; // [rsp+0h] [rbp-68h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-60h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-58h] BYREF

v4 = (unsigned int)(a3[2] + 1);
result = a3[24];
a3[2] = v4;
if ( result <= 0 || (_DWORD)v4 == result )
{
*((_QWORD *)a3 + 4) = a1;
v6 = stderr;
*((_QWORD *)a3 + 2) = a2;
fprintf(v6, "\x1B[94m%d. %s\x1B[0m\n", v4, a2);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
pthread_attr_setstacksize(&attr, 0x190000uLL);
pthread_create(&newthread, &attr, (void *(*)(void *))threaded_test, a3);
pthread_attr_destroy(&attr);
v7 = pthread_join(newthread, &v8);
if ( v7 == 22 || v7 == 35 )
{
result = fprintf(
stderr,
"\x1B[91mTest Exception in %s line %d - pthread_join returned %d \n\x1B[0m",
"test.c",
176LL,
v7);
}
else if ( (int)a3[6] < 0 )
{
result = fprintf(stderr, "\x1B[91m%s failed \n\x1B[0m\n", a2);
++a3[1];
}
else
{
result = fprintf(stderr, "\x1B[92m%s passed \n\x1B[0m\n", a2);
++*a3;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, const char *a2, _DWORD *a3)
{
__int64 v4; // rdx
int result; // eax
FILE *v6; // rdi
unsigned int v7; // eax
void *v8; // [rsp+0h] [rbp-68h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-60h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-58h] BYREF

v4 = (unsigned int)(a3[2] + 1);
result = a3[24];
a3[2] = v4;
if ( result <= 0 || (_DWORD)v4 == result )
{
*((_QWORD *)a3 + 4) = a1;
v6 = stderr;
*((_QWORD *)a3 + 2) = a2;
fprintf(v6, "\x1B[94m%d. %s\x1B[0m\n", v4, a2);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
pthread_attr_setstacksize(&attr, 0x190000uLL);
pthread_create(&newthread, &attr, (void *(*)(void *))threaded_test, a3);
pthread_attr_destroy(&attr);
v7 = pthread_join(newthread, &v8);
if ( v7 == 22 || v7 == 35 )
{
result = fprintf(
stderr,
"\x1B[91mTest Exception in %s line %d - pthread_join returned %d \n\x1B[0m",
"test.c",
176LL,
v7);
}
else if ( (int)a3[6] < 0 )
{
result = fprintf(stderr, "\x1B[91m%s failed \n\x1B[0m\n", a2);
++a3[1];
}
else
{
result = fprintf(stderr, "\x1B[92m%s passed \n\x1B[0m\n", a2);
++*a3;
}
}
return result;
}

original_code:
extern void run( void (*func)(TestContext*), char * test_name , TestContext *tc)
{
    int rc;
    void* status;
    pthread_t thread;
    pthread_attr_t attr;
    tc->num++;
    if (tc->single_test > 0 && tc->single_test != tc->num ) return;
    tc->current = test_name;
    tc->test_function = (void(*)(void*)) func;
    fprintf(stderr, "\033[94m" "%d. %s" "\033[0m" "\n" , tc->num , test_name );
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
    pthread_attr_setstacksize(&attr,16384*100 );
    pthread_create(&thread, &attr, threaded_test, (void*) tc );
    pthread_attr_destroy(&attr);
    rc = pthread_join( thread, &status);
    if (rc == 22 || rc == 35 )
    {
      fprintf(stderr,"\033[91m" "Test Exception in %s line %d - pthread_join returned %d \n" "\033[0m" ,
        "test.c", 176,rc );
    }
    else
    {
        if (tc->current_test_status < 0)
        {
          fprintf(stderr,"\033[91m" "%s failed \n" "\033[0m" "\n" , test_name );
          tc->fail++;
        }
        else
        {
          fprintf(stderr, "\033[92m" "%s passed \n" "\033[0m" "\n" , test_name );
          tc->pass++;
        }
    }
}

canonical_original_code:
extern void run(void (*func)(struct TestContext *), char *test_name, struct TestContext *tc)
{
    int rc;
    void *status;
    unsigned long thread;
    union pthread_attr_t attr;
    tc->num++;
    if (tc->single_test > 0 && tc->single_test != tc->num ) return;
    tc->current = test_name;
    tc->test_function = (void (*)(void *)) func;
    fprintf(stderr, "\033[94m" "%d. %s" "\033[0m" "\n" , tc->num , test_name );
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
    pthread_attr_setstacksize(&attr,16384*100 );
    pthread_create(&thread, &attr, threaded_test, (void *) tc );
    pthread_attr_destroy(&attr);
    rc = pthread_join( thread, &status);
    if (rc == 22 || rc == 35 )
    {
      fprintf(stderr,"\033[91m" "Test Exception in %s line %d - pthread_join returned %d \n" "\033[0m" ,
        "test.c", 176,rc );
    }
    else
    {
        if (tc->current_test_status < 0)
        {
          fprintf(stderr,"\033[91m" "%s failed \n" "\033[0m" "\n" , test_name );
          tc->fail++;
        }
        else
        {
          fprintf(stderr, "\033[92m" "%s passed \n" "\033[0m" "\n" , test_name );
          tc->pass++;
        }
    }
}

variable_types:
tc
struct TestContext *

variable_types: rc int

variable_types:
status
void *

variable_types:
thread
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }

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


decompiled_code:
// attributes: thunk
__int64 test_debug()
{
return proto_debug();
}

canonical_decompiled_code:
__int64 func8()
{
return func1();
}

original_code:
extern int test_debug()
{
  return proto_debug();
}