decompiled_code:
__int64 __fastcall eloop_event_add(int a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax
_DWORD *v6; // rbp
__int64 v7; // rax
_QWORD *v8; // rdx
__int64 v9; // rax
__int64 v10; // r13

v3 = events;
if ( events )
{
while ( *(_DWORD *)(v3 + 16) != a1 )
{
v3 = *(_QWORD *)v3;
if ( !v3 )
goto LABEL_6;
}
*(_QWORD *)(v3 + 24) = a2;
*(_QWORD *)(v3 + 32) = a3;
result = 0LL;
}
else
{
LABEL_6:
v6 = free_events;
if ( free_events )
{
v7 = *(_QWORD *)free_events;
v8 = (_QWORD *)*((_QWORD *)free_events + 1);
if ( *(_QWORD *)free_events )
*(_QWORD *)(v7 + 8) = v8;
else
off_898 = *((_QWORD *)free_events + 1);
*v8 = v7;
}
else
{
v6 = malloc(0x30uLL);
if ( !v6 )
{
syslog(3, "%s: %m", "eloop_event_add");
return 0xFFFFFFFFLL;
}
}
if ( ++events_len <= fds_len
|| (fds_len += 5LL, v10 = fds_len, free(fds), (fds = (struct pollfd *)malloc(8 * v10)) != 0LL) )
{
v9 = events;
v6[4] = a1;
*((_QWORD *)v6 + 3) = a2;
*((_QWORD *)v6 + 4) = a3;
*(_QWORD *)v6 = v9;
if ( v9 )
*(_QWORD *)(v9 + 8) = v6;
else
off_8A8 = (__int64 *)v6;
*((_QWORD *)v6 + 1) = &events;
events = (__int64)v6;
eloop_event_setup_fds();
result = 0LL;
}
else
{
syslog(3, "%s: %m", "eloop_event_add");
free(v6);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax
_DWORD *v6; // rbp
__int64 v7; // rax
_QWORD *v8; // rdx
__int64 v9; // rax
__int64 v10; // r13

v3 = events;
if ( events )
{
while ( *(_DWORD *)(v3 + 16) != a1 )
{
v3 = *(_QWORD *)v3;
if ( !v3 )
goto LABEL_6;
}
*(_QWORD *)(v3 + 24) = a2;
*(_QWORD *)(v3 + 32) = a3;
result = 0LL;
}
else
{
LABEL_6:
v6 = free_events;
if ( free_events )
{
v7 = *(_QWORD *)free_events;
v8 = (_QWORD *)*((_QWORD *)free_events + 1);
if ( *(_QWORD *)free_events )
*(_QWORD *)(v7 + 8) = v8;
else
off_898 = *((_QWORD *)free_events + 1);
*v8 = v7;
}
else
{
v6 = malloc(0x30uLL);
if ( !v6 )
{
syslog(3, "%s: %m", "eloop_event_add");
return 0xFFFFFFFFLL;
}
}
if ( ++events_len <= fds_len
|| (fds_len += 5LL, v10 = fds_len, free(fds), (fds = (struct pollfd *)malloc(8 * v10)) != 0LL) )
{
v9 = events;
v6[4] = a1;
*((_QWORD *)v6 + 3) = a2;
*((_QWORD *)v6 + 4) = a3;
*(_QWORD *)v6 = v9;
if ( v9 )
*(_QWORD *)(v9 + 8) = v6;
else
off_8A8 = (__int64 *)v6;
*((_QWORD *)v6 + 1) = &events;
events = (__int64)v6;
func0();
result = 0LL;
}
else
{
syslog(3, "%s: %m", "eloop_event_add");
free(v6);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int
eloop_event_add(int fd, void (*callback)(void *), void *arg)
{
 struct event *e;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  if (e->fd == fd) {
   e->callback = callback;
   e->arg = arg;
   return 0;
  }
 }
 if ((e = ((&free_events)->tqh_first))) {
  do { if (((e)->next.tqe_next) != ((void *)0)) (e)->next.tqe_next->next.tqe_prev = (e)->next.tqe_prev; else (&free_events)->tqh_last = (e)->next.tqe_prev; *(e)->next.tqe_prev = (e)->next.tqe_next; } while ( 0);
 } else {
  e = malloc(sizeof(*e));
  if (e == ((void *)0)) {
   syslog(3, "%s: %m", __func__);
   return -1;
  }
 }
 events_len++;
 if (events_len > fds_len) {
  fds_len += 5;
  free(fds);
  fds = malloc(sizeof(*fds) * fds_len);
  if (fds == ((void *)0)) {
   syslog(3, "%s: %m", __func__);
   free(e);
   return -1;
  }
 }
 e->fd = fd;
 e->callback = callback;
 e->arg = arg;
 do { if (((e)->next.tqe_next = (&events)->tqh_first) != ((void *)0)) (&events)->tqh_first->next.tqe_prev = &(e)->next.tqe_next; else (&events)->tqh_last = &(e)->next.tqe_next; (&events)->tqh_first = (e); (e)->next.tqe_prev = &(&events)->tqh_first; } while ( 0);
 eloop_event_setup_fds();
 return 0;
}

variable_types: fd int

variable_types:
callback
void (*)(void *)

variable_types:
arg
void *

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


repo: sklvjz/network

original_code:
void
eloop_event_delete(int fd)
{
 struct event *e;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  if (e->fd == fd) {
   do { if (((e)->next.tqe_next) != ((void *)0)) (e)->next.tqe_next->next.tqe_prev = (e)->next.tqe_prev; else (&events)->tqh_last = (e)->next.tqe_prev; *(e)->next.tqe_prev = (e)->next.tqe_next; } while ( 0);
   do { (e)->next.tqe_next = ((void *)0); (e)->next.tqe_prev = (&free_events)->tqh_last; *(&free_events)->tqh_last = (e); (&free_events)->tqh_last = &(e)->next.tqe_next; } while ( 0);
   events_len--;
   eloop_event_setup_fds();
   break;
  }
 }
}

variable_types: struct event { struct { struct event *tqe_next; struct event **tqe_prev; } next; int fd; void (*callback)(void *); void *arg; struct pollfd { int fd; short events; short revents; } *pollfd; } *

return_type: void

user_defined_types: struct event { struct { struct event *tqe_next; struct event **tqe_prev; } next; int fd; void (*callback)(void *); void *arg; struct pollfd *pollfd; }


original_code:
int
eloop_q_timeout_add_tv(int queue,
    const struct timeval *when, void (*callback)(void *), void *arg)
{
 struct timeval w;
 struct timeout *t, *tt = ((void *)0);
 get_monotonic(&now);
 do { (&w)->tv_sec = (&now)->tv_sec + (when)->tv_sec; (&w)->tv_usec = (&now)->tv_usec + (when)->tv_usec; if ((&w)->tv_usec >= 1000000) { ++(&w)->tv_sec; (&w)->tv_usec -= 1000000; } } while (0);
 if ((((&w)->tv_sec == (&now)->tv_sec) ? ((&w)->tv_usec < (&now)->tv_usec) : ((&w)->tv_sec < (&now)->tv_sec))) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for ((t) = ((&timeouts)->tqh_first); (t); (t) = ((t)->next.tqe_next)) {
  if (t->callback == callback && t->arg == arg) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
   break;
  }
 }
 if (t == ((void *)0)) {
  if ((t = ((&free_timeouts)->tqh_first))) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&free_timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
  } else {
   t = malloc(sizeof(*t));
   if (t == ((void *)0)) {
    syslog(3, "%s: %m", __func__);
    return -1;
   }
  }
 }
 t->when.tv_sec = w.tv_sec;
 t->when.tv_usec = w.tv_usec;
 t->callback = callback;
 t->arg = arg;
 t->queue = queue;
 for ((tt) = ((&timeouts)->tqh_first); (tt); (tt) = ((tt)->next.tqe_next)) {
  if ((((&t->when)->tv_sec == (&tt->when)->tv_sec) ? ((&t->when)->tv_usec < (&tt->when)->tv_usec) : ((&t->when)->tv_sec < (&tt->when)->tv_sec))) {
   do { (t)->next.tqe_prev = (tt)->next.tqe_prev; (t)->next.tqe_next = (tt); *(tt)->next.tqe_prev = (t); (tt)->next.tqe_prev = &(t)->next.tqe_next; } while ( 0);
   return 0;
  }
 }
 do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&timeouts)->tqh_last; *(&timeouts)->tqh_last = (t); (&timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
 return 0;
}

canonical_original_code:
int eloop_q_timeout_add_tv(int queue, struct timeval *when, void (*callback)(void *), void *arg)
{
 struct timeval w;
 struct timeout *t; struct timeout *tt = ((void *)0);
 get_monotonic(&now);
 do { (&w)->tv_sec = (&now)->tv_sec + (when)->tv_sec; (&w)->tv_usec = (&now)->tv_usec + (when)->tv_usec; if ((&w)->tv_usec >= 1000000) { ++(&w)->tv_sec; (&w)->tv_usec -= 1000000; } } while (0);
 if ((((&w)->tv_sec == (&now)->tv_sec) ? ((&w)->tv_usec < (&now)->tv_usec) : ((&w)->tv_sec < (&now)->tv_sec))) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for ((t) = ((&timeouts)->tqh_first); (t); (t) = ((t)->next.tqe_next)) {
  if (t->callback == callback && t->arg == arg) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
   break;
  }
 }
 if (t == ((void *)0)) {
  if ((t = ((&free_timeouts)->tqh_first))) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&free_timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
  } else {
   t = malloc(sizeof(*t));
   if (t == ((void *)0)) {
    syslog(3, "%s: %m", __func__);
    return -1;
   }
  }
 }
 t->when.tv_sec = w.tv_sec;
 t->when.tv_usec = w.tv_usec;
 t->callback = callback;
 t->arg = arg;
 t->queue = queue;
 for ((tt) = ((&timeouts)->tqh_first); (tt); (tt) = ((tt)->next.tqe_next)) {
  if ((((&t->when)->tv_sec == (&tt->when)->tv_sec) ? ((&t->when)->tv_usec < (&tt->when)->tv_usec) : ((&t->when)->tv_sec < (&tt->when)->tv_sec))) {
   do { (t)->next.tqe_prev = (tt)->next.tqe_prev; (t)->next.tqe_next = (tt); *(tt)->next.tqe_prev = (t); (tt)->next.tqe_prev = &(t)->next.tqe_next; } while ( 0);
   return 0;
  }
 }
 do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&timeouts)->tqh_last; *(&timeouts)->tqh_last = (t); (&timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
 return 0;
}

variable_types: queue int

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

variable_types: struct timeout { struct { struct timeout *tqe_next; struct timeout **tqe_prev; } next; struct timeval { long tv_sec; long tv_usec; } when; void (*callback)(void *); void *arg; int queue; } *

return_type: int

user_defined_types: struct timeout { struct { struct timeout *tqe_next; struct timeout **tqe_prev; } next; struct timeval when; void (*callback)(void *); void *arg; int queue; }


decompiled_code:
__int64 __fastcall eloop_q_timeout_add_sec(int a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v5[3]; // [rsp+0h] [rbp-18h] BYREF

v5[0] = a2;
v5[1] = 0LL;
return eloop_q_timeout_add_tv(a1, v5, a3, a4);
}

original_code:
int
eloop_q_timeout_add_sec(int queue, time_t when,
    void (*callback)(void *), void *arg)
{
 struct timeval tv;
 tv.tv_sec = when;
 tv.tv_usec = 0;
 return eloop_q_timeout_add_tv(queue, &tv, callback, arg);
}

canonical_original_code:
int eloop_q_timeout_add_sec(int queue, long when, void (*callback)(void *), void *arg)
{
 struct timeval tv;
 tv.tv_sec = when;
 tv.tv_usec = 0;
 return eloop_q_timeout_add_tv(queue, &tv, callback, arg);
}

variable_types: when long

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


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

if ( timeout0 )
{
syslog(4, "%s: timeout0 already set", "eloop_timeout_add_now");
result = eloop_q_timeout_add_sec(0, 0LL, a1, a2);
}
else
{
timeout0 = a1;
result = 0LL;
timeout0_arg = a2;
}
return result;
}

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

if ( timeout0 )
{
syslog(4, "%s: timeout0 already set", "eloop_timeout_add_now");
result = func4(0, 0LL, a1, a2);
}
else
{
timeout0 = a1;
result = 0LL;
timeout0_arg = a2;
}
return result;
}

original_code:
int
eloop_timeout_add_now(void (*callback)(void *), void *arg)
{
 if (timeout0 != ((void *)0)) {
  syslog(4, "%s: timeout0 already set", __func__);
  return eloop_q_timeout_add_sec(0, 0, callback, arg);
 }
 timeout0 = callback;
 timeout0_arg = arg;
 return 0;
}


decompiled_code:
_QWORD *eloop_q_timeouts_delete(int a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return eloop_q_timeouts_delete_v(a1, a2, a3, (const __m128i *)va);
}

canonical_decompiled_code:
_QWORD *func6(int a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return eloop_q_timeouts_delete_v(a1, a2, a3, (const __m128i *)va);
}

original_code:
void
eloop_q_timeouts_delete(int queue, void *arg, void (*callback)(void *), ...)
{
 va_list va;
 __builtin_va_start(va,callback);
 eloop_q_timeouts_delete_v(queue, arg, callback, va);
 __builtin_va_end(va);
}

canonical_original_code:
void eloop_q_timeouts_delete(int queue, void *arg, void (*callback)(void *), ...)
{
 __builtin_va_list va;
 __builtin_va_start(va,callback);
 eloop_q_timeouts_delete_v(queue, arg, callback, va);
 __builtin_va_end(va);
}


original_code:
__attribute__((__noreturn__)) void
eloop_start(const sigset_t *sigmask)
{
 int n;
 struct event *e;
 struct timeout *t;
 struct timeval tv;
 struct timespec ts, *tsp;
 void (*t0)(void *);
 for (;;) {
  if (timeout0) {
   t0 = timeout0;
   timeout0 = ((void *)0);
   t0(timeout0_arg);
   continue;
  }
  if ((t = ((&timeouts)->tqh_first))) {
   get_monotonic(&now);
   if ((((&now)->tv_sec == (&t->when)->tv_sec) ? ((&now)->tv_usec > (&t->when)->tv_usec) : ((&now)->tv_sec > (&t->when)->tv_sec))) {
    do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
    t->callback(t->arg);
    do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&free_timeouts)->tqh_last; *(&free_timeouts)->tqh_last = (t); (&free_timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
    continue;
   }
   do { (&tv)->tv_sec = (&t->when)->tv_sec - (&now)->tv_sec; (&tv)->tv_usec = (&t->when)->tv_usec - (&now)->tv_usec; if ((&tv)->tv_usec < 0) { --(&tv)->tv_sec; (&tv)->tv_usec += 1000000; } } while (0);
   { (&ts)->tv_sec = (&tv)->tv_sec; (&ts)->tv_nsec = (&tv)->tv_usec * 1000; };
   tsp = &ts;
  } else
   tsp = ((void *)0);
  if (tsp == ((void *)0) && events_len == 0) {
   syslog(3, "nothing to do");
   exit(1);
  }
  n = pollts(fds, events_len, tsp, sigmask);
  if (n == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   syslog(3, "poll: %m");
   exit(1);
  }
  if (n > 0) {
   for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
    if (e->pollfd->revents & (0x001 | 0x010)) {
     e->callback(e->arg);
     break;
    }
   }
  }
 }
}

canonical_original_code:
void eloop_start(struct __sigset_t *sigmask)
{
 int n;
 struct event *e;
 struct timeout *t;
 struct timeval tv;
 struct timespec ts; struct timespec *tsp;
 void (*t0)(void *);
 for (;;) {
  if (timeout0) {
   t0 = timeout0;
   timeout0 = ((void *)0);
   t0(timeout0_arg);
   continue;
  }
  if ((t = ((&timeouts)->tqh_first))) {
   get_monotonic(&now);
   if ((((&now)->tv_sec == (&t->when)->tv_sec) ? ((&now)->tv_usec > (&t->when)->tv_usec) : ((&now)->tv_sec > (&t->when)->tv_sec))) {
    do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
    t->callback(t->arg);
    do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&free_timeouts)->tqh_last; *(&free_timeouts)->tqh_last = (t); (&free_timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
    continue;
   }
   do { (&tv)->tv_sec = (&t->when)->tv_sec - (&now)->tv_sec; (&tv)->tv_usec = (&t->when)->tv_usec - (&now)->tv_usec; if ((&tv)->tv_usec < 0) { --(&tv)->tv_sec; (&tv)->tv_usec += 1000000; } } while (0);
   { (&ts)->tv_sec = (&tv)->tv_sec; (&ts)->tv_nsec = (&tv)->tv_usec * 1000; };
   tsp = &ts;
  } else
   tsp = ((void *)0);
  if (tsp == ((void *)0) && events_len == 0) {
   syslog(3, "nothing to do");
   exit(1);
  }
  n = pollts(fds, events_len, tsp, sigmask);
  if (n == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   syslog(3, "poll: %m");
   exit(1);
  }
  if (n > 0) {
   for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
    if (e->pollfd->revents & (0x001 | 0x010)) {
     e->callback(e->arg);
     break;
    }
   }
  }
 }
}

variable_types: struct __sigset_t { unsigned long __val[-1]; } *

variable_types: n int

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

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


decompiled_code:
int control_handle()
{
_DWORD *v0; // rax
unsigned int v1; // ebp
_QWORD *v2; // rdx
void *v3; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
LODWORD(v0) = accept(fd, &addr, &addr_len);
if ( (_DWORD)v0 != -1 )
{
v1 = (unsigned int)v0;
set_cloexec((unsigned int)v0);
v0 = malloc(0x10uLL);
v2 = v0;
if ( v0 )
{
*v0 = v1;
v0[1] = 0;
v3 = control_fds;
control_fds = v2;
v2[1] = v3;
LODWORD(v0) = eloop_event_add(v1, control_handle_data);
}
}
return (int)v0;
}

canonical_decompiled_code:
int func0()
{
_DWORD *v0; // rax
unsigned int v1; // ebp
_QWORD *v2; // rdx
void *v3; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
LODWORD(v0) = accept(fd, &addr, &addr_len);
if ( (_DWORD)v0 != -1 )
{
v1 = (unsigned int)v0;
set_cloexec((unsigned int)v0);
v0 = malloc(0x10uLL);
v2 = v0;
if ( v0 )
{
*v0 = v1;
v0[1] = 0;
v3 = control_fds;
control_fds = v2;
v2[1] = v3;
LODWORD(v0) = eloop_event_add(v1, control_handle_data);
}
}
return (int)v0;
}

original_code:
static void
control_handle(__attribute__((__unused__)) void *arg)
{
 struct sockaddr_un run;
 socklen_t len;
 struct fd_list *l;
 int f;
 len = sizeof(run);
 if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
  return;
 set_cloexec(f);
 l = malloc(sizeof(*l));
 if (l) {
  l->fd = f;
  l->listener = 0;
  l->next = control_fds;
  control_fds = l;
  eloop_event_add(l->fd, control_handle_data, l);
 }
}

canonical_original_code:
static void control_handle(void *arg)
{
 struct sockaddr_un run;
 unsigned int len;
 struct fd_list *l;
 int f;
 len = sizeof(run);
 if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
  return;
 set_cloexec(f);
 l = malloc(sizeof(*l));
 if (l) {
  l->fd = f;
  l->listener = 0;
  l->next = control_fds;
  control_fds = l;
  eloop_event_add(l->fd, control_handle_data, l);
 }
}

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

variable_types:
len
unsigned int

variable_types: struct fd_list { int fd; int listener; struct fd_list *next; } *

variable_types: f int

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


decompiled_code:
void __fastcall control_remove(unsigned int *a1)
{
unsigned int *v1; // rbx
unsigned int *i; // rbp

v1 = 0LL;
for ( i = (unsigned int *)control_fds; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
if ( i == a1 )
{
close(*i);
eloop_event_delete(*i);
if ( v1 )
*((_QWORD *)v1 + 1) = *((_QWORD *)i + 1);
else
control_fds = (void *)*((_QWORD *)i + 1);
free(i);
return;
}
v1 = i;
}
}

canonical_decompiled_code:
void __fastcall func1(unsigned int *a1)
{
unsigned int *v1; // rbx
unsigned int *i; // rbp

v1 = 0LL;
for ( i = (unsigned int *)control_fds; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
if ( i == a1 )
{
close(*i);
eloop_event_delete(*i);
if ( v1 )
*((_QWORD *)v1 + 1) = *((_QWORD *)i + 1);
else
control_fds = (void *)*((_QWORD *)i + 1);
free(i);
return;
}
v1 = i;
}
}

original_code:
static void
control_remove(void *arg)
{
 struct fd_list *l, *n, *last = ((void *)0);
 l = control_fds;
 while (l) {
  n = l->next;
  if (l == arg) {
   close(l->fd);
   eloop_event_delete(l->fd);
   if (last == ((void *)0))
    control_fds = l->next;
   else
    last->next = l->next;
   free(l);
   break;
  }
  last = l;
  l = n;
 }
}

canonical_original_code:
static void control_remove(void *arg)
{
 struct fd_list *l; struct fd_list *n; struct fd_list *last = ((void *)0);
 l = control_fds;
 while (l) {
  n = l->next;
  if (l == arg) {
   close(l->fd);
   eloop_event_delete(l->fd);
   if (last == ((void *)0))
    control_fds = l->next;
   else
    last->next = l->next;
   free(l);
   break;
  }
  last = l;
  l = n;
 }
}

user_defined_types: struct fd_list { int fd; int listener; struct fd_list *next; }


decompiled_code:
void __fastcall control_handle_data(int *a1)
{
ssize_t v1; // rax
char *v2; // r13
const char **v3; // r12
const char *v4; // rbx
unsigned int v5; // ebp

v1 = read(*a1, buffer, 0x3FFuLL);
if ( (unsigned __int64)(v1 + 1) <= 1 )
{
control_remove((unsigned int *)a1);
}
else
{
v2 = &buffer[v1];
buffer[v1] = 0;
if ( &buffer[v1] <= buffer )
{
v5 = 0;
}
else
{
v3 = (const char **)&argvp;
v4 = buffer;
v5 = 0;
do
{
*v3++ = v4;
++v5;
v4 += strlen(v4) + 1;
}
while ( v2 > v4 && v5 <= 0x7F7 );
}
handle_args(a1, v5, &argvp);
}
}

canonical_decompiled_code:
void __fastcall func2(int *a1)
{
ssize_t v1; // rax
char *v2; // r13
const char **v3; // r12
const char *v4; // rbx
unsigned int v5; // ebp

v1 = read(*a1, buffer, 0x3FFuLL);
if ( (unsigned __int64)(v1 + 1) <= 1 )
{
func1((unsigned int *)a1);
}
else
{
v2 = &buffer[v1];
buffer[v1] = 0;
if ( &buffer[v1] <= buffer )
{
v5 = 0;
}
else
{
v3 = (const char **)&argvp;
v4 = buffer;
v5 = 0;
do
{
*v3++ = v4;
++v5;
v4 += strlen(v4) + 1;
}
while ( v2 > v4 && v5 <= 0x7F7 );
}
handle_args(a1, v5, &argvp);
}
}

original_code:
static void
control_handle_data(void *arg)
{
 struct fd_list *l = arg;
 ssize_t bytes;
 int argc;
 char *e, *p;
 char **ap;
 bytes = read(l->fd, buffer, sizeof(buffer) - 1);
 if (bytes == -1 || bytes == 0) {
  control_remove(l);
  return;
 }
 buffer[bytes] = '\0';
 p = buffer;
 e = buffer + bytes;
 argc = 0;
 ap = argvp;
 while (p < e && (size_t)argc < sizeof(argvp)) {
  argc++;
  *ap++ = p;
  p += strlen(p) + 1;
 }
 handle_args(l, argc, argvp);
}

canonical_original_code:
static void control_handle_data(void *arg)
{
 struct fd_list *l = arg;
 long bytes;
 int argc;
 char *e; char *p;
 char **ap;
 bytes = read(l->fd, buffer, sizeof(buffer) - 1);
 if (bytes == -1 || bytes == 0) {
  control_remove(l);
  return;
 }
 buffer[bytes] = '\0';
 p = buffer;
 e = buffer + bytes;
 argc = 0;
 ap = argvp;
 while (p < e && (unsigned long)argc < sizeof(argvp)) {
  argc++;
  *ap++ = p;
  p += strlen(p) + 1;
 }
 handle_args(l, argc, argvp);
}

variable_types: bytes long

variable_types: argc int

variable_types:
e
char *

variable_types:
p
char *

variable_types:
ap
char **


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

result = socket(1, 1, 0);
fd = result;
if ( result != -1 )
{
memset(&sun, 0, 0x68uLL);
sun.sa_family = 1;
*((_DWORD *)&sun + 26) = 0;
word_50C = 0;
strlcpy(sun.sa_data, "/var/run/dhcpcd.sock", 108LL);
result = strlen(sun.sa_data) + 3;
}
return result;
}

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

result = socket(1, 1, 0);
fd = result;
if ( result != -1 )
{
memset(&sun, 0, 0x68uLL);
sun.sa_family = 1;
*((_DWORD *)&sun + 26) = 0;
word_50C = 0;
strlcpy(sun.sa_data, "/var/run/dhcpcd.sock", 108LL);
result = strlen(sun.sa_data) + 3;
}
return result;
}

original_code:
static int
make_sock(void)
{
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  return -1;
 memset(&sun, 0, sizeof(sun));
 sun.sun_family = 1;
 strlcpy(sun.sun_path, "/var/run" "/" "dhcpcd" ".sock", sizeof(sun.sun_path));
 return sizeof(sun.sun_family) + strlen(sun.sun_path) + 1;
}

canonical_original_code:
static int make_sock()
{
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  return -1;
 memset(&sun, 0, sizeof(sun));
 sun.sun_family = 1;
 strlcpy(sun.sun_path, "/var/run" "/" "dhcpcd" ".sock", sizeof(sun.sun_path));
 return sizeof(sun.sun_family) + strlen(sun.sun_path) + 1;
}


decompiled_code:
__int64 control_start()
{
unsigned int v0; // er12
unsigned int v1; // ebp
int v2; // eax

v0 = make_sock();
if ( v0 != -1 )
{
unlink("/var/run/dhcpcd.sock");
v1 = fd;
if ( bind(fd, &sun, v0) != -1 && chmod("/var/run/dhcpcd.sock", 0x1B0u) != -1 )
{
if ( (unsigned int)set_cloexec(v1) == -1 )
{
v1 = fd;
}
else
{
v2 = set_nonblock((unsigned int)fd);
v1 = fd;
if ( v2 != -1 && listen(fd, 8) != -1 )
{
eloop_event_add(v1, control_handle);
return (unsigned int)fd;
}
}
}
v0 = -1;
close(v1);
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // er12
unsigned int v1; // ebp
int v2; // eax

v0 = func3();
if ( v0 != -1 )
{
unlink("/var/run/dhcpcd.sock");
v1 = fd;
if ( bind(fd, &sun, v0) != -1 && chmod("/var/run/dhcpcd.sock", 0x1B0u) != -1 )
{
if ( (unsigned int)set_cloexec(v1) == -1 )
{
v1 = fd;
}
else
{
v2 = set_nonblock((unsigned int)fd);
v1 = fd;
if ( v2 != -1 && listen(fd, 8) != -1 )
{
eloop_event_add(v1, control_handle);
return (unsigned int)fd;
}
}
}
v0 = -1;
close(v1);
}
return v0;
}

original_code:
int
control_start(void)
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 unlink("/var/run" "/" "dhcpcd" ".sock");
 if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
     chmod("/var/run" "/" "dhcpcd" ".sock",
  0400 | 0200 | (0400 >> 3) | (0200 >> 3)) == -1 ||
     set_cloexec(fd) == -1 ||
     set_nonblock(fd) == -1 ||
     listen(fd, sizeof(control_fds)) == -1)
 {
  close(fd);
  return -1;
 }
 eloop_event_add(fd, control_handle, ((void *)0));
 return fd;
}

canonical_original_code:
int control_start()
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 unlink("/var/run" "/" "dhcpcd" ".sock");
 if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
     chmod("/var/run" "/" "dhcpcd" ".sock",
  0400 | 0200 | (0400 >> 3) | (0200 >> 3)) == -1 ||
     set_cloexec(fd) == -1 ||
     set_nonblock(fd) == -1 ||
     listen(fd, sizeof(control_fds)) == -1)
 {
  close(fd);
  return -1;
 }
 eloop_event_add(fd, control_handle, ((void *)0));
 return fd;
}

variable_types: len int


decompiled_code:
__int64 control_stop()
{
int v0; // eax
int v1; // ebp
unsigned int v2; // er12
__int64 v3; // rdi
int *v4; // rbp

eloop_event_delete((unsigned int)fd);
v0 = shutdown(fd, 2);
fd = -1;
v1 = v0;
v2 = unlink("/var/run/dhcpcd.sock");
if ( v2 != -1 )
v2 = v1 == -1;
while ( 1 )
{
v4 = (int *)control_fds;
if ( !control_fds )
break;
v3 = *(unsigned int *)control_fds;
control_fds = (void *)*((_QWORD *)control_fds + 1);
eloop_event_delete(v3);
shutdown(*v4, 2);
free(v4);
}
return v2;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // eax
int v1; // ebp
unsigned int v2; // er12
__int64 v3; // rdi
int *v4; // rbp

eloop_event_delete((unsigned int)fd);
v0 = shutdown(fd, 2);
fd = -1;
v1 = v0;
v2 = unlink("/var/run/dhcpcd.sock");
if ( v2 != -1 )
v2 = v1 == -1;
while ( 1 )
{
v4 = (int *)control_fds;
if ( !control_fds )
break;
v3 = *(unsigned int *)control_fds;
control_fds = (void *)*((_QWORD *)control_fds + 1);
eloop_event_delete(v3);
shutdown(*v4, 2);
free(v4);
}
return v2;
}

original_code:
int
control_stop(void)
{
 int retval = 0;
 struct fd_list *l;
 eloop_event_delete(fd);
 if (shutdown(fd, SHUT_RDWR) == -1)
  retval = 1;
 fd = -1;
 if (unlink("/var/run" "/" "dhcpcd" ".sock") == -1)
  retval = -1;
 l = control_fds;
 while (l != ((void *)0)) {
  control_fds = l->next;
  eloop_event_delete(l->fd);
  shutdown(l->fd, SHUT_RDWR);
  free(l);
  l = control_fds;
 }
 return retval;
}

canonical_original_code:
int control_stop()
{
 int retval = 0;
 struct fd_list *l;
 eloop_event_delete(fd);
 if (shutdown(fd, SHUT_RDWR) == -1)
  retval = 1;
 fd = -1;
 if (unlink("/var/run" "/" "dhcpcd" ".sock") == -1)
  retval = -1;
 l = control_fds;
 while (l != ((void *)0)) {
  control_fds = l->next;
  eloop_event_delete(l->fd);
  shutdown(l->fd, SHUT_RDWR);
  free(l);
  l = control_fds;
 }
 return retval;
}

variable_types: retval int


decompiled_code:
int control_open()
{
int v0; // eax
int result; // eax

v0 = make_sock();
if ( v0 == -1 )
result = -1;
else
result = connect(fd, &sun, v0);
return result;
}

canonical_decompiled_code:
int func6()
{
int v0; // eax
int result; // eax

v0 = func3();
if ( v0 == -1 )
result = -1;
else
result = connect(fd, &sun, v0);
return result;
}

original_code:
int
control_open(void)
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 return connect(fd, (struct sockaddr *)&sun, len);
}

canonical_original_code:
int control_open()
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 return connect(fd, (struct sockaddr *)&sun, len);
}


decompiled_code:
ssize_t __fastcall control_send(int a1, const char **a2)
{
size_t v2; // rdx
const char **v3; // rbp
char *v4; // r12
const char *v5; // r14
size_t v6; // rbx

if ( a1 > 255 )
{
LABEL_6:
*_errno_location() = 105;
return 0xFFFFFFFFLL;
}
v2 = 0LL;
if ( a1 > 0 )
{
v3 = a2;
v4 = buffer;
while ( 1 )
{
v5 = *v3;
v6 = strlen(*v3) + 1;
if ( v6 + v4 - buffer > 0x400 )
break;
++v3;
memcpy(v4, v5, v6);
v4 += v6;
if ( &a2[(unsigned int)(a1 - 1) + 1] == v3 )
{
v2 = v4 - buffer;
return write(fd, buffer, v2);
}
}
goto LABEL_6;
}
return write(fd, buffer, v2);
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, const char **a2)
{
size_t v2; // rdx
const char **v3; // rbp
char *v4; // r12
const char *v5; // r14
size_t v6; // rbx

if ( a1 > 255 )
{
LABEL_6:
*_errno_location() = 105;
return 0xFFFFFFFFLL;
}
v2 = 0LL;
if ( a1 > 0 )
{
v3 = a2;
v4 = buffer;
while ( 1 )
{
v5 = *v3;
v6 = strlen(*v3) + 1;
if ( v6 + v4 - buffer > 0x400 )
break;
++v3;
memcpy(v4, v5, v6);
v4 += v6;
if ( &a2[(unsigned int)(a1 - 1) + 1] == v3 )
{
v2 = v4 - buffer;
return write(fd, buffer, v2);
}
}
goto LABEL_6;
}
return write(fd, buffer, v2);
}

original_code:
int
control_send(int argc, char * const *argv)
{
 char *p = buffer;
 int i;
 size_t len;
 if (argc > 255) {
  (*__errno_location ()) = 105;
  return -1;
 }
 for (i = 0; i < argc; i++) {
  len = strlen(argv[i]) + 1;
  if ((p - buffer) + len > sizeof(buffer)) {
   (*__errno_location ()) = 105;
   return -1;
  }
  memcpy(p, argv[i], len);
  p += len;
 }
 return write(fd, buffer, p - buffer);
}

canonical_original_code:
int control_send(int argc, char **argv)
{
 char *p = buffer;
 int i;
 unsigned long len;
 if (argc > 255) {
  (*__errno_location ()) = 105;
  return -1;
 }
 for (i = 0; i < argc; i++) {
  len = strlen(argv[i]) + 1;
  if ((p - buffer) + len > sizeof(buffer)) {
   (*__errno_location ()) = 105;
   return -1;
  }
  memcpy(p, argv[i], len);
  p += len;
 }
 return write(fd, buffer, p - buffer);
}

variable_types:
argv
char **

variable_types: i int

variable_types:
len
unsigned long


serverSocket

decompiled_code:
__int64 __fastcall serverSocket(__int16 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int *v6; // rax
int optval; // [rsp+Ch] [rbp-4Ch] BYREF
char dest[16]; // [rsp+10h] [rbp-48h] BYREF
__int128 v9; // [rsp+20h] [rbp-38h]
__int64 v10; // [rsp+30h] [rbp-28h]

optval = 1;
v2 = socket(2, 2, 17);
if ( v2 == -1 )
return -1;
v3 = v2;
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
return -1;
v10 = 0LL;
v9 = 0LL;
strncpy(dest, a2, 0x10uLL);
if ( setsockopt(v3, 1, 25, dest, 0x28u) < 0 )
{
v6 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v6);
}
*(_OWORD *)dest = 0LL;
*(_WORD *)dest = 2;
*(_WORD *)&dest[2] = _bswap_16(a1);
if ( bind(v3, (const struct sockaddr *)dest, v4) == -1 )
return -1;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int *v6; // rax
int optval; // [rsp+Ch] [rbp-4Ch] BYREF
char dest[16]; // [rsp+10h] [rbp-48h] BYREF
__int128 v9; // [rsp+20h] [rbp-38h]
__int64 v10; // [rsp+30h] [rbp-28h]

optval = 1;
v2 = socket(2, 2, 17);
if ( v2 == -1 )
return -1;
v3 = v2;
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
return -1;
v10 = 0LL;
v9 = 0LL;
strncpy(dest, a2, 0x10uLL);
if ( setsockopt(v3, 1, 25, dest, 0x28u) < 0 )
{
v6 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v6);
}
*(_OWORD *)dest = 0LL;
*(_WORD *)dest = 2;
*(_WORD *)&dest[2] = func0(a1);
if ( bind(v3, (const struct sockaddr *)dest, v4) == -1 )
return -1;
return v3;
}

original_code:
int serverSocket(short listen_port, char *interface_name) {
        int server_socket;
        struct sockaddr_in server;
        int n = 1;
        server_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(server_socket == -1)
     return -1;
        if(setsockopt(server_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(server_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&server, sizeof(server));
        server.sin_family = 2;
        server.sin_port = htons(listen_port);
        server.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        if(bind(server_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
                return -1;
        return server_socket;
}

canonical_original_code:
int serverSocket(short listen_port, char *interface_name) {
        int server_socket;
        struct sockaddr_in server;
        int n = 1;
        server_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(server_socket == -1)
     return -1;
        if(setsockopt(server_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(server_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&server, sizeof(server));
        server.sin_family = 2;
        server.sin_port = htons(listen_port);
        server.sin_addr.s_addr = ((unsigned int) 0x00000000);
        if(bind(server_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
                return -1;
        return server_socket;
}

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

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

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

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


clientSocket

decompiled_code:
__int64 __fastcall clientSocket(__int16 a1, __int16 a2, const char *a3)
{
int v4; // eax
unsigned int v5; // er12
socklen_t v6; // edx
__int16 v7; // ax
socklen_t v8; // edx
int *v10; // rax
int optval; // [rsp+Ch] [rbp-5Ch] BYREF
char dest[16]; // [rsp+10h] [rbp-58h] BYREF
__int128 v13; // [rsp+20h] [rbp-48h]
__int64 v14; // [rsp+30h] [rbp-38h]

optval = 1;
v4 = socket(2, 2, 17);
if ( v4 == -1 )
return -1;
v5 = v4;
if ( setsockopt(v4, 1, 2, &optval, 4u) == -1 )
return -1;
setsockopt(v5, 1, 6, &optval, 4u);
v13 = 0LL;
v14 = 0LL;
strncpy(dest, a3, 0x10uLL);
if ( setsockopt(v5, 1, 25, dest, 0x28u) < 0 )
{
v10 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v10);
}
*(_OWORD *)dest = 0LL;
*(_WORD *)dest = 2;
*(_WORD *)&dest[2] = _bswap_16(a1);
if ( bind(v5, (const struct sockaddr *)dest, v6) == -1 )
return -1;
*(_QWORD *)&dest[8] = 0LL;
*(_WORD *)dest = 2;
v7 = _bswap_16(a2);
*(_DWORD *)&dest[4] = -1;
*(_WORD *)&dest[2] = v7;
if ( connect(v5, (const struct sockaddr *)dest, v8) == -1 )
return -1;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, __int16 a2, const char *a3)
{
int v4; // eax
unsigned int v5; // er12
socklen_t v6; // edx
__int16 v7; // ax
socklen_t v8; // edx
int *v10; // rax
int optval; // [rsp+Ch] [rbp-5Ch] BYREF
char dest[16]; // [rsp+10h] [rbp-58h] BYREF
__int128 v13; // [rsp+20h] [rbp-48h]
__int64 v14; // [rsp+30h] [rbp-38h]

optval = 1;
v4 = socket(2, 2, 17);
if ( v4 == -1 )
return -1;
v5 = v4;
if ( setsockopt(v4, 1, 2, &optval, 4u) == -1 )
return -1;
setsockopt(v5, 1, 6, &optval, 4u);
v13 = 0LL;
v14 = 0LL;
strncpy(dest, a3, 0x10uLL);
if ( setsockopt(v5, 1, 25, dest, 0x28u) < 0 )
{
v10 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v10);
}
*(_OWORD *)dest = 0LL;
*(_WORD *)dest = 2;
*(_WORD *)&dest[2] = func0(a1);
if ( bind(v5, (const struct sockaddr *)dest, v6) == -1 )
return -1;
*(_QWORD *)&dest[8] = 0LL;
*(_WORD *)dest = 2;
v7 = func0(a2);
*(_DWORD *)&dest[4] = -1;
*(_WORD *)&dest[2] = v7;
if ( connect(v5, (const struct sockaddr *)dest, v8) == -1 )
return -1;
return v5;
}

original_code:
int clientSocket(short send_from_port, short send_to_port, char *interface_name) {
        int n = 1;
        int client_socket;
        struct sockaddr_in client;
        client_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(client_socket == -1)
                return -1;
        if (setsockopt(client_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
        setsockopt(client_socket, 1, 6, (char *) &n, sizeof(n));
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(client_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_from_port);
        client.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        if(bind(client_socket,(struct sockaddr *)&client, sizeof(struct sockaddr))==-1)
                return -1;
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_to_port);
        client.sin_addr.s_addr = ((in_addr_t) 0xffffffff);
        if(connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
                return -1;
        return client_socket;
}

canonical_original_code:
int clientSocket(short send_from_port, short send_to_port, char *interface_name) {
        int n = 1;
        int client_socket;
        struct sockaddr_in client;
        client_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(client_socket == -1)
                return -1;
        if (setsockopt(client_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
        setsockopt(client_socket, 1, 6, (char *) &n, sizeof(n));
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(client_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_from_port);
        client.sin_addr.s_addr = ((unsigned int) 0x00000000);
        if(bind(client_socket,(struct sockaddr *)&client, sizeof(struct sockaddr))==-1)
                return -1;
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_to_port);
        client.sin_addr.s_addr = ((unsigned int) 0xffffffff);
        if(connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
                return -1;
        return client_socket;
}

variable_types: n int

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


original_code:
static int valid_dhcp_packet (unsigned char * data)
{
  struct udp_dhcp_packet *packet = (struct udp_dhcp_packet *) data;
  uint16_t bytes = ntohs (packet->ip.ip_len);
  uint16_t ipsum = packet->ip.ip_sum;
  uint16_t iplen = packet->ip.ip_len;
  uint16_t udpsum = packet->udp.uh_sum;
  struct in_addr source;
  struct in_addr dest;
  int retval = 0;
  packet->ip.ip_sum = 0;
  if (ipsum != checksum ((unsigned char *) &packet->ip, sizeof (struct ip)))
    {
      logger (7, "bad IP header checksum, ignoring");
      retval = -1;
      goto eexit;
    }
  memcpy (&source, &packet->ip.ip_src, sizeof (struct in_addr));
  memcpy (&dest, &packet->ip.ip_dst, sizeof (struct in_addr));
  memset (&packet->ip, 0, sizeof (struct ip));
  packet->udp.uh_sum = 0;
  packet->ip.ip_p = IPPROTO_UDP;
  memcpy (&packet->ip.ip_src, &source, sizeof (struct in_addr));
  memcpy (&packet->ip.ip_dst, &dest, sizeof (struct in_addr));
  packet->ip.ip_len = packet->udp.uh_ulen;
  if (udpsum && udpsum != checksum ((unsigned char *) packet, bytes))
    {
      logger (3, "bad UDP checksum, ignoring");
      retval = -1;
    }
eexit:
  packet->ip.ip_sum = ipsum;
  packet->ip.ip_len = iplen;
  packet->udp.uh_sum = udpsum;
  return retval;
}

canonical_original_code:
static int valid_dhcp_packet(unsigned char *data)
{
  struct udp_dhcp_packet *packet = (struct udp_dhcp_packet *) data;
  unsigned short bytes = ntohs (packet->ip.ip_len);
  unsigned short ipsum = packet->ip.ip_sum;
  unsigned short iplen = packet->ip.ip_len;
  unsigned short udpsum = packet->udp.uh_sum;
  struct in_addr source;
  struct in_addr dest;
  int retval = 0;
  packet->ip.ip_sum = 0;
  if (ipsum != checksum ((unsigned char *) &packet->ip, sizeof (struct ip)))
    {
      logger (7, "bad IP header checksum, ignoring");
      retval = -1;
      goto eexit;
    }
  memcpy (&source, &packet->ip.ip_src, sizeof (struct in_addr));
  memcpy (&dest, &packet->ip.ip_dst, sizeof (struct in_addr));
  memset (&packet->ip, 0, sizeof (struct ip));
  packet->udp.uh_sum = 0;
  packet->ip.ip_p = IPPROTO_UDP;
  memcpy (&packet->ip.ip_src, &source, sizeof (struct in_addr));
  memcpy (&packet->ip.ip_dst, &dest, sizeof (struct in_addr));
  packet->ip.ip_len = packet->udp.uh_ulen;
  if (udpsum && udpsum != checksum ((unsigned char *) packet, bytes))
    {
      logger (3, "bad UDP checksum, ignoring");
      retval = -1;
    }
eexit:
  packet->ip.ip_sum = ipsum;
  packet->ip.ip_len = iplen;
  packet->udp.uh_sum = udpsum;
  return retval;
}

variable_types:
data
unsigned char *

variable_types: struct udp_dhcp_packet { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } ip; struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } udp; struct dhcpmessage_t { char op; char hwtype; char hwlen; char hwopcount; int xid; short secs; short flags; int ciaddr; int yiaddr; int siaddr; int giaddr; unsigned char hwaddr[16]; char servername[64]; char bootfile[128]; unsigned int cookie; unsigned char options[308]; } dhcp; } *

variable_types:
bytes
unsigned short

variable_types:
ipsum
unsigned short

variable_types:
iplen
unsigned short

variable_types:
udpsum
unsigned short

variable_types:
source
struct in_addr { unsigned int s_addr; }

variable_types:
dest
struct in_addr { unsigned int s_addr; }

user_defined_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; }

user_defined_types: struct udp_dhcp_packet { struct ip ip; struct udphdr udp; struct dhcpmessage_t dhcp; }


original_code:
void make_dhcp_packet(struct udp_dhcp_packet *packet,
        unsigned char *data,
        struct in_addr source, struct in_addr dest)
{
  struct ip *ip = &packet->ip;
  struct udphdr *udp = &packet->udp;
  memcpy (&packet->dhcp, data, sizeof (dhcpmessage_t));
  ip->ip_p = IPPROTO_UDP;
  ip->ip_src.s_addr = htonl (source.s_addr);
  if (dest.s_addr == 0)
    ip->ip_dst.s_addr = htonl (((in_addr_t) 0xffffffff));
  else
    ip->ip_dst.s_addr = htonl (dest.s_addr);
  udp->uh_sport = htons (68);
  udp->uh_dport = htons (67);
  udp->uh_ulen = htons (sizeof (struct udphdr) + sizeof (struct dhcpmessage_t));
  ip->ip_len = udp->uh_ulen;
  udp->uh_sum = checksum ((unsigned char *) packet,
     sizeof (struct udp_dhcp_packet));
  ip->ip_v = 4;
  ip->ip_hl = 5;
  ip->ip_id = 0;
  ip->ip_tos = 0x10;
  ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) +
        sizeof (struct dhcpmessage_t));
  ip->ip_id = 0;
  ip->ip_off = 0;
  ip->ip_ttl = 64;
  ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip));
}

canonical_original_code:
void make_dhcp_packet(struct udp_dhcp_packet *packet, unsigned char *data, struct in_addr source, struct in_addr dest)
{
  struct ip *ip = &packet->ip;
  struct udphdr *udp = &packet->udp;
  memcpy (&packet->dhcp, data, sizeof (dhcpmessage_t));
  ip->ip_p = IPPROTO_UDP;
  ip->ip_src.s_addr = htonl (source.s_addr);
  if (dest.s_addr == 0)
    ip->ip_dst.s_addr = htonl (((unsigned int) 0xffffffff));
  else
    ip->ip_dst.s_addr = htonl (dest.s_addr);
  udp->uh_sport = htons (68);
  udp->uh_dport = htons (67);
  udp->uh_ulen = htons (sizeof (struct udphdr) + sizeof (struct dhcpmessage_t));
  ip->ip_len = udp->uh_ulen;
  udp->uh_sum = checksum ((unsigned char *) packet,
     sizeof (struct udp_dhcp_packet));
  ip->ip_v = 4;
  ip->ip_hl = 5;
  ip->ip_id = 0;
  ip->ip_tos = 0x10;
  ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) +
        sizeof (struct dhcpmessage_t));
  ip->ip_id = 0;
  ip->ip_off = 0;
  ip->ip_ttl = 64;
  ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip));
}

variable_types:
source
struct in_addr

variable_types:
dest
struct in_addr

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

variable_types: struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } *

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


decompiled_code:
__int64 __fastcall open_socket(char *ifname, char a2)
{
int v3; // ebp
int v4; // eax
unsigned int v5; // er12
int v6; // eax
int v7; // edi
unsigned __int16 v8; // ax
int *v10; // rax
const char *v11; // rsi
int v12; // edi
int *v13; // rax
char *v14; // rax
int *v15; // rax
_BYTE v16[20]; // [rsp+0h] [rbp-48h] BYREF

v3 = (unsigned __int16)_bswap_16(2048);
v4 = socket(17, 2, v3);
v5 = v4;
if ( v4 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logger(3LL, "socket: %s", v14);
return v5;
}
v6 = fcntl(v4, 1, 0LL);
if ( v6 < 0 || fcntl(v5, 2, v6 | 1u) < 0 )
{
v10 = _errno_location();
strerror(*v10);
v11 = "fcntl: %s";
LABEL_12:
logger(3LL, v11);
v12 = v5;
v5 = -1;
close(v12);
return v5;
}
*(_DWORD *)&v16[16] = 0;
*(_OWORD *)v16 = 0LL;
*(_WORD *)v16 = 17;
if ( a2 )
LOWORD(v3) = _bswap_16(2054);
*(_WORD *)&v16[2] = v3;
*(_DWORD *)&v16[4] = if_nametoindex(ifname);
v16[11] = 6;
*(_QWORD *)&v16[12] = -1LL;
if ( bind(v5, (const struct sockaddr *)v16, 0x14u) == -1 )
{
v15 = _errno_location();
strerror(*v15);
v11 = "bind: %s";
goto LABEL_12;
}
v7 = *((_DWORD *)ifname + 6);
if ( v7 >= 0 )
close(v7);
*((_DWORD *)ifname + 6) = v5;
v8 = _bswap_16(*(__int16 *)&v16[2]);
*((_DWORD *)ifname + 7) = 4096;
*((_DWORD *)ifname + 8) = v8;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *ifname, char a2)
{
int v3; // ebp
int v4; // eax
unsigned int v5; // er12
int v6; // eax
int v7; // edi
unsigned __int16 v8; // ax
int *v10; // rax
const char *v11; // rsi
int v12; // edi
int *v13; // rax
char *v14; // rax
int *v15; // rax
_BYTE v16[20]; // [rsp+0h] [rbp-48h] BYREF

v3 = (unsigned __int16)func1(2048);
v4 = socket(17, 2, v3);
v5 = v4;
if ( v4 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logger(3LL, "socket: %s", v14);
return v5;
}
v6 = fcntl(v4, 1, 0LL);
if ( v6 < 0 || fcntl(v5, 2, v6 | 1u) < 0 )
{
v10 = _errno_location();
strerror(*v10);
v11 = "fcntl: %s";
LABEL_12:
logger(3LL, v11);
v12 = v5;
v5 = -1;
close(v12);
return v5;
}
*(_DWORD *)&v16[16] = 0;
*(_OWORD *)v16 = 0LL;
*(_WORD *)v16 = 17;
if ( a2 )
LOWORD(v3) = func1(2054);
*(_WORD *)&v16[2] = v3;
*(_DWORD *)&v16[4] = if_nametoindex(ifname);
v16[11] = 6;
*(_QWORD *)&v16[12] = -1LL;
if ( bind(v5, (const struct sockaddr *)v16, 0x14u) == -1 )
{
v15 = _errno_location();
strerror(*v15);
v11 = "bind: %s";
goto LABEL_12;
}
v7 = *((_DWORD *)ifname + 6);
if ( v7 >= 0 )
close(v7);
*((_DWORD *)ifname + 6) = v5;
v8 = func1(*(__int16 *)&v16[2]);
*((_DWORD *)ifname + 7) = 4096;
*((_DWORD *)ifname + 8) = v8;
return v5;
}

original_code:
int open_socket (interface_t *iface, _Bool arp)
{
  int fd;
  int flags;
  struct sockaddr_ll sll;
  if ((fd = socket (17, SOCK_DGRAM, htons (0x0800))) == -1)
    {
      logger (3, "socket: %s", strerror ((*__errno_location ())));
      return -1;
    }
  if ((flags = fcntl (fd, 1, 0)) < 0
      || fcntl (fd, 2, flags | 1) < 0)
    {
      logger (3, "fcntl: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  if (arp)
    sll.sll_protocol = htons (0x0806);
  else
    sll.sll_protocol = htons (0x0800);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if (bind(fd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll)) == -1)
    {
      logger (3, "bind: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  if (iface->fd > -1)
    close (iface->fd);
  iface->fd = fd;
  iface->socket_protocol = ntohs (sll.sll_protocol);
  iface->buffer_length = 4096;
  return fd;
}

canonical_original_code:
int open_socket(struct interface_t *iface, _Bool arp)
{
  int fd;
  int flags;
  struct sockaddr_ll sll;
  if ((fd = socket (17, SOCK_DGRAM, htons (0x0800))) == -1)
    {
      logger (3, "socket: %s", strerror ((*__errno_location ())));
      return -1;
    }
  if ((flags = fcntl (fd, 1, 0)) < 0
      || fcntl (fd, 2, flags | 1) < 0)
    {
      logger (3, "fcntl: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  if (arp)
    sll.sll_protocol = htons (0x0806);
  else
    sll.sll_protocol = htons (0x0800);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if (bind(fd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll)) == -1)
    {
      logger (3, "bind: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  if (iface->fd > -1)
    close (iface->fd);
  iface->fd = fd;
  iface->socket_protocol = ntohs (sll.sll_protocol);
  iface->buffer_length = 4096;
  return fd;
}

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: fd int

variable_types: flags int

variable_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

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 send_packet(char *ifname, __int16 a2, const void *a3, int a4)
{
unsigned int v6; // eax
int v7; // edi
int v8; // er12
int *v10; // rax
char *v11; // rax
_BYTE v12[20]; // [rsp+0h] [rbp-38h] BYREF

if ( !ifname )
return (unsigned int)-1;
*(_OWORD *)v12 = 0LL;
*(_DWORD *)&v12[16] = 0;
*(_WORD *)v12 = 17;
*(_WORD *)&v12[2] = _bswap_16(a2);
v6 = if_nametoindex(ifname);
v7 = *((_DWORD *)ifname + 6);
*(_DWORD *)&v12[4] = v6;
v12[11] = 6;
*(_QWORD *)&v12[12] = -1LL;
v8 = sendto(v7, a3, a4, 0, (const struct sockaddr *)v12, 0x14u);
if ( v8 >= 0 )
return (unsigned int)v8;
v10 = _errno_location();
v11 = strerror(*v10);
logger(3LL, "sendto: %s", v11);
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *ifname, __int16 a2, const void *a3, int a4)
{
unsigned int v6; // eax
int v7; // edi
int v8; // er12
int *v10; // rax
char *v11; // rax
_BYTE v12[20]; // [rsp+0h] [rbp-38h] BYREF

if ( !ifname )
return (unsigned int)-1;
*(_OWORD *)v12 = 0LL;
*(_DWORD *)&v12[16] = 0;
*(_WORD *)v12 = 17;
*(_WORD *)&v12[2] = func1(a2);
v6 = if_nametoindex(ifname);
v7 = *((_DWORD *)ifname + 6);
*(_DWORD *)&v12[4] = v6;
v12[11] = 6;
*(_QWORD *)&v12[12] = -1LL;
v8 = sendto(v7, a3, a4, 0, (const struct sockaddr *)v12, 0x14u);
if ( v8 >= 0 )
return (unsigned int)v8;
v10 = _errno_location();
v11 = strerror(*v10);
logger(3LL, "sendto: %s", v11);
return (unsigned int)v8;
}

original_code:
int send_packet (interface_t *iface, int type, unsigned char *data, int len)
{
  struct sockaddr_ll sll;
  int retval;
  if (! iface)
    return -1;
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  sll.sll_protocol = htons (type);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if ((retval = sendto (iface->fd, data, len, 0, (struct sockaddr *) &sll,
   sizeof (struct sockaddr_ll))) < 0)
    logger (3, "sendto: %s", strerror ((*__errno_location ())));
  return retval;
}

canonical_original_code:
int send_packet(struct interface_t *iface, int type, unsigned char *data, int len)
{
  struct sockaddr_ll sll;
  int retval;
  if (! iface)
    return -1;
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  sll.sll_protocol = htons (type);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if ((retval = sendto (iface->fd, data, len, 0, (struct sockaddr *) &sll,
   sizeof (struct sockaddr_ll))) < 0)
    logger (3, "sendto: %s", strerror ((*__errno_location ())));
  return retval;
}

variable_types: type int

variable_types: len int

variable_types: retval int

user_defined_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

user_defined_types: struct ether_addr { unsigned char ether_addr_octet[6]; }

user_defined_types: struct route_t { struct in_addr destination; struct in_addr netmask; struct in_addr gateway; struct route_t *next; }


original_code:
size_t get_packet (interface_t *iface, unsigned char *data,
     unsigned char *buffer, int *buffer_len, int *buffer_pos)
{
  long bytes;
  *buffer_pos = 0;
  memset (buffer, 0, iface->buffer_length);
  bytes = read (iface->fd, buffer, iface->buffer_length);
  if (bytes < 0)
    {
      logger (3, "read: %s", strerror ((*__errno_location ())));
      return -1;
    }
  *buffer_len = bytes;
  if (iface->socket_protocol == 0x0806)
    {
      memcpy (data, buffer, bytes);
      return bytes;
    }
  if (bytes < (sizeof (struct ip) + sizeof (struct udphdr)))
    {
      logger (7, "message too short, ignoring");
      return -1;
    }
  struct udp_dhcp_packet *dhcp = (struct udp_dhcp_packet *) buffer;
  if (bytes < ntohs (dhcp->ip.ip_len))
    {
      logger (7, "truncated packet, ignoring");
      return -1;
    }
  bytes = ntohs (dhcp->ip.ip_len);
  if (dhcp->ip.ip_p != IPPROTO_UDP || dhcp->ip.ip_v != 4 ||
      dhcp->ip.ip_hl != sizeof (dhcp->ip) >> 2 ||
      dhcp->udp.uh_dport != htons (68) ||
      bytes > (int) sizeof (struct udp_dhcp_packet) ||
      ntohs (dhcp->udp.uh_ulen) != (uint16_t) (bytes - sizeof (dhcp->ip)))
    {
      return -1;
    }
  if (valid_dhcp_packet (buffer) < 0)
    return -1;
  memcpy(data, &dhcp->dhcp, bytes - (sizeof (dhcp->ip) +
         sizeof (dhcp->udp)));
  return bytes - (sizeof (dhcp->ip) + sizeof (dhcp->udp));
}

canonical_original_code:
unsigned long get_packet(struct interface_t *iface, unsigned char *data, unsigned char *buffer, int *buffer_len, int *buffer_pos)
{
  long bytes;
  *buffer_pos = 0;
  memset (buffer, 0, iface->buffer_length);
  bytes = read (iface->fd, buffer, iface->buffer_length);
  if (bytes < 0)
    {
      logger (3, "read: %s", strerror ((*__errno_location ())));
      return -1;
    }
  *buffer_len = bytes;
  if (iface->socket_protocol == 0x0806)
    {
      memcpy (data, buffer, bytes);
      return bytes;
    }
  if (bytes < (sizeof (struct ip) + sizeof (struct udphdr)))
    {
      logger (7, "message too short, ignoring");
      return -1;
    }
  struct udp_dhcp_packet *dhcp = (struct udp_dhcp_packet *) buffer;
  if (bytes < ntohs (dhcp->ip.ip_len))
    {
      logger (7, "truncated packet, ignoring");
      return -1;
    }
  bytes = ntohs (dhcp->ip.ip_len);
  if (dhcp->ip.ip_p != IPPROTO_UDP || dhcp->ip.ip_v != 4 ||
      dhcp->ip.ip_hl != sizeof (dhcp->ip) >> 2 ||
      dhcp->udp.uh_dport != htons (68) ||
      bytes > (int) sizeof (struct udp_dhcp_packet) ||
      ntohs (dhcp->udp.uh_ulen) != (unsigned short) (bytes - sizeof (dhcp->ip)))
    {
      return -1;
    }
  if (valid_dhcp_packet (buffer) < 0)
    return -1;
  memcpy(data, &dhcp->dhcp, bytes - (sizeof (dhcp->ip) +
         sizeof (dhcp->udp)));
  return bytes - (sizeof (dhcp->ip) + sizeof (dhcp->udp));
}

variable_types:
data
unsigned char *

variable_types:
buffer
unsigned char *

variable_types: bytes long

variable_types: struct udp_dhcp_packet { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } ip; struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } udp; struct dhcpmessage_t { char op; char hwtype; char hwlen; char hwopcount; int xid; short secs; short flags; int ciaddr; int yiaddr; int siaddr; int giaddr; unsigned char hwaddr[16]; char servername[64]; char bootfile[128]; unsigned int cookie; unsigned char options[308]; } dhcp; } *

return_type: unsigned long


checksum

decompiled_code:
__int64 __fastcall checksum(unsigned __int16 *a1, unsigned __int16 a2)
{
__int16 v2; // ax
unsigned int v3; // edx
unsigned __int16 *v4; // rcx
int v5; // eax
unsigned __int16 v6; // ax
int v7; // edx

v2 = a2;
if ( a2 <= 1u )
{
v4 = a1;
v3 = 0;
}
else
{
v3 = 0;
v4 = &a1[(unsigned __int16)(((unsigned __int16)(a2 - 2) >> 1) + 1)];
do
{
v5 = *a1++;
v3 += v5;
}
while ( v4 != a1 );
v2 = a2 & 1;
}
if ( v2 == 1 )
{
v6 = _bswap_16(*(unsigned __int8 *)v4);
v3 = (v6 << 8) + v7;
}
return ~(HIWORD(v3) + (unsigned __int16)v3 + ((unsigned int)(HIWORD(v3) + (unsigned __int16)v3) >> 16));
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 *a1, unsigned __int16 a2)
{
__int16 v2; // ax
unsigned int v3; // edx
unsigned __int16 *v4; // rcx
int v5; // eax
unsigned __int16 v6; // ax
int v7; // edx

v2 = a2;
if ( a2 <= 1u )
{
v4 = a1;
v3 = 0;
}
else
{
v3 = 0;
v4 = &a1[(unsigned __int16)(((unsigned __int16)(a2 - 2) >> 1) + 1)];
do
{
v5 = *a1++;
v3 += v5;
}
while ( v4 != a1 );
v2 = a2 & 1;
}
if ( v2 == 1 )
{
v6 = func1(*(unsigned __int8 *)v4);
v3 = (v6 << 8) + v7;
}
return ~(HIWORD(v3) + (unsigned __int16)v3 + ((unsigned int)(HIWORD(v3) + (unsigned __int16)v3) >> 16));
}

original_code:
static uint16_t checksum (unsigned char *addr, uint16_t len)
{
  register uint32_t sum = 0;
  register uint16_t *w = (uint16_t *) addr;
  register uint16_t nleft = len;
  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
  if (nleft == 1)
    {
      uint8_t a = 0;
      memcpy (&a, w, 1);
      sum += ntohs (a) << 8;
    }
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

canonical_original_code:
static unsigned short checksum(unsigned char *addr, unsigned short len)
{
  register unsigned int sum = 0;
  register unsigned short *w = (uint16_t *) addr;
  register unsigned short nleft = len;
  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
  if (nleft == 1)
    {
      unsigned char a = 0;
      memcpy (&a, w, 1);
      sum += ntohs (a) << 8;
    }
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

variable_types:
addr
unsigned char *

variable_types:
len
unsigned short

variable_types:
sum
unsigned int

variable_types:
w
unsigned short *

variable_types:
nleft
unsigned short

variable_types:
a
unsigned char

return_type: unsigned short


decompiled_code:
__int64 __fastcall ipv4_getrawpacket(__int64 a1, int a2, __int64 a3, __int64 a4, int *a5)
{
__int64 v6; // rax
int v7; // edi
ssize_t v8; // r12
bool v9; // cc
struct cmsghdr *v10; // rsi
__int64 v12[2]; // [rsp+0h] [rbp-98h] BYREF
char v13; // [rsp+10h] [rbp-88h] BYREF
struct msghdr message; // [rsp+40h] [rbp-58h] BYREF

v12[0] = a3;
message.msg_control = &v13;
v6 = *(_QWORD *)(a1 + 192);
v12[1] = a4;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v12;
message.msg_iovlen = 1LL;
message.msg_controllen = 36LL;
*(_OWORD *)&message.msg_name = 0LL;
if ( a2 == 2054 )
v7 = *(_DWORD *)(v6 + 184);
else
v7 = *(_DWORD *)(v6 + 176);
v8 = recvmsg(v7, &message, 0);
if ( v8 == -1 )
return -(__int64)(*_errno_location() != 11);
if ( a5 )
{
v9 = message.msg_controllen <= 0xF;
*a5 = 0;
if ( !v9 )
{
v10 = (struct cmsghdr *)message.msg_control;
if ( message.msg_control )
{
do
{
if ( *(_QWORD *)&v10->cmsg_level == 0x800000107LL )
*a5 = *(_DWORD *)v10->__cmsg_data & 8;
v10 = _cmsg_nxthdr(&message, v10);
}
while ( v10 );
}
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3, __int64 a4, int *a5)
{
__int64 v6; // rax
int v7; // edi
ssize_t v8; // r12
bool v9; // cc
struct cmsghdr *v10; // rsi
__int64 v12[2]; // [rsp+0h] [rbp-98h] BYREF
char v13; // [rsp+10h] [rbp-88h] BYREF
struct msghdr message; // [rsp+40h] [rbp-58h] BYREF

v12[0] = a3;
message.msg_control = &v13;
v6 = *(_QWORD *)(a1 + 192);
v12[1] = a4;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v12;
message.msg_iovlen = 1LL;
message.msg_controllen = 36LL;
*(_OWORD *)&message.msg_name = 0LL;
if ( a2 == 2054 )
v7 = *(_DWORD *)(v6 + 184);
else
v7 = *(_DWORD *)(v6 + 176);
v8 = recvmsg(v7, &message, 0);
if ( v8 == -1 )
return -(__int64)(*_errno_location() != 11);
if ( a5 )
{
v9 = message.msg_controllen <= 0xF;
*a5 = 0;
if ( !v9 )
{
v10 = (struct cmsghdr *)message.msg_control;
if ( message.msg_control )
{
do
{
if ( *(_QWORD *)&v10->cmsg_level == 0x800000107LL )
*a5 = *(_DWORD *)v10->__cmsg_data & 8;
v10 = _cmsg_nxthdr(&message, v10);
}
while ( v10 );
}
}
}
return v8;
}

original_code:
ssize_t
ipv4_getrawpacket(struct interface *ifp, int protocol,
    void *data, ssize_t len, int *partialcsum)
{
 struct iovec iov = {
  .iov_base = data,
  .iov_len = len,
 };
 struct msghdr msg = {
  .msg_iov = &iov,
  .msg_iovlen = 1,
 };
 struct dhcp_state *state;
 unsigned char cmsgbuf[((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(struct tpacket_auxdata)))];
 struct cmsghdr *cmsg;
 struct tpacket_auxdata *aux;
 ssize_t bytes;
 int fd = -1;
 msg.msg_control = cmsgbuf;
 msg.msg_controllen = sizeof(cmsgbuf);
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 if (protocol == 0x0806)
  fd = state->arp_fd;
 else
  fd = state->raw_fd;
 bytes = recvmsg(fd, &msg, 0);
 if (bytes == -1)
  return (*__errno_location ()) == 11 ? 0 : -1;
 if (partialcsum != ((void *)0)) {
  *partialcsum = 0;
  for (cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
       cmsg;
       cmsg = __cmsg_nxthdr (&msg, cmsg))
  {
   if (cmsg->cmsg_level == 263 &&
       cmsg->cmsg_type == 8) {
    aux = (void *)((cmsg)->__cmsg_data);
    *partialcsum = aux->tp_status &
        (1 << 3);
   }
  }
 }
 return bytes;
}

canonical_original_code:
long ipv4_getrawpacket(struct interface *ifp, int protocol, void *data, long len, int *partialcsum)
{
 struct iovec iov = {
  .iov_base = data,
  .iov_len = len,
 };
 struct msghdr msg = {
  .msg_iov = &iov,
  .msg_iovlen = 1,
 };
 struct dhcp_state *state;
 unsigned char cmsgbuf[-1];
 struct cmsghdr *cmsg;
 struct tpacket_auxdata *aux;
 long bytes;
 int fd = -1;
 msg.msg_control = cmsgbuf;
 msg.msg_controllen = sizeof(cmsgbuf);
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 if (protocol == 0x0806)
  fd = state->arp_fd;
 else
  fd = state->raw_fd;
 bytes = recvmsg(fd, &msg, 0);
 if (bytes == -1)
  return (*__errno_location ()) == 11 ? 0 : -1;
 if (partialcsum != ((void *)0)) {
  *partialcsum = 0;
  for (cmsg = ((unsigned long) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
       cmsg;
       cmsg = __cmsg_nxthdr (&msg, cmsg))
  {
   if (cmsg->cmsg_level == 263 &&
       cmsg->cmsg_type == 8) {
    aux = (void *)((cmsg)->__cmsg_data);
    *partialcsum = aux->tp_status &
        (1 << 3);
   }
  }
 }
 return bytes;
}

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: protocol int

variable_types:
data
void *

variable_types: len long

variable_types:
partialcsum
int *

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

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

variable_types:
cmsgbuf
unsigned char [-1]

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types: struct tpacket_auxdata { unsigned int tp_status; unsigned int tp_len; unsigned int tp_snaplen; unsigned short tp_mac; unsigned short tp_net; unsigned short tp_vlan_tci; unsigned short tp_vlan_tpid; } *

return_type: long

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

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:
void __fastcall ipv4ll_start(__int64 a1)
{
__int64 v2; // rbx
unsigned int v3; // edi
__int64 v4; // rax
_DWORD *v5; // rdi
unsigned int v6; // er12
__int64 v7; // rax
_DWORD *v8; // rax
__int64 v9; // rax
_DWORD *v10; // rax

v2 = *(_QWORD *)(a1 + 192);
eloop_q_timeout_delete(1LL, 0LL, a1);
v3 = *(_DWORD *)(v2 + 224);
*(_QWORD *)(v2 + 136) = 0LL;
if ( !v3 || (*(_DWORD *)(v2 + 144) = 0, v4 = _bswap_32(v3), LOWORD(v4) = 0, (_DWORD)v4 != -1442971648) )
{
v5 = *(_DWORD **)(v2 + 16);
if ( v5 )
{
v6 = v5[4];
free(v5);
v7 = _bswap_32(v6);
LOWORD(v7) = 0;
if ( (_DWORD)v7 == -1442971648 )
{
if ( v6 )
{
v8 = ipv4ll_make_lease(v6);
*(_QWORD *)(v2 + 16) = v8;
if ( !v8 )
{
LABEL_7:
syslog(3, "%s: %m", "ipv4ll_start");
return;
}
goto LABEL_11;
}
LABEL_10:
v10 = ipv4ll_find_lease(0);
*(_QWORD *)(v2 + 16) = v10;
if ( !v10 )
goto LABEL_7;
LABEL_11:
*(_BYTE *)(v2 + 96) = 0;
arp_probe(a1);
return;
}
}
else
{
v9 = _bswap_32(0);
LOWORD(v9) = 0;
if ( (_DWORD)v9 == -1442971648 )
goto LABEL_10;
}
syslog(6, "%s: probing for an IPv4LL address", (const char *)(a1 + 16));
goto LABEL_10;
}
arp_announce(a1);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
__int64 v2; // rbx
unsigned int v3; // edi
__int64 v4; // rax
_DWORD *v5; // rdi
unsigned int v6; // er12
__int64 v7; // rax
_DWORD *v8; // rax
__int64 v9; // rax
_DWORD *v10; // rax

v2 = *(_QWORD *)(a1 + 192);
eloop_q_timeout_delete(1LL, 0LL, a1);
v3 = *(_DWORD *)(v2 + 224);
*(_QWORD *)(v2 + 136) = 0LL;
if ( !v3 || (*(_DWORD *)(v2 + 144) = 0, v4 = func0(v3), LOWORD(v4) = 0, (_DWORD)v4 != -1442971648) )
{
v5 = *(_DWORD **)(v2 + 16);
if ( v5 )
{
v6 = v5[4];
free(v5);
v7 = func0(v6);
LOWORD(v7) = 0;
if ( (_DWORD)v7 == -1442971648 )
{
if ( v6 )
{
v8 = ipv4ll_make_lease(v6);
*(_QWORD *)(v2 + 16) = v8;
if ( !v8 )
{
LABEL_7:
syslog(3, "%s: %m", "ipv4ll_start");
return;
}
goto LABEL_11;
}
LABEL_10:
v10 = ipv4ll_find_lease(0);
*(_QWORD *)(v2 + 16) = v10;
if ( !v10 )
goto LABEL_7;
LABEL_11:
*(_BYTE *)(v2 + 96) = 0;
arp_probe(a1);
return;
}
}
else
{
v9 = func0(0);
LOWORD(v9) = 0;
if ( (_DWORD)v9 == -1442971648 )
goto LABEL_10;
}
syslog(6, "%s: probing for an IPv4LL address", (const char *)(a1 + 16));
goto LABEL_10;
}
arp_announce(a1);
}

original_code:
void
ipv4ll_start(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 uint32_t addr;
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 state->probes = 0;
 state->claims = 0;
 if (state->addr.s_addr) {
  state->conflicts = 0;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000)) {
   arp_announce(ifp);
   return;
  }
 }
 if (state->offer == ((void *)0))
  addr = 0;
 else {
  addr = state->offer->yiaddr;
  free(state->offer);
 }
 if (!((htonl(addr) & 0xffff0000) == 0xa9fe0000)) {
  syslog(6, "%s: probing for an IPv4LL address",
      ifp->name);
  addr = 0;
 }
 if (addr == 0)
  state->offer = ipv4ll_find_lease(addr);
 else
  state->offer = ipv4ll_make_lease(addr);
 if (state->offer == ((void *)0))
  syslog(3, "%s: %m", __func__);
 else {
  state->lease.frominfo = 0;
  arp_probe(ifp);
 }
}

canonical_original_code:
void ipv4ll_start(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 unsigned int addr;
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 state->probes = 0;
 state->claims = 0;
 if (state->addr.s_addr) {
  state->conflicts = 0;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000)) {
   arp_announce(ifp);
   return;
  }
 }
 if (state->offer == ((void *)0))
  addr = 0;
 else {
  addr = state->offer->yiaddr;
  free(state->offer);
 }
 if (!((htonl(addr) & 0xffff0000) == 0xa9fe0000)) {
  syslog(6, "%s: probing for an IPv4LL address",
      ifp->name);
  addr = 0;
 }
 if (addr == 0)
  state->offer = ipv4ll_find_lease(addr);
 else
  state->offer = ipv4ll_make_lease(addr);
 if (state->offer == ((void *)0))
  syslog(3, "%s: %m", __func__);
 else {
  state->lease.frominfo = 0;
  arp_probe(ifp);
 }
}

variable_types:
arg
void *

variable_types:
addr
unsigned int

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

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


decompiled_code:
void __fastcall ipv4ll_handle_failure(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax
__int64 v3; // rax
const char *v4; // rdx
__int64 v5; // rbp

v1 = *(_QWORD *)(a1 + 192);
if ( *(_DWORD *)(v1 + 160) == *(_DWORD *)(v1 + 224) )
{
v3 = uptime();
v4 = (const char *)(a1 + 16);
v5 = v3;
if ( *(_QWORD *)(v1 + 152) + 9LL < v3 )
{
syslog(7, "%s: defended IPv4LL address", v4);
*(_QWORD *)(v1 + 152) = v5;
return;
}
syslog(7, "%s: IPv4LL %d second defence failed", v4, 10LL);
dhcp_drop(a1, "EXPIRE");
*(_DWORD *)(v1 + 144) = -1;
}
dhcp_close(a1);
free(*(void **)(v1 + 16));
*(_QWORD *)(v1 + 16) = 0LL;
eloop_q_timeout_delete(1LL, 0LL, a1);
v2 = *(_DWORD *)(v1 + 144) + 1;
*(_DWORD *)(v1 + 144) = v2;
if ( v2 <= 10 )
{
eloop_q_timeout_add_sec(1LL, 1LL, ipv4ll_start, a1);
}
else
{
syslog(3, "%s: failed to acquire an IPv4LL address", (const char *)(a1 + 16));
*(_QWORD *)(v1 + 112) = 30LL;
dhcp_discover(a1);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax
__int64 v3; // rax
const char *v4; // rdx
__int64 v5; // rbp

v1 = *(_QWORD *)(a1 + 192);
if ( *(_DWORD *)(v1 + 160) == *(_DWORD *)(v1 + 224) )
{
v3 = uptime();
v4 = (const char *)(a1 + 16);
v5 = v3;
if ( *(_QWORD *)(v1 + 152) + 9LL < v3 )
{
syslog(7, "%s: defended IPv4LL address", v4);
*(_QWORD *)(v1 + 152) = v5;
return;
}
syslog(7, "%s: IPv4LL %d second defence failed", v4, 10LL);
dhcp_drop(a1, "EXPIRE");
*(_DWORD *)(v1 + 144) = -1;
}
dhcp_close(a1);
free(*(void **)(v1 + 16));
*(_QWORD *)(v1 + 16) = 0LL;
eloop_q_timeout_delete(1LL, 0LL, a1);
v2 = *(_DWORD *)(v1 + 144) + 1;
*(_DWORD *)(v1 + 144) = v2;
if ( v2 <= 10 )
{
eloop_q_timeout_add_sec(1LL, 1LL, ipv4ll_start, a1);
}
else
{
syslog(3, "%s: failed to acquire an IPv4LL address", (const char *)(a1 + 16));
*(_QWORD *)(v1 + 112) = 30LL;
dhcp_discover(a1);
}
}

original_code:
void
ipv4ll_handle_failure(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 time_t up;
 if (state->fail.s_addr == state->addr.s_addr) {
  up = uptime();
  if (state->defend + 10 > up) {
   syslog(7,
       "%s: IPv4LL %d second defence failed",
       ifp->name, 10);
   dhcp_drop(ifp, "EXPIRE");
   state->conflicts = -1;
  } else {
   syslog(7, "%s: defended IPv4LL address",
       ifp->name);
   state->defend = up;
   return;
  }
 }
 dhcp_close(ifp);
 free(state->offer);
 state->offer = ((void *)0);
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 if (++state->conflicts > 10) {
  syslog(3, "%s: failed to acquire an IPv4LL address",
      ifp->name);
  state->interval = 60 / 2;
  dhcp_discover(ifp);
 } else {
  eloop_q_timeout_add_sec(1, 1, ipv4ll_start, ifp);
 }
}

canonical_original_code:
void ipv4ll_handle_failure(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 long up;
 if (state->fail.s_addr == state->addr.s_addr) {
  up = uptime();
  if (state->defend + 10 > up) {
   syslog(7,
       "%s: IPv4LL %d second defence failed",
       ifp->name, 10);
   dhcp_drop(ifp, "EXPIRE");
   state->conflicts = -1;
  } else {
   syslog(7, "%s: defended IPv4LL address",
       ifp->name);
   state->defend = up;
   return;
  }
 }
 dhcp_close(ifp);
 free(state->offer);
 state->offer = ((void *)0);
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 if (++state->conflicts > 10) {
  syslog(3, "%s: failed to acquire an IPv4LL address",
      ifp->name);
  state->interval = 60 / 2;
  dhcp_discover(ifp);
 } else {
  eloop_q_timeout_add_sec(1, 1, ipv4ll_start, ifp);
 }
}

variable_types: up long


decompiled_code:
__int64 __fastcall make_option_mask(unsigned __int16 *a1, __int64 a2, const char *a3, int a4)
{
char *v7; // rax
const char *v8; // rbp
unsigned __int16 *v9; // rbx
const char *v10; // rdi
int *v11; // rax
int *v12; // r15
int v13; // eax
unsigned __int16 v15; // si
char *ptr; // [rsp+8h] [rbp-50h]
char *stringp; // [rsp+10h] [rbp-48h] BYREF
char *endptr; // [rsp+18h] [rbp-40h] BYREF

ptr = strdup(a3);
stringp = ptr;
while ( 1 )
{
do
{
v7 = strsep(&stringp, ", ");
v8 = v7;
if ( !v7 )
{
free(ptr);
return 0LL;
}
}
while ( !*v7 );
if ( !*a1 )
goto LABEL_12;
v9 = a1;
while ( 1 )
{
v10 = (const char *)*((_QWORD *)v9 + 1);
if ( v10 )
{
if ( !strcmp(v10, v8) )
break;
v11 = _errno_location();
*v11 = 0;
v12 = v11;
v13 = strtol(v8, &endptr, 0);
if ( !*v12 && !*endptr && *v9 == v13 )
break;
}
v9 += 8;
if ( !*v9 )
goto LABEL_12;
}
if ( a4 == 2 )
break;
v15 = *v9;
if ( (unsigned int)(a4 - 1) <= 1 )
goto LABEL_20;
*(_BYTE *)(a2 + (*v9 >> 3)) &= ~(unsigned __int8)(1 << (*v9 & 7));
LABEL_16:
if ( !*v9 )
{
LABEL_12:
free(ptr);
*_errno_location() = 2;
return 0xFFFFFFFFLL;
}
}
if ( (v9[2] & 0x40) != 0 )
{
v15 = *v9;
LABEL_20:
*(_BYTE *)(a2 + (v15 >> 3)) |= 1 << (v15 & 7);
goto LABEL_16;
}
free(ptr);
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 *a1, __int64 a2, const char *a3, int a4)
{
char *v7; // rax
const char *v8; // rbp
unsigned __int16 *v9; // rbx
const char *v10; // rdi
int *v11; // rax
int *v12; // r15
int v13; // eax
unsigned __int16 v15; // si
char *ptr; // [rsp+8h] [rbp-50h]
char *stringp; // [rsp+10h] [rbp-48h] BYREF
char *endptr; // [rsp+18h] [rbp-40h] BYREF

ptr = strdup(a3);
stringp = ptr;
while ( 1 )
{
do
{
v7 = strsep(&stringp, ", ");
v8 = v7;
if ( !v7 )
{
free(ptr);
return 0LL;
}
}
while ( !*v7 );
if ( !*a1 )
goto LABEL_12;
v9 = a1;
while ( 1 )
{
v10 = (const char *)*((_QWORD *)v9 + 1);
if ( v10 )
{
if ( !strcmp(v10, v8) )
break;
v11 = _errno_location();
*v11 = 0;
v12 = v11;
v13 = strtol(v8, &endptr, 0);
if ( !*v12 && !*endptr && *v9 == v13 )
break;
}
v9 += 8;
if ( !*v9 )
goto LABEL_12;
}
if ( a4 == 2 )
break;
v15 = *v9;
if ( (unsigned int)(a4 - 1) <= 1 )
goto LABEL_20;
*(_BYTE *)(a2 + (*v9 >> 3)) &= ~(unsigned __int8)(1 << (*v9 & 7));
LABEL_16:
if ( !*v9 )
{
LABEL_12:
free(ptr);
*_errno_location() = 2;
return 0xFFFFFFFFLL;
}
}
if ( (v9[2] & 0x40) != 0 )
{
v15 = *v9;
LABEL_20:
*(_BYTE *)(a2 + (v15 >> 3)) |= 1 << (v15 & 7);
goto LABEL_16;
}
free(ptr);
*_errno_location() = 22;
return 0xFFFFFFFFLL;
}

original_code:
int make_option_mask(const struct dhcp_opt *dopts,
    uint8_t *mask, const char *opts, int add)
{
 char *token, *o, *p, *t;
 const struct dhcp_opt *opt;
 int match, n;
 o = p = strdup(opts);
 if (opts == ((void *)0))
  return -1;
 while ((token = strsep(&p, ", "))) {
  if (*token == '\0')
   continue;
  for (opt = dopts; opt->option; opt++) {
   if (!opt->var)
    continue;
   match = 0;
   if (strcmp(opt->var, token) == 0)
    match = 1;
   else {
    (*__errno_location ()) = 0;
    n = strtol(token, &t, 0);
    if ((*__errno_location ()) == 0 && !*t)
     if (opt->option == n)
      match = 1;
   }
   if (match) {
    if (add == 2 && !(opt->type & (1 << 6))) {
     free(o);
     (*__errno_location ()) = 22;
     return -1;
    }
    if (add == 1 || add == 2)
     (mask[opt->option >> 3] |= 1 << (opt->option & 7));
    else
     (mask[opt->option >> 3] &= ~(1 << (opt->option & 7)));
    break;
   }
  }
  if (!opt->option) {
   free(o);
   (*__errno_location ()) = 2;
   return -1;
  }
 }
 free(o);
 return 0;
}

canonical_original_code:
int make_option_mask(struct dhcp_opt *dopts, unsigned char *mask, char *opts, int add)
{
 char *token; char *o; char *p; char *t;
 struct dhcp_opt *opt;
 int match; int n;
 o = p = strdup(opts);
 if (opts == ((void *)0))
  return -1;
 while ((token = strsep(&p, ", "))) {
  if (*token == '\0')
   continue;
  for (opt = dopts; opt->option; opt++) {
   if (!opt->var)
    continue;
   match = 0;
   if (strcmp(opt->var, token) == 0)
    match = 1;
   else {
    (*__errno_location ()) = 0;
    n = strtol(token, &t, 0);
    if ((*__errno_location ()) == 0 && !*t)
     if (opt->option == n)
      match = 1;
   }
   if (match) {
    if (add == 2 && !(opt->type & (1 << 6))) {
     free(o);
     (*__errno_location ()) = 22;
     return -1;
    }
    if (add == 1 || add == 2)
     (mask[opt->option >> 3] |= 1 << (opt->option & 7));
    else
     (mask[opt->option >> 3] &= ~(1 << (opt->option & 7)));
    break;
   }
  }
  if (!opt->option) {
   free(o);
   (*__errno_location ()) = 2;
   return -1;
  }
 }
 free(o);
 return 0;
}

variable_types: struct dhcp_opt { unsigned short option; int type; char *var; } *

variable_types:
mask
unsigned char *

variable_types:
opts
char *

variable_types: add int

variable_types:
token
char *

variable_types:
o
char *

variable_types:
p
char *

variable_types:
t
char *

variable_types: match int

variable_types: n int


original_code:
size_t
encode_rfc1035(const char *src, uint8_t *dst)
{
 uint8_t *p;
 uint8_t *lp;
 size_t len;
 uint8_t has_dot;
 if (src == ((void *)0) || *src == '\0')
  return 0;
 if (dst) {
  p = dst;
  lp = p++;
 }
 else
  p = lp = ((void *)0);
 len = 1;
 has_dot = 0;
 for (; *src; src++) {
  if (*src == '\0')
   break;
  if (*src == '.') {
   if (src[1] == '\0')
    break;
   has_dot = 1;
   if (dst) {
    *lp = p - lp - 1;
    if (*lp == '\0')
     return len;
    lp = p++;
   }
  } else if (dst)
   *p++ = (uint8_t)*src;
  len++;
 }
 if (dst) {
  *lp = p - lp - 1;
  if (has_dot)
   *p++ = '\0';
 }
 if (has_dot)
  len++;
 return len;
}

canonical_original_code:
unsigned long encode_rfc1035(char *src, unsigned char *dst)
{
 unsigned char *p;
 unsigned char *lp;
 unsigned long len;
 unsigned char has_dot;
 if (src == ((void *)0) || *src == '\0')
  return 0;
 if (dst) {
  p = dst;
  lp = p++;
 }
 else
  p = lp = ((void *)0);
 len = 1;
 has_dot = 0;
 for (; *src; src++) {
  if (*src == '\0')
   break;
  if (*src == '.') {
   if (src[1] == '\0')
    break;
   has_dot = 1;
   if (dst) {
    *lp = p - lp - 1;
    if (*lp == '\0')
     return len;
    lp = p++;
   }
  } else if (dst)
   *p++ = (unsigned char)*src;
  len++;
 }
 if (dst) {
  *lp = p - lp - 1;
  if (has_dot)
   *p++ = '\0';
 }
 if (has_dot)
  len++;
 return len;
}

variable_types:
src
char *

variable_types:
dst
unsigned char *

variable_types:
p
unsigned char *

variable_types:
lp
unsigned char *

variable_types:
len
unsigned long


original_code:
ssize_t
decode_rfc3397(char *out, ssize_t len, int pl, const uint8_t *p)
{
 const char *start;
 ssize_t start_len;
 const uint8_t *r, *q = p;
 int count = 0, l, hops;
 uint8_t ltype;
 start = out;
 start_len = len;
 while (q - p < pl) {
  r = ((void *)0);
  hops = 0;
  while ((l = *q++) && q - p < pl) {
   ltype = l & 0xc0;
   if (ltype == 0x80 || ltype == 0x40)
    return 0;
   else if (ltype == 0xc0) {
    l = (l & 0x3f) << 8;
    l |= *q++;
    if (!r)
     r = q;
    hops++;
    if (hops > 255)
     return 0;
    q = p + l;
    if (q - p >= pl)
     return 0;
   } else {
    count += l + 1;
    if (out) {
     if ((ssize_t)l + 1 > len) {
      (*__errno_location ()) = 105;
      return -1;
     }
     memcpy(out, q, l);
     out += l;
     *out++ = '.';
     len -= l;
     len--;
    }
    q += l;
   }
  }
  if (out && out != start)
   *(out - 1) = ' ';
  if (r)
   q = r;
 }
 if (out) {
  if (out != start)
   *(out - 1) = '\0';
  else if (start_len > 0)
   *out = '\0';
 }
 return count;
}

canonical_original_code:
long decode_rfc3397(char *out, long len, int pl, unsigned char *p)
{
 char *start;
 long start_len;
 unsigned char *r; unsigned char *q = p;
 int count = 0; int l; int hops;
 unsigned char ltype;
 start = out;
 start_len = len;
 while (q - p < pl) {
  r = ((void *)0);
  hops = 0;
  while ((l = *q++) && q - p < pl) {
   ltype = l & 0xc0;
   if (ltype == 0x80 || ltype == 0x40)
    return 0;
   else if (ltype == 0xc0) {
    l = (l & 0x3f) << 8;
    l |= *q++;
    if (!r)
     r = q;
    hops++;
    if (hops > 255)
     return 0;
    q = p + l;
    if (q - p >= pl)
     return 0;
   } else {
    count += l + 1;
    if (out) {
     if ((long)l + 1 > len) {
      (*__errno_location ()) = 105;
      return -1;
     }
     memcpy(out, q, l);
     out += l;
     *out++ = '.';
     len -= l;
     len--;
    }
    q += l;
   }
  }
  if (out && out != start)
   *(out - 1) = ' ';
  if (r)
   q = r;
 }
 if (out) {
  if (out != start)
   *(out - 1) = '\0';
  else if (start_len > 0)
   *out = '\0';
 }
 return count;
}

variable_types:
out
char *

variable_types: len long

variable_types: pl int

variable_types:
start
char *

variable_types:
r
unsigned char *

variable_types:
q
unsigned char *

variable_types: count int

variable_types: l int

variable_types: hops int

variable_types:
ltype
unsigned char


decompiled_code:
__int64 __fastcall print_string(char *s, size_t maxlen, int a3, unsigned __int8 *a4)
{
unsigned __int8 *v4; // r14
unsigned __int8 *v7; // rbx
__int64 v8; // rbp
__int64 v9; // rcx
int v10; // eax
unsigned __int8 v11; // r15
unsigned __int8 *v12; // rax
const unsigned __int16 **v13; // rax
__int64 result; // rax
const unsigned __int16 **v15; // rax

v4 = &a4[a3];
if ( a4 < v4 )
{
v7 = a4;
v8 = 0LL;
while ( 1 )
{
v11 = *v7++;
if ( v11 )
{
v9 = v11;
if ( (v11 & 0x80u) != 0 )
goto LABEL_4;
v15 = _ctype_b_loc();
v9 = v11;
if ( ((*v15)[v11] & 0x4000) == 0 )
goto LABEL_4;
if ( v11 <= 0x27u )
{
if ( v11 > 0x21u && ((1LL << v11) & 0xD400000000LL) != 0 )
{
if ( !s )
{
LABEL_33:
++v8;
goto LABEL_17;
}
goto LABEL_27;
}
}
else if ( (unsigned __int8)(v11 - 92) <= 0x20u && ((1LL << (v11 - 92)) & 0x100000011LL) != 0 )
{
if ( !s )
goto LABEL_33;
LABEL_27:
if ( (__int64)maxlen <= 2 )
{
LABEL_34:
*_errno_location() = 105;
return -1LL;
}
*s = 92;
--maxlen;
++v8;
++s;
LABEL_16:
*s = v11;
--maxlen;
++s;
goto LABEL_17;
}
LABEL_15:
if ( s )
goto LABEL_16;
LABEL_17:
++v8;
if ( v4 == v7 )
goto LABEL_18;
}
else
{
v12 = v7;
if ( v4 > v7 )
{
while ( !*v12 )
{
if ( v4 == ++v12 )
goto LABEL_18;
}
}
if ( v4 == v12 )
goto LABEL_18;
v13 = _ctype_b_loc();
v9 = 0LL;
if ( (**v13 & 0x4000) != 0 )
goto LABEL_15;
LABEL_4:
if ( s )
{
if ( (__int64)maxlen <= 4 )
goto LABEL_34;
v10 = snprintf(s, maxlen, "\\%03o", v9);
maxlen -= v10;
v8 += v10;
s += v10;
}
else
{
v8 += 4LL;
}
if ( v4 == v7 )
{
LABEL_18:
result = v8 + 1;
goto LABEL_19;
}
}
}
}
result = 1LL;
LABEL_19:
if ( s )
*s = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s, size_t maxlen, int a3, unsigned __int8 *a4)
{
unsigned __int8 *v4; // r14
unsigned __int8 *v7; // rbx
__int64 v8; // rbp
__int64 v9; // rcx
int v10; // eax
unsigned __int8 v11; // r15
unsigned __int8 *v12; // rax
const unsigned __int16 **v13; // rax
__int64 result; // rax
const unsigned __int16 **v15; // rax

v4 = &a4[a3];
if ( a4 < v4 )
{
v7 = a4;
v8 = 0LL;
while ( 1 )
{
v11 = *v7++;
if ( v11 )
{
v9 = v11;
if ( (v11 & 0x80u) != 0 )
goto LABEL_4;
v15 = _ctype_b_loc();
v9 = v11;
if ( ((*v15)[v11] & 0x4000) == 0 )
goto LABEL_4;
if ( v11 <= 0x27u )
{
if ( v11 > 0x21u && ((1LL << v11) & 0xD400000000LL) != 0 )
{
if ( !s )
{
LABEL_33:
++v8;
goto LABEL_17;
}
goto LABEL_27;
}
}
else if ( (unsigned __int8)(v11 - 92) <= 0x20u && ((1LL << (v11 - 92)) & 0x100000011LL) != 0 )
{
if ( !s )
goto LABEL_33;
LABEL_27:
if ( (__int64)maxlen <= 2 )
{
LABEL_34:
*_errno_location() = 105;
return -1LL;
}
*s = 92;
--maxlen;
++v8;
++s;
LABEL_16:
*s = v11;
--maxlen;
++s;
goto LABEL_17;
}
LABEL_15:
if ( s )
goto LABEL_16;
LABEL_17:
++v8;
if ( v4 == v7 )
goto LABEL_18;
}
else
{
v12 = v7;
if ( v4 > v7 )
{
while ( !*v12 )
{
if ( v4 == ++v12 )
goto LABEL_18;
}
}
if ( v4 == v12 )
goto LABEL_18;
v13 = _ctype_b_loc();
v9 = 0LL;
if ( (**v13 & 0x4000) != 0 )
goto LABEL_15;
LABEL_4:
if ( s )
{
if ( (__int64)maxlen <= 4 )
goto LABEL_34;
v10 = snprintf(s, maxlen, "\\%03o", v9);
maxlen -= v10;
v8 += v10;
s += v10;
}
else
{
v8 += 4LL;
}
if ( v4 == v7 )
{
LABEL_18:
result = v8 + 1;
goto LABEL_19;
}
}
}
}
result = 1LL;
LABEL_19:
if ( s )
*s = 0;
return result;
}

original_code:
ssize_t
print_string(char *s, ssize_t len, int dl, const uint8_t *data)
{
 uint8_t c;
 const uint8_t *e, *p;
 ssize_t bytes = 0;
 ssize_t r;
 e = data + dl;
 while (data < e) {
  c = *data++;
  if (c == '\0') {
   for (p = data; p < e; p++)
    if (*p != '\0')
     break;
   if (p == e)
    break;
  }
  if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint)) {
   if (s) {
    if (len < 5) {
     (*__errno_location ()) = 105;
     return -1;
    }
    r = snprintf(s, len, "\\%03o", c);
    len -= r;
    bytes += r;
    s += r;
   } else
    bytes += 4;
   continue;
  }
  switch (c) {
  case '"':
  case '\'':
  case '$':
  case '`':
  case '\\':
  case '|':
  case '&':
   if (s) {
    if (len < 3) {
     (*__errno_location ()) = 105;
     return -1;
    }
    *s++ = '\\';
    len--;
   }
   bytes++;
   break;
  }
  if (s) {
   *s++ = c;
   len--;
  }
  bytes++;
 }
 if (s)
  *s = '\0';
 bytes++;
 return bytes;
}

canonical_original_code:
long print_string(char *s, long len, int dl, unsigned char *data)
{
 unsigned char c;
 unsigned char *e; unsigned char *p;
 long bytes = 0;
 long r;
 e = data + dl;
 while (data < e) {
  c = *data++;
  if (c == '\0') {
   for (p = data; p < e; p++)
    if (*p != '\0')
     break;
   if (p == e)
    break;
  }
  if (!(((c) & ~0x7f) == 0) || !((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint)) {
   if (s) {
    if (len < 5) {
     (*__errno_location ()) = 105;
     return -1;
    }
    r = snprintf(s, len, "\\%03o", c);
    len -= r;
    bytes += r;
    s += r;
   } else
    bytes += 4;
   continue;
  }
  switch (c) {
  case '"':
  case '\'':
  case '$':
  case '`':
  case '\\':
  case '|':
  case '&':
   if (s) {
    if (len < 3) {
     (*__errno_location ()) = 105;
     return -1;
    }
    *s++ = '\\';
    len--;
   }
   bytes++;
   break;
  }
  if (s) {
   *s++ = c;
   len--;
  }
  bytes++;
 }
 if (s)
  *s = '\0';
 bytes++;
 return bytes;
}

variable_types:
s
char *

variable_types: dl int

variable_types:
data
unsigned char *

variable_types:
c
unsigned char

variable_types:
e
unsigned char *

variable_types: bytes long

variable_types: r long