main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
_QWORD *v4; // rax
size_t v5; // rsi
_QWORD *v6; // rbp
__int64 j; // rbx
void *v8; // rdi
int v10; // edx
__int64 v11; // rbx
unsigned __int64 v12; // r13
char *v13; // r12
__int64 v14; // rax
__int64 i; // rbx
void *v16; // rdi
char *v17; // r12
char *v18; // rax
struct stat v19; // [rsp+0h] [rbp-B8h] BYREF

parse_options(argc, argv, envp);
if ( LODWORD(options[6]) )
{
v6 = aprox_path_match(*(char **)&options[5]);
qsort((void *)*v6, *((int *)v6 + 2), 8uLL, (__compar_fn_t)match_compare);
v10 = *((_DWORD *)v6 + 2) - 1;
if ( v10 >= 0 )
{
v11 = 8LL * v10;
v12 = 8 * (*((int *)v6 + 2) - (unsigned __int64)(unsigned int)v10) - 16;
do
{
v13 = realpath(**(const char ***)(*v6 + v11), 0LL);
v14 = *(_QWORD *)(*v6 + v11);
v11 -= 8LL;
fprintf(stderr, "%.0f%% %s\n", 100.0 * *(double *)(v14 + 8), v13);
free(v13);
}
while ( v12 != v11 );
}
for ( i = 0LL; *((_DWORD *)v6 + 2) > (int)i; ++i )
{
v16 = *(void **)(*v6 + 8 * i);
match_free(v16);
}
}
else
{
v3 = *(const char **)&options[5];
if ( LODWORD(options[0]) )
{
complete(*(char **)&options[5]);
return 0;
}
if ( !*v3 )
{
v18 = getenv("HOME");
printf("%s", v18);
return 0;
}
if ( !strcmp(*(const char **)&options[5], "-") )
{
printf("%s", v3);
return 0;
}
if ( !stat(v3, &v19) && (v19.st_mode & 0xF000) == 0x4000 )
{
printf("%s", *(const char **)&options[5]);
return 0;
}
v4 = aprox_path_match(*(char **)&options[5]);
v5 = *((int *)v4 + 2);
v6 = v4;
if ( (_DWORD)v5 )
{
qsort((void *)*v4, v5, 8uLL, (__compar_fn_t)match_compare);
v17 = realpath(**(const char ***)(*v6 + 8LL * *((int *)v6 + 2) - 8), 0LL);
printf("%s", v17);
fprintf(stderr, "%.0f%% %s\n", 100.0 * *(double *)(*(_QWORD *)*v6 + 8LL), v17);
free(v17);
}
else
{
printf("%s", *(const char **)&options[5]);
}
for ( j = 0LL; *((_DWORD *)v6 + 2) > (int)j; ++j )
{
v8 = *(void **)(*v6 + 8 * j);
match_free(v8);
}
}
free((void *)*v6);
free(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
_QWORD *v4; // rax
size_t v5; // rsi
_QWORD *v6; // rbp
__int64 j; // rbx
void *v8; // rdi
int v10; // edx
__int64 v11; // rbx
unsigned __int64 v12; // r13
char *v13; // r12
__int64 v14; // rax
__int64 i; // rbx
void *v16; // rdi
char *v17; // r12
char *v18; // rax
struct stat v19; // [rsp+0h] [rbp-B8h] BYREF

parse_options(argc, argv, envp);
if ( LODWORD(options[6]) )
{
v6 = aprox_path_match(*(char **)&options[5]);
qsort((void *)*v6, *((int *)v6 + 2), 8uLL, (__compar_fn_t)match_compare);
v10 = *((_DWORD *)v6 + 2) - 1;
if ( v10 >= 0 )
{
v11 = 8LL * v10;
v12 = 8 * (*((int *)v6 + 2) - (unsigned __int64)(unsigned int)v10) - 16;
do
{
v13 = realpath(**(const char ***)(*v6 + v11), 0LL);
v14 = *(_QWORD *)(*v6 + v11);
v11 -= 8LL;
fprintf(stderr, "%.0f%% %s\n", 100.0 * *(double *)(v14 + 8), v13);
free(v13);
}
while ( v12 != v11 );
}
for ( i = 0LL; *((_DWORD *)v6 + 2) > (int)i; ++i )
{
v16 = *(void **)(*v6 + 8 * i);
func2(v16);
}
}
else
{
v3 = *(const char **)&options[5];
if ( LODWORD(options[0]) )
{
func5(*(char **)&options[5]);
return 0;
}
if ( !*v3 )
{
v18 = getenv("HOME");
printf("%s", v18);
return 0;
}
if ( !strcmp(*(const char **)&options[5], "-") )
{
printf("%s", v3);
return 0;
}
if ( !stat(v3, &v19) && (v19.st_mode & 0xF000) == 0x4000 )
{
printf("%s", *(const char **)&options[5]);
return 0;
}
v4 = aprox_path_match(*(char **)&options[5]);
v5 = *((int *)v4 + 2);
v6 = v4;
if ( (_DWORD)v5 )
{
qsort((void *)*v4, v5, 8uLL, (__compar_fn_t)match_compare);
v17 = realpath(**(const char ***)(*v6 + 8LL * *((int *)v6 + 2) - 8), 0LL);
printf("%s", v17);
fprintf(stderr, "%.0f%% %s\n", 100.0 * *(double *)(*(_QWORD *)*v6 + 8LL), v17);
free(v17);
}
else
{
printf("%s", *(const char **)&options[5]);
}
for ( j = 0LL; *((_DWORD *)v6 + 2) > (int)j; ++j )
{
v8 = *(void **)(*v6 + 8 * j);
func2(v8);
}
}
free((void *)*v6);
free(v6);
return 0;
}

original_code:
int main(int argc, char *const argv[]) {
    int i;
    char *path;
    struct darray_match *matches;
    struct stat buf;
    parse_options(argc, argv);
    if (options.simulate) {
        matches = aprox_path_match(options.directory);
        qsort(matches->items, matches->size, sizeof(*matches->items),
              match_compare);
        for (i = matches->size - 1; i >= 0; i--) {
            path = realpath(matches->items[i]->dir, ((void *)0));
            fprintf(stderr, "%.0f%% %s\n", matches->items[i]->score * 100, path);
            free(path);
        }
        do { for (i=0; i<(matches)->size; i++) match_free((matches)->items[i]); free((matches)->items); } while (0);
        free(matches);
        return 0;
    }
    if (options.complete) {
        complete(options.directory);
        return 0;
    }
    if (!*options.directory) {
        printf("%s", getenv("HOME"));
        return 0;
    }
    if (strcmp(options.directory, "-") == 0) {
        printf("%s", options.directory);
        return 0;
    }
    if (stat(options.directory, &buf) == 0 && ((((buf.st_mode)) & 0170000) == (0040000))) {
        printf("%s", options.directory);
        return 0;
    }
    matches = aprox_path_match(options.directory);
    if (matches->size) {
        qsort(matches->items, matches->size, sizeof(*matches->items),
              match_compare);
        path = realpath(matches->items[matches->size - 1]->dir, ((void *)0));
        printf("%s", path);
        fprintf(stderr, "%.0f%% %s\n", matches->items[0]->score * 100, path);
        free(path);
    }
    else
        printf("%s", options.directory);
    do { for (i=0; i<(matches)->size; i++) match_free((matches)->items[i]); free((matches)->items); } while (0);
    free(matches);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int i;
    char *path;
    struct darray_match *matches;
    struct stat buf;
    parse_options(argc, argv);
    if (options.simulate) {
        matches = aprox_path_match(options.directory);
        qsort(matches->items, matches->size, sizeof(*matches->items),
              match_compare);
        for (i = matches->size - 1; i >= 0; i--) {
            path = realpath(matches->items[i]->dir, ((void *)0));
            fprintf(stderr, "%.0f%% %s\n", matches->items[i]->score * 100, path);
            free(path);
        }
        do { for (i=0; i<(matches)->size; i++) match_free((matches)->items[i]); free((matches)->items); } while (0);
        free(matches);
        return 0;
    }
    if (options.complete) {
        complete(options.directory);
        return 0;
    }
    if (!*options.directory) {
        printf("%s", getenv("HOME"));
        return 0;
    }
    if (strcmp(options.directory, "-") == 0) {
        printf("%s", options.directory);
        return 0;
    }
    if (stat(options.directory, &buf) == 0 && ((((buf.st_mode)) & 0170000) == (0040000))) {
        printf("%s", options.directory);
        return 0;
    }
    matches = aprox_path_match(options.directory);
    if (matches->size) {
        qsort(matches->items, matches->size, sizeof(*matches->items),
              match_compare);
        path = realpath(matches->items[matches->size - 1]->dir, ((void *)0));
        printf("%s", path);
        fprintf(stderr, "%.0f%% %s\n", matches->items[0]->score * 100, path);
        free(path);
    }
    else
        printf("%s", options.directory);
    do { for (i=0; i<(matches)->size; i++) match_free((matches)->items[i]); free((matches)->items); } while (0);
    free(matches);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


min

repo: miguelvps/c

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 func6(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;
}

variable_types: x int

variable_types: y int

return_type: int


original_code:
int levenshtein_distance(const char *s, const char *t) {
    int i, j;
    int sl = strlen(s);
    int tl = strlen(t);
    int d[sl + 1][tl + 1];
    for (i = 0; i <= sl; i++)
        d[i][0] = i;
    for (j = 0; j <= tl; j++)
        d[0][j] = j;
    for (j = 1; j <= tl; j++) {
        for (i = 1; i <= sl; i++) {
            if (s[i - 1] == t[j - 1]) {
                d[i][j] = d[i - 1][j - 1];
            }
            else {
                d[i][j] = min(d[i - 1][j] + 1,
                              min(d[i][j - 1] + 1,
                                  d[i - 1][j - 1] + 1));
            }
        }
    }
    return d[sl][tl];
}

variable_types:
s
char *

variable_types:
t
char *

variable_types: i int

variable_types: j int

variable_types: sl int

variable_types: tl int


decompiled_code:
double __fastcall normalized_levenshtein_distance(const char *a1, const char *a2)
{
int v2; // er13
int v3; // ebx
int v4; // eax
double v5; // xmm0_8

v2 = strlen(a1);
v3 = strlen(a2);
v4 = levenshtein_distance();
if ( v2 <= v3 )
v5 = (double)v3;
else
v5 = (double)v2;
return 1.0 - (double)v4 / v5;
}

canonical_decompiled_code:
double __fastcall func8(const char *a1, const char *a2)
{
int v2; // er13
int v3; // ebx
int v4; // eax
double v5; // xmm0_8

v2 = strlen(a1);
v3 = strlen(a2);
v4 = func7();
if ( v2 <= v3 )
v5 = (double)v3;
else
v5 = (double)v2;
return 1.0 - (double)v4 / v5;
}

original_code:
double normalized_levenshtein_distance(const char *s, const char *t) {
    int sl = strlen(s);
    int tl = strlen(t);
    double d = levenshtein_distance(s, t);
    return 1 - (d / (sl > tl ? sl : tl));
}

canonical_original_code:
double normalized_levenshtein_distance(char *s, char *t) {
    int sl = strlen(s);
    int tl = strlen(t);
    double d = levenshtein_distance(s, t);
    return 1 - (d / (sl > tl ? sl : tl));
}

variable_types: d double

return_type: double


error

decompiled_code:
void __noreturn error(int status, int errnum, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", program_name);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s", v4);
}
fputc(10, stderr);
exit(status);
}

canonical_decompiled_code:
void __noreturn func0(int status, int errnum, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", program_name);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s", v4);
}
fputc(10, stderr);
exit(status);
}

original_code:
void error(int status, int errnum, const char *format, ...) {
    va_list args;
    fprintf(stderr, "%s: error: ", program_name);
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    if (errnum)
        fprintf(stderr, ": %s", strerror(errnum));
    fprintf(stderr, "\n");
    exit(status);
}

canonical_original_code:
void error(int status, int errnum, char *format, ...) {
    __builtin_va_list args;
    fprintf(stderr, "%s: error: ", program_name);
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    if (errnum)
        fprintf(stderr, ": %s", strerror(errnum));
    fprintf(stderr, "\n");
    exit(status);
}

variable_types: status int

variable_types: errnum int

variable_types:
format
char *

return_type: void


decompiled_code:
void *__fastcall s_malloc(size_t a1)
{
void *result; // rax
int *v2; // rax

result = malloc(a1);
if ( !result )
{
v2 = _errno_location();
error(1, *v2, "malloc");
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
void *result; // rax
int *v2; // rax

result = malloc(a1);
if ( !result )
{
v2 = _errno_location();
func0(1, *v2, "malloc");
}
return result;
}

original_code:
void *s_malloc(size_t size) {
    void *ptr;
    if (!(ptr = malloc(size)))
        error(1, (*__errno_location ()), "malloc");
    return ptr;
}

canonical_original_code:
void *s_malloc(unsigned long size) {
    void *ptr;
    if (!(ptr = malloc(size)))
        error(1, (*__errno_location ()), "malloc");
    return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


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

result = realloc(a1, a2);
if ( !result )
{
v3 = _errno_location();
error(1, *v3, "realloc");
}
return result;
}

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

result = realloc(a1, a2);
if ( !result )
{
v3 = _errno_location();
func0(1, *v3, "realloc");
}
return result;
}

original_code:
void *s_realloc(void *ptr, size_t size) {
    if (!(ptr = realloc(ptr, size)))
        error(1, (*__errno_location ()), "realloc");
    return ptr;
}

canonical_original_code:
void *s_realloc(void *ptr, unsigned long size) {
    if (!(ptr = realloc(ptr, size)))
        error(1, (*__errno_location ()), "realloc");
    return ptr;
}


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

v4 = a2;
v5 = *a2;
if ( a3 )
{
if ( (_BYTE)v5 )
{
v6 = *_ctype_tolower_loc();
while ( v6[v5] == v6[(char)*a1] )
{
v5 = (char)*++v4;
++a1;
if ( !(_BYTE)v5 )
goto LABEL_7;
}
LABEL_6:
LOBYTE(v5) = *v4;
}
LABEL_7:
result = (_BYTE)v5 == 0;
}
else
{
while ( (_BYTE)v5 )
{
if ( *a1 != (_BYTE)v5 )
goto LABEL_6;
LOBYTE(v5) = *++v4;
++a1;
}
result = 1LL;
}
return result;
}

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

v4 = a2;
v5 = *a2;
if ( a3 )
{
if ( (_BYTE)v5 )
{
v6 = *_ctype_tolower_loc();
while ( v6[v5] == v6[(char)*a1] )
{
v5 = (char)*++v4;
++a1;
if ( !(_BYTE)v5 )
goto LABEL_7;
}
LABEL_6:
LOBYTE(v5) = *v4;
}
LABEL_7:
result = (_BYTE)v5 == 0;
}
else
{
while ( (_BYTE)v5 )
{
if ( *a1 != (_BYTE)v5 )
goto LABEL_6;
LOBYTE(v5) = *++v4;
++a1;
}
result = 1LL;
}
return result;
}

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

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


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);
}


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)
{
__int64 v3; // rax
int v4; // eax
const char *v5; // rdi
void *v6; // r12
void *v7; // rax
char *v8; // rax
__int64 result; // rax
char *v10; // rax

v3 = *a2;
*(&options + 1) = 0LL;
program_name = v3;
*((_QWORD *)&options + 6) = 0LL;
*((_QWORD *)&options + 3) = &jaro_winkler_distance;
*((_QWORD *)&options + 4) = LC3;
*((_QWORD *)&options + 5) = &unk_2E8;
options = 0LL;
while ( 1 )
{
v4 = _posix_getopt((unsigned int)a1, a2, "chisvl:m:t:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'c':
LODWORD(options) = 1;
break;
case 'h':
print_usage();
exit(0);
case 'i':
DWORD1(options) = 1;
break;
case 'l':
*((_QWORD *)&options + 1) = optarg;
break;
case 'm':
*((_QWORD *)&options + 2) = 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);
}
}
v5 = (const char *)*((_QWORD *)&options + 1);
if ( v5 && *((_QWORD *)&options + 2) )
{
v6 = dlopen(v5, 1);
if ( !v6 )
{
v10 = dlerror();
error(1, 0, "dlopen: %s\n", v10);
}
v7 = dlsym(v6, *((const char **)&options + 2));
*((_QWORD *)&options + 3) = v7;
if ( !v7 )
{
v8 = dlerror();
error(1, 0, "dlsym: %s\n", v8);
}
}
result = optind;
if ( (_DWORD)options && optind + 1 < a1 )
{
++optind;
result = (int)result + 1;
goto LABEL_23;
}
if ( a1 > optind )
{
LABEL_23:
result = a2[result];
*((_QWORD *)&options + 5) = result;
}
return result;
}

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

v3 = *a2;
*(&options + 1) = 0LL;
program_name = v3;
*((_QWORD *)&options + 6) = 0LL;
*((_QWORD *)&options + 3) = &jaro_winkler_distance;
*((_QWORD *)&options + 4) = LC3;
*((_QWORD *)&options + 5) = &unk_2E8;
options = 0LL;
while ( 1 )
{
v4 = _posix_getopt((unsigned int)a1, a2, "chisvl:m:t:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'c':
LODWORD(options) = 1;
break;
case 'h':
func0();
exit(0);
case 'i':
DWORD1(options) = 1;
break;
case 'l':
*((_QWORD *)&options + 1) = optarg;
break;
case 'm':
*((_QWORD *)&options + 2) = 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);
}
}
v5 = (const char *)*((_QWORD *)&options + 1);
if ( v5 && *((_QWORD *)&options + 2) )
{
v6 = dlopen(v5, 1);
if ( !v6 )
{
v10 = dlerror();
error(1, 0, "dlopen: %s\n", v10);
}
v7 = dlsym(v6, *((const char **)&options + 2));
*((_QWORD *)&options + 3) = v7;
if ( !v7 )
{
v8 = dlerror();
error(1, 0, "dlsym: %s\n", v8);
}
}
result = optind;
if ( (_DWORD)options && optind + 1 < a1 )
{
++optind;
result = (int)result + 1;
goto LABEL_23;
}
if ( a1 > optind )
{
LABEL_23:
result = a2[result];
*((_QWORD *)&options + 5) = result;
}
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:
handle
void *


min

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;
}

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

variable_types: y int


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: i int

variable_types: j int

variable_types: l int

variable_types: m int

variable_types: t int

variable_types: al int

variable_types:
sflags
int [-1]

variable_types:
aflags
int [-1]

variable_types: range int

variable_types: dw 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


repo: mihaimaruseac/dphcar

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

v8 = *a2;
if ( a5 )
{
*a2 = a1[7] + v8;
*a3 += a1[8];
result = a1[9];
}
else
{
*a2 = a1[4] + v8;
*a3 += a1[5];
result = a1[6];
}
*a4 += result;
for ( i = 0LL; a1[2] > i; result = do_count(*(_QWORD *)(v11 + 8), a2, a3, a4, a5) )
v11 = *a1 + 16 * i++;
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; // rax
_QWORD *v1; // r12

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

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

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

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(const void **a1, int *a2, __int64 a3, char *a4, char *a5, char *a6)
{
return do_record_new_rule(a1, a2, a3, 1, a4, a5, a6);
}

canonical_decompiled_code:
__int64 __fastcall func6(const void **a1, int *a2, __int64 a3, char *a4, char *a5, char *a6)
{
return func4(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(const void **a1, int *a2, __int64 a3, char *a4, char *a5, char *a6)
{
return do_record_new_rule(a1, a2, a3, 0, a4, a5, a6);
}

canonical_decompiled_code:
__int64 __fastcall func7(const void **a1, int *a2, __int64 a3, char *a4, char *a5, char *a6)
{
return func4(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
int v6; // eax
size_t v7; // rdx
const void *v8; // rsi
_QWORD *v9; // rcx
int v10[10]; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int *a2, __int64 a3)
{
__int64 result; // rax
int v6; // eax
size_t v7; // rdx
const void *v8; // rsi
_QWORD *v9; // rcx
int v10[10]; // [rsp+0h] [rbp-28h] BYREF

if ( !a3 )
return *(unsigned int *)(a1 + 24);
v6 = *a2;
v7 = *(_QWORD *)(a1 + 16);
v8 = *(const void **)a1;
v10[0] = v6;
v9 = bsearch(v10, v8, v7, 0x10uLL, (__compar_fn_t)cmp);
result = 0LL;
if ( v9 )
result = func8(v9[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
__int64 v2; // rax

if ( *(_QWORD *)(a1 + 16) )
{
v1 = 0LL;
do
{
v2 = v1++;
free_itstree(*(_QWORD *)(*(_QWORD *)a1 + 16 * v2 + 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, __int64 *a2, _QWORD *a3, _QWORD *a4)
{
return do_count(a1, a2, a3, a4, 0);
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1, __int64 *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, __int64 *a2, _QWORD *a3, _QWORD *a4)
{
return do_count(a1, a2, a3, a4, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1, __int64 *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; // edx
_QWORD *v2; // rax
int v3; // ebx
_QWORD *v4; // rbp
__int64 v5; // r12
int v6; // eax

v1 = *(_DWORD *)(a1 + 16);
if ( v1 <= 0 )
return 1LL;
v2 = *(_QWORD **)(a1 + 24);
v3 = 0;
v4 = v2 + 1;
v5 = (__int64)&v2[(unsigned int)(v1 - 1) + 1];
while ( 1 )
{
v6 = fpt_get_height(*v2);
if ( v3 < v6 )
v3 = v6;
v2 = v4;
if ( v4 == (_QWORD *)v5 )
break;
++v4;
}
return (unsigned int)(v3 + 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)
{
unsigned int v1; // er12
int v2; // edx
_QWORD *v3; // rax
_QWORD *v4; // rbx
__int64 v5; // rbp

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

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:
__int64 __fastcall fpt_node_print(unsigned int *a1, int a2)
{
__int64 v4; // rbp
const void *v5; // rsi
unsigned int v6; // er12
__int64 v7; // rbp
__int64 result; // rax
__int64 v9; // rdi

if ( a2 )
printf("%*c", 2 * a2, 32LL);
v4 = 0LL;
printf("%p %d %d %p %d <", a1, *a1, a1[1], *((const void **)a1 + 1), a1[4]);
if ( (int)a1[4] > 0 )
{
do
{
v5 = *(const void **)(*((_QWORD *)a1 + 3) + 8 * v4++);
printf("%p ", v5);
}
while ( (int)a1[4] > (int)v4 );
}
v6 = a2 + 1;
v7 = 0LL;
printf("> %d\n", a1[8]);
result = a1[4];
if ( (int)result > 0 )
{
do
{
v9 = *(_QWORD *)(*((_QWORD *)a1 + 3) + 8 * v7++);
result = fpt_node_print(v9, v6);
}
while ( (int)a1[4] > (int)v7 );
}
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; // rax
_DWORD *v1; // r12

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

canonical_decompiled_code:
_DWORD *func4()
{
_DWORD *v0; // rax
_DWORD *v1; // r12

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

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:
__int64 __fastcall fpt_tree_print(__int64 a1)
{
return fpt_node_print(*(unsigned int **)(a1 + 24), 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return func3(*(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; } *


decompiled_code:
void __fastcall store_item(__int64 a1, __int64 a2, double a3, double a4, double a5)
{
unsigned __int64 v6; // rsi
unsigned __int64 v7; // rax
__int64 v8; // rax
size_t v9; // rsi

v6 = *(_QWORD *)(a1 + 8);
v7 = *(_QWORD *)(a1 + 16);
if ( v6 < v7 )
{
store_item_at(a1, v6, a2, a3, a4, a5);
v9 = *(_QWORD *)(a1 + 8) + 1LL;
*(_QWORD *)(a1 + 8) = v9;
if ( *(_QWORD *)(a1 + 16) != v9 )
return;
LABEL_6:
qsort(*(void **)a1, v9, 0x20uLL, reservoir_cmp);
return;
}
v8 = *(_QWORD *)a1 + 32 * v7 - 32;
if ( a5 < *(double *)(v8 + 24) )
{
(*(void (__fastcall **)(_QWORD))(a1 + 40))(*(_QWORD *)v8);
store_item_at(a1, *(_QWORD *)(a1 + 16) - 1LL, a2, a3, a4, a5);
v9 = *(_QWORD *)(a1 + 8);
if ( *(_QWORD *)(a1 + 16) == v9 )
goto LABEL_6;
}
}

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
__int64 v2; // rax

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

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

if ( *((_QWORD *)ptr + 1) )
{
v1 = 0LL;
do
{
v2 = v1++;
(*((void (__fastcall **)(_QWORD))ptr + 5))(*(_QWORD *)(*(_QWORD *)ptr + 32 * v2));
}
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 v4; // xmm2_8
double v5; // [rsp+0h] [rbp-28h]

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

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, struct drand48_data *a3, double a4)
{
double v4; // xmm2_8
double v5; // [rsp+0h] [rbp-28h]

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

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 v4; // xmm0_8
double v5; // xmm2_8
double v6; // [rsp+0h] [rbp-28h]

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

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

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

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:
// attributes: thunk
void free_reservoir_iterator(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func7(void *ptr)
{
free(ptr);
}

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;
}


laplace

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

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

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

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

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; // er13
unsigned int v7; // er15
unsigned int v8; // ebx

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

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; // r12

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

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

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)
{
__int64 v2; // r8
_QWORD *v3; // rax
__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 = 0LL;
v3 = (_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
v4 = (__int64)&v3[-a2 - 1];
do
v2 += *v3--;
while ( v3 != (_QWORD *)v4 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
__int64 v2; // r8
_QWORD *v3; // rax
__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 = 0LL;
v3 = (_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (int)a2);
v4 = (__int64)&v3[-a2 - 1];
do
v2 += *v3--;
while ( v3 != (_QWORD *)v4 );
return v2;
}

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_6:
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_6;
}
return result;
}

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

if ( !a2 )
{
LABEL_6:
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_6;
}
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)
{
__int64 v2; // rax
const char **v3; // r12
const char **v4; // rbp

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

canonical_decompiled_code:
const char **__fastcall func5(char *s1, int *a2)
{
__int64 v2; // rax
const char **v3; // r12
const char **v4; // rbp

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

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


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)
{
__int64 v2; // rax
__int64 result; // rax

if ( a2 && (v2 = (unsigned int)*(char *)(a1 - 1), *(_DWORD *)a2 >= (unsigned int)v2) )
result = *(_QWORD *)(a2 + 8) + 24 * v2;
else
result = 0LL;
return result;
}

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

if ( a2 && (v2 = (unsigned int)*(char *)(a1 - 1), *(_DWORD *)a2 >= (unsigned int)v2) )
result = *(_QWORD *)(a2 + 8) + 24 * v2;
else
result = 0LL;
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; // rax
char *v5; // r14
const char **v6; // rax
unsigned int v7; // er8
int v9; // ebx
const char **v10; // rax
const char **v11; // rax
int v12; // eax
__int64 v13; // r14
__int64 v14; // rbx
__int64 v15; // r13
const char **v16; // rax
int *v17; // [rsp+8h] [rbp-30h] BYREF

v4 = vfm_parse_entry(s1);
vfm_errno = 0;
if ( !v4 )
{
v11 = vfm_name2symb(s1, (int *)(a3 + 48));
*a2 = v11;
if ( v11 )
return 0;
v12 = *(_DWORD *)(a3 + 32);
if ( v12 > 0 )
{
v13 = *(_QWORD *)(a3 + 40);
v14 = 1LL;
v15 = (unsigned int)(v12 + 1);
do
{
v16 = vfm_name2symb(s1, (int *)(*(_QWORD *)(v13 + 8 * v14 - 8) + 48LL));
v7 = v14;
*a2 = v16;
if ( v16 )
return v7;
}
while ( v15 != ++v14 );
}
LABEL_14:
vfm_errno = -1;
return (unsigned int)-1;
}
v5 = v4;
if ( !strcmp(*(const char **)a3, s1) )
{
v6 = vfm_name2symb(v5, (int *)(a3 + 48));
*a2 = v6;
if ( v6 )
return 0;
}
v9 = vfm_name2dict(s1, &v17, a3);
if ( v9 < 0 )
goto LABEL_14;
v10 = vfm_name2symb(v5, v17);
*a2 = v10;
if ( !v10 )
goto LABEL_14;
return (unsigned int)(v9 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s1, const char ***a2, __int64 a3)
{
char *v4; // rax
char *v5; // r14
const char **v6; // rax
unsigned int v7; // er8
int v9; // ebx
const char **v10; // rax
const char **v11; // rax
int v12; // eax
__int64 v13; // r14
__int64 v14; // rbx
__int64 v15; // r13
const char **v16; // rax
int *v17; // [rsp+8h] [rbp-30h] BYREF

v4 = vfm_parse_entry(s1);
vfm_errno = 0;
if ( !v4 )
{
v11 = func5(s1, (int *)(a3 + 48));
*a2 = v11;
if ( v11 )
return 0;
v12 = *(_DWORD *)(a3 + 32);
if ( v12 > 0 )
{
v13 = *(_QWORD *)(a3 + 40);
v14 = 1LL;
v15 = (unsigned int)(v12 + 1);
do
{
v16 = func5(s1, (int *)(*(_QWORD *)(v13 + 8 * v14 - 8) + 48LL));
v7 = v14;
*a2 = v16;
if ( v16 )
return v7;
}
while ( v15 != ++v14 );
}
LABEL_14:
vfm_errno = -1;
return (unsigned int)-1;
}
v5 = v4;
if ( !strcmp(*(const char **)a3, s1) )
{
v6 = func5(v5, (int *)(a3 + 48));
*a2 = v6;
if ( v6 )
return 0;
}
v9 = func6(s1, &v17, a3);
if ( v9 < 0 )
goto LABEL_14;
v10 = func5(v5, v17);
*a2 = v10;
if ( !v10 )
goto LABEL_14;
return (unsigned int)(v9 + 1);
}

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:
// attributes: thunk
void chunk_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

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; }


decompiled_code:
__int64 __fastcall chunk_length(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 216);
if ( result )
{
v2 = a1 + 8 * result;
result = 0LL;
do
{
v3 = *(_QWORD *)(a1 + 192);
a1 += 8LL;
result += *(unsigned __int8 *)(v3 + 1);
}
while ( a1 != v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 216);
if ( result )
{
v2 = a1 + 8 * result;
result = 0LL;
do
{
v3 = *(_QWORD *)(a1 + 192);
a1 += 8LL;
result += *(unsigned __int8 *)(v3 + 1);
}
while ( a1 != v2 );
}
return result;
}

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
unsigned __int64 v5; // rdx

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

canonical_decompiled_code:
double __fastcall func3(__int64 a1)
{
__int64 v1; // rax
double v2; // xmm0_8
__int64 v3; // rax
unsigned __int64 v5; // rdx

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

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;
}


decompiled_code:
__int64 __fastcall chunk_degree_of_MF(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 216);
if ( result )
{
v2 = a1 + 8 * result;
result = 0LL;
do
{
v3 = *(_QWORD *)(a1 + 192);
a1 += 8LL;
result += *(int *)(v3 + 4);
}
while ( a1 != v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 216);
if ( result )
{
v2 = a1 + 8 * result;
result = 0LL;
do
{
v3 = *(_QWORD *)(a1 + 192);
a1 += 8LL;
result += *(int *)(v3 + 4);
}
while ( a1 != v2 );
}
return result;
}

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 *v2; // rax
unsigned __int64 i; // rdi

v2 = (unsigned __int64 *)&unk_388;
for ( i = 524309LL; a1 >= i; i = *v2++ )
{
if ( v2 == (unsigned __int64 *)&unk_3E0 )
return (unsigned int)a1;
}
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
unsigned __int64 *v2; // rax
unsigned __int64 i; // rdi

v2 = (unsigned __int64 *)&unk_388;
for ( i = 524309LL; a1 >= i; i = *v2++ )
{
if ( v2 == (unsigned __int64 *)&unk_3E0 )
return (unsigned int)a1;
}
return (unsigned int)i;
}

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; // r12
void *v1; // rax
_QWORD *result; // rax

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

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *v0; // r12
void *v1; // rax
_QWORD *result; // rax

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

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(__int64 a1, char *a2, size_t a3)
{
unsigned __int64 v4; // rax
_QWORD *v5; // rcx
__int64 v6; // r12

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

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

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

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)
{
bool v2; // zf
_QWORD *v3; // rdi
unsigned __int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rdi

v2 = *((_QWORD *)ptr + 1) == 0LL;
v3 = *(_QWORD **)ptr;
if ( !v2 )
{
v4 = 0LL;
do
{
v5 = v3[v4];
if ( v5 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 72);
pool_free(v6);
}
while ( v5 );
v3 = *(_QWORD **)ptr;
}
++v4;
}
while ( *((_QWORD *)ptr + 1) > v4 );
}
free(v3);
free(ptr);
}

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

v2 = *((_QWORD *)ptr + 1) == 0LL;
v3 = *(_QWORD **)ptr;
if ( !v2 )
{
v4 = 0LL;
do
{
v5 = v3[v4];
if ( v5 )
{
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 72);
pool_free(v6);
}
while ( v5 );
v3 = *(_QWORD **)ptr;
}
++v4;
}
while ( *((_QWORD *)ptr + 1) > v4 );
}
free(v3);
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; // rax
__int64 v3; // r8
const unsigned __int16 *v4; // rdx
__int64 v5; // rax

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

canonical_decompiled_code:
_BOOL8 __fastcall func2(char *a1)
{
char v1; // bp
const unsigned __int16 **v2; // rax
__int64 v3; // r8
const unsigned __int16 *v4; // rdx
__int64 v5; // rax

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

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; }