decompiled_code:
void __fastcall free_channels(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

original_code:
void free_channels(Uint8** channels, unsigned int num_of_channels)
{
 int i;
 for (i = 0; i < num_of_channels; i++)
  free(channels[i]);
 free(channels);
}

canonical_original_code:
void free_channels(unsigned char **channels, unsigned int num_of_channels)
{
 int i;
 for (i = 0; i < num_of_channels; i++)
  free(channels[i]);
 free(channels);
}

variable_types:
channels
unsigned char **


repo: poltak/Coursework

decompiled_code:
void __fastcall print_and_die(const char *a1, int a2)
{
switch ( a2 )
{
case 0:
fprintf(stderr, "Fatal IO error encountered:\n%s\n", a1);
break;
case 1:
fprintf(stderr, "Fatal memory error occurred:\n%s\n", a1);
break;
case 2:
fwrite("usage: dumbrletoppm <dumbrle file> <PPM output prefix>\n", 1uLL, 0x37uLL, stderr);
break;
case 3:
fwrite("usage: firstframeview <dumbrle file> <display time 0-*>\n", 1uLL, 0x38uLL, stderr);
break;
case 4:
fwrite("usage: dumbrleplayer <dumbrle file> <frame delay 0-*>\n", 1uLL, 0x36uLL, stderr);
break;
case 5:
fwrite(
"usage: smartrleplayer <dumbrle file> <frame delay 0-*> <brightness 0-100> <contrast 0-100> <saturation 0-100>\n",
1uLL,
0x6EuLL,
stderr);
break;
case 7:
fwrite("File is not a standard DUMBRLEv1 file.\n", 1uLL, 0x27uLL, stderr);
break;
case 8:
fprintf(stderr, "%s\n", a1);
break;
default:
break;
}
exit(1);
}

canonical_decompiled_code:
void __fastcall func13(const char *a1, int a2)
{
switch ( a2 )
{
case 0:
fprintf(stderr, "Fatal IO error encountered:\n%s\n", a1);
break;
case 1:
fprintf(stderr, "Fatal memory error occurred:\n%s\n", a1);
break;
case 2:
fwrite("usage: dumbrletoppm <dumbrle file> <PPM output prefix>\n", 1uLL, 0x37uLL, stderr);
break;
case 3:
fwrite("usage: firstframeview <dumbrle file> <display time 0-*>\n", 1uLL, 0x38uLL, stderr);
break;
case 4:
fwrite("usage: dumbrleplayer <dumbrle file> <frame delay 0-*>\n", 1uLL, 0x36uLL, stderr);
break;
case 5:
fwrite(
"usage: smartrleplayer <dumbrle file> <frame delay 0-*> <brightness 0-100> <contrast 0-100> <saturation 0-100>\n",
1uLL,
0x6EuLL,
stderr);
break;
case 7:
fwrite("File is not a standard DUMBRLEv1 file.\n", 1uLL, 0x27uLL, stderr);
break;
case 8:
fprintf(stderr, "%s\n", a1);
break;
default:
break;
}
exit(1);
}

original_code:
void print_and_die(char *message, Error_t type)
{
    switch (type)
    {
        case IO:
            fprintf(stderr, "Fatal IO error encountered:\n%s\n", message);
            break;
        case MEM:
            fprintf(stderr, "Fatal memory error occurred:\n%s\n", message);
            break;
        case NOT_DUMBRLE:
            fprintf(stderr, "File is not a standard DUMBRLEv1 file.\n");
            break;
        case USAGE_1:
            fprintf(stderr, "usage: dumbrletoppm <dumbrle file> <PPM output prefix>\n");
            break;
        case USAGE_2:
            fprintf(stderr, "usage: firstframeview <dumbrle file> <display time 0-*>\n");
            break;
        case USAGE_3:
            fprintf(stderr, "usage: dumbrleplayer <dumbrle file> <frame delay 0-*>\n");
            break;
        case USAGE_4:
            fprintf(stderr, "usage: smartrleplayer <dumbrle file> <frame delay 0-*> <brightness 0-100> <contrast 0-100> <saturation 0-100>\n");
            break;
        case BAD_HSV:
            fprintf(stderr, "%s\n", message);
            break;
        default:
            break;
    }
    exit(1);
}

canonical_original_code:
void print_and_die(char *message, enum Error_t type)
{
    switch (type)
    {
        case IO:
            fprintf(stderr, "Fatal IO error encountered:\n%s\n", message);
            break;
        case MEM:
            fprintf(stderr, "Fatal memory error occurred:\n%s\n", message);
            break;
        case NOT_DUMBRLE:
            fprintf(stderr, "File is not a standard DUMBRLEv1 file.\n");
            break;
        case USAGE_1:
            fprintf(stderr, "usage: dumbrletoppm <dumbrle file> <PPM output prefix>\n");
            break;
        case USAGE_2:
            fprintf(stderr, "usage: firstframeview <dumbrle file> <display time 0-*>\n");
            break;
        case USAGE_3:
            fprintf(stderr, "usage: dumbrleplayer <dumbrle file> <frame delay 0-*>\n");
            break;
        case USAGE_4:
            fprintf(stderr, "usage: smartrleplayer <dumbrle file> <frame delay 0-*> <brightness 0-100> <contrast 0-100> <saturation 0-100>\n");
            break;
        case BAD_HSV:
            fprintf(stderr, "%s\n", message);
            break;
        default:
            break;
    }
    exit(1);
}

variable_types:
message
char *

variable_types: enum Error_t { IO=0, MEM=1, USAGE_1=2, USAGE_2=3, USAGE_3=4, USAGE_4=5, USAGE_5=6, NOT_DUMBRLE=7, BAD_HSV=8 }

return_type: void


original_code:
Uint32 hsv_to_rgb(struct hsv pixel, SDL_PixelFormat *format)
{
    double R, G, B;
    if (pixel.s == 0)
        R = G = B = pixel.v;
    else
    {
        unsigned int sector_size = 60;
        unsigned int sector = (pixel.h / sector_size);
        double f = (pixel.h - sector * sector_size)/60;
        double m = (pixel.v * (1 - pixel.s));
        double n = (pixel.v * (1 - (pixel.s * f)));
        double k = (pixel.v * (1 - (pixel.s * (1 - f))));
        switch (sector)
        {
            case 0:
                R = pixel.v;
                G = k;
                B = m;
                break;
            case 1:
                R = n;
                G = pixel.v;
                B = m;
                break;
            case 2:
                R = m;
                G = pixel.v;
                B = k;
                break;
            case 3:
                R = m;
                G = n;
                B = pixel.v;
                break;
            case 4:
                R = k;
                G = m;
                B = pixel.v;
                break;
            case 5:
                R = pixel.v;
                G = m;
                B = n;
                break;
            default:
                print_and_die("Invalid H value passed to hsv_to_rgb", BAD_HSV);
        }
    }
    return SDL_MapRGB(format, (Uint8) (R * 255), (Uint8) (G * 255), (Uint8) (B * 255));
}

canonical_original_code:
unsigned int hsv_to_rgb(struct hsv pixel, struct SDL_PixelFormat *format)
{
    double R; double G; double B;
    if (pixel.s == 0)
        R = G = B = pixel.v;
    else
    {
        unsigned int sector_size = 60;
        unsigned int sector = (pixel.h / sector_size);
        double f = (pixel.h - sector * sector_size)/60;
        double m = (pixel.v * (1 - pixel.s));
        double n = (pixel.v * (1 - (pixel.s * f)));
        double k = (pixel.v * (1 - (pixel.s * (1 - f))));
        switch (sector)
        {
            case 0:
                R = pixel.v;
                G = k;
                B = m;
                break;
            case 1:
                R = n;
                G = pixel.v;
                B = m;
                break;
            case 2:
                R = m;
                G = pixel.v;
                B = k;
                break;
            case 3:
                R = m;
                G = n;
                B = pixel.v;
                break;
            case 4:
                R = k;
                G = m;
                B = pixel.v;
                break;
            case 5:
                R = pixel.v;
                G = m;
                B = n;
                break;
            default:
                print_and_die("Invalid H value passed to hsv_to_rgb", BAD_HSV);
        }
    }
    return SDL_MapRGB(format, (Uint8) (R * 255), (Uint8) (G * 255), (Uint8) (B * 255));
}

variable_types: struct hsv { double h; double s; double v; }

variable_types: struct SDL_PixelFormat { struct SDL_Palette { int ncolors; struct SDL_Color { unsigned char r; unsigned char g; unsigned char b; unsigned char unused; } *colors; } *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; } *

variable_types: R double

variable_types: G double

variable_types: B double

variable_types:
sector
unsigned int

variable_types: f double

variable_types: m double

variable_types: n double

variable_types: k double

return_type: unsigned int

user_defined_types: struct SDL_PixelFormat { struct SDL_Palette *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; }

user_defined_types: struct SDL_Palette { int ncolors; struct SDL_Color *colors; }


repo: polymorf/ipfw-user

original_code:
static void
altq_fetch(void)
{
 struct pfioc_altq pfioc;
 struct pf_altq *altq;
 int pffd;
 unsigned int mnr;
 static int altq_fetched = 0;
 if (altq_fetched)
  return;
 altq_fetched = 1;
 pffd = open("/dev/pf", 00);
 if (pffd == -1) {
  warn("altq support opening pf(4) control device");
  return;
 }
 bzero(&pfioc, sizeof(pfioc));
 if (ioctl(pffd, (((2U|1U) << (((0 +8)+8)+14)) | ((('D')) << (0 +8)) | (((47)) << 0) | ((((sizeof(struct pfioc_altq)))) << ((0 +8)+8))), &pfioc) != 0) {
  warn("altq support getting queue list");
  close(pffd);
  return;
 }
 mnr = pfioc.nr;
 for (pfioc.nr = 0; pfioc.nr < mnr; pfioc.nr++) {
  if (ioctl(pffd, (((2U|1U) << (((0 +8)+8)+14)) | ((('D')) << (0 +8)) | (((48)) << 0) | ((((sizeof(struct pfioc_altq)))) << ((0 +8)+8))), &pfioc) != 0) {
   if ((*__errno_location ()) == 16)
    break;
   warn("altq support getting queue list");
   close(pffd);
   return;
  }
  if (pfioc.altq.qid == 0)
   continue;
  altq = safe_calloc(1, sizeof(*altq));
  *altq = pfioc.altq;
  do { (altq)->entries.tqe_next = ((void *)0); (altq)->entries.tqe_prev = (&altq_entries)->tqh_last; *(&altq_entries)->tqh_last = (altq); (&altq_entries)->tqh_last = &(altq)->entries.tqe_next; } while ( 0);
 }
 close(pffd);
}

canonical_original_code:
static void altq_fetch()
{
 struct pfioc_altq pfioc;
 struct pf_altq *altq;
 int pffd;
 unsigned int mnr;
 static int altq_fetched = 0;
 if (altq_fetched)
  return;
 altq_fetched = 1;
 pffd = open("/dev/pf", 00);
 if (pffd == -1) {
  warn("altq support opening pf(4) control device");
  return;
 }
 bzero(&pfioc, sizeof(pfioc));
 if (ioctl(pffd, (((2U|1U) << (((0 +8)+8)+14)) | ((('D')) << (0 +8)) | (((47)) << 0) | ((((sizeof(struct pfioc_altq)))) << ((0 +8)+8))), &pfioc) != 0) {
  warn("altq support getting queue list");
  close(pffd);
  return;
 }
 mnr = pfioc.nr;
 for (pfioc.nr = 0; pfioc.nr < mnr; pfioc.nr++) {
  if (ioctl(pffd, (((2U|1U) << (((0 +8)+8)+14)) | ((('D')) << (0 +8)) | (((48)) << 0) | ((((sizeof(struct pfioc_altq)))) << ((0 +8)+8))), &pfioc) != 0) {
   if ((*__errno_location ()) == 16)
    break;
   warn("altq support getting queue list");
   close(pffd);
   return;
  }
  if (pfioc.altq.qid == 0)
   continue;
  altq = safe_calloc(1, sizeof(*altq));
  *altq = pfioc.altq;
  do { (altq)->entries.tqe_next = ((void *)0); (altq)->entries.tqe_prev = (&altq_entries)->tqh_last; *(&altq_entries)->tqh_last = (altq); (&altq_entries)->tqh_last = &(altq)->entries.tqe_next; } while ( 0);
 }
 close(pffd);
}

variable_types: struct pfioc_altq { unsigned int action; unsigned int ticket; unsigned int nr; struct pf_altq { struct { struct pf_altq *tqe_next; struct pf_altq **tqe_prev; } entries; unsigned int qid; char qname[64]; } altq; }

variable_types: struct pf_altq { struct { struct pf_altq *tqe_next; struct pf_altq **tqe_prev; } entries; unsigned int qid; char qname[64]; } *

variable_types: pffd int

variable_types:
mnr
unsigned int

user_defined_types: struct pfioc_altq { unsigned int action; unsigned int ticket; unsigned int nr; struct pf_altq altq; }


decompiled_code:
__int64 __fastcall altq_qid_to_name(int a1)
{
__int64 result; // rax

altq_fetch();
result = altq_entries;
if ( altq_entries )
{
while ( *(_DWORD *)(result + 16) != a1 )
{
result = *(_QWORD *)result;
if ( !result )
return result;
}
result += 20LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 result; // rax

func0();
result = altq_entries;
if ( altq_entries )
{
while ( *(_DWORD *)(result + 16) != a1 )
{
result = *(_QWORD *)result;
if ( !result )
return result;
}
result += 20LL;
}
return result;
}

original_code:
static const char *
altq_qid_to_name(u_int32_t qid)
{
 struct pf_altq *altq;
 altq_fetch();
 for ((altq) = ((&altq_entries)->tqh_first); (altq); (altq) = ((altq)->entries.tqe_next))
  if (qid == altq->qid)
   break;
 if (altq == ((void *)0))
  return ((void *)0);
 return altq->qname;
}

canonical_original_code:
static char *altq_qid_to_name(unsigned int qid)
{
 struct pf_altq *altq;
 altq_fetch();
 for ((altq) = ((&altq_entries)->tqh_first); (altq); (altq) = ((altq)->entries.tqe_next))
  if (qid == altq->qid)
   break;
 if (altq == ((void *)0))
  return ((void *)0);
 return altq->qname;
}

variable_types:
qid
unsigned int

return_type: char *

user_defined_types: struct pf_altq { struct { struct pf_altq *tqe_next; struct pf_altq **tqe_prev; } entries; unsigned int qid; char qname[64]; }


decompiled_code:
int __fastcall altq_set_enabled(int a1)
{
int v1; // eax
int v2; // ebx

v1 = open("/dev/pf", 2);
if ( v1 == -1 )
err(69, "altq support opening pf(4) control device");
v2 = v1;
if ( a1 )
{
if ( ioctl(v1, 0x442AuLL) && *_errno_location() != 17 )
err(69, "enabling altq");
}
else if ( ioctl(v1, 0x442BuLL) && *_errno_location() != 2 )
{
err(69, "disabling altq");
}
return close(v2);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
int v1; // eax
int v2; // ebx

v1 = open("/dev/pf", 2);
if ( v1 == -1 )
err(69, "altq support opening pf(4) control device");
v2 = v1;
if ( a1 )
{
if ( ioctl(v1, 0x442AuLL) && *_errno_location() != 17 )
err(69, "enabling altq");
}
else if ( ioctl(v1, 0x442BuLL) && *_errno_location() != 2 )
{
err(69, "disabling altq");
}
return close(v2);
}

variable_types: enabled int


decompiled_code:
__int64 __fastcall altq_name_to_qid(char *s1)
{
__int64 v1; // rbx

altq_fetch();
v1 = altq_entries;
if ( !altq_entries )
goto LABEL_4;
while ( strcmp(s1, (const char *)(v1 + 20)) )
{
v1 = *(_QWORD *)v1;
if ( !v1 )
goto LABEL_4;
}
if ( !v1 )
LABEL_4:
errx(65, "altq has no queue named `%s'", s1);
return *(unsigned int *)(v1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1)
{
__int64 v1; // rbx

func0();
v1 = altq_entries;
if ( !altq_entries )
goto LABEL_4;
while ( strcmp(s1, (const char *)(v1 + 20)) )
{
v1 = *(_QWORD *)v1;
if ( !v1 )
goto LABEL_4;
}
if ( !v1 )
LABEL_4:
errx(65, "altq has no queue named `%s'", s1);
return *(unsigned int *)(v1 + 16);
}

original_code:
u_int32_t
altq_name_to_qid(const char *name)
{
 struct pf_altq *altq;
 altq_fetch();
 for ((altq) = ((&altq_entries)->tqh_first); (altq); (altq) = ((altq)->entries.tqe_next))
  if (strcmp(name, altq->qname) == 0)
   break;
 if (altq == ((void *)0))
  errx(65, "altq has no queue named `%s'", name);
 return altq->qid;
}

canonical_original_code:
unsigned int altq_name_to_qid(char *name)
{
 struct pf_altq *altq;
 altq_fetch();
 for ((altq) = ((&altq_entries)->tqh_first); (altq); (altq) = ((altq)->entries.tqe_next))
  if (strcmp(name, altq->qname) == 0)
   break;
 if (altq == ((void *)0))
  errx(65, "altq has no queue named `%s'", name);
 return altq->qid;
}

variable_types:
name
char *


decompiled_code:
void __fastcall print_altq_cmd(__int64 a1)
{
const char *v1; // rsi

if ( a1 )
{
v1 = (const char *)altq_qid_to_name(*(_DWORD *)(a1 + 4));
if ( v1 )
printf(" altq %s", v1);
else
printf(" altq ?<%u>", *(unsigned int *)(a1 + 4));
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
const char *v1; // rsi

if ( a1 )
{
v1 = (const char *)func1(*(_DWORD *)(a1 + 4));
if ( v1 )
printf(" altq %s", v1);
else
printf(" altq ?<%u>", *(unsigned int *)(a1 + 4));
}
}

original_code:
void
print_altq_cmd(ipfw_insn_altq *altqptr)
{
 if (altqptr) {
  const char *qname;
  qname = altq_qid_to_name(altqptr->qid);
  if (qname == ((void *)0))
   printf(" altq ?<%u>", altqptr->qid);
  else
   printf(" altq %s", qname);
 }
}

canonical_original_code:
void print_altq_cmd(struct _ipfw_insn_altq *altqptr)
{
 if (altqptr) {
  char *qname;
  qname = altq_qid_to_name(altqptr->qid);
  if (qname == ((void *)0))
   printf(" altq ?<%u>", altqptr->qid);
  else
   printf(" altq %s", qname);
 }
}

variable_types: struct _ipfw_insn_altq { struct _ipfw_insn { unsigned int opcode; unsigned int len; unsigned int arg1; } o; unsigned int qid; } *

variable_types:
qname
char *

user_defined_types: struct _ipfw_insn_altq { struct _ipfw_insn o; unsigned int qid; }


decompiled_code:
__int64 __fastcall lookup_host6(char *name, void *buf)
{
unsigned int v2; // ebx
struct hostent *v4; // rax

v2 = 0;
if ( !inet_pton(10, name, buf) )
{
v2 = 0;
v4 = gethostbyname2(name, 10);
if ( v4 )
*(__m128i *)buf = _mm_loadu_si128((const __m128i *)*v4->h_addr_list);
else
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *name, void *buf)
{
unsigned int v2; // ebx
struct hostent *v4; // rax

v2 = 0;
if ( !inet_pton(10, name, buf) )
{
v2 = 0;
v4 = gethostbyname2(name, 10);
if ( v4 )
*(__m128i *)buf = _mm_loadu_si128((const __m128i *)*v4->h_addr_list);
else
v2 = -1;
}
return v2;
}

original_code:
static int
lookup_host6 (char *host, struct in6_addr *ip6addr)
{
 struct hostent *he;
 if (!inet_pton(10, host, ip6addr)) {
  if ((he = gethostbyname2(host, 10)) == ((void *)0))
   return(-1);
  memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
 }
 return(0);
}

canonical_original_code:
static int lookup_host6(char *host, struct in6_addr *ip6addr)
{
 struct hostent *he;
 if (!inet_pton(10, host, ip6addr)) {
  if ((he = gethostbyname2(host, 10)) == ((void *)0))
   return(-1);
  memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
 }
 return(0);
}

variable_types:
host
char *

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

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

return_type: int


decompiled_code:
int __fastcall print_unreach6_code(unsigned __int16 a1)
{
const char *v1; // rax
int result; // eax

v1 = (const char *)match_value(&icmp6codes, a1);
if ( v1 )
result = printf("unreach6 %s", v1);
else
result = printf("unreach6 %u", a1);
return result;
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int16 a1)
{
const char *v1; // rax
int result; // eax

v1 = (const char *)match_value(&icmp6codes, a1);
if ( v1 )
result = printf("unreach6 %s", v1);
else
result = printf("unreach6 %u", a1);
return result;
}

original_code:
void
print_unreach6_code(uint16_t code)
{
 char const *s = match_value(icmp6codes, code);
 if (s != ((void *)0))
  printf("unreach6 %s", s);
 else
  printf("unreach6 %u", code);
}

canonical_original_code:
void print_unreach6_code(unsigned short code)
{
 char *s = match_value(icmp6codes, code);
 if (s != ((void *)0))
  printf("unreach6 %s", s);
 else
  printf("unreach6 %u", code);
}

variable_types:
code
unsigned short

variable_types:
s
char *


decompiled_code:
int __fastcall print_ip6(char *a1, const char *a2)
{
char v3; // bl
const char *v4; // rsi
char v5; // al
unsigned __int8 v6; // dl
int v7; // ebx
int result; // eax
int v9; // ebx
int v10; // ebx
int v11; // ebx
_BYTE *v12; // rbp
struct hostent *v13; // r12
int v14; // eax
int v15; // er15
const char *v16; // rax
char v17[312]; // [rsp+0h] [rbp-138h] BYREF

v3 = a1[1];
v4 = " not";
if ( v3 >= 0 )
v4 = (const char *)&unk_C09;
printf("%s%s ", v4, a2);
v5 = *a1;
if ( *a1 == 67 || v5 == 70 )
return printf("me6");
if ( v5 == 75 )
return printf(" ip6");
v6 = v3 & 0x3F;
v7 = v3 & 0x3F;
result = v7 + 2;
v9 = v7 - 1;
if ( v9 < 0 )
v9 = result;
v10 = v9 >> 2;
if ( v6 > 4u )
{
v11 = v10 - 2;
v12 = a1 + 36;
v13 = 0LL;
while ( 1 )
{
if ( *a1 == 66 || *a1 == 69 || (v14 = contigmask(v12 - 16, 128LL), v15 = v14, v14 == 128) )
{
if ( co[1] )
v13 = gethostbyaddr(v12 - 32, 0x10u, 10);
if ( v13 )
{
LABEL_29:
printf("%s", v13->h_name);
goto LABEL_30;
}
v15 = 128;
if ( inet_ntop(10, v12 - 32, v17, 0xFFu) )
{
printf("%s", v17);
goto LABEL_30;
}
}
else
{
if ( v13 )
goto LABEL_29;
if ( !v14 )
{
printf("any");
goto LABEL_30;
}
if ( inet_ntop(10, v12 - 32, v17, 0xFFu) )
goto LABEL_36;
}
puts("Error ntop in print_ip6");
LABEL_36:
printf("%s", v17);
if ( v15 >= 0 )
{
if ( v15 <= 127 )
printf("/%d", (unsigned int)v15);
v13 = 0LL;
}
else
{
v16 = inet_ntop(10, v12 - 16, v17, 0xFFu);
printf(":%s", v16);
v13 = 0LL;
}
LABEL_30:
result = v11 + 2;
if ( v11 + 2 > 2 )
{
putchar(44);
}
else if ( v11 <= 0 )
{
return result;
}
v11 -= 2;
v12 += 32;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(char *a1, const char *a2)
{
char v3; // bl
const char *v4; // rsi
char v5; // al
unsigned __int8 v6; // dl
int v7; // ebx
int result; // eax
int v9; // ebx
int v10; // ebx
int v11; // ebx
_BYTE *v12; // rbp
struct hostent *v13; // r12
int v14; // eax
int v15; // er15
const char *v16; // rax
char v17[312]; // [rsp+0h] [rbp-138h] BYREF

v3 = a1[1];
v4 = " not";
if ( v3 >= 0 )
v4 = (const char *)&unk_C09;
printf("%s%s ", v4, a2);
v5 = *a1;
if ( *a1 == 67 || v5 == 70 )
return printf("me6");
if ( v5 == 75 )
return printf(" ip6");
v6 = v3 & 0x3F;
v7 = v3 & 0x3F;
result = v7 + 2;
v9 = v7 - 1;
if ( v9 < 0 )
v9 = result;
v10 = v9 >> 2;
if ( v6 > 4u )
{
v11 = v10 - 2;
v12 = a1 + 36;
v13 = 0LL;
while ( 1 )
{
if ( *a1 == 66 || *a1 == 69 || (v14 = contigmask(v12 - 16, 128LL), v15 = v14, v14 == 128) )
{
if ( co[1] )
v13 = gethostbyaddr(v12 - 32, 0x10u, 10);
if ( v13 )
{
LABEL_29:
printf("%s", v13->h_name);
goto LABEL_30;
}
v15 = 128;
if ( inet_ntop(10, v12 - 32, v17, 0xFFu) )
{
printf("%s", v17);
goto LABEL_30;
}
}
else
{
if ( v13 )
goto LABEL_29;
if ( !v14 )
{
printf("any");
goto LABEL_30;
}
if ( inet_ntop(10, v12 - 32, v17, 0xFFu) )
goto LABEL_36;
}
puts("Error ntop in print_ip6");
LABEL_36:
printf("%s", v17);
if ( v15 >= 0 )
{
if ( v15 <= 127 )
printf("/%d", (unsigned int)v15);
v13 = 0LL;
}
else
{
v16 = inet_ntop(10, v12 - 16, v17, 0xFFu);
printf(":%s", v16);
v13 = 0LL;
}
LABEL_30:
result = v11 + 2;
if ( v11 + 2 > 2 )
{
putchar(44);
}
else if ( v11 <= 0 )
{
return result;
}
v11 -= 2;
v12 += 32;
}
}
return result;
}

original_code:
void
print_ip6(ipfw_insn_ip6 *cmd, char const *s)
{
       struct hostent *he = ((void *)0);
       int len = (((ipfw_insn *) cmd)->len & 0x3f) - 1;
       struct in6_addr *a = &(cmd->addr6);
       char trad[255];
       printf("%s%s ", cmd->o.len & 0x80 ? " not": "", s);
       if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
        printf("me6");
        return;
       }
       if (cmd->o.opcode == O_IP6) {
        printf(" ip6");
        return;
       }
       for (len = len / 4; len > 0; len -= 2, a += 2) {
    int mb =
        (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ?
        128 : contigmask((uint8_t *)&(a[1]), 128);
    if (mb == 128 && co.do_resolv)
        he = gethostbyaddr((char *)a, sizeof(*a), 10);
    if (he != ((void *)0))
        printf("%s", he->h_name);
    else if (mb == 0)
        printf("any");
    else {
        if (inet_ntop(10, a, trad, sizeof( trad ) ) == ((void *)0))
     printf("Error ntop in print_ip6\n");
        printf("%s", trad );
        if (mb < 0)
     printf(":%s",
         inet_ntop(10, &a[1], trad, sizeof(trad)));
        else if (mb < 128)
     printf("/%d", mb);
    }
    if (len > 2)
        printf(",");
       }
}

canonical_original_code:
void print_ip6(struct _ipfw_insn_ip6 *cmd, char *s)
{
       struct hostent *he = ((void *)0);
       int len = (((ipfw_insn *) cmd)->len & 0x3f) - 1;
       struct in6_addr *a = &(cmd->addr6);
       char trad[255];
       printf("%s%s ", cmd->o.len & 0x80 ? " not": "", s);
       if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
        printf("me6");
        return;
       }
       if (cmd->o.opcode == O_IP6) {
        printf(" ip6");
        return;
       }
       for (len = len / 4; len > 0; len -= 2, a += 2) {
    int mb = (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ?
        128 : contigmask((uint8_t *)&(a[1]), 128);
    if (mb == 128 && co.do_resolv)
        he = gethostbyaddr((char *)a, sizeof(*a), 10);
    if (he != ((void *)0))
        printf("%s", he->h_name);
    else if (mb == 0)
        printf("any");
    else {
        if (inet_ntop(10, a, trad, sizeof( trad ) ) == ((void *)0))
     printf("Error ntop in print_ip6\n");
        printf("%s", trad );
        if (mb < 0)
     printf(":%s",
         inet_ntop(10, &a[1], trad, sizeof(trad)));
        else if (mb < 128)
     printf("/%d", mb);
    }
    if (len > 2)
        printf(",");
       }
}

variable_types: struct _ipfw_insn_ip6 { struct _ipfw_insn { unsigned int opcode; unsigned int len; unsigned int arg1; } o; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int u6_addr32[4]; } __in6_u; } addr6; struct in6_addr mask6; } *

variable_types: len int

variable_types:
trad
char [255]

variable_types: mb int

user_defined_types: struct _ipfw_insn_ip6 { struct _ipfw_insn o; struct in6_addr addr6; struct in6_addr mask6; }

user_defined_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }


decompiled_code:
int __fastcall print_icmp6types(__int64 a1)
{
_DWORD *v1; // rbp
int v2; // er13
char v3; // si
int i; // ebx
int result; // eax

printf(" ip6 icmp6types");
v1 = (_DWORD *)(a1 + 4);
v2 = 0;
v3 = 32;
do
{
for ( i = 0; i != 32; ++i )
{
result = *v1 & (1 << i);
if ( result )
{
result = printf("%c%d", (unsigned int)v3, (unsigned int)(i + v2));
v3 = 44;
}
}
v2 += 32;
++v1;
}
while ( v2 != 224 );
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
_DWORD *v1; // rbp
int v2; // er13
char v3; // si
int i; // ebx
int result; // eax

printf(" ip6 icmp6types");
v1 = (_DWORD *)(a1 + 4);
v2 = 0;
v3 = 32;
do
{
for ( i = 0; i != 32; ++i )
{
result = *v1 & (1 << i);
if ( result )
{
result = printf("%c%d", (unsigned int)v3, (unsigned int)(i + v2));
v3 = 44;
}
}
v2 += 32;
++v1;
}
while ( v2 != 224 );
return result;
}

original_code:
void
print_icmp6types(ipfw_insn_u32 *cmd)
{
       int i, j;
       char sep= ' ';
       printf(" ip6 icmp6types");
       for (i = 0; i < 7; i++)
        for (j=0; j < 32; ++j) {
         if ( (cmd->d[i] & (1 << (j))) == 0)
          continue;
         printf("%c%d", sep, (i*32 + j));
         sep = ',';
        }
}

canonical_original_code:
void print_icmp6types(struct _ipfw_insn_u32 *cmd)
{
       int i; int j;
       char sep = ' ';
       printf(" ip6 icmp6types");
       for (i = 0; i < 7; i++)
        for (j=0; j < 32; ++j) {
         if ( (cmd->d[i] & (1 << (j))) == 0)
          continue;
         printf("%c%d", sep, (i*32 + j));
         sep = ',';
        }
}

variable_types: struct _ipfw_insn_u32 { struct _ipfw_insn { unsigned int opcode; unsigned int len; unsigned int arg1; } o; unsigned int d[1]; } *

variable_types: i int

variable_types: j int

variable_types: sep char

user_defined_types: struct _ipfw_insn_u32 { struct _ipfw_insn o; unsigned int d[1]; }


decompiled_code:
int __fastcall print_flow6id(__int64 a1)
{
unsigned __int16 v1; // r13
int result; // eax
__int64 v3; // rbx
char v4; // bp

v1 = *(_WORD *)(a1 + 2);
result = printf(" flow-id ");
if ( v1 )
{
v3 = 0LL;
v4 = 44;
do
{
if ( v1 - 1 == (_DWORD)v3 )
v4 = 32;
result = printf("%d%c", *(unsigned int *)(a1 + 4 * v3++ + 4), (unsigned int)v4);
}
while ( v1 > (unsigned __int16)v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
unsigned __int16 v1; // r13
int result; // eax
__int64 v3; // rbx
char v4; // bp

v1 = *(_WORD *)(a1 + 2);
result = printf(" flow-id ");
if ( v1 )
{
v3 = 0LL;
v4 = 44;
do
{
if ( v1 - 1 == (_DWORD)v3 )
v4 = 32;
result = printf("%d%c", *(unsigned int *)(a1 + 4 * v3++ + 4), (unsigned int)v4);
}
while ( v1 > (unsigned __int16)v3 );
}
return result;
}

original_code:
void
print_flow6id( ipfw_insn_u32 *cmd)
{
       uint16_t i, limit = cmd->o.arg1;
       char sep = ',';
       printf(" flow-id ");
       for( i=0; i < limit; ++i) {
        if (i == limit - 1)
         sep = ' ';
        printf("%d%c", cmd->d[i], sep);
       }
}

canonical_original_code:
void print_flow6id(struct _ipfw_insn_u32 *cmd)
{
       unsigned short i; unsigned short limit = cmd->o.arg1;
       char sep = ',';
       printf(" flow-id ");
       for( i=0; i < limit; ++i) {
        if (i == limit - 1)
         sep = ' ';
        printf("%d%c", cmd->d[i], sep);
       }
}

variable_types:
i
unsigned short

variable_types:
limit
unsigned short

user_defined_types: struct _ipfw_insn { unsigned int opcode; unsigned int len; unsigned int arg1; }


decompiled_code:
__int64 __fastcall fill_ext6hdr(__int64 a1, char *a2)
{
__int64 result; // rax
char *v3; // rax
int v4; // eax
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_WORD *)(a1 + 2) = 0;
if ( !a2 )
return 0LL;
do
{
v3 = strsep(&stringp, ",");
v4 = match_token(&ext6hdrcodes, v3);
if ( v4 <= 32 )
{
if ( v4 > 0 )
{
switch ( v4 )
{
case 1:
*(_WORD *)(a1 + 2) |= 1u;
continue;
case 2:
*(_WORD *)(a1 + 2) |= 2u;
continue;
case 4:
*(_WORD *)(a1 + 2) |= 4u;
continue;
case 8:
*(_WORD *)(a1 + 2) |= 8u;
continue;
case 16:
*(_WORD *)(a1 + 2) |= 0x10u;
continue;
case 32:
*(_WORD *)(a1 + 2) |= 0x20u;
continue;
default:
break;
}
}
LABEL_17:
errx(65, "invalid option for ipv6 exten header");
}
if ( v4 == 64 )
{
*(_WORD *)(a1 + 2) |= 0x40u;
}
else
{
if ( v4 != 128 )
goto LABEL_17;
*(_WORD *)(a1 + 2) |= 0x80u;
}
}
while ( stringp );
result = 0LL;
if ( *(_WORD *)(a1 + 2) )
{
*(_BYTE *)a1 = 74;
*(_BYTE *)(a1 + 1) |= 1u;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, char *a2)
{
__int64 result; // rax
char *v3; // rax
int v4; // eax
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_WORD *)(a1 + 2) = 0;
if ( !a2 )
return 0LL;
do
{
v3 = strsep(&stringp, ",");
v4 = match_token(&ext6hdrcodes, v3);
if ( v4 <= 32 )
{
if ( v4 > 0 )
{
switch ( v4 )
{
case 1:
*(_WORD *)(a1 + 2) |= 1u;
continue;
case 2:
*(_WORD *)(a1 + 2) |= 2u;
continue;
case 4:
*(_WORD *)(a1 + 2) |= 4u;
continue;
case 8:
*(_WORD *)(a1 + 2) |= 8u;
continue;
case 16:
*(_WORD *)(a1 + 2) |= 0x10u;
continue;
case 32:
*(_WORD *)(a1 + 2) |= 0x20u;
continue;
default:
break;
}
}
LABEL_17:
errx(65, "invalid option for ipv6 exten header");
}
if ( v4 == 64 )
{
*(_WORD *)(a1 + 2) |= 0x40u;
}
else
{
if ( v4 != 128 )
goto LABEL_17;
*(_WORD *)(a1 + 2) |= 0x80u;
}
}
while ( stringp );
result = 0LL;
if ( *(_WORD *)(a1 + 2) )
{
*(_BYTE *)a1 = 74;
*(_BYTE *)(a1 + 1) |= 1u;
result = 1LL;
}
return result;
}

original_code:
int
fill_ext6hdr( ipfw_insn *cmd, char *av)
{
       int tok;
       char *s = av;
       cmd->arg1 = 0;
       while(s) {
    av = strsep( &s, ",") ;
    tok = match_token(ext6hdrcodes, av);
    switch (tok) {
    case 0x1:
        cmd->arg1 |= 0x1;
        break;
    case 0x2:
        cmd->arg1 |= 0x2;
        break;
    case 0x4:
        cmd->arg1 |= 0x4;
        break;
    case 0x20:
        cmd->arg1 |= 0x20;
        break;
    case 0x8:
        cmd->arg1 |= 0x8;
        break;
    case 0x10:
        cmd->arg1 |= 0x10;
        break;
    case 0x40:
        cmd->arg1 |= 0x40;
        break;
    case 0x80:
        cmd->arg1 |= 0x80;
        break;
    default:
        errx( 65, "invalid option for ipv6 exten header" );
        break;
    }
       }
       if (cmd->arg1 == 0 )
    return 0;
       cmd->opcode = O_EXT_HDR;
       cmd->len |= ((sizeof (ipfw_insn))/sizeof(u_int32_t));
       return 1;
}

canonical_original_code:
int fill_ext6hdr(struct _ipfw_insn *cmd, char *av)
{
       int tok;
       char *s = av;
       cmd->arg1 = 0;
       while(s) {
    av = strsep( &s, ",") ;
    tok = match_token(ext6hdrcodes, av);
    switch (tok) {
    case 0x1:
        cmd->arg1 |= 0x1;
        break;
    case 0x2:
        cmd->arg1 |= 0x2;
        break;
    case 0x4:
        cmd->arg1 |= 0x4;
        break;
    case 0x20:
        cmd->arg1 |= 0x20;
        break;
    case 0x8:
        cmd->arg1 |= 0x8;
        break;
    case 0x10:
        cmd->arg1 |= 0x10;
        break;
    case 0x40:
        cmd->arg1 |= 0x40;
        break;
    case 0x80:
        cmd->arg1 |= 0x80;
        break;
    default:
        errx( 65, "invalid option for ipv6 exten header" );
        break;
    }
       }
       if (cmd->arg1 == 0 )
    return 0;
       cmd->opcode = O_EXT_HDR;
       cmd->len |= ((sizeof (ipfw_insn))/sizeof(u_int32_t));
       return 1;
}

variable_types: struct _ipfw_insn { unsigned int opcode; unsigned int len; unsigned int arg1; } *

variable_types:
av
char *

variable_types: tok int


decompiled_code:
int __fastcall print_ext6hdr(__int64 a1)
{
int result; // eax
char v2; // si

result = printf(" extension header:");
v2 = 32;
if ( (*(_BYTE *)(a1 + 2) & 1) != 0 )
{
result = printf("%cfragmentation", 32LL);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 2) != 0 )
{
result = printf("%chop options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 4) != 0 )
{
result = printf("%crouting options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x40) != 0 )
{
result = printf("%crthdr0", (unsigned int)v2);
v2 = 44;
}
if ( *(char *)(a1 + 2) < 0 )
{
result = printf("%crthdr2", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x20) != 0 )
{
result = printf("%cdestination options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 8) != 0 )
{
result = printf("%cauthentication header", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x10) != 0 )
result = printf("%cencapsulated security payload", (unsigned int)v2);
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1)
{
int result; // eax
char v2; // si

result = printf(" extension header:");
v2 = 32;
if ( (*(_BYTE *)(a1 + 2) & 1) != 0 )
{
result = printf("%cfragmentation", 32LL);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 2) != 0 )
{
result = printf("%chop options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 4) != 0 )
{
result = printf("%crouting options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x40) != 0 )
{
result = printf("%crthdr0", (unsigned int)v2);
v2 = 44;
}
if ( *(char *)(a1 + 2) < 0 )
{
result = printf("%crthdr2", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x20) != 0 )
{
result = printf("%cdestination options", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 8) != 0 )
{
result = printf("%cauthentication header", (unsigned int)v2);
v2 = 44;
}
if ( (*(_BYTE *)(a1 + 2) & 0x10) != 0 )
result = printf("%cencapsulated security payload", (unsigned int)v2);
return result;
}

original_code:
void
print_ext6hdr( ipfw_insn *cmd )
{
       char sep = ' ';
       printf(" extension header:");
       if (cmd->arg1 & 0x1 ) {
    printf("%cfragmentation", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x2 ) {
    printf("%chop options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x4 ) {
    printf("%crouting options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x40 ) {
    printf("%crthdr0", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x80 ) {
    printf("%crthdr2", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x20 ) {
    printf("%cdestination options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x8 ) {
    printf("%cauthentication header", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x10 ) {
    printf("%cencapsulated security payload", sep);
       }
}

canonical_original_code:
void print_ext6hdr(struct _ipfw_insn *cmd)
{
       char sep = ' ';
       printf(" extension header:");
       if (cmd->arg1 & 0x1 ) {
    printf("%cfragmentation", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x2 ) {
    printf("%chop options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x4 ) {
    printf("%crouting options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x40 ) {
    printf("%crthdr0", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x80 ) {
    printf("%crthdr2", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x20 ) {
    printf("%cdestination options", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x8 ) {
    printf("%cauthentication header", sep);
    sep = ',';
       }
       if (cmd->arg1 & 0x10 ) {
    printf("%cencapsulated security payload", sep);
       }
}


decompiled_code:
__int64 __fastcall fill_flow6(__int64 a1, char *a2)
{
int v2; // ebx
unsigned int v3; // eax
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF
char *stringp; // [rsp+18h] [rbp-20h] BYREF

endptr = a2;
stringp = a2;
*(_DWORD *)(a1 + 4) = 0;
if ( !a2 )
goto LABEL_11;
v2 = 0;
do
{
endptr = strsep(&stringp, ",");
v3 = strtoul(endptr, &endptr, 0);
if ( *endptr != 44 && *endptr )
errx(65, "invalid ipv6 flow number %s", endptr);
if ( v3 > 0xFFFFF )
errx(65, "flow number out of range %s", endptr);
*(_DWORD *)(a1 + 4LL * (unsigned __int16)v2++ + 4) |= v3;
}
while ( stringp );
if ( !(_WORD)v2 )
LABEL_11:
errx(65, "invalid ipv6 flow number %s", endptr);
*(_BYTE *)a1 = 72;
result = (unsigned int)(v2 + 2);
*(_BYTE *)(a1 + 1) |= (_BYTE)v2 + 2;
*(_WORD *)(a1 + 2) = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char *a2)
{
int v2; // ebx
unsigned int v3; // eax
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF
char *stringp; // [rsp+18h] [rbp-20h] BYREF

endptr = a2;
stringp = a2;
*(_DWORD *)(a1 + 4) = 0;
if ( !a2 )
goto LABEL_11;
v2 = 0;
do
{
endptr = strsep(&stringp, ",");
v3 = strtoul(endptr, &endptr, 0);
if ( *endptr != 44 && *endptr )
errx(65, "invalid ipv6 flow number %s", endptr);
if ( v3 > 0xFFFFF )
errx(65, "flow number out of range %s", endptr);
*(_DWORD *)(a1 + 4LL * (unsigned __int16)v2++ + 4) |= v3;
}
while ( stringp );
if ( !(_WORD)v2 )
LABEL_11:
errx(65, "invalid ipv6 flow number %s", endptr);
*(_BYTE *)a1 = 72;
result = (unsigned int)(v2 + 2);
*(_BYTE *)(a1 + 1) |= (_BYTE)v2 + 2;
*(_WORD *)(a1 + 2) = v2;
return result;
}

original_code:
void
fill_flow6( ipfw_insn_u32 *cmd, char *av )
{
 u_int32_t type;
 u_int16_t nflow = 0;
 char *s = av;
 cmd->d[0] = 0;
 while (s) {
  av = strsep( &s, ",") ;
  type = strtoul(av, &av, 0);
  if (*av != ',' && *av != '\0')
   errx(65, "invalid ipv6 flow number %s", av);
  if (type > 0xfffff)
   errx(65, "flow number out of range %s", av);
  cmd->d[nflow] |= type;
  nflow++;
 }
 if( nflow > 0 ) {
  cmd->o.opcode = O_FLOW6ID;
  cmd->o.len |= ((sizeof (ipfw_insn_u32))/sizeof(u_int32_t)) + nflow;
  cmd->o.arg1 = nflow;
 }
 else {
  errx(65, "invalid ipv6 flow number %s", av);
 }
}

canonical_original_code:
void fill_flow6(struct _ipfw_insn_u32 *cmd, char *av)
{
 unsigned int type;
 unsigned int nflow = 0;
 char *s = av;
 cmd->d[0] = 0;
 while (s) {
  av = strsep( &s, ",") ;
  type = strtoul(av, &av, 0);
  if (*av != ',' && *av != '\0')
   errx(65, "invalid ipv6 flow number %s", av);
  if (type > 0xfffff)
   errx(65, "flow number out of range %s", av);
  cmd->d[nflow] |= type;
  nflow++;
 }
 if( nflow > 0 ) {
  cmd->o.opcode = O_FLOW6ID;
  cmd->o.len |= ((sizeof (ipfw_insn_u32))/sizeof(u_int32_t)) + nflow;
  cmd->o.arg1 = nflow;
 }
 else {
  errx(65, "invalid ipv6 flow number %s", av);
 }
}

variable_types:
type
unsigned int

variable_types:
nflow
unsigned int


decompiled_code:
__int64 do_del()
{
return 1LL;
}

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

original_code:
static int
do_del(void *obj, void *arg)
{
 return DNHT_SCAN_DEL;
}

variable_types:
obj
void *

variable_types:
arg
void *


decompiled_code:
__int64 __fastcall heap_resize(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // esi
unsigned int v4; // er12
void *v5; // rax
void *v6; // rbp

result = 0LL;
if ( *a1 < a2 )
{
v3 = a2 | (a2 >> 1) | ((a2 | (a2 >> 1)) >> 2);
v4 = (((v3 >> 4) | v3 | (((v3 >> 4) | v3) >> 8)) >> 16) | (v3 >> 4) | v3 | (((v3 >> 4) | v3) >> 8);
v5 = (void *)kern_malloc(16 * v4);
v6 = v5;
if ( v5 )
{
if ( (int)*a1 > 0 )
{
memmove(v5, *((const void **)a1 + 2), 16LL * (int)*a1);
kern_free(*((_QWORD *)a1 + 2));
}
*((_QWORD *)a1 + 2) = v6;
*a1 = v4;
result = 0LL;
}
else
{
printf("--- %s, resize %d failed\n", "heap_resize", v4);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // esi
unsigned int v4; // er12
void *v5; // rax
void *v6; // rbp

result = 0LL;
if ( *a1 < a2 )
{
v3 = a2 | (a2 >> 1) | ((a2 | (a2 >> 1)) >> 2);
v4 = (((v3 >> 4) | v3 | (((v3 >> 4) | v3) >> 8)) >> 16) | (v3 >> 4) | v3 | (((v3 >> 4) | v3) >> 8);
v5 = (void *)kern_malloc(16 * v4);
v6 = v5;
if ( v5 )
{
if ( (int)*a1 > 0 )
{
memmove(v5, *((const void **)a1 + 2), 16LL * (int)*a1);
kern_free(*((_QWORD *)a1 + 2));
}
*((_QWORD *)a1 + 2) = v6;
*a1 = v4;
result = 0LL;
}
else
{
printf("--- %s, resize %d failed\n", "heap_resize", v4);
result = 1LL;
}
}
return result;
}

original_code:
static int
heap_resize(struct dn_heap *h, unsigned int new_size)
{
 struct dn_heap_entry *p;
 if (h->size >= new_size )
  return 0;
 new_size |= new_size >> 1;
 new_size |= new_size >> 2;
 new_size |= new_size >> 4;
 new_size |= new_size >> 8;
 new_size |= new_size >> 16;
 p = kern_malloc(new_size * sizeof(*p));
 if (p == ((void *)0)) {
  printf("--- %s, resize %d failed\n", __func__, new_size );
  return 1;
 }
 if (h->size > 0) {
  bcopy(h->p, p, h->size * sizeof(*p) );
  kern_free(h->p);
 }
 h->p = p;
 h->size = new_size;
 return 0;
}

variable_types: struct dn_heap { int size; int elements; int ofs; struct dn_heap_entry { unsigned long key; void *object; } *p; } *

user_defined_types: struct dn_heap { int size; int elements; int ofs; struct dn_heap_entry *p; }


decompiled_code:
__int64 __fastcall heap_init(unsigned int *a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = heap_resize(a1, a2);
if ( (_DWORD)result )
return 1LL;
a1[1] = 0;
a1[2] = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = func1(a1, a2);
if ( (_DWORD)result )
return 1LL;
a1[1] = 0;
a1[2] = a3;
return result;
}

original_code:
int
heap_init(struct dn_heap *h, int size, int ofs)
{
 if (heap_resize(h, size))
  return 1;
 h->elements = 0;
 h->ofs = ofs;
 return 0;
}

variable_types: size int

variable_types: ofs int

user_defined_types: struct dn_heap_entry { unsigned long key; void *object; }


original_code:
int
heap_insert(struct dn_heap *h, uint64_t key1, void *p)
{
 int son = h->elements;
 if (p == ((void *)0)) {
  son = key1;
 } else {
  son = h->elements;
  if (son == h->size)
   if (heap_resize(h, h->elements+16) )
    return 1;
  h->p[son].object = p;
  h->p[son].key = key1;
  h->elements++;
 }
 while (son > 0) {
  int father = ( ( (son) - 1 ) / 2 );
  struct dn_heap_entry tmp;
  if (((int64_t)((h->p[father].key)-(h->p[son].key)) < 0) )
   break;
  { tmp = h->p[son] ; h->p[son] = h->p[father] ; h->p[father] = tmp ; };
  do { if (h->ofs > 0) *((int32_t *)((char *)(h->p[son].object) + h->ofs)) = son; } while (0);
  son = father;
 }
 do { if (h->ofs > 0) *((int32_t *)((char *)(h->p[son].object) + h->ofs)) = son; } while (0);
 return 0;
}

canonical_original_code:
int heap_insert(struct dn_heap *h, unsigned long key1, void *p)
{
 int son = h->elements;
 if (p == ((void *)0)) {
  son = key1;
 } else {
  son = h->elements;
  if (son == h->size)
   if (heap_resize(h, h->elements+16) )
    return 1;
  h->p[son].object = p;
  h->p[son].key = key1;
  h->elements++;
 }
 while (son > 0) {
  int father = ( ( (son) - 1 ) / 2 );
  struct dn_heap_entry tmp;
  if (((long)((h->p[father].key)-(h->p[son].key)) < 0) )
   break;
  { tmp = h->p[son] ; h->p[son] = h->p[father] ; h->p[father] = tmp ; };
  do { if (h->ofs > 0) *((int *)((char *)(h->p[son].object) + h->ofs)) = son; } while (0);
  son = father;
 }
 do { if (h->ofs > 0) *((int *)((char *)(h->p[son].object) + h->ofs)) = son; } while (0);
 return 0;
}

variable_types:
p
void *

variable_types: son int

variable_types: father int

variable_types: struct dn_heap_entry { unsigned long key; void *object; }


heapify

decompiled_code:
__int64 __fastcall heapify(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax

if ( *(int *)(a1 + 4) > 0 )
{
v1 = 0LL;
do
result = heap_insert(a1, v1++, 0LL);
while ( *(_DWORD *)(a1 + 4) > (int)v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax

if ( *(int *)(a1 + 4) > 0 )
{
v1 = 0LL;
do
result = func3(a1, v1++, 0LL);
while ( *(_DWORD *)(a1 + 4) > (int)v1 );
}
return result;
}

original_code:
static void
heapify(struct dn_heap *h)
{
 int i;
 for (i = 0; i < h->elements; i++ )
  heap_insert(h, i , ((void *)0));
}

variable_types: i int


original_code:
void
heap_extract(struct dn_heap *h, void *obj)
{
 int child, father, max = h->elements - 1;
 if (max < 0) {
  printf("--- %s: empty heap 0x%p\n", __FUNCTION__, h);
  return;
 }
 if (obj == ((void *)0))
  father = 0;
 else {
  if (h->ofs <= 0)
   panic("%s: extract from middle not set on %p\n",
    __FUNCTION__, h);
  father = *((int *)((char *)obj + h->ofs));
  if (father < 0 || father >= h->elements) {
   panic("%s: father %d out of bound 0..%d\n",
    __FUNCTION__, father, h->elements);
  }
 }
 do { if (h->ofs > 0) *((int32_t *)((char *)(h->p[father].object) + h->ofs)) = -16; } while (0);
 while ( (child = ( (father)+(father) + 1 )) <= max ) {
  if (child != max &&
      ((int64_t)((h->p[child+1].key)-(h->p[child].key)) < 0) )
   child++;
  h->p[father] = h->p[child];
  do { if (h->ofs > 0) *((int32_t *)((char *)(h->p[father].object) + h->ofs)) = father; } while (0);
  father = child;
 }
 h->elements--;
 if (father != max) {
  h->p[father] = h->p[max];
  heap_insert(h, father, ((void *)0));
 }
}

canonical_original_code:
void heap_extract(struct dn_heap *h, void *obj)
{
 int child; int father; int max = h->elements - 1;
 if (max < 0) {
  printf("--- %s: empty heap 0x%p\n", __FUNCTION__, h);
  return;
 }
 if (obj == ((void *)0))
  father = 0;
 else {
  if (h->ofs <= 0)
   panic("%s: extract from middle not set on %p\n",
    __FUNCTION__, h);
  father = *((int *)((char *)obj + h->ofs));
  if (father < 0 || father >= h->elements) {
   panic("%s: father %d out of bound 0..%d\n",
    __FUNCTION__, father, h->elements);
  }
 }
 do { if (h->ofs > 0) *((int *)((char *)(h->p[father].object) + h->ofs)) = -16; } while (0);
 while ( (child = ( (father)+(father) + 1 )) <= max ) {
  if (child != max &&
      ((long)((h->p[child+1].key)-(h->p[child].key)) < 0) )
   child++;
  h->p[father] = h->p[child];
  do { if (h->ofs > 0) *((int *)((char *)(h->p[father].object) + h->ofs)) = father; } while (0);
  father = child;
 }
 h->elements--;
 if (father != max) {
  h->p[father] = h->p[max];
  heap_insert(h, father, ((void *)0));
 }
}

variable_types: child int

variable_types: max int


original_code:
int
heap_scan(struct dn_heap *h, int (*fn)(void *, uintptr_t),
 uintptr_t arg)
{
 int i, ret, found;
 for (i = found = 0 ; i < h->elements ;) {
  ret = fn(h->p[i].object, arg);
  if (ret & HEAP_SCAN_DEL) {
   h->elements-- ;
   h->p[i] = h->p[h->elements] ;
   found++ ;
  } else
   i++ ;
  if (ret & HEAP_SCAN_END)
   break;
 }
 if (found)
  heapify(h);
 return found;
}

canonical_original_code:
int heap_scan(struct dn_heap *h, int (*fn)(void *, unsigned long), unsigned long arg)
{
 int i; int ret; int found;
 for (i = found = 0 ; i < h->elements ;) {
  ret = fn(h->p[i].object, arg);
  if (ret & HEAP_SCAN_DEL) {
   h->elements-- ;
   h->p[i] = h->p[h->elements] ;
   found++ ;
  } else
   i++ ;
  if (ret & HEAP_SCAN_END)
   break;
 }
 if (found)
  heapify(h);
 return found;
}

variable_types:
fn
int (*)(void *, unsigned long)

variable_types:
arg
unsigned long

variable_types: ret int

variable_types: found int


decompiled_code:
__int64 __fastcall heap_free(__int64 a1)
{
__int64 result; // rax

if ( *(int *)a1 > 0 )
result = kern_free(*(_QWORD *)(a1 + 16));
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax

if ( *(int *)a1 > 0 )
result = kern_free(*(_QWORD *)(a1 + 16));
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

original_code:
void
heap_free(struct dn_heap *h)
{
 if (h->size >0 )
  kern_free(h->p);
 bzero(h, sizeof(*h) );
}


decompiled_code:
__int64 __fastcall dn_ht_entries(__int64 a1)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
result = *(unsigned int *)(a1 + 4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
result = *(unsigned int *)(a1 + 4);
return result;
}

original_code:
int
dn_ht_entries(struct dn_ht *ht)
{
 return ht ? ht->entries : 0;
}

variable_types: struct dn_ht { int buckets; int entries; int ofs; unsigned int (*hash)(unsigned long, int, void *arg); int (*match)(void *_el, unsigned long key, int, void *); void *(*newh)(unsigned long, int, void *); void **ht; } *


original_code:
void *
dn_ht_find(struct dn_ht *ht, uintptr_t key, int flags, void *arg)
{
 int i;
 void **pp, *p;
 if (ht == ((void *)0))
  return ((void *)0);
 i = (ht->buckets == 1) ? 0 :
  (ht->hash(key, flags, arg) & ht->buckets);
 for (pp = &ht->ht[i]; (p = *pp); pp = (void **)((char *)p + ht->ofs)) {
  if (flags & DNHT_MATCH_PTR) {
   if (key == (uintptr_t)p)
    break;
  } else if (ht->match(p, key, flags, arg))
   break;
 }
 if (p) {
  if (flags & DNHT_REMOVE) {
   *pp = *(void **)((char *)p + ht->ofs);
   *(void **)((char *)p + ht->ofs) = ((void *)0);
   ht->entries--;
  }
 } else if (flags & DNHT_INSERT) {
  p = ht->newh ? ht->newh(key, flags, arg) : (void *)key;
  if (p) {
   ht->entries++;
   *(void **)((char *)p + ht->ofs) = ht->ht[i];
   ht->ht[i] = p;
  }
 }
 return p;
}

canonical_original_code:
void *dn_ht_find(struct dn_ht *ht, unsigned long key, int flags, void *arg)
{
 int i;
 void **pp; void *p;
 if (ht == ((void *)0))
  return ((void *)0);
 i = (ht->buckets == 1) ? 0 :
  (ht->hash(key, flags, arg) & ht->buckets);
 for (pp = &ht->ht[i]; (p = *pp); pp = (void **)((char *)p + ht->ofs)) {
  if (flags & DNHT_MATCH_PTR) {
   if (key == (unsigned long)p)
    break;
  } else if (ht->match(p, key, flags, arg))
   break;
 }
 if (p) {
  if (flags & DNHT_REMOVE) {
   *pp = *(void **)((char *)p + ht->ofs);
   *(void **)((char *)p + ht->ofs) = ((void *)0);
   ht->entries--;
  }
 } else if (flags & DNHT_INSERT) {
  p = ht->newh ? ht->newh(key, flags, arg) : (void *)key;
  if (p) {
   ht->entries++;
   *(void **)((char *)p + ht->ofs) = ht->ht[i];
   ht->ht[i] = p;
  }
 }
 return p;
}

variable_types:
key
unsigned long

variable_types: flags int

variable_types:
pp
void **

return_type: void *

user_defined_types: struct dn_ht { int buckets; int entries; int ofs; unsigned int (*hash)(unsigned long, int, void *arg); int (*match)(void *_el, unsigned long key, int, void *); void *(*newh)(unsigned long, int, void *); void **ht; }


original_code:
int
dn_ht_scan(struct dn_ht *ht, int (*fn)(void *, void *), void *arg)
{
 int i, ret, found = 0;
 void **curp, *cur, *next;
 if (ht == ((void *)0) || fn == ((void *)0))
  return 0;
 for (i = 0; i <= ht->buckets; i++) {
  curp = &ht->ht[i];
  while ( (cur = *curp) != ((void *)0)) {
   next = *(void **)((char *)cur + ht->ofs);
   ret = fn(cur, arg);
   if (ret & DNHT_SCAN_DEL) {
    found++;
    ht->entries--;
    *curp = next;
   } else {
    curp = (void **)((char *)cur + ht->ofs);
   }
   if (ret & DNHT_SCAN_END)
    return found;
  }
 }
 return found;
}

canonical_original_code:
int dn_ht_scan(struct dn_ht *ht, int (*fn)(void *, void *), void *arg)
{
 int i; int ret; int found = 0;
 void **curp; void *cur; void *next;
 if (ht == ((void *)0) || fn == ((void *)0))
  return 0;
 for (i = 0; i <= ht->buckets; i++) {
  curp = &ht->ht[i];
  while ( (cur = *curp) != ((void *)0)) {
   next = *(void **)((char *)cur + ht->ofs);
   ret = fn(cur, arg);
   if (ret & DNHT_SCAN_DEL) {
    found++;
    ht->entries--;
    *curp = next;
   } else {
    curp = (void **)((char *)cur + ht->ofs);
   }
   if (ret & DNHT_SCAN_END)
    return found;
  }
 }
 return found;
}

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

variable_types:
arg
void *

variable_types:
curp
void **

variable_types:
cur
void *

variable_types:
next
void *


decompiled_code:
__int64 __fastcall dn_ht_free(__int64 a1, char a2)
{
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
if ( (a2 & 0x40) != 0 )
{
result = dn_ht_scan(a1, (__int64 (__fastcall *)(_QWORD, __int64))do_del, 0LL);
}
else
{
v3 = *(_QWORD *)(a1 + 40);
if ( v3 )
{
if ( v3 != a1 + 48 )
kern_free(v3);
}
result = kern_free(a1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, char a2)
{
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
if ( (a2 & 0x40) != 0 )
{
result = func11(a1, (__int64 (__fastcall *)(_QWORD, __int64))do_del, 0LL);
}
else
{
v3 = *(_QWORD *)(a1 + 40);
if ( v3 )
{
if ( v3 != a1 + 48 )
kern_free(v3);
}
result = kern_free(a1);
}
}
return result;
}

original_code:
void
dn_ht_free(struct dn_ht *ht, int flags)
{
 if (ht == ((void *)0))
  return;
 if (flags & DNHT_REMOVE) {
  (void)dn_ht_scan(ht, do_del, ((void *)0));
 } else {
  if (ht->ht && ht->ht != (void *)(ht + 1))
   kern_free(ht->ht);
  kern_free(ht);
 }
}


original_code:
int
dn_ht_scan_bucket(struct dn_ht *ht, int *bucket, int (*fn)(void *, void *),
   void *arg)
{
 int i, ret, found = 0;
 void **curp, *cur, *next;
 if (ht == ((void *)0) || fn == ((void *)0))
  return 0;
 if (*bucket > ht->buckets)
  *bucket = 0;
 i = *bucket;
 curp = &ht->ht[i];
 while ( (cur = *curp) != ((void *)0)) {
  next = *(void **)((char *)cur + ht->ofs);
  ret = fn(cur, arg);
  if (ret & DNHT_SCAN_DEL) {
   found++;
   ht->entries--;
   *curp = next;
  } else {
   curp = (void **)((char *)cur + ht->ofs);
  }
  if (ret & DNHT_SCAN_END)
   return found;
 }
 return found;
}

canonical_original_code:
int dn_ht_scan_bucket(struct dn_ht *ht, int *bucket, int (*fn)(void *, void *), void *arg)
{
 int i; int ret; int found = 0;
 void **curp; void *cur; void *next;
 if (ht == ((void *)0) || fn == ((void *)0))
  return 0;
 if (*bucket > ht->buckets)
  *bucket = 0;
 i = *bucket;
 curp = &ht->ht[i];
 while ( (cur = *curp) != ((void *)0)) {
  next = *(void **)((char *)cur + ht->ofs);
  ret = fn(cur, arg);
  if (ret & DNHT_SCAN_DEL) {
   found++;
   ht->entries--;
   *curp = next;
  } else {
   curp = (void **)((char *)cur + ht->ofs);
  }
  if (ret & DNHT_SCAN_END)
   return found;
 }
 return found;
}

variable_types:
bucket
int *


writen

decompiled_code:
__int64 __fastcall writen(int fd, void *buf, int a3)
{
int v4; // ebp
int v6; // ebx

v4 = a3;
if ( a3 <= 0 )
return 0LL;
do
{
v6 = write(fd, buf, v4);
if ( v6 < 0 && *_errno_location() != 11 )
return 0xFFFFFFFFLL;
buf = (char *)buf + v6;
v4 -= v6;
}
while ( v4 > 0 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, int a3)
{
int v4; // ebp
int v6; // ebx

v4 = a3;
if ( a3 <= 0 )
return 0LL;
do
{
v6 = write(fd, buf, v4);
if ( v6 < 0 && *_errno_location() != 11 )
return 0xFFFFFFFFLL;
buf = (char *)buf + v6;
v4 -= v6;
}
while ( v4 > 0 );
return 0LL;
}

original_code:
static int
writen(int fd, const char *buf, int len)
{
 int i;
 for (; len > 0; buf += i, len -= i) {
  i = write(fd, buf, len);
  do {} while (0);
  if (i < 0) {
   if ((*__errno_location ()) == 11)
    continue;
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
static int writen(int fd, char *buf, int len)
{
 int i;
 for (; len > 0; buf += i, len -= i) {
  i = write(fd, buf, len);
  do {} while (0);
  if (i < 0) {
   if ((*__errno_location ()) == 11)
    continue;
   return -1;
  }
 }
 return 0;
}

variable_types: fd int

variable_types:
buf
char *

variable_types: len int

variable_types: i int


readn

decompiled_code:
__int64 __fastcall readn(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // ebp

if ( a3 <= 0 )
return 0LL;
for ( i = 0; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( v5 < 0 && *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // ebp

if ( a3 <= 0 )
return 0LL;
for ( i = 0; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( v5 < 0 && *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
return 0LL;
}

original_code:
static int
readn(int fd, char *buf, int len)
{
 int i, pos;
 for (pos = 0; pos < len; pos += i) {
  i = read(fd, buf + pos, len - pos);
  do {} while (0);
  if (i < 0) {
   if ((*__errno_location ()) == 11)
    continue;
   return -1;
  }
 }
 do {} while (0);
 return 0;
}

canonical_original_code:
static int readn(int fd, char *buf, int len)
{
 int i; int pos;
 for (pos = 0; pos < len; pos += i) {
  i = read(fd, buf + pos, len - pos);
  do {} while (0);
  if (i < 0) {
   if ((*__errno_location ()) == 11)
    continue;
   return -1;
  }
 }
 do {} while (0);
 return 0;
}

variable_types: pos int


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

v0 = xyz("../extra/glue.c");
return fprintf(stderr, "%s:%-10s [%d] unsupported\n", v0, "ipfw_show_nat", 22LL);
}

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

v0 = func0("../extra/glue.c");
return fprintf(stderr, "%s:%-10s [%d] unsupported\n", v0, "ipfw_show_nat", 22LL);
}

original_code:
void
ipfw_show_nat(int ac, char **av)
{
        fprintf(stderr, "%s:%-10s [%d] " "unsupported" "\n", xyz("../extra/glue.c"), __FUNCTION__, 22);
}

variable_types: ac int

variable_types:
av
char **


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

v0 = xyz("../extra/glue.c");
return fprintf(stderr, "%s:%-10s [%d] unsupported\n", v0, "ipfw_config_nat", 28LL);
}

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

v0 = func0("../extra/glue.c");
return fprintf(stderr, "%s:%-10s [%d] unsupported\n", v0, "ipfw_config_nat", 28LL);
}

original_code:
void
ipfw_config_nat(int ac, char **av)
{
        fprintf(stderr, "%s:%-10s [%d] " "unsupported" "\n", xyz("../extra/glue.c"), __FUNCTION__, 28);
}


strtonum

decompiled_code:
__int64 __fastcall strtonum(char *nptr, __int64 a2, __int64 a3, char **a4)
{
int *v5; // rbx
int v6; // er13
__int64 result; // rax

v5 = _errno_location();
v6 = *v5;
*v5 = 0;
result = strtoll(nptr, a4, 0);
if ( *v5 || *a4 == nptr || **a4 )
{
*a4 = "invalid";
}
else
{
*a4 = 0LL;
*v5 = v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *nptr, __int64 a2, __int64 a3, char **a4)
{
int *v5; // rbx
int v6; // er13
__int64 result; // rax

v5 = _errno_location();
v6 = *v5;
*v5 = 0;
result = strtoll(nptr, a4, 0);
if ( *v5 || *a4 == nptr || **a4 )
{
*a4 = "invalid";
}
else
{
*a4 = 0LL;
*v5 = v6;
}
return result;
}

original_code:
long long int
strtonum(const char *nptr, long long minval, long long maxval,
         const char **errstr)
{
        long long ret;
        int errno_c = (*__errno_location ());
        (*__errno_location ()) = 0;
        ret = strtoll(nptr, (char **)errstr, 0);
        if ((*__errno_location ()) != 0 || nptr == *errstr || **errstr != '\0')
                *errstr = "invalid";
        else {
                *errstr = ((void *)0);
                (*__errno_location ()) = errno_c;
        }
        return ret;
}

canonical_original_code:
long long strtonum(char *nptr, long long minval, long long maxval, char **errstr)
{
        long long ret;
        int errno_c = (*__errno_location ());
        (*__errno_location ()) = 0;
        ret = strtoll(nptr, (char **)errstr, 0);
        if ((*__errno_location ()) != 0 || nptr == *errstr || **errstr != '\0')
                *errstr = "invalid";
        else {
                *errstr = ((void *)0);
                (*__errno_location ()) = errno_c;
        }
        return ret;
}

variable_types:
nptr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstr
char **

variable_types:
ret
long long

return_type: long long


ishexnumber

decompiled_code:
_BOOL8 __fastcall ishexnumber(int a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( (unsigned int)(a1 - 48) > 9 )
result = (a1 & 0xFFFFFFDF) - 65 <= 5;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(int a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( (unsigned int)(a1 - 48) > 9 )
result = (a1 & 0xFFFFFFDF) - 65 <= 5;
return result;
}

variable_types: c int


strlcpy

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
        char *d = dst;
        const char *s = src;
        size_t n = siz;
        if (n != 0 && --n != 0) {
                do {
                        if ((*d++ = *s++) == 0)
                                break;
                } while (--n != 0);
        }
        if (n == 0) {
                if (siz != 0)
                        *d = '\0';
                while (*s++)
                        ;
        }
        return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
        char *d = dst;
        char *s = src;
        unsigned long n = siz;
        if (n != 0 && --n != 0) {
                do {
                        if ((*d++ = *s++) == 0)
                                break;
                } while (--n != 0);
        }
        if (n == 0) {
                if (siz != 0)
                        *d = '\0';
                while (*s++)
                        ;
        }
        return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall getsockopt2(int a1, unsigned int a2, unsigned int a3, void *a4, unsigned int *a5)
{
return _sockopt2(a1, a2, a3, a4, a5, 0);
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, unsigned int a2, unsigned int a3, void *a4, unsigned int *a5)
{
return func11(a1, a2, a3, a4, a5, 0);
}

original_code:
int
getsockopt2(int s, int level, int optname, void *optval,
 socklen_t *optlen)
{
 return __sockopt2(s, level, optname, optval, optlen, SOPT_GET);
}

canonical_original_code:
int getsockopt2(int s, int level, int optname, void *optval, unsigned int *optlen)
{
 return __sockopt2(s, level, optname, optval, optlen, SOPT_GET);
}

variable_types: s int

variable_types: level int

variable_types: optname int

variable_types:
optval
void *

variable_types:
optlen
unsigned int *


decompiled_code:
__int64 __fastcall setsockopt2(int a1, unsigned int a2, unsigned int a3, void *a4, unsigned int a5)
{
unsigned int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a5;
return _sockopt2(a1, a2, a3, a4, v6, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1, unsigned int a2, unsigned int a3, void *a4, unsigned int a5)
{
unsigned int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a5;
return func11(a1, a2, a3, a4, v6, 1u);
}

original_code:
int
setsockopt2(int s, int level, int optname, void *optval,
 socklen_t optlen)
{
 return __sockopt2(s, level, optname, optval, &optlen, SOPT_SET);
}

canonical_original_code:
int setsockopt2(int s, int level, int optname, void *optval, unsigned int optlen)
{
 return __sockopt2(s, level, optname, optval, &optlen, SOPT_SET);
}

variable_types:
optlen
unsigned int


decompiled_code:
__int64 __fastcall do_connect(char *cp, __int16 a2)
{
int v4; // eax
unsigned int v5; // ebp
char *v6; // rdi
char *v7; // rdi
int v8; // eax
unsigned __int16 v9; // bx
char *v10; // rax
struct sockaddr inp[2]; // [rsp+0h] [rbp-28h] BYREF

v4 = socket(2, 1, 0);
if ( v4 < 0 )
{
perror("socket");
v5 = -1;
}
else
{
v5 = v4;
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
v6 = getenv("IPFW_HOST");
if ( !v6 )
v6 = cp;
inet_aton(v6, (struct in_addr *)&inp[0].sa_data[2]);
v7 = getenv("IPFW_PORT");
if ( v7 )
{
v8 = atoi(v7);
if ( v8 > 0 )
a2 = v8;
}
*(_WORD *)inp[0].sa_data = _bswap_16(a2);
if ( connect(v5, inp, 0x10u) < 0 )
{
perror("connect");
v5 = -1;
}
else
{
v9 = _bswap_16(*(__int16 *)inp[0].sa_data);
v10 = inet_ntoa(*(struct in_addr *)&inp[0].sa_data[2]);
fprintf(stderr, "connected to %s:%d\n", v10, v9);
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *cp, __int16 a2)
{
int v4; // eax
unsigned int v5; // ebp
char *v6; // rdi
char *v7; // rdi
int v8; // eax
unsigned __int16 v9; // bx
char *v10; // rax
struct sockaddr inp[2]; // [rsp+0h] [rbp-28h] BYREF

v4 = socket(2, 1, 0);
if ( v4 < 0 )
{
perror("socket");
v5 = -1;
}
else
{
v5 = v4;
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
v6 = getenv("IPFW_HOST");
if ( !v6 )
v6 = cp;
inet_aton(v6, (struct in_addr *)&inp[0].sa_data[2]);
v7 = getenv("IPFW_PORT");
if ( v7 )
{
v8 = atoi(v7);
if ( v8 > 0 )
a2 = v8;
}
*(_WORD *)inp[0].sa_data = func4(a2);
if ( connect(v5, inp, 0x10u) < 0 )
{
perror("connect");
v5 = -1;
}
else
{
v9 = func4(*(__int16 *)inp[0].sa_data);
v10 = inet_ntoa(*(struct in_addr *)&inp[0].sa_data[2]);
fprintf(stderr, "connected to %s:%d\n", v10, v9);
}
}
return v5;
}

original_code:
int
do_connect(const char *addr, int port)
{
 int conn_fd;
        struct sockaddr_in server;
 const char *s;
        conn_fd = socket(2, SOCK_STREAM, 0);
        if (conn_fd < 0) {
  perror("socket");
  return -1;
 }
 bzero(&server, sizeof(server));
 server.sin_family = 2;
 s = getenv("IPFW_HOST");
 if (s)
  addr = s;
 inet_aton(addr, &server.sin_addr);
 s = getenv("IPFW_PORT");
 if (s && atoi(s) > 0)
  port = atoi(s);
 server.sin_port = htons(port);
        if (connect(conn_fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
  perror("connect");
  return -1;
 }
 if (1)
  fprintf(stderr, "connected to %s:%d\n",
   inet_ntoa(server.sin_addr), ntohs(server.sin_port));
 return conn_fd;
}

canonical_original_code:
int do_connect(char *addr, int port)
{
 int conn_fd;
        struct sockaddr_in server;
 char *s;
        conn_fd = socket(2, SOCK_STREAM, 0);
        if (conn_fd < 0) {
  perror("socket");
  return -1;
 }
 bzero(&server, sizeof(server));
 server.sin_family = 2;
 s = getenv("IPFW_HOST");
 if (s)
  addr = s;
 inet_aton(addr, &server.sin_addr);
 s = getenv("IPFW_PORT");
 if (s && atoi(s) > 0)
  port = atoi(s);
 server.sin_port = htons(port);
        if (connect(conn_fd, (struct sockaddr *) &server, sizeof(server)) < 0) {
  perror("connect");
  return -1;
 }
 if (1)
  fprintf(stderr, "connected to %s:%d\n",
   inet_ntoa(server.sin_addr), ntohs(server.sin_port));
 return conn_fd;
}

variable_types:
addr
char *

variable_types: port int

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

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

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


original_code:
static int
ipfw_ctl_h(struct sockopt *s, int cmd, int dir, int len, void *user)
{
 struct thread t;
 int ret = 22;
 memset(s, 0, sizeof(s));
 s->sopt_name = cmd;
 s->sopt_dir = dir;
 s->sopt_valsize = len;
 s->sopt_val = user;
 memset(&t, 0, sizeof(t));
 s->sopt_td = &t;
 if (ip_fw_ctl_ptr && cmd != (100 + 9) && (cmd == (100 + 8) ||
     cmd < (100 + 20)))
  ret = ip_fw_ctl_ptr(s);
 else if (ip_dn_ctl_ptr && (cmd == (100 + 9) ||
     cmd >= (100 + 20)))
  ret = ip_dn_ctl_ptr(s);
 return -ret;
}

variable_types: struct sockopt { enum sopt_dir { SOPT_GET=0, SOPT_SET=1 } sopt_dir; int sopt_level; int sopt_name; void *sopt_val; unsigned long sopt_valsize; struct thread { void *sopt_td; void *td_ucred; } *sopt_td; } *

variable_types: cmd int

variable_types: dir int

variable_types: len int

variable_types:
user
void *

variable_types: ret int

user_defined_types: struct sockopt { enum sopt_dir sopt_dir; int sopt_level; int sopt_name; void *sopt_val; unsigned long sopt_valsize; struct thread *sopt_td; }


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

return ipfw_ctl_h((__int64)&v5, a2, 1, a4, a3);
}

original_code:
int
do_ipfw_set_ctl(void *sk, int cmd,
 void *user, unsigned int len)
{
 struct sockopt s;
 return ipfw_ctl_h(&s, cmd, SOPT_SET, len, user);
}

variable_types:
sk
void *

variable_types:
len
unsigned int

variable_types: struct sockopt { enum sopt_dir { SOPT_GET=0, SOPT_SET=1 } sopt_dir; int sopt_level; int sopt_name; void *sopt_val; unsigned long sopt_valsize; struct thread { void *sopt_td; void *td_ucred; } *sopt_td; }


decompiled_code:
__int64 __fastcall do_ipfw_get_ctl(__int64 a1, __int64 a2, __int64 a3, int *a4)
{
__int64 result; // rax
_DWORD v6[14]; // [rsp+0h] [rbp-38h] BYREF

result = ipfw_ctl_h((__int64)v6, a2, 0, *a4, a3);
*a4 = v6[6];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, int *a4)
{
__int64 result; // rax
_DWORD v6[14]; // [rsp+0h] [rbp-38h] BYREF

result = func0((__int64)v6, a2, 0, *a4, a3);
*a4 = v6[6];
return result;
}

original_code:
int
do_ipfw_get_ctl(void *sk,
 int cmd, void *user, int *len)
{
 struct sockopt s;
 int ret = ipfw_ctl_h(&s, cmd, SOPT_GET, *len, user);
 *len = s.sopt_valsize;
 return ret;
}

variable_types:
len
int *

user_defined_types: enum sopt_dir { SOPT_GET=0, SOPT_SET=1 }

user_defined_types: struct thread { void *sopt_td; void *td_ucred; }


decompiled_code:
__int64 ipfw_module_init()
{
rn_init(64LL);
my_mod_register((__int64)"ipfw", 1, moddesc_ipfw, 0LL, 0LL);
my_mod_register((__int64)"sy_ipfw", 2, 0LL, sysinit_ipfw_init, sysuninit_ipfw_destroy);
my_mod_register((__int64)"sy_Vnet_ipfw", 3, 0LL, sysinit_vnet_ipfw_init, sysuninit_vnet_ipfw_uninit);
my_mod_register((__int64)"dummynet", 4, moddesc_dummynet, 0LL, 0LL);
my_mod_register((__int64)"dn_fifo", 5, moddesc_dn_fifo, 0LL, 0LL);
my_mod_register((__int64)"dn_wf2qp", 6, moddesc_dn_wf2qp, 0LL, 0LL);
my_mod_register((__int64)"dn_rr", 7, moddesc_dn_rr, 0LL, 0LL);
my_mod_register((__int64)"dn_qfq", 8, moddesc_dn_qfq, 0LL, 0LL);
my_mod_register((__int64)"dn_prio", 9, moddesc_dn_prio, 0LL, 0LL);
init_children();
keinit_GST();
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
rn_init(64LL);
func5((__int64)"ipfw", 1, moddesc_ipfw, 0LL, 0LL);
func5((__int64)"sy_ipfw", 2, 0LL, sysinit_ipfw_init, sysuninit_ipfw_destroy);
func5((__int64)"sy_Vnet_ipfw", 3, 0LL, sysinit_vnet_ipfw_init, sysuninit_vnet_ipfw_uninit);
func5((__int64)"dummynet", 4, moddesc_dummynet, 0LL, 0LL);
func5((__int64)"dn_fifo", 5, moddesc_dn_fifo, 0LL, 0LL);
func5((__int64)"dn_wf2qp", 6, moddesc_dn_wf2qp, 0LL, 0LL);
func5((__int64)"dn_rr", 7, moddesc_dn_rr, 0LL, 0LL);
func5((__int64)"dn_qfq", 8, moddesc_dn_qfq, 0LL, 0LL);
func5((__int64)"dn_prio", 9, moddesc_dn_prio, 0LL, 0LL);
func1();
keinit_GST();
return 0LL;
}

original_code:
int
ipfw_module_init(void)
{
 int ret = 0;
 rn_init(64);
 my_mod_register("ipfw", 1, moddesc_ipfw, ((void *)0), ((void *)0));
 my_mod_register("sy_ipfw", 2, ((void *)0),
  sysinit_ipfw_init, sysuninit_ipfw_destroy);
 my_mod_register("sy_Vnet_ipfw", 3, ((void *)0),
  sysinit_vnet_ipfw_init, sysuninit_vnet_ipfw_uninit);
 my_mod_register("dummynet", 4, moddesc_dummynet, ((void *)0), ((void *)0));
 my_mod_register("dn_fifo", 5, moddesc_dn_fifo, ((void *)0), ((void *)0));
 my_mod_register("dn_wf2qp", 6, moddesc_dn_wf2qp, ((void *)0), ((void *)0));
 my_mod_register("dn_rr", 7, moddesc_dn_rr, ((void *)0), ((void *)0));
 my_mod_register("dn_qfq", 8, moddesc_dn_qfq, ((void *)0), ((void *)0));
 my_mod_register("dn_prio", 9, moddesc_dn_prio, ((void *)0), ((void *)0));
 init_children();
 keinit_GST();
 return ret;
}

canonical_original_code:
int ipfw_module_init()
{
 int ret = 0;
 rn_init(64);
 my_mod_register("ipfw", 1, moddesc_ipfw, ((void *)0), ((void *)0));
 my_mod_register("sy_ipfw", 2, ((void *)0),
  sysinit_ipfw_init, sysuninit_ipfw_destroy);
 my_mod_register("sy_Vnet_ipfw", 3, ((void *)0),
  sysinit_vnet_ipfw_init, sysuninit_vnet_ipfw_uninit);
 my_mod_register("dummynet", 4, moddesc_dummynet, ((void *)0), ((void *)0));
 my_mod_register("dn_fifo", 5, moddesc_dn_fifo, ((void *)0), ((void *)0));
 my_mod_register("dn_wf2qp", 6, moddesc_dn_wf2qp, ((void *)0), ((void *)0));
 my_mod_register("dn_rr", 7, moddesc_dn_rr, ((void *)0), ((void *)0));
 my_mod_register("dn_qfq", 8, moddesc_dn_qfq, ((void *)0), ((void *)0));
 my_mod_register("dn_prio", 9, moddesc_dn_prio, ((void *)0), ((void *)0));
 init_children();
 keinit_GST();
 return ret;
}


decompiled_code:
int ipfw_module_exit()
{
keexit_GST();
fini_children();
return printf("%s unloaded\n", "ipfw_module_exit");
}

canonical_decompiled_code:
int func7()
{
keexit_GST();
func2();
return printf("%s unloaded\n", "ipfw_module_exit");
}

original_code:
void
ipfw_module_exit(void)
{
 keexit_GST();
 fini_children();
 printf("%s unloaded\n", __FUNCTION__);
}

canonical_original_code:
void ipfw_module_exit()
{
 keexit_GST();
 fini_children();
 printf("%s unloaded\n", __FUNCTION__);
}


decompiled_code:
void ipfw_log_bpf()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void
ipfw_log_bpf(int onoff)
{
}

canonical_original_code:
void ipfw_log_bpf(int onoff)
{
}

variable_types: onoff int


decompiled_code:
__int64 __fastcall expand_number(const char *a1, uintmax_t *a2)
{
uintmax_t v2; // rax
char *v3; // r13
uintmax_t v4; // rbp
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v2 = strtoumax(a1, &endptr, 0);
v3 = endptr;
if ( endptr == a1 || (v4 = v2, (*_ctype_tolower_loc())[(unsigned __int8)*v3]) )
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
else
{
*a2 = v4;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, uintmax_t *a2)
{
uintmax_t v2; // rax
char *v3; // r13
uintmax_t v4; // rbp
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v2 = strtoumax(a1, &endptr, 0);
v3 = endptr;
if ( endptr == a1 || (v4 = v2, (*_ctype_tolower_loc())[(unsigned __int8)*v3]) )
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
else
{
*a2 = v4;
result = 0LL;
}
return result;
}

original_code:
int
expand_number(const char *buf, uint64_t *num)
{
 uint64_t number;
 unsigned shift;
 char *endptr;
 number = strtoumax(buf, &endptr, 0);
 if (endptr == buf) {
  (*__errno_location ()) = 22;
  return (-1);
 }
 switch (tolower((unsigned char)*endptr)) {
 case 'e':
  shift = 60;
  break;
 case 'p':
  shift = 50;
  break;
 case 't':
  shift = 40;
  break;
 case 'g':
  shift = 30;
  break;
 case 'm':
  shift = 20;
  break;
 case 'k':
  shift = 10;
  break;
 case 'b':
 case '\0':
  *num = number;
  return (0);
 default:
  (*__errno_location ()) = 22;
  return (-1);
 }
 if ((number << shift) >> shift != number) {
  (*__errno_location ()) = 34;
  return (-1);
 }
 *num = number << shift;
 return (0);
}

canonical_original_code:
int expand_number(char *buf, unsigned long *num)
{
 unsigned long number;
 unsigned int shift;
 char *endptr;
 number = strtoumax(buf, &endptr, 0);
 if (endptr == buf) {
  (*__errno_location ()) = 22;
  return (-1);
 }
 switch (tolower((unsigned char)*endptr)) {
 case 'e':
  shift = 60;
  break;
 case 'p':
  shift = 50;
  break;
 case 't':
  shift = 40;
  break;
 case 'g':
  shift = 30;
  break;
 case 'm':
  shift = 20;
  break;
 case 'k':
  shift = 10;
  break;
 case 'b':
 case '\0':
  *num = number;
  return (0);
 default:
  (*__errno_location ()) = 22;
  return (-1);
 }
 if ((number << shift) >> shift != number) {
  (*__errno_location ()) = 34;
  return (-1);
 }
 *num = number << shift;
 return (0);
}

variable_types:
buf
char *

variable_types:
num
unsigned long *

variable_types:
number
unsigned long

variable_types:
shift
unsigned int

variable_types:
endptr
char *


decompiled_code:
__int64 __fastcall humanize_number(char *a1, size_t a2, __int64 a3, const char *a4, int a5, char a6)
{
__int64 v11; // rax
__int64 v12; // rbp
const char *v13; // rdx
size_t v14; // rcx
int i; // esi
__int64 v16; // rdx
__int64 v17; // rdi
__int64 v18; // rbp
int v19; // er13
struct lconv *v20; // rax
const char *v21; // rdx
int v22; // eax
__int64 v23; // rdi
__int64 v25; // [rsp+8h] [rbp-60h]
const char *v27; // [rsp+18h] [rbp-50h]
int v28; // [rsp+24h] [rbp-44h]
const char *v29; // [rsp+28h] [rbp-40h]

if ( !a1 )
_assert_fail("buf != NULL", "../extra/humanize_number.c", 0x38u, "humanize_number");
if ( !a4 )
_assert_fail("suffix != NULL", "../extra/humanize_number.c", 0x39u, "humanize_number");
if ( a5 < 0 )
_assert_fail("scale >= 0", "../extra/humanize_number.c", 0x3Au, "humanize_number");
if ( a5 > 6 && (a5 & 0x30) == 0 )
_assert_fail(
"scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0))",
"../extra/humanize_number.c",
0x3Bu,
"humanize_number");
if ( (a6 & 0x18) == 24 )
_assert_fail(
"!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))",
"../extra/humanize_number.c",
0x3Cu,
"humanize_number");
if ( (a6 & 0x10) != 0 )
{
v11 = 2LL;
v12 = 1024LL;
v13 = (const char *)&unk_3E9;
if ( (a6 & 4) != 0 )
v13 = (const char *)&unk_3D4;
v29 = v13;
}
else
{
v12 = (a6 & 8) == 0 ? 1024LL : 1000LL;
v11 = 1LL;
v21 = (const char *)&unk_3FD;
if ( (a6 & 4) != 0 )
v21 = (const char *)&unk_3C0;
v29 = v21;
}
if ( a5 > 6 && (a5 & 0x30) == 0 )
return (unsigned int)-1;
if ( a2 )
*a1 = 0;
v25 = v11 + 1;
v28 = 1;
if ( a3 < 0 )
{
a3 = -a3;
v25 = v11 + 2;
v28 = -1;
}
v27 = (const char *)&unk_432;
if ( (a6 & 2) == 0 )
{
++v25;
v27 = " ";
}
v14 = strlen(a4) + v25;
if ( v14 + 1 > a2 )
return (unsigned int)-1;
i = a5 & 0x30;
if ( (a5 & 0x30) == 0 )
{
LODWORD(v16) = a5 & 0x30;
if ( a5 <= 0 )
return (unsigned int)snprintf(a1, a2, "%ld%s%s%s", v28 * (a3 + ((int)v16 + 50) / 1000), v27, &v29[3 * i], a4);
do
{
v17 = a3;
a3 /= v12;
++i;
}
while ( a5 > i && i <= 6 );
v16 = v17 % v12;
LABEL_25:
if ( (int)v16 <= 949 && a3 <= 9 && i > 0 && (a6 & 1) != 0 )
{
if ( v14 + 3 > a2 )
{
i = -1;
}
else
{
v18 = (int)v16 + 50;
v19 = ((int)v16 + 50) >> 31;
v20 = localeconv();
i = snprintf(
a1,
a2,
"%d%s%d%s%s%s",
(unsigned int)(((int)v18 / 1000 + a3) * v28),
v20->decimal_point,
(unsigned int)((((int)((unsigned __int64)(1374389535 * v18) >> 32) >> 5) - v19) % 10),
v27,
&v29[3 * i],
a4);
}
return (unsigned int)i;
}
return (unsigned int)snprintf(a1, a2, "%ld%s%s%s", v28 * (a3 + ((int)v16 + 50) / 1000), v27, &v29[3 * i], a4);
}
v22 = a2 - v14 - 1;
if ( (int)a2 - (int)v14 <= 0 )
{
v23 = 1LL;
}
else
{
v23 = 1LL;
do
{
v23 *= 10LL;
--v22;
}
while ( v22 != -1 );
}
LODWORD(v16) = 0;
for ( i = 0; (a3 >= v23 || v23 - 1 == a3 && (int)v16 > 949) && i != 7; ++i )
{
v16 = a3 % v12;
a3 /= v12;
}
if ( (a5 & 0x10) == 0 )
goto LABEL_25;
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, size_t a2, __int64 a3, const char *a4, int a5, char a6)
{
__int64 v11; // rax
__int64 v12; // rbp
const char *v13; // rdx
size_t v14; // rcx
int i; // esi
__int64 v16; // rdx
__int64 v17; // rdi
__int64 v18; // rbp
int v19; // er13
struct lconv *v20; // rax
const char *v21; // rdx
int v22; // eax
__int64 v23; // rdi
__int64 v25; // [rsp+8h] [rbp-60h]
const char *v27; // [rsp+18h] [rbp-50h]
int v28; // [rsp+24h] [rbp-44h]
const char *v29; // [rsp+28h] [rbp-40h]

if ( !a1 )
_assert_fail("buf != NULL", "../extra/humanize_number.c", 0x38u, "humanize_number");
if ( !a4 )
_assert_fail("suffix != NULL", "../extra/humanize_number.c", 0x39u, "humanize_number");
if ( a5 < 0 )
_assert_fail("scale >= 0", "../extra/humanize_number.c", 0x3Au, "humanize_number");
if ( a5 > 6 && (a5 & 0x30) == 0 )
_assert_fail(
"scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0))",
"../extra/humanize_number.c",
0x3Bu,
"humanize_number");
if ( (a6 & 0x18) == 24 )
_assert_fail(
"!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))",
"../extra/humanize_number.c",
0x3Cu,
"humanize_number");
if ( (a6 & 0x10) != 0 )
{
v11 = 2LL;
v12 = 1024LL;
v13 = (const char *)&unk_3E9;
if ( (a6 & 4) != 0 )
v13 = (const char *)&unk_3D4;
v29 = v13;
}
else
{
v12 = (a6 & 8) == 0 ? 1024LL : 1000LL;
v11 = 1LL;
v21 = (const char *)&unk_3FD;
if ( (a6 & 4) != 0 )
v21 = (const char *)&unk_3C0;
v29 = v21;
}
if ( a5 > 6 && (a5 & 0x30) == 0 )
return (unsigned int)-1;
if ( a2 )
*a1 = 0;
v25 = v11 + 1;
v28 = 1;
if ( a3 < 0 )
{
a3 = -a3;
v25 = v11 + 2;
v28 = -1;
}
v27 = (const char *)&unk_432;
if ( (a6 & 2) == 0 )
{
++v25;
v27 = " ";
}
v14 = strlen(a4) + v25;
if ( v14 + 1 > a2 )
return (unsigned int)-1;
i = a5 & 0x30;
if ( (a5 & 0x30) == 0 )
{
LODWORD(v16) = a5 & 0x30;
if ( a5 <= 0 )
return (unsigned int)snprintf(a1, a2, "%ld%s%s%s", v28 * (a3 + ((int)v16 + 50) / 1000), v27, &v29[3 * i], a4);
do
{
v17 = a3;
a3 /= v12;
++i;
}
while ( a5 > i && i <= 6 );
v16 = v17 % v12;
LABEL_25:
if ( (int)v16 <= 949 && a3 <= 9 && i > 0 && (a6 & 1) != 0 )
{
if ( v14 + 3 > a2 )
{
i = -1;
}
else
{
v18 = (int)v16 + 50;
v19 = ((int)v16 + 50) >> 31;
v20 = localeconv();
i = snprintf(
a1,
a2,
"%d%s%d%s%s%s",
(unsigned int)(((int)v18 / 1000 + a3) * v28),
v20->decimal_point,
(unsigned int)((((int)((unsigned __int64)(1374389535 * v18) >> 32) >> 5) - v19) % 10),
v27,
&v29[3 * i],
a4);
}
return (unsigned int)i;
}
return (unsigned int)snprintf(a1, a2, "%ld%s%s%s", v28 * (a3 + ((int)v16 + 50) / 1000), v27, &v29[3 * i], a4);
}
v22 = a2 - v14 - 1;
if ( (int)a2 - (int)v14 <= 0 )
{
v23 = 1LL;
}
else
{
v23 = 1LL;
do
{
v23 *= 10LL;
--v22;
}
while ( v22 != -1 );
}
LODWORD(v16) = 0;
for ( i = 0; (a3 >= v23 || v23 - 1 == a3 && (int)v16 > 949) && i != 7; ++i )
{
v16 = a3 % v12;
a3 /= v12;
}
if ( (a5 & 0x10) == 0 )
goto LABEL_25;
return (unsigned int)i;
}

original_code:
int
humanize_number(char *buf, size_t len, int64_t quotient,
    const char *suffix, int scale, int flags)
{
 const char *prefixes, *sep;
 int i, r, remainder, s1, s2, sign;
 int64_t divisor, max;
 size_t baselen;
 ((void) sizeof ((buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (buf != ((void *)0)) ; else __assert_fail ("buf != NULL", "../extra/humanize_number.c", 56, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((suffix != ((void *)0)) ? 1 : 0), __extension__ ({ if (suffix != ((void *)0)) ; else __assert_fail ("suffix != NULL", "../extra/humanize_number.c", 57, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((scale >= 0) ? 1 : 0), __extension__ ({ if (scale >= 0) ; else __assert_fail ("scale >= 0", "../extra/humanize_number.c", 58, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((scale < maxscale || (((scale & (0x20 | 0x10)) != 0))) ? 1 : 0), __extension__ ({ if (scale < maxscale || (((scale & (0x20 | 0x10)) != 0))) ; else __assert_fail ("scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0))", "../extra/humanize_number.c", 59, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((!((flags & 0x08) && (flags & 0x10))) ? 1 : 0), __extension__ ({ if (!((flags & 0x08) && (flags & 0x10))) ; else __assert_fail ("!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))", "../extra/humanize_number.c", 60, __extension__ __PRETTY_FUNCTION__); }));
 remainder = 0;
 if (flags & 0x10) {
  baselen = 2;
  divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
  else
   prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
 } else {
  baselen = 1;
  if (flags & 0x08)
   divisor = 1000;
  else
   divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
  else
   prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
 }
 if (scale < 0 || (scale >= maxscale &&
     (scale & (0x20 | 0x10)) == 0))
  return (-1);
 if (buf == ((void *)0) || suffix == ((void *)0))
  return (-1);
 if (len > 0)
  buf[0] = '\0';
 if (quotient < 0) {
  sign = -1;
  quotient = -quotient;
  baselen += 2;
 } else {
  sign = 1;
  baselen += 1;
 }
 if (flags & 0x02)
  sep = "";
 else {
  sep = " ";
  baselen++;
 }
 baselen += strlen(suffix);
 if (len < baselen + 1)
  return (-1);
 if (scale & (0x20 | 0x10)) {
  for (max = 1, i = len - baselen; i-- > 0;)
   max *= 10;
  for (i = 0;
      (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
      i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
  if (scale & 0x10)
   return (i);
 } else {
  for (i = 0; i < scale && i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
 }
 if (quotient <= 9 && remainder < 950 && i > 0 && flags & 0x01) {
  if (len < baselen + 1 + 2)
   return (-1);
  s1 = (int)quotient + ((remainder + 50) / 1000);
  s2 = ((remainder + 50) / 100) % 10;
  r = snprintf(buf, len, "%d%s%d%s%s%s",
      sign * s1, localeconv()->decimal_point, s2,
      sep, (&prefixes[(i) * 3]), suffix);
 } else
  r = snprintf(buf, len, "%" "l" "d" "%s%s%s",
      sign * (quotient + (remainder + 50) / 1000),
      sep, (&prefixes[(i) * 3]), suffix);
 return (r);
}

canonical_original_code:
int humanize_number(char *buf, unsigned long len, long quotient, char *suffix, int scale, int flags)
{
 char *prefixes; char *sep;
 int i; int r; int remainder; int s1; int s2; int sign;
 long divisor; long max;
 unsigned long baselen;
 ((void) sizeof ((buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (buf != ((void *)0)) ; else __assert_fail ("buf != NULL", "../extra/humanize_number.c", 56, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((suffix != ((void *)0)) ? 1 : 0), __extension__ ({ if (suffix != ((void *)0)) ; else __assert_fail ("suffix != NULL", "../extra/humanize_number.c", 57, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((scale >= 0) ? 1 : 0), __extension__ ({ if (scale >= 0) ; else __assert_fail ("scale >= 0", "../extra/humanize_number.c", 58, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((scale < maxscale || (((scale & (0x20 | 0x10)) != 0))) ? 1 : 0), __extension__ ({ if (scale < maxscale || (((scale & (0x20 | 0x10)) != 0))) ; else __assert_fail ("scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0))", "../extra/humanize_number.c", 59, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((!((flags & 0x08) && (flags & 0x10))) ? 1 : 0), __extension__ ({ if (!((flags & 0x08) && (flags & 0x10))) ; else __assert_fail ("!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))", "../extra/humanize_number.c", 60, __extension__ __PRETTY_FUNCTION__); }));
 remainder = 0;
 if (flags & 0x10) {
  baselen = 2;
  divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
  else
   prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
 } else {
  baselen = 1;
  if (flags & 0x08)
   divisor = 1000;
  else
   divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
  else
   prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
 }
 if (scale < 0 || (scale >= maxscale &&
     (scale & (0x20 | 0x10)) == 0))
  return (-1);
 if (buf == ((void *)0) || suffix == ((void *)0))
  return (-1);
 if (len > 0)
  buf[0] = '\0';
 if (quotient < 0) {
  sign = -1;
  quotient = -quotient;
  baselen += 2;
 } else {
  sign = 1;
  baselen += 1;
 }
 if (flags & 0x02)
  sep = "";
 else {
  sep = " ";
  baselen++;
 }
 baselen += strlen(suffix);
 if (len < baselen + 1)
  return (-1);
 if (scale & (0x20 | 0x10)) {
  for (max = 1, i = len - baselen; i-- > 0;)
   max *= 10;
  for (i = 0;
      (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
      i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
  if (scale & 0x10)
   return (i);
 } else {
  for (i = 0; i < scale && i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
 }
 if (quotient <= 9 && remainder < 950 && i > 0 && flags & 0x01) {
  if (len < baselen + 1 + 2)
   return (-1);
  s1 = (int)quotient + ((remainder + 50) / 1000);
  s2 = ((remainder + 50) / 100) % 10;
  r = snprintf(buf, len, "%d%s%d%s%s%s",
      sign * s1, localeconv()->decimal_point, s2,
      sep, (&prefixes[(i) * 3]), suffix);
 } else
  r = snprintf(buf, len, "%" "l" "d" "%s%s%s",
      sign * (quotient + (remainder + 50) / 1000),
      sep, (&prefixes[(i) * 3]), suffix);
 return (r);
}

variable_types:
len
unsigned long

variable_types: quotient long

variable_types:
suffix
char *

variable_types: scale int

variable_types: flags int

variable_types:
prefixes
char *

variable_types:
sep
char *

variable_types: i int

variable_types: r int

variable_types: remainder int

variable_types: sign int

variable_types: divisor long

variable_types: max long

variable_types:
baselen
unsigned long


decompiled_code:
__int64 sysctl_addgroup_f1()
{
return sysctl_pushback("net_inet_ip_fw.enable", 258LL, 4LL, ipfw_chg_hook);
}

canonical_decompiled_code:
__int64 func11()
{
return sysctl_pushback("net_inet_ip_fw.enable", 258LL, 4LL, ipfw_chg_hook);
}

original_code:
void sysctl_addgroup_f1() {
;
sysctl_pushback("_net_inet_ip_fw" "." "enable" + 1, (CTLTYPE_INT | 2 | 0), 4 , ipfw_chg_hook);
}


repo: polyphemus/libspacemouse

decompiled_code:
__int64 __fastcall spacemouse_device_list_get_next(__int64 a1)
{
return *(_QWORD *)(a1 + 88);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(_QWORD *)(a1 + 88);
}

original_code:
struct spacemouse *spacemouse_device_list_get_next(struct spacemouse *mouse) {
  return mouse->next;
}

variable_types:
mouse
struct spacemouse *

return_type: struct spacemouse { int id; int fd; void *data; char *devnode; char *manufacturer; char *product; struct spacemouse_buf { struct spacemouse_event_motion { int type; int x; int rx; unsigned int period; } motion; struct timeval { long tv_sec; long tv_usec; } time; } buf; struct spacemouse *next; } *

user_defined_types: struct spacemouse { int id; int fd; void *data; char *devnode; char *manufacturer; char *product; struct spacemouse_buf buf; struct spacemouse *next; }

user_defined_types: struct spacemouse_buf { struct spacemouse_event_motion motion; struct timeval time; }


decompiled_code:
__int64 __fastcall spacemouse_device_get_id(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
return *a1;
}

original_code:
int spacemouse_device_get_id(struct spacemouse *mouse) {
  return mouse->id;
}

variable_types: struct spacemouse { int id; int fd; void *data; char *devnode; char *manufacturer; char *product; struct spacemouse_buf { struct spacemouse_event_motion { int type; int x; int rx; unsigned int period; } motion; struct timeval { long tv_sec; long tv_usec; } time; } buf; struct spacemouse *next; } *


decompiled_code:
__int64 __fastcall spacemouse_device_get_fd(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int spacemouse_device_get_fd(struct spacemouse *mouse) {
  return mouse->fd;
}

user_defined_types: struct spacemouse_event_motion { int type; int x; int rx; unsigned int period; }

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


decompiled_code:
__int64 __fastcall spacemouse_device_get_devnode(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
char const * const spacemouse_device_get_devnode(struct spacemouse *mouse) {
  return mouse->devnode;
}

canonical_original_code:
char *spacemouse_device_get_devnode(struct spacemouse *mouse) {
  return mouse->devnode;
}


decompiled_code:
__int64 __fastcall spacemouse_device_get_manufacturer(__int64 a1)
{
return *(_QWORD *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(_QWORD *)(a1 + 24);
}

original_code:
char const * const spacemouse_device_get_manufacturer(
    struct spacemouse *mouse) {
  return mouse->manufacturer;
}

canonical_original_code:
char *spacemouse_device_get_manufacturer(struct spacemouse *mouse) {
  return mouse->manufacturer;
}


decompiled_code:
__int64 __fastcall spacemouse_device_get_product(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

original_code:
char const * const spacemouse_device_get_product(struct spacemouse *mouse) {
  return mouse->product;
}

canonical_original_code:
char *spacemouse_device_get_product(struct spacemouse *mouse) {
  return mouse->product;
}


decompiled_code:
void __fastcall spacemouse_device_set_data(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
}

original_code:
void spacemouse_device_set_data(struct spacemouse *mouse, void *data) {
  mouse->data = data;
}

variable_types:
data
void *


decompiled_code:
__int64 __fastcall spacemouse_device_get_data(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
void *spacemouse_device_get_data(struct spacemouse *mouse) {
  return mouse->data;
}


decompiled_code:
__int64 __fastcall devnode_used_in_list(char *s2)
{
__int64 i; // rbx

for ( i = spacemouse_head; i; i = *(_QWORD *)(i + 88) )
{
if ( !strcmp(*(const char **)(i + 16), s2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2)
{
__int64 i; // rbx

for ( i = spacemouse_head; i; i = *(_QWORD *)(i + 88) )
{
if ( !strcmp(*(const char **)(i + 16), s2) )
break;
}
return i;
}

original_code:
static struct spacemouse *devnode_used_in_list(char const *devnode)
{
  struct spacemouse *iter = spacemouse_head;
  for ( ; iter; iter = iter->next)
    if (strcmp(iter->devnode, devnode) == 0)
      return iter;
  return ((void *)0);
}

canonical_original_code:
static struct spacemouse *devnode_used_in_list(char *devnode)
{
  struct spacemouse *iter = spacemouse_head;
  for ( ; iter; iter = iter->next)
    if (strcmp(iter->devnode, devnode) == 0)
      return iter;
  return ((void *)0);
}

variable_types:
devnode
char *


decompiled_code:
__int64 spacemouse_monitor_open()
{
int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // eax

if ( !udev_monitor )
{
if ( !udev )
udev = udev_new();
udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", 0LL);
udev_monitor_enable_receiving(udev_monitor);
}
v0 = udev_monitor_get_fd(udev_monitor);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-*_errno_location();
v2 = fcntl(v0, 3);
BYTE1(v2) &= 0xF7u;
fcntl(v1, 4, v2);
return v1;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // eax

if ( !udev_monitor )
{
if ( !udev )
udev = udev_new();
udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", 0LL);
udev_monitor_enable_receiving(udev_monitor);
}
v0 = udev_monitor_get_fd(udev_monitor);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-*_errno_location();
v2 = fcntl(v0, 3);
BYTE1(v2) &= 0xF7u;
fcntl(v1, 4, v2);
return v1;
}

original_code:
int spacemouse_monitor_open(void)
{
  int fd;
  if (!udev_monitor) {
    if (!udev)
      udev = udev_new();
    udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input",
                                                    ((void *)0));
    udev_monitor_enable_receiving(udev_monitor);
  }
  fd = udev_monitor_get_fd(udev_monitor);
  if (fd < 0)
    return -(*__errno_location ());
  fcntl(fd, 4, fcntl(fd, 3) & ~04000);
  return fd;
}

canonical_original_code:
int spacemouse_monitor_open()
{
  int fd;
  if (!udev_monitor) {
    if (!udev)
      udev = udev_new();
    udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input",
                                                    ((void *)0));
    udev_monitor_enable_receiving(udev_monitor);
  }
  fd = udev_monitor_get_fd(udev_monitor);
  if (fd < 0)
    return -(*__errno_location ());
  fcntl(fd, 4, fcntl(fd, 3) & ~04000);
  return fd;
}

variable_types: fd int


decompiled_code:
__int64 spacemouse_monitor_close()
{
if ( udev_monitor )
udev_monitor_unref();
if ( udev )
udev_unref(udev);
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
if ( udev_monitor )
udev_monitor_unref();
if ( udev )
udev_unref(udev);
return 0LL;
}

original_code:
int spacemouse_monitor_close(void)
{
  if (udev_monitor)
    udev_monitor_unref(udev_monitor);
  if (udev)
    udev_unref(udev);
  return 0;
}

canonical_original_code:
int spacemouse_monitor_close()
{
  if (udev_monitor)
    udev_monitor_unref(udev_monitor);
  if (udev)
    udev_unref(udev);
  return 0;
}


decompiled_code:
int __fastcall spacemouse_device_open(__int64 a1)
{
int result; // eax

result = open(*(const char **)(a1 + 16), 2);
if ( result == -1 )
{
result = open(*(const char **)(a1 + 16), 0);
if ( result == -1 )
return -*_errno_location();
}
*(_DWORD *)(a1 + 4) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int result; // eax

result = open(*(const char **)(a1 + 16), 2);
if ( result == -1 )
{
result = open(*(const char **)(a1 + 16), 0);
if ( result == -1 )
return -*_errno_location();
}
*(_DWORD *)(a1 + 4) = result;
return result;
}

original_code:
int spacemouse_device_open(struct spacemouse *mouse)
{
  int fd;
  if ((fd = open(mouse->devnode, 02)) == -1) {
     if ((fd = open(mouse->devnode, 00)) == -1) {
      return -(*__errno_location ());
    }
  }
  return (mouse->fd = fd);
}


decompiled_code:
__int64 __fastcall spacemouse_device_set_grab(__int64 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // edx

result = 0LL;
if ( a2 <= 1 )
{
v3 = ioctl(*(_DWORD *)(a1 + 4), 0x40044590uLL, a2 != 0);
result = 0LL;
if ( v3 == -1 )
result = (unsigned int)-*_errno_location();
}
return result;
}

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

result = 0LL;
if ( a2 <= 1 )
{
v3 = ioctl(*(_DWORD *)(a1 + 4), 0x40044590uLL, a2 != 0);
result = 0LL;
if ( v3 == -1 )
result = (unsigned int)-*_errno_location();
}
return result;
}

original_code:
int spacemouse_device_set_grab(struct spacemouse *mouse, int grab)
{
  if (grab == 0 || grab == 1)
    if (ioctl(mouse->fd, (((1U) << (((0 +8)+8)+14)) | ((('E')) << (0 +8)) | (((0x90)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), grab ? (void *)1 : (void *)0) == -1)
      return -(*__errno_location ());
    return 0;
  return -22;
}

variable_types: grab int