repo: east/php-revsocks

decompiled_code:
// write access to const memory has been detected, the output may be wrong!
__int64 __fastcall s5_new_connect(__int64 a1, _QWORD *a2)
{
__int64 v3; // rsi
_DWORD *v4; // rax
__int64 v5; // rdx
_DWORD *v6; // rbp
struct hostent *v7; // rax
int **v8; // rax
int v9; // edx
int *v10; // rax
unsigned int v11; // eax
__int64 result; // rax
int v13[74]; // [rsp+0h] [rbp-128h] BYREF

v3 = *(unsigned int *)(a1 + 24);
empty_sel_timeout = 1;
if ( (_DWORD)v3 == 3 )
{
v4 = malloc(8uLL);
v5 = *(unsigned int *)(a1 + 156);
*a2 = v4;
v6 = v4;
*v4 = 0;
printf("[s5srv] request %s:%d\n", (const char *)(a1 + 28), v5);
v7 = gethostbyname((const char *)(a1 + 28));
if ( !v7 )
{
*(_BYTE *)s5_new_connect = 0;
BUG();
}
v8 = (int **)v7->h_addr_list;
v9 = *(_DWORD *)(a1 + 156);
v13[0] = 0;
v10 = *v8;
v13[65] = v9;
v13[1] = *v10;
v11 = revsrv_init_conn(&revsrv, v13, v6);
v6[1] = v11;
printf("[s5srv] new rev connection handle %d\n", v11);
result = 0LL;
}
else
{
printf("[s5srv] request invalid addr type %d\n", v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
__int64 v3; // rsi
_DWORD *v4; // rax
__int64 v5; // rdx
_DWORD *v6; // rbp
struct hostent *v7; // rax
int **v8; // rax
int v9; // edx
int *v10; // rax
unsigned int v11; // eax
__int64 result; // rax
int v13[74]; // [rsp+0h] [rbp-128h] BYREF

v3 = *(unsigned int *)(a1 + 24);
empty_sel_timeout = 1;
if ( (_DWORD)v3 == 3 )
{
v4 = malloc(8uLL);
v5 = *(unsigned int *)(a1 + 156);
*a2 = v4;
v6 = v4;
*v4 = 0;
printf("[s5srv] request %s:%d\n", (const char *)(a1 + 28), v5);
v7 = gethostbyname((const char *)(a1 + 28));
if ( !v7 )
{
*(_BYTE *)s5_new_connect = 0;
BUG();
}
v8 = (int **)v7->h_addr_list;
v9 = *(_DWORD *)(a1 + 156);
v13[0] = 0;
v10 = *v8;
v13[65] = v9;
v13[1] = *v10;
v11 = revsrv_init_conn(&revsrv, v13, v6);
v6[1] = v11;
printf("[s5srv] new rev connection handle %d\n", v11);
result = 0LL;
}
else
{
printf("[s5srv] request invalid addr type %d\n", v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
s5_new_connect(S5SRV_CL *cl, void **user_ptr)
{
 struct cl_hndl *cl_hndl;
 empty_sel_timeout = 1;
 if (cl->req.addr_type == S5_DOMAIN)
 {
  cl_hndl = malloc(sizeof(struct cl_hndl));
  *user_ptr = cl_hndl;
  cl_hndl->state = CLSTATE_CONNECTING;
  printf("[s5srv] request %s:%d\n", cl->req.data, cl->req.port);
  struct hostent *host_addr;
  struct netaddr addr;
  host_addr = gethostbyname(cl->req.data);
  if (!(host_addr)) { *((char*)((void *)0)) = 0; }
  addr.port = cl->req.port;
  addr.type = ADDR_IPV4;
  memcpy(addr.addr_data, host_addr->h_addr_list[0], 4);
  cl_hndl->rev_netw_hndl = revsrv_init_conn(&revsrv, &addr, cl_hndl);
  printf("[s5srv] new rev connection handle %d\n", cl_hndl->rev_netw_hndl);
  return 0;
 }
 else
  printf("[s5srv] request invalid addr type %d\n", cl->req.addr_type);
 return -1;
}

canonical_original_code:
int s5_new_connect(struct S5SRV_CL *cl, void **user_ptr)
{
 struct cl_hndl *cl_hndl;
 empty_sel_timeout = 1;
 if (cl->req.addr_type == S5_DOMAIN)
 {
  cl_hndl = malloc(sizeof(struct cl_hndl));
  *user_ptr = cl_hndl;
  cl_hndl->state = CLSTATE_CONNECTING;
  printf("[s5srv] request %s:%d\n", cl->req.data, cl->req.port);
  struct hostent *host_addr;
  struct netaddr addr;
  host_addr = gethostbyname(cl->req.data);
  if (!(host_addr)) { *((char *)((void *)0)) = 0; }
  addr.port = cl->req.port;
  addr.type = ADDR_IPV4;
  memcpy(addr.addr_data, host_addr->h_addr_list[0], 4);
  cl_hndl->rev_netw_hndl = revsrv_init_conn(&revsrv, &addr, cl_hndl);
  printf("[s5srv] new rev connection handle %d\n", cl_hndl->rev_netw_hndl);
  return 0;
 }
 else
  printf("[s5srv] request invalid addr type %d\n", cl->req.addr_type);
 return -1;
}

variable_types: struct S5SRV_CL { int state; int socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; } req; void *conn_cb_user; } *

variable_types: struct cl_hndl { int state; int rev_netw_hndl; } *

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

variable_types: struct netaddr { int type; char addr_data[256]; int port; }

user_defined_types: struct S5SRV_CL { int state; int socket; struct sockaddr_in addr; struct S5SRV_CONN_REQ req; void *conn_cb_user; }


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

result = revsrv_conn_state(&revsrv, *(unsigned int *)(a1 + 4));
if ( (_DWORD)result )
result = (unsigned int)((_DWORD)result != 2) + 1;
return result;
}

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

result = revsrv_conn_state(&revsrv, *(unsigned int *)(a1 + 4));
if ( (_DWORD)result )
result = (unsigned int)((_DWORD)result != 2) + 1;
return result;
}

original_code:
int
s5_conn_state(void *user_ptr)
{
 struct cl_hndl *cl_hndl = user_ptr;
 int state = revsrv_conn_state(&revsrv, cl_hndl->rev_netw_hndl);
 switch(state)
 {
  case REV_CONN_PENDING:
   return S5SRV_CB_CONN_PENDING;
  break;
  case REV_CONN_FAILED:
   return S5SRV_CB_CONN_FAILED;
  break;
  default:
   return S5SRV_CB_CONN_ONLINE;
  break;
 }
}

variable_types: state int

return_type: int

user_defined_types: struct cl_hndl { int state; int rev_netw_hndl; }


decompiled_code:
__int64 __fastcall s5_on_data(__int64 a1, __int64 a2, unsigned int a3)
{
empty_sel_timeout = 1;
return revsrv_cl_send(&revsrv, *(unsigned int *)(a1 + 4), a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, unsigned int a3)
{
empty_sel_timeout = 1;
return revsrv_cl_send(&revsrv, *(unsigned int *)(a1 + 4), a2, a3);
}

original_code:
void
s5_on_data(void *user_ptr, char *data, int size)
{
 empty_sel_timeout = 1;
 struct cl_hndl *cl_hndl = user_ptr;
 revsrv_cl_send(&revsrv, cl_hndl->rev_netw_hndl, data, size);
}

variable_types:
data
char *

variable_types: size int

return_type: void


decompiled_code:
int __fastcall s5_on_disc(__int64 a1)
{
__int64 v1; // rsi

v1 = *(unsigned int *)(a1 + 4);
empty_sel_timeout = 1;
revsrv_cl_close(&revsrv, v1);
return puts("[s5srv] disc");
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 v1; // rsi

v1 = *(unsigned int *)(a1 + 4);
empty_sel_timeout = 1;
revsrv_cl_close(&revsrv, v1);
return puts("[s5srv] disc");
}

original_code:
void
s5_on_disc(void *user_ptr)
{
 struct cl_hndl *cl_hndl = user_ptr;
 empty_sel_timeout = 1;
 revsrv_cl_close(&revsrv, cl_hndl->rev_netw_hndl);
 printf("[s5srv] disc\n");
}


decompiled_code:
__int64 __fastcall s5_get_data(__int64 a1, __int64 a2, unsigned int a3)
{
return revsrv_cl_recv(&revsrv, *(unsigned int *)(a1 + 4), a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3)
{
return revsrv_cl_recv(&revsrv, *(unsigned int *)(a1 + 4), a2, a3);
}

original_code:
int
s5_get_data(void *user_ptr, char *data, int size)
{
 struct cl_hndl *cl_hndl = user_ptr;
 return revsrv_cl_recv(&revsrv, cl_hndl->rev_netw_hndl, data, size);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // ebp
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // er13
unsigned int v8; // eax
int v9; // eax
__suseconds_t v10; // r13
unsigned int v11; // er13
unsigned int v12; // eax
int v13; // eax
struct timeval v14; // [rsp+0h] [rbp-138h] BYREF
fd_set readfds; // [rsp+10h] [rbp-128h] BYREF
fd_set writefds; // [rsp+90h] [rbp-A8h] BYREF

if ( argc == 7 )
{
v4 = atoi(argv[4]);
v5 = atoi(argv[2]);
if ( (unsigned int)revsrv_init(&revsrv, argv[1], v5, argv[3], v4) )
{
puts("failed to init rev server");
}
else
{
v6 = atoi(argv[6]);
if ( !(unsigned int)s5srv_init(
&s5srv,
argv[5],
v6,
s5_new_connect,
s5_conn_state,
s5_on_data,
s5_on_disc,
s5_get_data) )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
v14.tv_sec = 0LL;
v14.tv_usec = 10000LL;
v7 = s5srv_max_block_time(&s5srv);
v8 = revsrv_max_block_time(&revsrv);
v9 = max(v7, v8);
v10 = v9;
if ( v9 != -1 )
{
puts("own timeout control");
v14.tv_sec = 0LL;
v14.tv_usec = v10;
}
if ( empty_sel_timeout )
{
v14.tv_sec = 0LL;
v14.tv_usec = 0LL;
empty_sel_timeout = 0;
}
v11 = s5srv_get_fds(&s5srv, &readfds, &writefds);
v12 = revsrv_get_fds(&revsrv, &readfds, &writefds);
v13 = max(v12, v11);
if ( v13 == -1 )
{
puts("no desc");
usleep(0x3E8u);
}
else
{
select(v13 + 1, &readfds, &writefds, 0LL, &v14);
}
revsrv_tick(&revsrv, &readfds, &writefds);
s5srv_tick_ex(&s5srv, &readfds, &writefds);
}
}
puts("failed to init socks5 server");
}
}
else
{
printf("Usage: %s <bind ip> <bind port> <php url> <http timeout> <socks ip> <socks port>\n", *argv);
}
return 1;
}

original_code:
int
main (int argc, char **argv)
{
 enum
 {
  BIND_IP=1,
  BIND_PORT,
  PHP_URL,
  HTTP_TIMEOUT,
  SOCKS_IP,
  SOCKS_PORT
 };
 if (argc != 7)
 {
  printf("Usage: %s <bind ip> <bind port> <php url> <http timeout> <socks ip> <socks port>\n", argv[0]);
  return 1;
 }
 if (revsrv_init(&revsrv, argv[BIND_IP], atoi(argv[BIND_PORT]),
     argv[PHP_URL], atoi(argv[HTTP_TIMEOUT])) != 0)
 {
  printf("failed to init rev server\n");
  return 1;
 }
 if (s5srv_init(&s5srv, argv[SOCKS_IP], atoi(argv[SOCKS_PORT]), s5_new_connect,
    s5_conn_state, s5_on_data, s5_on_disc, s5_get_data) != 0)
 {
  printf("failed to init socks5 server\n");
  return 1;
 }
 while (1)
 {
  fd_set rfds, wfds;
  int highest_fd = -1, max_block[2], sel_timeout;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 10000;
  max_block[0] = s5srv_max_block_time(&s5srv);
  max_block[1] = revsrv_max_block_time(&revsrv);
  sel_timeout = max(max_block[0], max_block[1]);
  if (sel_timeout != -1)
  {
   printf("own timeout control\n");
   tv.tv_sec = 0;
   tv.tv_usec = sel_timeout;
  }
  if (empty_sel_timeout)
  {
   tv.tv_sec = 0;
   tv.tv_usec = 0;
   empty_sel_timeout = 0;
  }
  highest_fd = max(revsrv_get_fds(&revsrv, &rfds, &wfds),
       s5srv_get_fds(&s5srv, &rfds, &wfds));
  if (highest_fd == -1)
  {
   printf("no desc\n");
   usleep(1000);
  }
  else
   select(highest_fd+1, &rfds, &wfds, ((void *)0), &tv);
  revsrv_tick(&revsrv, &rfds, &wfds);
  s5srv_tick_ex(&s5srv, &rfds, &wfds);
 }
 s5srv_close(&s5srv);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 enum
 {
  BIND_IP=1,
  BIND_PORT,
  PHP_URL,
  HTTP_TIMEOUT,
  SOCKS_IP,
  SOCKS_PORT
 };
 if (argc != 7)
 {
  printf("Usage: %s <bind ip> <bind port> <php url> <http timeout> <socks ip> <socks port>\n", argv[0]);
  return 1;
 }
 if (revsrv_init(&revsrv, argv[BIND_IP], atoi(argv[BIND_PORT]),
     argv[PHP_URL], atoi(argv[HTTP_TIMEOUT])) != 0)
 {
  printf("failed to init rev server\n");
  return 1;
 }
 if (s5srv_init(&s5srv, argv[SOCKS_IP], atoi(argv[SOCKS_PORT]), s5_new_connect,
    s5_conn_state, s5_on_data, s5_on_disc, s5_get_data) != 0)
 {
  printf("failed to init socks5 server\n");
  return 1;
 }
 while (1)
 {
  struct fd_set rfds; struct fd_set wfds;
  int highest_fd = -1; int max_block[2]; int sel_timeout;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 10000;
  max_block[0] = s5srv_max_block_time(&s5srv);
  max_block[1] = revsrv_max_block_time(&revsrv);
  sel_timeout = max(max_block[0], max_block[1]);
  if (sel_timeout != -1)
  {
   printf("own timeout control\n");
   tv.tv_sec = 0;
   tv.tv_usec = sel_timeout;
  }
  if (empty_sel_timeout)
  {
   tv.tv_sec = 0;
   tv.tv_usec = 0;
   empty_sel_timeout = 0;
  }
  highest_fd = max(revsrv_get_fds(&revsrv, &rfds, &wfds),
       s5srv_get_fds(&s5srv, &rfds, &wfds));
  if (highest_fd == -1)
  {
   printf("no desc\n");
   usleep(1000);
  }
  else
   select(highest_fd+1, &rfds, &wfds, ((void *)0), &tv);
  revsrv_tick(&revsrv, &rfds, &wfds);
  s5srv_tick_ex(&s5srv, &rfds, &wfds);
 }
 s5srv_close(&s5srv);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

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

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

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


decompiled_code:
__int64 create_tcp_socket()
{
unsigned int v0; // er12
int *v2; // rax
char *v3; // rax

v0 = socket(2, 1, 0);
if ( v0 == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
printf("socket() : %s\n", v3);
}
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
int *v2; // rax
char *v3; // rax

v0 = socket(2, 1, 0);
if ( v0 == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
printf("socket() : %s\n", v3);
}
return v0;
}

original_code:
int
create_tcp_socket()
{
   int sock = socket(2, SOCK_STREAM, 0);
   if (sock == -1)
   {
    printf("socket() : %s\n", strerror((*__errno_location ())));
    return -1;
   }
   return sock;
}

variable_types: sock int


decompiled_code:
int __fastcall socket_set_block(int a1, int a2)
{
_BOOL4 v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2 == 0;
return ioctl(a1, 0x5421uLL, v3);
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2)
{
_BOOL4 v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2 == 0;
return ioctl(a1, 0x5421uLL, v3);
}

original_code:
void
socket_set_block(int sock, int block)
{
 block = !block;
 ioctl(sock, 0x5421, &block);
}

variable_types: block int


decompiled_code:
int __fastcall socket_set_linger(int a1)
{
__int64 optval; // [rsp+8h] [rbp-10h] BYREF

optval = 1LL;
return setsockopt(a1, 1, 13, &optval, 8u);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
__int64 optval; // [rsp+8h] [rbp-10h] BYREF

optval = 1LL;
return setsockopt(a1, 1, 13, &optval, 8u);
}

original_code:
void
socket_set_linger(int sock)
{
   struct linger li;
   li.l_onoff = 1;
   li.l_linger = 0;
   setsockopt(sock, 1, 13,
      &li, sizeof(struct linger));
}

variable_types: struct linger { int l_onoff; int l_linger; }


min

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

original_code:
int min(int x, int y)
{
 if (x > y)
  return x;
 return y;
}

variable_types: x int

variable_types: y int


max

decompiled_code:
// attributes: thunk
__int64 __fastcall max(unsigned int a1, unsigned int a2)
{
return min(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, unsigned int a2)
{
return func4(a1, a2);
}

original_code:
int max(int x, int y)
{
 if (x > y)
  return x;
 return y;
}


decompiled_code:
__int64 __fastcall fifo_len(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

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

original_code:
uint64_t fifo_len(struct FIFOBUF *buf)
{
 return buf->offset;
}

canonical_original_code:
unsigned long fifo_len(struct FIFOBUF *buf)
{
 return buf->offset;
}

variable_types: struct FIFOBUF { char *data; unsigned long size; unsigned long offset; } *

return_type: unsigned long


decompiled_code:
__int64 __fastcall fifo_space_left(__int64 a1)
{
return *(_QWORD *)(a1 + 8) - *(_QWORD *)(a1 + 16);
}

original_code:
uint64_t fifo_space_left(struct FIFOBUF *buf)
{
 return buf->size - buf->offset;
}

canonical_original_code:
unsigned long fifo_space_left(struct FIFOBUF *buf)
{
 return buf->size - buf->offset;
}

user_defined_types: struct FIFOBUF { char *data; unsigned long size; unsigned long offset; }


decompiled_code:
__int64 __fastcall fifo_write(_QWORD *a1, const void *a2)
{
unsigned __int64 v2; // rax
size_t v3; // rdx
size_t v4; // rbp

v2 = fifo_space_left((__int64)a1);
if ( v2 < v3 )
return 0xFFFFFFFFLL;
v4 = v3;
memcpy((void *)(*a1 + a1[2]), a2, v3);
a1[2] += v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, const void *a2)
{
unsigned __int64 v2; // rax
size_t v3; // rdx
size_t v4; // rbp

v2 = func2((__int64)a1);
if ( v2 < v3 )
return 0xFFFFFFFFLL;
v4 = v3;
memcpy((void *)(*a1 + a1[2]), a2, v3);
a1[2] += v4;
return 0LL;
}

original_code:
int fifo_write(struct FIFOBUF *buf, const char *data, uint64_t size)
{
 if (fifo_space_left(buf) < size)
  return -1;
 memcpy(buf->data+buf->offset,
   data,
   size);
 buf->offset += size;
 return 0;
}

canonical_original_code:
int fifo_write(struct FIFOBUF *buf, char *data, unsigned long size)
{
 if (fifo_space_left(buf) < size)
  return -1;
 memcpy(buf->data+buf->offset,
   data,
   size);
 buf->offset += size;
 return 0;
}

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall fifo_read(__int64 a1, void *a2, size_t a3)
{
unsigned __int64 v4; // rax
unsigned __int64 v5; // rdx
__int64 v6; // r8

v4 = fifo_len(a1);
if ( v4 <= v5 )
a3 = v4;
if ( !a3 )
return 0LL;
v6 = v4;
if ( a2 )
{
memcpy(a2, *(const void **)a1, a3);
v6 = fifo_len(a1);
}
memmove(*(void **)a1, (const void *)(*(_QWORD *)a1 + a3), v6);
*(_QWORD *)(a1 + 16) -= a3;
return (unsigned int)a3;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, void *a2, size_t a3)
{
unsigned __int64 v4; // rax
unsigned __int64 v5; // rdx
__int64 v6; // r8

v4 = func1(a1);
if ( v4 <= v5 )
a3 = v4;
if ( !a3 )
return 0LL;
v6 = v4;
if ( a2 )
{
memcpy(a2, *(const void **)a1, a3);
v6 = func1(a1);
}
memmove(*(void **)a1, (const void *)(*(_QWORD *)a1 + a3), v6);
*(_QWORD *)(a1 + 16) -= a3;
return (unsigned int)a3;
}

original_code:
int fifo_read(struct FIFOBUF *buf, char *out, uint64_t size)
{
 if (size > fifo_len(buf))
  size = fifo_len(buf);
 if (size == 0)
  return 0;
 if (out)
  memcpy(out, buf->data, size);
 memmove(buf->data,
    buf->data+size,
    fifo_len(buf));
 buf->offset -= size;
 return size;
}

canonical_original_code:
int fifo_read(struct FIFOBUF *buf, char *out, unsigned long size)
{
 if (size > fifo_len(buf))
  size = fifo_len(buf);
 if (size == 0)
  return 0;
 if (out)
  memcpy(out, buf->data, size);
 memmove(buf->data,
    buf->data+size,
    fifo_len(buf));
 buf->offset -= size;
 return size;
}

variable_types:
out
char *


decompiled_code:
void __fastcall fifo_clean(__int64 a1)
{
*(_QWORD *)(a1 + 16) = 0LL;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
*(_QWORD *)(a1 + 16) = 0LL;
}

original_code:
void fifo_clean(struct FIFOBUF *buf)
{
 buf->offset = 0;
}


decompiled_code:
int __fastcall add_new_clients(__int64 a1)
{
int v2; // edi
__int64 v3; // rax
int v4; // ebp
int v5; // eax
_DWORD *v6; // rdx
__m128i v7; // xmm0
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v2 = *(_DWORD *)(a1 + 4);
addr_len = 16;
LODWORD(v3) = accept(v2, &addr, &addr_len);
if ( (_DWORD)v3 != -1 )
{
v4 = v3;
v5 = 0;
v6 = (_DWORD *)(a1 + 16);
if ( addr_len == 16 )
{
do
{
if ( !*v6 )
{
v7 = _mm_loadu_si128((const __m128i *)&addr);
v3 = a1 + 168LL * v5;
*(_DWORD *)(v3 + 16) = 1;
*(_DWORD *)(v3 + 20) = v4;
*(__m128i *)(v3 + 24) = v7;
return v3;
}
++v5;
v6 += 42;
}
while ( v5 != 256 );
puts("I'm full, can't accept moar");
}
LODWORD(v3) = close(v4);
}
return v3;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
int v2; // edi
__int64 v3; // rax
int v4; // ebp
int v5; // eax
_DWORD *v6; // rdx
__m128i v7; // xmm0
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v2 = *(_DWORD *)(a1 + 4);
addr_len = 16;
LODWORD(v3) = accept(v2, &addr, &addr_len);
if ( (_DWORD)v3 != -1 )
{
v4 = v3;
v5 = 0;
v6 = (_DWORD *)(a1 + 16);
if ( addr_len == 16 )
{
do
{
if ( !*v6 )
{
v7 = _mm_loadu_si128((const __m128i *)&addr);
v3 = a1 + 168LL * v5;
*(_DWORD *)(v3 + 16) = 1;
*(_DWORD *)(v3 + 20) = v4;
*(__m128i *)(v3 + 24) = v7;
return v3;
}
++v5;
v6 += 42;
}
while ( v5 != 256 );
puts("I'm full, can't accept moar");
}
LODWORD(v3) = close(v4);
}
return v3;
}

original_code:
static void
add_new_clients(S5SRV *srv)
{
 int i;
 int new_sock;
 struct sockaddr_in addr;
 socklen_t sock_len = sizeof(addr);
 new_sock = accept(srv->socket, (struct sockaddr*)&addr, &sock_len);
 if (new_sock == -1)
  return;
 if (sock_len != sizeof(addr))
 {
  close(new_sock);
  return;
 }
 S5SRV_CL *new_cl = ((void *)0);
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state == S5SRV_CL_OFFLINE)
  {
   new_cl = &srv->clients[i];
   break;
  }
 }
 if (!new_cl)
 {
  printf("I'm full, can't accept moar\n");
  close(new_sock);
  return;
 }
 new_cl->state = S5SRV_CL_ACCEPTED;
 new_cl->socket = new_sock;
 new_cl->addr = addr;
 return;
}

canonical_original_code:
static void add_new_clients(struct S5SRV *srv)
{
 int i;
 int new_sock;
 struct sockaddr_in addr;
 unsigned int sock_len = sizeof(addr);
 new_sock = accept(srv->socket, (struct sockaddr *)&addr, &sock_len);
 if (new_sock == -1)
  return;
 if (sock_len != sizeof(addr))
 {
  close(new_sock);
  return;
 }
 struct S5SRV_CL *new_cl = ((void *)0);
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state == S5SRV_CL_OFFLINE)
  {
   new_cl = &srv->clients[i];
   break;
  }
 }
 if (!new_cl)
 {
  printf("I'm full, can't accept moar\n");
  close(new_sock);
  return;
 }
 new_cl->state = S5SRV_CL_ACCEPTED;
 new_cl->socket = new_sock;
 new_cl->addr = addr;
 return;
}

variable_types: struct S5SRV { int error; int socket; int num_clients; struct S5SRV_CL { int state; int socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; } req; void *conn_cb_user; } clients[-1]; int (*new_connect)(struct S5SRV_CL *cl, void **userdata); int (*conn_state)(void *userdata); void (*on_data)(void *userdata, char *data, int size); void (*on_disc)(void *userdata); int (*get_data)(void *userdata, char *data, int size); int ignore_sel_timeout; } *

variable_types: i int

variable_types: struct S5SRV_CL { int state; int socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; } req; void *conn_cb_user; } *

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 S5SRV { int error; int socket; int num_clients; struct S5SRV_CL clients[-1]; int (*new_connect)(struct S5SRV_CL *cl, void **userdata); int (*conn_state)(void *userdata); void (*on_data)(void *userdata, char *data, int size); void (*on_disc)(void *userdata); int (*get_data)(void *userdata, char *data, int size); int ignore_sel_timeout; }

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


original_code:
static void
cl_handle_packet(S5SRV *srv, S5SRV_CL *cl, char *data, int size)
{
 int i = 0;
 if (cl->state == S5SRV_CL_ACCEPTED)
 {
  if (data[i++] != 0x5)
  {
   cl_disc(cl);
   return;
  }
  int num_methods = data[i++];
  if (!num_methods || num_methods > size-1)
  {
   cl_disc(cl);
   return;
  }
  int g;
  for (g = 0; g < num_methods; g++)
  {
   if (data[i++] == S5_NO_AUTH_REQUIRED)
    break;
  }
  if (g == num_methods)
  {
   cl_disc(cl);
   return;
  }
  char d[] = {0x05, S5_NO_AUTH_REQUIRED};
  send(cl->socket, d, 2, 0);
  cl->state = S5SRV_CL_RECV_REQUEST;
 }
 else if (cl->state == S5SRV_CL_RECV_REQUEST)
 {
  if (data[i++] != 0x5)
  {
   cl_disc(cl);
   return;
  }
  if (size < 5)
  {
   cl_disc(cl);
   return;
  }
  int cmd = data[i++];
  i++;
  int addr_type = data[i++];
  if (cmd != S5_CONNECT || addr_type != S5_DOMAIN)
  {
   cl_disc(cl);
   return;
  }
  if (addr_type == S5_DOMAIN)
  {
   int len = data[i++];
   if (len > size-4 || len > sizeof(cl->req.data)-1)
   {
    cl_disc(cl);
    return;
   }
   int min_len = 4 +
       1 +
        len +
         2 ;
   if (size < min_len)
   {
    cl_disc(cl);
    return;
   }
   cl->req.addr_type = S5_DOMAIN;
   memcpy(cl->req.data, data+i, len);
   cl->req.data[len] = '\0';
   i += len;
   cl->req.port = ((unsigned char)data[i++])<<8;
   cl->req.port += (unsigned char)data[i++];
   if (srv->new_connect(cl, &cl->conn_cb_user) == 0)
   {
    cl->state = S5SRV_CL_CONN_PENDING;
   }
   else
   {
    cl_disc(cl);
   }
  }
 }
 else if (cl->state == S5SRV_CL_ONLINE)
  srv->on_data(cl->conn_cb_user, data, size);
}

canonical_original_code:
static void cl_handle_packet(struct S5SRV *srv, struct S5SRV_CL *cl, char *data, int size)
{
 int i = 0;
 if (cl->state == S5SRV_CL_ACCEPTED)
 {
  if (data[i++] != 0x5)
  {
   cl_disc(cl);
   return;
  }
  int num_methods = data[i++];
  if (!num_methods || num_methods > size-1)
  {
   cl_disc(cl);
   return;
  }
  int g;
  for (g = 0; g < num_methods; g++)
  {
   if (data[i++] == S5_NO_AUTH_REQUIRED)
    break;
  }
  if (g == num_methods)
  {
   cl_disc(cl);
   return;
  }
  char d[] = {0x05, S5_NO_AUTH_REQUIRED};
  send(cl->socket, d, 2, 0);
  cl->state = S5SRV_CL_RECV_REQUEST;
 }
 else if (cl->state == S5SRV_CL_RECV_REQUEST)
 {
  if (data[i++] != 0x5)
  {
   cl_disc(cl);
   return;
  }
  if (size < 5)
  {
   cl_disc(cl);
   return;
  }
  int cmd = data[i++];
  i++;
  int addr_type = data[i++];
  if (cmd != S5_CONNECT || addr_type != S5_DOMAIN)
  {
   cl_disc(cl);
   return;
  }
  if (addr_type == S5_DOMAIN)
  {
   int len = data[i++];
   if (len > size-4 || len > sizeof(cl->req.data)-1)
   {
    cl_disc(cl);
    return;
   }
   int min_len = 4 +
       1 +
        len +
         2;
   if (size < min_len)
   {
    cl_disc(cl);
    return;
   }
   cl->req.addr_type = S5_DOMAIN;
   memcpy(cl->req.data, data+i, len);
   cl->req.data[len] = '\0';
   i += len;
   cl->req.port = ((unsigned char)data[i++])<<8;
   cl->req.port += (unsigned char)data[i++];
   if (srv->new_connect(cl, &cl->conn_cb_user) == 0)
   {
    cl->state = S5SRV_CL_CONN_PENDING;
   }
   else
   {
    cl_disc(cl);
   }
  }
 }
 else if (cl->state == S5SRV_CL_ONLINE)
  srv->on_data(cl->conn_cb_user, data, size);
}

variable_types:
cl
struct S5SRV_CL *

variable_types:
data
char *

variable_types: size int

variable_types: g int

variable_types:
d
char []

variable_types: cmd int

variable_types: len int

user_defined_types: struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; }


original_code:
static void
handle_client(S5SRV *srv, fd_set *fds, int id)
{
 char buf[2048];
 S5SRV_CL *cl = &srv->clients[id];
 if (((((fds)->__fds_bits)[((cl->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((cl->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
 {
  int res;
  res = recv(cl->socket, buf, sizeof(buf), 0);
  if (res <= 0)
  {
   if (cl->state == S5SRV_CL_CONN_PENDING || cl->state == S5SRV_CL_ONLINE)
    srv->on_disc(cl->conn_cb_user);
   close(cl->socket);
   cl->state = S5SRV_CL_OFFLINE;
  }
  else
   cl_handle_packet(srv, cl, buf, res);
 }
 if (cl->state == S5SRV_CL_CONN_PENDING)
 {
  int state = srv->conn_state(cl->conn_cb_user);
  if (state == S5SRV_CB_CONN_ONLINE)
  {
   cl->state = S5SRV_CL_ONLINE;
   char accbuf[] = {0x5, S5_SUCEEDED, 0x0, S5_IPV4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
   send(cl->socket, accbuf, sizeof(accbuf), 0);
  }
  else if (state == S5SRV_CB_CONN_FAILED)
  {
   cl_disc(cl);
  }
 }
 else if (cl->state == S5SRV_CL_ONLINE)
 {
  char data[2048];
  int size;
  if (srv->conn_state(cl->conn_cb_user) == S5SRV_CB_CONN_FAILED)
   cl->state = S5SRV_CL_SHUTDOWN;
  else
  {
   size = srv->get_data(cl->conn_cb_user, data, sizeof(data));
   if (size == -1)
   {
    cl->state = S5SRV_CL_SHUTDOWN;
   }
   else if (size > 0)
    send(cl->socket, data, size, 0);
  }
 }
 else if (cl->state == S5SRV_CL_SHUTDOWN)
 {
  srv->on_disc(cl->conn_cb_user);
  close(cl->socket);
  cl->state = S5SRV_CL_OFFLINE;
 }
}

canonical_original_code:
static void handle_client(struct S5SRV *srv, struct fd_set *fds, int id)
{
 char buf[2048];
 struct S5SRV_CL *cl = &srv->clients[id];
 if (((((fds)->__fds_bits)[((cl->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((cl->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
 {
  int res;
  res = recv(cl->socket, buf, sizeof(buf), 0);
  if (res <= 0)
  {
   if (cl->state == S5SRV_CL_CONN_PENDING || cl->state == S5SRV_CL_ONLINE)
    srv->on_disc(cl->conn_cb_user);
   close(cl->socket);
   cl->state = S5SRV_CL_OFFLINE;
  }
  else
   cl_handle_packet(srv, cl, buf, res);
 }
 if (cl->state == S5SRV_CL_CONN_PENDING)
 {
  int state = srv->conn_state(cl->conn_cb_user);
  if (state == S5SRV_CB_CONN_ONLINE)
  {
   cl->state = S5SRV_CL_ONLINE;
   char accbuf[] = {0x5, S5_SUCEEDED, 0x0, S5_IPV4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
   send(cl->socket, accbuf, sizeof(accbuf), 0);
  }
  else if (state == S5SRV_CB_CONN_FAILED)
  {
   cl_disc(cl);
  }
 }
 else if (cl->state == S5SRV_CL_ONLINE)
 {
  char data[2048];
  int size;
  if (srv->conn_state(cl->conn_cb_user) == S5SRV_CB_CONN_FAILED)
   cl->state = S5SRV_CL_SHUTDOWN;
  else
  {
   size = srv->get_data(cl->conn_cb_user, data, sizeof(data));
   if (size == -1)
   {
    cl->state = S5SRV_CL_SHUTDOWN;
   }
   else if (size > 0)
    send(cl->socket, data, size, 0);
  }
 }
 else if (cl->state == S5SRV_CL_SHUTDOWN)
 {
  srv->on_disc(cl->conn_cb_user);
  close(cl->socket);
  cl->state = S5SRV_CL_OFFLINE;
 }
}

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

variable_types: id int

variable_types:
buf
char [2048]

variable_types: res int

variable_types: state int

variable_types:
accbuf
char []

variable_types:
data
char [2048]


decompiled_code:
int __fastcall s5srv_init(__int64 a1, __int64 a2, __int16 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8)
{
_DWORD *v9; // rax
const char *v10; // r10
in_addr_t v11; // eax
unsigned int v12; // eax
int result; // eax
int v14; // edi
__int64 v15; // [rsp+8h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

*(_QWORD *)(a1 + 43024) = a4;
*(_QWORD *)(a1 + 43032) = a5;
*(_QWORD *)(a1 + 43040) = a6;
*(_QWORD *)(a1 + 43048) = a7;
*(_QWORD *)(a1 + 43056) = a8;
v9 = (_DWORD *)(a1 + 16);
do
{
*v9 = 0;
v9 += 42;
}
while ( v9 != (_DWORD *)(a1 + 43024) );
*(_DWORD *)(a1 + 8) = 0;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a3);
v11 = inet_addr(v10);
*(_DWORD *)a1 = 0;
*(_DWORD *)&addr.sa_data[2] = v11;
v12 = create_tcp_socket();
*(_DWORD *)(a1 + 4) = v12;
socket_set_linger(v12);
result = *(_DWORD *)(a1 + 4);
if ( result == -1 )
{
*(_DWORD *)a1 = 2;
}
else
{
result = bind(result, &addr, 0x10u);
if ( result == -1 )
{
*(_DWORD *)a1 = 3;
}
else
{
result = listen(*(_DWORD *)(a1 + 4), 3);
if ( result == -1 )
{
*(_DWORD *)a1 = 4;
}
else
{
v14 = *(_DWORD *)(a1 + 4);
v15 = 1LL;
result = ioctl(v14, 0x5421uLL, &v15);
if ( result == -1 )
*(_DWORD *)a1 = 5;
else
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2, __int16 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8)
{
_DWORD *v9; // rax
const char *v10; // r10
in_addr_t v11; // eax
unsigned int v12; // eax
int result; // eax
int v14; // edi
__int64 v15; // [rsp+8h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

*(_QWORD *)(a1 + 43024) = a4;
*(_QWORD *)(a1 + 43032) = a5;
*(_QWORD *)(a1 + 43040) = a6;
*(_QWORD *)(a1 + 43048) = a7;
*(_QWORD *)(a1 + 43056) = a8;
v9 = (_DWORD *)(a1 + 16);
do
{
*v9 = 0;
v9 += 42;
}
while ( v9 != (_DWORD *)(a1 + 43024) );
*(_DWORD *)(a1 + 8) = 0;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a3);
v11 = inet_addr(v10);
*(_DWORD *)a1 = 0;
*(_DWORD *)&addr.sa_data[2] = v11;
v12 = create_tcp_socket();
*(_DWORD *)(a1 + 4) = v12;
socket_set_linger(v12);
result = *(_DWORD *)(a1 + 4);
if ( result == -1 )
{
*(_DWORD *)a1 = 2;
}
else
{
result = bind(result, &addr, 0x10u);
if ( result == -1 )
{
*(_DWORD *)a1 = 3;
}
else
{
result = listen(*(_DWORD *)(a1 + 4), 3);
if ( result == -1 )
{
*(_DWORD *)a1 = 4;
}
else
{
v14 = *(_DWORD *)(a1 + 4);
v15 = 1LL;
result = ioctl(v14, 0x5421uLL, &v15);
if ( result == -1 )
*(_DWORD *)a1 = 5;
else
result = 0;
}
}
}
return result;
}

original_code:
int
s5srv_init(S5SRV *srv, const char *bind_host, int listening_port, cb_new_connect new_connect, cb_conn_state conn_state, cb_on_data on_data, cb_on_disc on_disc, cb_get_data get_data)
{
 struct sockaddr_in addr;
 srv->new_connect = new_connect;
 srv->conn_state = conn_state;
 srv->on_data = on_data;
 srv->on_disc = on_disc;
 srv->get_data = get_data;
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
  srv->clients[i].state = S5SRV_CL_OFFLINE;
 srv->num_clients = 0;
 addr.sin_family = 2;
 addr.sin_port = htons(listening_port);
 addr.sin_addr.s_addr = inet_addr(bind_host);
 srv->error = 0;
 srv->socket = create_tcp_socket();
 socket_set_linger(srv->socket);
 if (srv->socket == -1)
 {
  srv->error = S5SRV_ERROR_CREATING_SOCKET_FAILED;
  return -1;
 }
 if (bind(srv->socket, (struct sockaddr*)&addr, sizeof(addr)) == -1)
 {
  srv->error = S5SRV_ERROR_BINDING_SOCKET;
  return -1;
 }
 if (listen(srv->socket, 3) == -1)
 {
  srv->error = S5SRV_ERROR_LISTENING_SOCKET;
  return -1;
 }
 unsigned long mode = 1;
 if (ioctl(srv->socket, 0x5421, &mode) == -1)
 {
  srv->error = S5SRV_ERROR_NON_BLOCK;
  return -1;
 }
 return 0;
}

canonical_original_code:
int s5srv_init(struct S5SRV *srv, char *bind_host, int listening_port, int (*new_connect)(struct S5SRV_CL *cl, void **userdata), int (*conn_state)(void *userdata), void (*on_data)(void *userdata, char *data, int size), void (*on_disc)(void *userdata), int (*get_data)(void *userdata, char *data, int size))
{
 struct sockaddr_in addr;
 srv->new_connect = new_connect;
 srv->conn_state = conn_state;
 srv->on_data = on_data;
 srv->on_disc = on_disc;
 srv->get_data = get_data;
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
  srv->clients[i].state = S5SRV_CL_OFFLINE;
 srv->num_clients = 0;
 addr.sin_family = 2;
 addr.sin_port = htons(listening_port);
 addr.sin_addr.s_addr = inet_addr(bind_host);
 srv->error = 0;
 srv->socket = create_tcp_socket();
 socket_set_linger(srv->socket);
 if (srv->socket == -1)
 {
  srv->error = S5SRV_ERROR_CREATING_SOCKET_FAILED;
  return -1;
 }
 if (bind(srv->socket, (struct sockaddr *)&addr, sizeof(addr)) == -1)
 {
  srv->error = S5SRV_ERROR_BINDING_SOCKET;
  return -1;
 }
 if (listen(srv->socket, 3) == -1)
 {
  srv->error = S5SRV_ERROR_LISTENING_SOCKET;
  return -1;
 }
 unsigned long mode = 1;
 if (ioctl(srv->socket, 0x5421, &mode) == -1)
 {
  srv->error = S5SRV_ERROR_NON_BLOCK;
  return -1;
 }
 return 0;
}

variable_types: struct S5SRV { int error; int socket; int num_clients; struct S5SRV_CL { int state; int socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; } req; void *conn_cb_user; } clients[-1]; int (*new_connect)(struct S5SRV_CL *cl, void **userdata); int (*conn_state)(void *userdata); void (*on_data)(void *userdata, char *data, int size); void (*on_disc)(void *userdata); int (*get_data)(void *userdata, char *data, int size); int ignore_sel_timeout; } *

variable_types: int (*)(struct S5SRV_CL *cl, void **userdata)

variable_types: void (*)(void *userdata, char *data, int size)

variable_types: int (*)(void *userdata, char *data, int size)

variable_types:
mode
unsigned long


decompiled_code:
int __fastcall s5srv_close(int *a1)
{
int *v1; // rbp
int *v2; // rbx

v1 = a1 + 10756;
v2 = a1 + 4;
do
{
while ( !*v2 )
{
v2 += 42;
if ( v2 == v1 )
return close(a1[1]);
}
close(*v2);
v2 += 42;
}
while ( v2 != v1 );
return close(a1[1]);
}

canonical_decompiled_code:
int __fastcall func7(int *a1)
{
int *v1; // rbp
int *v2; // rbx

v1 = a1 + 10756;
v2 = a1 + 4;
do
{
while ( !*v2 )
{
v2 += 42;
if ( v2 == v1 )
return close(a1[1]);
}
close(*v2);
v2 += 42;
}
while ( v2 != v1 );
return close(a1[1]);
}

original_code:
void
s5srv_close(S5SRV *srv)
{
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state != 0)
   close(srv->clients[i].state);
 }
 close(srv->socket);
}

canonical_original_code:
void s5srv_close(struct S5SRV *srv)
{
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state != 0)
   close(srv->clients[i].state);
 }
 close(srv->socket);
}

variable_types: i int


original_code:
int
s5srv_tick_ex(S5SRV *srv, fd_set *rfds, fd_set *wfds)
{
 if (((((rfds)->__fds_bits)[((srv->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((srv->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  add_new_clients(srv);
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state > S5SRV_CL_OFFLINE)
   handle_client(srv, rfds, i);
 }
 return 0;
}

canonical_original_code:
int s5srv_tick_ex(struct S5SRV *srv, struct fd_set *rfds, struct fd_set *wfds)
{
 if (((((rfds)->__fds_bits)[((srv->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((srv->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  add_new_clients(srv);
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state > S5SRV_CL_OFFLINE)
   handle_client(srv, rfds, i);
 }
 return 0;
}

variable_types:
wfds
struct fd_set *


decompiled_code:
int __fastcall s5srv_tick(int *a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set v3; // [rsp+10h] [rbp-88h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 1000LL;
result = do_select_constprop_0(a1, &v3, &v2);
if ( result != -1 )
result = s5srv_tick_ex((__int64)a1, (__int64)&v3);
return result;
}

canonical_decompiled_code:
int __fastcall func9(int *a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set v3; // [rsp+10h] [rbp-88h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 1000LL;
result = func5(a1, &v3, &v2);
if ( result != -1 )
result = func8((__int64)a1, (__int64)&v3);
return result;
}

original_code:
int
s5srv_tick(S5SRV *srv, int block)
{
 fd_set fds;
 struct timeval tv;
 tv.tv_sec = 0;
 tv.tv_usec = 1000;
 int res;
 if ((res = do_select(srv, &fds, &tv)) == -1)
  return -1;
 return s5srv_tick_ex(srv, &fds, ((void *)0));
}

canonical_original_code:
int s5srv_tick(struct S5SRV *srv, int block)
{
 struct fd_set fds;
 struct timeval tv;
 tv.tv_sec = 0;
 tv.tv_usec = 1000;
 int res;
 if ((res = do_select(srv, &fds, &tv)) == -1)
  return -1;
 return s5srv_tick_ex(srv, &fds, ((void *)0));
}

variable_types: block int

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

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


original_code:
int
s5srv_get_fds(S5SRV *srv, fd_set *rfds, fd_set *wfds)
{
 int highest_fd;
 highest_fd = srv->socket;
 ((void) (((rfds)->__fds_bits)[((srv->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((srv->socket) % (8 * (int) sizeof (__fd_mask)))))));
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state > S5SRV_CL_OFFLINE)
  {
   if (srv->clients[i].socket > highest_fd)
    highest_fd = srv->clients[i].socket;
   ((void) (((rfds)->__fds_bits)[((srv->clients[i].socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((srv->clients[i].socket) % (8 * (int) sizeof (__fd_mask)))))));
  }
 }
 return highest_fd;
}

canonical_original_code:
int s5srv_get_fds(struct S5SRV *srv, struct fd_set *rfds, struct fd_set *wfds)
{
 int highest_fd;
 highest_fd = srv->socket;
 ((void) (((rfds)->__fds_bits)[((srv->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((srv->socket) % (8 * (int) sizeof (__fd_mask)))))));
 int i;
 for (i = 0; i < S5SRV_MAX_CLIENTS; i++)
 {
  if (srv->clients[i].state > S5SRV_CL_OFFLINE)
  {
   if (srv->clients[i].socket > highest_fd)
    highest_fd = srv->clients[i].socket;
   ((void) (((rfds)->__fds_bits)[((srv->clients[i].socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((srv->clients[i].socket) % (8 * (int) sizeof (__fd_mask)))))));
  }
 }
 return highest_fd;
}

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


decompiled_code:
ssize_t __fastcall s5srv_send(_DWORD *a1, const void *a2, size_t a3)
{
ssize_t result; // rax

if ( a3 )
return send(a1[1], a2, a3, 0);
*a1 = 5;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func11(_DWORD *a1, const void *a2, size_t a3)
{
ssize_t result; // rax

if ( a3 )
return send(a1[1], a2, a3, 0);
*a1 = 5;
return result;
}

original_code:
void
s5srv_send(S5SRV_CL *cl, char *data, size_t size)
{
 if (size > 0)
  send(cl->socket, data, size, 0);
 else
  cl->state = S5SRV_CL_SHUTDOWN;
}

canonical_original_code:
void s5srv_send(struct S5SRV_CL *cl, char *data, unsigned long size)
{
 if (size > 0)
  send(cl->socket, data, size, 0);
 else
  cl->state = S5SRV_CL_SHUTDOWN;
}

variable_types: struct S5SRV_CL { int state; int socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; struct S5SRV_CONN_REQ { int addr_type; char data[128]; int port; } req; void *conn_cb_user; } *

variable_types:
data
char *

variable_types:
size
unsigned long


decompiled_code:
__int64 s5srv_max_block_time()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func12()
{
return 0xFFFFFFFFLL;
}

original_code:
int s5srv_max_block_time(S5SRV *srv)
{
 return -1;
}

canonical_original_code:
int s5srv_max_block_time(struct S5SRV *srv)
{
 return -1;
}


decompiled_code:
__int64 __fastcall rev_send_msg(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rbp
unsigned int v5; // er12
__int64 v6; // rdx
char v8; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v9[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = a2 + 32;
v9[0] = *(_DWORD *)(a3 + 4);
v8 = *(_DWORD *)a3;
if ( fifo_space_left(a2 + 32) < (unsigned __int64)(*(_DWORD *)(a3 + 4) + 3) )
return (unsigned int)-1;
v5 = 0;
fifo_write(v3, v9, 2LL);
fifo_write(v3, &v8, 1LL);
v6 = *(int *)(a3 + 4);
if ( (int)v6 > 0 )
fifo_write(v3, *(_QWORD *)(a3 + 8), v6);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rbp
unsigned int v5; // er12
__int64 v6; // rdx
char v8; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v9[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = a2 + 32;
v9[0] = *(_DWORD *)(a3 + 4);
v8 = *(_DWORD *)a3;
if ( fifo_space_left(a2 + 32) < (unsigned __int64)(*(_DWORD *)(a3 + 4) + 3) )
return (unsigned int)-1;
v5 = 0;
fifo_write(v3, v9, 2LL);
fifo_write(v3, &v8, 1LL);
v6 = *(int *)(a3 + 4);
if ( (int)v6 > 0 )
fifo_write(v3, *(_QWORD *)(a3 + 8), v6);
return v5;
}

original_code:
int
rev_send_msg(struct rev_server *revsrv, struct rev_client *cl, struct netmsg *msg)
{
 struct FIFOBUF *outbuf = (&cl->rev_out_buf);
 uint16_t size_16 = msg->size;
 uint8_t msg_id_8 = msg->id;
 if (fifo_space_left(outbuf) < 3 + msg->size)
  return -1;
 fifo_write(outbuf, (const char*)&size_16, 2);
 fifo_write(outbuf, (const char*)&msg_id_8, 1);
 if (msg->size > 0)
  fifo_write(outbuf, msg->data, msg->size);
 return 0;
}

canonical_original_code:
int rev_send_msg(struct rev_server *revsrv, struct rev_client *cl, struct netmsg *msg)
{
 struct FIFOBUF *outbuf = (&cl->rev_out_buf);
 unsigned short size_16 = msg->size;
 unsigned char msg_id_8 = msg->id;
 if (fifo_space_left(outbuf) < 3 + msg->size)
  return -1;
 fifo_write(outbuf, (char *)&size_16, 2);
 fifo_write(outbuf, (char *)&msg_id_8, 1);
 if (msg->size > 0)
  fifo_write(outbuf, msg->data, msg->size);
 return 0;
}

variable_types: struct rev_server { struct http_idler { int state; int sock; long date; char http_host[128]; char http_uri[256]; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; } http_idlers[-1]; int rev_listen_sock; struct rev_client { int sock; int date; struct FIFOBUF { char *data; unsigned long size; unsigned long offset; } rev_in_buf; struct FIFOBUF rev_out_buf; char in_buf[-1]; char out_buf[-1]; } clients[2]; struct rev_client *usable_cl; char http_url[512]; char bind_ip[128]; int bind_port; int http_timeout; struct fd_set { long __fds_bits[-1]; } *read_fds; struct fd_set *write_fds; long new_idler_date; int last_idler_lifetime; struct network_handle { int state; int id; struct rev_client *cl; void *user_ptr; struct netaddr { int type; char addr_data[256]; int port; } dst_addr; struct FIFOBUF tcp_in_buf; struct FIFOBUF tcp_out_buf; char in_buf[-1]; char out_buf[-1]; int terminate; } netw_hndls[-1]; } *

variable_types:
cl
struct rev_client *

variable_types: struct netmsg { int id; int size; char *data; } *

variable_types: struct FIFOBUF { char *data; unsigned long size; unsigned long offset; } *

user_defined_types: struct rev_server { struct http_idler http_idlers[-1]; int rev_listen_sock; struct rev_client clients[2]; struct rev_client *usable_cl; char http_url[512]; char bind_ip[128]; int bind_port; int http_timeout; struct fd_set *read_fds; struct fd_set *write_fds; long new_idler_date; int last_idler_lifetime; struct network_handle netw_hndls[-1]; }

user_defined_types: struct http_idler { int state; int sock; long date; char http_host[128]; char http_uri[256]; struct sockaddr_in addr; }

user_defined_types: struct rev_client { int sock; int date; struct FIFOBUF rev_in_buf; struct FIFOBUF rev_out_buf; char in_buf[-1]; char out_buf[-1]; }

user_defined_types: struct network_handle { int state; int id; struct rev_client *cl; void *user_ptr; struct netaddr dst_addr; struct FIFOBUF tcp_in_buf; struct FIFOBUF tcp_out_buf; char in_buf[-1]; char out_buf[-1]; int terminate; }


decompiled_code:
// write access to const memory has been detected, the output may be wrong!
__int64 __fastcall handle_msg(__int64 a1, __int64 a2, int *a3)
{
unsigned int v3; // er12
unsigned __int16 *v4; // r12
int v5; // ebx
__int64 v6; // rax
const char *v8; // r13
unsigned int v9; // er15
int v10; // er14
unsigned int *v11; // rax
unsigned int *v12; // rbx
__int64 v13; // rax

v3 = *a3;
if ( !*a3 )
{
printf("dbg msg '%s'\n", *((const char **)a3 + 1));
return v3;
}
if ( v3 == 1 )
{
v3 = 0;
puts("got ping, send pong");
v13 = empty_msg_constprop_0(a1);
rev_send_msg(a1, a2, v13);
return v3;
}
if ( v3 == 4 )
{
v8 = (const char *)*((_QWORD *)a3 + 1);
v9 = *(unsigned __int16 *)v8;
v10 = v8[2];
v11 = (unsigned int *)revsrv_netw_hndl(a1, *(unsigned __int16 *)v8);
v3 = *v11;
v12 = v11;
if ( *v11 == 2 )
{
if ( v10 == 1 )
{
printf("netw handle %d failed to connect '%s'\n", v9, v8 + 3);
v3 = 0;
*v12 = 5;
return v3;
}
if ( !v10 )
{
v3 = 0;
printf("netw handle %d established connection\n", v9);
*v12 = 3;
return v3;
}
}
else
{
if ( v3 == 3 && v10 == 1 )
{
*v11 = 4;
return 0;
}
if ( !v3 )
{
puts("WARNING: conn state on offline handle");
return v3;
}
}
LABEL_6:
*(_BYTE *)empty_msg_constprop_0 = 0;
BUG();
}
if ( v3 != 5 )
return (unsigned int)-1;
v4 = (unsigned __int16 *)*((_QWORD *)a3 + 1);
v5 = v4[1];
if ( a3[1] != v5 + 4 )
goto LABEL_6;
v6 = revsrv_netw_hndl(a1, *v4);
if ( v6 )
{
if ( (unsigned int)fifo_write(v6 + 288, v4 + 2, (unsigned __int16)v5) )
goto LABEL_6;
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int *a3)
{
unsigned int v3; // er12
unsigned __int16 *v4; // r12
int v5; // ebx
__int64 v6; // rax
const char *v8; // r13
unsigned int v9; // er15
int v10; // er14
unsigned int *v11; // rax
unsigned int *v12; // rbx
__int64 v13; // rax

v3 = *a3;
if ( !*a3 )
{
printf("dbg msg '%s'\n", *((const char **)a3 + 1));
return v3;
}
if ( v3 == 1 )
{
v3 = 0;
puts("got ping, send pong");
v13 = empty_msg_constprop_0(a1);
func0(a1, a2, v13);
return v3;
}
if ( v3 == 4 )
{
v8 = (const char *)*((_QWORD *)a3 + 1);
v9 = *(unsigned __int16 *)v8;
v10 = v8[2];
v11 = (unsigned int *)revsrv_netw_hndl(a1, *(unsigned __int16 *)v8);
v3 = *v11;
v12 = v11;
if ( *v11 == 2 )
{
if ( v10 == 1 )
{
printf("netw handle %d failed to connect '%s'\n", v9, v8 + 3);
v3 = 0;
*v12 = 5;
return v3;
}
if ( !v10 )
{
v3 = 0;
printf("netw handle %d established connection\n", v9);
*v12 = 3;
return v3;
}
}
else
{
if ( v3 == 3 && v10 == 1 )
{
*v11 = 4;
return 0;
}
if ( !v3 )
{
puts("WARNING: conn state on offline handle");
return v3;
}
}
LABEL_6:
*(_BYTE *)empty_msg_constprop_0 = 0;
BUG();
}
if ( v3 != 5 )
return (unsigned int)-1;
v4 = (unsigned __int16 *)*((_QWORD *)a3 + 1);
v5 = v4[1];
if ( a3[1] != v5 + 4 )
goto LABEL_6;
v6 = revsrv_netw_hndl(a1, *v4);
if ( v6 )
{
if ( (unsigned int)fifo_write(v6 + 288, v4 + 2, (unsigned __int16)v5) )
goto LABEL_6;
}
return 0;
}

original_code:
static int
handle_msg(struct rev_server *revsrv, struct rev_client *cl, struct netmsg *msg)
{
 if (msg->id == MSG_DBGMSG)
  printf("dbg msg '%s'\n", msg->data);
 else if(msg->id == MSG_PING)
 {
  printf("got ping, send pong\n");
  rev_send_msg(revsrv, cl, empty_msg(MSG_PONG));
 }
 else if(msg->id == MSG_CONN_STATE)
 {
  const char *p = msg->data;
  int id = *((uint16_t*)p);
  int state = *(p+2);
  const char *err_str = p+3;
  struct network_handle *hndl = revsrv_netw_hndl(revsrv, id);
  if (hndl->state == NETW_HNDL_TCP_CONNECT)
  {
   if (state == CONN_STATE_ERROR)
   {
    printf("netw handle %d failed to connect '%s'\n", id, err_str);
    hndl->state = NETW_HNDL_TCP_FAIL;
   }
   else if(state == CONN_STATE_ONLINE)
   {
    printf("netw handle %d established connection\n", id);
    hndl->state = NETW_HNDL_TCP_ONLINE;
   }
   else { if (!(0)) { *((char*)((void *)0)) = 0; } }
  }
  else if(hndl->state == NETW_HNDL_TCP_ONLINE &&
     state == CONN_STATE_ERROR)
  {
   hndl->state = NETW_HNDL_TCP_DISC;
  }
  else if(hndl->state == NETW_HNDL_OFFLINE)
   printf("WARNING: conn state on offline handle\n");
  else { if (!(0)) { *((char*)((void *)0)) = 0; } }
 }
 else if (msg->id == MSG_SEND)
 {
  const char *p = msg->data;
  int session_id = *((uint16_t*)p);
  int size = *((uint16_t*)(p+2));
  const char *data = p+4;
  if (!(msg->size == size+4)) { *((char*)((void *)0)) = 0; }
  struct network_handle *hndl =
     revsrv_netw_hndl(revsrv, session_id);
  if (hndl)
  {
   int res = fifo_write(&hndl->tcp_in_buf, data, size);
   if (!(res == 0)) { *((char*)((void *)0)) = 0; }
  }
 }
 else
  return -1;
 return 0;
}

canonical_original_code:
static int handle_msg(struct rev_server *revsrv, struct rev_client *cl, struct netmsg *msg)
{
 if (msg->id == MSG_DBGMSG)
  printf("dbg msg '%s'\n", msg->data);
 else if(msg->id == MSG_PING)
 {
  printf("got ping, send pong\n");
  rev_send_msg(revsrv, cl, empty_msg(MSG_PONG));
 }
 else if(msg->id == MSG_CONN_STATE)
 {
  char *p = msg->data;
  int id = *((uint16_t*)p);
  int state = *(p+2);
  char *err_str = p+3;
  struct network_handle *hndl = revsrv_netw_hndl(revsrv, id);
  if (hndl->state == NETW_HNDL_TCP_CONNECT)
  {
   if (state == CONN_STATE_ERROR)
   {
    printf("netw handle %d failed to connect '%s'\n", id, err_str);
    hndl->state = NETW_HNDL_TCP_FAIL;
   }
   else if(state == CONN_STATE_ONLINE)
   {
    printf("netw handle %d established connection\n", id);
    hndl->state = NETW_HNDL_TCP_ONLINE;
   }
   else { if (!(0)) { *((char *)((void *)0)) = 0; } }
  }
  else if(hndl->state == NETW_HNDL_TCP_ONLINE &&
     state == CONN_STATE_ERROR)
  {
   hndl->state = NETW_HNDL_TCP_DISC;
  }
  else if(hndl->state == NETW_HNDL_OFFLINE)
   printf("WARNING: conn state on offline handle\n");
  else { if (!(0)) { *((char *)((void *)0)) = 0; } }
 }
 else if (msg->id == MSG_SEND)
 {
  char *p = msg->data;
  int session_id = *((uint16_t*)p);
  int size = *((uint16_t*)(p+2));
  char *data = p+4;
  if (!(msg->size == size+4)) { *((char *)((void *)0)) = 0; }
  struct network_handle *hndl = revsrv_netw_hndl(revsrv, session_id);
  if (hndl)
  {
   int res = fifo_write(&hndl->tcp_in_buf, data, size);
   if (!(res == 0)) { *((char *)((void *)0)) = 0; }
  }
 }
 else
  return -1;
 return 0;
}

variable_types:
p
char *

variable_types: id int

variable_types: state int

variable_types: struct network_handle { int state; int id; struct rev_client { int sock; int date; struct FIFOBUF { char *data; unsigned long size; unsigned long offset; } rev_in_buf; struct FIFOBUF rev_out_buf; char in_buf[-1]; char out_buf[-1]; } *cl; void *user_ptr; struct netaddr { int type; char addr_data[256]; int port; } dst_addr; struct FIFOBUF tcp_in_buf; struct FIFOBUF tcp_out_buf; char in_buf[-1]; char out_buf[-1]; int terminate; } *

variable_types: size int

variable_types: res int

user_defined_types: struct netmsg { int id; int size; char *data; }


decompiled_code:
// write access to const memory has been detected, the output may be wrong!
_BOOL8 __fastcall handle_buffer(__int64 a1, __int64 a2)
{
__int64 v2; // r12
unsigned __int16 *v3; // rax
unsigned int v4; // er15
int v5; // er14
int v7[2]; // [rsp+0h] [rbp-1048h] BYREF
char *v8; // [rsp+8h] [rbp-1040h]
char v9[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = a2 + 8;
if ( (unsigned __int64)fifo_len(a2 + 8) <= 2 )
return 0LL;
v3 = *(unsigned __int16 **)(a2 + 8);
v4 = *v3;
v5 = *((unsigned __int8 *)v3 + 2);
if ( v4 > 0x1000 )
goto LABEL_3;
if ( fifo_len(a2 + 8) < (unsigned __int64)(int)(v4 + 3) )
return 0LL;
fifo_read(v2, 0LL, 3LL);
v7[0] = v5;
v7[1] = v4;
v8 = v9;
fifo_read(v2, v9, (unsigned __int16)v4);
if ( (unsigned int)handle_msg(a1, a2, v7) )
{
LABEL_3:
*(_BYTE *)empty_msg_constprop_0 = 0;
BUG();
}
return fifo_len(v2) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // r12
unsigned __int16 *v3; // rax
unsigned int v4; // er15
int v5; // er14
int v7[2]; // [rsp+0h] [rbp-1048h] BYREF
char *v8; // [rsp+8h] [rbp-1040h]
char v9[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = a2 + 8;
if ( (unsigned __int64)fifo_len(a2 + 8) <= 2 )
return 0LL;
v3 = *(unsigned __int16 **)(a2 + 8);
v4 = *v3;
v5 = *((unsigned __int8 *)v3 + 2);
if ( v4 > 0x1000 )
goto LABEL_3;
if ( fifo_len(a2 + 8) < (unsigned __int64)(int)(v4 + 3) )
return 0LL;
fifo_read(v2, 0LL, 3LL);
v7[0] = v5;
v7[1] = v4;
v8 = v9;
fifo_read(v2, v9, (unsigned __int16)v4);
if ( (unsigned int)func1(a1, a2, v7) )
{
LABEL_3:
*(_BYTE *)empty_msg_constprop_0 = 0;
BUG();
}
return fifo_len(v2) != 0;
}

original_code:
static int
handle_buffer(struct rev_server *revsrv, struct rev_client *cl)
{
 struct FIFOBUF *inbuf = (&cl->rev_in_buf);
 if (fifo_len(inbuf) < 3)
  return 0;
 int msg_size = *((uint16_t*)inbuf->data);
 int msg_id = *((uint8_t*)(inbuf->data+2));
 if (!(msg_size <= MAX_MSG_SIZE)) { *((char*)((void *)0)) = 0; }
 if (fifo_len(inbuf) < 3 + msg_size)
  return 0;
 fifo_read(inbuf, ((void *)0), 3);
 char buf[MAX_MSG_SIZE];
 struct netmsg msg;
 msg.id = msg_id;
 msg.size = msg_size;
 msg.data = buf;
 fifo_read(inbuf, buf, msg_size);
 if (!(handle_msg(revsrv, cl, &msg) == 0)) { *((char*)((void *)0)) = 0; };
 if (fifo_len(inbuf) > 0)
  return 1;
 return 0;
}

canonical_original_code:
static int handle_buffer(struct rev_server *revsrv, struct rev_client *cl)
{
 struct FIFOBUF *inbuf = (&cl->rev_in_buf);
 if (fifo_len(inbuf) < 3)
  return 0;
 int msg_size = *((uint16_t*)inbuf->data);
 int msg_id = *((uint8_t*)(inbuf->data+2));
 if (!(msg_size <= MAX_MSG_SIZE)) { *((char *)((void *)0)) = 0; }
 if (fifo_len(inbuf) < 3 + msg_size)
  return 0;
 fifo_read(inbuf, ((void *)0), 3);
 char buf[-1];
 struct netmsg msg;
 msg.id = msg_id;
 msg.size = msg_size;
 msg.data = buf;
 fifo_read(inbuf, buf, msg_size);
 if (!(handle_msg(revsrv, cl, &msg) == 0)) { *((char *)((void *)0)) = 0; };
 if (fifo_len(inbuf) > 0)
  return 1;
 return 0;
}

variable_types:
buf
char [-1]

variable_types: struct netmsg { int id; int size; char *data; }

user_defined_types: struct netaddr { int type; char addr_data[256]; int port; }


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

do
result = handle_buffer(a1, a2);
while ( result );
return result;
}

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

do
result = func2(a1, a2);
while ( result );
return result;
}

original_code:
int
rev_pump_network(struct rev_server *revsrv, struct rev_client *cl)
{
 while (handle_buffer(revsrv, cl));
 return 0;
}


original_code:
void rev_netmsg_send(struct rev_server *revsrv, struct rev_client *cl,
     int netw_hndl, const char *data, int size)
{
 struct netmsg msg;
 char buf[MAX_MSG_SIZE];
 msg.id = MSG_SEND;
 msg.size = 4 + size;
 msg.data = buf;
 *((uint16_t*)buf) = netw_hndl;
 *((uint16_t*)(buf+2)) = size;
 memcpy(buf+4, data, size);
 rev_send_msg(revsrv, cl, &msg);
}

canonical_original_code:
void rev_netmsg_send(struct rev_server *revsrv, struct rev_client *cl, int netw_hndl, char *data, int size)
{
 struct netmsg msg;
 char buf[-1];
 msg.id = MSG_SEND;
 msg.size = 4 + size;
 msg.data = buf;
 *((unsigned short *)buf) = netw_hndl;
 *((unsigned short *)(buf+2)) = size;
 memcpy(buf+4, data, size);
 rev_send_msg(revsrv, cl, &msg);
}


original_code:
static void
do_request(SOCKS_HANDLER *socks)
{
 char buf[16];
 int i = 0;
 buf[i++] = 0x05;
 if (socks->username[0] || socks->password[0])
 {
 }
 else
 {
  buf[i++] = 1;
  buf[i++] = S5_NO_AUTH_REQUIRED;
 }
 socks->send_cb(socks->userptr, buf, i);
 socks->state = S5_RECV_METHOD;
}

canonical_original_code:
static void do_request(struct SOCKS_HANDLER *socks)
{
 char buf[16];
 int i = 0;
 buf[i++] = 0x05;
 if (socks->username[0] || socks->password[0])
 {
 }
 else
 {
  buf[i++] = 1;
  buf[i++] = S5_NO_AUTH_REQUIRED;
 }
 socks->send_cb(socks->userptr, buf, i);
 socks->state = S5_RECV_METHOD;
}

variable_types: struct SOCKS_HANDLER { int state; int version; int error; int cmd; char username[64]; char password[64]; void *userptr; int addr_type; char addr[64]; int port; void (*send_cb)(void *userptr, void *data, int size); int (*recv_cb)(void *userptr, void *out, int maxsize); } *

variable_types:
buf
char [16]

variable_types: i int


original_code:
void
s5_init_v4conn(SOCKS_HANDLER *socks, const char *addr, int port, socks_send_cb send_cb, socks_recv_cb recv_cb, void *userptr)
{
 socks->version = SOCKS_VER_5;
 socks->addr_type = S5_IPV4;
 memcpy(socks->addr, addr, 4);
 socks->port = port;
 socks->cmd = S5_CONNECT;
 socks->send_cb = send_cb;
 socks->recv_cb = recv_cb;
 socks->userptr = userptr;
 socks->username[0] = 0;
 socks->password[0] = 0;
 do_request(socks);
}

canonical_original_code:
void s5_init_v4conn(struct SOCKS_HANDLER *socks, char *addr, int port, void (*send_cb)(void *userptr, void *data, int size), int (*recv_cb)(void *userptr, void *out, int maxsize), void *userptr)
{
 socks->version = SOCKS_VER_5;
 socks->addr_type = S5_IPV4;
 memcpy(socks->addr, addr, 4);
 socks->port = port;
 socks->cmd = S5_CONNECT;
 socks->send_cb = send_cb;
 socks->recv_cb = recv_cb;
 socks->userptr = userptr;
 socks->username[0] = 0;
 socks->password[0] = 0;
 do_request(socks);
}

variable_types:
addr
char *

variable_types: port int

variable_types: void (*)(void *userptr, void *data, int size)

variable_types: int (*)(void *userptr, void *out, int maxsize)

variable_types:
userptr
void *

user_defined_types: struct SOCKS_HANDLER { int state; int version; int error; int cmd; char username[64]; char password[64]; void *userptr; int addr_type; char addr[64]; int port; void (*send_cb)(void *userptr, void *data, int size); int (*recv_cb)(void *userptr, void *out, int maxsize); }


original_code:
void
s5_init_domconn(SOCKS_HANDLER *socks, const char *domain, int port, socks_send_cb send_cb, socks_recv_cb recv_cb, void *userptr)
{
 socks->version = SOCKS_VER_5;
 socks->addr_type = S5_DOMAIN;
 strncpy(socks->addr, domain, sizeof(socks->addr));
 socks->port = port;
 socks->cmd = S5_CONNECT;
 socks->send_cb = send_cb;
 socks->recv_cb = recv_cb;
 socks->userptr = userptr;
 socks->username[0] = 0;
 socks->password[0] = 0;
 do_request(socks);
}

canonical_original_code:
void s5_init_domconn(struct SOCKS_HANDLER *socks, char *domain, int port, void (*send_cb)(void *userptr, void *data, int size), int (*recv_cb)(void *userptr, void *out, int maxsize), void *userptr)
{
 socks->version = SOCKS_VER_5;
 socks->addr_type = S5_DOMAIN;
 strncpy(socks->addr, domain, sizeof(socks->addr));
 socks->port = port;
 socks->cmd = S5_CONNECT;
 socks->send_cb = send_cb;
 socks->recv_cb = recv_cb;
 socks->userptr = userptr;
 socks->username[0] = 0;
 socks->password[0] = 0;
 do_request(socks);
}

variable_types:
domain
char *


original_code:
void
socks_send_conn_details(SOCKS_HANDLER *socks)
{
 char buf[256];
 int i = 0;
 if (socks->version == SOCKS_VER_5)
 {
  buf[i++] = 0x05;
  buf[i++] = socks->cmd;
  buf[i++] = 0;
  buf[i++] = socks->addr_type;
  if (socks->addr_type == S5_IPV4)
  {
   buf[i++] = socks->addr[0];
   buf[i++] = socks->addr[1];
   buf[i++] = socks->addr[2];
   buf[i++] = socks->addr[3];
  }
  else if (socks->addr_type == S5_DOMAIN)
  {
   int len = strlen(socks->addr);
   buf[i++] = len;
   memcpy(buf+i, socks->addr, len);
   i += len;
  }
  buf[i++] = (socks->port&0xFFFF)>>8;
  buf[i++] = socks->port&0xFF;
 }
 socks->send_cb(socks->userptr, buf, i);
}

canonical_original_code:
void socks_send_conn_details(struct SOCKS_HANDLER *socks)
{
 char buf[256];
 int i = 0;
 if (socks->version == SOCKS_VER_5)
 {
  buf[i++] = 0x05;
  buf[i++] = socks->cmd;
  buf[i++] = 0;
  buf[i++] = socks->addr_type;
  if (socks->addr_type == S5_IPV4)
  {
   buf[i++] = socks->addr[0];
   buf[i++] = socks->addr[1];
   buf[i++] = socks->addr[2];
   buf[i++] = socks->addr[3];
  }
  else if (socks->addr_type == S5_DOMAIN)
  {
   int len = strlen(socks->addr);
   buf[i++] = len;
   memcpy(buf+i, socks->addr, len);
   i += len;
  }
  buf[i++] = (socks->port&0xFFFF)>>8;
  buf[i++] = socks->port&0xFF;
 }
 socks->send_cb(socks->userptr, buf, i);
}

variable_types:
buf
char [256]

variable_types: len int


original_code:
int
socks_tick(SOCKS_HANDLER *socks)
{
 char buf[256];
 if (socks->version == SOCKS_VER_5)
 {
  if (socks->state == S5_RECV_METHOD)
  {
   int bytes = socks->recv_cb(socks->userptr, buf, sizeof(buf));
   if (bytes <= 0)
   {
    socks->error = S5_RECV_FAILED;
    return SOCKS_FAILED;
   }
   if (bytes != 2 || buf[0] != 0x05)
   {
    socks->error = S5_PROTOCOL_ERROR;
    return SOCKS_FAILED;
   }
   if (buf[1] == 0xFF)
   {
    socks->error = S5_METHODS_REFUSED;
    return SOCKS_FAILED;
   }
   else
   {
    socks_send_conn_details(socks);
    socks->state = S5_RECV_CONNECTION;
    return SOCKS_PENDING;
   }
  }
  else if (socks->state == S5_RECV_CONNECTION)
  {
   int bytes = socks->recv_cb(socks->userptr, buf, sizeof(buf));
   if (bytes <= 0)
   {
    socks->error = S5_RECV_FAILED;
    return SOCKS_FAILED;
   }
   if (buf[0] != 0x05 || bytes < 4)
   {
    socks->error = S5_PROTOCOL_ERROR;
    return SOCKS_FAILED;
   }
   if (buf[1] == S5_SUCEEDED)
   {
    socks->state = S5_ONLINE;
    return SOCKS_ONLINE;
   }
   else
   {
    socks->error = buf[1];
    return SOCKS_FAILED;
   }
  }
 }
 return SOCKS_FAILED;
}

canonical_original_code:
int socks_tick(struct SOCKS_HANDLER *socks)
{
 char buf[256];
 if (socks->version == SOCKS_VER_5)
 {
  if (socks->state == S5_RECV_METHOD)
  {
   int bytes = socks->recv_cb(socks->userptr, buf, sizeof(buf));
   if (bytes <= 0)
   {
    socks->error = S5_RECV_FAILED;
    return SOCKS_FAILED;
   }
   if (bytes != 2 || buf[0] != 0x05)
   {
    socks->error = S5_PROTOCOL_ERROR;
    return SOCKS_FAILED;
   }
   if (buf[1] == 0xFF)
   {
    socks->error = S5_METHODS_REFUSED;
    return SOCKS_FAILED;
   }
   else
   {
    socks_send_conn_details(socks);
    socks->state = S5_RECV_CONNECTION;
    return SOCKS_PENDING;
   }
  }
  else if (socks->state == S5_RECV_CONNECTION)
  {
   int bytes = socks->recv_cb(socks->userptr, buf, sizeof(buf));
   if (bytes <= 0)
   {
    socks->error = S5_RECV_FAILED;
    return SOCKS_FAILED;
   }
   if (buf[0] != 0x05 || bytes < 4)
   {
    socks->error = S5_PROTOCOL_ERROR;
    return SOCKS_FAILED;
   }
   if (buf[1] == S5_SUCEEDED)
   {
    socks->state = S5_ONLINE;
    return SOCKS_ONLINE;
   }
   else
   {
    socks->error = buf[1];
    return SOCKS_FAILED;
   }
  }
 }
 return SOCKS_FAILED;
}

variable_types: bytes int


storeAddr

repo: ebb/chisa

decompiled_code:
__int64 __fastcall storeAddr(unsigned __int64 a1)
{
return qword_10650 + (a1 >> 16);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
return qword_10650 + (a1 >> 16);
}

original_code:
static void *storeAddr(long x)
{
    return store.data + ((unsigned long)x >> 16);
}

variable_types: x long

return_type: void *


makeNumber

decompiled_code:
__int64 __fastcall makeNumber(__int64 a1)
{
return a1 << 16;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return a1 << 16;
}

original_code:
static long makeNumber(long n)
{
    return (long)((unsigned long)n << 16);
}

variable_types: n long

return_type: long


fixnumValue

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

result = (unsigned __int64)a1 >> 16;
if ( a1 < 0 )
result |= 0xFFFF000000000000LL;
return result;
}

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

result = (unsigned __int64)a1 >> 16;
if ( a1 < 0 )
result |= 0xFFFF000000000000LL;
return result;
}

original_code:
static long fixnumValue(long n)
{
    unsigned long bits;
    bits = (unsigned long)n >> 16;
    if (n < 0)
        bits |= (unsigned long)0xffff << 48;
    return (long)bits;
}

variable_types:
bits
unsigned long


makeString

decompiled_code:
__int64 __fastcall makeString(void *src)
{
__int64 v1; // rbp
__int64 v2; // rbx
__int64 v3; // rax
__int64 v4; // rcx
__int64 *v5; // rdx

v1 = strlen((const char *)src);
v2 = storeAlloc_constprop_0(v1 + 9);
v3 = makeNumber(v1);
*v5 = v3;
memmove((void *)(v4 + v2 + 8), src, v1 + 1);
return (v2 << 16) | 1;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *src)
{
__int64 v1; // rbp
__int64 v2; // rbx
__int64 v3; // rax
__int64 v4; // rcx
__int64 *v5; // rdx

v1 = strlen((const char *)src);
v2 = func4(v1 + 9);
v3 = func1(v1);
*v5 = v3;
memmove((void *)(v4 + v2 + 8), src, v1 + 1);
return (v2 << 16) | 1;
}

original_code:
static long makeString(const char *s)
{
    unsigned long align;
    unsigned long size;
    unsigned long len;
    unsigned long i;
    len = strlen(s);
    align = sizeof(long);
    size = sizeof(long) + len + 1;
    i = storeAlloc(align, size);
    *(long *)(store.data + i) = makeNumber((long)len);
    memmove(store.data + i + sizeof(long), s, len + 1);
    return (long)(i << 16 | CLASS_String);
}

canonical_original_code:
static long makeString(char *s)
{
    unsigned long align;
    unsigned long size;
    unsigned long len;
    unsigned long i;
    len = strlen(s);
    align = sizeof(long);
    size = sizeof(long) + len + 1;
    i = storeAlloc(align, size);
    *(long *)(store.data + i) = makeNumber((long)len);
    memmove(store.data + i + sizeof(long), s, len + 1);
    return (long)(i << 16 | CLASS_String);
}

variable_types:
s
char *

variable_types:
align
unsigned long

variable_types:
size
unsigned long

variable_types:
len
unsigned long

variable_types:
i
unsigned long


decompiled_code:
// attributes: thunk
__int64 __fastcall runtime_makeString(void *a1)
{
return makeString(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(void *a1)
{
return func5(a1);
}

original_code:
long runtime_makeString(const char *s)
{
    return makeString(s);
}

canonical_original_code:
long runtime_makeString(char *s)
{
    return makeString(s);
}


decompiled_code:
__int64 __fastcall runtime_class(__int64 a1)
{
classArgSave = a1;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
classArgSave = a1;
return a1;
}

original_code:
unsigned short runtime_class(long x)
{
    classArgSave = x;
    return (unsigned short)((unsigned long)x & 0xffff);
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall runtime_stringValue(unsigned __int64 a1)
{
if ( (unsigned __int16)runtime_class(a1) != 1 )
die("Type error.");
return storeAddr(a1) + 8;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 a1)
{
if ( (unsigned __int16)func7(a1) != 1 )
die("Type error.");
return func0(a1) + 8;
}

original_code:
const char *runtime_stringValue(long s)
{
    if (runtime_class(s) != CLASS_String)
        die("Type error.");
    return storeAddr(s) + sizeof(long);
}

variable_types: s long

return_type: char *


decompiled_code:
// attributes: thunk
unsigned __int64 __fastcall runtime_fixnumValue(__int64 a1)
{
return fixnumValue(a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(__int64 a1)
{
return func2(a1);
}

original_code:
long runtime_fixnumValue(long n)
{
    return fixnumValue(n);
}


decompiled_code:
__int64 __fastcall prim_fetch(unsigned __int64 a1, __int64 a2)
{
signed __int64 v2; // rbx
__int64 v3; // rcx

runtime_class(a1);
v2 = fixnumValue(a2);
if ( (unsigned __int8)runtime_classArities[v3] <= v2 )
die("Fetching slot that does not exist.");
return *(_QWORD *)(storeAddr(a1) + 8 * v2);
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int64 a1, __int64 a2)
{
signed __int64 v2; // rbx
__int64 v3; // rcx

func7(a1);
v2 = func2(a2);
if ( (unsigned __int8)runtime_classArities[v3] <= v2 )
die("Fetching slot that does not exist.");
return *(_QWORD *)(func0(a1) + 8 * v2);
}

original_code:
long prim_fetch(long m, long k)
{
    long *tuple;
    unsigned char arity;
    long i;
    arity = runtime_classArities[runtime_class(m)];
    i = fixnumValue(k);
    if (i >= arity)
        die("Fetching slot that does not exist.");
    tuple = storeAddr(m);
    return tuple[i];
}

variable_types: m long

variable_types: k long

variable_types:
tuple
long *

variable_types:
arity
unsigned char

variable_types: i long


decompiled_code:
// attributes: thunk
__int64 __fastcall runtime_makeNumber(__int64 a1)
{
return makeNumber(a1);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return func1(a1);
}

original_code:
long runtime_makeNumber(long n)
{
    return makeNumber(n);
}


decompiled_code:
__int64 __fastcall prim_die(unsigned __int64 a1)
{
__int64 v1; // rax

mustBe_constprop_0(a1);
v1 = storeAddr(a1);
die(v1 + 8);
return 0LL;
}

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

func9(a1);
v1 = func0(a1);
die(v1 + 8);
return 0LL;
}

original_code:
long prim_die(long e)
{
    const char *s;
    mustBe(CLASS_String, e);
    s = storeAddr(e) + sizeof(long);
    die(s);
    return 0;
}

canonical_original_code:
long prim_die(long e)
{
    char *s;
    mustBe(CLASS_String, e);
    s = storeAddr(e) + sizeof(long);
    die(s);
    return 0;
}

variable_types: e long


decompiled_code:
__int64 __fastcall runtime_makeTuple0(unsigned __int16 a1)
{
if ( runtime_classArities[a1] )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 0LL);
die("Arity error while making tuple.");
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int16 a1)
{
if ( runtime_classArities[a1] )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 0LL);
die("Arity error while making tuple.");
}
return a1;
}

original_code:
long runtime_makeTuple0(unsigned short class)
{
    if (runtime_classArities[class] != 0) {
        fprintf(stderr, "Class: %d Arity: %d\n", (int)class, 0);
        die("Arity error while making tuple.");
    }
    return (long)class;
}

variable_types:
class
unsigned short


decompiled_code:
unsigned __int64 __fastcall runtime_makeTuple1(unsigned __int16 a1, __int64 a2)
{
__int64 v2; // rax

if ( runtime_classArities[a1] != 1 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 1LL);
die("Arity error while making tuple.");
}
v2 = storeAlloc_constprop_0(8LL);
*(_QWORD *)(qword_10650 + v2) = a2;
return a1 | (unsigned __int64)(v2 << 16);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func15(unsigned __int16 a1, __int64 a2)
{
__int64 v2; // rax

if ( runtime_classArities[a1] != 1 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 1LL);
die("Arity error while making tuple.");
}
v2 = func4(8LL);
*(_QWORD *)(qword_10650 + v2) = a2;
return a1 | (unsigned __int64)(v2 << 16);
}

original_code:
long runtime_makeTuple1(unsigned short class, long a)
{
    unsigned long align;
    unsigned long size;
    unsigned long i;
    long *tuple;
    if (runtime_classArities[class] != 1) {
        fprintf(stderr, "Class: %d Arity: %d\n", (int)class, 1);
        die("Arity error while making tuple.");
    }
    align = sizeof(long);
    size = sizeof(long);
    i = storeAlloc(align, size);
    tuple = store.data + i;
    tuple[0] = a;
    return (long)(i << 16 | class);
}

variable_types: a long


Id

decompiled_code:
unsigned __int64 __fastcall Id(__int64 a1)
{
return runtime_makeTuple1(4u, a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func16(__int64 a1)
{
return func15(4u, a1);
}

original_code:
static inline long Id(long name)
{
    return runtime_makeTuple1(CLASS_Id, name);
}

variable_types: name long


decompiled_code:
unsigned __int64 __fastcall runtime_makeTuple2(unsigned __int16 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
_QWORD *v5; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 2 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 2LL);
die("Arity error while making tuple.");
}
v4 = storeAlloc_constprop_0(16LL);
v5 = (_QWORD *)(v4 + qword_10650);
*v5 = a2;
result = a1 | (unsigned __int64)(v4 << 16);
v5[1] = a3;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func17(unsigned __int16 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
_QWORD *v5; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 2 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 2LL);
die("Arity error while making tuple.");
}
v4 = func4(16LL);
v5 = (_QWORD *)(v4 + qword_10650);
*v5 = a2;
result = a1 | (unsigned __int64)(v4 << 16);
v5[1] = a3;
return result;
}

original_code:
long runtime_makeTuple2(unsigned short class, long a, long b)
{
    unsigned long align;
    unsigned long size;
    unsigned long i;
    long *tuple;
    if (runtime_classArities[class] != 2) {
        fprintf(stderr, "Class: %d Arity: %d\n", (int)class, 2);
        die("Arity error while making tuple.");
    }
    align = sizeof(long);
    size = 2 * sizeof(long);
    i = storeAlloc(align, size);
    tuple = store.data + i;
    tuple[0] = a;
    tuple[1] = b;
    return (long)(i << 16 | class);
}

variable_types: b long


decompiled_code:
unsigned __int64 __fastcall runtime_makeTuple3(unsigned __int16 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v6; // rax
_QWORD *v7; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 3 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 3LL);
die("Arity error while making tuple.");
}
v6 = storeAlloc_constprop_0(24LL);
v7 = (_QWORD *)(v6 + qword_10650);
*v7 = a2;
result = a1 | (unsigned __int64)(v6 << 16);
v7[1] = a3;
v7[2] = a4;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func18(unsigned __int16 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v6; // rax
_QWORD *v7; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 3 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 3LL);
die("Arity error while making tuple.");
}
v6 = func4(24LL);
v7 = (_QWORD *)(v6 + qword_10650);
*v7 = a2;
result = a1 | (unsigned __int64)(v6 << 16);
v7[1] = a3;
v7[2] = a4;
return result;
}

original_code:
long runtime_makeTuple3(unsigned short class, long a, long b, long c)
{
    unsigned long align;
    unsigned long size;
    unsigned long i;
    long *tuple;
    if (runtime_classArities[class] != 3) {
        fprintf(stderr, "Class: %d Arity: %d\n", (int)class, 3);
        die("Arity error while making tuple.");
    }
    align = sizeof(long);
    size = 3 * sizeof(long);
    i = storeAlloc(align, size);
    tuple = store.data + i;
    tuple[0] = a;
    tuple[1] = b;
    tuple[2] = c;
    return (long)(i << 16 | class);
}

variable_types: c long


decompiled_code:
unsigned __int64 __fastcall runtime_makeTuple4(unsigned __int16 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // rax
_QWORD *v9; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 4 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 4LL);
die("Arity error while making tuple.");
}
v8 = storeAlloc_constprop_0(32LL);
v9 = (_QWORD *)(v8 + qword_10650);
*v9 = a2;
result = a1 | (unsigned __int64)(v8 << 16);
v9[1] = a3;
v9[2] = a4;
v9[3] = a5;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func19(unsigned __int16 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // rax
_QWORD *v9; // rdx
unsigned __int64 result; // rax

if ( runtime_classArities[a1] != 4 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 4LL);
die("Arity error while making tuple.");
}
v8 = func4(32LL);
v9 = (_QWORD *)(v8 + qword_10650);
*v9 = a2;
result = a1 | (unsigned __int64)(v8 << 16);
v9[1] = a3;
v9[2] = a4;
v9[3] = a5;
return result;
}

original_code:
long runtime_makeTuple4(unsigned short class, long a, long b, long c, long d)
{
    unsigned long align;
    unsigned long size;
    unsigned long i;
    long *tuple;
    if (runtime_classArities[class] != 4) {
        fprintf(stderr, "Class: %d Arity: %d\n", (int)class, 4);
        die("Arity error while making tuple.");
    }
    align = sizeof(long);
    size = 4 * sizeof(long);
    i = storeAlloc(align, size);
    tuple = store.data + i;
    tuple[0] = a;
    tuple[1] = b;
    tuple[2] = c;
    tuple[3] = d;
    return (long)(i << 16 | class);
}

variable_types: d long


decompiled_code:
__int64 runtime_matchFailure()
{
unsigned __int16 v0; // ax
const char *v1; // rdx
__int64 v2; // rcx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v0 = runtime_class(classArgSave);
snprintf(v4, 0x100uLL, v1, v2, v0);
return die(v4);
}

canonical_decompiled_code:
__int64 func20()
{
unsigned __int16 v0; // ax
const char *v1; // rdx
__int64 v2; // rcx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v0 = func7(classArgSave);
snprintf(v4, 0x100uLL, v1, v2, v0);
return die(v4);
}

original_code:
void runtime_matchFailure(int line)
{
    char buf[256];
    snprintf(buf, sizeof(buf), "Match failure. Line: %d. Class: %d",
        line, (int)runtime_class(classArgSave));
    die(buf);
}

variable_types: line int

variable_types:
buf
char [256]


decompiled_code:
unsigned __int64 __fastcall prim_cons(__int64 a1, __int64 a2)
{
return runtime_makeTuple2(3u, a1, a2);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func21(__int64 a1, __int64 a2)
{
return func17(3u, a1, a2);
}

original_code:
long prim_cons(long a, long d)
{
    return runtime_makeTuple2(CLASS_Cons, a, d);
}


decompiled_code:
unsigned __int64 prim_genTmp()
{
__int64 v0; // rcx
__int64 v1; // rax
char v3[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = (unsigned int)tmpCounter++;
snprintf(v3, 0x10uLL, "x%d", v0);
v1 = makeString(v3);
return Id(v1);
}

canonical_decompiled_code:
unsigned __int64 func22()
{
__int64 v0; // rcx
__int64 v1; // rax
char v3[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = (unsigned int)tmpCounter++;
snprintf(v3, 0x10uLL, "x%d", v0);
v1 = func5(v3);
return func16(v1);
}

original_code:
long prim_genTmp(void)
{
    char name[16];
    snprintf(name, sizeof(name), "x%d", tmpCounter++);
    return Id(makeString(name));
}

canonical_original_code:
long prim_genTmp()
{
    char name[16];
    snprintf(name, sizeof(name), "x%d", tmpCounter++);
    return Id(makeString(name));
}

variable_types:
name
char [16]


decompiled_code:
unsigned __int64 prim_genLabel()
{
__int64 v0; // rcx
__int64 v1; // rax
char v3[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = (unsigned int)labelCounter++;
snprintf(v3, 0x10uLL, "L%d", v0);
v1 = makeString(v3);
return Id(v1);
}

canonical_decompiled_code:
unsigned __int64 func23()
{
__int64 v0; // rcx
__int64 v1; // rax
char v3[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = (unsigned int)labelCounter++;
snprintf(v3, 0x10uLL, "L%d", v0);
v1 = func5(v3);
return func16(v1);
}

original_code:
long prim_genLabel(void)
{
    char name[16];
    snprintf(name, sizeof(name), "L%d", labelCounter++);
    return Id(makeString(name));
}

canonical_original_code:
long prim_genLabel()
{
    char name[16];
    snprintf(name, sizeof(name), "L%d", labelCounter++);
    return Id(makeString(name));
}


decompiled_code:
__int64 __fastcall runtime_isPrim(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
if ( !strcmp("fetch", s2) )
return 1LL;
while ( ++v1 != 5 )
{
if ( !strcmp(prims[v1], s2) )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func24(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
if ( !strcmp("fetch", s2) )
return 1LL;
while ( ++v1 != 5 )
{
if ( !strcmp(prims[v1], s2) )
return 1LL;
}
return 0LL;
}

original_code:
int runtime_isPrim(const char *name)
{
    int i;
    for (i = 0; i < (sizeof(prims) / sizeof((prims)[0])); i++)
        if (!strcmp(prims[i], name))
            return 1;
    return 0;
}

canonical_original_code:
int runtime_isPrim(char *name)
{
    int i;
    for (i = 0; i < (sizeof(prims) / sizeof((prims)[0])); i++)
        if (!strcmp(prims[i], name))
            return 1;
    return 0;
}

variable_types:
name
char *

variable_types: i int


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

storeInit_constprop_0();
runtime_0 = runtime_makeNumber(0LL);
runtime_1 = runtime_makeNumber(1LL);
runtime_2 = runtime_makeNumber(2LL);
runtime_3 = runtime_makeNumber(3LL);
result = runtime_makeTuple0(2u);
nil = result;
return result;
}

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

func3();
runtime_0 = func12(0LL);
runtime_1 = func12(1LL);
runtime_2 = func12(2LL);
runtime_3 = func12(3LL);
result = func14(2u);
nil = result;
return result;
}

original_code:
void runtime_init(void)
{
    storeInit(128 * 1024 * 1024);
    runtime_0 = runtime_makeNumber(0);
    runtime_1 = runtime_makeNumber(1);
    runtime_2 = runtime_makeNumber(2);
    runtime_3 = runtime_makeNumber(3);
    nil = runtime_makeTuple0(CLASS_Nil);
}

canonical_original_code:
void runtime_init()
{
    storeInit(128 * 1024 * 1024);
    runtime_0 = runtime_makeNumber(0);
    runtime_1 = runtime_makeNumber(1);
    runtime_2 = runtime_makeNumber(2);
    runtime_3 = runtime_makeNumber(3);
    nil = runtime_makeTuple0(CLASS_Nil);
}


die

decompiled_code:
void __fastcall __noreturn die(const char *a1)
{
fprintf(stderr, "Error: %s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(stderr, "Error: %s\n", a1);
exit(1);
}

original_code:
void die(const char *e)
{
    fprintf(stderr, "Error: %s\n", e);
    exit(1);
}

canonical_original_code:
void die(char *e)
{
    fprintf(stderr, "Error: %s\n", e);
    exit(1);
}

variable_types:
e
char *


decompiled_code:
void require64BitLongs()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void require64BitLongs(void)
{
    if (sizeof(long) != 8)
        die("The C long type is not 64 bits wide.");
}

canonical_original_code:
void require64BitLongs()
{
    if (sizeof(long) != 8)
        die("The C long type is not 64 bits wide.");
}


repo: ebfe/vtcolors

decompiled_code:
int __fastcall get_cmap(int a1, __int64 a2)
{
return ioctl(a1, 0x4B70uLL, a2);
}

canonical_decompiled_code:
int __fastcall func7(int a1, __int64 a2)
{
return ioctl(a1, 0x4B70uLL, a2);
}

original_code:
int get_cmap(int fd, unsigned char *cmap) {
 return ioctl(fd, 0x4B70, cmap);
}

variable_types: fd int

variable_types:
cmap
unsigned char *


decompiled_code:
int __fastcall set_cmap(int a1, __int64 a2)
{
return ioctl(a1, 0x4B71uLL, a2);
}

canonical_decompiled_code:
int __fastcall func8(int a1, __int64 a2)
{
return ioctl(a1, 0x4B71uLL, a2);
}

original_code:
int set_cmap(int fd, unsigned char *cmap) {
 return ioctl(fd, 0x4B71, cmap);
}


decompiled_code:
int __fastcall set_underlined_color(unsigned int a1)
{
return printf("\x1B[1;%d]", a1);
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1)
{
return printf("\x1B[1;%d]", a1);
}

original_code:
void set_underlined_color(int c) {
 printf("\x1b[1;%d]", c);
}

variable_types: c int


decompiled_code:
int __fastcall set_italic_color(unsigned int a1)
{
return printf("\x1B[2;%d]", a1);
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1)
{
return printf("\x1B[2;%d]", a1);
}

original_code:
void set_italic_color(int c) {
 printf("\x1b[2;%d]", c);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
unsigned int v4; // ebp
int v5; // er12
unsigned int v6; // eax
char *v8; // rbx
__int64 v9; // rcx
__int64 v10; // rdx
__int64 v11; // rsi
_BYTE v12[48]; // [rsp+0h] [rbp-58h] BYREF
char v13; // [rsp+30h] [rbp-28h] BYREF

if ( argc <= 1 )
{
v4 = open("/dev/tty", 2, envp);
if ( v4 != -1 )
{
LABEL_11:
if ( (unsigned int)set_cmap(v4, &default_cmap) == -1 )
LABEL_15:
perror("set_cmap");
return 0;
}
LABEL_20:
v5 = 1;
perror("open");
return v5;
}
v3 = argv[1];
v4 = open("/dev/tty", 2, envp);
if ( v4 == -1 )
goto LABEL_20;
if ( !strcmp(v3, "default") )
goto LABEL_11;
if ( !strcmp(v3, "slate") )
{
if ( (unsigned int)set_cmap(v4, &slate_cmap) == -1 )
goto LABEL_15;
return 0;
}
v5 = strcmp(v3, "dump");
if ( v5 )
{
v5 = strcmp(v3, "underlined");
if ( v5 )
{
v5 = 1;
fprintf(stderr, "unknown cmd %s\n", v3);
}
else if ( argc == 2 )
{
v5 = 1;
fwrite("missing color index\n", 1uLL, 0x14uLL, stderr);
}
else
{
v6 = atoi(argv[2]);
if ( v6 > 0xF )
{
v5 = 1;
fwrite("color index out of range\n", 1uLL, 0x19uLL, stderr);
}
else
{
set_underlined_color(v6);
}
}
}
else if ( (unsigned int)get_cmap(v4, v12) == -1 )
{
perror("get_cmap");
}
else
{
v8 = v12;
do
{
v9 = (unsigned __int8)v8[2];
v10 = (unsigned __int8)v8[1];
v11 = (unsigned __int8)*v8;
v8 += 3;
printf("0x%02x, 0x%02x, 0x%02x,\n", v11, v10, v9);
}
while ( &v13 != v8 );
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
unsigned int v4; // ebp
int v5; // er12
unsigned int v6; // eax
char *v8; // rbx
__int64 v9; // rcx
__int64 v10; // rdx
__int64 v11; // rsi
_BYTE v12[48]; // [rsp+0h] [rbp-58h] BYREF
char v13; // [rsp+30h] [rbp-28h] BYREF

if ( argc <= 1 )
{
v4 = open("/dev/tty", 2, envp);
if ( v4 != -1 )
{
LABEL_11:
if ( (unsigned int)func8(v4, &default_cmap) == -1 )
LABEL_15:
perror("set_cmap");
return 0;
}
LABEL_20:
v5 = 1;
perror("open");
return v5;
}
v3 = argv[1];
v4 = open("/dev/tty", 2, envp);
if ( v4 == -1 )
goto LABEL_20;
if ( !strcmp(v3, "default") )
goto LABEL_11;
if ( !strcmp(v3, "slate") )
{
if ( (unsigned int)func8(v4, &slate_cmap) == -1 )
goto LABEL_15;
return 0;
}
v5 = strcmp(v3, "dump");
if ( v5 )
{
v5 = strcmp(v3, "underlined");
if ( v5 )
{
v5 = 1;
fprintf(stderr, "unknown cmd %s\n", v3);
}
else if ( argc == 2 )
{
v5 = 1;
fwrite("missing color index\n", 1uLL, 0x14uLL, stderr);
}
else
{
v6 = atoi(argv[2]);
if ( v6 > 0xF )
{
v5 = 1;
fwrite("color index out of range\n", 1uLL, 0x19uLL, stderr);
}
else
{
func9(v6);
}
}
}
else if ( (unsigned int)func7(v4, v12) == -1 )
{
perror("get_cmap");
}
else
{
v8 = v12;
do
{
v9 = (unsigned __int8)v8[2];
v10 = (unsigned __int8)v8[1];
v11 = (unsigned __int8)*v8;
v8 += 3;
printf("0x%02x, 0x%02x, 0x%02x,\n", v11, v10, v9);
}
while ( &v13 != v8 );
}
return v5;
}

original_code:
int main(int argc, char *argv[]) {
 const char *tty = "/dev/tty";
 const char *cmd = "default";
 int fd;
 if (argc > 1)
  cmd = argv[1];
 fd = open(tty, 02);
 if (fd == -1) {
  perror("open");
  return 1;
 }
 if (strcmp(cmd, "default") == 0) {
  if (set_cmap(fd, default_cmap) == -1) {
   perror("set_cmap");
  }
 } else if (strcmp(cmd, "slate") == 0) {
  if (set_cmap(fd, slate_cmap) == -1) {
   perror("set_cmap");
  }
 } else if (strcmp(cmd, "dump") == 0) {
  unsigned char cmap[3*16];
  if (get_cmap(fd, cmap) != -1) {
   for (int i = 0; i < sizeof(cmap); i += 3) {
    printf("0x%02x, 0x%02x, 0x%02x,\n", cmap[i], cmap[i+1], cmap[i+2]);
   }
  } else {
   perror("get_cmap");
  }
 } else if (strcmp(cmd, "underlined") == 0) {
  int c;
  if (argc < 3) {
   fprintf(stderr, "missing color index\n");
   return 1;
  }
  c = atoi(argv[2]);
  if (c < 0 || c > 15) {
   fprintf(stderr, "color index out of range\n");
   return 1;
  }
  set_underlined_color(c);
 } else {
  fprintf(stderr, "unknown cmd %s\n", cmd);
  return 1;
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 char *tty = "/dev/tty";
 char *cmd = "default";
 int fd;
 if (argc > 1)
  cmd = argv[1];
 fd = open(tty, 02);
 if (fd == -1) {
  perror("open");
  return 1;
 }
 if (strcmp(cmd, "default") == 0) {
  if (set_cmap(fd, default_cmap) == -1) {
   perror("set_cmap");
  }
 } else if (strcmp(cmd, "slate") == 0) {
  if (set_cmap(fd, slate_cmap) == -1) {
   perror("set_cmap");
  }
 } else if (strcmp(cmd, "dump") == 0) {
  unsigned char cmap[-1];
  if (get_cmap(fd, cmap) != -1) {
   for (int i = 0; i < sizeof(cmap); i += 3) {
    printf("0x%02x, 0x%02x, 0x%02x,\n", cmap[i], cmap[i+1], cmap[i+2]);
   }
  } else {
   perror("get_cmap");
  }
 } else if (strcmp(cmd, "underlined") == 0) {
  int c;
  if (argc < 3) {
   fprintf(stderr, "missing color index\n");
   return 1;
  }
  c = atoi(argv[2]);
  if (c < 0 || c > 15) {
   fprintf(stderr, "color index out of range\n");
   return 1;
  }
  set_underlined_color(c);
 } else {
  fprintf(stderr, "unknown cmd %s\n", cmd);
  return 1;
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
tty
char *

variable_types:
cmd
char *

variable_types:
cmap
unsigned char [-1]


decompiled_code:
int __fastcall csi_m(unsigned int a1)
{
return printf("%s%dm", "\x1B[", a1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1)
{
return printf("%s%dm", "\x1B[", a1);
}

original_code:
void csi_m(int n) {
 printf("%s%dm", "\x1b[", n);
}

variable_types: n int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rdx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rdx
__int64 v17; // rdx
__int64 v18; // rdx
__int64 v19; // rdx

for ( i = 0; i != 8; ++i )
{
csi_m(0LL, argv, envp);
csi_m(i + 30, argv, v4);
printf("color%d", i);
csi_m(0LL, i, v5);
putchar(32);
csi_m(i + 30, i, v6);
csi_m(1LL, i, v7);
printf("bold%d", i);
csi_m(0LL, i, v8);
putchar(32);
csi_m(i + 30, i, v9);
csi_m(3LL, i, v10);
printf("italic%d", i);
csi_m(0LL, i, v11);
putchar(32);
csi_m(i + 30, i, v12);
csi_m(4LL, i, v13);
printf("underlined%d", i);
csi_m(0LL, i, v14);
putchar(32);
csi_m(i + 30, i, v15);
csi_m(5LL, i, v16);
printf("blink%d", i);
csi_m(0LL, i, v17);
putchar(32);
csi_m(i + 30, i, v18);
csi_m(7LL, i, v19);
argv = (const char **)i;
printf("reverse%d\n", i);
}
csi_m(0LL, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rdx
__int64 v11; // rdx
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rdx
__int64 v15; // rdx
__int64 v16; // rdx
__int64 v17; // rdx
__int64 v18; // rdx
__int64 v19; // rdx

for ( i = 0; i != 8; ++i )
{
func7(0LL, argv, envp);
func7(i + 30, argv, v4);
printf("color%d", i);
func7(0LL, i, v5);
putchar(32);
func7(i + 30, i, v6);
func7(1LL, i, v7);
printf("bold%d", i);
func7(0LL, i, v8);
putchar(32);
func7(i + 30, i, v9);
func7(3LL, i, v10);
printf("italic%d", i);
func7(0LL, i, v11);
putchar(32);
func7(i + 30, i, v12);
func7(4LL, i, v13);
printf("underlined%d", i);
func7(0LL, i, v14);
putchar(32);
func7(i + 30, i, v15);
func7(5LL, i, v16);
printf("blink%d", i);
func7(0LL, i, v17);
putchar(32);
func7(i + 30, i, v18);
func7(7LL, i, v19);
argv = (const char **)i;
printf("reverse%d\n", i);
}
func7(0LL, argv, envp);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 for (int color = 0; color < 8; color++) {
  csi_m(0);
  csi_m(30 + color);
  printf("color%d", color);
  csi_m(0);
  putchar(' ');
  csi_m(30 + color);
  csi_m(1);
  printf("bold%d", color);
  csi_m(0);
  putchar(' ');
  csi_m(30 + color);
  csi_m(3);
  printf("italic%d", color);
  csi_m(0);
  putchar(' ');
  csi_m(30 + color);
  csi_m(4);
  printf("underlined%d", color);
  csi_m(0);
  putchar(' ');
  csi_m(30 + color);
  csi_m(5);
  printf("blink%d", color);
  csi_m(0);
  putchar(' ');
  csi_m(30 + color);
  csi_m(7);
  printf("reverse%d\n", color);
 }
 csi_m(0);
}

variable_types: color int


repo: ebobby/stackoverflow-cli

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er8
int result; // eax
int v5[18]; // [rsp+0h] [rbp-48h] BYREF

v3 = processArguments(argc, argv, v5);
result = 1;
if ( v3 && v5[0] )
{
switch ( v5[0] )
{
case 1:
result = searchCommand(v5) == 0;
break;
case 2:
result = usersCommand(v5) == 0;
break;
case 3:
result = questionsCommand(v5) == 0;
break;
}
}
return result;
}

original_code:
int main (int argc, char **argv) {
    stackoverflow_cli_opts global_opts;
    int result = 1;
    if (!processArguments(argc, argv, &global_opts))
        return result;
    if (global_opts.operation == none)
        result = 1;
    else if (global_opts.operation == search)
        result = searchCommand(&global_opts) ? 0 : 1;
    else if (global_opts.operation == users)
        result = usersCommand(&global_opts) ? 0 : 1;
    else if (global_opts.operation == questions)
        result = questionsCommand(&global_opts) ? 0 : 1;
    return result;
}

canonical_original_code:
int main(int argc, char **argv) {
    struct stackoverflow_cli_opts global_opts;
    int result = 1;
    if (!processArguments(argc, argv, &global_opts))
        return result;
    if (global_opts.operation == none)
        result = 1;
    else if (global_opts.operation == search)
        result = searchCommand(&global_opts) ? 0 : 1;
    else if (global_opts.operation == users)
        result = usersCommand(&global_opts) ? 0 : 1;
    else if (global_opts.operation == questions)
        result = questionsCommand(&global_opts) ? 0 : 1;
    return result;
}

variable_types:
argv
char **

variable_types: struct stackoverflow_cli_opts { enum stackoverflow_operation { none=0, search=1, users=2, questions=3, user=4 } operation; char *nottagged; char *tagged; char *intitle; char *filter; char *id; char *pagesize; char *page; }

variable_types: result int

user_defined_types: struct stackoverflow_cli_opts { enum stackoverflow_operation operation; char *nottagged; char *tagged; char *intitle; char *filter; char *id; char *pagesize; char *page; }