decompiled_code:
__int64 __fastcall bbus_str_join(const char *a1, const char *a2)
{
size_t v2; // rbx
size_t v3; // rax
size_t v4; // r13
size_t v5; // r14
__int64 v6; // rax
__int64 v7; // r12

v2 = strlen(a1);
v3 = strlen(a2);
v4 = v2 + v3;
v5 = v3;
v6 = bbus_realloc(a1, v2 + v3 + 1);
v7 = v6;
if ( v6 )
{
strncpy((char *)(v6 + v2), a2, v5);
*(_BYTE *)(v7 + v4) = 0;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
size_t v2; // rbx
size_t v3; // rax
size_t v4; // r13
size_t v5; // r14
__int64 v6; // rax
__int64 v7; // r12

v2 = strlen(a1);
v3 = strlen(a2);
v4 = v2 + v3;
v5 = v3;
v6 = bbus_realloc(a1, v2 + v3 + 1);
v7 = v6;
if ( v6 )
{
strncpy((char *)(v6 + v2), a2, v5);
*(_BYTE *)(v7 + v4) = 0;
}
return v7;
}

original_code:
char* bbus_str_join(char* dst, const char* src)
{
 size_t dsize, ssize;
 dsize = strlen(dst);
 ssize = strlen(src);
 dst = bbus_realloc(dst, dsize + ssize + 1);
 if (dst == ((void *)0))
  return ((void *)0);
 strncpy(dst+dsize, src, ssize);
 dst[dsize + ssize] = '\0';
 return dst;
}

canonical_original_code:
char *bbus_str_join(char *dst, char *src)
{
 unsigned long dsize; unsigned long ssize;
 dsize = strlen(dst);
 ssize = strlen(src);
 dst = bbus_realloc(dst, dsize + ssize + 1);
 if (dst == ((void *)0))
  return ((void *)0);
 strncpy(dst+dsize, src, ssize);
 dst[dsize + ssize] = '\0';
 return dst;
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
dsize
unsigned long

variable_types:
ssize
unsigned long


decompiled_code:
// attributes: thunk
__int64 __fastcall bbus_str_free(__int64 a1)
{
return bbus_free(a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return bbus_free(a1);
}

original_code:
void bbus_str_free(char* str)
{
 bbus_free(str);
}

canonical_original_code:
void bbus_str_free(char *str)
{
 bbus_free(str);
}

variable_types:
str
char *

return_type: void


decompiled_code:
__int64 __fastcall bbusd_insert_method(__int64 a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbp
unsigned int v4; // er12

v2 = (char *)bbus_str_cpy();
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = do_insert_method(v2, a2, (__int64 *)srvc_tree);
bbus_str_free(v3);
return v4;
}

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

v2 = (char *)bbus_str_cpy();
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = func1(v2, a2, (__int64 *)srvc_tree);
bbus_str_free(v3);
return v4;
}

original_code:
int bbusd_insert_method(const char* path, struct bbusd_method* mthd)
{
 char* mname;
 int ret;
 mname = bbus_str_cpy(path);
 if (mname == ((void *)0))
  return -1;
 ret = do_insert_method(mname, mthd, srvc_tree);
 bbus_str_free(mname);
 return ret;
}

canonical_original_code:
int bbusd_insert_method(char *path, struct bbusd_method *mthd)
{
 char *mname;
 int ret;
 mname = bbus_str_cpy(path);
 if (mname == ((void *)0))
  return -1;
 ret = do_insert_method(mname, mthd, srvc_tree);
 bbus_str_free(mname);
 return ret;
}

variable_types:
path
char *

variable_types: struct bbusd_method { int type; char data[]; } *

variable_types:
mname
char *

variable_types: ret int

return_type: int


decompiled_code:
__int64 bbusd_locate_method()
{
char *v0; // rax
char *v1; // rbp
__int64 v2; // r12

v0 = (char *)bbus_str_cpy();
if ( !v0 )
return 0LL;
v1 = v0;
v2 = do_locate_method(v0, (_QWORD *)srvc_tree);
bbus_str_free(v1);
return v2;
}

canonical_decompiled_code:
__int64 func3()
{
char *v0; // rax
char *v1; // rbp
__int64 v2; // r12

v0 = (char *)bbus_str_cpy();
if ( !v0 )
return 0LL;
v1 = v0;
v2 = func0(v0, (_QWORD *)srvc_tree);
bbus_str_free(v1);
return v2;
}

original_code:
struct bbusd_method* bbusd_locate_method(const char* path)
{
 char* mname;
 struct bbusd_method* ret;
 mname = bbus_str_cpy(path);
 if (mname == ((void *)0))
  return ((void *)0);
 ret = do_locate_method(mname, srvc_tree);
 bbus_str_free(mname);
 return ret;
}

canonical_original_code:
struct bbusd_method *bbusd_locate_method(char *path)
{
 char *mname;
 struct bbusd_method *ret;
 mname = bbus_str_cpy(path);
 if (mname == ((void *)0))
  return ((void *)0);
 ret = do_locate_method(mname, srvc_tree);
 bbus_str_free(mname);
 return ret;
}

return_type: struct bbusd_method { int type; char data[]; } *


decompiled_code:
__int64 bbusd_free_service_map()
{
bbus_hmap_free(*(_QWORD *)(srvc_tree + 8));
bbus_hmap_free(*(_QWORD *)srvc_tree);
return bbus_free(srvc_tree);
}

canonical_decompiled_code:
__int64 func5()
{
bbus_hmap_free(*(_QWORD *)(srvc_tree + 8));
bbus_hmap_free(*(_QWORD *)srvc_tree);
return bbus_free(srvc_tree);
}

original_code:
void bbusd_free_service_map(void)
{
 bbus_hmap_free(srvc_tree->methods);
 bbus_hmap_free(srvc_tree->subsrvc);
 bbus_free(srvc_tree);
}

canonical_original_code:
void bbusd_free_service_map()
{
 bbus_hmap_free(srvc_tree->methods);
 bbus_hmap_free(srvc_tree->subsrvc);
 bbus_free(srvc_tree);
}


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

switch ( a1 )
{
case 2:
*(_QWORD *)a2 = a3;
break;
case 3:
result = a2(a3);
break;
case 1:
*(_DWORD *)a2 = 1;
break;
}
return result;
}

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

switch ( a1 )
{
case 2:
*(_QWORD *)a2 = a3;
break;
case 3:
result = a2(a3);
break;
case 1:
*(_DWORD *)a2 = 1;
break;
}
return result;
}

original_code:
static void handle_option(enum bbus_opt_action action,
    void* actdata, char* arg)
{
 switch (action) {
 case BBUS_OPTACT_SETFLAG:
  *((int*)actdata) = 1;
  break;
 case BBUS_OPTACT_GETOPTARG:
  *((char**)actdata) = arg;
  break;
 case BBUS_OPTACT_CALLFUNC:
  ((bbus_opt_callback)actdata)(arg);
  break;
 case BBUS_OPTACT_NOTHING:
 default:
  break;
 }
}

canonical_original_code:
static void handle_option(enum bbus_opt_action action, void *actdata, char *arg)
{
 switch (action) {
 case BBUS_OPTACT_SETFLAG:
  *((int *)actdata) = 1;
  break;
 case BBUS_OPTACT_GETOPTARG:
  *((char **)actdata) = arg;
  break;
 case BBUS_OPTACT_CALLFUNC:
  ((void (*)(char *arg))actdata)(arg);
  break;
 case BBUS_OPTACT_NOTHING:
 default:
  break;
 }
}

variable_types: enum bbus_opt_action { BBUS_OPTACT_NOTHING=0, BBUS_OPTACT_SETFLAG=1, BBUS_OPTACT_GETOPTARG=2, BBUS_OPTACT_CALLFUNC=3 }

variable_types:
actdata
void *

variable_types:
arg
char *


decompiled_code:
__int64 __fastcall make_info_string(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbp
unsigned int v3; // er14
__int64 v4; // rax
__int64 v5; // r13
__int64 v6; // rax
unsigned int v7; // er12
__int64 v8; // rax
const char *v9; // rcx
const char *v10; // r8
__int64 v11; // rdx
const char *v12; // rsi
__int64 v13; // rax
__int64 v14; // r14
__int64 v15; // rax
const char *v16; // r9
int v17; // edx
const char *v18; // rax

result = bbus_str_build(
"%s %s\n\n%s\n\n",
*(const char **)(a1 + 32),
*(const char **)(a1 + 40),
*(const char **)(a1 + 48));
if ( !result )
return result;
if ( *(_QWORD *)(a1 + 8) )
{
v2 = bbus_str_join(result, "Options:\n");
if ( !v2 )
{
LABEL_11:
v2 = 0LL;
LABEL_12:
bbus_str_free(v2);
return 0LL;
}
if ( !*(_QWORD *)(a1 + 8) )
{
LABEL_25:
result = bbus_str_join(v2, "\n");
if ( !result )
{
v2 = 0LL;
goto LABEL_12;
}
goto LABEL_3;
}
v7 = 0;
v8 = 0LL;
while ( 1 )
{
v15 = *(_QWORD *)a1 + 40 * v8;
v16 = *(const char **)(v15 + 8);
v17 = *(_DWORD *)v15;
v18 = *(const char **)(v15 + 32);
if ( v16 )
{
v9 = (const char *)&LC2;
v10 = (const char *)&LC3;
if ( (_BYTE)v17 )
goto LABEL_17;
}
else
{
if ( (_BYTE)v17 )
{
v9 = (const char *)&LC0;
v10 = (const char *)&LC0;
v16 = (const char *)&LC1;
LABEL_17:
v11 = (unsigned int)(char)v17;
v12 = (const char *)&LC4;
goto LABEL_18;
}
v10 = (const char *)&LC0;
v16 = (const char *)&LC1;
}
v11 = 32LL;
v9 = (const char *)&LC0;
v12 = (const char *)&LC1;
LABEL_18:
v13 = bbus_str_build(" %s%c%s%s%s -\t%s\n", v12, v11, v9, v10, v16, v18);
v14 = v13;
if ( !v13 )
goto LABEL_12;
v2 = bbus_str_join(v2, v13);
bbus_str_free(v14);
if ( !v2 )
goto LABEL_11;
v8 = ++v7;
if ( (unsigned __int64)v7 >= *(_QWORD *)(a1 + 8) )
goto LABEL_25;
}
}
LABEL_3:
if ( *(_QWORD *)(a1 + 24) )
{
v2 = bbus_str_join(result, "Positional parameters:\n");
if ( !v2 )
goto LABEL_11;
v3 = 0;
while ( 1 )
{
v6 = v3;
if ( (unsigned __int64)v3 >= *(_QWORD *)(a1 + 24) )
break;
v4 = bbus_str_build(" #%d - %s\n", ++v3, *(const char **)(*(_QWORD *)(a1 + 16) + 24 * v6 + 16));
v5 = v4;
if ( !v4 )
goto LABEL_12;
v2 = bbus_str_join(v2, v4);
bbus_str_free(v5);
if ( !v2 )
goto LABEL_11;
}
result = bbus_str_join(v2, "\n");
if ( !result )
goto LABEL_11;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbp
unsigned int v3; // er14
__int64 v4; // rax
__int64 v5; // r13
__int64 v6; // rax
unsigned int v7; // er12
__int64 v8; // rax
const char *v9; // rcx
const char *v10; // r8
__int64 v11; // rdx
const char *v12; // rsi
__int64 v13; // rax
__int64 v14; // r14
__int64 v15; // rax
const char *v16; // r9
int v17; // edx
const char *v18; // rax

result = bbus_str_build(
"%s %s\n\n%s\n\n",
*(const char **)(a1 + 32),
*(const char **)(a1 + 40),
*(const char **)(a1 + 48));
if ( !result )
return result;
if ( *(_QWORD *)(a1 + 8) )
{
v2 = bbus_str_join(result, "Options:\n");
if ( !v2 )
{
LABEL_11:
v2 = 0LL;
LABEL_12:
bbus_str_free(v2);
return 0LL;
}
if ( !*(_QWORD *)(a1 + 8) )
{
LABEL_25:
result = bbus_str_join(v2, "\n");
if ( !result )
{
v2 = 0LL;
goto LABEL_12;
}
goto LABEL_3;
}
v7 = 0;
v8 = 0LL;
while ( 1 )
{
v15 = *(_QWORD *)a1 + 40 * v8;
v16 = *(const char **)(v15 + 8);
v17 = *(_DWORD *)v15;
v18 = *(const char **)(v15 + 32);
if ( v16 )
{
v9 = (const char *)&LC2;
v10 = (const char *)&LC3;
if ( (_BYTE)v17 )
goto LABEL_17;
}
else
{
if ( (_BYTE)v17 )
{
v9 = (const char *)&LC0;
v10 = (const char *)&LC0;
v16 = (const char *)&LC1;
LABEL_17:
v11 = (unsigned int)(char)v17;
v12 = (const char *)&LC4;
goto LABEL_18;
}
v10 = (const char *)&LC0;
v16 = (const char *)&LC1;
}
v11 = 32LL;
v9 = (const char *)&LC0;
v12 = (const char *)&LC1;
LABEL_18:
v13 = bbus_str_build(" %s%c%s%s%s -\t%s\n", v12, v11, v9, v10, v16, v18);
v14 = v13;
if ( !v13 )
goto LABEL_12;
v2 = bbus_str_join(v2, v13);
bbus_str_free(v14);
if ( !v2 )
goto LABEL_11;
v8 = ++v7;
if ( (unsigned __int64)v7 >= *(_QWORD *)(a1 + 8) )
goto LABEL_25;
}
}
LABEL_3:
if ( *(_QWORD *)(a1 + 24) )
{
v2 = bbus_str_join(result, "Positional parameters:\n");
if ( !v2 )
goto LABEL_11;
v3 = 0;
while ( 1 )
{
v6 = v3;
if ( (unsigned __int64)v3 >= *(_QWORD *)(a1 + 24) )
break;
v4 = bbus_str_build(" #%d - %s\n", ++v3, *(const char **)(*(_QWORD *)(a1 + 16) + 24 * v6 + 16));
v5 = v4;
if ( !v4 )
goto LABEL_12;
v2 = bbus_str_join(v2, v4);
bbus_str_free(v5);
if ( !v2 )
goto LABEL_11;
}
result = bbus_str_join(v2, "\n");
if ( !result )
goto LABEL_11;
}
return result;
}

original_code:
static char* make_info_string(const struct bbus_opt_list* optlist)
{
 char* info;
 char* tmp;
 unsigned i;
 char shortopt;
 const char* longopt;
 info = bbus_str_build("%s %s\n\n%s\n\n",
    optlist->progname,
    optlist->version,
    optlist->progdescr);
 if (info == ((void *)0))
  return ((void *)0);
 if (optlist->numopts > 0) {
  info = bbus_str_join(info, "Options:\n");
  if (info == ((void *)0))
   goto errout;
  for (i = 0; i < optlist->numopts; ++i) {
   shortopt = optlist->opts[i].shortopt;
   longopt = optlist->opts[i].longopt;
   tmp = bbus_str_build("  %s%c%s%s%s -\t%s\n",
    shortopt == 0 ? " " : "-",
    shortopt == 0 ? ' ' : shortopt,
    (shortopt != 0)
     && (longopt != ((void *)0)) ? ", " : "  ",
    longopt == ((void *)0) ? "  " : "--",
    longopt == ((void *)0) ? " " : longopt,
    optlist->opts[i].descr);
   if (tmp == ((void *)0))
    goto errout;
   info = bbus_str_join(info, tmp);
   bbus_str_free(tmp);
   if (info == ((void *)0))
    goto errout;
  }
  info = bbus_str_join(info, "\n");
  if (info == ((void *)0))
   goto errout;
 }
 if (optlist->numpargs > 0) {
  info = bbus_str_join(info, "Positional parameters:\n");
  if (info == ((void *)0))
   goto errout;
  for (i = 0; i < optlist->numpargs; ++i) {
   tmp = bbus_str_build("  #%d - %s\n",
      i+1, optlist->pargs[i].descr);
   if (tmp == ((void *)0))
    goto errout;
   info = bbus_str_join(info, tmp);
   bbus_str_free(tmp);
   if (info == ((void *)0))
    goto errout;
  }
  info = bbus_str_join(info, "\n");
  if (info == ((void *)0))
   goto errout;
 }
 return info;
errout:
 bbus_str_free(info);
 return ((void *)0);
}

canonical_original_code:
static char *make_info_string(struct bbus_opt_list *optlist)
{
 char *info;
 char *tmp;
 unsigned int i;
 char shortopt;
 char *longopt;
 info = bbus_str_build("%s %s\n\n%s\n\n",
    optlist->progname,
    optlist->version,
    optlist->progdescr);
 if (info == ((void *)0))
  return ((void *)0);
 if (optlist->numopts > 0) {
  info = bbus_str_join(info, "Options:\n");
  if (info == ((void *)0))
   goto errout;
  for (i = 0; i < optlist->numopts; ++i) {
   shortopt = optlist->opts[i].shortopt;
   longopt = optlist->opts[i].longopt;
   tmp = bbus_str_build("  %s%c%s%s%s -\t%s\n",
    shortopt == 0 ? " " : "-",
    shortopt == 0 ? ' ' : shortopt,
    (shortopt != 0)
     && (longopt != ((void *)0)) ? ", " : "  ",
    longopt == ((void *)0) ? "  " : "--",
    longopt == ((void *)0) ? " " : longopt,
    optlist->opts[i].descr);
   if (tmp == ((void *)0))
    goto errout;
   info = bbus_str_join(info, tmp);
   bbus_str_free(tmp);
   if (info == ((void *)0))
    goto errout;
  }
  info = bbus_str_join(info, "\n");
  if (info == ((void *)0))
   goto errout;
 }
 if (optlist->numpargs > 0) {
  info = bbus_str_join(info, "Positional parameters:\n");
  if (info == ((void *)0))
   goto errout;
  for (i = 0; i < optlist->numpargs; ++i) {
   tmp = bbus_str_build("  #%d - %s\n",
      i+1, optlist->pargs[i].descr);
   if (tmp == ((void *)0))
    goto errout;
   info = bbus_str_join(info, tmp);
   bbus_str_free(tmp);
   if (info == ((void *)0))
    goto errout;
  }
  info = bbus_str_join(info, "\n");
  if (info == ((void *)0))
   goto errout;
 }
 return info;
errout:
 bbus_str_free(info);
 return ((void *)0);
}

variable_types: struct bbus_opt_list { struct bbus_option { int shortopt; char *longopt; enum bbus_opt_hasarg { BBUS_OPT_NOARG=0, BBUS_OPT_ARGREQ=1, BBUS_OPT_ARGOPT=2 } hasarg; enum bbus_opt_action { BBUS_OPTACT_NOTHING=0, BBUS_OPTACT_SETFLAG=1, BBUS_OPTACT_GETOPTARG=2, BBUS_OPTACT_CALLFUNC=3 } action; void *actdata; char *descr; } *opts; unsigned long numopts; struct bbus_posarg { enum bbus_opt_action { BBUS_OPTACT_NOTHING=0, BBUS_OPTACT_SETFLAG=1, BBUS_OPTACT_GETOPTARG=2, BBUS_OPTACT_CALLFUNC=3 } action; void *actdata; char *descr; } *pargs; unsigned long numpargs; char *progname; char *version; char *progdescr; } *

variable_types:
info
char *

variable_types:
tmp
char *

variable_types:
i
unsigned int

variable_types: shortopt char

variable_types:
longopt
char *

return_type: char *

user_defined_types: struct bbus_opt_list { struct bbus_option *opts; unsigned long numopts; struct bbus_posarg *pargs; unsigned long numpargs; char *progname; char *version; char *progdescr; }

user_defined_types: struct bbus_option { int shortopt; char *longopt; enum bbus_opt_hasarg hasarg; enum bbus_opt_action action; void *actdata; char *descr; }

user_defined_types: struct bbus_posarg { enum bbus_opt_action action; void *actdata; char *descr; }


original_code:
static char* make_shortopts(const struct bbus_opt_list* optlist)
{
 unsigned i;
 size_t size = 0;
 char* ret;
 char* tmp;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].shortopt != 0) {
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    size += 2;
    break;
   case BBUS_OPT_ARGOPT:
    size += 3;
    break;
   case BBUS_OPT_NOARG:
   default:
    ++size;
    break;
   }
  }
 }
 ret = bbus_malloc0(size);
 if (ret == ((void *)0))
  return ((void *)0);
 tmp = ret;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].shortopt != 0) {
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    *tmp++ = (char)optlist->opts[i].shortopt;
    *tmp++ = ':';
    break;
   case BBUS_OPT_ARGOPT:
    *tmp++ = (char)optlist->opts[i].shortopt;
    *tmp++ = ':';
    *tmp++ = ':';
    break;
   case BBUS_OPT_NOARG:
   default:
    *tmp++ = (char)optlist->opts[i].shortopt;
    break;
   }
  }
 }
 return ret;
}

canonical_original_code:
static char *make_shortopts(struct bbus_opt_list *optlist)
{
 unsigned int i;
 unsigned long size = 0;
 char *ret;
 char *tmp;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].shortopt != 0) {
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    size += 2;
    break;
   case BBUS_OPT_ARGOPT:
    size += 3;
    break;
   case BBUS_OPT_NOARG:
   default:
    ++size;
    break;
   }
  }
 }
 ret = bbus_malloc0(size);
 if (ret == ((void *)0))
  return ((void *)0);
 tmp = ret;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].shortopt != 0) {
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    *tmp++ = (char)optlist->opts[i].shortopt;
    *tmp++ = ':';
    break;
   case BBUS_OPT_ARGOPT:
    *tmp++ = (char)optlist->opts[i].shortopt;
    *tmp++ = ':';
    *tmp++ = ':';
    break;
   case BBUS_OPT_NOARG:
   default:
    *tmp++ = (char)optlist->opts[i].shortopt;
    break;
   }
  }
 }
 return ret;
}

variable_types:
size
unsigned long

variable_types:
ret
char *

user_defined_types: enum bbus_opt_hasarg { BBUS_OPT_NOARG=0, BBUS_OPT_ARGREQ=1, BBUS_OPT_ARGOPT=2 }

user_defined_types: enum bbus_opt_action { BBUS_OPTACT_NOTHING=0, BBUS_OPTACT_SETFLAG=1, BBUS_OPTACT_GETOPTARG=2, BBUS_OPTACT_CALLFUNC=3 }


original_code:
static struct option* make_longopts(const struct bbus_opt_list* optlist,
        int* flag)
{
 struct option* ret;
 unsigned i;
 unsigned j = 2;
 size_t size = 2;
 for (i = 0; i < optlist->numopts; ++i)
  if (optlist->opts[i].longopt != ((void *)0))
   ++size;
 ret = bbus_malloc0((size+1) * sizeof(struct option));
 if (ret == ((void *)0))
  return ((void *)0);
 ret[0].name = "help";
 ret[0].has_arg = 0;
 ret[0].flag = flag;
 ret[0].val = -1;
 ret[1].name = "version";
 ret[1].has_arg = 0;
 ret[1].flag = flag;
 ret[1].val = -2;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].longopt != ((void *)0)) {
   ret[j].name = optlist->opts[i].longopt;
   ret[j].flag = flag;
   ret[j].val = i;
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    ret[j].has_arg = 1;
    break;
   case BBUS_OPT_ARGOPT:
    ret[j].has_arg = 2;
    break;
   case BBUS_OPT_NOARG:
   default:
    ret[j].has_arg = 0;
    break;
   }
   ++j;
  }
 }
 return ret;
}

canonical_original_code:
static struct option *make_longopts(struct bbus_opt_list *optlist, int *flag)
{
 struct option *ret;
 unsigned int i;
 unsigned int j = 2;
 unsigned long size = 2;
 for (i = 0; i < optlist->numopts; ++i)
  if (optlist->opts[i].longopt != ((void *)0))
   ++size;
 ret = bbus_malloc0((size+1) * sizeof(struct option));
 if (ret == ((void *)0))
  return ((void *)0);
 ret[0].name = "help";
 ret[0].has_arg = 0;
 ret[0].flag = flag;
 ret[0].val = -1;
 ret[1].name = "version";
 ret[1].has_arg = 0;
 ret[1].flag = flag;
 ret[1].val = -2;
 for (i = 0; i < optlist->numopts; ++i) {
  if (optlist->opts[i].longopt != ((void *)0)) {
   ret[j].name = optlist->opts[i].longopt;
   ret[j].flag = flag;
   ret[j].val = i;
   switch (optlist->opts[i].hasarg) {
   case BBUS_OPT_ARGREQ:
    ret[j].has_arg = 1;
    break;
   case BBUS_OPT_ARGOPT:
    ret[j].has_arg = 2;
    break;
   case BBUS_OPT_NOARG:
   default:
    ret[j].has_arg = 0;
    break;
   }
   ++j;
  }
 }
 return ret;
}

variable_types:
flag
int *

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

variable_types:
j
unsigned int


decompiled_code:
__int64 __fastcall bbus_free_nonopts(_QWORD *a1)
{
bbus_free(*a1);
return bbus_free(a1);
}

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

original_code:
void bbus_free_nonopts(struct bbus_nonopts* nonopts)
{
 bbus_free(nonopts->args);
 bbus_free(nonopts);
}

canonical_original_code:
void bbus_free_nonopts(struct bbus_nonopts *nonopts)
{
 bbus_free(nonopts->args);
 bbus_free(nonopts);
}

variable_types: struct bbus_nonopts { char **args; unsigned long numargs; } *


decompiled_code:
__int64 __fastcall bbus_parse_args(int a1, const char **a2, __int64 a3, __int64 **a4)
{
unsigned __int64 v6; // rdx
const char *v7; // r12
unsigned __int64 v8; // rdx
const char *v9; // rbp
const struct option *v10; // r14
__int64 i; // r15
unsigned __int64 v12; // rdx
int v13; // er8
unsigned __int64 v14; // rsi
unsigned int v15; // edx
__int64 v16; // rax
_DWORD *v17; // rax
const char *v18; // rax
int v19; // er8
int v21; // ecx
int v22; // eax
bool v23; // cf
__int64 *v24; // rax
int longind; // [rsp+8h] [rbp-60h]
unsigned int argc; // [rsp+18h] [rbp-50h]
int argca; // [rsp+18h] [rbp-50h]
int v29; // [rsp+1Ch] [rbp-4Ch]
int v30; // [rsp+28h] [rbp-40h] BYREF
int v31[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v30 = 0;
v7 = (const char *)make_info_string(a3);
if ( !v7 || (v9 = (const char *)make_shortopts((__int64 *)a3, (__int64)a2, v6)) == 0LL )
{
v10 = 0LL;
v9 = 0LL;
goto LABEL_17;
}
v10 = (const struct option *)make_longopts((__int64 *)a3, (__int64)v31, v8);
if ( !v10 )
{
LABEL_17:
v18 = (const char *)bbus_strerror(10001LL);
fprintf(stderr, "%s: %s\n", "bbus_parse_args", v18);
v19 = -1;
goto LABEL_18;
}
for ( i = 0LL; ; handle_option(*(_DWORD *)(i + 20), *(__int64 (__fastcall **)(__int64))(i + 24), (__int64)optarg) )
{
v13 = getopt_long(a1, (char *const *)a2, v9, v10, &v30);
if ( v13 == -1 )
break;
if ( v13 == 58 || v13 == 63 )
{
fprintf(stderr, "try %s --help\n", *a2);
v19 = -1;
goto LABEL_18;
}
if ( v13 )
{
v14 = *(_QWORD *)(a3 + 8);
if ( v14 )
{
v15 = 0;
v16 = 0LL;
do
{
v17 = (_DWORD *)(*(_QWORD *)a3 + 40 * v16);
if ( *v17 == v13 )
{
if ( *v17 )
i = (__int64)v17;
}
v16 = ++v15;
}
while ( v15 < v14 );
}
}
else
{
if ( v31[0] < 0 )
{
if ( v31[0] == -2 )
fprintf(stdout, "%s %s\n", *(const char **)(a3 + 32), *(const char **)(a3 + 40));
else
fputs(v7, stdout);
v19 = 1;
goto LABEL_18;
}
i = *(_QWORD *)a3 + 40LL * v31[0];
}
}
if ( !*(_QWORD *)(a3 + 24) )
{
LABEL_27:
v19 = 0;
if ( !a4 )
goto LABEL_18;
v24 = find_nonopts(a1, (__int64)a2, v12);
v19 = 0;
*a4 = v24;
if ( v24 )
goto LABEL_18;
goto LABEL_17;
}
v21 = 0;
v12 = 0LL;
v22 = optind;
while ( a1 > v22 )
{
v29 = v13;
longind = v21;
handle_option(
*(_DWORD *)(*(_QWORD *)(a3 + 16) + 24 * v12),
*(__int64 (__fastcall **)(__int64))(*(_QWORD *)(a3 + 16) + 24 * v12 + 8),
(__int64)a2[v22]);
v13 = v29;
v12 = (unsigned int)(longind + 1);
v21 = longind + 1;
v22 = optind + 1;
v23 = v12 < *(_QWORD *)(a3 + 24);
++optind;
if ( !v23 )
goto LABEL_27;
}
argca = v13;
fprintf(stderr, "%s: expected additional parameters\ntry %s --help\n", *a2, *a2);
v19 = argca;
LABEL_18:
argc = v19;
bbus_free(v9);
bbus_free(v10);
bbus_free(v7);
return argc;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, const char **a2, __int64 a3, __int64 **a4)
{
unsigned __int64 v6; // rdx
const char *v7; // r12
unsigned __int64 v8; // rdx
const char *v9; // rbp
const struct option *v10; // r14
__int64 i; // r15
unsigned __int64 v12; // rdx
int v13; // er8
unsigned __int64 v14; // rsi
unsigned int v15; // edx
__int64 v16; // rax
_DWORD *v17; // rax
const char *v18; // rax
int v19; // er8
int v21; // ecx
int v22; // eax
bool v23; // cf
__int64 *v24; // rax
int longind; // [rsp+8h] [rbp-60h]
unsigned int argc; // [rsp+18h] [rbp-50h]
int argca; // [rsp+18h] [rbp-50h]
int v29; // [rsp+1Ch] [rbp-4Ch]
int v30; // [rsp+28h] [rbp-40h] BYREF
int v31[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v30 = 0;
v7 = (const char *)func1(a3);
if ( !v7 || (v9 = (const char *)func2((__int64 *)a3, (__int64)a2, v6)) == 0LL )
{
v10 = 0LL;
v9 = 0LL;
goto LABEL_17;
}
v10 = (const struct option *)func3((__int64 *)a3, (__int64)v31, v8);
if ( !v10 )
{
LABEL_17:
v18 = (const char *)bbus_strerror(10001LL);
fprintf(stderr, "%s: %s\n", "bbus_parse_args", v18);
v19 = -1;
goto LABEL_18;
}
for ( i = 0LL; ; func0(*(_DWORD *)(i + 20), *(__int64 (__fastcall **)(__int64))(i + 24), (__int64)optarg) )
{
v13 = getopt_long(a1, (char *const *)a2, v9, v10, &v30);
if ( v13 == -1 )
break;
if ( v13 == 58 || v13 == 63 )
{
fprintf(stderr, "try %s --help\n", *a2);
v19 = -1;
goto LABEL_18;
}
if ( v13 )
{
v14 = *(_QWORD *)(a3 + 8);
if ( v14 )
{
v15 = 0;
v16 = 0LL;
do
{
v17 = (_DWORD *)(*(_QWORD *)a3 + 40 * v16);
if ( *v17 == v13 )
{
if ( *v17 )
i = (__int64)v17;
}
v16 = ++v15;
}
while ( v15 < v14 );
}
}
else
{
if ( v31[0] < 0 )
{
if ( v31[0] == -2 )
fprintf(stdout, "%s %s\n", *(const char **)(a3 + 32), *(const char **)(a3 + 40));
else
fputs(v7, stdout);
v19 = 1;
goto LABEL_18;
}
i = *(_QWORD *)a3 + 40LL * v31[0];
}
}
if ( !*(_QWORD *)(a3 + 24) )
{
LABEL_27:
v19 = 0;
if ( !a4 )
goto LABEL_18;
v24 = find_nonopts(a1, (__int64)a2, v12);
v19 = 0;
*a4 = v24;
if ( v24 )
goto LABEL_18;
goto LABEL_17;
}
v21 = 0;
v12 = 0LL;
v22 = optind;
while ( a1 > v22 )
{
v29 = v13;
longind = v21;
func0(
*(_DWORD *)(*(_QWORD *)(a3 + 16) + 24 * v12),
*(__int64 (__fastcall **)(__int64))(*(_QWORD *)(a3 + 16) + 24 * v12 + 8),
(__int64)a2[v22]);
v13 = v29;
v12 = (unsigned int)(longind + 1);
v21 = longind + 1;
v22 = optind + 1;
v23 = v12 < *(_QWORD *)(a3 + 24);
++optind;
if ( !v23 )
goto LABEL_27;
}
argca = v13;
fprintf(stderr, "%s: expected additional parameters\ntry %s --help\n", *a2, *a2);
v19 = argca;
LABEL_18:
argc = v19;
bbus_free(v9);
bbus_free(v10);
bbus_free(v7);
return argc;
}

original_code:
int bbus_parse_args(int argc, char** argv, const struct bbus_opt_list* optlist,
      struct bbus_nonopts** nonopts)
{
 char* shortopts = ((void *)0);
 struct option* longopts = ((void *)0);
 const struct bbus_option* curopt = ((void *)0);
 int opt;
 int ind = 0;
 int ret = 0;
 int flag;
 char* info = ((void *)0);
 unsigned i;
 info = make_info_string(optlist);
 if (info == ((void *)0))
  goto out_of_memory;
 shortopts = make_shortopts(optlist);
 if (shortopts == ((void *)0))
  goto out_of_memory;
 longopts = make_longopts(optlist, &flag);
 if (longopts == ((void *)0))
  goto out_of_memory;
 while ((opt = getopt_long(argc, argv, shortopts,
     longopts, &ind)) != -1) {
  switch (opt) {
  case '?':
  case ':':
   fprintf(stderr, "try %s --help\n", argv[0]);
   ret = -1;
   goto out;
   break;
  case 0:
   if (flag < 0) {
    if (flag == -2) {
     fprintf(stdout, "%s %s\n",
       optlist->progname,
       optlist->version);
    } else {
     fprintf(stdout, "%s", info);
    }
    ret = 1;
    goto out;
   } else {
    curopt = &optlist->opts[flag];
   }
   break;
  default:
   for (i = 0; i < optlist->numopts; ++i) {
    if (optlist->opts[i].shortopt != 0) {
     if (optlist->opts[i].shortopt == opt) {
      curopt = &optlist->opts[i];
     }
    }
   }
   break;
  }
  handle_option(curopt->action, curopt->actdata, optarg);
 }
 for (i = 0; i < optlist->numpargs; ++i) {
  if (optind >= argc) {
   fprintf(stderr, "%s: expected additional parameters"
       "\ntry %s --help\n",
       argv[0], argv[0]);
   ret = -1;
   goto out;
  }
  handle_option(optlist->pargs[i].action,
    optlist->pargs[i].actdata,
    argv[optind]);
  ++optind;
 }
 if (nonopts != ((void *)0)) {
  *nonopts = find_nonopts(argc, argv);
  if (*nonopts == ((void *)0))
   goto out_of_memory;
 }
 goto out;
out_of_memory:
 fprintf(stderr, "%s: %s\n", __FUNCTION__, bbus_strerror(10001));
 ret = -1;
out:
 bbus_free(shortopts);
 bbus_free(longopts);
 bbus_free(info);
 return ret;
}

canonical_original_code:
int bbus_parse_args(int argc, char **argv, struct bbus_opt_list *optlist, struct bbus_nonopts **nonopts)
{
 char *shortopts = ((void *)0);
 struct option *longopts = ((void *)0);
 struct bbus_option *curopt = ((void *)0);
 int opt;
 int ind = 0;
 int ret = 0;
 int flag;
 char *info = ((void *)0);
 unsigned int i;
 info = make_info_string(optlist);
 if (info == ((void *)0))
  goto out_of_memory;
 shortopts = make_shortopts(optlist);
 if (shortopts == ((void *)0))
  goto out_of_memory;
 longopts = make_longopts(optlist, &flag);
 if (longopts == ((void *)0))
  goto out_of_memory;
 while ((opt = getopt_long(argc, argv, shortopts,
     longopts, &ind)) != -1) {
  switch (opt) {
  case '?':
  case ':':
   fprintf(stderr, "try %s --help\n", argv[0]);
   ret = -1;
   goto out;
   break;
  case 0:
   if (flag < 0) {
    if (flag == -2) {
     fprintf(stdout, "%s %s\n",
       optlist->progname,
       optlist->version);
    } else {
     fprintf(stdout, "%s", info);
    }
    ret = 1;
    goto out;
   } else {
    curopt = &optlist->opts[flag];
   }
   break;
  default:
   for (i = 0; i < optlist->numopts; ++i) {
    if (optlist->opts[i].shortopt != 0) {
     if (optlist->opts[i].shortopt == opt) {
      curopt = &optlist->opts[i];
     }
    }
   }
   break;
  }
  handle_option(curopt->action, curopt->actdata, optarg);
 }
 for (i = 0; i < optlist->numpargs; ++i) {
  if (optind >= argc) {
   fprintf(stderr, "%s: expected additional parameters"
       "\ntry %s --help\n",
       argv[0], argv[0]);
   ret = -1;
   goto out;
  }
  handle_option(optlist->pargs[i].action,
    optlist->pargs[i].actdata,
    argv[optind]);
  ++optind;
 }
 if (nonopts != ((void *)0)) {
  *nonopts = find_nonopts(argc, argv);
  if (*nonopts == ((void *)0))
   goto out_of_memory;
 }
 goto out;
out_of_memory:
 fprintf(stderr, "%s: %s\n", __FUNCTION__, bbus_strerror(10001));
 ret = -1;
out:
 bbus_free(shortopts);
 bbus_free(longopts);
 bbus_free(info);
 return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct bbus_nonopts { char **args; unsigned long numargs; } **

variable_types:
shortopts
char *

variable_types: struct bbus_option { int shortopt; char *longopt; enum bbus_opt_hasarg { BBUS_OPT_NOARG=0, BBUS_OPT_ARGREQ=1, BBUS_OPT_ARGOPT=2 } hasarg; enum bbus_opt_action { BBUS_OPTACT_NOTHING=0, BBUS_OPTACT_SETFLAG=1, BBUS_OPTACT_GETOPTARG=2, BBUS_OPTACT_CALLFUNC=3 } action; void *actdata; char *descr; } *

variable_types: opt int

variable_types: ind int

variable_types: flag int

user_defined_types: struct option { char *name; int has_arg; int *flag; int val; }


decompiled_code:
__int64 bbus_lasterror()
{
return *(unsigned int *)((char *)bbus_lasterror + _tls_get_addr(&last_error_tlsind));
}

canonical_decompiled_code:
__int64 func0()
{
return *(unsigned int *)((char *)bbus_lasterror + _tls_get_addr(&last_error_tlsind));
}

original_code:
int bbus_lasterror(void)
{
 return last_error;
}

canonical_original_code:
int bbus_lasterror()
{
 return last_error;
}


decompiled_code:
char *__fastcall bbus_strerror(int a1)
{
char *result; // rax
__int64 v2; // rax

if ( a1 <= 9999 )
{
v2 = _tls_get_addr(&errmsg_tlsind);
result = strerror_r(a1, &errmsg[v2 - 1068], 0x40uLL);
}
else
{
result = "invalid error code";
if ( a1 <= 10019 )
result = error_descr[a1 - 10000];
}
return result;
}

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

if ( a1 <= 9999 )
{
v2 = _tls_get_addr(&errmsg_tlsind);
result = strerror_r(a1, &errmsg[v2 - 1068], 0x40uLL);
}
else
{
result = "invalid error code";
if ( a1 <= 10019 )
result = error_descr[a1 - 10000];
}
return result;
}

original_code:
const char* bbus_strerror(int errnum)
{
 if (errnum < 10000)
  return strerror_r(errnum, errmsg, 64);
 else if (errnum >= 10020)
  return "invalid error code";
 else
  return error_descr[errnum-10000];
}

canonical_original_code:
char *bbus_strerror(int errnum)
{
 if (errnum < 10000)
  return strerror_r(errnum, errmsg, 64);
 else if (errnum >= 10020)
  return "invalid error code";
 else
  return error_descr[errnum-10000];
}

variable_types: errnum int


decompiled_code:
__int64 bbusd_register_local_methods()
{
__int64 result; // rax

result = bbusd_insert_method("bbus.bbusd.echo", &_m_lm_echo__);
if ( (int)result < 0 )
{
bbusd_die("Error inserting method: '%s'\n", "bbus.bbusd.echo");
JUMPOUT(0x75LL);
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

result = bbusd_insert_method("bbus.bbusd.echo", &_m_lm_echo__);
if ( (int)result < 0 )
{
bbusd_die("Error inserting method: '%s'\n", "bbus.bbusd.echo");
JUMPOUT(0x75LL);
}
return result;
}

original_code:
void bbusd_register_local_methods(void)
{
 do { if (bbusd_insert_method("bbus.bbusd.echo", (struct bbusd_method*) &__m_lm_echo__) < 0) { bbusd_die( "Error inserting method: '%s'\n", "bbus.bbusd.echo"); } } while (0);
}

canonical_original_code:
void bbusd_register_local_methods()
{
 do { if (bbusd_insert_method("bbus.bbusd.echo", (struct bbusd_method *) &__m_lm_echo__) < 0) { bbusd_die( "Error inserting method: '%s'\n", "bbus.bbusd.echo"); } } while (0);
}

user_defined_types: struct bbusd_method { int type; char data[]; }


decompiled_code:
__int64 __fastcall bbus_crc32(_BYTE *a1, __int64 a2)
{
return (unsigned int)~update_crc32_constprop_0(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, __int64 a2)
{
return (unsigned int)~func0(a1, a2);
}

original_code:
uint32_t bbus_crc32(const void* buf, size_t bufsize)
{
 return update_crc32(0xffffffff,
  (const unsigned char*)buf, bufsize) ^ 0xffffffff;
}

canonical_original_code:
unsigned int bbus_crc32(void *buf, unsigned long bufsize)
{
 return update_crc32(0xffffffff,
  (unsigned char *)buf, bufsize) ^ 0xffffffff;
}

variable_types:
buf
void *

variable_types:
bufsize
unsigned long

return_type: unsigned int


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

if ( !a1 )
a1 = 1LL;
result = malloc(a1);
if ( !result )
{
_bbus_seterr(10001LL);
result = 0LL;
}
return result;
}

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

if ( !a1 )
a1 = 1LL;
result = malloc(a1);
if ( !result )
{
_bbus_seterr(10001LL);
result = 0LL;
}
return result;
}

original_code:
void* bbus_malloc(size_t size)
{
 void* p;
 if (size == 0)
  size = 1;
 p = malloc(size);
 if (p == ((void *)0))
  __bbus_seterr(10001);
 return p;
}

canonical_original_code:
void *bbus_malloc(unsigned long size)
{
 void *p;
 if (size == 0)
  size = 1;
 p = malloc(size);
 if (p == ((void *)0))
  __bbus_seterr(10001);
 return p;
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


decompiled_code:
void *__fastcall bbus_malloc0(size_t n)
{
void *v1; // rax
void *v2; // r8

v1 = bbus_malloc(n);
v2 = v1;
if ( v1 )
v2 = memset(v1, 0, n);
return v2;
}

canonical_decompiled_code:
void *__fastcall func1(size_t n)
{
void *v1; // rax
void *v2; // r8

v1 = func0(n);
v2 = v1;
if ( v1 )
v2 = memset(v1, 0, n);
return v2;
}

original_code:
void* bbus_malloc0(size_t size)
{
 void* p;
 p = bbus_malloc(size);
 if (p)
  memset(p, 0, size);
 return p;
}

canonical_original_code:
void *bbus_malloc0(unsigned long size)
{
 void *p;
 p = bbus_malloc(size);
 if (p)
  memset(p, 0, size);
 return p;
}


decompiled_code:
void *__fastcall bbus_realloc(void *a1, size_t a2)
{
void *result; // rax

if ( !a2 )
a2 = 1LL;
result = realloc(a1, a2);
if ( !result )
{
_bbus_seterr(10001LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1, size_t a2)
{
void *result; // rax

if ( !a2 )
a2 = 1LL;
result = realloc(a1, a2);
if ( !result )
{
_bbus_seterr(10001LL);
result = 0LL;
}
return result;
}

original_code:
void* bbus_realloc(void* ptr, size_t size)
{
 void* p;
 if (size == 0)
  size = 1;
 p = realloc(ptr, size);
 if (p == ((void *)0))
  __bbus_seterr(10001);
 return p;
}

canonical_original_code:
void *bbus_realloc(void *ptr, unsigned long size)
{
 void *p;
 if (size == 0)
  size = 1;
 p = realloc(ptr, size);
 if (p == ((void *)0))
  __bbus_seterr(10001);
 return p;
}

variable_types:
ptr
void *


decompiled_code:
void __fastcall bbus_free(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void bbus_free(void* ptr)
{
 if (ptr != ((void *)0))
  free(ptr);
}

canonical_original_code:
void bbus_free(void *ptr)
{
 if (ptr != ((void *)0))
  free(ptr);
}


decompiled_code:
void *__fastcall bbus_memdup(void *src, size_t n)
{
void *v2; // rax
void *v3; // r8

v2 = bbus_malloc(n);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, n);
return v3;
}

canonical_decompiled_code:
void *__fastcall func4(void *src, size_t n)
{
void *v2; // rax
void *v3; // r8

v2 = func0(n);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, n);
return v3;
}

original_code:
void* bbus_memdup(const void* src, size_t size)
{
 void* newp;
 newp = bbus_malloc(size);
 if (newp)
  memcpy(newp, src, size);
 return newp;
}

canonical_original_code:
void *bbus_memdup(void *src, unsigned long size)
{
 void *newp;
 newp = bbus_malloc(size);
 if (newp)
  memcpy(newp, src, size);
 return newp;
}

variable_types:
src
void *

variable_types:
newp
void *


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

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

original_code:
int bbusd_auth_client(const struct bbus_client_cred* cred __attribute__((unused)))
{
 return 0;
}

canonical_original_code:
int bbusd_auth_client(struct bbus_client_cred *cred)
{
 return 0;
}

variable_types: struct bbus_client_cred { int pid; unsigned int uid; unsigned int gid; } *


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

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

original_code:
static inline int loglvl_to_sysloglvl(enum bbusd_loglevel lvl)
{
 return (int)lvl;
}

variable_types: enum bbusd_loglevel { BBUSD_LOG_EMERG=0, BBUSD_LOG_ALERT=1, BBUSD_LOG_CRIT=2, BBUSD_LOG_ERR=3, BBUSD_LOG_WARN=4, BBUSD_LOG_NOTICE=5, BBUSD_LOG_INFO=6, BBUSD_LOG_DEBUG=7 }


decompiled_code:
int bbusd_logmsg(unsigned int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 <= 4 )
return vfprintf(stderr, a2, arg);
if ( a1 - 5 > 2 )
{
bbusd_die("Invalid log level\n");
JUMPOUT(0xE2LL);
}
return vfprintf(stdout, a2, arg);
}

canonical_decompiled_code:
int func1(unsigned int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 <= 4 )
return vfprintf(stderr, a2, arg);
if ( a1 - 5 > 2 )
{
bbusd_die("Invalid log level\n");
JUMPOUT(0xE2LL);
}
return vfprintf(stdout, a2, arg);
}

original_code:
void bbusd_logmsg(enum bbusd_loglevel lvl, const char* fmt, ...)
{
 va_list va;
 if (logmask & (1 << 0)) {
  __builtin_va_start(va,fmt);
  switch (lvl) {
  case BBUSD_LOG_EMERG:
  case BBUSD_LOG_ALERT:
  case BBUSD_LOG_CRIT:
  case BBUSD_LOG_ERR:
  case BBUSD_LOG_WARN:
   vfprintf(stderr, fmt, va);
   break;
  case BBUSD_LOG_NOTICE:
  case BBUSD_LOG_INFO:
  case BBUSD_LOG_DEBUG:
   vfprintf(stdout, fmt, va);
   break;
  default:
   bbusd_die("Invalid log level\n");
   break;
  }
  __builtin_va_end(va);
 }
 if (logmask & (1 << 1)) {
  __builtin_va_start(va,fmt);
  openlog("bbusd", 0x01, (3<<3));
  vsyslog(loglvl_to_sysloglvl(lvl), fmt, va);
  closelog();
  __builtin_va_end(va);
 }
}

canonical_original_code:
void bbusd_logmsg(enum bbusd_loglevel lvl, char *fmt, ...)
{
 __builtin_va_list va;
 if (logmask & (1 << 0)) {
  __builtin_va_start(va,fmt);
  switch (lvl) {
  case BBUSD_LOG_EMERG:
  case BBUSD_LOG_ALERT:
  case BBUSD_LOG_CRIT:
  case BBUSD_LOG_ERR:
  case BBUSD_LOG_WARN:
   vfprintf(stderr, fmt, va);
   break;
  case BBUSD_LOG_NOTICE:
  case BBUSD_LOG_INFO:
  case BBUSD_LOG_DEBUG:
   vfprintf(stdout, fmt, va);
   break;
  default:
   bbusd_die("Invalid log level\n");
   break;
  }
  __builtin_va_end(va);
 }
 if (logmask & (1 << 1)) {
  __builtin_va_start(va,fmt);
  openlog("bbusd", 0x01, (3<<3));
  vsyslog(loglvl_to_sysloglvl(lvl), fmt, va);
  closelog();
  __builtin_va_end(va);
 }
}

variable_types:
fmt
char *

user_defined_types: enum bbusd_loglevel { BBUSD_LOG_EMERG=0, BBUSD_LOG_ALERT=1, BBUSD_LOG_CRIT=2, BBUSD_LOG_ERR=3, BBUSD_LOG_WARN=4, BBUSD_LOG_NOTICE=5, BBUSD_LOG_INFO=6, BBUSD_LOG_DEBUG=7 }


decompiled_code:
__int64 bbusd_init_caller_map()
{
__int64 result; // rax
unsigned int v1; // eax
const char *v2; // rax

result = bbus_hmap_create(1LL);
caller_map = result;
if ( !result )
{
v1 = bbus_lasterror();
v2 = (const char *)bbus_strerror(v1);
bbusd_die("Error creating the caller hashmap: %s\n", v2);
result = bbusd_clean_caller_map();
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
unsigned int v1; // eax
const char *v2; // rax

result = bbus_hmap_create(1LL);
caller_map = result;
if ( !result )
{
v1 = bbus_lasterror();
v2 = (const char *)bbus_strerror(v1);
bbusd_die("Error creating the caller hashmap: %s\n", v2);
result = func1();
}
return result;
}

original_code:
void bbusd_init_caller_map(void)
{
 caller_map = bbus_hmap_create(BBUS_HMAP_KEYUINT);
 if (caller_map == ((void *)0)) {
  bbusd_die("Error creating the caller hashmap: %s\n",
     bbus_strerror(bbus_lasterror()));
 }
}

canonical_original_code:
void bbusd_init_caller_map()
{
 caller_map = bbus_hmap_create(BBUS_HMAP_KEYUINT);
 if (caller_map == ((void *)0)) {
  bbusd_die("Error creating the caller hashmap: %s\n",
     bbus_strerror(bbus_lasterror()));
 }
}


decompiled_code:
__int64 bbusd_clean_caller_map()
{
return bbus_hmap_free(caller_map);
}

canonical_decompiled_code:
__int64 func1()
{
return bbus_hmap_free(caller_map);
}

original_code:
void bbusd_clean_caller_map(void)
{
 bbus_hmap_free(caller_map);
}

canonical_original_code:
void bbusd_clean_caller_map()
{
 bbus_hmap_free(caller_map);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rbx
char v5; // al
char v7; // al
char v8; // al
char v9; // al
__int64 *v10; // [rsp+8h] [rbp-2040h]
char v11[8248]; // [rsp+10h] [rbp-2038h] BYREF

v3 = bbus_parse_args();
if ( v3 == 1 )
return 0;
if ( v3 == -1 )
return 1;
if ( v10[1] != strlen(argdescr) )
die("Need to pass arguments matching the description\n", (char)argv);
argstart = *v10;
signal(13, (__sighandler_t)1);
if ( !bbus_connect() )
{
bbus_lasterror();
v8 = bbus_strerror();
die("Error connecting to bbusd: %s\n", v8);
}
if ( !bbus_obj_alloc() )
goto LABEL_15;
v4 = argstart;
v5 = *argdescr;
if ( *argdescr )
{
while ( 1 )
{
if ( v5 != 115 )
{
bbus_closeconn();
bbus_obj_free();
die("Invalid argument description character: %c\n", *argdescr);
}
if ( (int)bbus_obj_insstr() < 0 )
break;
v4 += 8LL;
v5 = *++argdescr;
if ( !*argdescr )
goto LABEL_10;
}
LABEL_15:
bbus_closeconn();
bbus_obj_free();
bbus_lasterror();
v7 = bbus_strerror();
die("Error creating the argument object: %s\n", v7);
}
LABEL_10:
if ( !bbus_callmethod() )
{
bbus_lasterror();
bbus_closeconn();
bbus_obj_free();
bbus_lasterror();
bbus_strerror();
die("Error calling method '%s': %s\n", method[0]);
}
memset(v11, 0, 0x2000uLL);
if ( (int)bbus_obj_repr() < 0 )
{
bbus_closeconn();
bbus_obj_free();
bbus_obj_free();
bbus_lasterror();
v9 = bbus_strerror();
die("Error creating object representation: %s\n", v9);
}
fprintf(stdout, "%s\n", v11);
bbus_closeconn();
bbus_obj_free();
bbus_obj_free();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rbx
char v5; // al
char v7; // al
char v8; // al
char v9; // al
__int64 *v10; // [rsp+8h] [rbp-2040h]
char v11[8248]; // [rsp+10h] [rbp-2038h] BYREF

v3 = bbus_parse_args();
if ( v3 == 1 )
return 0;
if ( v3 == -1 )
return 1;
if ( v10[1] != strlen(argdescr) )
func8("Need to pass arguments matching the description\n", (char)argv);
argstart = *v10;
signal(13, (__sighandler_t)1);
if ( !bbus_connect() )
{
bbus_lasterror();
v8 = bbus_strerror();
func8("Error connecting to bbusd: %s\n", v8);
}
if ( !bbus_obj_alloc() )
goto LABEL_15;
v4 = argstart;
v5 = *argdescr;
if ( *argdescr )
{
while ( 1 )
{
if ( v5 != 115 )
{
bbus_closeconn();
bbus_obj_free();
func8("Invalid argument description character: %c\n", *argdescr);
}
if ( (int)bbus_obj_insstr() < 0 )
break;
v4 += 8LL;
v5 = *++argdescr;
if ( !*argdescr )
goto LABEL_10;
}
LABEL_15:
bbus_closeconn();
bbus_obj_free();
bbus_lasterror();
v7 = bbus_strerror();
func8("Error creating the argument object: %s\n", v7);
}
LABEL_10:
if ( !bbus_callmethod() )
{
bbus_lasterror();
bbus_closeconn();
bbus_obj_free();
bbus_lasterror();
bbus_strerror();
func8("Error calling method '%s': %s\n", method[0]);
}
memset(v11, 0, 0x2000uLL);
if ( (int)bbus_obj_repr() < 0 )
{
bbus_closeconn();
bbus_obj_free();
bbus_obj_free();
bbus_lasterror();
v9 = bbus_strerror();
func8("Error creating object representation: %s\n", v9);
}
fprintf(stdout, "%s\n", v11);
bbus_closeconn();
bbus_obj_free();
bbus_obj_free();
return 0;
}

original_code:
int main(int argc __attribute__((unused)), char** argv __attribute__((unused)))
{
 struct bbusunit_listelem* el;
 int r;
 unsigned tests_run = 0;
 unsigned tests_failed = 0;
 struct timeval begin;
 struct timeval end;
 struct timeval time_spent;
 memset(&begin, 0, sizeof(struct timeval));
 memset(&end, 0, sizeof(struct timeval));
 memset(&time_spent, 0, sizeof(struct timeval));
 bbusunit_print("##############################");
 bbusunit_print("####> Busybus unit-tests <####");
 bbusunit_print("##############################");
 bbusunit_print("%u tests registered.", tests_registered);
 bbusunit_print("Running tests...");
 el = tests.head;
 gettimeofday(&begin, ((void *)0));
 while (el != ((void *)0)) {
  bbusunit_print("Case:\t[%s]", el->name);
  r = el->testfunc();
  if (r < 0) {
   bbusunit_printerr("[%s]: TEST FAILED", el->name);
   tests_failed++;
  }
  ++tests_run;
  el = el->next;
 }
 gettimeofday(&end, ((void *)0));
 timeval_diff(&time_spent, &end, &begin);
 bbusunit_print("All done!");
 bbusunit_print("SUMMARY:");
 bbusunit_print("  %u %s run in %d.%06d seconds.", tests_run,
   tests_run == 1 ? "test" : "tests",
   (int)time_spent.tv_sec, (int)time_spent.tv_usec);
 if (tests_failed > 0) {
  bbusunit_printerr("  %u %s FAILED", tests_failed,
    tests_failed == 1 ? "test" : "tests");
  bbusunit_printerr("  Failure rate: %.2f%%",
     ((double)tests_failed/tests_run)*100);
  r = 1;
 } else {
  bbusunit_print("  All tests PASSED!");
  r = 0;
 }
 return r;
}

canonical_original_code:
int main(int argc, char **argv)
{
 struct bbusunit_listelem *el;
 int r;
 unsigned int tests_run = 0;
 unsigned int tests_failed = 0;
 struct timeval begin;
 struct timeval end;
 struct timeval time_spent;
 memset(&begin, 0, sizeof(struct timeval));
 memset(&end, 0, sizeof(struct timeval));
 memset(&time_spent, 0, sizeof(struct timeval));
 bbusunit_print("##############################");
 bbusunit_print("####> Busybus unit-tests <####");
 bbusunit_print("##############################");
 bbusunit_print("%u tests registered.", tests_registered);
 bbusunit_print("Running tests...");
 el = tests.head;
 gettimeofday(&begin, ((void *)0));
 while (el != ((void *)0)) {
  bbusunit_print("Case:\t[%s]", el->name);
  r = el->testfunc();
  if (r < 0) {
   bbusunit_printerr("[%s]: TEST FAILED", el->name);
   tests_failed++;
  }
  ++tests_run;
  el = el->next;
 }
 gettimeofday(&end, ((void *)0));
 timeval_diff(&time_spent, &end, &begin);
 bbusunit_print("All done!");
 bbusunit_print("SUMMARY:");
 bbusunit_print("  %u %s run in %d.%06d seconds.", tests_run,
   tests_run == 1 ? "test" : "tests",
   (int)time_spent.tv_sec, (int)time_spent.tv_usec);
 if (tests_failed > 0) {
  bbusunit_printerr("  %u %s FAILED", tests_failed,
    tests_failed == 1 ? "test" : "tests");
  bbusunit_printerr("  Failure rate: %.2f%%",
     ((double)tests_failed/tests_run)*100);
  r = 1;
 } else {
  bbusunit_print("  All tests PASSED!");
  r = 0;
 }
 return r;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct bbusunit_listelem { struct bbusunit_listelem *next; char *name; int (*testfunc)(); } *

variable_types: r int

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


decompiled_code:
// attributes: thunk
__int64 opt_setsockpath()
{
return bbus_prot_setsockpath();
}

canonical_decompiled_code:
__int64 func7()
{
return bbus_prot_setsockpath();
}

original_code:
static void opt_setsockpath(const char* path)
{
 bbus_prot_setsockpath(path);
}

canonical_original_code:
static void opt_setsockpath(char *path)
{
 bbus_prot_setsockpath(path);
}

variable_types:
path
char *


die

decompiled_code:
void __noreturn die(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func8(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
static void __attribute__((format(printf, 1, 2))) __attribute__((noreturn)) die(const char* format, ...)
{
 va_list va;
 __builtin_va_start(va,format);
 vfprintf(stderr, format, va);
 __builtin_va_end(va);
 exit(1);
}

canonical_original_code:
static void die(char *format, ...)
{
 __builtin_va_list va;
 __builtin_va_start(va,format);
 vfprintf(stderr, format, va);
 __builtin_va_end(va);
 exit(1);
}

variable_types:
format
char *


decompiled_code:
__int64 __fastcall bbus_cred_uidtousername(__uid_t uid, char *s, size_t maxlen)
{
int v4; // eax
size_t v5; // r12
char *v6; // rax
char *v7; // r14
int v8; // er12
int *v10; // rax
struct passwd *result; // [rsp+8h] [rbp-60h] BYREF
struct passwd resultbuf; // [rsp+10h] [rbp-58h] BYREF

v4 = sysconf(70);
if ( v4 < 0 )
v4 = 256;
v5 = v4;
v6 = (char *)bbus_malloc0(v4);
if ( v6 )
{
v7 = v6;
*(_OWORD *)&resultbuf.pw_name = 0LL;
*(_OWORD *)&resultbuf.pw_uid = 0LL;
*(_OWORD *)&resultbuf.pw_dir = 0LL;
v8 = getpwuid_r(uid, &resultbuf, v6, v5, &result);
if ( v8 < 0 )
{
v10 = _errno_location();
v8 = -1;
_bbus_seterr((unsigned int)*v10);
}
else
{
snprintf(s, maxlen, "%s", result->pw_name);
}
bbus_free(v7);
}
else
{
v8 = -1;
}
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(__uid_t uid, char *s, size_t maxlen)
{
int v4; // eax
size_t v5; // r12
char *v6; // rax
char *v7; // r14
int v8; // er12
int *v10; // rax
struct passwd *result; // [rsp+8h] [rbp-60h] BYREF
struct passwd resultbuf; // [rsp+10h] [rbp-58h] BYREF

v4 = sysconf(70);
if ( v4 < 0 )
v4 = 256;
v5 = v4;
v6 = (char *)bbus_malloc0(v4);
if ( v6 )
{
v7 = v6;
*(_OWORD *)&resultbuf.pw_name = 0LL;
*(_OWORD *)&resultbuf.pw_uid = 0LL;
*(_OWORD *)&resultbuf.pw_dir = 0LL;
v8 = getpwuid_r(uid, &resultbuf, v6, v5, &result);
if ( v8 < 0 )
{
v10 = _errno_location();
v8 = -1;
_bbus_seterr((unsigned int)*v10);
}
else
{
snprintf(s, maxlen, "%s", result->pw_name);
}
bbus_free(v7);
}
else
{
v8 = -1;
}
return (unsigned int)v8;
}

original_code:
int bbus_cred_uidtousername(uid_t uid, char* buf, size_t buflen)
{
 struct passwd pwd;
 struct passwd* pres;
 char* pbuf;
 int pbufsiz;
 int ret;
 pbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
 if (pbufsiz < 0)
  pbufsiz = 256;
 pbuf = bbus_malloc0(pbufsiz);
 if (pbuf == ((void *)0))
  return -1;
 memset(&pwd, 0, sizeof(struct passwd));
 ret = getpwuid_r(uid, &pwd, pbuf, pbufsiz, &pres);
 if (ret < 0) {
  __bbus_seterr((*__errno_location ()));
  ret = -1;
  goto out;
 }
 (void)snprintf(buf, buflen, "%s", pres->pw_name);
out:
 bbus_free(pbuf);
 return ret;
}

canonical_original_code:
int bbus_cred_uidtousername(unsigned int uid, char *buf, unsigned long buflen)
{
 struct passwd pwd;
 struct passwd *pres;
 char *pbuf;
 int pbufsiz;
 int ret;
 pbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
 if (pbufsiz < 0)
  pbufsiz = 256;
 pbuf = bbus_malloc0(pbufsiz);
 if (pbuf == ((void *)0))
  return -1;
 memset(&pwd, 0, sizeof(struct passwd));
 ret = getpwuid_r(uid, &pwd, pbuf, pbufsiz, &pres);
 if (ret < 0) {
  __bbus_seterr((*__errno_location ()));
  ret = -1;
  goto out;
 }
 (void)snprintf(buf, buflen, "%s", pres->pw_name);
out:
 bbus_free(pbuf);
 return ret;
}

variable_types:
uid
unsigned int

variable_types:
buf
char *

variable_types:
buflen
unsigned long

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
pbuf
char *

variable_types: pbufsiz int

variable_types: ret int


decompiled_code:
void __noreturn bbusd_die(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
void bbusd_die(const char* format, ...)
{
 va_list va;
 __builtin_va_start(va,format);
 vfprintf(stderr, format, va);
 __builtin_va_end(va);
 exit(1);
}

canonical_original_code:
void bbusd_die(char *format, ...)
{
 __builtin_va_list va;
 __builtin_va_start(va,format);
 vfprintf(stderr, format, va);
 __builtin_va_end(va);
 exit(1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbp
__int64 *v6; // rsi
__int64 v7; // rdi
int v8; // er12
int v9; // eax
char v11; // al
char v12; // al
char v13; // al
__int64 v14[5]; // [rsp+0h] [rbp-28h] BYREF

v3 = bbus_parse_args();
if ( v3 == 1 )
return 0;
if ( v3 == -1 )
return 1;
signal(15, sighandler);
signal(2, sighandler);
v4 = bbus_srvc_connect();
v5 = v4;
if ( !v4 )
{
bbus_lasterror();
v13 = bbus_strerror();
die("Error connecting to bbusd: %s\n", v13);
}
v6 = (__int64 *)&echo_method;
v7 = v4;
if ( (int)bbus_srvc_regmethod() < 0 )
{
bbus_lasterror();
v12 = bbus_strerror();
die("Error registering method: %s\n", v12);
}
run = 1;
while ( 1 )
{
v8 = do_run(v7, v6);
if ( !v8 )
break;
while ( 1 )
{
v6 = v14;
v7 = v5;
v14[0] = 0LL;
v14[1] = 500000LL;
v9 = bbus_srvc_listencalls();
if ( v9 < 0 )
{
bbus_lasterror();
v11 = bbus_strerror();
die("Error receiving a method call: %s\n", v11);
}
if ( !v9 )
break;
fwrite("Method properly called.\n", 1uLL, 0x18uLL, stderr);
v8 = do_run("Method properly called.\n", 1LL);
if ( !v8 )
goto LABEL_10;
}
}
LABEL_10:
bbus_srvc_closeconn();
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbp
__int64 *v6; // rsi
__int64 v7; // rdi
int v8; // er12
int v9; // eax
char v11; // al
char v12; // al
char v13; // al
__int64 v14[5]; // [rsp+0h] [rbp-28h] BYREF

v3 = bbus_parse_args();
if ( v3 == 1 )
return 0;
if ( v3 == -1 )
return 1;
signal(15, sighandler);
signal(2, sighandler);
v4 = bbus_srvc_connect();
v5 = v4;
if ( !v4 )
{
bbus_lasterror();
v13 = bbus_strerror();
func10("Error connecting to bbusd: %s\n", v13);
}
v6 = (__int64 *)&echo_method;
v7 = v4;
if ( (int)bbus_srvc_regmethod() < 0 )
{
bbus_lasterror();
v12 = bbus_strerror();
func10("Error registering method: %s\n", v12);
}
run = 1;
while ( 1 )
{
v8 = func11(v7, v6);
if ( !v8 )
break;
while ( 1 )
{
v6 = v14;
v7 = v5;
v14[0] = 0LL;
v14[1] = 500000LL;
v9 = bbus_srvc_listencalls();
if ( v9 < 0 )
{
bbus_lasterror();
v11 = bbus_strerror();
func10("Error receiving a method call: %s\n", v11);
}
if ( !v9 )
break;
fwrite("Method properly called.\n", 1uLL, 0x18uLL, stderr);
v8 = func11("Method properly called.\n", 1LL);
if ( !v8 )
goto LABEL_10;
}
}
LABEL_10:
bbus_srvc_closeconn();
return v8;
}

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

user_defined_types: struct bbusunit_listelem { struct bbusunit_listelem *next; char *name; int (*testfunc)(); }


decompiled_code:
__int64 do_stop()
{
return (unsigned int)_InterlockedExchange(&run, 0);
}

canonical_decompiled_code:
__int64 func8()
{
return (unsigned int)_InterlockedExchange(&run, 0);
}

original_code:
static void do_stop(void)
{
 do { (void)__sync_lock_test_and_set(&(run), (0)); } while (0);
}

canonical_original_code:
static void do_stop()
{
 do { (void)__sync_lock_test_and_set(&(run), (0)); } while (0);
}


sighandler

decompiled_code:
void __fastcall sighandler(int a1)
{
if ( a1 == 2 || a1 == 15 )
do_stop();
}

canonical_decompiled_code:
void __fastcall func9(int a1)
{
if ( a1 == 2 || a1 == 15 )
func8();
}

original_code:
static void sighandler(int signum)
{
 switch (signum) {
 case 2:
 case 15:
  do_stop();
  break;
 }
}

variable_types: signum int


die

canonical_decompiled_code:
void __noreturn func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}


decompiled_code:
__int64 do_run()
{
signed __int32 v0; // eax
unsigned __int32 v1; // er8
signed __int32 v2; // ett

v0 = run;
do
{
v1 = v0;
v2 = v0;
v0 = _InterlockedCompareExchange(&run, v0, v0);
}
while ( v2 != v0 );
return v1;
}

canonical_decompiled_code:
__int64 func11()
{
signed __int32 v0; // eax
unsigned __int32 v1; // er8
signed __int32 v2; // ett

v0 = run;
do
{
v1 = v0;
v2 = v0;
v0 = _InterlockedCompareExchange(&run, v0, v0);
}
while ( v2 != v0 );
return v1;
}

original_code:
static int do_run(void)
{
 return __sync_fetch_and_or(&(run), 0);
}

canonical_original_code:
static int do_run()
{
 return __sync_fetch_and_or(&(run), 0);
}


decompiled_code:
__int16 __fastcall int16_rcpy(__int16 *a1)
{
return _bswap_16(*a1);
}

canonical_decompiled_code:
__int16 __fastcall func1(__int16 *a1)
{
return func0(*a1);
}

original_code:
static inline uint16_t int16_rcpy(const uint8_t *p)
{
  uint16_t tmp;
  memcpy(&tmp, p, 2);
  tmp = ntohs(tmp);
  return tmp;
}

canonical_original_code:
static inline unsigned short int16_rcpy(unsigned char *p)
{
  unsigned short tmp;
  memcpy(&tmp, p, 2);
  tmp = ntohs(tmp);
  return tmp;
}

variable_types:
p
unsigned char *

variable_types:
tmp
unsigned short

return_type: unsigned short


decompiled_code:
int __fastcall udp_write(__int64 a1, __int64 a2, __int64 a3, int a4)
{
int v6; // ebx
int result; // eax
__int64 v8; // r8
__int64 v9; // rcx
int v10; // [rsp+0h] [rbp-30h]
int v11[11]; // [rsp+4h] [rbp-2Ch] BYREF

if ( a4 > 0 )
{
v6 = 0;
while ( 1 )
{
udp_payload_header_parse((__int16 *)(a3 + v6), v11);
v9 = *(unsigned int *)(a1 + 60);
if ( (int)v9 < v10 )
break;
packet_write(*(_DWORD *)a1, a1 + 4, *(_DWORD *)(a1 + 4LL * v10 + 20), (const void *)(a3 + v8 + 3), v11[0]);
result = v11[0];
v6 += v11[0] + 3;
if ( a4 <= v6 )
return result;
}
result = fprintf(stderr, "Bad stream %d > %d\n", v10, v9);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2, __int64 a3, int a4)
{
int v6; // ebx
int result; // eax
__int64 v8; // r8
__int64 v9; // rcx
int v10; // [rsp+0h] [rbp-30h]
int v11[11]; // [rsp+4h] [rbp-2Ch] BYREF

if ( a4 > 0 )
{
v6 = 0;
while ( 1 )
{
func2((__int16 *)(a3 + v6), v11);
v9 = *(unsigned int *)(a1 + 60);
if ( (int)v9 < v10 )
break;
func3(*(_DWORD *)a1, a1 + 4, *(_DWORD *)(a1 + 4LL * v10 + 20), (const void *)(a3 + v8 + 3), v11[0]);
result = v11[0];
v6 += v11[0] + 3;
if ( a4 <= v6 )
return result;
}
result = fprintf(stderr, "Bad stream %d > %d\n", v10, v9);
}
return result;
}

original_code:
static void udp_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
{
  int i = 0;
  while (i < size) {
    int stream, psize;
    udp_payload_header_parse(data + i, &psize, &stream);
    if (stream > o->ports) {
      fprintf(stderr, "Bad stream %d > %d\n", stream, o->ports);
      return;
    }
    packet_write(o->outfd, o->ip, o->port[stream], data + i + (2 + 1), psize);
    i += (2 + 1) + psize;
  }
}

canonical_original_code:
static void udp_write(struct dechunkiser_ctx *o, int id, unsigned char *data, int size)
{
  int i = 0;
  while (i < size) {
    int stream; int psize;
    udp_payload_header_parse(data + i, &psize, &stream);
    if (stream > o->ports) {
      fprintf(stderr, "Bad stream %d > %d\n", stream, o->ports);
      return;
    }
    packet_write(o->outfd, o->ip, o->port[stream], data + i + (2 + 1), psize);
    i += (2 + 1) + psize;
  }
}

variable_types: struct dechunkiser_ctx { int outfd; char ip[16]; int port[10]; int ports; } *

variable_types: id int

variable_types:
data
unsigned char *

variable_types: size int

variable_types: i int

variable_types: stream int

variable_types: psize int


decompiled_code:
char *__fastcall udp_open_out(__int64 a1, __int64 a2)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *v6; // rdi

if ( !a2 )
{
v2 = 0LL;
fwrite("udp output not configured, please specify the output ports\n", 1uLL, 0x3BuLL, stderr);
return v2;
}
v2 = (char *)malloc(0x40uLL);
if ( !v2 )
return v2;
v3 = socket(2, 2, 17);
*(_DWORD *)v2 = v3;
if ( v3 >= 0 )
{
v4 = dst_parse(a2, (__int64)(v2 + 20), v2 + 4);
*((_DWORD *)v2 + 15) = v4;
if ( !v4 )
{
fwrite("cannot parse the output ports.\n", 1uLL, 0x1FuLL, stderr);
close(*(_DWORD *)v2);
v6 = v2;
v2 = 0LL;
free(v6);
}
return v2;
}
fwrite("cannot open the output socket.\n", 1uLL, 0x1FuLL, stderr);
free(v2);
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 a1, __int64 a2)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *v6; // rdi

if ( !a2 )
{
v2 = 0LL;
fwrite("udp output not configured, please specify the output ports\n", 1uLL, 0x3BuLL, stderr);
return v2;
}
v2 = (char *)malloc(0x40uLL);
if ( !v2 )
return v2;
v3 = socket(2, 2, 17);
*(_DWORD *)v2 = v3;
if ( v3 >= 0 )
{
v4 = func6(a2, (__int64)(v2 + 20), v2 + 4);
*((_DWORD *)v2 + 15) = v4;
if ( !v4 )
{
fwrite("cannot parse the output ports.\n", 1uLL, 0x1FuLL, stderr);
close(*(_DWORD *)v2);
v6 = v2;
v2 = 0LL;
free(v6);
}
return v2;
}
fwrite("cannot open the output socket.\n", 1uLL, 0x1FuLL, stderr);
free(v2);
return 0LL;
}

original_code:
static struct dechunkiser_ctx *udp_open_out(const char *fname, const char *config)
{
  struct dechunkiser_ctx *res;
  if (!config) {
    fprintf(stderr, "udp output not configured, please specify the output ports\n");
    return ((void *)0);
  }
  res = malloc(sizeof(struct dechunkiser_ctx));
  if (res == ((void *)0)) {
    return ((void *)0);
  }
  res->outfd = socket(2, SOCK_DGRAM, IPPROTO_UDP);
  if (res->outfd < 0) {
    fprintf(stderr, "cannot open the output socket.\n");
    free(res);
    return ((void *)0);
  }
  res->ports = dst_parse(config, res->port, res->ip);
  if (res->ports == 0) {
    fprintf(stderr, "cannot parse the output ports.\n");
    close(res->outfd);
    free(res);
    return ((void *)0);
  }
  return res;
}

canonical_original_code:
static struct dechunkiser_ctx *udp_open_out(char *fname, char *config)
{
  struct dechunkiser_ctx *res;
  if (!config) {
    fprintf(stderr, "udp output not configured, please specify the output ports\n");
    return ((void *)0);
  }
  res = malloc(sizeof(struct dechunkiser_ctx));
  if (res == ((void *)0)) {
    return ((void *)0);
  }
  res->outfd = socket(2, SOCK_DGRAM, IPPROTO_UDP);
  if (res->outfd < 0) {
    fprintf(stderr, "cannot open the output socket.\n");
    free(res);
    return ((void *)0);
  }
  res->ports = dst_parse(config, res->port, res->ip);
  if (res->ports == 0) {
    fprintf(stderr, "cannot parse the output ports.\n");
    close(res->outfd);
    free(res);
    return ((void *)0);
  }
  return res;
}

variable_types:
fname
char *

variable_types:
config
char *

return_type: struct dechunkiser_ctx { int outfd; char ip[16]; int port[10]; int ports; } *


decompiled_code:
__int64 __fastcall dumb_get_fds(__int64 a1)
{
return a1 + 8;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1 + 8;
}

original_code:
const int *dumb_get_fds(const struct chunkiser_ctx *s)
{
  return s->fds;
}

canonical_original_code:
int *dumb_get_fds(struct chunkiser_ctx *s)
{
  return s->fds;
}

variable_types: struct chunkiser_ctx { int loop; int chunk_size; int fds[2]; } *

return_type: int *


original_code:
static uint8_t *dumb_chunkise(struct chunkiser_ctx *s, int id, int *size, uint64_t *ts, void **attr, int *attr_size)
{
  uint8_t *res;
  res = malloc(s->chunk_size);
  if (res == ((void *)0)) {
    *size = -1;
    return ((void *)0);
  }
  *ts = 0;
  *size = read(s->fds[0], res, s->chunk_size);
  if ((*size == 0) && ((*__errno_location ()) != 11)) {
    *size = -1;
    if (s->loop) {
      if (lseek(s->fds[0], 0, 0) == 0) {
        *size = 0;
      }
    }
    free(res);
    res = ((void *)0);
  }
  return res;
}

canonical_original_code:
static unsigned char *dumb_chunkise(struct chunkiser_ctx *s, int id, int *size, unsigned long *ts, void **attr, int *attr_size)
{
  unsigned char *res;
  res = malloc(s->chunk_size);
  if (res == ((void *)0)) {
    *size = -1;
    return ((void *)0);
  }
  *ts = 0;
  *size = read(s->fds[0], res, s->chunk_size);
  if ((*size == 0) && ((*__errno_location ()) != 11)) {
    *size = -1;
    if (s->loop) {
      if (lseek(s->fds[0], 0, 0) == 0) {
        *size = 0;
      }
    }
    free(res);
    res = ((void *)0);
  }
  return res;
}

variable_types:
size
int *

variable_types:
ts
unsigned long *

variable_types:
attr
void **

variable_types:
res
unsigned char *

return_type: unsigned char *

user_defined_types: struct chunkiser_ctx { int loop; int chunk_size; int fds[2]; }


decompiled_code:
void __fastcall dumb_close(void *ptr)
{
close(*((_DWORD *)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
close(*((_DWORD *)ptr + 2));
free(ptr);
}

original_code:
static void dumb_close(struct chunkiser_ctx *s)
{
  close(s->fds[0]);
  free(s);
}


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

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

original_code:
static inline uint32_t int_rcpy(const uint8_t *p)
{
  uint32_t tmp;
  memcpy(&tmp, p, 4);
  tmp = ntohl(tmp);
  return tmp;
}

canonical_original_code:
static inline unsigned int int_rcpy(unsigned char *p)
{
  unsigned int tmp;
  memcpy(&tmp, p, 4);
  tmp = ntohl(tmp);
  return tmp;
}

variable_types:
tmp
unsigned int


decompiled_code:
__int64 __fastcall bmap_decode(__int64 a1, __int64 a2, int a3, _DWORD *a4)
{
unsigned int v4; // er8
int v5; // er9
int v6; // edx
int v7; // eax
int v8; // ecx
__int64 v9; // rdx
__int64 v10; // rcx
__int64 result; // rax

if ( ((*(_DWORD *)(a1 + 4) & 7) != 0) + (*(_DWORD *)(a1 + 4) >> 3) + 16 + *a4 > a3 )
{
fwrite("Error in decoding chunkid set - wrong length\n", 1uLL, 0x2DuLL, stderr);
chunkID_set_free(a1);
result = 0LL;
}
else
{
v5 = int_rcpy((unsigned int *)(a2 + 12));
v7 = v6 - 1;
if ( v6 - 1 >= 0 )
{
do
{
v8 = *(unsigned __int8 *)(a2 + (v7 >> 3) + 16);
if ( _bittest(&v8, v7 & 7) )
{
v9 = *(unsigned int *)(a1 + 8);
v10 = *(_QWORD *)(a1 + 16);
*(_DWORD *)(a1 + 8) = v9 + 1;
*(_DWORD *)(v10 + 4 * v9) = v7 + v5;
}
}
while ( v7-- != 0 );
}
result = a2 + v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int a3, _DWORD *a4)
{
unsigned int v4; // er8
int v5; // er9
int v6; // edx
int v7; // eax
int v8; // ecx
__int64 v9; // rdx
__int64 v10; // rcx
__int64 result; // rax

if ( ((*(_DWORD *)(a1 + 4) & 7) != 0) + (*(_DWORD *)(a1 + 4) >> 3) + 16 + *a4 > a3 )
{
fwrite("Error in decoding chunkid set - wrong length\n", 1uLL, 0x2DuLL, stderr);
chunkID_set_free(a1);
result = 0LL;
}
else
{
v5 = func1((unsigned int *)(a2 + 12));
v7 = v6 - 1;
if ( v6 - 1 >= 0 )
{
do
{
v8 = *(unsigned __int8 *)(a2 + (v7 >> 3) + 16);
if ( _bittest(&v8, v7 & 7) )
{
v9 = *(unsigned int *)(a1 + 8);
v10 = *(_QWORD *)(a1 + 16);
*(_DWORD *)(a1 + 8) = v9 + 1;
*(_DWORD *)(v10 + 4 * v9) = v7 + v5;
}
}
while ( v7-- != 0 );
}
result = a2 + v4;
}
return result;
}

original_code:
static const uint8_t *bmap_decode(struct chunkID_set *h, const uint8_t *buff, int buff_len, int *meta_len)
{
  int i;
  int base;
  int byte_cnt;
  byte_cnt = h->size / 8 + (h->size % 8 ? 1 : 0);
  if (buff_len < 16 + byte_cnt + *meta_len) {
    fprintf(stderr, "Error in decoding chunkid set - wrong length\n");
    chunkID_set_free(h);
    return ((void *)0);
  }
  base = int_rcpy(buff + 12);
  for (i = h->size - 1; i >= 0; i--) {
  if (buff[16 + (i / 8)] & 1 << (i % 8))
    h->elements[h->n_elements++] = base + i;
  }
  return buff + 16 + byte_cnt;
}

canonical_original_code:
static unsigned char *bmap_decode(struct chunkID_set *h, unsigned char *buff, int buff_len, int *meta_len)
{
  int i;
  int base;
  int byte_cnt;
  byte_cnt = h->size / 8 + (h->size % 8 ? 1 : 0);
  if (buff_len < 16 + byte_cnt + *meta_len) {
    fprintf(stderr, "Error in decoding chunkid set - wrong length\n");
    chunkID_set_free(h);
    return ((void *)0);
  }
  base = int_rcpy(buff + 12);
  for (i = h->size - 1; i >= 0; i--) {
  if (buff[16 + (i / 8)] & 1 << (i % 8))
    h->elements[h->n_elements++] = base + i;
  }
  return buff + 16 + byte_cnt;
}

variable_types: struct chunkID_set { unsigned int type; unsigned int size; unsigned int n_elements; int *elements; struct cids_ops_iface { int (*add_chunk)(struct chunkID_set *h, int chunk_id); int (*check)(struct chunkID_set *h, int chunk_id); } *ops; struct cids_encoding_iface { unsigned char *(*encode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int meta_len); unsigned char *(*decode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int *meta_len); } *enc; } *

variable_types:
buff
unsigned char *

variable_types: base int

user_defined_types: struct chunkID_set { unsigned int type; unsigned int size; unsigned int n_elements; int *elements; struct cids_ops_iface *ops; struct cids_encoding_iface *enc; }


decompiled_code:
__int64 __fastcall int_cpy(__int64 a1, unsigned int a2)
{
__int64 result; // rax
_DWORD *v3; // rdx

result = _bswap_32(a2);
*v3 = result;
return result;
}

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

result = func0(a2);
*v3 = result;
return result;
}

original_code:
static inline void int_cpy(uint8_t *p, int v)
{
  uint32_t tmp;
  tmp = htonl(v);
  memcpy(p, &tmp, 4);
}

canonical_original_code:
static inline void int_cpy(unsigned char *p, int v)
{
  unsigned int tmp;
  tmp = htonl(v);
  memcpy(p, &tmp, 4);
}

variable_types:
p
unsigned char *

variable_types: v int


original_code:
static uint8_t *bmap_encode(const struct chunkID_set *h, uint8_t *buff, int buff_len, int meta_len)
{
  int i, elements;
  uint32_t c_min, c_max;
  c_min = c_max = h->n_elements ? h->elements[0] : 0;
  for (i = 1; i < h->n_elements; i++) {
    if (h->elements[i] < c_min)
      c_min = h->elements[i];
    else if (h->elements[i] > c_max)
      c_max = h->elements[i];
  }
  elements = h->n_elements ? c_max - c_min + 1 : 0;
  int_cpy(buff, elements);
  elements = elements / 8 + (elements % 8 ? 1 : 0);
  if (buff_len < elements + 16 + meta_len) {
    return ((void *)0);
  }
  int_cpy(buff + 12, c_min);
  memset(buff + 16, 0, elements);
  for (i = 0; i < h->n_elements; i++) {
    buff[16 + (h->elements[i] - c_min) / 8] |= 1 << ((h->elements[i] - c_min) % 8);
  }
  return buff + 16 + elements;
}

canonical_original_code:
static unsigned char *bmap_encode(struct chunkID_set *h, unsigned char *buff, int buff_len, int meta_len)
{
  int i; int elements;
  unsigned int c_min; unsigned int c_max;
  c_min = c_max = h->n_elements ? h->elements[0] : 0;
  for (i = 1; i < h->n_elements; i++) {
    if (h->elements[i] < c_min)
      c_min = h->elements[i];
    else if (h->elements[i] > c_max)
      c_max = h->elements[i];
  }
  elements = h->n_elements ? c_max - c_min + 1 : 0;
  int_cpy(buff, elements);
  elements = elements / 8 + (elements % 8 ? 1 : 0);
  if (buff_len < elements + 16 + meta_len) {
    return ((void *)0);
  }
  int_cpy(buff + 12, c_min);
  memset(buff + 16, 0, elements);
  for (i = 0; i < h->n_elements; i++) {
    buff[16 + (h->elements[i] - c_min) / 8] |= 1 << ((h->elements[i] - c_min) % 8);
  }
  return buff + 16 + elements;
}

variable_types: elements int

user_defined_types: struct cids_ops_iface { int (*add_chunk)(struct chunkID_set *h, int chunk_id); int (*check)(struct chunkID_set *h, int chunk_id); }

user_defined_types: struct cids_encoding_iface { unsigned char *(*encode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int meta_len); unsigned char *(*decode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int *meta_len); }


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


decompiled_code:
int __fastcall raw_write(int *a1, __int64 a2, char *a3, int a4)
{
char *v4; // r11
int v5; // ebx
int v6; // eax
unsigned int *v8; // r11
__int64 v9; // rdx
int v10; // ebp
int v11; // eax
int v12; // [rsp+8h] [rbp-60h]
int v13; // [rsp+Ch] [rbp-5Ch]
unsigned __int8 v14; // [rsp+17h] [rbp-51h] BYREF
int v15; // [rsp+1Ch] [rbp-4Ch] BYREF

v4 = a3;
v5 = a4;
v6 = a1[1];
if ( v6 != 1 )
{
if ( v6 == 2 )
{
v5 = a4 - 3;
v4 = a3 + 3;
}
else if ( v6 == 3 )
{
v5 = a4 - 15;
v4 = a3 + 15;
}
return write(*a1, v4, v5);
}
if ( *a3 )
{
if ( (unsigned __int8)*a3 <= 0x7Eu )
{
video_payload_header_parse((__int64)a3, &v14);
v13 = 10;
v9 = 9LL;
}
else
{
audio_payload_header_parse(a3, &v14, &v15);
v13 = 7;
v9 = 6LL;
}
v10 = 0;
v12 = *((unsigned __int8 *)v8 + v9);
if ( *((_BYTE *)v8 + v9) )
{
do
{
frame_header_parse(v8, &v15);
++v10;
}
while ( v12 != v10 );
}
v11 = v13 + 8 * v12;
v5 -= v11;
v4 = (char *)v8 + v11;
return write(*a1, v4, v5);
}
return fprintf(stderr, "Error! Strange chunk: %x!!!\n", v14);
}

original_code:
static void raw_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
{
  int offset;
  if (o->payload_type == avf) {
    int header_size;
    int frames;
    int i;
    uint8_t codec;
    if (data[0] == 0) {
      fprintf(stderr, "Error! Strange chunk: %x!!!\n", codec);
      return;
    } else if (data[0] < 127) {
      int width, height, frame_rate_n, frame_rate_d;
      header_size = 1 + 2 + 2 + 2 + 2 + 1;
      video_payload_header_parse(data, &codec, &width, &height, &frame_rate_n, &frame_rate_d);
    } else {
      uint8_t channels;
      int sample_rate, frame_size;
      header_size = 1 + 1 + 2 + 2 + 1;
      audio_payload_header_parse(data, &codec, &channels, &sample_rate, &frame_size);
    }
    frames = data[header_size - 1];
    for (i = 0; i < frames; i++) {
      int frame_size;
      int64_t pts, dts;
      frame_header_parse(data, &frame_size, &pts, &dts);
    }
    offset = header_size + frames * (3 + 4 + 1);
  } else if (o->payload_type == udp) {
    offset = (2 + 1);
  } else if (o->payload_type == rtp) {
    offset = (2 + 1) + 12;
  } else {
    offset = 0;
  }
  write(o->fd, data + offset, size - offset);
}

canonical_original_code:
static void raw_write(struct dechunkiser_ctx *o, int id, unsigned char *data, int size)
{
  int offset;
  if (o->payload_type == avf) {
    int header_size;
    int frames;
    int i;
    unsigned char codec;
    if (data[0] == 0) {
      fprintf(stderr, "Error! Strange chunk: %x!!!\n", codec);
      return;
    } else if (data[0] < 127) {
      int width; int height; int frame_rate_n; int frame_rate_d;
      header_size = 1 + 2 + 2 + 2 + 2 + 1;
      video_payload_header_parse(data, &codec, &width, &height, &frame_rate_n, &frame_rate_d);
    } else {
      unsigned char channels;
      int sample_rate; int frame_size;
      header_size = 1 + 1 + 2 + 2 + 1;
      audio_payload_header_parse(data, &codec, &channels, &sample_rate, &frame_size);
    }
    frames = data[header_size - 1];
    for (i = 0; i < frames; i++) {
      int frame_size;
      long pts; long dts;
      frame_header_parse(data, &frame_size, &pts, &dts);
    }
    offset = header_size + frames * (3 + 4 + 1);
  } else if (o->payload_type == udp) {
    offset = (2 + 1);
  } else if (o->payload_type == rtp) {
    offset = (2 + 1) + 12;
  } else {
    offset = 0;
  }
  write(o->fd, data + offset, size - offset);
}

variable_types: struct dechunkiser_ctx { int fd; enum pt { raw=0, avf=1, udp=2, rtp=3 } payload_type; } *

variable_types: id int

variable_types:
data
unsigned char *

variable_types: size int

variable_types: offset int

variable_types: frames int

variable_types: i int

variable_types:
codec
unsigned char

variable_types: width int

variable_types: height int

variable_types:
channels
unsigned char

variable_types: pts long

variable_types: dts long

user_defined_types: struct dechunkiser_ctx { int fd; enum pt payload_type; }


decompiled_code:
void __fastcall raw_close(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
static void raw_close(struct dechunkiser_ctx *s)
{
  close(s->fd);
  free(s);
}


decompiled_code:
__int64 __fastcall chunkID_set_add_chunk(__int64 a1)
{
return (**(__int64 (***)(void))(a1 + 24))();
}

original_code:
int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id)
{
  return h->ops->add_chunk(h, chunk_id);
}

variable_types: struct chunkID_set { unsigned int type; unsigned int size; unsigned int n_elements; int *elements; struct cids_ops_iface { int (*add_chunk)(struct chunkID_set *h, int chunk_id); int (*check)(struct chunkID_set *h, int chunk_id); } *ops; struct cids_encoding_iface { unsigned char *(*encode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int meta_len); unsigned char *(*decode)(struct chunkID_set *h, unsigned char *buff, int buff_len, int *meta_len); } *enc; } *


decompiled_code:
__int64 __fastcall chunkID_set_size(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

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

original_code:
int chunkID_set_size(const struct chunkID_set *h)
{
  return h->n_elements;
}

canonical_original_code:
int chunkID_set_size(struct chunkID_set *h)
{
  return h->n_elements;
}


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

if ( *(_DWORD *)(a1 + 8) <= a2 )
result = 0xFFFFFFFFLL;
else
result = *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * (int)a2);
return result;
}

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

if ( *(_DWORD *)(a1 + 8) <= a2 )
result = 0xFFFFFFFFLL;
else
result = *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * (int)a2);
return result;
}

original_code:
int chunkID_set_get_chunk(const struct chunkID_set *h, int i)
{
  if (i < h->n_elements) {
    return h->elements[i];
  }
  return -1;
}

canonical_original_code:
int chunkID_set_get_chunk(struct chunkID_set *h, int i)
{
  if (i < h->n_elements) {
    return h->elements[i];
  }
  return -1;
}


decompiled_code:
__int64 __fastcall chunkID_set_check(__int64 a1)
{
return (*(__int64 (**)(void))(*(_QWORD *)(a1 + 24) + 8LL))();
}

original_code:
int chunkID_set_check(const struct chunkID_set *h, int chunk_id)
{
  return h->ops->check(h, chunk_id);
}

canonical_original_code:
int chunkID_set_check(struct chunkID_set *h, int chunk_id)
{
  return h->ops->check(h, chunk_id);
}


decompiled_code:
void *__fastcall chunkID_set_clear(__int64 a1, int a2)
{
void *result; // rax

*(_DWORD *)(a1 + 4) = a2;
*(_DWORD *)(a1 + 8) = 0;
result = realloc(*(void **)(a1 + 16), 4LL * a2);
*(_QWORD *)(a1 + 16) = result;
if ( !result )
*(_DWORD *)(a1 + 4) = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1, int a2)
{
void *result; // rax

*(_DWORD *)(a1 + 4) = a2;
*(_DWORD *)(a1 + 8) = 0;
result = realloc(*(void **)(a1 + 16), 4LL * a2);
*(_QWORD *)(a1 + 16) = result;
if ( !result )
*(_DWORD *)(a1 + 4) = 0;
return result;
}

original_code:
void chunkID_set_clear(struct chunkID_set *h, int size)
{
  h->n_elements = 0;
  h->size = size;
  h->elements = realloc(h->elements, size * sizeof(int));
  if (h->elements == ((void *)0)) {
    h->size = 0;
  }
}


decompiled_code:
void __fastcall chunkID_set_free(void *ptr)
{
chunkID_set_clear((__int64)ptr, 0);
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
func4((__int64)ptr, 0);
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void chunkID_set_free(struct chunkID_set *h)
{
  chunkID_set_clear(h,0);
  free(h->elements);
  free(h);
}


decompiled_code:
void *__fastcall chunkID_set_trim(__int64 a1, signed int a2)
{
void *result; // rax

result = (void *)*(unsigned int *)(a1 + 8);
if ( (unsigned int)result > a2 )
{
result = memmove(*(void **)(a1 + 16), (const void *)(*(_QWORD *)(a1 + 16) + 4 * ((__int64)result - a2)), 4LL * a2);
*(_DWORD *)(a1 + 8) = a2;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1, signed int a2)
{
void *result; // rax

result = (void *)*(unsigned int *)(a1 + 8);
if ( (unsigned int)result > a2 )
{
result = memmove(*(void **)(a1 + 16), (const void *)(*(_QWORD *)(a1 + 16) + 4 * ((__int64)result - a2)), 4LL * a2);
*(_DWORD *)(a1 + 8) = a2;
}
return result;
}

original_code:
void chunkID_set_trim(struct chunkID_set *h, int size)
{
  if (h->n_elements > size) {
    memmove(h->elements, h->elements + h->n_elements - size, sizeof(h->elements[0]) * size);
    h->n_elements = size;
  }
}


original_code:
struct tag *grapes_config_parse(const char *cfg)
{
  struct tag *res;
  int i = 0;
  const char *p = cfg;
  res = malloc(sizeof(struct tag) * 16);
  if (res == ((void *)0)) {
    return res;
  }
  while (p && *p != 0) {
    char *p1 = strchr(p, '=');
    memset(res[i].name, 0, 32);
    memset(res[i].value, 0, 64);
    if (p1) {
      if (i % 16 == 0) {
        res = realloc(res, sizeof(struct tag) * (i + 16));
      }
      if (p1 - p > 32 - 1) {
        fprintf(stderr, "Error, config name too long:%s\n", p);
        free(res);
        return ((void *)0);
      }
      memcpy(res[i].name, p, p1 - p);
      p = strchr(p1, ',');
      if (p == ((void *)0)) {
        p = p1 + strlen(p1);
      }
      if (p - p1 > 64 - 1) {
        fprintf(stderr, "Error, config value too long:%s\n", p1);
        free(res);
        return ((void *)0);
      }
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
      if (*p) p++;
    } else {
      p = ((void *)0);
    }
  }
  memset(res[i].name, 0, 32);
  memset(res[i].value, 0, 64);
  res = realloc(res, sizeof(struct tag) * (i + 1));
  return res;
}

canonical_original_code:
struct tag *grapes_config_parse(char *cfg)
{
  struct tag *res;
  int i = 0;
  char *p = cfg;
  res = malloc(sizeof(struct tag) * 16);
  if (res == ((void *)0)) {
    return res;
  }
  while (p && *p != 0) {
    char *p1 = strchr(p, '=');
    memset(res[i].name, 0, 32);
    memset(res[i].value, 0, 64);
    if (p1) {
      if (i % 16 == 0) {
        res = realloc(res, sizeof(struct tag) * (i + 16));
      }
      if (p1 - p > 32 - 1) {
        fprintf(stderr, "Error, config name too long:%s\n", p);
        free(res);
        return ((void *)0);
      }
      memcpy(res[i].name, p, p1 - p);
      p = strchr(p1, ',');
      if (p == ((void *)0)) {
        p = p1 + strlen(p1);
      }
      if (p - p1 > 64 - 1) {
        fprintf(stderr, "Error, config value too long:%s\n", p1);
        free(res);
        return ((void *)0);
      }
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
      if (*p) p++;
    } else {
      p = ((void *)0);
    }
  }
  memset(res[i].name, 0, 32);
  memset(res[i].value, 0, 64);
  res = realloc(res, sizeof(struct tag) * (i + 1));
  return res;
}

variable_types:
cfg
char *

variable_types: struct tag { char name[32]; char value[64]; } *

variable_types:
p
char *


decompiled_code:
char *__fastcall grapes_config_value_str(char *s1, char *s2)
{
do
{
if ( !strcmp(s1, s2) )
return s1 + 32;
s1 += 96;
}
while ( *(s1 - 96) );
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func8(char *s1, char *s2)
{
do
{
if ( !strcmp(s1, s2) )
return s1 + 32;
s1 += 96;
}
while ( *(s1 - 96) );
return 0LL;
}

original_code:
const char *grapes_config_value_str(const struct tag *cfg_values, const char *value)
{
  int i, done;
  i = 0; done = 0;
  while (!done) {
    if (!strcmp(cfg_values[i].name, value)) {
      return cfg_values[i].value;
    }
    if (cfg_values[i].name[0] == 0) {
      done = 1;
    } else {
      i++;
    }
  }
  return ((void *)0);
}

canonical_original_code:
char *grapes_config_value_str(struct tag *cfg_values, char *value)
{
  int i; int done;
  i = 0; done = 0;
  while (!done) {
    if (!strcmp(cfg_values[i].name, value)) {
      return cfg_values[i].value;
    }
    if (cfg_values[i].name[0] == 0) {
      done = 1;
    } else {
      i++;
    }
  }
  return ((void *)0);
}

variable_types:
value
char *

variable_types: done int

user_defined_types: struct tag { char name[32]; char value[64]; }


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

v4 = grapes_config_value_str(a1, a2);
if ( !v4 )
return 0LL;
*a3 = atoi(v4);
return 1LL;
}

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

v4 = func8(a1, a2);
if ( !v4 )
return 0LL;
*a3 = atoi(v4);
return 1LL;
}

original_code:
int grapes_config_value_int(const struct tag *cfg_values, const char *value, int *res)
{
  const char *str_res;
  str_res = grapes_config_value_str(cfg_values, value);
  if (str_res == ((void *)0)) {
    return 0;
  }
  *res = atoi(str_res);
  return 1;
}

canonical_original_code:
int grapes_config_value_int(struct tag *cfg_values, char *value, int *res)
{
  char *str_res;
  str_res = grapes_config_value_str(cfg_values, value);
  if (str_res == ((void *)0)) {
    return 0;
  }
  *res = atoi(str_res);
  return 1;
}

variable_types:
res
int *


decompiled_code:
__int64 __fastcall grapes_config_value_double(char *a1, char *a2, double *a3)
{
char *v4; // rdi
__int64 result; // rax
double v6; // xmm0_8

v4 = grapes_config_value_str(a1, a2);
result = 0LL;
if ( v4 )
{
v6 = strtod(v4, 0LL);
result = 1LL;
*a3 = v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1, char *a2, double *a3)
{
char *v4; // rdi
__int64 result; // rax
double v6; // xmm0_8

v4 = func8(a1, a2);
result = 0LL;
if ( v4 )
{
v6 = strtod(v4, 0LL);
result = 1LL;
*a3 = v6;
}
return result;
}

original_code:
int grapes_config_value_double(const struct tag *cfg_values, const char *value, double *res)
{
  const char *str_res;
  str_res = grapes_config_value_str(cfg_values, value);
  if (str_res == ((void *)0)) {
    return 0;
  }
  *res = strtod(str_res, ((void *)0));
  return 1;
}

canonical_original_code:
int grapes_config_value_double(struct tag *cfg_values, char *value, double *res)
{
  char *str_res;
  str_res = grapes_config_value_str(cfg_values, value);
  if (str_res == ((void *)0)) {
    return 0;
  }
  *res = strtod(str_res, ((void *)0));
  return 1;
}

variable_types:
res
double *


decompiled_code:
char *__fastcall grapes_config_value_str_default(char *a1, char *a2, char *a3)
{
char *result; // rax

result = grapes_config_value_str(a1, a2);
if ( !result )
result = a3;
return result;
}

canonical_decompiled_code:
char *__fastcall func11(char *a1, char *a2, char *a3)
{
char *result; // rax

result = func8(a1, a2);
if ( !result )
result = a3;
return result;
}

original_code:
const char *grapes_config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value)
{
  const char *res;
  res = grapes_config_value_str(cfg_values, value);
  return res ? res : default_value;
}

canonical_original_code:
char *grapes_config_value_str_default(struct tag *cfg_values, char *value, char *default_value)
{
  char *res;
  res = grapes_config_value_str(cfg_values, value);
  return res ? res : default_value;
}

variable_types:
res
char *


decompiled_code:
__int64 __fastcall grapes_config_value_int_default(char *a1, char *a2, int *a3, int a4)
{
__int64 result; // rax

result = grapes_config_value_int(a1, a2, a3);
if ( !(_DWORD)result )
*a3 = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *a1, char *a2, int *a3, int a4)
{
__int64 result; // rax

result = func9(a1, a2, a3);
if ( !(_DWORD)result )
*a3 = a4;
return result;
}

original_code:
int grapes_config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value)
{
  int r;
  r = grapes_config_value_int(cfg_values, value, res);
  if (!r) {
    *res = default_value;
  }
  return r;
}

canonical_original_code:
int grapes_config_value_int_default(struct tag *cfg_values, char *value, int *res, int default_value)
{
  int r;
  r = grapes_config_value_int(cfg_values, value, res);
  if (!r) {
    *res = default_value;
  }
  return r;
}

variable_types: r int


decompiled_code:
__int64 __fastcall grapes_config_value_double_default(char *a1, char *a2, double *a3, double a4)
{
__int64 result; // rax

result = grapes_config_value_double(a1, a2, a3);
if ( !(_DWORD)result )
*a3 = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *a1, char *a2, double *a3, double a4)
{
__int64 result; // rax

result = func10(a1, a2, a3);
if ( !(_DWORD)result )
*a3 = a4;
return result;
}

original_code:
int grapes_config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value)
{
  int r;
  r = grapes_config_value_double(cfg_values, value, res);
  if (!r) {
    *res = default_value;
  }
  return r;
}

canonical_original_code:
int grapes_config_value_double_default(struct tag *cfg_values, char *value, double *res, double default_value)
{
  int r;
  r = grapes_config_value_double(cfg_values, value, res);
  if (!r) {
    *res = default_value;
  }
  return r;
}


decompiled_code:
__int64 __fastcall chunkID_set_check_list(__int64 a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *(unsigned int *)(a1 + 8);
if ( !(_DWORD)v2 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *(_DWORD *)(*(_QWORD *)(a1 + 16) + 4 * result) != a2 )
{
if ( ++result == v2 )
return 0xFFFFFFFFLL;
}
return (unsigned int)result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *(unsigned int *)(a1 + 8);
if ( !(_DWORD)v2 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *(_DWORD *)(*(_QWORD *)(a1 + 16) + 4 * result) != a2 )
{
if ( ++result == v2 )
return 0xFFFFFFFFLL;
}
return (unsigned int)result;
}

original_code:
static int chunkID_set_check_list(const struct chunkID_set *h, int chunk_id)
{
  int i;
  for (i = 0; i < h->n_elements; i++) {
    if (h->elements[i] == chunk_id) {
      return i;
    }
  }
  return -1;
}

canonical_original_code:
static int chunkID_set_check_list(struct chunkID_set *h, int chunk_id)
{
  int i;
  for (i = 0; i < h->n_elements; i++) {
    if (h->elements[i] == chunk_id) {
      return i;
    }
  }
  return -1;
}

variable_types: i int


decompiled_code:
__int64 __fastcall chunkID_set_add_chunk_list(__int64 a1, int a2)
{
int v3; // er8
__int64 result; // rax
__int64 v5; // rax
void *v6; // rdi
void *v7; // rax

v3 = chunkID_set_check_list(a1, a2);
result = 0LL;
if ( v3 < 0 )
{
v5 = *(unsigned int *)(a1 + 8);
v6 = *(void **)(a1 + 16);
if ( (_DWORD)v5 == *(_DWORD *)(a1 + 4) )
{
v7 = realloc(v6, 4LL * (unsigned int)(v5 + 32));
v6 = v7;
if ( !v7 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 16) = v7;
v5 = *(unsigned int *)(a1 + 8);
*(_DWORD *)(a1 + 4) += 32;
}
*(_DWORD *)(a1 + 8) = v5 + 1;
*((_DWORD *)v6 + v5) = a2;
result = *(unsigned int *)(a1 + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
int v3; // er8
__int64 result; // rax
__int64 v5; // rax
void *v6; // rdi
void *v7; // rax

v3 = func0(a1, a2);
result = 0LL;
if ( v3 < 0 )
{
v5 = *(unsigned int *)(a1 + 8);
v6 = *(void **)(a1 + 16);
if ( (_DWORD)v5 == *(_DWORD *)(a1 + 4) )
{
v7 = realloc(v6, 4LL * (unsigned int)(v5 + 32));
v6 = v7;
if ( !v7 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 16) = v7;
v5 = *(unsigned int *)(a1 + 8);
*(_DWORD *)(a1 + 4) += 32;
}
*(_DWORD *)(a1 + 8) = v5 + 1;
*((_DWORD *)v6 + v5) = a2;
result = *(unsigned int *)(a1 + 8);
}
return result;
}

original_code:
static int chunkID_set_add_chunk_list(struct chunkID_set *h, int chunk_id)
{
  if (chunkID_set_check_list(h, chunk_id) >= 0) {
    return 0;
  }
  if (h->n_elements == h->size) {
    int *res;
    res = realloc(h->elements, (h->size + 32) * sizeof(int));
    if (res == ((void *)0)) {
      return -1;
    }
    h->size += 32;
    h->elements = res;
  }
  h->elements[h->n_elements++] = chunk_id;
  return h->n_elements;
}


decompiled_code:
__int16 __fastcall int16_rcpy(__int16 *a1)
{
return _bswap_16(*a1);
}

canonical_decompiled_code:
__int16 __fastcall func1(__int16 *a1)
{
return func0(*a1);
}

original_code:
static inline uint16_t int16_rcpy(const uint8_t *p)
{
  uint16_t tmp;
  memcpy(&tmp, p, 2);
  tmp = ntohs(tmp);
  return tmp;
}

canonical_original_code:
static inline unsigned short int16_rcpy(unsigned char *p)
{
  unsigned short tmp;
  memcpy(&tmp, p, 2);
  tmp = ntohs(tmp);
  return tmp;
}

variable_types:
p
unsigned char *

variable_types:
tmp
unsigned short


decompiled_code:
int __fastcall rtp_write(__int64 a1, __int64 a2, __int16 *a3, __int64 a4)
{
unsigned __int64 v4; // r12
__int16 *v6; // rbx
int result; // eax
__int16 *v8; // rdi
char *v9; // rbx
__int64 v10; // r8
int v11; // [rsp+8h] [rbp-30h]
unsigned int v12[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = (unsigned __int64)a3 + (int)a4;
v6 = a3;
result = printf_log(a1, 2, "Got chunk of size %i", a4);
if ( (unsigned __int64)v6 < v4 )
{
while ( 1 )
{
v8 = v6;
v9 = (char *)v6 + 3;
rtp_payload_per_pkt_header_parse(v8, v12);
v10 = *(unsigned int *)(a1 + 100);
if ( (int)v10 < v11 )
break;
printf_log(
a1,
2,
"sending packet of size %i from port id #%i to port %i",
v12[0],
(unsigned int)v11,
*(unsigned int *)(a1 + 4LL * v11 + 20));
result = packet_write(*(_DWORD *)a1, a1 + 4, *(_DWORD *)(a1 + 4LL * v11 + 20), v9, v12[0]);
v6 = (__int16 *)&v9[v12[0]];
if ( v4 <= (unsigned __int64)v6 )
return result;
}
result = printf_log(a1, 1, "Received Chunk with bad stream %d > %d", (unsigned int)v11, v10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2, __int16 *a3, __int64 a4)
{
unsigned __int64 v4; // r12
__int16 *v6; // rbx
int result; // eax
__int16 *v8; // rdi
char *v9; // rbx
__int64 v10; // r8
int v11; // [rsp+8h] [rbp-30h]
unsigned int v12[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = (unsigned __int64)a3 + (int)a4;
v6 = a3;
result = func3(a1, 2, "Got chunk of size %i", a4);
if ( (unsigned __int64)v6 < v4 )
{
while ( 1 )
{
v8 = v6;
v9 = (char *)v6 + 3;
func2(v8, v12);
v10 = *(unsigned int *)(a1 + 100);
if ( (int)v10 < v11 )
break;
func3(
a1,
2,
"sending packet of size %i from port id #%i to port %i",
v12[0],
(unsigned int)v11,
*(unsigned int *)(a1 + 4LL * v11 + 20));
result = func4(*(_DWORD *)a1, a1 + 4, *(_DWORD *)(a1 + 4LL * v11 + 20), v9, v12[0]);
v6 = (__int16 *)&v9[v12[0]];
if ( v4 <= (unsigned __int64)v6 )
return result;
}
result = func3(a1, 1, "Received Chunk with bad stream %d > %d", (unsigned int)v11, v10);
}
return result;
}

original_code:
static void rtp_write(struct dechunkiser_ctx *ctx, int id, uint8_t *data, int size) {
  uint8_t* data_end = data + size;
  printf_log(ctx, 2, "Got chunk of size %i", size);
  while (data < data_end) {
    int stream, psize;
    rtp_payload_per_pkt_header_parse(data, &psize, &stream);
    data += (2 + 1);
    if (stream > ctx->ports_len) {
      printf_log(ctx, 1, "Received Chunk with bad stream %d > %d",
                 stream, ctx->ports_len);
      return;
    }
    printf_log(ctx, 2,
               "sending packet of size %i from port id #%i to port %i",
               psize, stream, ctx->ports[stream]);
    packet_write(ctx->outfd, ctx->ip, ctx->ports[stream], data, psize);
    data += psize;
  }
}

canonical_original_code:
static void rtp_write(struct dechunkiser_ctx *ctx, int id, unsigned char *data, int size) {
  unsigned char *data_end = data + size;
  printf_log(ctx, 2, "Got chunk of size %i", size);
  while (data < data_end) {
    int stream; int psize;
    rtp_payload_per_pkt_header_parse(data, &psize, &stream);
    data += (2 + 1);
    if (stream > ctx->ports_len) {
      printf_log(ctx, 1, "Received Chunk with bad stream %d > %d",
                 stream, ctx->ports_len);
      return;
    }
    printf_log(ctx, 2,
               "sending packet of size %i from port id #%i to port %i",
               psize, stream, ctx->ports[stream]);
    packet_write(ctx->outfd, ctx->ip, ctx->ports[stream], data, psize);
    data += psize;
  }
}

variable_types: struct dechunkiser_ctx { int outfd; char ip[16]; int ports[-1]; int ports_len; int verbosity; } *

variable_types: id int

variable_types:
data
unsigned char *

variable_types: size int

variable_types: stream int

variable_types: psize int


decompiled_code:
void __fastcall rtp_close(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
static void rtp_close(struct dechunkiser_ctx *ctx) {
  close(ctx->outfd);
  free(ctx);
}

user_defined_types: struct dechunkiser_ctx { int outfd; char ip[16]; int ports[-1]; int ports_len; int verbosity; }


decompiled_code:
_BOOL8 __fastcall port_pair_parse(const char *a1, _DWORD *a2)
{
_BOOL8 result; // rax
int v3; // er8
int v4; // eax
int v5; // [rsp+Ch] [rbp-1Ch] BYREF

if ( strchr(a1, 58) )
return (unsigned int)_isoc99_sscanf(a1, "%d:%d", a2, a2 + 1) == -1;
v3 = _isoc99_sscanf(a1, "%d", &v5);
result = 1LL;
if ( v3 != -1 )
{
v4 = v5;
if ( (v5 & 1) != 0 )
{
a2[1] = v5;
*a2 = v4 - 1;
}
else
{
*a2 = v5;
a2[1] = v4 + 1;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(const char *a1, _DWORD *a2)
{
_BOOL8 result; // rax
int v3; // er8
int v4; // eax
int v5; // [rsp+Ch] [rbp-1Ch] BYREF

if ( strchr(a1, 58) )
return (unsigned int)_isoc99_sscanf(a1, "%d:%d", a2, a2 + 1) == -1;
v3 = _isoc99_sscanf(a1, "%d", &v5);
result = 1LL;
if ( v3 != -1 )
{
v4 = v5;
if ( (v5 & 1) != 0 )
{
a2[1] = v5;
*a2 = v4 - 1;
}
else
{
*a2 = v5;
a2[1] = v4 + 1;
}
result = 0LL;
}
return result;
}

original_code:
static inline int port_pair_parse(const char *conf_str, int *ports) {
  char *ptr;
  int port;
  ptr = strchr(conf_str, ':');
  if (ptr == ((void *)0)) {
    if (sscanf(conf_str, "%d", &port) == (-1)) {
      return 1;
    }
    if (port % 2 == 0) {
      ports[0] = port;
      ports[1] = port + 1;
    }
    else {
      ports[1] = port;
      ports[0] = port - 1;
    }
  }
  else {
    if (sscanf(conf_str, "%d:%d", &ports[0], &ports[1]) == (-1)) {
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
static inline int port_pair_parse(char *conf_str, int *ports) {
  char *ptr;
  int port;
  ptr = strchr(conf_str, ':');
  if (ptr == ((void *)0)) {
    if (sscanf(conf_str, "%d", &port) == (-1)) {
      return 1;
    }
    if (port % 2 == 0) {
      ports[0] = port;
      ports[1] = port + 1;
    }
    else {
      ports[1] = port;
      ports[0] = port - 1;
    }
  }
  else {
    if (sscanf(conf_str, "%d:%d", &ports[0], &ports[1]) == (-1)) {
      return 1;
    }
  }
  return 0;
}

variable_types:
ports
int *

variable_types:
ptr
char *

variable_types: port int