decompiled_code:
int __fastcall setipaddr_tap(char *src, unsigned int a2)
{
int v2; // ebp
__int64 v3; // rax
__int128 v5; // [rsp+0h] [rbp-48h] BYREF
__int128 v6; // [rsp+10h] [rbp-38h]
__int64 v7; // [rsp+20h] [rbp-28h]

v5 = 0LL;
v7 = 0LL;
v6 = 0LL;
strcpy((char *)&v5, src);
v2 = skfd;
LOWORD(v6) = 2;
DWORD1(v6) = a2;
if ( ioctl(skfd, 0x8916uLL, &v5) < 0 )
{
close(v2);
perrx("socket SIOCSIFADDR");
}
v3 = syscall(186LL);
return fprintf(
stderr,
"[%d]%s set IPaddr: %d.%d.%d.%d\n",
v3,
"setipaddr_tap",
(unsigned __int8)a2,
BYTE1(a2),
BYTE2(a2),
HIBYTE(a2));
}

canonical_decompiled_code:
int __fastcall func7(char *src, unsigned int a2)
{
int v2; // ebp
__int64 v3; // rax
__int128 v5; // [rsp+0h] [rbp-48h] BYREF
__int128 v6; // [rsp+10h] [rbp-38h]
__int64 v7; // [rsp+20h] [rbp-28h]

v5 = 0LL;
v7 = 0LL;
v6 = 0LL;
strcpy((char *)&v5, src);
v2 = skfd;
LOWORD(v6) = 2;
DWORD1(v6) = a2;
if ( ioctl(skfd, 0x8916uLL, &v5) < 0 )
{
close(v2);
perrx("socket SIOCSIFADDR");
}
v3 = syscall(186LL);
return fprintf(
stderr,
"[%d]%s set IPaddr: %d.%d.%d.%d\n",
v3,
"setipaddr_tap",
(unsigned __int8)a2,
BYTE1(a2),
BYTE2(a2),
HIBYTE(a2));
}

original_code:
void setipaddr_tap(unsigned char *name, unsigned int ipaddr)
{
 struct ifreq ifr = {};
 struct sockaddr_in *saddr;
 strcpy(ifr.ifr_ifrn.ifrn_name, (char *)name);
 saddr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
 saddr->sin_family = 2;
 saddr->sin_addr.s_addr = ipaddr;
 if (ioctl(skfd, 0x8916, (void *)&ifr) < 0) {
  close(skfd);
  perrx("socket SIOCSIFADDR");
 }
 fprintf(stderr, "[%d]%s " "set IPaddr: ""%d.%d.%d.%d" "\n", (int)syscall(186), __FUNCTION__, (ipaddr) & 0xff, ((ipaddr) >> 8) & 0xff, ((ipaddr) >> 16) & 0xff, ((ipaddr) >> 24) & 0xff);
}

variable_types:
ipaddr
unsigned int


repo: chobits/tapip

decompiled_code:
int __fastcall getipaddr_tap(char *src, _DWORD *a2)
{
int v2; // er12
unsigned int v3; // ebx
__int64 v4; // rax
__int128 v6; // [rsp+0h] [rbp-48h] BYREF
__int128 v7; // [rsp+10h] [rbp-38h]
__int64 v8; // [rsp+20h] [rbp-28h]

v6 = 0LL;
v8 = 0LL;
v7 = 0LL;
strcpy((char *)&v6, src);
v2 = skfd;
if ( ioctl(skfd, 0x8915uLL, &v6) < 0 )
{
close(v2);
perrx("socket SIOCGIFADDR");
}
v3 = DWORD1(v7);
*a2 = DWORD1(v7);
v4 = syscall(186LL);
return fprintf(
stderr,
"[%d]%s get IPaddr: %d.%d.%d.%d\n",
v4,
"getipaddr_tap",
(unsigned __int8)v3,
BYTE1(v3),
BYTE2(v3),
HIBYTE(v3));
}

canonical_decompiled_code:
int __fastcall func8(char *src, _DWORD *a2)
{
int v2; // er12
unsigned int v3; // ebx
__int64 v4; // rax
__int128 v6; // [rsp+0h] [rbp-48h] BYREF
__int128 v7; // [rsp+10h] [rbp-38h]
__int64 v8; // [rsp+20h] [rbp-28h]

v6 = 0LL;
v8 = 0LL;
v7 = 0LL;
strcpy((char *)&v6, src);
v2 = skfd;
if ( ioctl(skfd, 0x8915uLL, &v6) < 0 )
{
close(v2);
perrx("socket SIOCGIFADDR");
}
v3 = DWORD1(v7);
*a2 = DWORD1(v7);
v4 = syscall(186LL);
return fprintf(
stderr,
"[%d]%s get IPaddr: %d.%d.%d.%d\n",
v4,
"getipaddr_tap",
(unsigned __int8)v3,
BYTE1(v3),
BYTE2(v3),
HIBYTE(v3));
}

original_code:
void getipaddr_tap(unsigned char *name, unsigned int *ipaddr)
{
 struct ifreq ifr = {};
 struct sockaddr_in *saddr;
 strcpy(ifr.ifr_ifrn.ifrn_name, (char *)name);
 if (ioctl(skfd, 0x8915, (void *)&ifr) < 0) {
  close(skfd);
  perrx("socket SIOCGIFADDR");
 }
 saddr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
 *ipaddr = saddr->sin_addr.s_addr;
 fprintf(stderr, "[%d]%s " "get IPaddr: ""%d.%d.%d.%d" "\n", (int)syscall(186), __FUNCTION__, (*ipaddr) & 0xff, ((*ipaddr) >> 8) & 0xff, ((*ipaddr) >> 16) & 0xff, ((*ipaddr) >> 24) & 0xff);
}

variable_types:
name
unsigned char *

variable_types:
ipaddr
unsigned int *

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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

return_type: void

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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


decompiled_code:
int __fastcall getname_tap(int a1, char *a2)
{
__int64 v2; // rax
__int128 v4[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+20h] [rbp-18h]

v4[0] = 0LL;
v5 = 0LL;
v4[1] = 0LL;
if ( ioctl(a1, 0x800454D2uLL, v4) < 0 )
perrx("ioctl SIOCGIFHWADDR");
strcpy(a2, (const char *)v4);
v2 = syscall(186LL);
return fprintf(stderr, "[%d]%s net device: %s\n", v2, "getname_tap", a2);
}

canonical_decompiled_code:
int __fastcall func9(int a1, char *a2)
{
__int64 v2; // rax
__int128 v4[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+20h] [rbp-18h]

v4[0] = 0LL;
v5 = 0LL;
v4[1] = 0LL;
if ( ioctl(a1, 0x800454D2uLL, v4) < 0 )
perrx("ioctl SIOCGIFHWADDR");
strcpy(a2, (const char *)v4);
v2 = syscall(186LL);
return fprintf(stderr, "[%d]%s net device: %s\n", v2, "getname_tap", a2);
}

original_code:
void getname_tap(int tapfd, unsigned char *name)
{
 struct ifreq ifr = {};
 if (ioctl(tapfd, (((2U) << (((0 +8)+8)+14)) | ((('T')) << (0 +8)) | (((210)) << 0) | ((((sizeof(unsigned int)))) << ((0 +8)+8))), (void *)&ifr) < 0)
  perrx("ioctl SIOCGIFHWADDR");
 strcpy((char *)name, ifr.ifr_ifrn.ifrn_name);
 fprintf(stderr, "[%d]%s " "net device: %s" "\n", (int)syscall(186), __FUNCTION__, name);
}

variable_types: tapfd int

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

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }


original_code:
void gethwaddr_tap(int tapfd, unsigned char *ha)
{
 struct ifreq ifr;
 memset(&ifr, 0x0, sizeof(ifr));
 if (ioctl(tapfd, 0x8927, (void *)&ifr) < 0)
  perrx("ioctl SIOCGIFHWADDR");
 hwacpy(ha, ifr.ifr_ifru.ifru_hwaddr.sa_data);
 fprintf(stderr, "[%d]%s " "mac addr: %02x:%02x:%02x:%02x:%02x:%02x" "\n", (int)syscall(186), __FUNCTION__, ha[0], ha[1], ha[2], ha[3], ha[4], ha[5]);
}

variable_types:
ha
unsigned char *


decompiled_code:
__int64 __fastcall alloc_tap(char *src)
{
int v1; // eax
unsigned int v2; // er12
bool v3; // zf
int v5; // edi
__int128 v6; // [rsp+0h] [rbp-48h] BYREF
__int128 v7; // [rsp+10h] [rbp-38h]
__int64 v8; // [rsp+20h] [rbp-28h]

v1 = open("/dev/net/tun", 2);
if ( v1 < 0 )
{
v2 = -1;
perror("open");
}
else
{
v2 = v1;
v3 = *src == 0;
v7 = 0LL;
v8 = 0LL;
LOWORD(v7) = 4098;
v6 = 0LL;
if ( !v3 )
strncpy((char *)&v6, src, 0x10uLL);
if ( ioctl(v2, 0x400454CAuLL, &v6) < 0 )
{
perror("ioctl TUNSETIFF");
v5 = v2;
v2 = -1;
close(v5);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *src)
{
int v1; // eax
unsigned int v2; // er12
bool v3; // zf
int v5; // edi
__int128 v6; // [rsp+0h] [rbp-48h] BYREF
__int128 v7; // [rsp+10h] [rbp-38h]
__int64 v8; // [rsp+20h] [rbp-28h]

v1 = open("/dev/net/tun", 2);
if ( v1 < 0 )
{
v2 = -1;
perror("open");
}
else
{
v2 = v1;
v3 = *src == 0;
v7 = 0LL;
v8 = 0LL;
LOWORD(v7) = 4098;
v6 = 0LL;
if ( !v3 )
strncpy((char *)&v6, src, 0x10uLL);
if ( ioctl(v2, 0x400454CAuLL, &v6) < 0 )
{
perror("ioctl TUNSETIFF");
v5 = v2;
v2 = -1;
close(v5);
}
}
return v2;
}

original_code:
int alloc_tap(char *dev)
{
 struct ifreq ifr;
 int tapfd;
 tapfd = open("/dev/net/tun", 02);
 if (tapfd < 0) {
  perror("open");
  return -1;
 }
 memset(&ifr, 0x0, sizeof(ifr));
 ifr.ifr_ifru.ifru_flags = 0x0002 | 0x1000;
 if (*dev)
  strncpy(ifr.ifr_ifrn.ifrn_name, dev, 16);
 if (ioctl(tapfd, (((1U) << (((0 +8)+8)+14)) | ((('T')) << (0 +8)) | (((202)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), (void *)&ifr) < 0) {
  perror("ioctl TUNSETIFF");
  close(tapfd);
  return -1;
 }
 return tapfd;
}

variable_types:
dev
char *

return_type: int


decompiled_code:
int __fastcall delete_tap(int fd)
{
int result; // eax

result = ioctl(fd, 0x400454CBuLL, 0LL);
if ( result >= 0 )
result = close(fd);
return result;
}

canonical_decompiled_code:
int __fastcall func12(int fd)
{
int result; // eax

result = ioctl(fd, 0x400454CBuLL, 0LL);
if ( result >= 0 )
result = close(fd);
return result;
}


decompiled_code:
int set_tap()
{
int result; // eax

result = socket(2, 2, 0);
skfd = result;
if ( result < 0 )
result = perrx("socket PF_INET");
return result;
}

canonical_decompiled_code:
int func13()
{
int result; // eax

result = socket(2, 2, 0);
skfd = result;
if ( result < 0 )
result = perrx("socket PF_INET");
return result;
}

original_code:
void set_tap(void)
{
 skfd = socket(2, SOCK_DGRAM, IPPROTO_IP);
 if (skfd < 0)
  perrx("socket PF_INET");
}

canonical_original_code:
void set_tap()
{
 skfd = socket(2, SOCK_DGRAM, IPPROTO_IP);
 if (skfd < 0)
  perrx("socket PF_INET");
}


decompiled_code:
int unset_tap()
{
return close(skfd);
}

canonical_decompiled_code:
int func14()
{
return close(skfd);
}

original_code:
void unset_tap(void)
{
 close(skfd);
}

canonical_original_code:
void unset_tap()
{
 close(skfd);
}


original_code:
static char *tcp_control_string(struct tcp *tcphdr)
{
 static char ss[32];
 char *ssp = ss;
 if (tcphdr->fin) {
  strcpy(ssp, "FIN|");
  ssp += 4;
 }
 if (tcphdr->syn) {
  strcpy(ssp, "SYN|");
  ssp += 4;
 }
 if (tcphdr->rst) {
  strcpy(ssp, "RST|");
  ssp += 4;
 }
 if (tcphdr->psh) {
  strcpy(ssp, "PSH|");
  ssp += 4;
 }
 if (tcphdr->ack) {
  strcpy(ssp, "ACK|");
  ssp += 4;
 }
 if (tcphdr->urg) {
  strcpy(ssp, "URG|");
  ssp += 4;
 }
 if (ssp == ss)
  ssp[0] = '\0';
 else
  ssp[-1] = '\0';
 return ss;
}

variable_types: struct tcp { unsigned short src; unsigned short dst; unsigned int seq; unsigned int ackn; unsigned short reserved; unsigned short window; unsigned short checksum; unsigned short urgptr; unsigned char data[]; } *

variable_types:
ss
char [32]

variable_types:
ssp
char *

return_type: char *


original_code:
static void tcp_segment_init(struct tcp_segment *seg, struct ip *iphdr, struct tcp *tcphdr)
{
 seg->seq = _htonl(tcphdr->seq);
 seg->dlen = ((iphdr)->ip_len - ((iphdr)->ip_hlen << 2)) - ((tcphdr)->doff << 2);
 seg->len = seg->dlen + tcphdr->syn + tcphdr->fin;
 seg->text = ((unsigned char *)(tcphdr) + ((tcphdr)->doff << 2));
 seg->lastseq = seg->len ? (seg->seq + seg->len - 1) : seg->seq;
 seg->ack = tcphdr->ack ? _htonl(tcphdr->ackn) : 0;
 seg->wnd = _htons(tcphdr->window);
 seg->up = _htons(tcphdr->urgptr);
 seg->prc = 0;
 seg->iphdr = iphdr;
 seg->tcphdr = tcphdr;
 do { if (net_debug & 0x00000040) fprintf(stderr, "[%d]%s " "\e[01;35m""tcp""\e\[0m"" ""from ""%d.%d.%d.%d"":%d" " to " "%d.%d.%d.%d" ":%d" "\tseq:%u(%d:%d) ack:%u %s" "\n", (int)syscall(186), __FUNCTION__, (iphdr->ip_src) & 0xff, ((iphdr->ip_src) >> 8) & 0xff, ((iphdr->ip_src) >> 16) & 0xff, ((iphdr->ip_src) >> 24) & 0xff, _htons(tcphdr->src), (iphdr->ip_dst) & 0xff, ((iphdr->ip_dst) >> 8) & 0xff, ((iphdr->ip_dst) >> 16) & 0xff, ((iphdr->ip_dst) >> 24) & 0xff, _htons(tcphdr->dst), _htonl(tcphdr->seq), seg->dlen, seg->len, _htonl(tcphdr->ackn), tcp_control_string(tcphdr));} while (0);
}

variable_types: struct tcp_segment { unsigned int seq; unsigned int ack; unsigned int lastseq; unsigned int len; unsigned int dlen; unsigned int wnd; unsigned int up; unsigned int prc; unsigned char *text; struct ip { unsigned char ip_hlen; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_fragoff; unsigned char ip_ttl; unsigned char ip_pro; unsigned short ip_cksum; unsigned int ip_src; unsigned int ip_dst; unsigned char ip_data[]; } *iphdr; struct tcp { unsigned short src; unsigned short dst; unsigned int seq; unsigned int ackn; unsigned short reserved; unsigned short window; unsigned short checksum; unsigned short urgptr; unsigned char data[]; } *tcphdr; } *

variable_types:
iphdr
struct ip *

variable_types:
tcphdr
struct tcp *

user_defined_types: struct tcp_segment { unsigned int seq; unsigned int ack; unsigned int lastseq; unsigned int len; unsigned int dlen; unsigned int wnd; unsigned int up; unsigned int prc; unsigned char *text; struct ip *iphdr; struct tcp *tcphdr; }


decompiled_code:
__int64 init_signal()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_0;
memset(&v1.sa_mask, 0, 0x90uLL);
return (unsigned int)(sigaction(2, &v1, 0LL) >> 31);
}

canonical_decompiled_code:
__int64 func13()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_0;
memset(&v1.sa_mask, 0, 0x90uLL);
return (unsigned int)(sigaction(2, &v1, 0LL) >> 31);
}

original_code:
static int init_signal(void)
{
 struct sigaction act = { };
 act.sa_flags = 0;
 act.__sigaction_handler.sa_handler = sigint;
 if (sigaction(2, &act, ((void *)0)) < 0)
  return -1;
 return 0;
}

canonical_original_code:
static int init_signal()
{
 struct sigaction act = { };
 act.sa_flags = 0;
 act.__sigaction_handler.sa_handler = sigint;
 if (sigaction(2, &act, ((void *)0)) < 0)
  return -1;
 return 0;
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
__int64 create_socket()
{
__int64 v0; // rax
unsigned int v1; // er12
const char *v3; // rbx
__int64 v4; // rax

LODWORD(v0) = socket(1, type, 0);
*(_QWORD *)&sock_0 = v0;
if ( v0 )
return 0;
v1 = -1;
if ( (flags & 0x10) != 0 )
{
v3 = (const char *)prostr;
v4 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): _socket error\n", v4, "create_socket", v3);
}
return v1;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 v0; // rax
unsigned int v1; // er12
const char *v3; // rbx
__int64 v4; // rax

LODWORD(v0) = socket(1, type, 0);
*(_QWORD *)&sock_0 = v0;
if ( v0 )
return 0;
v1 = -1;
if ( (flags & 0x10) != 0 )
{
v3 = (const char *)prostr;
v4 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): _socket error\n", v4, "create_socket", v3);
}
return v1;
}

original_code:
static int create_socket(void)
{
 sock = _socket(AF_INET, type, 0);
 if (!sock) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""_socket error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return -1;
 }
 return 0;
}

canonical_original_code:
static int create_socket()
{
 sock = _socket(AF_INET, type, 0);
 if (!sock) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""_socket error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return -1;
 }
 return 0;
}


decompiled_code:
int __fastcall recv_udp_packet(__int64 a1, void *a2, size_t a3, int a4)
{
int v4; // ebx
ssize_t v5; // rax
ssize_t v6; // rbp
int v7; // eax
__int16 *v8; // r12
unsigned int v9; // ebx
__int16 v10; // r13
const char *v11; // r15
unsigned int v12; // er14
__int64 v13; // rdx
const char *v14; // rbx
__int64 v15; // rax

while ( 1 )
{
v5 = recv(sock_0, a2, a3, a4);
if ( !v5 )
break;
v6 = v5;
v7 = 4 * (*(_BYTE *)(v5 + 66) & 0xF);
v8 = (__int16 *)(v6 + v7 + 66);
if ( (flags & 0x10) != 0 )
{
v9 = *(_DWORD *)(v6 + 78);
v10 = *v8;
v11 = (const char *)prostr;
v12 = *(unsigned __int16 *)(v6 + 68) - v7;
v13 = syscall(186LL);
fprintf(
stderr,
"[%d]%s (%s): ip: %d bytes from %d.%d.%d.%d:%d\n",
v13,
"recv_udp_packet",
v11,
v12,
(unsigned __int8)v9,
BYTE1(v9),
BYTE2(v9),
HIBYTE(v9),
(unsigned __int16)__ROL2__(v10, 8));
}
a2 = v8 + 4;
v4 = (unsigned __int16)__ROL2__(v8[2], 8) - 8;
if ( write(1, v8 + 4, v4) != v4 )
{
perror("write");
LODWORD(v5) = free_pkb(v6);
return v5;
}
free_pkb(v6);
}
if ( (flags & 0x10) != 0 )
{
v14 = (const char *)prostr;
v15 = syscall(186LL);
LODWORD(v5) = fprintf(stderr, "[%d]%s (%s): recv no pkb\n", v15, "recv_udp_packet", v14);
}
return v5;
}

canonical_decompiled_code:
int __fastcall func15(__int64 a1, void *a2, size_t a3, int a4)
{
int v4; // ebx
ssize_t v5; // rax
ssize_t v6; // rbp
int v7; // eax
__int16 *v8; // r12
unsigned int v9; // ebx
__int16 v10; // r13
const char *v11; // r15
unsigned int v12; // er14
__int64 v13; // rdx
const char *v14; // rbx
__int64 v15; // rax

while ( 1 )
{
v5 = recv(sock_0, a2, a3, a4);
if ( !v5 )
break;
v6 = v5;
v7 = 4 * (*(_BYTE *)(v5 + 66) & 0xF);
v8 = (__int16 *)(v6 + v7 + 66);
if ( (flags & 0x10) != 0 )
{
v9 = *(_DWORD *)(v6 + 78);
v10 = *v8;
v11 = (const char *)prostr;
v12 = *(unsigned __int16 *)(v6 + 68) - v7;
v13 = syscall(186LL);
fprintf(
stderr,
"[%d]%s (%s): ip: %d bytes from %d.%d.%d.%d:%d\n",
v13,
"recv_udp_packet",
v11,
v12,
(unsigned __int8)v9,
BYTE1(v9),
BYTE2(v9),
HIBYTE(v9),
(unsigned __int16)__ROL2__(v10, 8));
}
a2 = v8 + 4;
v4 = (unsigned __int16)__ROL2__(v8[2], 8) - 8;
if ( write(1, v8 + 4, v4) != v4 )
{
perror("write");
LODWORD(v5) = free_pkb(v6);
return v5;
}
free_pkb(v6);
}
if ( (flags & 0x10) != 0 )
{
v14 = (const char *)prostr;
v15 = syscall(186LL);
LODWORD(v5) = fprintf(stderr, "[%d]%s (%s): recv no pkb\n", v15, "recv_udp_packet", v14);
}
return v5;
}

original_code:
static void recv_udp_packet(void)
{
 struct pkbuf *pkb;
 struct ip *iphdr;
 struct udp *udphdr;
 int len;
 while (1) {
  pkb = _recv(sock);
  if (!pkb) {
   do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""recv no pkb" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
   break;
  }
  iphdr = ((struct ip *)((pkb)->pk_data + sizeof(struct ether)));
  udphdr = ((struct udp *)((unsigned char *)(iphdr) + ((iphdr)->ip_hlen << 2)));
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""ip: %d bytes from " "%d.%d.%d.%d" ":%d" "\n", (int)syscall(186), __FUNCTION__, prostr, ((iphdr)->ip_len - ((iphdr)->ip_hlen << 2)), (iphdr->ip_src) & 0xff, ((iphdr->ip_src) >> 8) & 0xff, ((iphdr->ip_src) >> 16) & 0xff, ((iphdr->ip_src) >> 24) & 0xff, _htons(udphdr->src));} while (0);
  len = _htons(udphdr->length) - sizeof(struct udp);
  if (write(1, udphdr->data, len) != len) {
   perror("write");
   free_pkb(pkb);
   break;
  }
  free_pkb(pkb);
 }
}

canonical_original_code:
static void recv_udp_packet()
{
 struct pkbuf *pkb;
 struct ip *iphdr;
 struct udp *udphdr;
 int len;
 while (1) {
  pkb = _recv(sock);
  if (!pkb) {
   do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""recv no pkb" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
   break;
  }
  iphdr = ((struct ip *)((pkb)->pk_data + sizeof(struct ether)));
  udphdr = ((struct udp *)((unsigned char *)(iphdr) + ((iphdr)->ip_hlen << 2)));
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""ip: %d bytes from " "%d.%d.%d.%d" ":%d" "\n", (int)syscall(186), __FUNCTION__, prostr, ((iphdr)->ip_len - ((iphdr)->ip_hlen << 2)), (iphdr->ip_src) & 0xff, ((iphdr->ip_src) >> 8) & 0xff, ((iphdr->ip_src) >> 16) & 0xff, ((iphdr->ip_src) >> 24) & 0xff, _htons(udphdr->src));} while (0);
  len = _htons(udphdr->length) - sizeof(struct udp);
  if (write(1, udphdr->data, len) != len) {
   perror("write");
   free_pkb(pkb);
   break;
  }
  free_pkb(pkb);
 }
}

variable_types: struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *pk_sk; unsigned char pk_data[]; } *

variable_types: struct ip { unsigned char ip_hlen; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_fragoff; unsigned char ip_ttl; unsigned char ip_pro; unsigned short ip_cksum; unsigned int ip_src; unsigned int ip_dst; unsigned char ip_data[]; } *

variable_types: struct udp { unsigned short src; unsigned short dst; unsigned short length; unsigned short checksum; unsigned char data[]; } *

variable_types: len int

user_defined_types: struct ether { unsigned char eth_dst[6]; unsigned char eth_src[6]; unsigned short eth_pro; unsigned char eth_data[]; }

user_defined_types: struct pkbuf { struct list_head pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev *pk_indev; struct rtentry *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; }

user_defined_types: struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops *net_ops; struct netstats net_stats; struct list_head net_list; }

user_defined_types: struct sock { unsigned char protocol; struct sock_addr sk_addr; struct socket *sock; struct sock_ops *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node hash_list; int refcnt; }

user_defined_types: struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait sleep; struct socket_ops *ops; struct sock *sk; int refcnt; }

user_defined_types: struct tapip_wait { union pthread_mutex_t mutex; union pthread_cond_t cond; int notified; int dead; int sleep; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int recv_tcp_packet()
{
__int64 v0; // rax
socklen_t *v1; // rdx
int v2; // edi
__int64 v3; // rbx
__int64 v4; // rax
const char *v5; // rbx
__int64 v6; // rax
__int64 v7; // rbx
__int64 v8; // rax
const char *v9; // rbx
__int64 v10; // rax
unsigned int v11; // ebp
__int16 v12; // bx
const char *v13; // r12
__int64 v14; // rax
const char *v15; // rbx
__int64 v16; // rax
unsigned int v18; // [rsp+Ch] [rbp-21Ch]
char buf[536]; // [rsp+10h] [rbp-218h] BYREF

if ( (flags & 0x10) != 0 )
{
v5 = (const char *)prostr;
v6 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): listen with backlog:10\n", v6, "recv_tcp_packet", v5);
}
LODWORD(v0) = listen(sock_0, 10);
if ( (int)v0 < 0 )
{
if ( (flags & 0x10) != 0 )
{
v3 = prostr;
v4 = syscall(186LL);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): listen error\n", v4, "recv_tcp_packet", v3);
}
}
else
{
LODWORD(v0) = accept(sock_0, &skaddr_0, v1);
*(_QWORD *)&csock = v0;
v2 = v0;
if ( v0 )
{
if ( (flags & 0x10) != 0 )
{
v11 = *(_DWORD *)&skaddr_0.sa_family;
v12 = *(_WORD *)&skaddr_0.sa_data[6];
v13 = (const char *)prostr;
v14 = syscall(186LL);
fprintf(
stderr,
"[%d]%s (%s): Three-way handshake successes: from %d.%d.%d.%d:%d\n\n",
v14,
"recv_tcp_packet",
v13,
(unsigned __int8)v11,
BYTE1(v11),
BYTE2(v11),
HIBYTE(v11),
(unsigned __int16)__ROL2__(v12, 8));
if ( (flags & 0x10) == 0 )
goto LABEL_8;
v15 = (const char *)prostr;
v16 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): starting _read()...\n", v16, "recv_tcp_packet", v15);
v2 = csock;
}
while ( 1 )
{
LODWORD(v0) = read(v2, buf, 0x200uLL);
if ( (int)v0 <= 0 )
break;
printf("%.*s", v0, buf);
fflush(stdout);
LABEL_8:
v2 = csock;
}
if ( (flags & 0x10) != 0 )
{
v18 = v0;
v9 = (const char *)prostr;
v10 = syscall(186LL, (unsigned int)v0);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): last _read() return %d\n", v10, "recv_tcp_packet", v9, v18);
}
}
else if ( (flags & 0x10) != 0 )
{
v7 = prostr;
v8 = syscall(186LL);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): Three-way handshake error\n", v8, "recv_tcp_packet", v7);
}
}
return v0;
}

canonical_decompiled_code:
int func16()
{
__int64 v0; // rax
socklen_t *v1; // rdx
int v2; // edi
__int64 v3; // rbx
__int64 v4; // rax
const char *v5; // rbx
__int64 v6; // rax
__int64 v7; // rbx
__int64 v8; // rax
const char *v9; // rbx
__int64 v10; // rax
unsigned int v11; // ebp
__int16 v12; // bx
const char *v13; // r12
__int64 v14; // rax
const char *v15; // rbx
__int64 v16; // rax
unsigned int v18; // [rsp+Ch] [rbp-21Ch]
char buf[536]; // [rsp+10h] [rbp-218h] BYREF

if ( (flags & 0x10) != 0 )
{
v5 = (const char *)prostr;
v6 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): listen with backlog:10\n", v6, "recv_tcp_packet", v5);
}
LODWORD(v0) = listen(sock_0, 10);
if ( (int)v0 < 0 )
{
if ( (flags & 0x10) != 0 )
{
v3 = prostr;
v4 = syscall(186LL);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): listen error\n", v4, "recv_tcp_packet", v3);
}
}
else
{
LODWORD(v0) = accept(sock_0, &skaddr_0, v1);
*(_QWORD *)&csock = v0;
v2 = v0;
if ( v0 )
{
if ( (flags & 0x10) != 0 )
{
v11 = *(_DWORD *)&skaddr_0.sa_family;
v12 = *(_WORD *)&skaddr_0.sa_data[6];
v13 = (const char *)prostr;
v14 = syscall(186LL);
fprintf(
stderr,
"[%d]%s (%s): Three-way handshake successes: from %d.%d.%d.%d:%d\n\n",
v14,
"recv_tcp_packet",
v13,
(unsigned __int8)v11,
BYTE1(v11),
BYTE2(v11),
HIBYTE(v11),
(unsigned __int16)__ROL2__(v12, 8));
if ( (flags & 0x10) == 0 )
goto LABEL_8;
v15 = (const char *)prostr;
v16 = syscall(186LL);
fprintf(stderr, "[%d]%s (%s): starting _read()...\n", v16, "recv_tcp_packet", v15);
v2 = csock;
}
while ( 1 )
{
LODWORD(v0) = read(v2, buf, 0x200uLL);
if ( (int)v0 <= 0 )
break;
printf("%.*s", v0, buf);
fflush(stdout);
LABEL_8:
v2 = csock;
}
if ( (flags & 0x10) != 0 )
{
v18 = v0;
v9 = (const char *)prostr;
v10 = syscall(186LL, (unsigned int)v0);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): last _read() return %d\n", v10, "recv_tcp_packet", v9, v18);
}
}
else if ( (flags & 0x10) != 0 )
{
v7 = prostr;
v8 = syscall(186LL);
LODWORD(v0) = fprintf(stderr, "[%d]%s (%s): Three-way handshake error\n", v8, "recv_tcp_packet", v7);
}
}
return v0;
}

original_code:
static void recv_tcp_packet(void)
{
 char buf[512];
 int len;
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""listen with backlog:10" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
 if (_listen(sock, 10) < 0) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""listen error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return;
 }
 csock = _accept(sock, &skaddr);
 if (!csock) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""Three-way handshake error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return;
 }
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""Three-way handshake successes: from ""%d.%d.%d.%d"":%d\n" "\n", (int)syscall(186), __FUNCTION__, prostr, (skaddr.src_addr) & 0xff, ((skaddr.src_addr) >> 8) & 0xff, ((skaddr.src_addr) >> 16) & 0xff, ((skaddr.src_addr) >> 24) & 0xff, _htons(skaddr.src_port));} while (0);
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""starting _read()..." "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
 while ((len = _read(csock, buf, 512)) > 0) {
  printf("%.*s", len, buf);
  fflush(stdout);
 }
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""last _read() return %d" "\n", (int)syscall(186), __FUNCTION__, prostr, len);} while (0);
}

canonical_original_code:
static void recv_tcp_packet()
{
 char buf[512];
 int len;
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""listen with backlog:10" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
 if (_listen(sock, 10) < 0) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""listen error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return;
 }
 csock = _accept(sock, &skaddr);
 if (!csock) {
  do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""Three-way handshake error" "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
  return;
 }
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""Three-way handshake successes: from ""%d.%d.%d.%d"":%d\n" "\n", (int)syscall(186), __FUNCTION__, prostr, (skaddr.src_addr) & 0xff, ((skaddr.src_addr) >> 8) & 0xff, ((skaddr.src_addr) >> 16) & 0xff, ((skaddr.src_addr) >> 24) & 0xff, _htons(skaddr.src_port));} while (0);
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""starting _read()..." "\n", (int)syscall(186), __FUNCTION__, prostr);} while (0);
 while ((len = _read(csock, buf, 512)) > 0) {
  printf("%.*s", len, buf);
  fflush(stdout);
 }
 do { if (flags & 16) fprintf(stderr, "[%d]%s " "(%s): ""last _read() return %d" "\n", (int)syscall(186), __FUNCTION__, prostr, len);} while (0);
}

variable_types:
buf
char [512]


snc

decompiled_code:
int __fastcall snc(__int64 a1, char **a2)
{
int result; // eax
socklen_t v3; // edx

init_options();
result = parse_args_0(a1, a2);
if ( result < 0 )
{
if ( result == -1 )
result = usage_0();
}
else
{
if ( (int)init_signal() >= 0 && (int)create_socket() >= 0 )
{
if ( (flags & 1) != 0 )
recv_packet_0(a1, (__int64)a2, v3);
else
send_packet_0(a1, (__int64)a2, v3);
}
result = close_socket_0();
}
return result;
}

canonical_decompiled_code:
int __fastcall func21(__int64 a1, char **a2)
{
int result; // eax
socklen_t v3; // edx

func10();
result = func11(a1, a2);
if ( result < 0 )
{
if ( result == -1 )
result = func12();
}
else
{
if ( (int)func13() >= 0 && (int)func14() >= 0 )
{
if ( (flags & 1) != 0 )
func17(a1, (__int64)a2, v3);
else
func18(a1, (__int64)a2, v3);
}
result = func19();
}
return result;
}

original_code:
void snc(int argc, char **argv)
{
 int err;
 init_options();
 if ((err = parse_args(argc, argv)) < 0) {
  if (err == -1)
   usage();
  return;
 }
 if (init_signal() < 0)
  goto out;
 if (create_socket() < 0)
  goto out;
 if (flags & 1)
  recv_packet();
 else
  send_packet();
out:
 close_socket();
}

variable_types: argc int

variable_types:
argv
char **

variable_types: err int


decompiled_code:
__int64 __fastcall inet_listen(__int64 a1)
{
__int64 v1; // r8
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 128);
if ( *(_DWORD *)(a1 + 8) == 1 && v1 )
result = (*(__int64 (__fastcall **)(_QWORD))(*(_QWORD *)(v1 + 21) + 96LL))(*(_QWORD *)(a1 + 128));
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // r8
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 128);
if ( *(_DWORD *)(a1 + 8) == 1 && v1 )
result = (*(__int64 (__fastcall **)(_QWORD))(*(_QWORD *)(v1 + 21) + 96LL))(*(_QWORD *)(a1 + 128));
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int inet_listen(struct socket *sock, int backlog)
{
 struct sock *sk = sock->sk;
 int err = -1;
 if (sock->type != SOCK_STREAM)
  return -1;
 if (sk)
  err = sk->ops->listen(sk, backlog);
 return err;
}

variable_types: struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock { unsigned char protocol; struct sock_addr sk_addr; struct socket *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *pk_sk; unsigned char pk_data[]; } *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *

variable_types: backlog int

variable_types: struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; } *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *

user_defined_types: struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); }


original_code:
static int inet_read(struct socket *sock, void *buf, int len)
{
 struct sock *sk = sock->sk;
 int ret = -1;
 if (sk) {
  sk->recv_wait = &sock->sleep;
  ret = sk->ops->recv_buf(sock->sk, buf, len);
  sk->recv_wait = ((void *)0);
 }
 return ret;
}

variable_types:
buf
void *

variable_types: ret int

user_defined_types: struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; }

user_defined_types: struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; }

user_defined_types: struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

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

user_defined_types: struct list_head { struct list_head *prev; }

user_defined_types: struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; }

user_defined_types: struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; }

user_defined_types: struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); }


decompiled_code:
__int64 __fastcall inet_write(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rdi
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 128);
if ( v3 )
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))(*(_QWORD *)(v3 + 21) + 24LL))(
v3,
a2,
a3,
0LL);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rdi
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 128);
if ( v3 )
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))(*(_QWORD *)(v3 + 21) + 24LL))(
v3,
a2,
a3,
0LL);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int inet_write(struct socket *sock, void *buf, int len)
{
 struct sock *sk = sock->sk;
 int ret = -1;
 if (sk)
  ret = sk->ops->send_buf(sock->sk, buf, len, ((void *)0));
 return ret;
}

user_defined_types: struct pkbuf { struct list_head pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev *pk_indev; struct rtentry *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; }

user_defined_types: struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops *net_ops; struct netstats net_stats; struct list_head net_list; }


decompiled_code:
__int64 __fastcall inet_send(__int64 a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 128);
if ( v1 )
result = (*(__int64 (**)(void))(*(_QWORD *)(v1 + 21) + 24LL))();
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 128);
if ( v1 )
result = (*(__int64 (**)(void))(*(_QWORD *)(v1 + 21) + 24LL))();
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int inet_send(struct socket *sock, void *buf, int size,
   struct sock_addr *skaddr)
{
 struct sock *sk = sock->sk;
 if (sk)
  return sk->ops->send_buf(sock->sk, buf, size, skaddr);
 return -1;
}

variable_types: size int

variable_types:
skaddr
struct sock_addr *

user_defined_types: struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait sleep; struct socket_ops *ops; struct sock *sk; int refcnt; }

user_defined_types: struct sock { unsigned char protocol; struct sock_addr sk_addr; struct socket *sock; struct sock_ops *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node hash_list; int refcnt; }


original_code:
static struct pkbuf *inet_recv(struct socket *sock)
{
 struct sock *sk = sock->sk;
 struct pkbuf *pkb = ((void *)0);
 if (sk) {
  sk->recv_wait = &sock->sleep;
  pkb = sk->ops->recv(sock->sk);
  sk->recv_wait = ((void *)0);
 }
 return pkb;
}

variable_types:
sock
struct socket *

return_type: struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *pk_sk; unsigned char pk_data[]; } *

user_defined_types: struct tapip_wait { union pthread_mutex_t mutex; union pthread_cond_t cond; int notified; int dead; int sleep; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }


original_code:
static int inet_connect(struct socket *sock, struct sock_addr *skaddr)
{
 struct sock *sk = sock->sk;
 int err = -1;
 if (!skaddr->dst_port || !skaddr->dst_addr)
  goto out;
 if (sk->sk_addr.dst_port)
  goto out;
 if (!sk->sk_addr.src_port && sock_autobind(sk) < 0)
  goto out;
 {
  struct rtentry *rt = rt_lookup(skaddr->dst_addr);
  if (!rt)
   goto out;
  sk->sk_dst = rt;
  sk->sk_addr.src_addr = sk->sk_dst->rt_dev->net_ipaddr;
 }
 if (sk->ops->connect)
  err = sk->ops->connect(sk, skaddr);
out:
 return err;
}

variable_types: struct rtentry { struct list_head { struct list_head *prev; } rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf { struct list_head pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev *pk_indev; struct rtentry *pk_rtdst; struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *pk_sk; unsigned char pk_data[]; } *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *rt_dev; } *

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


original_code:
static int inet_bind(struct socket *sock, struct sock_addr *skaddr)
{
 struct sock *sk = sock->sk;
 int err = -1;
 if (sk->ops->bind)
  return sk->ops->bind(sock->sk, skaddr);
 if (sk->sk_addr.src_port)
  goto err_out;
 if (!local_address(skaddr->src_addr))
  goto err_out;
 sk->sk_addr.src_addr = skaddr->src_addr;
 if (sk->ops->set_port) {
  err = sk->ops->set_port(sk, skaddr->src_port);
  if (err < 0) {
   sk->sk_addr.src_addr = 0;
   goto err_out;
  }
 } else {
  sk->sk_addr.src_port = skaddr->src_port;
 }
 err = 0;
 sk->sk_addr.dst_addr = 0;
 sk->sk_addr.dst_port = 0;
err_out:
 return err;
}

user_defined_types: struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; }

user_defined_types: struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); }


original_code:
static int inet_accept(struct socket *sock,
  struct socket *newsock, struct sock_addr *skaddr)
{
 struct sock *sk = sock->sk;
 struct sock *newsk;
 int err = -1;
 if (!sk)
  goto out;
 newsk = sk->ops->accept(sk);
 if (newsk) {
  newsock->sk = get_sock(newsk);
  if (skaddr) {
   skaddr->src_addr = newsk->sk_addr.dst_addr;
   skaddr->src_port = newsk->sk_addr.dst_port;
  }
  err = 0;
 }
out:
 return err;
}

variable_types:
newsock
struct socket *

user_defined_types: struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; }

user_defined_types: struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); }


original_code:
static int inet_socket(struct socket *sock, int protocol)
{
 struct inet_type *inet;
 struct sock *sk;
 int type;
 type = sock->type;
 if (type >= SOCK_MAX)
  return -1;
 inet = &inet_type_table[type];
 sk = inet->alloc_sock(protocol);
 if (!sk)
  return -1;
 if (!protocol)
  protocol = inet->protocol;
 sock->sk = get_sock(sk);
 list_init(&sk->recv_queue);
 hlist_node_init(&sk->hash_list);
 sk->protocol = protocol;
 sk->sock = sock;
 if (sk->hash && sk->ops->hash)
  sk->ops->hash(sk);
 return 0;
}

variable_types: protocol int

variable_types: struct inet_type { struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; } *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *(*alloc_sock)(int); int type; int protocol; } *

variable_types: type int

user_defined_types: struct inet_type { struct sock *(*alloc_sock)(int); int type; int protocol; }


decompiled_code:
__int64 __fastcall inet_close(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // er12

v1 = *(_QWORD *)(a1 + 128);
if ( !v1 )
return (unsigned int)-1;
v2 = (*(__int64 (__fastcall **)(_QWORD))(*(_QWORD *)(v1 + 21) + 88LL))(*(_QWORD *)(a1 + 128));
free_sock(v1);
*(_QWORD *)(a1 + 128) = 0LL;
return v2;
}

original_code:
static int inet_close(struct socket *sock)
{
 struct sock *sk = sock->sk;
 int err = -1;
 if (sk) {
  err = sk->ops->close(sk);
  free_sock(sk);
  sock->sk = ((void *)0);
 }
 return err;
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

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

user_defined_types: struct list_head { struct list_head *prev; }


decompiled_code:
__int64 inet_init()
{
raw_init();
udp_init();
return tcp_init();
}

canonical_decompiled_code:
__int64 func10()
{
raw_init();
udp_init();
return tcp_init();
}

original_code:
void inet_init(void)
{
 raw_init();
 udp_init();
 tcp_init();
}

canonical_original_code:
void inet_init()
{
 raw_init();
 udp_init();
 tcp_init();
}


decompiled_code:
// attributes: thunk
__int64 socket_init()
{
return inet_init();
}

canonical_decompiled_code:
__int64 func14()
{
return inet_init();
}

original_code:
void socket_init(void)
{
 inet_init();
}

canonical_original_code:
void socket_init()
{
 inet_init();
}


perrx

decompiled_code:
void __fastcall __noreturn perrx(char *s)
{
if ( *_errno_location() )
perror(s);
else
fprintf(stderr, "ERROR:%s\n", s);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(char *s)
{
if ( *_errno_location() )
perror(s);
else
fprintf(stderr, "ERROR:%s\n", s);
exit(1);
}

original_code:
void perrx(char *str)
{
 if ((*__errno_location ()))
  perror(str);
 else
  fprintf(stderr, "ERROR:%s\n", str);
 exit(1);
}

variable_types:
str
char *


xmalloc

decompiled_code:
void *__fastcall xmalloc(int a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
perrx("malloc");
return result;
}

canonical_decompiled_code:
void *__fastcall func1(int a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
func0("malloc");
return result;
}

original_code:
void *xmalloc(int size)
{
 void *p = malloc(size);
 if (!p)
  perrx("malloc");
 return p;
}

variable_types:
p
void *

return_type: void *


xzalloc

decompiled_code:
void *__fastcall xzalloc(int a1)
{
void *result; // rax

result = calloc(1uLL, a1);
if ( !result )
perrx("calloc");
return result;
}

canonical_decompiled_code:
void *__fastcall func2(int a1)
{
void *result; // rax

result = calloc(1uLL, a1);
if ( !result )
func0("calloc");
return result;
}

original_code:
void *xzalloc(int size)
{
 void *p = calloc(1, size);
 if (!p)
  perrx("calloc");
 return p;
}


printfs

decompiled_code:
int printfs(int a1, const char *a2, ...)
{
int v2; // ebp
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-1E0h] BYREF
char s[256]; // [rsp+20h] [rbp-1C8h] BYREF

va_start(arg, a2);
v2 = vsprintf(s, a2, arg);
result = printf("%.*s", a1, s);
if ( v2 < a1 )
result = printf("%.*s", a1 - v2, " ");
return result;
}

canonical_decompiled_code:
int func3(int a1, const char *a2, ...)
{
int v2; // ebp
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-1E0h] BYREF
char s[256]; // [rsp+20h] [rbp-1C8h] BYREF

va_start(arg, a2);
v2 = vsprintf(s, a2, arg);
result = printf("%.*s", a1, s);
if ( v2 < a1 )
result = printf("%.*s", a1 - v2, " ");
return result;
}

original_code:
void printfs(int mlen, const char *fmt, ...)
{
 char buf[256];
 va_list ap;
 int slen;
 __builtin_va_start(ap,fmt);
 slen = vsprintf(buf, fmt, ap);
 __builtin_va_end(ap);
 printf("%.*s", mlen, buf);
 if (mlen > slen)
  printf("%.*s", mlen - slen, _space);
}

canonical_original_code:
void printfs(int mlen, char *fmt, ...)
{
 char buf[256];
 __builtin_va_list ap;
 int slen;
 __builtin_va_start(ap,fmt);
 slen = vsprintf(buf, fmt, ap);
 __builtin_va_end(ap);
 printf("%.*s", mlen, buf);
 if (mlen > slen)
  printf("%.*s", mlen - slen, _space);
}

variable_types: mlen int

variable_types:
fmt
char *

variable_types:
buf
char [256]

variable_types: slen int


decompiled_code:
__int64 __fastcall str2ip(__int64 a1, unsigned int *a2)
{
unsigned int v3; // [rsp+0h] [rbp-18h] BYREF
unsigned int v4; // [rsp+4h] [rbp-14h] BYREF
unsigned int v5; // [rsp+8h] [rbp-10h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%u.%u.%u.%u", &v3, &v4, &v5, &v6) != 4
|| v3 > 0xFF
|| v4 > 0xFF
|| v5 > 0xFF
|| v6 > 0xFF )
{
return 0xFFFFFFFFLL;
}
*a2 = (v6 << 24) | v3 | (v5 << 16) | (v4 << 8);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int *a2)
{
unsigned int v3; // [rsp+0h] [rbp-18h] BYREF
unsigned int v4; // [rsp+4h] [rbp-14h] BYREF
unsigned int v5; // [rsp+8h] [rbp-10h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)_isoc99_sscanf(a1, "%u.%u.%u.%u", &v3, &v4, &v5, &v6) != 4
|| v3 > 0xFF
|| v4 > 0xFF
|| v5 > 0xFF
|| v6 > 0xFF )
{
return 0xFFFFFFFFLL;
}
*a2 = (v6 << 24) | v3 | (v5 << 16) | (v4 << 8);
return 0LL;
}

original_code:
int str2ip(char *str, unsigned int *ip)
{
 unsigned int a, b, c, d;
 if (sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  return -1;
 if (a > 255 || b > 255 || c > 255 || d > 255)
  return -1;
 *ip = a | (b << 8) | (c << 16) | (d << 24);
 return 0;
}

canonical_original_code:
int str2ip(char *str, unsigned int *ip)
{
 unsigned int a; unsigned int b; unsigned int c; unsigned int d;
 if (sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  return -1;
 if (a > 255 || b > 255 || c > 255 || d > 255)
  return -1;
 *ip = a | (b << 8) | (c << 16) | (d << 24);
 return 0;
}

variable_types:
ip
unsigned int *

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
c
unsigned int

variable_types:
d
unsigned int


decompiled_code:
__int64 __fastcall parse_ip_port(const char *a1, unsigned int *a2, _WORD *a3)
{
char *v4; // rax
char *v5; // rbx

v4 = strchr(a1, 58);
if ( !v4 )
return (unsigned int)((int)str2ip((__int64)a1, a2) >> 31);
v5 = v4;
*a3 = __ROL2__(atoi(v4 + 1), 8);
*v5 = 0;
if ( (int)str2ip((__int64)a1, a2) < 0 )
return 0xFFFFFFFFLL;
*v5 = 58;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, unsigned int *a2, _WORD *a3)
{
char *v4; // rax
char *v5; // rbx

v4 = strchr(a1, 58);
if ( !v4 )
return (unsigned int)((int)func4((__int64)a1, a2) >> 31);
v5 = v4;
*a3 = __ROL2__(atoi(v4 + 1), 8);
*v5 = 0;
if ( (int)func4((__int64)a1, a2) < 0 )
return 0xFFFFFFFFLL;
*v5 = 58;
return 0LL;
}

original_code:
int parse_ip_port(char *str, unsigned int *addr, unsigned short *nport)
{
 char *port;
 if ((port = strchr(str, ':')) != ((void *)0)) {
  *nport = _htons(atoi(&port[1]));
  *port = '\0';
 }
 if (str2ip(str, addr) < 0)
  return -1;
 if (port)
  *port = ':';
 return 0;
}

variable_types:
addr
unsigned int *

variable_types:
nport
unsigned short *

variable_types:
port
char *


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

result = netdev_alloc("lo", &loop_ops);
loop = result;
return result;
}

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

result = netdev_alloc("lo", &loop_ops);
loop = result;
return result;
}

original_code:
void loop_init(void)
{
 loop = netdev_alloc("lo", &loop_ops);
}

canonical_original_code:
void loop_init()
{
 loop = netdev_alloc("lo", &loop_ops);
}


decompiled_code:
__int64 loop_exit()
{
return netdev_free(loop);
}

canonical_decompiled_code:
__int64 func4()
{
return netdev_free(loop);
}

original_code:
void loop_exit(void)
{
 netdev_free(loop);
}

canonical_original_code:
void loop_exit()
{
 netdev_free(loop);
}


original_code:
static inline void arp_hton(struct arp *ahdr)
{
 ahdr->arp_hrd = _htons(ahdr->arp_hrd);
 ahdr->arp_pro = _htons(ahdr->arp_pro);
 ahdr->arp_op = _htons(ahdr->arp_op);
}

variable_types: struct arp { unsigned short arp_hrd; unsigned short arp_pro; unsigned char arp_hrdlen; unsigned char arp_prolen; unsigned short arp_op; unsigned char arp_sha[6]; unsigned int arp_sip; unsigned char arp_tha[6]; unsigned int arp_tip; } *


hwacpy

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

*(_DWORD *)a1 = *(_DWORD *)a2;
result = *(unsigned __int16 *)(a2 + 4);
*(_WORD *)(a1 + 4) = result;
return result;
}

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

*(_DWORD *)a1 = *(_DWORD *)a2;
result = *(unsigned __int16 *)(a2 + 4);
*(_WORD *)(a1 + 4) = result;
return result;
}

original_code:
static inline void hwacpy(void *dst, void *src)
{
 memcpy(dst, src, 6);
}

variable_types:
dst
void *

variable_types:
src
void *


hwacmp

decompiled_code:
int __fastcall hwacmp(const void *a1, const void *a2)
{
return memcmp(a1, a2, 6uLL);
}

canonical_decompiled_code:
int __fastcall func2(const void *a1, const void *a2)
{
return memcmp(a1, a2, 6uLL);
}

original_code:
static inline int hwacmp(void *hwa1, void *hwa2)
{
 return memcmp(hwa1, hwa2, 6);
}


decompiled_code:
int arp_cache_init()
{
int *v0; // rax
__int64 v1; // rax
__int64 v2; // rax
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

v0 = dword_1500;
do
{
*v0 = 1;
v0 += 14;
}
while ( v0 != (int *)&perrx );
v1 = syscall(186LL);
fprintf(stderr, "[%d]%s ARP CACHE INIT\n", v1, "arp_cache_init");
if ( pthread_mutexattr_init(&attr) )
{
perrx("pthread_mutexattr_init");
if ( !pthread_mutexattr_settype(&attr, 0) )
goto LABEL_5;
}
else if ( !pthread_mutexattr_settype(&attr, 0) )
{
goto LABEL_5;
}
perrx("pthread_mutexattr_settype");
LABEL_5:
if ( pthread_mutex_init(&arp_cache_mutex, &attr) )
perrx("pthread_mutex_init");
v2 = syscall(186LL);
return fprintf(stderr, "[%d]%s ARP CACHE SEMAPHORE INIT\n", v2, "arp_cache_init");
}

canonical_decompiled_code:
int func13()
{
int *v0; // rax
__int64 v1; // rax
__int64 v2; // rax
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

v0 = dword_1500;
do
{
*v0 = 1;
v0 += 14;
}
while ( v0 != (int *)&perrx );
v1 = syscall(186LL);
fprintf(stderr, "[%d]%s ARP CACHE INIT\n", v1, "arp_cache_init");
if ( pthread_mutexattr_init(&attr) )
{
perrx("pthread_mutexattr_init");
if ( !pthread_mutexattr_settype(&attr, 0) )
goto LABEL_5;
}
else if ( !pthread_mutexattr_settype(&attr, 0) )
{
goto LABEL_5;
}
perrx("pthread_mutexattr_settype");
LABEL_5:
if ( pthread_mutex_init(&arp_cache_mutex, &attr) )
perrx("pthread_mutex_init");
v2 = syscall(186LL);
return fprintf(stderr, "[%d]%s ARP CACHE SEMAPHORE INIT\n", v2, "arp_cache_init");
}

original_code:
void arp_cache_init(void)
{
 int i;
 for (i = 0; i < 20; i++)
  arp_cache[i].ae_state = 1;
 fprintf(stderr, "[%d]%s " "ARP CACHE INIT" "\n", (int)syscall(186), __FUNCTION__);
 arp_cache_lock_init();
 fprintf(stderr, "[%d]%s " "ARP CACHE SEMAPHORE INIT" "\n", (int)syscall(186), __FUNCTION__);
}

canonical_original_code:
void arp_cache_init()
{
 int i;
 for (i = 0; i < 20; i++)
  arp_cache[i].ae_state = 1;
 fprintf(stderr, "[%d]%s " "ARP CACHE INIT" "\n", (int)syscall(186), __FUNCTION__);
 arp_cache_lock_init();
 fprintf(stderr, "[%d]%s " "ARP CACHE SEMAPHORE INIT" "\n", (int)syscall(186), __FUNCTION__);
}

variable_types: i int


ipnfmt

decompiled_code:
char *__fastcall ipnfmt(unsigned int a1)
{
snprintf(ipbuf_0, 0x10uLL, "%d.%d.%d.%d", (unsigned __int8)a1, BYTE1(a1), BYTE2(a1), HIBYTE(a1));
return ipbuf_0;
}

canonical_decompiled_code:
char *__fastcall func14(unsigned int a1)
{
snprintf(ipbuf_0, 0x10uLL, "%d.%d.%d.%d", (unsigned __int8)a1, BYTE1(a1), BYTE2(a1), HIBYTE(a1));
return ipbuf_0;
}

original_code:
char *ipnfmt(unsigned int ipaddr)
{
 static char ipbuf[16];
 snprintf(ipbuf, 16, "%d.%d.%d.%d", (ipaddr) & 0xff, ((ipaddr) >> 8) & 0xff, ((ipaddr) >> 16) & 0xff, ((ipaddr) >> 24) & 0xff);
 return ipbuf;
}

variable_types:
ipaddr
unsigned int

variable_types:
ipbuf
char [16]


decompiled_code:
__int64 udp_get_port_slow()
{
int v0; // ecx
__int64 v1; // rsi
__int64 i; // rax
unsigned __int16 v3; // cx
int v4; // er9
unsigned int v5; // er8
_QWORD *v6; // rax
__int16 v7; // dx
__int64 result; // rax

v0 = dword_D28[4 * (int)qword_1520];
v1 = (int)qword_1520;
for ( i = 0LL; i != 128; ++i )
{
if ( dword_D28[4 * i] < v0 )
{
v0 = dword_D28[4 * i];
v1 = (int)i;
}
}
if ( (_DWORD)v1 == (_DWORD)qword_1520 )
return 0LL;
LODWORD(qword_1520) = v1;
v3 = v1 + 0x8000;
HIDWORD(qword_1520) = 10;
v4 = dword_D28[4 * v1];
if ( v4 )
{
if ( v3 <= 0xEFFFu )
{
while ( 1 )
{
v5 = v3;
if ( !udp_table[2 * v1] )
goto LABEL_15;
v6 = (_QWORD *)udp_table[2 * v1];
v7 = __ROL2__(v3, 8);
while ( v7 != *((_WORD *)v6 - 28) )
{
v6 = (_QWORD *)*v6;
if ( !v6 )
goto LABEL_15;
}
v3 += 128;
if ( v3 > 0xEFFFu )
return 0LL;
}
}
return 0LL;
}
v5 = v3;
LABEL_15:
result = v5;
dword_D28[4 * v1] = v4 + 1;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // ecx
__int64 v1; // rsi
__int64 i; // rax
unsigned __int16 v3; // cx
int v4; // er9
unsigned int v5; // er8
_QWORD *v6; // rax
__int16 v7; // dx
__int64 result; // rax

v0 = dword_D28[4 * (int)qword_1520];
v1 = (int)qword_1520;
for ( i = 0LL; i != 128; ++i )
{
if ( dword_D28[4 * i] < v0 )
{
v0 = dword_D28[4 * i];
v1 = (int)i;
}
}
if ( (_DWORD)v1 == (_DWORD)qword_1520 )
return 0LL;
LODWORD(qword_1520) = v1;
v3 = v1 + 0x8000;
HIDWORD(qword_1520) = 10;
v4 = dword_D28[4 * v1];
if ( v4 )
{
if ( v3 <= 0xEFFFu )
{
while ( 1 )
{
v5 = v3;
if ( !udp_table[2 * v1] )
goto LABEL_15;
v6 = (_QWORD *)udp_table[2 * v1];
v7 = __ROL2__(v3, 8);
while ( v7 != *((_WORD *)v6 - 28) )
{
v6 = (_QWORD *)*v6;
if ( !v6 )
goto LABEL_15;
}
v3 += 128;
if ( v3 > 0xEFFFu )
return 0LL;
}
}
return 0LL;
}
v5 = v3;
LABEL_15:
result = v5;
dword_D28[4 * v1] = v4 + 1;
return result;
}

original_code:
static int udp_get_port_slow(void)
{
 int best_slot = udp_table.best_slot;
 int best_used = (&udp_table.slot[udp_table.best_slot])->used;
 int i;
 for (i = 0; i < 128; i++) {
  if ((&udp_table.slot[i])->used < best_used) {
   best_used = (&udp_table.slot[i])->used;
   best_slot = i;
  }
 }
 if (best_slot == udp_table.best_slot)
  return 0;
 udp_table.best_slot = best_slot;
 udp_table.best_update = 10;
 return udp_get_best_port();
}

canonical_original_code:
static int udp_get_port_slow()
{
 int best_slot = udp_table.best_slot;
 int best_used = (&udp_table.slot[udp_table.best_slot])->used;
 int i;
 for (i = 0; i < 128; i++) {
  if ((&udp_table.slot[i])->used < best_used) {
   best_used = (&udp_table.slot[i])->used;
   best_slot = i;
  }
 }
 if (best_slot == udp_table.best_slot)
  return 0;
 udp_table.best_slot = best_slot;
 udp_table.best_update = 10;
 return udp_get_best_port();
}


decompiled_code:
__int16 udp_get_port()
{
unsigned __int16 v0; // cx
int v1; // edi
_QWORD *v2; // rax
__int16 v3; // dx
__int16 result; // ax
__int64 v5; // rax

v0 = qword_1520 + 0x8000;
v1 = dword_D28[4 * (int)qword_1520];
if ( v1 )
{
if ( v0 > 0xEFFFu )
return udp_get_port_slow();
while ( udp_table[2 * (int)qword_1520] )
{
v2 = (_QWORD *)udp_table[2 * (int)qword_1520];
v3 = __ROL2__(v0, 8);
while ( *((_WORD *)v2 - 28) != v3 )
{
v2 = (_QWORD *)*v2;
if ( !v2 )
goto LABEL_9;
}
v0 += 128;
if ( v0 > 0xEFFFu )
return udp_get_port_slow();
}
}
LABEL_9:
dword_D28[4 * (int)qword_1520] = v1 + 1;
if ( !v0 )
return udp_get_port_slow();
if ( HIDWORD(qword_1520) - 1 <= 0 )
{
HIDWORD(qword_1520) = 10;
v5 = 0LL;
while ( dword_D28[4 * v5] >= v1 + 1 )
{
if ( ++v5 == 128 )
goto LABEL_12;
}
LODWORD(qword_1520) = v5;
result = __ROL2__(v0, 8);
}
else
{
--HIDWORD(qword_1520);
LABEL_12:
result = __ROL2__(v0, 8);
}
return result;
}

canonical_decompiled_code:
__int16 func3()
{
unsigned __int16 v0; // cx
int v1; // edi
_QWORD *v2; // rax
__int16 v3; // dx
__int16 result; // ax
__int64 v5; // rax

v0 = qword_1520 + 0x8000;
v1 = dword_D28[4 * (int)qword_1520];
if ( v1 )
{
if ( v0 > 0xEFFFu )
return func2();
while ( udp_table[2 * (int)qword_1520] )
{
v2 = (_QWORD *)udp_table[2 * (int)qword_1520];
v3 = __ROL2__(v0, 8);
while ( *((_WORD *)v2 - 28) != v3 )
{
v2 = (_QWORD *)*v2;
if ( !v2 )
goto LABEL_9;
}
v0 += 128;
if ( v0 > 0xEFFFu )
return func2();
}
}
LABEL_9:
dword_D28[4 * (int)qword_1520] = v1 + 1;
if ( !v0 )
return func2();
if ( HIDWORD(qword_1520) - 1 <= 0 )
{
HIDWORD(qword_1520) = 10;
v5 = 0LL;
while ( dword_D28[4 * v5] >= v1 + 1 )
{
if ( ++v5 == 128 )
goto LABEL_12;
}
LODWORD(qword_1520) = v5;
result = __ROL2__(v0, 8);
}
else
{
--HIDWORD(qword_1520);
LABEL_12:
result = __ROL2__(v0, 8);
}
return result;
}

original_code:
static unsigned short udp_get_port(void)
{
 struct hash_slot *best = (&udp_table.slot[udp_table.best_slot]);
 unsigned short port;
 port = udp_get_best_port();
 if (port == 0)
  return udp_get_port_slow();
 if (--udp_table.best_update <= 0) {
  int i;
  udp_table.best_update = 10;
  for (i = 0; i < 128; i++) {
   if (udp_table.slot[i].used < best->used) {
    udp_table.best_slot = i;
    break;
   }
  }
 }
 return _htons(port);
}

canonical_original_code:
static unsigned short udp_get_port()
{
 struct hash_slot *best = (&udp_table.slot[udp_table.best_slot]);
 unsigned short port;
 port = udp_get_best_port();
 if (port == 0)
  return udp_get_port_slow();
 if (--udp_table.best_update <= 0) {
  int i;
  udp_table.best_update = 10;
  for (i = 0; i < 128; i++) {
   if (udp_table.slot[i].used < best->used) {
    udp_table.best_slot = i;
    break;
   }
  }
 }
 return _htons(port);
}

variable_types: struct hash_slot { struct hlist_head { struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } *first; } head; int used; } *

variable_types:
port
unsigned short

return_type: unsigned short

user_defined_types: struct hash_slot { struct hlist_head head; int used; }

user_defined_types: struct hlist_head { struct hlist_node *first; }


decompiled_code:
__int64 __fastcall udp_unset_sport(__int64 a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = *(unsigned int *)(a1 + 61);
--dword_D28[4 * v1];
result = (int)qword_1520;
if ( (_DWORD)v1 != (_DWORD)qword_1520 )
{
result = (unsigned int)dword_D28[4 * (int)qword_1520];
if ( dword_D28[4 * (int)v1] < (int)result )
LODWORD(qword_1520) = v1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = *(unsigned int *)(a1 + 61);
--dword_D28[4 * v1];
result = (int)qword_1520;
if ( (_DWORD)v1 != (_DWORD)qword_1520 )
{
result = (unsigned int)dword_D28[4 * (int)qword_1520];
if ( dword_D28[4 * (int)v1] < (int)result )
LODWORD(qword_1520) = v1;
}
return result;
}

original_code:
static void udp_unset_sport(struct sock *sk)
{
 struct hash_slot *slot = (&udp_table.slot[sk->hash]);
 slot->used--;
 udp_update_best(sk->hash);
}

variable_types: struct sock { unsigned char protocol; struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; } sk_addr; struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; int notified; int dead; int sleep; } sleep; struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf { struct list_head { struct list_head *prev; } pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); } *net_ops; struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; } net_stats; struct list_head net_list; } *pk_indev; struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; } *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; } *(*recv)(struct socket *); } *ops; struct sock *sk; int refcnt; } *sock; struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); } *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } hash_list; int refcnt; } *

user_defined_types: struct pkbuf { struct list_head pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev *pk_indev; struct rtentry *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; }

user_defined_types: struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops *net_ops; struct netstats net_stats; struct list_head net_list; }


decompiled_code:
__int64 __fastcall udp_set_sport(__int64 a1, __int16 a2)
{
__int16 v2; // r12
__int16 v3; // ax
int v4; // ecx
__int64 v5; // rax
unsigned int v6; // er12
void (__fastcall *v7)(__int64); // rax
unsigned __int16 v9; // bx
_QWORD *v10; // rax

v2 = a2;
pthread_mutex_lock(&mutex);
if ( !a2 )
{
v3 = udp_get_port();
v2 = v3;
if ( v3 )
{
v4 = __ROL2__(v3, 8) & 0x7F;
goto LABEL_4;
}
LABEL_14:
v6 = -1;
goto LABEL_9;
}
v9 = __ROL2__(a2, 8);
v4 = v9 & 0x7F;
v10 = (_QWORD *)udp_table[2 * (v9 & 0x7F)];
if ( v10 )
{
while ( *((_WORD *)v10 - 28) != (HIBYTE(v9) | (unsigned __int16)(v9 << 8)) )
{
v10 = (_QWORD *)*v10;
if ( !v10 )
goto LABEL_4;
}
goto LABEL_14;
}
LABEL_4:
if ( (_DWORD)qword_1520 != v4 && dword_D28[4 * v4] < dword_D28[4 * (int)qword_1520] )
LODWORD(qword_1520) = v4;
v5 = *(_QWORD *)(a1 + 21);
*(_WORD *)(a1 + 9) = v2;
v6 = 0;
*(_DWORD *)(a1 + 61) = v4;
v7 = *(void (__fastcall **)(__int64))(v5 + 48);
if ( v7 )
v7(a1);
LABEL_9:
pthread_mutex_unlock(&mutex);
return v6;
}

original_code:
static int udp_set_sport(struct sock *sk, unsigned short nport)
{
 int hash, err = -1;
 udp_htable_lock();
 if ((nport && port_used(_htons(nport))) ||
  (!nport && !(nport = udp_get_port())))
  goto unlock;
 err = 0;
 hash = _htons(nport) & (128 - 1);
 udp_update_best(hash);
 sk->hash = hash;
 sk->sk_addr.src_port = nport;
 if (sk->ops->hash)
  sk->ops->hash(sk);
unlock:
 udp_htable_unlock();
 return err;
}

canonical_original_code:
static int udp_set_sport(struct sock *sk, unsigned short nport)
{
 int hash; int err = -1;
 udp_htable_lock();
 if ((nport && port_used(_htons(nport))) ||
  (!nport && !(nport = udp_get_port())))
  goto unlock;
 err = 0;
 hash = _htons(nport) & (128 - 1);
 udp_update_best(hash);
 sk->hash = hash;
 sk->sk_addr.src_port = nport;
 if (sk->ops->hash)
  sk->ops->hash(sk);
unlock:
 udp_htable_unlock();
 return err;
}

variable_types:
nport
unsigned short

variable_types: hash int

variable_types: err int

user_defined_types: struct sock { unsigned char protocol; struct sock_addr sk_addr; struct socket *sock; struct sock_ops *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node hash_list; int refcnt; }

user_defined_types: struct socket { unsigned int state; unsigned int family; unsigned int type; struct tapip_wait sleep; struct socket_ops *ops; struct sock *sk; int refcnt; }

user_defined_types: struct rtentry { struct list_head rt_list; unsigned int rt_net; unsigned int rt_netmask; unsigned int rt_gw; unsigned int rt_flags; int rt_metric; struct netdev *rt_dev; }

user_defined_types: struct netstats { unsigned int rx_packets; unsigned int tx_packets; unsigned int rx_errors; unsigned int tx_errors; unsigned int rx_bytes; unsigned int tx_bytes; }

user_defined_types: struct netdev_ops { int (*xmit)(struct netdev *, struct pkbuf *); int (*init)(struct netdev *); void (*exit)(struct netdev *); }


decompiled_code:
__int64 __fastcall udp_unhash(__int64 a1)
{
udp_unset_sport(a1);
return sock_del_hash();
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
func4(a1);
return sock_del_hash();
}

original_code:
static void udp_unhash(struct sock *sk)
{
 udp_unset_sport(sk);
 sock_del_hash(sk);
}

user_defined_types: struct sock_addr { unsigned int src_addr; unsigned int dst_addr; unsigned short src_port; unsigned short dst_port; }

user_defined_types: struct tapip_wait { union pthread_mutex_t mutex; union pthread_cond_t cond; int notified; int dead; int sleep; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall udp_hash(__int64 a1)
{
sock_add_hash(a1, &udp_table[2 * *(unsigned int *)(a1 + 61)]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
sock_add_hash(a1, &udp_table[2 * *(unsigned int *)(a1 + 61)]);
return 0LL;
}

original_code:
static int udp_hash(struct sock *sk)
{
 sock_add_hash(sk, (&(&udp_table.slot[sk->hash])->head));
 return 0;
}

user_defined_types: struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; }

user_defined_types: struct sock_ops { void (*recv_notify)(struct sock *); void (*send_notify)(struct sock *); int (*send_pkb)(struct sock *, struct pkbuf *); int (*send_buf)(struct sock *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct sock *); int (*recv_buf)(struct sock *, char *, int); int (*hash)(struct sock *); void (*unhash)(struct sock *); int (*bind)(struct sock *, struct sock_addr *); int (*connect)(struct sock *, struct sock_addr *); int (*set_port)(struct sock *, unsigned short); int (*close)(struct sock *); int (*listen)(struct sock *, int); struct sock *(*accept)(struct sock *); }

user_defined_types: struct socket_ops { int (*socket)(struct socket *, int); int (*close)(struct socket *); int (*accept)(struct socket *, struct socket *, struct sock_addr *); int (*listen)(struct socket *, int); int (*bind)(struct socket *, struct sock_addr *); int (*connect)(struct socket *, struct sock_addr *); int (*read)(struct socket *, void *, int); int (*write)(struct socket *, void *, int); int (*send)(struct socket *, void *, int, struct sock_addr *); struct pkbuf *(*recv)(struct socket *); }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


original_code:
static int udp_init_pkb(struct sock *sk, struct pkbuf *pkb,
  void *buf, int size, struct sock_addr *skaddr)
{
 struct ip *iphdr = ((struct ip *)((pkb)->pk_data + sizeof(struct ether)));
 struct udp *udphdr = (struct udp *)iphdr->ip_data;
 iphdr->ip_hlen = sizeof(struct ip) >> 2;
 iphdr->ip_ver = 4;
 iphdr->ip_tos = 0;
 iphdr->ip_len = _htons(pkb->pk_len - sizeof(struct ether));
 iphdr->ip_id = _htons(udp_id);
 iphdr->ip_fragoff = 0;
 iphdr->ip_ttl = 64;
 iphdr->ip_pro = sk->protocol;
 iphdr->ip_dst = skaddr->dst_addr;
 if (rt_output(pkb) < 0)
  return -1;
 udphdr->src = sk->sk_addr.src_port;
 udphdr->dst = skaddr->dst_port;
 udphdr->length = _htons(size + sizeof(struct udp));
 memcpy(udphdr->data, buf, size);
 do { if (net_debug & 0x00000020) fprintf(stderr, "[%d]%s " "\e[01;35m""udp""\e\[0m"" ""%d.%d.%d.%d"":%d" "->" "%d.%d.%d.%d"":%d(proto %d)" "\n", (int)syscall(186), __FUNCTION__, (iphdr->ip_src) & 0xff, ((iphdr->ip_src) >> 8) & 0xff, ((iphdr->ip_src) >> 16) & 0xff, ((iphdr->ip_src) >> 24) & 0xff, _htons(udphdr->src), (iphdr->ip_dst) & 0xff, ((iphdr->ip_dst) >> 8) & 0xff, ((iphdr->ip_dst) >> 16) & 0xff, ((iphdr->ip_dst) >> 24) & 0xff, _htons(udphdr->dst), iphdr->ip_pro);} while (0);
 udp_set_checksum(iphdr, udphdr);
 return 0;
}

variable_types:
pkb
struct pkbuf *

variable_types:
buf
void *

variable_types: size int

variable_types:
skaddr
struct sock_addr *

variable_types: struct ip { unsigned char ip_hlen; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_fragoff; unsigned char ip_ttl; unsigned char ip_pro; unsigned short ip_cksum; unsigned int ip_src; unsigned int ip_dst; unsigned char ip_data[]; } *

variable_types: struct udp { unsigned short src; unsigned short dst; unsigned short length; unsigned short checksum; unsigned char data[]; } *

user_defined_types: struct ether { unsigned char eth_dst[6]; unsigned char eth_src[6]; unsigned short eth_pro; unsigned char eth_data[]; }

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

user_defined_types: struct list_head { struct list_head *prev; }


decompiled_code:
__int64 __fastcall udp_send_pkb(__int64 a1, __int64 a2)
{
ip_send_out(a2);
return (unsigned int)(*(_DWORD *)(a2 + 20) - 42);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
ip_send_out(a2);
return (unsigned int)(*(_DWORD *)(a2 + 20) - 42);
}

original_code:
static int udp_send_pkb(struct sock *sk, struct pkbuf *pkb)
{
 ip_send_out(pkb);
 return pkb->pk_len - sizeof(struct ether) - sizeof(struct ip) - sizeof(struct udp);
}

user_defined_types: struct ip { unsigned char ip_hlen; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_fragoff; unsigned char ip_ttl; unsigned char ip_pro; unsigned short ip_cksum; unsigned int ip_src; unsigned int ip_dst; unsigned char ip_data[]; }

user_defined_types: struct udp { unsigned short src; unsigned short dst; unsigned short length; unsigned short checksum; unsigned char data[]; }

user_defined_types: struct sock { unsigned char protocol; struct sock_addr sk_addr; struct socket *sock; struct sock_ops *ops; struct rtentry *sk_dst; struct list_head recv_queue; struct tapip_wait *recv_wait; unsigned int hash; struct hlist_node hash_list; int refcnt; }

user_defined_types: struct pkbuf { struct list_head pk_list; unsigned short pk_pro; unsigned short pk_type; int pk_len; int pk_refcnt; struct netdev *pk_indev; struct rtentry *pk_rtdst; struct sock *pk_sk; unsigned char pk_data[]; }

user_defined_types: struct netdev { int net_mtu; unsigned int net_ipaddr; unsigned int net_mask; unsigned char net_hwaddr[6]; unsigned char net_name[16]; struct netdev_ops *net_ops; struct netstats net_stats; struct list_head net_list; }