decompiled_code:
unsigned __int64 __fastcall unshare_trans(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 8) = clone_bytes(
*(_QWORD *)(a1 + 8),
8LL * *(unsigned int *)(a1 + 16),
"sa copy attrs array (unshare)");
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
*(_QWORD *)(a1 + 8) = clone_bytes(
*(_QWORD *)(a1 + 8),
8LL * *(unsigned int *)(a1 + 16),
"sa copy attrs array (unshare)");
return v2 - __readfsqword(0x28u);
}

original_code:
static void unshare_trans(struct db_trans *tr)
{
 tr->attrs = clone_bytes(tr->attrs, tr->attr_cnt * sizeof(tr->attrs[0]),
  "sa copy attrs array (unshare)");
}

user_defined_types: enum ikev1_oakley_attr { OAKLEY_ATTR_undefined=0, OAKLEY_ENCRYPTION_ALGORITHM=1, OAKLEY_HASH_ALGORITHM=2, OAKLEY_AUTHENTICATION_METHOD=3, OAKLEY_GROUP_DESCRIPTION=4, OAKLEY_GROUP_TYPE=5, OAKLEY_GROUP_PRIME=6, OAKLEY_GROUP_GENERATOR_ONE=7, OAKLEY_GROUP_GENERATOR_TWO=8, OAKLEY_GROUP_CURVE_A=9, OAKLEY_GROUP_CURVE_B=10, OAKLEY_LIFE_TYPE=11, OAKLEY_LIFE_DURATION=12, OAKLEY_PRF=13, OAKLEY_KEY_LENGTH=14, OAKLEY_FIELD_SIZE=15, OAKLEY_GROUP_ORDER=16 }

user_defined_types: enum ikev1_ipsec_attr { SA_LIFE_TYPE=1, SA_LIFE_DURATION=2, GROUP_DESCRIPTION=3, ENCAPSULATION_MODE=4, AUTH_ALGORITHM=5, KEY_LENGTH=6, KEY_ROUNDS=7, COMPRESS_DICT_SIZE=8, COMPRESS_PRIVATE_ALG=9, ECN_TUNNEL=10, ECN_TUNNEL_or_old_SECCTX=expr, ESN_64BIT_SEQNUM=11, IKEv1_IPSEC_ATTR_UNSPEC_12=12, SIG_ENC_ALGO_VAL=13, ADDRESS_PRESERVATION=14, SA_DIRECTION=15, IPSEC_ATTR_VAL_DESCS_ROOF=16, SECCTX=32001 }


repo: libreswan/libreswan

decompiled_code:
unsigned __int64 __fastcall unshare_prop(__int64 a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // rdx
unsigned int v4; // ebx
__int64 v5; // rax
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
v1 = clone_bytes(*(_QWORD *)(a1 + 8), 24LL * *(unsigned int *)(a1 + 16), "sa copy trans array (unshare)");
v2 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v1;
if ( v2 )
{
v3 = v1;
v4 = 0;
while ( 1 )
{
v5 = v4++;
unshare_trans(v3 + 24 * v5);
if ( *(_DWORD *)(a1 + 16) <= v4 )
break;
v3 = *(_QWORD *)(a1 + 8);
}
}
return v7 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // rdx
unsigned int v4; // ebx
__int64 v5; // rax
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
v1 = clone_bytes(*(_QWORD *)(a1 + 8), 24LL * *(unsigned int *)(a1 + 16), "sa copy trans array (unshare)");
v2 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v1;
if ( v2 )
{
v3 = v1;
v4 = 0;
while ( 1 )
{
v5 = v4++;
func3(v3 + 24 * v5);
if ( *(_DWORD *)(a1 + 16) <= v4 )
break;
v3 = *(_QWORD *)(a1 + 8);
}
}
return v7 - __readfsqword(0x28u);
}

original_code:
static void unshare_prop(struct db_prop *p)
{
 unsigned int i;
 p->trans = clone_bytes(p->trans, p->trans_cnt * sizeof(p->trans[0]),
  "sa copy trans array (unshare)");
 for (i = 0; i < p->trans_cnt; i++)
  unshare_trans(&p->trans[i]);
}

variable_types: struct db_prop { unsigned char protoid; struct db_trans { unsigned short transid; struct db_attr { union { enum ikev1_oakley_attr { OAKLEY_ATTR_undefined=0, OAKLEY_ENCRYPTION_ALGORITHM=1, OAKLEY_HASH_ALGORITHM=2, OAKLEY_AUTHENTICATION_METHOD=3, OAKLEY_GROUP_DESCRIPTION=4, OAKLEY_GROUP_TYPE=5, OAKLEY_GROUP_PRIME=6, OAKLEY_GROUP_GENERATOR_ONE=7, OAKLEY_GROUP_GENERATOR_TWO=8, OAKLEY_GROUP_CURVE_A=9, OAKLEY_GROUP_CURVE_B=10, OAKLEY_LIFE_TYPE=11, OAKLEY_LIFE_DURATION=12, OAKLEY_PRF=13, OAKLEY_KEY_LENGTH=14, OAKLEY_FIELD_SIZE=15, OAKLEY_GROUP_ORDER=16 } oakley; enum ikev1_ipsec_attr { SA_LIFE_TYPE=1, SA_LIFE_DURATION=2, GROUP_DESCRIPTION=3, ENCAPSULATION_MODE=4, AUTH_ALGORITHM=5, KEY_LENGTH=6, KEY_ROUNDS=7, COMPRESS_DICT_SIZE=8, COMPRESS_PRIVATE_ALG=9, ECN_TUNNEL=10, ECN_TUNNEL_or_old_SECCTX=expr, ESN_64BIT_SEQNUM=11, IKEv1_IPSEC_ATTR_UNSPEC_12=12, SIG_ENC_ALGO_VAL=13, ADDRESS_PRESERVATION=14, SA_DIRECTION=15, IPSEC_ATTR_VAL_DESCS_ROOF=16, SECCTX=32001 } ipsec; } type; unsigned short val; } *attrs; unsigned int attr_cnt; } *trans; unsigned int trans_cnt; } *

variable_types:
i
unsigned int

return_type: void

user_defined_types: struct db_prop { unsigned char protoid; struct db_trans *trans; unsigned int trans_cnt; }

user_defined_types: struct db_trans { unsigned short transid; struct db_attr *attrs; unsigned int attr_cnt; }

user_defined_types: struct db_attr { union { enum ikev1_oakley_attr oakley; enum ikev1_ipsec_attr ipsec; } type; unsigned short val; }


decompiled_code:
unsigned __int64 __fastcall unshare_propconj(__int64 *a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // rdx
unsigned int v4; // ebx
__int64 v5; // rax
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
v1 = clone_bytes(*a1, 24LL * *((unsigned int *)a1 + 2), "sa copy prop array (unshare)");
v2 = *((_DWORD *)a1 + 2);
*a1 = v1;
if ( v2 )
{
v3 = v1;
v4 = 0;
while ( 1 )
{
v5 = v4++;
unshare_prop(v3 + 24 * v5);
if ( *((_DWORD *)a1 + 2) <= v4 )
break;
v3 = *a1;
}
}
return v7 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(__int64 *a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // rdx
unsigned int v4; // ebx
__int64 v5; // rax
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

v7 = __readfsqword(0x28u);
v1 = clone_bytes(*a1, 24LL * *((unsigned int *)a1 + 2), "sa copy prop array (unshare)");
v2 = *((_DWORD *)a1 + 2);
*a1 = v1;
if ( v2 )
{
v3 = v1;
v4 = 0;
while ( 1 )
{
v5 = v4++;
func4(v3 + 24 * v5);
if ( *((_DWORD *)a1 + 2) <= v4 )
break;
v3 = *a1;
}
}
return v7 - __readfsqword(0x28u);
}

original_code:
static void unshare_propconj(struct db_prop_conj *pc)
{
 unsigned int i;
 pc->props = clone_bytes(pc->props, pc->prop_cnt * sizeof(pc->props[0]),
  "sa copy prop array (unshare)");
 for (i = 0; i < pc->prop_cnt; i++)
  unshare_prop(&pc->props[i]);
}

variable_types: struct db_prop_conj { struct db_prop { unsigned char protoid; struct db_trans { unsigned short transid; struct db_attr { union { enum ikev1_oakley_attr { OAKLEY_ATTR_undefined=0, OAKLEY_ENCRYPTION_ALGORITHM=1, OAKLEY_HASH_ALGORITHM=2, OAKLEY_AUTHENTICATION_METHOD=3, OAKLEY_GROUP_DESCRIPTION=4, OAKLEY_GROUP_TYPE=5, OAKLEY_GROUP_PRIME=6, OAKLEY_GROUP_GENERATOR_ONE=7, OAKLEY_GROUP_GENERATOR_TWO=8, OAKLEY_GROUP_CURVE_A=9, OAKLEY_GROUP_CURVE_B=10, OAKLEY_LIFE_TYPE=11, OAKLEY_LIFE_DURATION=12, OAKLEY_PRF=13, OAKLEY_KEY_LENGTH=14, OAKLEY_FIELD_SIZE=15, OAKLEY_GROUP_ORDER=16 } oakley; enum ikev1_ipsec_attr { SA_LIFE_TYPE=1, SA_LIFE_DURATION=2, GROUP_DESCRIPTION=3, ENCAPSULATION_MODE=4, AUTH_ALGORITHM=5, KEY_LENGTH=6, KEY_ROUNDS=7, COMPRESS_DICT_SIZE=8, COMPRESS_PRIVATE_ALG=9, ECN_TUNNEL=10, ECN_TUNNEL_or_old_SECCTX=expr, ESN_64BIT_SEQNUM=11, IKEv1_IPSEC_ATTR_UNSPEC_12=12, SIG_ENC_ALGO_VAL=13, ADDRESS_PRESERVATION=14, SA_DIRECTION=15, IPSEC_ATTR_VAL_DESCS_ROOF=16, SECCTX=32001 } ipsec; } type; unsigned short val; } *attrs; unsigned int attr_cnt; } *trans; unsigned int trans_cnt; } *props; unsigned int prop_cnt; } *

user_defined_types: struct db_prop_conj { struct db_prop *props; unsigned int prop_cnt; }


decompiled_code:
__int64 __fastcall sa_copy_sa(__int64 a1, __int64 a2)
{
__int64 v3; // r12
__int64 v4; // rsi
__int64 v5; // rdi
__int64 v6; // rax
int v7; // edx
unsigned int v8; // ebx
__int64 v9; // rdi

v3 = clone_bytes(a1, 24LL, "sa copy prop_conj (sa_copy_sa)");
dbg_alloc("sadb", v3, a2);
*(_BYTE *)v3 = 1;
v4 = *(unsigned int *)(v3 + 16);
v5 = *(_QWORD *)(v3 + 8);
*(_BYTE *)(v3 + 1) = *(_BYTE *)(a1 + 1);
v6 = clone_bytes(v5, 16 * v4, "sa copy prop conj array (sa_copy_sa)");
v7 = *(_DWORD *)(v3 + 16);
*(_QWORD *)(v3 + 8) = v6;
if ( v7 )
{
v8 = 0;
while ( 1 )
{
v9 = v8++;
unshare_propconj((__int64 *)(v6 + 16 * v9));
if ( *(_DWORD *)(v3 + 16) <= v8 )
break;
v6 = *(_QWORD *)(v3 + 8);
}
}
return v3;
}

original_code:
struct db_sa *sa_copy_sa(const struct db_sa *sa, where_t where)
{
 struct db_sa *nsa = clone_bytes((const void *)&(*sa), sizeof(*sa), ("sa copy prop_conj (sa_copy_sa)"));
 dbg_alloc("sadb", nsa, where);
 nsa->dynamic = 1;
 nsa->parentSA = sa->parentSA;
 nsa->prop_conjs = clone_bytes(nsa->prop_conjs,
  sizeof(nsa->prop_conjs[0]) * nsa->prop_conj_cnt,
  "sa copy prop conj array (sa_copy_sa)");
 for (unsigned int i = 0; i < nsa->prop_conj_cnt; i++)
  unshare_propconj(&nsa->prop_conjs[i]);
 return nsa;
}

canonical_original_code:
struct db_sa *sa_copy_sa(struct db_sa *sa, struct where *where)
{
 struct db_sa *nsa = clone_bytes((const void *)&(*sa), sizeof(*sa), ("sa copy prop_conj (sa_copy_sa)"));
 dbg_alloc("sadb", nsa, where);
 nsa->dynamic = 1;
 nsa->parentSA = sa->parentSA;
 nsa->prop_conjs = clone_bytes(nsa->prop_conjs,
  sizeof(nsa->prop_conjs[0]) * nsa->prop_conj_cnt,
  "sa copy prop conj array (sa_copy_sa)");
 for (unsigned int i = 0; i < nsa->prop_conj_cnt; i++)
  unshare_propconj(&nsa->prop_conjs[i]);
 return nsa;
}

variable_types:
sa
struct db_sa *

variable_types: struct where { char *func; char *file; long line; } *

variable_types: struct db_sa { _Bool dynamic; _Bool parentSA; struct db_prop_conj { struct db_prop { unsigned char protoid; struct db_trans { unsigned short transid; struct db_attr { union { enum ikev1_oakley_attr { OAKLEY_ATTR_undefined=0, OAKLEY_ENCRYPTION_ALGORITHM=1, OAKLEY_HASH_ALGORITHM=2, OAKLEY_AUTHENTICATION_METHOD=3, OAKLEY_GROUP_DESCRIPTION=4, OAKLEY_GROUP_TYPE=5, OAKLEY_GROUP_PRIME=6, OAKLEY_GROUP_GENERATOR_ONE=7, OAKLEY_GROUP_GENERATOR_TWO=8, OAKLEY_GROUP_CURVE_A=9, OAKLEY_GROUP_CURVE_B=10, OAKLEY_LIFE_TYPE=11, OAKLEY_LIFE_DURATION=12, OAKLEY_PRF=13, OAKLEY_KEY_LENGTH=14, OAKLEY_FIELD_SIZE=15, OAKLEY_GROUP_ORDER=16 } oakley; enum ikev1_ipsec_attr { SA_LIFE_TYPE=1, SA_LIFE_DURATION=2, GROUP_DESCRIPTION=3, ENCAPSULATION_MODE=4, AUTH_ALGORITHM=5, KEY_LENGTH=6, KEY_ROUNDS=7, COMPRESS_DICT_SIZE=8, COMPRESS_PRIVATE_ALG=9, ECN_TUNNEL=10, ECN_TUNNEL_or_old_SECCTX=expr, ESN_64BIT_SEQNUM=11, IKEv1_IPSEC_ATTR_UNSPEC_12=12, SIG_ENC_ALGO_VAL=13, ADDRESS_PRESERVATION=14, SA_DIRECTION=15, IPSEC_ATTR_VAL_DESCS_ROOF=16, SECCTX=32001 } ipsec; } type; unsigned short val; } *attrs; unsigned int attr_cnt; } *trans; unsigned int trans_cnt; } *props; unsigned int prop_cnt; } *prop_conjs; unsigned int prop_conj_cnt; } *

user_defined_types: struct db_sa { _Bool dynamic; _Bool parentSA; struct db_prop_conj *prop_conjs; unsigned int prop_conj_cnt; }


ttoul

decompiled_code:
const char *__fastcall ttoul(_BYTE *a1, unsigned __int64 a2, int a3, unsigned __int64 *a4)
{
const char *result; // rax
unsigned __int64 v6; // rbp
int v7; // edi
_BYTE *v8; // rbp
int v9; // er8
__int64 v10; // rsi
unsigned __int64 v11; // rax
unsigned __int64 v12; // r12
int v13; // edx
int v14; // edx
_BYTE *v15; // r14
signed int v16; // eax
int v17; // er15
char *v18; // rax
char *v19; // rax

result = "empty string";
if ( !a2 )
return result;
v6 = a2;
v7 = a3;
if ( a3 )
{
if ( (a3 & 0xFFFFFFFD) == 8 )
{
v10 = a3;
v11 = 0xFFFFFFFFFFFFFFFFLL / a3;
v8 = &a1[v6];
v9 = ~(v11 * a3);
goto LABEL_6;
}
result = "unsupported number base";
if ( a3 != 16 )
return result;
LABEL_23:
v15 = &a1[v6];
v12 = 0LL;
if ( a1 < &a1[v6] )
{
do
{
v17 = (char)*a1++;
v18 = strchr("0123456789abcdef", v17);
if ( v18 )
{
v16 = (_DWORD)v18 - (unsigned int)"0123456789abcdef";
}
else
{
v19 = strchr("0123456789ABCDEF", v17);
if ( !v19 )
return "non-hex digit in hex number";
v16 = (_DWORD)v19 - (unsigned int)"0123456789ABCDEF";
}
v12 = (16 * v12) | v16;
}
while ( a1 != v15 );
}
result = "hex number too long";
if ( v6 <= 0x10 )
goto LABEL_7;
return result;
}
if ( a2 <= 2 )
{
if ( a2 == 1 )
{
v8 = a1 + 1;
}
else
{
v8 = a1 + 2;
if ( *a1 == 48 )
goto LABEL_20;
}
v9 = 5;
v10 = 10LL;
v7 = 10;
v11 = 0x1999999999999999LL;
goto LABEL_6;
}
if ( *a1 == 48 )
{
if ( (a1[1] & 0xDF) == 88 )
{
v6 = a2 - 2;
a1 += 2;
goto LABEL_23;
}
v8 = &a1[a2];
LABEL_20:
++a1;
v9 = 7;
v10 = 8LL;
v11 = 0x1FFFFFFFFFFFFFFFLL;
v7 = 8;
goto LABEL_6;
}
v8 = &a1[a2];
v9 = 5;
v10 = 10LL;
v11 = 0x1999999999999999LL;
v7 = 10;
LABEL_6:
v12 = 0LL;
if ( a1 >= v8 )
{
LABEL_7:
*a4 = v12;
return 0LL;
}
while ( 1 )
{
v13 = (char)*a1++;
v14 = v13 - 48;
if ( v14 < 0 || v14 >= v7 )
return "non-digit in number";
if ( v12 > v11 || v12 == v11 && v14 > v9 )
return "unsigned-long overflow";
v12 = v14 + v10 * v12;
if ( a1 == v8 )
goto LABEL_7;
}
}

canonical_decompiled_code:
const char *__fastcall func0(_BYTE *a1, unsigned __int64 a2, int a3, unsigned __int64 *a4)
{
const char *result; // rax
unsigned __int64 v6; // rbp
int v7; // edi
_BYTE *v8; // rbp
int v9; // er8
__int64 v10; // rsi
unsigned __int64 v11; // rax
unsigned __int64 v12; // r12
int v13; // edx
int v14; // edx
_BYTE *v15; // r14
signed int v16; // eax
int v17; // er15
char *v18; // rax
char *v19; // rax

result = "empty string";
if ( !a2 )
return result;
v6 = a2;
v7 = a3;
if ( a3 )
{
if ( (a3 & 0xFFFFFFFD) == 8 )
{
v10 = a3;
v11 = 0xFFFFFFFFFFFFFFFFLL / a3;
v8 = &a1[v6];
v9 = ~(v11 * a3);
goto LABEL_6;
}
result = "unsupported number base";
if ( a3 != 16 )
return result;
LABEL_23:
v15 = &a1[v6];
v12 = 0LL;
if ( a1 < &a1[v6] )
{
do
{
v17 = (char)*a1++;
v18 = strchr("0123456789abcdef", v17);
if ( v18 )
{
v16 = (_DWORD)v18 - (unsigned int)"0123456789abcdef";
}
else
{
v19 = strchr("0123456789ABCDEF", v17);
if ( !v19 )
return "non-hex digit in hex number";
v16 = (_DWORD)v19 - (unsigned int)"0123456789ABCDEF";
}
v12 = (16 * v12) | v16;
}
while ( a1 != v15 );
}
result = "hex number too long";
if ( v6 <= 0x10 )
goto LABEL_7;
return result;
}
if ( a2 <= 2 )
{
if ( a2 == 1 )
{
v8 = a1 + 1;
}
else
{
v8 = a1 + 2;
if ( *a1 == 48 )
goto LABEL_20;
}
v9 = 5;
v10 = 10LL;
v7 = 10;
v11 = 0x1999999999999999LL;
goto LABEL_6;
}
if ( *a1 == 48 )
{
if ( (a1[1] & 0xDF) == 88 )
{
v6 = a2 - 2;
a1 += 2;
goto LABEL_23;
}
v8 = &a1[a2];
LABEL_20:
++a1;
v9 = 7;
v10 = 8LL;
v11 = 0x1FFFFFFFFFFFFFFFLL;
v7 = 8;
goto LABEL_6;
}
v8 = &a1[a2];
v9 = 5;
v10 = 10LL;
v11 = 0x1999999999999999LL;
v7 = 10;
LABEL_6:
v12 = 0LL;
if ( a1 >= v8 )
{
LABEL_7:
*a4 = v12;
return 0LL;
}
while ( 1 )
{
v13 = (char)*a1++;
v14 = v13 - 48;
if ( v14 < 0 || v14 >= v7 )
return "non-digit in number";
if ( v12 > v11 || v12 == v11 && v14 > v9 )
return "unsigned-long overflow";
v12 = v14 + v10 * v12;
if ( a1 == v8 )
goto LABEL_7;
}
}

original_code:
static err_t ttoul(const char *src, size_t srclen,
     int base,
     unsigned long *resultp)
{
 const char *stop;
 static const char hex[] = "0123456789abcdef";
 static const char uchex[] = "0123456789ABCDEF";
 int d;
 char c;
 const char *p;
 unsigned long r;
 unsigned long rlimit;
 int dlimit;
 if (srclen == 0)
  return "empty string";
 if (base == 0) {
  if (srclen > 2 && *src == '0' &&
   (*(src + 1) == 'x' || *(src + 1) == 'X'))
   return ttoul(src + 2, srclen - 2, 16, resultp);
  if (srclen > 1 && *src == '0')
   return ttoul(src + 1, srclen - 1, 8, resultp);
  return ttoul(src, srclen, 10, resultp);
 }
 if (base != 8 && base != 10 && base != 16)
  return "unsupported number base";
 r = 0;
 stop = src + srclen;
 if (base == 16) {
  while (src < stop) {
   c = *src++;
   p = strchr(hex, c);
   if (p != ((void *)0)) {
    d = p - hex;
   } else {
    p = strchr(uchex, c);
    if (p == ((void *)0))
     return "non-hex digit in hex number";
    d = p - uchex;
   }
   r = (r << 4) | d;
  }
  if (srclen > sizeof(unsigned long) * 2)
   return "hex number too long";
 } else {
  rlimit = (0x7fffffffffffffffL * 2UL + 1UL) / base;
  dlimit = (int)((0x7fffffffffffffffL * 2UL + 1UL) - rlimit * base);
  while (src < stop) {
   c = *src++;
   d = c - '0';
   if (d < 0 || d >= base)
    return "non-digit in number";
   if (r > rlimit || (r == rlimit && d > dlimit))
    return "unsigned-long overflow";
   r = r * base + d;
  }
 }
 *resultp = r;
 return ((void *)0);
}

canonical_original_code:
static char *ttoul(char *src, unsigned long srclen, int base, unsigned long *resultp)
{
 char *stop;
 static char hex[] = "0123456789abcdef";
 static char uchex[] = "0123456789ABCDEF";
 int d;
 char c;
 char *p;
 unsigned long r;
 unsigned long rlimit;
 int dlimit;
 if (srclen == 0)
  return "empty string";
 if (base == 0) {
  if (srclen > 2 && *src == '0' &&
   (*(src + 1) == 'x' || *(src + 1) == 'X'))
   return ttoul(src + 2, srclen - 2, 16, resultp);
  if (srclen > 1 && *src == '0')
   return ttoul(src + 1, srclen - 1, 8, resultp);
  return ttoul(src, srclen, 10, resultp);
 }
 if (base != 8 && base != 10 && base != 16)
  return "unsupported number base";
 r = 0;
 stop = src + srclen;
 if (base == 16) {
  while (src < stop) {
   c = *src++;
   p = strchr(hex, c);
   if (p != ((void *)0)) {
    d = p - hex;
   } else {
    p = strchr(uchex, c);
    if (p == ((void *)0))
     return "non-hex digit in hex number";
    d = p - uchex;
   }
   r = (r << 4) | d;
  }
  if (srclen > sizeof(unsigned long) * 2)
   return "hex number too long";
 } else {
  rlimit = (0x7fffffffffffffffL * 2UL + 1UL) / base;
  dlimit = (int)((0x7fffffffffffffffL * 2UL + 1UL) - rlimit * base);
  while (src < stop) {
   c = *src++;
   d = c - '0';
   if (d < 0 || d >= base)
    return "non-digit in number";
   if (r > rlimit || (r == rlimit && d > dlimit))
    return "unsigned-long overflow";
   r = r * base + d;
  }
 }
 *resultp = r;
 return ((void *)0);
}

variable_types:
src
char *

variable_types:
srclen
unsigned long

variable_types: base int

variable_types:
resultp
unsigned long *

variable_types:
stop
char *

variable_types:
hex
char []

variable_types:
uchex
char []

variable_types: d int

variable_types: c char

variable_types:
p
char *

variable_types:
r
unsigned long

variable_types:
rlimit
unsigned long

variable_types: dlimit int

return_type: char *


ttoulb

decompiled_code:
const char *__fastcall ttoulb(char *a1, unsigned __int64 a2, int a3, unsigned __int64 a4, unsigned __int64 *a5)
{
const char *result; // rax

if ( !a2 )
a2 = strlen(a1);
result = ttoul(a1, a2, a3, a5);
if ( !result && *a5 > a4 )
result = "too large";
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(char *a1, unsigned __int64 a2, int a3, unsigned __int64 a4, unsigned __int64 *a5)
{
const char *result; // rax

if ( !a2 )
a2 = strlen(a1);
result = func0(a1, a2, a3, a5);
if ( !result && *a5 > a4 )
result = "too large";
return result;
}

original_code:
err_t ttoulb(const char *src,
      size_t srclen,
      int base,
      unsigned long upb,
      unsigned long *resultp)
{
 if (srclen == 0)
  srclen = strlen(src);
 const char *ugh = ttoul(src, srclen, base, resultp);
 return ugh != ((void *)0) ? ugh : *resultp > upb ? "too large" : ((void *)0);
}

canonical_original_code:
char *ttoulb(char *src, unsigned long srclen, int base, unsigned long upb, unsigned long *resultp)
{
 if (srclen == 0)
  srclen = strlen(src);
 char *ugh = ttoul(src, srclen, base, resultp);
 return ugh != ((void *)0) ? ugh : *resultp > upb ? "too large" : ((void *)0);
}

variable_types:
upb
unsigned long

variable_types:
ugh
char *


addrcmp

decompiled_code:
__int64 __fastcall addrcmp(__int64 a1, __int64 a2)
{
char v2; // al
unsigned int v3; // er8

if ( (unsigned __int8)((__int64 (*)(void))address_is_unset)() && (unsigned __int8)address_is_unset(a2) )
return 0;
if ( (unsigned __int8)address_is_unset(a1) )
return (unsigned int)-1;
v2 = address_is_unset(a2);
v3 = 1;
if ( v2 )
return v3;
return ip_bytes_cmp(
*(unsigned int *)(a1 + 4),
*(_QWORD *)(a1 + 8),
*(_QWORD *)(a1 + 16),
*(unsigned int *)(a2 + 4),
*(_QWORD *)(a2 + 8),
*(_QWORD *)(a2 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
char v2; // al
unsigned int v3; // er8

if ( (unsigned __int8)((__int64 (*)(void))address_is_unset)() && (unsigned __int8)address_is_unset(a2) )
return 0;
if ( (unsigned __int8)address_is_unset(a1) )
return (unsigned int)-1;
v2 = address_is_unset(a2);
v3 = 1;
if ( v2 )
return v3;
return ip_bytes_cmp(
*(unsigned int *)(a1 + 4),
*(_QWORD *)(a1 + 8),
*(_QWORD *)(a1 + 16),
*(unsigned int *)(a2 + 4),
*(_QWORD *)(a2 + 8),
*(_QWORD *)(a2 + 16));
}

original_code:
int
addrcmp(const ip_address *l, const ip_address *r)
{
 if (address_is_unset(l) && address_is_unset(r)) {
  return 0;
 }
 if (address_is_unset(l)) {
  return -1;
 }
 if (address_is_unset(r)) {
  return 1;
 }
 return ip_bytes_cmp(l->version, l->bytes,
       r->version, r->bytes);
}

canonical_original_code:
int addrcmp(struct ip_address *l, struct ip_address *r)
{
 if (address_is_unset(l) && address_is_unset(r)) {
  return 0;
 }
 if (address_is_unset(l)) {
  return -1;
 }
 if (address_is_unset(r)) {
  return 1;
 }
 return ip_bytes_cmp(l->version, l->bytes,
       r->version, r->bytes);
}

variable_types: struct ip_address { _Bool is_set; enum ip_version { IPv4=4, IPv6=6 } version; struct ip_bytes { unsigned char byte[16]; } bytes; } *

variable_types:
r
struct ip_address *

return_type: int

user_defined_types: struct ip_address { _Bool is_set; enum ip_version version; struct ip_bytes bytes; }


sameaddr

decompiled_code:
bool __fastcall sameaddr(__int64 a1, __int64 a2)
{
return (unsigned int)addrcmp(a1, a2) == 0;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, __int64 a2)
{
return (unsigned int)func0(a1, a2) == 0;
}

original_code:
_Bool sameaddr(const ip_address *a, const ip_address *b)
{
 return addrcmp(a, b) == 0;
}

canonical_original_code:
_Bool sameaddr(struct ip_address *a, struct ip_address *b)
{
 return addrcmp(a, b) == 0;
}

variable_types:
b
struct ip_address *

user_defined_types: struct ip_bytes { unsigned char byte[16]; }


decompiled_code:
__int64 __fastcall jam_nss_ckf(__int64 a1, __int64 a2)
{
const char *v3; // rsi
__int64 v4; // r13
__int64 v6; // r13
__int64 v7; // rbx
__int64 v8; // rbx

v3 = (const char *)&LC1;
v4 = a2 & 0x800;
if ( (a2 & 0x800) != 0 )
{
a2 ^= 0x800uLL;
v6 = jam_string(a1, &LC1);
v3 = (const char *)&LC0;
v4 = jam_string(a1, "SIGN") + v6;
if ( (a2 & 0x100) == 0 )
{
LABEL_3:
if ( (a2 & 0x200) == 0 )
goto LABEL_4;
LABEL_8:
a2 ^= 0x200uLL;
v8 = jam_string(a1, v3);
v3 = (const char *)&LC0;
v4 += jam_string(a1, "DECRYPT") + v8;
if ( !a2 )
return v4;
LABEL_9:
v4 += jam(a1, "%sCKF_%08lx", v3, a2);
return v4;
}
}
else if ( (a2 & 0x100) == 0 )
{
goto LABEL_3;
}
a2 ^= 0x100uLL;
v7 = jam_string(a1, v3);
v3 = (const char *)&LC0;
v4 += jam_string(a1, "ENCRYPT") + v7;
if ( (a2 & 0x200) != 0 )
goto LABEL_8;
LABEL_4:
if ( a2 )
goto LABEL_9;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
const char *v3; // rsi
__int64 v4; // r13
__int64 v6; // r13
__int64 v7; // rbx
__int64 v8; // rbx

v3 = (const char *)&LC1;
v4 = a2 & 0x800;
if ( (a2 & 0x800) != 0 )
{
a2 ^= 0x800uLL;
v6 = jam_string(a1, &LC1);
v3 = (const char *)&LC0;
v4 = jam_string(a1, "SIGN") + v6;
if ( (a2 & 0x100) == 0 )
{
LABEL_3:
if ( (a2 & 0x200) == 0 )
goto LABEL_4;
LABEL_8:
a2 ^= 0x200uLL;
v8 = jam_string(a1, v3);
v3 = (const char *)&LC0;
v4 += jam_string(a1, "DECRYPT") + v8;
if ( !a2 )
return v4;
LABEL_9:
v4 += jam(a1, "%sCKF_%08lx", v3, a2);
return v4;
}
}
else if ( (a2 & 0x100) == 0 )
{
goto LABEL_3;
}
a2 ^= 0x100uLL;
v7 = jam_string(a1, v3);
v3 = (const char *)&LC0;
v4 += jam_string(a1, "ENCRYPT") + v7;
if ( (a2 & 0x200) != 0 )
goto LABEL_8;
LABEL_4:
if ( a2 )
goto LABEL_9;
return v4;
}

original_code:
size_t jam_nss_ckf(struct jambuf *buf, CK_FLAGS flags)
{
 const char *sep = "";
 size_t size = 0;
 if (flags & 0x00000800) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_SIGN"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000800; };
 if (flags & 0x00000100) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_ENCRYPT"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000100; };
 if (flags & 0x00000200) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_DECRYPT"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000200; };
 if (flags != 0) {
  size += jam(buf, "%sCKF_%08lx", sep, (long) flags);
 }
 return size;
}

canonical_original_code:
unsigned long jam_nss_ckf(struct jambuf *buf, unsigned long flags)
{
 char *sep = "";
 unsigned long size = 0;
 if (flags & 0x00000800) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_SIGN"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000800; };
 if (flags & 0x00000100) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_ENCRYPT"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000100; };
 if (flags & 0x00000200) { size += jam_string(buf, sep); size += jam_string(buf, &"CKF_DECRYPT"[strlen("CKF_")]); sep = "+"; flags ^= 0x00000200; };
 if (flags != 0) {
  size += jam(buf, "%sCKF_%08lx", sep, (long) flags);
 }
 return size;
}

variable_types: struct jambuf { char *array; unsigned long total; unsigned long roof; char *dots; } *

variable_types:
flags
unsigned long

variable_types:
sep
char *

variable_types:
size
unsigned long

return_type: unsigned long


decompiled_code:
__int64 alloc_printf(__int64 a1, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-D8h] BYREF
unsigned __int64 v3; // [rsp+18h] [rbp-C0h]

va_start(va, a1);
v3 = __readfsqword(0x28u);
return alloc_vprintf(a1, (const __m128i *)va);
}

canonical_decompiled_code:
__int64 func1(__int64 a1, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-D8h] BYREF
unsigned __int64 v3; // [rsp+18h] [rbp-C0h]

va_start(va, a1);
v3 = __readfsqword(0x28u);
return func0(a1, (const __m128i *)va);
}

original_code:
char *alloc_printf(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 char *d = alloc_vprintf(fmt, ap);
 __builtin_va_end(ap);
 return d;
}

canonical_original_code:
char *alloc_printf(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 char *d = alloc_vprintf(fmt, ap);
 __builtin_va_end(ap);
 return d;
}

variable_types:
fmt
char *

variable_types:
d
char *


decompiled_code:
__int64 __fastcall chunk2(__int64 a1)
{
return a1;
}

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

original_code:
chunk_t chunk2(void *ptr, size_t len)
{
 return (chunk_t) { .ptr = ptr, .len = len, };
}

canonical_original_code:
struct chunk_t chunk2(void *ptr, unsigned long len)
{
 return (struct chunk_t) { .ptr = ptr, .len = len, };
}

variable_types:
ptr
void *

variable_types:
len
unsigned long

return_type: struct chunk_t { unsigned char *ptr; unsigned long len; }


decompiled_code:
__int64 alloc_chunk()
{
__int64 v0; // rdi

v0 = alloc_bytes();
return chunk2(v0);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rdi

v0 = alloc_bytes();
return func0(v0);
}

original_code:
chunk_t alloc_chunk(size_t count, const char *name)
{
 uint8_t *ptr = ((uint8_t*) alloc_bytes(sizeof(uint8_t) * (count), (name)));
 return chunk2(ptr, count);
}

canonical_original_code:
struct chunk_t alloc_chunk(unsigned long count, char *name)
{
 unsigned char *ptr = ((uint8_t*) alloc_bytes(sizeof(uint8_t) * (count), (name)));
 return chunk2(ptr, count);
}

variable_types:
count
unsigned long

variable_types:
name
char *

variable_types:
ptr
unsigned char *

user_defined_types: struct chunk_t { unsigned char *ptr; unsigned long len; }


decompiled_code:
unsigned __int64 __fastcall free_chunk_content(_QWORD *a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
if ( *a1 )
pfree();
*a1 = 0LL;
a1[1] = 0LL;
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
if ( *a1 )
pfree();
*a1 = 0LL;
a1[1] = 0LL;
return v2 - __readfsqword(0x28u);
}

original_code:
void free_chunk_content(chunk_t *chunk)
{
 if (chunk->ptr != ((void *)0)) {
  pfree(chunk->ptr);
 }
 *chunk = empty_chunk;
}

canonical_original_code:
void free_chunk_content(struct chunk_t *chunk)
{
 if (chunk->ptr != ((void *)0)) {
  pfree(chunk->ptr);
 }
 *chunk = empty_chunk;
}

variable_types: struct chunk_t { unsigned char *ptr; unsigned long len; } *


decompiled_code:
__int64 clone_bytes_as_chunk()
{
__int64 v0; // rdi

v0 = clone_bytes();
return chunk2(v0);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rdi

v0 = clone_bytes();
return func0(v0);
}

original_code:
chunk_t clone_bytes_as_chunk(const void *bytes, size_t sizeof_bytes, const char *name)
{
 return chunk2(clone_bytes(bytes, sizeof_bytes, name), sizeof_bytes);
}

canonical_original_code:
struct chunk_t clone_bytes_as_chunk(void *bytes, unsigned long sizeof_bytes, char *name)
{
 return chunk2(clone_bytes(bytes, sizeof_bytes, name), sizeof_bytes);
}

variable_types:
bytes
void *


decompiled_code:
__int64 clone_bytes_bytes_as_chunk()
{
__int64 v0; // rdi

v0 = clone_bytes_bytes();
return chunk2(v0);
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rdi

v0 = clone_bytes_bytes();
return func0(v0);
}

original_code:
chunk_t clone_bytes_bytes_as_chunk(const void *lhs_ptr, size_t lhs_len,
       const void *rhs_ptr, size_t rhs_len,
       const char *name)
{
 void *new = clone_bytes_bytes(lhs_ptr, lhs_len, rhs_ptr, rhs_len, name);
 return chunk2(new, lhs_len + rhs_len);
}

canonical_original_code:
struct chunk_t clone_bytes_bytes_as_chunk(void *lhs_ptr, unsigned long lhs_len, void *rhs_ptr, unsigned long rhs_len, char *name)
{
 void *new = clone_bytes_bytes(lhs_ptr, lhs_len, rhs_ptr, rhs_len, name);
 return chunk2(new, lhs_len + rhs_len);
}

variable_types:
new
void *


decompiled_code:
unsigned __int64 __fastcall append_chunk_bytes(__int64 a1, __int64 *a2, __int64 a3, __int64 a4)
{
__int64 v4; // r12
__int64 v5; // rdx
__int64 v6; // r13
unsigned __int64 v8; // [rsp+8h] [rbp-20h]

v8 = __readfsqword(0x28u);
v4 = clone_bytes_bytes_as_chunk(*a2, a2[1], a3, a4, a1);
v6 = v5;
free_chunk_content(a2);
*a2 = v4;
a2[1] = v6;
return v8 - __readfsqword(0x28u);
}

original_code:
void append_chunk_bytes(const char *name, chunk_t *lhs,
   const void *rhs_ptr, size_t rhs_len)
{
 chunk_t new = clone_bytes_bytes_as_chunk(lhs->ptr, lhs->len, rhs_ptr, rhs_len, name);
 free_chunk_content(lhs);
 *lhs = new;
}

canonical_original_code:
void append_chunk_bytes(char *name, struct chunk_t *lhs, void *rhs_ptr, unsigned long rhs_len)
{
 struct chunk_t new = clone_bytes_bytes_as_chunk(lhs->ptr, lhs->len, rhs_ptr, rhs_len, name);
 free_chunk_content(lhs);
 *lhs = new;
}

variable_types: struct chunk_t { unsigned char *ptr; unsigned long len; }


decompiled_code:
const char *__fastcall keyblob_to_keyid(__int64 a1, __int64 a2, __int64 a3)
{
bool v3; // cc
const char *result; // rax

v3 = (unsigned __int64)datatot(a1, a2, 64LL, a3, 10LL) <= 9;
result = "key blob is too small";
if ( !v3 )
result = 0LL;
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
bool v3; // cc
const char *result; // rax

v3 = (unsigned __int64)datatot(a1, a2, 64LL, a3, 10LL) <= 9;
result = "key blob is too small";
if ( !v3 )
result = 0LL;
return result;
}

original_code:
err_t keyblob_to_keyid(const uint8_t *src, size_t srclen, keyid_t *dst)
{
 size_t ret = datatot(src, srclen, 64, dst->keyid, sizeof(dst->keyid));
 if (ret < sizeof(dst->keyid)) {
  return "key blob is too small";
 } else {
  return ((void *)0);
 }
}

canonical_original_code:
char *keyblob_to_keyid(unsigned char *src, unsigned long srclen, struct keyid_t *dst)
{
 unsigned long ret = datatot(src, srclen, 64, dst->keyid, sizeof(dst->keyid));
 if (ret < sizeof(dst->keyid)) {
  return "key blob is too small";
 } else {
  return ((void *)0);
 }
}

variable_types:
src
unsigned char *

variable_types:
srclen
unsigned long

variable_types:
dst
struct keyid_t { char keyid[10]; } *

variable_types:
ret
unsigned long


original_code:
err_t splitkey_to_keyid(const uint8_t *e, size_t elen,
   const uint8_t *m, size_t mlen,
   keyid_t *dst)
{
 uint8_t keyblob[sizeof(keyid_t)];
 uint8_t *const blob_end = keyblob + sizeof(keyblob);
 uint8_t *p = keyblob;
 if (elen <= 0xff) {
  *p++ = elen;
 } else if (elen <= 0xffff) {
  *p++ = 0;
  *p++ = (elen >> 8) & 0xff;
  *p++ = elen & 0xff;
 } else {
  *dst = empty_keyid;
  return "unrepresentable exponent length";
 }
 while (elen > 0 && p < blob_end) {
  *p++ = *e++;
  elen--;
 }
 while (mlen > 0 && p < blob_end) {
  *p++ = *m++;
  mlen--;
 }
 return keyblob_to_keyid(keyblob, p - keyblob, dst);
}

canonical_original_code:
char *splitkey_to_keyid(unsigned char *e, unsigned long elen, unsigned char *m, unsigned long mlen, struct keyid_t *dst)
{
 unsigned char keyblob[-1];
 unsigned char *blob_end = keyblob + sizeof(keyblob);
 unsigned char *p = keyblob;
 if (elen <= 0xff) {
  *p++ = elen;
 } else if (elen <= 0xffff) {
  *p++ = 0;
  *p++ = (elen >> 8) & 0xff;
  *p++ = elen & 0xff;
 } else {
  *dst = empty_keyid;
  return "unrepresentable exponent length";
 }
 while (elen > 0 && p < blob_end) {
  *p++ = *e++;
  elen--;
 }
 while (mlen > 0 && p < blob_end) {
  *p++ = *m++;
  mlen--;
 }
 return keyblob_to_keyid(keyblob, p - keyblob, dst);
}

variable_types:
e
unsigned char *

variable_types:
elen
unsigned long

variable_types:
m
unsigned char *

variable_types:
mlen
unsigned long

variable_types:
keyblob
unsigned char [-1]

variable_types:
p
unsigned char *


decompiled_code:
__int64 lsw_fips_system()
{
return 1 - ((unsigned int)((unsigned int)PK11_IsFIPS() == 0) - 1);
}

canonical_decompiled_code:
__int64 func0()
{
return 1 - ((unsigned int)((unsigned int)PK11_IsFIPS() == 0) - 1);
}

original_code:
static enum lsw_fips_mode lsw_fips_system(void)
{
 return PK11_IsFIPS() ? LSW_FIPS_ON : LSW_FIPS_OFF;
}

canonical_original_code:
static enum lsw_fips_mode lsw_fips_system()
{
 return PK11_IsFIPS() ? LSW_FIPS_ON : LSW_FIPS_OFF;
}

return_type: enum lsw_fips_mode { LSW_FIPS_UNKNOWN=0, LSW_FIPS_OFF=1, LSW_FIPS_ON=2 }


decompiled_code:
unsigned __int64 __fastcall lsw_set_fips_mode(int a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
fips_mode = a1;
return v2 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(int a1)
{
unsigned __int64 v2; // [rsp+8h] [rbp-10h]

v2 = __readfsqword(0x28u);
fips_mode = a1;
return v2 - __readfsqword(0x28u);
}

original_code:
void lsw_set_fips_mode(enum lsw_fips_mode fips)
{
 fips_mode = fips;
}

variable_types: enum lsw_fips_mode { LSW_FIPS_UNKNOWN=0, LSW_FIPS_OFF=1, LSW_FIPS_ON=2 }


decompiled_code:
__int64 __fastcall same_secitem_as_chunk(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, __int64 a8)
{
return chunk2(*(unsigned __int128 *)&_mm_loadu_si128((const __m128i *)&a7) >> 64, (unsigned int)a8);
}

original_code:
chunk_t same_secitem_as_chunk(SECItem si)
{
 return chunk2(si.data, si.len);
}

canonical_original_code:
struct chunk_t same_secitem_as_chunk(struct SECItemStr si)
{
 return chunk2(si.data, si.len);
}

variable_types: struct SECItemStr { enum SECItemType { siBuffer=0, siClearDataBuffer=1, siCipherDataBuffer=2, siDERCertBuffer=3, siEncodedCertBuffer=4, siDERNameBuffer=5, siEncodedNameBuffer=6, siAsciiNameString=7, siAsciiString=8, siDEROID=9, siUnsignedInteger=10, siUTCTime=11, siGeneralizedTime=12, siVisibleString=13, siUTF8String=14, siBMPString=15 } type; unsigned char *data; unsigned int len; }

user_defined_types: struct SECItemStr { enum SECItemType type; unsigned char *data; unsigned int len; }


decompiled_code:
__int64 __fastcall same_secitem_as_shunk(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, __int64 a8)
{
return shunk2(*(unsigned __int128 *)&_mm_loadu_si128((const __m128i *)&a7) >> 64, (unsigned int)a8);
}

original_code:
shunk_t same_secitem_as_shunk(SECItem si)
{
 return shunk2(si.data, si.len);
}

canonical_original_code:
struct shunk same_secitem_as_shunk(struct SECItemStr si)
{
 return shunk2(si.data, si.len);
}

return_type: struct shunk { void *ptr; unsigned long len; }

user_defined_types: enum SECItemType { siBuffer=0, siClearDataBuffer=1, siCipherDataBuffer=2, siDERCertBuffer=3, siEncodedCertBuffer=4, siDERNameBuffer=5, siEncodedNameBuffer=6, siAsciiNameString=7, siAsciiString=8, siDEROID=9, siUnsignedInteger=10, siUTCTime=11, siGeneralizedTime=12, siVisibleString=13, siUTF8String=14, siBMPString=15 }


decompiled_code:
__int64 __fastcall clone_secitem_as_chunk(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, unsigned int a8)
{
return clone_bytes(*(unsigned __int128 *)&_mm_loadu_si128((const __m128i *)&a7) >> 64, a8, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, unsigned int a8)
{
return clone_bytes(*(unsigned __int128 *)&_mm_loadu_si128((const __m128i *)&a7) >> 64, a8, a1);
}

original_code:
chunk_t clone_secitem_as_chunk(SECItem si, const char *name)
{
 chunk_t chunk = {
  .len = si.len,
  .ptr = clone_bytes(si.data, si.len, name),
 };
 return chunk;
}

canonical_original_code:
struct chunk_t clone_secitem_as_chunk(struct SECItemStr si, char *name)
{
 struct chunk_t chunk = {
  .len = si.len,
  .ptr = clone_bytes(si.data, si.len, name),
 };
 return chunk;
}

variable_types:
name
char *


decompiled_code:
__int64 refresh_v2_cookie_secret()
{
__int64 result; // rax
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
get_rnd_bytes(&v2_cookie_secret, 32LL);
if ( (cur_debugging & 0x10) != 0 )
result = DBG_dump("v2_cookie_secret", &v2_cookie_secret, 32LL);
else
result = v1 - __readfsqword(0x28u);
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
get_rnd_bytes(&v2_cookie_secret, 32LL);
if ( (cur_debugging & 0x10) != 0 )
result = DBG_dump("v2_cookie_secret", &v2_cookie_secret, 32LL);
else
result = v1 - __readfsqword(0x28u);
return result;
}

original_code:
void refresh_v2_cookie_secret(void)
{
 get_rnd_bytes(&v2_cookie_secret, sizeof(v2_cookie_secret));
 if ((cur_debugging & (((lset_t)1 << (DBG_CRYPT_IX))))) {
  DBG_dump("v2_cookie_secret", &(v2_cookie_secret), sizeof(v2_cookie_secret));
 }
}

canonical_original_code:
void refresh_v2_cookie_secret()
{
 get_rnd_bytes(&v2_cookie_secret, sizeof(v2_cookie_secret));
 if ((cur_debugging & (((unsigned long)1 << (DBG_CRYPT_IX))))) {
  DBG_dump("v2_cookie_secret", &(v2_cookie_secret), sizeof(v2_cookie_secret));
 }
}


decompiled_code:
__int64 __fastcall lswglob_errfunc(const char *a1, __int64 a2)
{
log_error(lswglob_logger, a2, "problem with %s file \"%s\"", (const char *)lswglob_what, a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, __int64 a2)
{
log_error(lswglob_logger, a2, "problem with %s file \"%s\"", (const char *)lswglob_what, a1);
return 1LL;
}

original_code:
static int lswglob_errfunc(const char *epath, int eerrno)
{
 { int e_ = eerrno; log_error(lswglob_logger, e_, "problem with %s file \"%s\"", lswglob_what, epath); };
 return 1;
}

canonical_original_code:
static int lswglob_errfunc(char *epath, int eerrno)
{
 { int e_ = eerrno; log_error(lswglob_logger, e_, "problem with %s file \"%s\"", lswglob_what, epath); };
 return 1;
}

variable_types:
epath
char *

variable_types: eerrno int


decompiled_code:
__int64 __fastcall DBG_dump(__int64 a1, __int64 a2, __int64 a3)
{
if ( a1 )
llog(3145728LL, &global_logger, &LC0, a1);
return llog_dump(3145728LL, &global_logger, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
if ( a1 )
llog(3145728LL, &global_logger, &LC0, a1);
return llog_dump(3145728LL, &global_logger, a2, a3);
}

original_code:
void DBG_dump(const char *label, const void *p, size_t len)
{
 if (label != ((void *)0)) {
  llog(DEBUG_STREAM, &global_logger, "%s", label);
 }
 llog_dump(DEBUG_STREAM, &global_logger, p, len);
}

canonical_original_code:
void DBG_dump(char *label, void *p, unsigned long len)
{
 if (label != ((void *)0)) {
  llog(DEBUG_STREAM, &global_logger, "%s", label);
 }
 llog_dump(DEBUG_STREAM, &global_logger, p, len);
}

variable_types:
label
char *

variable_types:
p
void *

variable_types:
len
unsigned long


decompiled_code:
__int64 init_root_certs()
{
return init_oneshot_timer(6LL, free_root_certs);
}

canonical_decompiled_code:
__int64 func4()
{
return init_oneshot_timer(6LL, free_root_certs);
}

original_code:
void init_root_certs(void)
{
 init_oneshot_timer(EVENT_FREE_ROOT_CERTS, free_root_certs);
}

canonical_original_code:
void init_root_certs()
{
 init_oneshot_timer(EVENT_FREE_ROOT_CERTS, free_root_certs);
}


decompiled_code:
unsigned __int64 unbound_ctx_free()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
if ( dns_ctx )
{
ub_ctx_delete();
dns_ctx = 0LL;
}
return v1 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func1()
{
unsigned __int64 v1; // [rsp+8h] [rbp-10h]

v1 = __readfsqword(0x28u);
if ( dns_ctx )
{
ub_ctx_delete();
dns_ctx = 0LL;
}
return v1 - __readfsqword(0x28u);
}

original_code:
void unbound_ctx_free(void)
{
 if (dns_ctx != ((void *)0)) {
  ub_ctx_delete(dns_ctx);
  dns_ctx = ((void *)0);
 }
}

canonical_original_code:
void unbound_ctx_free()
{
 if (dns_ctx != ((void *)0)) {
  ub_ctx_delete(dns_ctx);
  dns_ctx = ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall hash_spd_route_remote_client(__int64 a1)
{
__int64 v1; // rdi
__int64 v2; // rdx

v1 = shunk2(a1 + 8, 16LL);
return hash_bytes(v1, v2, zero_hash);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rdi
__int64 v2; // rdx

v1 = shunk2(a1 + 8, 16LL);
return hash_bytes(v1, v2, zero_hash);
}

original_code:
static hash_t hash_spd_route_remote_client(const ip_selector *sr)
{
 return ({ shunk_t h_ = shunk2(&(sr->bytes), sizeof(sr->bytes)); hash_bytes(h_.ptr, h_.len, zero_hash); });
}

canonical_original_code:
static struct hash_t hash_spd_route_remote_client(struct ip_selector *sr)
{
 return ({ struct shunk h_ = shunk2(&(sr->bytes), sizeof(sr->bytes)); hash_bytes(h_.ptr, h_.len, zero_hash); });
}

variable_types: struct ip_selector { _Bool is_set; enum ip_version { IPv4=4, IPv6=6 } version; struct ip_bytes { unsigned char byte[16]; } bytes; unsigned int maskbits; unsigned int ipproto; int hport; } *

variable_types: struct shunk { void *ptr; unsigned long len; }

return_type: struct hash_t { unsigned int hash; }

user_defined_types: struct ip_selector { _Bool is_set; enum ip_version version; struct ip_bytes bytes; unsigned int maskbits; unsigned int ipproto; int hport; }


decompiled_code:
__int64 __fastcall lsw_alias_cmp(char *s2, char *s)
{
char *v2; // rbx
size_t v3; // r14
const char *v4; // rbx
size_t v5; // rbp

if ( s )
{
v2 = s;
v3 = strlen(s2);
while ( 1 )
{
v4 = &v2[strspn(v2, " \t")];
if ( !*v4 )
break;
v5 = strcspn(v4, " \t");
if ( v3 == v5 && !strncmp(v4, s2, v3) )
return 1LL;
v2 = (char *)&v4[v5];
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2, char *s)
{
char *v2; // rbx
size_t v3; // r14
const char *v4; // rbx
size_t v5; // rbp

if ( s )
{
v2 = s;
v3 = strlen(s2);
while ( 1 )
{
v4 = &v2[strspn(v2, " \t")];
if ( !*v4 )
break;
v5 = strcspn(v4, " \t");
if ( v3 == v5 && !strncmp(v4, s2, v3) )
return 1LL;
v2 = (char *)&v4[v5];
}
}
return 0LL;
}

original_code:
_Bool lsw_alias_cmp(const char *name, const char *aliases)
{
 if (aliases == ((void *)0))
  return 0;
 size_t nlen = strlen(name);
 for (const char *s = aliases;;) {
  s += strspn(s, " \t");
  if (*s == '\0')
   return 0;
  size_t aw = strcspn(s, " \t");
  if (aw == nlen && (strncmp((s), (name), (nlen)) == 0))
   return 1;
  s += aw;
 }
}

canonical_original_code:
_Bool lsw_alias_cmp(char *name, char *aliases)
{
 if (aliases == ((void *)0))
  return 0;
 unsigned long nlen = strlen(name);
 for (char *s = aliases;;) {
  s += strspn(s, " \t");
  if (*s == '\0')
   return 0;
  unsigned long aw = strcspn(s, " \t");
  if (aw == nlen && (strncmp((s), (name), (nlen)) == 0))
   return 1;
  s += aw;
 }
}

variable_types:
aliases
char *

variable_types:
nlen
unsigned long

variable_types:
s
char *

variable_types:
aw
unsigned long


ttobool

decompiled_code:
const char *__fastcall ttobool(char *s1, _BYTE *a2)
{
const char *result; // rax

if ( !strcasecmp(s1, "1") || !strcasecmp(s1, "yes") || !strcasecmp(s1, "true") || !strcasecmp(s1, "on") )
{
*a2 = 1;
result = 0LL;
}
else if ( !strcasecmp(s1, "0") || !strcasecmp(s1, "no") || !strcasecmp(s1, "false") || !strcasecmp(s1, "off") )
{
*a2 = 0;
result = 0LL;
}
else
{
*a2 = 0;
result = "invalid";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(char *s1, _BYTE *a2)
{
const char *result; // rax

if ( !strcasecmp(s1, "1") || !strcasecmp(s1, "yes") || !strcasecmp(s1, "true") || !strcasecmp(s1, "on") )
{
*a2 = 1;
result = 0LL;
}
else if ( !strcasecmp(s1, "0") || !strcasecmp(s1, "no") || !strcasecmp(s1, "false") || !strcasecmp(s1, "off") )
{
*a2 = 0;
result = 0LL;
}
else
{
*a2 = 0;
result = "invalid";
}
return result;
}

original_code:
err_t ttobool(const char *t, _Bool *b)
{
 if ((strcasecmp((t), ("1")) == 0) ||
     (strcasecmp((t), ("yes")) == 0) ||
     (strcasecmp((t), ("true")) == 0) ||
     (strcasecmp((t), ("on")) == 0)) {
  *b = 1;
  return ((void *)0);
 }
 if ((strcasecmp((t), ("0")) == 0) ||
     (strcasecmp((t), ("no")) == 0) ||
     (strcasecmp((t), ("false")) == 0) ||
     (strcasecmp((t), ("off")) == 0)) {
  *b = 0;
  return ((void *)0);
 }
 *b = 0;
 return "invalid";
}

variable_types:
t
char *

variable_types:
b
_Bool *


decompiled_code:
unsigned __int64 __fastcall dbg_ref(const char *a1, const char *a2, const void *a3, __int64 a4, __int64 a5, __int64 a6)
{
unsigned __int64 v7; // [rsp+8h] [rbp-10h]

v7 = __readfsqword(0x28u);
if ( (cur_debugging & 4) != 0 )
DBG_log(
"%sref %s@%p(%u->%u) (%s() +%lu %s)",
a1,
a2,
a3,
a5,
a6,
*(const char **)a4,
*(_QWORD *)(a4 + 16),
*(const char **)(a4 + 8));
return v7 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(const char *a1, const char *a2, const void *a3, __int64 a4, __int64 a5, __int64 a6)
{
unsigned __int64 v7; // [rsp+8h] [rbp-10h]

v7 = __readfsqword(0x28u);
if ( (cur_debugging & 4) != 0 )
DBG_log(
"%sref %s@%p(%u->%u) (%s() +%lu %s)",
a1,
a2,
a3,
a5,
a6,
*(const char **)a4,
*(_QWORD *)(a4 + 16),
*(const char **)(a4 + 8));
return v7 - __readfsqword(0x28u);
}

original_code:
static void dbg_ref(const char *why, const char *what,
      const void *pointer, where_t where,
      int old_count, int new_count)
{
 if ((cur_debugging & (((lset_t)1 << (DBG_REFCNT_IX))))) {
  DBG_log("%sref %s@%p(%u->%u) ""(%s() +%lu %s)""",
   why, what, pointer, old_count, new_count,
   (where)->func, (where)->line, (where)->file);
 }
}

canonical_original_code:
static void dbg_ref(char *why, char *what, void *pointer, struct where *where, int old_count, int new_count)
{
 if ((cur_debugging & (((unsigned long)1 << (DBG_REFCNT_IX))))) {
  DBG_log("%sref %s@%p(%u->%u) ""(%s() +%lu %s)""",
   why, what, pointer, old_count, new_count,
   (where)->func, (where)->line, (where)->file);
 }
}

variable_types:
why
char *

variable_types:
what
char *

variable_types:
pointer
void *

variable_types: struct where { char *func; char *file; long line; } *


decompiled_code:
unsigned __int64 __fastcall dbg_alloc(const char *a1, const void *a2, __int64 a3)
{
return dbg_ref("new", a1, a2, a3, 0LL, 1LL);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(const char *a1, const void *a2, __int64 a3)
{
return func0("new", a1, a2, a3, 0LL, 1LL);
}

original_code:
void dbg_alloc(const char *what, const void *pointer, where_t where)
{
 dbg_ref("new", what, pointer, where, 0, 1);
}

canonical_original_code:
void dbg_alloc(char *what, void *pointer, struct where *where)
{
 dbg_ref("new", what, pointer, where, 0, 1);
}

user_defined_types: struct where { char *func; char *file; long line; }


decompiled_code:
unsigned __int64 __fastcall dbg_free(const char *a1, const void *a2, __int64 a3)
{
return dbg_ref("del", a1, a2, a3, 1LL, 0LL);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(const char *a1, const void *a2, __int64 a3)
{
return func0("del", a1, a2, a3, 1LL, 0LL);
}

original_code:
void dbg_free(const char *what, const void *pointer, where_t where)
{
 dbg_ref("del", what, pointer, where, 1, 0);
}

canonical_original_code:
void dbg_free(char *what, void *pointer, struct where *where)
{
 dbg_ref("del", what, pointer, where, 1, 0);
}


decompiled_code:
unsigned __int64 __fastcall refcnt_init(const void *a1, int *a2, const char **a3, __int64 a4)
{
unsigned int v6; // er14
unsigned int v7; // eax
unsigned int v8; // er15

pthread_mutex_lock(&refcnt_mutex);
v6 = *a2;
v7 = *a2;
*((_QWORD *)a2 + 1) = a3;
*a2 = v7 + 1;
v8 = *a2;
pthread_mutex_unlock(&refcnt_mutex);
if ( v6 || v8 != 1 )
llog_pexpect(&global_logger, a4, "refcnt for %s@%p should have been 0 initialized", *a3, a1);
return dbg_ref("new", **((const char ***)a2 + 1), a1, a4, v6, v8);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(const void *a1, int *a2, const char **a3, __int64 a4)
{
unsigned int v6; // er14
unsigned int v7; // eax
unsigned int v8; // er15

pthread_mutex_lock(&refcnt_mutex);
v6 = *a2;
v7 = *a2;
*((_QWORD *)a2 + 1) = a3;
*a2 = v7 + 1;
v8 = *a2;
pthread_mutex_unlock(&refcnt_mutex);
if ( v6 || v8 != 1 )
llog_pexpect(&global_logger, a4, "refcnt for %s@%p should have been 0 initialized", *a3, a1);
return func0("new", **((const char ***)a2 + 1), a1, a4, v6, v8);
}

original_code:
void refcnt_init(const void *pointer, struct refcnt *refcnt,
   const struct refcnt_base *base,
   const struct where *where)
{
 unsigned old, new;
 pthread_mutex_lock(&refcnt_mutex);
 {
  old = refcnt->count;
  refcnt->count++;
  refcnt->base = base;
  new = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 if (old != 0 || new != 1) {
  llog_pexpect(&global_logger, where, "refcnt for %s@%p should have been 0 initialized",
       base->what, pointer);
 }
 dbg_ref("new", refcnt->base->what, pointer, where, old, new);
}

canonical_original_code:
void refcnt_init(void *pointer, struct refcnt *refcnt, struct refcnt_base *base, struct where *where)
{
 unsigned int old; unsigned int new;
 pthread_mutex_lock(&refcnt_mutex);
 {
  old = refcnt->count;
  refcnt->count++;
  refcnt->base = base;
  new = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 if (old != 0 || new != 1) {
  llog_pexpect(&global_logger, where, "refcnt for %s@%p should have been 0 initialized",
       base->what, pointer);
 }
 dbg_ref("new", refcnt->base->what, pointer, where, old, new);
}

variable_types: struct refcnt { unsigned int count; struct refcnt_base { char *what; } *base; } *

variable_types:
base
struct refcnt_base *

variable_types:
old
unsigned int

variable_types:
new
unsigned int

user_defined_types: struct refcnt { unsigned int count; struct refcnt_base *base; }


decompiled_code:
unsigned __int64 __fastcall refcnt_addref_where(const char *a1, const void *a2, int *a3, __int64 a4)
{
unsigned int v6; // ebp
unsigned int v7; // er15
unsigned __int64 result; // rax
unsigned __int64 v9; // [rsp+8h] [rbp-40h]

v9 = __readfsqword(0x28u);
if ( a2 )
{
pthread_mutex_lock(&refcnt_mutex);
v6 = (*a3)++;
v7 = *a3;
pthread_mutex_unlock(&refcnt_mutex);
if ( !v6 )
llog_pexpect(&global_logger, a4, "refcnt for %s@%p should have been non-0", a1, a2);
result = dbg_ref("add", **((const char ***)a3 + 1), a2, a4, v6, v7);
}
else if ( (cur_debugging & 1) != 0 )
{
result = DBG_log(
"addref %s@NULL (%s() +%lu %s)",
a1,
*(const char **)a4,
*(_QWORD *)(a4 + 16),
*(const char **)(a4 + 8));
}
else
{
result = v9 - __readfsqword(0x28u);
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(const char *a1, const void *a2, int *a3, __int64 a4)
{
unsigned int v6; // ebp
unsigned int v7; // er15
unsigned __int64 result; // rax
unsigned __int64 v9; // [rsp+8h] [rbp-40h]

v9 = __readfsqword(0x28u);
if ( a2 )
{
pthread_mutex_lock(&refcnt_mutex);
v6 = (*a3)++;
v7 = *a3;
pthread_mutex_unlock(&refcnt_mutex);
if ( !v6 )
llog_pexpect(&global_logger, a4, "refcnt for %s@%p should have been non-0", a1, a2);
result = func0("add", **((const char ***)a3 + 1), a2, a4, v6, v7);
}
else if ( (cur_debugging & 1) != 0 )
{
result = DBG_log(
"addref %s@NULL (%s() +%lu %s)",
a1,
*(const char **)a4,
*(_QWORD *)(a4 + 16),
*(const char **)(a4 + 8));
}
else
{
result = v9 - __readfsqword(0x28u);
}
return result;
}

original_code:
void refcnt_addref_where(const char *what, const void *pointer,
    refcnt_t *refcnt, where_t where)
{
 if (pointer == ((void *)0)) {
  { if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) { DBG_log("addref %s@NULL ""(%s() +%lu %s)""", what, (where)->func, (where)->line, (where)->file); } };
  return;
 }
 unsigned old, new;
 pthread_mutex_lock(&refcnt_mutex);
 {
  old = refcnt->count;
  refcnt->count++;
  new = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 if (old == 0) {
  llog_pexpect(&global_logger, where, "refcnt for %s@%p should have been non-0",
       what, pointer);
 }
 dbg_ref("add", refcnt->base->what, pointer, where, old, new);
}

canonical_original_code:
void refcnt_addref_where(char *what, void *pointer, struct refcnt *refcnt, struct where *where)
{
 if (pointer == ((void *)0)) {
  { if ((cur_debugging & (((unsigned long)1 << (DBG_BASE_IX))))) { DBG_log("addref %s@NULL ""(%s() +%lu %s)""", what, (where)->func, (where)->line, (where)->file); } };
  return;
 }
 unsigned int old; unsigned int new;
 pthread_mutex_lock(&refcnt_mutex);
 {
  old = refcnt->count;
  refcnt->count++;
  new = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 if (old == 0) {
  llog_pexpect(&global_logger, where, "refcnt for %s@%p should have been non-0",
       what, pointer);
 }
 dbg_ref("add", refcnt->base->what, pointer, where, old, new);
}


decompiled_code:
__int64 __fastcall refcnt_peek(unsigned int *a1)
{
unsigned int v1; // er12

pthread_mutex_lock(&refcnt_mutex);
v1 = *a1;
pthread_mutex_unlock(&refcnt_mutex);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1)
{
unsigned int v1; // er12

pthread_mutex_lock(&refcnt_mutex);
v1 = *a1;
pthread_mutex_unlock(&refcnt_mutex);
return v1;
}

original_code:
unsigned refcnt_peek(const refcnt_t *refcnt)
{
 unsigned val;
 pthread_mutex_lock(&refcnt_mutex);
 {
  val = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 return val;
}

canonical_original_code:
unsigned int refcnt_peek(struct refcnt *refcnt)
{
 unsigned int val;
 pthread_mutex_lock(&refcnt_mutex);
 {
  val = refcnt->count;
 }
 pthread_mutex_unlock(&refcnt_mutex);
 return val;
}

variable_types:
val
unsigned int

return_type: unsigned int


decompiled_code:
char *__fastcall nl_addattr_nest(_DWORD *a1, unsigned __int16 a2, __int16 a3)
{
char *v3; // r12

v3 = (char *)a1 + ((*a1 + 3) & 0xFFFFFFFC);
nl_addattr_l(a1, a2, a3, 0LL, 0);
return v3;
}

canonical_decompiled_code:
char *__fastcall func1(_DWORD *a1, unsigned __int16 a2, __int16 a3)
{
char *v3; // r12

v3 = (char *)a1 + ((*a1 + 3) & 0xFFFFFFFC);
func0(a1, a2, a3, 0LL, 0);
return v3;
}

original_code:
struct rtattr *nl_addattr_nest(struct nlmsghdr *n, int maxlen, int type)
{
 struct rtattr *nest = ((struct rtattr *) (((void *) (n)) + ( (((n)->nlmsg_len)+4U -1) & ~(4U -1) )));
 nl_addattr_l(n, maxlen, type, ((void *)0), 0);
 return nest;
}

variable_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; } *

variable_types: maxlen int

variable_types: type int

variable_types: struct rtattr { unsigned short rta_len; unsigned short rta_type; } *


decompiled_code:
unsigned __int64 __fastcall nl_addattr_nest_end(_WORD *a1, _WORD *a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a2 = ((*a1 + 3) & 0xFFFC) + (_WORD)a1 - (_WORD)a2;
return v3 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_WORD *a1, _WORD *a2)
{
unsigned __int64 v3; // [rsp+8h] [rbp-10h]

v3 = __readfsqword(0x28u);
*a2 = ((*a1 + 3) & 0xFFFC) + (_WORD)a1 - (_WORD)a2;
return v3 - __readfsqword(0x28u);
}

original_code:
void nl_addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
{
 nest->rta_len = (void *)((struct rtattr *) (((void *) (n)) + ( (((n)->nlmsg_len)+4U -1) & ~(4U -1) ))) - (void *)nest;
}

user_defined_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; }

user_defined_types: struct rtattr { unsigned short rta_len; unsigned short rta_type; }


decompiled_code:
unsigned __int64 __fastcall nl_addattrstrz(_DWORD *a1, unsigned __int16 a2, __int16 a3, const char *a4)
{
int v6; // eax

v6 = strlen(a4);
return nl_addattr_l(a1, a2, a3, a4, v6 + 1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(_DWORD *a1, unsigned __int16 a2, __int16 a3, const char *a4)
{
int v6; // eax

v6 = strlen(a4);
return func0(a1, a2, a3, a4, v6 + 1);
}

original_code:
void nl_addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str )
{
 nl_addattr_l(n, maxlen, type, str, strlen(str)+1);
}

canonical_original_code:
void nl_addattrstrz(struct nlmsghdr *n, int maxlen, int type, char *str)
{
 nl_addattr_l(n, maxlen, type, str, strlen(str)+1);
}

variable_types:
str
char *


decompiled_code:
unsigned __int64 __fastcall nl_addattr32(_DWORD *a1, unsigned __int16 a2, __int16 a3, int a4)
{
int v5[3]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int64 v6; // [rsp+18h] [rbp-10h]

v5[0] = a4;
v6 = __readfsqword(0x28u);
nl_addattr_l(a1, a2, a3, v5, 4);
return v6 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(_DWORD *a1, unsigned __int16 a2, __int16 a3, int a4)
{
int v5[3]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int64 v6; // [rsp+18h] [rbp-10h]

v5[0] = a4;
v6 = __readfsqword(0x28u);
func0(a1, a2, a3, v5, 4);
return v6 - __readfsqword(0x28u);
}

original_code:
void nl_addattr32(struct nlmsghdr *n, int maxlen, int type, const uint32_t data)
{
 nl_addattr_l(n, maxlen, type, &data, sizeof(uint32_t));
}

canonical_original_code:
void nl_addattr32(struct nlmsghdr *n, int maxlen, int type, unsigned int data)
{
 nl_addattr_l(n, maxlen, type, &data, sizeof(unsigned int));
}

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall jam_va_nss_error_code(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
jam(a1, "NSS: ");
jam_va_list(a1, a3, a4);
jam(a1, ": ");
return jam_nss_error_code(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
jam(a1, "NSS: ");
jam_va_list(a1, a3, a4);
jam(a1, ": ");
return jam_nss_error_code(a1, a2);
}

original_code:
__attribute__((format(printf, 3, 0)))
static void jam_va_nss_error_code(struct jambuf *buf, PRErrorCode code,
      const char *message, va_list ap)
{
 jam(buf, "NSS: ");
 jam_va_list(buf, message, ap);
 __builtin_va_end(ap);
 jam(buf, ": ");
 jam_nss_error_code(buf, code);
}

canonical_original_code:
static void jam_va_nss_error_code(struct jambuf *buf, int code, char *message, __builtin_va_list ap)
{
 jam(buf, "NSS: ");
 jam_va_list(buf, message, ap);
 __builtin_va_end(ap);
 jam(buf, ": ");
 jam_nss_error_code(buf, code);
}

variable_types: struct jambuf { char *array; unsigned long total; unsigned long roof; char *dots; } *

variable_types: code int

variable_types:
message
char *


decompiled_code:
void *__fastcall bool_str(char a1)
{
void *result; // rax

result = &LC0;
if ( !a1 )
result = &LC1;
return result;
}

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

result = &LC0;
if ( !a1 )
result = &LC1;
return result;
}

original_code:
const char *bool_str(_Bool b)
{
 return b ? "yes" : "no";
}


decompiled_code:
__int64 init_constants()
{
check_enum_names(&en_checklist, 64LL);
return enum_enum_table(&v2_transform_ID_enums, -42LL);
}

canonical_decompiled_code:
__int64 func3()
{
check_enum_names(&en_checklist, 64LL);
return enum_enum_table(&v2_transform_ID_enums, -42LL);
}

original_code:
void init_constants(void)
{
 check_enum_names((en_checklist), (sizeof(en_checklist) / sizeof(*(en_checklist))));
 (void) enum_enum_table(&v2_transform_ID_enums, -42UL);
}

canonical_original_code:
void init_constants()
{
 check_enum_names((en_checklist), (sizeof(en_checklist) / sizeof(*(en_checklist))));
 (void) enum_enum_table(&v2_transform_ID_enums, -42UL);
}


decompiled_code:
void __fastcall libreswan_bad_case(const char *a1, __int64 a2, __int64 a3)
{
llog_passert(&global_logger, a3, "switch (%s) case %ld (0x%lx) unexpected", a1, a2, a2);
JUMPOUT(0x33LL);
}

canonical_decompiled_code:
void __fastcall func0(const char *a1, __int64 a2, __int64 a3)
{
llog_passert(&global_logger, a3, "switch (%s) case %ld (0x%lx) unexpected", a1, a2, a2);
JUMPOUT(0x33LL);
}

original_code:
void libreswan_bad_case(const char *expression, long value, where_t where)
{
 llog_passert(&global_logger, where,
       "switch (%s) case %ld (0x%lx) unexpected",
       expression, value, value);
}

canonical_original_code:
void libreswan_bad_case(char *expression, long value, struct where *where)
{
 llog_passert(&global_logger, where,
       "switch (%s) case %ld (0x%lx) unexpected",
       expression, value, value);
}

variable_types:
expression
char *

variable_types: value long


decompiled_code:
__int64 __fastcall all_zero(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi

if ( !a2 )
return 1LL;
v2 = &a1[a2];
while ( !*a1 )
{
if ( ++a1 == v2 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi

if ( !a2 )
return 1LL;
v2 = &a1[a2];
while ( !*a1 )
{
if ( ++a1 == v2 )
return 1LL;
}
return 0LL;
}

original_code:
_Bool all_zero(const unsigned char *m, size_t len)
{
 size_t i;
 for (i = 0; i != len; i++)
  if (m[i] != '\0')
   return 0;
 return 1;
}

canonical_original_code:
_Bool all_zero(unsigned char *m, unsigned long len)
{
 unsigned long i;
 for (i = 0; i != len; i++)
  if (m[i] != '\0')
   return 0;
 return 1;
}

variable_types:
m
unsigned char *

variable_types:
len
unsigned long

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall fuzz_rekey_margin(__int64 a1, unsigned int a2, int a3)
{
int v4; // eax

if ( a2 == 1 )
{
v4 = get_rnd_uintmax((unsigned int)(a3 + 1));
deltatime_scale(a1, (unsigned int)(v4 + 100), 100LL);
}
else
{
if ( a2 != 2 )
libreswan_bad_case("role", a2, here_6);
deltatime_scale(a1, 1LL, 2LL);
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, int a3)
{
int v4; // eax

if ( a2 == 1 )
{
v4 = get_rnd_uintmax((unsigned int)(a3 + 1));
deltatime_scale(a1, (unsigned int)(v4 + 100), 100LL);
}
else
{
if ( a2 != 2 )
libreswan_bad_case("role", a2, here_6);
deltatime_scale(a1, 1LL, 2LL);
}
return a1;
}

original_code:
deltatime_t fuzz_rekey_margin(enum sa_role role, deltatime_t marg, unsigned fuzz_percent)
{
 switch (role) {
 case SA_INITIATOR:
  return deltatime_scale(marg,
           100 + get_rnd_uintmax( fuzz_percent + 1),
           100);
 case SA_RESPONDER:
  return deltatime_scale(marg, 1, 2);
 default:
  libreswan_bad_case("role", (role), ({ static const struct where here = { .func = __func__, .file = "programs/pluto/rekeyfuzz.c", .line = 55, }; &here; }));
 }
}

canonical_original_code:
struct deltatime_t fuzz_rekey_margin(enum sa_role role, struct deltatime_t marg, unsigned int fuzz_percent)
{
 switch (role) {
 case SA_INITIATOR:
  return deltatime_scale(marg,
           100 + get_rnd_uintmax( fuzz_percent + 1),
           100);
 case SA_RESPONDER:
  return deltatime_scale(marg, 1, 2);
 default:
  libreswan_bad_case("role", (role), ({ static struct where here = { .func = __func__, .file = "programs/pluto/rekeyfuzz.c", .line = 55, }; &here; }));
 }
}

variable_types: enum sa_role { SA_INITIATOR=1, SA_RESPONDER=2 }

variable_types:
marg
struct deltatime_t

variable_types: struct where { char *func; char *file; long line; }

return_type: struct deltatime_t { struct timeval { long tv_sec; long tv_usec; } dt; _Bool is_set; }

user_defined_types: struct deltatime_t { struct timeval dt; _Bool is_set; }


decompiled_code:
__int64 __fastcall create_ppk_id_payload(const __m128i *a1, __int64 a2)
{
*(_DWORD *)a2 = 2;
*(__m128i *)(a2 + 8) = _mm_loadu_si128(a1);
return 1LL;
}

original_code:
_Bool create_ppk_id_payload(chunk_t *ppk_id, struct ppk_id_payload *payl)
{
 payl->type = PPK_ID_FIXED;
 payl->ppk_id = *ppk_id;
 return 1;
}

canonical_original_code:
_Bool create_ppk_id_payload(struct chunk_t *ppk_id, struct ppk_id_payload *payl)
{
 payl->type = PPK_ID_FIXED;
 payl->ppk_id = *ppk_id;
 return 1;
}

variable_types: struct chunk_t { unsigned char *ptr; unsigned long len; } *

variable_types: struct ppk_id_payload { enum ikev2_ppk_id_type { PPK_ID_OPAQUE=1, PPK_ID_FIXED=2 } type; struct chunk_t ppk_id; } *

user_defined_types: struct ppk_id_payload { enum ikev2_ppk_id_type type; struct chunk_t ppk_id; }


decompiled_code:
__int64 __fastcall jam_port_range(__int64 a1, __int64 a2, unsigned int a3)
{
if ( !(_BYTE)a2 )
return jam(a1, "<unset-port-range>");
if ( HIDWORD(a2) == a3 )
return jam(a1, "%u", HIDWORD(a2));
return jam(a1, "%u-%u", HIDWORD(a2), a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int a3)
{
if ( !(_BYTE)a2 )
return jam(a1, "<unset-port-range>");
if ( HIDWORD(a2) == a3 )
return jam(a1, "%u", HIDWORD(a2));
return jam(a1, "%u-%u", HIDWORD(a2), a3);
}

original_code:
size_t jam_port_range(struct jambuf *buf, ip_port_range r)
{
 if (!r.is_set) {
  return jam(buf, "<unset-port-range>");
 }
 if (r.lo == r.hi) {
  return jam(buf, "%u", r.lo);
 } else {
  return jam(buf, "%u-%u", r.lo, r.hi);
 }
}

canonical_original_code:
unsigned long jam_port_range(struct jambuf *buf, struct ip_port_range r)
{
 if (!r.is_set) {
  return jam(buf, "<unset-port-range>");
 }
 if (r.lo == r.hi) {
  return jam(buf, "%u", r.lo);
 } else {
  return jam(buf, "%u-%u", r.lo, r.hi);
 }
}

variable_types: struct ip_port_range { _Bool is_set; unsigned int lo; unsigned int hi; }

user_defined_types: struct jambuf { char *array; unsigned long total; unsigned long roof; char *dots; }


decompiled_code:
__int64 __fastcall str_port_range(__int64 a1, unsigned int a2, __int64 a3)
{
char v5[40]; // [rsp+10h] [rbp-48h] BYREF
unsigned __int64 v6; // [rsp+38h] [rbp-20h]

v6 = __readfsqword(0x28u);
array_as_jambuf(v5, a3, 13LL);
jam_port_range((__int64)v5, a1, a2);
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2, __int64 a3)
{
char v5[40]; // [rsp+10h] [rbp-48h] BYREF
unsigned __int64 v6; // [rsp+38h] [rbp-20h]

v6 = __readfsqword(0x28u);
array_as_jambuf(v5, a3, 13LL);
func1((__int64)v5, a1, a2);
return a3;
}

original_code:
const char *str_port_range(ip_port_range port, port_range_buf *buf)
{
 struct jambuf jambuf = array_as_jambuf((buf->buf), sizeof(buf->buf));
 jam_port_range(&jambuf, port);
 return buf->buf;
}

canonical_original_code:
char *str_port_range(struct ip_port_range port, struct port_range_buf *buf)
{
 struct jambuf jambuf = array_as_jambuf((buf->buf), sizeof(buf->buf));
 jam_port_range(&jambuf, port);
 return buf->buf;
}

variable_types: struct port_range_buf { char buf[-1]; } *

variable_types: struct jambuf { char *array; unsigned long total; unsigned long roof; char *dots; }

user_defined_types: struct ip_port_range { _Bool is_set; unsigned int lo; unsigned int hi; }


decompiled_code:
__int64 __fastcall vid_is_oppo(void *s1, size_t n)
{
unsigned int v2; // er13
const void *v3; // rax
__int64 v4; // rdx

v2 = 0;
v3 = (const void *)shunk_from_vendorid(66LL);
if ( n == v4 && !memcmp(s1, v3, n) )
{
v2 = 1;
if ( (cur_debugging & 1) != 0 )
DBG_log("VID_OPPORTUNISTIC received");
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *s1, size_t n)
{
unsigned int v2; // er13
const void *v3; // rax
__int64 v4; // rdx

v2 = 0;
v3 = (const void *)shunk_from_vendorid(66LL);
if ( n == v4 && !memcmp(s1, v3, n) )
{
v2 = 1;
if ( (cur_debugging & 1) != 0 )
DBG_log("VID_OPPORTUNISTIC received");
}
return v2;
}

original_code:
_Bool vid_is_oppo(const char *vid, size_t len)
{
 shunk_t oppo = shunk_from_vendorid(VID_OPPORTUNISTIC);
 if (oppo.len == len && (memcmp((vid), (oppo.ptr), (len)) == 0)) {
  { if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) { DBG_log("VID_OPPORTUNISTIC received"); } };
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
_Bool vid_is_oppo(char *vid, unsigned long len)
{
 struct shunk oppo = shunk_from_vendorid(VID_OPPORTUNISTIC);
 if (oppo.len == len && (memcmp((vid), (oppo.ptr), (len)) == 0)) {
  { if ((cur_debugging & (((unsigned long)1 << (DBG_BASE_IX))))) { DBG_log("VID_OPPORTUNISTIC received"); } };
  return 1;
 } else {
  return 0;
 }
}

variable_types:
vid
char *

variable_types: struct shunk { void *ptr; unsigned long len; }


decompiled_code:
__int64 __fastcall str_protoport(__int64 a1, __int64 a2)
{
double v2; // xmm0_8
_QWORD v4[9]; // [rsp+0h] [rbp-48h] BYREF

v4[5] = __readfsqword(0x28u);
v2 = array_as_jambuf(v4, a2, 67LL);
jam_protoport((__int64)v4, a1, v2);
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
double v2; // xmm0_8
_QWORD v4[9]; // [rsp+0h] [rbp-48h] BYREF

v4[5] = __readfsqword(0x28u);
v2 = array_as_jambuf(v4, a2, 67LL);
func1((__int64)v4, a1, v2);
return a2;
}

original_code:
const char *str_protoport(const ip_protoport *protoport, protoport_buf *buf)
{
 struct jambuf jambuf = array_as_jambuf((buf->buf), sizeof(buf->buf));
 jam_protoport(&jambuf, protoport);
 return buf->buf;
}

canonical_original_code:
char *str_protoport(struct ip_protoport *protoport, struct protoport_buf *buf)
{
 struct jambuf jambuf = array_as_jambuf((buf->buf), sizeof(buf->buf));
 jam_protoport(&jambuf, protoport);
 return buf->buf;
}

variable_types: struct ip_protoport { _Bool is_set; _Bool has_port_wildcard; unsigned int hport; unsigned int ipproto; } *

variable_types: struct protoport_buf { char buf[-1]; } *


decompiled_code:
__int64 __fastcall jam_nss_secitem(__int64 a1, __int64 a2)
{
if ( !a2 )
return jam_string(a1, "(SECItem*)NULL");
jam_nss_secitemtype_isra_0(a1, *(_DWORD *)a2);
jam(a1, ": ");
jam_dump_bytes(a1, *(_QWORD *)(a2 + 8), *(unsigned int *)(a2 + 16));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
if ( !a2 )
return jam_string(a1, "(SECItem*)NULL");
func0(a1, *(_DWORD *)a2);
jam(a1, ": ");
jam_dump_bytes(a1, *(_QWORD *)(a2 + 8), *(unsigned int *)(a2 + 16));
return 0LL;
}

original_code:
size_t jam_nss_secitem(struct jambuf *buf, const SECItem *secitem)
{
 size_t size = 0;
 if (secitem == ((void *)0)) {
  size = jam_string(buf, "(SECItem*)NULL");
 } else {
  jam_nss_secitemtype(buf, secitem->type);
  jam(buf, ": ");
  jam_dump_bytes(buf, secitem->data, secitem->len);
 }
 return size;
}

canonical_original_code:
unsigned long jam_nss_secitem(struct jambuf *buf, struct SECItemStr *secitem)
{
 unsigned long size = 0;
 if (secitem == ((void *)0)) {
  size = jam_string(buf, "(SECItem*)NULL");
 } else {
  jam_nss_secitemtype(buf, secitem->type);
  jam(buf, ": ");
  jam_dump_bytes(buf, secitem->data, secitem->len);
 }
 return size;
}

variable_types: struct jambuf { char *array; unsigned long total; unsigned long roof; char *dots; } *

variable_types: struct SECItemStr { enum SECItemType { siBuffer=0, siClearDataBuffer=1, siCipherDataBuffer=2, siDERCertBuffer=3, siEncodedCertBuffer=4, siDERNameBuffer=5, siEncodedNameBuffer=6, siAsciiNameString=7, siAsciiString=8, siDEROID=9, siUnsignedInteger=10, siUTCTime=11, siGeneralizedTime=12, siVisibleString=13, siUTF8String=14, siBMPString=15 } type; unsigned char *data; unsigned int len; } *

variable_types:
size
unsigned long


decompiled_code:
unsigned __int64 __fastcall free_messages(__int64 *a1)
{
__int64 v1; // rbp
unsigned __int64 i; // [rsp+8h] [rbp-20h]

v1 = *a1;
for ( i = __readfsqword(0x28u); *a1; v1 = *a1 )
{
*a1 = *(_QWORD *)(v1 + 16);
free_chunk_content(v1);
pfree(v1);
}
return i - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 *a1)
{
__int64 v1; // rbp
unsigned __int64 i; // [rsp+8h] [rbp-20h]

v1 = *a1;
for ( i = __readfsqword(0x28u); *a1; v1 = *a1 )
{
*a1 = *(_QWORD *)(v1 + 16);
free_chunk_content(v1);
pfree(v1);
}
return i - __readfsqword(0x28u);
}

original_code:
static void free_messages(struct message **messages)
{
 while (*messages != ((void *)0)) {
  struct message *m = (*messages);
  *messages = m->next;
  free_chunk_content(&m->body);
  pfree(m);
 }
}

variable_types: struct message { struct chunk_t { unsigned char *ptr; unsigned long len; } body; struct message *next; } **

user_defined_types: struct message { struct chunk_t body; struct message *next; }


decompiled_code:
__int64 gng_vh_start()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-18h]

v0 = tilemap_create(get_fg_tile_info, &tilemap_scan_rows, 1LL, 8LL, 8LL, 32LL, 32LL);
LODWORD(v3) = 32;
fg_tilemap = v0;
v1 = tilemap_create(get_bg_tile_info, &tilemap_scan_cols, 2LL, 16LL, 16LL, 32LL, v3);
bg_tilemap = v1;
if ( !v1 || !fg_tilemap )
return 1LL;
*(_DWORD *)(fg_tilemap + 132) = 3;
*(_QWORD *)(v1 + 136) = 0x1000000FFLL;
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-18h]

v0 = tilemap_create(get_fg_tile_info, &tilemap_scan_rows, 1LL, 8LL, 8LL, 32LL, 32LL);
LODWORD(v3) = 32;
fg_tilemap = v0;
v1 = tilemap_create(get_bg_tile_info, &tilemap_scan_cols, 2LL, 16LL, 16LL, 32LL, v3);
bg_tilemap = v1;
if ( !v1 || !fg_tilemap )
return 1LL;
*(_DWORD *)(fg_tilemap + 132) = 3;
*(_QWORD *)(v1 + 136) = 0x1000000FFLL;
return 0LL;
}

original_code:
int gng_vh_start(void)
{
 fg_tilemap = tilemap_create(get_fg_tile_info,tilemap_scan_rows,0x01,8,8,32,32);
 bg_tilemap = tilemap_create(get_bg_tile_info,tilemap_scan_cols,0x02, 16,16,32,32);
 if (!fg_tilemap || !bg_tilemap)
  return 1;
 fg_tilemap->transparent_pen = 3;
 bg_tilemap->transmask[0] = 0xff;
 bg_tilemap->transmask[1] = 0x01;
 return 0;
}

canonical_original_code:
int gng_vh_start()
{
 fg_tilemap = tilemap_create(get_fg_tile_info,tilemap_scan_rows,0x01,8,8,32,32);
 bg_tilemap = tilemap_create(get_bg_tile_info,tilemap_scan_cols,0x02, 16,16,32,32);
 if (!fg_tilemap || !bg_tilemap)
  return 1;
 fg_tilemap->transparent_pen = 3;
 bg_tilemap->transmask[0] = 0xff;
 bg_tilemap->transmask[1] = 0x01;
 return 0;
}


decompiled_code:
__int64 __fastcall gng_bgscrollx_w(int a1, char a2)
{
scrollx_2[a1] = a2;
return tilemap_set_scrollx(bg_tilemap, 0LL, (unsigned __int8)scrollx_2[0] + ((unsigned __int8)byte_CD3 << 8));
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, char a2)
{
scrollx_2[a1] = a2;
return tilemap_set_scrollx(bg_tilemap, 0LL, (unsigned __int8)scrollx_2[0] + ((unsigned __int8)byte_CD3 << 8));
}

original_code:
void gng_bgscrollx_w(offs_t offset,data_t data)
{
 static unsigned char scrollx[2];
 scrollx[offset] = data;
 tilemap_set_scrollx( bg_tilemap, 0, scrollx[0] + 256 * scrollx[1] );
}

canonical_original_code:
void gng_bgscrollx_w(unsigned int offset, unsigned int data)
{
 static unsigned char scrollx[2];
 scrollx[offset] = data;
 tilemap_set_scrollx( bg_tilemap, 0, scrollx[0] + 256 * scrollx[1] );
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int

variable_types:
scrollx
unsigned char [2]


decompiled_code:
__int64 __fastcall gng_bgscrolly_w(int a1, char a2)
{
scrolly_1[a1] = a2;
return tilemap_set_scrolly(bg_tilemap, 0LL, (unsigned __int8)scrolly_1[0] + ((unsigned __int8)byte_CD1 << 8));
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, char a2)
{
scrolly_1[a1] = a2;
return tilemap_set_scrolly(bg_tilemap, 0LL, (unsigned __int8)scrolly_1[0] + ((unsigned __int8)byte_CD1 << 8));
}

original_code:
void gng_bgscrolly_w(offs_t offset,data_t data)
{
 static unsigned char scrolly[2];
 scrolly[offset] = data;
 tilemap_set_scrolly( bg_tilemap, 0, scrolly[0] + 256 * scrolly[1] );
}

canonical_original_code:
void gng_bgscrolly_w(unsigned int offset, unsigned int data)
{
 static unsigned char scrolly[2];
 scrolly[offset] = data;
 tilemap_set_scrolly( bg_tilemap, 0, scrolly[0] + 256 * scrolly[1] );
}

variable_types:
scrolly
unsigned char [2]


decompiled_code:
__int64 __fastcall gng_flipscreen_w(__int64 a1, char a2)
{
flipscreen = (a2 & 1) == 0;
return tilemap_set_flip(0LL, -flipscreen & 3);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, char a2)
{
flipscreen = (a2 & 1) == 0;
return tilemap_set_flip(0LL, -flipscreen & 3);
}

original_code:
void gng_flipscreen_w(offs_t offset,data_t data)
{
 flipscreen = ~data & 1;
 tilemap_set_flip(0,flipscreen ? (0x2 | 0x1) : 0);
}

canonical_original_code:
void gng_flipscreen_w(unsigned int offset, unsigned int data)
{
 flipscreen = ~data & 1;
 tilemap_set_flip(0,flipscreen ? (0x2 | 0x1) : 0);
}


decompiled_code:
__int64 __fastcall gng_vh_screenrefresh(__int64 a1)
{
tilemap_update(0LL);
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
tilemap_draw(a1, bg_tilemap, 32LL);
draw_sprites(a1);
tilemap_draw(a1, bg_tilemap, 64LL);
return tilemap_draw(a1, fg_tilemap, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
tilemap_update(0LL);
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
tilemap_draw(a1, bg_tilemap, 32LL);
func6(a1);
tilemap_draw(a1, bg_tilemap, 64LL);
return tilemap_draw(a1, fg_tilemap, 0LL);
}

original_code:
void gng_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 tilemap_update(0);
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 tilemap_draw(bitmap,bg_tilemap,0x20);
 draw_sprites(bitmap);
 tilemap_draw(bitmap,bg_tilemap,0x40);
 tilemap_draw(bitmap,fg_tilemap,0);
}

canonical_original_code:
void gng_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 tilemap_update(0);
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 tilemap_draw(bitmap,bg_tilemap,0x20);
 draw_sprites(bitmap);
 tilemap_draw(bitmap,bg_tilemap,0x40);
 tilemap_draw(bitmap,fg_tilemap,0);
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *


decompiled_code:
void *__fastcall gng_bankswitch_w(__int64 a1, int a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[1] = bank_0[a2] + memory_region(129LL);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, int a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[1] = bank_0[a2] + memory_region(129LL);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

original_code:
void gng_bankswitch_w(offs_t offset,data_t data)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 static int bank[] = { 0x10000, 0x12000, 0x14000, 0x16000, 0x04000, 0x18000 };
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[bank[data]]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
void gng_bankswitch_w(unsigned int offset, unsigned int data)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 static int bank[] = { 0x10000, 0x12000, 0x14000, 0x16000, 0x04000, 0x18000 };
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[bank[data]]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

variable_types:
RAM
unsigned char *

variable_types:
bank
int []


decompiled_code:
__int64 __fastcall get_fg_tile_info(int a1)
{
__int64 v1; // rax
int v2; // edi
__int64 v3; // rsi
__int64 v4; // rcx
unsigned int v5; // edx
__int64 v6; // rax
__int64 v7; // rcx
__int64 result; // rax

v1 = a1;
v2 = *(unsigned __int8 *)(gng_fgcolorram + a1);
v3 = *(_QWORD *)(Machine + 512LL);
v4 = *(_DWORD *)(v3 + 12) * (v2 & 0xF);
v5 = (*(unsigned __int8 *)(gng_fgvideoram + v1) + ((4 * (_WORD)v2) & 0x300u)) % *(_DWORD *)(v3 + 8);
tile_info = *(_QWORD *)(v3 + 40) + (int)(v5 * *(_DWORD *)(v3 + 52));
v6 = *(_QWORD *)(v3 + 16) + 2 * v4;
v7 = *(_QWORD *)(v3 + 32);
*((_QWORD *)&tile_info + 1) = v6;
result = 0LL;
if ( v7 )
result = *(unsigned int *)(v7 + 4LL * v5);
*((_DWORD *)&tile_info + 4) = result;
*((_DWORD *)&tile_info + 5) = (v2 >> 4) & 3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rax
int v2; // edi
__int64 v3; // rsi
__int64 v4; // rcx
unsigned int v5; // edx
__int64 v6; // rax
__int64 v7; // rcx
__int64 result; // rax

v1 = a1;
v2 = *(unsigned __int8 *)(gng_fgcolorram + a1);
v3 = *(_QWORD *)(Machine + 512LL);
v4 = *(_DWORD *)(v3 + 12) * (v2 & 0xF);
v5 = (*(unsigned __int8 *)(gng_fgvideoram + v1) + ((4 * (_WORD)v2) & 0x300u)) % *(_DWORD *)(v3 + 8);
tile_info = *(_QWORD *)(v3 + 40) + (int)(v5 * *(_DWORD *)(v3 + 52));
v6 = *(_QWORD *)(v3 + 16) + 2 * v4;
v7 = *(_QWORD *)(v3 + 32);
*((_QWORD *)&tile_info + 1) = v6;
result = 0LL;
if ( v7 )
result = *(unsigned int *)(v7 + 4LL * v5);
*((_DWORD *)&tile_info + 4) = result;
*((_DWORD *)&tile_info + 5) = (v2 >> 4) & 3;
return result;
}

original_code:
static void get_fg_tile_info(int tile_index)
{
 unsigned char attr = gng_fgcolorram[tile_index];
 { const struct GfxElement *gfx = Machine->gfx[(0)]; int _code = (gng_fgvideoram[tile_index] + ((attr & 0xc0) << 2)) % gfx->total_elements; tile_info.pen_data = gfx->gfxdata + _code*gfx->char_modulo; tile_info.pal_data = &gfx->colortable[gfx->color_granularity * (attr & 0x0f)]; tile_info.pen_usage = gfx->pen_usage?gfx->pen_usage[_code]:0; }
 tile_info.flags = ((attr & 0x30) >> 4);
}

canonical_original_code:
static void get_fg_tile_info(int tile_index)
{
 unsigned char attr = gng_fgcolorram[tile_index];
 { struct GfxElement *gfx = Machine->gfx[(0)]; int _code = (gng_fgvideoram[tile_index] + ((attr & 0xc0) << 2)) % gfx->total_elements; tile_info.pen_data = gfx->gfxdata + _code*gfx->char_modulo; tile_info.pal_data = &gfx->colortable[gfx->color_granularity * (attr & 0x0f)]; tile_info.pen_usage = gfx->pen_usage?gfx->pen_usage[_code]:0; }
 tile_info.flags = ((attr & 0x30) >> 4);
}

variable_types:
attr
unsigned char

variable_types: struct GfxElement { int width; unsigned int total_elements; int color_granularity; unsigned short *colortable; int total_colors; unsigned int *pen_usage; unsigned char *gfxdata; int line_modulo; int char_modulo; } *


decompiled_code:
__int64 __fastcall get_bg_tile_info(int a1)
{
__int64 v1; // rax
int v2; // esi
int v3; // ecx
__int64 v4; // rdi
__int64 v5; // rcx
unsigned int v6; // edx
__int64 v7; // rax
__int64 v8; // rcx
int v9; // eax
__int64 result; // rax

v1 = a1;
v2 = *(unsigned __int8 *)(gng_bgcolorram + a1);
v3 = *(_BYTE *)(gng_bgcolorram + a1) & 7;
v4 = *(_QWORD *)(Machine + 520LL);
v5 = *(_DWORD *)(v4 + 12) * v3;
v6 = (*(unsigned __int8 *)(gng_bgvideoram + v1) + ((4 * (_WORD)v2) & 0x300u)) % *(_DWORD *)(v4 + 8);
tile_info = *(_QWORD *)(v4 + 40) + (int)(v6 * *(_DWORD *)(v4 + 52));
v7 = *(_QWORD *)(v4 + 16) + 2 * v5;
v8 = *(_QWORD *)(v4 + 32);
*((_QWORD *)&tile_info + 1) = v7;
v9 = 0;
if ( v8 )
v9 = *(_DWORD *)(v8 + 4LL * v6);
*((_DWORD *)&tile_info + 4) = v9;
result = ((unsigned int)v2 >> 1) & 4;
*((_DWORD *)&tile_info + 5) = result | (v2 >> 4) & 3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 v1; // rax
int v2; // esi
int v3; // ecx
__int64 v4; // rdi
__int64 v5; // rcx
unsigned int v6; // edx
__int64 v7; // rax
__int64 v8; // rcx
int v9; // eax
__int64 result; // rax

v1 = a1;
v2 = *(unsigned __int8 *)(gng_bgcolorram + a1);
v3 = *(_BYTE *)(gng_bgcolorram + a1) & 7;
v4 = *(_QWORD *)(Machine + 520LL);
v5 = *(_DWORD *)(v4 + 12) * v3;
v6 = (*(unsigned __int8 *)(gng_bgvideoram + v1) + ((4 * (_WORD)v2) & 0x300u)) % *(_DWORD *)(v4 + 8);
tile_info = *(_QWORD *)(v4 + 40) + (int)(v6 * *(_DWORD *)(v4 + 52));
v7 = *(_QWORD *)(v4 + 16) + 2 * v5;
v8 = *(_QWORD *)(v4 + 32);
*((_QWORD *)&tile_info + 1) = v7;
v9 = 0;
if ( v8 )
v9 = *(_DWORD *)(v8 + 4LL * v6);
*((_DWORD *)&tile_info + 4) = v9;
result = ((unsigned int)v2 >> 1) & 4;
*((_DWORD *)&tile_info + 5) = result | (v2 >> 4) & 3;
return result;
}

original_code:
static void get_bg_tile_info(int tile_index)
{
 unsigned char attr = gng_bgcolorram[tile_index];
 { const struct GfxElement *gfx = Machine->gfx[(1)]; int _code = (gng_bgvideoram[tile_index] + ((attr & 0xc0) << 2)) % gfx->total_elements; tile_info.pen_data = gfx->gfxdata + _code*gfx->char_modulo; tile_info.pal_data = &gfx->colortable[gfx->color_granularity * (attr & 0x07)]; tile_info.pen_usage = gfx->pen_usage?gfx->pen_usage[_code]:0; }
 tile_info.flags = ((attr & 0x30) >> 4) | (((attr & 0x08) >> 3)<<2);
}

canonical_original_code:
static void get_bg_tile_info(int tile_index)
{
 unsigned char attr = gng_bgcolorram[tile_index];
 { struct GfxElement *gfx = Machine->gfx[(1)]; int _code = (gng_bgvideoram[tile_index] + ((attr & 0xc0) << 2)) % gfx->total_elements; tile_info.pen_data = gfx->gfxdata + _code*gfx->char_modulo; tile_info.pal_data = &gfx->colortable[gfx->color_granularity * (attr & 0x07)]; tile_info.pen_usage = gfx->pen_usage?gfx->pen_usage[_code]:0; }
 tile_info.flags = ((attr & 0x30) >> 4) | (((attr & 0x08) >> 3)<<2);
}

user_defined_types: struct GfxElement { int width; unsigned int total_elements; int color_granularity; unsigned short *colortable; int total_colors; unsigned int *pen_usage; unsigned char *gfxdata; int line_modulo; int char_modulo; }


decompiled_code:
void *__fastcall draw_sprites(__int64 a1)
{
void *result; // rax
__int64 v2; // r13
__int64 v3; // rbp
int v4; // ebx
__int64 v5; // r15
__int64 v6; // r11
__int16 v7; // dx
__int64 v8; // r10
unsigned __int8 v9; // cl
__int64 v10; // r9
__int64 v11; // rsi
__int64 v12; // r8

result = &spriteram_size;
v2 = *(_QWORD *)(Machine + 528LL);
v3 = Machine + 776LL;
v4 = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v5 = v4 + 1LL;
do
{
v6 = v5 - 1;
v7 = *(unsigned __int8 *)(spriteram + v5);
v8 = *(unsigned __int8 *)(spriteram + v5 + 1);
v9 = *(_BYTE *)(spriteram + v5);
v10 = v9 & 8;
v11 = *(unsigned __int8 *)(spriteram + v5 + 2) - ((unsigned __int8)(v7 & 1) << 8);
v12 = v9 & 4;
if ( flipscreen )
{
v11 = (unsigned int)(240 - v11);
v8 = (unsigned int)(240 - v8);
v12 = (_DWORD)v12 == 0;
v10 = (_DWORD)v10 == 0;
}
v5 -= 4LL;
result = (void *)drawgfx(
a1,
v2,
*(unsigned __int8 *)(spriteram + v6) + ((4 * v7) & 0x300u),
(v9 >> 4) & 3,
v12,
v10,
v11,
v8,
v3,
2LL,
15LL);
v4 -= 4;
}
while ( v4 >= 0 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1)
{
void *result; // rax
__int64 v2; // r13
__int64 v3; // rbp
int v4; // ebx
__int64 v5; // r15
__int64 v6; // r11
__int16 v7; // dx
__int64 v8; // r10
unsigned __int8 v9; // cl
__int64 v10; // r9
__int64 v11; // rsi
__int64 v12; // r8

result = &spriteram_size;
v2 = *(_QWORD *)(Machine + 528LL);
v3 = Machine + 776LL;
v4 = spriteram_size - 4;
if ( spriteram_size - 4 >= 0 )
{
v5 = v4 + 1LL;
do
{
v6 = v5 - 1;
v7 = *(unsigned __int8 *)(spriteram + v5);
v8 = *(unsigned __int8 *)(spriteram + v5 + 1);
v9 = *(_BYTE *)(spriteram + v5);
v10 = v9 & 8;
v11 = *(unsigned __int8 *)(spriteram + v5 + 2) - ((unsigned __int8)(v7 & 1) << 8);
v12 = v9 & 4;
if ( flipscreen )
{
v11 = (unsigned int)(240 - v11);
v8 = (unsigned int)(240 - v8);
v12 = (_DWORD)v12 == 0;
v10 = (_DWORD)v10 == 0;
}
v5 -= 4LL;
result = (void *)drawgfx(
a1,
v2,
*(unsigned __int8 *)(spriteram + v6) + ((4 * v7) & 0x300u),
(v9 >> 4) & 3,
v12,
v10,
v11,
v8,
v3,
2LL,
15LL);
v4 -= 4;
}
while ( v4 >= 0 );
}
return result;
}

original_code:
static void draw_sprites(struct osd_bitmap *bitmap)
{
 const struct GfxElement *gfx = Machine->gfx[2];
 const struct rectangle *clip = &Machine->visible_area;
 int offs;
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4){
  unsigned char attributes = spriteram[offs+1];
  int sx = spriteram[offs + 3] - 0x100 * (attributes & 0x01);
  int sy = spriteram[offs + 2];
  int flipx = attributes & 0x04;
  int flipy = attributes & 0x08;
  if (flipscreen){
   sx = 240 - sx;
   sy = 240 - sy;
   flipx = !flipx;
   flipy = !flipy;
  }
  drawgfx(bitmap,gfx,
    spriteram[offs] + ((attributes<<2) & 0x300),
    (attributes >> 4) & 3,
    flipx,flipy,
    sx,sy,
    clip,TRANSPARENCY_PEN,15);
 }
}

canonical_original_code:
static void draw_sprites(struct osd_bitmap *bitmap)
{
 struct GfxElement *gfx = Machine->gfx[2];
 struct rectangle *clip = &Machine->visible_area;
 int offs;
 for (offs = spriteram_size - 4;offs >= 0;offs -= 4){
  unsigned char attributes = spriteram[offs+1];
  int sx = spriteram[offs + 3] - 0x100 * (attributes & 0x01);
  int sy = spriteram[offs + 2];
  int flipx = attributes & 0x04;
  int flipy = attributes & 0x08;
  if (flipscreen){
   sx = 240 - sx;
   sy = 240 - sy;
   flipx = !flipx;
   flipy = !flipy;
  }
  drawgfx(bitmap,gfx,
    spriteram[offs] + ((attributes<<2) & 0x300),
    (attributes >> 4) & 3,
    flipx,flipy,
    sx,sy,
    clip,TRANSPARENCY_PEN,15);
 }
}

variable_types: struct rectangle { int min_x; int min_y; } *

variable_types: offs int

variable_types:
attributes
unsigned char

variable_types: sx int

variable_types: sy int

variable_types: flipx int

variable_types: flipy int

user_defined_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; }


original_code:
static UINT32 background_scan(UINT32 col,UINT32 row,UINT32 num_cols,UINT32 num_rows)
{
 return (col & 0x0f) + ((row & 0x0f) << 4) + ((col & 0x10) << 4) + ((row & 0x10) << 5);
}

canonical_original_code:
static unsigned int background_scan(unsigned int col, unsigned int row, unsigned int num_cols, unsigned int num_rows)
{
 return (col & 0x0f) + ((row & 0x0f) << 4) + ((col & 0x10) << 4) + ((row & 0x10) << 5);
}

variable_types:
col
unsigned int

variable_types:
row
unsigned int


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

sprite_irq = 2;
sound_irq = 1;
dd2_video = 0;
result = 0x100000001LL;
ym_irq = 1;
dd_sub_cpu_busy = 16;
adpcm_idle = 0x100000001LL;
return result;
}

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

sprite_irq = 2;
sound_irq = 1;
dd2_video = 0;
result = 0x100000001LL;
ym_irq = 1;
dd_sub_cpu_busy = 16;
adpcm_idle = 0x100000001LL;
return result;
}

original_code:
static void ddragon_init_machine( void )
{
 sprite_irq = 2;
 sound_irq = 1;
 ym_irq = 1;
 dd2_video = 0;
 dd_sub_cpu_busy = 0x10;
 adpcm_idle[0] = adpcm_idle[1] = 1;
}

canonical_original_code:
static void ddragon_init_machine()
{
 sprite_irq = 2;
 sound_irq = 1;
 ym_irq = 1;
 dd2_video = 0;
 dd_sub_cpu_busy = 0x10;
 adpcm_idle[0] = adpcm_idle[1] = 1;
}


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

sprite_irq = 4;
sound_irq = 1;
dd2_video = 0;
result = 0x100000001LL;
ym_irq = 1;
dd_sub_cpu_busy = 16;
adpcm_idle = 0x100000001LL;
return result;
}

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

sprite_irq = 4;
sound_irq = 1;
dd2_video = 0;
result = 0x100000001LL;
ym_irq = 1;
dd_sub_cpu_busy = 16;
adpcm_idle = 0x100000001LL;
return result;
}

original_code:
static void ddragonb_init_machine( void )
{
 sprite_irq = 4;
 sound_irq = 1;
 ym_irq = 1;
 dd2_video = 0;
 dd_sub_cpu_busy = 0x10;
 adpcm_idle[0] = adpcm_idle[1] = 1;
}

canonical_original_code:
static void ddragonb_init_machine()
{
 sprite_irq = 4;
 sound_irq = 1;
 ym_irq = 1;
 dd2_video = 0;
 dd_sub_cpu_busy = 0x10;
 adpcm_idle[0] = adpcm_idle[1] = 1;
}


decompiled_code:
_DWORD *ddragon2_init_machine()
{
_DWORD *result; // rax

result = dd2_video;
sprite_irq = -2;
sound_irq = -2;
ym_irq = 0;
dd2_video[0] = 1;
dd_sub_cpu_busy = 16;
return result;
}

canonical_decompiled_code:
_DWORD *func5()
{
_DWORD *result; // rax

result = dd2_video;
sprite_irq = -2;
sound_irq = -2;
ym_irq = 0;
dd2_video[0] = 1;
dd_sub_cpu_busy = 16;
return result;
}

original_code:
static void ddragon2_init_machine( void )
{
 sprite_irq = -2;
 sound_irq = -2;
 ym_irq = 0;
 dd2_video = 1;
 dd_sub_cpu_busy = 0x10;
}

canonical_original_code:
static void ddragon2_init_machine()
{
 sprite_irq = -2;
 sound_irq = -2;
 ym_irq = 0;
 dd2_video = 1;
 dd_sub_cpu_busy = 0x10;
}


decompiled_code:
__int64 __fastcall ddragon_spriteram_r(unsigned int a1)
{
return *(unsigned __int8 *)(ddragon_spriteram + a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
return *(unsigned __int8 *)(ddragon_spriteram + a1);
}

original_code:
static data_t ddragon_spriteram_r(offs_t offset)
{
 return ddragon_spriteram[offset];
}

canonical_original_code:
static unsigned int ddragon_spriteram_r(unsigned int offset)
{
 return ddragon_spriteram[offset];
}

variable_types:
offset
unsigned int


decompiled_code:
__int64 dd_adpcm_status_r()
{
return (unsigned int)(adpcm_idle + 2 * HIDWORD(adpcm_idle));
}

canonical_decompiled_code:
__int64 func7()
{
return (unsigned int)(adpcm_idle + 2 * HIDWORD(adpcm_idle));
}

original_code:
static data_t dd_adpcm_status_r(offs_t offset)
{
 return adpcm_idle[0] + (adpcm_idle[1] << 1);
}

canonical_original_code:
static unsigned int dd_adpcm_status_r(unsigned int offset)
{
 return adpcm_idle[0] + (adpcm_idle[1] << 1);
}


decompiled_code:
__int64 ddragon_vh_start()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-18h]

v0 = tilemap_create(get_bg_tile_info, background_scan, 0LL, 16LL, 16LL, 32LL, 32LL);
LODWORD(v3) = 32;
bg_tilemap = v0;
v1 = tilemap_create(get_fg_tile_info, &tilemap_scan_rows, 1LL, 8LL, 8LL, 32LL, v3);
fg_tilemap = v1;
if ( !bg_tilemap || !v1 )
return 1LL;
*(_DWORD *)(v1 + 132) = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v3; // [rsp+0h] [rbp-18h]

v0 = tilemap_create(get_bg_tile_info, background_scan, 0LL, 16LL, 16LL, 32LL, 32LL);
LODWORD(v3) = 32;
bg_tilemap = v0;
v1 = tilemap_create(get_fg_tile_info, &tilemap_scan_rows, 1LL, 8LL, 8LL, 32LL, v3);
fg_tilemap = v1;
if ( !bg_tilemap || !v1 )
return 1LL;
*(_DWORD *)(v1 + 132) = 0;
return 0LL;
}

original_code:
int ddragon_vh_start(void)
{
 bg_tilemap = tilemap_create(get_bg_tile_info,background_scan, 0x00, 16,16,32,32);
 fg_tilemap = tilemap_create(get_fg_tile_info,tilemap_scan_rows,0x01, 8, 8,32,32);
 if (!bg_tilemap || !fg_tilemap)
  return 1;
 fg_tilemap->transparent_pen = 0;
 return 0;
}

canonical_original_code:
int ddragon_vh_start()
{
 bg_tilemap = tilemap_create(get_bg_tile_info,background_scan, 0x00, 16,16,32,32);
 fg_tilemap = tilemap_create(get_fg_tile_info,tilemap_scan_rows,0x01, 8, 8,32,32);
 if (!bg_tilemap || !fg_tilemap)
  return 1;
 fg_tilemap->transparent_pen = 0;
 return 0;
}


decompiled_code:
__int64 __fastcall ddragon_vh_screenrefresh(__int64 a1)
{
unsigned int v1; // er12

v1 = ddragon_scrolly_hi + (unsigned __int8)*ddragon_scrolly_lo;
tilemap_set_scrollx(bg_tilemap, 0LL, ddragon_scrollx_hi + (unsigned int)(unsigned __int8)*ddragon_scrollx_lo);
tilemap_set_scrolly(bg_tilemap, 0LL, v1);
tilemap_update(0LL);
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
tilemap_draw(a1, bg_tilemap, 0LL);
draw_sprites(a1);
return tilemap_draw(a1, fg_tilemap, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unsigned int v1; // er12

v1 = ddragon_scrolly_hi + (unsigned __int8)*ddragon_scrolly_lo;
tilemap_set_scrollx(bg_tilemap, 0LL, ddragon_scrollx_hi + (unsigned int)(unsigned __int8)*ddragon_scrollx_lo);
tilemap_set_scrolly(bg_tilemap, 0LL, v1);
tilemap_update(0LL);
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
tilemap_draw(a1, bg_tilemap, 0LL);
func9(a1);
return tilemap_draw(a1, fg_tilemap, 0LL);
}

original_code:
void ddragon_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int scrollx = ddragon_scrollx_hi + *ddragon_scrollx_lo;
 int scrolly = ddragon_scrolly_hi + *ddragon_scrolly_lo;
 tilemap_set_scrollx(bg_tilemap,0,scrollx);
 tilemap_set_scrolly(bg_tilemap,0,scrolly);
 tilemap_update(0);
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 tilemap_draw(bitmap,bg_tilemap,0);
 draw_sprites(bitmap);
 tilemap_draw(bitmap,fg_tilemap,0);
}

canonical_original_code:
void ddragon_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int scrollx = ddragon_scrollx_hi + *ddragon_scrollx_lo;
 int scrolly = ddragon_scrolly_hi + *ddragon_scrolly_lo;
 tilemap_set_scrollx(bg_tilemap,0,scrollx);
 tilemap_set_scrolly(bg_tilemap,0,scrolly);
 tilemap_update(0);
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 tilemap_draw(bitmap,bg_tilemap,0);
 draw_sprites(bitmap);
 tilemap_draw(bitmap,fg_tilemap,0);
}

variable_types: scrollx int

variable_types: scrolly int


decompiled_code:
__int64 ddragon_interrupt()
{
cpu_set_irq_line(0LL, 1LL, 2LL);
cpu_set_nmi_line(0LL, 3LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func14()
{
cpu_set_irq_line(0LL, 1LL, 2LL);
cpu_set_nmi_line(0LL, 3LL);
return 0LL;
}

original_code:
static int ddragon_interrupt(void)
{
    cpu_set_irq_line(0, 1, 2);
    cpu_set_nmi_line(0, 3);
    return 0;
}

canonical_original_code:
static int ddragon_interrupt()
{
    cpu_set_irq_line(0, 1, 2);
    cpu_set_nmi_line(0, 3);
    return 0;
}


decompiled_code:
__int64 ddragon_forcedIRQ_w()
{
return cpu_cause_interrupt(0LL, 1LL);
}

canonical_decompiled_code:
__int64 func15()
{
return cpu_cause_interrupt(0LL, 1LL);
}

original_code:
static void ddragon_forcedIRQ_w(offs_t offset,data_t data)
{
 cpu_cause_interrupt( 0, 1 );
}

canonical_original_code:
static void ddragon_forcedIRQ_w(unsigned int offset, unsigned int data)
{
 cpu_cause_interrupt( 0, 1 );
}

variable_types:
data
unsigned int


decompiled_code:
void *__fastcall ddragon_bankswitch_w(__int64 a1, __int16 a2)
{
__int64 v2; // rbp
void *result; // rax
unsigned int v4; // eax

v2 = memory_region(129LL);
ddragon_scrolly_hi = (a2 << 7) & 0x100;
ddragon_scrollx_hi = (a2 << 8) & 0x100;
flip_screen_w(0LL, ~(_BYTE)a2 & 4);
if ( (a2 & 0x10) != 0 )
{
dd_sub_cpu_busy = 0;
}
else if ( !dd_sub_cpu_busy )
{
cpu_cause_interrupt(1LL, (unsigned int)sprite_irq);
}
cpu_bankbase[1] = v2 + (((unsigned int)((unsigned __int8)a2 >> 5) + 4) << 14);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v4 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v4);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func17(__int64 a1, __int16 a2)
{
__int64 v2; // rbp
void *result; // rax
unsigned int v4; // eax

v2 = memory_region(129LL);
ddragon_scrolly_hi = (a2 << 7) & 0x100;
ddragon_scrollx_hi = (a2 << 8) & 0x100;
flip_screen_w(0LL, ~(_BYTE)a2 & 4);
if ( (a2 & 0x10) != 0 )
{
dd_sub_cpu_busy = 0;
}
else if ( !dd_sub_cpu_busy )
{
cpu_cause_interrupt(1LL, (unsigned int)sprite_irq);
}
cpu_bankbase[1] = v2 + (((unsigned int)((unsigned __int8)a2 >> 5) + 4) << 14);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v4 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v4);
}
return result;
}

original_code:
static void ddragon_bankswitch_w(offs_t offset,data_t data)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 ddragon_scrolly_hi = ( ( data & 0x02 ) << 7 );
 ddragon_scrollx_hi = ( ( data & 0x01 ) << 8 );
 flip_screen_w(0,~data & 0x04);
 if (data & 0x10)
  dd_sub_cpu_busy = 0x00;
 else if (dd_sub_cpu_busy == 0x00)
  cpu_cause_interrupt( 1, sprite_irq );
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[ 0x10000 + ( 0x4000 * ( ( data & 0xe0) >> 5 ) ) ]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
static void ddragon_bankswitch_w(unsigned int offset, unsigned int data)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 ddragon_scrolly_hi = ( ( data & 0x02 ) << 7 );
 ddragon_scrollx_hi = ( ( data & 0x01 ) << 8 );
 flip_screen_w(0,~data & 0x04);
 if (data & 0x10)
  dd_sub_cpu_busy = 0x00;
 else if (dd_sub_cpu_busy == 0x00)
  cpu_cause_interrupt( 1, sprite_irq );
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[ 0x10000 + ( 0x4000 * ( ( data & 0xe0) >> 5 ) ) ]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

variable_types:
RAM
unsigned char *