decompiled_code:
_BOOL8 __fastcall cmd_client(int a1, __int64 a2)
{
int v2; // er14
const char **v3; // r15
const char *v4; // rax
bool v5; // zf
_BOOL4 v6; // er12
const char **v7; // rbx
unsigned __int64 v8; // rbp
__int64 v9; // rbp
size_t v10; // r12
const char *v11; // r13
size_t v12; // rbx
size_t v13; // r12
int v14; // ebx
ssize_t v15; // rax
int *v16; // rax
char *v17; // rax
int *v18; // rax
char *v19; // rax
const char *v21; // rbx
size_t v22; // r12
char *src; // [rsp+8h] [rbp-1B0h]
struct sockaddr addr; // [rsp+10h] [rbp-1A8h] BYREF
int v25; // [rsp+78h] [rbp-140h]
__int16 v26; // [rsp+7Ch] [rbp-13Ch]
char buf[312]; // [rsp+80h] [rbp-138h] BYREF

memset(&addr, 0, 0x68uLL);
v25 = 0;
v26 = 0;
v2 = a1 - 1;
v3 = (const char **)(a2 + 8);
if ( a1 - 1 <= 0 )
{
src = "/tmp/kadnode/kadnode_cmd.sock";
}
else
{
v4 = *(const char **)(a2 + 8);
v5 = strcmp(v4, "-h") == 0;
v6 = !v5;
if ( v5 )
{
fwrite(
"kadnode Control Program - Send commands to a KadNode instance.\n"
"\n"
"Usage: kadnode-ctl [OPTIONS] [COMMANDS]\n"
"\n"
" -p <file>\tConnect to this unix socket (Default: /tmp/kadnode/kadnode_cmd.sock)\n"
" -h\t\tPrint this help.\n"
"\n",
1uLL,
0xD0uLL,
stdout);
return v6;
}
if ( !strcmp(v4, "-p") )
{
if ( v2 <= 1 )
{
fwrite("Path is missing!\n", 1uLL, 0x11uLL, stderr);
return 1;
}
src = *(char **)(a2 + 16);
v2 = a1 - 3;
v3 = (const char **)(a2 + 24);
if ( strlen(src) > 0x6B )
{
fwrite("Path too long!\n", 1uLL, 0xFuLL, stderr);
return 1;
}
}
else
{
src = "/tmp/kadnode/kadnode_cmd.sock";
}
}
if ( v2 <= 1 )
{
if ( v2 == 1 )
{
v21 = *v3;
v22 = strlen(*v3);
memcpy(buf, v21, v22);
buf[v22] = 32;
v10 = v22 + 1;
}
else
{
v10 = 0LL;
}
}
else
{
v7 = v3 + 1;
v8 = 0LL;
do
v8 += strlen(*v7++) + 1;
while ( v7 != &v3[(unsigned int)(v2 - 2) + 2] );
if ( v8 > 0xFF )
{
fwrite("Input too long!\n", 1uLL, 0x10uLL, stderr);
return 1;
}
v9 = 0LL;
v10 = 0LL;
do
{
v11 = v3[v9];
v12 = strlen(v11);
memcpy(&buf[v10], v11, v12);
v13 = v12 + v10;
buf[v13] = 32;
v10 = v13 + 1;
++v9;
}
while ( v2 > (int)v9 );
}
buf[v10] = 10;
v14 = socket(1, 1, 0);
if ( v14 < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
fprintf(stderr, "socket(): %s\n", v17);
v6 = 1;
}
else
{
addr.sa_family = 1;
strncpy(addr.sa_data, src, 0x6BuLL);
if ( connect(v14, &addr, 0x6Eu) < 0 )
{
v18 = _errno_location();
v19 = strerror(*v18);
fprintf(stderr, "Failed to connect to '%s': %s\n", src, v19);
v6 = 1;
if ( v14 > 0 )
{
close(v14);
v6 = 1;
}
}
else
{
write(v14, buf, v10 + 1);
while ( 1 )
{
v15 = read(v14, buf, 0x100uLL);
if ( !v15 )
break;
if ( v15 > 0 )
{
buf[v15] = 0;
printf("%s", buf);
}
}
close(v14);
v6 = 0;
}
}
return v6;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(int a1, __int64 a2)
{
int v2; // er14
const char **v3; // r15
const char *v4; // rax
bool v5; // zf
_BOOL4 v6; // er12
const char **v7; // rbx
unsigned __int64 v8; // rbp
__int64 v9; // rbp
size_t v10; // r12
const char *v11; // r13
size_t v12; // rbx
size_t v13; // r12
int v14; // ebx
ssize_t v15; // rax
int *v16; // rax
char *v17; // rax
int *v18; // rax
char *v19; // rax
const char *v21; // rbx
size_t v22; // r12
char *src; // [rsp+8h] [rbp-1B0h]
struct sockaddr addr; // [rsp+10h] [rbp-1A8h] BYREF
int v25; // [rsp+78h] [rbp-140h]
__int16 v26; // [rsp+7Ch] [rbp-13Ch]
char buf[312]; // [rsp+80h] [rbp-138h] BYREF

memset(&addr, 0, 0x68uLL);
v25 = 0;
v26 = 0;
v2 = a1 - 1;
v3 = (const char **)(a2 + 8);
if ( a1 - 1 <= 0 )
{
src = "/tmp/kadnode/kadnode_cmd.sock";
}
else
{
v4 = *(const char **)(a2 + 8);
v5 = strcmp(v4, "-h") == 0;
v6 = !v5;
if ( v5 )
{
fwrite(
"kadnode Control Program - Send commands to a KadNode instance.\n"
"\n"
"Usage: kadnode-ctl [OPTIONS] [COMMANDS]\n"
"\n"
" -p <file>\tConnect to this unix socket (Default: /tmp/kadnode/kadnode_cmd.sock)\n"
" -h\t\tPrint this help.\n"
"\n",
1uLL,
0xD0uLL,
stdout);
return v6;
}
if ( !strcmp(v4, "-p") )
{
if ( v2 <= 1 )
{
fwrite("Path is missing!\n", 1uLL, 0x11uLL, stderr);
return 1;
}
src = *(char **)(a2 + 16);
v2 = a1 - 3;
v3 = (const char **)(a2 + 24);
if ( strlen(src) > 0x6B )
{
fwrite("Path too long!\n", 1uLL, 0xFuLL, stderr);
return 1;
}
}
else
{
src = "/tmp/kadnode/kadnode_cmd.sock";
}
}
if ( v2 <= 1 )
{
if ( v2 == 1 )
{
v21 = *v3;
v22 = strlen(*v3);
memcpy(buf, v21, v22);
buf[v22] = 32;
v10 = v22 + 1;
}
else
{
v10 = 0LL;
}
}
else
{
v7 = v3 + 1;
v8 = 0LL;
do
v8 += strlen(*v7++) + 1;
while ( v7 != &v3[(unsigned int)(v2 - 2) + 2] );
if ( v8 > 0xFF )
{
fwrite("Input too long!\n", 1uLL, 0x10uLL, stderr);
return 1;
}
v9 = 0LL;
v10 = 0LL;
do
{
v11 = v3[v9];
v12 = strlen(v11);
memcpy(&buf[v10], v11, v12);
v13 = v12 + v10;
buf[v13] = 32;
v10 = v13 + 1;
++v9;
}
while ( v2 > (int)v9 );
}
buf[v10] = 10;
v14 = socket(1, 1, 0);
if ( v14 < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
fprintf(stderr, "socket(): %s\n", v17);
v6 = 1;
}
else
{
addr.sa_family = 1;
strncpy(addr.sa_data, src, 0x6BuLL);
if ( connect(v14, &addr, 0x6Eu) < 0 )
{
v18 = _errno_location();
v19 = strerror(*v18);
fprintf(stderr, "Failed to connect to '%s': %s\n", src, v19);
v6 = 1;
if ( v14 > 0 )
{
close(v14);
v6 = 1;
}
}
else
{
write(v14, buf, v10 + 1);
while ( 1 )
{
v15 = read(v14, buf, 0x100uLL);
if ( !v15 )
break;
if ( v15 > 0 )
{
buf[v15] = 0;
printf("%s", buf);
}
}
close(v14);
v6 = 0;
}
}
return v6;
}

original_code:
int cmd_client(int argc, char *argv[])
{
 char buffer[256];
 const char *path;
 struct sockaddr_un addr = { 0 };
 ssize_t size;
 size_t all;
 size_t pos;
 int sock;
 int i;
 path = "/tmp/kadnode/kadnode_cmd.sock";
 argc -= 1;
 argv += 1;
 if (argc >= 1) {
  if (strcmp(argv[0], "-h") == 0) {
   fprintf(stdout, "%s", g_client_usage);
   return 0;
  } else if (strcmp(argv[0], "-p") == 0) {
   if (argc >= 2) {
    path = argv[1];
    argc -= 2;
    argv += 2;
   } else {
    fprintf(stderr, "Path is missing!\n");
    return 1;
   }
  }
 }
 if (strlen(path) > (sizeof(((struct sockaddr_un*)0)->sun_path)) - 1) {
  fprintf(stderr, "Path too long!\n");
  return 1;
 }
 for (all = 0, i = 1; i < argc; i++) {
  all += strlen(argv[i]) + 1;
 }
 if (all >= sizeof(buffer)) {
  fprintf(stderr, "Input too long!\n");
  return 1;
 }
 for (i = 0, pos = 0; i < argc; i++) {
  size_t len = strlen(argv[i]);
  memcpy(&buffer[pos], argv[i], len);
  pos += len;
  buffer[pos] = ' ';
  pos += 1;
 }
 buffer[pos] = '\n';
 pos += 1;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0) {
  fprintf(stderr, "socket(): %s\n", strerror((*__errno_location ())));
  return 1;
 }
 addr.sun_family = 1;
 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
 if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  fprintf(stderr, "Failed to connect to '%s': %s\n", path, strerror((*__errno_location ())));
  goto error;
 }
 size_t ret = write(sock, buffer, pos);
 if (ret < 0) {
  fprintf(stderr, "write(): %s\n", strerror((*__errno_location ())));
  goto error;
 }
 while (1) {
  size = read(sock, buffer, sizeof(buffer));
  if (size == 0) {
   break;
  } else if (size > 0) {
   buffer[size] = 0;
   printf("%s", buffer);
  }
 }
 close(sock);
 return 0;
error:
 if (sock > 0) {
  close(sock);
 }
 return 1;
}

canonical_original_code:
int cmd_client(int argc, char *argv[])
{
 char buffer[256];
 char *path;
 struct sockaddr_un addr = { 0 };
 long size;
 unsigned long all;
 unsigned long pos;
 int sock;
 int i;
 path = "/tmp/kadnode/kadnode_cmd.sock";
 argc -= 1;
 argv += 1;
 if (argc >= 1) {
  if (strcmp(argv[0], "-h") == 0) {
   fprintf(stdout, "%s", g_client_usage);
   return 0;
  } else if (strcmp(argv[0], "-p") == 0) {
   if (argc >= 2) {
    path = argv[1];
    argc -= 2;
    argv += 2;
   } else {
    fprintf(stderr, "Path is missing!\n");
    return 1;
   }
  }
 }
 if (strlen(path) > (sizeof(((struct sockaddr_un *)0)->sun_path)) - 1) {
  fprintf(stderr, "Path too long!\n");
  return 1;
 }
 for (all = 0, i = 1; i < argc; i++) {
  all += strlen(argv[i]) + 1;
 }
 if (all >= sizeof(buffer)) {
  fprintf(stderr, "Input too long!\n");
  return 1;
 }
 for (i = 0, pos = 0; i < argc; i++) {
  unsigned long len = strlen(argv[i]);
  memcpy(&buffer[pos], argv[i], len);
  pos += len;
  buffer[pos] = ' ';
  pos += 1;
 }
 buffer[pos] = '\n';
 pos += 1;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0) {
  fprintf(stderr, "socket(): %s\n", strerror((*__errno_location ())));
  return 1;
 }
 addr.sun_family = 1;
 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
 if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  fprintf(stderr, "Failed to connect to '%s': %s\n", path, strerror((*__errno_location ())));
  goto error;
 }
 unsigned long ret = write(sock, buffer, pos);
 if (ret < 0) {
  fprintf(stderr, "write(): %s\n", strerror((*__errno_location ())));
  goto error;
 }
 while (1) {
  size = read(sock, buffer, sizeof(buffer));
  if (size == 0) {
   break;
  } else if (size > 0) {
   buffer[size] = 0;
   printf("%s", buffer);
  }
 }
 close(sock);
 return 0;
error:
 if (sock > 0) {
  close(sock);
 }
 return 1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
buffer
char [256]

variable_types:
path
char *

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: size long

variable_types:
all
unsigned long

variable_types:
pos
unsigned long

variable_types: sock int

variable_types: i int

variable_types:
len
unsigned long

variable_types:
ret
unsigned long


repo: mwarning/KadNode

decompiled_code:
__int64 __fastcall is_valid_ifa(__int64 a1, int a2)
{
unsigned __int16 *v2; // rcx
unsigned int v3; // edx

v2 = *(unsigned __int16 **)(a1 + 24);
if ( !v2 )
return 0;
v3 = 0;
if ( (*(_DWORD *)(a1 + 16) & 0x48) == 64 && *v2 == a2 )
{
if ( *(_QWORD *)(gconf + 80LL) )
return strcmp(*(const char **)(gconf + 80LL), *(const char **)(a1 + 8)) == 0;
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
unsigned __int16 *v2; // rcx
unsigned int v3; // edx

v2 = *(unsigned __int16 **)(a1 + 24);
if ( !v2 )
return 0;
v3 = 0;
if ( (*(_DWORD *)(a1 + 16) & 0x48) == 64 && *v2 == a2 )
{
if ( *(_QWORD *)(gconf + 80LL) )
return strcmp(*(const char **)(gconf + 80LL), *(const char **)(a1 + 8)) == 0;
v3 = 1;
}
return v3;
}

original_code:
static int is_valid_ifa(struct ifaddrs *ifa, int af)
{
 if ((ifa->ifa_addr == ((void *)0))
   || !(ifa->ifa_flags & IFF_RUNNING)
   || (ifa->ifa_flags & IFF_LOOPBACK)
   || (ifa->ifa_addr->sa_family != af)) {
  return 0;
 }
 if (gconf->dht_ifname && 0 != strcmp(gconf->dht_ifname, ifa->ifa_name)) {
  return 0;
 } else {
  return 1;
 }
}

variable_types: struct ifaddrs { struct ifaddrs *ifa_next; char *ifa_name; unsigned int ifa_flags; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ifa_addr; struct sockaddr *ifa_netmask; union { struct sockaddr *ifu_broadaddr; struct sockaddr *ifu_dstaddr; } ifa_ifu; void *ifa_data; } *

variable_types: af int

return_type: int

user_defined_types: struct ifaddrs { struct ifaddrs *ifa_next; char *ifa_name; unsigned int ifa_flags; struct sockaddr *ifa_addr; struct sockaddr *ifa_netmask; union { struct sockaddr *ifu_broadaddr; struct sockaddr *ifu_dstaddr; } ifa_ifu; void *ifa_data; }


original_code:
static void join_mcast(const struct lpd_state* lpd, struct ifaddrs *ifa)
{
 for (; ifa != ((void *)0); ifa = ifa->ifa_next) {
  if (is_valid_ifa(ifa, 17)) {
   unsigned ifindex = if_nametoindex(ifa->ifa_name);
   if (lpd->mcast_addr.ss_family == 2) {
    struct ip_mreq mcastReq;
    memset(&mcastReq, 0, sizeof(mcastReq));
    mcastReq.imr_multiaddr = ((IP4*) &lpd->mcast_addr)->sin_addr;
    mcastReq.imr_interface.s_addr = htonl(((in_addr_t) 0x00000000));
    setsockopt(lpd->sock_listen, IPPROTO_IP, 35, (void const*)&mcastReq, sizeof(mcastReq));
   } else {
    struct ipv6_mreq mreq6;
    memcpy(&mreq6.ipv6mr_multiaddr, &((IP6*) &lpd->mcast_addr)->sin6_addr, 16);
    mreq6.ipv6mr_interface = ifindex;
    setsockopt(lpd->sock_listen, IPPROTO_IPV6, 20, &mreq6, sizeof(mreq6));
   }
  }
 }
}

canonical_original_code:
static void join_mcast(struct lpd_state *lpd, struct ifaddrs *ifa)
{
 for (; ifa != ((void *)0); ifa = ifa->ifa_next) {
  if (is_valid_ifa(ifa, 17)) {
   unsigned int ifindex = if_nametoindex(ifa->ifa_name);
   if (lpd->mcast_addr.ss_family == 2) {
    struct ip_mreq mcastReq;
    memset(&mcastReq, 0, sizeof(mcastReq));
    mcastReq.imr_multiaddr = ((struct sockaddr_in *) &lpd->mcast_addr)->sin_addr;
    mcastReq.imr_interface.s_addr = htonl(((unsigned int) 0x00000000));
    setsockopt(lpd->sock_listen, IPPROTO_IP, 35, (void *)&mcastReq, sizeof(mcastReq));
   } else {
    struct ipv6_mreq mreq6;
    memcpy(&mreq6.ipv6mr_multiaddr, &((struct sockaddr_in6 *) &lpd->mcast_addr)->sin6_addr, 16);
    mreq6.ipv6mr_interface = ifindex;
    setsockopt(lpd->sock_listen, IPPROTO_IPV6, 20, &mreq6, sizeof(mreq6));
   }
  }
 }
}

variable_types: struct lpd_state { struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } mcast_addr; long mcast_time; int packet_limit; int sock_send; int sock_listen; } *

variable_types:
ifindex
unsigned int

variable_types: struct ip_mreq { struct in_addr { unsigned int s_addr; } imr_multiaddr; struct in_addr imr_interface; }

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

return_type: void

user_defined_types: struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; }

user_defined_types: struct ipv6_mreq { struct in6_addr ipv6mr_multiaddr; unsigned int ipv6mr_interface; }

user_defined_types: struct lpd_state { struct sockaddr_storage mcast_addr; long mcast_time; int packet_limit; int sock_send; int sock_listen; }

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

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

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


decompiled_code:
int __fastcall send_mcasts(struct sockaddr *addr, __int64 a2)
{
__int64 v2; // rbx
int result; // eax
int v4; // ebp
const char *v5; // rax
socklen_t v6; // er13
size_t v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
unsigned int optval; // [rsp+Ch] [rbp-3Ch] BYREF
char s[56]; // [rsp+10h] [rbp-38h] BYREF

v2 = a2;
if ( !*(_DWORD *)(gconf + 60LL) )
{
v5 = (const char *)str_addr();
log_print(7LL, "LPD: Send discovery message to %s", v5);
}
result = sprintf(s, "DHT %d", *(unsigned int *)(gconf + 72LL));
v4 = addr->sa_family;
if ( a2 )
{
while ( v4 == 2 )
{
result = is_valid_ifa(v2, 2);
if ( result )
{
optval = *(_DWORD *)(*(_QWORD *)(v2 + 24) + 4LL);
if ( setsockopt(*(_DWORD *)&addr[8].sa_data[10], 0, 32, &optval, 4u) < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
result = log_print(3LL, "setsockopt(IP_MULTICAST_IF) %s", v11);
goto LABEL_8;
}
LABEL_7:
v6 = addr_len(addr);
v7 = strlen(s);
result = sendto(*(_DWORD *)&addr[8].sa_data[10], s, v7, 0, addr, v6);
}
LABEL_8:
v2 = *(_QWORD *)v2;
if ( !v2 )
return result;
}
if ( v4 != 10 )
goto LABEL_8;
result = is_valid_ifa(v2, 17);
if ( !result )
goto LABEL_8;
optval = if_nametoindex(*(const char **)(v2 + 8));
if ( setsockopt(*(_DWORD *)&addr[8].sa_data[10], 41, 17, &optval, 4u) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
result = log_print(3LL, "setsockopt(IPV6_MULTICAST_IF) %s", v9);
goto LABEL_8;
}
goto LABEL_7;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(struct sockaddr *addr, __int64 a2)
{
__int64 v2; // rbx
int result; // eax
int v4; // ebp
const char *v5; // rax
socklen_t v6; // er13
size_t v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
unsigned int optval; // [rsp+Ch] [rbp-3Ch] BYREF
char s[56]; // [rsp+10h] [rbp-38h] BYREF

v2 = a2;
if ( !*(_DWORD *)(gconf + 60LL) )
{
v5 = (const char *)str_addr();
log_print(7LL, "LPD: Send discovery message to %s", v5);
}
result = sprintf(s, "DHT %d", *(unsigned int *)(gconf + 72LL));
v4 = addr->sa_family;
if ( a2 )
{
while ( v4 == 2 )
{
result = func0(v2, 2);
if ( result )
{
optval = *(_DWORD *)(*(_QWORD *)(v2 + 24) + 4LL);
if ( setsockopt(*(_DWORD *)&addr[8].sa_data[10], 0, 32, &optval, 4u) < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
result = log_print(3LL, "setsockopt(IP_MULTICAST_IF) %s", v11);
goto LABEL_8;
}
LABEL_7:
v6 = addr_len(addr);
v7 = strlen(s);
result = sendto(*(_DWORD *)&addr[8].sa_data[10], s, v7, 0, addr, v6);
}
LABEL_8:
v2 = *(_QWORD *)v2;
if ( !v2 )
return result;
}
if ( v4 != 10 )
goto LABEL_8;
result = func0(v2, 17);
if ( !result )
goto LABEL_8;
optval = if_nametoindex(*(const char **)(v2 + 8));
if ( setsockopt(*(_DWORD *)&addr[8].sa_data[10], 41, 17, &optval, 4u) < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
result = log_print(3LL, "setsockopt(IPV6_MULTICAST_IF) %s", v9);
goto LABEL_8;
}
goto LABEL_7;
}
return result;
}

original_code:
static void send_mcasts(const struct lpd_state* lpd, struct ifaddrs *ifa)
{
 char message[16];
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "LPD: Send discovery message to %s", str_addr(&lpd->mcast_addr)); } while (0);
 sprintf(message, "DHT %d", gconf->dht_port);
 int family = lpd->mcast_addr.ss_family;
 for (; ifa != ((void *)0); ifa = ifa->ifa_next) {
  if (family == 2 && is_valid_ifa(ifa, 2)) {
   struct in_addr addr = ((struct sockaddr_in*) ifa->ifa_addr)->sin_addr;
   if (setsockopt(lpd->sock_send, IPPROTO_IP, 32, &addr, sizeof(addr)) < 0) {
    log_print(3, "setsockopt(IP_MULTICAST_IF) %s", strerror((*__errno_location ())));;
    continue;
   }
  } else if (family == 10 && is_valid_ifa(ifa, 17)) {
   unsigned ifindex = if_nametoindex(ifa->ifa_name);
   if (setsockopt(lpd->sock_send, IPPROTO_IPV6, 17, &ifindex, sizeof(ifindex)) < 0) {
    log_print(3, "setsockopt(IPV6_MULTICAST_IF) %s", strerror((*__errno_location ())));;
    continue;
   }
  } else {
   continue;
  }
  sendto(lpd->sock_send, (void const*) message, strlen(message), 0,
    (struct sockaddr const*) &lpd->mcast_addr, addr_len(&lpd->mcast_addr));
 }
}

canonical_original_code:
static void send_mcasts(struct lpd_state *lpd, struct ifaddrs *ifa)
{
 char message[16];
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "LPD: Send discovery message to %s", str_addr(&lpd->mcast_addr)); } while (0);
 sprintf(message, "DHT %d", gconf->dht_port);
 int family = lpd->mcast_addr.ss_family;
 for (; ifa != ((void *)0); ifa = ifa->ifa_next) {
  if (family == 2 && is_valid_ifa(ifa, 2)) {
   struct in_addr addr = ((struct sockaddr_in*) ifa->ifa_addr)->sin_addr;
   if (setsockopt(lpd->sock_send, IPPROTO_IP, 32, &addr, sizeof(addr)) < 0) {
    log_print(3, "setsockopt(IP_MULTICAST_IF) %s", strerror((*__errno_location ())));;
    continue;
   }
  } else if (family == 10 && is_valid_ifa(ifa, 17)) {
   unsigned int ifindex = if_nametoindex(ifa->ifa_name);
   if (setsockopt(lpd->sock_send, IPPROTO_IPV6, 17, &ifindex, sizeof(ifindex)) < 0) {
    log_print(3, "setsockopt(IPV6_MULTICAST_IF) %s", strerror((*__errno_location ())));;
    continue;
   }
  } else {
   continue;
  }
  sendto(lpd->sock_send, (void *) message, strlen(message), 0,
    (struct sockaddr *) &lpd->mcast_addr, addr_len(&lpd->mcast_addr));
 }
}

variable_types:
message
char [16]

variable_types: family int

variable_types:
addr
struct in_addr { unsigned int s_addr; }

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
ssize_t __fastcall handle_mcast(int a1, __int64 a2)
{
ssize_t result; // rax
int v3; // edx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
const char *v8; // rax
struct sockaddr v9[8]; // [rsp+0h] [rbp-C8h] BYREF
unsigned __int16 v10; // [rsp+8Eh] [rbp-3Ah] BYREF
char buf[20]; // [rsp+90h] [rbp-38h] BYREF
socklen_t addr_len; // [rsp+A4h] [rbp-24h] BYREF
struct ifaddrs *ifap; // [rsp+A8h] [rbp-20h] BYREF

result = *gconf;
if ( *(_QWORD *)(a2 + 128) <= *gconf )
{
if ( getifaddrs(&ifap) )
{
v4 = _errno_location();
v5 = strerror(*v4);
log_print(3LL, "getifaddrs() %s", v5);
}
else
{
join_mcast(a2, (__int64)ifap);
if ( !(unsigned int)kad_count_nodes(0LL) )
send_mcasts((struct sockaddr *)a2, (__int64)ifap);
freeifaddrs(ifap);
}
*(_DWORD *)(a2 + 136) = 100;
result = time_add_mins(5LL);
*(_QWORD *)(a2 + 128) = result;
}
if ( a1 > 0 )
{
addr_len = 128;
result = recvfrom(*(_DWORD *)(a2 + 144), buf, 0xFuLL, 0, v9, &addr_len);
if ( (int)result <= 0 )
{
result = gconf;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = log_print(4LL, "LPD: Cannot receive multicast message: %s", v7);
}
}
else
{
v3 = *(_DWORD *)(a2 + 136);
if ( v3 >= 0 )
{
*(_DWORD *)(a2 + 136) = v3 - 1;
buf[(int)result] = 0;
result = _isoc99_sscanf(buf, "DHT %hu", &v10);
if ( (_DWORD)result == 1 )
{
port_set(v9, v10);
if ( !*(_DWORD *)(gconf + 60LL) )
{
v8 = (const char *)str_addr(v9);
log_print(7LL, "LPD: Ping lonely peer at %s", v8);
}
result = kad_ping(v9);
}
}
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int a1, __int64 a2)
{
ssize_t result; // rax
int v3; // edx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
const char *v8; // rax
struct sockaddr v9[8]; // [rsp+0h] [rbp-C8h] BYREF
unsigned __int16 v10; // [rsp+8Eh] [rbp-3Ah] BYREF
char buf[20]; // [rsp+90h] [rbp-38h] BYREF
socklen_t addr_len; // [rsp+A4h] [rbp-24h] BYREF
struct ifaddrs *ifap; // [rsp+A8h] [rbp-20h] BYREF

result = *gconf;
if ( *(_QWORD *)(a2 + 128) <= *gconf )
{
if ( getifaddrs(&ifap) )
{
v4 = _errno_location();
v5 = strerror(*v4);
log_print(3LL, "getifaddrs() %s", v5);
}
else
{
func2(a2, (__int64)ifap);
if ( !(unsigned int)kad_count_nodes(0LL) )
func3((struct sockaddr *)a2, (__int64)ifap);
freeifaddrs(ifap);
}
*(_DWORD *)(a2 + 136) = 100;
result = time_add_mins(5LL);
*(_QWORD *)(a2 + 128) = result;
}
if ( a1 > 0 )
{
addr_len = 128;
result = recvfrom(*(_DWORD *)(a2 + 144), buf, 0xFuLL, 0, v9, &addr_len);
if ( (int)result <= 0 )
{
result = gconf;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = log_print(4LL, "LPD: Cannot receive multicast message: %s", v7);
}
}
else
{
v3 = *(_DWORD *)(a2 + 136);
if ( v3 >= 0 )
{
*(_DWORD *)(a2 + 136) = v3 - 1;
buf[(int)result] = 0;
result = _isoc99_sscanf(buf, "DHT %hu", &v10);
if ( (_DWORD)result == 1 )
{
port_set(v9, v10);
if ( !*(_DWORD *)(gconf + 60LL) )
{
v8 = (const char *)str_addr(v9);
log_print(7LL, "LPD: Ping lonely peer at %s", v8);
}
result = kad_ping(v9);
}
}
}
}
return result;
}

original_code:
static void handle_mcast(int rc, struct lpd_state* lpd)
{
 struct ifaddrs *ifaddrs;
 socklen_t addrlen;
 char buf[16];
 uint16_t port;
 IP addr;
 if (lpd->mcast_time <= (gconf->time_now)) {
  if (getifaddrs(&ifaddrs) == 0) {
   join_mcast(lpd, ifaddrs);
   if (kad_count_nodes(0) == 0) {
    send_mcasts(lpd, ifaddrs);
   }
   freeifaddrs(ifaddrs);
  } else {
   log_print(3, "getifaddrs() %s", strerror((*__errno_location ())));;
  }
  lpd->packet_limit = 5 * PACKET_LIMIT_MAX;
  lpd->mcast_time = time_add_mins(5);
 }
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(IP);
 rc = recvfrom(lpd->sock_listen, buf, sizeof(buf) - 1, 0, (struct sockaddr*) &addr, (socklen_t*) &addrlen);
 if (rc <= 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot receive multicast message: %s", strerror((*__errno_location ()))); } while (0);
  return;
 } else if (lpd->packet_limit < 0) {
  return;
 } else {
  lpd->packet_limit -= 1;
 }
 buf[rc] = '\0';
 if (sscanf(buf, "DHT %hu", &port) == 1) {
  port_set(&addr, port);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "LPD: Ping lonely peer at %s", str_addr(&addr)); } while (0);
  kad_ping(&addr);
 }
}

canonical_original_code:
static void handle_mcast(int rc, struct lpd_state *lpd)
{
 struct ifaddrs *ifaddrs;
 unsigned int addrlen;
 char buf[16];
 unsigned short port;
 struct sockaddr_storage addr;
 if (lpd->mcast_time <= (gconf->time_now)) {
  if (getifaddrs(&ifaddrs) == 0) {
   join_mcast(lpd, ifaddrs);
   if (kad_count_nodes(0) == 0) {
    send_mcasts(lpd, ifaddrs);
   }
   freeifaddrs(ifaddrs);
  } else {
   log_print(3, "getifaddrs() %s", strerror((*__errno_location ())));;
  }
  lpd->packet_limit = 5 * PACKET_LIMIT_MAX;
  lpd->mcast_time = time_add_mins(5);
 }
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(IP);
 rc = recvfrom(lpd->sock_listen, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &addr, (unsigned int *) &addrlen);
 if (rc <= 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot receive multicast message: %s", strerror((*__errno_location ()))); } while (0);
  return;
 } else if (lpd->packet_limit < 0) {
  return;
 } else {
  lpd->packet_limit -= 1;
 }
 buf[rc] = '\0';
 if (sscanf(buf, "DHT %hu", &port) == 1) {
  port_set(&addr, port);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "LPD: Ping lonely peer at %s", str_addr(&addr)); } while (0);
  kad_ping(&addr);
 }
}

variable_types: rc int

variable_types:
addrlen
unsigned int

variable_types:
buf
char [16]

variable_types:
port
unsigned short

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
ssize_t __fastcall handle_mcast6(int a1, int a2)
{
if ( *((_DWORD *)&g_lpd6 + 36) != a2 )
_assert_fail("sock == g_lpd6.sock_listen", "src/ext-lpd.c", 0xCBu, "handle_mcast6");
return handle_mcast(a1, (__int64)&g_lpd6);
}

canonical_decompiled_code:
ssize_t __fastcall func5(int a1, int a2)
{
if ( *((_DWORD *)&g_lpd6 + 36) != a2 )
_assert_fail("sock == g_lpd6.sock_listen", "src/ext-lpd.c", 0xCBu, "handle_mcast6");
return func4(a1, (__int64)&g_lpd6);
}

original_code:
static void handle_mcast6(int rc, int sock)
{
 ((void) sizeof ((sock == g_lpd6.sock_listen) ? 1 : 0), __extension__ ({ if (sock == g_lpd6.sock_listen) ; else __assert_fail ("sock == g_lpd6.sock_listen", "src/ext-lpd.c", 203, __extension__ __PRETTY_FUNCTION__); }));
 handle_mcast(rc, &g_lpd6);
}

variable_types: sock int


decompiled_code:
ssize_t __fastcall handle_mcast4(int a1, int a2)
{
if ( *((_DWORD *)&g_lpd4 + 36) != a2 )
_assert_fail("sock == g_lpd4.sock_listen", "src/ext-lpd.c", 0xC5u, "handle_mcast4");
return handle_mcast(a1, (__int64)&g_lpd4);
}

canonical_decompiled_code:
ssize_t __fastcall func6(int a1, int a2)
{
if ( *((_DWORD *)&g_lpd4 + 36) != a2 )
_assert_fail("sock == g_lpd4.sock_listen", "src/ext-lpd.c", 0xC5u, "handle_mcast4");
return func4(a1, (__int64)&g_lpd4);
}

original_code:
static void handle_mcast4(int rc, int sock)
{
 ((void) sizeof ((sock == g_lpd4.sock_listen) ? 1 : 0), __extension__ ({ if (sock == g_lpd4.sock_listen) ; else __assert_fail ("sock == g_lpd4.sock_listen", "src/ext-lpd.c", 197, __extension__ __PRETTY_FUNCTION__); }));
 handle_mcast(rc, &g_lpd4);
}


decompiled_code:
__int64 __fastcall create_receive_socket(struct sockaddr *addr)
{
socklen_t v1; // er12
unsigned int v2; // er13
int v3; // eax
unsigned int v4; // ebx
int *v6; // rax
char *v7; // rbx
const char *v8; // rax
int v9; // [rsp+8h] [rbp-30h] BYREF
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

optval[0] = 0;
v1 = addr_len(addr);
v2 = addr->sa_family;
v3 = net_socket("LPD", 0LL, 17LL, addr->sa_family);
if ( v3 < 0 )
return (unsigned int)-1;
v4 = v3;
if ( v2 == 10 )
{
v9 = 0;
if ( setsockopt(v3, 41, 19, &v9, 4u) >= 0 )
{
LABEL_4:
if ( !bind(v4, addr, v1) )
return v4;
goto LABEL_7;
}
}
else if ( !setsockopt(v3, 0, 34, optval, 4u) )
{
goto LABEL_4;
}
LABEL_7:
close(v4);
v4 = -1;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
v8 = (const char *)str_af(v2);
log_print(4LL, "LPD: Cannot create receive %s socket: %s", v8, v7);
v4 = -1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(struct sockaddr *addr)
{
socklen_t v1; // er12
unsigned int v2; // er13
int v3; // eax
unsigned int v4; // ebx
int *v6; // rax
char *v7; // rbx
const char *v8; // rax
int v9; // [rsp+8h] [rbp-30h] BYREF
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

optval[0] = 0;
v1 = addr_len(addr);
v2 = addr->sa_family;
v3 = net_socket("LPD", 0LL, 17LL, addr->sa_family);
if ( v3 < 0 )
return (unsigned int)-1;
v4 = v3;
if ( v2 == 10 )
{
v9 = 0;
if ( setsockopt(v3, 41, 19, &v9, 4u) >= 0 )
{
LABEL_4:
if ( !bind(v4, addr, v1) )
return v4;
goto LABEL_7;
}
}
else if ( !setsockopt(v3, 0, 34, optval, 4u) )
{
goto LABEL_4;
}
LABEL_7:
close(v4);
v4 = -1;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
v8 = (const char *)str_af(v2);
log_print(4LL, "LPD: Cannot create receive %s socket: %s", v8, v7);
v4 = -1;
}
return v4;
}

original_code:
static int create_receive_socket(const IP *mcast_addr)
{
 const int opt_off = 0;
 socklen_t addrlen;
 int sock;
 int af;
 addrlen = addr_len(mcast_addr);
 af = mcast_addr->ss_family;
 if ((sock = net_socket("LPD", ((void *)0), IPPROTO_UDP, af)) < 0) {
  return -1;
 }
 if (af == 10) {
  int loop = 0;
  if (setsockopt(sock, IPPROTO_IPV6, 19, (char *)&loop, sizeof(loop)) < 0) {
   goto fail;
  }
 } else {
  if (setsockopt(sock, IPPROTO_IP, 34, (void const*)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 }
 if (bind(sock, (struct sockaddr*)mcast_addr, addrlen) != 0) {
  goto fail;
 }
 return sock;
fail:
 close(sock);
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot create receive %s socket: %s", str_af(af), strerror((*__errno_location ()))); } while (0);
 return -1;
}

canonical_original_code:
static int create_receive_socket(struct sockaddr_storage *mcast_addr)
{
 int opt_off = 0;
 unsigned int addrlen;
 int sock;
 int af;
 addrlen = addr_len(mcast_addr);
 af = mcast_addr->ss_family;
 if ((sock = net_socket("LPD", ((void *)0), IPPROTO_UDP, af)) < 0) {
  return -1;
 }
 if (af == 10) {
  int loop = 0;
  if (setsockopt(sock, IPPROTO_IPV6, 19, (char *)&loop, sizeof(loop)) < 0) {
   goto fail;
  }
 } else {
  if (setsockopt(sock, IPPROTO_IP, 34, (void *)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 }
 if (bind(sock, (struct sockaddr *)mcast_addr, addrlen) != 0) {
  goto fail;
 }
 return sock;
fail:
 close(sock);
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot create receive %s socket: %s", str_af(af), strerror((*__errno_location ()))); } while (0);
 return -1;
}

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *

variable_types: loop int


decompiled_code:
__int64 __fastcall create_send_socket(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rbx
const char *v6; // rax
int v7; // [rsp+4h] [rbp-24h] BYREF
int v8; // [rsp+8h] [rbp-20h] BYREF
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v8 = 0;
v7 = 0;
v1 = net_socket("LPD", 0LL, 0LL, a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
if ( a1 == 2 )
{
if ( !setsockopt(v1, 0, 33, optval, 4u) && !setsockopt(v2, 0, 32, &v7, 4u) && !setsockopt(v2, 0, 34, &v8, 4u) )
return v2;
}
else if ( !setsockopt(v1, 41, 18, optval, 4u) && !setsockopt(v2, 41, 19, &v8, 4u) )
{
return v2;
}
close(v2);
v2 = -1;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v4 = _errno_location();
v5 = strerror(*v4);
v6 = (const char *)str_af(a1);
log_print(4LL, "LPD: Cannot create send %s socket: %s", v6, v5);
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rbx
const char *v6; // rax
int v7; // [rsp+4h] [rbp-24h] BYREF
int v8; // [rsp+8h] [rbp-20h] BYREF
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v8 = 0;
v7 = 0;
v1 = net_socket("LPD", 0LL, 0LL, a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
if ( a1 == 2 )
{
if ( !setsockopt(v1, 0, 33, optval, 4u) && !setsockopt(v2, 0, 32, &v7, 4u) && !setsockopt(v2, 0, 34, &v8, 4u) )
return v2;
}
else if ( !setsockopt(v1, 41, 18, optval, 4u) && !setsockopt(v2, 41, 19, &v8, 4u) )
{
return v2;
}
close(v2);
v2 = -1;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v4 = _errno_location();
v5 = strerror(*v4);
v6 = (const char *)str_af(a1);
log_print(4LL, "LPD: Cannot create send %s socket: %s", v6, v5);
v2 = -1;
}
return v2;
}

original_code:
static int create_send_socket(int af)
{
 const int scope = TTL_SAME_SUBNET;
 const int opt_off = 0;
 in_addr_t iface = ((in_addr_t) 0x00000000);
 int sock;
 if ((sock = net_socket("LPD", ((void *)0), IPPROTO_IP, af)) < 0) {
  return -1;
 }
 if (af == 2) {
  if (setsockopt(sock, IPPROTO_IP, 33, (void const*)&scope, sizeof(scope)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IP, 32, (char*)&iface, sizeof(iface)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IP, 34, (void const*)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 } else {
  if (setsockopt(sock, IPPROTO_IPV6, 18, (char*)&scope, sizeof(scope)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IPV6, 19, (void const*)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 }
 return sock;
fail:
 close(sock);
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot create send %s socket: %s", str_af(af), strerror((*__errno_location ()))); } while (0);
 return -1;
}

canonical_original_code:
static int create_send_socket(int af)
{
 int scope = TTL_SAME_SUBNET;
 int opt_off = 0;
 unsigned int iface = ((in_addr_t) 0x00000000);
 int sock;
 if ((sock = net_socket("LPD", ((void *)0), IPPROTO_IP, af)) < 0) {
  return -1;
 }
 if (af == 2) {
  if (setsockopt(sock, IPPROTO_IP, 33, (void *)&scope, sizeof(scope)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IP, 32, (char *)&iface, sizeof(iface)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IP, 34, (void *)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 } else {
  if (setsockopt(sock, IPPROTO_IPV6, 18, (char *)&scope, sizeof(scope)) != 0) {
   goto fail;
  }
  if (setsockopt(sock, IPPROTO_IPV6, 19, (void *)&opt_off, sizeof(opt_off)) != 0) {
   goto fail;
  }
 }
 return sock;
fail:
 close(sock);
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: Cannot create send %s socket: %s", str_af(af), strerror((*__errno_location ()))); } while (0);
 return -1;
}

variable_types: scope int

variable_types:
iface
unsigned int


decompiled_code:
_BOOL8 lpd_setup()
{
__int64 v0; // rdi
int v1; // ebx
__int64 v2; // rdi

if ( *(_BYTE *)(gconf + 88LL) )
return 0LL;
if ( *(_QWORD *)(gconf + 80LL) && (*(_DWORD *)(gconf + 68LL) & 0xFFFFFFFD) == 0 && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "LPD: ifname setting not supported for IPv4", 0LL);
addr_parse(&g_lpd4, "239.192.152.143", "6771", 2LL);
addr_parse(&g_lpd6, "ff15::efc0:988f", "6771", 10LL);
*((_DWORD *)&g_lpd4 + 36) = create_receive_socket(&g_lpd4);
*((_DWORD *)&g_lpd4 + 35) = create_send_socket(2u);
*((_DWORD *)&g_lpd6 + 36) = create_receive_socket(&g_lpd6);
*((_DWORD *)&g_lpd6 + 35) = create_send_socket(0xAu);
v0 = *((unsigned int *)&g_lpd4 + 36);
v1 = 0;
if ( (int)v0 >= 0 && *((int *)&g_lpd4 + 35) >= 0 )
{
net_add_handler(v0, handle_mcast4);
v1 = 1;
}
v2 = *((unsigned int *)&g_lpd6 + 36);
if ( (int)v2 >= 0 && *((int *)&g_lpd6 + 35) >= 0 )
{
net_add_handler(v2, handle_mcast6);
++v1;
}
return v1 == 0;
}

canonical_decompiled_code:
_BOOL8 func9()
{
__int64 v0; // rdi
int v1; // ebx
__int64 v2; // rdi

if ( *(_BYTE *)(gconf + 88LL) )
return 0LL;
if ( *(_QWORD *)(gconf + 80LL) && (*(_DWORD *)(gconf + 68LL) & 0xFFFFFFFD) == 0 && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "LPD: ifname setting not supported for IPv4", 0LL);
addr_parse(&g_lpd4, "239.192.152.143", "6771", 2LL);
addr_parse(&g_lpd6, "ff15::efc0:988f", "6771", 10LL);
*((_DWORD *)&g_lpd4 + 36) = func7(&g_lpd4);
*((_DWORD *)&g_lpd4 + 35) = func8(2u);
*((_DWORD *)&g_lpd6 + 36) = func7(&g_lpd6);
*((_DWORD *)&g_lpd6 + 35) = func8(0xAu);
v0 = *((unsigned int *)&g_lpd4 + 36);
v1 = 0;
if ( (int)v0 >= 0 && *((int *)&g_lpd4 + 35) >= 0 )
{
net_add_handler(v0, handle_mcast4);
v1 = 1;
}
v2 = *((unsigned int *)&g_lpd6 + 36);
if ( (int)v2 >= 0 && *((int *)&g_lpd6 + 35) >= 0 )
{
net_add_handler(v2, handle_mcast6);
++v1;
}
return v1 == 0;
}

original_code:
int lpd_setup(void)
{
 const char *ifname;
 int ready = 0;
 if (gconf->lpd_disable) {
  return 0;
 }
 ifname = gconf->dht_ifname;
 if (ifname && (gconf->af == 0 || gconf->af == 2)) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: ifname setting not supported for IPv4"); } while (0);
 }
 addr_parse(&g_lpd4.mcast_addr, "239.192.152.143", "6771", 2);
 addr_parse(&g_lpd6.mcast_addr, "ff15::efc0:988f", "6771", 10);
 g_lpd4.sock_listen = create_receive_socket(&g_lpd4.mcast_addr);
 g_lpd4.sock_send = create_send_socket(2);
 g_lpd6.sock_listen = create_receive_socket(&g_lpd6.mcast_addr);
 g_lpd6.sock_send = create_send_socket(10);
 if (g_lpd4.sock_listen >= 0 && g_lpd4.sock_send >= 0) {
  net_add_handler(g_lpd4.sock_listen, &handle_mcast4);
  ready += 1;
 }
 if (g_lpd6.sock_listen >= 0 && g_lpd6.sock_send >= 0) {
  net_add_handler(g_lpd6.sock_listen, &handle_mcast6);
  ready += 1;
 }
 return ready ? 0 : 1;
}

canonical_original_code:
int lpd_setup()
{
 char *ifname;
 int ready = 0;
 if (gconf->lpd_disable) {
  return 0;
 }
 ifname = gconf->dht_ifname;
 if (ifname && (gconf->af == 0 || gconf->af == 2)) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "LPD: ifname setting not supported for IPv4"); } while (0);
 }
 addr_parse(&g_lpd4.mcast_addr, "239.192.152.143", "6771", 2);
 addr_parse(&g_lpd6.mcast_addr, "ff15::efc0:988f", "6771", 10);
 g_lpd4.sock_listen = create_receive_socket(&g_lpd4.mcast_addr);
 g_lpd4.sock_send = create_send_socket(2);
 g_lpd6.sock_listen = create_receive_socket(&g_lpd6.mcast_addr);
 g_lpd6.sock_send = create_send_socket(10);
 if (g_lpd4.sock_listen >= 0 && g_lpd4.sock_send >= 0) {
  net_add_handler(g_lpd4.sock_listen, &handle_mcast4);
  ready += 1;
 }
 if (g_lpd6.sock_listen >= 0 && g_lpd6.sock_send >= 0) {
  net_add_handler(g_lpd6.sock_listen, &handle_mcast6);
  ready += 1;
 }
 return ready ? 0 : 1;
}

variable_types:
ifname
char *

variable_types: ready int


decompiled_code:
void lpd_free()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
void lpd_free(void)
{
}

canonical_original_code:
void lpd_free()
{
}


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

result = gconf;
if ( !*(_BYTE *)(gconf + 57LL) )
exit(1);
*(_BYTE *)(gconf + 57LL) = 0;
return result;
}

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

result = gconf;
if ( !*(_BYTE *)(gconf + 57LL) )
exit(1);
*(_BYTE *)(gconf + 57LL) = 0;
return result;
}

original_code:
static void shutdown_handler(int signo)
{
 if (!gconf->is_running) {
  exit(1);
 }
 gconf->is_running = 0;
}

variable_types: signo int


decompiled_code:
_BOOL8 __fastcall is_unix_socket_valid(char *src)
{
int v1; // ebx
int v2; // ebp
struct sockaddr v4[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = socket(1, 1, 0);
v4[0].sa_family = 1;
strcpy(v4[0].sa_data, src);
v2 = connect(v1, v4, 0x6Eu);
close(v1);
return v2 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char *src)
{
int v1; // ebx
int v2; // ebp
struct sockaddr v4[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = socket(1, 1, 0);
v4[0].sa_family = 1;
strcpy(v4[0].sa_data, src);
v2 = connect(v1, v4, 0x6Eu);
close(v1);
return v2 == 0;
}

original_code:
static int is_unix_socket_valid(const char path[])
{
 struct sockaddr_un addr;
 int sock;
 int rc;
 sock = socket(1, SOCK_STREAM, 0);
 addr.sun_family = 1;
 strcpy(addr.sun_path, path);
 rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
 close(sock);
 return (rc == 0);
}

canonical_original_code:
static int is_unix_socket_valid(char path[])
{
 struct sockaddr_un addr;
 int sock;
 int rc;
 sock = socket(1, SOCK_STREAM, 0);
 addr.sun_family = 1;
 strcpy(addr.sun_path, path);
 rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
 close(sock);
 return (rc == 0);
}

variable_types:
path
char []

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__sighandler_t unix_signals()
{
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-A8h] BYREF

v6.sa_handler = (__sighandler_t)shutdown_handler;
v6.sa_flags = 0;
if ( sigemptyset(&v6.sa_mask) == -1 || sigaction(2, &v6, 0LL) )
{
v1 = _errno_location();
v2 = strerror(*v1);
log_print(3LL, "Failed to set SIGINT handler: %s", v2);
exit(1);
}
v5.sa_handler = (__sighandler_t)shutdown_handler;
v5.sa_flags = 0;
if ( sigemptyset(&v5.sa_mask) == -1 || sigaction(15, &v5, 0LL) )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "Failed to set SIGTERM handler: %s", v4);
exit(1);
}
return signal(13, (__sighandler_t)((char *)shutdown_handler + 1));
}

canonical_decompiled_code:
__sighandler_t func2()
{
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-A8h] BYREF

v6.sa_handler = (__sighandler_t)shutdown_handler;
v6.sa_flags = 0;
if ( sigemptyset(&v6.sa_mask) == -1 || sigaction(2, &v6, 0LL) )
{
v1 = _errno_location();
v2 = strerror(*v1);
log_print(3LL, "Failed to set SIGINT handler: %s", v2);
exit(1);
}
v5.sa_handler = (__sighandler_t)shutdown_handler;
v5.sa_flags = 0;
if ( sigemptyset(&v5.sa_mask) == -1 || sigaction(15, &v5, 0LL) )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "Failed to set SIGTERM handler: %s", v4);
exit(1);
}
return signal(13, (__sighandler_t)((char *)shutdown_handler + 1));
}

original_code:
void unix_signals(void)
{
 struct sigaction sig_stop;
 struct sigaction sig_term;
 sig_stop.__sigaction_handler.sa_handler = shutdown_handler;
 sig_stop.sa_flags = 0;
 if ((sigemptyset(&sig_stop.sa_mask) == -1) || (sigaction(2, &sig_stop, ((void *)0)) != 0)) {
  log_print(3, "Failed to set SIGINT handler: %s", strerror((*__errno_location ())));;
  exit(1);
 }
 sig_term.__sigaction_handler.sa_handler = shutdown_handler;
 sig_term.sa_flags = 0;
 if ((sigemptyset(&sig_term.sa_mask) == -1) || (sigaction(15, &sig_term, ((void *)0)) != 0)) {
  log_print(3, "Failed to set SIGTERM handler: %s", strerror((*__errno_location ())));;
  exit(1);
 }
 signal(13, ((__sighandler_t) 1));
}

canonical_original_code:
void unix_signals()
{
 struct sigaction sig_stop;
 struct sigaction sig_term;
 sig_stop.__sigaction_handler.sa_handler = shutdown_handler;
 sig_stop.sa_flags = 0;
 if ((sigemptyset(&sig_stop.sa_mask) == -1) || (sigaction(2, &sig_stop, ((void *)0)) != 0)) {
  log_print(3, "Failed to set SIGINT handler: %s", strerror((*__errno_location ())));;
  exit(1);
 }
 sig_term.__sigaction_handler.sa_handler = shutdown_handler;
 sig_term.sa_flags = 0;
 if ((sigemptyset(&sig_term.sa_mask) == -1) || (sigaction(15, &sig_term, ((void *)0)) != 0)) {
  log_print(3, "Failed to set SIGTERM handler: %s", strerror((*__errno_location ())));;
  exit(1);
 }
 signal(13, ((void (*)(int)) 1));
}

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:
void __fastcall unix_remove_unix_socket(char *name, int fd)
{
char *v2; // rax
char *v3; // rbx

v2 = strdup(name);
v3 = dirname(v2);
close(fd);
unlink(name);
rmdir(v3);
free(v3);
}

canonical_decompiled_code:
void __fastcall func3(char *name, int fd)
{
char *v2; // rax
char *v3; // rbx

v2 = strdup(name);
v3 = dirname(v2);
close(fd);
unlink(name);
rmdir(v3);
free(v3);
}

original_code:
void unix_remove_unix_socket(const char path[], int sock_in)
{
 char *dir;
 dir = dirname(strdup(path));
 close(sock_in);
 unlink(path);
 rmdir(dir);
 free(dir);
}

canonical_original_code:
void unix_remove_unix_socket(char path[], int sock_in)
{
 char *dir;
 dir = dirname(strdup(path));
 close(sock_in);
 unlink(path);
 rmdir(dir);
 free(dir);
}

variable_types:
dir
char *


decompiled_code:
__int64 __fastcall unix_create_unix_socket(char *name, int *a2)
{
unsigned int v2; // ebp
char *v4; // rax
char *v5; // r12
int v6; // er14
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct stat v13; // [rsp+0h] [rbp-128h] BYREF
struct sockaddr addr; // [rsp+90h] [rbp-98h] BYREF

if ( !name )
return 1;
v2 = 1;
if ( *name )
{
v4 = strdup(name);
v5 = dirname(v4);
if ( !is_unix_socket_valid(name) )
{
if ( !unlink(name) && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "Removed stale file: %s", name);
if ( stat(v5, &v13) == -1 && mkdir(v5, 0x1EDu) )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_print(3LL, "Cannot create directory %s %s", v5, v8);
}
else
{
v6 = socket(1, 1, 0);
if ( v6 >= 0 )
{
addr.sa_family = 1;
strcpy(addr.sa_data, name);
v2 = bind(v6, &addr, 0x6Eu);
if ( v2 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_print(3LL, "bind() %s %s", name, v12);
free(v5);
unix_remove_unix_socket(name, v6);
v2 = 1;
}
else
{
chmod(name, 0x1FFu);
listen(v6, 5);
*a2 = v6;
}
return v2;
}
v9 = _errno_location();
v10 = strerror(*v9);
log_print(3LL, "socket(): %s", v10);
}
free(v5);
return 1;
}
log_print(3LL, "Socket already in use: %s", name);
if ( v5 )
{
free(v5);
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *name, int *a2)
{
unsigned int v2; // ebp
char *v4; // rax
char *v5; // r12
int v6; // er14
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct stat v13; // [rsp+0h] [rbp-128h] BYREF
struct sockaddr addr; // [rsp+90h] [rbp-98h] BYREF

if ( !name )
return 1;
v2 = 1;
if ( *name )
{
v4 = strdup(name);
v5 = dirname(v4);
if ( !func1(name) )
{
if ( !unlink(name) && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "Removed stale file: %s", name);
if ( stat(v5, &v13) == -1 && mkdir(v5, 0x1EDu) )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_print(3LL, "Cannot create directory %s %s", v5, v8);
}
else
{
v6 = socket(1, 1, 0);
if ( v6 >= 0 )
{
addr.sa_family = 1;
strcpy(addr.sa_data, name);
v2 = bind(v6, &addr, 0x6Eu);
if ( v2 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_print(3LL, "bind() %s %s", name, v12);
free(v5);
func3(name, v6);
v2 = 1;
}
else
{
chmod(name, 0x1FFu);
listen(v6, 5);
*a2 = v6;
}
return v2;
}
v9 = _errno_location();
v10 = strerror(*v9);
log_print(3LL, "socket(): %s", v10);
}
free(v5);
return 1;
}
log_print(3LL, "Socket already in use: %s", name);
if ( v5 )
{
free(v5);
v2 = 1;
}
}
return v2;
}

original_code:
int unix_create_unix_socket(const char path[], int *sock_out)
{
 struct sockaddr_un addr;
 struct stat st;
 char *dir = ((void *)0);
 int sock = -1;
 int rc;
 if (path == ((void *)0) || strlen(path) == 0) {
  goto err;
 }
 dir = dirname(strdup(path));
 if (is_unix_socket_valid(path)) {
  log_print(3, "Socket already in use: %s", path);;
  goto err;
 }
 rc = unlink(path);
 if (rc == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "Removed stale file: %s", path); } while (0);
 }
 if (stat(dir, &st) == -1 && mkdir(dir, 0755) != 0) {
  log_print(3, "Cannot create directory %s %s", dir, strerror((*__errno_location ())));;
  goto err;
 }
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0) {
  log_print(3, "socket(): %s", strerror((*__errno_location ())));;
  goto err;
 }
 addr.sun_family = 1;
 strcpy(addr.sun_path, path);
 if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
  log_print(3, "bind() %s %s", path, strerror((*__errno_location ())));;
  goto err;
 }
 chmod(path, 0777);
 listen(sock, 5);
 *sock_out = sock;
 return 0;
err:
 if (dir) {
  free(dir);
 }
 if (sock >= 0) {
  unix_remove_unix_socket(path, sock);
 }
 return 1;
}

canonical_original_code:
int unix_create_unix_socket(char path[], int *sock_out)
{
 struct sockaddr_un addr;
 struct stat st;
 char *dir = ((void *)0);
 int sock = -1;
 int rc;
 if (path == ((void *)0) || strlen(path) == 0) {
  goto err;
 }
 dir = dirname(strdup(path));
 if (is_unix_socket_valid(path)) {
  log_print(3, "Socket already in use: %s", path);;
  goto err;
 }
 rc = unlink(path);
 if (rc == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "Removed stale file: %s", path); } while (0);
 }
 if (stat(dir, &st) == -1 && mkdir(dir, 0755) != 0) {
  log_print(3, "Cannot create directory %s %s", dir, strerror((*__errno_location ())));;
  goto err;
 }
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0) {
  log_print(3, "socket(): %s", strerror((*__errno_location ())));;
  goto err;
 }
 addr.sun_family = 1;
 strcpy(addr.sun_path, path);
 if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
  log_print(3, "bind() %s %s", path, strerror((*__errno_location ())));;
  goto err;
 }
 chmod(path, 0777);
 listen(sock, 5);
 *sock_out = sock;
 return 0;
err:
 if (dir) {
  free(dir);
 }
 if (sock >= 0) {
  unix_remove_unix_socket(path, sock);
 }
 return 1;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: rc int

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
__mode_t unix_fork()
{
__pid_t v0; // eax
int *v1; // rax
char *v2; // rax

v0 = fork();
if ( v0 < 0 )
{
v1 = _errno_location();
v2 = strerror(*v1);
log_print(3LL, "Failed to fork: %s", v2);
exit(1);
}
if ( v0 )
exit(0);
if ( setsid() < 0 )
exit(1);
return umask(0);
}

canonical_decompiled_code:
__mode_t func5()
{
__pid_t v0; // eax
int *v1; // rax
char *v2; // rax

v0 = fork();
if ( v0 < 0 )
{
v1 = _errno_location();
v2 = strerror(*v1);
log_print(3LL, "Failed to fork: %s", v2);
exit(1);
}
if ( v0 )
exit(0);
if ( setsid() < 0 )
exit(1);
return umask(0);
}

original_code:
void unix_fork(void)
{
 pid_t pid;
 pid_t sid;
 pid = fork();
 if (pid < 0) {
  log_print(3, "Failed to fork: %s", strerror((*__errno_location ())));;
  exit(1);
 } else if (pid != 0) {
  exit(0);
 }
 sid = setsid();
 if (sid < 0) {
  exit(1);
 }
 umask(0);
}

canonical_original_code:
void unix_fork()
{
 int pid;
 int sid;
 pid = fork();
 if (pid < 0) {
  log_print(3, "Failed to fork: %s", strerror((*__errno_location ())));;
  exit(1);
 } else if (pid != 0) {
  exit(0);
 }
 sid = setsid();
 if (sid < 0) {
  exit(1);
 }
 umask(0);
}

variable_types: pid int

variable_types: sid int


decompiled_code:
__uid_t unix_dropuid0()
{
__uid_t result; // eax
struct passwd *v1; // rax
struct passwd *v2; // rbx

result = gconf;
if ( *(_QWORD *)(gconf + 16LL) )
{
result = getuid();
if ( !result )
{
v1 = getpwnam(*(const char **)(gconf + 16LL));
v2 = v1;
if ( !v1 )
{
log_print(3LL, "Dropping uid 0 failed. Set a valid user.");
exit(1);
}
if ( setenv("HOME", v1->pw_dir, 1) )
{
log_print(3LL, "Setting new $HOME failed.");
exit(1);
}
if ( setgid(v2->pw_gid) )
{
log_print(3LL, "Unable to drop group privileges");
exit(1);
}
if ( setuid(v2->pw_uid) )
{
log_print(3LL, "Unable to drop user privileges");
exit(1);
}
if ( setuid(0) != -1 || (result = setgid(0), result != -1) )
{
log_print(3LL, "We still have root privileges");
exit(1);
}
}
}
return result;
}

canonical_decompiled_code:
__uid_t func7()
{
__uid_t result; // eax
struct passwd *v1; // rax
struct passwd *v2; // rbx

result = gconf;
if ( *(_QWORD *)(gconf + 16LL) )
{
result = getuid();
if ( !result )
{
v1 = getpwnam(*(const char **)(gconf + 16LL));
v2 = v1;
if ( !v1 )
{
log_print(3LL, "Dropping uid 0 failed. Set a valid user.");
exit(1);
}
if ( setenv("HOME", v1->pw_dir, 1) )
{
log_print(3LL, "Setting new $HOME failed.");
exit(1);
}
if ( setgid(v2->pw_gid) )
{
log_print(3LL, "Unable to drop group privileges");
exit(1);
}
if ( setuid(v2->pw_uid) )
{
log_print(3LL, "Unable to drop user privileges");
exit(1);
}
if ( setuid(0) != -1 || (result = setgid(0), result != -1) )
{
log_print(3LL, "We still have root privileges");
exit(1);
}
}
}
return result;
}

original_code:
void unix_dropuid0(void)
{
 struct passwd *pw;
 if (gconf->user == ((void *)0)) {
  return;
 }
 if (getuid() != 0) {
  return;
 }
 if ((pw = getpwnam(gconf->user)) == ((void *)0)) {
  log_print(3, "Dropping uid 0 failed. Set a valid user.");;
  exit(1);
 }
 if (setenv("HOME", pw->pw_dir, 1) != 0) {
  log_print(3, "Setting new $HOME failed.");;
  exit(1);
 }
 if (setgid(pw->pw_gid) != 0) {
  log_print(3, "Unable to drop group privileges");;
  exit(1);
 }
 if (setuid(pw->pw_uid) != 0) {
  log_print(3, "Unable to drop user privileges");;
  exit(1);
 }
 if (setuid(0) != -1 || setgid(0) != -1) {
  log_print(3, "We still have root privileges");;
  exit(1);
 }
}

canonical_original_code:
void unix_dropuid0()
{
 struct passwd *pw;
 if (gconf->user == ((void *)0)) {
  return;
 }
 if (getuid() != 0) {
  return;
 }
 if ((pw = getpwnam(gconf->user)) == ((void *)0)) {
  log_print(3, "Dropping uid 0 failed. Set a valid user.");;
  exit(1);
 }
 if (setenv("HOME", pw->pw_dir, 1) != 0) {
  log_print(3, "Setting new $HOME failed.");;
  exit(1);
 }
 if (setgid(pw->pw_gid) != 0) {
  log_print(3, "Unable to drop group privileges");;
  exit(1);
 }
 if (setuid(pw->pw_uid) != 0) {
  log_print(3, "Unable to drop user privileges");;
  exit(1);
 }
 if (setuid(0) != -1 || setgid(0) != -1) {
  log_print(3, "We still have root privileges");;
  exit(1);
 }
}

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


original_code:
static void compress_entries()
{
 int i;
 for (i = 0; i < g_count; i += 1) {
  while (g_cbs[i] == ((void *)0) && i < g_count) {
   g_count -= 1;
   g_cbs[i] = g_cbs[g_count];
   g_fds[i].fd = g_fds[g_count].fd;
   g_fds[i].events = g_fds[g_count].events;
  }
 }
}

variable_types: i int


decompiled_code:
int __fastcall net_set_nonblocking(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func1(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
int net_set_nonblocking(int fd)
{
 return fcntl(fd, 4, fcntl(fd, 3) | 04000);
}

variable_types: fd int


decompiled_code:
__int64 __fastcall net_add_handler(int a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

if ( !a2 )
{
log_print(3LL, "Invalid arguments.");
exit(1);
}
if ( g_count == 16 )
{
log_print(3LL, "No more space for handlers.");
exit(1);
}
if ( a1 >= 0 )
net_set_nonblocking(a1);
v2 = g_count;
v3 = g_count;
g_cbs[g_count] = a2;
*(&g_fds.fd + 2 * v3) = a1;
*(&g_fds.events + 4 * v3) = 1;
result = (unsigned int)(v2 + 1);
g_count = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

if ( !a2 )
{
log_print(3LL, "Invalid arguments.");
exit(1);
}
if ( g_count == 16 )
{
log_print(3LL, "No more space for handlers.");
exit(1);
}
if ( a1 >= 0 )
func1(a1);
v2 = g_count;
v3 = g_count;
g_cbs[g_count] = a2;
*(&g_fds.fd + 2 * v3) = a1;
*(&g_fds.events + 4 * v3) = 1;
result = (unsigned int)(v2 + 1);
g_count = result;
return result;
}

original_code:
void net_add_handler(int fd, net_callback *cb)
{
 if (cb == ((void *)0)) {
  log_print(3, "Invalid arguments.");;
  exit(1);
 }
 if (g_count == (sizeof(g_cbs) / sizeof((g_cbs)[0]))) {
  log_print(3, "No more space for handlers.");;
  exit(1);
 }
 if (fd >= 0) {
  net_set_nonblocking(fd);
 }
 g_cbs[g_count] = cb;
 g_fds[g_count].fd = fd;
 g_fds[g_count].events = 0x001;
 g_count += 1;
}

canonical_original_code:
void net_add_handler(int fd, void (*cb)(int revents, int fd))
{
 if (cb == ((void *)0)) {
  log_print(3, "Invalid arguments.");;
  exit(1);
 }
 if (g_count == (sizeof(g_cbs) / sizeof((g_cbs)[0]))) {
  log_print(3, "No more space for handlers.");;
  exit(1);
 }
 if (fd >= 0) {
  net_set_nonblocking(fd);
 }
 g_cbs[g_count] = cb;
 g_fds[g_count].fd = fd;
 g_fds[g_count].events = 0x001;
 g_count += 1;
}

variable_types:
cb
void (*)(int revents, int fd)


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

if ( !a2 )
{
fwrite("Invalid arguments.", 1uLL, 0x12uLL, stderr);
exit(1);
}
if ( g_count <= 0 )
{
LABEL_9:
log_print(3LL, "Handler not found to remove.");
exit(1);
}
result = 0LL;
while ( g_cbs[result] != a2 || *(&g_fds.fd + 2 * result) != a1 )
{
if ( ++result == g_count )
goto LABEL_9;
}
g_cbs[(int)result] = 0LL;
g_entry_removed = 1;
return result;
}

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

if ( !a2 )
{
fwrite("Invalid arguments.", 1uLL, 0x12uLL, stderr);
exit(1);
}
if ( g_count <= 0 )
{
LABEL_9:
log_print(3LL, "Handler not found to remove.");
exit(1);
}
result = 0LL;
while ( g_cbs[result] != a2 || *(&g_fds.fd + 2 * result) != a1 )
{
if ( ++result == g_count )
goto LABEL_9;
}
g_cbs[(int)result] = 0LL;
g_entry_removed = 1;
return result;
}

original_code:
void net_remove_handler(int fd, net_callback *cb)
{
 int i;
 if (cb == ((void *)0)) {
  fprintf(stderr, "Invalid arguments.");
  exit(1);
 }
 for (i = 0; i < g_count; i++) {
  if (g_cbs[i] == cb && g_fds[i].fd == fd) {
   g_cbs[i] = ((void *)0);
   g_entry_removed = 1;
   return;
  }
 }
 log_print(3, "Handler not found to remove.");;
 exit(1);
}

canonical_original_code:
void net_remove_handler(int fd, void (*cb)(int revents, int fd))
{
 int i;
 if (cb == ((void *)0)) {
  fprintf(stderr, "Invalid arguments.");
  exit(1);
 }
 for (i = 0; i < g_count; i++) {
  if (g_cbs[i] == cb && g_fds[i].fd == fd) {
   g_cbs[i] = ((void *)0);
   g_entry_removed = 1;
   return;
  }
 }
 log_print(3, "Handler not found to remove.");;
 exit(1);
}


decompiled_code:
int net_loop()
{
int result; // eax
__int64 v1; // rbx
__int64 v2; // rdi
void (__fastcall *v3)(__int64, _QWORD); // rax
time_t v4; // rax
_BOOL4 v5; // ebp

for ( result = gconf; *(_BYTE *)(gconf + 57LL); result = gconf )
{
result = poll(&g_fds, g_count, 1000);
if ( result < 0 )
break;
v4 = time(0LL);
v5 = *gconf < v4;
*gconf = v4;
if ( g_count > 0 )
{
v1 = 0LL;
do
{
v2 = (unsigned int)*(&g_fds.revents + 4 * v1);
if ( (unsigned int)v2 | v5 )
{
v3 = (void (__fastcall *)(__int64, _QWORD))g_cbs[v1];
if ( v3 )
v3(v2, *((unsigned int *)&g_fds.fd + 2 * v1));
}
++v1;
}
while ( g_count > (int)v1 );
}
if ( g_entry_removed )
{
compress_entries();
g_entry_removed = 0;
}
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
__int64 v1; // rbx
__int64 v2; // rdi
void (__fastcall *v3)(__int64, _QWORD); // rax
time_t v4; // rax
_BOOL4 v5; // ebp

for ( result = gconf; *(_BYTE *)(gconf + 57LL); result = gconf )
{
result = poll(&g_fds, g_count, 1000);
if ( result < 0 )
break;
v4 = time(0LL);
v5 = *gconf < v4;
*gconf = v4;
if ( g_count > 0 )
{
v1 = 0LL;
do
{
v2 = (unsigned int)*(&g_fds.revents + 4 * v1);
if ( (unsigned int)v2 | v5 )
{
v3 = (void (__fastcall *)(__int64, _QWORD))g_cbs[v1];
if ( v3 )
v3(v2, *((unsigned int *)&g_fds.fd + 2 * v1));
}
++v1;
}
while ( g_count > (int)v1 );
}
if ( g_entry_removed )
{
func0();
g_entry_removed = 0;
}
}
return result;
}

original_code:
void net_loop(void)
{
 time_t n;
 int revents;
 int all;
 int rc;
 int i;
 while (gconf->is_running) {
  rc = poll(g_fds, g_count, 1000);
  if (rc < 0) {
   break;
  }
  n = time(((void *)0));
  all = (n > gconf->time_now);
  gconf->time_now = n;
  for (i = 0; i < g_count; i++) {
   revents = g_fds[i].revents;
   if ((revents || all) && (g_cbs[i] != ((void *)0))) {
    g_cbs[i](revents, g_fds[i].fd);
   }
  }
  if (g_entry_removed) {
   compress_entries();
   g_entry_removed = 0;
  }
 }
}

canonical_original_code:
void net_loop()
{
 long n;
 int revents;
 int all;
 int rc;
 int i;
 while (gconf->is_running) {
  rc = poll(g_fds, g_count, 1000);
  if (rc < 0) {
   break;
  }
  n = time(((void *)0));
  all = (n > gconf->time_now);
  gconf->time_now = n;
  for (i = 0; i < g_count; i++) {
   revents = g_fds[i].revents;
   if ((revents || all) && (g_cbs[i] != ((void *)0))) {
    g_cbs[i](revents, g_fds[i].fd);
   }
  }
  if (g_entry_removed) {
   compress_entries();
   g_entry_removed = 0;
  }
 }
}

variable_types: n long

variable_types: revents int

variable_types: all int


decompiled_code:
__int64 __fastcall net_socket(const char *a1, const char *a2, int a3, int a4)
{
int v4; // eax
int v5; // eax
unsigned int v6; // ebx
socklen_t v7; // eax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v4 = *(_DWORD *)(gconf + 68LL);
if ( v4 != a4 && v4 )
{
v6 = -1;
goto LABEL_14;
}
v5 = socket(a4, (a3 != 6) + 1, a3);
v6 = v5;
if ( v5 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_print(3LL, "%s: Failed to create socket: %s", a1, v10);
LABEL_14:
close(v6);
return (unsigned int)-1;
}
if ( net_set_nonblocking(v5) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_print(3LL, "%s: Failed to make socket nonblocking: %s", a1, v12);
goto LABEL_14;
}
if ( a2 )
{
v7 = strlen(a2);
if ( setsockopt(v6, 1, 25, a2, v7) )
{
v13 = _errno_location();
v14 = strerror(*v13);
log_print(3LL, "%s: Unable to bind to device %s: %s", a1, a2, v14);
goto LABEL_14;
}
}
if ( setsockopt(v6, 1, 2, optval, 4u) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
log_print(3LL, "%s: Unable to set SO_REUSEADDR for %s: %s", a1, a2, v16);
goto LABEL_14;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, const char *a2, int a3, int a4)
{
int v4; // eax
int v5; // eax
unsigned int v6; // ebx
socklen_t v7; // eax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v4 = *(_DWORD *)(gconf + 68LL);
if ( v4 != a4 && v4 )
{
v6 = -1;
goto LABEL_14;
}
v5 = socket(a4, (a3 != 6) + 1, a3);
v6 = v5;
if ( v5 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_print(3LL, "%s: Failed to create socket: %s", a1, v10);
LABEL_14:
close(v6);
return (unsigned int)-1;
}
if ( func1(v5) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_print(3LL, "%s: Failed to make socket nonblocking: %s", a1, v12);
goto LABEL_14;
}
if ( a2 )
{
v7 = strlen(a2);
if ( setsockopt(v6, 1, 25, a2, v7) )
{
v13 = _errno_location();
v14 = strerror(*v13);
log_print(3LL, "%s: Unable to bind to device %s: %s", a1, a2, v14);
goto LABEL_14;
}
}
if ( setsockopt(v6, 1, 2, optval, 4u) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
log_print(3LL, "%s: Unable to set SO_REUSEADDR for %s: %s", a1, a2, v16);
goto LABEL_14;
}
return v6;
}

original_code:
int net_socket(const char name[], const char ifname[], const int protocol, const int af)
{
 const int opt_on = 1;
 int sock = -1;
 if (gconf->af != 0 && gconf->af != af) {
  goto fail;
 }
 if ((sock = socket(af, (protocol == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM, protocol)) < 0) {
  log_print(3, "%s: Failed to create socket: %s", name, strerror((*__errno_location ())));;
  goto fail;
 }
 if (net_set_nonblocking(sock) < 0) {
  log_print(3, "%s: Failed to make socket nonblocking: %s", name, strerror((*__errno_location ())));;
  goto fail;
 }
 if (ifname && setsockopt(sock, 1, 25, ifname, strlen(ifname))) {
  log_print(3, "%s: Unable to bind to device %s: %s", name, ifname, strerror((*__errno_location ())));;
  goto fail;
 }
 if (setsockopt(sock, 1, 2, &opt_on, sizeof(opt_on)) < 0) {
  log_print(3, "%s: Unable to set SO_REUSEADDR for %s: %s", name, ifname, strerror((*__errno_location ())));;
  goto fail;
 }
 return sock;
fail:
 close(sock);
 return -1;
}

canonical_original_code:
int net_socket(char name[], char ifname[], int protocol, int af)
{
 int opt_on = 1;
 int sock = -1;
 if (gconf->af != 0 && gconf->af != af) {
  goto fail;
 }
 if ((sock = socket(af, (protocol == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM, protocol)) < 0) {
  log_print(3, "%s: Failed to create socket: %s", name, strerror((*__errno_location ())));;
  goto fail;
 }
 if (net_set_nonblocking(sock) < 0) {
  log_print(3, "%s: Failed to make socket nonblocking: %s", name, strerror((*__errno_location ())));;
  goto fail;
 }
 if (ifname && setsockopt(sock, 1, 25, ifname, strlen(ifname))) {
  log_print(3, "%s: Unable to bind to device %s: %s", name, ifname, strerror((*__errno_location ())));;
  goto fail;
 }
 if (setsockopt(sock, 1, 2, &opt_on, sizeof(opt_on)) < 0) {
  log_print(3, "%s: Unable to set SO_REUSEADDR for %s: %s", name, ifname, strerror((*__errno_location ())));;
  goto fail;
 }
 return sock;
fail:
 close(sock);
 return -1;
}

variable_types:
name
char []

variable_types:
ifname
char []

variable_types: protocol int

variable_types: af int

variable_types: sock int


decompiled_code:
__int64 __fastcall net_bind(const char *a1, const char *a2, unsigned __int16 a3, const char *a4, int a5)
{
int v8; // eax
unsigned int v9; // ebx
socklen_t v10; // eax
int *v12; // rax
char *v13; // rbp
const char *v14; // rax
int *v15; // rax
char *v16; // rbp
const char *v17; // rax
int *v18; // rax
char *v19; // rbp
const char *v20; // rax
const char *v21; // r9
__int64 v22; // rcx
const char *v23; // rsi
struct sockaddr v24[8]; // [rsp+0h] [rbp-B8h] BYREF
int optval[11]; // [rsp+8Ch] [rbp-2Ch] BYREF

optval[0] = 1;
if ( !(unsigned __int8)addr_parse(v24, a2, "0", 0LL) )
{
log_print(3LL, "%s: Failed to parse IP address '%s'", a1, a2);
v9 = -1;
goto LABEL_9;
}
port_set(v24, a3);
v8 = net_socket(a1, a4, a5, v24[0].sa_family);
v9 = v8;
if ( v8 < 0 )
{
LABEL_9:
close(v9);
return (unsigned int)-1;
}
if ( v24[0].sa_family == 10 && setsockopt(v8, 41, 26, optval, 4u) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
v14 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to set IPV6_V6ONLY for %s: %s", a1, v14, v13);
goto LABEL_9;
}
v10 = addr_len(v24);
if ( bind(v9, v24, v10) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
v17 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to bind socket to %s: %s", a1, v17, v16);
goto LABEL_9;
}
if ( a5 == 6 && listen(v9, 5) < 0 )
{
v18 = _errno_location();
v19 = strerror(*v18);
v20 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to listen on %s: %s (%s)", a1, v20, v19, v21);
goto LABEL_9;
}
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v22 = str_addr(v24);
v23 = "%s: Bind to %s, interface %s";
if ( !a4 )
v23 = "%s: Bind to %s";
log_print(6LL, v23, a1, v22, a4);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, const char *a2, unsigned __int16 a3, const char *a4, int a5)
{
int v8; // eax
unsigned int v9; // ebx
socklen_t v10; // eax
int *v12; // rax
char *v13; // rbp
const char *v14; // rax
int *v15; // rax
char *v16; // rbp
const char *v17; // rax
int *v18; // rax
char *v19; // rbp
const char *v20; // rax
const char *v21; // r9
__int64 v22; // rcx
const char *v23; // rsi
struct sockaddr v24[8]; // [rsp+0h] [rbp-B8h] BYREF
int optval[11]; // [rsp+8Ch] [rbp-2Ch] BYREF

optval[0] = 1;
if ( !(unsigned __int8)addr_parse(v24, a2, "0", 0LL) )
{
log_print(3LL, "%s: Failed to parse IP address '%s'", a1, a2);
v9 = -1;
goto LABEL_9;
}
port_set(v24, a3);
v8 = func5(a1, a4, a5, v24[0].sa_family);
v9 = v8;
if ( v8 < 0 )
{
LABEL_9:
close(v9);
return (unsigned int)-1;
}
if ( v24[0].sa_family == 10 && setsockopt(v8, 41, 26, optval, 4u) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
v14 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to set IPV6_V6ONLY for %s: %s", a1, v14, v13);
goto LABEL_9;
}
v10 = addr_len(v24);
if ( bind(v9, v24, v10) < 0 )
{
v15 = _errno_location();
v16 = strerror(*v15);
v17 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to bind socket to %s: %s", a1, v17, v16);
goto LABEL_9;
}
if ( a5 == 6 && listen(v9, 5) < 0 )
{
v18 = _errno_location();
v19 = strerror(*v18);
v20 = (const char *)str_addr(v24);
log_print(3LL, "%s: Failed to listen on %s: %s (%s)", a1, v20, v19, v21);
goto LABEL_9;
}
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v22 = str_addr(v24);
v23 = "%s: Bind to %s, interface %s";
if ( !a4 )
v23 = "%s: Bind to %s";
log_print(6LL, v23, a1, v22, a4);
}
return v9;
}

original_code:
int net_bind(
 const char name[],
 const char addr[],
 const int port,
 const char ifname[],
 const int protocol)
{
 const int opt_on = 1;
 socklen_t addrlen;
 IP sockaddr;
 int sock = -1;
 if (!addr_parse(&sockaddr, addr, "0", 0)) {
  log_print(3, "%s: Failed to parse IP address '%s'", name, addr);;
  goto fail;
 }
 port_set(&sockaddr, port);
 if ((sock = net_socket(name, ifname, protocol, sockaddr.ss_family)) < 0) {
  goto fail;
 }
 if (sockaddr.ss_family == 10) {
  if (setsockopt(sock, IPPROTO_IPV6, 26, &opt_on, sizeof(opt_on)) < 0) {
   log_print(3, "%s: Failed to set IPV6_V6ONLY for %s: %s", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
   goto fail;
  }
 }
 addrlen = addr_len(&sockaddr);
 if (bind(sock, (struct sockaddr*) &sockaddr, addrlen) < 0) {
  log_print(3, "%s: Failed to bind socket to %s: %s", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
  goto fail;
 }
 if (protocol == IPPROTO_TCP && listen(sock, 5) < 0) {
  log_print(3, "%s: Failed to listen on %s: %s (%s)", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
  goto fail;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, ifname ? "%s: Bind to %s, interface %s" : "%s: Bind to %s", name, str_addr(&sockaddr), ifname); } while (0);
 return sock;
fail:
 close(sock);
 return -1;
}

canonical_original_code:
int net_bind(char name[], char addr[], int port, char ifname[], int protocol)
{
 int opt_on = 1;
 unsigned int addrlen;
 struct sockaddr_storage sockaddr;
 int sock = -1;
 if (!addr_parse(&sockaddr, addr, "0", 0)) {
  log_print(3, "%s: Failed to parse IP address '%s'", name, addr);;
  goto fail;
 }
 port_set(&sockaddr, port);
 if ((sock = net_socket(name, ifname, protocol, sockaddr.ss_family)) < 0) {
  goto fail;
 }
 if (sockaddr.ss_family == 10) {
  if (setsockopt(sock, IPPROTO_IPV6, 26, &opt_on, sizeof(opt_on)) < 0) {
   log_print(3, "%s: Failed to set IPV6_V6ONLY for %s: %s", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
   goto fail;
  }
 }
 addrlen = addr_len(&sockaddr);
 if (bind(sock, (struct sockaddr *) &sockaddr, addrlen) < 0) {
  log_print(3, "%s: Failed to bind socket to %s: %s", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
  goto fail;
 }
 if (protocol == IPPROTO_TCP && listen(sock, 5) < 0) {
  log_print(3, "%s: Failed to listen on %s: %s (%s)", name, str_addr(&sockaddr), strerror((*__errno_location ())));;
  goto fail;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, ifname ? "%s: Bind to %s, interface %s" : "%s: Bind to %s", name, str_addr(&sockaddr), ifname); } while (0);
 return sock;
fail:
 close(sock);
 return -1;
}

variable_types:
addr
char []

variable_types: port int

variable_types:
addrlen
unsigned int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
__int64 net_free()
{
__int64 v0; // rbx
__int64 result; // rax

if ( g_count > 0 )
{
v0 = 0LL;
do
{
g_cbs[v0] = 0LL;
close(*(&g_fds.fd + 2 * v0));
result = (int)v0;
*((_QWORD *)&g_fds + (int)v0++) = 0LL;
}
while ( g_count > (int)v0 );
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rbx
__int64 result; // rax

if ( g_count > 0 )
{
v0 = 0LL;
do
{
g_cbs[v0] = 0LL;
close(*(&g_fds.fd + 2 * v0));
result = (int)v0;
*((_QWORD *)&g_fds + (int)v0++) = 0LL;
}
while ( g_count > (int)v0 );
}
return result;
}

original_code:
void net_free(void)
{
 int i;
 for (i = 0; i < g_count; i++) {
  g_cbs[i] = ((void *)0);
  close(g_fds[i].fd);
  g_fds[i] = (struct pollfd){ 0 };
 }
}

canonical_original_code:
void net_free()
{
 int i;
 for (i = 0; i < g_count; i++) {
  g_cbs[i] = ((void *)0);
  close(g_fds[i].fd);
  g_fds[i] = (struct pollfd){ 0 };
 }
}

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


decompiled_code:
__int64 announces_announce()
{
__int64 result; // rax
__int64 v1; // rbp
__int64 i; // rbx

result = gconf;
v1 = *gconf;
for ( i = g_values; i; i = *(_QWORD *)i )
{
if ( *(_QWORD *)(i + 296) < v1 )
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "Announce %s:%hu", (const char *)(i + 28), *(unsigned int *)(i + 284));
result = kad_announce_once(i + 8, *(unsigned int *)(i + 284));
*(_QWORD *)(i + 296) = v1 + 1200;
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__int64 v1; // rbp
__int64 i; // rbx

result = gconf;
v1 = *gconf;
for ( i = g_values; i; i = *(_QWORD *)i )
{
if ( *(_QWORD *)(i + 296) < v1 )
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "Announce %s:%hu", (const char *)(i + 28), *(unsigned int *)(i + 284));
result = kad_announce_once(i + 8, *(unsigned int *)(i + 284));
*(_QWORD *)(i + 296) = v1 + 1200;
}
}
return result;
}

original_code:
static void announces_announce(void)
{
 struct value_t *value;
 time_t now;
 now = (gconf->time_now);
 value = g_values;
 while (value) {
  if (value->refresh < now) {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "Announce %s:%hu", value->query, value->port); } while (0);
   kad_announce_once(value->id, value->port);
   value->refresh = now + (20*60);
  }
  value = value->next;
 }
}

canonical_original_code:
static void announces_announce()
{
 struct value_t *value;
 long now;
 now = (gconf->time_now);
 value = g_values;
 while (value) {
  if (value->refresh < now) {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "Announce %s:%hu", value->query, value->port); } while (0);
   kad_announce_once(value->id, value->port);
   value->refresh = now + (20*60);
  }
  value = value->next;
 }
}

variable_types: struct value_t { struct value_t *next; unsigned char id[20]; char query[256]; int port; long lifetime; long refresh; } *

variable_types: now long


decompiled_code:
__int64 announces_get()
{
return g_values;
}

canonical_decompiled_code:
__int64 func1()
{
return g_values;
}

original_code:
struct value_t* announces_get(void)
{
 return g_values;
}

canonical_original_code:
struct value_t *announces_get()
{
 return g_values;
}

return_type: struct value_t { struct value_t *next; unsigned char id[20]; char query[256]; int port; long lifetime; long refresh; } *


decompiled_code:
void __fastcall value_free(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func4(void *a1)
{
free(a1);
}

original_code:
void value_free(struct value_t *value)
{
 free(value);
}

user_defined_types: struct value_t { struct value_t *next; unsigned char id[20]; char query[256]; int port; long lifetime; long refresh; }


decompiled_code:
void announces_expire()
{
_QWORD *v0; // rdi
_QWORD *v1; // rdx

v0 = (_QWORD *)g_values;
if ( g_values )
{
v1 = 0LL;
while ( v0[36] >= *gconf )
{
v1 = v0;
if ( !*v0 )
return;
v0 = (_QWORD *)*v0;
}
if ( v1 )
*v1 = *v0;
else
g_values = *v0;
value_free(v0);
}
}

canonical_decompiled_code:
void func5()
{
_QWORD *v0; // rdi
_QWORD *v1; // rdx

v0 = (_QWORD *)g_values;
if ( g_values )
{
v1 = 0LL;
while ( v0[36] >= *gconf )
{
v1 = v0;
if ( !*v0 )
return;
v0 = (_QWORD *)*v0;
}
if ( v1 )
*v1 = *v0;
else
g_values = *v0;
func4(v0);
}
}

original_code:
static void announces_expire(void)
{
 struct value_t *pre;
 struct value_t *cur;
 time_t now;
 now = (gconf->time_now);
 pre = ((void *)0);
 cur = g_values;
 while (cur) {
  if (cur->lifetime < now) {
   if (pre) {
    pre->next = cur->next;
   } else {
    g_values = cur->next;
   }
   value_free(cur);
   return;
  }
  pre = cur;
  cur = cur->next;
 }
}

canonical_original_code:
static void announces_expire()
{
 struct value_t *pre;
 struct value_t *cur;
 long now;
 now = (gconf->time_now);
 pre = ((void *)0);
 cur = g_values;
 while (cur) {
  if (cur->lifetime < now) {
   if (pre) {
    pre->next = cur->next;
   } else {
    g_values = cur->next;
   }
   value_free(cur);
   return;
  }
  pre = cur;
  cur = cur->next;
 }
}


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

if ( *gconf >= g_announces_expire )
{
announces_expire();
g_announces_expire = time_add_mins(1LL);
}
result = gconf;
if ( *gconf >= g_announces_announce )
{
result = kad_count_nodes(0LL);
if ( (_DWORD)result )
{
announces_announce();
result = time_add_mins(1LL);
g_announces_announce = result;
}
}
return result;
}

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

if ( *gconf >= g_announces_expire )
{
func5();
g_announces_expire = time_add_mins(1LL);
}
result = gconf;
if ( *gconf >= g_announces_announce )
{
result = kad_count_nodes(0LL);
if ( (_DWORD)result )
{
func0();
result = time_add_mins(1LL);
g_announces_announce = result;
}
}
return result;
}

original_code:
static void announces_handle(int _rc, int _sock)
{
 if (g_announces_expire <= (gconf->time_now)) {
  announces_expire();
  g_announces_expire = time_add_mins(1);
 }
 if (g_announces_announce <= (gconf->time_now) && kad_count_nodes(0) != 0) {
  announces_announce();
  g_announces_announce = time_add_mins(1);
 }
}


decompiled_code:
__int64 announces_setup()
{
return net_add_handler(0xFFFFFFFFLL, announces_handle);
}

canonical_decompiled_code:
__int64 func7()
{
return net_add_handler(0xFFFFFFFFLL, announces_handle);
}

original_code:
void announces_setup(void)
{
 net_add_handler(-1, &announces_handle);
}

canonical_original_code:
void announces_setup()
{
 net_add_handler(-1, &announces_handle);
}


decompiled_code:
void announces_free()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = (_QWORD *)g_values;
if ( g_values )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
value_free(v1);
}
while ( v0 );
}
g_values = 0LL;
}

canonical_decompiled_code:
void func8()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = (_QWORD *)g_values;
if ( g_values )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
func4(v1);
}
while ( v0 );
}
g_values = 0LL;
}

original_code:
void announces_free(void)
{
 struct value_t *cur;
 struct value_t *next;
 cur = g_values;
 while (cur) {
  next = cur->next;
  value_free(cur);
  cur = next;
 }
 g_values = ((void *)0);
}

canonical_original_code:
void announces_free()
{
 struct value_t *cur;
 struct value_t *next;
 cur = g_values;
 while (cur) {
  next = cur->next;
  value_free(cur);
  cur = next;
 }
 g_values = ((void *)0);
}


decompiled_code:
_BOOL8 __fastcall tls_client_get_id(void *s, size_t n, char *a3)
{
unsigned int v3; // ebx
char *v5; // rdx
_BOOL8 result; // rax
size_t v7; // rax
int v8; // er12
int v9; // ebp
unsigned __int64 v10; // rcx
unsigned int v11; // ebx
unsigned int v12; // ebx
unsigned int v13; // eax
_BYTE v14[112]; // [rsp+0h] [rbp-B8h] BYREF
__int64 v15[9]; // [rsp+70h] [rbp-48h] BYREF

v3 = n;
v5 = strchr(a3, 46);
result = 1LL;
if ( v5 )
{
mbedtls_sha256_init(v14);
v7 = strlen(a3);
v8 = mbedtls_sha256_update_ret(v14, a3, v7);
v9 = mbedtls_sha256_finish_ret(v14, v15);
memset(s, 0, n);
if ( n > 0x20 )
v3 = 32;
if ( v3 >= 8 )
{
*(_QWORD *)s = v15[0];
*(_QWORD *)((char *)s + v3 - 8) = *(__int64 *)((char *)&v15[-1] + v3);
v10 = ((unsigned __int64)s + 8) & 0xFFFFFFFFFFFFFFF8LL;
v11 = ((_DWORD)s - v10 + v3) & 0xFFFFFFF8;
if ( v11 >= 8 )
{
v12 = v11 & 0xFFFFFFF8;
v13 = 0;
do
{
*(_QWORD *)(v10 + v13) = *(_QWORD *)((char *)v15 - ((char *)s - v10) + v13);
v13 += 8;
}
while ( v13 < v12 );
}
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)s = v15[0];
*(_DWORD *)((char *)s + v3 - 4) = *(_DWORD *)((char *)v15 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)s = v15[0];
if ( (v3 & 2) != 0 )
*(_WORD *)((char *)s + v3 - 2) = *(_WORD *)((char *)v15 + v3 - 2);
}
result = (v9 | v8) != 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(void *s, size_t n, char *a3)
{
unsigned int v3; // ebx
char *v5; // rdx
_BOOL8 result; // rax
size_t v7; // rax
int v8; // er12
int v9; // ebp
unsigned __int64 v10; // rcx
unsigned int v11; // ebx
unsigned int v12; // ebx
unsigned int v13; // eax
_BYTE v14[112]; // [rsp+0h] [rbp-B8h] BYREF
__int64 v15[9]; // [rsp+70h] [rbp-48h] BYREF

v3 = n;
v5 = strchr(a3, 46);
result = 1LL;
if ( v5 )
{
mbedtls_sha256_init(v14);
v7 = strlen(a3);
v8 = mbedtls_sha256_update_ret(v14, a3, v7);
v9 = mbedtls_sha256_finish_ret(v14, v15);
memset(s, 0, n);
if ( n > 0x20 )
v3 = 32;
if ( v3 >= 8 )
{
*(_QWORD *)s = v15[0];
*(_QWORD *)((char *)s + v3 - 8) = *(__int64 *)((char *)&v15[-1] + v3);
v10 = ((unsigned __int64)s + 8) & 0xFFFFFFFFFFFFFFF8LL;
v11 = ((_DWORD)s - v10 + v3) & 0xFFFFFFF8;
if ( v11 >= 8 )
{
v12 = v11 & 0xFFFFFFF8;
v13 = 0;
do
{
*(_QWORD *)(v10 + v13) = *(_QWORD *)((char *)v15 - ((char *)s - v10) + v13);
v13 += 8;
}
while ( v13 < v12 );
}
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)s = v15[0];
*(_DWORD *)((char *)s + v3 - 4) = *(_DWORD *)((char *)v15 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)s = v15[0];
if ( (v3 & 2) != 0 )
*(_WORD *)((char *)s + v3 - 2) = *(_WORD *)((char *)v15 + v3 - 2);
}
result = (v9 | v8) != 0;
}
return result;
}

original_code:
int tls_client_get_id(uint8_t id[], size_t len, const char query[])
{
 uint8_t hash[32];
 int ret = 0;
 if (strchr(query, '.')) {
  mbedtls_sha256_context ctx;
  mbedtls_sha256_init(&ctx);
  ret |= mbedtls_sha256_update_ret(&ctx, (uint8_t*) &query[0], strlen(query));
  ret |= mbedtls_sha256_finish_ret(&ctx, hash);
  memset(id, 0, len);
  memcpy(id, hash, ((len) <= (sizeof(hash)) ? (len) : (sizeof(hash))));
  return (ret == 0) ? 0 : 1;
 }
 return 1;
}

canonical_original_code:
int tls_client_get_id(unsigned char id[], unsigned long len, char query[])
{
 unsigned char hash[32];
 int ret = 0;
 if (strchr(query, '.')) {
  struct mbedtls_sha256_context ctx;
  mbedtls_sha256_init(&ctx);
  ret |= mbedtls_sha256_update_ret(&ctx, (unsigned char *) &query[0], strlen(query));
  ret |= mbedtls_sha256_finish_ret(&ctx, hash);
  memset(id, 0, len);
  memcpy(id, hash, ((len) <= (sizeof(hash)) ? (len) : (sizeof(hash))));
  return (ret == 0) ? 0 : 1;
 }
 return 1;
}

variable_types:
id
unsigned char []

variable_types:
len
unsigned long

variable_types:
query
char []

variable_types:
hash
unsigned char [32]

variable_types: ret int

variable_types: struct mbedtls_sha256_context { unsigned int total[2]; unsigned int state[8]; unsigned char buffer[64]; int is224; }


decompiled_code:
__int64 __fastcall tls_client_add_ca(const char *a1)
{
int v1; // eax
char v3[120]; // [rsp+0h] [rbp-78h] BYREF

if ( !g_client_enable )
{
mbedtls_x509_crt_init(&g_cacert);
g_client_enable = 1;
}
v1 = mbedtls_x509_crt_parse_file(&g_cacert, a1);
if ( v1 >= 0 || (v1 = mbedtls_x509_crt_parse_path(&g_cacert, a1), v1 >= 0) )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "TLS-Client: Loaded certificates from %s (%d skipped)", a1, (unsigned int)v1);
}
else
{
mbedtls_strerror((unsigned int)v1, v3, 100LL);
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Client: Failed to load CA root certificates from %s (%s)", a1, v3);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
int v1; // eax
char v3[120]; // [rsp+0h] [rbp-78h] BYREF

if ( !g_client_enable )
{
mbedtls_x509_crt_init(&g_cacert);
g_client_enable = 1;
}
v1 = mbedtls_x509_crt_parse_file(&g_cacert, a1);
if ( v1 >= 0 || (v1 = mbedtls_x509_crt_parse_path(&g_cacert, a1), v1 >= 0) )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "TLS-Client: Loaded certificates from %s (%d skipped)", a1, (unsigned int)v1);
}
else
{
mbedtls_strerror((unsigned int)v1, v3, 100LL);
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Client: Failed to load CA root certificates from %s (%s)", a1, v3);
}
return 0LL;
}

original_code:
int tls_client_add_ca(const char path[])
{
 char error_buf[100];
 int ret;
 if (g_client_enable == 0) {
  mbedtls_x509_crt_init(&g_cacert);
  g_client_enable = 1;
 }
 if (((ret = mbedtls_x509_crt_parse_file(&g_cacert, path)) < 0) &&
  ((ret = mbedtls_x509_crt_parse_path(&g_cacert, path)) < 0)) {
  mbedtls_strerror(ret, error_buf, sizeof(error_buf));
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Client: Failed to load CA root certificates from %s (%s)", path, error_buf); } while (0);
  return 0;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "TLS-Client: Loaded certificates from %s (%d skipped)", path, ret); } while (0);
 return 0;
}

canonical_original_code:
int tls_client_add_ca(char path[])
{
 char error_buf[100];
 int ret;
 if (g_client_enable == 0) {
  mbedtls_x509_crt_init(&g_cacert);
  g_client_enable = 1;
 }
 if (((ret = mbedtls_x509_crt_parse_file(&g_cacert, path)) < 0) &&
  ((ret = mbedtls_x509_crt_parse_path(&g_cacert, path)) < 0)) {
  mbedtls_strerror(ret, error_buf, sizeof(error_buf));
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Client: Failed to load CA root certificates from %s (%s)", path, error_buf); } while (0);
  return 0;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "TLS-Client: Loaded certificates from %s (%d skipped)", path, ret); } while (0);
 return 0;
}

variable_types:
path
char []


decompiled_code:
__int64 tls_client_setup()
{
__int64 result; // rax
int v1; // eax
unsigned int v2; // ebx

result = (unsigned int)g_client_enable;
if ( g_client_enable )
{
if ( dword_A830 <= 0 )
{
log_print(3LL, "TLS-Client: No root CA certificates could be loaded.");
result = 1LL;
}
else
{
mbedtls_ctr_drbg_init(&g_drbg);
mbedtls_entropy_init(&g_entropy);
v1 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v1 )
{
log_print(3LL, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v1);
result = 1LL;
}
else
{
mbedtls_ssl_init(&g_tls_resources);
mbedtls_net_init(&dword_B88);
mbedtls_ssl_init(&unk_D10);
mbedtls_net_init(&dword_ED8);
mbedtls_ssl_config_init(&g_conf);
v2 = mbedtls_ssl_config_defaults(&g_conf, 0LL, 0LL, 0LL);
if ( v2 )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -v2);
result = 1LL;
}
else
{
mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, 0LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, 0LL);
LODWORD(result) = mbedtls_ssl_setup(&g_tls_resources, &g_conf);
if ( (_DWORD)result || (result = mbedtls_ssl_setup(&unk_D10, &g_conf), (_DWORD)result) )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_setup returned -0x%x", (unsigned int)-(int)result);
result = v2;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 result; // rax
int v1; // eax
unsigned int v2; // ebx

result = (unsigned int)g_client_enable;
if ( g_client_enable )
{
if ( dword_A830 <= 0 )
{
log_print(3LL, "TLS-Client: No root CA certificates could be loaded.");
result = 1LL;
}
else
{
mbedtls_ctr_drbg_init(&g_drbg);
mbedtls_entropy_init(&g_entropy);
v1 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v1 )
{
log_print(3LL, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v1);
result = 1LL;
}
else
{
mbedtls_ssl_init(&g_tls_resources);
mbedtls_net_init(&dword_B88);
mbedtls_ssl_init(&unk_D10);
mbedtls_net_init(&dword_ED8);
mbedtls_ssl_config_init(&g_conf);
v2 = mbedtls_ssl_config_defaults(&g_conf, 0LL, 0LL, 0LL);
if ( v2 )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -v2);
result = 1LL;
}
else
{
mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, 0LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, 0LL);
LODWORD(result) = mbedtls_ssl_setup(&g_tls_resources, &g_conf);
if ( (_DWORD)result || (result = mbedtls_ssl_setup(&unk_D10, &g_conf), (_DWORD)result) )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_setup returned -0x%x", (unsigned int)-(int)result);
result = v2;
}
}
}
}
}
return result;
}

original_code:
int tls_client_setup(void)
{
 const char *pers = "kadnode";
 int ret;
 int i;
 if (g_client_enable == 0) {
  return 0;
 }
 if (g_cacert.version <= 0) {
  log_print(3, "TLS-Client: No root CA certificates could be loaded.");;
  return 1;
 }
 mbedtls_ctr_drbg_init(&g_drbg);
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (const unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_init(&g_tls_resources[i].ssl);
  mbedtls_net_init(&g_tls_resources[i].fdc);
 }
 mbedtls_ssl_config_init(&g_conf);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  0,
  0,
  0)) != 0) {
  log_print(3, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, ((void *)0));
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, ((void *)0));
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  if ((ret = mbedtls_ssl_setup(&g_tls_resources[i].ssl, &g_conf)) != 0) {
   log_print(3, "TLS-Client: mbedtls_ssl_setup returned -0x%x", -ret);;
   return 0;
  }
 }
 return 0;
}

canonical_original_code:
int tls_client_setup()
{
 char *pers = "kadnode";
 int ret;
 int i;
 if (g_client_enable == 0) {
  return 0;
 }
 if (g_cacert.version <= 0) {
  log_print(3, "TLS-Client: No root CA certificates could be loaded.");;
  return 1;
 }
 mbedtls_ctr_drbg_init(&g_drbg);
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_init(&g_tls_resources[i].ssl);
  mbedtls_net_init(&g_tls_resources[i].fdc);
 }
 mbedtls_ssl_config_init(&g_conf);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  0,
  0,
  0)) != 0) {
  log_print(3, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, ((void *)0));
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, ((void *)0));
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  if ((ret = mbedtls_ssl_setup(&g_tls_resources[i].ssl, &g_conf)) != 0) {
   log_print(3, "TLS-Client: mbedtls_ssl_setup returned -0x%x", -ret);;
   return 0;
  }
 }
 return 0;
}

variable_types:
pers
char *

variable_types: i int


decompiled_code:
__int64 tls_client_free()
{
mbedtls_ssl_free(&g_tls_resources);
mbedtls_net_free(&dword_B88);
mbedtls_ssl_free(&unk_D10);
mbedtls_net_free(&dword_ED8);
mbedtls_x509_crt_free(&g_cacert);
mbedtls_ssl_config_free(&g_conf);
mbedtls_entropy_free(&g_entropy);
return mbedtls_ctr_drbg_free(&g_drbg);
}

canonical_decompiled_code:
__int64 func10()
{
mbedtls_ssl_free(&g_tls_resources);
mbedtls_net_free(&dword_B88);
mbedtls_ssl_free(&unk_D10);
mbedtls_net_free(&dword_ED8);
mbedtls_x509_crt_free(&g_cacert);
mbedtls_ssl_config_free(&g_conf);
mbedtls_entropy_free(&g_entropy);
return mbedtls_ctr_drbg_free(&g_drbg);
}

original_code:
void tls_client_free(void)
{
 int i;
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_free(&g_tls_resources[i].ssl);
  mbedtls_net_free(&g_tls_resources[i].fdc);
 }
 mbedtls_x509_crt_free(&g_cacert);
 mbedtls_ssl_config_free(&g_conf);
 mbedtls_entropy_free(&g_entropy);
 mbedtls_ctr_drbg_free(&g_drbg);
}

canonical_original_code:
void tls_client_free()
{
 int i;
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_free(&g_tls_resources[i].ssl);
  mbedtls_net_free(&g_tls_resources[i].fdc);
 }
 mbedtls_x509_crt_free(&g_cacert);
 mbedtls_ssl_config_free(&g_conf);
 mbedtls_entropy_free(&g_entropy);
 mbedtls_ctr_drbg_free(&g_drbg);
}


decompiled_code:
char *log_time()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
clock_gettime(1, &v1);
if ( log_start == 0uLL )
clock_gettime(1, &log_start);
sprintf(
buf_0,
"[%8.2f] ",
(double)SLODWORD(v1.tv_nsec) * 0.000000001
+ (double)SLODWORD(v1.tv_sec)
- ((double)SLODWORD(log_start.tv_nsec) * 0.000000001
+ (double)SLODWORD(log_start.tv_sec)));
return buf_0;
}

canonical_decompiled_code:
char *func0()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
clock_gettime(1, &v1);
if ( log_start == 0uLL )
clock_gettime(1, &log_start);
sprintf(
buf_0,
"[%8.2f] ",
(double)SLODWORD(v1.tv_nsec) * 0.000000001
+ (double)SLODWORD(v1.tv_sec)
- ((double)SLODWORD(log_start.tv_nsec) * 0.000000001
+ (double)SLODWORD(log_start.tv_sec)));
return buf_0;
}

original_code:
const char *log_time()
{
 static char buf[16];
 struct timespec now = { 0, 0 };
 clock_gettime(1, &now);
 if (log_start.tv_sec == 0 && log_start.tv_nsec == 0) {
  clock_gettime(1, &log_start);
 }
 sprintf(buf, "[%8.2f] ",
  ((double) now.tv_sec + 1.0e-9 * (double)now.tv_nsec) -
  ((double) log_start.tv_sec + 1.0e-9 * (double)log_start.tv_nsec)
 );
 return buf;
}

variable_types:
buf
char [16]

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

return_type: char *


decompiled_code:
__int64 __fastcall peerfile_import_peer(const char *a1)
{
int v1; // eax
unsigned int v2; // ebp
char v3; // r12
char v4; // dl
__int64 result; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
_BYTE v10[152]; // [rsp+0h] [rbp-98h] BYREF

memset(v10, 0, 0x80uLL);
v1 = *(_DWORD *)(gconf + 68LL);
v2 = v1 & 0xFFFFFFFD;
if ( (!v1 || v1 == 10) && (unsigned __int8)addr_parse(v10, a1, "6881", 10LL) )
{
v3 = kad_ping(v10);
if ( v2 || !(unsigned __int8)addr_parse(v10, a1, "6881", 2LL) )
{
LABEL_10:
result = 1LL;
if ( !v3 )
{
result = 0LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(4LL, "PEERFILE: Cannot ping address '%s': %s", a1, v9);
result = 0LL;
}
}
return result;
}
LABEL_6:
v4 = kad_ping(v10);
result = 1LL;
if ( v4 )
return result;
goto LABEL_10;
}
if ( !v2 && (unsigned __int8)addr_parse(v10, a1, "6881", 2LL) )
{
v3 = 0;
goto LABEL_6;
}
result = 0LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
log_print(4LL, "PEERFILE: Cannot parse/resolve address '%s': %s", a1, v7);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
unsigned int v2; // ebp
char v3; // r12
char v4; // dl
__int64 result; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
_BYTE v10[152]; // [rsp+0h] [rbp-98h] BYREF

memset(v10, 0, 0x80uLL);
v1 = *(_DWORD *)(gconf + 68LL);
v2 = v1 & 0xFFFFFFFD;
if ( (!v1 || v1 == 10) && (unsigned __int8)addr_parse(v10, a1, "6881", 10LL) )
{
v3 = kad_ping(v10);
if ( v2 || !(unsigned __int8)addr_parse(v10, a1, "6881", 2LL) )
{
LABEL_10:
result = 1LL;
if ( !v3 )
{
result = 0LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(4LL, "PEERFILE: Cannot ping address '%s': %s", a1, v9);
result = 0LL;
}
}
return result;
}
LABEL_6:
v4 = kad_ping(v10);
result = 1LL;
if ( v4 )
return result;
goto LABEL_10;
}
if ( !v2 && (unsigned __int8)addr_parse(v10, a1, "6881", 2LL) )
{
v3 = 0;
goto LABEL_6;
}
result = 0LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
{
v6 = _errno_location();
v7 = strerror(*v6);
log_print(4LL, "PEERFILE: Cannot parse/resolve address '%s': %s", a1, v7);
result = 0LL;
}
return result;
}

original_code:
static int peerfile_import_peer(const char addr_str[])
{
 const char *port_str = "6881";
 _Bool parsed = 0;
 _Bool pinged = 0;
 IP addr = {0};
 int af = gconf->af;
 if (af == 0 || af == 10) {
  if (addr_parse(&addr, addr_str, port_str, 10)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (af == 0 || af == 2) {
  if (addr_parse(&addr, addr_str, port_str, 2)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (!parsed) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot parse/resolve address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 if (!pinged) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot ping address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 return 1;
}

canonical_original_code:
static int peerfile_import_peer(char addr_str[])
{
 char *port_str = "6881";
 _Bool parsed = 0;
 _Bool pinged = 0;
 struct sockaddr_storage addr = {0};
 int af = gconf->af;
 if (af == 0 || af == 10) {
  if (addr_parse(&addr, addr_str, port_str, 10)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (af == 0 || af == 2) {
  if (addr_parse(&addr, addr_str, port_str, 2)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (!parsed) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot parse/resolve address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 if (!pinged) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot ping address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 return 1;
}

variable_types: af int