decompiled_code:
_BOOL8 __fastcall str_starts_with(_BYTE *a1, _BYTE *a2, int a3)
{
_BYTE *v4; // rbx
char v5; // al
char v6; // r12
const __int32_t *v7; // rax

v4 = a2;
if ( a3 )
{
v6 = *a2;
if ( *a2 )
{
v7 = *_ctype_tolower_loc();
do
{
if ( v7[v6] != v7[(char)*a1] )
break;
++a1;
v6 = *++v4;
}
while ( *v4 );
}
}
else
{
v5 = *a2;
if ( *a2 )
{
do
{
if ( *a1 != v5 )
break;
++a1;
v5 = *++v4;
}
while ( *v4 );
}
}
return *v4 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_BYTE *a1, _BYTE *a2, int a3)
{
_BYTE *v4; // rbx
char v5; // al
char v6; // r12
const __int32_t *v7; // rax

v4 = a2;
if ( a3 )
{
v6 = *a2;
if ( *a2 )
{
v7 = *_ctype_tolower_loc();
do
{
if ( v7[v6] != v7[(char)*a1] )
break;
++a1;
v6 = *++v4;
}
while ( *v4 );
}
}
else
{
v5 = *a2;
if ( *a2 )
{
do
{
if ( *a1 != v5 )
break;
++a1;
v5 = *++v4;
}
while ( *v4 );
}
}
return *v4 == 0;
}

original_code:
int str_starts_with(const char *str, const char *prefix, int icase) {
    if (icase)
        for (; *prefix && tolower(*str) == tolower(*prefix); str++, prefix++);
    else
        for (; *prefix && *str == *prefix; str++, prefix++);
    return !*prefix;
}

canonical_original_code:
int str_starts_with(char *str, char *prefix, int icase) {
    if (icase)
        for (; *prefix && tolower(*str) == tolower(*prefix); str++, prefix++);
    else
        for (; *prefix && *str == *prefix; str++, prefix++);
    return !*prefix;
}

variable_types:
str
char *

variable_types:
prefix
char *

variable_types: icase int


max

repo: miguelvps/c

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

original_code:
int max(int x, int y) {
    return x > y ? x : y;
}

variable_types: x int

variable_types: y int

return_type: int


decompiled_code:
int print_usage()
{
return fprintf(
stderr,
"Usage: %s [-chisv] [-l library] [-m matcher] [-t threshold] [directory]\n",
(const char *)program_name);
}

canonical_decompiled_code:
int func0()
{
return fprintf(
stderr,
"Usage: %s [-chisv] [-l library] [-m matcher] [-t threshold] [directory]\n",
(const char *)program_name);
}

original_code:
void print_usage() {
    fprintf(stderr,
            "Usage: %s [-chisv] [-l library] [-m matcher] [-t threshold] [directory]\n",
            program_name);
}

return_type: void


decompiled_code:
int print_version()
{
return fprintf(stderr, "c %s - a 'cd' wrapper\n", "e7a45dd");
}

canonical_decompiled_code:
int func1()
{
return fprintf(stderr, "c %s - a 'cd' wrapper\n", "e7a45dd");
}

original_code:
void print_version() {
    fprintf(stderr, "c %s - a 'cd' wrapper\n", "e7a45dd");
}


decompiled_code:
__int64 __fastcall parse_options(int a1, __int64 *a2)
{
int v3; // eax
const char *v4; // rdi
void *v5; // r12
void *v6; // rax
__int64 result; // rax
char *v8; // rax
char *v9; // rax

program_name = *a2;
options = 0LL;
*(&options + 1) = 0LL;
*(&options + 2) = 0LL;
*(&options + 6) = 0LL;
*(&options + 3) = (__int64)&jaro_winkler_distance;
*(&options + 4) = LC3;
*(&options + 5) = (__int64)&unk_2E8;
while ( 1 )
{
v3 = _posix_getopt((unsigned int)a1, a2, "chisvl:m:t:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'c':
LODWORD(options) = 1;
break;
case 'h':
print_usage();
exit(0);
case 'i':
HIDWORD(options) = 1;
break;
case 'l':
*(&options + 1) = (__int64)optarg;
break;
case 'm':
*(&options + 2) = (__int64)optarg;
break;
case 's':
*((_DWORD *)&options + 12) = 1;
break;
case 't':
*((double *)&options + 4) = atof(optarg);
break;
case 'v':
print_version();
exit(0);
default:
print_usage();
exit(1);
}
}
v4 = (const char *)*(&options + 1);
if ( v4 && *(&options + 2) )
{
v5 = dlopen(v4, 1);
if ( !v5 )
{
v8 = dlerror();
error(1, 0, "dlopen: %s\n", v8);
}
v6 = dlsym(v5, (const char *)*(&options + 2));
*(&options + 3) = (__int64)v6;
if ( !v6 )
{
v9 = dlerror();
error(1, 0, "dlsym: %s\n", v9);
}
}
if ( (_DWORD)options )
{
LODWORD(result) = optind + 1;
if ( optind + 1 < a1 )
{
++optind;
LABEL_22:
result = a2[(int)result];
*(&options + 5) = result;
return result;
}
}
result = (unsigned int)optind;
if ( optind < a1 )
goto LABEL_22;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 *a2)
{
int v3; // eax
const char *v4; // rdi
void *v5; // r12
void *v6; // rax
__int64 result; // rax
char *v8; // rax
char *v9; // rax

program_name = *a2;
options = 0LL;
*(&options + 1) = 0LL;
*(&options + 2) = 0LL;
*(&options + 6) = 0LL;
*(&options + 3) = (__int64)&jaro_winkler_distance;
*(&options + 4) = LC3;
*(&options + 5) = (__int64)&unk_2E8;
while ( 1 )
{
v3 = _posix_getopt((unsigned int)a1, a2, "chisvl:m:t:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'c':
LODWORD(options) = 1;
break;
case 'h':
func0();
exit(0);
case 'i':
HIDWORD(options) = 1;
break;
case 'l':
*(&options + 1) = (__int64)optarg;
break;
case 'm':
*(&options + 2) = (__int64)optarg;
break;
case 's':
*((_DWORD *)&options + 12) = 1;
break;
case 't':
*((double *)&options + 4) = atof(optarg);
break;
case 'v':
func1();
exit(0);
default:
func0();
exit(1);
}
}
v4 = (const char *)*(&options + 1);
if ( v4 && *(&options + 2) )
{
v5 = dlopen(v4, 1);
if ( !v5 )
{
v8 = dlerror();
error(1, 0, "dlopen: %s\n", v8);
}
v6 = dlsym(v5, (const char *)*(&options + 2));
*(&options + 3) = (__int64)v6;
if ( !v6 )
{
v9 = dlerror();
error(1, 0, "dlsym: %s\n", v9);
}
}
if ( (_DWORD)options )
{
LODWORD(result) = optind + 1;
if ( optind + 1 < a1 )
{
++optind;
LABEL_22:
result = a2[(int)result];
*(&options + 5) = result;
return result;
}
}
result = (unsigned int)optind;
if ( optind < a1 )
goto LABEL_22;
return result;
}

original_code:
void parse_options(int argc, char *const argv[]) {
    int i;
    program_name = argv[0];
    memset(&options, 0, sizeof(options));
    options.matcher = jaro_winkler_distance;
    options.threshold = 0.75;
    options.directory = "";
    while ((i=getopt(argc, argv, "chisvl:m:t:")) != -1) {
        switch (i) {
            case 'c':
                options.complete = 1;
                break;
            case 'h':
                print_usage();
                exit(0);
            case 'i':
                options.icase = 1;
                break;
            case 'l':
                options.library = optarg;
                break;
            case 'm':
                options.matcher_name = optarg;
                break;
            case 's':
                options.simulate = 1;
                break;
            case 't':
                options.threshold = atof(optarg);
                break;
            case 'v':
                print_version();
                exit(0);
            default:
                print_usage();
                exit(1);
        }
    }
    if (options.library && options.matcher_name) {
        void *handle;
        handle = dlopen(options.library, 0x00001);
        if (!handle)
            error(1, 0, "dlopen: %s\n", dlerror());
        *(void **)(&options.matcher) = dlsym(handle, options.matcher_name);
        if (options.matcher == ((void *)0))
            error(1, 0, "dlsym: %s\n", dlerror());
    }
    if (options.complete && optind + 1 < argc)
        optind++;
    if (optind < argc)
        options.directory = argv[optind];
}

canonical_original_code:
void parse_options(int argc, char *argv[]) {
    int i;
    program_name = argv[0];
    memset(&options, 0, sizeof(options));
    options.matcher = jaro_winkler_distance;
    options.threshold = 0.75;
    options.directory = "";
    while ((i=getopt(argc, argv, "chisvl:m:t:")) != -1) {
        switch (i) {
            case 'c':
                options.complete = 1;
                break;
            case 'h':
                print_usage();
                exit(0);
            case 'i':
                options.icase = 1;
                break;
            case 'l':
                options.library = optarg;
                break;
            case 'm':
                options.matcher_name = optarg;
                break;
            case 's':
                options.simulate = 1;
                break;
            case 't':
                options.threshold = atof(optarg);
                break;
            case 'v':
                print_version();
                exit(0);
            default:
                print_usage();
                exit(1);
        }
    }
    if (options.library && options.matcher_name) {
        void *handle;
        handle = dlopen(options.library, 0x00001);
        if (!handle)
            error(1, 0, "dlopen: %s\n", dlerror());
        *(void **)(&options.matcher) = dlsym(handle, options.matcher_name);
        if (options.matcher == ((void *)0))
            error(1, 0, "dlsym: %s\n", dlerror());
    }
    if (options.complete && optind + 1 < argc)
        optind++;
    if (optind < argc)
        options.directory = argv[optind];
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types:
handle
void *


min

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

result = a1;
if ( (int)a2 <= (int)a1 )
result = a2;
return result;
}

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

result = a1;
if ( (int)a2 <= (int)a1 )
result = a2;
return result;
}

original_code:
static int min(int x, int y) {
    return x < y ? x : y;
}


original_code:
double jaro_winkler_distance(const char *s, const char *a) {
    int i, j, l;
    int m = 0, t = 0;
    int sl = strlen(s);
    int al = strlen(a);
    int sflags[sl], aflags[al];
    int range = max(0, max(sl, al) / 2 - 1);
    double dw;
    if (!sl || !al)
        return 0.0;
    for (i = 0; i < al; i++)
        aflags[i] = 0;
    for (i = 0; i < sl; i++)
        sflags[i] = 0;
    for (i = 0; i < al; i++) {
        for (j = max(i - range, 0), l = min(i + range + 1, sl); j < l; j++) {
            if (a[i] == s[j] && !sflags[j]) {
                sflags[j] = 1;
                aflags[i] = 1;
                m++;
                break;
            }
        }
    }
    if (!m)
        return 0.0;
    l = 0;
    for (i = 0; i < al; i++) {
        if (aflags[i] == 1) {
            for (j = l; j < sl; j++) {
                if (sflags[j] == 1) {
                    l = j + 1;
                    break;
                }
            }
            if (a[i] != s[j])
                t++;
        }
    }
    t /= 2;
    dw = (((double)m / sl) + ((double)m / al) + ((double)(m - t) / m)) / 3.0;
    l = 0;
    for (i = 0; i < min(min(sl, al), 4); i++)
        if (s[i] == a[i])
            l++;
    dw = dw + (l * 0.1 * (1 - dw));
    return dw;
}

canonical_original_code:
double jaro_winkler_distance(char *s, char *a) {
    int i; int j; int l;
    int m = 0; int t = 0;
    int sl = strlen(s);
    int al = strlen(a);
    int sflags[-1]; int aflags[-1];
    int range = max(0, max(sl, al) / 2 - 1);
    double dw;
    if (!sl || !al)
        return 0.0;
    for (i = 0; i < al; i++)
        aflags[i] = 0;
    for (i = 0; i < sl; i++)
        sflags[i] = 0;
    for (i = 0; i < al; i++) {
        for (j = max(i - range, 0), l = min(i + range + 1, sl); j < l; j++) {
            if (a[i] == s[j] && !sflags[j]) {
                sflags[j] = 1;
                aflags[i] = 1;
                m++;
                break;
            }
        }
    }
    if (!m)
        return 0.0;
    l = 0;
    for (i = 0; i < al; i++) {
        if (aflags[i] == 1) {
            for (j = l; j < sl; j++) {
                if (sflags[j] == 1) {
                    l = j + 1;
                    break;
                }
            }
            if (a[i] != s[j])
                t++;
        }
    }
    t /= 2;
    dw = (((double)m / sl) + ((double)m / al) + ((double)(m - t) / m)) / 3.0;
    l = 0;
    for (i = 0; i < min(min(sl, al), 4); i++)
        if (s[i] == a[i])
            l++;
    dw = dw + (l * 0.1 * (1 - dw));
    return dw;
}

variable_types:
s
char *

variable_types:
a
char *

variable_types: j int

variable_types: l int

variable_types: m int

variable_types: t int

variable_types: sl int

variable_types: al int

variable_types:
sflags
int [-1]

variable_types:
aflags
int [-1]

variable_types: range int

variable_types: dw double

return_type: double


max

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

original_code:
static int max(int x, int y) {
    return x > y ? x : y;
}

variable_types: x int

variable_types: y int


repo: mihaimaruseac/dphcar

decompiled_code:
__int64 __fastcall do_count(_QWORD *a1, _QWORD *a2, _QWORD *a3, _QWORD *a4, unsigned int a5)
{
__int64 result; // rax
unsigned __int64 i; // rbx

if ( a5 )
{
*a2 += a1[7];
*a3 += a1[8];
result = a1[9];
}
else
{
*a2 += a1[4];
*a3 += a1[5];
result = a1[6];
}
*a4 += result;
for ( i = 0LL; a1[2] > i; ++i )
result = do_count(*(_QWORD *)(*a1 + 16 * i + 8), a2, a3, a4, a5);
return result;
}

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

if ( a5 )
{
*a2 += a1[7];
*a3 += a1[8];
result = a1[9];
}
else
{
*a2 += a1[4];
*a3 += a1[5];
result = a1[6];
}
*a4 += result;
for ( i = 0LL; a1[2] > i; ++i )
result = func1(*(_QWORD *)(*a1 + 16 * i + 8), a2, a3, a4, a5);
return result;
}

original_code:
static void do_count(const struct itstree_node *itst,
  size_t *n30, size_t *n50, size_t *n70, int private)
{
 size_t i;
 if (private) {
  *n30 += itst->pc30;
  *n50 += itst->pc50;
  *n70 += itst->pc70;
 } else {
  *n30 += itst->rc30;
  *n50 += itst->rc50;
  *n70 += itst->rc70;
 }
 for (i = 0; i < itst->sz; i++)
  do_count(itst->children[i].iptr, n30, n50, n70, private);
}

canonical_original_code:
static void do_count(struct itstree_node *itst, unsigned long *n30, unsigned long *n50, unsigned long *n70, int private)
{
 unsigned long i;
 if (private) {
  *n30 += itst->pc30;
  *n50 += itst->pc50;
  *n70 += itst->pc70;
 } else {
  *n30 += itst->rc30;
  *n50 += itst->rc50;
  *n70 += itst->rc70;
 }
 for (i = 0; i < itst->sz; i++)
  do_count(itst->children[i].iptr, n30, n50, n70, private);
}

variable_types: struct itstree_node { struct children_info { int item; struct itstree_node *iptr; } *children; unsigned long sp; unsigned long sz; int dpseen; unsigned long rc30; unsigned long pc30; } *

variable_types: private int

variable_types:
i
unsigned long

user_defined_types: struct itstree_node { struct children_info *children; unsigned long sp; unsigned long sz; int dpseen; unsigned long rc30; unsigned long pc30; }


decompiled_code:
_QWORD *init_empty_itstree()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x50uLL);
v0[1] = 10LL;
*v0 = calloc(0xAuLL, 0x10uLL);
return v0;
}

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x50uLL);
v0[1] = 10LL;
*v0 = calloc(0xAuLL, 0x10uLL);
return v0;
}

original_code:
struct itstree_node *init_empty_itstree()
{
 struct itstree_node *ret = calloc(1, sizeof(*ret));
 ret->sp = 10;
 ret->children = calloc(ret->sp, sizeof(ret->children[0]));
 return ret;
}

return_type: struct itstree_node { struct children_info { int item; struct itstree_node *iptr; } *children; unsigned long sp; unsigned long sz; int dpseen; unsigned long rc30; unsigned long pc30; } *


original_code:
static void do_record_new_rule(struct itstree_node *itst, const int *its,
  size_t sz, int private, size_t rc30, size_t rc50, size_t rc70)
{
 struct children_info k, *p;
 if (!sz) {
  if (private) {
   itst->dpseen = 1;
   itst->pc30 = rc30;
   itst->pc50 = rc50;
   itst->pc70 = rc70;
  } else {
   itst->rc30 = rc30;
   itst->rc50 = rc50;
   itst->rc70 = rc70;
  }
  return;
 }
 k.item = its[0];
 p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 if (!p) {
  if (itst->sz == itst->sp) {
   itst->sp *= 2;
   itst->children = realloc(itst->children,
     itst->sp * sizeof(itst->children[0]));
  }
  itst->children[itst->sz].item = its[0];
  itst->children[itst->sz].iptr = init_empty_itstree();
  itst->sz++;
  qsort(itst->children, itst->sz, sizeof(k), cmp);
  p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 }
 do_record_new_rule(p->iptr, its+1, sz-1, private, rc30, rc50, rc70);
}

canonical_original_code:
static void do_record_new_rule(struct itstree_node *itst, int *its, unsigned long sz, int private, unsigned long rc30, unsigned long rc50, unsigned long rc70)
{
 struct children_info k; struct children_info *p;
 if (!sz) {
  if (private) {
   itst->dpseen = 1;
   itst->pc30 = rc30;
   itst->pc50 = rc50;
   itst->pc70 = rc70;
  } else {
   itst->rc30 = rc30;
   itst->rc50 = rc50;
   itst->rc70 = rc70;
  }
  return;
 }
 k.item = its[0];
 p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 if (!p) {
  if (itst->sz == itst->sp) {
   itst->sp *= 2;
   itst->children = realloc(itst->children,
     itst->sp * sizeof(itst->children[0]));
  }
  itst->children[itst->sz].item = its[0];
  itst->children[itst->sz].iptr = init_empty_itstree();
  itst->sz++;
  qsort(itst->children, itst->sz, sizeof(k), cmp);
  p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 }
 do_record_new_rule(p->iptr, its+1, sz-1, private, rc30, rc50, rc70);
}

variable_types:
its
int *

variable_types:
sz
unsigned long

variable_types: struct children_info { int item; struct itstree_node { struct children_info *children; unsigned long sp; unsigned long sz; int dpseen; unsigned long rc30; unsigned long pc30; } *iptr; } *

user_defined_types: struct children_info { int item; struct itstree_node *iptr; }


decompiled_code:
__int64 __fastcall record_its_private(__int64 a1, int *a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
return do_record_new_rule(a1, a2, a3, 1, a4, a5, a6);
}

original_code:
void record_its_private(struct itstree_node *itst, const int *its, size_t sz,
  size_t rc30, size_t rc50, size_t rc70)
{
 do_record_new_rule(itst, its, sz, 1, rc30, rc50, rc70);
}

canonical_original_code:
void record_its_private(struct itstree_node *itst, int *its, unsigned long sz, unsigned long rc30, unsigned long rc50, unsigned long rc70)
{
 do_record_new_rule(itst, its, sz, 1, rc30, rc50, rc70);
}


decompiled_code:
__int64 __fastcall record_its(__int64 a1, int *a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
return do_record_new_rule(a1, a2, a3, 0, a4, a5, a6);
}

original_code:
void record_its(struct itstree_node *itst, const int *its, size_t sz,
  size_t rc30, size_t rc50, size_t rc70)
{
 do_record_new_rule(itst, its, sz, 0, rc30, rc50, rc70);
}

canonical_original_code:
void record_its(struct itstree_node *itst, int *its, unsigned long sz, unsigned long rc30, unsigned long rc50, unsigned long rc70)
{
 do_record_new_rule(itst, its, sz, 0, rc30, rc50, rc70);
}


decompiled_code:
__int64 __fastcall search_its_private(__int64 a1, int *a2, __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // rcx
int v6[10]; // [rsp+0h] [rbp-28h] BYREF

if ( !a3 )
return *(unsigned int *)(a1 + 24);
v6[0] = *a2;
v5 = bsearch(v6, *(const void **)a1, *(_QWORD *)(a1 + 16), 0x10uLL, (__compar_fn_t)cmp);
result = 0LL;
if ( v5 )
result = search_its_private(v5[1], a2 + 1, a3 - 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int *a2, __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // rcx
int v6[10]; // [rsp+0h] [rbp-28h] BYREF

if ( !a3 )
return *(unsigned int *)(a1 + 24);
v6[0] = *a2;
v5 = bsearch(v6, *(const void **)a1, *(_QWORD *)(a1 + 16), 0x10uLL, (__compar_fn_t)cmp);
result = 0LL;
if ( v5 )
result = func8(v5[1], a2 + 1, a3 - 1);
return result;
}

original_code:
int search_its_private(const struct itstree_node *itst, const int *its,
  size_t sz)
{
 struct children_info k, *p;
 if (!sz)
  return itst->dpseen;
 k.item = its[0];
 p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 if (!p)
  return 0;
 return search_its_private(p->iptr, its+1, sz-1);
}

canonical_original_code:
int search_its_private(struct itstree_node *itst, int *its, unsigned long sz)
{
 struct children_info k; struct children_info *p;
 if (!sz)
  return itst->dpseen;
 k.item = its[0];
 p = bsearch(&k, itst->children, itst->sz, sizeof(k), cmp);
 if (!p)
  return 0;
 return search_its_private(p->iptr, its+1, sz-1);
}

variable_types: struct children_info { int item; struct itstree_node { struct children_info *children; unsigned long sp; unsigned long sz; int dpseen; unsigned long rc30; unsigned long pc30; } *iptr; }


decompiled_code:
void __fastcall free_itstree(__int64 a1)
{
unsigned __int64 v1; // rbx

if ( *(_QWORD *)(a1 + 16) )
{
v1 = 0LL;
do
free_itstree(*(_QWORD *)(*(_QWORD *)a1 + 16 * v1++ + 8));
while ( *(_QWORD *)(a1 + 16) > v1 );
}
free(*(void **)a1);
free((void *)a1);
}

original_code:
void free_itstree(struct itstree_node *itst)
{
 size_t i;
 for (i = 0; i < itst->sz; i++)
  free_itstree(itst->children[i].iptr);
 free(itst->children);
 free(itst);
}

canonical_original_code:
void free_itstree(struct itstree_node *itst)
{
 unsigned long i;
 for (i = 0; i < itst->sz; i++)
  free_itstree(itst->children[i].iptr);
 free(itst->children);
 free(itst);
}


decompiled_code:
__int64 __fastcall itstree_count_real(_QWORD *a1, _QWORD *a2, _QWORD *a3, _QWORD *a4)
{
return do_count(a1, a2, a3, a4, 0);
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1, _QWORD *a2, _QWORD *a3, _QWORD *a4)
{
return func1(a1, a2, a3, a4, 0);
}

original_code:
void itstree_count_real(const struct itstree_node *itst,
  size_t *n30, size_t *n50, size_t *n70)
{
 do_count(itst, n30, n50, n70, 0);
}

canonical_original_code:
void itstree_count_real(struct itstree_node *itst, unsigned long *n30, unsigned long *n50, unsigned long *n70)
{
 do_count(itst, n30, n50, n70, 0);
}


decompiled_code:
__int64 __fastcall itstree_count_priv(_QWORD *a1, _QWORD *a2, _QWORD *a3, _QWORD *a4)
{
return do_count(a1, a2, a3, a4, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1, _QWORD *a2, _QWORD *a3, _QWORD *a4)
{
return func1(a1, a2, a3, a4, 1u);
}

original_code:
void itstree_count_priv(const struct itstree_node *itst,
  size_t *p30, size_t *p50, size_t *p70)
{
 do_count(itst, p30, p50, p70, 1);
}

canonical_original_code:
void itstree_count_priv(struct itstree_node *itst, unsigned long *p30, unsigned long *p50, unsigned long *p70)
{
 do_count(itst, p30, p50, p70, 1);
}


cmp

decompiled_code:
__int64 __fastcall cmp(const void *a1, const void *a2)
{
return (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
}

original_code:
static int cmp(const void *a, const void *b)
{
 const struct children_info *pa = a, *pb = b;
 return pa->item - pb->item;
}

canonical_original_code:
static int cmp(void *a, void *b)
{
 struct children_info *pa = a; struct children_info *pb = b;
 return pa->item - pb->item;
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
__int64 __fastcall fptable_cmp(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a2 + 1) - *((_DWORD *)a1 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a2 + 1) - *((_DWORD *)a1 + 2);
}

original_code:
static int fptable_cmp(const void *a, const void *b)
{
 const struct table *ta = a, *tb = b;
 return tb->cnt - ta->cnt;
}

canonical_original_code:
static int fptable_cmp(void *a, void *b)
{
 struct table *ta = a; struct table *tb = b;
 return tb->cnt - ta->cnt;
}

variable_types: struct table { unsigned long val; unsigned long cnt; struct fptree_node { int val; int cnt; struct fptree_node *next; int num_children; struct fptree_node **children; int sz_children; struct fptree_node *parent; } *fst; struct fptree_node *lst; unsigned long rpi; } *

user_defined_types: struct table { unsigned long val; unsigned long cnt; struct fptree_node *fst; struct fptree_node *lst; unsigned long rpi; }


decompiled_code:
__int64 __fastcall fpt_get_height(__int64 a1)
{
int v1; // eax
int v2; // ebp
_QWORD *v3; // rbx
__int64 v4; // r12
int v5; // eax

v1 = *(_DWORD *)(a1 + 16);
v2 = 0;
if ( v1 > 0 )
{
v3 = *(_QWORD **)(a1 + 24);
v4 = (__int64)&v3[(unsigned int)(v1 - 1) + 1];
do
{
v5 = fpt_get_height(*v3);
if ( v2 < v5 )
v2 = v5;
++v3;
}
while ( v3 != (_QWORD *)v4 );
}
return (unsigned int)(v2 + 1);
}

original_code:
static int fpt_get_height(const struct fptree_node *r)
{
 int ret = 0, i, x;
 for (i = 0; i < r->num_children; i++) {
  x = fpt_get_height(r->children[i]);
  if (ret < x)
   ret = x;
 }
 return ret + 1;
}

canonical_original_code:
static int fpt_get_height(struct fptree_node *r)
{
 int ret = 0; int i; int x;
 for (i = 0; i < r->num_children; i++) {
  x = fpt_get_height(r->children[i]);
  if (ret < x)
   ret = x;
 }
 return ret + 1;
}

variable_types: struct fptree_node { int val; int cnt; struct fptree_node *next; int num_children; struct fptree_node **children; int sz_children; struct fptree_node *parent; } *

variable_types: ret int

variable_types: i int


decompiled_code:
__int64 __fastcall fpt_get_nodes(__int64 a1)
{
int v1; // eax
unsigned int v2; // ebp
_QWORD *v3; // rbx
__int64 v4; // r12

v1 = *(_DWORD *)(a1 + 16);
v2 = 1;
if ( v1 > 0 )
{
v3 = *(_QWORD **)(a1 + 24);
v4 = (__int64)&v3[(unsigned int)(v1 - 1) + 1];
do
v2 += fpt_get_nodes(*v3++);
while ( v3 != (_QWORD *)v4 );
}
return v2;
}

original_code:
static int fpt_get_nodes(const struct fptree_node *r)
{
 int ret = 1, i;
 for (i = 0; i < r->num_children; i++)
  ret += fpt_get_nodes(r->children[i]);
 return ret;
}

canonical_original_code:
static int fpt_get_nodes(struct fptree_node *r)
{
 int ret = 1; int i;
 for (i = 0; i < r->num_children; i++)
  ret += fpt_get_nodes(r->children[i]);
 return ret;
}

user_defined_types: struct fptree_node { int val; int cnt; struct fptree_node *next; int num_children; struct fptree_node **children; int sz_children; struct fptree_node *parent; }


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

if ( a2 )
printf("%*c", 2 * a2, 32LL);
printf("%p %d %d %p %d <", a1, *a1, a1[1], *((const void **)a1 + 1), a1[4]);
if ( (int)a1[4] > 0 )
{
v2 = 0LL;
do
printf("%p ", *(const void **)(*((_QWORD *)a1 + 3) + 8 * v2++));
while ( (int)a1[4] > (int)v2 );
}
result = printf("> %d\n", a1[8]);
if ( (int)a1[4] > 0 )
{
v4 = 0LL;
do
result = fpt_node_print(*(_QWORD *)(*((_QWORD *)a1 + 3) + 8 * v4++), (unsigned int)(a2 + 1));
while ( (int)a1[4] > (int)v4 );
}
return result;
}

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

if ( a2 )
printf("%*c", 2 * a2, 32LL);
printf("%p %d %d %p %d <", a1, *a1, a1[1], *((const void **)a1 + 1), a1[4]);
if ( (int)a1[4] > 0 )
{
v2 = 0LL;
do
printf("%p ", *(const void **)(*((_QWORD *)a1 + 3) + 8 * v2++));
while ( (int)a1[4] > (int)v2 );
}
result = printf("> %d\n", a1[8]);
if ( (int)a1[4] > 0 )
{
v4 = 0LL;
do
result = func4(*(_QWORD *)(*((_QWORD *)a1 + 3) + 8 * v4++), (unsigned int)(a2 + 1));
while ( (int)a1[4] > (int)v4 );
}
return result;
}

original_code:
static void fpt_node_print(const struct fptree_node *r, int gap)
{
 int i, j;
 if (gap)
  printf("%*c", 2 * gap, ' ');
 printf("%p %d %d %p %d <", r, r->val, r->cnt, r->next, r->num_children);
 for (j = 0; j < r->num_children; j++)
  printf("%p ", r->children[j]);
 printf("> %d\n", r->sz_children);
 for (i = 0; i < r->num_children; i++) {
  fpt_node_print(r->children[i], gap + 1);
 }
}

canonical_original_code:
static void fpt_node_print(struct fptree_node *r, int gap)
{
 int i; int j;
 if (gap)
  printf("%*c", 2 * gap, ' ');
 printf("%p %d %d %p %d <", r, r->val, r->cnt, r->next, r->num_children);
 for (j = 0; j < r->num_children; j++)
  printf("%p ", r->children[j]);
 printf("> %d\n", r->sz_children);
 for (i = 0; i < r->num_children; i++) {
  fpt_node_print(r->children[i], gap + 1);
 }
}

variable_types: gap int

variable_types: j int


decompiled_code:
_DWORD *fpt_node_new()
{
_DWORD *v0; // rbx

v0 = calloc(1uLL, 0x30uLL);
v0[8] = 10;
*((_QWORD *)v0 + 3) = calloc(0xAuLL, 8uLL);
return v0;
}

canonical_decompiled_code:
_DWORD *func5()
{
_DWORD *v0; // rbx

v0 = calloc(1uLL, 0x30uLL);
v0[8] = 10;
*((_QWORD *)v0 + 3) = calloc(0xAuLL, 8uLL);
return v0;
}

original_code:
static struct fptree_node *fpt_node_new()
{
 struct fptree_node *ret = calloc(1, sizeof(ret[0]));
 ret->sz_children = 10;
 ret->children = calloc(ret->sz_children, sizeof(ret->children[0]));
 return ret;
}

return_type: struct fptree_node { int val; int cnt; struct fptree_node *next; int num_children; struct fptree_node **children; int sz_children; struct fptree_node *parent; } *


original_code:
static void fpt_node_free(const struct fptree_node *r)
{
 int i;
 for (i = 0; i < r->num_children; i++)
  fpt_node_free(r->children[i]);
 free(r->children);
 free((void*)r);
}

canonical_original_code:
static void fpt_node_free(struct fptree_node *r)
{
 int i;
 for (i = 0; i < r->num_children; i++)
  fpt_node_free(r->children[i]);
 free(r->children);
 free((void *)r);
}


decompiled_code:
int __fastcall fpt_tree_print(__int64 a1)
{
return fpt_node_print(*(unsigned int **)(a1 + 24), 0);
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
return func4(*(unsigned int **)(a1 + 24), 0);
}

original_code:
void fpt_tree_print(const struct fptree *fp)
{
 fpt_node_print(fp->tree, 0);
}

canonical_original_code:
void fpt_tree_print(struct fptree *fp)
{
 fpt_node_print(fp->tree, 0);
}

variable_types: struct fptree { unsigned long n; unsigned long t; struct table { unsigned long val; unsigned long cnt; struct fptree_node { int val; int cnt; struct fptree_node *next; int num_children; struct fptree_node **children; int sz_children; struct fptree_node *parent; } *fst; struct fptree_node *lst; unsigned long rpi; } *table; struct fptree_node *tree; } *

user_defined_types: struct fptree { unsigned long n; unsigned long t; struct table *table; struct fptree_node *tree; }


original_code:
void fpt_table_print(const struct fptree *fp)
{
 struct table *table = fp->table;
 struct fptree_node *p;
 int n = fp->n;
 int i;
 for (i = 0; i < n; i++) {
  printf("%d] %lu %lu %lu | %p -> %p |", i, table[i].val, table[i].cnt, table[i].rpi, table[i].fst, table[i].lst);
  p = table[i].fst;
  while (p != table[i].lst) {
   printf(" %p", p);
   p = p->next;
  }
  printf(" %p\n", p);
 }
}

canonical_original_code:
void fpt_table_print(struct fptree *fp)
{
 struct table *table = fp->table;
 struct fptree_node *p;
 int n = fp->n;
 int i;
 for (i = 0; i < n; i++) {
  printf("%d] %lu %lu %lu | %p -> %p |", i, table[i].val, table[i].cnt, table[i].rpi, table[i].fst, table[i].lst);
  p = table[i].fst;
  while (p != table[i].lst) {
   printf(" %p", p);
   p = p->next;
  }
  printf(" %p\n", p);
 }
}

variable_types: n int


decompiled_code:
void __fastcall fpt_cleanup(__int64 a1)
{
free(*(void **)(a1 + 16));
fpt_node_free(*(_QWORD *)(a1 + 24));
}

original_code:
void fpt_cleanup(const struct fptree *fp)
{
 free(fp->table);
 fpt_node_free(fp->tree);
}

canonical_original_code:
void fpt_cleanup(struct fptree *fp)
{
 free(fp->table);
 fpt_node_free(fp->tree);
}


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

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

original_code:
int fpt_height(const struct fptree *fp)
{
 return fpt_get_height(fp->tree);
}

canonical_original_code:
int fpt_height(struct fptree *fp)
{
 return fpt_get_height(fp->tree);
}


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

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

original_code:
int fpt_nodes(const struct fptree *fp)
{
 return fpt_get_nodes(fp->tree);
}

canonical_original_code:
int fpt_nodes(struct fptree *fp)
{
 return fpt_get_nodes(fp->tree);
}


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

result = 0LL;
if ( a2 >= 0 && (unsigned __int64)a2 < *a1 )
result = *(unsigned int *)(a1[2] + 40LL * *(_QWORD *)(a1[2] + 40LL * a2 + 32) + 8);
return result;
}

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

result = 0LL;
if ( a2 >= 0 && (unsigned __int64)a2 < *a1 )
result = *(unsigned int *)(a1[2] + 40LL * *(_QWORD *)(a1[2] + 40LL * a2 + 32) + 8);
return result;
}

original_code:
int fpt_item_count(const struct fptree *fp, int it)
{
 if (it < 0 || (size_t)it >= fp->n)
  return 0;
 return fp->table[fp->table[it].rpi].cnt;
}

canonical_original_code:
int fpt_item_count(struct fptree *fp, int it)
{
 if (it < 0 || (unsigned long)it >= fp->n)
  return 0;
 return fp->table[fp->table[it].rpi].cnt;
}

variable_types: it int


original_code:
int fpt_itemset_count(const struct fptree *fp, const int *its, int itslen)
{
 int *search_key = calloc(itslen, sizeof(search_key[0]));
 int i, count = 0, key_len = 0;
 struct fptree_node *p, *l;
 for (i = 0; i < itslen; i++)
  if (its[i] > 0)
   search_key[key_len++] = fp->table[its[i] - 1].rpi;
 qsort(search_key, key_len, sizeof(search_key[0]), int_cmp);
 for (i = 0; i < key_len; i++)
  search_key[i] = fp->table[search_key[i]].val;
 i = fp->table[search_key[key_len - 1] - 1].rpi;
 p = fp->table[i].fst;
 l = fp->table[i].lst;
 while (p && p != l) {
  count += search_on_path(p, search_key, key_len);
  p = p->next;
 }
 if (p)
  count += search_on_path(p, search_key, key_len);
 free(search_key);
 return count;
}

canonical_original_code:
int fpt_itemset_count(struct fptree *fp, int *its, int itslen)
{
 int *search_key = calloc(itslen, sizeof(search_key[0]));
 int i; int count = 0; int key_len = 0;
 struct fptree_node *p; struct fptree_node *l;
 for (i = 0; i < itslen; i++)
  if (its[i] > 0)
   search_key[key_len++] = fp->table[its[i] - 1].rpi;
 qsort(search_key, key_len, sizeof(search_key[0]), int_cmp);
 for (i = 0; i < key_len; i++)
  search_key[i] = fp->table[search_key[i]].val;
 i = fp->table[search_key[key_len - 1] - 1].rpi;
 p = fp->table[i].fst;
 l = fp->table[i].lst;
 while (p && p != l) {
  count += search_on_path(p, search_key, key_len);
  p = p->next;
 }
 if (p)
  count += search_on_path(p, search_key, key_len);
 free(search_key);
 return count;
}

variable_types: itslen int

variable_types: count int


original_code:
static void store_item_at(struct reservoir *r, size_t ix, const void *it,
  double w, double u, double v)
{
 r->its[ix].item_ptr = r->clone_fun(it );
 r->its[ix].w = w;
 r->its[ix].u = u;
 r->its[ix].v = v;
}

canonical_original_code:
static void store_item_at(struct reservoir *r, unsigned long ix, void *it, double w, double u, double v)
{
 r->its[ix].item_ptr = r->clone_fun(it );
 r->its[ix].w = w;
 r->its[ix].u = u;
 r->its[ix].v = v;
}

variable_types: struct reservoir { struct reservoir_item { void *item_ptr; double w; double u; double v; } *its; unsigned long actual; unsigned long sz; void (*print_fun)(void *it); void *(*clone_fun)(void *it); void (*free_fun)(void *it); } *

variable_types:
ix
unsigned long

variable_types:
it
void *

variable_types: w double

variable_types: u double

variable_types: v double

user_defined_types: struct reservoir { struct reservoir_item *its; unsigned long actual; unsigned long sz; void (*print_fun)(void *it); void *(*clone_fun)(void *it); void (*free_fun)(void *it); }


decompiled_code:
double __fastcall generate_random_uniform(struct drand48_data *a1)
{
double result[2]; // [rsp+8h] [rbp-10h] BYREF

drand48_r(a1, result);
return result[0];
}

canonical_decompiled_code:
double __fastcall func1(struct drand48_data *a1)
{
double result[2]; // [rsp+8h] [rbp-10h] BYREF

drand48_r(a1, result);
return result[0];
}

original_code:
static inline double generate_random_uniform(struct drand48_data *randbuffer)
{
 double u;
 drand48_r(randbuffer, &u);
 return u;
}

variable_types: struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } *


original_code:
static void store_item(struct reservoir *r, const void *it,
  double w, double u, double v)
{
 if (r->actual < r->sz) {
  store_item_at(r, r->actual, it, w, u, v);
  r->actual++;
  goto end;
 }
 if (v >= r->its[r->sz - 1].v)
  return;
 r->free_fun((void*)r->its[r->sz-1].item_ptr);
 store_item_at(r, r->sz - 1, it, w, u, v);
end:
 if (r->actual == r->sz) {
  qsort(r->its, r->sz, sizeof(r->its[0]), reservoir_cmp);
 }
}

canonical_original_code:
static void store_item(struct reservoir *r, void *it, double w, double u, double v)
{
 if (r->actual < r->sz) {
  store_item_at(r, r->actual, it, w, u, v);
  r->actual++;
  goto end;
 }
 if (v >= r->its[r->sz - 1].v)
  return;
 r->free_fun((void *)r->its[r->sz-1].item_ptr);
 store_item_at(r, r->sz - 1, it, w, u, v);
end:
 if (r->actual == r->sz) {
  qsort(r->its, r->sz, sizeof(r->its[0]), reservoir_cmp);
 }
}

user_defined_types: struct reservoir_item { void *item_ptr; double w; double u; double v; }


decompiled_code:
__int64 __fastcall reservoir_cmp(const void *a1, const void *a2)
{
return double_cmp((char *)a1 + 24, (char *)a2 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func3(const void *a1, const void *a2)
{
return double_cmp((char *)a1 + 24, (char *)a2 + 24);
}

original_code:
static int reservoir_cmp(const void *a, const void *b)
{
 const struct reservoir_item *ra = a, *rb = b;
 return double_cmp(&ra->v, &rb->v);
}

canonical_original_code:
static int reservoir_cmp(void *a, void *b)
{
 struct reservoir_item *ra = a; struct reservoir_item *rb = b;
 return double_cmp(&ra->v, &rb->v);
}

variable_types:
a
void *

variable_types:
b
void *

variable_types: struct reservoir_item { void *item_ptr; double w; double u; double v; } *


decompiled_code:
void __fastcall free_reservoir(void *ptr)
{
unsigned __int64 v1; // rbx

if ( *((_QWORD *)ptr + 1) )
{
v1 = 0LL;
do
(*((void (__fastcall **)(_QWORD))ptr + 5))(*(_QWORD *)(*(_QWORD *)ptr + 32 * v1++));
while ( *((_QWORD *)ptr + 1) > v1 );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
unsigned __int64 v1; // rbx

if ( *((_QWORD *)ptr + 1) )
{
v1 = 0LL;
do
(*((void (__fastcall **)(_QWORD))ptr + 5))(*(_QWORD *)(*(_QWORD *)ptr + 32 * v1++));
while ( *((_QWORD *)ptr + 1) > v1 );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void free_reservoir(struct reservoir *r)
{
 size_t i;
 for (i = 0; i < r->actual; i++)
  r->free_fun((void*)r->its[i].item_ptr);
 free(r->its);
 free(r);
}

canonical_original_code:
void free_reservoir(struct reservoir *r)
{
 unsigned long i;
 for (i = 0; i < r->actual; i++)
  r->free_fun((void *)r->its[i].item_ptr);
 free(r->its);
 free(r);
}

variable_types:
i
unsigned long


decompiled_code:
void __fastcall add_to_reservoir(__int64 a1, __int64 a2, struct drand48_data *a3, double a4)
{
double v5; // rbx
double v6; // xmm2_8

v5 = generate_random_uniform(a3);
v6 = COERCE_DOUBLE(COERCE_UNSIGNED_INT64(log(v5)) ^ LC0) / a4;
store_item(a1, a2, a4, v5, v6);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, struct drand48_data *a3, double a4)
{
double v5; // rbx
double v6; // xmm2_8

v5 = func1(a3);
v6 = COERCE_DOUBLE(COERCE_UNSIGNED_INT64(log(v5)) ^ LC0) / a4;
func2(a1, a2, a4, v5, v6);
}

original_code:
void add_to_reservoir(struct reservoir *r, const void *it,
  double w, struct drand48_data *randbuffer)
{
 double u = generate_random_uniform(randbuffer);
 double v = -log(u)/w;
 store_item(r, it, w, u, v);
}

canonical_original_code:
void add_to_reservoir(struct reservoir *r, void *it, double w, struct drand48_data *randbuffer)
{
 double u = generate_random_uniform(randbuffer);
 double v = -log(u)/w;
 store_item(r, it, w, u, v);
}

user_defined_types: struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; }


decompiled_code:
void __fastcall add_to_reservoir_log(__int64 a1, __int64 a2, struct drand48_data *a3, double a4)
{
double v5; // xmm0_8
double v6; // xmm2_8
double v7; // [rsp+8h] [rbp-20h]

v7 = generate_random_uniform(a3);
v5 = log(1.0 / v7);
v6 = log(v5) - a4;
store_item(a1, a2, a4, v7, v6);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, __int64 a2, struct drand48_data *a3, double a4)
{
double v5; // xmm0_8
double v6; // xmm2_8
double v7; // [rsp+8h] [rbp-20h]

v7 = func1(a3);
v5 = log(1.0 / v7);
v6 = log(v5) - a4;
func2(a1, a2, a4, v7, v6);
}

original_code:
void add_to_reservoir_log(struct reservoir *r, const void *it,
  double logw, struct drand48_data *randbuffer)
{
 double u = generate_random_uniform(randbuffer);
 double v = log(log(1/u)) - logw;
 store_item(r, it, logw, u, v);
}

canonical_original_code:
void add_to_reservoir_log(struct reservoir *r, void *it, double logw, struct drand48_data *randbuffer)
{
 double u = generate_random_uniform(randbuffer);
 double v = log(log(1/u)) - logw;
 store_item(r, it, logw, u, v);
}

variable_types: logw double


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

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

original_code:
void free_reservoir_iterator(struct reservoir_iterator *ri)
{
 free(ri);
}

variable_types: struct reservoir_iterator { struct reservoir { struct reservoir_item { void *item_ptr; double w; double u; double v; } *its; unsigned long actual; unsigned long sz; void (*print_fun)(void *it); void *(*clone_fun)(void *it); void (*free_fun)(void *it); } *reservoir; unsigned long current_pos; } *

user_defined_types: struct reservoir_iterator { struct reservoir *reservoir; unsigned long current_pos; }


original_code:
const void *next_item(struct reservoir_iterator *ri)
{
 if (ri->current_pos == ri->reservoir->actual)
  return ((void *)0);
 return ri->reservoir->its[ri->current_pos++].item_ptr;
}

return_type: void *


laplace

decompiled_code:
double __fastcall laplace(struct drand48_data *a1, double a2)
{
__m128d v3; // xmm0
double v4; // xmm0_8
double result[2]; // [rsp+8h] [rbp-10h] BYREF

drand48_r(a1, result);
v3 = (__m128d)(unsigned __int64)LC0;
result[0] = 0.5 - result[0];
v3.m128d_f64[0] = result[0];
if ( (_mm_movemask_pd(v3) & 1) != 0 )
v4 = log(result[0] + result[0] + 1.0) * a2;
else
v4 = COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC2) * log(1.0 - (result[0] + result[0]));
return v4;
}

canonical_decompiled_code:
double __fastcall func0(struct drand48_data *a1, double a2)
{
__m128d v3; // xmm0
double v4; // xmm0_8
double result[2]; // [rsp+8h] [rbp-10h] BYREF

drand48_r(a1, result);
v3 = (__m128d)(unsigned __int64)LC0;
result[0] = 0.5 - result[0];
v3.m128d_f64[0] = result[0];
if ( (_mm_movemask_pd(v3) & 1) != 0 )
v4 = log(result[0] + result[0] + 1.0) * a2;
else
v4 = COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC2) * log(1.0 - (result[0] + result[0]));
return v4;
}

original_code:
static double laplace(double lambda, struct drand48_data *buffer)
{
 double rnd;
 drand48_r(buffer, &rnd);
 rnd = 0.5 - rnd;
 if (__builtin_signbit (rnd))
  return lambda * log(1 + 2 * rnd);
 return -lambda * log(1 - 2 * rnd);
}

variable_types: lambda double

variable_types: rnd double


decompiled_code:
int __fastcall init_rng(__int64 seedval, struct drand48_data *buffer)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
return srand48_r(seedval, buffer);
}

canonical_decompiled_code:
int __fastcall func1(__int64 seedval, struct drand48_data *buffer)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
return srand48_r(seedval, buffer);
}

original_code:
void init_rng(long int seed, struct drand48_data *buffer)
{
 struct timeval now;
 gettimeofday(&now, ((void *)0));
 srand48_r(seed, buffer);
}

canonical_original_code:
void init_rng(long seed, struct drand48_data *buffer)
{
 struct timeval now;
 gettimeofday(&now, ((void *)0));
 srand48_r(seed, buffer);
}

variable_types: seed long

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


decompiled_code:
__int64 __fastcall int_cmp(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

original_code:
int int_cmp(const void *a, const void *b)
{
 const int *ia = a, *ib = b;
 return *ia - *ib;
}

canonical_original_code:
int int_cmp(void *a, void *b)
{
 int *ia = a; int *ib = b;
 return *ia - *ib;
}

variable_types:
ia
int *

variable_types:
ib
int *


decompiled_code:
__int64 __fastcall int_cmp_r(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a2 - *a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a2 - *a1);
}

original_code:
int int_cmp_r(const void *a, const void *b)
{
 const int *ia = a, *ib = b;
 return *ib - *ia;
}

canonical_original_code:
int int_cmp_r(void *a, void *b)
{
 int *ia = a; int *ib = b;
 return *ib - *ia;
}


decompiled_code:
__int64 __fastcall double_cmp(double *a1, double *a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *a2 <= *a1 )
result = *a1 > *a2;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *a2 <= *a1 )
result = *a1 > *a2;
return result;
}

original_code:
int double_cmp(const void *a, const void *b)
{
 const double *fa = a, *fb = b;
 if (*fa < *fb)
  return -1;
 else if (*fa > *fb)
  return 1;
 return 0;
}

canonical_original_code:
int double_cmp(void *a, void *b)
{
 double *fa = a; double *fb = b;
 if (*fa < *fb)
  return -1;
 else if (*fa > *fb)
  return 1;
 return 0;
}

variable_types:
fa
double *

variable_types:
fb
double *


decompiled_code:
__int64 __fastcall double_cmp_r(double *a1, double *a2)
{
return (unsigned int)-double_cmp(a1, a2);
}

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

original_code:
int double_cmp_r(const void *a, const void *b)
{
 return -double_cmp(a, b);
}

canonical_original_code:
int double_cmp_r(void *a, void *b)
{
 return -double_cmp(a, b);
}


decompiled_code:
double __fastcall laplace_mechanism(struct drand48_data *a1, double a2, double a3, double a4)
{
return laplace(a1, a4 / a3) + a2;
}

canonical_decompiled_code:
double __fastcall func6(struct drand48_data *a1, double a2, double a3, double a4)
{
return func0(a1, a4 / a3) + a2;
}

original_code:
double laplace_mechanism(double x, double eps, double sens,
  struct drand48_data *buffer)
{
 return x + laplace(sens/eps, buffer);
}

variable_types: x double

variable_types: eps double

variable_types: sens double


decompiled_code:
__int64 __fastcall bsearch_i(__int64 a1, __int64 a2, int a3, __int64 a4, int (__fastcall *a5)(__int64, __int64))
{
int v5; // er12
unsigned int v8; // ebp
unsigned int v9; // ebx

v5 = a3 - 1;
if ( a3 - 1 < 0 )
return 0;
v8 = 0;
do
{
v9 = v8 + ((int)(v5 - v8) >> 1);
if ( a5(a2 + a4 * (int)v9, a1) > 0 )
v5 = v9 - 1;
else
v8 = v9 + 1;
}
while ( v5 >= (int)v8 );
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3, __int64 a4, int (__fastcall *a5)(__int64, __int64))
{
int v5; // er12
unsigned int v8; // ebp
unsigned int v9; // ebx

v5 = a3 - 1;
if ( a3 - 1 < 0 )
return 0;
v8 = 0;
do
{
v9 = v8 + ((int)(v5 - v8) >> 1);
if ( a5(a2 + a4 * (int)v9, a1) > 0 )
v5 = v9 - 1;
else
v8 = v9 + 1;
}
while ( v5 >= (int)v8 );
return v8;
}

original_code:
int bsearch_i(const void *key, const void *base, size_t nmemb, size_t size,
  int (*compar)(const void *, const void *))
{
 int low = 0, high = nmemb - 1, mid, test;
 while (low <= high) {
  mid = low + ((high - low) >> 1);
  test = compar(base + mid * size, key);
  if (test > 0)
   high = mid - 1;
  else
   low = mid + 1;
 }
 return low;
}

canonical_original_code:
int bsearch_i(void *key, void *base, unsigned long nmemb, unsigned long size, int (*compar)(void *, void *))
{
 int low = 0; int high = nmemb - 1; int mid; int test;
 while (low <= high) {
  mid = low + ((high - low) >> 1);
  test = compar(base + mid * size, key);
  if (test > 0)
   high = mid - 1;
  else
   low = mid + 1;
 }
 return low;
}

variable_types:
key
void *

variable_types:
base
void *

variable_types:
nmemb
unsigned long

variable_types:
size
unsigned long

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

variable_types: low int

variable_types: high int

variable_types: mid int

variable_types: test int


decompiled_code:
double __fastcall div_or_zero(double a1, double a2)
{
return a1 / a2;
}

canonical_decompiled_code:
double __fastcall func8(double a1, double a2)
{
return a1 / a2;
}

original_code:
double div_or_zero(double a, double b)
{
 double r = a / b;
 if (r != r) return 0;
 return r;
}

variable_types: a double

variable_types: b double

variable_types: r double


decompiled_code:
_QWORD *init_histogram()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x10uLL);
v0[1] = calloc(0xAuLL, 8uLL);
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x10uLL);
v0[1] = calloc(0xAuLL, 8uLL);
return v0;
}

original_code:
struct histogram *init_histogram()
{
 struct histogram *h = calloc(1, sizeof(*h));
 h->values = calloc(c_num_values, sizeof(h->values[0]));
 return h;
}

variable_types: struct histogram { unsigned long total; unsigned long *values; } *


decompiled_code:
__int64 __fastcall histogram_get_bin_c(__int64 a1, unsigned int a2)
{
_QWORD *v2; // rax
__int64 v3; // rcx
__int64 v4; // rdx

if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_get_bin_c", 44LL);
exit(1);
}
v2 = (_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
v3 = (__int64)&v2[-a2 - 1];
v4 = 0LL;
do
v4 += *v2--;
while ( v2 != (_QWORD *)v3 );
return v4;
}

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

if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_get_bin_c", 44LL);
exit(1);
}
v2 = (_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
v3 = (__int64)&v2[-a2 - 1];
v4 = 0LL;
do
v4 += *v2--;
while ( v2 != (_QWORD *)v3 );
return v4;
}

original_code:
size_t histogram_get_bin_c(const struct histogram *h, int bin)
{
 size_t ret = 0;
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 44); exit(1); } while (0);
 for (; bin >= 0; bin--)
  ret += h->values[bin];
 return ret;
}

canonical_original_code:
unsigned long histogram_get_bin_c(struct histogram *h, int bin)
{
 unsigned long ret = 0;
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 44); exit(1); } while (0);
 for (; bin >= 0; bin--)
  ret += h->values[bin];
 return ret;
}

variable_types: bin int

variable_types:
ret
unsigned long

return_type: unsigned long

user_defined_types: struct histogram { unsigned long total; unsigned long *values; }


decompiled_code:
__int64 __fastcall histogram_get_bin(__int64 a1, unsigned int a2)
{
if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_get_bin", 55LL);
exit(1);
}
return *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2)
{
if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_get_bin", 55LL);
exit(1);
}
return *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
}

original_code:
size_t histogram_get_bin(const struct histogram *h, int bin)
{
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 55); exit(1); } while (0);
 return h->values[bin];
}

canonical_original_code:
unsigned long histogram_get_bin(struct histogram *h, int bin)
{
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 55); exit(1); } while (0);
 return h->values[bin];
}


decompiled_code:
double __fastcall histogram_bin_bound(__int64 a1, unsigned int a2)
{
if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_bin_bound", 62LL);
exit(1);
}
return c_values[a2];
}

canonical_decompiled_code:
double __fastcall func4(__int64 a1, unsigned int a2)
{
if ( a2 > 9 )
{
fprintf(stderr, "[%s: %s %d] Invalid bin requested\n", "histogram.c", "histogram_bin_bound", 62LL);
exit(1);
}
return c_values[a2];
}

original_code:
double histogram_bin_bound(const struct histogram *h, int bin)
{
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 62); exit(1); } while (0);
 h = h;
 return c_values[bin];
}

canonical_original_code:
double histogram_bin_bound(struct histogram *h, int bin)
{
 if (bin < 0 || bin >= c_num_values)
  do { fprintf(stderr, "[%s: %s %d] ""Invalid bin requested""\n", "histogram.c", __func__, 62); exit(1); } while (0);
 h = h;
 return c_values[bin];
}


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

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

original_code:
size_t histogram_get_all(const struct histogram *h)
{
 return h->total;
}

canonical_original_code:
unsigned long histogram_get_all(struct histogram *h)
{
 return h->total;
}


decompiled_code:
__int64 histogram_get_count_bins()
{
return 10LL;
}

canonical_decompiled_code:
__int64 func6()
{
return 10LL;
}

original_code:
int histogram_get_count_bins(const struct histogram *h)
{
 h = h;
 return c_num_values;
}

canonical_original_code:
int histogram_get_count_bins(struct histogram *h)
{
 h = h;
 return c_num_values;
}


decompiled_code:
void __fastcall free_histogram(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void free_histogram(struct histogram *h)
{
 free(h->values);
 free(h);
}


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

if ( !a2 )
{
LABEL_5:
fprintf(stderr, "[%s: %s %d] Invalid value in ic_search\n", "recall.c", "ic_search", 40LL);
exit(1);
}
result = 0LL;
while ( *(_DWORD *)(a1 + 8 * result) != a3 )
{
if ( a2 == ++result )
goto LABEL_5;
}
return result;
}

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

if ( !a2 )
{
LABEL_5:
fprintf(stderr, "[%s: %s %d] Invalid value in ic_search\n", "recall.c", "ic_search", 40LL);
exit(1);
}
result = 0LL;
while ( *(_DWORD *)(a1 + 8 * result) != a3 )
{
if ( a2 == ++result )
goto LABEL_5;
}
return result;
}

original_code:
static size_t ic_search(const struct item_count *ic, size_t n, int x)
{
 size_t i;
 for (i = 0; i < n; i++)
  if (ic[i].value == x)
   return i;
 do { fprintf(stderr, "[%s: %s %d] ""Invalid value in ic_search""\n", "recall.c", __func__, 40); exit(1); } while (0);
}

canonical_original_code:
static unsigned long ic_search(struct item_count *ic, unsigned long n, int x)
{
 unsigned long i;
 for (i = 0; i < n; i++)
  if (ic[i].value == x)
   return i;
 do { fprintf(stderr, "[%s: %s %d] ""Invalid value in ic_search""\n", "recall.c", __func__, 40); exit(1); } while (0);
}

variable_types: struct item_count { int value; int real_count; } *

variable_types:
n
unsigned long

variable_types: x int

variable_types:
i
unsigned long


cmp

decompiled_code:
__int64 __fastcall cmp(const void *a1, const void *a2)
{
return int_cmp_r((char *)a1 + 4, (char *)a2 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1, const void *a2)
{
return int_cmp_r((char *)a1 + 4, (char *)a2 + 4);
}

original_code:
static int cmp(const void *a, const void *b)
{
 const struct item_count *ia = a, *ib = b;
 return int_cmp_r(&ia->real_count, &ib->real_count);
}

canonical_original_code:
static int cmp(void *a, void *b)
{
 struct item_count *ia = a; struct item_count *ib = b;
 return int_cmp_r(&ia->real_count, &ib->real_count);
}

variable_types:
a
void *

variable_types:
b
void *

user_defined_types: struct item_count { int value; int real_count; }


repo: mikaelpatel/vfm

decompiled_code:
const char **__fastcall vfm_name2symb(char *s1, int *a2)
{
const char **v2; // rbx
const char **v3; // r12

if ( !a2 || !s1 )
return 0LL;
if ( !*s1 )
return 0LL;
v2 = (const char **)(*((_QWORD *)a2 + 1) + 24LL * *a2 - 24);
if ( *a2 <= 0 )
return 0LL;
v3 = &v2[-3 * (unsigned int)*a2];
while ( strcmp(s1, *v2) )
{
v2 -= 3;
if ( v2 == v3 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func5(char *s1, int *a2)
{
const char **v2; // rbx
const char **v3; // r12

if ( !a2 || !s1 )
return 0LL;
if ( !*s1 )
return 0LL;
v2 = (const char **)(*((_QWORD *)a2 + 1) + 24LL * *a2 - 24);
if ( *a2 <= 0 )
return 0LL;
v3 = &v2[-3 * (unsigned int)*a2];
while ( strcmp(s1, *v2) )
{
v2 -= 3;
if ( v2 == v3 )
return 0LL;
}
return v2;
}

original_code:
vfm_symb_t* vfm_name2symb(char* name, vfm_dict_t *dict)
{
  if (!dict || !name || !*name) return (0);
  vfm_symb_t *symbol = dict->symbols + dict->count - 1;
  int i;
  for(i = 0; i < dict->count; i++, symbol--)
    if (!strcmp(name, symbol->name))
      return (symbol);
  return (0);
}

canonical_original_code:
struct vfm_symb_t *vfm_name2symb(char *name, struct vfm_dict_t *dict)
{
  if (!dict || !name || !*name) return (0);
  struct vfm_symb_t *symbol = dict->symbols + dict->count - 1;
  int i;
  for(i = 0; i < dict->count; i++, symbol--)
    if (!strcmp(name, symbol->name))
      return (symbol);
  return (0);
}

variable_types:
name
char *

variable_types: struct vfm_dict_t { int count; int size; struct vfm_symb_t *symbols; } *

variable_types: struct vfm_symb_t { char *name; char *code; int mode; int refcnt; } *

variable_types: i int


decompiled_code:
__int64 __fastcall vfm_name2dict(char *s1, _QWORD *a2, __int64 a3)
{
int v3; // er12
__int64 v4; // r15
__int64 v5; // rbx
unsigned int v6; // er14
const char **v7; // rbp

v3 = *(_DWORD *)(a3 + 32);
if ( v3 <= 0 )
{
LABEL_5:
*a2 = 0LL;
v6 = -1;
}
else
{
v4 = *(_QWORD *)(a3 + 40);
v5 = 0LL;
while ( 1 )
{
v6 = v5;
v7 = *(const char ***)(v4 + 8 * v5);
if ( !strcmp(s1, *v7) )
break;
if ( ++v5 == v3 )
goto LABEL_5;
}
*a2 = v7 + 6;
}
return v6;
}

original_code:
int vfm_name2dict(char* name, vfm_dict_t **dict, vfm_mod_t *mod)
{
  int i;
  for (i = 0; i < mod->use.count; i++)
    if (!strcmp(name, mod->use.mod[i]->name)) {
      *dict = &mod->use.mod[i]->dict;
      return (i);
    }
  *dict = 0;
  return (-1);
}

canonical_original_code:
int vfm_name2dict(char *name, struct vfm_dict_t **dict, struct vfm_mod_t *mod)
{
  int i;
  for (i = 0; i < mod->use.count; i++)
    if (!strcmp(name, mod->use.mod[i]->name)) {
      *dict = &mod->use.mod[i]->dict;
      return (i);
    }
  *dict = 0;
  return (-1);
}

variable_types: struct vfm_dict_t { int count; int size; struct vfm_symb_t { char *name; char *code; int mode; int refcnt; } *symbols; } **

variable_types: struct vfm_mod_t { char *name; char *ident; char *version; long timestamp; struct vfm_use_t { int count; int size; struct vfm_mod_t **mod; } use; struct vfm_dict_t dict; struct vfm_segm_t { int count; int size; char *code; char *entry; } segment; } *

user_defined_types: struct vfm_mod_t { char *name; char *ident; char *version; long timestamp; struct vfm_use_t use; struct vfm_dict_t dict; struct vfm_segm_t segment; }

user_defined_types: struct vfm_dict_t { int count; int size; struct vfm_symb_t *symbols; }


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

if ( !a2 )
return 0LL;
v2 = *(char *)(a1 - 1);
if ( *(_DWORD *)a2 < v2 )
result = 0LL;
else
result = *(_QWORD *)(a2 + 8) + 24LL * v2;
return result;
}

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

if ( !a2 )
return 0LL;
v2 = *(char *)(a1 - 1);
if ( *(_DWORD *)a2 < v2 )
result = 0LL;
else
result = *(_QWORD *)(a2 + 8) + 24LL * v2;
return result;
}

original_code:
vfm_symb_t* vfm_addr2symb(vfm_code_t* addr, vfm_dict_t *dict)
{
  if (!dict) return (0);
  vfm_symb_t *symbol = dict->symbols;
  unsigned i = (unsigned) *(addr - 1);
  if (i > dict->count)
    return (0);
  return (&symbol[i]);
}

canonical_original_code:
struct vfm_symb_t *vfm_addr2symb(char *addr, struct vfm_dict_t *dict)
{
  if (!dict) return (0);
  struct vfm_symb_t *symbol = dict->symbols;
  unsigned int i = (unsigned) *(addr - 1);
  if (i > dict->count)
    return (0);
  return (&symbol[i]);
}

variable_types:
addr
char *

variable_types:
i
unsigned int

return_type: struct vfm_symb_t { char *name; char *code; int mode; int refcnt; } *


decompiled_code:
__int64 __fastcall vfm_lookup_module(char *s1, const char ***a2, __int64 a3)
{
char *v4; // r14
unsigned int v5; // ebx
const char **v6; // rax
int v7; // ebx
const char **v8; // rax
const char **v10; // rax
const char **v11; // rax
int *v12; // [rsp+8h] [rbp-30h] BYREF

v4 = vfm_parse_entry(s1);
vfm_errno = 0;
if ( v4 )
{
v5 = strcmp(*(const char **)a3, s1);
if ( v5 || (v6 = vfm_name2symb(v4, (int *)(a3 + 48)), (*a2 = v6) == 0LL) )
{
v7 = vfm_name2dict(s1, &v12, a3);
if ( v7 < 0 )
{
vfm_errno = -1;
v5 = -1;
}
else
{
v8 = vfm_name2symb(v4, v12);
*a2 = v8;
if ( v8 )
{
v5 = v7 + 1;
}
else
{
vfm_errno = -1;
v5 = -1;
}
}
}
}
else
{
v10 = vfm_name2symb(s1, (int *)(a3 + 48));
*a2 = v10;
v5 = 0;
if ( !v10 )
{
if ( *(int *)(a3 + 32) <= 0 )
{
LABEL_12:
vfm_errno = -1;
v5 = -1;
}
else
{
while ( 1 )
{
v11 = vfm_name2symb(s1, (int *)(*(_QWORD *)(*(_QWORD *)(a3 + 40) + 8LL * (_QWORD)v4) + 48LL));
*a2 = v11;
if ( v11 )
break;
if ( *(_DWORD *)(a3 + 32) <= (int)++v4 )
goto LABEL_12;
}
v5 = (_DWORD)v4 + 1;
}
}
}
return v5;
}

original_code:
int vfm_lookup_module(char* fullname, vfm_symb_t** symb, vfm_mod_t *mod)
{
  char* modulename = fullname;
  char* entryname = vfm_parse_entry(fullname);
  int i;
  vfm_errno = 0;
  if (!entryname) {
    entryname = fullname;
    *symb = vfm_name2symb(entryname, &mod->dict);
    if (*symb) return (0);
    for (i = 0; i < mod->use.count; i++) {
      *symb = vfm_name2symb(entryname, &mod->use.mod[i]->dict);
      if (*symb) return (i + 1);
    }
    return (vfm_errno = -1);
  }
  if (!strcmp(mod->name, modulename)) {
    *symb = vfm_name2symb(entryname, &mod->dict);
    if (*symb != 0) return (0);
  }
  vfm_dict_t* dict;
  i = vfm_name2dict(modulename, &dict, mod);
  if (i < 0) return (vfm_errno = -1);
  *symb = vfm_name2symb(entryname, dict);
  if (*symb) return (i + 1);
  return (vfm_errno = -1);
}

canonical_original_code:
int vfm_lookup_module(char *fullname, struct vfm_symb_t **symb, struct vfm_mod_t *mod)
{
  char *modulename = fullname;
  char *entryname = vfm_parse_entry(fullname);
  int i;
  vfm_errno = 0;
  if (!entryname) {
    entryname = fullname;
    *symb = vfm_name2symb(entryname, &mod->dict);
    if (*symb) return (0);
    for (i = 0; i < mod->use.count; i++) {
      *symb = vfm_name2symb(entryname, &mod->use.mod[i]->dict);
      if (*symb) return (i + 1);
    }
    return (vfm_errno = -1);
  }
  if (!strcmp(mod->name, modulename)) {
    *symb = vfm_name2symb(entryname, &mod->dict);
    if (*symb != 0) return (0);
  }
  struct vfm_dict_t *dict;
  i = vfm_name2dict(modulename, &dict, mod);
  if (i < 0) return (vfm_errno = -1);
  *symb = vfm_name2symb(entryname, dict);
  if (*symb) return (i + 1);
  return (vfm_errno = -1);
}

variable_types:
fullname
char *

variable_types: struct vfm_symb_t { char *name; char *code; int mode; int refcnt; } **

variable_types: struct vfm_mod_t { char *name; char *ident; char *version; long timestamp; struct vfm_use_t { int count; int size; struct vfm_mod_t **mod; } use; struct vfm_dict_t { int count; int size; struct vfm_symb_t *symbols; } dict; struct vfm_segm_t { int count; int size; char *code; char *entry; } segment; } *

variable_types:
modulename
char *

variable_types:
entryname
char *

variable_types: struct vfm_dict_t { int count; int size; struct vfm_symb_t { char *name; char *code; int mode; int refcnt; } *symbols; } *


decompiled_code:
__int64 __fastcall inc_refcnt(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = vfm_addr2symb(a1, a2);
if ( result )
++*(_DWORD *)(result + 20);
return result;
}

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

result = vfm_addr2symb(a1, a2);
if ( result )
++*(_DWORD *)(result + 20);
return result;
}

original_code:
static vfm_symb_t* inc_refcnt(vfm_code_t *cp, vfm_dict_t* dict)
{
  vfm_symb_t* symb = vfm_addr2symb(cp, dict);
  if (symb)
    symb->refcnt += 1;
  return (symb);
}

canonical_original_code:
static struct vfm_symb_t *inc_refcnt(char *cp, struct vfm_dict_t *dict)
{
  struct vfm_symb_t *symb = vfm_addr2symb(cp, dict);
  if (symb)
    symb->refcnt += 1;
  return (symb);
}

variable_types:
cp
char *

user_defined_types: struct vfm_symb_t { char *name; char *code; int mode; int refcnt; }


decompiled_code:
__int64 vfm_init()
{
return vfm_run(0LL);
}

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

original_code:
int vfm_init()
{
  return (vfm_run(0));
}

canonical_original_code:
int vfm_init()
{
  return (vfm_run(0));
}


repo: mikeandmore/segc

decompiled_code:
void *chunk_new()
{
return malloc(0xE0uLL);
}

canonical_decompiled_code:
void *func0()
{
return malloc(0xE0uLL);
}

original_code:
chunk_t*
chunk_new(void)
{
    chunk_t* ch = malloc(sizeof(chunk_t));
    return ch;
}

canonical_original_code:
struct _chunk_t *chunk_new()
{
    struct _chunk_t *ch = malloc(sizeof(chunk_t));
    return ch;
}

variable_types: struct _chunk_t { char w[3][64]; struct _word_info_t { unsigned char nbytes; unsigned char length; int freq; } *winfo[3]; unsigned long n; } *

user_defined_types: struct _chunk_t { char w[3][64]; struct _word_info_t *winfo[3]; unsigned long n; }


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

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

original_code:
void
chunk_free(chunk_t* ch)
{
    free(ch);
}

canonical_original_code:
void chunk_free(struct _chunk_t *ch)
{
    free(ch);
}

user_defined_types: struct _word_info_t { unsigned char nbytes; unsigned char length; int freq; }


original_code:
size_t
chunk_length(chunk_t* ch)
{
    int i;
    size_t len = 0;
    for(i = 0; i < ch->n; i++)
        len += ch->winfo[i]->length;
    return len;
}

canonical_original_code:
unsigned long chunk_length(struct _chunk_t *ch)
{
    int i;
    unsigned long len = 0;
    for(i = 0; i < ch->n; i++)
        len += ch->winfo[i]->length;
    return len;
}

variable_types:
len
unsigned long


decompiled_code:
double __fastcall chunk_average_length(__int64 a1)
{
__int64 v1; // rax
double v2; // xmm0_8
__int64 v3; // rax
double v4; // xmm1_8

v1 = chunk_length(a1);
if ( v1 < 0 )
v2 = (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1)) + (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1));
else
v2 = (double)(int)v1;
v3 = *(_QWORD *)(a1 + 216);
if ( v3 < 0 )
v4 = (double)(int)(*(_DWORD *)(a1 + 216) & 1 | ((unsigned __int64)v3 >> 1))
+ (double)(int)(*(_DWORD *)(a1 + 216) & 1 | ((unsigned __int64)v3 >> 1));
else
v4 = (double)(int)v3;
return v2 / v4;
}

canonical_decompiled_code:
double __fastcall func3(__int64 a1)
{
__int64 v1; // rax
double v2; // xmm0_8
__int64 v3; // rax
double v4; // xmm1_8

v1 = func2(a1);
if ( v1 < 0 )
v2 = (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1)) + (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1));
else
v2 = (double)(int)v1;
v3 = *(_QWORD *)(a1 + 216);
if ( v3 < 0 )
v4 = (double)(int)(*(_DWORD *)(a1 + 216) & 1 | ((unsigned __int64)v3 >> 1))
+ (double)(int)(*(_DWORD *)(a1 + 216) & 1 | ((unsigned __int64)v3 >> 1));
else
v4 = (double)(int)v3;
return v2 / v4;
}

original_code:
double
chunk_average_length(chunk_t* ch)
{
    return 1.0 * chunk_length(ch) / ch->n;
}

canonical_original_code:
double chunk_average_length(struct _chunk_t *ch)
{
    return 1.0 * chunk_length(ch) / ch->n;
}


original_code:
size_t
chunk_degree_of_MF(chunk_t* ch)
{
    size_t mf = 0;
    int i;
    for(i = 0; i < ch->n; i++)
        mf += ch->winfo[i]->freq;
    return mf;
}

canonical_original_code:
unsigned long chunk_degree_of_MF(struct _chunk_t *ch)
{
    unsigned long mf = 0;
    int i;
    for(i = 0; i < ch->n; i++)
        mf += ch->winfo[i]->freq;
    return mf;
}

variable_types:
mf
unsigned long


decompiled_code:
__int64 __fastcall get_new_size(unsigned __int64 a1)
{
unsigned __int64 *v1; // rax

v1 = (unsigned __int64 *)ℙ
while ( *v1 <= a1 )
{
if ( ++v1 == (unsigned __int64 *)&unk_360 )
return (unsigned int)a1;
}
return (unsigned int)*v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
unsigned __int64 *v1; // rax

v1 = (unsigned __int64 *)ℙ
while ( *v1 <= a1 )
{
if ( ++v1 == (unsigned __int64 *)&unk_360 )
return (unsigned int)a1;
}
return (unsigned int)*v1;
}

original_code:
static int
get_new_size(size_t size)
{
    int i;
    for(i = 0; i < sizeof(primes) / sizeof(size_t); i++)
        if(primes[i] > size)
            return primes[i];
    return size;
}

canonical_original_code:
static int get_new_size(unsigned long size)
{
    int i;
    for(i = 0; i < sizeof(primes) / sizeof(unsigned long); i++)
        if(primes[i] > size)
            return primes[i];
    return size;
}

variable_types:
size
unsigned long


hash

original_code:
static inline size_t
hash(const char* str,
     size_t len)
{
    size_t key = 0;
    while(len--)
    {
        key += *(str++);
        key += (key << 10);
        key ^= (key >> 6);
    }
    key += (key << 3);
    key ^= (key >> 11);
    key += (key << 15);
    return key;
}

canonical_original_code:
static inline unsigned long hash(char *str, unsigned long len)
{
    unsigned long key = 0;
    while(len--)
    {
        key += *(str++);
        key += (key << 10);
        key ^= (key >> 6);
    }
    key += (key << 3);
    key ^= (key >> 11);
    key += (key << 15);
    return key;
}

variable_types:
str
char *

variable_types:
key
unsigned long


rehash

original_code:
static void
rehash(hash_table_t* table)
{
    size_t newsize = get_new_size(table->capacity);
    hash_entry_t** newt = malloc(sizeof(hash_entry_t*) * newsize);
    memset(newt, 0, sizeof(hash_entry_t*) * newsize);
    hash_entry_t* entry, *next;
    int i;
    for(i = 0; i < table->capacity; i++)
    {
        entry = table->arr[i];
        while(entry)
        {
            int hash_val = hash(entry->key, strlen(entry->key));
            hash_val %= newsize;
            next = entry->next;
            entry->next = newt[hash_val];
            newt[hash_val] = entry;
            entry = next;
        }
    }
    free(table->arr);
    table->arr = newt;
    table->capacity = newsize;
}

canonical_original_code:
static void rehash(struct _hash_table_t *table)
{
    unsigned long newsize = get_new_size(table->capacity);
    struct _hash_entry_t **newt = malloc(sizeof(hash_entry_t*) * newsize);
    memset(newt, 0, sizeof(struct _hash_entry_t *) * newsize);
    struct _hash_entry_t *entry; struct _hash_entry_t *next;
    int i;
    for(i = 0; i < table->capacity; i++)
    {
        entry = table->arr[i];
        while(entry)
        {
            int hash_val = hash(entry->key, strlen(entry->key));
            hash_val %= newsize;
            next = entry->next;
            entry->next = newt[hash_val];
            newt[hash_val] = entry;
            entry = next;
        }
    }
    free(table->arr);
    table->arr = newt;
    table->capacity = newsize;
}

variable_types: struct _hash_table_t { struct _hash_entry_t { char key[64]; void *data; struct _hash_entry_t *next; } **arr; unsigned long capacity; unsigned long size; } *

variable_types:
newsize
unsigned long

variable_types: i int

user_defined_types: struct _hash_table_t { struct _hash_entry_t **arr; unsigned long capacity; unsigned long size; }


decompiled_code:
_QWORD *hash_table_new()
{
_QWORD *v0; // rbx
void *v1; // rax

v0 = malloc(0x18uLL);
v1 = malloc(0x200018uLL);
*v0 = v1;
memset(v1, 0, 0x200018uLL);
v0[1] = 262147LL;
v0[2] = 0LL;
return v0;
}

original_code:
hash_table_t*
hash_table_new(void)
{
    hash_table_t* table = malloc(sizeof(hash_table_t));
    table->arr = malloc(sizeof(hash_entry_t*) * 262147);
    memset(table->arr, 0, sizeof(hash_entry_t*) * 262147);
    table->capacity = 262147;
    table->size = 0;
    return table;
}

canonical_original_code:
struct _hash_table_t *hash_table_new()
{
    struct _hash_table_t *table = malloc(sizeof(hash_table_t));
    table->arr = malloc(sizeof(struct _hash_entry_t *) * 262147);
    memset(table->arr, 0, sizeof(struct _hash_entry_t *) * 262147);
    table->capacity = 262147;
    table->size = 0;
    return table;
}

return_type: struct _hash_table_t { struct _hash_entry_t { char key[64]; void *data; struct _hash_entry_t *next; } **arr; unsigned long capacity; unsigned long size; } *


original_code:
void
hash_table_insert(hash_table_t* table,
                  const char* w,
                  void* data)
{
    table->size++;
    if(table->capacity / table->size < 5)
        rehash(table);
    int val = hash(w, strlen(w)) % table->capacity;
    hash_entry_t* ent = table->arr[val];
    if(!ent)
    {
        table->arr[val] = pool_alloc(sizeof(hash_entry_t));
        table->arr[val]->next = 0;
        table->arr[val]->data = data;
        strcpy(table->arr[val]->key, w);
    }
    else
    {
        while(ent->next)
            ent = ent->next;
        ent->next = pool_alloc(sizeof(hash_entry_t));
        ent->next->next = 0;
        ent->next->data = data;
        strcpy(ent->next->key, w);
    }
}

canonical_original_code:
void hash_table_insert(struct _hash_table_t *table, char *w, void *data)
{
    table->size++;
    if(table->capacity / table->size < 5)
        rehash(table);
    int val = hash(w, strlen(w)) % table->capacity;
    struct _hash_entry_t *ent = table->arr[val];
    if(!ent)
    {
        table->arr[val] = pool_alloc(sizeof(hash_entry_t));
        table->arr[val]->next = 0;
        table->arr[val]->data = data;
        strcpy(table->arr[val]->key, w);
    }
    else
    {
        while(ent->next)
            ent = ent->next;
        ent->next = pool_alloc(sizeof(hash_entry_t));
        ent->next->next = 0;
        ent->next->data = data;
        strcpy(ent->next->key, w);
    }
}

variable_types:
w
char *

variable_types:
data
void *

variable_types: val int

variable_types: struct _hash_entry_t { char key[64]; void *data; struct _hash_entry_t *next; } *


decompiled_code:
__int64 __fastcall hash_table_search(_QWORD *a1, char *a2, unsigned __int64 a3)
{
__int64 v4; // rbx

v4 = *(_QWORD *)(*a1 + 8LL * (int)(hash(a2, a3) % a1[1]));
if ( v4 )
{
while ( strncmp((const char *)v4, a2, a3) )
{
v4 = *(_QWORD *)(v4 + 72);
if ( !v4 )
return v4;
}
v4 = *(_QWORD *)(v4 + 64);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, char *a2, unsigned __int64 a3)
{
__int64 v4; // rbx

v4 = *(_QWORD *)(*a1 + 8LL * (int)(func1(a2, a3) % a1[1]));
if ( v4 )
{
while ( strncmp((const char *)v4, a2, a3) )
{
v4 = *(_QWORD *)(v4 + 72);
if ( !v4 )
return v4;
}
v4 = *(_QWORD *)(v4 + 64);
}
return v4;
}

original_code:
void*
hash_table_search(hash_table_t* table,
            const char* w,
            size_t wlen)
{
    int val = hash(w, wlen) % table->capacity;
    hash_entry_t* ent = table->arr[val];
    while(ent)
    {
        if(strncmp(ent->key, w, wlen) == 0)
            return ent->data;
        ent = ent->next;
    }
    return ((void *)0);
}

canonical_original_code:
void *hash_table_search(struct _hash_table_t *table, char *w, unsigned long wlen)
{
    int val = hash(w, wlen) % table->capacity;
    struct _hash_entry_t *ent = table->arr[val];
    while(ent)
    {
        if(strncmp(ent->key, w, wlen) == 0)
            return ent->data;
        ent = ent->next;
    }
    return ((void *)0);
}

variable_types:
wlen
unsigned long

user_defined_types: struct _hash_entry_t { char key[64]; void *data; struct _hash_entry_t *next; }


decompiled_code:
void __fastcall hash_table_destroy(void *ptr)
{
unsigned __int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rdi

if ( *((_QWORD *)ptr + 1) )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*(_QWORD *)ptr + 8 * v2);
while ( v3 )
{
v4 = v3;
v3 = *(_QWORD *)(v3 + 72);
pool_free(v4);
}
++v2;
}
while ( *((_QWORD *)ptr + 1) > v2 );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
unsigned __int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rdi

if ( *((_QWORD *)ptr + 1) )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*(_QWORD *)ptr + 8 * v2);
while ( v3 )
{
v4 = v3;
v3 = *(_QWORD *)(v3 + 72);
pool_free(v4);
}
++v2;
}
while ( *((_QWORD *)ptr + 1) > v2 );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void
hash_table_destroy(hash_table_t* table)
{
    int i;
    for(i = 0; i < table->capacity; i++)
    {
        hash_entry_t* ent = table->arr[i];
        while(ent)
        {
            hash_entry_t* tmp = ent->next;
            pool_free(ent);
            ent = tmp;
        }
    }
    free(table->arr);
    free(table);
}

canonical_original_code:
void hash_table_destroy(struct _hash_table_t *table)
{
    int i;
    for(i = 0; i < table->capacity; i++)
    {
        struct _hash_entry_t *ent = table->arr[i];
        while(ent)
        {
            struct _hash_entry_t *tmp = ent->next;
            pool_free(ent);
            ent = tmp;
        }
    }
    free(table->arr);
    free(table);
}


decompiled_code:
_BOOL8 __fastcall is_wide_char(unsigned __int8 a1)
{
return (unsigned __int64)utf8_char_len(a1) > 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned __int8 a1)
{
return (unsigned __int64)utf8_char_len(a1) > 1;
}

original_code:
static int
is_wide_char(char ch)
{
    return utf8_char_len(ch) > 1;
}

variable_types: ch char


original_code:
static size_t
match_word_to_cache(algor_t* al,
                    const char* text)
{
    int i;
    for(i = 0; i < 8; i++)
        if(al->match_cache[i].pos_key == text)
            return i;
    al->current_cache_pos++;
    if(al->current_cache_pos == 8)
        al->current_cache_pos = 0;
    al->match_cache[al->current_cache_pos].size = 0;
    al->match_cache[al->current_cache_pos].pos_key = text;
    size_t text_len = 0;
    i = 0;
    while(i <= 4)
    {
        i++;
        size_t ch_len = utf8_char_len(text[text_len]);
        if(ch_len == 1 || ch_len == 0)
            continue;
        text_len += ch_len;
        word_info_t* info = dict_search_word_info(text, text_len);
        if(info)
            al->match_cache[al->current_cache_pos].
                content[al->match_cache[al->current_cache_pos].size++] = info;
    }
    return al->current_cache_pos;
}

canonical_original_code:
static unsigned long match_word_to_cache(struct _algor_t *al, char *text)
{
    int i;
    for(i = 0; i < 8; i++)
        if(al->match_cache[i].pos_key == text)
            return i;
    al->current_cache_pos++;
    if(al->current_cache_pos == 8)
        al->current_cache_pos = 0;
    al->match_cache[al->current_cache_pos].size = 0;
    al->match_cache[al->current_cache_pos].pos_key = text;
    unsigned long text_len = 0;
    i = 0;
    while(i <= 4)
    {
        i++;
        unsigned long ch_len = utf8_char_len(text[text_len]);
        if(ch_len == 1 || ch_len == 0)
            continue;
        text_len += ch_len;
        struct _word_info_t *info = dict_search_word_info(text, text_len);
        if(info)
            al->match_cache[al->current_cache_pos].
                content[al->match_cache[al->current_cache_pos].size++] = info;
    }
    return al->current_cache_pos;
}

variable_types: struct _algor_t { struct _chunk_t { char w[3][64]; struct _word_info_t { unsigned char nbytes; unsigned char length; int freq; } *winfo[3]; unsigned long n; } *current_chunks[32]; unsigned long current_chunks_size; struct _chunk_t *current_chunk; int current_token_pos; struct _cache_t { struct _word_info_t *content[4]; unsigned long size; char *pos_key; } match_cache[8]; unsigned long current_cache_pos; char *target_string; struct _chunk_t *temp_chunk; } *

variable_types:
text
char *

user_defined_types: struct _algor_t { struct _chunk_t *current_chunks[32]; unsigned long current_chunks_size; struct _chunk_t *current_chunk; int current_token_pos; struct _cache_t match_cache[8]; unsigned long current_cache_pos; char *target_string; struct _chunk_t *temp_chunk; }


decompiled_code:
_BOOL8 __fastcall latin_token(char *a1)
{
char v1; // bp
const unsigned __int16 *v2; // rcx
char v3; // al
__int64 v4; // rdx

v1 = *a1;
if ( !*a1 )
return v1 != 0;
v2 = *_ctype_b_loc();
v3 = v1;
v4 = 0LL;
do
{
if ( (v2[v3] & 0xC00) == 0 )
break;
v3 = a1[++v4];
}
while ( v3 );
if ( !v4 )
return v1 != 0;
return v4;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(char *a1)
{
char v1; // bp
const unsigned __int16 *v2; // rcx
char v3; // al
__int64 v4; // rdx

v1 = *a1;
if ( !*a1 )
return v1 != 0;
v2 = *_ctype_b_loc();
v3 = v1;
v4 = 0LL;
do
{
if ( (v2[v3] & 0xC00) == 0 )
break;
v3 = a1[++v4];
}
while ( v3 );
if ( !v4 )
return v1 != 0;
return v4;
}

original_code:
static size_t
latin_token(const char* text)
{
    size_t i = 0;
    while(text[i])
    {
        if(!((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short int) _ISalpha) && !((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short int) _ISdigit))
            break;
        i++;
    }
    if(i == 0 && text[0])
        return 1;
    return i;
}

canonical_original_code:
static unsigned long latin_token(char *text)
{
    unsigned long i = 0;
    while(text[i])
    {
        if(!((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short) _ISalpha) && !((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short) _ISdigit))
            break;
        i++;
    }
    if(i == 0 && text[0])
        return 1;
    return i;
}

variable_types:
i
unsigned long


original_code:
static size_t
chinese_token(algor_t* al)
{
    if(al->current_token_pos == -1)
    {
        refresh_chunks(al);
        al->current_token_pos = 0;
    }
    size_t ret = al->current_chunk->winfo[al->current_token_pos]->nbytes;
    al->current_token_pos++;
    if(al->current_token_pos == al->current_chunk->n)
        al->current_token_pos = -1;
    return ret;
}

canonical_original_code:
static unsigned long chinese_token(struct _algor_t *al)
{
    if(al->current_token_pos == -1)
    {
        refresh_chunks(al);
        al->current_token_pos = 0;
    }
    unsigned long ret = al->current_chunk->winfo[al->current_token_pos]->nbytes;
    al->current_token_pos++;
    if(al->current_token_pos == al->current_chunk->n)
        al->current_token_pos = -1;
    return ret;
}

variable_types:
ret
unsigned long

user_defined_types: struct _cache_t { struct _word_info_t *content[4]; unsigned long size; char *pos_key; }


decompiled_code:
__int64 __fastcall complex_algo_next_token(__int64 a1)
{
unsigned __int8 *v2; // rbp
unsigned __int8 v3; // di
__int64 result; // rax
__int64 v5; // rax

v2 = *(unsigned __int8 **)(a1 + 672);
v3 = *v2;
result = 0LL;
if ( *v2 )
{
result = 1LL;
if ( v3 != 10 )
{
if ( *(int *)(a1 + 272) >= 0 )
{
result = chinese_token((const __m128i *)a1);
}
else
{
v5 = utf8_char_len(v3);
if ( v5 == 1 )
{
result = latin_token(*(char **)(a1 + 672));
}
else if ( dict_search_word_info(v2, v5) )
{
result = chinese_token((const __m128i *)a1);
}
else
{
result = one_unicode_token(a1);
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
unsigned __int8 *v2; // rbp
unsigned __int8 v3; // di
__int64 result; // rax
__int64 v5; // rax

v2 = *(unsigned __int8 **)(a1 + 672);
v3 = *v2;
result = 0LL;
if ( *v2 )
{
result = 1LL;
if ( v3 != 10 )
{
if ( *(int *)(a1 + 272) >= 0 )
{
result = func5((const __m128i *)a1);
}
else
{
v5 = utf8_char_len(v3);
if ( v5 == 1 )
{
result = func3(*(char **)(a1 + 672));
}
else if ( dict_search_word_info(v2, v5) )
{
result = func5((const __m128i *)a1);
}
else
{
result = func1(a1);
}
}
}
}
return result;
}

original_code:
static size_t
complex_algo_next_token(algor_t* al)
{
    const char* text = al->target_string;
    if(!text[0])
        return 0;
    if(text[0] == '\n')
        return 1;
    if(al->current_token_pos >= 0)
        return chinese_token(al);
    size_t len = utf8_char_len(text[0]);
    if(len == 1)
        return latin_token(al->target_string);
    if(dict_search_word_info(text, len))
        return chinese_token(al);
    else
        return one_unicode_token(al);
}

canonical_original_code:
static unsigned long complex_algo_next_token(struct _algor_t *al)
{
    char *text = al->target_string;
    if(!text[0])
        return 0;
    if(text[0] == '\n')
        return 1;
    if(al->current_token_pos >= 0)
        return chinese_token(al);
    unsigned long len = utf8_char_len(text[0]);
    if(len == 1)
        return latin_token(al->target_string);
    if(dict_search_word_info(text, len))
        return chinese_token(al);
    else
        return one_unicode_token(al);
}

variable_types:
len
unsigned long


decompiled_code:
void __fastcall algor_destroy(void *ptr)
{
_QWORD *v1; // rbx

v1 = ptr;
do
chunk_free(*v1++);
while ( v1 != (_QWORD *)((char *)ptr + 256) );
chunk_free(*((_QWORD *)ptr + 85));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
_QWORD *v1; // rbx

v1 = ptr;
do
chunk_free(*v1++);
while ( v1 != (_QWORD *)((char *)ptr + 256) );
chunk_free(*((_QWORD *)ptr + 85));
free(ptr);
}

original_code:
void
algor_destroy(algor_t* al)
{
    int i;
    for(i = 0; i < 32; i++)
        chunk_free(al->current_chunks[i]);
    chunk_free(al->temp_chunk);
    free(al);
}

canonical_original_code:
void algor_destroy(struct _algor_t *al)
{
    int i;
    for(i = 0; i < 32; i++)
        chunk_free(al->current_chunks[i]);
    chunk_free(al->temp_chunk);
    free(al);
}


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

result = complex_algo_next_token(a1);
*(_QWORD *)(a1 + 672) += result;
return result;
}

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

result = func6(a1);
*(_QWORD *)(a1 + 672) += result;
return result;
}

original_code:
size_t
algor_get_next_token(algor_t* al)
{
    size_t tok = complex_algo_next_token(al);
    al->target_string += tok;
    return tok;
}

canonical_original_code:
unsigned long algor_get_next_token(struct _algor_t *al)
{
    unsigned long tok = complex_algo_next_token(al);
    al->target_string += tok;
    return tok;
}

variable_types:
tok
unsigned long


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

result = 2LL;
if ( (unsigned __int8)(a1 + 64) > 0x1Fu )
{
result = 3LL;
if ( (unsigned __int8)(a1 + 32) > 0xFu )
result = a1 != 0;
}
return result;
}

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

result = 2LL;
if ( (unsigned __int8)(a1 + 64) > 0x1Fu )
{
result = 3LL;
if ( (unsigned __int8)(a1 + 32) > 0xFu )
result = a1 != 0;
}
return result;
}

original_code:
size_t
utf8_char_len(unsigned char ch)
{
    if(ch >= 0xC0 && ch <= 0xDF)
        return 2;
    if(ch >= 0xE0 && ch <= 0xEF)
        return 3;
    if(ch == 0)
        return 0;
    return 1;
}

canonical_original_code:
unsigned long utf8_char_len(unsigned char ch)
{
    if(ch >= 0xC0 && ch <= 0xDF)
        return 2;
    if(ch >= 0xE0 && ch <= 0xEF)
        return 3;
    if(ch == 0)
        return 0;
    return 1;
}

variable_types:
ch
unsigned char


decompiled_code:
__int64 __fastcall utf8_len(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 v3; // rbp

v1 = a1;
v2 = *a1;
if ( !v2 )
return 0LL;
v3 = 0LL;
do
{
v1 += utf8_char_len(v2);
++v3;
v2 = *v1;
}
while ( *v1 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 v3; // rbp

v1 = a1;
v2 = *a1;
if ( !v2 )
return 0LL;
v3 = 0LL;
do
{
v1 += func0(v2);
++v3;
v2 = *v1;
}
while ( *v1 );
return v3;
}

original_code:
size_t
utf8_len(const char* text)
{
    size_t len = 0;
    while(1)
    {
        char ch = (*text);
        if(ch == 0)
            break;
        text += utf8_char_len(ch);
        len++;
    }
    return len;
}

canonical_original_code:
unsigned long utf8_len(char *text)
{
    unsigned long len = 0;
    while(1)
    {
        char ch = (*text);
        if(ch == 0)
            break;
        text += utf8_char_len(ch);
        len++;
    }
    return len;
}


decompiled_code:
__int64 __fastcall make_word_info(char *a1, int a2)
{
char v2; // r13
__int64 v3; // rbx

v2 = strlen(a1);
v3 = pool_alloc(8LL);
*(_BYTE *)v3 = v2;
*(_DWORD *)(v3 + 4) = a2;
*(_BYTE *)(v3 + 1) = utf8_len(a1);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, int a2)
{
char v2; // r13
__int64 v3; // rbx

v2 = strlen(a1);
v3 = pool_alloc(8LL);
*(_BYTE *)v3 = v2;
*(_DWORD *)(v3 + 4) = a2;
*(_BYTE *)(v3 + 1) = func1(a1);
return v3;
}

original_code:
word_info_t*
make_word_info(const char* text,
               int freq)
{
    size_t len = strlen(text);
    word_info_t* w = pool_alloc(sizeof(word_info_t));
    w->nbytes = len;
    w->freq = freq;
    w->length = utf8_len(text);
    return w;
}

canonical_original_code:
struct _word_info_t *make_word_info(char *text, int freq)
{
    unsigned long len = strlen(text);
    struct _word_info_t *w = pool_alloc(sizeof(word_info_t));
    w->nbytes = len;
    w->freq = freq;
    w->length = utf8_len(text);
    return w;
}

variable_types: freq int

variable_types: struct _word_info_t { unsigned char nbytes; unsigned char length; int freq; } *


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

v1 = &ch_punc_1;
while ( strcmp(s1, (const char *)*v1) )
{
if ( ++v1 == (void **)&unk_170 )
return 0LL;
}
return 1LL;
}

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

v1 = &ch_punc_1;
while ( strcmp(s1, (const char *)*v1) )
{
if ( ++v1 == (void **)&unk_170 )
return 0LL;
}
return 1LL;
}

original_code:
int
is_chinese_punc(char* text)
{
    static char* ch_punc[] = {"，", "。", "：", "“", "”", "；", "-", "、", "《", "》"};
    static size_t ch_punc_size = sizeof(ch_punc) / sizeof(char*);
    int i;
    for(i = 0; i < ch_punc_size; i++)
        if(strcmp(text, ch_punc[i]) == 0)
            return 1;
    return 0;
}

canonical_original_code:
int is_chinese_punc(char *text)
{
    static char *ch_punc[] = {"，", "。", "：", "“", "”", "；", "-", "、", "《", "》"};
    static unsigned long ch_punc_size = sizeof(ch_punc) / sizeof(char*);
    int i;
    for(i = 0; i < ch_punc_size; i++)
        if(strcmp(text, ch_punc[i]) == 0)
            return 1;
    return 0;
}


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

result = hash_table_new();
root = result;
return result;
}

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

result = hash_table_new();
root = result;
return result;
}

original_code:
void
dict_init(void)
{
    root = hash_table_new();
}

canonical_original_code:
void dict_init()
{
    root = hash_table_new();
}


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

result = hash_table_destroy(root);
root = 0LL;
return result;
}

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

result = hash_table_destroy(root);
root = 0LL;
return result;
}

original_code:
void
dict_deinit(void)
{
    hash_table_destroy(root);
    root = ((void *)0);
}

canonical_original_code:
void dict_deinit()
{
    hash_table_destroy(root);
    root = ((void *)0);
}


decompiled_code:
__int64 __fastcall dict_search_word_info(__int64 a1, __int64 a2)
{
return hash_table_search(root, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
return hash_table_search(root, a1, a2);
}

original_code:
word_info_t*
dict_search_word_info(const char* text,
                      size_t tsize)
{
    word_info_t* ret = (word_info_t*)hash_table_search(root, text, tsize);
    return ret;
}

canonical_original_code:
struct _word_info_t *dict_search_word_info(char *text, unsigned long tsize)
{
    struct _word_info_t *ret = (word_info_t*)hash_table_search(root, text, tsize);
    return ret;
}

variable_types:
tsize
unsigned long

return_type: struct _word_info_t { unsigned char nbytes; unsigned char length; int freq; } *


decompiled_code:
char *__fastcall pool_alloc(size_t a1)
{
char *result; // rax

if ( a1 > 0x1FF || pool_max_size - pool_size <= a1 )
return (char *)malloc(a1);
result = (char *)pool_mem + pool_size;
pool_size += a1;
return result;
}

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

if ( a1 > 0x1FF || pool_max_size - pool_size <= a1 )
return (char *)malloc(a1);
result = (char *)pool_mem + pool_size;
pool_size += a1;
return result;
}

original_code:
void*
pool_alloc(size_t size)
{
    if(size < 512 &&
       size < _pool_max_size - _pool_size)
    {
        void* ret = _pool_mem + _pool_size;
        _pool_size += size;
        return ret;
    }
    return malloc(size);
}

canonical_original_code:
void *pool_alloc(unsigned long size)
{
    if(size < 512 &&
       size < _pool_max_size - _pool_size)
    {
        void *ret = _pool_mem + _pool_size;
        _pool_size += size;
        return ret;
    }
    return malloc(size);
}

variable_types:
size
unsigned long

variable_types:
ret
void *


decompiled_code:
void __fastcall pool_free(char *a1)
{
if ( pool_mem > a1 || a1 >= (char *)pool_mem + pool_max_size )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(char *a1)
{
if ( pool_mem > a1 || a1 >= (char *)pool_mem + pool_max_size )
free(a1);
}

original_code:
void
pool_free(void* ptr)
{
    if(ptr < _pool_mem ||
       ptr >= _pool_mem + _pool_max_size)
        free(ptr);
}

canonical_original_code:
void pool_free(void *ptr)
{
    if(ptr < _pool_mem ||
       ptr >= _pool_mem + _pool_max_size)
        free(ptr);
}

variable_types:
ptr
void *


decompiled_code:
void *pool_init()
{
void *result; // rax

pool_max_size = 0x200000LL;
result = malloc(0x200000uLL);
pool_mem = result;
pool_size = 0LL;
return result;
}

canonical_decompiled_code:
void *func2()
{
void *result; // rax

pool_max_size = 0x200000LL;
result = malloc(0x200000uLL);
pool_mem = result;
pool_size = 0LL;
return result;
}

original_code:
void
pool_init()
{
    _pool_max_size = 2097152;
    _pool_mem = malloc(_pool_max_size);
    _pool_size = 0;
}


decompiled_code:
void pool_deinit()
{
free(pool_mem);
pool_size = 0LL;
}

canonical_decompiled_code:
void func3()
{
free(pool_mem);
pool_size = 0LL;
}

original_code:
void
pool_deinit()
{
    free(_pool_mem);
    _pool_size = 0;
}


repo: mikedlowis-prototypes/sclpl

original_code:
static uint64_t hash64(uint64_t key)
{
    key = (~key) + (key << 21);
    key = key ^ (key >> 24);
    key = (key + (key << 3)) + (key << 8);
    key = key ^ (key >> 14);
    key = (key + (key << 2)) + (key << 4);
    key = key ^ (key >> 28);
    key = key + (key << 31);
    return key;
}

canonical_original_code:
static unsigned long hash64(unsigned long key)
{
    key = (~key) + (key << 21);
    key = key ^ (key >> 24);
    key = (key + (key << 3)) + (key << 8);
    key = key ^ (key >> 14);
    key = (key + (key << 2)) + (key << 4);
    key = key ^ (key >> 28);
    key = key + (key << 31);
    return key;
}

variable_types:
key
unsigned long


decompiled_code:
__int64 __fastcall num_buckets(unsigned int a1)
{
if ( a1 > 0x1C )
_assert_fail("idx < NUM_PRIMES", "source/gc.c", 0x22u, "num_buckets");
return (unsigned int)Primes[a1];
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
if ( a1 > 0x1C )
_assert_fail("idx < NUM_PRIMES", "source/gc.c", 0x22u, "num_buckets");
return (unsigned int)Primes[a1];
}

original_code:
static unsigned int num_buckets(unsigned int idx) {
    ((idx < (sizeof(Primes)/sizeof(unsigned int))) ? (void) (0) : __assert_fail ("idx < NUM_PRIMES", "source/gc.c", 34, __extension__ __PRETTY_FUNCTION__));
    return Primes[idx];
}

variable_types:
idx
unsigned int

return_type: unsigned int


original_code:
static hash_entry_t* hash_del(hash_t* hash, hash_entry_t* entry)
{
    unsigned int index;
    hash_entry_t *parent, *node, *ret = ((void *)0);
    entry->hash = hash64((uint64_t)(entry->object));
    index = (entry->hash % num_buckets(hash->bkt_count));
    parent = ((void *)0);
    node = hash->buckets[index];
    find_entry(&parent, &node, entry);
    if (node != ((void *)0)) {
        ret = node;
        node = node->next;
        if (parent != ((void *)0))
            parent->next = node;
        else
            hash->buckets[index] = node;
        hash->size--;
    }
    return ret;
}

canonical_original_code:
static struct hash_entry_t *hash_del(struct hash_t *hash, struct hash_entry_t *entry)
{
    unsigned int index;
    struct hash_entry_t *parent; struct hash_entry_t *node; struct hash_entry_t *ret = ((void *)0);
    entry->hash = hash64((unsigned long)(entry->object));
    index = (entry->hash % num_buckets(hash->bkt_count));
    parent = ((void *)0);
    node = hash->buckets[index];
    find_entry(&parent, &node, entry);
    if (node != ((void *)0)) {
        ret = node;
        node = node->next;
        if (parent != ((void *)0))
            parent->next = node;
        else
            hash->buckets[index] = node;
        hash->size--;
    }
    return ret;
}

variable_types: struct hash_t { unsigned long size; unsigned long bkt_count; struct hash_entry_t **buckets; } *

variable_types:
entry
struct hash_entry_t *

variable_types:
index
unsigned int

variable_types: struct hash_entry_t { struct obj_t { unsigned long refs; void (*destructor)(void *); } *object; unsigned int hash; struct hash_entry_t *next; } *

user_defined_types: struct hash_entry_t { struct obj_t *object; unsigned int hash; struct hash_entry_t *next; }