original_code:
void
sort_interfaces(void)
{
 struct if_head sorted;
 struct interface *ifp, *ift;
 if (ifaces == ((void *)0) ||
     (ifp = ((ifaces)->tqh_first)) == ((void *)0) ||
     ((ifp)->next.tqe_next) == ((void *)0))
  return;
 do { (&sorted)->tqh_first = ((void *)0); (&sorted)->tqh_last = &(&sorted)->tqh_first; } while ( 0);
 do { if (((ifp)->next.tqe_next) != ((void *)0)) (ifp)->next.tqe_next->next.tqe_prev = (ifp)->next.tqe_prev; else (ifaces)->tqh_last = (ifp)->next.tqe_prev; *(ifp)->next.tqe_prev = (ifp)->next.tqe_next; } while ( 0);
 do { if (((ifp)->next.tqe_next = (&sorted)->tqh_first) != ((void *)0)) (&sorted)->tqh_first->next.tqe_prev = &(ifp)->next.tqe_next; else (&sorted)->tqh_last = &(ifp)->next.tqe_next; (&sorted)->tqh_first = (ifp); (ifp)->next.tqe_prev = &(&sorted)->tqh_first; } while ( 0);
 while ((ifp = ((ifaces)->tqh_first))) {
  do { if (((ifp)->next.tqe_next) != ((void *)0)) (ifp)->next.tqe_next->next.tqe_prev = (ifp)->next.tqe_prev; else (ifaces)->tqh_last = (ifp)->next.tqe_prev; *(ifp)->next.tqe_prev = (ifp)->next.tqe_next; } while ( 0);
  for ((ift) = ((&sorted)->tqh_first); (ift); (ift) = ((ift)->next.tqe_next)) {
   if (ifcmp(ifp, ift) == -1) {
    do { (ifp)->next.tqe_prev = (ift)->next.tqe_prev; (ifp)->next.tqe_next = (ift); *(ift)->next.tqe_prev = (ifp); (ift)->next.tqe_prev = &(ifp)->next.tqe_next; } while ( 0);
    break;
   }
  }
  if (ift == ((void *)0))
   do { (ifp)->next.tqe_next = ((void *)0); (ifp)->next.tqe_prev = (&sorted)->tqh_last; *(&sorted)->tqh_last = (ifp); (&sorted)->tqh_last = &(ifp)->next.tqe_next; } while ( 0);
 }
 do { if (!((&sorted)->tqh_first == ((void *)0))) { *(ifaces)->tqh_last = (&sorted)->tqh_first; (&sorted)->tqh_first->next.tqe_prev = (ifaces)->tqh_last; (ifaces)->tqh_last = (&sorted)->tqh_last; do { ((&sorted))->tqh_first = ((void *)0); ((&sorted))->tqh_last = &((&sorted))->tqh_first; } while ( 0); } } while ( 0);
}

canonical_original_code:
void sort_interfaces()
{
 struct if_head sorted;
 struct interface *ifp; struct interface *ift;
 if (ifaces == ((void *)0) ||
     (ifp = ((ifaces)->tqh_first)) == ((void *)0) ||
     ((ifp)->next.tqe_next) == ((void *)0))
  return;
 do { (&sorted)->tqh_first = ((void *)0); (&sorted)->tqh_last = &(&sorted)->tqh_first; } while ( 0);
 do { if (((ifp)->next.tqe_next) != ((void *)0)) (ifp)->next.tqe_next->next.tqe_prev = (ifp)->next.tqe_prev; else (ifaces)->tqh_last = (ifp)->next.tqe_prev; *(ifp)->next.tqe_prev = (ifp)->next.tqe_next; } while ( 0);
 do { if (((ifp)->next.tqe_next = (&sorted)->tqh_first) != ((void *)0)) (&sorted)->tqh_first->next.tqe_prev = &(ifp)->next.tqe_next; else (&sorted)->tqh_last = &(ifp)->next.tqe_next; (&sorted)->tqh_first = (ifp); (ifp)->next.tqe_prev = &(&sorted)->tqh_first; } while ( 0);
 while ((ifp = ((ifaces)->tqh_first))) {
  do { if (((ifp)->next.tqe_next) != ((void *)0)) (ifp)->next.tqe_next->next.tqe_prev = (ifp)->next.tqe_prev; else (ifaces)->tqh_last = (ifp)->next.tqe_prev; *(ifp)->next.tqe_prev = (ifp)->next.tqe_next; } while ( 0);
  for ((ift) = ((&sorted)->tqh_first); (ift); (ift) = ((ift)->next.tqe_next)) {
   if (ifcmp(ifp, ift) == -1) {
    do { (ifp)->next.tqe_prev = (ift)->next.tqe_prev; (ifp)->next.tqe_next = (ift); *(ift)->next.tqe_prev = (ifp); (ift)->next.tqe_prev = &(ifp)->next.tqe_next; } while ( 0);
    break;
   }
  }
  if (ift == ((void *)0))
   do { (ifp)->next.tqe_next = ((void *)0); (ifp)->next.tqe_prev = (&sorted)->tqh_last; *(&sorted)->tqh_last = (ifp); (&sorted)->tqh_last = &(ifp)->next.tqe_next; } while ( 0);
 }
 do { if (!((&sorted)->tqh_first == ((void *)0))) { *(ifaces)->tqh_last = (&sorted)->tqh_first; (&sorted)->tqh_first->next.tqe_prev = (ifaces)->tqh_last; (ifaces)->tqh_last = (&sorted)->tqh_last; do { ((&sorted))->tqh_first = ((void *)0); ((&sorted))->tqh_last = &((&sorted))->tqh_first; } while ( 0); } } while ( 0);
}

variable_types: struct if_head { struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr { unsigned int s_addr; } req_addr; struct in_addr req_mask; struct rt_head { struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface *iface; int metric; struct in_addr src; } *tqh_first; struct rt **tqh_last; } *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; } *sla; } *iaid; int dadtransmits; } *options; void *if_data[4]; } *tqh_first; struct interface **tqh_last; }


repo: sklvjz/network

decompiled_code:
__int64 __fastcall arp_failure(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax
__int64 v3; // rdx
int v4; // eax
__int64 v5; // rdx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 192);
if ( *(_QWORD *)(v1 + 16) )
{
v2 = _bswap_32(0x63825363u);
if ( *(_DWORD *)(v3 + 236) != v2 )
return ipv4ll_handle_failure(a1);
}
if ( *(_QWORD *)(v1 + 24) )
{
v4 = _bswap_32(0x63825363u);
if ( *(_DWORD *)(v5 + 236) != v4 )
return ipv4ll_handle_failure(a1);
}
unlink((const char *)(v1 + 236));
if ( !*(_BYTE *)(v1 + 96) )
dhcp_decline(a1);
dhcp_close(a1);
eloop_q_timeout_delete(1LL, 0LL, a1);
if ( *(_BYTE *)(v1 + 96) )
result = start_interface(a1);
else
result = eloop_q_timeout_add_sec(1LL, 2LL, &start_interface, a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax
__int64 v3; // rdx
int v4; // eax
__int64 v5; // rdx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 192);
if ( *(_QWORD *)(v1 + 16) )
{
v2 = func1(0x63825363u);
if ( *(_DWORD *)(v3 + 236) != v2 )
return ipv4ll_handle_failure(a1);
}
if ( *(_QWORD *)(v1 + 24) )
{
v4 = func1(0x63825363u);
if ( *(_DWORD *)(v5 + 236) != v4 )
return ipv4ll_handle_failure(a1);
}
unlink((const char *)(v1 + 236));
if ( !*(_BYTE *)(v1 + 96) )
dhcp_decline(a1);
dhcp_close(a1);
eloop_q_timeout_delete(1LL, 0LL, a1);
if ( *(_BYTE *)(v1 + 96) )
result = start_interface(a1);
else
result = eloop_q_timeout_add_sec(1LL, 2LL, &start_interface, a1);
return result;
}

original_code:
static void
arp_failure(struct interface *ifp)
{
 const struct dhcp_state *state = ((const struct dhcp_state *)(ifp)->if_data[0]);
 if ((state->offer != ((void *)0) &&
  state->offer->cookie != htonl(0x63825363)) ||
     (state->new != ((void *)0) &&
  state->new->cookie != htonl(0x63825363)))
 {
  ipv4ll_handle_failure(ifp);
  return;
 }
 unlink(state->leasefile);
 if (!state->lease.frominfo)
  dhcp_decline(ifp);
 dhcp_close(ifp);
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 if (state->lease.frominfo)
  start_interface(ifp);
 else
  eloop_q_timeout_add_sec(1, 2, start_interface, ifp);
}

canonical_original_code:
static void arp_failure(struct interface *ifp)
{
 struct dhcp_state *state = ((const struct dhcp_state *)(ifp)->if_data[0]);
 if ((state->offer != ((void *)0) &&
  state->offer->cookie != htonl(0x63825363)) ||
     (state->new != ((void *)0) &&
  state->new->cookie != htonl(0x63825363)))
 {
  ipv4ll_handle_failure(ifp);
  return;
 }
 unlink(state->leasefile);
 if (!state->lease.frominfo)
  dhcp_decline(ifp);
 dhcp_close(ifp);
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 if (state->lease.frominfo)
  start_interface(ifp);
 else
  eloop_q_timeout_add_sec(1, 2, start_interface, ifp);
}

variable_types: struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr { unsigned int s_addr; } req_addr; struct in_addr req_mask; struct rt_head { struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface *iface; int metric; struct in_addr src; } *tqh_first; struct rt **tqh_last; } *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; } *sla; } *iaid; int dadtransmits; } *options; void *if_data[4]; } *

variable_types: struct dhcp_state { enum DHS { DHS_INIT=0, DHS_DISCOVER=1, DHS_REQUEST=2, DHS_BOUND=3, DHS_RENEW=4, DHS_REBIND=5, DHS_REBOOT=6, DHS_INFORM=7, DHS_RENEW_REQUESTED=8, DHS_INIT_IPV4LL=9, DHS_PROBE=10 } state; struct dhcp_message { unsigned char op; unsigned char hwtype; unsigned char hwlen; unsigned char hwopcount; unsigned int xid; unsigned short secs; unsigned short flags; unsigned int ciaddr; unsigned int yiaddr; unsigned int siaddr; unsigned int giaddr; unsigned char chaddr[16]; unsigned char servername[64]; unsigned char bootfile[128]; unsigned int cookie; unsigned char options[-1]; } *sent; struct dhcp_message *offer; struct dhcp_message *new; struct dhcp_message *old; struct dhcp_lease { struct in_addr { unsigned int s_addr; } addr; struct in_addr net; struct in_addr brd; unsigned int leasetime; unsigned int renewaltime; unsigned int rebindtime; struct in_addr server; long leasedfrom; struct timeval { long tv_sec; long tv_usec; } boundtime; unsigned char frominfo; unsigned int cookie; } lease; char *reason; long interval; long nakoff; unsigned int xid; int socket; int probes; int claims; int conflicts; long defend; struct in_addr fail; unsigned long arping_index; int raw_fd; int udp_fd; int arp_fd; unsigned long buffer_size; unsigned char *buffer; struct in_addr addr; struct in_addr net; struct in_addr dst; char leasefile[4096]; long start_uptime; unsigned char *clientid; } *

return_type: void

user_defined_types: struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options *options; void *if_data[4]; }

user_defined_types: struct dhcp_state { enum DHS state; struct dhcp_message *sent; struct dhcp_message *offer; struct dhcp_message *new; struct dhcp_message *old; struct dhcp_lease lease; char *reason; long interval; long nakoff; unsigned int xid; int socket; int probes; int claims; int conflicts; long defend; struct in_addr fail; unsigned long arping_index; int raw_fd; int udp_fd; int arp_fd; unsigned long buffer_size; unsigned char *buffer; struct in_addr addr; struct in_addr net; struct in_addr dst; char leasefile[4096]; long start_uptime; unsigned char *clientid; }

user_defined_types: struct dhcp_lease { struct in_addr addr; struct in_addr net; struct in_addr brd; unsigned int leasetime; unsigned int renewaltime; unsigned int rebindtime; struct in_addr server; long leasedfrom; struct timeval boundtime; unsigned char frominfo; unsigned int cookie; }

user_defined_types: struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr req_addr; struct in_addr req_mask; struct rt_head *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid *iaid; int dadtransmits; }

user_defined_types: struct rt_head { struct rt *tqh_first; struct rt **tqh_last; }

user_defined_types: struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla *sla; }


decompiled_code:
__int64 __fastcall arp_packet(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // r13
unsigned __int64 v3; // rdx
__int64 result; // rax
unsigned __int64 v5; // rcx
__int16 v6; // r8
unsigned __int64 v7; // rsi
signed __int64 v8; // rdx
unsigned __int64 v9; // rcx
unsigned __int64 v10; // rcx
__int64 v11; // r14
__int64 v12; // rax
char *v13; // r15
unsigned __int64 v14; // rax
int v15; // ecx
int v16; // esi
struct in_addr v17; // er15
__int64 v18; // rax
int v19; // eax
__int64 v20; // rax
int v21; // edx
int v22; // ecx
char *v23; // rbx
const char *v24; // rax
const char *v25; // r12
char *v26; // rax
char *v27; // rax
__int64 v28; // [rsp+0h] [rbp-78h] BYREF
int s1[28]; // [rsp+8h] [rbp-70h] BYREF

v1 = *(_QWORD *)(a1 + 192);
v2 = *(_QWORD *)(a1 + 184);
*(_DWORD *)(v1 + 160) = 0;
while ( 1 )
{
v3 = ipv4_getrawpacket(a1, 2054LL, &v28, 56LL, 0LL);
result = v3 + 1;
if ( v3 == -1LL || v3 == 0 )
return result;
if ( v3 > 7 )
{
v6 = _bswap_16(2048);
if ( v6 == WORD1(v5) && BYTE5(v5) == 4 )
{
v7 = HIWORD(v5);
if ( _bswap_16(2) == HIWORD(v5) || _bswap_16(1) == (_WORD)v7 )
{
v10 = HIDWORD(v9);
v11 = (unsigned __int8)v10;
v12 = (unsigned __int8)v10 + 4LL;
v13 = (char *)s1 + v12;
if ( (char *)s1 + v12 + v12 - (char *)&v28 <= v8
&& ((unsigned __int8)v10 != *(_QWORD *)(a1 + 64) || memcmp(
s1,
(const void *)(a1 + 42),
(unsigned __int8)v10)) )
{
v14 = *(_QWORD *)(v1 + 168);
v15 = *(int *)((char *)s1 + v11);
v16 = *(_DWORD *)&v13[v11];
if ( v14 && v14 <= *(_QWORD *)(v2 + 29976) )
{
v17.s_addr = *(_DWORD *)(*(_QWORD *)(v2 + 29984) + 4 * v14 - 4);
if ( v15 == v17.s_addr )
goto LABEL_32;
if ( v16 == v17.s_addr && !v15 )
{
v17.s_addr = 0;
LABEL_32:
v25 = (const char *)hwaddr_ntoa(s1, v11);
v26 = inet_ntoa(v17);
syslog(6, "%s: found %s on hardware address %s", (const char *)(a1 + 16), v26, v25);
if ( (unsigned int)select_profile(a1, v25) == -1 && *_errno_location() == 2 )
{
v27 = inet_ntoa(v17);
select_profile(a1, v27);
}
dhcp_close(a1);
eloop_q_timeout_delete(1LL, 0LL, a1);
return start_interface(a1);
}
}
v18 = *(_QWORD *)(v1 + 16);
if ( v18 )
{
v19 = *(_DWORD *)(v18 + 16);
if ( v15 == v19 || !v15 && v16 == v19 )
*(_DWORD *)(v1 + 160) = v19;
}
v20 = _bswap_32(*(_DWORD *)(v1 + 224));
LOWORD(v20) = 0;
if ( (_DWORD)v20 == -1442971648 && (v22 == v21 || v16 == v21 && !v22) )
*(_DWORD *)(v1 + 160) = v21;
else
v21 = *(_DWORD *)(v1 + 160);
if ( v21 )
{
v23 = inet_ntoa(*(struct in_addr *)(v1 + 160));
v24 = (const char *)hwaddr_ntoa(s1, v11);
syslog(3, "%s: hardware address %s claims %s", (const char *)(a1 + 16), v24, v23);
*_errno_location() = 17;
return arp_failure(a1);
}
}
}
}
}
}
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // r13
unsigned __int64 v3; // rdx
__int64 result; // rax
unsigned __int64 v5; // rcx
__int16 v6; // r8
unsigned __int64 v7; // rsi
signed __int64 v8; // rdx
unsigned __int64 v9; // rcx
unsigned __int64 v10; // rcx
__int64 v11; // r14
__int64 v12; // rax
char *v13; // r15
unsigned __int64 v14; // rax
int v15; // ecx
int v16; // esi
struct in_addr v17; // er15
__int64 v18; // rax
int v19; // eax
__int64 v20; // rax
int v21; // edx
int v22; // ecx
char *v23; // rbx
const char *v24; // rax
const char *v25; // r12
char *v26; // rax
char *v27; // rax
__int64 v28; // [rsp+0h] [rbp-78h] BYREF
int s1[28]; // [rsp+8h] [rbp-70h] BYREF

v1 = *(_QWORD *)(a1 + 192);
v2 = *(_QWORD *)(a1 + 184);
*(_DWORD *)(v1 + 160) = 0;
while ( 1 )
{
v3 = ipv4_getrawpacket(a1, 2054LL, &v28, 56LL, 0LL);
result = v3 + 1;
if ( v3 == -1LL || v3 == 0 )
return result;
if ( v3 > 7 )
{
v6 = func0(2048);
if ( v6 == WORD1(v5) && BYTE5(v5) == 4 )
{
v7 = HIWORD(v5);
if ( func0(2) == HIWORD(v5) || func0(1) == (_WORD)v7 )
{
v10 = HIDWORD(v9);
v11 = (unsigned __int8)v10;
v12 = (unsigned __int8)v10 + 4LL;
v13 = (char *)s1 + v12;
if ( (char *)s1 + v12 + v12 - (char *)&v28 <= v8
&& ((unsigned __int8)v10 != *(_QWORD *)(a1 + 64) || memcmp(
s1,
(const void *)(a1 + 42),
(unsigned __int8)v10)) )
{
v14 = *(_QWORD *)(v1 + 168);
v15 = *(int *)((char *)s1 + v11);
v16 = *(_DWORD *)&v13[v11];
if ( v14 && v14 <= *(_QWORD *)(v2 + 29976) )
{
v17.s_addr = *(_DWORD *)(*(_QWORD *)(v2 + 29984) + 4 * v14 - 4);
if ( v15 == v17.s_addr )
goto LABEL_32;
if ( v16 == v17.s_addr && !v15 )
{
v17.s_addr = 0;
LABEL_32:
v25 = (const char *)hwaddr_ntoa(s1, v11);
v26 = inet_ntoa(v17);
syslog(6, "%s: found %s on hardware address %s", (const char *)(a1 + 16), v26, v25);
if ( (unsigned int)select_profile(a1, v25) == -1 && *_errno_location() == 2 )
{
v27 = inet_ntoa(v17);
select_profile(a1, v27);
}
dhcp_close(a1);
eloop_q_timeout_delete(1LL, 0LL, a1);
return start_interface(a1);
}
}
v18 = *(_QWORD *)(v1 + 16);
if ( v18 )
{
v19 = *(_DWORD *)(v18 + 16);
if ( v15 == v19 || !v15 && v16 == v19 )
*(_DWORD *)(v1 + 160) = v19;
}
v20 = func1(*(_DWORD *)(v1 + 224));
LOWORD(v20) = 0;
if ( (_DWORD)v20 == -1442971648 && (v22 == v21 || v16 == v21 && !v22) )
*(_DWORD *)(v1 + 160) = v21;
else
v21 = *(_DWORD *)(v1 + 160);
if ( v21 )
{
v23 = inet_ntoa(*(struct in_addr *)(v1 + 160));
v24 = (const char *)hwaddr_ntoa(s1, v11);
syslog(3, "%s: hardware address %s claims %s", (const char *)(a1 + 16), v24, v23);
*_errno_location() = 17;
return func2(a1);
}
}
}
}
}
}
}

original_code:
static void
arp_packet(void *arg)
{
 struct interface *ifp = arg;
 uint8_t arp_buffer[(sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * 20))];
 struct arphdr ar;
 uint32_t reply_s;
 uint32_t reply_t;
 uint8_t *hw_s, *hw_t;
 ssize_t bytes;
 struct dhcp_state *state;
 struct if_options *opts = ifp->options;
 const char *hwaddr;
 struct in_addr ina;
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 state->fail.s_addr = 0;
 for(;;) {
  bytes = ipv4_getrawpacket(ifp, 0x0806,
      arp_buffer, sizeof(arp_buffer), ((void *)0));
  if (bytes == 0 || bytes == -1)
   return;
  if ((size_t)bytes < sizeof(ar))
   continue;
  memcpy(&ar, arp_buffer, sizeof(ar));
  if (ar.ar_pro != htons(0x0800))
   continue;
  if (ar.ar_pln != sizeof(reply_s))
   continue;
  if (ar.ar_op != htons(2) &&
      ar.ar_op != htons(1))
   continue;
  hw_s = arp_buffer + sizeof(ar);
  hw_t = hw_s + ar.ar_hln + ar.ar_pln;
  if ((hw_t + ar.ar_hln + ar.ar_pln) - arp_buffer > bytes)
   continue;
  if (ar.ar_hln == ifp->hwlen &&
      memcmp(hw_s, ifp->hwaddr, ifp->hwlen) == 0)
   continue;
  memcpy(&reply_s, hw_s + ar.ar_hln, ar.ar_pln);
  memcpy(&reply_t, hw_t + ar.ar_hln, ar.ar_pln);
  if (state->arping_index &&
      state->arping_index <= opts->arping_len &&
      (reply_s == opts->arping[state->arping_index - 1] ||
   (reply_s == 0 &&
       reply_t == opts->arping[state->arping_index - 1])))
  {
   ina.s_addr = reply_s;
   hwaddr = hwaddr_ntoa((unsigned char *)hw_s,
       (size_t)ar.ar_hln);
   syslog(6,
       "%s: found %s on hardware address %s",
       ifp->name, inet_ntoa(ina), hwaddr);
   if (select_profile(ifp, hwaddr) == -1 &&
       (*__errno_location ()) == 2)
    select_profile(ifp, inet_ntoa(ina));
   dhcp_close(ifp);
   eloop_q_timeout_delete(1, ((void *)0), ifp);
   start_interface(ifp);
   return;
  }
  if (state->offer &&
      (reply_s == state->offer->yiaddr ||
   (reply_s == 0 && reply_t == state->offer->yiaddr)))
   state->fail.s_addr = state->offer->yiaddr;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000) &&
      (reply_s == state->addr.s_addr ||
   (reply_s == 0 && reply_t == state->addr.s_addr)))
   state->fail.s_addr = state->addr.s_addr;
  if (state->fail.s_addr) {
   syslog(3, "%s: hardware address %s claims %s",
       ifp->name,
       hwaddr_ntoa((unsigned char *)hw_s,
    (size_t)ar.ar_hln),
       inet_ntoa(state->fail));
   (*__errno_location ()) = 17;
   arp_failure(ifp);
   return;
  }
 }
}

canonical_original_code:
static void arp_packet(void *arg)
{
 struct interface *ifp = arg;
 unsigned char arp_buffer[-1];
 struct arphdr ar;
 unsigned int reply_s;
 unsigned int reply_t;
 unsigned char *hw_s; unsigned char *hw_t;
 long bytes;
 struct dhcp_state *state;
 struct if_options *opts = ifp->options;
 char *hwaddr;
 struct in_addr ina;
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 state->fail.s_addr = 0;
 for(;;) {
  bytes = ipv4_getrawpacket(ifp, 0x0806,
      arp_buffer, sizeof(arp_buffer), ((void *)0));
  if (bytes == 0 || bytes == -1)
   return;
  if ((unsigned long)bytes < sizeof(ar))
   continue;
  memcpy(&ar, arp_buffer, sizeof(ar));
  if (ar.ar_pro != htons(0x0800))
   continue;
  if (ar.ar_pln != sizeof(reply_s))
   continue;
  if (ar.ar_op != htons(2) &&
      ar.ar_op != htons(1))
   continue;
  hw_s = arp_buffer + sizeof(ar);
  hw_t = hw_s + ar.ar_hln + ar.ar_pln;
  if ((hw_t + ar.ar_hln + ar.ar_pln) - arp_buffer > bytes)
   continue;
  if (ar.ar_hln == ifp->hwlen &&
      memcmp(hw_s, ifp->hwaddr, ifp->hwlen) == 0)
   continue;
  memcpy(&reply_s, hw_s + ar.ar_hln, ar.ar_pln);
  memcpy(&reply_t, hw_t + ar.ar_hln, ar.ar_pln);
  if (state->arping_index &&
      state->arping_index <= opts->arping_len &&
      (reply_s == opts->arping[state->arping_index - 1] ||
   (reply_s == 0 &&
       reply_t == opts->arping[state->arping_index - 1])))
  {
   ina.s_addr = reply_s;
   hwaddr = hwaddr_ntoa((unsigned char *)hw_s,
       (unsigned long)ar.ar_hln);
   syslog(6,
       "%s: found %s on hardware address %s",
       ifp->name, inet_ntoa(ina), hwaddr);
   if (select_profile(ifp, hwaddr) == -1 &&
       (*__errno_location ()) == 2)
    select_profile(ifp, inet_ntoa(ina));
   dhcp_close(ifp);
   eloop_q_timeout_delete(1, ((void *)0), ifp);
   start_interface(ifp);
   return;
  }
  if (state->offer &&
      (reply_s == state->offer->yiaddr ||
   (reply_s == 0 && reply_t == state->offer->yiaddr)))
   state->fail.s_addr = state->offer->yiaddr;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000) &&
      (reply_s == state->addr.s_addr ||
   (reply_s == 0 && reply_t == state->addr.s_addr)))
   state->fail.s_addr = state->addr.s_addr;
  if (state->fail.s_addr) {
   syslog(3, "%s: hardware address %s claims %s",
       ifp->name,
       hwaddr_ntoa((unsigned char *)hw_s,
    (unsigned long)ar.ar_hln),
       inet_ntoa(state->fail));
   (*__errno_location ()) = 17;
   arp_failure(ifp);
   return;
  }
 }
}

variable_types:
arg
void *

variable_types: struct arphdr { unsigned short ar_hrd; unsigned short ar_pro; unsigned char ar_hln; unsigned char ar_pln; unsigned short ar_op; }

variable_types: bytes long

variable_types: struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr { unsigned int s_addr; } req_addr; struct in_addr req_mask; struct rt_head { struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options *options; void *if_data[4]; } *iface; int metric; struct in_addr src; } *tqh_first; struct rt **tqh_last; } *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; } *sla; } *iaid; int dadtransmits; } *

variable_types:
hwaddr
char *

variable_types:
ina
struct in_addr { unsigned int s_addr; }

user_defined_types: enum DHS { DHS_INIT=0, DHS_DISCOVER=1, DHS_REQUEST=2, DHS_BOUND=3, DHS_RENEW=4, DHS_REBIND=5, DHS_REBOOT=6, DHS_INFORM=7, DHS_RENEW_REQUESTED=8, DHS_INIT_IPV4LL=9, DHS_PROBE=10 }

user_defined_types: struct dhcp_message { unsigned char op; unsigned char hwtype; unsigned char hwlen; unsigned char hwopcount; unsigned int xid; unsigned short secs; unsigned short flags; unsigned int ciaddr; unsigned int yiaddr; unsigned int siaddr; unsigned int giaddr; unsigned char chaddr[16]; unsigned char servername[64]; unsigned char bootfile[128]; unsigned int cookie; unsigned char options[-1]; }

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

user_defined_types: struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface *iface; int metric; struct in_addr src; }


decompiled_code:
void __fastcall arp_announce(__int64 a1)
{
__int64 v1; // rbx
const char *v2; // rdx
__int64 v3; // rcx
__int64 v4; // rax
unsigned int v5; // eax
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h]

v1 = *(_QWORD *)(a1 + 192);
if ( !*(_QWORD *)(v1 + 24) )
return;
if ( *(_DWORD *)(v1 + 184) == -1 )
{
ipv4_opensocket(a1, 2054LL);
eloop_event_add(*(unsigned int *)(v1 + 184), arp_packet, a1);
}
v2 = (const char *)(a1 + 16);
v3 = (unsigned int)(*(_DWORD *)(v1 + 140) + 1);
*(_DWORD *)(v1 + 140) = v3;
if ( (int)v3 <= 1 )
syslog(7, "%s: sending ARP announce (%d of %d), next in %d.00 seconds", v2, v3, 2LL, 2LL);
else
syslog(7, "%s: sending ARP announce (%d of %d)", v2, v3, 2LL);
if ( (unsigned int)arp_send_constprop_0(
a1,
*(_DWORD *)(*(_QWORD *)(v1 + 24) + 16LL),
*(_DWORD *)(*(_QWORD *)(v1 + 24) + 16LL)) != -1 )
{
if ( *(int *)(v1 + 140) > 1 )
goto LABEL_8;
LABEL_17:
eloop_q_timeout_add_sec(1LL, 2LL, arp_announce, a1);
return;
}
syslog(3, "send_arp: %m");
if ( *(int *)(v1 + 140) <= 1 )
goto LABEL_17;
LABEL_8:
if ( *(_DWORD *)(*(_QWORD *)(v1 + 24) + 236LL) == (unsigned int)_bswap_32(0x63825363u) )
{
eloop_event_delete(*(unsigned int *)(v1 + 184));
close(*(_DWORD *)(v1 + 184));
*(_DWORD *)(v1 + 184) = -1;
}
else
{
v4 = 1LL;
if ( *(_QWORD *)(v1 + 112) )
{
*(_QWORD *)(v1 + 112) = 64LL;
v4 = 65LL;
}
*(_QWORD *)(v1 + 136) = 0LL;
v6 = v4;
v5 = (unsigned int)arc4random() % 0x1E8480;
v7 = v5;
if ( v5 > 0xF423F )
{
++v6;
v7 = v5 - 1000000LL;
}
eloop_q_timeout_add_tv(1LL, &v6, &dhcp_discover, a1);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
__int64 v1; // rbx
const char *v2; // rdx
__int64 v3; // rcx
__int64 v4; // rax
unsigned int v5; // eax
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h]

v1 = *(_QWORD *)(a1 + 192);
if ( !*(_QWORD *)(v1 + 24) )
return;
if ( *(_DWORD *)(v1 + 184) == -1 )
{
ipv4_opensocket(a1, 2054LL);
eloop_event_add(*(unsigned int *)(v1 + 184), arp_packet, a1);
}
v2 = (const char *)(a1 + 16);
v3 = (unsigned int)(*(_DWORD *)(v1 + 140) + 1);
*(_DWORD *)(v1 + 140) = v3;
if ( (int)v3 <= 1 )
syslog(7, "%s: sending ARP announce (%d of %d), next in %d.00 seconds", v2, v3, 2LL, 2LL);
else
syslog(7, "%s: sending ARP announce (%d of %d)", v2, v3, 2LL);
if ( (unsigned int)func4(
a1,
*(_DWORD *)(*(_QWORD *)(v1 + 24) + 16LL),
*(_DWORD *)(*(_QWORD *)(v1 + 24) + 16LL)) != -1 )
{
if ( *(int *)(v1 + 140) > 1 )
goto LABEL_8;
LABEL_17:
eloop_q_timeout_add_sec(1LL, 2LL, arp_announce, a1);
return;
}
syslog(3, "send_arp: %m");
if ( *(int *)(v1 + 140) <= 1 )
goto LABEL_17;
LABEL_8:
if ( *(_DWORD *)(*(_QWORD *)(v1 + 24) + 236LL) == (unsigned int)func1(0x63825363u) )
{
eloop_event_delete(*(unsigned int *)(v1 + 184));
close(*(_DWORD *)(v1 + 184));
*(_DWORD *)(v1 + 184) = -1;
}
else
{
v4 = 1LL;
if ( *(_QWORD *)(v1 + 112) )
{
*(_QWORD *)(v1 + 112) = 64LL;
v4 = 65LL;
}
*(_QWORD *)(v1 + 136) = 0LL;
v6 = v4;
v5 = (unsigned int)arc4random() % 0x1E8480;
v7 = v5;
if ( v5 > 0xF423F )
{
++v6;
v7 = v5 - 1000000LL;
}
eloop_q_timeout_add_tv(1LL, &v6, &dhcp_discover, a1);
}
}

original_code:
void
arp_announce(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 struct timeval tv;
 if (state->new == ((void *)0))
  return;
 if (state->arp_fd == -1) {
  ipv4_opensocket(ifp, 0x0806);
  eloop_event_add(state->arp_fd, arp_packet, ifp);
 }
 if (++state->claims < 2)
  syslog(7,
      "%s: sending ARP announce (%d of %d), "
      "next in %d.00 seconds",
      ifp->name, state->claims, 2, 2);
 else
  syslog(7,
      "%s: sending ARP announce (%d of %d)",
      ifp->name, state->claims, 2);
 if (arp_send(ifp, 1,
  state->new->yiaddr, state->new->yiaddr) == -1)
  syslog(3, "send_arp: %m");
 if (state->claims < 2) {
  eloop_q_timeout_add_sec(1, 2, arp_announce, ifp);
  return;
 }
 if (state->new->cookie != htonl(0x63825363)) {
  if (state->interval != 0)
   state->interval = 64;
  state->probes = 0;
  state->claims = 0;
  tv.tv_sec = state->interval - -1;
  tv.tv_usec = arc4random() % (1 * 1000000 - -1 * 1000000);
  do { while ((&tv)->tv_usec >= 1000000) { (&tv)->tv_sec++; (&tv)->tv_usec -= 1000000; } } while (0 );;
  eloop_q_timeout_add_tv(1, &tv, dhcp_discover, ifp);
 } else {
  eloop_event_delete(state->arp_fd);
  close(state->arp_fd);
  state->arp_fd = -1;
 }
}

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

user_defined_types: struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; }


original_code:
void
arp_probe(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 struct in_addr addr;
 struct timeval tv;
 int arping = 0;
 if (state->arp_fd == -1) {
  if (ipv4_opensocket(ifp, 0x0806) == -1)
   return;
  eloop_event_add(state->arp_fd, arp_packet, ifp);
 }
 if (state->arping_index < ifp->options->arping_len) {
  addr.s_addr = ifp->options->arping[state->arping_index];
  arping = 1;
 } else if (state->offer) {
  if (state->offer->yiaddr)
   addr.s_addr = state->offer->yiaddr;
  else
   addr.s_addr = state->offer->ciaddr;
 } else
  addr.s_addr = state->addr.s_addr;
 if (state->probes == 0) {
  if (arping)
   syslog(7, "%s: searching for %s",
       ifp->name, inet_ntoa(addr));
  else
   syslog(7, "%s: checking for %s",
       ifp->name, inet_ntoa(addr));
 }
 if (++state->probes < 3) {
  tv.tv_sec = 1;
  tv.tv_usec = arc4random() % (2 * 1000000 - 1 * 1000000);
  do { while ((&tv)->tv_usec >= 1000000) { (&tv)->tv_sec++; (&tv)->tv_usec -= 1000000; } } while (0 );;
  eloop_q_timeout_add_tv(1, &tv, arp_probe, ifp);
 } else {
  tv.tv_sec = 2;
  tv.tv_usec = 0;
  if (arping) {
   state->probes = 0;
   if (++state->arping_index < ifp->options->arping_len)
    eloop_q_timeout_add_tv(1, &tv, arp_probe, ifp);
   else
    eloop_q_timeout_add_tv(1, &tv, start_interface, ifp);
  } else
   eloop_q_timeout_add_tv(1, &tv, dhcp_bind, ifp);
 }
 syslog(7,
     "%s: sending ARP probe (%d of %d), next in %0.2f seconds",
     ifp->name, state->probes ? state->probes : 3, 3,
     ((&tv)->tv_sec * 1.0 + (&tv)->tv_usec * 1.0e-6));
 if (arp_send(ifp, 1, 0, addr.s_addr) == -1)
  syslog(3, "send_arp: %m");
}

variable_types:
addr
struct in_addr { unsigned int s_addr; }

variable_types: arping int


original_code:
void
arp_start(struct interface *ifp)
{
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 state->probes = 0;
 state->arping_index = 0;
 arp_probe(ifp);
}


decompiled_code:
__int64 __fastcall arp_check(__int64 a1, struct in_addr a2)
{
__int16 v3; // r15
__int16 v4; // ax
__int16 v5; // dx
int v6; // eax
char *v7; // rax
void *v8; // rsp
__int64 v9; // rax
int v10; // edi
int v11; // eax
int v12; // ecx
int v13; // eax
int v14; // edx
char *v15; // r12
char *v16; // rax
__int64 result; // rax
__int64 v18; // [rsp+0h] [rbp-11F0h] BYREF
size_t n; // [rsp+8h] [rbp-11E8h]
int v20; // [rsp+10h] [rbp-11E0h]
__int16 v21; // [rsp+16h] [rbp-11DAh]
__int64 v22; // [rsp+18h] [rbp-11D8h]
int v23; // [rsp+28h] [rbp-11C8h] BYREF
int v24; // [rsp+2Ch] [rbp-11C4h] BYREF
struct timeval timeout; // [rsp+30h] [rbp-11C0h] BYREF
fd_set readfds; // [rsp+40h] [rbp-11B0h] BYREF
__int16 v27[2]; // [rsp+C0h] [rbp-1130h] BYREF
__int16 v28; // [rsp+C4h] [rbp-112Ch]
__int16 v29; // [rsp+C6h] [rbp-112Ah]
int v30[62]; // [rsp+C8h] [rbp-1128h] BYREF
__int16 v31[2]; // [rsp+1C0h] [rbp-1030h] BYREF
unsigned __int8 v32; // [rsp+1C4h] [rbp-102Ch]
char v33; // [rsp+1C5h] [rbp-102Bh]
__int16 v34; // [rsp+1C6h] [rbp-102Ah]
struct ether_addr addr; // [rsp+1C8h] [rbp-1028h] BYREF

if ( *(_BYTE *)(a1 + 22) )
{
memset(v30, 0, sizeof(v30));
v3 = _bswap_16(1);
v27[0] = v3;
v4 = _bswap_16(2048);
v28 = v5;
v21 = v4;
v27[1] = v4;
v6 = *(_DWORD *)(a1 + 16);
v30[4] = a2.s_addr;
v30[0] = v6;
LOWORD(v6) = *(_WORD *)(a1 + 20);
v29 = v3;
LOWORD(v30[1]) = v6;
v7 = inet_ntoa(a2);
logger(6LL, "checking %s is available on attached networks", v7);
open_socket(a1, 1LL);
send_packet(a1, 2054LL, v27, 2 * HIBYTE(v28) + 2 * (unsigned int)(unsigned __int8)v28 + 8);
v20 = *(_DWORD *)(a1 + 28);
n = v20;
v8 = alloca(v20);
v22 = uptime() + 2;
LABEL_3:
while ( 1 )
{
v9 = uptime();
timeout.tv_usec = 0LL;
timeout.tv_sec = v22 - v9;
if ( v22 - v9 <= 0 )
break;
memset(&readfds, 0, sizeof(readfds));
v10 = *(_DWORD *)(a1 + 24);
v11 = v10 + 63;
if ( v10 >= 0 )
v11 = *(_DWORD *)(a1 + 24);
readfds.fds_bits[v11 >> 6] |= 1LL << (*(_DWORD *)(a1 + 24) % 64);
if ( !select(v10 + 1, &readfds, 0LL, 0LL, &timeout) )
break;
v12 = *(_DWORD *)(a1 + 24);
v13 = v12 + 63;
if ( v12 >= 0 )
v13 = *(_DWORD *)(a1 + 24);
if ( (readfds.fds_bits[v13 >> 6] & (1LL << (*(_DWORD *)(a1 + 24) % 64))) != 0 )
{
memset(&v18, 0, n);
v24 = -1;
v23 = v20;
while ( 1 )
{
memset(v31, 0, 0x1000uLL);
if ( (int)get_packet(a1, v31, &v18, &v23, &v24) < 0 )
break;
if ( v34 == _bswap_16(2) && v3 == v31[0] && v21 == v31[1] && v33 == 4 && v32 == 6 && v14 > 27 )
{
v15 = ether_ntoa(&addr);
v16 = inet_ntoa(*(struct in_addr *)&addr.ether_addr_octet[v32]);
logger(3LL, "ARPOP_REPLY received from %s (%s)", v16, v15);
close(*(_DWORD *)(a1 + 24));
*(_DWORD *)(a1 + 24) = -1;
return 1LL;
}
if ( !v24 )
goto LABEL_3;
}
}
}
close(*(_DWORD *)(a1 + 24));
*(_DWORD *)(a1 + 24) = -1;
result = 0LL;
}
else
{
logger(7LL, "arp_check: interface `%s' is not ARPable", (const char *)a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, struct in_addr a2)
{
__int16 v3; // r15
__int16 v4; // ax
__int16 v5; // dx
int v6; // eax
char *v7; // rax
void *v8; // rsp
__int64 v9; // rax
int v10; // edi
int v11; // eax
int v12; // ecx
int v13; // eax
int v14; // edx
char *v15; // r12
char *v16; // rax
__int64 result; // rax
__int64 v18; // [rsp+0h] [rbp-11F0h] BYREF
size_t n; // [rsp+8h] [rbp-11E8h]
int v20; // [rsp+10h] [rbp-11E0h]
__int16 v21; // [rsp+16h] [rbp-11DAh]
__int64 v22; // [rsp+18h] [rbp-11D8h]
int v23; // [rsp+28h] [rbp-11C8h] BYREF
int v24; // [rsp+2Ch] [rbp-11C4h] BYREF
struct timeval timeout; // [rsp+30h] [rbp-11C0h] BYREF
fd_set readfds; // [rsp+40h] [rbp-11B0h] BYREF
__int16 v27[2]; // [rsp+C0h] [rbp-1130h] BYREF
__int16 v28; // [rsp+C4h] [rbp-112Ch]
__int16 v29; // [rsp+C6h] [rbp-112Ah]
int v30[62]; // [rsp+C8h] [rbp-1128h] BYREF
__int16 v31[2]; // [rsp+1C0h] [rbp-1030h] BYREF
unsigned __int8 v32; // [rsp+1C4h] [rbp-102Ch]
char v33; // [rsp+1C5h] [rbp-102Bh]
__int16 v34; // [rsp+1C6h] [rbp-102Ah]
struct ether_addr addr; // [rsp+1C8h] [rbp-1028h] BYREF

if ( *(_BYTE *)(a1 + 22) )
{
memset(v30, 0, sizeof(v30));
v3 = func0(1);
v27[0] = v3;
v4 = func0(2048);
v28 = v5;
v21 = v4;
v27[1] = v4;
v6 = *(_DWORD *)(a1 + 16);
v30[4] = a2.s_addr;
v30[0] = v6;
LOWORD(v6) = *(_WORD *)(a1 + 20);
v29 = v3;
LOWORD(v30[1]) = v6;
v7 = inet_ntoa(a2);
logger(6LL, "checking %s is available on attached networks", v7);
open_socket(a1, 1LL);
send_packet(a1, 2054LL, v27, 2 * HIBYTE(v28) + 2 * (unsigned int)(unsigned __int8)v28 + 8);
v20 = *(_DWORD *)(a1 + 28);
n = v20;
v8 = alloca(v20);
v22 = uptime() + 2;
LABEL_3:
while ( 1 )
{
v9 = uptime();
timeout.tv_usec = 0LL;
timeout.tv_sec = v22 - v9;
if ( v22 - v9 <= 0 )
break;
memset(&readfds, 0, sizeof(readfds));
v10 = *(_DWORD *)(a1 + 24);
v11 = v10 + 63;
if ( v10 >= 0 )
v11 = *(_DWORD *)(a1 + 24);
readfds.fds_bits[v11 >> 6] |= 1LL << (*(_DWORD *)(a1 + 24) % 64);
if ( !select(v10 + 1, &readfds, 0LL, 0LL, &timeout) )
break;
v12 = *(_DWORD *)(a1 + 24);
v13 = v12 + 63;
if ( v12 >= 0 )
v13 = *(_DWORD *)(a1 + 24);
if ( (readfds.fds_bits[v13 >> 6] & (1LL << (*(_DWORD *)(a1 + 24) % 64))) != 0 )
{
memset(&v18, 0, n);
v24 = -1;
v23 = v20;
while ( 1 )
{
memset(v31, 0, 0x1000uLL);
if ( (int)get_packet(a1, v31, &v18, &v23, &v24) < 0 )
break;
if ( v34 == func0(2) && v3 == v31[0] && v21 == v31[1] && v33 == 4 && v32 == 6 && v14 > 27 )
{
v15 = ether_ntoa(&addr);
v16 = inet_ntoa(*(struct in_addr *)&addr.ether_addr_octet[v32]);
logger(3LL, "ARPOP_REPLY received from %s (%s)", v16, v15);
close(*(_DWORD *)(a1 + 24));
*(_DWORD *)(a1 + 24) = -1;
return 1LL;
}
if ( !v24 )
goto LABEL_3;
}
}
}
close(*(_DWORD *)(a1 + 24));
*(_DWORD *)(a1 + 24) = -1;
result = 0LL;
}
else
{
logger(7LL, "arp_check: interface `%s' is not ARPable", (const char *)a1);
result = 0LL;
}
return result;
}

original_code:
int arp_check (interface_t *iface, struct in_addr address)
{
  if (! iface->arpable)
    {
      logger (7, "arp_check: interface `%s' is not ARPable",
       iface->name);
      return 0;
    }
  unsigned char buf[256];
  struct arphdr *ah = (struct arphdr *) buf;
  memset (&buf, 0, sizeof (buf));
  ah->ar_hrd = htons (1);
  ah->ar_pro = htons (0x0800);
  ah->ar_hln = 6;
  ah->ar_pln = sizeof (struct in_addr);
  ah->ar_op = htons (1);
  memcpy ((((unsigned char *) ((ah) + 1)) + 0), &iface->ethernet_address, ah->ar_hln);
  memcpy ((((unsigned char *) ((ah) + 1)) + 2 * (ah)->ar_hln + (ah)->ar_pln), &address, ah->ar_pln);
  logger (6, "checking %s is available on attached networks", inet_ntoa
   (address));
  open_socket (iface, 1);
  send_packet (iface, 0x0806, (unsigned char *) &buf, ((sizeof (struct arphdr) + 2 * ((ah)->ar_hln) + 2 * ((ah)->ar_pln))));
  unsigned char reply[4096];
  int bytes;
  unsigned char buffer[iface->buffer_length];
  struct timeval tv;
  long timeout = 0;
  fd_set rset;
  timeout = uptime() + 2;
  while (1)
    {
      tv.tv_sec = timeout - uptime ();
      tv.tv_usec = 0;
      if (tv.tv_sec < 1)
 break;
      do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rset)->__fds_bits)[0]) : "memory"); } while (0);
      ((void) (((&rset)->__fds_bits)[((iface->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((iface->fd) % (8 * (int) sizeof (__fd_mask)))))));
      if (select (iface->fd + 1, &rset, ((void *)0), ((void *)0), &tv) == 0)
 break;
      if (! ((((&rset)->__fds_bits)[((iface->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((iface->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
 continue;
      memset (buffer, 0, sizeof (buffer));
      int buflen = sizeof (buffer);
      int bufpos = -1;
      while (bufpos != 0)
 {
   memset (&reply, 0, sizeof (reply));
   if ((bytes = get_packet (iface, (unsigned char *) &reply, buffer,
       &buflen, &bufpos)) < 0)
     break;
   ah = (struct arphdr *) reply;
   if (ah->ar_op != htons(2)
       || ah->ar_hrd != htons (1))
     continue;
   if (ah->ar_pro != htons (0x0800))
     continue;
   if (ah->ar_pln != sizeof (struct in_addr))
     continue;
   if (ah->ar_hln != 6)
     continue;
   if (bytes < sizeof (*ah) + 2 * (4 + ah->ar_hln))
     continue;
   logger (3, "ARPOP_REPLY received from %s (%s)",
    inet_ntoa (* (struct in_addr *) (((unsigned char *) ((ah) + 1)) + (ah)->ar_hln)),
    ether_ntoa ((struct ether_addr *) (((unsigned char *) ((ah) + 1)) + 0)));
   close (iface->fd);
   iface->fd = -1;
   return 1;
 }
    }
  close (iface->fd);
  iface->fd = -1;
  return 0;
}

canonical_original_code:
int arp_check(struct interface_t *iface, struct in_addr address)
{
  if (! iface->arpable)
    {
      logger (7, "arp_check: interface `%s' is not ARPable",
       iface->name);
      return 0;
    }
  unsigned char buf[256];
  struct arphdr *ah = (struct arphdr *) buf;
  memset (&buf, 0, sizeof (buf));
  ah->ar_hrd = htons (1);
  ah->ar_pro = htons (0x0800);
  ah->ar_hln = 6;
  ah->ar_pln = sizeof (struct in_addr);
  ah->ar_op = htons (1);
  memcpy ((((unsigned char *) ((ah) + 1)) + 0), &iface->ethernet_address, ah->ar_hln);
  memcpy ((((unsigned char *) ((ah) + 1)) + 2 * (ah)->ar_hln + (ah)->ar_pln), &address, ah->ar_pln);
  logger (6, "checking %s is available on attached networks", inet_ntoa
   (address));
  open_socket (iface, 1);
  send_packet (iface, 0x0806, (unsigned char *) &buf, ((sizeof (struct arphdr) + 2 * ((ah)->ar_hln) + 2 * ((ah)->ar_pln))));
  unsigned char reply[4096];
  int bytes;
  unsigned char buffer[-1];
  struct timeval tv;
  long timeout = 0;
  struct fd_set rset;
  timeout = uptime() + 2;
  while (1)
    {
      tv.tv_sec = timeout - uptime ();
      tv.tv_usec = 0;
      if (tv.tv_sec < 1)
 break;
      do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rset)->__fds_bits)[0]) : "memory"); } while (0);
      ((void) (((&rset)->__fds_bits)[((iface->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((iface->fd) % (8 * (int) sizeof (__fd_mask)))))));
      if (select (iface->fd + 1, &rset, ((void *)0), ((void *)0), &tv) == 0)
 break;
      if (! ((((&rset)->__fds_bits)[((iface->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((iface->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
 continue;
      memset (buffer, 0, sizeof (buffer));
      int buflen = sizeof (buffer);
      int bufpos = -1;
      while (bufpos != 0)
 {
   memset (&reply, 0, sizeof (reply));
   if ((bytes = get_packet (iface, (unsigned char *) &reply, buffer,
       &buflen, &bufpos)) < 0)
     break;
   ah = (struct arphdr *) reply;
   if (ah->ar_op != htons(2)
       || ah->ar_hrd != htons (1))
     continue;
   if (ah->ar_pro != htons (0x0800))
     continue;
   if (ah->ar_pln != sizeof (struct in_addr))
     continue;
   if (ah->ar_hln != 6)
     continue;
   if (bytes < sizeof (*ah) + 2 * (4 + ah->ar_hln))
     continue;
   logger (3, "ARPOP_REPLY received from %s (%s)",
    inet_ntoa (* (struct in_addr *) (((unsigned char *) ((ah) + 1)) + (ah)->ar_hln)),
    ether_ntoa ((struct ether_addr *) (((unsigned char *) ((ah) + 1)) + 0)));
   close (iface->fd);
   iface->fd = -1;
   return 1;
 }
    }
  close (iface->fd);
  iface->fd = -1;
  return 0;
}

variable_types: struct interface_t { char name[16]; struct ether_addr { unsigned char ether_addr_octet[6]; } ethernet_address; _Bool arpable; int fd; int buffer_length; int socket_protocol; char infofile[4096]; struct in_addr { unsigned int s_addr; } previous_address; struct route_t { struct in_addr destination; struct in_addr netmask; struct in_addr gateway; struct route_t *next; } *previous_routes; } *

variable_types:
address
struct in_addr

variable_types:
buf
unsigned char [256]

variable_types: struct arphdr { unsigned short ar_hrd; unsigned short ar_pro; unsigned char ar_hln; unsigned char ar_pln; unsigned short ar_op; } *

variable_types:
reply
unsigned char [4096]

variable_types: bytes int

variable_types:
buffer
unsigned char [-1]

variable_types: timeout long

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

variable_types: buflen int

variable_types: bufpos int

return_type: int

user_defined_types: struct interface_t { char name[16]; struct ether_addr ethernet_address; _Bool arpable; int fd; int buffer_length; int socket_protocol; char infofile[4096]; struct in_addr previous_address; struct route_t *previous_routes; }


decompiled_code:
__int64 __fastcall kill_pid(char *name, int sig)
{
__pid_t v2; // eax
unsigned int v3; // er12

v2 = readpid(name);
v3 = v2;
if ( !v2 || (v3 = kill(v2, sig)) != 0 )
{
logger(3LL, "dhcpcd not running");
unlink(name);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *name, int sig)
{
__pid_t v2; // eax
unsigned int v3; // er12

v2 = func1(name);
v3 = v2;
if ( !v2 || (v3 = kill(v2, sig)) != 0 )
{
logger(3LL, "dhcpcd not running");
unlink(name);
}
return v3;
}

original_code:
static int kill_pid (char *pidfile, int sig)
{
  pid_t pid = readpid (pidfile);
  int r = 0;
  if (!pid || (r = kill (pid, sig)))
    {
      logger (3, """dhcpcd"" not running");
      unlink (pidfile);
    }
  return r;
}

canonical_original_code:
static int kill_pid(char *pidfile, int sig)
{
  int pid = readpid (pidfile);
  int r = 0;
  if (!pid || (r = kill (pid, sig)))
    {
      logger (3, """dhcpcd"" not running");
      unlink (pidfile);
    }
  return r;
}

variable_types:
pidfile
char *

variable_types: sig int

variable_types: pid int

variable_types: r int


usage

decompiled_code:
int usage()
{
return puts(
"usage: dhcpcd [-adknpGHNRY] [-c script] [-h hostame] [-i classID]\n"
" [-l leasetime] [-m metric] [-s ipaddress] [-t timeout]\n"
" [-u userclass] [-F [none | ptr | both]] [-I clientID]");
}

canonical_decompiled_code:
int func0()
{
return puts(
"usage: dhcpcd [-adknpGHNRY] [-c script] [-h hostame] [-i classID]\n"
" [-l leasetime] [-m metric] [-s ipaddress] [-t timeout]\n"
" [-u userclass] [-F [none | ptr | both]] [-I clientID]");
}

original_code:
static void usage ()
{
  printf ("usage: ""dhcpcd"" [-adknpGHNRY] [-c script] [-h hostame] [-i classID]\n"
   "              [-l leasetime] [-m metric] [-s ipaddress] [-t timeout]\n"
   "              [-u userclass] [-F [none | ptr | both]] [-I clientID]\n");
}

canonical_original_code:
static void usage()
{
  printf ("usage: ""dhcpcd"" [-adknpGHNRY] [-c script] [-h hostame] [-i classID]\n"
   "              [-l leasetime] [-m metric] [-s ipaddress] [-t timeout]\n"
   "              [-u userclass] [-F [none | ptr | both]] [-I clientID]\n");
}


main

canonical_name: main

original_code:
int main(int argc, char **argv)
{
  options_t options;
  int zero;
  if ((zero = open ("/dev/null", 02, 0)) >= 0)
    {
      while (zero < 3)
 zero = dup (zero);
      close(zero);
    }
  openlog ("dhcpcd", 0x01, (16<<3));
  memset (&options, 0, sizeof (options_t));
  options.script = "/etc" "/" "dhcpcd" ".sh";
  snprintf (options.classid, 48, "%s %s", "dhcpcd", "3.0.0_alpha3");
  options.doarp = 0;
  options.dodns = 1;
  options.dontp = 1;
  options.dogateway = 1;
  options.timeout = 10;
  int doversion = 0;
  int dohelp = 0;
  int userclasses = 0;
  const struct option longopts[] =
    {
 {"arp", 0, ((void *)0), 'a'},
 {"script",1, ((void *)0), 'c'},
 {"debug", 0, ((void *)0), 'd'},
 {"hostname", 1, ((void *)0), 'h'},
 {"classid", 1, ((void *)0), 'i'},
 {"release", 0, ((void *)0), 'k'},
 {"leasetime", 1, ((void *)0), 'l'},
 {"metric", 1, ((void *)0), 'm'},
 {"renew", 0, ((void *)0), 'n'},
 {"persistent", 0, ((void *)0), 'p'},
 {"request", 1, ((void *)0), 's'},
 {"timeout", 1, ((void *)0), 't'},
 {"userclass", 1, ((void *)0), 'u'},
 {"fqdn", 2, ((void *)0), 'F'},
 {"nogateway", 0, ((void *)0), 'G'},
 {"sethostname", 0, ((void *)0), 'H'},
 {"clientid", 1, ((void *)0), 'I'},
 {"nontp", 0, ((void *)0), 'N'},
 {"nodns", 0, ((void *)0), 'R'},
 {"nonis", 0, ((void *)0), 'Y'},
 {"help", 0, &dohelp, 1},
 {"version", 0, &doversion, 1},
 {((void *)0), 0, ((void *)0), 0}
    };
  int ch;
  int option_index = 0;
  while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:F:GHI:NRY", longopts,
      &option_index)) != -1)
    switch (ch)
      {
      case 0:
 if (longopts[option_index].flag)
   break;
 logger (3, "option `%s' should set a flag",
  longopts[option_index].name);
 exit (1);
 break;
      case 'a':
 options.doarp = 1;
 break;
      case 'c':
 options.script = optarg;
 break;
      case 'd':
 setloglevel(7);
 break;
      case 'h':
 if (strlen (optarg) > 64)
   {
     logger(3, "`%s' too long for HostName string, max is %d",
     optarg, 64);
     exit (1);
   }
 else
   options.hostname = optarg;
 break;
      case 'i':
 if (strlen(optarg) > 48)
   {
     logger (3, "`%s' too long for ClassID string, max is %d",
      optarg, 48);
     exit (1);
   }
 else
   sprintf(options.classid, "%s", optarg);
 break;
      case 'k':
 options.signal = 1;
 break;
      case 'l':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.leasetime = (int) _number; };
 if (options.leasetime <= 0)
   {
     logger (3, "leasetime must be a positive value");
     exit (1);
   }
 break;
      case 'm':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.metric = (int) _number; };
 break;
      case 'n':
 options.signal = 14;
 break;
      case 'p':
 options.persistent = 1;
 break;
      case 's':
 if (! inet_aton (optarg, &options.requestaddress))
   {
     logger (3, "`%s' is not a valid IP address", optarg);
     exit (1);
   }
 break;
      case 't':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.timeout = (int) _number; };
 if (options.timeout < 0)
   {
     logger (3, "timeout must be a positive value");
     exit (1);
   }
 break;
      case 'u':
   {
     int i;
     int offset = 0;
     for (i = 0; i < userclasses; i++)
       offset += (int) options.userclass[offset] + 1;
     if (offset + 1 + strlen (optarg) > 255)
       {
  logger (3, "userclass overrun, max is %d",
   255);
  exit (1);
       }
     userclasses++;
     memcpy (options.userclass + offset + 1 , optarg, strlen (optarg));
     options.userclass[offset] = strlen (optarg);
   }
 break;
      case 'F':
 if (strcmp (optarg, "none") == 0)
   options.fqdn = FQDN_NONE;
 else if (strcmp (optarg, "ptr") == 0)
   options.fqdn = FQDN_PTR;
 else if (strcmp (optarg, "both") == 0)
   options.fqdn = FQDN_BOTH;
 else
   {
     logger (3, "invalid value `%s' for FQDN", optarg);
     exit (1);
   }
 break;
      case 'G':
 options.dogateway = 0;
 break;
      case 'H':
 options.dohostname = 1;
 break;
      case 'I':
 if (strlen (optarg) > 48)
   {
     logger (3, "`%s' is too long for ClientID, max is %d",
      optarg, 48);
     exit (1);
   }
 else
   sprintf(options.clientid, "%s", optarg);
 break;
      case 'N':
 options.dontp = 0;
 break;
      case 'R':
 options.dodns = 0;
 break;
      case 'Y':
 options.donis = 0;
 break;
      case '?':
 usage ();
 exit (1);
      default:
 usage ();
 exit (1);
      }
  if (doversion)
    printf ("""dhcpcd"" ""3.0.0_alpha3""\n");
  if (dohelp)
    usage ();
  if (optind < argc)
    {
      if (strlen (argv[optind]) > 16)
 {
   logger (3, "`%s' is too long for an interface name (max=%d)",
    argv[optind], 16);
   exit (1);
 }
      options.interface = argv[optind];
    }
  else
    {
      if (doversion || dohelp)
 exit (0);
      logger (3, "no interface specified", options.interface);
      exit (1);
    }
  if (geteuid ())
    {
      logger (3, "you need to be root to run ""dhcpcd");
      exit (1);
    }
  char prefix[16 + 3];
  snprintf (prefix, 16, "%s: ", options.interface);
  setlogprefix (prefix);
  snprintf (options.pidfile, sizeof (options.pidfile), "/var" "/run/" "dhcpcd" "-%s.pid",
     options.interface);
  if (options.signal != 0)
    exit (kill_pid (options.pidfile, options.signal));
  umask (022);
  if (readpid (options.pidfile))
    {
      logger (3, """dhcpcd"" already running (%s)", options.pidfile);
      exit (1);
    }
  if (mkdir ("/var" "/lib/" "dhcpcd", 0400 |0200 |0100 | (0400 >> 3) | (0100 >> 3)
      | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) && (*__errno_location ()) != 17 )
    {
      logger( 3, "mkdir(\"%s\",0): %m\n", "/var" "/lib/" "dhcpcd");
      exit (1);
    }
  if (mkdir ("/etc", 0400 | 0200 | 0100 | (0400 >> 3) | (0100 >> 3)
      | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) && (*__errno_location ()) != 17 )
    {
      logger (3, "mkdir(\"%s\",0): %m\n", "/etc");
      exit (1);
    }
  logger (6, "dhcpcd" " " "3.0.0_alpha3" " starting");
  if (dhcp_run (&options))
    exit (1);
  exit (0);
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct options_t options;
  int zero;
  if ((zero = open ("/dev/null", 02, 0)) >= 0)
    {
      while (zero < 3)
 zero = dup (zero);
      close(zero);
    }
  openlog ("dhcpcd", 0x01, (16<<3));
  memset (&options, 0, sizeof (options_t));
  options.script = "/etc" "/" "dhcpcd" ".sh";
  snprintf (options.classid, 48, "%s %s", "dhcpcd", "3.0.0_alpha3");
  options.doarp = 0;
  options.dodns = 1;
  options.dontp = 1;
  options.dogateway = 1;
  options.timeout = 10;
  int doversion = 0;
  int dohelp = 0;
  int userclasses = 0;
  struct option longopts[] = {
 {"arp", 0, ((void *)0), 'a'},
 {"script",1, ((void *)0), 'c'},
 {"debug", 0, ((void *)0), 'd'},
 {"hostname", 1, ((void *)0), 'h'},
 {"classid", 1, ((void *)0), 'i'},
 {"release", 0, ((void *)0), 'k'},
 {"leasetime", 1, ((void *)0), 'l'},
 {"metric", 1, ((void *)0), 'm'},
 {"renew", 0, ((void *)0), 'n'},
 {"persistent", 0, ((void *)0), 'p'},
 {"request", 1, ((void *)0), 's'},
 {"timeout", 1, ((void *)0), 't'},
 {"userclass", 1, ((void *)0), 'u'},
 {"fqdn", 2, ((void *)0), 'F'},
 {"nogateway", 0, ((void *)0), 'G'},
 {"sethostname", 0, ((void *)0), 'H'},
 {"clientid", 1, ((void *)0), 'I'},
 {"nontp", 0, ((void *)0), 'N'},
 {"nodns", 0, ((void *)0), 'R'},
 {"nonis", 0, ((void *)0), 'Y'},
 {"help", 0, &dohelp, 1},
 {"version", 0, &doversion, 1},
 {((void *)0), 0, ((void *)0), 0}
    };
  int ch;
  int option_index = 0;
  while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:F:GHI:NRY", longopts,
      &option_index)) != -1)
    switch (ch)
      {
      case 0:
 if (longopts[option_index].flag)
   break;
 logger (3, "option `%s' should set a flag",
  longopts[option_index].name);
 exit (1);
 break;
      case 'a':
 options.doarp = 1;
 break;
      case 'c':
 options.script = optarg;
 break;
      case 'd':
 setloglevel(7);
 break;
      case 'h':
 if (strlen (optarg) > 64)
   {
     logger(3, "`%s' too long for HostName string, max is %d",
     optarg, 64);
     exit (1);
   }
 else
   options.hostname = optarg;
 break;
      case 'i':
 if (strlen(optarg) > 48)
   {
     logger (3, "`%s' too long for ClassID string, max is %d",
      optarg, 48);
     exit (1);
   }
 else
   sprintf(options.classid, "%s", optarg);
 break;
      case 'k':
 options.signal = 1;
 break;
      case 'l':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.leasetime = (int) _number; };
 if (options.leasetime <= 0)
   {
     logger (3, "leasetime must be a positive value");
     exit (1);
   }
 break;
      case 'm':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.metric = (int) _number; };
 break;
      case 'n':
 options.signal = 14;
 break;
      case 'p':
 options.persistent = 1;
 break;
      case 's':
 if (! inet_aton (optarg, &options.requestaddress))
   {
     logger (3, "`%s' is not a valid IP address", optarg);
     exit (1);
   }
 break;
      case 't':
 { char *_tmp; (*__errno_location ()) = 0; long _number = strtol (optarg, &_tmp, 0); if (((*__errno_location ()) != 0 && _number == 0) || optarg == _tmp || ((*__errno_location ()) == 34 && (_number == 0x7fffffffffffffffL || _number == (-0x7fffffffffffffffL - 1L)))) { logger (3, "`%s' out of range", optarg);; exit (1); } else options.timeout = (int) _number; };
 if (options.timeout < 0)
   {
     logger (3, "timeout must be a positive value");
     exit (1);
   }
 break;
      case 'u':
   {
     int i;
     int offset = 0;
     for (i = 0; i < userclasses; i++)
       offset += (int) options.userclass[offset] + 1;
     if (offset + 1 + strlen (optarg) > 255)
       {
  logger (3, "userclass overrun, max is %d",
   255);
  exit (1);
       }
     userclasses++;
     memcpy (options.userclass + offset + 1 , optarg, strlen (optarg));
     options.userclass[offset] = strlen (optarg);
   }
 break;
      case 'F':
 if (strcmp (optarg, "none") == 0)
   options.fqdn = FQDN_NONE;
 else if (strcmp (optarg, "ptr") == 0)
   options.fqdn = FQDN_PTR;
 else if (strcmp (optarg, "both") == 0)
   options.fqdn = FQDN_BOTH;
 else
   {
     logger (3, "invalid value `%s' for FQDN", optarg);
     exit (1);
   }
 break;
      case 'G':
 options.dogateway = 0;
 break;
      case 'H':
 options.dohostname = 1;
 break;
      case 'I':
 if (strlen (optarg) > 48)
   {
     logger (3, "`%s' is too long for ClientID, max is %d",
      optarg, 48);
     exit (1);
   }
 else
   sprintf(options.clientid, "%s", optarg);
 break;
      case 'N':
 options.dontp = 0;
 break;
      case 'R':
 options.dodns = 0;
 break;
      case 'Y':
 options.donis = 0;
 break;
      case '?':
 usage ();
 exit (1);
      default:
 usage ();
 exit (1);
      }
  if (doversion)
    printf ("""dhcpcd"" ""3.0.0_alpha3""\n");
  if (dohelp)
    usage ();
  if (optind < argc)
    {
      if (strlen (argv[optind]) > 16)
 {
   logger (3, "`%s' is too long for an interface name (max=%d)",
    argv[optind], 16);
   exit (1);
 }
      options.interface = argv[optind];
    }
  else
    {
      if (doversion || dohelp)
 exit (0);
      logger (3, "no interface specified", options.interface);
      exit (1);
    }
  if (geteuid ())
    {
      logger (3, "you need to be root to run ""dhcpcd");
      exit (1);
    }
  char prefix[-1];
  snprintf (prefix, 16, "%s: ", options.interface);
  setlogprefix (prefix);
  snprintf (options.pidfile, sizeof (options.pidfile), "/var" "/run/" "dhcpcd" "-%s.pid",
     options.interface);
  if (options.signal != 0)
    exit (kill_pid (options.pidfile, options.signal));
  umask (022);
  if (readpid (options.pidfile))
    {
      logger (3, """dhcpcd"" already running (%s)", options.pidfile);
      exit (1);
    }
  if (mkdir ("/var" "/lib/" "dhcpcd", 0400 |0200 |0100 | (0400 >> 3) | (0100 >> 3)
      | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) && (*__errno_location ()) != 17 )
    {
      logger( 3, "mkdir(\"%s\",0): %m\n", "/var" "/lib/" "dhcpcd");
      exit (1);
    }
  if (mkdir ("/etc", 0400 | 0200 | 0100 | (0400 >> 3) | (0100 >> 3)
      | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) && (*__errno_location ()) != 17 )
    {
      logger (3, "mkdir(\"%s\",0): %m\n", "/etc");
      exit (1);
    }
  logger (6, "dhcpcd" " " "3.0.0_alpha3" " starting");
  if (dhcp_run (&options))
    exit (1);
  exit (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct options_t { char *interface; char *hostname; int fqdn; char classid[48]; char clientid[48]; char userclass[255]; unsigned int leasetime; long timeout; int metric; struct in_addr { unsigned int s_addr; } requestaddress; _Bool doarp; _Bool dodns; _Bool dontp; _Bool donis; _Bool dogateway; _Bool dohostname; _Bool dodomainname; int signal; _Bool persistent; char *script; char pidfile[4096]; }

variable_types: zero int

variable_types: doversion int

variable_types: dohelp int

variable_types: userclasses int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: ch int

variable_types: i int

variable_types: offset int

variable_types:
prefix
char [-1]

user_defined_types: struct options_t { char *interface; char *hostname; int fqdn; char classid[48]; char clientid[48]; char userclass[255]; unsigned int leasetime; long timeout; int metric; struct in_addr requestaddress; _Bool doarp; _Bool dodns; _Bool dontp; _Bool donis; _Bool dogateway; _Bool dohostname; _Bool dodomainname; int signal; _Bool persistent; char *script; char pidfile[4096]; }


xmalloc

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

result = malloc(a1);
if ( !result )
{
logger(3LL, "memory exhausted");
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
logger(3LL, "memory exhausted");
exit(1);
}
return result;
}

original_code:
void *xmalloc (size_t size)
{
  register void *value = malloc (size);
  if (value)
    return value;
  logger (3, "memory exhausted");
  exit (1);
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
  register void *value = malloc (size);
  if (value)
    return value;
  logger (3, "memory exhausted");
  exit (1);
}

variable_types:
size
unsigned long

variable_types:
value
void *

return_type: void *


uptime

decompiled_code:
__kernel_long_t uptime()
{
struct sysinfo v1; // [rsp+0h] [rbp-78h] BYREF

sysinfo(&v1);
return v1.uptime;
}

canonical_decompiled_code:
__kernel_long_t func0()
{
struct sysinfo v1; // [rsp+0h] [rbp-78h] BYREF

sysinfo(&v1);
return v1.uptime;
}

original_code:
long uptime (void)
{
  struct sysinfo info;
  sysinfo (&info);
  return info.uptime;
}

canonical_original_code:
long uptime()
{
  struct sysinfo info;
  sysinfo (&info);
  return info.uptime;
}

variable_types: struct sysinfo { long uptime; unsigned long loads[3]; unsigned long totalram; unsigned long freeram; unsigned long sharedram; unsigned long bufferram; unsigned long totalswap; unsigned long freeswap; unsigned short procs; unsigned short pad; unsigned long totalhigh; unsigned long freehigh; unsigned int mem_unit; char _f[-1]; }

return_type: long


getinput

repo: skoneka/screen-session

decompiled_code:
__int64 __fastcall getinput(int *a1, char *a2)
{
__int64 v2; // rbx
char v3; // al
int v4; // ebp
int v5; // eax
char v7; // [rsp+5h] [rbp-33h]
char nptr[50]; // [rsp+6h] [rbp-32h] BYREF

v2 = 0LL;
nptr[0] = 0;
while ( 1 )
{
v3 = mygetch();
v4 = v2++;
if ( (unsigned __int8)(v3 - 48) > 9u )
break;
*(&v7 + v2) = v3;
nptr[v2] = 0;
}
*a2 = v3;
v5 = 1;
if ( v4 )
v5 = atoi(nptr);
*a1 = v5;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, char *a2)
{
__int64 v2; // rbx
char v3; // al
int v4; // ebp
int v5; // eax
char v7; // [rsp+5h] [rbp-33h]
char nptr[50]; // [rsp+6h] [rbp-32h] BYREF

v2 = 0LL;
nptr[0] = 0;
while ( 1 )
{
v3 = func0();
v4 = v2++;
if ( (unsigned __int8)(v3 - 48) > 9u )
break;
*(&v7 + v2) = v3;
nptr[v2] = 0;
}
*a2 = v3;
v5 = 1;
if ( v4 )
v5 = atoi(nptr);
*a1 = v5;
return 0LL;
}

original_code:
int
getinput (int *prefix, char *mode)
{
  char prefix_str[10];
  int prefix_str_c = 0;
  char ch;
  prefix_str[0] = '\0';
  while (1) {
    ch = mygetch ();
    if (ch >= '0' && ch <= '9' && prefix_str_c < 10) {
      prefix_str[prefix_str_c] = ch;
      prefix_str_c++;
      prefix_str[prefix_str_c] = '\0';
    }
    else {
      *mode = ch;
      break;
    }
  }
  if (prefix_str_c > 0)
    *prefix = atoi (prefix_str);
  else
    *prefix = 1;
  return 0;
}

canonical_original_code:
int getinput(int *prefix, char *mode)
{
  char prefix_str[10];
  int prefix_str_c = 0;
  char ch;
  prefix_str[0] = '\0';
  while (1) {
    ch = mygetch ();
    if (ch >= '0' && ch <= '9' && prefix_str_c < 10) {
      prefix_str[prefix_str_c] = ch;
      prefix_str_c++;
      prefix_str[prefix_str_c] = '\0';
    }
    else {
      *mode = ch;
      break;
    }
  }
  if (prefix_str_c > 0)
    *prefix = atoi (prefix_str);
  else
    *prefix = 1;
  return 0;
}

variable_types:
prefix
int *

variable_types:
mode
char *

variable_types: ch char


decompiled_code:
char **__fastcall get_font(char a1)
{
unsigned __int8 v1; // di
char **result; // rax

v1 = a1 - 32;
result = (char **)(unsigned int)((_DWORD)&fonts + 616);
if ( v1 <= 0x19u )
result = CSWTCH_9[v1];
return result;
}

canonical_decompiled_code:
char **__fastcall func2(char a1)
{
unsigned __int8 v1; // di
char **result; // rax

v1 = a1 - 32;
result = (char **)(unsigned int)((_DWORD)&fonts + 616);
if ( v1 <= 0x19u )
result = CSWTCH_9[v1];
return result;
}

original_code:
char **
get_font (char c)
{
  char **font = ((void *)0);
  switch (c) {
  case '0':
    font = &fonts[0 * 7];
    break;
  case '1':
    font = &fonts[1 * 7];
    break;
  case '2':
    font = &fonts[2 * 7];
    break;
  case '3':
    font = &fonts[3 * 7];
    break;
  case '4':
    font = &fonts[4 * 7];
    break;
  case '5':
    font = &fonts[5 * 7];
    break;
  case '6':
    font = &fonts[6 * 7];
    break;
  case '7':
    font = &fonts[7 * 7];
    break;
  case '8':
    font = &fonts[8 * 7];
    break;
  case '9':
    font = &fonts[9 * 7];
    break;
  case ' ':
    font = &fonts[10 * 7];
    break;
  default:
    font = &fonts[11 * 7];
    break;
  }
  return font;
}

canonical_original_code:
char **get_font(char c)
{
  char **font = ((void *)0);
  switch (c) {
  case '0':
    font = &fonts[0 * 7];
    break;
  case '1':
    font = &fonts[1 * 7];
    break;
  case '2':
    font = &fonts[2 * 7];
    break;
  case '3':
    font = &fonts[3 * 7];
    break;
  case '4':
    font = &fonts[4 * 7];
    break;
  case '5':
    font = &fonts[5 * 7];
    break;
  case '6':
    font = &fonts[6 * 7];
    break;
  case '7':
    font = &fonts[7 * 7];
    break;
  case '8':
    font = &fonts[8 * 7];
    break;
  case '9':
    font = &fonts[9 * 7];
    break;
  case ' ':
    font = &fonts[10 * 7];
    break;
  default:
    font = &fonts[11 * 7];
    break;
  }
  return font;
}

variable_types: c char

variable_types:
font
char **

return_type: char **


decompiled_code:
void __fastcall print_number(const char *a1, const char *a2)
{
size_t v3; // rax
__int64 *v4; // rax
char v5; // di
__int64 *v6; // r13
char **v7; // rax
__int64 v8; // rdx
int v9; // er14
__int64 v10; // rbp
__int64 *v11; // rbx
__int64 v12; // rax

v3 = strlen(a1);
v4 = (__int64 *)malloc(8 * v3 + 8);
v5 = *a1;
v6 = v4;
if ( v5 )
{
do
{
v7 = get_font(v5);
v9 = v8;
v6[v8 - 1] = (__int64)v7;
v5 = a1[v8];
}
while ( v5 );
}
else
{
v9 = 0;
}
v10 = 0LL;
printf("%s", a2);
do
{
v11 = v6;
if ( v9 )
{
do
{
v12 = *v11++;
printf("%s ", *(const char **)(v12 + v10));
}
while ( &v6[(unsigned int)(v9 - 1) + 1] != v11 );
}
v10 += 8LL;
putchar(10);
}
while ( v10 != 56 );
printf("%s", "\x1B[0m");
if ( !v6 )
_assert_fail("font!=NULL", "ScreenSession/screen-session-helper.c", 0x100u, "print_number");
free(v6);
}

canonical_decompiled_code:
void __fastcall func3(const char *a1, const char *a2)
{
size_t v3; // rax
__int64 *v4; // rax
char v5; // di
__int64 *v6; // r13
char **v7; // rax
__int64 v8; // rdx
int v9; // er14
__int64 v10; // rbp
__int64 *v11; // rbx
__int64 v12; // rax

v3 = strlen(a1);
v4 = (__int64 *)malloc(8 * v3 + 8);
v5 = *a1;
v6 = v4;
if ( v5 )
{
do
{
v7 = func2(v5);
v9 = v8;
v6[v8 - 1] = (__int64)v7;
v5 = a1[v8];
}
while ( v5 );
}
else
{
v9 = 0;
}
v10 = 0LL;
printf("%s", a2);
do
{
v11 = v6;
if ( v9 )
{
do
{
v12 = *v11++;
printf("%s ", *(const char **)(v12 + v10));
}
while ( &v6[(unsigned int)(v9 - 1) + 1] != v11 );
}
v10 += 8LL;
putchar(10);
}
while ( v10 != 56 );
printf("%s", "\x1B[0m");
if ( !v6 )
_assert_fail("font!=NULL", "ScreenSession/screen-session-helper.c", 0x100u, "print_number");
free(v6);
}

original_code:
void
print_number (char *n, char *color)
{
  int k;
  int j;
  char ***font = malloc ((strlen (n) + 1) * sizeof (char ***));
  int letter_c = 0;
  while (n[letter_c] != '\0') {
    font[letter_c] = get_font (n[letter_c]);
    letter_c++;
  }
  printf ("%s", color);
  for (k = 0; k < 7; k++) {
    for (j = 0; j < letter_c; j++) {
      printf ("%s ", font[j][k]);
    }
    printf ("\n");
  }
  printf ("%s", "\033[0m");
  ((font!=((void *)0)) ? (void) (0) : __assert_fail ("font!=NULL", "ScreenSession/screen-session-helper.c", 256, __extension__ __PRETTY_FUNCTION__)); free(font); font = ((void *)0);;
}

canonical_original_code:
void print_number(char *n, char *color)
{
  int k;
  int j;
  char ***font = malloc ((strlen (n) + 1) * sizeof (char ***));
  int letter_c = 0;
  while (n[letter_c] != '\0') {
    font[letter_c] = get_font (n[letter_c]);
    letter_c++;
  }
  printf ("%s", color);
  for (k = 0; k < 7; k++) {
    for (j = 0; j < letter_c; j++) {
      printf ("%s ", font[j][k]);
    }
    printf ("\n");
  }
  printf ("%s", "\033[0m");
  ((font!=((void *)0)) ? (void) (0) : __assert_fail ("font!=NULL", "ScreenSession/screen-session-helper.c", 256, __extension__ __PRETTY_FUNCTION__)); free(font); font = ((void *)0);;
}

variable_types:
n
char *

variable_types:
color
char *

variable_types: k int

variable_types: j int

variable_types:
font
char ***


main

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

if ( argc == 1 )
{
LABEL_7:
printf("screen-session %s helper program\n", "0.6.6devel");
return 0;
}
v4 = argv[1];
if ( *v4 != 45 || v4[1] != 110 || v4[2] )
{
if ( !strcmp(v4, "-nh") )
{
regions_helper((char *)argv[2], argv[3]);
return 0;
}
goto LABEL_7;
}
print_number(argv[2], "\x1B[0m");
return 0;
}

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

if ( argc == 1 )
{
LABEL_7:
printf("screen-session %s helper program\n", "0.6.6devel");
return 0;
}
v4 = argv[1];
if ( *v4 != 45 || v4[1] != 110 || v4[2] )
{
if ( !strcmp(v4, "-nh") )
{
func4((char *)argv[2], argv[3]);
return 0;
}
goto LABEL_7;
}
func3(argv[2], "\x1B[0m");
return 0;
}

original_code:
int
main (int argc, char **argv)
{
  if (argc == 1) {
    printf ("screen-session %s helper program\n", "0.6.6devel");
    return 0;
  }
  else if (strcmp (argv[1], "-n") == 0) {
    print_number (argv[2], "\033[0m");
    return 0;
  }
  else if (strcmp (argv[1], "-nh") == 0) {
    regions_helper (argv[2], argv[3]);
    return 0;
  }
  else {
    printf ("screen-session %s helper program\n", "0.6.6devel");
  }
}

canonical_original_code:
int main(int argc, char **argv)
{
  if (argc == 1) {
    printf ("screen-session %s helper program\n", "0.6.6devel");
    return 0;
  }
  else if (strcmp (argv[1], "-n") == 0) {
    print_number (argv[2], "\033[0m");
    return 0;
  }
  else if (strcmp (argv[1], "-nh") == 0) {
    regions_helper (argv[2], argv[3]);
    return 0;
  }
  else {
    printf ("screen-session %s helper program\n", "0.6.6devel");
  }
}

variable_types: argc int

variable_types:
argv
char **


mygetch

decompiled_code:
__int64 mygetch()
{
__m128i v0; // xmm0
__m128i v1; // xmm2
unsigned int v2; // er12
struct termios v4; // [rsp+0h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-48h] BYREF

tcgetattr(0, &v4);
v0 = _mm_loadu_si128((const __m128i *)&v4);
v1 = _mm_loadu_si128((const __m128i *)&v4.c_cc[15]);
*(__m128i *)&termios_p.c_line = _mm_loadu_si128((const __m128i *)&v4.c_line);
*(_QWORD *)&termios_p.c_cc[31] = *(_QWORD *)&v4.c_cc[31];
*(__m128i *)&termios_p.c_iflag = v0;
termios_p.c_ospeed = v4.c_ospeed;
*(__m128i *)&termios_p.c_cc[15] = v1;
termios_p.c_lflag = v0.m128i_i32[3] & 0xFFFFFFF5;
*(_WORD *)&termios_p.c_cc[5] = 256;
tcsetattr(0, 0, &termios_p);
v2 = getchar();
tcsetattr(0, 0, &v4);
fputc(v2, stdout);
return v2;
}

canonical_decompiled_code:
__int64 func0()
{
__m128i v0; // xmm0
__m128i v1; // xmm2
unsigned int v2; // er12
struct termios v4; // [rsp+0h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-48h] BYREF

tcgetattr(0, &v4);
v0 = _mm_loadu_si128((const __m128i *)&v4);
v1 = _mm_loadu_si128((const __m128i *)&v4.c_cc[15]);
*(__m128i *)&termios_p.c_line = _mm_loadu_si128((const __m128i *)&v4.c_line);
*(_QWORD *)&termios_p.c_cc[31] = *(_QWORD *)&v4.c_cc[31];
*(__m128i *)&termios_p.c_iflag = v0;
termios_p.c_ospeed = v4.c_ospeed;
*(__m128i *)&termios_p.c_cc[15] = v1;
termios_p.c_lflag = v0.m128i_i32[3] & 0xFFFFFFF5;
*(_WORD *)&termios_p.c_cc[5] = 256;
tcsetattr(0, 0, &termios_p);
v2 = getchar();
tcsetattr(0, 0, &v4);
fputc(v2, stdout);
return v2;
}

original_code:
int
mygetch (void)
{
  int ch;
  struct termios oldt, newt;
  tcgetattr (0, &oldt);
  newt = oldt;
  newt.c_cc[6] = 1;
  newt.c_cc[5] = 0;
  newt.c_lflag &= ~(0000002 | 0000010);
  tcsetattr (0, 0, &newt);
  ch = getchar ();
  tcsetattr (0, 0, &oldt);
  fputc(ch,stdout);
  return ch;
}

canonical_original_code:
int mygetch()
{
  int ch;
  struct termios oldt; struct termios newt;
  tcgetattr (0, &oldt);
  newt = oldt;
  newt.c_cc[6] = 1;
  newt.c_cc[5] = 0;
  newt.c_lflag &= ~(0000002 | 0000010);
  tcsetattr (0, 0, &newt);
  ch = getchar ();
  tcsetattr (0, 0, &oldt);
  fputc(ch,stdout);
  return ch;
}

variable_types: ch int

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


main

decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
char *v4; // rsi

if ( a1 == 1 )
{
LABEL_7:
printf("screen-session %s helper program\n", "0.6.6devel");
return 0LL;
}
v4 = a2[1];
if ( *v4 != 45 || v4[1] != 110 || v4[2] )
{
if ( !strcmp(v4, "-nh") )
{
sub_4014C0(a2[2]);
return 0LL;
}
goto LABEL_7;
}
sub_4013C0(a2[2], "\x1B[0m", a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
char *v4; // rsi

if ( a1 == 1 )
{
LABEL_7:
printf("screen-session %s helper program\n", "0.6.6devel");
return 0LL;
}
v4 = a2[1];
if ( *v4 != 45 || v4[1] != 110 || v4[2] )
{
if ( !strcmp(v4, "-nh") )
{
func10(a2[2]);
return 0LL;
}
goto LABEL_7;
}
func9(a2[2], "\x1B[0m", a3);
return 0LL;
}


repo: skunne/minilibt

decompiled_code:
void mlt_sig_int()
{
__int64 v0; // rax

v0 = mlt_get_param(0LL);
mlt_exit(v0);
}

canonical_decompiled_code:
void func1()
{
__int64 v0; // rax

v0 = func0(0LL);
mlt_exit(v0);
}

original_code:
static void mlt_sig_int(int x)
{
 t_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 mlt_exit(mlt);
}

canonical_original_code:
static void mlt_sig_int(int x)
{
 struct s_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 mlt_exit(mlt);
}

variable_types: x int

variable_types: struct s_mlt { struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } thermos; struct termios original_thermos; struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; } w; } *

user_defined_types: struct s_mlt { struct termios thermos; struct termios original_thermos; struct winsize w; }


decompiled_code:
void mlt_sig_cont()
{
__int64 v0; // rbx
char *v1; // rax
char *v2; // rax

v0 = mlt_get_param(0LL);
signal(20, mlt_sig_pause);
v1 = tgetstr("ti", 0LL);
tputs(v1, 1, (int (*)(int))&mlt_tputs_putchar);
v2 = tgetstr("vi", 0LL);
tputs(v2, 1, (int (*)(int))&mlt_tputs_putchar);
tcsetattr(0, 0, (const struct termios *)v0);
f_expose_hook(*(unsigned __int16 *)(v0 + 122), *(unsigned __int16 *)(v0 + 120), p_expose_hook);
}

canonical_decompiled_code:
void func2()
{
__int64 v0; // rbx
char *v1; // rax
char *v2; // rax

v0 = func0(0LL);
signal(20, mlt_sig_pause);
v1 = tgetstr("ti", 0LL);
tputs(v1, 1, (int (*)(int))&mlt_tputs_putchar);
v2 = tgetstr("vi", 0LL);
tputs(v2, 1, (int (*)(int))&mlt_tputs_putchar);
tcsetattr(0, 0, (const struct termios *)v0);
f_expose_hook(*(unsigned __int16 *)(v0 + 122), *(unsigned __int16 *)(v0 + 120), p_expose_hook);
}

original_code:
static void mlt_sig_cont(int x)
{
 t_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 signal(20, &mlt_sig_pause);
 tputs((tgetstr(("ti"), ((void *)0))), 1, &mlt_tputs_putchar);
 tputs((tgetstr(("vi"), ((void *)0))), 1, &mlt_tputs_putchar);
 tcsetattr(0, 0, &mlt->thermos);
 (*f_expose_hook)(mlt->w.ws_col, mlt->w.ws_row, p_expose_hook);
}

canonical_original_code:
static void mlt_sig_cont(int x)
{
 struct s_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 signal(20, &mlt_sig_pause);
 tputs((tgetstr(("ti"), ((void *)0))), 1, &mlt_tputs_putchar);
 tputs((tgetstr(("vi"), ((void *)0))), 1, &mlt_tputs_putchar);
 tcsetattr(0, 0, &mlt->thermos);
 (*f_expose_hook)(mlt->w.ws_col, mlt->w.ws_row, p_expose_hook);
}

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

user_defined_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }


decompiled_code:
void mlt_sig_pause()
{
__int64 v0; // rbx

v0 = mlt_get_param(0LL);
mlt_restore_term(v0 + 60);
signal(20, 0LL);
ioctl(0, 0x5412uLL, v0 + 27);
}

canonical_decompiled_code:
void func3()
{
__int64 v0; // rbx

v0 = func0(0LL);
mlt_restore_term(v0 + 60);
signal(20, 0LL);
ioctl(0, 0x5412uLL, v0 + 27);
}

original_code:
static void mlt_sig_pause(int x)
{
 t_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 mlt_restore_term(&mlt->original_thermos);
 signal(20, ((__sighandler_t) 0));
 ioctl(0, 0x5412, &mlt->thermos.c_cc[10]);
}

canonical_original_code:
static void mlt_sig_pause(int x)
{
 struct s_mlt *mlt;
 (void) x;
 mlt = mlt_get_param(((void *)0));
 mlt_restore_term(&mlt->original_thermos);
 signal(20, ((void (*)(int)) 0));
 ioctl(0, 0x5412, &mlt->thermos.c_cc[10]);
}


decompiled_code:
__sighandler_t __fastcall mlt_init_signal(__int64 a1)
{
mlt_get_param(a1);
signal(20, (__sighandler_t)mlt_sig_pause);
signal(18, (__sighandler_t)mlt_sig_cont);
return signal(2, (__sighandler_t)mlt_sig_int);
}

canonical_decompiled_code:
__sighandler_t __fastcall func4(__int64 a1)
{
func0(a1);
signal(20, (__sighandler_t)mlt_sig_pause);
signal(18, (__sighandler_t)mlt_sig_cont);
return signal(2, (__sighandler_t)mlt_sig_int);
}

original_code:
void mlt_init_signal(t_mlt *mlt_ptr)
{
 mlt_get_param(mlt_ptr);
 signal(20, &mlt_sig_pause);
 signal(18, &mlt_sig_cont);
 signal(2, &mlt_sig_int);
}

canonical_original_code:
void mlt_init_signal(struct s_mlt *mlt_ptr)
{
 mlt_get_param(mlt_ptr);
 signal(20, &mlt_sig_pause);
 signal(18, &mlt_sig_cont);
 signal(2, &mlt_sig_int);
}


decompiled_code:
ssize_t __fastcall mlt_char_put(int a1, int a2, int a3, int a4, char a5)
{
char *v7; // rax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
char *v12; // rdi
int v13; // eax
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

buf[0] = a5;
v7 = tgetstr("cm", 0LL);
v8 = tgoto(v7, a1, a2);
tputs(v8, 1, (int (*)(int))&mlt_tputs_putchar);
v9 = tgetstr("AF", 0LL);
v10 = tgoto(v9, 0, a3);
tputs(v10, 1, (int (*)(int))&mlt_tputs_putchar);
v11 = tgetstr("AB", 0LL);
v12 = tgoto(v11, 0, a4);
tputs(v12, 1, (int (*)(int))&mlt_tputs_putchar);
v13 = mlt_spe_out();
return write(v13, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func0(int a1, int a2, int a3, int a4, char a5)
{
char *v7; // rax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
char *v12; // rdi
int v13; // eax
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

buf[0] = a5;
v7 = tgetstr("cm", 0LL);
v8 = tgoto(v7, a1, a2);
tputs(v8, 1, (int (*)(int))&mlt_tputs_putchar);
v9 = tgetstr("AF", 0LL);
v10 = tgoto(v9, 0, a3);
tputs(v10, 1, (int (*)(int))&mlt_tputs_putchar);
v11 = tgetstr("AB", 0LL);
v12 = tgoto(v11, 0, a4);
tputs(v12, 1, (int (*)(int))&mlt_tputs_putchar);
v13 = mlt_spe_out();
return write(v13, buf, 1uLL);
}

original_code:
void mlt_char_put(int x, int y, int fg, int bg, char c)
{
 tputs((tgoto(tgetstr(("cm"), ((void *)0)), (x), (y))), 1, &mlt_tputs_putchar);
 tputs((tgoto(tgetstr(("AF"), ((void *)0)), (0), (fg))), 1, &mlt_tputs_putchar);
 tputs((tgoto(tgetstr(("AB"), ((void *)0)), (0), (bg))), 1, &mlt_tputs_putchar);
 write(mlt_spe_out(), &c, 1);
}

variable_types: y int

variable_types: fg int

variable_types: bg int


decompiled_code:
ssize_t __fastcall mlt_string_put(int a1, int a2, int a3, int a4, char *s)
{
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
char *v12; // rax
char *v13; // rax
size_t v14; // r12
int v15; // eax

v8 = tgetstr("cm", 0LL);
v9 = tgoto(v8, a1, a2);
tputs(v9, 1, (int (*)(int))&mlt_tputs_putchar);
v10 = tgetstr("AF", 0LL);
v11 = tgoto(v10, 0, a3);
tputs(v11, 1, (int (*)(int))&mlt_tputs_putchar);
v12 = tgetstr("AB", 0LL);
v13 = tgoto(v12, 0, a4);
tputs(v13, 1, (int (*)(int))&mlt_tputs_putchar);
v14 = strlen(s);
v15 = mlt_spe_out();
return write(v15, s, v14);
}

canonical_decompiled_code:
ssize_t __fastcall func1(int a1, int a2, int a3, int a4, char *s)
{
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
char *v12; // rax
char *v13; // rax
size_t v14; // r12
int v15; // eax

v8 = tgetstr("cm", 0LL);
v9 = tgoto(v8, a1, a2);
tputs(v9, 1, (int (*)(int))&mlt_tputs_putchar);
v10 = tgetstr("AF", 0LL);
v11 = tgoto(v10, 0, a3);
tputs(v11, 1, (int (*)(int))&mlt_tputs_putchar);
v12 = tgetstr("AB", 0LL);
v13 = tgoto(v12, 0, a4);
tputs(v13, 1, (int (*)(int))&mlt_tputs_putchar);
v14 = strlen(s);
v15 = mlt_spe_out();
return write(v15, s, v14);
}

original_code:
void mlt_string_put(int x, int y, int fg, int bg, char *str)
{
 tputs((tgoto(tgetstr(("cm"), ((void *)0)), (x), (y))), 1, &mlt_tputs_putchar);
 tputs((tgoto(tgetstr(("AF"), ((void *)0)), (0), (fg))), 1, &mlt_tputs_putchar);
 tputs((tgoto(tgetstr(("AB"), ((void *)0)), (0), (bg))), 1, &mlt_tputs_putchar);
 write(mlt_spe_out(), str, strlen(str));
}

variable_types:
str
char *


decompiled_code:
ssize_t __fastcall mlt_img_put(__int64 a1)
{
return write(1, *(const void **)a1, 23 * *(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
}

canonical_decompiled_code:
ssize_t __fastcall func2(__int64 a1)
{
return write(1, *(const void **)a1, 23 * *(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
}

original_code:
void mlt_img_put(t_mlt_img *img)
{
 write(1, img->img, img->co * img->li * 23);
}

canonical_original_code:
void mlt_img_put(struct s_mlt_img *img)
{
 write(1, img->img, img->co * img->li * 23);
}

variable_types: struct s_mlt_img { char *img; int co; int li; } *


decompiled_code:
void f_expose_int()
{
mlt_recalc_win(p_mlt);
f_expose_hook(*(unsigned __int16 *)(p_mlt + 122), *(unsigned __int16 *)(p_mlt + 120), p_expose_hook);
}

canonical_decompiled_code:
void func0()
{
mlt_recalc_win(p_mlt);
f_expose_hook(*(unsigned __int16 *)(p_mlt + 122), *(unsigned __int16 *)(p_mlt + 120), p_expose_hook);
}

original_code:
static void f_expose_int(int x)
{
 (void) x;
 mlt_recalc_win(p_mlt);
 (*f_expose_hook)(p_mlt->w.ws_col, p_mlt->w.ws_row, p_expose_hook);
}


decompiled_code:
void __fastcall __noreturn mlt_loop(__int64 a1)
{
unsigned int buf; // [rsp+Bh] [rbp-Dh] BYREF
char v2; // [rsp+Fh] [rbp-9h]

f_expose_hook(*(unsigned __int16 *)(a1 + 122), *(unsigned __int16 *)(a1 + 120), p_expose_hook);
p_mlt = a1;
signal(28, (__sighandler_t)f_expose_int);
while ( 1 )
{
f_loop_hook(p_loop_hook);
while ( 1 )
{
f_loop_hook(p_loop_hook);
buf = 0;
v2 = 0;
if ( read(0, &buf, 4uLL) <= 0 )
break;
f_key_hook(buf, p_key_hook);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1)
{
unsigned int buf; // [rsp+Bh] [rbp-Dh] BYREF
char v2; // [rsp+Fh] [rbp-9h]

f_expose_hook(*(unsigned __int16 *)(a1 + 122), *(unsigned __int16 *)(a1 + 120), p_expose_hook);
p_mlt = a1;
signal(28, (__sighandler_t)f_expose_int);
while ( 1 )
{
f_loop_hook(p_loop_hook);
while ( 1 )
{
f_loop_hook(p_loop_hook);
buf = 0;
v2 = 0;
if ( read(0, &buf, 4uLL) <= 0 )
break;
f_key_hook(buf, p_key_hook);
}
}
}

original_code:
void mlt_loop(t_mlt *mlt_ptr)
{
 char buff[5];
 (*f_expose_hook)(mlt_ptr->w.ws_col, mlt_ptr->w.ws_row, p_expose_hook);
 p_mlt = mlt_ptr;
 signal(28, &f_expose_int);
   (*f_loop_hook)(p_loop_hook);
 while (42)
 {
  (*f_loop_hook)(p_loop_hook);
  bzero(buff, 5);
  if (read(0, buff, 4) > 0)
   (*f_key_hook)(*(int *) buff, p_key_hook);
  else
   (*f_loop_hook)(p_loop_hook);
 }
}

canonical_original_code:
void mlt_loop(struct s_mlt *mlt_ptr)
{
 char buff[5];
 (*f_expose_hook)(mlt_ptr->w.ws_col, mlt_ptr->w.ws_row, p_expose_hook);
 p_mlt = mlt_ptr;
 signal(28, &f_expose_int);
   (*f_loop_hook)(p_loop_hook);
 while (42)
 {
  (*f_loop_hook)(p_loop_hook);
  bzero(buff, 5);
  if (read(0, buff, 4) > 0)
   (*f_key_hook)(*(int *) buff, p_key_hook);
  else
   (*f_loop_hook)(p_loop_hook);
 }
}

variable_types:
buff
char [5]


decompiled_code:
void __fastcall mlt_key_hook(__int64 a1, __int64 (__fastcall *a2)(_QWORD, _QWORD), __int64 a3)
{
f_key_hook = a2;
p_key_hook = a3;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 (__fastcall *a2)(_QWORD, _QWORD), __int64 a3)
{
f_key_hook = a2;
p_key_hook = a3;
}

original_code:
void mlt_key_hook(t_mlt *mlt_ptr, void (*f)(int, void *), void *p)
{
 (void) mlt_ptr;
 f_key_hook = f;
 p_key_hook = p;
}

canonical_original_code:
void mlt_key_hook(struct s_mlt *mlt_ptr, void (*f)(int, void *), void *p)
{
 (void) mlt_ptr;
 f_key_hook = f;
 p_key_hook = p;
}

variable_types:
f
void (*)(int, void *)

variable_types:
p
void *


decompiled_code:
void __fastcall mlt_expose_hook(__int64 a1, __int64 (__fastcall *a2)(_QWORD, _QWORD, _QWORD), __int64 a3)
{
f_expose_hook = a2;
p_expose_hook = a3;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 (__fastcall *a2)(_QWORD, _QWORD, _QWORD), __int64 a3)
{
f_expose_hook = a2;
p_expose_hook = a3;
}

original_code:
void mlt_expose_hook(t_mlt *mlt_ptr,
        void (*f)(int x, int y, void *),
        void *p)
{
 (void) mlt_ptr;
 f_expose_hook = f;
 p_expose_hook = p;
}

canonical_original_code:
void mlt_expose_hook(struct s_mlt *mlt_ptr, void (*f)(int x, int y, void *), void *p)
{
 (void) mlt_ptr;
 f_expose_hook = f;
 p_expose_hook = p;
}

variable_types:
f
void (*)(int x, int y, void *)


decompiled_code:
void __fastcall mlt_loop_hook(__int64 a1, __int64 (__fastcall *a2)(_QWORD), __int64 a3)
{
f_loop_hook = a2;
p_loop_hook = a3;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, __int64 (__fastcall *a2)(_QWORD), __int64 a3)
{
f_loop_hook = a2;
p_loop_hook = a3;
}

original_code:
void mlt_loop_hook(t_mlt *mlt_ptr, void (*f)(void *), void *p)
{
 (void) mlt_ptr;
 f_loop_hook = f;
 p_loop_hook = p;
}

canonical_original_code:
void mlt_loop_hook(struct s_mlt *mlt_ptr, void (*f)(void *), void *p)
{
 (void) mlt_ptr;
 f_loop_hook = f;
 p_loop_hook = p;
}

variable_types:
f
void (*)(void *)


decompiled_code:
int mlt_clear_window()
{
char *v0; // rax

v0 = tgetstr("cl", 0LL);
return tputs(v0, 1, (int (*)(int))&mlt_tputs_putchar);
}

canonical_decompiled_code:
int func0()
{
char *v0; // rax

v0 = tgetstr("cl", 0LL);
return tputs(v0, 1, (int (*)(int))&mlt_tputs_putchar);
}

original_code:
void mlt_clear_window(t_mlt *mlt_ptr)
{
 (void) mlt_ptr;
 tputs((tgetstr(("cl"), ((void *)0))), 1, &mlt_tputs_putchar);
}

canonical_original_code:
void mlt_clear_window(struct s_mlt *mlt_ptr)
{
 (void) mlt_ptr;
 tputs((tgetstr(("cl"), ((void *)0))), 1, &mlt_tputs_putchar);
}


decompiled_code:
int __fastcall mlt_restore_term(struct termios *termios_p)
{
char *v1; // rax
char *v2; // rax

v1 = tgetstr("te", 0LL);
tputs(v1, 1, (int (*)(int))&mlt_tputs_putchar);
v2 = tgetstr("ve", 0LL);
tputs(v2, 1, (int (*)(int))&mlt_tputs_putchar);
return tcsetattr(0, 0, termios_p);
}

canonical_decompiled_code:
int __fastcall func1(struct termios *termios_p)
{
char *v1; // rax
char *v2; // rax

v1 = tgetstr("te", 0LL);
tputs(v1, 1, (int (*)(int))&mlt_tputs_putchar);
v2 = tgetstr("ve", 0LL);
tputs(v2, 1, (int (*)(int))&mlt_tputs_putchar);
return tcsetattr(0, 0, termios_p);
}

original_code:
void mlt_restore_term(struct termios *original_thermos)
{
 tputs((tgetstr(("te"), ((void *)0))), 1, &mlt_tputs_putchar);
 tputs((tgetstr(("ve"), ((void *)0))), 1, &mlt_tputs_putchar);
 tcsetattr(0, 0, original_thermos);
}

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


decompiled_code:
int __fastcall mlt_recalc_win(__int64 a1)
{
return ioctl(0, 0x5413uLL, a1 + 120);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
return ioctl(0, 0x5413uLL, a1 + 120);
}

original_code:
void mlt_recalc_win(t_mlt *mlt_ptr)
{
 ioctl(0, 0x5413, &mlt_ptr->w);
}

canonical_original_code:
void mlt_recalc_win(struct s_mlt *mlt_ptr)
{
 ioctl(0, 0x5413, &mlt_ptr->w);
}


original_code:
t_mlt *mlt_init(void)
{
 t_mlt *mlt_ptr;
 char buff[2048];
 mlt_ptr = (t_mlt *) malloc(sizeof(t_mlt));
 tgetent(buff, getenv("TERM"));
 tcgetattr(0, &mlt_ptr->original_thermos);
 memcpy(&mlt_ptr->thermos,
     &mlt_ptr->original_thermos,
     sizeof(struct termios));
 ((mlt_ptr->thermos.c_lflag) = (mlt_ptr->thermos.c_lflag) & ~(0000002));
 ((mlt_ptr->thermos.c_lflag) = (mlt_ptr->thermos.c_lflag) & ~(0000010));
 mlt_ptr->thermos.c_cc[6] = 1;
 mlt_ptr->thermos.c_cc[5] = 0;
 tputs((tgetstr(("ti"), ((void *)0))), 1, &mlt_tputs_putchar);
 tcsetattr(0, 0, &mlt_ptr->thermos);
 mlt_recalc_win(mlt_ptr);
 mlt_init_signal(mlt_ptr);
 return (mlt_ptr);
}

canonical_original_code:
struct s_mlt *mlt_init()
{
 struct s_mlt *mlt_ptr;
 char buff[2048];
 mlt_ptr = (struct s_mlt *) malloc(sizeof(t_mlt));
 tgetent(buff, getenv("TERM"));
 tcgetattr(0, &mlt_ptr->original_thermos);
 memcpy(&mlt_ptr->thermos,
     &mlt_ptr->original_thermos,
     sizeof(struct termios));
 ((mlt_ptr->thermos.c_lflag) = (mlt_ptr->thermos.c_lflag) & ~(0000002));
 ((mlt_ptr->thermos.c_lflag) = (mlt_ptr->thermos.c_lflag) & ~(0000010));
 mlt_ptr->thermos.c_cc[6] = 1;
 mlt_ptr->thermos.c_cc[5] = 0;
 tputs((tgetstr(("ti"), ((void *)0))), 1, &mlt_tputs_putchar);
 tcsetattr(0, 0, &mlt_ptr->thermos);
 mlt_recalc_win(mlt_ptr);
 mlt_init_signal(mlt_ptr);
 return (mlt_ptr);
}

variable_types:
buff
char [2048]

return_type: struct s_mlt { struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } thermos; struct termios original_thermos; struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; } w; } *


decompiled_code:
void __fastcall __noreturn mlt_exit(void *ptr)
{
mlt_restore_term((struct termios *)ptr + 1);
free(ptr);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *ptr)
{
func1((struct termios *)ptr + 1);
free(ptr);
exit(0);
}

original_code:
void mlt_exit(t_mlt *mlt_ptr)
{
 mlt_restore_term(&mlt_ptr->original_thermos);
 free(mlt_ptr);
 exit(0);
}

canonical_original_code:
void mlt_exit(struct s_mlt *mlt_ptr)
{
 mlt_restore_term(&mlt_ptr->original_thermos);
 free(mlt_ptr);
 exit(0);
}


decompiled_code:
void f_do_nothing()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void f_do_nothing(void)
{
}

canonical_original_code:
void f_do_nothing()
{
}


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

result = fd_0;
if ( fd_0 == -1 )
{
result = open("/dev/tty", 1);
fd_0 = result;
}
return result;
}

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

result = fd_0;
if ( fd_0 == -1 )
{
result = open("/dev/tty", 1);
fd_0 = result;
}
return result;
}

original_code:
int mlt_spe_out(void)
{
 static int fd = -1;
 if (fd == -1)
  fd = open("/dev/tty", 01);
 return (fd);
}

canonical_original_code:
int mlt_spe_out()
{
 static int fd = -1;
 if (fd == -1)
  fd = open("/dev/tty", 01);
 return (fd);
}

variable_types: fd int


decompiled_code:
__int64 __fastcall mlt_tputs_putchar(int a1)
{
int v1; // eax
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
v1 = mlt_spe_out();
write(v1, buf, 1uLL);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // eax
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
v1 = func1();
write(v1, buf, 1uLL);
return 1LL;
}

original_code:
int mlt_tputs_putchar(int c)
{
 write(mlt_spe_out(), &c, 1);
 return (1);
}

variable_types: c int


repo: skyatua/serial

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
ssize_t v5; // rax
int v7[8]; // [rsp+0h] [rbp-168h] BYREF
struct termios termios_p; // [rsp+20h] [rbp-148h] BYREF
char buf[264]; // [rsp+60h] [rbp-108h] BYREF

v3 = open("/dev/ttyAMA0", 2306, envp);
v4 = v3;
if ( v3 == -1 )
{
puts("Error - Unable to open UART. Ensure it is not in use by another application");
tcgetattr(-1, &termios_p);
cfsetispeed(&termios_p, 0xDu);
cfsetospeed(&termios_p, 0xDu);
*(_QWORD *)&termios_p.c_iflag = 260LL;
termios_p.c_cflag = 2237;
tcflush(-1, 0);
tcsetattr(-1, 0, &termios_p);
qmemcpy(v7, "Hello", 5);
}
else
{
tcgetattr(v3, &termios_p);
cfsetispeed(&termios_p, 0xDu);
cfsetospeed(&termios_p, 0xDu);
*(_QWORD *)&termios_p.c_iflag = 260LL;
termios_p.c_cflag = 2237;
tcflush(v4, 0);
tcsetattr(v4, 0, &termios_p);
qmemcpy(v7, "Hello", 5);
if ( (int)write(v4, v7, 5uLL) < 0 )
puts("UART TX error");
v5 = read(v4, buf, 0xFFuLL);
if ( (int)v5 < 0 )
{
puts("UART RX error");
}
else if ( (_DWORD)v5 )
{
buf[(int)v5] = 0;
printf("%i bytes read : %s\n", v5, buf);
}
else
{
puts("no data UART RX test commit");
}
}
close(v4);
return 0;
}

original_code:
int main(int argv, char * argc[])
{
 int uart0_filestream = -1;
 uart0_filestream = open("/dev/ttyAMA0", 02 | 0400 | 04000);
 if (uart0_filestream == -1)
 {
  printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
 }
 struct termios options;
 tcgetattr(uart0_filestream, &options);
 cfsetispeed(&options, 0000015);
 cfsetospeed(&options, 0000015);
 options.c_cflag = 0000015 | 0000060 | 0004000 | 0000200;
 options.c_iflag = 0000004 | 0000400;
 options.c_oflag = 0;
 tcflush(uart0_filestream, 0);
 tcsetattr(uart0_filestream, 0, &options);
 unsigned char tx_buffer[20];
 unsigned char *p_tx_buffer;
 p_tx_buffer = &tx_buffer[0];
 *p_tx_buffer++ = 'H';
 *p_tx_buffer++ = 'e';
 *p_tx_buffer++ = 'l';
 *p_tx_buffer++ = 'l';
 *p_tx_buffer++ = 'o';
 if (uart0_filestream != -1)
 {
  int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
  if (count < 0)
  {
   printf("UART TX error\n");
  }
 }
 if (uart0_filestream != -1)
 {
  unsigned char rx_buffer[256];
  int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
  if (rx_length < 0)
  {
   printf("UART RX error\n");
  }
  else if (rx_length == 0)
  {
    printf("no data UART RX   test commit\n");
  }
  else
  {
   rx_buffer[rx_length] = '\0';
   printf("%i bytes read : %s\n", rx_length, rx_buffer);
  }
 }
 close(uart0_filestream);
  return 0;
}

canonical_original_code:
int main(int argv, char *argc[])
{
 int uart0_filestream = -1;
 uart0_filestream = open("/dev/ttyAMA0", 02 | 0400 | 04000);
 if (uart0_filestream == -1)
 {
  printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
 }
 struct termios options;
 tcgetattr(uart0_filestream, &options);
 cfsetispeed(&options, 0000015);
 cfsetospeed(&options, 0000015);
 options.c_cflag = 0000015 | 0000060 | 0004000 | 0000200;
 options.c_iflag = 0000004 | 0000400;
 options.c_oflag = 0;
 tcflush(uart0_filestream, 0);
 tcsetattr(uart0_filestream, 0, &options);
 unsigned char tx_buffer[20];
 unsigned char *p_tx_buffer;
 p_tx_buffer = &tx_buffer[0];
 *p_tx_buffer++ = 'H';
 *p_tx_buffer++ = 'e';
 *p_tx_buffer++ = 'l';
 *p_tx_buffer++ = 'l';
 *p_tx_buffer++ = 'o';
 if (uart0_filestream != -1)
 {
  int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
  if (count < 0)
  {
   printf("UART TX error\n");
  }
 }
 if (uart0_filestream != -1)
 {
  unsigned char rx_buffer[256];
  int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
  if (rx_length < 0)
  {
   printf("UART RX error\n");
  }
  else if (rx_length == 0)
  {
    printf("no data UART RX   test commit\n");
  }
  else
  {
   rx_buffer[rx_length] = '\0';
   printf("%i bytes read : %s\n", rx_length, rx_buffer);
  }
 }
 close(uart0_filestream);
  return 0;
}

variable_types: argv int

variable_types:
argc
char *[]

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

variable_types: count int


decompiled_code:
__sighandler_t __fastcall sigh_install(int a1)
{
__sighandler_t result; // rax

result = _sysv_signal(a1, sigh);
if ( result == (__sighandler_t)-1LL )
{
fwrite("Phailed to install signal handler!\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__sighandler_t __fastcall func0(int a1)
{
__sighandler_t result; // rax

result = _sysv_signal(a1, sigh);
if ( result == (__sighandler_t)-1LL )
{
fwrite("Phailed to install signal handler!\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

original_code:
static void sigh_install(int signo)
{
 if (signal(signo, sigh) == ((__sighandler_t) -1))
 {
  fputs("Phailed to install signal handler!\n", stderr);
  exit(-1);
 }
}

variable_types: signo int


cleanup

decompiled_code:
__int64 cleanup()
{
fwrite("Cleaning up!\n", 1uLL, 0xDuLL, stderr);
ui_fini(&teh_ui);
g_fini(&teh_game);
r_fini();
return sdl_fini_isra_0();
}

canonical_decompiled_code:
__int64 func4()
{
fwrite("Cleaning up!\n", 1uLL, 0xDuLL, stderr);
ui_fini(&teh_ui);
g_fini(&teh_game);
r_fini();
return func3();
}

original_code:
static void cleanup()
{
 fprintf(stderr, "Cleaning up!\n");
 ui_fini(&teh_ui);
 g_fini(&teh_game);
 r_fini();
 sdl_fini();
}