main

original_code:
int main(void)
{
    uint8_t *buf;
    point_t *points;
    color_t c;
    int i;
    struct timespec delay = { 0, 10000000 };
    if (sparkle_init() < 0) {
        fprintf(stderr, "Could not initialize sparkle\n");
        exit(1);
    }
    sparkle_max_luminance = 0x50;
    buf = malloc((3 * 60));
    points = calloc(4, sizeof(point_t));
    points[0].pos = 0 * 10;
    points[0].speed = 1;
    points[0].color.r = 0xff;
    points[0].color.g = 0x00;
    points[0].color.b = 0x00;
    points[1].pos = 15 * 10;
    points[1].speed = 1;
    points[1].color.r = 0x00;
    points[1].color.g = 0xff;
    points[1].color.b = 0x00;
    points[2].pos = 30 * 10;
    points[2].speed = 1;
    points[2].color.r = 0xff;
    points[2].color.g = 0x00;
    points[2].color.b = 0x00;
    points[3].pos = 45 * 10;
    points[3].speed = 1;
    points[3].color.r = 0x00;
    points[3].color.g = 0xff;
    points[3].color.b = 0x00;
    while ( 1 ) {
        for (i = 0; i < 4; i++) {
            points[i].pos = (points[i].pos + points[i].speed) % (60 * 10);
        }
        for (i = 0; i < 60; i++) {
            c = led_color(points, 4, i);
            buf[3*i+0] = c.g;
            buf[3*i+1] = c.r;
            buf[3*i+2] = c.b;
        }
        sparkle_write(buf, (3 * 60));
        nanosleep(&delay, ((void *)0));
    }
    sparkle_exit();
    return 0;
}

canonical_original_code:
int main()
{
    unsigned char *buf;
    struct point *points;
    struct color c;
    int i;
    struct timespec delay = { 0, 10000000 };
    if (sparkle_init() < 0) {
        fprintf(stderr, "Could not initialize sparkle\n");
        exit(1);
    }
    sparkle_max_luminance = 0x50;
    buf = malloc((3 * 60));
    points = calloc(4, sizeof(point_t));
    points[0].pos = 0 * 10;
    points[0].speed = 1;
    points[0].color.r = 0xff;
    points[0].color.g = 0x00;
    points[0].color.b = 0x00;
    points[1].pos = 15 * 10;
    points[1].speed = 1;
    points[1].color.r = 0x00;
    points[1].color.g = 0xff;
    points[1].color.b = 0x00;
    points[2].pos = 30 * 10;
    points[2].speed = 1;
    points[2].color.r = 0xff;
    points[2].color.g = 0x00;
    points[2].color.b = 0x00;
    points[3].pos = 45 * 10;
    points[3].speed = 1;
    points[3].color.r = 0x00;
    points[3].color.g = 0xff;
    points[3].color.b = 0x00;
    while ( 1 ) {
        for (i = 0; i < 4; i++) {
            points[i].pos = (points[i].pos + points[i].speed) % (60 * 10);
        }
        for (i = 0; i < 60; i++) {
            c = led_color(points, 4, i);
            buf[3*i+0] = c.g;
            buf[3*i+1] = c.r;
            buf[3*i+2] = c.b;
        }
        sparkle_write(buf, (3 * 60));
        nanosleep(&delay, ((void *)0));
    }
    sparkle_exit();
    return 0;
}

variable_types:
buf
unsigned char *

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

user_defined_types: struct color { unsigned char r; unsigned char g; unsigned char b; }


canonical_name: main

repo: markusfisch/libhttp

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r12
const char **v4; // r13
int v5; // ebx
void *v7[527]; // [rsp+0h] [rbp-1078h] BYREF

if ( argc == 1 )
return 0;
v3 = argv;
v4 = &argv[argc - 1];
do
{
v5 = http_request(*++v3, argv, envp);
if ( v5 <= 0 )
{
perror("http_request");
}
else
{
memset(v7, 0, 0x1048uLL);
while ( 1 )
{
argv = (const char **)v7;
if ( (int)http_response((unsigned int)v5, v7) <= 0 )
break;
if ( v7[1] )
write(1, v7[1], SLODWORD(v7[2]));
}
close(v5);
envp = (const char **)LODWORD(v7[0]);
if ( LODWORD(v7[0]) != 200 )
{
argv = (const char **)"error: returned HTTP code %d\n";
fprintf(stderr, "error: returned HTTP code %d\n", LODWORD(v7[0]));
}
}
}
while ( v3 != v4 );
return 0;
}

original_code:
int main(int argc, char **argv) {
 while (--argc) {
  int sd;
  struct http_message msg;
  if ((sd = http_request(*++argv)) < 1) {
   perror("http_request");
   continue;
  }
  memset(&msg, 0, sizeof(msg));
  while (http_response(sd, &msg) > 0)
   if (msg.content) {
    write(1, msg.content, msg.length);
   }
  close(sd);
  if (msg.header.code != 200) {
   fprintf(
    stderr,
    "error: returned HTTP code %d\n",
    msg.header.code);
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: sd int

variable_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; }

return_type: int


original_code:
static char *http_parse_content(
  struct http_message *msg,
  char *bod,
  char *eod) {
 int len = eod - bod;
 if (msg->state.chunk > -1) {
  if (msg->state.chunk < len) {
   char *eoc;
   if (msg->state.chunk > 0) {
    if (msg->state.chunk == 1) {
     ++bod;
     msg->length = 0;
     msg->state.chunk = 0;
    } else {
     msg->length = msg->state.chunk;
     msg->content = bod;
     bod += msg->state.chunk;
     msg->state.chunk = 0;
     http_cut_trailing_crlf(msg);
    }
    return bod;
   }
   if (!(eoc = strchr(bod, '\n'))) {
    return bod;
   }
   *eoc = 0;
   sscanf(bod, "%x", &msg->state.chunk);
   msg->state.chunk += 2;
   msg->content = ++eoc;
   len = eod - msg->content;
   if (msg->state.chunk < len) {
    msg->length = msg->state.chunk;
    bod = msg->content + msg->state.chunk;
    msg->state.chunk = 0;
    http_cut_trailing_crlf(msg);
    return bod;
   } else {
    msg->length = len;
    msg->state.chunk -= len;
    http_cut_trailing_crlf(msg);
    return eod;
   }
  } else {
   msg->state.chunk -= len;
  }
 }
 msg->content = bod;
 msg->length = len;
 http_cut_trailing_crlf(msg);
 return eod;
}

canonical_original_code:
static char *http_parse_content(struct http_message *msg, char *bod, char *eod) {
 int len = eod - bod;
 if (msg->state.chunk > -1) {
  if (msg->state.chunk < len) {
   char *eoc;
   if (msg->state.chunk > 0) {
    if (msg->state.chunk == 1) {
     ++bod;
     msg->length = 0;
     msg->state.chunk = 0;
    } else {
     msg->length = msg->state.chunk;
     msg->content = bod;
     bod += msg->state.chunk;
     msg->state.chunk = 0;
     http_cut_trailing_crlf(msg);
    }
    return bod;
   }
   if (!(eoc = strchr(bod, '\n'))) {
    return bod;
   }
   *eoc = 0;
   sscanf(bod, "%x", &msg->state.chunk);
   msg->state.chunk += 2;
   msg->content = ++eoc;
   len = eod - msg->content;
   if (msg->state.chunk < len) {
    msg->length = msg->state.chunk;
    bod = msg->content + msg->state.chunk;
    msg->state.chunk = 0;
    http_cut_trailing_crlf(msg);
    return bod;
   } else {
    msg->length = len;
    msg->state.chunk -= len;
    http_cut_trailing_crlf(msg);
    return eod;
   }
  } else {
   msg->state.chunk -= len;
  }
 }
 msg->content = bod;
 msg->length = len;
 http_cut_trailing_crlf(msg);
 return eod;
}

variable_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; } *

variable_types:
bod
char *

variable_types:
eod
char *

variable_types: len int

variable_types:
eoc
char *

return_type: char *


decompiled_code:
char *__fastcall http_parse_message(int *a1, const char *a2, const char *a3)
{
const char *v3; // rbx
char *v6; // r12
char *result; // rax
const char *v8; // rsi
char *v9; // rax
const char *v10; // rbp
char *v11; // rbx
char *v12; // rbp

v3 = a2;
if ( !a2 )
return 0LL;
if ( a1[6] )
return (char *)http_parse_content((__int64)a1, a2, a3);
v6 = strchr(a2, 10);
if ( !v6 )
return (char *)a2;
if ( *a1 )
{
while ( 1 )
{
*v6 = 0;
if ( !*v3 )
break;
if ( !strcmp(v3, "\r") )
{
v8 = v3 + 2;
goto LABEL_10;
}
v9 = strchr(v3, 58);
if ( v9 )
{
*v9 = 0;
v10 = v9 + 1;
v11 = (char *)&v3[strspn(v3, " \t\r\n")];
strtok(v11, " \t\r\n");
v12 = (char *)&v10[strspn(v10, " \t\r\n")];
strtok(v12, " \t\r\n");
if ( !strcasecmp(v11, "Transfer-Encoding") && !strcasecmp(v12, "chunked") )
{
a1[7] = 0;
}
else if ( !strcasecmp(v11, "Content-Length") )
{
a1[1] = atoi(v12);
}
}
v3 = v6 + 1;
v6 = strchr(v6 + 1, 10);
if ( !v6 )
return (char *)v3;
}
v8 = v3 + 1;
LABEL_10:
a1[6] = 1;
return (char *)http_parse_message(a1, v8, a3);
}
if ( memcmp(a2, "HTTP/1.", 7uLL) )
return 0LL;
result = strchr("01", v3[7]);
if ( result )
{
*a1 = atoi(v3 + 8);
result = (char *)http_parse_message(a1, v6 + 1, a3);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(int *a1, const char *a2, const char *a3)
{
const char *v3; // rbx
char *v6; // r12
char *result; // rax
const char *v8; // rsi
char *v9; // rax
const char *v10; // rbp
char *v11; // rbx
char *v12; // rbp

v3 = a2;
if ( !a2 )
return 0LL;
if ( a1[6] )
return (char *)func1((__int64)a1, a2, a3);
v6 = strchr(a2, 10);
if ( !v6 )
return (char *)a2;
if ( *a1 )
{
while ( 1 )
{
*v6 = 0;
if ( !*v3 )
break;
if ( !strcmp(v3, "\r") )
{
v8 = v3 + 2;
goto LABEL_10;
}
v9 = strchr(v3, 58);
if ( v9 )
{
*v9 = 0;
v10 = v9 + 1;
v11 = (char *)&v3[strspn(v3, " \t\r\n")];
strtok(v11, " \t\r\n");
v12 = (char *)&v10[strspn(v10, " \t\r\n")];
strtok(v12, " \t\r\n");
if ( !strcasecmp(v11, "Transfer-Encoding") && !strcasecmp(v12, "chunked") )
{
a1[7] = 0;
}
else if ( !strcasecmp(v11, "Content-Length") )
{
a1[1] = atoi(v12);
}
}
v3 = v6 + 1;
v6 = strchr(v6 + 1, 10);
if ( !v6 )
return (char *)v3;
}
v8 = v3 + 1;
LABEL_10:
a1[6] = 1;
return (char *)func2(a1, v8, a3);
}
if ( memcmp(a2, "HTTP/1.", 7uLL) )
return 0LL;
result = strchr("01", v3[7]);
if ( result )
{
*a1 = atoi(v3 + 8);
result = (char *)func2(a1, v6 + 1, a3);
}
return result;
}

original_code:
static char *http_parse_message(
  struct http_message *msg,
  char *bod,
  char *eod) {
 char *lf;
 if (!bod) {
  return bod;
 }
 if (msg->state.in_content) {
  return http_parse_content(msg, bod, eod);
 }
 if (!(lf = strchr(bod, '\n'))) {
  return bod;
 }
 if (!msg->header.code) {
  if (strncmp(bod, "HTTP/1.", 7) ||
    !strchr("01", *(bod + 7))) {
   return ((void *)0);
  }
  bod += 8;
  msg->header.code = atoi(bod);
  return http_parse_message(msg, ++lf, eod);
 }
 for (; lf; bod = ++lf, lf = strchr(bod, '\n')) {
  char *value;
  *lf = 0;
  if (!*bod || !strcmp(bod, "\r")) {
   if (!*bod) {
    ++bod;
   } else {
    bod += 2;
   }
   msg->state.in_content = 1;
   return http_parse_message(msg, bod, eod);
  }
  if (!(value = strchr(bod, ':'))) {
   continue;
  }
  *value = 0;
  ++value;
  bod += strspn(bod, " \t\r\n");
  strtok(bod, " \t\r\n");
  value += strspn(value, " \t\r\n");
  strtok(value, " \t\r\n");
  if (!strcasecmp(bod, "Transfer-Encoding") &&
    !strcasecmp(value, "chunked")) {
   msg->state.chunk = 0;
  } else if (!strcasecmp(bod, "Content-Length")) {
   msg->header.length = atoi(value);
  }
 }
 return bod;
}

canonical_original_code:
static char *http_parse_message(struct http_message *msg, char *bod, char *eod) {
 char *lf;
 if (!bod) {
  return bod;
 }
 if (msg->state.in_content) {
  return http_parse_content(msg, bod, eod);
 }
 if (!(lf = strchr(bod, '\n'))) {
  return bod;
 }
 if (!msg->header.code) {
  if (strncmp(bod, "HTTP/1.", 7) ||
    !strchr("01", *(bod + 7))) {
   return ((void *)0);
  }
  bod += 8;
  msg->header.code = atoi(bod);
  return http_parse_message(msg, ++lf, eod);
 }
 for (; lf; bod = ++lf, lf = strchr(bod, '\n')) {
  char *value;
  *lf = 0;
  if (!*bod || !strcmp(bod, "\r")) {
   if (!*bod) {
    ++bod;
   } else {
    bod += 2;
   }
   msg->state.in_content = 1;
   return http_parse_message(msg, bod, eod);
  }
  if (!(value = strchr(bod, ':'))) {
   continue;
  }
  *value = 0;
  ++value;
  bod += strspn(bod, " \t\r\n");
  strtok(bod, " \t\r\n");
  value += strspn(value, " \t\r\n");
  strtok(value, " \t\r\n");
  if (!strcasecmp(bod, "Transfer-Encoding") &&
    !strcasecmp(value, "chunked")) {
   msg->state.chunk = 0;
  } else if (!strcasecmp(bod, "Content-Length")) {
   msg->header.length = atoi(value);
  }
 }
 return bod;
}

variable_types:
lf
char *

variable_types:
value
char *

user_defined_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; }


decompiled_code:
char *__fastcall http_parse_url(void *src)
{
size_t v1; // rax
size_t v2; // r12
char *v3; // rbx
char *v4; // rax
const char *v5; // r13
char *v6; // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
const char *v10; // rdx
bool v11; // zf
const char *v12; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src);
v2 = v1;
v3 = 0LL;
if ( v1 )
{
v4 = (char *)calloc(1uLL, v1 + 25);
v3 = v4;
if ( v4 )
{
v5 = v4 + 24;
memcpy(v4 + 24, src, v2);
v6 = strstr(v5, "://");
if ( v6 )
{
*v6 = 0;
*(_QWORD *)v3 = v5;
v5 = v6 + 3;
}
*((_QWORD *)v3 + 1) = v5;
v7 = strchr(v5, 47);
if ( v7 )
{
*v7 = 0;
*((_QWORD *)v3 + 2) = v7 + 1;
v8 = strchr(v7 + 1, 35);
if ( v8 )
*v8 = 0;
}
else
{
*((_QWORD *)v3 + 2) = &unk_89C;
}
v9 = strchr(*((const char **)v3 + 1), 58);
v10 = v9 + 1;
v11 = v9 == 0LL;
v12 = "http";
if ( !v11 )
v12 = v10;
*(_QWORD *)v3 = v12;
}
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func3(void *src)
{
size_t v1; // rax
size_t v2; // r12
char *v3; // rbx
char *v4; // rax
const char *v5; // r13
char *v6; // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
const char *v10; // rdx
bool v11; // zf
const char *v12; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src);
v2 = v1;
v3 = 0LL;
if ( v1 )
{
v4 = (char *)calloc(1uLL, v1 + 25);
v3 = v4;
if ( v4 )
{
v5 = v4 + 24;
memcpy(v4 + 24, src, v2);
v6 = strstr(v5, "://");
if ( v6 )
{
*v6 = 0;
*(_QWORD *)v3 = v5;
v5 = v6 + 3;
}
*((_QWORD *)v3 + 1) = v5;
v7 = strchr(v5, 47);
if ( v7 )
{
*v7 = 0;
*((_QWORD *)v3 + 2) = v7 + 1;
v8 = strchr(v7 + 1, 35);
if ( v8 )
*v8 = 0;
}
else
{
*((_QWORD *)v3 + 2) = &unk_89C;
}
v9 = strchr(*((const char **)v3 + 1), 58);
v10 = v9 + 1;
v11 = v9 == 0LL;
v12 = "http";
if ( !v11 )
v12 = v10;
*(_QWORD *)v3 = v12;
}
}
return v3;
}

original_code:
struct http_url *http_parse_url(const char *url) {
 struct http_url *hu;
 size_t len;
 char *buf;
 char *p;
 if (!url ||
   (len = strlen(url)) < 1 ||
   !(hu = calloc(1, sizeof(struct http_url) + len + 1))) {
  return ((void *)0);
 }
 buf = (char *) hu + sizeof(struct http_url);
 memcpy(buf, url, len);
 if ((p = strstr(buf, "://"))) {
  *p = 0;
  hu->protocol = buf;
  buf = p + 3;
 }
 hu->host = buf;
 if ((p = strchr(buf, '/'))) {
  *p = 0;
  hu->query = ++p;
  if ((p = strchr(p, '#'))) {
   *p = 0;
  }
 } else {
  hu->query = "";
 }
 if ((p = strchr(hu->host, ':'))) {
  hu->protocol = ++p;
 } else {
  hu->protocol = "http";
 }
 return hu;
}

canonical_original_code:
struct http_url *http_parse_url(char *url) {
 struct http_url *hu;
 unsigned long len;
 char *buf;
 char *p;
 if (!url ||
   (len = strlen(url)) < 1 ||
   !(hu = calloc(1, sizeof(struct http_url) + len + 1))) {
  return ((void *)0);
 }
 buf = (char *) hu + sizeof(struct http_url);
 memcpy(buf, url, len);
 if ((p = strstr(buf, "://"))) {
  *p = 0;
  hu->protocol = buf;
  buf = p + 3;
 }
 hu->host = buf;
 if ((p = strchr(buf, '/'))) {
  *p = 0;
  hu->query = ++p;
  if ((p = strchr(p, '#'))) {
   *p = 0;
  }
 } else {
  hu->query = "";
 }
 if ((p = strchr(hu->host, ':'))) {
  hu->protocol = ++p;
 } else {
  hu->protocol = "http";
 }
 return hu;
}

variable_types:
url
char *

variable_types: struct http_url { char *protocol; char *host; char *query; } *

variable_types:
len
unsigned long

variable_types:
buf
char *

variable_types:
p
char *


decompiled_code:
__int64 __fastcall http_send(int fd, void *buf)
{
char *v2; // rbp
size_t v3; // rbx
int v4; // eax

if ( !buf )
return 0xFFFFFFFFLL;
v2 = (char *)buf;
v3 = strlen((const char *)buf);
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = send(fd, v2, v3, 0);
if ( v4 < 0 )
break;
v2 += v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, void *buf)
{
char *v2; // rbp
size_t v3; // rbx
int v4; // eax

if ( !buf )
return 0xFFFFFFFFLL;
v2 = (char *)buf;
v3 = strlen((const char *)buf);
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = send(fd, v2, v3, 0);
if ( v4 < 0 )
break;
v2 += v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int http_send(int sd, const char *rq) {
 size_t l;
 if (!rq) {
  return -1;
 }
 for (l = strlen(rq); l > 0;) {
  int bytes = send(sd, rq, l, 0);
  if (bytes < 0) {
   return -1;
  }
  rq += bytes;
  l -= bytes;
 }
 return 0;
}

canonical_original_code:
int http_send(int sd, char *rq) {
 unsigned long l;
 if (!rq) {
  return -1;
 }
 for (l = strlen(rq); l > 0;) {
  int bytes = send(sd, rq, l, 0);
  if (bytes < 0) {
   return -1;
  }
  rq += bytes;
  l -= bytes;
 }
 return 0;
}

variable_types:
rq
char *

variable_types:
l
unsigned long

variable_types: bytes int


original_code:
int http_read(int sd, struct http_message *msg) {
 if (!msg) {
  return -1;
 }
 if (!msg->state.offset) {
  msg->state.size = sizeof(msg->state.buf) - 1;
  msg->state.offset = msg->state.buf;
  msg->state.chunk = -1;
  msg->header.length = -1;
 }
 if (msg->state.total == msg->header.length) {
  return 0;
 }
 for (;;) {
  if (msg->state.left > 0) {
   char *parsed_until;
   msg->length = 0;
   parsed_until = http_parse_message(
    msg,
    msg->state.offset,
    msg->state.offset + msg->state.left);
   if (parsed_until > msg->state.offset) {
    msg->state.left -= parsed_until - msg->state.offset;
    msg->state.offset = parsed_until;
    msg->state.total += msg->length;
    return 1;
   }
  }
  if (msg->state.offset > msg->state.buf) {
   if (msg->state.left > 0)
    memmove(
     msg->state.buf,
     msg->state.offset,
     msg->state.left);
   msg->state.offset = msg->state.buf;
  }
  {
   char *append = msg->state.offset + msg->state.left;
   int bytes,
    size = (msg->state.buf + msg->state.size)-append;
   if (size < 1) {
    int half = msg->state.size >> 1;
    memcpy(
     msg->state.buf,
     msg->state.buf + half,
     half);
    msg->state.offset = msg->state.buf;
    msg->state.left = half;
    append = msg->state.offset + msg->state.left;
    size = msg->state.size - msg->state.left;
   }
   if ((bytes = recv(
     sd,
     append,
     size,
     0)) < 1) {
    return 0;
   }
   append[bytes] = 0;
   msg->state.left += bytes;
  }
 }
 return 0;
}

canonical_original_code:
int http_read(int sd, struct http_message *msg) {
 if (!msg) {
  return -1;
 }
 if (!msg->state.offset) {
  msg->state.size = sizeof(msg->state.buf) - 1;
  msg->state.offset = msg->state.buf;
  msg->state.chunk = -1;
  msg->header.length = -1;
 }
 if (msg->state.total == msg->header.length) {
  return 0;
 }
 for (;;) {
  if (msg->state.left > 0) {
   char *parsed_until;
   msg->length = 0;
   parsed_until = http_parse_message(
    msg,
    msg->state.offset,
    msg->state.offset + msg->state.left);
   if (parsed_until > msg->state.offset) {
    msg->state.left -= parsed_until - msg->state.offset;
    msg->state.offset = parsed_until;
    msg->state.total += msg->length;
    return 1;
   }
  }
  if (msg->state.offset > msg->state.buf) {
   if (msg->state.left > 0)
    memmove(
     msg->state.buf,
     msg->state.offset,
     msg->state.left);
   msg->state.offset = msg->state.buf;
  }
  {
   char *append = msg->state.offset + msg->state.left;
   int bytes; int size = (msg->state.buf + msg->state.size)-append;
   if (size < 1) {
    int half = msg->state.size >> 1;
    memcpy(
     msg->state.buf,
     msg->state.buf + half,
     half);
    msg->state.offset = msg->state.buf;
    msg->state.left = half;
    append = msg->state.offset + msg->state.left;
    size = msg->state.size - msg->state.left;
   }
   if ((bytes = recv(
     sd,
     append,
     size,
     0)) < 1) {
    return 0;
   }
   append[bytes] = 0;
   msg->state.left += bytes;
  }
 }
 return 0;
}

variable_types: sd int

variable_types:
append
char *

variable_types: size int

variable_types: half int


decompiled_code:
__int64 __fastcall http_request(void *a1)
{
char *v1; // rax
void **v2; // rbp
int v3; // eax
unsigned int v4; // ebx

v1 = http_parse_url(a1);
v2 = (void **)v1;
if ( v1 && (v3 = http_connect((__int64)v1), v4 = v3, v3 >= 0) )
{
if ( (unsigned int)http_send(v3, "GET /")
|| (unsigned int)http_send(v4, v2[2])
|| (unsigned int)http_send(v4, " HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (Linux)\r\nHost: ")
|| (unsigned int)http_send(v4, v2[1])
|| (unsigned int)http_send(v4, "\r\nAccept: */*\r\nConnection: close\r\n\r\n") )
{
close(v4);
v4 = -1;
}
else
{
free(v2);
}
}
else
{
free(v2);
v4 = -1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *a1)
{
char *v1; // rax
void **v2; // rbp
int v3; // eax
unsigned int v4; // ebx

v1 = func3(a1);
v2 = (void **)v1;
if ( v1 && (v3 = func4((__int64)v1), v4 = v3, v3 >= 0) )
{
if ( (unsigned int)func5(v3, "GET /")
|| (unsigned int)func5(v4, v2[2])
|| (unsigned int)func5(v4, " HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (Linux)\r\nHost: ")
|| (unsigned int)func5(v4, v2[1])
|| (unsigned int)func5(v4, "\r\nAccept: */*\r\nConnection: close\r\n\r\n") )
{
close(v4);
v4 = -1;
}
else
{
free(v2);
}
}
else
{
free(v2);
v4 = -1;
}
return v4;
}

original_code:
int http_request(const char *url) {
 struct http_url *hu;
 int sd;
 if (!(hu = http_parse_url(url)) ||
   (sd = http_connect(hu)) < 0) {
  free(hu);
  return -1;
 }
 if (http_send(sd, "GET /") ||
   http_send(sd, hu->query) ||
   http_send(sd, " HTTP/1.1\r\nUser-Agent: ""Mozilla/4.0 (Linux)""\r\nHost: ") ||
   http_send(sd, hu->host) ||
   http_send(sd, "\r\nAccept: */*\r\nConnection: close\r\n\r\n")) {
  close(sd);
  return -1;
 }
 free(hu);
 return sd;
}

canonical_original_code:
int http_request(char *url) {
 struct http_url *hu;
 int sd;
 if (!(hu = http_parse_url(url)) ||
   (sd = http_connect(hu)) < 0) {
  free(hu);
  return -1;
 }
 if (http_send(sd, "GET /") ||
   http_send(sd, hu->query) ||
   http_send(sd, " HTTP/1.1\r\nUser-Agent: ""Mozilla/4.0 (Linux)""\r\nHost: ") ||
   http_send(sd, hu->host) ||
   http_send(sd, "\r\nAccept: */*\r\nConnection: close\r\n\r\n")) {
  close(sd);
  return -1;
 }
 free(hu);
 return sd;
}

user_defined_types: struct http_url { char *protocol; char *host; char *query; }


decompiled_code:
__int64 __fastcall http_response(int fd, __int64 a2)
{
__int64 v2; // r12
struct timeval v4; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v4.tv_sec = 360LL;
v4.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
v2 = 1LL << (fd % 64);
readfds.fds_bits[fd / 64] |= v2;
if ( select(fd + 1, &readfds, 0LL, 0LL, &v4) <= 0 )
return 0xFFFFFFFFLL;
if ( (readfds.fds_bits[fd / 64] & v2) != 0 )
return http_read(fd, a2);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, __int64 a2)
{
__int64 v2; // r12
struct timeval v4; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v4.tv_sec = 360LL;
v4.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
v2 = 1LL << (fd % 64);
readfds.fds_bits[fd / 64] |= v2;
if ( select(fd + 1, &readfds, 0LL, 0LL, &v4) <= 0 )
return 0xFFFFFFFFLL;
if ( (readfds.fds_bits[fd / 64] & v2) != 0 )
return func6(fd, a2);
return 0xFFFFFFFFLL;
}

original_code:
int http_response(int sd, struct http_message *msg) {
 fd_set r;
 struct timeval tv;
 tv.tv_sec = 360;
 tv.tv_usec = 0;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&r)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&r)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
 if (select(sd + 1, &r, ((void *)0), ((void *)0), &tv) < 1 ||
   !((((&r)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  return -1;
 }
 return http_read(sd, msg);
}

canonical_original_code:
int http_response(int sd, struct http_message *msg) {
 struct fd_set r;
 struct timeval tv;
 tv.tv_sec = 360;
 tv.tv_usec = 0;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&r)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&r)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
 if (select(sd + 1, &r, ((void *)0), ((void *)0), &tv) < 1 ||
   !((((&r)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  return -1;
 }
 return http_read(sd, msg);
}

variable_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; } *

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

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


decompiled_code:
__int64 allocate_memory_leser()
{
__int64 v0; // rbx

puts("allocating memory...");
v0 = TotNumPart;
P2 = (__int64)malloc(48LL * TotNumPart);
if ( !P2 )
{
fwrite("failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
Id = (__int64)malloc(4 * v0);
if ( !Id )
{
fwrite("failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
puts("allocating memory...done");
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx

puts("allocating memory...");
v0 = TotNumPart;
P2 = (__int64)malloc(48LL * TotNumPart);
if ( !P2 )
{
fwrite("failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
Id = (__int64)malloc(4 * v0);
if ( !Id )
{
fwrite("failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
puts("allocating memory...done");
return 0LL;
}

original_code:
int allocate_memory_leser(void)
{
  printf("allocating memory...\n");
  if(!(P2=malloc(TotNumPart*sizeof(struct particle_data2))))
    {
      fprintf(stderr,"failed to allocate memory.\n");
      exit(0);
    }
  if(!(Id=malloc(TotNumPart*sizeof(int))))
    {
      fprintf(stderr,"failed to allocate memory.\n");
      exit(0);
    }
  printf("allocating memory...done\n");
  return 0;
}

canonical_original_code:
int allocate_memory_leser()
{
  printf("allocating memory...\n");
  if(!(P2=malloc(TotNumPart*sizeof(struct particle_data2))))
    {
      fprintf(stderr,"failed to allocate memory.\n");
      exit(0);
    }
  if(!(Id=malloc(TotNumPart*sizeof(int))))
    {
      fprintf(stderr,"failed to allocate memory.\n");
      exit(0);
    }
  printf("allocating memory...done\n");
  return 0;
}

user_defined_types: struct particle_data2 { float Pos[3]; float Vel[3]; float Mass; int Type; float Rho; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
unsigned int j; // ebx
unsigned int i; // ebp

N22 = (int)&unk_200000;
N = (int)&unk_40000;
TotNumPart = (int)&unk_200000;
lpath = (__int64)argv[1];
spath = (__int64)argv[2];
v3 = 0;
while ( 2 )
{
dichterechner(v3);
getPoten();
FFTW_dist();
for ( i = 0; ; ++i )
{
FFTW_rho(i, v3);
if ( !i )
{
speicher_pot_fits(v3, 0LL);
free(Phi);
puts("juhu");
continue;
}
if ( (int)i > 0 )
{
for ( j = 0; j != i; ++j )
{
printf("otto: %i --- n: %i\n", j, i);
getbackPoten(j, i);
}
}
speicher_pot_fits(v3, i);
free(Phi);
puts("juhu");
if ( (int)(i + 1) > 3 )
break;
}
free(POSITION_X);
free(POSITION_Y);
free(POSITION_Z);
free(MASSE);
free(Rho);
fftw_free(rhoft);
fftw_free(rho);
fftw_free(distft);
fftw_free(backpot);
fftw_free(dist);
if ( ++v3 != 200 )
continue;
break;
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
  N22 = (2*64)*(2*64)*(2*64);
  N = (64)*(64)*(64);
  TotNumPart=128*128*128;
  int n, otto, erwin, ii;
  lpath=argv[1];
  spath=argv[2];
   for (ii=0; ii < 200; ii++ )
   {
   erwin = ii;
   dichterechner(erwin);
   getPoten();
   FFTW_dist();
   for (n = 0; n<4; n++)
       {
 FFTW_rho(n, erwin);
        if (n!=0)
   {
    for (otto=0; otto < n; otto++)
       {
             printf("otto: %i --- n: %i\n", otto, n);
      getbackPoten(otto,n);
        }
  }
             speicher_pot_fits(erwin, n);
        free(Phi);
             printf("juhu\n") ;
        }
        free(POSITION_X);
        free(POSITION_Y);
        free(POSITION_Z);
        free(MASSE);
        free(Rho);
        fftw_free(rhoft);
        fftw_free(rho);
        fftw_free(distft);
        fftw_free(backpot);
        fftw_free(dist);
      }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  N22 = (2*64)*(2*64)*(2*64);
  N = (64)*(64)*(64);
  TotNumPart=128*128*128;
  int n; int otto; int erwin; int ii;
  lpath=argv[1];
  spath=argv[2];
   for (ii=0; ii < 200; ii++ )
   {
   erwin = ii;
   dichterechner(erwin);
   getPoten();
   FFTW_dist();
   for (n = 0; n<4; n++)
       {
 FFTW_rho(n, erwin);
        if (n!=0)
   {
    for (otto=0; otto < n; otto++)
       {
             printf("otto: %i --- n: %i\n", otto, n);
      getbackPoten(otto,n);
        }
  }
             speicher_pot_fits(erwin, n);
        free(Phi);
             printf("juhu\n") ;
        }
        free(POSITION_X);
        free(POSITION_Y);
        free(POSITION_Z);
        free(MASSE);
        free(Rho);
        fftw_free(rhoft);
        fftw_free(rho);
        fftw_free(distft);
        fftw_free(backpot);
        fftw_free(dist);
      }
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: n int

variable_types: otto int

variable_types: erwin int

variable_types: ii int


usage

repo: markushx/librpl

decompiled_code:
int __fastcall usage(const char *a1, const char *a2)
{
const char *v2; // rbx
char *v3; // rax

v2 = a1;
v3 = strrchr(a1, 47);
if ( v3 )
v2 = v3 + 1;
return fprintf(
stderr,
"%s v%s -- a RPL root node implementation derived from Contiki\n"
"(c) 2012 Markus Becker <mab@comnets.uni-bremen.de>\n"
"\n"
"usage: %s [-i interface] [-s sourceaddress]\n"
"\n"
"\t-i interface\tinterface to operate the RPL root node on\n"
"\t-s sourceaddress\tsourceaddress to use\n",
v2,
a2,
v2);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2)
{
const char *v2; // rbx
char *v3; // rax

v2 = a1;
v3 = strrchr(a1, 47);
if ( v3 )
v2 = v3 + 1;
return fprintf(
stderr,
"%s v%s -- a RPL root node implementation derived from Contiki\n"
"(c) 2012 Markus Becker <mab@comnets.uni-bremen.de>\n"
"\n"
"usage: %s [-i interface] [-s sourceaddress]\n"
"\n"
"\t-i interface\tinterface to operate the RPL root node on\n"
"\t-s sourceaddress\tsourceaddress to use\n",
v2,
a2,
v2);
}

original_code:
void
usage( const char *program, const char *version) {
  const char *p;
  p = strrchr( program, '/' );
  if ( p )
    program = ++p;
  fprintf( stderr, "%s v%s -- a RPL root node implementation derived from Contiki\n"
    "(c) 2012 Markus Becker <mab@comnets.uni-bremen.de>\n\n"
    "usage: %s [-i interface] [-s sourceaddress]\n\n"
    "\t-i interface\tinterface to operate the RPL root node on\n"
    "\t-s sourceaddress\tsourceaddress to use\n",
    program, version, program );
}

canonical_original_code:
void usage(char *program, char *version) {
  char *p;
  p = strrchr( program, '/' );
  if ( p )
    program = ++p;
  fprintf( stderr, "%s v%s -- a RPL root node implementation derived from Contiki\n"
    "(c) 2012 Markus Becker <mab@comnets.uni-bremen.de>\n\n"
    "usage: %s [-i interface] [-s sourceaddress]\n\n"
    "\t-i interface\tinterface to operate the RPL root node on\n"
    "\t-s sourceaddress\tsourceaddress to use\n",
    program, version, program );
}

variable_types:
program
char *

variable_types:
version
char *

variable_types:
p
char *

return_type: void


decompiled_code:
void __noreturn main_loop()
{
int v0; // eax
unsigned int v1; // [rsp+4h] [rbp-624h] BYREF
__int64 v2; // [rsp+8h] [rbp-620h] BYREF
char v3[32]; // [rsp+10h] [rbp-618h] BYREF
char v4[1504]; // [rsp+30h] [rbp-5F8h] BYREF
struct pollfd fds; // [rsp+610h] [rbp-18h] BYREF
__int64 v6; // [rsp+618h] [rbp-10h]

*(_DWORD *)&fds.events = 1;
v6 = 0LL;
fds.fd = sock;
while ( 1 )
{
while ( poll(&fds, 2uLL, 50) <= 0 )
;
if ( (fds.revents & 0x38) != 0 )
{
fwrite("socket error on fds[0].fd", 1uLL, 0x19uLL, stderr);
}
else if ( (fds.revents & 1) != 0 )
{
v2 = 0LL;
fwrite("incoming ICMPv6 message?!", 1uLL, 0x19uLL, stdout);
v0 = recv_dao(v4, v3, &v2, &v1);
if ( v0 > 0 )
process(v4, (unsigned int)v0, v3, v2, v1);
}
}
}

canonical_decompiled_code:
void __noreturn func1()
{
int v0; // eax
unsigned int v1; // [rsp+4h] [rbp-624h] BYREF
__int64 v2; // [rsp+8h] [rbp-620h] BYREF
char v3[32]; // [rsp+10h] [rbp-618h] BYREF
char v4[1504]; // [rsp+30h] [rbp-5F8h] BYREF
struct pollfd fds; // [rsp+610h] [rbp-18h] BYREF
__int64 v6; // [rsp+618h] [rbp-10h]

*(_DWORD *)&fds.events = 1;
v6 = 0LL;
fds.fd = sock;
while ( 1 )
{
while ( poll(&fds, 2uLL, 50) <= 0 )
;
if ( (fds.revents & 0x38) != 0 )
{
fwrite("socket error on fds[0].fd", 1uLL, 0x19uLL, stderr);
}
else if ( (fds.revents & 1) != 0 )
{
v2 = 0LL;
fwrite("incoming ICMPv6 message?!", 1uLL, 0x19uLL, stdout);
v0 = recv_dao(v4, v3, &v2, &v1);
if ( v0 > 0 )
process(v4, (unsigned int)v0, v3, v2, v1);
}
}
}

original_code:
void main_loop(void)
{
  struct pollfd fds[2];
  memset(fds, 0, sizeof(fds));
  fds[0].fd = sock;
  fds[0].events = 0x001;
  fds[0].revents = 0;
  for (;;) {
    int timeout = 50;
    int rc;
    rc = poll(fds, sizeof(fds)/sizeof(fds[0]), timeout);
    if (rc > 0) {
      if (fds[0].revents & (0x008 | 0x010 | 0x020)) {
 fprintf(stderr, "socket error on fds[0].fd");
      }
      else if (fds[0].revents & 0x001) {
 int len, hoplimit;
 struct sockaddr_in6 rcv_addr;
 struct in6_pktinfo *pkt_info = ((void *)0);
 unsigned char msg[1500];
 fprintf(stdout, "incoming ICMPv6 message?!");
 len = recv_dao(msg, &rcv_addr, &pkt_info, &hoplimit);
 if (len > 0) {
   process(msg, len, &rcv_addr, pkt_info, hoplimit);
 }
      }
    }
    else if ( rc == -1 ) {
    }
  }
}

canonical_original_code:
void main_loop()
{
  struct pollfd fds[2];
  memset(fds, 0, sizeof(fds));
  fds[0].fd = sock;
  fds[0].events = 0x001;
  fds[0].revents = 0;
  for (;;) {
    int timeout = 50;
    int rc;
    rc = poll(fds, sizeof(fds)/sizeof(fds[0]), timeout);
    if (rc > 0) {
      if (fds[0].revents & (0x008 | 0x010 | 0x020)) {
 fprintf(stderr, "socket error on fds[0].fd");
      }
      else if (fds[0].revents & 0x001) {
 int len; int hoplimit;
 struct sockaddr_in6 rcv_addr;
 struct in6_pktinfo *pkt_info = ((void *)0);
 unsigned char msg[1500];
 fprintf(stdout, "incoming ICMPv6 message?!");
 len = recv_dao(msg, &rcv_addr, &pkt_info, &hoplimit);
 if (len > 0) {
   process(msg, len, &rcv_addr, pkt_info, hoplimit);
 }
      }
    }
    else if ( rc == -1 ) {
    }
  }
}

variable_types: struct pollfd { int fd; short events; short revents; } [2]

variable_types: timeout int

variable_types: rc int

variable_types: len int

variable_types: hoplimit int

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; }

variable_types: struct in6_pktinfo { struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ipi6_addr; unsigned int ipi6_ifindex; } *

variable_types:
msg
unsigned char [1500]

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

user_defined_types: struct in6_pktinfo { struct in6_addr ipi6_addr; unsigned int ipi6_ifindex; }


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "i:s:");
if ( v3 == -1 )
break;
if ( v3 == 105 )
{
if_str = (__int64)strdup(optarg);
}
else
{
if ( v3 != 115 )
{
usage(*argv, "0.1");
exit(1);
}
ip_src_str = (__int64)strdup(optarg);
}
}
if ( !if_str )
{
if_str = (__int64)malloc(8uLL);
*(_QWORD *)if_str = 0x306E6170776F6CLL;
}
if ( !ip_src_str )
{
ip_src_str = (__int64)malloc(4uLL);
*(_DWORD *)ip_src_str = 3226170;
}
sock = open_icmpv6_socket();
if ( sock >= 0 )
{
fprintf(stdout, "RPL-Border router started on interface %s (%s)\n", (const char *)if_str, (const char *)ip_src_str);
v4 = rpl_set_root(30LL, &dag_id);
if ( v4 )
{
rpl_set_prefix(v4, &prefix, 64LL);
fwrite("created a new RPL dag\n", 1uLL, 0x16uLL, stdout);
}
main_loop();
}
fwrite("open_icmpv6_socket failed", 1uLL, 0x19uLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "i:s:");
if ( v3 == -1 )
break;
if ( v3 == 105 )
{
if_str = (__int64)strdup(optarg);
}
else
{
if ( v3 != 115 )
{
func0(*argv, "0.1");
exit(1);
}
ip_src_str = (__int64)strdup(optarg);
}
}
if ( !if_str )
{
if_str = (__int64)malloc(8uLL);
*(_QWORD *)if_str = 0x306E6170776F6CLL;
}
if ( !ip_src_str )
{
ip_src_str = (__int64)malloc(4uLL);
*(_DWORD *)ip_src_str = 3226170;
}
sock = open_icmpv6_socket();
if ( sock >= 0 )
{
fprintf(stdout, "RPL-Border router started on interface %s (%s)\n", (const char *)if_str, (const char *)ip_src_str);
v4 = rpl_set_root(30LL, &dag_id);
if ( v4 )
{
rpl_set_prefix(v4, &prefix, 64LL);
fwrite("created a new RPL dag\n", 1uLL, 0x16uLL, stdout);
}
func1();
}
fwrite("open_icmpv6_socket failed", 1uLL, 0x19uLL, stderr);
exit(1);
}

original_code:
int
main(int argc, char **argv) {
  int opt;
  rpl_dag_t *dag;
  while ((opt = getopt(argc, argv, "i:s:")) != -1) {
    switch (opt) {
    case 'i':
      if_str = strdup(optarg);
      break;
    case 's':
      ip_src_str = strdup(optarg);
      break;
    default:
      usage( argv[0], "0.1" );
      exit( 1 );
    }
  }
  if (if_str == ((void *)0)) {
    if_str = malloc(sizeof("lowpan0"));
    strcpy(if_str, "lowpan0");
  }
  if (ip_src_str == ((void *)0)) {
    ip_src_str = malloc(sizeof("::1"));
    strcpy(ip_src_str, "::1");
  }
  sock = open_icmpv6_socket();
  if (sock < 0) {
    fprintf(stderr, "open_icmpv6_socket failed");
    exit(1);
  }
  fprintf(stdout, "RPL-Border router started on interface %s (%s)\n",
   if_str, ip_src_str);
  dag = rpl_set_root(0x1e,(struct in6_addr *)dag_id);
  if(dag != ((void *)0)) {
    rpl_set_prefix(dag, &prefix, 64);
    fprintf(stdout, "created a new RPL dag\n");
  }
  main_loop();
  close(sock);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  int opt;
  struct rpl_dag *dag;
  while ((opt = getopt(argc, argv, "i:s:")) != -1) {
    switch (opt) {
    case 'i':
      if_str = strdup(optarg);
      break;
    case 's':
      ip_src_str = strdup(optarg);
      break;
    default:
      usage( argv[0], "0.1" );
      exit( 1 );
    }
  }
  if (if_str == ((void *)0)) {
    if_str = malloc(sizeof("lowpan0"));
    strcpy(if_str, "lowpan0");
  }
  if (ip_src_str == ((void *)0)) {
    ip_src_str = malloc(sizeof("::1"));
    strcpy(ip_src_str, "::1");
  }
  sock = open_icmpv6_socket();
  if (sock < 0) {
    fprintf(stderr, "open_icmpv6_socket failed");
    exit(1);
  }
  fprintf(stdout, "RPL-Border router started on interface %s (%s)\n",
   if_str, ip_src_str);
  dag = rpl_set_root(0x1e,(struct in6_addr *)dag_id);
  if(dag != ((void *)0)) {
    rpl_set_prefix(dag, &prefix, 64);
    fprintf(stdout, "created a new RPL dag\n");
  }
  main_loop();
  close(sock);
  return 0;
}

variable_types: argc int

variable_types: opt int

variable_types: struct rpl_dag { struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } dag_id; unsigned short min_rank; unsigned char version; unsigned char grounded; unsigned char preference; unsigned char used; unsigned char joined; struct rpl_parent { struct rpl_parent *next; struct rpl_dag *dag; struct rpl_metric_container { unsigned char type; unsigned char flags; unsigned char aggr; unsigned char prec; unsigned char length; union metric_object { struct rpl_metric_object_energy { unsigned char flags; unsigned char energy_est; } energy; unsigned short etx; } obj; } mc; struct in6_addr addr; unsigned short rank; unsigned char link_metric; unsigned char dtsn; unsigned char updated; } *preferred_parent; unsigned short rank; struct rpl_instance { struct rpl_metric_container mc; struct rpl_of { void (*reset)(struct rpl_dag *); void (*parent_state_callback)(struct rpl_parent *, int, int); struct rpl_parent *(*best_parent)(struct rpl_parent *, struct rpl_parent *); struct rpl_dag *(*best_dag)(struct rpl_dag *, struct rpl_dag *); unsigned short (*calculate_rank)(struct rpl_parent *, unsigned short); void (*update_metric_container)(struct rpl_instance *); unsigned short ocp; } *of; struct rpl_dag *current_dag; struct rpl_dag dag_table[2]; unsigned char instance_id; unsigned char used; unsigned char dtsn_out; unsigned char mop; unsigned char dio_intdoubl; unsigned char dio_intmin; unsigned char dio_redundancy; unsigned char default_lifetime; unsigned char dio_intcurrent; unsigned char dio_send; unsigned char dio_counter; unsigned short max_rankinc; unsigned short min_hoprankinc; unsigned short lifetime_unit; long dio_next_delay; void **dio_timer; void **dao_timer; } *instance; struct rpl_prefix { struct in6_addr prefix; unsigned int lifetime; unsigned char length; unsigned char flags; } prefix_info; } *

user_defined_types: struct rpl_dag { struct in6_addr dag_id; unsigned short min_rank; unsigned char version; unsigned char grounded; unsigned char preference; unsigned char used; unsigned char joined; struct rpl_parent *preferred_parent; unsigned short rank; struct rpl_instance *instance; struct rpl_prefix prefix_info; }

user_defined_types: struct rpl_parent { struct rpl_parent *next; struct rpl_dag *dag; struct rpl_metric_container mc; struct in6_addr addr; unsigned short rank; unsigned char link_metric; unsigned char dtsn; unsigned char updated; }

user_defined_types: struct rpl_instance { struct rpl_metric_container mc; struct rpl_of *of; struct rpl_dag *current_dag; struct rpl_dag dag_table[2]; unsigned char instance_id; unsigned char used; unsigned char dtsn_out; unsigned char mop; unsigned char dio_intdoubl; unsigned char dio_intmin; unsigned char dio_redundancy; unsigned char default_lifetime; unsigned char dio_intcurrent; unsigned char dio_send; unsigned char dio_counter; unsigned short max_rankinc; unsigned short min_hoprankinc; unsigned short lifetime_unit; long dio_next_delay; void **dio_timer; void **dao_timer; }

user_defined_types: struct rpl_metric_container { unsigned char type; unsigned char flags; unsigned char aggr; unsigned char prec; unsigned char length; union metric_object obj; }

user_defined_types: union metric_object { struct rpl_metric_object_energy energy; unsigned short etx; }


decompiled_code:
__int64 __fastcall recv_dao(__int64 a1, void *a2, _QWORD *a3, unsigned int *a4)
{
int v7; // edi
int v8; // er12
unsigned int v9; // er13
_QWORD *v10; // rsi
int *v12; // rax
char *v13; // rax
int v14; // edi
char *v15; // rax
int v16; // edi
char *v17; // rax
unsigned int v18; // eax
int v19; // eax
fd_set v20; // [rsp+0h] [rbp-F8h] BYREF
__int64 v21[2]; // [rsp+80h] [rbp-78h] BYREF
struct msghdr message; // [rsp+90h] [rbp-68h] BYREF

if ( chdr_1 || (chdrlen_0 = 64, (chdr_1 = (__int64)malloc(0x40uLL)) != 0) )
{
memset(&v20, 0, sizeof(v20));
v7 = sock;
v20.fds_bits[sock / 64] |= 1LL << (sock % 64);
if ( select(v7 + 1, &v20, 0LL, 0LL, 0LL) < 0 )
{
v14 = *_errno_location();
v9 = -1;
if ( v14 != 4 )
{
v15 = strerror(v14);
fprintf(stderr, "select: %s", v15);
}
}
else
{
v21[1] = 1500LL;
v21[0] = a1;
*(_QWORD *)&message.msg_namelen = 28LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_name = a2;
message.msg_iov = (iovec *)v21;
message.msg_iovlen = 1LL;
message.msg_control = (void *)chdr_1;
message.msg_controllen = (unsigned int)chdrlen_0;
v8 = recvmsg(sock, &message, 0);
v9 = v8;
if ( v8 < 0 )
{
v16 = *_errno_location();
if ( v16 != 4 )
{
v17 = strerror(v16);
fprintf(stderr, "recvmsg: %s", v17);
}
}
else
{
*a4 = 255;
if ( message.msg_controllen > 0xF )
{
v10 = message.msg_control;
if ( message.msg_control )
{
do
{
if ( *((_DWORD *)v10 + 2) == 41 )
{
v19 = *((_DWORD *)v10 + 3);
switch ( v19 )
{
case 50:
if ( *v10 != 36LL || !*((_DWORD *)v10 + 8) )
{
fprintf(
stderr,
"received a bogus IPV6_PKTINFO from the kernel! len=%d, index=%d",
*v10,
*((unsigned int *)v10 + 8));
return (unsigned int)-1;
}
*a3 = v10 + 2;
break;
case 52:
if ( *v10 != 20LL || (v18 = *((_DWORD *)v10 + 4), v18 > 0xFF) )
{
fprintf(
stderr,
"received a bogus IPV6_HOPLIMIT from the kernel! len=%d, data=%d",
*v10,
*((unsigned int *)v10 + 4));
return (unsigned int)-1;
}
*a4 = v18;
break;
case 2:
if ( *v10 != 36LL || !*((_DWORD *)v10 + 8) )
{
fprintf(
stderr,
"received a bogus RPL_CODE_DAO ! len=%d, index=%d",
*v10,
*((unsigned int *)v10 + 8));
return (unsigned int)-1;
}
*a3 = v10 + 2;
break;
}
}
v10 = _cmsg_nxthdr(&message, (struct cmsghdr *)v10);
}
while ( v10 );
}
}
fprintf(stdout, "recvmsg len=%d", (unsigned int)v8);
}
}
}
else
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "recv_rs_ra: malloc: %s", v13);
v9 = -1;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, void *a2, _QWORD *a3, unsigned int *a4)
{
int v7; // edi
int v8; // er12
unsigned int v9; // er13
_QWORD *v10; // rsi
int *v12; // rax
char *v13; // rax
int v14; // edi
char *v15; // rax
int v16; // edi
char *v17; // rax
unsigned int v18; // eax
int v19; // eax
fd_set v20; // [rsp+0h] [rbp-F8h] BYREF
__int64 v21[2]; // [rsp+80h] [rbp-78h] BYREF
struct msghdr message; // [rsp+90h] [rbp-68h] BYREF

if ( chdr_1 || (chdrlen_0 = 64, (chdr_1 = (__int64)malloc(0x40uLL)) != 0) )
{
memset(&v20, 0, sizeof(v20));
v7 = sock;
v20.fds_bits[sock / 64] |= 1LL << (sock % 64);
if ( select(v7 + 1, &v20, 0LL, 0LL, 0LL) < 0 )
{
v14 = *_errno_location();
v9 = -1;
if ( v14 != 4 )
{
v15 = strerror(v14);
fprintf(stderr, "select: %s", v15);
}
}
else
{
v21[1] = 1500LL;
v21[0] = a1;
*(_QWORD *)&message.msg_namelen = 28LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_name = a2;
message.msg_iov = (iovec *)v21;
message.msg_iovlen = 1LL;
message.msg_control = (void *)chdr_1;
message.msg_controllen = (unsigned int)chdrlen_0;
v8 = recvmsg(sock, &message, 0);
v9 = v8;
if ( v8 < 0 )
{
v16 = *_errno_location();
if ( v16 != 4 )
{
v17 = strerror(v16);
fprintf(stderr, "recvmsg: %s", v17);
}
}
else
{
*a4 = 255;
if ( message.msg_controllen > 0xF )
{
v10 = message.msg_control;
if ( message.msg_control )
{
do
{
if ( *((_DWORD *)v10 + 2) == 41 )
{
v19 = *((_DWORD *)v10 + 3);
switch ( v19 )
{
case 50:
if ( *v10 != 36LL || !*((_DWORD *)v10 + 8) )
{
fprintf(
stderr,
"received a bogus IPV6_PKTINFO from the kernel! len=%d, index=%d",
*v10,
*((unsigned int *)v10 + 8));
return (unsigned int)-1;
}
*a3 = v10 + 2;
break;
case 52:
if ( *v10 != 20LL || (v18 = *((_DWORD *)v10 + 4), v18 > 0xFF) )
{
fprintf(
stderr,
"received a bogus IPV6_HOPLIMIT from the kernel! len=%d, data=%d",
*v10,
*((unsigned int *)v10 + 4));
return (unsigned int)-1;
}
*a4 = v18;
break;
case 2:
if ( *v10 != 36LL || !*((_DWORD *)v10 + 8) )
{
fprintf(
stderr,
"received a bogus RPL_CODE_DAO ! len=%d, index=%d",
*v10,
*((unsigned int *)v10 + 8));
return (unsigned int)-1;
}
*a3 = v10 + 2;
break;
}
}
v10 = _cmsg_nxthdr(&message, (struct cmsghdr *)v10);
}
while ( v10 );
}
}
fprintf(stdout, "recvmsg len=%d", (unsigned int)v8);
}
}
}
else
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "recv_rs_ra: malloc: %s", v13);
v9 = -1;
}
return v9;
}

original_code:
int
recv_dao(unsigned char *msg, struct sockaddr_in6 *addr,
  struct in6_pktinfo **pkt_info, int *hoplimit)
{
  struct msghdr mhdr;
  struct cmsghdr *cmsg;
  struct iovec iov;
  static unsigned char *chdr = ((void *)0);
  static unsigned int chdrlen = 0;
  int len;
  fd_set rfds;
  if( ! chdr )
    {
      chdrlen = ((((sizeof(struct in6_pktinfo)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))) +
 ((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
      if ((chdr = malloc(chdrlen)) == ((void *)0)) {
 fprintf(stderr, "recv_rs_ra: malloc: %s", strerror((*__errno_location ())));
 return -1;
      }
    }
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  if( select( sock+1, &rfds, ((void *)0), ((void *)0), ((void *)0) ) < 0 )
    {
      if ((*__errno_location ()) != 4)
 fprintf(stderr, "select: %s", strerror((*__errno_location ())));
      return -1;
    }
  iov.iov_len = 1500;
  iov.iov_base = (caddr_t) msg;
  memset(&mhdr, 0, sizeof(mhdr));
  mhdr.msg_name = (caddr_t)addr;
  mhdr.msg_namelen = sizeof(*addr);
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = (void *)chdr;
  mhdr.msg_controllen = chdrlen;
  len = recvmsg(sock, &mhdr, 0);
  if (len < 0)
    {
      if ((*__errno_location ()) != 4)
 fprintf(stderr, "recvmsg: %s", strerror((*__errno_location ())));
      return len;
    }
  *hoplimit = 255;
  for (cmsg = ((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0); cmsg != ((void *)0); cmsg = __cmsg_nxthdr (&mhdr, cmsg))
    {
      if (cmsg->cmsg_level != IPPROTO_IPV6)
 continue;
      switch(cmsg->cmsg_type)
 {
 case 52:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int)))) &&
       (*(int *)((cmsg)->__cmsg_data) >= 0) &&
       (*(int *)((cmsg)->__cmsg_data) < 256))
     {
       *hoplimit = *(int *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus IPV6_HOPLIMIT from the kernel! len=%d, data=%d",
     cmsg->cmsg_len, *(int *)((cmsg)->__cmsg_data));
       return (-1);
     }
   break;
 case 50:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(struct in6_pktinfo)))) &&
       ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex)
     {
       *pkt_info = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus IPV6_PKTINFO from the kernel! len=%d, index=%d",
     cmsg->cmsg_len, ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex);
       return (-1);
     }
   break;
  case 0x02:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(struct in6_pktinfo)))) &&
       ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex)
     {
       *pkt_info = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus RPL_CODE_DAO ! len=%d, index=%d",
     cmsg->cmsg_len, ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex);
       return (-1);
     }
   break;
 }
    }
  fprintf(stdout, "recvmsg len=%d", len);
  return len;
}

canonical_original_code:
int recv_dao(unsigned char *msg, struct sockaddr_in6 *addr, struct in6_pktinfo **pkt_info, int *hoplimit)
{
  struct msghdr mhdr;
  struct cmsghdr *cmsg;
  struct iovec iov;
  static unsigned char *chdr = ((void *)0);
  static unsigned int chdrlen = 0;
  int len;
  struct fd_set rfds;
  if( ! chdr )
    {
      chdrlen = ((((sizeof(struct in6_pktinfo)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1))) +
 ((((sizeof(int)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)));
      if ((chdr = malloc(chdrlen)) == ((void *)0)) {
 fprintf(stderr, "recv_rs_ra: malloc: %s", strerror((*__errno_location ())));
 return -1;
      }
    }
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  if( select( sock+1, &rfds, ((void *)0), ((void *)0), ((void *)0) ) < 0 )
    {
      if ((*__errno_location ()) != 4)
 fprintf(stderr, "select: %s", strerror((*__errno_location ())));
      return -1;
    }
  iov.iov_len = 1500;
  iov.iov_base = (char *) msg;
  memset(&mhdr, 0, sizeof(mhdr));
  mhdr.msg_name = (char *)addr;
  mhdr.msg_namelen = sizeof(*addr);
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = (void *)chdr;
  mhdr.msg_controllen = chdrlen;
  len = recvmsg(sock, &mhdr, 0);
  if (len < 0)
    {
      if ((*__errno_location ()) != 4)
 fprintf(stderr, "recvmsg: %s", strerror((*__errno_location ())));
      return len;
    }
  *hoplimit = 255;
  for (cmsg = ((unsigned long) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0); cmsg != ((void *)0); cmsg = __cmsg_nxthdr (&mhdr, cmsg))
    {
      if (cmsg->cmsg_level != IPPROTO_IPV6)
 continue;
      switch(cmsg->cmsg_type)
 {
 case 52:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int)))) &&
       (*(int *)((cmsg)->__cmsg_data) >= 0) &&
       (*(int *)((cmsg)->__cmsg_data) < 256))
     {
       *hoplimit = *(int *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus IPV6_HOPLIMIT from the kernel! len=%d, data=%d",
     cmsg->cmsg_len, *(int *)((cmsg)->__cmsg_data));
       return (-1);
     }
   break;
 case 50:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(struct in6_pktinfo)))) &&
       ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex)
     {
       *pkt_info = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus IPV6_PKTINFO from the kernel! len=%d, index=%d",
     cmsg->cmsg_len, ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex);
       return (-1);
     }
   break;
  case 0x02:
   if ((cmsg->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(struct in6_pktinfo)))) &&
       ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex)
     {
       *pkt_info = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
     }
   else
     {
       fprintf(stderr, "received a bogus RPL_CODE_DAO ! len=%d, index=%d",
     cmsg->cmsg_len, ((struct in6_pktinfo *)((cmsg)->__cmsg_data))->ipi6_ifindex);
       return (-1);
     }
   break;
 }
    }
  fprintf(stdout, "recvmsg len=%d", len);
  return len;
}

variable_types:
msg
unsigned char *

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } *

variable_types: struct in6_pktinfo { struct in6_addr ipi6_addr; unsigned int ipi6_ifindex; } **

variable_types:
hoplimit
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 cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types:
chdr
unsigned char *

variable_types:
chdrlen
unsigned int

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

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


process

decompiled_code:
int __fastcall process(unsigned __int8 *a1, unsigned int a2, __int64 a3, __int64 a4, unsigned int a5)
{
__int64 v7; // r12
unsigned __int8 v8; // dl
int result; // eax
char v10[88]; // [rsp+0h] [rbp-58h] BYREF

v7 = a3 + 8;
print_addr(a3 + 8, v10);
if ( !a4 )
return fprintf(stderr, "received packet with no pkt_info from %s!\n", v10);
if ( a2 <= 7 )
return fprintf(stderr, "received icmpv6 packet with invalid length (%d) from %s\n", a2, v10);
if ( *a1 != 0x9B )
return fprintf(stderr, "icmpv6 filter failed 0x%x\n", *a1);
v8 = a1[1];
if ( v8 )
return fprintf(stderr, "received icmpv6 RS/RA packet with invalid code (%d) from %s\n", v8, v10);
fprintf(stderr, "if_index %u", *(unsigned int *)(a4 + 16));
if ( a5 == 255 )
{
if ( *a1 == 0x9B )
result = fprintf(stdout, "received DAO from %s\n", v10);
else
result = fprintf(stderr, "unknown ICMPv6 type %i\n", *a1);
}
else
{
print_addr(v7, v10);
result = fprintf(stderr, "received RS or RA with invalid hoplimit %d from %s\n", a5, v10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int8 *a1, unsigned int a2, __int64 a3, __int64 a4, unsigned int a5)
{
__int64 v7; // r12
unsigned __int8 v8; // dl
int result; // eax
char v10[88]; // [rsp+0h] [rbp-58h] BYREF

v7 = a3 + 8;
print_addr(a3 + 8, v10);
if ( !a4 )
return fprintf(stderr, "received packet with no pkt_info from %s!\n", v10);
if ( a2 <= 7 )
return fprintf(stderr, "received icmpv6 packet with invalid length (%d) from %s\n", a2, v10);
if ( *a1 != 0x9B )
return fprintf(stderr, "icmpv6 filter failed 0x%x\n", *a1);
v8 = a1[1];
if ( v8 )
return fprintf(stderr, "received icmpv6 RS/RA packet with invalid code (%d) from %s\n", v8, v10);
fprintf(stderr, "if_index %u", *(unsigned int *)(a4 + 16));
if ( a5 == 255 )
{
if ( *a1 == 0x9B )
result = fprintf(stdout, "received DAO from %s\n", v10);
else
result = fprintf(stderr, "unknown ICMPv6 type %i\n", *a1);
}
else
{
print_addr(v7, v10);
result = fprintf(stderr, "received RS or RA with invalid hoplimit %d from %s\n", a5, v10);
}
return result;
}

original_code:
void
process(unsigned char *msg, int len,
 struct sockaddr_in6 *addr, struct in6_pktinfo *pkt_info, int hoplimit)
{
 struct icmp6_hdr *icmph;
 char addr_str[46];
 print_addr(&addr->sin6_addr, addr_str);
 if ( ! pkt_info )
 {
  fprintf(stderr, "received packet with no pkt_info from %s!\n", addr_str );
  return;
 }
 if (len < sizeof(struct icmp6_hdr))
 {
  fprintf(stderr, "received icmpv6 packet with invalid length (%d) from %s\n",
   len, addr_str);
  return;
 }
 icmph = (struct icmp6_hdr *) msg;
 if (icmph->icmp6_type != 155)
 {
   fprintf(stderr, "icmpv6 filter failed 0x%x\n", icmph->icmp6_type);
  return;
 }
 if (icmph->icmp6_type == 134)
 {
  if (len < sizeof(struct nd_router_advert)) {
   fprintf(stderr, "received icmpv6 RA packet with invalid length (%d) from %s\n",
    len, addr_str);
   return;
  }
  if (!(__extension__ ({ const struct in6_addr *__a = (const struct in6_addr *) (&addr->sin6_addr); (__a->__in6_u.__u6_addr32[0] & htonl (0xffc00000)) == htonl (0xfe800000); }))) {
   fprintf(stderr, "received icmpv6 RA packet with non-linklocal source address from %s\n", addr_str);
   return;
  }
 }
 if (icmph->icmp6_type == 133)
 {
  if (len < sizeof(struct nd_router_solicit)) {
   fprintf(stderr, "received icmpv6 RS packet with invalid length (%d) from %s\n",
    len, addr_str);
   return;
  }
 }
 if (icmph->icmp6_code != 0)
 {
  fprintf(stderr, "received icmpv6 RS/RA packet with invalid code (%d) from %s\n",
   icmph->icmp6_code, addr_str);
  return;
 }
 fprintf(stderr, "if_index %u", pkt_info->ipi6_ifindex);
 if (hoplimit != 255)
 {
  print_addr(&addr->sin6_addr, addr_str);
  fprintf(stderr, "received RS or RA with invalid hoplimit %d from %s\n",
   hoplimit, addr_str);
  return;
 }
 if (icmph->icmp6_type == 155)
 {
  fprintf(stdout, "received DAO from %s\n", addr_str);
  process_dao(msg, len, addr);
 }
 else
 {
  fprintf(stderr, "unknown ICMPv6 type %i\n", icmph->icmp6_type);
 }
}

canonical_original_code:
void process(unsigned char *msg, int len, struct sockaddr_in6 *addr, struct in6_pktinfo *pkt_info, int hoplimit)
{
 struct icmp6_hdr *icmph;
 char addr_str[46];
 print_addr(&addr->sin6_addr, addr_str);
 if ( ! pkt_info )
 {
  fprintf(stderr, "received packet with no pkt_info from %s!\n", addr_str );
  return;
 }
 if (len < sizeof(struct icmp6_hdr))
 {
  fprintf(stderr, "received icmpv6 packet with invalid length (%d) from %s\n",
   len, addr_str);
  return;
 }
 icmph = (struct icmp6_hdr *) msg;
 if (icmph->icmp6_type != 155)
 {
   fprintf(stderr, "icmpv6 filter failed 0x%x\n", icmph->icmp6_type);
  return;
 }
 if (icmph->icmp6_type == 134)
 {
  if (len < sizeof(struct nd_router_advert)) {
   fprintf(stderr, "received icmpv6 RA packet with invalid length (%d) from %s\n",
    len, addr_str);
   return;
  }
  if (!(__extension__ ({ struct in6_addr *__a = (const struct in6_addr *) (&addr->sin6_addr); (__a->__in6_u.__u6_addr32[0] & htonl (0xffc00000)) == htonl (0xfe800000); }))) {
   fprintf(stderr, "received icmpv6 RA packet with non-linklocal source address from %s\n", addr_str);
   return;
  }
 }
 if (icmph->icmp6_type == 133)
 {
  if (len < sizeof(struct nd_router_solicit)) {
   fprintf(stderr, "received icmpv6 RS packet with invalid length (%d) from %s\n",
    len, addr_str);
   return;
  }
 }
 if (icmph->icmp6_code != 0)
 {
  fprintf(stderr, "received icmpv6 RS/RA packet with invalid code (%d) from %s\n",
   icmph->icmp6_code, addr_str);
  return;
 }
 fprintf(stderr, "if_index %u", pkt_info->ipi6_ifindex);
 if (hoplimit != 255)
 {
  print_addr(&addr->sin6_addr, addr_str);
  fprintf(stderr, "received RS or RA with invalid hoplimit %d from %s\n",
   hoplimit, addr_str);
  return;
 }
 if (icmph->icmp6_type == 155)
 {
  fprintf(stdout, "received DAO from %s\n", addr_str);
  process_dao(msg, len, addr);
 }
 else
 {
  fprintf(stderr, "unknown ICMPv6 type %i\n", icmph->icmp6_type);
 }
}

variable_types: len int

variable_types: struct in6_pktinfo { struct in6_addr ipi6_addr; unsigned int ipi6_ifindex; } *

variable_types: struct icmp6_hdr { unsigned char icmp6_type; unsigned char icmp6_code; unsigned short icmp6_cksum; union { unsigned int icmp6_un_data32[1]; unsigned short icmp6_un_data16[2]; unsigned char icmp6_un_data8[4]; } icmp6_dataun; } *

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

user_defined_types: struct nd_router_advert { struct icmp6_hdr nd_ra_hdr; unsigned int nd_ra_reachable; unsigned int nd_ra_retransmit; }

user_defined_types: struct nd_router_solicit { struct icmp6_hdr nd_rs_hdr; }


decompiled_code:
int __fastcall new_dio_interval(__int64 a1)
{
__int64 v1; // rsi
unsigned int v2; // ebp
int v3; // eax
__int64 v4; // rcx
int result; // eax
int *v6; // rax
unsigned int v7; // eax
struct itimerspec v8; // [rsp+0h] [rbp-38h] BYREF

v1 = 1000 * (unsigned int)(1LL << *(_BYTE *)(a1 + 176));
*(_QWORD *)(a1 + 192) = v1;
v2 = (unsigned int)v1 >> 1;
v3 = rand();
v4 = v2 + v2 * v3 / 0x7FFFFFFF;
v8.it_value.tv_sec = (v2 + v2 * v3 / 0x7FFFFFFF) / 0xF4240;
v8.it_value.tv_nsec = (unsigned int)v4 % 0xF4240;
v8.it_interval.tv_sec = 0LL;
v8.it_interval.tv_nsec = 0LL;
*(_QWORD *)(a1 + 192) -= v4;
*(_BYTE *)(a1 + 177) = 1;
*(_BYTE *)(a1 + 178) = 0;
puts("RPL: Scheduling DIO timer");
result = timer_settime(**(timer_t **)(a1 + 200), 0, &v8, 0LL);
if ( result )
{
v6 = _errno_location();
v7 = (unsigned int)strerror(*v6);
printf("RPL: Scheduling DIO timer failed '%s'.\n", (const char *)v7);
result = printf("RPL: FAIL INFO: %i %i %i\n", a1 + 200, v8.it_value.tv_sec, v8.it_value.tv_nsec);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
__int64 v1; // rsi
unsigned int v2; // ebp
int v3; // eax
__int64 v4; // rcx
int result; // eax
int *v6; // rax
unsigned int v7; // eax
struct itimerspec v8; // [rsp+0h] [rbp-38h] BYREF

v1 = 1000 * (unsigned int)(1LL << *(_BYTE *)(a1 + 176));
*(_QWORD *)(a1 + 192) = v1;
v2 = (unsigned int)v1 >> 1;
v3 = rand();
v4 = v2 + v2 * v3 / 0x7FFFFFFF;
v8.it_value.tv_sec = (v2 + v2 * v3 / 0x7FFFFFFF) / 0xF4240;
v8.it_value.tv_nsec = (unsigned int)v4 % 0xF4240;
v8.it_interval.tv_sec = 0LL;
v8.it_interval.tv_nsec = 0LL;
*(_QWORD *)(a1 + 192) -= v4;
*(_BYTE *)(a1 + 177) = 1;
*(_BYTE *)(a1 + 178) = 0;
puts("RPL: Scheduling DIO timer");
result = timer_settime(**(timer_t **)(a1 + 200), 0, &v8, 0LL);
if ( result )
{
v6 = _errno_location();
v7 = (unsigned int)strerror(*v6);
printf("RPL: Scheduling DIO timer failed '%s'.\n", (const char *)v7);
result = printf("RPL: FAIL INFO: %i %i %i\n", a1 + 200, v8.it_value.tv_sec, v8.it_value.tv_nsec);
}
return result;
}

original_code:
static void
new_dio_interval(rpl_instance_t *instance)
{
  int ret;
  uint32_t time;
  struct itimerspec its;
  time = 1UL << instance->dio_intcurrent;
  time *= 1000;
  instance->dio_next_delay = time;
  time = time/2 + (time / 2 * (uint32_t)rand()) / 2147483647;
  its.it_value.tv_sec = time / 1000000;
  its.it_value.tv_nsec = time % 1000000;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  instance->dio_next_delay -= time;
  instance->dio_send = 1;
  instance->dio_counter = 0;
  printf("RPL: Scheduling DIO timer\n");
  ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
  if (ret != 0) {
    printf("RPL: Scheduling DIO timer failed '%s'.\n", strerror((*__errno_location ())));
    printf("RPL: FAIL INFO: %i %i %i\n", &instance->dio_timer, its.it_value.tv_sec, its.it_value.tv_nsec);
  }
}

canonical_original_code:
static void new_dio_interval(struct rpl_instance *instance)
{
  int ret;
  unsigned int time;
  struct itimerspec its;
  time = 1UL << instance->dio_intcurrent;
  time *= 1000;
  instance->dio_next_delay = time;
  time = time/2 + (time / 2 * (unsigned int)rand()) / 2147483647;
  its.it_value.tv_sec = time / 1000000;
  its.it_value.tv_nsec = time % 1000000;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  instance->dio_next_delay -= time;
  instance->dio_send = 1;
  instance->dio_counter = 0;
  printf("RPL: Scheduling DIO timer\n");
  ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
  if (ret != 0) {
    printf("RPL: Scheduling DIO timer failed '%s'.\n", strerror((*__errno_location ())));
    printf("RPL: FAIL INFO: %i %i %i\n", &instance->dio_timer, its.it_value.tv_sec, its.it_value.tv_nsec);
  }
}

variable_types: struct rpl_instance { struct rpl_metric_container { unsigned char type; unsigned char flags; unsigned char aggr; unsigned char prec; unsigned char length; union metric_object { struct rpl_metric_object_energy { unsigned char flags; unsigned char energy_est; } energy; unsigned short etx; } obj; } mc; struct rpl_of { void (*reset)(struct rpl_dag { struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } dag_id; unsigned short min_rank; unsigned char version; unsigned char grounded; unsigned char preference; unsigned char used; unsigned char joined; struct rpl_parent { struct rpl_parent *next; struct rpl_dag *dag; struct rpl_metric_container mc; struct in6_addr addr; unsigned short rank; unsigned char link_metric; unsigned char dtsn; unsigned char updated; } *preferred_parent; unsigned short rank; struct rpl_instance *instance; struct rpl_prefix { struct in6_addr prefix; unsigned int lifetime; unsigned char length; unsigned char flags; } prefix_info; } *); void (*parent_state_callback)(struct rpl_parent *, int, int); struct rpl_parent *(*best_parent)(struct rpl_parent *, struct rpl_parent *); struct rpl_dag *(*best_dag)(struct rpl_dag *, struct rpl_dag *); unsigned short (*calculate_rank)(struct rpl_parent *, unsigned short); void (*update_metric_container)(struct rpl_instance *); unsigned short ocp; } *of; struct rpl_dag *current_dag; struct rpl_dag dag_table[2]; unsigned char instance_id; unsigned char used; unsigned char dtsn_out; unsigned char mop; unsigned char dio_intdoubl; unsigned char dio_intmin; unsigned char dio_redundancy; unsigned char default_lifetime; unsigned char dio_intcurrent; unsigned char dio_send; unsigned char dio_counter; unsigned short max_rankinc; unsigned short min_hoprankinc; unsigned short lifetime_unit; long dio_next_delay; void **dio_timer; void **dao_timer; } *

variable_types: ret int

variable_types:
time
unsigned int

variable_types: struct itimerspec { struct timespec { long tv_sec; long tv_nsec; } it_interval; struct timespec it_value; }

user_defined_types: struct itimerspec { struct timespec it_interval; struct timespec it_value; }

user_defined_types: struct rpl_of { void (*reset)(struct rpl_dag *); void (*parent_state_callback)(struct rpl_parent *, int, int); struct rpl_parent *(*best_parent)(struct rpl_parent *, struct rpl_parent *); struct rpl_dag *(*best_dag)(struct rpl_dag *, struct rpl_dag *); unsigned short (*calculate_rank)(struct rpl_parent *, unsigned short); void (*update_metric_container)(struct rpl_instance *); unsigned short ocp; }


decompiled_code:
int __fastcall handle_dio_timer(__int64 a1)
{
unsigned __int8 v1; // si
unsigned __int8 v2; // dl
__int64 v3; // rcx
int result; // eax
unsigned __int8 v5; // si
unsigned __int8 v6; // si
struct itimerspec v7; // [rsp+0h] [rbp-28h] BYREF

v7.it_value.tv_sec = 1LL;
v7.it_value.tv_nsec = 0LL;
v7.it_interval.tv_sec = 0LL;
v7.it_interval.tv_nsec = 0LL;
puts("RPL: DIO Timer triggered");
if ( !dio_send_ok )
dio_send_ok = 1;
if ( *(_BYTE *)(a1 + 177) )
{
v1 = *(_BYTE *)(a1 + 178);
v2 = *(_BYTE *)(a1 + 174);
if ( v1 >= v2 )
printf("RPL: Supressing DIO transmission (%d >= %d)\n", v1, v2);
else
dio_output(a1, 0LL);
*(_BYTE *)(a1 + 177) = 0;
printf("RPL: Scheduling DIO timer %lu ticks in future (sent)\n", *(_QWORD *)(a1 + 192));
v3 = *(_QWORD *)(a1 + 192);
v7.it_value.tv_sec = v3 / 1000000;
v7.it_value.tv_nsec = v3 % 1000000;
v7.it_interval.tv_sec = 0LL;
v7.it_interval.tv_nsec = 0LL;
result = timer_settime(**(timer_t **)(a1 + 200), 0, &v7, 0LL);
if ( result )
result = puts("RPL: Scheduling DIO timer failed.");
}
else
{
v5 = *(_BYTE *)(a1 + 176);
if ( v5 < *(unsigned __int8 *)(a1 + 172) + *(unsigned __int8 *)(a1 + 173) )
{
v6 = v5 + 1;
*(_BYTE *)(a1 + 176) = v6;
printf("RPL: DIO Timer interval doubled %d\n", v6);
}
result = new_dio_interval(a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
unsigned __int8 v1; // si
unsigned __int8 v2; // dl
__int64 v3; // rcx
int result; // eax
unsigned __int8 v5; // si
unsigned __int8 v6; // si
struct itimerspec v7; // [rsp+0h] [rbp-28h] BYREF

v7.it_value.tv_sec = 1LL;
v7.it_value.tv_nsec = 0LL;
v7.it_interval.tv_sec = 0LL;
v7.it_interval.tv_nsec = 0LL;
puts("RPL: DIO Timer triggered");
if ( !dio_send_ok )
dio_send_ok = 1;
if ( *(_BYTE *)(a1 + 177) )
{
v1 = *(_BYTE *)(a1 + 178);
v2 = *(_BYTE *)(a1 + 174);
if ( v1 >= v2 )
printf("RPL: Supressing DIO transmission (%d >= %d)\n", v1, v2);
else
dio_output(a1, 0LL);
*(_BYTE *)(a1 + 177) = 0;
printf("RPL: Scheduling DIO timer %lu ticks in future (sent)\n", *(_QWORD *)(a1 + 192));
v3 = *(_QWORD *)(a1 + 192);
v7.it_value.tv_sec = v3 / 1000000;
v7.it_value.tv_nsec = v3 % 1000000;
v7.it_interval.tv_sec = 0LL;
v7.it_interval.tv_nsec = 0LL;
result = timer_settime(**(timer_t **)(a1 + 200), 0, &v7, 0LL);
if ( result )
result = puts("RPL: Scheduling DIO timer failed.");
}
else
{
v5 = *(_BYTE *)(a1 + 176);
if ( v5 < *(unsigned __int8 *)(a1 + 172) + *(unsigned __int8 *)(a1 + 173) )
{
v6 = v5 + 1;
*(_BYTE *)(a1 + 176) = v6;
printf("RPL: DIO Timer interval doubled %d\n", v6);
}
result = func0(a1);
}
return result;
}

original_code:
static void
handle_dio_timer(void *ptr)
{
  rpl_instance_t *instance;
  struct itimerspec its;
  int ret;
  its.it_value.tv_sec = 1;
  its.it_value.tv_nsec = 0;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  instance = (rpl_instance_t *)ptr;
  printf("RPL: DIO Timer triggered\n");
  if(!dio_send_ok) {
    if(1 ) {
      dio_send_ok = 1;
    } else {
      printf("RPL: Postponing DIO transmission since link local address is not ok\n");
      ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
      if (ret != 0) {
 printf("RPL: Scheduling DIO timer failed.\n");
      }
      return;
    }
  }
  if(instance->dio_send) {
    if(instance->dio_counter < instance->dio_redundancy) {
      dio_output(instance, ((void *)0));
    } else {
      printf("RPL: Supressing DIO transmission (%d >= %d)\n", instance->dio_counter, instance->dio_redundancy);
    }
    instance->dio_send = 0;
    printf("RPL: Scheduling DIO timer %lu ticks in future (sent)\n", instance->dio_next_delay);
    its.it_value.tv_sec = instance->dio_next_delay / 1000000;
    its.it_value.tv_nsec = instance->dio_next_delay % 1000000;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
    if (ret != 0) {
      printf("RPL: Scheduling DIO timer failed.\n");
    }
  } else {
    if(instance->dio_intcurrent < instance->dio_intmin + instance->dio_intdoubl) {
      instance->dio_intcurrent++;
      printf("RPL: DIO Timer interval doubled %d\n", instance->dio_intcurrent);
    }
    new_dio_interval(instance);
  }
}

canonical_original_code:
static void handle_dio_timer(void *ptr)
{
  struct rpl_instance *instance;
  struct itimerspec its;
  int ret;
  its.it_value.tv_sec = 1;
  its.it_value.tv_nsec = 0;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  instance = (struct rpl_instance *)ptr;
  printf("RPL: DIO Timer triggered\n");
  if(!dio_send_ok) {
    if(1 ) {
      dio_send_ok = 1;
    } else {
      printf("RPL: Postponing DIO transmission since link local address is not ok\n");
      ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
      if (ret != 0) {
 printf("RPL: Scheduling DIO timer failed.\n");
      }
      return;
    }
  }
  if(instance->dio_send) {
    if(instance->dio_counter < instance->dio_redundancy) {
      dio_output(instance, ((void *)0));
    } else {
      printf("RPL: Supressing DIO transmission (%d >= %d)\n", instance->dio_counter, instance->dio_redundancy);
    }
    instance->dio_send = 0;
    printf("RPL: Scheduling DIO timer %lu ticks in future (sent)\n", instance->dio_next_delay);
    its.it_value.tv_sec = instance->dio_next_delay / 1000000;
    its.it_value.tv_nsec = instance->dio_next_delay % 1000000;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    ret = timer_settime(*(instance->dio_timer), 0, &its, ((void *)0));
    if (ret != 0) {
      printf("RPL: Scheduling DIO timer failed.\n");
    }
  } else {
    if(instance->dio_intcurrent < instance->dio_intmin + instance->dio_intdoubl) {
      instance->dio_intcurrent++;
      printf("RPL: DIO Timer interval doubled %d\n", instance->dio_intcurrent);
    }
    new_dio_interval(instance);
  }
}

variable_types:
ptr
void *

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

user_defined_types: struct rpl_metric_object_energy { unsigned char flags; unsigned char energy_est; }

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

user_defined_types: struct rpl_prefix { struct in6_addr prefix; unsigned int lifetime; unsigned char length; unsigned char flags; }


timerHandler

decompiled_code:
int __fastcall timerHandler(unsigned int a1, __int64 a2)
{
const void *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbp

v2 = *(const void **)(a2 + 24);
puts("timerHandler");
printf("timerValue %p %i %i %i\n", v2, a1, 10LL, 12LL);
v3 = rpl_get_instance(30LL);
if ( v3 )
{
v4 = v3;
if ( a1 == 10 )
{
puts("DIO timer");
LODWORD(v3) = handle_dio_timer(v4);
}
}
else
{
LODWORD(v3) = printf("Which RPL instance?");
}
return v3;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, __int64 a2)
{
const void *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbp

v2 = *(const void **)(a2 + 24);
puts("timerHandler");
printf("timerValue %p %i %i %i\n", v2, a1, 10LL, 12LL);
v3 = rpl_get_instance(30LL);
if ( v3 )
{
v4 = v3;
if ( a1 == 10 )
{
puts("DIO timer");
LODWORD(v3) = func1(v4);
}
}
else
{
LODWORD(v3) = printf("Which RPL instance?");
}
return v3;
}

original_code:
static void
timerHandler(int sig, siginfo_t *si, void *uc)
{
  rpl_instance_t *instance;
  timer_t *tidp;
  uint8_t instance_id = 30;
  tidp = si->_sifields._rt.si_sigval.sival_ptr;
  printf("timerHandler\n");
  printf("timerValue %p %i %i %i\n", tidp, sig, 10, 12);
  instance = rpl_get_instance(instance_id);
  if(instance == ((void *)0)) {
    printf("Which RPL instance?");
    return;
  }
  if (sig == 10) {
    printf("DIO timer\n");
    handle_dio_timer((void*)instance);
  }
}

canonical_original_code:
static void timerHandler(int sig, struct siginfo_t *si, void *uc)
{
  struct rpl_instance *instance;
  void **tidp;
  unsigned char instance_id = 30;
  tidp = si->_sifields._rt.si_sigval.sival_ptr;
  printf("timerHandler\n");
  printf("timerValue %p %i %i %i\n", tidp, sig, 10, 12);
  instance = rpl_get_instance(instance_id);
  if(instance == ((void *)0)) {
    printf("Which RPL instance?");
    return;
  }
  if (sig == 10) {
    printf("DIO timer\n");
    handle_dio_timer((void *)instance);
  }
}

variable_types: sig int

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

variable_types:
uc
void *

variable_types:
tidp
void **

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


decompiled_code:
int __fastcall rpl_init_timers(__int64 a1)
{
int *v2; // rax
unsigned int v3; // eax
struct sigevent v4; // [rsp+0h] [rbp-F8h] BYREF
struct sigaction v5; // [rsp+40h] [rbp-B8h] BYREF

*(_QWORD *)(a1 + 200) = malloc(8uLL);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)timerHandler;
sigemptyset(&v5.sa_mask);
if ( sigaction(10, &v5, 0LL) == -1 )
return puts("Failed to setup signal handling.");
v4.sigev_notify = 0;
v4.sigev_signo = 10;
v4.sigev_value.sival_ptr = (void *)(a1 + 200);
if ( !timer_create(0, &v4, *(timer_t **)(a1 + 200)) )
return printf("RPL: DIO timer %p\n", (const void *)(a1 + 200));
v2 = _errno_location();
v3 = (unsigned int)strerror(*v2);
return printf("RPL: Creating DIO timer failed '%s'.\n", (const char *)v3);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int *v2; // rax
unsigned int v3; // eax
struct sigevent v4; // [rsp+0h] [rbp-F8h] BYREF
struct sigaction v5; // [rsp+40h] [rbp-B8h] BYREF

*(_QWORD *)(a1 + 200) = malloc(8uLL);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)timerHandler;
sigemptyset(&v5.sa_mask);
if ( sigaction(10, &v5, 0LL) == -1 )
return puts("Failed to setup signal handling.");
v4.sigev_notify = 0;
v4.sigev_signo = 10;
v4.sigev_value.sival_ptr = (void *)(a1 + 200);
if ( !timer_create(0, &v4, *(timer_t **)(a1 + 200)) )
return printf("RPL: DIO timer %p\n", (const void *)(a1 + 200));
v2 = _errno_location();
v3 = (unsigned int)strerror(*v2);
return printf("RPL: Creating DIO timer failed '%s'.\n", (const char *)v3);
}

original_code:
void rpl_init_timers(rpl_instance_t *instance) {
  struct sigaction sa;
  struct sigevent te;
  int ret;
  instance->dio_timer = malloc(sizeof(timer_t));
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = timerHandler;
  sigemptyset(&sa.sa_mask);
  if (sigaction(10, &sa, ((void *)0)) == -1) {
    printf("Failed to setup signal handling.\n");
    return;
  }
  te.sigev_notify = SIGEV_SIGNAL;
  te.sigev_signo = 10;
  te.sigev_value.sival_ptr = &instance->dio_timer;
  ret = timer_create(0, &te, instance->dio_timer);
  if (ret != 0) {
    printf("RPL: Creating DIO timer failed '%s'.\n", strerror((*__errno_location ())));
    return;
  } else {
    printf("RPL: DIO timer %p\n", &instance->dio_timer);
  }
}

canonical_original_code:
void rpl_init_timers(struct rpl_instance *instance) {
  struct sigaction sa;
  struct sigevent te;
  int ret;
  instance->dio_timer = malloc(sizeof(timer_t));
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = timerHandler;
  sigemptyset(&sa.sa_mask);
  if (sigaction(10, &sa, ((void *)0)) == -1) {
    printf("Failed to setup signal handling.\n");
    return;
  }
  te.sigev_notify = SIGEV_SIGNAL;
  te.sigev_signo = 10;
  te.sigev_value.sival_ptr = &instance->dio_timer;
  ret = timer_create(0, &te, instance->dio_timer);
  if (ret != 0) {
    printf("RPL: Creating DIO timer failed '%s'.\n", strerror((*__errno_location ())));
    return;
  } else {
    printf("RPL: DIO timer %p\n", &instance->dio_timer);
  }
}

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

variable_types: struct sigevent { union sigval { int sival_int; void *sival_ptr; } sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t { char __size[56]; long __align; } *_attribute; } _sigev_thread; } _sigev_un; }

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

user_defined_types: struct sigevent { union sigval sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t *_attribute; } _sigev_thread; } _sigev_un; }


decompiled_code:
int rpl_reset_periodic_timer()
{
int result; // eax
struct itimerspec v1; // [rsp+0h] [rbp-28h] BYREF

v1.it_value.tv_sec = 1LL;
v1.it_value.tv_nsec = 0LL;
v1.it_interval.tv_sec = 0LL;
v1.it_interval.tv_nsec = 0LL;
rand();
result = timer_settime(0LL, 0, &v1, 0LL);
if ( result )
result = puts("RPL: Scheduling DIS timer failed.");
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
struct itimerspec v1; // [rsp+0h] [rbp-28h] BYREF

v1.it_value.tv_sec = 1LL;
v1.it_value.tv_nsec = 0LL;
v1.it_interval.tv_sec = 0LL;
v1.it_interval.tv_nsec = 0LL;
rand();
result = timer_settime(0LL, 0, &v1, 0LL);
if ( result )
result = puts("RPL: Scheduling DIS timer failed.");
return result;
}

original_code:
void
rpl_reset_periodic_timer(void)
{
  struct itimerspec its;
  int ret;
  its.it_value.tv_sec = 1;
  its.it_value.tv_nsec = 0;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  next_dis = 60 / 2 +
    ((uint32_t)60 * (uint32_t)rand()) / 2147483647 -
    5;
  ret = timer_settime(periodic_timer, 0, &its, ((void *)0));
  if (ret != 0) {
    printf("RPL: Scheduling DIS timer failed.\n");
  }
}

canonical_original_code:
void rpl_reset_periodic_timer()
{
  struct itimerspec its;
  int ret;
  its.it_value.tv_sec = 1;
  its.it_value.tv_nsec = 0;
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 0;
  next_dis = 60 / 2 +
    ((unsigned int)60 * (unsigned int)rand()) / 2147483647 -
    5;
  ret = timer_settime(periodic_timer, 0, &its, ((void *)0));
  if (ret != 0) {
    printf("RPL: Scheduling DIS timer failed.\n");
  }
}


decompiled_code:
int __fastcall rpl_reset_dio_timer(unsigned __int8 *a1)
{
int result; // eax

result = a1[173];
if ( a1[176] > (unsigned __int8)result )
{
a1[178] = 0;
a1[176] = result;
result = new_dio_interval((__int64)a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(unsigned __int8 *a1)
{
int result; // eax

result = a1[173];
if ( a1[176] > (unsigned __int8)result )
{
a1[178] = 0;
a1[176] = result;
result = func0((__int64)a1);
}
return result;
}

original_code:
void
rpl_reset_dio_timer(rpl_instance_t *instance)
{
  if(instance->dio_intcurrent > instance->dio_intmin) {
    instance->dio_counter = 0;
    instance->dio_intcurrent = instance->dio_intmin;
    new_dio_interval(instance);
  }
}

canonical_original_code:
void rpl_reset_dio_timer(struct rpl_instance *instance)
{
  if(instance->dio_intcurrent > instance->dio_intmin) {
    instance->dio_counter = 0;
    instance->dio_intcurrent = instance->dio_intmin;
    new_dio_interval(instance);
  }
}

user_defined_types: struct rpl_dag { struct in6_addr dag_id; unsigned short min_rank; unsigned char version; unsigned char grounded; unsigned char preference; unsigned char used; unsigned char joined; struct rpl_parent *preferred_parent; unsigned short rank; struct rpl_instance *instance; struct rpl_prefix prefix_info; }


decompiled_code:
int __fastcall rpl_schedule_dao(__int64 a1)
{
int result; // eax
struct itimerspec v2; // [rsp+0h] [rbp-28h] BYREF

v2.it_value.tv_sec = 1LL;
v2.it_value.tv_nsec = 0LL;
v2.it_interval.tv_sec = 0LL;
v2.it_interval.tv_nsec = 0LL;
result = timer_settime(**(timer_t **)(a1 + 208), 0, &v2, 0LL);
if ( result )
result = puts("RPL: Stopping DIO timer failed.");
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
int result; // eax
struct itimerspec v2; // [rsp+0h] [rbp-28h] BYREF

v2.it_value.tv_sec = 1LL;
v2.it_value.tv_nsec = 0LL;
v2.it_interval.tv_sec = 0LL;
v2.it_interval.tv_nsec = 0LL;
result = timer_settime(**(timer_t **)(a1 + 208), 0, &v2, 0LL);
if ( result )
result = puts("RPL: Stopping DIO timer failed.");
return result;
}

original_code:
void
rpl_schedule_dao(rpl_instance_t *instance)
{
  struct itimerspec its;
  int ret;
  if( 0) {
    printf("RPL: DAO timer already scheduled\n");
  } else {
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    ret = timer_settime(*(instance->dao_timer), 0, &its, ((void *)0));
    if (ret != 0) {
      printf("RPL: Stopping DIO timer failed.\n");
    }
  }
}

canonical_original_code:
void rpl_schedule_dao(struct rpl_instance *instance)
{
  struct itimerspec its;
  int ret;
  if( 0) {
    printf("RPL: DAO timer already scheduled\n");
  } else {
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    ret = timer_settime(*(instance->dao_timer), 0, &its, ((void *)0));
    if (ret != 0) {
      printf("RPL: Stopping DIO timer failed.\n");
    }
  }
}

user_defined_types: struct rpl_parent { struct rpl_parent *next; struct rpl_dag *dag; struct rpl_metric_container mc; struct in6_addr addr; unsigned short rank; unsigned char link_metric; unsigned char dtsn; unsigned char updated; }


decompiled_code:
__int64 rpl_verify_header()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
int
rpl_verify_header(int uip_ext_opt_offset)
{
  return 0;
}


decompiled_code:
__int64 rpl_update_header_final()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 0LL;
}

original_code:
int
rpl_update_header_final(struct in6_addr *addr)
{
  return 0;
}


decompiled_code:
void rpl_remove_header()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void
rpl_remove_header(void)
{
}

canonical_original_code:
void rpl_remove_header()
{
}


decompiled_code:
__int64 rpl_invert_header()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
return 0LL;
}

original_code:
uint8_t
rpl_invert_header(void)
{
  return 0;
}

canonical_original_code:
unsigned char rpl_invert_header()
{
  return 0;
}

return_type: unsigned char


decompiled_code:
void dis_output()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void
dis_output(struct in6_addr *addr)
{
}