getifaddr

repo: zyclonite/dlnaproxy

decompiled_code:
__int64 __fastcall getifaddr(char *src, char *s, socklen_t len)
{
int v4; // eax
int v5; // ebp
int v6; // ebx
int v7; // eax
int *v8; // rax
char *v9; // rax
__int64 result; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17; // eax
int v18; // [rsp+Ch] [rbp-5Ch] BYREF
char dest[20]; // [rsp+10h] [rbp-58h] BYREF
unsigned int cp[17]; // [rsp+24h] [rbp-44h] BYREF

v18 = 40;
v4 = socket(2, 2, 0);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_err(2LL, 0LL, "getifaddr.c", 64LL, "socket(PF_INET, SOCK_DGRAM): %s\n", v9);
result = 0xFFFFFFFFLL;
}
else
{
v5 = v4;
strncpy(dest, src, 0x10uLL);
if ( ioctl(v5, 0x8915uLL, dest, &v18) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_err(2LL, 0LL, "getifaddr.c", 70LL, "ioctl(s, SIOCGIFADDR, ...): %s\n", v12);
close(v5);
result = 0xFFFFFFFFLL;
}
else if ( inet_ntop(2, cp, s, len) )
{
v6 = ioctl(v5, 0x891BuLL, dest, &v18);
if ( v6 )
{
v15 = _errno_location();
v16 = strerror(*v15);
log_err(2LL, 0LL, "getifaddr.c", 98LL, "ioctl(s, SIOCGIFNETMASK, ...): %s\n", v16);
}
else
{
v7 = _bswap_32(cp[0]);
if ( (v7 & 1) == 0 )
{
v6 = 1;
while ( !_bittest(&v7, v6) )
{
if ( ++v6 == 32 )
goto LABEL_14;
}
}
v17 = strlen(s);
snprintf(&s[v17], (int)(len - v17), "/%u", (unsigned int)(32 - v6));
}
LABEL_14:
close(v5);
result = 0LL;
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_err(2LL, 0LL, "getifaddr.c", 77LL, "inet_ntop(): %s\n", v14);
close(v5);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src, char *s, socklen_t len)
{
int v4; // eax
int v5; // ebp
int v6; // ebx
int v7; // eax
int *v8; // rax
char *v9; // rax
__int64 result; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17; // eax
int v18; // [rsp+Ch] [rbp-5Ch] BYREF
char dest[20]; // [rsp+10h] [rbp-58h] BYREF
unsigned int cp[17]; // [rsp+24h] [rbp-44h] BYREF

v18 = 40;
v4 = socket(2, 2, 0);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_err(2LL, 0LL, "getifaddr.c", 64LL, "socket(PF_INET, SOCK_DGRAM): %s\n", v9);
result = 0xFFFFFFFFLL;
}
else
{
v5 = v4;
strncpy(dest, src, 0x10uLL);
if ( ioctl(v5, 0x8915uLL, dest, &v18) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_err(2LL, 0LL, "getifaddr.c", 70LL, "ioctl(s, SIOCGIFADDR, ...): %s\n", v12);
close(v5);
result = 0xFFFFFFFFLL;
}
else if ( inet_ntop(2, cp, s, len) )
{
v6 = ioctl(v5, 0x891BuLL, dest, &v18);
if ( v6 )
{
v15 = _errno_location();
v16 = strerror(*v15);
log_err(2LL, 0LL, "getifaddr.c", 98LL, "ioctl(s, SIOCGIFNETMASK, ...): %s\n", v16);
}
else
{
v7 = func0(cp[0]);
if ( (v7 & 1) == 0 )
{
v6 = 1;
while ( !_bittest(&v7, v6) )
{
if ( ++v6 == 32 )
goto LABEL_14;
}
}
v17 = strlen(s);
snprintf(&s[v17], (int)(len - v17), "/%u", (unsigned int)(32 - v6));
}
LABEL_14:
close(v5);
result = 0LL;
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_err(2LL, 0LL, "getifaddr.c", 77LL, "inet_ntop(): %s\n", v14);
close(v5);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int
getifaddr(const char * ifname, char * buf, int len)
{
 int s;
 struct ifreq ifr;
 int ifrlen;
 struct sockaddr_in * addr;
 uint32_t mask;
 int i;
 ifrlen = sizeof(ifr);
 s = socket(2, SOCK_DGRAM, 0);
 if(s < 0)
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 64, "socket(PF_INET, SOCK_DGRAM): %s\n", strerror((*__errno_location ()))); };
  return -1;
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, ifname, 16);
 if(ioctl(s, 0x8915, &ifr, &ifrlen) < 0)
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 70, "ioctl(s, SIOCGIFADDR, ...): %s\n", strerror((*__errno_location ()))); };
  close(s);
  return -1;
 }
 addr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
 if(!inet_ntop(2, &addr->sin_addr, buf, len))
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 77, "inet_ntop(): %s\n", strerror((*__errno_location ()))); };
  close(s);
  return -1;
 }
 if(ioctl(s, 0x891b, &ifr, &ifrlen) == 0)
 {
  addr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_netmask;
  mask = ntohl(addr->sin_addr.s_addr);
  for (i = 0; i < 32; i++)
  {
   if ((mask >> i) & 1)
    break;
  }
  mask = 32 - i;
  if (mask)
  {
   i = strlen(buf);
   snprintf(buf+i, len-i, "/%u", mask);
  }
 }
 else
  { log_err(2, L_GENERAL, "getifaddr.c", 98, "ioctl(s, SIOCGIFNETMASK, ...): %s\n", strerror((*__errno_location ()))); };
 close(s);
 return 0;
}

canonical_original_code:
int getifaddr(char *ifname, char *buf, int len)
{
 int s;
 struct ifreq ifr;
 int ifrlen;
 struct sockaddr_in *addr;
 unsigned int mask;
 int i;
 ifrlen = sizeof(ifr);
 s = socket(2, SOCK_DGRAM, 0);
 if(s < 0)
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 64, "socket(PF_INET, SOCK_DGRAM): %s\n", strerror((*__errno_location ()))); };
  return -1;
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, ifname, 16);
 if(ioctl(s, 0x8915, &ifr, &ifrlen) < 0)
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 70, "ioctl(s, SIOCGIFADDR, ...): %s\n", strerror((*__errno_location ()))); };
  close(s);
  return -1;
 }
 addr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
 if(!inet_ntop(2, &addr->sin_addr, buf, len))
 {
  { log_err(2, L_GENERAL, "getifaddr.c", 77, "inet_ntop(): %s\n", strerror((*__errno_location ()))); };
  close(s);
  return -1;
 }
 if(ioctl(s, 0x891b, &ifr, &ifrlen) == 0)
 {
  addr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_netmask;
  mask = ntohl(addr->sin_addr.s_addr);
  for (i = 0; i < 32; i++)
  {
   if ((mask >> i) & 1)
    break;
  }
  mask = 32 - i;
  if (mask)
  {
   i = strlen(buf);
   snprintf(buf+i, len-i, "/%u", mask);
  }
 }
 else
  { log_err(2, L_GENERAL, "getifaddr.c", 98, "ioctl(s, SIOCGIFNETMASK, ...): %s\n", strerror((*__errno_location ()))); };
 close(s);
 return 0;
}

variable_types:
ifname
char *

variable_types:
buf
char *

variable_types: len int

variable_types: s int

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

variable_types: ifrlen int

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

variable_types:
mask
unsigned int

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

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


getsysaddr

original_code:
int
getsysaddr(char * buf, int len)
{
 int i;
 int s = socket(2, SOCK_STREAM, 0);
 struct sockaddr_in addr;
 struct ifreq ifr;
 uint32_t mask;
 int ret = -1;
 for (i=1; i > 0; i++)
 {
  ifr.ifr_ifru.ifru_ivalue = i;
  if( ioctl(s, 0x8910, &ifr) < 0 )
   break;
  if(ioctl(s, 0x8915, &ifr, sizeof(struct ifreq)) < 0)
   continue;
  memcpy(&addr, &ifr.ifr_ifru.ifru_addr, sizeof(addr));
  if(strncmp(inet_ntoa(addr.sin_addr), "127.", 4) == 0)
   continue;
  if(ioctl(s, 0x891b, &ifr, sizeof(struct ifreq)) < 0)
   continue;
  if(!inet_ntop(2, &addr.sin_addr, buf, len))
  {
   { log_err(2, L_GENERAL, "getifaddr.c", 127, "inet_ntop(): %s\n", strerror((*__errno_location ()))); };
   close(s);
   break;
  }
  ret = 0;
  memcpy(&addr, &ifr.ifr_ifru.ifru_netmask, sizeof(addr));
  mask = ntohl(addr.sin_addr.s_addr);
  for (i = 0; i < 32; i++)
  {
   if ((mask >> i) & 1)
    break;
  }
  mask = 32 - i;
  if (mask)
  {
   i = strlen(buf);
   snprintf(buf+i, len-i, "/%u", mask);
  }
  break;
 }
 close(s);
 return(ret);
}

canonical_original_code:
int getsysaddr(char *buf, int len)
{
 int i;
 int s = socket(2, SOCK_STREAM, 0);
 struct sockaddr_in addr;
 struct ifreq ifr;
 unsigned int mask;
 int ret = -1;
 for (i=1; i > 0; i++)
 {
  ifr.ifr_ifru.ifru_ivalue = i;
  if( ioctl(s, 0x8910, &ifr) < 0 )
   break;
  if(ioctl(s, 0x8915, &ifr, sizeof(struct ifreq)) < 0)
   continue;
  memcpy(&addr, &ifr.ifr_ifru.ifru_addr, sizeof(addr));
  if(strncmp(inet_ntoa(addr.sin_addr), "127.", 4) == 0)
   continue;
  if(ioctl(s, 0x891b, &ifr, sizeof(struct ifreq)) < 0)
   continue;
  if(!inet_ntop(2, &addr.sin_addr, buf, len))
  {
   { log_err(2, L_GENERAL, "getifaddr.c", 127, "inet_ntop(): %s\n", strerror((*__errno_location ()))); };
   close(s);
   break;
  }
  ret = 0;
  memcpy(&addr, &ifr.ifr_ifru.ifru_netmask, sizeof(addr));
  mask = ntohl(addr.sin_addr.s_addr);
  for (i = 0; i < 32; i++)
  {
   if ((mask >> i) & 1)
    break;
  }
  mask = 32 - i;
  if (mask)
  {
   i = strlen(buf);
   snprintf(buf+i, len-i, "/%u", mask);
  }
  break;
 }
 close(s);
 return(ret);
}

variable_types: i int

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

variable_types: ret int

return_type: int

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

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


getsyshwaddr

decompiled_code:
__int64 __fastcall getsyshwaddr(char *s, int a2)
{
int v2; // eax
int v3; // ebp
struct if_nameindex *v4; // rax
struct if_nameindex *v5; // r12
struct if_nameindex *v6; // rbx
__int64 result; // rax
char v8[16]; // [rsp+0h] [rbp-58h] BYREF
__int64 v9; // [rsp+10h] [rbp-48h]
int v10; // [rsp+2Ah] [rbp-2Eh]
__int16 v11; // [rsp+2Eh] [rbp-2Ah]

v10 = 0;
v11 = 0;
v2 = socket(2, 2, 0);
if ( v2 < 0 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = if_nameindex();
v5 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
if ( v4->if_index )
{
v6 = v4;
while ( 1 )
{
strncpy(v8, v6->if_name, 0x10uLL);
if ( ioctl(v3, 0x8913uLL, v8) >= 0
&& (v9 & 8) == 0
&& ioctl(v3, 0x8927uLL, v8) >= 0
&& (v9 & 0xFFFFFFFFFFFF0000LL) != 0 )
{
break;
}
++v6;
if ( !v6->if_index )
goto LABEL_17;
}
if_freenameindex(v5);
close(v3);
if ( a2 > 12 )
{
v10 = *(_DWORD *)((char *)&v9 + 2);
v11 = HIWORD(v9);
sprintf(
s,
"%02x%02x%02x%02x%02x%02x",
BYTE2(v9),
BYTE3(v9),
BYTE4(v9),
HIBYTE(*(_DWORD *)((char *)&v9 + 2)),
BYTE6(v9),
HIBYTE(v9));
result = 0LL;
}
else
{
result = 0LL;
if ( a2 == 6 )
{
*(_DWORD *)s = *(_DWORD *)((char *)&v9 + 2);
*((_WORD *)s + 2) = HIWORD(v9);
result = 0LL;
}
}
}
else
{
LABEL_17:
if_freenameindex(v5);
close(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s, int a2)
{
int v2; // eax
int v3; // ebp
struct if_nameindex *v4; // rax
struct if_nameindex *v5; // r12
struct if_nameindex *v6; // rbx
__int64 result; // rax
char v8[16]; // [rsp+0h] [rbp-58h] BYREF
__int64 v9; // [rsp+10h] [rbp-48h]
int v10; // [rsp+2Ah] [rbp-2Eh]
__int16 v11; // [rsp+2Eh] [rbp-2Ah]

v10 = 0;
v11 = 0;
v2 = socket(2, 2, 0);
if ( v2 < 0 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = if_nameindex();
v5 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
if ( v4->if_index )
{
v6 = v4;
while ( 1 )
{
strncpy(v8, v6->if_name, 0x10uLL);
if ( ioctl(v3, 0x8913uLL, v8) >= 0
&& (v9 & 8) == 0
&& ioctl(v3, 0x8927uLL, v8) >= 0
&& (v9 & 0xFFFFFFFFFFFF0000LL) != 0 )
{
break;
}
++v6;
if ( !v6->if_index )
goto LABEL_17;
}
if_freenameindex(v5);
close(v3);
if ( a2 > 12 )
{
v10 = *(_DWORD *)((char *)&v9 + 2);
v11 = HIWORD(v9);
sprintf(
s,
"%02x%02x%02x%02x%02x%02x",
BYTE2(v9),
BYTE3(v9),
BYTE4(v9),
HIBYTE(*(_DWORD *)((char *)&v9 + 2)),
BYTE6(v9),
HIBYTE(v9));
result = 0LL;
}
else
{
result = 0LL;
if ( a2 == 6 )
{
*(_DWORD *)s = *(_DWORD *)((char *)&v9 + 2);
*((_WORD *)s + 2) = HIWORD(v9);
result = 0LL;
}
}
}
else
{
LABEL_17:
if_freenameindex(v5);
close(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
getsyshwaddr(char * buf, int len)
{
 struct if_nameindex *ifaces, *if_idx;
 unsigned char mac[6];
 struct ifreq ifr;
 int fd;
 int ret = -1;
 memset(&mac, '\0', sizeof(mac));
 fd = socket(2, SOCK_DGRAM, 0);
 if( fd < 0 )
  return(ret);
 ifaces = if_nameindex();
 if(!ifaces)
  return(ret);
 for(if_idx = ifaces; if_idx->if_index; if_idx++)
 {
  strncpy(ifr.ifr_ifrn.ifrn_name, if_idx->if_name, 16);
  if(ioctl(fd, 0x8913, &ifr) < 0)
   continue;
  if(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK)
   continue;
  if( ioctl(fd, 0x8927, &ifr) < 0 )
   continue;
  if( ((ifr.ifr_ifru.ifru_hwaddr.sa_data[0] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[1] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[2] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[3] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[4] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[5] == 0x00)) )
   continue;
  ret = 0;
  break;
 }
 if_freenameindex(ifaces);
 close(fd);
 if(ret == 0)
 {
  if(len > 12)
  {
   memmove(mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
   sprintf(buf, "%02x%02x%02x%02x%02x%02x",
           mac[0]&0xFF, mac[1]&0xFF, mac[2]&0xFF,
           mac[3]&0xFF, mac[4]&0xFF, mac[5]&0xFF);
  }
  else if(len == 6)
  {
   memmove(buf, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
  }
 }
 return ret;
}

canonical_original_code:
int getsyshwaddr(char *buf, int len)
{
 struct if_nameindex *ifaces; struct if_nameindex *if_idx;
 unsigned char mac[6];
 struct ifreq ifr;
 int fd;
 int ret = -1;
 memset(&mac, '\0', sizeof(mac));
 fd = socket(2, SOCK_DGRAM, 0);
 if( fd < 0 )
  return(ret);
 ifaces = if_nameindex();
 if(!ifaces)
  return(ret);
 for(if_idx = ifaces; if_idx->if_index; if_idx++)
 {
  strncpy(ifr.ifr_ifrn.ifrn_name, if_idx->if_name, 16);
  if(ioctl(fd, 0x8913, &ifr) < 0)
   continue;
  if(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK)
   continue;
  if( ioctl(fd, 0x8927, &ifr) < 0 )
   continue;
  if( ((ifr.ifr_ifru.ifru_hwaddr.sa_data[0] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[1] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[2] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[3] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[4] == 0x00) && (ifr.ifr_ifru.ifru_hwaddr.sa_data[5] == 0x00)) )
   continue;
  ret = 0;
  break;
 }
 if_freenameindex(ifaces);
 close(fd);
 if(ret == 0)
 {
  if(len > 12)
  {
   memmove(mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
   sprintf(buf, "%02x%02x%02x%02x%02x%02x",
           mac[0]&0xFF, mac[1]&0xFF, mac[2]&0xFF,
           mac[3]&0xFF, mac[4]&0xFF, mac[5]&0xFF);
  }
  else if(len == 6)
  {
   memmove(buf, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
  }
 }
 return ret;
}

variable_types: struct if_nameindex { unsigned int if_index; char *if_name; } *

variable_types:
mac
unsigned char [6]

variable_types: fd int


decompiled_code:
char *__fastcall strcat_str(void *ptr, int *a2, int *a3, const char *a4)
{
int v7; // ebx
int v8; // esi
int v9; // esi
char *v10; // r12
int v12; // esi

v7 = strlen(a4);
v8 = *a3;
if ( *a3 > *a2 + v7 )
{
v10 = (char *)ptr;
}
else if ( v7 > 255 )
{
v12 = v7 + 1 + v8;
*a3 = v12;
v10 = (char *)realloc(ptr, v12);
if ( !v10 )
{
*a3 -= v7 + 1;
return (char *)ptr;
}
}
else
{
v9 = v8 + 256;
*a3 = v9;
v10 = (char *)realloc(ptr, v9);
if ( !v10 )
{
*a3 -= 256;
return (char *)ptr;
}
}
memcpy(&v10[*a2], a4, v7 + 1);
*a2 += v7;
return v10;
}

canonical_decompiled_code:
char *__fastcall func0(void *ptr, int *a2, int *a3, const char *a4)
{
int v7; // ebx
int v8; // esi
int v9; // esi
char *v10; // r12
int v12; // esi

v7 = strlen(a4);
v8 = *a3;
if ( *a3 > *a2 + v7 )
{
v10 = (char *)ptr;
}
else if ( v7 > 255 )
{
v12 = v7 + 1 + v8;
*a3 = v12;
v10 = (char *)realloc(ptr, v12);
if ( !v10 )
{
*a3 -= v7 + 1;
return (char *)ptr;
}
}
else
{
v9 = v8 + 256;
*a3 = v9;
v10 = (char *)realloc(ptr, v9);
if ( !v10 )
{
*a3 -= 256;
return (char *)ptr;
}
}
memcpy(&v10[*a2], a4, v7 + 1);
*a2 += v7;
return v10;
}

original_code:
static char *
strcat_str(char * str, int * len, int * tmplen, const char * s2)
{
 char *p;
 int s2len;
 s2len = (int)strlen(s2);
 if(*tmplen <= (*len + s2len))
 {
  if(s2len < 256)
   *tmplen += 256;
  else
   *tmplen += s2len + 1;
  p = realloc(str, *tmplen);
  if (!p)
  {
   if(s2len < 256)
    *tmplen -= 256;
   else
    *tmplen -= s2len + 1;
   return str;
  }
  else
   str = p;
 }
 memcpy(str + *len, s2, s2len + 1);
 *len += s2len;
 return str;
}

canonical_original_code:
static char *strcat_str(char *str, int *len, int *tmplen, char *s2)
{
 char *p;
 int s2len;
 s2len = (int)strlen(s2);
 if(*tmplen <= (*len + s2len))
 {
  if(s2len < 256)
   *tmplen += 256;
  else
   *tmplen += s2len + 1;
  p = realloc(str, *tmplen);
  if (!p)
  {
   if(s2len < 256)
    *tmplen -= 256;
   else
    *tmplen -= s2len + 1;
   return str;
  }
  else
   str = p;
 }
 memcpy(str + *len, s2, s2len + 1);
 *len += s2len;
 return str;
}

variable_types:
str
char *

variable_types:
len
int *

variable_types:
tmplen
int *

variable_types:
p
char *

return_type: char *


genEventVars

decompiled_code:
void *__fastcall genEventVars(int *a1, __int64 a2, const char *a3)
{
void *v4; // rbp
const char **v5; // rbx
char *v6; // rbp
const char *i; // rcx
char *v9; // rbp
unsigned __int8 v10; // al
char v11[524]; // [rsp+0h] [rbp-238h] BYREF
int v12[11]; // [rsp+20Ch] [rbp-2Ch] BYREF

v12[0] = 512;
v4 = malloc(0x200uLL);
if ( v4 )
{
*a1 = 0;
v5 = *(const char ***)(a2 + 8);
snprintf(v11, 0x200uLL, "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\" xmlns:s=\"%s\">", a3);
v6 = strcat_str(v4, a1, v12, v11);
for ( i = *v5; *v5; i = *v5 )
{
if ( *((char *)v5 + 8) < 0 )
{
snprintf(v11, 0x200uLL, "<e:property><%s>", i);
v9 = strcat_str(v6, a1, v12, v11);
v10 = *((_BYTE *)v5 + 11);
if ( v10 )
{
if ( v10 == 0xFF )
{
if ( !strcmp(*v5, "SystemUpdateID") )
{
snprintf(v11, 0x200uLL, "%d", updateID);
v9 = strcat_str(v9, a1, v12, v11);
}
}
else
{
v9 = strcat_str(v9, a1, v12, (const char *)upnpallowedvalues[v10]);
}
}
snprintf(v11, 0x200uLL, "</%s></e:property>", *v5);
v6 = strcat_str(v9, a1, v12, v11);
}
v5 += 2;
}
v4 = strcat_str(v6, a1, v12, "</e:propertyset>");
*((_BYTE *)v4 + *a1) = 0;
}
return v4;
}

canonical_decompiled_code:
void *__fastcall func1(int *a1, __int64 a2, const char *a3)
{
void *v4; // rbp
const char **v5; // rbx
char *v6; // rbp
const char *i; // rcx
char *v9; // rbp
unsigned __int8 v10; // al
char v11[524]; // [rsp+0h] [rbp-238h] BYREF
int v12[11]; // [rsp+20Ch] [rbp-2Ch] BYREF

v12[0] = 512;
v4 = malloc(0x200uLL);
if ( v4 )
{
*a1 = 0;
v5 = *(const char ***)(a2 + 8);
snprintf(v11, 0x200uLL, "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\" xmlns:s=\"%s\">", a3);
v6 = func0(v4, a1, v12, v11);
for ( i = *v5; *v5; i = *v5 )
{
if ( *((char *)v5 + 8) < 0 )
{
snprintf(v11, 0x200uLL, "<e:property><%s>", i);
v9 = func0(v6, a1, v12, v11);
v10 = *((_BYTE *)v5 + 11);
if ( v10 )
{
if ( v10 == 0xFF )
{
if ( !strcmp(*v5, "SystemUpdateID") )
{
snprintf(v11, 0x200uLL, "%d", updateID);
v9 = func0(v9, a1, v12, v11);
}
}
else
{
v9 = func0(v9, a1, v12, (const char *)upnpallowedvalues[v10]);
}
}
snprintf(v11, 0x200uLL, "</%s></e:property>", *v5);
v6 = func0(v9, a1, v12, v11);
}
v5 += 2;
}
v4 = func0(v6, a1, v12, "</e:propertyset>");
*((_BYTE *)v4 + *a1) = 0;
}
return v4;
}

original_code:
static char *
genEventVars(int * len, const struct serviceDesc * s, const char * servns)
{
 const struct stateVar * v;
 char * str;
 int tmplen;
 char buf[512];
 tmplen = 512;
 str = (char *)malloc(tmplen);
 if(str == ((void *)0))
  return ((void *)0);
 *len = 0;
 v = s->serviceStateTable;
 snprintf(buf, sizeof(buf), "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\" xmlns:s=\"%s\">", servns);
 str = strcat_str(str, len, &tmplen, buf);
 while(v->name) {
  if(v->itype & 0x80) {
   snprintf(buf, sizeof(buf), "<e:property><%s>", v->name);
   str = strcat_str(str, len, &tmplen, buf);
   switch(v->ieventvalue) {
   case 0:
    break;
   case 255:
    if( strcmp(v->name, "SystemUpdateID") == 0 )
    {
     snprintf(buf, sizeof(buf), "%d", updateID);
     str = strcat_str(str, len, &tmplen, buf);
    }
    break;
   default:
    str = strcat_str(str, len, &tmplen, upnpallowedvalues[v->ieventvalue]);
   }
   snprintf(buf, sizeof(buf), "</%s></e:property>", v->name);
   str = strcat_str(str, len, &tmplen, buf);
  }
  v++;
 }
 str = strcat_str(str, len, &tmplen, "</e:propertyset>");
 str[*len] = '\0';
 return str;
}

canonical_original_code:
static char *genEventVars(int *len, struct serviceDesc *s, char *servns)
{
 struct stateVar *v;
 char *str;
 int tmplen;
 char buf[512];
 tmplen = 512;
 str = (char *)malloc(tmplen);
 if(str == ((void *)0))
  return ((void *)0);
 *len = 0;
 v = s->serviceStateTable;
 snprintf(buf, sizeof(buf), "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\" xmlns:s=\"%s\">", servns);
 str = strcat_str(str, len, &tmplen, buf);
 while(v->name) {
  if(v->itype & 0x80) {
   snprintf(buf, sizeof(buf), "<e:property><%s>", v->name);
   str = strcat_str(str, len, &tmplen, buf);
   switch(v->ieventvalue) {
   case 0:
    break;
   case 255:
    if( strcmp(v->name, "SystemUpdateID") == 0 )
    {
     snprintf(buf, sizeof(buf), "%d", updateID);
     str = strcat_str(str, len, &tmplen, buf);
    }
    break;
   default:
    str = strcat_str(str, len, &tmplen, upnpallowedvalues[v->ieventvalue]);
   }
   snprintf(buf, sizeof(buf), "</%s></e:property>", v->name);
   str = strcat_str(str, len, &tmplen, buf);
  }
  v++;
 }
 str = strcat_str(str, len, &tmplen, "</e:propertyset>");
 str[*len] = '\0';
 return str;
}

variable_types: struct serviceDesc { struct action { char *name; struct argument { char *name; unsigned char dir; unsigned char relatedVar; } *args; } *actionList; struct stateVar { char *name; unsigned char itype; unsigned char idefault; unsigned char iallowedlist; unsigned char ieventvalue; } *serviceStateTable; } *

variable_types:
servns
char *

variable_types: tmplen int

variable_types:
buf
char [512]

user_defined_types: struct serviceDesc { struct action *actionList; struct stateVar *serviceStateTable; }

user_defined_types: struct action { char *name; struct argument *args; }


getVarsContentDirectory

decompiled_code:
void *__fastcall getVarsContentDirectory(int *a1)
{
return genEventVars(a1, (__int64)scpdContentDirectory, "urn:schemas-upnp-org:service:ContentDirectory:1");
}

canonical_decompiled_code:
void *__fastcall func2(int *a1)
{
return func1(a1, (__int64)scpdContentDirectory, "urn:schemas-upnp-org:service:ContentDirectory:1");
}

original_code:
char *
getVarsContentDirectory(int * l)
{
 return genEventVars(l,
                        &scpdContentDirectory,
                     "urn:schemas-upnp-org:service:ContentDirectory:1");
}

canonical_original_code:
char *getVarsContentDirectory(int *l)
{
 return genEventVars(l,
                        &scpdContentDirectory,
                     "urn:schemas-upnp-org:service:ContentDirectory:1");
}

variable_types:
l
int *


getVarsConnectionManager

decompiled_code:
void *__fastcall getVarsConnectionManager(int *a1)
{
return genEventVars(a1, (__int64)scpdConnectionManager, "urn:schemas-upnp-org:service:ConnectionManager:1");
}

canonical_decompiled_code:
void *__fastcall func3(int *a1)
{
return func1(a1, (__int64)scpdConnectionManager, "urn:schemas-upnp-org:service:ConnectionManager:1");
}

original_code:
char *
getVarsConnectionManager(int * l)
{
 return genEventVars(l,
                        &scpdConnectionManager,
                     "urn:schemas-upnp-org:service:ConnectionManager:1");
}

canonical_original_code:
char *getVarsConnectionManager(int *l)
{
 return genEventVars(l,
                        &scpdConnectionManager,
                     "urn:schemas-upnp-org:service:ConnectionManager:1");
}


decompiled_code:
void *__fastcall getVarsX_MS_MediaReceiverRegistrar(int *a1)
{
return genEventVars(
a1,
(__int64)scpdX_MS_MediaReceiverRegistrar,
"urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1");
}

canonical_decompiled_code:
void *__fastcall func4(int *a1)
{
return func1(
a1,
(__int64)scpdX_MS_MediaReceiverRegistrar,
"urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1");
}

original_code:
char *
getVarsX_MS_MediaReceiverRegistrar(int * l)
{
 return genEventVars(l,
                        &scpdX_MS_MediaReceiverRegistrar,
                     "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1");
}

canonical_original_code:
char *getVarsX_MS_MediaReceiverRegistrar(int *l)
{
 return genEventVars(l,
                        &scpdX_MS_MediaReceiverRegistrar,
                     "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1");
}


NameValueParserGetData

original_code:
static void
NameValueParserGetData(void * d, const char * datas, int l)
{
    struct NameValueParserData * data = (struct NameValueParserData *)d;
    struct NameValue * nv;
    if(l>1975)
        l = 1975;
    nv = malloc(sizeof(struct NameValue)+l+1);
    strncpy(nv->name, data->curelt, 64);
    nv->name[63] = '\0';
    memcpy(nv->value, datas, l);
    nv->value[l] = '\0';
    do { if (((nv)->entries.le_next = (&(data->head))->lh_first) != ((void *)0)) (&(data->head))->lh_first->entries.le_prev = &(nv)->entries.le_next; (&(data->head))->lh_first = (nv); (nv)->entries.le_prev = &(&(data->head))->lh_first; } while ( 0);
}

canonical_original_code:
static void NameValueParserGetData(void *d, char *datas, int l)
{
    struct NameValueParserData *data = (struct NameValueParserData *)d;
    struct NameValue *nv;
    if(l>1975)
        l = 1975;
    nv = malloc(sizeof(struct NameValue)+l+1);
    strncpy(nv->name, data->curelt, 64);
    nv->name[63] = '\0';
    memcpy(nv->value, datas, l);
    nv->value[l] = '\0';
    do { if (((nv)->entries.le_next = (&(data->head))->lh_first) != ((void *)0)) (&(data->head))->lh_first->entries.le_prev = &(nv)->entries.le_next; (&(data->head))->lh_first = (nv); (nv)->entries.le_prev = &(&(data->head))->lh_first; } while ( 0);
}

variable_types:
d
void *

variable_types:
datas
char *

variable_types: l int

variable_types: struct NameValueParserData { struct listhead { struct NameValue { struct { struct NameValue *le_next; struct NameValue **le_prev; } entries; char name[64]; char value[]; } *lh_first; } head; char curelt[64]; } *

return_type: void

user_defined_types: struct NameValueParserData { struct listhead head; char curelt[64]; }

user_defined_types: struct listhead { struct NameValue *lh_first; }


NameValueParserStartElt

original_code:
static void
NameValueParserStartElt(void * d, const char * name, int l)
{
    struct NameValueParserData * data = (struct NameValueParserData *)d;
    if(l>63)
        l = 63;
    memcpy(data->curelt, name, l);
    data->curelt[l] = '\0';
    if(!data->head.lh_first)
    {
        struct NameValue * nv;
        nv = malloc(sizeof(struct NameValue)+l+1);
        strcpy(nv->name, "rootElement");
        memcpy(nv->value, name, l);
        nv->value[l] = '\0';
        do { if (((nv)->entries.le_next = (&(data->head))->lh_first) != ((void *)0)) (&(data->head))->lh_first->entries.le_prev = &(nv)->entries.le_next; (&(data->head))->lh_first = (nv); (nv)->entries.le_prev = &(&(data->head))->lh_first; } while ( 0);
    }
}

canonical_original_code:
static void NameValueParserStartElt(void *d, char *name, int l)
{
    struct NameValueParserData *data = (struct NameValueParserData *)d;
    if(l>63)
        l = 63;
    memcpy(data->curelt, name, l);
    data->curelt[l] = '\0';
    if(!data->head.lh_first)
    {
        struct NameValue *nv;
        nv = malloc(sizeof(struct NameValue)+l+1);
        strcpy(nv->name, "rootElement");
        memcpy(nv->value, name, l);
        nv->value[l] = '\0';
        do { if (((nv)->entries.le_next = (&(data->head))->lh_first) != ((void *)0)) (&(data->head))->lh_first->entries.le_prev = &(nv)->entries.le_next; (&(data->head))->lh_first = (nv); (nv)->entries.le_prev = &(&(data->head))->lh_first; } while ( 0);
    }
}

variable_types:
name
char *

variable_types: struct NameValue { struct { struct NameValue *le_next; struct NameValue **le_prev; } entries; char name[64]; char value[]; } *


ParseNameValue

decompiled_code:
__int64 __fastcall ParseNameValue(__int64 a1, int a2, _QWORD *a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-58h] BYREF
int v5; // [rsp+18h] [rbp-40h]
_QWORD *v6; // [rsp+20h] [rbp-38h]
char *(__fastcall *v7)(_QWORD *, char *, int); // [rsp+28h] [rbp-30h]
__int64 v8; // [rsp+30h] [rbp-28h]
__int64 (__fastcall *v9)(char *, _DWORD *, int); // [rsp+38h] [rbp-20h]
__int64 v10; // [rsp+40h] [rbp-18h]

*a3 = 0LL;
v4[0] = a1;
v5 = a2;
v6 = a3;
v7 = NameValueParserStartElt;
v8 = 0LL;
v9 = NameValueParserGetData;
v10 = 0LL;
return parsexml(v4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, _QWORD *a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-58h] BYREF
int v5; // [rsp+18h] [rbp-40h]
_QWORD *v6; // [rsp+20h] [rbp-38h]
char *(__fastcall *v7)(_QWORD *, char *, int); // [rsp+28h] [rbp-30h]
__int64 v8; // [rsp+30h] [rbp-28h]
__int64 (__fastcall *v9)(char *, _DWORD *, int); // [rsp+38h] [rbp-20h]
__int64 v10; // [rsp+40h] [rbp-18h]

*a3 = 0LL;
v4[0] = a1;
v5 = a2;
v6 = a3;
v7 = NameValueParserStartElt;
v8 = 0LL;
v9 = NameValueParserGetData;
v10 = 0LL;
return parsexml(v4);
}

original_code:
void
ParseNameValue(const char * buffer, int bufsize,
                    struct NameValueParserData * data)
{
    struct xmlparser parser;
    do { (&(data->head))->lh_first = ((void *)0); } while ( 0);
    parser.xmlstart = buffer;
    parser.xmlsize = bufsize;
    parser.data = data;
    parser.starteltfunc = NameValueParserStartElt;
    parser.endeltfunc = 0;
    parser.datafunc = NameValueParserGetData;
    parser.attfunc = 0;
    parsexml(&parser);
}

canonical_original_code:
void ParseNameValue(char *buffer, int bufsize, struct NameValueParserData *data)
{
    struct xmlparser parser;
    do { (&(data->head))->lh_first = ((void *)0); } while ( 0);
    parser.xmlstart = buffer;
    parser.xmlsize = bufsize;
    parser.data = data;
    parser.starteltfunc = NameValueParserStartElt;
    parser.endeltfunc = 0;
    parser.datafunc = NameValueParserGetData;
    parser.attfunc = 0;
    parsexml(&parser);
}

variable_types:
buffer
char *

variable_types: bufsize int

variable_types: struct xmlparser { char *xmlstart; char *xmlend; char *xml; int xmlsize; void *data; void (*starteltfunc)(void *, char *, int); void (*endeltfunc)(void *, char *, int); void (*datafunc)(void *, char *, int); void (*attfunc)(void *, char *, int, char *, int); }

user_defined_types: struct NameValue { struct { struct NameValue *le_next; struct NameValue **le_prev; } entries; char name[64]; char value[]; }


decompiled_code:
int close_server()
{
return close(*(int *)&rc.sa_family);
}

canonical_decompiled_code:
int func1()
{
return close(*(int *)&rc.sa_family);
}

original_code:
void close_server(void)
{
 close(rc.server_socket);
}

canonical_original_code:
void close_server()
{
 close(rc.server_socket);
}


decompiled_code:
__int64 build_server()
{
int v0; // edx
__int64 result; // rax

*(_QWORD *)&rc.sa_data[10] = 0LL;
*(_QWORD *)((char *)&rc + 20) = 0LL;
*(_WORD *)&rc.sa_data[12] = _bswap_16(runtime_vars);
*(_WORD *)&rc.sa_data[10] = 2;
*(_DWORD *)&rc.sa_family = socket(2, 1, 0);
if ( bind(*(int *)&rc.sa_family, (const struct sockaddr *)(unsigned int)((_DWORD)&rc + 12), 0x10u) < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 94LL, "Failed to bind to socket for tcp tunnel. EXITING.\n");
result = 1LL;
}
else
{
v0 = listen(*(int *)&rc.sa_family, 1);
result = 0LL;
if ( v0 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 100LL, "Failed to listen on socket for tcp tunnel. EXITING.\n");
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // edx
__int64 result; // rax

*(_QWORD *)&rc.sa_data[10] = 0LL;
*(_QWORD *)((char *)&rc + 20) = 0LL;
*(_WORD *)&rc.sa_data[12] = func0(runtime_vars);
*(_WORD *)&rc.sa_data[10] = 2;
*(_DWORD *)&rc.sa_family = socket(2, 1, 0);
if ( bind(*(int *)&rc.sa_family, (const struct sockaddr *)(unsigned int)((_DWORD)&rc + 12), 0x10u) < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 94LL, "Failed to bind to socket for tcp tunnel. EXITING.\n");
result = 1LL;
}
else
{
v0 = listen(*(int *)&rc.sa_family, 1);
result = 0LL;
if ( v0 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 100LL, "Failed to listen on socket for tcp tunnel. EXITING.\n");
result = 1LL;
}
}
return result;
}

original_code:
int build_server(void)
{
 memset(&rc.server_addr, 0, sizeof(rc.server_addr));
 rc.server_addr.sin_port = htons(runtime_vars.port);
 rc.server_addr.sin_family = 2;
 rc.server_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 rc.server_socket = socket(2, SOCK_STREAM, 0);
 if (rc.server_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 86, "Failed to open socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (bind(rc.server_socket, (struct sockaddr *) &rc.server_addr, sizeof(rc.server_addr)) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 94, "Failed to bind to socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (listen(rc.server_socket, 1) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 100, "Failed to listen on socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}

canonical_original_code:
int build_server()
{
 memset(&rc.server_addr, 0, sizeof(rc.server_addr));
 rc.server_addr.sin_port = htons(runtime_vars.port);
 rc.server_addr.sin_family = 2;
 rc.server_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
 rc.server_socket = socket(2, SOCK_STREAM, 0);
 if (rc.server_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 86, "Failed to open socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (bind(rc.server_socket, (struct sockaddr *) &rc.server_addr, sizeof(rc.server_addr)) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 94, "Failed to bind to socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (listen(rc.server_socket, 1) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 100, "Failed to listen on socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 wait_for_clients()
{
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

addr_len[0] = 16;
*(_DWORD *)&rc.sa_data[2] = accept(
*(int *)&rc.sa_family,
(struct sockaddr *)(unsigned int)((_DWORD)&rc + 28),
addr_len);
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

addr_len[0] = 16;
*(_DWORD *)&rc.sa_data[2] = accept(
*(int *)&rc.sa_family,
(struct sockaddr *)(unsigned int)((_DWORD)&rc + 28),
addr_len);
return 0LL;
}

original_code:
int wait_for_clients(void)
{
 int client_addr_size = sizeof(struct sockaddr_in);
 rc.client_socket = accept(rc.server_socket, (struct sockaddr *) &rc.client_addr, (socklen_t *) &client_addr_size);
 if (rc.client_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 114, "Failed to wait for connections on socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}

canonical_original_code:
int wait_for_clients()
{
 int client_addr_size = sizeof(struct sockaddr_in);
 rc.client_socket = accept(rc.server_socket, (struct sockaddr *) &rc.client_addr, (unsigned int *) &client_addr_size);
 if (rc.client_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 114, "Failed to wait for connections on socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 build_tunnel()
{
struct hostent *v0; // rax
struct hostent *v1; // rbx
int v2; // eax
int v3; // edx
__int64 result; // rax

v0 = gethostbyname(*((const char **)&runtime_vars + 1));
*((_QWORD *)&rc + 8) = v0;
if ( v0 )
{
v1 = v0;
*(_QWORD *)((char *)&rc + 44) = 0LL;
*(_QWORD *)((char *)&rc + 52) = 0LL;
*((_WORD *)&rc + 22) = 2;
*((_WORD *)&rc + 23) = _bswap_16(*((_WORD *)&runtime_vars + 2));
memcpy((void *)(unsigned int)((_DWORD)&rc + 48), *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
*(_DWORD *)&rc.sa_data[6] = v2;
v3 = connect(v2, (const struct sockaddr *)(unsigned int)((_DWORD)&rc + 44), 0x10u);
result = 0LL;
if ( v3 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 148LL, "Failed to connect to remote host for tcp tunnel. EXITING.\n");
result = 1LL;
}
}
else
{
log_err(1LL, 0LL, "tcptunnel.c", 128LL, "Failed to resolve hostname for tcp tunnel. EXITING.\n");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
struct hostent *v0; // rax
struct hostent *v1; // rbx
int v2; // eax
int v3; // edx
__int64 result; // rax

v0 = gethostbyname(*((const char **)&runtime_vars + 1));
*((_QWORD *)&rc + 8) = v0;
if ( v0 )
{
v1 = v0;
*(_QWORD *)((char *)&rc + 44) = 0LL;
*(_QWORD *)((char *)&rc + 52) = 0LL;
*((_WORD *)&rc + 22) = 2;
*((_WORD *)&rc + 23) = func0(*((_WORD *)&runtime_vars + 2));
memcpy((void *)(unsigned int)((_DWORD)&rc + 48), *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
*(_DWORD *)&rc.sa_data[6] = v2;
v3 = connect(v2, (const struct sockaddr *)(unsigned int)((_DWORD)&rc + 44), 0x10u);
result = 0LL;
if ( v3 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 148LL, "Failed to connect to remote host for tcp tunnel. EXITING.\n");
result = 1LL;
}
}
else
{
log_err(1LL, 0LL, "tcptunnel.c", 128LL, "Failed to resolve hostname for tcp tunnel. EXITING.\n");
result = 1LL;
}
return result;
}

original_code:
int build_tunnel(void)
{
 rc.remote_host = gethostbyname(runtime_vars.rhost);
 if (rc.remote_host == ((void *)0))
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 128, "Failed to resolve hostname for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 memset(&rc.remote_addr, 0, sizeof(rc.remote_addr));
 rc.remote_addr.sin_family = 2;
 rc.remote_addr.sin_port = htons(runtime_vars.rport);
 memcpy(&rc.remote_addr.sin_addr.s_addr, rc.remote_host->h_addr_list[0], rc.remote_host->h_length);
 rc.remote_socket = socket(2, SOCK_STREAM, 0);
 if (rc.remote_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 142, "Failed to open socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (connect(rc.remote_socket, (struct sockaddr *) &rc.remote_addr, sizeof(rc.remote_addr)) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 148, "Failed to connect to remote host for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}

canonical_original_code:
int build_tunnel()
{
 rc.remote_host = gethostbyname(runtime_vars.rhost);
 if (rc.remote_host == ((void *)0))
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 128, "Failed to resolve hostname for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 memset(&rc.remote_addr, 0, sizeof(rc.remote_addr));
 rc.remote_addr.sin_family = 2;
 rc.remote_addr.sin_port = htons(runtime_vars.rport);
 memcpy(&rc.remote_addr.sin_addr.s_addr, rc.remote_host->h_addr_list[0], rc.remote_host->h_length);
 rc.remote_socket = socket(2, SOCK_STREAM, 0);
 if (rc.remote_socket < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 142, "Failed to open socket for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 if (connect(rc.remote_socket, (struct sockaddr *) &rc.remote_addr, sizeof(rc.remote_addr)) < 0)
 {
  { log_err(1, L_GENERAL, "tcptunnel.c", 148, "Failed to connect to remote host for tcp tunnel. EXITING.\n"); };
  return 1;
 }
 return 0;
}


fd

decompiled_code:
__int64 fd()
{
unsigned int v0; // eax

v0 = *(_DWORD *)&rc.sa_data[2];
if ( *(_DWORD *)&rc.sa_data[6] >= v0 )
v0 = *(_DWORD *)&rc.sa_data[6];
return v0 + 1;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // eax

v0 = *(_DWORD *)&rc.sa_data[2];
if ( *(_DWORD *)&rc.sa_data[6] >= v0 )
v0 = *(_DWORD *)&rc.sa_data[6];
return v0 + 1;
}

original_code:
int fd(void)
{
 unsigned int fd = rc.client_socket;
 if (fd < rc.remote_socket)
 {
  fd = rc.remote_socket;
 }
 return fd + 1;
}

canonical_original_code:
int fd()
{
 unsigned int fd = rc.client_socket;
 if (fd < rc.remote_socket)
 {
  fd = rc.remote_socket;
 }
 return fd + 1;
}

variable_types:
fd
unsigned int


decompiled_code:
__int64 use_tunnel()
{
unsigned int v0; // ebp
int v1; // eax
int v2; // eax
int v3; // eax
_BYTE v5[4096]; // [rsp+0h] [rbp-1098h] BYREF
fd_set readfds; // [rsp+1000h] [rbp-98h] BYREF

while ( !quitting )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[*(_DWORD *)&rc.sa_data[2] >> 6] |= 1LL << *(_DWORD *)&rc.sa_data[2];
readfds.fds_bits[*(_DWORD *)&rc.sa_data[6] >> 6] |= 1LL << *(_DWORD *)&rc.sa_data[6];
memset(v5, 0, sizeof(v5));
v1 = fd();
if ( select(v1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 170LL, "Failed to use selected tcp tunnel. EXITING.\n");
return 0;
}
if ( (readfds.fds_bits[*(_DWORD *)&rc.sa_data[2] >> 6] & (1LL << *(_DWORD *)&rc.sa_data[2])) != 0 )
{
v2 = recv(*(_DWORD *)&rc.sa_data[2], v5, 0x1000uLL, 0);
v0 = v2;
if ( v2 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 179LL, "Failed to receive on client socket for tcp tunnel. EXITING.\n");
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return 1;
}
if ( !v2 )
{
LABEL_4:
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return v0;
}
send(*(_DWORD *)&rc.sa_data[6], v5, v2, 0);
}
if ( (readfds.fds_bits[*(_DWORD *)&rc.sa_data[6] >> 6] & (1LL << *(_DWORD *)&rc.sa_data[6])) != 0 )
{
v3 = recv(*(_DWORD *)&rc.sa_data[6], v5, 0x1000uLL, 0);
v0 = v3;
if ( v3 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 204LL, "Failed to receive on remote socket for tcp tunnel. EXITING.\n");
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return 1;
}
if ( !v3 )
goto LABEL_4;
send(*(_DWORD *)&rc.sa_data[2], v5, v3, 0);
}
}
return 0;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int v0; // ebp
int v1; // eax
int v2; // eax
int v3; // eax
_BYTE v5[4096]; // [rsp+0h] [rbp-1098h] BYREF
fd_set readfds; // [rsp+1000h] [rbp-98h] BYREF

while ( !quitting )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[*(_DWORD *)&rc.sa_data[2] >> 6] |= 1LL << *(_DWORD *)&rc.sa_data[2];
readfds.fds_bits[*(_DWORD *)&rc.sa_data[6] >> 6] |= 1LL << *(_DWORD *)&rc.sa_data[6];
memset(v5, 0, sizeof(v5));
v1 = func5();
if ( select(v1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 170LL, "Failed to use selected tcp tunnel. EXITING.\n");
return 0;
}
if ( (readfds.fds_bits[*(_DWORD *)&rc.sa_data[2] >> 6] & (1LL << *(_DWORD *)&rc.sa_data[2])) != 0 )
{
v2 = recv(*(_DWORD *)&rc.sa_data[2], v5, 0x1000uLL, 0);
v0 = v2;
if ( v2 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 179LL, "Failed to receive on client socket for tcp tunnel. EXITING.\n");
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return 1;
}
if ( !v2 )
{
LABEL_4:
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return v0;
}
send(*(_DWORD *)&rc.sa_data[6], v5, v2, 0);
}
if ( (readfds.fds_bits[*(_DWORD *)&rc.sa_data[6] >> 6] & (1LL << *(_DWORD *)&rc.sa_data[6])) != 0 )
{
v3 = recv(*(_DWORD *)&rc.sa_data[6], v5, 0x1000uLL, 0);
v0 = v3;
if ( v3 < 0 )
{
log_err(1LL, 0LL, "tcptunnel.c", 204LL, "Failed to receive on remote socket for tcp tunnel. EXITING.\n");
close(*(_DWORD *)&rc.sa_data[2]);
close(*(_DWORD *)&rc.sa_data[6]);
return 1;
}
if ( !v3 )
goto LABEL_4;
send(*(_DWORD *)&rc.sa_data[2], v5, v3, 0);
}
}
return 0;
}

original_code:
int use_tunnel(void)
{
 fd_set io;
 char buffer[4096];
 while(!quitting)
 {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&io)->fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&io)->fds_bits)[((rc.client_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((rc.client_socket) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&io)->fds_bits)[((rc.remote_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((rc.remote_socket) % (8 * (int) sizeof (__fd_mask)))))));
  memset(buffer, 0, 4096);
  if (select(fd(), &io, ((void *)0), ((void *)0), ((void *)0)) < 0 )
  {
   { log_err(1, L_GENERAL, "tcptunnel.c", 170, "Failed to use selected tcp tunnel. EXITING.\n"); };
   break;
  }
  if (((((&io)->fds_bits)[((rc.client_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((rc.client_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   int count = recv(rc.client_socket, buffer, 4096, 0);
   if (count < 0)
   {
    { log_err(1, L_GENERAL, "tcptunnel.c", 179, "Failed to receive on client socket for tcp tunnel. EXITING.\n"); };
    close(rc.client_socket);
    close(rc.remote_socket);
    return 1;
   }
   if (count == 0)
   {
    close(rc.client_socket);
    close(rc.remote_socket);
    return 0;
   }
   send(rc.remote_socket, buffer, count, 0);
  }
  if (((((&io)->fds_bits)[((rc.remote_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((rc.remote_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   int count = recv(rc.remote_socket, buffer, 4096, 0);
   if (count < 0)
   {
    { log_err(1, L_GENERAL, "tcptunnel.c", 204, "Failed to receive on remote socket for tcp tunnel. EXITING.\n"); };
    close(rc.client_socket);
    close(rc.remote_socket);
    return 1;
   }
   if (count == 0)
   {
    close(rc.client_socket);
    close(rc.remote_socket);
    return 0;
   }
   send(rc.client_socket, buffer, count, 0);
  }
 }
 return 0;
}

canonical_original_code:
int use_tunnel()
{
 struct fd_set io;
 char buffer[4096];
 while(!quitting)
 {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&io)->fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&io)->fds_bits)[((rc.client_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((rc.client_socket) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&io)->fds_bits)[((rc.remote_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((rc.remote_socket) % (8 * (int) sizeof (__fd_mask)))))));
  memset(buffer, 0, 4096);
  if (select(fd(), &io, ((void *)0), ((void *)0), ((void *)0)) < 0 )
  {
   { log_err(1, L_GENERAL, "tcptunnel.c", 170, "Failed to use selected tcp tunnel. EXITING.\n"); };
   break;
  }
  if (((((&io)->fds_bits)[((rc.client_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((rc.client_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   int count = recv(rc.client_socket, buffer, 4096, 0);
   if (count < 0)
   {
    { log_err(1, L_GENERAL, "tcptunnel.c", 179, "Failed to receive on client socket for tcp tunnel. EXITING.\n"); };
    close(rc.client_socket);
    close(rc.remote_socket);
    return 1;
   }
   if (count == 0)
   {
    close(rc.client_socket);
    close(rc.remote_socket);
    return 0;
   }
   send(rc.remote_socket, buffer, count, 0);
  }
  if (((((&io)->fds_bits)[((rc.remote_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((rc.remote_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   int count = recv(rc.remote_socket, buffer, 4096, 0);
   if (count < 0)
   {
    { log_err(1, L_GENERAL, "tcptunnel.c", 204, "Failed to receive on remote socket for tcp tunnel. EXITING.\n"); };
    close(rc.client_socket);
    close(rc.remote_socket);
    return 1;
   }
   if (count == 0)
   {
    close(rc.client_socket);
    close(rc.remote_socket);
    return 0;
   }
   send(rc.client_socket, buffer, count, 0);
  }
 }
 return 0;
}

variable_types:
io
struct fd_set { long fds_bits[-1]; }

variable_types:
buffer
char [4096]

variable_types: count int


decompiled_code:
char *get_current_timestamp()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v0 = localtime(&timer);
strftime(date_str_0, 0xFFFuLL, "%a %d %H:%M:%S", v0);
return date_str_0;
}

canonical_decompiled_code:
char *func7()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
v0 = localtime(&timer);
strftime(date_str_0, 0xFFFuLL, "%a %d %H:%M:%S", v0);
return date_str_0;
}

original_code:
char *get_current_timestamp(void)
{
 static char date_str[4096];
 time_t date;
 time(&date);
 strftime(date_str, 4096 - 1, "%a %d %H:%M:%S", localtime(&date));
 return date_str;
}

canonical_original_code:
char *get_current_timestamp()
{
 static char date_str[4096];
 long date;
 time(&date);
 strftime(date_str, 4096 - 1, "%a %d %H:%M:%S", localtime(&date));
 return date_str;
}

variable_types: date long


original_code:
unsigned long long
monotonic_us(void)
{
 struct timespec ts;
 syscall(228, 1, &ts);
 return ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000;
}

canonical_original_code:
unsigned long long monotonic_us()
{
 struct timespec ts;
 syscall(228, 1, &ts);
 return ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000;
}

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

return_type: unsigned long long


decompiled_code:
void __fastcall read_random_bytes(void *buf, size_t nbytes)
{
int v2; // eax
unsigned int v3; // eax
unsigned int i; // er12
_BYTE *v5; // rbx
int v6; // ebx

v2 = open64("/dev/urandom", 0);
if ( v2 >= 0 )
{
v6 = v2;
if ( read(v2, buf, nbytes) )
close(v6);
}
v3 = monotonic_us();
srand(v3);
for ( i = getpid(); ; i = 0 )
{
if ( nbytes )
{
v5 = buf;
do
*v5++ ^= rand() >> 5;
while ( v5 != (char *)buf + nbytes );
}
if ( !i )
break;
srand(i);
}
}

canonical_decompiled_code:
void __fastcall func1(void *buf, size_t nbytes)
{
int v2; // eax
unsigned int v3; // eax
unsigned int i; // er12
_BYTE *v5; // rbx
int v6; // ebx

v2 = open64("/dev/urandom", 0);
if ( v2 >= 0 )
{
v6 = v2;
if ( read(v2, buf, nbytes) )
close(v6);
}
v3 = func0();
srand(v3);
for ( i = getpid(); ; i = 0 )
{
if ( nbytes )
{
v5 = buf;
do
*v5++ ^= rand() >> 5;
while ( v5 != (char *)buf + nbytes );
}
if ( !i )
break;
srand(i);
}
}

original_code:
static void
read_random_bytes(unsigned char *buf, size_t size)
{
 int i;
 pid_t pid;
 i = open("/dev/urandom", 00);
 if(i >= 0)
 {
  if(read(i, buf, size))
  {
   close(i);
  }
 }
 srand(monotonic_us());
 pid = getpid();
 while(1)
 {
  for(i = 0; i < size; i++)
   buf[i] ^= rand() >> 5;
  if(pid == 0)
   break;
  srand(pid);
  pid = 0;
 }
}

canonical_original_code:
static void read_random_bytes(unsigned char *buf, unsigned long size)
{
 int i;
 int pid;
 i = open("/dev/urandom", 00);
 if(i >= 0)
 {
  if(read(i, buf, size))
  {
   close(i);
  }
 }
 srand(monotonic_us());
 pid = getpid();
 while(1)
 {
  for(i = 0; i < size; i++)
   buf[i] ^= rand() >> 5;
  if(pid == 0)
   break;
  srand(pid);
  pid = 0;
 }
}

variable_types:
buf
unsigned char *

variable_types:
size
unsigned long

variable_types: i int

variable_types: pid int


decompiled_code:
__int64 init_clockseq()
{
__int64 result; // rax
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

read_random_bytes(buf, 4uLL);
result = buf[0] & 0x3FFF;
clock_seq = buf[0] & 0x3FFF;
clock_seq_initialized = 1;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

func1(buf, 4uLL);
result = buf[0] & 0x3FFF;
clock_seq = buf[0] & 0x3FFF;
clock_seq_initialized = 1;
return result;
}

original_code:
void
init_clockseq(void)
{
 unsigned char buf[4];
 read_random_bytes(buf, 4);
 memcpy(&clock_seq, &buf, sizeof(clock_seq));
 clock_seq &= clock_seq_max;
 clock_seq_initialized = 1;
}

canonical_original_code:
void init_clockseq()
{
 unsigned char buf[4];
 read_random_bytes(buf, 4);
 memcpy(&clock_seq, &buf, sizeof(clock_seq));
 clock_seq &= clock_seq_max;
 clock_seq_initialized = 1;
}

variable_types:
buf
unsigned char [4]


decompiled_code:
__int64 __fastcall generate_uuid(__int64 a1)
{
unsigned __int64 v1; // rbx
int v2; // eax
char v3; // ah
int v5; // [rsp+Ah] [rbp-2Eh] BYREF
__int16 v6; // [rsp+Eh] [rbp-2Ah]
__int64 v7; // [rsp+10h] [rbp-28h] BYREF
__int64 v8; // [rsp+18h] [rbp-20h]

v5 = 0;
v6 = 0;
if ( (unsigned int)getsyshwaddr(&v5, 6LL) )
{
log_err(4LL, 2LL, "uuid.c", 146LL, "Could not find MAC. Use bootid's nodeID.\n");
if ( (unsigned int)read_bootid_node(a1 + 10, 6LL) )
{
log_err(4LL, 2LL, "uuid.c", 149LL, "bootid node not successfully read.\n");
read_random_bytes((void *)(a1 + 10), 6uLL);
}
}
else
{
*(_DWORD *)(a1 + 10) = v5;
*(_WORD *)(a1 + 14) = v6;
}
if ( !memcmp(&last_node_2, (const void *)(a1 + 10), 6uLL) )
{
syscall(228LL, 0LL, &v7);
v1 = (10000000 * v7 + v8 / 100 + 122192928000000000LL) & 0xFFFFFFFFFFFFFFFLL;
if ( clock_seq_initialized )
{
if ( last_time_all_0 < v1 )
{
clock_seq_started_1 = clock_seq;
goto LABEL_7;
}
goto LABEL_5;
}
LABEL_10:
init_clockseq();
clock_seq_started_1 = clock_seq;
goto LABEL_7;
}
last_node_2 = *(_DWORD *)(a1 + 10);
word_508 = *(_WORD *)(a1 + 14);
syscall(228LL, 0LL, &v7);
v1 = (10000000 * v7 + v8 / 100 + 122192928000000000LL) & 0xFFFFFFFFFFFFFFFLL;
if ( !clock_seq_initialized )
goto LABEL_10;
LABEL_5:
v2 = ((_WORD)clock_seq + 1) & 0x3FFF;
clock_seq = v2;
if ( v2 == clock_seq_started_1 )
clock_seq = ((_WORD)v2 - 1) & 0x3FFF;
LABEL_7:
last_time_all_0 = v1;
*(_BYTE *)(a1 + 3) = v1;
*(_BYTE *)(a1 + 2) = BYTE1(v1);
*(_BYTE *)(a1 + 1) = BYTE2(v1);
*(_BYTE *)a1 = BYTE3(v1);
*(_BYTE *)(a1 + 5) = BYTE4(v1);
*(_BYTE *)(a1 + 4) = BYTE5(v1);
*(_BYTE *)(a1 + 7) = BYTE6(v1);
v3 = BYTE1(clock_seq);
*(_BYTE *)(a1 + 9) = clock_seq;
*(_BYTE *)(a1 + 6) = HIBYTE(v1) | 0x10;
*(_BYTE *)(a1 + 8) = v3 & 0x3F | 0x80;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
unsigned __int64 v1; // rbx
int v2; // eax
char v3; // ah
int v5; // [rsp+Ah] [rbp-2Eh] BYREF
__int16 v6; // [rsp+Eh] [rbp-2Ah]
__int64 v7; // [rsp+10h] [rbp-28h] BYREF
__int64 v8; // [rsp+18h] [rbp-20h]

v5 = 0;
v6 = 0;
if ( (unsigned int)getsyshwaddr(&v5, 6LL) )
{
log_err(4LL, 2LL, "uuid.c", 146LL, "Could not find MAC. Use bootid's nodeID.\n");
if ( (unsigned int)func2(a1 + 10, 6LL) )
{
log_err(4LL, 2LL, "uuid.c", 149LL, "bootid node not successfully read.\n");
func1((void *)(a1 + 10), 6uLL);
}
}
else
{
*(_DWORD *)(a1 + 10) = v5;
*(_WORD *)(a1 + 14) = v6;
}
if ( !memcmp(&last_node_2, (const void *)(a1 + 10), 6uLL) )
{
syscall(228LL, 0LL, &v7);
v1 = (10000000 * v7 + v8 / 100 + 122192928000000000LL) & 0xFFFFFFFFFFFFFFFLL;
if ( clock_seq_initialized )
{
if ( last_time_all_0 < v1 )
{
clock_seq_started_1 = clock_seq;
goto LABEL_7;
}
goto LABEL_5;
}
LABEL_10:
func3();
clock_seq_started_1 = clock_seq;
goto LABEL_7;
}
last_node_2 = *(_DWORD *)(a1 + 10);
word_508 = *(_WORD *)(a1 + 14);
syscall(228LL, 0LL, &v7);
v1 = (10000000 * v7 + v8 / 100 + 122192928000000000LL) & 0xFFFFFFFFFFFFFFFLL;
if ( !clock_seq_initialized )
goto LABEL_10;
LABEL_5:
v2 = ((_WORD)clock_seq + 1) & 0x3FFF;
clock_seq = v2;
if ( v2 == clock_seq_started_1 )
clock_seq = ((_WORD)v2 - 1) & 0x3FFF;
LABEL_7:
last_time_all_0 = v1;
*(_BYTE *)(a1 + 3) = v1;
*(_BYTE *)(a1 + 2) = BYTE1(v1);
*(_BYTE *)(a1 + 1) = BYTE2(v1);
*(_BYTE *)a1 = BYTE3(v1);
*(_BYTE *)(a1 + 5) = BYTE4(v1);
*(_BYTE *)(a1 + 4) = BYTE5(v1);
*(_BYTE *)(a1 + 7) = BYTE6(v1);
v3 = BYTE1(clock_seq);
*(_BYTE *)(a1 + 9) = clock_seq;
*(_BYTE *)(a1 + 6) = HIBYTE(v1) | 0x10;
*(_BYTE *)(a1 + 8) = v3 & 0x3F | 0x80;
return 0LL;
}

original_code:
int
generate_uuid(unsigned char uuid_out[16])
{
 static u_int64_t last_time_all;
 static unsigned int clock_seq_started;
 static char last_node[6] = { 0, 0, 0, 0, 0, 0 };
 struct timespec ts;
 u_int64_t time_all;
 int inc_clock_seq = 0;
 unsigned char mac[6];
 int mac_error;
 memset(&mac, '\0', sizeof(mac));
 mac_error = getsyshwaddr((char *)mac, sizeof(mac));
 if(!mac_error)
 {
  memcpy(&uuid_out[10], mac, 6);
 }
 else
 {
  { log_err(4, L_HTTP, "uuid.c", 146, "Could not find MAC.  Use bootid's nodeID.\n"); };
  if( read_bootid_node(&uuid_out[10], 6) != 0)
  {
   { log_err(4, L_HTTP, "uuid.c", 149, "bootid node not successfully read.\n"); };
   read_random_bytes(&uuid_out[10], 6);
  }
 }
 if(memcmp(last_node, uuid_out+10, 6) != 0)
 {
  inc_clock_seq = 1;
  memcpy(last_node, uuid_out+10, 6);
 }
 syscall(228, 0, &ts);
 time_all = ((u_int64_t)ts.tv_sec) * (1000000000L / 100);
 time_all += ts.tv_nsec / 100;
 time_all += 12219292800000ULL * (1000000L / 100);
 time_all &= 0x0fffffffffffffffULL;
 if(!clock_seq_initialized)
 {
  init_clockseq();
  clock_seq_started = clock_seq;
 }
 else
 {
  if(inc_clock_seq || time_all <= last_time_all)
  {
   clock_seq = (clock_seq + 1) & clock_seq_max;
   if(clock_seq == clock_seq_started)
   {
    clock_seq = (clock_seq - 1) & clock_seq_max;
   }
  }
  else
   clock_seq_started = clock_seq;
 }
 last_time_all = time_all;
 uuid_out[3] = (u_int8_t)time_all;
 uuid_out[2] = (u_int8_t)(time_all >> 8);
 uuid_out[1] = (u_int8_t)(time_all >> 16);
 uuid_out[0] = (u_int8_t)(time_all >> 24);
 uuid_out[5] = (u_int8_t)(time_all >> 32);
 uuid_out[4] = (u_int8_t)(time_all >> 40);
 uuid_out[7] = (u_int8_t)(time_all >> 48);
 uuid_out[6] = (u_int8_t)(time_all >> 56);
 uuid_out[8] = clock_seq >> 8;
 uuid_out[9] = clock_seq & 0xff;
 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x10;
 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
 return 0;
}

canonical_original_code:
int generate_uuid(unsigned char uuid_out[16])
{
 static unsigned int last_time_all;
 static unsigned int clock_seq_started;
 static char last_node[6] = { 0, 0, 0, 0, 0, 0 };
 struct timespec ts;
 unsigned int time_all;
 int inc_clock_seq = 0;
 unsigned char mac[6];
 int mac_error;
 memset(&mac, '\0', sizeof(mac));
 mac_error = getsyshwaddr((char *)mac, sizeof(mac));
 if(!mac_error)
 {
  memcpy(&uuid_out[10], mac, 6);
 }
 else
 {
  { log_err(4, L_HTTP, "uuid.c", 146, "Could not find MAC.  Use bootid's nodeID.\n"); };
  if( read_bootid_node(&uuid_out[10], 6) != 0)
  {
   { log_err(4, L_HTTP, "uuid.c", 149, "bootid node not successfully read.\n"); };
   read_random_bytes(&uuid_out[10], 6);
  }
 }
 if(memcmp(last_node, uuid_out+10, 6) != 0)
 {
  inc_clock_seq = 1;
  memcpy(last_node, uuid_out+10, 6);
 }
 syscall(228, 0, &ts);
 time_all = ((unsigned int)ts.tv_sec) * (1000000000L / 100);
 time_all += ts.tv_nsec / 100;
 time_all += 12219292800000ULL * (1000000L / 100);
 time_all &= 0x0fffffffffffffffULL;
 if(!clock_seq_initialized)
 {
  init_clockseq();
  clock_seq_started = clock_seq;
 }
 else
 {
  if(inc_clock_seq || time_all <= last_time_all)
  {
   clock_seq = (clock_seq + 1) & clock_seq_max;
   if(clock_seq == clock_seq_started)
   {
    clock_seq = (clock_seq - 1) & clock_seq_max;
   }
  }
  else
   clock_seq_started = clock_seq;
 }
 last_time_all = time_all;
 uuid_out[3] = (unsigned int)time_all;
 uuid_out[2] = (u_int8_t)(time_all >> 8);
 uuid_out[1] = (u_int8_t)(time_all >> 16);
 uuid_out[0] = (u_int8_t)(time_all >> 24);
 uuid_out[5] = (u_int8_t)(time_all >> 32);
 uuid_out[4] = (u_int8_t)(time_all >> 40);
 uuid_out[7] = (u_int8_t)(time_all >> 48);
 uuid_out[6] = (u_int8_t)(time_all >> 56);
 uuid_out[8] = clock_seq >> 8;
 uuid_out[9] = clock_seq & 0xff;
 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x10;
 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
 return 0;
}

variable_types:
mac
unsigned char [6]

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


decompiled_code:
__int64 __fastcall get_uuid_string(char *s)
{
unsigned int v1; // ebx
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

if ( (unsigned int)generate_uuid((__int64)v3) )
return (unsigned int)-1;
v1 = 0;
sprintf(
s,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
(unsigned __int8)v3[0],
(unsigned __int8)v3[1],
(unsigned __int8)v3[2],
(unsigned __int8)v3[3],
(unsigned __int8)v3[4],
(unsigned __int8)v3[5],
(unsigned __int8)v3[6],
(unsigned __int8)v3[7],
(unsigned __int8)v3[8],
(unsigned __int8)v3[9],
(unsigned __int8)v3[10],
(unsigned __int8)v3[11],
(unsigned __int8)v3[12],
(unsigned __int8)v3[13],
(unsigned __int8)v3[14],
(unsigned __int8)v3[15]);
s[36] = 0;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s)
{
unsigned int v1; // ebx
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

if ( (unsigned int)func4((__int64)v3) )
return (unsigned int)-1;
v1 = 0;
sprintf(
s,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
(unsigned __int8)v3[0],
(unsigned __int8)v3[1],
(unsigned __int8)v3[2],
(unsigned __int8)v3[3],
(unsigned __int8)v3[4],
(unsigned __int8)v3[5],
(unsigned __int8)v3[6],
(unsigned __int8)v3[7],
(unsigned __int8)v3[8],
(unsigned __int8)v3[9],
(unsigned __int8)v3[10],
(unsigned __int8)v3[11],
(unsigned __int8)v3[12],
(unsigned __int8)v3[13],
(unsigned __int8)v3[14],
(unsigned __int8)v3[15]);
s[36] = 0;
return v1;
}

variable_types:
buf
char *

variable_types:
uuid
unsigned char [16]


parseatt

decompiled_code:
__int64 __fastcall parseatt(_QWORD *a1)
{
unsigned __int8 *v1; // rsi
unsigned __int64 v2; // rdx
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // r13
unsigned __int64 v7; // rax
unsigned int v8; // er9
unsigned __int8 *v9; // rax
unsigned __int8 *v10; // rax
unsigned __int64 v11; // rcx
_BYTE *v12; // rax
unsigned __int8 *v14; // rcx
unsigned __int8 *v15; // rcx
unsigned __int64 v16; // rax
__int64 v17; // r8
_BYTE *v18; // rdi
_BYTE *v19; // rdi
unsigned __int8 *v20; // rax
unsigned __int8 *v21; // rax
unsigned __int64 v22; // rdi
void (__fastcall *v23)(_QWORD, unsigned __int8 *, _QWORD, unsigned __int8 *, __int64); // rax

v1 = (unsigned __int8 *)a1[2];
v2 = a1[1];
if ( (unsigned __int64)v1 >= v2 )
return 0xFFFFFFFFLL;
v4 = 0x2000000100002600LL;
v5 = 0x100002600LL;
v6 = 0x4000800100002600LL;
while ( 1 )
{
v7 = *v1;
if ( (_BYTE)v7 == 47 || (_BYTE)v7 == 62 )
return 0LL;
if ( (unsigned __int8)v7 <= 0x20u )
{
if ( !_bittest64(&v5, v7) )
{
v8 = 0;
goto LABEL_10;
}
goto LABEL_4;
}
v8 = 0;
LABEL_10:
while ( 1 )
{
v10 = (unsigned __int8 *)a1[2];
v11 = *v10;
if ( (unsigned __int8)v11 <= 0x3Du )
{
if ( _bittest64(&v4, v11) )
break;
}
++v8;
v9 = v10 + 1;
a1[2] = v9;
if ( (unsigned __int64)v9 >= v2 )
return 0xFFFFFFFFLL;
}
while ( 1 )
{
v12 = (_BYTE *)a1[2];
a1[2] = v12 + 1;
if ( *v12 == 61 )
break;
if ( (unsigned __int64)(v12 + 1) >= v2 )
return 0xFFFFFFFFLL;
}
while ( 1 )
{
v15 = (unsigned __int8 *)a1[2];
v16 = *v15;
if ( (unsigned __int8)v16 > 0x20u || !_bittest64(&v5, v16) )
break;
v14 = v15 + 1;
a1[2] = v14;
if ( (unsigned __int64)v14 >= v2 )
return 0xFFFFFFFFLL;
}
if ( (_BYTE)v16 != 39 )
{
v17 = 0LL;
if ( (_BYTE)v16 != 34 )
{
while ( 1 )
{
v21 = (unsigned __int8 *)a1[2];
v22 = *v21;
if ( (unsigned __int8)v22 <= 0x3Eu )
{
if ( _bittest64(&v6, v22) )
break;
}
v17 = (unsigned int)(v17 + 1);
v20 = v21 + 1;
a1[2] = v20;
if ( (unsigned __int64)v20 >= v2 )
return 0xFFFFFFFFLL;
}
goto LABEL_30;
}
}
a1[2] = ++v15;
if ( (unsigned __int64)v15 >= v2 )
return 0xFFFFFFFFLL;
v17 = 0LL;
while ( 1 )
{
v18 = (_BYTE *)a1[2];
if ( (_BYTE)v16 == *v18 )
break;
v17 = (unsigned int)(v17 + 1);
v19 = v18 + 1;
a1[2] = v19;
if ( (unsigned __int64)v19 >= v2 )
return 0xFFFFFFFFLL;
}
LABEL_30:
v23 = (void (__fastcall *)(_QWORD, unsigned __int8 *, _QWORD, unsigned __int8 *, __int64))a1[8];
if ( v23 )
v23(a1[4], v1, v8, v15, v17);
LABEL_4:
v1 = (unsigned __int8 *)(a1[2] + 1LL);
a1[2] = v1;
v2 = a1[1];
if ( (unsigned __int64)v1 >= v2 )
return 0xFFFFFFFFLL;
}
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
unsigned __int8 *v1; // rsi
unsigned __int64 v2; // rdx
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // r13
unsigned __int64 v7; // rax
unsigned int v8; // er9
unsigned __int8 *v9; // rax
unsigned __int8 *v10; // rax
unsigned __int64 v11; // rcx
_BYTE *v12; // rax
unsigned __int8 *v14; // rcx
unsigned __int8 *v15; // rcx
unsigned __int64 v16; // rax
__int64 v17; // r8
_BYTE *v18; // rdi
_BYTE *v19; // rdi
unsigned __int8 *v20; // rax
unsigned __int8 *v21; // rax
unsigned __int64 v22; // rdi
void (__fastcall *v23)(_QWORD, unsigned __int8 *, _QWORD, unsigned __int8 *, __int64); // rax

v1 = (unsigned __int8 *)a1[2];
v2 = a1[1];
if ( (unsigned __int64)v1 >= v2 )
return 0xFFFFFFFFLL;
v4 = 0x2000000100002600LL;
v5 = 0x100002600LL;
v6 = 0x4000800100002600LL;
while ( 1 )
{
v7 = *v1;
if ( (_BYTE)v7 == 47 || (_BYTE)v7 == 62 )
return 0LL;
if ( (unsigned __int8)v7 <= 0x20u )
{
if ( !_bittest64(&v5, v7) )
{
v8 = 0;
goto LABEL_10;
}
goto LABEL_4;
}
v8 = 0;
LABEL_10:
while ( 1 )
{
v10 = (unsigned __int8 *)a1[2];
v11 = *v10;
if ( (unsigned __int8)v11 <= 0x3Du )
{
if ( _bittest64(&v4, v11) )
break;
}
++v8;
v9 = v10 + 1;
a1[2] = v9;
if ( (unsigned __int64)v9 >= v2 )
return 0xFFFFFFFFLL;
}
while ( 1 )
{
v12 = (_BYTE *)a1[2];
a1[2] = v12 + 1;
if ( *v12 == 61 )
break;
if ( (unsigned __int64)(v12 + 1) >= v2 )
return 0xFFFFFFFFLL;
}
while ( 1 )
{
v15 = (unsigned __int8 *)a1[2];
v16 = *v15;
if ( (unsigned __int8)v16 > 0x20u || !_bittest64(&v5, v16) )
break;
v14 = v15 + 1;
a1[2] = v14;
if ( (unsigned __int64)v14 >= v2 )
return 0xFFFFFFFFLL;
}
if ( (_BYTE)v16 != 39 )
{
v17 = 0LL;
if ( (_BYTE)v16 != 34 )
{
while ( 1 )
{
v21 = (unsigned __int8 *)a1[2];
v22 = *v21;
if ( (unsigned __int8)v22 <= 0x3Eu )
{
if ( _bittest64(&v6, v22) )
break;
}
v17 = (unsigned int)(v17 + 1);
v20 = v21 + 1;
a1[2] = v20;
if ( (unsigned __int64)v20 >= v2 )
return 0xFFFFFFFFLL;
}
goto LABEL_30;
}
}
a1[2] = ++v15;
if ( (unsigned __int64)v15 >= v2 )
return 0xFFFFFFFFLL;
v17 = 0LL;
while ( 1 )
{
v18 = (_BYTE *)a1[2];
if ( (_BYTE)v16 == *v18 )
break;
v17 = (unsigned int)(v17 + 1);
v19 = v18 + 1;
a1[2] = v19;
if ( (unsigned __int64)v19 >= v2 )
return 0xFFFFFFFFLL;
}
LABEL_30:
v23 = (void (__fastcall *)(_QWORD, unsigned __int8 *, _QWORD, unsigned __int8 *, __int64))a1[8];
if ( v23 )
v23(a1[4], v1, v8, v15, v17);
LABEL_4:
v1 = (unsigned __int8 *)(a1[2] + 1LL);
a1[2] = v1;
v2 = a1[1];
if ( (unsigned __int64)v1 >= v2 )
return 0xFFFFFFFFLL;
}
}

original_code:
int parseatt(struct xmlparser * p)
{
 const char * attname;
 int attnamelen;
 const char * attvalue;
 int attvaluelen;
 while(p->xml < p->xmlend)
 {
  if(*p->xml=='/' || *p->xml=='>')
   return 0;
  if( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
  {
   char sep;
   attname = p->xml;
   attnamelen = 0;
   while(*p->xml!='=' && !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
   {
    attnamelen++; p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
   }
   while(*(p->xml++) != '=')
   {
    if(p->xml >= p->xmlend)
     return -1;
   }
   while(((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')))
   {
    p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
   }
   sep = *p->xml;
   if(sep=='\'' || sep=='\"')
   {
    p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
    attvalue = p->xml;
    attvaluelen = 0;
    while(*p->xml != sep)
    {
     attvaluelen++; p->xml++;
     if(p->xml >= p->xmlend)
      return -1;
    }
   }
   else
   {
    attvalue = p->xml;
    attvaluelen = 0;
    while( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n'))
       && *p->xml != '>' && *p->xml != '/')
    {
     attvaluelen++; p->xml++;
     if(p->xml >= p->xmlend)
      return -1;
    }
   }
   if(p->attfunc)
    p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen);
  }
  p->xml++;
 }
 return -1;
}

canonical_original_code:
int parseatt(struct xmlparser *p)
{
 char *attname;
 int attnamelen;
 char *attvalue;
 int attvaluelen;
 while(p->xml < p->xmlend)
 {
  if(*p->xml=='/' || *p->xml=='>')
   return 0;
  if( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
  {
   char sep;
   attname = p->xml;
   attnamelen = 0;
   while(*p->xml!='=' && !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
   {
    attnamelen++; p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
   }
   while(*(p->xml++) != '=')
   {
    if(p->xml >= p->xmlend)
     return -1;
   }
   while(((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')))
   {
    p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
   }
   sep = *p->xml;
   if(sep=='\'' || sep=='\"')
   {
    p->xml++;
    if(p->xml >= p->xmlend)
     return -1;
    attvalue = p->xml;
    attvaluelen = 0;
    while(*p->xml != sep)
    {
     attvaluelen++; p->xml++;
     if(p->xml >= p->xmlend)
      return -1;
    }
   }
   else
   {
    attvalue = p->xml;
    attvaluelen = 0;
    while( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n'))
       && *p->xml != '>' && *p->xml != '/')
    {
     attvaluelen++; p->xml++;
     if(p->xml >= p->xmlend)
      return -1;
    }
   }
   if(p->attfunc)
    p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen);
  }
  p->xml++;
 }
 return -1;
}

variable_types: struct xmlparser { char *xmlstart; char *xmlend; char *xml; int xmlsize; void *data; void (*starteltfunc)(void *, char *, int); void (*endeltfunc)(void *, char *, int); void (*datafunc)(void *, char *, int); void (*attfunc)(void *, char *, int, char *, int); } *

variable_types:
attname
char *

variable_types: attnamelen int

variable_types:
attvalue
char *

variable_types: attvaluelen int

variable_types: sep char


parseelt

decompiled_code:
unsigned __int8 *__fastcall parseelt(_QWORD *a1)
{
_BYTE *v1; // rsi
unsigned __int64 v2; // rcx
unsigned __int8 *result; // rax
__int64 v4; // rbp
int v5; // er8
unsigned __int64 v6; // rdx
void (__fastcall *v7)(_QWORD, unsigned __int8 *, _QWORD); // rax
int v8; // er9
_BYTE *v9; // r8
_BYTE *v10; // r8
unsigned __int64 v11; // rsi
unsigned __int8 *v12; // rsi
_BYTE *v13; // rax
char v14; // dl
_BYTE *v15; // rax
void (__fastcall *v16)(_QWORD, _BYTE *, _QWORD); // rax
unsigned __int8 *v17; // rsi
__int64 v18; // rdx
_BYTE *v19; // rax
void (__fastcall *v20)(_QWORD, unsigned __int8 *, __int64); // rax

v1 = (_BYTE *)a1[2];
v2 = a1[1];
result = (unsigned __int8 *)(v2 - 1);
if ( (unsigned __int64)v1 < v2 - 1 )
{
v4 = 0x4000800100002600LL;
do
{
if ( *v1 != 60 || v1[1] == 63 )
{
a1[2] = v1 + 1;
}
else
{
v12 = v1 + 1;
a1[2] = v12;
LABEL_39:
v5 = 0;
while ( 1 )
{
result = (unsigned __int8 *)a1[2];
v6 = *result;
if ( (unsigned __int8)v6 <= 0x3Eu )
{
if ( _bittest64(&v4, v6) )
break;
}
++v5;
a1[2] = result + 1;
if ( (unsigned __int64)(result + 1) >= v2 )
return result;
if ( result[1] == 58 )
{
v12 = result + 2;
a1[2] = result + 2;
goto LABEL_39;
}
}
if ( v5 <= 0 )
{
if ( (_BYTE)v6 == 47 )
{
v17 = result + 1;
a1[2] = result + 1;
if ( (unsigned __int64)(result + 1) >= v2 )
return result;
v18 = 0LL;
while ( 1 )
{
v19 = (_BYTE *)a1[2];
if ( *v19 == 62 )
break;
v18 = (unsigned int)(v18 + 1);
result = v19 + 1;
a1[2] = result;
if ( (unsigned __int64)result >= v2 )
return result;
}
v20 = (void (__fastcall *)(_QWORD, unsigned __int8 *, __int64))a1[6];
if ( v20 )
v20(a1[4], v17, v18);
++a1[2];
}
}
else
{
v7 = (void (__fastcall *)(_QWORD, unsigned __int8 *, _QWORD))a1[5];
if ( v7 )
v7(a1[4], v12, (unsigned int)v5);
result = (unsigned __int8 *)parseatt(a1);
v8 = (int)result;
if ( (_DWORD)result )
return result;
v9 = (_BYTE *)a1[2];
if ( *v9 != 47 )
{
v10 = v9 + 1;
a1[2] = v10;
v11 = a1[1];
if ( (unsigned __int64)v10 >= v11 )
return result;
while ( 1 )
{
v13 = (_BYTE *)a1[2];
v14 = 1;
if ( *v13 <= 0x20u )
v14 = ((0x100002600uLL >> *v13) ^ 1) & 1;
if ( v14 )
break;
result = v13 + 1;
a1[2] = result;
if ( v11 <= (unsigned __int64)result )
return result;
}
while ( 1 )
{
v15 = (_BYTE *)a1[2];
if ( *v15 == 60 )
break;
++v8;
result = v15 + 1;
a1[2] = result;
if ( v11 <= (unsigned __int64)result )
return result;
}
if ( v8 > 0 )
{
v16 = (void (__fastcall *)(_QWORD, _BYTE *, _QWORD))a1[7];
if ( v16 )
v16(a1[4], v10, (unsigned int)v8);
}
}
}
}
v1 = (_BYTE *)a1[2];
v2 = a1[1];
result = (unsigned __int8 *)(v2 - 1);
}
while ( (unsigned __int64)v1 < v2 - 1 );
}
return result;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func1(_QWORD *a1)
{
_BYTE *v1; // rsi
unsigned __int64 v2; // rcx
unsigned __int8 *result; // rax
__int64 v4; // rbp
int v5; // er8
unsigned __int64 v6; // rdx
void (__fastcall *v7)(_QWORD, unsigned __int8 *, _QWORD); // rax
int v8; // er9
_BYTE *v9; // r8
_BYTE *v10; // r8
unsigned __int64 v11; // rsi
unsigned __int8 *v12; // rsi
_BYTE *v13; // rax
char v14; // dl
_BYTE *v15; // rax
void (__fastcall *v16)(_QWORD, _BYTE *, _QWORD); // rax
unsigned __int8 *v17; // rsi
__int64 v18; // rdx
_BYTE *v19; // rax
void (__fastcall *v20)(_QWORD, unsigned __int8 *, __int64); // rax

v1 = (_BYTE *)a1[2];
v2 = a1[1];
result = (unsigned __int8 *)(v2 - 1);
if ( (unsigned __int64)v1 < v2 - 1 )
{
v4 = 0x4000800100002600LL;
do
{
if ( *v1 != 60 || v1[1] == 63 )
{
a1[2] = v1 + 1;
}
else
{
v12 = v1 + 1;
a1[2] = v12;
LABEL_39:
v5 = 0;
while ( 1 )
{
result = (unsigned __int8 *)a1[2];
v6 = *result;
if ( (unsigned __int8)v6 <= 0x3Eu )
{
if ( _bittest64(&v4, v6) )
break;
}
++v5;
a1[2] = result + 1;
if ( (unsigned __int64)(result + 1) >= v2 )
return result;
if ( result[1] == 58 )
{
v12 = result + 2;
a1[2] = result + 2;
goto LABEL_39;
}
}
if ( v5 <= 0 )
{
if ( (_BYTE)v6 == 47 )
{
v17 = result + 1;
a1[2] = result + 1;
if ( (unsigned __int64)(result + 1) >= v2 )
return result;
v18 = 0LL;
while ( 1 )
{
v19 = (_BYTE *)a1[2];
if ( *v19 == 62 )
break;
v18 = (unsigned int)(v18 + 1);
result = v19 + 1;
a1[2] = result;
if ( (unsigned __int64)result >= v2 )
return result;
}
v20 = (void (__fastcall *)(_QWORD, unsigned __int8 *, __int64))a1[6];
if ( v20 )
v20(a1[4], v17, v18);
++a1[2];
}
}
else
{
v7 = (void (__fastcall *)(_QWORD, unsigned __int8 *, _QWORD))a1[5];
if ( v7 )
v7(a1[4], v12, (unsigned int)v5);
result = (unsigned __int8 *)func0(a1);
v8 = (int)result;
if ( (_DWORD)result )
return result;
v9 = (_BYTE *)a1[2];
if ( *v9 != 47 )
{
v10 = v9 + 1;
a1[2] = v10;
v11 = a1[1];
if ( (unsigned __int64)v10 >= v11 )
return result;
while ( 1 )
{
v13 = (_BYTE *)a1[2];
v14 = 1;
if ( *v13 <= 0x20u )
v14 = ((0x100002600uLL >> *v13) ^ 1) & 1;
if ( v14 )
break;
result = v13 + 1;
a1[2] = result;
if ( v11 <= (unsigned __int64)result )
return result;
}
while ( 1 )
{
v15 = (_BYTE *)a1[2];
if ( *v15 == 60 )
break;
++v8;
result = v15 + 1;
a1[2] = result;
if ( v11 <= (unsigned __int64)result )
return result;
}
if ( v8 > 0 )
{
v16 = (void (__fastcall *)(_QWORD, _BYTE *, _QWORD))a1[7];
if ( v16 )
v16(a1[4], v10, (unsigned int)v8);
}
}
}
}
v1 = (_BYTE *)a1[2];
v2 = a1[1];
result = (unsigned __int8 *)(v2 - 1);
}
while ( (unsigned __int64)v1 < v2 - 1 );
}
return result;
}

original_code:
void parseelt(struct xmlparser * p)
{
 int i;
 const char * elementname;
 while(p->xml < (p->xmlend - 1))
 {
  if((p->xml)[0]=='<' && (p->xml)[1]!='?')
  {
   i = 0; elementname = ++p->xml;
   while( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n'))
      && (*p->xml!='>') && (*p->xml!='/')
     )
   {
    i++; p->xml++;
    if (p->xml >= p->xmlend)
     return;
    if(*p->xml==':')
    {
     i = 0;
     elementname = ++p->xml;
    }
   }
   if(i>0)
   {
    if(p->starteltfunc)
     p->starteltfunc(p->data, elementname, i);
    if(parseatt(p))
     return;
    if(*p->xml!='/')
    {
     const char * data;
     i = 0; data = ++p->xml;
     if (p->xml >= p->xmlend)
      return;
     while( ((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
     {
      p->xml++;
      if (p->xml >= p->xmlend)
       return;
     }
     while(*p->xml!='<')
     {
      i++; p->xml++;
      if (p->xml >= p->xmlend)
       return;
     }
     if(i>0 && p->datafunc)
      p->datafunc(p->data, data, i);
    }
   }
   else if(*p->xml == '/')
   {
    i = 0; elementname = ++p->xml;
    if (p->xml >= p->xmlend)
     return;
    while((*p->xml != '>'))
    {
     i++; p->xml++;
     if (p->xml >= p->xmlend)
      return;
    }
    if(p->endeltfunc)
     p->endeltfunc(p->data, elementname, i);
    p->xml++;
   }
  }
  else
  {
   p->xml++;
  }
 }
}

canonical_original_code:
void parseelt(struct xmlparser *p)
{
 int i;
 char *elementname;
 while(p->xml < (p->xmlend - 1))
 {
  if((p->xml)[0]=='<' && (p->xml)[1]!='?')
  {
   i = 0; elementname = ++p->xml;
   while( !((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n'))
      && (*p->xml!='>') && (*p->xml!='/')
     )
   {
    i++; p->xml++;
    if (p->xml >= p->xmlend)
     return;
    if(*p->xml==':')
    {
     i = 0;
     elementname = ++p->xml;
    }
   }
   if(i>0)
   {
    if(p->starteltfunc)
     p->starteltfunc(p->data, elementname, i);
    if(parseatt(p))
     return;
    if(*p->xml!='/')
    {
     char *data;
     i = 0; data = ++p->xml;
     if (p->xml >= p->xmlend)
      return;
     while( ((*p->xml==' ') || (*p->xml=='\t') || (*p->xml=='\r') || (*p->xml=='\n')) )
     {
      p->xml++;
      if (p->xml >= p->xmlend)
       return;
     }
     while(*p->xml!='<')
     {
      i++; p->xml++;
      if (p->xml >= p->xmlend)
       return;
     }
     if(i>0 && p->datafunc)
      p->datafunc(p->data, data, i);
    }
   }
   else if(*p->xml == '/')
   {
    i = 0; elementname = ++p->xml;
    if (p->xml >= p->xmlend)
     return;
    while((*p->xml != '>'))
    {
     i++; p->xml++;
     if (p->xml >= p->xmlend)
      return;
    }
    if(p->endeltfunc)
     p->endeltfunc(p->data, elementname, i);
    p->xml++;
   }
  }
  else
  {
   p->xml++;
  }
 }
}

variable_types:
elementname
char *

variable_types:
data
char *

user_defined_types: struct xmlparser { char *xmlstart; char *xmlend; char *xml; int xmlsize; void *data; void (*starteltfunc)(void *, char *, int); void (*endeltfunc)(void *, char *, int); void (*datafunc)(void *, char *, int); void (*attfunc)(void *, char *, int, char *, int); }


parsexml

decompiled_code:
unsigned __int8 *__fastcall parsexml(__int64 *a1)
{
__int64 v1; // rax

v1 = *a1;
a1[2] = *a1;
a1[1] = *((int *)a1 + 6) + v1;
return parseelt(a1);
}

original_code:
void parsexml(struct xmlparser * parser)
{
 parser->xml = parser->xmlstart;
 parser->xmlend = parser->xmlstart + parser->xmlsize;
 parseelt(parser);
}

canonical_original_code:
void parsexml(struct xmlparser *parser)
{
 parser->xml = parser->xmlstart;
 parser->xmlend = parser->xmlstart + parser->xmlsize;
 parseelt(parser);
}


freeoptions

decompiled_code:
void freeoptions()
{
if ( ary_options )
{
free(ary_options);
ary_options = 0LL;
num_options = 0;
}
}

canonical_decompiled_code:
void func1()
{
if ( ary_options )
{
free(ary_options);
ary_options = 0LL;
num_options = 0;
}
}

original_code:
void
freeoptions(void)
{
 if(ary_options)
 {
  free(ary_options);
  ary_options = ((void *)0);
  num_options = 0;
 }
}

canonical_original_code:
void freeoptions()
{
 if(ary_options)
 {
  free(ary_options);
  ary_options = ((void *)0);
  num_options = 0;
 }
}


writepidfile

decompiled_code:
__int64 __fastcall writepidfile(const char *a1, unsigned int a2)
{
int v2; // ebp
int v3; // eax
__int64 result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
char v9[40]; // [rsp-28h] [rbp-28h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = open64(a1, 65, 420LL);
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
log_err(2LL, 0LL, "daemonize.c", 98LL, "Unable to open pidfile for writing %s: %s\n", a1, v6);
result = 0xFFFFFFFFLL;
}
else
{
v3 = snprintf(v9, 0x10uLL, "%d\n", a2);
if ( v3 <= 0 )
{
log_err(2LL, 0LL, "daemonize.c", 105LL, "Unable to write to pidfile %s: snprintf(): FAILED\n", a1);
close(v2);
result = 0xFFFFFFFFLL;
}
else
{
if ( write(v2, v9, v3) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_err(2LL, 0LL, "daemonize.c", 113LL, "Unable to write to pidfile %s: %s\n", a1, v8);
}
close(v2);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2)
{
int v2; // ebp
int v3; // eax
__int64 result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
char v9[40]; // [rsp-28h] [rbp-28h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = open64(a1, 65, 420LL);
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
log_err(2LL, 0LL, "daemonize.c", 98LL, "Unable to open pidfile for writing %s: %s\n", a1, v6);
result = 0xFFFFFFFFLL;
}
else
{
v3 = snprintf(v9, 0x10uLL, "%d\n", a2);
if ( v3 <= 0 )
{
log_err(2LL, 0LL, "daemonize.c", 105LL, "Unable to write to pidfile %s: snprintf(): FAILED\n", a1);
close(v2);
result = 0xFFFFFFFFLL;
}
else
{
if ( write(v2, v9, v3) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_err(2LL, 0LL, "daemonize.c", 113LL, "Unable to write to pidfile %s: %s\n", a1, v8);
}
close(v2);
result = 0LL;
}
}
return result;
}

original_code:
int
writepidfile(const char * fname, int pid)
{
 char pidstring[16];
 int pidstringlen;
 int pidfile;
 if(!fname || (strlen(fname) == 0))
  return -1;
 if( (pidfile = open(fname, 01|0100, 0644)) < 0)
 {
  { log_err(2, L_GENERAL, "daemonize.c", 98, "Unable to open pidfile for writing %s: %s\n", fname, strerror((*__errno_location ()))); };
  return -1;
 }
 pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
 if(pidstringlen <= 0)
 {
  { log_err(2, L_GENERAL, "daemonize.c", 105, "Unable to write to pidfile %s: snprintf(): FAILED\n", fname); };
  close(pidfile);
  return -1;
 }
 else
 {
  if(write(pidfile, pidstring, pidstringlen) < 0)
   { log_err(2, L_GENERAL, "daemonize.c", 113, "Unable to write to pidfile %s: %s\n", fname, strerror((*__errno_location ()))); };
 }
 close(pidfile);
 return 0;
}

canonical_original_code:
int writepidfile(char *fname, int pid)
{
 char pidstring[16];
 int pidstringlen;
 int pidfile;
 if(!fname || (strlen(fname) == 0))
  return -1;
 if( (pidfile = open(fname, 01|0100, 0644)) < 0)
 {
  { log_err(2, L_GENERAL, "daemonize.c", 98, "Unable to open pidfile for writing %s: %s\n", fname, strerror((*__errno_location ()))); };
  return -1;
 }
 pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
 if(pidstringlen <= 0)
 {
  { log_err(2, L_GENERAL, "daemonize.c", 105, "Unable to write to pidfile %s: snprintf(): FAILED\n", fname); };
  close(pidfile);
  return -1;
 }
 else
 {
  if(write(pidfile, pidstring, pidstringlen) < 0)
   { log_err(2, L_GENERAL, "daemonize.c", 113, "Unable to write to pidfile %s: %s\n", fname, strerror((*__errno_location ()))); };
 }
 close(pidfile);
 return 0;
}

variable_types:
fname
char *

variable_types:
pidstring
char [16]

variable_types: pidstringlen int

variable_types: pidfile int


checkforrunning

decompiled_code:
__int64 __fastcall checkforrunning(const char *a1)
{
int v1; // ebx
__int64 result; // rax
__pid_t v3; // eax
__int64 v4[9]; // [rsp+0h] [rbp-48h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*a1 )
return 0xFFFFFFFFLL;
v1 = open64(a1, 0);
result = 0LL;
if ( v1 >= 0 )
{
v4[0] = 0LL;
v4[1] = 0LL;
v4[2] = 0LL;
v4[3] = 0LL;
v4[4] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
if ( !read(v1, v4, 0x3FuLL) || (v3 = atol((const char *)v4), v3 <= 0) || kill(v3, 0) )
{
close(v1);
result = 0LL;
}
else
{
close(v1);
result = 4294967294LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
int v1; // ebx
__int64 result; // rax
__pid_t v3; // eax
__int64 v4[9]; // [rsp+0h] [rbp-48h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*a1 )
return 0xFFFFFFFFLL;
v1 = open64(a1, 0);
result = 0LL;
if ( v1 >= 0 )
{
v4[0] = 0LL;
v4[1] = 0LL;
v4[2] = 0LL;
v4[3] = 0LL;
v4[4] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
if ( !read(v1, v4, 0x3FuLL) || (v3 = atol((const char *)v4), v3 <= 0) || kill(v3, 0) )
{
close(v1);
result = 0LL;
}
else
{
close(v1);
result = 4294967294LL;
}
}
return result;
}

original_code:
int
checkforrunning(const char * fname)
{
 char buffer[64];
 int pidfile;
 pid_t pid;
 if(!fname || (strlen(fname) == 0))
  return -1;
 if( (pidfile = open(fname, 00)) < 0)
  return 0;
 memset(buffer, 0, 64);
 if(read(pidfile, buffer, 63))
 {
  if( (pid = atol(buffer)) > 0)
  {
   if(!kill(pid, 0))
   {
    close(pidfile);
    return -2;
   }
  }
 }
 close(pidfile);
 return 0;
}

canonical_original_code:
int checkforrunning(char *fname)
{
 char buffer[64];
 int pidfile;
 int pid;
 if(!fname || (strlen(fname) == 0))
  return -1;
 if( (pidfile = open(fname, 00)) < 0)
  return 0;
 memset(buffer, 0, 64);
 if(read(pidfile, buffer, 63))
 {
  if( (pid = atol(buffer)) > 0)
  {
   if(!kill(pid, 0))
   {
    close(pidfile);
    return -2;
   }
  }
 }
 close(pidfile);
 return 0;
}

variable_types:
buffer
char [64]

variable_types: pid int


SearchClientCache

decompiled_code:
__int64 __fastcall SearchClientCache(unsigned int a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rbp
__int64 v5; // rax
char s1[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v2 = 0LL;
while ( 1 )
{
v3 = (unsigned int)v2;
if ( *((_DWORD *)&clients + 8 * v2) == a1 )
break;
if ( ++v2 == 20 )
{
LODWORD(v3) = -1;
return (unsigned int)v3;
}
}
if ( time(0LL) - *((_QWORD *)&clients + 4 * (int)v2 + 3) > 3600 )
{
if ( (unsigned int)get_remote_mac(a1, s1) || memcmp(s1, (char *)&clients + 32 * (int)v3 + 4, 6uLL) )
{
v5 = 32LL * (int)v3;
*(_QWORD *)((char *)&clients + v5) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 8) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 16) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 24) = 0LL;
LODWORD(v3) = -1;
return (unsigned int)v3;
}
*((_QWORD *)&clients + 4 * (int)v3 + 3) = time(0LL);
}
if ( !a2 )
log_err(
5LL,
2LL,
"utils.c",
61LL,
"Client found in cache. [type %d/entry %d]\n",
*((unsigned int *)&clients + 8 * (int)v3 + 3),
v3);
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rbp
__int64 v5; // rax
char s1[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v2 = 0LL;
while ( 1 )
{
v3 = (unsigned int)v2;
if ( *((_DWORD *)&clients + 8 * v2) == a1 )
break;
if ( ++v2 == 20 )
{
LODWORD(v3) = -1;
return (unsigned int)v3;
}
}
if ( time(0LL) - *((_QWORD *)&clients + 4 * (int)v2 + 3) > 3600 )
{
if ( (unsigned int)get_remote_mac(a1, s1) || memcmp(s1, (char *)&clients + 32 * (int)v3 + 4, 6uLL) )
{
v5 = 32LL * (int)v3;
*(_QWORD *)((char *)&clients + v5) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 8) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 16) = 0LL;
*(_QWORD *)((char *)&clients + v5 + 24) = 0LL;
LODWORD(v3) = -1;
return (unsigned int)v3;
}
*((_QWORD *)&clients + 4 * (int)v3 + 3) = time(0LL);
}
if ( !a2 )
log_err(
5LL,
2LL,
"utils.c",
61LL,
"Client found in cache. [type %d/entry %d]\n",
*((unsigned int *)&clients + 8 * (int)v3 + 3),
v3);
return (unsigned int)v3;
}

original_code:
int
SearchClientCache(struct in_addr addr, int quiet)
{
 int i;
 for( i=0; i<20; i++ )
 {
  if( clients[i].addr.s_addr == addr.s_addr )
  {
   if( (time(((void *)0)) - clients[i].age) > 3600 )
   {
    unsigned char mac[6];
    if( get_remote_mac(addr, mac) == 0 &&
        memcmp(mac, clients[i].mac, 6) == 0 )
    {
     clients[i].age = time(((void *)0));
    }
    else
    {
     memset(&clients[i], 0, sizeof(struct client_cache_s));
     return -1;
    }
   }
   if( !quiet )
    { log_err(5, L_HTTP, "utils.c", 61, "Client found in cache. [type %d/entry %d]\n", clients[i].type, i); };
   return i;
  }
 }
 return -1;
}

variable_types:
addr
struct in_addr { unsigned int s_addr; }

variable_types: quiet int

variable_types: i int

variable_types:
mac
unsigned char [6]

user_defined_types: struct client_cache_s { struct in_addr addr; unsigned char mac[6]; enum client_types type; unsigned int flags; long age; }

user_defined_types: enum client_types { EXbox=1, EPS3=2, ESamsungTV=3, EDenonReceiver=4, EFreeBox=5, EPopcornHour=6, EMediaRoom=7, ESonyBDP=8, ESonyBravia=9, ERokuSoundBridge=10, EToshibaTV=11, ELGDevice=12, ENetgearEVA2000=13, ESamsungSeriesA=14, ESamsungSeriesB=15, EMarantzDMP=16, EStandardDLNA150=100 }


strstrc

decompiled_code:
char *__fastcall strstrc(const char *a1, const char *a2, char a3)
{
char *v4; // rbx
size_t v5; // rax
size_t v6; // r12
signed __int64 v7; // rdi
char v8; // r13
size_t v9; // r15
const char *v10; // rbp

v4 = strchr(a1, a3);
if ( v4 )
{
v5 = strlen(a2);
v6 = v5;
v7 = v4 - a1;
if ( v5 > v4 - a1 )
{
v4 = 0LL;
}
else
{
v8 = *a2;
v4 = (char *)a1;
v9 = v5 - 1;
v10 = &a1[v7];
while ( *v4 != v8 || strncmp(v4 + 1, a2 + 1, v9) )
{
++v4;
if ( v10 - v4 < v6 )
return 0LL;
}
}
}
return v4;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1, const char *a2, char a3)
{
char *v4; // rbx
size_t v5; // rax
size_t v6; // r12
signed __int64 v7; // rdi
char v8; // r13
size_t v9; // r15
const char *v10; // rbp

v4 = strchr(a1, a3);
if ( v4 )
{
v5 = strlen(a2);
v6 = v5;
v7 = v4 - a1;
if ( v5 > v4 - a1 )
{
v4 = 0LL;
}
else
{
v8 = *a2;
v4 = (char *)a1;
v9 = v5 - 1;
v10 = &a1[v7];
while ( *v4 != v8 || strncmp(v4 + 1, a2 + 1, v9) )
{
++v4;
if ( v10 - v4 < v6 )
return 0LL;
}
}
}
return v4;
}

original_code:
char *
strstrc(const char *s, const char *p, const char t)
{
 char *endptr;
 size_t slen, plen;
 endptr = strchr(s, t);
 if (!endptr)
  return ((void *)0);
 plen = strlen(p);
 slen = endptr - s;
 while (slen >= plen)
 {
  if (*s == *p && strncmp(s+1, p+1, plen-1) == 0)
   return (char*)s;
  s++;
  slen--;
 }
 return ((void *)0);
}

canonical_original_code:
char *strstrc(char *s, char *p, char t)
{
 char *endptr;
 unsigned long slen; unsigned long plen;
 endptr = strchr(s, t);
 if (!endptr)
  return ((void *)0);
 plen = strlen(p);
 slen = endptr - s;
 while (slen >= plen)
 {
  if (*s == *p && strncmp(s+1, p+1, plen-1) == 0)
   return (char *)s;
  s++;
  slen--;
 }
 return ((void *)0);
}

variable_types:
s
char *

variable_types:
p
char *

variable_types: t char

variable_types:
endptr
char *

variable_types:
slen
unsigned long

variable_types:
plen
unsigned long


decompiled_code:
__int64 __fastcall make_dir(char *path, __mode_t mode)
{
char *i; // rbx
char v3; // bp
char v4; // al
__int64 result; // rax
struct stat64 v6; // [rsp+0h] [rbp-B8h] BYREF

for ( i = path; ; *i = v3 )
{
v4 = *i;
if ( !*i )
break;
while ( v4 != 47 )
{
v4 = *++i;
if ( !*i )
goto LABEL_9;
}
do
v3 = *++i;
while ( *i == 47 );
*i = 0;
if ( mkdir(path, mode) < 0 )
goto LABEL_11;
LABEL_4:
if ( !v3 )
return 0LL;
}
LABEL_9:
if ( mkdir(path, mode) >= 0 )
return 0LL;
v3 = 0;
LABEL_11:
if ( *_errno_location() == 17 && stat64(path, &v6) >= 0 && (v6.st_mode & 0xF000) == 0x4000 )
goto LABEL_4;
log_err(3LL, 0LL, "utils.c", 135LL, "make_dir: cannot create directory '%s'\n", path);
result = 0xFFFFFFFFLL;
if ( v3 )
*i = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *path, __mode_t mode)
{
char *i; // rbx
char v3; // bp
char v4; // al
__int64 result; // rax
struct stat64 v6; // [rsp+0h] [rbp-B8h] BYREF

for ( i = path; ; *i = v3 )
{
v4 = *i;
if ( !*i )
break;
while ( v4 != 47 )
{
v4 = *++i;
if ( !*i )
goto LABEL_9;
}
do
v3 = *++i;
while ( *i == 47 );
*i = 0;
if ( mkdir(path, mode) < 0 )
goto LABEL_11;
LABEL_4:
if ( !v3 )
return 0LL;
}
LABEL_9:
if ( mkdir(path, mode) >= 0 )
return 0LL;
v3 = 0;
LABEL_11:
if ( *_errno_location() == 17 && stat64(path, &v6) >= 0 && (v6.st_mode & 0xF000) == 0x4000 )
goto LABEL_4;
log_err(3LL, 0LL, "utils.c", 135LL, "make_dir: cannot create directory '%s'\n", path);
result = 0xFFFFFFFFLL;
if ( v3 )
*i = v3;
return result;
}

original_code:
int
make_dir(char * path, mode_t mode)
{
 char * s = path;
 char c;
 struct stat st;
 do {
  c = '\0';
  while (*s) {
   if (*s == '/') {
    do {
     ++s;
    } while (*s == '/');
    c = *s;
    *s = '\0';
    break;
   }
   ++s;
  }
  if (mkdir(path, mode) < 0) {
   if ((*__errno_location ()) != 17 || (stat(path, &st) < 0 || !((((st.st_mode)) & 0170000) == (0040000)))) {
    { log_err(3, L_GENERAL, "utils.c", 135, "make_dir: cannot create directory '%s'\n", path); };
    if (c)
     *s = c;
    return -1;
   }
  }
         if (!c)
   return 0;
  *s = c;
 } while (1);
}

canonical_original_code:
int make_dir(char *path, unsigned int mode)
{
 char *s = path;
 char c;
 struct stat st;
 do {
  c = '\0';
  while (*s) {
   if (*s == '/') {
    do {
     ++s;
    } while (*s == '/');
    c = *s;
    *s = '\0';
    break;
   }
   ++s;
  }
  if (mkdir(path, mode) < 0) {
   if ((*__errno_location ()) != 17 || (stat(path, &st) < 0 || !((((st.st_mode)) & 0170000) == (0040000)))) {
    { log_err(3, L_GENERAL, "utils.c", 135, "make_dir: cannot create directory '%s'\n", path); };
    if (c)
     *s = c;
    return -1;
   }
  }
         if (!c)
   return 0;
  *s = c;
 } while (1);
}

variable_types:
path
char *

variable_types:
mode
unsigned int

variable_types: c char

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

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:
void log_err(int a1, int a2, const char *a3, unsigned int a4, const char *a5, ...)
{
int v7; // eax
struct tm *v8; // rax
time_t timer; // [rsp+8h] [rbp-F0h] BYREF
gcc_va_list arg; // [rsp+10h] [rbp-E8h] BYREF
char *ptr; // [rsp+28h] [rbp-D0h] BYREF

if ( !a1 )
goto LABEL_5;
v7 = 1;
if ( log_level[a2] > 0 )
v7 = log_level[a2];
if ( a1 <= v7 )
{
LABEL_5:
if ( !log_fp )
log_fp = stdout;
va_start(arg, a5);
if ( vasprintf(&ptr, a5, arg) != -1 )
{
timer = time(0LL);
v8 = localtime(&timer);
fprintf(
log_fp,
"[%04d/%02d/%02d %02d:%02d:%02d] ",
(unsigned int)(v8->tm_year + 1900),
(unsigned int)(v8->tm_mon + 1),
(unsigned int)v8->tm_mday,
(unsigned int)v8->tm_hour,
(unsigned int)v8->tm_min,
(unsigned int)v8->tm_sec);
if ( a1 )
{
fprintf(log_fp, "%s:%d: %s: %s", a3, a4, level_name[a1], ptr);
fflush(log_fp);
free(ptr);
if ( a1 == 1 )
exit(-1);
}
else
{
fprintf(log_fp, "%s:%d: %s", a3, a4, ptr);
fflush(log_fp);
free(ptr);
}
}
}
}

canonical_decompiled_code:
void func1(int a1, int a2, const char *a3, unsigned int a4, const char *a5, ...)
{
int v7; // eax
struct tm *v8; // rax
time_t timer; // [rsp+8h] [rbp-F0h] BYREF
gcc_va_list arg; // [rsp+10h] [rbp-E8h] BYREF
char *ptr; // [rsp+28h] [rbp-D0h] BYREF

if ( !a1 )
goto LABEL_5;
v7 = 1;
if ( log_level[a2] > 0 )
v7 = log_level[a2];
if ( a1 <= v7 )
{
LABEL_5:
if ( !log_fp )
log_fp = stdout;
va_start(arg, a5);
if ( vasprintf(&ptr, a5, arg) != -1 )
{
timer = time(0LL);
v8 = localtime(&timer);
fprintf(
log_fp,
"[%04d/%02d/%02d %02d:%02d:%02d] ",
(unsigned int)(v8->tm_year + 1900),
(unsigned int)(v8->tm_mon + 1),
(unsigned int)v8->tm_mday,
(unsigned int)v8->tm_hour,
(unsigned int)v8->tm_min,
(unsigned int)v8->tm_sec);
if ( a1 )
{
fprintf(log_fp, "%s:%d: %s: %s", a3, a4, level_name[a1], ptr);
fflush(log_fp);
free(ptr);
if ( a1 == 1 )
exit(-1);
}
else
{
fprintf(log_fp, "%s:%d: %s", a3, a4, ptr);
fflush(log_fp);
free(ptr);
}
}
}
}

original_code:
void
log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
{
 char * errbuf;
 va_list ap;
 time_t t;
 struct tm *tm;
 if (level && level>log_level[facility] && level>1)
  return;
 if (!log_fp)
  log_fp = stdout;
 __builtin_va_start(ap,fmt);
 if (vasprintf(&errbuf, fmt, ap) == -1)
 {
  __builtin_va_end(ap);
  return;
 }
 __builtin_va_end(ap);
 t = time(((void *)0));
 tm = localtime(&t);
 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
         tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
         tm->tm_hour, tm->tm_min, tm->tm_sec);
 if (level)
  fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
 else
  fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
 fflush(log_fp);
 free(errbuf);
 if (level==1)
  exit(-1);
 return;
}

canonical_original_code:
void log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
{
 char *errbuf;
 __builtin_va_list ap;
 long t;
 struct tm *tm;
 if (level && level>log_level[facility] && level>1)
  return;
 if (!log_fp)
  log_fp = stdout;
 __builtin_va_start(ap,fmt);
 if (vasprintf(&errbuf, fmt, ap) == -1)
 {
  __builtin_va_end(ap);
  return;
 }
 __builtin_va_end(ap);
 t = time(((void *)0));
 tm = localtime(&t);
 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
         tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
         tm->tm_hour, tm->tm_min, tm->tm_sec);
 if (level)
  fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
 else
  fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
 fflush(log_fp);
 free(errbuf);
 if (level==1)
  exit(-1);
 return;
}

variable_types: level int

variable_types: enum _log_facility { L_GENERAL=0, L_INOTIFY=1, L_HTTP=2, L_SSDP=3, L_MAX=4 }

variable_types:
fname
char *

variable_types: lineno int

variable_types:
fmt
char *

variable_types:
errbuf
char *

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


repo: zydcom/dnfs

decompiled_code:
bool_t __fastcall xdr_chunk_info(XDR *a1, void *a2)
{
bool_t result; // eax

result = xdr_uint32_t(a1, (uint32_t *)a2);
if ( result )
result = xdr_uint32_t(a1, (uint32_t *)a2 + 1) != 0;
return result;
}

canonical_decompiled_code:
bool_t __fastcall func0(XDR *a1, void *a2)
{
bool_t result; // eax

result = xdr_uint32_t(a1, (uint32_t *)a2);
if ( result )
result = xdr_uint32_t(a1, (uint32_t *)a2 + 1) != 0;
return result;
}

original_code:
bool_t
xdr_chunk_info (XDR *xdrs, chunk_info *objp)
{
 register int32_t *buf;
  if (!xdr_uint32_t (xdrs, &objp->ip))
   return (0);
  if (!xdr_uint32_t (xdrs, &objp->bip))
   return (0);
 return (1);
}

canonical_original_code:
int xdr_chunk_info(struct XDR *xdrs, struct chunk_info *objp)
{
 register int *buf;
  if (!xdr_uint32_t (xdrs, &objp->ip))
   return (0);
  if (!xdr_uint32_t (xdrs, &objp->bip))
   return (0);
 return (1);
}

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; } *

variable_types: struct chunk_info { unsigned int ip; unsigned int bip; } *

variable_types:
buf
int *

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }