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

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v2 = *((_DWORD *)&Proto_Server + 16);
if ( v2 > 1023 )
{
v3 = 0LL;
LABEL_5:
while ( *((_DWORD *)&Proto_Server + v3 + 18) != -1 )
{
if ( ++v3 == 1024 )
{
v4 = -1;
goto LABEL_9;
}
}
*((_DWORD *)&Proto_Server + (int)v3 + 18) = a1;
++*((_DWORD *)&Proto_Server + 17);
*((_DWORD *)&Proto_Server + 16) = v3 + 1;
*a2 = v3;
v4 = 1;
}
else
{
v3 = 0LL;
if ( *((_DWORD *)&Proto_Server + v2 + 18) != -1 )
goto LABEL_5;
*((_DWORD *)&Proto_Server + v2 + 18) = a1;
++*((_DWORD *)&Proto_Server + 17);
*a2 = v2;
++*((_DWORD *)&Proto_Server + 16);
v4 = 1;
}
LABEL_9:
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 v3; // rax
unsigned int v4; // ebx

pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&Proto_Server + 24));
v2 = *((_DWORD *)&Proto_Server + 16);
if ( v2 > 1023 )
{
v3 = 0LL;
LABEL_5:
while ( *((_DWORD *)&Proto_Server + v3 + 18) != -1 )
{
if ( ++v3 == 1024 )
{
v4 = -1;
goto LABEL_9;
}
}
*((_DWORD *)&Proto_Server + (int)v3 + 18) = a1;
++*((_DWORD *)&Proto_Server + 17);
*((_DWORD *)&Proto_Server + 16) = v3 + 1;
*a2 = v3;
v4 = 1;
}
else
{
v3 = 0LL;
if ( *((_DWORD *)&Proto_Server + v2 + 18) != -1 )
goto LABEL_5;
*((_DWORD *)&Proto_Server + v2 + 18) = a1;
++*((_DWORD *)&Proto_Server + 17);
*a2 = v2;
++*((_DWORD *)&Proto_Server + 16);
v4 = 1;
}
LABEL_9:
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: rc int

variable_types: i int


repo: samirahmed/bogosort

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

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

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

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

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:
arg
void *

variable_types: connfd int

variable_types:
tid
unsigned long

return_type: void *


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

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

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

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

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; // rbx
unsigned int v3; // eax
unsigned int v4; // ebx
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() )
{
v3 = proto_session_hdr_unmarshall_type(fd);
fprintf(stderr, "proto_rpc_dispatcher: type=%d \n", v3);
}
}
if ( v4 - 1 <= 3 )
v1 = (int (__fastcall *)(int *))*((_QWORD *)&Proto_Server + v4 + 16921);
}
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; // rbx
unsigned int v3; // eax
unsigned int v4; // ebx
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() )
{
v3 = proto_session_hdr_unmarshall_type(fd);
fprintf(stderr, "proto_rpc_dispatcher: type=%d \n", v3);
}
}
if ( v4 - 1 <= 3 )
v1 = (int (__fastcall *)(int *))*((_QWORD *)&Proto_Server + v4 + 16921);
}
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]; }

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; }

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

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


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]; } *

return_type: int


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 )
{
*((_QWORD *)&Proto_Server + (int)v2 + 16922) = a2;
result = 1LL;
}
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 )
{
*((_QWORD *)&Proto_Server + (int)v2 + 16922) = a2;
result = 1LL;
}
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;
  }
}

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 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);
}

return_type: void


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
{
v2 = *(_DWORD *)v1;
*((_DWORD *)&Proto_Server + 1042) = *(_DWORD *)v1;
if ( v2 != -1 )
{
--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
{
v2 = *(_DWORD *)v1;
*((_DWORD *)&Proto_Server + 1042) = *(_DWORD *)v1;
if ( v2 != -1 )
{
--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 i; // ebx
unsigned __int64 v1; // rax
int result; // eax

proto_session_init((unsigned int)((_DWORD)&Proto_Server + 4168));
proto_server_set_session_lost_handler((__int64)proto_session_lost_default_handler);
for ( i = 1; i != 5; ++i )
proto_server_set_req_handler(i, (__int64)proto_server_mt_null_handler);
v1 = (unsigned int)((_DWORD)&Proto_Server + 72);
do
{
*(_DWORD *)v1 = -1;
v1 += 4LL;
}
while ( v1 != (_DWORD)&Proto_Server + 4168 );
*((_DWORD *)&Proto_Server + 17) = 0;
*((_DWORD *)&Proto_Server + 16) = 0;
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 i; // ebx
unsigned __int64 v1; // rax
int result; // eax

proto_session_init((unsigned int)((_DWORD)&Proto_Server + 4168));
func10((__int64)proto_session_lost_default_handler);
for ( i = 1; i != 5; ++i )
func11(i, (__int64)proto_server_mt_null_handler);
v1 = (unsigned int)((_DWORD)&Proto_Server + 72);
do
{
*(_DWORD *)v1 = -1;
v1 += 4LL;
}
while ( v1 != (_DWORD)&Proto_Server + 4168 );
*((_DWORD *)&Proto_Server + 17) = 0;
*((_DWORD *)&Proto_Server + 16) = 0;
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:
__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:
__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; // ebp
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int64 v11[4]; // [rsp+0h] [rbp-48h] BYREF
__int64 v12; // [rsp+20h] [rbp-28h]
__int64 v13; // [rsp+28h] [rbp-20h]

if ( a2 )
{
LODWORD(v11[0]) = 0;
v11[1] = 0LL;
v11[2] = 0LL;
v11[3] = 0LL;
v12 = 0LL;
v13 = 0LL;
HIDWORD(v11[0]) = a2;
if ( a3 )
HIDWORD(v12) = a3;
proto_session_hdr_marshall(a1, v11);
}
if ( a4 )
proto_session_body_marshall_int(a1, a4);
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; // ebp
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int64 v11[4]; // [rsp+0h] [rbp-48h] BYREF
__int64 v12; // [rsp+20h] [rbp-28h]
__int64 v13; // [rsp+28h] [rbp-20h]

if ( a2 )
{
LODWORD(v11[0]) = 0;
v11[1] = 0LL;
v11[2] = 0LL;
v11[3] = 0LL;
v12 = 0LL;
v13 = 0LL;
HIDWORD(v11[0]) = a2;
if ( a3 )
HIDWORD(v12) = a3;
proto_session_hdr_marshall(a1, v11);
}
if ( a4 )
proto_session_body_marshall_int(a1, a4);
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


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)
{
pthread_t v1; // rax

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

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

v1 = pthread_self();
pthread_detach(v1);
while ( (unsigned int)proto_session_rcv_msg((char *)a1 + 131192) == 1 )
{
if ( (unsigned int)proto_session_hdr_unmarshall_type((char *)a1 + 131192) == 13
&& (*((int (__fastcall **)(char *))a1 + 32800))((char *)a1 + 131192) < 0 )
{
goto LABEL_7;
}
}
(*((void (__fastcall **)(char *))a1 + 32799))((char *)a1 + 131192);
LABEL_7:
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: 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;
  }
}


decompiled_code:
__int64 __fastcall proto_client_init(__int64 *a1)
{
void *v1; // rax
__int64 v2; // rbx

v1 = malloc(0x40108uLL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (__int64)v1;
memset(v1, 0, 0x40108uLL);
proto_client_set_session_lost_handler(v2, (__int64)proto_client_session_lost_default_hdlr);
proto_client_set_event_handler(v2, 13, (__int64)proto_client_event_null_handler);
*a1 = v2;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1)
{
void *v1; // rax
__int64 v2; // rbx

v1 = malloc(0x40108uLL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (__int64)v1;
memset(v1, 0, 0x40108uLL);
func5(v2, (__int64)proto_client_session_lost_default_hdlr);
func6(v2, 13, (__int64)proto_client_event_null_handler);
*a1 = v2;
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:
__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 *

variable_types: rc int


decompiled_code:
__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; // rbx
__int64 v5; // r12
__int64 result; // rax

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

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

if ( a3 <= 0 )
return (unsigned int)a2;
v4 = a4;
v5 = a4 + 4LL * (unsigned int)a3;
do
{
result = proto_session_body_unmarshall_int(a1, a2, v4);
a2 = (unsigned int)result;
v4 += 4LL;
}
while ( v4 != v5 );
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; // er12
int v2; // ebx
struct timespec v3; // [rsp+0h] [rbp-38h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-28h] BYREF

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

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

requested_time.tv_nsec = 0LL;
v1 = 1;
if ( *((int *)a1 + 14) > 0 )
v1 = *((_DWORD *)a1 + 14);
v2 = 0;
do
{
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = (int)randint() % 10;
(*((void (__fastcall **)(void *))a1 + 6))(a1);
nanosleep(&requested_time, &v3);
++v2;
}
while ( v1 != v2 );
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; }

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; }


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 test_nanosleep()
{
struct timespec v1; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = (int)randint() % 5;
return nanosleep(&requested_time, &v1);
}

canonical_decompiled_code:
int func5()
{
struct timespec v1; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = (int)randint() % 5;
return nanosleep(&requested_time, &v1);
}

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);
}
if ( *(_DWORD *)(a3 + 12) )
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);
}
if ( *(_DWORD *)(a3 + 12) )
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
unsigned int v6; // eax
pthread_attr_t v7; // [rsp+0h] [rbp-68h] BYREF
pthread_t newthread; // [rsp+40h] [rbp-28h] BYREF
void *thread_return; // [rsp+48h] [rbp-20h] BYREF

v4 = (unsigned int)(a3[2] + 1);
a3[2] = v4;
result = a3[24];
if ( (_DWORD)v4 == result || result <= 0 )
{
*((_QWORD *)a3 + 2) = a2;
*((_QWORD *)a3 + 4) = a1;
fprintf(stderr, "\x1B[94m%d. %s\x1B[0m\n", v4, a2);
pthread_attr_init(&v7);
pthread_attr_setdetachstate(&v7, 0);
pthread_attr_setstacksize(&v7, 0x190000uLL);
pthread_create(&newthread, &v7, (void *(*)(void *))threaded_test, a3);
pthread_attr_destroy(&v7);
v6 = pthread_join(newthread, &thread_return);
if ( v6 == 22 || v6 == 35 )
{
result = fprintf(
stderr,
"\x1B[91mTest Exception in %s line %d - pthread_join returned %d \n\x1B[0m",
"test.c",
176LL,
v6);
}
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
unsigned int v6; // eax
pthread_attr_t v7; // [rsp+0h] [rbp-68h] BYREF
pthread_t newthread; // [rsp+40h] [rbp-28h] BYREF
void *thread_return; // [rsp+48h] [rbp-20h] BYREF

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


parallelize

decompiled_code:
int __fastcall parallelize(__int64 a1, int a2, int a3)
{
int v4; // er13
pthread_t *v5; // r15
int v6; // er14
pthread_t *v7; // r12
int i; // ebx
int v9; // eax
unsigned int v10; // ebp
int v11; // eax
__int64 v12; // rbx
int v14; // [rsp+8h] [rbp-90h]
struct sched_param param; // [rsp+1Ch] [rbp-7Ch] BYREF
pthread_attr_t attr; // [rsp+20h] [rbp-78h] BYREF

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
pthread_attr_setstacksize(&attr, 0x8000uLL);
param.__sched_priority = 0;
if ( a3 <= 0 )
{
v5 = (pthread_t *)malloc(8uLL);
v6 = sched_get_priority_min(0);
v14 = sched_get_priority_max(0);
v4 = 1;
}
else
{
v4 = a2 * a3;
v5 = (pthread_t *)malloc(8LL * v4);
v6 = sched_get_priority_min(0);
v14 = sched_get_priority_max(0);
if ( v4 <= 0 )
{
if ( (unsigned int)proto_debug() )
{
fprintf(stderr, "%s_%d main thread suspending\n", "test.c", 102LL);
goto LABEL_16;
}
goto LABEL_13;
}
}
v7 = v5;
for ( i = 0; i < v4; ++i )
{
v9 = 0;
if ( v6 >= 0 )
v9 = v6;
if ( v9 < v14 )
{
param.__sched_priority = v6 + (int)randint() % v14;
pthread_attr_setschedparam(&attr, &param);
}
v10 = pthread_create(v7, &attr, (void *(*)(void *))threaded_task, (void *)(a1 + ((__int64)(i % a2) << 6)));
v11 = test_debug();
if ( v10 && v11 )
fprintf(stderr, "%s_%d pthread_create failed %d\n", "test.c", 99LL, v10);
++v7;
}
if ( !(unsigned int)proto_debug() )
{
LABEL_13:
if ( v4 <= 0 )
goto LABEL_18;
goto LABEL_14;
}
fprintf(stderr, "%s_%d main thread suspending\n", "test.c", 102LL);
LABEL_14:
v12 = 0LL;
do
pthread_join(v5[v12++], 0LL);
while ( v4 > (int)v12 );
LABEL_16:
if ( (unsigned int)proto_debug() )
fprintf(stderr, "%s_%d main thread resuming\n", "test.c", 109LL);
LABEL_18:
free(v5);
return pthread_attr_destroy(&attr);
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, int a2, int a3)
{
int v4; // er13
pthread_t *v5; // r15
int v6; // er14
pthread_t *v7; // r12
int i; // ebx
int v9; // eax
unsigned int v10; // ebp
int v11; // eax
__int64 v12; // rbx
int v14; // [rsp+8h] [rbp-90h]
struct sched_param param; // [rsp+1Ch] [rbp-7Ch] BYREF
pthread_attr_t attr; // [rsp+20h] [rbp-78h] BYREF

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
pthread_attr_setstacksize(&attr, 0x8000uLL);
param.__sched_priority = 0;
if ( a3 <= 0 )
{
v5 = (pthread_t *)malloc(8uLL);
v6 = sched_get_priority_min(0);
v14 = sched_get_priority_max(0);
v4 = 1;
}
else
{
v4 = a2 * a3;
v5 = (pthread_t *)malloc(8LL * v4);
v6 = sched_get_priority_min(0);
v14 = sched_get_priority_max(0);
if ( v4 <= 0 )
{
if ( (unsigned int)func1() )
{
fprintf(stderr, "%s_%d main thread suspending\n", "test.c", 102LL);
goto LABEL_16;
}
goto LABEL_13;
}
}
v7 = v5;
for ( i = 0; i < v4; ++i )
{
v9 = 0;
if ( v6 >= 0 )
v9 = v6;
if ( v9 < v14 )
{
param.__sched_priority = v6 + (int)randint() % v14;
pthread_attr_setschedparam(&attr, &param);
}
v10 = pthread_create(v7, &attr, (void *(*)(void *))threaded_task, (void *)(a1 + ((__int64)(i % a2) << 6)));
v11 = func8();
if ( v10 && v11 )
fprintf(stderr, "%s_%d pthread_create failed %d\n", "test.c", 99LL, v10);
++v7;
}
if ( !(unsigned int)func1() )
{
LABEL_13:
if ( v4 <= 0 )
goto LABEL_18;
goto LABEL_14;
}
fprintf(stderr, "%s_%d main thread suspending\n", "test.c", 102LL);
LABEL_14:
v12 = 0LL;
do
pthread_join(v5[v12++], 0LL);
while ( v4 > (int)v12 );
LABEL_16:
if ( (unsigned int)func1() )
fprintf(stderr, "%s_%d main thread resuming\n", "test.c", 109LL);
LABEL_18:
free(v5);
return pthread_attr_destroy(&attr);
}

original_code:
extern void parallelize(Task tasks[], int num_tasks, int threads_per_task)
{
  int ii, thread_count;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
  pthread_attr_setstacksize(&attr,16384*2);
  struct sched_param param;
  bzero(&param,sizeof(struct sched_param));
  thread_count = (threads_per_task < 1 ? 1 : threads_per_task*num_tasks);
  pthread_t * threads;
  threads = (pthread_t *) malloc(sizeof(pthread_t)*thread_count);
  int min = sched_get_priority_min(0);
  int max = sched_get_priority_max(0);
  for (ii=0;ii < thread_count ;ii++)
  {
    if (max > min && max > 0)
    {
      param.sched_priority = (randint()%max)+min;
      pthread_attr_setschedparam(&attr, &param);
    }
    int rc;
    rc = pthread_create(&threads[ii], &attr, threaded_task, (void*) &tasks[ii%num_tasks] );
    if (test_debug() && rc!=0 )
      fprintf(stderr,"%s_%d pthread_create failed %d\n","test.c",99,rc);
  }
  if (proto_debug() ) fprintf(stderr,"%s_%d main thread suspending\n", "test.c", 102 );
  for (ii=0;ii < thread_count ;ii++)
  {
    pthread_join(threads[ii],((void *)0));
  }
  if (proto_debug() ) fprintf(stderr,"%s_%d main thread resuming\n", "test.c", 109 );
  free(threads);
  pthread_attr_destroy(&attr);
}

canonical_original_code:
extern void parallelize(struct Task tasks[], int num_tasks, int threads_per_task)
{
  int ii; int thread_count;
  union pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
  pthread_attr_setstacksize(&attr,16384*2);
  struct sched_param param;
  bzero(&param,sizeof(struct sched_param));
  thread_count = (threads_per_task < 1 ? 1 : threads_per_task*num_tasks);
  unsigned long *threads;
  threads = (unsigned long *) malloc(sizeof(pthread_t)*thread_count);
  int min = sched_get_priority_min(0);
  int max = sched_get_priority_max(0);
  for (ii=0;ii < thread_count ;ii++)
  {
    if (max > min && max > 0)
    {
      param.sched_priority = (randint()%max)+min;
      pthread_attr_setschedparam(&attr, &param);
    }
    int rc;
    rc = pthread_create(&threads[ii], &attr, threaded_task, (void *) &tasks[ii%num_tasks] );
    if (test_debug() && rc!=0 )
      fprintf(stderr,"%s_%d pthread_create failed %d\n","test.c",99,rc);
  }
  if (proto_debug() ) fprintf(stderr,"%s_%d main thread suspending\n", "test.c", 102 );
  for (ii=0;ii < thread_count ;ii++)
  {
    pthread_join(threads[ii],((void *)0));
  }
  if (proto_debug() ) fprintf(stderr,"%s_%d main thread resuming\n", "test.c", 109 );
  free(threads);
  pthread_attr_destroy(&attr);
}

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

variable_types: ii int

variable_types: struct sched_param { int sched_priority; }

variable_types:
threads
unsigned long *

variable_types: min int

variable_types: max int

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


decompiled_code:
int __fastcall test_summary(unsigned int *a1)
{
time_t v1; // rax
int v2; // ebp
__int64 v3; // rdx
__int64 v4; // rdx
int result; // eax

v1 = time(0LL);
*((_QWORD *)a1 + 11) = v1;
v2 = v1 - a1[20];
fprintf(stderr, "\x1B[94m%d\x1B[0m test run", *a1 + a1[1]);
if ( v2 > 0 )
fprintf(stderr, " in %d seconds", (unsigned int)v2);
fputc(10, stderr);
v3 = *a1;
if ( (int)v3 > 0 )
fprintf(stderr, "\x1B[92m%d\x1B[0m passed, ", v3);
v4 = a1[1];
if ( (int)v4 <= 0 )
result = fwrite("no failed tests\n", 1uLL, 0x10uLL, stderr);
else
result = fprintf(stderr, "\x1B[91m%d\x1B[0m failed.", v4);
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int *a1)
{
time_t v1; // rax
int v2; // ebp
__int64 v3; // rdx
__int64 v4; // rdx
int result; // eax

v1 = time(0LL);
*((_QWORD *)a1 + 11) = v1;
v2 = v1 - a1[20];
fprintf(stderr, "\x1B[94m%d\x1B[0m test run", *a1 + a1[1]);
if ( v2 > 0 )
fprintf(stderr, " in %d seconds", (unsigned int)v2);
fputc(10, stderr);
v3 = *a1;
if ( (int)v3 > 0 )
fprintf(stderr, "\x1B[92m%d\x1B[0m passed, ", v3);
v4 = a1[1];
if ( (int)v4 <= 0 )
result = fwrite("no failed tests\n", 1uLL, 0x10uLL, stderr);
else
result = fprintf(stderr, "\x1B[91m%d\x1B[0m failed.", v4);
return result;
}

original_code:
extern void test_summary(TestContext *tc)
{
    int seconds;
    tc->stop_time = time(((void *)0));
    seconds = tc->stop_time - tc->start_time;
    fprintf(stderr, "\033[94m" "%d" "\033[0m" " test run", tc->pass+tc->fail);
    if (seconds>0) fprintf(stderr, " in %d seconds", seconds);
    fprintf(stderr,"\n");
    if (tc->pass > 0) fprintf(stderr, "\033[92m" "%d" "\033[0m" " passed, ", tc->pass );
    if (tc->fail > 0) fprintf(stderr, "\033[91m" "%d" "\033[0m" " failed.", tc->fail );
    else fprintf(stderr, "no failed tests\n");
}

canonical_original_code:
extern void test_summary(struct TestContext *tc)
{
    int seconds;
    tc->stop_time = time(((void *)0));
    seconds = tc->stop_time - tc->start_time;
    fprintf(stderr, "\033[94m" "%d" "\033[0m" " test run", tc->pass+tc->fail);
    if (seconds>0) fprintf(stderr, " in %d seconds", seconds);
    fprintf(stderr,"\n");
    if (tc->pass > 0) fprintf(stderr, "\033[92m" "%d" "\033[0m" " passed, ", tc->pass );
    if (tc->fail > 0) fprintf(stderr, "\033[91m" "%d" "\033[0m" " failed.", tc->fail );
    else fprintf(stderr, "no failed tests\n");
}

variable_types: seconds int


original_code:
extern void test_init(int argc, char** argv, TestContext *tc)
{
  bzero(tc,sizeof(TestContext));
  if (argc > 1)
  {
    int argument;
    for (argument = 1; argument<argc; argument++)
    {
      if (strncmp(argv[argument],"-v",sizeof("-v")-1) == 0) tc->verbose = 1 ;
      if (strncmp(argv[argument],"--verbose",sizeof("--verbose")-1) == 0) tc->verbose = 1 ;
      if (strncmp(argv[argument],"-d",sizeof("-d")-1) == 0) { proto_debug_on() ; tc->verbose=1; }
      if (strncmp(argv[argument],"--debug",sizeof("--debug")-1) == 0){ proto_debug_on() ; tc->verbose=1;}
      if (strncmp(argv[argument],"--only",sizeof("--only")-1) == 0)
      {
        if (argument+1<argc)
        {
          int only;
          if ( (only = atoi(argv[argument+1]) ) >0 )
          {
            tc->single_test = only;
          }
        }
      }
    }
  }
  tc->pass=0;
  tc->fail=0;
  tc->num =0;
  tc->start_time = time(((void *)0));
  fprintf(stderr,"\n" "\033[95m" "%s---------------------\n" "\033[0m" "\n",argv[0]);
}

canonical_original_code:
extern void test_init(int argc, char **argv, struct TestContext *tc)
{
  bzero(tc,sizeof(TestContext));
  if (argc > 1)
  {
    int argument;
    for (argument = 1; argument<argc; argument++)
    {
      if (strncmp(argv[argument],"-v",sizeof("-v")-1) == 0) tc->verbose = 1 ;
      if (strncmp(argv[argument],"--verbose",sizeof("--verbose")-1) == 0) tc->verbose = 1 ;
      if (strncmp(argv[argument],"-d",sizeof("-d")-1) == 0) { proto_debug_on() ; tc->verbose=1; }
      if (strncmp(argv[argument],"--debug",sizeof("--debug")-1) == 0){ proto_debug_on() ; tc->verbose=1;}
      if (strncmp(argv[argument],"--only",sizeof("--only")-1) == 0)
      {
        if (argument+1<argc)
        {
          int only;
          if ( (only = atoi(argv[argument+1]) ) >0 )
          {
            tc->single_test = only;
          }
        }
      }
    }
  }
  tc->pass=0;
  tc->fail=0;
  tc->num =0;
  tc->start_time = time(((void *)0));
  fprintf(stderr,"\n" "\033[95m" "%s---------------------\n" "\033[0m" "\n",argv[0]);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: argument int

variable_types: only int


decompiled_code:
__int64 __fastcall net_listen(int a1)
{
int v1; // edx
__int64 result; // rax
int *v3; // rax

v1 = listen(a1, 1024);
result = 1LL;
if ( v1 < 0 )
{
v3 = _errno_location();
fprintf(stderr, "Error: EventListen listen failed (%d)\n", (unsigned int)*v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // edx
__int64 result; // rax
int *v3; // rax

v1 = listen(a1, 1024);
result = 1LL;
if ( v1 < 0 )
{
v3 = _errno_location();
fprintf(stderr, "Error: EventListen listen failed (%d)\n", (unsigned int)*v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
extern int
net_listen(FDType fd)
{
  if (listen(fd, 1024) < 0) {
    fprintf(stderr, "Error: EventListen listen failed (%d)\n", (*__errno_location ()));
    return -1;
  }
  return 1;
}

canonical_original_code:
extern int net_listen(int fd)
{
  if (listen(fd, 1024) < 0) {
    fprintf(stderr, "Error: EventListen listen failed (%d)\n", (*__errno_location ()));
    return -1;
  }
  return 1;
}

variable_types: fd int


decompiled_code:
int __fastcall net_accept(int a1)
{
return accept(a1, 0LL, 0LL);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
return accept(a1, 0LL, 0LL);
}

original_code:
extern FDType
net_accept(FDType fd)
{
  FDType nfd;
  nfd=accept(fd, ((void *)0), ((void *)0));
  return nfd;
}

canonical_original_code:
extern int net_accept(int fd)
{
  int nfd;
  nfd=accept(fd, ((void *)0), ((void *)0));
  return nfd;
}

variable_types: nfd int


decompiled_code:
__int64 __fastcall net_setup_listen_socket(int *a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebx
int *v5; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
int *v9; // rax
socklen_t len; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
len = 16;
v2 = socket(2, 1, 6);
if ( v2 < 0 )
{
v5 = _errno_location();
fprintf(stderr, "Error: server socket create failed (%d)\n", (unsigned int)*v5);
exit(1);
}
v3 = v2;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "setsockopt(SO_REUSEADDR): %s\n", v7);
exit(1);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
if ( *a2 )
*(_WORD *)addr.sa_data = _bswap_16(*a2);
if ( bind(v3, &addr, len) < 0 )
{
v8 = _errno_location();
fprintf(stderr, "Error: server bind failed (%d)\n", (unsigned int)*v8);
close(v3);
exit(1);
}
if ( !*a2 )
{
if ( getsockname(v3, &addr, &len) < 0 )
{
v9 = _errno_location();
fprintf(stderr, "Error server getsocketname failed (%d)\n", (unsigned int)*v9);
close(v3);
exit(1);
}
*a2 = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
}
*a1 = v3;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebx
int *v5; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
int *v9; // rax
socklen_t len; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
len = 16;
v2 = socket(2, 1, 6);
if ( v2 < 0 )
{
v5 = _errno_location();
fprintf(stderr, "Error: server socket create failed (%d)\n", (unsigned int)*v5);
exit(1);
}
v3 = v2;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "setsockopt(SO_REUSEADDR): %s\n", v7);
exit(1);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func0(0);
if ( *a2 )
*(_WORD *)addr.sa_data = func1(*a2);
if ( bind(v3, &addr, len) < 0 )
{
v8 = _errno_location();
fprintf(stderr, "Error: server bind failed (%d)\n", (unsigned int)*v8);
close(v3);
exit(1);
}
if ( !*a2 )
{
if ( getsockname(v3, &addr, &len) < 0 )
{
v9 = _errno_location();
fprintf(stderr, "Error server getsocketname failed (%d)\n", (unsigned int)*v9);
close(v3);
exit(1);
}
*a2 = (unsigned __int16)func1(*(__int16 *)addr.sa_data);
}
*a1 = v3;
return 1LL;
}

original_code:
extern int
net_setup_listen_socket(FDType *fd, PortType *port)
{
    struct sockaddr_in name;
    int one = 1;
    FDType fd_listen;
    socklen_t addrLen = sizeof(struct sockaddr);
    int rc;
    fd_listen = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (fd_listen < 0) {
        fprintf(stderr, "Error: server socket create failed (%d)\n", (*__errno_location ()));
        exit(1);
    }
    rc = setsockopt(fd_listen, 1, 2, &one, sizeof(one));
    if (rc != 0) {
        fprintf(stderr, "setsockopt(SO_REUSEADDR): %s\n", strerror((*__errno_location ())));
        exit(1);
    }
    bzero(&name, sizeof(name));
    name.sin_family = 2;
    name.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    if (*port) name.sin_port = htons(*port);
    else name.sin_port = 0;
    rc = bind(fd_listen, (struct sockaddr *)&name, addrLen);
    if (rc < 0) {
        fprintf(stderr, "Error: server bind failed (%d)\n", (*__errno_location ()));
        close (fd_listen);
        exit(1);
    }
    if (*port == 0) {
        rc = getsockname(fd_listen, (struct sockaddr *)&name, &addrLen);
        if (rc < 0) {
            fprintf(stderr, "Error server getsocketname failed (%d)\n", (*__errno_location ()));
            close(fd_listen);
            exit(1);
        }
        *port = ntohs(name.sin_port);
    }
    *fd = fd_listen;
    return 1;
}

canonical_original_code:
extern int net_setup_listen_socket(int *fd, int *port)
{
    struct sockaddr_in name;
    int one = 1;
    int fd_listen;
    unsigned int addrLen = sizeof(struct sockaddr);
    int rc;
    fd_listen = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (fd_listen < 0) {
        fprintf(stderr, "Error: server socket create failed (%d)\n", (*__errno_location ()));
        exit(1);
    }
    rc = setsockopt(fd_listen, 1, 2, &one, sizeof(one));
    if (rc != 0) {
        fprintf(stderr, "setsockopt(SO_REUSEADDR): %s\n", strerror((*__errno_location ())));
        exit(1);
    }
    bzero(&name, sizeof(name));
    name.sin_family = 2;
    name.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    if (*port) name.sin_port = htons(*port);
    else name.sin_port = 0;
    rc = bind(fd_listen, (struct sockaddr *)&name, addrLen);
    if (rc < 0) {
        fprintf(stderr, "Error: server bind failed (%d)\n", (*__errno_location ()));
        close (fd_listen);
        exit(1);
    }
    if (*port == 0) {
        rc = getsockname(fd_listen, (struct sockaddr *)&name, &addrLen);
        if (rc < 0) {
            fprintf(stderr, "Error server getsocketname failed (%d)\n", (*__errno_location ()));
            close(fd_listen);
            exit(1);
        }
        *port = ntohs(name.sin_port);
    }
    *fd = fd_listen;
    return 1;
}

variable_types:
fd
int *

variable_types:
port
int *

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

variable_types: one int

variable_types:
addrLen
unsigned int

variable_types: rc int

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

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


decompiled_code:
__int64 __fastcall net_setup_connection(int *a1, const char *a2, unsigned int a3)
{
struct hostent *v4; // rax
int v5; // ebx
int v6; // edx
__int64 result; // rax
struct sockaddr v8; // [rsp+0h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v4 = gethostbyname(a2);
if ( v4 )
{
v8.sa_family = v4->h_addrtype;
memcpy(&v8.sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
*(_WORD *)v8.sa_data = _bswap_16(a3);
v5 = socket(2, 1, 0);
*a1 = v5;
if ( v5 < 0 )
{
fwrite("unable to create socket", 1uLL, 0x17uLL, stderr);
perror("oops ");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(0);
if ( bind(v5, &addr, 0x10u) < 0 )
{
fprintf(stderr, "cannot bind port TCP %u\n", a3);
perror("oops ");
result = 0xFFFFFFFFLL;
}
else
{
v6 = connect(*a1, &v8, 0x10u);
result = 1LL;
if ( v6 < 0 )
{
perror("cannot connect ");
result = 0xFFFFFFFFLL;
}
}
}
}
else
{
fprintf(stderr, "unknown host '%s'\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1, const char *a2, unsigned int a3)
{
struct hostent *v4; // rax
int v5; // ebx
int v6; // edx
__int64 result; // rax
struct sockaddr v8; // [rsp+0h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v4 = gethostbyname(a2);
if ( v4 )
{
v8.sa_family = v4->h_addrtype;
memcpy(&v8.sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
*(_WORD *)v8.sa_data = func1(a3);
v5 = socket(2, 1, 0);
*a1 = v5;
if ( v5 < 0 )
{
fwrite("unable to create socket", 1uLL, 0x17uLL, stderr);
perror("oops ");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = func0(0);
*(_WORD *)addr.sa_data = func1(0);
if ( bind(v5, &addr, 0x10u) < 0 )
{
fprintf(stderr, "cannot bind port TCP %u\n", a3);
perror("oops ");
result = 0xFFFFFFFFLL;
}
else
{
v6 = connect(*a1, &v8, 0x10u);
result = 1LL;
if ( v6 < 0 )
{
perror("cannot connect ");
result = 0xFFFFFFFFLL;
}
}
}
}
else
{
fprintf(stderr, "unknown host '%s'\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
extern int
net_setup_connection(FDType *fd, char *host, PortType port)
{
    int rc;
    struct sockaddr_in localAddr, servAddr;
    struct hostent *h;
    h = gethostbyname(host);
    if (h == ((void *)0)) {
        fprintf(stderr, "unknown host '%s'\n", host);
        return -1;
    }
    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = htons(port);
    *fd = socket(2, SOCK_STREAM, 0);
    if (*fd<0) {
        fprintf(stderr, "unable to create socket");
        perror("oops ");
        return -1;
    }
    localAddr.sin_family = 2;
    localAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    localAddr.sin_port = htons(0);
    rc = bind(*fd, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (rc<0) {
        fprintf(stderr, "cannot bind port TCP %u\n", port);
        perror("oops ");
        return -1;
    }
    rc = connect(*fd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if (rc<0) {
        perror("cannot connect ");
        return -1;
    }
    return 1;
}

canonical_original_code:
extern int net_setup_connection(int *fd, char *host, int port)
{
    int rc;
    struct sockaddr_in localAddr; struct sockaddr_in servAddr;
    struct hostent *h;
    h = gethostbyname(host);
    if (h == ((void *)0)) {
        fprintf(stderr, "unknown host '%s'\n", host);
        return -1;
    }
    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = htons(port);
    *fd = socket(2, SOCK_STREAM, 0);
    if (*fd<0) {
        fprintf(stderr, "unable to create socket");
        perror("oops ");
        return -1;
    }
    localAddr.sin_family = 2;
    localAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    localAddr.sin_port = htons(0);
    rc = bind(*fd, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (rc<0) {
        fprintf(stderr, "cannot bind port TCP %u\n", port);
        perror("oops ");
        return -1;
    }
    rc = connect(*fd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if (rc<0) {
        perror("cannot connect ");
        return -1;
    }
    return 1;
}

variable_types:
host
char *

variable_types: port int

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


decompiled_code:
size_t __fastcall net_writen(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = send(fd, buf, v5, 0x4000);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( v6 >= 0 )
return -1LL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func6(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = send(fd, buf, v5, 0x4000);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( v6 >= 0 )
return -1LL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

original_code:
extern ssize_t
net_writen(FDType fd, const void *vptr, size_t n)
{
  size_t nleft;
  ssize_t nwritten;
  const char *ptr;
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ( (nwritten = send(fd, ptr, nleft, MSG_NOSIGNAL)) <= 0) {
      if (nwritten < 0 && (*__errno_location ()) == 4)
  nwritten = 0;
      else
  return(-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return(n);
}

canonical_original_code:
extern long net_writen(int fd, void *vptr, unsigned long n)
{
  unsigned long nleft;
  long nwritten;
  char *ptr;
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ( (nwritten = send(fd, ptr, nleft, MSG_NOSIGNAL)) <= 0) {
      if (nwritten < 0 && (*__errno_location ()) == 4)
  nwritten = 0;
      else
  return(-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return(n);
}

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nwritten long

variable_types:
ptr
char *

return_type: long