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

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebx
unsigned __int16 *v4; // r12
__int64 v5; // rsi
unsigned __int16 v6; // r13
__int64 v7; // rax
int *v9; // rax
const char *v10; // r12
unsigned int v11; // er14
int v12; // er13
_DWORD *v13; // rax
_DWORD *v14; // rbp

v3 = *(_DWORD *)a3;
if ( *(_DWORD *)a3 )
{
switch ( v3 )
{
case 1u:
puts("got ping, send pong");
v9 = func0(2);
func1(a1, a2, (__int64)v9);
v3 = 0;
break;
case 4u:
v10 = *(const char **)(a3 + 8);
v11 = *(unsigned __int16 *)v10;
v12 = v10[2];
v13 = (_DWORD *)revsrv_netw_hndl(a1, *(unsigned __int16 *)v10);
v14 = v13;
v3 = *v13;
if ( *v13 == 2 )
{
if ( v12 == 1 )
{
printf("netw handle %d failed to connect '%s'\n", v11, v10 + 3);
*v14 = 5;
v3 = 0;
}
else
{
if ( v12 )
{
*(_BYTE *)empty_msg = 0;
}
else
{
printf("netw handle %d established connection\n", v11);
*v14 = 3;
}
v3 = 0;
}
}
else if ( v12 == 1 && v3 == 3 )
{
*v13 = 4;
v3 = 0;
}
else if ( v3 )
{
*(_BYTE *)empty_msg = 0;
v3 = 0;
}
else
{
puts("WARNING: conn state on offline handle");
}
break;
case 5u:
v4 = *(unsigned __int16 **)(a3 + 8);
v5 = *v4;
v6 = v4[1];
if ( *(_DWORD *)(a3 + 4) != v6 + 4 )
*(_BYTE *)empty_msg = 0;
v7 = revsrv_netw_hndl(a1, v5);
v3 = 0;
if ( v7 )
{
v3 = fifo_write(v7 + 288, v4 + 2, v6);
if ( v3 )
{
*(_BYTE *)empty_msg = 0;
v3 = 0;
}
}
break;
default:
v3 = -1;
break;
}
}
else
{
printf("dbg msg '%s'\n", *(const char **)(a3 + 8));
}
return v3;
}

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


repo: east/php-revsocks

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; // rbp
unsigned __int64 v3; // rdx
_BOOL8 result; // rax
unsigned __int16 *v5; // rax
unsigned __int16 v6; // r14
unsigned int v7; // er13
unsigned __int8 v8; // r15
unsigned __int64 v9; // rcx
int v10[2]; // [rsp+0h] [rbp-1048h] BYREF
char *v11; // [rsp+8h] [rbp-1040h]
char v12[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = a2 + 8;
v3 = fifo_len(a2 + 8);
result = 0LL;
if ( v3 > 2 )
{
v5 = *(unsigned __int16 **)(a2 + 8);
v6 = *v5;
v7 = *v5;
v8 = *((_BYTE *)v5 + 2);
if ( v7 > 0x1000 )
*(_BYTE *)empty_msg = 0;
v9 = fifo_len(a2 + 8);
result = 0LL;
if ( v9 >= (int)(v7 + 3) )
{
fifo_read(v2, 0LL, 3LL);
v10[0] = v8;
v10[1] = v6;
v11 = v12;
fifo_read(v2, v12, v6);
if ( (unsigned int)handle_msg(a1, a2, (__int64)v10) )
*(_BYTE *)empty_msg = 0;
result = fifo_len(v2) != 0;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
unsigned __int64 v3; // rdx
_BOOL8 result; // rax
unsigned __int16 *v5; // rax
unsigned __int16 v6; // r14
unsigned int v7; // er13
unsigned __int8 v8; // r15
unsigned __int64 v9; // rcx
int v10[2]; // [rsp+0h] [rbp-1048h] BYREF
char *v11; // [rsp+8h] [rbp-1040h]
char v12[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = a2 + 8;
v3 = fifo_len(a2 + 8);
result = 0LL;
if ( v3 > 2 )
{
v5 = *(unsigned __int16 **)(a2 + 8);
v6 = *v5;
v7 = *v5;
v8 = *((_BYTE *)v5 + 2);
if ( v7 > 0x1000 )
*(_BYTE *)empty_msg = 0;
v9 = fifo_len(a2 + 8);
result = 0LL;
if ( v9 >= (int)(v7 + 3) )
{
fifo_read(v2, 0LL, 3LL);
v10[0] = v8;
v10[1] = v6;
v11 = v12;
fifo_read(v2, v12, v6);
if ( (unsigned int)func2(a1, a2, (__int64)v10) )
*(_BYTE *)empty_msg = 0;
result = fifo_len(v2) != 0;
}
}
return result;
}

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: 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 FIFOBUF { char *data; unsigned long size; unsigned long offset; } *

variable_types:
buf
char [-1]

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

return_type: int

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

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


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 func4(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax

do
result = func3(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;
}

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

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


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

variable_types:
data
char *

return_type: void


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_10610 + (a1 >> 16);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
return qword_10610 + (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:
unsigned __int64 __fastcall makeString(void *src)
{
__int64 v1; // rbp
unsigned __int64 v2; // rbx
__int64 v3; // r13
__int64 *v4; // r14

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

canonical_decompiled_code:
unsigned __int64 __fastcall func4(void *src)
{
__int64 v1; // rbp
unsigned __int64 v2; // rbx
__int64 v3; // r13
__int64 *v4; // r14

v1 = strlen((const char *)src);
v2 = func3(8uLL, v1 + 9);
v3 = qword_10610;
v4 = (__int64 *)(qword_10610 + v2);
*v4 = func1(v1);
memmove((void *)(v3 + 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:
unsigned __int64 __fastcall runtime_makeString(void *a1)
{
return makeString(a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(void *a1)
{
return func4(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)
{
__int64 result; // rax

result = a1;
classArgSave = a1;
return result;
}

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

result = a1;
classArgSave = a1;
return result;
}

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, __int64 a2)
{
__int64 v2; // rdx

if ( (unsigned __int16)runtime_class(a1) != 1 )
die("Type error.", a2, v2);
return storeAddr(a1) + 8;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 a1, __int64 a2)
{
__int64 v2; // rdx

if ( (unsigned __int16)func7(a1) != 1 )
die("Type error.", a2, v2);
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)
{
unsigned __int16 v2; // bx
__int64 v3; // rdx
signed __int64 v4; // rbp

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

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int64 a1, __int64 a2)
{
unsigned __int16 v2; // bx
__int64 v3; // rdx
signed __int64 v4; // rbp

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

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
__int64 v2; // rdx

mustBe(1, a1);
v1 = storeAddr(a1);
die(v1 + 8, a1, v2);
return 0LL;
}

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

func9(1, a1);
v1 = func0(a1);
die(v1 + 8, a1, v2);
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)
{
__int64 v2; // rdx

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

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int16 a1)
{
__int64 v2; // rdx

if ( runtime_classArities[a1] )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 0LL);
die("Arity error while making tuple.", "Class: %d Arity: %d\n", v2);
}
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)
{
unsigned __int64 v2; // rax
__int64 v4; // rdx

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

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

if ( runtime_classArities[a1] != 1 )
{
fprintf(stderr, "Class: %d Arity: %d\n", a1, 1LL);
die("Arity error while making tuple.", "Class: %d Arity: %d\n", v4);
}
v2 = func3(8uLL, 8LL);
*(_QWORD *)(qword_10610 + v2) = a2;
return a1 | (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)
{
unsigned __int64 v4; // rax
_QWORD *v5; // rdx
__int64 v7; // rdx

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

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

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

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)
{
unsigned __int64 v6; // rax
_QWORD *v7; // rdx
__int64 v9; // rdx

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

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

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

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)
{
unsigned __int64 v8; // rax
_QWORD *v9; // rdx
__int64 v11; // rdx

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

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

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

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 __fastcall runtime_matchFailure(unsigned int a1)
{
unsigned __int16 v1; // ax
__int64 v2; // rdx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = runtime_class(classArgSave);
snprintf(v4, 0x100uLL, "Match failure. Line: %d. Class: %d", a1, v1);
return die(v4, 256LL, v2);
}

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

v1 = func7(classArgSave);
snprintf(v4, 0x100uLL, "Match failure. Line: %d. Class: %d", a1, v1);
return die(v4, 256LL, v2);
}

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 = func4(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 = func4(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;
while ( strcmp((&prims)[v1], s2) )
{
if ( ++v1 == 5 )
return 0LL;
}
return 1LL;
}

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

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

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 __fastcall runtime_init(__int64 a1, __int64 a2)
{
__int64 result; // rax

storeInit(0x8000000uLL, a2);
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 __fastcall func25(__int64 a1, __int64 a2)
{
__int64 result; // rax

func5(0x8000000uLL, a2);
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

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // ebx
int v5; // ebp
unsigned int v6; // eax
unsigned __int8 *v7; // rbx
_BYTE v9[48]; // [rsp+0h] [rbp-58h] BYREF
char v10; // [rsp+30h] [rbp-28h] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // ebx
int v5; // ebp
unsigned int v6; // eax
unsigned __int8 *v7; // rbx
_BYTE v9[48]; // [rsp+0h] [rbp-58h] BYREF
char v10; // [rsp+30h] [rbp-28h] BYREF

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

for ( i = 0; i != 8; ++i )
{
csi_m(0);
csi_m(i + 30);
printf("color%d", i);
csi_m(0);
putchar(32);
csi_m(i + 30);
csi_m(1u);
printf("bold%d", i);
csi_m(0);
putchar(32);
csi_m(i + 30);
csi_m(3u);
printf("italic%d", i);
csi_m(0);
putchar(32);
csi_m(i + 30);
csi_m(4u);
printf("underlined%d", i);
csi_m(0);
putchar(32);
csi_m(i + 30);
csi_m(5u);
printf("blink%d", i);
csi_m(0);
putchar(32);
csi_m(i + 30);
csi_m(7u);
printf("reverse%d\n", i);
}
csi_m(0);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx

for ( i = 0; i != 8; ++i )
{
func7(0);
func7(i + 30);
printf("color%d", i);
func7(0);
putchar(32);
func7(i + 30);
func7(1u);
printf("bold%d", i);
func7(0);
putchar(32);
func7(i + 30);
func7(3u);
printf("italic%d", i);
func7(0);
putchar(32);
func7(i + 30);
func7(4u);
printf("underlined%d", i);
func7(0);
putchar(32);
func7(i + 30);
func7(5u);
printf("blink%d", i);
func7(0);
putchar(32);
func7(i + 30);
func7(7u);
printf("reverse%d\n", i);
}
func7(0);
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; // edx
int result; // eax
int v5[18]; // [rsp+0h] [rbp-48h] BYREF

v3 = processArguments(argc, argv, v5);
result = 1;
if ( v3 && v5[0] )
{
if ( v5[0] == 1 )
{
result = searchCommand(v5) == 0;
}
else if ( v5[0] == 2 )
{
result = usersCommand(v5) == 0;
}
else
{
result = 1;
if ( v5[0] == 3 )
result = questionsCommand(v5) == 0;
}
}
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; }


printUsage

decompiled_code:
int __fastcall printUsage(const char *a1, const char *a2)
{
if ( a2 )
fprintf(stderr, "%s\n", a2);
printf("Usage: %s <operation> [options...]\n\n", a1);
puts("Operations has to be one of the following:\n");
puts("Search:");
puts("\t--search\t\tSearches the questions database.");
puts("\t--intitle <string>\tA string that must appear verbatim in the title.");
puts("\t--tagged <string>\tList of tags delimited by semicolons at least one has to be on the questions.");
puts("\t--nottagged <string>\tList of tags delimited by semicolons that must not be in the question.");
putchar(10);
puts("Users:");
puts("\t--users\t\t\tSearches the users database.");
puts("\t--id <string>\t\tList of user ids delimited by a semicolon.");
puts("\t--filter <string>\tA string that must appear in the users name.");
putchar(10);
puts("Questions:");
puts("\t--questions\t\tSearches the questions database with answers.");
puts("\t--id <string>\t\tList of question ids delimited by a semicolon.");
putchar(10);
puts("Generic parameters:\n");
puts("\t--pagesize <size>\tHow many items per page? 100 maximum.");
puts("\t--page <page>\t\tWhat page do you want to see?");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2)
{
if ( a2 )
fprintf(stderr, "%s\n", a2);
printf("Usage: %s <operation> [options...]\n\n", a1);
puts("Operations has to be one of the following:\n");
puts("Search:");
puts("\t--search\t\tSearches the questions database.");
puts("\t--intitle <string>\tA string that must appear verbatim in the title.");
puts("\t--tagged <string>\tList of tags delimited by semicolons at least one has to be on the questions.");
puts("\t--nottagged <string>\tList of tags delimited by semicolons that must not be in the question.");
putchar(10);
puts("Users:");
puts("\t--users\t\t\tSearches the users database.");
puts("\t--id <string>\t\tList of user ids delimited by a semicolon.");
puts("\t--filter <string>\tA string that must appear in the users name.");
putchar(10);
puts("Questions:");
puts("\t--questions\t\tSearches the questions database with answers.");
puts("\t--id <string>\t\tList of question ids delimited by a semicolon.");
putchar(10);
puts("Generic parameters:\n");
puts("\t--pagesize <size>\tHow many items per page? 100 maximum.");
puts("\t--page <page>\t\tWhat page do you want to see?");
return putchar(10);
}

original_code:
void printUsage (const char *name, const char *msg) {
    if (msg != ((void *)0))
        fprintf(stderr, "%s\n", msg);
    printf("Usage: %s <operation> [options...]\n\n", name);
    printf("Operations has to be one of the following:\n\n");
    printf("Search:\n");
    printf("\t--search\t\tSearches the questions database.\n");
    printf("\t--intitle <string>\tA string that must appear verbatim in the title.\n");
    printf("\t--tagged <string>\tList of tags delimited by semicolons at least one has to be on the questions.\n");
    printf("\t--nottagged <string>\tList of tags delimited by semicolons that must not be in the question.\n");
    printf("\n");
    printf("Users:\n");
    printf("\t--users\t\t\tSearches the users database.\n");
    printf("\t--id <string>\t\tList of user ids delimited by a semicolon.\n");
    printf("\t--filter <string>\tA string that must appear in the users name.\n");
    printf("\n");
    printf("Questions:\n");
    printf("\t--questions\t\tSearches the questions database with answers.\n");
    printf("\t--id <string>\t\tList of question ids delimited by a semicolon.\n");
    printf("\n");
    printf("Generic parameters:\n\n");
    printf("\t--pagesize <size>\tHow many items per page? 100 maximum.\n");
    printf("\t--page <page>\t\tWhat page do you want to see?\n");
    printf("\n");
}

canonical_original_code:
void printUsage(char *name, char *msg) {
    if (msg != ((void *)0))
        fprintf(stderr, "%s\n", msg);
    printf("Usage: %s <operation> [options...]\n\n", name);
    printf("Operations has to be one of the following:\n\n");
    printf("Search:\n");
    printf("\t--search\t\tSearches the questions database.\n");
    printf("\t--intitle <string>\tA string that must appear verbatim in the title.\n");
    printf("\t--tagged <string>\tList of tags delimited by semicolons at least one has to be on the questions.\n");
    printf("\t--nottagged <string>\tList of tags delimited by semicolons that must not be in the question.\n");
    printf("\n");
    printf("Users:\n");
    printf("\t--users\t\t\tSearches the users database.\n");
    printf("\t--id <string>\t\tList of user ids delimited by a semicolon.\n");
    printf("\t--filter <string>\tA string that must appear in the users name.\n");
    printf("\n");
    printf("Questions:\n");
    printf("\t--questions\t\tSearches the questions database with answers.\n");
    printf("\t--id <string>\t\tList of question ids delimited by a semicolon.\n");
    printf("\n");
    printf("Generic parameters:\n\n");
    printf("\t--pagesize <size>\tHow many items per page? 100 maximum.\n");
    printf("\t--page <page>\t\tWhat page do you want to see?\n");
    printf("\n");
}

variable_types:
msg
char *


processArguments

decompiled_code:
__int64 __fastcall processArguments(int argc, char **argv, _QWORD *a3)
{
int v4; // eax
unsigned int v5; // edx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

*a3 = 0LL;
a3[1] = 0LL;
a3[2] = 0LL;
a3[3] = 0LL;
a3[4] = 0LL;
a3[5] = 0LL;
a3[6] = 0LL;
a3[7] = 0LL;
while ( 1 )
{
longind[0] = 0;
v4 = getopt_long(argc, argv, "SUn:g:t:s:p:f:h?", &getopt_long_opts_0, longind);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'Q':
*(_DWORD *)a3 = 3;
break;
case 'S':
*(_DWORD *)a3 = 1;
break;
case 'U':
*(_DWORD *)a3 = 2;
break;
case 'f':
a3[4] = optarg;
break;
case 'g':
a3[2] = optarg;
break;
case 'i':
a3[5] = optarg;
break;
case 'n':
a3[1] = optarg;
break;
case 'p':
a3[7] = optarg;
break;
case 's':
a3[6] = optarg;
break;
case 't':
a3[3] = optarg;
break;
default:
continue;
}
}
if ( *(_DWORD *)a3 )
{
v5 = 1;
if ( *(_DWORD *)a3 == 3 && !a3[5] )
{
printUsage(*argv, "This operation needs --id to be specified.\n");
v5 = 0;
}
}
else
{
printUsage(*argv, 0LL);
v5 = 0;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv, _QWORD *a3)
{
int v4; // eax
unsigned int v5; // edx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

*a3 = 0LL;
a3[1] = 0LL;
a3[2] = 0LL;
a3[3] = 0LL;
a3[4] = 0LL;
a3[5] = 0LL;
a3[6] = 0LL;
a3[7] = 0LL;
while ( 1 )
{
longind[0] = 0;
v4 = getopt_long(argc, argv, "SUn:g:t:s:p:f:h?", &getopt_long_opts_0, longind);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'Q':
*(_DWORD *)a3 = 3;
break;
case 'S':
*(_DWORD *)a3 = 1;
break;
case 'U':
*(_DWORD *)a3 = 2;
break;
case 'f':
a3[4] = optarg;
break;
case 'g':
a3[2] = optarg;
break;
case 'i':
a3[5] = optarg;
break;
case 'n':
a3[1] = optarg;
break;
case 'p':
a3[7] = optarg;
break;
case 's':
a3[6] = optarg;
break;
case 't':
a3[3] = optarg;
break;
default:
continue;
}
}
if ( *(_DWORD *)a3 )
{
v5 = 1;
if ( *(_DWORD *)a3 == 3 && !a3[5] )
{
func0(*argv, "This operation needs --id to be specified.\n");
v5 = 0;
}
}
else
{
func0(*argv, 0LL);
v5 = 0;
}
return v5;
}

original_code:
int processArguments (int argc, char **argv, stackoverflow_cli_opts *opts) {
    static const struct option getopt_long_opts[] = {
        { "search", 0, ((void *)0), 'S' },
        { "nottagged", 1, ((void *)0), 'n' },
        { "tagged", 1, ((void *)0), 'g' },
        { "intitle", 1, ((void *)0), 't' },
        { "pagesize", 1, ((void *)0), 's' },
        { "page", 1, ((void *)0), 'p' },
        { "users", 0, ((void *)0), 'U' },
        { "filter", 1, ((void *)0), 'f' },
        { "id", 1, ((void *)0), 'i' },
        { "questions", 0, ((void *)0), 'Q' },
        { ((void *)0), 0, ((void *)0), 0 }
    };
    char *optstring = "SUn:g:t:s:p:f:h?";
    memset(opts, 0, sizeof(stackoverflow_cli_opts));
    opts->operation = none;
    for (;;) {
        int opt_index = 0;
        int opt = getopt_long(argc, argv, optstring, getopt_long_opts, &opt_index);
        if (opt == -1)
            break;
        switch (opt) {
        case 'S':
            opts->operation = search;
            break;
        case 'U':
            opts->operation = users;
            break;
        case 'Q':
            opts->operation = questions;
            break;
        case 'n':
            opts->nottagged = optarg;
            break;
        case 'g':
            opts->tagged = optarg;
            break;
        case 't':
            opts->intitle = optarg;
            break;
        case 's':
            opts->pagesize = optarg;
            break;
        case 'p':
            opts->page = optarg;
            break;
        case 'f':
            opts->filter = optarg;
            break;
        case 'i':
            opts->id = optarg;
            break;
        case 'h':
        case '?':
        default:
            break;
        }
    }
    if (opts->operation == none) {
        printUsage(argv[0], ((void *)0));
        return 0;
    }
    if (opts->operation == questions && opts->id == ((void *)0)) {
        printUsage(argv[0], "This operation needs --id to be specified.\n");
        return 0;
    }
    return 1;
}

canonical_original_code:
int processArguments(int argc, char **argv, struct stackoverflow_cli_opts *opts) {
    static struct option getopt_long_opts[] = {
        { "search", 0, ((void *)0), 'S' },
        { "nottagged", 1, ((void *)0), 'n' },
        { "tagged", 1, ((void *)0), 'g' },
        { "intitle", 1, ((void *)0), 't' },
        { "pagesize", 1, ((void *)0), 's' },
        { "page", 1, ((void *)0), 'p' },
        { "users", 0, ((void *)0), 'U' },
        { "filter", 1, ((void *)0), 'f' },
        { "id", 1, ((void *)0), 'i' },
        { "questions", 0, ((void *)0), 'Q' },
        { ((void *)0), 0, ((void *)0), 0 }
    };
    char *optstring = "SUn:g:t:s:p:f:h?";
    memset(opts, 0, sizeof(stackoverflow_cli_opts));
    opts->operation = none;
    for (;;) {
        int opt_index = 0;
        int opt = getopt_long(argc, argv, optstring, getopt_long_opts, &opt_index);
        if (opt == -1)
            break;
        switch (opt) {
        case 'S':
            opts->operation = search;
            break;
        case 'U':
            opts->operation = users;
            break;
        case 'Q':
            opts->operation = questions;
            break;
        case 'n':
            opts->nottagged = optarg;
            break;
        case 'g':
            opts->tagged = optarg;
            break;
        case 't':
            opts->intitle = optarg;
            break;
        case 's':
            opts->pagesize = optarg;
            break;
        case 'p':
            opts->page = optarg;
            break;
        case 'f':
            opts->filter = optarg;
            break;
        case 'i':
            opts->id = optarg;
            break;
        case 'h':
        case '?':
        default:
            break;
        }
    }
    if (opts->operation == none) {
        printUsage(argv[0], ((void *)0));
        return 0;
    }
    if (opts->operation == questions && opts->id == ((void *)0)) {
        printUsage(argv[0], "This operation needs --id to be specified.\n");
        return 0;
    }
    return 1;
}

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: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types:
optstring
char *

variable_types: opt int


requestCallback

decompiled_code:
size_t __fastcall requestCallback(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v5; // rsi
char *v7; // rax
size_t v8; // rax

v5 = a3 * a2;
if ( v5 )
{
v7 = (char *)realloc(*(void **)(a4 + 8), v5 + *(_QWORD *)a4 + 1);
*(_QWORD *)(a4 + 8) = v7;
if ( !v7 )
{
printf("Not enough memory!");
exit(1);
}
memcpy(&v7[*(_QWORD *)a4], src, v5);
v8 = *(_QWORD *)a4 + v5;
*(_QWORD *)a4 = v8;
*(_BYTE *)(*(_QWORD *)(a4 + 8) + v8) = 0;
}
else
{
*(_QWORD *)(a4 + 8) = 0LL;
*(_QWORD *)a4 = 0LL;
}
return v5;
}

canonical_decompiled_code:
size_t __fastcall func0(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v5; // rsi
char *v7; // rax
size_t v8; // rax

v5 = a3 * a2;
if ( v5 )
{
v7 = (char *)realloc(*(void **)(a4 + 8), v5 + *(_QWORD *)a4 + 1);
*(_QWORD *)(a4 + 8) = v7;
if ( !v7 )
{
printf("Not enough memory!");
exit(1);
}
memcpy(&v7[*(_QWORD *)a4], src, v5);
v8 = *(_QWORD *)a4 + v5;
*(_QWORD *)a4 = v8;
*(_BYTE *)(*(_QWORD *)(a4 + 8) + v8) = 0;
}
else
{
*(_QWORD *)(a4 + 8) = 0LL;
*(_QWORD *)a4 = 0LL;
}
return v5;
}

original_code:
static size_t requestCallback (char *ptr, size_t size, size_t nmemb, void *usr) {
    size_t realsize = size * nmemb;
    responseObject *response = (responseObject *)usr;
    if (realsize != 0) {
        response->data = realloc(response->data, response->size + realsize + 1);
        if (response->data == ((void *)0)) {
            printf("Not enough memory!");
            exit(1);
        }
        memcpy(&response->data[response->size], ptr, realsize);
        response->size += realsize;
        response->data[response->size] = 0;
    } else {
        response->data = ((void *)0);
        response->size = 0;
    }
    return realsize;
}

canonical_original_code:
static unsigned long requestCallback(char *ptr, unsigned long size, unsigned long nmemb, void *usr) {
    unsigned long realsize = size * nmemb;
    struct responseObject *response = (responseObject *)usr;
    if (realsize != 0) {
        response->data = realloc(response->data, response->size + realsize + 1);
        if (response->data == ((void *)0)) {
            printf("Not enough memory!");
            exit(1);
        }
        memcpy(&response->data[response->size], ptr, realsize);
        response->size += realsize;
        response->data[response->size] = 0;
    } else {
        response->data = ((void *)0);
        response->size = 0;
    }
    return realsize;
}

variable_types:
ptr
char *

variable_types:
size
unsigned long

variable_types:
nmemb
unsigned long

variable_types:
usr
void *

variable_types:
realsize
unsigned long

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

return_type: unsigned long


freeWebResponse

decompiled_code:
void __fastcall freeWebResponse(_QWORD *a1)
{
void *v2; // rdi

if ( a1 )
{
v2 = (void *)a1[1];
if ( v2 )
free(v2);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func3(_QWORD *a1)
{
void *v2; // rdi

if ( a1 )
{
v2 = (void *)a1[1];
if ( v2 )
free(v2);
*a1 = 0LL;
}
}

original_code:
void freeWebResponse (responseObject *response) {
    if (response == ((void *)0))
        return;
    if (response->data != ((void *)0))
        free(response->data);
    response->size = 0;
}

canonical_original_code:
void freeWebResponse(struct responseObject *response) {
    if (response == ((void *)0))
        return;
    if (response->data != ((void *)0))
        free(response->data);
    response->size = 0;
}

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


IoNotifyInit

repo: ebortolini/MonitorB

decompiled_code:
int IoNotifyInit()
{
return inotify_init();
}

canonical_decompiled_code:
int func0()
{
return inotify_init();
}

original_code:
int IoNotifyInit(){
 return inotify_init();
}


AddWatcher

decompiled_code:
__int64 __fastcall AddWatcher(char *name)
{
__int64 result; // rax
int v2; // eax

result = 0xFFFFFFFFLL;
if ( name )
{
v2 = inotify_add_watch(*((_DWORD *)name + 125), name, *((_DWORD *)name + 127));
*((_DWORD *)name + 126) = v2;
result = (v2 >> 31) | 1u;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *name)
{
__int64 result; // rax
int v2; // eax

result = 0xFFFFFFFFLL;
if ( name )
{
v2 = inotify_add_watch(*((_DWORD *)name + 125), name, *((_DWORD *)name + 127));
*((_DWORD *)name + 126) = v2;
result = (v2 >> 31) | 1u;
}
return result;
}

original_code:
int AddWatcher(TargetToMonitor * Target){
 if(!Target )
  return -1;
 Target->WatchHandle = inotify_add_watch(Target->InitHandle,
   Target->DictoryName,Target->Flags);
 if (Target->WatchHandle < 0)
  return -1;
 return 1;
}

canonical_original_code:
int AddWatcher(struct STargetToMonitor *Target){
 if(!Target )
  return -1;
 Target->WatchHandle = inotify_add_watch(Target->InitHandle,
   Target->DictoryName,Target->Flags);
 if (Target->WatchHandle < 0)
  return -1;
 return 1;
}

variable_types: struct STargetToMonitor { char DictoryName[500]; int InitHandle; int WatchHandle; int Flags; struct STargetToMonitor *Next; } *


RemoveWatcher

decompiled_code:
int __fastcall RemoveWatcher(__int64 a1)
{
__int64 i; // rbx
int result; // eax

for ( i = a1; i; i = *(_QWORD *)(i + 512) )
result = inotify_rm_watch(*(_DWORD *)(i + 500), *(_DWORD *)(i + 504));
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
__int64 i; // rbx
int result; // eax

for ( i = a1; i; i = *(_QWORD *)(i + 512) )
result = inotify_rm_watch(*(_DWORD *)(i + 500), *(_DWORD *)(i + 504));
return result;
}

original_code:
void RemoveWatcher(TargetToMonitor * Queue){
 TargetToMonitor * AuxQueue = Queue;
 while(AuxQueue){
  inotify_rm_watch(AuxQueue->InitHandle, AuxQueue->WatchHandle);
  AuxQueue = AuxQueue->Next;
 }
}

canonical_original_code:
void RemoveWatcher(struct STargetToMonitor *Queue){
 struct STargetToMonitor *AuxQueue = Queue;
 while(AuxQueue){
  inotify_rm_watch(AuxQueue->InitHandle, AuxQueue->WatchHandle);
  AuxQueue = AuxQueue->Next;
 }
}

user_defined_types: struct STargetToMonitor { char DictoryName[500]; int InitHandle; int WatchHandle; int Flags; struct STargetToMonitor *Next; }


InstallWatchers

decompiled_code:
__int64 __fastcall InstallWatchers(char *name)
{
char *v1; // rbx
int v2; // eax

v1 = name;
if ( !name )
return 1LL;
while ( 1 )
{
v2 = AddWatcher(v1);
if ( v2 == -1 )
_assert_fail("ReturnedValue != -1", "IoNotify.c", 0x24u, "InstallWatchers");
if ( v2 < 0 )
break;
v1 = (char *)*((_QWORD *)v1 + 64);
if ( !v1 )
return 1LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *name)
{
char *v1; // rbx
int v2; // eax

v1 = name;
if ( !name )
return 1LL;
while ( 1 )
{
v2 = func1(v1);
if ( v2 == -1 )
_assert_fail("ReturnedValue != -1", "IoNotify.c", 0x24u, "InstallWatchers");
if ( v2 < 0 )
break;
v1 = (char *)*((_QWORD *)v1 + 64);
if ( !v1 )
return 1LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int InstallWatchers(TargetToMonitor * Queue){
 TargetToMonitor * AuxQueue = Queue;
 int ReturnedValue;
 while(AuxQueue){
  ReturnedValue = AddWatcher(AuxQueue);
  ((void) sizeof ((ReturnedValue != -1) ? 1 : 0), __extension__ ({ if (ReturnedValue != -1) ; else __assert_fail ("ReturnedValue != -1", "IoNotify.c", 36, __extension__ __PRETTY_FUNCTION__); }));
  if(ReturnedValue < 0){
   return -1;
  }
  AuxQueue = AuxQueue->Next;
 }
 return 1;
}

canonical_original_code:
int InstallWatchers(struct STargetToMonitor *Queue){
 struct STargetToMonitor *AuxQueue = Queue;
 int ReturnedValue;
 while(AuxQueue){
  ReturnedValue = AddWatcher(AuxQueue);
  ((void) sizeof ((ReturnedValue != -1) ? 1 : 0), __extension__ ({ if (ReturnedValue != -1) ; else __assert_fail ("ReturnedValue != -1", "IoNotify.c", 36, __extension__ __PRETTY_FUNCTION__); }));
  if(ReturnedValue < 0){
   return -1;
  }
  AuxQueue = AuxQueue->Next;
 }
 return 1;
}

variable_types: ReturnedValue int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v5; // eax
_BYTE v6[500]; // [rsp+0h] [rbp-208h] BYREF
int v7; // [rsp+1F4h] [rbp-14h]
int v8; // [rsp+1F8h] [rbp-10h]

v7 = IoNotifyInit(argc, argv, envp);
if ( v7 < 0 )
printf("\n\tError");
v8 = 966;
if ( v7 >= 0 )
{
v3 = readFile(v6);
printQueueInfo(v3);
if ( (int)InstallWatchers(v3) >= 0 )
{
setbuf(stdout, 0LL);
while ( 1 )
{
setbuf(stdout, 0LL);
v5 = HasEvents((unsigned int)v7, 2LL);
if ( v5 < 0 )
break;
if ( v5 )
ProcessEvent(v6, v3);
sleep(2u);
}
}
}
return -1;
}

original_code:
int main(){
 int ret;
 TargetToMonitor *Target;
 Info Info;
 Info.InitHandle = IoNotifyInit();
 if(Info.InitHandle < 0)
 {
  printf("\n\tError");
 }
 Info.Flags = 0x00000100 | 0x00000200 | 0x00000002 | 0x00000004 | 0x00000040 | 0x00000080;
 if(Info.InitHandle < 0)
  return -1;
 Target = readFile(&Info);
 printQueueInfo(Target);
 ret = InstallWatchers(Target);
 if(ret < 0)
  return -1;
 setbuf(stdout, ((void *)0));
 while(1){
  setbuf(stdout, ((void *)0));
  ret = HasEvents(Info.InitHandle, 2);
  if (ret < 0)
   return -1;
  else if(ret)
   ProcessEvent(&Info, Target);
  sleep(2);
 }
 RemoveWatcher(Target);
    close( Info.InitHandle );
 return 1;
}

canonical_original_code:
int main(){
 int ret;
 struct STargetToMonitor *Target;
 struct SInfo Info;
 Info.InitHandle = IoNotifyInit();
 if(Info.InitHandle < 0)
 {
  printf("\n\tError");
 }
 Info.Flags = 0x00000100 | 0x00000200 | 0x00000002 | 0x00000004 | 0x00000040 | 0x00000080;
 if(Info.InitHandle < 0)
  return -1;
 Target = readFile(&Info);
 printQueueInfo(Target);
 ret = InstallWatchers(Target);
 if(ret < 0)
  return -1;
 setbuf(stdout, ((void *)0));
 while(1){
  setbuf(stdout, ((void *)0));
  ret = HasEvents(Info.InitHandle, 2);
  if (ret < 0)
   return -1;
  else if(ret)
   ProcessEvent(&Info, Target);
  sleep(2);
 }
 RemoveWatcher(Target);
    close( Info.InitHandle );
 return 1;
}

variable_types: ret int

variable_types: struct SInfo { char Directory[500]; int InitHandle; int Flags; }


CreateNode

decompiled_code:
void *CreateNode()
{
return malloc(0x208uLL);
}

canonical_decompiled_code:
void *func0()
{
return malloc(0x208uLL);
}

original_code:
TargetToMonitor * CreateNode(){
 TargetToMonitor * NewNode;
 NewNode = (TargetToMonitor *)malloc(sizeof(TargetToMonitor));
 if(!NewNode){
  return ((void *)0);
 }
 return NewNode;
}

canonical_original_code:
struct STargetToMonitor *CreateNode(){
 struct STargetToMonitor *NewNode;
 NewNode = (struct STargetToMonitor *)malloc(sizeof(TargetToMonitor));
 if(!NewNode){
  return ((void *)0);
 }
 return NewNode;
}

return_type: struct STargetToMonitor { char DictoryName[500]; int InitHandle; int WatchHandle; int Flags; struct STargetToMonitor *Next; } *


initData

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

result = 0xFFFFFFFFLL;
if ( a1 )
{
*(_QWORD *)(a1 + 512) = 0LL;
*(_DWORD *)(a1 + 508) = *(_DWORD *)(a2 + 504);
*(_DWORD *)(a1 + 500) = *(_DWORD *)(a2 + 500);
strcpy((char *)a1, (const char *)a2);
result = 1LL;
}
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a1 )
{
*(_QWORD *)(a1 + 512) = 0LL;
*(_DWORD *)(a1 + 508) = *(_DWORD *)(a2 + 504);
*(_DWORD *)(a1 + 500) = *(_DWORD *)(a2 + 500);
strcpy((char *)a1, (const char *)a2);
result = 1LL;
}
return result;
}

original_code:
int initData(TargetToMonitor * Node, Info * info){
 if(!Node)
  return -1;
 Node->Next = ((void *)0);
 Node->Flags = info->Flags;
 Node->InitHandle = info->InitHandle;
 strcpy(Node->DictoryName,info->Directory);
 return 1;
}

canonical_original_code:
int initData(struct STargetToMonitor *Node, struct SInfo *info){
 if(!Node)
  return -1;
 Node->Next = ((void *)0);
 Node->Flags = info->Flags;
 Node->InitHandle = info->InitHandle;
 strcpy(Node->DictoryName,info->Directory);
 return 1;
}

variable_types: struct SInfo { char Directory[500]; int InitHandle; int Flags; } *


AddNewNode

decompiled_code:
void *__fastcall AddNewNode(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
void *v4; // rax
void *v5; // rbp
void *v7; // rax

v2 = a1;
if ( a1 )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[64];
}
while ( v2 );
v4 = CreateNode();
v5 = v4;
v3[64] = v4;
if ( v4 )
{
initData((__int64)v4, a2);
v5 = a1;
}
}
else
{
v7 = CreateNode();
v5 = v7;
if ( v7 )
initData((__int64)v7, a2);
}
return v5;
}

original_code:
TargetToMonitor * AddNewNode(TargetToMonitor * QueueOfTargets, Info * info){
 TargetToMonitor * AuxTarget;
 if(!QueueOfTargets){
  AuxTarget = CreateNode();
  if(!AuxTarget)
   return ((void *)0);
  initData(AuxTarget, info);
  return AuxTarget;
 }
 AuxTarget = QueueOfTargets;
 while(AuxTarget->Next != ((void *)0))
  AuxTarget = AuxTarget->Next;
 AuxTarget->Next = CreateNode();
 AuxTarget = AuxTarget->Next;
 if(!AuxTarget)
  return ((void *)0);
 initData(AuxTarget, info);
 return QueueOfTargets;
}

canonical_original_code:
struct STargetToMonitor *AddNewNode(struct STargetToMonitor *QueueOfTargets, struct SInfo *info){
 struct STargetToMonitor *AuxTarget;
 if(!QueueOfTargets){
  AuxTarget = CreateNode();
  if(!AuxTarget)
   return ((void *)0);
  initData(AuxTarget, info);
  return AuxTarget;
 }
 AuxTarget = QueueOfTargets;
 while(AuxTarget->Next != ((void *)0))
  AuxTarget = AuxTarget->Next;
 AuxTarget->Next = CreateNode();
 AuxTarget = AuxTarget->Next;
 if(!AuxTarget)
  return ((void *)0);
 initData(AuxTarget, info);
 return QueueOfTargets;
}

variable_types:
QueueOfTargets
struct STargetToMonitor *

user_defined_types: struct SInfo { char Directory[500]; int InitHandle; int Flags; }


printQueueInfo

decompiled_code:
int __fastcall printQueueInfo(__int64 a1)
{
__int64 i; // rbx

for ( i = a1; i; i = *(_QWORD *)(i + 512) )
printf("\n\tDir Name: %s", (const char *)i);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 i; // rbx

for ( i = a1; i; i = *(_QWORD *)(i + 512) )
printf("\n\tDir Name: %s", (const char *)i);
return putchar(10);
}

original_code:
void printQueueInfo(TargetToMonitor * QueueOfTargets){
 TargetToMonitor * AuxTarget = QueueOfTargets;
 while (AuxTarget){
  printf("\n\tDir Name: %s", AuxTarget->DictoryName);
  AuxTarget = AuxTarget->Next;
 }
 printf("\n");
}

canonical_original_code:
void printQueueInfo(struct STargetToMonitor *QueueOfTargets){
 struct STargetToMonitor *AuxTarget = QueueOfTargets;
 while (AuxTarget){
  printf("\n\tDir Name: %s", AuxTarget->DictoryName);
  AuxTarget = AuxTarget->Next;
 }
 printf("\n");
}


HasEvents

decompiled_code:
int __fastcall HasEvents(int a1, int a2)
{
int v2; // er8
struct timeval v4; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v2 = a2;
memset(&readfds, 0, sizeof(readfds));
if ( a2 <= 0 )
v2 = 1;
v4.tv_sec = v2;
v4.tv_usec = 0LL;
readfds.fds_bits[a1 / 64] |= 1LL << (a1 % 64);
return select(1024, &readfds, 0LL, 0LL, &v4);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2)
{
int v2; // er8
struct timeval v4; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v2 = a2;
memset(&readfds, 0, sizeof(readfds));
if ( a2 <= 0 )
v2 = 1;
v4.tv_sec = v2;
v4.tv_usec = 0LL;
readfds.fds_bits[a1 / 64] |= 1LL << (a1 % 64);
return select(1024, &readfds, 0LL, 0LL, &v4);
}

original_code:
int HasEvents(int Handle, int TimeOut){
 fd_set rfds;
 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);
 struct timeval Time;
 Time.tv_sec = TimeOut < 1 ? 1 : TimeOut;
 Time.tv_usec = 0;
 ((void) (((&rfds)->__fds_bits)[((Handle) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((Handle) % (8 * (int) sizeof (__fd_mask)))))));
 return select (1024, &rfds, ((void *)0), ((void *)0), &Time);
}

canonical_original_code:
int HasEvents(int Handle, int TimeOut){
 struct fd_set rfds;
 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);
 struct timeval Time;
 Time.tv_sec = TimeOut < 1 ? 1 : TimeOut;
 Time.tv_usec = 0;
 ((void) (((&rfds)->__fds_bits)[((Handle) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((Handle) % (8 * (int) sizeof (__fd_mask)))))));
 return select (1024, &rfds, ((void *)0), ((void *)0), &Time);
}

variable_types: Handle int

variable_types: TimeOut int

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

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


GetTarget

decompiled_code:
__int64 __fastcall GetTarget(int a1, __int64 a2)
{
if ( !a2 || a1 < 0 )
return 0LL;
while ( *(_DWORD *)(a2 + 504) != a1 )
{
a2 = *(_QWORD *)(a2 + 512);
if ( !a2 )
return 0LL;
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, __int64 a2)
{
if ( !a2 || a1 < 0 )
return 0LL;
while ( *(_DWORD *)(a2 + 504) != a1 )
{
a2 = *(_QWORD *)(a2 + 512);
if ( !a2 )
return 0LL;
}
return a2;
}

original_code:
TargetToMonitor * GetTarget(int Descriptor, TargetToMonitor * Targets){
 if(!Targets || Descriptor < 0)
  return ((void *)0);
 TargetToMonitor * AuxTarget = Targets;
 while(AuxTarget){
  if(AuxTarget->WatchHandle == Descriptor)
   return AuxTarget;
  AuxTarget = AuxTarget->Next;
 }
 return ((void *)0);
}

canonical_original_code:
struct STargetToMonitor *GetTarget(int Descriptor, struct STargetToMonitor *Targets){
 if(!Targets || Descriptor < 0)
  return ((void *)0);
 struct STargetToMonitor *AuxTarget = Targets;
 while(AuxTarget){
  if(AuxTarget->WatchHandle == Descriptor)
   return AuxTarget;
  AuxTarget = AuxTarget->Next;
 }
 return ((void *)0);
}

variable_types: Descriptor int

variable_types:
Targets
struct STargetToMonitor *


TKDestroy

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

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

original_code:
void TKDestroy(TokenizerT *tk) {
 free(tk->sep);
 free(tk->string);
 free(tk);
}

canonical_original_code:
void TKDestroy(struct TokenizerT_ *tk) {
 free(tk->sep);
 free(tk->string);
 free(tk);
}

variable_types: struct TokenizerT_ { char *sep; char *string; char *currPos; } *


TKGetNextToken

decompiled_code:
char *__fastcall TKGetNextToken(const char **a1)
{
const char *v1; // r13
char *i; // rbx
const char *v3; // rbp
const char *v4; // rbp
char *v5; // r13
int v6; // er14
int v7; // er14

v1 = *a1;
for ( i = strpbrk(a1[2], *a1); i; i = strpbrk(i + 1, v1) )
{
v3 = a1[2];
if ( !v3[1] )
break;
if ( v3 != i )
{
v6 = strlen(a1[2]);
v7 = v6 - strlen(i);
v5 = (char *)malloc(v7 + 1);
strncpy(v5, v3, v7);
v5[v7] = 0;
a1[2] = i + 1;
return v5;
}
a1[2] = i + 1;
}
v4 = a1[2];
if ( !*v4 || v4 == i )
return 0LL;
v5 = strdup(a1[2]);
a1[2] = &v4[strlen(v4)];
return v5;
}

canonical_decompiled_code:
char *__fastcall func8(const char **a1)
{
const char *v1; // r13
char *i; // rbx
const char *v3; // rbp
const char *v4; // rbp
char *v5; // r13
int v6; // er14
int v7; // er14

v1 = *a1;
for ( i = strpbrk(a1[2], *a1); i; i = strpbrk(i + 1, v1) )
{
v3 = a1[2];
if ( !v3[1] )
break;
if ( v3 != i )
{
v6 = strlen(a1[2]);
v7 = v6 - strlen(i);
v5 = (char *)malloc(v7 + 1);
strncpy(v5, v3, v7);
v5[v7] = 0;
a1[2] = i + 1;
return v5;
}
a1[2] = i + 1;
}
v4 = a1[2];
if ( !*v4 || v4 == i )
return 0LL;
v5 = strdup(a1[2]);
a1[2] = &v4[strlen(v4)];
return v5;
}

original_code:
char *TKGetNextToken(TokenizerT *tk) {
 char* delim;
 char* token;
 int length;
 delim = strpbrk(tk->currPos,tk->sep);
 while(delim!=((void *)0) && tk->currPos[1]!='\0'){
  if(delim!=tk->currPos){
   length= (strlen(tk->currPos)-strlen(delim));
   token= (char*)malloc(length+1);
   strncpy(token,tk->currPos,length);
   token[length]='\0';
   tk->currPos = delim+1;
   return token;
   }
  else{
   tk->currPos = tk->currPos+1; delim = strpbrk(tk->currPos,
   tk->sep);
   }
  }
 if (tk->currPos[0]!='\0'&& tk->currPos!=delim){
  token = strdup(tk->currPos);
  tk->currPos=tk->currPos+strlen(tk->currPos);
  return token;
 }
  return 0;
}

canonical_original_code:
char *TKGetNextToken(struct TokenizerT_ *tk) {
 char *delim;
 char *token;
 int length;
 delim = strpbrk(tk->currPos,tk->sep);
 while(delim!=((void *)0) && tk->currPos[1]!='\0'){
  if(delim!=tk->currPos){
   length= (strlen(tk->currPos)-strlen(delim));
   token= (char *)malloc(length+1);
   strncpy(token,tk->currPos,length);
   token[length]='\0';
   tk->currPos = delim+1;
   return token;
   }
  else{
   tk->currPos = tk->currPos+1; delim = strpbrk(tk->currPos,
   tk->sep);
   }
  }
 if (tk->currPos[0]!='\0'&& tk->currPos!=delim){
  token = strdup(tk->currPos);
  tk->currPos=tk->currPos+strlen(tk->currPos);
  return token;
 }
  return 0;
}

variable_types:
delim
char *

variable_types:
token
char *

variable_types: length int

user_defined_types: struct TokenizerT_ { char *sep; char *string; char *currPos; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int result; // eax
char *v5; // rax
char *v6; // rbx

if ( argc == 3 )
{
v3 = (const char **)TKCreate((char *)argv[1], (char *)argv[2]);
while ( 1 )
{
v5 = TKGetNextToken(v3);
v6 = v5;
if ( !v5 )
break;
puts(v5);
free(v6);
}
TKDestroy(v3);
result = 0;
}
else
{
puts("Error: Invalid number of arguments!!!");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
int result; // eax
char *v5; // rax
char *v6; // rbx

if ( argc == 3 )
{
v3 = (const char **)TKCreate((char *)argv[1], (char *)argv[2]);
while ( 1 )
{
v5 = func8(v3);
v6 = v5;
if ( !v5 )
break;
puts(v5);
free(v6);
}
func7(v3);
result = 0;
}
else
{
puts("Error: Invalid number of arguments!!!");
result = -1;
}
return result;
}

original_code:
int main(int argc, char **argv){
 TokenizerT *tk;
 int size;
 char* token;
 if (argc!=3){
  puts("Error: Invalid number of arguments!!!");
  return -1;
 }
 size=strlen(argv[1]);
 tk = TKCreate(argv[1],argv[2]);
 do{
  token = TKGetNextToken(tk);
  if(token!=0){
   printf("%s\n",token);
   free(token);
  }
 }while(token!=0);
 TKDestroy(tk);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 struct TokenizerT_ *tk;
 int size;
 char *token;
 if (argc!=3){
  puts("Error: Invalid number of arguments!!!");
  return -1;
 }
 size=strlen(argv[1]);
 tk = TKCreate(argv[1],argv[2]);
 do{
  token = TKGetNextToken(tk);
  if(token!=0){
   printf("%s\n",token);
   free(token);
  }
 }while(token!=0);
 TKDestroy(tk);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: size int


decompiled_code:
__int64 init_net()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
int init_net()
{
 return 0;
}

canonical_original_code:
int init_net()
{
 return 0;
}


decompiled_code:
void cleanup_net()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void cleanup_net()
{
}

canonical_original_code:
void cleanup_net()
{
}


decompiled_code:
__int64 net_errno()
{
return (unsigned int)*_errno_location();
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)*_errno_location();
}

original_code:
int net_errno()
{
 return (*__errno_location ());
}


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

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

original_code:
int net_close(int s)
{
 return close(s);
}

variable_types: s int


decompiled_code:
int __fastcall net_ioctl(int a1, unsigned __int64 a2)
{
return ioctl(a1, a2);
}

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

original_code:
int net_ioctl(int s, long cmd, unsigned long* argp)
{
 return ioctl(s, cmd, argp);
}

canonical_original_code:
int net_ioctl(int s, long cmd, unsigned long *argp)
{
 return ioctl(s, cmd, argp);
}

variable_types: cmd long

variable_types:
argp
unsigned long *


msleep

decompiled_code:
int __fastcall msleep(__useconds_t a1)
{
return usleep(a1);
}

canonical_decompiled_code:
int __fastcall func0(__useconds_t a1)
{
return usleep(a1);
}

original_code:
void msleep(int microsec) {
 usleep((unsigned long) microsec);
}

variable_types: microsec int


decompiled_code:
__int64 device_init()
{
__int64 result; // rax
_QWORD *v1; // rdx

usb_find_busses();
usb_find_devices();
result = usb_get_busses();
v1 = (_QWORD *)result;
if ( result )
{
while ( 1 )
{
result = v1[515];
if ( result )
break;
LABEL_5:
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
while ( *(_DWORD *)(result + 4136) != 133523 )
{
result = *(_QWORD *)result;
if ( !result )
goto LABEL_5;
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
_QWORD *v1; // rdx

usb_find_busses();
usb_find_devices();
result = usb_get_busses();
v1 = (_QWORD *)result;
if ( result )
{
while ( 1 )
{
result = v1[515];
if ( result )
break;
LABEL_5:
v1 = (_QWORD *)*v1;
if ( !v1 )
return 0LL;
}
while ( *(_DWORD *)(result + 4136) != 133523 )
{
result = *(_QWORD *)result;
if ( !result )
goto LABEL_5;
}
}
return result;
}

original_code:
struct usb_device *device_init()
{
    struct usb_bus *usb_bus;
    struct usb_device *dev;
    usb_find_busses();
    usb_find_devices();
    for (usb_bus = usb_get_busses(); usb_bus; usb_bus = usb_bus->next) {
        for (dev = usb_bus->devices; dev; dev = dev->next)
            {
                if ((dev->descriptor.idVendor == 0x0993) &&
                    (dev->descriptor.idProduct == 0x0002))
                    {
                        return dev;
                    }
            }
    }
    return ((void *)0);
}

variable_types: struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *devices; unsigned int location; struct usb_device *root_dev; } *

variable_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device *devices; unsigned int location; struct usb_device *root_dev; } *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *

user_defined_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; }

user_defined_types: struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; int extralen; }

user_defined_types: struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }

user_defined_types: struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen; }


decompiled_code:
__int64 __fastcall parse_int(__int64 a1, int *a2)
{
__int64 result; // rax
int v3; // edx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

result = strtol((const char *)(a1 + *a2), &endptr, 10);
v3 = (_DWORD)endptr + *a2 - (a1 + *a2);
*a2 = v3;
if ( *(_BYTE *)(a1 + v3) == 44 )
*a2 = v3 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int *a2)
{
__int64 result; // rax
int v3; // edx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

result = strtol((const char *)(a1 + *a2), &endptr, 10);
v3 = (_DWORD)endptr + *a2 - (a1 + *a2);
*a2 = v3;
if ( *(_BYTE *)(a1 + v3) == 44 )
*a2 = v3 + 1;
return result;
}

original_code:
int parse_int(char *msg, int *offset)
{
    char *t;
    int i;
    i = strtol(&msg[*offset], &t, 10);
    *offset = *offset + (t-&msg[*offset]);
    if (msg[*offset]==',')
        *offset = *offset + 1;
    return i;
}

variable_types:
msg
char *

variable_types:
offset
int *

variable_types:
t
char *

variable_types: i int


decompiled_code:
void __fastcall cleanup_command(_QWORD *a1)
{
if ( a1 )
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1)
{
if ( a1 )
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
}
}

original_code:
void cleanup_command(struct reader_command *command)
{
    if (command == ((void *)0))
        return;
    memset(command, 0, sizeof(struct reader_command));
    command->cmd = r_NULL;
    command->ip1 = 0;
    command->ip2 = 0;
    command->n = 0;
    if (command->sp1 != ((void *)0)) {
        free(command->sp1);
        command->sp1 = ((void *)0);
    }
}

variable_types: struct reader_command { enum reader_command_t { r_NULL=0, r_gethostbyname=1, r_socket=2, r_connect=3, r_send=4, r_recv=5, r_close=6 } cmd; int n; char *sp1; int ip1; int ip2; } *

user_defined_types: struct reader_command { enum reader_command_t cmd; int n; char *sp1; int ip1; int ip2; }


repo: ebylaska/Simple-CPSD

decompiled_code:
void __fastcall set_Dirac_alpha(float a1)
{
int v1; // xmm0_4

*(float *)&v1 = a1 * 0.6666666666666666;
alpha = v1;
}

variable_types: aa float


decompiled_code:
float Dirac_alpha()
{
return *(float *)α
}

canonical_decompiled_code:
float func1()
{
return *(float *)α
}

original_code:
float Dirac_alpha()
{
   return alpha;
}

canonical_original_code:
float Dirac_alpha()
{
   return alpha;
}

return_type: float


variable_types: ispin int

variable_types:
rho
float []

variable_types:
vx
float []

variable_types:
ex
float []

variable_types: n float

variable_types: xi float

variable_types: f float

variable_types: df float