original_code:
static int
request_block(struct peer *peer, uint8_t hash[32])
{
 struct inv_msg *msg = xmalloc(sizeof(*msg) + sizeof(struct inv_vec));
 msg->count = 1;
 msg->inv_vec[0].type = INV_TYPE_BLOCK;
 memcpy(msg->inv_vec[0].hash, hash, 32);
 int error = write_getdata_msg(msg, peer->proto.conn);
 free(msg);
 return error;
}

canonical_original_code:
static int request_block(struct peer *peer, unsigned char hash[32])
{
 struct inv_msg *msg = xmalloc(sizeof(*msg) + sizeof(struct inv_vec));
 msg->count = 1;
 msg->inv_vec[0].type = INV_TYPE_BLOCK;
 memcpy(msg->inv_vec[0].hash, hash, 32);
 int error = write_getdata_msg(msg, peer->proto.conn);
 free(msg);
 return error;
}

variable_types:
hash
unsigned char [32]

variable_types: struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } *

user_defined_types: struct peer { enum peer_state state; int addr_family; struct sockaddr_in6 addr; struct version_msg version; struct peer *next; struct protocol proto; }

user_defined_types: struct inv_msg { enum msg_types type; unsigned long count; struct inv_vec inv_vec[]; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct version_msg { enum msg_types type; int version; unsigned long services; unsigned long timestamp; struct netaddr recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; }

user_defined_types: struct protocol { int conn; enum protocol_state state; struct buffer *buf; struct msg_hdr hdr; union message *msg; }

user_defined_types: union message { enum msg_types type; struct version_msg version; struct ping_msg ping; struct addr_msg addr; struct getheaders_msg getheaders; struct inv_msg inv; struct headers_msg headers; struct block_msg block; }

user_defined_types: struct ping_msg { enum msg_types type; unsigned long nonce; }

user_defined_types: struct addr_msg { enum msg_types type; unsigned long count; struct netaddr addr_list[]; }

user_defined_types: struct headers_msg { enum msg_types type; unsigned long count; struct block_hdr headers[]; }

user_defined_types: struct block_msg { enum msg_types type; struct block_hdr hdr; struct tx_msg tx[]; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
int v5; // ebx
__pid_t v6; // eax
__int64 v7; // rbx
int result; // eax
int *v9; // rax
int *v10; // rax
int v11; // eax
unsigned int v12; // eax
unsigned int *v13; // [rsp+8h] [rbp-30h] BYREF
struct epoll_event events; // [rsp+14h] [rbp-24h] BYREF

openlog("btcd", 41, 8);
v3 = epoll_create(1);
if ( v3 == -1 )
{
v9 = _errno_location();
syslog(3, "epoll_create failed, errno %d", (unsigned int)*v9);
result = 1;
}
else
{
v4 = v3;
v5 = time(0LL);
v6 = getpid();
srand(v5 ^ v6);
v7 = discover_peers();
if ( v7 )
{
do
{
if ( !(unsigned int)peer_connect(v7) )
{
epoll_add(v4, (epoll_data_t)v7);
while ( 1 )
{
do
{
v11 = epoll_wait(v4, &events, 1, -1);
if ( v11 == -1 )
{
v10 = _errno_location();
syslog(3, "epoll_wait failed, errno %d", (unsigned int)*v10);
exit(1);
}
}
while ( !v11 );
v13 = 0LL;
v12 = peer_read_message(v7, &v13);
if ( v12 )
break;
if ( v13 )
handle_message(v7, v13);
}
syslog(3, "%s: failed to read a message from peer, return code %d", "main", v12);
free_msg(v13);
epoll_del(v4, v7);
close(*(_DWORD *)(v7 + 424));
}
v7 = *(_QWORD *)(v7 + 416);
}
while ( v7 );
result = 0;
}
else
{
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
int v5; // ebx
__pid_t v6; // eax
__int64 v7; // rbx
int result; // eax
int *v9; // rax
int *v10; // rax
int v11; // eax
unsigned int v12; // eax
unsigned int *v13; // [rsp+8h] [rbp-30h] BYREF
struct epoll_event events; // [rsp+14h] [rbp-24h] BYREF

openlog("btcd", 41, 8);
v3 = epoll_create(1);
if ( v3 == -1 )
{
v9 = _errno_location();
syslog(3, "epoll_create failed, errno %d", (unsigned int)*v9);
result = 1;
}
else
{
v4 = v3;
v5 = time(0LL);
v6 = getpid();
srand(v5 ^ v6);
v7 = discover_peers();
if ( v7 )
{
do
{
if ( !(unsigned int)peer_connect(v7) )
{
func1(v4, (epoll_data_t)v7);
while ( 1 )
{
do
{
v11 = epoll_wait(v4, &events, 1, -1);
if ( v11 == -1 )
{
v10 = _errno_location();
syslog(3, "epoll_wait failed, errno %d", (unsigned int)*v10);
exit(1);
}
}
while ( !v11 );
v13 = 0LL;
v12 = peer_read_message(v7, &v13);
if ( v12 )
break;
if ( v13 )
func8(v7, v13);
}
syslog(3, "%s: failed to read a message from peer, return code %d", "main", v12);
free_msg(v13);
func2(v4, v7);
close(*(_DWORD *)(v7 + 424));
}
v7 = *(_QWORD *)(v7 + 416);
}
while ( v7 );
result = 0;
}
else
{
result = 0;
}
}
return result;
}

original_code:
int
main(int argc, char **argv)
{
 (void)argc;
 (void)argv;
 openlog("btcd", 0x01 | 0x08 | 0x20, (1<<3));
 int epfd = epoll_create(1);
 if (epfd == -1) {
  syslog(3, "epoll_create failed, errno %d", (*__errno_location ()));
  return 1;
 }
 srand(time(((void *)0)) ^ getpid());
 struct peer *peers = discover_peers();
 for (struct peer *peer = peers; peer != ((void *)0); peer = peer->next) {
  int error = peer_connect(peer);
  if (error) {
   continue;
  }
  epoll_add(epfd, peer);
  for (;;) {
   struct epoll_event ev;
   int rc = epoll_wait(epfd, &ev, 1, -1);
   switch (rc) {
   case -1:
    syslog(3, "epoll_wait failed, errno %d", (*__errno_location ()));
    exit(1);
   case 0:
    continue;
   }
   union message *msg = ((void *)0);
   error = peer_read_message(peer, &msg);
   if (error) {
    syslog(3, "%s: failed to read a message from peer, return code %d", __func__, error);
    free_msg(msg);
    epoll_del(epfd, peer);
    close(peer->proto.conn);
    break;
   }
   if (msg != ((void *)0))
    handle_message(peer, msg);
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: epfd int

variable_types: struct peer { enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 } state; int addr_family; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr; struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; } version; struct peer *next; struct protocol { int conn; enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 } state; struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; } *buf; struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; } hdr; union message { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct version_msg version; struct ping_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long nonce; } ping; struct addr_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct netaddr addr_list[]; } addr; struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; } getheaders; struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } inv; struct headers_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; } headers[]; } headers; struct block_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct block_hdr hdr; struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input { struct outpoint { unsigned char hash[32]; unsigned int index; } prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; } **in; unsigned long out_count; struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; } **out; unsigned int locktime; } tx[]; } block; } *msg; } proto; } *

variable_types: error int

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

variable_types: rc int

variable_types: union message { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; } version; struct ping_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long nonce; } ping; struct addr_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct netaddr addr_list[]; } addr; struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; } getheaders; struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } inv; struct headers_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; } headers[]; } headers; struct block_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct block_hdr hdr; struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input { struct outpoint { unsigned char hash[32]; unsigned int index; } prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; } **in; unsigned long out_count; struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; } **out; unsigned int locktime; } tx[]; } block; } *

return_type: int

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct getheaders_msg { enum msg_types type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; }

user_defined_types: struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input **in; unsigned long out_count; struct tx_output **out; unsigned int locktime; }

user_defined_types: struct tx_input { struct outpoint prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; }


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, __int64 a2)
{
__int64 i; // rbx

fflush(stdout);
if ( !a2 )
goto LABEL_15;
for ( i = 0LL; ; ++i )
{
if ( !i )
goto LABEL_4;
if ( (i & 0xF) == 0 )
{
putchar(10);
LABEL_4:
printf("0x%08zx| ", i);
goto LABEL_5;
}
if ( (i & 7) != 0 )
{
if ( (i & 3) == 0 )
putchar(32);
}
else
{
printf(" ");
}
LABEL_5:
printf("%02x", *(unsigned __int8 *)(a1 + i));
if ( a2 == i + 1 )
break;
}
if ( (i & 0xF) != 0 )
LABEL_15:
putchar(10);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
__int64 i; // rbx

fflush(stdout);
if ( !a2 )
goto LABEL_15;
for ( i = 0LL; ; ++i )
{
if ( !i )
goto LABEL_4;
if ( (i & 0xF) == 0 )
{
putchar(10);
LABEL_4:
printf("0x%08zx| ", i);
goto LABEL_5;
}
if ( (i & 7) != 0 )
{
if ( (i & 3) == 0 )
putchar(32);
}
else
{
printf(" ");
}
LABEL_5:
printf("%02x", *(unsigned __int8 *)(a1 + i));
if ( a2 == i + 1 )
break;
}
if ( (i & 0xF) != 0 )
LABEL_15:
putchar(10);
return fflush(stdout);
}

original_code:
void
hexdump(const void *ptr, size_t size)
{
 const uint8_t *buf = ptr;
 size_t i;
 fflush(stdout);
 for (i = 0; i < size; ++i) {
  if (i > 0 && (i % 16) == 0)
   putchar('\n');
  else if (i > 0 && (i % 8) == 0)
   printf("  ");
  else if (i > 0 && (i % 4) == 0)
   putchar(' ');
  if (i == 0 || (i % 16) == 0)
   printf("0x%08zx| ", i);
  printf("%02x", buf[i]);
 }
 if (((i - 1) % 16) != 0)
  putchar('\n');
 fflush(stdout);
}

canonical_original_code:
void hexdump(void *ptr, unsigned long size)
{
 unsigned char *buf = ptr;
 unsigned long i;
 fflush(stdout);
 for (i = 0; i < size; ++i) {
  if (i > 0 && (i % 16) == 0)
   putchar('\n');
  else if (i > 0 && (i % 8) == 0)
   printf("  ");
  else if (i > 0 && (i % 4) == 0)
   putchar(' ');
  if (i == 0 || (i % 16) == 0)
   printf("0x%08zx| ", i);
  printf("%02x", buf[i]);
 }
 if (((i - 1) % 16) != 0)
  putchar('\n');
 fflush(stdout);
}

variable_types:
ptr
void *

variable_types:
size
unsigned long

variable_types:
buf
unsigned char *

variable_types:
i
unsigned long

return_type: void


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

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

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

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void
write_log(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void write_log(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
}

variable_types:
fmt
char *


decompiled_code:
const char *__fastcall str_state(__int16 a1)
{
const char *result; // rax

result = "TIOCM_LE";
if ( (a1 & 1) == 0 )
{
result = "TIOCM_DTR";
if ( (a1 & 2) == 0 )
{
result = "TIOCM_RTS";
if ( (a1 & 4) == 0 )
{
result = "TIOCM_ST";
if ( (a1 & 8) == 0 )
{
result = "TIOCM_SR";
if ( (a1 & 0x10) == 0 )
{
result = "TIOCM_CTS";
if ( (a1 & 0x20) == 0 )
{
result = "TIOCM_CAR";
if ( (a1 & 0x40) == 0 )
{
result = "TIOCM_RNG";
if ( (a1 & 0x80u) == 0 )
{
result = "(unknown)";
if ( (a1 & 0x100) != 0 )
result = "TIOCM_DSR";
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(__int16 a1)
{
const char *result; // rax

result = "TIOCM_LE";
if ( (a1 & 1) == 0 )
{
result = "TIOCM_DTR";
if ( (a1 & 2) == 0 )
{
result = "TIOCM_RTS";
if ( (a1 & 4) == 0 )
{
result = "TIOCM_ST";
if ( (a1 & 8) == 0 )
{
result = "TIOCM_SR";
if ( (a1 & 0x10) == 0 )
{
result = "TIOCM_CTS";
if ( (a1 & 0x20) == 0 )
{
result = "TIOCM_CAR";
if ( (a1 & 0x40) == 0 )
{
result = "TIOCM_RNG";
if ( (a1 & 0x80u) == 0 )
{
result = "(unknown)";
if ( (a1 & 0x100) != 0 )
result = "TIOCM_DSR";
}
}
}
}
}
}
}
}
return result;
}

original_code:
const char *
str_state(int state)
{
 if (state & 0x001) { return "TIOCM_LE"; };
 if (state & 0x002) { return "TIOCM_DTR"; };
 if (state & 0x004) { return "TIOCM_RTS"; };
 if (state & 0x008) { return "TIOCM_ST"; };
 if (state & 0x010) { return "TIOCM_SR"; };
 if (state & 0x020) { return "TIOCM_CTS"; };
 if (state & 0x040) { return "TIOCM_CAR"; };
 if (state & 0x080) { return "TIOCM_RNG"; };
 if (state & 0x100) { return "TIOCM_DSR"; };
 return "(unknown)";
}

canonical_original_code:
char *str_state(int state)
{
 if (state & 0x001) { return "TIOCM_LE"; };
 if (state & 0x002) { return "TIOCM_DTR"; };
 if (state & 0x004) { return "TIOCM_RTS"; };
 if (state & 0x008) { return "TIOCM_ST"; };
 if (state & 0x010) { return "TIOCM_SR"; };
 if (state & 0x020) { return "TIOCM_CTS"; };
 if (state & 0x040) { return "TIOCM_CAR"; };
 if (state & 0x080) { return "TIOCM_RNG"; };
 if (state & 0x100) { return "TIOCM_DSR"; };
 return "(unknown)";
}

variable_types: state int

return_type: char *


decompiled_code:
int __fastcall print_line_state(int a1)
{
unsigned int v1; // ebx
const char *v2; // rax
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( ioctl(a1, 0x5415uLL, v4) )
err(1, "ioctl TIOCMGET");
v1 = v4[0];
v2 = str_state(v4[0]);
return write_log("Line state: %s (0x%x)\n", v2, v1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
unsigned int v1; // ebx
const char *v2; // rax
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( ioctl(a1, 0x5415uLL, v4) )
err(1, "ioctl TIOCMGET");
v1 = v4[0];
v2 = func1(v4[0]);
return func0("Line state: %s (0x%x)\n", v2, v1);
}

original_code:
void
print_line_state(int f)
{
 int error;
 int state;
 error = ioctl(f, 0x5415, &state);
 if (error)
  err(1, "ioctl TIOCMGET");
 write_log("Line state: %s (0x%x)\n", str_state(state), (unsigned)state);
}

canonical_original_code:
void print_line_state(int f)
{
 int error;
 int state;
 error = ioctl(f, 0x5415, &state);
 if (error)
  err(1, "ioctl TIOCMGET");
 write_log("Line state: %s (0x%x)\n", str_state(state), (unsigned int)state);
}

variable_types: f int


decompiled_code:
int __fastcall print_baud_rate(int a1)
{
speed_t v1; // ebx
speed_t v2; // eax
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(a1, &v4) )
err(1, "tcgetattr");
v1 = cfgetispeed(&v4);
v2 = cfgetospeed(&v4);
return write_log("Line speed: %u input, %u output\n", v1, v2);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
speed_t v1; // ebx
speed_t v2; // eax
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(a1, &v4) )
err(1, "tcgetattr");
v1 = cfgetispeed(&v4);
v2 = cfgetospeed(&v4);
return func0("Line speed: %u input, %u output\n", v1, v2);
}

original_code:
void
print_baud_rate(int f)
{
 int error;
 struct termios t;
 speed_t ispeed;
 speed_t ospeed;
 error = tcgetattr(f, &t);
 if (error)
  err(1, "tcgetattr");
 ispeed = cfgetispeed(&t);
 ospeed = cfgetospeed(&t);
 write_log("Line speed: %u input, %u output\n", ispeed, ospeed);
}

canonical_original_code:
void print_baud_rate(int f)
{
 int error;
 struct termios t;
 unsigned int ispeed;
 unsigned int ospeed;
 error = tcgetattr(f, &t);
 if (error)
  err(1, "tcgetattr");
 ispeed = cfgetispeed(&t);
 ospeed = cfgetospeed(&t);
 write_log("Line speed: %u input, %u output\n", ispeed, ospeed);
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types:
ispeed
unsigned int

variable_types:
ospeed
unsigned int


decompiled_code:
int __fastcall set_line_state(int fd, int a2)
{
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( ioctl(fd, 0x5415uLL, v3) )
err(1, "ioctl TIOCMGET");
v3[0] |= a2;
if ( ioctl(fd, 0x5418uLL, v3) )
err(1, "ioctl TIOCMSET");
return print_line_state(fd);
}

canonical_decompiled_code:
int __fastcall func4(int fd, int a2)
{
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( ioctl(fd, 0x5415uLL, v3) )
err(1, "ioctl TIOCMGET");
v3[0] |= a2;
if ( ioctl(fd, 0x5418uLL, v3) )
err(1, "ioctl TIOCMSET");
return func2(fd);
}

original_code:
void
set_line_state(int f, int new_state)
{
 int error;
 int state;
 error = ioctl(f, 0x5415, &state);
 if (error)
  err(1, "ioctl TIOCMGET");
 state |= new_state;
 error = ioctl(f, 0x5418, &state);
 if (error)
  err(1, "ioctl TIOCMSET");
 print_line_state(f);
}


decompiled_code:
int __fastcall clear_line_state(int fd, int a2)
{
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( ioctl(fd, 0x5415uLL, v3) )
err(1, "ioctl TIOCMGET");
v3[0] &= ~a2;
if ( ioctl(fd, 0x5418uLL, v3) )
err(1, "ioctl TIOCMSET");
return print_line_state(fd);
}

canonical_decompiled_code:
int __fastcall func5(int fd, int a2)
{
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( ioctl(fd, 0x5415uLL, v3) )
err(1, "ioctl TIOCMGET");
v3[0] &= ~a2;
if ( ioctl(fd, 0x5418uLL, v3) )
err(1, "ioctl TIOCMSET");
return func2(fd);
}

original_code:
void
clear_line_state(int f, int bits)
{
 int error;
 int state;
 error = ioctl(f, 0x5415, &state);
 if (error)
  err(1, "ioctl TIOCMGET");
 state &= ~bits;
 error = ioctl(f, 0x5418, &state);
 if (error)
  err(1, "ioctl TIOCMSET");
 print_line_state(f);
}

variable_types: bits int


decompiled_code:
int __fastcall set_baud_rate(int a1, speed_t a2)
{
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

if ( tcgetattr(a1, &v3) )
err(1, "tcgetattr");
cfsetspeed(&v3, a2);
return print_baud_rate(a1);
}

canonical_decompiled_code:
int __fastcall func6(int a1, speed_t a2)
{
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

if ( tcgetattr(a1, &v3) )
err(1, "tcgetattr");
cfsetspeed(&v3, a2);
return func3(a1);
}

original_code:
void
set_baud_rate(int f, speed_t rate)
{
 int error;
 struct termios t;
 error = tcgetattr(f, &t);
 if (error)
  err(1, "tcgetattr");
 cfsetspeed(&t, rate);
 print_baud_rate(f);
}

canonical_original_code:
void set_baud_rate(int f, unsigned int rate)
{
 int error;
 struct termios t;
 error = tcgetattr(f, &t);
 if (error)
  err(1, "tcgetattr");
 cfsetspeed(&t, rate);
 print_baud_rate(f);
}

variable_types:
rate
unsigned int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int __fastcall set_raw_io(int fd)
{
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(fd, &v2) )
err(1, "tcgetattr");
cfmakeraw(&v2);
if ( tcsetattr(fd, 2, &v2) )
err(1, "tcsetattr");
return write_log("Set raw I/O mode.\n");
}

canonical_decompiled_code:
int __fastcall func7(int fd)
{
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

if ( tcgetattr(fd, &v2) )
err(1, "tcgetattr");
cfmakeraw(&v2);
if ( tcsetattr(fd, 2, &v2) )
err(1, "tcsetattr");
return func0("Set raw I/O mode.\n");
}

original_code:
void
set_raw_io(int f)
{
 int error;
 struct termios t;
 error = tcgetattr(f, &t);
 if (error)
  err(1, "tcgetattr");
 cfmakeraw(&t);
 error = tcsetattr(f, 2, &t);
 if (error)
  err(1, "tcsetattr");
 write_log("Set raw I/O mode.\n");
}


decompiled_code:
int __fastcall set_nonblock(int a1)
{
if ( fcntl(a1, 4, 2048LL) )
err(1, "fcntl F_SETFL O_NONBLOCK");
return write_log("Set non-blocking I/O mode\n");
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
if ( fcntl(a1, 4, 2048LL) )
err(1, "fcntl F_SETFL O_NONBLOCK");
return func0("Set non-blocking I/O mode\n");
}

original_code:
void
set_nonblock(int f)
{
 int error;
 error = fcntl(f, 4, 04000);
 if (error)
  err(1, "fcntl F_SETFL O_NONBLOCK");
 write_log("Set non-blocking I/O mode\n");
}


decompiled_code:
int __fastcall print_byte(unsigned __int8 a1)
{
putchar(hex_2[a1 >> 4]);
return putchar(hex_2[a1 & 0xF]);
}

canonical_decompiled_code:
int __fastcall func9(unsigned __int8 a1)
{
putchar(hex_2[a1 >> 4]);
return putchar(hex_2[a1 & 0xF]);
}

original_code:
void
print_byte(uint8_t byte)
{
 static const char hex[] = "0123456789abcdef";
 putchar(hex[byte >> 4]);
 putchar(hex[byte & 0xf]);
}

canonical_original_code:
void print_byte(unsigned char byte)
{
 static char hex[] = "0123456789abcdef";
 putchar(hex[byte >> 4]);
 putchar(hex[byte & 0xf]);
}

variable_types:
byte
unsigned char

variable_types:
hex
char []


decompiled_code:
int __fastcall flush_queues(int a1)
{
int result; // eax

result = tcflush(a1, 2);
if ( result )
err(1, "ioctl TIOCFLUSH");
return result;
}

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

result = tcflush(a1, 2);
if ( result )
err(1, "ioctl TIOCFLUSH");
return result;
}

original_code:
void
flush_queues(int f)
{
 int error;
 error = tcflush(f, 2);
 if (error)
  err(1, "ioctl TIOCFLUSH");
}

variable_types: error int


decompiled_code:
int __fastcall sleep_for_ms(unsigned int a1)
{
int *v1; // rax
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

requested_time.tv_sec = a1 / 0x3E8;
requested_time.tv_nsec = 1000000 * (a1 % 0x3E8);
do
{
LODWORD(v1) = nanosleep(&requested_time, &v3);
requested_time = (struct timespec)_mm_loadu_si128((const __m128i *)&v3);
if ( (_DWORD)v1 != -1 )
break;
v1 = _errno_location();
}
while ( *v1 == 4 );
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1)
{
int *v1; // rax
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

requested_time.tv_sec = a1 / 0x3E8;
requested_time.tv_nsec = 1000000 * (a1 % 0x3E8);
do
{
LODWORD(v1) = nanosleep(&requested_time, &v3);
requested_time = (struct timespec)_mm_loadu_si128((const __m128i *)&v3);
if ( (_DWORD)v1 != -1 )
break;
v1 = _errno_location();
}
while ( *v1 == 4 );
return (int)v1;
}

original_code:
void
sleep_for_ms(unsigned ms)
{
 struct timespec t;
 struct timespec r;
 int rc;
 t.tv_sec = ms / 1000;
 t.tv_nsec = (ms % 1000) * 1000 * 1000;
 do {
  rc = nanosleep(&t, &r);
  t = r;
 } while (rc == -1 && (*__errno_location ()) == 4);
}

canonical_original_code:
void sleep_for_ms(unsigned int ms)
{
 struct timespec t;
 struct timespec r;
 int rc;
 t.tv_sec = ms / 1000;
 t.tv_nsec = (ms % 1000) * 1000 * 1000;
 do {
  rc = nanosleep(&t, &r);
  t = r;
 } while (rc == -1 && (*__errno_location ()) == 4);
}

variable_types:
ms
unsigned int

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

variable_types: rc int


decompiled_code:
ssize_t __fastcall sync_read(int fd, __int64 a2, unsigned __int64 a3, int a4)
{
__int64 v6; // rbp
__int64 v7; // rbx
int v8; // eax
ssize_t result; // rax
struct pollfd fds; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
fds.fd = fd;
fds.events = 1;
fds.revents = 0;
v8 = poll(&fds, 1uLL, a4);
if ( v8 < 0 )
err(1, "poll");
if ( !v8 )
{
warnx("%s: poll timeout", "sync_read");
goto LABEL_10;
}
result = read(fd, (void *)(a2 + v7), a3 - v7);
if ( result < 0 )
break;
v6 += result;
v7 = v6;
if ( v6 >= a3 )
goto LABEL_10;
}
if ( v6 > 0 )
result = (unsigned int)v6;
}
else
{
LODWORD(v6) = 0;
LABEL_10:
result = (unsigned int)v6;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(int fd, __int64 a2, unsigned __int64 a3, int a4)
{
__int64 v6; // rbp
__int64 v7; // rbx
int v8; // eax
ssize_t result; // rax
struct pollfd fds; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
fds.fd = fd;
fds.events = 1;
fds.revents = 0;
v8 = poll(&fds, 1uLL, a4);
if ( v8 < 0 )
err(1, "poll");
if ( !v8 )
{
warnx("%s: poll timeout", "sync_read");
goto LABEL_10;
}
result = read(fd, (void *)(a2 + v7), a3 - v7);
if ( result < 0 )
break;
v6 += result;
v7 = v6;
if ( v6 >= a3 )
goto LABEL_10;
}
if ( v6 > 0 )
result = (unsigned int)v6;
}
else
{
LODWORD(v6) = 0;
LABEL_10:
result = (unsigned int)v6;
}
return result;
}

original_code:
int
sync_read(int f, void *buf, size_t size, int timeout_ms)
{
 ssize_t n;
 ssize_t off;
 for (n = off = 0; (size_t)off < size; off += n) {
  struct pollfd pfd = { f, 0x001, 0 };
  int rc = poll(&pfd, 1, timeout_ms);
  if (rc < 0)
   err(1, "poll");
  if (rc == 0) {
   warnx("%s: poll timeout", __func__);
   break;
  }
  n = read(f, (uint8_t *)buf + off, size - off);
  if (n < 0)
   return off > 0 ? off : n;
 }
 return off;
}

canonical_original_code:
int sync_read(int f, void *buf, unsigned long size, int timeout_ms)
{
 long n;
 long off;
 for (n = off = 0; (unsigned long)off < size; off += n) {
  struct pollfd pfd = { f, 0x001, 0 };
  int rc = poll(&pfd, 1, timeout_ms);
  if (rc < 0)
   err(1, "poll");
  if (rc == 0) {
   warnx("%s: poll timeout", __func__);
   break;
  }
  n = read(f, (unsigned char *)buf + off, size - off);
  if (n < 0)
   return off > 0 ? off : n;
 }
 return off;
}

variable_types:
buf
void *

variable_types:
size
unsigned long

variable_types: n long

variable_types: off long

variable_types: struct pollfd { int fd; short events; short revents; }


original_code:
const char *
str_nibble(uint8_t nibble)
{
 static char bits[] = "0000";
 bits[0] = '0' + !!(nibble & 8);
 bits[1] = '0' + !!(nibble & 4);
 bits[2] = '0' + !!(nibble & 2);
 bits[3] = '0' + !!(nibble & 1);
 return bits;
}

canonical_original_code:
char *str_nibble(unsigned char nibble)
{
 static char bits[] = "0000";
 bits[0] = '0' + !!(nibble & 8);
 bits[1] = '0' + !!(nibble & 4);
 bits[2] = '0' + !!(nibble & 2);
 bits[3] = '0' + !!(nibble & 1);
 return bits;
}

variable_types:
nibble
unsigned char

variable_types:
bits
char []


decompiled_code:
int __fastcall read_decode_atr(int fd)
{
int v1; // ebp
const char *v2; // rsi
unsigned int v3; // er12
int v4; // ebp
unsigned int v5; // er15
char v6; // bl
char *v7; // rax
int result; // eax
char v9; // r15
char v10; // r14
char *v11; // rax
int i; // ebx
unsigned int v13; // er12
char v14; // bl
int v15; // [rsp+Ch] [rbp-4Ch]
char v16[57]; // [rsp+1Fh] [rbp-39h] BYREF

if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TS_%d character", 0LL);
v1 = (unsigned __int8)v16[0];
printf(" TS=%02x", (unsigned __int8)v16[0]);
v2 = "direct";
if ( v16[0] != 59 )
{
v2 = "inverse";
if ( v16[0] != 63 )
v2 = "unknown";
}
printf(": %s convention\n", v2);
if ( v16[0] != 59 )
errx(1, "unsupported convention");
v3 = sync_read(fd, (__int64)v16, 1uLL, 100);
if ( v3 != 1 )
errx(1, "failed to read T0_%d character", 0LL);
v4 = (unsigned __int8)v16[0] ^ v1 ^ 0x3B;
printf(" T0=%02x", (unsigned __int8)v16[0]);
v5 = v16[0] & 0xF;
v15 = v5;
v6 = v16[0] & 0xF0;
v7 = str_nibble((unsigned __int8)v16[0] >> 4);
result = printf(": Y_1=0b%s, K=%u\n", v7, v5);
if ( v6 )
{
v9 = 0;
do
{
if ( (v6 & 0x10) != 0 )
{
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TA_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TA_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TA=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( (v6 & 0x20) != 0 )
{
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TB_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TB_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TB=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( (v6 & 0x40) != 0 )
{
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TC_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TC_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TC=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( v6 >= 0 )
break;
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TD_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TD_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TD=%02x", (unsigned __int8)v16[0]);
v10 = v16[0];
v6 = v16[0] & 0xF0;
v11 = str_nibble((unsigned __int8)v16[0] >> 4);
printf(": Y=0b%s, T=%d\n", v11, v10 & 0xF);
result = 1;
if ( (v16[0] & 0xF) != 0 )
v9 = 1;
++v3;
}
while ( v6 );
if ( v15 > 0 )
goto LABEL_41;
LABEL_50:
if ( v9 )
{
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TCK_%d character", 0LL);
v14 = v16[0];
printf(" TCK=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
if ( v14 != (_BYTE)v4 )
errx(1, "TCK check failed");
}
}
else
{
v9 = 0;
if ( v15 > 0 )
{
LABEL_41:
for ( i = 2; ; ++i )
{
v13 = i - 1;
if ( (unsigned int)sync_read(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read T_%d character", v13);
LOBYTE(v4) = v16[0] ^ v4;
if ( i == 1 )
{
printf(" T=%02x", (unsigned __int8)v16[0]);
putchar(10);
}
else
{
printf(" T_%d=%02x", v13, (unsigned __int8)v16[0]);
result = putchar(10);
if ( v15 < i )
goto LABEL_50;
}
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func14(int fd)
{
int v1; // ebp
const char *v2; // rsi
unsigned int v3; // er12
int v4; // ebp
unsigned int v5; // er15
char v6; // bl
char *v7; // rax
int result; // eax
char v9; // r15
char v10; // r14
char *v11; // rax
int i; // ebx
unsigned int v13; // er12
char v14; // bl
int v15; // [rsp+Ch] [rbp-4Ch]
char v16[57]; // [rsp+1Fh] [rbp-39h] BYREF

if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TS_%d character", 0LL);
v1 = (unsigned __int8)v16[0];
printf(" TS=%02x", (unsigned __int8)v16[0]);
v2 = "direct";
if ( v16[0] != 59 )
{
v2 = "inverse";
if ( v16[0] != 63 )
v2 = "unknown";
}
printf(": %s convention\n", v2);
if ( v16[0] != 59 )
errx(1, "unsupported convention");
v3 = func12(fd, (__int64)v16, 1uLL, 100);
if ( v3 != 1 )
errx(1, "failed to read T0_%d character", 0LL);
v4 = (unsigned __int8)v16[0] ^ v1 ^ 0x3B;
printf(" T0=%02x", (unsigned __int8)v16[0]);
v5 = v16[0] & 0xF;
v15 = v5;
v6 = v16[0] & 0xF0;
v7 = func13((unsigned __int8)v16[0] >> 4);
result = printf(": Y_1=0b%s, K=%u\n", v7, v5);
if ( v6 )
{
v9 = 0;
do
{
if ( (v6 & 0x10) != 0 )
{
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TA_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TA_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TA=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( (v6 & 0x20) != 0 )
{
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TB_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TB_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TB=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( (v6 & 0x40) != 0 )
{
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TC_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TC_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TC=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
}
if ( v6 >= 0 )
break;
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TD_%d character", v3);
v4 ^= (unsigned __int8)v16[0];
if ( v3 )
printf(" TD_%d=%02x", v3, (unsigned __int8)v16[0]);
else
printf(" TD=%02x", (unsigned __int8)v16[0]);
v10 = v16[0];
v6 = v16[0] & 0xF0;
v11 = func13((unsigned __int8)v16[0] >> 4);
printf(": Y=0b%s, T=%d\n", v11, v10 & 0xF);
result = 1;
if ( (v16[0] & 0xF) != 0 )
v9 = 1;
++v3;
}
while ( v6 );
if ( v15 > 0 )
goto LABEL_41;
LABEL_50:
if ( v9 )
{
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read TCK_%d character", 0LL);
v14 = v16[0];
printf(" TCK=%02x", (unsigned __int8)v16[0]);
result = putchar(10);
if ( v14 != (_BYTE)v4 )
errx(1, "TCK check failed");
}
}
else
{
v9 = 0;
if ( v15 > 0 )
{
LABEL_41:
for ( i = 2; ; ++i )
{
v13 = i - 1;
if ( (unsigned int)func12(fd, (__int64)v16, 1uLL, 100) != 1 )
errx(1, "failed to read T_%d character", v13);
LOBYTE(v4) = v16[0] ^ v4;
if ( i == 1 )
{
printf(" T=%02x", (unsigned __int8)v16[0]);
putchar(10);
}
else
{
printf(" T_%d=%02x", v13, (unsigned __int8)v16[0]);
result = putchar(10);
if ( v15 < i )
goto LABEL_50;
}
}
}
}
return result;
}

original_code:
void
read_decode_atr(int f)
{
 const int max_read_time = 100;
 uint8_t ch;
 uint8_t Y;
 uint8_t K;
 uint8_t tck = 0;
 _Bool is_tck_present = 0;
 do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TS" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "TS" "_%d=%02x", 0, ch); else printf(" " "TS" "=%02x", ch); printf(": %s convention\n", ch == 0x3b ? "direct" : (ch == 0x3f ? "inverse" : "unknown")); } while (0);
 if (ch != 0x3b)
  errx(1, "unsupported convention");
 tck ^= ch;
 do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "T0" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "T0" "_%d=%02x", 0, ch); else printf(" " "T0" "=%02x", ch); printf(": Y_1=0b%s, K=%u\n", str_nibble((Y = (ch & 0xf0)) >> 4), K = (ch & 0xf)); } while (0);
 for (int i = 1; Y != 0; i++) {
  if (Y & (1 << 4))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TA" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TA" "_%d=%02x", i, ch); else printf(" " "TA" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 5))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TB" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TB" "_%d=%02x", i, ch); else printf(" " "TB" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 6))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TC" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TC" "_%d=%02x", i, ch); else printf(" " "TC" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 7)) {
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TD" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TD" "_%d=%02x", i, ch); else printf(" " "TD" "=%02x", ch); printf(": Y=0b%s, T=%d\n", str_nibble((Y = (ch & 0xf0)) >> 4), ch & 0xf); } while (0);
   if ((ch & 0xf) != 0)
    is_tck_present = 1;
  } else {
   Y = 0;
  }
 }
 for (int i = 1; i <= K; i++)
  do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "T" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "T" "_%d=%02x", i, ch); else printf(" " "T" "=%02x", ch); printf("\n"); } while (0);
 if (is_tck_present) {
  do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TCK" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "TCK" "_%d=%02x", 0, ch); else printf(" " "TCK" "=%02x", ch); printf("\n"); } while (0);
  if (tck != 0)
   errx(1, "TCK check failed");
 }
}

canonical_original_code:
void read_decode_atr(int f)
{
 int max_read_time = 100;
 unsigned char ch;
 unsigned char Y;
 unsigned char K;
 unsigned char tck = 0;
 _Bool is_tck_present = 0;
 do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TS" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "TS" "_%d=%02x", 0, ch); else printf(" " "TS" "=%02x", ch); printf(": %s convention\n", ch == 0x3b ? "direct" : (ch == 0x3f ? "inverse" : "unknown")); } while (0);
 if (ch != 0x3b)
  errx(1, "unsupported convention");
 tck ^= ch;
 do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "T0" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "T0" "_%d=%02x", 0, ch); else printf(" " "T0" "=%02x", ch); printf(": Y_1=0b%s, K=%u\n", str_nibble((Y = (ch & 0xf0)) >> 4), K = (ch & 0xf)); } while (0);
 for (int i = 1; Y != 0; i++) {
  if (Y & (1 << 4))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TA" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TA" "_%d=%02x", i, ch); else printf(" " "TA" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 5))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TB" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TB" "_%d=%02x", i, ch); else printf(" " "TB" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 6))
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TC" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TC" "_%d=%02x", i, ch); else printf(" " "TC" "=%02x", ch); printf("\n"); } while (0);
  if (Y & (1 << 7)) {
   do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TD" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "TD" "_%d=%02x", i, ch); else printf(" " "TD" "=%02x", ch); printf(": Y=0b%s, T=%d\n", str_nibble((Y = (ch & 0xf0)) >> 4), ch & 0xf); } while (0);
   if ((ch & 0xf) != 0)
    is_tck_present = 1;
  } else {
   Y = 0;
  }
 }
 for (int i = 1; i <= K; i++)
  do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "T" "_%d character", i); tck ^= ch; if (i != 0) printf(" " "T" "_%d=%02x", i, ch); else printf(" " "T" "=%02x", ch); printf("\n"); } while (0);
 if (is_tck_present) {
  do { if (sync_read(f, &ch, 1, max_read_time) != 1) errx(1, "failed to read " "TCK" "_%d character", 0); tck ^= ch; if (0 != 0) printf(" " "TCK" "_%d=%02x", 0, ch); else printf(" " "TCK" "=%02x", ch); printf("\n"); } while (0);
  if (tck != 0)
   errx(1, "TCK check failed");
 }
}

variable_types: f int

variable_types:
ch
unsigned char

variable_types:
Y
unsigned char

variable_types:
K
unsigned char

variable_types:
tck
unsigned char

variable_types: i int


decompiled_code:
int __fastcall dump_atr(int a1)
{
int v1; // eax
unsigned __int8 *v2; // rbx
unsigned __int8 *v3; // rbp
_BYTE v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = sync_read(a1, (__int64)v5, 0x20uLL, 1000);
if ( v1 > 0 )
{
v2 = v5;
v3 = &v5[v1 - 1 + 1];
do
printf("%02X ", *v2++);
while ( v2 != v3 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func15(int a1)
{
int v1; // eax
unsigned __int8 *v2; // rbx
unsigned __int8 *v3; // rbp
_BYTE v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = func12(a1, (__int64)v5, 0x20uLL, 1000);
if ( v1 > 0 )
{
v2 = v5;
v3 = &v5[v1 - 1 + 1];
do
printf("%02X ", *v2++);
while ( v2 != v3 );
}
return putchar(10);
}

original_code:
void
dump_atr(int f)
{
 uint8_t buf[32];
 int n;
 int i;
 n = sync_read(f, buf, 32, 1000);
 for (i = 0; i < n; ++i)
  printf("%02X ", buf[i]);
 putchar('\n');
}

canonical_original_code:
void dump_atr(int f)
{
 unsigned char buf[32];
 int n;
 int i;
 n = sync_read(f, buf, 32, 1000);
 for (i = 0; i < n; ++i)
  printf("%02X ", buf[i]);
 putchar('\n');
}

variable_types:
buf
unsigned char [32]

variable_types: n int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx

if ( argc <= 1 )
{
puts("usage: uicc /path/to/tty/device/node");
exit(1);
}
v3 = open(argv[1], 2, envp);
if ( v3 == -1 )
err(1, "open");
write_log("Opened device node.\n");
print_line_state(v3);
print_baud_rate(v3);
write_log("Configuring serial line...\n");
set_raw_io(v3);
set_nonblock(v3);
set_baud_rate(v3, 0xDu);
write_log("Resetting ICC...\n");
write_log("Clearing DTR...\n");
clear_line_state(v3, 2);
sleep_for_ms(0xC8u);
flush_queues(v3);
write_log("Setting DTR...\n");
set_line_state(v3, 2);
write_log("Reading ICC's Answer To Reset (ATR)...\n");
read_decode_atr(v3);
close(v3);
return 0;
}

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

if ( argc <= 1 )
{
puts("usage: uicc /path/to/tty/device/node");
exit(1);
}
v3 = open(argv[1], 2, envp);
if ( v3 == -1 )
err(1, "open");
func0("Opened device node.\n");
func2(v3);
func3(v3);
func0("Configuring serial line...\n");
func7(v3);
func8(v3);
func6(v3, 0xDu);
func0("Resetting ICC...\n");
func0("Clearing DTR...\n");
func5(v3, 2);
func11(0xC8u);
func10(v3);
func0("Setting DTR...\n");
func4(v3, 2);
func0("Reading ICC's Answer To Reset (ATR)...\n");
func14(v3);
close(v3);
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 int f;
 if (argc < 2) {
  printf("usage: uicc /path/to/tty/device/node\n");
  exit(1);
 }
 f = open(argv[1], 02);
 if (f == -1)
  err(1, "open");
 write_log("Opened device node.\n");
 print_line_state(f);
 print_baud_rate(f);
 write_log("Configuring serial line...\n");
 set_raw_io(f);
 set_nonblock(f);
 set_baud_rate(f, 0000015);
 write_log("Resetting ICC...\n");
 write_log("Clearing DTR...\n");
 clear_line_state(f, 0x002);
 sleep_for_ms(200);
 flush_queues(f);
 write_log("Setting DTR...\n");
 set_line_state(f, 0x002);
 write_log("Reading ICC's Answer To Reset (ATR)...\n");
 read_decode_atr(f);
 close(f);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
void __fastcall print_peers(__int64 a1)
{
__int64 i; // rbx
const char *v2; // rax
int v3; // edi
const void *v4; // rsi
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

for ( i = a1; i; i = *(_QWORD *)(i + 416) )
{
while ( 1 )
{
v3 = *(_DWORD *)(i + 4);
v4 = (const void *)(i + 28);
if ( v3 == 2 )
break;
v4 = (const void *)(i + 16);
if ( v3 == 10 )
break;
syslog(7, "(unknown address family)");
}
v2 = inet_ntop(v3, v4, v5, 0x2Eu);
syslog(7, "%s\n", v2);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
__int64 i; // rbx
const char *v2; // rax
int v3; // edi
const void *v4; // rsi
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

for ( i = a1; i; i = *(_QWORD *)(i + 416) )
{
while ( 1 )
{
v3 = *(_DWORD *)(i + 4);
v4 = (const void *)(i + 28);
if ( v3 == 2 )
break;
v4 = (const void *)(i + 16);
if ( v3 == 10 )
break;
syslog(7, "(unknown address family)");
}
v2 = inet_ntop(v3, v4, v5, 0x2Eu);
syslog(7, "%s\n", v2);
}
}

original_code:
static void
print_peers(struct peer *peer)
{
 void *addr;
 char addrstr[46];
 while (peer != ((void *)0)) {
  switch (peer->addr_family) {
  case 2:
   addr = peer->addr.sin6_addr.__in6_u.__u6_addr8 + 12;
   break;
  case 10:
   addr = &peer->addr.sin6_addr;
   break;
  default:
   syslog(7, "(unknown address family)");
   continue;
  }
  syslog(7, "%s\n", inet_ntop(peer->addr_family, addr, addrstr, sizeof(addrstr)));
  peer = peer->next;
 }
}

variable_types: struct peer { enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 } state; int addr_family; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr; struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; } version; struct peer *next; struct protocol { int conn; enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 } state; struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; } *buf; struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; } hdr; union message { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct version_msg version; struct ping_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long nonce; } ping; struct addr_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct netaddr addr_list[]; } addr; struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; } getheaders; struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } inv; struct headers_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; } headers[]; } headers; struct block_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct block_hdr hdr; struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input { struct outpoint { unsigned char hash[32]; unsigned int index; } prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; } **in; unsigned long out_count; struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; } **out; unsigned int locktime; } tx[]; } block; } *msg; } proto; } *

variable_types:
addr
void *

variable_types:
addrstr
char [46]


decompiled_code:
__int64 discover_peers()
{
struct addrinfo *v0; // rbx
__int64 v1; // r13
__int64 v2; // r12
int *v3; // rax
__int64 v4; // rbp
int v5; // eax
const __m128i *v6; // roff
struct sockaddr *v7; // rax
struct addrinfo v9; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-30h] BYREF

pai = 0LL;
*(_QWORD *)&v9.ai_flags = 0LL;
*(_QWORD *)&v9.ai_socktype = 1LL;
*(_QWORD *)&v9.ai_addrlen = 0LL;
v9.ai_addr = 0LL;
v9.ai_canonname = 0LL;
v9.ai_next = 0LL;
if ( getaddrinfo("seed.bitcoin.sipa.be.", 0LL, &v9, &pai) == -1 )
{
v3 = _errno_location();
syslog(3, "%s: getaddrinfo failed, errno %d", "discover_peers", (unsigned int)*v3);
v2 = 0LL;
}
else
{
v0 = pai;
if ( pai )
{
v1 = 0LL;
v2 = 0LL;
do
{
if ( (v0->ai_family & 0xFFFFFFF7) == 2 )
{
v4 = xcalloc(1LL, 472LL);
*(_QWORD *)(v4 + 432) = xcalloc(1LL, 16LL);
v5 = v0->ai_family;
*(_DWORD *)(v4 + 4) = v5;
if ( v5 == 10 )
{
v6 = (const __m128i *)v0->ai_addr;
*(__m128i *)(v4 + 8) = _mm_loadu_si128(v6);
*(_QWORD *)(v4 + 24) = v6[1].m128i_i64[0];
*(_DWORD *)(v4 + 32) = v6[1].m128i_i32[2];
}
else
{
v7 = v0->ai_addr;
*(_WORD *)(v4 + 26) = -1;
*(_DWORD *)(v4 + 28) = *(_DWORD *)&v7->sa_data[2];
}
if ( v2 )
{
*(_QWORD *)(v1 + 416) = v4;
v1 = v4;
}
else
{
v1 = v4;
v2 = v4;
}
}
v0 = v0->ai_next;
}
while ( v0 );
}
else
{
v2 = 0LL;
}
freeaddrinfo(pai);
if ( pai )
{
syslog(7, "%s: discovered:", "discover_peers");
print_peers(v2);
}
else
{
syslog(7, "%s: no peers discovered, perhaps there's no internet connection", "discover_peers");
}
}
return v2;
}

canonical_decompiled_code:
__int64 func3()
{
struct addrinfo *v0; // rbx
__int64 v1; // r13
__int64 v2; // r12
int *v3; // rax
__int64 v4; // rbp
int v5; // eax
const __m128i *v6; // roff
struct sockaddr *v7; // rax
struct addrinfo v9; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-30h] BYREF

pai = 0LL;
*(_QWORD *)&v9.ai_flags = 0LL;
*(_QWORD *)&v9.ai_socktype = 1LL;
*(_QWORD *)&v9.ai_addrlen = 0LL;
v9.ai_addr = 0LL;
v9.ai_canonname = 0LL;
v9.ai_next = 0LL;
if ( getaddrinfo("seed.bitcoin.sipa.be.", 0LL, &v9, &pai) == -1 )
{
v3 = _errno_location();
syslog(3, "%s: getaddrinfo failed, errno %d", "discover_peers", (unsigned int)*v3);
v2 = 0LL;
}
else
{
v0 = pai;
if ( pai )
{
v1 = 0LL;
v2 = 0LL;
do
{
if ( (v0->ai_family & 0xFFFFFFF7) == 2 )
{
v4 = xcalloc(1LL, 472LL);
*(_QWORD *)(v4 + 432) = xcalloc(1LL, 16LL);
v5 = v0->ai_family;
*(_DWORD *)(v4 + 4) = v5;
if ( v5 == 10 )
{
v6 = (const __m128i *)v0->ai_addr;
*(__m128i *)(v4 + 8) = _mm_loadu_si128(v6);
*(_QWORD *)(v4 + 24) = v6[1].m128i_i64[0];
*(_DWORD *)(v4 + 32) = v6[1].m128i_i32[2];
}
else
{
v7 = v0->ai_addr;
*(_WORD *)(v4 + 26) = -1;
*(_DWORD *)(v4 + 28) = *(_DWORD *)&v7->sa_data[2];
}
if ( v2 )
{
*(_QWORD *)(v1 + 416) = v4;
v1 = v4;
}
else
{
v1 = v4;
v2 = v4;
}
}
v0 = v0->ai_next;
}
while ( v0 );
}
else
{
v2 = 0LL;
}
freeaddrinfo(pai);
if ( pai )
{
syslog(7, "%s: discovered:", "discover_peers");
func1(v2);
}
else
{
syslog(7, "%s: no peers discovered, perhaps there's no internet connection", "discover_peers");
}
}
return v2;
}

original_code:
struct peer *
discover_peers(void)
{
 const char *domain_name = "seed.bitcoin.sipa.be.";
 struct addrinfo *addrs = ((void *)0);
 struct addrinfo hints;
 memset(&hints, 0, sizeof hints);
 hints.ai_socktype = SOCK_STREAM;
 if (getaddrinfo(domain_name, ((void *)0), &hints, &addrs) == -1) {
  syslog(3, "%s: getaddrinfo failed, errno %d", __func__, (*__errno_location ()));
  return ((void *)0);
 }
 struct peer *head, *tail, *peer;
 head = tail = peer = ((void *)0);
 for (struct addrinfo *ai = addrs; ai != ((void *)0); ai = ai->ai_next) {
  switch (ai->ai_family) {
  case 2:
  case 10:
   peer = xcalloc(1, sizeof(*peer));
   peer->proto.buf = xcalloc(1, sizeof(*peer->proto.buf));
   peer->addr_family = ai->ai_family;
   if (ai->ai_family == 10) {
    peer->addr = *((struct sockaddr_in6 *)ai->ai_addr);
   } else {
    uint8_t *s6_addr_ptr = peer->addr.sin6_addr.__in6_u.__u6_addr8;
    void *s_addr_ptr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
    memset(s6_addr_ptr + 10, 0xff, 2);
    memcpy(s6_addr_ptr + 12, s_addr_ptr, 4);
   }
   if (head == ((void *)0)) {
    head = tail = peer;
   } else {
    tail->next = peer;
    tail = peer;
   }
   break;
  default:
   continue;
  }
 }
 freeaddrinfo(addrs);
 if (addrs == ((void *)0)) {
  syslog(7, "%s: no peers discovered, perhaps there's no internet connection", __func__);
 } else {
  syslog(7, "%s: discovered:", __func__);
  print_peers(head);
 }
 return head;
}

canonical_original_code:
struct peer *discover_peers()
{
 char *domain_name = "seed.bitcoin.sipa.be.";
 struct addrinfo *addrs = ((void *)0);
 struct addrinfo hints;
 memset(&hints, 0, sizeof hints);
 hints.ai_socktype = SOCK_STREAM;
 if (getaddrinfo(domain_name, ((void *)0), &hints, &addrs) == -1) {
  syslog(3, "%s: getaddrinfo failed, errno %d", __func__, (*__errno_location ()));
  return ((void *)0);
 }
 struct peer *head; struct peer *tail; struct peer *peer;
 head = tail = peer = ((void *)0);
 for (struct addrinfo *ai = addrs; ai != ((void *)0); ai = ai->ai_next) {
  switch (ai->ai_family) {
  case 2:
  case 10:
   peer = xcalloc(1, sizeof(*peer));
   peer->proto.buf = xcalloc(1, sizeof(*peer->proto.buf));
   peer->addr_family = ai->ai_family;
   if (ai->ai_family == 10) {
    peer->addr = *((struct sockaddr_in6 *)ai->ai_addr);
   } else {
    unsigned char *s6_addr_ptr = peer->addr.sin6_addr.__in6_u.__u6_addr8;
    void *s_addr_ptr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
    memset(s6_addr_ptr + 10, 0xff, 2);
    memcpy(s6_addr_ptr + 12, s_addr_ptr, 4);
   }
   if (head == ((void *)0)) {
    head = tail = peer;
   } else {
    tail->next = peer;
    tail = peer;
   }
   break;
  default:
   continue;
  }
 }
 freeaddrinfo(addrs);
 if (addrs == ((void *)0)) {
  syslog(7, "%s: no peers discovered, perhaps there's no internet connection", __func__);
 } else {
  syslog(7, "%s: discovered:", __func__);
  print_peers(head);
 }
 return head;
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

return_type: struct peer { enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 } state; int addr_family; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr; struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; } version; struct peer *next; struct protocol { int conn; enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 } state; struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; } *buf; struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; } hdr; union message { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct version_msg version; struct ping_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long nonce; } ping; struct addr_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct netaddr addr_list[]; } addr; struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; } getheaders; struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } inv; struct headers_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; } headers[]; } headers; struct block_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct block_hdr hdr; struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input { struct outpoint { unsigned char hash[32]; unsigned int index; } prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; } **in; unsigned long out_count; struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; } **out; unsigned int locktime; } tx[]; } block; } *msg; } proto; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


original_code:
int
peer_send_version(struct peer *peer)
{
 struct version_msg msg;
 const struct in6_addr loopback = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
 int error;
 memset(&msg, 0, sizeof(msg));
 msg.version = 70014;
 msg.services = 0;
 msg.timestamp = time(((void *)0));
 msg.recv_addr.services = 0;
 msg.recv_addr.ip = peer->addr.sin6_addr;
 msg.recv_addr.port = ntohs(peer->addr.sin6_port);
 msg.trans_addr.services = 0;
 msg.trans_addr.ip = loopback;
 msg.trans_addr.port = ntohs(peer->addr.sin6_port);
 msg.nonce = 0;
 msg.user_agent_len = sizeof(self_user_agent) - 1;
 strcpy(msg.user_agent, self_user_agent);
 msg.start_height = 0;
 msg.relay = 1;
 error = write_version_msg(&msg, peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to write version message to peer", __func__);
  return error;
 }
 return 0;
}

canonical_original_code:
int peer_send_version(struct peer *peer)
{
 struct version_msg msg;
 struct in6_addr loopback = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
 int error;
 memset(&msg, 0, sizeof(msg));
 msg.version = 70014;
 msg.services = 0;
 msg.timestamp = time(((void *)0));
 msg.recv_addr.services = 0;
 msg.recv_addr.ip = peer->addr.sin6_addr;
 msg.recv_addr.port = ntohs(peer->addr.sin6_port);
 msg.trans_addr.services = 0;
 msg.trans_addr.ip = loopback;
 msg.trans_addr.port = ntohs(peer->addr.sin6_port);
 msg.nonce = 0;
 msg.user_agent_len = sizeof(self_user_agent) - 1;
 strcpy(msg.user_agent, self_user_agent);
 msg.start_height = 0;
 msg.relay = 1;
 error = write_version_msg(&msg, peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to write version message to peer", __func__);
  return error;
 }
 return 0;
}

variable_types: struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; }

variable_types: error int

user_defined_types: struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; }

user_defined_types: enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 }

user_defined_types: enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 }

user_defined_types: struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; }

user_defined_types: struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; }

user_defined_types: struct inv_vec { unsigned int type; unsigned char hash[32]; }

user_defined_types: struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; }

user_defined_types: struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; }

user_defined_types: struct outpoint { unsigned char hash[32]; unsigned int index; }


decompiled_code:
__int64 __fastcall peer_connect(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebx
__int16 v4; // ax
int v5; // er12
socklen_t v6; // er14
struct sockaddr *v7; // r13
char *v8; // rsi
__int16 v9; // ax
int *v10; // rax
int *v11; // rbp
char v12[48]; // [rsp+0h] [rbp-88h] BYREF
__m128i cp; // [rsp+30h] [rbp-58h] BYREF
__int64 v14; // [rsp+40h] [rbp-48h]
int v15; // [rsp+48h] [rbp-40h]
struct sockaddr addr; // [rsp+50h] [rbp-38h] BYREF

v1 = *(_DWORD *)(a1 + 4);
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
cp = 0uLL;
v14 = 0LL;
v15 = 0;
if ( v1 == 2 )
{
addr.sa_family = 2;
v4 = _bswap_16(8333);
*(_WORD *)(a1 + 10) = v4;
*(_WORD *)addr.sa_data = v4;
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a1 + 28);
v5 = socket(2, 1, 0);
if ( v5 != -1 )
{
v6 = 16;
v7 = &addr;
v8 = &addr.sa_data[2];
goto LABEL_9;
}
LABEL_16:
v11 = _errno_location();
syslog(
4,
"%s: failed to create stream socket for address family %d, errno %d",
"peer_connect",
v1,
(unsigned int)*v11);
return (unsigned int)*v11;
}
if ( v1 != 10 )
{
syslog(3, "%s: unknown address family %d", "peer_connect", v1);
return (unsigned int)-1;
}
cp = _mm_loadu_si128((const __m128i *)(a1 + 8));
v14 = *(_QWORD *)(a1 + 24);
v15 = *(_DWORD *)(a1 + 32);
v9 = _bswap_16(8333);
*(_WORD *)(a1 + 10) = v9;
cp.m128i_i16[1] = v9;
v5 = socket(10, 1, 0);
if ( v5 == -1 )
goto LABEL_16;
v6 = 28;
v7 = (struct sockaddr *)&cp;
v8 = &cp.m128i_i8[8];
LABEL_9:
inet_ntop(v1, v8, v12, 0x2Eu);
syslog(7, "%s: connecting to %s", "peer_connect", v12);
v2 = connect(v5, v7, v6);
if ( v2 == -1 )
{
syslog(4, "%s: failed to connect to %s", "peer_connect", v12);
close(v5);
}
else
{
*(_DWORD *)(a1 + 424) = v5;
v2 = peer_send_version((const __m128i *)a1);
if ( v2 )
{
syslog(3, "%s: failed to send version message to peer", "peer_connect");
}
else
{
v2 = read_version_msg(*(unsigned int *)(a1 + 424), a1 + 40);
if ( v2 )
{
syslog(3, "%s: failed to read version message from peer", "peer_connect");
}
else
{
v2 = write_verack_msg(*(unsigned int *)(a1 + 424));
if ( v2 )
{
syslog(3, "%s: failed to send verack message to peer", "peer_connect");
}
else
{
v2 = read_verack_msg(*(unsigned int *)(a1 + 424));
if ( v2 )
{
syslog(3, "%s: failed to read verack message from peer", "peer_connect");
}
else
{
*(_DWORD *)a1 = 1;
*(_DWORD *)(a1 + 428) = 0;
if ( fcntl(v5, 4, 2048LL) == -1 )
{
v10 = _errno_location();
syslog(
2,
"%s: failed to set non-blocing mode for peer's socket, errno %d",
"peer_connect",
(unsigned int)*v10);
exit(1);
}
}
}
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebx
__int16 v4; // ax
int v5; // er12
socklen_t v6; // er14
struct sockaddr *v7; // r13
char *v8; // rsi
__int16 v9; // ax
int *v10; // rax
int *v11; // rbp
char v12[48]; // [rsp+0h] [rbp-88h] BYREF
__m128i cp; // [rsp+30h] [rbp-58h] BYREF
__int64 v14; // [rsp+40h] [rbp-48h]
int v15; // [rsp+48h] [rbp-40h]
struct sockaddr addr; // [rsp+50h] [rbp-38h] BYREF

v1 = *(_DWORD *)(a1 + 4);
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
cp = 0uLL;
v14 = 0LL;
v15 = 0;
if ( v1 == 2 )
{
addr.sa_family = 2;
v4 = func2(8333);
*(_WORD *)(a1 + 10) = v4;
*(_WORD *)addr.sa_data = v4;
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a1 + 28);
v5 = socket(2, 1, 0);
if ( v5 != -1 )
{
v6 = 16;
v7 = &addr;
v8 = &addr.sa_data[2];
goto LABEL_9;
}
LABEL_16:
v11 = _errno_location();
syslog(
4,
"%s: failed to create stream socket for address family %d, errno %d",
"peer_connect",
v1,
(unsigned int)*v11);
return (unsigned int)*v11;
}
if ( v1 != 10 )
{
syslog(3, "%s: unknown address family %d", "peer_connect", v1);
return (unsigned int)-1;
}
cp = _mm_loadu_si128((const __m128i *)(a1 + 8));
v14 = *(_QWORD *)(a1 + 24);
v15 = *(_DWORD *)(a1 + 32);
v9 = func2(8333);
*(_WORD *)(a1 + 10) = v9;
cp.m128i_i16[1] = v9;
v5 = socket(10, 1, 0);
if ( v5 == -1 )
goto LABEL_16;
v6 = 28;
v7 = (struct sockaddr *)&cp;
v8 = &cp.m128i_i8[8];
LABEL_9:
inet_ntop(v1, v8, v12, 0x2Eu);
syslog(7, "%s: connecting to %s", "peer_connect", v12);
v2 = connect(v5, v7, v6);
if ( v2 == -1 )
{
syslog(4, "%s: failed to connect to %s", "peer_connect", v12);
close(v5);
}
else
{
*(_DWORD *)(a1 + 424) = v5;
v2 = func4((const __m128i *)a1);
if ( v2 )
{
syslog(3, "%s: failed to send version message to peer", "peer_connect");
}
else
{
v2 = read_version_msg(*(unsigned int *)(a1 + 424), a1 + 40);
if ( v2 )
{
syslog(3, "%s: failed to read version message from peer", "peer_connect");
}
else
{
v2 = write_verack_msg(*(unsigned int *)(a1 + 424));
if ( v2 )
{
syslog(3, "%s: failed to send verack message to peer", "peer_connect");
}
else
{
v2 = read_verack_msg(*(unsigned int *)(a1 + 424));
if ( v2 )
{
syslog(3, "%s: failed to read verack message from peer", "peer_connect");
}
else
{
*(_DWORD *)a1 = 1;
*(_DWORD *)(a1 + 428) = 0;
if ( fcntl(v5, 4, 2048LL) == -1 )
{
v10 = _errno_location();
syslog(
2,
"%s: failed to set non-blocing mode for peer's socket, errno %d",
"peer_connect",
(unsigned int)*v10);
exit(1);
}
}
}
}
}
}
return v2;
}

original_code:
int
peer_connect(struct peer *peer)
{
 int family;
 struct sockaddr_in sin;
 struct sockaddr_in6 sin6;
 struct sockaddr *sa;
 socklen_t sa_len;
 int conn;
 int error;
 family = peer->addr_family;
 memset(&sin, 0, sizeof(sin));
 memset(&sin6, 0, sizeof(sin6));
 switch (family) {
 case 2:
  sin.sin_family = 2;
  sin.sin_port = peer->addr.sin6_port = htons(8333);
  memcpy(&sin.sin_addr.s_addr, peer->addr.sin6_addr.__in6_u.__u6_addr8 + 12, 4);
  sa = (struct sockaddr *)&sin;
  sa_len = sizeof(sin);
  break;
 case 10:
  sin6 = peer->addr;
  sin6.sin6_port = peer->addr.sin6_port = htons(8333);
  sa = (struct sockaddr *)&sin6;
  sa_len = sizeof(sin6);
  break;
 default:
  syslog(3, "%s: unknown address family %d", __func__, family);
  return -1;
 }
 conn = socket(family, SOCK_STREAM, 0);
 if (conn == -1) {
  syslog(4, "%s: failed to create stream socket for address family %d, errno %d",
      __func__, family, (*__errno_location ()));
  return (*__errno_location ());
 }
 char addrstr[46];
 inet_ntop(family, family == 2 ? (void *)&sin.sin_addr : (void *)&sin6.sin6_addr,
     addrstr, sizeof(addrstr));
 syslog(7, "%s: connecting to %s", __func__, addrstr);
 if (connect(conn, sa, sa_len) == -1) {
  syslog(4, "%s: failed to connect to %s", __func__, addrstr);
  close(conn);
  return -1;
 }
 peer->proto.conn = conn;
 error = peer_send_version(peer);
 if (error) {
  syslog(3, "%s: failed to send version message to peer", __func__);
  return error;
 }
 error = read_version_msg(peer->proto.conn, &peer->version);
 if (error) {
  syslog(3, "%s: failed to read version message from peer", __func__);
  return error;
 }
 error = write_verack_msg(peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to send verack message to peer", __func__);
  return error;
 }
 error = read_verack_msg(peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to read verack message from peer", __func__);
  return error;
 }
 peer->state = PEER_CONNECTED;
 peer->proto.state = WAIT_HEADER;
 if (fcntl(conn, 4, 04000) == -1) {
  syslog(2, "%s: failed to set non-blocing mode for peer's socket, errno %d", __func__, (*__errno_location ()));
  exit(1);
 }
 return 0;
}

canonical_original_code:
int peer_connect(struct peer *peer)
{
 int family;
 struct sockaddr_in sin;
 struct sockaddr_in6 sin6;
 struct sockaddr *sa;
 unsigned int sa_len;
 int conn;
 int error;
 family = peer->addr_family;
 memset(&sin, 0, sizeof(sin));
 memset(&sin6, 0, sizeof(sin6));
 switch (family) {
 case 2:
  sin.sin_family = 2;
  sin.sin_port = peer->addr.sin6_port = htons(8333);
  memcpy(&sin.sin_addr.s_addr, peer->addr.sin6_addr.__in6_u.__u6_addr8 + 12, 4);
  sa = (struct sockaddr *)&sin;
  sa_len = sizeof(sin);
  break;
 case 10:
  sin6 = peer->addr;
  sin6.sin6_port = peer->addr.sin6_port = htons(8333);
  sa = (struct sockaddr *)&sin6;
  sa_len = sizeof(sin6);
  break;
 default:
  syslog(3, "%s: unknown address family %d", __func__, family);
  return -1;
 }
 conn = socket(family, SOCK_STREAM, 0);
 if (conn == -1) {
  syslog(4, "%s: failed to create stream socket for address family %d, errno %d",
      __func__, family, (*__errno_location ()));
  return (*__errno_location ());
 }
 char addrstr[46];
 inet_ntop(family, family == 2 ? (void *)&sin.sin_addr : (void *)&sin6.sin6_addr,
     addrstr, sizeof(addrstr));
 syslog(7, "%s: connecting to %s", __func__, addrstr);
 if (connect(conn, sa, sa_len) == -1) {
  syslog(4, "%s: failed to connect to %s", __func__, addrstr);
  close(conn);
  return -1;
 }
 peer->proto.conn = conn;
 error = peer_send_version(peer);
 if (error) {
  syslog(3, "%s: failed to send version message to peer", __func__);
  return error;
 }
 error = read_version_msg(peer->proto.conn, &peer->version);
 if (error) {
  syslog(3, "%s: failed to read version message from peer", __func__);
  return error;
 }
 error = write_verack_msg(peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to send verack message to peer", __func__);
  return error;
 }
 error = read_verack_msg(peer->proto.conn);
 if (error) {
  syslog(3, "%s: failed to read verack message from peer", __func__);
  return error;
 }
 peer->state = PEER_CONNECTED;
 peer->proto.state = WAIT_HEADER;
 if (fcntl(conn, 4, 04000) == -1) {
  syslog(2, "%s: failed to set non-blocing mode for peer's socket, errno %d", __func__, (*__errno_location ()));
  exit(1);
 }
 return 0;
}

variable_types: family int

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

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; }

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

variable_types: conn int

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

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct peer { enum peer_state state; int addr_family; struct sockaddr_in6 addr; struct version_msg version; struct peer *next; struct protocol proto; }

user_defined_types: struct version_msg { enum msg_types type; int version; unsigned long services; unsigned long timestamp; struct netaddr recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; }

user_defined_types: struct protocol { int conn; enum protocol_state state; struct buffer *buf; struct msg_hdr hdr; union message *msg; }

user_defined_types: enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 }

user_defined_types: union message { enum msg_types type; struct version_msg version; struct ping_msg ping; struct addr_msg addr; struct getheaders_msg getheaders; struct inv_msg inv; struct headers_msg headers; struct block_msg block; }

user_defined_types: struct ping_msg { enum msg_types type; unsigned long nonce; }

user_defined_types: struct addr_msg { enum msg_types type; unsigned long count; struct netaddr addr_list[]; }

user_defined_types: struct inv_msg { enum msg_types type; unsigned long count; struct inv_vec inv_vec[]; }

user_defined_types: struct headers_msg { enum msg_types type; unsigned long count; struct block_hdr headers[]; }


decompiled_code:
__int64 __fastcall peer_free_list(void *ptr)
{
_DWORD *v1; // rbx
_DWORD *v3; // rbp
int v4; // edi

v1 = ptr;
if ( ptr )
{
do
{
v3 = v1;
v1 = (_DWORD *)*((_QWORD *)v1 + 52);
v4 = v3[106];
if ( v4 != -1 )
close(v4);
free(v3);
}
while ( v1 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *ptr)
{
_DWORD *v1; // rbx
_DWORD *v3; // rbp
int v4; // edi

v1 = ptr;
if ( ptr )
{
do
{
v3 = v1;
v1 = (_DWORD *)*((_QWORD *)v1 + 52);
v4 = v3[106];
if ( v4 != -1 )
close(v4);
free(v3);
}
while ( v1 );
}
return 0LL;
}

original_code:
void *
peer_free_list(struct peer *peer)
{
 struct peer *next;
 while (peer != ((void *)0)) {
  next = peer->next;
  if (peer->proto.conn != -1)
   close(peer->proto.conn);
  free(peer);
  peer = next;
 }
 return ((void *)0);
}

canonical_original_code:
void *peer_free_list(struct peer *peer)
{
 struct peer *next;
 while (peer != ((void *)0)) {
  next = peer->next;
  if (peer->proto.conn != -1)
   close(peer->proto.conn);
  free(peer);
  peer = next;
 }
 return ((void *)0);
}

return_type: void *

user_defined_types: enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }

user_defined_types: struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; }

user_defined_types: enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 }

user_defined_types: struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; }

user_defined_types: struct getheaders_msg { enum msg_types type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; }

user_defined_types: struct block_msg { enum msg_types type; struct block_hdr hdr; struct tx_msg tx[]; }

user_defined_types: struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input **in; unsigned long out_count; struct tx_output **out; unsigned int locktime; }

user_defined_types: struct tx_input { struct outpoint prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; }


decompiled_code:
__int64 __fastcall peer_recv_data(__int64 a1)
{
ssize_t v1; // rbp
unsigned __int64 *v2; // rbx
unsigned __int64 v3; // r14
ssize_t v4; // rax
int v5; // eax
const char *v6; // rcx
__int64 v7; // rax
size_t v8; // r14
__int64 v9; // rdx
__int64 v11; // rax
const char *v12; // rcx

LODWORD(v1) = 0;
if ( *(_DWORD *)(a1 + 428) != 2 )
{
v2 = *(unsigned __int64 **)(a1 + 432);
do
{
if ( *v2 == v2[1] )
{
v2 = (unsigned __int64 *)grow_buffer((__int64)v2, 0x2000LL);
*(_QWORD *)(a1 + 432) = v2;
}
v4 = read(*(_DWORD *)(a1 + 424), (char *)v2 + *v2 + 16, v2[1] - *v2);
v1 = v4;
if ( v4 > 0 )
{
*v2 += v4;
syslog(7, "%s: read %d bytes from peer", "peer_recv_data", (unsigned int)v4);
}
v3 = *v2;
}
while ( *v2 == v2[1] );
if ( v1 != -1 || (v9 = (unsigned int)*_errno_location(), (_DWORD)v9 == 11) )
{
v5 = *(_DWORD *)(a1 + 428);
if ( v5 )
{
LODWORD(v1) = 0;
if ( v5 == 1 )
{
LODWORD(v1) = 0;
if ( v3 >= (unsigned __int64)*(unsigned int *)(a1 + 456) + 24 )
{
LODWORD(v1) = unmarshal_msg(v2, a1 + 440, a1 + 464);
v6 = "un";
if ( !(_DWORD)v1 )
v6 = (const char *)&unk_968;
syslog(7, "%s: %ssuccessfully unmarshalled a message", "peer_recv_data", v6);
v7 = *(unsigned int *)(a1 + 456);
v8 = *v2 + -24 - v7;
memmove(v2 + 2, (char *)v2 + v7 + 40, v8);
*v2 = v8;
*(_DWORD *)(a1 + 428) = 2;
}
}
}
else
{
LODWORD(v1) = 0;
if ( v3 > 0x17 )
{
v11 = unmarshal_msg_hdr(v2 + 2, a1 + 440);
v12 = (const char *)&unk_968;
if ( v11 != 24 )
v12 = "un";
syslog(7, "%s: %ssuccessfully unmarshalled header", "peer_recv_data", v12);
*(_DWORD *)(a1 + 428) = 1;
}
}
}
else
{
syslog(3, "failed to read from peer, errno %d", v9);
}
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
ssize_t v1; // rbp
unsigned __int64 *v2; // rbx
unsigned __int64 v3; // r14
ssize_t v4; // rax
int v5; // eax
const char *v6; // rcx
__int64 v7; // rax
size_t v8; // r14
__int64 v9; // rdx
__int64 v11; // rax
const char *v12; // rcx

LODWORD(v1) = 0;
if ( *(_DWORD *)(a1 + 428) != 2 )
{
v2 = *(unsigned __int64 **)(a1 + 432);
do
{
if ( *v2 == v2[1] )
{
v2 = (unsigned __int64 *)func0((__int64)v2, 0x2000LL);
*(_QWORD *)(a1 + 432) = v2;
}
v4 = read(*(_DWORD *)(a1 + 424), (char *)v2 + *v2 + 16, v2[1] - *v2);
v1 = v4;
if ( v4 > 0 )
{
*v2 += v4;
syslog(7, "%s: read %d bytes from peer", "peer_recv_data", (unsigned int)v4);
}
v3 = *v2;
}
while ( *v2 == v2[1] );
if ( v1 != -1 || (v9 = (unsigned int)*_errno_location(), (_DWORD)v9 == 11) )
{
v5 = *(_DWORD *)(a1 + 428);
if ( v5 )
{
LODWORD(v1) = 0;
if ( v5 == 1 )
{
LODWORD(v1) = 0;
if ( v3 >= (unsigned __int64)*(unsigned int *)(a1 + 456) + 24 )
{
LODWORD(v1) = unmarshal_msg(v2, a1 + 440, a1 + 464);
v6 = "un";
if ( !(_DWORD)v1 )
v6 = (const char *)&unk_968;
syslog(7, "%s: %ssuccessfully unmarshalled a message", "peer_recv_data", v6);
v7 = *(unsigned int *)(a1 + 456);
v8 = *v2 + -24 - v7;
memmove(v2 + 2, (char *)v2 + v7 + 40, v8);
*v2 = v8;
*(_DWORD *)(a1 + 428) = 2;
}
}
}
else
{
LODWORD(v1) = 0;
if ( v3 > 0x17 )
{
v11 = unmarshal_msg_hdr(v2 + 2, a1 + 440);
v12 = (const char *)&unk_968;
if ( v11 != 24 )
v12 = "un";
syslog(7, "%s: %ssuccessfully unmarshalled header", "peer_recv_data", v12);
*(_DWORD *)(a1 + 428) = 1;
}
}
}
else
{
syslog(3, "failed to read from peer, errno %d", v9);
}
}
return (unsigned int)v1;
}

original_code:
int
peer_recv_data(struct peer *peer)
{
 const size_t grow_size = 8192;
 struct buffer *buf;
 ssize_t n_read;
 int error = 0;
 if (peer->proto.state == HAVE_MESSAGE) {
  return 0;
 }
 buf = peer->proto.buf;
 do {
  if (buf->len == buf->size)
   buf = peer->proto.buf = grow_buffer(buf, grow_size);
  n_read = read(peer->proto.conn, buf->bytes + buf->len, buf->size - buf->len);
  if (n_read > 0) {
   buf->len += n_read;
   syslog(7, "%s: read %d bytes from peer", __func__, (int)n_read);
  }
 } while (buf->len == buf->size);
 if (n_read == -1 && (*__errno_location ()) != 11) {
  syslog(3, "failed to read from peer, errno %d", (*__errno_location ()));
  return -1;
 }
 switch (peer->proto.state) {
 case WAIT_HEADER:
  if (buf->len >= ((size_t)(4 + 12 + sizeof(uint32_t) + 4))) {
   size_t size = unmarshal_msg_hdr(buf->bytes, &peer->proto.hdr);
   syslog(7, "%s: %ssuccessfully unmarshalled header", __func__, size == ((size_t)(4 + 12 + sizeof(uint32_t) + 4)) ? "" : "un");
   peer->proto.state = WAIT_MESSAGE;
  }
  break;
 case WAIT_MESSAGE:
  if (buf->len >= ((size_t)(4 + 12 + sizeof(uint32_t) + 4)) + peer->proto.hdr.payload_size) {
   error = unmarshal_msg(buf, &peer->proto.hdr, &peer->proto.msg);
   syslog(7, "%s: %ssuccessfully unmarshalled a message", __func__, error ? "un" : "");
   size_t next_len = buf->len - (((size_t)(4 + 12 + sizeof(uint32_t) + 4)) + peer->proto.hdr.payload_size);
   memmove(buf->bytes, buf->bytes + buf->len - next_len, next_len);
   buf->len = next_len;
   peer->proto.state = HAVE_MESSAGE;
  }
  break;
 default:
  break;
 }
 return error;
}

canonical_original_code:
int peer_recv_data(struct peer *peer)
{
 unsigned long grow_size = 8192;
 struct buffer *buf;
 long n_read;
 int error = 0;
 if (peer->proto.state == HAVE_MESSAGE) {
  return 0;
 }
 buf = peer->proto.buf;
 do {
  if (buf->len == buf->size)
   buf = peer->proto.buf = grow_buffer(buf, grow_size);
  n_read = read(peer->proto.conn, buf->bytes + buf->len, buf->size - buf->len);
  if (n_read > 0) {
   buf->len += n_read;
   syslog(7, "%s: read %d bytes from peer", __func__, (int)n_read);
  }
 } while (buf->len == buf->size);
 if (n_read == -1 && (*__errno_location ()) != 11) {
  syslog(3, "failed to read from peer, errno %d", (*__errno_location ()));
  return -1;
 }
 switch (peer->proto.state) {
 case WAIT_HEADER:
  if (buf->len >= ((unsigned long)(4 + 12 + sizeof(unsigned int) + 4))) {
   unsigned long size = unmarshal_msg_hdr(buf->bytes, &peer->proto.hdr);
   syslog(7, "%s: %ssuccessfully unmarshalled header", __func__, size == ((unsigned long)(4 + 12 + sizeof(unsigned int) + 4)) ? "" : "un");
   peer->proto.state = WAIT_MESSAGE;
  }
  break;
 case WAIT_MESSAGE:
  if (buf->len >= ((unsigned long)(4 + 12 + sizeof(unsigned int) + 4)) + peer->proto.hdr.payload_size) {
   error = unmarshal_msg(buf, &peer->proto.hdr, &peer->proto.msg);
   syslog(7, "%s: %ssuccessfully unmarshalled a message", __func__, error ? "un" : "");
   unsigned long next_len = buf->len - (((size_t)(4 + 12 + sizeof(uint32_t) + 4)) + peer->proto.hdr.payload_size);
   memmove(buf->bytes, buf->bytes + buf->len - next_len, next_len);
   buf->len = next_len;
   peer->proto.state = HAVE_MESSAGE;
  }
  break;
 default:
  break;
 }
 return error;
}

variable_types: struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; } *

variable_types:
size
unsigned long

user_defined_types: struct peer { enum peer_state state; int addr_family; struct sockaddr_in6 addr; struct version_msg version; struct peer *next; struct protocol proto; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct version_msg { enum msg_types type; int version; unsigned long services; unsigned long timestamp; struct netaddr recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; }

user_defined_types: struct inv_vec { unsigned int type; unsigned char hash[32]; }

user_defined_types: struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; }

user_defined_types: struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; }

user_defined_types: struct outpoint { unsigned char hash[32]; unsigned int index; }


decompiled_code:
__int64 __fastcall peer_read_message(__int64 a1, _QWORD *a2)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)(a1 + 428) <= 1u )
result = peer_recv_data(a1);
if ( *(_DWORD *)(a1 + 428) == 2 )
{
*a2 = *(_QWORD *)(a1 + 464);
*(_QWORD *)(a1 + 464) = 0LL;
*(_DWORD *)(a1 + 428) = 0;
result = peer_recv_data(a1);
}
return result;
}

original_code:
int
peer_read_message(struct peer *peer, union message **msg)
{
 int error = 0;
 switch (peer->proto.state) {
 case WAIT_HEADER:
 case WAIT_MESSAGE:
  error = peer_recv_data(peer);
  break;
 case HAVE_MESSAGE:
  break;
 }
 if (peer->proto.state == HAVE_MESSAGE) {
  *msg = peer->proto.msg;
  peer->proto.msg = ((void *)0);
  peer->proto.state = WAIT_HEADER;
  error = peer_recv_data(peer);
 }
 return error;
}

variable_types:
msg
union message **

user_defined_types: struct protocol { int conn; enum protocol_state state; struct buffer *buf; struct msg_hdr hdr; union message *msg; }

user_defined_types: enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 }

user_defined_types: struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; }

user_defined_types: union message { enum msg_types type; struct version_msg version; struct ping_msg ping; struct addr_msg addr; struct getheaders_msg getheaders; struct inv_msg inv; struct headers_msg headers; struct block_msg block; }

user_defined_types: struct ping_msg { enum msg_types type; unsigned long nonce; }

user_defined_types: struct addr_msg { enum msg_types type; unsigned long count; struct netaddr addr_list[]; }

user_defined_types: struct inv_msg { enum msg_types type; unsigned long count; struct inv_vec inv_vec[]; }

user_defined_types: struct headers_msg { enum msg_types type; unsigned long count; struct block_hdr headers[]; }


CopyFigure

decompiled_code:
void *__fastcall CopyFigure(void *a1, unsigned int *a2)
{
return memcpy(a1, a2, 8LL * *a2 + 4);
}

canonical_decompiled_code:
void *__fastcall func7(void *a1, unsigned int *a2)
{
return memcpy(a1, a2, 8LL * *a2 + 4);
}

original_code:
Figure *
CopyFigure(Figure *dst, Figure *src)
{
 memcpy(dst, src, sizeof(*src) + src->n * sizeof(Vertex));
}

canonical_original_code:
struct Figure *CopyFigure(struct Figure *dst, struct Figure *src)
{
 memcpy(dst, src, sizeof(*src) + src->n * sizeof(Vertex));
}

variable_types:
dst
struct Figure *

variable_types:
src
struct Figure *

return_type: struct Figure { unsigned int n; struct Vertex { int x; int y; } v[]; } *

user_defined_types: struct Figure { unsigned int n; struct Vertex v[]; }


DrawFigure

decompiled_code:
int __fastcall DrawFigure(unsigned int *a1)
{
unsigned int v1; // ecx
unsigned int v2; // ebx
unsigned int v3; // er8
int result; // eax

v1 = *a1;
v2 = 0;
do
{
v3 = v2 % v1;
++v2;
result = XDrawLine(
dpy,
w,
gc,
a1[2 * v3 + 1] + origin,
dword_4040E4 - a1[2 * v3 + 2],
a1[2 * (v2 % v1) + 1] + origin,
dword_4040E4 - a1[2 * (v2 % v1) + 2]);
v1 = *a1;
}
while ( v2 <= *a1 );
return result;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int *a1)
{
unsigned int v1; // ecx
unsigned int v2; // ebx
unsigned int v3; // er8
int result; // eax

v1 = *a1;
v2 = 0;
do
{
v3 = v2 % v1;
++v2;
result = XDrawLine(
dpy,
w,
gc,
a1[2 * v3 + 1] + origin,
dword_4040E4 - a1[2 * v3 + 2],
a1[2 * (v2 % v1) + 1] + origin,
dword_4040E4 - a1[2 * (v2 % v1) + 2]);
v1 = *a1;
}
while ( v2 <= *a1 );
return result;
}

original_code:
void
DrawFigure(Figure *f)
{
 unsigned j;
 unsigned k;
 for (unsigned i = 0; i <= f->n; i++) {
  j = i % f->n;
  k = (i + 1) % f->n;
  XDrawLine(dpy, w, gc,
   origin.x + f->v[j].x, origin.y - f->v[j].y,
   origin.x + f->v[k].x, origin.y - f->v[k].y);
 }
}

canonical_original_code:
void DrawFigure(struct Figure *f)
{
 unsigned int j;
 unsigned int k;
 for (unsigned int i = 0; i <= f->n; i++) {
  j = i % f->n;
  k = (i + 1) % f->n;
  XDrawLine(dpy, w, gc,
   origin.x + f->v[j].x, origin.y - f->v[j].y,
   origin.x + f->v[k].x, origin.y - f->v[k].y);
 }
}

variable_types: struct Figure { unsigned int n; struct Vertex { int x; int y; } v[]; } *

variable_types:
j
unsigned int

variable_types:
k
unsigned int

variable_types:
i
unsigned int


RotateFigure

decompiled_code:
__int64 __fastcall RotateFigure(_DWORD *a1, double a2)
{
int *v2; // rbx
double v3; // xmm0_8
__int64 v4; // rsi
__int64 v5; // rdx
__int64 result; // rax
double v7; // xmm0_8
double v8; // [rsp+0h] [rbp-48h]
double v9; // [rsp+8h] [rbp-40h]
double v10; // [rsp+10h] [rbp-38h]
double cosx; // [rsp+20h] [rbp-28h] BYREF
double sinx[4]; // [rsp+28h] [rbp-20h] BYREF

if ( *a1 )
{
v2 = a1 + 1;
do
{
v9 = (double)*v2;
v10 = (double)v2[1];
v7 = v9 * v9 + v10 * v10;
if ( v7 < 0.0 )
v8 = sqrt(v7);
else
v8 = sqrt(v7);
if ( v8 != 0.0 )
{
v3 = acos(v9 / v8);
sincos(v3 + a2, sinx, &cosx);
v4 = (unsigned int)(int)(v8 * cosx);
*v2 = v4;
v5 = (unsigned int)(int)(v8 * sinx[0]);
v2[1] = v5;
printf("(%f, %f) -> (%d, %d) (d=%f)\n", v9, v10, v4, v5, v8);
}
v2 += 2;
result = (__int64)&a1[2 * *a1 + 1];
}
while ( v2 != (int *)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_DWORD *a1, double a2)
{
int *v2; // rbx
double v3; // xmm0_8
__int64 v4; // rsi
__int64 v5; // rdx
__int64 result; // rax
double v7; // xmm0_8
double v8; // [rsp+0h] [rbp-48h]
double v9; // [rsp+8h] [rbp-40h]
double v10; // [rsp+10h] [rbp-38h]
double cosx; // [rsp+20h] [rbp-28h] BYREF
double sinx[4]; // [rsp+28h] [rbp-20h] BYREF

if ( *a1 )
{
v2 = a1 + 1;
do
{
v9 = (double)*v2;
v10 = (double)v2[1];
v7 = v9 * v9 + v10 * v10;
if ( v7 < 0.0 )
v8 = sqrt(v7);
else
v8 = sqrt(v7);
if ( v8 != 0.0 )
{
v3 = acos(v9 / v8);
sincos(v3 + a2, sinx, &cosx);
v4 = (unsigned int)(int)(v8 * cosx);
*v2 = v4;
v5 = (unsigned int)(int)(v8 * sinx[0]);
v2[1] = v5;
printf("(%f, %f) -> (%d, %d) (d=%f)\n", v9, v10, v4, v5, v8);
}
v2 += 2;
result = (__int64)&a1[2 * *a1 + 1];
}
while ( v2 != (int *)result );
}
return result;
}

original_code:
void
RotateFigure(Figure *f, double angle_delta)
{
 Vertex *v;
 double x;
 double y;
 double d;
 double angle;
 for (v = f->v; v != f->v + f->n; v++) {
  x = v->x;
  y = v->y;
  d = sqrt(x*x + y*y);
  if (d == 0.0)
   continue;
  angle = acos(x / d) + angle_delta;
  v->x = cos(angle) * d;
  v->y = sin(angle) * d;
  printf("(%f, %f) -> (%d, %d) (d=%f)\n", x, y, v->x, v->y, d);
 }
}

canonical_original_code:
void RotateFigure(struct Figure *f, double angle_delta)
{
 struct Vertex *v;
 double x;
 double y;
 double d;
 double angle;
 for (v = f->v; v != f->v + f->n; v++) {
  x = v->x;
  y = v->y;
  d = sqrt(x*x + y*y);
  if (d == 0.0)
   continue;
  angle = acos(x / d) + angle_delta;
  v->x = cos(angle) * d;
  v->y = sin(angle) * d;
  printf("(%f, %f) -> (%d, %d) (d=%f)\n", x, y, v->x, v->y, d);
 }
}

variable_types:
v
struct Vertex { int x; int y; } *

variable_types: x double

variable_types: y double

variable_types: d double

variable_types: angle double


Draw

original_code:
void
Draw(void)
{
 const int side = 80;
 Figure *square;
 Figure *square_t;
 square = AllocFigure(4);
 square->v[0] = (Vertex){ 0, 0 };
 square->v[1] = (Vertex){ 0, side };
 square->v[2] = (Vertex){ side, side };
 square->v[3] = (Vertex){ side, 0 };
 DrawFigure(square);
 square_t = DupFigure(square);
 RotateFigure(square_t, (3.14159265358979323846) / 6.0);
 DrawFigure(square_t);
 CopyFigure(square_t, square);
 RotateFigure(square_t, (3.14159265358979323846) / 5.0);
 DrawFigure(square_t);
 CopyFigure(square_t, square);
 RotateFigure(square_t, (3.14159265358979323846) / 4.0);
 DrawFigure(square_t);
}

canonical_original_code:
void Draw()
{
 int side = 80;
 struct Figure *square;
 struct Figure *square_t;
 square = AllocFigure(4);
 square->v[0] = (struct Vertex){ 0, 0 };
 square->v[1] = (struct Vertex){ 0, side };
 square->v[2] = (struct Vertex){ side, side };
 square->v[3] = (struct Vertex){ side, 0 };
 DrawFigure(square);
 square_t = DupFigure(square);
 RotateFigure(square_t, (3.14159265358979323846) / 6.0);
 DrawFigure(square_t);
 CopyFigure(square_t, square);
 RotateFigure(square_t, (3.14159265358979323846) / 5.0);
 DrawFigure(square_t);
 CopyFigure(square_t, square);
 RotateFigure(square_t, (3.14159265358979323846) / 4.0);
 DrawFigure(square_t);
}

variable_types: side int


unloadlibs

decompiled_code:
int __fastcall unloadlibs(__int64 a1)
{
__int64 v1; // rbx
int result; // eax
char *v3; // rax

v1 = a1;
for ( result = puts("unloading"); *(_QWORD *)v1; v1 += 16LL )
{
result = dlclose(*(void **)(v1 + 8));
if ( result )
{
v3 = dlerror();
errx(1, "dlclose: %s: %s", *(const char **)v1, v3);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
int result; // eax
char *v3; // rax

v1 = a1;
for ( result = puts("unloading"); *(_QWORD *)v1; v1 += 16LL )
{
result = dlclose(*(void **)(v1 + 8));
if ( result )
{
v3 = dlerror();
errx(1, "dlclose: %s: %s", *(const char **)v1, v3);
}
}
return result;
}

original_code:
static void
unloadlibs(struct lib *libs)
{
 puts("unloading");
 for (; libs->name != ((void *)0); ++libs) {
  if (dlclose(libs->handle))
   errx(1, "dlclose: %s: %s", libs->name, dlerror());
 }
}

variable_types: struct lib { char *name; void *handle; } *


loadlibs

decompiled_code:
int __fastcall loadlibs(const char **a1)
{
const char **v1; // rbx
const char *v2; // rax
const char *v3; // rdi
char *v4; // rax

v1 = a1;
LODWORD(v2) = puts("loading");
v3 = *a1;
if ( v3 )
{
do
{
v2 = (const char *)dlopen(v3, 2);
v1[1] = v2;
if ( !v2 )
{
v4 = dlerror();
errx(1, "dlopen: %s: %s", *v1, v4);
}
v1 += 2;
v3 = *v1;
}
while ( *v1 );
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func1(const char **a1)
{
const char **v1; // rbx
const char *v2; // rax
const char *v3; // rdi
char *v4; // rax

v1 = a1;
LODWORD(v2) = puts("loading");
v3 = *a1;
if ( v3 )
{
do
{
v2 = (const char *)dlopen(v3, 2);
v1[1] = v2;
if ( !v2 )
{
v4 = dlerror();
errx(1, "dlopen: %s: %s", *v1, v4);
}
v1 += 2;
v3 = *v1;
}
while ( *v1 );
}
return (int)v2;
}

original_code:
static void
loadlibs(struct lib *libs)
{
 puts("loading");
 for (; libs->name != ((void *)0); ++libs) {
  libs->handle = dlopen(libs->name, 0x00002);
  if (libs->handle == ((void *)0))
   errx(1, "dlopen: %s: %s", libs->name, dlerror());
 }
}

user_defined_types: struct lib { char *name; void *handle; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4[9]; // [rsp+0h] [rbp-48h] BYREF

v4[1] = 0LL;
v4[3] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
v4[0] = "libX11.so";
v4[2] = "libXcursor.so";
v4[4] = "libXrandr.so";
loadlibs(v4);
unloadlibs((__int64)v4);
loadlibs(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4[9]; // [rsp+0h] [rbp-48h] BYREF

v4[1] = 0LL;
v4[3] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
v4[0] = "libX11.so";
v4[2] = "libXcursor.so";
v4[4] = "libXrandr.so";
func1(v4);
func0((__int64)v4);
func1(v4);
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 struct lib libs[] = {
  { "libX11.so" },
  { "libXcursor.so" },
  { "libXrandr.so" },
  { ((void *)0) },
 };
 loadlibs(libs);
 unloadlibs(libs);
 loadlibs(libs);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct lib { char *name; void *handle; } []


decompiled_code:
int __fastcall print_array(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx

if ( a2 )
{
v2 = a1;
do
{
v2 += 4LL;
printf("%d ", *(unsigned int *)(v2 - 4));
}
while ( v2 != a1 + 4LL * a2 );
}
putchar(10);
return fflush(_bss_start);
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx

if ( a2 )
{
v2 = a1;
do
{
v2 += 4LL;
printf("%d ", *(unsigned int *)(v2 - 4));
}
while ( v2 != a1 + 4LL * a2 );
}
putchar(10);
return fflush(_bss_start);
}

original_code:
void
print_array(int *a, int n)
{
 while (n--) {
  printf("%d ", *a++);
 }
 putchar('\n');
 fflush(stdout);
}

variable_types:
a
int *

variable_types: n int


repo: Sevauk/libsquare

decompiled_code:
__int64 always_true()
{
return 1LL;
}

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

original_code:
_Bool always_true(cell_t cell)
{
    return (1);
}

canonical_original_code:
_Bool always_true(union cell_t cell)
{
    return (1);
}

variable_types: union cell_t { char ccell; int icell; long lcell; }


decompiled_code:
__int64 __fastcall binary_print(char a1)
{
return a1 == 0 ? 95 : 35;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
return a1 == 0 ? 95 : 35;
}

original_code:
char binary_print(cell_t cell)
{
    return (cell.ccell ? '#' : '_');
}

canonical_original_code:
char binary_print(union cell_t cell)
{
    return (cell.ccell ? '#' : '_');
}

return_type: char

user_defined_types: union cell_t { char ccell; int icell; long lcell; }


decompiled_code:
bool __fastcall zero_empty(char a1)
{
return a1 != 0;
}

canonical_decompiled_code:
bool __fastcall func2(char a1)
{
return a1 != 0;
}

original_code:
_Bool zero_empty(cell_t cell)
{
    return (cell.ccell);
}

canonical_original_code:
_Bool zero_empty(union cell_t cell)
{
    return (cell.ccell);
}