decompiled_code:
__int64 __fastcall probe_parents(_QWORD *a1, int a2)
{
__int64 v2; // r12
struct stat v4; // [rsp+0h] [rbp-138h] BYREF
struct stat v5; // [rsp+90h] [rbp-A8h] BYREF

debug("no context claimed current dir: walking up the tree");
stat("/", &v4);
while ( 1 )
{
v2 = probe_all(a1, a2);
if ( v2 )
break;
stat(".", &v5);
if ( v4.st_dev == v5.st_dev && v4.st_ino == v5.st_ino || chdir("..") == -1 )
return v2;
}
debug("found a context");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, int a2)
{
__int64 v2; // r12
struct stat v4; // [rsp+0h] [rbp-138h] BYREF
struct stat v5; // [rsp+90h] [rbp-A8h] BYREF

debug("no context claimed current dir: walking up the tree");
stat("/", &v4);
while ( 1 )
{
v2 = func3(a1, a2);
if ( v2 )
break;
stat(".", &v5);
if ( v4.st_dev == v5.st_dev && v4.st_ino == v5.st_ino || chdir("..") == -1 )
return v2;
}
debug("found a context");
return v2;
}

original_code:
vccontext_t* probe_parents(vccontext_t** contexts, int num_contexts)
{
    vccontext_t* context;
    struct stat rootdir;
    struct stat curdir;
    debug("no context claimed current dir: walking up the tree");
    stat("/", &rootdir);
    while (1) {
        context = probe_all(contexts, num_contexts);
        if (context != ((void *)0)) {
            debug("found a context");
            return context;
        }
        stat(".", &curdir);
        int isroot = (rootdir.st_dev == curdir.st_dev &&
                      rootdir.st_ino == curdir.st_ino);
        if (isroot || (-1 == chdir(".."))) {
            return ((void *)0);
        }
    }
}

canonical_original_code:
struct vccontext_t *probe_parents(struct vccontext_t **contexts, int num_contexts)
{
    struct vccontext_t *context;
    struct stat rootdir;
    struct stat curdir;
    debug("no context claimed current dir: walking up the tree");
    stat("/", &rootdir);
    while (1) {
        context = probe_all(contexts, num_contexts);
        if (context != ((void *)0)) {
            debug("found a context");
            return context;
        }
        stat(".", &curdir);
        int isroot = (rootdir.st_dev == curdir.st_dev &&
                      rootdir.st_ino == curdir.st_ino);
        if (isroot || (-1 == chdir(".."))) {
            return ((void *)0);
        }
    }
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: isroot int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct result_t { char *branch; char *revision; int unknown; int modified; }


main

canonical_name: main

repo: avsej/vcprompt

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
const char **v4; // rbp
__int64 v5; // rax
__int64 v6; // r12
__int128 v8; // [rsp+0h] [rbp-68h] BYREF
__int128 v9; // [rsp+10h] [rbp-58h]
__int64 v10[9]; // [rsp+20h] [rbp-48h] BYREF

*(_QWORD *)&v8 = 0LL;
*(_QWORD *)&v9 = 0x100000001LL;
*((_QWORD *)&v8 + 1) = "[%n:%b%r%m%u] ";
*((_QWORD *)&v9 + 1) = 1LL;
parse_args(argc, (char **)argv, (__int64)&v8);
parse_format((__int64)&v8);
set_options(&v8);
v10[0] = get_cvs_context(&v8);
v10[1] = get_git_context(&v8);
v10[2] = get_hg_context(&v8);
v10[3] = get_svn_context(&v8);
v10[4] = get_bzr_context(&v8);
v3 = probe_parents(v10, 5);
if ( v3 )
{
v4 = (const char **)v3;
v5 = (*(__int64 (__fastcall **)(__int64))(v3 + 24))(v3);
v6 = v5;
if ( v5 )
{
print_result(v4, (__int64)&v8, v5);
free_result(v6);
if ( (_DWORD)v8 )
putc(10, stdout);
}
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
    options_t options = { 0,
                          "[%n:%b%r%m%u] ",
                          1,
                          1,
                          1,
    };
    parse_args(argc, argv, &options);
    parse_format(&options);
    set_options(&options);
    vccontext_t* contexts[] = {
        get_cvs_context(&options),
        get_git_context(&options),
        get_hg_context(&options),
        get_svn_context(&options),
        get_bzr_context(&options),
    };
    int num_contexts = sizeof(contexts) / sizeof(vccontext_t*);
    result_t* result = ((void *)0);
    vccontext_t* context = ((void *)0);
    context = probe_parents(contexts, num_contexts);
    if (context == ((void *)0)) {
        return 0;
    }
    result = context->get_info(context);
    if (result != ((void *)0)) {
        print_result(context, &options, result);
        free_result(result);
        if (options.debug)
            putc('\n', stdout);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct options_t options = { 0,
                          "[%n:%b%r%m%u] ",
                          1,
                          1,
                          1,
    };
    parse_args(argc, argv, &options);
    parse_format(&options);
    set_options(&options);
    struct vccontext_t *contexts[] = {
        get_cvs_context(&options),
        get_git_context(&options),
        get_hg_context(&options),
        get_svn_context(&options),
        get_bzr_context(&options),
    };
    int num_contexts = sizeof(contexts) / sizeof(vccontext_t*);
    struct result_t *result = ((void *)0);
    struct vccontext_t *context = ((void *)0);
    context = probe_parents(contexts, num_contexts);
    if (context == ((void *)0)) {
        return 0;
    }
    result = context->get_info(context);
    if (result != ((void *)0)) {
        print_result(context, &options, result);
        free_result(result);
        if (options.debug)
            putc('\n', stdout);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t { char *branch; char *revision; int unknown; int modified; } *(*get_info)(struct vccontext_t *); } *[]

return_type: int

user_defined_types: struct vccontext_t { char *name; struct options_t *options; int (*probe)(struct vccontext_t *); struct result_t *(*get_info)(struct vccontext_t *); }


decompiled_code:
__int64 svn_probe()
{
return isdir(".svn");
}

canonical_decompiled_code:
__int64 func1()
{
return isdir(".svn");
}

original_code:
static int
svn_probe(vccontext_t* context)
{
    return isdir(".svn");
}

canonical_original_code:
static int svn_probe(struct vccontext_t *context)
{
    return isdir(".svn");
}

variable_types: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t { char *branch; char *revision; int unknown; int modified; } *(*get_info)(struct vccontext_t *); } *


decompiled_code:
__int64 __fastcall get_svn_context(__int64 a1)
{
return init_context("svn", a1, svn_probe, svn_get_info);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return init_context("svn", a1, svn_probe, svn_get_info);
}

original_code:
vccontext_t* get_svn_context(options_t* options)
{
    return init_context("svn", options, svn_probe, svn_get_info);
}

canonical_original_code:
struct vccontext_t *get_svn_context(struct options_t *options)
{
    return init_context("svn", options, svn_probe, svn_get_info);
}

variable_types:
options
struct options_t *

return_type: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t { char *branch; char *revision; int unknown; int modified; } *(*get_info)(struct vccontext_t *); } *


decompiled_code:
char **cvs_get_info()
{
char **v0; // r12
char **result; // rax
char v2; // [rsp+0h] [rbp-408h] BYREF
char s[1031]; // [rsp+1h] [rbp-407h] BYREF

v0 = (char **)init_result();
if ( (unsigned int)read_first_line("CVS/Tag", &v2, 1024LL) )
{
debug("read first line of CVS/Tag: '%s'", &v2);
if ( v2 == 84 )
*v0 = strdup(s);
else
*v0 = "(unknown)";
result = v0;
}
else
{
debug("unable to read CVS/Tag: assuming trunk");
*v0 = "trunk";
result = v0;
}
return result;
}

canonical_decompiled_code:
char **func0()
{
char **v0; // r12
char **result; // rax
char v2; // [rsp+0h] [rbp-408h] BYREF
char s[1031]; // [rsp+1h] [rbp-407h] BYREF

v0 = (char **)init_result();
if ( (unsigned int)read_first_line("CVS/Tag", &v2, 1024LL) )
{
debug("read first line of CVS/Tag: '%s'", &v2);
if ( v2 == 84 )
*v0 = strdup(s);
else
*v0 = "(unknown)";
result = v0;
}
else
{
debug("unable to read CVS/Tag: assuming trunk");
*v0 = "trunk";
result = v0;
}
return result;
}

original_code:
static result_t*
cvs_get_info(vccontext_t* context)
{
    result_t* result = init_result();
    char buf[1024];
    if (!read_first_line("CVS/Tag", buf, 1024)) {
        debug("unable to read CVS/Tag: assuming trunk");
        result->branch = "trunk";
    }
    else {
        debug("read first line of CVS/Tag: '%s'", buf);
        if (buf[0] == 'T') {
            result->branch = strdup(buf+1);
        }
        else {
            result->branch = "(unknown)";
        }
    }
    return result;
}

canonical_original_code:
static struct result_t *cvs_get_info(struct vccontext_t *context)
{
    struct result_t *result = init_result();
    char buf[1024];
    if (!read_first_line("CVS/Tag", buf, 1024)) {
        debug("unable to read CVS/Tag: assuming trunk");
        result->branch = "trunk";
    }
    else {
        debug("read first line of CVS/Tag: '%s'", buf);
        if (buf[0] == 'T') {
            result->branch = strdup(buf+1);
        }
        else {
            result->branch = "(unknown)";
        }
    }
    return result;
}

variable_types: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t *(*get_info)(struct vccontext_t *); } *

variable_types: struct result_t { char *branch; char *revision; int unknown; int modified; } *

variable_types:
buf
char [1024]


decompiled_code:
__int64 cvs_probe()
{
return isdir("CVS");
}

canonical_decompiled_code:
__int64 func1()
{
return isdir("CVS");
}

original_code:
static int
cvs_probe(vccontext_t* context)
{
    return isdir("CVS");
}

canonical_original_code:
static int cvs_probe(struct vccontext_t *context)
{
    return isdir("CVS");
}

user_defined_types: struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; }


decompiled_code:
__int64 __fastcall get_cvs_context(__int64 a1)
{
return init_context("cvs", a1, cvs_probe, cvs_get_info);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return init_context("cvs", a1, cvs_probe, cvs_get_info);
}

original_code:
vccontext_t* get_cvs_context(options_t* options)
{
    return init_context("cvs", options, cvs_probe, cvs_get_info);
}

canonical_original_code:
struct vccontext_t *get_cvs_context(struct options_t *options)
{
    return init_context("cvs", options, cvs_probe, cvs_get_info);
}


decompiled_code:
__int64 git_get_info()
{
__int64 v0; // r12
char *v2; // rax
__int64 v3[2]; // [rsp+0h] [rbp-408h] BYREF
char s[1016]; // [rsp+10h] [rbp-3F8h] BYREF

v0 = init_result();
if ( !(unsigned int)read_first_line(".git/HEAD", v3, 1024LL) )
{
v0 = 0LL;
debug("unable to read .git/HEAD: assuming not a git repo");
return v0;
}
if ( !(v3[0] ^ 0x666572203A666572LL | v3[1] ^ 0x2F73646165682F73LL) )
{
debug("read a head ref from .git/HEAD: '%s'", (const char *)v3);
*(_QWORD *)v0 = strdup(s);
return v0;
}
debug(".git/HEAD doesn't look like a head ref: unknown branch");
v2 = (char *)malloc(9uLL);
*(_QWORD *)v0 = v2;
strncpy(v2, (const char *)v3, 8uLL);
*(_BYTE *)(*(_QWORD *)v0 + 8LL) = 0;
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r12
char *v2; // rax
__int64 v3[2]; // [rsp+0h] [rbp-408h] BYREF
char s[1016]; // [rsp+10h] [rbp-3F8h] BYREF

v0 = init_result();
if ( !(unsigned int)read_first_line(".git/HEAD", v3, 1024LL) )
{
v0 = 0LL;
debug("unable to read .git/HEAD: assuming not a git repo");
return v0;
}
if ( !(v3[0] ^ 0x666572203A666572LL | v3[1] ^ 0x2F73646165682F73LL) )
{
debug("read a head ref from .git/HEAD: '%s'", (const char *)v3);
*(_QWORD *)v0 = strdup(s);
return v0;
}
debug(".git/HEAD doesn't look like a head ref: unknown branch");
v2 = (char *)malloc(9uLL);
*(_QWORD *)v0 = v2;
strncpy(v2, (const char *)v3, 8uLL);
*(_BYTE *)(*(_QWORD *)v0 + 8LL) = 0;
return v0;
}

original_code:
static result_t*
git_get_info(vccontext_t* context)
{
    result_t* result = init_result();
    char buf[1024];
    if (!read_first_line(".git/HEAD", buf, 1024)) {
        debug("unable to read .git/HEAD: assuming not a git repo");
        return ((void *)0);
    }
    else {
        char* prefix = "ref: refs/heads/";
        int prefixlen = strlen(prefix);
        if (strncmp(prefix, buf, prefixlen) == 0) {
            debug("read a head ref from .git/HEAD: '%s'", buf);
            result->branch = strdup(buf+prefixlen);
        }
        else {
            debug(".git/HEAD doesn't look like a head ref: unknown branch");
            result->branch = malloc(sizeof(char) * 9);
            strncpy(result->branch, buf, 8);
            result->branch[8] = '\0';
        }
    }
    return result;
}

canonical_original_code:
static struct result_t *git_get_info(struct vccontext_t *context)
{
    struct result_t *result = init_result();
    char buf[1024];
    if (!read_first_line(".git/HEAD", buf, 1024)) {
        debug("unable to read .git/HEAD: assuming not a git repo");
        return ((void *)0);
    }
    else {
        char *prefix = "ref: refs/heads/";
        int prefixlen = strlen(prefix);
        if (strncmp(prefix, buf, prefixlen) == 0) {
            debug("read a head ref from .git/HEAD: '%s'", buf);
            result->branch = strdup(buf+prefixlen);
        }
        else {
            debug(".git/HEAD doesn't look like a head ref: unknown branch");
            result->branch = malloc(sizeof(char) * 9);
            strncpy(result->branch, buf, 8);
            result->branch[8] = '\0';
        }
    }
    return result;
}

variable_types:
prefix
char *

variable_types: prefixlen int

return_type: struct result_t { char *branch; char *revision; int unknown; int modified; } *


decompiled_code:
__int64 git_probe()
{
return isdir(".git");
}

canonical_decompiled_code:
__int64 func1()
{
return isdir(".git");
}

original_code:
static int
git_probe(vccontext_t* context)
{
    return isdir(".git");
}

canonical_original_code:
static int git_probe(struct vccontext_t *context)
{
    return isdir(".git");
}


decompiled_code:
__int64 __fastcall get_git_context(__int64 a1)
{
return init_context("git", a1, git_probe, git_get_info);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return init_context("git", a1, git_probe, git_get_info);
}

original_code:
vccontext_t* get_git_context(options_t* options)
{
    return init_context("git", options, git_probe, git_get_info);
}

canonical_original_code:
struct vccontext_t *get_git_context(struct options_t *options)
{
    return init_context("git", options, git_probe, git_get_info);
}


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

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

original_code:
result_t* init_result()
{
    return (result_t*) calloc(1, sizeof(result_t));
}

canonical_original_code:
struct result_t *init_result()
{
    return (struct result_t *) calloc(1, sizeof(result_t));
}


decompiled_code:
void __fastcall set_options(__int64 a1)
{
options = a1;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
options = a1;
}

original_code:
void set_options(options_t* options)
{
    _options = options;
}

canonical_original_code:
void set_options(struct options_t *options)
{
    _options = options;
}

variable_types: struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *

return_type: void


decompiled_code:
// attributes: thunk
void free_context(void *ptr)
{
free(ptr);
}

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

original_code:
void free_context(vccontext_t* context)
{
    free(context);
}

canonical_original_code:
void free_context(struct vccontext_t *context)
{
    free(context);
}


decompiled_code:
// attributes: thunk
void __fastcall free_result(void *ptr)
{
free_context(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
func2(ptr);
}

original_code:
void free_result(result_t* result)
{
    free(result);
}

canonical_original_code:
void free_result(struct result_t *result)
{
    free(result);
}


debug

decompiled_code:
int debug(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = *(_DWORD *)options;
if ( *(_DWORD *)options )
{
va_start(arg, format);
fwrite("vcprompt: debug: ", 1uLL, 0x11uLL, stdout);
vfprintf(stdout, format, arg);
result = fputc(10, stdout);
}
return result;
}

canonical_decompiled_code:
int func4(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = *(_DWORD *)options;
if ( *(_DWORD *)options )
{
va_start(arg, format);
fwrite("vcprompt: debug: ", 1uLL, 0x11uLL, stdout);
vfprintf(stdout, format, arg);
result = fputc(10, stdout);
}
return result;
}

original_code:
void debug(char* fmt, ...)
{
    va_list args;
    if (!_options->debug)
        return;
    __builtin_va_start(args,fmt);
    fputs("vcprompt: debug: ", stdout);
    vfprintf(stdout, fmt, args);
    fputc('\n', stdout);
    __builtin_va_end(args);
}

canonical_original_code:
void debug(char *fmt, ...)
{
    __builtin_va_list args;
    if (!_options->debug)
        return;
    __builtin_va_start(args,fmt);
    fputs("vcprompt: debug: ", stdout);
    vfprintf(stdout, fmt, args);
    fputc('\n', stdout);
    __builtin_va_end(args);
}

variable_types:
fmt
char *


isdir

decompiled_code:
__int64 __fastcall isdir(const char *a1)
{
__int64 result; // rax
int *v2; // rax
char *v3; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v4) < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
debug("failed to stat() '%s': %s", a1, v3);
result = 0LL;
}
else if ( (v4.st_mode & 0xF000) == 0x4000 )
{
result = 1LL;
}
else
{
debug("'%s' not a directory", a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
__int64 result; // rax
int *v2; // rax
char *v3; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v4) < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
func4("failed to stat() '%s': %s", a1, v3);
result = 0LL;
}
else if ( (v4.st_mode & 0xF000) == 0x4000 )
{
result = 1LL;
}
else
{
func4("'%s' not a directory", a1);
result = 0LL;
}
return result;
}

original_code:
int isdir(char* name)
{
    struct stat statbuf;
    if (stat(name, &statbuf) < 0) {
        debug("failed to stat() '%s': %s", name, strerror((*__errno_location ())));
        return 0;
    }
    if (!((((statbuf.st_mode)) & 0170000) == (0040000))) {
        debug("'%s' not a directory", name);
        return 0;
    }
    return 1;
}

canonical_original_code:
int isdir(char *name)
{
    struct stat statbuf;
    if (stat(name, &statbuf) < 0) {
        debug("failed to stat() '%s': %s", name, strerror((*__errno_location ())));
        return 0;
    }
    if (!((((statbuf.st_mode)) & 0170000) == (0040000))) {
        debug("'%s' not a directory", name);
        return 0;
    }
    return 1;
}

variable_types:
name
char *

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


decompiled_code:
const char *__fastcall chop_newline(const char *a1)
{
const char *result; // rax

result = &a1[(int)strlen(a1) - 1];
if ( *result == 10 )
*result = 0;
return result;
}

canonical_decompiled_code:
const char *__fastcall func6(const char *a1)
{
const char *result; // rax

result = &a1[(int)strlen(a1) - 1];
if ( *result == 10 )
*result = 0;
return result;
}

original_code:
void chop_newline(char* buf)
{
    int len = strlen(buf);
    if (buf[len-1] == '\n')
        buf[len-1] = '\0';
}

canonical_original_code:
void chop_newline(char *buf)
{
    int len = strlen(buf);
    if (buf[len-1] == '\n')
        buf[len-1] = '\0';
}

variable_types:
buf
char *

variable_types: len int


decompiled_code:
char **hg_get_info()
{
char **v0; // r12
char **result; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = (char **)init_result();
if ( (unsigned int)read_first_line(".hg/branch", v2, 1024LL) )
{
debug("read first line from .hg/branch: '%s'", v2);
*v0 = strdup(v2);
result = v0;
}
else
{
debug("failed to read from .hg/branch: assuming not an hg repo");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
char **func0()
{
char **v0; // r12
char **result; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = (char **)init_result();
if ( (unsigned int)read_first_line(".hg/branch", v2, 1024LL) )
{
debug("read first line from .hg/branch: '%s'", v2);
*v0 = strdup(v2);
result = v0;
}
else
{
debug("failed to read from .hg/branch: assuming not an hg repo");
result = 0LL;
}
return result;
}

original_code:
static result_t*
hg_get_info(vccontext_t* context)
{
    result_t* result = init_result();
    char buf[1024];
    if (read_first_line(".hg/branch", buf, 1024)) {
        debug("read first line from .hg/branch: '%s'", buf);
        result->branch = strdup(buf);
    }
    else {
        debug("failed to read from .hg/branch: assuming not an hg repo");
        return ((void *)0);
    }
    return result;
}

canonical_original_code:
static struct result_t *hg_get_info(struct vccontext_t *context)
{
    struct result_t *result = init_result();
    char buf[1024];
    if (read_first_line(".hg/branch", buf, 1024)) {
        debug("read first line from .hg/branch: '%s'", buf);
        result->branch = strdup(buf);
    }
    else {
        debug("failed to read from .hg/branch: assuming not an hg repo");
        return ((void *)0);
    }
    return result;
}


decompiled_code:
__int64 hg_probe()
{
return isdir(".hg");
}

canonical_decompiled_code:
__int64 func1()
{
return isdir(".hg");
}

original_code:
static int
hg_probe(vccontext_t* context)
{
    return isdir(".hg");
}

canonical_original_code:
static int hg_probe(struct vccontext_t *context)
{
    return isdir(".hg");
}


decompiled_code:
__int64 __fastcall get_hg_context(__int64 a1)
{
return init_context("hg", a1, hg_probe, hg_get_info);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return init_context("hg", a1, hg_probe, hg_get_info);
}

original_code:
vccontext_t* get_hg_context(options_t* options)
{
    return init_context("hg", options, hg_probe, hg_get_info);
}

canonical_original_code:
struct vccontext_t *get_hg_context(struct options_t *options)
{
    return init_context("hg", options, hg_probe, hg_get_info);
}


decompiled_code:
__int64 bzr_get_info()
{
__int64 v0; // r12
char *v1; // rax
__int64 result; // rax
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = init_result();
if ( (unsigned int)read_first_line(".bzr/branch/last-revision", v3, 1024LL) )
{
debug("read first line from .bzr/branch/last-revision: '%s'", v3);
v1 = strdup(v3);
*(_QWORD *)(v0 + 8) = v1;
*strchr(v1, 32) = 0;
result = v0;
}
else
{
debug("failed to read from .bzr/branch/last-revision: assuming not an bzr repo");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r12
char *v1; // rax
__int64 result; // rax
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = init_result();
if ( (unsigned int)read_first_line(".bzr/branch/last-revision", v3, 1024LL) )
{
debug("read first line from .bzr/branch/last-revision: '%s'", v3);
v1 = strdup(v3);
*(_QWORD *)(v0 + 8) = v1;
*strchr(v1, 32) = 0;
result = v0;
}
else
{
debug("failed to read from .bzr/branch/last-revision: assuming not an bzr repo");
result = 0LL;
}
return result;
}

original_code:
static result_t*
bzr_get_info(vccontext_t* context)
{
    result_t* result = init_result();
    char buf[1024];
    if (read_first_line(".bzr/branch/last-revision", buf, 1024)) {
        debug("read first line from .bzr/branch/last-revision: '%s'", buf);
        result->revision = strdup(buf);
        *(strchr(result->revision, ' ')) = '\0';
    }
    else {
        debug("failed to read from .bzr/branch/last-revision: assuming not an bzr repo");
        return ((void *)0);
    }
    return result;
}

canonical_original_code:
static struct result_t *bzr_get_info(struct vccontext_t *context)
{
    struct result_t *result = init_result();
    char buf[1024];
    if (read_first_line(".bzr/branch/last-revision", buf, 1024)) {
        debug("read first line from .bzr/branch/last-revision: '%s'", buf);
        result->revision = strdup(buf);
        *(strchr(result->revision, ' ')) = '\0';
    }
    else {
        debug("failed to read from .bzr/branch/last-revision: assuming not an bzr repo");
        return ((void *)0);
    }
    return result;
}


decompiled_code:
__int64 bzr_probe()
{
return isdir(".bzr");
}

canonical_decompiled_code:
__int64 func1()
{
return isdir(".bzr");
}

original_code:
static int
bzr_probe(vccontext_t* context)
{
    return isdir(".bzr");
}

canonical_original_code:
static int bzr_probe(struct vccontext_t *context)
{
    return isdir(".bzr");
}


decompiled_code:
__int64 __fastcall get_bzr_context(__int64 a1)
{
return init_context("bzr", a1, bzr_probe, bzr_get_info);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return init_context("bzr", a1, bzr_probe, bzr_get_info);
}

original_code:
vccontext_t* get_bzr_context(options_t* options)
{
    return init_context("bzr", options, bzr_probe, bzr_get_info);
}

canonical_original_code:
struct vccontext_t *get_bzr_context(struct options_t *options)
{
    return init_context("bzr", options, bzr_probe, bzr_get_info);
}


repo: avz/flap

decompiled_code:
void *__fastcall Server__connectionPollerThread(void *a1)
{
int v1; // eax
unsigned int v2; // ebp
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 0x80000;
while ( 1 )
{
v1 = accept(*(_DWORD *)a1, 0LL, 0LL);
v2 = v1;
if ( v1 < 0 )
break;
setsockopt(v1, 1, 7, optval, 4u);
ServerClientsPool_lock((char *)a1 + 8);
ServerClientsPool_attachClient((char *)a1 + 8, v2, 0LL, 0LL);
ServerClientsPool_unlock((char *)a1 + 8);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
int v1; // eax
unsigned int v2; // ebp
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 0x80000;
while ( 1 )
{
v1 = accept(*(_DWORD *)a1, 0LL, 0LL);
v2 = v1;
if ( v1 < 0 )
break;
setsockopt(v1, 1, 7, optval, 4u);
ServerClientsPool_lock((char *)a1 + 8);
ServerClientsPool_attachClient((char *)a1 + 8, v2, 0LL, 0LL);
ServerClientsPool_unlock((char *)a1 + 8);
}
return 0LL;
}

original_code:
static void *Server__connectionPollerThread(void *serverPtr) {
 struct Server *server = (struct Server *)serverPtr;
 int s;
 int bufSize = 1024 * 512;
 while((s = accept(server->sock, ((void *)0), ((void *)0))) >= 0) {
  setsockopt(s, 1, 7, &bufSize, (socklen_t)sizeof(bufSize));
  ServerClientsPool_lock(&server->pool);
  ServerClientsPool_attachClient(&server->pool, s, 00, 0);
  ServerClientsPool_unlock(&server->pool);
 }
 return ((void *)0);
}

canonical_original_code:
static void *Server__connectionPollerThread(void *serverPtr) {
 struct Server *server = (struct Server *)serverPtr;
 int s;
 int bufSize = 1024 * 512;
 while((s = accept(server->sock, ((void *)0), ((void *)0))) >= 0) {
  setsockopt(s, 1, 7, &bufSize, (unsigned int)sizeof(bufSize));
  ServerClientsPool_lock(&server->pool);
  ServerClientsPool_attachClient(&server->pool, s, 00, 0);
  ServerClientsPool_unlock(&server->pool);
 }
 return ((void *)0);
}

variable_types:
serverPtr
void *

variable_types: struct Server { int sock; struct ServerClientsPool { struct ServerClient { int sock; int mode; int flags; } **clients; unsigned long clientsAllocated; unsigned long clientsCount; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } pool; unsigned long connectionPollerThread; } *

variable_types: s int

variable_types: bufSize int

return_type: void *

user_defined_types: struct Server { int sock; struct ServerClientsPool pool; unsigned long connectionPollerThread; }

user_defined_types: struct ServerClientsPool { struct ServerClient **clients; unsigned long clientsAllocated; unsigned long clientsCount; union pthread_mutex_t mutex; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall Server_init(_DWORD *a1)
{
*a1 = -1;
return ServerClientsPool_init(a1 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
*a1 = -1;
return ServerClientsPool_init(a1 + 2);
}

original_code:
void Server_init(struct Server *server) {
 server->sock = -1;
 ServerClientsPool_init(&server->pool);
}

user_defined_types: struct ServerClient { int sock; int mode; int flags; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
int __fastcall Server_bindUnix(void *arg, char *s, int a3)
{
int v4; // eax
int v5; // ebp
int result; // eax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rbx
int *v11; // rax
int *v12; // rax
int *v13; // rax
int *v14; // [rsp+8h] [rbp-A0h]
struct sockaddr addr; // [rsp+10h] [rbp-98h] BYREF

if ( strlen(s) > 0x6B )
{
v9 = _errno_location();
*v9 = 36;
v10 = v9;
perror(s);
exit(*v10);
}
v4 = socket(1, 1, 0);
*(_DWORD *)arg = v4;
v5 = v4;
if ( v4 == -1 )
{
perror("socket(AF_UNIX, SOCK_STREAM, 0)");
v8 = _errno_location();
exit(*v8);
}
strncpy(addr.sa_data, s, 0x6CuLL);
addr.sa_family = 1;
if ( a3 )
{
if ( unlink(addr.sa_data) )
{
v7 = _errno_location();
if ( *v7 != 2 )
{
v14 = v7;
perror(addr.sa_data);
exit(*v14);
}
}
v5 = *(_DWORD *)arg;
}
if ( bind(v5, &addr, 0x6Eu) )
{
perror(addr.sa_data);
v13 = _errno_location();
exit(*v13);
}
if ( listen(*(_DWORD *)arg, 32) )
{
perror("listen()");
v12 = _errno_location();
exit(*v12);
}
result = pthread_create((pthread_t *)arg + 9, 0LL, Server__connectionPollerThread, arg);
if ( result )
{
perror("pthread_create(Server__connectionPollerThread)");
v11 = _errno_location();
exit(*v11);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *arg, char *s, int a3)
{
int v4; // eax
int v5; // ebp
int result; // eax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rbx
int *v11; // rax
int *v12; // rax
int *v13; // rax
int *v14; // [rsp+8h] [rbp-A0h]
struct sockaddr addr; // [rsp+10h] [rbp-98h] BYREF

if ( strlen(s) > 0x6B )
{
v9 = _errno_location();
*v9 = 36;
v10 = v9;
perror(s);
exit(*v10);
}
v4 = socket(1, 1, 0);
*(_DWORD *)arg = v4;
v5 = v4;
if ( v4 == -1 )
{
perror("socket(AF_UNIX, SOCK_STREAM, 0)");
v8 = _errno_location();
exit(*v8);
}
strncpy(addr.sa_data, s, 0x6CuLL);
addr.sa_family = 1;
if ( a3 )
{
if ( unlink(addr.sa_data) )
{
v7 = _errno_location();
if ( *v7 != 2 )
{
v14 = v7;
perror(addr.sa_data);
exit(*v14);
}
}
v5 = *(_DWORD *)arg;
}
if ( bind(v5, &addr, 0x6Eu) )
{
perror(addr.sa_data);
v13 = _errno_location();
exit(*v13);
}
if ( listen(*(_DWORD *)arg, 32) )
{
perror("listen()");
v12 = _errno_location();
exit(*v12);
}
result = pthread_create((pthread_t *)arg + 9, 0LL, Server__connectionPollerThread, arg);
if ( result )
{
perror("pthread_create(Server__connectionPollerThread)");
v11 = _errno_location();
exit(*v11);
}
return result;
}

original_code:
void Server_bindUnix(struct Server *server, const char *path, int forceReuse) {
 struct sockaddr_un addr;
 if(strlen(path) >= sizeof(addr.sun_path)) {
  (*__errno_location ()) = 36;
  perror(path);
  exit((*__errno_location ()));
 }
 server->sock = socket(1, SOCK_STREAM, 0);
 if(server->sock == -1) {
  perror("socket(AF_UNIX, SOCK_STREAM, 0)");
  exit((*__errno_location ()));
 }
 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
 addr.sun_family = 1;
 if(forceReuse) {
  if(unlink(addr.sun_path) != 0 && (*__errno_location ()) != 2) {
   perror(addr.sun_path);
   exit((*__errno_location ()));
  }
 }
 if(bind(server->sock, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) != 0) {
  perror(addr.sun_path);
  exit((*__errno_location ()));
 }
 if(listen(server->sock, 32) != 0) {
  perror("listen()");
  exit((*__errno_location ()));
 }
 if(pthread_create(&server->connectionPollerThread, ((void *)0), Server__connectionPollerThread, server) != 0) {
  perror("pthread_create(Server__connectionPollerThread)");
  exit((*__errno_location ()));
 }
}

canonical_original_code:
void Server_bindUnix(struct Server *server, char *path, int forceReuse) {
 struct sockaddr_un addr;
 if(strlen(path) >= sizeof(addr.sun_path)) {
  (*__errno_location ()) = 36;
  perror(path);
  exit((*__errno_location ()));
 }
 server->sock = socket(1, SOCK_STREAM, 0);
 if(server->sock == -1) {
  perror("socket(AF_UNIX, SOCK_STREAM, 0)");
  exit((*__errno_location ()));
 }
 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
 addr.sun_family = 1;
 if(forceReuse) {
  if(unlink(addr.sun_path) != 0 && (*__errno_location ()) != 2) {
   perror(addr.sun_path);
   exit((*__errno_location ()));
  }
 }
 if(bind(server->sock, (struct sockaddr *)&addr, (unsigned int)sizeof(addr)) != 0) {
  perror(addr.sun_path);
  exit((*__errno_location ()));
 }
 if(listen(server->sock, 32) != 0) {
  perror("listen()");
  exit((*__errno_location ()));
 }
 if(pthread_create(&server->connectionPollerThread, ((void *)0), Server__connectionPollerThread, server) != 0) {
  perror("pthread_create(Server__connectionPollerThread)");
  exit((*__errno_location ()));
 }
}

variable_types:
path
char *

variable_types: forceReuse int

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

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


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

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

original_code:
void Server_write(struct Server *server, const void *buf, ssize_t len) {
 ServerClientsPool_write(&server->pool, buf, len);
}

canonical_original_code:
void Server_write(struct Server *server, void *buf, long len) {
 ServerClientsPool_write(&server->pool, buf, len);
}

variable_types:
buf
void *

variable_types: len long


decompiled_code:
__int64 __fastcall Server_destroy(__int64 a1)
{
int v2; // edi

v2 = *(_DWORD *)a1;
if ( v2 >= 0 )
close(v2);
pthread_cancel(*(_QWORD *)(a1 + 72));
return ServerClientsPool_destroy(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v2; // edi

v2 = *(_DWORD *)a1;
if ( v2 >= 0 )
close(v2);
pthread_cancel(*(_QWORD *)(a1 + 72));
return ServerClientsPool_destroy(a1 + 8);
}

original_code:
void Server_destroy(struct Server *server) {
 if(server->sock >= 0)
  close(server->sock);
 pthread_cancel(server->connectionPollerThread);
 ServerClientsPool_destroy(&server->pool);
}


usageAndExit

decompiled_code:
void __fastcall __noreturn usageAndExit(const char *a1)
{
fprintf(stderr, "Usage: %s (-s|-c) /path/to/socket.sock\n\t-s: server mode\n\t-c: client mode (read-only)\n", a1);
exit(255);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(stderr, "Usage: %s (-s|-c) /path/to/socket.sock\n\t-s: server mode\n\t-c: client mode (read-only)\n", a1);
exit(255);
}

original_code:
void usageAndExit(const char *cmd) {
 fprintf(stderr,
  "Usage: %s (-s|-c) /path/to/socket.sock\n"
  "	-s: server mode\n"
  "	-c: client mode (read-only)\n"
  , cmd
 );
 exit(255);
}

canonical_original_code:
void usageAndExit(char *cmd) {
 fprintf(stderr,
  "Usage: %s (-s|-c) /path/to/socket.sock\n"
  "	-s: server mode\n"
  "	-c: client mode (read-only)\n"
  , cmd
 );
 exit(255);
}

variable_types:
cmd
char *


runClientMode

decompiled_code:
__int64 __fastcall runClientMode(char *s)
{
int v1; // er12
ssize_t v2; // rax
ssize_t v3; // rbp
__int64 result; // rax
__int64 v5; // rbx
ssize_t v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rbx
int optval; // [rsp+Ch] [rbp-408Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-4088h] BYREF
char buf[16408]; // [rsp+80h] [rbp-4018h] BYREF

optval = 0x80000;
if ( strlen(s) > 0x6B )
{
v8 = _errno_location();
*v8 = 36;
v9 = v8;
perror(s);
exit(*v9);
}
v1 = socket(1, 1, 0);
if ( v1 == -1 )
{
perror("socket(AF_UNIX, SOCK_STREAM, 0)");
v7 = _errno_location();
exit(*v7);
}
strncpy(addr.sa_data, s, 0x6CuLL);
addr.sa_family = 1;
if ( connect(v1, &addr, 0x6Eu) )
{
perror(s);
result = (unsigned int)*_errno_location();
}
else
{
setsockopt(v1, 1, 7, &optval, 4u);
LABEL_5:
while ( 1 )
{
v2 = read(v1, buf, 0x4000uLL);
v3 = v2;
if ( v2 > 0 )
break;
if ( !v2 )
return 0LL;
result = (unsigned int)*_errno_location();
if ( (_DWORD)result != 4 )
return result;
}
v5 = 0LL;
while ( 1 )
{
v6 = write(1, &buf[v5], v3 - v5);
if ( v6 <= 0 )
break;
v5 += v6;
if ( v3 <= v5 )
goto LABEL_5;
}
perror("read(STDIN_FILENO)");
result = (unsigned int)*_errno_location();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s)
{
int v1; // er12
ssize_t v2; // rax
ssize_t v3; // rbp
__int64 result; // rax
__int64 v5; // rbx
ssize_t v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rbx
int optval; // [rsp+Ch] [rbp-408Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-4088h] BYREF
char buf[16408]; // [rsp+80h] [rbp-4018h] BYREF

optval = 0x80000;
if ( strlen(s) > 0x6B )
{
v8 = _errno_location();
*v8 = 36;
v9 = v8;
perror(s);
exit(*v9);
}
v1 = socket(1, 1, 0);
if ( v1 == -1 )
{
perror("socket(AF_UNIX, SOCK_STREAM, 0)");
v7 = _errno_location();
exit(*v7);
}
strncpy(addr.sa_data, s, 0x6CuLL);
addr.sa_family = 1;
if ( connect(v1, &addr, 0x6Eu) )
{
perror(s);
result = (unsigned int)*_errno_location();
}
else
{
setsockopt(v1, 1, 7, &optval, 4u);
LABEL_5:
while ( 1 )
{
v2 = read(v1, buf, 0x4000uLL);
v3 = v2;
if ( v2 > 0 )
break;
if ( !v2 )
return 0LL;
result = (unsigned int)*_errno_location();
if ( (_DWORD)result != 4 )
return result;
}
v5 = 0LL;
while ( 1 )
{
v6 = write(1, &buf[v5], v3 - v5);
if ( v6 <= 0 )
break;
v5 += v6;
if ( v3 <= v5 )
goto LABEL_5;
}
perror("read(STDIN_FILENO)");
result = (unsigned int)*_errno_location();
}
return result;
}

original_code:
int runClientMode(const char *sockPath) {
 char buf[16 * 1024];
 struct sockaddr_un addr;
 int sock;
 int sockBufSize = 1024 * 512;
 if(strlen(sockPath) >= sizeof(addr.sun_path)) {
  (*__errno_location ()) = 36;
  perror(sockPath);
  exit((*__errno_location ()));
 }
 sock = socket(1, SOCK_STREAM, 0);
 if(sock == -1) {
  perror("socket(AF_UNIX, SOCK_STREAM, 0)");
  exit((*__errno_location ()));
 }
 strncpy(addr.sun_path, sockPath, sizeof(addr.sun_path));
 addr.sun_family = 1;
 if(connect(sock, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) != 0) {
  perror(sockPath);
  return (*__errno_location ());
 }
 setsockopt(sock, 1, 7, &sockBufSize, (socklen_t)sizeof(sockBufSize));
 while(1) {
  ssize_t written = 0;
  ssize_t r = read(sock, buf, sizeof(buf));
  if(r <= 0) {
   if(r < 0 && (*__errno_location ()) == 4)
    continue;
   return r ? (*__errno_location ()) : 0;
  }
  while(written < r) {
   ssize_t w = write(1, buf + written, (size_t)(r - written));
   if(w <= 0) {
    perror("read(STDIN_FILENO)");
    return (*__errno_location ());
   } else {
    written += w;
   }
  }
 }
 return 0;
}

canonical_original_code:
int runClientMode(char *sockPath) {
 char buf[-1];
 struct sockaddr_un addr;
 int sock;
 int sockBufSize = 1024 * 512;
 if(strlen(sockPath) >= sizeof(addr.sun_path)) {
  (*__errno_location ()) = 36;
  perror(sockPath);
  exit((*__errno_location ()));
 }
 sock = socket(1, SOCK_STREAM, 0);
 if(sock == -1) {
  perror("socket(AF_UNIX, SOCK_STREAM, 0)");
  exit((*__errno_location ()));
 }
 strncpy(addr.sun_path, sockPath, sizeof(addr.sun_path));
 addr.sun_family = 1;
 if(connect(sock, (struct sockaddr *)&addr, (unsigned int)sizeof(addr)) != 0) {
  perror(sockPath);
  return (*__errno_location ());
 }
 setsockopt(sock, 1, 7, &sockBufSize, (unsigned int)sizeof(sockBufSize));
 while(1) {
  long written = 0;
  long r = read(sock, buf, sizeof(buf));
  if(r <= 0) {
   if(r < 0 && (*__errno_location ()) == 4)
    continue;
   return r ? (*__errno_location ()) : 0;
  }
  while(written < r) {
   long w = write(1, buf + written, (size_t)(r - written));
   if(w <= 0) {
    perror("read(STDIN_FILENO)");
    return (*__errno_location ());
   } else {
    written += w;
   }
  }
 }
 return 0;
}

variable_types:
sockPath
char *

variable_types:
buf
char [-1]

variable_types: sock int

variable_types: sockBufSize int

variable_types: written long

variable_types: r long

variable_types: w long

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


runServerMode

decompiled_code:
__int64 __fastcall runServerMode(__int64 a1)
{
ssize_t v1; // rax
ssize_t v2; // r12
int *v3; // rbx
__int64 v5; // rbp
ssize_t v6; // rax
ssize_t v7; // rbx
int *v8; // rax
int *v9; // [rsp+8h] [rbp-8070h]
char v10[80]; // [rsp+10h] [rbp-8068h] BYREF
char buf[32792]; // [rsp+60h] [rbp-8018h] BYREF

signal(13, (__sighandler_t)((char *)usageAndExit + 1));
Server_init(v10);
Server_bindUnix(v10, a1, 1LL);
LABEL_2:
while ( 1 )
{
v1 = read(0, buf, 0x8000uLL);
v2 = v1;
if ( v1 > 0 )
break;
if ( !v1 )
goto LABEL_13;
v3 = _errno_location();
if ( *v3 != 4 )
{
perror("read(STDIN_FILENO)");
return (unsigned int)*v3;
}
}
v5 = 0LL;
while ( 1 )
{
v6 = write(1, &buf[v5], v2 - v5);
v7 = v6;
if ( v6 <= 0 )
break;
v5 += v6;
if ( v2 <= v5 )
{
Server_write(v10, buf, v2);
goto LABEL_2;
}
}
v8 = _errno_location();
if ( v7 == -1 && *v8 == 32 )
{
Server_write(v10, buf, v2);
LABEL_13:
Server_destroy(v10);
return 0LL;
}
v9 = v8;
perror("write(STDOUT_FILENO)");
return (unsigned int)*v9;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
ssize_t v1; // rax
ssize_t v2; // r12
int *v3; // rbx
__int64 v5; // rbp
ssize_t v6; // rax
ssize_t v7; // rbx
int *v8; // rax
int *v9; // [rsp+8h] [rbp-8070h]
char v10[80]; // [rsp+10h] [rbp-8068h] BYREF
char buf[32792]; // [rsp+60h] [rbp-8018h] BYREF

signal(13, (__sighandler_t)((char *)usageAndExit + 1));
Server_init(v10);
Server_bindUnix(v10, a1, 1LL);
LABEL_2:
while ( 1 )
{
v1 = read(0, buf, 0x8000uLL);
v2 = v1;
if ( v1 > 0 )
break;
if ( !v1 )
goto LABEL_13;
v3 = _errno_location();
if ( *v3 != 4 )
{
perror("read(STDIN_FILENO)");
return (unsigned int)*v3;
}
}
v5 = 0LL;
while ( 1 )
{
v6 = write(1, &buf[v5], v2 - v5);
v7 = v6;
if ( v6 <= 0 )
break;
v5 += v6;
if ( v2 <= v5 )
{
Server_write(v10, buf, v2);
goto LABEL_2;
}
}
v8 = _errno_location();
if ( v7 == -1 && *v8 == 32 )
{
Server_write(v10, buf, v2);
LABEL_13:
Server_destroy(v10);
return 0LL;
}
v9 = v8;
perror("write(STDOUT_FILENO)");
return (unsigned int)*v9;
}

original_code:
int runServerMode(const char *sockPath) {
 char buf[32 * 1024];
 struct Server server;
 char eof = 0;
 signal(13, ((__sighandler_t) 1));
 Server_init(&server);
 Server_bindUnix(&server, sockPath, 1);
 while(!eof) {
  ssize_t r = read(0, buf, sizeof(buf));
  ssize_t written = 0;
  if(r <= 0) {
   if(r == 0)
    break;
   if((*__errno_location ()) == 4)
    continue;
   perror("read(STDIN_FILENO)");
   return (*__errno_location ());
  }
  while(written < r && !eof) {
   ssize_t w = write(1, buf + written, (size_t)(r - written));
   if(w <= 0) {
    if(w == -1 && (*__errno_location ()) == 32) {
     eof = 1;
     break;
    }
    perror("write(STDOUT_FILENO)");
    return (*__errno_location ());
   } else {
    written += w;
   }
  }
  Server_write(&server, buf, r);
 }
 Server_destroy(&server);
 return 0;
}

canonical_original_code:
int runServerMode(char *sockPath) {
 char buf[-1];
 struct Server server;
 char eof = 0;
 signal(13, ((void (*)(int)) 1));
 Server_init(&server);
 Server_bindUnix(&server, sockPath, 1);
 while(!eof) {
  long r = read(0, buf, sizeof(buf));
  long written = 0;
  if(r <= 0) {
   if(r == 0)
    break;
   if((*__errno_location ()) == 4)
    continue;
   perror("read(STDIN_FILENO)");
   return (*__errno_location ());
  }
  while(written < r && !eof) {
   long w = write(1, buf + written, (size_t)(r - written));
   if(w <= 0) {
    if(w == -1 && (*__errno_location ()) == 32) {
     eof = 1;
     break;
    }
    perror("write(STDOUT_FILENO)");
    return (*__errno_location ());
   } else {
    written += w;
   }
  }
  Server_write(&server, buf, r);
 }
 Server_destroy(&server);
 return 0;
}

variable_types: struct Server { int sock; struct ServerClientsPool { struct ServerClient { int sock; int mode; int flags; } **clients; unsigned long clientsAllocated; unsigned long clientsCount; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } pool; unsigned long connectionPollerThread; }

variable_types: eof char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r13
char v4; // r12
int v5; // eax
char *v6; // rdi

v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "sc");
if ( v5 == -1 )
break;
if ( v5 == 99 )
{
v3 = 1;
}
else
{
if ( v5 != 115 )
goto LABEL_14;
v4 = 1;
}
}
if ( optind > argc || ((unsigned __int8)v3 & (unsigned __int8)v4) != 0 || !((unsigned __int8)v4 | (unsigned __int8)v3) )
LABEL_14:
usageAndExit(*argv);
v6 = (char *)argv[optind];
if ( v4 )
runServerMode((__int64)v6);
else
runClientMode(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r13
char v4; // r12
int v5; // eax
char *v6; // rdi

v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "sc");
if ( v5 == -1 )
break;
if ( v5 == 99 )
{
v3 = 1;
}
else
{
if ( v5 != 115 )
goto LABEL_14;
v4 = 1;
}
}
if ( optind > argc || ((unsigned __int8)v3 & (unsigned __int8)v4) != 0 || !((unsigned __int8)v4 | (unsigned __int8)v3) )
LABEL_14:
func0(*argv);
v6 = (char *)argv[optind];
if ( v4 )
func2((__int64)v6);
else
func1(v6);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 int opt;
 char serverMode = 0;
 char clientMode = 0;
 while((opt = getopt(argc, argv, "sc")) != -1) {
  switch(opt) {
   case 's':
    serverMode = 1;
   break;
   case 'c':
    clientMode = 1;
   break;
   default:
    usageAndExit(argv[0]);
  }
 }
 if(optind > argc)
  usageAndExit(argv[0]);
 if((serverMode && clientMode) || (!serverMode && !clientMode))
  usageAndExit(argv[0]);
 if(serverMode) {
  runServerMode(argv[optind]);
 } else if(clientMode) {
  runClientMode(argv[optind]);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: serverMode char

variable_types: clientMode char


decompiled_code:
void *__fastcall ServerClientsPool__enlarge(__int64 a1)
{
__int64 v1; // rbp
__int64 v3; // r12
size_t v4; // rdi
void *result; // rax
void *v6; // rcx

v1 = *(_QWORD *)(a1 + 8);
v3 = 2 * v1;
if ( 2 * v1 )
{
v4 = 16 * v1;
}
else
{
v4 = 16LL;
v3 = 2LL;
}
result = calloc(v4, 1uLL);
v6 = result;
if ( *(_QWORD *)a1 )
{
result = memcpy(result, *(const void **)a1, 8 * v1);
v6 = result;
}
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)a1 = v6;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1)
{
__int64 v1; // rbp
__int64 v3; // r12
size_t v4; // rdi
void *result; // rax
void *v6; // rcx

v1 = *(_QWORD *)(a1 + 8);
v3 = 2 * v1;
if ( 2 * v1 )
{
v4 = 16 * v1;
}
else
{
v4 = 16LL;
v3 = 2LL;
}
result = calloc(v4, 1uLL);
v6 = result;
if ( *(_QWORD *)a1 )
{
result = memcpy(result, *(const void **)a1, 8 * v1);
v6 = result;
}
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)a1 = v6;
return result;
}

original_code:
static void ServerClientsPool__enlarge(struct ServerClientsPool *pool) {
 struct ServerClient **newArray;
 size_t newSize = pool->clientsAllocated * 2;
 if(!newSize)
  newSize = 2;
 newArray = (struct ServerClient **)malloc(sizeof(*newArray) * newSize);
 memset(newArray, 0, (sizeof(*newArray) * newSize));
 if(pool->clients)
  memcpy(newArray, pool->clients, pool->clientsAllocated * sizeof(*pool->clients));
 pool->clients = newArray;
 pool->clientsAllocated = newSize;
}

canonical_original_code:
static void ServerClientsPool__enlarge(struct ServerClientsPool *pool) {
 struct ServerClient **newArray;
 unsigned long newSize = pool->clientsAllocated * 2;
 if(!newSize)
  newSize = 2;
 newArray = (struct ServerClient **)malloc(sizeof(*newArray) * newSize);
 memset(newArray, 0, (sizeof(*newArray) * newSize));
 if(pool->clients)
  memcpy(newArray, pool->clients, pool->clientsAllocated * sizeof(*pool->clients));
 pool->clients = newArray;
 pool->clientsAllocated = newSize;
}

variable_types: struct ServerClientsPool { struct ServerClient { int sock; int mode; int flags; } **clients; unsigned long clientsAllocated; unsigned long clientsCount; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *

variable_types:
newSize
unsigned long


decompiled_code:
void *__fastcall ServerClientsPool_init(__int64 a1)
{
pthread_mutex_t *v2; // rdi

v2 = (pthread_mutex_t *)(a1 + 24);
*(&v2[-1].__align + 2) = 0LL;
v2[-1].__list.__prev = 0LL;
v2[-1].__list.__next = 0LL;
pthread_mutex_init(v2, 0LL);
return ServerClientsPool__enlarge(a1);
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1)
{
pthread_mutex_t *v2; // rdi

v2 = (pthread_mutex_t *)(a1 + 24);
*(&v2[-1].__align + 2) = 0LL;
v2[-1].__list.__prev = 0LL;
v2[-1].__list.__next = 0LL;
pthread_mutex_init(v2, 0LL);
return func0(a1);
}

original_code:
void ServerClientsPool_init(struct ServerClientsPool *pool) {
 pool->clients = ((void *)0);
 pool->clientsAllocated = 0;
 pool->clientsCount = 0;
 pthread_mutex_init(&pool->mutex, ((void *)0));
 ServerClientsPool__enlarge(pool);
}


decompiled_code:
int __fastcall ServerClientsPool_destroy(__int64 a1)
{
_QWORD *v1; // r8
unsigned __int64 v3; // rbx
__int64 v4; // rdi
int result; // eax

v1 = *(_QWORD **)a1;
if ( *(_QWORD *)a1 )
{
if ( *(_QWORD *)(a1 + 8) )
{
v3 = 0LL;
do
{
v4 = v1[v3];
if ( v4 )
{
ServerClient_destroy(v4);
free(*(void **)(*(_QWORD *)a1 + 8 * v3));
v1 = *(_QWORD **)a1;
}
++v3;
}
while ( *(_QWORD *)(a1 + 8) > v3 );
}
free(v1);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
result = pthread_mutex_destroy((pthread_mutex_t *)(a1 + 24));
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
_QWORD *v1; // r8
unsigned __int64 v3; // rbx
__int64 v4; // rdi
int result; // eax

v1 = *(_QWORD **)a1;
if ( *(_QWORD *)a1 )
{
if ( *(_QWORD *)(a1 + 8) )
{
v3 = 0LL;
do
{
v4 = v1[v3];
if ( v4 )
{
ServerClient_destroy(v4);
free(*(void **)(*(_QWORD *)a1 + 8 * v3));
v1 = *(_QWORD **)a1;
}
++v3;
}
while ( *(_QWORD *)(a1 + 8) > v3 );
}
free(v1);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
result = pthread_mutex_destroy((pthread_mutex_t *)(a1 + 24));
}
return result;
}

original_code:
void ServerClientsPool_destroy(struct ServerClientsPool *pool) {
 size_t i;
 if(!pool->clients)
  return;
 for(i = 0; i < pool->clientsAllocated; i++) {
  if(!pool->clients[i])
   continue;
  ServerClient_destroy(pool->clients[i]);
  free(pool->clients[i]);
 }
 free(pool->clients);
 pool->clients = ((void *)0);
 pool->clientsAllocated = 0;
 pool->clientsCount = 0;
 pthread_mutex_destroy(&pool->mutex);
}

canonical_original_code:
void ServerClientsPool_destroy(struct ServerClientsPool *pool) {
 unsigned long i;
 if(!pool->clients)
  return;
 for(i = 0; i < pool->clientsAllocated; i++) {
  if(!pool->clients[i])
   continue;
  ServerClient_destroy(pool->clients[i]);
  free(pool->clients[i]);
 }
 free(pool->clients);
 pool->clients = ((void *)0);
 pool->clientsAllocated = 0;
 pool->clientsCount = 0;
 pthread_mutex_destroy(&pool->mutex);
}

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall ServerClientsPool_attachClient(_QWORD *a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
void *v6; // r12
__int64 v7; // rcx
_QWORD *v8; // rax
__int64 v9; // rdx
__int64 result; // rax

if ( (unsigned int)fcntl64(a2, 4LL, 2048LL) == -1 )
{
perror("fcntl(sock, F_SETFL, O_NONBLOCK)");
result = (unsigned int)*_errno_location();
}
else
{
if ( (unsigned __int64)(a1[2] + 1LL) > a1[1] )
ServerClientsPool__enlarge((__int64)a1);
v6 = malloc(0xCuLL);
ServerClient_init(v6, a2, a3, a4);
v7 = a1[1];
if ( v7 )
{
v8 = (_QWORD *)*a1;
v9 = 0LL;
while ( *v8 )
{
++v9;
++v8;
if ( v9 == v7 )
goto LABEL_9;
}
*v8 = v6;
}
LABEL_9:
++a1[2];
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
void *v6; // r12
__int64 v7; // rcx
_QWORD *v8; // rax
__int64 v9; // rdx
__int64 result; // rax

if ( (unsigned int)fcntl64(a2, 4LL, 2048LL) == -1 )
{
perror("fcntl(sock, F_SETFL, O_NONBLOCK)");
result = (unsigned int)*_errno_location();
}
else
{
if ( (unsigned __int64)(a1[2] + 1LL) > a1[1] )
func0((__int64)a1);
v6 = malloc(0xCuLL);
ServerClient_init(v6, a2, a3, a4);
v7 = a1[1];
if ( v7 )
{
v8 = (_QWORD *)*a1;
v9 = 0LL;
while ( *v8 )
{
++v9;
++v8;
if ( v9 == v7 )
goto LABEL_9;
}
*v8 = v6;
}
LABEL_9:
++a1[2];
result = 0LL;
}
return result;
}

original_code:
int ServerClientsPool_attachClient(struct ServerClientsPool *pool, int sock, int mode, int flags) {
 size_t i;
 struct ServerClient *client;
 if(fcntl(sock, 4, 04000) == -1) {
  perror("fcntl(sock, F_SETFL, O_NONBLOCK)");
  return (*__errno_location ());
 }
 if(pool->clientsCount + 1 > pool->clientsAllocated)
  ServerClientsPool__enlarge(pool);
 client = malloc(sizeof(*client));
 ServerClient_init(client, sock, mode, flags);
 for(i = 0; i < pool->clientsAllocated; i++) {
  if(!pool->clients[i]) {
   pool->clients[i] = client;
   break;
  }
 }
 pool->clientsCount++;
 return 0;
}

canonical_original_code:
int ServerClientsPool_attachClient(struct ServerClientsPool *pool, int sock, int mode, int flags) {
 unsigned long i;
 struct ServerClient *client;
 if(fcntl(sock, 4, 04000) == -1) {
  perror("fcntl(sock, F_SETFL, O_NONBLOCK)");
  return (*__errno_location ());
 }
 if(pool->clientsCount + 1 > pool->clientsAllocated)
  ServerClientsPool__enlarge(pool);
 client = malloc(sizeof(*client));
 ServerClient_init(client, sock, mode, flags);
 for(i = 0; i < pool->clientsAllocated; i++) {
  if(!pool->clients[i]) {
   pool->clients[i] = client;
   break;
  }
 }
 pool->clientsCount++;
 return 0;
}

variable_types: mode int

variable_types: flags int

variable_types: struct ServerClient { int sock; int mode; int flags; } *


decompiled_code:
int __fastcall ServerClientsPool_lock(__int64 a1)
{
int result; // eax
int *v2; // rax

result = pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( result )
{
perror("pthread_mutex_lock()");
v2 = _errno_location();
exit(*v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
int result; // eax
int *v2; // rax

result = pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( result )
{
perror("pthread_mutex_lock()");
v2 = _errno_location();
exit(*v2);
}
return result;
}

original_code:
void ServerClientsPool_lock(struct ServerClientsPool *pool) {
 if(pthread_mutex_lock(&pool->mutex) != 0) {
  perror("pthread_mutex_lock()");
  exit((*__errno_location ()));
 }
}


decompiled_code:
int __fastcall ServerClientsPool_unlock(__int64 a1)
{
int result; // eax
int *v2; // rax

result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
if ( result )
{
perror("pthread_mutex_unlock()");
v2 = _errno_location();
exit(*v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
int result; // eax
int *v2; // rax

result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
if ( result )
{
perror("pthread_mutex_unlock()");
v2 = _errno_location();
exit(*v2);
}
return result;
}

original_code:
void ServerClientsPool_unlock(struct ServerClientsPool *pool) {
 if(pthread_mutex_unlock(&pool->mutex) != 0) {
  perror("pthread_mutex_unlock()");
  exit((*__errno_location ()));
 }
}


decompiled_code:
int __fastcall ServerClientsPool_write(_QWORD *a1, const void *a2, size_t a3)
{
unsigned __int64 v3; // r15
unsigned __int64 v5; // rbx
__int64 v6; // r12
int *v7; // rax
int v8; // eax

v3 = 0LL;
v5 = 0LL;
ServerClientsPool_lock((__int64)a1);
if ( a1[1] )
{
while ( a1[2] > v3 )
{
v6 = 8 * v5;
v7 = *(int **)(*a1 + 8 * v5);
if ( !v7 || (++v3, write(*v7, a2, a3) != -1) || (v8 = *_errno_location(), v8 == 4) || v8 == 11 )
{
if ( a1[1] <= ++v5 )
return ServerClientsPool_unlock((__int64)a1);
}
else
{
++v5;
ServerClientsPool__removeClient(a1, *(void **)(*a1 + v6));
if ( a1[1] <= v5 )
return ServerClientsPool_unlock((__int64)a1);
}
}
}
return ServerClientsPool_unlock((__int64)a1);
}

original_code:
void ServerClientsPool_write(struct ServerClientsPool *pool, const void *buf, ssize_t size) {
 size_t i;
 ssize_t w;
 size_t count = 0;
 ServerClientsPool_lock(pool);
 for(i = 0; i < pool->clientsAllocated && count < pool->clientsCount; i++) {
  if(!pool->clients[i])
   continue;
  count++;
  w = write(pool->clients[i]->sock, buf, (size_t)size);
  if(w == -1) {
   if((*__errno_location ()) == 4 || (*__errno_location ()) == 11 || (*__errno_location ()) == 11)
    continue;
   ServerClientsPool__removeClient(pool, pool->clients[i]);
  }
 }
 ServerClientsPool_unlock(pool);
}

canonical_original_code:
void ServerClientsPool_write(struct ServerClientsPool *pool, void *buf, long size) {
 unsigned long i;
 long w;
 unsigned long count = 0;
 ServerClientsPool_lock(pool);
 for(i = 0; i < pool->clientsAllocated && count < pool->clientsCount; i++) {
  if(!pool->clients[i])
   continue;
  count++;
  w = write(pool->clients[i]->sock, buf, (unsigned long)size);
  if(w == -1) {
   if((*__errno_location ()) == 4 || (*__errno_location ()) == 11 || (*__errno_location ()) == 11)
    continue;
   ServerClientsPool__removeClient(pool, pool->clients[i]);
  }
 }
 ServerClientsPool_unlock(pool);
}

variable_types:
buf
void *

variable_types: size long

variable_types:
count
unsigned long


decompiled_code:
int __fastcall ServerClient_destroy(int *a1)
{
int v2; // edi
int result; // eax

v2 = *a1;
if ( v2 >= 0 )
result = close(v2);
*a1 = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
int v2; // edi
int result; // eax

v2 = *a1;
if ( v2 >= 0 )
result = close(v2);
*a1 = -1;
return result;
}

original_code:
void ServerClient_destroy(struct ServerClient *client) {
 if(client->sock >= 0)
  close(client->sock);
 client->sock = -1;
}


gettimed

repo: avz/pp

decompiled_code:
double gettimed()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

canonical_decompiled_code:
double func0()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

original_code:
static double gettimed() {
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}

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

return_type: double


decompiled_code:
__int64 __fastcall get_terminal_width(int a1)
{
int v1; // er8
__int64 result; // rax
char v3[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-Eh]

v1 = ioctl(a1, 0x5413uLL, v3);
result = 80LL;
if ( v1 >= 0 )
result = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // er8
__int64 result; // rax
char v3[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-Eh]

v1 = ioctl(a1, 0x5413uLL, v3);
result = 80LL;
if ( v1 >= 0 )
result = v4;
return result;
}

original_code:
static size_t get_terminal_width(int fd) {
 size_t width = 80;
 struct winsize wsz;
 if(ioctl(fd, 0x5413, &wsz) < 0) {
 } else {
  width = wsz.ws_col;
 }
 return width;
}

canonical_original_code:
static unsigned long get_terminal_width(int fd) {
 unsigned long width = 80;
 struct winsize wsz;
 if(ioctl(fd, 0x5413, &wsz) < 0) {
 } else {
  width = wsz.ws_col;
 }
 return width;
}

variable_types: fd int

variable_types:
width
unsigned long

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

return_type: unsigned long


checkPtr

repo: awblocker/rowavedt

decompiled_code:
void __fastcall checkPtr(__int64 a1, const char *a2)
{
if ( !a1 )
{
fprintf(stderr, "Error -- %s\n", a2);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, const char *a2)
{
if ( !a1 )
{
fprintf(stderr, "Error -- %s\n", a2);
exit(1);
}
}

original_code:
void checkPtr(const void * ptr, const char * msg)
{
  if (ptr==((void *)0))
  {
    fprintf(stderr, "Error -- %s\n", msg);
    exit(1);
  }
}

canonical_original_code:
void checkPtr(void *ptr, char *msg)
{
  if (ptr==((void *)0))
  {
    fprintf(stderr, "Error -- %s\n", msg);
    exit(1);
  }
}

variable_types:
ptr
void *

variable_types:
msg
char *


arrayMinMax

decompiled_code:
__int64 __fastcall arrayMinMax(double *a1, int a2, double *a3, double *a4)
{
double v4; // xmm1_8
double *v5; // rax
__int64 v6; // rsi
double v7; // xmm0_8

v4 = *a1;
*a3 = *a1;
*a4 = v4;
if ( a2 > 1 )
{
v5 = a1 + 1;
v6 = (__int64)&a1[(unsigned int)(a2 - 2) + 2];
while ( 1 )
{
v7 = *v5;
if ( *v5 > v4 )
break;
if ( *a3 <= v7 )
goto LABEL_4;
++v5;
*a3 = v7;
if ( v5 == (double *)v6 )
return 0LL;
LABEL_5:
v4 = *a4;
}
*a4 = v7;
LABEL_4:
if ( ++v5 == (double *)v6 )
return 0LL;
goto LABEL_5;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(double *a1, int a2, double *a3, double *a4)
{
double v4; // xmm1_8
double *v5; // rax
__int64 v6; // rsi
double v7; // xmm0_8

v4 = *a1;
*a3 = *a1;
*a4 = v4;
if ( a2 > 1 )
{
v5 = a1 + 1;
v6 = (__int64)&a1[(unsigned int)(a2 - 2) + 2];
while ( 1 )
{
v7 = *v5;
if ( *v5 > v4 )
break;
if ( *a3 <= v7 )
goto LABEL_4;
++v5;
*a3 = v7;
if ( v5 == (double *)v6 )
return 0LL;
LABEL_5:
v4 = *a4;
}
*a4 = v7;
LABEL_4:
if ( ++v5 == (double *)v6 )
return 0LL;
goto LABEL_5;
}
return 0LL;
}

original_code:
int arrayMinMax(double * X, int n, double * min, double * max)
{
  *min = X[0];
  *max = X[0];
  if (n > 1)
  {
    int i;
    for (i=1; i<n; i++)
    {
      if (X[i] > *max)
      {
        *max = X[i];
      }
      else if (X[i] < *min)
      {
        *min = X[i];
      }
    }
  }
  return 0;
}

canonical_original_code:
int arrayMinMax(double *X, int n, double *min, double *max)
{
  *min = X[0];
  *max = X[0];
  if (n > 1)
  {
    int i;
    for (i=1; i<n; i++)
    {
      if (X[i] > *max)
      {
        *max = X[i];
      }
      else if (X[i] < *min)
      {
        *min = X[i];
      }
    }
  }
  return 0;
}

variable_types:
X
double *

variable_types: n int

variable_types:
min
double *

variable_types:
max
double *

variable_types: i int


allocateMatrix

decompiled_code:
__int64 __fastcall allocateMatrix(_QWORD *a1, int a2, int a3)
{
__int64 v3; // rbp
_QWORD *v4; // rax
_QWORD *v5; // rbx
size_t v6; // rbp
_QWORD *v7; // r12
void *v8; // rax

v3 = a3;
v4 = malloc(8LL * a2);
*a1 = v4;
if ( v4 )
{
if ( a2 <= 0 )
return 0LL;
v5 = v4;
v6 = 8 * v3;
v7 = &v4[a2];
while ( 1 )
{
v8 = malloc(v6);
*v5 = v8;
if ( !v8 )
break;
if ( ++v5 == v7 )
return 0LL;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, int a2, int a3)
{
__int64 v3; // rbp
_QWORD *v4; // rax
_QWORD *v5; // rbx
size_t v6; // rbp
_QWORD *v7; // r12
void *v8; // rax

v3 = a3;
v4 = malloc(8LL * a2);
*a1 = v4;
if ( v4 )
{
if ( a2 <= 0 )
return 0LL;
v5 = v4;
v6 = 8 * v3;
v7 = &v4[a2];
while ( 1 )
{
v8 = malloc(v6);
*v5 = v8;
if ( !v8 )
break;
if ( ++v5 == v7 )
return 0LL;
}
}
return 1LL;
}

original_code:
int allocateMatrix(double*** matrix, int nRows, int nCols)
{
  int i;
  (*matrix) = malloc(nRows * sizeof(double*));
  if ((*matrix) == ((void *)0))
    return 1;
  for (i=0; i<nRows; i++)
  {
    (*matrix)[i] = malloc(nCols * sizeof(double));
    if ((*matrix)[i] == ((void *)0))
      return 1;
  }
  return 0;
}

canonical_original_code:
int allocateMatrix(double ***matrix, int nRows, int nCols)
{
  int i;
  (*matrix) = malloc(nRows * sizeof(double *));
  if ((*matrix) == ((void *)0))
    return 1;
  for (i=0; i<nRows; i++)
  {
    (*matrix)[i] = malloc(nCols * sizeof(double));
    if ((*matrix)[i] == ((void *)0))
      return 1;
  }
  return 0;
}

variable_types:
matrix
double ***

variable_types: nRows int

variable_types: nCols int


freeMatrix

decompiled_code:
void __fastcall freeMatrix(void **a1, __int64 a2, int a3)
{
__int64 v4; // rbx
__int64 v5; // rbp
void *v6; // rdi

if ( a3 > 0 )
{
v4 = 0LL;
v5 = 8LL * a3;
do
{
v6 = *(void **)((char *)*a1 + v4);
v4 += 8LL;
free(v6);
}
while ( v4 != v5 );
}
free(*a1);
*a1 = 0LL;
}

original_code:
void freeMatrix(double*** matrix, int nRows, int nCols)
{
  int i;
  for (i=0; i<nCols; i++)
  {
    free((*matrix)[i]);
  }
  free(*matrix);
  *matrix = ((void *)0);
}

canonical_original_code:
void freeMatrix(double ***matrix, int nRows, int nCols)
{
  int i;
  for (i=0; i<nCols; i++)
  {
    free((*matrix)[i]);
  }
  free(*matrix);
  *matrix = ((void *)0);
}


bisect

decompiled_code:
double __fastcall bisect(__int64 a1, int a2, int a3, double a4)
{
int v5; // ecx
int v6; // eax
double v7; // xmm0_8

v5 = a3;
if ( a2 > a3 )
return (double)(a2 + v5) * 0.5;
while ( 1 )
{
while ( 1 )
{
v6 = (a2 + v5) / 2;
v7 = *(double *)(a1 + 8LL * v6);
if ( v7 <= a4 )
break;
v5 = v6 - 1;
if ( v6 - 1 < a2 )
return (double)(a2 + v5) * 0.5;
}
if ( a4 <= v7 )
break;
a2 = v6 + 1;
if ( v5 < v6 + 1 )
return (double)(a2 + v5) * 0.5;
}
return (double)v6;
}

canonical_decompiled_code:
double __fastcall func5(__int64 a1, int a2, int a3, double a4)
{
int v5; // ecx
int v6; // eax
double v7; // xmm0_8

v5 = a3;
if ( a2 > a3 )
return (double)(a2 + v5) * 0.5;
while ( 1 )
{
while ( 1 )
{
v6 = (a2 + v5) / 2;
v7 = *(double *)(a1 + 8LL * v6);
if ( v7 <= a4 )
break;
v5 = v6 - 1;
if ( v6 - 1 < a2 )
return (double)(a2 + v5) * 0.5;
}
if ( a4 <= v7 )
break;
a2 = v6 + 1;
if ( v5 < v6 + 1 )
return (double)(a2 + v5) * 0.5;
}
return (double)v6;
}

original_code:
double bisect(double x, double data[], int start, int end)
{
  int mid;
  while (start <= end) {
    mid = (start + end)/2;
    if (x < data[mid])
      end = mid - 1;
    else if (x > data[mid])
      start = mid + 1;
    else
      return mid;
  }
  return (double) (start+end)/2;
}

variable_types: x double

variable_types:
data
double []

variable_types: start int

variable_types: end int

variable_types: mid int


decompiled_code:
double __fastcall quantile_int(double *a1, int a2, double a3)
{
if ( *a1 > a3 )
return 0.0;
if ( a3 < a1[a2 - 1] )
return bisect((__int64)a1, 0, a2, a3) + 1.0;
return (double)a2;
}

canonical_decompiled_code:
double __fastcall func6(double *a1, int a2, double a3)
{
if ( *a1 > a3 )
return 0.0;
if ( a3 < a1[a2 - 1] )
return func5((__int64)a1, 0, a2, a3) + 1.0;
return (double)a2;
}

original_code:
double quantile_int(double x, double data[], int n)
{
  if (x < data[0])
    return 0;
  else if (x >= data[n-1])
    return n;
  return (1+bisect(x, data, 0, n));
}


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

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

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

canonical_original_code:
int compare_dbl(void *a, void *b)
{
  return (*(double *)a - *(double *)b);
}

variable_types:
a
void *

variable_types:
b
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r15
int v4; // er13
unsigned int v5; // er12
__int64 v6; // rbp
int v7; // eax
int v8; // eax
double v9; // xmm0_8
__int64 v10; // xmm2_8
double v11; // xmm1_8
double v12; // xmm1_8
double v13; // xmm0_8
double v14; // xmm0_8
int v16; // eax
__int64 v17; // r14
const char *v18; // rdi
__int64 v19; // rsi
const char *v20; // rdx
int v21; // ebx
int v22; // eax
double v23; // xmm0_8
void *v24; // rax
void *v25; // r12
__int64 v26; // r15
int *v27; // rdi
double *v28; // rdx
__int64 v29; // rax
int v30; // ebx
__int64 v31; // rsi
_QWORD *v32; // rsi
__int64 i; // rax
__int64 v34; // rcx
void *v35; // r15
double *v36; // rax
double *v37; // r13
double v38; // xmm0_8
double *v39; // rbx
double v40; // xmm0_8
char *v41; // [rsp+0h] [rbp-C8h]
const char *v42; // [rsp+0h] [rbp-C8h]
double v43; // [rsp+8h] [rbp-C0h]
int v44; // [rsp+10h] [rbp-B8h]
double v45; // [rsp+10h] [rbp-B8h]
double v46; // [rsp+18h] [rbp-B0h]
double v47; // [rsp+18h] [rbp-B0h]
double x; // [rsp+20h] [rbp-A8h]
unsigned int xa; // [rsp+20h] [rbp-A8h]
__int64 v50; // [rsp+28h] [rbp-A0h]
const char *v51; // [rsp+28h] [rbp-A0h]
int v52; // [rsp+34h] [rbp-94h]
char *ptra; // [rsp+38h] [rbp-90h]
void *ptr; // [rsp+38h] [rbp-90h]
const char *v55; // [rsp+48h] [rbp-80h]
void *v56; // [rsp+50h] [rbp-78h] BYREF
void *v57; // [rsp+58h] [rbp-70h] BYREF
double v58; // [rsp+60h] [rbp-68h] BYREF
double v59; // [rsp+68h] [rbp-60h] BYREF
char v60[8]; // [rsp+70h] [rbp-58h] BYREF
double v61; // [rsp+78h] [rbp-50h] BYREF
double v62; // [rsp+80h] [rbp-48h] BYREF
double v63[8]; // [rsp+88h] [rbp-40h] BYREF

v3 = 0;
v4 = 0;
v5 = 1;
v6 = 8LL;
v44 = 10;
v41 = 0LL;
v46 = *(double *)&LC1;
v43 = *(double *)&LC2;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "c:d:i:m:n:s:t:h");
if ( v7 != -1 )
{
switch ( v7 )
{
case '?':
if ( ((*_ctype_b_loc())[optopt] & 0x4000) != 0 )
fprintf(stderr, "Unknown argument '%c'\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
goto LABEL_44;
case 'c':
v5 = 0;
v16 = atoi(optarg);
if ( v16 >= 0 )
v5 = v16;
continue;
case 'd':
v43 = fmax(1.0, atof(optarg));
continue;
case 'h':
puts(kHelpMessage);
return 0;
case 'i':
v3 = 1;
v41 = optarg;
continue;
case 'm':
v46 = atof(optarg);
continue;
case 'n':
v44 = atoi(optarg);
continue;
case 's':
v8 = atoi(optarg);
v6 = (unsigned int)v8;
x = (double)v8;
v9 = log2((double)v8);
v10 = LC4;
v11 = v9;
if ( COERCE_DOUBLE(*(_QWORD *)&v9 & LC4) < 4.503599627370496e15 )
{
v10 = ~LC4 & *(_QWORD *)&v9;
*(_QWORD *)&v11 = COERCE_UNSIGNED_INT64((double)(int)v9) | v10;
}
v12 = v11 - log2(x);
if ( v12 > 1.0e-16 )
{
v13 = log2(x);
v14 = trunc(v13);
v6 = (unsigned int)(int)gsl_pow_int((unsigned int)(int)v14, 2.0, v12, *(double *)&v10, x);
}
continue;
case 't':
v4 = atoi(optarg);
if ( v4 < 0 )
v4 = 1;
continue;
default:
abort();
}
}
break;
}
if ( argc - optind <= 5 )
{
fwrite("Not enough arguments\n", 1uLL, 0x15uLL, stderr);
LABEL_44:
exit(1);
}
v50 = optind;
v55 = argv[v50];
xa = atoi(argv[v50 + 1]);
v17 = (unsigned int)atoi(argv[v50 + 2]);
v18 = argv[v50 + 4];
ptra = (char *)(v50 * 8);
v51 = argv[v50 + 3];
v52 = atoi(v18);
v19 = *(_QWORD *)&ptra[(_QWORD)argv + 40];
v20 = v41;
if ( !v3 )
v20 = v51;
v42 = v20;
ptr = malloc(8LL * (int)(v17 * xa));
checkPtr(ptr, "out of memory");
readToDoubleMatrix(v55, xa, (unsigned int)v17, ptr);
v56 = malloc(8LL * v52);
if ( !v56 )
goto LABEL_48;
v21 = readToDoubleVectorDynamic(v51, (unsigned int)v52, (unsigned int)v4, &v56);
v57 = malloc(8LL * v52);
if ( !v57 )
goto LABEL_48;
v22 = readToDoubleVectorDynamic(v51, (unsigned int)v52, v5, &v57);
v23 = *(double *)v57;
if ( v21 != v22 )
{
fwrite("Error -- differing number of entries ", 1uLL, 0x25uLL, stderr);
fwrite("in time and data columns\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
v24 = malloc(8LL * ((int)v17 - 1));
v25 = v24;
if ( !v24 )
{
LABEL_48:
fwrite("Error -- out of memory\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
v26 = v21;
readToDoubleVector(v19, (unsigned int)(v17 - 1), 0LL, v24, v23);
v27 = (int *)calloc(4uLL, v21);
if ( v21 <= 0 )
{
if ( v44 <= 0 )
goto LABEL_46;
v30 = 0;
LABEL_50:
fprintf(stderr, "Error -- read %d obs, minimum to process is %d\n", (unsigned int)v30, (unsigned int)v44);
exit(1);
}
v28 = (double *)v57;
v29 = 0LL;
v30 = 0;
do
{
if ( v46 != v28[v29] )
{
v31 = v30++;
v27[v31] = v29;
}
++v29;
}
while ( v29 != v26 );
if ( v44 > v30 )
goto LABEL_50;
if ( v30 <= 0 )
{
LABEL_46:
v30 = 0;
goto LABEL_36;
}
v32 = v56;
for ( i = 0LL; i != v30; v32[i++] = v32[v34] )
{
v34 = v27[i];
v28[i] = v28[v34];
}
LABEL_36:
v35 = malloc(8LL * (int)v6);
if ( !v35 )
goto LABEL_48;
v36 = (double *)malloc(8LL * ((int)v17 - 1) + 8);
v37 = v36;
if ( !v36 )
goto LABEL_48;
lmT(ptr, xa, (unsigned int)v17, v57, (unsigned int)v30, v56, v43, 0.000000001, v25, v17, 1000LL, &v61, &v62, v36, v63);
lmT(ptr, xa, (unsigned int)v17, v57, (unsigned int)v30, v56, v43, 0.000000001, v25, v6, 1000LL, &v58, &v59, v35, v60);
v47 = v62 - v59;
v45 = v61 - v58;
fprintf(stdout, "%s %d %d %d %g ", v42, (unsigned int)v30, (unsigned int)v17, (unsigned int)v6, v43);
fprintf(stdout, "%g %g ", v47 + v47, v45);
v38 = sqrt(v63[0]);
fprintf(stdout, "%g ", v38);
if ( (int)v17 > 0 )
{
v39 = v37;
do
{
v40 = *v39++;
fprintf(stdout, "%g ", v40);
}
while ( &v37[(unsigned int)v17] != v39 );
}
fputc(10, stdout);
free(ptr);
free(v56);
free(v57);
free(v35);
free(v37);
free(v25);
return 0;
}

original_code:
int main(int argc, char * argv[]) {
  const int nArgs = 6;
  int c, timeCol=0, valueCol=1;
  extern char *optarg;
  char * dataFile, * basisFile, * priorFile, * idString=((void *)0);
  short readID = 0;
  int kSmooth = 8;
  int maxIter = 1e3;
  double nu=5;
  double tol=1e-9;
  double missingCode = 99.999;
  int minObs = 10;
  int basisRows, basisCols, dataRows;
  int i;
  while ( (c=getopt(argc, argv, "c:d:i:m:n:s:t:h")) != -1 ) {
    switch(c) {
      case 'h':
        puts(kHelpMessage);
        return 0;
      case 'c':
        valueCol = atoi(optarg);
        valueCol = (valueCol < 0) ? 0 : valueCol;
        break;
      case 'd':
        nu = atof(optarg);
        nu = (nu < 1) ? 1 : nu;
        break;
      case 'i':
        idString = optarg;
        readID = 1;
        break;
      case 'm':
        missingCode = atof(optarg);
        break;
      case 'n':
        minObs = atoi(optarg);
        break;
      case 's':
        kSmooth = atoi(optarg);
        kSmooth = (trunc(log2(kSmooth))-log2(kSmooth) > 1e-16) ?
          (int) gsl_pow_int(2, trunc(log2(kSmooth))) : kSmooth;
        break;
      case 't':
        timeCol = atoi(optarg);
        timeCol = (timeCol < 0) ? 1 : timeCol;
        break;
      case '?':
        if ( ((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint) )
          fprintf(stderr, "Unknown argument '%c'\n", optopt);
        else
          fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
        exit(1);
        break;
      default:
        abort();
    }
  }
  if (argc-optind < nArgs) {
    fprintf(stderr, "Not enough arguments\n");
    exit(1);
  }
  basisFile = argv[optind];
  basisRows = atoi( argv[optind+1] );
  basisCols = atoi( argv[optind+2] );
  dataFile = argv[optind+3];
  dataRows = atoi( argv[optind+4] );
  priorFile = argv[optind + 5];
  if (readID == 0)
  {
    idString = dataFile;
  }
  double* basisMat;
  basisMat = malloc(basisCols * basisRows * sizeof(double));
  checkPtr(basisMat, "out of memory");
  readToDoubleMatrix(basisFile, basisRows, basisCols, basisMat);
  double* timeVec;
  timeVec = malloc(dataRows * sizeof(double));
  if (timeVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int timesRead = readToDoubleVectorDynamic(dataFile, dataRows,
      timeCol, &timeVec);
  if (__builtin_isnan (timeVec[0]))
  {
    fprintf(stderr, "Error -- NaN at first time; aborting\n");
    exit(1);
  }
  double* yVec;
  yVec = malloc(dataRows * sizeof(double));
  if (yVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int obsRead = readToDoubleVectorDynamic(dataFile, dataRows,
      valueCol, &yVec);
  if (__builtin_isnan (yVec[0]))
  {
    fprintf(stderr, "Error -- NaN at first observation; aborting\n");
    exit(1);
  }
  if (timesRead != obsRead)
  {
    fprintf(stderr, "Error -- differing number of entries ");
    fprintf(stderr, "in time and data columns\n");
    exit(1);
  }
  double * priorVec;
  priorVec = malloc( (basisCols-1) * sizeof(double));
  if (priorVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  readToDoubleVector(priorFile, basisCols-1, 0, priorVec);
  int nObs = 0;
  int * validInd;
  validInd = calloc(sizeof(int), obsRead);
  for (i=0; i<obsRead; i++)
  {
    if (yVec[i] != missingCode)
    {
      validInd[nObs]=i;
      nObs++;
    }
  }
  if (nObs < minObs)
  {
    fprintf(stderr, "Error -- read %d obs, minimum to process is %d\n",
        nObs, minObs);
    exit(1);
  }
  for (i=0; i<nObs; i++)
  {
    yVec[i] = yVec[validInd[i]];
    timeVec[i] = timeVec[validInd[i]];
  }
  double logPosterior_l, logLikelihood_l, tau_l;
  double logPosterior_m, logLikelihood_m, tau_m;
  double * coef_l, * coef_m;
  coef_l = malloc(kSmooth * sizeof(double));
  if (coef_l==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  coef_m = malloc(basisCols * sizeof(double));
  if (coef_m==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int iter_m;
  iter_m = lmT(basisMat, basisRows, basisCols,
      yVec, nObs,
      timeVec,
      priorVec,
      nu, basisCols,
      maxIter, tol,
      &logPosterior_m,
      &logLikelihood_m,
      coef_m,
      &tau_m);
  int iter_l;
  iter_l = lmT(basisMat, basisRows, basisCols,
      yVec, nObs,
      timeVec,
      priorVec,
      nu, kSmooth,
      maxIter, tol,
      &logPosterior_l,
      &logLikelihood_l,
      coef_l,
      &tau_l);
  double llr, lpr;
  llr = logLikelihood_m - logLikelihood_l;
  llr *= 2;
  lpr = logPosterior_m - logPosterior_l;
  fprintf(stdout, "%s %d %d %d %g ", idString, nObs, basisCols, kSmooth, nu);
  fprintf(stdout, "%g %g ", llr, lpr);
  fprintf(stdout, "%g ", sqrt(tau_m));
  for (i=0; i<basisCols; i++) fprintf(stdout, "%g ", coef_m[i]);
  fprintf(stdout, "\n");
  free(basisMat);
  basisMat = ((void *)0);
  free(timeVec);
  timeVec=((void *)0);
  free(yVec);
  yVec=((void *)0);
  free(coef_l);
  coef_l=((void *)0);
  free(coef_m);
  coef_m=((void *)0);
  free(priorVec);
  priorVec=((void *)0);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int nArgs = 6;
  int c; int timeCol = 0; int valueCol = 1;
  extern char *optarg;
  char *dataFile; char *basisFile; char *priorFile; char *idString = ((void *)0);
  short readID = 0;
  int kSmooth = 8;
  int maxIter = 1e3;
  double nu = 5;
  double tol = 1e-9;
  double missingCode = 99.999;
  int minObs = 10;
  int basisRows; int basisCols; int dataRows;
  int i;
  while ( (c=getopt(argc, argv, "c:d:i:m:n:s:t:h")) != -1 ) {
    switch(c) {
      case 'h':
        puts(kHelpMessage);
        return 0;
      case 'c':
        valueCol = atoi(optarg);
        valueCol = (valueCol < 0) ? 0 : valueCol;
        break;
      case 'd':
        nu = atof(optarg);
        nu = (nu < 1) ? 1 : nu;
        break;
      case 'i':
        idString = optarg;
        readID = 1;
        break;
      case 'm':
        missingCode = atof(optarg);
        break;
      case 'n':
        minObs = atoi(optarg);
        break;
      case 's':
        kSmooth = atoi(optarg);
        kSmooth = (trunc(log2(kSmooth))-log2(kSmooth) > 1e-16) ?
          (int) gsl_pow_int(2, trunc(log2(kSmooth))) : kSmooth;
        break;
      case 't':
        timeCol = atoi(optarg);
        timeCol = (timeCol < 0) ? 1 : timeCol;
        break;
      case '?':
        if ( ((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint) )
          fprintf(stderr, "Unknown argument '%c'\n", optopt);
        else
          fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
        exit(1);
        break;
      default:
        abort();
    }
  }
  if (argc-optind < nArgs) {
    fprintf(stderr, "Not enough arguments\n");
    exit(1);
  }
  basisFile = argv[optind];
  basisRows = atoi( argv[optind+1] );
  basisCols = atoi( argv[optind+2] );
  dataFile = argv[optind+3];
  dataRows = atoi( argv[optind+4] );
  priorFile = argv[optind + 5];
  if (readID == 0)
  {
    idString = dataFile;
  }
  double *basisMat;
  basisMat = malloc(basisCols * basisRows * sizeof(double));
  checkPtr(basisMat, "out of memory");
  readToDoubleMatrix(basisFile, basisRows, basisCols, basisMat);
  double *timeVec;
  timeVec = malloc(dataRows * sizeof(double));
  if (timeVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int timesRead = readToDoubleVectorDynamic(dataFile, dataRows,
      timeCol, &timeVec);
  if (__builtin_isnan (timeVec[0]))
  {
    fprintf(stderr, "Error -- NaN at first time; aborting\n");
    exit(1);
  }
  double *yVec;
  yVec = malloc(dataRows * sizeof(double));
  if (yVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int obsRead = readToDoubleVectorDynamic(dataFile, dataRows,
      valueCol, &yVec);
  if (__builtin_isnan (yVec[0]))
  {
    fprintf(stderr, "Error -- NaN at first observation; aborting\n");
    exit(1);
  }
  if (timesRead != obsRead)
  {
    fprintf(stderr, "Error -- differing number of entries ");
    fprintf(stderr, "in time and data columns\n");
    exit(1);
  }
  double *priorVec;
  priorVec = malloc( (basisCols-1) * sizeof(double));
  if (priorVec==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  readToDoubleVector(priorFile, basisCols-1, 0, priorVec);
  int nObs = 0;
  int *validInd;
  validInd = calloc(sizeof(int), obsRead);
  for (i=0; i<obsRead; i++)
  {
    if (yVec[i] != missingCode)
    {
      validInd[nObs]=i;
      nObs++;
    }
  }
  if (nObs < minObs)
  {
    fprintf(stderr, "Error -- read %d obs, minimum to process is %d\n",
        nObs, minObs);
    exit(1);
  }
  for (i=0; i<nObs; i++)
  {
    yVec[i] = yVec[validInd[i]];
    timeVec[i] = timeVec[validInd[i]];
  }
  double logPosterior_l; double logLikelihood_l; double tau_l;
  double logPosterior_m; double logLikelihood_m; double tau_m;
  double *coef_l; double *coef_m;
  coef_l = malloc(kSmooth * sizeof(double));
  if (coef_l==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  coef_m = malloc(basisCols * sizeof(double));
  if (coef_m==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int iter_m;
  iter_m = lmT(basisMat, basisRows, basisCols,
      yVec, nObs,
      timeVec,
      priorVec,
      nu, basisCols,
      maxIter, tol,
      &logPosterior_m,
      &logLikelihood_m,
      coef_m,
      &tau_m);
  int iter_l;
  iter_l = lmT(basisMat, basisRows, basisCols,
      yVec, nObs,
      timeVec,
      priorVec,
      nu, kSmooth,
      maxIter, tol,
      &logPosterior_l,
      &logLikelihood_l,
      coef_l,
      &tau_l);
  double llr; double lpr;
  llr = logLikelihood_m - logLikelihood_l;
  llr *= 2;
  lpr = logPosterior_m - logPosterior_l;
  fprintf(stdout, "%s %d %d %d %g ", idString, nObs, basisCols, kSmooth, nu);
  fprintf(stdout, "%g %g ", llr, lpr);
  fprintf(stdout, "%g ", sqrt(tau_m));
  for (i=0; i<basisCols; i++) fprintf(stdout, "%g ", coef_m[i]);
  fprintf(stdout, "\n");
  free(basisMat);
  basisMat = ((void *)0);
  free(timeVec);
  timeVec=((void *)0);
  free(yVec);
  yVec=((void *)0);
  free(coef_l);
  coef_l=((void *)0);
  free(coef_m);
  coef_m=((void *)0);
  free(priorVec);
  priorVec=((void *)0);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: nArgs int

variable_types: c int

variable_types: timeCol int

variable_types: valueCol int

variable_types:
optarg
char *

variable_types:
dataFile
char *

variable_types:
basisFile
char *

variable_types:
priorFile
char *

variable_types:
idString
char *

variable_types: readID short

variable_types: kSmooth int

variable_types: maxIter int

variable_types: nu double

variable_types: tol double

variable_types: missingCode double

variable_types: minObs int

variable_types: basisRows int

variable_types: basisCols int

variable_types: dataRows int

variable_types:
basisMat
double *

variable_types:
timeVec
double *

variable_types: timesRead int

variable_types:
yVec
double *

variable_types: obsRead int

variable_types:
priorVec
double *

variable_types: nObs int

variable_types:
validInd
int *

variable_types: llr double

variable_types: lpr double


dscal

decompiled_code:
__int64 __fastcall dscal(int a1, __int64 a2, int a3, double a4)
{
double v5; // [rsp+0h] [rbp-18h] BYREF
int v6; // [rsp+8h] [rbp-10h] BYREF
int v7[3]; // [rsp+Ch] [rbp-Ch] BYREF

v7[0] = a1;
v6 = a3;
v5 = a4;
return dscal_(v7, &v5, a2, &v6);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, int a3, double a4)
{
double v5; // [rsp+0h] [rbp-18h] BYREF
int v6; // [rsp+8h] [rbp-10h] BYREF
int v7[3]; // [rsp+Ch] [rbp-Ch] BYREF

v7[0] = a1;
v6 = a3;
v5 = a4;
return dscal_(v7, &v5, a2, &v6);
}

original_code:
void dscal(int N, double ALPHA, double * X, int INCX)
{
  dscal_(&N, &ALPHA, X, &INCX);
}

canonical_original_code:
void dscal(int N, double ALPHA, double *X, int INCX)
{
  dscal_(&N, &ALPHA, X, &INCX);
}

variable_types: N int

variable_types: ALPHA double

variable_types:
X
double *

variable_types: INCX int


dasum

decompiled_code:
__int64 __fastcall dasum(int a1, __int64 a2, int a3)
{
int v4; // [rsp+8h] [rbp-10h] BYREF
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a1;
v4 = a3;
return dasum_(v5, a2, &v4);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, int a3)
{
int v4; // [rsp+8h] [rbp-10h] BYREF
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a1;
v4 = a3;
return dasum_(v5, a2, &v4);
}

original_code:
double dasum(int N, double * X, int INCX)
{
  return dasum_(&N, X, &INCX);
}

canonical_original_code:
double dasum(int N, double *X, int INCX)
{
  return dasum_(&N, X, &INCX);
}


dcopy

decompiled_code:
__int64 __fastcall dcopy(int a1, __int64 a2, int a3, __int64 a4, int a5)
{
int v6; // [rsp+4h] [rbp-14h] BYREF
int v7; // [rsp+8h] [rbp-10h] BYREF
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = a1;
v7 = a3;
v6 = a5;
return dcopy_(v8, a2, &v7, a4, &v6);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2, int a3, __int64 a4, int a5)
{
int v6; // [rsp+4h] [rbp-14h] BYREF
int v7; // [rsp+8h] [rbp-10h] BYREF
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = a1;
v7 = a3;
v6 = a5;
return dcopy_(v8, a2, &v7, a4, &v6);
}

original_code:
void dcopy(int N, double * X, int INCX, double * Y, int INCY)
{
  dcopy_(&N, X, &INCX, Y, &INCY);
}

canonical_original_code:
void dcopy(int N, double *X, int INCX, double *Y, int INCY)
{
  dcopy_(&N, X, &INCX, Y, &INCY);
}

variable_types:
Y
double *

variable_types: INCY int


daxpy

decompiled_code:
__int64 __fastcall daxpy(int a1, __int64 a2, int a3, __int64 a4, int a5, double a6)
{
int v7; // [rsp+Ch] [rbp-1Ch] BYREF
double v8; // [rsp+10h] [rbp-18h] BYREF
int v9; // [rsp+18h] [rbp-10h] BYREF
int v10[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v10[0] = a1;
v9 = a3;
v7 = a5;
v8 = a6;
return daxpy_(v10, &v8, a2, &v9, a4, &v7);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2, int a3, __int64 a4, int a5, double a6)
{
int v7; // [rsp+Ch] [rbp-1Ch] BYREF
double v8; // [rsp+10h] [rbp-18h] BYREF
int v9; // [rsp+18h] [rbp-10h] BYREF
int v10[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v10[0] = a1;
v9 = a3;
v7 = a5;
v8 = a6;
return daxpy_(v10, &v8, a2, &v9, a4, &v7);
}

original_code:
void daxpy(int N, double ALPHA, double * X, int INCX, double * Y, int INCY)
{
  daxpy_(&N, &ALPHA, X, &INCX, Y, &INCY);
}

canonical_original_code:
void daxpy(int N, double ALPHA, double *X, int INCX, double *Y, int INCY)
{
  daxpy_(&N, &ALPHA, X, &INCX, Y, &INCY);
}


dgemv

decompiled_code:
__int64 __fastcall dgemv(char a1, int a2, int a3, __int64 a4, int a5, __int64 a6, double a7, double a8, char a9, __int64 a10, char a11)
{
double v12; // [rsp+8h] [rbp-28h] BYREF
double v13; // [rsp+10h] [rbp-20h] BYREF
int v14; // [rsp+18h] [rbp-18h] BYREF
int v15; // [rsp+1Ch] [rbp-14h] BYREF
int v16; // [rsp+20h] [rbp-10h] BYREF
char v17[12]; // [rsp+24h] [rbp-Ch] BYREF

v16 = a2;
v15 = a3;
v14 = a5;
v17[0] = a1;
v13 = a7;
v12 = a8;
return dgemv_(v17, &v16, &v15, &v13, a4, &v14, a6, &a9, &v12, a10, &a11);
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, int a2, int a3, __int64 a4, int a5, __int64 a6, double a7, double a8, char a9, __int64 a10, char a11)
{
double v12; // [rsp+8h] [rbp-28h] BYREF
double v13; // [rsp+10h] [rbp-20h] BYREF
int v14; // [rsp+18h] [rbp-18h] BYREF
int v15; // [rsp+1Ch] [rbp-14h] BYREF
int v16; // [rsp+20h] [rbp-10h] BYREF
char v17[12]; // [rsp+24h] [rbp-Ch] BYREF

v16 = a2;
v15 = a3;
v14 = a5;
v17[0] = a1;
v13 = a7;
v12 = a8;
return dgemv_(v17, &v16, &v15, &v13, a4, &v14, a6, &a9, &v12, a10, &a11);
}

original_code:
void dgemv(char TRANS, int M, int N, double ALPHA, double * A, int LDA,
    double * X, int INCX, double BETA, double * Y, int INCY)
{
  dgemv_(&TRANS, &M, &N, &ALPHA, A, &LDA, X, &INCX, &BETA, Y, &INCY);
}

canonical_original_code:
void dgemv(char TRANS, int M, int N, double ALPHA, double *A, int LDA, double *X, int INCX, double BETA, double *Y, int INCY)
{
  dgemv_(&TRANS, &M, &N, &ALPHA, A, &LDA, X, &INCX, &BETA, Y, &INCY);
}

variable_types: TRANS char

variable_types: M int

variable_types:
A
double *

variable_types: LDA int

variable_types: BETA double


dsyrk

decompiled_code:
__int64 __fastcall dsyrk(char a1, char a2, int a3, int a4, __int64 a5, int a6, double a7, double a8, __int64 a9, char a10)
{
double v11; // [rsp+8h] [rbp-30h] BYREF
int v12; // [rsp+14h] [rbp-24h] BYREF
double v13; // [rsp+18h] [rbp-20h] BYREF
int v14; // [rsp+20h] [rbp-18h] BYREF
int v15; // [rsp+24h] [rbp-14h] BYREF
char v16[4]; // [rsp+28h] [rbp-10h] BYREF
char v17[12]; // [rsp+2Ch] [rbp-Ch] BYREF

v15 = a3;
v14 = a4;
v12 = a6;
v17[0] = a1;
v16[0] = a2;
v13 = a7;
v11 = a8;
return dsyrk_(v17, v16, &v15, &v14, &v13, a5, &v12, &v11, a9, &a10);
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1, char a2, int a3, int a4, __int64 a5, int a6, double a7, double a8, __int64 a9, char a10)
{
double v11; // [rsp+8h] [rbp-30h] BYREF
int v12; // [rsp+14h] [rbp-24h] BYREF
double v13; // [rsp+18h] [rbp-20h] BYREF
int v14; // [rsp+20h] [rbp-18h] BYREF
int v15; // [rsp+24h] [rbp-14h] BYREF
char v16[4]; // [rsp+28h] [rbp-10h] BYREF
char v17[12]; // [rsp+2Ch] [rbp-Ch] BYREF

v15 = a3;
v14 = a4;
v12 = a6;
v17[0] = a1;
v16[0] = a2;
v13 = a7;
v11 = a8;
return dsyrk_(v17, v16, &v15, &v14, &v13, a5, &v12, &v11, a9, &a10);
}

original_code:
void dsyrk(char UPLO, char TRANS, int N, int K, double ALPHA,
    double* A, int LDA, double BETA, double* C, int LDC)
{
  dsyrk_(&UPLO, &TRANS, &N, &K, &ALPHA, A, &LDA, &BETA, C, &LDC);
}

canonical_original_code:
void dsyrk(char UPLO, char TRANS, int N, int K, double ALPHA, double *A, int LDA, double BETA, double *C, int LDC)
{
  dsyrk_(&UPLO, &TRANS, &N, &K, &ALPHA, A, &LDA, &BETA, C, &LDC);
}

variable_types: UPLO char

variable_types: K int

variable_types:
C
double *

variable_types: LDC int


dposv

decompiled_code:
__int64 __fastcall dposv(char a1, int a2, int a3, __int64 a4, int a5)
{
int v6; // [rsp+0h] [rbp-28h] BYREF
int v7; // [rsp+4h] [rbp-24h] BYREF
int v8; // [rsp+8h] [rbp-20h] BYREF
char v9[16]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned int v10; // [rsp+1Ch] [rbp-Ch]

v8 = a2;
v7 = a3;
v6 = a5;
v9[0] = a1;
dposv_(v9, &v8, &v7, a4, &v6);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1, int a2, int a3, __int64 a4, int a5)
{
int v6; // [rsp+0h] [rbp-28h] BYREF
int v7; // [rsp+4h] [rbp-24h] BYREF
int v8; // [rsp+8h] [rbp-20h] BYREF
char v9[16]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned int v10; // [rsp+1Ch] [rbp-Ch]

v8 = a2;
v7 = a3;
v6 = a5;
v9[0] = a1;
dposv_(v9, &v8, &v7, a4, &v6);
return v10;
}

original_code:
int dposv(char UPLO, int N, int NRHS, double * A, int LDA,
    double * B, int LDB)
{
  int INFO;
  dposv_(&UPLO, &N, &NRHS, A, &LDA, B, &LDB, &INFO);
  return INFO;
}

canonical_original_code:
int dposv(char UPLO, int N, int NRHS, double *A, int LDA, double *B, int LDB)
{
  int INFO;
  dposv_(&UPLO, &N, &NRHS, A, &LDA, B, &LDB, &INFO);
  return INFO;
}

variable_types: NRHS int

variable_types:
B
double *

variable_types: LDB int

variable_types: INFO int


dgels

decompiled_code:
__int64 __fastcall dgels(char a1, int a2, int a3, unsigned int a4, __int64 a5, int a6, __int64 a7, char a8)
{
void *v9; // rax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v12; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+14h] [rbp-34h] BYREF
int v14; // [rsp+18h] [rbp-30h] BYREF
char v15[12]; // [rsp+1Ch] [rbp-2Ch] BYREF
unsigned int v16; // [rsp+28h] [rbp-20h] BYREF
unsigned int v17[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

v14 = a2;
v15[0] = a1;
v13 = a3;
v12 = a4;
v11 = a6;
v16 = a2 * a3 + ((unsigned int)max((unsigned int)(a2 * a3), a4) << 10);
v9 = malloc(8LL * (int)v16);
if ( !v9 )
{
fwrite("Error -- out of memory\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
dgels_(v15, &v14, &v13, &v12, a5, &v11, a7, &a8, v9, &v16, v17);
return v17[0];
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1, int a2, int a3, unsigned int a4, __int64 a5, int a6, __int64 a7, char a8)
{
void *v9; // rax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v12; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+14h] [rbp-34h] BYREF
int v14; // [rsp+18h] [rbp-30h] BYREF
char v15[12]; // [rsp+1Ch] [rbp-2Ch] BYREF
unsigned int v16; // [rsp+28h] [rbp-20h] BYREF
unsigned int v17[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

v14 = a2;
v15[0] = a1;
v13 = a3;
v12 = a4;
v11 = a6;
v16 = a2 * a3 + ((unsigned int)max((unsigned int)(a2 * a3), a4) << 10);
v9 = malloc(8LL * (int)v16);
if ( !v9 )
{
fwrite("Error -- out of memory\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
dgels_(v15, &v14, &v13, &v12, a5, &v11, a7, &a8, v9, &v16, v17);
return v17[0];
}

original_code:
int dgels(char TRANS, int M, int N, int NRHS, double * A, int LDA,
    double * B, int LDB)
{
  const int NB=1024;
  int LWORK;
  LWORK = M*N + max(M*N, NRHS)*NB;
  double * WORK;
  WORK = malloc(LWORK * sizeof(double));
  if (WORK==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int INFO;
  dgels_(&TRANS, &M, &N, &NRHS, A, &LDA, B, &LDB, WORK, &LWORK, &INFO);
  return INFO;
}

canonical_original_code:
int dgels(char TRANS, int M, int N, int NRHS, double *A, int LDA, double *B, int LDB)
{
  int NB = 1024;
  int LWORK;
  LWORK = M*N + max(M*N, NRHS)*NB;
  double *WORK;
  WORK = malloc(LWORK * sizeof(double));
  if (WORK==((void *)0))
  {
    fprintf(stderr, "Error -- out of memory\n");
    exit(1);
  }
  int INFO;
  dgels_(&TRANS, &M, &N, &NRHS, A, &LDA, B, &LDB, WORK, &LWORK, &INFO);
  return INFO;
}

variable_types: N int

variable_types: NB int

variable_types: LWORK int

variable_types:
WORK
double *


wls

original_code:
int wls(double* X, int n, int k,
    double* y, double* w,
    double* XTX, double *sqw, double* sqwX, double* sqwy,
    double* coef) {
  int i, j;
  for (i=0; i<n; i++)
  {
    sqw[i] = sqrt(w[i]);
    for (j=0; j<k; j++) {
      sqwX[i + j*n] = X[i + j*n] * sqw[i];
    }
    sqwy[i] = sqw[i] * y[i];
  }
  dsyrk('u', 't', k, n, 1, sqwX, n, 0, XTX, k);
  dgemv('t', n, k, 1, sqwX, n, sqwy, 1, 0, coef, 1);
  int info;
  info = dposv('u', k, 1, XTX, k, coef, k);
  return info;
}

canonical_original_code:
int wls(double *X, int n, int k, double *y, double *w, double *XTX, double *sqw, double *sqwX, double *sqwy, double *coef) {
  int i; int j;
  for (i=0; i<n; i++)
  {
    sqw[i] = sqrt(w[i]);
    for (j=0; j<k; j++) {
      sqwX[i + j*n] = X[i + j*n] * sqw[i];
    }
    sqwy[i] = sqw[i] * y[i];
  }
  dsyrk('u', 't', k, n, 1, sqwX, n, 0, XTX, k);
  dgemv('t', n, k, 1, sqwX, n, sqwy, 1, 0, coef, 1);
  int info;
  info = dposv('u', k, 1, XTX, k, coef, k);
  return info;
}

variable_types:
X
double *

variable_types: n int

variable_types: k int

variable_types:
y
double *

variable_types:
w
double *

variable_types:
XTX
double *

variable_types:
sqw
double *

variable_types:
sqwX
double *

variable_types:
sqwy
double *

variable_types:
coef
double *

variable_types: i int

variable_types: j int

variable_types: info int


calcFitted

decompiled_code:
__int64 __fastcall calcFitted(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
dgemv(110LL, a2, a3, a1, (unsigned int)a2, a5, 1.0, 0.0, 1LL, a6, 1);
return 0LL;
}

original_code:
int calcFitted(double* X, int n, int k,
    double* y,
    double* coef,
    double* fitted)
{
  dgemv('n', n, k, 1, X, n, coef, 1, 0, fitted, 1);
  return 0;
}

canonical_original_code:
int calcFitted(double *X, int n, int k, double *y, double *coef, double *fitted)
{
  dgemv('n', n, k, 1, X, n, coef, 1, 0, fitted, 1);
  return 0;
}

variable_types:
fitted
double *


calcResid

decompiled_code:
__int64 __fastcall calcResid(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
dgemv(110LL, a2, a3, a1, (unsigned int)a2, a5, 1.0, 0.0, 1LL, a6, 1);
dscal((unsigned int)a2, a6, 1LL, -1.0);
daxpy((unsigned int)a2, a4, 1LL, a6, 1LL, 1.0);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
dgemv(110LL, a2, a3, a1, (unsigned int)a2, a5, 1.0, 0.0, 1LL, a6, 1);
dscal((unsigned int)a2, a6, 1LL, -1.0);
daxpy((unsigned int)a2, a4, 1LL, a6, 1LL, 1.0);
return 0LL;
}

original_code:
int calcResid(double* X, int n, int k,
    double* y,
    double* coef,
    double* resid)
{
  dgemv('n', n, k, 1, X, n, coef, 1, 0, resid, 1);
  dscal(n, -1, resid, 1);
  daxpy(n, 1, y, 1, resid, 1);
  return 0;
}

canonical_original_code:
int calcResid(double *X, int n, int k, double *y, double *coef, double *resid)
{
  dgemv('n', n, k, 1, X, n, coef, 1, 0, resid, 1);
  dscal(n, -1, resid, 1);
  daxpy(n, 1, y, 1, resid, 1);
  return 0;
}

variable_types:
resid
double *