decompiled_code:
__int64 __fastcall get_number_dec(__int64 a1, const void **a2, double a3)
{
char *v5; // rsi
char *v6; // rbx
int v7; // ecx
int v8; // edx
int v9; // edi
int v10; // er8
int v11; // eax
void *v12; // rsp
char v14; // al
char v15[15]; // [rsp+0h] [rbp-30h] BYREF

v5 = (char *)*a2;
v6 = v5;
v7 = 0;
v8 = 0;
v9 = 0;
v10 = 0;
while ( 1 )
{
v14 = *v6;
if ( *v6 > 69 )
break;
if ( v14 <= 42 )
{
LABEL_3:
v11 = 0;
if ( v6 == v5 )
goto LABEL_5;
LABEL_4:
v12 = alloca((int)v6 - (int)v5 + 1 + 8LL);
memcpy(v15, v5, (int)v6 - (int)v5);
v15[(int)v6 - (int)v5] = 0;
a3 = atof(v15);
*a2 = v6;
v11 = 8;
goto LABEL_5;
}
switch ( v14 )
{
case '+':
if ( v9 == 0 || v6 == v5 || v8 )
goto LABEL_3;
if ( v7 )
goto LABEL_4;
v8 = 0;
v7 = 1;
break;
case '-':
if ( v9 == 0 || v6 == v5 || v7 )
goto LABEL_3;
if ( v8 )
goto LABEL_4;
v7 = 0;
v8 = 1;
break;
case '.':
if ( v10 )
goto LABEL_3;
++v10;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 'E':
goto LABEL_12;
default:
goto LABEL_3;
}
LABEL_8:
++v6;
}
if ( v14 != 101 )
goto LABEL_3;
LABEL_12:
if ( v6 != v5 )
{
if ( v9 )
goto LABEL_4;
++v9;
goto LABEL_8;
}
v11 = 0;
LABEL_5:
*(_DWORD *)a1 = v11;
*(double *)(a1 + 8) = a3;
*(_QWORD *)(a1 + 16) = 0LL;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const void **a2, double a3)
{
char *v5; // rsi
char *v6; // rbx
int v7; // ecx
int v8; // edx
int v9; // edi
int v10; // er8
int v11; // eax
void *v12; // rsp
char v14; // al
char v15[15]; // [rsp+0h] [rbp-30h] BYREF

v5 = (char *)*a2;
v6 = v5;
v7 = 0;
v8 = 0;
v9 = 0;
v10 = 0;
while ( 1 )
{
v14 = *v6;
if ( *v6 > 69 )
break;
if ( v14 <= 42 )
{
LABEL_3:
v11 = 0;
if ( v6 == v5 )
goto LABEL_5;
LABEL_4:
v12 = alloca((int)v6 - (int)v5 + 1 + 8LL);
memcpy(v15, v5, (int)v6 - (int)v5);
v15[(int)v6 - (int)v5] = 0;
a3 = atof(v15);
*a2 = v6;
v11 = 8;
goto LABEL_5;
}
switch ( v14 )
{
case '+':
if ( v9 == 0 || v6 == v5 || v8 )
goto LABEL_3;
if ( v7 )
goto LABEL_4;
v8 = 0;
v7 = 1;
break;
case '-':
if ( v9 == 0 || v6 == v5 || v7 )
goto LABEL_3;
if ( v8 )
goto LABEL_4;
v7 = 0;
v8 = 1;
break;
case '.':
if ( v10 )
goto LABEL_3;
++v10;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 'E':
goto LABEL_12;
default:
goto LABEL_3;
}
LABEL_8:
++v6;
}
if ( v14 != 101 )
goto LABEL_3;
LABEL_12:
if ( v6 != v5 )
{
if ( v9 )
goto LABEL_4;
++v9;
goto LABEL_8;
}
v11 = 0;
LABEL_5:
*(_DWORD *)a1 = v11;
*(double *)(a1 + 8) = a3;
*(_QWORD *)(a1 + 16) = 0LL;
return a1;
}

original_code:
static tok get_number_dec(const char **s)
{
 const char *end;
 tok ret;
 char *num;
 int dot_count = 0, e_count = 0, neg_count = 0, plus_count = 0;
 ret.type = T_NONE;
 for (end = *s; ; end++) {
  switch (*end) {
  case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
   break;
  case '.':
   if (dot_count++)
    goto done;
   break;
  case 'e': case 'E':
   if (end == *s || e_count++)
    goto done;
   break;
  case '-':
   if (!e_count)
    goto done;
   if (end == *s)
    goto done;
   if (plus_count || neg_count++)
    goto done;
   break;
  case '+':
   if (!e_count)
    goto done;
   if (end == *s)
    goto done;
   if (neg_count || plus_count++)
    goto done;
   break;
  default:
   goto done;
  }
 }
done:
 if (end != *s) {
  int len = end - *s;
  num = __builtin_alloca (len + 1);
  memcpy(num, *s, len);
  num[len] = 0;
  ret.type = T_NUM;
  ret.val = atof(num);
  *s = end;
 }
 return ret;
}

canonical_original_code:
static struct tok get_number_dec(char **s)
{
 char *end;
 struct tok ret;
 char *num;
 int dot_count = 0; int e_count = 0; int neg_count = 0; int plus_count = 0;
 ret.type = T_NONE;
 for (end = *s; ; end++) {
  switch (*end) {
  case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
   break;
  case '.':
   if (dot_count++)
    goto done;
   break;
  case 'e': case 'E':
   if (end == *s || e_count++)
    goto done;
   break;
  case '-':
   if (!e_count)
    goto done;
   if (end == *s)
    goto done;
   if (plus_count || neg_count++)
    goto done;
   break;
  case '+':
   if (!e_count)
    goto done;
   if (end == *s)
    goto done;
   if (neg_count || plus_count++)
    goto done;
   break;
  default:
   goto done;
  }
 }
done:
 if (end != *s) {
  int len = end - *s;
  num = __builtin_alloca (len + 1);
  memcpy(num, *s, len);
  num[len] = 0;
  ret.type = T_NUM;
  ret.val = atof(num);
  *s = end;
 }
 return ret;
}

variable_types:
num
char *

variable_types: len int


repo: danielverkamp/mathparse

original_code:
static void get_token(parse_ctx *ctx)
{
 char c;
 if (ctx->t.type == T_WORD) {
  free(ctx->t.str);
  ctx->t.str = ((void *)0);
 }
 ctx->t.type = T_NONE;
 do {
  ctx->t = get_number(ctx->s);
  if (ctx->t.type == T_NONE) {
   ctx->t = get_word(ctx->s);
  }
  if (ctx->t.type == T_NONE) {
   c = **ctx->s;
   (*ctx->s)++;
   switch (c) {
   case 0: ctx->t.type = T_EOF; break;
   case '+': ctx->t.type = T_PLUS; break;
   case '-': ctx->t.type = T_MINUS; break;
   case '*': ctx->t.type = T_MUL; break;
   case '/': ctx->t.type = T_DIV; break;
   case '(': ctx->t.type = T_LPAREN; break;
   case ')': ctx->t.type = T_RPAREN; break;
   case '^': ctx->t.type = T_POW; break;
   case '!': ctx->t.type = T_BANG; break;
   case '%': ctx->t.type = T_MOD; break;
   }
  }
 } while (ctx->t.type == T_NONE && c);
}

canonical_original_code:
static void get_token(struct parse_ctx *ctx)
{
 char c;
 if (ctx->t.type == T_WORD) {
  free(ctx->t.str);
  ctx->t.str = ((void *)0);
 }
 ctx->t.type = T_NONE;
 do {
  ctx->t = get_number(ctx->s);
  if (ctx->t.type == T_NONE) {
   ctx->t = get_word(ctx->s);
  }
  if (ctx->t.type == T_NONE) {
   c = **ctx->s;
   (*ctx->s)++;
   switch (c) {
   case 0: ctx->t.type = T_EOF; break;
   case '+': ctx->t.type = T_PLUS; break;
   case '-': ctx->t.type = T_MINUS; break;
   case '*': ctx->t.type = T_MUL; break;
   case '/': ctx->t.type = T_DIV; break;
   case '(': ctx->t.type = T_LPAREN; break;
   case ')': ctx->t.type = T_RPAREN; break;
   case '^': ctx->t.type = T_POW; break;
   case '!': ctx->t.type = T_BANG; break;
   case '%': ctx->t.type = T_MOD; break;
   }
  }
 } while (ctx->t.type == T_NONE && c);
}

variable_types: struct parse_ctx { struct tok { int type; double val; char *str; } t; char **s; } *

variable_types: c char

return_type: void

user_defined_types: struct parse_ctx { struct tok t; char **s; }


decompiled_code:
const char **__fastcall find_const(char *s2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = (const char **)&consts;
v3 = "e";
while ( strcasecmp(v3, s2) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func7(char *s2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = (const char **)&consts;
v3 = "e";
while ( strcasecmp(v3, s2) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return v2;
}

original_code:
static const constant *find_const(const char *name)
{
 const constant *c;
 for (c = consts; c->name; c++)
  if (!strcasecmp(c->name, name))
   return c;
 return ((void *)0);
}

canonical_original_code:
static struct constant *find_const(char *name)
{
 struct constant *c;
 for (c = consts; c->name; c++)
  if (!strcasecmp(c->name, name))
   return c;
 return ((void *)0);
}

variable_types:
name
char *

variable_types: struct constant { char *name; double val; } *


decompiled_code:
const char **__fastcall find_funct(char *s2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = (const char **)&functs;
v3 = "acos";
while ( strcasecmp(v3, s2) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func8(char *s2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = (const char **)&functs;
v3 = "acos";
while ( strcasecmp(v3, s2) )
{
v2 += 2;
v3 = *v2;
if ( !*v2 )
return 0LL;
}
return v2;
}

original_code:
static const funct *find_funct(const char *name)
{
 const funct *f;
 for (f = functs; f->name; f++)
  if (!strcasecmp(f->name, name))
   return f;
 return ((void *)0);
}

canonical_original_code:
static struct funct *find_funct(char *name)
{
 struct funct *f;
 for (f = functs; f->name; f++)
  if (!strcasecmp(f->name, name))
   return f;
 return ((void *)0);
}

variable_types: struct funct { char *name; double (*funct)(double); } *


decompiled_code:
double __fastcall rand_dbl(double a1)
{
return (double)rand() * a1 / 2147483647.0;
}

canonical_decompiled_code:
double __fastcall func9(double a1)
{
return (double)rand() * a1 / 2147483647.0;
}

original_code:
static double rand_dbl(double max)
{
 return (double)rand() * max / (double)2147483647;
}

variable_types: max double

return_type: double


roll

decompiled_code:
__int64 __fastcall roll(int a1, int a2)
{
int v2; // ebx
unsigned int v3; // er12

if ( a1 <= 0 || a2 <= 0 )
return 0;
if ( a1 > 100 )
return (unsigned int)-1;
v2 = 0;
v3 = 0;
do
{
v3 += rand() % a2 + 1;
++v2;
}
while ( a1 > v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
int v2; // ebx
unsigned int v3; // er12

if ( a1 <= 0 || a2 <= 0 )
return 0;
if ( a1 > 100 )
return (unsigned int)-1;
v2 = 0;
v3 = 0;
do
{
v3 += rand() % a2 + 1;
++v2;
}
while ( a1 > v2 );
return v3;
}

original_code:
static int roll(int x, int y)
{
 int ret = 0, i;
 if (x <= 0 || y <= 0)
  return 0;
 if (x > 100 || y > 2147483647)
  return -1;
 for (i = 0; i < x; i++)
  ret += (rand() % y) + 1;
 return ret;
}

canonical_original_code:
static int roll(int x, int y)
{
 int ret = 0; int i;
 if (x <= 0 || y <= 0)
  return 0;
 if (x > 100 || y > 2147483647)
  return -1;
 for (i = 0; i < x; i++)
  ret += (rand() % y) + 1;
 return ret;
}

variable_types: x int

variable_types: y int

variable_types: ret int

variable_types: i int

return_type: int


err

decompiled_code:
void __noreturn err()
{
fwrite("parse error\n", 1uLL, 0xCuLL, stderr);
}

canonical_decompiled_code:
void __noreturn func11()
{
fwrite("parse error\n", 1uLL, 0xCuLL, stderr);
}

original_code:
static void err()
{
 fprintf(stderr, "parse error\n");
}


power

decompiled_code:
double __fastcall power(__int64 a1, double a2)
{
int v2; // eax
double v3; // rbp
char *v5; // rbp
const char **v6; // rax
const char **v7; // rbp
double (*v8)(void); // rbp

v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 8 )
{
v3 = *(double *)(a1 + 8);
get_token(a1, a2);
}
else if ( v2 == 10 )
{
v5 = *(char **)(a1 + 16);
v6 = find_const(v5);
if ( v6 )
{
v3 = *((double *)v6 + 1);
}
else
{
v7 = find_funct(v5);
if ( !v7 )
err();
get_token(a1, a2);
get_token(a1, a2);
v8 = (double (*)(void))v7[1];
expr(a1);
a2 = v8();
v3 = a2;
}
get_token(a1, a2);
}
else
{
if ( v2 != 6 )
err();
get_token(a1, a2);
v3 = expr(a1);
get_token(a1, v3);
}
return v3;
}

canonical_decompiled_code:
double __fastcall func12(__int64 a1, double a2)
{
int v2; // eax
double v3; // rbp
char *v5; // rbp
const char **v6; // rax
const char **v7; // rbp
double (*v8)(void); // rbp

v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 8 )
{
v3 = *(double *)(a1 + 8);
func6(a1, a2);
}
else if ( v2 == 10 )
{
v5 = *(char **)(a1 + 16);
v6 = func7(v5);
if ( v6 )
{
v3 = *((double *)v6 + 1);
}
else
{
v7 = func8(v5);
if ( !v7 )
func11();
func6(a1, a2);
func6(a1, a2);
v8 = (double (*)(void))v7[1];
func16(a1);
a2 = v8();
v3 = a2;
}
func6(a1, a2);
}
else
{
if ( v2 != 6 )
func11();
func6(a1, a2);
v3 = func16(a1);
func6(a1, v3);
}
return v3;
}

original_code:
static double power(parse_ctx *ctx)
{
 double ret;
 switch (ctx->t.type) {
 case T_NUM:
  ret = ctx->t.val;
  get_token(ctx);
  return ret;
 case T_WORD:
 {
  const constant *c = find_const(ctx->t.str);
  const funct *f = ((void *)0);
  if (!c) {
   f = find_funct(ctx->t.str);
   if (!f) {
    err();
    return 0.0;
   }
   get_token(ctx);
   get_token(ctx);
   ret = f->funct(expr(ctx));
  } else {
   ret = c->val;
  }
  get_token(ctx);
  return ret;
 }
 case T_LPAREN:
  get_token(ctx);
  ret = expr(ctx);
  get_token(ctx);
  return ret;
 default:
  err();
  return 0.0;
 }
}

canonical_original_code:
static double power(struct parse_ctx *ctx)
{
 double ret;
 switch (ctx->t.type) {
 case T_NUM:
  ret = ctx->t.val;
  get_token(ctx);
  return ret;
 case T_WORD:
 {
  struct constant *c = find_const(ctx->t.str);
  struct funct *f = ((void *)0);
  if (!c) {
   f = find_funct(ctx->t.str);
   if (!f) {
    err();
    return 0.0;
   }
   get_token(ctx);
   get_token(ctx);
   ret = f->funct(expr(ctx));
  } else {
   ret = c->val;
  }
  get_token(ctx);
  return ret;
 }
 case T_LPAREN:
  get_token(ctx);
  ret = expr(ctx);
  get_token(ctx);
  return ret;
 default:
  err();
  return 0.0;
 }
}

variable_types: ret double

user_defined_types: struct constant { char *name; double val; }

user_defined_types: struct funct { char *name; double (*funct)(double); }

user_defined_types: struct tok { int type; double val; char *str; }


unary

decompiled_code:
double __fastcall unary(__int64 a1, double a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
const __int32_t **v4; // rdx
char *v5; // rax
double x; // [rsp+8h] [rbp-10h]

v2 = power(a1, a2);
for ( x = v2; ; x = (double)(int)roll((int)x, (int)v2) )
{
while ( *(_DWORD *)a1 == 9 )
{
get_token(a1, v2);
v3 = power(a1, v2);
v2 = pow(x, v3);
x = v2;
}
if ( *(_DWORD *)a1 != 10 )
break;
v4 = _ctype_tolower_loc();
v5 = *(char **)(a1 + 16);
if ( (*v4)[*v5] != 100 || v5[1] )
break;
get_token(a1, v2);
v2 = power(a1, v2);
}
return x;
}

canonical_decompiled_code:
double __fastcall func13(__int64 a1, double a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
const __int32_t **v4; // rdx
char *v5; // rax
double x; // [rsp+8h] [rbp-10h]

v2 = func12(a1, a2);
for ( x = v2; ; x = (double)(int)func10((int)x, (int)v2) )
{
while ( *(_DWORD *)a1 == 9 )
{
func6(a1, v2);
v3 = func12(a1, v2);
v2 = pow(x, v3);
x = v2;
}
if ( *(_DWORD *)a1 != 10 )
break;
v4 = _ctype_tolower_loc();
v5 = *(char **)(a1 + 16);
if ( (*v4)[*v5] != 100 || v5[1] )
break;
func6(a1, v2);
v2 = func12(a1, v2);
}
return x;
}

original_code:
static double unary(parse_ctx *ctx)
{
 double ret;
 ret = power(ctx);
 while (ctx->t.type == T_POW ||
  (ctx->t.type == T_WORD &&
  tolower(ctx->t.str[0]) == 'd' &&
  ctx->t.str[1] == '\0'))
 {
  int type = ctx->t.type;
  get_token(ctx);
  if (type == T_POW)
   ret = pow(ret, power(ctx));
  else
   ret = roll(ret, power(ctx));
 }
 return ret;
}

canonical_original_code:
static double unary(struct parse_ctx *ctx)
{
 double ret;
 ret = power(ctx);
 while (ctx->t.type == T_POW ||
  (ctx->t.type == T_WORD &&
  tolower(ctx->t.str[0]) == 'd' &&
  ctx->t.str[1] == '\0'))
 {
  int type = ctx->t.type;
  get_token(ctx);
  if (type == T_POW)
   ret = pow(ret, power(ctx));
  else
   ret = roll(ret, power(ctx));
 }
 return ret;
}

variable_types: type int


factor

decompiled_code:
double __fastcall factor(int *a1, double a2)
{
int v2; // eax
int v3; // ebp
double v4; // xmm0_8
double v5; // xmm1_8
__m128d v6; // xmm1
double i; // [rsp+8h] [rbp-20h]

v2 = *a1;
if ( (unsigned int)(*a1 - 2) > 1 )
{
v3 = 1;
}
else
{
v3 = 1;
do
{
if ( v2 == 3 )
v3 = -v3;
get_token((__int64)a1, a2);
v2 = *a1;
}
while ( (unsigned int)(*a1 - 2) <= 1 );
}
v4 = unary((__int64)a1, a2);
for ( i = (double)v3 * v4; *a1 == 11; i = v4 )
{
get_token((__int64)a1, v4);
v5 = i;
if ( COERCE_DOUBLE(*(_QWORD *)&i & LC11) < 4.503599627370496e15 )
{
v6.m128d_f64[0] = (double)(int)i;
*(_QWORD *)&v5 = ~LC11 & *(_QWORD *)&i | COERCE_UNSIGNED_INT64(
v6.m128d_f64[0]
- COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(
v6,
(__m128d)*(unsigned __int64 *)&i) & LC4));
}
v4 = factorial(v5);
}
return i;
}

original_code:
static double factor(parse_ctx *ctx)
{
 double ret;
 int sign = 1;
 while (ctx->t.type == T_MINUS || ctx->t.type == T_PLUS) {
  if (ctx->t.type == T_MINUS)
   sign = -sign;
  get_token(ctx);
 }
 ret = sign * unary(ctx);
 while (ctx->t.type == T_BANG) {
  get_token(ctx);
  ret = factorial(floor(ret));
 }
 return ret;
}

canonical_original_code:
static double factor(struct parse_ctx *ctx)
{
 double ret;
 int sign = 1;
 while (ctx->t.type == T_MINUS || ctx->t.type == T_PLUS) {
  if (ctx->t.type == T_MINUS)
   sign = -sign;
  get_token(ctx);
 }
 ret = sign * unary(ctx);
 while (ctx->t.type == T_BANG) {
  get_token(ctx);
  ret = factorial(floor(ret));
 }
 return ret;
}

variable_types: sign int


term

decompiled_code:
double __fastcall term(int *a1, double a2)
{
double v2; // xmm0_8
int i; // ebx
double v5; // [rsp+8h] [rbp-20h]

v2 = factor(a1, a2);
v5 = v2;
for ( i = *a1; (unsigned int)(*a1 - 4) <= 1 || i == 12; i = *a1 )
{
get_token((__int64)a1, v2);
switch ( i )
{
case 4:
v2 = factor(a1, v2) * v5;
v5 = v2;
break;
case 5:
v2 = factor(a1, v2);
v5 = v5 / v2;
break;
case 12:
v2 = factor(a1, v2);
v5 = (double)(int)((unsigned int)(int)v5 % (__int64)(unsigned int)(int)v2);
break;
}
}
return v5;
}

canonical_decompiled_code:
double __fastcall func15(int *a1, double a2)
{
double v2; // xmm0_8
int i; // ebx
double v5; // [rsp+8h] [rbp-20h]

v2 = func14(a1, a2);
v5 = v2;
for ( i = *a1; (unsigned int)(*a1 - 4) <= 1 || i == 12; i = *a1 )
{
func6((__int64)a1, v2);
switch ( i )
{
case 4:
v2 = func14(a1, v2) * v5;
v5 = v2;
break;
case 5:
v2 = func14(a1, v2);
v5 = v5 / v2;
break;
case 12:
v2 = func14(a1, v2);
v5 = (double)(int)((unsigned int)(int)v5 % (__int64)(unsigned int)(int)v2);
break;
}
}
return v5;
}

original_code:
static double term(parse_ctx *ctx)
{
 double ret;
 int op;
 ret = factor(ctx);
 while (ctx->t.type == T_MUL || ctx->t.type == T_DIV || ctx->t.type == T_MOD) {
  op = ctx->t.type;
  get_token(ctx);
  if (op == T_MUL)
   ret *= factor(ctx);
  else if (op == T_DIV)
   ret /= factor(ctx);
  else if (op == T_MOD)
   ret = (double)((long long)ret % (long long)factor(ctx));
 }
 return ret;
}

canonical_original_code:
static double term(struct parse_ctx *ctx)
{
 double ret;
 int op;
 ret = factor(ctx);
 while (ctx->t.type == T_MUL || ctx->t.type == T_DIV || ctx->t.type == T_MOD) {
  op = ctx->t.type;
  get_token(ctx);
  if (op == T_MUL)
   ret *= factor(ctx);
  else if (op == T_DIV)
   ret /= factor(ctx);
  else if (op == T_MOD)
   ret = (double)((long long)ret % (long long)factor(ctx));
 }
 return ret;
}

variable_types: op int


expr

decompiled_code:
double __fastcall expr(int *a1, double a2)
{
double v2; // xmm0_8
int i; // ebx
double v5; // [rsp+8h] [rbp-20h]

v2 = term(a1, a2);
v5 = v2;
for ( i = *a1; (unsigned int)(*a1 - 2) <= 1; i = *a1 )
{
get_token((__int64)a1, v2);
if ( i == 2 )
{
v2 = term(a1, v2) + v5;
v5 = v2;
}
else if ( i == 3 )
{
v2 = term(a1, v2);
v5 = v5 - v2;
}
}
return v5;
}

canonical_decompiled_code:
double __fastcall func16(int *a1, double a2)
{
double v2; // xmm0_8
int i; // ebx
double v5; // [rsp+8h] [rbp-20h]

v2 = func15(a1, a2);
v5 = v2;
for ( i = *a1; (unsigned int)(*a1 - 2) <= 1; i = *a1 )
{
func6((__int64)a1, v2);
if ( i == 2 )
{
v2 = func15(a1, v2) + v5;
v5 = v2;
}
else if ( i == 3 )
{
v2 = func15(a1, v2);
v5 = v5 - v2;
}
}
return v5;
}

original_code:
static double expr(parse_ctx *ctx)
{
 double ret;
 int op;
 ret = term(ctx);
 while (ctx->t.type == T_PLUS || ctx->t.type == T_MINUS) {
  op = ctx->t.type;
  get_token(ctx);
  if (op == T_PLUS)
   ret += term(ctx);
  else if (op == T_MINUS)
   ret -= term(ctx);
 }
 return ret;
}

canonical_original_code:
static double expr(struct parse_ctx *ctx)
{
 double ret;
 int op;
 ret = term(ctx);
 while (ctx->t.type == T_PLUS || ctx->t.type == T_MINUS) {
  op = ctx->t.type;
  get_token(ctx);
  if (op == T_PLUS)
   ret += term(ctx);
  else if (op == T_MINUS)
   ret -= term(ctx);
 }
 return ret;
}


parse

decompiled_code:
double __fastcall parse(__int64 a1, double a2)
{
__int64 v3; // [rsp+8h] [rbp-30h] BYREF
int v4[4]; // [rsp+10h] [rbp-28h] BYREF
__int64 v5; // [rsp+20h] [rbp-18h]
__int64 *v6; // [rsp+28h] [rbp-10h]

v3 = a1;
v5 = 0LL;
v6 = &v3;
get_token((__int64)v4, a2);
return expr(v4, a2);
}

original_code:
double parse(const char *s)
{
 parse_ctx ctx;
 ctx.t.str = ((void *)0);
 ctx.s = &s;
 get_token(&ctx);
 return expr(&ctx);
}

canonical_original_code:
double parse(char *s)
{
 struct parse_ctx ctx;
 ctx.t.str = ((void *)0);
 ctx.s = &s;
 get_token(&ctx);
 return expr(&ctx);
}

variable_types:
s
char *

variable_types: struct parse_ctx { struct tok { int type; double val; char *str; } t; char **s; }


repo: danihegglin/ChronosAndroid

decompiled_code:
void __fastcall ndn_digest_destroy(void **a1)
{
void *v2; // rdi

v2 = *a1;
if ( v2 )
{
free(v2);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func0(void **a1)
{
void *v2; // rdi

v2 = *a1;
if ( v2 )
{
free(v2);
*a1 = 0LL;
}
}

original_code:
void
ndn_digest_destroy(struct ndn_digest **pd)
{
    if (*pd != ((void *)0)) {
        free(*pd);
        *pd = ((void *)0);
    }
}

variable_types: struct ndn_digest { enum ndn_digest_id { NDN_DIGEST_DEFAULT=0, NDN_DIGEST_SHA1=1, NDN_DIGEST_SHA224=2, NDN_DIGEST_SHA256=3, NDN_DIGEST_SHA384=4, NDN_DIGEST_SHA512=5 } id; unsigned short sz; short ready; struct SHA256state_st { unsigned int h[8]; unsigned int Nl; unsigned int data[16]; unsigned int num; } sha256_ctx; } **

user_defined_types: struct ndn_digest { enum ndn_digest_id id; unsigned short sz; short ready; struct SHA256state_st sha256_ctx; }


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

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

original_code:
enum ndn_digest_id
ndn_digest_getid(struct ndn_digest *d)
{
    return(d->id);
}

variable_types: struct ndn_digest { enum ndn_digest_id { NDN_DIGEST_DEFAULT=0, NDN_DIGEST_SHA1=1, NDN_DIGEST_SHA224=2, NDN_DIGEST_SHA256=3, NDN_DIGEST_SHA384=4, NDN_DIGEST_SHA512=5 } id; unsigned short sz; short ready; struct SHA256state_st { unsigned int h[8]; unsigned int Nl; unsigned int data[16]; unsigned int num; } sha256_ctx; } *


decompiled_code:
__int64 __fastcall ndn_digest_size(__int64 a1)
{
return *(unsigned __int16 *)(a1 + 4);
}

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

original_code:
size_t
ndn_digest_size(struct ndn_digest *d)
{
    return(d->sz);
}

canonical_original_code:
unsigned long ndn_digest_size(struct ndn_digest *d)
{
    return(d->sz);
}

return_type: unsigned long

user_defined_types: enum ndn_digest_id { NDN_DIGEST_DEFAULT=0, NDN_DIGEST_SHA1=1, NDN_DIGEST_SHA224=2, NDN_DIGEST_SHA256=3, NDN_DIGEST_SHA384=4, NDN_DIGEST_SHA512=5 }

user_defined_types: struct SHA256state_st { unsigned int h[8]; unsigned int Nl; unsigned int data[16]; unsigned int num; }


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

result = SHA256_Init(a1 + 8);
*(_WORD *)(a1 + 6) = 1;
return result;
}

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

result = SHA256_Init(a1 + 8);
*(_WORD *)(a1 + 6) = 1;
return result;
}

original_code:
void
ndn_digest_init(struct ndn_digest *d)
{
    SHA256_Init(&d->sha256_ctx);
    d->ready = 1;
}


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

if ( *(_WORD *)(a1 + 6) == 1 )
result = (unsigned int)-((unsigned int)SHA256_Update(a1 + 8) != 1);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( *(_WORD *)(a1 + 6) == 1 )
result = (unsigned int)-((unsigned int)SHA256_Update(a1 + 8) != 1);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int
ndn_digest_update(struct ndn_digest *d, const void *data, size_t size)
{
    int res;
    if (d->ready != 1)
        return(-1);
    res = SHA256_Update(&d->sha256_ctx, data, size);
    return((res == 1) ? 0 : -1);
}

canonical_original_code:
int ndn_digest_update(struct ndn_digest *d, void *data, unsigned long size)
{
    int res;
    if (d->ready != 1)
        return(-1);
    res = SHA256_Update(&d->sha256_ctx, data, size);
    return((res == 1) ? 0 : -1);
}

variable_types:
data
void *

variable_types:
size
unsigned long

variable_types: res int


decompiled_code:
__int64 __fastcall ndn_digest_final(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // eax

if ( *(unsigned __int16 *)(a1 + 4) != a3 )
return 0xFFFFFFFFLL;
if ( *(_WORD *)(a1 + 6) != 1 )
return 0xFFFFFFFFLL;
v3 = SHA256_Final(a2, a1 + 8);
*(_WORD *)(a1 + 6) = 0;
return (unsigned int)-(v3 != 1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // eax

if ( *(unsigned __int16 *)(a1 + 4) != a3 )
return 0xFFFFFFFFLL;
if ( *(_WORD *)(a1 + 6) != 1 )
return 0xFFFFFFFFLL;
v3 = SHA256_Final(a2, a1 + 8);
*(_WORD *)(a1 + 6) = 0;
return (unsigned int)-(v3 != 1);
}

original_code:
int
ndn_digest_final(struct ndn_digest *d, unsigned char *result, size_t digest_size)
{
    int res;
    if (digest_size != d->sz) return(-1);
    if (d->ready != 1)
        return(-1);
    res = SHA256_Final(result, &d->sha256_ctx);
    d->ready = 0;
    return((res == 1) ? 0 : -1);
}

canonical_original_code:
int ndn_digest_final(struct ndn_digest *d, unsigned char *result, unsigned long digest_size)
{
    int res;
    if (digest_size != d->sz) return(-1);
    if (d->ready != 1)
        return(-1);
    res = SHA256_Final(result, &d->sha256_ctx);
    d->ready = 0;
    return((res == 1) ? 0 : -1);
}

variable_types:
result
unsigned char *


decompiled_code:
void *ndn_charbuf_create()
{
return calloc(1uLL, 0x18uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(1uLL, 0x18uLL);
}

original_code:
struct ndn_charbuf *
ndn_charbuf_create(void)
{
    struct ndn_charbuf *c;
    c = calloc(1, sizeof(*c));
    return(c);
}

canonical_original_code:
struct ndn_charbuf *ndn_charbuf_create()
{
    struct ndn_charbuf *c;
    c = calloc(1, sizeof(*c));
    return(c);
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *


decompiled_code:
void __fastcall ndn_charbuf_destroy(_QWORD **a1)
{
_QWORD *v1; // rbp
void *v3; // rdi

v1 = *a1;
if ( *a1 )
{
v3 = (void *)v1[2];
if ( v3 )
free(v3);
free(v1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func1(_QWORD **a1)
{
_QWORD *v1; // rbp
void *v3; // rdi

v1 = *a1;
if ( *a1 )
{
v3 = (void *)v1[2];
if ( v3 )
free(v3);
free(v1);
*a1 = 0LL;
}
}

original_code:
void
ndn_charbuf_destroy(struct ndn_charbuf **cbp)
{
    struct ndn_charbuf *c = *cbp;
    if (c != ((void *)0)) {
        if (c->buf != ((void *)0))
            free(c->buf);
        free(c);
        *cbp = ((void *)0);
    }
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } **


original_code:
unsigned char *
ndn_charbuf_reserve(struct ndn_charbuf *c, size_t n)
{
    size_t newsz = n + c->length;
    unsigned char *buf = c->buf;
    if (newsz < n)
        return(((void *)0));
    if (newsz > c->limit) {
        if (2 * c->limit > newsz)
            newsz = 2 * c->limit;
        buf = realloc(c->buf, newsz);
        if (buf == ((void *)0))
            return(((void *)0));
        memset(buf + c->limit, 0, newsz - c->limit);
        c->buf = buf;
        c->limit = newsz;
    }
    buf += c->length;
    return(buf);
}

canonical_original_code:
unsigned char *ndn_charbuf_reserve(struct ndn_charbuf *c, unsigned long n)
{
    unsigned long newsz = n + c->length;
    unsigned char *buf = c->buf;
    if (newsz < n)
        return(((void *)0));
    if (newsz > c->limit) {
        if (2 * c->limit > newsz)
            newsz = 2 * c->limit;
        buf = realloc(c->buf, newsz);
        if (buf == ((void *)0))
            return(((void *)0));
        memset(buf + c->limit, 0, newsz - c->limit);
        c->buf = buf;
        c->limit = newsz;
    }
    buf += c->length;
    return(buf);
}

variable_types:
n
unsigned long

variable_types:
newsz
unsigned long

variable_types:
buf
unsigned char *

return_type: unsigned char *

user_defined_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; }


decompiled_code:
__int64 __fastcall ndn_charbuf_append(_QWORD *a1, const void *a2, size_t a3)
{
char *v4; // rax

v4 = ndn_charbuf_reserve((__int64)a1, a3);
if ( !v4 )
return 0xFFFFFFFFLL;
memcpy(v4, a2, a3);
*a1 += a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, const void *a2, size_t a3)
{
char *v4; // rax

v4 = func2((__int64)a1, a3);
if ( !v4 )
return 0xFFFFFFFFLL;
memcpy(v4, a2, a3);
*a1 += a3;
return 0LL;
}

original_code:
int
ndn_charbuf_append(struct ndn_charbuf *c, const void *p, size_t n)
{
    unsigned char *dst = ndn_charbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    memcpy(dst, p, n);
    c->length += n;
    return(0);
}

canonical_original_code:
int ndn_charbuf_append(struct ndn_charbuf *c, void *p, unsigned long n)
{
    unsigned char *dst = ndn_charbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    memcpy(dst, p, n);
    c->length += n;
    return(0);
}

variable_types:
p
void *

variable_types:
dst
unsigned char *


decompiled_code:
__int64 __fastcall ndn_charbuf_append_value(_QWORD *a1, unsigned int a2, unsigned int a3)
{
__int64 v4; // r12
char *v5; // rax
int v6; // ecx

if ( a3 > 4 )
return 0xFFFFFFFFLL;
v4 = a3;
v5 = ndn_charbuf_reserve((__int64)a1, a3);
if ( !v5 )
return 0xFFFFFFFFLL;
if ( a3 )
{
v6 = 8 * a3 - 8;
do
{
*v5 = a2 >> v6;
v6 -= 8;
++v5;
}
while ( v6 != -8 );
}
*a1 += v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, unsigned int a2, unsigned int a3)
{
__int64 v4; // r12
char *v5; // rax
int v6; // ecx

if ( a3 > 4 )
return 0xFFFFFFFFLL;
v4 = a3;
v5 = func2((__int64)a1, a3);
if ( !v5 )
return 0xFFFFFFFFLL;
if ( a3 )
{
v6 = 8 * a3 - 8;
do
{
*v5 = a2 >> v6;
v6 -= 8;
++v5;
}
while ( v6 != -8 );
}
*a1 += v4;
return 0LL;
}

original_code:
int
ndn_charbuf_append_value(struct ndn_charbuf *c, unsigned val, unsigned n)
{
    unsigned char *dst;
    unsigned i;
    if (n > sizeof(val))
        return(-1);
    dst = ndn_charbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    for (i = 0; i < n; i++)
        dst[i] = (unsigned char)(val >> (8 * (n-1-i)));
    c->length += n;
    return(0);
}

canonical_original_code:
int ndn_charbuf_append_value(struct ndn_charbuf *c, unsigned int val, unsigned int n)
{
    unsigned char *dst;
    unsigned int i;
    if (n > sizeof(val))
        return(-1);
    dst = ndn_charbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    for (i = 0; i < n; i++)
        dst[i] = (unsigned char)(val >> (8 * (n-1-i)));
    c->length += n;
    return(0);
}

variable_types:
val
unsigned int

variable_types:
n
unsigned int

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall ndn_charbuf_append_charbuf(_QWORD *a1, __int64 a2)
{
return ndn_charbuf_append(a1, *(const void **)(a2 + 16), *(_QWORD *)a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 a2)
{
return func4(a1, *(const void **)(a2 + 16), *(_QWORD *)a2);
}

original_code:
int
ndn_charbuf_append_charbuf(struct ndn_charbuf *c, const struct ndn_charbuf *in)
{
  return(ndn_charbuf_append(c, in->buf, in->length));
}

canonical_original_code:
int ndn_charbuf_append_charbuf(struct ndn_charbuf *c, struct ndn_charbuf *in)
{
  return(ndn_charbuf_append(c, in->buf, in->length));
}

variable_types:
in
struct ndn_charbuf *


decompiled_code:
__int64 __fastcall ndn_charbuf_append_string(_QWORD *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
return ndn_charbuf_append(a1, a2, v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
return func4(a1, a2, v2);
}

original_code:
int
ndn_charbuf_append_string(struct ndn_charbuf *c, const char *s)
{
  return(ndn_charbuf_append(c, s, strlen(s)));
}

canonical_original_code:
int ndn_charbuf_append_string(struct ndn_charbuf *c, char *s)
{
  return(ndn_charbuf_append(c, s, strlen(s)));
}

variable_types:
s
char *


decompiled_code:
int ndn_charbuf_putf(unsigned __int64 *a1, const char *a2, ...)
{
size_t v3; // rax
char *v4; // rdi
int result; // eax
unsigned __int64 v6; // rdx
char *v7; // rdi
unsigned __int64 v8; // rdx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v3 = strlen(a2);
v4 = ndn_charbuf_reserve((__int64)a1, v3 + 10);
if ( !v4 )
return -1;
va_start(arg, a2);
result = vsnprintf(v4, a1[1] - *a1, a2, arg);
if ( result >= 0 )
{
v6 = *a1 + result;
if ( v6 >= a1[1] )
{
v7 = ndn_charbuf_reserve((__int64)a1, result + 1);
if ( v7 )
{
va_end(arg);
va_start(arg, a2);
result = vsnprintf(v7, a1[1] - *a1, a2, arg);
v8 = *a1 + result;
if ( v8 >= a1[1] )
result = -1;
else
*a1 = v8;
}
else
{
result = -1;
}
}
else
{
*a1 = v6;
}
}
return result;
}

canonical_decompiled_code:
int func8(unsigned __int64 *a1, const char *a2, ...)
{
size_t v3; // rax
char *v4; // rdi
int result; // eax
unsigned __int64 v6; // rdx
char *v7; // rdi
unsigned __int64 v8; // rdx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v3 = strlen(a2);
v4 = func2((__int64)a1, v3 + 10);
if ( !v4 )
return -1;
va_start(arg, a2);
result = vsnprintf(v4, a1[1] - *a1, a2, arg);
if ( result >= 0 )
{
v6 = *a1 + result;
if ( v6 >= a1[1] )
{
v7 = func2((__int64)a1, result + 1);
if ( v7 )
{
va_end(arg);
va_start(arg, a2);
result = vsnprintf(v7, a1[1] - *a1, a2, arg);
v8 = *a1 + result;
if ( v8 >= a1[1] )
result = -1;
else
*a1 = v8;
}
else
{
result = -1;
}
}
else
{
*a1 = v6;
}
}
return result;
}

original_code:
int
ndn_charbuf_putf(struct ndn_charbuf *c, const char *fmt, ...)
{
    int sz;
    va_list ap;
    char *buf;
    buf = (char *)ndn_charbuf_reserve(c, strlen(fmt) + 10);
    if (buf == ((void *)0)) return(-1);
    __builtin_va_start(ap,fmt);
    sz = vsnprintf(buf, c->limit - c->length, fmt, ap);
    __builtin_va_end(ap);
    if (sz < 0)
        return(sz);
    if (c->length + sz < c->limit) {
        c->length += sz;
        return(sz);
    }
    buf = (char *)ndn_charbuf_reserve(c, sz + 1);
    if (buf == ((void *)0)) return(-1);
    __builtin_va_start(ap,fmt);
    sz = vsnprintf(buf, c->limit - c->length, fmt, ap);
    __builtin_va_end(ap);
    if (c->length + sz < c->limit) {
        c->length += sz;
        return(sz);
    }
    return(-1);
}

canonical_original_code:
int ndn_charbuf_putf(struct ndn_charbuf *c, char *fmt, ...)
{
    int sz;
    __builtin_va_list ap;
    char *buf;
    buf = (char *)ndn_charbuf_reserve(c, strlen(fmt) + 10);
    if (buf == ((void *)0)) return(-1);
    __builtin_va_start(ap,fmt);
    sz = vsnprintf(buf, c->limit - c->length, fmt, ap);
    __builtin_va_end(ap);
    if (sz < 0)
        return(sz);
    if (c->length + sz < c->limit) {
        c->length += sz;
        return(sz);
    }
    buf = (char *)ndn_charbuf_reserve(c, sz + 1);
    if (buf == ((void *)0)) return(-1);
    __builtin_va_start(ap,fmt);
    sz = vsnprintf(buf, c->limit - c->length, fmt, ap);
    __builtin_va_end(ap);
    if (c->length + sz < c->limit) {
        c->length += sz;
        return(sz);
    }
    return(-1);
}

variable_types:
fmt
char *

variable_types: sz int

variable_types:
buf
char *


decompiled_code:
__int64 __fastcall ndn_charbuf_append_datetime(_QWORD *a1, time_t a2, unsigned int a3)
{
struct tm *v4; // rax
unsigned int v5; // eax
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // rax
time_t timer; // [rsp+8h] [rbp-80h] BYREF
struct tm tp; // [rsp+10h] [rbp-78h] BYREF
char s[56]; // [rsp+50h] [rbp-38h] BYREF

timer = a2;
v4 = gmtime_r(&timer, &tp);
v5 = strftime(s, 0x20uLL, "%FT%T", v4);
if ( v5 > 0x1F )
return 0xFFFFFFFFLL;
v6 = v5;
v7 = v5;
if ( !a3 )
{
LABEL_7:
s[v7] = 90;
return ndn_charbuf_append(a1, s, (int)(v7 + 1));
}
if ( a3 > 0x3B9AC9FF )
return 0xFFFFFFFFLL;
snprintf(&s[v5], 32LL - (int)v5, ".%09d", a3);
LODWORD(v8) = v6 + 10;
if ( v6 + 10 <= 0x1F )
{
v8 = (int)v8;
do
v7 = v8--;
while ( s[v8] == 48 );
goto LABEL_7;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, time_t a2, unsigned int a3)
{
struct tm *v4; // rax
unsigned int v5; // eax
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // rax
time_t timer; // [rsp+8h] [rbp-80h] BYREF
struct tm tp; // [rsp+10h] [rbp-78h] BYREF
char s[56]; // [rsp+50h] [rbp-38h] BYREF

timer = a2;
v4 = gmtime_r(&timer, &tp);
v5 = strftime(s, 0x20uLL, "%FT%T", v4);
if ( v5 > 0x1F )
return 0xFFFFFFFFLL;
v6 = v5;
v7 = v5;
if ( !a3 )
{
LABEL_7:
s[v7] = 90;
return func4(a1, s, (int)(v7 + 1));
}
if ( a3 > 0x3B9AC9FF )
return 0xFFFFFFFFLL;
snprintf(&s[v5], 32LL - (int)v5, ".%09d", a3);
LODWORD(v8) = v6 + 10;
if ( v6 + 10 <= 0x1F )
{
v8 = (int)v8;
do
v7 = v8--;
while ( s[v8] == 48 );
goto LABEL_7;
}
return 0xFFFFFFFFLL;
}

original_code:
int
ndn_charbuf_append_datetime(struct ndn_charbuf *c, time_t secs, int nsecs)
{
    char timestring[32];
    int timelen;
    struct tm time_tm;
    int res;
    timelen = strftime(timestring, sizeof(timestring),
                       "%FT%T", gmtime_r(&secs, &time_tm));
    if (timelen >= sizeof(timestring))
        return(-1);
    if (nsecs != 0) {
        if (nsecs < 0 || nsecs >= 1000000000)
            return(-1);
        timelen += snprintf(&timestring[timelen], sizeof(timestring) - timelen,
                            ".%09d", nsecs);
        if (timelen >= sizeof(timestring))
            return(-1);
        while (timestring[timelen - 1] == '0') timelen--;
    }
    timestring[timelen++] = 'Z';
    res = ndn_charbuf_append(c, timestring, timelen);
    return (res);
}

canonical_original_code:
int ndn_charbuf_append_datetime(struct ndn_charbuf *c, long secs, int nsecs)
{
    char timestring[32];
    int timelen;
    struct tm time_tm;
    int res;
    timelen = strftime(timestring, sizeof(timestring),
                       "%FT%T", gmtime_r(&secs, &time_tm));
    if (timelen >= sizeof(timestring))
        return(-1);
    if (nsecs != 0) {
        if (nsecs < 0 || nsecs >= 1000000000)
            return(-1);
        timelen += snprintf(&timestring[timelen], sizeof(timestring) - timelen,
                            ".%09d", nsecs);
        if (timelen >= sizeof(timestring))
            return(-1);
        while (timestring[timelen - 1] == '0') timelen--;
    }
    timestring[timelen++] = 'Z';
    res = ndn_charbuf_append(c, timestring, timelen);
    return (res);
}

variable_types: secs long

variable_types: nsecs int

variable_types:
timestring
char [32]

variable_types: timelen int

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

variable_types: res int


decompiled_code:
char *__fastcall ndn_charbuf_as_string(__int64 a1)
{
char *result; // rax

result = ndn_charbuf_reserve(a1, 1LL);
if ( result )
{
*result = 0;
result = *(char **)(a1 + 16);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func10(__int64 a1)
{
char *result; // rax

result = func2(a1, 1LL);
if ( result )
{
*result = 0;
result = *(char **)(a1 + 16);
}
return result;
}

original_code:
char *
ndn_charbuf_as_string(struct ndn_charbuf *c)
{
    unsigned char *r;
    r = ndn_charbuf_reserve(c, 1);
    if (r == ((void *)0))
        return(((void *)0));
    r[0] = 0;
    return((char *)c->buf);
}

canonical_original_code:
char *ndn_charbuf_as_string(struct ndn_charbuf *c)
{
    unsigned char *r;
    r = ndn_charbuf_reserve(c, 1);
    if (r == ((void *)0))
        return(((void *)0));
    r[0] = 0;
    return((char *)c->buf);
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *

variable_types:
r
unsigned char *

return_type: char *


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

if ( !*((_QWORD *)a1 + 1) )
return *((_QWORD *)a2 + 1) != 0LL;
if ( !*((_QWORD *)a2 + 1) )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *(_DWORD *)a1 != *(_DWORD *)a2 )
result = 2 * (unsigned int)(*(_DWORD *)a1 >= *(_DWORD *)a2) - 1;
return result;
}

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

if ( !*((_QWORD *)a1 + 1) )
return *((_QWORD *)a2 + 1) != 0LL;
if ( !*((_QWORD *)a2 + 1) )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *(_DWORD *)a1 != *(_DWORD *)a2 )
result = 2 * (unsigned int)(*(_DWORD *)a1 >= *(_DWORD *)a2) - 1;
return result;
}

original_code:
static int
qsort_compare_dict_indices(const void *x, const void *y)
{
    const struct ndn_dict_entry *ex = x;
    const struct ndn_dict_entry *ey = y;
    if (ex->name == ((void *)0))
        return ((ey->name == ((void *)0)) ? 0 : 1);
    if (ey->name == ((void *)0)) return (-1);
    if (ex->index == ey->index) return (0);
    return ((ex->index < ey->index) ? -1 : 1);
}

canonical_original_code:
static int qsort_compare_dict_indices(void *x, void *y)
{
    struct ndn_dict_entry *ex = x;
    struct ndn_dict_entry *ey = y;
    if (ex->name == ((void *)0))
        return ((ey->name == ((void *)0)) ? 0 : 1);
    if (ey->name == ((void *)0)) return (-1);
    if (ex->index == ey->index) return (0);
    return ((ex->index < ey->index) ? -1 : 1);
}

variable_types:
x
void *

variable_types:
y
void *

variable_types: struct ndn_dict_entry { int index; char *name; } *


decompiled_code:
int __fastcall qsort_compare_dict_names(const void *a1, const void *a2)
{
return strcmp(*((const char **)a1 + 1), *((const char **)a2 + 1));
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, const void *a2)
{
return strcmp(*((const char **)a1 + 1), *((const char **)a2 + 1));
}

original_code:
static int
qsort_compare_dict_names(const void *x, const void *y)
{
    const struct ndn_dict_entry *ex = x;
    const struct ndn_dict_entry *ey = y;
    return (strcmp(ex->name, ey->name));
}

canonical_original_code:
static int qsort_compare_dict_names(void *x, void *y)
{
    struct ndn_dict_entry *ex = x;
    struct ndn_dict_entry *ey = y;
    return (strcmp(ex->name, ey->name));
}

user_defined_types: struct ndn_dict_entry { int index; char *name; }


original_code:
void
ndn_destroy_dict(struct ndn_dict **dp)
{
    struct ndn_dict *d = *dp;
    int i;
    if (d != ((void *)0)) {
        for (i = 0; i < d->count; i++) {
            if (d->dict[i].name != ((void *)0))
                free((void *)d->dict[i].name);
        }
        free(d);
    }
    *dp = ((void *)0);
}

variable_types: struct ndn_dict { int count; struct ndn_dict_entry { int index; char *name; } *dict; } **

variable_types: i int

user_defined_types: struct ndn_dict { int count; struct ndn_dict_entry *dict; }


decompiled_code:
int __fastcall ndn_setup_sockaddr_un(char *nptr, __int64 a2)
{
char *v2; // rbp
char *v3; // rax
const char *v4; // r12
int v5; // er13
int result; // eax

v2 = nptr;
v3 = getenv("NDN_LOCAL_SOCKNAME");
v4 = "/tmp/.ndnd.sock";
if ( v3 )
{
v4 = v3;
if ( !*v3 )
v4 = "/tmp/.ndnd.sock";
}
*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 102) = 0LL;
memset(
(void *)((a2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 110) >> 3));
*(_WORD *)a2 = 1;
if ( (nptr && *nptr || (v2 = getenv("NDN_LOCAL_PORT")) != 0LL) && (v5 = atoi(v2), v5 > 0) && v5 != atoi("6363") )
result = snprintf((char *)(a2 + 2), 0x6CuLL, "%s.%s", v4, v2);
else
result = snprintf((char *)(a2 + 2), 0x6CuLL, "%s", v4);
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *nptr, __int64 a2)
{
char *v2; // rbp
char *v3; // rax
const char *v4; // r12
int v5; // er13
int result; // eax

v2 = nptr;
v3 = getenv("NDN_LOCAL_SOCKNAME");
v4 = "/tmp/.ndnd.sock";
if ( v3 )
{
v4 = v3;
if ( !*v3 )
v4 = "/tmp/.ndnd.sock";
}
*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 102) = 0LL;
memset(
(void *)((a2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 110) >> 3));
*(_WORD *)a2 = 1;
if ( (nptr && *nptr || (v2 = getenv("NDN_LOCAL_PORT")) != 0LL) && (v5 = atoi(v2), v5 > 0) && v5 != atoi("6363") )
result = snprintf((char *)(a2 + 2), 0x6CuLL, "%s.%s", v4, v2);
else
result = snprintf((char *)(a2 + 2), 0x6CuLL, "%s", v4);
return result;
}

original_code:
void
ndn_setup_sockaddr_un(const char *portstr, struct sockaddr_un *result)
{
    struct sockaddr_un *sa = result;
    const char *sockname = getenv("NDN_LOCAL_SOCKNAME");
    if (sockname == ((void *)0) || sockname[0] == 0)
        sockname = "/tmp/.ndnd.sock";
    memset(sa, 0, sizeof(*sa));
    sa->sun_family = 1;
    if (portstr == ((void *)0) || portstr[0] == 0)
        portstr = getenv("NDN_LOCAL_PORT");
    if (portstr != ((void *)0) && atoi(portstr) > 0 &&
          atoi(portstr) != atoi("6363"))
        snprintf(sa->sun_path, sizeof(sa->sun_path), "%s.%s",
                 sockname, portstr);
    else
        snprintf(sa->sun_path, sizeof(sa->sun_path), "%s",
                 sockname);
}

canonical_original_code:
void ndn_setup_sockaddr_un(char *portstr, struct sockaddr_un *result)
{
    struct sockaddr_un *sa = result;
    char *sockname = getenv("NDN_LOCAL_SOCKNAME");
    if (sockname == ((void *)0) || sockname[0] == 0)
        sockname = "/tmp/.ndnd.sock";
    memset(sa, 0, sizeof(*sa));
    sa->sun_family = 1;
    if (portstr == ((void *)0) || portstr[0] == 0)
        portstr = getenv("NDN_LOCAL_PORT");
    if (portstr != ((void *)0) && atoi(portstr) > 0 &&
          atoi(portstr) != atoi("6363"))
        snprintf(sa->sun_path, sizeof(sa->sun_path), "%s.%s",
                 sockname, portstr);
    else
        snprintf(sa->sun_path, sizeof(sa->sun_path), "%s",
                 sockname);
}

variable_types:
portstr
char *

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

variable_types:
sockname
char *


decompiled_code:
__int64 __fastcall ndn_setup_sockaddr_in(const char *a1, void *a2, socklen_t a3)
{
char *v4; // rbp
char *v5; // rax
char *v6; // rbx
unsigned int v7; // ebx
socklen_t v8; // edx
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
v4 = strdup(a1);
v5 = strchr(v4, 58);
if ( (!v5 || (*v5 = 0, v6 = v5 + 1, !v5[1])) && (v6 = getenv("NDN_LOCAL_PORT")) == 0LL || !*v6 )
v6 = "6363";
memset(a2, 0, (int)a3);
req.ai_family = 0;
if ( !strcasecmp(v4, "tcp6") )
req.ai_family = 10;
if ( !strcasecmp(v4, "tcp4") )
req.ai_family = 2;
req.ai_socktype = 1;
req.ai_flags = 32;
req.ai_protocol = 0;
v7 = getaddrinfo(0LL, v6, &req, &pai);
if ( v7 )
{
v7 = -1;
}
else
{
v8 = pai->ai_addrlen;
if ( v8 > a3 )
v7 = -1;
else
memcpy(a2, pai->ai_addr, v8);
}
free(v4);
freeaddrinfo(pai);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, void *a2, socklen_t a3)
{
char *v4; // rbp
char *v5; // rax
char *v6; // rbx
unsigned int v7; // ebx
socklen_t v8; // edx
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
v4 = strdup(a1);
v5 = strchr(v4, 58);
if ( (!v5 || (*v5 = 0, v6 = v5 + 1, !v5[1])) && (v6 = getenv("NDN_LOCAL_PORT")) == 0LL || !*v6 )
v6 = "6363";
memset(a2, 0, (int)a3);
req.ai_family = 0;
if ( !strcasecmp(v4, "tcp6") )
req.ai_family = 10;
if ( !strcasecmp(v4, "tcp4") )
req.ai_family = 2;
req.ai_socktype = 1;
req.ai_flags = 32;
req.ai_protocol = 0;
v7 = getaddrinfo(0LL, v6, &req, &pai);
if ( v7 )
{
v7 = -1;
}
else
{
v8 = pai->ai_addrlen;
if ( v8 > a3 )
v7 = -1;
else
memcpy(a2, pai->ai_addr, v8);
}
free(v4);
freeaddrinfo(pai);
return v7;
}

original_code:
int
ndn_setup_sockaddr_in(const char *name, struct sockaddr *result, int length)
{
    struct addrinfo hints = {0};
    struct addrinfo *ai = ((void *)0);
    char *port;
    char *nameonly = strdup(name);
    int ans = -1;
    int res;
    port = strchr(nameonly, ':');
    if (port)
        *port++ = 0;
    if (port == ((void *)0) || port[0] == 0)
        port = getenv("NDN_LOCAL_PORT");
    if (port == ((void *)0) || port[0] == 0)
        port = "6363";
    memset(result, 0, length);
    hints.ai_family = 0;
    if (strcasecmp(nameonly, "tcp6") == 0) hints.ai_family = 10;
    if (strcasecmp(nameonly, "tcp4") == 0) hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0020;
    hints.ai_protocol = 0;
    res = getaddrinfo(((void *)0), port, &hints, &ai);
    if (res != 0 || ai->ai_addrlen > length)
        goto Bail;
    memcpy(result, ai->ai_addr, ai->ai_addrlen);
    ans = 0;
Bail:
    free(nameonly);
    freeaddrinfo(ai);
    return (ans);
}

canonical_original_code:
int ndn_setup_sockaddr_in(char *name, struct sockaddr *result, int length)
{
    struct addrinfo hints = {0};
    struct addrinfo *ai = ((void *)0);
    char *port;
    char *nameonly = strdup(name);
    int ans = -1;
    int res;
    port = strchr(nameonly, ':');
    if (port)
        *port++ = 0;
    if (port == ((void *)0) || port[0] == 0)
        port = getenv("NDN_LOCAL_PORT");
    if (port == ((void *)0) || port[0] == 0)
        port = "6363";
    memset(result, 0, length);
    hints.ai_family = 0;
    if (strcasecmp(nameonly, "tcp6") == 0) hints.ai_family = 10;
    if (strcasecmp(nameonly, "tcp4") == 0) hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0020;
    hints.ai_protocol = 0;
    res = getaddrinfo(((void *)0), port, &hints, &ai);
    if (res != 0 || ai->ai_addrlen > length)
        goto Bail;
    memcpy(result, ai->ai_addr, ai->ai_addrlen);
    ans = 0;
Bail:
    free(nameonly);
    freeaddrinfo(ai);
    return (ans);
}

variable_types:
name
char *

variable_types: length int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
port
char *

variable_types:
nameonly
char *

variable_types: ans int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall ndn_btree_content_cobid(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 v3; // rdx

v2 = ndn_btree_node_getentry(64LL, a1, a2);
v3 = 0LL;
if ( v2 )
v3 = ndn_btree_fetchval64((unsigned __int8 *)(v2 + 24), 8);
return v3;
}

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

v2 = ndn_btree_node_getentry(64LL, a1, a2);
v3 = 0LL;
if ( v2 )
v3 = func0((unsigned __int8 *)(v2 + 24), 8);
return v3;
}

original_code:
uint_least64_t
ndn_btree_content_cobid(struct ndn_btree_node *node, int ndx)
{
    struct ndn_btree_content_payload *e = ((void *)0);
    uint_least64_t ans = 0;
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e != ((void *)0))
        ans = ndn_btree_fetchval64(&((e)->cobid[0]), sizeof((e)->cobid));
    return(ans);
}

canonical_original_code:
unsigned long ndn_btree_content_cobid(struct ndn_btree_node *node, int ndx)
{
    struct ndn_btree_content_payload *e = ((void *)0);
    unsigned long ans = 0;
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e != ((void *)0))
        ans = ndn_btree_fetchval64(&((e)->cobid[0]), sizeof((e)->cobid));
    return(ans);
}

variable_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *

variable_types: ndx int

variable_types: struct ndn_btree_content_payload { unsigned char magic[1]; unsigned char ctype[3]; unsigned char cobsz[4]; unsigned char ncomp[2]; unsigned char flags[1]; unsigned char ttpad[1]; unsigned char timex[6]; unsigned char actim[6]; unsigned char cobid[8]; unsigned char ppkdg[32]; } *

variable_types:
ans
unsigned long

user_defined_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; }


decompiled_code:
__int64 __fastcall ndn_btree_content_set_cobid(__int64 a1, unsigned int a2, unsigned __int64 a3)
{
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // edx
__int64 v7; // rbx

v4 = ndn_btree_node_getentry(64LL, a1, a2);
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
ndn_btree_storeval64(v4 + 24, 8, a3);
v6 = 0;
v7 = v5 - *(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL);
if ( v7 >= 0 && *(unsigned int *)(a1 + 28) > v7 )
*(_DWORD *)(a1 + 28) = v7;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2, unsigned __int64 a3)
{
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // edx
__int64 v7; // rbx

v4 = ndn_btree_node_getentry(64LL, a1, a2);
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
func1(v4 + 24, 8, a3);
v6 = 0;
v7 = v5 - *(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL);
if ( v7 >= 0 && *(unsigned int *)(a1 + 28) > v7 )
*(_DWORD *)(a1 + 28) = v7;
return v6;
}

original_code:
int
ndn_btree_content_set_cobid(struct ndn_btree_node *node, int ndx,
                            uint_least64_t cobid)
{
    struct ndn_btree_content_payload *e = ((void *)0);
    ptrdiff_t dirty;
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e == ((void *)0))
        return(-1);
    ndn_btree_storeval64(&((e)->cobid[0]), sizeof((e)->cobid), (cobid));
    dirty = (((unsigned char *)e) - node->buf->buf);
    if (dirty >= 0 && dirty < node->clean)
        node->clean = dirty;
    return(0);
}

canonical_original_code:
int ndn_btree_content_set_cobid(struct ndn_btree_node *node, int ndx, unsigned long cobid)
{
    struct ndn_btree_content_payload *e = ((void *)0);
    long dirty;
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e == ((void *)0))
        return(-1);
    ndn_btree_storeval64(&((e)->cobid[0]), sizeof((e)->cobid), (cobid));
    dirty = (((unsigned char *)e) - node->buf->buf);
    if (dirty >= 0 && dirty < node->clean)
        node->clean = dirty;
    return(0);
}

variable_types:
cobid
unsigned long

variable_types: dirty long

user_defined_types: struct ndn_btree_content_payload { unsigned char magic[1]; unsigned char ctype[3]; unsigned char cobsz[4]; unsigned char ncomp[2]; unsigned char flags[1]; unsigned char ttpad[1]; unsigned char timex[6]; unsigned char actim[6]; unsigned char cobid[8]; unsigned char ppkdg[32]; }


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

v2 = ndn_btree_node_getentry(64LL, a1, a2);
if ( v2 )
result = ndn_btree_fetchval(v2 + 4, 4LL);
else
result = 0xFFFFFFFFLL;
return result;
}

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

v2 = ndn_btree_node_getentry(64LL, a1, a2);
if ( v2 )
result = ndn_btree_fetchval(v2 + 4, 4LL);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int
ndn_btree_content_cobsz(struct ndn_btree_node *node, int ndx)
{
    struct ndn_btree_content_payload *e = ((void *)0);
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e != ((void *)0))
        return(ndn_btree_fetchval(&((e)->cobsz[0]), sizeof((e)->cobsz)));
    return(-1);
}


decompiled_code:
__int64 __fastcall ndn_flatname_compare(const void *a1, size_t a2, const void *a3, size_t a4)
{
size_t v7; // rdx
unsigned int v8; // edx

v7 = a2;
if ( a4 <= a2 )
v7 = a4;
v8 = memcmp(a1, a3, v7);
if ( !v8 )
{
if ( a4 > a2 )
{
v8 = -9999;
}
else if ( a4 != a2 )
{
v8 = 9999;
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(const void *a1, size_t a2, const void *a3, size_t a4)
{
size_t v7; // rdx
unsigned int v8; // edx

v7 = a2;
if ( a4 <= a2 )
v7 = a4;
v8 = memcmp(a1, a3, v7);
if ( !v8 )
{
if ( a4 > a2 )
{
v8 = -9999;
}
else if ( a4 != a2 )
{
v8 = 9999;
}
}
return v8;
}

original_code:
int
ndn_flatname_compare(const unsigned char *a, size_t al, const unsigned char *b, size_t bl)
{
    int res;
    res = memcmp(a, b, al < bl ? al : bl);
    if (res != 0)
        return(res);
    if (al < bl)
        return(-9999);
    else if (al == bl)
        return(0);
    else
        return(9999);
}

canonical_original_code:
int ndn_flatname_compare(unsigned char *a, unsigned long al, unsigned char *b, unsigned long bl)
{
    int res;
    res = memcmp(a, b, al < bl ? al : bl);
    if (res != 0)
        return(res);
    if (al < bl)
        return(-9999);
    else if (al == bl)
        return(0);
    else
        return(9999);
}

variable_types:
a
unsigned char *

variable_types:
al
unsigned long

variable_types:
b
unsigned char *

variable_types:
bl
unsigned long

variable_types: res int


decompiled_code:
__int64 __fastcall ndn_flatname_charbuf_compare(__int64 a1, __int64 a2)
{
return ndn_flatname_compare(*(const void **)(a1 + 16), *(_QWORD *)a1, *(const void **)(a2 + 16), *(_QWORD *)a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
return func5(*(const void **)(a1 + 16), *(_QWORD *)a1, *(const void **)(a2 + 16), *(_QWORD *)a2);
}

original_code:
int
ndn_flatname_charbuf_compare(struct ndn_charbuf *a, struct ndn_charbuf *b)
{
    return(ndn_flatname_compare(a->buf, a->length, b->buf, b->length));
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *

variable_types:
b
struct ndn_charbuf *


decompiled_code:
__int64 __fastcall ndn_flatname_append_component(__int64 *a1, __int64 a2, unsigned __int64 a3)
{
__int64 v4; // r15
int i; // ebx
int v6; // ebp
int v7; // ebp
__int64 result; // rax

if ( a3 > 0x1FFFFF )
return 0xFFFFFFFFLL;
v4 = *a1;
for ( i = 0; 1 << (i + 7) <= a3; i += 7 )
;
if ( i <= 0 )
{
v6 = 0;
}
else
{
v6 = 0;
do
{
v6 |= ndn_charbuf_append_value(a1, (unsigned __int8)(a3 >> i) | 0x80u, 1LL);
i -= 7;
}
while ( i > 0 );
}
v7 = ndn_charbuf_append_value(a1, a3 & 0x7F, 1LL) | v6;
result = v7 | (unsigned int)ndn_charbuf_append(a1, a2, a3);
if ( (int)result < 0 )
*a1 = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1, __int64 a2, unsigned __int64 a3)
{
__int64 v4; // r15
int i; // ebx
int v6; // ebp
int v7; // ebp
__int64 result; // rax

if ( a3 > 0x1FFFFF )
return 0xFFFFFFFFLL;
v4 = *a1;
for ( i = 0; 1 << (i + 7) <= a3; i += 7 )
;
if ( i <= 0 )
{
v6 = 0;
}
else
{
v6 = 0;
do
{
v6 |= ndn_charbuf_append_value(a1, (unsigned __int8)(a3 >> i) | 0x80u, 1LL);
i -= 7;
}
while ( i > 0 );
}
v7 = ndn_charbuf_append_value(a1, a3 & 0x7F, 1LL) | v6;
result = v7 | (unsigned int)ndn_charbuf_append(a1, a2, a3);
if ( (int)result < 0 )
*a1 = v4;
return result;
}

original_code:
int
ndn_flatname_append_component(struct ndn_charbuf *dst,
                              const unsigned char *comp, size_t size)
{
    int res;
    int s;
    size_t save;
    if (size >= (1 << 21))
        return(-1);
    save = dst->length;
    res = 0;
    for (s = 0; size >= (1 << (s + 7)); s += 7)
        continue;
    for (; s > 0; s -= 7)
        res |= ndn_charbuf_append_value(dst, (((size >> s) & 0x7F) | 0x80), 1);
    res |= ndn_charbuf_append_value(dst, (size & 0x7F), 1);
    res |= ndn_charbuf_append(dst, comp, size);
    if (res < 0)
        dst->length = save;
    return(res);
}

canonical_original_code:
int ndn_flatname_append_component(struct ndn_charbuf *dst, unsigned char *comp, unsigned long size)
{
    int res;
    int s;
    unsigned long save;
    if (size >= (1 << 21))
        return(-1);
    save = dst->length;
    res = 0;
    for (s = 0; size >= (1 << (s + 7)); s += 7)
        continue;
    for (; s > 0; s -= 7)
        res |= ndn_charbuf_append_value(dst, (((size >> s) & 0x7F) | 0x80), 1);
    res |= ndn_charbuf_append_value(dst, (size & 0x7F), 1);
    res |= ndn_charbuf_append(dst, comp, size);
    if (res < 0)
        dst->length = save;
    return(res);
}

variable_types:
comp
unsigned char *

variable_types:
size
unsigned long

variable_types: s int

variable_types:
save
unsigned long


decompiled_code:
__int64 __fastcall ndn_flatname_append_from_ndnb(__int64 *a1, __int64 a2, __int64 a3, int a4, unsigned int a5)
{
__int64 v7; // rbx
int v8; // ebp
unsigned int v9; // er12
int v11; // [rsp+Ch] [rbp-8Ch]
char v12[64]; // [rsp+10h] [rbp-88h] BYREF
unsigned __int64 v13; // [rsp+50h] [rbp-48h] BYREF
__int64 v14[8]; // [rsp+58h] [rbp-40h] BYREF

v14[0] = 0LL;
v13 = 0LL;
v7 = ndn_buf_decoder_start(v12);
if ( (unsigned int)ndn_buf_match_dtag(v7, 26LL) || (unsigned int)ndn_buf_match_dtag(v7, 64LL) )
{
ndn_buf_advance(v7);
if ( (unsigned int)ndn_buf_match_dtag(v7, 37LL) )
ndn_buf_advance_past_element(v7);
}
if ( (unsigned int)ndn_buf_match_dtag(v7, 14LL) || (v11 = ndn_buf_match_dtag(v7, 105LL)) != 0 )
{
ndn_buf_advance(v7);
v11 = 1;
}
else
{
a5 = a5 != 0;
}
v8 = 0;
v9 = 0;
while ( (unsigned int)ndn_buf_match_dtag(v7, 15LL) )
{
if ( v9 == a5 )
return a5;
ndn_buf_advance(v7);
v13 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v7, v14, &v13) )
ndn_buf_advance(v7);
ndn_buf_check_close(v7);
if ( *(int *)(v7 + 8) < 0 )
return (unsigned int)-1;
if ( ++v8 > a4 )
{
if ( (int)ndn_flatname_append_component(a1, v14[0], v13) < 0 )
return (unsigned int)-1;
++v9;
}
}
if ( v11 )
ndn_buf_check_close(v7);
if ( *(int *)(v7 + 8) < 0 )
v9 = -1;
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 *a1, __int64 a2, __int64 a3, int a4, unsigned int a5)
{
__int64 v7; // rbx
int v8; // ebp
unsigned int v9; // er12
int v11; // [rsp+Ch] [rbp-8Ch]
char v12[64]; // [rsp+10h] [rbp-88h] BYREF
unsigned __int64 v13; // [rsp+50h] [rbp-48h] BYREF
__int64 v14[8]; // [rsp+58h] [rbp-40h] BYREF

v14[0] = 0LL;
v13 = 0LL;
v7 = ndn_buf_decoder_start(v12);
if ( (unsigned int)ndn_buf_match_dtag(v7, 26LL) || (unsigned int)ndn_buf_match_dtag(v7, 64LL) )
{
ndn_buf_advance(v7);
if ( (unsigned int)ndn_buf_match_dtag(v7, 37LL) )
ndn_buf_advance_past_element(v7);
}
if ( (unsigned int)ndn_buf_match_dtag(v7, 14LL) || (v11 = ndn_buf_match_dtag(v7, 105LL)) != 0 )
{
ndn_buf_advance(v7);
v11 = 1;
}
else
{
a5 = a5 != 0;
}
v8 = 0;
v9 = 0;
while ( (unsigned int)ndn_buf_match_dtag(v7, 15LL) )
{
if ( v9 == a5 )
return a5;
ndn_buf_advance(v7);
v13 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v7, v14, &v13) )
ndn_buf_advance(v7);
ndn_buf_check_close(v7);
if ( *(int *)(v7 + 8) < 0 )
return (unsigned int)-1;
if ( ++v8 > a4 )
{
if ( (int)func7(a1, v14[0], v13) < 0 )
return (unsigned int)-1;
++v9;
}
}
if ( v11 )
ndn_buf_check_close(v7);
if ( *(int *)(v7 + 8) < 0 )
v9 = -1;
return v9;
}

original_code:
int
ndn_flatname_append_from_ndnb(struct ndn_charbuf *dst,
                              const unsigned char *ndnb, size_t size,
                              int skip, int count)
{
    int ans = 0;
    int ncomp = 0;
    const unsigned char *comp = ((void *)0);
    size_t compsize = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, ndnb, size);
    int checkclose = 0;
    int res;
    if (ndn_buf_match_dtag(d, NDN_DTAG_Interest) ||
        ndn_buf_match_dtag(d, NDN_DTAG_ContentObject)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_dtag(d, NDN_DTAG_Signature))
            ndn_buf_advance_past_element(d);
    }
    if ((ndn_buf_match_dtag(d, NDN_DTAG_Name) ||
         ndn_buf_match_dtag(d, NDN_DTAG_Prefix))) {
        checkclose = 1;
        ndn_buf_advance(d);
    }
    else if (count != 0)
        count = 1;
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        if (ans == count)
            return(ans);
        ndn_buf_advance(d);
        compsize = 0;
        if (ndn_buf_match_blob(d, &comp, &compsize))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (d->decoder.state < 0)
            return(-1);
        ncomp += 1;
        if (ncomp > skip) {
            res = ndn_flatname_append_component(dst, comp, compsize);
            if (res < 0)
                return(-1);
            ans++;
        }
    }
    if (checkclose)
        ndn_buf_check_close(d);
    if (d->decoder.state < 0)
        return (-1);
    return(ans);
}

canonical_original_code:
int ndn_flatname_append_from_ndnb(struct ndn_charbuf *dst, unsigned char *ndnb, unsigned long size, int skip, int count)
{
    int ans = 0;
    int ncomp = 0;
    unsigned char *comp = ((void *)0);
    unsigned long compsize = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, ndnb, size);
    int checkclose = 0;
    int res;
    if (ndn_buf_match_dtag(d, NDN_DTAG_Interest) ||
        ndn_buf_match_dtag(d, NDN_DTAG_ContentObject)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_dtag(d, NDN_DTAG_Signature))
            ndn_buf_advance_past_element(d);
    }
    if ((ndn_buf_match_dtag(d, NDN_DTAG_Name) ||
         ndn_buf_match_dtag(d, NDN_DTAG_Prefix))) {
        checkclose = 1;
        ndn_buf_advance(d);
    }
    else if (count != 0)
        count = 1;
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        if (ans == count)
            return(ans);
        ndn_buf_advance(d);
        compsize = 0;
        if (ndn_buf_match_blob(d, &comp, &compsize))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (d->decoder.state < 0)
            return(-1);
        ncomp += 1;
        if (ncomp > skip) {
            res = ndn_flatname_append_component(dst, comp, compsize);
            if (res < 0)
                return(-1);
            ans++;
        }
    }
    if (checkclose)
        ndn_buf_check_close(d);
    if (d->decoder.state < 0)
        return (-1);
    return(ans);
}

variable_types:
ndnb
unsigned char *

variable_types: skip int

variable_types: count int

variable_types: ans int

variable_types: ncomp int

variable_types:
compsize
unsigned long

variable_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder { long index; int state; int nest; unsigned long numval; unsigned long token_index; unsigned long element_index; } decoder; unsigned char *buf; unsigned long size; } *

variable_types: checkclose int

user_defined_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder decoder; unsigned char *buf; unsigned long size; }


decompiled_code:
__int64 __fastcall ndn_flatname_from_ndnb(__int64 *a1, __int64 a2, __int64 a3)
{
*a1 = 0LL;
return ndn_flatname_append_from_ndnb(a1, a2, a3, 0, 0xFFFFFFFF);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 *a1, __int64 a2, __int64 a3)
{
*a1 = 0LL;
return func8(a1, a2, a3, 0, 0xFFFFFFFF);
}

original_code:
int
ndn_flatname_from_ndnb(struct ndn_charbuf *dst,
                       const unsigned char *ndnb, size_t size)
{
    dst->length = 0;
    return(ndn_flatname_append_from_ndnb(dst, ndnb, size, 0, -1));
}

canonical_original_code:
int ndn_flatname_from_ndnb(struct ndn_charbuf *dst, unsigned char *ndnb, unsigned long size)
{
    dst->length = 0;
    return(ndn_flatname_append_from_ndnb(dst, ndnb, size, 0, -1));
}


decompiled_code:
__int64 __fastcall ndn_flatname_next_comp(_BYTE *a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned int v3; // er10
__int64 v4; // rax
int v5; // ecx
char v6; // dl
int v7; // ecx

result = 0LL;
if ( a2 )
{
if ( *a1 == 0x80 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = 3;
if ( a2 <= 3 )
v3 = a2;
v4 = 0LL;
v5 = 0;
while ( 1 )
{
v6 = a1[v4];
if ( v6 >= 0 )
break;
v5 = (v5 | v6 & 0x7F) << 7;
if ( v3 <= (unsigned int)++v4 )
return 0xFFFFFFFFLL;
}
if ( v3 <= (unsigned int)v4 )
{
result = 0xFFFFFFFFLL;
}
else
{
v7 = (unsigned __int8)a1[(unsigned int)v4] | v5;
if ( (unsigned int)(v4 + 1 + v7) > a2 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v4 + 1 + 4 * v7);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned int v3; // er10
__int64 v4; // rax
int v5; // ecx
char v6; // dl
int v7; // ecx

result = 0LL;
if ( a2 )
{
if ( *a1 == 0x80 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = 3;
if ( a2 <= 3 )
v3 = a2;
v4 = 0LL;
v5 = 0;
while ( 1 )
{
v6 = a1[v4];
if ( v6 >= 0 )
break;
v5 = (v5 | v6 & 0x7F) << 7;
if ( v3 <= (unsigned int)++v4 )
return 0xFFFFFFFFLL;
}
if ( v3 <= (unsigned int)v4 )
{
result = 0xFFFFFFFFLL;
}
else
{
v7 = (unsigned __int8)a1[(unsigned int)v4] | v5;
if ( (unsigned int)(v4 + 1 + v7) > a2 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v4 + 1 + 4 * v7);
}
}
}
return result;
}

original_code:
int
ndn_flatname_next_comp(const unsigned char *flatname, size_t size)
{
    unsigned i, l, m;
    if (size == 0)
        return(0);
    if (flatname[0] == 0x80)
        return(-1);
    m = (size < 3) ? size : 3;
    for (i = 0, l = 0; i < m && (flatname[i] & 0x80) != 0; i++)
        l = (l | (flatname[i] & 0x7F)) << 7;
    if (i >= m)
        return(-1);
    l |= flatname[i++];
    if (i + l > size)
        return(-1);
    return(l * 4 + i);
}

canonical_original_code:
int ndn_flatname_next_comp(unsigned char *flatname, unsigned long size)
{
    unsigned int i; unsigned int l; unsigned int m;
    if (size == 0)
        return(0);
    if (flatname[0] == 0x80)
        return(-1);
    m = (size < 3) ? size : 3;
    for (i = 0, l = 0; i < m && (flatname[i] & 0x80) != 0; i++)
        l = (l | (flatname[i] & 0x7F)) << 7;
    if (i >= m)
        return(-1);
    l |= flatname[i++];
    if (i + l > size)
        return(-1);
    return(l * 4 + i);
}

variable_types:
flatname
unsigned char *

variable_types:
i
unsigned int

variable_types:
l
unsigned int

variable_types:
m
unsigned int


decompiled_code:
__int64 __fastcall ndn_btree_match_interest(__int64 a1, unsigned int a2, __int64 a3, __int64 a4, unsigned __int64 *a5)
{
__int64 v8; // rax
_BYTE *v9; // r12
__int64 result; // rax
int v11; // edx
unsigned __int16 v12; // dx
unsigned __int16 v13; // cx
int v14; // edx
unsigned __int64 v15; // r15
int v16; // er13
int v17; // er14
__int64 v18; // r12
int v19; // eax
int v20; // edx
unsigned __int64 v21; // rdx
unsigned __int64 v22; // [rsp+8h] [rbp-50h]
__int64 v23; // [rsp+10h] [rbp-48h] BYREF
void *s1; // [rsp+18h] [rbp-40h] BYREF

s1 = 0LL;
v23 = 0LL;
v8 = ndn_btree_node_getentry(64LL, a1, a2);
if ( !v8 )
return 0xFFFFFFFFLL;
v9 = (_BYTE *)v8;
result = 0xFFFFFFFFLL;
if ( *v9 == 0xC0 )
{
v11 = ndn_btree_fetchval(v9 + 8, 2LL);
result = 0LL;
if ( *(_DWORD *)(a4 + 8) + *(_DWORD *)(a4 + 4) <= v11 && *(_DWORD *)(a4 + 12) + *(_DWORD *)(a4 + 4) >= v11 )
{
v12 = *(_WORD *)(a4 + 46);
v13 = *(_WORD *)(a4 + 52);
if ( v12 >= v13
|| (v23 = 0LL, ndn_ref_tagged_BLOB(60LL, a3, v12, v13, &s1, &v23), result = 0LL, v23 == 32)
&& (v20 = memcmp(s1, v9 + 32, 0x20uLL), result = 0LL, !v20) )
{
result = 1LL;
if ( *(_WORD *)(a4 + 56) > *(_WORD *)(a4 + 54) )
{
v14 = ndn_btree_key_fetch(a5, a1, a2);
result = 0xFFFFFFFFLL;
if ( v14 >= 0 )
{
v22 = a5[2];
v15 = *a5;
if ( *a5 )
{
v16 = 0;
v17 = 0;
v18 = 0LL;
while ( 1 )
{
v19 = ndn_flatname_next_comp((_BYTE *)(v22 + v18), v15 - v18);
if ( v19 <= 0 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)(a4 + 4) == v16 )
break;
v17 += (v19 & 3) + (v19 >> 2);
++v16;
v18 = v17;
if ( v17 >= v15 )
goto LABEL_14;
}
v21 = v22 + v18 + (v19 & 3);
if ( v21 )
result = (unsigned int)ndn_excluded(
a3 + *(unsigned __int16 *)(a4 + 54),
*(unsigned __int16 *)(a4 + 56) - *(unsigned __int16 *)(a4 + 54),
v21,
v19 >> 2) == 0;
else
result = 0LL;
}
else
{
LABEL_14:
result = 0LL;
}
}
}
}
}
}
return result;
}

original_code:
int
ndn_btree_match_interest(struct ndn_btree_node *node, int ndx,
                         const unsigned char *interest_msg,
                         const struct ndn_parsed_interest *pi,
                         struct ndn_charbuf *scratch)
{
    const unsigned char *blob = ((void *)0);
    const unsigned char *nextcomp = ((void *)0);
    int i;
    int n;
    int ncomps;
    int pubidend;
    int pubidstart;
    int res;
    int rnc;
    size_t blob_size = 0;
    size_t nextcomp_size = 0;
    size_t size;
    struct ndn_btree_content_payload *e = ((void *)0);
    unsigned char *flatname = ((void *)0);
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e == ((void *)0) || e->magic[0] != 0xC0)
        return(-1);
    ncomps = ndn_btree_fetchval(&((e)->ncomp[0]), sizeof((e)->ncomp));
    if (ncomps < pi->prefix_comps + pi->min_suffix_comps)
        return(0);
    if (ncomps > pi->prefix_comps + pi->max_suffix_comps)
        return(0);
    pubidstart = pi->offset[NDN_PI_B_PublisherID];
    pubidend = pi->offset[NDN_PI_E_PublisherID];
    if (pubidstart < pubidend) {
        blob_size = 0;
        ndn_ref_tagged_BLOB(NDN_DTAG_PublisherPublicKeyDigest,
                            interest_msg,
                            pubidstart, pubidend,
                            &blob, &blob_size);
        if (blob_size != sizeof(e->ppkdg))
            return(0);
        if (0 != memcmp(blob, e->ppkdg, blob_size))
            return(0);
    }
    if (pi->offset[NDN_PI_E_Exclude] > pi->offset[NDN_PI_B_Exclude]) {
        res = ndn_btree_key_fetch(scratch, node, ndx);
        if (res < 0)
            return(-1);
        flatname = scratch->buf;
        size = scratch->length;
        nextcomp = ((void *)0);
        nextcomp_size = 0;
        for (i = 0, n = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2)), n++) {
            rnc = ndn_flatname_next_comp(flatname + i, size - i);
            if (rnc <= 0)
                return(-1);
            if (n == pi->prefix_comps) {
                nextcomp = flatname + i + ((rnc) & 3);
                nextcomp_size = ((rnc) >> 2);
                break;
            }
        }
        if (nextcomp == ((void *)0))
            return(0);
        if (ndn_excluded(interest_msg + pi->offset[NDN_PI_B_Exclude],
                         (pi->offset[NDN_PI_E_Exclude] -
                          pi->offset[NDN_PI_B_Exclude]),
                         nextcomp,
                         nextcomp_size))
            return(0);
    }
    return(1);
}

canonical_original_code:
int ndn_btree_match_interest(struct ndn_btree_node *node, int ndx, unsigned char *interest_msg, struct ndn_parsed_interest *pi, struct ndn_charbuf *scratch)
{
    unsigned char *blob = ((void *)0);
    unsigned char *nextcomp = ((void *)0);
    int i;
    int n;
    int ncomps;
    int pubidend;
    int pubidstart;
    int res;
    int rnc;
    unsigned long blob_size = 0;
    unsigned long nextcomp_size = 0;
    unsigned long size;
    struct ndn_btree_content_payload *e = ((void *)0);
    unsigned char *flatname = ((void *)0);
    e = ndn_btree_node_getentry(sizeof(*e), node, ndx);
    if (e == ((void *)0) || e->magic[0] != 0xC0)
        return(-1);
    ncomps = ndn_btree_fetchval(&((e)->ncomp[0]), sizeof((e)->ncomp));
    if (ncomps < pi->prefix_comps + pi->min_suffix_comps)
        return(0);
    if (ncomps > pi->prefix_comps + pi->max_suffix_comps)
        return(0);
    pubidstart = pi->offset[NDN_PI_B_PublisherID];
    pubidend = pi->offset[NDN_PI_E_PublisherID];
    if (pubidstart < pubidend) {
        blob_size = 0;
        ndn_ref_tagged_BLOB(NDN_DTAG_PublisherPublicKeyDigest,
                            interest_msg,
                            pubidstart, pubidend,
                            &blob, &blob_size);
        if (blob_size != sizeof(e->ppkdg))
            return(0);
        if (0 != memcmp(blob, e->ppkdg, blob_size))
            return(0);
    }
    if (pi->offset[NDN_PI_E_Exclude] > pi->offset[NDN_PI_B_Exclude]) {
        res = ndn_btree_key_fetch(scratch, node, ndx);
        if (res < 0)
            return(-1);
        flatname = scratch->buf;
        size = scratch->length;
        nextcomp = ((void *)0);
        nextcomp_size = 0;
        for (i = 0, n = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2)), n++) {
            rnc = ndn_flatname_next_comp(flatname + i, size - i);
            if (rnc <= 0)
                return(-1);
            if (n == pi->prefix_comps) {
                nextcomp = flatname + i + ((rnc) & 3);
                nextcomp_size = ((rnc) >> 2);
                break;
            }
        }
        if (nextcomp == ((void *)0))
            return(0);
        if (ndn_excluded(interest_msg + pi->offset[NDN_PI_B_Exclude],
                         (pi->offset[NDN_PI_E_Exclude] -
                          pi->offset[NDN_PI_B_Exclude]),
                         nextcomp,
                         nextcomp_size))
            return(0);
    }
    return(1);
}

variable_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *

variable_types: ndx int

variable_types: struct ndn_parsed_interest { int magic; int prefix_comps; int min_suffix_comps; int max_suffix_comps; int orderpref; int answerfrom; int scope; unsigned short offset[-1]; } *

variable_types:
scratch
struct ndn_charbuf *

variable_types:
blob
unsigned char *

variable_types:
nextcomp
unsigned char *

variable_types: i int

variable_types: n int

variable_types: ncomps int

variable_types: pubidend int

variable_types: pubidstart int

variable_types: res int

variable_types: rnc int

variable_types: struct ndn_btree_content_payload { unsigned char magic[1]; unsigned char ctype[3]; unsigned char cobsz[4]; unsigned char ncomp[2]; unsigned char flags[1]; unsigned char ttpad[1]; unsigned char timex[6]; unsigned char actim[6]; unsigned char cobid[8]; unsigned char ppkdg[32]; } *


decompiled_code:
__int64 __fastcall ndn_name_append_flatname(__int64 a1, __int64 a2, unsigned __int64 a3, int a4, int a5)
{
int v6; // er13
int v7; // er12
unsigned int v8; // er15
__int64 v9; // rbp
int v10; // eax
int v11; // ebx
int v12; // eax
int v13; // ebx
int v15; // [rsp+4h] [rbp-54h]

if ( a4 < 0 )
return (unsigned int)-1;
if ( a5 && a3 )
{
v6 = 0;
v7 = 0;
v8 = 0;
v9 = 0LL;
while ( 1 )
{
v10 = ndn_flatname_next_comp((_BYTE *)(a2 + v9), a3 - v9);
v11 = v10;
if ( v10 <= 0 )
return (unsigned int)-1;
v12 = v10 & 3;
v15 = v12;
v13 = v11 >> 2;
if ( a4 <= v7 )
{
if ( (int)ndn_name_append(a1, a2 + v9 + v12, v13) < 0 )
return (unsigned int)-1;
++v8;
}
++v7;
v6 += v15 + v13;
v9 = v6;
if ( v8 == a5 || v6 >= a3 )
return v8;
}
}
return 0;
}

original_code:
int
ndn_name_append_flatname(struct ndn_charbuf *dst,
                         const unsigned char *flatname, size_t size,
                         int skip, int count)
{
    int ans;
    int compnum;
    int i;
    int rnc;
    int res;
    const unsigned char *cp;
    size_t cs;
    if (skip < 0)
        return(-1);
    ans = 0;
    compnum = 0;
    for (i = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2))) {
        if (ans == count)
            return(ans);
        rnc = ndn_flatname_next_comp(flatname + i, size - i);
        if (rnc <= 0)
            return(-1);
        cp = flatname + i + ((rnc) & 3);
        cs = ((rnc) >> 2);
        if (compnum >= skip) {
            res = ndn_name_append(dst, cp, cs);
            if (res < 0)
                return(-1);
            ans++;
        }
        compnum++;
    }
    return(ans);
}

canonical_original_code:
int ndn_name_append_flatname(struct ndn_charbuf *dst, unsigned char *flatname, unsigned long size, int skip, int count)
{
    int ans;
    int compnum;
    int i;
    int rnc;
    int res;
    unsigned char *cp;
    unsigned long cs;
    if (skip < 0)
        return(-1);
    ans = 0;
    compnum = 0;
    for (i = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2))) {
        if (ans == count)
            return(ans);
        rnc = ndn_flatname_next_comp(flatname + i, size - i);
        if (rnc <= 0)
            return(-1);
        cp = flatname + i + ((rnc) & 3);
        cs = ((rnc) >> 2);
        if (compnum >= skip) {
            res = ndn_name_append(dst, cp, cs);
            if (res < 0)
                return(-1);
            ans++;
        }
        compnum++;
    }
    return(ans);
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *

variable_types:
flatname
unsigned char *

variable_types:
size
unsigned long

variable_types: skip int

variable_types: count int

variable_types: ans int

variable_types: compnum int

variable_types:
cp
unsigned char *

variable_types:
cs
unsigned long


decompiled_code:
__int64 __fastcall ndn_uri_append_flatname(__int64 a1, __int64 a2, unsigned __int64 a3, unsigned int a4)
{
_QWORD *v6; // rax
int v7; // ebx
__int64 v9[6]; // [rsp+8h] [rbp-30h] BYREF

v6 = (_QWORD *)ndn_charbuf_create();
v9[0] = (__int64)v6;
if ( v6 )
{
v7 = ndn_name_init(v6);
if ( v7 >= 0 )
{
v7 = ndn_name_append_flatname(v9[0], a2, a3, 0, -1);
if ( v7 >= 0 )
v7 = ndn_uri_append(a1, *(_QWORD *)(v9[0] + 16), *(_QWORD *)v9[0], a4);
}
ndn_charbuf_destroy(v9);
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2, unsigned __int64 a3, unsigned int a4)
{
_QWORD *v6; // rax
int v7; // ebx
__int64 v9[6]; // [rsp+8h] [rbp-30h] BYREF

v6 = (_QWORD *)ndn_charbuf_create();
v9[0] = (__int64)v6;
if ( v6 )
{
v7 = ndn_name_init(v6);
if ( v7 >= 0 )
{
v7 = func12(v9[0], a2, a3, 0, -1);
if ( v7 >= 0 )
v7 = ndn_uri_append(a1, *(_QWORD *)(v9[0] + 16), *(_QWORD *)v9[0], a4);
}
ndn_charbuf_destroy(v9);
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

original_code:
int
ndn_uri_append_flatname(struct ndn_charbuf *uri,
                             const unsigned char *flatname, size_t size,
                             int includescheme)
{
    struct ndn_charbuf *ndnb = ((void *)0);
    int res;
    ndnb = ndn_charbuf_create();
    if (ndnb == ((void *)0))
        return(-1);
    res = ndn_name_init(ndnb);
    if (res < 0)
        goto Bail;
    res = ndn_name_append_flatname(ndnb, flatname, size, 0, -1);
    if (res < 0)
        goto Bail;
    res = ndn_uri_append(uri, ndnb->buf, ndnb->length, includescheme);
Bail:
    ndn_charbuf_destroy(&ndnb);
    return(res);
}

canonical_original_code:
int ndn_uri_append_flatname(struct ndn_charbuf *uri, unsigned char *flatname, unsigned long size, int includescheme)
{
    struct ndn_charbuf *ndnb = ((void *)0);
    int res;
    ndnb = ndn_charbuf_create();
    if (ndnb == ((void *)0))
        return(-1);
    res = ndn_name_init(ndnb);
    if (res < 0)
        goto Bail;
    res = ndn_name_append_flatname(ndnb, flatname, size, 0, -1);
    if (res < 0)
        goto Bail;
    res = ndn_uri_append(uri, ndnb->buf, ndnb->length, includescheme);
Bail:
    ndn_charbuf_destroy(&ndnb);
    return(res);
}

variable_types: includescheme int


decompiled_code:
__int64 __fastcall ndn_flatname_ncomps(__int64 a1, unsigned __int64 a2)
{
int v3; // ebx
unsigned int v4; // er12
__int64 v5; // rdi
int v6; // eax

if ( !a2 )
return 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = ndn_flatname_next_comp((_BYTE *)(a1 + v5), a2 - v5);
if ( v6 <= 0 )
break;
++v4;
v3 += (v6 & 3) + (v6 >> 2);
v5 = v3;
if ( v3 >= a2 )
return v4;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, unsigned __int64 a2)
{
int v3; // ebx
unsigned int v4; // er12
__int64 v5; // rdi
int v6; // eax

if ( !a2 )
return 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = func10((_BYTE *)(a1 + v5), a2 - v5);
if ( v6 <= 0 )
break;
++v4;
v3 += (v6 & 3) + (v6 >> 2);
v5 = v3;
if ( v3 >= a2 )
return v4;
}
return (unsigned int)-1;
}

original_code:
int
ndn_flatname_ncomps(const unsigned char *flatname, size_t size)
{
    int ans;
    int i;
    int rnc;
    ans = 0;
    for (i = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2))) {
        rnc = ndn_flatname_next_comp(flatname + i, size - i);
        if (rnc <= 0)
            return(-1);
        ans++;
    }
    return(ans);
}

canonical_original_code:
int ndn_flatname_ncomps(unsigned char *flatname, unsigned long size)
{
    int ans;
    int i;
    int rnc;
    ans = 0;
    for (i = 0; i < size; i += (((rnc) & 3) + ((rnc) >> 2))) {
        rnc = ndn_flatname_next_comp(flatname + i, size - i);
        if (rnc <= 0)
            return(-1);
        ans++;
    }
    return(ans);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ndn_btree_insert_content(__int64 a1, unsigned int a2, unsigned __int64 a3, __int64 a4, __int64 a5, unsigned __int64 *a6)
{
unsigned int v10; // er15
unsigned int v11; // er12
size_t n; // [rsp+10h] [rbp-88h] BYREF
void *src; // [rsp+18h] [rbp-80h] BYREF
__int128 v15[7]; // [rsp+20h] [rbp-78h] BYREF

src = 0LL;
n = 0LL;
v10 = *(unsigned __int16 *)(a5 + 84);
v11 = ndn_flatname_ncomps(a6[2], *a6);
if ( *(_DWORD *)(a5 + 8) + 1 != v11 )
return 0xFFFFFFFFLL;
memset(v15, 85, 64);
ndn_btree_storeval(v15, 1LL, 192LL);
ndn_btree_storeval((char *)v15 + 1, 3LL, *(unsigned int *)(a5 + 4));
ndn_btree_storeval((char *)v15 + 4, 4LL, v10);
ndn_btree_storeval((char *)v15 + 8, 2LL, v11);
ndn_btree_storeval((char *)v15 + 10, 1LL, 0LL);
ndn_btree_storeval((char *)v15 + 11, 1LL, 0LL);
ndn_btree_storeval((char *)v15 + 12, 6LL, 0LL);
if ( (int)ndn_ref_tagged_BLOB(39LL, a4, *(unsigned __int16 *)(a5 + 42), *(unsigned __int16 *)(a5 + 44), &src, &n) < 0 )
return 0xFFFFFFFFLL;
if ( n > 6 )
return 0xFFFFFFFFLL;
memcpy((char *)v15 + 6 - n + 12, src, n);
ndn_btree_storeval64((__int64)&v15[1] + 8, 8, a3);
if ( (int)ndn_ref_tagged_BLOB(60LL, a4, *(unsigned __int16 *)(a5 + 38), *(unsigned __int16 *)(a5 + 40), &src, &n) < 0 )
return 0xFFFFFFFFLL;
if ( n != 32 )
return 0xFFFFFFFFLL;
v15[2] = (__int128)_mm_loadu_si128((const __m128i *)src);
v15[3] = (__int128)_mm_loadu_si128((const __m128i *)src + 1);
return ndn_btree_insert_entry(a1, a2, a6[2], *a6, v15, 64LL);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, unsigned int a2, unsigned __int64 a3, __int64 a4, __int64 a5, unsigned __int64 *a6)
{
unsigned int v10; // er15
unsigned int v11; // er12
size_t n; // [rsp+10h] [rbp-88h] BYREF
void *src; // [rsp+18h] [rbp-80h] BYREF
__int128 v15[7]; // [rsp+20h] [rbp-78h] BYREF

src = 0LL;
n = 0LL;
v10 = *(unsigned __int16 *)(a5 + 84);
v11 = func14(a6[2], *a6);
if ( *(_DWORD *)(a5 + 8) + 1 != v11 )
return 0xFFFFFFFFLL;
memset(v15, 85, 64);
ndn_btree_storeval(v15, 1LL, 192LL);
ndn_btree_storeval((char *)v15 + 1, 3LL, *(unsigned int *)(a5 + 4));
ndn_btree_storeval((char *)v15 + 4, 4LL, v10);
ndn_btree_storeval((char *)v15 + 8, 2LL, v11);
ndn_btree_storeval((char *)v15 + 10, 1LL, 0LL);
ndn_btree_storeval((char *)v15 + 11, 1LL, 0LL);
ndn_btree_storeval((char *)v15 + 12, 6LL, 0LL);
if ( (int)ndn_ref_tagged_BLOB(39LL, a4, *(unsigned __int16 *)(a5 + 42), *(unsigned __int16 *)(a5 + 44), &src, &n) < 0 )
return 0xFFFFFFFFLL;
if ( n > 6 )
return 0xFFFFFFFFLL;
memcpy((char *)v15 + 6 - n + 12, src, n);
func1((__int64)&v15[1] + 8, 8, a3);
if ( (int)ndn_ref_tagged_BLOB(60LL, a4, *(unsigned __int16 *)(a5 + 38), *(unsigned __int16 *)(a5 + 40), &src, &n) < 0 )
return 0xFFFFFFFFLL;
if ( n != 32 )
return 0xFFFFFFFFLL;
v15[2] = (__int128)_mm_loadu_si128((const __m128i *)src);
v15[3] = (__int128)_mm_loadu_si128((const __m128i *)src + 1);
return ndn_btree_insert_entry(a1, a2, a6[2], *a6, v15, 64LL);
}

original_code:
int
ndn_btree_insert_content(struct ndn_btree_node *node, int ndx,
                         uint_least64_t cobid,
                         const unsigned char *content_object,
                         struct ndn_parsed_ContentObject *pc,
                         struct ndn_charbuf *flatname)
{
    struct ndn_btree_content_payload payload;
    struct ndn_btree_content_payload *e = &payload;
    int ncomp;
    int res;
    unsigned size;
    unsigned flags = 0;
    const unsigned char *blob = ((void *)0);
    size_t blob_size = 0;
    size = pc->offset[NDN_PCO_E];
    ncomp = ndn_flatname_ncomps(flatname->buf, flatname->length);
    if (ncomp != pc->name_ncomps + 1)
        return(-1);
    memset(e, 'U', sizeof(*e));
    ndn_btree_storeval(&((e)->magic[0]), sizeof((e)->magic), (0xC0));
    ndn_btree_storeval(&((e)->ctype[0]), sizeof((e)->ctype), (pc->type));
    ndn_btree_storeval(&((e)->cobsz[0]), sizeof((e)->cobsz), (size));
    ndn_btree_storeval(&((e)->ncomp[0]), sizeof((e)->ncomp), (ncomp));
    ndn_btree_storeval(&((e)->flags[0]), sizeof((e)->flags), (flags));
    ndn_btree_storeval(&((e)->ttpad[0]), sizeof((e)->ttpad), (0));
    ndn_btree_storeval(&((e)->timex[0]), sizeof((e)->timex), (0));
    res = ndn_ref_tagged_BLOB(NDN_DTAG_Timestamp, content_object,
                              pc->offset[NDN_PCO_B_Timestamp],
                              pc->offset[NDN_PCO_E_Timestamp],
                              &blob, &blob_size);
    if (res < 0 || blob_size > sizeof(e->timex))
        return(-1);
    memcpy(e->timex + sizeof(e->timex) - blob_size, blob, blob_size);
    ndn_btree_storeval64(&((e)->cobid[0]), sizeof((e)->cobid), (cobid));
    res = ndn_ref_tagged_BLOB(NDN_DTAG_PublisherPublicKeyDigest, content_object,
                              pc->offset[NDN_PCO_B_PublisherPublicKeyDigest],
                              pc->offset[NDN_PCO_E_PublisherPublicKeyDigest],
                              &blob, &blob_size);
    if (res < 0 || blob_size != sizeof(e->ppkdg))
        return(-1);
    memcpy(e->ppkdg, blob, sizeof(e->ppkdg));
    res = ndn_btree_insert_entry(node, ndx,
                                 flatname->buf, flatname->length,
                                 e, sizeof(*e));
    return(res);
}

canonical_original_code:
int ndn_btree_insert_content(struct ndn_btree_node *node, int ndx, unsigned long cobid, unsigned char *content_object, struct ndn_parsed_ContentObject *pc, struct ndn_charbuf *flatname)
{
    struct ndn_btree_content_payload payload;
    struct ndn_btree_content_payload *e = &payload;
    int ncomp;
    int res;
    unsigned int size;
    unsigned int flags = 0;
    unsigned char *blob = ((void *)0);
    unsigned long blob_size = 0;
    size = pc->offset[NDN_PCO_E];
    ncomp = ndn_flatname_ncomps(flatname->buf, flatname->length);
    if (ncomp != pc->name_ncomps + 1)
        return(-1);
    memset(e, 'U', sizeof(*e));
    ndn_btree_storeval(&((e)->magic[0]), sizeof((e)->magic), (0xC0));
    ndn_btree_storeval(&((e)->ctype[0]), sizeof((e)->ctype), (pc->type));
    ndn_btree_storeval(&((e)->cobsz[0]), sizeof((e)->cobsz), (size));
    ndn_btree_storeval(&((e)->ncomp[0]), sizeof((e)->ncomp), (ncomp));
    ndn_btree_storeval(&((e)->flags[0]), sizeof((e)->flags), (flags));
    ndn_btree_storeval(&((e)->ttpad[0]), sizeof((e)->ttpad), (0));
    ndn_btree_storeval(&((e)->timex[0]), sizeof((e)->timex), (0));
    res = ndn_ref_tagged_BLOB(NDN_DTAG_Timestamp, content_object,
                              pc->offset[NDN_PCO_B_Timestamp],
                              pc->offset[NDN_PCO_E_Timestamp],
                              &blob, &blob_size);
    if (res < 0 || blob_size > sizeof(e->timex))
        return(-1);
    memcpy(e->timex + sizeof(e->timex) - blob_size, blob, blob_size);
    ndn_btree_storeval64(&((e)->cobid[0]), sizeof((e)->cobid), (cobid));
    res = ndn_ref_tagged_BLOB(NDN_DTAG_PublisherPublicKeyDigest, content_object,
                              pc->offset[NDN_PCO_B_PublisherPublicKeyDigest],
                              pc->offset[NDN_PCO_E_PublisherPublicKeyDigest],
                              &blob, &blob_size);
    if (res < 0 || blob_size != sizeof(e->ppkdg))
        return(-1);
    memcpy(e->ppkdg, blob, sizeof(e->ppkdg));
    res = ndn_btree_insert_entry(node, ndx,
                                 flatname->buf, flatname->length,
                                 e, sizeof(*e));
    return(res);
}

variable_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *

variable_types: ndx int

variable_types:
cobid
unsigned long

variable_types: struct ndn_parsed_ContentObject { int magic; enum ndn_content_type { NDN_CONTENT_DATA=787648, NDN_CONTENT_ENCR=1101969, NDN_CONTENT_GONE=1631044, NDN_CONTENT_KEY=2639423, NDN_CONTENT_LINK=2917194, NDN_CONTENT_NACK=3408010 } type; int name_ncomps; unsigned short offset[-1]; unsigned char digest[32]; int digest_bytes; } *

variable_types:
flatname
struct ndn_charbuf *

variable_types: struct ndn_btree_content_payload { unsigned char magic[1]; unsigned char ctype[3]; unsigned char cobsz[4]; unsigned char ncomp[2]; unsigned char flags[1]; unsigned char ttpad[1]; unsigned char timex[6]; unsigned char actim[6]; unsigned char cobid[8]; unsigned char ppkdg[32]; }

variable_types: ncomp int

variable_types:
size
unsigned int

variable_types:
flags
unsigned int

variable_types:
blob
unsigned char *

user_defined_types: struct ndn_parsed_ContentObject { int magic; enum ndn_content_type type; int name_ncomps; unsigned short offset[-1]; unsigned char digest[32]; int digest_bytes; }


decompiled_code:
void *ndn_indexbuf_create()
{
return calloc(1uLL, 0x18uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(1uLL, 0x18uLL);
}

original_code:
struct ndn_indexbuf *
ndn_indexbuf_create(void)
{
    struct ndn_indexbuf *c;
    c = calloc(1, sizeof(*c));
    return(c);
}

canonical_original_code:
struct ndn_indexbuf *ndn_indexbuf_create()
{
    struct ndn_indexbuf *c;
    c = calloc(1, sizeof(*c));
    return(c);
}

variable_types: struct ndn_indexbuf { unsigned long n; unsigned long limit; unsigned long *buf; } *


decompiled_code:
void __fastcall ndn_indexbuf_destroy(_QWORD **a1)
{
_QWORD *v1; // rbp
void *v3; // rdi

v1 = *a1;
if ( *a1 )
{
v3 = (void *)v1[2];
if ( v3 )
free(v3);
free(v1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func1(_QWORD **a1)
{
_QWORD *v1; // rbp
void *v3; // rdi

v1 = *a1;
if ( *a1 )
{
v3 = (void *)v1[2];
if ( v3 )
free(v3);
free(v1);
*a1 = 0LL;
}
}

original_code:
void
ndn_indexbuf_destroy(struct ndn_indexbuf **cbp)
{
    struct ndn_indexbuf *c = *cbp;
    if (c != ((void *)0)) {
        if (c->buf != ((void *)0)) {
            free(c->buf);
        }
        free(c);
        *cbp = ((void *)0);
    }
}

variable_types: struct ndn_indexbuf { unsigned long n; unsigned long limit; unsigned long *buf; } **


original_code:
size_t *
ndn_indexbuf_reserve(struct ndn_indexbuf *c, size_t n)
{
    size_t newlim = n + c->n;
    size_t oldlim = c->limit;
    size_t *buf = c->buf;
    if (newlim < n)
        return(((void *)0));
    if (newlim > oldlim) {
        if (2 * oldlim > newlim)
            newlim = 2 * oldlim;
        buf = realloc(c->buf, newlim * sizeof(size_t));
        if (buf == ((void *)0))
            return(((void *)0));
        memset(buf + oldlim, 0, (newlim - oldlim) * sizeof(size_t));
        c->buf = buf;
        c->limit = newlim;
    }
    buf += c->n;
    return(buf);
}

canonical_original_code:
unsigned long *ndn_indexbuf_reserve(struct ndn_indexbuf *c, unsigned long n)
{
    unsigned long newlim = n + c->n;
    unsigned long oldlim = c->limit;
    unsigned long *buf = c->buf;
    if (newlim < n)
        return(((void *)0));
    if (newlim > oldlim) {
        if (2 * oldlim > newlim)
            newlim = 2 * oldlim;
        buf = realloc(c->buf, newlim * sizeof(unsigned long));
        if (buf == ((void *)0))
            return(((void *)0));
        memset(buf + oldlim, 0, (newlim - oldlim) * sizeof(unsigned long));
        c->buf = buf;
        c->limit = newlim;
    }
    buf += c->n;
    return(buf);
}

variable_types:
n
unsigned long

variable_types:
newlim
unsigned long

variable_types:
oldlim
unsigned long

variable_types:
buf
unsigned long *

return_type: unsigned long *

user_defined_types: struct ndn_indexbuf { unsigned long n; unsigned long limit; unsigned long *buf; }


decompiled_code:
__int64 __fastcall ndn_indexbuf_append(_QWORD *a1, const void *a2, __int64 a3)
{
char *v4; // rax

v4 = ndn_indexbuf_reserve((__int64)a1, a3);
if ( !v4 )
return 0xFFFFFFFFLL;
memcpy(v4, a2, 8 * a3);
*a1 += a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, const void *a2, __int64 a3)
{
char *v4; // rax

v4 = func2((__int64)a1, a3);
if ( !v4 )
return 0xFFFFFFFFLL;
memcpy(v4, a2, 8 * a3);
*a1 += a3;
return 0LL;
}

original_code:
int
ndn_indexbuf_append(struct ndn_indexbuf *c, const size_t *p, size_t n)
{
    size_t *dst = ndn_indexbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    memcpy(dst, p, n * sizeof(size_t));
    c->n += n;
    return(0);
}

canonical_original_code:
int ndn_indexbuf_append(struct ndn_indexbuf *c, unsigned long *p, unsigned long n)
{
    unsigned long *dst = ndn_indexbuf_reserve(c, n);
    if (dst == ((void *)0))
        return(-1);
    memcpy(dst, p, n * sizeof(unsigned long));
    c->n += n;
    return(0);
}

variable_types:
p
unsigned long *

variable_types:
dst
unsigned long *


decompiled_code:
__int64 __fastcall ndn_indexbuf_append_element(_QWORD *a1, __int64 a2)
{
char *v2; // rax

v2 = ndn_indexbuf_reserve((__int64)a1, 1LL);
if ( !v2 )
return 0xFFFFFFFFLL;
*(_QWORD *)v2 = a2;
++*a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, __int64 a2)
{
char *v2; // rax

v2 = func2((__int64)a1, 1LL);
if ( !v2 )
return 0xFFFFFFFFLL;
*(_QWORD *)v2 = a2;
++*a1;
return 0LL;
}

original_code:
int
ndn_indexbuf_append_element(struct ndn_indexbuf *c, size_t v)
{
    size_t *dst = ndn_indexbuf_reserve(c, 1);
    if (dst == ((void *)0))
        return(-1);
    *dst = v;
    c->n += 1;
    return(0);
}

canonical_original_code:
int ndn_indexbuf_append_element(struct ndn_indexbuf *c, unsigned long v)
{
    unsigned long *dst = ndn_indexbuf_reserve(c, 1);
    if (dst == ((void *)0))
        return(-1);
    *dst = v;
    c->n += 1;
    return(0);
}

variable_types:
v
unsigned long


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

if ( !a1 )
return (unsigned int)-1;
LODWORD(v2) = *(_DWORD *)a1 - 1;
if ( (int)v2 < 0 )
return (unsigned int)-1;
v2 = (int)v2;
while ( 1 )
{
v3 = v2;
if ( *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * v2) == a2 )
break;
if ( (int)--v2 < 0 )
return (unsigned int)-1;
}
return v3;
}

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

if ( !a1 )
return (unsigned int)-1;
LODWORD(v2) = *(_DWORD *)a1 - 1;
if ( (int)v2 < 0 )
return (unsigned int)-1;
v2 = (int)v2;
while ( 1 )
{
v3 = v2;
if ( *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * v2) == a2 )
break;
if ( (int)--v2 < 0 )
return (unsigned int)-1;
}
return v3;
}

original_code:
int
ndn_indexbuf_member(struct ndn_indexbuf *x, size_t val)
{
    int i;
    if (x == ((void *)0))
        return (-1);
    for (i = x->n - 1; i >= 0; i--)
        if (x->buf[i] == val)
            return(i);
    return(-1);
}

canonical_original_code:
int ndn_indexbuf_member(struct ndn_indexbuf *x, unsigned long val)
{
    int i;
    if (x == ((void *)0))
        return (-1);
    for (i = x->n - 1; i >= 0; i--)
        if (x->buf[i] == val)
            return(i);
    return(-1);
}

variable_types:
val
unsigned long

variable_types: i int


original_code:
void
ndn_indexbuf_remove_element(struct ndn_indexbuf *x, size_t val)
{
    int i;
    if (x == ((void *)0)) return;
    for (i = x->n - 1; i >= 0; i--)
        if (x->buf[i] == val) {
            x->buf[i] = x->buf[--x->n];
            return;
        }
}

canonical_original_code:
void ndn_indexbuf_remove_element(struct ndn_indexbuf *x, unsigned long val)
{
    int i;
    if (x == ((void *)0)) return;
    for (i = x->n - 1; i >= 0; i--)
        if (x->buf[i] == val) {
            x->buf[i] = x->buf[--x->n];
            return;
        }
}